Repository navigation
cpufreq: apple-soc: park idle clusters at their slowest state - #1988
RyanTheTide wants to merge 2 commits into
Conversation
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>
|
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 if (dev->states_usage[i].disable ||
s->exit_latency_ns <= latency_req ||WFI-park sets 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 Leaving |
|
Early note on this draft (no full review yet):
Assisted-by: LLM (Claude:Opus 5.5) |
|
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. |
|
Reopened and kept as a draft; the comment above lists what it still needs before review. cc @niteria |
|
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
One fix we needed: parking never ran at first (0 parks and 0 skipped idle entries in 14 minutes on every policy), because - 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. |
|
#3717 is a rewrite of this on current 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. |
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>
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>
Split out of #1811: the two cpufreq commits that park an idle cluster at its slowest state.
6b72af7a8c93cpufreq: apple-soc: park an idle cluster at its slowest stateb17d649b440ccpufreq: apple-soc: park idle clusters on lazy_verify SoCs tooThey add a
WFI-parkidle state between WFI and the deep state incpuidle-apple.c, which changes the idle-state table, including the index s2idle picks, on every apple-cpuidle SoC.Status: draft.
apple-dcp›cpuidle-s2idlefails 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, andfind_deepest_state()must still land on the deep state for s2idle.