diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index e29dd701a8484..f69de49a585bd 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -289,6 +289,15 @@ 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; + /* + * 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 ANS has been + * shut down cleanly (apple_nvme_rtkit_shutdown()). + */ + bool shmem_defer; + struct list_head kept_shmem; unsigned long recovery_flags; void __iomem *mmio_coproc; @@ -667,14 +676,17 @@ static int apple_nvme_sart_dma_setup(void *cookie, bfr->buffer = NULL; return -ENOMEM; } - return 0; } -static void apple_nvme_sart_dma_destroy(void *cookie, - struct apple_rtkit_shmem *bfr) +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) { - struct apple_nvme *anv = cookie; struct apple_nvme_rtkit_mapping *mapping = bfr->private; if (mapping) { @@ -688,6 +700,45 @@ 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; + + struct apple_nvme_kept_shmem *kept; + + if (!anv->shmem_defer) { + apple_nvme_shmem_free(anv, bfr); + return; + } + /* 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); +} + +/* + * 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) +{ + 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 = { .crashed = apple_nvme_rtkit_crashed, .shmem_setup = apple_nvme_sart_dma_setup, @@ -2028,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; @@ -2061,6 +2112,15 @@ 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. 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)) + anv->shmem_defer = true; + ret = reset_control_assert(anv->reset); if (ret) goto out; @@ -2127,6 +2187,8 @@ static void apple_nvme_reset_work_impl(struct work_struct *work) dev_dbg(anv->dev, "ANS booted successfully."); + anv->shmem_defer = false; + /* * 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 @@ -2382,6 +2444,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 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); if (apple_nvme_disable(anv, false) || READ_ONCE(anv->removing)) @@ -2760,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) @@ -3123,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); } @@ -3163,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); } @@ -3197,8 +3262,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 +3563,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); @@ -3522,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); 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 14c0e65d19cbb..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; } @@ -461,6 +482,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;