xref: /openbsd/gnu/llvm/clang/lib/Headers/cpuid.h (revision 12c85518)
1e5dd7070Spatrick /*===---- cpuid.h - X86 cpu model detection --------------------------------===
2e5dd7070Spatrick  *
3e5dd7070Spatrick  * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick  * See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick  * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick  *
7e5dd7070Spatrick  *===-----------------------------------------------------------------------===
8e5dd7070Spatrick  */
9e5dd7070Spatrick 
10a9ac8606Spatrick #ifndef __CPUID_H
11a9ac8606Spatrick #define __CPUID_H
12a9ac8606Spatrick 
13e5dd7070Spatrick #if !(__x86_64__ || __i386__)
14e5dd7070Spatrick #error this header is for x86 only
15e5dd7070Spatrick #endif
16e5dd7070Spatrick 
17e5dd7070Spatrick /* Responses identification request with %eax 0 */
18e5dd7070Spatrick /* AMD:     "AuthenticAMD" */
19e5dd7070Spatrick #define signature_AMD_ebx 0x68747541
20e5dd7070Spatrick #define signature_AMD_edx 0x69746e65
21e5dd7070Spatrick #define signature_AMD_ecx 0x444d4163
22e5dd7070Spatrick /* CENTAUR: "CentaurHauls" */
23e5dd7070Spatrick #define signature_CENTAUR_ebx 0x746e6543
24e5dd7070Spatrick #define signature_CENTAUR_edx 0x48727561
25e5dd7070Spatrick #define signature_CENTAUR_ecx 0x736c7561
26e5dd7070Spatrick /* CYRIX:   "CyrixInstead" */
27e5dd7070Spatrick #define signature_CYRIX_ebx 0x69727943
28e5dd7070Spatrick #define signature_CYRIX_edx 0x736e4978
29e5dd7070Spatrick #define signature_CYRIX_ecx 0x64616574
30ec727ea7Spatrick /* HYGON:   "HygonGenuine" */
31ec727ea7Spatrick #define signature_HYGON_ebx 0x6f677948
32ec727ea7Spatrick #define signature_HYGON_edx 0x6e65476e
33ec727ea7Spatrick #define signature_HYGON_ecx 0x656e6975
34e5dd7070Spatrick /* INTEL:   "GenuineIntel" */
35e5dd7070Spatrick #define signature_INTEL_ebx 0x756e6547
36e5dd7070Spatrick #define signature_INTEL_edx 0x49656e69
37e5dd7070Spatrick #define signature_INTEL_ecx 0x6c65746e
38e5dd7070Spatrick /* TM1:     "TransmetaCPU" */
39e5dd7070Spatrick #define signature_TM1_ebx 0x6e617254
40e5dd7070Spatrick #define signature_TM1_edx 0x74656d73
41e5dd7070Spatrick #define signature_TM1_ecx 0x55504361
42e5dd7070Spatrick /* TM2:     "GenuineTMx86" */
43e5dd7070Spatrick #define signature_TM2_ebx 0x756e6547
44e5dd7070Spatrick #define signature_TM2_edx 0x54656e69
45e5dd7070Spatrick #define signature_TM2_ecx 0x3638784d
46e5dd7070Spatrick /* NSC:     "Geode by NSC" */
47e5dd7070Spatrick #define signature_NSC_ebx 0x646f6547
48e5dd7070Spatrick #define signature_NSC_edx 0x79622065
49e5dd7070Spatrick #define signature_NSC_ecx 0x43534e20
50e5dd7070Spatrick /* NEXGEN:  "NexGenDriven" */
51e5dd7070Spatrick #define signature_NEXGEN_ebx 0x4778654e
52e5dd7070Spatrick #define signature_NEXGEN_edx 0x72446e65
53e5dd7070Spatrick #define signature_NEXGEN_ecx 0x6e657669
54e5dd7070Spatrick /* RISE:    "RiseRiseRise" */
55e5dd7070Spatrick #define signature_RISE_ebx 0x65736952
56e5dd7070Spatrick #define signature_RISE_edx 0x65736952
57e5dd7070Spatrick #define signature_RISE_ecx 0x65736952
58e5dd7070Spatrick /* SIS:     "SiS SiS SiS " */
59e5dd7070Spatrick #define signature_SIS_ebx 0x20536953
60e5dd7070Spatrick #define signature_SIS_edx 0x20536953
61e5dd7070Spatrick #define signature_SIS_ecx 0x20536953
62e5dd7070Spatrick /* UMC:     "UMC UMC UMC " */
63e5dd7070Spatrick #define signature_UMC_ebx 0x20434d55
64e5dd7070Spatrick #define signature_UMC_edx 0x20434d55
65e5dd7070Spatrick #define signature_UMC_ecx 0x20434d55
66e5dd7070Spatrick /* VIA:     "VIA VIA VIA " */
67e5dd7070Spatrick #define signature_VIA_ebx 0x20414956
68e5dd7070Spatrick #define signature_VIA_edx 0x20414956
69e5dd7070Spatrick #define signature_VIA_ecx 0x20414956
70e5dd7070Spatrick /* VORTEX:  "Vortex86 SoC" */
71e5dd7070Spatrick #define signature_VORTEX_ebx 0x74726f56
72e5dd7070Spatrick #define signature_VORTEX_edx 0x36387865
73e5dd7070Spatrick #define signature_VORTEX_ecx 0x436f5320
74e5dd7070Spatrick 
75e5dd7070Spatrick /* Features in %ecx for leaf 1 */
76e5dd7070Spatrick #define bit_SSE3        0x00000001
77e5dd7070Spatrick #define bit_PCLMULQDQ   0x00000002
78e5dd7070Spatrick #define bit_PCLMUL      bit_PCLMULQDQ   /* for gcc compat */
79e5dd7070Spatrick #define bit_DTES64      0x00000004
80e5dd7070Spatrick #define bit_MONITOR     0x00000008
81e5dd7070Spatrick #define bit_DSCPL       0x00000010
82e5dd7070Spatrick #define bit_VMX         0x00000020
83e5dd7070Spatrick #define bit_SMX         0x00000040
84e5dd7070Spatrick #define bit_EIST        0x00000080
85e5dd7070Spatrick #define bit_TM2         0x00000100
86e5dd7070Spatrick #define bit_SSSE3       0x00000200
87e5dd7070Spatrick #define bit_CNXTID      0x00000400
88e5dd7070Spatrick #define bit_FMA         0x00001000
89e5dd7070Spatrick #define bit_CMPXCHG16B  0x00002000
90e5dd7070Spatrick #define bit_xTPR        0x00004000
91e5dd7070Spatrick #define bit_PDCM        0x00008000
92e5dd7070Spatrick #define bit_PCID        0x00020000
93e5dd7070Spatrick #define bit_DCA         0x00040000
94e5dd7070Spatrick #define bit_SSE41       0x00080000
95e5dd7070Spatrick #define bit_SSE4_1      bit_SSE41       /* for gcc compat */
96e5dd7070Spatrick #define bit_SSE42       0x00100000
97e5dd7070Spatrick #define bit_SSE4_2      bit_SSE42       /* for gcc compat */
98e5dd7070Spatrick #define bit_x2APIC      0x00200000
99e5dd7070Spatrick #define bit_MOVBE       0x00400000
100e5dd7070Spatrick #define bit_POPCNT      0x00800000
101e5dd7070Spatrick #define bit_TSCDeadline 0x01000000
102e5dd7070Spatrick #define bit_AESNI       0x02000000
103e5dd7070Spatrick #define bit_AES         bit_AESNI       /* for gcc compat */
104e5dd7070Spatrick #define bit_XSAVE       0x04000000
105e5dd7070Spatrick #define bit_OSXSAVE     0x08000000
106e5dd7070Spatrick #define bit_AVX         0x10000000
107e5dd7070Spatrick #define bit_F16C        0x20000000
108e5dd7070Spatrick #define bit_RDRND       0x40000000
109e5dd7070Spatrick 
110e5dd7070Spatrick /* Features in %edx for leaf 1 */
111e5dd7070Spatrick #define bit_FPU         0x00000001
112e5dd7070Spatrick #define bit_VME         0x00000002
113e5dd7070Spatrick #define bit_DE          0x00000004
114e5dd7070Spatrick #define bit_PSE         0x00000008
115e5dd7070Spatrick #define bit_TSC         0x00000010
116e5dd7070Spatrick #define bit_MSR         0x00000020
117e5dd7070Spatrick #define bit_PAE         0x00000040
118e5dd7070Spatrick #define bit_MCE         0x00000080
119e5dd7070Spatrick #define bit_CX8         0x00000100
120e5dd7070Spatrick #define bit_CMPXCHG8B   bit_CX8         /* for gcc compat */
121e5dd7070Spatrick #define bit_APIC        0x00000200
122e5dd7070Spatrick #define bit_SEP         0x00000800
123e5dd7070Spatrick #define bit_MTRR        0x00001000
124e5dd7070Spatrick #define bit_PGE         0x00002000
125e5dd7070Spatrick #define bit_MCA         0x00004000
126e5dd7070Spatrick #define bit_CMOV        0x00008000
127e5dd7070Spatrick #define bit_PAT         0x00010000
128e5dd7070Spatrick #define bit_PSE36       0x00020000
129e5dd7070Spatrick #define bit_PSN         0x00040000
130e5dd7070Spatrick #define bit_CLFSH       0x00080000
131e5dd7070Spatrick #define bit_DS          0x00200000
132e5dd7070Spatrick #define bit_ACPI        0x00400000
133e5dd7070Spatrick #define bit_MMX         0x00800000
134e5dd7070Spatrick #define bit_FXSR        0x01000000
135e5dd7070Spatrick #define bit_FXSAVE      bit_FXSR        /* for gcc compat */
136e5dd7070Spatrick #define bit_SSE         0x02000000
137e5dd7070Spatrick #define bit_SSE2        0x04000000
138e5dd7070Spatrick #define bit_SS          0x08000000
139e5dd7070Spatrick #define bit_HTT         0x10000000
140e5dd7070Spatrick #define bit_TM          0x20000000
141e5dd7070Spatrick #define bit_PBE         0x80000000
142e5dd7070Spatrick 
143e5dd7070Spatrick /* Features in %ebx for leaf 7 sub-leaf 0 */
144e5dd7070Spatrick #define bit_FSGSBASE    0x00000001
145e5dd7070Spatrick #define bit_SGX         0x00000004
146e5dd7070Spatrick #define bit_BMI         0x00000008
147e5dd7070Spatrick #define bit_HLE         0x00000010
148e5dd7070Spatrick #define bit_AVX2        0x00000020
149e5dd7070Spatrick #define bit_SMEP        0x00000080
150e5dd7070Spatrick #define bit_BMI2        0x00000100
151e5dd7070Spatrick #define bit_ENH_MOVSB   0x00000200
152e5dd7070Spatrick #define bit_INVPCID     0x00000400
153e5dd7070Spatrick #define bit_RTM         0x00000800
154e5dd7070Spatrick #define bit_MPX         0x00004000
155e5dd7070Spatrick #define bit_AVX512F     0x00010000
156e5dd7070Spatrick #define bit_AVX512DQ    0x00020000
157e5dd7070Spatrick #define bit_RDSEED      0x00040000
158e5dd7070Spatrick #define bit_ADX         0x00080000
159e5dd7070Spatrick #define bit_AVX512IFMA  0x00200000
160e5dd7070Spatrick #define bit_CLFLUSHOPT  0x00800000
161e5dd7070Spatrick #define bit_CLWB        0x01000000
162e5dd7070Spatrick #define bit_AVX512PF    0x04000000
163e5dd7070Spatrick #define bit_AVX512ER    0x08000000
164e5dd7070Spatrick #define bit_AVX512CD    0x10000000
165e5dd7070Spatrick #define bit_SHA         0x20000000
166e5dd7070Spatrick #define bit_AVX512BW    0x40000000
167e5dd7070Spatrick #define bit_AVX512VL    0x80000000
168e5dd7070Spatrick 
169e5dd7070Spatrick /* Features in %ecx for leaf 7 sub-leaf 0 */
170e5dd7070Spatrick #define bit_PREFTCHWT1       0x00000001
171e5dd7070Spatrick #define bit_AVX512VBMI       0x00000002
172e5dd7070Spatrick #define bit_PKU              0x00000004
173e5dd7070Spatrick #define bit_OSPKE            0x00000010
174e5dd7070Spatrick #define bit_WAITPKG          0x00000020
175e5dd7070Spatrick #define bit_AVX512VBMI2      0x00000040
176e5dd7070Spatrick #define bit_SHSTK            0x00000080
177e5dd7070Spatrick #define bit_GFNI             0x00000100
178e5dd7070Spatrick #define bit_VAES             0x00000200
179e5dd7070Spatrick #define bit_VPCLMULQDQ       0x00000400
180e5dd7070Spatrick #define bit_AVX512VNNI       0x00000800
181e5dd7070Spatrick #define bit_AVX512BITALG     0x00001000
182e5dd7070Spatrick #define bit_AVX512VPOPCNTDQ  0x00004000
183e5dd7070Spatrick #define bit_RDPID            0x00400000
184e5dd7070Spatrick #define bit_CLDEMOTE         0x02000000
185e5dd7070Spatrick #define bit_MOVDIRI          0x08000000
186e5dd7070Spatrick #define bit_MOVDIR64B        0x10000000
187e5dd7070Spatrick #define bit_ENQCMD           0x20000000
188e5dd7070Spatrick 
189e5dd7070Spatrick /* Features in %edx for leaf 7 sub-leaf 0 */
190e5dd7070Spatrick #define bit_AVX5124VNNIW  0x00000004
191e5dd7070Spatrick #define bit_AVX5124FMAPS  0x00000008
192a9ac8606Spatrick #define bit_UINTR         0x00000020
193ec727ea7Spatrick #define bit_SERIALIZE     0x00004000
194ec727ea7Spatrick #define bit_TSXLDTRK      0x00010000
195e5dd7070Spatrick #define bit_PCONFIG       0x00040000
196e5dd7070Spatrick #define bit_IBT           0x00100000
197ec727ea7Spatrick #define bit_AMXBF16       0x00400000
198*12c85518Srobert #define bit_AVX512FP16    0x00800000
199ec727ea7Spatrick #define bit_AMXTILE       0x01000000
200ec727ea7Spatrick #define bit_AMXINT8       0x02000000
201e5dd7070Spatrick 
202e5dd7070Spatrick /* Features in %eax for leaf 7 sub-leaf 1 */
203*12c85518Srobert #define bit_RAOINT        0x00000008
204*12c85518Srobert #define bit_AVXVNNI       0x00000010
205e5dd7070Spatrick #define bit_AVX512BF16    0x00000020
206*12c85518Srobert #define bit_CMPCCXADD     0x00000080
207*12c85518Srobert #define bit_AMXFP16       0x00200000
208a9ac8606Spatrick #define bit_HRESET        0x00400000
209*12c85518Srobert #define bit_AVXIFMA       0x00800000
210*12c85518Srobert 
211*12c85518Srobert /* Features in %edx for leaf 7 sub-leaf 1 */
212*12c85518Srobert #define bit_AVXVNNIINT8   0x00000010
213*12c85518Srobert #define bit_AVXNECONVERT  0x00000020
214*12c85518Srobert #define bit_PREFETCHI     0x00004000
215e5dd7070Spatrick 
216e5dd7070Spatrick /* Features in %eax for leaf 13 sub-leaf 1 */
217e5dd7070Spatrick #define bit_XSAVEOPT    0x00000001
218e5dd7070Spatrick #define bit_XSAVEC      0x00000002
219e5dd7070Spatrick #define bit_XSAVES      0x00000008
220e5dd7070Spatrick 
221e5dd7070Spatrick /* Features in %eax for leaf 0x14 sub-leaf 0 */
222e5dd7070Spatrick #define bit_PTWRITE     0x00000010
223e5dd7070Spatrick 
224e5dd7070Spatrick /* Features in %ecx for leaf 0x80000001 */
225e5dd7070Spatrick #define bit_LAHF_LM     0x00000001
226e5dd7070Spatrick #define bit_ABM         0x00000020
227e5dd7070Spatrick #define bit_LZCNT       bit_ABM        /* for gcc compat */
228e5dd7070Spatrick #define bit_SSE4a       0x00000040
229e5dd7070Spatrick #define bit_PRFCHW      0x00000100
230e5dd7070Spatrick #define bit_XOP         0x00000800
231e5dd7070Spatrick #define bit_LWP         0x00008000
232e5dd7070Spatrick #define bit_FMA4        0x00010000
233e5dd7070Spatrick #define bit_TBM         0x00200000
234e5dd7070Spatrick #define bit_MWAITX      0x20000000
235e5dd7070Spatrick 
236e5dd7070Spatrick /* Features in %edx for leaf 0x80000001 */
237e5dd7070Spatrick #define bit_MMXEXT      0x00400000
238e5dd7070Spatrick #define bit_LM          0x20000000
239e5dd7070Spatrick #define bit_3DNOWP      0x40000000
240e5dd7070Spatrick #define bit_3DNOW       0x80000000
241e5dd7070Spatrick 
242e5dd7070Spatrick /* Features in %ebx for leaf 0x80000008 */
243e5dd7070Spatrick #define bit_CLZERO      0x00000001
244*12c85518Srobert #define bit_RDPRU       0x00000010
245e5dd7070Spatrick #define bit_WBNOINVD    0x00000200
246e5dd7070Spatrick 
247e5dd7070Spatrick 
248e5dd7070Spatrick #if __i386__
249e5dd7070Spatrick #define __cpuid(__leaf, __eax, __ebx, __ecx, __edx) \
250e5dd7070Spatrick     __asm("cpuid" : "=a"(__eax), "=b" (__ebx), "=c"(__ecx), "=d"(__edx) \
251e5dd7070Spatrick                   : "0"(__leaf))
252e5dd7070Spatrick 
253e5dd7070Spatrick #define __cpuid_count(__leaf, __count, __eax, __ebx, __ecx, __edx) \
254e5dd7070Spatrick     __asm("cpuid" : "=a"(__eax), "=b" (__ebx), "=c"(__ecx), "=d"(__edx) \
255e5dd7070Spatrick                   : "0"(__leaf), "2"(__count))
256e5dd7070Spatrick #else
257e5dd7070Spatrick /* x86-64 uses %rbx as the base register, so preserve it. */
258e5dd7070Spatrick #define __cpuid(__leaf, __eax, __ebx, __ecx, __edx) \
259e5dd7070Spatrick     __asm("  xchgq  %%rbx,%q1\n" \
260e5dd7070Spatrick           "  cpuid\n" \
261e5dd7070Spatrick           "  xchgq  %%rbx,%q1" \
262e5dd7070Spatrick         : "=a"(__eax), "=r" (__ebx), "=c"(__ecx), "=d"(__edx) \
263e5dd7070Spatrick         : "0"(__leaf))
264e5dd7070Spatrick 
265e5dd7070Spatrick #define __cpuid_count(__leaf, __count, __eax, __ebx, __ecx, __edx) \
266e5dd7070Spatrick     __asm("  xchgq  %%rbx,%q1\n" \
267e5dd7070Spatrick           "  cpuid\n" \
268e5dd7070Spatrick           "  xchgq  %%rbx,%q1" \
269e5dd7070Spatrick         : "=a"(__eax), "=r" (__ebx), "=c"(__ecx), "=d"(__edx) \
270e5dd7070Spatrick         : "0"(__leaf), "2"(__count))
271e5dd7070Spatrick #endif
272e5dd7070Spatrick 
__get_cpuid_max(unsigned int __leaf,unsigned int * __sig)273*12c85518Srobert static __inline unsigned int __get_cpuid_max (unsigned int __leaf,
274*12c85518Srobert                                               unsigned int *__sig)
275e5dd7070Spatrick {
276e5dd7070Spatrick     unsigned int __eax, __ebx, __ecx, __edx;
277e5dd7070Spatrick #if __i386__
278e5dd7070Spatrick     int __cpuid_supported;
279e5dd7070Spatrick 
280e5dd7070Spatrick     __asm("  pushfl\n"
281e5dd7070Spatrick           "  popl   %%eax\n"
282e5dd7070Spatrick           "  movl   %%eax,%%ecx\n"
283e5dd7070Spatrick           "  xorl   $0x00200000,%%eax\n"
284e5dd7070Spatrick           "  pushl  %%eax\n"
285e5dd7070Spatrick           "  popfl\n"
286e5dd7070Spatrick           "  pushfl\n"
287e5dd7070Spatrick           "  popl   %%eax\n"
288e5dd7070Spatrick           "  movl   $0,%0\n"
289e5dd7070Spatrick           "  cmpl   %%eax,%%ecx\n"
290e5dd7070Spatrick           "  je     1f\n"
291e5dd7070Spatrick           "  movl   $1,%0\n"
292e5dd7070Spatrick           "1:"
293e5dd7070Spatrick         : "=r" (__cpuid_supported) : : "eax", "ecx");
294e5dd7070Spatrick     if (!__cpuid_supported)
295e5dd7070Spatrick         return 0;
296e5dd7070Spatrick #endif
297e5dd7070Spatrick 
298e5dd7070Spatrick     __cpuid(__leaf, __eax, __ebx, __ecx, __edx);
299e5dd7070Spatrick     if (__sig)
300e5dd7070Spatrick         *__sig = __ebx;
301e5dd7070Spatrick     return __eax;
302e5dd7070Spatrick }
303e5dd7070Spatrick 
__get_cpuid(unsigned int __leaf,unsigned int * __eax,unsigned int * __ebx,unsigned int * __ecx,unsigned int * __edx)304e5dd7070Spatrick static __inline int __get_cpuid (unsigned int __leaf, unsigned int *__eax,
305e5dd7070Spatrick                                  unsigned int *__ebx, unsigned int *__ecx,
306e5dd7070Spatrick                                  unsigned int *__edx)
307e5dd7070Spatrick {
308e5dd7070Spatrick     unsigned int __max_leaf = __get_cpuid_max(__leaf & 0x80000000, 0);
309e5dd7070Spatrick 
310e5dd7070Spatrick     if (__max_leaf == 0 || __max_leaf < __leaf)
311e5dd7070Spatrick         return 0;
312e5dd7070Spatrick 
313e5dd7070Spatrick     __cpuid(__leaf, *__eax, *__ebx, *__ecx, *__edx);
314e5dd7070Spatrick     return 1;
315e5dd7070Spatrick }
316e5dd7070Spatrick 
__get_cpuid_count(unsigned int __leaf,unsigned int __subleaf,unsigned int * __eax,unsigned int * __ebx,unsigned int * __ecx,unsigned int * __edx)317e5dd7070Spatrick static __inline int __get_cpuid_count (unsigned int __leaf,
318e5dd7070Spatrick                                        unsigned int __subleaf,
319e5dd7070Spatrick                                        unsigned int *__eax, unsigned int *__ebx,
320e5dd7070Spatrick                                        unsigned int *__ecx, unsigned int *__edx)
321e5dd7070Spatrick {
322e5dd7070Spatrick     unsigned int __max_leaf = __get_cpuid_max(__leaf & 0x80000000, 0);
323e5dd7070Spatrick 
324e5dd7070Spatrick     if (__max_leaf == 0 || __max_leaf < __leaf)
325e5dd7070Spatrick         return 0;
326e5dd7070Spatrick 
327e5dd7070Spatrick     __cpuid_count(__leaf, __subleaf, *__eax, *__ebx, *__ecx, *__edx);
328e5dd7070Spatrick     return 1;
329e5dd7070Spatrick }
330a9ac8606Spatrick 
331a9ac8606Spatrick #endif /* __CPUID_H */
332