Skip to contentSkip to conversation

Add page alignment check in Intel IOMMU.

PATCH Superseded [email protected] 4 participants 13 messages Feb 7, 2019 – Feb 11, 2019

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Add page alignment check in Intel IOMMU.

From: Kuppuswamy Sathyanarayanan <[email protected]>

As per Intel vt-d specification, Rev 3.0 (section 7.5.1.1, title "Page Request Descriptor"), Intel IOMMU page request descriptor only uses bits[63:12] of the page address. Hence its required to enforce that the device will only send page request with page-aligned address. So, this patch set adds support to verify whether the device uses page aligned address before enabling the ATS service in Intel IOMMU driver.

Kuppuswamy Sathyanarayanan (2):
PCI: ATS: Add function to check ATS page aligned request status. iommu/vt-d: Enable ATS only if the device uses page aligned address.

 drivers/iommu/intel-iommu.c   |  1 +
 drivers/pci/ats.c             | 22 ++++++++++++++++++++++
 include/linux/pci.h           |  2 ++
 include/uapi/linux/pci_regs.h |  1 +
 4 files changed, 26 insertions(+)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/2 PCI: ATS: Add function to check ATS page aligned request status.

From: Kuppuswamy Sathyanarayanan <[email protected]>

Add a new function to return the status of ATS page aligned request bit in ATS capability register. This function will be used by drivers like IOMMU, if it is required to enforce page-aligned requests in ATS.

---
 drivers/pci/ats.c             | 22 ++++++++++++++++++++++
 include/linux/pci.h           |  2 ++
 include/uapi/linux/pci_regs.h |  1 +
 3 files changed, 25 insertions(+)
drivers/pci/ats.c +22 -0
index 5b78f3b1b918..7d14b9a1981e 100644
@@ -142,6 +142,28 @@ int pci_ats_queue_depth(struct pci_dev *dev)
 }
 EXPORT_SYMBOL_GPL(pci_ats_queue_depth);
 
+/**
+ * pci_ats_page_aligned - Return ATS page aligned request bit status.
+ * @pdev: the PCI device
+ *
+ * Returns  value > 0 if address is aligned or 0 otherwise.
+ *
+ * As per PCI spec, If page aligned request bit is set, it indicates
+ * the untranslated address is always aligned to a 4096 byte boundary.
+ */
+int pci_ats_page_aligned(struct pci_dev *pdev)
+{
+	u16 cap;
+
+	if (!pdev->ats_cap)
+		return 0;
+
+	pci_read_config_word(pdev, pdev->ats_cap + PCI_ATS_CAP, &cap);
+
+	return PCI_ATS_CAP_PAGE_ALIGNED(cap);
+}
+EXPORT_SYMBOL_GPL(pci_ats_page_aligned);
+
 #ifdef CONFIG_PCI_PRI
 /**
  * pci_enable_pri - Enable PRI capability
include/linux/pci.h +2 -0
index 65f1d8c2f082..9724a8c0496b 100644
@@ -1524,11 +1524,13 @@ void pci_ats_init(struct pci_dev *dev);
 int pci_enable_ats(struct pci_dev *dev, int ps);
 void pci_disable_ats(struct pci_dev *dev);
 int pci_ats_queue_depth(struct pci_dev *dev);
+int pci_ats_page_aligned(struct pci_dev *dev);
 #else
 static inline void pci_ats_init(struct pci_dev *d) { }
 static inline int pci_enable_ats(struct pci_dev *d, int ps) { return -ENODEV; }
 static inline void pci_disable_ats(struct pci_dev *d) { }
 static inline int pci_ats_queue_depth(struct pci_dev *d) { return -ENODEV; }
+static inline int pci_ats_page_aligned(struct pci_dev *dev) { return 0; }
 #endif
 
 #ifdef CONFIG_PCIE_PTM
include/uapi/linux/pci_regs.h +1 -0
index e1e9888c85e6..d42a759867b8 100644
@@ -866,6 +866,7 @@
 #define PCI_ATS_CAP		0x04	/* ATS Capability Register */
 #define  PCI_ATS_CAP_QDEP(x)	((x) & 0x1f)	/* Invalidate Queue Depth */
 #define  PCI_ATS_MAX_QDEP	32	/* Max Invalidate Queue Depth */
+#define  PCI_ATS_CAP_PAGE_ALIGNED(x)	0x0020 /* Page Aligned Request */
 #define PCI_ATS_CTRL		0x06	/* ATS Control Register */
 #define  PCI_ATS_CTRL_ENABLE	0x8000	/* ATS Enable */
 #define  PCI_ATS_CTRL_STU(x)	((x) & 0x1f)	/* Smallest Translation Unit */
Signature · 1 line
2.20.1
#2 lore raw

Re: [PATCH v1 1/2] PCI: ATS: Add function to check ATS page aligned request status.

Hi Kuppuswamy,

Previous changes to ats.c used subject lines starting with just "PCI:".

I think it does make sense to include "ATS", but please do it in the way we do it for other PCI features, e.g.,

PCI/ATS: Add pci_ats_page_aligned() interface

Quoted 6 lines · On Thu, Feb 07, 2019 at 10:41:13AM -0800…

From: Kuppuswamy Sathyanarayanan <[email protected]>

Add a new function to return the status of ATS page aligned request bit in ATS capability register. This function will be used by drivers like IOMMU, if it is required to enforce page-aligned requests in ATS.

"return the Page Aligned Request bit in the ATS Capability Register"

This is just to make the terminology match the PCIe spec exactly so it's easier to look up.

Quoted 16 lines
---
 drivers/pci/ats.c             | 22 ++++++++++++++++++++++
 include/linux/pci.h           |  2 ++
 include/uapi/linux/pci_regs.h |  1 +
 3 files changed, 25 insertions(+)
drivers/pci/ats.c +0 -0
index 5b78f3b1b918..7d14b9a1981e 100644

drivers/pci/ats.c int pci_ats_queue_depth(struct pci_dev *dev) · line 146

Quoted 5 lines
drivers/pci/ats.c +2 -0
@@ -142,6 +142,28 @@ int pci_ats_queue_depth(struct pci_dev *dev)
 }
 EXPORT_SYMBOL_GPL(pci_ats_queue_depth);
 
+/**
+ * pci_ats_page_aligned - Return ATS page aligned request bit status.

Capitalize name of bit as above.

drivers/pci/ats.c int pci_ats_queue_depth(struct pci_dev *dev) · line 149

Quoted 3 lines
drivers/pci/ats.c +3 -0
+ * @pdev: the PCI device
+ *
+ * Returns  value > 0 if address is aligned or 0 otherwise.

s/ / /

"if Untranslated Addresses generated by the device are always aligned or ..."

drivers/pci/ats.c int pci_ats_queue_depth(struct pci_dev *dev) · line 152

Quoted 3 lines
drivers/pci/ats.c +3 -0
+ *
+ * As per PCI spec, If page aligned request bit is set, it indicates
+ * the untranslated address is always aligned to a 4096 byte boundary.

"Per PCIe r4.0, sec 10.5.1.2, if the Page Aligned Request bit, Untranslated Addresses generated by the device are always aligned to a 4096 byte boundary."

Quoted 40 lines
drivers/pci/ats.c +14 -0
+ */
+int pci_ats_page_aligned(struct pci_dev *pdev)
+{
+	u16 cap;
+
+	if (!pdev->ats_cap)
+		return 0;
+
+	pci_read_config_word(pdev, pdev->ats_cap + PCI_ATS_CAP, &cap);
+
+	return PCI_ATS_CAP_PAGE_ALIGNED(cap);
+}
+EXPORT_SYMBOL_GPL(pci_ats_page_aligned);
+
 #ifdef CONFIG_PCI_PRI
 /**
  * pci_enable_pri - Enable PRI capability
include/linux/pci.h +2 -0
index 65f1d8c2f082..9724a8c0496b 100644
@@ -1524,11 +1524,13 @@ void pci_ats_init(struct pci_dev *dev);
 int pci_enable_ats(struct pci_dev *dev, int ps);
 void pci_disable_ats(struct pci_dev *dev);
 int pci_ats_queue_depth(struct pci_dev *dev);
+int pci_ats_page_aligned(struct pci_dev *dev);
 #else
 static inline void pci_ats_init(struct pci_dev *d) { }
 static inline int pci_enable_ats(struct pci_dev *d, int ps) { return -ENODEV; }
 static inline void pci_disable_ats(struct pci_dev *d) { }
 static inline int pci_ats_queue_depth(struct pci_dev *d) { return -ENODEV; }
+static inline int pci_ats_page_aligned(struct pci_dev *dev) { return 0; }
 #endif
 
 #ifdef CONFIG_PCIE_PTM
include/uapi/linux/pci_regs.h +0 -0
index e1e9888c85e6..d42a759867b8 100644

include/uapi/linux/pci_regs.h line 869

Quoted 4 lines
include/uapi/linux/pci_regs.h +1 -0
@@ -866,6 +866,7 @@
 #define PCI_ATS_CAP		0x04	/* ATS Capability Register */
 #define  PCI_ATS_CAP_QDEP(x)	((x) & 0x1f)	/* Invalidate Queue Depth */
 #define  PCI_ATS_MAX_QDEP	32	/* Max Invalidate Queue Depth */
+#define  PCI_ATS_CAP_PAGE_ALIGNED(x)	0x0020 /* Page Aligned Request */

This is wrong because it *always* returns "true", regardless of the value of the ATS Capability register.

I would prefer this:

  #define  PCI_ATS_CAP_PAGE_ALIGNED   0x0020

and then test it like this:

if (cap & PCI_ATS_CAP_PAGE_ALIGNED)

    return 1;

return 0;

Quoted 6 lines
include/uapi/linux/pci_regs.h +0 -0
 #define PCI_ATS_CTRL		0x06	/* ATS Control Register */
 #define  PCI_ATS_CTRL_ENABLE	0x8000	/* ATS Enable */
 #define  PCI_ATS_CTRL_STU(x)	((x) & 0x1f)	/* Smallest Translation Unit */
Signature · 1 line
2.20.1
#3 lore raw

Re: [PATCH v1 1/2] PCI: ATS: Add function to check ATS page aligned request status.

Quoted 9 lines · Bjorn Helgaas

Hi Kuppuswamy,

Previous changes to ats.c used subject lines starting with just "PCI:".

I think it does make sense to include "ATS", but please do it in the way we do it for other PCI features, e.g.,

PCI/ATS: Add pci_ats_page_aligned() interface

Got it. I will follow PCI/ATS format.

Quoted 12 lines · On Thu, Feb 07, 2019 at 10:41:13AM -0800…
Quoted 6 lines

From: Kuppuswamy Sathyanarayanan <[email protected]>

Add a new function to return the status of ATS page aligned request bit in ATS capability register. This function will be used by drivers like IOMMU, if it is required to enforce page-aligned requests in ATS.

"return the Page Aligned Request bit in the ATS Capability Register"

This is just to make the terminology match the PCIe spec exactly so it's easier to look up.

Will fix it in next version.

Quoted 20 lines
Quoted 20 lines
---
  drivers/pci/ats.c             | 22 ++++++++++++++++++++++
  include/linux/pci.h           |  2 ++
  include/uapi/linux/pci_regs.h |  1 +
  3 files changed, 25 insertions(+)
drivers/pci/ats.c +0 -0
index 5b78f3b1b918..7d14b9a1981e 100644
@@ -142,6 +142,28 @@ int pci_ats_queue_depth(struct pci_dev *dev)
  }
  EXPORT_SYMBOL_GPL(pci_ats_queue_depth);
 
Quoted 3 lines

drivers/pci/ats.c int pci_ats_queue_depth(struct pci_dev *dev) · line 146

Quoted 2 lines
drivers/pci/ats.c +2 -0
+/**
+ * pci_ats_page_aligned - Return ATS page aligned request bit status.

Capitalize name of bit as above.

Agreed.

Quoted 63 lines

drivers/pci/ats.c int pci_ats_queue_depth(struct pci_dev *dev) · line 149

Quoted 4 lines
drivers/pci/ats.c +3 -0
+ * @pdev: the PCI device
+ *
+ * Returns  value > 0 if address is aligned or 0 otherwise.

s/ / /

"if Untranslated Addresses generated by the device are always aligned or ..."

drivers/pci/ats.c int pci_ats_queue_depth(struct pci_dev *dev) · line 152

Quoted 3 lines
drivers/pci/ats.c +3 -0
+ *
+ * As per PCI spec, If page aligned request bit is set, it indicates
+ * the untranslated address is always aligned to a 4096 byte boundary.

"Per PCIe r4.0, sec 10.5.1.2, if the Page Aligned Request bit, Untranslated Addresses generated by the device are always aligned to a 4096 byte boundary."

Quoted 40 lines
drivers/pci/ats.c +14 -0
+ */
+int pci_ats_page_aligned(struct pci_dev *pdev)
+{
+	u16 cap;
+
+	if (!pdev->ats_cap)
+		return 0;
+
+	pci_read_config_word(pdev, pdev->ats_cap + PCI_ATS_CAP, &cap);
+
+	return PCI_ATS_CAP_PAGE_ALIGNED(cap);
+}
+EXPORT_SYMBOL_GPL(pci_ats_page_aligned);
+
  #ifdef CONFIG_PCI_PRI
  /**
   * pci_enable_pri - Enable PRI capability
include/linux/pci.h +2 -0
index 65f1d8c2f082..9724a8c0496b 100644
@@ -1524,11 +1524,13 @@ void pci_ats_init(struct pci_dev *dev);
  int pci_enable_ats(struct pci_dev *dev, int ps);
  void pci_disable_ats(struct pci_dev *dev);
  int pci_ats_queue_depth(struct pci_dev *dev);
+int pci_ats_page_aligned(struct pci_dev *dev);
  #else
  static inline void pci_ats_init(struct pci_dev *d) { }
  static inline int pci_enable_ats(struct pci_dev *d, int ps) { return -ENODEV; }
  static inline void pci_disable_ats(struct pci_dev *d) { }
  static inline int pci_ats_queue_depth(struct pci_dev *d) { return -ENODEV; }
+static inline int pci_ats_page_aligned(struct pci_dev *dev) { return 0; }
  #endif
 
  #ifdef CONFIG_PCIE_PTM
include/uapi/linux/pci_regs.h +0 -0
index e1e9888c85e6..d42a759867b8 100644

include/uapi/linux/pci_regs.h line 869

Quoted 4 lines
include/uapi/linux/pci_regs.h +1 -0
@@ -866,6 +866,7 @@
  #define PCI_ATS_CAP		0x04	/* ATS Capability Register */
  #define  PCI_ATS_CAP_QDEP(x)	((x) & 0x1f)	/* Invalidate Queue Depth */
  #define  PCI_ATS_MAX_QDEP	32	/* Max Invalidate Queue Depth */
+#define  PCI_ATS_CAP_PAGE_ALIGNED(x)	0x0020 /* Page Aligned Request */

This is wrong because it *always* returns "true", regardless of the value of the ATS Capability register.

Quoted 3 lines

I would prefer this:

   #define  PCI_ATS_CAP_PAGE_ALIGNED   0x0020

Good catch. it looks like I messed it when I did some cleanup. Sorry about it. Initially it was (x & 0x0020).

Quoted 6 lines

and then test it like this:

   if (cap & PCI_ATS_CAP_PAGE_ALIGNED)
     return 1;
   return 0;

Ok.

Quoted 6 lines
Quoted 6 lines
include/uapi/linux/pci_regs.h +0 -0
  #define PCI_ATS_CTRL		0x06	/* ATS Control Register */
  #define  PCI_ATS_CTRL_ENABLE	0x8000	/* ATS Enable */
  #define  PCI_ATS_CTRL_STU(x)	((x) & 0x1f)	/* Smallest Translation Unit */
Signature · 1 line
2.20.1
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux kernel developer
#4 lore raw

Re: [PATCH v1 1/2] PCI: ATS: Add function to check ATS page aligned request status.

In reply to [email protected]

drivers/pci/ats.c int pci_ats_queue_depth(struct pci_dev *dev) · line 161

Quoted 11 lines · On 2/7/2019 1:41 PM, [email protected] wrote:

+ * As per PCI spec, If page aligned request bit is set, it indicates
+ * the untranslated address is always aligned to a 4096 byte boundary.
+ */

drivers/pci/ats.c +8 -0
+int pci_ats_page_aligned(struct pci_dev *pdev)
+{
+	u16 cap;
+
+	if (!pdev->ats_cap)
+		return 0;
+
+	pci_read_config_word(pdev, pdev->ats_cap + PCI_ATS_CAP, &cap);

If I remember this right, aligned request is only supported on ATS v1.1 but not supported on v1.0.

Can you please check the spec?

#5 lore raw

Re: [PATCH v1 1/2] PCI: ATS: Add function to check ATS page aligned request status.

Quoted 16 lines · Sinan Kaya

drivers/pci/ats.c int pci_ats_queue_depth(struct pci_dev *dev) · line 161

Quoted 11 lines · On 2/7/2019 1:41 PM, [email protected] wrote:

+ * As per PCI spec, If page aligned request bit is set, it indicates
+ * the untranslated address is always aligned to a 4096 byte boundary.
+ */

drivers/pci/ats.c +8 -0
+int pci_ats_page_aligned(struct pci_dev *pdev)
+{
+    u16 cap;
+
+    if (!pdev->ats_cap)
+        return 0;
+
+    pci_read_config_word(pdev, pdev->ats_cap + PCI_ATS_CAP, &cap);

If I remember this right, aligned request is only supported on ATS v1.1 but not supported on v1.0.

Its added in v1.1.

Quoted 3 lines

Can you please check the spec?

Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux kernel developer
#6 lore raw

Re: [PATCH v1 1/2] PCI: ATS: Add function to check ATS page aligned request status.

Quoted 3 lines · sathyanarayanan kuppuswamy
Quoted 2 lines

If I remember this right, aligned request is only supported on ATS v1.1 but not supported on v1.0.

Its added in v1.1.

This means that you should probably have some kind of version check here.

#7 lore raw

Re: [PATCH v1 1/2] PCI: ATS: Add function to check ATS page aligned request status.

Quoted 8 lines · Sinan Kaya
Quoted 3 lines · sathyanarayanan kuppuswamy
Quoted 2 lines

If I remember this right, aligned request is only supported on ATS v1.1 but not supported on v1.0.

Its added in v1.1.

This means that you should probably have some kind of version check here.

There is no version field in ATS v1.0 spec. Also, If I follow the history log in PCI spec, I think ATS if first added at v1.2. Please correct me if I am wrong.

Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux kernel developer
#8 lore raw

Re: [PATCH v1 1/2] PCI: ATS: Add function to check ATS page aligned request status.

Quoted 4 lines · sathyanarayanan kuppuswamy
Quoted 2 lines

This means that you should probably have some kind of version check here.

There is no version field in ATS v1.0 spec. Also, If I follow the history log in PCI spec, I think ATS if first added at v1.2. Please correct me if I am wrong.

v1.2 was incorporated into PCIe spec at that time. However, the ATS spec is old and there could be some HW that could claim to be ATS compatible. I know AMD GPUs declare ATS capability.

See this ECN

https://composter.com.ua/documents/ats_r1.1_26Jan09.pdf

You need to validate the version field from ATS capability header to be 1 before reading this register.

See Table 5-1: ATS Extended Capability Header

#9 lore raw

Re: [PATCH v1 1/2] PCI: ATS: Add function to check ATS page aligned request status.

Quoted 11 lines · Sinan Kaya
Quoted 6 lines · sathyanarayanan kuppuswamy
Quoted 2 lines

This means that you should probably have some kind of version check here.

There is no version field in ATS v1.0 spec. Also, If I follow the history log in PCI spec, I think ATS if first added at v1.2. Please correct me if I am wrong.

v1.2 was incorporated into PCIe spec at that time. However, the ATS spec is old and there could be some HW that could claim to be ATS compatible. I know AMD GPUs declare ATS capability.

It seems rather odd we have to check for ATS version.

I always assumed unspecified bits (Reserved) must be 0. We only check this if ATS is enabled, and this particular bit wasn't given away for another feature.

Is it really required to check for ATS version before consuming this?

Quoted 9 lines

See this ECN

https://composter.com.ua/documents/ats_r1.1_26Jan09.pdf

You need to validate the version field from ATS capability header to be 1 before reading this register.

See Table 5-1: ATS Extended Capability Header

#10 lore raw

Re: [PATCH v1 1/2] PCI: ATS: Add function to check ATS page aligned request status.

Quoted 10 lines · Ashok
Quoted 9 lines · Sinan Kaya
Quoted 5 lines · sathyanarayanan kuppuswamy
Quoted 2 lines

This means that you should probably have some kind of version check here.

There is no version field in ATS v1.0 spec. Also, If I follow the history log in PCI spec, I think ATS if first added at v1.2. Please correct me if I am wrong.

v1.2 was incorporated into PCIe spec at that time. However, the ATS spec is old and there could be some HW that could claim to be ATS compatible. I know AMD GPUs declare ATS capability.

Quoted 7 lines

It seems rather odd we have to check for ATS version.

I always assumed unspecified bits (Reserved) must be 0. We only check this if ATS is enabled, and this particular bit wasn't given away for another feature.

Is it really required to check for ATS version before consuming this?

If the version check is required then, it needs to be added before reading "Invalidate Queue Depth" value as well.

Quoted 10 lines
Quoted 10 lines

See this ECN

https://composter.com.ua/documents/ats_r1.1_26Jan09.pdf

You need to validate the version field from ATS capability header to be 1 before reading this register.

See Table 5-1: ATS Extended Capability Header

Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux kernel developer
#11 lore raw

Re: [PATCH v1 1/2] PCI: ATS: Add function to check ATS page aligned request status.

In reply to Raj, Ashok
Quoted 7 lines · Ashok

It seems rather odd we have to check for ATS version.

I always assumed unspecified bits (Reserved) must be 0. We only check this if ATS is enabled, and this particular bit wasn't given away for another feature.

Is it really required to check for ATS version before consuming this?

Reading again, it looks like version check is not necessary since it is implied by the presence of this bit per this paragraph.

Page Aligned Request – If Set, indicates the Untranslated Address is always aligned to a 4096 byte boundary. Setting this bit is recommended. This bit permits software to distinguish between implementations compatible with earlier version of this specification that permitted a requester to supply anything in bits [11:2].

#12 lore raw

PATCH 2/2 iommu/vt-d: Enable ATS only if the device uses page aligned address.

In reply to [email protected]

From: Kuppuswamy Sathyanarayanan <[email protected]>

As per Intel vt-d specification, Rev 3.0 (section 7.5.1.1, title "Page Request Descriptor"), Intel IOMMU page request descriptor only uses bits[63:12] of the page address. Hence Intel IOMMU driver would only permit devices that advertise they would only send page aligned requests to participate in ATS service.

---
 drivers/iommu/intel-iommu.c | 1 +
 1 file changed, 1 insertion(+)
drivers/iommu/intel-iommu.c +1 -0
index 1457f931218e..6a0b5270cd2e 100644
@@ -1406,6 +1406,7 @@ static void iommu_enable_dev_iotlb(struct device_domain_info *info)
 		info->pri_enabled = 1;
 #endif
 	if (!pdev->untrusted && info->ats_supported &&
+	    pci_ats_page_aligned(pdev) &&
 	    !pci_enable_ats(pdev, VTD_PAGE_SHIFT)) {
 		info->ats_enabled = 1;
 		domain_update_iotlb(info->domain);
Signature · 1 line
2.20.1