From nobody Sat Sep 21 05:51:18 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1569496967; cv=none; d=zoho.com; s=zohoarc; b=oU0XFdzlNjXrb68WvVeepZ+iMfuv+Wd0oE93rgwv3ES8fuLgNCwnPYqucNRex0vaD8FvRyM5KDP2+B5qOrfPr3YlQwVvdSFv9JMBMj9YQjj06hr0r+VVCHjJ7smH8gJ4fomv73djlLDePZtOilfRU+xpR56pl5praRPoUKPwBn0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1569496967; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=8oZtstGBPHfBRhTJOLVD7Pf0Af2AfbYbXSW4lfrNSJc=; b=GMscLH47LhVJ7glk+z5Kh/KJCjQXOAl6RWzPOSEc/XF7jsjondakbBuu1CqzzoV3LoDupvwQGF1LPHQbtWI2Ml3E2gVMHvU1yMwFyvy7Bg0NJE8NlculD9ES/76ppZ5A4/Qan+E+tuJRbqCkVGlQK1Cz9Ip/2C5AKgOnNr3Fozg= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1569496967845118.88105150911429; Thu, 26 Sep 2019 04:22:47 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iDRq4-00007M-PN; Thu, 26 Sep 2019 11:21:36 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iDRq2-00006N-Vi for xen-devel@lists.xenproject.org; Thu, 26 Sep 2019 11:21:35 +0000 Received: from mail-lj1-x241.google.com (unknown [2a00:1450:4864:20::241]) by localhost (Halon) with ESMTPS id b8712180-e04f-11e9-97fb-bc764e2007e4; Thu, 26 Sep 2019 11:21:01 +0000 (UTC) Received: by mail-lj1-x241.google.com with SMTP id y3so1758234ljj.6 for ; Thu, 26 Sep 2019 04:21:01 -0700 (PDT) Received: from otyshchenko.kyiv.epam.com (ll-22.209.223.85.sovam.net.ua. [85.223.209.22]) by smtp.gmail.com with ESMTPSA id n4sm526810lfi.63.2019.09.26.04.20.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 26 Sep 2019 04:21:00 -0700 (PDT) X-Inumbo-ID: b8712180-e04f-11e9-97fb-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=PAgFrd7DRogKgdXlH5r3pNcIc0Ff4MXaChYlPlJIims=; b=Wto/WeuIgHa0wM7vk1TiJlLH72WqP4Uq4jAMe4hJzUBsVlT/kykO9tdgMmF54CDOTi fSFHUrDc1EUG/3w9kMohQ/dhQ7+LkFvl1/Oqn5NVCb0pC1yOzrfcZaJ7hLcb1eL3fLrH BU73cJrEONiwzLvAPI9an3LJ4e3U1DFu4lSiVehxFz5j4SWYCDg7yGggF+X/uK7n37Ph pEJmdCc00FMK4f9QbKcr8SrSQXK0MJahb3IBlqFJEa4oXo4tjpjgx8G3kBibOiyRx0H9 t2cp9slCt039xTrAexK1JweLxImeVA19exEG+RVz2XJIw7nUIIabXqjPbevm3DfRzdNV +SjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=PAgFrd7DRogKgdXlH5r3pNcIc0Ff4MXaChYlPlJIims=; b=lf8Bopsp5wHalVg2bC/ivauUuf18G/bM6xDKZt+/HpBEIeTlec439Fzw2MH89toxqt 5+u+OFWtPVn7ifjO7vc8y6Fg+bz4OzbOKl89uaPyTbPqLQuAnRLA7ROEuYQPkMAD4VlL QAtegnNOuUMN8d/ro28X3kDGtt5ob8JxvdG3unG4MtyjpCulr8Ks12dsdcsmSyaD2/ZT pZskrhutjWY0vVpB/LmnMgZuLU7s+OLPEjav3J2b+jTEW6HkBs6OalOT7xmO6TFOCMqN PWsBPPcnKhJ5qr1t9mcbpzrmawuvyNFGSZtbUMEHsfwyctlOdo0go8/tS5+As8uXIVvk IAqg== X-Gm-Message-State: APjAAAUBg+iSntq5hrkpIoaZOkHgqdpvY1usYg9zoV3fAO8KxhrzzEkP VLQ2ZRNuoJ2fjkj6gH2FHvc3ya6ov7s= X-Google-Smtp-Source: APXvYqwke5/HRjiOv/DUYQWbi/+11g6Hd9TLOkdo22jGTnrtQ43uR/vvcVFPXcGQX8D1E1OJ/ZmBPQ== X-Received: by 2002:a2e:9f52:: with SMTP id v18mr2313022ljk.166.1569496860741; Thu, 26 Sep 2019 04:21:00 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Date: Thu, 26 Sep 2019 14:20:33 +0300 Message-Id: <1569496834-7796-8-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1569496834-7796-1-git-send-email-olekstysh@gmail.com> References: <1569496834-7796-1-git-send-email-olekstysh@gmail.com> Subject: [Xen-devel] [PATCH V6 7/8] iommu/arm: Introduce iommu_add_dt_device API X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Oleksandr Tyshchenko , julien.grall@arm.com, sstabellini@kernel.org, volodymyr_babchuk@epam.com, Jan Beulich MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) From: Oleksandr Tyshchenko The main puprose of this patch is to add a way to register DT device (which is behind the IOMMU) using the generic IOMMU DT bindings [1] before assigning that device to a domain. So, this patch adds new "iommu_add_dt_device" API for adding DT device to the IOMMU using generic IOMMU DT bindings and previously added "iommu_fwspec" support. As devices can be assigned to the hardware domain and other domains this function is called from two places: handle_device() and iommu_do_dt_domctl(). Besides that, this patch adds new "dt_xlate" callback (borrowed from Linux "of_xlate") for providing the driver with DT IOMMU specifier which describes the IOMMU master interfaces of that device (device IDs, etc= ). According to the generic IOMMU DT bindings the context of required properties for IOMMU device/master node (#iommu-cells, iommus) depends on many factors and is really driver depended thing. Please note, all IOMMU drivers which support generic IOMMU DT bindings should use "dt_xlate" and "add_device" callbacks. [1] https://www.kernel.org/doc/Documentation/devicetree/bindings/iommu/iomm= u.txt Signed-off-by: Oleksandr Tyshchenko CC: Julien Grall CC: Jan Beulich Acked-by: Jan Beulich Acked-by: Julien Grall --- Changes V5 -> V6: - added check for the function's return value in both cases - removed extra check for dt_device_is_protected() from iommu_do_dt_domctl() Changes V4 -> V5: - added "const" to struct dt_phandle_args *args in dt_xlate - moved iommu_add_dt_device() to xen/passthrough/device_tree.c - modified logic, don't try to add "all" devices to the IOMMU when constructing Dom0, but only devices which are going to be assigned to hwdom - updated iommu_do_dt_domctl() to call iommu_add_dt_device() - clarified patch description - removed "__init" from iommu_add_dt_device() Changes V3 -> V4: - squashed with "iommu: Add of_xlate callback" patch - renamed "of_xlate" to "dt_xlate" - reworked patch description - clarified comments in code, removed confusing word "initialize device", etc - updated debug message in handle_device() - modified to check ops->of_xlate and ops->add_device only if "iommus" property is exists Changes V2 -> V3: - clarified patch description - clarified comments in code - modified to provide DT IOMMU specifier to the driver using "of_xlate" callback - documented function usage - modified to return an error if ops is not present/implemented, - added ability to return a possitive value to indicate that device doesn't need to be protected - removed check for the "iommu" property presence in the common code - included directly --- xen/arch/arm/domain_build.c | 25 +++++++++--- xen/drivers/passthrough/device_tree.c | 77 +++++++++++++++++++++++++++++++= ++++ xen/include/xen/iommu.h | 21 ++++++++++ 3 files changed, 118 insertions(+), 5 deletions(-) diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c index a0fee1e..b84a448 100644 --- a/xen/arch/arm/domain_build.c +++ b/xen/arch/arm/domain_build.c @@ -1243,6 +1243,7 @@ static int __init map_device_children(struct domain *= d, * - Give permission to the guest to manage IRQ and MMIO range * - Retrieve the IRQ configuration (i.e edge/level) from device tree * When the device is not marked for guest passthrough: + * - Try to call iommu_add_dt_device to protect the device by an IOMMU * - Assign the device to the guest if it's protected by an IOMMU * - Map the IRQs and iomem regions to DOM0 */ @@ -1263,16 +1264,30 @@ static int __init handle_device(struct domain *d, s= truct dt_device_node *dev, dt_dprintk("%s passthrough =3D %d nirq =3D %d naddr =3D %u\n", dt_node_full_name(dev), need_mapping, nirq, naddr); =20 - if ( dt_device_is_protected(dev) && need_mapping ) + if ( need_mapping ) { - dt_dprintk("%s setup iommu\n", dt_node_full_name(dev)); - res =3D iommu_assign_dt_device(d, dev); - if ( res ) + dt_dprintk("Check if %s is behind the IOMMU and add it\n", + dt_node_full_name(dev)); + + res =3D iommu_add_dt_device(dev); + if ( res < 0 ) { - printk(XENLOG_ERR "Failed to setup the IOMMU for %s\n", + printk(XENLOG_ERR "Failed to add %s to the IOMMU\n", dt_node_full_name(dev)); return res; } + + if ( dt_device_is_protected(dev) ) + { + dt_dprintk("%s setup iommu\n", dt_node_full_name(dev)); + res =3D iommu_assign_dt_device(d, dev); + if ( res ) + { + printk(XENLOG_ERR "Failed to setup the IOMMU for %s\n", + dt_node_full_name(dev)); + return res; + } + } } =20 /* Give permission and map IRQs */ diff --git a/xen/drivers/passthrough/device_tree.c b/xen/drivers/passthroug= h/device_tree.c index 921a6e5..cc900ba 100644 --- a/xen/drivers/passthrough/device_tree.c +++ b/xen/drivers/passthrough/device_tree.c @@ -22,6 +22,8 @@ #include #include =20 +#include + static spinlock_t dtdevs_lock =3D SPIN_LOCK_UNLOCKED; =20 int iommu_assign_dt_device(struct domain *d, struct dt_device_node *dev) @@ -125,6 +127,68 @@ int iommu_release_dt_devices(struct domain *d) return 0; } =20 +int iommu_add_dt_device(struct dt_device_node *np) +{ + const struct iommu_ops *ops =3D iommu_get_ops(); + struct dt_phandle_args iommu_spec; + struct device *dev =3D dt_to_dev(np); + int rc =3D 1, index =3D 0; + + if ( !iommu_enabled ) + return 1; + + if ( !ops ) + return -EINVAL; + + if ( dev_iommu_fwspec_get(dev) ) + return -EEXIST; + + /* + * According to the Documentation/devicetree/bindings/iommu/iommu.txt + * from Linux. + */ + while ( !dt_parse_phandle_with_args(np, "iommus", "#iommu-cells", + index, &iommu_spec) ) + { + /* + * The driver which supports generic IOMMU DT bindings must have + * these callback implemented. + */ + if ( !ops->add_device || !ops->dt_xlate ) + return -EINVAL; + + if ( !dt_device_is_available(iommu_spec.np) ) + break; + + rc =3D iommu_fwspec_init(dev, &iommu_spec.np->dev); + if ( rc ) + break; + + /* + * Provide DT IOMMU specifier which describes the IOMMU master + * interfaces of that device (device IDs, etc) to the driver. + * The driver is responsible to decide how to interpret them. + */ + rc =3D ops->dt_xlate(dev, &iommu_spec); + if ( rc ) + break; + + index++; + } + + /* + * Add master device to the IOMMU if latter is present and available. + * The driver is responsible to mark that device as protected. + */ + if ( !rc ) + rc =3D ops->add_device(0, dev); + + if ( rc < 0 ) + iommu_fwspec_free(dev); + + return rc; +} + int iommu_do_dt_domctl(struct xen_domctl *domctl, struct domain *d, XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl) { @@ -166,6 +230,19 @@ int iommu_do_dt_domctl(struct xen_domctl *domctl, stru= ct domain *d, break; } =20 + ret =3D iommu_add_dt_device(dev); + /* + * Ignore "-EEXIST" error code as it would mean that the device is + * already added to the IOMMU (positive result). Such happens after + * re-creating guest domain. + */ + if ( ret < 0 && ret !=3D -EEXIST ) + { + printk(XENLOG_G_ERR "Failed to add %s to the IOMMU\n", + dt_node_full_name(dev)); + break; + } + ret =3D iommu_assign_dt_device(d, dev); =20 if ( ret ) diff --git a/xen/include/xen/iommu.h b/xen/include/xen/iommu.h index 7c3003f..974bd3f 100644 --- a/xen/include/xen/iommu.h +++ b/xen/include/xen/iommu.h @@ -182,6 +182,17 @@ int iommu_deassign_dt_device(struct domain *d, struct = dt_device_node *dev); int iommu_dt_domain_init(struct domain *d); int iommu_release_dt_devices(struct domain *d); =20 +/* + * Helper to add master device to the IOMMU using generic IOMMU DT binding= s. + * + * Return values: + * 0 : device is protected by an IOMMU + * <0 : device is not protected by an IOMMU, but must be (error condition) + * >0 : device doesn't need to be protected by an IOMMU + * (IOMMU is not enabled/present or device is not connected to it). + */ +int iommu_add_dt_device(struct dt_device_node *np); + int iommu_do_dt_domctl(struct xen_domctl *, struct domain *, XEN_GUEST_HANDLE_PARAM(xen_domctl_t)); =20 @@ -250,6 +261,16 @@ struct iommu_ops { int __must_check (*iotlb_flush_all)(struct domain *d); int (*get_reserved_device_memory)(iommu_grdm_t *, void *); void (*dump_p2m_table)(struct domain *d); + +#ifdef CONFIG_HAS_DEVICE_TREE + /* + * All IOMMU drivers which support generic IOMMU DT bindings should use + * this callback. This is a way for the framework to provide the driver + * with DT IOMMU specifier which describes the IOMMU master interfaces= of + * that device (device IDs, etc). + */ + int (*dt_xlate)(device_t *dev, const struct dt_phandle_args *args); +#endif }; =20 #include --=20 2.7.4 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel