From 416ba83b2e7387e0455dc84d0e0123e0ccae82de Mon Sep 17 00:00:00 2001 From: Jeff Queisser Date: Tue, 24 Nov 2026 01:32:46 -0800 Subject: [PATCH 1/4] dt-bindings: watchdog: apple,wdt: add optional simple-reconfig register Apple Silicon SoCs with a simple-reconfig watchdog (such as T6050 / M5 Ultra) route WD2's bark interrupt to a cold SoC reset via a separate AON routing register, rather than resetting through WD1's RESET_EN bit. Document an optional second reg entry ("reconfig") in apple,wdt.yaml and require reg-names when both "wdt" and "reconfig" regions are present, keeping existing single-reg nodes valid without reg-names. Signed-off-by: Jeff Queisser --- .../bindings/watchdog/apple,wdt.yaml | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/Documentation/devicetree/bindings/watchdog/apple,wdt.yaml b/Documentation/devicetree/bindings/watchdog/apple,wdt.yaml index 9a6634e146d75..f65a37d504791 100644 --- a/Documentation/devicetree/bindings/watchdog/apple,wdt.yaml +++ b/Documentation/devicetree/bindings/watchdog/apple,wdt.yaml @@ -11,6 +11,15 @@ maintainers: allOf: - $ref: watchdog.yaml# + - if: + properties: + reg: + minItems: 2 + required: + - reg + then: + required: + - reg-names properties: compatible: @@ -42,7 +51,16 @@ properties: - const: apple,wdt reg: - maxItems: 1 + minItems: 1 + items: + - description: Watchdog timer registers + - description: Simple-reconfig routing register + + reg-names: + minItems: 1 + items: + - const: wdt + - const: reconfig clocks: maxItems: 1 @@ -69,5 +87,17 @@ examples: clocks = <&clk>; interrupts = ; }; + - | + #include + #include + + watchdog@8836c000 { + compatible = "apple,t6050-wdt", "apple,t8103-wdt"; + reg = <0x8836c000 0x4000>, + <0x8833002c 0x4>; + reg-names = "wdt", "reconfig"; + clocks = <&clk>; + interrupts = ; + }; ... From 9927b97fc39038785e6bb33c86510ffd90c3ddb6 Mon Sep 17 00:00:00 2001 From: Jeff Queisser Date: Tue, 24 Nov 2026 01:32:52 -0800 Subject: [PATCH 2/4] soc: apple: sleep: hand off WD2 to apple_wdt and preserve T6050 reconfig across wake drivers/soc/apple/sleep.c uses WD2 as its suspend/resume safety net between apple_sleep_begin() and apple_sleep_end(). Once apple_wdt also drives WD2 on SoCs with a simple-reconfig routing register, the two drivers must coordinate ownership so apple_wdt_suspend() does not disarm sleep's 120 s safety net and apple_sleep_end() restores an active apple_wdt timer after clearing sleep's WD2 timer. Add a lock-protected WD2 handoff API in include/linux/soc/apple/sleep.h (with inline no-op stubs when CONFIG_APPLE_SOC_SLEEP=n): - apple_sleep_wdt_register(dev, wdt, restore, data) matches @wdt's resource start against available apple_sleep DT nodes' "wdt" regions regardless of probe order; watchdogs without a matching sleep node (such as J775D or a secondary die) return 0 without taking the slot, while a second device claiming the same block returns -EBUSY. - apple_sleep_wdt_active(dev) reports whether sleep currently owns WD2 for @dev so only that watchdog yields during [begin .. end]. - apple_sleep_end() clears WD2_CTL, invokes @restore while holding the handoff lock, and clears ownership. On T6050, apple,seq-commit-wdt writes 0x20 (sleep-commit routing) to wdt-aux + AON_WDT_SLEEP (0x2c) during commit without restoring it on wake. Bracket t6050_seq(T6050_SEQ_WDT) using struct apple_sleep's existing aon_wdt_saved/aon_wdt_marked fields (matching T8142): save wdt-aux + AON_WDT_SLEEP before commit, and in t6050_sleep_wake_regs() stop WD2, W1C-clear IRQ_STATUS with a readl() barrier, restore the saved routing word with a readl() barrier, and re-arm the 120 s wake net. Leave T8142's aon_wdt_sleep OR path unchanged (T8142 has no "reconfig" DT node in this series) and leave shared apple_sleep_set_wd2() unchanged (empirical J775D register checks confirm a stopped WD2 does not latch IRQ_STATUS past bark). Add isolated KUnit coverage for block matching, slot ownership, and .end ordering in sleep-test.c. Signed-off-by: Jeff Queisser --- drivers/soc/apple/sleep-t6050.h | 35 ++++++-- drivers/soc/apple/sleep-test.c | 131 ++++++++++++++++++++++++++++++ drivers/soc/apple/sleep.c | 138 ++++++++++++++++++++++++++++++-- include/linux/soc/apple/sleep.h | 28 +++++++ 4 files changed, 321 insertions(+), 11 deletions(-) diff --git a/drivers/soc/apple/sleep-t6050.h b/drivers/soc/apple/sleep-t6050.h index a4bf3fabcf375..e57a42caa4e14 100644 --- a/drivers/soc/apple/sleep-t6050.h +++ b/drivers/soc/apple/sleep-t6050.h @@ -3123,6 +3123,33 @@ static void t6050_clusters_down_first_report(struct apple_sleep *s) r->early_seq[1], r->early_us[1], r->early_ret[2]); } +static void t6050_sleep_commit_wdt(struct apple_sleep *s) +{ + struct apple_sleep_region_state *r = s->region; + + if (r && r->seq[T6050_SEQ_WDT].n) { + s->aon_wdt_saved = readl(s->wdt_aux + AON_WDT_SLEEP); + s->aon_wdt_marked = true; + } + t6050_seq(s, T6050_SEQ_WDT); + writel(0, s->wdt + WDT_WD0_CTL); + apple_sleep_set_wd2(s, T6050_WDT_COMMIT_SECS); + writel(0, s->wdt_aux + AON_AP_DEADLINE); +} + +static void t6050_sleep_wake_wdt(struct apple_sleep *s) +{ + writel(0, s->wdt + WDT_WD2_CTL); + writel(WDT_WD2_CTL_IRQ_STATUS, s->wdt + WDT_WD2_CTL); + (void)readl(s->wdt + WDT_WD2_CTL); + if (s->aon_wdt_marked) { + writel(s->aon_wdt_saved, s->wdt_aux + AON_WDT_SLEEP); + (void)readl(s->wdt_aux + AON_WDT_SLEEP); + s->aon_wdt_marked = false; + } + apple_sleep_set_wd2(s, WDT_SUSPEND_SECS); +} + /* * The commit window, each step at the DT schedule's time before the sleep timer: the MCC * requests released, the storage link's PHY parked and its words down, the watchdogs, the @@ -3175,10 +3202,7 @@ static void t6050_sleep_commit_regs(struct apple_sleep *s) } } t6050_step(s, T6050_STEP_WDT); - t6050_seq(s, T6050_SEQ_WDT); - writel(0, s->wdt + WDT_WD0_CTL); - apple_sleep_set_wd2(s, T6050_WDT_COMMIT_SECS); - writel(0, s->wdt_aux + 8); + t6050_sleep_commit_wdt(s); /* The memory floor's voter to 0, not read */ t6050_step(s, T6050_STEP_FLOOR); s->region->floor_changed |= BIT(1); @@ -3796,8 +3820,7 @@ static void t6050_sleep_wake_regs(struct apple_sleep *s) r->gfx_aic[2] = readl(r->aic_gfx + 0x14); } /* The complexes' requests back, in macOS's early wake order */ - writel(0, s->wdt + WDT_WD2_CTL); - apple_sleep_set_wd2(s, WDT_SUSPEND_SECS); + t6050_sleep_wake_wdt(s); for (i = 0; i < 3; i++) writel(r->cluster_saved[i], s->cluster + 4 * i); writel(PWRGATE_ON, s->pwrgate); diff --git a/drivers/soc/apple/sleep-test.c b/drivers/soc/apple/sleep-test.c index fff01f6447e5a..56df6219d5677 100644 --- a/drivers/soc/apple/sleep-test.c +++ b/drivers/soc/apple/sleep-test.c @@ -364,6 +364,136 @@ static void apple_sleep_test_region_rejects_bad_inventory(struct kunit *test) KUNIT_EXPECT_EQ(test, sleep_region_validate(&p), -EINVAL); } +struct apple_sleep_test_wdt_saved { + struct device *dev; + void (*restore)(void *data); + void *data; + bool owned; +}; + +struct apple_sleep_test_wdt_ctx { + u32 *wdt_regs; + int calls; + u32 ctl_at_restore; + bool owned_at_restore; +}; + +struct apple_sleep_test_wdt_state { + struct device dev1; + struct device dev2; + u32 fake_wdt[16]; + struct apple_sleep_test_wdt_saved saved; +}; + +static void apple_sleep_test_wdt_restore_global(void *data) +{ + struct apple_sleep_test_wdt_saved *saved = data; + unsigned long flags; + + spin_lock_irqsave(&apple_sleep_wdt_lock, flags); + apple_sleep_wdt_dev = saved->dev; + apple_sleep_wdt_restore = saved->restore; + apple_sleep_wdt_data = saved->data; + apple_sleep_wd2_owned = saved->owned; + spin_unlock_irqrestore(&apple_sleep_wdt_lock, flags); +} + +static void apple_sleep_test_wdt_restore_cb(void *data) +{ + struct apple_sleep_test_wdt_ctx *ctx = data; + + ctx->calls++; + ctx->ctl_at_restore = ctx->wdt_regs[WDT_WD2_CTL / 4]; + ctx->owned_at_restore = apple_sleep_wd2_owned; +} + +static void apple_sleep_test_wdt_handoff(struct kunit *test) +{ + struct resource t6050_sleep_wdt = DEFINE_RES_MEM(0x28836c000ULL, 0x4000); + struct resource t6050_dev_wdt = DEFINE_RES_MEM(0x28836c000ULL, 0x4000); + struct resource t8142_sleep_wdt = DEFINE_RES_MEM(0x23d2b0000ULL, 0x30); + struct resource t8142_dev_wdt = DEFINE_RES_MEM(0x23d2b0000ULL, 0x4000); + struct resource die1_wdt = DEFINE_RES_MEM(0x228836c000ULL, 0x4000); + struct apple_sleep_test_wdt_state *st; + struct apple_sleep_test_wdt_ctx ctx; + unsigned long flags; + + st = kunit_kzalloc(test, sizeof(*st), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, st); + + /* Isolate global handoff state so a probed apple_wdt on J714S/J716C is untouched. */ + spin_lock_irqsave(&apple_sleep_wdt_lock, flags); + st->saved.dev = apple_sleep_wdt_dev; + st->saved.restore = apple_sleep_wdt_restore; + st->saved.data = apple_sleep_wdt_data; + st->saved.owned = apple_sleep_wd2_owned; + apple_sleep_wdt_dev = NULL; + apple_sleep_wdt_restore = NULL; + apple_sleep_wdt_data = NULL; + apple_sleep_wd2_owned = false; + spin_unlock_irqrestore(&apple_sleep_wdt_lock, flags); + KUNIT_ASSERT_EQ(test, + kunit_add_action_or_reset(test, apple_sleep_test_wdt_restore_global, + &st->saved), 0); + + ctx = (struct apple_sleep_test_wdt_ctx){ .wdt_regs = st->fake_wdt }; + + /* Block matching compares resource start across differing map sizes. */ + KUNIT_EXPECT_TRUE(test, apple_sleep_wdt_same_block(&t6050_sleep_wdt, &t6050_dev_wdt)); + KUNIT_EXPECT_TRUE(test, apple_sleep_wdt_same_block(&t8142_sleep_wdt, &t8142_dev_wdt)); + KUNIT_EXPECT_FALSE(test, apple_sleep_wdt_same_block(&t6050_sleep_wdt, &die1_wdt)); + KUNIT_EXPECT_FALSE(test, apple_sleep_wdt_same_block(NULL, &t6050_dev_wdt)); + KUNIT_EXPECT_FALSE(test, apple_sleep_wdt_same_block(&t6050_sleep_wdt, NULL)); + + /* Invalid arguments and unmatched watchdogs. */ + KUNIT_EXPECT_EQ(test, apple_sleep_wdt_register_matched(NULL, true, + apple_sleep_test_wdt_restore_cb, + &ctx), -EINVAL); + KUNIT_EXPECT_EQ(test, apple_sleep_wdt_register_matched(&st->dev1, true, NULL, &ctx), + -EINVAL); + KUNIT_EXPECT_EQ(test, apple_sleep_wdt_register_matched(&st->dev2, false, + apple_sleep_test_wdt_restore_cb, + &ctx), 0); + KUNIT_EXPECT_FALSE(test, apple_sleep_wdt_active(&st->dev2)); + + /* Matching watchdog claims the single slot; a second claiming it gets -EBUSY. */ + KUNIT_EXPECT_EQ(test, apple_sleep_wdt_register_matched(&st->dev1, true, + apple_sleep_test_wdt_restore_cb, + &ctx), 0); + KUNIT_EXPECT_EQ(test, apple_sleep_wdt_register_matched(&st->dev2, true, + apple_sleep_test_wdt_restore_cb, + &ctx), -EBUSY); + KUNIT_EXPECT_EQ(test, apple_sleep_wdt_register_matched(&st->dev2, false, + apple_sleep_test_wdt_restore_cb, + &ctx), 0); + KUNIT_EXPECT_FALSE(test, apple_sleep_wdt_active(&st->dev1)); + + /* Ownership applies only to the registered watchdog; .end orders CTL=0, restore, clear. */ + spin_lock_irqsave(&apple_sleep_wdt_lock, flags); + apple_sleep_wd2_owned = true; + spin_unlock_irqrestore(&apple_sleep_wdt_lock, flags); + KUNIT_EXPECT_TRUE(test, apple_sleep_wdt_active(&st->dev1)); + KUNIT_EXPECT_FALSE(test, apple_sleep_wdt_active(&st->dev2)); + + st->fake_wdt[WDT_WD2_CTL / 4] = 1; + apple_sleep_wdt_end((void __iomem __force *)st->fake_wdt); + KUNIT_EXPECT_EQ(test, ctx.calls, 1); + KUNIT_EXPECT_EQ(test, ctx.ctl_at_restore, 0u); + KUNIT_EXPECT_TRUE(test, ctx.owned_at_restore); + KUNIT_EXPECT_FALSE(test, apple_sleep_wdt_active(&st->dev1)); + + /* Unregistering a non-holder leaves the slot intact; unregistering the holder frees it. */ + apple_sleep_wdt_unregister(&st->dev2); + KUNIT_EXPECT_EQ(test, apple_sleep_wdt_register_matched(&st->dev2, true, + apple_sleep_test_wdt_restore_cb, + &ctx), -EBUSY); + apple_sleep_wdt_unregister(&st->dev1); + KUNIT_EXPECT_EQ(test, apple_sleep_wdt_register_matched(&st->dev2, true, + apple_sleep_test_wdt_restore_cb, + &ctx), 0); + apple_sleep_wdt_unregister(&st->dev2); +} + static struct kunit_case apple_sleep_test_cases[] = { KUNIT_CASE(apple_sleep_test_layout), KUNIT_CASE(apple_sleep_test_t6050), @@ -377,6 +507,7 @@ static struct kunit_case apple_sleep_test_cases[] = { KUNIT_CASE(apple_sleep_test_words), KUNIT_CASE(apple_sleep_test_books), KUNIT_CASE(apple_sleep_test_wake_names), + KUNIT_CASE(apple_sleep_test_wdt_handoff), {} }; diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 9cee856626f91..bfad9de85080b 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -140,6 +140,7 @@ struct aura_s2r_mailbox { #define WDT_WD2_COUNT 0x20 #define WDT_WD2_ALARM 0x28 #define WDT_WD2_CTL 0x2c +#define WDT_WD2_CTL_IRQ_STATUS BIT(1) #define WDT_TICKS_PER_S 24000000 /* The sleep's own safety net: ADT /arm-io/wdt simple-reconfig-wdog-icc-time. */ #define WDT_SLEEP_SECS 5 @@ -148,9 +149,10 @@ struct aura_s2r_mailbox { #define AON_AP_DEADLINE 0x08 /* - * T8142: a word of the watchdog's second page (/arm-io/wdt + 0x802c). macOS's entry - * read-modify-writes it from 0 to 0x20 right before WD0's control and WD2 (26B131 trace, - * 11.1 ms before the S2R timer); XNU writes 0x10 there during boot. Meaning unknown. + * Simple-reconfig WD2 routing word (bits 5:4: 01 = run-time cold reset, 10 = sleep-commit + * routing). Lives at wdt-aux + 0x2c on T6050 and aon-misc + 0x2c (/arm-io/wdt + 0x802c) on + * T8142; sleep saves it before commit (T6050 DT sequence writes 0x20, T8142 ORs 0x20) and + * restores the saved value on wake. */ #define AON_WDT_SLEEP 0x2c @@ -1860,6 +1862,125 @@ void apple_sleep_smc_unregister(struct device *dev) } EXPORT_SYMBOL_GPL(apple_sleep_smc_unregister); +static const struct of_device_id apple_sleep_of_match[]; + +static DEFINE_SPINLOCK(apple_sleep_wdt_lock); +static struct device *apple_sleep_wdt_dev; +static void (*apple_sleep_wdt_restore)(void *data); +static void *apple_sleep_wdt_data; +static bool apple_sleep_wd2_owned; + +static bool apple_sleep_wdt_same_block(const struct resource *sleep_wdt, + const struct resource *wdt) +{ + if (!sleep_wdt || !wdt) + return false; + return sleep_wdt->start == wdt->start; +} + +static bool apple_sleep_dt_owns_wdt(const struct resource *wdt) +{ + struct device_node *np = NULL; + bool found = false; + + if (!wdt) + return false; + + for_each_matching_node(np, apple_sleep_of_match) { + struct resource res; + int idx; + + if (!of_device_is_available(np)) + continue; + idx = of_property_match_string(np, "reg-names", "wdt"); + if (idx < 0 || of_address_to_resource(np, idx, &res)) + continue; + if (apple_sleep_wdt_same_block(&res, wdt)) { + found = true; + of_node_put(np); + break; + } + } + + return found; +} + +static int apple_sleep_wdt_register_matched(struct device *dev, bool matched, + void (*restore)(void *data), void *data) +{ + unsigned long flags; + + if (!dev || !restore) + return -EINVAL; + if (!matched) + return 0; + + spin_lock_irqsave(&apple_sleep_wdt_lock, flags); + if (apple_sleep_wdt_dev && apple_sleep_wdt_dev != dev) { + spin_unlock_irqrestore(&apple_sleep_wdt_lock, flags); + return -EBUSY; + } + apple_sleep_wdt_dev = dev; + apple_sleep_wdt_restore = restore; + apple_sleep_wdt_data = data; + spin_unlock_irqrestore(&apple_sleep_wdt_lock, flags); + + return 0; +} + +int apple_sleep_wdt_register(struct device *dev, const struct resource *wdt, + void (*restore)(void *data), void *data) +{ + if (!dev || !wdt || !restore) + return -EINVAL; + + return apple_sleep_wdt_register_matched(dev, apple_sleep_dt_owns_wdt(wdt), + restore, data); +} +EXPORT_SYMBOL_GPL(apple_sleep_wdt_register); + +void apple_sleep_wdt_unregister(struct device *dev) +{ + unsigned long flags; + + spin_lock_irqsave(&apple_sleep_wdt_lock, flags); + if (apple_sleep_wdt_dev == dev) { + apple_sleep_wdt_dev = NULL; + apple_sleep_wdt_restore = NULL; + apple_sleep_wdt_data = NULL; + } + spin_unlock_irqrestore(&apple_sleep_wdt_lock, flags); +} +EXPORT_SYMBOL_GPL(apple_sleep_wdt_unregister); + +bool apple_sleep_wdt_active(struct device *dev) +{ + unsigned long flags; + bool active; + + if (!dev) + return false; + + spin_lock_irqsave(&apple_sleep_wdt_lock, flags); + active = apple_sleep_wd2_owned && apple_sleep_wdt_dev == dev; + spin_unlock_irqrestore(&apple_sleep_wdt_lock, flags); + + return active; +} +EXPORT_SYMBOL_GPL(apple_sleep_wdt_active); + +static void apple_sleep_wdt_end(void __iomem *wdt) +{ + unsigned long flags; + + spin_lock_irqsave(&apple_sleep_wdt_lock, flags); + writel(0, wdt + WDT_WD2_CTL); + if (apple_sleep_wdt_restore) + apple_sleep_wdt_restore(apple_sleep_wdt_data); + apple_sleep_wd2_owned = false; + spin_unlock_irqrestore(&apple_sleep_wdt_lock, flags); +} + void apple_sleep_display_quiesced(struct device *dev, bool quiesced) { struct apple_sleep *s = apple_sleep; @@ -6462,7 +6583,14 @@ static int apple_sleep_begin(suspend_state_t state) apple_sleep->smc_woke_alone = false; sleep_pins_save(apple_sleep); } - apple_sleep_set_wd2(apple_sleep, WDT_SUSPEND_SECS); + { + unsigned long flags; + + spin_lock_irqsave(&apple_sleep_wdt_lock, flags); + apple_sleep_wd2_owned = true; + apple_sleep_set_wd2(apple_sleep, WDT_SUSPEND_SECS); + spin_unlock_irqrestore(&apple_sleep_wdt_lock, flags); + } pm_set_suspend_via_firmware(); sleep_ans_late_open(); @@ -6480,7 +6608,7 @@ static void apple_sleep_end(void) if (pm_suspend_via_firmware() && apple_sleep->soc->ops->pmu_wake) apple_sleep->soc->ops->pmu_wake(apple_sleep); if (pm_suspend_via_firmware()) - writel(0, apple_sleep->wdt + WDT_WD2_CTL); + apple_sleep_wdt_end(apple_sleep->wdt); } /* diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index db4337d663b81..5ae87fdff0f9d 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -13,6 +13,7 @@ struct device; struct device_node; +struct resource; /* The SMC's sleep messages: the pmVC value and the spacings between them, used as floors */ struct apple_sleep_smc_policy { @@ -256,6 +257,18 @@ void apple_sleep_restore_pins(void); * sleep engine's PMGR reset of its own domains, its DARTs kept. The caller starts it after. */ int apple_sleep_engine_reset(struct device *dev); + +/* + * Watchdog handoff over a firmware-driven suspend. Between .begin and .end the + * sleep driver owns WD2 as its suspend/resume safety net; at .end it clears WD2 + * and invokes @restore so a watchdog driver sharing that WD2 block can re-arm + * its active timer. Registration matches @wdt against any available sleep node's + * "wdt" region; watchdogs without a matching sleep node return 0 without registering. + */ +int apple_sleep_wdt_register(struct device *dev, const struct resource *wdt, + void (*restore)(void *data), void *data); +void apple_sleep_wdt_unregister(struct device *dev); +bool apple_sleep_wdt_active(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; } @@ -463,6 +476,21 @@ static inline int apple_sleep_engine_reset(struct device *dev) { return -EOPNOTSUPP; } + +static inline int apple_sleep_wdt_register(struct device *dev, const struct resource *wdt, + void (*restore)(void *data), void *data) +{ + return 0; +} + +static inline void apple_sleep_wdt_unregister(struct device *dev) +{ +} + +static inline bool apple_sleep_wdt_active(struct device *dev) +{ + return false; +} #endif #endif From 0897a4c2b35e788e610157500672252e6cdbba70 Mon Sep 17 00:00:00 2001 From: Jeff Queisser Date: Tue, 24 Nov 2026 01:53:04 -0800 Subject: [PATCH 3/4] watchdog: apple: use WD2 simple-reconfig so expiry and reboot reset the SoC On Apple Silicon SoCs whose watchdog DT node provides the optional "reconfig" routing register (such as T6050 / M5 Ultra), legacy WD1 RESET_EN stalls the SoC instead of performing a cold reset. Routing WD2's bark interrupt via bits 5:4 = 01 of the "reconfig" register (with WD2_BITE = U32_MAX and WD2_CTRL = IRQ_EN) triggers a clean cold reset on both watchdog expiry and apple_wdt_restart(). When "reconfig" (>= 4 bytes, mapped non-exclusively via devm_ioremap) is present: - Register the apple_sleep WD2 handoff (matching the watchdog's reg[0] resource) before touching any watchdog registers so a registration error writes zero registers and devm unregisters the watchdog before the sleep handoff. - Unify all four WD2 arming paths (start(), restart(), pre-probe takeover, and apple_wdt_sleep_restore()) into apple_wdt_wd2_arm(): CTRL=0, BITE=U32_MAX, CUR=0, BARK=ticks, W1C clear IRQ_STATUS, a readl(WD2_CTRL) ordering barrier ahead of the separate reconfig mapping, apple_wdt_enable_reconfig() (with readback), and CTRL=IRQ_EN. - Unify pre-probe takeover: always clear WD1_CTRL, and when either a legacy boot-loader WD1 (RESET_EN) or WD2 (IRQ_EN or RESET_EN) was armed before probe (outside kdump), set WDOG_HW_RUNNING before calling apple_wdt_wd2_arm() so a watchdog already armed at probe stays armed and resets cleanly instead of stalling or being dropped. - Derive the 40 ms reboot bark delay from clk_rate and yield WD2 to apple_sleep when apple_sleep_wdt_active(dev) is true. Single-reg DT nodes keep existing WD1 behavior unchanged aside from guarding apple_wdt_get_timeleft() against cur_time >= reset_time underflow on both WD1 and WD2. Add tools/asahi/apple-wdt-tests.py (registered in run-host-tests.py) compiling the real driver body and T6050 commit/wake WDT helpers under ASan/UBSan with --old-ref 70a00e583b032 and eight negative mutations. Signed-off-by: Jeff Queisser --- drivers/watchdog/apple_wdt.c | 214 ++++++- tools/asahi/apple-wdt-tests.py | 1017 ++++++++++++++++++++++++++++++++ tools/asahi/run-host-tests.py | 7 + 3 files changed, 1211 insertions(+), 27 deletions(-) create mode 100755 tools/asahi/apple-wdt-tests.py diff --git a/drivers/watchdog/apple_wdt.c b/drivers/watchdog/apple_wdt.c index 3c867a3ce636e..1300a00d5cd65 100644 --- a/drivers/watchdog/apple_wdt.c +++ b/drivers/watchdog/apple_wdt.c @@ -12,28 +12,32 @@ #include #include #include +#include #include #include #include +#include #include /* * Apple Watchdog MMIO registers * - * This HW block has three separate watchdogs. WD0 resets the machine - * to recovery mode and is not very useful for us. WD1 and WD2 trigger a normal - * machine reset. WD0 additionally supports a configurable interrupt. - * This information can be used to implement pretimeout support at a later time. + * This HW block has three separate watchdogs. WD0 resets the machine to + * recovery mode and is not used by Linux. On legacy wdt-version 2 platforms + * WD1 triggers a normal machine reset via APPLE_WDT_CTRL_RESET_EN when + * CUR_TIME >= BITE_TIME. + * + * On platforms that provide the optional "reconfig" routing register + * (simple-reconfig-wdog-support), WD1/WD2 hardware RESET_EN does not reset + * reliably; instead, setting bits 5:4 = 01 in the reconfig register routes + * WD2's bark interrupt (CUR_TIME >= BARK_TIME with APPLE_WDT_CTRL_IRQ_EN + * set and BITE_TIME = U32_MAX) to a clean cold reset. * * APPLE_WDT_WDx_CUR_TIME is a simple counter incremented for each tick of the * reference clock. It can also be overwritten to any value. - * Whenever APPLE_WDT_CTRL_RESET_EN is set in APPLE_WDT_WDx_CTRL and - * APPLE_WDT_WDx_CUR_TIME >= APPLE_WDT_WDx_BITE_TIME the entire machine is - * reset. - * Whenever APPLE_WDT_CTRL_IRQ_EN is set and APPLE_WDTx_WD1_CUR_TIME >= - * APPLE_WDTx_WD1_BARK_TIME an interrupt is triggered and - * APPLE_WDT_CTRL_IRQ_STATUS is set. The interrupt can be cleared by writing - * 1 to APPLE_WDT_CTRL_IRQ_STATUS. + * Whenever APPLE_WDT_CTRL_IRQ_EN is set and CUR_TIME >= BARK_TIME an interrupt + * is triggered and APPLE_WDT_CTRL_IRQ_STATUS is set. The interrupt can be + * cleared by writing 1 to APPLE_WDT_CTRL_IRQ_STATUS. */ #define APPLE_WDT_WD0_CUR_TIME 0x00 #define APPLE_WDT_WD0_BITE_TIME 0x04 @@ -46,17 +50,26 @@ #define APPLE_WDT_WD2_CUR_TIME 0x20 #define APPLE_WDT_WD2_BITE_TIME 0x24 +#define APPLE_WDT_WD2_BARK_TIME 0x28 #define APPLE_WDT_WD2_CTRL 0x2c #define APPLE_WDT_CTRL_IRQ_EN BIT(0) #define APPLE_WDT_CTRL_IRQ_STATUS BIT(1) #define APPLE_WDT_CTRL_RESET_EN BIT(2) +/* Optional "reconfig" register: bits 5:4 = 01 routes WD2 bark to cold reset */ +#define APPLE_WDT_RECONFIG_MASK GENMASK(5, 4) +#define APPLE_WDT_RECONFIG_WD2 BIT(4) + +/* ~40 ms bark delay before simple-reconfig cold reset */ +#define APPLE_WDT_REBOOT_DELAY_MS 40 + #define APPLE_WDT_TIMEOUT_DEFAULT 30 struct apple_wdt { struct watchdog_device wdd; void __iomem *regs; + void __iomem *reconfig; unsigned long clk_rate; }; @@ -65,10 +78,52 @@ static struct apple_wdt *to_apple_wdt(struct watchdog_device *wdd) return container_of(wdd, struct apple_wdt, wdd); } +static u32 apple_wdt_timeout_ticks(struct apple_wdt *wdt, unsigned int s) +{ + u32 actual = min(s, wdt->wdd.max_hw_heartbeat_ms / 1000); + + return wdt->clk_rate * actual; +} + +static void apple_wdt_enable_reconfig(struct apple_wdt *wdt) +{ + u32 val = readl_relaxed(wdt->reconfig); + + writel_relaxed((val & ~APPLE_WDT_RECONFIG_MASK) | APPLE_WDT_RECONFIG_WD2, + wdt->reconfig); + (void)readl(wdt->reconfig); +} + +static void apple_wdt_wd2_arm(struct apple_wdt *wdt, u32 bark_ticks) +{ + /* + * Disarm WD2, program safe BITE/CUR/BARK, and clear any latched + * IRQ_STATUS before routing WD2 bark to cold reset and enabling IRQ_EN. + * Read back WD2_CTRL to order the watchdog-block writes before touching + * the separate AON reconfig mapping; apple_wdt_enable_reconfig() reads + * back reconfig before the final IRQ_EN write. + */ + writel_relaxed(0, wdt->regs + APPLE_WDT_WD2_CTRL); + writel_relaxed(U32_MAX, wdt->regs + APPLE_WDT_WD2_BITE_TIME); + writel_relaxed(0, wdt->regs + APPLE_WDT_WD2_CUR_TIME); + writel_relaxed(bark_ticks, wdt->regs + APPLE_WDT_WD2_BARK_TIME); + writel_relaxed(APPLE_WDT_CTRL_IRQ_STATUS, + wdt->regs + APPLE_WDT_WD2_CTRL); + (void)readl(wdt->regs + APPLE_WDT_WD2_CTRL); + apple_wdt_enable_reconfig(wdt); + writel_relaxed(APPLE_WDT_CTRL_IRQ_EN, wdt->regs + APPLE_WDT_WD2_CTRL); +} + static int apple_wdt_start(struct watchdog_device *wdd) { struct apple_wdt *wdt = to_apple_wdt(wdd); + if (wdt->reconfig) { + apple_wdt_wd2_arm(wdt, + apple_wdt_timeout_ticks(wdt, wdd->timeout)); + return 0; + } + writel_relaxed(0, wdt->regs + APPLE_WDT_WD1_CUR_TIME); writel_relaxed(APPLE_WDT_CTRL_RESET_EN, wdt->regs + APPLE_WDT_WD1_CTRL); @@ -78,8 +133,9 @@ static int apple_wdt_start(struct watchdog_device *wdd) static int apple_wdt_stop(struct watchdog_device *wdd) { struct apple_wdt *wdt = to_apple_wdt(wdd); + u32 ctrl_off = wdt->reconfig ? APPLE_WDT_WD2_CTRL : APPLE_WDT_WD1_CTRL; - writel_relaxed(0, wdt->regs + APPLE_WDT_WD1_CTRL); + writel_relaxed(0, wdt->regs + ctrl_off); return 0; } @@ -87,8 +143,13 @@ static int apple_wdt_stop(struct watchdog_device *wdd) static int apple_wdt_ping(struct watchdog_device *wdd) { struct apple_wdt *wdt = to_apple_wdt(wdd); + u32 cur_off; - writel_relaxed(0, wdt->regs + APPLE_WDT_WD1_CUR_TIME); + if (wdt->reconfig && apple_sleep_wdt_active(wdd->parent)) + return 0; + + cur_off = wdt->reconfig ? APPLE_WDT_WD2_CUR_TIME : APPLE_WDT_WD1_CUR_TIME; + writel_relaxed(0, wdt->regs + cur_off); return 0; } @@ -96,12 +157,15 @@ static int apple_wdt_ping(struct watchdog_device *wdd) static int apple_wdt_set_timeout(struct watchdog_device *wdd, unsigned int s) { struct apple_wdt *wdt = to_apple_wdt(wdd); - u32 actual; - - writel_relaxed(0, wdt->regs + APPLE_WDT_WD1_CUR_TIME); - - actual = min(s, wdd->max_hw_heartbeat_ms / 1000); - writel_relaxed(wdt->clk_rate * actual, wdt->regs + APPLE_WDT_WD1_BITE_TIME); + u32 ticks = apple_wdt_timeout_ticks(wdt, s); + + if (wdt->reconfig) { + writel_relaxed(0, wdt->regs + APPLE_WDT_WD2_CUR_TIME); + writel_relaxed(ticks, wdt->regs + APPLE_WDT_WD2_BARK_TIME); + } else { + writel_relaxed(0, wdt->regs + APPLE_WDT_WD1_CUR_TIME); + writel_relaxed(ticks, wdt->regs + APPLE_WDT_WD1_BITE_TIME); + } wdd->timeout = s; @@ -113,8 +177,16 @@ static unsigned int apple_wdt_get_timeleft(struct watchdog_device *wdd) struct apple_wdt *wdt = to_apple_wdt(wdd); u32 cur_time, reset_time; - cur_time = readl_relaxed(wdt->regs + APPLE_WDT_WD1_CUR_TIME); - reset_time = readl_relaxed(wdt->regs + APPLE_WDT_WD1_BITE_TIME); + if (wdt->reconfig) { + cur_time = readl_relaxed(wdt->regs + APPLE_WDT_WD2_CUR_TIME); + reset_time = readl_relaxed(wdt->regs + APPLE_WDT_WD2_BARK_TIME); + } else { + cur_time = readl_relaxed(wdt->regs + APPLE_WDT_WD1_CUR_TIME); + reset_time = readl_relaxed(wdt->regs + APPLE_WDT_WD1_BITE_TIME); + } + + if (cur_time >= reset_time) + return 0; return (reset_time - cur_time) / wdt->clk_rate; } @@ -124,6 +196,22 @@ static int apple_wdt_restart(struct watchdog_device *wdd, unsigned long mode, { struct apple_wdt *wdt = to_apple_wdt(wdd); + if (wdt->reconfig) { + u32 ticks = max_t(u32, 1, + div_u64((u64)wdt->clk_rate * APPLE_WDT_REBOOT_DELAY_MS, + MSEC_PER_SEC)); + + apple_wdt_wd2_arm(wdt, ticks); + + /* + * Flush writes and wait for the bark delay (~40 ms) plus SoC + * reset latency (~125 ms) before returning from restart. + */ + (void)readl(wdt->regs + APPLE_WDT_WD2_CUR_TIME); + mdelay(200); + return 0; + } + writel_relaxed(APPLE_WDT_CTRL_RESET_EN, wdt->regs + APPLE_WDT_WD1_CTRL); writel_relaxed(0, wdt->regs + APPLE_WDT_WD1_BITE_TIME); writel_relaxed(0, wdt->regs + APPLE_WDT_WD1_CUR_TIME); @@ -155,21 +243,46 @@ static const struct watchdog_info apple_wdt_info = { .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT, }; +static void apple_wdt_sleep_restore(void *data) +{ + struct apple_wdt *wdt = data; + + if (watchdog_active(&wdt->wdd) || watchdog_hw_running(&wdt->wdd)) + apple_wdt_start(&wdt->wdd); +} + +static void apple_wdt_sleep_unregister(void *data) +{ + apple_sleep_wdt_unregister(data); +} + static int apple_wdt_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; + struct resource *wdt_res, *reconfig_res; struct apple_wdt *wdt; struct clk *clk; u32 wdt_ctrl; + int ret; wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL); if (!wdt) return -ENOMEM; - wdt->regs = devm_platform_ioremap_resource(pdev, 0); + wdt->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &wdt_res); if (IS_ERR(wdt->regs)) return PTR_ERR(wdt->regs); + reconfig_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "reconfig"); + if (reconfig_res) { + if (resource_size(reconfig_res) < sizeof(u32)) + return -EINVAL; + wdt->reconfig = devm_ioremap(dev, reconfig_res->start, + resource_size(reconfig_res)); + if (!wdt->reconfig) + return -ENOMEM; + } + clk = devm_clk_get_enabled(dev, NULL); if (IS_ERR(clk)) return PTR_ERR(clk); @@ -185,13 +298,54 @@ static int apple_wdt_probe(struct platform_device *pdev) wdt->wdd.max_hw_heartbeat_ms = U32_MAX / wdt->clk_rate * 1000; wdt->wdd.timeout = APPLE_WDT_TIMEOUT_DEFAULT; - wdt_ctrl = readl_relaxed(wdt->regs + APPLE_WDT_WD1_CTRL); - if ((wdt_ctrl & APPLE_WDT_CTRL_RESET_EN) && !is_kdump_kernel()) - set_bit(WDOG_HW_RUNNING, &wdt->wdd.status); - watchdog_set_nowayout(&wdt->wdd, WATCHDOG_NOWAYOUT); watchdog_init_timeout(&wdt->wdd, 0, dev); - apple_wdt_set_timeout(&wdt->wdd, wdt->wdd.timeout); + + if (wdt->reconfig) { + u32 wd1_ctrl, wd2_ctrl; + bool armed; + + /* + * Register the sleep handoff before touching watchdog registers + * so a registration failure cannot leave WD2 armed, and so devm + * unwinds watchdog_unregister_device() before unregistering here. + */ + ret = apple_sleep_wdt_register(dev, wdt_res, + apple_wdt_sleep_restore, wdt); + if (ret) + return ret; + ret = devm_add_action_or_reset(dev, apple_wdt_sleep_unregister, dev); + if (ret) + return ret; + + wd1_ctrl = readl_relaxed(wdt->regs + APPLE_WDT_WD1_CTRL); + wd2_ctrl = readl_relaxed(wdt->regs + APPLE_WDT_WD2_CTRL); + armed = (wd1_ctrl & APPLE_WDT_CTRL_RESET_EN) || + (wd2_ctrl & (APPLE_WDT_CTRL_IRQ_EN | APPLE_WDT_CTRL_RESET_EN)); + + if (wd1_ctrl) + writel_relaxed(0, wdt->regs + APPLE_WDT_WD1_CTRL); + + if (armed && !is_kdump_kernel()) { + if (wd1_ctrl & APPLE_WDT_CTRL_RESET_EN) + dev_info(dev, "taking over legacy WD1 watchdog as WD2 simple-reconfig\n"); + if (wd2_ctrl & APPLE_WDT_CTRL_RESET_EN) + dev_info(dev, "taking over WD2 RESET_EN watchdog as WD2 simple-reconfig\n"); + set_bit(WDOG_HW_RUNNING, &wdt->wdd.status); + apple_wdt_wd2_arm(wdt, + apple_wdt_timeout_ticks(wdt, wdt->wdd.timeout)); + } else { + if (wd2_ctrl) + writel_relaxed(APPLE_WDT_CTRL_IRQ_STATUS, + wdt->regs + APPLE_WDT_WD2_CTRL); + apple_wdt_set_timeout(&wdt->wdd, wdt->wdd.timeout); + } + } else { + wdt_ctrl = readl_relaxed(wdt->regs + APPLE_WDT_WD1_CTRL); + if ((wdt_ctrl & APPLE_WDT_CTRL_RESET_EN) && !is_kdump_kernel()) + set_bit(WDOG_HW_RUNNING, &wdt->wdd.status); + apple_wdt_set_timeout(&wdt->wdd, wdt->wdd.timeout); + } /* * A crash kernel only saves the dump and restarts. If it stalls, the @@ -215,6 +369,9 @@ static int apple_wdt_resume(struct device *dev) { struct apple_wdt *wdt = dev_get_drvdata(dev); + if (wdt->reconfig && apple_sleep_wdt_active(dev)) + return 0; + if (watchdog_active(&wdt->wdd) || watchdog_hw_running(&wdt->wdd)) apple_wdt_start(&wdt->wdd); @@ -225,6 +382,9 @@ static int apple_wdt_suspend(struct device *dev) { struct apple_wdt *wdt = dev_get_drvdata(dev); + if (wdt->reconfig && apple_sleep_wdt_active(dev)) + return 0; + if (watchdog_active(&wdt->wdd) || watchdog_hw_running(&wdt->wdd)) apple_wdt_stop(&wdt->wdd); diff --git a/tools/asahi/apple-wdt-tests.py b/tools/asahi/apple-wdt-tests.py new file mode 100755 index 0000000000000..f6f8cf01dd2d6 --- /dev/null +++ b/tools/asahi/apple-wdt-tests.py @@ -0,0 +1,1017 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only OR MIT +"""Check Apple SoC Watchdog probe, ops, PM, sleep handoff and T6050 wake WDT helpers. + +The full production driver body from drivers/watchdog/apple_wdt.c (ops, probe, +suspend, resume, and sleep restore) and the T6050 commit/wake WDT helpers from +drivers/soc/apple/sleep-t6050.h are compiled unchanged under ASan and UBSan +against an in-memory MMIO register model with write-order and barrier tracking. + +- Without a "reconfig" MMIO resource (legacy wdt-version 2): probe adopts + WD1_CTRL RESET_EN, start/stop/ping/set_timeout/get_timeleft/suspend/resume/ + restart drive WD1 unchanged and leave WD2 untouched. +- With a "reconfig" MMIO resource (simple-reconfig-wdog-support): + - Probe rejects undersized (< 4 B) reconfig resources with -EINVAL. + - Probe registers the sleep handoff before touching watchdog registers so a + registration failure (-EBUSY) writes zero registers, and devm unregisters + the watchdog before unregistering the sleep handoff. + - Probe clears any pre-armed WD1_CTRL and unifies pre-probe takeover: any + armed WD1 (RESET_EN) or WD2 (IRQ_EN or RESET_EN) sets WDOG_HW_RUNNING + before the first WD2 write and re-arms WD2 safely through apple_wdt_wd2_arm(). + - start/stop/ping/set_timeout/get_timeleft/restart drive WD2 bark-IRQ with + clock-derived reboot delay, write-order and readl() barrier checks, and + underflow guard. + - resume without apple_sleep reprograms reconfig, WD2_BITE and WD2_BARK even + if register state was lost across suspend. + - suspend/ping/resume during firmware-driven apple_sleep leave sleep's WD2 + safety net intact only for the device owning the sleep slot; non-matching + devices keep operating normally. + - kdump probe skips takeover (both WD1 and WD2) and arms WD2 at max_hw_heartbeat_ms. + - T6050 sleep commit/wake WDT helpers save wdt-aux + AON_WDT_SLEEP before + t6050_seq(T6050_SEQ_WDT) and restore it on wake after CTL=0, W1C IRQ_STATUS + clear, and a readl() barrier, followed by a readl() barrier before re-arming. + +--old-ref REV compiles and runs apple_wdt.c from REV and expects failure. +--mutation NAME mutates the driver/helper source and expects failure. +""" +import argparse +from pathlib import Path +import subprocess +import tempfile + +ROOT = Path(__file__).resolve().parents[2] +DRIVER = 'drivers/watchdog/apple_wdt.c' +SLEEP_T6050 = 'drivers/soc/apple/sleep-t6050.h' + + +def show(ref, path): + if not ref: + return (ROOT / path).read_text() + return subprocess.run(['git', '-C', str(ROOT), 'show', f'{ref}:{path}'], check=True, + capture_output=True, text=True).stdout + + +def extract_body(source): + start = source.index('#define APPLE_WDT_WD0_CUR_TIME') + end = source.index('static DEFINE_SIMPLE_DEV_PM_OPS') + return source[start:end] + + +def extract_t6050_wdt_helpers(source): + start = source.index('static void t6050_sleep_commit_wdt(') + end = source.index('/*\n * The commit window,') + return source[start:end] + + +def apply_mutation(body, t6050_helpers, mutation): + if not mutation: + return body, t6050_helpers + if mutation == 'ignore-sleep-handoff': + needle = 'if (wdt->reconfig && apple_sleep_wdt_active(wdd->parent))\n\t\treturn 0;' + if needle not in body: + raise SystemExit(f'mutation {mutation}: target pattern not found') + return body.replace(needle, 'if (0)\n\t\treturn 0;'), t6050_helpers + if mutation == 'no-bark-in-start': + needle = '\twritel_relaxed(bark_ticks, wdt->regs + APPLE_WDT_WD2_BARK_TIME);' + if needle not in body: + raise SystemExit(f'mutation {mutation}: target pattern not found') + return body.replace(needle, '\t/* omitted BARK write */'), t6050_helpers + if mutation == 'no-status-clear': + needle = '\twritel_relaxed(APPLE_WDT_CTRL_IRQ_STATUS,\n\t\t wdt->regs + APPLE_WDT_WD2_CTRL);' + if needle not in body: + raise SystemExit(f'mutation {mutation}: target pattern not found') + return body.replace(needle, '\t/* omitted IRQ_STATUS clear */'), t6050_helpers + if mutation == 'reconfig-before-bark': + old = ('\twritel_relaxed(0, wdt->regs + APPLE_WDT_WD2_CTRL);\n' + '\twritel_relaxed(U32_MAX, wdt->regs + APPLE_WDT_WD2_BITE_TIME);\n' + '\twritel_relaxed(0, wdt->regs + APPLE_WDT_WD2_CUR_TIME);\n' + '\twritel_relaxed(bark_ticks, wdt->regs + APPLE_WDT_WD2_BARK_TIME);\n' + '\twritel_relaxed(APPLE_WDT_CTRL_IRQ_STATUS,\n' + '\t\t wdt->regs + APPLE_WDT_WD2_CTRL);\n' + '\t(void)readl(wdt->regs + APPLE_WDT_WD2_CTRL);\n' + '\tapple_wdt_enable_reconfig(wdt);') + if old not in body: + raise SystemExit(f'mutation {mutation}: target pattern not found') + new = ('\tapple_wdt_enable_reconfig(wdt);\n' + '\twritel_relaxed(0, wdt->regs + APPLE_WDT_WD2_CTRL);\n' + '\twritel_relaxed(U32_MAX, wdt->regs + APPLE_WDT_WD2_BITE_TIME);\n' + '\twritel_relaxed(0, wdt->regs + APPLE_WDT_WD2_CUR_TIME);\n' + '\twritel_relaxed(bark_ticks, wdt->regs + APPLE_WDT_WD2_BARK_TIME);\n' + '\twritel_relaxed(APPLE_WDT_CTRL_IRQ_STATUS,\n' + '\t\t wdt->regs + APPLE_WDT_WD2_CTRL);\n' + '\t(void)readl(wdt->regs + APPLE_WDT_WD2_CTRL);') + return body.replace(old, new), t6050_helpers + if mutation == 'register-after-arm': + reg_block = ('\t\tret = apple_sleep_wdt_register(dev, wdt_res,\n' + '\t\t\t\t\t apple_wdt_sleep_restore, wdt);\n' + '\t\tif (ret)\n' + '\t\t\treturn ret;\n' + '\t\tret = devm_add_action_or_reset(dev, apple_wdt_sleep_unregister, dev);\n' + '\t\tif (ret)\n' + '\t\t\treturn ret;\n') + tail = '\treturn devm_watchdog_register_device(dev, &wdt->wdd);' + if reg_block not in body or tail not in body: + raise SystemExit(f'mutation {mutation}: target pattern not found') + mut = body.replace(reg_block, '') + mut = mut.replace(tail, + '\tret = devm_watchdog_register_device(dev, &wdt->wdd);\n' + '\tif (ret)\n\t\treturn ret;\n' + '\tif (wdt->reconfig) {\n' + reg_block + '\t}\n\treturn 0;') + return mut, t6050_helpers + if mutation == 'hwrunning-after-arm': + old = ('\t\t\tset_bit(WDOG_HW_RUNNING, &wdt->wdd.status);\n' + '\t\t\tapple_wdt_wd2_arm(wdt,\n' + '\t\t\t\t\t apple_wdt_timeout_ticks(wdt, wdt->wdd.timeout));') + if old not in body: + raise SystemExit(f'mutation {mutation}: target pattern not found') + new = ('\t\t\tapple_wdt_wd2_arm(wdt,\n' + '\t\t\t\t\t apple_wdt_timeout_ticks(wdt, wdt->wdd.timeout));\n' + '\t\t\tset_bit(WDOG_HW_RUNNING, &wdt->wdd.status);') + return body.replace(old, new), t6050_helpers + if mutation == 'no-wd1-adopt': + old = ('\t\tarmed = (wd1_ctrl & APPLE_WDT_CTRL_RESET_EN) ||\n' + '\t\t\t(wd2_ctrl & (APPLE_WDT_CTRL_IRQ_EN | APPLE_WDT_CTRL_RESET_EN));') + if old not in body: + raise SystemExit(f'mutation {mutation}: target pattern not found') + new = '\t\tarmed = !!(wd2_ctrl & (APPLE_WDT_CTRL_IRQ_EN | APPLE_WDT_CTRL_RESET_EN));' + return body.replace(old, new), t6050_helpers + if mutation == 'no-t6050-wake-restore': + old = ('\tif (s->aon_wdt_marked) {\n' + '\t\twritel(s->aon_wdt_saved, s->wdt_aux + AON_WDT_SLEEP);\n' + '\t\t(void)readl(s->wdt_aux + AON_WDT_SLEEP);\n' + '\t\ts->aon_wdt_marked = false;\n' + '\t}') + if old not in t6050_helpers: + raise SystemExit(f'mutation {mutation}: target pattern not found') + return body, t6050_helpers.replace(old, '\t/* omitted wake restore */') + raise SystemExit(f'unknown mutation: {mutation}') + + +PREFIX = r''' +#include +#include +#include +#include +#include +#include +#include +#include + +typedef uint32_t u32; +typedef uint64_t u64; +typedef uintptr_t resource_size_t; + +#define BIT(x) (1u << (x)) +#define GENMASK(h, l) (((~0u) << (l)) & (~0u >> (31 - (h)))) +#define U32_MAX 0xffffffffu +#define MSEC_PER_SEC 1000u +#define __iomem +#define GFP_KERNEL 0 +#define THIS_MODULE NULL +#define IORESOURCE_MEM 0x00000200u +#define WATCHDOG_NOWAYOUT 0 +#define WDIOF_MAGICCLOSE 0x0100u +#define WDIOF_KEEPALIVEPING 0x8000u +#define WDIOF_SETTIMEOUT 0x0080u + +#define WDOG_ACTIVE 0 +#define WDOG_HW_RUNNING 1 + +#define min(a, b) ((a) < (b) ? (a) : (b)) +#define max_t(type, a, b) ((type)(a) > (type)(b) ? (type)(a) : (type)(b)) +#define container_of(ptr, type, member) \ + ((type *)((char *)(ptr) - offsetof(type, member))) + +static inline u64 div_u64(u64 dividend, u32 divisor) +{ + return dividend / divisor; +} + +static inline void set_bit(unsigned int nr, volatile unsigned long *addr) +{ + *addr |= (1UL << nr); +} + +static inline bool test_bit(unsigned int nr, const volatile unsigned long *addr) +{ + return (*addr & (1UL << nr)) != 0; +} + +static inline bool IS_ERR(const void *ptr) +{ + return (uintptr_t)ptr >= (uintptr_t)-4095L; +} + +static inline long PTR_ERR(const void *ptr) +{ + return (long)(intptr_t)ptr; +} + +struct device { + void *drvdata; +}; + +struct platform_device { + struct device dev; +}; + +struct resource { + resource_size_t start; + resource_size_t end; +}; + +static inline resource_size_t resource_size(const struct resource *res) +{ + return res->end - res->start + 1; +} + +struct clk { + unsigned long rate; +}; + +struct watchdog_device; + +struct watchdog_ops { + void *owner; + int (*start)(struct watchdog_device *); + int (*stop)(struct watchdog_device *); + int (*ping)(struct watchdog_device *); + int (*set_timeout)(struct watchdog_device *, unsigned int); + unsigned int (*get_timeleft)(struct watchdog_device *); + int (*restart)(struct watchdog_device *, unsigned long, void *); +}; + +struct watchdog_info { + const char *identity; + unsigned int options; +}; + +struct watchdog_device { + struct device *parent; + const struct watchdog_ops *ops; + const struct watchdog_info *info; + unsigned int timeout; + unsigned int max_hw_heartbeat_ms; + unsigned long status; +}; + +static inline bool watchdog_active(struct watchdog_device *wdd) +{ + return test_bit(WDOG_ACTIVE, &wdd->status); +} + +static inline bool watchdog_hw_running(struct watchdog_device *wdd) +{ + return test_bit(WDOG_HW_RUNNING, &wdd->status); +} + +/* Mock hardware + write log */ +#define MMIO_WORDS 16 +static u32 mmio_regs[MMIO_WORDS]; +static u32 wdt_aux_regs[MMIO_WORDS]; +#define reconfig_reg (wdt_aux_regs[0x2c / 4]) + +struct mmio_event { + bool is_reconfig; + u32 off; + u32 val; +}; + +static struct mmio_event write_log[256]; +static unsigned int write_log_len; + +static unsigned int mdelay_total_ms; +static bool mock_has_reconfig; +static resource_size_t mock_reconfig_size = 4; +static struct resource mock_wdt_res; +static struct resource mock_reconfig_res; +static struct clk mock_clk = { .rate = 24000000UL }; +static bool mock_kdump; +static unsigned int mock_warns; +static unsigned int mock_infos; +static unsigned int mock_dt_timeout; +static bool wdt_ctrl_read_since_write; +static bool reconfig_read_since_write; +static struct watchdog_device *registered_wdd; +static struct watchdog_device *current_probe_wdd; +static bool expect_hw_running_before_wd2_write; +static bool mock_sleep_active; +static int mock_sleep_reg_ret; +static struct device *mock_sleep_owner; +static const struct resource *mock_sleep_reg_res; +static void (*mock_sleep_restore)(void *); +static void *mock_sleep_data; +static void *mock_devm_alloc; + +/* Devm LIFO stack to verify teardown order (watchdog unregister before sleep unregister) */ +enum devm_kind { DEVM_ACTION = 1, DEVM_WDT = 2 }; +struct devm_entry { + enum devm_kind kind; + void (*action)(void *); + void *data; +}; +static struct devm_entry devm_stack[16]; +static unsigned int devm_stack_len; +static enum devm_kind teardown_order[16]; +static unsigned int teardown_order_len; + +static void record_write(const volatile void *addr, u32 val) +{ + assert(write_log_len < 256); + if (addr == (const volatile void *)&reconfig_reg) { + if ((val & GENMASK(5, 4)) == BIT(4)) { + /* + * Routing WD2 bark to cold reset must never occur while + * IRQ_EN is set, IRQ_STATUS is latched, BITE is short, + * CUR >= BARK, or without a readl() barrier on the + * separate watchdog register block first. + */ + assert((mmio_regs[0x2c / 4] & (BIT(0) | BIT(1))) == 0); + assert(mmio_regs[0x24 / 4] == U32_MAX); + assert(mmio_regs[0x20 / 4] < mmio_regs[0x28 / 4]); + assert(wdt_ctrl_read_since_write); + } + reconfig_read_since_write = false; + write_log[write_log_len++] = (struct mmio_event){ true, 0x2c, val }; + } else if (addr >= (const volatile void *)wdt_aux_regs && + addr < (const volatile void *)(wdt_aux_regs + MMIO_WORDS)) { + uintptr_t base = (uintptr_t)wdt_aux_regs; + uintptr_t p = (uintptr_t)addr; + write_log[write_log_len++] = (struct mmio_event){ true, (u32)(p - base), val }; + } else { + uintptr_t base = (uintptr_t)mmio_regs; + uintptr_t p = (uintptr_t)addr; + u32 off = (u32)(p - base); + assert(p >= base && p < base + sizeof(mmio_regs)); + if (expect_hw_running_before_wd2_write && off >= 0x20 && off <= 0x2c) + assert(current_probe_wdd && watchdog_hw_running(current_probe_wdd)); + /* Enabling WD2 IRQ_EN under simple-reconfig requires a readl(reconfig) first */ + if (off == 0x2c && (val & BIT(0)) && (reconfig_reg & GENMASK(5, 4)) == BIT(4)) + assert(reconfig_read_since_write); + wdt_ctrl_read_since_write = false; + write_log[write_log_len++] = (struct mmio_event){ false, off, val }; + } +} + +static u32 readl_relaxed(const volatile void *addr) +{ + return *(const volatile u32 *)addr; +} + +static u32 readl(const volatile void *addr) +{ + if (addr == (const volatile void *)&mmio_regs[0x2c / 4]) + wdt_ctrl_read_since_write = true; + if (addr == (const volatile void *)&reconfig_reg) + reconfig_read_since_write = true; + return *(const volatile u32 *)addr; +} + +static void writel_relaxed(u32 val, volatile void *addr) +{ + if (addr == (volatile void *)&mmio_regs[0x2c / 4]) { + /* Model W1C semantics on APPLE_WDT_CTRL_IRQ_STATUS (BIT(1)) */ + u32 latched = mmio_regs[0x2c / 4] & BIT(1); + if (val & BIT(1)) + latched = 0; + mmio_regs[0x2c / 4] = (val & ~BIT(1)) | latched; + } else { + *(volatile u32 *)addr = val; + } + record_write(addr, val); +} + +static void writel(u32 val, volatile void *addr) +{ + writel_relaxed(val, addr); +} + +static void mdelay(unsigned int ms) +{ + mdelay_total_ms += ms; +} + +static void *devm_kzalloc(struct device *dev, size_t size, int gfp) +{ + assert(!mock_devm_alloc); + mock_devm_alloc = calloc(1, size); + return mock_devm_alloc; +} + +static void __iomem *devm_platform_ioremap_resource(struct platform_device *pdev, + unsigned int index) +{ + assert(index == 0); + return (void __iomem *)mmio_regs; +} + +static void __iomem *devm_platform_get_and_ioremap_resource(struct platform_device *pdev, + unsigned int index, + struct resource **res) +{ + assert(index == 0); + mock_wdt_res.start = 0x28836c000ULL; + mock_wdt_res.end = 0x28836c000ULL + 0x4000 - 1; + if (res) + *res = &mock_wdt_res; + return (void __iomem *)mmio_regs; +} + +static struct resource *platform_get_resource_byname(struct platform_device *pdev, + unsigned int type, const char *name) +{ + assert(type == IORESOURCE_MEM); + if (mock_has_reconfig && strcmp(name, "reconfig") == 0) { + mock_reconfig_res.start = (resource_size_t)(uintptr_t)&reconfig_reg; + mock_reconfig_res.end = mock_reconfig_res.start + mock_reconfig_size - 1; + return &mock_reconfig_res; + } + return NULL; +} + +static void __iomem *devm_ioremap(struct device *dev, resource_size_t offset, + resource_size_t size) +{ + return (void __iomem *)(uintptr_t)offset; +} + +static struct clk *devm_clk_get_enabled(struct device *dev, const char *id) +{ + return &mock_clk; +} + +static unsigned long clk_get_rate(struct clk *clk) +{ + return clk->rate; +} + +static void platform_set_drvdata(struct platform_device *pdev, void *data) +{ + pdev->dev.drvdata = data; +} + +static void *dev_get_drvdata(struct device *dev) +{ + return dev->drvdata; +} + +static bool is_kdump_kernel(void) +{ + return mock_kdump; +} + +static void watchdog_set_nowayout(struct watchdog_device *wdd, bool nowayout) {} +static int watchdog_init_timeout(struct watchdog_device *wdd, unsigned int t, struct device *dev) +{ + current_probe_wdd = wdd; + if (mock_dt_timeout) + wdd->timeout = mock_dt_timeout; + return 0; +} +static void watchdog_stop_on_unregister(struct watchdog_device *wdd) {} +static void watchdog_set_restart_priority(struct watchdog_device *wdd, int prio) {} + +static int devm_watchdog_register_device(struct device *dev, struct watchdog_device *wdd) +{ + registered_wdd = wdd; + assert(devm_stack_len < 16); + devm_stack[devm_stack_len++] = (struct devm_entry){ DEVM_WDT, NULL, wdd }; + return 0; +} + +static int devm_add_action_or_reset(struct device *dev, void (*action)(void *), void *data) +{ + assert(devm_stack_len < 16); + devm_stack[devm_stack_len++] = (struct devm_entry){ DEVM_ACTION, action, data }; + return 0; +} + +static int apple_sleep_wdt_register(struct device *dev, const struct resource *wdt, + void (*restore)(void *), void *data) +{ + if (mock_sleep_reg_ret) + return mock_sleep_reg_ret; + mock_sleep_owner = dev; + mock_sleep_reg_res = wdt; + mock_sleep_restore = restore; + mock_sleep_data = data; + return 0; +} + +static void apple_sleep_wdt_unregister(struct device *dev) +{ + if (mock_sleep_owner == dev) { + mock_sleep_owner = NULL; + mock_sleep_reg_res = NULL; + mock_sleep_restore = NULL; + mock_sleep_data = NULL; + } +} + +static bool apple_sleep_wdt_active(struct device *dev) +{ + return mock_sleep_active && mock_sleep_owner == dev; +} + +#define dev_warn(dev, fmt, ...) do { mock_warns++; } while (0) +#define dev_info(dev, fmt, ...) do { mock_infos++; } while (0) +''' + +T6050_PRELUDE = r''' +/* Minimal harness for T6050 sleep commit/wake WDT helpers extracted from sleep-t6050.h */ +#define WDT_WD0_CTL 0x0c +#define WDT_WD2_COUNT 0x20 +#define WDT_WD2_ALARM 0x28 +#define WDT_WD2_CTL 0x2c +#define WDT_WD2_CTL_IRQ_STATUS BIT(1) +#define WDT_TICKS_PER_S 24000000u +#define WDT_SUSPEND_SECS 120u +#define T6050_WDT_COMMIT_SECS 13u +#define AON_AP_DEADLINE 0x08 +#define AON_WDT_SLEEP 0x2c +#define T6050_SEQ_WDT 2 +#define T6050_SEQS 6 + +struct t6050_ops { + const u32 *op; + unsigned int n; +}; + +struct apple_sleep_region_state { + struct t6050_ops seq[T6050_SEQS]; +}; + +struct apple_sleep { + void __iomem *wdt; + void __iomem *wdt_aux; + struct apple_sleep_region_state *region; + u32 aon_wdt_saved; + bool aon_wdt_marked; +}; + +static bool mock_t6050_seq_saw_saved; +static u32 mock_t6050_seq_saved_val; +static bool mock_t6050_wake_armed_after_readback; + +static void t6050_seq(struct apple_sleep *s, int which) +{ + assert(which == T6050_SEQ_WDT); + if (s->region && s->region->seq[T6050_SEQ_WDT].n) { + mock_t6050_seq_saw_saved = s->aon_wdt_marked; + mock_t6050_seq_saved_val = s->aon_wdt_saved; + writel(0x20u, s->wdt_aux + AON_WDT_SLEEP); + } +} + +static void apple_sleep_set_wd2(struct apple_sleep *s, unsigned int secs) +{ + if (secs == WDT_SUSPEND_SECS) { + /* Before re-arming during wake, status must be clear and reconfig read back */ + assert((mmio_regs[0x2c / 4] & BIT(1)) == 0); + mock_t6050_wake_armed_after_readback = reconfig_read_since_write; + } + writel(0, s->wdt + WDT_WD2_CTL); + writel(0, s->wdt + WDT_WD2_COUNT); + writel(secs * WDT_TICKS_PER_S, s->wdt + WDT_WD2_ALARM); + writel(1, s->wdt + WDT_WD2_CTL); +} +''' + +MAIN = r''' +static void reset_hw(bool has_reconfig, unsigned long clk_rate, bool kdump) +{ + memset(mmio_regs, 0, sizeof(mmio_regs)); + memset(wdt_aux_regs, 0, sizeof(wdt_aux_regs)); + write_log_len = 0; + mdelay_total_ms = 0; + mock_has_reconfig = has_reconfig; + mock_reconfig_size = 4; + mock_clk.rate = clk_rate; + mock_kdump = kdump; + mock_warns = 0; + mock_infos = 0; + mock_dt_timeout = 0; + wdt_ctrl_read_since_write = false; + reconfig_read_since_write = false; + registered_wdd = NULL; + current_probe_wdd = NULL; + expect_hw_running_before_wd2_write = false; + mock_sleep_active = false; + mock_sleep_reg_ret = 0; + mock_sleep_owner = NULL; + mock_sleep_reg_res = NULL; + mock_sleep_restore = NULL; + mock_sleep_data = NULL; + devm_stack_len = 0; + teardown_order_len = 0; +} + +static int find_write(bool is_reconfig, u32 off, u32 val, int from) +{ + for (int i = from; i < (int)write_log_len; i++) { + if (write_log[i].is_reconfig == is_reconfig && + write_log[i].off == off && + write_log[i].val == val) + return i; + } + return -1; +} + +static void teardown_pdev(struct platform_device *pdev) +{ + teardown_order_len = 0; + while (devm_stack_len > 0) { + struct devm_entry e = devm_stack[--devm_stack_len]; + teardown_order[teardown_order_len++] = e.kind; + if (e.kind == DEVM_ACTION && e.action) + e.action(e.data); + } + free(mock_devm_alloc); + mock_devm_alloc = NULL; + pdev->dev.drvdata = NULL; +} + +int main(void) +{ + struct platform_device pdev = {}; + struct device other_dev = {}; + struct watchdog_device *wdd; + + /* 1. Legacy WD1 path (no reconfig): unchanged behaviour, WD2 untouched */ + reset_hw(false, 24000000UL, false); + mmio_regs[0x1c / 4] = BIT(2); /* WD1_CTRL = RESET_EN armed by bootloader */ + assert(apple_wdt_probe(&pdev) == 0); + wdd = registered_wdd; + assert(wdd != NULL); + assert(watchdog_hw_running(wdd)); + assert(mmio_regs[0x14 / 4] == 30u * 24000000u); + assert(mmio_regs[0x2c / 4] == 0); + + assert(wdd->ops->start(wdd) == 0); + assert(mmio_regs[0x1c / 4] == BIT(2)); + assert(mmio_regs[0x2c / 4] == 0); + + mmio_regs[0x10 / 4] = 10u * 24000000u; + assert(wdd->ops->get_timeleft(wdd) == 20u); + assert(wdd->ops->ping(wdd) == 0); + assert(mmio_regs[0x10 / 4] == 0); + + /* Underflow guard when counter reaches or passes bite (catches old driver) */ + mmio_regs[0x10 / 4] = 35u * 24000000u; + assert(wdd->ops->get_timeleft(wdd) == 0); + + assert(wdd->ops->stop(wdd) == 0); + assert(mmio_regs[0x1c / 4] == 0); + + assert(wdd->ops->restart(wdd, 0, NULL) == 0); + assert(mmio_regs[0x1c / 4] == BIT(2)); + assert(mmio_regs[0x14 / 4] == 0); + assert(mmio_regs[0x2c / 4] == 0); + assert(mdelay_total_ms == 150); + teardown_pdev(&pdev); + + /* 2. Simple-reconfig WD2: undersized reconfig resource rejected with 0 writes */ + reset_hw(true, 24000000UL, false); + mock_reconfig_size = 2; + assert(apple_wdt_probe(&pdev) == -EINVAL); + assert(write_log_len == 0); + teardown_pdev(&pdev); + + /* 2b. Failed sleep handoff registration returns -EBUSY with 0 register writes */ + reset_hw(true, 24000000UL, false); + mock_sleep_reg_ret = -EBUSY; + mmio_regs[0x1c / 4] = BIT(2); + mmio_regs[0x2c / 4] = BIT(0); + assert(apple_wdt_probe(&pdev) == -EBUSY); + assert(write_log_len == 0); + assert(registered_wdd == NULL); + teardown_pdev(&pdev); + + /* 3. Simple-reconfig WD2: probe clears armed WD1 and safely takes over hazardous WD2 */ + reset_hw(true, 24000000UL, false); + mock_dt_timeout = 20; /* verify watchdog_init_timeout() runs before takeover */ + expect_hw_running_before_wd2_write = true; + reconfig_reg = 0x000000a5u; /* unrelated bits must be preserved */ + mmio_regs[0x1c / 4] = BIT(2); /* legacy WD1 armed before probe */ + mmio_regs[0x20 / 4] = 500u; /* hazardous: CUR >= BARK */ + mmio_regs[0x24 / 4] = 1000u; /* stale short WD2_BITE */ + mmio_regs[0x28 / 4] = 100u; /* stale short WD2_BARK */ + mmio_regs[0x2c / 4] = BIT(0) | BIT(1) | BIT(2); /* IRQ_EN + latched IRQ_STATUS + RESET_EN */ + assert(apple_wdt_probe(&pdev) == 0); + expect_hw_running_before_wd2_write = false; + wdd = registered_wdd; + assert(mock_sleep_reg_res == &mock_wdt_res); + assert(mock_infos == 2); /* both legacy WD1 and WD2 RESET_EN takeover logged */ + assert(mmio_regs[0x1c / 4] == 0); /* WD1 cleared */ + assert(watchdog_hw_running(wdd)); + assert(reconfig_reg == 0x00000095u); + assert(mmio_regs[0x24 / 4] == U32_MAX); + assert(mmio_regs[0x2c / 4] == BIT(0)); + assert(mmio_regs[0x28 / 4] == 20u * 24000000u); + assert(mmio_regs[0x20 / 4] == 0); + + /* Verify unified arming write order: CTRL=0, BITE=U32_MAX, CUR=0, BARK, CTRL=IRQ_STATUS, reconfig, CTRL=IRQ_EN */ + { + int i_stop = find_write(false, 0x2c, 0, 0); + int i_bite = find_write(false, 0x24, U32_MAX, i_stop); + int i_cur = find_write(false, 0x20, 0, i_bite); + int i_bark = find_write(false, 0x28, 20u * 24000000u, i_cur); + int i_clr = find_write(false, 0x2c, BIT(1), i_bark); + int i_reconfig = find_write(true, 0x2c, 0x00000095u, i_clr); + int i_en = find_write(false, 0x2c, BIT(0), i_reconfig); + assert(i_stop >= 0 && i_bite > i_stop && i_cur > i_bite && i_bark > i_cur); + assert(i_clr > i_bark && i_reconfig > i_clr && i_en > i_reconfig); + } + + /* 4. WD2 start / ping / set_timeout / get_timeleft / underflow / stop */ + write_log_len = 0; + assert(wdd->ops->set_timeout(wdd, 15) == 0); + assert(mmio_regs[0x28 / 4] == 15u * 24000000u); + assert(mmio_regs[0x2c / 4] == BIT(0)); + { + int i_cur = find_write(false, 0x20, 0, 0); + int i_bark = find_write(false, 0x28, 15u * 24000000u, 0); + assert(i_cur >= 0 && i_bark > i_cur); + } + + mmio_regs[0x20 / 4] = 5u * 24000000u; + assert(wdd->ops->get_timeleft(wdd) == 10u); + assert(wdd->ops->ping(wdd) == 0); + assert(mmio_regs[0x20 / 4] == 0); + mmio_regs[0x20 / 4] = 20u * 24000000u; + assert(wdd->ops->get_timeleft(wdd) == 0); + + /* 5. Suspend/resume WITHOUT apple_sleep: resume reprograms lost BARK and reconfig */ + set_bit(WDOG_ACTIVE, &wdd->status); + assert(apple_wdt_suspend(&pdev.dev) == 0); + assert(mmio_regs[0x2c / 4] == 0); + /* Simulate power loss clearing BARK, BITE and reconfig across suspend */ + mmio_regs[0x24 / 4] = 0; + mmio_regs[0x28 / 4] = 0; + reconfig_reg = 0; + write_log_len = 0; + assert(apple_wdt_resume(&pdev.dev) == 0); + assert(reconfig_reg == BIT(4)); + assert(mmio_regs[0x24 / 4] == U32_MAX); + assert(mmio_regs[0x28 / 4] == 15u * 24000000u); + assert(mmio_regs[0x20 / 4] == 0); + assert(mmio_regs[0x2c / 4] == BIT(0)); + { + int i_stop = find_write(false, 0x2c, 0, 0); + int i_bite = find_write(false, 0x24, U32_MAX, i_stop); + int i_cur = find_write(false, 0x20, 0, i_bite); + int i_bark = find_write(false, 0x28, 15u * 24000000u, i_cur); + int i_clr = find_write(false, 0x2c, BIT(1), i_bark); + int i_reconfig = find_write(true, 0x2c, BIT(4), i_clr); + int i_en = find_write(false, 0x2c, BIT(0), i_reconfig); + assert(i_stop >= 0 && i_bite > i_stop && i_cur > i_bite && i_bark > i_cur); + assert(i_clr > i_bark && i_reconfig > i_clr && i_en > i_reconfig); + } + + /* 6. Suspend/resume WITH apple_sleep active: sleep owns WD2 from .begin to .end */ + assert(mock_sleep_restore != NULL); + mock_sleep_active = true; + /* apple_sleep_begin() armed WD2 for 120 s */ + mmio_regs[0x20 / 4] = 1000u; + mmio_regs[0x28 / 4] = 120u * 24000000u; + mmio_regs[0x2c / 4] = BIT(0); + assert(apple_wdt_suspend(&pdev.dev) == 0); + /* Must NOT stop sleep's 120 s safety net */ + assert(mmio_regs[0x2c / 4] == BIT(0)); + assert(mmio_regs[0x28 / 4] == 120u * 24000000u); + /* Keepalive ping during sleep must NOT zero sleep's counter */ + assert(wdd->ops->ping(wdd) == 0); + assert(mmio_regs[0x20 / 4] == 1000u); + /* Sleep commit switched reconfig to 0x20 and wake armed 120 s */ + reconfig_reg = 0x20u; + assert(apple_wdt_resume(&pdev.dev) == 0); + /* Must NOT clobber sleep's resume safety net before .end */ + assert(reconfig_reg == 0x20u); + assert(mmio_regs[0x28 / 4] == 120u * 24000000u); + /* apple_sleep_end() clears WD2_CTL and invokes restore callback */ + mmio_regs[0x2c / 4] = 0; + mock_sleep_restore(mock_sleep_data); + mock_sleep_active = false; + assert(reconfig_reg == BIT(4)); + assert(mmio_regs[0x24 / 4] == U32_MAX); + assert(mmio_regs[0x28 / 4] == 15u * 24000000u); + assert(mmio_regs[0x20 / 4] == 0); + assert(mmio_regs[0x2c / 4] == BIT(0)); + + /* 6b. Per-device active(): a non-owner device still pings/suspends during sleep */ + mock_sleep_active = true; + mock_sleep_owner = &other_dev; + mmio_regs[0x20 / 4] = 1000u; + assert(wdd->ops->ping(wdd) == 0); + assert(mmio_regs[0x20 / 4] == 0); + assert(apple_wdt_suspend(&pdev.dev) == 0); + assert(mmio_regs[0x2c / 4] == 0); + assert(apple_wdt_resume(&pdev.dev) == 0); + assert(mmio_regs[0x2c / 4] == BIT(0)); + mock_sleep_owner = &pdev.dev; + mock_sleep_active = false; + + /* When watchdog is stopped, sleep .end restore must leave WD2 off */ + wdd->status = 0; + assert(wdd->ops->stop(wdd) == 0); + mock_sleep_active = true; + mmio_regs[0x2c / 4] = 0; + mock_sleep_restore(mock_sleep_data); + mock_sleep_active = false; + assert(mmio_regs[0x2c / 4] == 0); + + /* 7. Restart uses clock-derived bark delay (40 ms) and WD2, never WD1 */ + reconfig_reg = 0; + mdelay_total_ms = 0; + write_log_len = 0; + assert(wdd->ops->restart(wdd, 0, NULL) == 0); + assert(reconfig_reg == BIT(4)); + assert(mmio_regs[0x24 / 4] == U32_MAX); + assert(mmio_regs[0x28 / 4] == 24000000u * 40u / 1000u); + assert(mmio_regs[0x2c / 4] == BIT(0)); + assert(mmio_regs[0x1c / 4] == 0); + assert(mdelay_total_ms >= 200); + { + int i_stop = find_write(false, 0x2c, 0, 0); + int i_bite = find_write(false, 0x24, U32_MAX, i_stop); + int i_cur = find_write(false, 0x20, 0, i_bite); + int i_bark = find_write(false, 0x28, 24000000u * 40u / 1000u, i_cur); + int i_clr = find_write(false, 0x2c, BIT(1), i_bark); + int i_reconfig = find_write(true, 0x2c, BIT(4), i_clr); + int i_en = find_write(false, 0x2c, BIT(0), i_reconfig); + assert(i_stop >= 0 && i_bite > i_stop && i_cur > i_bite && i_bark > i_cur); + assert(i_clr > i_bark && i_reconfig > i_clr && i_en > i_reconfig); + } + teardown_pdev(&pdev); + /* Verify devm unwinds watchdog before sleep handoff */ + assert(teardown_order_len == 2); + assert(teardown_order[0] == DEVM_WDT); + assert(teardown_order[1] == DEVM_ACTION); + assert(mock_sleep_restore == NULL); + + /* 7b. Pre-probe legacy WD1 alone is adopted as WD2 simple-reconfig with HW_RUNNING */ + reset_hw(true, 24000000UL, false); + expect_hw_running_before_wd2_write = true; + mmio_regs[0x1c / 4] = BIT(2); /* published Aura trial-boot WD1 */ + assert(apple_wdt_probe(&pdev) == 0); + expect_hw_running_before_wd2_write = false; + wdd = registered_wdd; + assert(mock_infos == 1); + assert(mmio_regs[0x1c / 4] == 0); + assert(watchdog_hw_running(wdd)); + assert(reconfig_reg == BIT(4)); + assert(mmio_regs[0x24 / 4] == U32_MAX); + assert(mmio_regs[0x28 / 4] == 30u * 24000000u); + assert(mmio_regs[0x2c / 4] == BIT(0)); + teardown_pdev(&pdev); + + /* 7c. Pre-probe WD2 RESET_EN alone is adopted as WD2 simple-reconfig with HW_RUNNING */ + reset_hw(true, 24000000UL, false); + expect_hw_running_before_wd2_write = true; + mmio_regs[0x2c / 4] = BIT(2); + assert(apple_wdt_probe(&pdev) == 0); + expect_hw_running_before_wd2_write = false; + wdd = registered_wdd; + assert(mock_infos == 1); + assert(watchdog_hw_running(wdd)); + assert(reconfig_reg == BIT(4)); + assert(mmio_regs[0x2c / 4] == BIT(0)); + teardown_pdev(&pdev); + + /* 7d. Idle pre-probe with only stale latched IRQ_STATUS clears status and stays stopped */ + reset_hw(true, 24000000UL, false); + mmio_regs[0x2c / 4] = BIT(1); + assert(apple_wdt_probe(&pdev) == 0); + wdd = registered_wdd; + assert(!watchdog_hw_running(wdd)); + assert(mmio_regs[0x2c / 4] == 0); + assert(reconfig_reg == 0); + teardown_pdev(&pdev); + + /* 8. Restart bark delay scales with clk_rate (non-24 MHz) */ + reset_hw(true, 12000000UL, false); + assert(apple_wdt_probe(&pdev) == 0); + wdd = registered_wdd; + assert(wdd->ops->restart(wdd, 0, NULL) == 0); + assert(mmio_regs[0x28 / 4] == 12000000u * 40u / 1000u); + teardown_pdev(&pdev); + + /* 9. Crash kernel skips takeover (WD1 and WD2) and arms WD2 at max_hw_heartbeat_ms */ + reset_hw(true, 24000000UL, true); + mmio_regs[0x1c / 4] = BIT(2); + mmio_regs[0x2c / 4] = BIT(0); + assert(apple_wdt_probe(&pdev) == 0); + wdd = registered_wdd; + assert(!watchdog_hw_running(wdd)); + assert(mmio_regs[0x1c / 4] == 0); + assert(reconfig_reg == BIT(4)); + assert(mmio_regs[0x24 / 4] == U32_MAX); + assert(mmio_regs[0x28 / 4] == (U32_MAX / 24000000u) * 24000000u); + assert(mmio_regs[0x2c / 4] == BIT(0)); + teardown_pdev(&pdev); + +#ifndef SKIP_T6050_SLEEP_HELPERS + /* 10. T6050 sleep commit/wake WDT helpers save and restore wdt-aux + AON_WDT_SLEEP */ + { + struct apple_sleep_region_state reg = { .seq = { [T6050_SEQ_WDT] = { .n = 1 } } }; + struct apple_sleep s = { + .wdt = (void __iomem *)mmio_regs, + .wdt_aux = (void __iomem *)wdt_aux_regs, + .region = ®, + }; + + reset_hw(true, 24000000UL, false); + reconfig_reg = 0x00000095u; /* 0x10 routing + unrelated bits */ + reconfig_read_since_write = true; /* already settled before commit */ + mmio_regs[0x24 / 4] = U32_MAX; + mmio_regs[0x28 / 4] = 30u * 24000000u; + mmio_regs[0x2c / 4] = BIT(0); + mock_t6050_seq_saw_saved = false; + mock_t6050_seq_saved_val = 0; + + t6050_sleep_commit_wdt(&s); + assert(mock_t6050_seq_saw_saved); + assert(mock_t6050_seq_saved_val == 0x00000095u); + assert(s.aon_wdt_marked); + assert(reconfig_reg == 0x20u); + assert(mmio_regs[0x2c / 4] == BIT(0)); + assert(mmio_regs[0x28 / 4] == T6050_WDT_COMMIT_SECS * WDT_TICKS_PER_S); + + /* Wake: simulate latched IRQ_STATUS and verify CTL=0 -> clear -> readl -> restore -> readl -> set_wd2 */ + mmio_regs[0x2c / 4] = BIT(0) | BIT(1); + write_log_len = 0; + mock_t6050_wake_armed_after_readback = false; + t6050_sleep_wake_wdt(&s); + assert(!s.aon_wdt_marked); + assert(reconfig_reg == 0x00000095u); + assert(mock_t6050_wake_armed_after_readback); + assert(mmio_regs[0x2c / 4] == BIT(0)); + assert(mmio_regs[0x28 / 4] == WDT_SUSPEND_SECS * WDT_TICKS_PER_S); + { + int i_stop = find_write(false, 0x2c, 0, 0); + int i_clr = find_write(false, 0x2c, BIT(1), i_stop); + int i_rest = find_write(true, 0x2c, 0x00000095u, i_clr); + int i_arm = find_write(false, 0x2c, BIT(0), i_rest); + assert(i_stop >= 0 && i_clr > i_stop && i_rest > i_clr && i_arm > i_rest); + } + + /* When seq[T6050_SEQ_WDT] is empty, aon_wdt_marked stays false and wake leaves reconfig alone */ + reg.seq[T6050_SEQ_WDT].n = 0; + reconfig_reg = 0x10u; + reconfig_read_since_write = true; + write_log_len = 0; + t6050_sleep_commit_wdt(&s); + assert(!s.aon_wdt_marked); + write_log_len = 0; + t6050_sleep_wake_wdt(&s); + assert(find_write(true, 0x2c, 0x10u, 0) < 0); + assert(reconfig_reg == 0x10u); + } +#endif + + puts("PASS apple_wdt legacy WD1, simple-reconfig WD2, sleep handoff, and T6050 wake"); + return 0; +} +''' + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--out', type=Path, help='keep build and result files here') + parser.add_argument('--cc', default='cc') + parser.add_argument('--old-ref', help='compile and run apple_wdt.c from this revision') + parser.add_argument('--mutation', help='apply a named mutation and expect failure') + args = parser.parse_args() + + source = show(args.old_ref, DRIVER) + body = extract_body(source) + if args.old_ref: + t6050_part = '#define SKIP_T6050_SLEEP_HELPERS 1\n' + else: + t6050_src = show(None, SLEEP_T6050) + t6050_helpers = extract_t6050_wdt_helpers(t6050_src) + body, t6050_helpers = apply_mutation(body, t6050_helpers, args.mutation) + t6050_part = T6050_PRELUDE + t6050_helpers + + code = PREFIX + body + t6050_part + MAIN + + def run(out): + out.mkdir(parents=True, exist_ok=True) + cfile, exe = out / 'apple-wdt.c', out / 'apple-wdt' + cfile.write_text(code) + subprocess.run([args.cc, '-std=gnu11', '-O1', '-g', '-Wall', '-Wextra', '-Werror', + '-Wno-unused-parameter', '-Wno-unused-function', + '-fsanitize=address,undefined', '-fno-sanitize-recover=all', + str(cfile), '-o', str(exe)], check=True) + res = subprocess.run([str(exe)], capture_output=True, text=True) + print(res.stdout + res.stderr, end='') + return res.returncode + + if args.out: + return run(args.out) + with tempfile.TemporaryDirectory(prefix='apple-wdt-') as out: + return run(Path(out)) + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index 26b4986911288..90460b60ee5ce 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -113,6 +113,13 @@ def mutations(flag, names, base, expect=KILL): 'binding-lifetime-tests.py': [(out(), PASS)] + mutations('--mutation', ('no-close', 'no-join'), out()), 'apple-pwm-pm-tests.py': [([], PASS), (['--negative-control'], PASS)], + # 70a00e583b032 underflows get_timeleft() on cur > bite and ignores the reconfig reg. + 'apple-wdt-tests.py': [(out(), PASS), + (out() + ['--old-ref', '70a00e583b032'], KILL)] + + mutations('--mutation', ('ignore-sleep-handoff', 'no-bark-in-start', 'no-status-clear', + 'reconfig-before-bark', 'register-after-arm', + 'hwrunning-after-arm', 'no-wd1-adopt', + 'no-t6050-wake-restore'), out()), 'apple-m4-pmu-tests.py': [([], PASS)] + mutations('--mutation', ('m1-layout', 'cycles-anywhere', 'expose-8-9', 'flags-in-event'), []), 'apple-dart-dapf-tests.py': [(out(), PASS)] From 67f0883910025e639cebe9abf66c243771466926 Mon Sep 17 00:00:00 2001 From: Jeff Queisser Date: Tue, 24 Nov 2026 01:53:08 -0800 Subject: [PATCH 4/4] arm64: dts: apple: t6050: add watchdog simple-reconfig register Add the optional "reconfig" register (<0x2 0x8833002c 0x0 0x4>) and reg-names ("wdt", "reconfig") to watchdog@28836c000 in t6050-die0.dtsi so apple_wdt selects WD2 bark-IRQ simple-reconfig mode on T6050 boards for clean cold resets on watchdog expiry and reboot. Signed-off-by: Jeff Queisser --- arch/arm64/boot/dts/apple/t6050-die0.dtsi | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/arm64/boot/dts/apple/t6050-die0.dtsi b/arch/arm64/boot/dts/apple/t6050-die0.dtsi index 1d6887ef8bde1..6643e2d66215e 100644 --- a/arch/arm64/boot/dts/apple/t6050-die0.dtsi +++ b/arch/arm64/boot/dts/apple/t6050-die0.dtsi @@ -39,7 +39,9 @@ wdt: watchdog@28836c000 { compatible = "apple,t6050-wdt", "apple,t8103-wdt"; - reg = <0x2 0x8836c000 0x0 0x4000>; + reg = <0x2 0x8836c000 0x0 0x4000>, + <0x2 0x8833002c 0x0 0x4>; + reg-names = "wdt", "reconfig"; clocks = <&clkref>; interrupts = ; };