Skip to content

Commit 43d86e3

Browse files
committed
x86/cpu: Provide cpuid_read() et al.
Provide a few helper functions to read CPUID leafs or individual registers into a data structure without requiring unions. Signed-off-by: Thomas Gleixner <[email protected]> Tested-by: Juergen Gross <[email protected]> Tested-by: Sohil Mehta <[email protected]> Tested-by: Michael Kelley <[email protected]> Tested-by: Peter Zijlstra (Intel) <[email protected]> Tested-by: Zhang Rui <[email protected]> Tested-by: Wang Wendy <[email protected]> Tested-by: K Prateek Nayak <[email protected]> Acked-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lore.kernel.org/r/878r3mg570.ffs@tglx
1 parent 841c351 commit 43d86e3

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

arch/x86/include/asm/cpuid.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,42 @@ static inline unsigned int cpuid_edx(unsigned int op)
127127
return edx;
128128
}
129129

130+
static inline void __cpuid_read(unsigned int leaf, unsigned int subleaf, u32 *regs)
131+
{
132+
regs[CPUID_EAX] = leaf;
133+
regs[CPUID_ECX] = subleaf;
134+
__cpuid(regs + CPUID_EAX, regs + CPUID_EBX, regs + CPUID_ECX, regs + CPUID_EDX);
135+
}
136+
137+
#define cpuid_subleaf(leaf, subleaf, regs) { \
138+
static_assert(sizeof(*(regs)) == 16); \
139+
__cpuid_read(leaf, subleaf, (u32 *)(regs)); \
140+
}
141+
142+
#define cpuid_leaf(leaf, regs) { \
143+
static_assert(sizeof(*(regs)) == 16); \
144+
__cpuid_read(leaf, 0, (u32 *)(regs)); \
145+
}
146+
147+
static inline void __cpuid_read_reg(unsigned int leaf, unsigned int subleaf,
148+
enum cpuid_regs_idx regidx, u32 *reg)
149+
{
150+
u32 regs[4];
151+
152+
__cpuid_read(leaf, subleaf, regs);
153+
*reg = regs[regidx];
154+
}
155+
156+
#define cpuid_subleaf_reg(leaf, subleaf, regidx, reg) { \
157+
static_assert(sizeof(*(reg)) == 4); \
158+
__cpuid_read_reg(leaf, subleaf, regidx, (u32 *)(reg)); \
159+
}
160+
161+
#define cpuid_leaf_reg(leaf, regidx, reg) { \
162+
static_assert(sizeof(*(reg)) == 4); \
163+
__cpuid_read_reg(leaf, 0, regidx, (u32 *)(reg)); \
164+
}
165+
130166
static __always_inline bool cpuid_function_is_indexed(u32 function)
131167
{
132168
switch (function) {

0 commit comments

Comments
 (0)