cpu_detect.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
  2. // SPDX-FileCopyrightText: Copyright 2013 Dolphin Emulator Project / 2015 Citra Emulator Project
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. #include <array>
  5. #include <cstring>
  6. #include <fstream>
  7. #include <iterator>
  8. #include <optional>
  9. #include <string_view>
  10. #include <thread>
  11. #include <vector>
  12. #include "common/bit_util.h"
  13. #include "common/common_types.h"
  14. #include "common/logging/log.h"
  15. #include "common/x64/cpu_detect.h"
  16. #include "common/x64/rdtsc.h"
  17. #ifdef _WIN32
  18. #include <windows.h>
  19. #endif
  20. #ifdef _MSC_VER
  21. #include <intrin.h>
  22. static inline u64 xgetbv(u32 index) {
  23. return _xgetbv(index);
  24. }
  25. #else
  26. #if defined(__DragonFly__) || defined(__FreeBSD__)
  27. // clang-format off
  28. #include <sys/types.h>
  29. #include <machine/cpufunc.h>
  30. // clang-format on
  31. #endif
  32. static inline void __cpuidex(int info[4], u32 function_id, u32 subfunction_id) {
  33. #if defined(__DragonFly__) || defined(__FreeBSD__)
  34. // Despite the name, this is just do_cpuid() with ECX as second input.
  35. cpuid_count((u_int)function_id, (u_int)subfunction_id, (u_int*)info);
  36. #else
  37. info[0] = function_id; // eax
  38. info[2] = subfunction_id; // ecx
  39. __asm__("cpuid"
  40. : "=a"(info[0]), "=b"(info[1]), "=c"(info[2]), "=d"(info[3])
  41. : "a"(function_id), "c"(subfunction_id));
  42. #endif
  43. }
  44. static inline void __cpuid(int info[4], u32 function_id) {
  45. return __cpuidex(info, function_id, 0);
  46. }
  47. #define _XCR_XFEATURE_ENABLED_MASK 0
  48. static inline u64 xgetbv(u32 index) {
  49. u32 eax, edx;
  50. __asm__ __volatile__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(index));
  51. return ((u64)edx << 32) | eax;
  52. }
  53. #endif // _MSC_VER
  54. namespace Common {
  55. CPUCaps::Manufacturer CPUCaps::ParseManufacturer(std::string_view brand_string) {
  56. if (brand_string == "GenuineIntel") {
  57. return Manufacturer::Intel;
  58. } else if (brand_string == "AuthenticAMD") {
  59. return Manufacturer::AMD;
  60. } else if (brand_string == "HygonGenuine") {
  61. return Manufacturer::Hygon;
  62. }
  63. return Manufacturer::Unknown;
  64. }
  65. // Detects the various CPU features
  66. static CPUCaps Detect() {
  67. CPUCaps caps = {};
  68. // Assumes the CPU supports the CPUID instruction. Those that don't would likely not support
  69. // yuzu at all anyway
  70. int cpu_id[4];
  71. // Detect CPU's CPUID capabilities and grab manufacturer string
  72. __cpuid(cpu_id, 0x00000000);
  73. const u32 max_std_fn = cpu_id[0]; // EAX
  74. std::memset(caps.brand_string, 0, std::size(caps.brand_string));
  75. std::memcpy(&caps.brand_string[0], &cpu_id[1], sizeof(u32));
  76. std::memcpy(&caps.brand_string[4], &cpu_id[3], sizeof(u32));
  77. std::memcpy(&caps.brand_string[8], &cpu_id[2], sizeof(u32));
  78. caps.manufacturer = CPUCaps::ParseManufacturer(caps.brand_string);
  79. // Set reasonable default cpu string even if brand string not available
  80. std::strncpy(caps.cpu_string, caps.brand_string, std::size(caps.brand_string));
  81. __cpuid(cpu_id, 0x80000000);
  82. const u32 max_ex_fn = cpu_id[0];
  83. // Detect family and other miscellaneous features
  84. if (max_std_fn >= 1) {
  85. __cpuid(cpu_id, 0x00000001);
  86. caps.sse = Common::Bit<25>(cpu_id[3]);
  87. caps.sse2 = Common::Bit<26>(cpu_id[3]);
  88. caps.sse3 = Common::Bit<0>(cpu_id[2]);
  89. caps.pclmulqdq = Common::Bit<1>(cpu_id[2]);
  90. caps.ssse3 = Common::Bit<9>(cpu_id[2]);
  91. caps.sse4_1 = Common::Bit<19>(cpu_id[2]);
  92. caps.sse4_2 = Common::Bit<20>(cpu_id[2]);
  93. caps.movbe = Common::Bit<22>(cpu_id[2]);
  94. caps.popcnt = Common::Bit<23>(cpu_id[2]);
  95. caps.aes = Common::Bit<25>(cpu_id[2]);
  96. caps.f16c = Common::Bit<29>(cpu_id[2]);
  97. // AVX support requires 3 separate checks:
  98. // - Is the AVX bit set in CPUID?
  99. // - Is the XSAVE bit set in CPUID?
  100. // - XGETBV result has the XCR bit set.
  101. if (Common::Bit<28>(cpu_id[2]) && Common::Bit<27>(cpu_id[2])) {
  102. if ((xgetbv(_XCR_XFEATURE_ENABLED_MASK) & 0x6) == 0x6) {
  103. caps.avx = true;
  104. if (Common::Bit<12>(cpu_id[2]))
  105. caps.fma = true;
  106. }
  107. }
  108. if (max_std_fn >= 7) {
  109. __cpuidex(cpu_id, 0x00000007, 0x00000000);
  110. // Can't enable AVX{2,512} unless the XSAVE/XGETBV checks above passed
  111. if (caps.avx) {
  112. caps.avx2 = Common::Bit<5>(cpu_id[1]);
  113. caps.avx512f = Common::Bit<16>(cpu_id[1]);
  114. caps.avx512dq = Common::Bit<17>(cpu_id[1]);
  115. caps.avx512cd = Common::Bit<28>(cpu_id[1]);
  116. caps.avx512bw = Common::Bit<30>(cpu_id[1]);
  117. caps.avx512vl = Common::Bit<31>(cpu_id[1]);
  118. caps.avx512vbmi = Common::Bit<1>(cpu_id[2]);
  119. caps.avx512bitalg = Common::Bit<12>(cpu_id[2]);
  120. }
  121. caps.bmi1 = Common::Bit<3>(cpu_id[1]);
  122. caps.bmi2 = Common::Bit<8>(cpu_id[1]);
  123. caps.sha = Common::Bit<29>(cpu_id[1]);
  124. caps.waitpkg = Common::Bit<5>(cpu_id[2]);
  125. caps.gfni = Common::Bit<8>(cpu_id[2]);
  126. __cpuidex(cpu_id, 0x00000007, 0x00000001);
  127. caps.avx_vnni = caps.avx && Common::Bit<4>(cpu_id[0]);
  128. }
  129. }
  130. if (max_ex_fn >= 0x80000004) {
  131. // Extract CPU model string
  132. __cpuid(cpu_id, 0x80000002);
  133. std::memcpy(caps.cpu_string, cpu_id, sizeof(cpu_id));
  134. __cpuid(cpu_id, 0x80000003);
  135. std::memcpy(caps.cpu_string + 16, cpu_id, sizeof(cpu_id));
  136. __cpuid(cpu_id, 0x80000004);
  137. std::memcpy(caps.cpu_string + 32, cpu_id, sizeof(cpu_id));
  138. }
  139. if (max_ex_fn >= 0x80000001) {
  140. // Check for more features
  141. __cpuid(cpu_id, 0x80000001);
  142. caps.lzcnt = Common::Bit<5>(cpu_id[2]);
  143. caps.fma4 = Common::Bit<16>(cpu_id[2]);
  144. }
  145. if (max_ex_fn >= 0x80000007) {
  146. __cpuid(cpu_id, 0x80000007);
  147. caps.invariant_tsc = Common::Bit<8>(cpu_id[3]);
  148. }
  149. if (max_std_fn >= 0x15) {
  150. __cpuid(cpu_id, 0x15);
  151. caps.tsc_crystal_ratio_denominator = cpu_id[0];
  152. caps.tsc_crystal_ratio_numerator = cpu_id[1];
  153. caps.crystal_frequency = cpu_id[2];
  154. // Some CPU models might not return a crystal frequency.
  155. // The CPU model can be detected to use the values from turbostat
  156. // https://github.com/torvalds/linux/blob/master/tools/power/x86/turbostat/turbostat.c#L5569
  157. // but it's easier to just estimate the TSC tick rate for these cases.
  158. if (caps.tsc_crystal_ratio_denominator) {
  159. caps.tsc_frequency = static_cast<u64>(caps.crystal_frequency) *
  160. caps.tsc_crystal_ratio_numerator /
  161. caps.tsc_crystal_ratio_denominator;
  162. } else {
  163. caps.tsc_frequency = X64::EstimateRDTSCFrequency();
  164. }
  165. }
  166. if (max_std_fn >= 0x16) {
  167. __cpuid(cpu_id, 0x16);
  168. caps.base_frequency = cpu_id[0];
  169. caps.max_frequency = cpu_id[1];
  170. caps.bus_frequency = cpu_id[2];
  171. }
  172. return caps;
  173. }
  174. const CPUCaps& GetCPUCaps() {
  175. static CPUCaps caps = Detect();
  176. return caps;
  177. }
  178. std::optional<int> GetProcessorCount() {
  179. #if defined(_WIN32)
  180. // Get the buffer length.
  181. DWORD length = 0;
  182. GetLogicalProcessorInformation(nullptr, &length);
  183. if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
  184. LOG_ERROR(Frontend, "Failed to query core count.");
  185. return std::nullopt;
  186. }
  187. std::vector<SYSTEM_LOGICAL_PROCESSOR_INFORMATION> buffer(
  188. length / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION));
  189. // Now query the core count.
  190. if (!GetLogicalProcessorInformation(buffer.data(), &length)) {
  191. LOG_ERROR(Frontend, "Failed to query core count.");
  192. return std::nullopt;
  193. }
  194. return static_cast<int>(
  195. std::count_if(buffer.cbegin(), buffer.cend(), [](const auto& proc_info) {
  196. return proc_info.Relationship == RelationProcessorCore;
  197. }));
  198. #elif defined(__unix__)
  199. const int thread_count = std::thread::hardware_concurrency();
  200. std::ifstream smt("/sys/devices/system/cpu/smt/active");
  201. char state = '0';
  202. if (smt) {
  203. smt.read(&state, sizeof(state));
  204. }
  205. switch (state) {
  206. case '0':
  207. return thread_count;
  208. case '1':
  209. return thread_count / 2;
  210. default:
  211. return std::nullopt;
  212. }
  213. #else
  214. // Shame on you
  215. return std::nullopt;
  216. #endif
  217. }
  218. } // namespace Common