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 about 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 #define is_intel_cpu() cpuid_get_cpu_type() == CT_INTEL
128 #define is_amd_cpu() cpuid_get_cpu_type() == CT_AMDK5 || cpuid_get_cpu_type() == CT_AMD
129 cputype_t cpuid_get_cpu_type(void);
132 * cpuid_sprintf_cputype - Get CPU Type string
133 * @cputype: a char of atleast 12 bytes in it.
135 * Returns true on success, false on failure
137 bool cpuid_sprintf_cputype(const cputype_t cputype, char *buf);
140 * cpuid_is_supported - test if the CPUID instruction is supported
142 * CPUID is not supported by old CPUS.
144 * Returns true if the cpuid instruction is supported, false otherwise.
148 bool cpuid_is_supported(void);
151 * cpuid_highest_ext_func_supported - Get the highest extended function supported
154 * Returns the highest extended function supported.
156 * This is the same as calling:
157 * cpuid(CPU_HIGHEST_EEXTENDED_FUNCTION_SUPPORTED, &highest);
159 * This is made visible to the linker because it's easier to call it
160 * instead of calling cpuid with less type-checking. cpuid calls this.
164 uint32_t cpuid_highest_ext_func_supported(void);
167 * cpuid - Get Some information from the CPU.
169 * This function expects buf to be a valid pointer to a string/int/...
170 * depending on the requested information.
173 * Returns a string into buf.
175 * For CPU_PROCINFO_AND_FEATUREBITS:
180 * - 13:12 - Processor Type
181 * - 19:16 - Extended Model
182 * - 27:20 - Extended family
186 * Additional feature information.
188 * For CPU_L1_CACHE_AND_TLB_IDS:
190 * - 7..0 Number of times to exec cpuid to get all descriptors.
191 * - 15..8 Instruction TLB: 4K Pages, 4-way set associtive, 128 entries.
192 * - 23..16 Data TLB: 4k Pages, 4-way set associtive, 128 entries.
193 * - 24..31 Instruction TLB: 4K Pages, 4-way set associtive, 2 entries.
195 * - 7..0 64-byte prefetching
196 * - 8..31 Null descriptor
198 * - 0..31 Null descriptor
200 * - 7..0 2nd-level cache, 2M, 8-way set associtive, 64-byte line size
201 * - 15..8 1st-level instruction cache: 32K, 8-way set associtive, 64 byte line size
202 * - 16..23 Data TLB: 4M Pages, 4-way set associtive, 8 entires.
203 * - 24..31 1st-level data cache: 32K, 8-way set associtive, 64 byte line size
205 * For CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED:
206 * Returns the highest supported function in *buf (expects an integer ofc)
208 * For CPU_EXTENDED_PROC_INFO_FEATURE_BITS:
209 * Returns them in buf[0] and buf[1].
211 * For CPU_EXTENDED_L2_CACHE_FEATURES:
213 * buf[1]: Associativity
214 * buf[2]: Cache size.
216 * For CPU_VIRT_PHYS_ADDR_SIZES:
220 * For CPU_PROC_BRAND_STRING:
221 * Have a char array with at least 48 bytes assigned to it.
223 * Here's a page which will help you parse the data provided by this function.
224 * http://www.flounder.com/cpuid_explorer2.htm
226 * If an invalid flag has been passed a 0xbaadf00d is returned in *buf.
228 void cpuid(cpuid_t info, uint32_t *buf);
231 * cpuid_test_feature - Test if @feature is available
233 * Returns true if feature is supported, false otherwise.
235 * The feature parameter must be >= CPU_EXTENDED_PROC_INFO_FEATURE_BITS
236 * and <= CPU_VIRT_PHYS_ADDR_SIZES.
238 bool cpuid_test_feature(cpuid_t feature);
241 * cpuid_has_feature - Test if @feature is supported
243 * Test if the CPU supports MMX/SSE* etc.
244 * For the extended parameter, usually you want to pass it as
245 * false if you're not passing CEF_*.
247 * For more information about the CPU extended features, have a look
249 * http://en.wikipedia.org/wiki/CPUID
251 * Returns true if the feature is available, false otherwise.
253 #define cpuid_has_mmx() cpuid_has_feature(CF_MMX, false)
254 #define cpuid_has_sse() cpuid_has_feature(CF_SSE, false)
255 #define cpuid_has_sse2() cpuid_has_feature(CF_SSE2, false)
256 #define cpuid_has_sse3() cpuid_has_feature(CF_SSE3, false)
257 #define cpuid_has_ssse3() cpuid_has_feature(CF_SSSE3, false)
258 #define cpuid_has_avx() cpuid_has_feature(CF_AVX, false)
259 #define cpuid_has_fma() cpuid_has_feature(CF_FMA, false)
260 #define cpuid_has_x64() cpuid_has_feature(CEF_x64, true)
261 #define cpuid_has_sse4a() cpuid_has_feature(CEF_SSE4a, true)
262 #define cpuid_has_fma4() cpuid_has_feature(CEF_FMA4, true)
263 #define cpuid_has_xop() cpuid_has_feature(CEF_XOP, true)
264 bool cpuid_has_feature(int feature, bool extended);
267 #include <ccan/build_assert/build_assert.h>
269 #define cpuid_get_cpu_type() BUILD_ASSERT_OR_ZERO(0)
270 #define cpuid_get_cpu_type_string() BUILD_ASSERT_OR_ZERO(0)
272 #define cpuid_is_supported() BUILD_ASSERT_OR_ZERO(0)
273 #define cpuid(info, buf) BUILD_ASSERT_OR_ZERO(0)
275 #define cpuid_highest_ext_func_supported() BUILD_ASSERT_OR_ZERO(0)
276 #define cpuid_test_feature(feature) BUILD_ASSERT_OR_ZERO(0)
277 #define cpuid_has_feature(feature, ext) BUILD_ASSERT_OR_ZERO(0)