cpu_detect.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 <iterator>
  7. #include <string_view>
  8. #include "common/bit_util.h"
  9. #include "common/common_types.h"
  10. #include "common/x64/cpu_detect.h"
  11. #ifdef _MSC_VER
  12. #include <intrin.h>
  13. #else
  14. #if defined(__DragonFly__) || defined(__FreeBSD__)
  15. // clang-format off
  16. #include <sys/types.h>
  17. #include <machine/cpufunc.h>
  18. // clang-format on
  19. #endif
  20. static inline void __cpuidex(int info[4], u32 function_id, u32 subfunction_id) {
  21. #if defined(__DragonFly__) || defined(__FreeBSD__)
  22. // Despite the name, this is just do_cpuid() with ECX as second input.
  23. cpuid_count((u_int)function_id, (u_int)subfunction_id, (u_int*)info);
  24. #else
  25. info[0] = function_id; // eax
  26. info[2] = subfunction_id; // ecx
  27. __asm__("cpuid"
  28. : "=a"(info[0]), "=b"(info[1]), "=c"(info[2]), "=d"(info[3])
  29. : "a"(function_id), "c"(subfunction_id));
  30. #endif
  31. }
  32. static inline void __cpuid(int info[4], u32 function_id) {
  33. return __cpuidex(info, function_id, 0);
  34. }
  35. #define _XCR_XFEATURE_ENABLED_MASK 0
  36. static inline u64 _xgetbv(u32 index) {
  37. u32 eax, edx;
  38. __asm__ __volatile__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(index));
  39. return ((u64)edx << 32) | eax;
  40. }
  41. #endif // _MSC_VER
  42. namespace Common {
  43. CPUCaps::Manufacturer CPUCaps::ParseManufacturer(std::string_view brand_string) {
  44. if (brand_string == "GenuineIntel") {
  45. return Manufacturer::Intel;
  46. } else if (brand_string == "AuthenticAMD") {
  47. return Manufacturer::AMD;
  48. } else if (brand_string == "HygonGenuine") {
  49. return Manufacturer::Hygon;
  50. }
  51. return Manufacturer::Unknown;
  52. }
  53. // Detects the various CPU features
  54. static CPUCaps Detect() {
  55. CPUCaps caps = {};
  56. // Assumes the CPU supports the CPUID instruction. Those that don't would likely not support
  57. // yuzu at all anyway
  58. int cpu_id[4];
  59. // Detect CPU's CPUID capabilities and grab manufacturer string
  60. __cpuid(cpu_id, 0x00000000);
  61. const u32 max_std_fn = cpu_id[0]; // EAX
  62. std::memset(caps.brand_string, 0, std::size(caps.brand_string));
  63. std::memcpy(&caps.brand_string[0], &cpu_id[1], sizeof(u32));
  64. std::memcpy(&caps.brand_string[4], &cpu_id[3], sizeof(u32));
  65. std::memcpy(&caps.brand_string[8], &cpu_id[2], sizeof(u32));
  66. caps.manufacturer = CPUCaps::ParseManufacturer(caps.brand_string);
  67. // Set reasonable default cpu string even if brand string not available
  68. std::strncpy(caps.cpu_string, caps.brand_string, std::size(caps.brand_string));
  69. __cpuid(cpu_id, 0x80000000);
  70. const u32 max_ex_fn = cpu_id[0];
  71. // Detect family and other miscellaneous features
  72. if (max_std_fn >= 1) {
  73. __cpuid(cpu_id, 0x00000001);
  74. caps.sse = Common::Bit<25>(cpu_id[3]);
  75. caps.sse2 = Common::Bit<26>(cpu_id[3]);
  76. caps.sse3 = Common::Bit<0>(cpu_id[2]);
  77. caps.pclmulqdq = Common::Bit<1>(cpu_id[2]);
  78. caps.ssse3 = Common::Bit<9>(cpu_id[2]);
  79. caps.sse4_1 = Common::Bit<19>(cpu_id[2]);
  80. caps.sse4_2 = Common::Bit<20>(cpu_id[2]);
  81. caps.movbe = Common::Bit<22>(cpu_id[2]);
  82. caps.popcnt = Common::Bit<23>(cpu_id[2]);
  83. caps.aes = Common::Bit<25>(cpu_id[2]);
  84. caps.f16c = Common::Bit<29>(cpu_id[2]);
  85. // AVX support requires 3 separate checks:
  86. // - Is the AVX bit set in CPUID?
  87. // - Is the XSAVE bit set in CPUID?
  88. // - XGETBV result has the XCR bit set.
  89. if (Common::Bit<28>(cpu_id[2]) && Common::Bit<27>(cpu_id[2])) {
  90. if ((_xgetbv(_XCR_XFEATURE_ENABLED_MASK) & 0x6) == 0x6) {
  91. caps.avx = true;
  92. if (Common::Bit<12>(cpu_id[2]))
  93. caps.fma = true;
  94. }
  95. }
  96. if (max_std_fn >= 7) {
  97. __cpuidex(cpu_id, 0x00000007, 0x00000000);
  98. // Can't enable AVX{2,512} unless the XSAVE/XGETBV checks above passed
  99. if (caps.avx) {
  100. caps.avx2 = Common::Bit<5>(cpu_id[1]);
  101. caps.avx512f = Common::Bit<16>(cpu_id[1]);
  102. caps.avx512dq = Common::Bit<17>(cpu_id[1]);
  103. caps.avx512cd = Common::Bit<28>(cpu_id[1]);
  104. caps.avx512bw = Common::Bit<30>(cpu_id[1]);
  105. caps.avx512vl = Common::Bit<31>(cpu_id[1]);
  106. caps.avx512vbmi = Common::Bit<1>(cpu_id[2]);
  107. caps.avx512bitalg = Common::Bit<12>(cpu_id[2]);
  108. }
  109. caps.bmi1 = Common::Bit<3>(cpu_id[1]);
  110. caps.bmi2 = Common::Bit<8>(cpu_id[1]);
  111. caps.sha = Common::Bit<29>(cpu_id[1]);
  112. caps.gfni = Common::Bit<8>(cpu_id[2]);
  113. __cpuidex(cpu_id, 0x00000007, 0x00000001);
  114. caps.avx_vnni = caps.avx && Common::Bit<4>(cpu_id[0]);
  115. }
  116. }
  117. if (max_ex_fn >= 0x80000004) {
  118. // Extract CPU model string
  119. __cpuid(cpu_id, 0x80000002);
  120. std::memcpy(caps.cpu_string, cpu_id, sizeof(cpu_id));
  121. __cpuid(cpu_id, 0x80000003);
  122. std::memcpy(caps.cpu_string + 16, cpu_id, sizeof(cpu_id));
  123. __cpuid(cpu_id, 0x80000004);
  124. std::memcpy(caps.cpu_string + 32, cpu_id, sizeof(cpu_id));
  125. }
  126. if (max_ex_fn >= 0x80000001) {
  127. // Check for more features
  128. __cpuid(cpu_id, 0x80000001);
  129. caps.lzcnt = Common::Bit<5>(cpu_id[2]);
  130. caps.fma4 = Common::Bit<16>(cpu_id[2]);
  131. }
  132. if (max_ex_fn >= 0x80000007) {
  133. __cpuid(cpu_id, 0x80000007);
  134. caps.invariant_tsc = Common::Bit<8>(cpu_id[3]);
  135. }
  136. if (max_std_fn >= 0x15) {
  137. __cpuid(cpu_id, 0x15);
  138. caps.tsc_crystal_ratio_denominator = cpu_id[0];
  139. caps.tsc_crystal_ratio_numerator = cpu_id[1];
  140. caps.crystal_frequency = cpu_id[2];
  141. // Some CPU models might not return a crystal frequency.
  142. // The CPU model can be detected to use the values from turbostat
  143. // https://github.com/torvalds/linux/blob/master/tools/power/x86/turbostat/turbostat.c#L5569
  144. // but it's easier to just estimate the TSC tick rate for these cases.
  145. caps.tsc_frequency = static_cast<u64>(caps.crystal_frequency) *
  146. caps.tsc_crystal_ratio_numerator / caps.tsc_crystal_ratio_denominator;
  147. }
  148. if (max_std_fn >= 0x16) {
  149. __cpuid(cpu_id, 0x16);
  150. caps.base_frequency = cpu_id[0];
  151. caps.max_frequency = cpu_id[1];
  152. caps.bus_frequency = cpu_id[2];
  153. }
  154. return caps;
  155. }
  156. const CPUCaps& GetCPUCaps() {
  157. static CPUCaps caps = Detect();
  158. return caps;
  159. }
  160. } // namespace Common