1*eca53ba6SRoland Levillain // Copyright 2023 Google LLC
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_LOONGARCH
18*eca53ba6SRoland Levillain #if defined(CPU_FEATURES_OS_LINUX)
19*eca53ba6SRoland Levillain
20*eca53ba6SRoland Levillain #include "cpuinfo_loongarch.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(LOONGARCH_CPUCFG, CPUCFG, "cfg", HWCAP_LOONGARCH_CPUCFG, 0) \
27*eca53ba6SRoland Levillain LINE(LOONGARCH_LAM, LAM, "lam", HWCAP_LOONGARCH_LAM, 0) \
28*eca53ba6SRoland Levillain LINE(LOONGARCH_UAL, UAL, "ual", HWCAP_LOONGARCH_UAL, 0) \
29*eca53ba6SRoland Levillain LINE(LOONGARCH_FPU, FPU, "fpu", HWCAP_LOONGARCH_FPU, 0) \
30*eca53ba6SRoland Levillain LINE(LOONGARCH_LSX, LSX, "lsx", HWCAP_LOONGARCH_LSX, 0) \
31*eca53ba6SRoland Levillain LINE(LOONGARCH_LASX, LASX, "lasx", HWCAP_LOONGARCH_LASX, 0) \
32*eca53ba6SRoland Levillain LINE(LOONGARCH_CRC32, CRC32, "crc32", HWCAP_LOONGARCH_CRC32, 0) \
33*eca53ba6SRoland Levillain LINE(LOONGARCH_COMPLEX, COMPLEX, "complex", HWCAP_LOONGARCH_COMPLEX, 0) \
34*eca53ba6SRoland Levillain LINE(LOONGARCH_CRYPTO, CRYPTO, "crypto", HWCAP_LOONGARCH_CRYPTO, 0) \
35*eca53ba6SRoland Levillain LINE(LOONGARCH_LVZ, LVZ, "lvz", HWCAP_LOONGARCH_LVZ, 0) \
36*eca53ba6SRoland Levillain LINE(LOONGARCH_LBT_X86, LBT_X86, "lbt_x86", HWCAP_LOONGARCH_LBT_X86, 0) \
37*eca53ba6SRoland Levillain LINE(LOONGARCH_LBT_ARM, LBT_ARM, "lbt_arm", HWCAP_LOONGARCH_LBT_ARM, 0) \
38*eca53ba6SRoland Levillain LINE(LOONGARCH_LBT_MIPS, LBT_MIPS, "lbt_mips", HWCAP_LOONGARCH_LBT_MIPS, 0) \
39*eca53ba6SRoland Levillain LINE(LOONGARCH_PTW, PTW, "ptw", HWCAP_LOONGARCH_PTW, 0)
40*eca53ba6SRoland Levillain #define INTROSPECTION_PREFIX LoongArch
41*eca53ba6SRoland Levillain #define INTROSPECTION_ENUM_PREFIX LOONGARCH
42*eca53ba6SRoland Levillain #include "define_introspection_and_hwcaps.inl"
43*eca53ba6SRoland Levillain
44*eca53ba6SRoland Levillain ////////////////////////////////////////////////////////////////////////////////
45*eca53ba6SRoland Levillain // Implementation.
46*eca53ba6SRoland Levillain ////////////////////////////////////////////////////////////////////////////////
47*eca53ba6SRoland Levillain
48*eca53ba6SRoland Levillain #include <stdbool.h>
49*eca53ba6SRoland Levillain #include <stdio.h>
50*eca53ba6SRoland Levillain
51*eca53ba6SRoland Levillain #include "internal/filesystem.h"
52*eca53ba6SRoland Levillain #include "internal/stack_line_reader.h"
53*eca53ba6SRoland Levillain
54*eca53ba6SRoland Levillain static const LoongArchInfo kEmptyLoongArchInfo;
55*eca53ba6SRoland Levillain
HandleLoongArchLine(const LineResult result,LoongArchInfo * const info)56*eca53ba6SRoland Levillain static bool HandleLoongArchLine(const LineResult result, LoongArchInfo* const info) {
57*eca53ba6SRoland Levillain StringView line = result.line;
58*eca53ba6SRoland Levillain StringView key, value;
59*eca53ba6SRoland Levillain if (CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)) {
60*eca53ba6SRoland Levillain if (CpuFeatures_StringView_IsEquals(key, str("Features"))) {
61*eca53ba6SRoland Levillain for (size_t i = 0; i < LOONGARCH_LAST_; ++i) {
62*eca53ba6SRoland Levillain kSetters[i](&info->features, CpuFeatures_StringView_HasWord(
63*eca53ba6SRoland Levillain value, kCpuInfoFlags[i], ' '));
64*eca53ba6SRoland Levillain }
65*eca53ba6SRoland Levillain }
66*eca53ba6SRoland Levillain }
67*eca53ba6SRoland Levillain return !result.eof;
68*eca53ba6SRoland Levillain }
69*eca53ba6SRoland Levillain
FillProcCpuInfoData(LoongArchInfo * const info)70*eca53ba6SRoland Levillain static void FillProcCpuInfoData(LoongArchInfo* const info) {
71*eca53ba6SRoland Levillain const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
72*eca53ba6SRoland Levillain if (fd >= 0) {
73*eca53ba6SRoland Levillain StackLineReader reader;
74*eca53ba6SRoland Levillain StackLineReader_Initialize(&reader, fd);
75*eca53ba6SRoland Levillain for (;;) {
76*eca53ba6SRoland Levillain if (!HandleLoongArchLine(StackLineReader_NextLine(&reader), info)) break;
77*eca53ba6SRoland Levillain }
78*eca53ba6SRoland Levillain CpuFeatures_CloseFile(fd);
79*eca53ba6SRoland Levillain }
80*eca53ba6SRoland Levillain }
81*eca53ba6SRoland Levillain
GetLoongArchInfo(void)82*eca53ba6SRoland Levillain LoongArchInfo GetLoongArchInfo(void) {
83*eca53ba6SRoland Levillain LoongArchInfo info = kEmptyLoongArchInfo;
84*eca53ba6SRoland Levillain FillProcCpuInfoData(&info);
85*eca53ba6SRoland Levillain return info;
86*eca53ba6SRoland Levillain }
87*eca53ba6SRoland Levillain
88*eca53ba6SRoland Levillain #endif // defined(CPU_FEATURES_OS_LINUX)
89*eca53ba6SRoland Levillain #endif // CPU_FEATURES_ARCH_LOONGARCH
90