4 * Copyright (C) Cavium, Inc. 2015.
5 * Copyright(c) 2015 RehiveTech. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Cavium, Inc nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "rte_cpuflags.h"
51 #define AT_PLATFORM 15
62 typedef uint32_t hwcap_registers_t[REG_MAX];
65 * Struct to hold a processor feature entry
67 struct feature_entry {
70 #define CPU_FLAG_NAME_MAX_LEN 64
71 char name[CPU_FLAG_NAME_MAX_LEN];
74 #define FEAT_DEF(name, reg, bit) \
75 [RTE_CPUFLAG_##name] = {reg, bit, #name},
78 #define PLATFORM_STR "v7l"
79 typedef Elf32_auxv_t _Elfx_auxv_t;
81 const struct feature_entry rte_cpu_feature_table[] = {
82 FEAT_DEF(SWP, REG_HWCAP, 0)
83 FEAT_DEF(HALF, REG_HWCAP, 1)
84 FEAT_DEF(THUMB, REG_HWCAP, 2)
85 FEAT_DEF(A26BIT, REG_HWCAP, 3)
86 FEAT_DEF(FAST_MULT, REG_HWCAP, 4)
87 FEAT_DEF(FPA, REG_HWCAP, 5)
88 FEAT_DEF(VFP, REG_HWCAP, 6)
89 FEAT_DEF(EDSP, REG_HWCAP, 7)
90 FEAT_DEF(JAVA, REG_HWCAP, 8)
91 FEAT_DEF(IWMMXT, REG_HWCAP, 9)
92 FEAT_DEF(CRUNCH, REG_HWCAP, 10)
93 FEAT_DEF(THUMBEE, REG_HWCAP, 11)
94 FEAT_DEF(NEON, REG_HWCAP, 12)
95 FEAT_DEF(VFPv3, REG_HWCAP, 13)
96 FEAT_DEF(VFPv3D16, REG_HWCAP, 14)
97 FEAT_DEF(TLS, REG_HWCAP, 15)
98 FEAT_DEF(VFPv4, REG_HWCAP, 16)
99 FEAT_DEF(IDIVA, REG_HWCAP, 17)
100 FEAT_DEF(IDIVT, REG_HWCAP, 18)
101 FEAT_DEF(VFPD32, REG_HWCAP, 19)
102 FEAT_DEF(LPAE, REG_HWCAP, 20)
103 FEAT_DEF(EVTSTRM, REG_HWCAP, 21)
104 FEAT_DEF(AES, REG_HWCAP2, 0)
105 FEAT_DEF(PMULL, REG_HWCAP2, 1)
106 FEAT_DEF(SHA1, REG_HWCAP2, 2)
107 FEAT_DEF(SHA2, REG_HWCAP2, 3)
108 FEAT_DEF(CRC32, REG_HWCAP2, 4)
109 FEAT_DEF(V7L, REG_PLATFORM, 0)
112 #elif defined RTE_ARCH_ARM64
113 #define PLATFORM_STR "aarch64"
114 typedef Elf64_auxv_t _Elfx_auxv_t;
116 const struct feature_entry rte_cpu_feature_table[] = {
117 FEAT_DEF(FP, REG_HWCAP, 0)
118 FEAT_DEF(NEON, REG_HWCAP, 1)
119 FEAT_DEF(EVTSTRM, REG_HWCAP, 2)
120 FEAT_DEF(AES, REG_HWCAP, 3)
121 FEAT_DEF(PMULL, REG_HWCAP, 4)
122 FEAT_DEF(SHA1, REG_HWCAP, 5)
123 FEAT_DEF(SHA2, REG_HWCAP, 6)
124 FEAT_DEF(CRC32, REG_HWCAP, 7)
125 FEAT_DEF(ATOMICS, REG_HWCAP, 8)
126 FEAT_DEF(AARCH64, REG_PLATFORM, 1)
128 #endif /* RTE_ARCH */
131 * Read AUXV software register and get cpu features for ARM
134 rte_cpu_get_features(hwcap_registers_t out)
139 auxv_fd = open("/proc/self/auxv", O_RDONLY);
140 assert(auxv_fd != -1);
141 while (read(auxv_fd, &auxv, sizeof(auxv)) == sizeof(auxv)) {
142 if (auxv.a_type == AT_HWCAP) {
143 out[REG_HWCAP] = auxv.a_un.a_val;
144 } else if (auxv.a_type == AT_HWCAP2) {
145 out[REG_HWCAP2] = auxv.a_un.a_val;
146 } else if (auxv.a_type == AT_PLATFORM) {
147 if (!strcmp((const char *)auxv.a_un.a_val, PLATFORM_STR))
148 out[REG_PLATFORM] = 0x0001;
155 * Checks if a particular flag is available on current machine.
158 rte_cpu_get_flag_enabled(enum rte_cpu_flag_t feature)
160 const struct feature_entry *feat;
161 hwcap_registers_t regs = {0};
163 if (feature >= RTE_CPUFLAG_NUMFLAGS)
166 feat = &rte_cpu_feature_table[feature];
167 if (feat->reg == REG_NONE)
170 rte_cpu_get_features(regs);
171 return (regs[feat->reg] >> feat->bit) & 1;
175 rte_cpu_get_flag_name(enum rte_cpu_flag_t feature)
177 if (feature >= RTE_CPUFLAG_NUMFLAGS)
179 return rte_cpu_feature_table[feature].name;