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
27 CPU_PROCINFO_AND_FEATUREBITS = 1,
28 CPU_CACHE_AND_TLBD_INFO = 2,
30 CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED = 0x80000000,
31 CPU_EXTENDED_PROC_INFO_FEATURE_BITS = 0x80000001,
32 CPU_PROC_BRAND_STRING = 0x80000002,
33 CPU_L1_CACHE_AND_TLB_IDS = 0x80000005,
34 CPU_EXTENDED_L2_CACHE_FEATURES = 0x80000006,
35 CPU_ADV_POWER_MGT_INFO = 0x80000007,
36 CPU_VIRT_PHYS_ADDR_SIZES = 0x80000008
39 typedef enum cpufeature {
53 typedef enum cpuextfeature {
60 /* returns 1 if the cpuid instruction is supported, 0 otherwise.
62 * CPUID isn't supported on very old Intel CPUs.
63 * Defined in issupprted.S
65 int cpuid_is_supported(void);
67 /* returns the highest extended function supported.
69 * This is the same as calling:
70 * cpuid(CPU_HIGHEST_EEXTENDED_FUNCTION_SUPPORTED, &highest);
72 * This is made visible to the linker because it's easier to call it
73 * instead of calling cpuid with less type-checking. cpuid calls this.
75 int highest_ext_func_supported(void);
77 /* Get Some information from the CPU.
78 * This function expects buf to be a valid pointer to a string/int/...
79 * depending on the requested information.
82 * Returns a string into buf.
84 * For CPU_PROCINFO_AND_FEATUREBITS:
89 * - 13:12 - Processor Type
90 * - 19:16 - Extended Model
91 * - 27:20 - Extended family
95 * Additional feature information.
97 * For CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED:
98 * Returns the highest supported function in *buf (expects an integer ofc)
100 * For CPU_EXTENDED_PROC_INFO_FEATURE_BITS:
101 * Returns them in buf[0] and buf[1].
103 * For CPU_VIRT_PHYS_ADDR_SIZES:
104 * Returns it as an integer in *buf.
106 * For CPU_PROC_BRAND_STRING:
107 * Have a char array with at least 48 bytes assigned to it.
109 * If an invalid flag has been passed a 0xbaadf00d is returned in *buf.
111 void cpuid(cpuid_t info, void *buf);
114 * Returns 1 if feature is supported, 0 otherwise.
116 * The feature parameter must be >= CPU_EXTENDED_PROC_INFO_FEATURE_BITS
117 * and <= CPU_VIRT_PHYS_ADDR_SIZES.
119 int cpuid_test_feature(cpuid_t feature);
121 /* Test if the CPU supports MMX/SSE*
123 * Returns 1 if the feature is available, 0 otherwise.
125 #define cpuid_has_mmx() cpuid_has_feature(CF_MMX)
126 #define cpuid_has_sse() cpuid_has_feature(CF_SSE)
127 #define cpuid_has_sse2() cpuid_has_feature(CF_SSE2)
128 #define cpuid_has_sse3() cpuid_has_feature(CF_SSE3)
129 #define cpuid_has_ssse3() cpuid_has_feature(CF_SSSE3)
130 #define cpuid_has_sse41() cpuid_has_feature(CF_SSE41)
131 #define cpuid_has_sse42() cpuid_has_feature(CF_SSE42)
132 #define cpuid_has_avx() cpuid_has_feature(CF_AVX)
133 #define cpuid_has_fma() cpuid_has_feature(CF_FMA)
134 int cpuid_has_feature(cpufeature_t feature);
136 /* Test if the CPU supports an extended feature.
138 * Returns 1 if available, 0 otherwise.
140 #define cpuid_has_x64() cpuid_has_ext_feature(CEF_x64)
141 #define cpuid_has_sse4a() cpuid_has_ext_feature(CEF_SSE4a)
142 #define cpuid_has_fma4() cpuid_has_ext_feature(CEF_FMA4)
143 #define cpuid_has_xop() cpuid_has_ext_feature(CEF_XOP)
144 int cpuid_has_ext_feature(cpuextfeature_t extfeature);