2015-05-24 06:55:12 +02:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 03:22:19 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2021-12-10 03:22:16 +01:00
|
|
|
#include "Common/CPUDetect.h"
|
|
|
|
|
2022-06-16 09:02:39 +02:00
|
|
|
#ifdef _WIN32
|
2023-01-29 09:51:41 +01:00
|
|
|
#include <windows.h>
|
2022-06-16 09:02:39 +02:00
|
|
|
#include <processthreadsapi.h>
|
|
|
|
#endif
|
|
|
|
|
2023-01-29 09:51:41 +01:00
|
|
|
#include <algorithm>
|
2014-02-20 04:11:52 +01:00
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
2021-01-02 13:47:18 +01:00
|
|
|
#include <thread>
|
2014-02-17 11:18:15 +01:00
|
|
|
|
2022-07-19 06:45:27 +02:00
|
|
|
#include <fmt/format.h>
|
2024-09-22 09:24:21 +02:00
|
|
|
#include <fmt/ranges.h>
|
2022-07-19 06:45:27 +02:00
|
|
|
|
2014-09-08 03:06:58 +02:00
|
|
|
#include "Common/CommonTypes.h"
|
2015-02-23 20:40:05 +01:00
|
|
|
#include "Common/Intrinsics.h"
|
2022-06-16 09:02:39 +02:00
|
|
|
#include "Common/MsgHandler.h"
|
2022-07-19 06:45:27 +02:00
|
|
|
#include "Common/StringUtil.h"
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2015-02-23 20:40:05 +01:00
|
|
|
#ifndef _WIN32
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2015-05-14 23:14:34 +02:00
|
|
|
#ifdef __FreeBSD__
|
2017-01-24 02:44:10 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2011-08-27 20:42:11 +02:00
|
|
|
#include <machine/cpufunc.h>
|
|
|
|
#include <sys/types.h>
|
2015-05-14 23:14:34 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline void __cpuidex(int info[4], int function_id, int subfunction_id)
|
2008-08-17 05:56:07 +02:00
|
|
|
{
|
2015-05-14 23:14:34 +02:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
// Despite the name, this is just do_cpuid() with ECX as second input.
|
|
|
|
cpuid_count((u_int)function_id, (u_int)subfunction_id, (u_int*)info);
|
2011-02-07 20:08:19 +01:00
|
|
|
#else
|
2015-05-14 23:14:34 +02:00
|
|
|
info[0] = function_id; // eax
|
|
|
|
info[2] = subfunction_id; // ecx
|
2015-02-15 20:43:31 +01:00
|
|
|
__asm__("cpuid"
|
2015-05-14 23:14:34 +02:00
|
|
|
: "=a"(info[0]), "=b"(info[1]), "=c"(info[2]), "=d"(info[3])
|
|
|
|
: "a"(function_id), "c"(subfunction_id));
|
2011-02-07 20:08:19 +01:00
|
|
|
#endif
|
2008-08-15 22:48:40 +02:00
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2022-07-19 06:45:27 +02:00
|
|
|
constexpr u32 XCR_XFEATURE_ENABLED_MASK = 0;
|
|
|
|
|
|
|
|
static u64 xgetbv(u32 index)
|
2008-08-15 22:48:40 +02:00
|
|
|
{
|
2022-07-19 06:45:27 +02:00
|
|
|
u32 eax, edx;
|
|
|
|
__asm__ __volatile__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(index));
|
|
|
|
return ((u64)edx << 32) | eax;
|
2008-08-15 22:48:40 +02:00
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2022-07-19 06:45:27 +02:00
|
|
|
#else
|
2018-05-08 02:42:38 +02:00
|
|
|
|
2022-07-19 06:45:27 +02:00
|
|
|
constexpr u32 XCR_XFEATURE_ENABLED_MASK = _XCR_XFEATURE_ENABLED_MASK;
|
2018-05-08 02:42:38 +02:00
|
|
|
|
|
|
|
static u64 xgetbv(u32 index)
|
|
|
|
{
|
|
|
|
return _xgetbv(index);
|
|
|
|
}
|
|
|
|
|
2022-06-16 09:02:39 +02:00
|
|
|
static void WarnIfRunningUnderEmulation()
|
|
|
|
{
|
|
|
|
// Starting with win11, arm64 windows can run x64 processes under emulation.
|
|
|
|
// This detects such a scenario and informs the user they probably want to run a native build.
|
|
|
|
PROCESS_MACHINE_INFORMATION info{};
|
|
|
|
if (!GetProcessInformation(GetCurrentProcess(), ProcessMachineTypeInfo, &info, sizeof(info)))
|
|
|
|
{
|
|
|
|
// Possibly we are running on version of windows which doesn't support ProcessMachineTypeInfo.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (info.MachineAttributes & MACHINE_ATTRIBUTES::KernelEnabled)
|
|
|
|
{
|
|
|
|
// KernelEnabled will be set if process arch matches the kernel arch - how we want people to run
|
|
|
|
// dolphin.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The process is not native; could use IsWow64Process2 to get native machine type, but for now
|
|
|
|
// we can assume it is arm64.
|
|
|
|
PanicAlertFmtT("This build of Dolphin is not natively compiled for your CPU.\n"
|
|
|
|
"Please run the ARM64 build of Dolphin for a better experience.");
|
|
|
|
}
|
|
|
|
|
2022-07-19 06:45:27 +02:00
|
|
|
#endif // ifdef _WIN32
|
2018-05-08 02:42:38 +02:00
|
|
|
|
2022-07-19 06:45:27 +02:00
|
|
|
struct CPUIDResult
|
2013-08-20 01:25:10 +02:00
|
|
|
{
|
2022-07-19 06:45:27 +02:00
|
|
|
u32 eax{}, ebx{}, ecx{}, edx{};
|
|
|
|
};
|
|
|
|
static_assert(sizeof(CPUIDResult) == sizeof(u32) * 4);
|
|
|
|
|
|
|
|
static inline CPUIDResult cpuid(int function_id, int subfunction_id = 0)
|
|
|
|
{
|
|
|
|
CPUIDResult info;
|
|
|
|
__cpuidex((int*)&info, function_id, subfunction_id);
|
|
|
|
return info;
|
2013-08-20 01:25:10 +02:00
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2008-08-15 22:43:14 +02:00
|
|
|
CPUInfo cpu_info;
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2014-08-24 18:14:54 +02:00
|
|
|
CPUInfo::CPUInfo()
|
|
|
|
{
|
2010-02-24 11:10:48 +01:00
|
|
|
Detect();
|
|
|
|
}
|
|
|
|
|
2008-08-15 22:43:14 +02:00
|
|
|
void CPUInfo::Detect()
|
2008-07-12 19:40:22 +02:00
|
|
|
{
|
2022-06-16 09:02:39 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
WarnIfRunningUnderEmulation();
|
|
|
|
#endif
|
|
|
|
|
2022-07-19 06:45:27 +02:00
|
|
|
// This should be much more reliable and easier than trying to get the number of cores out of the
|
|
|
|
// CPUID data ourselves.
|
|
|
|
num_cores = std::max(static_cast<int>(std::thread::hardware_concurrency()), 1);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2009-02-28 02:26:56 +01:00
|
|
|
// Assume CPU supports the CPUID instruction. Those that don't can barely
|
2022-07-19 06:45:27 +02:00
|
|
|
// boot modern OS anyway.
|
|
|
|
|
|
|
|
// Detect CPU's CPUID capabilities and grab vendor string.
|
|
|
|
auto info = cpuid(0);
|
|
|
|
const u32 func_id_max = info.eax;
|
|
|
|
|
|
|
|
std::string vendor_id;
|
|
|
|
vendor_id.resize(sizeof(u32) * 3);
|
|
|
|
std::memcpy(&vendor_id[0], &info.ebx, sizeof(u32));
|
|
|
|
std::memcpy(&vendor_id[4], &info.edx, sizeof(u32));
|
|
|
|
std::memcpy(&vendor_id[8], &info.ecx, sizeof(u32));
|
|
|
|
TruncateToCString(&vendor_id);
|
|
|
|
if (vendor_id == "GenuineIntel")
|
2018-04-01 23:43:40 +02:00
|
|
|
vendor = CPUVendor::Intel;
|
2022-07-19 06:45:27 +02:00
|
|
|
else if (vendor_id == "AuthenticAMD")
|
2018-04-01 23:43:40 +02:00
|
|
|
vendor = CPUVendor::AMD;
|
2008-08-15 22:43:14 +02:00
|
|
|
else
|
2018-04-01 23:43:40 +02:00
|
|
|
vendor = CPUVendor::Other;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-08-15 22:43:14 +02:00
|
|
|
// Detect family and other misc stuff.
|
2022-07-19 06:45:27 +02:00
|
|
|
bool is_amd_family_17 = false;
|
|
|
|
bool has_sse = false;
|
|
|
|
if (func_id_max >= 1)
|
2014-08-24 18:14:54 +02:00
|
|
|
{
|
2022-07-19 06:45:27 +02:00
|
|
|
info = cpuid(1);
|
|
|
|
const u32 version = info.eax;
|
|
|
|
const u32 family = ((version >> 8) & 0xf) + ((version >> 20) & 0xff);
|
|
|
|
const u32 model = ((version >> 4) & 0xf) + ((version >> 12) & 0xf0);
|
|
|
|
const u32 stepping = version & 0xf;
|
|
|
|
|
|
|
|
cpu_id = fmt::format("{:02X}:{:02X}:{:X}", family, model, stepping);
|
|
|
|
|
2014-10-11 23:22:44 +02:00
|
|
|
// Detect people unfortunate enough to be running Dolphin on an Atom
|
2022-07-19 06:45:27 +02:00
|
|
|
if (vendor == CPUVendor::Intel && family == 6 &&
|
2014-10-11 23:22:44 +02:00
|
|
|
(model == 0x1C || model == 0x26 || model == 0x27 || model == 0x35 || model == 0x36 ||
|
|
|
|
model == 0x37 || model == 0x4A || model == 0x4D || model == 0x5A || model == 0x5D))
|
|
|
|
bAtom = true;
|
2022-07-19 06:45:27 +02:00
|
|
|
|
2020-11-08 14:27:24 +01:00
|
|
|
// Detect AMD Zen1, Zen1+ and Zen2
|
2022-07-19 06:45:27 +02:00
|
|
|
if (vendor == CPUVendor::AMD && family == 0x17)
|
|
|
|
is_amd_family_17 = true;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2021-01-02 13:47:18 +01:00
|
|
|
// AMD CPUs before Zen faked this flag and didn't actually
|
|
|
|
// implement simultaneous multithreading (SMT; Intel calls it HTT)
|
|
|
|
// but rather some weird middle-ground between 1-2 cores
|
2022-07-19 06:45:27 +02:00
|
|
|
const bool ht = (info.edx >> 28) & 1;
|
|
|
|
HTT = ht && (vendor == CPUVendor::Intel || (vendor == CPUVendor::AMD && family >= 0x17));
|
2021-01-02 13:47:18 +01:00
|
|
|
|
2022-07-19 06:45:27 +02:00
|
|
|
if ((info.edx >> 25) & 1)
|
|
|
|
has_sse = true;
|
|
|
|
if (info.ecx & 1)
|
2011-01-26 22:27:26 +01:00
|
|
|
bSSE3 = true;
|
2022-07-19 06:45:27 +02:00
|
|
|
if ((info.ecx >> 9) & 1)
|
2011-01-26 22:27:26 +01:00
|
|
|
bSSSE3 = true;
|
2022-07-19 06:45:27 +02:00
|
|
|
if ((info.ecx >> 19) & 1)
|
2008-08-15 22:43:14 +02:00
|
|
|
bSSE4_1 = true;
|
2022-07-19 06:45:27 +02:00
|
|
|
if ((info.ecx >> 20) & 1)
|
2008-08-15 22:43:14 +02:00
|
|
|
bSSE4_2 = true;
|
2022-07-19 06:45:27 +02:00
|
|
|
if ((info.ecx >> 22) & 1)
|
2014-03-16 01:41:37 +01:00
|
|
|
bMOVBE = true;
|
2022-07-19 06:45:27 +02:00
|
|
|
if ((info.ecx >> 25) & 1)
|
2011-01-26 22:27:26 +01:00
|
|
|
bAES = true;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2013-08-20 01:25:10 +02:00
|
|
|
// AVX support requires 3 separate checks:
|
|
|
|
// - Is the AVX bit set in CPUID?
|
|
|
|
// - Is the XSAVE bit set in CPUID?
|
|
|
|
// - XGETBV result has the XCR bit set.
|
2022-07-19 06:45:27 +02:00
|
|
|
if (((info.ecx >> 28) & 1) && ((info.ecx >> 27) & 1))
|
2013-08-20 01:25:10 +02:00
|
|
|
{
|
2022-07-19 06:45:27 +02:00
|
|
|
// Check that XSAVE can be used for SSE and AVX
|
|
|
|
if ((xgetbv(XCR_XFEATURE_ENABLED_MASK) & 0b110) == 0b110)
|
2013-11-13 04:46:34 +01:00
|
|
|
{
|
2013-08-20 01:25:10 +02:00
|
|
|
bAVX = true;
|
2022-07-19 06:45:27 +02:00
|
|
|
if ((info.ecx >> 12) & 1)
|
2013-11-13 04:46:34 +01:00
|
|
|
bFMA = true;
|
2013-08-20 01:25:10 +02:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
}
|
|
|
|
|
2022-07-19 06:45:27 +02:00
|
|
|
if (func_id_max >= 7)
|
2014-08-24 18:14:54 +02:00
|
|
|
{
|
2022-07-19 06:45:27 +02:00
|
|
|
info = cpuid(7);
|
|
|
|
if ((info.ebx >> 3) & 1)
|
2014-08-24 18:14:54 +02:00
|
|
|
bBMI1 = true;
|
2022-07-19 06:45:27 +02:00
|
|
|
if ((info.ebx >> 8) & 1)
|
2014-08-24 18:14:54 +02:00
|
|
|
bBMI2 = true;
|
2022-07-19 06:45:27 +02:00
|
|
|
if ((info.ebx >> 29) & 1)
|
|
|
|
bSHA1 = bSHA2 = true;
|
2008-08-12 22:05:45 +02:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
}
|
|
|
|
|
2022-07-19 06:45:27 +02:00
|
|
|
info = cpuid(0x80000000);
|
|
|
|
const u32 ext_func_id_max = info.eax;
|
|
|
|
if (ext_func_id_max >= 0x80000004)
|
2014-08-24 18:14:54 +02:00
|
|
|
{
|
2014-10-20 20:01:50 +02:00
|
|
|
// Extract CPU model string
|
2022-07-19 06:45:27 +02:00
|
|
|
model_name.resize(sizeof(info) * 3);
|
|
|
|
for (u32 i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
info = cpuid(0x80000002 + i);
|
|
|
|
memcpy(&model_name[sizeof(info) * i], &info, sizeof(info));
|
|
|
|
}
|
|
|
|
TruncateToCString(&model_name);
|
|
|
|
model_name = StripSpaces(model_name);
|
2008-07-12 19:40:22 +02:00
|
|
|
}
|
2022-07-19 06:45:27 +02:00
|
|
|
if (ext_func_id_max >= 0x80000001)
|
2014-08-24 18:14:54 +02:00
|
|
|
{
|
2008-08-15 22:43:14 +02:00
|
|
|
// Check for more features.
|
2022-07-19 06:45:27 +02:00
|
|
|
info = cpuid(0x80000001);
|
|
|
|
if ((info.ecx >> 5) & 1)
|
2014-09-14 14:31:22 +02:00
|
|
|
bLZCNT = true;
|
2022-07-19 06:45:27 +02:00
|
|
|
if ((info.ecx >> 16) & 1)
|
2015-05-17 09:20:29 +02:00
|
|
|
bFMA4 = true;
|
2008-07-12 19:40:22 +02:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2022-07-19 06:45:27 +02:00
|
|
|
// Computed flags
|
|
|
|
bFlushToZero = has_sse;
|
|
|
|
bBMI2FastParallelBitOps = bBMI2 && !is_amd_family_17;
|
|
|
|
bCRC32 = bSSE4_2;
|
|
|
|
|
|
|
|
model_name = ReplaceAll(model_name, ",", "_");
|
|
|
|
cpu_id = ReplaceAll(cpu_id, ",", "_");
|
2008-07-12 19:40:22 +02:00
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2008-08-15 22:43:14 +02:00
|
|
|
std::string CPUInfo::Summarize()
|
|
|
|
{
|
2022-07-19 06:45:27 +02:00
|
|
|
std::vector<std::string> sum;
|
|
|
|
sum.push_back(model_name);
|
|
|
|
sum.push_back(cpu_id);
|
|
|
|
|
2008-08-15 22:43:14 +02:00
|
|
|
if (bSSE3)
|
2022-07-19 06:45:27 +02:00
|
|
|
sum.push_back("SSE3");
|
2008-08-15 22:43:14 +02:00
|
|
|
if (bSSSE3)
|
2022-07-19 06:45:27 +02:00
|
|
|
sum.push_back("SSSE3");
|
2008-08-15 22:43:14 +02:00
|
|
|
if (bSSE4_1)
|
2022-07-19 06:45:27 +02:00
|
|
|
sum.push_back("SSE4.1");
|
2008-08-15 22:43:14 +02:00
|
|
|
if (bSSE4_2)
|
2022-07-19 06:45:27 +02:00
|
|
|
sum.push_back("SSE4.2");
|
2016-06-24 10:43:46 +02:00
|
|
|
if (HTT)
|
2022-07-19 06:45:27 +02:00
|
|
|
sum.push_back("HTT");
|
2011-01-26 22:27:26 +01:00
|
|
|
if (bAVX)
|
2022-07-19 06:45:27 +02:00
|
|
|
sum.push_back("AVX");
|
2014-08-24 18:14:54 +02:00
|
|
|
if (bBMI1)
|
2022-07-19 06:45:27 +02:00
|
|
|
sum.push_back("BMI1");
|
2014-08-24 18:14:54 +02:00
|
|
|
if (bBMI2)
|
2022-07-19 06:45:27 +02:00
|
|
|
sum.push_back("BMI2");
|
2013-11-13 04:46:34 +01:00
|
|
|
if (bFMA)
|
2022-07-19 06:45:27 +02:00
|
|
|
sum.push_back("FMA");
|
2014-03-16 01:41:37 +01:00
|
|
|
if (bMOVBE)
|
2022-07-19 06:45:27 +02:00
|
|
|
sum.push_back("MOVBE");
|
|
|
|
if (bAES)
|
|
|
|
sum.push_back("AES");
|
|
|
|
if (bCRC32)
|
|
|
|
sum.push_back("CRC32");
|
|
|
|
if (bSHA1)
|
|
|
|
sum.push_back("SHA1");
|
|
|
|
if (bSHA2)
|
|
|
|
sum.push_back("SHA2");
|
|
|
|
|
2024-09-22 09:24:21 +02:00
|
|
|
return fmt::to_string(fmt::join(sum, ","));
|
2016-06-24 10:43:46 +02:00
|
|
|
}
|