3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
18 #include "cpuinfo_ppc.h"
19 #include "internal/bit_utils.h"
20 #include "internal/filesystem.h"
21 #include "internal/linux_features_aggregator.h"
22 #include "internal/stack_line_reader.h"
23 #include "internal/string_view.h"
25 DECLARE_SETTER(PPCFeatures, ppc32)
26 DECLARE_SETTER(PPCFeatures, ppc64)
27 DECLARE_SETTER(PPCFeatures, ppc601)
28 DECLARE_SETTER(PPCFeatures, altivec)
29 DECLARE_SETTER(PPCFeatures, fpu)
30 DECLARE_SETTER(PPCFeatures, mmu)
31 DECLARE_SETTER(PPCFeatures, mac_4xx)
32 DECLARE_SETTER(PPCFeatures, unifiedcache)
33 DECLARE_SETTER(PPCFeatures, spe)
34 DECLARE_SETTER(PPCFeatures, efpsingle)
35 DECLARE_SETTER(PPCFeatures, efpdouble)
36 DECLARE_SETTER(PPCFeatures, no_tb)
37 DECLARE_SETTER(PPCFeatures, power4)
38 DECLARE_SETTER(PPCFeatures, power5)
39 DECLARE_SETTER(PPCFeatures, power5plus)
40 DECLARE_SETTER(PPCFeatures, cell)
41 DECLARE_SETTER(PPCFeatures, booke)
42 DECLARE_SETTER(PPCFeatures, smt)
43 DECLARE_SETTER(PPCFeatures, icachesnoop)
44 DECLARE_SETTER(PPCFeatures, arch205)
45 DECLARE_SETTER(PPCFeatures, pa6t)
46 DECLARE_SETTER(PPCFeatures, dfp)
47 DECLARE_SETTER(PPCFeatures, power6ext)
48 DECLARE_SETTER(PPCFeatures, arch206)
49 DECLARE_SETTER(PPCFeatures, vsx)
50 DECLARE_SETTER(PPCFeatures, pseries_perfmon_compat)
51 DECLARE_SETTER(PPCFeatures, truele)
52 DECLARE_SETTER(PPCFeatures, ppcle)
53 DECLARE_SETTER(PPCFeatures, arch207)
54 DECLARE_SETTER(PPCFeatures, htm)
55 DECLARE_SETTER(PPCFeatures, dscr)
56 DECLARE_SETTER(PPCFeatures, ebb)
57 DECLARE_SETTER(PPCFeatures, isel)
58 DECLARE_SETTER(PPCFeatures, tar)
59 DECLARE_SETTER(PPCFeatures, vcrypto)
60 DECLARE_SETTER(PPCFeatures, htm_nosc)
61 DECLARE_SETTER(PPCFeatures, arch300)
62 DECLARE_SETTER(PPCFeatures, ieee128)
63 DECLARE_SETTER(PPCFeatures, darn)
64 DECLARE_SETTER(PPCFeatures, scv)
65 DECLARE_SETTER(PPCFeatures, htm_no_suspend)
67 static const CapabilityConfig kConfigs[] = {
68 {{PPC_FEATURE_32, 0}, "ppc32", &set_ppc32},
69 {{PPC_FEATURE_64, 0}, "ppc64", &set_ppc64},
70 {{PPC_FEATURE_601_INSTR, 0}, "ppc601", &set_ppc601},
71 {{PPC_FEATURE_HAS_ALTIVEC, 0}, "altivec", &set_altivec},
72 {{PPC_FEATURE_HAS_FPU, 0}, "fpu", &set_fpu},
73 {{PPC_FEATURE_HAS_MMU, 0}, "mmu", &set_mmu},
74 {{PPC_FEATURE_HAS_4xxMAC, 0}, "4xxmac", &set_mac_4xx},
75 {{PPC_FEATURE_UNIFIED_CACHE, 0}, "ucache", &set_unifiedcache},
76 {{PPC_FEATURE_HAS_SPE, 0}, "spe", &set_spe},
77 {{PPC_FEATURE_HAS_EFP_SINGLE, 0}, "efpsingle", &set_efpsingle},
78 {{PPC_FEATURE_HAS_EFP_DOUBLE, 0}, "efpdouble", &set_efpdouble},
79 {{PPC_FEATURE_NO_TB, 0}, "notb", &set_no_tb},
80 {{PPC_FEATURE_POWER4, 0}, "power4", &set_power4},
81 {{PPC_FEATURE_POWER5, 0}, "power5", &set_power5},
82 {{PPC_FEATURE_POWER5_PLUS, 0}, "power5+", &set_power5plus},
83 {{PPC_FEATURE_CELL, 0}, "cellbe", &set_cell},
84 {{PPC_FEATURE_BOOKE, 0}, "booke", &set_booke},
85 {{PPC_FEATURE_SMT, 0}, "smt", &set_smt},
86 {{PPC_FEATURE_ICACHE_SNOOP, 0}, "ic_snoop", &set_icachesnoop},
87 {{PPC_FEATURE_ARCH_2_05, 0}, "arch_2_05", &set_arch205},
88 {{PPC_FEATURE_PA6T, 0}, "pa6t", &set_pa6t},
89 {{PPC_FEATURE_HAS_DFP, 0}, "dfp", &set_dfp},
90 {{PPC_FEATURE_POWER6_EXT, 0}, "power6x", &set_power6ext},
91 {{PPC_FEATURE_ARCH_2_06, 0}, "arch_2_06", &set_arch206},
92 {{PPC_FEATURE_HAS_VSX, 0}, "vsx", &set_vsx},
93 {{PPC_FEATURE_PSERIES_PERFMON_COMPAT, 0},
95 &set_pseries_perfmon_compat},
96 {{PPC_FEATURE_TRUE_LE, 0}, "true_le", &set_truele},
97 {{PPC_FEATURE_PPC_LE, 0}, "ppcle", &set_ppcle},
98 {{0, PPC_FEATURE2_ARCH_2_07}, "arch_2_07", &set_arch207},
99 {{0, PPC_FEATURE2_HTM}, "htm", &set_htm},
100 {{0, PPC_FEATURE2_DSCR}, "dscr", &set_dscr},
101 {{0, PPC_FEATURE2_EBB}, "ebb", &set_ebb},
102 {{0, PPC_FEATURE2_ISEL}, "isel", &set_isel},
103 {{0, PPC_FEATURE2_TAR}, "tar", &set_tar},
104 {{0, PPC_FEATURE2_VEC_CRYPTO}, "vcrypto", &set_vcrypto},
105 {{0, PPC_FEATURE2_HTM_NOSC}, "htm-nosc", &set_htm_nosc},
106 {{0, PPC_FEATURE2_ARCH_3_00}, "arch_3_00", &set_arch300},
107 {{0, PPC_FEATURE2_HAS_IEEE128}, "ieee128", &set_ieee128},
108 {{0, PPC_FEATURE2_DARN}, "darn", &set_darn},
109 {{0, PPC_FEATURE2_SCV}, "scv", &set_scv},
110 {{0, PPC_FEATURE2_HTM_NO_SUSPEND}, "htm-no-suspend", &set_htm_no_suspend},
112 static const size_t kConfigsSize = sizeof(kConfigs) / sizeof(CapabilityConfig);
114 static bool HandlePPCLine(const LineResult result,
115 PPCPlatformStrings* const strings) {
116 StringView line = result.line;
117 StringView key, value;
118 if (CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)) {
119 if (CpuFeatures_StringView_HasWord(key, "platform")) {
120 CpuFeatures_StringView_CopyString(value, strings->platform,
121 sizeof(strings->platform));
122 } else if (CpuFeatures_StringView_IsEquals(key, str("model"))) {
123 CpuFeatures_StringView_CopyString(value, strings->model,
124 sizeof(strings->platform));
125 } else if (CpuFeatures_StringView_IsEquals(key, str("machine"))) {
126 CpuFeatures_StringView_CopyString(value, strings->machine,
127 sizeof(strings->platform));
128 } else if (CpuFeatures_StringView_IsEquals(key, str("cpu"))) {
129 CpuFeatures_StringView_CopyString(value, strings->cpu,
130 sizeof(strings->platform));
136 static void FillProcCpuInfoData(PPCPlatformStrings* const strings) {
137 const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
139 StackLineReader reader;
140 StackLineReader_Initialize(&reader, fd);
142 if (!HandlePPCLine(StackLineReader_NextLine(&reader), strings)) {
146 CpuFeatures_CloseFile(fd);
150 static const PPCInfo kEmptyPPCInfo;
152 PPCInfo GetPPCInfo(void) {
154 * On Power feature flags aren't currently in cpuinfo so we only look at
155 * the auxilary vector.
157 PPCInfo info = kEmptyPPCInfo;
159 CpuFeatures_OverrideFromHwCaps(kConfigsSize, kConfigs,
160 CpuFeatures_GetHardwareCapabilities(),
165 static const PPCPlatformStrings kEmptyPPCPlatformStrings;
167 PPCPlatformStrings GetPPCPlatformStrings(void) {
168 PPCPlatformStrings strings = kEmptyPPCPlatformStrings;
170 FillProcCpuInfoData(&strings);
171 strings.type = CpuFeatures_GetPlatformType();
175 ////////////////////////////////////////////////////////////////////////////////
176 // Introspection functions
178 int GetPPCFeaturesEnumValue(const PPCFeatures* features,
179 PPCFeaturesEnum value) {
182 return features->ppc32;
184 return features->ppc64;
186 return features->ppc601;
187 case PPC_HAS_ALTIVEC:
188 return features->altivec;
190 return features->fpu;
192 return features->mmu;
194 return features->mac_4xx;
195 case PPC_UNIFIED_CACHE:
196 return features->unifiedcache;
198 return features->spe;
199 case PPC_HAS_EFP_SINGLE:
200 return features->efpsingle;
201 case PPC_HAS_EFP_DOUBLE:
202 return features->efpdouble;
204 return features->no_tb;
206 return features->power4;
208 return features->power5;
209 case PPC_POWER5_PLUS:
210 return features->power5plus;
212 return features->cell;
214 return features->booke;
216 return features->smt;
217 case PPC_ICACHE_SNOOP:
218 return features->icachesnoop;
220 return features->arch205;
222 return features->pa6t;
224 return features->dfp;
226 return features->power6ext;
228 return features->arch206;
230 return features->vsx;
231 case PPC_PSERIES_PERFMON_COMPAT:
232 return features->pseries_perfmon_compat;
234 return features->truele;
236 return features->ppcle;
238 return features->arch207;
240 return features->htm;
242 return features->dscr;
244 return features->ebb;
246 return features->isel;
248 return features->tar;
250 return features->vcrypto;
252 return features->htm_nosc;
254 return features->arch300;
255 case PPC_HAS_IEEE128:
256 return features->ieee128;
258 return features->darn;
260 return features->scv;
261 case PPC_HTM_NO_SUSPEND:
262 return features->htm_no_suspend;
269 /* Have used the same names as glibc */
270 const char* GetPPCFeaturesEnumName(PPCFeaturesEnum value) {
278 case PPC_HAS_ALTIVEC:
286 case PPC_UNIFIED_CACHE:
290 case PPC_HAS_EFP_SINGLE:
292 case PPC_HAS_EFP_DOUBLE:
300 case PPC_POWER5_PLUS:
308 case PPC_ICACHE_SNOOP:
322 case PPC_PSERIES_PERFMON_COMPAT:
346 case PPC_HAS_IEEE128:
352 case PPC_HTM_NO_SUSPEND:
353 return "htm-no-suspend";
357 return "unknown_feature";