Imported Upstream version 16.07.2
[deb_dpdk.git] / lib / librte_eal / common / arch / ppc_64 / rte_cpuflags.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) IBM Corporation 2014.
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 IBM Corporation 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 #include "rte_cpuflags.h"
34
35 #include <elf.h>
36 #include <fcntl.h>
37 #include <assert.h>
38 #include <unistd.h>
39
40 /* Symbolic values for the entries in the auxiliary table */
41 #define AT_HWCAP  16
42 #define AT_HWCAP2 26
43
44 /* software based registers */
45 enum cpu_register_t {
46         REG_NONE = 0,
47         REG_HWCAP,
48         REG_HWCAP2,
49         REG_MAX
50 };
51
52 typedef uint32_t hwcap_registers_t[REG_MAX];
53
54 struct feature_entry {
55         uint32_t reg;
56         uint32_t bit;
57 #define CPU_FLAG_NAME_MAX_LEN 64
58         char name[CPU_FLAG_NAME_MAX_LEN];
59 };
60
61 #define FEAT_DEF(name, reg, bit) \
62         [RTE_CPUFLAG_##name] = {reg, bit, #name},
63
64 const struct feature_entry rte_cpu_feature_table[] = {
65         FEAT_DEF(PPC_LE,                 REG_HWCAP,   0)
66         FEAT_DEF(TRUE_LE,                REG_HWCAP,   1)
67         FEAT_DEF(PSERIES_PERFMON_COMPAT, REG_HWCAP,   6)
68         FEAT_DEF(VSX,                    REG_HWCAP,   7)
69         FEAT_DEF(ARCH_2_06,              REG_HWCAP,   8)
70         FEAT_DEF(POWER6_EXT,             REG_HWCAP,   9)
71         FEAT_DEF(DFP,                    REG_HWCAP,  10)
72         FEAT_DEF(PA6T,                   REG_HWCAP,  11)
73         FEAT_DEF(ARCH_2_05,              REG_HWCAP,  12)
74         FEAT_DEF(ICACHE_SNOOP,           REG_HWCAP,  13)
75         FEAT_DEF(SMT,                    REG_HWCAP,  14)
76         FEAT_DEF(BOOKE,                  REG_HWCAP,  15)
77         FEAT_DEF(CELLBE,                 REG_HWCAP,  16)
78         FEAT_DEF(POWER5_PLUS,            REG_HWCAP,  17)
79         FEAT_DEF(POWER5,                 REG_HWCAP,  18)
80         FEAT_DEF(POWER4,                 REG_HWCAP,  19)
81         FEAT_DEF(NOTB,                   REG_HWCAP,  20)
82         FEAT_DEF(EFP_DOUBLE,             REG_HWCAP,  21)
83         FEAT_DEF(EFP_SINGLE,             REG_HWCAP,  22)
84         FEAT_DEF(SPE,                    REG_HWCAP,  23)
85         FEAT_DEF(UNIFIED_CACHE,          REG_HWCAP,  24)
86         FEAT_DEF(4xxMAC,                 REG_HWCAP,  25)
87         FEAT_DEF(MMU,                    REG_HWCAP,  26)
88         FEAT_DEF(FPU,                    REG_HWCAP,  27)
89         FEAT_DEF(ALTIVEC,                REG_HWCAP,  28)
90         FEAT_DEF(PPC601,                 REG_HWCAP,  29)
91         FEAT_DEF(PPC64,                  REG_HWCAP,  30)
92         FEAT_DEF(PPC32,                  REG_HWCAP,  31)
93         FEAT_DEF(TAR,                    REG_HWCAP2, 26)
94         FEAT_DEF(LSEL,                   REG_HWCAP2, 27)
95         FEAT_DEF(EBB,                    REG_HWCAP2, 28)
96         FEAT_DEF(DSCR,                   REG_HWCAP2, 29)
97         FEAT_DEF(HTM,                    REG_HWCAP2, 30)
98         FEAT_DEF(ARCH_2_07,              REG_HWCAP2, 31)
99 };
100
101 /*
102  * Read AUXV software register and get cpu features for Power
103  */
104 static void
105 rte_cpu_get_features(hwcap_registers_t out)
106 {
107         int auxv_fd;
108         Elf64_auxv_t auxv;
109
110         auxv_fd = open("/proc/self/auxv", O_RDONLY);
111         assert(auxv_fd);
112         while (read(auxv_fd, &auxv,
113                 sizeof(Elf64_auxv_t)) == sizeof(Elf64_auxv_t)) {
114                 if (auxv.a_type == AT_HWCAP)
115                         out[REG_HWCAP] = auxv.a_un.a_val;
116                 else if (auxv.a_type == AT_HWCAP2)
117                         out[REG_HWCAP2] = auxv.a_un.a_val;
118         }
119         close(auxv_fd);
120 }
121
122 /*
123  * Checks if a particular flag is available on current machine.
124  */
125 int
126 rte_cpu_get_flag_enabled(enum rte_cpu_flag_t feature)
127 {
128         const struct feature_entry *feat;
129         hwcap_registers_t regs = {0};
130
131         if (feature >= RTE_CPUFLAG_NUMFLAGS)
132                 return -ENOENT;
133
134         feat = &rte_cpu_feature_table[feature];
135         if (feat->reg == REG_NONE)
136                 return -EFAULT;
137
138         rte_cpu_get_features(regs);
139         return (regs[feat->reg] >> feat->bit) & 1;
140 }
141
142 const char *
143 rte_cpu_get_flag_name(enum rte_cpu_flag_t feature)
144 {
145         if (feature >= RTE_CPUFLAG_NUMFLAGS)
146                 return NULL;
147         return rte_cpu_feature_table[feature].name;
148 }