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 8d2601e1599c5..3c138468fa85d 100644 --- a/Documentation/devicetree/bindings/power/apple,pmc.yaml +++ b/Documentation/devicetree/bindings/power/apple,pmc.yaml @@ -283,9 +283,12 @@ patternProperties: apple,smc: $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. + 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 @@ -310,6 +313,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,sustained-power-key-ids: + $ref: /schemas/types.yaml#/definitions/string-array + minItems: 1 + maxItems: 4 + items: + pattern: "^[A-Za-z0-9#!]{4}$" + description: + 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 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 pattern: "^[A-Za-z0-9#!]{4}$" @@ -385,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 diff --git a/arch/arm64/boot/dts/apple/t8132-j604.dts b/arch/arm64/boot/dts/apple/t8132-j604.dts index 8019dd4493dea..81cecc885081d 100644 --- a/arch/arm64/boot/dts/apple/t8132-j604.dts +++ b/arch/arm64/boot/dts/apple/t8132-j604.dts @@ -685,3 +685,30 @@ &aop_spmi1 { status = "okay"; }; + +/* + * 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,sustained-power-key-ids = "PP0b", "PPbb"; + apple,sustained-power-mw = <13500>; +}; diff --git a/drivers/soc/apple/clpc.c b/drivers/soc/apple/clpc.c index 296830c01b2b0..3847ccc72bb47 100644 --- a/drivers/soc/apple/clpc.c +++ b/drivers/soc/apple/clpc.c @@ -74,6 +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 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 +#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, @@ -177,6 +194,15 @@ 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; + /* 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]; @@ -328,6 +354,14 @@ struct apple_clpc { /* Per-CPU baselines for power_busy, taken at each package power reading */ u64 *power_idle; u64 *power_wall; + /* 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; }; /* Protect publication/removal of provider drvdata, never a submission path. */ @@ -663,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) { @@ -683,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]; @@ -690,6 +798,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); } @@ -945,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)) @@ -1222,8 +1350,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; @@ -1231,7 +1359,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; @@ -1244,6 +1372,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 @@ -1348,27 +1500,183 @@ static int apple_clpc_pick_profile_step(struct apple_clpc *clpc, return pick; } +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_cluster *cl) +{ + if (cl->win_online < 2 || !cl->all_core_held) + return 0; + return cl->all_core_khz; +} + +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) + 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 (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; +} + /* - * 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. + * 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 i, j; + + if (clpc->nr_clusters < 2) + return; + for (i = 0; i < clpc->nr_clusters; i++) { + struct apple_clpc_cluster *cl = &clpc->clusters[i]; + bool faster_busy = false; + + 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; + } +} + +/* + * 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 CPU rail 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++; @@ -1387,6 +1695,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); @@ -1405,17 +1715,85 @@ 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(cl); + 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->sustained_power_mw = -1; + 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->nr_sustained_keys && + clpc->smc_np && (sustained_top[i] || cl->power_khz)) + any_sustained = true; + } + if (any_sustained) { + mw = apple_clpc_read_sustained_power(clpc); + clpc->power_reads++; + if (mw >= 0 && mw != clpc->sustained_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->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], + floor, 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->sustained_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]; @@ -1503,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)); } @@ -1624,10 +2004,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); @@ -1645,12 +2025,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) + 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->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", @@ -1862,6 +2253,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; @@ -2161,6 +2554,73 @@ 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; + struct device_node *np = dev->of_node; + const char *key; + u32 mw; + int i, n; + + 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 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 (of_property_read_string_index(np, + "apple,sustained-power-key-ids", + i, &key) || + strlen(key) != 4) { + dev_warn(dev, + "ignoring invalid apple,sustained-power-key-ids[%d]\n", + i); + valid = false; + break; + } + clpc->sustained_keys[i] = _SMC_KEY(key); + } + if (valid) { + clpc->nr_sustained_keys = n; + clpc->sustained_mw = 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) { struct apple_clpc *clpc = data; @@ -2207,6 +2667,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). @@ -2221,12 +2682,13 @@ 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->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 and there is no High Power mode. + * its ceiling, there is no High Power mode, and sustained + * CPU-rail power stepping stays inactive. */ if (IS_ENABLED(CONFIG_MFD_MACSMC)) { clpc->smc_np = of_parse_phandle(dev->of_node, "apple,smc", 0); @@ -2349,6 +2811,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)) { @@ -2434,11 +2906,11 @@ 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) + apple_clpc_policy_nb_register(clpc); if (has_profile) { struct device *ppdev; - if (clpc->low_power_khz) - apple_clpc_policy_nb_register(clpc); /* * "apple-clpc" budgets the low-power profile itself; "apple-smc" * only carries High Power mode, and userspace keeps the CPU budget. @@ -2486,19 +2958,27 @@ 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++) + 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; + 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; 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; } @@ -3726,6 +4206,154 @@ 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 = 2616000 }, + { .frequency = 3096000 }, + { .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, 3096000, -1); + KUNIT_EXPECT_EQ(test, khz, 3876000u); + khz = apple_clpc_sustained_step(&pol, khz, 3936000, 3096000, -1); + KUNIT_EXPECT_EQ(test, khz, 3756000u); + 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, 3096000, 0), 3624000u); + + /* 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 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, 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, 3096000, 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 = 3096000 }, + { .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; + + /* 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; + + /* 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->clusters[1]), 3936000u); + + /* 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; + 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); + + /* 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) { static const u32 ladder[APPLE_CLPC_RAILS * APPLE_CLPC_LEVELS] = { @@ -3795,6 +4423,9 @@ static struct kunit_case apple_clpc_policy_cases[] = { KUNIT_CASE(apple_clpc_profile_step_busy_test), KUNIT_CASE(apple_clpc_gpu_busy_clear_test), KUNIT_CASE(apple_clpc_t6050_gpu_fabric_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 = { 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)')),