diff --git a/drivers/soc/apple/rtkit-helper-test.c b/drivers/soc/apple/rtkit-helper-test.c index 4e3aa1b0e07aa..7ee3ac26fc21a 100644 --- a/drivers/soc/apple/rtkit-helper-test.c +++ b/drivers/soc/apple/rtkit-helper-test.c @@ -431,6 +431,7 @@ static void rtkh_inherit_one_sram(struct kunit *test) KUNIT_EXPECT_NULL(test, b[0].shmem.buffer); KUNIT_EXPECT_PTR_EQ(test, (__force void *)b[0].shmem.iomem, ctx->sram_mem + 0x1000); + KUNIT_EXPECT_FALSE(test, ctx->helper.dram_buffers); } static void rtkh_inherit_max(struct kunit *test) @@ -459,6 +460,7 @@ static void rtkh_inherit_max(struct kunit *test) KUNIT_EXPECT_PTR_EQ(test, (__force void *)b[i].shmem.iomem, ctx->sram_mem + (b[i].shmem.iova - s)); } + KUNIT_EXPECT_FALSE(test, ctx->helper.dram_buffers); } static void rtkh_inherit_bad_count(struct kunit *test) @@ -569,6 +571,67 @@ static void rtkh_inherit_dma_mask_edge(struct kunit *test) KUNIT_EXPECT_EQ(test, rtkh_inherit(ctx, b), -EINVAL); } +#define RTKH_TEST_DRAM_START 0x9f0000000ULL +#define RTKH_TEST_DRAM_SIZE 0x10000 + +static void __iomem *rtkh_stub_map_dram(struct apple_rtkit_helper *helper, + const struct resource *res) +{ + struct kunit *test = kunit_get_current_test(); + struct rtkh_test *ctx = test->priv; + + if (res->start >= RTKH_TEST_DRAM_START && + res->end < RTKH_TEST_DRAM_START + RTKH_TEST_DRAM_SIZE) + return (__force void __iomem *)(ctx->sram_mem + (res->start - RTKH_TEST_DRAM_START)); + return ERR_PTR(-EINVAL); +} + +static void rtkh_inherit_dram_buffers(struct kunit *test) +{ + struct rtkh_test *ctx = test->priv; + struct apple_rtkit_inherited_buffer b[APPLE_RTKIT_HELPER_MAX_BUFFERS]; + const u64 s = RTKH_TEST_SRAM_START; + const u64 d = RTKH_TEST_DRAM_START; + const u32 dram_only[] = { 2, RTKH_HI(d + 0x1000), RTKH_LO(d + 0x1000), 0x2000, 0 }; + const u32 mixed[] = { + 1, RTKH_HI(s), RTKH_LO(s), 0x1000, 0, + 2, RTKH_HI(d + 0x2000), RTKH_LO(d + 0x2000), 0x1000, 0, + }; + const u32 dram_then_bad[] = { + 2, RTKH_HI(d), RTKH_LO(d), 0x1000, 0, + 4, RTKH_HI(s), RTKH_LO(s), 0x1000, 3, + }; + const u32 sram_only[] = { 1, RTKH_HI(s), RTKH_LO(s), 0x1000, 0 }; + + kunit_activate_static_stub(test, apple_rtkit_helper_map_dram, rtkh_stub_map_dram); + + /* A DRAM buffer followed by a bad tuple fails without setting dram_buffers. */ + rtkh_node(ctx, "apple,t8112-mtp"); + rtkh_inventory(ctx, dram_then_bad, ARRAY_SIZE(dram_then_bad)); + KUNIT_EXPECT_EQ(test, rtkh_inherit(ctx, b), -EINVAL); + KUNIT_EXPECT_FALSE(test, ctx->helper.dram_buffers); + + /* A valid reserved DRAM buffer records dram_buffers. */ + rtkh_node(ctx, "apple,t8112-mtp"); + rtkh_inventory(ctx, dram_only, ARRAY_SIZE(dram_only)); + KUNIT_ASSERT_EQ(test, rtkh_inherit(ctx, b), 1); + KUNIT_EXPECT_TRUE(test, ctx->helper.dram_buffers); + KUNIT_EXPECT_PTR_EQ(test, (__force void *)b[0].shmem.iomem, + ctx->sram_mem + 0x1000); + + /* Mixed SRAM and DRAM buffers also record dram_buffers. */ + rtkh_node(ctx, "apple,t8112-mtp"); + rtkh_inventory(ctx, mixed, ARRAY_SIZE(mixed)); + KUNIT_ASSERT_EQ(test, rtkh_inherit(ctx, b), 2); + KUNIT_EXPECT_TRUE(test, ctx->helper.dram_buffers); + + /* A subsequent SRAM-only inventory clears dram_buffers. */ + rtkh_node(ctx, "apple,t8112-mtp"); + rtkh_inventory(ctx, sram_only, ARRAY_SIZE(sram_only)); + KUNIT_ASSERT_EQ(test, rtkh_inherit(ctx, b), 1); + KUNIT_EXPECT_FALSE(test, ctx->helper.dram_buffers); +} + /* Sleep: the AP power notices */ static void rtkh_expect_calls(struct kunit *test, const bool *on, unsigned int n) @@ -891,6 +954,7 @@ static struct kunit_case apple_rtkit_helper_test_cases[] = { KUNIT_CASE(rtkh_inherit_bad_tuple), KUNIT_CASE(rtkh_inherit_bad_second), KUNIT_CASE(rtkh_inherit_dma_mask_edge), + KUNIT_CASE(rtkh_inherit_dram_buffers), KUNIT_CASE(rtkh_pm_fw_sleep_cycle), KUNIT_CASE(rtkh_pm_idle_late), KUNIT_CASE(rtkh_pm_no_fw_no_notice), diff --git a/drivers/soc/apple/rtkit-helper.c b/drivers/soc/apple/rtkit-helper.c index 2d0871dc5af5e..54410c70126ce 100644 --- a/drivers/soc/apple/rtkit-helper.c +++ b/drivers/soc/apple/rtkit-helper.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -44,6 +45,8 @@ struct apple_rtkit_helper { * it does not list must not replace storage the firmware still owns. */ bool strict_buffers; + /* Adopted buffers include reserved DRAM regions outside on-chip SRAM. */ + bool dram_buffers; /* The AP idle notice waits for .suspend_late, after the HID host's packets */ bool idle_late; /* The firmware was told the AP is going to sleep. */ @@ -130,6 +133,11 @@ static void apple_rtkit_helper_release(void *rtk) apple_rtkit_free_retaining_buffers(rtk); } +static void apple_rtkit_helper_release_slp_ddr(void *unused) +{ + apple_sleep_coproc_slp_ddr(false); +} + /* * MTP firmware cannot always be restarted once it has been taken to QUIESCED * or SLEEP, so a boot stage that used it hands the session over running. It @@ -203,6 +211,18 @@ static bool apple_rtkit_helper_retained_mtp(struct device_node *np) of_device_is_compatible(np, "apple,t6041-mtp"); } +static void __iomem *apple_rtkit_helper_map_dram(struct apple_rtkit_helper *helper, + const struct resource *res) +{ +#if IS_ENABLED(CONFIG_APPLE_RTKIT_HELPER_KUNIT_TEST) + KUNIT_STATIC_STUB_REDIRECT(apple_rtkit_helper_map_dram, helper, res); +#endif + /* DVA == PA is part of this retained-bypass handoff contract. */ + if (!apple_rtkit_helper_reserved(helper->dev->of_node, res)) + return ERR_PTR(-EINVAL); + return devm_ioremap(helper->dev, res->start, resource_size(res)) ?: ERR_PTR(-ENOMEM); +} + static int apple_rtkit_helper_inherit_buffers( struct apple_rtkit_helper *helper, struct apple_rtkit_inherited_buffer *buffers) @@ -211,6 +231,7 @@ static int apple_rtkit_helper_inherit_buffers( struct property *inventory; u32 cells[APPLE_RTKIT_HELPER_MAX_BUFFERS * APPLE_RTKIT_HELPER_BUFFER_CELLS]; int count, i, length, ret; + bool has_dram = false; /* T8140 producers predating the buffer inventory remain compatible. */ inventory = of_find_property(np, "apple,rtkit-buffers", &length); @@ -219,8 +240,10 @@ static int apple_rtkit_helper_inherit_buffers( return -EINVAL; return 0; } - if (!length) + if (!length) { + helper->dram_buffers = false; return 0; + } count = of_property_count_u32_elems(np, "apple,rtkit-buffers"); if (count < 0 || count > ARRAY_SIZE(cells) || count % APPLE_RTKIT_HELPER_BUFFER_CELLS) @@ -253,14 +276,13 @@ static int apple_rtkit_helper_inherit_buffers( if (helper->sram && resource_contains(helper->sram, &res)) { b->iomem = helper->sram_base + res.start - helper->sram->start; } else { - /* DVA == PA is part of this retained-bypass handoff contract. */ - if (!apple_rtkit_helper_reserved(np, &res)) - return -EINVAL; - b->iomem = devm_ioremap(helper->dev, res.start, resource_size(&res)); - if (!b->iomem) - return -ENOMEM; + b->iomem = apple_rtkit_helper_map_dram(helper, &res); + if (IS_ERR(b->iomem)) + return PTR_ERR(b->iomem); + has_dram = true; } } + helper->dram_buffers = has_dram; return count; } @@ -370,6 +392,14 @@ static int apple_rtkit_helper_probe(struct platform_device *pdev) ret = apple_rtkit_helper_pm_register(helper); if (ret) return ret; + if (helper->dram_buffers) { + apple_sleep_coproc_slp_ddr(true); + ret = devm_add_action_or_reset(dev, + apple_rtkit_helper_release_slp_ddr, + NULL); + if (ret) + return ret; + } /* There is no safe module-unload boundary for the retained session. */ __module_get(THIS_MODULE); diff --git a/drivers/soc/apple/sleep-t8112.h b/drivers/soc/apple/sleep-t8112.h index 24ef5ab871b39..5a9d5865b82b9 100644 --- a/drivers/soc/apple/sleep-t8112.h +++ b/drivers/soc/apple/sleep-t8112.h @@ -275,6 +275,8 @@ static int t8112_sleep_map_regs(struct apple_sleep *s) } } + sleep_slp_ddr_bind(s); + { int ret = apple_sleep_read_wake_sources(s); @@ -1222,6 +1224,7 @@ static const struct apple_sleep_soc t8112_sleep_soc = { .ops = &t8112_sleep_ops, .untested = true, .early_context = true, + .coproc_slp_ddr = true, .early_cpu_wfi = true, .boot_wfi_delay_ms = 2, .no_pmu_notes = true, diff --git a/drivers/soc/apple/sleep-test.c b/drivers/soc/apple/sleep-test.c index a3e80bce4be68..3774fa26749da 100644 --- a/drivers/soc/apple/sleep-test.c +++ b/drivers/soc/apple/sleep-test.c @@ -73,7 +73,6 @@ static void apple_sleep_test_known_profiles(struct kunit *test) KUNIT_ASSERT_LT(test, n, ARRAY_SIZE(apple_sleep_test_profiles)); KUNIT_EXPECT_STREQ(test, id->compatible, apple_sleep_test_profiles[n]); if (!strcmp(id->compatible, "apple,t8142-sleep") || - !strcmp(id->compatible, "apple,t8112-sleep") || !strcmp(id->compatible, "apple,t8122-sleep")) { const struct apple_sleep_soc *soc = id->data; @@ -82,6 +81,11 @@ static void apple_sleep_test_known_profiles(struct kunit *test) /* T8142 has run full deep sleeps and wakes; T8122 has not yet. */ KUNIT_EXPECT_EQ(test, (int)soc->untested, !strcmp(id->compatible, "apple,t8122-sleep")); + } else if (!strcmp(id->compatible, "apple,t8112-sleep")) { + const struct apple_sleep_soc *soc = id->data; + + KUNIT_ASSERT_NOT_NULL(test, soc); + KUNIT_EXPECT_TRUE(test, soc->untested); } } KUNIT_EXPECT_EQ(test, n, ARRAY_SIZE(apple_sleep_test_profiles)); @@ -607,6 +611,117 @@ static void apple_sleep_test_t6040_wdt_span(struct kunit *test) KUNIT_EXPECT_FALSE(test, t6040_wdt_resource_ok(&elsewhere)); } +struct apple_sleep_test_slp_ddr_saved { + bool usb; + unsigned int coproc; + bool coproc_en; + void __iomem *word; + bool crashed; + bool deep_once; +}; + +struct apple_sleep_test_slp_ddr_state { + u32 fake_wake; + struct apple_sleep_soc soc; + struct apple_sleep sleep; + struct apple_sleep_test_slp_ddr_saved saved; +}; + +static void apple_sleep_test_slp_ddr_restore(void *data) +{ + struct apple_sleep_test_slp_ddr_saved *saved = data; + + guard(spinlock_irqsave)(&sleep_slp_ddr_lock); + sleep_slp_ddr_usb = saved->usb; + sleep_slp_ddr_coproc = saved->coproc; + sleep_slp_ddr_coproc_en = saved->coproc_en; + sleep_slp_ddr_word = saved->word; + sleep_slp_ddr_crashed = saved->crashed; + sleep_slp_ddr_deep_once = saved->deep_once; +} + +static void apple_sleep_test_slp_ddr_voters(struct kunit *test) +{ + struct apple_sleep_test_slp_ddr_state *st; + + st = kunit_kzalloc(test, sizeof(*st), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, st); + + scoped_guard(spinlock_irqsave, &sleep_slp_ddr_lock) { + st->saved.usb = sleep_slp_ddr_usb; + st->saved.coproc = sleep_slp_ddr_coproc; + st->saved.coproc_en = sleep_slp_ddr_coproc_en; + st->saved.word = sleep_slp_ddr_word; + st->saved.crashed = sleep_slp_ddr_crashed; + st->saved.deep_once = sleep_slp_ddr_deep_once; + sleep_slp_ddr_usb = false; + sleep_slp_ddr_coproc = 0; + sleep_slp_ddr_coproc_en = false; + sleep_slp_ddr_word = NULL; + sleep_slp_ddr_crashed = false; + sleep_slp_ddr_deep_once = false; + } + KUNIT_ASSERT_EQ(test, + kunit_add_action_or_reset(test, apple_sleep_test_slp_ddr_restore, + &st->saved), 0); + + st->fake_wake = 0xdead0000U; + st->sleep.soc = &st->soc; + st->sleep.wake = (void __iomem __force *)&st->fake_wake; + + /* A coprocessor vote taken before sleep probes stays deferred until bind. */ + apple_sleep_coproc_slp_ddr(true); + KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U); + + /* A SoC without coproc_slp_ddr ignores coprocessor votes (USB still works). */ + st->soc.coproc_slp_ddr = false; + sleep_slp_ddr_bind(&st->sleep); + KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U); + + apple_sleep_usb_slp_ddr(true); + KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U | WAKE_SLP_DDR_USB); + apple_sleep_usb_slp_ddr(false); + KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U); + + /* A SoC with coproc_slp_ddr applies the deferred vote and preserves other bits. */ + st->soc.coproc_slp_ddr = true; + sleep_slp_ddr_bind(&st->sleep); + KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U | WAKE_SLP_DDR_USB); + + /* Coprocessor votes are refcounted and guard against underflow. */ + apple_sleep_coproc_slp_ddr(true); + apple_sleep_coproc_slp_ddr(false); + KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U | WAKE_SLP_DDR_USB); + apple_sleep_coproc_slp_ddr(false); + KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U); + apple_sleep_coproc_slp_ddr(false); + apple_sleep_coproc_slp_ddr(true); + KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U | WAKE_SLP_DDR_USB); + + /* deep_once overrides active votes for one sleep and restores on clear. */ + sleep_slp_ddr_set_deep_once(true); + KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U); + sleep_slp_ddr_set_deep_once(false); + KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U | WAKE_SLP_DDR_USB); + + /* A crashed unmanaged coprocessor permanently clears bit 0. */ + scoped_guard(spinlock_irqsave, &sleep_slp_ddr_lock) { + sleep_slp_ddr_crashed = true; + sleep_slp_ddr_write(); + } + KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U); + apple_sleep_usb_slp_ddr(true); + KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U); + + /* Only T8112 enables coprocessor SLP_DDR votes among matched profiles. */ + for (const struct of_device_id *id = apple_sleep_of_match; id->compatible[0]; id++) { + const struct apple_sleep_soc *soc = id->data; + bool want = !strcmp(id->compatible, "apple,t8112-sleep"); + + KUNIT_EXPECT_EQ_MSG(test, soc->coproc_slp_ddr, want, "%s", id->compatible); + } +} + static struct kunit_case apple_sleep_test_cases[] = { KUNIT_CASE(apple_sleep_test_layout), KUNIT_CASE(apple_sleep_test_t6050), @@ -623,6 +738,7 @@ static struct kunit_case apple_sleep_test_cases[] = { KUNIT_CASE(apple_sleep_test_wdt_handoff), KUNIT_CASE(apple_sleep_test_apcie_slots), KUNIT_CASE(apple_sleep_test_t6040_wdt_span), + KUNIT_CASE(apple_sleep_test_slp_ddr_voters), {} }; diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 97c533a763602..15197ca9cec7a 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -855,6 +855,8 @@ struct apple_sleep_soc { bool wdt_wd1; /* The SMC gets pmVC and WKTP before slpa (gen 1, as t6000 has them) */ bool smc_gen1_keys; + /* Live coprocessors with DRAM buffers require WAKE_SLP_DDR across sleep. */ + bool coproc_slp_ddr; }; struct apple_sleep { @@ -1283,9 +1285,14 @@ bool apple_sleep_usb_aon_keep(unsigned int port, bool keep) } EXPORT_SYMBOL_GPL(apple_sleep_usb_aon_keep); -/* The USB host's vote, kept until the word is mapped. Only bit 0 is touched, as on macOS. */ +/* + * USB and live-coprocessor votes, kept until the word is mapped. Only bit 0 is + * touched, as on macOS. + */ static DEFINE_SPINLOCK(sleep_slp_ddr_lock); static bool sleep_slp_ddr_usb; +static unsigned int sleep_slp_ddr_coproc; +static bool sleep_slp_ddr_coproc_en; static void __iomem *sleep_slp_ddr_word; /* @@ -1306,7 +1313,8 @@ static void sleep_slp_ddr_write(void) if (!sleep_slp_ddr_word) return; val = readl(sleep_slp_ddr_word) & ~WAKE_SLP_DDR_USB; - if (sleep_slp_ddr_usb && !sleep_slp_ddr_crashed && !sleep_slp_ddr_deep_once) + if ((sleep_slp_ddr_usb || (sleep_slp_ddr_coproc_en && sleep_slp_ddr_coproc)) && + !sleep_slp_ddr_crashed && !sleep_slp_ddr_deep_once) val |= WAKE_SLP_DDR_USB; writel(val, sleep_slp_ddr_word); } @@ -1339,6 +1347,25 @@ void apple_sleep_usb_slp_ddr(bool up) } EXPORT_SYMBOL_GPL(apple_sleep_usb_slp_ddr); +void apple_sleep_coproc_slp_ddr(bool up) +{ + guard(spinlock_irqsave)(&sleep_slp_ddr_lock); + if (up) + sleep_slp_ddr_coproc++; + else if (sleep_slp_ddr_coproc) + sleep_slp_ddr_coproc--; + sleep_slp_ddr_write(); +} +EXPORT_SYMBOL_GPL(apple_sleep_coproc_slp_ddr); + +static void sleep_slp_ddr_bind(struct apple_sleep *s) +{ + guard(spinlock_irqsave)(&sleep_slp_ddr_lock); + sleep_slp_ddr_word = s->wake + WAKE_SLP_DDR; + sleep_slp_ddr_coproc_en = s->soc->coproc_slp_ddr; + sleep_slp_ddr_write(); +} + /* * From RTKit, for every firmware crash. One this driver stops and restarts around each sleep * doesn't run through it; for any other, every later sleep takes the deep path. It may come @@ -8572,10 +8599,7 @@ static int apple_sleep_probe(struct platform_device *pdev) ret = apple_sleep_alloc_books(s); if (ret) return ret; - scoped_guard(spinlock_irqsave, &sleep_slp_ddr_lock) { - sleep_slp_ddr_word = s->wake + WAKE_SLP_DDR; - sleep_slp_ddr_write(); - } + sleep_slp_ddr_bind(s); ret = apple_sleep_read_wake_sources(s); if (ret) diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index 6707370aa4466..ae171d7ac7968 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -186,6 +186,12 @@ void apple_sleep_usb_asleep_notifier_unregister(struct notifier_block *nb); */ void apple_sleep_usb_slp_ddr(bool up); +/* + * A coprocessor that stays live through a sleep with retained DRAM buffers + * votes to keep DRAM powered across sleep on SoCs whose sleep engine drops it. + */ +void apple_sleep_coproc_slp_ddr(bool up); + /* * A Type-C port in use asks for its always-on USB domain to stay up through the next sleep, * and takes the request back after it. Returns whether the domain will be kept. @@ -456,6 +462,10 @@ static inline void apple_sleep_usb_slp_ddr(bool up) { } +static inline void apple_sleep_coproc_slp_ddr(bool up) +{ +} + static inline bool apple_sleep_usb_aon_keep(unsigned int port, bool keep) { return false;