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