From 544c49819597c0f456f947a1f7058a35e2b30da5 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Thu, 19 Nov 2026 23:15:35 +1100 Subject: [PATCH 1/2] cpufreq: apple-soc: verify every SoC's transitions lazily on fast switch Lazy verification was enabled by a separate lazy_verify flag set only on T8140. The other SoCs whose command register reads back the accepted state (T8142, T8152, T6050) still waited about a transition per request on every frequency change, and so kept schedutil off the fast-switch path: every change woke the governor kthread, which then busy-waited on the register. Nothing in the lazy scheme is specific to T8140. It needs what verify_transition already describes: a command register whose PS1 field reads back the accepted state and a BUSY bit. It uses the per-SoC transition timeout for its deadline, so T8152 and T6050 keep their 2 ms bound, and it still stops all requests when the accepted state is not the one last written, so an unexpected owner is caught as before, one request later. Waited-for requests (initial check, static governors) are unchanged; suspend first settles a pending request, as on T8140. Drop lazy_verify and key the lazy path on verify_transition. Every policy can now switch from the scheduler, and a lowered fast die-temperature limit is applied to the cluster at once on all of them, as on T8140. The lazy scheme itself is Diljit Singh's. Fixes: b11a5e9e8cd8 ("cpufreq: apple: verify T8140 transitions on the next request") Signed-off-by: Ryan Murray --- drivers/cpufreq/apple-soc-cpufreq.c | 52 +++++++++++++++-------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/drivers/cpufreq/apple-soc-cpufreq.c b/drivers/cpufreq/apple-soc-cpufreq.c index 09233d4b4208d..9f26e2e38d01e 100644 --- a/drivers/cpufreq/apple-soc-cpufreq.c +++ b/drivers/cpufreq/apple-soc-cpufreq.c @@ -202,8 +202,13 @@ static int apple_fast_die_hp_state; struct apple_soc_cpufreq_info { bool has_ps2; + /* + * The command register reads back the accepted state, and each + * request is checked against it. Requests from the scheduler's + * fast-switch path are checked by a timer instead of waited for, + * see "Lazy verification". + */ bool verify_transition; - bool lazy_verify; /* Fail closed when the DT describes no fast die-temperature limit. */ bool needs_thermal_policy; bool needs_thermal_owner; @@ -289,15 +294,11 @@ static const struct apple_soc_cpufreq_info soc_t8112_info = { /* * T8140 uses 5-bit PS1 state IDs starting at 1. The command register reads * back the last accepted request, not a measured frequency; each request is - * verified. Requests from the scheduler's fast-switch path are verified by a - * timer rather than waited for, so schedutil can switch from the scheduler, - * see apple_soc_cpufreq_lazy_request(); requests through ->target_index - * (suspend, the static governors) are still waited for. Voltage, PLL and - * firmware throttling configuration are left as the bootloader set them. + * verified. Voltage, PLL and firmware throttling configuration are left as + * the bootloader set them. */ static const struct apple_soc_cpufreq_info soc_t8140_info = { .verify_transition = true, - .lazy_verify = true, .needs_thermal_policy = true, .min_pstate = 1, .max_pstate = 31, @@ -525,7 +526,7 @@ static int apple_soc_cpufreq_request(struct cpufreq_policy *policy, } /* - * Lazy verification (lazy_verify SoCs) + * Lazy verification (verify_transition SoCs) * * A transition takes about 50 us. The scheduler's fast-switch path must not * wait for one, nor drop a request: schedutil records the frequency it asked @@ -734,9 +735,9 @@ static unsigned int apple_soc_cpufreq_lazy_request(struct cpufreq_policy *policy /* * Applies a new fast die-temperature limit, whose highest P-state is @cap at - * @freq, to a lazy_verify cluster at once. schedutil picks up a lower limit - * only at its next frequency update, which a nohz_full CPU running with its - * tick stopped may not have for a long time. The cluster is moved to the + * @freq, to a verify_transition cluster at once. schedutil picks up a lower + * limit only at its next frequency update, which a nohz_full CPU running with + * its tick stopped may not have for a long time. The cluster is moved to the * P-state the scheduler last asked for, at most @cap, and later requests are * clamped to @cap. That also restores the request when the limit rises * again before schedutil has seen it fall: the QoS request is then left @@ -808,7 +809,7 @@ static int apple_soc_cpufreq_set_target(struct cpufreq_policy *policy, struct apple_cpu_priv *priv = policy->driver_data; /* A waited-for request replaces a pending one, see "Lazy verification". */ - if (priv->info->lazy_verify) + if (priv->info->verify_transition) apple_soc_cpufreq_drain(priv); return apple_soc_cpufreq_request(policy, index); @@ -827,13 +828,13 @@ static unsigned int apple_soc_cpufreq_pstate_freq(struct cpufreq_policy *policy, } /* - * On a lazy_verify SoC policy->cur is the last fast-switch request, which the - * cluster may not hold: it may still be pending, be lost to a failure that - * stopped requests, or have been moved since by a fast die-temperature limit, - * see apple_soc_cpufreq_lazy_cap(). The core skips a suspend frequency equal - * to policy->cur, so settle the cluster first, writing and verifying the - * pending request or reporting the failure, and record the state it settled - * in as policy->cur before the generic suspend. + * On a verify_transition SoC policy->cur is the last fast-switch request, + * which the cluster may not hold: it may still be pending, be lost to a + * failure that stopped requests, or have been moved since by a fast + * die-temperature limit, see apple_soc_cpufreq_lazy_cap(). The core skips a + * suspend frequency equal to policy->cur, so settle the cluster first, + * writing and verifying the pending request or reporting the failure, and + * record the state it settled in as policy->cur before the generic suspend. */ static int apple_soc_cpufreq_suspend(struct cpufreq_policy *policy) { @@ -852,7 +853,7 @@ static int apple_soc_cpufreq_suspend(struct cpufreq_policy *policy) spin_unlock_bh(&fd->lock); } - if (priv->info->lazy_verify) { + if (priv->info->verify_transition) { int pstate = apple_soc_cpufreq_drain(priv); unsigned int freq; int ret; @@ -913,7 +914,7 @@ static unsigned int apple_soc_cpufreq_fast_switch(struct cpufreq_policy *policy, if (idx < 0) return 0; - if (priv->info->lazy_verify) + if (priv->info->verify_transition) return apple_soc_cpufreq_lazy_request(policy, idx); if (apple_soc_cpufreq_request(policy, idx)) return 0; @@ -1010,8 +1011,8 @@ static void apple_fast_die_arm(struct apple_fast_die *fd) } /* - * Applies @limit to a lazy_verify cluster at once, as its fastest state at or - * below the limit, see apple_soc_cpufreq_lazy_cap(). + * Applies @limit to a verify_transition cluster at once, as its fastest state + * at or below the limit, see apple_soc_cpufreq_lazy_cap(). */ static void apple_fast_die_cap(struct apple_fast_die *fd, unsigned int limit) { @@ -1021,7 +1022,7 @@ static void apple_fast_die_cap(struct apple_fast_die *fd, unsigned int limit) unsigned int cap = 0, freq = 0; int moved; - if (!priv->info->lazy_verify) + if (!priv->info->verify_transition) return; cpufreq_for_each_valid_entry(pos, policy->freq_table) { @@ -1537,7 +1538,8 @@ static int apple_soc_cpufreq_init(struct cpufreq_policy *policy) policy->cpuinfo.transition_latency = transition_latency; policy->dvfs_possible_from_any_cpu = true; - policy->fast_switch_possible = !info->verify_transition || info->lazy_verify; + /* Verified transitions are checked lazily on the fast-switch path. */ + policy->fast_switch_possible = true; policy->suspend_freq = freq_table[0].frequency; return 0; From c1b6d1c0c24a7e137da1d4af32f3ad8665eab13f Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 27 Nov 2026 16:31:29 +1100 Subject: [PATCH 2/2] cpufreq: apple-soc: time the busy-transition retries from the SoC's own latency Review follow-up. With every verify_transition SoC on the lazy path, a request that finds the cluster busy was retried at write_time + 50 us, T8140's transition time, and then every 4 us while BUSY stayed set. On a SoC whose transitions take longer (T8152 and T6050 allow 2 ms), that is hundreds of hard-IRQ timer runs per transition. Take the first retry point from the SoC: the OPP table's clock-latency-ns when the tree has one, otherwise an eighth of the SoC's transaction timeout, which is 50 us for the default 400 us and keeps T8140 where it was. While the transition stays busy, the re-check interval starts at 4 us and doubles up to that point, so a 2 ms transition costs about eight timer runs. Signed-off-by: Ryan Murray --- drivers/cpufreq/apple-soc-cpufreq.c | 43 +++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/drivers/cpufreq/apple-soc-cpufreq.c b/drivers/cpufreq/apple-soc-cpufreq.c index 9f26e2e38d01e..419a8734c7360 100644 --- a/drivers/cpufreq/apple-soc-cpufreq.c +++ b/drivers/cpufreq/apple-soc-cpufreq.c @@ -81,7 +81,13 @@ */ #define APPLE_DVFS_VERIFY_LAZY_NS (1 * NSEC_PER_MSEC) #define APPLE_DVFS_VERIFY_SLACK_NS (500 * NSEC_PER_USEC) -#define APPLE_DVFS_TRANSITION_NS (50 * NSEC_PER_USEC) +/* + * Without an OPP clock-latency-ns, a transition is taken to finish within an + * eighth of the SoC's transaction timeout: 50 us of the default 400 us, as + * measured on T8140. + */ +#define APPLE_DVFS_TRANSITION_DIV 8 +/* First re-check of a transition still busy; doubles up to the above. */ #define APPLE_DVFS_RETRY_NS (4 * NSEC_PER_USEC) /* @@ -246,6 +252,12 @@ struct apple_cpu_priv { /* When the last request was written, and when it times out. */ ktime_t write_time; ktime_t verify_deadline; + /* + * When a transition should have finished after its write, and the + * current re-check interval for one still busy, see "Lazy verification". + */ + u64 transition_ns; + u64 retry_ns; /* P-state requested while a transition was busy, or -1. */ int pending_pstate; /* @@ -528,9 +540,10 @@ static int apple_soc_cpufreq_request(struct cpufreq_policy *policy, /* * Lazy verification (verify_transition SoCs) * - * A transition takes about 50 us. The scheduler's fast-switch path must not - * wait for one, nor drop a request: schedutil records the frequency it asked - * for and does not ask again for the same one. + * A transition takes tens of microseconds (about 50 us on T8140), longer on + * some SoCs. The scheduler's fast-switch path must not wait for one, nor drop + * a request: schedutil records the frequency it asked for and does not ask + * again for the same one. * * A request finding the cluster idle requires the accepted state to be the * one last written, so verifying the previous request, and is written at @@ -541,7 +554,12 @@ static int apple_soc_cpufreq_request(struct cpufreq_policy *policy, * * A request finding the cluster busy is kept as pending, replacing an older * pending one. The timer writes it as soon as the transition has finished, - * after the same check, and then verifies it as above. + * after the same check, and then verifies it as above. It first looks when + * the transition should have finished, transition_ns after the write: the + * OPP table's clock-latency-ns, or an eighth of the SoC's transaction + * timeout. While the transition is still busy it looks again after a delay + * that starts at APPLE_DVFS_RETRY_NS and doubles up to transition_ns, so a + * slow transition costs a few timer runs, not one every few microseconds. * * A failure stops all further requests, as for a request that is waited for, * and the timer reports it. @@ -597,6 +615,7 @@ static bool apple_soc_cpufreq_lazy_write(struct apple_cpu_priv *priv, u64 reg, priv->write_time = ktime_get(); priv->verify_deadline = ktime_add_us(priv->write_time, apple_soc_cpufreq_timeout(priv->info)); + priv->retry_ns = APPLE_DVFS_RETRY_NS; return true; } @@ -631,9 +650,12 @@ static enum hrtimer_restart apple_soc_cpufreq_verify(struct hrtimer *timer) if (ktime_after(now, priv->verify_deadline)) apple_soc_cpufreq_fail(priv, reg); - else if (priv->pending_pstate >= 0) - hrtimer_start(timer, ns_to_ktime(APPLE_DVFS_RETRY_NS), + else if (priv->pending_pstate >= 0) { + hrtimer_start(timer, ns_to_ktime(priv->retry_ns), HRTIMER_MODE_REL_HARD); + priv->retry_ns = min(priv->retry_ns * 2, + priv->transition_ns); + } else hrtimer_start(timer, priv->verify_deadline, HRTIMER_MODE_ABS_HARD); @@ -675,7 +697,7 @@ static int apple_soc_cpufreq_lazy_set(struct apple_cpu_priv *priv, if (reg & APPLE_DVFS_CMD_BUSY) { /* Retry about when the transition should have finished. */ ktime_t now = ktime_get(); - ktime_t at = ktime_add_ns(priv->write_time, APPLE_DVFS_TRANSITION_NS); + ktime_t at = ktime_add_ns(priv->write_time, priv->transition_ns); if (ktime_before(at, ktime_add_ns(now, APPLE_DVFS_RETRY_NS))) at = ktime_add_ns(now, APPLE_DVFS_RETRY_NS); @@ -1537,6 +1559,11 @@ static int apple_soc_cpufreq_init(struct cpufreq_policy *policy) } policy->cpuinfo.transition_latency = transition_latency; + /* Measured OPP latency if there is one, see "Lazy verification". */ + priv->transition_ns = dev_pm_opp_get_max_transition_latency(cpu_dev) ?: + div_u64((u64)apple_soc_cpufreq_timeout(info) * NSEC_PER_USEC, + APPLE_DVFS_TRANSITION_DIV); + priv->retry_ns = APPLE_DVFS_RETRY_NS; policy->dvfs_possible_from_any_cpu = true; /* Verified transitions are checked lazily on the fast-switch path. */ policy->fast_switch_possible = true;