xref: /linux/arch/um/include/asm/cpufeature.h (revision b4db9f840283caca0d904436f187ef56a9126eaa)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_UM_CPUFEATURE_H
3 #define _ASM_UM_CPUFEATURE_H
4 
5 #include <asm/processor.h>
6 
7 #if defined(__KERNEL__) && !defined(__ASSEMBLY__)
8 
9 #include <asm/asm.h>
10 #include <linux/bitops.h>
11 
12 extern const char * const x86_cap_flags[NCAPINTS*32];
13 extern const char * const x86_power_flags[32];
14 #define X86_CAP_FMT "%s"
15 #define x86_cap_flag(flag) x86_cap_flags[flag]
16 
17 /*
18  * In order to save room, we index into this array by doing
19  * X86_BUG_<name> - NCAPINTS*32.
20  */
21 extern const char * const x86_bug_flags[NBUGINTS*32];
22 
23 #define test_cpu_cap(c, bit)						\
24 	 test_bit(bit, (unsigned long *)((c)->x86_capability))
25 
26 /*
27  * There are 32 bits/features in each mask word.  The high bits
28  * (selected with (bit>>5) give us the word number and the low 5
29  * bits give us the bit/feature number inside the word.
30  * (1UL<<((bit)&31) gives us a mask for the feature_bit so we can
31  * see if it is set in the mask word.
32  */
33 #define CHECK_BIT_IN_MASK_WORD(maskname, word, bit)	\
34 	(((bit)>>5)==(word) && (1UL<<((bit)&31) & maskname##word ))
35 
36 #define cpu_has(c, bit)							\
37 	 test_cpu_cap(c, bit)
38 
39 #define this_cpu_has(bit)						\
40 	(__builtin_constant_p(bit) && REQUIRED_MASK_BIT_SET(bit) ? 1 :	\
41 	 x86_this_cpu_test_bit(bit,					\
42 		(unsigned long __percpu *)&cpu_info.x86_capability))
43 
44 /*
45  * This macro is for detection of features which need kernel
46  * infrastructure to be used.  It may *not* directly test the CPU
47  * itself.  Use the cpu_has() family if you want true runtime
48  * testing of CPU features, like in hypervisor code where you are
49  * supporting a possible guest feature where host support for it
50  * is not relevant.
51  */
52 #define cpu_feature_enabled(bit)	\
53 	(__builtin_constant_p(bit) && DISABLED_MASK_BIT_SET(bit) ? 0 : static_cpu_has(bit))
54 
55 #define boot_cpu_has(bit)	cpu_has(&boot_cpu_data, bit)
56 
57 #define set_cpu_cap(c, bit)	set_bit(bit, (unsigned long *)((c)->x86_capability))
58 
59 extern void setup_clear_cpu_cap(unsigned int bit);
60 
61 #define setup_force_cpu_cap(bit) do { \
62 	set_cpu_cap(&boot_cpu_data, bit);	\
63 	set_bit(bit, (unsigned long *)cpu_caps_set);	\
64 } while (0)
65 
66 #define setup_force_cpu_bug(bit) setup_force_cpu_cap(bit)
67 
68 /*
69  * Static testing of CPU features. Used the same as boot_cpu_has(). It
70  * statically patches the target code for additional performance. Use
71  * static_cpu_has() only in fast paths, where every cycle counts. Which
72  * means that the boot_cpu_has() variant is already fast enough for the
73  * majority of cases and you should stick to using it as it is generally
74  * only two instructions: a RIP-relative MOV and a TEST.
75  */
76 static __always_inline bool _static_cpu_has(u16 bit)
77 {
78 	asm goto("1: jmp 6f\n"
79 		 "2:\n"
80 		 ".skip -(((5f-4f) - (2b-1b)) > 0) * "
81 			 "((5f-4f) - (2b-1b)),0x90\n"
82 		 "3:\n"
83 		 ".section .altinstructions,\"a\"\n"
84 		 " .long 1b - .\n"		/* src offset */
85 		 " .long 4f - .\n"		/* repl offset */
86 		 " .word %P[always]\n"		/* always replace */
87 		 " .byte 3b - 1b\n"		/* src len */
88 		 " .byte 5f - 4f\n"		/* repl len */
89 		 " .byte 3b - 2b\n"		/* pad len */
90 		 ".previous\n"
91 		 ".section .altinstr_replacement,\"ax\"\n"
92 		 "4: jmp %l[t_no]\n"
93 		 "5:\n"
94 		 ".previous\n"
95 		 ".section .altinstructions,\"a\"\n"
96 		 " .long 1b - .\n"		/* src offset */
97 		 " .long 0\n"			/* no replacement */
98 		 " .word %P[feature]\n"		/* feature bit */
99 		 " .byte 3b - 1b\n"		/* src len */
100 		 " .byte 0\n"			/* repl len */
101 		 " .byte 0\n"			/* pad len */
102 		 ".previous\n"
103 		 ".section .altinstr_aux,\"ax\"\n"
104 		 "6:\n"
105 		 " testb %[bitnum],%[cap_byte]\n"
106 		 " jnz %l[t_yes]\n"
107 		 " jmp %l[t_no]\n"
108 		 ".previous\n"
109 		 : : [feature]  "i" (bit),
110 		     [always]   "i" (X86_FEATURE_ALWAYS),
111 		     [bitnum]   "i" (1 << (bit & 7)),
112 		     [cap_byte] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3])
113 		 : : t_yes, t_no);
114 t_yes:
115 	return true;
116 t_no:
117 	return false;
118 }
119 
120 #define static_cpu_has(bit)					\
121 (								\
122 	__builtin_constant_p(boot_cpu_has(bit)) ?		\
123 		boot_cpu_has(bit) :				\
124 		_static_cpu_has(bit)				\
125 )
126 
127 #define cpu_has_bug(c, bit)		cpu_has(c, (bit))
128 #define set_cpu_bug(c, bit)		set_cpu_cap(c, (bit))
129 
130 #define static_cpu_has_bug(bit)		static_cpu_has((bit))
131 #define boot_cpu_has_bug(bit)		cpu_has_bug(&boot_cpu_data, (bit))
132 #define boot_cpu_set_bug(bit)		set_cpu_cap(&boot_cpu_data, (bit))
133 
134 #define MAX_CPU_FEATURES		(NCAPINTS * 32)
135 #define cpu_have_feature		boot_cpu_has
136 
137 #define CPU_FEATURE_TYPEFMT		"x86,ven%04Xfam%04Xmod%04X"
138 #define CPU_FEATURE_TYPEVAL		boot_cpu_data.x86_vendor, boot_cpu_data.x86, \
139 					boot_cpu_data.x86_model
140 
141 #endif /* defined(__KERNEL__) && !defined(__ASSEMBLY__) */
142 #endif /* _ASM_UM_CPUFEATURE_H */
143