From nobody Fri Mar 29 07:10:24 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; 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 ARC-Seal: i=1; a=rsa-sha256; t=1557321933; cv=none; d=zoho.com; s=zohoarc; b=Th8f7T3H+f4uM6j4xU33QGDxUSvGsL8ungjkGZcFx9HnJ2phhwScAMXmU28IuMGmBzqnA+f+7pr93CpSyb9ybgxLPOV5heRNamMjX8AYXEY61jQn8dqAyzcjuwm5A61YCeInEedg8HFc9K1DxQ4oiEhd/kgtFjXCKVfhsMUsBWE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1557321933; 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=B+JwQtfT2KUbEdaHflAEI6gwfXR0rXxWDb5PR+2PRS8=; b=RzCYpk2haPEBpHS1j3/EIHsO/kdNkDN07PmhQ1fj1qBtVK0l9mjdNxw8JaizE2bdgdhlviRU5Uk1qifmRY6rLnVXseR7G8ESV5g12ZXUewSigYx96K9hpGbt5Bu4sAUAx1WcIAvbDR2zoEv3VsO72kjoQedwuVX0RYTGABnBGII= ARC-Authentication-Results: i=1; mx.zoho.com; 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 Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1557321933910138.8989285628336; Wed, 8 May 2019 06:25:33 -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 1hOMYT-0000ek-Uy; Wed, 08 May 2019 13:24:17 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hOMYS-0000dh-5Y for xen-devel@lists.xenproject.org; Wed, 08 May 2019 13:24:16 +0000 Received: from SMTP03.CITRIX.COM (unknown [162.221.156.55]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 914338ca-7194-11e9-a4bf-abcea8b8197d; Wed, 08 May 2019 13:24:11 +0000 (UTC) X-Inumbo-ID: 914338ca-7194-11e9-a4bf-abcea8b8197d X-IronPort-AV: E=Sophos;i="5.60,446,1549929600"; d="scan'208";a="85265425" From: Paul Durrant To: Date: Wed, 8 May 2019 14:24:02 +0100 Message-ID: <20190508132403.1454-5-paul.durrant@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190508132403.1454-1-paul.durrant@citrix.com> References: <20190508132403.1454-1-paul.durrant@citrix.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH 4/5] iommu: introduce iommu_groups 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: Stefano Stabellini , Wei Liu , Konrad Rzeszutek Wilk , George Dunlap , Andrew Cooper , Ian Jackson , Tim Deegan , Julien Grall , Paul Durrant , Jan Beulich Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Some devices may share a single PCIe initiator id, e.g. if they are actually legacy PCI devices behind a bridge, and hence DMA from such devices will be subject to the same address translation in the IOMMU. Hence these devices should be treated as a unit for the purposes of assignment. There are also other reasons why multiple devices should be treated as a unit, e.g. those subject to a shared RMRR or those downstream of a bridge that does not support ACS. This patch introduces a new struct iommu_group to act as a container for devices that should be treated as a unit, and builds a list of them as PCI devices are scanned. The iommu_ops already implement a method, get_device_group_id(), that is seemingly intended to return the initiator id for a given SBDF so use this as the mechanism for group assignment in the first instance. Assignment based on shared RMRR or lack of ACS will be dealt with in subsequent patches, as will modifications to the device assignment code. Signed-off-by: Paul Durrant --- Cc: Jan Beulich Cc: Andrew Cooper Cc: George Dunlap Cc: Ian Jackson Cc: Julien Grall Cc: Konrad Rzeszutek Wilk Cc: Stefano Stabellini Cc: Tim Deegan Cc: Wei Liu --- xen/drivers/passthrough/iommu.c | 76 +++++++++++++++++++++++++++++++++++++= ++++ xen/drivers/passthrough/pci.c | 3 ++ xen/include/xen/iommu.h | 7 ++++ xen/include/xen/pci.h | 3 ++ 4 files changed, 89 insertions(+) diff --git a/xen/drivers/passthrough/iommu.c b/xen/drivers/passthrough/iomm= u.c index d3a6199b77..11319fbaae 100644 --- a/xen/drivers/passthrough/iommu.c +++ b/xen/drivers/passthrough/iommu.c @@ -655,6 +655,82 @@ static void iommu_dump_p2m_table(unsigned char key) } } =20 +#ifdef CONFIG_HAS_PCI + +struct iommu_group { + unsigned int id; + unsigned int index; + struct list_head devs_list; +}; + +static struct radix_tree_root iommu_groups; + +void __init iommu_groups_init(void) +{ + radix_tree_init(&iommu_groups); +} + +static struct iommu_group *alloc_iommu_group(unsigned int id) +{ + struct iommu_group *grp; + static unsigned int index; + + grp =3D xzalloc(struct iommu_group); + if ( !grp ) + return NULL; + + grp->id =3D id; + grp->index =3D index++; + INIT_LIST_HEAD(&grp->devs_list); + + if ( radix_tree_insert(&iommu_groups, id, grp) ) + { + xfree(grp); + grp =3D NULL; + } + + return grp; +} + +static struct iommu_group *get_iommu_group(unsigned int id) +{ + struct iommu_group *grp =3D radix_tree_lookup(&iommu_groups, id); + + if ( !grp ) + grp =3D alloc_iommu_group(id); + + return grp; +} + +int iommu_group_assign(struct pci_dev *pdev) +{ + const struct iommu_ops *ops; + unsigned int id; + struct iommu_group *grp; + + ops =3D iommu_get_ops(); + if ( !ops || !ops->get_device_group_id ) + return 0; + + id =3D ops->get_device_group_id(pdev->seg, pdev->bus, pdev->devfn); + grp =3D get_iommu_group(id); + + if ( ! grp ) + return -ENOMEM; + + if ( iommu_verbose ) + printk(XENLOG_INFO "Assign %04x:%02x:%02x.%u -> IOMMU group %u\n", + pdev->seg, pdev->bus, PCI_SLOT(pdev->devfn), + PCI_FUNC(pdev->devfn), grp->index); + + list_add(&pdev->grpdevs_list, &grp->devs_list); + pdev->grp =3D grp; + + return 0; +} + +#endif /* CONFIG_HAS_PCI */ + /* * Local variables: * mode: C diff --git a/xen/drivers/passthrough/pci.c b/xen/drivers/passthrough/pci.c index 8108ed5f9a..6210409741 100644 --- a/xen/drivers/passthrough/pci.c +++ b/xen/drivers/passthrough/pci.c @@ -427,6 +427,7 @@ static struct pci_dev *alloc_pdev(struct pci_seg *pseg,= u8 bus, u8 devfn) =20 check_pdev(pdev); apply_quirks(pdev); + iommu_group_assign(pdev); =20 return pdev; } @@ -1098,6 +1099,8 @@ int __init scan_pci_devices(void) { int ret; =20 + iommu_groups_init(); + pcidevs_lock(); ret =3D pci_segments_iterate(_scan_pci_devices, NULL); pcidevs_unlock(); diff --git a/xen/include/xen/iommu.h b/xen/include/xen/iommu.h index b2d429a6ef..6d6aa12314 100644 --- a/xen/include/xen/iommu.h +++ b/xen/include/xen/iommu.h @@ -315,6 +315,13 @@ DECLARE_PER_CPU(bool_t, iommu_dont_flush_iotlb); extern struct spinlock iommu_pt_cleanup_lock; extern struct page_list_head iommu_pt_cleanup_list; =20 +#ifdef CONFIG_HAS_PCI + +void iommu_groups_init(void); +int iommu_group_assign(struct pci_dev *pdev); + +#endif /* CONFIG_HAS_PCI */ + #endif /* _IOMMU_H_ */ =20 /* diff --git a/xen/include/xen/pci.h b/xen/include/xen/pci.h index 8b21e8dc84..5fe7525db6 100644 --- a/xen/include/xen/pci.h +++ b/xen/include/xen/pci.h @@ -75,6 +75,9 @@ struct pci_dev { struct list_head alldevs_list; struct list_head domain_list; =20 + struct list_head grpdevs_list; + struct iommu_group *grp; + struct list_head msi_list; =20 struct arch_msix *msix; --=20 2.11.0 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel