eal: move cpu flags out of headers
[dpdk.git] / lib / librte_eal / common / include / arch / arm / rte_cpuflags_32.h
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015 RehiveTech. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of RehiveTech nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #ifndef _RTE_CPUFLAGS_ARM32_H_
34 #define _RTE_CPUFLAGS_ARM32_H_
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 #include <elf.h>
41 #include <fcntl.h>
42 #include <assert.h>
43 #include <unistd.h>
44 #include <string.h>
45
46 #include "generic/rte_cpuflags.h"
47
48 extern const struct feature_entry rte_cpu_feature_table[];
49
50 #ifndef AT_HWCAP
51 #define AT_HWCAP 16
52 #endif
53
54 #ifndef AT_HWCAP2
55 #define AT_HWCAP2 26
56 #endif
57
58 #ifndef AT_PLATFORM
59 #define AT_PLATFORM 15
60 #endif
61
62 /* software based registers */
63 enum cpu_register_t {
64         REG_HWCAP = 0,
65         REG_HWCAP2,
66         REG_PLATFORM,
67 };
68
69 /**
70  * Enumeration of all CPU features supported
71  */
72 enum rte_cpu_flag_t {
73         RTE_CPUFLAG_SWP = 0,
74         RTE_CPUFLAG_HALF,
75         RTE_CPUFLAG_THUMB,
76         RTE_CPUFLAG_A26BIT,
77         RTE_CPUFLAG_FAST_MULT,
78         RTE_CPUFLAG_FPA,
79         RTE_CPUFLAG_VFP,
80         RTE_CPUFLAG_EDSP,
81         RTE_CPUFLAG_JAVA,
82         RTE_CPUFLAG_IWMMXT,
83         RTE_CPUFLAG_CRUNCH,
84         RTE_CPUFLAG_THUMBEE,
85         RTE_CPUFLAG_NEON,
86         RTE_CPUFLAG_VFPv3,
87         RTE_CPUFLAG_VFPv3D16,
88         RTE_CPUFLAG_TLS,
89         RTE_CPUFLAG_VFPv4,
90         RTE_CPUFLAG_IDIVA,
91         RTE_CPUFLAG_IDIVT,
92         RTE_CPUFLAG_VFPD32,
93         RTE_CPUFLAG_LPAE,
94         RTE_CPUFLAG_EVTSTRM,
95         RTE_CPUFLAG_AES,
96         RTE_CPUFLAG_PMULL,
97         RTE_CPUFLAG_SHA1,
98         RTE_CPUFLAG_SHA2,
99         RTE_CPUFLAG_CRC32,
100         RTE_CPUFLAG_V7L,
101         /* The last item */
102         RTE_CPUFLAG_NUMFLAGS,/**< This should always be the last! */
103 };
104
105 /*
106  * Read AUXV software register and get cpu features for ARM
107  */
108 static inline void
109 rte_cpu_get_features(__attribute__((unused)) uint32_t leaf,
110         __attribute__((unused)) uint32_t subleaf, cpuid_registers_t out)
111 {
112         int auxv_fd;
113         Elf32_auxv_t auxv;
114
115         auxv_fd = open("/proc/self/auxv", O_RDONLY);
116         assert(auxv_fd);
117         while (read(auxv_fd, &auxv,
118                 sizeof(Elf32_auxv_t)) == sizeof(Elf32_auxv_t)) {
119                 if (auxv.a_type == AT_HWCAP)
120                         out[REG_HWCAP] = auxv.a_un.a_val;
121                 else if (auxv.a_type == AT_HWCAP2)
122                         out[REG_HWCAP2] = auxv.a_un.a_val;
123                 else if (auxv.a_type == AT_PLATFORM) {
124                         if (!strcmp((const char *)auxv.a_un.a_val, "v7l"))
125                                 out[REG_PLATFORM] = 0x0001;
126                 }
127         }
128 }
129
130 /*
131  * Checks if a particular flag is available on current machine.
132  */
133 static inline int
134 rte_cpu_get_flag_enabled(enum rte_cpu_flag_t feature)
135 {
136         const struct feature_entry *feat;
137         cpuid_registers_t regs = {0};
138
139         if (feature >= RTE_CPUFLAG_NUMFLAGS)
140                 /* Flag does not match anything in the feature tables */
141                 return -ENOENT;
142
143         feat = &rte_cpu_feature_table[feature];
144
145         if (!feat->leaf)
146                 /* This entry in the table wasn't filled out! */
147                 return -EFAULT;
148
149         /* get the cpuid leaf containing the desired feature */
150         rte_cpu_get_features(feat->leaf, feat->subleaf, regs);
151
152         /* check if the feature is enabled */
153         return (regs[feat->reg] >> feat->bit) & 1;
154 }
155
156 #ifdef __cplusplus
157 }
158 #endif
159
160 #endif /* _RTE_CPUFLAGS_ARM32_H_ */