diff --git a/drivers/soc/apple/clpc.c b/drivers/soc/apple/clpc.c index d05149ff2ff57..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,11 +651,14 @@ 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) +/* + * 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) { - 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; @@ -662,20 +670,30 @@ static void apple_clpc_sample_cluster(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)); } + return 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; + + busy = apple_clpc_busy_since(cl, clpc->wall, clpc->idle); cl->busy = busy; cl->level = 0; @@ -1162,19 +1180,65 @@ 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 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, + 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->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, + 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 (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, khz, pick_khz = 0; - int mw, step = 0, pick = -1; + 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; @@ -1185,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) @@ -1195,6 +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 (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; @@ -1209,19 +1283,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++) { @@ -2059,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)) { @@ -2183,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); @@ -2949,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; } @@ -3063,11 +3140,84 @@ 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 over the PHPC interval must not step + * any cluster down, even if the short PMC sampler window read busy. + */ + 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].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].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].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].power_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 = {