3232
3333#include "amd-pstate.h"
3434
35- /*
36- * Abbreviations:
37- * amd_pstate_ut: used as a shortform for AMD P-State unit test.
38- * It helps to keep variable names smaller, simpler
39- */
40- enum amd_pstate_ut_result {
41- AMD_PSTATE_UT_RESULT_PASS ,
42- AMD_PSTATE_UT_RESULT_FAIL ,
43- };
4435
4536struct amd_pstate_ut_struct {
4637 const char * name ;
47- void (* func )(u32 index );
48- enum amd_pstate_ut_result result ;
38+ int (* func )(u32 index );
4939};
5040
5141/*
5242 * Kernel module for testing the AMD P-State unit test
5343 */
54- static void amd_pstate_ut_acpi_cpc_valid (u32 index );
55- static void amd_pstate_ut_check_enabled (u32 index );
56- static void amd_pstate_ut_check_perf (u32 index );
57- static void amd_pstate_ut_check_freq (u32 index );
58- static void amd_pstate_ut_check_driver (u32 index );
44+ static int amd_pstate_ut_acpi_cpc_valid (u32 index );
45+ static int amd_pstate_ut_check_enabled (u32 index );
46+ static int amd_pstate_ut_check_perf (u32 index );
47+ static int amd_pstate_ut_check_freq (u32 index );
48+ static int amd_pstate_ut_check_driver (u32 index );
5949
6050static struct amd_pstate_ut_struct amd_pstate_ut_cases [] = {
6151 {"amd_pstate_ut_acpi_cpc_valid" , amd_pstate_ut_acpi_cpc_valid },
@@ -78,51 +68,46 @@ static bool get_shared_mem(void)
7868/*
7969 * check the _CPC object is present in SBIOS.
8070 */
81- static void amd_pstate_ut_acpi_cpc_valid (u32 index )
71+ static int amd_pstate_ut_acpi_cpc_valid (u32 index )
8272{
83- if (acpi_cpc_valid ())
84- amd_pstate_ut_cases [index ].result = AMD_PSTATE_UT_RESULT_PASS ;
85- else {
86- amd_pstate_ut_cases [index ].result = AMD_PSTATE_UT_RESULT_FAIL ;
73+ if (!acpi_cpc_valid ()) {
8774 pr_err ("%s the _CPC object is not present in SBIOS!\n" , __func__ );
75+ return - EINVAL ;
8876 }
77+
78+ return 0 ;
8979}
9080
91- static void amd_pstate_ut_pstate_enable (u32 index )
81+ /*
82+ * check if amd pstate is enabled
83+ */
84+ static int amd_pstate_ut_check_enabled (u32 index )
9285{
93- int ret = 0 ;
9486 u64 cppc_enable = 0 ;
87+ int ret ;
88+
89+ if (get_shared_mem ())
90+ return 0 ;
9591
9692 ret = rdmsrl_safe (MSR_AMD_CPPC_ENABLE , & cppc_enable );
9793 if (ret ) {
98- amd_pstate_ut_cases [index ].result = AMD_PSTATE_UT_RESULT_FAIL ;
9994 pr_err ("%s rdmsrl_safe MSR_AMD_CPPC_ENABLE ret=%d error!\n" , __func__ , ret );
100- return ;
95+ return ret ;
10196 }
102- if (cppc_enable )
103- amd_pstate_ut_cases [index ].result = AMD_PSTATE_UT_RESULT_PASS ;
104- else {
105- amd_pstate_ut_cases [index ].result = AMD_PSTATE_UT_RESULT_FAIL ;
97+
98+ if (!cppc_enable ) {
10699 pr_err ("%s amd pstate must be enabled!\n" , __func__ );
100+ return - EINVAL ;
107101 }
108- }
109102
110- /*
111- * check if amd pstate is enabled
112- */
113- static void amd_pstate_ut_check_enabled (u32 index )
114- {
115- if (get_shared_mem ())
116- amd_pstate_ut_cases [index ].result = AMD_PSTATE_UT_RESULT_PASS ;
117- else
118- amd_pstate_ut_pstate_enable (index );
103+ return 0 ;
119104}
120105
121106/*
122107 * check if performance values are reasonable.
123108 * highest_perf >= nominal_perf > lowest_nonlinear_perf > lowest_perf > 0
124109 */
125- static void amd_pstate_ut_check_perf (u32 index )
110+ static int amd_pstate_ut_check_perf (u32 index )
126111{
127112 int cpu = 0 , ret = 0 ;
128113 u32 highest_perf = 0 , nominal_perf = 0 , lowest_nonlinear_perf = 0 , lowest_perf = 0 ;
@@ -142,9 +127,8 @@ static void amd_pstate_ut_check_perf(u32 index)
142127 if (get_shared_mem ()) {
143128 ret = cppc_get_perf_caps (cpu , & cppc_perf );
144129 if (ret ) {
145- amd_pstate_ut_cases [index ].result = AMD_PSTATE_UT_RESULT_FAIL ;
146130 pr_err ("%s cppc_get_perf_caps ret=%d error!\n" , __func__ , ret );
147- return ;
131+ return ret ;
148132 }
149133
150134 highest_perf = cppc_perf .highest_perf ;
@@ -154,9 +138,8 @@ static void amd_pstate_ut_check_perf(u32 index)
154138 } else {
155139 ret = rdmsrl_safe_on_cpu (cpu , MSR_AMD_CPPC_CAP1 , & cap1 );
156140 if (ret ) {
157- amd_pstate_ut_cases [index ].result = AMD_PSTATE_UT_RESULT_FAIL ;
158141 pr_err ("%s read CPPC_CAP1 ret=%d error!\n" , __func__ , ret );
159- return ;
142+ return ret ;
160143 }
161144
162145 highest_perf = AMD_CPPC_HIGHEST_PERF (cap1 );
@@ -169,40 +152,38 @@ static void amd_pstate_ut_check_perf(u32 index)
169152 if (highest_perf != cur_perf .highest_perf && !cpudata -> hw_prefcore ) {
170153 pr_err ("%s cpu%d highest=%d %d highest perf doesn't match\n" ,
171154 __func__ , cpu , highest_perf , cur_perf .highest_perf );
172- return ;
155+ return - EINVAL ;
173156 }
174157 if (nominal_perf != cur_perf .nominal_perf ||
175158 (lowest_nonlinear_perf != cur_perf .lowest_nonlinear_perf ) ||
176159 (lowest_perf != cur_perf .lowest_perf )) {
177- amd_pstate_ut_cases [index ].result = AMD_PSTATE_UT_RESULT_FAIL ;
178160 pr_err ("%s cpu%d nominal=%d %d lowest_nonlinear=%d %d lowest=%d %d, they should be equal!\n" ,
179161 __func__ , cpu , nominal_perf , cur_perf .nominal_perf ,
180162 lowest_nonlinear_perf , cur_perf .lowest_nonlinear_perf ,
181163 lowest_perf , cur_perf .lowest_perf );
182- return ;
164+ return - EINVAL ;
183165 }
184166
185167 if (!((highest_perf >= nominal_perf ) &&
186168 (nominal_perf > lowest_nonlinear_perf ) &&
187169 (lowest_nonlinear_perf >= lowest_perf ) &&
188170 (lowest_perf > 0 ))) {
189- amd_pstate_ut_cases [index ].result = AMD_PSTATE_UT_RESULT_FAIL ;
190171 pr_err ("%s cpu%d highest=%d >= nominal=%d > lowest_nonlinear=%d > lowest=%d > 0, the formula is incorrect!\n" ,
191172 __func__ , cpu , highest_perf , nominal_perf ,
192173 lowest_nonlinear_perf , lowest_perf );
193- return ;
174+ return - EINVAL ;
194175 }
195176 }
196177
197- amd_pstate_ut_cases [ index ]. result = AMD_PSTATE_UT_RESULT_PASS ;
178+ return 0 ;
198179}
199180
200181/*
201182 * Check if frequency values are reasonable.
202183 * max_freq >= nominal_freq > lowest_nonlinear_freq > min_freq > 0
203184 * check max freq when set support boost mode.
204185 */
205- static void amd_pstate_ut_check_freq (u32 index )
186+ static int amd_pstate_ut_check_freq (u32 index )
206187{
207188 int cpu = 0 ;
208189 struct amd_cpudata * cpudata = NULL ;
@@ -219,39 +200,33 @@ static void amd_pstate_ut_check_freq(u32 index)
219200 (cpudata -> nominal_freq > cpudata -> lowest_nonlinear_freq ) &&
220201 (cpudata -> lowest_nonlinear_freq >= policy -> cpuinfo .min_freq ) &&
221202 (policy -> cpuinfo .min_freq > 0 ))) {
222- amd_pstate_ut_cases [index ].result = AMD_PSTATE_UT_RESULT_FAIL ;
223203 pr_err ("%s cpu%d max=%d >= nominal=%d > lowest_nonlinear=%d > min=%d > 0, the formula is incorrect!\n" ,
224204 __func__ , cpu , policy -> cpuinfo .max_freq , cpudata -> nominal_freq ,
225205 cpudata -> lowest_nonlinear_freq , policy -> cpuinfo .min_freq );
226- return ;
206+ return - EINVAL ;
227207 }
228208
229209 if (cpudata -> lowest_nonlinear_freq != policy -> min ) {
230- amd_pstate_ut_cases [index ].result = AMD_PSTATE_UT_RESULT_FAIL ;
231210 pr_err ("%s cpu%d cpudata_lowest_nonlinear_freq=%d policy_min=%d, they should be equal!\n" ,
232211 __func__ , cpu , cpudata -> lowest_nonlinear_freq , policy -> min );
233- return ;
212+ return - EINVAL ;
234213 }
235214
236215 if (cpudata -> boost_supported ) {
237- if ((policy -> max == policy -> cpuinfo .max_freq ) ||
238- (policy -> max == cpudata -> nominal_freq ))
239- amd_pstate_ut_cases [index ].result = AMD_PSTATE_UT_RESULT_PASS ;
240- else {
241- amd_pstate_ut_cases [index ].result = AMD_PSTATE_UT_RESULT_FAIL ;
216+ if ((policy -> max != policy -> cpuinfo .max_freq ) &&
217+ (policy -> max != cpudata -> nominal_freq )) {
242218 pr_err ("%s cpu%d policy_max=%d should be equal cpu_max=%d or cpu_nominal=%d !\n" ,
243219 __func__ , cpu , policy -> max , policy -> cpuinfo .max_freq ,
244220 cpudata -> nominal_freq );
245- return ;
221+ return - EINVAL ;
246222 }
247223 } else {
248- amd_pstate_ut_cases [index ].result = AMD_PSTATE_UT_RESULT_FAIL ;
249224 pr_err ("%s cpu%d must support boost!\n" , __func__ , cpu );
250- return ;
225+ return - EINVAL ;
251226 }
252227 }
253228
254- amd_pstate_ut_cases [ index ]. result = AMD_PSTATE_UT_RESULT_PASS ;
229+ return 0 ;
255230}
256231
257232static int amd_pstate_set_mode (enum amd_pstate_mode mode )
@@ -263,49 +238,41 @@ static int amd_pstate_set_mode(enum amd_pstate_mode mode)
263238 return amd_pstate_update_status (mode_str , strlen (mode_str ));
264239}
265240
266- static void amd_pstate_ut_check_driver (u32 index )
241+ static int amd_pstate_ut_check_driver (u32 index )
267242{
268243 enum amd_pstate_mode mode1 , mode2 = AMD_PSTATE_DISABLE ;
269- int ret ;
270244
271245 for (mode1 = AMD_PSTATE_DISABLE ; mode1 < AMD_PSTATE_MAX ; mode1 ++ ) {
272- ret = amd_pstate_set_mode (mode1 );
246+ int ret = amd_pstate_set_mode (mode1 );
273247 if (ret )
274- goto out ;
248+ return ret ;
275249 for (mode2 = AMD_PSTATE_DISABLE ; mode2 < AMD_PSTATE_MAX ; mode2 ++ ) {
276250 if (mode1 == mode2 )
277251 continue ;
278252 ret = amd_pstate_set_mode (mode2 );
279- if (ret )
280- goto out ;
253+ if (ret ) {
254+ pr_err ("%s: failed to update status for %s->%s\n" , __func__ ,
255+ amd_pstate_get_mode_string (mode1 ),
256+ amd_pstate_get_mode_string (mode2 ));
257+ return ret ;
258+ }
281259 }
282260 }
283- out :
284- if (ret )
285- pr_warn ("%s: failed to update status for %s->%s: %d\n" , __func__ ,
286- amd_pstate_get_mode_string (mode1 ),
287- amd_pstate_get_mode_string (mode2 ), ret );
288-
289- amd_pstate_ut_cases [index ].result = ret ?
290- AMD_PSTATE_UT_RESULT_FAIL :
291- AMD_PSTATE_UT_RESULT_PASS ;
261+
262+ return 0 ;
292263}
293264
294265static int __init amd_pstate_ut_init (void )
295266{
296267 u32 i = 0 , arr_size = ARRAY_SIZE (amd_pstate_ut_cases );
297268
298269 for (i = 0 ; i < arr_size ; i ++ ) {
299- amd_pstate_ut_cases [i ].func (i );
300- switch (amd_pstate_ut_cases [i ].result ) {
301- case AMD_PSTATE_UT_RESULT_PASS :
270+ int ret = amd_pstate_ut_cases [i ].func (i );
271+
272+ if (ret )
273+ pr_err ("%-4d %-20s\t fail: %d!\n" , i + 1 , amd_pstate_ut_cases [i ].name , ret );
274+ else
302275 pr_info ("%-4d %-20s\t success!\n" , i + 1 , amd_pstate_ut_cases [i ].name );
303- break ;
304- case AMD_PSTATE_UT_RESULT_FAIL :
305- default :
306- pr_info ("%-4d %-20s\t fail!\n" , i + 1 , amd_pstate_ut_cases [i ].name );
307- break ;
308- }
309276 }
310277
311278 return 0 ;
0 commit comments