git: c6e1457a1cdb - main - x86: Do not take a CPUID leaf 1Fh module for the package

From: R. Christian McDonald <rcm_at_FreeBSD.org>
Date: Sun, 22 Nov 2026 03:01:59 UTC
The branch main has been updated by rcm:

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

commit c6e1457a1cdbb0edd10bdce1e9e6c384a3b256e4
Author:     R. Christian McDonald <rcm@FreeBSD.org>
AuthorDate: 2026-11-22 03:00:59 +0000
Commit:     R. Christian McDonald <rcm@FreeBSD.org>
CommitDate: 2026-11-22 03:00:59 +0000

    x86: Do not take a CPUID leaf 1Fh module for the package
    
    Each level of CPUID leaf 0Bh or 1Fh reports the shift that addresses
    the next level up.  Leaf 0Bh has only the SMT and core levels, so the
    core level's shift identifies the package.  Leaf 1Fh may enumerate
    module, tile, die and die group levels above the core level, and then
    the core level's shift identifies one of those instead.
    
    topo_probe_intel_0xb() used the core level's shift for the package in
    both cases.  On a processor that enumerates a module level, every
    module became a package, and the L3 cache was cut down to the module
    with "WARNING: L3 data cache covers more APIC IDs than a package", so
    the scheduler did not see that the modules share it.  An Atom x7809C
    (Amston Lake), which reports shifts of 1, 3 and 7 for its SMT, core and
    module levels, came up as two packages of four cores.  The N150 (Twin
    Lake) in the PR is affected in the same way.
    
    Use the shift of the last level enumerated for the package.  The SDM
    describes the package as the unenumerated domain above the last valid
    sub-leaf, and no level type marks it.  This also covers a leaf that
    reports only an SMT level, which used to make every thread a package.
    A processor that enumerates a die level is now counted as one package
    per socket and not one per die; its dies remain visible through their
    caches.
    
    Cross-check what the leaf reports.  Ignore a level whose shift is
    smaller than that of the level below it.  Warn about a level type that
    is out of the order the SDM fixes for the types defined so far, or that
    leaf 0Bh reserves, but still use its shift, which bounds the package in
    any case.  Accept a type that leaf 1Fh does not define yet wherever it
    appears: the SDM does not order the type values and allows for a new
    level between two existing ones.
    
    While here, name the remaining leaf 1Fh level types, and test only
    EBX[15:0], the field that is defined, when choosing between leaves 1Fh,
    0Bh and 04h.
    
    PR:                     297032
    Reviewed by:            kib
    Fixes:                  6badb512a94d ("Prefer CPUID leaf 1Fh for Intel CPU...")
    MFC after:              1 month
    Sponsored by:           Rubicon Communications, LLC ("Netgate")
    Differential Revision:  https://reviews.freebsd.org/D61521
---
 sys/x86/include/specialreg.h |  4 ++++
 sys/x86/x86/mp_x86.c         | 55 ++++++++++++++++++++++++++++++++++++++------
 2 files changed, 52 insertions(+), 7 deletions(-)

diff --git a/sys/x86/include/specialreg.h b/sys/x86/include/specialreg.h
index 888d0bfe8a41..aca1d0ddf7b5 100644
--- a/sys/x86/include/specialreg.h
+++ b/sys/x86/include/specialreg.h
@@ -402,6 +402,10 @@
 #define	CPUID_TYPE_INVAL	0
 #define	CPUID_TYPE_SMT		1
 #define	CPUID_TYPE_CORE		2
+#define	CPUID_TYPE_MODULE	3
+#define	CPUID_TYPE_TILE		4
+#define	CPUID_TYPE_DIE		5
+#define	CPUID_TYPE_DIEGRP	6
 
 /*
  * CPUID instruction 0xd Processor Extended State Enumeration Sub-leaf 1
diff --git a/sys/x86/x86/mp_x86.c b/sys/x86/x86/mp_x86.c
index 0adcb7077256..f9373c70c39e 100644
--- a/sys/x86/x86/mp_x86.c
+++ b/sys/x86/x86/mp_x86.c
@@ -404,6 +404,8 @@ topo_probe_intel_0xb(void)
 	u_int leaf;
 	u_int p[4] = { 0 };
 	int bits;
+	int max_type;
+	int prev_type;
 	int type;
 	int i;
 
@@ -413,17 +415,27 @@ topo_probe_intel_0xb(void)
 		cpuid_count(leaf, 0, p);
 	}
 	/* Fall back to leaf 0Bh (Extended Topology Enumeration). */
-	if (p[1] == 0) {
+	if ((p[1] & 0xffff) == 0) {
 		leaf = 0x0b;
 		cpuid_count(leaf, 0, p);
 	}
 	/* Fall back to leaf 04h (Deterministic Cache Parameters). */
-	if (p[1] == 0) {
+	if ((p[1] & 0xffff) == 0) {
 		topo_probe_intel_0x4();
 		return;
 	}
 
-	/* We only support three levels for now. */
+	/*
+	 * The sub-leaves are ordered from the smallest domain to the largest,
+	 * and each reports the shift that addresses the next domain up.  The
+	 * package is not enumerated: it is the domain above the last valid
+	 * sub-leaf, so the last shift is the package's.  Leaf 1Fh may
+	 * enumerate module, tile, die and die group levels above the core
+	 * level; they are not represented in the topology, but they must not
+	 * be mistaken for the package boundary.
+	 */
+	max_type = leaf == 0x1f ? CPUID_TYPE_DIEGRP : CPUID_TYPE_CORE;
+	prev_type = CPUID_TYPE_INVAL;
 	for (i = 0; ; i++) {
 		cpuid_count(leaf, i, p);
 
@@ -433,12 +445,41 @@ topo_probe_intel_0xb(void)
 		if (type == 0)
 			break;
 
+		/*
+		 * No level type marks the package, so cross-check what is
+		 * reported instead.  A level cannot cover fewer APIC IDs than
+		 * the level below it; ignore one that claims to.
+		 */
+		if (bits < pkg_id_shift) {
+			printf("WARNING: ignoring CPUID leaf %#x sub-leaf %d: "
+			    "type %d shift %d is below shift %d\n", leaf, i,
+			    type, bits, pkg_id_shift);
+			continue;
+		}
+
+		/*
+		 * The order of the level types defined so far is fixed, but
+		 * their values are not ordered in general: leaf 1Fh may come
+		 * to report a new type, with a larger value, between two of
+		 * them.  Leaf 0Bh defines only the SMT and core levels.
+		 * Whatever its type, a level's shift still bounds the package.
+		 */
+		if (type <= max_type) {
+			if (type <= prev_type)
+				printf("WARNING: CPUID leaf %#x sub-leaf %d "
+				    "has level type %d out of order\n", leaf,
+				    i, type);
+			prev_type = type;
+		} else if (leaf == 0x0b)
+			printf("WARNING: CPUID leaf 0xb sub-leaf %d has "
+			    "reserved level type %d\n", i, type);
+
+		pkg_id_shift = bits;
 		if (type == CPUID_TYPE_SMT)
 			core_id_shift = bits;
-		else if (type == CPUID_TYPE_CORE)
-			pkg_id_shift = bits;
-		else if (bootverbose)
-			printf("Topology level type %d shift: %d\n", type, bits);
+		else if (type != CPUID_TYPE_CORE && bootverbose)
+			printf("Topology level type %d shift: %d\n", type,
+			    bits);
 	}
 
 	if (pkg_id_shift < core_id_shift) {