From patchwork Wed May 8 13:24:02 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 10935667 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7D9E213AD for ; Wed, 8 May 2019 13:26:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6D24A28563 for ; Wed, 8 May 2019 13:26:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 60D5628587; Wed, 8 May 2019 13:26:08 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 0874028573 for ; Wed, 8 May 2019 13:26:06 +0000 (UTC) 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 X-Inumbo-ID: 914338ca-7194-11e9-a4bf-abcea8b8197d 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-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 Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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/iommu.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) } } +#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 = xzalloc(struct iommu_group); + if ( !grp ) + return NULL; + + grp->id = id; + grp->index = index++; + INIT_LIST_HEAD(&grp->devs_list); + + if ( radix_tree_insert(&iommu_groups, id, grp) ) + { + xfree(grp); + grp = NULL; + } + + return grp; +} + +static struct iommu_group *get_iommu_group(unsigned int id) +{ + struct iommu_group *grp = radix_tree_lookup(&iommu_groups, id); + + if ( !grp ) + grp = 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 = iommu_get_ops(); + if ( !ops || !ops->get_device_group_id ) + return 0; + + id = ops->get_device_group_id(pdev->seg, pdev->bus, pdev->devfn); + grp = 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 = 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) check_pdev(pdev); apply_quirks(pdev); + iommu_group_assign(pdev); return pdev; } @@ -1098,6 +1099,8 @@ int __init scan_pci_devices(void) { int ret; + iommu_groups_init(); + pcidevs_lock(); ret = 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; +#ifdef CONFIG_HAS_PCI + +void iommu_groups_init(void); +int iommu_group_assign(struct pci_dev *pdev); + +#endif /* CONFIG_HAS_PCI */ + #endif /* _IOMMU_H_ */ /* 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; + struct list_head grpdevs_list; + struct iommu_group *grp; + struct list_head msi_list; struct arch_msix *msix;