x86/amd-iommu: Use only dev_data for dte and iotlb flushing routines
[cascardo/linux.git] / arch / x86 / kernel / amd_iommu.c
index 4e8b176..0a5b465 100644 (file)
@@ -693,19 +693,17 @@ void iommu_flush_all_caches(struct amd_iommu *iommu)
 /*
  * Command send function for flushing on-device TLB
  */
-static int device_flush_iotlb(struct device *dev, u64 address, size_t size)
+static int device_flush_iotlb(struct iommu_dev_data *dev_data,
+                             u64 address, size_t size)
 {
-       struct pci_dev *pdev = to_pci_dev(dev);
        struct amd_iommu *iommu;
        struct iommu_cmd cmd;
-       u16 devid;
        int qdep;
 
-       qdep  = pci_ats_queue_depth(pdev);
-       devid = get_device_id(dev);
-       iommu = amd_iommu_rlookup_table[devid];
+       qdep     = dev_data->ats.qdep;
+       iommu    = amd_iommu_rlookup_table[dev_data->devid];
 
-       build_inv_iotlb_pages(&cmd, devid, qdep, address, size);
+       build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
 
        return iommu_queue_command(iommu, &cmd);
 }
@@ -713,23 +711,19 @@ static int device_flush_iotlb(struct device *dev, u64 address, size_t size)
 /*
  * Command send function for invalidating a device table entry
  */
-static int device_flush_dte(struct device *dev)
+static int device_flush_dte(struct iommu_dev_data *dev_data)
 {
-       struct iommu_dev_data *dev_data;
        struct amd_iommu *iommu;
-       struct pci_dev *pdev;
        int ret;
 
-       pdev     = to_pci_dev(dev);
-       dev_data = get_dev_data(dev);
-       iommu    = amd_iommu_rlookup_table[dev_data->devid];
+       iommu = amd_iommu_rlookup_table[dev_data->devid];
 
        ret = iommu_flush_dte(iommu, dev_data->devid);
        if (ret)
                return ret;
 
-       if (pci_ats_enabled(pdev))
-               ret = device_flush_iotlb(dev, 0, ~0UL);
+       if (dev_data->ats.enabled)
+               ret = device_flush_iotlb(dev_data, 0, ~0UL);
 
        return ret;
 }
@@ -760,12 +754,11 @@ static void __domain_flush_pages(struct protection_domain *domain,
        }
 
        list_for_each_entry(dev_data, &domain->dev_list, list) {
-               struct pci_dev *pdev = to_pci_dev(dev_data->dev);
 
-               if (!pci_ats_enabled(pdev))
+               if (!dev_data->ats.enabled)
                        continue;
 
-               ret |= device_flush_iotlb(dev_data->dev, address, size);
+               ret |= device_flush_iotlb(dev_data, address, size);
        }
 
        WARN_ON(ret);
@@ -817,7 +810,7 @@ static void domain_flush_devices(struct protection_domain *domain)
        spin_lock_irqsave(&domain->lock, flags);
 
        list_for_each_entry(dev_data, &domain->dev_list, list)
-               device_flush_dte(dev_data->dev);
+               device_flush_dte(dev_data);
 
        spin_unlock_irqrestore(&domain->lock, flags);
 }
@@ -1576,8 +1569,7 @@ static void do_attach(struct device *dev, struct protection_domain *domain)
        iommu    = amd_iommu_rlookup_table[dev_data->devid];
        pdev     = to_pci_dev(dev);
 
-       if (amd_iommu_iotlb_sup)
-               ats = pci_ats_enabled(pdev);
+       ats = dev_data->ats.enabled;
 
        /* Update data structures */
        dev_data->domain = domain;
@@ -1589,7 +1581,7 @@ static void do_attach(struct device *dev, struct protection_domain *domain)
        domain->dev_cnt                 += 1;
 
        /* Flush the DTE entry */
-       device_flush_dte(dev);
+       device_flush_dte(dev_data);
 }
 
 static void do_detach(struct device *dev)
@@ -1610,7 +1602,7 @@ static void do_detach(struct device *dev)
        clear_dte_entry(dev_data->devid);
 
        /* Flush the DTE entry */
-       device_flush_dte(dev);
+       device_flush_dte(dev_data);
 }
 
 /*
@@ -1674,11 +1666,16 @@ static int attach_device(struct device *dev,
                         struct protection_domain *domain)
 {
        struct pci_dev *pdev = to_pci_dev(dev);
+       struct iommu_dev_data *dev_data;
        unsigned long flags;
        int ret;
 
-       if (amd_iommu_iotlb_sup)
-               pci_enable_ats(pdev, PAGE_SHIFT);
+       dev_data = get_dev_data(dev);
+
+       if (amd_iommu_iotlb_sup && pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
+               dev_data->ats.enabled = true;
+               dev_data->ats.qdep    = pci_ats_queue_depth(pdev);
+       }
 
        write_lock_irqsave(&amd_iommu_devtable_lock, flags);
        ret = __attach_device(dev, domain);
@@ -1736,7 +1733,7 @@ static void __detach_device(struct device *dev)
  */
 static void detach_device(struct device *dev)
 {
-       struct pci_dev *pdev = to_pci_dev(dev);
+       struct iommu_dev_data *dev_data;
        unsigned long flags;
 
        /* lock device table */
@@ -1744,8 +1741,12 @@ static void detach_device(struct device *dev)
        __detach_device(dev);
        write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
 
-       if (amd_iommu_iotlb_sup && pci_ats_enabled(pdev))
-               pci_disable_ats(pdev);
+       dev_data = get_dev_data(dev);
+
+       if (dev_data->ats.enabled) {
+               pci_disable_ats(to_pci_dev(dev));
+               dev_data->ats.enabled = false;
+       }
 }
 
 /*
@@ -1890,10 +1891,8 @@ static void update_device_table(struct protection_domain *domain)
 {
        struct iommu_dev_data *dev_data;
 
-       list_for_each_entry(dev_data, &domain->dev_list, list) {
-               struct pci_dev *pdev = to_pci_dev(dev_data->dev);
-               set_dte_entry(dev_data->devid, domain, pci_ats_enabled(pdev));
-       }
+       list_for_each_entry(dev_data, &domain->dev_list, list)
+               set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
 }
 
 static void update_domain(struct protection_domain *domain)