cppc.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * cppc.c: CPPC Interface for x86
  4. * Copyright (c) 2016, Intel Corporation.
  5. */
  6. #include <acpi/cppc_acpi.h>
  7. #include <asm/msr.h>
  8. #include <asm/processor.h>
  9. #include <asm/topology.h>
  10. #define CPPC_HIGHEST_PERF_PERFORMANCE 196
  11. #define CPPC_HIGHEST_PERF_PREFCORE 166
  12. enum amd_pref_core {
  13. AMD_PREF_CORE_UNKNOWN = 0,
  14. AMD_PREF_CORE_SUPPORTED,
  15. AMD_PREF_CORE_UNSUPPORTED,
  16. };
  17. static enum amd_pref_core amd_pref_core_detected;
  18. static u64 boost_numerator;
  19. /* Refer to drivers/acpi/cppc_acpi.c for the description of functions */
  20. bool cpc_supported_by_cpu(void)
  21. {
  22. switch (boot_cpu_data.x86_vendor) {
  23. case X86_VENDOR_AMD:
  24. case X86_VENDOR_HYGON:
  25. if (boot_cpu_data.x86 == 0x19 && ((boot_cpu_data.x86_model <= 0x0f) ||
  26. (boot_cpu_data.x86_model >= 0x20 && boot_cpu_data.x86_model <= 0x2f)))
  27. return true;
  28. else if (boot_cpu_data.x86 == 0x17 &&
  29. boot_cpu_data.x86_model >= 0x30 && boot_cpu_data.x86_model <= 0x7f)
  30. return true;
  31. return boot_cpu_has(X86_FEATURE_CPPC);
  32. }
  33. return false;
  34. }
  35. bool cpc_ffh_supported(void)
  36. {
  37. return true;
  38. }
  39. int cpc_read_ffh(int cpunum, struct cpc_reg *reg, u64 *val)
  40. {
  41. int err;
  42. err = rdmsrl_safe_on_cpu(cpunum, reg->address, val);
  43. if (!err) {
  44. u64 mask = GENMASK_ULL(reg->bit_offset + reg->bit_width - 1,
  45. reg->bit_offset);
  46. *val &= mask;
  47. *val >>= reg->bit_offset;
  48. }
  49. return err;
  50. }
  51. int cpc_write_ffh(int cpunum, struct cpc_reg *reg, u64 val)
  52. {
  53. u64 rd_val;
  54. int err;
  55. err = rdmsrl_safe_on_cpu(cpunum, reg->address, &rd_val);
  56. if (!err) {
  57. u64 mask = GENMASK_ULL(reg->bit_offset + reg->bit_width - 1,
  58. reg->bit_offset);
  59. val <<= reg->bit_offset;
  60. val &= mask;
  61. rd_val &= ~mask;
  62. rd_val |= val;
  63. err = wrmsrl_safe_on_cpu(cpunum, reg->address, rd_val);
  64. }
  65. return err;
  66. }
  67. static void amd_set_max_freq_ratio(void)
  68. {
  69. struct cppc_perf_caps perf_caps;
  70. u64 numerator, nominal_perf;
  71. u64 perf_ratio;
  72. int rc;
  73. rc = cppc_get_perf_caps(0, &perf_caps);
  74. if (rc) {
  75. pr_warn("Could not retrieve perf counters (%d)\n", rc);
  76. return;
  77. }
  78. rc = amd_get_boost_ratio_numerator(0, &numerator);
  79. if (rc) {
  80. pr_warn("Could not retrieve highest performance (%d)\n", rc);
  81. return;
  82. }
  83. nominal_perf = perf_caps.nominal_perf;
  84. if (!nominal_perf) {
  85. pr_warn("Could not retrieve nominal performance\n");
  86. return;
  87. }
  88. /* midpoint between max_boost and max_P */
  89. perf_ratio = (div_u64(numerator * SCHED_CAPACITY_SCALE, nominal_perf) + SCHED_CAPACITY_SCALE) >> 1;
  90. freq_invariance_set_perf_ratio(perf_ratio, false);
  91. }
  92. static DEFINE_MUTEX(freq_invariance_lock);
  93. static inline void init_freq_invariance_cppc(void)
  94. {
  95. static bool init_done;
  96. if (!cpu_feature_enabled(X86_FEATURE_APERFMPERF))
  97. return;
  98. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
  99. return;
  100. mutex_lock(&freq_invariance_lock);
  101. if (!init_done)
  102. amd_set_max_freq_ratio();
  103. init_done = true;
  104. mutex_unlock(&freq_invariance_lock);
  105. }
  106. void acpi_processor_init_invariance_cppc(void)
  107. {
  108. init_freq_invariance_cppc();
  109. }
  110. /*
  111. * Get the highest performance register value.
  112. * @cpu: CPU from which to get highest performance.
  113. * @highest_perf: Return address for highest performance value.
  114. *
  115. * Return: 0 for success, negative error code otherwise.
  116. */
  117. int amd_get_highest_perf(unsigned int cpu, u32 *highest_perf)
  118. {
  119. u64 val;
  120. int ret;
  121. if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
  122. ret = rdmsrl_safe_on_cpu(cpu, MSR_AMD_CPPC_CAP1, &val);
  123. if (ret)
  124. goto out;
  125. val = AMD_CPPC_HIGHEST_PERF(val);
  126. } else {
  127. ret = cppc_get_highest_perf(cpu, &val);
  128. if (ret)
  129. goto out;
  130. }
  131. WRITE_ONCE(*highest_perf, (u32)val);
  132. out:
  133. return ret;
  134. }
  135. EXPORT_SYMBOL_GPL(amd_get_highest_perf);
  136. /**
  137. * amd_detect_prefcore: Detect if CPUs in the system support preferred cores
  138. * @detected: Output variable for the result of the detection.
  139. *
  140. * Determine whether CPUs in the system support preferred cores. On systems
  141. * that support preferred cores, different highest perf values will be found
  142. * on different cores. On other systems, the highest perf value will be the
  143. * same on all cores.
  144. *
  145. * The result of the detection will be stored in the 'detected' parameter.
  146. *
  147. * Return: 0 for success, negative error code otherwise
  148. */
  149. int amd_detect_prefcore(bool *detected)
  150. {
  151. int cpu, count = 0;
  152. u64 highest_perf[2] = {0};
  153. if (WARN_ON(!detected))
  154. return -EINVAL;
  155. switch (amd_pref_core_detected) {
  156. case AMD_PREF_CORE_SUPPORTED:
  157. *detected = true;
  158. return 0;
  159. case AMD_PREF_CORE_UNSUPPORTED:
  160. *detected = false;
  161. return 0;
  162. default:
  163. break;
  164. }
  165. for_each_present_cpu(cpu) {
  166. u32 tmp;
  167. int ret;
  168. ret = amd_get_highest_perf(cpu, &tmp);
  169. if (ret)
  170. return ret;
  171. if (!count || (count == 1 && tmp != highest_perf[0]))
  172. highest_perf[count++] = tmp;
  173. if (count == 2)
  174. break;
  175. }
  176. *detected = (count == 2);
  177. boost_numerator = highest_perf[0];
  178. amd_pref_core_detected = *detected ? AMD_PREF_CORE_SUPPORTED :
  179. AMD_PREF_CORE_UNSUPPORTED;
  180. pr_debug("AMD CPPC preferred core is %ssupported (highest perf: 0x%llx)\n",
  181. *detected ? "" : "un", highest_perf[0]);
  182. return 0;
  183. }
  184. EXPORT_SYMBOL_GPL(amd_detect_prefcore);
  185. /**
  186. * amd_get_boost_ratio_numerator: Get the numerator to use for boost ratio calculation
  187. * @cpu: CPU to get numerator for.
  188. * @numerator: Output variable for numerator.
  189. *
  190. * Determine the numerator to use for calculating the boost ratio on
  191. * a CPU. On systems that support preferred cores, this will be a hardcoded
  192. * value. On other systems this will the highest performance register value.
  193. *
  194. * If booting the system with amd-pstate enabled but preferred cores disabled then
  195. * the correct boost numerator will be returned to match hardware capabilities
  196. * even if the preferred cores scheduling hints are not enabled.
  197. *
  198. * Return: 0 for success, negative error code otherwise.
  199. */
  200. int amd_get_boost_ratio_numerator(unsigned int cpu, u64 *numerator)
  201. {
  202. bool prefcore;
  203. int ret;
  204. ret = amd_detect_prefcore(&prefcore);
  205. if (ret)
  206. return ret;
  207. /* without preferred cores, return the highest perf register value */
  208. if (!prefcore) {
  209. *numerator = boost_numerator;
  210. return 0;
  211. }
  212. /*
  213. * For AMD CPUs with Family ID 19H and Model ID range 0x70 to 0x7f,
  214. * the highest performance level is set to 196.
  215. * https://bugzilla.kernel.org/show_bug.cgi?id=218759
  216. */
  217. if (cpu_feature_enabled(X86_FEATURE_ZEN4)) {
  218. switch (boot_cpu_data.x86_model) {
  219. case 0x70 ... 0x7f:
  220. *numerator = CPPC_HIGHEST_PERF_PERFORMANCE;
  221. return 0;
  222. default:
  223. break;
  224. }
  225. }
  226. *numerator = CPPC_HIGHEST_PERF_PREFCORE;
  227. return 0;
  228. }
  229. EXPORT_SYMBOL_GPL(amd_get_boost_ratio_numerator);