From bb4586bac2838d91508f5f5558ad31cc304615b6 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Fri, 20 Nov 2026 11:31:06 -0500 Subject: [PATCH 1/2] nvme: apple: T8122: idle ANS for a firmware sleep, as macOS does J613's NVMe node is apple,t8122-nvme-ans2 and falls back to the T8103 data, whose suspend shuts ANS down (AP quiesced, IOP sleep, AP off) and clears its run bit; at the commit its ASC then reads status 0x2a. macOS idles ANS before its sleep (AP then IOP power state 0x201), well before its PMP stops, and its ASC reads control 0, status 0x2e at the commit. Give T8122 its own data: T8103's values plus sleep_idle, behind sleep_untested. With it, ANS registers with the sleep driver, and its PMP report entry is found (the "ans" domain itself, or the entry on that power state). - Suspend: the shutdown notification goes out as before and nvme_disable_ctrl()'s result is kept (clearing CC.EN afterwards resets CSTS.SHST). If it completed, the report entry is known and ANS's PMP request can be held for the commit (commit-owned, as the G17 GPU's sleep holds its own), ANS is idled (AP then IOP idle, interrupts on) and marked ready; otherwise it is shut down as before and the sleep driver refuses the sleep. - Last stop: the PMP request is withdrawn, polled (an error fails the stop); the sleep driver then clears RUN alone. Nothing touches the NVMe window, which is unreachable at the commit on J613. - Resume: the ownership is dropped and the request made again, waiting for the PMP's acknowledgement; without it, I/O is held rather than the NVMe window touched. RUN is clear, so the reset work resets and boots ANS, as after a shutdown. ANS's ASC state is logged at suspend_late, suspend_noirq and each step of the last stop, and each branch logs which path it took. Signed-off-by: Satya Benson --- drivers/nvme/host/apple.c | 164 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 158 insertions(+), 6 deletions(-) diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index d743916e99398..9760886b89f4a 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -223,6 +224,8 @@ struct apple_nvme_hw { bool sleep_traced; /* ... or derived and never run: only with nvme_apple.sleep_untested */ bool sleep_untested; + /* A firmware sleep idles ANS (AP then IOP idle) instead of shutting it down. */ + bool sleep_idle; /* The last endpoint map reply from that trace */ u32 sleep_epmap_last_mask; /* Higher namespaces are the firmware's, and a Flush to one faults the controller. */ @@ -260,6 +263,8 @@ struct apple_nvme { bool sleep_stop; bool sleep_idled; u32 sleep_csts; + int legacy_shn_ret; /* the last legacy disable's shutdown notification */ + struct device_node *sleep_pmp_entry; /* T8122: ANS's PMP request, held to the last stop */ /* ANS came back across a sleep, or failed to. reset_work picks these up. */ bool sleep_restarted; bool sleep_failed; @@ -359,7 +364,8 @@ MODULE_PARM_DESC(flush_interval, "Grace period in msecs between flushes (ignored on post-M4 ANS)"); /* - * Stop ANS for a deep sleep on a controller whose sleep path was never run (T8142). Off by + * Stop ANS for a deep sleep on a controller whose sleep path was never run (T8142), or idle it + * as macOS does where that was never run through a resume (T8122). Off by * default: the root disk stays up through a sleep the SoC refuses. */ static bool sleep_untested; @@ -1296,6 +1302,7 @@ static void apple_nvme_disable_legacy(struct apple_nvme *anv, bool shutdown) bool dead = false, freeze = false; unsigned long flags; + anv->legacy_shn_ret = -ENODATA; if (apple_rtkit_is_crashed(anv->rtk)) dead = true; if (!(csts & NVME_CSTS_RDY)) @@ -1338,8 +1345,8 @@ static void apple_nvme_disable_legacy(struct apple_nvme *anv, bool shutdown) * NVMe controller is handed off with firmware shut down, in an * NVMe disabled state, after a clean shutdown). */ - if (shutdown) - nvme_disable_ctrl(&anv->ctrl, shutdown); + /* The notification's own result: CC.EN cleared below resets CSTS.SHST. */ + anv->legacy_shn_ret = shutdown ? nvme_disable_ctrl(&anv->ctrl, shutdown) : -ENODATA; nvme_disable_ctrl(&anv->ctrl, false); } @@ -2801,6 +2808,15 @@ static int apple_nvme_sleep_shutdown(void *data) * brings it back, so a failure goes on to the commit: what a flush acknowledged is on * flash, and the watchdog is the net. */ +/* T8122 trial: ANS's ASC state at each step from its idle to the commit. */ +static void apple_nvme_ans_trace(struct apple_nvme *anv, const char *step) +{ + if (anv->hw->sleep_idle && anv->sleep_stop) + dev_info(anv->dev, "ANS at %s: control %#x status %#x\n", step, + readl(anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL), + readl(anv->mmio_coproc + APPLE_ANS_COPROC_CPU_STATUS)); +} + static int apple_nvme_sleep_last_stop(void *data) { struct apple_nvme *anv = data; @@ -2808,17 +2824,46 @@ static int apple_nvme_sleep_last_stop(void *data) u32 cc, csts = anv->sleep_csts; int ret; - cc = readl(anv->mmio_nvme + NVME_REG_CC); - + /* T8122: the NVMe window is unreachable at the commit; ANS's own pages are not. */ + cc = anv->hw->sleep_idle ? 0 : readl(anv->mmio_nvme + NVME_REG_CC); + + /* T8122: already idled in the suspend; the sleep driver clears RUN after this. */ + if (anv->hw->sleep_idle && anv->sleep_idled) { + int wret; + + apple_nvme_ans_trace(anv, "the last stop"); + apple_rtkit_mailbox_recv_off(anv->rtk); + apple_nvme_ans_trace(anv, "mailbox receive off"); + wret = apple_pmp_report_entry_withdraw_polled(anv->sleep_pmp_entry); + apple_nvme_ans_trace(anv, "the PMP withdraw"); + if (wret) { + dev_err(anv->dev, "ANS's PMP request not withdrawn at the last stop: %d\n", wret); + return 1; + } + return 0; + } anv->sleep_idled = true; ret = apple_rtkit_sleep_idle_polled(anv->rtk, anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL, &us); if (ret) dev_err(anv->dev, "ANS did not idle: %d\n", ret); + else if (anv->hw->sleep_idle) + dev_info(anv->dev, "ANS idle at the last stop, acknowledged in about %u us\n", us); else dev_dbg(anv->dev, "ANS idle, acknowledged in about %u us, CC %#x CSTS %#x\n", us, cc, csts); apple_rtkit_mailbox_recv_off(anv->rtk); + /* + * T8122: ANS's PMP request was kept for this idle (macOS idles ANS well before its PMP + * stops); with the CPU stopped, give it up, as the G17 GPU's last stop does. + */ + if (anv->hw->sleep_idle && anv->sleep_pmp_entry) { + writel(readl(anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL) & + ~APPLE_ANS_COPROC_CPU_CONTROL_RUN, + anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL); + if (apple_pmp_report_entry_withdraw_polled(anv->sleep_pmp_entry)) + dev_err(anv->dev, "ANS's PMP request not withdrawn at the last stop\n"); + } return ret ? 1 : 0; } @@ -2870,6 +2915,42 @@ static int apple_nvme_probe(struct platform_device *pdev) apple_rtkit_set_warm_epmap_mask(anv->rtk, anv->hw->sleep_epmap_last_mask); } + if (anv->hw->sleep_idle && (!anv->hw->sleep_untested || sleep_untested)) { + dev_warn(anv->dev, "idle for sleep enabled, untested through a resume\n"); + anv->sleep_registered = + !apple_sleep_coproc_register(anv->dev, anv->mmio_coproc, false, + apple_nvme_sleep_last_stop, anv); + /* The shutdown notification is checked in the suspend, not at the commit. */ + if (anv->sleep_registered) { + int i = of_property_match_string(anv->dev->of_node, "power-domain-names", "ans"); + struct device_node *e = i < 0 ? NULL : + of_parse_phandle(anv->dev->of_node, "power-domains", i); + + /* + * The "ans" domain is the PMP report entry itself (APPLE_USE_PMP), or the + * power state the entry sits on: then the entry is the one on that state. + */ + if (e && !of_device_is_compatible(e, "apple,t6000-pmp-v2-report-entry")) { + struct device_node *r, *ps = e; + + e = NULL; + for_each_compatible_node(r, NULL, "apple,t6000-pmp-v2-report-entry") { + struct device_node *d = of_parse_phandle(r, "power-domains", 0); + + of_node_put(d); + if (d == ps && of_device_is_available(r)) { + e = of_node_get(r); + of_node_put(r); + break; + } + } + of_node_put(ps); + } + anv->sleep_pmp_entry = e; + dev_info(anv->dev, "ANS's PMP request for the sleep: %pOF\n", e); + } + } + apple_nvme_set_reset_ready(anv, true); nvme_reset_ctrl(&anv->ctrl); async_schedule(apple_nvme_async_probe, anv); @@ -3078,6 +3159,23 @@ static int apple_nvme_resume(struct device *dev) WRITE_ONCE(anv->suspended, false); return nvme_try_sched_reset(&anv->ctrl); } + if (anv->hw->sleep_idle && anv->sleep_stop) { + apple_sleep_coproc_set_ready(dev, false); + anv->sleep_stop = false; + anv->sleep_idled = false; + if (anv->sleep_pmp_entry) { + int ret; + + apple_pmp_report_entry_commit_owned(anv->sleep_pmp_entry, false); + /* The last stop gave up ANS's request; nothing touches NVMe until it's back. */ + ret = apple_pmp_report_entry_request(anv->sleep_pmp_entry, true); + if (ret) { + dev_crit(dev, "holding all I/O: ANS's PMP request not acknowledged after the sleep (%d), reboot to recover\n", + ret); + return 0; + } + } + } return nvme_reset_ctrl(&anv->ctrl); } @@ -3238,6 +3336,33 @@ static int apple_nvme_suspend(struct device *dev) } if (apple_rtkit_is_running(anv->rtk)) { + /* + * T8122: as macOS leaves ANS before its sleep, the sleep driver idles it last of + * all (AP then IOP idle, polled, apple_nvme_sleep_last_stop()) and clears its RUN + * bit; the shutdown notification above must have completed (its result is kept: the + * NVMe window is unreachable at the commit on J613, and CC.EN cleared after it resets + * CSTS.SHST). Otherwise it is shut down as before. Resume finds RUN + * clear and resets and boots ANS, as after a shutdown. + */ + if (anv->sleep_registered && anv->hw->sleep_idle && + pm_suspend_target_state == PM_SUSPEND_MEM && pm_suspend_via_firmware() && + !anv->legacy_shn_ret && anv->sleep_pmp_entry && + !apple_pmp_report_entry_commit_owned(anv->sleep_pmp_entry, true)) { + int idle = apple_rtkit_idle(anv->rtk); + + anv->sleep_stop = true; + /* Idled here, with interrupts on: at the commit, polled, it does not answer. */ + anv->sleep_idled = !idle; + apple_sleep_coproc_set_ready(dev, true); + dev_info(dev, "ANS idled for the sleep: %d (status %#x), its run bit left to the commit (notification %d)\n", + idle, readl(anv->mmio_coproc + APPLE_ANS_COPROC_CPU_STATUS), + anv->legacy_shn_ret); + return 0; + } + /* Registered but not idled at the commit: the sleep driver refuses the sleep. */ + if (anv->sleep_registered && pm_suspend_via_firmware()) + dev_warn(dev, "ANS shut down for the sleep instead (notification %d, PMP request %s): the sleep will be refused\n", + anv->legacy_shn_ret, anv->sleep_pmp_entry ? "found" : "not found"); ret = apple_rtkit_shutdown(anv->rtk); writel(0, anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL); @@ -3246,9 +3371,22 @@ static int apple_nvme_suspend(struct device *dev) return ret; } +static int apple_nvme_suspend_late(struct device *dev) +{ + apple_nvme_ans_trace(dev_get_drvdata(dev), "suspend_late"); + return 0; +} + +static int apple_nvme_suspend_noirq(struct device *dev) +{ + apple_nvme_ans_trace(dev_get_drvdata(dev), "suspend_noirq"); + return 0; +} + static const struct dev_pm_ops apple_nvme_pm_ops = { SYSTEM_SLEEP_PM_OPS(apple_nvme_suspend, apple_nvme_resume) - LATE_SYSTEM_SLEEP_PM_OPS(NULL, apple_nvme_resume_early) + LATE_SYSTEM_SLEEP_PM_OPS(apple_nvme_suspend_late, apple_nvme_resume_early) + NOIRQ_SYSTEM_SLEEP_PM_OPS(apple_nvme_suspend_noirq, NULL) }; static const struct apple_nvme_hw apple_nvme_t8015_hw = { @@ -3263,6 +3401,19 @@ static const struct apple_nvme_hw apple_nvme_t8103_hw = { .max_queue_depth = 64, }; +/* + * T8122 (M3) is laid out like T8103. macOS idles its ANS before a sleep (AP then IOP power + * state 0x201) and leaves the CPU stopped, where the T8103 path shuts it down. Untested through + * a resume: off unless sleep_untested is set. + */ +static const struct apple_nvme_hw apple_nvme_t8122_hw = { + .has_lsq_nvmmu = true, + .has_queue_count = true, + .max_queue_depth = 64, + .sleep_idle = true, + .sleep_untested = true, +}; + /* * The post-M4 (T8132 and later) I/O queue setup follows Yureka Lilian's T8132 * support in m1n1 (commit 53f8ee9b54ba, "nvme: support T8132"). @@ -3354,6 +3505,7 @@ static const struct of_device_id apple_nvme_of_match[] = { { .compatible = "apple,t6050-nvme-ans2", .data = &apple_nvme_t6050_hw }, { .compatible = "apple,t8015-nvme-ans2", .data = &apple_nvme_t8015_hw }, { .compatible = "apple,t8103-nvme-ans2", .data = &apple_nvme_t8103_hw }, + { .compatible = "apple,t8122-nvme-ans2", .data = &apple_nvme_t8122_hw }, { .compatible = "apple,t8132-nvme-ans2", .data = &apple_nvme_t8132_hw }, { .compatible = "apple,t8140-nvme-ans2", .data = &apple_nvme_t8140_hw }, { .compatible = "apple,t8142-nvme-ans2", .data = &apple_nvme_t8142_hw }, From d464093101fa70227ce606383369df42a213a333 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Sat, 21 Nov 2026 06:30:28 -0500 Subject: [PATCH 2/2] nvme: apple: fall back to shutdown if ANS cannot idle Mark ANS ready for the sleep commit only after RTKit acknowledges idle. Release the PMP request hold on failure and let the existing shutdown path run so the sleep can be refused. Put the retained PMP entry node in device resource cleanup and move the last-stop comment back to its callback. Signed-off-by: Satya Benson --- drivers/nvme/host/apple.c | 43 +++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index 9760886b89f4a..de389badb0a99 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -400,6 +400,8 @@ static void apple_nvme_put_resource_owner(void *data) { struct apple_nvme *anv = data; + of_node_put(anv->sleep_pmp_entry); + anv->sleep_pmp_entry = NULL; WRITE_ONCE(anv->detached, true); if (!apple_nvme_m5_retained(anv)) kref_put(&anv->ref, apple_nvme_release); @@ -2803,11 +2805,6 @@ static int apple_nvme_sleep_shutdown(void *data) return -EIO; } -/* - * The rest of a sleep's stop, last of all. From the first idle message only a real sleep - * brings it back, so a failure goes on to the commit: what a flush acknowledged is on - * flash, and the watchdog is the net. - */ /* T8122 trial: ANS's ASC state at each step from its idle to the commit. */ static void apple_nvme_ans_trace(struct apple_nvme *anv, const char *step) { @@ -2817,6 +2814,11 @@ static void apple_nvme_ans_trace(struct apple_nvme *anv, const char *step) readl(anv->mmio_coproc + APPLE_ANS_COPROC_CPU_STATUS)); } +/* + * The rest of a sleep's stop, last of all. From the first idle message only a real sleep + * brings it back, so a failure goes on to the commit: what a flush acknowledged is on + * flash, and the watchdog is the net. + */ static int apple_nvme_sleep_last_stop(void *data) { struct apple_nvme *anv = data; @@ -3337,27 +3339,28 @@ static int apple_nvme_suspend(struct device *dev) if (apple_rtkit_is_running(anv->rtk)) { /* - * T8122: as macOS leaves ANS before its sleep, the sleep driver idles it last of - * all (AP then IOP idle, polled, apple_nvme_sleep_last_stop()) and clears its RUN - * bit; the shutdown notification above must have completed (its result is kept: the - * NVMe window is unreachable at the commit on J613, and CC.EN cleared after it resets - * CSTS.SHST). Otherwise it is shut down as before. Resume finds RUN - * clear and resets and boots ANS, as after a shutdown. + * T8122: idle ANS here with interrupts on, then leave its RUN + * bit for the last stop. The shutdown notification result must + * be checked here too: clearing CC.EN resets CSTS.SHST, and + * the NVMe window is unreachable at the commit on J613. + * If either step fails, shut ANS down so resume can reset it. */ if (anv->sleep_registered && anv->hw->sleep_idle && pm_suspend_target_state == PM_SUSPEND_MEM && pm_suspend_via_firmware() && !anv->legacy_shn_ret && anv->sleep_pmp_entry && !apple_pmp_report_entry_commit_owned(anv->sleep_pmp_entry, true)) { - int idle = apple_rtkit_idle(anv->rtk); + ret = apple_rtkit_idle(anv->rtk); + if (!ret) { + anv->sleep_stop = true; + anv->sleep_idled = true; + apple_sleep_coproc_set_ready(dev, true); + dev_info(dev, "ANS idled for the sleep (status %#x), its run bit left to the commit\n", + readl(anv->mmio_coproc + APPLE_ANS_COPROC_CPU_STATUS)); + return 0; + } - anv->sleep_stop = true; - /* Idled here, with interrupts on: at the commit, polled, it does not answer. */ - anv->sleep_idled = !idle; - apple_sleep_coproc_set_ready(dev, true); - dev_info(dev, "ANS idled for the sleep: %d (status %#x), its run bit left to the commit (notification %d)\n", - idle, readl(anv->mmio_coproc + APPLE_ANS_COPROC_CPU_STATUS), - anv->legacy_shn_ret); - return 0; + apple_pmp_report_entry_commit_owned(anv->sleep_pmp_entry, false); + dev_warn(dev, "ANS could not idle for the sleep: %d\n", ret); } /* Registered but not idled at the commit: the sleep driver refuses the sleep. */ if (anv->sleep_registered && pm_suspend_via_firmware())