amd-pstate-ut.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * AMD Processor P-state Frequency Driver Unit Test
  4. *
  5. * Copyright (C) 2022 Advanced Micro Devices, Inc. All Rights Reserved.
  6. *
  7. * Author: Meng Li <li.meng@amd.com>
  8. *
  9. * The AMD P-State Unit Test is a test module for testing the amd-pstate
  10. * driver. 1) It can help all users to verify their processor support
  11. * (SBIOS/Firmware or Hardware). 2) Kernel can have a basic function
  12. * test to avoid the kernel regression during the update. 3) We can
  13. * introduce more functional or performance tests to align the result
  14. * together, it will benefit power and performance scale optimization.
  15. *
  16. * This driver implements basic framework with plans to enhance it with
  17. * additional test cases to improve the depth and coverage of the test.
  18. *
  19. * See Documentation/admin-guide/pm/amd-pstate.rst Unit Tests for
  20. * amd-pstate to get more detail.
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/fs.h>
  27. #include <acpi/cppc_acpi.h>
  28. #include "amd-pstate.h"
  29. /*
  30. * Abbreviations:
  31. * amd_pstate_ut: used as a shortform for AMD P-State unit test.
  32. * It helps to keep variable names smaller, simpler
  33. */
  34. enum amd_pstate_ut_result {
  35. AMD_PSTATE_UT_RESULT_PASS,
  36. AMD_PSTATE_UT_RESULT_FAIL,
  37. };
  38. struct amd_pstate_ut_struct {
  39. const char *name;
  40. void (*func)(u32 index);
  41. enum amd_pstate_ut_result result;
  42. };
  43. /*
  44. * Kernel module for testing the AMD P-State unit test
  45. */
  46. static void amd_pstate_ut_acpi_cpc_valid(u32 index);
  47. static void amd_pstate_ut_check_enabled(u32 index);
  48. static void amd_pstate_ut_check_perf(u32 index);
  49. static void amd_pstate_ut_check_freq(u32 index);
  50. static void amd_pstate_ut_check_driver(u32 index);
  51. static struct amd_pstate_ut_struct amd_pstate_ut_cases[] = {
  52. {"amd_pstate_ut_acpi_cpc_valid", amd_pstate_ut_acpi_cpc_valid },
  53. {"amd_pstate_ut_check_enabled", amd_pstate_ut_check_enabled },
  54. {"amd_pstate_ut_check_perf", amd_pstate_ut_check_perf },
  55. {"amd_pstate_ut_check_freq", amd_pstate_ut_check_freq },
  56. {"amd_pstate_ut_check_driver", amd_pstate_ut_check_driver }
  57. };
  58. static bool get_shared_mem(void)
  59. {
  60. bool result = false;
  61. if (!boot_cpu_has(X86_FEATURE_CPPC))
  62. result = true;
  63. return result;
  64. }
  65. /*
  66. * check the _CPC object is present in SBIOS.
  67. */
  68. static void amd_pstate_ut_acpi_cpc_valid(u32 index)
  69. {
  70. if (acpi_cpc_valid())
  71. amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_PASS;
  72. else {
  73. amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
  74. pr_err("%s the _CPC object is not present in SBIOS!\n", __func__);
  75. }
  76. }
  77. static void amd_pstate_ut_pstate_enable(u32 index)
  78. {
  79. int ret = 0;
  80. u64 cppc_enable = 0;
  81. ret = rdmsrl_safe(MSR_AMD_CPPC_ENABLE, &cppc_enable);
  82. if (ret) {
  83. amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
  84. pr_err("%s rdmsrl_safe MSR_AMD_CPPC_ENABLE ret=%d error!\n", __func__, ret);
  85. return;
  86. }
  87. if (cppc_enable)
  88. amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_PASS;
  89. else {
  90. amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
  91. pr_err("%s amd pstate must be enabled!\n", __func__);
  92. }
  93. }
  94. /*
  95. * check if amd pstate is enabled
  96. */
  97. static void amd_pstate_ut_check_enabled(u32 index)
  98. {
  99. if (get_shared_mem())
  100. amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_PASS;
  101. else
  102. amd_pstate_ut_pstate_enable(index);
  103. }
  104. /*
  105. * check if performance values are reasonable.
  106. * highest_perf >= nominal_perf > lowest_nonlinear_perf > lowest_perf > 0
  107. */
  108. static void amd_pstate_ut_check_perf(u32 index)
  109. {
  110. int cpu = 0, ret = 0;
  111. u32 highest_perf = 0, nominal_perf = 0, lowest_nonlinear_perf = 0, lowest_perf = 0;
  112. u64 cap1 = 0;
  113. struct cppc_perf_caps cppc_perf;
  114. struct cpufreq_policy *policy = NULL;
  115. struct amd_cpudata *cpudata = NULL;
  116. for_each_possible_cpu(cpu) {
  117. policy = cpufreq_cpu_get(cpu);
  118. if (!policy)
  119. break;
  120. cpudata = policy->driver_data;
  121. if (get_shared_mem()) {
  122. ret = cppc_get_perf_caps(cpu, &cppc_perf);
  123. if (ret) {
  124. amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
  125. pr_err("%s cppc_get_perf_caps ret=%d error!\n", __func__, ret);
  126. goto skip_test;
  127. }
  128. highest_perf = cppc_perf.highest_perf;
  129. nominal_perf = cppc_perf.nominal_perf;
  130. lowest_nonlinear_perf = cppc_perf.lowest_nonlinear_perf;
  131. lowest_perf = cppc_perf.lowest_perf;
  132. } else {
  133. ret = rdmsrl_safe_on_cpu(cpu, MSR_AMD_CPPC_CAP1, &cap1);
  134. if (ret) {
  135. amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
  136. pr_err("%s read CPPC_CAP1 ret=%d error!\n", __func__, ret);
  137. goto skip_test;
  138. }
  139. highest_perf = AMD_CPPC_HIGHEST_PERF(cap1);
  140. nominal_perf = AMD_CPPC_NOMINAL_PERF(cap1);
  141. lowest_nonlinear_perf = AMD_CPPC_LOWNONLIN_PERF(cap1);
  142. lowest_perf = AMD_CPPC_LOWEST_PERF(cap1);
  143. }
  144. if (highest_perf != READ_ONCE(cpudata->highest_perf) && !cpudata->hw_prefcore) {
  145. pr_err("%s cpu%d highest=%d %d highest perf doesn't match\n",
  146. __func__, cpu, highest_perf, cpudata->highest_perf);
  147. goto skip_test;
  148. }
  149. if ((nominal_perf != READ_ONCE(cpudata->nominal_perf)) ||
  150. (lowest_nonlinear_perf != READ_ONCE(cpudata->lowest_nonlinear_perf)) ||
  151. (lowest_perf != READ_ONCE(cpudata->lowest_perf))) {
  152. amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
  153. pr_err("%s cpu%d nominal=%d %d lowest_nonlinear=%d %d lowest=%d %d, they should be equal!\n",
  154. __func__, cpu, nominal_perf, cpudata->nominal_perf,
  155. lowest_nonlinear_perf, cpudata->lowest_nonlinear_perf,
  156. lowest_perf, cpudata->lowest_perf);
  157. goto skip_test;
  158. }
  159. if (!((highest_perf >= nominal_perf) &&
  160. (nominal_perf > lowest_nonlinear_perf) &&
  161. (lowest_nonlinear_perf > lowest_perf) &&
  162. (lowest_perf > 0))) {
  163. amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
  164. pr_err("%s cpu%d highest=%d >= nominal=%d > lowest_nonlinear=%d > lowest=%d > 0, the formula is incorrect!\n",
  165. __func__, cpu, highest_perf, nominal_perf,
  166. lowest_nonlinear_perf, lowest_perf);
  167. goto skip_test;
  168. }
  169. cpufreq_cpu_put(policy);
  170. }
  171. amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_PASS;
  172. return;
  173. skip_test:
  174. cpufreq_cpu_put(policy);
  175. }
  176. /*
  177. * Check if frequency values are reasonable.
  178. * max_freq >= nominal_freq > lowest_nonlinear_freq > min_freq > 0
  179. * check max freq when set support boost mode.
  180. */
  181. static void amd_pstate_ut_check_freq(u32 index)
  182. {
  183. int cpu = 0;
  184. struct cpufreq_policy *policy = NULL;
  185. struct amd_cpudata *cpudata = NULL;
  186. u32 nominal_freq_khz;
  187. for_each_possible_cpu(cpu) {
  188. policy = cpufreq_cpu_get(cpu);
  189. if (!policy)
  190. break;
  191. cpudata = policy->driver_data;
  192. nominal_freq_khz = cpudata->nominal_freq*1000;
  193. if (!((cpudata->max_freq >= nominal_freq_khz) &&
  194. (nominal_freq_khz > cpudata->lowest_nonlinear_freq) &&
  195. (cpudata->lowest_nonlinear_freq > cpudata->min_freq) &&
  196. (cpudata->min_freq > 0))) {
  197. amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
  198. pr_err("%s cpu%d max=%d >= nominal=%d > lowest_nonlinear=%d > min=%d > 0, the formula is incorrect!\n",
  199. __func__, cpu, cpudata->max_freq, nominal_freq_khz,
  200. cpudata->lowest_nonlinear_freq, cpudata->min_freq);
  201. goto skip_test;
  202. }
  203. if (cpudata->min_freq != policy->min) {
  204. amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
  205. pr_err("%s cpu%d cpudata_min_freq=%d policy_min=%d, they should be equal!\n",
  206. __func__, cpu, cpudata->min_freq, policy->min);
  207. goto skip_test;
  208. }
  209. if (cpudata->boost_supported) {
  210. if ((policy->max == cpudata->max_freq) ||
  211. (policy->max == nominal_freq_khz))
  212. amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_PASS;
  213. else {
  214. amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
  215. pr_err("%s cpu%d policy_max=%d should be equal cpu_max=%d or cpu_nominal=%d !\n",
  216. __func__, cpu, policy->max, cpudata->max_freq,
  217. nominal_freq_khz);
  218. goto skip_test;
  219. }
  220. } else {
  221. amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
  222. pr_err("%s cpu%d must support boost!\n", __func__, cpu);
  223. goto skip_test;
  224. }
  225. cpufreq_cpu_put(policy);
  226. }
  227. amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_PASS;
  228. return;
  229. skip_test:
  230. cpufreq_cpu_put(policy);
  231. }
  232. static int amd_pstate_set_mode(enum amd_pstate_mode mode)
  233. {
  234. const char *mode_str = amd_pstate_get_mode_string(mode);
  235. pr_debug("->setting mode to %s\n", mode_str);
  236. return amd_pstate_update_status(mode_str, strlen(mode_str));
  237. }
  238. static void amd_pstate_ut_check_driver(u32 index)
  239. {
  240. enum amd_pstate_mode mode1, mode2 = AMD_PSTATE_DISABLE;
  241. int ret;
  242. for (mode1 = AMD_PSTATE_DISABLE; mode1 < AMD_PSTATE_MAX; mode1++) {
  243. ret = amd_pstate_set_mode(mode1);
  244. if (ret)
  245. goto out;
  246. for (mode2 = AMD_PSTATE_DISABLE; mode2 < AMD_PSTATE_MAX; mode2++) {
  247. if (mode1 == mode2)
  248. continue;
  249. ret = amd_pstate_set_mode(mode2);
  250. if (ret)
  251. goto out;
  252. }
  253. }
  254. out:
  255. if (ret)
  256. pr_warn("%s: failed to update status for %s->%s: %d\n", __func__,
  257. amd_pstate_get_mode_string(mode1),
  258. amd_pstate_get_mode_string(mode2), ret);
  259. amd_pstate_ut_cases[index].result = ret ?
  260. AMD_PSTATE_UT_RESULT_FAIL :
  261. AMD_PSTATE_UT_RESULT_PASS;
  262. }
  263. static int __init amd_pstate_ut_init(void)
  264. {
  265. u32 i = 0, arr_size = ARRAY_SIZE(amd_pstate_ut_cases);
  266. for (i = 0; i < arr_size; i++) {
  267. amd_pstate_ut_cases[i].func(i);
  268. switch (amd_pstate_ut_cases[i].result) {
  269. case AMD_PSTATE_UT_RESULT_PASS:
  270. pr_info("%-4d %-20s\t success!\n", i+1, amd_pstate_ut_cases[i].name);
  271. break;
  272. case AMD_PSTATE_UT_RESULT_FAIL:
  273. default:
  274. pr_info("%-4d %-20s\t fail!\n", i+1, amd_pstate_ut_cases[i].name);
  275. break;
  276. }
  277. }
  278. return 0;
  279. }
  280. static void __exit amd_pstate_ut_exit(void)
  281. {
  282. }
  283. module_init(amd_pstate_ut_init);
  284. module_exit(amd_pstate_ut_exit);
  285. MODULE_AUTHOR("Meng Li <li.meng@amd.com>");
  286. MODULE_DESCRIPTION("AMD P-state driver Test module");
  287. MODULE_LICENSE("GPL");