From dc8572f1c5ec58c856f83713a19f84e773b1dbee Mon Sep 17 00:00:00 2001 From: Dylan Ironside Date: Wed, 2 Dec 2026 14:29:47 -0600 Subject: [PATCH 1/8] dt-bindings: power: apple,pmc: document CLPC all-core and sustained power limits Document three optional board-level CLPC properties on the PMC performance-controller child node for non-low-power sustained multi-core regulation: - apple,all-core-frequency-max-mhz: per-cluster-type frequency ceilings when all online CPUs of a cluster are busy across the 100 ms window. - apple,slow-cluster-busy-max-mhz: frequency ceiling for the slowest cluster while a faster cluster is busy. - apple,sustained-power-mw: sustained all-core SMC package power target in milliwatts while all-core load holds a faster cluster. Update the apple,smc description to cover sustained package power regulation alongside the low-power profile and High Power mode. Signed-off-by: Dylan Ironside --- .../devicetree/bindings/power/apple,pmc.yaml | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/Documentation/devicetree/bindings/power/apple,pmc.yaml b/Documentation/devicetree/bindings/power/apple,pmc.yaml index fb0663ff2499c..4f4f2a61a8499 100644 --- a/Documentation/devicetree/bindings/power/apple,pmc.yaml +++ b/Documentation/devicetree/bindings/power/apple,pmc.yaml @@ -277,8 +277,9 @@ patternProperties: $ref: /schemas/types.yaml#/definitions/phandle description: The SMC, whose package power reading the low-power platform profile - holds the CPU clusters to, and which the performance profile tells - about High Power mode on Macs that have it. + and sustained all-core power limit hold the CPU clusters to, and + which the performance profile tells about High Power mode on Macs + that have it. apple,high-power-mode: type: boolean @@ -303,6 +304,31 @@ patternProperties: apple,power-key-id reading at it with the CPU clusters' ceilings (and, with apple,low-power-gpu-cap, the GPU's power budget). + apple,all-core-frequency-max-mhz: + minItems: 1 + maxItems: 2 + description: + Per-cluster-type CPU frequency ceilings in MHz applied outside the + low-power platform profile when all online CPUs of a cluster are + busy across the 100 ms power window. Each entry applies to any + cluster whose cpufreq frequency table contains that frequency. + + apple,slow-cluster-busy-max-mhz: + maxItems: 1 + description: + CPU frequency ceiling in MHz applied to the slowest cluster outside + the low-power platform profile while a faster cluster is busy. + + apple,sustained-power-mw: + $ref: /schemas/types.yaml#/definitions/uint32 + description: + Sustained all-core package power target in milliwatts outside the + low-power platform profile. With apple,smc, while all-core load + holds a faster cluster, the driver polls the SMC's + apple,power-key-id reading every 100 ms and steps faster clusters' + frequency ceilings to hold package power near this target, releasing + immediately when all-core load ends. + apple,power-key-id: $ref: /schemas/types.yaml#/definitions/string pattern: "^[A-Za-z0-9#!]{4}$" From 32458d8676c0f13cdf96723cb29945e319a224a5 Mon Sep 17 00:00:00 2001 From: Dylan Ironside Date: Wed, 2 Dec 2026 14:29:48 -0600 Subject: [PATCH 2/8] soc: apple: clpc: add all-core window, slow-cluster busy cap, and sustained power limit On T8132 (J604), sustained heavy multi-core workloads such as 7-Zip mtall (~915% CPU) hit two efficiency regressions against macOS on AC: 1. E-cluster 2892 MHz power cliff: brief per-core dips let the E-cluster jump to 2892 MHz while P-cores are active, paying a ~2.1x power jump over 2808 MHz for +3% clock. 2. Unbounded 3936/4464 MHz P-cluster power: macOS regulates sustained heavy all-core workloads on J604 around 15.5 W PHPC (~3600 MHz active P clock, die ~77 C), whereas uncapped Linux draws 17.3-25+ W PHPC (die up to 95-100 C), losing ~6% MIPS/W and inducing thermal droop on subsequent single-core runs. Add three optional, DT-driven controls in apple-clpc (inactive unless configured by board DT; zero runtime module parameters and zero machine name matching): - Integrated 100 ms all-core busy window (80% per-CPU busy threshold, 200 ms release hysteresis) applying apple,all-core-frequency-max-mhz per matching cluster outside the low-power profile. - Slow-cluster busy cap (apple,slow-cluster-busy-max-mhz) while a faster cluster is busy outside the low-power profile, leaving E-only workloads and Low Power mode untouched. - Sustained all-core SMC package power ceiling (apple,sustained-power-mw, using apple,smc and apple,power-key-id / PHPC) that polls SMC only while all-core load holds a faster cluster (zero SMC reads at idle or single-core load) and releases immediately once all-core load ends. Add KUnit tests in apple-clpc-policy covering the all-core window hold, sustained power stepping and floor/release behavior, all-core table matching, and slow-cluster busy capping. Signed-off-by: Dylan Ironside --- drivers/soc/apple/clpc.c | 515 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 479 insertions(+), 36 deletions(-) diff --git a/drivers/soc/apple/clpc.c b/drivers/soc/apple/clpc.c index f8d5fbbabf8a9..ed224d9ad9426 100644 --- a/drivers/soc/apple/clpc.c +++ b/drivers/soc/apple/clpc.c @@ -74,6 +74,11 @@ enum apple_clpc_rail { #define APPLE_CLPC_GPU_BACKOFF_MS 100 /* Package power readings in the low-power profile: every 100 ms */ #define APPLE_CLPC_POWER_MS 100 +/* All-core busy window threshold (%) and release hysteresis (ms) */ +#define APPLE_CLPC_ALL_CORE_BUSY_PCT 80 +#define APPLE_CLPC_ALL_CORE_HOLD_MS 200 +/* Step sustained all-core ceiling back up below this % of target */ +#define APPLE_CLPC_SUSTAINED_UP_PCT 92 /* * The GPU power cap of the low-power profile, in fractions of its power * target: each step moves the cap by 1/APPLE_CLPC_GPU_CAP_STEPS of the target, @@ -174,6 +179,11 @@ struct apple_clpc_cluster { unsigned int busy; /* Busy share since the last package power reading the profile acted on */ unsigned int power_busy; + /* Integrated 100 ms window all-core count, release hold and sustained cap */ + unsigned int win_online, win_nbusy; + unsigned long all_core_until; + bool all_core_held; + unsigned int power_khz; unsigned int streak; unsigned int level; unsigned int request[APPLE_CLPC_RAILS]; @@ -321,6 +331,13 @@ struct apple_clpc { /* Per-CPU baselines for power_busy, taken at each package power reading */ u64 *power_idle; u64 *power_wall; + /* Optional board DT all-core and sustained package power limits (0 = off) */ + unsigned int all_core_khz[2]; + unsigned int e_cap_khz; + unsigned int sustained_mw; + bool has_all_core; + u64 *win_idle; + u64 *win_wall; }; /* Protect publication/removal of provider drvdata, never a submission path. */ @@ -681,6 +698,9 @@ static void apple_clpc_find_clusters(struct apple_clpc *clpc) cpumask_set_cpu(cpu, clpc->covered); if (clpc->idle && clpc->wall) clpc->idle[cpu] = get_cpu_idle_time(cpu, &clpc->wall[cpu], 0); + if (clpc->win_idle && clpc->win_wall) + clpc->win_idle[cpu] = get_cpu_idle_time(cpu, + &clpc->win_wall[cpu], 0); } cpufreq_cpu_put(policy); } @@ -1339,27 +1359,194 @@ static int apple_clpc_pick_profile_step(struct apple_clpc *clpc, return pick; } +static bool apple_clpc_has_khz(struct cpufreq_policy *policy, unsigned int khz) +{ + struct cpufreq_frequency_table *pos; + + if (!policy->freq_table) + return false; + cpufreq_for_each_valid_entry(pos, policy->freq_table) + if (pos->frequency == khz) + return true; + return false; +} + +static bool apple_clpc_all_core_window_step(struct apple_clpc_cluster *cl, + bool all_busy, unsigned long now, + unsigned int hold_ms) +{ + if (all_busy) { + cl->all_core_until = now + msecs_to_jiffies(hold_ms); + cl->all_core_held = true; + } else if (cl->all_core_held && hold_ms && + time_before(now, cl->all_core_until)) { + cl->all_core_held = true; + } else { + cl->all_core_held = false; + } + + return cl->all_core_held; +} + +static void apple_clpc_sample_all_core_window(struct apple_clpc *clpc, + struct apple_clpc_cluster *cl, + unsigned long now, bool low) +{ + u64 max_gap_us = 2ULL * APPLE_CLPC_POWER_MS * USEC_PER_MSEC; + unsigned int cpu, online = 0; + bool valid_all = true; + bool all_busy; + + cl->win_nbusy = 0; + cl->win_online = 0; + if (!clpc->win_wall || !clpc->win_idle) { + cl->all_core_held = false; + return; + } + + for_each_cpu(cpu, cl->cpus) { + u64 wall, idle, dw, di; + bool valid; + + if (!cpu_online(cpu)) { + clpc->win_wall[cpu] = 0; + clpc->win_idle[cpu] = 0; + continue; + } + idle = get_cpu_idle_time(cpu, &wall, 0); + valid = clpc->win_wall[cpu] && wall > clpc->win_wall[cpu]; + dw = valid ? wall - clpc->win_wall[cpu] : 0; + di = valid && idle >= clpc->win_idle[cpu] ? + idle - clpc->win_idle[cpu] : dw + 1; + clpc->win_wall[cpu] = wall; + clpc->win_idle[cpu] = idle; + online++; + if (valid && dw && di <= dw && dw <= max_gap_us) { + unsigned int b = div64_u64((dw - di) * 100, dw); + + if (b >= APPLE_CLPC_ALL_CORE_BUSY_PCT) + cl->win_nbusy++; + } else { + valid_all = false; + } + } + + cl->win_online = online; + if (low) { + cl->all_core_held = false; + return; + } + + all_busy = online >= 2 && valid_all && cl->win_nbusy >= online; + apple_clpc_all_core_window_step(cl, all_busy, now, + APPLE_CLPC_ALL_CORE_HOLD_MS); +} + +/* Configured all-core ceiling for a cluster whose online CPUs are all busy, else 0. */ +static unsigned int apple_clpc_all_core_khz(const struct apple_clpc *clpc, + const struct apple_clpc_cluster *cl, + struct cpufreq_policy *policy) +{ + unsigned int i; + + if (cl->win_online < 2 || !cl->all_core_held) + return 0; + for (i = 0; i < ARRAY_SIZE(clpc->all_core_khz); i++) + if (clpc->all_core_khz[i] && + apple_clpc_has_khz(policy, clpc->all_core_khz[i])) + return clpc->all_core_khz[i]; + return 0; +} + +static unsigned int apple_clpc_sustained_step(struct cpufreq_policy *policy, + unsigned int cur_khz, + unsigned int top_khz, + unsigned int floor_khz, + int step) +{ + unsigned int base, next, min_khz; + + if (!top_khz) + return 0; + if (floor_khz) + min_khz = apple_clpc_step_khz(policy, floor_khz - 1, true); + else + min_khz = apple_clpc_step_khz(policy, policy->cpuinfo.min_freq, true); + if (step < 0) { + base = cur_khz ? min(cur_khz, top_khz) : top_khz; + next = apple_clpc_step_khz(policy, base, false); + if (min_khz && next < min_khz) + next = min_khz; + return min(next, top_khz); + } + if (step > 0) { + if (!cur_khz) + return 0; + next = apple_clpc_step_khz(policy, cur_khz, true); + return next >= top_khz ? 0 : next; + } + return cur_khz ? min(cur_khz, top_khz) : 0; +} + +/* Cap the slowest cluster while a faster cluster is busy outside low-power mode. */ +static void apple_clpc_e_cap(struct apple_clpc *clpc, + struct cpufreq_policy *const *policy) +{ + unsigned int cap = clpc->e_cap_khz, i, slow = 0, khz; + bool busy = false; + + if (!cap || clpc->nr_clusters < 2) + return; + for (i = 0; i < clpc->nr_clusters; i++) { + if (!policy[i]) + return; + if (policy[i]->cpuinfo.max_freq < policy[slow]->cpuinfo.max_freq) + slow = i; + } + for (i = 0; i < clpc->nr_clusters; i++) { + struct apple_clpc_cluster *cl = &clpc->clusters[i]; + + if (i != slow && + (cl->win_nbusy || cl->all_core_held || + cl->busy >= APPLE_CLPC_ALL_CORE_BUSY_PCT)) + busy = true; + } + khz = apple_clpc_step_khz(policy[slow], cap + 1, false); + if (!busy || khz > cap) + return; + if (!clpc->clusters[slow].max_khz || khz < clpc->clusters[slow].max_khz) + clpc->clusters[slow].max_khz = khz; +} + /* - * 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 power reading above it takes one step: off the GPU's cap while the - * GPU is active and above its lowest cap, otherwise one table step off one - * busy cluster (power_busy >= APPLE_CLPC_BUSY_PCT, never stepping onto the - * cluster's lowest table row); each reading below 90% of it puts one back, - * on any capped cluster first, in the order of apple_clpc_steps_first(), then - * on the GPU's cap. Elsewhere there is no ceiling and no cap. + * The platform profile's ceilings and optional board DT all-core/sustained + * limits. 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 power + * reading above it takes one step: off the GPU's cap while the GPU is active + * and above its lowest cap, otherwise one table step off one busy cluster + * (power_busy >= APPLE_CLPC_BUSY_PCT, never stepping onto the cluster's lowest + * table row); each reading below 90% of it puts one back, on any capped + * cluster first, in the order of apple_clpc_steps_first(), then on the GPU's + * cap. Outside the low-power profile, optional board DT ceilings apply while + * all online CPUs of a cluster are busy across the 100 ms window (with 200 ms + * release hysteresis), while a faster cluster is busy (slow-cluster cap), and + * while all-core load holds a faster cluster (sustained SMC package power). */ 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, cpu, pick_khz = 0; - int mw, step = 0, pick; - bool fresh = false; + unsigned int all_core[APPLE_CLPC_MAX_CLUSTERS] = {}; + unsigned int sustained_top[APPLE_CLPC_MAX_CLUSTERS] = {}; + unsigned int i, cpu, pick_khz = 0, slow = 0; + unsigned long now = jiffies; + bool fresh = false, have_slow = false, any_sustained = false; + int mw, step = 0, s_step = 0, pick; - if (!clpc->low_power_khz || time_before(jiffies, clpc->power_next)) + if ((!clpc->low_power_khz && !clpc->has_all_core) || + time_before(now, clpc->power_next)) return; - clpc->power_next = jiffies + msecs_to_jiffies(APPLE_CLPC_POWER_MS); + clpc->power_next = now + msecs_to_jiffies(APPLE_CLPC_POWER_MS); if (apple_clpc_power_limited(clpc)) { mw = apple_clpc_read_power(clpc); clpc->power_reads++; @@ -1378,6 +1565,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_all_core) + apple_clpc_sample_all_core_window(clpc, cl, now, low); if (fresh && clpc->power_wall) { cl->power_busy = apple_clpc_busy_since(cl, clpc->power_wall, clpc->power_idle); @@ -1396,17 +1585,80 @@ static void apple_clpc_profile_limit(struct apple_clpc *clpc) clpc->update_errors++; cl->top_khz = apple_clpc_low_power_top_khz(policy[i], clpc->low_power_khz); - if (!low) - cl->max_khz = 0; - else if (!cl->max_khz || !clpc->low_power_mw || - cl->max_khz > cl->top_khz) - cl->max_khz = cl->top_khz; + if (!low) { + all_core[i] = apple_clpc_all_core_khz(clpc, cl, policy[i]); + cl->max_khz = all_core[i]; + } else { + cl->power_khz = 0; + if (!cl->max_khz || !clpc->low_power_mw || + cl->max_khz > cl->top_khz) + cl->max_khz = cl->top_khz; + } } pick = apple_clpc_pick_profile_step(clpc, policy, step, &pick_khz); if (pick >= 0) clpc->clusters[pick].max_khz = pick_khz; else if (step > 0) apple_clpc_gpu_cap_up(clpc); + if (!low && clpc->has_all_core) { + if (clpc->nr_clusters >= 2 && policy[0]) { + have_slow = true; + for (i = 0; i < clpc->nr_clusters; i++) { + if (!policy[i]) { + have_slow = false; + break; + } + if (policy[i]->cpuinfo.max_freq < + policy[slow]->cpuinfo.max_freq) + slow = i; + } + } + for (i = 0; i < clpc->nr_clusters; i++) { + struct apple_clpc_cluster *cl = &clpc->clusters[i]; + + if (!policy[i] || (have_slow && i == slow)) + continue; + sustained_top[i] = all_core[i] ?: + (cl->all_core_held ? policy[i]->cpuinfo.max_freq : 0); + if (clpc->sustained_mw && clpc->smc_np && + (sustained_top[i] || cl->power_khz)) + any_sustained = true; + } + if (any_sustained) { + mw = apple_clpc_read_power(clpc); + clpc->power_reads++; + if (mw >= 0 && mw != clpc->power_mw) { + clpc->power_updates++; + if (mw > (int)clpc->sustained_mw) + s_step = -1; + else if (mw < (int)clpc->sustained_mw * + APPLE_CLPC_SUSTAINED_UP_PCT / 100) + s_step = 1; + } + clpc->power_mw = mw; + for (i = 0; i < clpc->nr_clusters; i++) { + struct apple_clpc_cluster *cl = &clpc->clusters[i]; + + if (!policy[i] || (have_slow && i == slow)) { + cl->power_khz = 0; + continue; + } + cl->power_khz = apple_clpc_sustained_step(policy[i], + cl->power_khz, + sustained_top[i], + clpc->low_power_khz, + s_step); + if (cl->power_khz && + (!cl->max_khz || cl->power_khz < cl->max_khz)) + cl->max_khz = cl->power_khz; + } + } else { + for (i = 0; i < clpc->nr_clusters; i++) + clpc->clusters[i].power_khz = 0; + clpc->power_mw = -1; + } + apple_clpc_e_cap(clpc, policy); + } for (i = 0; i < clpc->nr_clusters; i++) { struct apple_clpc_cluster *cl = &clpc->clusters[i]; @@ -1615,10 +1867,10 @@ static int apple_clpc_status_show(struct seq_file *s, void *unused) } seq_printf(s, "samples: %lu update errors: %lu\n", clpc->samples, clpc->update_errors); - if (clpc->low_power_khz) { + if (clpc->low_power_khz || clpc->has_all_core) { int power_mw = clpc->power_mw; - if (!clpc->low_power_mw && clpc->smc_np) { + if (clpc->low_power_khz && !clpc->low_power_mw && clpc->smc_np) { mutex_lock(&clpc->sample_lock); if (!READ_ONCE(clpc->stopped)) { int mw = apple_clpc_read_power(clpc); @@ -1637,10 +1889,10 @@ static int apple_clpc_status_show(struct seq_file *s, void *unused) clpc->clusters[i].max_khz); seq_puts(s, "\n"); if (clpc->smc_np) - seq_printf(s, "power key %c%c%c%c target %u mW gpu cap %u mW\n", + seq_printf(s, "power key %c%c%c%c target %u mW sustained %u mW gpu cap %u mW\n", (char)(clpc->power_key >> 24), (char)(clpc->power_key >> 16), (char)(clpc->power_key >> 8), (char)clpc->power_key, - clpc->low_power_mw, + clpc->low_power_mw, clpc->sustained_mw, clpc->gpu ? READ_ONCE(clpc->gpu->cap_mw) : 0); } if (clpc->high_power) @@ -1851,6 +2103,8 @@ static int apple_clpc_policy_notify(struct notifier_block *nb, unsigned long eve if (freq_qos_request_active(&clpc->clusters[i].max_qos)) freq_qos_remove_request(&clpc->clusters[i].max_qos); clpc->clusters[i].max_khz = 0; + clpc->clusters[i].power_khz = 0; + clpc->clusters[i].all_core_held = false; } if (clpc->ane_cpu < 0 || !cpumask_test_cpu(clpc->ane_cpu, policy->related_cpus)) return NOTIFY_OK; @@ -2084,6 +2338,45 @@ static const struct apple_clpc_hw *apple_clpc_dt_hw(struct device *dev, return hw; } +static void apple_clpc_all_core_dt_init(struct apple_clpc *clpc) +{ + struct device *dev = clpc->dev; + u32 cells[ARRAY_SIZE(clpc->all_core_khz)]; + u32 mhz, mw; + int i, n; + + if (of_property_present(dev->of_node, "apple,all-core-frequency-max-mhz")) { + n = apple_clpc_dt_cells(dev->of_node, "apple,all-core-frequency-max-mhz", + cells, ARRAY_SIZE(cells)); + if (n < 1) { + dev_warn(dev, + "ignoring invalid apple,all-core-frequency-max-mhz (%d)\n", n); + } else { + for (i = 0; i < n; i++) { + if (cells[i] > UINT_MAX / 1000) { + dev_warn(dev, + "ignoring out-of-range all-core frequency %u MHz\n", + cells[i]); + memset(clpc->all_core_khz, 0, sizeof(clpc->all_core_khz)); + break; + } + clpc->all_core_khz[i] = cells[i] * 1000; + } + } + } + if (!of_property_read_u32(dev->of_node, "apple,slow-cluster-busy-max-mhz", &mhz)) { + if (mhz <= UINT_MAX / 1000) + clpc->e_cap_khz = mhz * 1000; + else + dev_warn(dev, "ignoring out-of-range slow-cluster ceiling %u MHz\n", mhz); + } + if (!of_property_read_u32(dev->of_node, "apple,sustained-power-mw", &mw)) + clpc->sustained_mw = mw; + + clpc->has_all_core = clpc->all_core_khz[0] || clpc->all_core_khz[1] || + clpc->e_cap_khz || clpc->sustained_mw; +} + static void apple_clpc_put_smc_np(void *data) { struct apple_clpc *clpc = data; @@ -2130,6 +2423,7 @@ static int apple_clpc_probe(struct platform_device *pdev) clpc->low_power_mw = mw; else clpc->low_power_mw = clpc->hw->low_power_mw; + apple_clpc_all_core_dt_init(clpc); /* * The board DT says whether the machine has High Power mode: then the * performance profile tells the SMC that it is on (the zETM record). @@ -2144,12 +2438,14 @@ static int apple_clpc_probe(struct platform_device *pdev) */ if (!high_power && clpc->hw->high_power_machine) high_power = of_machine_is_compatible(clpc->hw->high_power_machine); - if ((clpc->low_power_khz || high_power) && - IS_REACHABLE(CONFIG_ACPI_PLATFORM_PROFILE)) { + if (((clpc->low_power_khz || high_power) && + IS_REACHABLE(CONFIG_ACPI_PLATFORM_PROFILE)) || + clpc->sustained_mw) { /* * The SMC is optional: without it (no apple,smc phandle, or a * kernel without the SMC driver) the low-power profile has only - * its ceiling and there is no High Power mode. + * its ceiling, there is no High Power mode, and sustained + * package power stepping stays inactive. */ if (IS_ENABLED(CONFIG_MFD_MACSMC)) { clpc->smc_np = of_parse_phandle(dev->of_node, "apple,smc", 0); @@ -2160,8 +2456,9 @@ static int apple_clpc_probe(struct platform_device *pdev) } } /* - * The SMC key whose power reading the low-power profile holds - * at its target: the board's, else PHPC. + * The SMC key whose power reading the low-power profile and + * sustained power limit hold at their targets: the board's, + * else PHPC. */ if (!of_property_read_string(dev->of_node, "apple,power-key-id", &key)) { if (strlen(key) != 4) @@ -2171,10 +2468,13 @@ static int apple_clpc_probe(struct platform_device *pdev) } else { clpc->power_key = SMC_KEY(PHPC); } - clpc->gpu_power_cap = of_property_read_bool(dev->of_node, - "apple,low-power-gpu-cap"); clpc->power_mw = -1; - clpc->high_power = clpc->smc_np && high_power; + if ((clpc->low_power_khz || high_power) && + IS_REACHABLE(CONFIG_ACPI_PLATFORM_PROFILE)) { + clpc->gpu_power_cap = of_property_read_bool(dev->of_node, + "apple,low-power-gpu-cap"); + clpc->high_power = clpc->smc_np && high_power; + } } /* High Power mode alone needs the SMC; without it there is no profile. */ has_profile = (clpc->low_power_khz || clpc->high_power) && @@ -2277,6 +2577,16 @@ static int apple_clpc_probe(struct platform_device *pdev) if (!clpc->power_idle || !clpc->power_wall) return -ENOMEM; } + if (clpc->has_all_core) { + clpc->win_idle = devm_kcalloc(dev, nr_cpu_ids, + sizeof(*clpc->win_idle), + GFP_KERNEL); + clpc->win_wall = devm_kcalloc(dev, nr_cpu_ids, + sizeof(*clpc->win_wall), + GFP_KERNEL); + if (!clpc->win_idle || !clpc->win_wall) + return -ENOMEM; + } for (i = 0; i < APPLE_CLPC_MAX_CLUSTERS; i++) { if (!zalloc_cpumask_var(&clpc->clusters[i].cpus, GFP_KERNEL)) { @@ -2349,14 +2659,14 @@ static int apple_clpc_probe(struct platform_device *pdev) clpc->profile = PLATFORM_PROFILE_BALANCED; clpc->power_next = jiffies; + if ((clpc->low_power_khz || clpc->has_all_core) && !clpc->policy_nb_added) { + clpc->policy_nb.notifier_call = apple_clpc_policy_notify; + clpc->policy_nb_added = !cpufreq_register_notifier(&clpc->policy_nb, + CPUFREQ_POLICY_NOTIFIER); + } if (has_profile) { struct device *ppdev; - if (clpc->low_power_khz && !clpc->policy_nb_added) { - clpc->policy_nb.notifier_call = apple_clpc_policy_notify; - clpc->policy_nb_added = !cpufreq_register_notifier(&clpc->policy_nb, - CPUFREQ_POLICY_NOTIFIER); - } /* * "apple-clpc" budgets the low-power profile itself; "apple-smc" * only carries High Power mode, and userspace keeps the CPU budget. @@ -2404,14 +2714,20 @@ static int apple_clpc_resume(struct device *dev) if (clpc->power_wall) for_each_possible_cpu(cpu) clpc->power_wall[cpu] = 0; + if (clpc->win_wall) + for_each_possible_cpu(cpu) + clpc->win_wall[cpu] = 0; /* * The low-power profile's steps answered readings from before the * sleep: start again from its ceiling and the GPU cap's start, and * act on the first reading after the wake. */ clpc->power_mw = -1; - for (i = 0; i < clpc->nr_clusters; i++) + for (i = 0; i < clpc->nr_clusters; i++) { clpc->clusters[i].max_khz = 0; + clpc->clusters[i].power_khz = 0; + clpc->clusters[i].all_core_held = false; + } if (clpc->gpu && READ_ONCE(clpc->gpu->cap_mw)) WRITE_ONCE(clpc->gpu->cap_mw, clpc->low_power_mw); clpc->power_next = jiffies; @@ -3610,6 +3926,130 @@ static void apple_clpc_gpu_busy_clear_test(struct kunit *test) kref_put(&gpu->ref, apple_clpc_gpu_free); } +static void apple_clpc_all_core_window_test(struct kunit *test) +{ + struct apple_clpc_cluster cl = {}; + unsigned long now = 1000; + unsigned long step = msecs_to_jiffies(APPLE_CLPC_POWER_MS); + unsigned long hold = msecs_to_jiffies(APPLE_CLPC_ALL_CORE_HOLD_MS); + + KUNIT_EXPECT_FALSE(test, apple_clpc_all_core_window_step(&cl, false, now, + APPLE_CLPC_ALL_CORE_HOLD_MS)); + KUNIT_EXPECT_TRUE(test, apple_clpc_all_core_window_step(&cl, true, now, + APPLE_CLPC_ALL_CORE_HOLD_MS)); + KUNIT_EXPECT_TRUE(test, apple_clpc_all_core_window_step(&cl, false, now + step, + APPLE_CLPC_ALL_CORE_HOLD_MS)); + KUNIT_EXPECT_FALSE(test, apple_clpc_all_core_window_step(&cl, false, now + hold, + APPLE_CLPC_ALL_CORE_HOLD_MS)); + KUNIT_EXPECT_TRUE(test, apple_clpc_all_core_window_step(&cl, true, now + hold, 0)); + KUNIT_EXPECT_FALSE(test, apple_clpc_all_core_window_step(&cl, false, now + hold + step, 0)); +} + +static void apple_clpc_sustained_power_test(struct kunit *test) +{ + struct cpufreq_frequency_table tbl[] = { + { .frequency = 1260000 }, + { .frequency = 2088000 }, + { .frequency = 3468000 }, + { .frequency = 3624000 }, + { .frequency = 3756000 }, + { .frequency = 3876000 }, + { .frequency = 3936000 }, + { .frequency = 4464000 }, + { .frequency = CPUFREQ_TABLE_END }, + }; + struct cpufreq_policy pol = { + .freq_table = tbl, + .cpuinfo = { .min_freq = 1260000, .max_freq = 4464000 }, + }; + unsigned int khz; + + /* Over budget steps down from all-core ceiling 3936 -> 3876 -> 3756 -> 3624. */ + khz = apple_clpc_sustained_step(&pol, 0, 3936000, 2088000, -1); + KUNIT_EXPECT_EQ(test, khz, 3876000u); + khz = apple_clpc_sustained_step(&pol, khz, 3936000, 2088000, -1); + KUNIT_EXPECT_EQ(test, khz, 3756000u); + khz = apple_clpc_sustained_step(&pol, khz, 3936000, 2088000, -1); + KUNIT_EXPECT_EQ(test, khz, 3624000u); + + /* Neutral reading holds current ceiling. */ + KUNIT_EXPECT_EQ(test, apple_clpc_sustained_step(&pol, khz, 3936000, 2088000, 0), 3624000u); + + /* Explicit floor clamp never drops below low_power_khz (2088 MHz). */ + khz = apple_clpc_sustained_step(&pol, 2088000, 3936000, 2088000, -1); + KUNIT_EXPECT_EQ(test, khz, 2088000u); + + /* Without a low-power floor, never step onto cpuinfo.min_freq. */ + khz = apple_clpc_sustained_step(&pol, 2088000, 3936000, 0, -1); + KUNIT_EXPECT_EQ(test, khz, 2088000u); + + /* Under budget steps back up and releases to 0 once reaching top. */ + khz = apple_clpc_sustained_step(&pol, 3876000, 3936000, 2088000, 1); + KUNIT_EXPECT_EQ(test, khz, 0u); + + /* Exiting all-core load releases immediately to 0. */ + KUNIT_EXPECT_EQ(test, apple_clpc_sustained_step(&pol, 3624000, 0, 2088000, 0), 0u); +} + +static void apple_clpc_all_core_and_e_cap_test(struct kunit *test) +{ + struct cpufreq_frequency_table e_tbl[] = { + { .frequency = 1020000 }, + { .frequency = 2808000 }, + { .frequency = 2892000 }, + { .frequency = CPUFREQ_TABLE_END }, + }; + struct cpufreq_frequency_table p_tbl[] = { + { .frequency = 1260000 }, + { .frequency = 3936000 }, + { .frequency = 4464000 }, + { .frequency = CPUFREQ_TABLE_END }, + }; + struct cpufreq_policy e_pol = { + .freq_table = e_tbl, + .cpuinfo = { .min_freq = 1020000, .max_freq = 2892000 }, + }; + struct cpufreq_policy p_pol = { + .freq_table = p_tbl, + .cpuinfo = { .min_freq = 1260000, .max_freq = 4464000 }, + }; + struct cpufreq_policy *policies[] = { &e_pol, &p_pol }; + struct apple_clpc *clpc = kunit_kzalloc(test, sizeof(*clpc), GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, clpc); + clpc->nr_clusters = 2; + clpc->all_core_khz[0] = 3936000; + clpc->all_core_khz[1] = 2808000; + clpc->e_cap_khz = 2808000; + + /* All-core ceiling applies only when >= 2 online CPUs are held busy. */ + clpc->clusters[0].win_online = 6; + clpc->clusters[1].win_online = 4; + KUNIT_EXPECT_EQ(test, apple_clpc_all_core_khz(clpc, &clpc->clusters[1], &p_pol), 0u); + clpc->clusters[1].all_core_held = true; + KUNIT_EXPECT_EQ(test, apple_clpc_all_core_khz(clpc, &clpc->clusters[1], &p_pol), 3936000u); + clpc->clusters[0].all_core_held = true; + KUNIT_EXPECT_EQ(test, apple_clpc_all_core_khz(clpc, &clpc->clusters[0], &e_pol), 2808000u); + + /* E-cluster stays uncapped for E-only work (P-cluster idle). */ + clpc->clusters[0].max_khz = 0; + clpc->clusters[1].all_core_held = false; + clpc->clusters[1].win_nbusy = 0; + clpc->clusters[1].busy = 10; + apple_clpc_e_cap(clpc, policies); + KUNIT_EXPECT_EQ(test, clpc->clusters[0].max_khz, 0u); + + /* Busy P-cluster caps E-cluster to 2808 MHz, avoiding the 2892 MHz cliff. */ + clpc->clusters[1].win_nbusy = 1; + apple_clpc_e_cap(clpc, policies); + KUNIT_EXPECT_EQ(test, clpc->clusters[0].max_khz, 2808000u); + + /* Existing lower ceiling is preserved. */ + clpc->clusters[0].max_khz = 2000000; + apple_clpc_e_cap(clpc, policies); + KUNIT_EXPECT_EQ(test, clpc->clusters[0].max_khz, 2000000u); +} + static struct kunit_case apple_clpc_policy_cases[] = { KUNIT_CASE(apple_clpc_step_khz_test), KUNIT_CASE(apple_clpc_steps_first_test), @@ -3617,6 +4057,9 @@ static struct kunit_case apple_clpc_policy_cases[] = { KUNIT_CASE(apple_clpc_floor_test), KUNIT_CASE(apple_clpc_profile_step_busy_test), KUNIT_CASE(apple_clpc_gpu_busy_clear_test), + KUNIT_CASE(apple_clpc_all_core_window_test), + KUNIT_CASE(apple_clpc_sustained_power_test), + KUNIT_CASE(apple_clpc_all_core_and_e_cap_test), {} }; static struct kunit_suite apple_clpc_policy_suite = { From 899e6ebcde40aad78a3aeb1cd0dcbe9f7927c55d Mon Sep 17 00:00:00 2001 From: Dylan Ironside Date: Wed, 2 Dec 2026 14:29:48 -0600 Subject: [PATCH 3/8] arm64: dts: apple: t8132-j604: configure CLPC all-core and sustained power limits Wire apple,smc and the J604 all-core/sustained CLPC power limits matching macOS on AC: - apple,all-core-frequency-max-mhz = <3936 2808> - apple,slow-cluster-busy-max-mhz = <2808> - apple,sustained-power-mw = <15500> Leaves single-core bursts (4464 MHz P), E-only workloads, and idle zero-SMC behavior unchanged. Signed-off-by: Dylan Ironside --- arch/arm64/boot/dts/apple/t8132-j604.dts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/arch/arm64/boot/dts/apple/t8132-j604.dts b/arch/arm64/boot/dts/apple/t8132-j604.dts index 8019dd4493dea..7553f2c3fef14 100644 --- a/arch/arm64/boot/dts/apple/t8132-j604.dts +++ b/arch/arm64/boot/dts/apple/t8132-j604.dts @@ -685,3 +685,16 @@ &aop_spmi1 { status = "okay"; }; + +/* + * All-core and sustained package power limits matching macOS on AC: + * P-cluster all-core ceiling 3936 MHz, E-cluster ceiling 2808 MHz + * (avoiding the 2892 MHz power cliff while P-cores are busy), and + * 15.5 W sustained all-core PHPC ceiling. + */ +&clpc { + apple,smc = <&smc>; + apple,all-core-frequency-max-mhz = <3936 2808>; + apple,slow-cluster-busy-max-mhz = <2808>; + apple,sustained-power-mw = <15500>; +}; From 47f942d2f1f2809bfebab26fa6f7bfacfbf91c6d Mon Sep 17 00:00:00 2001 From: Dylan Ironside Date: Wed, 2 Dec 2026 19:05:35 -0600 Subject: [PATCH 4/8] dt-bindings: apple: move CLPC limits to cpufreq domains and add CPU keys Address review feedback on CLPC all-core and sustained power bindings: - Move apple,all-core-frequency-max-mhz and apple,slow-cluster-busy-max-mhz from &clpc to the individual apple,cluster-cpufreq domain nodes alongside the new apple,sustained-frequency-min-mhz floor so each limit binds explicitly to its cluster and validates against that cluster OPP table. - Add apple,sustained-power-key-ids (required alongside apple,sustained-power-mw and apple,smc) so sustained CPU regulation sums dedicated CPU cluster rails instead of package PHPC (which includes GPU and display power). - Clarify that these optional limits apply outside the low-power platform profile (balanced and performance, on AC and battery). Signed-off-by: Dylan Ironside --- .../cpufreq/apple,cluster-cpufreq.yaml | 23 +++++++++ .../devicetree/bindings/power/apple,pmc.yaml | 51 +++++++++++-------- 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/Documentation/devicetree/bindings/cpufreq/apple,cluster-cpufreq.yaml b/Documentation/devicetree/bindings/cpufreq/apple,cluster-cpufreq.yaml index 6ffe0444f6468..5fc3a339e3d4c 100644 --- a/Documentation/devicetree/bindings/cpufreq/apple,cluster-cpufreq.yaml +++ b/Documentation/devicetree/bindings/cpufreq/apple,cluster-cpufreq.yaml @@ -78,6 +78,29 @@ properties: apple,fast-die-effort lets the OS enforce it. The T6050 driver registers DVFS only when this is present. + apple,all-core-frequency-max-mhz: + description: + CPU frequency ceiling in MHz applied to this cluster by apple-clpc + outside the low-power platform profile (both balanced and performance + profiles, on AC and battery) when all online CPUs of this cluster are + busy across the 100 ms CLPC power window. Must match an OPP in this + cluster's operating-points-v2 table. + + apple,slow-cluster-busy-max-mhz: + description: + CPU frequency ceiling in MHz applied to this cluster by apple-clpc + outside the low-power platform profile (both balanced and performance + profiles, on AC and battery) only while a faster cluster is busy. A + cluster with only this ceiling remains uncapped under slow-cluster-only + workloads. Must match an OPP in this cluster's operating-points-v2 + table. + + apple,sustained-frequency-min-mhz: + description: + Minimum CPU frequency floor in MHz for this cluster under apple-clpc + sustained all-core CPU power regulation (apple,sustained-power-mw). + Must match an OPP in this cluster's operating-points-v2 table. + '#performance-domain-cells': const: 0 diff --git a/Documentation/devicetree/bindings/power/apple,pmc.yaml b/Documentation/devicetree/bindings/power/apple,pmc.yaml index 5d8530f33f3cf..3c138468fa85d 100644 --- a/Documentation/devicetree/bindings/power/apple,pmc.yaml +++ b/Documentation/devicetree/bindings/power/apple,pmc.yaml @@ -283,10 +283,12 @@ patternProperties: apple,smc: $ref: /schemas/types.yaml#/definitions/phandle description: - The SMC, whose package power reading the low-power platform profile - and sustained all-core power limit hold the CPU clusters to, and - which the performance profile tells about High Power mode on Macs - that have it. + The SMC, whose package power reading (apple,power-key-id) the + low-power platform profile holds the CPU clusters to, whose CPU + cluster rail readings (apple,sustained-power-key-ids) the sustained + all-core CPU power limit regulates against, and which the + performance/low-power profiles notify on Macs that support those + records. apple,high-power-mode: type: boolean @@ -311,30 +313,30 @@ patternProperties: apple,power-key-id reading at it with the CPU clusters' ceilings (and, with apple,low-power-gpu-cap, the GPU's power budget). - apple,all-core-frequency-max-mhz: + apple,sustained-power-key-ids: + $ref: /schemas/types.yaml#/definitions/string-array minItems: 1 - maxItems: 2 - description: - Per-cluster-type CPU frequency ceilings in MHz applied outside the - low-power platform profile when all online CPUs of a cluster are - busy across the 100 ms power window. Each entry applies to any - cluster whose cpufreq frequency table contains that frequency. - - apple,slow-cluster-busy-max-mhz: - maxItems: 1 + maxItems: 4 + items: + pattern: "^[A-Za-z0-9#!]{4}$" description: - CPU frequency ceiling in MHz applied to the slowest cluster outside - the low-power platform profile while a faster cluster is busy. + Four-character SMC keys of the CPU cluster power rails (each a + float in watts) summed by the sustained all-core CPU power limit to + exclude GPU and display power. Required with apple,sustained-power-mw. apple,sustained-power-mw: $ref: /schemas/types.yaml#/definitions/uint32 description: - Sustained all-core package power target in milliwatts outside the - low-power platform profile. With apple,smc, while all-core load - holds a faster cluster, the driver polls the SMC's - apple,power-key-id reading every 100 ms and steps faster clusters' - frequency ceilings to hold package power near this target, releasing - immediately when all-core load ends. + Sustained all-core CPU power target in milliwatts applied outside + the low-power platform profile (both balanced and performance + profiles, on AC and battery). Requires apple,smc and + apple,sustained-power-key-ids. While all-core load holds a faster + cluster, the driver polls and sums the SMC's + apple,sustained-power-key-ids readings every 100 ms and steps faster + clusters' frequency ceilings between their all-core ceiling and + their sustained frequency floor (apple,sustained-frequency-min-mhz, + or max(low-power ceiling, half of max frequency) if unset), + releasing immediately when all-core load ends. apple,power-key-id: $ref: /schemas/types.yaml#/definitions/string @@ -411,6 +413,11 @@ patternProperties: - apple,gpu-busy-permille apple,gpu-busy-permille: - apple,gpu-busy-memory-states + apple,sustained-power-mw: + - apple,smc + - apple,sustained-power-key-ids + apple,sustained-power-key-ids: + - apple,sustained-power-mw required: - compatible From e450562323ccf85c41623d90facfbfae122be21c Mon Sep 17 00:00:00 2001 From: Dylan Ironside Date: Wed, 2 Dec 2026 19:05:37 -0600 Subject: [PATCH 5/8] soc: apple: clpc: regulate sustained CPU power on CPU rails with OPP limits Address review feedback on CLPC all-core and sustained CPU limits: - Regulate sustained all-core CPU power using the summed SMC CPU rails listed in apple,sustained-power-key-ids (separate from the low-power profile PHPC power_key) so GPU and display loads never throttle CPU. - Enforce an explicit per-cluster sustained frequency floor (apple,sustained-frequency-min-mhz, with a >= max_freq/2 fallback if unset) instead of falling back to the second OPP (1260 MHz). - Read per-cluster apple,all-core-frequency-max-mhz, apple,slow-cluster-busy-max-mhz, and apple,sustained-frequency-min-mhz from each cluster performance-domains node and warn if any value is not a valid OPP in that cluster frequency table. - Keep slow-cluster-only workloads uncapped when only apple,slow-cluster-busy-max-mhz is configured on the slow cluster. - Document the J604 measurement basis for the 80% busy threshold, 200 ms release hold, and 92% step-up hysteresis next to the constants, and expand apple-clpc-policy KUnit coverage for OPP validation, explicit and fallback floors, and uncapped E-only load. Signed-off-by: Dylan Ironside --- drivers/soc/apple/clpc.c | 414 +++++++++++++++++++++++++++------------ 1 file changed, 289 insertions(+), 125 deletions(-) diff --git a/drivers/soc/apple/clpc.c b/drivers/soc/apple/clpc.c index 2ad64e9aced22..f3f284aa2a13c 100644 --- a/drivers/soc/apple/clpc.c +++ b/drivers/soc/apple/clpc.c @@ -74,11 +74,23 @@ enum apple_clpc_rail { #define APPLE_CLPC_GPU_BACKOFF_MS 100 /* Package power readings in the low-power profile: every 100 ms */ #define APPLE_CLPC_POWER_MS 100 -/* All-core busy window threshold (%) and release hysteresis (ms) */ +/* + * All-core window and sustained CPU-rail regulation parameters: + * - 80% per-CPU busy across the 100 ms window distinguishes sustained + * all-core load (e.g. 7-Zip mtall ~915% CPU, ~88-96% per P-core on J604) + * from few-thread bursts while eliminating the ~6-9.5% 4464 MHz turbo + * leak seen with 20 ms sampling. + * - 200 ms hold (two 100 ms windows) bridges brief I/O or lock dips between + * all-core blocks without spiking to single-core turbo, then releases + * promptly for subsequent single-thread work. + * - 92% step-up threshold provides an 8% deadband below target (comparable + * to the low-power profile's 90% deadband, scaled to the 13.5 W CPU-rail + * budget so a single ~120 MHz / ~1 W P-cluster OPP step does not oscillate). + */ #define APPLE_CLPC_ALL_CORE_BUSY_PCT 80 #define APPLE_CLPC_ALL_CORE_HOLD_MS 200 -/* Step sustained all-core ceiling back up below this % of target */ #define APPLE_CLPC_SUSTAINED_UP_PCT 92 +#define APPLE_CLPC_MAX_SUSTAINED_KEYS 4 /* * The GPU power cap of the low-power profile, in fractions of its power * target: each step moves the cap by 1/APPLE_CLPC_GPU_CAP_STEPS of the target, @@ -187,6 +199,10 @@ struct apple_clpc_cluster { unsigned long all_core_until; bool all_core_held; unsigned int power_khz; + /* Optional per-cluster DT ceilings/floor (kHz, 0 = unset), OPP-validated */ + unsigned int all_core_khz; + unsigned int slow_busy_khz; + unsigned int sustained_min_khz; unsigned int streak; unsigned int level; unsigned int request[APPLE_CLPC_RAILS]; @@ -338,10 +354,11 @@ struct apple_clpc { /* Per-CPU baselines for power_busy, taken at each package power reading */ u64 *power_idle; u64 *power_wall; - /* Optional board DT all-core and sustained package power limits (0 = off) */ - unsigned int all_core_khz[2]; - unsigned int e_cap_khz; + /* Optional board DT sustained CPU-rail power limits (0 = off) */ + smc_key sustained_keys[APPLE_CLPC_MAX_SUSTAINED_KEYS]; + unsigned int nr_sustained_keys; unsigned int sustained_mw; + int sustained_power_mw; bool has_all_core; u64 *win_idle; u64 *win_wall; @@ -680,6 +697,78 @@ static bool apple_clpc_ane_input_demand(struct apple_clpc *clpc, unsigned int *d return active; } +static bool apple_clpc_has_khz(struct cpufreq_policy *policy, unsigned int khz) +{ + struct cpufreq_frequency_table *pos; + + if (!policy->freq_table) + return false; + cpufreq_for_each_valid_entry(pos, policy->freq_table) + if (pos->frequency == khz) + return true; + return false; +} + +static unsigned int apple_clpc_validate_opp_khz(struct device *dev, + const struct device_node *np, + const char *prop, + struct cpufreq_policy *policy, + unsigned int khz) +{ + if (!khz) + return 0; + if (!apple_clpc_has_khz(policy, khz)) { + if (dev) + dev_warn(dev, "%pOFn: %s (%u MHz) is not an OPP\n", + np, prop, khz / 1000); + return 0; + } + return khz; +} + +static unsigned int apple_clpc_read_domain_mhz(struct device *dev, + const struct device_node *dom, + const char *prop, + struct cpufreq_policy *policy) +{ + u32 mhz; + + if (of_property_read_u32(dom, prop, &mhz)) + return 0; + if (!mhz || mhz > UINT_MAX / 1000) { + dev_warn(dev, "%pOFn: ignoring out-of-range %s (%u MHz)\n", + dom, prop, mhz); + return 0; + } + return apple_clpc_validate_opp_khz(dev, dom, prop, policy, mhz * 1000); +} + +static void apple_clpc_cluster_dt_limits(struct apple_clpc *clpc, + struct apple_clpc_cluster *cl, + struct cpufreq_policy *policy) +{ + struct device_node *cpu_np, *dom; + + cpu_np = of_cpu_device_node_get(policy->cpu); + if (!cpu_np) + return; + dom = of_parse_phandle(cpu_np, "performance-domains", 0); + of_node_put(cpu_np); + if (!dom) + return; + + cl->all_core_khz = apple_clpc_read_domain_mhz(clpc->dev, dom, + "apple,all-core-frequency-max-mhz", + policy); + cl->slow_busy_khz = apple_clpc_read_domain_mhz(clpc->dev, dom, + "apple,slow-cluster-busy-max-mhz", + policy); + cl->sustained_min_khz = apple_clpc_read_domain_mhz(clpc->dev, dom, + "apple,sustained-frequency-min-mhz", + policy); + of_node_put(dom); +} + /* cpufreq policies can appear after probe; pick up new ones as they do. */ static void apple_clpc_find_clusters(struct apple_clpc *clpc) { @@ -700,6 +789,8 @@ static void apple_clpc_find_clusters(struct apple_clpc *clpc) if (i == clpc->nr_clusters && i < APPLE_CLPC_MAX_CLUSTERS) { cl = &clpc->clusters[clpc->nr_clusters++]; cl->policy_cpu = policy->cpu; + if (clpc->has_all_core) + apple_clpc_cluster_dt_limits(clpc, cl, policy); } if (i < clpc->nr_clusters) { cl = &clpc->clusters[i]; @@ -1242,8 +1333,8 @@ static struct apple_smc *apple_clpc_smc(struct apple_clpc *clpc) return clpc->smc; } -/* The power the SMC reports in @power_key (a float in W), in mW. */ -static int apple_clpc_read_power(struct apple_clpc *clpc) +/* The power the SMC reports in @key (a float in W), in mW. */ +static int apple_clpc_read_smc_key_mw(struct apple_clpc *clpc, smc_key key) { int exp, ret; u64 mw; @@ -1251,7 +1342,7 @@ static int apple_clpc_read_power(struct apple_clpc *clpc) if (!IS_ENABLED(CONFIG_MFD_MACSMC) || !apple_clpc_smc(clpc)) return -ENODEV; - ret = apple_smc_read_u32(clpc->smc, clpc->power_key, &f); + ret = apple_smc_read_u32(clpc->smc, key, &f); if (ret) return ret; exp = (f >> 23) & 0xff; @@ -1264,6 +1355,30 @@ static int apple_clpc_read_power(struct apple_clpc *clpc) return exp >= 0 ? INT_MAX : mw >> -exp; } +/* The power the SMC reports in @power_key (a float in W), in mW. */ +static int apple_clpc_read_power(struct apple_clpc *clpc) +{ + return apple_clpc_read_smc_key_mw(clpc, clpc->power_key); +} + +/* Sum of the CPU cluster SMC power keys in @sustained_keys, in mW. */ +static int apple_clpc_read_sustained_power(struct apple_clpc *clpc) +{ + u64 total = 0; + unsigned int i; + int mw; + + if (!clpc->nr_sustained_keys) + return -ENODEV; + for (i = 0; i < clpc->nr_sustained_keys; i++) { + mw = apple_clpc_read_smc_key_mw(clpc, clpc->sustained_keys[i]); + if (mw < 0) + return mw; + total += mw; + } + return total > INT_MAX ? INT_MAX : total; +} + /* * Whether cluster a's ceiling takes the next step before cluster b's: down, * the higher ceiling (the faster cluster on a tie); up, the lower (the @@ -1368,18 +1483,6 @@ static int apple_clpc_pick_profile_step(struct apple_clpc *clpc, return pick; } -static bool apple_clpc_has_khz(struct cpufreq_policy *policy, unsigned int khz) -{ - struct cpufreq_frequency_table *pos; - - if (!policy->freq_table) - return false; - cpufreq_for_each_valid_entry(pos, policy->freq_table) - if (pos->frequency == khz) - return true; - return false; -} - static bool apple_clpc_all_core_window_step(struct apple_clpc_cluster *cl, bool all_busy, unsigned long now, unsigned int hold_ms) @@ -1452,19 +1555,11 @@ static void apple_clpc_sample_all_core_window(struct apple_clpc *clpc, } /* Configured all-core ceiling for a cluster whose online CPUs are all busy, else 0. */ -static unsigned int apple_clpc_all_core_khz(const struct apple_clpc *clpc, - const struct apple_clpc_cluster *cl, - struct cpufreq_policy *policy) +static unsigned int apple_clpc_all_core_khz(const struct apple_clpc_cluster *cl) { - unsigned int i; - if (cl->win_online < 2 || !cl->all_core_held) return 0; - for (i = 0; i < ARRAY_SIZE(clpc->all_core_khz); i++) - if (clpc->all_core_khz[i] && - apple_clpc_has_khz(policy, clpc->all_core_khz[i])) - return clpc->all_core_khz[i]; - return 0; + return cl->all_core_khz; } static unsigned int apple_clpc_sustained_step(struct cpufreq_policy *policy, @@ -1477,14 +1572,15 @@ static unsigned int apple_clpc_sustained_step(struct cpufreq_policy *policy, if (!top_khz) return 0; - if (floor_khz) - min_khz = apple_clpc_step_khz(policy, floor_khz - 1, true); - else - min_khz = apple_clpc_step_khz(policy, policy->cpuinfo.min_freq, true); + if (!floor_khz) + floor_khz = policy->cpuinfo.max_freq / 2; + min_khz = apple_clpc_step_khz(policy, floor_khz - 1, true); + if (min_khz < floor_khz) + min_khz = policy->cpuinfo.max_freq; if (step < 0) { base = cur_khz ? min(cur_khz, top_khz) : top_khz; next = apple_clpc_step_khz(policy, base, false); - if (min_khz && next < min_khz) + if (next < min_khz) next = min_khz; return min(next, top_khz); } @@ -1497,34 +1593,42 @@ static unsigned int apple_clpc_sustained_step(struct cpufreq_policy *policy, return cur_khz ? min(cur_khz, top_khz) : 0; } -/* Cap the slowest cluster while a faster cluster is busy outside low-power mode. */ +/* + * Cap any cluster with a configured slow-cluster ceiling while a faster + * cluster is busy outside the low-power profile. Slow-cluster-only workloads + * (faster clusters idle) stay uncapped. + */ static void apple_clpc_e_cap(struct apple_clpc *clpc, struct cpufreq_policy *const *policy) { - unsigned int cap = clpc->e_cap_khz, i, slow = 0, khz; - bool busy = false; + unsigned int i, j; - if (!cap || clpc->nr_clusters < 2) + if (clpc->nr_clusters < 2) return; - for (i = 0; i < clpc->nr_clusters; i++) { - if (!policy[i]) - return; - if (policy[i]->cpuinfo.max_freq < policy[slow]->cpuinfo.max_freq) - slow = i; - } for (i = 0; i < clpc->nr_clusters; i++) { struct apple_clpc_cluster *cl = &clpc->clusters[i]; + bool faster_busy = false; - if (i != slow && - (cl->win_nbusy || cl->all_core_held || - cl->busy >= APPLE_CLPC_ALL_CORE_BUSY_PCT)) - busy = true; + if (!policy[i] || !cl->slow_busy_khz) + continue; + for (j = 0; j < clpc->nr_clusters; j++) { + struct apple_clpc_cluster *other = &clpc->clusters[j]; + + if (j == i || !policy[j]) + continue; + if (policy[j]->cpuinfo.max_freq <= policy[i]->cpuinfo.max_freq) + continue; + if (other->win_nbusy || other->all_core_held || + other->busy >= APPLE_CLPC_ALL_CORE_BUSY_PCT) { + faster_busy = true; + break; + } + } + if (!faster_busy) + continue; + if (!cl->max_khz || cl->slow_busy_khz < cl->max_khz) + cl->max_khz = cl->slow_busy_khz; } - khz = apple_clpc_step_khz(policy[slow], cap + 1, false); - if (!busy || khz > cap) - return; - if (!clpc->clusters[slow].max_khz || khz < clpc->clusters[slow].max_khz) - clpc->clusters[slow].max_khz = khz; } /* @@ -1595,7 +1699,7 @@ static void apple_clpc_profile_limit(struct apple_clpc *clpc) cl->top_khz = apple_clpc_low_power_top_khz(policy[i], clpc->low_power_khz); if (!low) { - all_core[i] = apple_clpc_all_core_khz(clpc, cl, policy[i]); + all_core[i] = apple_clpc_all_core_khz(cl); cl->max_khz = all_core[i]; } else { cl->power_khz = 0; @@ -1609,6 +1713,8 @@ static void apple_clpc_profile_limit(struct apple_clpc *clpc) clpc->clusters[pick].max_khz = pick_khz; else if (step > 0) apple_clpc_gpu_cap_up(clpc); + if (low) + clpc->sustained_power_mw = -1; if (!low && clpc->has_all_core) { if (clpc->nr_clusters >= 2 && policy[0]) { have_slow = true; @@ -1629,14 +1735,14 @@ static void apple_clpc_profile_limit(struct apple_clpc *clpc) continue; sustained_top[i] = all_core[i] ?: (cl->all_core_held ? policy[i]->cpuinfo.max_freq : 0); - if (clpc->sustained_mw && clpc->smc_np && - (sustained_top[i] || cl->power_khz)) + if (clpc->sustained_mw && clpc->nr_sustained_keys && + clpc->smc_np && (sustained_top[i] || cl->power_khz)) any_sustained = true; } if (any_sustained) { - mw = apple_clpc_read_power(clpc); + mw = apple_clpc_read_sustained_power(clpc); clpc->power_reads++; - if (mw >= 0 && mw != clpc->power_mw) { + if (mw >= 0 && mw != clpc->sustained_power_mw) { clpc->power_updates++; if (mw > (int)clpc->sustained_mw) s_step = -1; @@ -1644,19 +1750,22 @@ static void apple_clpc_profile_limit(struct apple_clpc *clpc) APPLE_CLPC_SUSTAINED_UP_PCT / 100) s_step = 1; } - clpc->power_mw = mw; + clpc->sustained_power_mw = mw; for (i = 0; i < clpc->nr_clusters; i++) { struct apple_clpc_cluster *cl = &clpc->clusters[i]; + unsigned int floor; if (!policy[i] || (have_slow && i == slow)) { cl->power_khz = 0; continue; } + floor = cl->sustained_min_khz ?: + max(clpc->low_power_khz, + policy[i]->cpuinfo.max_freq / 2); cl->power_khz = apple_clpc_sustained_step(policy[i], cl->power_khz, sustained_top[i], - clpc->low_power_khz, - s_step); + floor, s_step); if (cl->power_khz && (!cl->max_khz || cl->power_khz < cl->max_khz)) cl->max_khz = cl->power_khz; @@ -1664,7 +1773,7 @@ static void apple_clpc_profile_limit(struct apple_clpc *clpc) } else { for (i = 0; i < clpc->nr_clusters; i++) clpc->clusters[i].power_khz = 0; - clpc->power_mw = -1; + clpc->sustained_power_mw = -1; } apple_clpc_e_cap(clpc, policy); } @@ -1897,12 +2006,23 @@ static int apple_clpc_status_show(struct seq_file *s, void *unused) seq_printf(s, " cpu%u %u", clpc->clusters[i].policy_cpu, clpc->clusters[i].max_khz); seq_puts(s, "\n"); - if (clpc->smc_np) - seq_printf(s, "power key %c%c%c%c target %u mW sustained %u mW gpu cap %u mW\n", + if (clpc->smc_np && clpc->low_power_khz) + seq_printf(s, "power key %c%c%c%c target %u mW gpu cap %u mW\n", (char)(clpc->power_key >> 24), (char)(clpc->power_key >> 16), (char)(clpc->power_key >> 8), (char)clpc->power_key, - clpc->low_power_mw, clpc->sustained_mw, + clpc->low_power_mw, clpc->gpu ? READ_ONCE(clpc->gpu->cap_mw) : 0); + if (clpc->smc_np && clpc->sustained_mw && clpc->nr_sustained_keys) { + seq_puts(s, "sustained keys"); + for (i = 0; i < clpc->nr_sustained_keys; i++) { + smc_key k = clpc->sustained_keys[i]; + + seq_printf(s, " %c%c%c%c", (char)(k >> 24), (char)(k >> 16), + (char)(k >> 8), (char)k); + } + seq_printf(s, " target %u mW last %d mW\n", + clpc->sustained_mw, clpc->sustained_power_mw); + } } if (clpc->high_power || clpc->low_power_notice) seq_printf(s, "smc power mode: high power %s, low power %s (%u records sent)\n", @@ -2415,43 +2535,71 @@ static const struct apple_clpc_hw *apple_clpc_dt_hw(struct device *dev, return hw; } +static bool apple_clpc_has_cluster_limits(void) +{ + unsigned int cpu; + + for_each_possible_cpu(cpu) { + struct device_node *cpu_np, *dom; + bool found; + + cpu_np = of_cpu_device_node_get(cpu); + if (!cpu_np) + continue; + dom = of_parse_phandle(cpu_np, "performance-domains", 0); + of_node_put(cpu_np); + if (!dom) + continue; + found = of_property_present(dom, "apple,all-core-frequency-max-mhz") || + of_property_present(dom, "apple,slow-cluster-busy-max-mhz") || + of_property_present(dom, "apple,sustained-frequency-min-mhz"); + of_node_put(dom); + if (found) + return true; + } + return false; +} + static void apple_clpc_all_core_dt_init(struct apple_clpc *clpc) { struct device *dev = clpc->dev; - u32 cells[ARRAY_SIZE(clpc->all_core_khz)]; - u32 mhz, mw; + struct device_node *np = dev->of_node; + const char *key; + u32 mw; int i, n; - if (of_property_present(dev->of_node, "apple,all-core-frequency-max-mhz")) { - n = apple_clpc_dt_cells(dev->of_node, "apple,all-core-frequency-max-mhz", - cells, ARRAY_SIZE(cells)); - if (n < 1) { + clpc->sustained_power_mw = -1; + if (!of_property_read_u32(np, "apple,sustained-power-mw", &mw) && mw) { + n = of_property_count_strings(np, "apple,sustained-power-key-ids"); + if (n < 1 || n > APPLE_CLPC_MAX_SUSTAINED_KEYS) { dev_warn(dev, - "ignoring invalid apple,all-core-frequency-max-mhz (%d)\n", n); + "ignoring apple,sustained-power-mw without valid apple,sustained-power-key-ids (%d)\n", + n); } else { + bool valid = true; + for (i = 0; i < n; i++) { - if (cells[i] > UINT_MAX / 1000) { + if (of_property_read_string_index(np, + "apple,sustained-power-key-ids", + i, &key) || + strlen(key) != 4) { dev_warn(dev, - "ignoring out-of-range all-core frequency %u MHz\n", - cells[i]); - memset(clpc->all_core_khz, 0, sizeof(clpc->all_core_khz)); + "ignoring invalid apple,sustained-power-key-ids[%d]\n", + i); + valid = false; break; } - clpc->all_core_khz[i] = cells[i] * 1000; + clpc->sustained_keys[i] = _SMC_KEY(key); + } + if (valid) { + clpc->nr_sustained_keys = n; + clpc->sustained_mw = mw; } } } - if (!of_property_read_u32(dev->of_node, "apple,slow-cluster-busy-max-mhz", &mhz)) { - if (mhz <= UINT_MAX / 1000) - clpc->e_cap_khz = mhz * 1000; - else - dev_warn(dev, "ignoring out-of-range slow-cluster ceiling %u MHz\n", mhz); - } - if (!of_property_read_u32(dev->of_node, "apple,sustained-power-mw", &mw)) - clpc->sustained_mw = mw; - clpc->has_all_core = clpc->all_core_khz[0] || clpc->all_core_khz[1] || - clpc->e_cap_khz || clpc->sustained_mw; + clpc->has_all_core = apple_clpc_has_cluster_limits() || + (clpc->sustained_mw && clpc->nr_sustained_keys); } static void apple_clpc_put_smc_np(void *data) @@ -2517,12 +2665,12 @@ static int apple_clpc_probe(struct platform_device *pdev) high_power = of_machine_is_compatible(clpc->hw->high_power_machine); if (((clpc->low_power_khz || high_power) && IS_REACHABLE(CONFIG_ACPI_PLATFORM_PROFILE)) || - clpc->sustained_mw) { + (clpc->sustained_mw && clpc->nr_sustained_keys)) { /* * The SMC is optional: without it (no apple,smc phandle, or a * kernel without the SMC driver) the low-power profile has only * its ceiling, there is no High Power mode, and sustained - * package power stepping stays inactive. + * CPU-rail power stepping stays inactive. */ if (IS_ENABLED(CONFIG_MFD_MACSMC)) { clpc->smc_np = of_parse_phandle(dev->of_node, "apple,smc", 0); @@ -2532,22 +2680,23 @@ static int apple_clpc_probe(struct platform_device *pdev) return ret; } } - /* - * The SMC key whose power reading the low-power profile and - * sustained power limit hold at their targets: the board's, - * else PHPC. - */ - if (!of_property_read_string(dev->of_node, "apple,power-key-id", &key)) { - if (strlen(key) != 4) - return dev_err_probe(dev, -EINVAL, - "invalid apple,power-key-id \"%s\"\n", key); - clpc->power_key = _SMC_KEY(key); - } else { - clpc->power_key = SMC_KEY(PHPC); - } clpc->power_mw = -1; + clpc->sustained_power_mw = -1; if ((clpc->low_power_khz || high_power) && IS_REACHABLE(CONFIG_ACPI_PLATFORM_PROFILE)) { + /* + * The SMC key whose power reading the low-power profile + * holds at its target: the board's, else PHPC. + */ + if (!of_property_read_string(dev->of_node, "apple,power-key-id", &key)) { + if (strlen(key) != 4) + return dev_err_probe(dev, -EINVAL, + "invalid apple,power-key-id \"%s\"\n", + key); + clpc->power_key = _SMC_KEY(key); + } else { + clpc->power_key = SMC_KEY(PHPC); + } clpc->gpu_power_cap = of_property_read_bool(dev->of_node, "apple,low-power-gpu-cap"); clpc->high_power = clpc->smc_np && high_power; @@ -2805,6 +2954,7 @@ static int apple_clpc_resume(struct device *dev) * act on the first reading after the wake. */ clpc->power_mw = -1; + clpc->sustained_power_mw = -1; for (i = 0; i < clpc->nr_clusters; i++) { clpc->clusters[i].max_khz = 0; clpc->clusters[i].power_khz = 0; @@ -4066,6 +4216,8 @@ static void apple_clpc_sustained_power_test(struct kunit *test) struct cpufreq_frequency_table tbl[] = { { .frequency = 1260000 }, { .frequency = 2088000 }, + { .frequency = 2616000 }, + { .frequency = 3096000 }, { .frequency = 3468000 }, { .frequency = 3624000 }, { .frequency = 3756000 }, @@ -4081,30 +4233,30 @@ static void apple_clpc_sustained_power_test(struct kunit *test) unsigned int khz; /* Over budget steps down from all-core ceiling 3936 -> 3876 -> 3756 -> 3624. */ - khz = apple_clpc_sustained_step(&pol, 0, 3936000, 2088000, -1); + khz = apple_clpc_sustained_step(&pol, 0, 3936000, 3096000, -1); KUNIT_EXPECT_EQ(test, khz, 3876000u); - khz = apple_clpc_sustained_step(&pol, khz, 3936000, 2088000, -1); + khz = apple_clpc_sustained_step(&pol, khz, 3936000, 3096000, -1); KUNIT_EXPECT_EQ(test, khz, 3756000u); - khz = apple_clpc_sustained_step(&pol, khz, 3936000, 2088000, -1); + khz = apple_clpc_sustained_step(&pol, khz, 3936000, 3096000, -1); KUNIT_EXPECT_EQ(test, khz, 3624000u); /* Neutral reading holds current ceiling. */ - KUNIT_EXPECT_EQ(test, apple_clpc_sustained_step(&pol, khz, 3936000, 2088000, 0), 3624000u); + KUNIT_EXPECT_EQ(test, apple_clpc_sustained_step(&pol, khz, 3936000, 3096000, 0), 3624000u); - /* Explicit floor clamp never drops below low_power_khz (2088 MHz). */ - khz = apple_clpc_sustained_step(&pol, 2088000, 3936000, 2088000, -1); - KUNIT_EXPECT_EQ(test, khz, 2088000u); + /* Explicit per-cluster floor (3096 MHz) prevents dropping further. */ + khz = apple_clpc_sustained_step(&pol, 3096000, 3936000, 3096000, -1); + KUNIT_EXPECT_EQ(test, khz, 3096000u); - /* Without a low-power floor, never step onto cpuinfo.min_freq. */ - khz = apple_clpc_sustained_step(&pol, 2088000, 3936000, 0, -1); - KUNIT_EXPECT_EQ(test, khz, 2088000u); + /* Without an explicit floor, fallback clamps at >= max_freq / 2 (2616 MHz). */ + khz = apple_clpc_sustained_step(&pol, 2616000, 3936000, 0, -1); + KUNIT_EXPECT_EQ(test, khz, 2616000u); /* Under budget steps back up and releases to 0 once reaching top. */ - khz = apple_clpc_sustained_step(&pol, 3876000, 3936000, 2088000, 1); + khz = apple_clpc_sustained_step(&pol, 3876000, 3936000, 3096000, 1); KUNIT_EXPECT_EQ(test, khz, 0u); /* Exiting all-core load releases immediately to 0. */ - KUNIT_EXPECT_EQ(test, apple_clpc_sustained_step(&pol, 3624000, 0, 2088000, 0), 0u); + KUNIT_EXPECT_EQ(test, apple_clpc_sustained_step(&pol, 3624000, 0, 3096000, 0), 0u); } static void apple_clpc_all_core_and_e_cap_test(struct kunit *test) @@ -4117,6 +4269,7 @@ static void apple_clpc_all_core_and_e_cap_test(struct kunit *test) }; struct cpufreq_frequency_table p_tbl[] = { { .frequency = 1260000 }, + { .frequency = 3096000 }, { .frequency = 3936000 }, { .frequency = 4464000 }, { .frequency = CPUFREQ_TABLE_END }, @@ -4134,21 +4287,32 @@ static void apple_clpc_all_core_and_e_cap_test(struct kunit *test) KUNIT_ASSERT_NOT_NULL(test, clpc); clpc->nr_clusters = 2; - clpc->all_core_khz[0] = 3936000; - clpc->all_core_khz[1] = 2808000; - clpc->e_cap_khz = 2808000; - /* All-core ceiling applies only when >= 2 online CPUs are held busy. */ + /* OPP validation accepts real OPPs and rejects non-OPP frequencies. */ + KUNIT_EXPECT_EQ(test, apple_clpc_validate_opp_khz(NULL, NULL, "test", &p_pol, 3936000), + 3936000u); + KUNIT_EXPECT_EQ(test, apple_clpc_validate_opp_khz(NULL, NULL, "test", &p_pol, 3900000), + 0u); + KUNIT_EXPECT_EQ(test, apple_clpc_validate_opp_khz(NULL, NULL, "test", &e_pol, 2808000), + 2808000u); + KUNIT_EXPECT_EQ(test, apple_clpc_validate_opp_khz(NULL, NULL, "test", &p_pol, 2808000), + 0u); + + /* J604 binding: P has all-core 3936 MHz; E has slow-cluster busy cap 2808 MHz only. */ + clpc->clusters[0].slow_busy_khz = 2808000; + clpc->clusters[1].all_core_khz = 3936000; clpc->clusters[0].win_online = 6; clpc->clusters[1].win_online = 4; - KUNIT_EXPECT_EQ(test, apple_clpc_all_core_khz(clpc, &clpc->clusters[1], &p_pol), 0u); + + /* All-core ceiling applies to P only when all P-cores are held busy. */ + KUNIT_EXPECT_EQ(test, apple_clpc_all_core_khz(&clpc->clusters[1]), 0u); clpc->clusters[1].all_core_held = true; - KUNIT_EXPECT_EQ(test, apple_clpc_all_core_khz(clpc, &clpc->clusters[1], &p_pol), 3936000u); - clpc->clusters[0].all_core_held = true; - KUNIT_EXPECT_EQ(test, apple_clpc_all_core_khz(clpc, &clpc->clusters[0], &e_pol), 2808000u); + KUNIT_EXPECT_EQ(test, apple_clpc_all_core_khz(&clpc->clusters[1]), 3936000u); - /* E-cluster stays uncapped for E-only work (P-cluster idle). */ - clpc->clusters[0].max_khz = 0; + /* All 6 E-cores busy with P idle stays completely uncapped (E-only untouched). */ + clpc->clusters[0].all_core_held = true; + clpc->clusters[0].max_khz = apple_clpc_all_core_khz(&clpc->clusters[0]); + KUNIT_EXPECT_EQ(test, clpc->clusters[0].max_khz, 0u); clpc->clusters[1].all_core_held = false; clpc->clusters[1].win_nbusy = 0; clpc->clusters[1].busy = 10; From 5a4e1824503e74a4495409d280228b01b88ff33a Mon Sep 17 00:00:00 2001 From: Dylan Ironside Date: Wed, 2 Dec 2026 19:05:39 -0600 Subject: [PATCH 6/8] arm64: dts: apple: t8132-j604: use per-cluster CLPC limits and CPU rails Update J604 CLPC limits to address review feedback: - Place the P-cluster all-core ceiling (3936 MHz) and sustained floor (3096 MHz) on &cpufreq_p, and the slow-cluster busy cap (2808 MHz) only on &cpufreq_e so 6-thread E-only work stays uncapped while P is idle. - Regulate sustained CPU power at 13.5 W summed across the CPU cluster rails (PP0b P-cluster + PPbb E-cluster, excluding GPU PP1b and display). - Clarify that these limits apply outside low-power (balanced and performance, AC and battery; J604 has no High Power mode) and that wiring apple,smc on &clpc also enables the existing low-power SMC PHPC loop when the boot loader supplies Low Power mode values. Signed-off-by: Dylan Ironside --- arch/arm64/boot/dts/apple/t8132-j604.dts | 28 ++++++++++++++++++------ 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/arch/arm64/boot/dts/apple/t8132-j604.dts b/arch/arm64/boot/dts/apple/t8132-j604.dts index 7553f2c3fef14..81cecc885081d 100644 --- a/arch/arm64/boot/dts/apple/t8132-j604.dts +++ b/arch/arm64/boot/dts/apple/t8132-j604.dts @@ -687,14 +687,28 @@ }; /* - * All-core and sustained package power limits matching macOS on AC: - * P-cluster all-core ceiling 3936 MHz, E-cluster ceiling 2808 MHz - * (avoiding the 2892 MHz power cliff while P-cores are busy), and - * 15.5 W sustained all-core PHPC ceiling. + * J604 CLPC per-cluster all-core and sustained CPU-rail limits (applied + * outside the low-power platform profile, i.e. balanced and performance on + * both AC and battery; J604 has no High Power mode): + * - P-cluster all-core ceiling 3936 MHz, sustained floor 3096 MHz. + * - E-cluster ceiling 2808 MHz only while P-cores are busy (avoiding the + * 2892 MHz power cliff while leaving E-only workloads uncapped). + * - Sustained CPU power target 13.5 W summed across the CPU cluster rails + * (PP0b P-cluster + PPbb E-cluster, excluding GPU PP1b and display). + * - Wiring apple,smc also enables the existing low-power SMC PHPC power + * budgeting loop whenever the boot loader passes Low Power mode values. */ +&cpufreq_e { + apple,slow-cluster-busy-max-mhz = <2808>; +}; + +&cpufreq_p { + apple,all-core-frequency-max-mhz = <3936>; + apple,sustained-frequency-min-mhz = <3096>; +}; + &clpc { apple,smc = <&smc>; - apple,all-core-frequency-max-mhz = <3936 2808>; - apple,slow-cluster-busy-max-mhz = <2808>; - apple,sustained-power-mw = <15500>; + apple,sustained-power-key-ids = "PP0b", "PPbb"; + apple,sustained-power-mw = <13500>; }; From 6dc15236ba6e03249ca09e7eff299978ee94423d Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Wed, 2 Dec 2026 20:58:50 -0500 Subject: [PATCH 7/8] soc: apple: clpc: sample on time while a board ceiling is held The all-core, slow-cluster and sustained ceilings drop only when the sampler runs. 32 ms after the last CPU demand the memory floor is released and the T8132 sampler turns deferrable, so it runs again only when the CPU that owns the timer takes a tick. The all-core hold is 200 ms: once the load stops, the ceilings outlive it until that CPU wakes, and a thread that starts on another CPU meanwhile runs at the all-core ceiling, or down to the sustained floor, for that long. Keep sampling on time while a ceiling is held, as for a held vote. It drops at most two 100 ms windows later and the sampler is deferrable again. After a sleep take the first sample on time as well: the reset clears the recorded ceiling but not the frequency QoS request. Boards without the properties sample as before. The host test runs apple_clpc_sample() with a held ceiling and fails without the check. Signed-off-by: DjDeveloperr --- drivers/soc/apple/clpc.c | 40 ++++++++++++++++++++++++---- tools/asahi/clpc-gpu-demand-tests.py | 24 ++++++++++++++++- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/drivers/soc/apple/clpc.c b/drivers/soc/apple/clpc.c index f3f284aa2a13c..75908d4d967fb 100644 --- a/drivers/soc/apple/clpc.c +++ b/drivers/soc/apple/clpc.c @@ -1056,6 +1056,23 @@ static bool apple_clpc_power_limited(const struct apple_clpc *clpc) READ_ONCE(clpc->low_power); } +/* + * Whether a ceiling of the board's DT limits is held. It has to be sampled on + * time, as a held vote, so that it drops once the load that set it stops: a + * deferrable sample could leave it on until an idle CPU wakes. + */ +static bool apple_clpc_board_limited(const struct apple_clpc *clpc) +{ + unsigned int i; + + if (!clpc->has_all_core || READ_ONCE(clpc->low_power)) + return false; + for (i = 0; i < clpc->nr_clusters; i++) + if (clpc->clusters[i].max_khz) + return true; + return false; +} + static void apple_clpc_queue(struct apple_clpc *clpc, bool idle) { if (READ_ONCE(clpc->stopped)) @@ -1864,13 +1881,15 @@ static void apple_clpc_sample(struct apple_clpc *clpc) apple_clpc_profile_limit(clpc); /* - * Sample on time while graphics is powered, to see it power off, and in - * low power when an SMC package power target is active. A static - * ceiling is applied synchronously on profile change and a High Power - * only profile has nothing to poll at idle, so both stay deferrable. + * Sample on time while graphics is powered, to see it power off, while + * a board ceiling is held, and in low power when an SMC package power + * target is active. A static ceiling is applied synchronously on profile + * change and a High Power only profile has nothing to poll at idle, so + * both stay deferrable. */ apple_clpc_queue(clpc, !floor && !clpc->gpu_on && !clpc->ane_qos_khz && + !apple_clpc_board_limited(clpc) && !apple_clpc_power_limited(clpc)); } @@ -2964,7 +2983,8 @@ static int apple_clpc_resume(struct device *dev) WRITE_ONCE(clpc->gpu->cap_mw, clpc->low_power_mw); clpc->power_next = jiffies; WRITE_ONCE(clpc->stopped, false); - apple_clpc_queue(clpc, true); + /* A board ceiling held into the sleep drops at the first sample. */ + apple_clpc_queue(clpc, !clpc->has_all_core); mutex_unlock(&clpc->sample_lock); return 0; } @@ -4328,6 +4348,16 @@ static void apple_clpc_all_core_and_e_cap_test(struct kunit *test) clpc->clusters[0].max_khz = 2000000; apple_clpc_e_cap(clpc, policies); KUNIT_EXPECT_EQ(test, clpc->clusters[0].max_khz, 2000000u); + + /* A held ceiling is sampled on time until it drops; low power has its own rule. */ + KUNIT_EXPECT_FALSE(test, apple_clpc_board_limited(clpc)); + clpc->has_all_core = true; + KUNIT_EXPECT_TRUE(test, apple_clpc_board_limited(clpc)); + clpc->low_power = true; + KUNIT_EXPECT_FALSE(test, apple_clpc_board_limited(clpc)); + clpc->low_power = false; + clpc->clusters[0].max_khz = 0; + KUNIT_EXPECT_FALSE(test, apple_clpc_board_limited(clpc)); } static void apple_clpc_t6050_gpu_fabric_test(struct kunit *test) diff --git a/tools/asahi/clpc-gpu-demand-tests.py b/tools/asahi/clpc-gpu-demand-tests.py index 37f0c99987edc..bc5f9784ffa9b 100644 --- a/tools/asahi/clpc-gpu-demand-tests.py +++ b/tools/asahi/clpc-gpu-demand-tests.py @@ -362,6 +362,7 @@ def main(): 'static bool apple_clpc_ane_input_demand(', 'static void apple_clpc_ane_cluster(', 'static bool apple_clpc_power_limited(', + 'static bool apple_clpc_board_limited(', 'static void apple_clpc_sample(']) code = r''' #include @@ -406,7 +407,7 @@ def main(): struct apple_clpc_ane { int lock; bool active, pending; }; #define spin_lock_irqsave(l, f) ((void)(l), (f) = 0) #define spin_unlock_irqrestore(l, f) ((void)(l), (void)(f)) -struct apple_clpc_cluster { unsigned int request[APPLE_CLPC_RAILS]; }; +struct apple_clpc_cluster { unsigned int request[APPLE_CLPC_RAILS]; unsigned int max_khz; }; // cpufreq QoS stand-ins: one policy, the last requested minimum is recorded. #define FREQ_QOS_MIN 1 #define FREQ_QOS_MIN_DEFAULT_VALUE 0 @@ -447,6 +448,7 @@ def main(): unsigned long ane_until; bool has_pmc; bool low_power; + bool has_all_core; unsigned int low_power_khz; unsigned int low_power_mw; void *smc_np; @@ -1062,6 +1064,23 @@ def main(): jiffies=3012; apple_clpc_sample(&c); assert(published==0 && queued_idle); } +// A held board ceiling is sampled on time until it drops, so that it does not +// outlive its load; without the board properties a ceiling changes nothing. +static void check_board_ceiling(void) { + struct apple_clpc_gpu gpu={0}; + struct apple_clpc c={.hw=&apple_clpc_hw_t8132,.has_pmc=true,.agent=1,.mask=3,.gpu=&gpu,.nr_clusters=1}; + read_error=vote_error=0; pmgr_status=0xf0; published=~0u; + c.clusters[0].max_khz=3936000; + jiffies=3000; apple_clpc_sample(&c); assert(published==0 && queued_idle); + c.has_all_core=true; + jiffies=3004; apple_clpc_sample(&c); assert(published==0 && !queued_idle); + c.clusters[0].max_khz=0; + jiffies=3008; apple_clpc_sample(&c); assert(published==0 && queued_idle); + // The low-power profile's static ceiling stays deferrable. + c.clusters[0].max_khz=2220000; c.low_power=true; c.low_power_khz=2220000; + jiffies=3012; apple_clpc_sample(&c); assert(published==0 && queued_idle); +} + int main(void) { // T6040's supplier is always powered: it must NEVER substitute for work. struct apple_clpc_gpu gpu={0}; @@ -1107,6 +1126,7 @@ def main(): check_t6050_ane(); check_t8152_ane(); check_low_power(); + check_board_ceiling(); check_t6040_ane(); puts("PASS: T6040, T6041 and T8132 host demand; DT GPU busy share and down-step hold; T8140/T604x ANE, streaming, dense work, read errors and PMP backoff; idle release"); } @@ -1142,6 +1162,8 @@ def main(): '\tgpu->active = active;\n\tif (!active)\n\t\tgpu->busy_valid = false;\n')), ('gpu-busy-without-dt', code.replace('bool has_busy = hw->gpu_busy[APPLE_CLPC_DCS][APPLE_CLPC_LEVELS - 1];', 'bool has_busy = true;')), + ('board-ceiling-deferrable', code.replace('!apple_clpc_board_limited(clpc) &&', + '(apple_clpc_board_limited(clpc) || true) &&')), ('retain-at-idle', code.replace('vote->state = 0;', '')), ('dt-state-unchecked', code.replace('if (state > top || state > 15)', 'if ((void)top, false)')), ('dt-rail-bound-unchecked', code.replace('if (state > top || state > 15)', 'if ((void)top, state > 15)')), From 136204e3ee9b50102df2e210057a63e4bf6608e4 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Wed, 2 Dec 2026 20:58:59 -0500 Subject: [PATCH 8/8] soc: apple: clpc: look up the SMC and its power key in one block The nested condition guards settings that nothing reads without a low-power ceiling or High Power mode, so boards do not need it. Keep the block as it was and only add the sustained limit's keys to its condition. This also drops a misindented store that the DT setup has already done. The ceilings' comment still said the sustained limit reads the package power; it reads the CPU rails. Signed-off-by: DjDeveloperr --- drivers/soc/apple/clpc.c | 44 +++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/drivers/soc/apple/clpc.c b/drivers/soc/apple/clpc.c index 75908d4d967fb..3847ccc72bb47 100644 --- a/drivers/soc/apple/clpc.c +++ b/drivers/soc/apple/clpc.c @@ -1660,7 +1660,7 @@ static void apple_clpc_e_cap(struct apple_clpc *clpc, * cap. Outside the low-power profile, optional board DT ceilings apply while * all online CPUs of a cluster are busy across the 100 ms window (with 200 ms * release hysteresis), while a faster cluster is busy (slow-cluster cap), and - * while all-core load holds a faster cluster (sustained SMC package power). + * while all-core load holds a faster cluster (sustained CPU rail power). */ static void apple_clpc_profile_limit(struct apple_clpc *clpc) { @@ -2683,8 +2683,7 @@ static int apple_clpc_probe(struct platform_device *pdev) if (!high_power && clpc->hw->high_power_machine) high_power = of_machine_is_compatible(clpc->hw->high_power_machine); if (((clpc->low_power_khz || high_power) && - IS_REACHABLE(CONFIG_ACPI_PLATFORM_PROFILE)) || - (clpc->sustained_mw && clpc->nr_sustained_keys)) { + IS_REACHABLE(CONFIG_ACPI_PLATFORM_PROFILE)) || clpc->nr_sustained_keys) { /* * The SMC is optional: without it (no apple,smc phandle, or a * kernel without the SMC driver) the low-power profile has only @@ -2699,29 +2698,24 @@ static int apple_clpc_probe(struct platform_device *pdev) return ret; } } - clpc->power_mw = -1; - clpc->sustained_power_mw = -1; - if ((clpc->low_power_khz || high_power) && - IS_REACHABLE(CONFIG_ACPI_PLATFORM_PROFILE)) { - /* - * The SMC key whose power reading the low-power profile - * holds at its target: the board's, else PHPC. - */ - if (!of_property_read_string(dev->of_node, "apple,power-key-id", &key)) { - if (strlen(key) != 4) - return dev_err_probe(dev, -EINVAL, - "invalid apple,power-key-id \"%s\"\n", - key); - clpc->power_key = _SMC_KEY(key); - } else { - clpc->power_key = SMC_KEY(PHPC); - } - clpc->gpu_power_cap = of_property_read_bool(dev->of_node, - "apple,low-power-gpu-cap"); - clpc->high_power = clpc->smc_np && high_power; - clpc->low_power_notice = clpc->smc_np && clpc->low_power_khz && - of_property_read_bool(dev->of_node, "apple,low-power-smc-notice"); + /* + * The SMC key whose power reading the low-power profile holds + * at its target: the board's, else PHPC. + */ + if (!of_property_read_string(dev->of_node, "apple,power-key-id", &key)) { + if (strlen(key) != 4) + return dev_err_probe(dev, -EINVAL, + "invalid apple,power-key-id \"%s\"\n", key); + clpc->power_key = _SMC_KEY(key); + } else { + clpc->power_key = SMC_KEY(PHPC); } + clpc->gpu_power_cap = of_property_read_bool(dev->of_node, + "apple,low-power-gpu-cap"); + clpc->power_mw = -1; + clpc->high_power = clpc->smc_np && high_power; + clpc->low_power_notice = clpc->smc_np && clpc->low_power_khz && + of_property_read_bool(dev->of_node, "apple,low-power-smc-notice"); } /* High Power mode alone needs the SMC; without it there is no profile. */ has_profile = (clpc->low_power_khz || clpc->high_power) &&