]> git.ozlabs.org Git - ccan/blob - ccan/cpuid/cpuid.h
cpuid: drop support for testing extended features
[ccan] / ccan / cpuid / cpuid.h
1 /*
2  * Copyright (c) 2013 Ahmed Samy  <f.fallen45@gmail.com>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 #ifndef CCAN_CPUID_H
23 #define CCAN_CPUID_H
24
25 #include <stdbool.h>
26 #include <stdint.h>
27
28 /**
29  * enum cpuid - stuff to get information about from the CPU.
30  *
31  * This is used as a parameter in cpuid().
32  *
33  * CPU_VENDORID:
34  *      The CPU's Vendor ID.
35  *
36  * CPU_PROCINFO_AND_FEATUREBITS:
37  *      Processor information and feature bits (SSE, etc.).
38  *
39  * CPU_CACHE_AND_TLBD_INFO
40  *      Cache and TLBD Information.
41  *
42  * CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED:
43  *      Highest extended function supported address.
44  *      Can be like 0x80000008.
45  *
46  * CPU_EXTENDED_PROC_INFO_FEATURE_BITS:
47  *      Extended processor information and feature bits (64bit etc.)
48  *
49  * CPU_PROC_BRAND_STRING:
50  *      The Processor's brand string.
51  *
52  * CPU_L1_CACHE_AND_TLB_IDS:
53  *      L1 Cache and TLB Identifications.
54  *
55  * CPU_EXTENDED_L2_CACHE_FEATURES:
56  *      Extended L2 Cache features.
57  *
58  * CPU_ADV_POWER_MGT_INFO:
59  *      Advaned power management information.
60  *
61  * CPU_VIRT_PHYS_ADDR_SIZES:
62  *      Virtual and physical address sizes.
63  */
64
65 typedef enum cpuid {
66         CPU_VENDORID                                    = 0,
67         CPU_PROCINFO_AND_FEATUREBITS                    = 1,
68         CPU_CACHE_AND_TLBD_INFO                         = 2,
69
70         CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED         = 0x80000000,
71         CPU_EXTENDED_PROC_INFO_FEATURE_BITS             = 0x80000001,
72         CPU_PROC_BRAND_STRING                           = 0x80000002,
73         CPU_L1_CACHE_AND_TLB_IDS                        = 0x80000005,
74         CPU_EXTENDED_L2_CACHE_FEATURES                  = 0x80000006,
75         CPU_ADV_POWER_MGT_INFO                          = 0x80000007,
76         CPU_VIRT_PHYS_ADDR_SIZES                        = 0x80000008
77 } cpuid_t;
78
79 enum {
80         CPUID_FEAT_ECX_SSE3         = 1 << 0, 
81         CPUID_FEAT_ECX_PCLMUL       = 1 << 1,
82         CPUID_FEAT_ECX_DTES64       = 1 << 2,
83         CPUID_FEAT_ECX_MONITOR      = 1 << 3,  
84         CPUID_FEAT_ECX_DS_CPL       = 1 << 4,  
85         CPUID_FEAT_ECX_VMX          = 1 << 5,  
86         CPUID_FEAT_ECX_SMX          = 1 << 6,  
87         CPUID_FEAT_ECX_EST          = 1 << 7,  
88         CPUID_FEAT_ECX_TM2          = 1 << 8,  
89         CPUID_FEAT_ECX_SSSE3        = 1 << 9,  
90         CPUID_FEAT_ECX_CID          = 1 << 10,
91         CPUID_FEAT_ECX_FMA          = 1 << 12,
92         CPUID_FEAT_ECX_CX16         = 1 << 13, 
93         CPUID_FEAT_ECX_ETPRD        = 1 << 14, 
94         CPUID_FEAT_ECX_PDCM         = 1 << 15, 
95         CPUID_FEAT_ECX_DCA          = 1 << 18, 
96         CPUID_FEAT_ECX_SSE4_1       = 1 << 19, 
97         CPUID_FEAT_ECX_SSE4_2       = 1 << 20, 
98         CPUID_FEAT_ECX_x2APIC       = 1 << 21, 
99         CPUID_FEAT_ECX_MOVBE        = 1 << 22, 
100         CPUID_FEAT_ECX_POPCNT       = 1 << 23, 
101         CPUID_FEAT_ECX_AES          = 1 << 25, 
102         CPUID_FEAT_ECX_XSAVE        = 1 << 26, 
103         CPUID_FEAT_ECX_OSXSAVE      = 1 << 27, 
104         CPUID_FEAT_ECX_AVX          = 1 << 28,
105
106         CPUID_FEAT_EDX_FPU          = 1 << 0,  
107         CPUID_FEAT_EDX_VME          = 1 << 1,  
108         CPUID_FEAT_EDX_DE           = 1 << 2,  
109         CPUID_FEAT_EDX_PSE          = 1 << 3,  
110         CPUID_FEAT_EDX_TSC          = 1 << 4,  
111         CPUID_FEAT_EDX_MSR          = 1 << 5,  
112         CPUID_FEAT_EDX_PAE          = 1 << 6,  
113         CPUID_FEAT_EDX_MCE          = 1 << 7,  
114         CPUID_FEAT_EDX_CX8          = 1 << 8,  
115         CPUID_FEAT_EDX_APIC         = 1 << 9,  
116         CPUID_FEAT_EDX_SEP          = 1 << 11, 
117         CPUID_FEAT_EDX_MTRR         = 1 << 12, 
118         CPUID_FEAT_EDX_PGE          = 1 << 13, 
119         CPUID_FEAT_EDX_MCA          = 1 << 14, 
120         CPUID_FEAT_EDX_CMOV         = 1 << 15, 
121         CPUID_FEAT_EDX_PAT          = 1 << 16, 
122         CPUID_FEAT_EDX_PSE36        = 1 << 17, 
123         CPUID_FEAT_EDX_PSN          = 1 << 18, 
124         CPUID_FEAT_EDX_CLF          = 1 << 19, 
125         CPUID_FEAT_EDX_DTES         = 1 << 21, 
126         CPUID_FEAT_EDX_ACPI         = 1 << 22, 
127         CPUID_FEAT_EDX_MMX          = 1 << 23, 
128         CPUID_FEAT_EDX_FXSR         = 1 << 24, 
129         CPUID_FEAT_EDX_SSE          = 1 << 25, 
130         CPUID_FEAT_EDX_SSE2         = 1 << 26, 
131         CPUID_FEAT_EDX_SS           = 1 << 27, 
132         CPUID_FEAT_EDX_HTT          = 1 << 28, 
133         CPUID_FEAT_EDX_TM1          = 1 << 29, 
134         CPUID_FEAT_EDX_IA64         = 1 << 30,
135         CPUID_FEAT_EDX_PBE          = 1 << 31
136 };
137
138 typedef enum cputype {
139         CT_NONE,
140         CT_AMDK5,
141         CT_AMD,
142         CT_CENTAUR,
143         CT_CYRIX,
144         CT_INTEL,
145         CT_TRANSMETA,
146         CT_NATIONAL_SEMICONDUCTOR,
147         CT_NEXGEN,
148         CT_RISE,
149         CT_SIS,
150         CT_UMC,
151         CT_VIA,
152         CT_VORTEX,
153         CT_KVM
154 } cputype_t;
155
156 #if defined(__i386__) || defined(__i386) || defined(__x86_64) \
157         || defined(_M_AMD64) || defined(__M_X64)
158
159 /**
160  * cpuid_get_cpu_type - Get CPU Type
161  *
162  * Returns the CPU Type as cputype_t.
163  *
164  * See also: cpuid_get_cpu_type_string()
165  */
166 #define is_intel_cpu()  cpuid_get_cpu_type() == CT_INTEL
167 #define is_amd_cpu()    cpuid_get_cpu_type() == CT_AMDK5 || cpuid_get_cpu_type() == CT_AMD
168 cputype_t cpuid_get_cpu_type(void);
169
170 /**
171  * cpuid_sprintf_cputype - Get CPU Type string
172  * @cputype: a char of atleast 12 bytes in it.
173  *
174  * Returns true on success, false on failure
175  */
176 bool cpuid_sprintf_cputype(const cputype_t cputype, char *buf);
177
178 /**
179  * cpuid_is_supported - test if the CPUID instruction is supported
180  *
181  * CPUID is not supported by old CPUS.
182  *
183  * Returns true if the cpuid instruction is supported, false otherwise.
184  *
185  * See also: cpuid()
186  */
187 bool cpuid_is_supported(void);
188
189 /**
190  * cpuid_highest_ext_func_supported - Get the highest extended function supported
191  *
192  *
193  * Returns the highest extended function supported.
194  *
195  * This is the same as calling:
196  *      cpuid(CPU_HIGHEST_EEXTENDED_FUNCTION_SUPPORTED, &highest);
197  *
198  * This is made visible to the linker because it's easier to call it
199  * instead of calling cpuid with less type-checking.  cpuid calls this.
200  *
201  * See also: cpuid()
202  */
203 uint32_t cpuid_highest_ext_func_supported(void);
204
205 /**
206  * cpuid - Get Some information from the CPU.
207  *
208  * This function expects buf to be a valid pointer to a string/int/...
209  * depending on the requested information.
210  *
211  * For CPU_VENDOR_ID:
212  *      Returns a string into buf.
213  *
214  * For CPU_PROCINFO_AND_FEATUREBITS:
215  *      buf[0]: Stepping
216  *      buf[1]: Model
217  *      buf[2]: Family
218  *      buf[3]: Extended Model
219  *      buf[4]: Extended Family
220  *      buf[5] and buf[6]:
221  *              Feature flags
222  *      buf[7]: Brand Index
223  *      buf[8]: CL Flush Line Size
224  *      buf[9]: Logical Processors
225  *      buf[10]: Initial APICID
226  *
227  * For CPU_L1_CACHE_AND_TLB_IDS:
228  *      buf[0]: (eax):
229  *              - 7..0  Number of times to exec cpuid to get all descriptors.
230  *              - 15..8 Instruction TLB: 4K Pages, 4-way set associtive, 128 entries.
231  *              - 23..16 Data TLB: 4k Pages, 4-way set associtive, 128 entries.
232  *              - 24..31 Instruction TLB: 4K Pages, 4-way set associtive, 2 entries.
233  *      buf[1]: (ebx):
234  *              - 7..0 64-byte prefetching
235  *              - 8..31 Null descriptor
236  *      buf[2]: (ecx):
237  *              - 0..31 Null descriptor
238  *      buf[3]: (edx):
239  *              - 7..0 2nd-level cache, 2M, 8-way set associtive, 64-byte line size
240  *              - 15..8 1st-level instruction cache: 32K, 8-way set associtive, 64 byte line size
241  *              - 16..23 Data TLB: 4M Pages, 4-way set associtive, 8 entires.
242  *              - 24..31 1st-level data cache: 32K, 8-way set associtive, 64 byte line size
243  *
244  * For CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED:
245  *      Returns the highest supported function in *buf (expects an integer ofc)
246  *
247  * For CPU_EXTENDED_PROC_INFO_FEATURE_BITS:
248  *      Returns them in buf[0] and buf[1].
249  *
250  * For CPU_EXTENDED_L2_CACHE_FEATURES:
251  *      buf[0]: Line size
252  *      buf[1]: Associativity
253  *      buf[2]: Cache size.
254  *
255  * For CPU_VIRT_PHYS_ADDR_SIZES:
256  *      buf[0]: Physical
257  *      buf[1]: Virtual
258  *
259  * For CPU_PROC_BRAND_STRING:
260  *      Have a char array with at least 48 bytes assigned to it.
261  *
262  * Here's a page which will help you parse the data provided by this function.
263  *      http://www.flounder.com/cpuid_explorer2.htm
264  *
265  * If an invalid flag has been passed a 0xbaadf00d is returned in *buf.
266  */
267 void cpuid(cpuid_t info, uint32_t *buf);
268
269 /**
270  * cpuid_test_feature - Test if @feature is available
271  *
272  * Returns true if feature is supported, false otherwise.
273  *
274  * The feature parameter must be >= CPU_EXTENDED_PROC_INFO_FEATURE_BITS
275  *  and <= CPU_VIRT_PHYS_ADDR_SIZES.
276  */
277 bool cpuid_test_feature(cpuid_t feature);
278
279 /**
280  * cpuid_has_feature - Test if @feature is supported
281  *
282  * Test if the CPU supports MMX/SSE* etc.
283  * Use cpuid_has_ecxfeature() for *_ECX* features and
284  * cpuid_has_edxfeature() for *_EDX* features.
285  *
286  * Returns true if the feature is available, false otherwise.
287  */
288 bool cpuid_has_ecxfeature(int feature);
289 bool cpuid_has_edxfeature(int feature);
290
291 #else
292 #include <ccan/build_assert/build_assert.h>
293
294 #define cpuid_get_cpu_type()                    BUILD_ASSERT_OR_ZERO(0)
295 #define cpuid_get_cpu_type_string()             BUILD_ASSERT_OR_ZERO(0)
296
297 #define cpuid_is_supported()                    BUILD_ASSERT_OR_ZERO(0)
298 #define cpuid(info, buf)                        BUILD_ASSERT_OR_ZERO(0)
299
300 #define cpuid_highest_ext_func_supported()      BUILD_ASSERT_OR_ZERO(0)
301 #define cpuid_test_feature(feature)             BUILD_ASSERT_OR_ZERO(0)
302 #define cpuid_has_feature(feature, ext)         BUILD_ASSERT_OR_ZERO(0)
303
304 #endif
305 #endif