Skip to contentSkip to conversation

cpufreq: CPPC: Preserve OSPM-set registers across hotplug and unload

PATCH Superseded Sumit Gupta 4 participants 17 messages Aug 6 – Sep 21

Subsystems (7)

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Sumit Gupta

author reviewer
#0 lore raw

v4 cover cpufreq: CPPC: Preserve OSPM-set registers across hotplug and unload

This series keeps the CPPC cpufreq policy alive across CPU hotplug and preserves the OSPM-set CPPC registers (Energy Performance Preference, Autonomous Activity Window, Autonomous Selection - set via sysfs).

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.
- 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: adds online()/offline() callbacks so the core keeps policy alive across CPU hotplug instead of tearing it down and rebuilding it.
- Patch 2: makes the autonomous selection register helpers take a u64.
- Patch 3: adds a table-driven mechanism that captures each register's firmware value at init(), restores it from offline(), and reapplies the OSPM-set value from online().
- Patch 4: extends the same save/restore to system suspend/resume.

v3[3] -> v4:
- Patch 1:
- offline() parks the perf request at lowest_perf, as exit() did.
- online() resyncs the frequency invariance counters.
- raise MAX before the perf restore when the saved MIN is above it.
- Patch 3:
- write auto_sel first when enabling it and last when disabling it.
- replace the four save/restore helpers into save_regs() and apply_saved_regs(), each taking the firmware or requested type.
- keep the per-policy saved values in one struct, and name each register for the pr_debug diagnostics.
- Patch 4:
- suspend() also restores the firmware values and flags it, so offline() skips them and resume() only handles still-online policies.

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       |  20 +-
 drivers/cpufreq/amd-pstate.c   |   2 +-
 drivers/cpufreq/cppc_cpufreq.c | 369 ++++++++++++++++++++++++++++++++-
 include/acpi/cppc_acpi.h       |   8 +-
 4 files changed, 376 insertions(+), 23 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]/

Signature · 1 line
2.34.1
author reviewer
#1 lore raw

PATCH v4 1/4 cpufreq: CPPC: Keep the policy across CPU hotplug

Without online()/offline() callbacks, the cpufreq core fully tears down a policy during exit() when its last online CPU is offlined, and rebuilds it during init() when it 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. This avoids re-reading the CPPC capabilities on every offline/online, making CPU hotplug faster.

Move what init() and exit() did on hotplug into the new callbacks:

- offline() requests the lowest desired performance, as exit() did.
- 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.
- online() also resyncs the frequency invariance counters, so that the first tick does not measure across the offline window.

The restore in online() uses cppc_set_perf(), which writes MIN before MAX. If the platform lowered MAX while the CPU was offline, writing the saved MIN could briefly leave MIN above MAX on registers not accessed through PCC, as PCC delivers the writes in one transaction. Raise MAX ahead of the restore when the saved MIN is above it.

---
 drivers/cpufreq/cppc_cpufreq.c | 128 +++++++++++++++++++++++++++++++++
 1 file changed, 128 insertions(+)
drivers/cpufreq/cppc_cpufreq.c +128 -0
index 80893844353c..4b3da9a3e122 100644
@@ -211,6 +211,29 @@ static void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
 	}
 }
 
+/*
+ * Resync the counter snapshot, as the policy is kept across CPU hotplug and
+ * the first tick after online would otherwise span the offline window.
+ */
+static void cppc_cpufreq_cpu_fie_resync(struct cpufreq_policy *policy)
+{
+	struct cppc_freq_invariance *cppc_fi;
+	int cpu, ret;
+
+	if (fie_disabled)
+		return;
+
+	/* policy->cpus still holds related_cpus here, so skip offline CPUs. */
+	for_each_cpu_and(cpu, policy->cpus, cpu_online_mask) {
+		cppc_fi = &per_cpu(cppc_freq_inv, cpu);
+
+		ret = cppc_get_perf_ctrs(cpu, &cppc_fi->prev_perf_fb_ctrs);
+		if (ret)
+			pr_debug("%s: failed to read perf counters for cpu:%d: %d\n",
+				 __func__, cpu, ret);
+	}
+}
+
 static void cppc_fie_kworker_init(void)
 {
 	struct sched_attr attr = {
@@ -281,6 +304,10 @@ static inline void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
 {
 }
 
+static inline void cppc_cpufreq_cpu_fie_resync(struct cpufreq_policy *policy)
+{
+}
+
 static inline void cppc_freq_invariance_init(void)
 {
 }
@@ -735,6 +762,105 @@ static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
 	return ret;
 }
 
+/*
+ * With offline() defined, the cpufreq core keeps the policy alive when
+ * a CPU is hotplugged out.
+ */
+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;
+
+	/*
+	 * Request the lowest desired performance while the policy has no online
+	 * CPU. Zeroing MIN and MAX makes cppc_set_perf() leave them unchanged.
+	 */
+	perf_ctrls.desired_perf = cpu_data->perf_caps.lowest_perf;
+	perf_ctrls.min_perf = 0;
+	perf_ctrls.max_perf = 0;
+
+	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;
+}
+
+/*
+ * Raise MAX ahead of the full restore when the requested MIN is above the
+ * current MAX. cppc_set_perf() writes MIN before MAX, so the platform would
+ * otherwise briefly see MIN above MAX on registers not accessed through PCC.
+ * Lowering MAX is safe, as the MIN written first is never above it.
+ */
+static int
+cppc_cpufreq_prepare_perf_restore(unsigned int cpu,
+				  const struct cppc_perf_ctrls *target)
+{
+	struct cppc_perf_ctrls cur = {}, prep = {};
+	int ret;
+
+	ret = cppc_get_perf(cpu, &cur);
+	if (ret)
+		return ret;
+
+	if (!cur.max_perf || target->min_perf <= cur.max_perf)
+		return 0;
+
+	prep.desired_perf = target->desired_perf;
+	prep.min_perf = 0;	/* Zero leaves MIN unchanged. */
+	prep.max_perf = target->max_perf;
+
+	return cppc_set_perf(cpu, &prep);
+}
+
+/*
+ * Restore what the CPU may have lost while offline, as the platform may have
+ * disabled CPPC and reset the performance controls. Never fail the callback,
+ * or the core would free the policy and leave the CPU without cpufreq. The
+ * governor redoes the control writes, so they are best effort, unlike the
+ * enable, which only a later online() can retry.
+ */
+static int cppc_cpufreq_cpu_online(struct cpufreq_policy *policy)
+{
+	struct cppc_cpudata *cpu_data = policy->driver_data;
+	unsigned int cpu = policy->cpu;
+	int ret;
+
+	cppc_cpufreq_cpu_fie_resync(policy);
+
+	ret = cppc_set_enable(cpu, true);
+	if (ret && ret != -EOPNOTSUPP) {
+		pr_warn("Failed to re-enable CPPC for CPU%u (%d)\n", cpu, ret);
+		return 0;
+	}
+
+	/*
+	 * The platform may reset the controls while the CPU is offline, so
+	 * recompute min/max, clamp desired_perf into range, and reprogram them.
+	 */
+	cppc_cpufreq_update_perf_limits(cpu_data, policy);
+
+	cpu_data->perf_ctrls.desired_perf =
+		clamp_t(u32, cpu_data->perf_ctrls.desired_perf,
+			cpu_data->perf_ctrls.min_perf,
+			cpu_data->perf_ctrls.max_perf);
+
+	ret = cppc_cpufreq_prepare_perf_restore(cpu, &cpu_data->perf_ctrls);
+	if (ret)
+		pr_debug("Failed to reorder perf restore on CPU%u (%d)\n",
+			 cpu, ret);
+
+	ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
+	if (ret)
+		pr_debug("Failed to reapply perf request on CPU%u (%d)\n",
+			 cpu, ret);
+
+	return 0;
+}
+
 static void cppc_cpufreq_cpu_exit(struct cpufreq_policy *policy)
 {
 	struct cppc_cpudata *cpu_data = policy->driver_data;
@@ -1047,6 +1173,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,
 	.set_boost = cppc_cpufreq_set_boost,
 	.attr = cppc_cpufreq_attr,
 	.name = "cppc_cpufreq",
Signature · 1 line
2.34.1
reviewer
#2 lore raw

Re: [PATCH v4 1/4] cpufreq: CPPC: Keep the policy across CPU hotplug

Hi Sumit,

Sorry for catching up late.

A few questions inline.

Regards,
Jie

Quoted 128 lines · Sumit Gupta

Without online()/offline() callbacks, the cpufreq core fully tears down a policy during exit() when its last online CPU is offlined, and rebuilds it during init() when it 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. This avoids re-reading the CPPC capabilities on every offline/online, making CPU hotplug faster.

Move what init() and exit() did on hotplug into the new callbacks:

- offline() requests the lowest desired performance, as exit() did.
- 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.
- online() also resyncs the frequency invariance counters, so that the first tick does not measure across the offline window.

The restore in online() uses cppc_set_perf(), which writes MIN before MAX. If the platform lowered MAX while the CPU was offline, writing the saved MIN could briefly leave MIN above MAX on registers not accessed through PCC, as PCC delivers the writes in one transaction. Raise MAX ahead of the restore when the saved MIN is above it.

---
 drivers/cpufreq/cppc_cpufreq.c | 128 +++++++++++++++++++++++++++++++++
 1 file changed, 128 insertions(+)
drivers/cpufreq/cppc_cpufreq.c +77 -0
index 80893844353c..4b3da9a3e122 100644
@@ -211,6 +211,29 @@ static void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
 	}
 }
 
+/*
+ * Resync the counter snapshot, as the policy is kept across CPU hotplug and
+ * the first tick after online would otherwise span the offline window.
+ */
+static void cppc_cpufreq_cpu_fie_resync(struct cpufreq_policy *policy)
+{
+	struct cppc_freq_invariance *cppc_fi;
+	int cpu, ret;
+
+	if (fie_disabled)
+		return;
+
+	/* policy->cpus still holds related_cpus here, so skip offline CPUs. */
+	for_each_cpu_and(cpu, policy->cpus, cpu_online_mask) {
+		cppc_fi = &per_cpu(cppc_freq_inv, cpu);
+
+		ret = cppc_get_perf_ctrs(cpu, &cppc_fi->prev_perf_fb_ctrs);
+		if (ret)
+			pr_debug("%s: failed to read perf counters for cpu:%d: %d\n",
+				 __func__, cpu, ret);
+	}
+}
+
 static void cppc_fie_kworker_init(void)
 {
 	struct sched_attr attr = {
@@ -281,6 +304,10 @@ static inline void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
 {
 }
 
+static inline void cppc_cpufreq_cpu_fie_resync(struct cpufreq_policy *policy)
+{
+}
+
 static inline void cppc_freq_invariance_init(void)
 {
 }
@@ -735,6 +762,105 @@ static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
 	return ret;
 }
 
+/*
+ * With offline() defined, the cpufreq core keeps the policy alive when
+ * a CPU is hotplugged out.
+ */
+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;
+
+	/*
+	 * Request the lowest desired performance while the policy has no online
+	 * CPU. Zeroing MIN and MAX makes cppc_set_perf() leave them unchanged.
+	 */
+	perf_ctrls.desired_perf = cpu_data->perf_caps.lowest_perf;
+	perf_ctrls.min_perf = 0;
+	perf_ctrls.max_perf = 0;
+
+	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;
+}
+
+/*
+ * Raise MAX ahead of the full restore when the requested MIN is above the
+ * current MAX. cppc_set_perf() writes MIN before MAX, so the platform would
+ * otherwise briefly see MIN above MAX on registers not accessed through PCC.
+ * Lowering MAX is safe, as the MIN written first is never above it.
+ */
+static int
+cppc_cpufreq_prepare_perf_restore(unsigned int cpu,
+				  const struct cppc_perf_ctrls *target)
+{
+	struct cppc_perf_ctrls cur = {}, prep = {};
+	int ret;
+
+	ret = cppc_get_perf(cpu, &cur);
+	if (ret)
+		return ret;
+
+	if (!cur.max_perf || target->min_perf <= cur.max_perf)
+		return 0;
+
+	prep.desired_perf = target->desired_perf;
+	prep.min_perf = 0;	/* Zero leaves MIN unchanged. */
+	prep.max_perf = target->max_perf;

drivers/cpufreq/cppc_cpufreq.c static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 824

Quoted 10 lines
drivers/cpufreq/cppc_cpufreq.c +10 -0
+
+	return cppc_set_perf(cpu, &prep);
+}
+
+/*
+ * Restore what the CPU may have lost while offline, as the platform may have
+ * disabled CPPC and reset the performance controls. Never fail the callback,
+ * or the core would free the policy and leave the CPU without cpufreq. The
+ * governor redoes the control writes, so they are best effort, unlike the
+ * enable, which only a later online() can retry.

Sorry, I don't quite understand the last sentence.

Quoted 17 lines
drivers/cpufreq/cppc_cpufreq.c +17 -0
+ */
+static int cppc_cpufreq_cpu_online(struct cpufreq_policy *policy)
+{
+	struct cppc_cpudata *cpu_data = policy->driver_data;
+	unsigned int cpu = policy->cpu;
+	int ret;
+
+	cppc_cpufreq_cpu_fie_resync(policy);
+
+	ret = cppc_set_enable(cpu, true);
+	if (ret && ret != -EOPNOTSUPP) {
+		pr_warn("Failed to re-enable CPPC for CPU%u (%d)\n", cpu, ret);
+		return 0;
+	}
+
+	/*
+	 * The platform may reset the controls while the CPU is offline, so

drivers/cpufreq/cppc_cpufreq.c static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 851

Quoted 10 lines
drivers/cpufreq/cppc_cpufreq.c +10 -0
+	 * recompute min/max, clamp desired_perf into range, and reprogram them.
+	 */
+	cppc_cpufreq_update_perf_limits(cpu_data, policy);
+
+	cpu_data->perf_ctrls.desired_perf =
+		clamp_t(u32, cpu_data->perf_ctrls.desired_perf,
+			cpu_data->perf_ctrls.min_perf,
+			cpu_data->perf_ctrls.max_perf);
+
+	ret = cppc_cpufreq_prepare_perf_restore(cpu, &cpu_data->perf_ctrls);

Actually, I don't quite think this is necessary?

The motivation of doing this is fair (as mentioned in v3), but what's the real consequence of transiently setting min_perf larger than max_perf? Platforms should be able to handle this.

Even if we have to fix it, it's supposed to be done in cppc_acpi.c. The current ABI wraps many things up. cppc_get_perf() reads 4 values - min_perf, max_perf, energy_perf, auto_sel. cppc_set_perf writes 3 values - desired_perf, min_perf, max_perf. The cppc_cpufreq driver would be able to handle performance setting cleaner if those are separated.

I don't suggest we complicate the driver for now?

Quoted 5 lines
drivers/cpufreq/cppc_cpufreq.c +5 -0
+	if (ret)
+		pr_debug("Failed to reorder perf restore on CPU%u (%d)\n",
+			 cpu, ret);
+
+	ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);

drivers/cpufreq/cppc_cpufreq.c static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 866

Quoted 10 lines
drivers/cpufreq/cppc_cpufreq.c +7 -0
+	if (ret)
+		pr_debug("Failed to reapply perf request on CPU%u (%d)\n",
+			 cpu, ret);
+
+	return 0;
+}
+
 static void cppc_cpufreq_cpu_exit(struct cpufreq_policy *policy)
 {
 	struct cppc_cpudata *cpu_data = policy->driver_data;

For neatness, can we place the above functions after cppc_cpufreq_cpu_exit()? such that the order of source functions would be the same as the following structure, i.e. init, exit, online, offline, and perhaps, suspend, resume.

Quoted 9 lines
drivers/cpufreq/cppc_cpufreq.c +2 -0
@@ -1047,6 +1173,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,
 	.set_boost = cppc_cpufreq_set_boost,
 	.attr = cppc_cpufreq_attr,
 	.name = "cppc_cpufreq",
author reviewer
#3 lore raw

Re: [PATCH v4 1/4] cpufreq: CPPC: Keep the policy across CPU hotplug

Quoted 11 lines · Jie Zhan

External email: Use caution opening links or attachments

Hi Sumit,

Sorry for catching up late.

A few questions inline.

Regards,
Jie

Hi Jie,

Thanks for the review and sorry for the late reply.

Quoted 133 lines · Sumit Gupta
Quoted 131 lines

Without online()/offline() callbacks, the cpufreq core fully tears down a policy during exit() when its last online CPU is offlined, and rebuilds it during init() when it 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. This avoids re-reading the CPPC capabilities on every offline/online, making CPU hotplug faster.

Move what init() and exit() did on hotplug into the new callbacks:

- offline() requests the lowest desired performance, as exit() did.
- 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.
- online() also resyncs the frequency invariance counters, so that the first tick does not measure across the offline window.

The restore in online() uses cppc_set_perf(), which writes MIN before MAX. If the platform lowered MAX while the CPU was offline, writing the saved MIN could briefly leave MIN above MAX on registers not accessed through PCC, as PCC delivers the writes in one transaction. Raise MAX ahead of the restore when the saved MIN is above it.

---
  drivers/cpufreq/cppc_cpufreq.c | 128 +++++++++++++++++++++++++++++++++
  1 file changed, 128 insertions(+)
drivers/cpufreq/cppc_cpufreq.c +80 -0
index 80893844353c..4b3da9a3e122 100644
@@ -211,6 +211,29 @@ static void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
       }
  }
 
+/*
+ * Resync the counter snapshot, as the policy is kept across CPU hotplug and
+ * the first tick after online would otherwise span the offline window.
+ */
+static void cppc_cpufreq_cpu_fie_resync(struct cpufreq_policy *policy)
+{
+     struct cppc_freq_invariance *cppc_fi;
+     int cpu, ret;
+
+     if (fie_disabled)
+             return;
+
+     /* policy->cpus still holds related_cpus here, so skip offline CPUs. */
+     for_each_cpu_and(cpu, policy->cpus, cpu_online_mask) {
+             cppc_fi = &per_cpu(cppc_freq_inv, cpu);
+
+             ret = cppc_get_perf_ctrs(cpu, &cppc_fi->prev_perf_fb_ctrs);
+             if (ret)
+                     pr_debug("%s: failed to read perf counters for cpu:%d: %d\n",
+                              __func__, cpu, ret);
+     }
+}
+
  static void cppc_fie_kworker_init(void)
  {
       struct sched_attr attr = {
@@ -281,6 +304,10 @@ static inline void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
  {
  }
 
+static inline void cppc_cpufreq_cpu_fie_resync(struct cpufreq_policy *policy)
+{
+}
+
  static inline void cppc_freq_invariance_init(void)
  {
  }
@@ -735,6 +762,105 @@ static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
       return ret;
  }
 
+/*
+ * With offline() defined, the cpufreq core keeps the policy alive when
+ * a CPU is hotplugged out.
+ */
+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;
+
+     /*
+      * Request the lowest desired performance while the policy has no online
+      * CPU. Zeroing MIN and MAX makes cppc_set_perf() leave them unchanged.
+      */
+     perf_ctrls.desired_perf = cpu_data->perf_caps.lowest_perf;
+     perf_ctrls.min_perf = 0;
+     perf_ctrls.max_perf = 0;
+
+     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;
+}
+
+/*
+ * Raise MAX ahead of the full restore when the requested MIN is above the
+ * current MAX. cppc_set_perf() writes MIN before MAX, so the platform would
+ * otherwise briefly see MIN above MAX on registers not accessed through PCC.
+ * Lowering MAX is safe, as the MIN written first is never above it.
+ */
+static int
+cppc_cpufreq_prepare_perf_restore(unsigned int cpu,
+                               const struct cppc_perf_ctrls *target)
+{
+     struct cppc_perf_ctrls cur = {}, prep = {};
+     int ret;
+
+     ret = cppc_get_perf(cpu, &cur);
+     if (ret)
+             return ret;
+
+     if (!cur.max_perf || target->min_perf <= cur.max_perf)
+             return 0;
+
+     prep.desired_perf = target->desired_perf;
+     prep.min_perf = 0;      /* Zero leaves MIN unchanged. */
+     prep.max_perf = target->max_perf;
+
+     return cppc_set_perf(cpu, &prep);
+}
Quoted 8 lines

drivers/cpufreq/cppc_cpufreq.c static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 824

Quoted 7 lines
drivers/cpufreq/cppc_cpufreq.c +7 -0
+
+/*
+ * Restore what the CPU may have lost while offline, as the platform may have
+ * disabled CPPC and reset the performance controls. Never fail the callback,
+ * or the core would free the policy and leave the CPU without cpufreq. The
+ * governor redoes the control writes, so they are best effort, unlike the
+ * enable, which only a later online() can retry.

Sorry, I don't quite understand the last sentence.

Will rewrite in v5 as below:

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.

Quoted 33 lines
Quoted 17 lines
drivers/cpufreq/cppc_cpufreq.c +17 -0
+ */
+static int cppc_cpufreq_cpu_online(struct cpufreq_policy *policy)
+{
+     struct cppc_cpudata *cpu_data = policy->driver_data;
+     unsigned int cpu = policy->cpu;
+     int ret;
+
+     cppc_cpufreq_cpu_fie_resync(policy);
+
+     ret = cppc_set_enable(cpu, true);
+     if (ret && ret != -EOPNOTSUPP) {
+             pr_warn("Failed to re-enable CPPC for CPU%u (%d)\n", cpu, ret);
+             return 0;
+     }
+
+     /*
+      * The platform may reset the controls while the CPU is offline, so

drivers/cpufreq/cppc_cpufreq.c static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 851

Quoted 10 lines
drivers/cpufreq/cppc_cpufreq.c +10 -0
+      * recompute min/max, clamp desired_perf into range, and reprogram them.
+      */
+     cppc_cpufreq_update_perf_limits(cpu_data, policy);
+
+     cpu_data->perf_ctrls.desired_perf =
+             clamp_t(u32, cpu_data->perf_ctrls.desired_perf,
+                     cpu_data->perf_ctrls.min_perf,
+                     cpu_data->perf_ctrls.max_perf);
+
+     ret = cppc_cpufreq_prepare_perf_restore(cpu, &cpu_data->perf_ctrls);

Actually, I don't quite think this is necessary?

The motivation of doing this is fair (as mentioned in v3), but what's the real consequence of transiently setting min_perf larger than max_perf? Platforms should be able to handle this.

Quoted 7 lines

Even if we have to fix it, it's supposed to be done in cppc_acpi.c. The current ABI wraps many things up. cppc_get_perf() reads 4 values - min_perf, max_perf, energy_perf, auto_sel. cppc_set_perf writes 3 values - desired_perf, min_perf, max_perf. The cppc_cpufreq driver would be able to handle performance setting cleaner if those are separated.

I don't suggest we complicate the driver for now?

Agreed that it is not hotplug specific and can be done in the generic API.

cppc_set_perf() would have to know the programmed MIN and MAX to pick the write order. Separate accessors would let it read only those two, but that would add a read before every write, including fast_switch(). Caching what was last written would avoid that, but the platform can reset the registers while the CPU is offline or suspended.

I added this in response to Christian's comment on v3 [1], so I would prefer to keep it here for now. If the generic fix is still wanted, I can send the core API changes separately and we can discuss it there.

Quoted 10 lines
Quoted 10 lines
drivers/cpufreq/cppc_cpufreq.c +10 -0
+     if (ret)
+             pr_debug("Failed to reorder perf restore on CPU%u (%d)\n",
+                      cpu, ret);
+
+     ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
+     if (ret)
+             pr_debug("Failed to reapply perf request on CPU%u (%d)\n",
+                      cpu, ret);
+
+     return 0;
Quoted 8 lines

drivers/cpufreq/cppc_cpufreq.c static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 866

Quoted 5 lines
drivers/cpufreq/cppc_cpufreq.c +2 -0
+}
+
  static void cppc_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  {
       struct cppc_cpudata *cpu_data = policy->driver_data;

For neatness, can we place the above functions after cppc_cpufreq_cpu_exit()? such that the order of source functions would be the same as the following structure, i.e. init, exit, online, offline, and perhaps, suspend, resume.

Will change the order in v5, matching cppc_cpufreq_driver.

Thanks,
Sumit

....

reviewer
#4 lore raw

Re: [PATCH v4 1/4] cpufreq: CPPC: Keep the policy across CPU hotplug

Quoted 211 lines · Sumit Gupta
Quoted 11 lines · Jie Zhan

External email: Use caution opening links or attachments

Hi Sumit,

Sorry for catching up late.

A few questions inline.

Regards,
Jie

Hi Jie,

Thanks for the review and sorry for the late reply.

Quoted 133 lines · Sumit Gupta
Quoted 131 lines

Without online()/offline() callbacks, the cpufreq core fully tears down a policy during exit() when its last online CPU is offlined, and rebuilds it during init() when it 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. This avoids re-reading the CPPC capabilities on every offline/online, making CPU hotplug faster.

Move what init() and exit() did on hotplug into the new callbacks:

- offline() requests the lowest desired performance, as exit() did.
- 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.
- online() also resyncs the frequency invariance counters, so that the first tick does not measure across the offline window.

The restore in online() uses cppc_set_perf(), which writes MIN before MAX. If the platform lowered MAX while the CPU was offline, writing the saved MIN could briefly leave MIN above MAX on registers not accessed through PCC, as PCC delivers the writes in one transaction. Raise MAX ahead of the restore when the saved MIN is above it.

---
  drivers/cpufreq/cppc_cpufreq.c | 128 +++++++++++++++++++++++++++++++++
  1 file changed, 128 insertions(+)
drivers/cpufreq/cppc_cpufreq.c +80 -0
index 80893844353c..4b3da9a3e122 100644
@@ -211,6 +211,29 @@ static void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
       }
  }
 
+/*
+ * Resync the counter snapshot, as the policy is kept across CPU hotplug and
+ * the first tick after online would otherwise span the offline window.
+ */
+static void cppc_cpufreq_cpu_fie_resync(struct cpufreq_policy *policy)
+{
+     struct cppc_freq_invariance *cppc_fi;
+     int cpu, ret;
+
+     if (fie_disabled)
+             return;
+
+     /* policy->cpus still holds related_cpus here, so skip offline CPUs. */
+     for_each_cpu_and(cpu, policy->cpus, cpu_online_mask) {
+             cppc_fi = &per_cpu(cppc_freq_inv, cpu);
+
+             ret = cppc_get_perf_ctrs(cpu, &cppc_fi->prev_perf_fb_ctrs);
+             if (ret)
+                     pr_debug("%s: failed to read perf counters for cpu:%d: %d\n",
+                              __func__, cpu, ret);
+     }
+}
+
  static void cppc_fie_kworker_init(void)
  {
       struct sched_attr attr = {
@@ -281,6 +304,10 @@ static inline void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
  {
  }
 
+static inline void cppc_cpufreq_cpu_fie_resync(struct cpufreq_policy *policy)
+{
+}
+
  static inline void cppc_freq_invariance_init(void)
  {
  }
@@ -735,6 +762,105 @@ static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
       return ret;
  }
 
+/*
+ * With offline() defined, the cpufreq core keeps the policy alive when
+ * a CPU is hotplugged out.
+ */
+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;
+
+     /*
+      * Request the lowest desired performance while the policy has no online
+      * CPU. Zeroing MIN and MAX makes cppc_set_perf() leave them unchanged.
+      */
+     perf_ctrls.desired_perf = cpu_data->perf_caps.lowest_perf;
+     perf_ctrls.min_perf = 0;
+     perf_ctrls.max_perf = 0;
+
+     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;
+}
+
+/*
+ * Raise MAX ahead of the full restore when the requested MIN is above the
+ * current MAX. cppc_set_perf() writes MIN before MAX, so the platform would
+ * otherwise briefly see MIN above MAX on registers not accessed through PCC.
+ * Lowering MAX is safe, as the MIN written first is never above it.
+ */
+static int
+cppc_cpufreq_prepare_perf_restore(unsigned int cpu,
+                               const struct cppc_perf_ctrls *target)
+{
+     struct cppc_perf_ctrls cur = {}, prep = {};
+     int ret;
+
+     ret = cppc_get_perf(cpu, &cur);
+     if (ret)
+             return ret;
+
+     if (!cur.max_perf || target->min_perf <= cur.max_perf)
+             return 0;
+
+     prep.desired_perf = target->desired_perf;
+     prep.min_perf = 0;      /* Zero leaves MIN unchanged. */
+     prep.max_perf = target->max_perf;
+
+     return cppc_set_perf(cpu, &prep);
+}
Quoted 8 lines

drivers/cpufreq/cppc_cpufreq.c static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 824

Quoted 7 lines
drivers/cpufreq/cppc_cpufreq.c +7 -0
+
+/*
+ * Restore what the CPU may have lost while offline, as the platform may have
+ * disabled CPPC and reset the performance controls. Never fail the callback,
+ * or the core would free the policy and leave the CPU without cpufreq. The
+ * governor redoes the control writes, so they are best effort, unlike the
+ * enable, which only a later online() can retry.

Sorry, I don't quite understand the last sentence.

Will rewrite in v5 as below:

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.

Quoted 39 lines
Quoted 17 lines
drivers/cpufreq/cppc_cpufreq.c +17 -0
+ */
+static int cppc_cpufreq_cpu_online(struct cpufreq_policy *policy)
+{
+     struct cppc_cpudata *cpu_data = policy->driver_data;
+     unsigned int cpu = policy->cpu;
+     int ret;
+
+     cppc_cpufreq_cpu_fie_resync(policy);
+
+     ret = cppc_set_enable(cpu, true);
+     if (ret && ret != -EOPNOTSUPP) {
+             pr_warn("Failed to re-enable CPPC for CPU%u (%d)\n", cpu, ret);
+             return 0;
+     }
+
+     /*
+      * The platform may reset the controls while the CPU is offline, so

drivers/cpufreq/cppc_cpufreq.c static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 851

Quoted 10 lines
drivers/cpufreq/cppc_cpufreq.c +10 -0
+      * recompute min/max, clamp desired_perf into range, and reprogram them.
+      */
+     cppc_cpufreq_update_perf_limits(cpu_data, policy);
+
+     cpu_data->perf_ctrls.desired_perf =
+             clamp_t(u32, cpu_data->perf_ctrls.desired_perf,
+                     cpu_data->perf_ctrls.min_perf,
+                     cpu_data->perf_ctrls.max_perf);
+
+     ret = cppc_cpufreq_prepare_perf_restore(cpu, &cpu_data->perf_ctrls);

Actually, I don't quite think this is necessary?

The motivation of doing this is fair (as mentioned in v3), but what's the real consequence of transiently setting min_perf larger than max_perf? Platforms should be able to handle this.

Even if we have to fix it, it's supposed to be done in cppc_acpi.c. The current ABI wraps many things up. cppc_get_perf() reads 4 values - min_perf, max_perf, energy_perf, auto_sel. cppc_set_perf writes 3 values - desired_perf, min_perf, max_perf. The cppc_cpufreq driver would be able to handle performance setting cleaner if those are separated.

Quoted 8 lines
Quoted 1 line

I don't suggest we complicate the driver for now?

Agreed that it is not hotplug specific and can be done in the generic API.

cppc_set_perf() would have to know the programmed MIN and MAX to pick the write order. Separate accessors would let it read only those two, but that would add a read before every write, including fast_switch().

fast_switch() doesn't have to touch min/max_perf, but it did at the moment.

Quoted 2 lines

Caching what was last written would avoid that, but the platform can reset the registers while the CPU is offline or suspended.

Yeah, understood. My question is still whether it's practically useful and we're complicating this.

Two reasons.

1. Platform should be able to handle min_perf being trasiently larger than max_perf, otherwise it would be fragile.

2. It depends on the reset values of the two registers.

I went over the ACPI Spec and didn't manage to find a descprition on what the default/reset values of min/max perf registers should be.

For a sensisble design, min_perf defaults to be 0 or lowest perf, and max_perf defaults to be all 1s or highest perf. In those cases, we are safe to directly restore the saved values.

Quoted 32 lines

I added this in response to Christian's comment on v3 [1], so I would prefer to keep it here for now. If the generic fix is still wanted, I can send the core API changes separately and we can discuss it there.

Quoted 10 lines
Quoted 10 lines
drivers/cpufreq/cppc_cpufreq.c +10 -0
+     if (ret)
+             pr_debug("Failed to reorder perf restore on CPU%u (%d)\n",
+                      cpu, ret);
+
+     ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
+     if (ret)
+             pr_debug("Failed to reapply perf request on CPU%u (%d)\n",
+                      cpu, ret);
+
+     return 0;
Quoted 8 lines

drivers/cpufreq/cppc_cpufreq.c static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 866

Quoted 5 lines
drivers/cpufreq/cppc_cpufreq.c +2 -0
+}
+
  static void cppc_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  {
       struct cppc_cpudata *cpu_data = policy->driver_data;

For neatness, can we place the above functions after cppc_cpufreq_cpu_exit()? such that the order of source functions would be the same as the following structure, i.e. init, exit, online, offline, and perhaps, suspend, resume.

Will change the order in v5, matching cppc_cpufreq_driver.

Thanks,
Sumit

....

author reviewer
#5 lore raw

Re: [PATCH v4 1/4] cpufreq: CPPC: Keep the policy across CPU hotplug

Quoted 211 lines
Quoted 192 lines
Quoted 135 lines · Sumit Gupta
Quoted 131 lines

Without online()/offline() callbacks, the cpufreq core fully tears down a policy during exit() when its last online CPU is offlined, and rebuilds it during init() when it 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. This avoids re-reading the CPPC capabilities on every offline/online, making CPU hotplug faster.

Move what init() and exit() did on hotplug into the new callbacks:

- offline() requests the lowest desired performance, as exit() did.
- 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.
- online() also resyncs the frequency invariance counters, so that the first tick does not measure across the offline window.

The restore in online() uses cppc_set_perf(), which writes MIN before MAX. If the platform lowered MAX while the CPU was offline, writing the saved MIN could briefly leave MIN above MAX on registers not accessed through PCC, as PCC delivers the writes in one transaction. Raise MAX ahead of the restore when the saved MIN is above it.

---
   drivers/cpufreq/cppc_cpufreq.c | 128 +++++++++++++++++++++++++++++++++
   1 file changed, 128 insertions(+)
drivers/cpufreq/cppc_cpufreq.c +80 -0
index 80893844353c..4b3da9a3e122 100644
@@ -211,6 +211,29 @@ static void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
        }
   }
 
+/*
+ * Resync the counter snapshot, as the policy is kept across CPU hotplug and
+ * the first tick after online would otherwise span the offline window.
+ */
+static void cppc_cpufreq_cpu_fie_resync(struct cpufreq_policy *policy)
+{
+     struct cppc_freq_invariance *cppc_fi;
+     int cpu, ret;
+
+     if (fie_disabled)
+             return;
+
+     /* policy->cpus still holds related_cpus here, so skip offline CPUs. */
+     for_each_cpu_and(cpu, policy->cpus, cpu_online_mask) {
+             cppc_fi = &per_cpu(cppc_freq_inv, cpu);
+
+             ret = cppc_get_perf_ctrs(cpu, &cppc_fi->prev_perf_fb_ctrs);
+             if (ret)
+                     pr_debug("%s: failed to read perf counters for cpu:%d: %d\n",
+                              __func__, cpu, ret);
+     }
+}
+
   static void cppc_fie_kworker_init(void)
   {
        struct sched_attr attr = {
@@ -281,6 +304,10 @@ static inline void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
   {
   }
 
+static inline void cppc_cpufreq_cpu_fie_resync(struct cpufreq_policy *policy)
+{
+}
+
   static inline void cppc_freq_invariance_init(void)
   {
   }
@@ -735,6 +762,105 @@ static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
        return ret;
   }
 
+/*
+ * With offline() defined, the cpufreq core keeps the policy alive when
+ * a CPU is hotplugged out.
+ */
+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;
+
+     /*
+      * Request the lowest desired performance while the policy has no online
+      * CPU. Zeroing MIN and MAX makes cppc_set_perf() leave them unchanged.
+      */
+     perf_ctrls.desired_perf = cpu_data->perf_caps.lowest_perf;
+     perf_ctrls.min_perf = 0;
+     perf_ctrls.max_perf = 0;
+
+     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;
+}
+
+/*
+ * Raise MAX ahead of the full restore when the requested MIN is above the
+ * current MAX. cppc_set_perf() writes MIN before MAX, so the platform would
+ * otherwise briefly see MIN above MAX on registers not accessed through PCC.
+ * Lowering MAX is safe, as the MIN written first is never above it.
+ */
+static int
+cppc_cpufreq_prepare_perf_restore(unsigned int cpu,
+                               const struct cppc_perf_ctrls *target)
+{
+     struct cppc_perf_ctrls cur = {}, prep = {};
+     int ret;
+
+     ret = cppc_get_perf(cpu, &cur);
+     if (ret)
+             return ret;
+
+     if (!cur.max_perf || target->min_perf <= cur.max_perf)
+             return 0;
+
+     prep.desired_perf = target->desired_perf;
+     prep.min_perf = 0;      /* Zero leaves MIN unchanged. */
+     prep.max_perf = target->max_perf;
+
+     return cppc_set_perf(cpu, &prep);
+}
Quoted 8 lines

drivers/cpufreq/cppc_cpufreq.c static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 824

Quoted 7 lines
drivers/cpufreq/cppc_cpufreq.c +7 -0
+
+/*
+ * Restore what the CPU may have lost while offline, as the platform may have
+ * disabled CPPC and reset the performance controls. Never fail the callback,
+ * or the core would free the policy and leave the CPU without cpufreq. The
+ * governor redoes the control writes, so they are best effort, unlike the
+ * enable, which only a later online() can retry.

Sorry, I don't quite understand the last sentence.

Will rewrite in v5 as below:

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.

Quoted 39 lines
Quoted 17 lines
drivers/cpufreq/cppc_cpufreq.c +17 -0
+ */
+static int cppc_cpufreq_cpu_online(struct cpufreq_policy *policy)
+{
+     struct cppc_cpudata *cpu_data = policy->driver_data;
+     unsigned int cpu = policy->cpu;
+     int ret;
+
+     cppc_cpufreq_cpu_fie_resync(policy);
+
+     ret = cppc_set_enable(cpu, true);
+     if (ret && ret != -EOPNOTSUPP) {
+             pr_warn("Failed to re-enable CPPC for CPU%u (%d)\n", cpu, ret);
+             return 0;
+     }
+
+     /*
+      * The platform may reset the controls while the CPU is offline, so

drivers/cpufreq/cppc_cpufreq.c static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 851

Quoted 10 lines
drivers/cpufreq/cppc_cpufreq.c +10 -0
+      * recompute min/max, clamp desired_perf into range, and reprogram them.
+      */
+     cppc_cpufreq_update_perf_limits(cpu_data, policy);
+
+     cpu_data->perf_ctrls.desired_perf =
+             clamp_t(u32, cpu_data->perf_ctrls.desired_perf,
+                     cpu_data->perf_ctrls.min_perf,
+                     cpu_data->perf_ctrls.max_perf);
+
+     ret = cppc_cpufreq_prepare_perf_restore(cpu, &cpu_data->perf_ctrls);

Actually, I don't quite think this is necessary?

The motivation of doing this is fair (as mentioned in v3), but what's the real consequence of transiently setting min_perf larger than max_perf? Platforms should be able to handle this.

Even if we have to fix it, it's supposed to be done in cppc_acpi.c. The current ABI wraps many things up. cppc_get_perf() reads 4 values - min_perf, max_perf, energy_perf, auto_sel. cppc_set_perf writes 3 values - desired_perf, min_perf, max_perf. The cppc_cpufreq driver would be able to handle performance setting cleaner if those are separated.

Quoted 8 lines
Quoted 1 line

I don't suggest we complicate the driver for now?

Agreed that it is not hotplug specific and can be done in the generic API.

cppc_set_perf() would have to know the programmed MIN and MAX to pick the write order. Separate accessors would let it read only those two, but that would add a read before every write, including fast_switch().

fast_switch() doesn't have to touch min/max_perf, but it did at the moment.

Quoted 2 lines

Caching what was last written would avoid that, but the platform can reset the registers while the CPU is offline or suspended.

Yeah, understood. My question is still whether it's practically useful and we're complicating this.

Two reasons.

1. Platform should be able to handle min_perf being trasiently larger than max_perf, otherwise it would be fragile.

Quoted 8 lines

2. It depends on the reset values of the two registers.

I went over the ACPI Spec and didn't manage to find a descprition on what the default/reset values of min/max perf registers should be.

For a sensisble design, min_perf defaults to be 0 or lowest perf, and max_perf defaults to be all 1s or highest perf. In those cases, we are safe to directly restore the saved values.

Hi Jie,

Agreed, those values would be safe, although they are not required reset defaults. A platform could reset MAX to a lower value, such as lowest_perf, while the saved policy MIN is higher. Restoring MIN first would then temporarily result in MIN greater than MAX on non-PCC systems.

This preparation was added in response to Christian’s v3 comment [1]. I had already posted v5 [2] before receiving this reply, and it retains the preparation.

Christian, are you okay with dropping it and restoring the controls directly with cppc_set_perf(), as in v3?
Any general requirement for ordered MIN/MAX updates can then be handled in a separate CPPC core series.

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/

Thanks,
Sumit
....

maintainer
#6 lore raw

Re: [PATCH v4 1/4] cpufreq: CPPC: Keep the policy across CPU hotplug

Quoted 213 lines · Sumit Gupta
Quoted 213 lines
Quoted 192 lines
Quoted 135 lines · Sumit Gupta
Quoted 131 lines

Without online()/offline() callbacks, the cpufreq core fully tears down a policy during exit() when its last online CPU is offlined, and rebuilds it during init() when it 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. This avoids re-reading the CPPC capabilities on every offline/online, making CPU hotplug faster.

Move what init() and exit() did on hotplug into the new callbacks:

    - offline() requests the lowest desired performance, as exit() did.
    - 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.
    - online() also resyncs the frequency invariance counters, so that the first tick does not measure across the offline window.

The restore in online() uses cppc_set_perf(), which writes MIN before MAX. If the platform lowered MAX while the CPU was offline, writing the saved MIN could briefly leave MIN above MAX on registers not accessed through PCC, as PCC delivers the writes in one transaction. Raise MAX ahead of the restore when the saved MIN is above it.

---
   drivers/cpufreq/cppc_cpufreq.c | 128 +++++++++++++++++++++++++++++++++

   1 file changed, 128 insertions(+)
drivers/cpufreq/cppc_cpufreq.c +80 -0
index 80893844353c..4b3da9a3e122 100644
@@ -211,6 +211,29 @@ static void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
         }
    }
 
+/*
+ * Resync the counter snapshot, as the policy is kept across CPU hotplug and
+ * the first tick after online would otherwise span the offline window.
+ */
+static void cppc_cpufreq_cpu_fie_resync(struct cpufreq_policy *policy)
+{
+     struct cppc_freq_invariance *cppc_fi;
+     int cpu, ret;
+
+     if (fie_disabled)
+             return;
+
+     /* policy->cpus still holds related_cpus here, so skip offline CPUs. */
+     for_each_cpu_and(cpu, policy->cpus, cpu_online_mask) {
+             cppc_fi = &per_cpu(cppc_freq_inv, cpu);
+
+             ret = cppc_get_perf_ctrs(cpu, &cppc_fi->prev_perf_fb_ctrs);
+             if (ret)
+                     pr_debug("%s: failed to read perf counters for cpu:%d: %d\n",
+                              __func__, cpu, ret);
+     }
+}
+
    static void cppc_fie_kworker_init(void)
    {
         struct sched_attr attr = {
@@ -281,6 +304,10 @@ static inline void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
    {
    }
 
+static inline void cppc_cpufreq_cpu_fie_resync(struct cpufreq_policy *policy)
+{
+}
+
    static inline void cppc_freq_invariance_init(void)
    {
    }
@@ -735,6 +762,105 @@ static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
         return ret;
    }
 
+/*
+ * With offline() defined, the cpufreq core keeps the policy alive when
+ * a CPU is hotplugged out.
+ */
+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;
+
+     /*
+      * Request the lowest desired performance while the policy has no online
+      * CPU. Zeroing MIN and MAX makes cppc_set_perf() leave them unchanged.
+      */
+     perf_ctrls.desired_perf = cpu_data->perf_caps.lowest_perf;
+     perf_ctrls.min_perf = 0;
+     perf_ctrls.max_perf = 0;
+
+     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;
+}
+
+/*
+ * Raise MAX ahead of the full restore when the requested MIN is above the
+ * current MAX. cppc_set_perf() writes MIN before MAX, so the platform would
+ * otherwise briefly see MIN above MAX on registers not accessed through PCC.
+ * Lowering MAX is safe, as the MIN written first is never above it.
+ */
+static int
+cppc_cpufreq_prepare_perf_restore(unsigned int cpu,
+                               const struct cppc_perf_ctrls *target)
+{
+     struct cppc_perf_ctrls cur = {}, prep = {};
+     int ret;
+
+     ret = cppc_get_perf(cpu, &cur);
+     if (ret)
+             return ret;
+
+     if (!cur.max_perf || target->min_perf <= cur.max_perf)
+             return 0;
+
+     prep.desired_perf = target->desired_perf;
+     prep.min_perf = 0;      /* Zero leaves MIN unchanged. */
+     prep.max_perf = target->max_perf;
+
+     return cppc_set_perf(cpu, &prep);
+}
Quoted 8 lines

drivers/cpufreq/cppc_cpufreq.c static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 824

Quoted 7 lines
drivers/cpufreq/cppc_cpufreq.c +7 -0
+
+/*
+ * Restore what the CPU may have lost while offline, as the platform may have
+ * disabled CPPC and reset the performance controls. Never fail the callback,
+ * or the core would free the policy and leave the CPU without cpufreq. The
+ * governor redoes the control writes, so they are best effort, unlike the
+ * enable, which only a later online() can retry.

Sorry, I don't quite understand the last sentence.

Will rewrite in v5 as below:

  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.

Quoted 39 lines
Quoted 17 lines
drivers/cpufreq/cppc_cpufreq.c +17 -0
+ */
+static int cppc_cpufreq_cpu_online(struct cpufreq_policy *policy)
+{
+     struct cppc_cpudata *cpu_data = policy->driver_data;
+     unsigned int cpu = policy->cpu;
+     int ret;
+
+     cppc_cpufreq_cpu_fie_resync(policy);
+
+     ret = cppc_set_enable(cpu, true);
+     if (ret && ret != -EOPNOTSUPP) {
+             pr_warn("Failed to re-enable CPPC for CPU%u (%d)\n", cpu, ret);
+             return 0;
+     }
+
+     /*
+      * The platform may reset the controls while the CPU is offline, so

drivers/cpufreq/cppc_cpufreq.c static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 851

Quoted 10 lines
drivers/cpufreq/cppc_cpufreq.c +10 -0
+      * recompute min/max, clamp desired_perf into range, and reprogram them.
+      */
+     cppc_cpufreq_update_perf_limits(cpu_data, policy);
+
+     cpu_data->perf_ctrls.desired_perf =
+             clamp_t(u32, cpu_data->perf_ctrls.desired_perf,
+                     cpu_data->perf_ctrls.min_perf,
+                     cpu_data->perf_ctrls.max_perf);
+
+     ret = cppc_cpufreq_prepare_perf_restore(cpu, &cpu_data->perf_ctrls);

Actually, I don't quite think this is necessary?

The motivation of doing this is fair (as mentioned in v3), but what's the real consequence of transiently setting min_perf larger than max_perf? Platforms should be able to handle this.

Even if we have to fix it, it's supposed to be done in cppc_acpi.c.  The current ABI wraps many things up.  cppc_get_perf() reads 4 values - min_perf, max_perf, energy_perf, auto_sel.  cppc_set_perf writes 3 values - desired_perf, min_perf, max_perf.  The cppc_cpufreq driver would be able to handle performance setting cleaner if those are separated.

Quoted 8 lines
Quoted 1 line

I don't suggest we complicate the driver for now?

Agreed that it is not hotplug specific and can be done in the generic API.

cppc_set_perf() would have to know the programmed MIN and MAX to pick the write order. Separate accessors would let it read only those two, but that would add a read before every write, including fast_switch().

fast_switch() doesn't have to touch min/max_perf, but it did at the moment.

Quoted 2 lines

Caching what was last written would avoid that, but the platform can reset the registers while the CPU is offline or suspended.

Yeah, understood.  My question is still whether it's practically useful and we're complicating this.

Two reasons.

1. Platform should be able to handle min_perf being trasiently larger than max_perf, otherwise it would be fragile.

2. It depends on the reset values of the two registers.

Quoted 8 lines
Quoted 6 lines

I went over the ACPI Spec and didn't manage to find a descprition on what the default/reset values of min/max perf registers should be.

For a sensisble design, min_perf defaults to be 0 or lowest perf, and max_perf defaults to be all 1s or highest perf.  In those cases, we are safe to directly restore the saved values.

Hi Jie,

Hi Sumit, Jie

Quoted 10 lines

Agreed, those values would be safe, although they are not required reset defaults. A platform could reset MAX to a lower value, such as lowest_perf, while the saved policy MIN is higher. Restoring MIN first would then temporarily result in MIN greater than MAX on non-PCC systems.

This preparation was added in response to Christian’s v3 comment [1]. I had already posted v5 [2] before receiving this reply, and it retains the preparation.

I basically agree(d) with Jie here when I commented on v3:
"I think cppc_set_perf() needs some prep first before using it on reset values. We assume that reset value may be Autonomous Mode on, right? So we must never write MIN>MAX and vice versa. I think we may just have to read and write the 'otherwise-offending' value first on reset."
So I wanted to have this (as prep work) within cppc_set_perf() not in cppc-cpufreq,

Quoted 8 lines

Christian, are you okay with dropping it and restoring the controls directly with cppc_set_perf(), as in v3?
Any general requirement for ordered MIN/MAX updates can then be handled in a separate CPPC core series.

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/

author reviewer
#7 lore raw

Re: [PATCH v4 1/4] cpufreq: CPPC: Keep the policy across CPU hotplug

Quoted 239 lines · Christian Loehle

External email: Use caution opening links or attachments

Quoted 213 lines · Sumit Gupta
Quoted 213 lines
Quoted 192 lines
Quoted 135 lines · Sumit Gupta
Quoted 131 lines

Without online()/offline() callbacks, the cpufreq core fully tears down a policy during exit() when its last online CPU is offlined, and rebuilds it during init() when it 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. This avoids re-reading the CPPC capabilities on every offline/online, making CPU hotplug faster.

Move what init() and exit() did on hotplug into the new callbacks:

- offline() requests the lowest desired performance, as exit() did.
- 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.
- online() also resyncs the frequency invariance counters, so that the first tick does not measure across the offline window.

The restore in online() uses cppc_set_perf(), which writes MIN before MAX. If the platform lowered MAX while the CPU was offline, writing the saved MIN could briefly leave MIN above MAX on registers not accessed through PCC, as PCC delivers the writes in one transaction. Raise MAX ahead of the restore when the saved MIN is above it.

---
    drivers/cpufreq/cppc_cpufreq.c | 128 +++++++++++++++++++++++++++++++++
    1 file changed, 128 insertions(+)
drivers/cpufreq/cppc_cpufreq.c +80 -0
index 80893844353c..4b3da9a3e122 100644
@@ -211,6 +211,29 @@ static void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
         }
    }
 
+/*
+ * Resync the counter snapshot, as the policy is kept across CPU hotplug and
+ * the first tick after online would otherwise span the offline window.
+ */
+static void cppc_cpufreq_cpu_fie_resync(struct cpufreq_policy *policy)
+{
+     struct cppc_freq_invariance *cppc_fi;
+     int cpu, ret;
+
+     if (fie_disabled)
+             return;
+
+     /* policy->cpus still holds related_cpus here, so skip offline CPUs. */
+     for_each_cpu_and(cpu, policy->cpus, cpu_online_mask) {
+             cppc_fi = &per_cpu(cppc_freq_inv, cpu);
+
+             ret = cppc_get_perf_ctrs(cpu, &cppc_fi->prev_perf_fb_ctrs);
+             if (ret)
+                     pr_debug("%s: failed to read perf counters for cpu:%d: %d\n",
+                              __func__, cpu, ret);
+     }
+}
+
    static void cppc_fie_kworker_init(void)
    {
         struct sched_attr attr = {
@@ -281,6 +304,10 @@ static inline void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
    {
    }
 
+static inline void cppc_cpufreq_cpu_fie_resync(struct cpufreq_policy *policy)
+{
+}
+
    static inline void cppc_freq_invariance_init(void)
    {
    }
@@ -735,6 +762,105 @@ static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
         return ret;
    }
 
+/*
+ * With offline() defined, the cpufreq core keeps the policy alive when
+ * a CPU is hotplugged out.
+ */
+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;
+
+     /*
+      * Request the lowest desired performance while the policy has no online
+      * CPU. Zeroing MIN and MAX makes cppc_set_perf() leave them unchanged.
+      */
+     perf_ctrls.desired_perf = cpu_data->perf_caps.lowest_perf;
+     perf_ctrls.min_perf = 0;
+     perf_ctrls.max_perf = 0;
+
+     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;
+}
+
+/*
+ * Raise MAX ahead of the full restore when the requested MIN is above the
+ * current MAX. cppc_set_perf() writes MIN before MAX, so the platform would
+ * otherwise briefly see MIN above MAX on registers not accessed through PCC.
+ * Lowering MAX is safe, as the MIN written first is never above it.
+ */
+static int
+cppc_cpufreq_prepare_perf_restore(unsigned int cpu,
+                               const struct cppc_perf_ctrls *target)
+{
+     struct cppc_perf_ctrls cur = {}, prep = {};
+     int ret;
+
+     ret = cppc_get_perf(cpu, &cur);
+     if (ret)
+             return ret;
+
+     if (!cur.max_perf || target->min_perf <= cur.max_perf)
+             return 0;
+
+     prep.desired_perf = target->desired_perf;
+     prep.min_perf = 0;      /* Zero leaves MIN unchanged. */
+     prep.max_perf = target->max_perf;
+
+     return cppc_set_perf(cpu, &prep);
+}
Quoted 8 lines

drivers/cpufreq/cppc_cpufreq.c static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 824

Quoted 7 lines
drivers/cpufreq/cppc_cpufreq.c +7 -0
+
+/*
+ * Restore what the CPU may have lost while offline, as the platform may have
+ * disabled CPPC and reset the performance controls. Never fail the callback,
+ * or the core would free the policy and leave the CPU without cpufreq. The
+ * governor redoes the control writes, so they are best effort, unlike the
+ * enable, which only a later online() can retry.

Sorry, I don't quite understand the last sentence.

Will rewrite in v5 as below:

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.

Quoted 39 lines
Quoted 17 lines
drivers/cpufreq/cppc_cpufreq.c +17 -0
+ */
+static int cppc_cpufreq_cpu_online(struct cpufreq_policy *policy)
+{
+     struct cppc_cpudata *cpu_data = policy->driver_data;
+     unsigned int cpu = policy->cpu;
+     int ret;
+
+     cppc_cpufreq_cpu_fie_resync(policy);
+
+     ret = cppc_set_enable(cpu, true);
+     if (ret && ret != -EOPNOTSUPP) {
+             pr_warn("Failed to re-enable CPPC for CPU%u (%d)\n", cpu, ret);
+             return 0;
+     }
+
+     /*
+      * The platform may reset the controls while the CPU is offline, so

drivers/cpufreq/cppc_cpufreq.c static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 851

Quoted 10 lines
drivers/cpufreq/cppc_cpufreq.c +10 -0
+      * recompute min/max, clamp desired_perf into range, and reprogram them.
+      */
+     cppc_cpufreq_update_perf_limits(cpu_data, policy);
+
+     cpu_data->perf_ctrls.desired_perf =
+             clamp_t(u32, cpu_data->perf_ctrls.desired_perf,
+                     cpu_data->perf_ctrls.min_perf,
+                     cpu_data->perf_ctrls.max_perf);
+
+     ret = cppc_cpufreq_prepare_perf_restore(cpu, &cpu_data->perf_ctrls);

Actually, I don't quite think this is necessary?

The motivation of doing this is fair (as mentioned in v3), but what's the real consequence of transiently setting min_perf larger than max_perf? Platforms should be able to handle this.

Even if we have to fix it, it's supposed to be done in cppc_acpi.c. The current ABI wraps many things up. cppc_get_perf() reads 4 values - min_perf, max_perf, energy_perf, auto_sel. cppc_set_perf writes 3 values - desired_perf, min_perf, max_perf. The cppc_cpufreq driver would be able to handle performance setting cleaner if those are separated.

Quoted 8 lines
Quoted 1 line

I don't suggest we complicate the driver for now?

Agreed that it is not hotplug specific and can be done in the generic API.

cppc_set_perf() would have to know the programmed MIN and MAX to pick the write order. Separate accessors would let it read only those two, but that would add a read before every write, including fast_switch().

fast_switch() doesn't have to touch min/max_perf, but it did at the moment.

Quoted 2 lines

Caching what was last written would avoid that, but the platform can reset the registers while the CPU is offline or suspended.

Yeah, understood. My question is still whether it's practically useful and we're complicating this.

Two reasons.

1. Platform should be able to handle min_perf being trasiently larger than max_perf, otherwise it would be fragile.

2. It depends on the reset values of the two registers.

Quoted 8 lines
Quoted 6 lines

I went over the ACPI Spec and didn't manage to find a descprition on what the default/reset values of min/max perf registers should be.

For a sensisble design, min_perf defaults to be 0 or lowest perf, and max_perf defaults to be all 1s or highest perf. In those cases, we are safe to directly restore the saved values.

Hi Jie,

Hi Sumit, Jie

Quoted 10 lines

Agreed, those values would be safe, although they are not required reset defaults. A platform could reset MAX to a lower value, such as lowest_perf, while the saved policy MIN is higher. Restoring MIN first would then temporarily result in MIN greater than MAX on non-PCC systems.

This preparation was added in response to Christian’s v3 comment [1]. I had already posted v5 [2] before receiving this reply, and it retains the preparation.

Quoted 16 lines

I basically agree(d) with Jie here when I commented on v3:
"I think cppc_set_perf() needs some prep first before using it on reset values. We assume that reset value may be Autonomous Mode on, right? So we must never write MIN>MAX and vice versa. I think we may just have to read and write the 'otherwise-offending' value first on reset."
So I wanted to have this (as prep work) within cppc_set_perf() not in cppc-cpufreq,

Quoted 8 lines

Christian, are you okay with dropping it and restoring the controls directly with cppc_set_perf(), as in v3?
Any general requirement for ordered MIN/MAX updates can then be handled in a separate CPPC core series.

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/

Hi Christian, Jie,

Thanks for the clarification. I will drop the preparation from cppc_cpufreq in v6 and restore the controls directly using cppc_set_perf(). I will address the MIN/MAX write ordering in cppc_set_perf() in a separate series soon.

Thanks,
Sumit

maintainer
#8 lore raw

Re: [PATCH v4 1/4] cpufreq: CPPC: Keep the policy across CPU hotplug

Quoted 259 lines · Sumit Gupta
Quoted 239 lines · Christian Loehle

External email: Use caution opening links or attachments

Quoted 213 lines · Sumit Gupta
Quoted 213 lines
Quoted 192 lines
Quoted 135 lines · Sumit Gupta
Quoted 131 lines

Without online()/offline() callbacks, the cpufreq core fully tears down a policy during exit() when its last online CPU is offlined, and rebuilds it during init() when it 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. This avoids re-reading the CPPC capabilities on every offline/online, making CPU hotplug faster.

Move what init() and exit() did on hotplug into the new callbacks:

     - offline() requests the lowest desired performance, as exit() did.
     - 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.
     - online() also resyncs the frequency invariance counters, so that the first tick does not measure across the offline window.

The restore in online() uses cppc_set_perf(), which writes MIN before MAX. If the platform lowered MAX while the CPU was offline, writing the saved MIN could briefly leave MIN above MAX on registers not accessed through PCC, as PCC delivers the writes in one transaction. Raise MAX ahead of the restore when the saved MIN is above it.

---
    drivers/cpufreq/cppc_cpufreq.c | 128 +++++++++++++++++++++++++++++++++

    1 file changed, 128 insertions(+)
drivers/cpufreq/cppc_cpufreq.c +80 -0
index 80893844353c..4b3da9a3e122 100644
@@ -211,6 +211,29 @@ static void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
          }
     }
 
+/*
+ * Resync the counter snapshot, as the policy is kept across CPU hotplug and
+ * the first tick after online would otherwise span the offline window.
+ */
+static void cppc_cpufreq_cpu_fie_resync(struct cpufreq_policy *policy)
+{
+     struct cppc_freq_invariance *cppc_fi;
+     int cpu, ret;
+
+     if (fie_disabled)
+             return;
+
+     /* policy->cpus still holds related_cpus here, so skip offline CPUs. */
+     for_each_cpu_and(cpu, policy->cpus, cpu_online_mask) {
+             cppc_fi = &per_cpu(cppc_freq_inv, cpu);
+
+             ret = cppc_get_perf_ctrs(cpu, &cppc_fi->prev_perf_fb_ctrs);
+             if (ret)
+                     pr_debug("%s: failed to read perf counters for cpu:%d: %d\n",
+                              __func__, cpu, ret);
+     }
+}
+
     static void cppc_fie_kworker_init(void)
     {
          struct sched_attr attr = {
@@ -281,6 +304,10 @@ static inline void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
     {
     }
 
+static inline void cppc_cpufreq_cpu_fie_resync(struct cpufreq_policy *policy)
+{
+}
+
     static inline void cppc_freq_invariance_init(void)
     {
     }
@@ -735,6 +762,105 @@ static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
          return ret;
     }
 
+/*
+ * With offline() defined, the cpufreq core keeps the policy alive when
+ * a CPU is hotplugged out.
+ */
+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;
+
+     /*
+      * Request the lowest desired performance while the policy has no online
+      * CPU. Zeroing MIN and MAX makes cppc_set_perf() leave them unchanged.
+      */
+     perf_ctrls.desired_perf = cpu_data->perf_caps.lowest_perf;
+     perf_ctrls.min_perf = 0;
+     perf_ctrls.max_perf = 0;
+
+     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;
+}
+
+/*
+ * Raise MAX ahead of the full restore when the requested MIN is above the
+ * current MAX. cppc_set_perf() writes MIN before MAX, so the platform would
+ * otherwise briefly see MIN above MAX on registers not accessed through PCC.
+ * Lowering MAX is safe, as the MIN written first is never above it.
+ */
+static int
+cppc_cpufreq_prepare_perf_restore(unsigned int cpu,
+                               const struct cppc_perf_ctrls *target)
+{
+     struct cppc_perf_ctrls cur = {}, prep = {};
+     int ret;
+
+     ret = cppc_get_perf(cpu, &cur);
+     if (ret)
+             return ret;
+
+     if (!cur.max_perf || target->min_perf <= cur.max_perf)
+             return 0;
+
+     prep.desired_perf = target->desired_perf;
+     prep.min_perf = 0;      /* Zero leaves MIN unchanged. */
+     prep.max_perf = target->max_perf;
+
+     return cppc_set_perf(cpu, &prep);
+}
Quoted 8 lines

drivers/cpufreq/cppc_cpufreq.c static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 824

Quoted 7 lines
drivers/cpufreq/cppc_cpufreq.c +7 -0
+
+/*
+ * Restore what the CPU may have lost while offline, as the platform may have
+ * disabled CPPC and reset the performance controls. Never fail the callback,
+ * or the core would free the policy and leave the CPU without cpufreq. The
+ * governor redoes the control writes, so they are best effort, unlike the
+ * enable, which only a later online() can retry.

Sorry, I don't quite understand the last sentence.

Will rewrite in v5 as below:

   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.

Quoted 39 lines
Quoted 17 lines
drivers/cpufreq/cppc_cpufreq.c +17 -0
+ */
+static int cppc_cpufreq_cpu_online(struct cpufreq_policy *policy)
+{
+     struct cppc_cpudata *cpu_data = policy->driver_data;
+     unsigned int cpu = policy->cpu;
+     int ret;
+
+     cppc_cpufreq_cpu_fie_resync(policy);
+
+     ret = cppc_set_enable(cpu, true);
+     if (ret && ret != -EOPNOTSUPP) {
+             pr_warn("Failed to re-enable CPPC for CPU%u (%d)\n", cpu, ret);
+             return 0;
+     }
+
+     /*
+      * The platform may reset the controls while the CPU is offline, so

drivers/cpufreq/cppc_cpufreq.c static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 851

Quoted 10 lines
drivers/cpufreq/cppc_cpufreq.c +10 -0
+      * recompute min/max, clamp desired_perf into range, and reprogram them.
+      */
+     cppc_cpufreq_update_perf_limits(cpu_data, policy);
+
+     cpu_data->perf_ctrls.desired_perf =
+             clamp_t(u32, cpu_data->perf_ctrls.desired_perf,
+                     cpu_data->perf_ctrls.min_perf,
+                     cpu_data->perf_ctrls.max_perf);
+
+     ret = cppc_cpufreq_prepare_perf_restore(cpu, &cpu_data->perf_ctrls);

Actually, I don't quite think this is necessary?

The motivation of doing this is fair (as mentioned in v3), but what's the real consequence of transiently setting min_perf larger than max_perf? Platforms should be able to handle this.

Even if we have to fix it, it's supposed to be done in cppc_acpi.c.  The current ABI wraps many things up.  cppc_get_perf() reads 4 values - min_perf, max_perf, energy_perf, auto_sel.  cppc_set_perf writes 3 values - desired_perf, min_perf, max_perf.  The cppc_cpufreq driver would be able to handle performance setting cleaner if those are separated.

Quoted 8 lines
Quoted 1 line

I don't suggest we complicate the driver for now?

Agreed that it is not hotplug specific and can be done in the generic API.

cppc_set_perf() would have to know the programmed MIN and MAX to pick the write order. Separate accessors would let it read only those two, but that would add a read before every write, including fast_switch().

fast_switch() doesn't have to touch min/max_perf, but it did at the moment.

Quoted 2 lines

Caching what was last written would avoid that, but the platform can reset the registers while the CPU is offline or suspended.

Yeah, understood.  My question is still whether it's practically useful and we're complicating this.

Two reasons.

1. Platform should be able to handle min_perf being trasiently larger than max_perf, otherwise it would be fragile.

2. It depends on the reset values of the two registers.

Quoted 8 lines
Quoted 6 lines

I went over the ACPI Spec and didn't manage to find a descprition on what the default/reset values of min/max perf registers should be.

For a sensisble design, min_perf defaults to be 0 or lowest perf, and max_perf defaults to be all 1s or highest perf.  In those cases, we are safe to directly restore the saved values.

Hi Jie,

Hi Sumit, Jie

Quoted 10 lines

Agreed, those values would be safe, although they are not required reset defaults. A platform could reset MAX to a lower value, such as lowest_perf, while the saved policy MIN is higher. Restoring MIN first would then temporarily result in MIN greater than MAX on non-PCC systems.

This preparation was added in response to Christian’s v3 comment [1]. I had already posted v5 [2] before receiving this reply, and it retains the preparation.

Quoted 16 lines

I basically agree(d) with Jie here when I commented on v3:
"I think cppc_set_perf() needs some prep first before using it on reset values. We assume that reset value may be Autonomous Mode on, right? So we must never write MIN>MAX and vice versa. I think we may just have to read and write the 'otherwise-offending' value first on reset."
So I wanted to have this (as prep work) within cppc_set_perf() not in cppc-cpufreq,

Quoted 8 lines

Christian, are you okay with dropping it and restoring the controls directly with cppc_set_perf(), as in v3?
Any general requirement for ordered MIN/MAX updates can then be handled in a separate CPPC core series.

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/

Quoted 6 lines

Hi Christian, Jie,

Thanks for the clarification. I will drop the preparation from cppc_cpufreq in v6 and restore the controls directly using cppc_set_perf(). I will address the MIN/MAX write ordering in cppc_set_perf() in a separate series soon.

I don't quite understand why though, if anything it should land before this hotplug series? (It really should've landed with MIN/MAX_PERF support or at least to prepare for AUTOSEL support?)
I don't disagree with Jie that a sane platforms should be able to handle transient violations, but IMO just adhering to the spec here (which is absolutely doable) ensures that we never have to support a bunch of quirks sometime in the future, when we do want to be more strict about the spec.

In any case, after my v7 ACPI CPPC fixes land (fingers crossed this time), it seems pretty straightforward (and even a tiny optimization at that!). Here's what I had in mind, what do you think?
(Lightly tested, without seeing any violations. Needs some wiring into your series and would appreciate testing with AUTOSEL.)

--------->8-------------

From 49b45c6508bd306155db6e8d516ed61d0afa1b31 Mon Sep 17 00:00:00 2001
From: Christian Loehle <[email protected]>
Date: Thu, 17 Sep 2026 23:14:36 +0100
Subject: [PATCH] ACPI: CPPC: Order performance-control updates

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.

Fixes 76531df5e13b ("ACPI: CPPC: Add min and max perf register writing support")
---
 drivers/acpi/cppc_acpi.c       | 279 +++++++++++++++++++++++++--------
 drivers/cpufreq/cppc_cpufreq.c |  84 ++++++++--
 include/acpi/cppc_acpi.h       |   1 +
 3 files changed, 288 insertions(+), 76 deletions(-)
drivers/acpi/cppc_acpi.c +217 -62
index 80e2e6b32ce3..4f68ff3d8555 100644
@@ -3471,11 +3471,200 @@ 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, 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;
+	}
+
+	ret = cpc_read(cpu, min_reg, &min);
+	if (ret)
+		return ret;
+	ret = cpc_read(cpu, max_reg, &max);
+	if (ret)
+		return ret;
+	if (min > U32_MAX || max > U32_MAX)
+		return -EFAULT;
+	if (min > max)
+		return -EINVAL;
+
+	*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];
+	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, &current_min, &current_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;
+	if (target_min > target_max)
+		return -EINVAL;
+	if (desired_update && perf_ctrls->desired_perf &&
+	    (perf_ctrls->desired_perf < target_min ||
+	     perf_ctrls->desired_perf > target_max))
+		return -EINVAL;
+
+	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, perf_ctrls, widen,
+					       sync_desired);
+		if (ret)
+			return ret;
+		desired_done = sync_desired;
+	}
+
+	if (desired_update && !desired_done) {
+		ret = cppc_write_perf_controls(cpu, perf_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, perf_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.
+ *
  * Return: 0 for success, -ERRNO otherwise.
  */
 int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls)
@@ -3502,6 +3691,15 @@ 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;
+	if (desired_update && perf_ctrls->desired_perf &&
+	    min_update && max_update &&
+	    (perf_ctrls->desired_perf < perf_ctrls->min_perf ||
+	     perf_ctrls->desired_perf > perf_ctrls->max_perf))
+		return -EINVAL;
+
 	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);
@@ -3531,9 +3729,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 +3746,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 +3762,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 */
drivers/cpufreq/cppc_cpufreq.c +70 -14

169 lines not on this page · raw message

include/acpi/cppc_acpi.h +1 -0
index 3f0005abac64..fd1e891a045e 100644
@@ -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
author reviewer
#9 lore raw

Re: [PATCH v4 1/4] cpufreq: CPPC: Keep the policy across CPU hotplug

Quoted 270 lines · Christian Loehle

External email: Use caution opening links or attachments

Quoted 259 lines · Sumit Gupta
Quoted 239 lines · Christian Loehle

External email: Use caution opening links or attachments

Quoted 213 lines · Sumit Gupta
Quoted 213 lines
Quoted 192 lines
Quoted 135 lines · Sumit Gupta
Quoted 131 lines

Without online()/offline() callbacks, the cpufreq core fully tears down a policy during exit() when its last online CPU is offlined, and rebuilds it during init() when it 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. This avoids re-reading the CPPC capabilities on every offline/online, making CPU hotplug faster.

Move what init() and exit() did on hotplug into the new callbacks:

- offline() requests the lowest desired performance, as exit() did.
- 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.
- online() also resyncs the frequency invariance counters, so that the first tick does not measure across the offline window.

The restore in online() uses cppc_set_perf(), which writes MIN before MAX. If the platform lowered MAX while the CPU was offline, writing the saved MIN could briefly leave MIN above MAX on registers not accessed through PCC, as PCC delivers the writes in one transaction. Raise MAX ahead of the restore when the saved MIN is above it.

---
     drivers/cpufreq/cppc_cpufreq.c | 128 +++++++++++++++++++++++++++++++++
     1 file changed, 128 insertions(+)
drivers/cpufreq/cppc_cpufreq.c +80 -0
index 80893844353c..4b3da9a3e122 100644
@@ -211,6 +211,29 @@ static void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
          }
     }
 
+/*
+ * Resync the counter snapshot, as the policy is kept across CPU hotplug and
+ * the first tick after online would otherwise span the offline window.
+ */
+static void cppc_cpufreq_cpu_fie_resync(struct cpufreq_policy *policy)
+{
+     struct cppc_freq_invariance *cppc_fi;
+     int cpu, ret;
+
+     if (fie_disabled)
+             return;
+
+     /* policy->cpus still holds related_cpus here, so skip offline CPUs. */
+     for_each_cpu_and(cpu, policy->cpus, cpu_online_mask) {
+             cppc_fi = &per_cpu(cppc_freq_inv, cpu);
+
+             ret = cppc_get_perf_ctrs(cpu, &cppc_fi->prev_perf_fb_ctrs);
+             if (ret)
+                     pr_debug("%s: failed to read perf counters for cpu:%d: %d\n",
+                              __func__, cpu, ret);
+     }
+}
+
     static void cppc_fie_kworker_init(void)
     {
          struct sched_attr attr = {
@@ -281,6 +304,10 @@ static inline void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy)
     {
     }
 
+static inline void cppc_cpufreq_cpu_fie_resync(struct cpufreq_policy *policy)
+{
+}
+
     static inline void cppc_freq_invariance_init(void)
     {
     }
@@ -735,6 +762,105 @@ static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
          return ret;
     }
 
+/*
+ * With offline() defined, the cpufreq core keeps the policy alive when
+ * a CPU is hotplugged out.
+ */
+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;
+
+     /*
+      * Request the lowest desired performance while the policy has no online
+      * CPU. Zeroing MIN and MAX makes cppc_set_perf() leave them unchanged.
+      */
+     perf_ctrls.desired_perf = cpu_data->perf_caps.lowest_perf;
+     perf_ctrls.min_perf = 0;
+     perf_ctrls.max_perf = 0;
+
+     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;
+}
+
+/*
+ * Raise MAX ahead of the full restore when the requested MIN is above the
+ * current MAX. cppc_set_perf() writes MIN before MAX, so the platform would
+ * otherwise briefly see MIN above MAX on registers not accessed through PCC.
+ * Lowering MAX is safe, as the MIN written first is never above it.
+ */
+static int
+cppc_cpufreq_prepare_perf_restore(unsigned int cpu,
+                               const struct cppc_perf_ctrls *target)
+{
+     struct cppc_perf_ctrls cur = {}, prep = {};
+     int ret;
+
+     ret = cppc_get_perf(cpu, &cur);
+     if (ret)
+             return ret;
+
+     if (!cur.max_perf || target->min_perf <= cur.max_perf)
+             return 0;
+
+     prep.desired_perf = target->desired_perf;
+     prep.min_perf = 0;      /* Zero leaves MIN unchanged. */
+     prep.max_perf = target->max_perf;
+
+     return cppc_set_perf(cpu, &prep);
+}
Quoted 8 lines

drivers/cpufreq/cppc_cpufreq.c static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 824

Quoted 7 lines
drivers/cpufreq/cppc_cpufreq.c +7 -0
+
+/*
+ * Restore what the CPU may have lost while offline, as the platform may have
+ * disabled CPPC and reset the performance controls. Never fail the callback,
+ * or the core would free the policy and leave the CPU without cpufreq. The
+ * governor redoes the control writes, so they are best effort, unlike the
+ * enable, which only a later online() can retry.

Sorry, I don't quite understand the last sentence.

Will rewrite in v5 as below:

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.

Quoted 39 lines
Quoted 17 lines
drivers/cpufreq/cppc_cpufreq.c +17 -0
+ */
+static int cppc_cpufreq_cpu_online(struct cpufreq_policy *policy)
+{
+     struct cppc_cpudata *cpu_data = policy->driver_data;
+     unsigned int cpu = policy->cpu;
+     int ret;
+
+     cppc_cpufreq_cpu_fie_resync(policy);
+
+     ret = cppc_set_enable(cpu, true);
+     if (ret && ret != -EOPNOTSUPP) {
+             pr_warn("Failed to re-enable CPPC for CPU%u (%d)\n", cpu, ret);
+             return 0;
+     }
+
+     /*
+      * The platform may reset the controls while the CPU is offline, so

drivers/cpufreq/cppc_cpufreq.c static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 851

Quoted 10 lines
drivers/cpufreq/cppc_cpufreq.c +10 -0
+      * recompute min/max, clamp desired_perf into range, and reprogram them.
+      */
+     cppc_cpufreq_update_perf_limits(cpu_data, policy);
+
+     cpu_data->perf_ctrls.desired_perf =
+             clamp_t(u32, cpu_data->perf_ctrls.desired_perf,
+                     cpu_data->perf_ctrls.min_perf,
+                     cpu_data->perf_ctrls.max_perf);
+
+     ret = cppc_cpufreq_prepare_perf_restore(cpu, &cpu_data->perf_ctrls);

Actually, I don't quite think this is necessary?

The motivation of doing this is fair (as mentioned in v3), but what's the real consequence of transiently setting min_perf larger than max_perf? Platforms should be able to handle this.

Even if we have to fix it, it's supposed to be done in cppc_acpi.c. The current ABI wraps many things up. cppc_get_perf() reads 4 values - min_perf, max_perf, energy_perf, auto_sel. cppc_set_perf writes 3 values - desired_perf, min_perf, max_perf. The cppc_cpufreq driver would be able to handle performance setting cleaner if those are separated.

Quoted 8 lines
Quoted 1 line

I don't suggest we complicate the driver for now?

Agreed that it is not hotplug specific and can be done in the generic API.

cppc_set_perf() would have to know the programmed MIN and MAX to pick the write order. Separate accessors would let it read only those two, but that would add a read before every write, including fast_switch().

fast_switch() doesn't have to touch min/max_perf, but it did at the moment.

Quoted 2 lines

Caching what was last written would avoid that, but the platform can reset the registers while the CPU is offline or suspended.

Yeah, understood. My question is still whether it's practically useful and we're complicating this.

Two reasons.

1. Platform should be able to handle min_perf being trasiently larger than max_perf, otherwise it would be fragile.

2. It depends on the reset values of the two registers.

Quoted 8 lines
Quoted 6 lines

I went over the ACPI Spec and didn't manage to find a descprition on what the default/reset values of min/max perf registers should be.

For a sensisble design, min_perf defaults to be 0 or lowest perf, and max_perf defaults to be all 1s or highest perf. In those cases, we are safe to directly restore the saved values.

Hi Jie,

Hi Sumit, Jie

Quoted 10 lines

Agreed, those values would be safe, although they are not required reset defaults. A platform could reset MAX to a lower value, such as lowest_perf, while the saved policy MIN is higher. Restoring MIN first would then temporarily result in MIN greater than MAX on non-PCC systems.

This preparation was added in response to Christian’s v3 comment [1]. I had already posted v5 [2] before receiving this reply, and it retains the preparation.

Quoted 16 lines

I basically agree(d) with Jie here when I commented on v3:
"I think cppc_set_perf() needs some prep first before using it on reset values. We assume that reset value may be Autonomous Mode on, right? So we must never write MIN>MAX and vice versa. I think we may just have to read and write the 'otherwise-offending' value first on reset."
So I wanted to have this (as prep work) within cppc_set_perf() not in cppc-cpufreq,

Quoted 8 lines

Christian, are you okay with dropping it and restoring the controls directly with cppc_set_perf(), as in v3?
Any general requirement for ordered MIN/MAX updates can then be handled in a separate CPPC core series.

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/

Quoted 6 lines

Hi Christian, Jie,

Thanks for the clarification. I will drop the preparation from cppc_cpufreq in v6 and restore the controls directly using cppc_set_perf(). I will address the MIN/MAX write ordering in cppc_set_perf() in a separate series soon.

Quoted 9 lines

I don't quite understand why though, if anything it should land before this hotplug series? (It really should've landed with MIN/MAX_PERF support or at least to prepare for AUTOSEL support?)
I don't disagree with Jie that a sane platforms should be able to handle transient violations, but IMO just adhering to the spec here (which is absolutely doable) ensures that we never have to support a bunch of quirks sometime in the future, when we do want to be more strict about the spec.

The ordering affects every cppc_set_perf() caller, not only the restore here. So I planned it separately on top of your v7. Taking it as a prerequisite works too.

Quoted 6 lines

In any case, after my v7 ACPI CPPC fixes land (fingers crossed this time), it seems pretty straightforward (and even a tiny optimization at that!). Here's what I had in mind, what do you think?
(Lightly tested, without seeing any violations. Needs some wiring into your series and would appreciate testing with AUTOSEL.)

With your v7 and the ordering patch applied, cppc_cpufreq probe fails on my platform, where both MIN_PERF and MAX_PERF read zero until OSPM programs them.
- init() passes max_perf = 0, so max_update is false and the ordered path keeps that zero as the upper bound.
- Desired then falls outside [0, 0], cppc_set_perf() returns -EINVAL and init() treats it as fatal:

CPPC Cpufreq:Err setting perf value:XXX on CPU:X. ret:-22
cpufreq: cpufreq_register_driver: No CPU initialized for driver
cppc_cpufreq

The change below makes init() request [MIN=0, MAX=highest_perf] when it reads a zero MAX:

--- a/drivers/cpufreq/cppc_cpufreq.c
+++ b/drivers/cpufreq/cppc_cpufreq.c
+ if (!cpu_data->perf_ctrls.max_perf) {
+ cpu_data->perf_ctrls.min_perf = 0;
+ cpu_data->perf_ctrls.min_perf_valid = true;
+ cpu_data->perf_ctrls.max_perf = caps->highest_perf;
+ }
+
desired_perf = caps->highest_perf;

With that, cppc_cpufreq probes and the MIN and MAX registers follow the policy limits with AUTOSEL both disabled and enabled.

There is another case. Two updates to the same CPU can also interleave, as fast_switch() is not serialized against a sysfs update, leaving MIN above MAX. A later request should be able to repair that, so I removed the early rejection of a live MIN > MAX. The target_min > target_max check still rejects requests that would leave an invalid interval.

--- a/drivers/acpi/cppc_acpi.c
+++ b/drivers/acpi/cppc_acpi.c
if (min > U32_MAX || max > U32_MAX)
return -EFAULT;
- if (min > max)
- return -EINVAL;

If you agree with both changes, I can fold them into your ordering patch and carry it as the first patch of my series, or would you rather post it yourself?

Serializing that race needs something the fast path can take, as fast_switch() cannot take policy->rwsem. It only affects policies where fast switching is enabled, which excludes PCC. How would you prefer to handle it?

Thanks,
Sumit

....

author reviewer
#10 lore raw

PATCH v4 2/4 ACPI: CPPC: Make autonomous selection helpers take a u64

In reply to Sumit Gupta

cppc_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
index fef54fcd00b7..9e882b3911e6 100644
@@ -1904,23 +1904,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);
 
@@ -1929,7 +1917,7 @@ EXPORT_SYMBOL_GPL(cppc_get_auto_sel);
  * @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
index d4ff8b228f86..de0b29bfad61 100644
@@ -499,7 +499,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
index 4b3da9a3e122..b50d3f893b1d 100644
@@ -990,7 +990,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);
@@ -1002,7 +1002,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
index 94a6277edab2..3394e1b208be 100644
@@ -187,8 +187,8 @@ extern int cppc_set_epp_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls, bool e
 extern 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_set_auto_sel(int cpu, bool enable);
+extern int cppc_get_auto_sel(int cpu, u64 *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);
@@ -285,11 +285,11 @@ 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;
 }
-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
author reviewer
#11 lore raw

PATCH v4 3/4 cpufreq: CPPC: Preserve OSPM-set registers across hotplug and unload

In reply to Sumit Gupta

Values written to OSPM-set CPPC registers via sysfs can be lost in two ways:

- Across CPU hotplug: the platform may reset a CPU's registers while it is offline.
- On driver unload: the value the driver wrote is left in the register instead of returning to its pre-driver state.

Add a small table-driven mechanism that handles both:

- On init(), capture each register's firmware value before the driver programs anything.
- On offline(), read back each register's current value (whatever was last set via sysfs) so it can be reapplied, then restore the firmware value.
- On online(), reapply the value captured at offline() after the performance request is re-established.
- On exit(), nothing is needed, as the core calls offline() first, which already restored the firmware values.

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. If autonomous selection is already disabled, those writes may be ignored.

---
 drivers/cpufreq/cppc_cpufreq.c | 191 ++++++++++++++++++++++++++++++++-
 1 file changed, 190 insertions(+), 1 deletion(-)
drivers/cpufreq/cppc_cpufreq.c +190 -1
index b50d3f893b1d..f8628b1fc6b7 100644
@@ -28,6 +28,183 @@
 
 static struct cpufreq_driver cppc_cpufreq_driver;
 
+/*
+ * OSPM-set CPPC registers tracked for save/restore. A value set via sysfs is
+ * reapplied from online() across CPU hotplug, and the firmware value is
+ * restored from offline().
+ *
+ * Autonomous Selection (auto_sel) is kept first, as writes to the registers
+ * listed after it only have meaning while autonomous selection 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,
+};
+
+/*
+ * Per-policy values saved for each register in cppc_saved_regs[]:
+ *   firmware_val  - value before the driver touched it, captured at init()
+ *                   and restored while the policy is offline. U64_MAX if it
+ *                   could not be read
+ *   requested_val - value in effect when the policy last went offline,
+ *                   reapplied at online(). U64_MAX if none
+ */
+struct cppc_saved_vals {
+	u64 firmware_val;
+	u64 requested_val;
+};
+
+struct cppc_policy_state {
+	struct cppc_saved_vals regs[CPPC_NR_SAVED_REGS];
+};
+
+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 until the core fills it in after init(), so
+	 * fall back to policy->cpus, 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 register's current value. init() saves the firmware value, before
+ * the driver programs anything, and offline() saves the requested value, to
+ * reapply at online().
+ */
+static void cppc_cpufreq_save_regs(struct cpufreq_policy *policy,
+				   enum cppc_saved_type saved_type)
+{
+	struct cppc_policy_state *st = cppc_cpufreq_policy_state(policy);
+	unsigned int cpu = policy->cpu;
+	u64 val;
+	int i;
+
+	for (i = 0; i < CPPC_NR_SAVED_REGS; i++) {
+		if (cppc_saved_regs[i].get(cpu, &val))
+			val = U64_MAX;
+
+		if (saved_type == CPPC_SAVED_FIRMWARE) {
+			st->regs[i].firmware_val = val;
+			st->regs[i].requested_val = U64_MAX;
+		} else {
+			st->regs[i].requested_val = val;
+		}
+	}
+}
+
+static u64 cppc_cpufreq_saved_reg_value(const struct cppc_saved_vals *st,
+					enum cppc_saved_reg_id reg,
+					enum cppc_saved_type saved_type)
+{
+	if (saved_type == CPPC_SAVED_FIRMWARE)
+		return st[reg].firmware_val;
+
+	return st[reg].requested_val;
+}
+
+/*
+ * Write one tracked register, skipping values that were never saved and
+ * registers the platform does not allow writing.
+ */
+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 saved";
+	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. If autonomous selection is already
+ * disabled, those writes are best effort. 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_saved_vals *st = cppc_cpufreq_policy_state(policy)->regs;
+	unsigned int cpu = policy->cpu;
+	u64 auto_sel, val;
+	int i;
+
+	auto_sel = cppc_cpufreq_saved_reg_value(st, 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 = cppc_cpufreq_saved_reg_value(st, 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,
@@ -747,6 +924,8 @@ static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
 	policy->cur = cppc_perf_to_khz(caps, caps->highest_perf);
 	cpu_data->perf_ctrls.desired_perf =  caps->highest_perf;
 
+	cppc_cpufreq_save_regs(policy, CPPC_SAVED_FIRMWARE);
+
 	ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
 	if (ret) {
 		pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
@@ -773,6 +952,10 @@ 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 while offline. */
+	cppc_cpufreq_save_regs(policy, CPPC_SAVED_REQUESTED);
+	cppc_cpufreq_apply_saved_regs(policy, CPPC_SAVED_FIRMWARE);
+
 	/*
 	 * Request the lowest desired performance while the policy has no online
 	 * CPU. Zeroing MIN and MAX makes cppc_set_perf() leave them unchanged.
@@ -822,6 +1005,8 @@ cppc_cpufreq_prepare_perf_restore(unsigned int cpu,
  * or the core would free the policy and leave the CPU without cpufreq. The
  * governor redoes the control writes, so they are best effort, unlike the
  * enable, which only a later online() can retry.
+ *
+ * Also reapply the OSPM-set registers that offline() reset to firmware values.
  */
 static int cppc_cpufreq_cpu_online(struct cpufreq_policy *policy)
 {
@@ -854,9 +1039,13 @@ static int cppc_cpufreq_cpu_online(struct cpufreq_policy *policy)
 			 cpu, ret);
 
 	ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls);
-	if (ret)
+	if (ret) {
 		pr_debug("Failed to reapply perf request on CPU%u (%d)\n",
 			 cpu, ret);
+		return 0;
+	}
+
+	cppc_cpufreq_apply_saved_regs(policy, CPPC_SAVED_REQUESTED);
 
 	return 0;
 }
Signature · 1 line
2.34.1
author reviewer
#12 lore raw

PATCH v4 4/4 cpufreq: CPPC: Preserve OSPM-set registers across suspend/resume

In reply to Sumit Gupta

The driver preserves the OSPM-set registers across CPU hotplug, but system suspend/resume is a separate path. On platforms that reset those registers or the performance controls across suspend, the values are lost.

Reuse the same save/restore mechanism for suspend/resume:

- suspend() saves the current OSPM-set values, restores the firmware values and sets a per-policy flag. It runs before devices are suspended and before the secondary CPUs go offline, while CPPC access is still safe. It also covers a policy whose CPUs stay online, for which offline() never runs.
- offline() sees the flag and leaves those register accesses alone, parking only the performance request.
- resume() needs the same steps as online(). The core has already run online() for a policy whose CPUs were offlined and brought back. So, resume() calls it only for a policy that still has the flag set.
- online() clears the flag, so that a later offline() takes a fresh snapshot.

---
 drivers/cpufreq/cppc_cpufreq.c | 54 ++++++++++++++++++++++++++++++++--
 1 file changed, 51 insertions(+), 3 deletions(-)
drivers/cpufreq/cppc_cpufreq.c +51 -3
index f8628b1fc6b7..32f38b0c492b 100644
@@ -87,6 +87,12 @@ struct cppc_saved_vals {
 
 struct cppc_policy_state {
 	struct cppc_saved_vals regs[CPPC_NR_SAVED_REGS];
+	/*
+	 * Set by suspend() after it saves the OSPM-set values and restores the
+	 * firmware ones, so a later offline() does not repeat those accesses.
+	 * Cleared at init() and by online().
+	 */
+	bool suspend_regs_handled;
 };
 
 static DEFINE_PER_CPU(struct cppc_policy_state, cppc_policy_state);
@@ -125,6 +131,9 @@ static void cppc_cpufreq_save_regs(struct cpufreq_policy *policy,
 	u64 val;
 	int i;
 
+	if (saved_type == CPPC_SAVED_FIRMWARE)
+		st->suspend_regs_handled = false;
+
 	for (i = 0; i < CPPC_NR_SAVED_REGS; i++) {
 		if (cppc_saved_regs[i].get(cpu, &val))
 			val = U64_MAX;
@@ -952,9 +961,14 @@ 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 while offline. */
-	cppc_cpufreq_save_regs(policy, CPPC_SAVED_REQUESTED);
-	cppc_cpufreq_apply_saved_regs(policy, CPPC_SAVED_FIRMWARE);
+	/*
+	 * Leave the platform in its pre-driver state while offline, unless
+	 * suspend() already did so earlier in this suspend cycle.
+	 */
+	if (!cppc_cpufreq_policy_state(policy)->suspend_regs_handled) {
+		cppc_cpufreq_save_regs(policy, CPPC_SAVED_REQUESTED);
+		cppc_cpufreq_apply_saved_regs(policy, CPPC_SAVED_FIRMWARE);
+	}
 
 	/*
 	 * Request the lowest desired performance while the policy has no online
@@ -1014,6 +1028,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;
+
 	cppc_cpufreq_cpu_fie_resync(policy);
 
 	ret = cppc_set_enable(cpu, true);
@@ -1050,6 +1066,36 @@ static int cppc_cpufreq_cpu_online(struct cpufreq_policy *policy)
 	return 0;
 }
 
+/*
+ * Save the OSPM-set values and restore the firmware values here, while CPPC
+ * access is still safe. Secondary CPUs go offline much later, with devices
+ * already suspended, so offline() is too late for these accesses and leaves
+ * them alone until resume. Doing it here covers every policy, including one
+ * whose CPUs stay online, for which offline() never runs.
+ */
+static int cppc_cpufreq_cpu_suspend(struct cpufreq_policy *policy)
+{
+	cppc_cpufreq_save_regs(policy, CPPC_SAVED_REQUESTED);
+	cppc_cpufreq_apply_saved_regs(policy, CPPC_SAVED_FIRMWARE);
+	cppc_cpufreq_policy_state(policy)->suspend_regs_handled = true;
+
+	return 0;
+}
+
+/*
+ * CPUs offlined during suspend come back before the core calls resume(), so
+ * online() has already run for their policies and cleared the flag. It remains
+ * set only for a policy whose CPUs stayed online, and only that policy needs
+ * online() here.
+ */
+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 void cppc_cpufreq_cpu_exit(struct cpufreq_policy *policy)
 {
 	struct cppc_cpudata *cpu_data = policy->driver_data;
@@ -1364,6 +1410,8 @@ 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,
 	.name = "cppc_cpufreq",
Signature · 1 line
2.34.1
author reviewer
#13 lore raw
In reply to Sumit Gupta
Quoted 16 lines · Sumit Gupta

This series keeps the CPPC cpufreq policy alive across CPU hotplug and preserves the OSPM-set CPPC registers (Energy Performance Preference, Autonomous Activity Window, Autonomous Selection - set via sysfs).

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.
- On driver unload: the driver-written value is left in the register instead of returning to its pre-driver state.

Handle these with:

Quoted 7 lines

- Patch 1: adds online()/offline() callbacks so the core keeps policy alive across CPU hotplug instead of tearing it down and rebuilding it.
- Patch 2: makes the autonomous selection register helpers take a u64.
- Patch 3: adds a table-driven mechanism that captures each register's firmware value at init(), restores it from offline(), and reapplies the OSPM-set value from online().
- Patch 4: extends the same save/restore to system suspend/resume.

Gentle reminder.
Could this be considered for queuing if nothing further needs addressing.

Thanks,
Sumit

Quoted 31 lines

v3[3] -> v4:
- Patch 1:
- offline() parks the perf request at lowest_perf, as exit() did.
- online() resyncs the frequency invariance counters.
- raise MAX before the perf restore when the saved MIN is above it.
- Patch 3:
- write auto_sel first when enabling it and last when disabling it.
- replace the four save/restore helpers into save_regs() and apply_saved_regs(), each taking the firmware or requested type.
- keep the per-policy saved values in one struct, and name each register for the pr_debug diagnostics.
- Patch 4:
- suspend() also restores the firmware values and flags it, so offline() skips them and resume() only handles still-online policies.

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       |  20 +-
  drivers/cpufreq/amd-pstate.c   |   2 +-
  drivers/cpufreq/cppc_cpufreq.c | 369 ++++++++++++++++++++++++++++++++-
  include/acpi/cppc_acpi.h       |   8 +-
  4 files changed, 376 insertions(+), 23 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]/

maintainer
#14 lore raw
Quoted 26 lines · Sumit Gupta
Quoted 16 lines · Sumit Gupta

This series keeps the CPPC cpufreq policy alive across CPU hotplug and preserves the OSPM-set CPPC registers (Energy Performance Preference, Autonomous Activity Window, Autonomous Selection - set via sysfs).

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.
  - On driver unload: the driver-written value is left in the register instead of returning to its pre-driver state.

Handle these with:

Quoted 7 lines

  - Patch 1: adds online()/offline() callbacks so the core keeps policy alive across CPU hotplug instead of tearing it down and rebuilding it.
  - Patch 2: makes the autonomous selection register helpers take a u64.
  - Patch 3: adds a table-driven mechanism that captures each register's firmware value at init(), restores it from offline(), and reapplies the OSPM-set value from online().
  - Patch 4: extends the same save/restore to system suspend/resume.

Quoted 2 lines

Gentle reminder.
Could this be considered for queuing if nothing further needs addressing.

FWIW both sashiko findings look legit to me, the feedback counters one I wouldn't consider that drastic, given that these counters are expected to have 'fuzzy' readings anyhow, the second one is worse (losing sysfs settings on cpu_online())

#15 lore raw
Quoted 28 lines · Christian Loehle
Quoted 26 lines · Sumit Gupta
Quoted 23 lines · Sumit Gupta

This series keeps the CPPC cpufreq policy alive across CPU hotplug and preserves the OSPM-set CPPC registers (Energy Performance Preference, Autonomous Activity Window, Autonomous Selection - set via sysfs).

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.
- 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: adds online()/offline() callbacks so the core keeps policy alive across CPU hotplug instead of tearing it down and rebuilding it.
- Patch 2: makes the autonomous selection register helpers take a u64.
- Patch 3: adds a table-driven mechanism that captures each register's firmware value at init(), restores it from offline(), and reapplies the OSPM-set value from online().
- Patch 4: extends the same save/restore to system suspend/resume.

Quoted 7 lines
Quoted 2 lines

Gentle reminder.
Could this be considered for queuing if nothing further needs addressing.

FWIW both sashiko findings look legit to me, the feedback counters one I wouldn't consider that drastic, given that these counters are expected to have 'fuzzy' readings anyhow, the second one is worse (losing sysfs settings on cpu_online())

I agree.

Besides, as I said elsewhere, I want this series to go in before any other pending changes related to CPPC:

https://lore.kernel.org/linux-acpi/[email protected]/

and I really would like to get some tags on it before it goes in. It clearly is not ready for 7.3, but it may be applicable early for 7.4 if people care to respond to it.

Thanks!

author reviewer
#16 lore raw
Quoted 41 lines · Rafael J. Wysocki (Intel)

External email: Use caution opening links or attachments

Quoted 25 lines · Christian Loehle
Quoted 24 lines · Sumit Gupta
Quoted 23 lines · Sumit Gupta

This series keeps the CPPC cpufreq policy alive across CPU hotplug and preserves the OSPM-set CPPC registers (Energy Performance Preference, Autonomous Activity Window, Autonomous Selection - set via sysfs).

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.
- 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: adds online()/offline() callbacks so the core keeps policy alive across CPU hotplug instead of tearing it down and rebuilding it.
- Patch 2: makes the autonomous selection register helpers take a u64.
- Patch 3: adds a table-driven mechanism that captures each register's firmware value at init(), restores it from offline(), and reapplies the OSPM-set value from online().
- Patch 4: extends the same save/restore to system suspend/resume.

Quoted 6 lines
Quoted 2 lines

Gentle reminder.
Could this be considered for queuing if nothing further needs addressing.

FWIW both sashiko findings look legit to me, the feedback counters one I wouldn't consider that drastic, given that these counters are expected to have 'fuzzy' readings anyhow, the second one is worse (losing sysfs settings on cpu_online())

I agree.

Besides, as I said elsewhere, I want this series to go in before any other pending changes related to CPPC:

Quoted 7 lines

https://lore.kernel.org/linux-acpi/[email protected]/

and I really would like to get some tags on it before it goes in. It clearly is not ready for 7.3, but it may be applicable early for 7.4 if people care to respond to it.

Thanks!

Thanks for pointing this out.

I did not receive a Sashiko review email for this series, so I was unaware of the findings. I have found the web review now. I will go through both findings and address them in v5.

Thanks,
Sumit