git: a59c252903e8 - main - IOMMU: add GAS map entry flag IOMMU_MAP_ENTRY_FAKE

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Tue, 26 Dec 2023 01:28:54 UTC
The branch main has been updated by kib:

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

commit a59c252903e81f46c74903ce5b1cf0960927dbcc
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2023-12-22 22:39:27 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-12-26 01:28:22 +0000

    IOMMU: add GAS map entry flag IOMMU_MAP_ENTRY_FAKE
    
    to allow to shut down assert in iommu_gas_cmp_entries() when used
    against fake entry to search for specific place in the tree.
    
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
---
 sys/dev/iommu/iommu_gas.c | 6 +++++-
 sys/dev/iommu/iommu_gas.h | 1 +
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/sys/dev/iommu/iommu_gas.c b/sys/dev/iommu/iommu_gas.c
index e178acf60478..dc850747512a 100644
--- a/sys/dev/iommu/iommu_gas.c
+++ b/sys/dev/iommu/iommu_gas.c
@@ -123,7 +123,8 @@ iommu_gas_cmp_entries(struct iommu_map_entry *a, struct iommu_map_entry *b)
 	    a, (uintmax_t)a->start, (uintmax_t)a->end));
 	KASSERT(b->start <= b->end, ("inverted entry %p (%jx, %jx)",
 	    b, (uintmax_t)b->start, (uintmax_t)b->end));
-	KASSERT(a->end <= b->start || b->end <= a->start ||
+	KASSERT(((a->flags | b->flags) & IOMMU_MAP_ENTRY_FAKE) != 0 ||
+	    a->end <= b->start || b->end <= a->start ||
 	    a->end == a->start || b->end == b->start,
 	    ("overlapping entries %p (%jx, %jx) f %#x %p (%jx, %jx) f %#x"
 	    " domain %p %p",
@@ -536,11 +537,13 @@ iommu_gas_alloc_region(struct iommu_domain *domain, struct iommu_map_entry *entr
 	if (entry->end >= domain->end)
 		return (EINVAL);
 
+	entry->flags |= IOMMU_MAP_ENTRY_FAKE;
 	next = RB_NFIND(iommu_gas_entries_tree, &domain->rb_root, entry);
 	KASSERT(next != NULL, ("next must be non-null %p %jx", domain,
 	    (uintmax_t)entry->start));
 	prev = RB_PREV(iommu_gas_entries_tree, &domain->rb_root, next);
 	/* prev could be NULL */
+	entry->flags &= ~IOMMU_MAP_ENTRY_FAKE;
 
 	/*
 	 * Adapt to broken BIOSes which specify overlapping RMRR
@@ -658,6 +661,7 @@ iommu_gas_remove_clip_left(struct iommu_domain *domain, iommu_gaddr_t start,
 	bzero(&fentry, sizeof(fentry));
 	fentry.start = start + 1;
 	fentry.end = start + 1;
+	fentry.flags = IOMMU_MAP_ENTRY_FAKE;
 	entry = RB_NFIND(iommu_gas_entries_tree, &domain->rb_root, &fentry);
 
 	if (entry->start >= start ||
diff --git a/sys/dev/iommu/iommu_gas.h b/sys/dev/iommu/iommu_gas.h
index 8069c5a15a34..cdc4bd22d60c 100644
--- a/sys/dev/iommu/iommu_gas.h
+++ b/sys/dev/iommu/iommu_gas.h
@@ -50,6 +50,7 @@
 #define	IOMMU_MAP_ENTRY_UNMAPPED	0x0010	/* No backing pages */
 #define	IOMMU_MAP_ENTRY_REMOVING	0x0020	/* In process of removal by
 						   iommu_gas_remove() */
+#define	IOMMU_MAP_ENTRY_FAKE	0x0040	/* disable assert in cmp() */
 #define	IOMMU_MAP_ENTRY_READ	0x1000	/* Read permitted */
 #define	IOMMU_MAP_ENTRY_WRITE	0x2000	/* Write permitted */
 #define	IOMMU_MAP_ENTRY_SNOOP	0x4000	/* Snoop */