git: 9286d8fe2873 - stable/15 - x86/local_apic.c: add lapic_maxlvt() helper

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Sun, 25 Jan 2026 00:24:43 UTC
The branch stable/15 has been updated by kib:

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

commit 9286d8fe28732034f039fc26381ddb964d8a393f
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-01-19 12:50:09 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-01-25 00:23:34 +0000

    x86/local_apic.c: add lapic_maxlvt() helper
    
    (cherry picked from commit ad5e3cb950344f9822dbbd90f5ac7c256f97fa4c)
---
 sys/x86/x86/local_apic.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/sys/x86/x86/local_apic.c b/sys/x86/x86/local_apic.c
index 053d9814c14c..68f42c9eed0b 100644
--- a/sys/x86/x86/local_apic.c
+++ b/sys/x86/x86/local_apic.c
@@ -314,6 +314,16 @@ SYSCTL_INT(_hw_apic, OID_AUTO, ds_idle_timeout, CTLFLAG_RWTUN,
 
 static void lapic_calibrate_initcount(struct lapic *la);
 
+/*
+ * Calculate the max index of the present LVT entry from the value of
+ * the LAPIC version register.
+ */
+static int
+lapic_maxlvt(uint32_t version)
+{
+	return ((version & APIC_VER_MAXLVT) >> MAXLVTSHIFT);
+}
+
 /*
  * Use __nosanitizethread to exempt the LAPIC I/O accessors from KCSan
  * instrumentation.  Otherwise, if x2APIC is not available, use of the global
@@ -753,7 +763,7 @@ lapic_dump(const char* str)
 	int i;
 
 	version = lapic_read32(LAPIC_VERSION);
-	maxlvt = (version & APIC_VER_MAXLVT) >> MAXLVTSHIFT;
+	maxlvt = lapic_maxlvt(version);
 	printf("cpu%d %s:\n", PCPU_GET(cpuid), str);
 	printf("     ID: 0x%08x   VER: 0x%08x LDR: 0x%08x DFR: 0x%08x",
 	    lapic_read32(LAPIC_ID), version,
@@ -834,7 +844,7 @@ lapic_setup(int boot)
 	la = &lapics[lapic_id()];
 	KASSERT(la->la_present, ("missing APIC structure"));
 	version = lapic_read32(LAPIC_VERSION);
-	maxlvt = (version & APIC_VER_MAXLVT) >> MAXLVTSHIFT;
+	maxlvt = lapic_maxlvt(version);
 
 	/* Initialize the TPR to allow all interrupts. */
 	lapic_set_tpr(0);
@@ -990,7 +1000,7 @@ lapic_enable_pcint(void)
 #endif
 
 	/* Fail if the PMC LVT is not present. */
-	maxlvt = (lapic_read32(LAPIC_VERSION) & APIC_VER_MAXLVT) >> MAXLVTSHIFT;
+	maxlvt = lapic_maxlvt(lapic_read32(LAPIC_VERSION));
 	if (maxlvt < APIC_LVT_PMC)
 		return (0);
 	if (refcount_acquire(&pcint_refcnt) > 0)
@@ -1014,7 +1024,7 @@ lapic_disable_pcint(void)
 #endif
 
 	/* Fail if the PMC LVT is not present. */
-	maxlvt = (lapic_read32(LAPIC_VERSION) & APIC_VER_MAXLVT) >> MAXLVTSHIFT;
+	maxlvt = lapic_maxlvt(lapic_read32(LAPIC_VERSION));
 	if (maxlvt < APIC_LVT_PMC)
 		return;
 	if (!refcount_release(&pcint_refcnt))
@@ -1883,7 +1893,7 @@ DB_SHOW_COMMAND_FLAGS(lapic, db_show_lapic, DB_CMD_MEMSAFE)
 	v = lapic_read32(LAPIC_VERSION);
 	db_printf("version  = %d.%d\n", (v & APIC_VER_VERSION) >> 4,
 	    v & 0xf);
-	db_printf("max LVT  = %d\n", (v & APIC_VER_MAXLVT) >> MAXLVTSHIFT);
+	db_printf("max LVT  = %d\n", lapic_maxlvt(v));
 	v = lapic_read32(LAPIC_SVR);
 	db_printf("SVR      = %02x (%s)\n", v & APIC_SVR_VECTOR,
 	    v & APIC_SVR_ENABLE ? "enabled" : "disabled");