iommu/amd: Convert to use rlookup_amd_iommu helper function
authorSuravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Wed, 6 Jul 2022 11:38:05 +0000 (17:08 +0530)
committerJoerg Roedel <jroedel@suse.de>
Thu, 7 Jul 2022 07:37:42 +0000 (09:37 +0200)
Use rlookup_amd_iommu() helper function which will give per PCI
segment rlookup_table.

Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Signed-off-by: Vasant Hegde <vasant.hegde@amd.com>
Link: https://lore.kernel.org/r/20220706113825.25582-16-vasant.hegde@amd.com
Signed-off-by: Joerg Roedel <jroedel@suse.de>
drivers/iommu/amd/iommu.c

index cfecd07..19db4d5 100644 (file)
@@ -229,13 +229,17 @@ static struct iommu_dev_data *search_dev_data(struct amd_iommu *iommu, u16 devid
 
 static int clone_alias(struct pci_dev *pdev, u16 alias, void *data)
 {
+       struct amd_iommu *iommu;
        u16 devid = pci_dev_id(pdev);
 
        if (devid == alias)
                return 0;
 
-       amd_iommu_rlookup_table[alias] =
-               amd_iommu_rlookup_table[devid];
+       iommu = rlookup_amd_iommu(&pdev->dev);
+       if (!iommu)
+               return 0;
+
+       amd_iommu_set_rlookup_table(iommu, alias);
        memcpy(amd_iommu_dev_table[alias].data,
               amd_iommu_dev_table[devid].data,
               sizeof(amd_iommu_dev_table[alias].data));
@@ -366,7 +370,7 @@ static bool check_device(struct device *dev)
        if (devid > amd_iommu_last_bdf)
                return false;
 
-       if (amd_iommu_rlookup_table[devid] == NULL)
+       if (rlookup_amd_iommu(dev) == NULL)
                return false;
 
        return true;
@@ -1270,7 +1274,9 @@ static int device_flush_iotlb(struct iommu_dev_data *dev_data,
        int qdep;
 
        qdep     = dev_data->ats.qdep;
-       iommu    = amd_iommu_rlookup_table[dev_data->devid];
+       iommu    = rlookup_amd_iommu(dev_data->dev);
+       if (!iommu)
+               return -EINVAL;
 
        build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
 
@@ -1295,7 +1301,9 @@ static int device_flush_dte(struct iommu_dev_data *dev_data)
        u16 alias;
        int ret;
 
-       iommu = amd_iommu_rlookup_table[dev_data->devid];
+       iommu = rlookup_amd_iommu(dev_data->dev);
+       if (!iommu)
+               return -EINVAL;
 
        if (dev_is_pci(dev_data->dev))
                pdev = to_pci_dev(dev_data->dev);
@@ -1525,8 +1533,8 @@ static void free_gcr3_table(struct protection_domain *domain)
        free_page((unsigned long)domain->gcr3_tbl);
 }
 
-static void set_dte_entry(u16 devid, struct protection_domain *domain,
-                         bool ats, bool ppr)
+static void set_dte_entry(struct amd_iommu *iommu, u16 devid,
+                         struct protection_domain *domain, bool ats, bool ppr)
 {
        u64 pte_root = 0;
        u64 flags = 0;
@@ -1545,8 +1553,6 @@ static void set_dte_entry(u16 devid, struct protection_domain *domain,
                flags |= DTE_FLAG_IOTLB;
 
        if (ppr) {
-               struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
-
                if (iommu_feature(iommu, FEATURE_EPHSUP))
                        pte_root |= 1ULL << DEV_ENTRY_PPR;
        }
@@ -1590,8 +1596,6 @@ static void set_dte_entry(u16 devid, struct protection_domain *domain,
         * entries for the old domain ID that is being overwritten
         */
        if (old_domid) {
-               struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
-
                amd_iommu_flush_tlb_domid(iommu, old_domid);
        }
 }
@@ -1611,7 +1615,9 @@ static void do_attach(struct iommu_dev_data *dev_data,
        struct amd_iommu *iommu;
        bool ats;
 
-       iommu = amd_iommu_rlookup_table[dev_data->devid];
+       iommu = rlookup_amd_iommu(dev_data->dev);
+       if (!iommu)
+               return;
        ats   = dev_data->ats.enabled;
 
        /* Update data structures */
@@ -1623,7 +1629,7 @@ static void do_attach(struct iommu_dev_data *dev_data,
        domain->dev_cnt                 += 1;
 
        /* Update device table */
-       set_dte_entry(dev_data->devid, domain,
+       set_dte_entry(iommu, dev_data->devid, domain,
                      ats, dev_data->iommu_v2);
        clone_aliases(iommu, dev_data->dev);
 
@@ -1635,7 +1641,9 @@ static void do_detach(struct iommu_dev_data *dev_data)
        struct protection_domain *domain = dev_data->domain;
        struct amd_iommu *iommu;
 
-       iommu = amd_iommu_rlookup_table[dev_data->devid];
+       iommu = rlookup_amd_iommu(dev_data->dev);
+       if (!iommu)
+               return;
 
        /* Update data structures */
        dev_data->domain = NULL;
@@ -1813,13 +1821,14 @@ static struct iommu_device *amd_iommu_probe_device(struct device *dev)
 {
        struct iommu_device *iommu_dev;
        struct amd_iommu *iommu;
-       int ret, devid;
+       int ret;
 
        if (!check_device(dev))
                return ERR_PTR(-ENODEV);
 
-       devid = get_device_id(dev);
-       iommu = amd_iommu_rlookup_table[devid];
+       iommu = rlookup_amd_iommu(dev);
+       if (!iommu)
+               return ERR_PTR(-ENODEV);
 
        if (dev_iommu_priv_get(dev))
                return &iommu->iommu;
@@ -1849,13 +1858,14 @@ static void amd_iommu_probe_finalize(struct device *dev)
 
 static void amd_iommu_release_device(struct device *dev)
 {
-       int devid = get_device_id(dev);
        struct amd_iommu *iommu;
 
        if (!check_device(dev))
                return;
 
-       iommu = amd_iommu_rlookup_table[devid];
+       iommu = rlookup_amd_iommu(dev);
+       if (!iommu)
+               return;
 
        amd_iommu_uninit_device(dev);
        iommu_completion_wait(iommu);
@@ -1884,7 +1894,7 @@ static void update_device_table(struct protection_domain *domain)
 
                if (!iommu)
                        continue;
-               set_dte_entry(dev_data->devid, domain,
+               set_dte_entry(iommu, dev_data->devid, domain,
                              dev_data->ats.enabled, dev_data->iommu_v2);
                clone_aliases(iommu, dev_data->dev);
        }
@@ -2072,7 +2082,6 @@ static void amd_iommu_detach_device(struct iommu_domain *dom,
                                    struct device *dev)
 {
        struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
-       int devid = get_device_id(dev);
        struct amd_iommu *iommu;
 
        if (!check_device(dev))
@@ -2081,7 +2090,7 @@ static void amd_iommu_detach_device(struct iommu_domain *dom,
        if (dev_data->domain != NULL)
                detach_device(dev);
 
-       iommu = amd_iommu_rlookup_table[devid];
+       iommu = rlookup_amd_iommu(dev);
        if (!iommu)
                return;
 
@@ -2108,7 +2117,7 @@ static int amd_iommu_attach_device(struct iommu_domain *dom,
        dev_data = dev_iommu_priv_get(dev);
        dev_data->defer_attach = false;
 
-       iommu = amd_iommu_rlookup_table[dev_data->devid];
+       iommu = rlookup_amd_iommu(dev);
        if (!iommu)
                return -EINVAL;
 
@@ -2493,8 +2502,9 @@ static int __flush_pasid(struct protection_domain *domain, u32 pasid,
                        continue;
 
                qdep  = dev_data->ats.qdep;
-               iommu = amd_iommu_rlookup_table[dev_data->devid];
-
+               iommu = rlookup_amd_iommu(dev_data->dev);
+               if (!iommu)
+                       continue;
                build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
                                      qdep, address, size);
 
@@ -2656,7 +2666,9 @@ int amd_iommu_complete_ppr(struct pci_dev *pdev, u32 pasid,
        struct iommu_cmd cmd;
 
        dev_data = dev_iommu_priv_get(&pdev->dev);
-       iommu    = amd_iommu_rlookup_table[dev_data->devid];
+       iommu    = rlookup_amd_iommu(&pdev->dev);
+       if (!iommu)
+               return -ENODEV;
 
        build_complete_ppr(&cmd, dev_data->devid, pasid, status,
                           tag, dev_data->pri_tlp);