git: 5172cc8d5571 - main - amdiommu: Skip the invalidation after PTE-only maps

From: Olivier Cochard <olivier_at_FreeBSD.org>
Date: Mon, 23 Nov 2026 23:30:05 UTC
The branch main has been updated by olivier:

URL: https://cgit.FreeBSD.org/src/commit/?id=5172cc8d557118964c6203954ee7b55af40c0119

commit 5172cc8d557118964c6203954ee7b55af40c0119
Author:     Olivier Cochard <olivier@FreeBSD.org>
AuthorDate: 2026-11-16 14:34:58 +0000
Commit:     Olivier Cochard <olivier@FreeBSD.org>
CommitDate: 2026-11-23 23:23:56 +0000

    amdiommu: Skip the invalidation after PTE-only maps
    
    amdiommu_map_buf() waited for an invalidation after every map, so each
    busdma load cost a command round trip and a completion interrupt.  A
    128 KB NVMe read raised 51 IOMMU interrupts on a Ryzen 7 7735HS and 66
    on a Ryzen AI Max+ PRO 395, where reads ran at 200 MB/s instead of
    900 MB/s without the IOMMU.
    
    Filling non-present PTEs needs no invalidation unless the capability
    header reports NpCache, which is how Linux handles it.  A new PDE still
    does: unmap frees empty page table pages and clears their PDE before
    the deferred invalidation runs, and skipping the flush then produced
    IO_PAGE_FAULTs from nvme(4).  A concurrent map that uses such a PDE
    before its flush completes flushes too.
    
    With this change the same reads raise 19 and 33 interrupts, and the
    second machine reads at 400 to 440 MB/s.
    
    Reviewed by:    kib, kbowling
    Approved by:    kib, kbowling
    MFC after:      1 week
    Pull Request:   https://ron-dev.freebsd.org/FreeBSD/src/pulls/113
---
 sys/x86/iommu/amd_drv.c     |  2 ++
 sys/x86/iommu/amd_idpgtbl.c | 48 +++++++++++++++++++++++++++++++++------------
 sys/x86/iommu/amd_iommu.h   |  2 ++
 3 files changed, 39 insertions(+), 13 deletions(-)

diff --git a/sys/x86/iommu/amd_drv.c b/sys/x86/iommu/amd_drv.c
index ba6abae055fa..c871681c3793 100644
--- a/sys/x86/iommu/amd_drv.c
+++ b/sys/x86/iommu/amd_drv.c
@@ -484,6 +484,8 @@ amdiommu_attach(device_t dev)
 	error = pci_find_cap(dev, PCIY_SECDEV, &sc->seccap_reg);
 	if (error != 0 || sc->seccap_reg == 0)
 		return (ENXIO);
+	sc->cap_hdr = pci_read_config(dev, sc->seccap_reg +
+	    PCIR_AMDIOMMU_CAP_HEADER, 4);
 
 	base_low = pci_read_config(dev, sc->seccap_reg +
 	    PCIR_AMDIOMMU_BASE_LOW, 4);
diff --git a/sys/x86/iommu/amd_idpgtbl.c b/sys/x86/iommu/amd_idpgtbl.c
index 007883ccdaa9..6fd5adde1e29 100644
--- a/sys/x86/iommu/amd_idpgtbl.c
+++ b/sys/x86/iommu/amd_idpgtbl.c
@@ -129,7 +129,7 @@ amdiommu_domain_free_pgtbl(struct amdiommu_domain *domain)
 
 static iommu_pte_t *
 amdiommu_pgtbl_map_pte(struct amdiommu_domain *domain, iommu_gaddr_t base,
-    int lvl, int flags, vm_pindex_t *idxp, struct sf_buf **sf)
+    int lvl, int flags, vm_pindex_t *idxp, struct sf_buf **sf, bool *pde_set)
 {
 	iommu_pte_t *pte, *ptep;
 	struct sf_buf *sfp;
@@ -162,7 +162,7 @@ retry:
 
 			sfp = NULL;
 			ptep = amdiommu_pgtbl_map_pte(domain, base, lvl - 1,
-			    flags, &idx1, &sfp);
+			    flags, &idx1, &sfp, pde_set);
 			if (ptep == NULL) {
 				KASSERT(m->pindex != 0,
 				    ("loosing root page %p", domain));
@@ -174,6 +174,15 @@ retry:
 			ptep->pte = VM_PAGE_TO_PHYS(m) |  AMDIOMMU_PTE_IR |
 			    AMDIOMMU_PTE_IW | AMDIOMMU_PTE_PR |
 			    ((domain->pglvl - lvl) << AMDIOMMU_PTE_NLVL_SHIFT);
+			/*
+			 * Count the pending flush now: a sleepable
+			 * iommu_map_pgtbl() may drop the page table lock
+			 * before this map returns.
+			 */
+			if (pde_set != NULL && !*pde_set) {
+				*pde_set = true;
+				domain->pde_flushes++;
+			}
 			vm_page_wire(sf_buf_page(sfp));
 			vm_page_unwire_noq(m);
 			iommu_unmap_pgtbl(sfp);
@@ -188,7 +197,7 @@ retry:
 static int
 amdiommu_map_buf_locked(struct amdiommu_domain *domain, iommu_gaddr_t base,
     iommu_gaddr_t size, vm_page_t *ma, uint64_t pflags, int flags,
-    struct iommu_map_entry *entry)
+    struct iommu_map_entry *entry, bool *pde_set)
 {
 	iommu_pte_t *pte;
 	struct sf_buf *sf;
@@ -202,6 +211,7 @@ amdiommu_map_buf_locked(struct amdiommu_domain *domain, iommu_gaddr_t base,
 	idx = -1;
 	pte = NULL;
 	sf = NULL;
+	*pde_set = false;
 
 	for (pi = 0; size > 0; base += IOMMU_PAGE_SIZE, size -= IOMMU_PAGE_SIZE,
 	    pi++) {
@@ -209,7 +219,7 @@ amdiommu_map_buf_locked(struct amdiommu_domain *domain, iommu_gaddr_t base,
 		    ("mapping loop overflow %p %jx %jx %jx", domain,
 		    (uintmax_t)base, (uintmax_t)size, (uintmax_t)IOMMU_PAGE_SIZE));
 		pte = amdiommu_pgtbl_map_pte(domain, base, domain->pglvl - 1,
-		    flags, &idx, &sf);
+		    flags, &idx, &sf, pde_set);
 		if (pte == NULL) {
 			KASSERT((flags & IOMMU_PGF_WAITOK) == 0,
 			    ("failed waitable pte alloc %p", domain));
@@ -236,6 +246,7 @@ amdiommu_map_buf(struct iommu_domain *iodom, struct iommu_map_entry *entry,
 	uint64_t pflags;
 	iommu_gaddr_t base, size;
 	int error;
+	bool flush, pde_set;
 
 	base = entry->start;
 	size = entry->end - entry->start;
@@ -274,15 +285,26 @@ amdiommu_map_buf(struct iommu_domain *iodom, struct iommu_map_entry *entry,
 
 	AMDIOMMU_DOMAIN_PGLOCK(domain);
 	error = amdiommu_map_buf_locked(domain, base, size, ma, pflags,
-	    flags, entry);
-	AMDIOMMU_DOMAIN_PGUNLOCK(domain);
-
+	    flags, entry, &pde_set);
 	/*
-	 * XXXKIB invalidation seems to be needed even for non-valid->valid
-	 * updates.  Recheck.
+	 * Filling a non-present PTE needs no invalidation unless the
+	 * IOMMU caches non-present entries (NpCache).  A new PDE does:
+	 * unmap clears a PDE before its invalidation is done, so the
+	 * IOMMU may still hold the old one.  So does a map that uses a
+	 * new PDE whose flush, by a concurrent map, is not done yet.
 	 */
-	iommu_qi_invalidate_sync(iodom, base, size,
-	    (flags & IOMMU_PGF_WAITOK) != 0);
+	flush = domain->pde_flushes != 0 ||
+	    (DOM2AMD(domain)->cap_hdr & PCIM_AMDIOMMU_CAP_NPCACHE) != 0;
+	AMDIOMMU_DOMAIN_PGUNLOCK(domain);
+
+	if (flush)
+		iommu_qi_invalidate_sync(iodom, base, size,
+		    (flags & IOMMU_PGF_WAITOK) != 0);
+	if (pde_set) {
+		AMDIOMMU_DOMAIN_PGLOCK(domain);
+		domain->pde_flushes--;
+		AMDIOMMU_DOMAIN_PGUNLOCK(domain);
+	}
 	return (error);
 }
 
@@ -295,7 +317,7 @@ amdiommu_free_pgtbl_pde(struct amdiommu_domain *domain, iommu_gaddr_t base,
 	vm_pindex_t idx;
 
 	sf = NULL;
-	pde = amdiommu_pgtbl_map_pte(domain, base, lvl, flags, &idx, &sf);
+	pde = amdiommu_pgtbl_map_pte(domain, base, lvl, flags, &idx, &sf, NULL);
 	amdiommu_unmap_clear_pte(domain, base, lvl, flags, pde, &sf, entry,
 	    true);
 }
@@ -362,7 +384,7 @@ amdiommu_unmap_buf_locked(struct amdiommu_domain *domain, iommu_gaddr_t base,
 
 	for (sf = NULL; size > 0; base += pg_sz, size -= pg_sz) {
 		pte = amdiommu_pgtbl_map_pte(domain, base,
-		    domain->pglvl - 1, flags, &idx, &sf);
+		    domain->pglvl - 1, flags, &idx, &sf, NULL);
 		KASSERT(pte != NULL,
 		    ("sleeping or page missed %p %jx %d 0x%x",
 		    domain, (uintmax_t)base, domain->pglvl - 1, flags));
diff --git a/sys/x86/iommu/amd_iommu.h b/sys/x86/iommu/amd_iommu.h
index f138efb4b23e..b2a7c30970fe 100644
--- a/sys/x86/iommu/amd_iommu.h
+++ b/sys/x86/iommu/amd_iommu.h
@@ -48,6 +48,7 @@ struct amdiommu_domain {
 	vm_object_t pgtbl_obj;		/* (c) Page table pages */
 	vm_page_t pgtblr;		/* (c) Page table root page */
 	u_int pglvl;			/* (c) Page table levels */
+	u_int pde_flushes;		/* Maps flushing a new PDE, pglock */
 };
 
 struct amdiommu_ctx {
@@ -65,6 +66,7 @@ struct amdiommu_unit {
 	u_int		unit_id;	/* Hypertransport Unit ID, deprecated */
 	TAILQ_ENTRY(amdiommu_unit) unit_next;
 	int		seccap_reg;
+	uint32_t	cap_hdr;	/* Capability header */
 	uint64_t	efr;
 	vm_paddr_t	mmio_base;
 	vm_size_t	mmio_sz;