5636e9c1d0cc5bbd0b54d49aaab10f7b7463b0ca
[deb_dpdk.git] / lib / librte_eal / common / arch / arm / rte_cpuflags.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium, Inc. 2015.
5  *   Copyright(c) 2015 RehiveTech. All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include "rte_cpuflags.h"
35
36 #include <elf.h>
37 #include <fcntl.h>
38 #include <assert.h>
39 #include <unistd.h>
40 #include <string.h>
41
42 #ifndef AT_HWCAP
43 #define AT_HWCAP 16
44 #endif
45
46 #ifndef AT_HWCAP2
47 #define AT_HWCAP2 26
48 #endif
49
50 #ifndef AT_PLATFORM
51 #define AT_PLATFORM 15
52 #endif
53
54 enum cpu_register_t {
55         REG_NONE = 0,
56         REG_HWCAP,
57         REG_HWCAP2,
58         REG_PLATFORM,
59         REG_MAX
60 };
61
62 typedef uint32_t hwcap_registers_t[REG_MAX];
63
64 /**
65  * Struct to hold a processor feature entry
66  */
67 struct feature_entry {
68         uint32_t reg;
69         uint32_t bit;
70 #define CPU_FLAG_NAME_MAX_LEN 64
71         char name[CPU_FLAG_NAME_MAX_LEN];
72 };
73
74 #define FEAT_DEF(name, reg, bit) \
75         [RTE_CPUFLAG_##name] = {reg, bit, #name},
76
77 #ifdef RTE_ARCH_ARMv7
78 #define PLATFORM_STR "v7l"
79 typedef Elf32_auxv_t _Elfx_auxv_t;
80
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)
110 };
111
112 #elif defined RTE_ARCH_ARM64
113 #define PLATFORM_STR "aarch64"
114 typedef Elf64_auxv_t _Elfx_auxv_t;
115
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)
127 };
128 #endif /* RTE_ARCH */
129
130 /*
131  * Read AUXV software register and get cpu features for ARM
132  */
133 static void
134 rte_cpu_get_features(hwcap_registers_t out)
135 {
136         int auxv_fd;
137         _Elfx_auxv_t auxv;
138
139         auxv_fd = open("/proc/self/auxv", O_RDONLY);
140         assert(auxv_fd);
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;
149                 }
150         }
151         close(auxv_fd);
152 }
153
154 /*
155  * Checks if a particular flag is available on current machine.
156  */
157 int
158 rte_cpu_get_flag_enabled(enum rte_cpu_flag_t feature)
159 {
160         const struct feature_entry *feat;
161         hwcap_registers_t regs = {0};
162
163         if (feature >= RTE_CPUFLAG_NUMFLAGS)
164                 return -ENOENT;
165
166         feat = &rte_cpu_feature_table[feature];
167         if (feat->reg == REG_NONE)
168                 return -EFAULT;
169
170         rte_cpu_get_features(regs);
171         return (regs[feat->reg] >> feat->bit) & 1;
172 }
173
174 const char *
175 rte_cpu_get_flag_name(enum rte_cpu_flag_t feature)
176 {
177         if (feature >= RTE_CPUFLAG_NUMFLAGS)
178                 return NULL;
179         return rte_cpu_feature_table[feature].name;
180 }