[tip: x86/cpu] x86/lib: Add CPUID(0x1) family and model calculation

tip-bot2 for Ahmed S. Darwish posted 1 patch 2 months ago
arch/x86/include/asm/cpu.h |  6 +++++-
arch/x86/lib/cpu.c         | 45 +++++++++++++++++++++++--------------
2 files changed, 35 insertions(+), 16 deletions(-)
[tip: x86/cpu] x86/lib: Add CPUID(0x1) family and model calculation
Posted by tip-bot2 for Ahmed S. Darwish 2 months ago
The following commit has been merged into the x86/cpu branch of tip:

Commit-ID:     21834b541d933d52a3359ba368645f375f8c9ad1
Gitweb:        https://git.kernel.org/tip/21834b541d933d52a3359ba368645f375f8c9ad1
Author:        Ahmed S. Darwish <darwi@linutronix.de>
AuthorDate:    Thu, 28 May 2026 17:37:32 +02:00
Committer:     Borislav Petkov (AMD) <bp@alien8.de>
CommitterDate: Fri, 31 Jul 2026 13:57:04 -07:00

x86/lib: Add CPUID(0x1) family and model calculation

The x86 library provides x86_family() and x86_model().  They take raw
CPUID register output and calculate the CPU family and model from it.

In follow-up work, the x86 subsystem will use APIs which access
previously-parsed CPUID leafs structure instead of doing direct CPUID queries.
These new APIs force using the auto generated leaf data types at
<asm/cpuid/leaf_types.h>.

Introduce x86 family and model calculation functions that take these
auto-generated data types.  Refactor the original code so that no logic is
duplicated.

  [ bp: Massage commit message, unbreak too long line. ]

Signed-off-by: Ahmed S. Darwish <darwi@linutronix.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Link: https://patch.msgid.link/20260528153923.403473-11-darwi@linutronix.de
---
 arch/x86/include/asm/cpu.h |  6 +++++-
 arch/x86/lib/cpu.c         | 45 +++++++++++++++++++++++--------------
 2 files changed, 35 insertions(+), 16 deletions(-)

diff --git a/arch/x86/include/asm/cpu.h b/arch/x86/include/asm/cpu.h
index 57a0786..0131307 100644
--- a/arch/x86/include/asm/cpu.h
+++ b/arch/x86/include/asm/cpu.h
@@ -7,7 +7,9 @@
 #include <linux/topology.h>
 #include <linux/nodemask.h>
 #include <linux/percpu.h>
+
 #include <asm/ibt.h>
+#include <asm/cpuid/leaf_types.h>
 
 #ifndef CONFIG_SMP
 #define cpu_physical_id(cpu)			boot_cpu_physical_apicid
@@ -24,6 +26,10 @@ int mwait_usable(const struct cpuinfo_x86 *);
 unsigned int x86_family(unsigned int sig);
 unsigned int x86_model(unsigned int sig);
 unsigned int x86_stepping(unsigned int sig);
+
+unsigned int cpuid_family(const struct leaf_0x1_0 *l);
+unsigned int cpuid_model(const struct leaf_0x1_0 *l);
+
 #ifdef CONFIG_X86_BUS_LOCK_DETECT
 extern void __init sld_setup(struct cpuinfo_x86 *c);
 extern bool handle_user_split_lock(struct pt_regs *regs, long error_code);
diff --git a/arch/x86/lib/cpu.c b/arch/x86/lib/cpu.c
index 7ad6891..d39fba7 100644
--- a/arch/x86/lib/cpu.c
+++ b/arch/x86/lib/cpu.c
@@ -1,33 +1,36 @@
 // SPDX-License-Identifier: GPL-2.0-only
 #include <linux/types.h>
 #include <linux/export.h>
+
 #include <asm/cpu.h>
+#include <asm/cpuid/leaf_types.h>
 
-unsigned int x86_family(unsigned int sig)
+static unsigned int __x86_family(unsigned int base_fam, unsigned int ext_fam)
 {
-	unsigned int x86;
+	if (base_fam == 0xf)
+		base_fam += ext_fam;
 
-	x86 = (sig >> 8) & 0xf;
+	return base_fam;
+}
 
-	if (x86 == 0xf)
-		x86 += (sig >> 20) & 0xff;
+static unsigned int __x86_model(unsigned int family, unsigned int base_model,
+				unsigned int ext_model)
+{
+	if (family >= 0x6)
+		base_model |= ext_model << 4;
 
-	return x86;
+	return base_model;
+}
+
+unsigned int x86_family(unsigned int sig)
+{
+	return __x86_family((sig >> 8) & 0xf, (sig >> 20) & 0xff);
 }
 EXPORT_SYMBOL_GPL(x86_family);
 
 unsigned int x86_model(unsigned int sig)
 {
-	unsigned int fam, model;
-
-	fam = x86_family(sig);
-
-	model = (sig >> 4) & 0xf;
-
-	if (fam >= 0x6)
-		model += ((sig >> 16) & 0xf) << 4;
-
-	return model;
+	return __x86_model(x86_family(sig), (sig >> 4) & 0xf, (sig >> 16) & 0xf);
 }
 EXPORT_SYMBOL_GPL(x86_model);
 
@@ -36,3 +39,13 @@ unsigned int x86_stepping(unsigned int sig)
 	return sig & 0xf;
 }
 EXPORT_SYMBOL_GPL(x86_stepping);
+
+unsigned int cpuid_family(const struct leaf_0x1_0 *l)
+{
+	return __x86_family(l->base_family_id, l->ext_family);
+}
+
+unsigned int cpuid_model(const struct leaf_0x1_0 *l)
+{
+	return __x86_model(cpuid_family(l), l->base_model, l->ext_model);
+}