Skip to contentSkip to conversation

iommu/vt-d: Enable ATS for the devices in SATC table

PATCH Superseded Yian Chen 2 participants 3 messages Feb 16, 2022 – Feb 18, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Yian Chen

author
#0 lore raw

PATCH 1/1 iommu/vt-d: Enable ATS for the devices in SATC table

Starting from Intel VT-d v3.2, Intel platform BIOS can provide additional SATC table structure. SATC table includes a list of SoC integrated devices that support ATC (Address translation cache).

Enabling ATC (via ATS capability) can be a functional requirement for SATC device operation or an optional to enhance device performance/functionality. This is determined by the bit of ATC_REQUIRED in SATC table. When IOMMU is working in scalable mode, software chooses to always enable ATS for every device in SATC table because Intel SoC devices in SATC table are trusted to use ATS.

On the other hand, if IOMMU is in legacy mode, ATS of SATC capable devices can work transparently to software and be automatically enabled by IOMMU hardware. As the result, there is no need for software to enable ATS on these devices.

---
 drivers/iommu/intel/iommu.c | 53 ++++++++++++++++++++++++++++++++++---
 include/linux/intel-iommu.h |  2 +-
 2 files changed, 51 insertions(+), 4 deletions(-)
drivers/iommu/intel/iommu.c +50 -3
index 92fea3fbbb11..58a80cec50bb 100644
@@ -872,7 +872,6 @@ static bool iommu_is_dummy(struct intel_iommu *iommu, struct device *dev)
 
 	return false;
 }
-
 struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
 {
 	struct dmar_drhd_unit *drhd = NULL;
@@ -2684,7 +2683,7 @@ static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu,
 
 		if (ecap_dev_iotlb_support(iommu->ecap) &&
 		    pci_ats_supported(pdev) &&
-		    dmar_find_matched_atsr_unit(pdev))
+		    dmar_ats_supported(pdev, iommu))
 			info->ats_supported = 1;
 
 		if (sm_supported(iommu)) {
@@ -4020,7 +4019,42 @@ static void intel_iommu_free_dmars(void)
 	}
 }
 
-int dmar_find_matched_atsr_unit(struct pci_dev *dev)
+/* dev_satc_state - Find if dev is in a DMAR SATC table
+ *
+ * return value:
+ *    1: dev is in STAC table and ATS is required
+ *    0: dev is in SATC table and ATS is optional
+ *    -1: dev isn't in SATC table
+ */
+static int dev_satc_state(struct pci_dev *dev)
+{
+	int i, ret = -1;
+	struct device *tmp;
+	struct dmar_satc_unit *satcu;
+	struct acpi_dmar_satc *satc;
+
+	dev = pci_physfn(dev);
+	rcu_read_lock();
+
+	list_for_each_entry_rcu(satcu, &dmar_satc_units, list) {
+		satc = container_of(satcu->hdr, struct acpi_dmar_satc, header);
+		if (satc->segment != pci_domain_nr(dev->bus))
+			continue;
+		for_each_dev_scope(satcu->devices, satcu->devices_cnt, i, tmp)
+			if (to_pci_dev(tmp) == dev) {
+				if (satc->flags)
+					ret = 1;
+				else
+					ret = 0;
+				goto out;
+			}
+	}
+out:
+	rcu_read_unlock();
+	return ret;
+}
+
+int dmar_ats_supported(struct pci_dev *dev, struct intel_iommu *iommu)
 {
 	int i, ret = 1;
 	struct pci_bus *bus;
@@ -4030,6 +4064,19 @@ int dmar_find_matched_atsr_unit(struct pci_dev *dev)
 	struct dmar_atsr_unit *atsru;
 
 	dev = pci_physfn(dev);
+	i = dev_satc_state(dev);
+	if (i >= 0) {
+		/* This dev supports ATS as it is in SATC table!
+		 * When IOMMU is in legacy mode, enabling ATS is done
+		 * automatically by HW for the device that requires
+		 * ATS, hence OS should not enable this device ATS
+		 * to avoid duplicated TLB invalidation
+		 */
+		if (i && !sm_supported(iommu))
+			ret = 0;
+		return ret;
+	}
+
 	for (bus = dev->bus; bus; bus = bus->parent) {
 		bridge = bus->self;
 		/* If it's an integrated device, allow ATS */
include/linux/intel-iommu.h +1 -1
index 69230fd695ea..fe9fd417d611 100644
@@ -717,7 +717,7 @@ static inline int nr_pte_to_next_page(struct dma_pte *pte)
 }
 
 extern struct dmar_drhd_unit * dmar_find_matched_drhd_unit(struct pci_dev *dev);
-extern int dmar_find_matched_atsr_unit(struct pci_dev *dev);
+extern int dmar_ats_supported(struct pci_dev *dev, struct intel_iommu *iommu);
 
 extern int dmar_enable_qi(struct intel_iommu *iommu);
 extern void dmar_disable_qi(struct intel_iommu *iommu);
Signature · 1 line
2.25.1
maintainer
#1 lore raw

Hi Yian,

Quoted 29 lines · Yian Chen

Starting from Intel VT-d v3.2, Intel platform BIOS can provide additional SATC table structure. SATC table includes a list of SoC integrated devices that support ATC (Address translation cache).

Enabling ATC (via ATS capability) can be a functional requirement for SATC device operation or an optional to enhance device performance/functionality. This is determined by the bit of ATC_REQUIRED in SATC table. When IOMMU is working in scalable mode, software chooses to always enable ATS for every device in SATC table because Intel SoC devices in SATC table are trusted to use ATS.

On the other hand, if IOMMU is in legacy mode, ATS of SATC capable devices can work transparently to software and be automatically enabled by IOMMU hardware. As the result, there is no need for software to enable ATS on these devices.

---
  drivers/iommu/intel/iommu.c | 53 ++++++++++++++++++++++++++++++++++---
  include/linux/intel-iommu.h |  2 +-
  2 files changed, 51 insertions(+), 4 deletions(-)
drivers/iommu/intel/iommu.c +0 -0
index 92fea3fbbb11..58a80cec50bb 100644

drivers/iommu/intel/iommu.c static bool iommu_is_dummy(struct intel_iommu *iommu, struct device *dev) · line 875

Quoted 4 lines
drivers/iommu/intel/iommu.c +0 -1
@@ -872,7 +872,6 @@ static bool iommu_is_dummy(struct intel_iommu *iommu, struct device *dev)
 
  	return false;
  }
-

Unnecessary.

Quoted 14 lines
drivers/iommu/intel/iommu.c +1 -1
  struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
  {
  	struct dmar_drhd_unit *drhd = NULL;
@@ -2684,7 +2683,7 @@ static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu,
 
  		if (ecap_dev_iotlb_support(iommu->ecap) &&
  		    pci_ats_supported(pdev) &&
-		    dmar_find_matched_atsr_unit(pdev))
+		    dmar_ats_supported(pdev, iommu))
  			info->ats_supported = 1;
 
  		if (sm_supported(iommu)) {
@@ -4020,7 +4019,42 @@ static void intel_iommu_free_dmars(void)
  	}

drivers/iommu/intel/iommu.c static void intel_iommu_free_dmars(void) · line 4028

Quoted 10 lines
drivers/iommu/intel/iommu.c +7 -1
  }
 
-int dmar_find_matched_atsr_unit(struct pci_dev *dev)
+/* dev_satc_state - Find if dev is in a DMAR SATC table
+ *
+ * return value:
+ *    1: dev is in STAC table and ATS is required
+ *    0: dev is in SATC table and ATS is optional
+ *    -1: dev isn't in SATC table
+ */

Judging the status of the device in table according to the hard coded return value will make the code hard to read. How about using two helpers with meaningful names, for example,

bool dmar_find_matched_satc_unit()
bool dmar_ats_required_in_satc()?

Quoted 7 lines
drivers/iommu/intel/iommu.c +7 -0
+static int dev_satc_state(struct pci_dev *dev)
+{
+	int i, ret = -1;
+	struct device *tmp;
+	struct dmar_satc_unit *satcu;
+	struct acpi_dmar_satc *satc;
+

drivers/iommu/intel/iommu.c static void intel_iommu_free_dmars(void) · line 4045

Quoted 10 lines
drivers/iommu/intel/iommu.c +10 -0
+	dev = pci_physfn(dev);
+	rcu_read_lock();
+
+	list_for_each_entry_rcu(satcu, &dmar_satc_units, list) {
+		satc = container_of(satcu->hdr, struct acpi_dmar_satc, header);
+		if (satc->segment != pci_domain_nr(dev->bus))
+			continue;
+		for_each_dev_scope(satcu->devices, satcu->devices_cnt, i, tmp)
+			if (to_pci_dev(tmp) == dev) {
+				if (satc->flags)

Do you need to check the ATC_REQUIRED bit field?

Quoted 40 lines
drivers/iommu/intel/iommu.c +25 -0
+					ret = 1;
+				else
+					ret = 0;
+				goto out;
+			}
+	}
+out:
+	rcu_read_unlock();
+	return ret;
+}
+
+int dmar_ats_supported(struct pci_dev *dev, struct intel_iommu *iommu)
  {
  	int i, ret = 1;
  	struct pci_bus *bus;
@@ -4030,6 +4064,19 @@ int dmar_find_matched_atsr_unit(struct pci_dev *dev)
  	struct dmar_atsr_unit *atsru;
 
  	dev = pci_physfn(dev);
+	i = dev_satc_state(dev);
+	if (i >= 0) {
+		/* This dev supports ATS as it is in SATC table!
+		 * When IOMMU is in legacy mode, enabling ATS is done
+		 * automatically by HW for the device that requires
+		 * ATS, hence OS should not enable this device ATS
+		 * to avoid duplicated TLB invalidation
+		 */
+		if (i && !sm_supported(iommu))
+			ret = 0;
+		return ret;
+	}
+
  	for (bus = dev->bus; bus; bus = bus->parent) {
  		bridge = bus->self;
  		/* If it's an integrated device, allow ATS */
include/linux/intel-iommu.h +0 -0
index 69230fd695ea..fe9fd417d611 100644

include/linux/intel-iommu.h static inline int nr_pte_to_next_page(struct dma_pte *pte) · line 720

Quoted 5 lines
include/linux/intel-iommu.h +1 -1
@@ -717,7 +717,7 @@ static inline int nr_pte_to_next_page(struct dma_pte *pte)
  }
 
  extern struct dmar_drhd_unit * dmar_find_matched_drhd_unit(struct pci_dev *dev);
-extern int dmar_find_matched_atsr_unit(struct pci_dev *dev);
+extern int dmar_ats_supported(struct pci_dev *dev, struct intel_iommu *iommu);

The dmar_find_matched_atsr_unit() is not used out of iommu.c. Can you please make a patch to change it to a static one?

include/linux/intel-iommu.h static inline int nr_pte_to_next_page(struct dma_pte *pte) · line 723

Quoted 3 lines
include/linux/intel-iommu.h +0 -0
  extern int dmar_enable_qi(struct intel_iommu *iommu);
  extern void dmar_disable_qi(struct intel_iommu *iommu);

Best regards,
baolu

author
#2 lore raw
Quoted 27 lines · Lu Baolu

Hi Yian,

Quoted 24 lines · Yian Chen

Starting from Intel VT-d v3.2, Intel platform BIOS can provide additional SATC table structure. SATC table includes a list of SoC integrated devices that support ATC (Address translation cache).

Enabling ATC (via ATS capability) can be a functional requirement for SATC device operation or an optional to enhance device performance/functionality. This is determined by the bit of ATC_REQUIRED in SATC table. When IOMMU is working in scalable mode, software chooses to always enable ATS for every device in SATC table because Intel SoC devices in SATC table are trusted to use ATS.

On the other hand, if IOMMU is in legacy mode, ATS of SATC capable devices can work transparently to software and be automatically enabled by IOMMU hardware. As the result, there is no need for software to enable ATS on these devices.

---
  drivers/iommu/intel/iommu.c | 53 ++++++++++++++++++++++++++++++++++--- include/linux/intel-iommu.h |  2 +-

  2 files changed, 51 insertions(+), 4 deletions(-)
Quoted 11 lines

drivers/iommu/intel/iommu.c static bool iommu_is_dummy(struct intel_iommu · line 875

Quoted 9 lines
drivers/iommu/intel/iommu.c +0 -1
index 92fea3fbbb11..58a80cec50bb 100644
@@ -872,7 +872,6 @@ static bool iommu_is_dummy(struct intel_iommu 
 *iommu, struct device *dev)
       return false;
   }
-

Unnecessary.

sure, to remove this.

Quoted 28 lines
Quoted 13 lines
drivers/iommu/intel/iommu.c +0 -0
   struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
   {
       struct dmar_drhd_unit *drhd = NULL;
@@ -2684,7 +2683,7 @@ static struct dmar_domain 
 *dmar_insert_one_dev_info(struct intel_iommu *iommu,

          if (ecap_dev_iotlb_support(iommu->ecap) && pci_ats_supported(pdev) &&
-            dmar_find_matched_atsr_unit(pdev)) +            dmar_ats_supported(pdev, iommu)) info->ats_supported = 1;
          if (sm_supported(iommu)) {

drivers/iommu/intel/iommu.c static void intel_iommu_free_dmars(void) · line 4027

Quoted 10 lines
drivers/iommu/intel/iommu.c +7 -1
@@ -4020,7 +4019,42 @@ static void intel_iommu_free_dmars(void)
       }
   }
-int dmar_find_matched_atsr_unit(struct pci_dev *dev)
+/* dev_satc_state - Find if dev is in a DMAR SATC table
+ *
+ * return value:
+ *    1: dev is in STAC table and ATS is required
+ *    0: dev is in SATC table and ATS is optional
+ *    -1: dev isn't in SATC table
+ */

Judging the status of the device in table according to the hard coded return value will make the code hard to read. How about using two helpers with meaningful names, for example,

Quoted 3 lines

bool dmar_find_matched_satc_unit()
bool dmar_ats_required_in_satc()?
sure, this is good. how about I add one function.

//return address of found satcu, otherwise null:
static struct dmar_satc_unit *dmar_find_matched_satc_unit(struct pci_dev *)

and check if ats is required through the returned satc unit(atc_required)

Quoted 61 lines
Quoted 7 lines
drivers/iommu/intel/iommu.c +7 -0
+static int dev_satc_state(struct pci_dev *dev)
+{
+    int i, ret = -1;
+    struct device *tmp;
+    struct dmar_satc_unit *satcu;
+    struct acpi_dmar_satc *satc;
+

drivers/iommu/intel/iommu.c static void intel_iommu_free_dmars(void) · line 4045

Quoted 10 lines
drivers/iommu/intel/iommu.c +10 -0
+    dev = pci_physfn(dev);
+    rcu_read_lock();
+
+    list_for_each_entry_rcu(satcu, &dmar_satc_units, list) {
+        satc = container_of(satcu->hdr, struct acpi_dmar_satc, header);
+        if (satc->segment != pci_domain_nr(dev->bus))
+            continue;
+        for_each_dev_scope(satcu->devices, satcu->devices_cnt, i, tmp)
+            if (to_pci_dev(tmp) == dev) {
+                if (satc->flags)

Do you need to check the ATC_REQUIRED bit field?
Thanks, I can update this upon above response.

Quoted 41 lines
drivers/iommu/intel/iommu.c +25 -0
+                    ret = 1;
+                else
+                    ret = 0;
+                goto out;
+            }
+    }
+out:
+    rcu_read_unlock();
+    return ret;
+}
+
+int dmar_ats_supported(struct pci_dev *dev, struct intel_iommu *iommu)
   {
       int i, ret = 1;
       struct pci_bus *bus;
@@ -4030,6 +4064,19 @@ int dmar_find_matched_atsr_unit(struct pci_dev 
 *dev)
       struct dmar_atsr_unit *atsru;
       dev = pci_physfn(dev);
+    i = dev_satc_state(dev);
+    if (i >= 0) {
+        /* This dev supports ATS as it is in SATC table!
+         * When IOMMU is in legacy mode, enabling ATS is done
+         * automatically by HW for the device that requires
+         * ATS, hence OS should not enable this device ATS
+         * to avoid duplicated TLB invalidation
+         */
+        if (i && !sm_supported(iommu))
+            ret = 0;
+        return ret;
+    }
+
       for (bus = dev->bus; bus; bus = bus->parent) {
           bridge = bus->self;
           /* If it's an integrated device, allow ATS */
include/linux/intel-iommu.h +0 -0
index 69230fd695ea..fe9fd417d611 100644
@@ -717,7 +717,7 @@ static inline int nr_pte_to_next_page(struct 
 dma_pte *pte)
Quoted 9 lines

include/linux/intel-iommu.h static inline int nr_pte_to_next_page(struct dma_pte *pte)

Quoted 5 lines
include/linux/intel-iommu.h +1 -1
   }
   extern struct dmar_drhd_unit * dmar_find_matched_drhd_unit(struct pci_dev *dev);
-extern int dmar_find_matched_atsr_unit(struct pci_dev *dev);
+extern int dmar_ats_supported(struct pci_dev *dev, struct intel_iommu 
 *iommu);

The dmar_find_matched_atsr_unit() is not used out of iommu.c. Can you please make a patch to change it to a static one?

sure, i will replace "int dmar_find_matched_atsr_unit" as above response.

Quoted 5 lines

include/linux/intel-iommu.h static inline int nr_pte_to_next_page(struct dma_pte *pte) · line 723

Quoted 2 lines
include/linux/intel-iommu.h +0 -0
   extern int dmar_enable_qi(struct intel_iommu *iommu);
   extern void dmar_disable_qi(struct intel_iommu *iommu);

Best regards,
baolu

Thanks,
Yian