From a3610c4697a4387c5bf3e78f8813a6680c94e8b8 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Wed, 18 Nov 2026 11:01:24 -0700 Subject: [PATCH 1/5] soc: apple: sleep, mfd: macsmc: a port's wake with the lid closed is a dark one The SMC wakes the AP when a charger or a cable comes or goes, and has no mask for it: macOS writes none, and leaves such a wake a dark one. A charger that drops and renegotiates its contract under the sleep's load woke a K116s five times in six hours with the lid closed (wake record USB-C_plug each time). Every one was a full resume, with user space suspending again ten seconds later. Take a wake for acattach, acdetach or USB-C_plug alone as a dark wake when the lid reads closed, as the lid's own closing already is. Signed-off-by: Jens Axboe --- drivers/mfd/macsmc.c | 2 ++ drivers/soc/apple/sleep.c | 15 +++++++++++++++ include/linux/soc/apple/sleep.h | 5 +++++ 3 files changed, 22 insertions(+) diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 5204c960ebe9d..33f7eb5f75f3c 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -1019,6 +1019,8 @@ static void apple_smc_sleep_wake(struct apple_smc *smc) } smc->sleep_warned = false; apple_smc_lid_wake(smc); + if (apple_smc_port_reasons_only(smc, smc->wake_record)) + apple_sleep_port_wake(apple_sleep_lid_closed_at_wake() > 0); /* With a sleep policy (the J714s), the record is only named and the lid read. */ if (smc->sleep_policy) return; diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index ce1bcee755574..adb829afb0cbd 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -3698,6 +3698,21 @@ void apple_sleep_lid_closed_wake(void) } EXPORT_SYMBOL_GPL(apple_sleep_lid_closed_wake); +/* + * macOS leaves a wake for a charger or a cable alone a dark one. With the lid closed + * nothing there is for the user: sleep again. + */ +void apple_sleep_port_wake(bool lid_closed) +{ + struct apple_sleep *s = apple_sleep; + + if (!s || !s->smc_woke_alone || !lid_closed) + return; + dev_info(s->dev, "woken for a port with the lid closed: dark wake\n"); + pm_set_dark_wake(); +} +EXPORT_SYMBOL_GPL(apple_sleep_port_wake); + /* Read in the SMC's noirq resume. Forgotten as each sleep begins. */ static int sleep_lid_closed = -ENODATA; diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index cec1e88452676..baa59d2cbbef3 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -177,6 +177,7 @@ bool apple_sleep_debug_link_up(void); * device has resumed, as macOS does from a dark wake. */ void apple_sleep_lid_closed_wake(void); +void apple_sleep_port_wake(bool lid_closed); /* * From the SMC's driver, in its noirq resume: the lid as it reads now, after every @@ -377,6 +378,10 @@ static inline void apple_sleep_lid_closed_wake(void) { } +static inline void apple_sleep_port_wake(bool lid_closed) +{ +} + static inline void apple_sleep_lid_at_wake(bool closed) { } From 6e75b6bed02a151efe158c070a42ae7183fc61b2 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Wed, 18 Nov 2026 11:19:19 -0700 Subject: [PATCH 2/5] soc: apple: sleep: T6050: tell when the SMC alone woke the AP apple_sleep_smc_woke_alone() reads the wake status the common wake arming saved, which a SoC with its own wake never fills: on T6050 it is always false, and no wake is ever taken as a dark one. Use the status T6050's wake reads at its start: only the SMC's lines latched, and nothing in the GFX word. Seen on a K116s, where a wake for USB-C_plug alone with the lid closed logged SMC.OutboxNotEmpty as the only source and was still a full wake. Signed-off-by: Jens Axboe --- drivers/soc/apple/sleep-t6050.h | 26 ++++++++++++++++++++++++++ drivers/soc/apple/sleep.c | 5 +++++ 2 files changed, 31 insertions(+) diff --git a/drivers/soc/apple/sleep-t6050.h b/drivers/soc/apple/sleep-t6050.h index b9744f3a5e36c..cb72e1152141e 100644 --- a/drivers/soc/apple/sleep-t6050.h +++ b/drivers/soc/apple/sleep-t6050.h @@ -3601,6 +3601,32 @@ static void t6050_wake_source_report(struct apple_sleep *s) dev_info(s->dev, "wake status set: GFX %u\n", b); } +/* Only the SMC's lines are latched: its own wake, whatever else was armed. */ +static bool t6050_smc_woke_alone(struct apple_sleep *s) +{ + struct apple_sleep_region_state *r = s->region; + u32 smc[T6050_WAKE_WORDS] = {}; + unsigned int i; + bool any = false; + + if (!r || !r->wake || !r->wake_gfx || r->wake_status[T6050_WAKE_WORDS]) + return false; + for (i = 0; i < ARRAY_SIZE(t6050_wake_names); i++) { + u32 id = t6050_wake_names[i].id; + + if (strstarts(t6050_wake_names[i].name, "SMC.") && + id / 32 < T6050_WAKE_WORDS) + smc[id / 32] |= BIT(id % 32); + } + for (i = 0; i < T6050_WAKE_WORDS; i++) { + if (r->wake_status[i] & ~smc[i]) + return false; + any |= !!r->wake_status[i]; + } + + return any; +} + static void t6050_sleep_wake_regs(struct apple_sleep *s) { struct apple_sleep_region_state *r = s->region; diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index adb829afb0cbd..c68c84d206f40 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -6700,6 +6700,8 @@ static int apple_sleep_prepare(void) } /* Only the SMC's sources woke the SoC. */ +static bool t6050_smc_woke_alone(struct apple_sleep *s); + static bool apple_sleep_smc_woke_alone(struct apple_sleep *s) { const struct apple_sleep_wake_name *names = s->soc->wake_names; @@ -6707,6 +6709,9 @@ static bool apple_sleep_smc_woke_alone(struct apple_sleep *s) bool any = false; int i; + if (s->soc->regions) + return t6050_smc_woke_alone(s); + /* SMC.CPUWakeupAP and SMC.OutboxNotEmpty, by their names. */ for (i = 0; i < s->soc->nwake_names; i++) { u32 id = names[i].id; From 3baa40c39109297fb6c76bfc851faafef9f42413 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Thu, 19 Nov 2026 11:57:23 -0700 Subject: [PATCH 3/5] mfd: macsmc: wait out a command in flight at .prepare A dark wake suspends again as soon as every device has resumed. The SMC's .prepare then finds a command of the wake still in flight and refuses with -EBUSY: on a K116s, both dark wakes for a charger plugged back in ended that way, and user space took over ten seconds later. Take the mutex and so wait for that command to complete. Signed-off-by: Jens Axboe --- drivers/mfd/macsmc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 33f7eb5f75f3c..1383af405217d 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -1481,8 +1481,8 @@ static int apple_smc_prepare(struct device *dev) int ret = 0; if (smc->saved_enabled) { - if (!mutex_trylock(&smc->mutex)) - return -EBUSY; + /* A sleep right after a dark wake finds the wake's commands in flight */ + mutex_lock(&smc->mutex); ret = apple_smc_saved_guard(smc); mutex_unlock(&smc->mutex); } From b827310f53c1c9d75eebc90d46c9f6532249520f Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Thu, 19 Nov 2026 16:59:45 -0700 Subject: [PATCH 4/5] soc: apple: rtkit: one predicate for the OSLOG counter's echo Whether an OSLOG counter is answered with its echo is decided in three places of apple_rtkit_oslog_rx(). Put it in apple_rtkit_oslog_echoed() so that another reader of the mailbox can ask the same question. No change in what is echoed. Signed-off-by: Jens Axboe --- drivers/soc/apple/rtkit.c | 38 +++++++++++++------ tools/asahi/rtkit-oslog-tests.py | 6 ++- .../selftests/drivers/gpu/apple-dcp/boot.py | 7 +++- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/drivers/soc/apple/rtkit.c b/drivers/soc/apple/rtkit.c index 22a7fad956426..7986d4f0e300e 100644 --- a/drivers/soc/apple/rtkit.c +++ b/drivers/soc/apple/rtkit.c @@ -1350,6 +1350,25 @@ static void apple_rtkit_syslog_rx(struct apple_rtkit *rtk, u64 msg) } } +/* Where a counter has only ever run unanswered: nothing is echoed. */ +static bool apple_rtkit_oslog_unanswered(struct apple_rtkit *rtk) +{ + return !rtk->oslog_echo && + (rtk->sync_syslog_ack || of_machine_is_compatible("apple,t8140") || + of_machine_is_compatible("apple,t8142")); +} + +/* An OSLOG counter that is answered with its echo, on any lane. */ +static bool apple_rtkit_oslog_echoed(struct apple_rtkit *rtk, u64 msg) +{ + if (rtk->oslog_echo) + return true; + if (apple_rtkit_oslog_unanswered(rtk)) + return false; + return (rtk->oslog_buffer.size || rtk->oslog_inherited) && + !(msg & APPLE_RTKIT_OSLOG_LOG_RESERVED); +} + static void apple_rtkit_oslog_rx(struct apple_rtkit *rtk, u64 msg) { u8 type = FIELD_GET(APPLE_RTKIT_OSLOG_TYPE, msg); @@ -1373,11 +1392,12 @@ static void apple_rtkit_oslog_rx(struct apple_rtkit *rtk, u64 msg) return; } - /* Preserve the tested Neo/M5 and synchronous-log no-ACK policy. */ - if (type == APPLE_RTKIT_OSLOG_LOG && - (rtk->sync_syslog_ack || of_machine_is_compatible("apple,t8140") || - of_machine_is_compatible("apple,t8142"))) { - dev_dbg(rtk->dev, "RTKit: oslog notification: %016llx\n", msg); + if (type == APPLE_RTKIT_OSLOG_LOG && !apple_rtkit_oslog_echoed(rtk, msg)) { + /* Preserve the tested Neo/M5 and synchronous-log no-ACK policy. */ + if (apple_rtkit_oslog_unanswered(rtk)) + dev_dbg(rtk->dev, "RTKit: oslog notification: %016llx\n", msg); + else + dev_warn(rtk->dev, "RTKit: invalid oslog notification: %llx\n", msg); return; } @@ -1392,13 +1412,9 @@ static void apple_rtkit_oslog_rx(struct apple_rtkit *rtk, u64 msg) * AppleDCP waits for its log transport to drain before AP sleep. * This is not a buffer offset and may wrap. Other channels do not * have a buffer admitted by this endpoint implementation. An inherited - * channel zero needs no buffer mapping to acknowledge its counter. + * channel zero needs no buffer mapping to acknowledge its counter + * (apple_rtkit_oslog_echoed()). */ - if ((!rtk->oslog_buffer.size && !rtk->oslog_inherited) || - (msg & APPLE_RTKIT_OSLOG_LOG_RESERVED)) { - dev_warn(rtk->dev, "RTKit: invalid oslog notification: %llx\n", msg); - break; - } err = apple_rtkit_send_message(rtk, APPLE_RTKIT_EP_OSLOG, msg, NULL, false); if (err) diff --git a/tools/asahi/rtkit-oslog-tests.py b/tools/asahi/rtkit-oslog-tests.py index 0f2755d501c79..0f1bbf7be90c4 100644 --- a/tools/asahi/rtkit-oslog-tests.py +++ b/tools/asahi/rtkit-oslog-tests.py @@ -14,7 +14,7 @@ def function(source, name): - match = re.search(r'^(?:static )?(?:void|int) ' + name + r'\(', source, re.M) + match = re.search(r'^(?:static )?(?:void|int|bool) ' + name + r'\(', source, re.M) assert match, name opening = source.index('{', match.end()) depth, end = 1, opening + 1 @@ -173,12 +173,16 @@ def main(): constants += '\n#define APPLE_RTKIT_OSLOG_LOG 2\n' # The session reset reinit() and a cold restart share, where the driver has one. names = ['apple_rtkit_oslog_rx', 'apple_rtkit_mark_running'] + # The echo's predicate, where the receiver is built on one. + if 'static bool apple_rtkit_oslog_echoed(' in source: + names[:0] = ['apple_rtkit_oslog_unanswered', 'apple_rtkit_oslog_echoed'] if 'static void apple_rtkit_reset_session(' in source: names.append('apple_rtkit_reset_session') names.append('apple_rtkit_reinit') bodies = '\n'.join(function(source, name) for name in names) if args.mutate == 'admission': bodies = bodies.replace('(!rtk->oslog_buffer.size && !rtk->oslog_inherited)', 'false') + bodies = bodies.replace('(rtk->oslog_buffer.size || rtk->oslog_inherited)', 'true') elif args.mutate == 'channel': bodies = bodies.replace('(msg & APPLE_RTKIT_OSLOG_LOG_RESERVED)', 'false') elif args.mutate == 'counter': diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/boot.py b/tools/testing/selftests/drivers/gpu/apple-dcp/boot.py index e634c2f04effd..e3326880f5eb0 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/boot.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/boot.py @@ -7,6 +7,11 @@ def oslog(root): source = (root / 'drivers/soc/apple/rtkit.c').read_text() constants = '\n'.join(line for line in source.splitlines() if line.startswith('#define APPLE_RTKIT_OSLOG_')) + # The echo's predicate, where the receiver is built on one. + helpers = ''.join(block(source, marker) + '\n' + for marker in ('static bool apple_rtkit_oslog_unanswered(', + 'static bool apple_rtkit_oslog_echoed(') + if marker in source) return r''' #include #include @@ -37,7 +42,7 @@ def oslog(root): { (void)rtk; assert(ep==8 && !completion && !atomic); acks++; observed=msg; return 0; } static void apple_rtkit_common_rx_get_buffer(struct apple_rtkit *rtk, struct buffer *b, u8 ep, u64 msg) { assert(b==&rtk->oslog_buffer && ep==8); requests++; observed=msg; } -''' + constants + '\n' + block(source, 'static void apple_rtkit_oslog_rx(') + r''' +''' + constants + '\n' + helpers + block(source, 'static void apple_rtkit_oslog_rx(') + r''' static void reset(void) { warnings=notifications=requests=acks=errors=0; observed=0; } int main(void) { From ac2079fed0552c60668735e76802a0103f9ed6cb Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Thu, 19 Nov 2026 16:59:45 -0700 Subject: [PATCH 5/5] soc: apple: rtkit: echo log words on the saved lane The saved lane keeps a log word for the ordinary handler, which echoes it after the wake. The SMC takes no AP idle notice while a log word is owed: when it logs inside the committed part of the entry, the notice times out and the entry panics, past the last step it can undo. A dark wake that suspends again within two seconds does that on a K116s, with either kind of log word: - A charger plugged back in during a sleep, the SMC still logging its contract. 3 of 9 such runs ended in the panic; in the one with the lane's packets recorded, eight syslog words had reached it just before the notice. In a run with eight such words echoed, the SMC answered the notice in 166 us, as it does in every other sleep. - Dark wakes in a row with no charger: 3 panics in about 120, with the syslog words already echoed. The lane held one packet, the OSLOG counter: macsmc: sleep notice: AP IDLE: -110 after 817327 us Kernel panic - not syncing: Apple committed SMC notice failed after peer stop: -110 Echo a word on the saved lane when the ordinary handler would answer it with its echo and nothing else: a syslog record's word and the OSLOG counter. A syslog record echoed there is not printed, as with the words the receive path acknowledges ahead of the ordered queue. With both echoed, 200 dark wakes in a row went back to sleep. Signed-off-by: Jens Axboe --- drivers/soc/apple/rtkit.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/drivers/soc/apple/rtkit.c b/drivers/soc/apple/rtkit.c index 7986d4f0e300e..ebdb0a929632d 100644 --- a/drivers/soc/apple/rtkit.c +++ b/drivers/soc/apple/rtkit.c @@ -1816,6 +1816,16 @@ bool apple_rtkit_m5_saved_unresolved(struct apple_rtkit *rtk) } EXPORT_SYMBOL_GPL(apple_rtkit_m5_saved_unresolved); +/* A log word the ordinary handler answers with its echo and nothing else. */ +static bool apple_rtkit_log_word(struct apple_rtkit *rtk, u8 ep, u64 msg) +{ + if (ep == APPLE_RTKIT_EP_SYSLOG) + return apple_rtkit_notification_msg(ep, msg); + return ep == APPLE_RTKIT_EP_OSLOG && + FIELD_GET(APPLE_RTKIT_OSLOG_TYPE, msg) == APPLE_RTKIT_OSLOG_LOG && + apple_rtkit_oslog_echoed(rtk, msg); +} + int apple_rtkit_m5_saved_poll(struct apple_rtkit *rtk) { struct apple_mbox_msg msg; @@ -1840,6 +1850,10 @@ int apple_rtkit_m5_saved_poll(struct apple_rtkit *rtk) rtk->ops->recv_message_early && rtk->ops->recv_message_early(rtk->cookie, msg.msg1, msg.msg0)) return 1; + /* Echoed here, its record unread: the firmware takes no AP idle notice with one owed. */ + if (apple_rtkit_log_word(rtk, msg.msg1, msg.msg0) && + !apple_mbox_send_nowait(rtk->mbox, msg)) + return 1; /* SMC notifications remain ordered for the ordinary notifier worker. * Every other packet is retained, and refuses this attempt. In * particular no crash/report message is silently eaten by polling.