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/topology.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>
49 #include "amd-pstate.h"
50 #include "amd-pstate-trace.h"
52 #define AMD_PSTATE_TRANSITION_LATENCY 20000
53 #define AMD_PSTATE_TRANSITION_DELAY 1000
54 #define CPPC_HIGHEST_PERF_PERFORMANCE 196
55 #define CPPC_HIGHEST_PERF_DEFAULT 166
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
63 * enum amd_pstate_mode - driver working mode of amd pstate
65 enum amd_pstate_mode {
66 AMD_PSTATE_UNDEFINED = 0,
74 static const char * const amd_pstate_mode_string[] = {
75 [AMD_PSTATE_UNDEFINED] = "undefined",
76 [AMD_PSTATE_DISABLE] = "disable",
77 [AMD_PSTATE_PASSIVE] = "passive",
78 [AMD_PSTATE_ACTIVE] = "active",
79 [AMD_PSTATE_GUIDED] = "guided",
89 * TODO: We need more time to fine tune processors with shared memory solution
90 * with community together.
92 * There are some performance drops on the CPU benchmarks which reports from
93 * Suse. We are co-working with them to fine tune the shared memory solution. So
94 * we disable it by default to go acpi-cpufreq on these processors and add a
95 * module parameter to be able to enable it manually for debugging.
97 static struct cpufreq_driver *current_pstate_driver;
98 static struct cpufreq_driver amd_pstate_driver;
99 static struct cpufreq_driver amd_pstate_epp_driver;
100 static int cppc_state = AMD_PSTATE_UNDEFINED;
101 static bool cppc_enabled;
102 static bool amd_pstate_prefcore = true;
103 static struct quirk_entry *quirks;
106 * AMD Energy Preference Performance (EPP)
107 * The EPP is used in the CCLK DPM controller to drive
108 * the frequency that a core is going to operate during
109 * short periods of activity. EPP values will be utilized for
110 * different OS profiles (balanced, performance, power savings)
111 * display strings corresponding to EPP index in the
112 * energy_perf_strings[]
114 *-------------------------------------
117 * 2 balance_performance
121 enum energy_perf_value_index {
122 EPP_INDEX_DEFAULT = 0,
123 EPP_INDEX_PERFORMANCE,
124 EPP_INDEX_BALANCE_PERFORMANCE,
125 EPP_INDEX_BALANCE_POWERSAVE,
129 static const char * const energy_perf_strings[] = {
130 [EPP_INDEX_DEFAULT] = "default",
131 [EPP_INDEX_PERFORMANCE] = "performance",
132 [EPP_INDEX_BALANCE_PERFORMANCE] = "balance_performance",
133 [EPP_INDEX_BALANCE_POWERSAVE] = "balance_power",
134 [EPP_INDEX_POWERSAVE] = "power",
138 static unsigned int epp_values[] = {
139 [EPP_INDEX_DEFAULT] = 0,
140 [EPP_INDEX_PERFORMANCE] = AMD_CPPC_EPP_PERFORMANCE,
141 [EPP_INDEX_BALANCE_PERFORMANCE] = AMD_CPPC_EPP_BALANCE_PERFORMANCE,
142 [EPP_INDEX_BALANCE_POWERSAVE] = AMD_CPPC_EPP_BALANCE_POWERSAVE,
143 [EPP_INDEX_POWERSAVE] = AMD_CPPC_EPP_POWERSAVE,
146 typedef int (*cppc_mode_transition_fn)(int);
148 static struct quirk_entry quirk_amd_7k62 = {
149 .nominal_freq = 2600,
153 static int __init dmi_matched_7k62_bios_bug(const struct dmi_system_id *dmi)
156 * match the broken bios for family 17h processor support CPPC V2
157 * broken BIOS lack of nominal_freq and lowest_freq capabilities
158 * definition in ACPI tables
160 if (boot_cpu_has(X86_FEATURE_ZEN2)) {
161 quirks = dmi->driver_data;
162 pr_info("Overriding nominal and lowest frequencies for %s\n", dmi->ident);
169 static const struct dmi_system_id amd_pstate_quirks_table[] __initconst = {
171 .callback = dmi_matched_7k62_bios_bug,
172 .ident = "AMD EPYC 7K62",
174 DMI_MATCH(DMI_BIOS_VERSION, "5.14"),
175 DMI_MATCH(DMI_BIOS_RELEASE, "12/12/2019"),
177 .driver_data = &quirk_amd_7k62,
181 MODULE_DEVICE_TABLE(dmi, amd_pstate_quirks_table);
183 static inline int get_mode_idx_from_str(const char *str, size_t size)
187 for (i=0; i < AMD_PSTATE_MAX; i++) {
188 if (!strncmp(str, amd_pstate_mode_string[i], size))
194 static DEFINE_MUTEX(amd_pstate_limits_lock);
195 static DEFINE_MUTEX(amd_pstate_driver_lock);
197 static s16 amd_pstate_get_epp(struct amd_cpudata *cpudata, u64 cppc_req_cached)
202 if (boot_cpu_has(X86_FEATURE_CPPC)) {
203 if (!cppc_req_cached) {
204 epp = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,
209 epp = (cppc_req_cached >> 24) & 0xFF;
211 ret = cppc_get_epp_perf(cpudata->cpu, &epp);
213 pr_debug("Could not retrieve energy perf value (%d)\n", ret);
218 return (s16)(epp & 0xff);
221 static int amd_pstate_get_energy_pref_index(struct amd_cpudata *cpudata)
226 epp = amd_pstate_get_epp(cpudata, 0);
231 case AMD_CPPC_EPP_PERFORMANCE:
232 index = EPP_INDEX_PERFORMANCE;
234 case AMD_CPPC_EPP_BALANCE_PERFORMANCE:
235 index = EPP_INDEX_BALANCE_PERFORMANCE;
237 case AMD_CPPC_EPP_BALANCE_POWERSAVE:
238 index = EPP_INDEX_BALANCE_POWERSAVE;
240 case AMD_CPPC_EPP_POWERSAVE:
241 index = EPP_INDEX_POWERSAVE;
250 static int amd_pstate_set_epp(struct amd_cpudata *cpudata, u32 epp)
253 struct cppc_perf_ctrls perf_ctrls;
255 if (boot_cpu_has(X86_FEATURE_CPPC)) {
256 u64 value = READ_ONCE(cpudata->cppc_req_cached);
258 value &= ~GENMASK_ULL(31, 24);
259 value |= (u64)epp << 24;
260 WRITE_ONCE(cpudata->cppc_req_cached, value);
262 ret = wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
264 cpudata->epp_cached = epp;
266 perf_ctrls.energy_perf = epp;
267 ret = cppc_set_epp_perf(cpudata->cpu, &perf_ctrls, 1);
269 pr_debug("failed to set energy perf value (%d)\n", ret);
272 cpudata->epp_cached = epp;
278 static int amd_pstate_set_energy_pref_index(struct amd_cpudata *cpudata,
285 pr_debug("EPP pref_index is invalid\n");
290 epp = epp_values[pref_index];
292 if (epp > 0 && cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) {
293 pr_debug("EPP cannot be set under performance policy\n");
297 ret = amd_pstate_set_epp(cpudata, epp);
302 static inline int pstate_enable(bool enable)
305 unsigned long logical_proc_id_mask = 0;
307 if (enable == cppc_enabled)
310 for_each_present_cpu(cpu) {
311 unsigned long logical_id = topology_logical_die_id(cpu);
313 if (test_bit(logical_id, &logical_proc_id_mask))
316 set_bit(logical_id, &logical_proc_id_mask);
318 ret = wrmsrl_safe_on_cpu(cpu, MSR_AMD_CPPC_ENABLE,
324 cppc_enabled = enable;
328 static int cppc_enable(bool enable)
331 struct cppc_perf_ctrls perf_ctrls;
333 if (enable == cppc_enabled)
336 for_each_present_cpu(cpu) {
337 ret = cppc_set_enable(cpu, enable);
341 /* Enable autonomous mode for EPP */
342 if (cppc_state == AMD_PSTATE_ACTIVE) {
343 /* Set desired perf as zero to allow EPP firmware control */
344 perf_ctrls.desired_perf = 0;
345 ret = cppc_set_perf(cpu, &perf_ctrls);
351 cppc_enabled = enable;
355 DEFINE_STATIC_CALL(amd_pstate_enable, pstate_enable);
357 static inline int amd_pstate_enable(bool enable)
359 return static_call(amd_pstate_enable)(enable);
362 static u32 amd_pstate_highest_perf_set(struct amd_cpudata *cpudata)
364 struct cpuinfo_x86 *c = &cpu_data(0);
367 * For AMD CPUs with Family ID 19H and Model ID range 0x70 to 0x7f,
368 * the highest performance level is set to 196.
369 * https://bugzilla.kernel.org/show_bug.cgi?id=218759
371 if (c->x86 == 0x19 && (c->x86_model >= 0x70 && c->x86_model <= 0x7f))
372 return CPPC_HIGHEST_PERF_PERFORMANCE;
374 return CPPC_HIGHEST_PERF_DEFAULT;
377 static int pstate_init_perf(struct amd_cpudata *cpudata)
382 int ret = rdmsrl_safe_on_cpu(cpudata->cpu, MSR_AMD_CPPC_CAP1,
387 /* For platforms that do not support the preferred core feature, the
388 * highest_pef may be configured with 166 or 255, to avoid max frequency
389 * calculated wrongly. we take the AMD_CPPC_HIGHEST_PERF(cap1) value as
390 * the default max perf.
392 if (cpudata->hw_prefcore)
393 highest_perf = amd_pstate_highest_perf_set(cpudata);
395 highest_perf = AMD_CPPC_HIGHEST_PERF(cap1);
397 WRITE_ONCE(cpudata->highest_perf, highest_perf);
398 WRITE_ONCE(cpudata->max_limit_perf, highest_perf);
399 WRITE_ONCE(cpudata->nominal_perf, AMD_CPPC_NOMINAL_PERF(cap1));
400 WRITE_ONCE(cpudata->lowest_nonlinear_perf, AMD_CPPC_LOWNONLIN_PERF(cap1));
401 WRITE_ONCE(cpudata->lowest_perf, AMD_CPPC_LOWEST_PERF(cap1));
402 WRITE_ONCE(cpudata->prefcore_ranking, AMD_CPPC_HIGHEST_PERF(cap1));
403 WRITE_ONCE(cpudata->min_limit_perf, AMD_CPPC_LOWEST_PERF(cap1));
407 static int cppc_init_perf(struct amd_cpudata *cpudata)
409 struct cppc_perf_caps cppc_perf;
412 int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
416 if (cpudata->hw_prefcore)
417 highest_perf = amd_pstate_highest_perf_set(cpudata);
419 highest_perf = cppc_perf.highest_perf;
421 WRITE_ONCE(cpudata->highest_perf, highest_perf);
422 WRITE_ONCE(cpudata->max_limit_perf, highest_perf);
423 WRITE_ONCE(cpudata->nominal_perf, cppc_perf.nominal_perf);
424 WRITE_ONCE(cpudata->lowest_nonlinear_perf,
425 cppc_perf.lowest_nonlinear_perf);
426 WRITE_ONCE(cpudata->lowest_perf, cppc_perf.lowest_perf);
427 WRITE_ONCE(cpudata->prefcore_ranking, cppc_perf.highest_perf);
428 WRITE_ONCE(cpudata->min_limit_perf, cppc_perf.lowest_perf);
430 if (cppc_state == AMD_PSTATE_ACTIVE)
433 ret = cppc_get_auto_sel_caps(cpudata->cpu, &cppc_perf);
435 pr_warn("failed to get auto_sel, ret: %d\n", ret);
439 ret = cppc_set_auto_sel(cpudata->cpu,
440 (cppc_state == AMD_PSTATE_PASSIVE) ? 0 : 1);
443 pr_warn("failed to set auto_sel, ret: %d\n", ret);
448 DEFINE_STATIC_CALL(amd_pstate_init_perf, pstate_init_perf);
450 static inline int amd_pstate_init_perf(struct amd_cpudata *cpudata)
452 return static_call(amd_pstate_init_perf)(cpudata);
455 static void pstate_update_perf(struct amd_cpudata *cpudata, u32 min_perf,
456 u32 des_perf, u32 max_perf, bool fast_switch)
459 wrmsrl(MSR_AMD_CPPC_REQ, READ_ONCE(cpudata->cppc_req_cached));
461 wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,
462 READ_ONCE(cpudata->cppc_req_cached));
465 static void cppc_update_perf(struct amd_cpudata *cpudata,
466 u32 min_perf, u32 des_perf,
467 u32 max_perf, bool fast_switch)
469 struct cppc_perf_ctrls perf_ctrls;
471 perf_ctrls.max_perf = max_perf;
472 perf_ctrls.min_perf = min_perf;
473 perf_ctrls.desired_perf = des_perf;
475 cppc_set_perf(cpudata->cpu, &perf_ctrls);
478 DEFINE_STATIC_CALL(amd_pstate_update_perf, pstate_update_perf);
480 static inline void amd_pstate_update_perf(struct amd_cpudata *cpudata,
481 u32 min_perf, u32 des_perf,
482 u32 max_perf, bool fast_switch)
484 static_call(amd_pstate_update_perf)(cpudata, min_perf, des_perf,
485 max_perf, fast_switch);
488 static inline bool amd_pstate_sample(struct amd_cpudata *cpudata)
490 u64 aperf, mperf, tsc;
493 local_irq_save(flags);
494 rdmsrl(MSR_IA32_APERF, aperf);
495 rdmsrl(MSR_IA32_MPERF, mperf);
498 if (cpudata->prev.mperf == mperf || cpudata->prev.tsc == tsc) {
499 local_irq_restore(flags);
503 local_irq_restore(flags);
505 cpudata->cur.aperf = aperf;
506 cpudata->cur.mperf = mperf;
507 cpudata->cur.tsc = tsc;
508 cpudata->cur.aperf -= cpudata->prev.aperf;
509 cpudata->cur.mperf -= cpudata->prev.mperf;
510 cpudata->cur.tsc -= cpudata->prev.tsc;
512 cpudata->prev.aperf = aperf;
513 cpudata->prev.mperf = mperf;
514 cpudata->prev.tsc = tsc;
516 cpudata->freq = div64_u64((cpudata->cur.aperf * cpu_khz), cpudata->cur.mperf);
521 static void amd_pstate_update(struct amd_cpudata *cpudata, u32 min_perf,
522 u32 des_perf, u32 max_perf, bool fast_switch, int gov_flags)
524 u64 prev = READ_ONCE(cpudata->cppc_req_cached);
527 min_perf = clamp_t(unsigned long, min_perf, cpudata->min_limit_perf,
528 cpudata->max_limit_perf);
529 max_perf = clamp_t(unsigned long, max_perf, cpudata->min_limit_perf,
530 cpudata->max_limit_perf);
531 des_perf = clamp_t(unsigned long, des_perf, min_perf, max_perf);
533 if ((cppc_state == AMD_PSTATE_GUIDED) && (gov_flags & CPUFREQ_GOV_DYNAMIC_SWITCHING)) {
538 value &= ~AMD_CPPC_MIN_PERF(~0L);
539 value |= AMD_CPPC_MIN_PERF(min_perf);
541 value &= ~AMD_CPPC_DES_PERF(~0L);
542 value |= AMD_CPPC_DES_PERF(des_perf);
544 value &= ~AMD_CPPC_MAX_PERF(~0L);
545 value |= AMD_CPPC_MAX_PERF(max_perf);
547 if (trace_amd_pstate_perf_enabled() && amd_pstate_sample(cpudata)) {
548 trace_amd_pstate_perf(min_perf, des_perf, max_perf, cpudata->freq,
549 cpudata->cur.mperf, cpudata->cur.aperf, cpudata->cur.tsc,
550 cpudata->cpu, (value != prev), fast_switch);
556 WRITE_ONCE(cpudata->cppc_req_cached, value);
558 amd_pstate_update_perf(cpudata, min_perf, des_perf,
559 max_perf, fast_switch);
562 static int amd_pstate_verify(struct cpufreq_policy_data *policy)
564 cpufreq_verify_within_cpu_limits(policy);
569 static int amd_pstate_update_min_max_limit(struct cpufreq_policy *policy)
571 u32 max_limit_perf, min_limit_perf, lowest_perf;
572 struct amd_cpudata *cpudata = policy->driver_data;
574 max_limit_perf = div_u64(policy->max * cpudata->highest_perf, cpudata->max_freq);
575 min_limit_perf = div_u64(policy->min * cpudata->highest_perf, cpudata->max_freq);
577 lowest_perf = READ_ONCE(cpudata->lowest_perf);
578 if (min_limit_perf < lowest_perf)
579 min_limit_perf = lowest_perf;
581 if (max_limit_perf < min_limit_perf)
582 max_limit_perf = min_limit_perf;
584 WRITE_ONCE(cpudata->max_limit_perf, max_limit_perf);
585 WRITE_ONCE(cpudata->min_limit_perf, min_limit_perf);
586 WRITE_ONCE(cpudata->max_limit_freq, policy->max);
587 WRITE_ONCE(cpudata->min_limit_freq, policy->min);
592 static int amd_pstate_update_freq(struct cpufreq_policy *policy,
593 unsigned int target_freq, bool fast_switch)
595 struct cpufreq_freqs freqs;
596 struct amd_cpudata *cpudata = policy->driver_data;
597 unsigned long max_perf, min_perf, des_perf, cap_perf;
599 if (!cpudata->max_freq)
602 if (policy->min != cpudata->min_limit_freq || policy->max != cpudata->max_limit_freq)
603 amd_pstate_update_min_max_limit(policy);
605 cap_perf = READ_ONCE(cpudata->highest_perf);
606 min_perf = READ_ONCE(cpudata->lowest_perf);
609 freqs.old = policy->cur;
610 freqs.new = target_freq;
612 des_perf = DIV_ROUND_CLOSEST(target_freq * cap_perf,
615 WARN_ON(fast_switch && !policy->fast_switch_enabled);
617 * If fast_switch is desired, then there aren't any registered
618 * transition notifiers. See comment for
619 * cpufreq_enable_fast_switch().
622 cpufreq_freq_transition_begin(policy, &freqs);
624 amd_pstate_update(cpudata, min_perf, des_perf,
625 max_perf, fast_switch, policy->governor->flags);
628 cpufreq_freq_transition_end(policy, &freqs, false);
633 static int amd_pstate_target(struct cpufreq_policy *policy,
634 unsigned int target_freq,
635 unsigned int relation)
637 return amd_pstate_update_freq(policy, target_freq, false);
640 static unsigned int amd_pstate_fast_switch(struct cpufreq_policy *policy,
641 unsigned int target_freq)
643 if (!amd_pstate_update_freq(policy, target_freq, true))
648 static void amd_pstate_adjust_perf(unsigned int cpu,
649 unsigned long _min_perf,
650 unsigned long target_perf,
651 unsigned long capacity)
653 unsigned long max_perf, min_perf, des_perf,
654 cap_perf, lowest_nonlinear_perf, max_freq;
655 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
656 struct amd_cpudata *cpudata = policy->driver_data;
657 unsigned int target_freq;
659 if (policy->min != cpudata->min_limit_freq || policy->max != cpudata->max_limit_freq)
660 amd_pstate_update_min_max_limit(policy);
663 cap_perf = READ_ONCE(cpudata->highest_perf);
664 lowest_nonlinear_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
665 max_freq = READ_ONCE(cpudata->max_freq);
668 if (target_perf < capacity)
669 des_perf = DIV_ROUND_UP(cap_perf * target_perf, capacity);
671 min_perf = READ_ONCE(cpudata->lowest_perf);
672 if (_min_perf < capacity)
673 min_perf = DIV_ROUND_UP(cap_perf * _min_perf, capacity);
675 if (min_perf < lowest_nonlinear_perf)
676 min_perf = lowest_nonlinear_perf;
679 if (max_perf < min_perf)
682 des_perf = clamp_t(unsigned long, des_perf, min_perf, max_perf);
683 target_freq = div_u64(des_perf * max_freq, max_perf);
684 policy->cur = target_freq;
686 amd_pstate_update(cpudata, min_perf, des_perf, max_perf, true,
687 policy->governor->flags);
688 cpufreq_cpu_put(policy);
691 static int amd_pstate_set_boost(struct cpufreq_policy *policy, int state)
693 struct amd_cpudata *cpudata = policy->driver_data;
696 if (!cpudata->boost_supported) {
697 pr_err("Boost mode is not supported by this processor or SBIOS\n");
702 policy->cpuinfo.max_freq = cpudata->max_freq;
704 policy->cpuinfo.max_freq = cpudata->nominal_freq * 1000;
706 policy->max = policy->cpuinfo.max_freq;
708 ret = freq_qos_update_request(&cpudata->req[1],
709 policy->cpuinfo.max_freq);
716 static void amd_pstate_boost_init(struct amd_cpudata *cpudata)
718 u32 highest_perf, nominal_perf;
720 highest_perf = READ_ONCE(cpudata->highest_perf);
721 nominal_perf = READ_ONCE(cpudata->nominal_perf);
723 if (highest_perf <= nominal_perf)
726 cpudata->boost_supported = true;
727 current_pstate_driver->boost_enabled = true;
730 static void amd_perf_ctl_reset(unsigned int cpu)
732 wrmsrl_on_cpu(cpu, MSR_AMD_PERF_CTL, 0);
736 * Set amd-pstate preferred core enable can't be done directly from cpufreq callbacks
737 * due to locking, so queue the work for later.
739 static void amd_pstste_sched_prefcore_workfn(struct work_struct *work)
741 sched_set_itmt_support();
743 static DECLARE_WORK(sched_prefcore_work, amd_pstste_sched_prefcore_workfn);
746 * Get the highest performance register value.
747 * @cpu: CPU from which to get highest performance.
748 * @highest_perf: Return address.
750 * Return: 0 for success, -EIO otherwise.
752 static int amd_pstate_get_highest_perf(int cpu, u32 *highest_perf)
756 if (boot_cpu_has(X86_FEATURE_CPPC)) {
759 ret = rdmsrl_safe_on_cpu(cpu, MSR_AMD_CPPC_CAP1, &cap1);
762 WRITE_ONCE(*highest_perf, AMD_CPPC_HIGHEST_PERF(cap1));
764 u64 cppc_highest_perf;
766 ret = cppc_get_highest_perf(cpu, &cppc_highest_perf);
769 WRITE_ONCE(*highest_perf, cppc_highest_perf);
775 #define CPPC_MAX_PERF U8_MAX
777 static void amd_pstate_init_prefcore(struct amd_cpudata *cpudata)
782 ret = amd_pstate_get_highest_perf(cpudata->cpu, &highest_perf);
786 cpudata->hw_prefcore = true;
787 /* check if CPPC preferred core feature is enabled*/
788 if (highest_perf < CPPC_MAX_PERF)
789 prio = (int)highest_perf;
791 pr_debug("AMD CPPC preferred core is unsupported!\n");
792 cpudata->hw_prefcore = false;
796 if (!amd_pstate_prefcore)
800 * The priorities can be set regardless of whether or not
801 * sched_set_itmt_support(true) has been called and it is valid to
802 * update them at any time after it has been called.
804 sched_set_itmt_core_prio(prio, cpudata->cpu);
806 schedule_work(&sched_prefcore_work);
809 static void amd_pstate_update_limits(unsigned int cpu)
811 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
812 struct amd_cpudata *cpudata = policy->driver_data;
813 u32 prev_high = 0, cur_high = 0;
815 bool highest_perf_changed = false;
817 mutex_lock(&amd_pstate_driver_lock);
818 if ((!amd_pstate_prefcore) || (!cpudata->hw_prefcore))
819 goto free_cpufreq_put;
821 ret = amd_pstate_get_highest_perf(cpu, &cur_high);
823 goto free_cpufreq_put;
825 prev_high = READ_ONCE(cpudata->prefcore_ranking);
826 if (prev_high != cur_high) {
827 highest_perf_changed = true;
828 WRITE_ONCE(cpudata->prefcore_ranking, cur_high);
830 if (cur_high < CPPC_MAX_PERF)
831 sched_set_itmt_core_prio((int)cur_high, cpu);
835 cpufreq_cpu_put(policy);
837 if (!highest_perf_changed)
838 cpufreq_update_policy(cpu);
840 mutex_unlock(&amd_pstate_driver_lock);
844 * Get pstate transition delay time from ACPI tables that firmware set
845 * instead of using hardcode value directly.
847 static u32 amd_pstate_get_transition_delay_us(unsigned int cpu)
849 u32 transition_delay_ns;
851 transition_delay_ns = cppc_get_transition_latency(cpu);
852 if (transition_delay_ns == CPUFREQ_ETERNAL)
853 return AMD_PSTATE_TRANSITION_DELAY;
855 return transition_delay_ns / NSEC_PER_USEC;
859 * Get pstate transition latency value from ACPI tables that firmware
860 * set instead of using hardcode value directly.
862 static u32 amd_pstate_get_transition_latency(unsigned int cpu)
864 u32 transition_latency;
866 transition_latency = cppc_get_transition_latency(cpu);
867 if (transition_latency == CPUFREQ_ETERNAL)
868 return AMD_PSTATE_TRANSITION_LATENCY;
870 return transition_latency;
874 * amd_pstate_init_freq: Initialize the max_freq, min_freq,
875 * nominal_freq and lowest_nonlinear_freq for
876 * the @cpudata object.
878 * Requires: highest_perf, lowest_perf, nominal_perf and
879 * lowest_nonlinear_perf members of @cpudata to be
882 * Returns 0 on success, non-zero value on failure.
884 static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
888 u32 highest_perf, max_freq;
889 u32 nominal_perf, nominal_freq;
890 u32 lowest_nonlinear_perf, lowest_nonlinear_freq;
891 u32 boost_ratio, lowest_nonlinear_ratio;
892 struct cppc_perf_caps cppc_perf;
894 ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
898 if (quirks && quirks->lowest_freq)
899 min_freq = quirks->lowest_freq * 1000;
901 min_freq = cppc_perf.lowest_freq * 1000;
903 if (quirks && quirks->nominal_freq)
904 nominal_freq = quirks->nominal_freq ;
906 nominal_freq = cppc_perf.nominal_freq;
908 nominal_perf = READ_ONCE(cpudata->nominal_perf);
910 highest_perf = READ_ONCE(cpudata->highest_perf);
911 boost_ratio = div_u64(highest_perf << SCHED_CAPACITY_SHIFT, nominal_perf);
912 max_freq = (nominal_freq * boost_ratio >> SCHED_CAPACITY_SHIFT) * 1000;
914 lowest_nonlinear_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
915 lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,
917 lowest_nonlinear_freq = (nominal_freq * lowest_nonlinear_ratio >> SCHED_CAPACITY_SHIFT) * 1000;
919 WRITE_ONCE(cpudata->min_freq, min_freq);
920 WRITE_ONCE(cpudata->lowest_nonlinear_freq, lowest_nonlinear_freq);
921 WRITE_ONCE(cpudata->nominal_freq, nominal_freq);
922 WRITE_ONCE(cpudata->max_freq, max_freq);
927 static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
929 int min_freq, max_freq, nominal_freq, ret;
931 struct amd_cpudata *cpudata;
934 * Resetting PERF_CTL_MSR will put the CPU in P0 frequency,
935 * which is ideal for initialization process.
937 amd_perf_ctl_reset(policy->cpu);
938 dev = get_cpu_device(policy->cpu);
942 cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
946 cpudata->cpu = policy->cpu;
948 amd_pstate_init_prefcore(cpudata);
950 ret = amd_pstate_init_perf(cpudata);
954 ret = amd_pstate_init_freq(cpudata);
958 min_freq = READ_ONCE(cpudata->min_freq);
959 max_freq = READ_ONCE(cpudata->max_freq);
960 nominal_freq = READ_ONCE(cpudata->nominal_freq);
962 if (min_freq <= 0 || max_freq <= 0 ||
963 nominal_freq <= 0 || min_freq > max_freq) {
965 "min_freq(%d) or max_freq(%d) or nominal_freq (%d) value is incorrect, check _CPC in ACPI tables\n",
966 min_freq, max_freq, nominal_freq);
971 policy->cpuinfo.transition_latency = amd_pstate_get_transition_latency(policy->cpu);
972 policy->transition_delay_us = amd_pstate_get_transition_delay_us(policy->cpu);
974 policy->min = min_freq;
975 policy->max = max_freq;
977 policy->cpuinfo.min_freq = min_freq;
978 policy->cpuinfo.max_freq = max_freq;
980 /* It will be updated by governor */
981 policy->cur = policy->cpuinfo.min_freq;
983 if (boot_cpu_has(X86_FEATURE_CPPC))
984 policy->fast_switch_possible = true;
986 ret = freq_qos_add_request(&policy->constraints, &cpudata->req[0],
987 FREQ_QOS_MIN, policy->cpuinfo.min_freq);
989 dev_err(dev, "Failed to add min-freq constraint (%d)\n", ret);
993 ret = freq_qos_add_request(&policy->constraints, &cpudata->req[1],
994 FREQ_QOS_MAX, policy->cpuinfo.max_freq);
996 dev_err(dev, "Failed to add max-freq constraint (%d)\n", ret);
1000 cpudata->max_limit_freq = max_freq;
1001 cpudata->min_limit_freq = min_freq;
1003 policy->driver_data = cpudata;
1005 amd_pstate_boost_init(cpudata);
1006 if (!current_pstate_driver->adjust_perf)
1007 current_pstate_driver->adjust_perf = amd_pstate_adjust_perf;
1012 freq_qos_remove_request(&cpudata->req[0]);
1018 static int amd_pstate_cpu_exit(struct cpufreq_policy *policy)
1020 struct amd_cpudata *cpudata = policy->driver_data;
1022 freq_qos_remove_request(&cpudata->req[1]);
1023 freq_qos_remove_request(&cpudata->req[0]);
1024 policy->fast_switch_possible = false;
1030 static int amd_pstate_cpu_resume(struct cpufreq_policy *policy)
1034 ret = amd_pstate_enable(true);
1036 pr_err("failed to enable amd-pstate during resume, return %d\n", ret);
1041 static int amd_pstate_cpu_suspend(struct cpufreq_policy *policy)
1045 ret = amd_pstate_enable(false);
1047 pr_err("failed to disable amd-pstate during suspend, return %d\n", ret);
1052 /* Sysfs attributes */
1055 * This frequency is to indicate the maximum hardware frequency.
1056 * If boost is not active but supported, the frequency will be larger than the
1059 static ssize_t show_amd_pstate_max_freq(struct cpufreq_policy *policy,
1063 struct amd_cpudata *cpudata = policy->driver_data;
1065 max_freq = READ_ONCE(cpudata->max_freq);
1069 return sysfs_emit(buf, "%u\n", max_freq);
1072 static ssize_t show_amd_pstate_lowest_nonlinear_freq(struct cpufreq_policy *policy,
1076 struct amd_cpudata *cpudata = policy->driver_data;
1078 freq = READ_ONCE(cpudata->lowest_nonlinear_freq);
1082 return sysfs_emit(buf, "%u\n", freq);
1086 * In some of ASICs, the highest_perf is not the one in the _CPC table, so we
1087 * need to expose it to sysfs.
1089 static ssize_t show_amd_pstate_highest_perf(struct cpufreq_policy *policy,
1093 struct amd_cpudata *cpudata = policy->driver_data;
1095 perf = READ_ONCE(cpudata->highest_perf);
1097 return sysfs_emit(buf, "%u\n", perf);
1100 static ssize_t show_amd_pstate_prefcore_ranking(struct cpufreq_policy *policy,
1104 struct amd_cpudata *cpudata = policy->driver_data;
1106 perf = READ_ONCE(cpudata->prefcore_ranking);
1108 return sysfs_emit(buf, "%u\n", perf);
1111 static ssize_t show_amd_pstate_hw_prefcore(struct cpufreq_policy *policy,
1115 struct amd_cpudata *cpudata = policy->driver_data;
1117 hw_prefcore = READ_ONCE(cpudata->hw_prefcore);
1119 return sysfs_emit(buf, "%s\n", str_enabled_disabled(hw_prefcore));
1122 static ssize_t show_energy_performance_available_preferences(
1123 struct cpufreq_policy *policy, char *buf)
1127 struct amd_cpudata *cpudata = policy->driver_data;
1129 if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
1130 return sysfs_emit_at(buf, offset, "%s\n",
1131 energy_perf_strings[EPP_INDEX_PERFORMANCE]);
1133 while (energy_perf_strings[i] != NULL)
1134 offset += sysfs_emit_at(buf, offset, "%s ", energy_perf_strings[i++]);
1136 offset += sysfs_emit_at(buf, offset, "\n");
1141 static ssize_t store_energy_performance_preference(
1142 struct cpufreq_policy *policy, const char *buf, size_t count)
1144 struct amd_cpudata *cpudata = policy->driver_data;
1145 char str_preference[21];
1148 ret = sscanf(buf, "%20s", str_preference);
1152 ret = match_string(energy_perf_strings, -1, str_preference);
1156 mutex_lock(&amd_pstate_limits_lock);
1157 ret = amd_pstate_set_energy_pref_index(cpudata, ret);
1158 mutex_unlock(&amd_pstate_limits_lock);
1160 return ret ?: count;
1163 static ssize_t show_energy_performance_preference(
1164 struct cpufreq_policy *policy, char *buf)
1166 struct amd_cpudata *cpudata = policy->driver_data;
1169 preference = amd_pstate_get_energy_pref_index(cpudata);
1173 return sysfs_emit(buf, "%s\n", energy_perf_strings[preference]);
1176 static void amd_pstate_driver_cleanup(void)
1178 amd_pstate_enable(false);
1179 cppc_state = AMD_PSTATE_DISABLE;
1180 current_pstate_driver = NULL;
1183 static int amd_pstate_register_driver(int mode)
1187 if (mode == AMD_PSTATE_PASSIVE || mode == AMD_PSTATE_GUIDED)
1188 current_pstate_driver = &amd_pstate_driver;
1189 else if (mode == AMD_PSTATE_ACTIVE)
1190 current_pstate_driver = &amd_pstate_epp_driver;
1195 ret = cpufreq_register_driver(current_pstate_driver);
1197 amd_pstate_driver_cleanup();
1203 static int amd_pstate_unregister_driver(int dummy)
1205 cpufreq_unregister_driver(current_pstate_driver);
1206 amd_pstate_driver_cleanup();
1210 static int amd_pstate_change_mode_without_dvr_change(int mode)
1216 if (boot_cpu_has(X86_FEATURE_CPPC) || cppc_state == AMD_PSTATE_ACTIVE)
1219 for_each_present_cpu(cpu) {
1220 cppc_set_auto_sel(cpu, (cppc_state == AMD_PSTATE_PASSIVE) ? 0 : 1);
1226 static int amd_pstate_change_driver_mode(int mode)
1230 ret = amd_pstate_unregister_driver(0);
1234 ret = amd_pstate_register_driver(mode);
1241 static cppc_mode_transition_fn mode_state_machine[AMD_PSTATE_MAX][AMD_PSTATE_MAX] = {
1242 [AMD_PSTATE_DISABLE] = {
1243 [AMD_PSTATE_DISABLE] = NULL,
1244 [AMD_PSTATE_PASSIVE] = amd_pstate_register_driver,
1245 [AMD_PSTATE_ACTIVE] = amd_pstate_register_driver,
1246 [AMD_PSTATE_GUIDED] = amd_pstate_register_driver,
1248 [AMD_PSTATE_PASSIVE] = {
1249 [AMD_PSTATE_DISABLE] = amd_pstate_unregister_driver,
1250 [AMD_PSTATE_PASSIVE] = NULL,
1251 [AMD_PSTATE_ACTIVE] = amd_pstate_change_driver_mode,
1252 [AMD_PSTATE_GUIDED] = amd_pstate_change_mode_without_dvr_change,
1254 [AMD_PSTATE_ACTIVE] = {
1255 [AMD_PSTATE_DISABLE] = amd_pstate_unregister_driver,
1256 [AMD_PSTATE_PASSIVE] = amd_pstate_change_driver_mode,
1257 [AMD_PSTATE_ACTIVE] = NULL,
1258 [AMD_PSTATE_GUIDED] = amd_pstate_change_driver_mode,
1260 [AMD_PSTATE_GUIDED] = {
1261 [AMD_PSTATE_DISABLE] = amd_pstate_unregister_driver,
1262 [AMD_PSTATE_PASSIVE] = amd_pstate_change_mode_without_dvr_change,
1263 [AMD_PSTATE_ACTIVE] = amd_pstate_change_driver_mode,
1264 [AMD_PSTATE_GUIDED] = NULL,
1268 static ssize_t amd_pstate_show_status(char *buf)
1270 if (!current_pstate_driver)
1271 return sysfs_emit(buf, "disable\n");
1273 return sysfs_emit(buf, "%s\n", amd_pstate_mode_string[cppc_state]);
1276 static int amd_pstate_update_status(const char *buf, size_t size)
1280 if (size > strlen("passive") || size < strlen("active"))
1283 mode_idx = get_mode_idx_from_str(buf, size);
1285 if (mode_idx < 0 || mode_idx >= AMD_PSTATE_MAX)
1288 if (mode_state_machine[cppc_state][mode_idx])
1289 return mode_state_machine[cppc_state][mode_idx](mode_idx);
1294 static ssize_t status_show(struct device *dev,
1295 struct device_attribute *attr, char *buf)
1299 mutex_lock(&amd_pstate_driver_lock);
1300 ret = amd_pstate_show_status(buf);
1301 mutex_unlock(&amd_pstate_driver_lock);
1306 static ssize_t status_store(struct device *a, struct device_attribute *b,
1307 const char *buf, size_t count)
1309 char *p = memchr(buf, '\n', count);
1312 mutex_lock(&amd_pstate_driver_lock);
1313 ret = amd_pstate_update_status(buf, p ? p - buf : count);
1314 mutex_unlock(&amd_pstate_driver_lock);
1316 return ret < 0 ? ret : count;
1319 static ssize_t prefcore_show(struct device *dev,
1320 struct device_attribute *attr, char *buf)
1322 return sysfs_emit(buf, "%s\n", str_enabled_disabled(amd_pstate_prefcore));
1325 cpufreq_freq_attr_ro(amd_pstate_max_freq);
1326 cpufreq_freq_attr_ro(amd_pstate_lowest_nonlinear_freq);
1328 cpufreq_freq_attr_ro(amd_pstate_highest_perf);
1329 cpufreq_freq_attr_ro(amd_pstate_prefcore_ranking);
1330 cpufreq_freq_attr_ro(amd_pstate_hw_prefcore);
1331 cpufreq_freq_attr_rw(energy_performance_preference);
1332 cpufreq_freq_attr_ro(energy_performance_available_preferences);
1333 static DEVICE_ATTR_RW(status);
1334 static DEVICE_ATTR_RO(prefcore);
1336 static struct freq_attr *amd_pstate_attr[] = {
1337 &amd_pstate_max_freq,
1338 &amd_pstate_lowest_nonlinear_freq,
1339 &amd_pstate_highest_perf,
1340 &amd_pstate_prefcore_ranking,
1341 &amd_pstate_hw_prefcore,
1345 static struct freq_attr *amd_pstate_epp_attr[] = {
1346 &amd_pstate_max_freq,
1347 &amd_pstate_lowest_nonlinear_freq,
1348 &amd_pstate_highest_perf,
1349 &amd_pstate_prefcore_ranking,
1350 &amd_pstate_hw_prefcore,
1351 &energy_performance_preference,
1352 &energy_performance_available_preferences,
1356 static struct attribute *pstate_global_attributes[] = {
1357 &dev_attr_status.attr,
1358 &dev_attr_prefcore.attr,
1362 static const struct attribute_group amd_pstate_global_attr_group = {
1363 .name = "amd_pstate",
1364 .attrs = pstate_global_attributes,
1367 static bool amd_pstate_acpi_pm_profile_server(void)
1369 switch (acpi_gbl_FADT.preferred_profile) {
1370 case PM_ENTERPRISE_SERVER:
1371 case PM_SOHO_SERVER:
1372 case PM_PERFORMANCE_SERVER:
1378 static bool amd_pstate_acpi_pm_profile_undefined(void)
1380 if (acpi_gbl_FADT.preferred_profile == PM_UNSPECIFIED)
1382 if (acpi_gbl_FADT.preferred_profile >= NR_PM_PROFILES)
1387 static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
1389 int min_freq, max_freq, nominal_freq, ret;
1390 struct amd_cpudata *cpudata;
1395 * Resetting PERF_CTL_MSR will put the CPU in P0 frequency,
1396 * which is ideal for initialization process.
1398 amd_perf_ctl_reset(policy->cpu);
1399 dev = get_cpu_device(policy->cpu);
1403 cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
1407 cpudata->cpu = policy->cpu;
1408 cpudata->epp_policy = 0;
1410 amd_pstate_init_prefcore(cpudata);
1412 ret = amd_pstate_init_perf(cpudata);
1416 ret = amd_pstate_init_freq(cpudata);
1420 min_freq = READ_ONCE(cpudata->min_freq);
1421 max_freq = READ_ONCE(cpudata->max_freq);
1422 nominal_freq = READ_ONCE(cpudata->nominal_freq);
1423 if (min_freq <= 0 || max_freq <= 0 ||
1424 nominal_freq <= 0 || min_freq > max_freq) {
1426 "min_freq(%d) or max_freq(%d) or nominal_freq(%d) value is incorrect, check _CPC in ACPI tables\n",
1427 min_freq, max_freq, nominal_freq);
1432 policy->cpuinfo.min_freq = min_freq;
1433 policy->cpuinfo.max_freq = max_freq;
1434 /* It will be updated by governor */
1435 policy->cur = policy->cpuinfo.min_freq;
1437 policy->driver_data = cpudata;
1439 cpudata->epp_cached = amd_pstate_get_epp(cpudata, 0);
1441 policy->min = policy->cpuinfo.min_freq;
1442 policy->max = policy->cpuinfo.max_freq;
1445 * Set the policy to provide a valid fallback value in case
1446 * the default cpufreq governor is neither powersave nor performance.
1448 if (amd_pstate_acpi_pm_profile_server() ||
1449 amd_pstate_acpi_pm_profile_undefined())
1450 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
1452 policy->policy = CPUFREQ_POLICY_POWERSAVE;
1454 if (boot_cpu_has(X86_FEATURE_CPPC)) {
1455 ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, &value);
1458 WRITE_ONCE(cpudata->cppc_req_cached, value);
1460 ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_CAP1, &value);
1463 WRITE_ONCE(cpudata->cppc_cap1_cached, value);
1465 amd_pstate_boost_init(cpudata);
1474 static int amd_pstate_epp_cpu_exit(struct cpufreq_policy *policy)
1476 struct amd_cpudata *cpudata = policy->driver_data;
1480 policy->driver_data = NULL;
1483 pr_debug("CPU %d exiting\n", policy->cpu);
1487 static void amd_pstate_epp_update_limit(struct cpufreq_policy *policy)
1489 struct amd_cpudata *cpudata = policy->driver_data;
1490 u32 max_perf, min_perf, min_limit_perf, max_limit_perf;
1494 max_perf = READ_ONCE(cpudata->highest_perf);
1495 min_perf = READ_ONCE(cpudata->lowest_perf);
1496 max_limit_perf = div_u64(policy->max * cpudata->highest_perf, cpudata->max_freq);
1497 min_limit_perf = div_u64(policy->min * cpudata->highest_perf, cpudata->max_freq);
1499 if (min_limit_perf < min_perf)
1500 min_limit_perf = min_perf;
1502 if (max_limit_perf < min_limit_perf)
1503 max_limit_perf = min_limit_perf;
1505 WRITE_ONCE(cpudata->max_limit_perf, max_limit_perf);
1506 WRITE_ONCE(cpudata->min_limit_perf, min_limit_perf);
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);
1514 if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
1515 min_perf = max_perf;
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);
1521 value &= ~AMD_CPPC_MAX_PERF(~0L);
1522 value |= AMD_CPPC_MAX_PERF(max_perf);
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);
1528 cpudata->epp_policy = cpudata->policy;
1530 /* Get BIOS pre-defined epp value */
1531 epp = amd_pstate_get_epp(cpudata, value);
1534 * This return value can only be negative for shared_memory
1535 * systems where EPP register read/write not supported.
1540 if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
1543 /* Set initial EPP value */
1544 if (boot_cpu_has(X86_FEATURE_CPPC)) {
1545 value &= ~GENMASK_ULL(31, 24);
1546 value |= (u64)epp << 24;
1549 WRITE_ONCE(cpudata->cppc_req_cached, value);
1550 amd_pstate_set_epp(cpudata, epp);
1553 static int amd_pstate_epp_set_policy(struct cpufreq_policy *policy)
1555 struct amd_cpudata *cpudata = policy->driver_data;
1557 if (!policy->cpuinfo.max_freq)
1560 pr_debug("set_policy: cpuinfo.max %u policy->max %u\n",
1561 policy->cpuinfo.max_freq, policy->max);
1563 cpudata->policy = policy->policy;
1565 amd_pstate_epp_update_limit(policy);
1570 static void amd_pstate_epp_reenable(struct amd_cpudata *cpudata)
1572 struct cppc_perf_ctrls perf_ctrls;
1573 u64 value, max_perf;
1576 ret = amd_pstate_enable(true);
1578 pr_err("failed to enable amd pstate during resume, return %d\n", ret);
1580 value = READ_ONCE(cpudata->cppc_req_cached);
1581 max_perf = READ_ONCE(cpudata->highest_perf);
1583 if (boot_cpu_has(X86_FEATURE_CPPC)) {
1584 wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
1586 perf_ctrls.max_perf = max_perf;
1587 perf_ctrls.energy_perf = AMD_CPPC_ENERGY_PERF_PREF(cpudata->epp_cached);
1588 cppc_set_perf(cpudata->cpu, &perf_ctrls);
1592 static int amd_pstate_epp_cpu_online(struct cpufreq_policy *policy)
1594 struct amd_cpudata *cpudata = policy->driver_data;
1596 pr_debug("AMD CPU Core %d going online\n", cpudata->cpu);
1598 if (cppc_state == AMD_PSTATE_ACTIVE) {
1599 amd_pstate_epp_reenable(cpudata);
1600 cpudata->suspended = false;
1606 static void amd_pstate_epp_offline(struct cpufreq_policy *policy)
1608 struct amd_cpudata *cpudata = policy->driver_data;
1609 struct cppc_perf_ctrls perf_ctrls;
1613 min_perf = READ_ONCE(cpudata->lowest_perf);
1614 value = READ_ONCE(cpudata->cppc_req_cached);
1616 mutex_lock(&amd_pstate_limits_lock);
1617 if (boot_cpu_has(X86_FEATURE_CPPC)) {
1618 cpudata->epp_policy = CPUFREQ_POLICY_UNKNOWN;
1620 /* Set max perf same as min perf */
1621 value &= ~AMD_CPPC_MAX_PERF(~0L);
1622 value |= AMD_CPPC_MAX_PERF(min_perf);
1623 value &= ~AMD_CPPC_MIN_PERF(~0L);
1624 value |= AMD_CPPC_MIN_PERF(min_perf);
1625 wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
1627 perf_ctrls.desired_perf = 0;
1628 perf_ctrls.max_perf = min_perf;
1629 perf_ctrls.energy_perf = AMD_CPPC_ENERGY_PERF_PREF(HWP_EPP_BALANCE_POWERSAVE);
1630 cppc_set_perf(cpudata->cpu, &perf_ctrls);
1632 mutex_unlock(&amd_pstate_limits_lock);
1635 static int amd_pstate_epp_cpu_offline(struct cpufreq_policy *policy)
1637 struct amd_cpudata *cpudata = policy->driver_data;
1639 pr_debug("AMD CPU Core %d going offline\n", cpudata->cpu);
1641 if (cpudata->suspended)
1644 if (cppc_state == AMD_PSTATE_ACTIVE)
1645 amd_pstate_epp_offline(policy);
1650 static int amd_pstate_epp_verify_policy(struct cpufreq_policy_data *policy)
1652 cpufreq_verify_within_cpu_limits(policy);
1653 pr_debug("policy_max =%d, policy_min=%d\n", policy->max, policy->min);
1657 static int amd_pstate_epp_suspend(struct cpufreq_policy *policy)
1659 struct amd_cpudata *cpudata = policy->driver_data;
1662 /* avoid suspending when EPP is not enabled */
1663 if (cppc_state != AMD_PSTATE_ACTIVE)
1666 /* set this flag to avoid setting core offline*/
1667 cpudata->suspended = true;
1669 /* disable CPPC in lowlevel firmware */
1670 ret = amd_pstate_enable(false);
1672 pr_err("failed to suspend, return %d\n", ret);
1677 static int amd_pstate_epp_resume(struct cpufreq_policy *policy)
1679 struct amd_cpudata *cpudata = policy->driver_data;
1681 if (cpudata->suspended) {
1682 mutex_lock(&amd_pstate_limits_lock);
1684 /* enable amd pstate from suspend state*/
1685 amd_pstate_epp_reenable(cpudata);
1687 mutex_unlock(&amd_pstate_limits_lock);
1689 cpudata->suspended = false;
1695 static struct cpufreq_driver amd_pstate_driver = {
1696 .flags = CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_UPDATE_LIMITS,
1697 .verify = amd_pstate_verify,
1698 .target = amd_pstate_target,
1699 .fast_switch = amd_pstate_fast_switch,
1700 .init = amd_pstate_cpu_init,
1701 .exit = amd_pstate_cpu_exit,
1702 .suspend = amd_pstate_cpu_suspend,
1703 .resume = amd_pstate_cpu_resume,
1704 .set_boost = amd_pstate_set_boost,
1705 .update_limits = amd_pstate_update_limits,
1706 .name = "amd-pstate",
1707 .attr = amd_pstate_attr,
1710 static struct cpufreq_driver amd_pstate_epp_driver = {
1711 .flags = CPUFREQ_CONST_LOOPS,
1712 .verify = amd_pstate_epp_verify_policy,
1713 .setpolicy = amd_pstate_epp_set_policy,
1714 .init = amd_pstate_epp_cpu_init,
1715 .exit = amd_pstate_epp_cpu_exit,
1716 .offline = amd_pstate_epp_cpu_offline,
1717 .online = amd_pstate_epp_cpu_online,
1718 .suspend = amd_pstate_epp_suspend,
1719 .resume = amd_pstate_epp_resume,
1720 .update_limits = amd_pstate_update_limits,
1721 .name = "amd-pstate-epp",
1722 .attr = amd_pstate_epp_attr,
1725 static int __init amd_pstate_set_driver(int mode_idx)
1727 if (mode_idx >= AMD_PSTATE_DISABLE && mode_idx < AMD_PSTATE_MAX) {
1728 cppc_state = mode_idx;
1729 if (cppc_state == AMD_PSTATE_DISABLE)
1730 pr_info("driver is explicitly disabled\n");
1732 if (cppc_state == AMD_PSTATE_ACTIVE)
1733 current_pstate_driver = &amd_pstate_epp_driver;
1735 if (cppc_state == AMD_PSTATE_PASSIVE || cppc_state == AMD_PSTATE_GUIDED)
1736 current_pstate_driver = &amd_pstate_driver;
1744 static int __init amd_pstate_init(void)
1746 struct device *dev_root;
1749 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
1752 if (!acpi_cpc_valid()) {
1753 pr_warn_once("the _CPC object is not present in SBIOS or ACPI disabled\n");
1757 /* don't keep reloading if cpufreq_driver exists */
1758 if (cpufreq_get_current_driver())
1763 /* check if this machine need CPPC quirks */
1764 dmi_check_system(amd_pstate_quirks_table);
1766 switch (cppc_state) {
1767 case AMD_PSTATE_UNDEFINED:
1768 /* Disable on the following configs by default:
1769 * 1. Undefined platforms
1770 * 2. Server platforms
1771 * 3. Shared memory designs
1773 if (amd_pstate_acpi_pm_profile_undefined() ||
1774 amd_pstate_acpi_pm_profile_server() ||
1775 !boot_cpu_has(X86_FEATURE_CPPC)) {
1776 pr_info("driver load is disabled, boot with specific mode to enable this\n");
1779 ret = amd_pstate_set_driver(CONFIG_X86_AMD_PSTATE_DEFAULT_MODE);
1783 case AMD_PSTATE_DISABLE:
1785 case AMD_PSTATE_PASSIVE:
1786 case AMD_PSTATE_ACTIVE:
1787 case AMD_PSTATE_GUIDED:
1793 /* capability check */
1794 if (boot_cpu_has(X86_FEATURE_CPPC)) {
1795 pr_debug("AMD CPPC MSR based functionality is supported\n");
1796 if (cppc_state != AMD_PSTATE_ACTIVE)
1797 current_pstate_driver->adjust_perf = amd_pstate_adjust_perf;
1799 pr_debug("AMD CPPC shared memory based functionality is supported\n");
1800 static_call_update(amd_pstate_enable, cppc_enable);
1801 static_call_update(amd_pstate_init_perf, cppc_init_perf);
1802 static_call_update(amd_pstate_update_perf, cppc_update_perf);
1805 /* enable amd pstate feature */
1806 ret = amd_pstate_enable(true);
1808 pr_err("failed to enable with return %d\n", ret);
1812 ret = cpufreq_register_driver(current_pstate_driver);
1814 pr_err("failed to register with return %d\n", ret);
1816 dev_root = bus_get_dev_root(&cpu_subsys);
1818 ret = sysfs_create_group(&dev_root->kobj, &amd_pstate_global_attr_group);
1819 put_device(dev_root);
1821 pr_err("sysfs attribute export failed with error %d.\n", ret);
1822 goto global_attr_free;
1829 cpufreq_unregister_driver(current_pstate_driver);
1832 device_initcall(amd_pstate_init);
1834 static int __init amd_pstate_param(char *str)
1843 mode_idx = get_mode_idx_from_str(str, size);
1845 return amd_pstate_set_driver(mode_idx);
1848 static int __init amd_prefcore_param(char *str)
1850 if (!strcmp(str, "disable"))
1851 amd_pstate_prefcore = false;
1856 early_param("amd_pstate", amd_pstate_param);
1857 early_param("amd_prefcore", amd_prefcore_param);
1859 MODULE_AUTHOR("Huang Rui <ray.huang@amd.com>");
1860 MODULE_DESCRIPTION("AMD Processor P-state Frequency Driver");