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>
40 #include <acpi/processor.h>
41 #include <acpi/cppc_acpi.h>
44 #include <asm/processor.h>
45 #include <asm/cpufeature.h>
46 #include <asm/cpu_device_id.h>
47 #include "amd-pstate-trace.h"
49 #define AMD_PSTATE_TRANSITION_LATENCY 0x20000
50 #define AMD_PSTATE_TRANSITION_DELAY 500
53 * TODO: We need more time to fine tune processors with shared memory solution
54 * with community together.
56 * There are some performance drops on the CPU benchmarks which reports from
57 * Suse. We are co-working with them to fine tune the shared memory solution. So
58 * we disable it by default to go acpi-cpufreq on these processors and add a
59 * module parameter to be able to enable it manually for debugging.
61 static bool shared_mem = false;
62 module_param(shared_mem, bool, 0444);
63 MODULE_PARM_DESC(shared_mem,
64 "enable amd-pstate on processors with shared memory solution (false = disabled (default), true = enabled)");
66 static struct cpufreq_driver amd_pstate_driver;
69 * struct amd_cpudata - private CPU data for AMD P-State
71 * @req: constraint request to apply
72 * @cppc_req_cached: cached performance request hints
73 * @highest_perf: the maximum performance an individual processor may reach,
74 * assuming ideal conditions
75 * @nominal_perf: the maximum sustained performance level of the processor,
76 * assuming ideal operating conditions
77 * @lowest_nonlinear_perf: the lowest performance level at which nonlinear power
78 * savings are achieved
79 * @lowest_perf: the absolute lowest performance level of the processor
80 * @max_freq: the frequency that mapped to highest_perf
81 * @min_freq: the frequency that mapped to lowest_perf
82 * @nominal_freq: the frequency that mapped to nominal_perf
83 * @lowest_nonlinear_freq: the frequency that mapped to lowest_nonlinear_perf
84 * @boost_supported: check whether the Processor or SBIOS supports boost mode
86 * The amd_cpudata is key private data for each CPU thread in AMD P-State, and
87 * represents all the attributes and goals that AMD P-State requests at runtime.
92 struct freq_qos_request req[2];
97 u32 lowest_nonlinear_perf;
103 u32 lowest_nonlinear_freq;
105 bool boost_supported;
108 static inline int pstate_enable(bool enable)
110 return wrmsrl_safe(MSR_AMD_CPPC_ENABLE, enable);
113 static int cppc_enable(bool enable)
117 for_each_present_cpu(cpu) {
118 ret = cppc_set_enable(cpu, enable);
126 DEFINE_STATIC_CALL(amd_pstate_enable, pstate_enable);
128 static inline int amd_pstate_enable(bool enable)
130 return static_call(amd_pstate_enable)(enable);
133 static int pstate_init_perf(struct amd_cpudata *cpudata)
137 int ret = rdmsrl_safe_on_cpu(cpudata->cpu, MSR_AMD_CPPC_CAP1,
143 * TODO: Introduce AMD specific power feature.
145 * CPPC entry doesn't indicate the highest performance in some ASICs.
147 WRITE_ONCE(cpudata->highest_perf, amd_get_highest_perf());
149 WRITE_ONCE(cpudata->nominal_perf, AMD_CPPC_NOMINAL_PERF(cap1));
150 WRITE_ONCE(cpudata->lowest_nonlinear_perf, AMD_CPPC_LOWNONLIN_PERF(cap1));
151 WRITE_ONCE(cpudata->lowest_perf, AMD_CPPC_LOWEST_PERF(cap1));
156 static int cppc_init_perf(struct amd_cpudata *cpudata)
158 struct cppc_perf_caps cppc_perf;
160 int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
164 WRITE_ONCE(cpudata->highest_perf, amd_get_highest_perf());
166 WRITE_ONCE(cpudata->nominal_perf, cppc_perf.nominal_perf);
167 WRITE_ONCE(cpudata->lowest_nonlinear_perf,
168 cppc_perf.lowest_nonlinear_perf);
169 WRITE_ONCE(cpudata->lowest_perf, cppc_perf.lowest_perf);
174 DEFINE_STATIC_CALL(amd_pstate_init_perf, pstate_init_perf);
176 static inline int amd_pstate_init_perf(struct amd_cpudata *cpudata)
178 return static_call(amd_pstate_init_perf)(cpudata);
181 static void pstate_update_perf(struct amd_cpudata *cpudata, u32 min_perf,
182 u32 des_perf, u32 max_perf, bool fast_switch)
185 wrmsrl(MSR_AMD_CPPC_REQ, READ_ONCE(cpudata->cppc_req_cached));
187 wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,
188 READ_ONCE(cpudata->cppc_req_cached));
191 static void cppc_update_perf(struct amd_cpudata *cpudata,
192 u32 min_perf, u32 des_perf,
193 u32 max_perf, bool fast_switch)
195 struct cppc_perf_ctrls perf_ctrls;
197 perf_ctrls.max_perf = max_perf;
198 perf_ctrls.min_perf = min_perf;
199 perf_ctrls.desired_perf = des_perf;
201 cppc_set_perf(cpudata->cpu, &perf_ctrls);
204 DEFINE_STATIC_CALL(amd_pstate_update_perf, pstate_update_perf);
206 static inline void amd_pstate_update_perf(struct amd_cpudata *cpudata,
207 u32 min_perf, u32 des_perf,
208 u32 max_perf, bool fast_switch)
210 static_call(amd_pstate_update_perf)(cpudata, min_perf, des_perf,
211 max_perf, fast_switch);
214 static void amd_pstate_update(struct amd_cpudata *cpudata, u32 min_perf,
215 u32 des_perf, u32 max_perf, bool fast_switch)
217 u64 prev = READ_ONCE(cpudata->cppc_req_cached);
220 value &= ~AMD_CPPC_MIN_PERF(~0L);
221 value |= AMD_CPPC_MIN_PERF(min_perf);
223 value &= ~AMD_CPPC_DES_PERF(~0L);
224 value |= AMD_CPPC_DES_PERF(des_perf);
226 value &= ~AMD_CPPC_MAX_PERF(~0L);
227 value |= AMD_CPPC_MAX_PERF(max_perf);
229 trace_amd_pstate_perf(min_perf, des_perf, max_perf,
230 cpudata->cpu, (value != prev), fast_switch);
235 WRITE_ONCE(cpudata->cppc_req_cached, value);
237 amd_pstate_update_perf(cpudata, min_perf, des_perf,
238 max_perf, fast_switch);
241 static int amd_pstate_verify(struct cpufreq_policy_data *policy)
243 cpufreq_verify_within_cpu_limits(policy);
248 static int amd_pstate_target(struct cpufreq_policy *policy,
249 unsigned int target_freq,
250 unsigned int relation)
252 struct cpufreq_freqs freqs;
253 struct amd_cpudata *cpudata = policy->driver_data;
254 unsigned long max_perf, min_perf, des_perf, cap_perf;
256 if (!cpudata->max_freq)
259 cap_perf = READ_ONCE(cpudata->highest_perf);
260 min_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
263 freqs.old = policy->cur;
264 freqs.new = target_freq;
266 des_perf = DIV_ROUND_CLOSEST(target_freq * cap_perf,
269 cpufreq_freq_transition_begin(policy, &freqs);
270 amd_pstate_update(cpudata, min_perf, des_perf,
272 cpufreq_freq_transition_end(policy, &freqs, false);
277 static void amd_pstate_adjust_perf(unsigned int cpu,
278 unsigned long _min_perf,
279 unsigned long target_perf,
280 unsigned long capacity)
282 unsigned long max_perf, min_perf, des_perf,
283 cap_perf, lowest_nonlinear_perf;
284 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
285 struct amd_cpudata *cpudata = policy->driver_data;
287 cap_perf = READ_ONCE(cpudata->highest_perf);
288 lowest_nonlinear_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
291 if (target_perf < capacity)
292 des_perf = DIV_ROUND_UP(cap_perf * target_perf, capacity);
294 min_perf = READ_ONCE(cpudata->highest_perf);
295 if (_min_perf < capacity)
296 min_perf = DIV_ROUND_UP(cap_perf * _min_perf, capacity);
298 if (min_perf < lowest_nonlinear_perf)
299 min_perf = lowest_nonlinear_perf;
302 if (max_perf < min_perf)
305 des_perf = clamp_t(unsigned long, des_perf, min_perf, max_perf);
307 amd_pstate_update(cpudata, min_perf, des_perf, max_perf, true);
310 static int amd_get_min_freq(struct amd_cpudata *cpudata)
312 struct cppc_perf_caps cppc_perf;
314 int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
319 return cppc_perf.lowest_freq * 1000;
322 static int amd_get_max_freq(struct amd_cpudata *cpudata)
324 struct cppc_perf_caps cppc_perf;
325 u32 max_perf, max_freq, nominal_freq, nominal_perf;
328 int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
332 nominal_freq = cppc_perf.nominal_freq;
333 nominal_perf = READ_ONCE(cpudata->nominal_perf);
334 max_perf = READ_ONCE(cpudata->highest_perf);
336 boost_ratio = div_u64(max_perf << SCHED_CAPACITY_SHIFT,
339 max_freq = nominal_freq * boost_ratio >> SCHED_CAPACITY_SHIFT;
342 return max_freq * 1000;
345 static int amd_get_nominal_freq(struct amd_cpudata *cpudata)
347 struct cppc_perf_caps cppc_perf;
349 int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
354 return cppc_perf.nominal_freq * 1000;
357 static int amd_get_lowest_nonlinear_freq(struct amd_cpudata *cpudata)
359 struct cppc_perf_caps cppc_perf;
360 u32 lowest_nonlinear_freq, lowest_nonlinear_perf,
361 nominal_freq, nominal_perf;
362 u64 lowest_nonlinear_ratio;
364 int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
368 nominal_freq = cppc_perf.nominal_freq;
369 nominal_perf = READ_ONCE(cpudata->nominal_perf);
371 lowest_nonlinear_perf = cppc_perf.lowest_nonlinear_perf;
373 lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,
376 lowest_nonlinear_freq = nominal_freq * lowest_nonlinear_ratio >> SCHED_CAPACITY_SHIFT;
379 return lowest_nonlinear_freq * 1000;
382 static int amd_pstate_set_boost(struct cpufreq_policy *policy, int state)
384 struct amd_cpudata *cpudata = policy->driver_data;
387 if (!cpudata->boost_supported) {
388 pr_err("Boost mode is not supported by this processor or SBIOS\n");
393 policy->cpuinfo.max_freq = cpudata->max_freq;
395 policy->cpuinfo.max_freq = cpudata->nominal_freq;
397 policy->max = policy->cpuinfo.max_freq;
399 ret = freq_qos_update_request(&cpudata->req[1],
400 policy->cpuinfo.max_freq);
407 static void amd_pstate_boost_init(struct amd_cpudata *cpudata)
409 u32 highest_perf, nominal_perf;
411 highest_perf = READ_ONCE(cpudata->highest_perf);
412 nominal_perf = READ_ONCE(cpudata->nominal_perf);
414 if (highest_perf <= nominal_perf)
417 cpudata->boost_supported = true;
418 amd_pstate_driver.boost_enabled = true;
421 static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
423 int min_freq, max_freq, nominal_freq, lowest_nonlinear_freq, ret;
425 struct amd_cpudata *cpudata;
427 dev = get_cpu_device(policy->cpu);
431 cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
435 cpudata->cpu = policy->cpu;
437 ret = amd_pstate_init_perf(cpudata);
441 min_freq = amd_get_min_freq(cpudata);
442 max_freq = amd_get_max_freq(cpudata);
443 nominal_freq = amd_get_nominal_freq(cpudata);
444 lowest_nonlinear_freq = amd_get_lowest_nonlinear_freq(cpudata);
446 if (min_freq < 0 || max_freq < 0 || min_freq > max_freq) {
447 dev_err(dev, "min_freq(%d) or max_freq(%d) value is incorrect\n",
453 policy->cpuinfo.transition_latency = AMD_PSTATE_TRANSITION_LATENCY;
454 policy->transition_delay_us = AMD_PSTATE_TRANSITION_DELAY;
456 policy->min = min_freq;
457 policy->max = max_freq;
459 policy->cpuinfo.min_freq = min_freq;
460 policy->cpuinfo.max_freq = max_freq;
462 /* It will be updated by governor */
463 policy->cur = policy->cpuinfo.min_freq;
465 if (boot_cpu_has(X86_FEATURE_CPPC))
466 policy->fast_switch_possible = true;
468 ret = freq_qos_add_request(&policy->constraints, &cpudata->req[0],
469 FREQ_QOS_MIN, policy->cpuinfo.min_freq);
471 dev_err(dev, "Failed to add min-freq constraint (%d)\n", ret);
475 ret = freq_qos_add_request(&policy->constraints, &cpudata->req[1],
476 FREQ_QOS_MAX, policy->cpuinfo.max_freq);
478 dev_err(dev, "Failed to add max-freq constraint (%d)\n", ret);
482 /* Initial processor data capability frequencies */
483 cpudata->max_freq = max_freq;
484 cpudata->min_freq = min_freq;
485 cpudata->nominal_freq = nominal_freq;
486 cpudata->lowest_nonlinear_freq = lowest_nonlinear_freq;
488 policy->driver_data = cpudata;
490 amd_pstate_boost_init(cpudata);
495 freq_qos_remove_request(&cpudata->req[0]);
501 static int amd_pstate_cpu_exit(struct cpufreq_policy *policy)
503 struct amd_cpudata *cpudata;
505 cpudata = policy->driver_data;
507 freq_qos_remove_request(&cpudata->req[1]);
508 freq_qos_remove_request(&cpudata->req[0]);
514 /* Sysfs attributes */
517 * This frequency is to indicate the maximum hardware frequency.
518 * If boost is not active but supported, the frequency will be larger than the
521 static ssize_t show_amd_pstate_max_freq(struct cpufreq_policy *policy,
525 struct amd_cpudata *cpudata;
527 cpudata = policy->driver_data;
529 max_freq = amd_get_max_freq(cpudata);
533 return sprintf(&buf[0], "%u\n", max_freq);
536 static ssize_t show_amd_pstate_lowest_nonlinear_freq(struct cpufreq_policy *policy,
540 struct amd_cpudata *cpudata;
542 cpudata = policy->driver_data;
544 freq = amd_get_lowest_nonlinear_freq(cpudata);
548 return sprintf(&buf[0], "%u\n", freq);
552 * In some of ASICs, the highest_perf is not the one in the _CPC table, so we
553 * need to expose it to sysfs.
555 static ssize_t show_amd_pstate_highest_perf(struct cpufreq_policy *policy,
559 struct amd_cpudata *cpudata = policy->driver_data;
561 perf = READ_ONCE(cpudata->highest_perf);
563 return sprintf(&buf[0], "%u\n", perf);
566 cpufreq_freq_attr_ro(amd_pstate_max_freq);
567 cpufreq_freq_attr_ro(amd_pstate_lowest_nonlinear_freq);
569 cpufreq_freq_attr_ro(amd_pstate_highest_perf);
571 static struct freq_attr *amd_pstate_attr[] = {
572 &amd_pstate_max_freq,
573 &amd_pstate_lowest_nonlinear_freq,
574 &amd_pstate_highest_perf,
578 static struct cpufreq_driver amd_pstate_driver = {
579 .flags = CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_UPDATE_LIMITS,
580 .verify = amd_pstate_verify,
581 .target = amd_pstate_target,
582 .init = amd_pstate_cpu_init,
583 .exit = amd_pstate_cpu_exit,
584 .set_boost = amd_pstate_set_boost,
585 .name = "amd-pstate",
586 .attr = amd_pstate_attr,
589 static int __init amd_pstate_init(void)
593 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
596 if (!acpi_cpc_valid()) {
597 pr_debug("the _CPC object is not present in SBIOS\n");
601 /* don't keep reloading if cpufreq_driver exists */
602 if (cpufreq_get_current_driver())
605 /* capability check */
606 if (boot_cpu_has(X86_FEATURE_CPPC)) {
607 pr_debug("AMD CPPC MSR based functionality is supported\n");
608 amd_pstate_driver.adjust_perf = amd_pstate_adjust_perf;
609 } else if (shared_mem) {
610 static_call_update(amd_pstate_enable, cppc_enable);
611 static_call_update(amd_pstate_init_perf, cppc_init_perf);
612 static_call_update(amd_pstate_update_perf, cppc_update_perf);
614 pr_info("This processor supports shared memory solution, you can enable it with amd_pstate.shared_mem=1\n");
618 /* enable amd pstate feature */
619 ret = amd_pstate_enable(true);
621 pr_err("failed to enable amd-pstate with return %d\n", ret);
625 ret = cpufreq_register_driver(&amd_pstate_driver);
627 pr_err("failed to register amd_pstate_driver with return %d\n",
633 static void __exit amd_pstate_exit(void)
635 cpufreq_unregister_driver(&amd_pstate_driver);
637 amd_pstate_enable(false);
640 module_init(amd_pstate_init);
641 module_exit(amd_pstate_exit);
643 MODULE_AUTHOR("Huang Rui <ray.huang@amd.com>");
644 MODULE_DESCRIPTION("AMD Processor P-state Frequency Driver");
645 MODULE_LICENSE("GPL");