Debian Codegraph @

drivers/cpufreq/amd-pstate.c

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