From 9a5e97eb52f8fdc2d913f22d880e64efa7ebc5d7 Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Mon, 16 Nov 2026 18:15:50 +0100 Subject: [PATCH] soc: apple: sleep: send J813's final SMC notice after peer stops After a battery lid suspend on J813, ANS failed its IOP IDLE handshake and SMC faulted at the ANS mailbox. Linux resumed, but ANS could not restart and DCP power-on timed out. The matching native trace idles ANS before the final SMC MBSE/AP IDLE sequence. Defer that sequence until the peer stops have finished. Reserve the existing bounded SMC transport before IRQ shutdown and check ownership before irreversible stops. Release it at platform wake, retaining AP IDLE until macsmc resume_noirq so the original wake-notice timing is preserved. Sending AP ON from platform wake caused Alpha Wi-Fi aborts on the test stack. Keep the J813-only diagnostic switch, command sequence and delay floors. Three RTC and two battery lid wakes completed on the tested T8142 stack with the final behavior. The upstream build passes; T8142 runtime support still depends on the pending sleep series. Signed-off-by: Thomas Glanzmann --- drivers/mfd/macsmc.c | 70 +++++++++++++++++++++++++++++++-- drivers/soc/apple/sleep.c | 46 +++++++++++++++++++--- include/linux/soc/apple/sleep.h | 3 ++ 3 files changed, 110 insertions(+), 9 deletions(-) diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 1a4f9c2da8cfc..32823667e5a6f 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -650,6 +650,9 @@ static int apple_smc_probe(struct platform_device *pdev) of_device_is_compatible(dev->of_node, "apple,t6050-smc"); if (smc->saved_enabled) smc->wake_reasons = apple_smc_j714s_wake_reasons; + /* J813's late notice needs the bounded, IRQ-independent transport too. */ + if (of_machine_is_compatible("apple,j813")) + smc->saved_enabled = true; smc->sram_base = devm_platform_get_and_ioremap_resource(pdev, 1, &smc->sram); if (IS_ERR(smc->sram_base)) return dev_err_probe(dev, PTR_ERR(smc->sram_base), "Failed to map SRAM region"); @@ -1130,7 +1133,8 @@ static int apple_smc_sleep_saved_begin(void *data) struct apple_smc *smc = data; int ret; - if (!smc->saved_enabled || !smc->sleep_policy) + if (!smc->saved_enabled || + (!smc->sleep_policy && !apple_sleep_smc_defer_notice())) return -EOPNOTSUPP; if (!mutex_trylock(&smc->mutex)) return -EBUSY; @@ -1182,7 +1186,7 @@ static int apple_smc_sleep_saved_begin(void *data) return ret; } -static int apple_smc_sleep_saved_end(void *data) +static int apple_smc_sleep_saved_release(void *data, bool wake_ap) { struct apple_smc *smc = data; int ret, restore = 0; @@ -1195,7 +1199,7 @@ static int apple_smc_sleep_saved_end(void *data) /* Bounded reconciliation; never release pending ownership on timeout. */ if (!ret && smc->saved_pending) ret = apple_smc_saved_wait_reply(smc); - if (smc->ap_notified) { + if (wake_ap && smc->ap_notified) { restore = apple_rtkit_m5_saved_ap_power(smc->rtk, true, NULL, NULL); if (!restore) { smc->ap_notified = false; @@ -1217,6 +1221,23 @@ static int apple_smc_sleep_saved_end(void *data) return ret; } +static int apple_smc_sleep_saved_end(void *data) +{ + return apple_smc_sleep_saved_release(data, true); +} + +/* + * J813's ordinary wake notice belongs in macsmc's resume_noirq, after the + * device dependencies have resumed. Platform wake precedes every noirq + * device callback; an AP ON there can wake the radio before its DMA path. + * Release the bounded transport here, retaining ap_notified for the usual + * noirq wake callback. A refused entry never posted AP IDLE. + */ +static int apple_smc_j813_saved_end(void *data) +{ + return apple_smc_sleep_saved_release(data, false); +} + /* Readiness is a returning operation BEFORE SEP/ANS/PMP stop. A drained * notification remains retained and can cancel only on this side of the boundary. */ @@ -1336,6 +1357,42 @@ static const struct apple_sleep_smc_saved_ops apple_smc_saved_ops = { .pmvc = apple_smc_sleep_saved_pmvc, }; +/* + * J813 retains its existing MBSE/AP sequence and delay floors. The transport + * is the saved lane: no scheduler, jiffies, IRQ worker or SRAM reuse is needed + * after the peer stops. Unrelated notifications are retained until resume. + */ +static int apple_smc_sleep_after_peers(void *data, u64 *sent, u64 *ack) +{ + struct apple_smc *smc = data; + int ret; + + lockdep_assert_held(&smc->mutex); + *sent = *ack = 0; + if (!irqs_disabled() || !smc->saved_scope || !smc->sleep_warned || + smc->ap_notified) + return -EINVAL; + + smc->saved_committed = true; + ret = apple_smc_saved_write_u32(smc, SMC_KEY(MBSE), SMC_KEY(slpa)); + if (ret) + goto out; + mdelay(SMC_SLEEP_ARM_MS); + smc->ap_notified = true; + ret = apple_rtkit_m5_saved_ap_power(smc->rtk, false, sent, ack); + if (!ret) + mdelay(SMC_SLEEP_IDLE_MS); +out: + smc->saved_notice_error = ret; + return ret; +} + +static const struct apple_sleep_smc_saved_ops apple_smc_j813_saved_ops = { + .begin = apple_smc_sleep_saved_begin, + .late_notice = apple_smc_sleep_after_peers, + .end = apple_smc_j813_saved_end, +}; + static int apple_smc_prepare_policy(struct apple_smc *smc) { const struct apple_sleep_smc_policy *p = smc->sleep_policy; @@ -1401,6 +1458,8 @@ static int apple_smc_sleep_prepare(struct apple_smc *smc) else apple_smc_sleep_warn(smc); + if (apple_sleep_smc_defer_notice()) + return apple_sleep_smc_register_saved(smc->dev, &apple_smc_j813_saved_ops, smc); return 0; } @@ -1514,6 +1573,9 @@ static int apple_smc_sleep_enter(struct apple_smc *smc) apple_smc_outbox_poll(smc, "late sleep warning", SMC_SLEEP_WARN_MS, 0); } + /* Warning and debug-link handling stay in noirq; only the notice moves. */ + if (apple_sleep_smc_defer_notice()) + return 0; return apple_smc_sleep_commit(smc); } @@ -1541,7 +1603,7 @@ static int apple_smc_resume_noirq(struct device *dev) if (smc->saved_enabled && (smc->saved_recovery || smc->saved_pending || smc->saved_fault)) return smc->saved_restore_error ?: smc->saved_fault ?: -EBUSY; - if (smc->saved_enabled && smc->sleep_policy) { + if (smc->saved_enabled && (smc->sleep_policy || smc->saved_wake)) { if (smc->saved_wake) { apple_smc_set_polled(smc, true); apple_smc_sleep_wake(smc); diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index ebab4c4becf79..960525c7971b7 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -1470,6 +1470,18 @@ const struct apple_sleep_smc_policy *apple_sleep_smc_policy(void) } EXPORT_SYMBOL_GPL(apple_sleep_smc_policy); +static bool sleep_j813_smc_late = true; +module_param_named(j813_smc_late, sleep_j813_smc_late, bool, 0444); +MODULE_PARM_DESC(j813_smc_late, + "TEST ONLY: J813 final SMC sleep notice after peer stops"); + +bool apple_sleep_smc_defer_notice(void) +{ + return apple_sleep && READ_ONCE(sleep_j813_smc_late) && + of_machine_is_compatible("apple,j813") && pm_suspend_via_firmware(); +} +EXPORT_SYMBOL_GPL(apple_sleep_smc_defer_notice); + int apple_sleep_smc_register(struct device *dev, int (*late_notice)(void *data, u64 *sent, u64 *ack), void *data) { @@ -1494,8 +1506,8 @@ int apple_sleep_smc_register_saved(struct device *dev, struct apple_sleep *s = apple_sleep; guard(mutex)(&apple_sleep_smc_lock); - if (!s || !s->soc->early_context || !s->soc->smc_policy || - !pm_suspend_via_firmware()) + if (!s || ((!s->soc->early_context || !s->soc->smc_policy) && + !apple_sleep_smc_defer_notice()) || !pm_suspend_via_firmware()) return -EOPNOTSUPP; if (!ops || !ops->begin || !ops->late_notice || !ops->end || s->smc.dev) return -EBUSY; @@ -3907,11 +3919,12 @@ static void t8132_wake(struct apple_sleep *s) static int apple_sleep_smc_late_notice(struct apple_sleep *s) { - if (!s->soc->smc_policy) + if (!s->soc->smc_policy && !apple_sleep_smc_defer_notice()) return 0; if (!s->smc.dev || !s->smc.late_notice) return -ENODEV; - if (s->soc->early_context && (!s->smc.saved_ops || !s->smc.held)) + if ((s->soc->early_context || apple_sleep_smc_defer_notice()) && + (!s->smc.saved_ops || !s->smc.held)) return -EBUSY; return s->smc.late_notice(s->smc.data, &s->smc_idle_sent, &s->smc_idle_ack); } @@ -4097,6 +4110,14 @@ static int apple_sleep_enter(suspend_state_t state) if (s->soc->early_context) return apple_sleep_enter_early(s); + /* Refuse while all peer stops are still reversible. */ + if (apple_sleep_smc_defer_notice()) { + if (!s->smc.dev || !s->smc.late_notice) + return -ENODEV; + if (!s->smc.saved_ops || !s->smc.held) + return -EBUSY; + } + ret = apple_sleep_refuse(s); if (ret) return ret; @@ -4126,6 +4147,18 @@ static int apple_sleep_enter(suspend_state_t state) if (s->soc->ack_cluster_dcs && (readl(s->cluster_dcs) & CLUSTER_UP_DONE)) writel(readl(s->cluster_dcs), s->cluster_dcs); apple_sleep_coprocs_stop(s, false); + /* + * J813 native trace: ANS idle completes before SMC slpa/AP IDLE. + * In the battery failure SMC faulted at ANS + 0xc110 after ANS's + * idle ACK was missed. Keep its final notice behind the peer stops + * and ahead of the domain power-down in the last mile. + */ + if (apple_sleep_smc_defer_notice()) { + ret = apple_sleep_smc_late_notice(s); + dev_info(s->dev, "TEST: SMC notice after peer stops: %d\n", ret); + if (ret) + panic("J813 SMC notice failed after peer stops: %d", ret); + } /* Done by now unless no SMC driver is bound. */ apple_sleep_usb_down(s); s->deep_once = READ_ONCE(sleep_deep_requested); @@ -4398,7 +4431,8 @@ static int apple_sleep_smc_scope_begin(void) struct apple_sleep *s = apple_sleep; int ret = 0; - if (!s->soc->early_context || !s->soc->smc_policy) + if ((!s->soc->early_context || !s->soc->smc_policy) && + !apple_sleep_smc_defer_notice()) return 0; mutex_lock(&apple_sleep_smc_lock); if (!s->smc.saved_ops || !s->smc.dev) { @@ -4587,6 +4621,8 @@ static const struct platform_suspend_ops apple_sleep_ops = { .valid = suspend_valid_only_mem, .begin = apple_sleep_begin, .prepare = apple_sleep_prepare, + .prepare_late = apple_sleep_smc_scope_begin, + .finish = apple_sleep_smc_scope_end, .enter = apple_sleep_enter, .wake = apple_sleep_report, .end = apple_sleep_end, diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index b2767f37602dc..61420e557bdb3 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -38,6 +38,8 @@ struct apple_sleep_smc_saved_ops { #if IS_ENABLED(CONFIG_APPLE_SOC_SLEEP) const struct apple_sleep_smc_policy *apple_sleep_smc_policy(void); +/* J813 experiment: send the final SMC notice after the peer idle handshakes. */ +bool apple_sleep_smc_defer_notice(void); int apple_sleep_smc_register(struct device *dev, int (*late_notice)(void *data, u64 *sent, u64 *ack), void *data); int apple_sleep_smc_register_saved(struct device *dev, @@ -235,6 +237,7 @@ void apple_sleep_restore_pins(void); int apple_sleep_engine_reset(struct device *dev); #else static inline const struct apple_sleep_smc_policy *apple_sleep_smc_policy(void) { return NULL; } +static inline bool apple_sleep_smc_defer_notice(void) { return false; } static inline int apple_sleep_smc_register(struct device *dev, int (*late_notice)(void *data, u64 *sent, u64 *ack), void *data) { return -EOPNOTSUPP; } static inline int apple_sleep_smc_register_saved(struct device *dev,