b36040b2c0a696cc4e81e2a81a1789e2204cc041
[dpdk.git] / lib / librte_eal / common / include / arch / arm / rte_cpuflags_64.h
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium networks Ltd. 2015.
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 Cavium networks 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_ARM64_H_
34 #define _RTE_CPUFLAGS_ARM64_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_FP = 0,
74         RTE_CPUFLAG_NEON,
75         RTE_CPUFLAG_EVTSTRM,
76         RTE_CPUFLAG_AES,
77         RTE_CPUFLAG_PMULL,
78         RTE_CPUFLAG_SHA1,
79         RTE_CPUFLAG_SHA2,
80         RTE_CPUFLAG_CRC32,
81         RTE_CPUFLAG_AARCH64,
82         /* The last item */
83         RTE_CPUFLAG_NUMFLAGS,/**< This should always be the last! */
84 };
85
86 /*
87  * Read AUXV software register and get cpu features for ARM
88  */
89 static inline void
90 rte_cpu_get_features(__attribute__((unused)) uint32_t leaf,
91                      __attribute__((unused)) uint32_t subleaf,
92                      cpuid_registers_t out)
93 {
94         int auxv_fd;
95         Elf64_auxv_t auxv;
96
97         auxv_fd = open("/proc/self/auxv", O_RDONLY);
98         assert(auxv_fd);
99         while (read(auxv_fd, &auxv,
100                     sizeof(Elf64_auxv_t)) == sizeof(Elf64_auxv_t)) {
101                 if (auxv.a_type == AT_HWCAP) {
102                         out[REG_HWCAP] = auxv.a_un.a_val;
103                 } else if (auxv.a_type == AT_HWCAP2) {
104                         out[REG_HWCAP2] = auxv.a_un.a_val;
105                 } else if (auxv.a_type == AT_PLATFORM) {
106                         if (!strcmp((const char *)auxv.a_un.a_val, "aarch64"))
107                                 out[REG_PLATFORM] = 0x0001;
108                 }
109         }
110 }
111
112 /*
113  * Checks if a particular flag is available on current machine.
114  */
115 static inline int
116 rte_cpu_get_flag_enabled(enum rte_cpu_flag_t feature)
117 {
118         const struct feature_entry *feat;
119         cpuid_registers_t regs = {0};
120
121         if (feature >= RTE_CPUFLAG_NUMFLAGS)
122                 /* Flag does not match anything in the feature tables */
123                 return -ENOENT;
124
125         feat = &rte_cpu_feature_table[feature];
126
127         if (!feat->leaf)
128                 /* This entry in the table wasn't filled out! */
129                 return -EFAULT;
130
131         /* get the cpuid leaf containing the desired feature */
132         rte_cpu_get_features(feat->leaf, feat->subleaf, regs);
133
134         /* check if the feature is enabled */
135         return (regs[feat->reg] >> feat->bit) & 1;
136 }
137
138 #ifdef __cplusplus
139 }
140 #endif
141
142 #endif /* _RTE_CPUFLAGS_ARM64_H_ */