From bcf371afe132b68b424f26d997c57dfffefc5fd9 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Tue, 10 Nov 2026 03:14:17 -0500 Subject: [PATCH 1/2] soc: apple: sleep: look for the debug link's contract from 50 ms after its cut The cut waited a fixed second after the port controller's cold reset before its first look at the contract, and in every logged sleep the contract was already back at that first look. That second was most of a cut sleep's entry. Look from 50 ms on instead. The contract reads back as before for a moment after the reset, so count it as back only once it was seen gone (no contract, or no answer while the controller restarts), or after the old second if it never seemed to go. Log both times. Signed-off-by: Diljit Singh --- drivers/soc/apple/sleep.c | 40 ++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 91ccd2386e44c..1227c98d0abb4 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -162,7 +163,9 @@ struct aura_s2r_mailbox { #define HPM_CMD_SSPS 0x53505353 #define HPM_CMD_COLD_RESET 0x64696147 #define HPM_SSPS_S3 3 -#define HPM_RESET_MS 1000 +/* The first look after the reset, and how long a contract that never seemed to go is trusted to have gone. */ +#define HPM_RESET_FIRST_MS 50 +#define HPM_RESET_FLOOR_MS 1000 #define HPM_PAGE_POWER_STATUS 0x3f #define HPM_POWER_CONNECTED BIT(0) #define HPM_POWER_TYPEC_MODE GENMASK(3, 2) @@ -1837,26 +1840,38 @@ static int sleep_hpm_sleep(struct apple_sleep *s) * Left to finish across the commit, that sometimes failed: neither side had a contract or * saw the other, and no VDM got through until a replug. Returns the ms it took. */ -static int sleep_hpm_wait_contract(struct apple_sleep *s) +static int sleep_hpm_wait_contract(struct apple_sleep *s, int *gone_ms) { - bool wake = true; + ktime_t start = ktime_get(); + bool wake = true, up; u32 val = 0; int ms, ret; - for (ms = 0; ms < HPM_CONTRACT_MS; ms += 10) { + /* + * It reads back as before for a moment after the reset. Count it as back once it was + * seen gone (no contract, or no answer while the controller restarts), or after the + * floor if it never seemed to go. + */ + *gone_ms = -1; + mdelay(HPM_RESET_FIRST_MS); + for (;;) { + ms = ktime_ms_delta(ktime_get(), start); + if (ms >= HPM_CONTRACT_MS) + return -ETIMEDOUT; ret = wake ? sleep_hpm_cmd(s, SPMI_OP_WAKEUP, 0, NULL, NULL) : 0; if (!ret) ret = sleep_hpm_select(s, HPM_PAGE_POWER_STATUS); if (!ret) ret = sleep_hpm_cmd(s, SPMI_OP_EXT_READ(4), HPM_REG_DATA, NULL, &val); - if (!ret && (val & HPM_POWER_CONNECTED) && - FIELD_GET(HPM_POWER_TYPEC_MODE, val) == HPM_POWER_TYPEC_PD) + up = !ret && (val & HPM_POWER_CONNECTED) && + FIELD_GET(HPM_POWER_TYPEC_MODE, val) == HPM_POWER_TYPEC_PD; + if (!up && *gone_ms < 0) + *gone_ms = ms; + if (up && (*gone_ms >= 0 || ms >= HPM_RESET_FLOOR_MS)) return ms; wake = ret; mdelay(10); } - - return -ETIMEDOUT; } /* A live debug link holds the debug switch domain on. */ @@ -1877,7 +1892,7 @@ static bool apple_sleep_usb_down(struct apple_sleep *s) { u32 cmd = HPM_CMD_COLD_RESET; u32 enable; - int i, ret, ms; + int i, ret, ms, gone; if (s->usb_down_ran) return false; @@ -1899,11 +1914,10 @@ static bool apple_sleep_usb_down(struct apple_sleep *s) if (ret) break; WRITE_ONCE(sleep_debug_link_cut, true); - mdelay(HPM_RESET_MS); - ms = sleep_hpm_wait_contract(s); + ms = sleep_hpm_wait_contract(s, &gone); if (ms >= 0) { - dev_info(s->dev, "debug link: contract back %d ms after reset %d\n", - HPM_RESET_MS + ms, i); + dev_info(s->dev, "debug link: contract back %d ms after reset %d (gone at %d ms)\n", + ms, i, gone); break; } dev_warn(s->dev, "debug link: no contract with the partner after reset %d\n", i); From 567a28f1d9a4e4f9bb31f5e5450d8fcd1c4be8b9 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Tue, 10 Nov 2026 03:14:18 -0500 Subject: [PATCH 2/2] mfd: macsmc: shorten the waits a cut sleep adds before its commit A sleep that cuts the debug link waits for the SMC's outbox to be quiet for 500 ms after the cut, then warns it late and leaves macOS's 770 ms before slpa. No message came in the quiet window in any logged sleep, and the sleeps that warn at .prepare, with no cable, commit 150-500 ms after the warning and run to their alarm (154 of 154). Use 100 ms of quiet and a 200 ms gap. Signed-off-by: Diljit Singh --- drivers/mfd/macsmc.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 1d0e4d58b68a6..fce9dc9527e66 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -683,11 +683,14 @@ MODULE_DEVICE_TABLE(of, apple_smc_of_match); /* The gaps macOS leaves before the AP idle notice and after it. */ #define SMC_SLEEP_ARM_MS 6 #define SMC_SLEEP_IDLE_MS 30 -/* The shortest gap macOS left between slpw and slpa, in two traced sleeps. */ -#define SMC_SLEEP_WARN_MS 770 +/* + * Between a late warning (a sleep that cut the debug link) and slpa. macOS's shortest gap + * was 770 ms; the warning at .prepare leaves 150-500 ms, and those sleeps run to the alarm. + */ +#define SMC_SLEEP_WARN_MS 200 -/* After the debug link's cut. Its port and charger events keep coming for over a second. */ -#define SMC_OUTBOX_QUIET_MS 500 +/* After the debug link's cut. No message came in it in any logged sleep. */ +#define SMC_OUTBOX_QUIET_MS 100 #define SMC_OUTBOX_SETTLE_MS 5000 static int apple_smc_ap_power(struct apple_smc *smc, bool on)