xref: /openbmc/linux/arch/um/include/asm/cpufeature.h (revision c595db6d7c8bcf87ef42204391fa890e5950e566)
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 :	\
41d8fb32f4SAnton Ivanov 	 x86_this_cpu_test_bit(bit,					\
42d8fb32f4SAnton Ivanov 		(unsigned long __percpu *)&cpu_info.x86_capability))
43d8fb32f4SAnton Ivanov 
44d8fb32f4SAnton Ivanov /*
45d8fb32f4SAnton Ivanov  * This macro is for detection of features which need kernel
46d8fb32f4SAnton Ivanov  * infrastructure to be used.  It may *not* directly test the CPU
47d8fb32f4SAnton Ivanov  * itself.  Use the cpu_has() family if you want true runtime
48d8fb32f4SAnton Ivanov  * testing of CPU features, like in hypervisor code where you are
49d8fb32f4SAnton Ivanov  * supporting a possible guest feature where host support for it
50d8fb32f4SAnton Ivanov  * is not relevant.
51d8fb32f4SAnton Ivanov  */
52d8fb32f4SAnton Ivanov #define cpu_feature_enabled(bit)	\
53d8fb32f4SAnton Ivanov 	(__builtin_constant_p(bit) && DISABLED_MASK_BIT_SET(bit) ? 0 : static_cpu_has(bit))
54d8fb32f4SAnton Ivanov 
55d8fb32f4SAnton Ivanov #define boot_cpu_has(bit)	cpu_has(&boot_cpu_data, bit)
56d8fb32f4SAnton Ivanov 
57d8fb32f4SAnton Ivanov #define set_cpu_cap(c, bit)	set_bit(bit, (unsigned long *)((c)->x86_capability))
58d8fb32f4SAnton Ivanov 
59d8fb32f4SAnton Ivanov extern void setup_clear_cpu_cap(unsigned int bit);
60d8fb32f4SAnton Ivanov 
61d8fb32f4SAnton Ivanov #define setup_force_cpu_cap(bit) do { \
62d8fb32f4SAnton Ivanov 	set_cpu_cap(&boot_cpu_data, bit);	\
63d8fb32f4SAnton Ivanov 	set_bit(bit, (unsigned long *)cpu_caps_set);	\
64d8fb32f4SAnton Ivanov } while (0)
65d8fb32f4SAnton Ivanov 
66d8fb32f4SAnton Ivanov #define setup_force_cpu_bug(bit) setup_force_cpu_cap(bit)
67d8fb32f4SAnton Ivanov 
68d8fb32f4SAnton Ivanov /*
69d8fb32f4SAnton Ivanov  * Static testing of CPU features. Used the same as boot_cpu_has(). It
70d8fb32f4SAnton Ivanov  * statically patches the target code for additional performance. Use
71d8fb32f4SAnton Ivanov  * static_cpu_has() only in fast paths, where every cycle counts. Which
72d8fb32f4SAnton Ivanov  * means that the boot_cpu_has() variant is already fast enough for the
73d8fb32f4SAnton Ivanov  * majority of cases and you should stick to using it as it is generally
74d8fb32f4SAnton Ivanov  * only two instructions: a RIP-relative MOV and a TEST.
75d8fb32f4SAnton Ivanov  */
_static_cpu_has(u16 bit)76d8fb32f4SAnton Ivanov static __always_inline bool _static_cpu_has(u16 bit)
77d8fb32f4SAnton Ivanov {
78*aaff74d8SLinus Torvalds 	asm goto("1: jmp 6f\n"
79d8fb32f4SAnton Ivanov 		 "2:\n"
80d8fb32f4SAnton Ivanov 		 ".skip -(((5f-4f) - (2b-1b)) > 0) * "
81d8fb32f4SAnton Ivanov 			 "((5f-4f) - (2b-1b)),0x90\n"
82d8fb32f4SAnton Ivanov 		 "3:\n"
83d8fb32f4SAnton Ivanov 		 ".section .altinstructions,\"a\"\n"
84d8fb32f4SAnton Ivanov 		 " .long 1b - .\n"		/* src offset */
85d8fb32f4SAnton Ivanov 		 " .long 4f - .\n"		/* repl offset */
86d8fb32f4SAnton Ivanov 		 " .word %P[always]\n"		/* always replace */
87d8fb32f4SAnton Ivanov 		 " .byte 3b - 1b\n"		/* src len */
88d8fb32f4SAnton Ivanov 		 " .byte 5f - 4f\n"		/* repl len */
89d8fb32f4SAnton Ivanov 		 " .byte 3b - 2b\n"		/* pad len */
90d8fb32f4SAnton Ivanov 		 ".previous\n"
91d8fb32f4SAnton Ivanov 		 ".section .altinstr_replacement,\"ax\"\n"
92d8fb32f4SAnton Ivanov 		 "4: jmp %l[t_no]\n"
93d8fb32f4SAnton Ivanov 		 "5:\n"
94d8fb32f4SAnton Ivanov 		 ".previous\n"
95d8fb32f4SAnton Ivanov 		 ".section .altinstructions,\"a\"\n"
96d8fb32f4SAnton Ivanov 		 " .long 1b - .\n"		/* src offset */
97d8fb32f4SAnton Ivanov 		 " .long 0\n"			/* no replacement */
98d8fb32f4SAnton Ivanov 		 " .word %P[feature]\n"		/* feature bit */
99d8fb32f4SAnton Ivanov 		 " .byte 3b - 1b\n"		/* src len */
100d8fb32f4SAnton Ivanov 		 " .byte 0\n"			/* repl len */
101d8fb32f4SAnton Ivanov 		 " .byte 0\n"			/* pad len */
102d8fb32f4SAnton Ivanov 		 ".previous\n"
103d8fb32f4SAnton Ivanov 		 ".section .altinstr_aux,\"ax\"\n"
104d8fb32f4SAnton Ivanov 		 "6:\n"
105d8fb32f4SAnton Ivanov 		 " testb %[bitnum],%[cap_byte]\n"
106d8fb32f4SAnton Ivanov 		 " jnz %l[t_yes]\n"
107d8fb32f4SAnton Ivanov 		 " jmp %l[t_no]\n"
108d8fb32f4SAnton Ivanov 		 ".previous\n"
109d8fb32f4SAnton Ivanov 		 : : [feature]  "i" (bit),
110d8fb32f4SAnton Ivanov 		     [always]   "i" (X86_FEATURE_ALWAYS),
111d8fb32f4SAnton Ivanov 		     [bitnum]   "i" (1 << (bit & 7)),
112d8fb32f4SAnton Ivanov 		     [cap_byte] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3])
113d8fb32f4SAnton Ivanov 		 : : t_yes, t_no);
114d8fb32f4SAnton Ivanov t_yes:
115d8fb32f4SAnton Ivanov 	return true;
116d8fb32f4SAnton Ivanov t_no:
117d8fb32f4SAnton Ivanov 	return false;
118d8fb32f4SAnton Ivanov }
119d8fb32f4SAnton Ivanov 
120d8fb32f4SAnton Ivanov #define static_cpu_has(bit)					\
121d8fb32f4SAnton Ivanov (								\
122d8fb32f4SAnton Ivanov 	__builtin_constant_p(boot_cpu_has(bit)) ?		\
123d8fb32f4SAnton Ivanov 		boot_cpu_has(bit) :				\
124d8fb32f4SAnton Ivanov 		_static_cpu_has(bit)				\
125d8fb32f4SAnton Ivanov )
126d8fb32f4SAnton Ivanov 
127d8fb32f4SAnton Ivanov #define cpu_has_bug(c, bit)		cpu_has(c, (bit))
128d8fb32f4SAnton Ivanov #define set_cpu_bug(c, bit)		set_cpu_cap(c, (bit))
129d8fb32f4SAnton Ivanov 
130d8fb32f4SAnton Ivanov #define static_cpu_has_bug(bit)		static_cpu_has((bit))
131d8fb32f4SAnton Ivanov #define boot_cpu_has_bug(bit)		cpu_has_bug(&boot_cpu_data, (bit))
132d8fb32f4SAnton Ivanov #define boot_cpu_set_bug(bit)		set_cpu_cap(&boot_cpu_data, (bit))
133d8fb32f4SAnton Ivanov 
134d8fb32f4SAnton Ivanov #define MAX_CPU_FEATURES		(NCAPINTS * 32)
135d8fb32f4SAnton Ivanov #define cpu_have_feature		boot_cpu_has
136d8fb32f4SAnton Ivanov 
137d8fb32f4SAnton Ivanov #define CPU_FEATURE_TYPEFMT		"x86,ven%04Xfam%04Xmod%04X"
138d8fb32f4SAnton Ivanov #define CPU_FEATURE_TYPEVAL		boot_cpu_data.x86_vendor, boot_cpu_data.x86, \
139d8fb32f4SAnton Ivanov 					boot_cpu_data.x86_model
140d8fb32f4SAnton Ivanov 
141d8fb32f4SAnton Ivanov #endif /* defined(__KERNEL__) && !defined(__ASSEMBLY__) */
142d8fb32f4SAnton Ivanov #endif /* _ASM_UM_CPUFEATURE_H */
143