]> git.ozlabs.org Git - ccan/blob - ccan/cpuid/cpuid.h
cpuid: new module
[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 typedef enum cpuid {
26         CPU_VENDORID                                    = 0,
27         CPU_PROCINFO_AND_FEATUREBITS                    = 1,
28         CPU_CACHE_AND_TLBD_INFO                         = 2,
29
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
37 } cpuid_t;
38
39 typedef enum cpufeature {
40         CF_MMX          = 1 << 23,
41         CF_SSE          = 1 << 25,
42         CF_SSE2         = 1 << 26,
43         CF_SSE3         = 1 << 0,
44
45         CF_SSSE3        = 1 << 9,
46         CF_SSE41        = 1 << 19,
47         CF_SSE42        = 1 << 20,
48
49         CF_AVX          = 1 << 28,
50         CF_FMA          = 1 << 12
51 } cpufeature_t;
52
53 typedef enum cpuextfeature {
54         CEF_x64         = 1 << 29,
55         CEF_SSE4a       = 1 << 6,
56         CEF_FMA4        = 1 << 16,
57         CEF_XOP         = 1 << 11
58 } cpuextfeature_t;
59
60 /* returns 1 if the cpuid instruction is supported, 0 otherwise.
61  *
62  * CPUID isn't supported on very old Intel CPUs.
63  * Defined in issupprted.S
64  */
65 int cpuid_is_supported(void);
66
67 /* returns the highest extended function supported.
68  *
69  * This is the same as calling:
70  *      cpuid(CPU_HIGHEST_EEXTENDED_FUNCTION_SUPPORTED, &highest);
71  *
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.
74  */
75 int highest_ext_func_supported(void);
76
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.
80  *
81  * For CPU_VENDOR_ID:
82  *      Returns a string into buf.
83  *
84  * For CPU_PROCINFO_AND_FEATUREBITS:
85  *      buf[0]:
86  *              - 3:0 - Stepping
87  *              - 7:4 - Model
88  *              - 11:8 - Family
89  *              - 13:12 - Processor Type
90  *              - 19:16 - Extended Model
91  *              - 27:20 - Extended family
92  *      buf[1] and buf[2]:
93  *              Feature flags
94  *      buf[3]:
95  *              Additional feature information.
96  *
97  * For CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED:
98  *      Returns the highest supported function in *buf (expects an integer ofc)
99  *
100  * For CPU_EXTENDED_PROC_INFO_FEATURE_BITS:
101  *      Returns them in buf[0] and buf[1].
102  *
103  * For CPU_VIRT_PHYS_ADDR_SIZES:
104  *      Returns it as an integer in *buf.
105  *
106  * For CPU_PROC_BRAND_STRING:
107  *      Have a char array with at least 48 bytes assigned to it.
108  *
109  * If an invalid flag has been passed a 0xbaadf00d is returned in *buf.
110  */
111 void cpuid(cpuid_t info, void *buf);
112
113 /*
114  * Returns 1 if feature is supported, 0 otherwise.
115  *
116  * The feature parameter must be >= CPU_EXTENDED_PROC_INFO_FEATURE_BITS
117  *  and <= CPU_VIRT_PHYS_ADDR_SIZES.
118  */
119 int cpuid_test_feature(cpuid_t feature);
120
121 /* Test if the CPU supports MMX/SSE*
122  *
123  * Returns 1 if the feature is available, 0 otherwise.
124  */
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);
135
136 /* Test if the CPU supports an extended feature.
137  *
138  * Returns 1 if available, 0 otherwise.
139  */
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);
145
146 #endif
147