Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2015 Intel Corporation
3 : : */
4 : :
5 : : #include <eal_export.h>
6 : : #include "rte_cpuflags.h"
7 : :
8 : : #include <stdio.h>
9 : : #include <errno.h>
10 : : #include <stdint.h>
11 : : #include <string.h>
12 : : #include <stdbool.h>
13 : :
14 : : #include "rte_cpuid.h"
15 : : #include "rte_atomic.h"
16 : :
17 : : /**
18 : : * Struct to hold a processor feature entry
19 : : */
20 : : struct feature_entry {
21 : : uint32_t leaf; /**< cpuid leaf */
22 : : uint32_t subleaf; /**< cpuid subleaf */
23 : : uint32_t reg; /**< cpuid register */
24 : : uint32_t bit; /**< cpuid register bit */
25 : : #define CPU_FLAG_NAME_MAX_LEN 64
26 : : char name[CPU_FLAG_NAME_MAX_LEN]; /**< String for printing */
27 : : bool has_value;
28 : : bool value;
29 : : };
30 : :
31 : : #define FEAT_DEF(name, leaf, subleaf, reg, bit) \
32 : : [RTE_CPUFLAG_##name] = {leaf, subleaf, reg, bit, #name },
33 : :
34 : : struct feature_entry rte_cpu_feature_table[] = {
35 : : FEAT_DEF(SSE3, 0x00000001, 0, RTE_REG_ECX, 0)
36 : : FEAT_DEF(PCLMULQDQ, 0x00000001, 0, RTE_REG_ECX, 1)
37 : : FEAT_DEF(DTES64, 0x00000001, 0, RTE_REG_ECX, 2)
38 : : FEAT_DEF(MONITOR, 0x00000001, 0, RTE_REG_ECX, 3)
39 : : FEAT_DEF(DS_CPL, 0x00000001, 0, RTE_REG_ECX, 4)
40 : : FEAT_DEF(VMX, 0x00000001, 0, RTE_REG_ECX, 5)
41 : : FEAT_DEF(SMX, 0x00000001, 0, RTE_REG_ECX, 6)
42 : : FEAT_DEF(EIST, 0x00000001, 0, RTE_REG_ECX, 7)
43 : : FEAT_DEF(TM2, 0x00000001, 0, RTE_REG_ECX, 8)
44 : : FEAT_DEF(SSSE3, 0x00000001, 0, RTE_REG_ECX, 9)
45 : : FEAT_DEF(CNXT_ID, 0x00000001, 0, RTE_REG_ECX, 10)
46 : : FEAT_DEF(FMA, 0x00000001, 0, RTE_REG_ECX, 12)
47 : : FEAT_DEF(CMPXCHG16B, 0x00000001, 0, RTE_REG_ECX, 13)
48 : : FEAT_DEF(XTPR, 0x00000001, 0, RTE_REG_ECX, 14)
49 : : FEAT_DEF(PDCM, 0x00000001, 0, RTE_REG_ECX, 15)
50 : : FEAT_DEF(PCID, 0x00000001, 0, RTE_REG_ECX, 17)
51 : : FEAT_DEF(DCA, 0x00000001, 0, RTE_REG_ECX, 18)
52 : : FEAT_DEF(SSE4_1, 0x00000001, 0, RTE_REG_ECX, 19)
53 : : FEAT_DEF(SSE4_2, 0x00000001, 0, RTE_REG_ECX, 20)
54 : : FEAT_DEF(X2APIC, 0x00000001, 0, RTE_REG_ECX, 21)
55 : : FEAT_DEF(MOVBE, 0x00000001, 0, RTE_REG_ECX, 22)
56 : : FEAT_DEF(POPCNT, 0x00000001, 0, RTE_REG_ECX, 23)
57 : : FEAT_DEF(TSC_DEADLINE, 0x00000001, 0, RTE_REG_ECX, 24)
58 : : FEAT_DEF(AES, 0x00000001, 0, RTE_REG_ECX, 25)
59 : : FEAT_DEF(XSAVE, 0x00000001, 0, RTE_REG_ECX, 26)
60 : : FEAT_DEF(OSXSAVE, 0x00000001, 0, RTE_REG_ECX, 27)
61 : : FEAT_DEF(AVX, 0x00000001, 0, RTE_REG_ECX, 28)
62 : : FEAT_DEF(F16C, 0x00000001, 0, RTE_REG_ECX, 29)
63 : : FEAT_DEF(RDRAND, 0x00000001, 0, RTE_REG_ECX, 30)
64 : : FEAT_DEF(HYPERVISOR, 0x00000001, 0, RTE_REG_ECX, 31)
65 : :
66 : : FEAT_DEF(FPU, 0x00000001, 0, RTE_REG_EDX, 0)
67 : : FEAT_DEF(VME, 0x00000001, 0, RTE_REG_EDX, 1)
68 : : FEAT_DEF(DE, 0x00000001, 0, RTE_REG_EDX, 2)
69 : : FEAT_DEF(PSE, 0x00000001, 0, RTE_REG_EDX, 3)
70 : : FEAT_DEF(TSC, 0x00000001, 0, RTE_REG_EDX, 4)
71 : : FEAT_DEF(MSR, 0x00000001, 0, RTE_REG_EDX, 5)
72 : : FEAT_DEF(PAE, 0x00000001, 0, RTE_REG_EDX, 6)
73 : : FEAT_DEF(MCE, 0x00000001, 0, RTE_REG_EDX, 7)
74 : : FEAT_DEF(CX8, 0x00000001, 0, RTE_REG_EDX, 8)
75 : : FEAT_DEF(APIC, 0x00000001, 0, RTE_REG_EDX, 9)
76 : : FEAT_DEF(SEP, 0x00000001, 0, RTE_REG_EDX, 11)
77 : : FEAT_DEF(MTRR, 0x00000001, 0, RTE_REG_EDX, 12)
78 : : FEAT_DEF(PGE, 0x00000001, 0, RTE_REG_EDX, 13)
79 : : FEAT_DEF(MCA, 0x00000001, 0, RTE_REG_EDX, 14)
80 : : FEAT_DEF(CMOV, 0x00000001, 0, RTE_REG_EDX, 15)
81 : : FEAT_DEF(PAT, 0x00000001, 0, RTE_REG_EDX, 16)
82 : : FEAT_DEF(PSE36, 0x00000001, 0, RTE_REG_EDX, 17)
83 : : FEAT_DEF(PSN, 0x00000001, 0, RTE_REG_EDX, 18)
84 : : FEAT_DEF(CLFSH, 0x00000001, 0, RTE_REG_EDX, 19)
85 : : FEAT_DEF(DS, 0x00000001, 0, RTE_REG_EDX, 21)
86 : : FEAT_DEF(ACPI, 0x00000001, 0, RTE_REG_EDX, 22)
87 : : FEAT_DEF(MMX, 0x00000001, 0, RTE_REG_EDX, 23)
88 : : FEAT_DEF(FXSR, 0x00000001, 0, RTE_REG_EDX, 24)
89 : : FEAT_DEF(SSE, 0x00000001, 0, RTE_REG_EDX, 25)
90 : : FEAT_DEF(SSE2, 0x00000001, 0, RTE_REG_EDX, 26)
91 : : FEAT_DEF(SS, 0x00000001, 0, RTE_REG_EDX, 27)
92 : : FEAT_DEF(HTT, 0x00000001, 0, RTE_REG_EDX, 28)
93 : : FEAT_DEF(TM, 0x00000001, 0, RTE_REG_EDX, 29)
94 : : FEAT_DEF(PBE, 0x00000001, 0, RTE_REG_EDX, 31)
95 : :
96 : : FEAT_DEF(DIGTEMP, 0x00000006, 0, RTE_REG_EAX, 0)
97 : : FEAT_DEF(TRBOBST, 0x00000006, 0, RTE_REG_EAX, 1)
98 : : FEAT_DEF(ARAT, 0x00000006, 0, RTE_REG_EAX, 2)
99 : : FEAT_DEF(PLN, 0x00000006, 0, RTE_REG_EAX, 4)
100 : : FEAT_DEF(ECMD, 0x00000006, 0, RTE_REG_EAX, 5)
101 : : FEAT_DEF(PTM, 0x00000006, 0, RTE_REG_EAX, 6)
102 : :
103 : : FEAT_DEF(MPERF_APERF_MSR, 0x00000006, 0, RTE_REG_ECX, 0)
104 : : FEAT_DEF(ACNT2, 0x00000006, 0, RTE_REG_ECX, 1)
105 : : FEAT_DEF(ENERGY_EFF, 0x00000006, 0, RTE_REG_ECX, 3)
106 : :
107 : : FEAT_DEF(FSGSBASE, 0x00000007, 0, RTE_REG_EBX, 0)
108 : : FEAT_DEF(BMI1, 0x00000007, 0, RTE_REG_EBX, 3)
109 : : FEAT_DEF(HLE, 0x00000007, 0, RTE_REG_EBX, 4)
110 : : FEAT_DEF(AVX2, 0x00000007, 0, RTE_REG_EBX, 5)
111 : : FEAT_DEF(SMEP, 0x00000007, 0, RTE_REG_EBX, 7)
112 : : FEAT_DEF(BMI2, 0x00000007, 0, RTE_REG_EBX, 8)
113 : : FEAT_DEF(ERMS, 0x00000007, 0, RTE_REG_EBX, 9)
114 : : FEAT_DEF(INVPCID, 0x00000007, 0, RTE_REG_EBX, 10)
115 : : FEAT_DEF(RTM, 0x00000007, 0, RTE_REG_EBX, 11)
116 : : FEAT_DEF(AVX512F, 0x00000007, 0, RTE_REG_EBX, 16)
117 : : FEAT_DEF(AVX512DQ, 0x00000007, 0, RTE_REG_EBX, 17)
118 : : FEAT_DEF(RDSEED, 0x00000007, 0, RTE_REG_EBX, 18)
119 : : FEAT_DEF(AVX512IFMA, 0x00000007, 0, RTE_REG_EBX, 21)
120 : : FEAT_DEF(AVX512CD, 0x00000007, 0, RTE_REG_EBX, 28)
121 : : FEAT_DEF(AVX512BW, 0x00000007, 0, RTE_REG_EBX, 30)
122 : : FEAT_DEF(AVX512VL, 0x00000007, 0, RTE_REG_EBX, 31)
123 : :
124 : : FEAT_DEF(AVX512VBMI, 0x00000007, 0, RTE_REG_ECX, 1)
125 : : FEAT_DEF(WAITPKG, 0x00000007, 0, RTE_REG_ECX, 5)
126 : : FEAT_DEF(AVX512VBMI2, 0x00000007, 0, RTE_REG_ECX, 6)
127 : : FEAT_DEF(GFNI, 0x00000007, 0, RTE_REG_ECX, 8)
128 : : FEAT_DEF(VAES, 0x00000007, 0, RTE_REG_ECX, 9)
129 : : FEAT_DEF(VPCLMULQDQ, 0x00000007, 0, RTE_REG_ECX, 10)
130 : : FEAT_DEF(AVX512VNNI, 0x00000007, 0, RTE_REG_ECX, 11)
131 : : FEAT_DEF(AVX512BITALG, 0x00000007, 0, RTE_REG_ECX, 12)
132 : : FEAT_DEF(AVX512VPOPCNTDQ, 0x00000007, 0, RTE_REG_ECX, 14)
133 : : FEAT_DEF(CLDEMOTE, 0x00000007, 0, RTE_REG_ECX, 25)
134 : : FEAT_DEF(MOVDIRI, 0x00000007, 0, RTE_REG_ECX, 27)
135 : : FEAT_DEF(MOVDIR64B, 0x00000007, 0, RTE_REG_ECX, 28)
136 : :
137 : : FEAT_DEF(AVX512VP2INTERSECT, 0x00000007, 0, RTE_REG_EDX, 8)
138 : :
139 : : FEAT_DEF(LAHF_SAHF, 0x80000001, 0, RTE_REG_ECX, 0)
140 : : FEAT_DEF(LZCNT, 0x80000001, 0, RTE_REG_ECX, 4)
141 : : FEAT_DEF(MONITORX, 0x80000001, 0, RTE_REG_ECX, 29)
142 : :
143 : : FEAT_DEF(SYSCALL, 0x80000001, 0, RTE_REG_EDX, 11)
144 : : FEAT_DEF(XD, 0x80000001, 0, RTE_REG_EDX, 20)
145 : : FEAT_DEF(1GB_PG, 0x80000001, 0, RTE_REG_EDX, 26)
146 : : FEAT_DEF(RDTSCP, 0x80000001, 0, RTE_REG_EDX, 27)
147 : : FEAT_DEF(EM64T, 0x80000001, 0, RTE_REG_EDX, 29)
148 : :
149 : : FEAT_DEF(INVTSC, 0x80000007, 0, RTE_REG_EDX, 8)
150 : : };
151 : :
152 : : RTE_EXPORT_SYMBOL(rte_cpu_get_flag_enabled)
153 : : int
154 : 253636 : rte_cpu_get_flag_enabled(enum rte_cpu_flag_t feature)
155 : : {
156 : : struct feature_entry *feat;
157 : : cpuid_registers_t regs;
158 : : unsigned int maxleaf;
159 : :
160 [ + - ]: 253636 : if ((unsigned int)feature >= RTE_DIM(rte_cpu_feature_table))
161 : : /* Flag does not match anything in the feature tables */
162 : : return -ENOENT;
163 : :
164 : : feat = &rte_cpu_feature_table[feature];
165 [ + + ]: 253636 : if (feat->has_value)
166 : 247871 : return feat->value;
167 : :
168 [ + - ]: 5765 : if (!feat->leaf)
169 : : /* This entry in the table wasn't filled out! */
170 : : return -EFAULT;
171 : :
172 : 5765 : maxleaf = __get_cpuid_max(feat->leaf & 0x80000000, NULL);
173 : :
174 [ - + ]: 5765 : if (maxleaf < feat->leaf) {
175 : 0 : feat->value = 0;
176 : 0 : goto out;
177 : : }
178 : :
179 : : #ifdef RTE_TOOLCHAIN_MSVC
180 : : __cpuidex(regs, feat->leaf, feat->subleaf);
181 : : #else
182 : 5765 : __cpuid_count(feat->leaf, feat->subleaf,
183 : : regs[RTE_REG_EAX], regs[RTE_REG_EBX],
184 : : regs[RTE_REG_ECX], regs[RTE_REG_EDX]);
185 : : #endif
186 : :
187 : : /* check if the feature is enabled */
188 : 5765 : feat->value = (regs[feat->reg] >> feat->bit) & 1;
189 : 5765 : out:
190 : 5765 : rte_compiler_barrier();
191 : 5765 : feat->has_value = true;
192 : 5765 : return feat->value;
193 : : }
194 : :
195 : : RTE_EXPORT_SYMBOL(rte_cpu_get_flag_name)
196 : : const char *
197 : 0 : rte_cpu_get_flag_name(enum rte_cpu_flag_t feature)
198 : : {
199 [ # # ]: 0 : if ((unsigned int)feature >= RTE_DIM(rte_cpu_feature_table))
200 : : return NULL;
201 : 0 : return rte_cpu_feature_table[feature].name;
202 : : }
203 : :
204 : : RTE_EXPORT_SYMBOL(rte_cpu_get_intrinsics_support)
205 : : void
206 : 252 : rte_cpu_get_intrinsics_support(struct rte_cpu_intrinsics *intrinsics)
207 : : {
208 : : memset(intrinsics, 0, sizeof(*intrinsics));
209 : :
210 [ - + ]: 252 : if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_WAITPKG)) {
211 : 0 : intrinsics->power_monitor = 1;
212 : 0 : intrinsics->power_pause = 1;
213 [ # # ]: 0 : if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_RTM))
214 : 0 : intrinsics->power_monitor_multi = 1;
215 [ - + ]: 252 : } else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_MONITORX)) {
216 : 0 : intrinsics->power_monitor = 1;
217 : : }
218 : 252 : }
|