CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/LoongArchCPUDetect.cpp
Views: 1401
1
// Copyright (C) 2003 Dolphin Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official SVN repository and contact information can be found at
16
// http://code.google.com/p/dolphin-emu/
17
18
#include "ppsspp_config.h"
19
#if PPSSPP_ARCH(LOONGARCH64)
20
21
#include "ext/cpu_features/include/cpuinfo_loongarch.h"
22
23
#if defined(CPU_FEATURES_OS_LINUX)
24
#define USE_CPU_FEATURES 1
25
#endif
26
27
#include <cstring>
28
#include <set>
29
#include <sstream>
30
#include <sys/auxv.h>
31
#include <vector>
32
#include "Common/Common.h"
33
#include "Common/CPUDetect.h"
34
#include "Common/StringUtils.h"
35
#include "Common/File/FileUtil.h"
36
#include "Common/Data/Encoding/Utf8.h"
37
38
// Only Linux platforms have /proc/cpuinfo
39
#if defined(__linux__)
40
const char procfile[] = "/proc/cpuinfo";
41
// https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-devices-system-cpu
42
const char syscpupresentfile[] = "/sys/devices/system/cpu/present";
43
44
class LoongArchCPUInfoParser {
45
public:
46
LoongArchCPUInfoParser();
47
48
int ProcessorCount();
49
int TotalLogicalCount();
50
51
private:
52
std::vector<std::vector<std::string>> cores_;
53
};
54
55
LoongArchCPUInfoParser::LoongArchCPUInfoParser() {
56
std::string procdata, line;
57
if (!File::ReadSysTextFileToString(Path(procfile), &procdata))
58
return;
59
60
std::istringstream file(procdata);
61
int index = -1;
62
while (std::getline(file, line)) {
63
if (line.length() == 0) {
64
index = -1;
65
} else {
66
if (index == -1) {
67
index = (int)cores_.size();
68
cores_.push_back(std::vector<std::string>());
69
}
70
cores_[index].push_back(line);
71
}
72
}
73
}
74
75
int LoongArchCPUInfoParser::ProcessorCount() {
76
static const char * const marker = "core";
77
std::set<std::string> coreIndex;
78
for (auto core : cores_) {
79
for (auto line : core) {
80
if (line.find(marker) != line.npos)
81
coreIndex.insert(line);
82
}
83
}
84
85
return (int)coreIndex.size();
86
}
87
88
int LoongArchCPUInfoParser::TotalLogicalCount() {
89
std::string presentData, line;
90
bool presentSuccess = File::ReadSysTextFileToString(Path(syscpupresentfile), &presentData);
91
if (presentSuccess) {
92
std::istringstream presentFile(presentData);
93
94
int low, high, found;
95
std::getline(presentFile, line);
96
found = sscanf(line.c_str(), "%d-%d", &low, &high);
97
if (found == 1){
98
return 1;
99
}
100
if (found == 2){
101
return high - low + 1;
102
}
103
}else{
104
return 1;
105
}
106
}
107
108
#endif
109
110
static bool ExtensionSupported(unsigned long v, unsigned int i) {
111
// https://github.com/torvalds/linux/blob/master/arch/loongarch/include/uapi/asm/hwcap.h
112
unsigned long mask = 1 << i;
113
return v & mask;
114
}
115
116
CPUInfo cpu_info;
117
118
CPUInfo::CPUInfo() {
119
Detect();
120
}
121
122
// Detects the various cpu features
123
void CPUInfo::Detect()
124
{
125
// Set some defaults here
126
HTT = false;
127
#if PPSSPP_ARCH(LOONGARCH64)
128
OS64bit = true;
129
CPU64bit = true;
130
Mode64bit = true;
131
#else
132
OS64bit = false;
133
CPU64bit = false;
134
Mode64bit = false;
135
#endif
136
vendor = VENDOR_OTHER;
137
truncate_cpy(brand_string, "Loongson");
138
truncate_cpy(cpu_string, "Loongson");
139
#if !defined(__linux__)
140
num_cores = 1;
141
logical_cpu_count = 1;
142
truncate_cpy(brand_string, "Unknown");
143
truncate_cpy(cpu_string, "Unknown");
144
#else // __linux__
145
LoongArchCPUInfoParser parser;
146
num_cores = parser.ProcessorCount();
147
logical_cpu_count = parser.TotalLogicalCount() / num_cores;
148
if (logical_cpu_count <= 0)
149
logical_cpu_count = 1;
150
#endif
151
152
unsigned long hwcap = getauxval(AT_HWCAP);
153
LOONGARCH_CPUCFG = true;
154
LOONGARCH_LAM = ExtensionSupported(hwcap, 1);
155
LOONGARCH_UAL = ExtensionSupported(hwcap, 2);
156
LOONGARCH_FPU = ExtensionSupported(hwcap, 3);
157
LOONGARCH_LSX = ExtensionSupported(hwcap, 4);
158
LOONGARCH_LASX = ExtensionSupported(hwcap, 5);
159
LOONGARCH_CRC32 = ExtensionSupported(hwcap, 6);
160
LOONGARCH_COMPLEX = ExtensionSupported(hwcap, 7);
161
LOONGARCH_CRYPTO = ExtensionSupported(hwcap, 8);
162
LOONGARCH_LVZ = ExtensionSupported(hwcap, 9);
163
LOONGARCH_LBT_X86 = ExtensionSupported(hwcap, 10);
164
LOONGARCH_LBT_ARM = ExtensionSupported(hwcap, 11);
165
LOONGARCH_LBT_MIPS = ExtensionSupported(hwcap, 12);
166
LOONGARCH_PTW = ExtensionSupported(hwcap, 13);
167
168
#ifdef USE_CPU_FEATURES
169
cpu_features::LoongArchInfo info = cpu_features::GetLoongArchInfo();
170
LOONGARCH_CPUCFG = true;
171
LOONGARCH_LAM = info.features.LAM;
172
LOONGARCH_UAL = info.features.UAL;
173
LOONGARCH_FPU = info.features.FPU;
174
LOONGARCH_LSX = info.features.LSX;
175
LOONGARCH_LASX = info.features.LASX;
176
LOONGARCH_CRC32 = info.features.CRC32;
177
LOONGARCH_COMPLEX = info.features.COMPLEX;
178
LOONGARCH_CRYPTO = info.features.CRYPTO;
179
LOONGARCH_LVZ = info.features.LVZ;
180
LOONGARCH_LBT_X86 = info.features.LBT_X86;
181
LOONGARCH_LBT_ARM = info.features.LBT_ARM;
182
LOONGARCH_LBT_MIPS = info.features.LBT_MIPS;
183
LOONGARCH_PTW = info.features.PTW;
184
#endif
185
}
186
187
std::vector<std::string> CPUInfo::Features() {
188
std::vector<std::string> features;
189
190
struct Flag {
191
bool &flag;
192
const char *str;
193
};
194
const Flag list[] = {
195
{ LOONGARCH_CPUCFG, "Identify CPU Features" },
196
{ LOONGARCH_LAM, "Atomic Memory Access Instructions" },
197
{ LOONGARCH_UAL, "Non-Aligned Memory Access" },
198
{ LOONGARCH_FPU, "Basic Floating-Point Instructions" },
199
{ LOONGARCH_LSX, "Loongson SIMD eXtension" },
200
{ LOONGARCH_LASX, "Loongson Advanced SIMD eXtension" },
201
{ LOONGARCH_CRC32, "Cyclic Redundancy Check Instructions" },
202
{ LOONGARCH_COMPLEX, "Complex Vector Operation Instructions" },
203
{ LOONGARCH_CRYPTO, "Encryption And Decryption Vector Instructions" },
204
{ LOONGARCH_LVZ, "Virtualization" },
205
{ LOONGARCH_LBT_X86, "X86 Binary Translation Extension" },
206
{ LOONGARCH_LBT_ARM, "ARM Binary Translation Extension" },
207
{ LOONGARCH_LBT_MIPS, "MIPS Binary Translation Extension" },
208
{ LOONGARCH_PTW, "Page Table Walker" },
209
};
210
211
for (auto &item : list) {
212
if (item.flag) {
213
features.push_back(item.str);
214
}
215
}
216
217
return features;
218
}
219
220
// Turn the cpu info into a string we can show
221
std::string CPUInfo::Summarize() {
222
std::string sum;
223
if (num_cores == 1)
224
sum = StringFromFormat("%s, %i core", cpu_string, num_cores);
225
else
226
sum = StringFromFormat("%s, %i cores", cpu_string, num_cores);
227
228
auto features = Features();
229
for (std::string &feature : features) {
230
sum += ", " + feature;
231
}
232
return sum;
233
}
234
235
#endif // PPSSPP_ARCH(LOONGARCH64)
236
237