git: 83217f087be7 - stable/14 - amdiommu: changes for stable/14 merge

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Sat, 09 Nov 2024 20:19:16 UTC
The branch stable/14 has been updated by kib:

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

commit 83217f087be7de6e250039ca757e5d37facff72a
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2024-11-09 19:29:26 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2024-11-09 19:31:25 +0000

    amdiommu: changes for stable/14 merge
    
    to accomodate lack of ilog2() on stable/14.  Done as separate commit to
    ease future cherry-picks.
    
    Sponsored by:   Advanced Micro Devices (AMD)
    Sponsored by:   The FreeBSD Foundation
---
 sys/x86/iommu/amd_cmd.c     |  4 ++--
 sys/x86/iommu/amd_ctx.c     |  2 +-
 sys/x86/iommu/amd_drv.c     |  2 +-
 sys/x86/iommu/amd_event.c   |  2 +-
 sys/x86/iommu/amd_intrmap.c |  2 +-
 sys/x86/iommu/x86_iommu.h   | 10 ++++++++++
 6 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/sys/x86/iommu/amd_cmd.c b/sys/x86/iommu/amd_cmd.c
index bbc2a8e0ad9f..384d92066e36 100644
--- a/sys/x86/iommu/amd_cmd.c
+++ b/sys/x86/iommu/amd_cmd.c
@@ -320,7 +320,7 @@ amdiommu_init_cmd(struct amdiommu_unit *unit)
 {
 	uint64_t qi_sz, rv;
 
-	unit->x86c.qi_buf_maxsz = ilog2(AMDIOMMU_CMDBUF_MAX / PAGE_SIZE);
+	unit->x86c.qi_buf_maxsz = ilog2_local(AMDIOMMU_CMDBUF_MAX / PAGE_SIZE);
 	unit->x86c.qi_cmd_sz = AMDIOMMU_CMD_SZ;
 	iommu_qi_common_init(AMD2IOMMU(unit), amdiommu_qi_task);
 	get_x86_iommu()->qi_ensure = amdiommu_cmd_ensure;
@@ -334,7 +334,7 @@ amdiommu_init_cmd(struct amdiommu_unit *unit)
 	 * See the description of the ComLen encoding for Command
 	 * buffer Base Address Register.
 	 */
-	qi_sz = ilog2(unit->x86c.inv_queue_size / PAGE_SIZE) + 8;
+	qi_sz = ilog2_local(unit->x86c.inv_queue_size / PAGE_SIZE) + 8;
 	rv |= qi_sz << AMDIOMMU_CMDBUF_BASE_SZSHIFT;
 
 	AMDIOMMU_LOCK(unit);
diff --git a/sys/x86/iommu/amd_ctx.c b/sys/x86/iommu/amd_ctx.c
index b3e85350a995..81e284373fc8 100644
--- a/sys/x86/iommu/amd_ctx.c
+++ b/sys/x86/iommu/amd_ctx.c
@@ -462,7 +462,7 @@ dte_entry_init_one(struct amdiommu_dte *dtep, struct amdiommu_ctx *ctx,
 	if (unit->irte_enabled) {
 		dtep->iv = 1;
 		dtep->i = 0;
-		dtep->inttablen = ilog2(unit->irte_nentries);
+		dtep->inttablen = ilog2_local(unit->irte_nentries);
 		dtep->intrroot = pmap_kextract(unit->irte_x2apic ?
 		    (vm_offset_t)ctx->irtx2 :
 		    (vm_offset_t)ctx->irtb) >> 6;
diff --git a/sys/x86/iommu/amd_drv.c b/sys/x86/iommu/amd_drv.c
index 395cefc65caa..5db7a5225538 100644
--- a/sys/x86/iommu/amd_drv.c
+++ b/sys/x86/iommu/amd_drv.c
@@ -236,7 +236,7 @@ amdiommu_create_dev_tbl(struct amdiommu_unit *sc)
 		sc->devtbl_obj->domain.dr_policy = DOMAINSET_PREF(dom);
 
 	sc->hw_ctrl &= ~AMDIOMMU_CTRL_DEVTABSEG_MASK;
-	sc->hw_ctrl |= (uint64_t)segnum_log << ilog2(AMDIOMMU_CTRL_DEVTABSEG_2);
+	sc->hw_ctrl |= (uint64_t)segnum_log << 34; /* ilog2(AMDIOMMU_CTRL_DEVTABSEG_2) */
 	sc->hw_ctrl |= AMDIOMMU_CTRL_COHERENT;
 	amdiommu_write8(sc, AMDIOMMU_CTRL, sc->hw_ctrl);
 
diff --git a/sys/x86/iommu/amd_event.c b/sys/x86/iommu/amd_event.c
index 4a52e42260c2..d4be79474c6a 100644
--- a/sys/x86/iommu/amd_event.c
+++ b/sys/x86/iommu/amd_event.c
@@ -285,7 +285,7 @@ amdiommu_init_event(struct amdiommu_unit *unit)
 	    "amdiommu%d event taskq", unit->iommu.unit);
 
 	base_reg = pmap_kextract((vm_offset_t)unit->event_log) |
-	    (((uint64_t)0x8 + ilog2(unit->event_log_size /
+	    (((uint64_t)0x8 + ilog2_local(unit->event_log_size /
 	    AMDIOMMU_EVNTLOG_MIN)) << AMDIOMMU_EVNTLOG_BASE_SZSHIFT);
 	AMDIOMMU_LOCK(unit);
 	/*
diff --git a/sys/x86/iommu/amd_intrmap.c b/sys/x86/iommu/amd_intrmap.c
index a4c1a7836268..c5c1706f1f3e 100644
--- a/sys/x86/iommu/amd_intrmap.c
+++ b/sys/x86/iommu/amd_intrmap.c
@@ -374,7 +374,7 @@ amdiommu_init_irt(struct amdiommu_unit *unit)
 
 	nentries = 32;
 	TUNABLE_INT_FETCH("hw.iommu.amd.ir_num", &nentries);
-	nentries = roundup_pow_of_two(nentries);
+	nentries = roundup_pow_of_two_local(nentries);
 	if (nentries < 1)
 		nentries = 1;
 	if (nentries > 2048)
diff --git a/sys/x86/iommu/x86_iommu.h b/sys/x86/iommu/x86_iommu.h
index eb4a9907a5d6..835a4623a95e 100644
--- a/sys/x86/iommu/x86_iommu.h
+++ b/sys/x86/iommu/x86_iommu.h
@@ -200,4 +200,14 @@ void iommu_db_print_ctx(struct iommu_ctx *ctx);
 void iommu_db_domain_print_contexts(struct iommu_domain *iodom);
 void iommu_db_domain_print_mappings(struct iommu_domain *iodom);
 
+static __inline __pure2 int
+ilog2_local(int n)
+{
+	KASSERT(n != 0, ("ilog argument must be nonzero"));
+	return (8 * sizeof(n) - 1 - __builtin_clz((u_int)n));
+}
+
+#define order_base_2_local(n) ilog2_local(2*(n)-1)
+#define	roundup_pow_of_two_local(n) ((__typeof(n))1 << order_base_2_local(n))
+
 #endif