]> git.ozlabs.org Git - ccan/blob - ccan/cpuid/cpuid.h
a3e127e628c4ae7876acd8213b2ebd29630f9ec1
[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  * %CPUID_VENDORID:
34  *      The CPU's Vendor ID.
35  *
36  * %CPUID_PROCINFO_AND_FEATUREBITS:
37  *      Processor information and feature bits (SSE, etc.).
38  *
39  * %CPUID_CACHE_AND_TLBD_INFO
40  *      Cache and TLBD Information.
41  *      For AMD: Use CPUID_EXTENDED_L2_CACHE_FEATURES
42  *
43  * %CPUID_HIGHEST_EXTENDED_FUNCTION_SUPPORTED:
44  *      Highest extended function supported address.
45  *      Can be like 0x80000008.
46  *
47  * %CPUID_EXTENDED_PROC_INFO_FEATURE_BITS:
48  *      Extended processor information and feature bits (64bit etc.)
49  *
50  * %CPUID_PROC_BRAND_STRING:
51  *      The Processor's brand string.
52  *
53  * %CPUID_L1_CACHE_AND_TLB_IDS:
54  *      L1 Cache and TLB Identifications.
55  *      AMD Only.
56  *
57  * %CPUID_EXTENDED_L2_CACHE_FEATURES:
58  *      Extended L2 Cache features.
59  *
60  * %CPUID_ADV_POWER_MGT_INFO:
61  *      Advaned power management information.
62  *
63  * %CPUID_VIRT_PHYS_ADDR_SIZES:
64  *      Virtual and physical address sizes.
65  */
66
67 typedef enum cpuid {
68         CPUID_VENDORID                                  = 0,
69         CPUID_PROCINFO_AND_FEATUREBITS                  = 1,
70         CPUID_CACHE_AND_TLBD_INFO                       = 2,
71
72         CPUID_HIGHEST_EXTENDED_FUNCTION_SUPPORTED       = 0x80000000,
73         CPUID_EXTENDED_PROC_INFO_FEATURE_BITS           = 0x80000001,
74         CPUID_PROC_BRAND_STRING                         = 0x80000002,
75         CPUID_L1_CACHE_AND_TLB_IDS                      = 0x80000005,
76         CPUID_EXTENDED_L2_CACHE_FEATURES                = 0x80000006,
77         CPUID_ADV_POWER_MGT_INFO                        = 0x80000007,
78         CPUID_VIRT_PHYS_ADDR_SIZES                      = 0x80000008
79 } cpuid_t;
80
81 enum {
82         CPUID_FEAT_ECX_SSE3         = 1 << 0, 
83         CPUID_FEAT_ECX_PCLMUL       = 1 << 1,
84         CPUID_FEAT_ECX_DTES64       = 1 << 2,
85         CPUID_FEAT_ECX_MONITOR      = 1 << 3,  
86         CPUID_FEAT_ECX_DS_CPL       = 1 << 4,  
87         CPUID_FEAT_ECX_VMX          = 1 << 5,  
88         CPUID_FEAT_ECX_SMX          = 1 << 6,  
89         CPUID_FEAT_ECX_EST          = 1 << 7,  
90         CPUID_FEAT_ECX_TM2          = 1 << 8,  
91         CPUID_FEAT_ECX_SSSE3        = 1 << 9,  
92         CPUID_FEAT_ECX_CID          = 1 << 10,
93         CPUID_FEAT_ECX_FMA          = 1 << 12,
94         CPUID_FEAT_ECX_CX16         = 1 << 13, 
95         CPUID_FEAT_ECX_ETPRD        = 1 << 14, 
96         CPUID_FEAT_ECX_PDCM         = 1 << 15, 
97         CPUID_FEAT_ECX_DCA          = 1 << 18, 
98         CPUID_FEAT_ECX_SSE4_1       = 1 << 19, 
99         CPUID_FEAT_ECX_SSE4_2       = 1 << 20, 
100         CPUID_FEAT_ECX_x2APIC       = 1 << 21, 
101         CPUID_FEAT_ECX_MOVBE        = 1 << 22, 
102         CPUID_FEAT_ECX_POPCNT       = 1 << 23, 
103         CPUID_FEAT_ECX_AES          = 1 << 25, 
104         CPUID_FEAT_ECX_XSAVE        = 1 << 26, 
105         CPUID_FEAT_ECX_OSXSAVE      = 1 << 27, 
106         CPUID_FEAT_ECX_AVX          = 1 << 28,
107
108         CPUID_FEAT_ECX_ALL          = CPUID_FEAT_ECX_SSE3 | CPUID_FEAT_ECX_PCLMUL | CPUID_FEAT_ECX_DTES64
109                                         | CPUID_FEAT_ECX_MONITOR | CPUID_FEAT_ECX_DS_CPL | CPUID_FEAT_ECX_VMX
110                                         | CPUID_FEAT_ECX_SMX | CPUID_FEAT_ECX_EST | CPUID_FEAT_ECX_TM2
111                                         | CPUID_FEAT_ECX_SSSE3 | CPUID_FEAT_ECX_CID | CPUID_FEAT_ECX_FMA
112                                         | CPUID_FEAT_ECX_CX16 | CPUID_FEAT_ECX_ETPRD | CPUID_FEAT_ECX_PDCM
113                                         | CPUID_FEAT_ECX_DCA | CPUID_FEAT_ECX_SSE4_1 | CPUID_FEAT_ECX_SSE4_2
114                                         | CPUID_FEAT_ECX_x2APIC | CPUID_FEAT_ECX_MOVBE | CPUID_FEAT_ECX_POPCNT
115                                         | CPUID_FEAT_ECX_AES | CPUID_FEAT_ECX_XSAVE | CPUID_FEAT_ECX_OSXSAVE
116                                         | CPUID_FEAT_ECX_AVX,
117
118         CPUID_FEAT_EDX_FPU          = 1 << 0,  
119         CPUID_FEAT_EDX_VME          = 1 << 1,  
120         CPUID_FEAT_EDX_DE           = 1 << 2,  
121         CPUID_FEAT_EDX_PSE          = 1 << 3,  
122         CPUID_FEAT_EDX_TSC          = 1 << 4,  
123         CPUID_FEAT_EDX_MSR          = 1 << 5,  
124         CPUID_FEAT_EDX_PAE          = 1 << 6,  
125         CPUID_FEAT_EDX_MCE          = 1 << 7,  
126         CPUID_FEAT_EDX_CX8          = 1 << 8,  
127         CPUID_FEAT_EDX_APIC         = 1 << 9,  
128         CPUID_FEAT_EDX_SEP          = 1 << 11, 
129         CPUID_FEAT_EDX_MTRR         = 1 << 12, 
130         CPUID_FEAT_EDX_PGE          = 1 << 13, 
131         CPUID_FEAT_EDX_MCA          = 1 << 14, 
132         CPUID_FEAT_EDX_CMOV         = 1 << 15, 
133         CPUID_FEAT_EDX_PAT          = 1 << 16, 
134         CPUID_FEAT_EDX_PSE36        = 1 << 17, 
135         CPUID_FEAT_EDX_PSN          = 1 << 18, 
136         CPUID_FEAT_EDX_CLF          = 1 << 19, 
137         CPUID_FEAT_EDX_DTES         = 1 << 21, 
138         CPUID_FEAT_EDX_ACPI         = 1 << 22, 
139         CPUID_FEAT_EDX_MMX          = 1 << 23, 
140         CPUID_FEAT_EDX_FXSR         = 1 << 24, 
141         CPUID_FEAT_EDX_SSE          = 1 << 25, 
142         CPUID_FEAT_EDX_SSE2         = 1 << 26, 
143         CPUID_FEAT_EDX_SS           = 1 << 27, 
144         CPUID_FEAT_EDX_HTT          = 1 << 28, 
145         CPUID_FEAT_EDX_TM1          = 1 << 29, 
146         CPUID_FEAT_EDX_IA64         = 1 << 30,
147         CPUID_FEAT_EDX_PBE          = 1 << 31,
148
149         CPUID_FEAT_EDX_ALL          = CPUID_FEAT_EDX_FPU | CPUID_FEAT_EDX_VME | CPUID_FEAT_EDX_DE
150                                         | CPUID_FEAT_EDX_PSE | CPUID_FEAT_EDX_TSC | CPUID_FEAT_EDX_MSR
151                                         | CPUID_FEAT_EDX_PAE | CPUID_FEAT_EDX_MCE | CPUID_FEAT_EDX_CX8
152                                         | CPUID_FEAT_EDX_APIC | CPUID_FEAT_EDX_SEP | CPUID_FEAT_EDX_MTRR
153                                         | CPUID_FEAT_EDX_PGE | CPUID_FEAT_EDX_MCA | CPUID_FEAT_EDX_CMOV
154                                         | CPUID_FEAT_EDX_PAT | CPUID_FEAT_EDX_PSE36 | CPUID_FEAT_EDX_PSN
155                                         | CPUID_FEAT_EDX_CLF | CPUID_FEAT_EDX_DTES | CPUID_FEAT_EDX_ACPI
156                                         | CPUID_FEAT_EDX_MMX | CPUID_FEAT_EDX_FXSR | CPUID_FEAT_EDX_SSE
157                                         | CPUID_FEAT_EDX_SSE2 | CPUID_FEAT_EDX_SS | CPUID_FEAT_EDX_HTT
158                                         | CPUID_FEAT_EDX_TM1 | CPUID_FEAT_EDX_IA64 | CPUID_FEAT_EDX_PBE
159 };
160
161 typedef enum cputype {
162         CT_NONE,
163         CT_AMDK5,
164         CT_AMD,
165         CT_CENTAUR,
166         CT_CYRIX,
167         CT_INTEL,
168         CT_TRANSMETA,
169         CT_NATIONAL_SEMICONDUCTOR,
170         CT_NEXGEN,
171         CT_RISE,
172         CT_SIS,
173         CT_UMC,
174         CT_VIA,
175         CT_VORTEX,
176         CT_KVM
177 } cputype_t;
178
179 #if defined(__i386__) || defined(__i386) || defined(__x86_64) \
180         || defined(_M_AMD64) || defined(__M_X64)
181
182 /**
183  * cpuid_get_cpu_type - Get CPU Type
184  *
185  * Returns the CPU Type as cputype_t.
186  *
187  * See also: cpuid_get_cpu_type_string()
188  */
189 #define is_intel_cpu()  cpuid_get_cpu_type() == CT_INTEL
190 #define is_amd_cpu()    cpuid_get_cpu_type() == CT_AMDK5 || cpuid_get_cpu_type() == CT_AMD
191 cputype_t cpuid_get_cpu_type(void);
192
193 /**
194  * cpuid_sprintf_cputype - Get CPU Type string
195  * @cputype: a char of atleast 12 bytes in it.
196  *
197  * Returns true on success, false on failure
198  */
199 bool cpuid_sprintf_cputype(const cputype_t cputype, char *buf);
200
201 /**
202  * cpuid_is_supported - test if the CPUID instruction is supported
203  *
204  * CPUID is not supported by old CPUS.
205  *
206  * Returns true if the cpuid instruction is supported, false otherwise.
207  *
208  * See also: cpuid()
209  */
210 bool cpuid_is_supported(void);
211
212 /**
213  * cpuid_highest_ext_func_supported - Get the highest extended function supported
214  *
215  *
216  * Returns the highest extended function supported.
217  *
218  * This is the same as calling:
219  *      cpuid(CPUID_HIGHEST_EEXTENDED_FUNCTION_SUPPORTED, &highest);
220  *
221  * This is made visible to the linker because it's easier to call it
222  * instead of calling cpuid with less type-checking.  cpuid calls this.
223  *
224  * See also: cpuid()
225  */
226 uint32_t cpuid_highest_ext_func_supported(void);
227
228 /**
229  * cpuid - Get Some information from the CPU.
230  *
231  * This function expects buf to be a valid pointer to a string/int/...
232  * depending on the requested information.
233  *
234  * For CPUID_VENDOR_ID:
235  *      Returns a string into buf.
236  *
237  * For CPUID_PROCINFO_AND_FEATUREBITS:
238  *      buf[0]: Stepping
239  *      buf[1]: Model
240  *      buf[2]: Family
241  *      buf[3]: Extended Model
242  *      buf[4]: Extended Family
243  *      buf[5]: Brand Index
244  *      buf[6]: CL Flush Line Size
245  *      buf[7]: Logical Processors
246  *      buf[8]: Initial APICID
247  *
248  * For CPUID_L1_CACHE_AND_TLB_IDS:
249  *      buf[0] to buf[3]: 2M+4M page TLB info
250  *              0: Inst count
251  *              1: Inst Assoc
252  *              2: Data Count
253  *              3: Data Assoc
254  *      buf[4] to buf[7]: 4k page TLB info
255  *              0: Inst count
256  *              1: Inst Assoc
257  *              2: Data Count
258  *              3: Data Assoc
259  *      buf[8] to buf[11]: L1 data cache information
260  *              0: Line Size
261  *              1: LinesPerTag
262  *              2: Associativity
263  *              3: CacheSize
264  *      buf[12] to buf[15]: L1 instruction cache info
265  *              0: Line Size
266  *              1: LinesPerTag
267  *              2: Associativity
268  *              3: CacheSize
269  *
270  * For CPUID_HIGHEST_EXTENDED_FUNCTION_SUPPORTED:
271  *      Returns the highest supported function in *buf (expects an integer ofc)
272  *
273  * For CPUID_EXTENDED_PROC_INFO_FEATURE_BITS:
274  *      Returns them in buf[0] and buf[1].
275  *
276  * For CPUID_EXTENDED_L2_CACHE_FEATURES:
277  *      buf[0]: Line size
278  *      buf[1]: Associativity
279  *      buf[2]: Cache size.
280  *
281  * For CPUID_VIRT_PHYS_ADDR_SIZES:
282  *      buf[0]: Physical
283  *      buf[1]: Virtual
284  *
285  * For CPUID_PROC_BRAND_STRING:
286  *      Have a char array with at least 48 bytes assigned to it.
287  *
288  * If an invalid flag has been passed a 0xbaadf00d is returned in *buf.
289  */
290 void cpuid(cpuid_t info, uint32_t *buf);
291
292 /**
293  * cpuid_write_info - Write specified CPU information to a file.
294  * @info: Bit set of information to write.
295  * @featureset: Bit set of features to write.
296  * @outfile: Output filename (Max 256).
297  *
298  * If @outfile is NULL, a name is choosen in the following format:
299  *      CPUVENDOR_PROCESSORBRAND.cpuid
300  *
301  * Returns true on success, false otherwise.
302  *
303  * Example usage:
304  *      if (!cpuid_write_info(CPUID_VENDORID | CPUID_PROC_BRAND_STRING,
305  *                              CPUID_FEAT_ECX_SSE3 | CPUID_FEAT_EDX_FPU,
306  *                              "cpuinfo.cpuid"))
307  *              ... error ...
308  */
309 bool cpuid_write_info(uint32_t info, uint32_t featureset, const char *outfile);
310
311 /**
312  * cpuid_test_feature - Test if @feature is available
313  *
314  * Returns true if feature is supported, false otherwise.
315  *
316  * The feature parameter must be >= CPUID_EXTENDED_PROC_INFO_FEATURE_BITS
317  *  and <= CPUID_VIRT_PHYS_ADDR_SIZES.
318  */
319 bool cpuid_test_feature(cpuid_t feature);
320
321 /**
322  * cpuid_has_feature - Test if @feature is supported
323  *
324  * Test if the CPU supports MMX/SSE* etc.
325  * Use cpuid_has_ecxfeature() for *_ECX* features and
326  * cpuid_has_edxfeature() for *_EDX* features.
327  *
328  * Returns true if the feature is available, false otherwise.
329  */
330 bool cpuid_has_ecxfeature(int feature);
331 bool cpuid_has_edxfeature(int feature);
332
333 #else
334 #include <ccan/build_assert/build_assert.h>
335
336 #define cpuid_get_cpu_type()                            BUILD_ASSERT_OR_ZERO(0)
337 #define cpuid_get_cpu_type_string()                     BUILD_ASSERT_OR_ZERO(0)
338
339 #define cpuid_is_supported()                            BUILD_ASSERT_OR_ZERO(0)
340 #define cpuid(info, buf)                                BUILD_ASSERT_OR_ZERO(0)
341 #define cpuid_write_info(info, featureset, outfile)     BUILD_ASSERT_OR_ZERO(0)
342
343 #define cpuid_highest_ext_func_supported()              BUILD_ASSERT_OR_ZERO(0)
344 #define cpuid_test_feature(feature)                     BUILD_ASSERT_OR_ZERO(0)
345 #define cpuid_has_ecxfeature(feature)                   BUILD_ASSERT_OR_ZERO(0)
346 #define cpuid_has_edxfeature(feature)                   BUILD_ASSERT_OR_ZERO(0)
347
348 #endif
349 #endif