eal: move CPU flag functions out of headers
[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_HWCAP = 0,
47         REG_HWCAP2,
48 };
49
50 typedef uint32_t cpuid_registers_t[4];
51
52 /**
53  * Struct to hold a processor feature entry
54  */
55 struct feature_entry {
56         uint32_t leaf;                          /**< cpuid leaf */
57         uint32_t subleaf;                       /**< cpuid subleaf */
58         uint32_t reg;                           /**< cpuid register */
59         uint32_t bit;                           /**< cpuid register bit */
60 #define CPU_FLAG_NAME_MAX_LEN 64
61         char name[CPU_FLAG_NAME_MAX_LEN];       /**< String for printing */
62 };
63
64 #define FEAT_DEF(name, leaf, subleaf, reg, bit) \
65         [RTE_CPUFLAG_##name] = {leaf, subleaf, reg, bit, #name },
66
67 const struct feature_entry rte_cpu_feature_table[] = {
68         FEAT_DEF(PPC_LE, 0x00000001, 0, REG_HWCAP,  0)
69         FEAT_DEF(TRUE_LE, 0x00000001, 0, REG_HWCAP,  1)
70         FEAT_DEF(PSERIES_PERFMON_COMPAT, 0x00000001, 0, REG_HWCAP,  6)
71         FEAT_DEF(VSX, 0x00000001, 0, REG_HWCAP,  7)
72         FEAT_DEF(ARCH_2_06, 0x00000001, 0, REG_HWCAP,  8)
73         FEAT_DEF(POWER6_EXT, 0x00000001, 0, REG_HWCAP,  9)
74         FEAT_DEF(DFP, 0x00000001, 0, REG_HWCAP,  10)
75         FEAT_DEF(PA6T, 0x00000001, 0, REG_HWCAP,  11)
76         FEAT_DEF(ARCH_2_05, 0x00000001, 0, REG_HWCAP,  12)
77         FEAT_DEF(ICACHE_SNOOP, 0x00000001, 0, REG_HWCAP,  13)
78         FEAT_DEF(SMT, 0x00000001, 0, REG_HWCAP,  14)
79         FEAT_DEF(BOOKE, 0x00000001, 0, REG_HWCAP,  15)
80         FEAT_DEF(CELLBE, 0x00000001, 0, REG_HWCAP,  16)
81         FEAT_DEF(POWER5_PLUS, 0x00000001, 0, REG_HWCAP,  17)
82         FEAT_DEF(POWER5, 0x00000001, 0, REG_HWCAP,  18)
83         FEAT_DEF(POWER4, 0x00000001, 0, REG_HWCAP,  19)
84         FEAT_DEF(NOTB, 0x00000001, 0, REG_HWCAP,  20)
85         FEAT_DEF(EFP_DOUBLE, 0x00000001, 0, REG_HWCAP,  21)
86         FEAT_DEF(EFP_SINGLE, 0x00000001, 0, REG_HWCAP,  22)
87         FEAT_DEF(SPE, 0x00000001, 0, REG_HWCAP,  23)
88         FEAT_DEF(UNIFIED_CACHE, 0x00000001, 0, REG_HWCAP,  24)
89         FEAT_DEF(4xxMAC, 0x00000001, 0, REG_HWCAP,  25)
90         FEAT_DEF(MMU, 0x00000001, 0, REG_HWCAP,  26)
91         FEAT_DEF(FPU, 0x00000001, 0, REG_HWCAP,  27)
92         FEAT_DEF(ALTIVEC, 0x00000001, 0, REG_HWCAP,  28)
93         FEAT_DEF(PPC601, 0x00000001, 0, REG_HWCAP,  29)
94         FEAT_DEF(PPC64, 0x00000001, 0, REG_HWCAP,  30)
95         FEAT_DEF(PPC32, 0x00000001, 0, REG_HWCAP,  31)
96         FEAT_DEF(TAR, 0x00000001, 0, REG_HWCAP2,  26)
97         FEAT_DEF(LSEL, 0x00000001, 0, REG_HWCAP2,  27)
98         FEAT_DEF(EBB, 0x00000001, 0, REG_HWCAP2,  28)
99         FEAT_DEF(DSCR, 0x00000001, 0, REG_HWCAP2,  29)
100         FEAT_DEF(HTM, 0x00000001, 0, REG_HWCAP2,  30)
101         FEAT_DEF(ARCH_2_07, 0x00000001, 0, REG_HWCAP2,  31)
102 };
103
104 /*
105  * Read AUXV software register and get cpu features for Power
106  */
107 static void
108 rte_cpu_get_features(__attribute__((unused)) uint32_t leaf,
109         __attribute__((unused)) uint32_t subleaf, cpuid_registers_t out)
110 {
111         int auxv_fd;
112         Elf64_auxv_t auxv;
113
114         auxv_fd = open("/proc/self/auxv", O_RDONLY);
115         assert(auxv_fd);
116         while (read(auxv_fd, &auxv,
117                 sizeof(Elf64_auxv_t)) == sizeof(Elf64_auxv_t)) {
118                 if (auxv.a_type == AT_HWCAP)
119                         out[REG_HWCAP] = auxv.a_un.a_val;
120                 else if (auxv.a_type == AT_HWCAP2)
121                         out[REG_HWCAP2] = auxv.a_un.a_val;
122         }
123 }
124
125 /*
126  * Checks if a particular flag is available on current machine.
127  */
128 int
129 rte_cpu_get_flag_enabled(enum rte_cpu_flag_t feature)
130 {
131         const struct feature_entry *feat;
132         cpuid_registers_t regs = {0};
133
134         if (feature >= RTE_CPUFLAG_NUMFLAGS)
135                 /* Flag does not match anything in the feature tables */
136                 return -ENOENT;
137
138         feat = &rte_cpu_feature_table[feature];
139
140         if (!feat->leaf)
141                 /* This entry in the table wasn't filled out! */
142                 return -EFAULT;
143
144         /* get the cpuid leaf containing the desired feature */
145         rte_cpu_get_features(feat->leaf, feat->subleaf, regs);
146
147         /* check if the feature is enabled */
148         return (regs[feat->reg] >> feat->bit) & 1;
149 }
150
151 const char *
152 rte_cpu_get_flag_name(enum rte_cpu_flag_t feature)
153 {
154         if (feature >= RTE_CPUFLAG_NUMFLAGS)
155                 return NULL;
156         return rte_cpu_feature_table[feature].name;
157 }