From 36d2d18d3d4fdf9d15129392e3b42c50c573789a Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 20 Nov 2026 11:46:26 -0500 Subject: [PATCH] soc: apple: rtkit: give the saved scope's AP IDLE up to 10 s On the J714s (M5 Pro), about one deep sleep in thirty to fifty entered some 40 s after a Type-C event on the Dell monitor's port hung: the machine stayed silent until the sleep watchdog, armed at the sleep's begin for 120 s, reset it. A step trace of the committed SMC notice on the DockChannel named the step: the SMC took MBSE = slpa and the message drain, then AP IDLE: -110, 834453 us into the notice, 816318 us in the step and the committed notice's panic followed, silent with panic=0 and the consoles suspended. On a clean sleep AP IDLE and the drain after it take about 0.2 ms. The -110 is the saved scope's fixed bound of 50000 steps of 10 us (500 ms nominal, 816 ms as measured) on posting the AP power state or on its ack. Those hangs all ran without commit ac2079fed055 ("soc: apple: rtkit: echo log words on the saved lane"), which this tree has: the SMC holds back AP IDLE's ack while a syslog record word or OSLOG counter on the saved lane goes unechoed, and the saved poll used to defer those words unanswered. That is the cause. This is margin behind it: AP IDLE's post and its ack now each get up to 1000000 steps (10 s nominal, about 16 s as measured), so an SMC that answers late for any other reason gives a slow sleep instead of a halt. AP ON, which the wake sends from .finish with interrupts on, keeps the 500 ms bound, as do the other saved-scope messages. A post or an ack that takes more than 5 ms is logged with its count, and a failure with its count too. A longer wait does not change two other ends: an SMC that never answers still fails the notice (after ~16 s rather than ~0.8 s), and the deferred kfifo of SMC-endpoint messages (64 entries) can still fill over a long wait, which fails the poll with -ENOBUFS. Tested on the J714s, with the J714s macOS sleep entry, on a tree without ac2079fed055: 22 logind deep sleeps each entered 40 s after a replug of the Dell's port, 0 hangs (2 hangs in 55 such sleeps before). No AP IDLE post or ack was slow in those sleeps, so the longer bound was not needed there. Signed-off-by: Diljit Singh Co-Authored-By: Claude Opus 5.5 --- drivers/soc/apple/rtkit.c | 56 ++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/drivers/soc/apple/rtkit.c b/drivers/soc/apple/rtkit.c index ec668910d2180..1df98b873e221 100644 --- a/drivers/soc/apple/rtkit.c +++ b/drivers/soc/apple/rtkit.c @@ -1879,14 +1879,30 @@ int apple_rtkit_m5_saved_poll(struct apple_rtkit *rtk) } EXPORT_SYMBOL_GPL(apple_rtkit_m5_saved_poll); -static int apple_rtkit_m5_saved_tx(struct apple_rtkit *rtk, struct apple_mbox_msg msg, - u64 *sent) +/* + * AP IDLE's post and its ack in the saved scope. On the J714s, in a sleep entered some 40 s after + * a Type-C event, the SMC took more than 816 ms to take or ack AP IDLE in the committed SMC notice: + * at the 500 ms bound that was -ETIMEDOUT, then the notice's panic, a silent halt with the consoles + * suspended until the sleep watchdog's reset, which is armed at the sleep's begin for 120 s. On a + * clean sleep the step takes ~0.2 ms. The post and the ack of AP IDLE each get up to + * APPLE_RTKIT_M5_AP_IDLE_LOOPS steps (10 s nominal, ~16 s as measured), so an SMC that answers late + * gives a slow sleep instead. AP ON, sent at the wake with interrupts on, keeps the + * APPLE_RTKIT_M5_LOOPS bound, as does every other saved-scope message. A post or an ack that takes + * more than APPLE_RTKIT_M5_AP_SLOW_LOOPS steps (5 ms) is logged. + */ +#define APPLE_RTKIT_M5_AP_IDLE_LOOPS 1000000 +#define APPLE_RTKIT_M5_AP_SLOW_LOOPS 500 + +static int apple_rtkit_m5_saved_tx_loops(struct apple_rtkit *rtk, struct apple_mbox_msg msg, + u64 *sent, unsigned int loops, unsigned int *tries) { int i, ret; + *tries = 0; if (!rtk->m5_saved.active) return -EINVAL; - for (i = 0; i < APPLE_RTKIT_M5_LOOPS; i++) { + for (i = 0; i < loops; i++) { + *tries = i; if (rtk->m5_saved.error) return rtk->m5_saved.error; if (READ_ONCE(rtk->crashed) || READ_ONCE(rtk->shutting_down)) @@ -1905,9 +1921,18 @@ static int apple_rtkit_m5_saved_tx(struct apple_rtkit *rtk, struct apple_mbox_ms */ udelay(APPLE_RTKIT_M5_STEP_US); } + *tries = loops; return -ETIMEDOUT; } +static int apple_rtkit_m5_saved_tx(struct apple_rtkit *rtk, struct apple_mbox_msg msg, + u64 *sent) +{ + unsigned int tries; + + return apple_rtkit_m5_saved_tx_loops(rtk, msg, sent, APPLE_RTKIT_M5_LOOPS, &tries); +} + int apple_rtkit_m5_saved_send(struct apple_rtkit *rtk, u64 message) { struct apple_mbox_msg msg = { @@ -1920,21 +1945,29 @@ int apple_rtkit_m5_saved_send(struct apple_rtkit *rtk, u64 message) } EXPORT_SYMBOL_GPL(apple_rtkit_m5_saved_send); -static int apple_rtkit_m5_saved_wait_ap(struct apple_rtkit *rtk, u64 *ack) +static int apple_rtkit_m5_saved_wait_ap(struct apple_rtkit *rtk, u64 *ack, unsigned int loops) { int i, ret; - for (i = 0; i < APPLE_RTKIT_M5_LOOPS; i++) { + for (i = 0; i < loops; i++) { ret = apple_rtkit_m5_saved_poll(rtk); - if (ret < 0) + if (ret < 0) { + dev_err(rtk->dev, "RTKit: AP power state %u: poll error %d after %d polls\n", + rtk->m5_saved.ap_target, ret, i); return ret; + } if (!rtk->m5_saved.ap_pending) { if (ack) *ack = rtk->m5_saved.stamp(); + if (i >= APPLE_RTKIT_M5_AP_SLOW_LOOPS) + dev_warn(rtk->dev, "RTKit: AP power state %u: acked late, after %d polls\n", + rtk->m5_saved.ap_target, i + 1); return 0; } udelay(APPLE_RTKIT_M5_STEP_US); } + dev_err(rtk->dev, "RTKit: AP power state %u: no ack after %d polls\n", + rtk->m5_saved.ap_target, i); return -ETIMEDOUT; } @@ -1942,30 +1975,35 @@ int apple_rtkit_m5_saved_ap_power(struct apple_rtkit *rtk, bool on, u64 *sent, u64 *ack) { unsigned int target = on ? APPLE_RTKIT_PWR_STATE_ON : APPLE_RTKIT_PWR_STATE_IDLE; + unsigned int loops = on ? APPLE_RTKIT_M5_LOOPS : APPLE_RTKIT_M5_AP_IDLE_LOOPS; struct apple_mbox_msg msg = { .msg0 = FIELD_PREP(APPLE_RTKIT_MGMT_TYPE, APPLE_RTKIT_MGMT_SET_AP_PWR_STATE) | FIELD_PREP(APPLE_RTKIT_MGMT_PWR_STATE, target), .msg1 = APPLE_RTKIT_EP_MGMT, }; + unsigned int tries; int ret; if (!rtk->m5_saved.active) return -EINVAL; /* A timeout is not cancellation. Reconcile the old target first. */ if (rtk->m5_saved.ap_pending) { - ret = apple_rtkit_m5_saved_wait_ap(rtk, NULL); + ret = apple_rtkit_m5_saved_wait_ap(rtk, NULL, loops); if (ret) return ret; } rtk->m5_saved.ap_target = target; smp_store_release(&rtk->m5_saved.ap_pending, true); - ret = apple_rtkit_m5_saved_tx(rtk, msg, sent); + ret = apple_rtkit_m5_saved_tx_loops(rtk, msg, sent, loops, &tries); + if (ret || tries >= APPLE_RTKIT_M5_AP_SLOW_LOOPS) + dev_warn(rtk->dev, "RTKit: AP power state %u: posted %d after %u tries\n", target, ret, + tries); if (ret) { /* nowait TX guarantees a negative return means never posted. */ smp_store_release(&rtk->m5_saved.ap_pending, false); return ret; } - return apple_rtkit_m5_saved_wait_ap(rtk, ack); + return apple_rtkit_m5_saved_wait_ap(rtk, ack, loops); } EXPORT_SYMBOL_GPL(apple_rtkit_m5_saved_ap_power);