Skip to content

Commit

Permalink
Merge pull request #18049 from KatyushaScarlet/dev-loongarch64
Browse files Browse the repository at this point in the history
Add basic support for loongarch64
  • Loading branch information
hrydgard committed Sep 4, 2023
2 parents 412c3a0 + 75cb0d3 commit 4df4028
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 1 deletion.
19 changes: 18 additions & 1 deletion CMakeLists.txt
Expand Up @@ -67,6 +67,8 @@ if(CMAKE_SYSTEM_PROCESSOR)
set(MIPS_DEVICE ON)
elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "^riscv64")
set(RISCV64_DEVICE ON)
elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "^loongarch64")
set(LOONGARCH64_DEVICE ON)
else()
message("Unknown CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
endif()
Expand Down Expand Up @@ -139,6 +141,7 @@ option(ARMV7 "Set to ON if targeting an ARMv7 processor" ${ARMV7_DEVICE})
option(ARM "Set to ON if targeting an ARM processor" ${ARM_DEVICE})
option(MIPS "Set to ON if targeting a MIPS processor" ${MIPS_DEVICE})
option(RISCV64 "Set to ON if targeting a RISCV64 processor" ${RISCV64_DEVICE})
option(LOONGARCH64 "Set to ON if targeting a LOONGARCH64 processor" ${LOONGARCH64_DEVICE})
option(X86 "Set to ON if targeting an X86 processor" ${X86_DEVICE})
option(X86_64 "Set to ON if targeting an X86_64 processor" ${X86_64_DEVICE})
# :: Environments
Expand Down Expand Up @@ -318,6 +321,9 @@ endif()
if(RISCV64)
message("Generating for RISCV64, ${CMAKE_BUILD_TYPE}")
endif()
if(LOONGARCH64)
message("Generating for LOONGARCH64, ${CMAKE_BUILD_TYPE}")
endif()
if(X86)
message("Generating for x86, ${CMAKE_BUILD_TYPE}")
endif()
Expand Down Expand Up @@ -527,6 +533,14 @@ set(CommonRISCV64
)
source_group(RISCV64 FILES ${CommonRISCV64})

set(CommonLOONGARCH64
${CommonJIT}
Common/LoongArchCPUDetect.cpp
Core/MIPS/fake/FakeJit.cpp
Core/MIPS/fake/FakeJit.h
)
source_group(LOONGARCH64 FILES ${CommonLOONGARCH64})

if(WIN32)
set(CommonD3D
Common/GPU/D3D9/D3D9ShaderCompiler.cpp
Expand Down Expand Up @@ -565,6 +579,7 @@ add_library(Common STATIC
${CommonARM64}
${CommonMIPS}
${CommonRISCV64}
${CommonLOONGARCH64}
${CommonD3D}
${CommonVR}
Common/Serialize/Serializer.cpp
Expand Down Expand Up @@ -887,6 +902,8 @@ if(USE_FFMPEG)
set(PLATFORM_ARCH "linux/mips32")
elseif(RISCV64)
set(PLATFORM_ARCH "linux/riscv64")
elseif(LOONGARCH64)
set(PLATFORM_ARCH "linux/loongarch64")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(PLATFORM_ARCH "linux/x86_64")
elseif(X86)
Expand Down Expand Up @@ -1478,7 +1495,7 @@ if(LINUX AND NOT ANDROID)
endif()

set(ATOMIC_LIB)
if(ANDROID OR (LINUX AND ARM_DEVICE) OR (LINUX AND RISCV64))
if(ANDROID OR (LINUX AND ARM_DEVICE) OR (LINUX AND RISCV64) OR (LINUX AND LOONGARCH64))
set(ATOMIC_LIB atomic)
endif()

Expand Down
16 changes: 16 additions & 0 deletions Common/CPUDetect.h
Expand Up @@ -115,6 +115,22 @@ struct CPUInfo {
bool RiscV_Zbc;
bool RiscV_Zbs;

// LoongArch specific extension flags.
bool LOONGARCH_CPUCFG;
bool LOONGARCH_LAM;
bool LOONGARCH_UAL;
bool LOONGARCH_FPU;
bool LOONGARCH_LSX;
bool LOONGARCH_LASX;
bool LOONGARCH_CRC32;
bool LOONGARCH_COMPLEX;
bool LOONGARCH_CRYPTO;
bool LOONGARCH_LVZ;
bool LOONGARCH_LBT_X86;
bool LOONGARCH_LBT_ARM;
bool LOONGARCH_LBT_MIPS;
bool LOONGARCH_PTW;

// Quirks
struct {
// Samsung Galaxy S7 devices (Exynos 8890) have a big.LITTLE configuration where the cacheline size differs between big and LITTLE.
Expand Down
1 change: 1 addition & 0 deletions Common/Common.vcxproj
Expand Up @@ -941,6 +941,7 @@
<ClCompile Include="LogReporting.cpp" />
<ClCompile Include="RiscVCPUDetect.cpp" />
<ClCompile Include="RiscVEmitter.cpp" />
<ClCompile Include="LoongArchCPUDetect.cpp" />
<ClCompile Include="Serialize\Serializer.cpp" />
<ClCompile Include="Data\Convert\ColorConv.cpp" />
<ClCompile Include="ConsoleListener.cpp" />
Expand Down
1 change: 1 addition & 0 deletions Common/Common.vcxproj.filters
Expand Up @@ -875,6 +875,7 @@
<Filter>GPU\Vulkan</Filter>
</ClCompile>
<ClCompile Include="RiscVEmitter.cpp" />
<ClCompile Include="LoongArchCPUDetect.cpp" />
<ClCompile Include="GPU\Vulkan\VulkanFrameData.cpp">
<Filter>GPU\Vulkan</Filter>
</ClCompile>
Expand Down
2 changes: 2 additions & 0 deletions Common/FakeCPUDetect.cpp
Expand Up @@ -24,6 +24,8 @@
#define REAL_CPUDETECT_AVAIL 1
#elif PPSSPP_ARCH(RISCV64)
#define REAL_CPUDETECT_AVAIL 1
#elif PPSSPP_ARCH(LOONGARCH64)
#define REAL_CPUDETECT_AVAIL 1
#endif

#ifndef REAL_CPUDETECT_AVAIL
Expand Down
235 changes: 235 additions & 0 deletions Common/LoongArchCPUDetect.cpp
@@ -0,0 +1,235 @@
// 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(LOONGARCH64)

#include "ext/cpu_features/include/cpuinfo_loongarch.h"

#if defined(CPU_FEATURES_OS_LINUX)
#define USE_CPU_FEATURES 1
#endif

#include <cstring>
#include <set>
#include <sstream>
#include <sys/auxv.h>
#include <vector>
#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";

class LoongArchCPUInfoParser {
public:
LoongArchCPUInfoParser();

int ProcessorCount();
int TotalLogicalCount();

private:
std::vector<std::vector<std::string>> cores_;
};

LoongArchCPUInfoParser::LoongArchCPUInfoParser() {
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);
}
}
}

int LoongArchCPUInfoParser::ProcessorCount() {
static const char *marker = "core";
std::set<std::string> coreIndex;
for (auto core : cores_) {
for (auto line : core) {
if (line.find(marker) != line.npos)
coreIndex.insert(line);
}
}

return (int)coreIndex.size();
}

int LoongArchCPUInfoParser::TotalLogicalCount() {
std::string presentData, line;
bool presentSuccess = File::ReadFileToString(true, Path(syscpupresentfile), presentData);
if (presentSuccess) {
std::istringstream presentFile(presentData);

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;
}
}else{
return 1;
}
}

#endif

static bool ExtensionSupported(unsigned long v, unsigned int i) {
// https://github.com/torvalds/linux/blob/master/arch/loongarch/include/uapi/asm/hwcap.h
unsigned long mask = 1 << i;
return v & mask;
}

CPUInfo cpu_info;

CPUInfo::CPUInfo() {
Detect();
}

// Detects the various cpu features
void CPUInfo::Detect()
{
// Set some defaults here
HTT = false;
#if PPSSPP_ARCH(LOONGARCH64)
OS64bit = true;
CPU64bit = true;
Mode64bit = true;
#else
OS64bit = false;
CPU64bit = false;
Mode64bit = false;
#endif
vendor = VENDOR_OTHER;
truncate_cpy(brand_string, "Loongson");
truncate_cpy(cpu_string, "Loongson");
#if !defined(__linux__)
num_cores = 1;
logical_cpu_count = 1;
truncate_cpy(brand_string, "Unknown");
truncate_cpy(cpu_string, "Unknown");
#else // __linux__
LoongArchCPUInfoParser parser;
num_cores = parser.ProcessorCount();
logical_cpu_count = parser.TotalLogicalCount() / num_cores;
if (logical_cpu_count <= 0)
logical_cpu_count = 1;
#endif

unsigned long hwcap = getauxval(AT_HWCAP);
LOONGARCH_CPUCFG = true;
LOONGARCH_LAM = ExtensionSupported(hwcap, 1);
LOONGARCH_UAL = ExtensionSupported(hwcap, 2);
LOONGARCH_FPU = ExtensionSupported(hwcap, 3);
LOONGARCH_LSX = ExtensionSupported(hwcap, 4);
LOONGARCH_LASX = ExtensionSupported(hwcap, 5);
LOONGARCH_CRC32 = ExtensionSupported(hwcap, 6);
LOONGARCH_COMPLEX = ExtensionSupported(hwcap, 7);
LOONGARCH_CRYPTO = ExtensionSupported(hwcap, 8);
LOONGARCH_LVZ = ExtensionSupported(hwcap, 9);
LOONGARCH_LBT_X86 = ExtensionSupported(hwcap, 10);
LOONGARCH_LBT_ARM = ExtensionSupported(hwcap, 11);
LOONGARCH_LBT_MIPS = ExtensionSupported(hwcap, 12);
LOONGARCH_PTW = ExtensionSupported(hwcap, 13);

#ifdef USE_CPU_FEATURES
cpu_features::LoongArchInfo info = cpu_features::GetLoongArchInfo();
LOONGARCH_CPUCFG = true;
LOONGARCH_LAM = info.features.LAM;
LOONGARCH_UAL = info.features.UAL;
LOONGARCH_FPU = info.features.FPU;
LOONGARCH_LSX = info.features.LSX;
LOONGARCH_LASX = info.features.LASX;
LOONGARCH_CRC32 = info.features.CRC32;
LOONGARCH_COMPLEX = info.features.COMPLEX;
LOONGARCH_CRYPTO = info.features.CRYPTO;
LOONGARCH_LVZ = info.features.LVZ;
LOONGARCH_LBT_X86 = info.features.LBT_X86;
LOONGARCH_LBT_ARM = info.features.LBT_ARM;
LOONGARCH_LBT_MIPS = info.features.LBT_MIPS;
LOONGARCH_PTW = info.features.PTW;
#endif
}

std::vector<std::string> CPUInfo::Features() {
std::vector<std::string> features;

struct Flag {
bool &flag;
const char *str;
};
const Flag list[] = {
{ LOONGARCH_CPUCFG, "Identify CPU Features" },
{ LOONGARCH_LAM, "Atomic Memory Access Instructions" },
{ LOONGARCH_UAL, "Non-Aligned Memory Access" },
{ LOONGARCH_FPU, "Basic Floating-Point Instructions" },
{ LOONGARCH_LSX, "Loongson SIMD eXtension" },
{ LOONGARCH_LASX, "Loongson Advanced SIMD eXtension" },
{ LOONGARCH_CRC32, "Cyclic Redundancy Check Instructions" },
{ LOONGARCH_COMPLEX, "Complex Vector Operation Instructions" },
{ LOONGARCH_CRYPTO, "Encryption And Decryption Vector Instructions" },
{ LOONGARCH_LVZ, "Virtualization" },
{ LOONGARCH_LBT_X86, "X86 Binary Translation Extension" },
{ LOONGARCH_LBT_ARM, "ARM Binary Translation Extension" },
{ LOONGARCH_LBT_MIPS, "MIPS Binary Translation Extension" },
{ LOONGARCH_PTW, "Page Table Walker" },
};

for (auto &item : list) {
if (item.flag) {
features.push_back(item.str);
}
}

return features;
}

// Turn the cpu info into a string we can show
std::string CPUInfo::Summarize() {
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);

auto features = Features();
for (std::string &feature : features) {
sum += ", " + feature;
}
return sum;
}

#endif // PPSSPP_ARCH(LOONGARCH64)
5 changes: 5 additions & 0 deletions ppsspp_config.h
Expand Up @@ -75,6 +75,11 @@
#define PPSSPP_ARCH_64BIT 1
#endif

#if defined(__loongarch64)
//https://github.com/gcc-mirror/gcc/blob/master/gcc/config/loongarch/loongarch-c.cc
#define PPSSPP_ARCH_LOONGARCH64 1
#define PPSSPP_ARCH_64BIT 1
#endif

// PLATFORM defines
#if defined(_WIN32)
Expand Down

0 comments on commit 4df4028

Please sign in to comment.