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 | |
23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
24 | |
25 | #include <linux/kernel.h> |
26 | #include <linux/module.h> |
27 | #include <linux/moduleparam.h> |
28 | #include <linux/fs.h> |
29 | #include <linux/amd-pstate.h> |
30 | |
31 | #include <acpi/cppc_acpi.h> |
32 | |
33 | /* |
34 | * Abbreviations: |
35 | * amd_pstate_ut: used as a shortform for AMD P-State unit test. |
36 | * It helps to keep variable names smaller, simpler |
37 | */ |
38 | enum amd_pstate_ut_result { |
39 | AMD_PSTATE_UT_RESULT_PASS, |
40 | AMD_PSTATE_UT_RESULT_FAIL, |
41 | }; |
42 | |
43 | struct amd_pstate_ut_struct { |
44 | const char *name; |
45 | void (*func)(u32 index); |
46 | enum amd_pstate_ut_result result; |
47 | }; |
48 | |
49 | /* |
50 | * Kernel module for testing the AMD P-State unit test |
51 | */ |
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); |
56 | |
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 } |
62 | }; |
63 | |
64 | static bool get_shared_mem(void) |
65 | { |
66 | bool result = false; |
67 | |
68 | if (!boot_cpu_has(X86_FEATURE_CPPC)) |
69 | result = true; |
70 | |
71 | return result; |
72 | } |
73 | |
74 | /* |
75 | * check the _CPC object is present in SBIOS. |
76 | */ |
77 | static void amd_pstate_ut_acpi_cpc_valid(u32 index) |
78 | { |
79 | if (acpi_cpc_valid()) |
80 | amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_PASS; |
81 | else { |
82 | amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL; |
83 | pr_err("%s the _CPC object is not present in SBIOS!\n" , __func__); |
84 | } |
85 | } |
86 | |
87 | static void amd_pstate_ut_pstate_enable(u32 index) |
88 | { |
89 | int ret = 0; |
90 | u64 cppc_enable = 0; |
91 | |
92 | ret = rdmsrl_safe(MSR_AMD_CPPC_ENABLE, p: &cppc_enable); |
93 | if (ret) { |
94 | amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL; |
95 | pr_err("%s rdmsrl_safe MSR_AMD_CPPC_ENABLE ret=%d error!\n" , __func__, ret); |
96 | return; |
97 | } |
98 | if (cppc_enable) |
99 | amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_PASS; |
100 | else { |
101 | amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL; |
102 | pr_err("%s amd pstate must be enabled!\n" , __func__); |
103 | } |
104 | } |
105 | |
106 | /* |
107 | * check if amd pstate is enabled |
108 | */ |
109 | static void amd_pstate_ut_check_enabled(u32 index) |
110 | { |
111 | if (get_shared_mem()) |
112 | amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_PASS; |
113 | else |
114 | amd_pstate_ut_pstate_enable(index); |
115 | } |
116 | |
117 | /* |
118 | * check if performance values are reasonable. |
119 | * highest_perf >= nominal_perf > lowest_nonlinear_perf > lowest_perf > 0 |
120 | */ |
121 | static void amd_pstate_ut_check_perf(u32 index) |
122 | { |
123 | int cpu = 0, ret = 0; |
124 | u32 highest_perf = 0, nominal_perf = 0, lowest_nonlinear_perf = 0, lowest_perf = 0; |
125 | u64 cap1 = 0; |
126 | struct cppc_perf_caps cppc_perf; |
127 | struct cpufreq_policy *policy = NULL; |
128 | struct amd_cpudata *cpudata = NULL; |
129 | |
130 | for_each_possible_cpu(cpu) { |
131 | policy = cpufreq_cpu_get(cpu); |
132 | if (!policy) |
133 | break; |
134 | cpudata = policy->driver_data; |
135 | |
136 | if (get_shared_mem()) { |
137 | ret = cppc_get_perf_caps(cpu, caps: &cppc_perf); |
138 | if (ret) { |
139 | amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL; |
140 | pr_err("%s cppc_get_perf_caps ret=%d error!\n" , __func__, ret); |
141 | goto skip_test; |
142 | } |
143 | |
144 | highest_perf = cppc_perf.highest_perf; |
145 | nominal_perf = cppc_perf.nominal_perf; |
146 | lowest_nonlinear_perf = cppc_perf.lowest_nonlinear_perf; |
147 | lowest_perf = cppc_perf.lowest_perf; |
148 | } else { |
149 | ret = rdmsrl_safe_on_cpu(cpu, MSR_AMD_CPPC_CAP1, q: &cap1); |
150 | if (ret) { |
151 | amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL; |
152 | pr_err("%s read CPPC_CAP1 ret=%d error!\n" , __func__, ret); |
153 | goto skip_test; |
154 | } |
155 | |
156 | highest_perf = AMD_CPPC_HIGHEST_PERF(cap1); |
157 | nominal_perf = AMD_CPPC_NOMINAL_PERF(cap1); |
158 | lowest_nonlinear_perf = AMD_CPPC_LOWNONLIN_PERF(cap1); |
159 | lowest_perf = AMD_CPPC_LOWEST_PERF(cap1); |
160 | } |
161 | |
162 | if ((highest_perf != READ_ONCE(cpudata->highest_perf)) || |
163 | (nominal_perf != READ_ONCE(cpudata->nominal_perf)) || |
164 | (lowest_nonlinear_perf != READ_ONCE(cpudata->lowest_nonlinear_perf)) || |
165 | (lowest_perf != READ_ONCE(cpudata->lowest_perf))) { |
166 | amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL; |
167 | pr_err("%s cpu%d highest=%d %d nominal=%d %d lowest_nonlinear=%d %d lowest=%d %d, they should be equal!\n" , |
168 | __func__, cpu, highest_perf, cpudata->highest_perf, |
169 | nominal_perf, cpudata->nominal_perf, |
170 | lowest_nonlinear_perf, cpudata->lowest_nonlinear_perf, |
171 | lowest_perf, cpudata->lowest_perf); |
172 | goto skip_test; |
173 | } |
174 | |
175 | if (!((highest_perf >= nominal_perf) && |
176 | (nominal_perf > lowest_nonlinear_perf) && |
177 | (lowest_nonlinear_perf > lowest_perf) && |
178 | (lowest_perf > 0))) { |
179 | amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL; |
180 | pr_err("%s cpu%d highest=%d >= nominal=%d > lowest_nonlinear=%d > lowest=%d > 0, the formula is incorrect!\n" , |
181 | __func__, cpu, highest_perf, nominal_perf, |
182 | lowest_nonlinear_perf, lowest_perf); |
183 | goto skip_test; |
184 | } |
185 | cpufreq_cpu_put(policy); |
186 | } |
187 | |
188 | amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_PASS; |
189 | return; |
190 | skip_test: |
191 | cpufreq_cpu_put(policy); |
192 | } |
193 | |
194 | /* |
195 | * Check if frequency values are reasonable. |
196 | * max_freq >= nominal_freq > lowest_nonlinear_freq > min_freq > 0 |
197 | * check max freq when set support boost mode. |
198 | */ |
199 | static void amd_pstate_ut_check_freq(u32 index) |
200 | { |
201 | int cpu = 0; |
202 | struct cpufreq_policy *policy = NULL; |
203 | struct amd_cpudata *cpudata = NULL; |
204 | |
205 | for_each_possible_cpu(cpu) { |
206 | policy = cpufreq_cpu_get(cpu); |
207 | if (!policy) |
208 | break; |
209 | cpudata = policy->driver_data; |
210 | |
211 | if (!((cpudata->max_freq >= cpudata->nominal_freq) && |
212 | (cpudata->nominal_freq > cpudata->lowest_nonlinear_freq) && |
213 | (cpudata->lowest_nonlinear_freq > cpudata->min_freq) && |
214 | (cpudata->min_freq > 0))) { |
215 | amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL; |
216 | pr_err("%s cpu%d max=%d >= nominal=%d > lowest_nonlinear=%d > min=%d > 0, the formula is incorrect!\n" , |
217 | __func__, cpu, cpudata->max_freq, cpudata->nominal_freq, |
218 | cpudata->lowest_nonlinear_freq, cpudata->min_freq); |
219 | goto skip_test; |
220 | } |
221 | |
222 | if (cpudata->min_freq != policy->min) { |
223 | amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL; |
224 | pr_err("%s cpu%d cpudata_min_freq=%d policy_min=%d, they should be equal!\n" , |
225 | __func__, cpu, cpudata->min_freq, policy->min); |
226 | goto skip_test; |
227 | } |
228 | |
229 | if (cpudata->boost_supported) { |
230 | if ((policy->max == cpudata->max_freq) || |
231 | (policy->max == cpudata->nominal_freq)) |
232 | amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_PASS; |
233 | else { |
234 | amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL; |
235 | pr_err("%s cpu%d policy_max=%d should be equal cpu_max=%d or cpu_nominal=%d !\n" , |
236 | __func__, cpu, policy->max, cpudata->max_freq, |
237 | cpudata->nominal_freq); |
238 | goto skip_test; |
239 | } |
240 | } else { |
241 | amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_FAIL; |
242 | pr_err("%s cpu%d must support boost!\n" , __func__, cpu); |
243 | goto skip_test; |
244 | } |
245 | cpufreq_cpu_put(policy); |
246 | } |
247 | |
248 | amd_pstate_ut_cases[index].result = AMD_PSTATE_UT_RESULT_PASS; |
249 | return; |
250 | skip_test: |
251 | cpufreq_cpu_put(policy); |
252 | } |
253 | |
254 | static int __init amd_pstate_ut_init(void) |
255 | { |
256 | u32 i = 0, arr_size = ARRAY_SIZE(amd_pstate_ut_cases); |
257 | |
258 | for (i = 0; i < arr_size; i++) { |
259 | amd_pstate_ut_cases[i].func(i); |
260 | switch (amd_pstate_ut_cases[i].result) { |
261 | case AMD_PSTATE_UT_RESULT_PASS: |
262 | pr_info("%-4d %-20s\t success!\n" , i+1, amd_pstate_ut_cases[i].name); |
263 | break; |
264 | case AMD_PSTATE_UT_RESULT_FAIL: |
265 | default: |
266 | pr_info("%-4d %-20s\t fail!\n" , i+1, amd_pstate_ut_cases[i].name); |
267 | break; |
268 | } |
269 | } |
270 | |
271 | return 0; |
272 | } |
273 | |
274 | static void __exit amd_pstate_ut_exit(void) |
275 | { |
276 | } |
277 | |
278 | module_init(amd_pstate_ut_init); |
279 | module_exit(amd_pstate_ut_exit); |
280 | |
281 | MODULE_AUTHOR("Meng Li <li.meng@amd.com>" ); |
282 | MODULE_DESCRIPTION("AMD P-state driver Test module" ); |
283 | MODULE_LICENSE("GPL" ); |
284 | |