]> git.ozlabs.org Git - ccan/blob - ccan/cpuid/cpuid.h
2861394608313dede06e863900189af6dd8dafef
[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 #define CF_MMX          0
77 #define CF_SSE          1
78 #define CF_SSE2         2
79 #define CF_SSE3         3
80 #define CF_FPU          4
81 #define CF_TSC          5
82 #define CF_MSR          6
83 #define CF_SSSE3        7
84 #define CF_AVX          8
85 #define CF_FMA          9
86
87 #define CEF_x64         10
88 #define CEF_FPU         11
89 #define CEF_DE          12
90 #define CEF_SYSCALLRET  13
91 #define CEF_CMOV        14
92 #define CEF_SSE4a       15
93 #define CEF_FMA4        16
94 #define CEF_XOP         17
95
96 typedef enum cputype {
97         CT_NONE,
98         CT_AMDK5,
99         CT_AMD,
100         CT_CENTAUR,
101         CT_CYRIX,
102         CT_INTEL,
103         CT_TRANSMETA,
104         CT_NATIONAL_SEMICONDUCTOR,
105         CT_NEXGEN,
106         CT_RISE,
107         CT_SIS,
108         CT_UMC,
109         CT_VIA,
110         CT_VORTEX,
111         CT_KVM
112 } cputype_t;
113
114 /**
115  * cpuid_get_cpu_type - Get CPU Type
116  *
117  * Returns the CPU Type as cputype_t.
118  *
119  * See also: cpuid_get_cpu_type_string()
120  */
121 cputype_t cpuid_get_cpu_type(void);
122
123 /**
124  * cpuid_get_cpu_type_string - Get CPU Type string
125  *
126  * Returns the CPU type string based off cputype_t.
127  */
128 const char *cpuid_get_cpu_type_string(const cputype_t cputype);
129
130 /**
131  * cpuid_is_supported - test if the CPUID instruction is supported
132  *
133  * CPUID is not supported by old CPUS.
134  *
135  * Returns 1 if the cpuid instruction is supported, 0 otherwise.
136  *
137  * See also: cpuid()
138  */
139 int cpuid_is_supported(void);
140
141 /**
142  * cpuid_highest_ext_func_supported - Get the highest extended function supported
143  *
144  *
145  * Returns the highest extended function supported.
146  *
147  * This is the same as calling:
148  *      cpuid(CPU_HIGHEST_EEXTENDED_FUNCTION_SUPPORTED, &highest);
149  *
150  * This is made visible to the linker because it's easier to call it
151  * instead of calling cpuid with less type-checking.  cpuid calls this.
152  *
153  * See also: cpuid()
154  */
155 int cpuid_highest_ext_func_supported(void);
156
157 /**
158  * cpuid - Get Some information from the CPU.
159  *
160  * This function expects buf to be a valid pointer to a string/int/...
161  * depending on the requested information.
162  *
163  * For CPU_VENDOR_ID:
164  *      Returns a string into buf.
165  *
166  * For CPU_PROCINFO_AND_FEATUREBITS:
167  *      buf[0]:
168  *              - 3:0 - Stepping
169  *              - 7:4 - Model
170  *              - 11:8 - Family
171  *              - 13:12 - Processor Type
172  *              - 19:16 - Extended Model
173  *              - 27:20 - Extended family
174  *      buf[1] and buf[2]:
175  *              Feature flags
176  *      buf[3]:
177  *              Additional feature information.
178  *
179  * For CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED:
180  *      Returns the highest supported function in *buf (expects an integer ofc)
181  *
182  * For CPU_EXTENDED_PROC_INFO_FEATURE_BITS:
183  *      Returns them in buf[0] and buf[1].
184  *
185  * For CPU_VIRT_PHYS_ADDR_SIZES:
186  *      Returns it as an integer in *buf.
187  *
188  * For CPU_PROC_BRAND_STRING:
189  *      Have a char array with at least 48 bytes assigned to it.
190  *
191  * If an invalid flag has been passed a 0xbaadf00d is returned in *buf.
192  */
193 void cpuid(cpuid_t info, void *buf);
194
195 /**
196  * cpuid_test_feature - Test if @feature is available
197  *
198  * Returns 1 if feature is supported, 0 otherwise.
199  *
200  * The feature parameter must be >= CPU_EXTENDED_PROC_INFO_FEATURE_BITS
201  *  and <= CPU_VIRT_PHYS_ADDR_SIZES.
202  */
203 int cpuid_test_feature(cpuid_t feature);
204
205 /**
206  * cpuid_has_feature - Test if @feature is supported
207  *
208  * Test if the CPU supports MMX/SSE* etc.
209  * For the extended parameter, usually you want to pass it as
210  * 0 if you're not passing CEF_*.
211  *
212  * For more information about the CPU extended features, have a look
213  * at:
214  *      http://en.wikipedia.org/wiki/CPUID
215  *
216  * Returns 1 if the feature is available, 0 otherwise.
217  */
218 #define cpuid_has_mmx()         cpuid_has_feature(CF_MMX,       0)
219 #define cpuid_has_sse()         cpuid_has_feature(CF_SSE,       0)
220 #define cpuid_has_sse2()        cpuid_has_feature(CF_SSE2,      0)
221 #define cpuid_has_sse3()        cpuid_has_feature(CF_SSE3,      0)
222 #define cpuid_has_ssse3()       cpuid_has_feature(CF_SSSE3,     0)
223 #define cpuid_has_avx()         cpuid_has_feature(CF_AVX,       0)
224 #define cpuid_has_fma()         cpuid_has_feature(CF_FMA,       0)
225 #define cpuid_has_x64()         cpuid_has_feature(CEF_x64,      1)
226 #define cpuid_has_sse4a()       cpuid_has_feature(CEF_SSE4a,    1)
227 #define cpuid_has_fma4()        cpuid_has_feature(CEF_FMA4,     1)
228 #define cpuid_has_xop()         cpuid_has_feature(CEF_XOP,      1)
229 int cpuid_has_feature(int feature, int extended);
230
231 #endif
232