2 * Copyright (c) 2013 Ahmed Samy <f.fallen45@gmail.com>
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
29 * enum cpuid - stuff to get information on from the CPU.
31 * This is used as a parameter in cpuid().
34 * The CPU's Vendor ID.
36 * CPU_PROCINFO_AND_FEATUREBITS:
37 * Processor information and feature bits (SSE, etc.).
39 * CPU_CACHE_AND_TLBD_INFO
40 * Cache and TLBD Information.
42 * CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED:
43 * Highest extended function supported address.
44 * Can be like 0x80000008.
46 * CPU_EXTENDED_PROC_INFO_FEATURE_BITS:
47 * Extended processor information and feature bits (64bit etc.)
49 * CPU_PROC_BRAND_STRING:
50 * The Processor's brand string.
52 * CPU_L1_CACHE_AND_TLB_IDS:
53 * L1 Cache and TLB Identifications.
55 * CPU_EXTENDED_L2_CACHE_FEATURES:
56 * Extended L2 Cache features.
58 * CPU_ADV_POWER_MGT_INFO:
59 * Advaned power management information.
61 * CPU_VIRT_PHYS_ADDR_SIZES:
62 * Virtual and physical address sizes.
67 CPU_PROCINFO_AND_FEATUREBITS = 1,
68 CPU_CACHE_AND_TLBD_INFO = 2,
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
93 #define CEF_SYSCALLRET 13
99 typedef enum cputype {
107 CT_NATIONAL_SEMICONDUCTOR,
117 #if defined(__i386__) || defined(__i386) || defined(__x86_64) \
118 || defined(_M_AMD64) || defined(__M_X64)
121 * cpuid_get_cpu_type - Get CPU Type
123 * Returns the CPU Type as cputype_t.
125 * See also: cpuid_get_cpu_type_string()
127 cputype_t cpuid_get_cpu_type(void);
130 * cpuid_get_cpu_type_string - Get CPU Type string
132 * Returns the CPU type string based off cputype_t.
134 const char *cpuid_get_cpu_type_string(const cputype_t cputype);
137 * cpuid_is_supported - test if the CPUID instruction is supported
139 * CPUID is not supported by old CPUS.
141 * Returns true if the cpuid instruction is supported, false otherwise.
145 bool cpuid_is_supported(void);
148 * cpuid_highest_ext_func_supported - Get the highest extended function supported
151 * Returns the highest extended function supported.
153 * This is the same as calling:
154 * cpuid(CPU_HIGHEST_EEXTENDED_FUNCTION_SUPPORTED, &highest);
156 * This is made visible to the linker because it's easier to call it
157 * instead of calling cpuid with less type-checking. cpuid calls this.
161 uint32_t cpuid_highest_ext_func_supported(void);
164 * cpuid - Get Some information from the CPU.
166 * This function expects buf to be a valid pointer to a string/int/...
167 * depending on the requested information.
170 * Returns a string into buf.
172 * For CPU_PROCINFO_AND_FEATUREBITS:
177 * - 13:12 - Processor Type
178 * - 19:16 - Extended Model
179 * - 27:20 - Extended family
183 * Additional feature information.
185 * For CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED:
186 * Returns the highest supported function in *buf (expects an integer ofc)
188 * For CPU_EXTENDED_PROC_INFO_FEATURE_BITS:
189 * Returns them in buf[0] and buf[1].
191 * For CPU_VIRT_PHYS_ADDR_SIZES:
192 * Returns it as an integer in *buf.
194 * For CPU_PROC_BRAND_STRING:
195 * Have a char array with at least 48 bytes assigned to it.
197 * If an invalid flag has been passed a 0xbaadf00d is returned in *buf.
199 void cpuid(cpuid_t info, uint32_t *buf);
202 * cpuid_test_feature - Test if @feature is available
204 * Returns true if feature is supported, false otherwise.
206 * The feature parameter must be >= CPU_EXTENDED_PROC_INFO_FEATURE_BITS
207 * and <= CPU_VIRT_PHYS_ADDR_SIZES.
209 bool cpuid_test_feature(cpuid_t feature);
212 * cpuid_has_feature - Test if @feature is supported
214 * Test if the CPU supports MMX/SSE* etc.
215 * For the extended parameter, usually you want to pass it as
216 * false if you're not passing CEF_*.
218 * For more information about the CPU extended features, have a look
220 * http://en.wikipedia.org/wiki/CPUID
222 * Returns true if the feature is available, false otherwise.
224 #define cpuid_has_mmx() cpuid_has_feature(CF_MMX, false)
225 #define cpuid_has_sse() cpuid_has_feature(CF_SSE, false)
226 #define cpuid_has_sse2() cpuid_has_feature(CF_SSE2, false)
227 #define cpuid_has_sse3() cpuid_has_feature(CF_SSE3, false)
228 #define cpuid_has_ssse3() cpuid_has_feature(CF_SSSE3, false)
229 #define cpuid_has_avx() cpuid_has_feature(CF_AVX, false)
230 #define cpuid_has_fma() cpuid_has_feature(CF_FMA, false)
231 #define cpuid_has_x64() cpuid_has_feature(CEF_x64, true)
232 #define cpuid_has_sse4a() cpuid_has_feature(CEF_SSE4a, true)
233 #define cpuid_has_fma4() cpuid_has_feature(CEF_FMA4, true)
234 #define cpuid_has_xop() cpuid_has_feature(CEF_XOP, true)
235 bool cpuid_has_feature(int feature, bool extended);
239 #define cpuid_get_cpu_type() BUILD_ASSERT_OR_ZERO(0)
240 #define cpuid_get_cpu_type_string() BUILD_ASSERT_OR_ZERO(0)
242 #define cpuid_is_supported() BUILD_ASSERT_OR_ZERO(0)
243 #define cpuid(info, buf) BUILD_ASSERT_OR_ZERO(0)
245 #define cpuid_highest_ext_func_supported() BUILD_ASSERT_OR_ZERO(0)
246 #define cpuid_test_feature(feature) BUILD_ASSERT_OR_ZERO(0)
247 #define cpuid_has_feature(feature, ext) BUILD_ASSERT_OR_ZERO(0)