1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * amd-pstate.c - AMD Processor P-state Frequency Driver
4 *
5 * Copyright (C) 2021 Advanced Micro Devices, Inc. All Rights Reserved.
6 *
7 * Author: Huang Rui <ray.huang@amd.com>
8 *
9 * AMD P-State introduces a new CPU performance scaling design for AMD
10 * processors using the ACPI Collaborative Performance and Power Control (CPPC)
11 * feature which works with the AMD SMU firmware providing a finer grained
12 * frequency control range. It is to replace the legacy ACPI P-States control,
13 * allows a flexible, low-latency interface for the Linux kernel to directly
14 * communicate the performance hints to hardware.
15 *
16 * AMD P-State is supported on recent AMD Zen base CPU series include some of
17 * Zen2 and Zen3 processors. _CPC needs to be present in the ACPI tables of AMD
18 * P-State supported system. And there are two types of hardware implementations
19 * for AMD P-State: 1) Full MSR Solution and 2) Shared Memory Solution.
20 * X86_FEATURE_CPPC CPU feature flag is used to distinguish the different types.
21 */
22
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/smp.h>
29 #include <linux/sched.h>
30 #include <linux/cpufreq.h>
31 #include <linux/compiler.h>
32 #include <linux/dmi.h>
33 #include <linux/slab.h>
34 #include <linux/acpi.h>
35 #include <linux/io.h>
36 #include <linux/delay.h>
37 #include <linux/uaccess.h>
38 #include <linux/static_call.h>
39 #include <linux/topology.h>
40
41 #include <acpi/processor.h>
42 #include <acpi/cppc_acpi.h>
43
44 #include <asm/msr.h>
45 #include <asm/processor.h>
46 #include <asm/cpufeature.h>
47 #include <asm/cpu_device_id.h>
48
49 #include "amd-pstate.h"
50 #include "amd-pstate-trace.h"
51
52 #define AMD_PSTATE_TRANSITION_LATENCY 20000
53 #define AMD_PSTATE_TRANSITION_DELAY 1000
54 #define AMD_PSTATE_FAST_CPPC_TRANSITION_DELAY 600
55
56 #define AMD_CPPC_EPP_PERFORMANCE 0x00
57 #define AMD_CPPC_EPP_BALANCE_PERFORMANCE 0x80
58 #define AMD_CPPC_EPP_BALANCE_POWERSAVE 0xBF
59 #define AMD_CPPC_EPP_POWERSAVE 0xFF
60
61 static const char * const amd_pstate_mode_string[] = {
62 [AMD_PSTATE_UNDEFINED] = "undefined",
63 [AMD_PSTATE_DISABLE] = "disable",
64 [AMD_PSTATE_PASSIVE] = "passive",
65 [AMD_PSTATE_ACTIVE] = "active",
66 [AMD_PSTATE_GUIDED] = "guided",
67 NULL,
68 };
69
amd_pstate_get_mode_string(enum amd_pstate_mode mode)70 const char *amd_pstate_get_mode_string(enum amd_pstate_mode mode)
71 {
72 if (mode < 0 || mode >= AMD_PSTATE_MAX)
73 return NULL;
74 return amd_pstate_mode_string[mode];
75 }
76 EXPORT_SYMBOL_GPL(amd_pstate_get_mode_string);
77
78 struct quirk_entry {
79 u32 nominal_freq;
80 u32 lowest_freq;
81 };
82
83 static struct cpufreq_driver *current_pstate_driver;
84 static struct cpufreq_driver amd_pstate_driver;
85 static struct cpufreq_driver amd_pstate_epp_driver;
86 static int cppc_state = AMD_PSTATE_UNDEFINED;
87 static bool cppc_enabled;
88 static bool amd_pstate_prefcore = true;
89 static struct quirk_entry *quirks;
90
91 /*
92 * AMD Energy Preference Performance (EPP)
93 * The EPP is used in the CCLK DPM controller to drive
94 * the frequency that a core is going to operate during
95 * short periods of activity. EPP values will be utilized for
96 * different OS profiles (balanced, performance, power savings)
97 * display strings corresponding to EPP index in the
98 * energy_perf_strings[]
99 * index String
100 *-------------------------------------
101 * 0 default
102 * 1 performance
103 * 2 balance_performance
104 * 3 balance_power
105 * 4 power
106 */
107 enum energy_perf_value_index {
108 EPP_INDEX_DEFAULT = 0,
109 EPP_INDEX_PERFORMANCE,
110 EPP_INDEX_BALANCE_PERFORMANCE,
111 EPP_INDEX_BALANCE_POWERSAVE,
112 EPP_INDEX_POWERSAVE,
113 };
114
115 static const char * const energy_perf_strings[] = {
116 [EPP_INDEX_DEFAULT] = "default",
117 [EPP_INDEX_PERFORMANCE] = "performance",
118 [EPP_INDEX_BALANCE_PERFORMANCE] = "balance_performance",
119 [EPP_INDEX_BALANCE_POWERSAVE] = "balance_power",
120 [EPP_INDEX_POWERSAVE] = "power",
121 NULL
122 };
123
124 static unsigned int epp_values[] = {
125 [EPP_INDEX_DEFAULT] = 0,
126 [EPP_INDEX_PERFORMANCE] = AMD_CPPC_EPP_PERFORMANCE,
127 [EPP_INDEX_BALANCE_PERFORMANCE] = AMD_CPPC_EPP_BALANCE_PERFORMANCE,
128 [EPP_INDEX_BALANCE_POWERSAVE] = AMD_CPPC_EPP_BALANCE_POWERSAVE,
129 [EPP_INDEX_POWERSAVE] = AMD_CPPC_EPP_POWERSAVE,
130 };
131
132 typedef int (*cppc_mode_transition_fn)(int);
133
134 static struct quirk_entry quirk_amd_7k62 = {
135 .nominal_freq = 2600,
136 .lowest_freq = 550,
137 };
138
dmi_matched_7k62_bios_bug(const struct dmi_system_id * dmi)139 static int __init dmi_matched_7k62_bios_bug(const struct dmi_system_id *dmi)
140 {
141 /**
142 * match the broken bios for family 17h processor support CPPC V2
143 * broken BIOS lack of nominal_freq and lowest_freq capabilities
144 * definition in ACPI tables
145 */
146 if (cpu_feature_enabled(X86_FEATURE_ZEN2)) {
147 quirks = dmi->driver_data;
148 pr_info("Overriding nominal and lowest frequencies for %s\n", dmi->ident);
149 return 1;
150 }
151
152 return 0;
153 }
154
155 static const struct dmi_system_id amd_pstate_quirks_table[] __initconst = {
156 {
157 .callback = dmi_matched_7k62_bios_bug,
158 .ident = "AMD EPYC 7K62",
159 .matches = {
160 DMI_MATCH(DMI_BIOS_VERSION, "5.14"),
161 DMI_MATCH(DMI_BIOS_RELEASE, "12/12/2019"),
162 },
163 .driver_data = &quirk_amd_7k62,
164 },
165 {}
166 };
167 MODULE_DEVICE_TABLE(dmi, amd_pstate_quirks_table);
168
get_mode_idx_from_str(const char * str,size_t size)169 static inline int get_mode_idx_from_str(const char *str, size_t size)
170 {
171 int i;
172
173 for (i=0; i < AMD_PSTATE_MAX; i++) {
174 if (!strncmp(str, amd_pstate_mode_string[i], size))
175 return i;
176 }
177 return -EINVAL;
178 }
179
180 static DEFINE_MUTEX(amd_pstate_limits_lock);
181 static DEFINE_MUTEX(amd_pstate_driver_lock);
182
amd_pstate_get_epp(struct amd_cpudata * cpudata,u64 cppc_req_cached)183 static s16 amd_pstate_get_epp(struct amd_cpudata *cpudata, u64 cppc_req_cached)
184 {
185 u64 epp;
186 int ret;
187
188 if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
189 if (!cppc_req_cached) {
190 epp = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,
191 &cppc_req_cached);
192 if (epp)
193 return epp;
194 }
195 epp = (cppc_req_cached >> 24) & 0xFF;
196 } else {
197 ret = cppc_get_epp_perf(cpudata->cpu, &epp);
198 if (ret < 0) {
199 pr_debug("Could not retrieve energy perf value (%d)\n", ret);
200 return -EIO;
201 }
202 }
203
204 return (s16)(epp & 0xff);
205 }
206
amd_pstate_get_energy_pref_index(struct amd_cpudata * cpudata)207 static int amd_pstate_get_energy_pref_index(struct amd_cpudata *cpudata)
208 {
209 s16 epp;
210 int index = -EINVAL;
211
212 epp = amd_pstate_get_epp(cpudata, 0);
213 if (epp < 0)
214 return epp;
215
216 switch (epp) {
217 case AMD_CPPC_EPP_PERFORMANCE:
218 index = EPP_INDEX_PERFORMANCE;
219 break;
220 case AMD_CPPC_EPP_BALANCE_PERFORMANCE:
221 index = EPP_INDEX_BALANCE_PERFORMANCE;
222 break;
223 case AMD_CPPC_EPP_BALANCE_POWERSAVE:
224 index = EPP_INDEX_BALANCE_POWERSAVE;
225 break;
226 case AMD_CPPC_EPP_POWERSAVE:
227 index = EPP_INDEX_POWERSAVE;
228 break;
229 default:
230 break;
231 }
232
233 return index;
234 }
235
pstate_update_perf(struct amd_cpudata * cpudata,u32 min_perf,u32 des_perf,u32 max_perf,bool fast_switch)236 static void pstate_update_perf(struct amd_cpudata *cpudata, u32 min_perf,
237 u32 des_perf, u32 max_perf, bool fast_switch)
238 {
239 if (fast_switch)
240 wrmsrl(MSR_AMD_CPPC_REQ, READ_ONCE(cpudata->cppc_req_cached));
241 else
242 wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,
243 READ_ONCE(cpudata->cppc_req_cached));
244 }
245
246 DEFINE_STATIC_CALL(amd_pstate_update_perf, pstate_update_perf);
247
amd_pstate_update_perf(struct amd_cpudata * cpudata,u32 min_perf,u32 des_perf,u32 max_perf,bool fast_switch)248 static inline void amd_pstate_update_perf(struct amd_cpudata *cpudata,
249 u32 min_perf, u32 des_perf,
250 u32 max_perf, bool fast_switch)
251 {
252 static_call(amd_pstate_update_perf)(cpudata, min_perf, des_perf,
253 max_perf, fast_switch);
254 }
255
amd_pstate_set_epp(struct amd_cpudata * cpudata,u32 epp)256 static int amd_pstate_set_epp(struct amd_cpudata *cpudata, u32 epp)
257 {
258 int ret;
259 struct cppc_perf_ctrls perf_ctrls;
260
261 if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
262 u64 value = READ_ONCE(cpudata->cppc_req_cached);
263
264 value &= ~GENMASK_ULL(31, 24);
265 value |= (u64)epp << 24;
266 WRITE_ONCE(cpudata->cppc_req_cached, value);
267
268 ret = wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
269 if (!ret)
270 cpudata->epp_cached = epp;
271 } else {
272 amd_pstate_update_perf(cpudata, cpudata->min_limit_perf, 0U,
273 cpudata->max_limit_perf, false);
274
275 perf_ctrls.energy_perf = epp;
276 ret = cppc_set_epp_perf(cpudata->cpu, &perf_ctrls, 1);
277 if (ret) {
278 pr_debug("failed to set energy perf value (%d)\n", ret);
279 return ret;
280 }
281 cpudata->epp_cached = epp;
282 }
283
284 return ret;
285 }
286
amd_pstate_set_energy_pref_index(struct amd_cpudata * cpudata,int pref_index)287 static int amd_pstate_set_energy_pref_index(struct amd_cpudata *cpudata,
288 int pref_index)
289 {
290 int epp = -EINVAL;
291 int ret;
292
293 if (!pref_index)
294 epp = cpudata->epp_default;
295
296 if (epp == -EINVAL)
297 epp = epp_values[pref_index];
298
299 if (epp > 0 && cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) {
300 pr_debug("EPP cannot be set under performance policy\n");
301 return -EBUSY;
302 }
303
304 ret = amd_pstate_set_epp(cpudata, epp);
305
306 return ret;
307 }
308
pstate_enable(bool enable)309 static inline int pstate_enable(bool enable)
310 {
311 int ret, cpu;
312 unsigned long logical_proc_id_mask = 0;
313
314 if (enable == cppc_enabled)
315 return 0;
316
317 for_each_present_cpu(cpu) {
318 unsigned long logical_id = topology_logical_package_id(cpu);
319
320 if (test_bit(logical_id, &logical_proc_id_mask))
321 continue;
322
323 set_bit(logical_id, &logical_proc_id_mask);
324
325 ret = wrmsrl_safe_on_cpu(cpu, MSR_AMD_CPPC_ENABLE,
326 enable);
327 if (ret)
328 return ret;
329 }
330
331 cppc_enabled = enable;
332 return 0;
333 }
334
cppc_enable(bool enable)335 static int cppc_enable(bool enable)
336 {
337 int cpu, ret = 0;
338 struct cppc_perf_ctrls perf_ctrls;
339
340 if (enable == cppc_enabled)
341 return 0;
342
343 for_each_present_cpu(cpu) {
344 ret = cppc_set_enable(cpu, enable);
345 if (ret)
346 return ret;
347
348 /* Enable autonomous mode for EPP */
349 if (cppc_state == AMD_PSTATE_ACTIVE) {
350 /* Set desired perf as zero to allow EPP firmware control */
351 perf_ctrls.desired_perf = 0;
352 ret = cppc_set_perf(cpu, &perf_ctrls);
353 if (ret)
354 return ret;
355 }
356 }
357
358 cppc_enabled = enable;
359 return ret;
360 }
361
362 DEFINE_STATIC_CALL(amd_pstate_enable, pstate_enable);
363
amd_pstate_enable(bool enable)364 static inline int amd_pstate_enable(bool enable)
365 {
366 return static_call(amd_pstate_enable)(enable);
367 }
368
pstate_init_perf(struct amd_cpudata * cpudata)369 static int pstate_init_perf(struct amd_cpudata *cpudata)
370 {
371 u64 cap1;
372
373 int ret = rdmsrl_safe_on_cpu(cpudata->cpu, MSR_AMD_CPPC_CAP1,
374 &cap1);
375 if (ret)
376 return ret;
377
378 WRITE_ONCE(cpudata->highest_perf, AMD_CPPC_HIGHEST_PERF(cap1));
379 WRITE_ONCE(cpudata->max_limit_perf, AMD_CPPC_HIGHEST_PERF(cap1));
380 WRITE_ONCE(cpudata->nominal_perf, AMD_CPPC_NOMINAL_PERF(cap1));
381 WRITE_ONCE(cpudata->lowest_nonlinear_perf, AMD_CPPC_LOWNONLIN_PERF(cap1));
382 WRITE_ONCE(cpudata->lowest_perf, AMD_CPPC_LOWEST_PERF(cap1));
383 WRITE_ONCE(cpudata->prefcore_ranking, AMD_CPPC_HIGHEST_PERF(cap1));
384 WRITE_ONCE(cpudata->min_limit_perf, AMD_CPPC_LOWEST_PERF(cap1));
385 return 0;
386 }
387
cppc_init_perf(struct amd_cpudata * cpudata)388 static int cppc_init_perf(struct amd_cpudata *cpudata)
389 {
390 struct cppc_perf_caps cppc_perf;
391
392 int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
393 if (ret)
394 return ret;
395
396 WRITE_ONCE(cpudata->highest_perf, cppc_perf.highest_perf);
397 WRITE_ONCE(cpudata->max_limit_perf, cppc_perf.highest_perf);
398 WRITE_ONCE(cpudata->nominal_perf, cppc_perf.nominal_perf);
399 WRITE_ONCE(cpudata->lowest_nonlinear_perf,
400 cppc_perf.lowest_nonlinear_perf);
401 WRITE_ONCE(cpudata->lowest_perf, cppc_perf.lowest_perf);
402 WRITE_ONCE(cpudata->prefcore_ranking, cppc_perf.highest_perf);
403 WRITE_ONCE(cpudata->min_limit_perf, cppc_perf.lowest_perf);
404
405 if (cppc_state == AMD_PSTATE_ACTIVE)
406 return 0;
407
408 ret = cppc_get_auto_sel_caps(cpudata->cpu, &cppc_perf);
409 if (ret) {
410 pr_warn("failed to get auto_sel, ret: %d\n", ret);
411 return 0;
412 }
413
414 ret = cppc_set_auto_sel(cpudata->cpu,
415 (cppc_state == AMD_PSTATE_PASSIVE) ? 0 : 1);
416
417 if (ret)
418 pr_warn("failed to set auto_sel, ret: %d\n", ret);
419
420 return ret;
421 }
422
423 DEFINE_STATIC_CALL(amd_pstate_init_perf, pstate_init_perf);
424
amd_pstate_init_perf(struct amd_cpudata * cpudata)425 static inline int amd_pstate_init_perf(struct amd_cpudata *cpudata)
426 {
427 return static_call(amd_pstate_init_perf)(cpudata);
428 }
429
cppc_update_perf(struct amd_cpudata * cpudata,u32 min_perf,u32 des_perf,u32 max_perf,bool fast_switch)430 static void cppc_update_perf(struct amd_cpudata *cpudata,
431 u32 min_perf, u32 des_perf,
432 u32 max_perf, bool fast_switch)
433 {
434 struct cppc_perf_ctrls perf_ctrls;
435
436 perf_ctrls.max_perf = max_perf;
437 perf_ctrls.min_perf = min_perf;
438 perf_ctrls.desired_perf = des_perf;
439
440 cppc_set_perf(cpudata->cpu, &perf_ctrls);
441 }
442
amd_pstate_sample(struct amd_cpudata * cpudata)443 static inline bool amd_pstate_sample(struct amd_cpudata *cpudata)
444 {
445 u64 aperf, mperf, tsc;
446 unsigned long flags;
447
448 local_irq_save(flags);
449 rdmsrl(MSR_IA32_APERF, aperf);
450 rdmsrl(MSR_IA32_MPERF, mperf);
451 tsc = rdtsc();
452
453 if (cpudata->prev.mperf == mperf || cpudata->prev.tsc == tsc) {
454 local_irq_restore(flags);
455 return false;
456 }
457
458 local_irq_restore(flags);
459
460 cpudata->cur.aperf = aperf;
461 cpudata->cur.mperf = mperf;
462 cpudata->cur.tsc = tsc;
463 cpudata->cur.aperf -= cpudata->prev.aperf;
464 cpudata->cur.mperf -= cpudata->prev.mperf;
465 cpudata->cur.tsc -= cpudata->prev.tsc;
466
467 cpudata->prev.aperf = aperf;
468 cpudata->prev.mperf = mperf;
469 cpudata->prev.tsc = tsc;
470
471 cpudata->freq = div64_u64((cpudata->cur.aperf * cpu_khz), cpudata->cur.mperf);
472
473 return true;
474 }
475
amd_pstate_update(struct amd_cpudata * cpudata,u32 min_perf,u32 des_perf,u32 max_perf,bool fast_switch,int gov_flags)476 static void amd_pstate_update(struct amd_cpudata *cpudata, u32 min_perf,
477 u32 des_perf, u32 max_perf, bool fast_switch, int gov_flags)
478 {
479 unsigned long max_freq;
480 struct cpufreq_policy *policy = cpufreq_cpu_get(cpudata->cpu);
481 u64 prev = READ_ONCE(cpudata->cppc_req_cached);
482 u32 nominal_perf = READ_ONCE(cpudata->nominal_perf);
483 u64 value = prev;
484
485 if (!policy)
486 return;
487
488 min_perf = clamp_t(unsigned long, min_perf, cpudata->min_limit_perf,
489 cpudata->max_limit_perf);
490 max_perf = clamp_t(unsigned long, max_perf, cpudata->min_limit_perf,
491 cpudata->max_limit_perf);
492 des_perf = clamp_t(unsigned long, des_perf, min_perf, max_perf);
493
494 max_freq = READ_ONCE(cpudata->max_limit_freq);
495 policy->cur = div_u64(des_perf * max_freq, max_perf);
496
497 if ((cppc_state == AMD_PSTATE_GUIDED) && (gov_flags & CPUFREQ_GOV_DYNAMIC_SWITCHING)) {
498 min_perf = des_perf;
499 des_perf = 0;
500 }
501
502 value &= ~AMD_CPPC_MIN_PERF(~0L);
503 value |= AMD_CPPC_MIN_PERF(min_perf);
504
505 value &= ~AMD_CPPC_DES_PERF(~0L);
506 value |= AMD_CPPC_DES_PERF(des_perf);
507
508 /* limit the max perf when core performance boost feature is disabled */
509 if (!cpudata->boost_supported)
510 max_perf = min_t(unsigned long, nominal_perf, max_perf);
511
512 value &= ~AMD_CPPC_MAX_PERF(~0L);
513 value |= AMD_CPPC_MAX_PERF(max_perf);
514
515 if (trace_amd_pstate_perf_enabled() && amd_pstate_sample(cpudata)) {
516 trace_amd_pstate_perf(min_perf, des_perf, max_perf, cpudata->freq,
517 cpudata->cur.mperf, cpudata->cur.aperf, cpudata->cur.tsc,
518 cpudata->cpu, (value != prev), fast_switch);
519 }
520
521 if (value == prev)
522 goto cpufreq_policy_put;
523
524 WRITE_ONCE(cpudata->cppc_req_cached, value);
525
526 amd_pstate_update_perf(cpudata, min_perf, des_perf,
527 max_perf, fast_switch);
528
529 cpufreq_policy_put:
530 cpufreq_cpu_put(policy);
531 }
532
amd_pstate_verify(struct cpufreq_policy_data * policy)533 static int amd_pstate_verify(struct cpufreq_policy_data *policy)
534 {
535 cpufreq_verify_within_cpu_limits(policy);
536
537 return 0;
538 }
539
amd_pstate_update_min_max_limit(struct cpufreq_policy * policy)540 static int amd_pstate_update_min_max_limit(struct cpufreq_policy *policy)
541 {
542 u32 max_limit_perf, min_limit_perf, lowest_perf, max_perf;
543 struct amd_cpudata *cpudata = policy->driver_data;
544
545 if (cpudata->boost_supported && !policy->boost_enabled)
546 max_perf = READ_ONCE(cpudata->nominal_perf);
547 else
548 max_perf = READ_ONCE(cpudata->highest_perf);
549
550 max_limit_perf = div_u64(policy->max * max_perf, policy->cpuinfo.max_freq);
551 min_limit_perf = div_u64(policy->min * max_perf, policy->cpuinfo.max_freq);
552
553 lowest_perf = READ_ONCE(cpudata->lowest_perf);
554 if (min_limit_perf < lowest_perf)
555 min_limit_perf = lowest_perf;
556
557 if (max_limit_perf < min_limit_perf)
558 max_limit_perf = min_limit_perf;
559
560 WRITE_ONCE(cpudata->max_limit_perf, max_limit_perf);
561 WRITE_ONCE(cpudata->min_limit_perf, min_limit_perf);
562 WRITE_ONCE(cpudata->max_limit_freq, policy->max);
563 WRITE_ONCE(cpudata->min_limit_freq, policy->min);
564
565 return 0;
566 }
567
amd_pstate_update_freq(struct cpufreq_policy * policy,unsigned int target_freq,bool fast_switch)568 static int amd_pstate_update_freq(struct cpufreq_policy *policy,
569 unsigned int target_freq, bool fast_switch)
570 {
571 struct cpufreq_freqs freqs;
572 struct amd_cpudata *cpudata = policy->driver_data;
573 unsigned long max_perf, min_perf, des_perf, cap_perf;
574
575 if (!cpudata->max_freq)
576 return -ENODEV;
577
578 if (policy->min != cpudata->min_limit_freq || policy->max != cpudata->max_limit_freq)
579 amd_pstate_update_min_max_limit(policy);
580
581 cap_perf = READ_ONCE(cpudata->highest_perf);
582 min_perf = READ_ONCE(cpudata->lowest_perf);
583 max_perf = cap_perf;
584
585 freqs.old = policy->cur;
586 freqs.new = target_freq;
587
588 des_perf = DIV_ROUND_CLOSEST(target_freq * cap_perf,
589 cpudata->max_freq);
590
591 WARN_ON(fast_switch && !policy->fast_switch_enabled);
592 /*
593 * If fast_switch is desired, then there aren't any registered
594 * transition notifiers. See comment for
595 * cpufreq_enable_fast_switch().
596 */
597 if (!fast_switch)
598 cpufreq_freq_transition_begin(policy, &freqs);
599
600 amd_pstate_update(cpudata, min_perf, des_perf,
601 max_perf, fast_switch, policy->governor->flags);
602
603 if (!fast_switch)
604 cpufreq_freq_transition_end(policy, &freqs, false);
605
606 return 0;
607 }
608
amd_pstate_target(struct cpufreq_policy * policy,unsigned int target_freq,unsigned int relation)609 static int amd_pstate_target(struct cpufreq_policy *policy,
610 unsigned int target_freq,
611 unsigned int relation)
612 {
613 return amd_pstate_update_freq(policy, target_freq, false);
614 }
615
amd_pstate_fast_switch(struct cpufreq_policy * policy,unsigned int target_freq)616 static unsigned int amd_pstate_fast_switch(struct cpufreq_policy *policy,
617 unsigned int target_freq)
618 {
619 if (!amd_pstate_update_freq(policy, target_freq, true))
620 return target_freq;
621 return policy->cur;
622 }
623
amd_pstate_adjust_perf(unsigned int cpu,unsigned long _min_perf,unsigned long target_perf,unsigned long capacity)624 static void amd_pstate_adjust_perf(unsigned int cpu,
625 unsigned long _min_perf,
626 unsigned long target_perf,
627 unsigned long capacity)
628 {
629 unsigned long max_perf, min_perf, des_perf,
630 cap_perf, lowest_nonlinear_perf;
631 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
632 struct amd_cpudata *cpudata;
633
634 if (!policy)
635 return;
636
637 cpudata = policy->driver_data;
638
639 if (policy->min != cpudata->min_limit_freq || policy->max != cpudata->max_limit_freq)
640 amd_pstate_update_min_max_limit(policy);
641
642
643 cap_perf = READ_ONCE(cpudata->highest_perf);
644 lowest_nonlinear_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
645
646 des_perf = cap_perf;
647 if (target_perf < capacity)
648 des_perf = DIV_ROUND_UP(cap_perf * target_perf, capacity);
649
650 min_perf = READ_ONCE(cpudata->lowest_perf);
651 if (_min_perf < capacity)
652 min_perf = DIV_ROUND_UP(cap_perf * _min_perf, capacity);
653
654 if (min_perf < lowest_nonlinear_perf)
655 min_perf = lowest_nonlinear_perf;
656
657 max_perf = cap_perf;
658 if (max_perf < min_perf)
659 max_perf = min_perf;
660
661 des_perf = clamp_t(unsigned long, des_perf, min_perf, max_perf);
662
663 amd_pstate_update(cpudata, min_perf, des_perf, max_perf, true,
664 policy->governor->flags);
665 cpufreq_cpu_put(policy);
666 }
667
amd_pstate_cpu_boost_update(struct cpufreq_policy * policy,bool on)668 static int amd_pstate_cpu_boost_update(struct cpufreq_policy *policy, bool on)
669 {
670 struct amd_cpudata *cpudata = policy->driver_data;
671 u32 nominal_freq, max_freq;
672 int ret = 0;
673
674 nominal_freq = READ_ONCE(cpudata->nominal_freq);
675 max_freq = READ_ONCE(cpudata->max_freq);
676
677 if (on)
678 policy->cpuinfo.max_freq = max_freq;
679 else if (policy->cpuinfo.max_freq > nominal_freq * 1000)
680 policy->cpuinfo.max_freq = nominal_freq * 1000;
681
682 policy->max = policy->cpuinfo.max_freq;
683
684 if (cppc_state == AMD_PSTATE_PASSIVE) {
685 ret = freq_qos_update_request(&cpudata->req[1], policy->cpuinfo.max_freq);
686 if (ret < 0)
687 pr_debug("Failed to update freq constraint: CPU%d\n", cpudata->cpu);
688 }
689
690 return ret < 0 ? ret : 0;
691 }
692
amd_pstate_set_boost(struct cpufreq_policy * policy,int state)693 static int amd_pstate_set_boost(struct cpufreq_policy *policy, int state)
694 {
695 struct amd_cpudata *cpudata = policy->driver_data;
696 int ret;
697
698 if (!cpudata->boost_supported) {
699 pr_err("Boost mode is not supported by this processor or SBIOS\n");
700 return -EOPNOTSUPP;
701 }
702
703 ret = amd_pstate_cpu_boost_update(policy, state);
704 WRITE_ONCE(cpudata->boost_state, !ret ? state : false);
705 policy->boost_enabled = !ret ? state : false;
706 refresh_frequency_limits(policy);
707
708 return ret;
709 }
710
amd_pstate_init_boost_support(struct amd_cpudata * cpudata)711 static int amd_pstate_init_boost_support(struct amd_cpudata *cpudata)
712 {
713 u64 boost_val;
714 int ret = -1;
715
716 /*
717 * If platform has no CPB support or disable it, initialize current driver
718 * boost_enabled state to be false, it is not an error for cpufreq core to handle.
719 */
720 if (!cpu_feature_enabled(X86_FEATURE_CPB)) {
721 pr_debug_once("Boost CPB capabilities not present in the processor\n");
722 ret = 0;
723 goto exit_err;
724 }
725
726 /* at least one CPU supports CPB, even if others fail later on to set up */
727 current_pstate_driver->boost_enabled = true;
728
729 ret = rdmsrl_on_cpu(cpudata->cpu, MSR_K7_HWCR, &boost_val);
730 if (ret) {
731 pr_err_once("failed to read initial CPU boost state!\n");
732 ret = -EIO;
733 goto exit_err;
734 }
735
736 if (!(boost_val & MSR_K7_HWCR_CPB_DIS))
737 cpudata->boost_supported = true;
738
739 return 0;
740
741 exit_err:
742 cpudata->boost_supported = false;
743 return ret;
744 }
745
amd_perf_ctl_reset(unsigned int cpu)746 static void amd_perf_ctl_reset(unsigned int cpu)
747 {
748 wrmsrl_on_cpu(cpu, MSR_AMD_PERF_CTL, 0);
749 }
750
751 /*
752 * Set amd-pstate preferred core enable can't be done directly from cpufreq callbacks
753 * due to locking, so queue the work for later.
754 */
amd_pstste_sched_prefcore_workfn(struct work_struct * work)755 static void amd_pstste_sched_prefcore_workfn(struct work_struct *work)
756 {
757 sched_set_itmt_support();
758 }
759 static DECLARE_WORK(sched_prefcore_work, amd_pstste_sched_prefcore_workfn);
760
761 #define CPPC_MAX_PERF U8_MAX
762
amd_pstate_init_prefcore(struct amd_cpudata * cpudata)763 static void amd_pstate_init_prefcore(struct amd_cpudata *cpudata)
764 {
765 /* user disabled or not detected */
766 if (!amd_pstate_prefcore)
767 return;
768
769 cpudata->hw_prefcore = true;
770
771 /*
772 * The priorities can be set regardless of whether or not
773 * sched_set_itmt_support(true) has been called and it is valid to
774 * update them at any time after it has been called.
775 */
776 sched_set_itmt_core_prio((int)READ_ONCE(cpudata->highest_perf), cpudata->cpu);
777
778 schedule_work(&sched_prefcore_work);
779 }
780
amd_pstate_update_limits(unsigned int cpu)781 static void amd_pstate_update_limits(unsigned int cpu)
782 {
783 struct cpufreq_policy *policy = NULL;
784 struct amd_cpudata *cpudata;
785 u32 prev_high = 0, cur_high = 0;
786 int ret;
787 bool highest_perf_changed = false;
788
789 if (!amd_pstate_prefcore)
790 return;
791
792 policy = cpufreq_cpu_get(cpu);
793 if (!policy)
794 return;
795
796 cpudata = policy->driver_data;
797
798 guard(mutex)(&amd_pstate_driver_lock);
799
800 ret = amd_get_highest_perf(cpu, &cur_high);
801 if (ret) {
802 cpufreq_cpu_put(policy);
803 return;
804 }
805
806 prev_high = READ_ONCE(cpudata->prefcore_ranking);
807 highest_perf_changed = (prev_high != cur_high);
808 if (highest_perf_changed) {
809 WRITE_ONCE(cpudata->prefcore_ranking, cur_high);
810
811 if (cur_high < CPPC_MAX_PERF)
812 sched_set_itmt_core_prio((int)cur_high, cpu);
813 }
814 cpufreq_cpu_put(policy);
815
816 if (!highest_perf_changed)
817 cpufreq_update_policy(cpu);
818
819 }
820
821 /*
822 * Get pstate transition delay time from ACPI tables that firmware set
823 * instead of using hardcode value directly.
824 */
amd_pstate_get_transition_delay_us(unsigned int cpu)825 static u32 amd_pstate_get_transition_delay_us(unsigned int cpu)
826 {
827 u32 transition_delay_ns;
828
829 transition_delay_ns = cppc_get_transition_latency(cpu);
830 if (transition_delay_ns == CPUFREQ_ETERNAL) {
831 if (cpu_feature_enabled(X86_FEATURE_FAST_CPPC))
832 return AMD_PSTATE_FAST_CPPC_TRANSITION_DELAY;
833 else
834 return AMD_PSTATE_TRANSITION_DELAY;
835 }
836
837 return transition_delay_ns / NSEC_PER_USEC;
838 }
839
840 /*
841 * Get pstate transition latency value from ACPI tables that firmware
842 * set instead of using hardcode value directly.
843 */
amd_pstate_get_transition_latency(unsigned int cpu)844 static u32 amd_pstate_get_transition_latency(unsigned int cpu)
845 {
846 u32 transition_latency;
847
848 transition_latency = cppc_get_transition_latency(cpu);
849 if (transition_latency == CPUFREQ_ETERNAL)
850 return AMD_PSTATE_TRANSITION_LATENCY;
851
852 return transition_latency;
853 }
854
855 /*
856 * amd_pstate_init_freq: Initialize the max_freq, min_freq,
857 * nominal_freq and lowest_nonlinear_freq for
858 * the @cpudata object.
859 *
860 * Requires: highest_perf, lowest_perf, nominal_perf and
861 * lowest_nonlinear_perf members of @cpudata to be
862 * initialized.
863 *
864 * Returns 0 on success, non-zero value on failure.
865 */
amd_pstate_init_freq(struct amd_cpudata * cpudata)866 static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
867 {
868 int ret;
869 u32 min_freq, max_freq;
870 u64 numerator;
871 u32 nominal_perf, nominal_freq;
872 u32 lowest_nonlinear_perf, lowest_nonlinear_freq;
873 u32 boost_ratio, lowest_nonlinear_ratio;
874 struct cppc_perf_caps cppc_perf;
875
876 ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
877 if (ret)
878 return ret;
879
880 if (quirks && quirks->lowest_freq)
881 min_freq = quirks->lowest_freq * 1000;
882 else
883 min_freq = cppc_perf.lowest_freq * 1000;
884
885 if (quirks && quirks->nominal_freq)
886 nominal_freq = quirks->nominal_freq ;
887 else
888 nominal_freq = cppc_perf.nominal_freq;
889
890 nominal_perf = READ_ONCE(cpudata->nominal_perf);
891
892 ret = amd_get_boost_ratio_numerator(cpudata->cpu, &numerator);
893 if (ret)
894 return ret;
895 boost_ratio = div_u64(numerator << SCHED_CAPACITY_SHIFT, nominal_perf);
896 max_freq = (nominal_freq * boost_ratio >> SCHED_CAPACITY_SHIFT) * 1000;
897
898 lowest_nonlinear_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
899 lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,
900 nominal_perf);
901 lowest_nonlinear_freq = (nominal_freq * lowest_nonlinear_ratio >> SCHED_CAPACITY_SHIFT) * 1000;
902
903 WRITE_ONCE(cpudata->min_freq, min_freq);
904 WRITE_ONCE(cpudata->lowest_nonlinear_freq, lowest_nonlinear_freq);
905 WRITE_ONCE(cpudata->nominal_freq, nominal_freq);
906 WRITE_ONCE(cpudata->max_freq, max_freq);
907
908 /**
909 * Below values need to be initialized correctly, otherwise driver will fail to load
910 * max_freq is calculated according to (nominal_freq * highest_perf)/nominal_perf
911 * lowest_nonlinear_freq is a value between [min_freq, nominal_freq]
912 * Check _CPC in ACPI table objects if any values are incorrect
913 */
914 if (min_freq <= 0 || max_freq <= 0 || nominal_freq <= 0 || min_freq > max_freq) {
915 pr_err("min_freq(%d) or max_freq(%d) or nominal_freq(%d) value is incorrect\n",
916 min_freq, max_freq, nominal_freq * 1000);
917 return -EINVAL;
918 }
919
920 if (lowest_nonlinear_freq <= min_freq || lowest_nonlinear_freq > nominal_freq * 1000) {
921 pr_err("lowest_nonlinear_freq(%d) value is out of range [min_freq(%d), nominal_freq(%d)]\n",
922 lowest_nonlinear_freq, min_freq, nominal_freq * 1000);
923 return -EINVAL;
924 }
925
926 return 0;
927 }
928
amd_pstate_cpu_init(struct cpufreq_policy * policy)929 static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
930 {
931 int min_freq, max_freq, ret;
932 struct device *dev;
933 struct amd_cpudata *cpudata;
934
935 /*
936 * Resetting PERF_CTL_MSR will put the CPU in P0 frequency,
937 * which is ideal for initialization process.
938 */
939 amd_perf_ctl_reset(policy->cpu);
940 dev = get_cpu_device(policy->cpu);
941 if (!dev)
942 return -ENODEV;
943
944 cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
945 if (!cpudata)
946 return -ENOMEM;
947
948 cpudata->cpu = policy->cpu;
949
950 ret = amd_pstate_init_perf(cpudata);
951 if (ret)
952 goto free_cpudata1;
953
954 amd_pstate_init_prefcore(cpudata);
955
956 ret = amd_pstate_init_freq(cpudata);
957 if (ret)
958 goto free_cpudata1;
959
960 ret = amd_pstate_init_boost_support(cpudata);
961 if (ret)
962 goto free_cpudata1;
963
964 min_freq = READ_ONCE(cpudata->min_freq);
965 max_freq = READ_ONCE(cpudata->max_freq);
966
967 policy->cpuinfo.transition_latency = amd_pstate_get_transition_latency(policy->cpu);
968 policy->transition_delay_us = amd_pstate_get_transition_delay_us(policy->cpu);
969
970 policy->min = min_freq;
971 policy->max = max_freq;
972
973 policy->cpuinfo.min_freq = min_freq;
974 policy->cpuinfo.max_freq = max_freq;
975
976 policy->boost_enabled = READ_ONCE(cpudata->boost_supported);
977
978 /* It will be updated by governor */
979 policy->cur = policy->cpuinfo.min_freq;
980
981 if (cpu_feature_enabled(X86_FEATURE_CPPC))
982 policy->fast_switch_possible = true;
983
984 ret = freq_qos_add_request(&policy->constraints, &cpudata->req[0],
985 FREQ_QOS_MIN, policy->cpuinfo.min_freq);
986 if (ret < 0) {
987 dev_err(dev, "Failed to add min-freq constraint (%d)\n", ret);
988 goto free_cpudata1;
989 }
990
991 ret = freq_qos_add_request(&policy->constraints, &cpudata->req[1],
992 FREQ_QOS_MAX, policy->cpuinfo.max_freq);
993 if (ret < 0) {
994 dev_err(dev, "Failed to add max-freq constraint (%d)\n", ret);
995 goto free_cpudata2;
996 }
997
998 cpudata->max_limit_freq = max_freq;
999 cpudata->min_limit_freq = min_freq;
1000
1001 policy->driver_data = cpudata;
1002
1003 if (!current_pstate_driver->adjust_perf)
1004 current_pstate_driver->adjust_perf = amd_pstate_adjust_perf;
1005
1006 return 0;
1007
1008 free_cpudata2:
1009 freq_qos_remove_request(&cpudata->req[0]);
1010 free_cpudata1:
1011 kfree(cpudata);
1012 return ret;
1013 }
1014
amd_pstate_cpu_exit(struct cpufreq_policy * policy)1015 static void amd_pstate_cpu_exit(struct cpufreq_policy *policy)
1016 {
1017 struct amd_cpudata *cpudata = policy->driver_data;
1018
1019 freq_qos_remove_request(&cpudata->req[1]);
1020 freq_qos_remove_request(&cpudata->req[0]);
1021 policy->fast_switch_possible = false;
1022 kfree(cpudata);
1023 }
1024
amd_pstate_cpu_resume(struct cpufreq_policy * policy)1025 static int amd_pstate_cpu_resume(struct cpufreq_policy *policy)
1026 {
1027 int ret;
1028
1029 ret = amd_pstate_enable(true);
1030 if (ret)
1031 pr_err("failed to enable amd-pstate during resume, return %d\n", ret);
1032
1033 return ret;
1034 }
1035
amd_pstate_cpu_suspend(struct cpufreq_policy * policy)1036 static int amd_pstate_cpu_suspend(struct cpufreq_policy *policy)
1037 {
1038 int ret;
1039
1040 ret = amd_pstate_enable(false);
1041 if (ret)
1042 pr_err("failed to disable amd-pstate during suspend, return %d\n", ret);
1043
1044 return ret;
1045 }
1046
1047 /* Sysfs attributes */
1048
1049 /*
1050 * This frequency is to indicate the maximum hardware frequency.
1051 * If boost is not active but supported, the frequency will be larger than the
1052 * one in cpuinfo.
1053 */
show_amd_pstate_max_freq(struct cpufreq_policy * policy,char * buf)1054 static ssize_t show_amd_pstate_max_freq(struct cpufreq_policy *policy,
1055 char *buf)
1056 {
1057 int max_freq;
1058 struct amd_cpudata *cpudata = policy->driver_data;
1059
1060 max_freq = READ_ONCE(cpudata->max_freq);
1061 if (max_freq < 0)
1062 return max_freq;
1063
1064 return sysfs_emit(buf, "%u\n", max_freq);
1065 }
1066
show_amd_pstate_lowest_nonlinear_freq(struct cpufreq_policy * policy,char * buf)1067 static ssize_t show_amd_pstate_lowest_nonlinear_freq(struct cpufreq_policy *policy,
1068 char *buf)
1069 {
1070 int freq;
1071 struct amd_cpudata *cpudata = policy->driver_data;
1072
1073 freq = READ_ONCE(cpudata->lowest_nonlinear_freq);
1074 if (freq < 0)
1075 return freq;
1076
1077 return sysfs_emit(buf, "%u\n", freq);
1078 }
1079
1080 /*
1081 * In some of ASICs, the highest_perf is not the one in the _CPC table, so we
1082 * need to expose it to sysfs.
1083 */
show_amd_pstate_highest_perf(struct cpufreq_policy * policy,char * buf)1084 static ssize_t show_amd_pstate_highest_perf(struct cpufreq_policy *policy,
1085 char *buf)
1086 {
1087 u32 perf;
1088 struct amd_cpudata *cpudata = policy->driver_data;
1089
1090 perf = READ_ONCE(cpudata->highest_perf);
1091
1092 return sysfs_emit(buf, "%u\n", perf);
1093 }
1094
show_amd_pstate_prefcore_ranking(struct cpufreq_policy * policy,char * buf)1095 static ssize_t show_amd_pstate_prefcore_ranking(struct cpufreq_policy *policy,
1096 char *buf)
1097 {
1098 u32 perf;
1099 struct amd_cpudata *cpudata = policy->driver_data;
1100
1101 perf = READ_ONCE(cpudata->prefcore_ranking);
1102
1103 return sysfs_emit(buf, "%u\n", perf);
1104 }
1105
show_amd_pstate_hw_prefcore(struct cpufreq_policy * policy,char * buf)1106 static ssize_t show_amd_pstate_hw_prefcore(struct cpufreq_policy *policy,
1107 char *buf)
1108 {
1109 bool hw_prefcore;
1110 struct amd_cpudata *cpudata = policy->driver_data;
1111
1112 hw_prefcore = READ_ONCE(cpudata->hw_prefcore);
1113
1114 return sysfs_emit(buf, "%s\n", str_enabled_disabled(hw_prefcore));
1115 }
1116
show_energy_performance_available_preferences(struct cpufreq_policy * policy,char * buf)1117 static ssize_t show_energy_performance_available_preferences(
1118 struct cpufreq_policy *policy, char *buf)
1119 {
1120 int i = 0;
1121 int offset = 0;
1122 struct amd_cpudata *cpudata = policy->driver_data;
1123
1124 if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
1125 return sysfs_emit_at(buf, offset, "%s\n",
1126 energy_perf_strings[EPP_INDEX_PERFORMANCE]);
1127
1128 while (energy_perf_strings[i] != NULL)
1129 offset += sysfs_emit_at(buf, offset, "%s ", energy_perf_strings[i++]);
1130
1131 offset += sysfs_emit_at(buf, offset, "\n");
1132
1133 return offset;
1134 }
1135
store_energy_performance_preference(struct cpufreq_policy * policy,const char * buf,size_t count)1136 static ssize_t store_energy_performance_preference(
1137 struct cpufreq_policy *policy, const char *buf, size_t count)
1138 {
1139 struct amd_cpudata *cpudata = policy->driver_data;
1140 char str_preference[21];
1141 ssize_t ret;
1142
1143 ret = sscanf(buf, "%20s", str_preference);
1144 if (ret != 1)
1145 return -EINVAL;
1146
1147 ret = match_string(energy_perf_strings, -1, str_preference);
1148 if (ret < 0)
1149 return -EINVAL;
1150
1151 guard(mutex)(&amd_pstate_limits_lock);
1152
1153 ret = amd_pstate_set_energy_pref_index(cpudata, ret);
1154
1155 return ret ? ret : count;
1156 }
1157
show_energy_performance_preference(struct cpufreq_policy * policy,char * buf)1158 static ssize_t show_energy_performance_preference(
1159 struct cpufreq_policy *policy, char *buf)
1160 {
1161 struct amd_cpudata *cpudata = policy->driver_data;
1162 int preference;
1163
1164 preference = amd_pstate_get_energy_pref_index(cpudata);
1165 if (preference < 0)
1166 return preference;
1167
1168 return sysfs_emit(buf, "%s\n", energy_perf_strings[preference]);
1169 }
1170
amd_pstate_driver_cleanup(void)1171 static void amd_pstate_driver_cleanup(void)
1172 {
1173 amd_pstate_enable(false);
1174 cppc_state = AMD_PSTATE_DISABLE;
1175 current_pstate_driver = NULL;
1176 }
1177
amd_pstate_register_driver(int mode)1178 static int amd_pstate_register_driver(int mode)
1179 {
1180 int ret;
1181
1182 if (mode == AMD_PSTATE_PASSIVE || mode == AMD_PSTATE_GUIDED)
1183 current_pstate_driver = &amd_pstate_driver;
1184 else if (mode == AMD_PSTATE_ACTIVE)
1185 current_pstate_driver = &amd_pstate_epp_driver;
1186 else
1187 return -EINVAL;
1188
1189 cppc_state = mode;
1190
1191 ret = amd_pstate_enable(true);
1192 if (ret) {
1193 pr_err("failed to enable cppc during amd-pstate driver registration, return %d\n",
1194 ret);
1195 amd_pstate_driver_cleanup();
1196 return ret;
1197 }
1198
1199 ret = cpufreq_register_driver(current_pstate_driver);
1200 if (ret) {
1201 amd_pstate_driver_cleanup();
1202 return ret;
1203 }
1204
1205 return 0;
1206 }
1207
amd_pstate_unregister_driver(int dummy)1208 static int amd_pstate_unregister_driver(int dummy)
1209 {
1210 cpufreq_unregister_driver(current_pstate_driver);
1211 amd_pstate_driver_cleanup();
1212 return 0;
1213 }
1214
amd_pstate_change_mode_without_dvr_change(int mode)1215 static int amd_pstate_change_mode_without_dvr_change(int mode)
1216 {
1217 int cpu = 0;
1218
1219 cppc_state = mode;
1220
1221 if (cpu_feature_enabled(X86_FEATURE_CPPC) || cppc_state == AMD_PSTATE_ACTIVE)
1222 return 0;
1223
1224 for_each_present_cpu(cpu) {
1225 cppc_set_auto_sel(cpu, (cppc_state == AMD_PSTATE_PASSIVE) ? 0 : 1);
1226 }
1227
1228 return 0;
1229 }
1230
amd_pstate_change_driver_mode(int mode)1231 static int amd_pstate_change_driver_mode(int mode)
1232 {
1233 int ret;
1234
1235 ret = amd_pstate_unregister_driver(0);
1236 if (ret)
1237 return ret;
1238
1239 ret = amd_pstate_register_driver(mode);
1240 if (ret)
1241 return ret;
1242
1243 return 0;
1244 }
1245
1246 static cppc_mode_transition_fn mode_state_machine[AMD_PSTATE_MAX][AMD_PSTATE_MAX] = {
1247 [AMD_PSTATE_DISABLE] = {
1248 [AMD_PSTATE_DISABLE] = NULL,
1249 [AMD_PSTATE_PASSIVE] = amd_pstate_register_driver,
1250 [AMD_PSTATE_ACTIVE] = amd_pstate_register_driver,
1251 [AMD_PSTATE_GUIDED] = amd_pstate_register_driver,
1252 },
1253 [AMD_PSTATE_PASSIVE] = {
1254 [AMD_PSTATE_DISABLE] = amd_pstate_unregister_driver,
1255 [AMD_PSTATE_PASSIVE] = NULL,
1256 [AMD_PSTATE_ACTIVE] = amd_pstate_change_driver_mode,
1257 [AMD_PSTATE_GUIDED] = amd_pstate_change_mode_without_dvr_change,
1258 },
1259 [AMD_PSTATE_ACTIVE] = {
1260 [AMD_PSTATE_DISABLE] = amd_pstate_unregister_driver,
1261 [AMD_PSTATE_PASSIVE] = amd_pstate_change_driver_mode,
1262 [AMD_PSTATE_ACTIVE] = NULL,
1263 [AMD_PSTATE_GUIDED] = amd_pstate_change_driver_mode,
1264 },
1265 [AMD_PSTATE_GUIDED] = {
1266 [AMD_PSTATE_DISABLE] = amd_pstate_unregister_driver,
1267 [AMD_PSTATE_PASSIVE] = amd_pstate_change_mode_without_dvr_change,
1268 [AMD_PSTATE_ACTIVE] = amd_pstate_change_driver_mode,
1269 [AMD_PSTATE_GUIDED] = NULL,
1270 },
1271 };
1272
amd_pstate_show_status(char * buf)1273 static ssize_t amd_pstate_show_status(char *buf)
1274 {
1275 if (!current_pstate_driver)
1276 return sysfs_emit(buf, "disable\n");
1277
1278 return sysfs_emit(buf, "%s\n", amd_pstate_mode_string[cppc_state]);
1279 }
1280
amd_pstate_update_status(const char * buf,size_t size)1281 int amd_pstate_update_status(const char *buf, size_t size)
1282 {
1283 int mode_idx;
1284
1285 if (size > strlen("passive") || size < strlen("active"))
1286 return -EINVAL;
1287
1288 mode_idx = get_mode_idx_from_str(buf, size);
1289
1290 if (mode_idx < 0 || mode_idx >= AMD_PSTATE_MAX)
1291 return -EINVAL;
1292
1293 if (mode_state_machine[cppc_state][mode_idx])
1294 return mode_state_machine[cppc_state][mode_idx](mode_idx);
1295
1296 return 0;
1297 }
1298 EXPORT_SYMBOL_GPL(amd_pstate_update_status);
1299
status_show(struct device * dev,struct device_attribute * attr,char * buf)1300 static ssize_t status_show(struct device *dev,
1301 struct device_attribute *attr, char *buf)
1302 {
1303
1304 guard(mutex)(&amd_pstate_driver_lock);
1305
1306 return amd_pstate_show_status(buf);
1307 }
1308
status_store(struct device * a,struct device_attribute * b,const char * buf,size_t count)1309 static ssize_t status_store(struct device *a, struct device_attribute *b,
1310 const char *buf, size_t count)
1311 {
1312 char *p = memchr(buf, '\n', count);
1313 int ret;
1314
1315 guard(mutex)(&amd_pstate_driver_lock);
1316 ret = amd_pstate_update_status(buf, p ? p - buf : count);
1317
1318 return ret < 0 ? ret : count;
1319 }
1320
prefcore_show(struct device * dev,struct device_attribute * attr,char * buf)1321 static ssize_t prefcore_show(struct device *dev,
1322 struct device_attribute *attr, char *buf)
1323 {
1324 return sysfs_emit(buf, "%s\n", str_enabled_disabled(amd_pstate_prefcore));
1325 }
1326
1327 cpufreq_freq_attr_ro(amd_pstate_max_freq);
1328 cpufreq_freq_attr_ro(amd_pstate_lowest_nonlinear_freq);
1329
1330 cpufreq_freq_attr_ro(amd_pstate_highest_perf);
1331 cpufreq_freq_attr_ro(amd_pstate_prefcore_ranking);
1332 cpufreq_freq_attr_ro(amd_pstate_hw_prefcore);
1333 cpufreq_freq_attr_rw(energy_performance_preference);
1334 cpufreq_freq_attr_ro(energy_performance_available_preferences);
1335 static DEVICE_ATTR_RW(status);
1336 static DEVICE_ATTR_RO(prefcore);
1337
1338 static struct freq_attr *amd_pstate_attr[] = {
1339 &amd_pstate_max_freq,
1340 &amd_pstate_lowest_nonlinear_freq,
1341 &amd_pstate_highest_perf,
1342 &amd_pstate_prefcore_ranking,
1343 &amd_pstate_hw_prefcore,
1344 NULL,
1345 };
1346
1347 static struct freq_attr *amd_pstate_epp_attr[] = {
1348 &amd_pstate_max_freq,
1349 &amd_pstate_lowest_nonlinear_freq,
1350 &amd_pstate_highest_perf,
1351 &amd_pstate_prefcore_ranking,
1352 &amd_pstate_hw_prefcore,
1353 &energy_performance_preference,
1354 &energy_performance_available_preferences,
1355 NULL,
1356 };
1357
1358 static struct attribute *pstate_global_attributes[] = {
1359 &dev_attr_status.attr,
1360 &dev_attr_prefcore.attr,
1361 NULL
1362 };
1363
1364 static const struct attribute_group amd_pstate_global_attr_group = {
1365 .name = "amd_pstate",
1366 .attrs = pstate_global_attributes,
1367 };
1368
amd_pstate_acpi_pm_profile_server(void)1369 static bool amd_pstate_acpi_pm_profile_server(void)
1370 {
1371 switch (acpi_gbl_FADT.preferred_profile) {
1372 case PM_ENTERPRISE_SERVER:
1373 case PM_SOHO_SERVER:
1374 case PM_PERFORMANCE_SERVER:
1375 return true;
1376 }
1377 return false;
1378 }
1379
amd_pstate_acpi_pm_profile_undefined(void)1380 static bool amd_pstate_acpi_pm_profile_undefined(void)
1381 {
1382 if (acpi_gbl_FADT.preferred_profile == PM_UNSPECIFIED)
1383 return true;
1384 if (acpi_gbl_FADT.preferred_profile >= NR_PM_PROFILES)
1385 return true;
1386 return false;
1387 }
1388
amd_pstate_epp_cpu_init(struct cpufreq_policy * policy)1389 static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
1390 {
1391 int min_freq, max_freq, ret;
1392 struct amd_cpudata *cpudata;
1393 struct device *dev;
1394 u64 value;
1395
1396 /*
1397 * Resetting PERF_CTL_MSR will put the CPU in P0 frequency,
1398 * which is ideal for initialization process.
1399 */
1400 amd_perf_ctl_reset(policy->cpu);
1401 dev = get_cpu_device(policy->cpu);
1402 if (!dev)
1403 return -ENODEV;
1404
1405 cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
1406 if (!cpudata)
1407 return -ENOMEM;
1408
1409 cpudata->cpu = policy->cpu;
1410 cpudata->epp_policy = 0;
1411
1412 ret = amd_pstate_init_perf(cpudata);
1413 if (ret)
1414 goto free_cpudata1;
1415
1416 amd_pstate_init_prefcore(cpudata);
1417
1418 ret = amd_pstate_init_freq(cpudata);
1419 if (ret)
1420 goto free_cpudata1;
1421
1422 ret = amd_pstate_init_boost_support(cpudata);
1423 if (ret)
1424 goto free_cpudata1;
1425
1426 min_freq = READ_ONCE(cpudata->min_freq);
1427 max_freq = READ_ONCE(cpudata->max_freq);
1428
1429 policy->cpuinfo.min_freq = min_freq;
1430 policy->cpuinfo.max_freq = max_freq;
1431 /* It will be updated by governor */
1432 policy->cur = policy->cpuinfo.min_freq;
1433
1434 policy->driver_data = cpudata;
1435
1436 cpudata->epp_cached = cpudata->epp_default = amd_pstate_get_epp(cpudata, 0);
1437
1438 policy->min = policy->cpuinfo.min_freq;
1439 policy->max = policy->cpuinfo.max_freq;
1440
1441 policy->boost_enabled = READ_ONCE(cpudata->boost_supported);
1442
1443 /*
1444 * Set the policy to provide a valid fallback value in case
1445 * the default cpufreq governor is neither powersave nor performance.
1446 */
1447 if (amd_pstate_acpi_pm_profile_server() ||
1448 amd_pstate_acpi_pm_profile_undefined())
1449 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
1450 else
1451 policy->policy = CPUFREQ_POLICY_POWERSAVE;
1452
1453 if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
1454 ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, &value);
1455 if (ret)
1456 return ret;
1457 WRITE_ONCE(cpudata->cppc_req_cached, value);
1458
1459 ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_CAP1, &value);
1460 if (ret)
1461 return ret;
1462 WRITE_ONCE(cpudata->cppc_cap1_cached, value);
1463 }
1464
1465 return 0;
1466
1467 free_cpudata1:
1468 kfree(cpudata);
1469 return ret;
1470 }
1471
amd_pstate_epp_cpu_exit(struct cpufreq_policy * policy)1472 static void amd_pstate_epp_cpu_exit(struct cpufreq_policy *policy)
1473 {
1474 struct amd_cpudata *cpudata = policy->driver_data;
1475
1476 if (cpudata) {
1477 kfree(cpudata);
1478 policy->driver_data = NULL;
1479 }
1480
1481 pr_debug("CPU %d exiting\n", policy->cpu);
1482 }
1483
amd_pstate_epp_update_limit(struct cpufreq_policy * policy)1484 static int amd_pstate_epp_update_limit(struct cpufreq_policy *policy)
1485 {
1486 struct amd_cpudata *cpudata = policy->driver_data;
1487 u32 max_perf, min_perf, min_limit_perf, max_limit_perf;
1488 u64 value;
1489 s16 epp;
1490
1491 if (cpudata->boost_supported && !policy->boost_enabled)
1492 max_perf = READ_ONCE(cpudata->nominal_perf);
1493 else
1494 max_perf = READ_ONCE(cpudata->highest_perf);
1495 min_perf = READ_ONCE(cpudata->lowest_perf);
1496 max_limit_perf = div_u64(policy->max * max_perf, policy->cpuinfo.max_freq);
1497 min_limit_perf = div_u64(policy->min * max_perf, policy->cpuinfo.max_freq);
1498
1499 if (min_limit_perf < min_perf)
1500 min_limit_perf = min_perf;
1501
1502 if (max_limit_perf < min_limit_perf)
1503 max_limit_perf = min_limit_perf;
1504
1505 WRITE_ONCE(cpudata->max_limit_perf, max_limit_perf);
1506 WRITE_ONCE(cpudata->min_limit_perf, min_limit_perf);
1507
1508 max_perf = clamp_t(unsigned long, max_perf, cpudata->min_limit_perf,
1509 cpudata->max_limit_perf);
1510 min_perf = clamp_t(unsigned long, min_perf, cpudata->min_limit_perf,
1511 cpudata->max_limit_perf);
1512 value = READ_ONCE(cpudata->cppc_req_cached);
1513
1514 if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
1515 min_perf = min(cpudata->nominal_perf, max_perf);
1516
1517 /* Initial min/max values for CPPC Performance Controls Register */
1518 value &= ~AMD_CPPC_MIN_PERF(~0L);
1519 value |= AMD_CPPC_MIN_PERF(min_perf);
1520
1521 value &= ~AMD_CPPC_MAX_PERF(~0L);
1522 value |= AMD_CPPC_MAX_PERF(max_perf);
1523
1524 /* CPPC EPP feature require to set zero to the desire perf bit */
1525 value &= ~AMD_CPPC_DES_PERF(~0L);
1526 value |= AMD_CPPC_DES_PERF(0);
1527
1528 cpudata->epp_policy = cpudata->policy;
1529
1530 /* Get BIOS pre-defined epp value */
1531 epp = amd_pstate_get_epp(cpudata, value);
1532 if (epp < 0) {
1533 /**
1534 * This return value can only be negative for shared_memory
1535 * systems where EPP register read/write not supported.
1536 */
1537 return epp;
1538 }
1539
1540 if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
1541 epp = 0;
1542
1543 /* Set initial EPP value */
1544 if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
1545 value &= ~GENMASK_ULL(31, 24);
1546 value |= (u64)epp << 24;
1547 }
1548
1549 WRITE_ONCE(cpudata->cppc_req_cached, value);
1550 return amd_pstate_set_epp(cpudata, epp);
1551 }
1552
amd_pstate_epp_set_policy(struct cpufreq_policy * policy)1553 static int amd_pstate_epp_set_policy(struct cpufreq_policy *policy)
1554 {
1555 struct amd_cpudata *cpudata = policy->driver_data;
1556 int ret;
1557
1558 if (!policy->cpuinfo.max_freq)
1559 return -ENODEV;
1560
1561 pr_debug("set_policy: cpuinfo.max %u policy->max %u\n",
1562 policy->cpuinfo.max_freq, policy->max);
1563
1564 cpudata->policy = policy->policy;
1565
1566 ret = amd_pstate_epp_update_limit(policy);
1567 if (ret)
1568 return ret;
1569
1570 /*
1571 * policy->cur is never updated with the amd_pstate_epp driver, but it
1572 * is used as a stale frequency value. So, keep it within limits.
1573 */
1574 policy->cur = policy->min;
1575
1576 return 0;
1577 }
1578
amd_pstate_epp_reenable(struct amd_cpudata * cpudata)1579 static void amd_pstate_epp_reenable(struct amd_cpudata *cpudata)
1580 {
1581 u64 max_perf;
1582 int ret;
1583
1584 ret = amd_pstate_enable(true);
1585 if (ret)
1586 pr_err("failed to enable amd pstate during resume, return %d\n", ret);
1587
1588 max_perf = READ_ONCE(cpudata->highest_perf);
1589
1590 amd_pstate_update_perf(cpudata, 0, 0, max_perf, false);
1591 amd_pstate_set_epp(cpudata, cpudata->epp_cached);
1592 }
1593
amd_pstate_epp_cpu_online(struct cpufreq_policy * policy)1594 static int amd_pstate_epp_cpu_online(struct cpufreq_policy *policy)
1595 {
1596 struct amd_cpudata *cpudata = policy->driver_data;
1597
1598 pr_debug("AMD CPU Core %d going online\n", cpudata->cpu);
1599
1600 amd_pstate_epp_reenable(cpudata);
1601 cpudata->suspended = false;
1602
1603 return 0;
1604 }
1605
amd_pstate_epp_cpu_offline(struct cpufreq_policy * policy)1606 static int amd_pstate_epp_cpu_offline(struct cpufreq_policy *policy)
1607 {
1608 struct amd_cpudata *cpudata = policy->driver_data;
1609 int min_perf;
1610
1611 if (cpudata->suspended)
1612 return 0;
1613
1614 min_perf = READ_ONCE(cpudata->lowest_perf);
1615
1616 guard(mutex)(&amd_pstate_limits_lock);
1617
1618 amd_pstate_update_perf(cpudata, min_perf, 0, min_perf, false);
1619 amd_pstate_set_epp(cpudata, AMD_CPPC_EPP_BALANCE_POWERSAVE);
1620
1621 return 0;
1622 }
1623
amd_pstate_epp_verify_policy(struct cpufreq_policy_data * policy)1624 static int amd_pstate_epp_verify_policy(struct cpufreq_policy_data *policy)
1625 {
1626 cpufreq_verify_within_cpu_limits(policy);
1627 pr_debug("policy_max =%d, policy_min=%d\n", policy->max, policy->min);
1628 return 0;
1629 }
1630
amd_pstate_epp_suspend(struct cpufreq_policy * policy)1631 static int amd_pstate_epp_suspend(struct cpufreq_policy *policy)
1632 {
1633 struct amd_cpudata *cpudata = policy->driver_data;
1634 int ret;
1635
1636 /* avoid suspending when EPP is not enabled */
1637 if (cppc_state != AMD_PSTATE_ACTIVE)
1638 return 0;
1639
1640 /* set this flag to avoid setting core offline*/
1641 cpudata->suspended = true;
1642
1643 /* disable CPPC in lowlevel firmware */
1644 ret = amd_pstate_enable(false);
1645 if (ret)
1646 pr_err("failed to suspend, return %d\n", ret);
1647
1648 return 0;
1649 }
1650
amd_pstate_epp_resume(struct cpufreq_policy * policy)1651 static int amd_pstate_epp_resume(struct cpufreq_policy *policy)
1652 {
1653 struct amd_cpudata *cpudata = policy->driver_data;
1654
1655 if (cpudata->suspended) {
1656 guard(mutex)(&amd_pstate_limits_lock);
1657
1658 /* enable amd pstate from suspend state*/
1659 amd_pstate_epp_reenable(cpudata);
1660
1661 cpudata->suspended = false;
1662 }
1663
1664 return 0;
1665 }
1666
1667 static struct cpufreq_driver amd_pstate_driver = {
1668 .flags = CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_UPDATE_LIMITS,
1669 .verify = amd_pstate_verify,
1670 .target = amd_pstate_target,
1671 .fast_switch = amd_pstate_fast_switch,
1672 .init = amd_pstate_cpu_init,
1673 .exit = amd_pstate_cpu_exit,
1674 .suspend = amd_pstate_cpu_suspend,
1675 .resume = amd_pstate_cpu_resume,
1676 .set_boost = amd_pstate_set_boost,
1677 .update_limits = amd_pstate_update_limits,
1678 .name = "amd-pstate",
1679 .attr = amd_pstate_attr,
1680 };
1681
1682 static struct cpufreq_driver amd_pstate_epp_driver = {
1683 .flags = CPUFREQ_CONST_LOOPS,
1684 .verify = amd_pstate_epp_verify_policy,
1685 .setpolicy = amd_pstate_epp_set_policy,
1686 .init = amd_pstate_epp_cpu_init,
1687 .exit = amd_pstate_epp_cpu_exit,
1688 .offline = amd_pstate_epp_cpu_offline,
1689 .online = amd_pstate_epp_cpu_online,
1690 .suspend = amd_pstate_epp_suspend,
1691 .resume = amd_pstate_epp_resume,
1692 .update_limits = amd_pstate_update_limits,
1693 .set_boost = amd_pstate_set_boost,
1694 .name = "amd-pstate-epp",
1695 .attr = amd_pstate_epp_attr,
1696 };
1697
amd_pstate_set_driver(int mode_idx)1698 static int __init amd_pstate_set_driver(int mode_idx)
1699 {
1700 if (mode_idx >= AMD_PSTATE_DISABLE && mode_idx < AMD_PSTATE_MAX) {
1701 cppc_state = mode_idx;
1702 if (cppc_state == AMD_PSTATE_DISABLE)
1703 pr_info("driver is explicitly disabled\n");
1704
1705 if (cppc_state == AMD_PSTATE_ACTIVE)
1706 current_pstate_driver = &amd_pstate_epp_driver;
1707
1708 if (cppc_state == AMD_PSTATE_PASSIVE || cppc_state == AMD_PSTATE_GUIDED)
1709 current_pstate_driver = &amd_pstate_driver;
1710
1711 return 0;
1712 }
1713
1714 return -EINVAL;
1715 }
1716
1717 /**
1718 * CPPC function is not supported for family ID 17H with model_ID ranging from 0x10 to 0x2F.
1719 * show the debug message that helps to check if the CPU has CPPC support for loading issue.
1720 */
amd_cppc_supported(void)1721 static bool amd_cppc_supported(void)
1722 {
1723 struct cpuinfo_x86 *c = &cpu_data(0);
1724 bool warn = false;
1725
1726 if ((boot_cpu_data.x86 == 0x17) && (boot_cpu_data.x86_model < 0x30)) {
1727 pr_debug_once("CPPC feature is not supported by the processor\n");
1728 return false;
1729 }
1730
1731 /*
1732 * If the CPPC feature is disabled in the BIOS for processors
1733 * that support MSR-based CPPC, the AMD Pstate driver may not
1734 * function correctly.
1735 *
1736 * For such processors, check the CPPC flag and display a
1737 * warning message if the platform supports CPPC.
1738 *
1739 * Note: The code check below will not abort the driver
1740 * registration process because of the code is added for
1741 * debugging purposes. Besides, it may still be possible for
1742 * the driver to work using the shared-memory mechanism.
1743 */
1744 if (!cpu_feature_enabled(X86_FEATURE_CPPC)) {
1745 if (cpu_feature_enabled(X86_FEATURE_ZEN2)) {
1746 switch (c->x86_model) {
1747 case 0x60 ... 0x6F:
1748 case 0x80 ... 0xAF:
1749 warn = true;
1750 break;
1751 }
1752 } else if (cpu_feature_enabled(X86_FEATURE_ZEN3) ||
1753 cpu_feature_enabled(X86_FEATURE_ZEN4)) {
1754 switch (c->x86_model) {
1755 case 0x10 ... 0x1F:
1756 case 0x40 ... 0xAF:
1757 warn = true;
1758 break;
1759 }
1760 } else if (cpu_feature_enabled(X86_FEATURE_ZEN5)) {
1761 warn = true;
1762 }
1763 }
1764
1765 if (warn)
1766 pr_warn_once("The CPPC feature is supported but currently disabled by the BIOS.\n"
1767 "Please enable it if your BIOS has the CPPC option.\n");
1768 return true;
1769 }
1770
amd_pstate_init(void)1771 static int __init amd_pstate_init(void)
1772 {
1773 struct device *dev_root;
1774 int ret;
1775
1776 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
1777 return -ENODEV;
1778
1779 /* show debug message only if CPPC is not supported */
1780 if (!amd_cppc_supported())
1781 return -EOPNOTSUPP;
1782
1783 /* show warning message when BIOS broken or ACPI disabled */
1784 if (!acpi_cpc_valid()) {
1785 pr_warn_once("the _CPC object is not present in SBIOS or ACPI disabled\n");
1786 return -ENODEV;
1787 }
1788
1789 /* don't keep reloading if cpufreq_driver exists */
1790 if (cpufreq_get_current_driver())
1791 return -EEXIST;
1792
1793 quirks = NULL;
1794
1795 /* check if this machine need CPPC quirks */
1796 dmi_check_system(amd_pstate_quirks_table);
1797
1798 /*
1799 * determine the driver mode from the command line or kernel config.
1800 * If no command line input is provided, cppc_state will be AMD_PSTATE_UNDEFINED.
1801 * command line options will override the kernel config settings.
1802 */
1803
1804 if (cppc_state == AMD_PSTATE_UNDEFINED) {
1805 /* Disable on the following configs by default:
1806 * 1. Undefined platforms
1807 * 2. Server platforms
1808 */
1809 if (amd_pstate_acpi_pm_profile_undefined() ||
1810 amd_pstate_acpi_pm_profile_server()) {
1811 pr_info("driver load is disabled, boot with specific mode to enable this\n");
1812 return -ENODEV;
1813 }
1814 /* get driver mode from kernel config option [1:4] */
1815 cppc_state = CONFIG_X86_AMD_PSTATE_DEFAULT_MODE;
1816 }
1817
1818 switch (cppc_state) {
1819 case AMD_PSTATE_DISABLE:
1820 pr_info("driver load is disabled, boot with specific mode to enable this\n");
1821 return -ENODEV;
1822 case AMD_PSTATE_PASSIVE:
1823 case AMD_PSTATE_ACTIVE:
1824 case AMD_PSTATE_GUIDED:
1825 ret = amd_pstate_set_driver(cppc_state);
1826 if (ret)
1827 return ret;
1828 break;
1829 default:
1830 return -EINVAL;
1831 }
1832
1833 /* capability check */
1834 if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
1835 pr_debug("AMD CPPC MSR based functionality is supported\n");
1836 if (cppc_state != AMD_PSTATE_ACTIVE)
1837 current_pstate_driver->adjust_perf = amd_pstate_adjust_perf;
1838 } else {
1839 pr_debug("AMD CPPC shared memory based functionality is supported\n");
1840 static_call_update(amd_pstate_enable, cppc_enable);
1841 static_call_update(amd_pstate_init_perf, cppc_init_perf);
1842 static_call_update(amd_pstate_update_perf, cppc_update_perf);
1843 }
1844
1845 if (amd_pstate_prefcore) {
1846 ret = amd_detect_prefcore(&amd_pstate_prefcore);
1847 if (ret)
1848 return ret;
1849 }
1850
1851 /* enable amd pstate feature */
1852 ret = amd_pstate_enable(true);
1853 if (ret) {
1854 pr_err("failed to enable driver mode(%d)\n", cppc_state);
1855 return ret;
1856 }
1857
1858 ret = cpufreq_register_driver(current_pstate_driver);
1859 if (ret) {
1860 pr_err("failed to register with return %d\n", ret);
1861 goto disable_driver;
1862 }
1863
1864 dev_root = bus_get_dev_root(&cpu_subsys);
1865 if (dev_root) {
1866 ret = sysfs_create_group(&dev_root->kobj, &amd_pstate_global_attr_group);
1867 put_device(dev_root);
1868 if (ret) {
1869 pr_err("sysfs attribute export failed with error %d.\n", ret);
1870 goto global_attr_free;
1871 }
1872 }
1873
1874 return ret;
1875
1876 global_attr_free:
1877 cpufreq_unregister_driver(current_pstate_driver);
1878 disable_driver:
1879 amd_pstate_enable(false);
1880 return ret;
1881 }
1882 device_initcall(amd_pstate_init);
1883
amd_pstate_param(char * str)1884 static int __init amd_pstate_param(char *str)
1885 {
1886 size_t size;
1887 int mode_idx;
1888
1889 if (!str)
1890 return -EINVAL;
1891
1892 size = strlen(str);
1893 mode_idx = get_mode_idx_from_str(str, size);
1894
1895 return amd_pstate_set_driver(mode_idx);
1896 }
1897
amd_prefcore_param(char * str)1898 static int __init amd_prefcore_param(char *str)
1899 {
1900 if (!strcmp(str, "disable"))
1901 amd_pstate_prefcore = false;
1902
1903 return 0;
1904 }
1905
1906 early_param("amd_pstate", amd_pstate_param);
1907 early_param("amd_prefcore", amd_prefcore_param);
1908
1909 MODULE_AUTHOR("Huang Rui <ray.huang@amd.com>");
1910 MODULE_DESCRIPTION("AMD Processor P-state Frequency Driver");
1911