Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/LoongArchCPUDetect.cpp
3185 views
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 == 2){
98
return high - low + 1;
99
}
100
}
101
return 1;
102
}
103
104
#endif
105
106
static bool ExtensionSupported(unsigned long v, unsigned int i) {
107
// https://github.com/torvalds/linux/blob/master/arch/loongarch/include/uapi/asm/hwcap.h
108
unsigned long mask = 1 << i;
109
return v & mask;
110
}
111
112
CPUInfo cpu_info;
113
114
CPUInfo::CPUInfo() {
115
Detect();
116
}
117
118
// Detects the various cpu features
119
void CPUInfo::Detect()
120
{
121
// Set some defaults here
122
HTT = false;
123
#if PPSSPP_ARCH(LOONGARCH64)
124
OS64bit = true;
125
CPU64bit = true;
126
Mode64bit = true;
127
#else
128
OS64bit = false;
129
CPU64bit = false;
130
Mode64bit = false;
131
#endif
132
vendor = VENDOR_OTHER;
133
truncate_cpy(brand_string, "Loongson");
134
truncate_cpy(cpu_string, "Loongson");
135
#if !defined(__linux__)
136
num_cores = 1;
137
logical_cpu_count = 1;
138
truncate_cpy(brand_string, "Unknown");
139
truncate_cpy(cpu_string, "Unknown");
140
#else // __linux__
141
LoongArchCPUInfoParser parser;
142
num_cores = parser.ProcessorCount();
143
logical_cpu_count = parser.TotalLogicalCount() / num_cores;
144
if (logical_cpu_count <= 0)
145
logical_cpu_count = 1;
146
#endif
147
148
unsigned long hwcap = getauxval(AT_HWCAP);
149
LOONGARCH_CPUCFG = true;
150
LOONGARCH_LAM = ExtensionSupported(hwcap, 1);
151
LOONGARCH_UAL = ExtensionSupported(hwcap, 2);
152
LOONGARCH_FPU = ExtensionSupported(hwcap, 3);
153
LOONGARCH_LSX = ExtensionSupported(hwcap, 4);
154
LOONGARCH_LASX = ExtensionSupported(hwcap, 5);
155
LOONGARCH_CRC32 = ExtensionSupported(hwcap, 6);
156
LOONGARCH_COMPLEX = ExtensionSupported(hwcap, 7);
157
LOONGARCH_CRYPTO = ExtensionSupported(hwcap, 8);
158
LOONGARCH_LVZ = ExtensionSupported(hwcap, 9);
159
LOONGARCH_LBT_X86 = ExtensionSupported(hwcap, 10);
160
LOONGARCH_LBT_ARM = ExtensionSupported(hwcap, 11);
161
LOONGARCH_LBT_MIPS = ExtensionSupported(hwcap, 12);
162
LOONGARCH_PTW = ExtensionSupported(hwcap, 13);
163
164
#ifdef USE_CPU_FEATURES
165
cpu_features::LoongArchInfo info = cpu_features::GetLoongArchInfo();
166
LOONGARCH_CPUCFG = true;
167
LOONGARCH_LAM = info.features.LAM;
168
LOONGARCH_UAL = info.features.UAL;
169
LOONGARCH_FPU = info.features.FPU;
170
LOONGARCH_LSX = info.features.LSX;
171
LOONGARCH_LASX = info.features.LASX;
172
LOONGARCH_CRC32 = info.features.CRC32;
173
LOONGARCH_COMPLEX = info.features.COMPLEX;
174
LOONGARCH_CRYPTO = info.features.CRYPTO;
175
LOONGARCH_LVZ = info.features.LVZ;
176
LOONGARCH_LBT_X86 = info.features.LBT_X86;
177
LOONGARCH_LBT_ARM = info.features.LBT_ARM;
178
LOONGARCH_LBT_MIPS = info.features.LBT_MIPS;
179
LOONGARCH_PTW = info.features.PTW;
180
#endif
181
}
182
183
std::vector<std::string> CPUInfo::Features() {
184
std::vector<std::string> features;
185
186
struct Flag {
187
bool &flag;
188
const char *str;
189
};
190
const Flag list[] = {
191
{ LOONGARCH_CPUCFG, "Identify CPU Features" },
192
{ LOONGARCH_LAM, "Atomic Memory Access Instructions" },
193
{ LOONGARCH_UAL, "Non-Aligned Memory Access" },
194
{ LOONGARCH_FPU, "Basic Floating-Point Instructions" },
195
{ LOONGARCH_LSX, "Loongson SIMD eXtension" },
196
{ LOONGARCH_LASX, "Loongson Advanced SIMD eXtension" },
197
{ LOONGARCH_CRC32, "Cyclic Redundancy Check Instructions" },
198
{ LOONGARCH_COMPLEX, "Complex Vector Operation Instructions" },
199
{ LOONGARCH_CRYPTO, "Encryption And Decryption Vector Instructions" },
200
{ LOONGARCH_LVZ, "Virtualization" },
201
{ LOONGARCH_LBT_X86, "X86 Binary Translation Extension" },
202
{ LOONGARCH_LBT_ARM, "ARM Binary Translation Extension" },
203
{ LOONGARCH_LBT_MIPS, "MIPS Binary Translation Extension" },
204
{ LOONGARCH_PTW, "Page Table Walker" },
205
};
206
207
for (auto &item : list) {
208
if (item.flag) {
209
features.push_back(item.str);
210
}
211
}
212
213
return features;
214
}
215
216
// Turn the cpu info into a string we can show
217
std::string CPUInfo::Summarize() {
218
std::string sum;
219
if (num_cores == 1)
220
sum = StringFromFormat("%s, %i core", cpu_string, num_cores);
221
else
222
sum = StringFromFormat("%s, %i cores", cpu_string, num_cores);
223
224
auto features = Features();
225
for (std::string &feature : features) {
226
sum += ", " + feature;
227
}
228
return sum;
229
}
230
231
#endif // PPSSPP_ARCH(LOONGARCH64)
232
233