From 9163bf17bea2d9618a12541d67d6785e372d26ac Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sat, 28 Nov 2026 16:17:47 +1100 Subject: [PATCH 1/2] soc: apple: clpc: only step down busy CPU clusters on low-power PHPC In the low-power platform profile, apple_clpc_profile_limit() reads SMC PHPC (package power, which includes the GPU and display as well as the CPUs) and steps one CPU cluster down on every new reading above low_power_mw. When a GPU or display workload holds PHPC above low_power_mw while the CPUs are idle or lightly loaded, stepping down CPU ceilings saves almost no power, so PHPC stays above target and every CPU cluster walks down to its minimum OPP (300 MHz). Neither SMC nor apple-pmc exposes a per-cluster CPU-only power meter on these platforms (CPU and GPU share PHPC), so gate downward steps on the cluster's scheduler non-idle utilisation (cl->busy >= APPLE_CLPC_BUSY_PCT, already sampled by apple_clpc_sample_cluster()): - Extract apple_clpc_sample_busy() and run it for standalone (!clpc->has_pmc) low-power package-power instances as well, so cl->busy is populated uniformly without SoC gating. - Extract apple_clpc_pick_profile_step() and skip clusters with cl->busy < APPLE_CLPC_BUSY_PCT when stepping down (step < 0), while still stepping every capped cluster back up when PHPC drops below 90% of low_power_mw (step > 0). - Add KUnit test apple_clpc_profile_step_busy_test covering idle clusters under over-budget PHPC, single busy cluster selection, multi-cluster busy ordering, idle recovery on step-up, and step == 0. Fixes: https://github.com/aurora-silicon/linux/issues/3450 Signed-off-by: Ryan Murray --- drivers/soc/apple/clpc.c | 139 +++++++++++++++++++++++++++++++++------ 1 file changed, 118 insertions(+), 21 deletions(-) diff --git a/drivers/soc/apple/clpc.c b/drivers/soc/apple/clpc.c index d05149ff2ff57..534bc2206fa2c 100644 --- a/drivers/soc/apple/clpc.c +++ b/drivers/soc/apple/clpc.c @@ -646,11 +646,10 @@ static unsigned int apple_clpc_cpu_state(const struct apple_clpc *clpc, return clpc->hw->map[rail][q]; } -static void apple_clpc_sample_cluster(struct apple_clpc *clpc, - struct apple_clpc_cluster *cl) +static void apple_clpc_sample_busy(struct apple_clpc *clpc, + struct apple_clpc_cluster *cl) { - struct cpufreq_policy *policy; - unsigned int cpu, busy = 0, q, rail; + unsigned int cpu, busy = 0; for_each_cpu(cpu, cl->cpus) { u64 wall, idle, dw, di; @@ -677,6 +676,16 @@ static void apple_clpc_sample_cluster(struct apple_clpc *clpc, div64_u64((dw - di) * 100, dw)); } cl->busy = busy; +} + +static void apple_clpc_sample_cluster(struct apple_clpc *clpc, + struct apple_clpc_cluster *cl) +{ + struct cpufreq_policy *policy; + unsigned int busy, q, rail; + + apple_clpc_sample_busy(clpc, cl); + busy = cl->busy; cl->level = 0; policy = cpufreq_cpu_get(cl->policy_cpu); @@ -1162,19 +1171,59 @@ static bool apple_clpc_steps_first(unsigned int a_khz, unsigned int a_max, return up ? a_max < b_max : a_max > b_max; } +/* + * Pick which cluster takes @step (-1 down, +1 up) for the low-power package + * power loop, and return its next frequency in @out_khz. Stepping down skips + * clusters below APPLE_CLPC_BUSY_PCT so a GPU or display load holding PHPC + * above @low_power_mw does not walk idle CPUs down to their minimum OPP; + * stepping back up restores every capped cluster once PHPC drops. + */ +static int apple_clpc_pick_profile_step(struct apple_clpc *clpc, + struct cpufreq_policy *const *policy, + int step, unsigned int *out_khz) +{ + unsigned int i, khz, pick_khz = 0; + int pick = -1; + + if (!step) + return -1; + for (i = 0; i < clpc->nr_clusters; i++) { + struct apple_clpc_cluster *cl = &clpc->clusters[i]; + + if (!policy[i]) + continue; + if (step < 0 && cl->busy < APPLE_CLPC_BUSY_PCT) + continue; + khz = apple_clpc_step_khz(policy[i], cl->max_khz, step > 0); + if (khz == cl->max_khz || khz > cl->top_khz) + continue; + if (pick < 0 || + apple_clpc_steps_first(cl->max_khz, policy[i]->cpuinfo.max_freq, + clpc->clusters[pick].max_khz, + policy[pick]->cpuinfo.max_freq, step > 0)) { + pick = i; + pick_khz = khz; + } + } + if (pick >= 0) + *out_khz = pick_khz; + return pick; +} + /* * The platform profile's ceilings. In the low-power profile every cluster * stays at or below apple_clpc_low_power_top_khz(). When @low_power_mw is set, - * each new package power reading above it takes one table step off one - * cluster, and each one below 90% of it puts one back, in the order of - * apple_clpc_steps_first(). Elsewhere there is no ceiling. + * each new package power reading above it takes one table step off one busy + * cluster (busy >= APPLE_CLPC_BUSY_PCT), and each one below 90% of it puts one + * back on any capped cluster, in the order of apple_clpc_steps_first(). + * Elsewhere there is no ceiling. */ static void apple_clpc_profile_limit(struct apple_clpc *clpc) { bool low = READ_ONCE(clpc->low_power); struct cpufreq_policy *policy[APPLE_CLPC_MAX_CLUSTERS] = {}; - unsigned int i, khz, pick_khz = 0; - int mw, step = 0, pick = -1; + unsigned int i, pick_khz = 0; + int mw, step = 0, pick; if (!clpc->low_power_khz || time_before(jiffies, clpc->power_next)) return; @@ -1195,6 +1244,8 @@ static void apple_clpc_profile_limit(struct apple_clpc *clpc) for (i = 0; i < clpc->nr_clusters; i++) { struct apple_clpc_cluster *cl = &clpc->clusters[i]; + if (!clpc->has_pmc && low && clpc->low_power_mw) + apple_clpc_sample_busy(clpc, cl); policy[i] = cpufreq_cpu_get(cl->policy_cpu); if (!policy[i]) continue; @@ -1209,19 +1260,8 @@ static void apple_clpc_profile_limit(struct apple_clpc *clpc) else if (!cl->max_khz || !clpc->low_power_mw || cl->max_khz > cl->top_khz) cl->max_khz = cl->top_khz; - if (!step) - continue; - khz = apple_clpc_step_khz(policy[i], cl->max_khz, step > 0); - if (khz == cl->max_khz || khz > cl->top_khz) - continue; - if (pick < 0 || - apple_clpc_steps_first(cl->max_khz, policy[i]->cpuinfo.max_freq, - clpc->clusters[pick].max_khz, - policy[pick]->cpuinfo.max_freq, step > 0)) { - pick = i; - pick_khz = khz; - } } + pick = apple_clpc_pick_profile_step(clpc, policy, step, &pick_khz); if (pick >= 0) clpc->clusters[pick].max_khz = pick_khz; for (i = 0; i < clpc->nr_clusters; i++) { @@ -3063,11 +3103,68 @@ static void apple_clpc_floor_test(struct kunit *test) KUNIT_EXPECT_EQ(test, apple_clpc_floor(clpc, state), 0xf00u); } +static void apple_clpc_profile_step_busy_test(struct kunit *test) +{ + struct apple_clpc *clpc = kunit_kzalloc(test, sizeof(*clpc), GFP_KERNEL); + struct cpufreq_policy *slow = apple_clpc_test_policy(test); + struct cpufreq_policy *fast = apple_clpc_test_policy(test); + struct cpufreq_policy *policies[APPLE_CLPC_MAX_CLUSTERS] = { slow, fast }; + unsigned int next_khz = 0; + + KUNIT_ASSERT_NOT_NULL(test, clpc); + slow->cpuinfo.max_freq = 1500000; + fast->cpuinfo.max_freq = 2000000; + clpc->nr_clusters = 2; + clpc->clusters[0].top_khz = 1500000; + clpc->clusters[0].max_khz = 1500000; + clpc->clusters[1].top_khz = 2000000; + clpc->clusters[1].max_khz = 2000000; + + /* + * GPU/display holding PHPC above low_power_mw while all CPU clusters + * are below APPLE_CLPC_BUSY_PCT must not step any cluster down. + */ + clpc->clusters[0].busy = 10; + clpc->clusters[1].busy = APPLE_CLPC_BUSY_PCT - 1; + KUNIT_EXPECT_EQ(test, apple_clpc_pick_profile_step(clpc, policies, -1, &next_khz), -1); + + /* + * Only the busy cluster is eligible to step down, even when an idle + * cluster sits at a higher ceiling. + */ + clpc->clusters[0].busy = APPLE_CLPC_BUSY_PCT; + clpc->clusters[1].busy = 20; + KUNIT_EXPECT_EQ(test, apple_clpc_pick_profile_step(clpc, policies, -1, &next_khz), 0); + KUNIT_EXPECT_EQ(test, next_khz, 1200000u); + + /* When both clusters are busy, apple_clpc_steps_first() ordering applies. */ + clpc->clusters[0].busy = 80; + clpc->clusters[1].busy = 60; + KUNIT_EXPECT_EQ(test, apple_clpc_pick_profile_step(clpc, policies, -1, &next_khz), 1); + KUNIT_EXPECT_EQ(test, next_khz, 1500000u); + + /* + * Stepping back up when PHPC falls below 90% restores idle clusters too, + * lowest ceiling first (slower cluster on a tie). + */ + clpc->clusters[0].busy = 0; + clpc->clusters[1].busy = 0; + clpc->clusters[0].max_khz = 1200000; + clpc->clusters[1].max_khz = 1200000; + KUNIT_EXPECT_EQ(test, apple_clpc_pick_profile_step(clpc, policies, 1, &next_khz), 0); + KUNIT_EXPECT_EQ(test, next_khz, 1500000u); + + /* Zero step does nothing regardless of busy state. */ + clpc->clusters[0].busy = 100; + KUNIT_EXPECT_EQ(test, apple_clpc_pick_profile_step(clpc, policies, 0, &next_khz), -1); +} + static struct kunit_case apple_clpc_policy_cases[] = { KUNIT_CASE(apple_clpc_step_khz_test), KUNIT_CASE(apple_clpc_steps_first_test), KUNIT_CASE(apple_clpc_vote_test), KUNIT_CASE(apple_clpc_floor_test), + KUNIT_CASE(apple_clpc_profile_step_busy_test), {} }; static struct kunit_suite apple_clpc_policy_suite = { From 3ec9d7e086041dbd5c179979fd6c8cc7c296fc26 Mon Sep 17 00:00:00 2001 From: betizzel Date: Sun, 29 Nov 2026 19:05:25 -0600 Subject: [PATCH 2/2] soc: apple: clpc: gate low-power PHPC step-downs on PHPC-interval busy and floor On PMC-backed SoCs (such as T6050), cl->busy reflects the busiest CPU in the cluster over the 4 ms PMC sampling window. Sampling that on each ~1 s PHPC reading leaks step-downs during GPU/compositor workloads because short redraw bursts still push one CPU over APPLE_CLPC_BUSY_PCT in a 4 ms slice. In addition, the !clpc->has_pmc path read clpc->wall and clpc->idle even though probe only allocates those arrays when clpc->has_pmc is true. Track a separate per-cluster busy share over the PHPC update interval and prevent the low-power package power loop from stepping onto the minimum OPP: - Generalize apple_clpc_sample_busy() into apple_clpc_busy_since() taking explicit wall/idle baseline arrays. - Allocate per-CPU power_wall/power_idle baselines in probe whenever low_power_khz && low_power_mw (independent of has_pmc), reset them when leaving low-power mode and on resume, and update cl->power_busy on each fresh PHPC reading. - Gate apple_clpc_pick_profile_step() downward steps on cl->power_busy instead of cl->busy, and skip stepping a cluster onto policy->cpuinfo.min_freq (its lowest table row). - Update apple_clpc_profile_step_busy_test to cover cl->power_busy vs cl->busy independence and the lowest-row floor. Signed-off-by: betizzel --- drivers/soc/apple/clpc.c | 115 ++++++++++++++++++++++++++++----------- 1 file changed, 84 insertions(+), 31 deletions(-) diff --git a/drivers/soc/apple/clpc.c b/drivers/soc/apple/clpc.c index 534bc2206fa2c..e40bd3ca38daa 100644 --- a/drivers/soc/apple/clpc.c +++ b/drivers/soc/apple/clpc.c @@ -164,6 +164,8 @@ struct apple_clpc_cluster { unsigned int policy_cpu; cpumask_var_t cpus; unsigned int busy; + /* Busy share since the last package power reading the profile acted on */ + unsigned int power_busy; unsigned int streak; unsigned int level; unsigned int request[APPLE_CLPC_RAILS]; @@ -302,6 +304,9 @@ struct apple_clpc { unsigned long power_next; unsigned long power_reads, power_updates; int power_mw; + /* Per-CPU baselines for power_busy, taken at each package power reading */ + u64 *power_idle; + u64 *power_wall; }; /* Protect publication/removal of provider drvdata, never a submission path. */ @@ -646,8 +651,12 @@ static unsigned int apple_clpc_cpu_state(const struct apple_clpc *clpc, return clpc->hw->map[rail][q]; } -static void apple_clpc_sample_busy(struct apple_clpc *clpc, - struct apple_clpc_cluster *cl) +/* + * The busiest CPU of @cl since @base_wall/@base_idle, in percent, and move + * the baselines to now. + */ +static unsigned int apple_clpc_busy_since(const struct apple_clpc_cluster *cl, + u64 *base_wall, u64 *base_idle) { unsigned int cpu, busy = 0; @@ -661,21 +670,21 @@ static void apple_clpc_sample_busy(struct apple_clpc *clpc, * baseline once it is back. */ if (!cpu_online(cpu)) { - clpc->wall[cpu] = 0; + base_wall[cpu] = 0; continue; } idle = get_cpu_idle_time(cpu, &wall, 0); - dw = wall - clpc->wall[cpu]; - di = idle - clpc->idle[cpu]; - valid = clpc->wall[cpu]; - clpc->wall[cpu] = wall; - clpc->idle[cpu] = idle; + dw = wall - base_wall[cpu]; + di = idle - base_idle[cpu]; + valid = base_wall[cpu]; + base_wall[cpu] = wall; + base_idle[cpu] = idle; if (valid && dw && di <= dw) busy = max_t(unsigned int, busy, div64_u64((dw - di) * 100, dw)); } - cl->busy = busy; + return busy; } static void apple_clpc_sample_cluster(struct apple_clpc *clpc, @@ -684,8 +693,8 @@ static void apple_clpc_sample_cluster(struct apple_clpc *clpc, struct cpufreq_policy *policy; unsigned int busy, q, rail; - apple_clpc_sample_busy(clpc, cl); - busy = cl->busy; + busy = apple_clpc_busy_since(cl, clpc->wall, clpc->idle); + cl->busy = busy; cl->level = 0; policy = cpufreq_cpu_get(cl->policy_cpu); @@ -1174,9 +1183,11 @@ static bool apple_clpc_steps_first(unsigned int a_khz, unsigned int a_max, /* * Pick which cluster takes @step (-1 down, +1 up) for the low-power package * power loop, and return its next frequency in @out_khz. Stepping down skips - * clusters below APPLE_CLPC_BUSY_PCT so a GPU or display load holding PHPC - * above @low_power_mw does not walk idle CPUs down to their minimum OPP; - * stepping back up restores every capped cluster once PHPC drops. + * clusters whose busy share over the last PHPC interval (power_busy) is below + * APPLE_CLPC_BUSY_PCT so a GPU or display load holding PHPC above + * @low_power_mw does not walk idle CPUs down, and never steps a cluster onto + * its lowest table row (cpuinfo.min_freq); stepping back up restores every + * capped cluster once PHPC drops. */ static int apple_clpc_pick_profile_step(struct apple_clpc *clpc, struct cpufreq_policy *const *policy, @@ -1192,11 +1203,13 @@ static int apple_clpc_pick_profile_step(struct apple_clpc *clpc, if (!policy[i]) continue; - if (step < 0 && cl->busy < APPLE_CLPC_BUSY_PCT) + if (step < 0 && cl->power_busy < APPLE_CLPC_BUSY_PCT) continue; khz = apple_clpc_step_khz(policy[i], cl->max_khz, step > 0); if (khz == cl->max_khz || khz > cl->top_khz) continue; + if (step < 0 && khz <= policy[i]->cpuinfo.min_freq) + continue; if (pick < 0 || apple_clpc_steps_first(cl->max_khz, policy[i]->cpuinfo.max_freq, clpc->clusters[pick].max_khz, @@ -1214,16 +1227,18 @@ static int apple_clpc_pick_profile_step(struct apple_clpc *clpc, * The platform profile's ceilings. In the low-power profile every cluster * stays at or below apple_clpc_low_power_top_khz(). When @low_power_mw is set, * each new package power reading above it takes one table step off one busy - * cluster (busy >= APPLE_CLPC_BUSY_PCT), and each one below 90% of it puts one - * back on any capped cluster, in the order of apple_clpc_steps_first(). - * Elsewhere there is no ceiling. + * cluster (power_busy >= APPLE_CLPC_BUSY_PCT, never stepping onto the + * cluster's lowest table row), and each one below 90% of it puts one back on + * any capped cluster, in the order of apple_clpc_steps_first(). Elsewhere + * there is no ceiling. */ static void apple_clpc_profile_limit(struct apple_clpc *clpc) { bool low = READ_ONCE(clpc->low_power); struct cpufreq_policy *policy[APPLE_CLPC_MAX_CLUSTERS] = {}; - unsigned int i, pick_khz = 0; + unsigned int i, cpu, pick_khz = 0; int mw, step = 0, pick; + bool fresh = false; if (!clpc->low_power_khz || time_before(jiffies, clpc->power_next)) return; @@ -1234,6 +1249,7 @@ static void apple_clpc_profile_limit(struct apple_clpc *clpc) /* The SMC updates the reading less often: act on a new one only. */ if (mw >= 0 && mw != clpc->power_mw) { clpc->power_updates++; + fresh = true; if (mw > (int)clpc->low_power_mw) step = -1; else if (mw < (int)clpc->low_power_mw * 9 / 10) @@ -1244,8 +1260,15 @@ static void apple_clpc_profile_limit(struct apple_clpc *clpc) for (i = 0; i < clpc->nr_clusters; i++) { struct apple_clpc_cluster *cl = &clpc->clusters[i]; - if (!clpc->has_pmc && low && clpc->low_power_mw) - apple_clpc_sample_busy(clpc, cl); + if (fresh && clpc->power_wall) { + cl->power_busy = apple_clpc_busy_since(cl, clpc->power_wall, + clpc->power_idle); + } else if (!low && clpc->power_wall) { + /* The next low-power reading starts a fresh window. */ + cl->power_busy = 0; + for_each_cpu(cpu, cl->cpus) + clpc->power_wall[cpu] = 0; + } policy[i] = cpufreq_cpu_get(cl->policy_cpu); if (!policy[i]) continue; @@ -2099,6 +2122,16 @@ static int apple_clpc_probe(struct platform_device *pdev) clpc->saved_floor = apple_pmc_floor_read(clpc->pmc, clpc->agent) & clpc->mask; } + if (clpc->low_power_khz && clpc->low_power_mw) { + clpc->power_idle = devm_kcalloc(dev, nr_cpu_ids, + sizeof(*clpc->power_idle), + GFP_KERNEL); + clpc->power_wall = devm_kcalloc(dev, nr_cpu_ids, + sizeof(*clpc->power_wall), + GFP_KERNEL); + if (!clpc->power_idle || !clpc->power_wall) + return -ENOMEM; + } for (i = 0; i < APPLE_CLPC_MAX_CLUSTERS; i++) { if (!zalloc_cpumask_var(&clpc->clusters[i].cpus, GFP_KERNEL)) { @@ -2223,6 +2256,9 @@ static int apple_clpc_resume(struct device *dev) if (clpc->wall) for_each_possible_cpu(cpu) clpc->wall[cpu] = 0; + if (clpc->power_wall) + for_each_possible_cpu(cpu) + clpc->power_wall[cpu] = 0; clpc->power_next = jiffies; WRITE_ONCE(clpc->stopped, false); apple_clpc_queue(clpc, true); @@ -2989,6 +3025,7 @@ static struct cpufreq_policy *apple_clpc_test_policy(struct kunit *test) table[i].frequency = khz[i]; table[i].frequency = CPUFREQ_TABLE_END; policy->freq_table = table; + policy->cpuinfo.min_freq = 600000; policy->cpuinfo.max_freq = 2000000; return policy; } @@ -3122,40 +3159,56 @@ static void apple_clpc_profile_step_busy_test(struct kunit *test) /* * GPU/display holding PHPC above low_power_mw while all CPU clusters - * are below APPLE_CLPC_BUSY_PCT must not step any cluster down. + * are below APPLE_CLPC_BUSY_PCT over the PHPC interval must not step + * any cluster down, even if the short PMC sampler window read busy. */ - clpc->clusters[0].busy = 10; - clpc->clusters[1].busy = APPLE_CLPC_BUSY_PCT - 1; + clpc->clusters[0].busy = 100; + clpc->clusters[1].busy = 100; + clpc->clusters[0].power_busy = 10; + clpc->clusters[1].power_busy = APPLE_CLPC_BUSY_PCT - 1; KUNIT_EXPECT_EQ(test, apple_clpc_pick_profile_step(clpc, policies, -1, &next_khz), -1); /* * Only the busy cluster is eligible to step down, even when an idle * cluster sits at a higher ceiling. */ - clpc->clusters[0].busy = APPLE_CLPC_BUSY_PCT; - clpc->clusters[1].busy = 20; + clpc->clusters[0].power_busy = APPLE_CLPC_BUSY_PCT; + clpc->clusters[1].power_busy = 20; KUNIT_EXPECT_EQ(test, apple_clpc_pick_profile_step(clpc, policies, -1, &next_khz), 0); KUNIT_EXPECT_EQ(test, next_khz, 1200000u); /* When both clusters are busy, apple_clpc_steps_first() ordering applies. */ - clpc->clusters[0].busy = 80; - clpc->clusters[1].busy = 60; + clpc->clusters[0].power_busy = 80; + clpc->clusters[1].power_busy = 60; KUNIT_EXPECT_EQ(test, apple_clpc_pick_profile_step(clpc, policies, -1, &next_khz), 1); KUNIT_EXPECT_EQ(test, next_khz, 1500000u); + /* + * Never step a cluster down onto its lowest table row (cpuinfo.min_freq), + * even when busy. + */ + clpc->clusters[0].max_khz = 1200000; + clpc->clusters[1].max_khz = 1500000; + clpc->clusters[0].power_busy = 100; + clpc->clusters[1].power_busy = 100; + KUNIT_EXPECT_EQ(test, apple_clpc_pick_profile_step(clpc, policies, -1, &next_khz), 1); + KUNIT_EXPECT_EQ(test, next_khz, 1200000u); + clpc->clusters[1].max_khz = 1200000; + KUNIT_EXPECT_EQ(test, apple_clpc_pick_profile_step(clpc, policies, -1, &next_khz), -1); + /* * Stepping back up when PHPC falls below 90% restores idle clusters too, * lowest ceiling first (slower cluster on a tie). */ - clpc->clusters[0].busy = 0; - clpc->clusters[1].busy = 0; + clpc->clusters[0].power_busy = 0; + clpc->clusters[1].power_busy = 0; clpc->clusters[0].max_khz = 1200000; clpc->clusters[1].max_khz = 1200000; KUNIT_EXPECT_EQ(test, apple_clpc_pick_profile_step(clpc, policies, 1, &next_khz), 0); KUNIT_EXPECT_EQ(test, next_khz, 1500000u); /* Zero step does nothing regardless of busy state. */ - clpc->clusters[0].busy = 100; + clpc->clusters[0].power_busy = 100; KUNIT_EXPECT_EQ(test, apple_clpc_pick_profile_step(clpc, policies, 0, &next_khz), -1); }