xref: /aosp_15_r20/external/cpu_features/src/impl_s390x_linux.c (revision eca53ba6d2e951e174b64682eaf56a36b8204c89)
1*eca53ba6SRoland Levillain // Copyright 2022 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_S390X
18*eca53ba6SRoland Levillain #ifdef CPU_FEATURES_OS_LINUX
19*eca53ba6SRoland Levillain 
20*eca53ba6SRoland Levillain #include "cpuinfo_s390x.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(S390_ESAN3, esan3, "esan3", HWCAP_S390_ESAN3, 0)                              \
27*eca53ba6SRoland Levillain   LINE(S390_ZARCH, zarch, "zarch", HWCAP_S390_ZARCH, 0)                              \
28*eca53ba6SRoland Levillain   LINE(S390_STFLE, stfle, "stfle", HWCAP_S390_STFLE, 0)                              \
29*eca53ba6SRoland Levillain   LINE(S390_MSA, msa, "msa", HWCAP_S390_MSA, 0)                                      \
30*eca53ba6SRoland Levillain   LINE(S390_LDISP, ldisp, "ldisp", HWCAP_S390_LDISP, 0)                              \
31*eca53ba6SRoland Levillain   LINE(S390_EIMM, eimm, "eimm", HWCAP_S390_EIMM, 0)                                  \
32*eca53ba6SRoland Levillain   LINE(S390_DFP, dfp, "dfp", HWCAP_S390_DFP, 0)                                      \
33*eca53ba6SRoland Levillain   LINE(S390_EDAT, edat, "edat", HWCAP_S390_HPAGE, 0)                                 \
34*eca53ba6SRoland Levillain   LINE(S390_ETF3EH, etf3eh, "etf3eh", HWCAP_S390_ETF3EH, 0)                          \
35*eca53ba6SRoland Levillain   LINE(S390_HIGHGPRS, highgprs, "highgprs", HWCAP_S390_HIGH_GPRS, 0)                 \
36*eca53ba6SRoland Levillain   LINE(S390_TE, te, "te", HWCAP_S390_TE, 0)                                          \
37*eca53ba6SRoland Levillain   LINE(S390_VX, vx, "vx", HWCAP_S390_VXRS, 0)                                        \
38*eca53ba6SRoland Levillain   LINE(S390_VXD, vxd, "vxd", HWCAP_S390_VXRS_BCD, 0)                                 \
39*eca53ba6SRoland Levillain   LINE(S390_VXE, vxe, "vxe", HWCAP_S390_VXRS_EXT, 0)                                 \
40*eca53ba6SRoland Levillain   LINE(S390_GS, gs, "gs", HWCAP_S390_GS, 0)                                          \
41*eca53ba6SRoland Levillain   LINE(S390_VXE2, vxe2, "vxe2", HWCAP_S390_VXRS_EXT2, 0)                             \
42*eca53ba6SRoland Levillain   LINE(S390_VXP, vxp, "vxp", HWCAP_S390_VXRS_PDE, 0)                                 \
43*eca53ba6SRoland Levillain   LINE(S390_SORT, sort, "sort", HWCAP_S390_SORT, 0)                                  \
44*eca53ba6SRoland Levillain   LINE(S390_DFLT, dflt, "dflt", HWCAP_S390_DFLT, 0)                                  \
45*eca53ba6SRoland Levillain   LINE(S390_VXP2, vxp2, "vxp2", HWCAP_S390_VXRS_PDE2, 0)                             \
46*eca53ba6SRoland Levillain   LINE(S390_NNPA, nnpa, "nnpa", HWCAP_S390_NNPA, 0)                                  \
47*eca53ba6SRoland Levillain   LINE(S390_PCIMIO, pcimio, "pcimio", HWCAP_S390_PCI_MIO, 0)                         \
48*eca53ba6SRoland Levillain   LINE(S390_SIE, sie, "sie", HWCAP_S390_SIE, 0)
49*eca53ba6SRoland Levillain #define INTROSPECTION_PREFIX S390X
50*eca53ba6SRoland Levillain #define INTROSPECTION_ENUM_PREFIX S390X
51*eca53ba6SRoland Levillain #include "define_introspection_and_hwcaps.inl"
52*eca53ba6SRoland Levillain 
53*eca53ba6SRoland Levillain ////////////////////////////////////////////////////////////////////////////////
54*eca53ba6SRoland Levillain // Implementation.
55*eca53ba6SRoland Levillain ////////////////////////////////////////////////////////////////////////////////
56*eca53ba6SRoland Levillain 
57*eca53ba6SRoland Levillain #include <stdbool.h>
58*eca53ba6SRoland Levillain 
59*eca53ba6SRoland Levillain #include "internal/bit_utils.h"
60*eca53ba6SRoland Levillain #include "internal/filesystem.h"
61*eca53ba6SRoland Levillain #include "internal/hwcaps.h"
62*eca53ba6SRoland Levillain #include "internal/stack_line_reader.h"
63*eca53ba6SRoland Levillain #include "internal/string_view.h"
64*eca53ba6SRoland Levillain 
HandleS390XLine(const LineResult result,S390XPlatformStrings * const strings)65*eca53ba6SRoland Levillain static bool HandleS390XLine(const LineResult result,
66*eca53ba6SRoland Levillain                           S390XPlatformStrings* const strings) {
67*eca53ba6SRoland Levillain   StringView line = result.line;
68*eca53ba6SRoland Levillain   StringView key, value;
69*eca53ba6SRoland Levillain   if (CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)) {
70*eca53ba6SRoland Levillain     if (CpuFeatures_StringView_IsEquals(key, str("# processors"))) {
71*eca53ba6SRoland Levillain         strings->num_processors = CpuFeatures_StringView_ParsePositiveNumber(value);
72*eca53ba6SRoland Levillain     }
73*eca53ba6SRoland Levillain   }
74*eca53ba6SRoland Levillain   return !result.eof;
75*eca53ba6SRoland Levillain }
76*eca53ba6SRoland Levillain 
FillProcCpuInfoData(S390XPlatformStrings * const strings)77*eca53ba6SRoland Levillain static void FillProcCpuInfoData(S390XPlatformStrings* const strings) {
78*eca53ba6SRoland Levillain   const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
79*eca53ba6SRoland Levillain   if (fd >= 0) {
80*eca53ba6SRoland Levillain     StackLineReader reader;
81*eca53ba6SRoland Levillain     StackLineReader_Initialize(&reader, fd);
82*eca53ba6SRoland Levillain     for (;;) {
83*eca53ba6SRoland Levillain       if (!HandleS390XLine(StackLineReader_NextLine(&reader), strings)) {
84*eca53ba6SRoland Levillain         break;
85*eca53ba6SRoland Levillain       }
86*eca53ba6SRoland Levillain     }
87*eca53ba6SRoland Levillain     CpuFeatures_CloseFile(fd);
88*eca53ba6SRoland Levillain   }
89*eca53ba6SRoland Levillain }
90*eca53ba6SRoland Levillain 
91*eca53ba6SRoland Levillain static const S390XInfo kEmptyS390XInfo;
92*eca53ba6SRoland Levillain 
GetS390XInfo(void)93*eca53ba6SRoland Levillain S390XInfo GetS390XInfo(void) {
94*eca53ba6SRoland Levillain   S390XInfo info = kEmptyS390XInfo;
95*eca53ba6SRoland Levillain   const HardwareCapabilities hwcaps = CpuFeatures_GetHardwareCapabilities();
96*eca53ba6SRoland Levillain   for (size_t i = 0; i < S390X_LAST_; ++i) {
97*eca53ba6SRoland Levillain     if (CpuFeatures_IsHwCapsSet(kHardwareCapabilities[i], hwcaps)) {
98*eca53ba6SRoland Levillain       kSetters[i](&info.features, true);
99*eca53ba6SRoland Levillain     }
100*eca53ba6SRoland Levillain   }
101*eca53ba6SRoland Levillain   return info;
102*eca53ba6SRoland Levillain }
103*eca53ba6SRoland Levillain 
104*eca53ba6SRoland Levillain static const S390XPlatformStrings kEmptyS390XPlatformStrings;
105*eca53ba6SRoland Levillain 
GetS390XPlatformStrings(void)106*eca53ba6SRoland Levillain S390XPlatformStrings GetS390XPlatformStrings(void) {
107*eca53ba6SRoland Levillain   S390XPlatformStrings strings = kEmptyS390XPlatformStrings;
108*eca53ba6SRoland Levillain   const char* platform = CpuFeatures_GetPlatformPointer();
109*eca53ba6SRoland Levillain 
110*eca53ba6SRoland Levillain   FillProcCpuInfoData(&strings);
111*eca53ba6SRoland Levillain 
112*eca53ba6SRoland Levillain   if (platform != NULL)
113*eca53ba6SRoland Levillain     CpuFeatures_StringView_CopyString(str(platform), strings.type.platform,
114*eca53ba6SRoland Levillain                                       sizeof(strings.type.platform));
115*eca53ba6SRoland Levillain 
116*eca53ba6SRoland Levillain   return strings;
117*eca53ba6SRoland Levillain }
118*eca53ba6SRoland Levillain 
119*eca53ba6SRoland Levillain #endif  // CPU_FEATURES_OS_LINUX
120*eca53ba6SRoland Levillain #endif  // CPU_FEATURES_ARCH_S390X
121