cpufreq: CPPC: Preserve OSPM-set registers across hotplug and unload
Subsystems (7)
Sumit Gupta
reviewerv7 cover cpufreq: CPPC: Preserve OSPM-set registers across hotplug and unload
This series preserves the OSPM-set CPPC registers (Energy Performance Preference, Autonomous Activity Window, Autonomous Selection - set via sysfs) across CPU hotplug and suspend/resume. On driver unload, it puts back the values they had before the driver loaded.
To do this, it keeps the CPPC cpufreq policy alive across CPU hotplug. It also orders the performance-control writes in cppc_set_perf(), so that restoring them after a platform reset never sends an invalid request.
Without online()/offline() callbacks, the core tears a policy down when its last CPU goes offline and rebuilds it on the way back, re-reading the CPPC capabilities each time. The values written to these registers can be lost:
- Across CPU hotplug or suspend/resume: the platform may reset them while the CPU is offline or suspended.
- On driver unload: the driver-written value is left in the register instead of returning to its pre-driver state.
Handle these with:
- Patch 1: orders the Desired, MIN and MAX writes in cppc_set_perf(), so that the platform never sees an invalid request. Christian proposed it in [7], and this version adds fixes found in testing.
- Patch 2: adds online()/offline() callbacks so the core keeps policy alive across CPU hotplug instead of tearing it down and rebuilding it.
- Patch 3: makes the autonomous selection register helpers take a u64.
- Patch 4: saves each register's firmware value at init() and each successful sysfs write as the requested value. offline() restores the firmware values, and online() reapplies the requested values.
- Patch 5: extends the same restore and reapply to system suspend/resume.
v6[6] -> v7:
- Patch 1:
- clamp Desired into MIN and MAX instead of rejecting it. (Sashiko)
- store_auto_select() updates its cache only on success. (Sashiko)
- Patch 2:
- exit() only frees the driver data. (Sashiko)
- online() continues if enabling CPPC fails, as init() does.
- Patch 4:
- save the requested value on each successful sysfs write, instead of reading it in offline(). (Christian, Rafael [8])
- init() fails if a firmware value cannot be read. (Christian)
- split the auto_act_window store from perf_limited.
- Patch 5:
- suspend() no longer reads and saves the registers.
Christian Loehle (1):
ACPI: CPPC: Order performance-control updates
Sumit Gupta (4):
cpufreq: CPPC: Keep the policy across CPU hotplug
ACPI: CPPC: Make autonomous selection helpers take a u64
cpufreq: CPPC: Preserve OSPM-set registers across hotplug and unload
cpufreq: CPPC: Preserve OSPM-set registers across suspend/resume
drivers/acpi/cppc_acpi.c | 318 +++++++++++++++----- drivers/cpufreq/amd-pstate.c | 2 +- drivers/cpufreq/cppc_cpufreq.c | 530 ++++++++++++++++++++++++++++++--- include/acpi/cppc_acpi.h | 9 +- 4 files changed, 729 insertions(+), 130 deletions(-)
[1] v1: https://lore.kernel.org/lkml/[email protected]/
[2] v2: https://lore.kernel.org/lkml/[email protected]/
[3] v3: https://lore.kernel.org/lkml/[email protected]/
[4] v4: https://lore.kernel.org/lkml/[email protected]/
[5] v5: https://lore.kernel.org/lkml/[email protected]/
[6] v6: https://lore.kernel.org/lkml/[email protected]/
[7] https://lore.kernel.org/lkml/[email protected]/
[8] https://lore.kernel.org/linux-pm/CAJZ5v0iXip_5Ywk3EkrGL_u2w_hOcGKA1XvrubLPb3diWHx5Lw@mail.gmail.com/
Signature · 1 line
2.34.1
Sumit Gupta
reviewerPATCH v7 2/5 cpufreq: CPPC: Keep the policy across CPU hotplug
Without online()/offline() callbacks, the cpufreq core calls exit() when a policy's last online CPU goes down. That drops the driver's per-policy data, which init() rebuilds when a CPU comes back.
Add lightweight online()/offline() callbacks so the core instead keeps the policy active and reuses the driver's cpu_data across CPU hotplug. init() then runs once instead of on every hotplug, making CPU hotplug faster.
This also lets offline() and online() restore and reapply register values across CPU hotplug. A later patch in this series uses this to preserve the OSPM-set registers.
Move what init() and exit() did on hotplug into the new callbacks:
- offline() requests the lowest performance and stops the frequency invariance updates, as exit() did. It also sets MIN to the lowest performance, so that Desired is not below MIN while the CPU is offline.
- online() re-enables CPPC and restores the performance controls, as the platform may have reset them. Failures are logged, not returned, as the core would free the policy. It also restarts the frequency invariance updates with a new counter snapshot, as init() did, so that no sample spans the offline window.
- exit() now only frees the driver data, as the core calls offline() before it.
The driver keeps a copy of the MIN and MAX it last wrote, so that fast_switch() can skip writing them again. online() marks that copy out of date before the restore, as the platform may have reset the limits, and updates it once the restore succeeds.
--- drivers/cpufreq/cppc_cpufreq.c | 106 ++++++++++++++++++++++++++++++--- 1 file changed, 97 insertions(+), 9 deletions(-)
drivers/cpufreq/cppc_cpufreq.c +97 -9
@@ -209,7 +209,7 @@ static void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)if (fie_disabled)return ; -/* policy->cpus will be empty here, use related_cpus instead */ +/* policy->cpus has only the online CPUs, so use related_cpus */ topology_clear_scale_freq_source (SCALE_FREQ_SOURCE_CPPC , policy->related_cpus);for_each_cpu (cpu, policy->related_cpus) {
@@ -807,24 +807,110 @@ static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) }static void cppc_cpufreq_cpu_exit (struct cpufreq_policy *policy) +{ +/* +* The core always calls offline() first, so only the driver data is +* left to free. +*/ +cppc_cpufreq_put_cpu_data (policy); +} + +/* +* Run when the policy's first CPU comes back online, the counterpart of +* offline(). +* +* The platform may have disabled CPPC and reset the performance controls +* (desired, min and max performance) while the CPU was offline. Re-enable +* CPPC and reprogram the controls. +* +* Log failures but do not return them, as the core would free the policy. +* Continue after a failure to enable CPPC, as the writes that follow fail +* on their own if the platform does not take them. A failed controls write +* is not fatal, as the next frequency change writes the controls again. +*/ +static int cppc_cpufreq_cpu_online (struct cpufreq_policy *policy) {struct cppc_cpudata *cpu_data = policy->driver_data; -struct cppc_perf_caps *caps = &cpu_data->perf_caps; +u32 desired_perf, min_perf, max_perf; +struct cppc_perf_ctrls perf_ctrls = {}; +unsigned int cpu = policy->cpu; +int ret; + +/* +* The platform may have reset MIN and MAX, so mark the copy that +* fast_switch() uses as out of date until the restore below succeeds. +*/ +WRITE_ONCE (cpu_data->perf_limits_valid, false); + + ret =cppc_set_enable (cpu, true); +if (ret && ret != -EOPNOTSUPP ) +pr_warn ("Failed to re-enable CPPC for CPU%u (%d)\n" , cpu, ret); + +/* +* Recompute min/max from the policy, clamp the last request into range, +* and reprogram the performance controls. Update the driver's copy only +* if the write succeeds. Otherwise the next frequency change writes the +* limits again. +*/ +cppc_cpufreq_get_perf_limits (cpu_data, policy, &min_perf, &max_perf); + desired_perf =clamp_t (u32, cpu_data->perf_ctrls.desired_perf, + min_perf, max_perf); + + perf_ctrls.desired_perf = desired_perf; + perf_ctrls.min_perf = min_perf; + perf_ctrls.max_perf = max_perf; + perf_ctrls.min_perf_valid = true; + + ret =cppc_set_perf (cpu, &perf_ctrls); +if (ret) { +pr_debug ("Failed to restore perf controls on CPU%u (%d)\n" , + cpu, ret); + }else { +WRITE_ONCE (cpu_data->perf_ctrls.desired_perf, desired_perf); +WRITE_ONCE (cpu_data->perf_ctrls.min_perf, min_perf); +WRITE_ONCE (cpu_data->perf_ctrls.max_perf, max_perf); +WRITE_ONCE (cpu_data->perf_limits_valid, true); + } + +/* Restart what offline() stopped, with a new counter snapshot. */ +cppc_cpufreq_cpu_fie_init (policy); + +return 0 ; +} + +/* +* Run when the policy's last online CPU goes down, the counterpart of online(). +* Having offline() makes the core keep the policy rather than call exit(). +*/ +static int cppc_cpufreq_cpu_offline (struct cpufreq_policy *policy) +{ +struct cppc_cpudata *cpu_data = policy->driver_data; +struct cppc_perf_ctrls perf_ctrls = cpu_data->perf_ctrls;unsigned int cpu = policy->cpu;int ret; +/* +* Stop the frequency invariance updates and cancel the pending work, so +* that no sample spans the offline window. online() restarts them with +* a new counter snapshot. +*/ cppc_cpufreq_cpu_fie_exit (policy); - cpu_data->perf_ctrls.desired_perf = caps->lowest_perf; - cpu_data->perf_ctrls.min_perf = caps->lowest_perf; - cpu_data->perf_ctrls.min_perf_valid = true; +/* +* Request the lowest performance while the policy has no online CPU, +* lowering MIN with it so that the request stays a valid tuple. MAX +* keeps the value the policy last programmed. +*/ + perf_ctrls.desired_perf = cpu_data->perf_caps.lowest_perf; + perf_ctrls.min_perf = cpu_data->perf_caps.lowest_perf; + perf_ctrls.min_perf_valid = true; - ret =cppc_set_perf (cpu, &cpu_data->perf_ctrls); + ret =cppc_set_perf (cpu, &perf_ctrls);if (ret) -pr_debug ("Err setting perf value:%d on CPU:%d. ret:%d\n" , - caps->lowest_perf, cpu, ret); +pr_debug ("Err setting perf value:%u on CPU:%u. ret:%d\n" , + cpu_data->perf_caps.lowest_perf, cpu, ret); -cppc_cpufreq_put_cpu_data (policy); +return 0 ; }static int cppc_cpufreq_cpu_resume (struct cpufreq_policy *policy)
@@ -1141,6 +1227,8 @@ static struct cpufreq_driver cppc_cpufreq_driver = { .fast_switch = cppc_cpufreq_fast_switch, .init = cppc_cpufreq_cpu_init, .exit = cppc_cpufreq_cpu_exit, + .online = cppc_cpufreq_cpu_online, + .offline = cppc_cpufreq_cpu_offline, .resume = cppc_cpufreq_cpu_resume, .set_boost = cppc_cpufreq_set_boost, .attr = cppc_cpufreq_attr,
Signature · 1 line
2.34.1
Sumit Gupta
reviewerPATCH v7 3/5 ACPI: CPPC: Make autonomous selection helpers take a u64
In reply to Sumit Guptacppc_get_auto_sel()/cppc_set_auto_sel() use a bool, unlike the other CPPC register get/set helpers which use a u64.
The next patch in this series saves and restores the OSPM-set registers across CPU hotplug and driver unload through a common table of register get/set helpers that all take a u64. The bool autonomous selection helpers cannot be added to that table.
Change cppc_get_auto_sel()/cppc_set_auto_sel() to take a u64 so the autonomous selection register fits alongside the others, and update their callers.
--- drivers/acpi/cppc_acpi.c | 20 ++++---------------- drivers/cpufreq/amd-pstate.c | 2 +- drivers/cpufreq/cppc_cpufreq.c | 4 ++-- include/acpi/cppc_acpi.h | 8 ++++---- 4 files changed, 11 insertions(+), 23 deletions(-)
drivers/acpi/cppc_acpi.c +4 -16
@@ -3304,23 +3304,11 @@ EXPORT_SYMBOL_GPL(cppc_set_auto_act_window);/** * cppc_get_auto_sel() - Read autonomous selection register. * @cpu: CPU from which to read register. -* @enable: Return address. +* @enable: Return address, set to 0 or 1. */ -int cppc_get_auto_sel (int cpu,bool *enable) +int cppc_get_auto_sel (int cpu,u64 *enable) { -u64 auto_sel; -int ret; - -if (enable ==NULL ) -return -EINVAL ; - - ret =cppc_get_reg_val (cpu,AUTO_SEL_ENABLE , &auto_sel); -if (ret) -return ret; - - *enable = (bool )auto_sel; - -return 0 ; +return cppc_get_reg_val (cpu,AUTO_SEL_ENABLE , enable); }EXPORT_SYMBOL_GPL (cppc_get_auto_sel);
@@ -3351,7 +3339,7 @@ EXPORT_SYMBOL_GPL(cppc_auto_sel_is_immutable);* @cpu : CPU to which to write register. * @enable : the desired value of autonomous selection resiter to be updated. */ -int cppc_set_auto_sel (int cpu,bool enable) +int cppc_set_auto_sel (int cpu,u64 enable) {return cppc_set_reg_val (cpu,AUTO_SEL_ENABLE , enable); }
drivers/cpufreq/amd-pstate.c +1 -1
@@ -546,7 +546,7 @@ static int shmem_init_perf(struct amd_cpudata *cpudata)struct cppc_perf_caps cppc_perf;union perf_cached perf =READ_ONCE (cpudata->perf);u64 numerator; -bool auto_sel; +u64 auto_sel;int ret =cppc_get_perf_caps (cpudata->cpu, &cppc_perf);if (ret)
drivers/cpufreq/cppc_cpufreq.c +2 -2
@@ -1033,7 +1033,7 @@ static ssize_t show_freqdomain_cpus(struct cpufreq_policy *policy, char *buf)static ssize_t show_auto_select (struct cpufreq_policy *policy,char *buf) { -bool val; +u64 val;int ret; ret =cppc_get_auto_sel (policy->cpu, &val);
@@ -1045,7 +1045,7 @@ static ssize_t show_auto_select(struct cpufreq_policy *policy, char *buf)if (ret)return ret; -return sysfs_emit (buf,"%d\n" , val); +return sysfs_emit (buf,"%llu\n" , val); } static ssize_t store_auto_select(struct cpufreq_policy *policy,
include/acpi/cppc_acpi.h +4 -4
@@ -197,9 +197,9 @@ extern int cppc_set_epp_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls, bool eextern int cppc_set_epp (int cpu,u64 epp_val);extern int cppc_get_auto_act_window (int cpu,u64 *auto_act_window);extern int cppc_set_auto_act_window (int cpu,u64 auto_act_window); -extern int cppc_get_auto_sel (int cpu,bool *enable); +extern int cppc_get_auto_sel (int cpu,u64 *enable);bool cppc_auto_sel_is_immutable (int cpu); -extern int cppc_set_auto_sel (int cpu,bool enable); +extern int cppc_set_auto_sel (int cpu,u64 enable);extern int cppc_get_perf_limited (int cpu,u64 *perf_limited);extern int cppc_set_perf_limited (int cpu,u64 bits_to_clear);extern int amd_get_highest_perf (unsigned int cpu,u32 *highest_perf);
@@ -298,7 +298,7 @@ static inline int cppc_set_auto_act_window(int cpu, u64 auto_act_window) {return -EOPNOTSUPP ; } -static inline int cppc_get_auto_sel (int cpu,bool *enable) +static inline int cppc_get_auto_sel (int cpu,u64 *enable) {return -EOPNOTSUPP ; }
@@ -308,7 +308,7 @@ static inline bool cppc_auto_sel_is_immutable(int cpu)return false; } -static inline int cppc_set_auto_sel (int cpu,bool enable) +static inline int cppc_set_auto_sel (int cpu,u64 enable) {return -EOPNOTSUPP ; }
Signature · 1 line
2.34.1
Sumit Gupta
reviewerPATCH v7 4/5 cpufreq: CPPC: Preserve OSPM-set registers across hotplug and unload
In reply to Sumit GuptaValues written to OSPM-set CPPC registers via sysfs are not handled in two cases:
- Across CPU hotplug: the platform may reset a CPU's registers while it is offline, and the value is lost.
- On driver unload: the value stays in the register instead of returning to its pre-driver state.
Handle both by saving a firmware value and a requested value for each register:
- On init(), save each register's firmware value before the driver programs anything. Also use it as the requested value. If the OS never writes a register and the platform resets it while the CPU is offline, online() then restores the firmware value.
- Save each successful sysfs write as the requested value.
- On offline(), restore the firmware values.
- On online(), reapply the requested values after reprogramming the performance controls.
- On exit(), nothing is needed, as the core calls offline() first, which already restored the firmware values.
The value reapplied at online() is the last one the OS wrote, and it stays in effect until userspace changes it. The performance controls are not reapplied this way. online() recomputes them from the current policy limits.
Also split the auto_act_window store out of the handler it shared with perf_limited, so that it can save the requested value. perf_limited is not tracked, as writing it only clears status bits.
Cover the Autonomous Selection (auto_sel), Energy Performance Preference (EPP) and Autonomous Activity Window (auto_act_window) registers. Writes to EPP and auto_act_window only have meaning while auto_sel is enabled, so write auto_sel before them when enabling it and after them when disabling it. While autonomous selection stays disabled, the platform may ignore those writes.
--- drivers/cpufreq/cppc_cpufreq.c | 227 ++++++++++++++++++++++++++++++++- 1 file changed, 222 insertions(+), 5 deletions(-)
drivers/cpufreq/cppc_cpufreq.c +222 -5
@@ -29,6 +29,185 @@static struct cpufreq_driver cppc_cpufreq_driver; +/* +* OSPM-set CPPC registers tracked for save/restore. A value the OS wrote is +* reapplied from online() across CPU hotplug, and the firmware value is +* restored from offline(). +* +* Autonomous Selection (auto_sel) is kept first, as the registers after it +* only have meaning while it is enabled. +*/ +enum cppc_saved_reg_id { +CPPC_SAVED_AUTO_SEL , +CPPC_SAVED_EPP , +CPPC_SAVED_AUTO_ACT_WINDOW , +CPPC_NR_SAVED_REGS , +}; + +struct cppc_saved_reg { +const char *name; +int (*get)(int cpu,u64 *val); +int (*set)(int cpu,u64 val); +}; + +static const struct cppc_saved_reg cppc_saved_regs[CPPC_NR_SAVED_REGS ] = { + [CPPC_SAVED_AUTO_SEL ] = { + .name ="auto_sel" , + .get = cppc_get_auto_sel, + .set = cppc_set_auto_sel, + }, + [CPPC_SAVED_EPP ] = { + .name ="epp" , + .get = cppc_get_epp_perf, + .set = cppc_set_epp, + }, + [CPPC_SAVED_AUTO_ACT_WINDOW ] = { + .name ="auto_act_window" , + .get = cppc_get_auto_act_window, + .set = cppc_set_auto_act_window, + }, +}; + +enum cppc_saved_type { +CPPC_SAVED_FIRMWARE , +CPPC_SAVED_REQUESTED , +CPPC_NR_SAVED_TYPES , +}; + +/* +* Per-policy values saved for each register in cppc_saved_regs[], U64_MAX +* if the register is not implemented or not readable: +* CPPC_SAVED_FIRMWARE - value before the driver touched it, saved at +* init() and written back when the policy goes +* offline +* CPPC_SAVED_REQUESTED - value the OS asked for, reapplied at online(). +* It starts as the firmware value and changes only +* when a write from the OS succeeds. +*/ +struct cppc_policy_state { +u64 regs[CPPC_NR_SAVED_REGS ][CPPC_NR_SAVED_TYPES ]; +}; + +static DEFINE_PER_CPU(struct cppc_policy_state, cppc_policy_state); + +/* +* Per-policy state is kept in the per-CPU variable of the first CPU the policy +* manages. related_cpus (the policy's full set of CPUs) never changes while the +* policy exists, so this CPU (unlike policy->cpu) stays the same across CPU +* hotplug, and every callback reaches the same copy. +*/ +static struct cppc_policy_state * +cppc_cpufreq_policy_state (struct cpufreq_policy *policy) +{ +const struct cpumask *policy_cpus = policy->related_cpus; + +/* +* related_cpus is empty during init(), as the core fills it in only +* after init() returns. Use policy->cpus then, which has the same +* first CPU. +*/ +if (cpumask_empty (policy_cpus)) + policy_cpus = policy->cpus; + +return &per_cpu (cppc_policy_state,cpumask_first (policy_cpus)); +} + +/* +* Save each tracked register's firmware value before the driver programs +* anything. Also start the requested value from it. If the OS never writes a +* register and the platform resets it while the CPU is offline, online() then +* writes the firmware value back. +* +* Skip a register that is not implemented or not readable. Fail on any other +* read error, as the firmware value is needed to put the register back later. +*/ +static int cppc_cpufreq_save_firmware_regs (struct cpufreq_policy *policy) +{ +struct cppc_policy_state *st =cppc_cpufreq_policy_state (policy); +unsigned int cpu = policy->cpu; +u64 val; +int i, ret; + +for (i =0 ; i <CPPC_NR_SAVED_REGS ; i++) { + ret = cppc_saved_regs[i].get (cpu, &val); +if (ret == -EOPNOTSUPP ) + val =U64_MAX ; +else if (ret) +return ret; + + st->regs[i][CPPC_SAVED_FIRMWARE ] = val; + st->regs[i][CPPC_SAVED_REQUESTED ] = val; + } + +return 0 ; +} + +/* +* After a successful write to a tracked register, save the value so that +* online() can reapply it. +*/ +static void cppc_cpufreq_save_requested (struct cpufreq_policy *policy, +enum cppc_saved_reg_id reg,u64 val) +{ +cppc_cpufreq_policy_state (policy)->regs[reg][CPPC_SAVED_REQUESTED ] = val; +} + +/* +* Write one tracked register, skipping it when there is no saved value. +* A register the platform does not allow writing is not an error. +*/ +static void cppc_cpufreq_write_saved_reg (unsigned int cpu, +enum cppc_saved_reg_id reg,u64 val, +enum cppc_saved_type saved_type) +{ +const char *op = (saved_type ==CPPC_SAVED_FIRMWARE ) ? +"restore firmware" :"reapply requested" ; +int ret; + +if (val ==U64_MAX ) +return ; + + ret = cppc_saved_regs[reg].set (cpu, val); +if (ret == -EOPNOTSUPP ) +return ; +if (ret) +pr_debug ("Failed to %s %s=%llu on CPU%u (%d)\n" , op, + cppc_saved_regs[reg].name, val, cpu, ret); +} + +/* +* Apply the saved firmware or requested value to each tracked register. +* +* Write auto_sel first when the value being applied enables autonomous +* selection and last when it disables it, so the writes to the dependent +* registers can still take effect. While autonomous selection stays disabled, +* the platform may ignore those writes. Do not enable it temporarily to force +* them through. +*/ +static void cppc_cpufreq_apply_saved_regs (struct cpufreq_policy *policy, +enum cppc_saved_type saved_type) +{ +const struct cppc_policy_state *st =cppc_cpufreq_policy_state (policy); +unsigned int cpu = policy->cpu; +u64 auto_sel, val; +int i; + + auto_sel = st->regs[CPPC_SAVED_AUTO_SEL ][saved_type]; + +if (auto_sel) +cppc_cpufreq_write_saved_reg (cpu,CPPC_SAVED_AUTO_SEL , auto_sel, + saved_type); + +for (i =CPPC_SAVED_AUTO_SEL +1 ; i <CPPC_NR_SAVED_REGS ; i++) { + val = st->regs[i][saved_type]; +cppc_cpufreq_write_saved_reg (cpu, i, val, saved_type); + } + +if (!auto_sel) +cppc_cpufreq_write_saved_reg (cpu,CPPC_SAVED_AUTO_SEL , auto_sel, + saved_type); +} + #ifdef CONFIG_ACPI_CPPC_CPUFREQ_FIE static enum { FIE_UNSET = -1,
@@ -790,6 +969,12 @@ static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) policy->cur =cppc_perf_to_khz (caps, desired_perf); cpu_data->perf_ctrls.desired_perf = desired_perf; + ret =cppc_cpufreq_save_firmware_regs (policy); +if (ret) { +pr_debug ("Err reading CPU%d firmware values: ret:%d\n" , cpu, ret); + goto out; + } + ret =cppc_set_perf (cpu, &cpu_data->perf_ctrls);if (ret) { pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
@@ -821,12 +1006,14 @@ static void cppc_cpufreq_cpu_exit(struct cpufreq_policy *policy)* * The platform may have disabled CPPC and reset the performance controls * (desired, min and max performance) while the CPU was offline. Re-enable -* CPPC and reprogram the controls. +* CPPC, reprogram the controls and reapply the requested values of the +* OSPM-set registers, which offline() reset to firmware values. * * Log failures but do not return them, as the core would free the policy. * Continue after a failure to enable CPPC, as the writes that follow fail -* on their own if the platform does not take them. A failed controls write -* is not fatal, as the next frequency change writes the controls again. +* on their own if the platform does not take them. If the controls write +* fails, still reapply the OSPM-set registers, as frequency changes rewrite +* only the controls. */ static int cppc_cpufreq_cpu_online (struct cpufreq_policy *policy) {
@@ -872,6 +1059,8 @@ static int cppc_cpufreq_cpu_online(struct cpufreq_policy *policy)WRITE_ONCE (cpu_data->perf_limits_valid, true); } +cppc_cpufreq_apply_saved_regs (policy,CPPC_SAVED_REQUESTED ); +/* Restart what offline() stopped, with a new counter snapshot. */ cppc_cpufreq_cpu_fie_init (policy);
@@ -889,6 +1078,9 @@ static int cppc_cpufreq_cpu_offline(struct cpufreq_policy *policy)unsigned int cpu = policy->cpu;int ret; +/* Leave the platform in its pre-driver state. */ +cppc_cpufreq_apply_saved_regs (policy,CPPC_SAVED_FIRMWARE ); +/* * Stop the frequency invariance updates and cancel the pending work, so * that no sample spans the offline window. online() restarts them with
@@ -1097,6 +1289,8 @@ static ssize_t store_auto_select(struct cpufreq_policy *policy,WRITE_ONCE (cpu_data->perf_limits_valid, true); } +cppc_cpufreq_save_requested (policy,CPPC_SAVED_AUTO_SEL , val); +return count; }
@@ -1144,8 +1338,30 @@ static ssize_t store_##_name(struct cpufreq_policy *policy, \ buf, count); \ } -CPPC_CPUFREQ_ATTR_RW_U64 (auto_act_window, cppc_get_auto_act_window, - cppc_set_auto_act_window) +static ssize_t show_auto_act_window (struct cpufreq_policy *policy,char *buf) +{ +return cppc_cpufreq_sysfs_show_u64 (policy->cpu, cppc_get_auto_act_window, + buf); +} + +static ssize_t store_auto_act_window (struct cpufreq_policy *policy, +const char *buf,size_t count) +{ +u64 val; +int ret; + + ret =kstrtou64 (buf,0 , &val); +if (ret) +return ret; + + ret =cppc_set_auto_act_window (policy->cpu, val); +if (ret) +return ret; + +cppc_cpufreq_save_requested (policy,CPPC_SAVED_AUTO_ACT_WINDOW , val); + +return count; +}static ssize_t show_energy_performance_preference_val (struct cpufreq_policy *policy,char *buf)
@@ -1170,6 +1386,7 @@ store_energy_performance_preference_val(struct cpufreq_policy *policy,return ret; cpu_data->perf_ctrls.energy_perf = val; +cppc_cpufreq_save_requested (policy,CPPC_SAVED_EPP , val);return count; }
Signature · 1 line
2.34.1
Sumit Gupta
reviewerPATCH v7 5/5 cpufreq: CPPC: Preserve OSPM-set registers across suspend/resume
In reply to Sumit GuptaSome platforms reset the OSPM-set registers and the performance controls across system suspend, losing the values the OS set. The hotplug callbacks do not cover this. offline() runs only when a policy's last CPU goes down. The boot CPU stays up during suspend-to-RAM, so offline() never runs for its policy. During suspend-to-idle, no CPU goes down at all.
The cpufreq core calls the driver's suspend() for every active policy before any CPU goes offline, and resume() after the CPUs come back. Use them to restore and reapply the registers, as offline() and online() do:
- suspend() restores the firmware values and sets a per-policy flag to record it. It also stops the frequency invariance updates, so that no sample spans the suspend.
- offline() skips the firmware restore while the flag is set, as suspend() has already done it. The rest of offline() still runs.
- online() does the same as after hotplug. It re-enables CPPC and reprograms the performance controls. It reapplies the requested values and restarts the frequency invariance updates. It also clears the flag, so that the next offline() restores the firmware values again.
- resume() calls online() only if the flag is still set. This covers the boot CPU's policy in suspend-to-RAM and every policy in suspend-to-idle. For any other policy, online() already ran when its first CPU came back.
resume() no longer marks the copy of MIN and MAX that fast_switch() uses as out of date. online() does it, and every policy goes through online() on the way back, from CPU hotplug or from resume().
Also make fie_init() cover related_cpus, as fie_exit() does. suspend() stops the updates on all CPUs of the policy, even offline ones. When resume() calls online(), policy->cpus holds only the online CPUs. An offline CPU that comes up later does not trigger online() again, as its policy is already active. Without this, its updates would never restart.
--- drivers/cpufreq/cppc_cpufreq.c | 74 ++++++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 12 deletions(-)
drivers/cpufreq/cppc_cpufreq.c +62 -12
@@ -86,6 +86,12 @@ enum cppc_saved_type {*/ struct cppc_policy_state {u64 regs[CPPC_NR_SAVED_REGS ][CPPC_NR_SAVED_TYPES ]; +/* +* Set by suspend() after it restores the firmware values. While it is +* set, offline() skips that restore and resume() calls online(). +* Cleared by init() and online(). +*/ +bool suspend_regs_handled; }; static DEFINE_PER_CPU(struct cppc_policy_state, cppc_policy_state);
@@ -332,6 +338,7 @@ static struct scale_freq_data cppc_sftd_pcc = {static void cppc_cpufreq_cpu_fie_init (struct cpufreq_policy *policy) { +const struct cpumask *cpus = policy->related_cpus;struct scale_freq_data *sftd;struct cppc_freq_invariance *cppc_fi;int cpu, ret;
@@ -339,7 +346,15 @@ static void cppc_cpufreq_cpu_fie_init(struct cpufreq_policy *policy)if (fie_disabled)return ; -for_each_cpu (cpu, policy->cpus) { +/* +* When resume() calls online(), policy->cpus has only the online CPUs. +* online() does not run again for a CPU in the same policy that comes +* up later, so use related_cpus. It is empty only during init(). +*/ +if (cpumask_empty (cpus)) + cpus = policy->cpus; + +for_each_cpu (cpu, cpus) { cppc_fi = &per_cpu (cppc_freq_inv, cpu); cppc_fi->cpu = cpu; cppc_fi->cpu_data = policy->driver_data;
@@ -354,10 +369,7 @@ static void cppc_cpufreq_cpu_fie_init(struct cpufreq_policy *policy) ret =cppc_get_perf_ctrs (cpu, &cppc_fi->prev_perf_fb_ctrs); -/* -* Don't abort as the CPU was offline while the driver was -* getting registered. -*/ +/* Don't abort for a CPU that is offline. */ if (ret &&cpu_online (cpu)) {pr_debug ("%s: failed to read perf counters for cpu:%d: %d\n" , __func__, cpu, ret);
@@ -366,7 +378,7 @@ static void cppc_cpufreq_cpu_fie_init(struct cpufreq_policy *policy) }/* A shared policy may contain both PCC and non-PCC counters. */ -for_each_cpu (cpu, policy->cpus) { +for_each_cpu (cpu, cpus) { cppc_fi = &per_cpu (cppc_freq_inv, cpu);if (cppc_fi->pcc_work_initialized) sftd = &cppc_sftd_pcc;
@@ -969,6 +981,7 @@ static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) policy->cur =cppc_perf_to_khz (caps, desired_perf); cpu_data->perf_ctrls.desired_perf = desired_perf; +cppc_cpufreq_policy_state (policy)->suspend_regs_handled = false; ret =cppc_cpufreq_save_firmware_regs (policy);if (ret) {pr_debug ("Err reading CPU%d firmware values: ret:%d\n" , cpu, ret);
@@ -1023,6 +1036,8 @@ static int cppc_cpufreq_cpu_online(struct cpufreq_policy *policy)unsigned int cpu = policy->cpu;int ret; +cppc_cpufreq_policy_state (policy)->suspend_regs_handled = false; +/* * The platform may have reset MIN and MAX, so mark the copy that * fast_switch() uses as out of date until the restore below succeeds.
@@ -1078,8 +1093,12 @@ static int cppc_cpufreq_cpu_offline(struct cpufreq_policy *policy)unsigned int cpu = policy->cpu;int ret; -/* Leave the platform in its pre-driver state. */ -cppc_cpufreq_apply_saved_regs (policy,CPPC_SAVED_FIRMWARE ); +/* +* Leave the platform in its pre-driver state, unless suspend() already +* did so earlier in this suspend cycle. +*/ +if (!cppc_cpufreq_policy_state (policy)->suspend_regs_handled) +cppc_cpufreq_apply_saved_regs (policy,CPPC_SAVED_FIRMWARE );/* * Stop the frequency invariance updates and cancel the pending work, so
@@ -1105,16 +1124,46 @@ static int cppc_cpufreq_cpu_offline(struct cpufreq_policy *policy)return 0 ; } -static int cppc_cpufreq_cpu_resume (struct cpufreq_policy *policy) +/* +* Run for every active policy when the system suspends, before any CPU goes +* offline. +* +* Restore the firmware values. Doing it here covers a policy that keeps a CPU +* online, for which offline() never runs. offline() does not write them again +* in the same suspend cycle. +* +* Stop the frequency invariance updates too, so that no sample spans the +* suspend window. online() restarts them on the way back, from hotplug or +* from resume(). +*/ +static int cppc_cpufreq_cpu_suspend (struct cpufreq_policy *policy) { -struct cppc_cpudata *cpu_data = policy->driver_data; +cppc_cpufreq_apply_saved_regs (policy,CPPC_SAVED_FIRMWARE ); +cppc_cpufreq_policy_state (policy)->suspend_regs_handled = true; -/* The platform may have reset the controls during suspend. */ -WRITE_ONCE (cpu_data->perf_limits_valid, false); +cppc_cpufreq_cpu_fie_exit (policy);return 0 ; } +/* +* Run the online() restore for a policy that kept a CPU online through +* suspend. +* +* For any other policy, online() ran when its first CPU came back, before the +* core calls resume(). That cleared the flag. Only a policy that still has it +* set needs online() here. Either way every policy goes through online(), so +* resume() does not need to mark the copy of MIN and MAX that fast_switch() +* uses as out of date. +*/ +static int cppc_cpufreq_cpu_resume (struct cpufreq_policy *policy) +{ +if (!cppc_cpufreq_policy_state (policy)->suspend_regs_handled) +return 0 ; + +return cppc_cpufreq_cpu_online (policy); +} +static inline u64 get_delta (u64 t1,u64 t0) {if (t1 > t0 || t0 > ~(u32 )0 )
@@ -1446,6 +1495,7 @@ static struct cpufreq_driver cppc_cpufreq_driver = { .exit = cppc_cpufreq_cpu_exit, .online = cppc_cpufreq_cpu_online, .offline = cppc_cpufreq_cpu_offline, + .suspend = cppc_cpufreq_cpu_suspend, .resume = cppc_cpufreq_cpu_resume, .set_boost = cppc_cpufreq_set_boost, .attr = cppc_cpufreq_attr,
Signature · 1 line
2.34.1
Sumit Gupta
reviewerPATCH v7 1/5 ACPI: CPPC: Order performance-control updates
In reply to Sumit GuptaFrom: Christian Loehle <[email protected]>
The ACPI specification requires Minimum Performance to remain below Maximum Performance. When autonomous selection is disabled, Desired Performance must also remain within those limits.
cppc_set_perf() writes the three controls in a fixed order. This can expose an invalid tuple after firmware resets the controls or while policy bounds move across the current desired value.
Read the live limits. Apply non-atomic updates in three phases: widen the interval, update Desired Performance, then narrow it. Submit PCC controls in each phase together. Keep the existing single-command path when the complete tuple is in PCC, reject invalid requests, and stop after the first failed access.
Make cppc_cpufreq fast switches include the limits only when they differ from the last successful request. Invalidate the cache across resume and after a failed request, since firmware may have reset the controls or an earlier phase of a failed update may have reached the platform. Publish slow-path limits only after the complete request succeeds.
At policy teardown, lower Minimum Performance together with Desired Performance so the final request remains valid. On initialization, clamp Desired Performance to the live interval left by firmware or an earlier driver instance.
[sumitg:
- In cppc_read_perf_limits(), skip an unimplemented MIN or MAX, and do not fail on a live MIN above MAX.
- Clamp Desired into the new MIN and MAX instead of rejecting it, as amd-pstate resume can round it above MAX.
- In init(), request the full range if the live MAX is zero or below MIN.
- Clamp Desired in set_target(), fast_switch() and store_auto_select(), to keep the cached value in range.
- In store_auto_select(), publish the request only after success.]
--- drivers/acpi/cppc_acpi.c | 298 ++++++++++++++++++++++++++------- drivers/cpufreq/cppc_cpufreq.c | 141 ++++++++++++---- include/acpi/cppc_acpi.h | 1 + 3 files changed, 348 insertions(+), 92 deletions(-)
drivers/acpi/cppc_acpi.c +235 -63
@@ -3471,11 +3471,214 @@ int cppc_get_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls) }EXPORT_SYMBOL_GPL (cppc_get_perf); +enum cppc_perf_update { +CPPC_UPDATE_DESIRED =BIT (0 ), +CPPC_UPDATE_MIN =BIT (1 ), +CPPC_UPDATE_MAX =BIT (2 ), +}; + +/* The caller holds pcc_lock for write if either limit is in PCC. */ +static int cppc_read_perf_limits (int cpu,u32 *min_perf,u32 *max_perf) +{ +struct cpc_desc *cpc_desc =per_cpu (cpc_desc_ptr, cpu); +struct cpc_register_resource *min_reg = &cpc_desc->cpc_regs[MIN_PERF ]; +struct cpc_register_resource *max_reg = &cpc_desc->cpc_regs[MAX_PERF ]; +u64 min =0 , max =U32_MAX ; +int ret; + +if (!cpc_is_readable (min_reg) || !cpc_is_readable (max_reg)) +return -EOPNOTSUPP ; + +/* Firmware may have reset the controls, so read the live limits. */ +if (CPC_IN_PCC (min_reg) ||CPC_IN_PCC (max_reg)) { + ret =send_pcc_cmd (per_cpu (cpu_pcc_subspace_idx, cpu),CMD_READ ); +if (ret) +return ret; + } + +/* An absent optional limit leaves that side of the interval open. */ +if (CPC_SUPPORTED (min_reg)) { + ret =cpc_read (cpu, min_reg, &min); +if (ret) +return ret; + } +if (CPC_SUPPORTED (max_reg)) { + ret =cpc_read (cpu, max_reg, &max); +if (ret) +return ret; + } +if (min >U32_MAX || max >U32_MAX ) +return -EFAULT ; +if (min > max) +pr_debug ("CPU:%d live MIN_PERF %llu above MAX_PERF %llu\n" , + cpu, min, max); + + *min_perf = min; + *max_perf = max; +return 0 ; +} + +static bool cppc_perf_updates_in_pcc (int cpu,unsigned int updates) +{ +struct cpc_desc *cpc_desc =per_cpu (cpc_desc_ptr, cpu); + +return ((updates &CPPC_UPDATE_DESIRED ) && +CPC_IN_PCC (&cpc_desc->cpc_regs[DESIRED_PERF ])) || + ((updates &CPPC_UPDATE_MIN ) && +CPC_IN_PCC (&cpc_desc->cpc_regs[MIN_PERF ])) || + ((updates &CPPC_UPDATE_MAX ) && +CPC_IN_PCC (&cpc_desc->cpc_regs[MAX_PERF ])); +} + +/* The caller holds pcc_lock for write if an updated control is in PCC. */ +static int cppc_write_perf_controls (int cpu, +struct cppc_perf_ctrls *perf_ctrls, +unsigned int updates,bool sync_desired) +{ +struct cpc_desc *cpc_desc =per_cpu (cpc_desc_ptr, cpu); +struct cpc_register_resource *desired_reg = + &cpc_desc->cpc_regs[DESIRED_PERF ]; +struct cpc_register_resource *min_reg = &cpc_desc->cpc_regs[MIN_PERF ]; +struct cpc_register_resource *max_reg = &cpc_desc->cpc_regs[MAX_PERF ]; +struct cppc_pcc_data *pcc_ss_data; +int pcc_ss_id, ret; +bool pcc_update = false; + +/* Direct writes precede the PCC command in each ordered phase. */ +if ((updates &CPPC_UPDATE_DESIRED ) && !CPC_IN_PCC (desired_reg)) { + ret =cpc_write (cpu, desired_reg, perf_ctrls->desired_perf); +if (ret) +return ret; + } +if ((updates &CPPC_UPDATE_MIN ) && !CPC_IN_PCC (min_reg)) { + ret =cpc_write (cpu, min_reg, perf_ctrls->min_perf); +if (ret) +return ret; + } +if ((updates &CPPC_UPDATE_MAX ) && !CPC_IN_PCC (max_reg)) { + ret =cpc_write (cpu, max_reg, perf_ctrls->max_perf); +if (ret) +return ret; + } + +/* Keep Desired Performance valid in every intermediate PCC command. */ +if (((updates &CPPC_UPDATE_DESIRED ) || sync_desired) && +CPC_IN_PCC (desired_reg)) { + ret =cpc_write (cpu, desired_reg, perf_ctrls->desired_perf); +if (ret) +return ret; + pcc_update = true; + } +if ((updates &CPPC_UPDATE_MIN ) &&CPC_IN_PCC (min_reg)) { + ret =cpc_write (cpu, min_reg, perf_ctrls->min_perf); +if (ret) +return ret; + pcc_update = true; + } +if ((updates &CPPC_UPDATE_MAX ) &&CPC_IN_PCC (max_reg)) { + ret =cpc_write (cpu, max_reg, perf_ctrls->max_perf); +if (ret) +return ret; + pcc_update = true; + } +if (!pcc_update) +return 0 ; + + pcc_ss_id =per_cpu (cpu_pcc_subspace_idx, cpu); + pcc_ss_data = pcc_data[pcc_ss_id]; +WRITE_ONCE (pcc_ss_data->pending_pcc_write_cmd, true); + cpc_desc->write_cmd_id = pcc_ss_data->pcc_write_cnt; + cpc_desc->write_cmd_status =0 ; +return send_pcc_cmd (pcc_ss_id,CMD_WRITE ); +} + +/* +* Move from the live interval to the requested interval without exposing an +* invalid control tuple: widen, update Desired Performance, then narrow. +*/ +static int cppc_write_ordered_perf (int cpu, +struct cppc_perf_ctrls *perf_ctrls, +bool desired_update,bool min_update, +bool max_update) +{ +struct cpc_desc *cpc_desc =per_cpu (cpc_desc_ptr, cpu); +struct cpc_register_resource *desired_reg = + &cpc_desc->cpc_regs[DESIRED_PERF ]; +struct cppc_perf_ctrls ctrls = *perf_ctrls; +unsigned int widen =0 , narrow =0 ; +u32 current_min, current_max; +u32 target_min, target_max; +bool desired_done = false; +bool sync_desired; +int ret; + + ret =cppc_read_perf_limits (cpu, ¤t_min, ¤t_max); +if (ret) +return ret; + + target_min = min_update ? perf_ctrls->min_perf : current_min; + target_max = max_update ? perf_ctrls->max_perf : current_max; + +/* +* Check only the new interval. If the live one is inverted, this +* update fixes it. +*/ +if (target_min > target_max) +return -EINVAL ; + +/* Callers can round Desired just outside the new interval. */ +if (desired_update && ctrls.desired_perf) + ctrls.desired_perf =clamp (ctrls.desired_perf, target_min, + target_max); + +if (min_update) { +if (perf_ctrls->min_perf < current_min) + widen |=CPPC_UPDATE_MIN ; +else + narrow |=CPPC_UPDATE_MIN ; + } +if (max_update) { +if (perf_ctrls->max_perf > current_max) + widen |=CPPC_UPDATE_MAX ; +else + narrow |=CPPC_UPDATE_MAX ; + } + +if (widen) { + sync_desired = desired_update &&CPC_IN_PCC (desired_reg) && +cppc_perf_updates_in_pcc (cpu, widen); + ret =cppc_write_perf_controls (cpu, &ctrls, widen, + sync_desired); +if (ret) +return ret; + desired_done = sync_desired; + } + +if (desired_update && !desired_done) { + ret =cppc_write_perf_controls (cpu, &ctrls, +CPPC_UPDATE_DESIRED , false); +if (ret) +return ret; + } + +if (!narrow) +return 0 ; + + sync_desired = desired_update &&CPC_IN_PCC (desired_reg) && +cppc_perf_updates_in_pcc (cpu, narrow); +return cppc_write_perf_controls (cpu, &ctrls, narrow, sync_desired); +} +/** * cppc_set_perf - Set a CPU's performance controls. * @cpu: CPU for which to set performance controls. * @perf_ctrls: ptr to cppc_perf_ctrls. See cppc_acpi.h * +* Callers must serialize updates to the same CPU's performance controls. +* Limit updates preserve MIN <= MAX, including intermediate states on +* directly accessed registers. An error may leave an earlier write applied. +* A nonzero Desired is clamped to the new [MIN, MAX] when a limit is updated. +* * Return: 0 for success, -ERRNO otherwise. */ int cppc_set_perf (int cpu,struct cppc_perf_ctrls *perf_ctrls)
@@ -3487,6 +3690,7 @@ int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls)bool desired_update, min_update, max_update;bool desired_pcc, min_pcc, max_pcc, pcc_update;bool pcc_layout, direct_layout, mixed_layout; +u32 desired_perf;int ret =0 ;if (!cpc_desc) {
@@ -3502,10 +3706,21 @@ int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls) (perf_ctrls->min_perf || perf_ctrls->min_perf_valid); max_update =cpc_is_writable (max_perf_reg) && perf_ctrls->max_perf; + +if (min_update && max_update && + perf_ctrls->min_perf > perf_ctrls->max_perf) +return -EINVAL ; + + desired_perf = perf_ctrls->desired_perf; +if (desired_perf && min_update && max_update) + desired_perf =clamp (desired_perf, perf_ctrls->min_perf, + perf_ctrls->max_perf); + desired_pcc = desired_update &&CPC_IN_PCC (desired_reg); min_pcc = min_update &&CPC_IN_PCC (min_perf_reg); max_pcc = max_update &&CPC_IN_PCC (max_perf_reg); pcc_update = desired_pcc || min_pcc || max_pcc; + pcc_layout = (cpc_is_writable (desired_reg) &&CPC_IN_PCC (desired_reg)) || (cpc_is_writable (min_perf_reg) &&CPC_IN_PCC (min_perf_reg)) || (cpc_is_writable (max_perf_reg) &&CPC_IN_PCC (max_perf_reg));
@@ -3531,9 +3746,10 @@ int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls)* A mixed layout cannot batch fallible direct writes safely: another * CPU's staged PCC values may no longer match if a direct write fails. * Serialize the complete mixed transaction and drain an older batch -* before changing a direct control. +* before changing a direct control. A single PCC limit update also +* needs the exclusive lock to read and check the unchanged limit. */ -if (mixed_layout) { +if (mixed_layout || (pcc_update && min_update != max_update)) {down_write (&pcc_ss_data->pcc_lock);if (pcc_ss_data->pending_pcc_write_cmd) { ret =send_pcc_cmd (pcc_ss_id,CMD_WRITE );
@@ -3547,50 +3763,14 @@ int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls) goto out_mixed_unlock; } -if (desired_update && !desired_pcc) { - ret =cpc_write (cpu, desired_reg, - perf_ctrls->desired_perf); -if (ret) - goto out_mixed_unlock; - } -if (min_update && !min_pcc) { - ret =cpc_write (cpu, min_perf_reg, - perf_ctrls->min_perf); -if (ret) - goto out_mixed_unlock; - } -if (max_update && !max_pcc) { - ret =cpc_write (cpu, max_perf_reg, - perf_ctrls->max_perf); -if (ret) - goto out_mixed_unlock; - } - -if (desired_pcc) { - ret =cpc_write (cpu, desired_reg, - perf_ctrls->desired_perf); -if (ret) - goto out_mixed_unlock; - } -if (min_pcc) { - ret =cpc_write (cpu, min_perf_reg, - perf_ctrls->min_perf); -if (ret) - goto out_mixed_unlock; - } -if (max_pcc) { - ret =cpc_write (cpu, max_perf_reg, - perf_ctrls->max_perf); -if (ret) - goto out_mixed_unlock; - } - -if (pcc_update) { -WRITE_ONCE (pcc_ss_data->pending_pcc_write_cmd, true); - cpc_desc->write_cmd_id = pcc_ss_data->pcc_write_cnt; - cpc_desc->write_cmd_status =0 ; - ret =send_pcc_cmd (pcc_ss_id,CMD_WRITE ); - } +if (min_update || max_update) + ret =cppc_write_ordered_perf (cpu, perf_ctrls, + desired_update, + min_update, max_update); +else if (desired_update) + ret =cppc_write_perf_controls (cpu, perf_ctrls, +CPPC_UPDATE_DESIRED , + false); out_mixed_unlock:up_write (&pcc_ss_data->pcc_lock);
@@ -3599,22 +3779,14 @@ int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls)/* A request without PCC updates has no payload to coordinate. */ if (!pcc_update) { -if (desired_update) { - ret =cpc_write (cpu, desired_reg, - perf_ctrls->desired_perf); -if (ret) -return ret; - } -if (min_update) { - ret =cpc_write (cpu, min_perf_reg, - perf_ctrls->min_perf); -if (ret) -return ret; - } -if (max_update) - ret =cpc_write (cpu, max_perf_reg, - perf_ctrls->max_perf); -return ret; +if (min_update || max_update) +return cppc_write_ordered_perf (cpu, perf_ctrls, + desired_update, + min_update, max_update); +if (desired_update) +return cpc_write (cpu, desired_reg, + perf_ctrls->desired_perf); +return 0 ; }down_read (&pcc_ss_data->pcc_lock);/* BEGIN Phase-I */
@@ -3632,7 +3804,7 @@ int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls)* achieve that goal here. */ if (desired_pcc) { - ret =cpc_write (cpu, desired_reg, perf_ctrls->desired_perf); + ret =cpc_write (cpu, desired_reg, desired_perf);if (ret) goto out_pcc_read_unlock; }
drivers/cpufreq/cppc_cpufreq.c +112 -29
236 lines not on this page · raw message
include/acpi/cppc_acpi.h +1 -0
@@ -165,6 +165,7 @@ struct cppc_cpudata {struct cppc_perf_fb_ctrs perf_fb_ctrs;unsigned int shared_type;cpumask_var_t shared_cpu_map; +bool perf_limits_valid; };#ifdef CONFIG_ACPI_CPPC_LIB
Signature · 1 line
2.34.1
Re: [PATCH v7 2/5] cpufreq: CPPC: Keep the policy across CPU hotplug
In reply to Sumit GuptaQuoted 27 lines · Sumit Gupta
Without online()/offline() callbacks, the cpufreq core calls
exit()when a policy's last online CPU goes down. That drops the driver's per-policy data, whichinit()rebuilds when a CPU comes back.Add lightweight online()/offline() callbacks so the core instead keeps the policy active and reuses the driver's
cpu_dataacross CPU hotplug.init()then runs once instead of on every hotplug, making CPU hotplug faster.This also lets
offline()andonline()restore and reapply register values across CPU hotplug. A later patch in this series uses this to preserve the OSPM-set registers.Move what
init()andexit()did on hotplug into the new callbacks:-
offline()requests the lowest performance and stops the frequency invariance updates, asexit()did. It also sets MIN to the lowest performance, so that Desired is not below MIN while the CPU is offline.
-online()re-enables CPPC and restores the performance controls, as the platform may have reset them. Failures are logged, not returned, as the core would free the policy. It also restarts the frequency invariance updates with a new counter snapshot, asinit()did, so that no sample spans the offline window.
-exit()now only frees the driver data, as the core callsoffline()before it.
Quoted 6 lines
The driver keeps a copy of the MIN and MAX it last wrote, so that
fast_switch()can skip writing them again.online()marks that copy out of date before the restore, as the platform may have reset the limits, and updates it once the restore succeeds.Signed-off-by Sumit Gupta <[email protected]>
Quoted 145 lines
--- drivers/cpufreq/cppc_cpufreq.c | 106 ++++++++++++++++++++++++++++++--- 1 file changed, 97 insertions(+), 9 deletions(-)drivers/cpufreq/cppc_cpufreq.c +97
-9@@ -209,7 +209,7 @@ static void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)if (fie_disabled)return ; -/* policy->cpus will be empty here, use related_cpus instead */ +/* policy->cpus has only the online CPUs, so use related_cpus */ topology_clear_scale_freq_source (SCALE_FREQ_SOURCE_CPPC , policy->related_cpus);for_each_cpu (cpu, policy->related_cpus) {@@ -807,24 +807,110 @@ static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) }static void cppc_cpufreq_cpu_exit (struct cpufreq_policy *policy) +{ +/* +* The core always calls offline() first, so only the driver data is +* left to free. +*/ +cppc_cpufreq_put_cpu_data (policy); +} + +/* +* Run when the policy's first CPU comes back online, the counterpart of +* offline(). +* +* The platform may have disabled CPPC and reset the performance controls +* (desired, min and max performance) while the CPU was offline. Re-enable +* CPPC and reprogram the controls. +* +* Log failures but do not return them, as the core would free the policy. +* Continue after a failure to enable CPPC, as the writes that follow fail +* on their own if the platform does not take them. A failed controls write +* is not fatal, as the next frequency change writes the controls again. +*/ +static int cppc_cpufreq_cpu_online (struct cpufreq_policy *policy) {struct cppc_cpudata *cpu_data = policy->driver_data; -struct cppc_perf_caps *caps = &cpu_data->perf_caps; +u32 desired_perf, min_perf, max_perf; +struct cppc_perf_ctrls perf_ctrls = {}; +unsigned int cpu = policy->cpu; +int ret; + +/* +* The platform may have reset MIN and MAX, so mark the copy that +* fast_switch() uses as out of date until the restore below succeeds. +*/ +WRITE_ONCE (cpu_data->perf_limits_valid, false); + + ret =cppc_set_enable (cpu, true); +if (ret && ret != -EOPNOTSUPP ) +pr_warn ("Failed to re-enable CPPC for CPU%u (%d)\n" , cpu, ret); + +/* +* Recompute min/max from the policy, clamp the last request into range, +* and reprogram the performance controls. Update the driver's copy only +* if the write succeeds. Otherwise the next frequency change writes the +* limits again. +*/ +cppc_cpufreq_get_perf_limits (cpu_data, policy, &min_perf, &max_perf); + desired_perf =clamp_t (u32, cpu_data->perf_ctrls.desired_perf, + min_perf, max_perf); + + perf_ctrls.desired_perf = desired_perf; + perf_ctrls.min_perf = min_perf; + perf_ctrls.max_perf = max_perf; + perf_ctrls.min_perf_valid = true; + + ret =cppc_set_perf (cpu, &perf_ctrls); +if (ret) { +pr_debug ("Failed to restore perf controls on CPU%u (%d)\n" , + cpu, ret); + }else { +WRITE_ONCE (cpu_data->perf_ctrls.desired_perf, desired_perf); +WRITE_ONCE (cpu_data->perf_ctrls.min_perf, min_perf); +WRITE_ONCE (cpu_data->perf_ctrls.max_perf, max_perf); +WRITE_ONCE (cpu_data->perf_limits_valid, true); + } + +/* Restart what offline() stopped, with a new counter snapshot. */ +cppc_cpufreq_cpu_fie_init (policy); + +return 0 ; +} + +/* +* Run when the policy's last online CPU goes down, the counterpart of online(). +* Having offline() makes the core keep the policy rather than call exit(). +*/ +static int cppc_cpufreq_cpu_offline (struct cpufreq_policy *policy) +{ +struct cppc_cpudata *cpu_data = policy->driver_data; +struct cppc_perf_ctrls perf_ctrls = cpu_data->perf_ctrls;unsigned int cpu = policy->cpu;int ret; +/* +* Stop the frequency invariance updates and cancel the pending work, so +* that no sample spans the offline window. online() restarts them with +* a new counter snapshot. +*/ cppc_cpufreq_cpu_fie_exit (policy); - cpu_data->perf_ctrls.desired_perf = caps->lowest_perf; - cpu_data->perf_ctrls.min_perf = caps->lowest_perf; - cpu_data->perf_ctrls.min_perf_valid = true; +/* +* Request the lowest performance while the policy has no online CPU, +* lowering MIN with it so that the request stays a valid tuple. MAX +* keeps the value the policy last programmed. +*/ + perf_ctrls.desired_perf = cpu_data->perf_caps.lowest_perf; + perf_ctrls.min_perf = cpu_data->perf_caps.lowest_perf; + perf_ctrls.min_perf_valid = true; - ret =cppc_set_perf (cpu, &cpu_data->perf_ctrls); + ret =cppc_set_perf (cpu, &perf_ctrls);if (ret) -pr_debug ("Err setting perf value:%d on CPU:%d. ret:%d\n" , - caps->lowest_perf, cpu, ret); +pr_debug ("Err setting perf value:%u on CPU:%u. ret:%d\n" , + cpu_data->perf_caps.lowest_perf, cpu, ret); -cppc_cpufreq_put_cpu_data (policy); +return 0 ; }static int cppc_cpufreq_cpu_resume (struct cpufreq_policy *policy)@@ -1141,6 +1227,8 @@ static struct cpufreq_driver cppc_cpufreq_driver = { .fast_switch = cppc_cpufreq_fast_switch, .init = cppc_cpufreq_cpu_init, .exit = cppc_cpufreq_cpu_exit, + .online = cppc_cpufreq_cpu_online, + .offline = cppc_cpufreq_cpu_offline, .resume = cppc_cpufreq_cpu_resume, .set_boost = cppc_cpufreq_set_boost, .attr = cppc_cpufreq_attr,
Quoted 64 lines · Sumit Gupta
This series preserves the OSPM-set CPPC registers (Energy Performance Preference, Autonomous Activity Window, Autonomous Selection - set via sysfs) across CPU hotplug and suspend/resume. On driver unload, it puts back the values they had before the driver loaded.
To do this, it keeps the CPPC cpufreq policy alive across CPU hotplug. It also orders the performance-control writes in
cppc_set_perf(), so that restoring them after a platform reset never sends an invalid request.Without online()/offline() callbacks, the core tears a policy down when its last CPU goes offline and rebuilds it on the way back, re-reading the CPPC capabilities each time. The values written to these registers can be lost:
- Across CPU hotplug or suspend/resume: the platform may reset them while the CPU is offline or suspended.
- On driver unload: the driver-written value is left in the register instead of returning to its pre-driver state.Handle these with:
- Patch 1: orders the Desired, MIN and MAX writes in
cppc_set_perf(), so that the platform never sees an invalid request. Christian proposed it in [7], and this version adds fixes found in testing.
- Patch 2: adds online()/offline() callbacks so the core keeps policy alive across CPU hotplug instead of tearing it down and rebuilding it.
- Patch 3: makes the autonomous selection register helpers take a u64.
- Patch 4: saves each register's firmware value atinit()and each successful sysfs write as the requested value.offline()restores the firmware values, andonline()reapplies the requested values.
- Patch 5: extends the same restore and reapply to system suspend/resume.v6[6] -> v7:
- Patch 1:
- clamp Desired into MIN and MAX instead of rejecting it. (Sashiko)
-store_auto_select()updates its cache only on success. (Sashiko)
- Patch 2:
-exit()only frees the driver data. (Sashiko)
-online()continues if enabling CPPC fails, asinit()does.
- Patch 4:
- save the requested value on each successful sysfs write, instead of reading it inoffline(). (Christian, Rafael [8])
-init()fails if a firmware value cannot be read. (Christian)
- split theauto_act_windowstore fromperf_limited.
- Patch 5:
-suspend()no longer reads and saves the registers.Christian Loehle (1):
ACPI: CPPC: Order performance-control updatesSumit Gupta (4):
cpufreq: CPPC: Keep the policy across CPU hotplug
ACPI: CPPC: Make autonomous selection helpers take a u64
cpufreq: CPPC: Preserve OSPM-set registers across hotplug and unload
cpufreq: CPPC: Preserve OSPM-set registers across suspend/resumedrivers/acpi/cppc_acpi.c | 318 +++++++++++++++----- drivers/cpufreq/amd-pstate.c | 2 +- drivers/cpufreq/cppc_cpufreq.c | 530 ++++++++++++++++++++++++++++++--- include/acpi/cppc_acpi.h | 9 +- 4 files changed, 729 insertions(+), 130 deletions(-)
Quoted 9 lines
[1] v1: https://lore.kernel.org/lkml/[email protected]/
[2] v2: https://lore.kernel.org/lkml/[email protected]/
[3] v3: https://lore.kernel.org/lkml/[email protected]/
[4] v4: https://lore.kernel.org/lkml/[email protected]/
[5] v5: https://lore.kernel.org/lkml/[email protected]/
[6] v6: https://lore.kernel.org/lkml/[email protected]/
[7] https://lore.kernel.org/lkml/[email protected]/
[8] https://lore.kernel.org/linux-pm/CAJZ5v0iXip_5Ywk3EkrGL_u2w_hOcGKA1XvrubLPb3diWHx5Lw@mail.gmail.com/
My testing setup is far from perfect, but let's hope good enough:
Tested on Orion O6, AmpereOne and Blackview Ryzen 5 7430U (for ACPI-CPPC only):
- native and emulated CPPC hotplug, both auto-select modes
- S3 on Blackview and s2idle on AmpereOne
- amd-pstate active/passive/guided on Blackview.
Didn't observe any violations in restore order with both native register and fake support (which silently drops requests before the register write).