From 243f9793fc1245429ad457296a01a9a2ed91c190 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sat, 28 Nov 2026 09:19:45 +1100 Subject: [PATCH 1/4] nvme: apple: restart ANS cleanly after a refused T8122 warm sleep The T8122 warm sleep idles ANS (RTKit IDLE) and clears its run bit in .suspend, and keeps the RTKit session for the restart after the wake. When the sleep is refused after that (no firmware resume), the resume falls back to the cold path in the reset work: reset assert, apple_rtkit_reinit(), deassert, boot. That has two problems after a warm idle: - The reinit is the first to free the session's buffers. It does that through the SART, which on T8122 shares ANS's reset, so it reads a SART held in reset and takes an asynchronous SError: Kernel panic - not syncing: Asynchronous SError Interrupt sart3_get_entry apple_sart_remove_allowed_region apple_nvme_sart_dma_destroy apple_rtkit_reinit apple_nvme_reset_work_impl - With that out of the way, ANS doesn't boot ("ANS did not boot", reset failure -62) and the disk is lost: in IDLE ANS gates its SART, and the reset drops its entries, as on the M1 class's firmware sleep. Treat a refused warm sleep as that path already treats a refused firmware sleep: save the SART's entries and the word that gates it before the idle (and drop them again if the idle fails), and on the refusal take the start after a firmware sleep (fw_sleep_restart: SART power word, SART entries, mailbox enables). Free the session's buffers before the reset assert, with the SART power word written back first. A committed warm sleep keeps its warm restart; its SART snapshot is now taken before the idle instead of in the SART's suspend_late. Closes: https://github.com/aurora-silicon/linux/issues/3435 Signed-off-by: Ryan Murray --- drivers/nvme/host/apple.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index e29dd701a8484..9fce198c18908 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -289,6 +289,8 @@ struct apple_nvme { ktime_t sleep_run_time; bool sleep_registered; bool t8122_warm_sleep; + /* A refused warm sleep: the session's buffers are still mapped in the SART. */ + bool t8122_warm_refused; unsigned long recovery_flags; void __iomem *mmio_coproc; @@ -2061,6 +2063,23 @@ static void apple_nvme_reset_work_impl(struct work_struct *work) fw_sleep = READ_ONCE(anv->fw_sleep_restart); WRITE_ONCE(anv->fw_sleep_restart, false); + /* + * A refused warm sleep idled ANS but kept its session for the + * restart, so the session's buffers are still in the SART, which + * the idle gated. Ungate it and free them now: the reset below + * can take the SART too (T8122 shares ANS's reset with it), and + * the reinit after it would then read a SART in reset. The rest + * is the start after a firmware sleep (fw_sleep). + */ + if (xchg(&anv->t8122_warm_refused, false)) { + dev_dbg(anv->dev, "refused warm sleep: SART power %#x, saved %#x\n", + readl(anv->mmio_nvme + APPLE_ANS_SART_POWER), anv->sart_power); + writel(anv->sart_power, anv->mmio_nvme + APPLE_ANS_SART_POWER); + ret = apple_rtkit_reinit(anv->rtk); + if (ret) + goto out; + } + ret = reset_control_assert(anv->reset); if (ret) goto out; @@ -3197,8 +3216,11 @@ static void apple_nvme_t8122_warm_resume(struct apple_nvme *anv) * restart without a power cycle can hang; fall back to the cold PMGR * reset path in apple_nvme_reset_work_impl(). */ - if (!pm_resume_via_firmware()) + if (!pm_resume_via_firmware()) { + WRITE_ONCE(anv->t8122_warm_refused, true); + WRITE_ONCE(anv->fw_sleep_restart, true); return; + } apple_sleep_restore_pins(); if (anv->sart) @@ -3495,9 +3517,20 @@ static int apple_nvme_suspend(struct device *dev) !apple_sleep_dry_run()) { anv->sleep_failed = false; anv->sleep_restarted = false; + /* + * In IDLE, ANS gates its SART. A sleep refused after this + * restarts it cold, as after a firmware sleep: keep the + * SART's entries and the word that gates it for that. + */ + anv->sart_power = readl(anv->mmio_nvme + APPLE_ANS_SART_POWER); + if (anv->sart) + apple_sart_save_now(anv->sart); ret = apple_rtkit_idle(anv->rtk); if (ret) { dev_err(dev, "ANS warm idle failed: %d\n", ret); + /* No resume follows: drop the snapshot, it's still live. */ + if (anv->sart) + apple_sart_restore_early(anv->sart); return ret; } writel(0, anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL); From b0ced070aa6c02636ce62b74c43d8caa93dfe0a2 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sat, 28 Nov 2026 14:08:11 +1100 Subject: [PATCH 2/4] nvme: apple: keep a refused warm sleep's RTKit buffers while ANS uses them After a refused T8122 warm sleep, ANS comes back from the cold start on its old session's buffers: it reports on (IOP and AP power 0x0 -> 0x20) without asking for any new one. The previous change freed those buffers early and then wrote their SART entries back from the pre-idle snapshot, so ANS kept DMAing into freed memory through entries that used_entries no longer accounted for. Taking the snapshot again after the teardown instead removes the entries, and ANS's DMA is then refused: storage hangs (seen on J615). Keep them instead. While that restart runs, the shmem_destroy callback stashes the torn-down session's buffers, still allocated and still in the SART (and in used_entries), instead of freeing them, so the reinit no longer reads the SART inside the shared reset either; the early reinit and its SART power-word write go. The pre-idle snapshot written back now holds only live entries. The kept buffers are freed (with the SART out of reset) after a later start in which ANS allocates new buffers: the next one with a full handshake, such as after s2idle. If a start fails they stay kept, as ANS's state is unknown; buffers still kept at removal are not freed. Also log each SART entry written back from a snapshot (dev_dbg), with the boot loader's entries marked. Signed-off-by: Ryan Murray --- drivers/nvme/host/apple.c | 65 +++++++++++++++++++++++++++++---------- drivers/soc/apple/sart.c | 3 ++ 2 files changed, 51 insertions(+), 17 deletions(-) diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index 9fce198c18908..9bd6e175fb6cc 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -291,6 +291,15 @@ struct apple_nvme { bool t8122_warm_sleep; /* A refused warm sleep: the session's buffers are still mapped in the SART. */ bool t8122_warm_refused; + /* + * After a refused warm sleep ANS comes back on its old session's buffers and + * asks for none: they are kept, allocated and in the SART, until a start in + * which it asks for new ones. Still kept at removal, they are not freed. + */ + bool shmem_defer; + unsigned int shmem_setups; + unsigned int n_stale_shmem; + struct apple_rtkit_shmem stale_shmem[8]; unsigned long recovery_flags; void __iomem *mmio_coproc; @@ -669,14 +678,14 @@ static int apple_nvme_sart_dma_setup(void *cookie, bfr->buffer = NULL; return -ENOMEM; } + anv->shmem_setups++; return 0; } -static void apple_nvme_sart_dma_destroy(void *cookie, - struct apple_rtkit_shmem *bfr) +static void apple_nvme_shmem_free(struct apple_nvme *anv, + struct apple_rtkit_shmem *bfr) { - struct apple_nvme *anv = cookie; struct apple_nvme_rtkit_mapping *mapping = bfr->private; if (mapping) { @@ -690,6 +699,28 @@ static void apple_nvme_sart_dma_destroy(void *cookie, dma_free_coherent(anv->dev, bfr->size, bfr->buffer, bfr->iova); } +static void apple_nvme_sart_dma_destroy(void *cookie, + struct apple_rtkit_shmem *bfr) +{ + struct apple_nvme *anv = cookie; + + if (!anv->shmem_defer) { + apple_nvme_shmem_free(anv, bfr); + return; + } + if (anv->n_stale_shmem < ARRAY_SIZE(anv->stale_shmem)) + anv->stale_shmem[anv->n_stale_shmem++] = *bfr; + else + dev_warn(anv->dev, "RTKit buffer %pad+%#zx kept for good: no room to free it later\n", + &bfr->iova, bfr->size); +} + +static void apple_nvme_free_stale_shmem(struct apple_nvme *anv) +{ + while (anv->n_stale_shmem) + apple_nvme_shmem_free(anv, &anv->stale_shmem[--anv->n_stale_shmem]); +} + static const struct apple_rtkit_ops apple_nvme_rtkit_ops = { .crashed = apple_nvme_rtkit_crashed, .shmem_setup = apple_nvme_sart_dma_setup, @@ -1915,6 +1946,7 @@ static void apple_nvme_reset_work_impl(struct work_struct *work) bool inherited = !anv->owns_rtkit; bool fw_sleep; bool warm_restarted = false; + unsigned int setups = anv->shmem_setups; if (anv->hw->needs_ioq_registers) { mutex_lock(&anv->disable_lock); @@ -2064,21 +2096,13 @@ static void apple_nvme_reset_work_impl(struct work_struct *work) WRITE_ONCE(anv->fw_sleep_restart, false); /* - * A refused warm sleep idled ANS but kept its session for the - * restart, so the session's buffers are still in the SART, which - * the idle gated. Ungate it and free them now: the reset below - * can take the SART too (T8122 shares ANS's reset with it), and - * the reinit after it would then read a SART in reset. The rest - * is the start after a firmware sleep (fw_sleep). + * A refused warm sleep idled ANS but kept its session. ANS comes back + * from this start on that session's buffers and asks for none, so they + * stay allocated and in the SART (shmem_defer). Freeing them here would + * also read the SART in the reset below: T8122 shares ANS's reset. */ - if (xchg(&anv->t8122_warm_refused, false)) { - dev_dbg(anv->dev, "refused warm sleep: SART power %#x, saved %#x\n", - readl(anv->mmio_nvme + APPLE_ANS_SART_POWER), anv->sart_power); - writel(anv->sart_power, anv->mmio_nvme + APPLE_ANS_SART_POWER); - ret = apple_rtkit_reinit(anv->rtk); - if (ret) - goto out; - } + if (xchg(&anv->t8122_warm_refused, false)) + anv->shmem_defer = true; ret = reset_control_assert(anv->reset); if (ret) @@ -2146,6 +2170,11 @@ static void apple_nvme_reset_work_impl(struct work_struct *work) dev_dbg(anv->dev, "ANS booted successfully."); + /* Kept buffers go once ANS has asked for new ones (see shmem_defer). */ + anv->shmem_defer = false; + if (anv->n_stale_shmem && anv->shmem_setups != setups) + apple_nvme_free_stale_shmem(anv); + /* * Pre-M4 ANS keeps running across a kexec, and a crashed kernel hands * it to the crash kernel with the controller still enabled on its own @@ -2401,6 +2430,8 @@ static void apple_nvme_reset_work_impl(struct work_struct *work) } if (phase_locked) mutex_unlock(&anv->disable_lock); + /* ANS's state is unknown: buffers kept for it stay kept (see shmem_defer). */ + anv->shmem_defer = false; dev_warn(anv->ctrl.device, "Reset failure status: %d\n", ret); nvme_change_ctrl_state(&anv->ctrl, NVME_CTRL_DELETING); if (apple_nvme_disable(anv, false) || READ_ONCE(anv->removing)) diff --git a/drivers/soc/apple/sart.c b/drivers/soc/apple/sart.c index 14c0e65d19cbb..1bd8b4e24dc23 100644 --- a/drivers/soc/apple/sart.c +++ b/drivers/soc/apple/sart.c @@ -461,6 +461,9 @@ static unsigned int apple_sart_write_saved(struct apple_sart *sart) sart->ops->set_entry(sart, i, sart->saved[i].flags, sart->saved[i].paddr >> sart->ops->paddr_shift, sart->saved[i].size >> sart->ops->size_shift); + dev_dbg(sart->dev, "wrote back entry %d: flags %#x [%pa + %#zx]%s\n", i, + sart->saved[i].flags, &sart->saved[i].paddr, sart->saved[i].size, + test_bit(i, &sart->protected_entries) ? " (boot)" : ""); restored++; } return restored; From b58b62644dbf983b7d5199138a10f483271f6ce0 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sat, 28 Nov 2026 14:52:55 +1100 Subject: [PATCH 3/4] nvme: apple: free kept RTKit buffers only after ANS is shut down The previous change freed the buffers kept from a refused warm sleep after any start in which ANS allocated a new buffer. RTKit hands out buffers per endpoint, so a start that replaces only some of them would have freed others that ANS still uses. Free them instead where the driver already frees its session's own buffers: after ANS acknowledges an RTKit shutdown (AP QUIESCED, IOP SLEEP), with its SART still out of reset. All shutdowns of a running ANS go through apple_nvme_rtkit_shutdown() for that: the cold reset, s2idle's suspend, removal and system shutdown. If the shutdown fails, or ANS isn't running, the buffers stay kept. Track every kept buffer in a list (__GFP_NOFAIL for the small node), so that none can be left mapped without an owner. Signed-off-by: Ryan Murray --- drivers/nvme/host/apple.c | 63 ++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index 9bd6e175fb6cc..f69de49a585bd 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -293,13 +293,11 @@ struct apple_nvme { bool t8122_warm_refused; /* * After a refused warm sleep ANS comes back on its old session's buffers and - * asks for none: they are kept, allocated and in the SART, until a start in - * which it asks for new ones. Still kept at removal, they are not freed. + * asks for none: they are kept, allocated and in the SART, until ANS has been + * shut down cleanly (apple_nvme_rtkit_shutdown()). */ bool shmem_defer; - unsigned int shmem_setups; - unsigned int n_stale_shmem; - struct apple_rtkit_shmem stale_shmem[8]; + struct list_head kept_shmem; unsigned long recovery_flags; void __iomem *mmio_coproc; @@ -678,11 +676,14 @@ static int apple_nvme_sart_dma_setup(void *cookie, bfr->buffer = NULL; return -ENOMEM; } - anv->shmem_setups++; - return 0; } +struct apple_nvme_kept_shmem { + struct list_head node; + struct apple_rtkit_shmem bfr; +}; + static void apple_nvme_shmem_free(struct apple_nvme *anv, struct apple_rtkit_shmem *bfr) { @@ -704,21 +705,38 @@ static void apple_nvme_sart_dma_destroy(void *cookie, { struct apple_nvme *anv = cookie; + struct apple_nvme_kept_shmem *kept; + if (!anv->shmem_defer) { apple_nvme_shmem_free(anv, bfr); return; } - if (anv->n_stale_shmem < ARRAY_SIZE(anv->stale_shmem)) - anv->stale_shmem[anv->n_stale_shmem++] = *bfr; - else - dev_warn(anv->dev, "RTKit buffer %pad+%#zx kept for good: no room to free it later\n", - &bfr->iova, bfr->size); + /* Small, and a kept buffer must stay tracked. */ + kept = kmalloc(sizeof(*kept), GFP_KERNEL | __GFP_NOFAIL); + kept->bfr = *bfr; + list_add(&kept->node, &anv->kept_shmem); + dev_dbg(anv->dev, "keeping RTKit buffer %pad+%#zx while ANS uses it\n", + &bfr->iova, bfr->size); } -static void apple_nvme_free_stale_shmem(struct apple_nvme *anv) +/* + * Shut ANS down and free the buffers kept for it: once it acknowledges the shutdown it + * no longer uses them, the same point at which its session's own buffers are freed. + */ +static int apple_nvme_rtkit_shutdown(struct apple_nvme *anv) { - while (anv->n_stale_shmem) - apple_nvme_shmem_free(anv, &anv->stale_shmem[--anv->n_stale_shmem]); + struct apple_nvme_kept_shmem *kept, *tmp; + int ret; + + ret = apple_rtkit_shutdown(anv->rtk); + if (ret) + return ret; + list_for_each_entry_safe(kept, tmp, &anv->kept_shmem, node) { + list_del(&kept->node); + apple_nvme_shmem_free(anv, &kept->bfr); + kfree(kept); + } + return 0; } static const struct apple_rtkit_ops apple_nvme_rtkit_ops = { @@ -1946,7 +1964,6 @@ static void apple_nvme_reset_work_impl(struct work_struct *work) bool inherited = !anv->owns_rtkit; bool fw_sleep; bool warm_restarted = false; - unsigned int setups = anv->shmem_setups; if (anv->hw->needs_ioq_registers) { mutex_lock(&anv->disable_lock); @@ -2062,7 +2079,7 @@ static void apple_nvme_reset_work_impl(struct work_struct *work) if (anv->ctrl.ctrl_config & NVME_CC_ENABLE) apple_nvme_disable_legacy(anv, false); dev_dbg(anv->dev, "Trying to shut down RTKit before reset."); - ret = apple_rtkit_shutdown(anv->rtk); + ret = apple_nvme_rtkit_shutdown(anv); if (ret) goto out; @@ -2170,10 +2187,7 @@ static void apple_nvme_reset_work_impl(struct work_struct *work) dev_dbg(anv->dev, "ANS booted successfully."); - /* Kept buffers go once ANS has asked for new ones (see shmem_defer). */ anv->shmem_defer = false; - if (anv->n_stale_shmem && anv->shmem_setups != setups) - apple_nvme_free_stale_shmem(anv); /* * Pre-M4 ANS keeps running across a kexec, and a crashed kernel hands @@ -2430,7 +2444,7 @@ static void apple_nvme_reset_work_impl(struct work_struct *work) } if (phase_locked) mutex_unlock(&anv->disable_lock); - /* ANS's state is unknown: buffers kept for it stay kept (see shmem_defer). */ + /* ANS's state is unknown: buffers kept for it stay kept (see kept_shmem). */ anv->shmem_defer = false; dev_warn(anv->ctrl.device, "Reset failure status: %d\n", ret); nvme_change_ctrl_state(&anv->ctrl, NVME_CTRL_DELETING); @@ -2810,6 +2824,7 @@ static struct apple_nvme *apple_nvme_alloc(struct platform_device *pdev) mutex_init(&anv->disable_lock); spin_lock_init(&anv->event_lock); INIT_LIST_HEAD(&anv->quarantine_node); + INIT_LIST_HEAD(&anv->kept_shmem); anv->controller_start = resource->start; ret = devm_add_action_or_reset(dev, apple_nvme_put_resource_owner, anv); if (ret) @@ -3173,7 +3188,7 @@ static void apple_nvme_remove(struct platform_device *pdev) /* Post-M4 firmware stays live; see apple_nvme_shutdown(). */ if (!anv->hw->needs_ioq_registers && apple_rtkit_is_running(anv->rtk)) { - apple_rtkit_shutdown(anv->rtk); + apple_nvme_rtkit_shutdown(anv); writel(0, anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL); } @@ -3213,7 +3228,7 @@ static void apple_nvme_shutdown(struct platform_device *pdev) } if (apple_rtkit_is_running(anv->rtk)) { - apple_rtkit_shutdown(anv->rtk); + apple_nvme_rtkit_shutdown(anv); writel(0, anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL); } @@ -3586,7 +3601,7 @@ static int apple_nvme_suspend(struct device *dev) if (!ret) ret = apple_rtkit_reinit(anv->rtk); } else { - ret = apple_rtkit_shutdown(anv->rtk); + ret = apple_nvme_rtkit_shutdown(anv); } writel(0, anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL); From 718d68820a35454a159c9061b2a5a7755c673d32 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sat, 28 Nov 2026 15:26:54 +1100 Subject: [PATCH 4/4] soc: apple: sart: don't replay a region removed after the wake After a firmware-driven wake the SART keeps its snapshot for apple_sart_replay() (replay_valid), which a coprocessor's driver calls before it sets the run bit again, e.g. in the NVMe reset that follows a later failure. A region its driver removed in the meantime, freeing the memory behind it, stays in that snapshot, and the replay writes the entry back: the coprocessor can reach freed memory again. With nvme-apple, the buffers kept from a refused T8122 warm sleep and freed after a later shutdown are one case; the session's own buffers, freed by any RTKit shutdown after a wake, are another. Drop a removed region from the snapshot while it is kept for a replay. The bootloader's entries are never removed, so they stay. Before the wake, while the snapshot still waits for it, nothing changes: that wake writes back what was there when it was taken. Add a KUnit case: snapshot, a removal before the wake (kept), wake, removals after it (dropped; a protected entry can't be removed), power loss, replay. Signed-off-by: Ryan Murray --- drivers/soc/apple/sart-test.c | 63 +++++++++++++++++++++++++++++++++++ drivers/soc/apple/sart.c | 21 ++++++++++++ 2 files changed, 84 insertions(+) diff --git a/drivers/soc/apple/sart-test.c b/drivers/soc/apple/sart-test.c index 2095aaf7c2d97..89101eae64436 100644 --- a/drivers/soc/apple/sart-test.c +++ b/drivers/soc/apple/sart-test.c @@ -776,6 +776,68 @@ static void sart_test_replay(struct kunit *test) sart_test_check(test, t); } +/* + * A region removed after a wake, as a driver reclaiming a buffer does, is not brought back + * by a later replay; the bootloader's entry and the regions still in use are. One removed + * while the snapshot still waits for its wake stays in it: that wake writes it back as found. + */ +static void sart_test_replay_after_remove(struct kunit *test) +{ + struct sart_test *t = sart_test_new(test, test->param_value); + struct apple_sart *sart = &t->sart; + bool v3 = t->ver->size_reg; + + sart_test_preload(t, 0, t->ver->allow, 0x800000, 0x400); + sart->protected_entries = BIT(0); + KUNIT_ASSERT_EQ(test, 0, + apple_sart_add_allowed_region(sart, SART_TEST_PG(0x4000), + SART_TEST_PG(4))); + KUNIT_ASSERT_EQ(test, 0, + apple_sart_add_allowed_region(sart, SART_TEST_PG(0x812345), + SART_TEST_PG(0x40))); + KUNIT_ASSERT_EQ(test, 0, + apple_sart_add_allowed_region(sart, SART_TEST_PG(0x9000), + SART_TEST_PG(8))); + sart_test_model(t, 1, t->ver->allow, 0x4000, 4); + sart_test_model(t, 2, t->ver->allow, 0x812345, 0x40); + + sart_test_snapshot(test, t); + /* Before the wake: the snapshot keeps it */ + KUNIT_ASSERT_EQ(test, 0, + apple_sart_remove_allowed_region(sart, SART_TEST_PG(0x9000), + SART_TEST_PG(8))); + KUNIT_EXPECT_EQ(test, sart->saved[3].size, (size_t)SART_TEST_PG(8)); + + sart_test_power_loss(t); + pm_set_resume_via_firmware(); + KUNIT_ASSERT_EQ(test, 0, apple_sart_resume_noirq(sart->dev)); + KUNIT_ASSERT_TRUE(test, sart->replay_valid); + KUNIT_ASSERT_EQ(test, 0, + apple_sart_remove_allowed_region(sart, SART_TEST_PG(0x9000), + SART_TEST_PG(8))); + + /* After it: gone from the snapshot, and the protected entry can't be removed */ + KUNIT_ASSERT_EQ(test, 0, + apple_sart_remove_allowed_region(sart, SART_TEST_PG(0x4000), + SART_TEST_PG(4))); + sart_test_model(t, 1, 0, 0, 0); + KUNIT_EXPECT_EQ(test, sart->saved[1].size, (size_t)0); + KUNIT_EXPECT_EQ(test, sart->saved[1].flags, 0); + KUNIT_EXPECT_EQ(test, sart->saved[3].size, (size_t)0); + KUNIT_EXPECT_EQ(test, -EINVAL, + apple_sart_remove_allowed_region(sart, SART_TEST_PG(0x800000), + SART_TEST_PG(0x400))); + KUNIT_EXPECT_EQ(test, sart->saved[0].size, (size_t)SART_TEST_PG(0x400)); + + sart_test_power_loss(t); + if (!v3) { + KUNIT_EXPECT_EQ(test, -EOPNOTSUPP, apple_sart_replay(sart)); + return; + } + KUNIT_EXPECT_EQ(test, 0, apple_sart_replay(sart)); + sart_test_check(test, t); +} + /* The check before a sleep exists on SARTv4 only, and needs the snapshot */ static void sart_test_sleep_validate_unsupported(struct kunit *test) { @@ -936,6 +998,7 @@ static struct kunit_case sart_test_cases[] = { KUNIT_CASE_PARAM(sart_test_resume_idle_entries, sart_test_ver_gen_params), KUNIT_CASE_PARAM(sart_test_resume_last_in_use, sart_test_ver_gen_params), KUNIT_CASE_PARAM(sart_test_replay, sart_test_ver_gen_params), + KUNIT_CASE_PARAM(sart_test_replay_after_remove, sart_test_ver_gen_params), KUNIT_CASE_PARAM(sart_test_sleep_validate_unsupported, sart_test_ver_gen_params), KUNIT_CASE(sart_test_sleep_validate_changed), KUNIT_CASE(sart_test_sleep_validate_flags_only), diff --git a/drivers/soc/apple/sart.c b/drivers/soc/apple/sart.c index 1bd8b4e24dc23..af56e40297a39 100644 --- a/drivers/soc/apple/sart.c +++ b/drivers/soc/apple/sart.c @@ -363,6 +363,26 @@ int apple_sart_add_allowed_region(struct apple_sart *sart, phys_addr_t paddr, } EXPORT_SYMBOL_GPL(apple_sart_add_allowed_region); +/* + * A region its driver removes after a wake must not come back with a replay: drop it from + * the snapshot kept for apple_sart_replay(). The bootloader's entries stay. + */ +static void apple_sart_forget_replay(struct apple_sart *sart, phys_addr_t paddr, size_t size) +{ + int i; + + if (!sart->replay_valid) + return; + for (i = 0; i < APPLE_SART_MAX_ENTRIES; i++) { + if (test_bit(i, &sart->protected_entries) || + sart->saved[i].paddr != paddr || sart->saved[i].size != size) + continue; + sart->saved[i].flags = 0; + sart->saved[i].paddr = 0; + sart->saved[i].size = 0; + } +} + int apple_sart_remove_allowed_region(struct apple_sart *sart, phys_addr_t paddr, size_t size) { @@ -388,6 +408,7 @@ int apple_sart_remove_allowed_region(struct apple_sart *sart, phys_addr_t paddr, sart->ops->set_entry(sart, i, 0, 0, 0); clear_bit(i, &sart->used_entries); + apple_sart_forget_replay(sart, paddr, size); dev_dbg(sart->dev, "cleared entry %d\n", i); return 0; }