1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * amd-pstate.c - AMD Processor P-state Frequency Driver
5 * Copyright (C) 2021 Advanced Micro Devices, Inc. All Rights Reserved.
7 * Author: Huang Rui <ray.huang@amd.com>
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.
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.
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
36 #include <linux/delay.h>
37 #include <linux/uaccess.h>
38 #include <linux/static_call.h>
39 #include <linux/amd-pstate.h>
41 #include <acpi/processor.h>
42 #include <acpi/cppc_acpi.h>
45 #include <asm/processor.h>
46 #include <asm/cpufeature.h>
47 #include <asm/cpu_device_id.h>
48 #include "amd-pstate-trace.h"
50 #define AMD_PSTATE_TRANSITION_LATENCY 20000
51 #define AMD_PSTATE_TRANSITION_DELAY 1000
54 * TODO: We need more time to fine tune processors with shared memory solution
55 * with community together.
57 * There are some performance drops on the CPU benchmarks which reports from
58 * Suse. We are co-working with them to fine tune the shared memory solution. So
59 * we disable it by default to go acpi-cpufreq on these processors and add a
60 * module parameter to be able to enable it manually for debugging.
62 static struct cpufreq_driver *current_pstate_driver;
63 static struct cpufreq_driver amd_pstate_driver;
64 static struct cpufreq_driver amd_pstate_epp_driver;
65 static int cppc_state = AMD_PSTATE_DISABLE;
68 * AMD Energy Preference Performance (EPP)
69 * The EPP is used in the CCLK DPM controller to drive
70 * the frequency that a core is going to operate during
71 * short periods of activity. EPP values will be utilized for
72 * different OS profiles (balanced, performance, power savings)
73 * display strings corresponding to EPP index in the
74 * energy_perf_strings[]
76 *-------------------------------------
79 * 2 balance_performance
83 enum energy_perf_value_index {
84 EPP_INDEX_DEFAULT = 0,
85 EPP_INDEX_PERFORMANCE,
86 EPP_INDEX_BALANCE_PERFORMANCE,
87 EPP_INDEX_BALANCE_POWERSAVE,
91 static const char * const energy_perf_strings[] = {
92 [EPP_INDEX_DEFAULT] = "default",
93 [EPP_INDEX_PERFORMANCE] = "performance",
94 [EPP_INDEX_BALANCE_PERFORMANCE] = "balance_performance",
95 [EPP_INDEX_BALANCE_POWERSAVE] = "balance_power",
96 [EPP_INDEX_POWERSAVE] = "power",
100 static unsigned int epp_values[] = {
101 [EPP_INDEX_DEFAULT] = 0,
102 [EPP_INDEX_PERFORMANCE] = AMD_CPPC_EPP_PERFORMANCE,
103 [EPP_INDEX_BALANCE_PERFORMANCE] = AMD_CPPC_EPP_BALANCE_PERFORMANCE,
104 [EPP_INDEX_BALANCE_POWERSAVE] = AMD_CPPC_EPP_BALANCE_POWERSAVE,
105 [EPP_INDEX_POWERSAVE] = AMD_CPPC_EPP_POWERSAVE,
108 typedef int (*cppc_mode_transition_fn)(int);
110 static inline int get_mode_idx_from_str(const char *str, size_t size)
114 for (i=0; i < AMD_PSTATE_MAX; i++) {
115 if (!strncmp(str, amd_pstate_mode_string[i], size))
121 static DEFINE_MUTEX(amd_pstate_limits_lock);
122 static DEFINE_MUTEX(amd_pstate_driver_lock);
124 static s16 amd_pstate_get_epp(struct amd_cpudata *cpudata, u64 cppc_req_cached)
129 if (boot_cpu_has(X86_FEATURE_CPPC)) {
130 if (!cppc_req_cached) {
131 epp = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,
136 epp = (cppc_req_cached >> 24) & 0xFF;
138 ret = cppc_get_epp_perf(cpudata->cpu, &epp);
140 pr_debug("Could not retrieve energy perf value (%d)\n", ret);
145 return (s16)(epp & 0xff);
148 static int amd_pstate_get_energy_pref_index(struct amd_cpudata *cpudata)
153 epp = amd_pstate_get_epp(cpudata, 0);
158 case AMD_CPPC_EPP_PERFORMANCE:
159 index = EPP_INDEX_PERFORMANCE;
161 case AMD_CPPC_EPP_BALANCE_PERFORMANCE:
162 index = EPP_INDEX_BALANCE_PERFORMANCE;
164 case AMD_CPPC_EPP_BALANCE_POWERSAVE:
165 index = EPP_INDEX_BALANCE_POWERSAVE;
167 case AMD_CPPC_EPP_POWERSAVE:
168 index = EPP_INDEX_POWERSAVE;
177 static int amd_pstate_set_epp(struct amd_cpudata *cpudata, u32 epp)
180 struct cppc_perf_ctrls perf_ctrls;
182 if (boot_cpu_has(X86_FEATURE_CPPC)) {
183 u64 value = READ_ONCE(cpudata->cppc_req_cached);
185 value &= ~GENMASK_ULL(31, 24);
186 value |= (u64)epp << 24;
187 WRITE_ONCE(cpudata->cppc_req_cached, value);
189 ret = wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
191 cpudata->epp_cached = epp;
193 perf_ctrls.energy_perf = epp;
194 ret = cppc_set_epp_perf(cpudata->cpu, &perf_ctrls, 1);
196 pr_debug("failed to set energy perf value (%d)\n", ret);
199 cpudata->epp_cached = epp;
205 static int amd_pstate_set_energy_pref_index(struct amd_cpudata *cpudata,
212 pr_debug("EPP pref_index is invalid\n");
217 epp = epp_values[pref_index];
219 if (epp > 0 && cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) {
220 pr_debug("EPP cannot be set under performance policy\n");
224 ret = amd_pstate_set_epp(cpudata, epp);
229 static inline int pstate_enable(bool enable)
231 return wrmsrl_safe(MSR_AMD_CPPC_ENABLE, enable);
234 static int cppc_enable(bool enable)
237 struct cppc_perf_ctrls perf_ctrls;
239 for_each_present_cpu(cpu) {
240 ret = cppc_set_enable(cpu, enable);
244 /* Enable autonomous mode for EPP */
245 if (cppc_state == AMD_PSTATE_ACTIVE) {
246 /* Set desired perf as zero to allow EPP firmware control */
247 perf_ctrls.desired_perf = 0;
248 ret = cppc_set_perf(cpu, &perf_ctrls);
257 DEFINE_STATIC_CALL(amd_pstate_enable, pstate_enable);
259 static inline int amd_pstate_enable(bool enable)
261 return static_call(amd_pstate_enable)(enable);
264 static int pstate_init_perf(struct amd_cpudata *cpudata)
269 int ret = rdmsrl_safe_on_cpu(cpudata->cpu, MSR_AMD_CPPC_CAP1,
275 * TODO: Introduce AMD specific power feature.
277 * CPPC entry doesn't indicate the highest performance in some ASICs.
279 highest_perf = amd_get_highest_perf();
280 if (highest_perf > AMD_CPPC_HIGHEST_PERF(cap1))
281 highest_perf = AMD_CPPC_HIGHEST_PERF(cap1);
283 WRITE_ONCE(cpudata->highest_perf, highest_perf);
285 WRITE_ONCE(cpudata->nominal_perf, AMD_CPPC_NOMINAL_PERF(cap1));
286 WRITE_ONCE(cpudata->lowest_nonlinear_perf, AMD_CPPC_LOWNONLIN_PERF(cap1));
287 WRITE_ONCE(cpudata->lowest_perf, AMD_CPPC_LOWEST_PERF(cap1));
292 static int cppc_init_perf(struct amd_cpudata *cpudata)
294 struct cppc_perf_caps cppc_perf;
297 int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
301 highest_perf = amd_get_highest_perf();
302 if (highest_perf > cppc_perf.highest_perf)
303 highest_perf = cppc_perf.highest_perf;
305 WRITE_ONCE(cpudata->highest_perf, highest_perf);
307 WRITE_ONCE(cpudata->nominal_perf, cppc_perf.nominal_perf);
308 WRITE_ONCE(cpudata->lowest_nonlinear_perf,
309 cppc_perf.lowest_nonlinear_perf);
310 WRITE_ONCE(cpudata->lowest_perf, cppc_perf.lowest_perf);
312 if (cppc_state == AMD_PSTATE_ACTIVE)
315 ret = cppc_get_auto_sel_caps(cpudata->cpu, &cppc_perf);
317 pr_warn("failed to get auto_sel, ret: %d\n", ret);
321 ret = cppc_set_auto_sel(cpudata->cpu,
322 (cppc_state == AMD_PSTATE_PASSIVE) ? 0 : 1);
325 pr_warn("failed to set auto_sel, ret: %d\n", ret);
330 DEFINE_STATIC_CALL(amd_pstate_init_perf, pstate_init_perf);
332 static inline int amd_pstate_init_perf(struct amd_cpudata *cpudata)
334 return static_call(amd_pstate_init_perf)(cpudata);
337 static void pstate_update_perf(struct amd_cpudata *cpudata, u32 min_perf,
338 u32 des_perf, u32 max_perf, bool fast_switch)
341 wrmsrl(MSR_AMD_CPPC_REQ, READ_ONCE(cpudata->cppc_req_cached));
343 wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,
344 READ_ONCE(cpudata->cppc_req_cached));
347 static void cppc_update_perf(struct amd_cpudata *cpudata,
348 u32 min_perf, u32 des_perf,
349 u32 max_perf, bool fast_switch)
351 struct cppc_perf_ctrls perf_ctrls;
353 perf_ctrls.max_perf = max_perf;
354 perf_ctrls.min_perf = min_perf;
355 perf_ctrls.desired_perf = des_perf;
357 cppc_set_perf(cpudata->cpu, &perf_ctrls);
360 DEFINE_STATIC_CALL(amd_pstate_update_perf, pstate_update_perf);
362 static inline void amd_pstate_update_perf(struct amd_cpudata *cpudata,
363 u32 min_perf, u32 des_perf,
364 u32 max_perf, bool fast_switch)
366 static_call(amd_pstate_update_perf)(cpudata, min_perf, des_perf,
367 max_perf, fast_switch);
370 static inline bool amd_pstate_sample(struct amd_cpudata *cpudata)
372 u64 aperf, mperf, tsc;
375 local_irq_save(flags);
376 rdmsrl(MSR_IA32_APERF, aperf);
377 rdmsrl(MSR_IA32_MPERF, mperf);
380 if (cpudata->prev.mperf == mperf || cpudata->prev.tsc == tsc) {
381 local_irq_restore(flags);
385 local_irq_restore(flags);
387 cpudata->cur.aperf = aperf;
388 cpudata->cur.mperf = mperf;
389 cpudata->cur.tsc = tsc;
390 cpudata->cur.aperf -= cpudata->prev.aperf;
391 cpudata->cur.mperf -= cpudata->prev.mperf;
392 cpudata->cur.tsc -= cpudata->prev.tsc;
394 cpudata->prev.aperf = aperf;
395 cpudata->prev.mperf = mperf;
396 cpudata->prev.tsc = tsc;
398 cpudata->freq = div64_u64((cpudata->cur.aperf * cpu_khz), cpudata->cur.mperf);
403 static void amd_pstate_update(struct amd_cpudata *cpudata, u32 min_perf,
404 u32 des_perf, u32 max_perf, bool fast_switch, int gov_flags)
406 u64 prev = READ_ONCE(cpudata->cppc_req_cached);
409 des_perf = clamp_t(unsigned long, des_perf, min_perf, max_perf);
411 if ((cppc_state == AMD_PSTATE_GUIDED) && (gov_flags & CPUFREQ_GOV_DYNAMIC_SWITCHING)) {
416 value &= ~AMD_CPPC_MIN_PERF(~0L);
417 value |= AMD_CPPC_MIN_PERF(min_perf);
419 value &= ~AMD_CPPC_DES_PERF(~0L);
420 value |= AMD_CPPC_DES_PERF(des_perf);
422 value &= ~AMD_CPPC_MAX_PERF(~0L);
423 value |= AMD_CPPC_MAX_PERF(max_perf);
425 if (trace_amd_pstate_perf_enabled() && amd_pstate_sample(cpudata)) {
426 trace_amd_pstate_perf(min_perf, des_perf, max_perf, cpudata->freq,
427 cpudata->cur.mperf, cpudata->cur.aperf, cpudata->cur.tsc,
428 cpudata->cpu, (value != prev), fast_switch);
434 WRITE_ONCE(cpudata->cppc_req_cached, value);
436 amd_pstate_update_perf(cpudata, min_perf, des_perf,
437 max_perf, fast_switch);
440 static int amd_pstate_verify(struct cpufreq_policy_data *policy)
442 cpufreq_verify_within_cpu_limits(policy);
447 static int amd_pstate_update_freq(struct cpufreq_policy *policy,
448 unsigned int target_freq, bool fast_switch)
450 struct cpufreq_freqs freqs;
451 struct amd_cpudata *cpudata = policy->driver_data;
452 unsigned long max_perf, min_perf, des_perf, cap_perf;
454 if (!cpudata->max_freq)
457 cap_perf = READ_ONCE(cpudata->highest_perf);
458 min_perf = READ_ONCE(cpudata->lowest_perf);
461 freqs.old = policy->cur;
462 freqs.new = target_freq;
464 des_perf = DIV_ROUND_CLOSEST(target_freq * cap_perf,
467 WARN_ON(fast_switch && !policy->fast_switch_enabled);
469 * If fast_switch is desired, then there aren't any registered
470 * transition notifiers. See comment for
471 * cpufreq_enable_fast_switch().
474 cpufreq_freq_transition_begin(policy, &freqs);
476 amd_pstate_update(cpudata, min_perf, des_perf,
477 max_perf, fast_switch, policy->governor->flags);
480 cpufreq_freq_transition_end(policy, &freqs, false);
485 static int amd_pstate_target(struct cpufreq_policy *policy,
486 unsigned int target_freq,
487 unsigned int relation)
489 return amd_pstate_update_freq(policy, target_freq, false);
492 static unsigned int amd_pstate_fast_switch(struct cpufreq_policy *policy,
493 unsigned int target_freq)
495 return amd_pstate_update_freq(policy, target_freq, true);
498 static void amd_pstate_adjust_perf(unsigned int cpu,
499 unsigned long _min_perf,
500 unsigned long target_perf,
501 unsigned long capacity)
503 unsigned long max_perf, min_perf, des_perf,
504 cap_perf, lowest_nonlinear_perf, max_freq;
505 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
506 struct amd_cpudata *cpudata = policy->driver_data;
507 unsigned int target_freq;
509 cap_perf = READ_ONCE(cpudata->highest_perf);
510 lowest_nonlinear_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
511 max_freq = READ_ONCE(cpudata->max_freq);
514 if (target_perf < capacity)
515 des_perf = DIV_ROUND_UP(cap_perf * target_perf, capacity);
517 min_perf = READ_ONCE(cpudata->highest_perf);
518 if (_min_perf < capacity)
519 min_perf = DIV_ROUND_UP(cap_perf * _min_perf, capacity);
521 if (min_perf < lowest_nonlinear_perf)
522 min_perf = lowest_nonlinear_perf;
525 if (max_perf < min_perf)
528 des_perf = clamp_t(unsigned long, des_perf, min_perf, max_perf);
529 target_freq = div_u64(des_perf * max_freq, max_perf);
530 policy->cur = target_freq;
532 amd_pstate_update(cpudata, min_perf, des_perf, max_perf, true,
533 policy->governor->flags);
534 cpufreq_cpu_put(policy);
537 static int amd_get_min_freq(struct amd_cpudata *cpudata)
539 struct cppc_perf_caps cppc_perf;
541 int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
546 return cppc_perf.lowest_freq * 1000;
549 static int amd_get_max_freq(struct amd_cpudata *cpudata)
551 struct cppc_perf_caps cppc_perf;
552 u32 max_perf, max_freq, nominal_freq, nominal_perf;
555 int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
559 nominal_freq = cppc_perf.nominal_freq;
560 nominal_perf = READ_ONCE(cpudata->nominal_perf);
561 max_perf = READ_ONCE(cpudata->highest_perf);
563 boost_ratio = div_u64(max_perf << SCHED_CAPACITY_SHIFT,
566 max_freq = nominal_freq * boost_ratio >> SCHED_CAPACITY_SHIFT;
569 return max_freq * 1000;
572 static int amd_get_nominal_freq(struct amd_cpudata *cpudata)
574 struct cppc_perf_caps cppc_perf;
576 int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
581 return cppc_perf.nominal_freq * 1000;
584 static int amd_get_lowest_nonlinear_freq(struct amd_cpudata *cpudata)
586 struct cppc_perf_caps cppc_perf;
587 u32 lowest_nonlinear_freq, lowest_nonlinear_perf,
588 nominal_freq, nominal_perf;
589 u64 lowest_nonlinear_ratio;
591 int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
595 nominal_freq = cppc_perf.nominal_freq;
596 nominal_perf = READ_ONCE(cpudata->nominal_perf);
598 lowest_nonlinear_perf = cppc_perf.lowest_nonlinear_perf;
600 lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,
603 lowest_nonlinear_freq = nominal_freq * lowest_nonlinear_ratio >> SCHED_CAPACITY_SHIFT;
606 return lowest_nonlinear_freq * 1000;
609 static int amd_pstate_set_boost(struct cpufreq_policy *policy, int state)
611 struct amd_cpudata *cpudata = policy->driver_data;
614 if (!cpudata->boost_supported) {
615 pr_err("Boost mode is not supported by this processor or SBIOS\n");
620 policy->cpuinfo.max_freq = cpudata->max_freq;
622 policy->cpuinfo.max_freq = cpudata->nominal_freq;
624 policy->max = policy->cpuinfo.max_freq;
626 ret = freq_qos_update_request(&cpudata->req[1],
627 policy->cpuinfo.max_freq);
634 static void amd_pstate_boost_init(struct amd_cpudata *cpudata)
636 u32 highest_perf, nominal_perf;
638 highest_perf = READ_ONCE(cpudata->highest_perf);
639 nominal_perf = READ_ONCE(cpudata->nominal_perf);
641 if (highest_perf <= nominal_perf)
644 cpudata->boost_supported = true;
645 current_pstate_driver->boost_enabled = true;
648 static void amd_perf_ctl_reset(unsigned int cpu)
650 wrmsrl_on_cpu(cpu, MSR_AMD_PERF_CTL, 0);
653 static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
655 int min_freq, max_freq, nominal_freq, lowest_nonlinear_freq, ret;
657 struct amd_cpudata *cpudata;
660 * Resetting PERF_CTL_MSR will put the CPU in P0 frequency,
661 * which is ideal for initialization process.
663 amd_perf_ctl_reset(policy->cpu);
664 dev = get_cpu_device(policy->cpu);
668 cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
672 cpudata->cpu = policy->cpu;
674 ret = amd_pstate_init_perf(cpudata);
678 min_freq = amd_get_min_freq(cpudata);
679 max_freq = amd_get_max_freq(cpudata);
680 nominal_freq = amd_get_nominal_freq(cpudata);
681 lowest_nonlinear_freq = amd_get_lowest_nonlinear_freq(cpudata);
683 if (min_freq < 0 || max_freq < 0 || min_freq > max_freq) {
684 dev_err(dev, "min_freq(%d) or max_freq(%d) value is incorrect\n",
690 policy->cpuinfo.transition_latency = AMD_PSTATE_TRANSITION_LATENCY;
691 policy->transition_delay_us = AMD_PSTATE_TRANSITION_DELAY;
693 policy->min = min_freq;
694 policy->max = max_freq;
696 policy->cpuinfo.min_freq = min_freq;
697 policy->cpuinfo.max_freq = max_freq;
699 /* It will be updated by governor */
700 policy->cur = policy->cpuinfo.min_freq;
702 if (boot_cpu_has(X86_FEATURE_CPPC))
703 policy->fast_switch_possible = true;
705 ret = freq_qos_add_request(&policy->constraints, &cpudata->req[0],
706 FREQ_QOS_MIN, policy->cpuinfo.min_freq);
708 dev_err(dev, "Failed to add min-freq constraint (%d)\n", ret);
712 ret = freq_qos_add_request(&policy->constraints, &cpudata->req[1],
713 FREQ_QOS_MAX, policy->cpuinfo.max_freq);
715 dev_err(dev, "Failed to add max-freq constraint (%d)\n", ret);
719 /* Initial processor data capability frequencies */
720 cpudata->max_freq = max_freq;
721 cpudata->min_freq = min_freq;
722 cpudata->nominal_freq = nominal_freq;
723 cpudata->lowest_nonlinear_freq = lowest_nonlinear_freq;
725 policy->driver_data = cpudata;
727 amd_pstate_boost_init(cpudata);
728 if (!current_pstate_driver->adjust_perf)
729 current_pstate_driver->adjust_perf = amd_pstate_adjust_perf;
734 freq_qos_remove_request(&cpudata->req[0]);
740 static int amd_pstate_cpu_exit(struct cpufreq_policy *policy)
742 struct amd_cpudata *cpudata = policy->driver_data;
744 freq_qos_remove_request(&cpudata->req[1]);
745 freq_qos_remove_request(&cpudata->req[0]);
746 policy->fast_switch_possible = false;
752 static int amd_pstate_cpu_resume(struct cpufreq_policy *policy)
756 ret = amd_pstate_enable(true);
758 pr_err("failed to enable amd-pstate during resume, return %d\n", ret);
763 static int amd_pstate_cpu_suspend(struct cpufreq_policy *policy)
767 ret = amd_pstate_enable(false);
769 pr_err("failed to disable amd-pstate during suspend, return %d\n", ret);
774 /* Sysfs attributes */
777 * This frequency is to indicate the maximum hardware frequency.
778 * If boost is not active but supported, the frequency will be larger than the
781 static ssize_t show_amd_pstate_max_freq(struct cpufreq_policy *policy,
785 struct amd_cpudata *cpudata = policy->driver_data;
787 max_freq = amd_get_max_freq(cpudata);
791 return sysfs_emit(buf, "%u\n", max_freq);
794 static ssize_t show_amd_pstate_lowest_nonlinear_freq(struct cpufreq_policy *policy,
798 struct amd_cpudata *cpudata = policy->driver_data;
800 freq = amd_get_lowest_nonlinear_freq(cpudata);
804 return sysfs_emit(buf, "%u\n", freq);
808 * In some of ASICs, the highest_perf is not the one in the _CPC table, so we
809 * need to expose it to sysfs.
811 static ssize_t show_amd_pstate_highest_perf(struct cpufreq_policy *policy,
815 struct amd_cpudata *cpudata = policy->driver_data;
817 perf = READ_ONCE(cpudata->highest_perf);
819 return sysfs_emit(buf, "%u\n", perf);
822 static ssize_t show_energy_performance_available_preferences(
823 struct cpufreq_policy *policy, char *buf)
828 while (energy_perf_strings[i] != NULL)
829 offset += sysfs_emit_at(buf, offset, "%s ", energy_perf_strings[i++]);
831 sysfs_emit_at(buf, offset, "\n");
836 static ssize_t store_energy_performance_preference(
837 struct cpufreq_policy *policy, const char *buf, size_t count)
839 struct amd_cpudata *cpudata = policy->driver_data;
840 char str_preference[21];
843 ret = sscanf(buf, "%20s", str_preference);
847 ret = match_string(energy_perf_strings, -1, str_preference);
851 mutex_lock(&amd_pstate_limits_lock);
852 ret = amd_pstate_set_energy_pref_index(cpudata, ret);
853 mutex_unlock(&amd_pstate_limits_lock);
858 static ssize_t show_energy_performance_preference(
859 struct cpufreq_policy *policy, char *buf)
861 struct amd_cpudata *cpudata = policy->driver_data;
864 preference = amd_pstate_get_energy_pref_index(cpudata);
868 return sysfs_emit(buf, "%s\n", energy_perf_strings[preference]);
871 static void amd_pstate_driver_cleanup(void)
873 amd_pstate_enable(false);
874 cppc_state = AMD_PSTATE_DISABLE;
875 current_pstate_driver = NULL;
878 static int amd_pstate_register_driver(int mode)
882 if (mode == AMD_PSTATE_PASSIVE || mode == AMD_PSTATE_GUIDED)
883 current_pstate_driver = &amd_pstate_driver;
884 else if (mode == AMD_PSTATE_ACTIVE)
885 current_pstate_driver = &amd_pstate_epp_driver;
890 ret = cpufreq_register_driver(current_pstate_driver);
892 amd_pstate_driver_cleanup();
898 static int amd_pstate_unregister_driver(int dummy)
900 cpufreq_unregister_driver(current_pstate_driver);
901 amd_pstate_driver_cleanup();
905 static int amd_pstate_change_mode_without_dvr_change(int mode)
911 if (boot_cpu_has(X86_FEATURE_CPPC) || cppc_state == AMD_PSTATE_ACTIVE)
914 for_each_present_cpu(cpu) {
915 cppc_set_auto_sel(cpu, (cppc_state == AMD_PSTATE_PASSIVE) ? 0 : 1);
921 static int amd_pstate_change_driver_mode(int mode)
925 ret = amd_pstate_unregister_driver(0);
929 ret = amd_pstate_register_driver(mode);
936 static cppc_mode_transition_fn mode_state_machine[AMD_PSTATE_MAX][AMD_PSTATE_MAX] = {
937 [AMD_PSTATE_DISABLE] = {
938 [AMD_PSTATE_DISABLE] = NULL,
939 [AMD_PSTATE_PASSIVE] = amd_pstate_register_driver,
940 [AMD_PSTATE_ACTIVE] = amd_pstate_register_driver,
941 [AMD_PSTATE_GUIDED] = amd_pstate_register_driver,
943 [AMD_PSTATE_PASSIVE] = {
944 [AMD_PSTATE_DISABLE] = amd_pstate_unregister_driver,
945 [AMD_PSTATE_PASSIVE] = NULL,
946 [AMD_PSTATE_ACTIVE] = amd_pstate_change_driver_mode,
947 [AMD_PSTATE_GUIDED] = amd_pstate_change_mode_without_dvr_change,
949 [AMD_PSTATE_ACTIVE] = {
950 [AMD_PSTATE_DISABLE] = amd_pstate_unregister_driver,
951 [AMD_PSTATE_PASSIVE] = amd_pstate_change_driver_mode,
952 [AMD_PSTATE_ACTIVE] = NULL,
953 [AMD_PSTATE_GUIDED] = amd_pstate_change_driver_mode,
955 [AMD_PSTATE_GUIDED] = {
956 [AMD_PSTATE_DISABLE] = amd_pstate_unregister_driver,
957 [AMD_PSTATE_PASSIVE] = amd_pstate_change_mode_without_dvr_change,
958 [AMD_PSTATE_ACTIVE] = amd_pstate_change_driver_mode,
959 [AMD_PSTATE_GUIDED] = NULL,
963 static ssize_t amd_pstate_show_status(char *buf)
965 if (!current_pstate_driver)
966 return sysfs_emit(buf, "disable\n");
968 return sysfs_emit(buf, "%s\n", amd_pstate_mode_string[cppc_state]);
971 static int amd_pstate_update_status(const char *buf, size_t size)
975 if (size > strlen("passive") || size < strlen("active"))
978 mode_idx = get_mode_idx_from_str(buf, size);
980 if (mode_idx < 0 || mode_idx >= AMD_PSTATE_MAX)
983 if (mode_state_machine[cppc_state][mode_idx])
984 return mode_state_machine[cppc_state][mode_idx](mode_idx);
989 static ssize_t show_status(struct kobject *kobj,
990 struct kobj_attribute *attr, char *buf)
994 mutex_lock(&amd_pstate_driver_lock);
995 ret = amd_pstate_show_status(buf);
996 mutex_unlock(&amd_pstate_driver_lock);
1001 static ssize_t store_status(struct kobject *a, struct kobj_attribute *b,
1002 const char *buf, size_t count)
1004 char *p = memchr(buf, '\n', count);
1007 mutex_lock(&amd_pstate_driver_lock);
1008 ret = amd_pstate_update_status(buf, p ? p - buf : count);
1009 mutex_unlock(&amd_pstate_driver_lock);
1011 return ret < 0 ? ret : count;
1014 cpufreq_freq_attr_ro(amd_pstate_max_freq);
1015 cpufreq_freq_attr_ro(amd_pstate_lowest_nonlinear_freq);
1017 cpufreq_freq_attr_ro(amd_pstate_highest_perf);
1018 cpufreq_freq_attr_rw(energy_performance_preference);
1019 cpufreq_freq_attr_ro(energy_performance_available_preferences);
1020 define_one_global_rw(status);
1022 static struct freq_attr *amd_pstate_attr[] = {
1023 &amd_pstate_max_freq,
1024 &amd_pstate_lowest_nonlinear_freq,
1025 &amd_pstate_highest_perf,
1029 static struct freq_attr *amd_pstate_epp_attr[] = {
1030 &amd_pstate_max_freq,
1031 &amd_pstate_lowest_nonlinear_freq,
1032 &amd_pstate_highest_perf,
1033 &energy_performance_preference,
1034 &energy_performance_available_preferences,
1038 static struct attribute *pstate_global_attributes[] = {
1043 static const struct attribute_group amd_pstate_global_attr_group = {
1044 .name = "amd_pstate",
1045 .attrs = pstate_global_attributes,
1048 static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
1050 int min_freq, max_freq, nominal_freq, lowest_nonlinear_freq, ret;
1051 struct amd_cpudata *cpudata;
1056 * Resetting PERF_CTL_MSR will put the CPU in P0 frequency,
1057 * which is ideal for initialization process.
1059 amd_perf_ctl_reset(policy->cpu);
1060 dev = get_cpu_device(policy->cpu);
1064 cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
1068 cpudata->cpu = policy->cpu;
1069 cpudata->epp_policy = 0;
1071 ret = amd_pstate_init_perf(cpudata);
1075 min_freq = amd_get_min_freq(cpudata);
1076 max_freq = amd_get_max_freq(cpudata);
1077 nominal_freq = amd_get_nominal_freq(cpudata);
1078 lowest_nonlinear_freq = amd_get_lowest_nonlinear_freq(cpudata);
1079 if (min_freq < 0 || max_freq < 0 || min_freq > max_freq) {
1080 dev_err(dev, "min_freq(%d) or max_freq(%d) value is incorrect\n",
1081 min_freq, max_freq);
1086 policy->cpuinfo.min_freq = min_freq;
1087 policy->cpuinfo.max_freq = max_freq;
1088 /* It will be updated by governor */
1089 policy->cur = policy->cpuinfo.min_freq;
1091 /* Initial processor data capability frequencies */
1092 cpudata->max_freq = max_freq;
1093 cpudata->min_freq = min_freq;
1094 cpudata->nominal_freq = nominal_freq;
1095 cpudata->lowest_nonlinear_freq = lowest_nonlinear_freq;
1097 policy->driver_data = cpudata;
1099 cpudata->epp_cached = amd_pstate_get_epp(cpudata, 0);
1101 policy->min = policy->cpuinfo.min_freq;
1102 policy->max = policy->cpuinfo.max_freq;
1105 * Set the policy to powersave to provide a valid fallback value in case
1106 * the default cpufreq governor is neither powersave nor performance.
1108 policy->policy = CPUFREQ_POLICY_POWERSAVE;
1110 if (boot_cpu_has(X86_FEATURE_CPPC)) {
1111 ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, &value);
1114 WRITE_ONCE(cpudata->cppc_req_cached, value);
1116 ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_CAP1, &value);
1119 WRITE_ONCE(cpudata->cppc_cap1_cached, value);
1121 amd_pstate_boost_init(cpudata);
1130 static int amd_pstate_epp_cpu_exit(struct cpufreq_policy *policy)
1132 pr_debug("CPU %d exiting\n", policy->cpu);
1136 static void amd_pstate_epp_init(unsigned int cpu)
1138 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1139 struct amd_cpudata *cpudata = policy->driver_data;
1140 u32 max_perf, min_perf;
1144 max_perf = READ_ONCE(cpudata->highest_perf);
1145 min_perf = READ_ONCE(cpudata->lowest_perf);
1147 value = READ_ONCE(cpudata->cppc_req_cached);
1149 if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
1150 min_perf = max_perf;
1152 /* Initial min/max values for CPPC Performance Controls Register */
1153 value &= ~AMD_CPPC_MIN_PERF(~0L);
1154 value |= AMD_CPPC_MIN_PERF(min_perf);
1156 value &= ~AMD_CPPC_MAX_PERF(~0L);
1157 value |= AMD_CPPC_MAX_PERF(max_perf);
1159 /* CPPC EPP feature require to set zero to the desire perf bit */
1160 value &= ~AMD_CPPC_DES_PERF(~0L);
1161 value |= AMD_CPPC_DES_PERF(0);
1163 if (cpudata->epp_policy == cpudata->policy)
1166 cpudata->epp_policy = cpudata->policy;
1168 /* Get BIOS pre-defined epp value */
1169 epp = amd_pstate_get_epp(cpudata, value);
1172 * This return value can only be negative for shared_memory
1173 * systems where EPP register read/write not supported.
1178 if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
1181 /* Set initial EPP value */
1182 if (boot_cpu_has(X86_FEATURE_CPPC)) {
1183 value &= ~GENMASK_ULL(31, 24);
1184 value |= (u64)epp << 24;
1187 WRITE_ONCE(cpudata->cppc_req_cached, value);
1188 amd_pstate_set_epp(cpudata, epp);
1190 cpufreq_cpu_put(policy);
1193 static int amd_pstate_epp_set_policy(struct cpufreq_policy *policy)
1195 struct amd_cpudata *cpudata = policy->driver_data;
1197 if (!policy->cpuinfo.max_freq)
1200 pr_debug("set_policy: cpuinfo.max %u policy->max %u\n",
1201 policy->cpuinfo.max_freq, policy->max);
1203 cpudata->policy = policy->policy;
1205 amd_pstate_epp_init(policy->cpu);
1210 static void amd_pstate_epp_reenable(struct amd_cpudata *cpudata)
1212 struct cppc_perf_ctrls perf_ctrls;
1213 u64 value, max_perf;
1216 ret = amd_pstate_enable(true);
1218 pr_err("failed to enable amd pstate during resume, return %d\n", ret);
1220 value = READ_ONCE(cpudata->cppc_req_cached);
1221 max_perf = READ_ONCE(cpudata->highest_perf);
1223 if (boot_cpu_has(X86_FEATURE_CPPC)) {
1224 wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
1226 perf_ctrls.max_perf = max_perf;
1227 perf_ctrls.energy_perf = AMD_CPPC_ENERGY_PERF_PREF(cpudata->epp_cached);
1228 cppc_set_perf(cpudata->cpu, &perf_ctrls);
1232 static int amd_pstate_epp_cpu_online(struct cpufreq_policy *policy)
1234 struct amd_cpudata *cpudata = policy->driver_data;
1236 pr_debug("AMD CPU Core %d going online\n", cpudata->cpu);
1238 if (cppc_state == AMD_PSTATE_ACTIVE) {
1239 amd_pstate_epp_reenable(cpudata);
1240 cpudata->suspended = false;
1246 static void amd_pstate_epp_offline(struct cpufreq_policy *policy)
1248 struct amd_cpudata *cpudata = policy->driver_data;
1249 struct cppc_perf_ctrls perf_ctrls;
1253 min_perf = READ_ONCE(cpudata->lowest_perf);
1254 value = READ_ONCE(cpudata->cppc_req_cached);
1256 mutex_lock(&amd_pstate_limits_lock);
1257 if (boot_cpu_has(X86_FEATURE_CPPC)) {
1258 cpudata->epp_policy = CPUFREQ_POLICY_UNKNOWN;
1260 /* Set max perf same as min perf */
1261 value &= ~AMD_CPPC_MAX_PERF(~0L);
1262 value |= AMD_CPPC_MAX_PERF(min_perf);
1263 value &= ~AMD_CPPC_MIN_PERF(~0L);
1264 value |= AMD_CPPC_MIN_PERF(min_perf);
1265 wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
1267 perf_ctrls.desired_perf = 0;
1268 perf_ctrls.max_perf = min_perf;
1269 perf_ctrls.energy_perf = AMD_CPPC_ENERGY_PERF_PREF(HWP_EPP_BALANCE_POWERSAVE);
1270 cppc_set_perf(cpudata->cpu, &perf_ctrls);
1272 mutex_unlock(&amd_pstate_limits_lock);
1275 static int amd_pstate_epp_cpu_offline(struct cpufreq_policy *policy)
1277 struct amd_cpudata *cpudata = policy->driver_data;
1279 pr_debug("AMD CPU Core %d going offline\n", cpudata->cpu);
1281 if (cpudata->suspended)
1284 if (cppc_state == AMD_PSTATE_ACTIVE)
1285 amd_pstate_epp_offline(policy);
1290 static int amd_pstate_epp_verify_policy(struct cpufreq_policy_data *policy)
1292 cpufreq_verify_within_cpu_limits(policy);
1293 pr_debug("policy_max =%d, policy_min=%d\n", policy->max, policy->min);
1297 static int amd_pstate_epp_suspend(struct cpufreq_policy *policy)
1299 struct amd_cpudata *cpudata = policy->driver_data;
1302 /* avoid suspending when EPP is not enabled */
1303 if (cppc_state != AMD_PSTATE_ACTIVE)
1306 /* set this flag to avoid setting core offline*/
1307 cpudata->suspended = true;
1309 /* disable CPPC in lowlevel firmware */
1310 ret = amd_pstate_enable(false);
1312 pr_err("failed to suspend, return %d\n", ret);
1317 static int amd_pstate_epp_resume(struct cpufreq_policy *policy)
1319 struct amd_cpudata *cpudata = policy->driver_data;
1321 if (cpudata->suspended) {
1322 mutex_lock(&amd_pstate_limits_lock);
1324 /* enable amd pstate from suspend state*/
1325 amd_pstate_epp_reenable(cpudata);
1327 mutex_unlock(&amd_pstate_limits_lock);
1329 cpudata->suspended = false;
1335 static struct cpufreq_driver amd_pstate_driver = {
1336 .flags = CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_UPDATE_LIMITS,
1337 .verify = amd_pstate_verify,
1338 .target = amd_pstate_target,
1339 .fast_switch = amd_pstate_fast_switch,
1340 .init = amd_pstate_cpu_init,
1341 .exit = amd_pstate_cpu_exit,
1342 .suspend = amd_pstate_cpu_suspend,
1343 .resume = amd_pstate_cpu_resume,
1344 .set_boost = amd_pstate_set_boost,
1345 .name = "amd-pstate",
1346 .attr = amd_pstate_attr,
1349 static struct cpufreq_driver amd_pstate_epp_driver = {
1350 .flags = CPUFREQ_CONST_LOOPS,
1351 .verify = amd_pstate_epp_verify_policy,
1352 .setpolicy = amd_pstate_epp_set_policy,
1353 .init = amd_pstate_epp_cpu_init,
1354 .exit = amd_pstate_epp_cpu_exit,
1355 .offline = amd_pstate_epp_cpu_offline,
1356 .online = amd_pstate_epp_cpu_online,
1357 .suspend = amd_pstate_epp_suspend,
1358 .resume = amd_pstate_epp_resume,
1359 .name = "amd_pstate_epp",
1360 .attr = amd_pstate_epp_attr,
1363 static int __init amd_pstate_init(void)
1365 struct device *dev_root;
1368 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
1371 * by default the pstate driver is disabled to load
1372 * enable the amd_pstate passive mode driver explicitly
1373 * with amd_pstate=passive or other modes in kernel command line
1375 if (cppc_state == AMD_PSTATE_DISABLE) {
1376 pr_info("driver load is disabled, boot with specific mode to enable this\n");
1380 if (!acpi_cpc_valid()) {
1381 pr_warn_once("the _CPC object is not present in SBIOS or ACPI disabled\n");
1385 /* don't keep reloading if cpufreq_driver exists */
1386 if (cpufreq_get_current_driver())
1389 /* capability check */
1390 if (boot_cpu_has(X86_FEATURE_CPPC)) {
1391 pr_debug("AMD CPPC MSR based functionality is supported\n");
1392 if (cppc_state != AMD_PSTATE_ACTIVE)
1393 current_pstate_driver->adjust_perf = amd_pstate_adjust_perf;
1395 pr_debug("AMD CPPC shared memory based functionality is supported\n");
1396 static_call_update(amd_pstate_enable, cppc_enable);
1397 static_call_update(amd_pstate_init_perf, cppc_init_perf);
1398 static_call_update(amd_pstate_update_perf, cppc_update_perf);
1401 /* enable amd pstate feature */
1402 ret = amd_pstate_enable(true);
1404 pr_err("failed to enable with return %d\n", ret);
1408 ret = cpufreq_register_driver(current_pstate_driver);
1410 pr_err("failed to register with return %d\n", ret);
1412 dev_root = bus_get_dev_root(&cpu_subsys);
1414 ret = sysfs_create_group(&dev_root->kobj, &amd_pstate_global_attr_group);
1415 put_device(dev_root);
1417 pr_err("sysfs attribute export failed with error %d.\n", ret);
1418 goto global_attr_free;
1425 cpufreq_unregister_driver(current_pstate_driver);
1428 device_initcall(amd_pstate_init);
1430 static int __init amd_pstate_param(char *str)
1439 mode_idx = get_mode_idx_from_str(str, size);
1441 if (mode_idx >= AMD_PSTATE_DISABLE && mode_idx < AMD_PSTATE_MAX) {
1442 cppc_state = mode_idx;
1443 if (cppc_state == AMD_PSTATE_DISABLE)
1444 pr_info("driver is explicitly disabled\n");
1446 if (cppc_state == AMD_PSTATE_ACTIVE)
1447 current_pstate_driver = &amd_pstate_epp_driver;
1449 if (cppc_state == AMD_PSTATE_PASSIVE || cppc_state == AMD_PSTATE_GUIDED)
1450 current_pstate_driver = &amd_pstate_driver;
1457 early_param("amd_pstate", amd_pstate_param);
1459 MODULE_AUTHOR("Huang Rui <ray.huang@amd.com>");
1460 MODULE_DESCRIPTION("AMD Processor P-state Frequency Driver");