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