[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v2 8/8] xen/arm: smmuv3: Add PCI devices support for SMMUv3


  • To: <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Stewart Hildebrand <stewart.hildebrand@xxxxxxx>
  • Date: Thu, 11 May 2023 15:16:54 -0400
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=SO3THuJ+/ik7n2bM+aWq9Ulbj6rZ/FkssTS50Og6lnI=; b=DcVPho3m/knVFB70n+uJdsR+l1LRyMEVYqut78+7RKqzAGXvc4v1b9+EJifgHOzUcSoZkHS64HeYASE8jloHotNwv5o1STGH1/qDDpAdbTnnSHivsr5jHDiQ+2p3Umsd9aDQnemmnCOEluOP08s0mog6BqvWs0TVUVSv9LOaTomnM+I3llOzfMJSGHQAM3Zufyqf+QXMVKzftqFG71+fp/6M0r6J5ueQJUx9G/LEA9Y+vsmLInpsZB6NipiaTZk8m1dv070z0R7ONBBWLZdWTDIOQnGNFz+jwVs/5eHrEFZ2FEuH0pCd+64kgdhVneldA4YNSemYOtepZ8Q+rE74lg==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bak56nci9K5jjkkIkXvkbiaY+3qyRk9Y/L9C3w3i5L8DqGHjZQqoLxpjtSs+9Z7wLfvCAinnXUnKiA30MxXnXwomtoxjIDGEkjCXKmOmkT/Ymxowvei3Yi9PsISYTALPuP3LTb+nh7MDPwc61xDRsZ8GwdytdJi/I2X9tzUwkB6rMzThDWNCVAiux5I39wKRtNG4tTDdI7UiU6M3z/Wu7Sifui/Y4VrL8D7Sbkq54AhY8bkzrlg4siiYR7wQoO0+piCQnXgY6AfhAPyjdsJj21QK65keRuZUfRAHUGnchdezRzeeU7TxPQqKNadNrIdUYsGFzTA5wfa2Fge9kcGFRw==
  • Cc: Rahul Singh <rahul.singh@xxxxxxx>, Bertrand Marquis <bertrand.marquis@xxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, Julien Grall <julien@xxxxxxx>, Volodymyr Babchuk <Volodymyr_Babchuk@xxxxxxxx>, Stewart Hildebrand <stewart.hildebrand@xxxxxxx>
  • Delivery-date: Thu, 11 May 2023 19:22:44 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

From: Rahul Singh <rahul.singh@xxxxxxx>

Signed-off-by: Rahul Singh <rahul.singh@xxxxxxx>
Signed-off-by: Stewart Hildebrand <stewart.hildebrand@xxxxxxx>
---
v1->v2:
* ignore add_device/assign_device/reassign_device calls for phantom functions
  (i.e. devfn != pdev->devfn)

downstream->v1:
* rebase
* move 2 replacements of 
s/dt_device_set_protected(dev_to_dt(dev))/device_set_protected(dev)/
  from this commit to ("xen/arm: Move is_protected flag to struct device")
  so as to not break ability to bisect
* adjust patch title (remove stray space)
* arm_smmu_(de)assign_dev: return error instead of crashing system
* remove arm_smmu_remove_device() stub
* update condition in arm_smmu_reassign_dev
* style fixup

(cherry picked from commit 7ed6c3ab250d899fe6e893a514278e406a2893e8 from
 the downstream branch poc/pci-passthrough from
 https://gitlab.com/xen-project/people/bmarquis/xen-arm-poc.git)
---

This is a file imported from Linux with modifications for Xen. What should be
the coding style used for Xen modifications?
---
 xen/drivers/passthrough/arm/smmu-v3.c | 67 +++++++++++++++++++++++++--
 1 file changed, 64 insertions(+), 3 deletions(-)

diff --git a/xen/drivers/passthrough/arm/smmu-v3.c 
b/xen/drivers/passthrough/arm/smmu-v3.c
index 4b452e6fdd00..807cfe575345 100644
--- a/xen/drivers/passthrough/arm/smmu-v3.c
+++ b/xen/drivers/passthrough/arm/smmu-v3.c
@@ -1469,6 +1469,8 @@ static bool arm_smmu_sid_in_range(struct arm_smmu_device 
*smmu, u32 sid)
 }
 /* Forward declaration */
 static struct arm_smmu_device *arm_smmu_get_by_dev(struct device *dev);
+static int arm_smmu_assign_dev(struct domain *d, u8 devfn,
+                       struct device *dev, u32 flag);
 
 static int arm_smmu_add_device(u8 devfn, struct device *dev)
 {
@@ -1484,6 +1486,15 @@ static int arm_smmu_add_device(u8 devfn, struct device 
*dev)
        if (!smmu)
                return -ENODEV;
 
+#ifdef CONFIG_HAS_PCI
+       if ( dev_is_pci(dev) )
+       {
+               struct pci_dev *pdev = dev_to_pci(dev);
+               if ( devfn != pdev->devfn )
+                       return 0;
+       }
+#endif
+
        master = xzalloc(struct arm_smmu_master);
        if (!master)
                return -ENOMEM;
@@ -1527,6 +1538,17 @@ static int arm_smmu_add_device(u8 devfn, struct device 
*dev)
        dev_info(dev, "Added master device (SMMUv3 %s StreamIds %u)\n",
                        dev_name(fwspec->iommu_dev), fwspec->num_ids);
 
+#ifdef CONFIG_HAS_PCI
+       if ( dev_is_pci(dev) )
+       {
+               struct pci_dev *pdev = dev_to_pci(dev);
+
+               ret = arm_smmu_assign_dev(pdev->domain, devfn, dev, 0);
+               if (ret)
+                       goto err_free_master;
+       }
+#endif
+
        return 0;
 
 err_free_master:
@@ -2607,6 +2629,27 @@ static int arm_smmu_assign_dev(struct domain *d, u8 
devfn,
        struct arm_smmu_domain *smmu_domain;
        struct arm_smmu_xen_domain *xen_domain = dom_iommu(d)->arch.priv;
 
+#ifdef CONFIG_HAS_PCI
+       if ( dev_is_pci(dev) && !is_hardware_domain(d) )
+       {
+               struct pci_dev *pdev = dev_to_pci(dev);
+
+               printk(XENLOG_INFO "Assigning device %04x:%02x:%02x.%u to 
dom%d\n",
+                       pdev->seg, pdev->bus, PCI_SLOT(devfn),
+                       PCI_FUNC(devfn), d->domain_id);
+
+               if ( devfn != pdev->devfn || pdev->domain == d )
+                       return 0;
+
+               list_move(&pdev->domain_list, &d->pdev_list);
+               pdev->domain = d;
+
+               /* dom_io is used as a sentinel for quarantined devices */
+               if ( d == dom_io )
+                       return 0;
+       }
+#endif
+
        spin_lock(&xen_domain->lock);
 
        /*
@@ -2640,7 +2683,7 @@ out:
        return ret;
 }
 
-static int arm_smmu_deassign_dev(struct domain *d, struct device *dev)
+static int arm_smmu_deassign_dev(struct domain *d, uint8_t devfn, struct 
device *dev)
 {
        struct iommu_domain *io_domain = arm_smmu_get_domain(d, dev);
        struct arm_smmu_xen_domain *xen_domain = dom_iommu(d)->arch.priv;
@@ -2652,6 +2695,24 @@ static int arm_smmu_deassign_dev(struct domain *d, 
struct device *dev)
                return -ESRCH;
        }
 
+#ifdef CONFIG_HAS_PCI
+       if ( dev_is_pci(dev) )
+       {
+               struct pci_dev *pdev = dev_to_pci(dev);
+
+               printk(XENLOG_INFO "Deassigning device %04x:%02x:%02x.%u from 
dom%d\n",
+                       pdev->seg, pdev->bus, PCI_SLOT(devfn),
+                       PCI_FUNC(devfn), d->domain_id);
+
+               if ( devfn != pdev->devfn )
+                       return 0;
+
+               /* dom_io is used as a sentinel for quarantined devices */
+               if ( d == dom_io )
+                       return 0;
+       }
+#endif
+
        spin_lock(&xen_domain->lock);
 
        arm_smmu_detach_dev(master);
@@ -2671,13 +2732,13 @@ static int arm_smmu_reassign_dev(struct domain *s, 
struct domain *t,
        int ret = 0;
 
        /* Don't allow remapping on other domain than hwdom */
-       if ( t && !is_hardware_domain(t) )
+       if ( t && !is_hardware_domain(t) && (t != dom_io) )
                return -EPERM;
 
        if (t == s)
                return 0;
 
-       ret = arm_smmu_deassign_dev(s, dev);
+       ret = arm_smmu_deassign_dev(s, devfn, dev);
        if (ret)
                return ret;
 
-- 
2.40.1




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.