Skip to contentSkip to conversation

iommu/amd: Make PerfOpt compulsory

PATCH Superseded Mario Limonciello (AMD) 3 participants 4 messages Sep 30 – Oct 1

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 iommu/amd: Make PerfOpt compulsory

PerfOpt is only a feature usable by integrated GPUs and only in identity mode. Instead of leaving a policy knob in amdgpu, just turn it on when an integrated GPU is in identity.

This drops quite a bit of compatibility glue. There was a refcounting system, exported symbols, and device attach/detach logic. By just setting it immediately it's a lot more straightforward.

---
v3:
* Move amd_iommu_perfopt_set() to init.c and set static (Vasant)
* Clear bit if it fails to enable (Vasant)
* Use pr_err() instead of dev_err for early errors (Sashiko)
* Restore attach_device check for VFIO cases (Sashiko)
* Handle VFIO case (Sashiko)
* Handle multi-IOMMU case (Sashiko)
v2:
* Move WARN_ON to amd_iommu_perfopt_clear()
* Make amd_iommu_perfopt_clear() void
* Drop unnecessary cleanup/create paths that unset feature
* Enable the feature in IOMMU if supported (per guidance in IVRS spec)
* Use existing check_feature() helper instead

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h        |   1 -
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  50 -----
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c    |  12 --
 drivers/iommu/amd/amd_iommu.h              |   3 -
 drivers/iommu/amd/amd_iommu_types.h        |   3 -
 drivers/iommu/amd/init.c                   |  81 ++++----
 drivers/iommu/amd/iommu.c                  | 215 +--------------------
 include/linux/amd-iommu.h                  |  11 --
 8 files changed, 42 insertions(+), 334 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +0 -1
index 0e53a02ad1bab..a9c6f5d4a6397 100644
@@ -156,7 +156,6 @@ struct amdgpu_watchdog_timer {
  * Modules parameters.
  */
 extern int amdgpu_modeset;
-extern int amdgpu_iommu_perfopt;
 extern unsigned int amdgpu_vram_limit;
 extern int amdgpu_vis_vram_limit;
 extern int amdgpu_gart_size;
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -50

80 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +0 -12
index 3ca98091ce487..5c33c19fd9bc5 100644
@@ -185,7 +185,6 @@ char *amdgpu_disable_cu;
 char *amdgpu_virtual_display;
 int amdgpu_enforce_isolation = -1;
 int amdgpu_modeset = -1;
-int amdgpu_iommu_perfopt = -1;
 
 /* Specifies the default granularity for SVM, used in buffer
  * migration and restoration of backing memory when handling
@@ -393,17 +392,6 @@ module_param_named(fw_load_type, amdgpu_fw_load_type, int, 0444);
 MODULE_PARM_DESC(aspm, "ASPM support (1 = enable, 0 = disable, -1 = auto)");
 module_param_named(aspm, amdgpu_aspm, int, 0444);
 
-/**
- * DOC: iommu_perfopt (int)
- * Control the AMD IOMMU PerfOpt DMA-latency optimization
- * (0 = disable; -1 = enable on supported devices).
- * This arms the IOMMU PerfOpt control (IOMMU spec, MMIO Offset 016Ch, EFR PerfOptSup / PerfOptEn)
- * Arming it disables ATS, PRI, PASID and SVA for the GPU and removes IOMMU DMA containment for it,
- * trading isolation for lower DMA latency.
- */
-MODULE_PARM_DESC(iommu_perfopt, "Control IOMMU PerfOpt DMA-latency optimization (-1 = enable on supported devices, 0 = disable)");
-module_param_named(iommu_perfopt, amdgpu_iommu_perfopt, int, 0444);
-
 /**
  * DOC: runpm (int)
  * Override for runtime power management control for dGPUs. The amdgpu driver can dynamically power down
drivers/iommu/amd/amd_iommu.h +0 -3
index 93845b3c3fbf7..71113e8608594 100644
@@ -48,9 +48,6 @@ extern u8 amd_iommu_hpt_vasize;
 extern unsigned long amd_iommu_pgsize_bitmap;
 extern bool amd_iommu_hatdis;
 
-int amd_iommu_perfopt_clear(struct amd_iommu *iommu);
-int amd_iommu_perfopt_restore(struct amd_iommu *iommu);
-
 /* Protection domain ops */
 void amd_iommu_init_identity_domain(void);
 struct protection_domain *protection_domain_alloc(void);
drivers/iommu/amd/amd_iommu_types.h +0 -3
index 1c1624b7c9a36..9d873819304fb 100644
@@ -657,9 +657,6 @@ struct amd_iommu {
 	/* Extended features 2 */
 	u64 features2;
 
-	/* Devices requesting PerfOpt; the shared PERF_OPT_EN bit is on while >0. Protected by @lock. */
-	int perfopt_refcount;
-
 	/* PCI device id of the IOMMU device */
 	u16 devid;
 
drivers/iommu/amd/init.c +38 -43

127 lines not on this page · raw message

drivers/iommu/amd/iommu.c +4 -211

251 lines not on this page · raw message

include/linux/amd-iommu.h +0 -11
index 104817e8d8977..5c9dcf720f846 100644
@@ -76,15 +76,4 @@ static inline int amd_iommu_snp_disable(void) { return 0; }
 static inline bool amd_iommu_sev_tio_supported(void) { return false; }
 #endif
 
-#ifdef CONFIG_AMD_IOMMU
-int amd_iommu_enable_perfopt(struct pci_dev *pdev);
-void amd_iommu_disable_perfopt(struct pci_dev *pdev);
-#else
-static inline int amd_iommu_enable_perfopt(struct pci_dev *pdev)
-{
-	return 0;
-}
-static inline void amd_iommu_disable_perfopt(struct pci_dev *pdev) { }
-#endif
-
 #endif /* _ASM_X86_AMD_IOMMU_H */
Signature · 1 line
2.53.0
#1 lore raw

drivers/iommu/amd/iommu.c static int attach_device(struct device *dev, · line 2374

Quoted 8 lines

[ ... 336 lines skipped ... ]

drivers/iommu/amd/iommu.c +1 -1
@@ -2370,7 +2370,7 @@ static int attach_device(struct device *dev,
 	if (ret)
 		goto out;
 
-	if (dev_data->perfopt)
+	if (dev_data->perfopt && pdom_is_in_pt_mode(domain))
 		goto skip_caps;

This should also disable PASID support since without a gcr3 table that's broken. Set the max_pasid to zero during probe device.

The flow is weird like this because the driver still hasn't cleaned up the identity/blocking domain flows. There is no need to track them in lists and things because they never need invalidation..

This is much nicer if the above could be written in side amd_iommu_identity_attach().

Also, I'm a little confused, I thought this had to be toggled on and off so it is only set while in identity, how does the DTE influence what happens when in this special mode? I was sort of expecting it was ignored entirely in HW. This version seems to lock it to always on, but still permits a blocking domain to attach.

Signature · 1 line
Jason
maintainer
#2 lore raw
Quoted 11 lines · Jason Gunthorpe

drivers/iommu/amd/iommu.c static int attach_device(struct device *dev, · line 2374

Quoted 8 lines

[ ... 336 lines skipped ... ]

drivers/iommu/amd/iommu.c +1 -1
@@ -2370,7 +2370,7 @@ static int attach_device(struct device *dev,
 	if (ret)
 		goto out;
 
-	if (dev_data->perfopt)
+	if (dev_data->perfopt && pdom_is_in_pt_mode(domain))
 		goto skip_caps;

This should also disable PASID support since without a gcr3 table that's broken. Set the max_pasid to zero during probe device.

I think we can add it inside amd_iommu_probe_device().

Quoted 4 lines

The flow is weird like this because the driver still hasn't cleaned up the identity/blocking domain flows. There is no need to track them in lists and things because they never need invalidation..

Right. attach_device path cleanup is in my list. Now that _probe() path is settled (thanks to Pranjal), this is probably next item to pick up.

Quoted 3 lines

This is much nicer if the above could be written in side amd_iommu_identity_attach().

I'd say for now its fine to keep it in attach_device() as it gets called anyway. When I rework, will make sure its moved under amd_iommu_identity_attach().

Quoted 7 lines

Also, I'm a little confused, I thought this had to be toggled on and off so it is only set while in identity, how does the DTE influence what happens when in this special mode? I was sort of expecting it was ignored entirely in HW. This version seems to lock it to always on, but still permits a blocking domain to attach.

Mario, Can you check what happens if we move device to BLOCKED domain (DTE[v]=0) ?

-Vasant

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

[ ... 336 lines skipped ... ]

drivers/iommu/amd/iommu.c +0 -0
@@ -2370,7 +2370,7 @@ static int attach_device(struct device *dev,
  	if (ret)
  		goto out;
 
Quoted 8 lines
Quoted 6 lines

drivers/iommu/amd/iommu.c static int attach_device(struct device *dev, · line 2374

Quoted 3 lines
drivers/iommu/amd/iommu.c +1 -1
-	if (dev_data->perfopt)
+	if (dev_data->perfopt && pdom_is_in_pt_mode(domain))
  		goto skip_caps;

This should also disable PASID support since without a gcr3 table that's broken. Set the max_pasid to zero during probe device.

I think we can add it inside amd_iommu_probe_device().

OK, I'll rev the patch to do this.

Quoted 11 lines
Quoted 5 lines

The flow is weird like this because the driver still hasn't cleaned up the identity/blocking domain flows. There is no need to track them in lists and things because they never need invalidation..

Right. attach_device path cleanup is in my list. Now that _probe() path is settled (thanks to Pranjal), this is probably next item to pick up.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

This is much nicer if the above could be written in side amd_iommu_identity_attach().

I'd say for now its fine to keep it in attach_device() as it gets called anyway. When I rework, will make sure its moved under amd_iommu_identity_attach().

I'll leave these two items to Vasant.

Quoted 8 lines
Quoted 8 lines

Also, I'm a little confused, I thought this had to be toggled on and off so it is only set while in identity, how does the DTE influence what happens when in this special mode? I was sort of expecting it was ignored entirely in HW. This version seems to lock it to always on, but still permits a blocking domain to attach.

Yeah; AFAICT all the skips of features are mostly symbolic and for consistency when this is enabled.

I suppose really we should not permit a blocking domain to attach to make sure it's a clear message what's going on.

Quoted 4 lines

Mario, Can you check what happens if we move device to BLOCKED domain (DTE[v]=0) ?

-Vasant

I tested this on an APU that applies this.

Short answer: with PerfOpt enabled, moving the GPU towards a blocked DTE has no effect -- the device keeps doing DMA as if nothing changed. This matches Jason's expectation that the DTE is ignored entirely in HW for the device once it is in the PerfOpt/identity fast path.

Longer answer below (I had an agent help do this check).

Method
------
To keep the normal driver bound and the GPU doing real work, I added a small debugfs knob that flips the live DTE in place and back, reusing the existing paths:

clear_dte_entry() -> V=1, TV=0, IR=IW=0 (same DTE as blocking domain)
set_dte_entry() -> restore passthrough

Note the AMD "blocked" DTE is V=1/TV=0, not V=0 -- make_clear_dte() keeps V set, and TV=0 is what is supposed to block DMA. I confirmed the change via /sys/kernel/debug/iommu/amd/devtbl:

  passthrough:  QWORD[0] = 0x6000000000000003  (V=1 TV=1 IR=1 IW=1)
  blocked:      QWORD[0] = 0x0000000000000001  (V=1 TV=0 IR=0 IW=0)

The write goes through update_dte256() + iommu_flush_dte_sync() + a completion wait, so the IOMMU re-fetched the entry.

For a guaranteed IOMMU-routed DMA source I used amdgpu_benchmark test 1 (SDMA VRAM<->GTT copies). The GTT side is system memory, so for an identity device it has to traverse the IOMMU DTE.

Result
------
With the DTE blocked (TV=0), SDMA still moved 2GB to and from GTT at full speed:

dma 1024 bo moves of 1024 kB from 2 to 4 ... 5890 MB/s (GTT->VRAM) dma 1024 bo moves of 1024 kB from 4 to 2 ... 10922 MB/s (VRAM->GTT)

(vs ~10-11 GB/s unblocked, i.e. no change). During the blocked window:

- mem_target_abort (amd_iommu PMU) = 0
- no IO_PAGE_FAULT in the event log
- no SDMA error / ring timeout / GPU reset

I also tried to witness the GPU's DMA directly via the amd_iommu PMU filtered by devid. The filter itself works (NVMe, in a translating domain, shows ~120M mem_trans_total under load), but the GPU reads zero on every counter -- mem_trans_total, mem_pass_untrans, mem_target_abort -- even under the 2GB benchmark. The GPU is the only identity device on this system, so I had no non-PerfOpt passthrough device to validate the passthrough counters against; hence I am relying on the functional benchmark result above rather than the zero counts.

Conclusion
----------
Once PerfOpt is enabled and the device is in the identity/pt fast path, the DTE is effectively ignored by HW: TV=0 does not block DMA. So a blocking-domain attach would not actually stop the device while it is in this state.