iommu/amd: Refactor attaching / detaching device functions
authorSuravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Mon, 5 Feb 2024 11:56:09 +0000 (11:56 +0000)
committerJoerg Roedel <jroedel@suse.de>
Fri, 9 Feb 2024 12:16:27 +0000 (13:16 +0100)
If domain is configured with V2 page table then setup default GCR3
with domain GCR3 pointer. So that all devices in the domain uses same page
table for translation. Also return page table setup status from do_attach()
function.

Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Co-developed-by: Vasant Hegde <vasant.hegde@amd.com>
Signed-off-by: Vasant Hegde <vasant.hegde@amd.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Link: https://lore.kernel.org/r/20240205115615.6053-12-vasant.hegde@amd.com
Signed-off-by: Joerg Roedel <jroedel@suse.de>
drivers/iommu/amd/iommu.c

index 4fda158e043fcb3f9b4f94a1376963affd625fae..929a0e8d41d01edc191d57de7779038a04f5a8b1 100644 (file)
@@ -1901,10 +1901,11 @@ static void clear_dte_entry(struct amd_iommu *iommu, u16 devid)
        amd_iommu_apply_erratum_63(iommu, devid);
 }
 
-static void do_attach(struct iommu_dev_data *dev_data,
-                     struct protection_domain *domain)
+static int do_attach(struct iommu_dev_data *dev_data,
+                    struct protection_domain *domain)
 {
        struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
+       int ret = 0;
 
        /* Update data structures */
        dev_data->domain = domain;
@@ -1918,11 +1919,28 @@ static void do_attach(struct iommu_dev_data *dev_data,
        domain->dev_iommu[iommu->index] += 1;
        domain->dev_cnt                 += 1;
 
+       /* Init GCR3 table and update device table */
+       if (domain->pd_mode == PD_MODE_V2) {
+               /* By default, setup GCR3 table to support single PASID */
+               ret = setup_gcr3_table(dev_data->domain, 1);
+               if (ret)
+                       return ret;
+
+               ret = update_gcr3(dev_data, 0,
+                                 iommu_virt_to_phys(domain->iop.pgd), true);
+               if (ret) {
+                       free_gcr3_table(dev_data->domain);
+                       return ret;
+               }
+       }
+
        /* Update device table */
        set_dte_entry(iommu, dev_data);
        clone_aliases(iommu, dev_data->dev);
 
        device_flush_dte(dev_data);
+
+       return ret;
 }
 
 static void do_detach(struct iommu_dev_data *dev_data)
@@ -1930,6 +1948,12 @@ static void do_detach(struct iommu_dev_data *dev_data)
        struct protection_domain *domain = dev_data->domain;
        struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
 
+       /* Clear GCR3 table */
+       if (domain->pd_mode == PD_MODE_V2) {
+               update_gcr3(dev_data, 0, 0, false);
+               free_gcr3_table(dev_data->domain);
+       }
+
        /* Update data structures */
        dev_data->domain = NULL;
        list_del(&dev_data->list);
@@ -1972,7 +1996,7 @@ static int attach_device(struct device *dev,
        if (dev_is_pci(dev))
                pdev_enable_caps(to_pci_dev(dev));
 
-       do_attach(dev_data, domain);
+       ret = do_attach(dev_data, domain);
 
 out:
        spin_unlock(&dev_data->lock);