diff --git a/Documentation/devicetree/bindings/power/apple,pmc.yaml b/Documentation/devicetree/bindings/power/apple,pmc.yaml index 289eca84cd87e..8d2601e1599c5 100644 --- a/Documentation/devicetree/bindings/power/apple,pmc.yaml +++ b/Documentation/devicetree/bindings/power/apple,pmc.yaml @@ -325,6 +325,12 @@ patternProperties: budget, and takes its steps there first while the GPU is busy, before the CPU clusters' ceilings. + apple,low-power-smc-notice: + type: boolean + description: + The low-power platform profile tells the SMC (apple,smc) that Low + Power Mode is on, in the record that carries High Power mode. + apple,gpu-busy-memory-states: $ref: /schemas/types.yaml#/definitions/uint32-matrix description: diff --git a/Documentation/devicetree/bindings/soc/apple/apple,clpc.yaml b/Documentation/devicetree/bindings/soc/apple/apple,clpc.yaml index 649928d812c6e..061f1ccf77c43 100644 --- a/Documentation/devicetree/bindings/soc/apple/apple,clpc.yaml +++ b/Documentation/devicetree/bindings/soc/apple/apple,clpc.yaml @@ -73,6 +73,12 @@ properties: and takes its steps there first while the GPU is busy, before the CPU clusters' ceilings. + apple,low-power-smc-notice: + type: boolean + description: + The low-power platform profile tells the SMC (apple,smc) that Low Power + Mode is on, in the record that carries High Power mode. + required: - compatible diff --git a/arch/arm64/boot/dts/apple/t8142-j813-j815.dtsi b/arch/arm64/boot/dts/apple/t8142-j813-j815.dtsi index 134cbfefc6428..d3b4368d6732c 100644 --- a/arch/arm64/boot/dts/apple/t8142-j813-j815.dtsi +++ b/arch/arm64/boot/dts/apple/t8142-j813-j815.dtsi @@ -253,6 +253,8 @@ apple,smc = <&smc>; apple,power-key-id = "PPSM"; apple,low-power-gpu-cap; + /* Tell the SMC when the low-power platform profile is on. */ + apple,low-power-smc-notice; /* * T8142 GPU busy-share memory-state ladder (shared by * J813/J815 M5 MacBook Air): DCS 1, 2, 5, 7 and fabric diff --git a/drivers/soc/apple/clpc.c b/drivers/soc/apple/clpc.c index d5f98f484b796..80655e21ffc55 100644 --- a/drivers/soc/apple/clpc.c +++ b/drivers/soc/apple/clpc.c @@ -316,8 +316,12 @@ struct apple_clpc { bool gpu_power_cap; /* The performance profile is sent to the SMC as High Power mode */ bool high_power; - u8 high_power_seq; + /* apple,low-power-smc-notice: the low-power profile is sent to the SMC too */ + bool low_power_notice; + /* zETM records written, and what the last one that the SMC took said */ + u8 zetm_seq; bool high_power_sent; + bool low_power_sent; unsigned long power_next; unsigned long power_reads, power_updates; int power_mw; @@ -1647,9 +1651,10 @@ static int apple_clpc_status_show(struct seq_file *s, void *unused) clpc->low_power_mw, clpc->gpu ? READ_ONCE(clpc->gpu->cap_mw) : 0); } - if (clpc->high_power) - seq_printf(s, "smc high power: %s (%u records sent)\n", - clpc->high_power_sent ? "on" : "off", clpc->high_power_seq); + if (clpc->high_power || clpc->low_power_notice) + seq_printf(s, "smc power mode: high power %s, low power %s (%u records sent)\n", + clpc->high_power_sent ? "on" : "off", + clpc->low_power_sent ? "on" : "off", clpc->zetm_seq); if (clpc->gpu_pmgr) { seq_printf(s, "gpu: powered %u for %u ms, pmp limited ", clpc->gpu_on, clpc->gpu_on ? jiffies_to_msecs(jiffies - clpc->gpu_on_since) : 0); @@ -1782,7 +1787,7 @@ static void apple_clpc_halt(struct apple_clpc *clpc) clpc->ane_until = jiffies; } -static int apple_clpc_send_high_power(struct apple_clpc *clpc, bool on); +static int apple_clpc_send_mode(struct apple_clpc *clpc, bool low, bool high); static void apple_clpc_stop(void *data) { @@ -1817,8 +1822,9 @@ static void apple_clpc_stop(void *data) WRITE_ONCE(clpc->gpu->cap_mw, 0); /* The SMC keeps this mode after the driver's profile state is gone. */ mutex_lock(&clpc->sample_lock); - if (clpc->high_power && clpc->high_power_sent) - apple_clpc_send_high_power(clpc, false); + if ((clpc->high_power && clpc->high_power_sent) || + (clpc->low_power_notice && clpc->low_power_sent)) + apple_clpc_send_mode(clpc, false, false); mutex_unlock(&clpc->sample_lock); if (clpc->policy_nb_added) cpufreq_unregister_notifier(&clpc->policy_nb, CPUFREQ_POLICY_NOTIFIER); @@ -1948,32 +1954,38 @@ static int apple_clpc_cluster_cpu(unsigned int index) /* * macOS tells the SMC its power mode through thermalmonitord's 120-byte zETM * record, written at every mode change: a record type (0x1d), a version (1), a - * count of the writes, zeros, and at byte 112 a float that is 1.0 while High - * Power mode is on and 0.0 otherwise. With it on, the SMC lets the fans run up - * to their maximum under a sustained load; in Automatic they level off well - * below it, whatever the temperature. No other SMC key changes with the mode. + * count of the writes, zeros, and two floats that are 1.0 while their mode is + * on and 0.0 otherwise: at byte 76 Low Power Mode, at byte 112 High Power + * mode. With High Power on, the SMC lets the fans run up to their maximum + * under a sustained load; in Automatic they level off well below it, whatever + * the temperature. Each record states the whole mode, so one write carries + * both. No other SMC key changes with the mode. */ #define APPLE_CLPC_ZETM_SIZE 120 #define APPLE_CLPC_ZETM_TYPE 0x1d #define APPLE_CLPC_ZETM_VERSION 1 +#define APPLE_CLPC_ZETM_LOW_POWER 76 #define APPLE_CLPC_ZETM_HIGH_POWER 112 #define APPLE_CLPC_FLOAT_ONE 0x3f800000 -static int apple_clpc_send_high_power(struct apple_clpc *clpc, bool on) +static int apple_clpc_send_mode(struct apple_clpc *clpc, bool low, bool high) { u8 rec[APPLE_CLPC_ZETM_SIZE] = { APPLE_CLPC_ZETM_TYPE, APPLE_CLPC_ZETM_VERSION }; int ret; if (!IS_ENABLED(CONFIG_MFD_MACSMC) || !apple_clpc_smc(clpc)) return -ENODEV; - rec[2] = ++clpc->high_power_seq; - put_unaligned_le32(on ? APPLE_CLPC_FLOAT_ONE : 0, rec + APPLE_CLPC_ZETM_HIGH_POWER); + rec[2] = ++clpc->zetm_seq; + put_unaligned_le32(low ? APPLE_CLPC_FLOAT_ONE : 0, rec + APPLE_CLPC_ZETM_LOW_POWER); + put_unaligned_le32(high ? APPLE_CLPC_FLOAT_ONE : 0, rec + APPLE_CLPC_ZETM_HIGH_POWER); ret = apple_smc_write(clpc->smc, SMC_KEY(zETM), rec, sizeof(rec)); - if (ret < 0) - dev_warn(clpc->dev, "High Power mode %s not sent to the SMC: %d\n", - on ? "on" : "off", ret); - else - clpc->high_power_sent = on; + if (ret < 0) { + dev_warn(clpc->dev, "power mode (low power %s, High Power %s) not sent to the SMC: %d\n", + low ? "on" : "off", high ? "on" : "off", ret); + } else { + clpc->low_power_sent = low; + clpc->high_power_sent = high; + } return ret < 0 ? ret : 0; } @@ -1996,15 +2008,21 @@ static int apple_clpc_profile_get(struct device *dev, enum platform_profile_opti static int apple_clpc_profile_set(struct device *dev, enum platform_profile_option profile) { struct apple_clpc *clpc = dev_get_drvdata(dev); - bool high_power = profile == PLATFORM_PROFILE_PERFORMANCE; + bool high = clpc->high_power && profile == PLATFORM_PROFILE_PERFORMANCE; + bool low = clpc->low_power_notice && profile == PLATFORM_PROFILE_LOW_POWER; bool need_work; mutex_lock(&clpc->sample_lock); - /* Performance is macOS's High Power mode; the other two are its Automatic. */ - if (clpc->high_power && high_power != clpc->high_power_sent) { - int ret = apple_clpc_send_high_power(clpc, high_power); + /* + * Performance is macOS's High Power mode, low-power its Low Power Mode, + * balanced its Automatic. High Power mode is the SMC's alone, so without + * the write the profile change fails; the Low Power notice is advisory, + * and the profile's ceilings and limit apply without it. + */ + if (high != clpc->high_power_sent || low != clpc->low_power_sent) { + int ret = apple_clpc_send_mode(clpc, low, high); - if (ret) { + if (ret && high != clpc->high_power_sent) { mutex_unlock(&clpc->sample_lock); return ret; } @@ -2233,6 +2251,8 @@ static int apple_clpc_probe(struct platform_device *pdev) "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) && diff --git a/tools/asahi/clpc-high-power-cleanup-tests.py b/tools/asahi/clpc-high-power-cleanup-tests.py index f89201ab78f28..7c44f4ec6ae71 100755 --- a/tools/asahi/clpc-high-power-cleanup-tests.py +++ b/tools/asahi/clpc-high-power-cleanup-tests.py @@ -79,7 +79,9 @@ def function(source, name): struct hlist_node cpuhp_node; atomic_t idle_armed; bool has_pmc, stopped, high_power, high_power_sent, policy_nb_added, low_power, cpuhp_added; - u8 high_power_seq; + bool low_power_notice, low_power_sent; + /* the record counter: high_power_seq before the Low Power notice, zetm_seq after */ + u8 high_power_seq, zetm_seq; enum platform_profile_option profile; unsigned long power_next, ane_until; unsigned low_power_khz, low_power_mw, ane_qos_khz, nr_clusters, agent, mask, saved_floor; @@ -184,13 +186,15 @@ def function(source, name): detached = debug_removed = floor_restored = notifier_removed = node_put = freed = cleanup = false; smc_result = 0; memset(last_record, 0xff, sizeof(last_record)); } -static void record(bool on, unsigned seq) { +static void record2(bool low, bool on, unsigned seq) { for (unsigned i = 0; i < sizeof(last_record); i++) { unsigned expected = i == 0 ? 0x1d : i == 1 ? 1 : i == 2 ? seq : - on && i == 114 ? 0x80 : on && i == 115 ? 0x3f : 0; + on && i == 114 ? 0x80 : on && i == 115 ? 0x3f : + low && i == 78 ? 0x80 : low && i == 79 ? 0x3f : 0; assert(last_record[i] == expected); } } +static void record(bool on, unsigned seq) { record2(false, on, seq); } static void stop(void) { cleanup = true; apple_clpc_stop(&c); assert(freed && notifier_removed && qos_removed == 3); @@ -231,13 +235,40 @@ def function(source, name): smc_result = -EIO; stop(); assert(smc_writes == 2 && c.high_power_sent && warnings == 1); record(false, 2); reset(); c.high_power_sent = true; c.smc = NULL; - stop(); assert(!smc_writes && c.high_power_sent && !c.high_power_seq); + stop(); assert(!smc_writes && c.high_power_sent && !c.SEQ); /* The record sequence is an eight-bit counter, including during cleanup. */ - reset(); c.high_power_sent = true; c.high_power_seq = 255; + reset(); c.high_power_sent = true; c.SEQ = 255; stop(); record(false, 0); assert(!c.high_power_sent); /* The GPU's reference outlives the device: stop drops a held low-power cap. */ reset(); c.low_power = true; gpu.cap_mw = 1250; stop(); assert(gpu.cap_mw == 0); +#ifdef LOW_POWER_NOTICE + /* The Low Power notice: byte 76 in the same record, advisory, and cleared on removal. */ + reset(); c.low_power_notice = true; c.low_power_khz = 2220000; + assert(!apple_clpc_profile_set(&dev, PLATFORM_PROFILE_LOW_POWER)); + assert(c.low_power_sent && !c.high_power_sent && c.low_power); record2(true, false, 1); + /* Low power straight to performance: one record, low off and High Power on. */ + assert(!apple_clpc_profile_set(&dev, PLATFORM_PROFILE_PERFORMANCE)); + assert(!c.low_power_sent && c.high_power_sent && smc_writes == 2); record2(false, true, 2); + assert(!apple_clpc_profile_set(&dev, PLATFORM_PROFILE_LOW_POWER)); record2(true, false, 3); + stop(); assert(smc_writes == 4 && !c.low_power_sent); record(false, 4); + /* A failed notice still applies the profile and leaves nothing to withdraw. */ + reset(); c.low_power_notice = true; c.low_power_khz = 2220000; smc_result = -EIO; + assert(!apple_clpc_profile_set(&dev, PLATFORM_PROFILE_LOW_POWER)); + assert(c.low_power && c.profile == PLATFORM_PROFILE_LOW_POWER && !c.low_power_sent); + assert(warnings == 1 && queued == 1); + stop(); assert(smc_writes == 1); + /* A failed off-notice still leaves low power; the SMC keeps the flag, so removal withdraws it. */ + reset(); c.low_power_notice = true; c.low_power_khz = 2220000; + assert(!apple_clpc_profile_set(&dev, PLATFORM_PROFILE_LOW_POWER)); smc_result = -EIO; + assert(!apple_clpc_profile_set(&dev, PLATFORM_PROFILE_BALANCED)); + assert(!c.low_power && c.profile == PLATFORM_PROFILE_BALANCED && c.low_power_sent && warnings == 1); + smc_result = 0; stop(); assert(smc_writes == 3 && !c.low_power_sent); record(false, 3); + /* Without the DT opt-in, the low-power profile writes nothing. */ + reset(); assert(!apple_clpc_profile_set(&dev, PLATFORM_PROFILE_LOW_POWER)); + assert(!smc_writes && c.low_power); + stop(); assert(!smc_writes); +#endif puts("CLPC High Power: profile state, cleanup ordering, zETM off, SMC failures and GPU cap passed"); return 0; } @@ -256,9 +287,16 @@ def main(): assert source.index('devm_add_action_or_reset(dev, apple_clpc_stop, clpc)') < source.index( 'devm_platform_profile_register(dev,') defines = '\n'.join(re.findall(r'^#define APPLE_CLPC_(?:ZETM_\w+|FLOAT_ONE)\s+[^\n]+', source, re.M)) + # Older sources have one High Power writer; newer ones one writer for the whole mode. + notice = 'static int apple_clpc_send_mode(' in source + if notice: + defines += '\n#define LOW_POWER_NOTICE 1\n#define SEQ zetm_seq' + else: + defines += '\n#define SEQ high_power_seq' # Older sources (--old-ref) have no apple_clpc_power_limited(); newer profile_set calls it. names = (['apple_clpc_power_limited'] if 'static bool apple_clpc_power_limited(' in source else []) + [ - 'apple_clpc_send_high_power', 'apple_clpc_profile_set', 'apple_clpc_halt', 'apple_clpc_stop'] + 'apple_clpc_send_mode' if notice else 'apple_clpc_send_high_power', + 'apple_clpc_profile_set', 'apple_clpc_halt', 'apple_clpc_stop'] body = '\n\n'.join(function(source, n) for n in names) with tempfile.TemporaryDirectory(prefix='clpc-high-power-') as tmp: out = args.out or Path(tmp)