1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_CPUFEATURE_H
3 #define _ASM_X86_CPUFEATURE_H
5 #include <asm/processor.h>
7 #if defined(__KERNEL__) && !defined(__ASSEMBLER__)
10 #include <linux/bitops.h>
11 #include <asm/alternative.h>
12 #include <asm/cpufeaturemasks.h>
41 extern const char * const x86_cap_flags[NCAPINTS*32];
42 extern const char * const x86_power_flags[32];
45 * In order to save room, we index into this array by doing
46 * X86_BUG_<name> - NCAPINTS*32.
48 extern const char * const x86_bug_flags[NBUGINTS*32];
49 #define x86_bug_flag(flag) x86_bug_flags[flag]
51 #define test_cpu_cap(c, bit) \
52 arch_test_bit(bit, (unsigned long *)((c)->x86_capability))
54 #define cpu_has(c, bit) \
55 (__builtin_constant_p(bit) && REQUIRED_MASK_BIT_SET(bit) ? 1 : \
58 #define this_cpu_has(bit) \
59 (__builtin_constant_p(bit) && REQUIRED_MASK_BIT_SET(bit) ? 1 : \
60 x86_this_cpu_test_bit(bit, cpu_info.x86_capability))
63 * This is the default CPU features testing macro to use in code.
65 * It is for detection of features which need kernel infrastructure to be
66 * used. It may *not* directly test the CPU itself. Use the cpu_has() family
67 * if you want true runtime testing of CPU features, like in hypervisor code
68 * where you are supporting a possible guest feature where host support for it
71 #define cpu_feature_enabled(bit) \
72 (__builtin_constant_p(bit) && DISABLED_MASK_BIT_SET(bit) ? 0 : static_cpu_has(bit))
74 #define boot_cpu_has(bit) cpu_has(&boot_cpu_data, bit)
76 #define set_cpu_cap(c, bit) set_bit(bit, (unsigned long *)((c)->x86_capability))
78 extern void setup_clear_cpu_cap(unsigned int bit);
79 extern void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int bit);
80 void check_cpufeature_deps(struct cpuinfo_x86 *c);
82 #define setup_force_cpu_cap(bit) do { \
84 if (!boot_cpu_has(bit)) \
85 WARN_ON(alternatives_patched); \
87 set_cpu_cap(&boot_cpu_data, bit); \
88 set_bit(bit, (unsigned long *)cpu_caps_set); \
91 #define setup_force_cpu_bug(bit) setup_force_cpu_cap(bit)
94 * Do not use an "m" constraint for [cap_byte] here: gcc doesn't know
95 * that this is only used on a fallback path and will sometimes cause
96 * it to manifest the address of boot_cpu_data in a register, fouling
97 * the mainline (post-initialization) code.
99 static __always_inline bool _static_cpu_has(u16 bit)
101 asm goto(ALTERNATIVE_TERNARY("jmp 6f", %c[feature], "", "jmp %l[t_no]")
102 ".pushsection .altinstr_aux,\"ax\"\n"
104 " testb %[bitnum], %a[cap_byte]\n"
108 : : [feature] "i" (bit),
109 [bitnum] "i" (1 << (bit & 7)),
110 [cap_byte] "i" (&((const char *)boot_cpu_data.x86_capability)[bit >> 3])
118 #define static_cpu_has(bit) \
120 __builtin_constant_p(boot_cpu_has(bit)) ? \
121 boot_cpu_has(bit) : \
122 _static_cpu_has(bit) \
125 #define cpu_has_bug(c, bit) cpu_has(c, (bit))
126 #define set_cpu_bug(c, bit) set_cpu_cap(c, (bit))
127 #define clear_cpu_bug(c, bit) clear_cpu_cap(c, (bit))
129 #define static_cpu_has_bug(bit) static_cpu_has((bit))
130 #define boot_cpu_has_bug(bit) cpu_has_bug(&boot_cpu_data, (bit))
131 #define boot_cpu_set_bug(bit) set_cpu_cap(&boot_cpu_data, (bit))
133 #define MAX_CPU_FEATURES (NCAPINTS * 32)
134 #define cpu_have_feature boot_cpu_has
136 #define CPU_FEATURE_TYPEFMT "x86,ven%04Xfam%04Xmod%04X"
137 #define CPU_FEATURE_TYPEVAL boot_cpu_data.x86_vendor, boot_cpu_data.x86, \
138 boot_cpu_data.x86_model
140 #endif /* defined(__KERNEL__) && !defined(__ASSEMBLER__) */
141 #endif /* _ASM_X86_CPUFEATURE_H */