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.
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 AMD_PSTATE_FAST_CPPC_TRANSITION_DELAY 600
56 #define AMD_CPPC_EPP_PERFORMANCE 0x00
57 #define AMD_CPPC_EPP_BALANCE_PERFORMANCE 0x80
58 #define AMD_CPPC_EPP_BALANCE_POWERSAVE 0xBF
59 #define AMD_CPPC_EPP_POWERSAVE 0xFF
61 static const char * const amd_pstate_mode_string[] = {
62 [AMD_PSTATE_UNDEFINED] = "undefined",
63 [AMD_PSTATE_DISABLE] = "disable",
64 [AMD_PSTATE_PASSIVE] = "passive",
65 [AMD_PSTATE_ACTIVE] = "active",
66 [AMD_PSTATE_GUIDED] = "guided",
70 const char *amd_pstate_get_mode_string(enum amd_pstate_mode mode)
72 if (mode < 0 || mode >= AMD_PSTATE_MAX)
74 return amd_pstate_mode_string[mode];
76 EXPORT_SYMBOL_GPL(amd_pstate_get_mode_string);
83 static struct cpufreq_driver *current_pstate_driver;
84 static struct cpufreq_driver amd_pstate_driver;
85 static struct cpufreq_driver amd_pstate_epp_driver;
86 static int cppc_state = AMD_PSTATE_UNDEFINED;
87 static bool cppc_enabled;
88 static bool amd_pstate_prefcore = true;
89 static struct quirk_entry *quirks;
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[]
100 *-------------------------------------
103 * 2 balance_performance
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,
115 static const char * const energy_perf_strings[] = {
116 [EPP_INDEX_DEFAULT] = "default",
117 [EPP_INDEX_PERFORMANCE] = "performance",
118 [EPP_INDEX_BALANCE_PERFORMANCE] = "balance_performance",
119 [EPP_INDEX_BALANCE_POWERSAVE] = "balance_power",
120 [EPP_INDEX_POWERSAVE] = "power",
124 static unsigned int epp_values[] = {
125 [EPP_INDEX_DEFAULT] = 0,
126 [EPP_INDEX_PERFORMANCE] = AMD_CPPC_EPP_PERFORMANCE,
127 [EPP_INDEX_BALANCE_PERFORMANCE] = AMD_CPPC_EPP_BALANCE_PERFORMANCE,
128 [EPP_INDEX_BALANCE_POWERSAVE] = AMD_CPPC_EPP_BALANCE_POWERSAVE,
129 [EPP_INDEX_POWERSAVE] = AMD_CPPC_EPP_POWERSAVE,
132 typedef int (*cppc_mode_transition_fn)(int);
134 static struct quirk_entry quirk_amd_7k62 = {
135 .nominal_freq = 2600,
139 static int __init dmi_matched_7k62_bios_bug(const struct dmi_system_id *dmi)
142 * match the broken bios for family 17h processor support CPPC V2
143 * broken BIOS lack of nominal_freq and lowest_freq capabilities
144 * definition in ACPI tables
146 if (cpu_feature_enabled(X86_FEATURE_ZEN2)) {
147 quirks = dmi->driver_data;
148 pr_info("Overriding nominal and lowest frequencies for %s\n", dmi->ident);
155 static const struct dmi_system_id amd_pstate_quirks_table[] __initconst = {
157 .callback = dmi_matched_7k62_bios_bug,
158 .ident = "AMD EPYC 7K62",
160 DMI_MATCH(DMI_BIOS_VERSION, "5.14"),
161 DMI_MATCH(DMI_BIOS_RELEASE, "12/12/2019"),
163 .driver_data = &quirk_amd_7k62,
167 MODULE_DEVICE_TABLE(dmi, amd_pstate_quirks_table);
169 static inline int get_mode_idx_from_str(const char *str, size_t size)
173 for (i=0; i < AMD_PSTATE_MAX; i++) {
174 if (!strncmp(str, amd_pstate_mode_string[i], size))
180 static DEFINE_MUTEX(amd_pstate_limits_lock);
181 static DEFINE_MUTEX(amd_pstate_driver_lock);
183 static s16 amd_pstate_get_epp(struct amd_cpudata *cpudata, u64 cppc_req_cached)
188 if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
189 if (!cppc_req_cached) {
190 epp = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,
195 epp = (cppc_req_cached >> 24) & 0xFF;
197 ret = cppc_get_epp_perf(cpudata->cpu, &epp);
199 pr_debug("Could not retrieve energy perf value (%d)\n", ret);
204 return (s16)(epp & 0xff);
207 static int amd_pstate_get_energy_pref_index(struct amd_cpudata *cpudata)
212 epp = amd_pstate_get_epp(cpudata, 0);
217 case AMD_CPPC_EPP_PERFORMANCE:
218 index = EPP_INDEX_PERFORMANCE;
220 case AMD_CPPC_EPP_BALANCE_PERFORMANCE:
221 index = EPP_INDEX_BALANCE_PERFORMANCE;
223 case AMD_CPPC_EPP_BALANCE_POWERSAVE:
224 index = EPP_INDEX_BALANCE_POWERSAVE;
226 case AMD_CPPC_EPP_POWERSAVE:
227 index = EPP_INDEX_POWERSAVE;
236 static void pstate_update_perf(struct amd_cpudata *cpudata, u32 min_perf,
237 u32 des_perf, u32 max_perf, bool fast_switch)
240 wrmsrl(MSR_AMD_CPPC_REQ, READ_ONCE(cpudata->cppc_req_cached));
242 wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,
243 READ_ONCE(cpudata->cppc_req_cached));
246 DEFINE_STATIC_CALL(amd_pstate_update_perf, pstate_update_perf);
248 static inline void amd_pstate_update_perf(struct amd_cpudata *cpudata,
249 u32 min_perf, u32 des_perf,
250 u32 max_perf, bool fast_switch)
252 static_call(amd_pstate_update_perf)(cpudata, min_perf, des_perf,
253 max_perf, fast_switch);
256 static int amd_pstate_set_epp(struct amd_cpudata *cpudata, u32 epp)
259 struct cppc_perf_ctrls perf_ctrls;
261 if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
262 u64 value = READ_ONCE(cpudata->cppc_req_cached);
264 value &= ~GENMASK_ULL(31, 24);
265 value |= (u64)epp << 24;
266 WRITE_ONCE(cpudata->cppc_req_cached, value);
268 ret = wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
270 cpudata->epp_cached = epp;
272 amd_pstate_update_perf(cpudata, cpudata->min_limit_perf, 0U,
273 cpudata->max_limit_perf, false);
275 perf_ctrls.energy_perf = epp;
276 ret = cppc_set_epp_perf(cpudata->cpu, &perf_ctrls, 1);
278 pr_debug("failed to set energy perf value (%d)\n", ret);
281 cpudata->epp_cached = epp;
287 static int amd_pstate_set_energy_pref_index(struct amd_cpudata *cpudata,
294 epp = cpudata->epp_default;
297 epp = epp_values[pref_index];
299 if (epp > 0 && cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) {
300 pr_debug("EPP cannot be set under performance policy\n");
304 ret = amd_pstate_set_epp(cpudata, epp);
309 static inline int pstate_enable(bool enable)
312 unsigned long logical_proc_id_mask = 0;
314 if (enable == cppc_enabled)
317 for_each_present_cpu(cpu) {
318 unsigned long logical_id = topology_logical_package_id(cpu);
320 if (test_bit(logical_id, &logical_proc_id_mask))
323 set_bit(logical_id, &logical_proc_id_mask);
325 ret = wrmsrl_safe_on_cpu(cpu, MSR_AMD_CPPC_ENABLE,
331 cppc_enabled = enable;
335 static int cppc_enable(bool enable)
338 struct cppc_perf_ctrls perf_ctrls;
340 if (enable == cppc_enabled)
343 for_each_present_cpu(cpu) {
344 ret = cppc_set_enable(cpu, enable);
348 /* Enable autonomous mode for EPP */
349 if (cppc_state == AMD_PSTATE_ACTIVE) {
350 /* Set desired perf as zero to allow EPP firmware control */
351 perf_ctrls.desired_perf = 0;
352 ret = cppc_set_perf(cpu, &perf_ctrls);
358 cppc_enabled = enable;
362 DEFINE_STATIC_CALL(amd_pstate_enable, pstate_enable);
364 static inline int amd_pstate_enable(bool enable)
366 return static_call(amd_pstate_enable)(enable);
369 static int pstate_init_perf(struct amd_cpudata *cpudata)
373 int ret = rdmsrl_safe_on_cpu(cpudata->cpu, MSR_AMD_CPPC_CAP1,
378 WRITE_ONCE(cpudata->highest_perf, AMD_CPPC_HIGHEST_PERF(cap1));
379 WRITE_ONCE(cpudata->max_limit_perf, AMD_CPPC_HIGHEST_PERF(cap1));
380 WRITE_ONCE(cpudata->nominal_perf, AMD_CPPC_NOMINAL_PERF(cap1));
381 WRITE_ONCE(cpudata->lowest_nonlinear_perf, AMD_CPPC_LOWNONLIN_PERF(cap1));
382 WRITE_ONCE(cpudata->lowest_perf, AMD_CPPC_LOWEST_PERF(cap1));
383 WRITE_ONCE(cpudata->prefcore_ranking, AMD_CPPC_HIGHEST_PERF(cap1));
384 WRITE_ONCE(cpudata->min_limit_perf, AMD_CPPC_LOWEST_PERF(cap1));
388 static int cppc_init_perf(struct amd_cpudata *cpudata)
390 struct cppc_perf_caps cppc_perf;
392 int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
396 WRITE_ONCE(cpudata->highest_perf, cppc_perf.highest_perf);
397 WRITE_ONCE(cpudata->max_limit_perf, cppc_perf.highest_perf);
398 WRITE_ONCE(cpudata->nominal_perf, cppc_perf.nominal_perf);
399 WRITE_ONCE(cpudata->lowest_nonlinear_perf,
400 cppc_perf.lowest_nonlinear_perf);
401 WRITE_ONCE(cpudata->lowest_perf, cppc_perf.lowest_perf);
402 WRITE_ONCE(cpudata->prefcore_ranking, cppc_perf.highest_perf);
403 WRITE_ONCE(cpudata->min_limit_perf, cppc_perf.lowest_perf);
405 if (cppc_state == AMD_PSTATE_ACTIVE)
408 ret = cppc_get_auto_sel_caps(cpudata->cpu, &cppc_perf);
410 pr_warn("failed to get auto_sel, ret: %d\n", ret);
414 ret = cppc_set_auto_sel(cpudata->cpu,
415 (cppc_state == AMD_PSTATE_PASSIVE) ? 0 : 1);
418 pr_warn("failed to set auto_sel, ret: %d\n", ret);
423 DEFINE_STATIC_CALL(amd_pstate_init_perf, pstate_init_perf);
425 static inline int amd_pstate_init_perf(struct amd_cpudata *cpudata)
427 return static_call(amd_pstate_init_perf)(cpudata);
430 static void cppc_update_perf(struct amd_cpudata *cpudata,
431 u32 min_perf, u32 des_perf,
432 u32 max_perf, bool fast_switch)
434 struct cppc_perf_ctrls perf_ctrls;
436 perf_ctrls.max_perf = max_perf;
437 perf_ctrls.min_perf = min_perf;
438 perf_ctrls.desired_perf = des_perf;
440 cppc_set_perf(cpudata->cpu, &perf_ctrls);
443 static inline bool amd_pstate_sample(struct amd_cpudata *cpudata)
445 u64 aperf, mperf, tsc;
448 local_irq_save(flags);
449 rdmsrl(MSR_IA32_APERF, aperf);
450 rdmsrl(MSR_IA32_MPERF, mperf);
453 if (cpudata->prev.mperf == mperf || cpudata->prev.tsc == tsc) {
454 local_irq_restore(flags);
458 local_irq_restore(flags);
460 cpudata->cur.aperf = aperf;
461 cpudata->cur.mperf = mperf;
462 cpudata->cur.tsc = tsc;
463 cpudata->cur.aperf -= cpudata->prev.aperf;
464 cpudata->cur.mperf -= cpudata->prev.mperf;
465 cpudata->cur.tsc -= cpudata->prev.tsc;
467 cpudata->prev.aperf = aperf;
468 cpudata->prev.mperf = mperf;
469 cpudata->prev.tsc = tsc;
471 cpudata->freq = div64_u64((cpudata->cur.aperf * cpu_khz), cpudata->cur.mperf);
476 static void amd_pstate_update(struct amd_cpudata *cpudata, u32 min_perf,
477 u32 des_perf, u32 max_perf, bool fast_switch, int gov_flags)
479 unsigned long max_freq;
480 struct cpufreq_policy *policy = cpufreq_cpu_get(cpudata->cpu);
481 u64 prev = READ_ONCE(cpudata->cppc_req_cached);
482 u32 nominal_perf = READ_ONCE(cpudata->nominal_perf);
485 min_perf = clamp_t(unsigned long, min_perf, cpudata->min_limit_perf,
486 cpudata->max_limit_perf);
487 max_perf = clamp_t(unsigned long, max_perf, cpudata->min_limit_perf,
488 cpudata->max_limit_perf);
489 des_perf = clamp_t(unsigned long, des_perf, min_perf, max_perf);
491 max_freq = READ_ONCE(cpudata->max_limit_freq);
492 policy->cur = div_u64(des_perf * max_freq, max_perf);
494 if ((cppc_state == AMD_PSTATE_GUIDED) && (gov_flags & CPUFREQ_GOV_DYNAMIC_SWITCHING)) {
499 value &= ~AMD_CPPC_MIN_PERF(~0L);
500 value |= AMD_CPPC_MIN_PERF(min_perf);
502 value &= ~AMD_CPPC_DES_PERF(~0L);
503 value |= AMD_CPPC_DES_PERF(des_perf);
505 /* limit the max perf when core performance boost feature is disabled */
506 if (!cpudata->boost_supported)
507 max_perf = min_t(unsigned long, nominal_perf, max_perf);
509 value &= ~AMD_CPPC_MAX_PERF(~0L);
510 value |= AMD_CPPC_MAX_PERF(max_perf);
512 if (trace_amd_pstate_perf_enabled() && amd_pstate_sample(cpudata)) {
513 trace_amd_pstate_perf(min_perf, des_perf, max_perf, cpudata->freq,
514 cpudata->cur.mperf, cpudata->cur.aperf, cpudata->cur.tsc,
515 cpudata->cpu, (value != prev), fast_switch);
519 goto cpufreq_policy_put;
521 WRITE_ONCE(cpudata->cppc_req_cached, value);
523 amd_pstate_update_perf(cpudata, min_perf, des_perf,
524 max_perf, fast_switch);
527 cpufreq_cpu_put(policy);
530 static int amd_pstate_verify(struct cpufreq_policy_data *policy)
532 cpufreq_verify_within_cpu_limits(policy);
537 static int amd_pstate_update_min_max_limit(struct cpufreq_policy *policy)
539 u32 max_limit_perf, min_limit_perf, lowest_perf;
540 struct amd_cpudata *cpudata = policy->driver_data;
542 max_limit_perf = div_u64(policy->max * cpudata->highest_perf, cpudata->max_freq);
543 min_limit_perf = div_u64(policy->min * cpudata->highest_perf, cpudata->max_freq);
545 lowest_perf = READ_ONCE(cpudata->lowest_perf);
546 if (min_limit_perf < lowest_perf)
547 min_limit_perf = lowest_perf;
549 if (max_limit_perf < min_limit_perf)
550 max_limit_perf = min_limit_perf;
552 WRITE_ONCE(cpudata->max_limit_perf, max_limit_perf);
553 WRITE_ONCE(cpudata->min_limit_perf, min_limit_perf);
554 WRITE_ONCE(cpudata->max_limit_freq, policy->max);
555 WRITE_ONCE(cpudata->min_limit_freq, policy->min);
560 static int amd_pstate_update_freq(struct cpufreq_policy *policy,
561 unsigned int target_freq, bool fast_switch)
563 struct cpufreq_freqs freqs;
564 struct amd_cpudata *cpudata = policy->driver_data;
565 unsigned long max_perf, min_perf, des_perf, cap_perf;
567 if (!cpudata->max_freq)
570 if (policy->min != cpudata->min_limit_freq || policy->max != cpudata->max_limit_freq)
571 amd_pstate_update_min_max_limit(policy);
573 cap_perf = READ_ONCE(cpudata->highest_perf);
574 min_perf = READ_ONCE(cpudata->lowest_perf);
577 freqs.old = policy->cur;
578 freqs.new = target_freq;
580 des_perf = DIV_ROUND_CLOSEST(target_freq * cap_perf,
583 WARN_ON(fast_switch && !policy->fast_switch_enabled);
585 * If fast_switch is desired, then there aren't any registered
586 * transition notifiers. See comment for
587 * cpufreq_enable_fast_switch().
590 cpufreq_freq_transition_begin(policy, &freqs);
592 amd_pstate_update(cpudata, min_perf, des_perf,
593 max_perf, fast_switch, policy->governor->flags);
596 cpufreq_freq_transition_end(policy, &freqs, false);
601 static int amd_pstate_target(struct cpufreq_policy *policy,
602 unsigned int target_freq,
603 unsigned int relation)
605 return amd_pstate_update_freq(policy, target_freq, false);
608 static unsigned int amd_pstate_fast_switch(struct cpufreq_policy *policy,
609 unsigned int target_freq)
611 if (!amd_pstate_update_freq(policy, target_freq, true))
616 static void amd_pstate_adjust_perf(unsigned int cpu,
617 unsigned long _min_perf,
618 unsigned long target_perf,
619 unsigned long capacity)
621 unsigned long max_perf, min_perf, des_perf,
622 cap_perf, lowest_nonlinear_perf;
623 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
624 struct amd_cpudata *cpudata;
629 cpudata = policy->driver_data;
631 if (policy->min != cpudata->min_limit_freq || policy->max != cpudata->max_limit_freq)
632 amd_pstate_update_min_max_limit(policy);
635 cap_perf = READ_ONCE(cpudata->highest_perf);
636 lowest_nonlinear_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
639 if (target_perf < capacity)
640 des_perf = DIV_ROUND_UP(cap_perf * target_perf, capacity);
642 min_perf = READ_ONCE(cpudata->lowest_perf);
643 if (_min_perf < capacity)
644 min_perf = DIV_ROUND_UP(cap_perf * _min_perf, capacity);
646 if (min_perf < lowest_nonlinear_perf)
647 min_perf = lowest_nonlinear_perf;
650 if (max_perf < min_perf)
653 des_perf = clamp_t(unsigned long, des_perf, min_perf, max_perf);
655 amd_pstate_update(cpudata, min_perf, des_perf, max_perf, true,
656 policy->governor->flags);
657 cpufreq_cpu_put(policy);
660 static int amd_pstate_cpu_boost_update(struct cpufreq_policy *policy, bool on)
662 struct amd_cpudata *cpudata = policy->driver_data;
663 struct cppc_perf_ctrls perf_ctrls;
664 u32 highest_perf, nominal_perf, nominal_freq, max_freq;
667 highest_perf = READ_ONCE(cpudata->highest_perf);
668 nominal_perf = READ_ONCE(cpudata->nominal_perf);
669 nominal_freq = READ_ONCE(cpudata->nominal_freq);
670 max_freq = READ_ONCE(cpudata->max_freq);
672 if (boot_cpu_has(X86_FEATURE_CPPC)) {
673 u64 value = READ_ONCE(cpudata->cppc_req_cached);
675 value &= ~GENMASK_ULL(7, 0);
676 value |= on ? highest_perf : nominal_perf;
677 WRITE_ONCE(cpudata->cppc_req_cached, value);
679 wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
681 perf_ctrls.max_perf = on ? highest_perf : nominal_perf;
682 ret = cppc_set_perf(cpudata->cpu, &perf_ctrls);
684 cpufreq_cpu_release(policy);
685 pr_debug("Failed to set max perf on CPU:%d. ret:%d\n",
692 policy->cpuinfo.max_freq = max_freq;
693 else if (policy->cpuinfo.max_freq > nominal_freq * 1000)
694 policy->cpuinfo.max_freq = nominal_freq * 1000;
696 policy->max = policy->cpuinfo.max_freq;
698 if (cppc_state == AMD_PSTATE_PASSIVE) {
699 ret = freq_qos_update_request(&cpudata->req[1], policy->cpuinfo.max_freq);
701 pr_debug("Failed to update freq constraint: CPU%d\n", cpudata->cpu);
704 return ret < 0 ? ret : 0;
707 static int amd_pstate_set_boost(struct cpufreq_policy *policy, int state)
709 struct amd_cpudata *cpudata = policy->driver_data;
712 if (!cpudata->boost_supported) {
713 pr_err("Boost mode is not supported by this processor or SBIOS\n");
716 mutex_lock(&amd_pstate_driver_lock);
717 ret = amd_pstate_cpu_boost_update(policy, state);
718 WRITE_ONCE(cpudata->boost_state, !ret ? state : false);
719 policy->boost_enabled = !ret ? state : false;
720 refresh_frequency_limits(policy);
721 mutex_unlock(&amd_pstate_driver_lock);
726 static int amd_pstate_init_boost_support(struct amd_cpudata *cpudata)
732 * If platform has no CPB support or disable it, initialize current driver
733 * boost_enabled state to be false, it is not an error for cpufreq core to handle.
735 if (!cpu_feature_enabled(X86_FEATURE_CPB)) {
736 pr_debug_once("Boost CPB capabilities not present in the processor\n");
741 /* at least one CPU supports CPB, even if others fail later on to set up */
742 current_pstate_driver->boost_enabled = true;
744 ret = rdmsrl_on_cpu(cpudata->cpu, MSR_K7_HWCR, &boost_val);
746 pr_err_once("failed to read initial CPU boost state!\n");
751 if (!(boost_val & MSR_K7_HWCR_CPB_DIS))
752 cpudata->boost_supported = true;
757 cpudata->boost_supported = false;
761 static void amd_perf_ctl_reset(unsigned int cpu)
763 wrmsrl_on_cpu(cpu, MSR_AMD_PERF_CTL, 0);
767 * Set amd-pstate preferred core enable can't be done directly from cpufreq callbacks
768 * due to locking, so queue the work for later.
770 static void amd_pstste_sched_prefcore_workfn(struct work_struct *work)
772 sched_set_itmt_support();
774 static DECLARE_WORK(sched_prefcore_work, amd_pstste_sched_prefcore_workfn);
776 #define CPPC_MAX_PERF U8_MAX
778 static void amd_pstate_init_prefcore(struct amd_cpudata *cpudata)
780 /* user disabled or not detected */
781 if (!amd_pstate_prefcore)
784 cpudata->hw_prefcore = true;
787 * The priorities can be set regardless of whether or not
788 * sched_set_itmt_support(true) has been called and it is valid to
789 * update them at any time after it has been called.
791 sched_set_itmt_core_prio((int)READ_ONCE(cpudata->highest_perf), cpudata->cpu);
793 schedule_work(&sched_prefcore_work);
796 static void amd_pstate_update_limits(unsigned int cpu)
798 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
799 struct amd_cpudata *cpudata;
800 u32 prev_high = 0, cur_high = 0;
802 bool highest_perf_changed = false;
807 cpudata = policy->driver_data;
809 if (!amd_pstate_prefcore)
812 mutex_lock(&amd_pstate_driver_lock);
813 ret = amd_get_highest_perf(cpu, &cur_high);
815 goto free_cpufreq_put;
817 prev_high = READ_ONCE(cpudata->prefcore_ranking);
818 highest_perf_changed = (prev_high != cur_high);
819 if (highest_perf_changed) {
820 WRITE_ONCE(cpudata->prefcore_ranking, cur_high);
822 if (cur_high < CPPC_MAX_PERF)
823 sched_set_itmt_core_prio((int)cur_high, cpu);
827 cpufreq_cpu_put(policy);
829 if (!highest_perf_changed)
830 cpufreq_update_policy(cpu);
832 mutex_unlock(&amd_pstate_driver_lock);
836 * Get pstate transition delay time from ACPI tables that firmware set
837 * instead of using hardcode value directly.
839 static u32 amd_pstate_get_transition_delay_us(unsigned int cpu)
841 u32 transition_delay_ns;
843 transition_delay_ns = cppc_get_transition_latency(cpu);
844 if (transition_delay_ns == CPUFREQ_ETERNAL) {
845 if (cpu_feature_enabled(X86_FEATURE_FAST_CPPC))
846 return AMD_PSTATE_FAST_CPPC_TRANSITION_DELAY;
848 return AMD_PSTATE_TRANSITION_DELAY;
851 return transition_delay_ns / NSEC_PER_USEC;
855 * Get pstate transition latency value from ACPI tables that firmware
856 * set instead of using hardcode value directly.
858 static u32 amd_pstate_get_transition_latency(unsigned int cpu)
860 u32 transition_latency;
862 transition_latency = cppc_get_transition_latency(cpu);
863 if (transition_latency == CPUFREQ_ETERNAL)
864 return AMD_PSTATE_TRANSITION_LATENCY;
866 return transition_latency;
870 * amd_pstate_init_freq: Initialize the max_freq, min_freq,
871 * nominal_freq and lowest_nonlinear_freq for
872 * the @cpudata object.
874 * Requires: highest_perf, lowest_perf, nominal_perf and
875 * lowest_nonlinear_perf members of @cpudata to be
878 * Returns 0 on success, non-zero value on failure.
880 static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
883 u32 min_freq, max_freq;
885 u32 nominal_perf, nominal_freq;
886 u32 lowest_nonlinear_perf, lowest_nonlinear_freq;
887 u32 boost_ratio, lowest_nonlinear_ratio;
888 struct cppc_perf_caps cppc_perf;
890 ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
894 if (quirks && quirks->lowest_freq)
895 min_freq = quirks->lowest_freq * 1000;
897 min_freq = cppc_perf.lowest_freq * 1000;
899 if (quirks && quirks->nominal_freq)
900 nominal_freq = quirks->nominal_freq ;
902 nominal_freq = cppc_perf.nominal_freq;
904 nominal_perf = READ_ONCE(cpudata->nominal_perf);
906 ret = amd_get_boost_ratio_numerator(cpudata->cpu, &numerator);
909 boost_ratio = div_u64(numerator << SCHED_CAPACITY_SHIFT, nominal_perf);
910 max_freq = (nominal_freq * boost_ratio >> SCHED_CAPACITY_SHIFT) * 1000;
912 lowest_nonlinear_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
913 lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,
915 lowest_nonlinear_freq = (nominal_freq * lowest_nonlinear_ratio >> SCHED_CAPACITY_SHIFT) * 1000;
917 WRITE_ONCE(cpudata->min_freq, min_freq);
918 WRITE_ONCE(cpudata->lowest_nonlinear_freq, lowest_nonlinear_freq);
919 WRITE_ONCE(cpudata->nominal_freq, nominal_freq);
920 WRITE_ONCE(cpudata->max_freq, max_freq);
923 * Below values need to be initialized correctly, otherwise driver will fail to load
924 * max_freq is calculated according to (nominal_freq * highest_perf)/nominal_perf
925 * lowest_nonlinear_freq is a value between [min_freq, nominal_freq]
926 * Check _CPC in ACPI table objects if any values are incorrect
928 if (min_freq <= 0 || max_freq <= 0 || nominal_freq <= 0 || min_freq > max_freq) {
929 pr_err("min_freq(%d) or max_freq(%d) or nominal_freq(%d) value is incorrect\n",
930 min_freq, max_freq, nominal_freq * 1000);
934 if (lowest_nonlinear_freq <= min_freq || lowest_nonlinear_freq > nominal_freq * 1000) {
935 pr_err("lowest_nonlinear_freq(%d) value is out of range [min_freq(%d), nominal_freq(%d)]\n",
936 lowest_nonlinear_freq, min_freq, nominal_freq * 1000);
943 static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
945 int min_freq, max_freq, ret;
947 struct amd_cpudata *cpudata;
950 * Resetting PERF_CTL_MSR will put the CPU in P0 frequency,
951 * which is ideal for initialization process.
953 amd_perf_ctl_reset(policy->cpu);
954 dev = get_cpu_device(policy->cpu);
958 cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
962 cpudata->cpu = policy->cpu;
964 ret = amd_pstate_init_perf(cpudata);
968 amd_pstate_init_prefcore(cpudata);
970 ret = amd_pstate_init_freq(cpudata);
974 ret = amd_pstate_init_boost_support(cpudata);
978 min_freq = READ_ONCE(cpudata->min_freq);
979 max_freq = READ_ONCE(cpudata->max_freq);
981 policy->cpuinfo.transition_latency = amd_pstate_get_transition_latency(policy->cpu);
982 policy->transition_delay_us = amd_pstate_get_transition_delay_us(policy->cpu);
984 policy->min = min_freq;
985 policy->max = max_freq;
987 policy->cpuinfo.min_freq = min_freq;
988 policy->cpuinfo.max_freq = max_freq;
990 policy->boost_enabled = READ_ONCE(cpudata->boost_supported);
992 /* It will be updated by governor */
993 policy->cur = policy->cpuinfo.min_freq;
995 if (cpu_feature_enabled(X86_FEATURE_CPPC))
996 policy->fast_switch_possible = true;
998 ret = freq_qos_add_request(&policy->constraints, &cpudata->req[0],
999 FREQ_QOS_MIN, policy->cpuinfo.min_freq);
1001 dev_err(dev, "Failed to add min-freq constraint (%d)\n", ret);
1005 ret = freq_qos_add_request(&policy->constraints, &cpudata->req[1],
1006 FREQ_QOS_MAX, policy->cpuinfo.max_freq);
1008 dev_err(dev, "Failed to add max-freq constraint (%d)\n", ret);
1012 cpudata->max_limit_freq = max_freq;
1013 cpudata->min_limit_freq = min_freq;
1015 policy->driver_data = cpudata;
1017 if (!current_pstate_driver->adjust_perf)
1018 current_pstate_driver->adjust_perf = amd_pstate_adjust_perf;
1023 freq_qos_remove_request(&cpudata->req[0]);
1029 static void amd_pstate_cpu_exit(struct cpufreq_policy *policy)
1031 struct amd_cpudata *cpudata = policy->driver_data;
1033 freq_qos_remove_request(&cpudata->req[1]);
1034 freq_qos_remove_request(&cpudata->req[0]);
1035 policy->fast_switch_possible = false;
1039 static int amd_pstate_cpu_resume(struct cpufreq_policy *policy)
1043 ret = amd_pstate_enable(true);
1045 pr_err("failed to enable amd-pstate during resume, return %d\n", ret);
1050 static int amd_pstate_cpu_suspend(struct cpufreq_policy *policy)
1054 ret = amd_pstate_enable(false);
1056 pr_err("failed to disable amd-pstate during suspend, return %d\n", ret);
1061 /* Sysfs attributes */
1064 * This frequency is to indicate the maximum hardware frequency.
1065 * If boost is not active but supported, the frequency will be larger than the
1068 static ssize_t show_amd_pstate_max_freq(struct cpufreq_policy *policy,
1072 struct amd_cpudata *cpudata = policy->driver_data;
1074 max_freq = READ_ONCE(cpudata->max_freq);
1078 return sysfs_emit(buf, "%u\n", max_freq);
1081 static ssize_t show_amd_pstate_lowest_nonlinear_freq(struct cpufreq_policy *policy,
1085 struct amd_cpudata *cpudata = policy->driver_data;
1087 freq = READ_ONCE(cpudata->lowest_nonlinear_freq);
1091 return sysfs_emit(buf, "%u\n", freq);
1095 * In some of ASICs, the highest_perf is not the one in the _CPC table, so we
1096 * need to expose it to sysfs.
1098 static ssize_t show_amd_pstate_highest_perf(struct cpufreq_policy *policy,
1102 struct amd_cpudata *cpudata = policy->driver_data;
1104 perf = READ_ONCE(cpudata->highest_perf);
1106 return sysfs_emit(buf, "%u\n", perf);
1109 static ssize_t show_amd_pstate_prefcore_ranking(struct cpufreq_policy *policy,
1113 struct amd_cpudata *cpudata = policy->driver_data;
1115 perf = READ_ONCE(cpudata->prefcore_ranking);
1117 return sysfs_emit(buf, "%u\n", perf);
1120 static ssize_t show_amd_pstate_hw_prefcore(struct cpufreq_policy *policy,
1124 struct amd_cpudata *cpudata = policy->driver_data;
1126 hw_prefcore = READ_ONCE(cpudata->hw_prefcore);
1128 return sysfs_emit(buf, "%s\n", str_enabled_disabled(hw_prefcore));
1131 static ssize_t show_energy_performance_available_preferences(
1132 struct cpufreq_policy *policy, char *buf)
1136 struct amd_cpudata *cpudata = policy->driver_data;
1138 if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
1139 return sysfs_emit_at(buf, offset, "%s\n",
1140 energy_perf_strings[EPP_INDEX_PERFORMANCE]);
1142 while (energy_perf_strings[i] != NULL)
1143 offset += sysfs_emit_at(buf, offset, "%s ", energy_perf_strings[i++]);
1145 offset += sysfs_emit_at(buf, offset, "\n");
1150 static ssize_t store_energy_performance_preference(
1151 struct cpufreq_policy *policy, const char *buf, size_t count)
1153 struct amd_cpudata *cpudata = policy->driver_data;
1154 char str_preference[21];
1157 ret = sscanf(buf, "%20s", str_preference);
1161 ret = match_string(energy_perf_strings, -1, str_preference);
1165 mutex_lock(&amd_pstate_limits_lock);
1166 ret = amd_pstate_set_energy_pref_index(cpudata, ret);
1167 mutex_unlock(&amd_pstate_limits_lock);
1169 return ret ?: count;
1172 static ssize_t show_energy_performance_preference(
1173 struct cpufreq_policy *policy, char *buf)
1175 struct amd_cpudata *cpudata = policy->driver_data;
1178 preference = amd_pstate_get_energy_pref_index(cpudata);
1182 return sysfs_emit(buf, "%s\n", energy_perf_strings[preference]);
1185 static void amd_pstate_driver_cleanup(void)
1187 amd_pstate_enable(false);
1188 cppc_state = AMD_PSTATE_DISABLE;
1189 current_pstate_driver = NULL;
1192 static int amd_pstate_register_driver(int mode)
1196 if (mode == AMD_PSTATE_PASSIVE || mode == AMD_PSTATE_GUIDED)
1197 current_pstate_driver = &amd_pstate_driver;
1198 else if (mode == AMD_PSTATE_ACTIVE)
1199 current_pstate_driver = &amd_pstate_epp_driver;
1204 ret = cpufreq_register_driver(current_pstate_driver);
1206 amd_pstate_driver_cleanup();
1212 static int amd_pstate_unregister_driver(int dummy)
1214 cpufreq_unregister_driver(current_pstate_driver);
1215 amd_pstate_driver_cleanup();
1219 static int amd_pstate_change_mode_without_dvr_change(int mode)
1225 if (cpu_feature_enabled(X86_FEATURE_CPPC) || cppc_state == AMD_PSTATE_ACTIVE)
1228 for_each_present_cpu(cpu) {
1229 cppc_set_auto_sel(cpu, (cppc_state == AMD_PSTATE_PASSIVE) ? 0 : 1);
1235 static int amd_pstate_change_driver_mode(int mode)
1239 ret = amd_pstate_unregister_driver(0);
1243 ret = amd_pstate_register_driver(mode);
1250 static cppc_mode_transition_fn mode_state_machine[AMD_PSTATE_MAX][AMD_PSTATE_MAX] = {
1251 [AMD_PSTATE_DISABLE] = {
1252 [AMD_PSTATE_DISABLE] = NULL,
1253 [AMD_PSTATE_PASSIVE] = amd_pstate_register_driver,
1254 [AMD_PSTATE_ACTIVE] = amd_pstate_register_driver,
1255 [AMD_PSTATE_GUIDED] = amd_pstate_register_driver,
1257 [AMD_PSTATE_PASSIVE] = {
1258 [AMD_PSTATE_DISABLE] = amd_pstate_unregister_driver,
1259 [AMD_PSTATE_PASSIVE] = NULL,
1260 [AMD_PSTATE_ACTIVE] = amd_pstate_change_driver_mode,
1261 [AMD_PSTATE_GUIDED] = amd_pstate_change_mode_without_dvr_change,
1263 [AMD_PSTATE_ACTIVE] = {
1264 [AMD_PSTATE_DISABLE] = amd_pstate_unregister_driver,
1265 [AMD_PSTATE_PASSIVE] = amd_pstate_change_driver_mode,
1266 [AMD_PSTATE_ACTIVE] = NULL,
1267 [AMD_PSTATE_GUIDED] = amd_pstate_change_driver_mode,
1269 [AMD_PSTATE_GUIDED] = {
1270 [AMD_PSTATE_DISABLE] = amd_pstate_unregister_driver,
1271 [AMD_PSTATE_PASSIVE] = amd_pstate_change_mode_without_dvr_change,
1272 [AMD_PSTATE_ACTIVE] = amd_pstate_change_driver_mode,
1273 [AMD_PSTATE_GUIDED] = NULL,
1277 static ssize_t amd_pstate_show_status(char *buf)
1279 if (!current_pstate_driver)
1280 return sysfs_emit(buf, "disable\n");
1282 return sysfs_emit(buf, "%s\n", amd_pstate_mode_string[cppc_state]);
1285 int amd_pstate_update_status(const char *buf, size_t size)
1289 if (size > strlen("passive") || size < strlen("active"))
1292 mode_idx = get_mode_idx_from_str(buf, size);
1294 if (mode_idx < 0 || mode_idx >= AMD_PSTATE_MAX)
1297 if (mode_state_machine[cppc_state][mode_idx])
1298 return mode_state_machine[cppc_state][mode_idx](mode_idx);
1302 EXPORT_SYMBOL_GPL(amd_pstate_update_status);
1304 static ssize_t status_show(struct device *dev,
1305 struct device_attribute *attr, char *buf)
1309 mutex_lock(&amd_pstate_driver_lock);
1310 ret = amd_pstate_show_status(buf);
1311 mutex_unlock(&amd_pstate_driver_lock);
1316 static ssize_t status_store(struct device *a, struct device_attribute *b,
1317 const char *buf, size_t count)
1319 char *p = memchr(buf, '\n', count);
1322 mutex_lock(&amd_pstate_driver_lock);
1323 ret = amd_pstate_update_status(buf, p ? p - buf : count);
1324 mutex_unlock(&amd_pstate_driver_lock);
1326 return ret < 0 ? ret : count;
1329 static ssize_t prefcore_show(struct device *dev,
1330 struct device_attribute *attr, char *buf)
1332 return sysfs_emit(buf, "%s\n", str_enabled_disabled(amd_pstate_prefcore));
1335 cpufreq_freq_attr_ro(amd_pstate_max_freq);
1336 cpufreq_freq_attr_ro(amd_pstate_lowest_nonlinear_freq);
1338 cpufreq_freq_attr_ro(amd_pstate_highest_perf);
1339 cpufreq_freq_attr_ro(amd_pstate_prefcore_ranking);
1340 cpufreq_freq_attr_ro(amd_pstate_hw_prefcore);
1341 cpufreq_freq_attr_rw(energy_performance_preference);
1342 cpufreq_freq_attr_ro(energy_performance_available_preferences);
1343 static DEVICE_ATTR_RW(status);
1344 static DEVICE_ATTR_RO(prefcore);
1346 static struct freq_attr *amd_pstate_attr[] = {
1347 &amd_pstate_max_freq,
1348 &amd_pstate_lowest_nonlinear_freq,
1349 &amd_pstate_highest_perf,
1350 &amd_pstate_prefcore_ranking,
1351 &amd_pstate_hw_prefcore,
1355 static struct freq_attr *amd_pstate_epp_attr[] = {
1356 &amd_pstate_max_freq,
1357 &amd_pstate_lowest_nonlinear_freq,
1358 &amd_pstate_highest_perf,
1359 &amd_pstate_prefcore_ranking,
1360 &amd_pstate_hw_prefcore,
1361 &energy_performance_preference,
1362 &energy_performance_available_preferences,
1366 static struct attribute *pstate_global_attributes[] = {
1367 &dev_attr_status.attr,
1368 &dev_attr_prefcore.attr,
1372 static const struct attribute_group amd_pstate_global_attr_group = {
1373 .name = "amd_pstate",
1374 .attrs = pstate_global_attributes,
1377 static bool amd_pstate_acpi_pm_profile_server(void)
1379 switch (acpi_gbl_FADT.preferred_profile) {
1380 case PM_ENTERPRISE_SERVER:
1381 case PM_SOHO_SERVER:
1382 case PM_PERFORMANCE_SERVER:
1388 static bool amd_pstate_acpi_pm_profile_undefined(void)
1390 if (acpi_gbl_FADT.preferred_profile == PM_UNSPECIFIED)
1392 if (acpi_gbl_FADT.preferred_profile >= NR_PM_PROFILES)
1397 static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
1399 int min_freq, max_freq, ret;
1400 struct amd_cpudata *cpudata;
1405 * Resetting PERF_CTL_MSR will put the CPU in P0 frequency,
1406 * which is ideal for initialization process.
1408 amd_perf_ctl_reset(policy->cpu);
1409 dev = get_cpu_device(policy->cpu);
1413 cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
1417 cpudata->cpu = policy->cpu;
1418 cpudata->epp_policy = 0;
1420 ret = amd_pstate_init_perf(cpudata);
1424 amd_pstate_init_prefcore(cpudata);
1426 ret = amd_pstate_init_freq(cpudata);
1430 ret = amd_pstate_init_boost_support(cpudata);
1434 min_freq = READ_ONCE(cpudata->min_freq);
1435 max_freq = READ_ONCE(cpudata->max_freq);
1437 policy->cpuinfo.min_freq = min_freq;
1438 policy->cpuinfo.max_freq = max_freq;
1439 /* It will be updated by governor */
1440 policy->cur = policy->cpuinfo.min_freq;
1442 policy->driver_data = cpudata;
1444 cpudata->epp_cached = cpudata->epp_default = amd_pstate_get_epp(cpudata, 0);
1446 policy->min = policy->cpuinfo.min_freq;
1447 policy->max = policy->cpuinfo.max_freq;
1449 policy->boost_enabled = READ_ONCE(cpudata->boost_supported);
1452 * Set the policy to provide a valid fallback value in case
1453 * the default cpufreq governor is neither powersave nor performance.
1455 if (amd_pstate_acpi_pm_profile_server() ||
1456 amd_pstate_acpi_pm_profile_undefined())
1457 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
1459 policy->policy = CPUFREQ_POLICY_POWERSAVE;
1461 if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
1462 ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, &value);
1465 WRITE_ONCE(cpudata->cppc_req_cached, value);
1467 ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_CAP1, &value);
1470 WRITE_ONCE(cpudata->cppc_cap1_cached, value);
1480 static void amd_pstate_epp_cpu_exit(struct cpufreq_policy *policy)
1482 struct amd_cpudata *cpudata = policy->driver_data;
1486 policy->driver_data = NULL;
1489 pr_debug("CPU %d exiting\n", policy->cpu);
1492 static int amd_pstate_epp_update_limit(struct cpufreq_policy *policy)
1494 struct amd_cpudata *cpudata = policy->driver_data;
1495 u32 max_perf, min_perf, min_limit_perf, max_limit_perf;
1499 max_perf = READ_ONCE(cpudata->highest_perf);
1500 min_perf = READ_ONCE(cpudata->lowest_perf);
1501 max_limit_perf = div_u64(policy->max * cpudata->highest_perf, cpudata->max_freq);
1502 min_limit_perf = div_u64(policy->min * cpudata->highest_perf, cpudata->max_freq);
1504 if (min_limit_perf < min_perf)
1505 min_limit_perf = min_perf;
1507 if (max_limit_perf < min_limit_perf)
1508 max_limit_perf = min_limit_perf;
1510 WRITE_ONCE(cpudata->max_limit_perf, max_limit_perf);
1511 WRITE_ONCE(cpudata->min_limit_perf, min_limit_perf);
1513 max_perf = clamp_t(unsigned long, max_perf, cpudata->min_limit_perf,
1514 cpudata->max_limit_perf);
1515 min_perf = clamp_t(unsigned long, min_perf, cpudata->min_limit_perf,
1516 cpudata->max_limit_perf);
1517 value = READ_ONCE(cpudata->cppc_req_cached);
1519 if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
1520 min_perf = max_perf;
1522 /* Initial min/max values for CPPC Performance Controls Register */
1523 value &= ~AMD_CPPC_MIN_PERF(~0L);
1524 value |= AMD_CPPC_MIN_PERF(min_perf);
1526 value &= ~AMD_CPPC_MAX_PERF(~0L);
1527 value |= AMD_CPPC_MAX_PERF(max_perf);
1529 /* CPPC EPP feature require to set zero to the desire perf bit */
1530 value &= ~AMD_CPPC_DES_PERF(~0L);
1531 value |= AMD_CPPC_DES_PERF(0);
1533 cpudata->epp_policy = cpudata->policy;
1535 /* Get BIOS pre-defined epp value */
1536 epp = amd_pstate_get_epp(cpudata, value);
1539 * This return value can only be negative for shared_memory
1540 * systems where EPP register read/write not supported.
1545 if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
1548 /* Set initial EPP value */
1549 if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
1550 value &= ~GENMASK_ULL(31, 24);
1551 value |= (u64)epp << 24;
1554 WRITE_ONCE(cpudata->cppc_req_cached, value);
1555 return amd_pstate_set_epp(cpudata, epp);
1558 static int amd_pstate_epp_set_policy(struct cpufreq_policy *policy)
1560 struct amd_cpudata *cpudata = policy->driver_data;
1563 if (!policy->cpuinfo.max_freq)
1566 pr_debug("set_policy: cpuinfo.max %u policy->max %u\n",
1567 policy->cpuinfo.max_freq, policy->max);
1569 cpudata->policy = policy->policy;
1571 ret = amd_pstate_epp_update_limit(policy);
1576 * policy->cur is never updated with the amd_pstate_epp driver, but it
1577 * is used as a stale frequency value. So, keep it within limits.
1579 policy->cur = policy->min;
1584 static void amd_pstate_epp_reenable(struct amd_cpudata *cpudata)
1586 struct cppc_perf_ctrls perf_ctrls;
1587 u64 value, max_perf;
1590 ret = amd_pstate_enable(true);
1592 pr_err("failed to enable amd pstate during resume, return %d\n", ret);
1594 value = READ_ONCE(cpudata->cppc_req_cached);
1595 max_perf = READ_ONCE(cpudata->highest_perf);
1597 if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
1598 wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
1600 perf_ctrls.max_perf = max_perf;
1601 perf_ctrls.energy_perf = AMD_CPPC_ENERGY_PERF_PREF(cpudata->epp_cached);
1602 cppc_set_perf(cpudata->cpu, &perf_ctrls);
1606 static int amd_pstate_epp_cpu_online(struct cpufreq_policy *policy)
1608 struct amd_cpudata *cpudata = policy->driver_data;
1610 pr_debug("AMD CPU Core %d going online\n", cpudata->cpu);
1612 if (cppc_state == AMD_PSTATE_ACTIVE) {
1613 amd_pstate_epp_reenable(cpudata);
1614 cpudata->suspended = false;
1620 static void amd_pstate_epp_offline(struct cpufreq_policy *policy)
1622 struct amd_cpudata *cpudata = policy->driver_data;
1623 struct cppc_perf_ctrls perf_ctrls;
1627 min_perf = READ_ONCE(cpudata->lowest_perf);
1628 value = READ_ONCE(cpudata->cppc_req_cached);
1630 mutex_lock(&amd_pstate_limits_lock);
1631 if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
1632 cpudata->epp_policy = CPUFREQ_POLICY_UNKNOWN;
1634 /* Set max perf same as min perf */
1635 value &= ~AMD_CPPC_MAX_PERF(~0L);
1636 value |= AMD_CPPC_MAX_PERF(min_perf);
1637 value &= ~AMD_CPPC_MIN_PERF(~0L);
1638 value |= AMD_CPPC_MIN_PERF(min_perf);
1639 wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
1641 perf_ctrls.desired_perf = 0;
1642 perf_ctrls.max_perf = min_perf;
1643 perf_ctrls.energy_perf = AMD_CPPC_ENERGY_PERF_PREF(HWP_EPP_BALANCE_POWERSAVE);
1644 cppc_set_perf(cpudata->cpu, &perf_ctrls);
1646 mutex_unlock(&amd_pstate_limits_lock);
1649 static int amd_pstate_epp_cpu_offline(struct cpufreq_policy *policy)
1651 struct amd_cpudata *cpudata = policy->driver_data;
1653 pr_debug("AMD CPU Core %d going offline\n", cpudata->cpu);
1655 if (cpudata->suspended)
1658 if (cppc_state == AMD_PSTATE_ACTIVE)
1659 amd_pstate_epp_offline(policy);
1664 static int amd_pstate_epp_verify_policy(struct cpufreq_policy_data *policy)
1666 cpufreq_verify_within_cpu_limits(policy);
1667 pr_debug("policy_max =%d, policy_min=%d\n", policy->max, policy->min);
1671 static int amd_pstate_epp_suspend(struct cpufreq_policy *policy)
1673 struct amd_cpudata *cpudata = policy->driver_data;
1676 /* avoid suspending when EPP is not enabled */
1677 if (cppc_state != AMD_PSTATE_ACTIVE)
1680 /* set this flag to avoid setting core offline*/
1681 cpudata->suspended = true;
1683 /* disable CPPC in lowlevel firmware */
1684 ret = amd_pstate_enable(false);
1686 pr_err("failed to suspend, return %d\n", ret);
1691 static int amd_pstate_epp_resume(struct cpufreq_policy *policy)
1693 struct amd_cpudata *cpudata = policy->driver_data;
1695 if (cpudata->suspended) {
1696 mutex_lock(&amd_pstate_limits_lock);
1698 /* enable amd pstate from suspend state*/
1699 amd_pstate_epp_reenable(cpudata);
1701 mutex_unlock(&amd_pstate_limits_lock);
1703 cpudata->suspended = false;
1709 static struct cpufreq_driver amd_pstate_driver = {
1710 .flags = CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_UPDATE_LIMITS,
1711 .verify = amd_pstate_verify,
1712 .target = amd_pstate_target,
1713 .fast_switch = amd_pstate_fast_switch,
1714 .init = amd_pstate_cpu_init,
1715 .exit = amd_pstate_cpu_exit,
1716 .suspend = amd_pstate_cpu_suspend,
1717 .resume = amd_pstate_cpu_resume,
1718 .set_boost = amd_pstate_set_boost,
1719 .update_limits = amd_pstate_update_limits,
1720 .name = "amd-pstate",
1721 .attr = amd_pstate_attr,
1724 static struct cpufreq_driver amd_pstate_epp_driver = {
1725 .flags = CPUFREQ_CONST_LOOPS,
1726 .verify = amd_pstate_epp_verify_policy,
1727 .setpolicy = amd_pstate_epp_set_policy,
1728 .init = amd_pstate_epp_cpu_init,
1729 .exit = amd_pstate_epp_cpu_exit,
1730 .offline = amd_pstate_epp_cpu_offline,
1731 .online = amd_pstate_epp_cpu_online,
1732 .suspend = amd_pstate_epp_suspend,
1733 .resume = amd_pstate_epp_resume,
1734 .update_limits = amd_pstate_update_limits,
1735 .set_boost = amd_pstate_set_boost,
1736 .name = "amd-pstate-epp",
1737 .attr = amd_pstate_epp_attr,
1740 static int __init amd_pstate_set_driver(int mode_idx)
1742 if (mode_idx >= AMD_PSTATE_DISABLE && mode_idx < AMD_PSTATE_MAX) {
1743 cppc_state = mode_idx;
1744 if (cppc_state == AMD_PSTATE_DISABLE)
1745 pr_info("driver is explicitly disabled\n");
1747 if (cppc_state == AMD_PSTATE_ACTIVE)
1748 current_pstate_driver = &amd_pstate_epp_driver;
1750 if (cppc_state == AMD_PSTATE_PASSIVE || cppc_state == AMD_PSTATE_GUIDED)
1751 current_pstate_driver = &amd_pstate_driver;
1760 * CPPC function is not supported for family ID 17H with model_ID ranging from 0x10 to 0x2F.
1761 * show the debug message that helps to check if the CPU has CPPC support for loading issue.
1763 static bool amd_cppc_supported(void)
1765 struct cpuinfo_x86 *c = &cpu_data(0);
1768 if ((boot_cpu_data.x86 == 0x17) && (boot_cpu_data.x86_model < 0x30)) {
1769 pr_debug_once("CPPC feature is not supported by the processor\n");
1774 * If the CPPC feature is disabled in the BIOS for processors
1775 * that support MSR-based CPPC, the AMD Pstate driver may not
1776 * function correctly.
1778 * For such processors, check the CPPC flag and display a
1779 * warning message if the platform supports CPPC.
1781 * Note: The code check below will not abort the driver
1782 * registration process because of the code is added for
1783 * debugging purposes. Besides, it may still be possible for
1784 * the driver to work using the shared-memory mechanism.
1786 if (!cpu_feature_enabled(X86_FEATURE_CPPC)) {
1787 if (cpu_feature_enabled(X86_FEATURE_ZEN2)) {
1788 switch (c->x86_model) {
1794 } else if (cpu_feature_enabled(X86_FEATURE_ZEN3) ||
1795 cpu_feature_enabled(X86_FEATURE_ZEN4)) {
1796 switch (c->x86_model) {
1802 } else if (cpu_feature_enabled(X86_FEATURE_ZEN5)) {
1808 pr_warn_once("The CPPC feature is supported but currently disabled by the BIOS.\n"
1809 "Please enable it if your BIOS has the CPPC option.\n");
1813 static int __init amd_pstate_init(void)
1815 struct device *dev_root;
1818 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
1821 /* show debug message only if CPPC is not supported */
1822 if (!amd_cppc_supported())
1825 /* show warning message when BIOS broken or ACPI disabled */
1826 if (!acpi_cpc_valid()) {
1827 pr_warn_once("the _CPC object is not present in SBIOS or ACPI disabled\n");
1831 /* don't keep reloading if cpufreq_driver exists */
1832 if (cpufreq_get_current_driver())
1837 /* check if this machine need CPPC quirks */
1838 dmi_check_system(amd_pstate_quirks_table);
1841 * determine the driver mode from the command line or kernel config.
1842 * If no command line input is provided, cppc_state will be AMD_PSTATE_UNDEFINED.
1843 * command line options will override the kernel config settings.
1846 if (cppc_state == AMD_PSTATE_UNDEFINED) {
1847 /* Disable on the following configs by default:
1848 * 1. Undefined platforms
1849 * 2. Server platforms
1851 if (amd_pstate_acpi_pm_profile_undefined() ||
1852 amd_pstate_acpi_pm_profile_server()) {
1853 pr_info("driver load is disabled, boot with specific mode to enable this\n");
1856 /* get driver mode from kernel config option [1:4] */
1857 cppc_state = CONFIG_X86_AMD_PSTATE_DEFAULT_MODE;
1860 switch (cppc_state) {
1861 case AMD_PSTATE_DISABLE:
1862 pr_info("driver load is disabled, boot with specific mode to enable this\n");
1864 case AMD_PSTATE_PASSIVE:
1865 case AMD_PSTATE_ACTIVE:
1866 case AMD_PSTATE_GUIDED:
1867 ret = amd_pstate_set_driver(cppc_state);
1875 /* capability check */
1876 if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
1877 pr_debug("AMD CPPC MSR based functionality is supported\n");
1878 if (cppc_state != AMD_PSTATE_ACTIVE)
1879 current_pstate_driver->adjust_perf = amd_pstate_adjust_perf;
1881 pr_debug("AMD CPPC shared memory based functionality is supported\n");
1882 static_call_update(amd_pstate_enable, cppc_enable);
1883 static_call_update(amd_pstate_init_perf, cppc_init_perf);
1884 static_call_update(amd_pstate_update_perf, cppc_update_perf);
1887 if (amd_pstate_prefcore) {
1888 ret = amd_detect_prefcore(&amd_pstate_prefcore);
1893 /* enable amd pstate feature */
1894 ret = amd_pstate_enable(true);
1896 pr_err("failed to enable driver mode(%d)\n", cppc_state);
1900 ret = cpufreq_register_driver(current_pstate_driver);
1902 pr_err("failed to register with return %d\n", ret);
1903 goto disable_driver;
1906 dev_root = bus_get_dev_root(&cpu_subsys);
1908 ret = sysfs_create_group(&dev_root->kobj, &amd_pstate_global_attr_group);
1909 put_device(dev_root);
1911 pr_err("sysfs attribute export failed with error %d.\n", ret);
1912 goto global_attr_free;
1919 cpufreq_unregister_driver(current_pstate_driver);
1921 amd_pstate_enable(false);
1924 device_initcall(amd_pstate_init);
1926 static int __init amd_pstate_param(char *str)
1935 mode_idx = get_mode_idx_from_str(str, size);
1937 return amd_pstate_set_driver(mode_idx);
1940 static int __init amd_prefcore_param(char *str)
1942 if (!strcmp(str, "disable"))
1943 amd_pstate_prefcore = false;
1948 early_param("amd_pstate", amd_pstate_param);
1949 early_param("amd_prefcore", amd_prefcore_param);
1952 MODULE_DESCRIPTION("AMD Processor P-state Frequency Driver");