[PATCH v6 2/5] cpufreq: CPPC: Keep the policy across CPU hotplug
From: Sumit Gupta
Date: Tue Oct 20 2026 - 08:20:01 EST
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 live and reuses the driver's cpu_data across CPU hotplug.
init() then runs once instead of on every hotplug, making CPU hotplug
faster.
The driver can now save what the OS set in offline() and put it back in
online(). 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 lowers MIN with the request, so
that the parked controls stay valid.
- 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.
The restore reprograms both limits, so it also refreshes the limits
cached for the fast path.
Signed-off-by: Sumit Gupta <sumitg@xxxxxxxxxx>
---
drivers/cpufreq/cppc_cpufreq.c | 106 ++++++++++++++++++++++++++++++++-
1 file changed, 105 insertions(+), 1 deletion(-)
diff --git a/drivers/cpufreq/cppc_cpufreq.c b/drivers/cpufreq/cppc_cpufreq.c
index 709363e6d98d..7448ca43b363 100644
--- a/drivers/cpufreq/cppc_cpufreq.c
+++ b/drivers/cpufreq/cppc_cpufreq.c
@@ -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 excludes the offline CPUs, use related_cpus */
topology_clear_scale_freq_source(SCALE_FREQ_SOURCE_CPPC, policy->related_cpus);
for_each_cpu(cpu, policy->related_cpus) {
@@ -837,6 +837,108 @@ static void cppc_cpufreq_cpu_exit(struct cpufreq_policy *policy)
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, so re-enable
+ * CPPC and reprogram them.
+ *
+ * Report failures without returning them, or the core would free the policy and
+ * leave the CPU without cpufreq. A failed write to the performance controls is
+ * not fatal, as the governor's next request programs them again. A failed CPPC
+ * enable stops the restore, as the writes that follow may not reach the
+ * platform.
+ */
+static int cppc_cpufreq_cpu_online(struct cpufreq_policy *policy)
+{
+ struct cppc_cpudata *cpu_data = policy->driver_data;
+ 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 the controls, so drop the limits cached
+ * for the fast path until the restore below reprograms them.
+ */
+ 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);
+ goto out_fie;
+ }
+
+ /*
+ * Recompute min/max from the policy, clamp the last request into range,
+ * and reprogram the performance controls. Publish the values only once
+ * the platform has taken them.
+ */
+ 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);
+ }
+
+out_fie:
+ /* 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, undoing what online() did.
+ * Defining offline() is what makes the core keep the policy alive instead of
+ * tearing it down.
+ */
+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);
+
+ /*
+ * 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, &perf_ctrls);
+ if (ret)
+ pr_debug("Err setting perf value:%u on CPU:%u. ret:%d\n",
+ cpu_data->perf_caps.lowest_perf, cpu, ret);
+
+ return 0;
+}
+
static int cppc_cpufreq_cpu_resume(struct cpufreq_policy *policy)
{
struct cppc_cpudata *cpu_data = policy->driver_data;
@@ -1149,6 +1251,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,
--
2.34.1