cd7a7b16925d5fb306ce95d81fe8ffde2acaab50
[dpdk.git] / lib / librte_eal / common / arch / arm / rte_cpuflags.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium networks Ltd. 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 networks 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_HWCAP = 0,
56         REG_HWCAP2,
57         REG_PLATFORM,
58 };
59
60 typedef uint32_t cpuid_registers_t[4];
61
62 /**
63  * Struct to hold a processor feature entry
64  */
65 struct feature_entry {
66         uint32_t leaf;                          /**< cpuid leaf */
67         uint32_t subleaf;                       /**< cpuid subleaf */
68         uint32_t reg;                           /**< cpuid register */
69         uint32_t bit;                           /**< cpuid register bit */
70 #define CPU_FLAG_NAME_MAX_LEN 64
71         char name[CPU_FLAG_NAME_MAX_LEN];       /**< String for printing */
72 };
73
74 #define FEAT_DEF(name, leaf, subleaf, reg, bit) \
75         [RTE_CPUFLAG_##name] = {leaf, subleaf, 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,       0x00000001, 0, REG_HWCAP,  0)
83         FEAT_DEF(HALF,      0x00000001, 0, REG_HWCAP,  1)
84         FEAT_DEF(THUMB,     0x00000001, 0, REG_HWCAP,  2)
85         FEAT_DEF(A26BIT,    0x00000001, 0, REG_HWCAP,  3)
86         FEAT_DEF(FAST_MULT, 0x00000001, 0, REG_HWCAP,  4)
87         FEAT_DEF(FPA,       0x00000001, 0, REG_HWCAP,  5)
88         FEAT_DEF(VFP,       0x00000001, 0, REG_HWCAP,  6)
89         FEAT_DEF(EDSP,      0x00000001, 0, REG_HWCAP,  7)
90         FEAT_DEF(JAVA,      0x00000001, 0, REG_HWCAP,  8)
91         FEAT_DEF(IWMMXT,    0x00000001, 0, REG_HWCAP,  9)
92         FEAT_DEF(CRUNCH,    0x00000001, 0, REG_HWCAP,  10)
93         FEAT_DEF(THUMBEE,   0x00000001, 0, REG_HWCAP,  11)
94         FEAT_DEF(NEON,      0x00000001, 0, REG_HWCAP,  12)
95         FEAT_DEF(VFPv3,     0x00000001, 0, REG_HWCAP,  13)
96         FEAT_DEF(VFPv3D16,  0x00000001, 0, REG_HWCAP,  14)
97         FEAT_DEF(TLS,       0x00000001, 0, REG_HWCAP,  15)
98         FEAT_DEF(VFPv4,     0x00000001, 0, REG_HWCAP,  16)
99         FEAT_DEF(IDIVA,     0x00000001, 0, REG_HWCAP,  17)
100         FEAT_DEF(IDIVT,     0x00000001, 0, REG_HWCAP,  18)
101         FEAT_DEF(VFPD32,    0x00000001, 0, REG_HWCAP,  19)
102         FEAT_DEF(LPAE,      0x00000001, 0, REG_HWCAP,  20)
103         FEAT_DEF(EVTSTRM,   0x00000001, 0, REG_HWCAP,  21)
104         FEAT_DEF(AES,       0x00000001, 0, REG_HWCAP2,  0)
105         FEAT_DEF(PMULL,     0x00000001, 0, REG_HWCAP2,  1)
106         FEAT_DEF(SHA1,      0x00000001, 0, REG_HWCAP2,  2)
107         FEAT_DEF(SHA2,      0x00000001, 0, REG_HWCAP2,  3)
108         FEAT_DEF(CRC32,     0x00000001, 0, REG_HWCAP2,  4)
109         FEAT_DEF(V7L,       0x00000001, 0, 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,            0x00000001, 0, REG_HWCAP,  0)
118         FEAT_DEF(NEON,          0x00000001, 0, REG_HWCAP,  1)
119         FEAT_DEF(EVTSTRM,       0x00000001, 0, REG_HWCAP,  2)
120         FEAT_DEF(AES,           0x00000001, 0, REG_HWCAP,  3)
121         FEAT_DEF(PMULL,         0x00000001, 0, REG_HWCAP,  4)
122         FEAT_DEF(SHA1,          0x00000001, 0, REG_HWCAP,  5)
123         FEAT_DEF(SHA2,          0x00000001, 0, REG_HWCAP,  6)
124         FEAT_DEF(CRC32,         0x00000001, 0, REG_HWCAP,  7)
125         FEAT_DEF(AARCH64,       0x00000001, 0, REG_PLATFORM, 1)
126 };
127 #endif /* RTE_ARCH */
128
129 /*
130  * Read AUXV software register and get cpu features for ARM
131  */
132 static void
133 rte_cpu_get_features(__attribute__((unused)) uint32_t leaf,
134         __attribute__((unused)) uint32_t subleaf, cpuid_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 }
152
153 /*
154  * Checks if a particular flag is available on current machine.
155  */
156 int
157 rte_cpu_get_flag_enabled(enum rte_cpu_flag_t feature)
158 {
159         const struct feature_entry *feat;
160         cpuid_registers_t regs = {0};
161
162         if (feature >= RTE_CPUFLAG_NUMFLAGS)
163                 /* Flag does not match anything in the feature tables */
164                 return -ENOENT;
165
166         feat = &rte_cpu_feature_table[feature];
167
168         if (!feat->leaf)
169                 /* This entry in the table wasn't filled out! */
170                 return -EFAULT;
171
172         /* get the cpuid leaf containing the desired feature */
173         rte_cpu_get_features(feat->leaf, feat->subleaf, regs);
174
175         /* check if the feature is enabled */
176         return (regs[feat->reg] >> feat->bit) & 1;
177 }
178
179 const char *
180 rte_cpu_get_flag_name(enum rte_cpu_flag_t feature)
181 {
182         if (feature >= RTE_CPUFLAG_NUMFLAGS)
183                 return NULL;
184         return rte_cpu_feature_table[feature].name;
185 }