1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * AMD Processor P-state Frequency Driver Unit Test
5 * Copyright (C) 2022 Advanced Micro Devices, Inc. All Rights Reserved.
7 * Author: Meng Li <li.meng@amd.com>
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.
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.
19 * See Documentation/admin-guide/pm/amd-pstate.rst Unit Tests for
20 * amd-pstate to get more detail.
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
29 #include <linux/amd-pstate.h>
31 #include <acpi/cppc_acpi.h>
35 * amd_pstate_ut: used as a shortform for AMD P-State unit test.
36 * It helps to keep variable names smaller, simpler
38 enum amd_pstate_ut_result {
39 AMD_PSTATE_UT_RESULT_PASS,
40 AMD_PSTATE_UT_RESULT_FAIL,
43 struct amd_pstate_ut_struct {
45 void (*func)(u32 index);
46 enum amd_pstate_ut_result result;
50 * Kernel module for testing the AMD P-State unit test
52 static void amd_pstate_ut_acpi_cpc_valid(u32 index);
53 static void amd_pstate_ut_check_enabled(u32 index);
54 static void amd_pstate_ut_check_perf(u32 index);
55 static void amd_pstate_ut_check_freq(u32 index);
57 static struct amd_pstate_ut_struct amd_pstate_ut_cases[] = {
58 {"amd_pstate_ut_acpi_cpc_valid", amd_pstate_ut_acpi_cpc_valid },
59 {"amd_pstate_ut_check_enabled", amd_pstate_ut_check_enabled },
60 {"amd_pstate_ut_check_perf", amd_pstate_ut_check_perf },
61 {"amd_pstate_ut_check_freq", amd_pstate_ut_check_freq }
64 static bool get_shared_mem(void)
67 char path[] = "/sys/module/amd_pstate/parameters/shared_mem";
69 struct file *filp = NULL;
73 if (!boot_cpu_has(X86_FEATURE_CPPC)) {
74 filp = filp_open(path, O_RDONLY, 0);
76 pr_err("%s unable to open %s file!\n", __func__, path);
78 ret = kernel_read(filp, &buf, sizeof(buf), &pos);
80 pr_err("%s read %s file fail ret=%ld!\n",
81 __func__, path, (long)ret);
82 filp_close(filp, NULL);
93 * check the _CPC object is present in SBIOS.
95 static void amd_pstate_ut_acpi_cpc_valid(u32 index)
98 amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_PASS;
100 amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
101 pr_err("%s the _CPC object is not present in SBIOS!\n", __func__);
105 static void amd_pstate_ut_pstate_enable(u32 index)
110 ret = rdmsrl_safe(MSR_AMD_CPPC_ENABLE, &cppc_enable);
112 amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
113 pr_err("%s rdmsrl_safe MSR_AMD_CPPC_ENABLE ret=%d error!\n", __func__, ret);
117 amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_PASS;
119 amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
120 pr_err("%s amd pstate must be enabled!\n", __func__);
125 * check if amd pstate is enabled
127 static void amd_pstate_ut_check_enabled(u32 index)
129 if (get_shared_mem())
130 amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_PASS;
132 amd_pstate_ut_pstate_enable(index);
136 * check if performance values are reasonable.
137 * highest_perf >= nominal_perf > lowest_nonlinear_perf > lowest_perf > 0
139 static void amd_pstate_ut_check_perf(u32 index)
141 int cpu = 0, ret = 0;
142 u32 highest_perf = 0, nominal_perf = 0, lowest_nonlinear_perf = 0, lowest_perf = 0;
144 struct cppc_perf_caps cppc_perf;
145 struct cpufreq_policy *policy = NULL;
146 struct amd_cpudata *cpudata = NULL;
148 highest_perf = amd_get_highest_perf();
150 for_each_possible_cpu(cpu) {
151 policy = cpufreq_cpu_get(cpu);
154 cpudata = policy->driver_data;
156 if (get_shared_mem()) {
157 ret = cppc_get_perf_caps(cpu, &cppc_perf);
159 amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
160 pr_err("%s cppc_get_perf_caps ret=%d error!\n", __func__, ret);
164 nominal_perf = cppc_perf.nominal_perf;
165 lowest_nonlinear_perf = cppc_perf.lowest_nonlinear_perf;
166 lowest_perf = cppc_perf.lowest_perf;
168 ret = rdmsrl_safe_on_cpu(cpu, MSR_AMD_CPPC_CAP1, &cap1);
170 amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
171 pr_err("%s read CPPC_CAP1 ret=%d error!\n", __func__, ret);
175 nominal_perf = AMD_CPPC_NOMINAL_PERF(cap1);
176 lowest_nonlinear_perf = AMD_CPPC_LOWNONLIN_PERF(cap1);
177 lowest_perf = AMD_CPPC_LOWEST_PERF(cap1);
180 if ((highest_perf != READ_ONCE(cpudata->highest_perf)) ||
181 (nominal_perf != READ_ONCE(cpudata->nominal_perf)) ||
182 (lowest_nonlinear_perf != READ_ONCE(cpudata->lowest_nonlinear_perf)) ||
183 (lowest_perf != READ_ONCE(cpudata->lowest_perf))) {
184 amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
185 pr_err("%s cpu%d highest=%d %d nominal=%d %d lowest_nonlinear=%d %d lowest=%d %d, they should be equal!\n",
186 __func__, cpu, highest_perf, cpudata->highest_perf,
187 nominal_perf, cpudata->nominal_perf,
188 lowest_nonlinear_perf, cpudata->lowest_nonlinear_perf,
189 lowest_perf, cpudata->lowest_perf);
193 if (!((highest_perf >= nominal_perf) &&
194 (nominal_perf > lowest_nonlinear_perf) &&
195 (lowest_nonlinear_perf > lowest_perf) &&
196 (lowest_perf > 0))) {
197 amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
198 pr_err("%s cpu%d highest=%d >= nominal=%d > lowest_nonlinear=%d > lowest=%d > 0, the formula is incorrect!\n",
199 __func__, cpu, highest_perf, nominal_perf,
200 lowest_nonlinear_perf, lowest_perf);
205 amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_PASS;
209 * Check if frequency values are reasonable.
210 * max_freq >= nominal_freq > lowest_nonlinear_freq > min_freq > 0
211 * check max freq when set support boost mode.
213 static void amd_pstate_ut_check_freq(u32 index)
216 struct cpufreq_policy *policy = NULL;
217 struct amd_cpudata *cpudata = NULL;
219 for_each_possible_cpu(cpu) {
220 policy = cpufreq_cpu_get(cpu);
223 cpudata = policy->driver_data;
225 if (!((cpudata->max_freq >= cpudata->nominal_freq) &&
226 (cpudata->nominal_freq > cpudata->lowest_nonlinear_freq) &&
227 (cpudata->lowest_nonlinear_freq > cpudata->min_freq) &&
228 (cpudata->min_freq > 0))) {
229 amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
230 pr_err("%s cpu%d max=%d >= nominal=%d > lowest_nonlinear=%d > min=%d > 0, the formula is incorrect!\n",
231 __func__, cpu, cpudata->max_freq, cpudata->nominal_freq,
232 cpudata->lowest_nonlinear_freq, cpudata->min_freq);
236 if (cpudata->min_freq != policy->min) {
237 amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
238 pr_err("%s cpu%d cpudata_min_freq=%d policy_min=%d, they should be equal!\n",
239 __func__, cpu, cpudata->min_freq, policy->min);
243 if (cpudata->boost_supported) {
244 if ((policy->max == cpudata->max_freq) ||
245 (policy->max == cpudata->nominal_freq))
246 amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_PASS;
248 amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
249 pr_err("%s cpu%d policy_max=%d should be equal cpu_max=%d or cpu_nominal=%d !\n",
250 __func__, cpu, policy->max, cpudata->max_freq,
251 cpudata->nominal_freq);
255 amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL;
256 pr_err("%s cpu%d must support boost!\n", __func__, cpu);
261 amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_PASS;
264 static int __init amd_pstate_ut_init(void)
266 u32 i = 0, arr_size = ARRAY_SIZE(amd_pstate_ut_cases);
268 for (i = 0; i < arr_size; i++) {
269 amd_pstate_ut_cases[i].func(i);
270 switch (amd_pstate_ut_cases[i].result) {
271 case AMD_PSTATE_UT_RESULT_PASS:
272 pr_info("%-4d %-20s\t success!\n", i+1, amd_pstate_ut_cases[i].name);
274 case AMD_PSTATE_UT_RESULT_FAIL:
276 pr_info("%-4d %-20s\t fail!\n", i+1, amd_pstate_ut_cases[i].name);
284 static void __exit amd_pstate_ut_exit(void)
288 module_init(amd_pstate_ut_init);
289 module_exit(amd_pstate_ut_exit);
291 MODULE_AUTHOR("Meng Li <li.meng@amd.com>");
292 MODULE_DESCRIPTION("AMD P-state driver Test module");
293 MODULE_LICENSE("GPL");