Skip to content

cpufreq: apple-soc: park idle clusters at their slowest state - #1988

Draft
RyanTheTide wants to merge 2 commits into
mainfrom
cpufreq/park-idle-clusters
Draft

RyanTheTide wants to merge 2 commits into
mainfrom
cpufreq/park-idle-clusters

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Split out of #1811: the two cpufreq commits that park an idle cluster at its slowest state.

  • 6b72af7a8c93 cpufreq: apple-soc: park an idle cluster at its slowest state
  • b17d649b440c cpufreq: apple-soc: park idle clusters on lazy_verify SoCs too

They add a WFI-park idle state between WFI and the deep state in cpuidle-apple.c, which changes the idle-state table, including the index s2idle picks, on every apple-cpuidle SoC.

Status: draft.

  • apple-dcp › cpuidle-s2idle fails on this branch: the test expects two idle states on t8140 with the deep state at index 1. The test needs updating for the new table, and find_deepest_state() must still land on the deep state for s2idle.
  • Not yet run on hardware: s2idle x3 (rtc1 wakealarm) on each affected SoC, plus the idle-power and frequency numbers that justify parking.

The cluster DVFS controller holds the performance state it was last
asked for while every CPU of the cluster is idle, and the cluster
voltage follows that state. schedutil only runs from scheduler events
on the cluster's own CPUs, so nothing lowers the request once the
cluster goes idle. Real-time tasks get uclamp_min 1024 by default, so
a short periodic RT thread (an audio data loop, a threaded IRQ
handler, a 100 Hz supervisor loop) leaves an otherwise idle cluster at
its fastest state until the next wakeup.

On the MacBook Pro M1 Pro (J316s) at idle, the P clusters sat at
3036 MHz for 6-48% of the time. Setting sched_util_clamp_min_rt_default
to 0 dropped system power from 3.29 W to 2.98 W (SMC Total System
Power, 120 s rows, same boot), which is what the stale request costs.
The sysctl also takes the clock away from RT threads while they run,
so it cannot be the fix.

Add an idle state that parks the cluster instead. cpuidle-apple gets
a "WFI-park" state (exit latency 50 us, target residency 1 ms) and
calls the cpufreq driver's hooks around its WFI, and around the CPU
power-down state on M1 to M3. Once every online CPU of a cluster is in
one of those states, the driver requests the slowest state. The first
CPU to wake restores the last request, and the transition finishes
while it runs. Requests that arrive while the cluster is parked are
recorded and applied on wakeup, and the driver keeps reporting the
requested frequency. RT threads still run at the frequency their clamp
asks for.

Only SoCs that do not verify transitions park (T8103, T6000, T8112,
T6041). On M4 the second idle state used to be a copy of WFI kept for
s2idle; it is now the parking state, and s2idle uses it as before.

Assisted-by: LLM
Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
T8140 verifies every DVFS request, so the first version left it out.
Parking fits that scheme: the park request becomes the expected state
and the request that restores the cluster verifies it, the same way a
request finding the cluster idle verifies the one before it.

A cluster does not park while waited-for requests own it, while a
request is pending, or after a transition has failed. Fast-switch
requests and new fast die-temperature limits that arrive while the
cluster is parked are recorded and applied when it wakes. A waited-for
request unparks the cluster and is applied and verified at once.

Assisted-by: LLM
Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@freber

freber commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Claude Code wrote this up while I was putting this PR into a test kernel for my MacBook Air M3 (J615, T8122). It comes from reading the code. I haven't booted it yet.

With this PR the cpuidle-apple table is WFI (exit latency 1 us), WFI-park (50 us) and CPU PD (10 us), in that order. s2idle picks its state with find_deepest_state() in drivers/cpuidle/cpuidle.c. It walks the table from index 1 and skips every state whose exit latency is not above the best one so far.

if (dev->states_usage[i].disable ||
    s->exit_latency_ns <= latency_req ||

WFI-park sets enter_s2idle and comes first at 50 us, so CPU PD at 10 us gets skipped. On every SoC that has CPU PD, s2idle would then idle in WFI-park and the cores would not power down during suspend. Before this PR s2idle reached CPU PD. T8140 drops CPU PD, so the problem does not apply there.

The menu governor also stops at the first state whose exit latency is over the PM QoS limit. A limit between 10 and 50 us would now end its search at WFI-park, before CPU PD.

Separately, the cpuidle-s2idle case in tools/testing/selftests/drivers/gpu/apple-dcp/run.sh stops compiling with this PR. Its replay of cpuidle-apple.c has no mock for cpuidle_pause_and_lock() or EBUSY. Without the PR it passes all 122 assertions.

Leaving enter_s2idle off WFI-park looks like the smallest way to keep CPU PD for s2idle, unless the parking code needs it. I can check s2idle residency on the J615 (cpuidle/state*/s2idle/usage) with whichever version you pick.

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Early note on this draft (no full review yet):

  1. drivers/cpuidle/cpuidle-apple.c:174-175: the new WFI-park state uses a fixed exit_latency = 50 / target_residency = 1000. Leaving this state costs a cluster performance-state transition, which the cpufreq side already knows per cluster (its transition latency from the DT/OPP table). Derive the exit latency from that when the park ops are registered (or state where the 50 us comes from and that it bounds every SoC's transition), rather than a constant picked for one machine.

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Closing: the parking state uses a fixed latency and would keep s2idle out of CPU power-down on most SoCs; a rewrite should take the latency from the cluster's transition time.

@RyanTheTide RyanTheTide reopened this Nov 22, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Reopened and kept as a draft; the comment above lists what it still needs before review. cc @niteria

@rolotrealanis98

Copy link
Copy Markdown
Contributor

T6050 numbers (J716s, 16-inch M5 Pro, 27.3 beta stub) on our daily kernel, which carries this PR's two commits plus lazy verification for T6050, a park_stats file per policy, and the fix below. AC, idle desktop, performance profile (clusters requested at their top state, 4.38/4.61 GHz), Total System Power from the SMC, 60 s each:

  • idle_park=1: 8.05 W; idle_park=0: 8.49 W; idle_park=1 again: 7.79 W, so about 0.4-0.7 W saved (5-8%)
  • per 60 s with parking on: the two clusters with spare CPUs parked ~37-39 s each (16-20k parks); the boot cluster never parks (some CPU of it is always awake)
  • the idle-state table on T6050 is WFI and WFI-park

One fix we needed: parking never ran at first (0 parks and 0 skipped idle entries in 14 minutes on every policy), because ->init() takes the cluster's CPUs from policy->related_cpus, which the cpufreq core fills only after ->init() returns, so park_cpus stayed empty. Taking them from policy->cpus (which apple_soc_cpufreq_find_cluster() has just filled) fixes it:

-		cpumask_copy(&priv->park_cpus, policy->related_cpus);
+		/* policy->related_cpus is filled only after ->init() returns. */
+		cpumask_copy(&priv->park_cpus, policy->cpus);
 		priv->park = true;
-		for_each_cpu(i, policy->related_cpus) {
+		for_each_cpu(i, &priv->park_cpus) {
 			/* Pairs with the idle hooks' READ_ONCE(): priv is set up. */
 			smp_store_release(&per_cpu(apple_park_priv, i), priv);

Not run: s2idle with the new idle-state table; battery numbers.

@erikwb

erikwb commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

#3717 is a rewrite of this on current main, with your parking design credited in both commits. It parks from the existing WFI state instead of adding a WFI-park state, so the idle table and the s2idle pick don't change. cpufreq stays the only owner of the speed: a park never changes policy->cur, and the restore goes through main's lazily verified write. It's on for T6050 only for now.

On a J716c it parks on all three clusters with no failures and survives deep sleep. On an idle desktop it measured 16.61 W against 16.75 W without it. If #3717 lands, this draft can be closed as replaced.

IronsideXXVI pushed a commit to IronsideXXVI/linux that referenced this pull request Dec 2, 2026
An idle CPU cluster keeps the performance state it was last asked for,
and its voltage with it: schedutil only runs from scheduler events on
the cluster's CPUs. The cpufreq driver can park a cluster whose CPUs are
all idle at its slowest state and restore it when the first CPU wakes,
but it needs to know when each CPU enters and leaves idle.

From M4 on, the deepest idle state (index 1) is a copy of WFI. Let it
call a pair of hooks the cpufreq driver installs: @Enter before the WFI,
with the CPU's next timer event (cpuidle_device.next_hrtimer) and its
CPU latency limit, and, if @Enter returned a cookie, @EXIT with it after
the WFI. They run with RCU watching, so the state now has
CPUIDLE_FLAG_RCU_IDLE and leaves RCU itself around the WFI. Neither hook
may wait for the hardware.

No new idle state: the table, the exit latencies and target residencies
stay as they are, so the governors and the s2idle selector pick the same
state as before. The state's s2idle entry is plain WFI and calls no
hook. Index 0 and the M1 to M3 CPU PD state are unchanged.

The cpufreq driver may be a module, and frees what a cookie points to
when a policy goes away. A CPU runs @Enter, its WFI and @EXIT with
interrupts off from start to end, so once it has taken an interrupt it
is past @EXIT, and its next @Enter reads what was stored before the
interrupt was sent. apple_cpuidle_park_sync() sends a function call to
each online CPU of a mask and waits for it to run. A CPU in WFI wakes
for it, so the wait is bounded, and only the CPUs of the mask are
waited for: a CPU of another cluster between its own hooks does not
hold it up. The cpufreq driver calls it on a cluster's CPUs before it
frees the cluster, and clearing the hooks calls it on every CPU.

Update the apple-dcp cpuidle-s2idle host replay for the new entry
functions, and check that index 1 calls the hooks around its WFI, that
index 0 and s2idle do not, that @EXIT gets the cookie of an @Enter that
took part and runs only then, that the CPU has left RCU at its WFI and
is back for the hooks, that interrupts stay off from @Enter to @EXIT,
and that apple_cpuidle_park_sync() calls only the CPUs it is given, and
waits.

Based on the parking design in Ryan Murray's draft
(aurora-silicon#1988).

Signed-off-by: Erik Bourget <erik@ebourget.net>
IronsideXXVI pushed a commit to IronsideXXVI/linux that referenced this pull request Dec 2, 2026
schedutil only runs from scheduler events on a cluster's CPUs, so an
idle cluster keeps the last state it was asked for, and its voltage with
it. A short burst of work, or a real-time task, which runs at full speed
by default, leaves an idle cluster at a high state until its next
wakeup. Move a cluster whose CPUs are all idle to the slowest state of
its frequency table, and back to cpufreq's request when the first of
them wakes.

cpufreq stays the only owner of the request, and does not see a park.
Parking changes neither policy->cur nor the expected state, the pending
request or the verify deadline. ->get() reports the expected state of a
parked cluster, the last state issued to it (a pending request or a
newer limit may change what the restore writes), and reads the cluster
under the lock a park takes. The expected state is cpufreq's last
request, clamped to the limits and accepted by the cluster. The restore
writes cpufreq's pending request if there is one, else the expected
state, clamped to the limits in force then (a QoS ceiling the policy
work has yet to pass on included), as an ordinary lazily verified
request: the verify timer checks it, and a refusal stops requests and is
reported as for any other. A cluster parks only while fast-switch
requests own it, with no request pending, no failure, its last request
accepted and its verify timer idle.

Every request of cpufreq supersedes parking, under the same lock. A fast
switch becomes the pending request, which the restore writes at once,
or, while the park request runs, once it has finished; a fast switch
that restores the cluster itself reports the state the restore wrote,
which a limit newer than its own choice may have lowered; a new policy
or thermal limit becomes the pending request, which the restore writes
when a CPU wakes. Until it is written, such a request is not accepted,
as for a request that finds the cluster busy: policy->cur follows it,
but the time in state and the scheduler's frequency scale only once it
is written, and a failure before then drops it. A waited-for request
restores the cluster before its own, and stops parking until the next
fast switch. Suspend and resume go through waited-for requests, so a
cluster does not park from suspend until the governor runs again.

Last CPU in and first CPU out are found without a new idle state. The
deepest idle state calls hooks around its WFI (previous commit), and the
cluster counts the CPUs idling there atomically. Only the CPU that makes
the count full takes the lock to park. It marks the cluster parked and
reads the count again before it writes the park request; a waking CPU
lowers the count, then reads the mark; both with full barriers. So
either the park is called off, or every CPU that wakes after it sees the
mark, and takes the lock to restore the cluster before it runs anything.
The same word counts the wakeups, and the park is called off unless it
is unchanged since the last CPU counted itself in: the timers that CPU
read are then those of the idle the count describes, not of an earlier
one of a CPU that has woken and gone idle again meanwhile. An offline
CPU is never counted, so a cluster with an offline CPU does not park,
and a CPU coming online never finds its cluster parked.

Each park and restore command is read back before the lock is dropped.
The read completes only once the write has reached the register, so the
next CPU to take the lock reads the command register after it.

Latency: nothing in the idle path waits for the cluster. A waking CPU
takes the cluster's lock only if the cluster is parked, and no holder of
that lock waits for the cluster, so it waits at most for another CPU's
short critical section. The restore is a posted write, and the waking
CPU runs at the slowest state until the transition is done. A CPU that
wakes while the park request is still running does not wait for it: the
restore is owed to the verify timer, which writes it once the park
request has finished, unless a fast switch writes first. A cluster parks
only with its verify timer idle, so the timer of a parked cluster runs
only for an owed restore. A CPU takes part only if its CPU latency limit
allows a transition (policy->cpuinfo.transition_latency), and the
cluster parks only if, as each of its CPUs went idle, none had its next
timer event due within a millisecond of the park: a policy threshold
against the two transitions a park costs. The next timer event is read
as the generic PM domain's CPU governor does. The idle states and their
numbers stay as they are, so the governors and the s2idle selector pick
the same state as before, and s2idle does not park.

Failures: if the cluster did not take the park request, parking stops
for the cluster, and the restore is written once all the same. A park
request still busy a transition timeout after it was written is a
transition failure like any other: requests stop, and nothing more is
written, so the cluster stays where the park request leaves it, its
slowest state if it completes late, as after any timed-out transition. A
waited-for request waits for the park request to finish, for at most
that timeout, without holding the lock. The verify timer reports either
failure, after it drops the lock; nothing prints from the idle path.
There is no other correction; policy->cur changes only to drop a request
the failure left unwritten.

Lifetime: ->init() hands a cluster to the idle hooks through per-CPU
pointers. ->exit(), which the core also calls with the cluster's CPUs
online when a policy fails to come up, clears them, then waits with
apple_cpuidle_park_sync() for the cluster's own online CPUs to leave the
hooks (previous commit) before it frees the cluster. A CPU between them
keeps the cluster its enter hook returned until then. The wait ends once
those CPUs have taken an interrupt, which wakes them from WFI; CPUs of
other clusters, between their own hooks, do not hold it up. The core
holds the CPU hotplug lock around ->exit(), and an offline CPU is never
between the hooks.

Parking is on for T6050 only. It needs a SoC that verifies its requests
lazily: the command register reads back the state the cluster took, so
a refused park is seen, and every request goes through the lock the
idle path takes. T8140 verifies lazily as well, but parking stays off
there until it has been checked against T8140's thermal control on the
hardware; the per-SoC flag lets it be turned on later. The other SoCs
either do not read requests back, or wait for each one, which the idle
path cannot.

A debugfs file, apple-cpufreq/park, gives a line for each cluster that
parks: its CPUs, the park and restore commands written, the failures,
and whether it may park.

The KUnit cases for parking work on a cluster of their own, which the
idle hooks never see, and need two CPUs: run the suite with
--qemu_args='-smp 2', as the kunitconfig says, or they skip. Two of
them run a thread on CPU 1: a CPU waking while a waited-for request
waits for a park request, and a CPU of another cluster between the
hooks while a cluster is taken back.

Based on the parking design in Ryan Murray's draft
(aurora-silicon#1988).

Signed-off-by: Erik Bourget <erik@ebourget.net>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants