2021-07-28 11:58:56 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
|
|
|
#include "ppsspp_config.h"
|
|
|
|
#if PPSSPP_ARCH(RISCV64)
|
|
|
|
|
2023-01-29 23:23:53 +00:00
|
|
|
#include "ext/cpu_features/include/cpuinfo_riscv.h"
|
|
|
|
|
|
|
|
#if defined(CPU_FEATURES_OS_LINUX)
|
|
|
|
#define USE_CPU_FEATURES 1
|
|
|
|
#endif
|
|
|
|
|
2022-12-21 03:12:16 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <set>
|
|
|
|
#include <sstream>
|
|
|
|
#include <sys/auxv.h>
|
|
|
|
#include <vector>
|
2021-07-28 11:58:56 +00:00
|
|
|
#include "Common/Common.h"
|
|
|
|
#include "Common/CPUDetect.h"
|
|
|
|
#include "Common/StringUtils.h"
|
|
|
|
#include "Common/File/FileUtil.h"
|
|
|
|
#include "Common/Data/Encoding/Utf8.h"
|
|
|
|
|
|
|
|
// Only Linux platforms have /proc/cpuinfo
|
|
|
|
#if defined(__linux__)
|
|
|
|
const char procfile[] = "/proc/cpuinfo";
|
|
|
|
// https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-devices-system-cpu
|
|
|
|
const char syscpupresentfile[] = "/sys/devices/system/cpu/present";
|
2023-03-03 03:37:58 +00:00
|
|
|
const char firmwarefile[] = "/sys/firmware/devicetree/base/compatible";
|
2021-07-28 11:58:56 +00:00
|
|
|
|
2022-12-21 03:12:16 +00:00
|
|
|
class RiscVCPUInfoParser {
|
|
|
|
public:
|
|
|
|
RiscVCPUInfoParser();
|
2021-07-28 11:58:56 +00:00
|
|
|
|
2022-12-21 03:12:16 +00:00
|
|
|
int ProcessorCount();
|
|
|
|
int TotalLogicalCount();
|
|
|
|
|
|
|
|
std::string ISAString();
|
2023-03-03 03:37:58 +00:00
|
|
|
bool FirmwareMatchesCompatible(const std::string &str);
|
2022-12-21 03:12:16 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<std::vector<std::string>> cores_;
|
2023-03-03 03:37:58 +00:00
|
|
|
std::vector<std::string> firmware_;
|
|
|
|
bool firmwareLoaded_ = false;
|
2022-12-21 03:12:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
RiscVCPUInfoParser::RiscVCPUInfoParser() {
|
|
|
|
std::string procdata, line;
|
|
|
|
if (!File::ReadFileToString(true, Path(procfile), procdata))
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::istringstream file(procdata);
|
|
|
|
int index = -1;
|
|
|
|
while (std::getline(file, line)) {
|
|
|
|
if (line.length() == 0) {
|
|
|
|
index = -1;
|
|
|
|
} else {
|
|
|
|
if (index == -1) {
|
|
|
|
index = (int)cores_.size();
|
|
|
|
cores_.push_back(std::vector<std::string>());
|
|
|
|
}
|
|
|
|
cores_[index].push_back(line);
|
|
|
|
}
|
|
|
|
}
|
2021-07-30 17:03:13 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 03:12:16 +00:00
|
|
|
int RiscVCPUInfoParser::ProcessorCount() {
|
|
|
|
// Not using present as that counts the logical CPUs (aka harts.)
|
|
|
|
static const char *marker = "processor\t: ";
|
|
|
|
std::set<std::string> processors;
|
|
|
|
for (auto core : cores_) {
|
|
|
|
for (auto line : core) {
|
|
|
|
if (line.find(marker) != line.npos)
|
|
|
|
processors.insert(line);
|
|
|
|
}
|
|
|
|
}
|
2021-07-28 11:58:56 +00:00
|
|
|
|
2022-12-21 03:12:16 +00:00
|
|
|
return (int)processors.size();
|
|
|
|
}
|
2021-07-28 11:58:56 +00:00
|
|
|
|
2022-12-21 03:12:16 +00:00
|
|
|
int RiscVCPUInfoParser::TotalLogicalCount() {
|
|
|
|
std::string presentData, line;
|
|
|
|
bool presentSuccess = File::ReadFileToString(true, Path(syscpupresentfile), presentData);
|
2021-07-28 11:58:56 +00:00
|
|
|
if (presentSuccess) {
|
2022-12-21 03:12:16 +00:00
|
|
|
std::istringstream presentFile(presentData);
|
|
|
|
|
2021-07-28 11:58:56 +00:00
|
|
|
int low, high, found;
|
|
|
|
std::getline(presentFile, line);
|
|
|
|
found = sscanf(line.c_str(), "%d-%d", &low, &high);
|
|
|
|
if (found == 1)
|
|
|
|
return 1;
|
|
|
|
if (found == 2)
|
|
|
|
return high - low + 1;
|
|
|
|
}
|
|
|
|
|
2022-12-21 03:12:16 +00:00
|
|
|
static const char *marker = "hart\t\t: ";
|
|
|
|
std::set<std::string> harts;
|
|
|
|
for (auto core : cores_) {
|
|
|
|
for (auto line : core) {
|
|
|
|
if (line.find(marker) != line.npos)
|
|
|
|
harts.insert(line);
|
|
|
|
}
|
2021-07-28 11:58:56 +00:00
|
|
|
}
|
2022-12-21 03:12:16 +00:00
|
|
|
|
|
|
|
return (int)harts.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RiscVCPUInfoParser::ISAString() {
|
|
|
|
static const char *marker = "isa\t\t: ";
|
|
|
|
for (auto core : cores_) {
|
|
|
|
for (auto line : core) {
|
|
|
|
if (line.find(marker) != line.npos)
|
|
|
|
return line.substr(strlen(marker));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Unknown";
|
2021-07-28 11:58:56 +00:00
|
|
|
}
|
2023-03-03 03:37:58 +00:00
|
|
|
|
|
|
|
bool RiscVCPUInfoParser::FirmwareMatchesCompatible(const std::string &str) {
|
|
|
|
if (!firmwareLoaded_) {
|
|
|
|
firmwareLoaded_ = true;
|
|
|
|
|
|
|
|
std::string data;
|
|
|
|
if (!File::ReadFileToString(true, Path(firmwarefile), data))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SplitString(data, '\0', firmware_);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto compatible : firmware_) {
|
|
|
|
if (compatible == str)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-28 11:58:56 +00:00
|
|
|
#endif
|
|
|
|
|
2022-12-21 03:12:16 +00:00
|
|
|
static bool ExtensionSupported(unsigned long v, char c) {
|
|
|
|
unsigned long bit = (v >> (c - 'A')) & 1;
|
|
|
|
return bit == 1;
|
|
|
|
}
|
|
|
|
|
2021-07-28 11:58:56 +00:00
|
|
|
CPUInfo cpu_info;
|
|
|
|
|
|
|
|
CPUInfo::CPUInfo() {
|
|
|
|
Detect();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detects the various cpu features
|
|
|
|
void CPUInfo::Detect()
|
|
|
|
{
|
|
|
|
// Set some defaults here
|
|
|
|
HTT = false;
|
|
|
|
#if PPSSPP_ARCH(RISCV64)
|
|
|
|
OS64bit = true;
|
|
|
|
CPU64bit = true;
|
|
|
|
Mode64bit = true;
|
|
|
|
#else
|
|
|
|
OS64bit = false;
|
|
|
|
CPU64bit = false;
|
|
|
|
Mode64bit = false;
|
|
|
|
#endif
|
|
|
|
vendor = VENDOR_OTHER;
|
2022-12-21 03:12:16 +00:00
|
|
|
|
|
|
|
// Not sure how to get anything great here.
|
|
|
|
truncate_cpy(brand_string, "Unknown");
|
2021-07-28 11:58:56 +00:00
|
|
|
|
|
|
|
#if !defined(__linux__)
|
|
|
|
num_cores = 1;
|
2022-12-21 03:12:16 +00:00
|
|
|
logical_cpu_count = 1;
|
|
|
|
truncate_cpy(cpu_string, "Unknown");
|
2021-07-28 11:58:56 +00:00
|
|
|
#else // __linux__
|
2022-12-21 03:12:16 +00:00
|
|
|
RiscVCPUInfoParser parser;
|
|
|
|
num_cores = parser.ProcessorCount();
|
|
|
|
logical_cpu_count = parser.TotalLogicalCount() / num_cores;
|
|
|
|
if (logical_cpu_count <= 0)
|
|
|
|
logical_cpu_count = 1;
|
|
|
|
|
|
|
|
truncate_cpy(cpu_string, parser.ISAString().c_str());
|
2023-03-03 03:37:58 +00:00
|
|
|
|
|
|
|
// A number of CPUs support a limited set of B. It's not all U74, so we use SOC for now...
|
|
|
|
if (parser.FirmwareMatchesCompatible("starfive,jh7110")) {
|
|
|
|
RiscV_Zba = true;
|
|
|
|
RiscV_Zbb = true;
|
|
|
|
}
|
2021-07-28 11:58:56 +00:00
|
|
|
#endif
|
2022-12-21 03:12:16 +00:00
|
|
|
|
|
|
|
unsigned long hwcap = getauxval(AT_HWCAP);
|
|
|
|
RiscV_M = ExtensionSupported(hwcap, 'M');
|
|
|
|
RiscV_A = ExtensionSupported(hwcap, 'A');
|
|
|
|
RiscV_F = ExtensionSupported(hwcap, 'F');
|
|
|
|
RiscV_D = ExtensionSupported(hwcap, 'D');
|
|
|
|
RiscV_C = ExtensionSupported(hwcap, 'C');
|
|
|
|
RiscV_V = ExtensionSupported(hwcap, 'V');
|
2023-01-22 20:37:47 +00:00
|
|
|
RiscV_B = ExtensionSupported(hwcap, 'B');
|
2023-04-29 17:07:50 +00:00
|
|
|
// We assume as in RVA20U64 that F means Zicsr is available.
|
|
|
|
RiscV_Zicsr = RiscV_F;
|
2023-01-29 23:23:53 +00:00
|
|
|
|
|
|
|
#ifdef USE_CPU_FEATURES
|
|
|
|
cpu_features::RiscvInfo info = cpu_features::GetRiscvInfo();
|
|
|
|
CPU64bit = info.features.RV64I;
|
|
|
|
RiscV_M = info.features.M;
|
|
|
|
RiscV_A = info.features.A;
|
|
|
|
RiscV_F = info.features.F;
|
|
|
|
RiscV_D = info.features.D;
|
|
|
|
RiscV_C = info.features.C;
|
2023-04-29 17:07:50 +00:00
|
|
|
RiscV_V = info.features.V;
|
2023-07-19 06:54:14 +00:00
|
|
|
// Seems to be wrong sometimes, assume we have it if we have F.
|
|
|
|
RiscV_Zicsr = info.features.Zicsr || info.features.F;
|
2023-01-29 23:23:53 +00:00
|
|
|
|
|
|
|
truncate_cpy(brand_string, info.uarch);
|
|
|
|
#endif
|
2021-07-28 11:58:56 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 23:03:42 +00:00
|
|
|
std::vector<std::string> CPUInfo::Features() {
|
|
|
|
std::vector<std::string> features;
|
|
|
|
|
|
|
|
struct Flag {
|
|
|
|
bool &flag;
|
|
|
|
const char *str;
|
|
|
|
};
|
|
|
|
const Flag list[] = {
|
|
|
|
{ RiscV_M, "Muldiv" },
|
|
|
|
{ RiscV_A, "Atomic" },
|
|
|
|
{ RiscV_F, "Float" },
|
|
|
|
{ RiscV_D, "Double" },
|
|
|
|
{ RiscV_C, "Compressed" },
|
|
|
|
{ RiscV_V, "Vector" },
|
|
|
|
{ RiscV_B, "Bitmanip" },
|
2023-02-17 00:09:22 +00:00
|
|
|
{ RiscV_Zba, "Zba" },
|
|
|
|
{ RiscV_Zbb, "Zbb" },
|
|
|
|
{ RiscV_Zbc, "Zbc" },
|
|
|
|
{ RiscV_Zbs, "Zbs" },
|
2023-01-29 23:23:53 +00:00
|
|
|
{ RiscV_Zicsr, "Zicsr" },
|
2023-01-29 23:03:42 +00:00
|
|
|
{ CPU64bit, "64-bit" },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto &item : list) {
|
|
|
|
if (item.flag) {
|
|
|
|
features.push_back(item.str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return features;
|
|
|
|
}
|
|
|
|
|
2021-07-28 11:58:56 +00:00
|
|
|
// Turn the cpu info into a string we can show
|
2023-01-29 23:03:42 +00:00
|
|
|
std::string CPUInfo::Summarize() {
|
2021-07-28 11:58:56 +00:00
|
|
|
std::string sum;
|
|
|
|
if (num_cores == 1)
|
|
|
|
sum = StringFromFormat("%s, %i core", cpu_string, num_cores);
|
|
|
|
else
|
|
|
|
sum = StringFromFormat("%s, %i cores", cpu_string, num_cores);
|
2021-07-30 17:03:13 +00:00
|
|
|
|
2023-01-29 23:03:42 +00:00
|
|
|
auto features = Features();
|
|
|
|
for (std::string &feature : features) {
|
|
|
|
sum += ", " + feature;
|
|
|
|
}
|
2021-07-28 11:58:56 +00:00
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // PPSSPP_ARCH(RISCV64)
|