From 255b646d48d4fafd589efcdaa07626b1f321dec8 Mon Sep 17 00:00:00 2001 From: Dylan Ironside Date: Wed, 2 Dec 2026 07:10:23 -0600 Subject: [PATCH] soc: apple: sleep: fix APCIe auto-PM teardown and BUSY-latch lowering During repeated T8132 deep-sleep cycles with hardware PS_AUTO_ENABLE active on apcie_sys_st and apcie_phy_sw, an idle auto-gated domain reads PS_ACTUAL = 0 and causes apple_sleep_lower_apcie() to skip PHY/clkgen teardown or race against mid-MMIO auto-gating, and writing PS_TARGET = 0 without first waiting for the domain to wake after clearing PS_AUTO_ENABLE can latch PS_BUSY. Fix all three defects in drivers/soc/apple/sleep.c: - In sleep_ps_wait_on(), wait for FIELD_GET(PS_ACTUAL, val) to reach the full target state rather than merely non-zero. - Mask hardware-owned PS_BUSY in sleep_ps_lower_start() and sleep_ps_lower_finish(), and make sleep_ps_lower() wait for the domain to wake after clearing PS_AUTO_ENABLE before writing PS_TARGET = 0. - In apple_sleep_lower_apcie(), clear PS_AUTO_ENABLE and wait for full power on apcie_sys_st, apcie_sys_gp, and apcie_phy_sw before stopping ports, powering off the PHY/clkgen, and disabling AXI links, then lower each domain in order via sleep_ps_lower_finish(). Signed-off-by: Dylan Ironside --- drivers/soc/apple/sleep.c | 74 +++++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 23 deletions(-) diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 773fd10ae199d..af94fe528029c 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -109,6 +109,7 @@ struct aura_s2r_mailbox { /* PS word */ #define PS_AUTO_ENABLE BIT(28) #define PS_UNLATCH BIT(12) +#define PS_BUSY BIT(11) #define PS_DEV_DISABLE BIT(10) #define PS_FLAGS GENMASK(9, 8) #define PS_ACTUAL GENMASK(7, 4) @@ -3036,7 +3037,7 @@ static u32 sleep_ps_lower_start(struct apple_sleep *s, u32 offset) u32 val = readl(reg); if (val & PS_AUTO_ENABLE) { - val &= ~(PS_AUTO_ENABLE | PS_FLAGS); + val &= ~(PS_AUTO_ENABLE | PS_FLAGS | PS_BUSY); writel(val, reg); } @@ -3045,12 +3046,33 @@ static u32 sleep_ps_lower_start(struct apple_sleep *s, u32 offset) static void sleep_ps_lower_finish(struct apple_sleep *s, u32 offset, u32 val) { - writel((val & ~PS_TARGET) | PS_FLAGS, sleep_power_reg(s, offset)); + writel((val & ~(PS_TARGET | PS_BUSY)) | PS_FLAGS, sleep_power_reg(s, offset)); +} + +/* A domain the hardware idled under AUTO_ENABLE comes back once that's cleared. */ +static bool sleep_ps_wait_on(struct apple_sleep *s, u32 offset, u32 val) +{ + u32 target = FIELD_GET(PS_TARGET, val); + + if (!target) + return false; + + return !readl_poll_timeout_atomic(sleep_power_reg(s, offset), val, + FIELD_GET(PS_ACTUAL, val) == target, 1, + PS_OFF_TIMEOUT_US); } static void sleep_ps_lower(struct apple_sleep *s, u32 offset) { - sleep_ps_lower_finish(s, offset, sleep_ps_lower_start(s, offset)); + void __iomem *reg = sleep_power_reg(s, offset); + u32 val = readl(reg); + + if (val & PS_AUTO_ENABLE) { + val &= ~(PS_AUTO_ENABLE | PS_FLAGS | PS_BUSY); + writel(val, reg); + sleep_ps_wait_on(s, offset, val); + } + sleep_ps_lower_finish(s, offset, val); } /* @@ -3085,16 +3107,6 @@ static int sleep_ps_raise(struct apple_sleep *s, u32 offset) return ret; } -/* A domain the hardware idled under AUTO_ENABLE comes back once that's cleared. */ -static bool sleep_ps_wait_on(struct apple_sleep *s, u32 offset, u32 val) -{ - if (!FIELD_GET(PS_TARGET, val)) - return false; - - return !readl_poll_timeout_atomic(sleep_power_reg(s, offset), val, FIELD_GET(PS_ACTUAL, val), 1, - PS_OFF_TIMEOUT_US); -} - static int sleep_ps_wait_off(struct apple_sleep *s, u32 offset) { u32 val; @@ -3222,7 +3234,6 @@ EXPORT_SYMBOL_GPL(apple_sleep_domain_is_down); * target 4 and 0, released with the domain off, then raised again, its DARTs' state kept. The * caller has cleared the run bit, and starts the firmware after. */ -#define PS_BUSY BIT(11) #define PS_RESET BIT(31) #define PS_ENGINE_RESET_4 0x8f000244 #define PS_ENGINE_RESET_0 0x80000000 @@ -4579,21 +4590,39 @@ static void apple_sleep_lower_apcie_gen4(struct apple_sleep *s) static void apple_sleep_lower_apcie(struct apple_sleep *s) { const struct apple_sleep_domains *d = &s->soc->ps; - u32 val; + u32 st_val, gp_val, phy_val; + bool st_on, gp_on, phy_on; if (s->soc->apcie_gen4) { apple_sleep_lower_apcie_gen4(s); return; } - sleep_apcie_port_stop_once(s); + + /* + * Clear AUTO_ENABLE and wait for full power on apcie_sys_st, + * apcie_sys_gp, and apcie_phy_sw before touching port, PHY, clkgen, + * or AXI registers. Under hardware auto-PM an idle domain reads + * PS_ACTUAL=0 (skipping PHY/clkgen teardown) or can auto-gate + * mid-MMIO, and lowering directly from an auto-gated state can + * latch BUSY. + */ + st_val = sleep_ps_lower_start(s, d->apcie_sys_st); + st_on = sleep_ps_wait_on(s, d->apcie_sys_st, st_val); + gp_val = sleep_ps_lower_start(s, d->apcie_sys_gp); + gp_on = sleep_ps_wait_on(s, d->apcie_sys_gp, gp_val); + phy_val = sleep_ps_lower_start(s, d->apcie_phy_sw); + phy_on = sleep_ps_wait_on(s, d->apcie_phy_sw, phy_val); + + if (phy_on) + sleep_apcie_port_stop_once(s); /* The PHY's registers need both domains on. */ - if (s->apcie_phy && sleep_ps_on(s, d->apcie_phy_sw) && sleep_ps_on(s, d->apcie_sys_st)) + if (s->apcie_phy && phy_on && st_on) sleep_apcie_phy_off(s); - sleep_ps_lower(s, d->apcie_phy_sw); + sleep_ps_lower_finish(s, d->apcie_phy_sw, phy_val); - if (s->apcie_clkgen && sleep_ps_on(s, d->apcie_sys_st)) + if (s->apcie_clkgen && st_on) sleep_apcie_clk_off(s); - sleep_ps_lower(s, d->apcie_sys_st); + sleep_ps_lower_finish(s, d->apcie_sys_st, st_val); sleep_ps_lower(s, d->apcie_st); /* @@ -4601,12 +4630,11 @@ static void apple_sleep_lower_apcie(struct apple_sleep *s) * before clearing them: powered off with them set, SoC NI 1 latches a PCIe receive * link error once the block's last domain goes off. */ - val = sleep_ps_lower_start(s, d->apcie_sys_gp); - if (s->apcie_axi && sleep_ps_wait_on(s, d->apcie_sys_gp, val)) { + if (s->apcie_axi && gp_on) { writel(readl(s->apcie_axi + APCIE_AXI_EN0) & ~BIT(0), s->apcie_axi + APCIE_AXI_EN0); writel(readl(s->apcie_axi + APCIE_AXI_EN1) & ~BIT(0), s->apcie_axi + APCIE_AXI_EN1); } - sleep_ps_lower_finish(s, d->apcie_sys_gp, val); + sleep_ps_lower_finish(s, d->apcie_sys_gp, gp_val); sleep_ps_lower(s, d->apcie_gp); sleep_ps_lower(s, d->ans);