New upstream version 18.02
[deb_dpdk.git] / lib / librte_eal / common / arch / x86 / rte_cpuflags.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2015 Intel Corporation
3  */
4
5 #include "rte_cpuflags.h"
6
7 #include <stdio.h>
8 #include <errno.h>
9 #include <stdint.h>
10
11 #include "rte_cpuid.h"
12
13 /**
14  * Struct to hold a processor feature entry
15  */
16 struct feature_entry {
17         uint32_t leaf;                          /**< cpuid leaf */
18         uint32_t subleaf;                       /**< cpuid subleaf */
19         uint32_t reg;                           /**< cpuid register */
20         uint32_t bit;                           /**< cpuid register bit */
21 #define CPU_FLAG_NAME_MAX_LEN 64
22         char name[CPU_FLAG_NAME_MAX_LEN];       /**< String for printing */
23 };
24
25 #define FEAT_DEF(name, leaf, subleaf, reg, bit) \
26         [RTE_CPUFLAG_##name] = {leaf, subleaf, reg, bit, #name },
27
28 const struct feature_entry rte_cpu_feature_table[] = {
29         FEAT_DEF(SSE3, 0x00000001, 0, RTE_REG_ECX,  0)
30         FEAT_DEF(PCLMULQDQ, 0x00000001, 0, RTE_REG_ECX,  1)
31         FEAT_DEF(DTES64, 0x00000001, 0, RTE_REG_ECX,  2)
32         FEAT_DEF(MONITOR, 0x00000001, 0, RTE_REG_ECX,  3)
33         FEAT_DEF(DS_CPL, 0x00000001, 0, RTE_REG_ECX,  4)
34         FEAT_DEF(VMX, 0x00000001, 0, RTE_REG_ECX,  5)
35         FEAT_DEF(SMX, 0x00000001, 0, RTE_REG_ECX,  6)
36         FEAT_DEF(EIST, 0x00000001, 0, RTE_REG_ECX,  7)
37         FEAT_DEF(TM2, 0x00000001, 0, RTE_REG_ECX,  8)
38         FEAT_DEF(SSSE3, 0x00000001, 0, RTE_REG_ECX,  9)
39         FEAT_DEF(CNXT_ID, 0x00000001, 0, RTE_REG_ECX, 10)
40         FEAT_DEF(FMA, 0x00000001, 0, RTE_REG_ECX, 12)
41         FEAT_DEF(CMPXCHG16B, 0x00000001, 0, RTE_REG_ECX, 13)
42         FEAT_DEF(XTPR, 0x00000001, 0, RTE_REG_ECX, 14)
43         FEAT_DEF(PDCM, 0x00000001, 0, RTE_REG_ECX, 15)
44         FEAT_DEF(PCID, 0x00000001, 0, RTE_REG_ECX, 17)
45         FEAT_DEF(DCA, 0x00000001, 0, RTE_REG_ECX, 18)
46         FEAT_DEF(SSE4_1, 0x00000001, 0, RTE_REG_ECX, 19)
47         FEAT_DEF(SSE4_2, 0x00000001, 0, RTE_REG_ECX, 20)
48         FEAT_DEF(X2APIC, 0x00000001, 0, RTE_REG_ECX, 21)
49         FEAT_DEF(MOVBE, 0x00000001, 0, RTE_REG_ECX, 22)
50         FEAT_DEF(POPCNT, 0x00000001, 0, RTE_REG_ECX, 23)
51         FEAT_DEF(TSC_DEADLINE, 0x00000001, 0, RTE_REG_ECX, 24)
52         FEAT_DEF(AES, 0x00000001, 0, RTE_REG_ECX, 25)
53         FEAT_DEF(XSAVE, 0x00000001, 0, RTE_REG_ECX, 26)
54         FEAT_DEF(OSXSAVE, 0x00000001, 0, RTE_REG_ECX, 27)
55         FEAT_DEF(AVX, 0x00000001, 0, RTE_REG_ECX, 28)
56         FEAT_DEF(F16C, 0x00000001, 0, RTE_REG_ECX, 29)
57         FEAT_DEF(RDRAND, 0x00000001, 0, RTE_REG_ECX, 30)
58         FEAT_DEF(HYPERVISOR, 0x00000001, 0, RTE_REG_ECX, 31)
59
60         FEAT_DEF(FPU, 0x00000001, 0, RTE_REG_EDX,  0)
61         FEAT_DEF(VME, 0x00000001, 0, RTE_REG_EDX,  1)
62         FEAT_DEF(DE, 0x00000001, 0, RTE_REG_EDX,  2)
63         FEAT_DEF(PSE, 0x00000001, 0, RTE_REG_EDX,  3)
64         FEAT_DEF(TSC, 0x00000001, 0, RTE_REG_EDX,  4)
65         FEAT_DEF(MSR, 0x00000001, 0, RTE_REG_EDX,  5)
66         FEAT_DEF(PAE, 0x00000001, 0, RTE_REG_EDX,  6)
67         FEAT_DEF(MCE, 0x00000001, 0, RTE_REG_EDX,  7)
68         FEAT_DEF(CX8, 0x00000001, 0, RTE_REG_EDX,  8)
69         FEAT_DEF(APIC, 0x00000001, 0, RTE_REG_EDX,  9)
70         FEAT_DEF(SEP, 0x00000001, 0, RTE_REG_EDX, 11)
71         FEAT_DEF(MTRR, 0x00000001, 0, RTE_REG_EDX, 12)
72         FEAT_DEF(PGE, 0x00000001, 0, RTE_REG_EDX, 13)
73         FEAT_DEF(MCA, 0x00000001, 0, RTE_REG_EDX, 14)
74         FEAT_DEF(CMOV, 0x00000001, 0, RTE_REG_EDX, 15)
75         FEAT_DEF(PAT, 0x00000001, 0, RTE_REG_EDX, 16)
76         FEAT_DEF(PSE36, 0x00000001, 0, RTE_REG_EDX, 17)
77         FEAT_DEF(PSN, 0x00000001, 0, RTE_REG_EDX, 18)
78         FEAT_DEF(CLFSH, 0x00000001, 0, RTE_REG_EDX, 19)
79         FEAT_DEF(DS, 0x00000001, 0, RTE_REG_EDX, 21)
80         FEAT_DEF(ACPI, 0x00000001, 0, RTE_REG_EDX, 22)
81         FEAT_DEF(MMX, 0x00000001, 0, RTE_REG_EDX, 23)
82         FEAT_DEF(FXSR, 0x00000001, 0, RTE_REG_EDX, 24)
83         FEAT_DEF(SSE, 0x00000001, 0, RTE_REG_EDX, 25)
84         FEAT_DEF(SSE2, 0x00000001, 0, RTE_REG_EDX, 26)
85         FEAT_DEF(SS, 0x00000001, 0, RTE_REG_EDX, 27)
86         FEAT_DEF(HTT, 0x00000001, 0, RTE_REG_EDX, 28)
87         FEAT_DEF(TM, 0x00000001, 0, RTE_REG_EDX, 29)
88         FEAT_DEF(PBE, 0x00000001, 0, RTE_REG_EDX, 31)
89
90         FEAT_DEF(DIGTEMP, 0x00000006, 0, RTE_REG_EAX,  0)
91         FEAT_DEF(TRBOBST, 0x00000006, 0, RTE_REG_EAX,  1)
92         FEAT_DEF(ARAT, 0x00000006, 0, RTE_REG_EAX,  2)
93         FEAT_DEF(PLN, 0x00000006, 0, RTE_REG_EAX,  4)
94         FEAT_DEF(ECMD, 0x00000006, 0, RTE_REG_EAX,  5)
95         FEAT_DEF(PTM, 0x00000006, 0, RTE_REG_EAX,  6)
96
97         FEAT_DEF(MPERF_APERF_MSR, 0x00000006, 0, RTE_REG_ECX,  0)
98         FEAT_DEF(ACNT2, 0x00000006, 0, RTE_REG_ECX,  1)
99         FEAT_DEF(ENERGY_EFF, 0x00000006, 0, RTE_REG_ECX,  3)
100
101         FEAT_DEF(FSGSBASE, 0x00000007, 0, RTE_REG_EBX,  0)
102         FEAT_DEF(BMI1, 0x00000007, 0, RTE_REG_EBX,  2)
103         FEAT_DEF(HLE, 0x00000007, 0, RTE_REG_EBX,  4)
104         FEAT_DEF(AVX2, 0x00000007, 0, RTE_REG_EBX,  5)
105         FEAT_DEF(SMEP, 0x00000007, 0, RTE_REG_EBX,  6)
106         FEAT_DEF(BMI2, 0x00000007, 0, RTE_REG_EBX,  7)
107         FEAT_DEF(ERMS, 0x00000007, 0, RTE_REG_EBX,  8)
108         FEAT_DEF(INVPCID, 0x00000007, 0, RTE_REG_EBX, 10)
109         FEAT_DEF(RTM, 0x00000007, 0, RTE_REG_EBX, 11)
110         FEAT_DEF(AVX512F, 0x00000007, 0, RTE_REG_EBX, 16)
111
112         FEAT_DEF(LAHF_SAHF, 0x80000001, 0, RTE_REG_ECX,  0)
113         FEAT_DEF(LZCNT, 0x80000001, 0, RTE_REG_ECX,  4)
114
115         FEAT_DEF(SYSCALL, 0x80000001, 0, RTE_REG_EDX, 11)
116         FEAT_DEF(XD, 0x80000001, 0, RTE_REG_EDX, 20)
117         FEAT_DEF(1GB_PG, 0x80000001, 0, RTE_REG_EDX, 26)
118         FEAT_DEF(RDTSCP, 0x80000001, 0, RTE_REG_EDX, 27)
119         FEAT_DEF(EM64T, 0x80000001, 0, RTE_REG_EDX, 29)
120
121         FEAT_DEF(INVTSC, 0x80000007, 0, RTE_REG_EDX,  8)
122 };
123
124 int
125 rte_cpu_get_flag_enabled(enum rte_cpu_flag_t feature)
126 {
127         const struct feature_entry *feat;
128         cpuid_registers_t regs;
129         unsigned int maxleaf;
130
131         if (feature >= RTE_CPUFLAG_NUMFLAGS)
132                 /* Flag does not match anything in the feature tables */
133                 return -ENOENT;
134
135         feat = &rte_cpu_feature_table[feature];
136
137         if (!feat->leaf)
138                 /* This entry in the table wasn't filled out! */
139                 return -EFAULT;
140
141         maxleaf = __get_cpuid_max(feat->leaf & 0x80000000, NULL);
142
143         if (maxleaf < feat->leaf)
144                 return 0;
145
146          __cpuid_count(feat->leaf, feat->subleaf,
147                          regs[RTE_REG_EAX], regs[RTE_REG_EBX],
148                          regs[RTE_REG_ECX], regs[RTE_REG_EDX]);
149
150         /* check if the feature is enabled */
151         return (regs[feat->reg] >> feat->bit) & 1;
152 }
153
154 const char *
155 rte_cpu_get_flag_name(enum rte_cpu_flag_t feature)
156 {
157         if (feature >= RTE_CPUFLAG_NUMFLAGS)
158                 return NULL;
159         return rte_cpu_feature_table[feature].name;
160 }