From 12092c0bafb6771d06ec3cbdc78622a010bfc07c Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 13 Nov 2026 21:18:14 +1100 Subject: [PATCH 1/2] soc: apple: sart: write back entries a boot stage passed on A boot stage that powers a post-M4 coprocessor off loses its SART entries with the power, while the firmware keeps the buffers they cover. The stage passes the entries on, and the next owner has to write them back before the run bit. Add apple_sart_restore_entry(). It writes one entry, reads it back, and marks it protected like an entry the boot loader left, so Linux never clears it and apple_sart_is_inherited_region() covers it. Assisted-by: LLM Co-Authored-By: Claude Opus 5.5 Signed-off-by: Ryan Murray --- drivers/soc/apple/sart.c | 29 +++++++++++++++++++++++++++++ include/linux/soc/apple/sart.h | 7 +++++++ 2 files changed, 36 insertions(+) diff --git a/drivers/soc/apple/sart.c b/drivers/soc/apple/sart.c index 4fa31a0e54f47..f03704950012c 100644 --- a/drivers/soc/apple/sart.c +++ b/drivers/soc/apple/sart.c @@ -325,6 +325,35 @@ static int sart_set_entry(struct apple_sart *sart, int index, u8 flags, return 0; } +/* + * Write back entries that a boot stage set up before it powered the coprocessor off, and + * protect them as the boot loader's own: the firmware keeps the buffers they cover. + */ +int apple_sart_restore_entry(struct apple_sart *sart, int index, u8 flags, + phys_addr_t paddr, size_t size) +{ + phys_addr_t rb_paddr; + size_t rb_size; + u8 rb_flags; + + if (index < 0 || index >= APPLE_SART_MAX_ENTRIES || !flags || + paddr & ((1ULL << sart->ops->paddr_shift) - 1) || + size & ((1ULL << sart->ops->size_shift) - 1) || + (size >> sart->ops->size_shift) > sart->ops->size_max) + return -EINVAL; + sart->ops->set_entry(sart, index, flags, paddr >> sart->ops->paddr_shift, + size >> sart->ops->size_shift); + sart->ops->get_entry(sart, index, &rb_flags, &rb_paddr, &rb_size); + if (rb_flags != flags || rb_paddr != paddr || rb_size != size) { + dev_warn(sart->dev, "entry %d reads %#x %pa+%#zx, wrote %#x %pa+%#zx\n", + index, rb_flags, &rb_paddr, rb_size, flags, &paddr, size); + return -EIO; + } + set_bit(index, &sart->protected_entries); + return 0; +} +EXPORT_SYMBOL_GPL(apple_sart_restore_entry); + int apple_sart_add_allowed_region(struct apple_sart *sart, phys_addr_t paddr, size_t size) { diff --git a/include/linux/soc/apple/sart.h b/include/linux/soc/apple/sart.h index 53186e759e55d..c41fafcaa6abd 100644 --- a/include/linux/soc/apple/sart.h +++ b/include/linux/soc/apple/sart.h @@ -70,4 +70,11 @@ bool apple_sart_is_inherited_region(struct apple_sart *sart, phys_addr_t paddr, */ int apple_sart_replay(struct apple_sart *sart); +/* + * apple_sart_restore_entry - write back a SART entry that a boot stage passed on + * (apple,sart-entries) after it powered the coprocessor off, and protect it. + */ +int apple_sart_restore_entry(struct apple_sart *sart, int index, u8 flags, + phys_addr_t paddr, size_t size); + #endif /* _LINUX_SOC_APPLE_SART_H_ */ From d2539ff03ac5fbd9a9c516cec85a108f4fbb9559 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 13 Nov 2026 21:18:14 +1100 Subject: [PATCH 2/2] nvme: apple: start a post-M4 ANS that a boot stage powered off aura idles a post-M4 ANS when it is done with it, powers it and its PCIe block off, and passes the SART entries on in apple,sart-entries (three 64-bit cells per entry: index << 8 | flags, address, size). U-Boot starts ANS from that state. Linux could only adopt a running session, so every stage before it had to hand ANS over live. When the probe finds ANS stopped and apple,sart-entries present, the first reset work now starts it from off: reset ANS if its CPU isn't in its reset state or its boot status survived the power cycle, write the entries back, and start the firmware as after a power loss (no INIT, the endpoint map's last part masked to endpoint 0x20, no endpoint started again). The controller is then set up as on any start. A firmware that doesn't answer gets one more reset and start. Without the property nothing changes: a running session is adopted and iBoot's session gets its handshake as before. Assisted-by: LLM Co-Authored-By: Claude Opus 5.5 Signed-off-by: Ryan Murray --- drivers/nvme/host/apple.c | 96 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index f0892f4986b94..ff6f032a83662 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -55,6 +55,7 @@ #define APPLE_ANS_COPROC_CPU_CONTROL 0x44 #define APPLE_ANS_COPROC_CPU_CONTROL_RUN BIT(4) #define APPLE_ANS_COPROC_CPU_STATUS 0x48 +#define APPLE_ANS_COPROC_CPU_STATUS_RESET BIT(3) #define APPLE_ANS_ACQ_DB 0x1004 #define APPLE_ANS_IOCQ_DB 0x100c @@ -276,6 +277,9 @@ struct apple_nvme { struct reset_control *reset; bool owns_rtkit; bool inherited_rtkit; + /* apple,sart-entries: a boot stage powered ANS off and passed its SART entries on */ + const __be64 *sart_entries; + int sart_entries_len; struct dma_pool *prp_page_pool; struct dma_pool *prp_small_pool; @@ -1695,6 +1699,80 @@ static void apple_nvme_stop_crashed_session(struct apple_nvme *anv) anv->ctrl.ctrl_config = 0; } +/* + * Post-M4, idled and powered off by the boot stage that used it (as aura and U-Boot do + * between their own stages): its firmware stays in DRAM and the SART lost its entries. + * The stage passed them as apple,sart-entries, three 64-bit cells each: (index << 8 | + * flags), address and size. Write them back, then start the firmware as after a power + * loss: it says HELLO by itself, keeps the buffers it was given, and the endpoint map is + * answered with its last part masked to endpoint 0x20, as macOS does after a sleep. + * + * A power cycle leaves the CPU in its reset state and the boot status cleared. If either + * survived, the CPU was only stopped since it last ran and won't start from the run bit, + * so reset it first. A firmware that doesn't answer gets one more reset and start. + */ +static int apple_nvme_start_from_off(struct apple_nvme *anv) +{ + void __iomem *control = anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL; + int i, n = anv->sart_entries_len / (3 * sizeof(__be64)); + u32 status, boot_status; + int ret, attempt; + + for (attempt = 0;; attempt++) { + status = readl(anv->mmio_coproc + APPLE_ANS_COPROC_CPU_STATUS); + boot_status = readl(anv->mmio_nvme + APPLE_ANS_BOOT_STATUS); + if (attempt || !(status & APPLE_ANS_COPROC_CPU_STATUS_RESET) || + boot_status == APPLE_ANS_BOOT_STATUS_OK) { + dev_info(anv->dev, "ANS from off: resetting it first (status %#x, boot status %#x)\n", + status, boot_status); + writel(0, control); + ret = reset_control_assert(anv->reset); + if (!ret) { + usleep_range(10, 20); + ret = reset_control_deassert(anv->reset); + } + if (ret) + return ret; + } + + for (i = 0; i < n; i++) { + u64 key = be64_to_cpu(anv->sart_entries[3 * i]); + + ret = apple_sart_restore_entry(anv->sart, key >> 8, key & 0xff, + be64_to_cpu(anv->sart_entries[3 * i + 1]), + be64_to_cpu(anv->sart_entries[3 * i + 2])); + if (ret) { + dev_err(anv->dev, "ANS from off: SART entry %llu: %d\n", + key >> 8, ret); + return ret; + } + } + + apple_rtkit_set_warm_epmap_mask(anv->rtk, BIT(0)); + ret = apple_rtkit_power_cycled_return(anv->rtk, control); + if (!ret) + ret = readl_poll_timeout(anv->mmio_nvme + APPLE_ANS_BOOT_STATUS, + boot_status, + boot_status == APPLE_ANS_BOOT_STATUS_OK, + USEC_PER_MSEC, APPLE_ANS_BOOT_TIMEOUT); + if (!ret || attempt || !anv->reset) { + if (anv->hw->sleep_traced) + apple_rtkit_set_warm_epmap_mask(anv->rtk, + anv->hw->sleep_epmap_last_mask); + else + apple_rtkit_set_warm_epmap_mask(anv->rtk, U32_MAX); + break; + } + dev_warn(anv->dev, "ANS from off didn't start (%d, status %#x), resetting it again\n", + ret, readl(anv->mmio_coproc + APPLE_ANS_COPROC_CPU_STATUS)); + } + if (ret) + return ret; + + dev_info(anv->dev, "ANS started from off, %d SART entries written back\n", n); + return 0; +} + static bool apple_nvme_sleep_wait_on(struct apple_nvme *anv); static void apple_nvme_reset_work(struct work_struct *work) @@ -1743,6 +1821,12 @@ static void apple_nvme_reset_work(struct work_struct *work) goto out; } + if (anv->sart_entries) { + ret = apple_nvme_start_from_off(anv); + anv->sart_entries = NULL; + goto booted; + } + /* * Post-M4 firmware can be handed over live, but cannot renegotiate HELLO * and EPMAP across boot stages. Adopt the first healthy inherited session; @@ -2599,6 +2683,18 @@ static struct apple_nvme *apple_nvme_alloc(struct platform_device *pdev) * system endpoints before mailbox RX starts, otherwise inherited syslog * traffic can race the later reset work and be dropped as undiscovered. */ + if (anv->hw->needs_ioq_registers && + !(readl(anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL) & + APPLE_ANS_COPROC_CPU_CONTROL_RUN)) { + anv->sart_entries = of_get_property(dev->of_node, "apple,sart-entries", + &anv->sart_entries_len); + if (anv->sart_entries && + (!anv->sart_entries_len || anv->sart_entries_len % (3 * sizeof(__be64)))) { + dev_warn(dev, "malformed apple,sart-entries (%d bytes)\n", + anv->sart_entries_len); + anv->sart_entries = NULL; + } + } inherited_rtkit = apple_nvme_can_adopt_rtkit(anv); /* Publish admission before the initializer enables mailbox RX. */ anv->inherited_rtkit = inherited_rtkit;