xref: /aosp_15_r20/external/cpu_features/src/impl_ppc_linux.c (revision eca53ba6d2e951e174b64682eaf56a36b8204c89)
1*eca53ba6SRoland Levillain // Copyright 2018 IBM.
2*eca53ba6SRoland Levillain //
3*eca53ba6SRoland Levillain // Licensed under the Apache License, Version 2.0 (the "License");
4*eca53ba6SRoland Levillain // you may not use this file except in compliance with the License.
5*eca53ba6SRoland Levillain // You may obtain a copy of the License at
6*eca53ba6SRoland Levillain //
7*eca53ba6SRoland Levillain //    http://www.apache.org/licenses/LICENSE-2.0
8*eca53ba6SRoland Levillain //
9*eca53ba6SRoland Levillain // Unless required by applicable law or agreed to in writing, software
10*eca53ba6SRoland Levillain // distributed under the License is distributed on an "AS IS" BASIS,
11*eca53ba6SRoland Levillain // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*eca53ba6SRoland Levillain // See the License for the specific language governing permissions and
13*eca53ba6SRoland Levillain // limitations under the License.
14*eca53ba6SRoland Levillain 
15*eca53ba6SRoland Levillain #include "cpu_features_macros.h"
16*eca53ba6SRoland Levillain 
17*eca53ba6SRoland Levillain #ifdef CPU_FEATURES_ARCH_PPC
18*eca53ba6SRoland Levillain #ifdef CPU_FEATURES_OS_LINUX
19*eca53ba6SRoland Levillain 
20*eca53ba6SRoland Levillain #include "cpuinfo_ppc.h"
21*eca53ba6SRoland Levillain 
22*eca53ba6SRoland Levillain ////////////////////////////////////////////////////////////////////////////////
23*eca53ba6SRoland Levillain // Definitions for introspection.
24*eca53ba6SRoland Levillain ////////////////////////////////////////////////////////////////////////////////
25*eca53ba6SRoland Levillain #define INTROSPECTION_TABLE                                                    \
26*eca53ba6SRoland Levillain   LINE(PPC_32, ppc32, "ppc32", PPC_FEATURE_32, 0)                              \
27*eca53ba6SRoland Levillain   LINE(PPC_64, ppc64, "ppc64", PPC_FEATURE_64, 0)                              \
28*eca53ba6SRoland Levillain   LINE(PPC_601_INSTR, ppc601, "ppc601", PPC_FEATURE_601_INSTR, 0)              \
29*eca53ba6SRoland Levillain   LINE(PPC_HAS_ALTIVEC, altivec, "altivec", PPC_FEATURE_HAS_ALTIVEC, 0)        \
30*eca53ba6SRoland Levillain   LINE(PPC_HAS_FPU, fpu, "fpu", PPC_FEATURE_HAS_FPU, 0)                        \
31*eca53ba6SRoland Levillain   LINE(PPC_HAS_MMU, mmu, "mmu", PPC_FEATURE_HAS_MMU, 0)                        \
32*eca53ba6SRoland Levillain   LINE(PPC_HAS_4xxMAC, mac_4xx, "4xxmac", PPC_FEATURE_HAS_4xxMAC, 0)           \
33*eca53ba6SRoland Levillain   LINE(PPC_UNIFIED_CACHE, unifiedcache, "ucache", PPC_FEATURE_UNIFIED_CACHE,   \
34*eca53ba6SRoland Levillain        0)                                                                      \
35*eca53ba6SRoland Levillain   LINE(PPC_HAS_SPE, spe, "spe", PPC_FEATURE_HAS_SPE, 0)                        \
36*eca53ba6SRoland Levillain   LINE(PPC_HAS_EFP_SINGLE, efpsingle, "efpsingle", PPC_FEATURE_HAS_EFP_SINGLE, \
37*eca53ba6SRoland Levillain        0)                                                                      \
38*eca53ba6SRoland Levillain   LINE(PPC_HAS_EFP_DOUBLE, efpdouble, "efpdouble", PPC_FEATURE_HAS_EFP_DOUBLE, \
39*eca53ba6SRoland Levillain        0)                                                                      \
40*eca53ba6SRoland Levillain   LINE(PPC_NO_TB, no_tb, "notb", PPC_FEATURE_NO_TB, 0)                         \
41*eca53ba6SRoland Levillain   LINE(PPC_POWER4, power4, "power4", PPC_FEATURE_POWER4, 0)                    \
42*eca53ba6SRoland Levillain   LINE(PPC_POWER5, power5, "power5", PPC_FEATURE_POWER5, 0)                    \
43*eca53ba6SRoland Levillain   LINE(PPC_POWER5_PLUS, power5plus, "power5+", PPC_FEATURE_POWER5_PLUS, 0)     \
44*eca53ba6SRoland Levillain   LINE(PPC_CELL, cell, "cellbe", PPC_FEATURE_CELL, 0)                          \
45*eca53ba6SRoland Levillain   LINE(PPC_BOOKE, booke, "booke", PPC_FEATURE_BOOKE, 0)                        \
46*eca53ba6SRoland Levillain   LINE(PPC_SMT, smt, "smt", PPC_FEATURE_SMT, 0)                                \
47*eca53ba6SRoland Levillain   LINE(PPC_ICACHE_SNOOP, icachesnoop, "ic_snoop", PPC_FEATURE_ICACHE_SNOOP, 0) \
48*eca53ba6SRoland Levillain   LINE(PPC_ARCH_2_05, arch205, "arch_2_05", PPC_FEATURE_ARCH_2_05, 0)          \
49*eca53ba6SRoland Levillain   LINE(PPC_PA6T, pa6t, "pa6t", PPC_FEATURE_PA6T, 0)                            \
50*eca53ba6SRoland Levillain   LINE(PPC_HAS_DFP, dfp, "dfp", PPC_FEATURE_HAS_DFP, 0)                        \
51*eca53ba6SRoland Levillain   LINE(PPC_POWER6_EXT, power6ext, "power6x", PPC_FEATURE_POWER6_EXT, 0)        \
52*eca53ba6SRoland Levillain   LINE(PPC_ARCH_2_06, arch206, "arch_2_06", PPC_FEATURE_ARCH_2_06, 0)          \
53*eca53ba6SRoland Levillain   LINE(PPC_HAS_VSX, vsx, "vsx", PPC_FEATURE_HAS_VSX, 0)                        \
54*eca53ba6SRoland Levillain   LINE(PPC_PSERIES_PERFMON_COMPAT, pseries_perfmon_compat, "archpmu",          \
55*eca53ba6SRoland Levillain        PPC_FEATURE_PSERIES_PERFMON_COMPAT, 0)                                  \
56*eca53ba6SRoland Levillain   LINE(PPC_TRUE_LE, truele, "true_le", PPC_FEATURE_TRUE_LE, 0)                 \
57*eca53ba6SRoland Levillain   LINE(PPC_PPC_LE, ppcle, "ppcle", PPC_FEATURE_PPC_LE, 0)                      \
58*eca53ba6SRoland Levillain   LINE(PPC_ARCH_2_07, arch207, "arch_2_07", 0, PPC_FEATURE2_ARCH_2_07)         \
59*eca53ba6SRoland Levillain   LINE(PPC_HTM, htm, "htm", 0, PPC_FEATURE2_HTM)                               \
60*eca53ba6SRoland Levillain   LINE(PPC_DSCR, dscr, "dscr", 0, PPC_FEATURE2_DSCR)                           \
61*eca53ba6SRoland Levillain   LINE(PPC_EBB, ebb, "ebb", 0, PPC_FEATURE2_EBB)                               \
62*eca53ba6SRoland Levillain   LINE(PPC_ISEL, isel, "isel", 0, PPC_FEATURE2_ISEL)                           \
63*eca53ba6SRoland Levillain   LINE(PPC_TAR, tar, "tar", 0, PPC_FEATURE2_TAR)                               \
64*eca53ba6SRoland Levillain   LINE(PPC_VEC_CRYPTO, vcrypto, "vcrypto", 0, PPC_FEATURE2_VEC_CRYPTO)         \
65*eca53ba6SRoland Levillain   LINE(PPC_HTM_NOSC, htm_nosc, "htm-nosc", 0, PPC_FEATURE2_HTM_NOSC)           \
66*eca53ba6SRoland Levillain   LINE(PPC_ARCH_3_00, arch300, "arch_3_00", 0, PPC_FEATURE2_ARCH_3_00)         \
67*eca53ba6SRoland Levillain   LINE(PPC_HAS_IEEE128, ieee128, "ieee128", 0, PPC_FEATURE2_HAS_IEEE128)       \
68*eca53ba6SRoland Levillain   LINE(PPC_DARN, darn, "darn", 0, PPC_FEATURE2_DARN)                           \
69*eca53ba6SRoland Levillain   LINE(PPC_SCV, scv, "scv", 0, PPC_FEATURE2_SCV)                               \
70*eca53ba6SRoland Levillain   LINE(PPC_HTM_NO_SUSPEND, htm_no_suspend, "htm-no-suspend", 0,                \
71*eca53ba6SRoland Levillain        PPC_FEATURE2_HTM_NO_SUSPEND)
72*eca53ba6SRoland Levillain #undef PPC // Remove conflict with compiler generated preprocessor
73*eca53ba6SRoland Levillain #define INTROSPECTION_PREFIX PPC
74*eca53ba6SRoland Levillain #define INTROSPECTION_ENUM_PREFIX PPC
75*eca53ba6SRoland Levillain #include "define_introspection_and_hwcaps.inl"
76*eca53ba6SRoland Levillain 
77*eca53ba6SRoland Levillain ////////////////////////////////////////////////////////////////////////////////
78*eca53ba6SRoland Levillain // Implementation.
79*eca53ba6SRoland Levillain ////////////////////////////////////////////////////////////////////////////////
80*eca53ba6SRoland Levillain 
81*eca53ba6SRoland Levillain #include <stdbool.h>
82*eca53ba6SRoland Levillain 
83*eca53ba6SRoland Levillain #include "internal/bit_utils.h"
84*eca53ba6SRoland Levillain #include "internal/filesystem.h"
85*eca53ba6SRoland Levillain #include "internal/hwcaps.h"
86*eca53ba6SRoland Levillain #include "internal/stack_line_reader.h"
87*eca53ba6SRoland Levillain #include "internal/string_view.h"
88*eca53ba6SRoland Levillain 
HandlePPCLine(const LineResult result,PPCPlatformStrings * const strings)89*eca53ba6SRoland Levillain static bool HandlePPCLine(const LineResult result,
90*eca53ba6SRoland Levillain                           PPCPlatformStrings* const strings) {
91*eca53ba6SRoland Levillain   StringView line = result.line;
92*eca53ba6SRoland Levillain   StringView key, value;
93*eca53ba6SRoland Levillain   if (CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)) {
94*eca53ba6SRoland Levillain     if (CpuFeatures_StringView_HasWord(key, "platform", ' ')) {
95*eca53ba6SRoland Levillain       CpuFeatures_StringView_CopyString(value, strings->platform,
96*eca53ba6SRoland Levillain                                         sizeof(strings->platform));
97*eca53ba6SRoland Levillain     } else if (CpuFeatures_StringView_IsEquals(key, str("model"))) {
98*eca53ba6SRoland Levillain       CpuFeatures_StringView_CopyString(value, strings->model,
99*eca53ba6SRoland Levillain                                         sizeof(strings->platform));
100*eca53ba6SRoland Levillain     } else if (CpuFeatures_StringView_IsEquals(key, str("machine"))) {
101*eca53ba6SRoland Levillain       CpuFeatures_StringView_CopyString(value, strings->machine,
102*eca53ba6SRoland Levillain                                         sizeof(strings->platform));
103*eca53ba6SRoland Levillain     } else if (CpuFeatures_StringView_IsEquals(key, str("cpu"))) {
104*eca53ba6SRoland Levillain       CpuFeatures_StringView_CopyString(value, strings->cpu,
105*eca53ba6SRoland Levillain                                         sizeof(strings->platform));
106*eca53ba6SRoland Levillain     }
107*eca53ba6SRoland Levillain   }
108*eca53ba6SRoland Levillain   return !result.eof;
109*eca53ba6SRoland Levillain }
110*eca53ba6SRoland Levillain 
FillProcCpuInfoData(PPCPlatformStrings * const strings)111*eca53ba6SRoland Levillain static void FillProcCpuInfoData(PPCPlatformStrings* const strings) {
112*eca53ba6SRoland Levillain   const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
113*eca53ba6SRoland Levillain   if (fd >= 0) {
114*eca53ba6SRoland Levillain     StackLineReader reader;
115*eca53ba6SRoland Levillain     StackLineReader_Initialize(&reader, fd);
116*eca53ba6SRoland Levillain     for (;;) {
117*eca53ba6SRoland Levillain       if (!HandlePPCLine(StackLineReader_NextLine(&reader), strings)) {
118*eca53ba6SRoland Levillain         break;
119*eca53ba6SRoland Levillain       }
120*eca53ba6SRoland Levillain     }
121*eca53ba6SRoland Levillain     CpuFeatures_CloseFile(fd);
122*eca53ba6SRoland Levillain   }
123*eca53ba6SRoland Levillain }
124*eca53ba6SRoland Levillain 
125*eca53ba6SRoland Levillain static const PPCInfo kEmptyPPCInfo;
126*eca53ba6SRoland Levillain 
GetPPCInfo(void)127*eca53ba6SRoland Levillain PPCInfo GetPPCInfo(void) {
128*eca53ba6SRoland Levillain   /*
129*eca53ba6SRoland Levillain    * On Power feature flags aren't currently in cpuinfo so we only look at
130*eca53ba6SRoland Levillain    * the auxilary vector.
131*eca53ba6SRoland Levillain    */
132*eca53ba6SRoland Levillain   PPCInfo info = kEmptyPPCInfo;
133*eca53ba6SRoland Levillain   const HardwareCapabilities hwcaps = CpuFeatures_GetHardwareCapabilities();
134*eca53ba6SRoland Levillain   for (size_t i = 0; i < PPC_LAST_; ++i) {
135*eca53ba6SRoland Levillain     if (CpuFeatures_IsHwCapsSet(kHardwareCapabilities[i], hwcaps)) {
136*eca53ba6SRoland Levillain       kSetters[i](&info.features, true);
137*eca53ba6SRoland Levillain     }
138*eca53ba6SRoland Levillain   }
139*eca53ba6SRoland Levillain   return info;
140*eca53ba6SRoland Levillain }
141*eca53ba6SRoland Levillain 
142*eca53ba6SRoland Levillain static const PPCPlatformStrings kEmptyPPCPlatformStrings;
143*eca53ba6SRoland Levillain 
GetPPCPlatformStrings(void)144*eca53ba6SRoland Levillain PPCPlatformStrings GetPPCPlatformStrings(void) {
145*eca53ba6SRoland Levillain   PPCPlatformStrings strings = kEmptyPPCPlatformStrings;
146*eca53ba6SRoland Levillain   const char* platform = CpuFeatures_GetPlatformPointer();
147*eca53ba6SRoland Levillain   const char* base_platform = CpuFeatures_GetBasePlatformPointer();
148*eca53ba6SRoland Levillain 
149*eca53ba6SRoland Levillain   FillProcCpuInfoData(&strings);
150*eca53ba6SRoland Levillain 
151*eca53ba6SRoland Levillain   if (platform != NULL)
152*eca53ba6SRoland Levillain     CpuFeatures_StringView_CopyString(str(platform), strings.type.platform,
153*eca53ba6SRoland Levillain                                       sizeof(strings.type.platform));
154*eca53ba6SRoland Levillain   if (base_platform != NULL)
155*eca53ba6SRoland Levillain     CpuFeatures_StringView_CopyString(str(base_platform),
156*eca53ba6SRoland Levillain                                       strings.type.base_platform,
157*eca53ba6SRoland Levillain                                       sizeof(strings.type.base_platform));
158*eca53ba6SRoland Levillain 
159*eca53ba6SRoland Levillain   return strings;
160*eca53ba6SRoland Levillain }
161*eca53ba6SRoland Levillain 
162*eca53ba6SRoland Levillain #endif  // CPU_FEATURES_OS_LINUX
163*eca53ba6SRoland Levillain #endif  // CPU_FEATURES_ARCH_PPC
164