From a10defefab5d9585fa961f857e03e0873e522fc3 Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Mon, 9 Nov 2026 06:20:28 +1100 Subject: [PATCH] pmdomain: apple: clear a latched bit 11 with a PS reset on power-on On the Neo (T8140) ANE_SYS sometimes fails to come back on when genpd powers it on in the noirq resume of a system sleep: PS ane_sys: Failed to reach power state 0xf (requested 0x200f, now: 0x2b4f) Target 0xf, actual 0x4 (clock-gated), bit 11 set. genpd ignores the error there, the ANE finds ANE_SYS off at PM_POST_SUSPEND and quarantines itself until reboot, and every later power-on of the domain stops the same way. It happens in about one boot in four, within the first ten sleeps. Bit 11 is not a parent that comes up late here. A test kernel that kept watching the word saw no change for 20 ms; the fabric domains above ANE_SYS (0x1a8, 0x1e8, 0x228) and its siblings were all on. Gated again, the domain reports actual 0 at once, but bit 11 stays set (0x2b00), and the next power-on stops at 0x2b4f again. Writing bit 11 back does nothing. A PS reset pulse while the domain is off clears it (0x3300, then 0x2300), and the power-on then reaches 0x23ff in 8 us. So when a power-on times out with bit 11 set and the actual state short of the target, gate the domain, pulse the PS reset, release it and request the on state again, each step with the usual 100 us budget. The devices of the domain lose their state while it is off anyway. Domains whose bit 11 means a parent that is not up yet (wait_parent, T6000 GFX), strict-state and externally clocked domains are left as they were. A latch the reset does not clear still fails and logs. KUnit: four new cases (latch cleared, cleared in the noirq phase, a latch the reset does not clear, no reset for wait_parent domains or a plain stall). Signed-off-by: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Assisted-by: LLM --- drivers/pmdomain/apple/pmgr-pwrstate-test.c | 87 +++++++++++++++++++++ drivers/pmdomain/apple/pmgr-pwrstate.c | 53 +++++++++++++ 2 files changed, 140 insertions(+) diff --git a/drivers/pmdomain/apple/pmgr-pwrstate-test.c b/drivers/pmdomain/apple/pmgr-pwrstate-test.c index b73e2973cb6f1..b3a8cd1f5715c 100644 --- a/drivers/pmdomain/apple/pmgr-pwrstate-test.c +++ b/drivers/pmdomain/apple/pmgr-pwrstate-test.c @@ -15,6 +15,9 @@ struct pmgr_ps_test_reg { bool parent_read_error; unsigned int busy_writes; unsigned int auto_writes; /* writes that enabled automatic PM */ + bool latched; /* bit 11 latched: on stops at clock-gated, off still reaches 0 */ + bool latch_sticky; /* not even a PS reset clears it */ + unsigned int reset_writes; /* writes with the PS reset bit */ }; static int pmgr_ps_test_read(void *ctx, unsigned int reg, unsigned int *val) @@ -43,6 +46,19 @@ static int pmgr_ps_test_write(void *ctx, unsigned int reg, unsigned int val) u32 parent = r->val & APPLE_PMGR_BUSY; u32 actual; + if (val & APPLE_PMGR_PS_RESET) { + r->reset_writes++; + if (r->latched && !r->latch_sticky) { + r->latched = false; + parent = 0; + } + } + if (r->latched) { + actual = FIELD_GET(APPLE_PMGR_PS_TARGET, val) ? APPLE_PMGR_PS_CLKGATE : 0; + r->val = (val & ~(APPLE_PMGR_PS_ACTUAL | APPLE_PMGR_BUSY)) | APPLE_PMGR_BUSY | + FIELD_PREP(APPLE_PMGR_PS_ACTUAL, actual); + return 0; + } if (FIELD_GET(APPLE_PMGR_PS_TARGET, val) == APPLE_PMGR_PS_ACTIVE && (r->parent_stuck || r->parent_reads)) parent = APPLE_PMGR_BUSY; @@ -261,6 +277,73 @@ static void pmgr_ps_power_on_sleep_stuck_test(struct kunit *test) KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_PMGR_PS_TARGET, r->val), APPLE_PMGR_PS_ACTIVE); } +/* + * A latched bit 11 (T8140 ANE_SYS after sleep: 0x2b4f) is cleared by a PS reset + * while the domain is off, and the power-on then completes. + */ +static void pmgr_ps_power_on_latch_cleared_test(struct kunit *test) +{ + struct pmgr_ps_test_reg *r; + struct apple_pmgr_ps *ps; + + ps = pmgr_ps_test_domain(test, APPLE_PMGR_PS_PWRGATE, false, &r); + r->latched = true; + KUNIT_EXPECT_EQ(test, apple_pmgr_ps_power_on(&ps->genpd), 0); + KUNIT_EXPECT_EQ(test, r->reset_writes, 1U); + KUNIT_EXPECT_FALSE(test, r->val & (APPLE_PMGR_BUSY | APPLE_PMGR_PS_RESET)); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_PMGR_PS_TARGET, r->val), APPLE_PMGR_PS_ACTIVE); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_PMGR_PS_ACTUAL, r->val), APPLE_PMGR_PS_ACTIVE); + KUNIT_EXPECT_EQ(test, r->auto_writes, 1U); +} + +/* The same in the noirq phase of a system sleep, where genpd ignores errors. */ +static void pmgr_ps_power_on_latch_sleep_test(struct kunit *test) +{ + struct pmgr_ps_test_reg *r; + struct apple_pmgr_ps *ps; + + ps = pmgr_ps_test_domain(test, APPLE_PMGR_PS_PWRGATE, true, &r); + ps->system_sleep = true; + r->latched = true; + KUNIT_EXPECT_EQ(test, apple_pmgr_ps_power_on(&ps->genpd), 0); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_PMGR_PS_ACTUAL, r->val), APPLE_PMGR_PS_ACTIVE); + KUNIT_EXPECT_EQ(test, r->auto_writes, 0U); +} + +/* A latch the reset does not clear still fails, and the domain is requested off. */ +static void pmgr_ps_power_on_latch_sticky_test(struct kunit *test) +{ + struct pmgr_ps_test_reg *r; + struct apple_pmgr_ps *ps; + + ps = pmgr_ps_test_domain(test, APPLE_PMGR_PS_PWRGATE, true, &r); + r->latched = true; + r->latch_sticky = true; + KUNIT_EXPECT_EQ(test, apple_pmgr_ps_power_on(&ps->genpd), -ETIMEDOUT); + KUNIT_EXPECT_EQ(test, r->reset_writes, 1U); + KUNIT_EXPECT_FALSE(test, r->val & APPLE_PMGR_PS_RESET); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_PMGR_PS_TARGET, r->val), APPLE_PMGR_PS_PWRGATE); + KUNIT_EXPECT_EQ(test, r->auto_writes, 0U); +} + +/* No PS reset where bit 11 means a parent not up yet, nor on a plain stall. */ +static void pmgr_ps_power_on_no_reset_test(struct kunit *test) +{ + struct pmgr_ps_test_reg *r; + struct apple_pmgr_ps *ps; + + ps = pmgr_ps_test_domain(test, APPLE_PMGR_PS_PWRGATE, false, &r); + ps->wait_parent = true; + r->latched = true; + KUNIT_EXPECT_EQ(test, apple_pmgr_ps_power_on(&ps->genpd), -ETIMEDOUT); + KUNIT_EXPECT_EQ(test, r->reset_writes, 0U); + + ps = pmgr_ps_test_domain(test, APPLE_PMGR_PS_PWRGATE, true, &r); + r->stuck = true; + KUNIT_EXPECT_EQ(test, apple_pmgr_ps_power_on(&ps->genpd), -ETIMEDOUT); + KUNIT_EXPECT_EQ(test, r->reset_writes, 0U); +} + static struct kunit_case pmgr_ps_test_cases[] = { KUNIT_CASE(pmgr_ps_power_on_test), KUNIT_CASE(pmgr_ps_power_off_undo_test), @@ -272,6 +355,10 @@ static struct kunit_case pmgr_ps_test_cases[] = { KUNIT_CASE(pmgr_ps_power_on_parent_only_test), KUNIT_CASE(pmgr_ps_power_on_parent_read_error_test), KUNIT_CASE(pmgr_ps_power_on_sleep_stuck_test), + KUNIT_CASE(pmgr_ps_power_on_latch_cleared_test), + KUNIT_CASE(pmgr_ps_power_on_latch_sleep_test), + KUNIT_CASE(pmgr_ps_power_on_latch_sticky_test), + KUNIT_CASE(pmgr_ps_power_on_no_reset_test), {} }; diff --git a/drivers/pmdomain/apple/pmgr-pwrstate.c b/drivers/pmdomain/apple/pmgr-pwrstate.c index b84bfc4432111..0c825a64b42bd 100644 --- a/drivers/pmdomain/apple/pmgr-pwrstate.c +++ b/drivers/pmdomain/apple/pmgr-pwrstate.c @@ -84,6 +84,48 @@ static int apple_pmgr_ps_wait_parent(struct apple_pmgr_ps *ps, u32 *cur) return err ?: ret; } +/* + * A power-on can stop with the actual state short of the target and bit 11 + * latched: on the T8140 ANE_SYS after system sleep the word reads 0x2b4f + * (target 0xf, actual 0x4) and stays so. That is not a parent that comes up + * late: gated again, the domain reaches actual 0 at once, but bit 11 stays + * set and every later power-on stops the same way. Only a PS reset clears it. + * Power the domain off, pulse the PS reset while it is off (its devices lose + * their state there anyway), then request @reg, the on state, again. + * Domains whose bit 11 means a parent that is not up yet (wait_parent) and + * those with their own state contract are left alone. + */ +static int apple_pmgr_ps_clear_latch(struct apple_pmgr_ps *ps, u32 reg, u32 *cur) +{ + u32 off = (reg & ~APPLE_PMGR_PS_TARGET) | APPLE_PMGR_PS_PWRGATE; + int ret; + + if (ps->wait_parent || ps->strict_state || ps->externally_clocked || + !(*cur & APPLE_PMGR_BUSY) || + FIELD_GET(APPLE_PMGR_PS_ACTUAL, *cur) == APPLE_PMGR_PS_ACTIVE) + return -ETIMEDOUT; + + regmap_write(ps->regmap, ps->offset, off); + ret = regmap_read_poll_timeout_atomic(ps->regmap, ps->offset, *cur, + !FIELD_GET(APPLE_PMGR_PS_ACTUAL, *cur), 1, + APPLE_PMGR_PS_SET_TIMEOUT); + if (ret) + return ret; + regmap_write(ps->regmap, ps->offset, off | APPLE_PMGR_PS_RESET); + ret = regmap_read_poll_timeout_atomic(ps->regmap, ps->offset, *cur, + !(*cur & APPLE_PMGR_BUSY), 1, + APPLE_PMGR_PS_SET_TIMEOUT); + regmap_write(ps->regmap, ps->offset, off); + if (ret) + return ret; + regmap_write(ps->regmap, ps->offset, reg); + return regmap_read_poll_timeout_atomic(ps->regmap, ps->offset, *cur, + FIELD_GET(APPLE_PMGR_PS_ACTUAL, *cur) == + APPLE_PMGR_PS_ACTIVE && + !(*cur & APPLE_PMGR_BUSY), 1, + APPLE_PMGR_PS_SET_TIMEOUT); +} + static int apple_pmgr_ps_set(struct generic_pm_domain *genpd, u32 pstate, bool auto_enable) { int ret; @@ -174,6 +216,17 @@ static int apple_pmgr_ps_set(struct generic_pm_domain *genpd, u32 pstate, bool a return ret; } + if (ret < 0 && pstate == APPLE_PMGR_PS_ACTIVE) { + u32 stalled = cur; + + if (!apple_pmgr_ps_clear_latch(ps, reg, &cur)) { + dev_info_ratelimited(ps->dev, + "PS %s: power-on stopped at 0x%x with bit 11 latched; cleared by a PS reset\n", + genpd->name, stalled); + ret = 0; + } + } + report: if (ret < 0) dev_err_ratelimited(ps->dev,