Skip to contentSkip to conversation

iommu/amd: Mark v2 page tables incoherent on Cezanne

PATCH Applied Vasant Hegde 5 participants 7 messages Nov 20 – Nov 23

Subsystems (2)
lore
Share & tools
lore

Vasant Hegde

author maintainer
#0 lore raw

PATCH 1/1 iommu/amd: Mark v2 page tables incoherent on Cezanne

DMA fails on Cezanne (family 19h, model 50h) when a device is placed in an IOMMU v2 paging domain due to missing coherency setting in IOMMU HW. The IOMMU walks v2 page tables without observing CPU writes still held in the cache. v1 PTEs have a Force Coherent bit, which the driver sets on every entry, but the v2 format has no equivalent bit, so v2 walks are not kept coherent.

Set PT_FEAT_DMA_INCOHERENT for v2 domains on this CPU so the generic page-table code clflushes table memory before the IOMMU reads it.

---
 drivers/iommu/amd/iommu.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
drivers/iommu/amd/iommu.c +16 -0
index 41ee4ad1dcf8..0f04013f590d 100644
@@ -2792,6 +2792,15 @@ static const struct iommu_domain_ops amdv2_ops = {
 	.enforce_cache_coherency = amd_iommu_enforce_cache_coherency,
 };
 
+/*
+ * Force a clflush on Cezanne (family 19h, model 50h) when the v2
+ * page-table is enabled.
+ */
+static bool amd_iommu_force_incoherent(void)
+{
+	return boot_cpu_data.x86 == 0x19 && boot_cpu_data.x86_model == 0x50;
+}
+
 static struct iommu_domain *amd_iommu_domain_alloc_paging_v2(struct device *dev,
 							     u32 flags)
 {
@@ -2810,6 +2819,13 @@ static struct iommu_domain *amd_iommu_domain_alloc_paging_v2(struct device *dev,
 	domain->iommu.nid = dev_to_node(dev);
 
 	cfg.common.features = BIT(PT_FEAT_X86_64_AMD_ENCRYPT_TABLES);
+
+	if (amd_iommu_force_incoherent()) {
+		cfg.common.features |= BIT(PT_FEAT_DMA_INCOHERENT);
+		/* pt_iommu_init() rejects DMA_INCOHERENT without a device */
+		domain->iommu.iommu_device = dev;
+	}
+
 	if (amd_iommu_np_cache)
 		cfg.common.features |= BIT(PT_FEAT_FLUSH_RANGE_NO_GAPS);
 	else
Signature · 1 line
2.31.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Vasant Hegde

DMA fails on Cezanne (family 19h, model 50h) when a device is placed in an IOMMU v2 paging domain due to missing coherency setting in IOMMU HW. The IOMMU walks v2 page tables without observing CPU writes still held in the cache. v1 PTEs have a Force Coherent bit, which the driver sets on every entry, but the v2 format has no equivalent bit, so v2 walks are not kept coherent.

Set PT_FEAT_DMA_INCOHERENT for v2 domains on this CPU so the generic page-table code clflushes table memory before the IOMMU reads it.

Quoted 6 lines

Much appreciated for all your help here Nathan, you did a great job narrowing this down.

Quoted 39 lines
---
  drivers/iommu/amd/iommu.c | 16 ++++++++++++++++
  1 file changed, 16 insertions(+)
drivers/iommu/amd/iommu.c +16 -0
index 41ee4ad1dcf8..0f04013f590d 100644
@@ -2792,6 +2792,15 @@ static const struct iommu_domain_ops amdv2_ops = {
  	.enforce_cache_coherency = amd_iommu_enforce_cache_coherency,
  };
 
+/*
+ * Force a clflush on Cezanne (family 19h, model 50h) when the v2
+ * page-table is enabled.
+ */
+static bool amd_iommu_force_incoherent(void)
+{
+	return boot_cpu_data.x86 == 0x19 && boot_cpu_data.x86_model == 0x50;
+}
+
  static struct iommu_domain *amd_iommu_domain_alloc_paging_v2(struct device *dev,
  							     u32 flags)
  {
@@ -2810,6 +2819,13 @@ static struct iommu_domain *amd_iommu_domain_alloc_paging_v2(struct device *dev,
  	domain->iommu.nid = dev_to_node(dev);
 
  	cfg.common.features = BIT(PT_FEAT_X86_64_AMD_ENCRYPT_TABLES);
+
+	if (amd_iommu_force_incoherent()) {
+		cfg.common.features |= BIT(PT_FEAT_DMA_INCOHERENT);
+		/* pt_iommu_init() rejects DMA_INCOHERENT without a device */
+		domain->iommu.iommu_device = dev;
+	}
+
  	if (amd_iommu_np_cache)
  		cfg.common.features |= BIT(PT_FEAT_FLUSH_RANGE_NO_GAPS);
  	else
Tested-by on v1 1/1
#2 lore raw
In reply to Vasant Hegde

Hi Vasant,

I tested the posted patch on pristine v7.4-rc3 with a Ryzen 5 5600G (Cezanne) and Radeon RX 5500 XT. On the same setup, unpatched rc3 hit an IOMMU page fault and failed PSP initialization. With the patch, the GPU initialized successfully. The IOMMU device table confirmed translated v2 DMA-FQ without an amd_iommu= override.

As a simple test, I also used the RX 5500 XT to hardware-decode and transcode the full 1080p60 Big Buck Bunny movie from H.264 to HEVC, then hardware-decoded all frames of the output. No IOMMU faults, GPU resets, or DMA-API errors were observed.

Thanks,
Nathan

Quoted 57 lines · Vasant Hegde

DMA fails on Cezanne (family 19h, model 50h) when a device is placed in an IOMMU v2 paging domain due to missing coherency setting in IOMMU HW. The IOMMU walks v2 page tables without observing CPU writes still held in the cache. v1 PTEs have a Force Coherent bit, which the driver sets on every entry, but the v2 format has no equivalent bit, so v2 walks are not kept coherent.

Set PT_FEAT_DMA_INCOHERENT for v2 domains on this CPU so the generic page-table code clflushes table memory before the IOMMU reads it.

---
 drivers/iommu/amd/iommu.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
drivers/iommu/amd/iommu.c +16 -1
index 41ee4ad1dcf8..0f04013f590d 100644
@@ -2792,6 +2792,15 @@ static const struct iommu_domain_ops amdv2_ops = {
      .enforce_cache_coherency = amd_iommu_enforce_cache_coherency,
 };
 
+/*
+ * Force a clflush on Cezanne (family 19h, model 50h) when the v2
+ * page-table is enabled.
+ */
+static bool amd_iommu_force_incoherent(void)
+{
+     return boot_cpu_data.x86 == 0x19 && boot_cpu_data.x86_model == 0x50;
+}
+
 static struct iommu_domain *amd_iommu_domain_alloc_paging_v2(struct device *dev,
                                                           u32 flags)
 {
@@ -2810,6 +2819,13 @@ static struct iommu_domain *amd_iommu_domain_alloc_paging_v2(struct device *dev,
      domain->iommu.nid = dev_to_node(dev);
 
      cfg.common.features = BIT(PT_FEAT_X86_64_AMD_ENCRYPT_TABLES);
+
+     if (amd_iommu_force_incoherent()) {
+             cfg.common.features |= BIT(PT_FEAT_DMA_INCOHERENT);
+             /* pt_iommu_init() rejects DMA_INCOHERENT without a device */
+             domain->iommu.iommu_device = dev;
+     }
+
      if (amd_iommu_np_cache)
              cfg.common.features |= BIT(PT_FEAT_FLUSH_RANGE_NO_GAPS);
      else
--

2.31.1

Reviewed-by on v1 1/1
#3 lore raw
In reply to Vasant Hegde
Quoted 6 lines · Vasant Hegde

DMA fails on Cezanne (family 19h, model 50h) when a device is placed in an IOMMU v2 paging domain due to missing coherency setting in IOMMU HW. The IOMMU walks v2 page tables without observing CPU writes still held in the cache. v1 PTEs have a Force Coherent bit, which the driver sets on every entry, but the v2 format has no equivalent bit, so v2 walks are not kept coherent.

That seems like an enormous obvious hardware bug? I'm shocked it was released like that..

Is anything else non-coherent by accident?

Quoted 4 lines

+/*
+ * Force a clflush on Cezanne (family 19h, model 50h) when the v2
+ * page-table is enabled.
+ */

No errata #?

It seems OK to me otherwise

Jason

author maintainer
#4 lore raw
In reply to Jason Gunthorpe
Quoted 10 lines · Jason Gunthorpe
Quoted 6 lines · Vasant Hegde

DMA fails on Cezanne (family 19h, model 50h) when a device is placed in an IOMMU v2 paging domain due to missing coherency setting in IOMMU HW. The IOMMU walks v2 page tables without observing CPU writes still held in the cache. v1 PTEs have a Force Coherent bit, which the driver sets on every entry, but the v2 format has no equivalent bit, so v2 walks are not kept coherent.

That seems like an enormous obvious hardware bug? I'm shocked it was released like that..

I believe this is missing BIOS programming.

Quoted 2 lines

Is anything else non-coherent by accident?

We ran tests on as many variants we could get it. As we know this is the only platform hit this bug.

-Vasant

#5 lore raw
Quoted 11 lines · Vasant Hegde
Quoted 8 lines · Jason Gunthorpe
Quoted 6 lines · Vasant Hegde

DMA fails on Cezanne (family 19h, model 50h) when a device is placed in an IOMMU v2 paging domain due to missing coherency setting in IOMMU HW. The IOMMU walks v2 page tables without observing CPU writes still held in the cache. v1 PTEs have a Force Coherent bit, which the driver sets on every entry, but the v2 format has no equivalent bit, so v2 walks are not kept coherent.

Quoted 4 lines
Quoted 2 lines

That seems like an enormous obvious hardware bug? I'm shocked it was released like that..

I believe this is missing BIOS programming.

So a BIOS upgrade fixes it? Seems rough to patch the kernel based on sillicon ID then.

Because you also have to block viommu if the S2 is non-coherent, this patch should be updated.

And that's a big impact for a BIOS bug that will be fixed for some people...

Jason