]> git.ozlabs.org Git - ccan/blob - ccan/cpuid/cpuid.h
8c0b9b10487b5383b70c03e3c40855d12994a4a7
[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 /**
26  * enum cpuid - stuff to get information on from the CPU.
27  *
28  * This is used as a parameter in cpuid().
29  *
30  * CPU_VENDORID:
31  *      The CPU's Vendor ID.
32  *
33  * CPU_PROCINFO_AND_FEATUREBITS:
34  *      Processor information and feature bits (SSE, etc.).
35  *
36  * CPU_CACHE_AND_TLBD_INFO
37  *      Cache and TLBD Information.
38  *
39  * CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED:
40  *      Highest extended function supported address.
41  *      Can be like 0x80000008.
42  *
43  * CPU_EXTENDED_PROC_INFO_FEATURE_BITS:
44  *      Extended processor information and feature bits (64bit etc.)
45  *
46  * CPU_PROC_BRAND_STRING:
47  *      The Processor's brand string.
48  *
49  * CPU_L1_CACHE_AND_TLB_IDS:
50  *      L1 Cache and TLB Identifications.
51  *
52  * CPU_EXTENDED_L2_CACHE_FEATURES:
53  *      Extended L2 Cache features.
54  *
55  * CPU_ADV_POWER_MGT_INFO:
56  *      Advaned power management information.
57  *
58  * CPU_VIRT_PHYS_ADDR_SIZES:
59  *      Virtual and physical address sizes.
60  */
61
62 typedef enum cpuid {
63         CPU_VENDORID                                    = 0,
64         CPU_PROCINFO_AND_FEATUREBITS                    = 1,
65         CPU_CACHE_AND_TLBD_INFO                         = 2,
66
67         CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED         = 0x80000000,
68         CPU_EXTENDED_PROC_INFO_FEATURE_BITS             = 0x80000001,
69         CPU_PROC_BRAND_STRING                           = 0x80000002, 
70         CPU_L1_CACHE_AND_TLB_IDS                        = 0x80000005,
71         CPU_EXTENDED_L2_CACHE_FEATURES                  = 0x80000006,
72         CPU_ADV_POWER_MGT_INFO                          = 0x80000007,
73         CPU_VIRT_PHYS_ADDR_SIZES                        = 0x80000008
74 } cpuid_t;
75
76 /**
77  * enum cpufeature
78  *
79  * This is used by cpuid_has_feature().
80  *
81  * CF_MMX:
82  *      Test for MMX support.
83  *
84  * CF_SSE*:
85  *      Test for SSE* support.
86  *
87  * CF_AVX:
88  *      Test for AVX support.
89  *
90  * CF_FMA:
91  *      Test for FMA support.
92  */
93
94 typedef enum cpufeature {
95         CF_MMX          = 1 << 23,
96         CF_SSE          = 1 << 25,
97         CF_SSE2         = 1 << 26,
98         CF_SSE3         = 1 << 0,
99
100         CF_SSSE3        = 1 << 9,
101         CF_SSE41        = 1 << 19,
102         CF_SSE42        = 1 << 20,
103
104         CF_AVX          = 1 << 28,
105         CF_FMA          = 1 << 12
106 } cpufeature_t;
107
108 /**
109  * enum cpuextfeature - Test for an extended feature provided by the CPU
110  *
111  * This is used by cpuid_has_ext_feature()
112  *
113  * CEF_x64:
114  *      Test for 64-bits.
115  *
116  * CEF_SSE4a:
117  * CEF_FMA4:
118  * CEF_XOP:
119  *      Test for SSE4a/FMA4/XOP
120  */
121 typedef enum cpuextfeature {
122         CEF_x64         = 1 << 29,
123         CEF_SSE4a       = 1 << 6,
124         CEF_FMA4        = 1 << 16,
125         CEF_XOP         = 1 << 11
126 } cpuextfeature_t;
127
128 /**
129  * enum cputype - CPU type
130  *
131  * Warning, do not change this order or odd stuff may happen.
132  */
133 typedef enum cputype {
134         CT_NONE,
135         CT_AMDK5,
136         CT_AMD,
137         CT_CENTAUR,
138         CT_CYRIX,
139         CT_INTEL,
140         CT_TRANSMETA,
141         CT_NATIONAL_SEMICONDUCTOR,
142         CT_NEXGEN,
143         CT_RISE,
144         CT_SIS,
145         CT_UMC,
146         CT_VIA,
147         CT_VORTEX,
148         CT_KVM
149 } cputype_t;
150
151 /**
152  * get_cpu_type - Get CPU Type
153  *
154  * Returns the CPU Type as cputype_t.
155  */
156 cputype_t get_cpu_type(void);
157
158 /**
159  * get_cpu_type_string - Get CPU Type string
160  *
161  * Returns the CPU type string based off cputype_t.
162  */
163 const char *get_cpu_type_string(const cputype_t cputype);
164
165 /**
166  * cpuid_is_supported - test if the CPUID instruction is supported
167  *
168  * CPUID is not supported by old CPUS.
169  *
170  * Returns 1 if the cpuid instruction is supported, 0 otherwise.
171  *
172  * See also: cpuid()
173  */
174 int cpuid_is_supported(void);
175
176 /**
177  * highest_ext_func_supported - Get the highest extended function supported
178  *
179  *
180  * Returns the highest extended function supported.
181  *
182  * This is the same as calling:
183  *      cpuid(CPU_HIGHEST_EEXTENDED_FUNCTION_SUPPORTED, &highest);
184  *
185  * This is made visible to the linker because it's easier to call it
186  * instead of calling cpuid with less type-checking.  cpuid calls this.
187  *
188  * See also: cpuid()
189  */
190 int highest_ext_func_supported(void);
191
192 /**
193  * cpuid - Get Some information from the CPU.
194  *
195  * This function expects buf to be a valid pointer to a string/int/...
196  * depending on the requested information.
197  *
198  * For CPU_VENDOR_ID:
199  *      Returns a string into buf.
200  *
201  * For CPU_PROCINFO_AND_FEATUREBITS:
202  *      buf[0]:
203  *              - 3:0 - Stepping
204  *              - 7:4 - Model
205  *              - 11:8 - Family
206  *              - 13:12 - Processor Type
207  *              - 19:16 - Extended Model
208  *              - 27:20 - Extended family
209  *      buf[1] and buf[2]:
210  *              Feature flags
211  *      buf[3]:
212  *              Additional feature information.
213  *
214  * For CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED:
215  *      Returns the highest supported function in *buf (expects an integer ofc)
216  *
217  * For CPU_EXTENDED_PROC_INFO_FEATURE_BITS:
218  *      Returns them in buf[0] and buf[1].
219  *
220  * For CPU_VIRT_PHYS_ADDR_SIZES:
221  *      Returns it as an integer in *buf.
222  *
223  * For CPU_PROC_BRAND_STRING:
224  *      Have a char array with at least 48 bytes assigned to it.
225  *
226  * If an invalid flag has been passed a 0xbaadf00d is returned in *buf.
227  */
228 void cpuid(cpuid_t info, void *buf);
229
230 /**
231  * cpuid_test_feature - Test if @feature is available
232  *
233  * Returns 1 if feature is supported, 0 otherwise.
234  *
235  * The feature parameter must be >= CPU_EXTENDED_PROC_INFO_FEATURE_BITS
236  *  and <= CPU_VIRT_PHYS_ADDR_SIZES.
237  */
238 int cpuid_test_feature(cpuid_t feature);
239
240 /**
241  * cpuid_has_feature - Test if @feature is supported
242  *
243  * Test if the CPU supports MMX/SSE* etc
244  *
245  * Returns 1 if the feature is available, 0 otherwise.
246  */
247 #define cpuid_has_mmx()         cpuid_has_feature(CF_MMX)
248 #define cpuid_has_sse()         cpuid_has_feature(CF_SSE)
249 #define cpuid_has_sse2()        cpuid_has_feature(CF_SSE2)
250 #define cpuid_has_sse3()        cpuid_has_feature(CF_SSE3)
251 #define cpuid_has_ssse3()       cpuid_has_feature(CF_SSSE3)
252 #define cpuid_has_sse41()       cpuid_has_feature(CF_SSE41)
253 #define cpuid_has_sse42()       cpuid_has_feature(CF_SSE42)
254 #define cpuid_has_avx()         cpuid_has_feature(CF_AVX)
255 #define cpuid_has_fma()         cpuid_has_feature(CF_FMA)
256 int cpuid_has_feature(cpufeature_t feature);
257
258 /**
259  * cpuid_has_ext_feature - Test if @extfeature is an extended feature
260  * that's supported by the CPU.
261  *
262  * Test if the CPU supports this extended feature.
263  *
264  * Returns 1 if available, 0 otherwise.
265  */
266 #define cpuid_has_x64()         cpuid_has_ext_feature(CEF_x64)
267 #define cpuid_has_sse4a()       cpuid_has_ext_feature(CEF_SSE4a)
268 #define cpuid_has_fma4()        cpuid_has_ext_feature(CEF_FMA4)
269 #define cpuid_has_xop()         cpuid_has_ext_feature(CEF_XOP)
270 int cpuid_has_ext_feature(cpuextfeature_t extfeature);
271
272 #endif
273