x86/cpu: Provide cpuid_read() et al.
authorThomas Gleixner <tglx@linutronix.de>
Wed, 14 Feb 2024 20:29:39 +0000 (21:29 +0100)
committerThomas Gleixner <tglx@linutronix.de>
Thu, 15 Feb 2024 21:07:36 +0000 (22:07 +0100)
Provide a few helper functions to read CPUID leafs or individual registers
into a data structure without requiring unions.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Tested-by: Juergen Gross <jgross@suse.com>
Tested-by: Sohil Mehta <sohil.mehta@intel.com>
Tested-by: Michael Kelley <mhklinux@outlook.com>
Tested-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Tested-by: Zhang Rui <rui.zhang@intel.com>
Tested-by: Wang Wendy <wendy.wang@intel.com>
Tested-by: K Prateek Nayak <kprateek.nayak@amd.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/878r3mg570.ffs@tglx
arch/x86/include/asm/cpuid.h

index 9bee3e7bf97363e1d9b76438e94b902c8e1baf92..6b122a31da065a22ed660b04f41160d66c705c18 100644 (file)
@@ -127,6 +127,42 @@ static inline unsigned int cpuid_edx(unsigned int op)
        return edx;
 }
 
+static inline void __cpuid_read(unsigned int leaf, unsigned int subleaf, u32 *regs)
+{
+       regs[CPUID_EAX] = leaf;
+       regs[CPUID_ECX] = subleaf;
+       __cpuid(regs + CPUID_EAX, regs + CPUID_EBX, regs + CPUID_ECX, regs + CPUID_EDX);
+}
+
+#define cpuid_subleaf(leaf, subleaf, regs) {           \
+       static_assert(sizeof(*(regs)) == 16);           \
+       __cpuid_read(leaf, subleaf, (u32 *)(regs));     \
+}
+
+#define cpuid_leaf(leaf, regs) {                       \
+       static_assert(sizeof(*(regs)) == 16);           \
+       __cpuid_read(leaf, 0, (u32 *)(regs));           \
+}
+
+static inline void __cpuid_read_reg(unsigned int leaf, unsigned int subleaf,
+                                   enum cpuid_regs_idx regidx, u32 *reg)
+{
+       u32 regs[4];
+
+       __cpuid_read(leaf, subleaf, regs);
+       *reg = regs[regidx];
+}
+
+#define cpuid_subleaf_reg(leaf, subleaf, regidx, reg) {                \
+       static_assert(sizeof(*(reg)) == 4);                     \
+       __cpuid_read_reg(leaf, subleaf, regidx, (u32 *)(reg));  \
+}
+
+#define cpuid_leaf_reg(leaf, regidx, reg) {                    \
+       static_assert(sizeof(*(reg)) == 4);                     \
+       __cpuid_read_reg(leaf, 0, regidx, (u32 *)(reg));        \
+}
+
 static __always_inline bool cpuid_function_is_indexed(u32 function)
 {
        switch (function) {