From ac0befe72a5e33b6ae20f77ac8d9c207ffd15887 Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Fri, 27 Nov 2026 11:17:48 +0000 Subject: [PATCH 01/33] nvme: apple: keep the RTKit buffers of ANS across a firmware sleep For a suspend-to-RAM that goes through firmware, a controller without the warm sleep sequence is put in IDLE, not shut down: apple_nvme_suspend() saves the SART's entries, calls apple_rtkit_idle() and then apple_rtkit_reinit(). The reinit frees every buffer the firmware was given (syslog, crash log, ioreport, oslog) with dma_free_coherent() and takes its range out of the SART. On the wake the reset work calls apple_rtkit_reinit() once more, writes the saved SART entries back, the freed ranges among them, and starts ANS. An idled ANS keeps its state. On T6000 (M1 Pro, J314s), after the wake it sends HELLO, the kernel logs "RTKit: Initializing (protocol version 12)", the endpoints are started, and the firmware then sends syslog records without a buffer request or a SYSLOG_INIT before them: RTKit: received syslog message but no syslog_msg_buffer logged at each sleep and each wake. The firmware still writes the records' text to the buffer it had before the sleep, which the page allocator has handed to someone else by then, and which the SART admits again. With an out-of-tree T6000 sleep sequence that freed and re-admitted the buffers the same way, ANS's syslog text was found in a process's page tables ("Bad page map"), followed by an oops. Keep the buffers for as long as the firmware may use them. Add apple_rtkit_reinit_retaining_buffers(), which resets a session's handshake state as apple_rtkit_reinit() does and leaves the shared buffers, their SART ranges and the syslog geometry alone, and use it for the idle before the sleep and for the restart after it. Both ways the firmware can come back are then served by the code that is there: - a firmware that resumes with its state never asks, keeps writing to memory that is still its own, and its syslog records are read and printed again; - a firmware that starts from scratch asks again. A request of the same size for an endpoint that has a buffer is answered with that buffer by apple_rtkit_common_rx_get_buffer(), and a SYSLOG_INIT replaces the geometry. Nothing is allocated per sleep. The buffers are released as before by apple_rtkit_free(), or by the apple_rtkit_reinit() of a controller reset, which follows a shutdown the firmware acknowledged. The path is not specific to T6000: T8103 (M1) takes it as well. The T8122 warm sequence does not reinitialize and is unchanged. Fixes: 7dfc208abc18 ("nvme-apple: idle ANS for a firmware sleep and start it cold after the wake") Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/nvme/host/apple.c | 16 +++++++-- drivers/soc/apple/rtkit.c | 57 ++++++++++++++++++++++----------- include/linux/soc/apple/rtkit.h | 10 ++++++ 3 files changed, 62 insertions(+), 21 deletions(-) diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index a28913018466a..aab3dd96cf507 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -2280,7 +2280,14 @@ static void apple_nvme_reset_work_impl(struct work_struct *work) if (!anv->hw->needs_ioq_registers) apple_nvme_reset_fenced(anv); - ret = apple_rtkit_reinit(anv->rtk); + /* + * ANS was idled for the sleep, not shut down, and comes back with the + * buffers it had: it logs to its syslog buffer without asking again. + */ + if (fw_sleep) + ret = apple_rtkit_reinit_retaining_buffers(anv->rtk); + else + ret = apple_rtkit_reinit(anv->rtk); if (ret) goto out; @@ -3743,6 +3750,11 @@ static int apple_nvme_suspend(struct device *dev) * after its power-on it then brings its flash links back itself. * In IDLE it gates its SART, so the SART's entries and the word * that gates it are read before that. + * + * An idled ANS keeps its state, and after the wake it goes on + * writing to the buffers it was given without asking for them + * again. They stay allocated and in the SART: only the session's + * handshake state is reset. */ anv->sart_power = readl(anv->mmio_nvme + APPLE_ANS_SART_POWER); if (anv->sart) @@ -3750,7 +3762,7 @@ static int apple_nvme_suspend(struct device *dev) anv->fw_sleep_idled = true; ret = apple_rtkit_idle(anv->rtk); if (!ret) - ret = apple_rtkit_reinit(anv->rtk); + ret = apple_rtkit_reinit_retaining_buffers(anv->rtk); } else { ret = apple_rtkit_shutdown(anv->rtk); } diff --git a/drivers/soc/apple/rtkit.c b/drivers/soc/apple/rtkit.c index 4b873f1fdd464..616ed4bf7b7ad 100644 --- a/drivers/soc/apple/rtkit.c +++ b/drivers/soc/apple/rtkit.c @@ -2684,8 +2684,11 @@ int apple_rtkit_port_restart(struct apple_rtkit *rtk) } EXPORT_SYMBOL_GPL(apple_rtkit_port_restart); -/* Everything of a firmware session but the mailbox hardware. */ -static void apple_rtkit_reset_session(struct apple_rtkit *rtk) +/* + * Everything of a firmware session but the mailbox hardware and, with + * @keep_buffers, the shared buffers and the syslog geometry. + */ +static void apple_rtkit_reset_session(struct apple_rtkit *rtk, bool keep_buffers) { unsigned long flags; @@ -2694,23 +2697,25 @@ static void apple_rtkit_reset_session(struct apple_rtkit *rtk) flush_workqueue(rtk->wq); cancel_delayed_work_sync(&rtk->crashlog_late_work); - apple_rtkit_free_buffer(rtk, &rtk->coreanalytics_buffer); - apple_rtkit_free_buffer(rtk, &rtk->ioreport_buffer); - apple_rtkit_free_buffer(rtk, &rtk->crashlog_buffer); - apple_rtkit_free_buffer(rtk, &rtk->oslog_buffer); - apple_rtkit_free_buffer(rtk, &rtk->pmp_aux_buffer); - apple_rtkit_free_buffer(rtk, &rtk->syslog_buffer); - apple_rtkit_free_buffer(rtk, &rtk->kdebug_buffer0); - apple_rtkit_free_buffer(rtk, &rtk->kdebug_buffer1); + if (!keep_buffers) { + apple_rtkit_free_buffer(rtk, &rtk->coreanalytics_buffer); + apple_rtkit_free_buffer(rtk, &rtk->ioreport_buffer); + apple_rtkit_free_buffer(rtk, &rtk->crashlog_buffer); + apple_rtkit_free_buffer(rtk, &rtk->oslog_buffer); + apple_rtkit_free_buffer(rtk, &rtk->pmp_aux_buffer); + apple_rtkit_free_buffer(rtk, &rtk->syslog_buffer); + apple_rtkit_free_buffer(rtk, &rtk->kdebug_buffer0); + apple_rtkit_free_buffer(rtk, &rtk->kdebug_buffer1); - kfree(rtk->syslog_msg_buffer); + kfree(rtk->syslog_msg_buffer); - rtk->syslog_msg_buffer = NULL; - rtk->syslog_n_entries = 0; - rtk->syslog_msg_size = 0; - rtk->syslog_inherited = false; - rtk->crashlog_inherited = false; - rtk->oslog_inherited = false; + rtk->syslog_msg_buffer = NULL; + rtk->syslog_n_entries = 0; + rtk->syslog_msg_size = 0; + rtk->syslog_inherited = false; + rtk->crashlog_inherited = false; + rtk->oslog_inherited = false; + } bitmap_zero(rtk->endpoints, APPLE_RTKIT_MAX_ENDPOINTS); set_bit(APPLE_RTKIT_EP_MGMT, rtk->endpoints); @@ -2739,7 +2744,7 @@ int apple_rtkit_reinit(struct apple_rtkit *rtk) { int ret; - apple_rtkit_reset_session(rtk); + apple_rtkit_reset_session(rtk, false); ret = apple_mbox_start(rtk->mbox); if (ret) { apple_rtkit_detach_rx(rtk); @@ -2749,6 +2754,20 @@ int apple_rtkit_reinit(struct apple_rtkit *rtk) } EXPORT_SYMBOL_GPL(apple_rtkit_reinit); +int apple_rtkit_reinit_retaining_buffers(struct apple_rtkit *rtk) +{ + int ret; + + apple_rtkit_reset_session(rtk, true); + ret = apple_mbox_start(rtk->mbox); + if (ret) { + apple_rtkit_detach_rx(rtk); + flush_workqueue(rtk->wq); + } + return ret; +} +EXPORT_SYMBOL_GPL(apple_rtkit_reinit_retaining_buffers); + static int apple_rtkit_set_ap_power_state(struct apple_rtkit *rtk, unsigned int state) { @@ -3075,7 +3094,7 @@ int apple_rtkit_cold_restart(struct apple_rtkit *rtk, int (*start)(void *), void int ret; /* A fresh boot: no warm or port-only restart may answer its endpoint map. */ - apple_rtkit_reset_session(rtk); + apple_rtkit_reset_session(rtk, false); rtk->warm_restart = false; rtk->port_only = false; rtk->warm_last_mask = U32_MAX; diff --git a/include/linux/soc/apple/rtkit.h b/include/linux/soc/apple/rtkit.h index b0edf3d2b9ec1..0cb938d551351 100644 --- a/include/linux/soc/apple/rtkit.h +++ b/include/linux/soc/apple/rtkit.h @@ -238,6 +238,16 @@ void apple_rtkit_free_retaining_buffers(struct apple_rtkit *rtk); */ int apple_rtkit_reinit(struct apple_rtkit *rtk); +/* + * As apple_rtkit_reinit(), for a co-processor that was idled, not shut down, + * and so may keep its state across the reset: the shared buffers stay + * allocated and, through the client's shmem_setup, admitted. A firmware that + * asks for a buffer again is given the one it had; one that does not keeps + * using it, and its syslog is still served. A later apple_rtkit_reinit() or + * apple_rtkit_free() releases them. + */ +int apple_rtkit_reinit_retaining_buffers(struct apple_rtkit *rtk); + /* * Set how long each handshake step (power state and endpoint map replies) * waits for the co-processor, for one that is known to answer slowly. Call From 2f60f7d2bfeda703fcf210ad1e583d42a798e1f9 Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:20:14 +0000 Subject: [PATCH 02/33] PCI: apple: wait once after the last retrained link on S2R resume apple_pcie_hib_port_up() slept 100 ms after each link it retrained, the time PCIe r5.0 section 6.6.1 requires before the first configuration request. No configuration request is sent until every port is up, so one wait after the last retrained link covers them all. On a MacBook Pro 14" (M1 Pro, J314s), where the radio and the card reader ports both come back cold, this takes 100 ms off each wake. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/pci/controller/pcie-apple.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index f21ca71bcb946..62bf94624894a 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -7866,8 +7866,6 @@ static int apple_pcie_hib_port_up(struct apple_pcie *pcie, struct apple_pcie_por } apple_pcie_port_writel(port, BIT(PORT_INT_LINK_UP), PORT_INTSTAT); apple_pcie_port_writel(port, port->saved_intmask, PORT_INTMSK); - /* 100 ms after the link before configuration requests (PCIe r5.0, 6.6.1). */ - msleep(100); return 0; } @@ -7877,6 +7875,7 @@ static int apple_pcie_resume(struct device *dev) struct apple_pcie *pcie = apple_pcie_lookup(dev); struct apple_pcie_port *port; struct apple_pcie_hib_fn *fn; + bool trained = false; int ret; if (!pcie->hib_saved) @@ -7910,9 +7909,16 @@ static int apple_pcie_resume(struct device *dev) continue; } apple_pcie_port_enable_irq(port); + trained = true; dev_info(dev, "S2R: %pOF link back, PWREN and PERST# cycled as at probe\n", port->np); } + /* + * 100 ms after the last link before configuration requests (PCIe r5.0, + * 6.6.1); every earlier link has waited longer. + */ + if (trained) + msleep(100); list_for_each_entry(fn, &pcie->hib_fns, entry) { port = apple_pcie_get_port(fn->pdev); if (!port || !port->hib_cold) From 1c3375c256c6a852268b9cb5f8f686837ab829d2 Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Sat, 28 Nov 2026 02:49:09 +0000 Subject: [PATCH 03/33] PCI: apple: disable ASPM and L1SS across firmware sleep on reinit hosts On conventional Apple PCIe hosts that reinitialize their links after a firmware-driven suspend-to-RAM (apple_pcie_s2r_reinit()), endpoints such as the Wi-Fi/Bluetooth and SD card reader links on T6000 (M1 Pro) can run with ASPM L1, ClockPM and L1.1/L1.2 substates enabled on the default kernel command line. When those endpoints transition to D3hot during suspend, the link can drop CLKREQ# ahead of the sleep commit's APCIe bridge quiesce and hang suspend entry unless pcie_aspm=off is passed. Save and clear ASPM, ClockPM and L1SS on each downstream link of a reinit host in apple_pcie_prepare() for firmware-driven sleeps, and restore them in apple_pcie_complete() after resume or on rollback. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/pci/controller/pcie-apple.c | 234 +++++++++++++++++++++++++++- tools/asahi/pciec-policy-tests.py | 9 ++ 2 files changed, 239 insertions(+), 4 deletions(-) diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 62bf94624894a..d55109115460f 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -655,6 +655,7 @@ struct apple_pcie { unsigned long *bitmap; struct list_head ports; struct list_head hib_fns; + struct list_head hib_aspm; bool hib_saved; struct completion event; struct irq_fwspec fwspec; @@ -6010,6 +6011,7 @@ static int apple_pcie_probe(struct platform_device *pdev) mutex_init(&pcie->lock); INIT_LIST_HEAD(&pcie->ports); INIT_LIST_HEAD(&pcie->hib_fns); + INIT_LIST_HEAD(&pcie->hib_aspm); /* Resolve PM dependencies before publishing the PCI hierarchy. */ if (pcie->hw->tunneled) { @@ -7731,6 +7733,195 @@ struct apple_pcie_hib_fn { struct pci_saved_state *state; }; +/* + * Saved ASPM/ClockPM/L1SS state for one link of a conventional reinit host across + * a firmware-driven sleep. Endpoints entering D3hot with L1/L1SS enabled can drop + * CLKREQ# before the sleep commit quiesces the APCIe bridge and hang the suspend. + */ +struct apple_pcie_hib_aspm { + struct list_head entry; + struct pci_dev *parent; + struct pci_dev *child; + bool has_l1ss; + u32 pl_ctl2; + u32 pl_ctl1; + u32 cl_ctl2; + u32 cl_ctl1; + u16 pl_lnkctl; + u16 fn_lnkctl[8]; + u8 fn_devfn[8]; + unsigned int nfn; +}; + +struct apple_pcie_aspm_walk { + struct apple_pcie *pcie; + int err; +}; + +static int apple_pcie_hib_disable_aspm_cb(struct pci_dev *parent, void *data) +{ + struct apple_pcie_aspm_walk *walk = data; + struct apple_pcie *pcie = walk->pcie; + struct apple_pcie_hib_aspm *saved; + struct pci_dev *fn, *child = NULL; + u32 id; + u16 mask = PCI_EXP_LNKCTL_ASPMC | PCI_EXP_LNKCTL_CLKREQ_EN; + unsigned int i; + + if (!pcie_downstream_port(parent) || !parent->subordinate) + return 0; + if (pci_read_config_dword(parent, PCI_VENDOR_ID, &id) || id == U32_MAX) + return 0; + + saved = kzalloc_obj(*saved); + if (!saved) { + walk->err = -ENOMEM; + return 1; + } + saved->parent = pci_dev_get(parent); + + if (pcie_capability_read_word(parent, PCI_EXP_LNKCTL, &saved->pl_lnkctl)) + goto err_free; + + list_for_each_entry(fn, &parent->subordinate->devices, bus_list) { + if (saved->nfn >= ARRAY_SIZE(saved->fn_lnkctl)) + break; + if (pci_read_config_dword(fn, PCI_VENDOR_ID, &id) || id == U32_MAX) + continue; + if (pcie_capability_read_word(fn, PCI_EXP_LNKCTL, + &saved->fn_lnkctl[saved->nfn])) + continue; + saved->fn_devfn[saved->nfn++] = fn->devfn; + if (!child && fn->l1ss) + child = fn; + } + + if (!saved->nfn) + goto err_free; + + if (parent->l1ss && child) { + if (!pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL2, + &saved->pl_ctl2) && + !pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1, + &saved->pl_ctl1) && + !pci_read_config_dword(child, child->l1ss + PCI_L1SS_CTL2, + &saved->cl_ctl2) && + !pci_read_config_dword(child, child->l1ss + PCI_L1SS_CTL1, + &saved->cl_ctl1) && + saved->pl_ctl1 != U32_MAX && saved->cl_ctl1 != U32_MAX) { + saved->child = pci_dev_get(child); + saved->has_l1ss = true; + } + } + + pci_dbg(parent, "S2R: clearing ASPM/L1SS (rp LnkCtl=%#x L1SS=%#x, ep LnkCtl=%#x L1SS=%#x)\n", + saved->pl_lnkctl, saved->has_l1ss ? saved->pl_ctl1 : 0, + saved->fn_lnkctl[0], saved->has_l1ss ? saved->cl_ctl1 : 0); + + /* Clear ASPM and ClockPM downstream before upstream, then clear L1SS. */ + list_for_each_entry(fn, &parent->subordinate->devices, bus_list) { + for (i = 0; i < saved->nfn; i++) { + if (saved->fn_devfn[i] == fn->devfn && (saved->fn_lnkctl[i] & mask)) { + pcie_capability_write_word(fn, PCI_EXP_LNKCTL, + saved->fn_lnkctl[i] & ~mask); + break; + } + } + } + if (saved->pl_lnkctl & mask) + pcie_capability_write_word(parent, PCI_EXP_LNKCTL, saved->pl_lnkctl & ~mask); + + if (saved->has_l1ss && ((saved->pl_ctl1 | saved->cl_ctl1) & PCI_L1SS_CTL1_L1SS_MASK)) { + pci_write_config_dword(saved->child, saved->child->l1ss + PCI_L1SS_CTL1, + saved->cl_ctl1 & ~PCI_L1SS_CTL1_L1SS_MASK); + pci_write_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1, + saved->pl_ctl1 & ~PCI_L1SS_CTL1_L1SS_MASK); + } + + list_add_tail(&saved->entry, &pcie->hib_aspm); + return 0; + +err_free: + pci_dev_put(saved->parent); + kfree(saved); + return 0; +} + +static void apple_pcie_hib_restore_aspm(struct apple_pcie *pcie) +{ + struct apple_pcie_hib_aspm *saved, *tmp; + struct pci_dev *fn; + u32 id; + u16 cur, mask = PCI_EXP_LNKCTL_ASPMC | PCI_EXP_LNKCTL_CLKREQ_EN; + unsigned int i; + + list_for_each_entry_safe(saved, tmp, &pcie->hib_aspm, entry) { + struct pci_dev *parent = saved->parent; + + list_del(&saved->entry); + if (parent->subordinate && + !pci_read_config_dword(parent, PCI_VENDOR_ID, &id) && id != U32_MAX) { + if (saved->has_l1ss && + !pci_read_config_dword(saved->child, PCI_VENDOR_ID, &id) && + id != U32_MAX && + ((saved->pl_ctl1 | saved->cl_ctl1) & PCI_L1SS_CTL1_L1SS_MASK)) { + /* Keep ASPM clear while programming L1SS timing and enables. */ + list_for_each_entry(fn, &parent->subordinate->devices, bus_list) { + if (!pcie_capability_read_word(fn, PCI_EXP_LNKCTL, &cur) && + (cur & PCI_EXP_LNKCTL_ASPMC)) + pcie_capability_write_word(fn, PCI_EXP_LNKCTL, + cur & + ~PCI_EXP_LNKCTL_ASPMC); + } + if (!pcie_capability_read_word(parent, PCI_EXP_LNKCTL, &cur) && + (cur & PCI_EXP_LNKCTL_ASPMC)) + pcie_capability_write_word(parent, PCI_EXP_LNKCTL, + cur & ~PCI_EXP_LNKCTL_ASPMC); + pci_write_config_dword(parent, parent->l1ss + PCI_L1SS_CTL2, + saved->pl_ctl2); + pci_write_config_dword(saved->child, + saved->child->l1ss + PCI_L1SS_CTL2, + saved->cl_ctl2); + pci_write_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1, + saved->pl_ctl1 & ~PCI_L1SS_CTL1_L1_2_MASK); + pci_write_config_dword(saved->child, + saved->child->l1ss + PCI_L1SS_CTL1, + saved->cl_ctl1 & ~PCI_L1SS_CTL1_L1_2_MASK); + if ((saved->pl_ctl1 | saved->cl_ctl1) & PCI_L1SS_CTL1_L1_2_MASK) { + pci_write_config_dword(parent, + parent->l1ss + PCI_L1SS_CTL1, + saved->pl_ctl1); + pci_write_config_dword(saved->child, + saved->child->l1ss + PCI_L1SS_CTL1, + saved->cl_ctl1); + } + } + if ((saved->pl_lnkctl & mask) && + !pcie_capability_read_word(parent, PCI_EXP_LNKCTL, &cur)) + pcie_capability_write_word(parent, PCI_EXP_LNKCTL, + (cur & ~mask) | + (saved->pl_lnkctl & mask)); + list_for_each_entry(fn, &parent->subordinate->devices, bus_list) { + for (i = 0; i < saved->nfn; i++) { + if (saved->fn_devfn[i] == fn->devfn && + (saved->fn_lnkctl[i] & mask) && + !pcie_capability_read_word(fn, PCI_EXP_LNKCTL, &cur)) { + pcie_capability_write_word(fn, PCI_EXP_LNKCTL, + (cur & ~mask) | + (saved->fn_lnkctl[i] & + mask)); + break; + } + } + } + } + if (saved->child) + pci_dev_put(saved->child); + pci_dev_put(parent); + kfree(saved); + } +} + /* A real sleep takes a conventional host's power: its ports are set up again as at probe. */ static bool apple_pcie_s2r_reinit(struct apple_pcie *pcie) { @@ -7940,9 +8131,29 @@ static int apple_pcie_resume(struct device *dev) return 0; } +static int apple_pcie_hib_prepare_aspm(struct apple_pcie *pcie) +{ + struct pci_host_bridge *bridge; + struct apple_pcie_aspm_walk walk = { .pcie = pcie }; + + if (!pcie || !apple_pcie_s2r_reinit(pcie) || !pm_suspend_via_firmware() || + pm_suspend_target_state == PM_SUSPEND_ON) + return 0; + + bridge = pci_host_bridge_from_priv(pcie); + if (!bridge || !bridge->bus) + return 0; + + pci_walk_bus(bridge->bus, apple_pcie_hib_disable_aspm_cb, &walk); + if (walk.err) + apple_pcie_hib_restore_aspm(pcie); + return walk.err; +} + static int apple_pcie_prepare(struct device *dev) { struct apple_pcie *pcie = apple_pcie_lookup(dev); + int ret; /* * A host-owned PCIe-C DART sleeps only inside the NHI's deep-sleep @@ -7964,8 +8175,6 @@ static int apple_pcie_prepare(struct device *dev) */ if (pcie && pm_suspend_target_state != PM_SUSPEND_ON && apple_pcie_adopted_sleep_host(pcie)) { - int ret; - /* A firmware suspend: the port and DART apple_pcie_t6050_sleep_down() takes down */ if (pm_suspend_via_firmware()) ret = pcie->t6050_port && pcie->t6050_dart ? 0 : -EBUSY; @@ -7978,12 +8187,29 @@ static int apple_pcie_prepare(struct device *dev) return ret; } } - return apple_radio_prepare(dev); + ret = apple_radio_prepare(dev); + if (ret) + return ret; + ret = apple_pcie_hib_prepare_aspm(pcie); + if (ret) { + apple_radio_complete(dev); + return ret; + } + return 0; +} + +static void apple_pcie_complete(struct device *dev) +{ + struct apple_pcie *pcie = apple_pcie_lookup(dev); + + if (pcie && !list_empty(&pcie->hib_aspm)) + apple_pcie_hib_restore_aspm(pcie); + apple_radio_complete(dev); } static const struct dev_pm_ops apple_pcie_pm_ops = { .prepare = apple_pcie_prepare, - .complete = apple_radio_complete, + .complete = apple_pcie_complete, .suspend_noirq = apple_pcie_suspend_noirq, .resume_noirq = apple_pcie_resume_noirq, .resume = apple_pcie_resume, diff --git a/tools/asahi/pciec-policy-tests.py b/tools/asahi/pciec-policy-tests.py index 7093382b9c10d..41de88c3f936c 100644 --- a/tools/asahi/pciec-policy-tests.py +++ b/tools/asahi/pciec-policy-tests.py @@ -270,6 +270,15 @@ def function(source, name): radio_prepare_calls++; return 0; } +static void apple_radio_complete(struct device *dev) +{ + (void)dev; +} +static int apple_pcie_hib_prepare_aspm(struct apple_pcie *pcie) +{ + (void)pcie; + return 0; +} static int dev_pm_genpd_add_notifier(struct device *dev, struct notifier_block *nb) { From 2b6ad1400b7d4648b7aa3e243c270d1f0239c02b Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:20:26 +0000 Subject: [PATCH 04/33] HID: spi-hid-apple: do not arm the wake interrupt for a firmware sleep A firmware sleep (suspend-to-RAM) cuts the power of the SoC, the pin controller and the SPI controller. Keys and the trackpad wake the machine through the SMC there, not through the controller's interrupt. The interrupt armed for wake is still pending when the AIC comes back and is counted as a wakeup event after every wake, so a wake the SMC took for the lid or the charger alone cannot go back to sleep. Arm it only when the suspend does not go through firmware. For a firmware sleep the controller is powered off and booted again on resume, as for a device that may not wake the system. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/hid/spi-hid/spi-hid-apple-core.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/hid/spi-hid/spi-hid-apple-core.c b/drivers/hid/spi-hid/spi-hid-apple-core.c index 1cfdbc7eae48a..1e357145de28e 100644 --- a/drivers/hid/spi-hid/spi-hid-apple-core.c +++ b/drivers/hid/spi-hid/spi-hid-apple-core.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -1324,6 +1325,11 @@ static int spihid_apple_core_suspend(struct device *dev) { int ret; struct spihid_apple *spihid = spi_get_drvdata(to_spi_device(dev)); + /* + * A firmware sleep wakes through the SMC, and an armed interrupt would be + * counted as a wakeup event after every wake. + */ + bool wake = device_may_wakeup(dev) && !pm_suspend_via_firmware(); if (spihid->tp.hid) { ret = hid_driver_suspend(spihid->tp.hid, PMSG_SUSPEND); @@ -1357,7 +1363,7 @@ static int spihid_apple_core_suspend(struct device *dev) * system. Its state is kept, so resume does not reboot it. */ spihid->irq_wake_enabled = false; - if (device_may_wakeup(dev)) { + if (wake) { ret = spihid->ops->enable_irq_wake(spihid->ops); if (!ret) { spihid->irq_wake_enabled = true; From 5a610ea3e1acc5292441f5c54e382e2acfed606f Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:20:46 +0000 Subject: [PATCH 05/33] dmaengine: apple-sio: keep the firmware running in a sleep dry run A SoC whose commit does not stop SIO has its firmware stopped at the late suspend with apple_rtkit_sleep_stop(). A dry run of the sleep refuses before the commit, so the firmware was stopped for a sleep that never happened. Return before the stop when apple_sleep_dry_run() holds. The busy-channel check and the refusal for a SoC that needs the stop at its commit still run, so the dry run reports what a real sleep would refuse. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/dma/apple-sio.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/dma/apple-sio.c b/drivers/dma/apple-sio.c index b6f5286904459..a1800bfe747d2 100644 --- a/drivers/dma/apple-sio.c +++ b/drivers/dma/apple-sio.c @@ -2313,6 +2313,10 @@ static __maybe_unused int sio_suspend_late(struct device *dev) return -EBUSY; } + /* A dry run ends before the commit: the firmware would be stopped for nothing. */ + if (apple_sleep_dry_run()) + return 0; + ret = apple_rtkit_sleep_stop(sio->rtk, sio->base + REG_CPU_CONTROL); if (ret) { dev_err(dev, "firmware did not stop for sleep: %d\n", ret); From f502d2d01e270f399e7b5dee206fa907bc53e4e9 Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Sat, 28 Nov 2026 02:49:11 +0000 Subject: [PATCH 06/33] dt-bindings: mfd: apple,smc: add apple,sleep-low-pins Before its sleep notice to the SMC, macOS switches off supplies that are wired to SMC GPIOs and that no driver switches at that point. On the J314s (MacBook Pro 14", M1 Pro) it is GPIO 26, the SD card reader's power enable. Which SMC GPIO powers what is board wiring. apple,sleep-low-pins lists the SMC GPIO numbers to drive low before the sleep notice and high again after the wake. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- .../devicetree/bindings/mfd/apple,smc.yaml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Documentation/devicetree/bindings/mfd/apple,smc.yaml b/Documentation/devicetree/bindings/mfd/apple,smc.yaml index 255209498bd59..f3c1e26109fcb 100644 --- a/Documentation/devicetree/bindings/mfd/apple,smc.yaml +++ b/Documentation/devicetree/bindings/mfd/apple,smc.yaml @@ -69,6 +69,19 @@ properties: what the SoC's sleep sequence powers on itself. Without the property pmVC is not written. + apple,sleep-low-pins: + $ref: /schemas/types.yaml#/definitions/uint32-array + minItems: 1 + maxItems: 4 + items: + maximum: 0xff + description: + SMC GPIO numbers (the number in the GPIO's gPxx key) that are driven + low before the SMC is told of a suspend-to-RAM and high again after the + wake, for a supply that has to be off while the machine sleeps and that + no driver switches at that point. On the J314s this is GPIO 26, the SD + card reader's power enable. + gpio: oneOf: - $ref: /schemas/gpio/apple,smc-gpio.yaml From bcb5f5dae2510c41c85231b638705309a5ef89be Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Sat, 28 Nov 2026 02:49:11 +0000 Subject: [PATCH 07/33] dt-bindings: soc: apple: sleep: add the T6000 sleep controller Add "apple,t6000-sleep" for the M1 Pro, with the 22 register blocks its suspend-to-RAM sequence writes. Its wake sources are armed from apple,wake-enable, which is required. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- .../bindings/soc/apple/apple,t8140-sleep.yaml | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml b/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml index d83c4e312f20e..6cc6381b73f22 100644 --- a/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml +++ b/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml @@ -19,6 +19,7 @@ description: | properties: compatible: enum: + - apple,t6000-sleep - apple,t8103-sleep - apple,t8122-sleep - apple,t8132-sleep @@ -107,6 +108,42 @@ required: - apple,display-coprocessors allOf: + - if: + properties: + compatible: + contains: + const: apple,t6000-sleep + then: + required: + - apple,wake-enable + properties: + reg: + minItems: 22 + maxItems: 22 + reg-names: + items: + - const: ps + - const: ps-east + - const: ps-aon + - const: cpu-start + - const: cluster + - const: commit + - const: wdt + - const: wake + - const: sep-pg + - const: acc0 + - const: acc1 + - const: acc2 + - const: clkgen + - const: pll0 + - const: dcs + - const: apcie-q + - const: amcc0 + - const: amcc1 + - const: amcc2 + - const: amcc3 + - const: ptd + - const: spmi - if: properties: compatible: From ba6c77a1ac6a856765fa628b814f1a0e8c461162 Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:21:33 +0000 Subject: [PATCH 08/33] irqchip/apple-aic: keep AIC2's configuration across a firmware suspend A firmware-driven suspend powers the interrupt controller down with the rest of the SoC. AIC2 comes back with its global configuration, its per-interrupt target words and its masks at their reset values. The AIC3 variants already save and restore that state through aic_syscore_ops, selected by .fw_suspend. The saved registers are AIC2's own layout, which AIC3 inherited, so set .fw_suspend for AIC2 as well. Both operations do nothing unless the suspend goes through firmware, so an AIC2 SoC without a sleep driver only allocates the save array. Needed for suspend-to-RAM on T6000 (M1 Pro). Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/irqchip/irq-apple-aic.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/irqchip/irq-apple-aic.c b/drivers/irqchip/irq-apple-aic.c index a13eb5c1fcb2c..15610a16032b7 100644 --- a/drivers/irqchip/irq-apple-aic.c +++ b/drivers/irqchip/irq-apple-aic.c @@ -317,6 +317,8 @@ static const struct aic_info aic2_info __initconst = { .fast_ipi = true, .local_fast_ipi = true, + /* As AIC3: the configuration goes with the SoC's power in a firmware-driven suspend. */ + .fw_suspend = true, }; static const struct aic_info aic3_info __initconst = { From d1d9af514d032af1b5561f99b380b1cffb23a214 Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:22:16 +0000 Subject: [PATCH 09/33] soc: apple: rtkit: split the endpoint start from its state check apple_rtkit_start_ep() refuses an application endpoint unless the firmware is running. A restart through the full boot handshake has to start the application endpoints before that state is reached. Move the message into apple_rtkit_start_announced_ep(), which checks only that the endpoint map announced the endpoint, and keep the state check in apple_rtkit_start_ep(). No functional change. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/soc/apple/rtkit.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/drivers/soc/apple/rtkit.c b/drivers/soc/apple/rtkit.c index 616ed4bf7b7ad..3ae3fba9276cc 100644 --- a/drivers/soc/apple/rtkit.c +++ b/drivers/soc/apple/rtkit.c @@ -2177,16 +2177,14 @@ void apple_rtkit_adopt_ep(struct apple_rtkit *rtk, u8 ep) } EXPORT_SYMBOL_GPL(apple_rtkit_adopt_ep); -int apple_rtkit_start_ep(struct apple_rtkit *rtk, u8 endpoint) +/* The start message alone, for an endpoint the firmware's endpoint map announced. */ +static int apple_rtkit_start_announced_ep(struct apple_rtkit *rtk, u8 endpoint) { u64 msg; int ret; if (!test_bit(endpoint, rtk->endpoints)) return -EINVAL; - if (endpoint >= APPLE_RTKIT_APP_ENDPOINT_START && - !apple_rtkit_is_running(rtk)) - return -EINVAL; msg = FIELD_PREP(APPLE_RTKIT_MGMT_STARTEP_EP, endpoint); msg |= APPLE_RTKIT_MGMT_STARTEP_FLAG; @@ -2201,6 +2199,15 @@ int apple_rtkit_start_ep(struct apple_rtkit *rtk, u8 endpoint) set_bit(endpoint, rtk->started); return ret; } + +int apple_rtkit_start_ep(struct apple_rtkit *rtk, u8 endpoint) +{ + if (endpoint >= APPLE_RTKIT_APP_ENDPOINT_START && + !apple_rtkit_is_running(rtk)) + return -EINVAL; + + return apple_rtkit_start_announced_ep(rtk, endpoint); +} EXPORT_SYMBOL_GPL(apple_rtkit_start_ep); static void apple_rtkit_mark_running(struct apple_rtkit *rtk) From d9edd49ee8c03d87c4549f72e835a5a9189277b3 Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:22:39 +0000 Subject: [PATCH 10/33] soc: apple: rtkit: restore the mailbox enables after a firmware sleep apple_rtkit_sleep_return() restarts a coprocessor parked by apple_rtkit_sleep_stop(). The mailbox is not stopped on that path, so apple_mbox_start() finds it started and writes nothing. Where the mailbox's block lost power in the sleep, its interrupt enables and outbox enable are back at their reset values, the firmware's HELLO raises no interrupt and the handshake times out. That is the case for SIO and the PMP on T6000 (M1 Pro). Call apple_rtkit_mbox_restore() before the run bit is set. It writes the enables apple_mbox_start() wrote at the first start, so a mailbox that kept them is unchanged. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/soc/apple/rtkit.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/soc/apple/rtkit.c b/drivers/soc/apple/rtkit.c index 3ae3fba9276cc..378593f734015 100644 --- a/drivers/soc/apple/rtkit.c +++ b/drivers/soc/apple/rtkit.c @@ -3536,6 +3536,10 @@ int apple_rtkit_sleep_return(struct apple_rtkit *rtk, void __iomem *cpu_control) } ret = apple_rtkit_warm_restart(rtk); + if (ret) + return ret; + /* apple_mbox_start() skips a started mailbox: write the enables a sleep may have reset. */ + ret = apple_rtkit_mbox_restore(rtk); if (ret) return ret; From 390b848243cae08944d559f24e1426d2c172198f Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:22:48 +0000 Subject: [PATCH 11/33] soc: apple: rtkit: add apple_rtkit_sleep_return_with_init() apple_rtkit_sleep_return() expects a firmware that restarts warm, with its buffers and endpoints kept. On T6000 (M1 Pro) the display coprocessor and the GPU, both on firmware 13.5, boot from scratch after a sleep that took their power: they wait for IOP power state INIT before HELLO, send the endpoint map again and have every application endpoint closed. Treated as a warm restart they never answer. Add apple_rtkit_sleep_return_with_init() for them. It resets the session expecting a new endpoint map, restores the mailbox's enables, sets the run bit, sends IOP power state INIT and waits for the map and the power state acknowledgment as at a first boot. It then starts every application endpoint that was started before the sleep and sets the AP power state on. The restart preparation is shared with apple_rtkit_warm_restart() through apple_rtkit_prepare_restart(); the warm case is unchanged. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/soc/apple/rtkit.c | 68 ++++++++++++++++++++++++++++++++- include/linux/soc/apple/rtkit.h | 6 +++ 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/drivers/soc/apple/rtkit.c b/drivers/soc/apple/rtkit.c index 378593f734015..b82cb8aa72693 100644 --- a/drivers/soc/apple/rtkit.c +++ b/drivers/soc/apple/rtkit.c @@ -2210,6 +2210,21 @@ int apple_rtkit_start_ep(struct apple_rtkit *rtk, u8 endpoint) } EXPORT_SYMBOL_GPL(apple_rtkit_start_ep); +/* Every application endpoint that was started before, started again in endpoint order. */ +static int apple_rtkit_restart_app_endpoints(struct apple_rtkit *rtk) +{ + unsigned long endpoint = APPLE_RTKIT_APP_ENDPOINT_START; + int ret; + + for_each_set_bit_from(endpoint, rtk->started, APPLE_RTKIT_MAX_ENDPOINTS) { + ret = apple_rtkit_start_announced_ep(rtk, endpoint); + if (ret) + return ret; + } + + return 0; +} + static void apple_rtkit_mark_running(struct apple_rtkit *rtk) { static const u8 system_endpoints[] = { @@ -2639,8 +2654,11 @@ EXPORT_SYMBOL_GPL(apple_rtkit_set_handshake_timeout); * given and its rings, so unlike apple_rtkit_reinit() nothing is freed, and * endpoints aren't started again. The caller then sets the run bit and calls * apple_rtkit_boot(). + * + * With @full_handshake the firmware sends its endpoint map again: the map is cleared, and + * the caller starts the endpoints. */ -int apple_rtkit_warm_restart(struct apple_rtkit *rtk) +static int apple_rtkit_prepare_restart(struct apple_rtkit *rtk, bool full_handshake) { unsigned long flags; int ret; @@ -2652,8 +2670,13 @@ int apple_rtkit_warm_restart(struct apple_rtkit *rtk) reinit_completion(&rtk->iop_pwr_ack_completion); reinit_completion(&rtk->ap_pwr_ack_completion); + if (full_handshake) { + bitmap_zero(rtk->endpoints, APPLE_RTKIT_MAX_ENDPOINTS); + set_bit(APPLE_RTKIT_EP_MGMT, rtk->endpoints); + rtk->boot_result = -EINPROGRESS; + } rtk->crashed = false; - rtk->warm_restart = true; + rtk->warm_restart = !full_handshake; rtk->iop_power_state = APPLE_RTKIT_PWR_STATE_OFF; rtk->ap_power_state = APPLE_RTKIT_PWR_STATE_OFF; kfifo_reset(&rtk->sync_ack_fifo); @@ -2672,6 +2695,11 @@ int apple_rtkit_warm_restart(struct apple_rtkit *rtk) } return ret; } + +int apple_rtkit_warm_restart(struct apple_rtkit *rtk) +{ + return apple_rtkit_prepare_restart(rtk, false); +} EXPORT_SYMBOL_GPL(apple_rtkit_warm_restart); void apple_rtkit_set_warm_epmap_mask(struct apple_rtkit *rtk, u32 last_mask) @@ -3549,6 +3577,42 @@ int apple_rtkit_sleep_return(struct apple_rtkit *rtk, void __iomem *cpu_control) } EXPORT_SYMBOL_GPL(apple_rtkit_sleep_return); +int apple_rtkit_sleep_return_with_init(struct apple_rtkit *rtk, void __iomem *cpu_control) +{ + int ret, ap_ret; + u64 msg; + + ret = apple_rtkit_prepare_restart(rtk, true); + if (ret) + return ret; + ret = apple_rtkit_mbox_restore(rtk); + if (ret) + return ret; + + writel(readl(cpu_control) | APPLE_ASC_CPU_CONTROL_RUN, cpu_control); + msg = FIELD_PREP(APPLE_RTKIT_MGMT_PWR_STATE, APPLE_RTKIT_PWR_STATE_INIT); + ret = apple_rtkit_management_send(rtk, APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE, msg); + if (ret) + return ret; + + ret = apple_rtkit_wait(rtk, &rtk->epmap_completion); + if (ret) + return ret; + if (rtk->boot_result) + return rtk->boot_result; + ret = apple_rtkit_wait(rtk, &rtk->iop_pwr_ack_completion); + if (ret) + return ret; + if ((rtk->iop_power_state & 0xff) != APPLE_RTKIT_PWR_STATE_ON) + return -EPROTO; + + /* The AP's state goes on even if an endpoint didn't start: the first error is returned. */ + ret = apple_rtkit_restart_app_endpoints(rtk); + ap_ret = apple_rtkit_set_ap_power_state(rtk, APPLE_RTKIT_PWR_STATE_ON); + return ret ?: ap_ret; +} +EXPORT_SYMBOL_GPL(apple_rtkit_sleep_return_with_init); + int apple_rtkit_power_cycled_return(struct apple_rtkit *rtk, void __iomem *cpu_control) { int ret = apple_rtkit_warm_restart(rtk); diff --git a/include/linux/soc/apple/rtkit.h b/include/linux/soc/apple/rtkit.h index 0cb938d551351..63513775abac6 100644 --- a/include/linux/soc/apple/rtkit.h +++ b/include/linux/soc/apple/rtkit.h @@ -433,6 +433,12 @@ int apple_rtkit_sleep_stop(struct apple_rtkit *rtk, void __iomem *cpu_control); */ int apple_rtkit_sleep_return(struct apple_rtkit *rtk, void __iomem *cpu_control); +/* + * As apple_rtkit_sleep_return(), for a firmware that boots from scratch: the whole handshake, + * then the endpoints that were started. The caller checks pm_resume_via_firmware(). + */ +int apple_rtkit_sleep_return_with_init(struct apple_rtkit *rtk, void __iomem *cpu_control); + /* * A start as after a real sleep, for a co-processor its driver power-cycled without one: * the warm restart, the run bit set, then RTKit's handshake. From 0c356401c474d4f1c0c7797bfb2c92d5be511e30 Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:55:09 +0000 Subject: [PATCH 12/33] soc: apple: sep: accept a power-gated enclave as a taken sleep request After the sleep request is sent, the enclave's answer is polled for until it arrives, the power word shows DEV_DISABLE, or 5 s pass. The M1 generation never sets DEV_DISABLE: an enclave that is down shows an actual state of 0, which the wait after the poll already accepts through sep_ps_down(). An enclave that gated before its answer was read ran the poll to its timeout, and the request was treated as failed, which is fatal after the send. T6000 (M1 Pro) was brought up with this case accepted. Use sep_ps_down() for the poll as well, and accept a request with no answer read and the domain off. An answer that reports an error still fails, and so does a timeout with the domain on. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/soc/apple/pm_shim.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/drivers/soc/apple/pm_shim.c b/drivers/soc/apple/pm_shim.c index 221634f03a3db..7d6a039d30dac 100644 --- a/drivers/soc/apple/pm_shim.c +++ b/drivers/soc/apple/pm_shim.c @@ -311,7 +311,7 @@ static int sep_pm_commit_stop(void *data) if (ret) goto out; c->sent = true; - while (!c->answered && !((c->state = readl(c->ps)) & SEP_PS_DEV_DISABLE) && + while (!c->answered && !sep_ps_down(c->state = readl(c->ps)) && c->answer_us < SEP_PM_TIMEOUT_US) { ret = apple_mbox_poll_one(c->mbox); if (ret < 0) @@ -330,8 +330,17 @@ static int sep_pm_commit_stop(void *data) return ret; } /* Preserve the failure reason without another hardware query. */ - if (ret >= 0 && !c->answered) - ret = c->state & SEP_PS_DEV_DISABLE ? -EHOSTDOWN : -ETIMEDOUT; + if (ret >= 0 && !c->answered) { + if (c->state & SEP_PS_DEV_DISABLE) + ret = -EHOSTDOWN; + else if (FIELD_GET(SEP_PS_ACTUAL, c->state)) + ret = -ETIMEDOUT; + /* + * Otherwise the domain reads off without DEV_DISABLE: it took + * the request and gated before the answer was polled for. Never + * in place of an answer that reports an error. + */ + } if (c->answered && c->reply_result) ret = -EREMOTEIO; /* Native cmsgSLEEP errors/abort/timeout are fatal, never cancellation. */ From 7abb5e9d9ef2686e00783d79d604d216097c202e Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:29:00 +0000 Subject: [PATCH 13/33] soc: apple: aop: name the AFK endpoints of the T6000 sleep message For a system sleep the AOP driver sends each started AFK endpoint a sleep message and, after the wake, a wake message. The T6000 profile left both lists empty, which addresses every endpoint from 0x20 to 0x30. The profiles whose order was traced on macOS (T6041, T8142, T6050) address the twelve endpoints 0x20 to 0x2b and nothing above. Name the same twelve for T6000, in endpoint order both ways. The order has not been traced on this SoC; apple,sleep-endpoints and apple,wake-endpoints can still override it. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/soc/apple/aop.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/drivers/soc/apple/aop.rs b/drivers/soc/apple/aop.rs index 85123ea95d9af..d175d25053cbe 100644 --- a/drivers/soc/apple/aop.rs +++ b/drivers/soc/apple/aop.rs @@ -3154,6 +3154,9 @@ const HW_CFG_T8112: AopHwConfig = AopHwConfig { s2r_notice: AOP_S2R_ENDPOINTS | AOP_S2R_AP_IDLE, }; /// `nCal` keeps the firmware's own value, as on T8112. +/// +/// The sleep and wake lists are the twelve AFK endpoints the traced profiles name; the order +/// is untraced on this SoC. const HW_CFG_T6000: AopHwConfig = AopHwConfig { ec0p: 0x020000, ncal: None, @@ -3162,8 +3165,8 @@ const HW_CFG_T6000: AopHwConfig = AopHwConfig { patch_bootargs: true, epic_v4: false, setup_port: false, - sleep_endpoints: &[], - wake_endpoints: &[], + sleep_endpoints: &[0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b], + wake_endpoints: &[0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b], setup_epmap_endpoints: false, setup_wait_system_endpoints: true, quiesce_on_unbind: false, From 8fec9ac557211805cd4fd9070f2f78ba1b9ea3af Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:30:17 +0000 Subject: [PATCH 14/33] iommu/apple-dart: restore a locked T6000 DART after a firmware sleep The boot loader locks some DARTs before it hands over, and the driver neither saves nor restores a locked DART around system sleep. On T6000 (M1 Pro) suspend-to-RAM powers the DARTs down. A locked DART comes back with every register at its reset value, the lock bit included, while its page tables are still in DRAM. The first DMA of its consumer after the wake faults. Mark the T6000 DART type with sleep_clears_lock. Such a DART has its lock word and stream enables saved at suspend with the TCR and TTBR registers. After a wake through firmware, if the registers no longer hold the saved translations, they are written back, then the stream enables, then the lock word, and the TLBs are flushed. A locked DART whose registers survived, and the other DART types, are left alone as before. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/iommu/apple-dart.c | 48 +++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index 98aeeb6ac5d79..0190b81aea982 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -294,6 +294,8 @@ struct apple_dart_hw { u32 lock; u32 lock_bit; + /* A firmware sleep resets the DART and its lock: a locked DART is saved and restored. */ + bool sleep_clears_lock; u32 error; @@ -585,6 +587,10 @@ struct apple_dart { u32 save_tcr[DART_MAX_STREAMS]; u32 save_ttbr[DART_MAX_STREAMS][DART_MAX_TTBR]; + /* A locked DART's lock word and stream enables, see apple_dart_hw.sleep_clears_lock */ + u32 save_lock; + u32 save_enable[DART_MAX_STREAMS / 32]; + bool locked_saved; u64 *locked_ttbr[DART_MAX_STREAMS][DART_MAX_TTBR]; /* Values published by this driver into initially vacant root slots. */ @@ -7107,6 +7113,7 @@ static const struct apple_dart_hw apple_dart_hw_t6000 = { .enable_streams = DART_T8020_STREAMS_ENABLE, .lock = DART_T8020_CONFIG, .lock_bit = DART_T8020_CONFIG_LOCK, + .sleep_clears_lock = true, .error = DART_T8020_ERROR, @@ -7442,8 +7449,21 @@ static __maybe_unused int apple_dart_suspend(struct device *dev) return 0; /* Locked DARTs can't be restored so skip saving their registers. */ - if (dart->locked) - return 0; + if (dart->locked) { + unsigned int idx; + + /* + * Unless a sleep takes the lock with the registers. Saved at every suspend: + * a DART that is runtime suspended when the system sleeps isn't asked again. + */ + if (!dart->hw->sleep_clears_lock) + return 0; + dart->save_lock = apple_dart_readl(dart, dart->hw->lock); + for (idx = 0; idx < BITS_TO_U32(dart->num_streams); idx++) + dart->save_enable[idx] = + apple_dart_readl(dart, dart->hw->enable_streams + 4 * idx); + dart->locked_saved = true; + } apple_dart_save_state(dart); return 0; @@ -7750,8 +7770,30 @@ static __maybe_unused int apple_dart_resume(struct device *dev) return 0; /* Locked DARTs can't be restored, and they should not need it */ - if (dart->locked) + if (dart->locked) { + /* + * Except one the sleep reset, lock included: the translations and stream + * enables go back as they were, then the lock word, which closes them again. + */ + if (!dart->locked_saved || !pm_resume_via_firmware() || + apple_dart_state_retained(dart)) + return 0; + for (sid = 0; sid < dart->num_streams; sid++) { + for (idx = 0; idx < dart->hw->ttbr_count; idx++) + apple_dart_writel(dart, dart->save_ttbr[sid][idx], + DART_TTBR(dart, sid, idx)); + apple_dart_writel(dart, dart->save_tcr[sid], DART_TCR(dart, sid)); + } + for (idx = 0; idx < BITS_TO_U32(dart->num_streams); idx++) + apple_dart_writel(dart, dart->save_enable[idx], + dart->hw->enable_streams + 4 * idx); + apple_dart_writel(dart, dart->save_lock, dart->hw->lock); + ret = apple_dart_invalidate_each_stream(dart); + if (ret) + dev_warn(dev, "locked DART restored after the sleep, TLB flush failed: %d\n", + ret); return 0; + } /* * Not for a DART restored at every power-up, as macOS's SPTM does (its From 76d2d0e0da8ca86ee9dd950cf735a1f96d39dd95 Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Sat, 28 Nov 2026 02:49:19 +0000 Subject: [PATCH 15/33] mfd: macsmc: add the T6000 sleep notice: saved lane, pmVC and SMC GPIOs T6000 (M1 Pro) has a first-generation SMC like T8103: it is told of a suspend-to-RAM last, with interrupts off and one CPU left, which needs the saved command lane the driver uses for T8103 and J813. Enable the lane for "apple,t6000-smc" where the device tree has an enabled T6000 sleep controller, so the T6001 and T6002 machines that share the compatible keep the ordinary transport. Between slpw and slpa macOS writes, in this order: - gP1a = 0, the SMC GPIO that powers the SD card reader on the J314s; - pmVC, the VDD_CIO rail mask, which T8103's SMC does not have; - WKTP = 2, as on T8103. Take the GPIOs from apple,sleep-low-pins. Each is written 0 before the other keys and switched on again after waka, if this sleep lowered it. pmVC is written from the existing apple,sleep-pmvc, before WKTP. T8103 sets neither property and sends what it sent before. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/mfd/macsmc.c | 78 +++++++++++++++++++++++++++++++++++++- include/linux/mfd/macsmc.h | 5 +++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 7f4d7f9c9f369..8c62479a6a968 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -703,6 +704,16 @@ static void apple_smc_probe_wktp(struct apple_smc *smc) apple_smc_write_full_wake(smc); } +/* Whether the device tree has the sleep controller @compatible and leaves it enabled. */ +static bool apple_smc_sleep_controller_available(const char *compatible) +{ + struct device_node *np = of_find_compatible_node(NULL, NULL, compatible); + bool available = np && of_device_is_available(np); + + of_node_put(np); + return available; +} + /* pmVC as the boot left it, next to what apple,sleep-pmvc has the sleep commit write. */ static void apple_smc_sleep_pmvc_report(struct apple_smc *smc) { @@ -737,6 +748,10 @@ static int apple_smc_probe(struct platform_device *pdev) smc->sleep_wake_type = of_property_read_bool(dev->of_node, "apple,sleep-wake-type"); smc->sleep_pmvc_set = !of_property_read_u32(dev->of_node, "apple,sleep-pmvc", &smc->sleep_pmvc); + ret = of_property_read_variable_u32_array(dev->of_node, "apple,sleep-low-pins", + smc->sleep_low_pins, 1, + ARRAY_SIZE(smc->sleep_low_pins)); + smc->sleep_low_pin_count = ret > 0 ? ret : 0; apple_smc_init_state(smc); smc->dev = &pdev->dev; @@ -753,6 +768,10 @@ static int apple_smc_probe(struct platform_device *pdev) if (of_machine_is_compatible("apple,j813") || of_device_is_compatible(dev->of_node, "apple,t8103-smc")) smc->saved_enabled = true; + /* As is T6000's, on a board whose device tree enables its sleep controller. */ + if (of_device_is_compatible(dev->of_node, "apple,t6000-smc") && + apple_smc_sleep_controller_available("apple,t6000-sleep")) + smc->saved_enabled = true; smc->sram_base = devm_platform_get_and_ioremap_resource(pdev, 1, &smc->sram); if (IS_ERR(smc->sram_base)) return dev_err_probe(dev, PTR_ERR(smc->sram_base), "Failed to map SRAM region"); @@ -1270,6 +1289,34 @@ static void apple_smc_dark_wake(struct apple_smc *smc) pm_set_dark_wake(); } +/* + * apple,sleep-low-pins: key "gP" plus the GPIO number as two hex digits. Written 0 before + * the sleep notice, as macOS does, and switched on again with the SMC GPIO output command. + */ +#define SMC_GPIO_CMD_OUTPUT BIT(24) + +static smc_key apple_smc_gpio_key(u32 gpio) +{ + return __SMC_KEY('g', 'P', hex_asc_hi(gpio), hex_asc_lo(gpio)); +} + +/* After waka: the ones this sleep lowered, on again. */ +static void apple_smc_sleep_gpios_high(struct apple_smc *smc) +{ + unsigned long lowered = smc->sleep_low_pins_lowered; + unsigned int i; + int ret; + + smc->sleep_low_pins_lowered = 0; + for_each_set_bit(i, &lowered, ARRAY_SIZE(smc->sleep_low_pins)) { + ret = apple_smc_write_u32(smc, apple_smc_gpio_key(smc->sleep_low_pins[i]), + SMC_GPIO_CMD_OUTPUT | 1); + if (ret) + dev_err(smc->dev, "Failed to switch SMC GPIO %u back on: %d\n", + smc->sleep_low_pins[i], ret); + } +} + /* * The SMC's first wake commands after AP ON: MBSE and MBSW read, then MBSE = waka. With a sleep * policy, waka only after a clean read, and when it was answered for the pmVC that follows. @@ -1281,6 +1328,7 @@ static int apple_smc_wake_record(struct apple_smc *smc, bool *mbsw, bool later) smc->wake_lid_read = false; if (!smc->sleep_policy) { apple_smc_write_mbse(smc, SMC_KEY(waka)); + apple_smc_sleep_gpios_high(smc); return ret; } if (!ret) @@ -1722,6 +1770,22 @@ static int apple_smc_sleep_saved_pmvc(void *data) return 0; } +/* In the saved lane, after the peers stopped. The ones lowered are raised after the wake. */ +static int apple_smc_sleep_gpios_low(struct apple_smc *smc) +{ + unsigned int i; + int ret; + + for (i = 0; i < smc->sleep_low_pin_count; i++) { + ret = apple_smc_saved_write_u32(smc, apple_smc_gpio_key(smc->sleep_low_pins[i]), + 0); + if (ret) + return ret; + smc->sleep_low_pins_lowered |= BIT(i); + } + return 0; +} + static const struct apple_sleep_smc_saved_ops apple_smc_saved_ops = { .begin = apple_smc_sleep_saved_begin, .check = apple_smc_sleep_saved_check, @@ -1751,10 +1815,22 @@ static int apple_smc_sleep_after_peers(void *data, u64 *sent, u64 *ack) return -EINVAL; smc->saved_committed = true; - /* Gen 1 (T8103): WKTP between slpw and slpa; this SMC has no pmVC. */ + /* apple,sleep-low-pins: off before the keys below, as macOS has them. */ + ret = apple_smc_sleep_gpios_low(smc); + if (ret) + goto out; + /* + * Gen 1: WKTP between slpw and slpa. T8103's SMC has no pmVC; T6000's takes it + * before WKTP (apple,sleep-pmvc). + */ if (apple_sleep_smc_gen1_keys()) { u8 wktp = 2; + if (smc->sleep_pmvc_set) { + ret = apple_smc_saved_write_u32(smc, SMC_KEY(pmVC), smc->sleep_pmvc); + if (ret) + goto out; + } ret = apple_smc_saved_write(smc, SMC_KEY(WKTP), &wktp, sizeof(wktp)); if (ret) goto out; diff --git a/include/linux/mfd/macsmc.h b/include/linux/mfd/macsmc.h index 47d4bc7c9b35d..10c744c937058 100644 --- a/include/linux/mfd/macsmc.h +++ b/include/linux/mfd/macsmc.h @@ -154,6 +154,11 @@ struct apple_smc { /* apple,sleep-pmvc: when set, the pmVC value written at the sleep commit. */ bool sleep_pmvc_set; u32 sleep_pmvc; + /* apple,sleep-low-pins: SMC GPIOs low from the sleep notice to after waka. */ + u32 sleep_low_pins[4]; + unsigned int sleep_low_pin_count; + /* Bit n: sleep_low_pins[n] was lowered for this sleep. */ + unsigned long sleep_low_pins_lowered; /* To be told at the noirq suspend, after the debug link's cut. */ bool sleep_warn_late; /* In a noirq phase: replies are polled for. */ From 4ea3b1da0431bafe55098ae4f69dc55fb5646389 Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:31:51 +0000 Subject: [PATCH 16/33] mux: apple-display-crossbar: restore T6000 routes after a firmware sleep Suspend-to-RAM on T6000 (M1 Pro) powers the display crossbar down. It comes back with its tunable gone and every output unrouted, while the mux core and the driver still hold the selections made before the sleep. A display connected over USB-C before the sleep stays dark. Mark the T6000 crossbar as reset by a firmware sleep. On resume after such a sleep, write the probe-time tunable and run the ordinary selection for each output that has one. Suspend-to-idle, a refused sleep and the other crossbar variants are not affected. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/mux/apple-display-crossbar.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/drivers/mux/apple-display-crossbar.c b/drivers/mux/apple-display-crossbar.c index 7740922e7f316..cd58f7b47350b 100644 --- a/drivers/mux/apple-display-crossbar.c +++ b/drivers/mux/apple-display-crossbar.c @@ -23,6 +23,7 @@ #include #include #include +#include /* * T602x register interface is cleary different so most of the names below are @@ -110,12 +111,15 @@ struct apple_dpxbar_hw { bool t604x_dpin_atc; /* power domain held only while an output is selected */ bool runtime_pm; + /* a firmware sleep resets the block: the tunable and each selection are written again */ + bool restore_after_fw_sleep; /* T8142 layout: a connection status still busy after its poll is only logged */ bool status_log_only; }; struct apple_dpxbar { struct device *dev; + struct mux_chip *chip; const struct apple_dpxbar_hw *hw; void __iomem *regs; int selected_dispext[T8142_MUX_MAX]; @@ -1338,6 +1342,7 @@ static int apple_dpxbar_probe(struct platform_device *pdev) spin_lock_init(&dpxbar->lock); dpxbar->dev = dev; + dpxbar->chip = mux_chip; dpxbar->t6030_dpin = hw->t6030_flow || of_machine_is_compatible("apple,t6030"); dpxbar->regs = devm_platform_ioremap_resource(pdev, 0); if (IS_ERR(dpxbar->regs)) @@ -1387,7 +1392,23 @@ static int apple_dpxbar_resume(struct device *dev) unsigned long restored = 0, flags; unsigned int i; - if (!xbar || !xbar->t6030_dpin) + if (!xbar) + return 0; + + /* As at probe, then each output's route and clocks as its last selection set them. */ + if (xbar->hw->restore_after_fw_sleep && pm_resume_via_firmware()) { + writel(xbar->hw->tunable, xbar->regs + UNK_TUNABLE); + + for (i = 0; i < MUX_MAX; i++) { + int state = xbar->selected_dispext[i]; + + if (state < 0) + continue; + apple_dpxbar_set(&xbar->chip->mux[i], state); + } + } + + if (!xbar->t6030_dpin) return 0; spin_lock_irqsave(&xbar->lock, flags); @@ -1443,6 +1464,7 @@ static const struct apple_dpxbar_hw apple_dpxbar_hw_t6000 = { .tunable = 5, .ops = &apple_dpxbar_ops, .dpin_no_cycle_slip = true, + .restore_after_fw_sleep = true, }; static const struct apple_dpxbar_hw apple_dpxbar_hw_t6020 = { From f4ba279b5dd7c3d235561f8782f44eb19e133da3 Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:37:16 +0000 Subject: [PATCH 17/33] drm/apple: iomfb: add the A500 call to the 13.5 firmware's table macOS sends the display firmware the unnamed A500 call before it stops the coprocessor for a sleep. The H17P and 26.6 tables carry it as dcpep_unnamed_500 with iomfb_s2r_500(). The 13.5 firmware, which T6000 (M1 Pro) runs, dispatches the same call under the same tag. Add it to the 13.5 method table with iomfb_s2r_500_v13_3(). The argument is zero, the form the T6000 sleep was brought up with, and the firmware is given 2 s to answer. No caller yet; the T6000 stop that sends it follows. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/gpu/drm/apple/iomfb_template.c | 38 ++++++++++++++++++++++++++ drivers/gpu/drm/apple/iomfb_template.h | 2 +- drivers/gpu/drm/apple/iomfb_v13_3.c | 2 ++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/apple/iomfb_template.c b/drivers/gpu/drm/apple/iomfb_template.c index 050522f5a6fda..349e5d4b009a2 100644 --- a/drivers/gpu/drm/apple/iomfb_template.c +++ b/drivers/gpu/drm/apple/iomfb_template.c @@ -2843,6 +2843,44 @@ int DCP_FW_NAME(iomfb_s2r_500)(struct apple_dcp *dcp) } #endif +#if DCP_FW_VER == DCP_FW_VERSION(13, 3, 0) +DCP_THUNK_IN(dcp_unnamed_500, dcpep_unnamed_500, u32); + +static void dcp_s2r_500_done(struct apple_dcp *dcp, void *out, void *cookie) +{ + struct dcp_wait_cookie *wait = cookie; + + complete(&wait->done); + kref_put(&wait->refcount, release_wait_cookie); +} + +/* + * A500 on firmware 13.5, sent before the firmware is stopped for a suspend-to-RAM. The + * argument is zero, as T6000's sleep was brought up; H17P and 26.6 send 0xaaaa0000. + */ +int DCP_FW_NAME(iomfb_s2r_500)(struct apple_dcp *dcp) +{ + struct dcp_wait_cookie *cookie; + unsigned long left; + u32 val = 0; + + cookie = kzalloc_obj(*cookie); + if (!cookie) + return -ENOMEM; + init_completion(&cookie->done); + kref_init(&cookie->refcount); + /* increase refcount to ensure the receiver has a reference */ + kref_get(&cookie->refcount); + + dcp_unnamed_500(dcp, false, &val, dcp_s2r_500_done, cookie); + + left = wait_for_completion_timeout(&cookie->done, msecs_to_jiffies(2000)); + kref_put(&cookie->refcount, release_wait_cookie); + + return left ? 0 : -ETIMEDOUT; +} +#endif + static bool dcp_power_stop_confirmed(struct dcp_wait_cookie *cookie) { /* H17P does not confirm that scanout has stopped. */ diff --git a/drivers/gpu/drm/apple/iomfb_template.h b/drivers/gpu/drm/apple/iomfb_template.h index f4c47eba0406f..09c0edcc72b7b 100644 --- a/drivers/gpu/drm/apple/iomfb_template.h +++ b/drivers/gpu/drm/apple/iomfb_template.h @@ -394,7 +394,7 @@ void DCP_FW_NAME(iomfb_poweroff)(struct apple_dcp *dcp); void DCP_FW_NAME(iomfb_sleep)(struct apple_dcp *dcp); void DCP_FW_NAME(iomfb_start)(struct apple_dcp *dcp); void DCP_FW_NAME(iomfb_shutdown)(struct apple_dcp *dcp); -#if DCP_FW_DESKTOP_26(DCP_FW_VER) +#if DCP_FW_DESKTOP_26(DCP_FW_VER) || DCP_FW_VER == DCP_FW_VERSION(13, 3, 0) int DCP_FW_NAME(iomfb_s2r_500)(struct apple_dcp *dcp); #endif #if DCP_FW_H17(DCP_FW_VER) diff --git a/drivers/gpu/drm/apple/iomfb_v13_3.c b/drivers/gpu/drm/apple/iomfb_v13_3.c index 3521abccf038e..b9d77950bf349 100644 --- a/drivers/gpu/drm/apple/iomfb_v13_3.c +++ b/drivers/gpu/drm/apple/iomfb_v13_3.c @@ -39,6 +39,8 @@ static const struct dcp_method_entry dcp_methods[dcpep_num_methods] = { IOMFB_METHOD("A466", dcpep_flush_supports_power), IOMFB_METHOD("A467", iomfbep_abort_swaps_dcp), IOMFB_METHOD("A472", dcpep_set_power_state), + /* Unnamed here as on the later firmwares, see iomfb_s2r_500_v13_3(). */ + IOMFB_METHOD("A500", dcpep_unnamed_500), }; #define DCP_FW v13_3 From f7871d7ba71e1effc4e5f130780b7af0b9c8d61d Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:37:27 +0000 Subject: [PATCH 18/33] drm/apple: dcp: stop the T6000 panel firmware for a suspend-to-RAM The internal display coprocessor of T6000 (M1 Pro) runs firmware 13.5, for which the driver has no stop: across a system sleep the firmware is left running with its domains held on. Suspend-to-RAM takes the coprocessor's power regardless, and afterwards the driver waits on a session that no longer exists and the panel stays dark. Give "apple,t6000-dcp" its own match data with hw.sleep_13_5. For a sleep that goes through firmware, the internal processor on 13.5 is: - sent A500, as macOS sends it before the stop; - parked and stopped with apple_rtkit_sleep_stop(); - restarted on resume with apple_rtkit_sleep_return_with_init(), since this firmware boots from scratch after losing power. A dry run of the sleep leaves the firmware running. After a sleep refused with the firmware already parked, apple_rtkit_sleep_return() is used. The later firmwares have their own stop under hw.s2r, and no other SoC on 13.5 sets the flag. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/gpu/drm/apple/dcp-internal.h | 5 +++ drivers/gpu/drm/apple/dcp.c | 59 ++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index 81de9acc87461..760fce1bdf798 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -380,6 +380,11 @@ struct apple_dcp_hw_data { */ bool s2r; bool s2r_ext; + /* + * Firmware 13.5: a firmware-driven sleep takes the internal processor's power. It is + * stopped before the sleep and booted from scratch after it, see dcp_sleep_13_5_stop(). + */ + bool sleep_13_5; /* The firmware crashes on any mode but the 120 Hz boot timing, see dcp_mode_settable(). */ bool only_120hz_modes; /* A firmware 27 sleep in progress, and how it ended */ diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 6abd5cc9fda60..07d2461d467ed 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -11327,6 +11327,32 @@ bool dcp_sleep_keeps_firmware(struct device *dev) } EXPORT_SYMBOL_GPL(dcp_sleep_keeps_firmware); +/* + * hw.sleep_13_5: A500, as macOS sends it, then the firmware is parked and its CPU stopped. + * Not in a dry run. Returns whether the firmware was dealt with here. + */ +static bool dcp_sleep_13_5_stop(struct apple_dcp *dcp) +{ + int ret; + + if (!dcp->hw.sleep_13_5 || !pm_suspend_via_firmware() || apple_sleep_dry_run() || + dcp->external || dcp->fw_compat != DCP_FIRMWARE_V_13_5 || + IS_ERR_OR_NULL(dcp->rtk) || !dcp->coproc_reg || READ_ONCE(dcp->crashed) || + !apple_rtkit_is_running(dcp->rtk)) + return false; + + ret = iomfb_s2r_500_v13_3(dcp); + if (ret) + dev_warn(dcp->dev, "A500 before the sleep: %d\n", ret); + ret = apple_rtkit_sleep_stop(dcp->rtk, dcp->coproc_reg + APPLE_DCP_COPROC_CPU_CONTROL); + if (ret) + dev_warn(dcp->dev, "firmware did not stop for the sleep: %d\n", ret); + else + WRITE_ONCE(dcp->s2r_fw_down, true); + dcp->sleep_fw_runs = false; + return true; +} + static int dcp_platform_suspend(struct device *dev) { struct apple_dcp *dcp = dev_get_drvdata(dev); @@ -11524,6 +11550,8 @@ static int dcp_platform_suspend(struct device *dev) dcp->sleep_fw_runs = true; device_set_wakeup_path(dev); dcp_sleep_hold_phys(dcp); + if (dcp_sleep_13_5_stop(dcp)) + return 0; /* Last: the steps above still talk to the coprocessor. */ dcp_hold_for_sleep(dcp); @@ -11536,6 +11564,28 @@ void dcp_hdmi_disconnect(struct apple_dcp *dcp) dcp_dptx_disconnect(dcp, 0); } +/* + * hw.sleep_13_5: a real sleep took the firmware's memory, so it boots from scratch. A + * refused sleep left it parked with its power kept. + */ +static void dcp_sleep_13_5_start(struct apple_dcp *dcp) +{ + void __iomem *control = dcp->coproc_reg + APPLE_DCP_COPROC_CPU_CONTROL; + int ret; + + if (!dcp->hw.sleep_13_5 || dcp->external || dcp->fw_compat != DCP_FIRMWARE_V_13_5 || + !READ_ONCE(dcp->s2r_fw_down)) + return; + + WRITE_ONCE(dcp->s2r_fw_down, false); + if (pm_resume_via_firmware() && dcp_has_panel(dcp)) + ret = apple_rtkit_sleep_return_with_init(dcp->rtk, control); + else + ret = apple_rtkit_sleep_return(dcp->rtk, control); + if (ret) + dev_err(dcp->dev, "firmware did not start after the sleep: %d\n", ret); +} + static int dcp_platform_resume(struct device *dev) { struct apple_dcp *dcp = dev_get_drvdata(dev); @@ -11556,6 +11606,8 @@ static int dcp_platform_resume(struct device *dev) if (dcp->s2r_stopped) return dcp_s2r_resume(dcp); + dcp_sleep_13_5_start(dcp); + if (iomfb_is_m4(dcp)) { int ret = iomfb_m4_resume(dcp); @@ -11709,6 +11761,12 @@ static const struct apple_dcp_hw_data apple_dcp_hw_dcp = { .num_dptx_ports = 0, }; +/* T6000's DCP: as the generic one, with the firmware 13.5 stop around a suspend-to-RAM */ +static const struct apple_dcp_hw_data apple_dcp_hw_t6000 = { + .num_dptx_ports = 0, + .sleep_13_5 = true, +}; + static const struct apple_dcp_hw_data apple_dcp_hw_t6030 = { .iboot_handoff = "apple,t6030-handoff", .num_dptx_ports = 1, @@ -11840,6 +11898,7 @@ static const struct of_device_id of_match[] = { { .compatible = "apple,t6041-dcpext", .data = &apple_dcp_hw_t6041_dcpext, }, { .compatible = "apple,t6050-dcp", .data = &apple_dcp_hw_t6050 }, { .compatible = "apple,t6050-dcpext", .data = &apple_dcp_hw_t6050_dcpext }, + { .compatible = "apple,t6000-dcp", .data = &apple_dcp_hw_t6000, }, { .compatible = "apple,t6020-dcp", .data = &apple_dcp_hw_t6020, }, { .compatible = "apple,t6020-dcpext", .data = &apple_dcp_hw_t6020_dcpext, }, { .compatible = "apple,t8103-dcp", .data = &apple_dcp_hw_t8103, }, From b297dd4c5a0f9bb5d0c09901dc40f3f3b340216f Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:37:41 +0000 Subject: [PATCH 19/33] drm/apple: dcp: power the DP-to-HDMI converter down for suspend-to-RAM The HDMI port of the 14" and 16" MacBook Pro sits behind a DP-to-HDMI converter whose supply is an SMC GPIO (dp2hdmi-pwren-gpios). The driver switches it on at probe and leaves it on. The SMC keeps its GPIOs through a suspend-to-RAM, so the converter stays powered while the SoC is off and draws several hundred milliwatts, more than the rest of the machine in that state. macOS has it off. Switch the supply off in the suspend path when the sleep goes through firmware, and on again first thing on resume, before the port's hot-plug state is sampled. The converter's draw was first measured by Andreas Fernandez. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/gpu/drm/apple/dcp-internal.h | 2 ++ drivers/gpu/drm/apple/dcp.c | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index 760fce1bdf798..e20ac33694738 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -1236,6 +1236,8 @@ struct apple_dcp { struct gpio_desc *hdmi_hpd; struct gpio_desc *hdmi_pwren; struct gpio_desc *dp2hdmi_pwren; + /* The converter was powered down for a firmware sleep: resume powers it up again. */ + bool dp2hdmi_sleep_off; struct mutex hpd_mutex; diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 07d2461d467ed..670e4f3a2ea51 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -11542,6 +11542,11 @@ static int dcp_platform_suspend(struct device *dev) dcp_dptx_disconnect(dcp, 0); } } + /* The converter draws power through a suspend-to-RAM; macOS has it off. */ + if (dcp->dp2hdmi_pwren && pm_suspend_via_firmware()) { + gpiod_set_value_cansleep(dcp->dp2hdmi_pwren, 0); + dcp->dp2hdmi_sleep_off = true; + } /* * Set the device as a wakeup device, which forces its power * domains to stay on. We need this as we do not support full @@ -11606,6 +11611,12 @@ static int dcp_platform_resume(struct device *dev) if (dcp->s2r_stopped) return dcp_s2r_resume(dcp); + /* The converter first, so that it is up by the time HPD is sampled below. */ + if (dcp->dp2hdmi_sleep_off) { + dcp->dp2hdmi_sleep_off = false; + gpiod_set_value_cansleep(dcp->dp2hdmi_pwren, 1); + } + dcp_sleep_13_5_start(dcp); if (iomfb_is_m4(dcp)) { From 2f227471c6037ffc45b48452ade3d1d83d398bb1 Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:45:22 +0000 Subject: [PATCH 20/33] soc: apple: sleep: let a profile order SEP's sleep and the CPU stop Two points of the early-context entry are fixed in the core and differ on T6000 (M1 Pro), whose sequence follows macOS: - SEP is asked to sleep after the display blocks are down. On T6000 macOS asks SEP first, and the display blocks' lowering waits for SEP's power gate. With .enclave_first, SEP's stop runs right after the other cores are held, with ops->before_enclave ahead of it. - apple_sleep_commit() stops the parked cores before ops->commit_regs. T6000's commit stops them in the middle of its own sequence, after PLL0 is off and before the last cluster's target goes to 0. With .commit_stops_cpus the core leaves the stop to commit_regs. No profile sets either yet. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/soc/apple/sleep.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 4c25f5fd21cbf..faa158404a2db 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -845,6 +845,13 @@ 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; + /* + * SEP's sleep request first, right after the other cores are held: on T6000 the + * display's lowering waits for SEP's power gate. ops->before_enclave runs before it. + */ + bool enclave_first; + /* ops->commit_regs stops the parked cores itself, in place of the stop ahead of it. */ + bool commit_stops_cpus; }; struct apple_sleep { @@ -5809,7 +5816,8 @@ static int apple_sleep_commit(unsigned long unused) (s->soc->el1_wfi || READ_ONCE(sleep_el1_wfi) >= 2)); spin_table_release_stopped(); } - s->cpus_stop_ret = spin_table_stop_parked(); + if (!s->soc->commit_stops_cpus) + s->cpus_stop_ret = spin_table_stop_parked(); if (early) udelay(SLEEP_EARLY_WFI_SETTLE_US); @@ -6372,6 +6380,12 @@ static int apple_sleep_enter_prepare(struct apple_sleep *s) if (apple_sleep_dry(s)) return apple_sleep_enter_dry(s); ret = apple_sleep_cpus_hold(s); + if (!ret && s->soc->enclave_first) { + step = "enclave stop"; + if (s->soc->ops->before_enclave) + s->soc->ops->before_enclave(s); + ret = apple_sleep_enclave_stop(s); + } if (!ret) { step = "display prepare"; if (s->soc->regions) @@ -6414,7 +6428,7 @@ static int apple_sleep_enter_prepare(struct apple_sleep *s) ret = apple_sleep_enclave_stop(s); if (ret) panic("Apple SEP refused after the always-on GPU stop: %d", ret); - } else if (!ret) { + } else if (!ret && !s->soc->enclave_first) { ret = apple_sleep_enclave_stop(s); } if (ret) { From d5aeedaccef0ad385547abe586af2f1c7deb355b Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Sat, 28 Nov 2026 02:49:24 +0000 Subject: [PATCH 21/33] soc: apple: sleep: add T6000 (M1 Pro) suspend-to-RAM support Add the T6000 (M1 Pro) sleep profile to the Apple SoC sleep driver, following the PMGR S2R timer commit path used on Firestorm/Icestorm SoCs (based on the T6000 suspend-to-RAM reference by Andreas Fernandez). The profile: - arms wake sources from the device tree's apple,wake-enable property and reports T6000 wake source names from the ADT wake map; - waits for SEP to power-gate, saves PMGR domain state, quiesces the GPU and display fabric bridges (using the same 4-word bridge handshake as T8103) and drops PMP requests before lowering display and GPU domains; - quiesces the four AMCCs and their DCS channels (RMW low-power gating), the PCIe bridge, CPU complex counters/throttlers/APSC, DCS clock state, cluster targets, SoC clock muxes and PLL0, dcs_* domain words, NCO channels, and the final PMGR, clock, fabric p-state and SIO commit state before arming the PMU RTC SPMI vector, AIC mask and S2R timer; - restores clocks, clusters, AMCC/DCS state, PMGR domains, PMP DAPF and IORVBAR, NCO channels, SEP/SIO/GPIO domains, and all AP pins via the shared apple_sleep_restore_pins() path on wake, and reports any entry step that timed out or was left incomplete. Deep sleep is enabled by default on boards that enable the sleep node. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/soc/apple/sleep-t6000.h | 1948 +++++++++++++++++++++++++++++++ drivers/soc/apple/sleep.c | 2 + 2 files changed, 1950 insertions(+) create mode 100644 drivers/soc/apple/sleep-t6000.h diff --git a/drivers/soc/apple/sleep-t6000.h b/drivers/soc/apple/sleep-t6000.h new file mode 100644 index 0000000000000..d7ec8c23f68f7 --- /dev/null +++ b/drivers/soc/apple/sleep-t6000.h @@ -0,0 +1,1948 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* + * T6000 (M1 Pro): suspend-to-RAM through the PMGR's S2R timer, in the order macOS has on this + * SoC. Included by sleep.c after the other profiles, so every shared helper is in scope. + * + * Based on the t6000 suspend-to-RAM reference by Andreas Fernandez. + */ + +/* Per-core power-down controls, as on the other Firestorm/Icestorm SoCs */ +#define T6000_CYC_OVRD sys_reg(3, 5, 15, 5, 0) +#define T6000_CYC_DISABLE_WFI_RET BIT_ULL(0) +#define T6000_CYC_FIQ_MODE GENMASK_ULL(21, 20) +#define T6000_CYC_IRQ_MODE GENMASK_ULL(23, 22) +#define T6000_CYC_WFI_MODE GENMASK_ULL(25, 24) +#define T6000_ACC_OVRD sys_reg(3, 5, 15, 6, 0) +#define T6000_ACC_PWR_DN_SRM GENMASK_ULL(14, 13) +#define T6000_ACC_DIS_L2_FLUSH GENMASK_ULL(16, 15) +#define T6000_ACC_TRAIN_DOWN_LINK GENMASK_ULL(18, 17) +#define T6000_ACC_POWER_DOWN_CPM GENMASK_ULL(26, 25) +#define T6000_ACC_DIS_PIO_WFI BIT_ULL(32) +#define T6000_ACC_DEEP_SLEEP BIT_ULL(34) + +/* + * The watchdog is WD1. Each step of the entry arms it again, with the time in seconds the + * steps up to the next arm are given, and the wake arms it once more for the resume. + */ +#define T6000_WDT_WAKE_SECS 170 + +/* "wake": seven mask words (205 valid sources), then seven status words */ +#define T6000_WAKE_MASK 0x08 +#define T6000_WAKE_STATUS 0x24 +#define T6000_WAKE_WORDS 7 +#define T6000_WAKE_LAST_WORD GENMASK(12, 0) + +/* "sep-pg": bit 2 makes the S2R sequencer wait for SEP's power gate */ +#define T6000_SEP_PG_WAIT BIT(2) + +/* + * "cluster": one word per complex, at four times its index. The index is the first byte of + * the complex's entry in the ADT's soc-clusters. Target in bits 3:0, busy in bit 4. + */ +#define T6000_CLUSTERS 9 +#define T6000_CLUSTER_EACC 0 +#define T6000_CLUSTER_PACC0 1 +#define T6000_CLUSTER_PACC1 2 +#define T6000_CLUSTER_ANE0 3 +#define T6000_CLUSTER_AFR 5 +#define T6000_CLUSTER_DISPLAY 6 +#define T6000_CLUSTER_AVEMSR 8 +#define T6000_CLUSTERS_CPU (BIT(T6000_CLUSTER_EACC) | BIT(T6000_CLUSTER_PACC0) | \ + BIT(T6000_CLUSTER_PACC1)) +#define T6000_CLUSTERS_VALID (T6000_CLUSTERS_CPU | BIT(T6000_CLUSTER_ANE0) | \ + BIT(T6000_CLUSTER_AFR) | BIT(T6000_CLUSTER_DISPLAY) | \ + BIT(T6000_CLUSTER_AVEMSR)) +#define T6000_CLUSTER_PS_TARGET GENMASK(3, 0) +#define T6000_CLUSTER_PS_BUSY BIT(4) + +/* + * "acc0".."acc2": the CPU complexes' blocks (E, P0, P1). The two DVFS command values are the + * ones recorded from macOS; APSC_OFF and BUSY are bits of the same word. + */ +#define T6000_ACCS 3 +#define T6000_ACC_DVFS_CMD 0x00020 +#define T6000_ACC_DVFS_APSC_OFF BIT_ULL(22) +#define T6000_ACC_DVFS_BUSY BIT_ULL(7) +#define T6000_ACC_DVFS_RELOCK 0x100101ULL +#define T6000_ACC_DVFS_APSC 0x40000500101ULL +#define T6000_ACC_PERF_CTL 0x00660 +#define T6000_ACC_PERF_EN 0x15ULL +#define T6000_ACC_LLC_THRTL 0x20240 +#define T6000_ACC_AMX_THRTL 0x20250 +#define T6000_ACC_THRTL_EN BIT_ULL(63) +#define T6000_ACC_ENERGY_CTL 0x28080 +#define T6000_ACC_ENERGY_EN (BIT_ULL(63) | BIT_ULL(61)) +#define T6000_ACC_PPT_THRTL 0x28400 + +/* + * "clkgen": muxes parked onto the reference clock before PLL0 goes off, plus four muxes moved + * to source 7 and four muxes disabled at commit. Select in bits 27:24, enable in bit 31, busy + * in bit 30. + */ +#define T6000_CLKGEN_MUX_ON BIT(31) +#define T6000_CLKGEN_MUX_BUSY BIT(30) +#define T6000_CLKGEN_MUX_SEL GENMASK(27, 24) +#define T6000_CLKGEN_LATE_SEL 7 + +/* "pll0": off in three writes, each waited for: off mode 3, off allowed, then the enable */ +#define T6000_PLL_ON BIT(0) +#define T6000_PLL_OFF_OK BIT(1) +#define T6000_PLL_OFF_MODE GENMASK(5, 3) +#define T6000_PLL_BUSY BIT(31) + +/* "dcs": the memory clock's performance state, target in bits 3:0, busy in bit 31 */ +#define T6000_DCS_PS_TARGET GENMASK(3, 0) +#define T6000_DCS_PS_BUSY BIT(31) +/* The states the commit steps through on the way down, and the first on the way back */ +#define T6000_DCS_PS_STEP 7 +#define T6000_DCS_PS_SLEEP 5 + +/* + * "amcc0".."amcc3": the memory controllers, each with four DCS channel blocks 2 MiB apart from + * 2 MiB above its base. Bits 12 and 8 enable dynamic low-power gating across the commit. + */ +#define T6000_AMCCS 4 +#define T6000_AMCC_DRAIN_CTL 0x1c00 +#define T6000_AMCC_DRAIN_COUNT 0x1c04 +#define T6000_AMCC_LOCK 0x10000c +#define T6000_DCS_PER_AMCC 4 +#define T6000_DCS_BLOCK(k) (0x200000ULL * ((k) + 1)) +#define T6000_DCS_BLOCK_LEN 0x20 +#define T6000_DCS_CTL 0x10 +#define T6000_DCS_CTL_LP (BIT(12) | BIT(8)) + +/* + * A fabric bridge's handshake before its domain powers off, as on T8103: 1 at +0x14, then 1, + * 2 and 4 at +0x2bc. "apcie-q" is PCIe's; the others are 0x8000 into their fabric blocks. + */ +#define T6000_BRIDGE_8014 0x014 +#define T6000_BRIDGE_82BC 0x2bc +#define T6000_BRIDGE_LEN 0x300 + +/* + * "ptd", the PMP's telemetry: entry n is read at 0x10 * n and written at 0x10000 + 8 * n. + * 0xf8 is the device power requests, one bit per pmp_report entry; 0x100 acknowledges them. + */ +#define T6000_PTD_PS_REQ_RD 0xf80 +#define T6000_PTD_PS_ACK_RD 0x1000 +#define T6000_PTD_PS_REQ_WR 0x107c0 +#define T6000_PTD_DVFS_WR(n) ((n) == 3 ? 0x10860 : 0x10840 + 8 * (n)) +#define T6000_PTD_DVFS_RD(n) ((n) == 3 ? 0x10c0 : 0x1080 + 0x10 * (n)) +#define T6000_PTD_DVFS_IDLE 0x2000000000000000ULL +/* The requests of the GPU and of the display coprocessors */ +#define T6000_PTD_REQ_GFX BIT_ULL(0x9) +#define T6000_PTD_REQ_DISP0 BIT_ULL(0xc) +#define T6000_PTD_REQ_DISPEXT0 BIT_ULL(0xd) +#define T6000_PTD_REQ_DISPEXT1 BIT_ULL(0xe) +/* The PMP's sleep word: 4 from .begin, 1 at .prepare, back as it was at .finish */ +#define T6000_PTD_SLEEP 0x18328 +#define T6000_PTD_SLEEP_BEGIN 4 +#define T6000_PTD_SLEEP_COMMIT 1 + +/* + * "spmi" (nub, gen 1): vector enable words from +0x20, their acknowledgment 0x40 above. The + * PMU RTC's alarm is vector 2. + */ +#define T6000_SPMI_VEC_ENABLE 0x20 +#define T6000_SPMI_VEC_ACK 0x60 +#define T6000_SPMI_VEC_WORDS 7 +#define T6000_SPMI_VEC_RTC BIT(2) + +/* Power words in "ps" (pmgr), "ps-east" (pmgr_east), and "ps-aon" (pmgr_mini) */ +#define T6000_PS_GPIO 0x120 +#define T6000_PS_PMGR_SOC_OCLA 0x130 +#define T6000_PS_PCIE0_REF 0x138 +#define T6000_PS_PCIE1_REF 0x140 +#define T6000_PS_APCIE_ST 0x148 +#define T6000_PS_AFR 0x1e8 +#define T6000_PS_AFNC1_LW2 0x220 +#define T6000_PS_ATC0_COMMON 0x238 +#define T6000_PS_DISPEXT0_FE 0x258 +#define T6000_PS_DISPEXT1_FE 0x260 +#define T6000_PS_DISPEXT0_CPU0 0x280 +#define T6000_PS_DISPEXT1_CPU0 0x2a8 +#define T6000_PS_PMP 0x2d8 +#define T6000_PS_PMS_SRAM 0x2e0 +#define T6000_PS_APCIE_ST_SYS 0x2e8 +#define T6000_PS_APCIE_ST1_SYS 0x2f0 +#define T6000_PS_ATC2_COMMON 0x2f8 +#define T6000_PS_ATC3_COMMON 0x300 +#define T6000_PS_APCIE_GP_SYS 0x320 +#define T6000_PS_ATC0_PCIE 0x330 +#define T6000_PS_ATC1_PCIE 0x340 +#define T6000_PS_ATC2_PCIE 0x350 +#define T6000_PS_ATC3_PCIE 0x360 + +#define T6000_PS_EAST_AFNC2_LW1 0x160 +#define T6000_PS_EAST_AFNC3_LW0 0x178 +#define T6000_PS_EAST_SIO 0x180 +#define T6000_PS_EAST_SIO_CPU 0x188 +#define T6000_PS_EAST_FPWM0 0x190 +#define T6000_PS_EAST_SPI_P 0x1e8 +#define T6000_PS_EAST_SIO_SPMI0 0x218 +#define T6000_PS_EAST_SIO_SPMI1 0x220 +#define T6000_PS_EAST_SIO_SPMI2 0x228 +#define T6000_PS_EAST_UART_N 0x238 +#define T6000_PS_EAST_UART1 0x248 +#define T6000_PS_EAST_UART2 0x250 +#define T6000_PS_EAST_UART3 0x258 +#define T6000_PS_EAST_UART4 0x260 +#define T6000_PS_EAST_UART6 0x268 +#define T6000_PS_EAST_UART7 0x270 +#define T6000_PS_EAST_AUDIO_P 0x278 +#define T6000_PS_EAST_AES 0x2d0 +#define T6000_PS_EAST_DISP0_FE 0x328 +#define T6000_PS_EAST_DISP0_CPU0 0x350 +#define T6000_PS_EAST_ANS2 0x3b8 +#define T6000_PS_EAST_GFX 0x3c0 +#define T6000_PS_EAST_SEP 0xc00 + +#define T6000_PS_AON_MSG 0x078 +#define T6000_PS_AON_ATC0_USB_AON 0x088 +#define T6000_PS_AON_ATC1_USB_AON 0x090 +#define T6000_PS_AON_ATC2_USB_AON 0x098 +#define T6000_PS_AON_ATC3_USB_AON 0x0a0 +#define T6000_PS_AON_ATC0_USB 0x0d0 +#define T6000_PS_AON_ATC1_USB 0x0d8 +#define T6000_PS_AON_ATC2_USB 0x0e0 + +/* PS word fields the core doesn't name, as pmgr-pwrstate has them */ +#define T6000_PS_RESET BIT(31) +#define T6000_PS_AUTO GENMASK(27, 24) +#define T6000_PS_MIN GENMASK(19, 16) +#define T6000_PS_BUSY BIT(11) +#define T6000_PS_WAS_CLKGATED BIT(9) +/* Bits 12:8: the latches and status bits a restoring write leaves at 0 */ +#define T6000_PS_LATCHES (PS_UNLATCH | T6000_PS_BUSY | PS_DEV_DISABLE | PS_FLAGS) + +#define T6000_PS_WORD(auto_en, auto_st, flags, target) \ + (((auto_en) ? PS_AUTO_ENABLE : 0) | \ + FIELD_PREP_CONST(T6000_PS_AUTO, (auto_st)) | \ + (flags) | \ + FIELD_PREP_CONST(PS_TARGET, (target))) + +#define T6000_PS_WORD_MIN(auto_en, auto_st, min_st, flags, target) \ + (T6000_PS_WORD((auto_en), (auto_st), (flags), (target)) | \ + FIELD_PREP_CONST(T6000_PS_MIN, (min_st))) + +/* + * Blocks with no register entry in the sleep node. Each belongs to a device with its own + * node or to none, and only the words named here are touched. + */ +/* The fabric bridges of dispext0, disp0, dispext1, and the GPU (+0x8000 into each fabric) */ +static const u64 t6000_disp_bridge_pa[] = { + 0x28a008000ULL, 0x38c008000ULL, 0x28d008000ULL, +}; +#define T6000_GFX_BRIDGE_PA 0x405008000ULL +/* + * The enable word of the thermal/voltage monitor in front of the GFX domain, the T6000's + * counterpart of the T8132 GFX TVM gate. As macOS: 1 before AFR and GFX are powered for the + * bridge quiesce, 0 once both are lowered again. Zeroed with the other monitors too, in + * t6000_quiesce_zero_pa. + */ +#define T6000_AFR_GATE 0x28e5e4000ULL +/* SIO's CPU control, mailbox I2A control, and its two DARTs' stream enables */ +static const u64 t6000_sio_stop_pa[] = { + 0x39bc00044ULL, /* sio ASC_CPU_CONTROL */ + 0x39bc08114ULL, /* sio mailbox I2A_CONTROL */ + 0x39b0040fcULL, /* sio_dart_0 stream enable */ + 0x39b0080fcULL, /* sio_dart_1 stream enable */ +}; +/* + * The PMP's address filter (16 entries of six words, in use with bits 1:0 of the first set) + * and reset vector. The sleep loses both and the PMP's restart needs them: saved at probe. + */ +#define T6000_PMP_DAPF 0x28e304000ULL +#define T6000_PMP_DAPF_ENTRIES 16 +#define T6000_PMP_DAPF_STRIDE 0x40 +#define T6000_PMP_DAPF_WORDS 6 +#define T6000_PMP_DAPF_IN_USE GENMASK(1, 0) +#define T6000_PMP_IORVBAR 0x28e850000ULL +#define T6000_PMP_IORVBAR_VALID BIT_ULL(0) + +/* + * The NCO's channels, five words each as in clk-apple-nco. A channel is enabled across the + * commit, and its words are written back at the wake in that driver's order. + */ +#define T6000_NCO_CHANNELS 5 +#define T6000_NCO_STRIDE 0x4000 +#define T6000_NCO_WORDS 5 +#define T6000_NCO_CTRL 0 +#define T6000_NCO_CTRL_ENABLE BIT(31) + +enum { + T6000_ACCR_PERF, + T6000_ACCR_ENERGY, + T6000_ACCR_AMX, + T6000_ACCR_LLC, + T6000_ACCR_PPT, + T6000_ACCR_DVFS, + T6000_ACCR_N, +}; + +static const u32 t6000_acc_regs[T6000_ACCR_N] = { + [T6000_ACCR_PERF] = T6000_ACC_PERF_CTL, + [T6000_ACCR_ENERGY] = T6000_ACC_ENERGY_CTL, + [T6000_ACCR_AMX] = T6000_ACC_AMX_THRTL, + [T6000_ACCR_LLC] = T6000_ACC_LLC_THRTL, + [T6000_ACCR_PPT] = T6000_ACC_PPT_THRTL, + [T6000_ACCR_DVFS] = T6000_ACC_DVFS_CMD, +}; + +/* What the quiesce clears in each of the first five */ +static const u64 t6000_acc_enable[T6000_ACCR_DVFS] = { + [T6000_ACCR_PERF] = T6000_ACC_PERF_EN, + [T6000_ACCR_ENERGY] = T6000_ACC_ENERGY_EN, + [T6000_ACCR_AMX] = T6000_ACC_THRTL_EN, + [T6000_ACCR_LLC] = T6000_ACC_THRTL_EN, + [T6000_ACCR_PPT] = T6000_ACC_THRTL_EN, +}; + +/* In "clkgen": muxes parked onto refclk before PLL0 off, late source-7 muxes, and off muxes */ +static const u16 t6000_clkgen_mux[] = { 0x164, 0x16c, 0x168, 0x15c, 0x170 }; +static const u16 t6000_clkgen_late_mux[] = { 0x180, 0x184, 0x188, 0x18c }; +static const u16 t6000_clkgen_off_mux[] = { 0x98, 0x9c, 0xa0, 0xa4 }; + +/* + * Words zeroed at the head of the quiesce and restored on wake: + * - 4 fabric error-handler enables (south/west/east/gfx fabrics) + * - 9 PMGR performance/telemetry controls + * - 6 thermal/voltage monitor enables (two east monitors, the second of them the GFX + * monitor t6000_gfx_down() switches as T6000_AFR_GATE, + four SoC TVMs) + */ +static const u64 t6000_quiesce_zero_pa[] = { + /* Fabric error-handler enables */ + 0x282000a00ULL, 0x304000a00ULL, 0x383000a00ULL, 0x402000a00ULL, + /* PMGR performance/telemetry controls */ + 0x28e0ad000ULL, 0x28e0f8000ULL, 0x28e0f9000ULL, 0x28e0fa000ULL, + 0x28e0fb000ULL, 0x28e0fc000ULL, 0x28e0fd000ULL, 0x28e070000ULL, + 0x28e5d8000ULL, + /* Thermal/voltage monitors */ + 0x28e5e0000ULL, T6000_AFR_GATE, + 0x28e0c8200ULL, 0x28e0c8400ULL, 0x28e0c8600ULL, 0x28e0c8000ULL, +}; + +/* ADT /arm-io/pmgr ap-wake-sources on T6000, for wake reporting and SMC-only detection */ +static const struct apple_sleep_wake_name t6000_wake_names[] = { + { 12, "AOP.GPIO.IRQ4" }, + { 14, "AOP.GPIO.IRQ6" }, + { 26, "AOP.OutboxNotEmpty" }, + { 27, "AOP.CPUWakeupAP" }, + { 40, "NUB.WakeupAP" }, + { 43, "NUB.GPIO.IRQ2" }, + { 49, "NUB.APWatchdog" }, + { 50, "NUB.APWakeupTime" }, + { 51, "NUB.SPMISw0IRQ" }, + { 52, "NUB.SPMISw1IRQ" }, + { 54, "NUB.SPMISw3IRQ" }, + { 85, "SMC.CPUWakeupAP" }, + { 90, "SMC.OutboxNotEmpty" }, + { 126, "ATC0.CIOWakeup" }, + { 127, "ATC1.CIOWakeup" }, + { 128, "ATC2.CIOWakeup" }, + { 129, "ATC3.CIOWakeup" }, + { 130, "ATC0.USBWakeup" }, + { 131, "ATC1.USBWakeup" }, + { 132, "ATC2.USBWakeup" }, + { 133, "ATC3.USBWakeup" }, +}; + +/* The register blocks a power word is in */ +enum { + T6000_PS, /* "ps": pmgr */ + T6000_PS_EAST, /* "ps-east": pmgr_east */ + T6000_PS_AON, /* "ps-aon": pmgr_mini, the always-on (NUB) domains */ + T6000_BLOCKS, +}; + +struct t6000_ps_word { + u8 block; + u16 offset; +}; + +/* Each display coprocessor's CPU and front-end words: dispext0, disp0, dispext1 */ +static const struct t6000_ps_word t6000_disp_ps[6] = { + { T6000_PS, T6000_PS_DISPEXT0_CPU0 }, + { T6000_PS, T6000_PS_DISPEXT0_FE }, + { T6000_PS_EAST, T6000_PS_EAST_DISP0_CPU0 }, + { T6000_PS_EAST, T6000_PS_EAST_DISP0_FE }, + { T6000_PS, T6000_PS_DISPEXT1_CPU0 }, + { T6000_PS, T6000_PS_DISPEXT1_FE }, +}; + +/* The order macOS lowers them in: disp0, dispext0, dispext1, each CPU before its front end */ +static const u8 t6000_disp_order[6] = { 2, 3, 0, 1, 4, 5 }; + +/* The PMP's request of each, by the index of its pair in t6000_disp_ps[] */ +static const u64 t6000_disp_request[3] = { + T6000_PTD_REQ_DISPEXT0, T6000_PTD_REQ_DISP0, T6000_PTD_REQ_DISPEXT1, +}; + +/* The domains that must read off before a complex's target is taken off (offset 0 ends) */ +static const struct t6000_ps_word t6000_gate_ane0[] = { + { T6000_PS, 0x268 }, /* ane_sys */ + { T6000_PS, 0x2c8 }, /* ane_sys_cpu */ + { } +}; + +static const struct t6000_ps_word t6000_gate_afr[] = { + { T6000_PS, T6000_PS_AFR }, + { T6000_PS_EAST, T6000_PS_EAST_GFX }, + { T6000_PS, 0x268 }, /* ane_sys */ + { T6000_PS, 0x270 }, /* avd_sys */ + { T6000_PS_EAST, 0x390 }, /* jpg */ + { T6000_PS_EAST, 0x398 }, /* msr0 */ + { T6000_PS_EAST, 0x3b0 }, /* venc_sys */ + { } +}; + +static const struct t6000_ps_word t6000_gate_display[] = { + { T6000_PS_EAST, T6000_PS_EAST_DISP0_CPU0 }, + { T6000_PS, T6000_PS_DISPEXT0_CPU0 }, + { T6000_PS, T6000_PS_DISPEXT1_CPU0 }, + { } +}; + +static const struct t6000_ps_word t6000_gate_avemsr[] = { + { T6000_PS, 0x270 }, /* avd_sys */ + { T6000_PS_EAST, 0x398 }, /* msr0 */ + { T6000_PS_EAST, 0x3b0 }, /* venc_sys */ + { } +}; + +/* + * Always-on ("ps-aon") domains lowered at commit: they keep their written state across the + * sleep, so each is saved at .prepare and restored on wake. + */ +static const struct { + u16 offset; + u32 value; +} t6000_aon_commit_off[] = { + { T6000_PS_AON_ATC0_USB, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS_AON_ATC2_USB, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS_AON_ATC1_USB, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS_AON_ATC0_USB_AON, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS_AON_ATC2_USB_AON, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS_AON_ATC1_USB_AON, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS_AON_ATC3_USB_AON, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS_AON_MSG, T6000_PS_WORD(false, 0, T6000_PS_WAS_CLKGATED, 0) }, +}; + +/* Non-AON PMGR domains programmed into their sequencer commit state before the S2R timer */ +static const struct { + u8 block; + u16 offset; + u32 value; +} t6000_ps_commit_state[] = { + /* Lower stopped USB-C PCIe/common and UART domains */ + { T6000_PS, T6000_PS_ATC0_PCIE, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS, T6000_PS_ATC1_PCIE, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS, T6000_PS_ATC2_PCIE, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS, T6000_PS_ATC3_PCIE, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS, T6000_PS_ATC0_COMMON, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS, T6000_PS_ATC2_COMMON, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS, T6000_PS_ATC3_COMMON, T6000_PS_WORD(false, 0, PS_FLAGS, 0) }, + { T6000_PS_EAST, T6000_PS_EAST_UART2, + T6000_PS_WORD(false, 0, T6000_PS_WAS_CLKGATED, 0) }, + { T6000_PS_EAST, T6000_PS_EAST_UART3, + T6000_PS_WORD(false, 0, T6000_PS_WAS_CLKGATED, 0) }, + { T6000_PS_EAST, T6000_PS_EAST_UART4, + T6000_PS_WORD(false, 0, T6000_PS_WAS_CLKGATED, 0) }, + { T6000_PS_EAST, T6000_PS_EAST_UART6, + T6000_PS_WORD(false, 0, T6000_PS_WAS_CLKGATED, 0) }, + { T6000_PS_EAST, T6000_PS_EAST_UART7, + T6000_PS_WORD(false, 0, T6000_PS_WAS_CLKGATED, 0) }, + /* Hand fabric link-wrapper and PCIe/storage/SIO domains to the S2R sequencer */ + { T6000_PS, T6000_PS_AFNC1_LW2, T6000_PS_WORD(true, 0, PS_FLAGS, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_AFNC2_LW1, T6000_PS_WORD(true, 0, PS_FLAGS, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_AFNC3_LW0, T6000_PS_WORD(true, 0, PS_FLAGS, 0xf) }, + { T6000_PS, T6000_PS_APCIE_GP_SYS, T6000_PS_WORD(true, 0, PS_FLAGS, 0xf) }, + { T6000_PS, T6000_PS_PCIE0_REF, + T6000_PS_WORD(true, 4, T6000_PS_WAS_CLKGATED, 0xf) }, + { T6000_PS, T6000_PS_PCIE1_REF, + T6000_PS_WORD(true, 4, T6000_PS_WAS_CLKGATED, 0xf) }, + { T6000_PS, T6000_PS_APCIE_ST_SYS, T6000_PS_WORD(true, 0xf, PS_FLAGS, 0xf) }, + { T6000_PS, T6000_PS_APCIE_ST1_SYS, T6000_PS_WORD(true, 0xf, PS_FLAGS, 0xf) }, + { T6000_PS, T6000_PS_APCIE_ST, T6000_PS_WORD(false, 0, 0, 0xf) }, + { T6000_PS, T6000_PS_GPIO, T6000_PS_WORD(false, 0, 0, 0xf) }, + { T6000_PS, T6000_PS_PMGR_SOC_OCLA, T6000_PS_WORD(false, 0, 0, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_SIO_CPU, + T6000_PS_WORD(false, 0xf, T6000_PS_WAS_CLKGATED, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_FPWM0, + T6000_PS_WORD(false, 0, T6000_PS_WAS_CLKGATED, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_SPI_P, T6000_PS_WORD(false, 0, 0, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_SIO_SPMI0, T6000_PS_WORD(true, 0xf, 0, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_SIO_SPMI1, T6000_PS_WORD(true, 0xf, 0, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_SIO_SPMI2, T6000_PS_WORD(true, 0xf, 0, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_UART_N, T6000_PS_WORD(false, 0, 0, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_UART1, T6000_PS_WORD(false, 0, 0, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_AUDIO_P, + T6000_PS_WORD(false, 0, T6000_PS_WAS_CLKGATED, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_AES, T6000_PS_WORD(false, 0, PS_FLAGS, 0xf) }, + { T6000_PS_EAST, T6000_PS_EAST_ANS2, T6000_PS_WORD(true, 0xf, PS_FLAGS, 0xf) }, +}; + +/* How long the late commit writes get to take effect before the timer is written */ +#define T6000_LATE_SETTLE_MS 200 + +/* + * Every PMGR power domain of the device tree: the sleep cuts their power while genpd counts + * them on, so each is raised again at the wake, parents first. Not SEP's, afc or afi. + */ +#define T6000_DOMAINS_MAX 256 +#define T6000_DOMAIN_BLOCKS 8 +#define T6000_DOMAIN_PASSES 30 + +/* + * Steps of the entry that were left out or did not finish, kept for the report after the + * wake: the commit runs with one CPU and no console. + */ +#define T6000_INCOMPLETE_SEP BIT(0) +#define T6000_INCOMPLETE_PMP BIT(1) +#define T6000_INCOMPLETE_PMS_SRAM BIT(2) +#define T6000_INCOMPLETE_DCS_KEPT BIT(3) +#define T6000_INCOMPLETE_DCS_BUSY BIT(4) +#define T6000_INCOMPLETE_PLL_KEPT BIT(5) + +static const char *const t6000_incomplete_names[] = { + "SEP not power-gated", + "PMP not off", + "PMP SRAM not off", + "memory clock state not lowered", + "memory clock state not reached", + "clock mux busy and PLL0 left on", +}; + +static struct t6000_domain { + struct device_node *np; + void __iomem *reg; + const char *name; + u32 saved; + /* A display word: its driver lowers it in its suspend, so it is read at .begin */ + bool save_at_begin; + /* In "ps-aon": it keeps its state, and isn't raised here */ + bool always_on; + /* A dcs_* word: what it read at the commit of the first sleep since boot */ + u32 commit_ref; +} t6000_domains[T6000_DOMAINS_MAX]; + +static struct { + resource_size_t start; + void __iomem *base; +} t6000_domain_blocks[T6000_DOMAIN_BLOCKS]; + +static struct t6000_sleep { + void __iomem *ps_east, *sep_pg, *clkgen, *pll0, *dcs, *apcie_q, *ptd; + void __iomem *acc[T6000_ACCS]; + void __iomem *amcc[T6000_AMCCS]; + void __iomem *dcs_block[T6000_AMCCS][T6000_DCS_PER_AMCC]; + void __iomem *disp_bridge[ARRAY_SIZE(t6000_disp_bridge_pa)]; + void __iomem *gfx_bridge, *afr_gate, *pmp_dapf, *pmp_iorvbar, *nco, *fabric_ps; + void __iomem *sio_stop[ARRAY_SIZE(t6000_sio_stop_pa)]; + void __iomem *zero[ARRAY_SIZE(t6000_quiesce_zero_pa)]; + /* Where "ps", "ps-east" and "ps-aon" start, by their enum */ + resource_size_t block_start[T6000_BLOCKS]; + + unsigned int ndomains, ndomain_blocks; + bool domains_saved; + unsigned int domain_pass, domains_stuck; + + u32 aon_saved[ARRAY_SIZE(t6000_aon_commit_off)]; + bool aon_saved_valid; + + u32 sep_pg_saved; + bool sep_pg_cleared; + + u32 spmi_saved[T6000_SPMI_VEC_WORDS]; + bool spmi_armed; + + u64 ptd_req_saved; + u64 dvfs_saved[4]; + bool ptd_written; + u32 ptd_sleep_saved; + bool ptd_sleep_set; + + u32 afr_cluster_before; + bool afr_cluster_valid; + + u32 dcs_ctl_saved[T6000_AMCCS][T6000_DCS_PER_AMCC]; + u32 zero_saved[ARRAY_SIZE(t6000_quiesce_zero_pa)]; + u32 apcie_saved[2]; + u64 acc_saved[T6000_ACCS][T6000_ACCR_N]; + u64 relock_saved[2]; + u32 dcs_saved; + /* The dcs_* words: the first sleep's values taken, and what the commit did to them */ + bool dcs_ref_taken; + unsigned int dcs_floors, dcs_changed; + const struct t6000_domain *dcs_changed_first; + u32 dcs_changed_was; + u32 afr_saved; + u32 cluster_saved[T6000_CLUSTERS]; + u32 cluster_written; + /* T6000_INCOMPLETE_*, and the complexes whose target stayed: a domain of theirs was on */ + u32 incomplete, clusters_kept; + u32 mux_saved[ARRAY_SIZE(t6000_clkgen_mux)]; + u32 mux_n; + u32 pll_saved; + bool pll_done, dcs_done, afr_done, mcc_done, zeros_done, apcie_done, acc_done, relock_done; + + u32 wdt_saved_ctl, wdt_saved_bite; + bool wdt_saved; + bool aic_masked; + + u32 nco_saved[T6000_NCO_CHANNELS][T6000_NCO_WORDS]; + bool nco_saved_valid; + + u32 pmp_dapf_saved[T6000_PMP_DAPF_ENTRIES][T6000_PMP_DAPF_WORDS]; + unsigned int pmp_dapf_n; + u64 pmp_iorvbar_saved; + bool pmp_iorvbar_valid; +} t6000_sleep; + +static void __iomem *t6000_map(struct apple_sleep *s, const char *name, struct resource *res) +{ + struct device_node *np = s->dev->of_node; + int index = of_property_match_string(np, "reg-names", name); + + if (index < 0 || of_address_to_resource(np, index, res)) + return IOMEM_ERR_PTR(-ENODEV); + /* Shared with the drivers of these blocks, so not claimed. */ + return apple_sleep_map(s->dev, name); +} + +/* Mapped for good, non-posted (a posted write to these blocks is an SError), or NULL */ +static void __iomem *t6000_ioremap(struct apple_sleep *s, phys_addr_t pa, size_t len) +{ + void __iomem *base = ioremap_np(pa, len); + + if (base && devm_add_action_or_reset(s->dev, apple_sleep_iounmap, (void __force *)base)) + return NULL; + return base; +} + +static void __iomem *t6000_block(struct apple_sleep *s, unsigned int block) +{ + switch (block) { + case T6000_PS: return s->ps; + case T6000_PS_EAST: return t6000_sleep.ps_east; + case T6000_PS_AON: return s->ps_aon; + default: return NULL; + } +} + +static void __iomem *t6000_ps_reg(struct apple_sleep *s, struct t6000_ps_word word) +{ + return t6000_block(s, word.block) + word.offset; +} + +/* A PMGR node's block, mapped once however many of its domains are tracked */ +static void __iomem *t6000_domain_block(struct apple_sleep *s, struct device_node *parent, + resource_size_t *start) +{ + struct t6000_sleep *t = &t6000_sleep; + struct resource res; + unsigned int b; + + if (of_address_to_resource(parent, 0, &res)) + return NULL; + *start = res.start; + for (b = 0; b < t->ndomain_blocks; b++) + if (t6000_domain_blocks[b].start == res.start) + return t6000_domain_blocks[b].base; + if (t->ndomain_blocks == T6000_DOMAIN_BLOCKS) + return NULL; + t6000_domain_blocks[b].start = res.start; + t6000_domain_blocks[b].base = t6000_ioremap(s, res.start, resource_size(&res)); + if (t6000_domain_blocks[b].base) + t->ndomain_blocks++; + return t6000_domain_blocks[b].base; +} + +static void t6000_domains_init(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + struct device_node *np, *parent; + resource_size_t start; + void __iomem *base; + const char *label; + unsigned int i; + u32 off; + + for_each_compatible_node(np, NULL, "apple,pmgr-pwrstate") { + struct t6000_domain *d = &t6000_domains[t->ndomains]; + + if (t->ndomains == T6000_DOMAINS_MAX) { + of_node_put(np); + break; + } + label = NULL; + of_property_read_string(np, "label", &label); + if (!label || !strcmp(label, "sep") || !strcmp(label, "afc") || + !strcmp(label, "afi")) + continue; + if (of_property_read_u32(np, "reg", &off)) + continue; + parent = of_get_parent(np); + base = parent ? t6000_domain_block(s, parent, &start) : NULL; + of_node_put(parent); + if (!base) + continue; + d->np = of_node_get(np); + d->reg = base + off; + d->name = label; + d->always_on = start == t->block_start[T6000_PS_AON]; + for (i = 0; i < ARRAY_SIZE(t6000_disp_ps); i++) + if (t->block_start[t6000_disp_ps[i].block] + t6000_disp_ps[i].offset == + start + off) + d->save_at_begin = true; + t->ndomains++; + } +} + +static void t6000_domains_save(bool at_begin) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int i; + + t->domains_saved = false; + for (i = 0; i < t->ndomains; i++) + if (t6000_domains[i].save_at_begin == at_begin) + t6000_domains[i].saved = readl(t6000_domains[i].reg); + t->domains_saved = !at_begin; +} + +static int t6000_domain_find(struct device_node *np) +{ + unsigned int i; + + for (i = 0; i < t6000_sleep.ndomains; i++) + if (t6000_domains[i].np == np) + return i; + return -1; +} + +/* + * One domain back to its saved state, parents first: minimum state, target with AUTO_ENABLE + * off, then AUTO_ENABLE. A domain that doesn't follow is taken to 0 first on later passes. + */ +static void t6000_domain_on(unsigned int i, unsigned int depth, unsigned long *seen) +{ + struct t6000_sleep *t = &t6000_sleep; + struct t6000_domain *d = &t6000_domains[i]; + struct of_phandle_iterator it; + u32 saved = d->saved, cur; + unsigned int n; + int err, parent; + + if (test_and_set_bit(i, seen) || depth > 8) + return; + if (d->always_on) + return; + if (!FIELD_GET(PS_ACTUAL, saved)) + return; + of_for_each_phandle(&it, err, d->np, "power-domains", "#power-domain-cells", 0) { + parent = t6000_domain_find(it.node); + if (parent >= 0) + t6000_domain_on(parent, depth + 1, seen); + } + cur = readl(d->reg); + if ((cur ^ saved) & T6000_PS_MIN) { + writel((cur & ~(T6000_PS_LATCHES | T6000_PS_MIN)) | (saved & T6000_PS_MIN), + d->reg); + cur = readl(d->reg); + } + if (FIELD_GET(PS_TARGET, cur) == FIELD_GET(PS_TARGET, saved) && + FIELD_GET(PS_ACTUAL, cur) >= FIELD_GET(PS_ACTUAL, saved)) + return; + cur &= ~(T6000_PS_LATCHES | PS_AUTO_ENABLE | PS_TARGET | T6000_PS_AUTO); + cur |= saved & T6000_PS_AUTO; + if (t->domain_pass && !FIELD_GET(PS_ACTUAL, readl(d->reg))) { + writel(cur & ~PS_ACTUAL, d->reg); + udelay(100); + } + writel(cur | (saved & PS_TARGET), d->reg); + for (n = 0; n < 2000 && FIELD_GET(PS_ACTUAL, readl(d->reg)) < + FIELD_GET(PS_ACTUAL, saved); n++) + udelay(10); + cur = readl(d->reg) & ~(T6000_PS_LATCHES | PS_ACTUAL); + if ((saved & PS_AUTO_ENABLE) && !t->domain_pass) + writel(cur | PS_AUTO_ENABLE, d->reg); + cur = readl(d->reg); + if ((cur & T6000_PS_RESET) && !(saved & T6000_PS_RESET) && FIELD_GET(PS_ACTUAL, cur)) { + writel(cur & ~(T6000_PS_RESET | T6000_PS_BUSY | PS_FLAGS | PS_ACTUAL), d->reg); + udelay(1); + cur = readl(d->reg); + } + if (FIELD_GET(PS_ACTUAL, cur) < FIELD_GET(PS_ACTUAL, saved)) + t->domains_stuck++; +} + +/* Until every domain is back, or T6000_DOMAIN_PASSES passes 10 ms apart */ +static void t6000_domains_restore(void) +{ + struct t6000_sleep *t = &t6000_sleep; + DECLARE_BITMAP(seen, T6000_DOMAINS_MAX); + unsigned int i, pass; + + if (!t->domains_saved) + return; + t->domains_saved = false; + for (pass = 0; pass < T6000_DOMAIN_PASSES; pass++) { + if (pass) + mdelay(10); + t->domain_pass = pass; + t->domains_stuck = 0; + bitmap_zero(seen, T6000_DOMAINS_MAX); + for (i = 0; i < t->ndomains; i++) + t6000_domain_on(i, 0, seen); + if (!t->domains_stuck) + break; + } +} + +static int t6000_sleep_map_regs(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + static const char *const acc_names[T6000_ACCS] = { "acc0", "acc1", "acc2" }; + static const char *const amcc_names[T6000_AMCCS] = { "amcc0", "amcc1", "amcc2", "amcc3" }; + struct resource res, ps_east, ps_aon, amcc; + struct device_node *np; + unsigned int i, j, a, k; + int ret; + + s->ps = t6000_map(s, "ps", &s->ps_res); + t->ps_east = t6000_map(s, "ps-east", &ps_east); + s->ps_aon = t6000_map(s, "ps-aon", &ps_aon); + s->cpu_start = t6000_map(s, "cpu-start", &res); + s->cluster = t6000_map(s, "cluster", &res); + s->commit = t6000_map(s, "commit", &res); + s->wdt = t6000_map(s, "wdt", &res); + s->wake = t6000_map(s, "wake", &res); + s->spmi = t6000_map(s, "spmi", &res); + t->sep_pg = t6000_map(s, "sep-pg", &res); + t->clkgen = t6000_map(s, "clkgen", &res); + t->pll0 = t6000_map(s, "pll0", &res); + t->dcs = t6000_map(s, "dcs", &res); + t->apcie_q = t6000_map(s, "apcie-q", &res); + t->ptd = t6000_map(s, "ptd", &res); + if (IS_ERR(s->ps) || IS_ERR(t->ps_east) || IS_ERR(s->ps_aon) || IS_ERR(s->cpu_start) || + IS_ERR(s->cluster) || IS_ERR(s->commit) || IS_ERR(s->wdt) || IS_ERR(s->wake) || + IS_ERR(s->spmi) || IS_ERR(t->sep_pg) || IS_ERR(t->clkgen) || IS_ERR(t->pll0) || + IS_ERR(t->dcs) || IS_ERR(t->apcie_q) || IS_ERR(t->ptd)) + return dev_err_probe(s->dev, -EINVAL, + "ps, ps-east, ps-aon, cpu-start, cluster, commit, wdt, wake, spmi, sep-pg, clkgen, pll0, dcs, apcie-q and ptd must all be mapped\n"); + t->block_start[T6000_PS] = s->ps_res.start; + t->block_start[T6000_PS_EAST] = ps_east.start; + t->block_start[T6000_PS_AON] = ps_aon.start; + + ret = apple_sleep_read_wake_sources(s); + if (ret) + return ret; + + for (i = 0; i < T6000_ACCS; i++) { + t->acc[i] = t6000_map(s, acc_names[i], &res); + if (IS_ERR(t->acc[i])) + return dev_err_probe(s->dev, PTR_ERR(t->acc[i]), "no %s registers\n", + acc_names[i]); + } + for (a = 0; a < T6000_AMCCS; a++) { + t->amcc[a] = t6000_map(s, amcc_names[a], &amcc); + if (IS_ERR(t->amcc[a])) + return dev_err_probe(s->dev, PTR_ERR(t->amcc[a]), "no %s registers\n", + amcc_names[a]); + /* Its DCS channel blocks, above the controller's own: left alone if unmapped */ + for (k = 0; k < T6000_DCS_PER_AMCC; k++) + t->dcs_block[a][k] = t6000_ioremap(s, amcc.start + T6000_DCS_BLOCK(k), + T6000_DCS_BLOCK_LEN); + } + + /* + * The blocks with no register entry here. Each is used only if it mapped, as the + * steps that write them say. + */ + for (i = 0; i < ARRAY_SIZE(t6000_disp_bridge_pa); i++) + t->disp_bridge[i] = t6000_ioremap(s, t6000_disp_bridge_pa[i], T6000_BRIDGE_LEN); + t->gfx_bridge = t6000_ioremap(s, T6000_GFX_BRIDGE_PA, T6000_BRIDGE_LEN); + t->afr_gate = t6000_ioremap(s, T6000_AFR_GATE, 4); + for (i = 0; i < ARRAY_SIZE(t6000_sio_stop_pa); i++) + t->sio_stop[i] = t6000_ioremap(s, t6000_sio_stop_pa[i], 4); + for (i = 0; i < ARRAY_SIZE(t6000_quiesce_zero_pa); i++) + t->zero[i] = t6000_ioremap(s, t6000_quiesce_zero_pa[i], 4); + + s->driver_down = devm_bitmap_zalloc(s->dev, resource_size(&s->ps_res) / 4, GFP_KERNEL); + if (!s->driver_down) + return -ENOMEM; + + t->pmp_dapf = t6000_ioremap(s, T6000_PMP_DAPF, + T6000_PMP_DAPF_ENTRIES * T6000_PMP_DAPF_STRIDE); + if (t->pmp_dapf) { + for (i = 0; i < T6000_PMP_DAPF_ENTRIES; i++) { + void __iomem *entry = t->pmp_dapf + i * T6000_PMP_DAPF_STRIDE; + + if (!(readl(entry) & T6000_PMP_DAPF_IN_USE)) + break; + for (j = 0; j < T6000_PMP_DAPF_WORDS; j++) + t->pmp_dapf_saved[i][j] = readl(entry + 4 * j); + } + t->pmp_dapf_n = i; + } + t->pmp_iorvbar = t6000_ioremap(s, T6000_PMP_IORVBAR, 8); + if (t->pmp_iorvbar) { + t->pmp_iorvbar_saved = readq(t->pmp_iorvbar); + t->pmp_iorvbar_valid = true; + } + + np = of_find_compatible_node(NULL, NULL, "apple,t6000-nco"); + if (np && !of_address_to_resource(np, 0, &res) && + resource_size(&res) >= T6000_NCO_CHANNELS * T6000_NCO_STRIDE) + t->nco = t6000_ioremap(s, res.start, T6000_NCO_CHANNELS * T6000_NCO_STRIDE); + of_node_put(np); + + np = of_find_compatible_node(NULL, NULL, "apple,t6000-pmgr-misc"); + if (np && !of_address_to_resource(np, 0, &res)) + t->fabric_ps = t6000_ioremap(s, res.start, 4); + of_node_put(np); + + t6000_domains_init(s); + + return apple_sleep_alloc_books(s); +} + +/* @secs from now. What WD1 had before the first arm of a sleep is kept for .finish. */ +static void t6000_wdt_arm(struct apple_sleep *s, unsigned int secs) +{ + struct t6000_sleep *t = &t6000_sleep; + + if (!t->wdt_saved) { + t->wdt_saved_ctl = readl(s->wdt + WDT_WD1_CTL); + t->wdt_saved_bite = readl(s->wdt + WDT_WD1_BITE); + t->wdt_saved = true; + } + writel(0, s->wdt + WDT_WD1_CTL); + writel(0, s->wdt + WDT_WD1_COUNT); + writel(secs * WDT_TICKS_PER_S, s->wdt + WDT_WD1_BITE); + writel(WDT_WD1_RESET_EN, s->wdt + WDT_WD1_CTL); + dsb(sy); +} + +static void t6000_wdt_restore(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + + if (!t->wdt_saved) + return; + writel(0, s->wdt + WDT_WD1_CTL); + writel(0, s->wdt + WDT_WD1_COUNT); + writel(t->wdt_saved_bite, s->wdt + WDT_WD1_BITE); + writel(t->wdt_saved_ctl, s->wdt + WDT_WD1_CTL); + t->wdt_saved = false; +} + +/* + * The L1 data cache written back by set and way, the EL02 timers masked, and the overrides + * for a WFI without retention set, as XNU has them on T6000. + */ +static void t6000_deep_wfi_arm(void) +{ + u64 acc, cyc; + + if (is_kernel_in_hyp_mode()) { + write_sysreg_s(0, SYS_CNTP_CTL_EL02); + write_sysreg_s(0, SYS_CNTV_CTL_EL02); + } + + acc = read_sysreg_s(T6000_ACC_OVRD); + acc &= ~(T6000_ACC_PWR_DN_SRM | T6000_ACC_DIS_L2_FLUSH | + T6000_ACC_TRAIN_DOWN_LINK | T6000_ACC_POWER_DOWN_CPM); + acc |= FIELD_PREP(T6000_ACC_PWR_DN_SRM, 3) | + FIELD_PREP(T6000_ACC_DIS_L2_FLUSH, 2) | + FIELD_PREP(T6000_ACC_TRAIN_DOWN_LINK, 3) | + FIELD_PREP(T6000_ACC_POWER_DOWN_CPM, 3) | + T6000_ACC_DIS_PIO_WFI | T6000_ACC_DEEP_SLEEP; + + cyc = read_sysreg_s(T6000_CYC_OVRD); + cyc &= ~(T6000_CYC_WFI_MODE | T6000_CYC_IRQ_MODE | T6000_CYC_FIQ_MODE); + cyc |= FIELD_PREP(T6000_CYC_WFI_MODE, 3) | T6000_CYC_DISABLE_WFI_RET; + + /* + * No return once SCTLR_EL1.C is clear: the write-back only reaches L2, so a stack pop + * with the data cache off could read stale DRAM before the WFI. + */ + asm volatile( + "msr\tdaifset, #0xf\n\t" + "mrs\tx3, sctlr_el1\n\t" + "bic\tx3, x3, %[cbit]\n\t" + "msr\tsctlr_el1, x3\n\t" + "isb\n\t" + "msr\tcsselr_el1, xzr\n\t" + "isb\n\t" + "mrs\tx3, ccsidr_el1\n\t" + "and\tx4, x3, #7\n\t" + "add\tx4, x4, #4\n\t" + "ubfx\tx5, x3, #3, #10\n\t" + "ubfx\tx6, x3, #13, #15\n\t" + "clz\tw7, w5\n\t" + "1:\n\t" + "mov\tx8, x6\n\t" + "2:\n\t" + "lsl\tx9, x5, x7\n\t" + "lsl\tx10, x8, x4\n\t" + "orr\tx9, x9, x10\n\t" + "dc\tcisw, x9\n\t" + "subs\tx8, x8, #1\n\t" + "b.ge\t2b\n\t" + "subs\tx5, x5, #1\n\t" + "b.ge\t1b\n\t" + "dsb\tsy\n\t" + "isb\n\t" + "msr\tcntp_ctl_el0, xzr\n\t" + "msr\tcntv_ctl_el0, xzr\n\t" + "msr\ts3_1_c15_c0_0, xzr\n\t" + "msr\ts3_5_c15_c6_0, %[acc]\n\t" + "msr\ts3_5_c15_c5_0, %[cyc]\n\t" + "isb\n\t" + "mov\tx3, #1\n\t" + "3:\n\t" + "dsb\tsy\n\t" + "isb\n\t" + "wfi\n\t" + "msr\ts3_5_c15_c1_1, x3\n\t" + "b\t3b\n\t" + : + : [cbit] "i" (SCTLR_ELx_C), [acc] "r" (acc), [cyc] "r" (cyc) + : "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "memory" + ); + unreachable(); +} + +/* Masked once, before the timer: the shared commit's call after it then changes nothing. */ +static void t6000_aic_mask_all(void) +{ + if (t6000_sleep.aic_masked) + return; + aic_sleep_mask_all(); + t6000_sleep.aic_masked = true; +} + +static void t6000_aic_restore(void) +{ + if (!t6000_sleep.aic_masked) + return; + aic_sleep_restore(); + t6000_sleep.aic_masked = false; +} + +/* The bridge's handshake, see T6000_BRIDGE_8014 */ +static void t6000_bridge_quiesce(void __iomem *bridge) +{ + writel(1, bridge + T6000_BRIDGE_8014); + writel(1, bridge + T6000_BRIDGE_82BC); + writel(2, bridge + T6000_BRIDGE_82BC); + writel(4, bridge + T6000_BRIDGE_82BC); +} + +/* A device's request to the PMP set or cleared, and its acknowledgment waited for */ +static int t6000_ptd_request(u64 request, bool on) +{ + void __iomem *ptd = t6000_sleep.ptd; + u64 val = readq(ptd + T6000_PTD_PS_REQ_RD); + + writeq(on ? val | request : val & ~request, ptd + T6000_PTD_PS_REQ_WR); + return readq_poll_timeout_atomic(ptd + T6000_PTD_PS_ACK_RD, val, + on ? val & request : !(val & request), 10, 200000); +} + +/* + * The GPU's way down: AFR and GFX up, GFX released from the PMP, the bridge quiesced, then + * GFX and AFR lowered again. + */ +static void t6000_gfx_down(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + void __iomem *gfx = t->ps_east + T6000_PS_EAST_GFX; + void __iomem *afr = s->ps + T6000_PS_AFR; + void __iomem *afr_cluster = s->cluster + 4 * T6000_CLUSTER_AFR; + bool gfx_on = true; + u32 val; + + if (!t->gfx_bridge) + return; + + t->afr_cluster_before = readl(afr_cluster); + t->afr_cluster_valid = true; + writel(CLUSTER_ON, afr_cluster); + readl_poll_timeout_atomic(afr_cluster, val, val & CLUSTER_UP_DONE, 1, 50000); + writel(CLUSTER_ON | CLUSTER_UP_DONE, afr_cluster); + + if (t->afr_gate) + writel(1, t->afr_gate); + writel(T6000_PS_WORD_MIN(false, 0xf, 4, 0, 0), afr); + writel(T6000_PS_WORD_MIN(false, 0xf, 4, 0, 0xf), afr); + writel(T6000_PS_WORD_MIN(true, 0xf, 4, 0, 0xf), afr); + readl_poll_timeout_atomic(afr, val, FIELD_GET(PS_ACTUAL, val), 1, 50000); + + writel(T6000_PS_WORD(false, 0xf, 0, 0), gfx); + writel(T6000_PS_WORD(false, 0xf, 0, 0xf), gfx); + writel(T6000_PS_WORD(true, 0xf, 0, 0xf), gfx); + + t6000_ptd_request(T6000_PTD_REQ_GFX, true); + + if (readl_poll_timeout_atomic(gfx, val, FIELD_GET(PS_ACTUAL, val), 1, 50000)) + gfx_on = false; + + t6000_ptd_request(T6000_PTD_REQ_GFX, false); + + if (gfx_on && FIELD_GET(PS_ACTUAL, readl(gfx))) + t6000_bridge_quiesce(t->gfx_bridge); + + writel(T6000_PS_WORD(false, 0xf, 0, 0xf), gfx); + writel(T6000_PS_WORD(false, 0xf, PS_FLAGS, 0), gfx); + readl_poll_timeout_atomic(gfx, val, !FIELD_GET(PS_ACTUAL, val), 1, 10000); + + writel(T6000_PS_WORD_MIN(false, 4, 4, 0, 0xf), afr); + writel(T6000_PS_WORD_MIN(false, 0xf, 4, PS_FLAGS, 0xf), afr); + writel(T6000_PS_WORD_MIN(true, 0xf, 4, 0, 0xf), afr); + writel(T6000_PS_WORD_MIN(true, 4, 0xf, T6000_PS_WAS_CLKGATED, 0xf), afr); + writel(T6000_PS_WORD_MIN(false, 0xf, 0xf, T6000_PS_WAS_CLKGATED, 0xf), afr); + writel(T6000_PS_WORD_MIN(false, 4, 4, PS_FLAGS, 0), afr); + readl_poll_timeout_atomic(afr, val, !FIELD_GET(PS_ACTUAL, val), 1, 10000); + + if (t->afr_gate) + writel(0, t->afr_gate); + writel(0, afr_cluster); +} + +static int t6000_sleep_begin(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + + t->ptd_sleep_set = false; + if (s->dry_run) + return 0; + /* The display drivers power their domains off in their own suspend. */ + t6000_domains_save(true); + t->ptd_sleep_saved = readl(t->ptd + T6000_PTD_SLEEP); + writel(T6000_PTD_SLEEP_BEGIN, t->ptd + T6000_PTD_SLEEP); + t->ptd_sleep_set = true; + return 0; +} + +/* SEP answers its sleep request only with the GPIO domain on. */ +static void t6000_before_enclave(struct apple_sleep *s) +{ + sleep_ps_raise(s, T6000_PS_GPIO); + sleep_note(s, 0x29); +} + +static int t6000_sleep_display_off(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int k, i; + u32 val, stuck = 0; + + /* Before anything in .enter touches the hardware */ + t6000_wdt_arm(s, 20); + + /* As macOS: SEP gets up to 5 s to power-gate after its sleep request's answer. */ + if (apple_sleep_enclave.stop) { + void __iomem *sep_ps = t->ps_east + T6000_PS_EAST_SEP; + + sleep_note(s, 0x2a); + if (readl_poll_timeout_atomic(sep_ps, val, !FIELD_GET(PS_ACTUAL, val), 100, + 5000000)) + t->incomplete |= T6000_INCOMPLETE_SEP; + sleep_note(s, 0x2b); + } + + /* Every PMGR domain as the drivers left it, before the steps below lower any */ + t6000_domains_save(false); + + /* The internal display's coprocessor, if its driver left it powered: its CPU off */ + if (s->display[0].asc && + FIELD_GET(PS_ACTUAL, readl(t6000_ps_reg(s, t6000_disp_ps[2])))) { + writel(readl(s->display[0].asc + ASC_CPU_CONTROL) & ~ASC_CPU_CONTROL_RUN, + s->display[0].asc + ASC_CPU_CONTROL); + dsb(sy); + } + + t6000_wdt_arm(s, 35); + t6000_gfx_down(s); + t6000_wdt_arm(s, 50); + + for (k = 0; k < ARRAY_SIZE(t6000_disp_ps); k++) { + void __iomem *reg, *fe, *bridge; + unsigned int disp; + bool cpu; + + i = t6000_disp_order[k]; + disp = i / 2; + cpu = !(i & 1); + reg = t6000_ps_reg(s, t6000_disp_ps[i]); + fe = t6000_ps_reg(s, t6000_disp_ps[i | 1]); + bridge = t->disp_bridge[disp]; + + /* dispext0 is brought up first: its front end if it is off, then its CPU */ + if (i == 0) { + if (!FIELD_GET(PS_ACTUAL, readl(fe))) + sleep_ps_raise_start(s, T6000_PS_DISPEXT0_FE); + if (FIELD_GET(PS_ACTUAL, readl(reg)) != 0xf) + sleep_ps_raise_start(s, T6000_PS_DISPEXT0_CPU0); + } + /* A front end that is off has no bridge to quiesce. */ + if (bridge && !FIELD_GET(PS_ACTUAL, readl(fe))) + bridge = NULL; + + if (cpu) { + t6000_ptd_request(t6000_disp_request[disp], false); + writel(T6000_PS_WORD_MIN(false, 0xf, 4, 0, 0xf), reg); + writel(T6000_PS_WORD_MIN(false, 0xf, 4, PS_FLAGS, 0), reg); + } else { + if (bridge) + t6000_bridge_quiesce(bridge); + writel(T6000_PS_WORD(false, 0, PS_FLAGS, 0), reg); + } + if (readl_poll_timeout_atomic(reg, val, !FIELD_GET(PS_ACTUAL, val), 1, 10000)) + stuck |= BIT(i); + } + + if (stuck) { + dev_err(s->dev, "T6000: display power words still on (mask %#x): refusing the sleep\n", + stuck); + t6000_domains_restore(); + t6000_wdt_restore(s); + return -EBUSY; + } + t6000_wdt_arm(s, 80); + return 0; +} + +static int t6000_sleep_final_check(struct apple_sleep *s) +{ + t6000_wdt_arm(s, 90); + return 0; +} + +static void t6000_sleep_prepare_regs(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int i, n; + u64 val; + + /* A hang in the PMP's or the SMC's last steps or in the quiesce resets the SoC. */ + t6000_wdt_arm(s, 100); + sleep_note(s, 0x70); + + for (i = 0; i < ARRAY_SIZE(t6000_aon_commit_off); i++) + t->aon_saved[i] = readl(s->ps_aon + t6000_aon_commit_off[i].offset); + t->aon_saved_valid = true; + + /* + * Only a SEP that took its sleep request in this entry gives the sequencer the power + * gate it waits for. An idle, running SEP reads an actual state of 0 as well. + */ + t->sep_pg_saved = readl(t->sep_pg); + t->sep_pg_cleared = !apple_sleep_enclave.stop && (t->sep_pg_saved & T6000_SEP_PG_WAIT); + if (t->sep_pg_cleared) + writel(t->sep_pg_saved & ~T6000_SEP_PG_WAIT, t->sep_pg); + + sleep_note(s, 0x71); + /* The PMP's sleep tail, right before the coprocessors' last stop idles it */ + t->ptd_req_saved = readq(t->ptd + T6000_PTD_PS_REQ_RD); + writeq(0, t->ptd + T6000_PTD_PS_REQ_WR); + readq_poll_timeout_atomic(t->ptd + T6000_PTD_PS_ACK_RD, val, !(u32)val, 100, 200000); + for (i = 0; i < 4; i++) + t->dvfs_saved[i] = readq(t->ptd + T6000_PTD_DVFS_RD(i)); + /* As recorded: all four twice, the sleep word, then the first three once more */ + for (n = 0; n < 2; n++) + for (i = 0; i < 4; i++) + writeq(T6000_PTD_DVFS_IDLE, t->ptd + T6000_PTD_DVFS_WR(i)); + writel(T6000_PTD_SLEEP_COMMIT, t->ptd + T6000_PTD_SLEEP); + for (i = 0; i < 3; i++) + writeq(T6000_PTD_DVFS_IDLE, t->ptd + T6000_PTD_DVFS_WR(i)); + t->ptd_written = true; + sleep_note(s, 0x72); +} + +static void t6000_sleep_undo_regs(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + + if (t->sep_pg_cleared) { + writel(t->sep_pg_saved, t->sep_pg); + t->sep_pg_cleared = false; + } +} + +/* A complex's target goes off only with every domain it powers reading off. */ +static bool t6000_cluster_gate_ok(struct apple_sleep *s, unsigned int cluster) +{ + const struct t6000_ps_word *word; + + switch (cluster) { + case T6000_CLUSTER_ANE0: + word = t6000_gate_ane0; + break; + case T6000_CLUSTER_AFR: + word = t6000_gate_afr; + break; + case T6000_CLUSTER_DISPLAY: + word = t6000_gate_display; + break; + case T6000_CLUSTER_AVEMSR: + word = t6000_gate_avemsr; + break; + default: + return true; + } + + for (; word->offset; word++) + if (FIELD_GET(PS_ACTUAL, readl(t6000_ps_reg(s, *word)))) + return false; + return true; +} + +static bool t6000_dcs_set(u32 state) +{ + void __iomem *dcs = t6000_sleep.dcs; + u32 val = readl(dcs); + + writel((val & ~T6000_DCS_PS_TARGET) | FIELD_PREP(T6000_DCS_PS_TARGET, state), dcs); + return !readl_poll_timeout_atomic(dcs, val, !(val & T6000_DCS_PS_BUSY), 1, 8000); +} + +/* + * The fields of a dcs_* word that are compared with its first sleep's: the state it has + * reached only while it isn't automatic in both. + */ +static u32 t6000_dcs_word_mask(u32 a, u32 b) +{ + u32 mask = PS_TARGET | PS_ACTUAL | PS_DEV_DISABLE | T6000_PS_MIN | PS_AUTO_ENABLE; + + if (a & b & PS_AUTO_ENABLE) + mask &= ~PS_ACTUAL; + return mask; +} + +/* + * The dcs_* words before the commit, as the reference has them: the wake waits for each to + * reach 0. The first sleep clears a minimum state and keeps what each word reads; later + * sleeps put a differing word back to that. + */ +static void t6000_dcs_words_commit(void) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int i; + u32 val, ref; + + for (i = 0; i < t->ndomains; i++) { + struct t6000_domain *d = &t6000_domains[i]; + + if (strncmp(d->name, "dcs_", 4)) + continue; + val = readl(d->reg); + if (!t->dcs_ref_taken) { + if (val & T6000_PS_MIN) { + writel(val & ~(T6000_PS_MIN | T6000_PS_RESET | PS_FLAGS), d->reg); + udelay(100); + t->dcs_floors++; + val = readl(d->reg); + } + d->commit_ref = val; + continue; + } + ref = d->commit_ref; + if (!((val ^ ref) & t6000_dcs_word_mask(val, ref)) || (ref & T6000_PS_MIN)) + continue; + writel((val & ~(PS_TARGET | PS_AUTO_ENABLE | T6000_PS_MIN | T6000_PS_RESET | + PS_FLAGS)) | + (ref & (PS_TARGET | PS_AUTO_ENABLE | T6000_PS_MIN)), d->reg); + udelay(100); + if (!t->dcs_changed++) { + t->dcs_changed_first = d; + t->dcs_changed_was = val; + } + } + t->dcs_ref_taken = true; +} + +/* A channel enabled for the commit; every word as it was, for the wake */ +static void t6000_nco_enable(void) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int ch, w; + + if (!t->nco) + return; + for (ch = 0; ch < T6000_NCO_CHANNELS; ch++) { + void __iomem *chan = t->nco + ch * T6000_NCO_STRIDE; + + for (w = 0; w < T6000_NCO_WORDS; w++) + t->nco_saved[ch][w] = readl(chan + 4 * w); + if (!(t->nco_saved[ch][T6000_NCO_CTRL] & T6000_NCO_CTRL_ENABLE)) + writel(t->nco_saved[ch][T6000_NCO_CTRL] | T6000_NCO_CTRL_ENABLE, + chan + T6000_NCO_CTRL); + } + t->nco_saved_valid = true; +} + +/* The control word with the enable off, the other four words, then the control word */ +static void t6000_nco_restore(void) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int ch, w; + + if (!t->nco_saved_valid) + return; + t->nco_saved_valid = false; + for (ch = 0; ch < T6000_NCO_CHANNELS; ch++) { + void __iomem *chan = t->nco + ch * T6000_NCO_STRIDE; + + writel(t->nco_saved[ch][T6000_NCO_CTRL] & ~T6000_NCO_CTRL_ENABLE, + chan + T6000_NCO_CTRL); + for (w = 1; w < T6000_NCO_WORDS; w++) + writel(t->nco_saved[ch][w], chan + 4 * w); + writel(t->nco_saved[ch][T6000_NCO_CTRL], chan + T6000_NCO_CTRL); + } +} + +/* + * Final PMGR, clock, fabric p-state and SIO commit programming immediately before the S2R + * timer. Only domains and muxes whose state differs from the running system are written. + */ +static void t6000_commit_late_state(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int i; + u32 val; + + writel(T6000_PS_WORD(true, 0xf, 0, 0xf), t->ps_east + T6000_PS_EAST_AFNC2_LW1); + writel(T6000_PS_WORD(true, 0xf, 0, 0xf), t->ps_east + T6000_PS_EAST_SIO); + udelay(2000); + + for (i = 0; i < ARRAY_SIZE(t6000_aon_commit_off); i++) + writel(t6000_aon_commit_off[i].value, + s->ps_aon + t6000_aon_commit_off[i].offset); + + for (i = 0; i < ARRAY_SIZE(t6000_ps_commit_state); i++) + writel(t6000_ps_commit_state[i].value, + t6000_block(s, t6000_ps_commit_state[i].block) + + t6000_ps_commit_state[i].offset); + + for (i = 0; i < ARRAY_SIZE(t6000_clkgen_late_mux); i++) { + void __iomem *reg = t->clkgen + t6000_clkgen_late_mux[i]; + + val = readl(reg); + writel((val & ~T6000_CLKGEN_MUX_SEL) | + FIELD_PREP(T6000_CLKGEN_MUX_SEL, T6000_CLKGEN_LATE_SEL), reg); + readl_poll_timeout_atomic(reg, val, !(val & T6000_CLKGEN_MUX_BUSY), 1, 8000); + } + + if (t->fabric_ps) { + writel(SOC_SLEEP_STATE, t->fabric_ps); + sleep_perf_wait(t->fabric_ps, SOC_SLEEP_STATE); + } + udelay(2000); + + writel(T6000_PS_WORD(false, 0xf, 0, 0xf), t->ps_east + T6000_PS_EAST_SIO_CPU); + if (!readl_poll_timeout_atomic(t->ps_east + T6000_PS_EAST_SIO_CPU, val, + FIELD_GET(PS_ACTUAL, val) == 0xf, 1, 2000)) { + for (i = 0; i < ARRAY_SIZE(t6000_sio_stop_pa); i++) + if (t->sio_stop[i]) + writel(0, t->sio_stop[i]); + } + + writel(0, s->ps + T6000_PS_PCIE0_REF); + writel(0, s->ps + T6000_PS_APCIE_GP_SYS); + + for (i = 0; i < ARRAY_SIZE(t6000_clkgen_off_mux); i++) + writel(0, t->clkgen + t6000_clkgen_off_mux[i]); + udelay(100); +} + +static void t6000_sleep_commit_regs(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int a, i, r, n; + u32 val; + u64 qval; + + sleep_note(s, 0x73); + + /* The halted PMP and its SRAM down, so the memory clock and the sequencer can follow */ + sleep_ps_lower(s, T6000_PS_PMP); + if (sleep_ps_wait_off(s, T6000_PS_PMP)) + t->incomplete |= T6000_INCOMPLETE_PMP; + sleep_ps_lower(s, T6000_PS_PMS_SRAM); + if (sleep_ps_wait_off(s, T6000_PS_PMS_SRAM)) + t->incomplete |= T6000_INCOMPLETE_PMS_SRAM; + + /* 1. The memory controllers: enable DCS low-power gating, then drain */ + t->mcc_done = true; + for (a = 0; a < T6000_AMCCS; a++) + for (i = 0; i < T6000_DCS_PER_AMCC; i++) + if (t->dcs_block[a][i]) { + t->dcs_ctl_saved[a][i] = + readl(t->dcs_block[a][i] + T6000_DCS_CTL); + writel(t->dcs_ctl_saved[a][i] | T6000_DCS_CTL_LP, + t->dcs_block[a][i] + T6000_DCS_CTL); + } + for (a = 0; a < T6000_AMCCS; a++) + writel(1, t->amcc[a] + T6000_AMCC_LOCK); + for (a = 0; a < T6000_AMCCS; a++) + writel(0, t->amcc[a] + T6000_AMCC_DRAIN_CTL); + for (a = 0; a < T6000_AMCCS; a++) + writel(0, t->amcc[a] + T6000_AMCC_LOCK); + for (a = 0; a < T6000_AMCCS; a++) + readl_poll_timeout_atomic(t->amcc[a] + T6000_AMCC_DRAIN_COUNT, val, !val, + 1, 20000); + + /* 2. The error handlers, counters and voltage monitors */ + t->zeros_done = true; + for (i = 0; i < ARRAY_SIZE(t6000_quiesce_zero_pa); i++) { + if (!t->zero[i]) + continue; + t->zero_saved[i] = readl(t->zero[i]); + writel(0, t->zero[i]); + } + + sleep_note(s, 0x74); + /* 3. The PCIe bridge */ + t->apcie_done = true; + t->apcie_saved[0] = readl(t->apcie_q + T6000_BRIDGE_8014); + t->apcie_saved[1] = readl(t->apcie_q + T6000_BRIDGE_82BC); + t6000_bridge_quiesce(t->apcie_q); + + sleep_note(s, 0x75); + /* 4. The CPU complexes: counters and throttlers off, the PLL relock, APSC */ + t->acc_done = true; + for (i = 0; i < T6000_ACCS; i++) { + for (r = 0; r < T6000_ACCR_DVFS; r++) { + void __iomem *reg = t->acc[i] + t6000_acc_regs[r]; + + t->acc_saved[i][r] = readq(reg); + writeq(t->acc_saved[i][r] & ~t6000_acc_enable[r], reg); + } + } + t->relock_done = true; + for (i = 0; i < 2; i++) { + void __iomem *reg = t->acc[i] + T6000_ACC_DVFS_CMD; + + t->relock_saved[i] = readq(reg); + writeq(T6000_ACC_DVFS_RELOCK, reg); + readq_poll_timeout_atomic(reg, qval, !(qval & T6000_ACC_DVFS_BUSY), 1, 8000); + } + for (i = 0; i < T6000_ACCS; i++) { + void __iomem *reg = t->acc[i] + T6000_ACC_DVFS_CMD; + + t->acc_saved[i][T6000_ACCR_DVFS] = readq(reg); + for (n = 0; n < 2; n++) { + writeq(T6000_ACC_DVFS_APSC, reg); + readq_poll_timeout_atomic(reg, qval, !(qval & T6000_ACC_DVFS_BUSY), + 1, 8000); + } + } + + sleep_note(s, 0x76); + /* 5. The memory clock's state down, only with the PMP off, and AFR */ + t->dcs_done = false; + if (!FIELD_GET(PS_ACTUAL, readl(s->ps + T6000_PS_PMP))) { + t->dcs_saved = readl(t->dcs); + t->dcs_done = true; + if (!t6000_dcs_set(T6000_DCS_PS_STEP)) + t->incomplete |= T6000_INCOMPLETE_DCS_BUSY; + if (!t6000_dcs_set(T6000_DCS_PS_SLEEP)) + t->incomplete |= T6000_INCOMPLETE_DCS_BUSY; + } else { + t->incomplete |= T6000_INCOMPLETE_DCS_KEPT; + } + t->afr_saved = readl(s->ps + T6000_PS_AFR); + t->afr_done = true; + writel(0, s->ps + T6000_PS_AFR); + readl_poll_timeout_atomic(s->ps + T6000_PS_AFR, val, !(val & PS_ACTUAL), 1, 8000); + + sleep_note(s, 0x77); + /* 6. The complexes' targets, all but the E cluster's, which goes last */ + t->cluster_written = 0; + for (i = 1; i < T6000_CLUSTERS; i++) { + void __iomem *reg = s->cluster + 4 * i; + + if (!(T6000_CLUSTERS_VALID & BIT(i))) + continue; + if (!t6000_cluster_gate_ok(s, i)) { + t->clusters_kept |= BIT(i); + continue; + } + t->cluster_saved[i] = readl(reg); + writel(t->cluster_saved[i] & ~T6000_CLUSTER_PS_TARGET, reg); + t->cluster_written |= BIT(i); + if (!(T6000_CLUSTERS_CPU & BIT(i))) + readl_poll_timeout_atomic(reg, val, !(val & T6000_CLUSTER_PS_BUSY), + 1, 10000); + } + + sleep_note(s, 0x78); + /* + * 7. The SoC clock muxes onto the reference clock, then PLL0 off, the three writes + * twice over as recorded. A mux that stays busy leaves PLL0 on. + */ + t->mux_n = 0; + for (i = 0; i < ARRAY_SIZE(t6000_clkgen_mux); i++) { + void __iomem *reg = t->clkgen + t6000_clkgen_mux[i]; + + val = readl(reg); + t->mux_saved[i] = val; + writel((val & ~T6000_CLKGEN_MUX_SEL) | T6000_CLKGEN_MUX_ON, reg); + t->mux_n = i + 1; + if (readl_poll_timeout_atomic(reg, val, !(val & T6000_CLKGEN_MUX_BUSY), 1, 8000)) { + t->incomplete |= T6000_INCOMPLETE_PLL_KEPT; + goto skip_pll; + } + } + t->pll_saved = readl(t->pll0); + t->pll_done = true; + for (n = 0; n < 2; n++) { + val = readl(t->pll0); + writel((val & ~T6000_PLL_OFF_MODE) | FIELD_PREP(T6000_PLL_OFF_MODE, 3), t->pll0); + readl_poll_timeout_atomic(t->pll0, val, !(val & T6000_PLL_BUSY), 1, 8000); + val = readl(t->pll0); + writel(val | T6000_PLL_OFF_OK, t->pll0); + readl_poll_timeout_atomic(t->pll0, val, !(val & T6000_PLL_BUSY), 1, 8000); + val = readl(t->pll0); + writel(val & ~T6000_PLL_ON, t->pll0); + readl_poll_timeout_atomic(t->pll0, val, !(val & T6000_PLL_BUSY), 1, 8000); + } +skip_pll: + + sleep_note(s, 0x79); + /* 8. The other cores stopped, as macOS does it here, then the E cluster's target */ + s->cpus_stop_ret = spin_table_stop_parked(); + t->cluster_saved[T6000_CLUSTER_EACC] = readl(s->cluster); + writel(t->cluster_saved[T6000_CLUSTER_EACC] & ~T6000_CLUSTER_PS_TARGET, s->cluster); + t->cluster_written |= BIT(T6000_CLUSTER_EACC); + dsb(sy); + + /* 9. The dcs_* domains' words as the first sleep had them, and NCO channels enabled */ + t6000_dcs_words_commit(); + t6000_nco_enable(); + + sleep_note(s, 0x7a); + t6000_wdt_arm(s, 125); + + /* 10. Final PMGR, clock, fabric p-state and SIO state for the S2R sequencer */ + t6000_commit_late_state(s); + dsb(sy); + mdelay(T6000_LATE_SETTLE_MS); + + /* 11. The PMU RTC alarm's SPMI vector: its latched status cleared, then enabled */ + for (i = 0; i < T6000_SPMI_VEC_WORDS; i++) + t->spmi_saved[i] = readl(s->spmi + T6000_SPMI_VEC_ENABLE + 4 * i); + writel(T6000_SPMI_VEC_RTC, s->spmi + T6000_SPMI_VEC_ACK); + writel(t->spmi_saved[0] | T6000_SPMI_VEC_RTC, s->spmi + T6000_SPMI_VEC_ENABLE); + t->spmi_armed = true; + + /* 12. Every interrupt line masked here, so the timer's is the last write */ + t6000_aic_mask_all(); + t6000_wdt_arm(s, 150); +} + +static void t6000_sleep_wake(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int a, i, j, r; + u32 val; + u64 qval; + + sleep_note(s, 0x7b); + /* For the rest of the wake, until .finish puts WD1 back */ + t6000_wdt_arm(s, T6000_WDT_WAKE_SECS); + + if (t->sep_pg_cleared) { + writel(t->sep_pg_saved, t->sep_pg); + t->sep_pg_cleared = false; + } + + if (t->spmi_armed) { + for (i = 0; i < T6000_SPMI_VEC_WORDS; i++) + writel(t->spmi_saved[i], s->spmi + T6000_SPMI_VEC_ENABLE + 4 * i); + t->spmi_armed = false; + } + + /* The DCS channels' control as before the commit */ + if (t->mcc_done) { + for (a = 0; a < T6000_AMCCS; a++) + for (i = 0; i < T6000_DCS_PER_AMCC; i++) + if (t->dcs_block[a][i]) + writel(t->dcs_ctl_saved[a][i], + t->dcs_block[a][i] + T6000_DCS_CTL); + t->mcc_done = false; + } + + /* PLL0 on, then the SoC clock muxes back, last first */ + if (t->pll_done) { + val = readl(t->pll0); + writel(val | T6000_PLL_ON, t->pll0); + readl_poll_timeout_atomic(t->pll0, val, !(val & T6000_PLL_BUSY), 1, 8000); + writel(t->pll_saved, t->pll0); + readl_poll_timeout_atomic(t->pll0, val, !(val & T6000_PLL_BUSY), 1, 8000); + t->pll_done = false; + } + while (t->mux_n) { + void __iomem *reg = t->clkgen + t6000_clkgen_mux[--t->mux_n]; + + writel(t->mux_saved[t->mux_n], reg); + readl_poll_timeout_atomic(reg, val, !(val & T6000_CLKGEN_MUX_BUSY), 1, 8000); + } + + /* + * The complexes' targets. AFR's was already 0 when the commit read it, after the GPU's + * way down: it goes back to what it was before that. + */ + for (i = 0; i < T6000_CLUSTERS; i++) { + void __iomem *reg = s->cluster + 4 * i; + + if (!(t->cluster_written & BIT(i))) + continue; + if (i == T6000_CLUSTER_AFR && t->afr_cluster_valid && + !(t->cluster_saved[i] & T6000_CLUSTER_PS_TARGET)) { + if ((readl(reg) ^ t->afr_cluster_before) & T6000_CLUSTER_PS_TARGET) + writel(t->afr_cluster_before, reg); + } else { + writel(t->cluster_saved[i], reg); + } + readl_poll_timeout_atomic(reg, val, !(val & T6000_CLUSTER_PS_BUSY), 1, 10000); + } + t->cluster_written = 0; + + if (t->afr_done) { + writel(t->afr_saved, s->ps + T6000_PS_AFR); + readl_poll_timeout_atomic(s->ps + T6000_PS_AFR, val, + FIELD_GET(PS_ACTUAL, val) == FIELD_GET(PS_TARGET, val), + 1, 8000); + t->afr_done = false; + } + if (t->dcs_done) { + t6000_dcs_set(T6000_DCS_PS_STEP); + t6000_dcs_set(FIELD_GET(T6000_DCS_PS_TARGET, t->dcs_saved)); + t->dcs_done = false; + } + + /* The CPU complexes: APSC as it was, then the other words, last first */ + if (t->acc_done) { + for (i = 0; i < T6000_ACCS; i++) { + for (r = T6000_ACCR_N; r-- > 0;) { + void __iomem *reg = t->acc[i] + t6000_acc_regs[r]; + + if (r == T6000_ACCR_DVFS) { + qval = readq(reg) & ~T6000_ACC_DVFS_APSC_OFF; + writeq(qval | (t->acc_saved[i][r] & + T6000_ACC_DVFS_APSC_OFF), reg); + readq_poll_timeout_atomic(reg, qval, + !(qval & T6000_ACC_DVFS_BUSY), + 1, 8000); + } else { + writeq(t->acc_saved[i][r], reg); + } + } + } + t->acc_done = false; + } + if (t->relock_done) { + for (i = 2; i-- > 0;) { + void __iomem *reg = t->acc[i] + T6000_ACC_DVFS_CMD; + + writeq(t->relock_saved[i], reg); + readq_poll_timeout_atomic(reg, qval, !(qval & T6000_ACC_DVFS_BUSY), + 1, 8000); + } + t->relock_done = false; + } + + if (t->zeros_done) { + for (i = ARRAY_SIZE(t6000_quiesce_zero_pa); i-- > 0;) + if (t->zero[i]) + writel(t->zero_saved[i], t->zero[i]); + t->zeros_done = false; + } + + /* The always-on power words the commit lowered: they kept that through the sleep */ + if (t->aon_saved_valid) { + for (i = ARRAY_SIZE(t6000_aon_commit_off); i-- > 0;) { + void __iomem *reg = s->ps_aon + t6000_aon_commit_off[i].offset; + u32 saved = t->aon_saved[i]; + + writel(saved & ~(PS_FLAGS | PS_ACTUAL), reg); + readl_poll_timeout_atomic(reg, val, + FIELD_GET(PS_ACTUAL, val) == + FIELD_GET(PS_TARGET, saved), 1, 5000); + } + t->aon_saved_valid = false; + } + + /* Every other PMGR domain that was on, back on */ + t6000_domains_restore(); + + /* The PMP's address filter and reset vector, before its driver restarts it */ + for (i = 0; t->pmp_dapf && i < t->pmp_dapf_n; i++) { + void __iomem *entry = t->pmp_dapf + i * T6000_PMP_DAPF_STRIDE; + + for (j = 1; j < T6000_PMP_DAPF_WORDS; j++) + writel(t->pmp_dapf_saved[i][j], entry + 4 * j); + writel(t->pmp_dapf_saved[i][0], entry); + } + if (t->pmp_iorvbar_valid && + !(readq(t->pmp_iorvbar) & T6000_PMP_IORVBAR_VALID)) + writeq(t->pmp_iorvbar_saved, t->pmp_iorvbar); + + t6000_nco_restore(); + + /* SEP's domain up before its DART resumes: automatic state and target 0xf */ + if (apple_sleep_enclave.stop) { + void __iomem *sep_ps = t->ps_east + T6000_PS_EAST_SEP; + + val = readl(sep_ps); + writel((val & ~(T6000_PS_AUTO | PS_DEV_DISABLE | PS_FLAGS)) | + FIELD_PREP(T6000_PS_AUTO, 0xf) | T6000_PS_WAS_CLKGATED | PS_TARGET, sep_ps); + readl_poll_timeout_atomic(sep_ps, val, FIELD_GET(PS_ACTUAL, val) == 0xf, + 10, 50000); + } + + /* SIO, then GPIO, on before the AP pin state is put back */ + val = readl(t->ps_east + T6000_PS_EAST_SIO); + writel((val & ~GENMASK(11, 0)) | PS_TARGET, t->ps_east + T6000_PS_EAST_SIO); + readl_poll_timeout_atomic(t->ps_east + T6000_PS_EAST_SIO, val, + FIELD_GET(PS_ACTUAL, val) == 0xf, 10, 50000); + sleep_ps_raise(s, T6000_PS_GPIO); + readl_poll_timeout_atomic(s->ps + T6000_PS_GPIO, val, FIELD_GET(PS_ACTUAL, val) == 0xf, + 10, 50000); + + /* + * The sleep resets every AP pin with the gpio domain, and the pin controller's driver + * restores only pins it configured: put all of them back now, before drivers resume. + */ + s->pins_lost = s->pins_saved_valid; + apple_sleep_restore_pins(); + + sleep_note(s, 0x7d); +} + +/* One line for the steps of the entry that were left out or did not finish, if any */ +static void t6000_report_incomplete(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + char steps[192] = ""; + unsigned int i, len = 0; + + if (!t->incomplete && !t->clusters_kept) + return; + for (i = 0; i < ARRAY_SIZE(t6000_incomplete_names); i++) + if (t->incomplete & BIT(i)) + len += scnprintf(steps + len, sizeof(steps) - len, "%s%s", + len ? ", " : "", t6000_incomplete_names[i]); + if (t->clusters_kept) + scnprintf(steps + len, sizeof(steps) - len, "%scomplexes %#x left on", + len ? ", " : "", t->clusters_kept); + dev_warn(s->dev, "T6000: sleep entry incomplete: %s\n", steps); + t->incomplete = 0; + t->clusters_kept = 0; +} + +/* At .wake and again at .end: what the entry changed in the PMP and the watchdog, once */ +static void t6000_sleep_finish(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int i; + + if (t->ptd_sleep_set) { + writel(t->ptd_sleep_saved, t->ptd + T6000_PTD_SLEEP); + t->ptd_sleep_set = false; + } + + /* The devices' requests and the fabric DVFS words, with the PMP running again */ + if (t->ptd_written) { + u64 cur = readq(t->ptd + T6000_PTD_PS_REQ_RD); + + if (t->ptd_req_saved & ~cur) + writeq(cur | t->ptd_req_saved, t->ptd + T6000_PTD_PS_REQ_WR); + for (i = 0; i < 4; i++) + writeq(t->dvfs_saved[i], t->ptd + T6000_PTD_DVFS_WR(i)); + mdelay(10); + t->ptd_written = false; + } + + t6000_wdt_restore(s); + + t6000_report_incomplete(s); + if (t->dcs_floors) { + dev_dbg(s->dev, "T6000: minimum state cleared in %u dcs power words at the first sleep\n", + t->dcs_floors); + t->dcs_floors = 0; + } + if (t->dcs_changed) { + dev_info(s->dev, "T6000: %u dcs power words put back to their first sleep's state, %s from %#010x to %#010x\n", + t->dcs_changed, t->dcs_changed_first->name, t->dcs_changed_was, + readl(t->dcs_changed_first->reg)); + t->dcs_changed = 0; + } +} + +/* + * Nothing per core before its stop or after its start: on the deep sleep path the reference + * leaves each core's interrupt routing as it is. + */ +static int t6000_sleep_cpu_prepare(u64 mpidr) +{ + return 0; +} + +static void t6000_sleep_cpu_restore(u64 mpidr) +{ +} + +static const struct apple_sleep_ops t6000_sleep_ops = { + .deep_wfi_arm = t6000_deep_wfi_arm, + .resume = cpu_resume, + .aic_mask_all = t6000_aic_mask_all, + .aic_ack = aic_sleep_ack, + .aic_restore = t6000_aic_restore, + .map_regs = t6000_sleep_map_regs, + .begin = t6000_sleep_begin, + .before_enclave = t6000_before_enclave, + .display_off = t6000_sleep_display_off, + .final_check = t6000_sleep_final_check, + .prepare_regs = t6000_sleep_prepare_regs, + .undo_regs = t6000_sleep_undo_regs, + .commit_regs = t6000_sleep_commit_regs, + .wake = t6000_sleep_wake, + .finish = t6000_sleep_finish, + .cpu_prepare = t6000_sleep_cpu_prepare, + .cpu_restore = t6000_sleep_cpu_restore, +}; + +static const struct apple_sleep_soc t6000_sleep_soc = { + .ops = &t6000_sleep_ops, + .early_context = true, + /* The safety net is WD1, armed by the operations themselves. */ + .no_wd2 = true, + .no_pmu_notes = true, + .smc_gen1_keys = true, + .enclave_first = true, + .commit_stops_cpus = true, + .pmu_usid = 0xf, + .ndisp = 1, + /* MPIDR affinity 1 is the cluster: E, P0, P1; start bits 4 * cluster + core */ + .cpu_cluster_affinity = 1, + .cpu_clusters = 3, + .cpu_cores_per_cluster = 4, + .cpu_start_stride = 4, + .cluster_cpu = { 4 * T6000_CLUSTER_EACC, 4 * T6000_CLUSTER_PACC0, 4 * T6000_CLUSTER_PACC1 }, + .ncluster_cpu = 3, + .wake_mask = T6000_WAKE_MASK, + .wake_status = T6000_WAKE_STATUS, + .wake_words = T6000_WAKE_WORDS, + .wake_last_word = T6000_WAKE_LAST_WORD, + .wake_names = t6000_wake_names, + .nwake_names = ARRAY_SIZE(t6000_wake_names), + .ps = { + .gpio = T6000_PS_GPIO, + }, +}; diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index faa158404a2db..3b19f4f32f498 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -10155,6 +10155,7 @@ static const struct apple_sleep_soc t8142_sleep_soc = { .wake_last_word = GENMASK(11, 0), }; +#include "sleep-t6000.h" #include "sleep-t8103.h" #include "sleep-t8122.h" @@ -10169,6 +10170,7 @@ static const struct of_device_id apple_sleep_of_match[] = { { .compatible = "apple,t8142-sleep", .data = &t8142_sleep_soc }, { .compatible = "apple,t6050-sleep", .data = &t6050_sleep_soc }, { .compatible = "apple,t6050-j716c-sleep", .data = &t6050_j716c_sleep_soc }, + { .compatible = "apple,t6000-sleep", .data = &t6000_sleep_soc }, { .compatible = "apple,t8103-sleep", .data = &t8103_sleep_soc }, { .compatible = "apple,t8122-sleep", .data = &t8122_sleep_soc }, { } From 9756f637c0af06f30f98ef42e7dad73c99c5c704 Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:54:26 +0000 Subject: [PATCH 22/33] rust: soc: apple: rtkit: wrap sleep_stop and sleep_return_with_init Add the two calls a Rust driver needs to take its coprocessor through a sleep that cuts its power: apple_rtkit_sleep_stop() and apple_rtkit_sleep_return_with_init(). Both take the mapped CPU control word of the coprocessor's ASC and are unsafe for that reason. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- rust/kernel/soc/apple/rtkit.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/rust/kernel/soc/apple/rtkit.rs b/rust/kernel/soc/apple/rtkit.rs index 22d0bf9bbcfc3..ffdcdcdd301c3 100644 --- a/rust/kernel/soc/apple/rtkit.rs +++ b/rust/kernel/soc/apple/rtkit.rs @@ -427,6 +427,31 @@ impl RtKit { unsafe { bindings::apple_rtkit_set_oslog_echo(self.rtk, echo) }; } + /// Parks a running coprocessor for a system sleep: AP and IOP state idle, the mailbox off, + /// the CPU's run bit cleared. + /// + /// # Safety + /// + /// `cpu_control` must be the mapped CPU control word of this coprocessor's ASC. + pub unsafe fn sleep_stop(self: Pin<&mut Self>, cpu_control: *mut core::ffi::c_void) -> Result { + // SAFETY: `rtk` is valid per the type invariant, `cpu_control` per the caller. + to_result(unsafe { bindings::apple_rtkit_sleep_stop(self.rtk, cpu_control) }) + } + + /// Restarts a parked coprocessor whose firmware lost its memory in the sleep: the whole + /// boot handshake, the application endpoints started again, then the AP's state on. + /// + /// # Safety + /// + /// `cpu_control` must be the mapped CPU control word of this coprocessor's ASC. + pub unsafe fn sleep_return_with_init( + self: Pin<&mut Self>, + cpu_control: *mut core::ffi::c_void, + ) -> Result { + // SAFETY: `rtk` is valid per the type invariant, `cpu_control` per the caller. + to_result(unsafe { bindings::apple_rtkit_sleep_return_with_init(self.rtk, cpu_control) }) + } + /// Checks if an endpoint is present pub fn has_endpoint(self: Pin<&mut Self>, endpoint: u8) -> bool { unsafe { bindings::apple_rtkit_has_endpoint(self.rtk, endpoint) } From c18c3ebf84cceeb4a2be5cdbdace51f4fd4b4ee5 Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:54:26 +0000 Subject: [PATCH 23/33] drm/asahi: park and restart the T6000 GPU firmware around suspend-to-RAM The first-generation driver, which runs the G13 GPUs on firmware 13.5, refuses every sleep that goes through firmware: the sleep takes the GPU's power and the driver could not bring its firmware back. On T6000 (M1 Pro) it can: - at .suspend, submissions still in flight get 500 ms to finish, then the firmware is parked and its CPU stopped with apple_rtkit_sleep_stop(). A submission still pending refuses the sleep; - at .resume it is restarted with apple_rtkit_sleep_return_with_init(). The firmware boots from scratch; the driver keeps the state it had in DRAM and kicks the firmware once it is up. Select this with a new HwConfig field, sleep_restart, set for T6000 alone. The driver keeps the GPU object and its ASC block for the two callbacks. A dry run of the sleep leaves the firmware running. A sleep refused after the park leaves the GPU down until the next sleep that happens, and says so. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/gpu/drm/asahi/driver.rs | 91 +++++++++++++++++++++++++++++++ drivers/gpu/drm/asahi/gpu.rs | 90 ++++++++++++++++++++++++++++++ drivers/gpu/drm/asahi/hw/mod.rs | 3 + drivers/gpu/drm/asahi/hw/t600x.rs | 3 + drivers/gpu/drm/asahi/hw/t602x.rs | 1 + drivers/gpu/drm/asahi/hw/t8103.rs | 1 + drivers/gpu/drm/asahi/hw/t8112.rs | 1 + 7 files changed, 190 insertions(+) diff --git a/drivers/gpu/drm/asahi/driver.rs b/drivers/gpu/drm/asahi/driver.rs index 4ca274e2be406..b31e20b21c0ad 100644 --- a/drivers/gpu/drm/asahi/driver.rs +++ b/drivers/gpu/drm/asahi/driver.rs @@ -62,6 +62,9 @@ pub(crate) struct AsahiPlatformDriverData<'bound> { raw_stats: *mut core::ffi::c_void, // Own the allocation until unregister has drained all sysfs readers. stats: Option>, + /// The first-generation driver's GPU and its ASC block, for [`legacy_sleep`]. + legacy_gpu: Option>, + legacy_asc: Option>, } // SAFETY: `raw_dev` is only passed back to the sysfs shim, which serializes its own state. @@ -285,10 +288,76 @@ unsafe extern "C" fn sleep_prepare(dev: *mut bindings::device) -> core::ffi::c_i } match data.g17.as_ref() { Some(gpu) => kernel::error::from_result(|| gpu.sleep_prepare().map(|_| 0)), + // A GPU whose firmware is parked at .suspend and restarted at .resume has nothing to + // refuse here. + None if legacy_sleep(data).is_some() => 0, None => EBUSY.to_errno(), } } +#[cfg(CONFIG_APPLE_SOC_SLEEP)] +extern "C" { + fn apple_sleep_dry_run() -> bool; +} + +/// Whether this firmware-driven sleep stops before its commit. +fn sleep_dry_run() -> bool { + #[cfg(CONFIG_APPLE_SOC_SLEEP)] + // SAFETY: Takes no arguments and reads the sleep driver's state. + return unsafe { apple_sleep_dry_run() }; + #[cfg(not(CONFIG_APPLE_SOC_SLEEP))] + false +} + +/// The offset of the ASC's CPU control word in its `asc` block. +const ASC_CPU_CONTROL_OFFSET: usize = 0x44; + +/// The GPU and its ASC CPU control word, where the firmware is parked for a firmware-driven +/// sleep and restarted after it ([`hw::HwConfig::sleep_restart`]). +fn legacy_sleep<'a>( + data: &'a AsahiPlatformDriverData<'_>, +) -> Option<(&'a Arc, *mut core::ffi::c_void)> { + let gpu = data.legacy_gpu.as_ref()?; + let asc = data.legacy_asc.as_ref()?; + + if !gpu.sleep_supported() { + return None; + } + let control = + ::as_ptr(kernel::io::IoBase::as_view(asc)) + .cast::() + .wrapping_add(ASC_CPU_CONTROL_OFFSET) + .cast::(); + Some((gpu, control)) +} + +/// Parks the firmware for a sleep that takes its power. Not in a dry run: a parked firmware +/// only comes back through a real sleep. +unsafe extern "C" fn sleep_suspend(dev: *mut bindings::device) -> core::ffi::c_int { + // SAFETY: Reads a global flag word. + if !unsafe { bindings::pm_suspend_via_firmware() } || sleep_dry_run() { + return 0; + } + // SAFETY: Called by the PM core. + let data = unsafe { sleep_data(dev) }; + match legacy_sleep(data) { + Some((gpu, control)) => { + kernel::error::from_result(|| gpu.sleep_suspend(control).map(|_| 0)) + } + None => 0, + } +} + +/// Runs once the power domains and the PMP are back, before user space thaws. +unsafe extern "C" fn sleep_resume(dev: *mut bindings::device) -> core::ffi::c_int { + // SAFETY: Called by the PM core. + let data = unsafe { sleep_data(dev) }; + if let Some((gpu, control)) = legacy_sleep(data) { + gpu.sleep_resume(control); + } + 0 +} + extern "C" { fn asahi_sleep_pd_init(dev: *mut core::ffi::c_void) -> core::ffi::c_int; fn asahi_sleep_pd_suspend_noirq(dev: *mut core::ffi::c_void) -> core::ffi::c_int; @@ -333,6 +402,8 @@ unsafe extern "C" fn sleep_complete(dev: *mut bindings::device) { static SLEEP_OPS: SleepOps = SleepOps(bindings::dev_pm_ops { prepare: Some(sleep_prepare), complete: Some(sleep_complete), + suspend: Some(sleep_suspend), + resume: Some(sleep_resume), suspend_noirq: Some(sleep_suspend_noirq), resume_noirq: Some(sleep_resume_noirq), // SAFETY: All-zero is the absence of every other callback. @@ -497,6 +568,10 @@ impl platform::Driver for AsahiPlatformDriver { }; gpu.init()?; + let legacy_gpu = Some(gpu.clone()); + let legacy_asc = pdev + .io_request_by_name(c"asc") + .and_then(|req| req.iomap_sized::<{ regs::ASC_CTL_SIZE }>().ok()); let stats = gpu.stats_snapshot(); let debug_stats = gpu.raw_stats_snapshot(); let data = try_pin_init!(AsahiDrmRegistrationData { gpu, resources }); @@ -535,6 +610,8 @@ impl platform::Driver for AsahiPlatformDriver { raw_dev, raw_stats, stats, + legacy_gpu, + legacy_asc, }; Ok(driver) @@ -580,6 +657,8 @@ impl AsahiPlatformDriver { raw_dev: core::ptr::null_mut(), raw_stats: core::ptr::null_mut(), stats: None, + legacy_gpu: None, + legacy_asc: None, }) } @@ -617,6 +696,8 @@ impl AsahiPlatformDriver { raw_dev: core::ptr::null_mut(), raw_stats: core::ptr::null_mut(), stats: None, + legacy_gpu: None, + legacy_asc: None, }) } @@ -659,6 +740,8 @@ impl AsahiPlatformDriver { raw_dev: core::ptr::null_mut(), raw_stats: core::ptr::null_mut(), stats: None, + legacy_gpu: None, + legacy_asc: None, }) } @@ -744,6 +827,8 @@ impl AsahiPlatformDriver { raw_dev, raw_stats, stats, + legacy_gpu: None, + legacy_asc: None, }) } } @@ -793,6 +878,8 @@ impl AsahiPlatformDriver { raw_dev: core::ptr::null_mut(), raw_stats: core::ptr::null_mut(), stats: None, + legacy_gpu: None, + legacy_asc: None, }) } } @@ -834,6 +921,8 @@ impl AsahiPlatformDriver { raw_dev, raw_stats, stats: None, + legacy_gpu: None, + legacy_asc: None, }) } } @@ -893,6 +982,8 @@ impl AsahiPlatformDriver { raw_dev: core::ptr::null_mut(), raw_stats: core::ptr::null_mut(), stats: None, + legacy_gpu: None, + legacy_asc: None, }), }) } diff --git a/drivers/gpu/drm/asahi/gpu.rs b/drivers/gpu/drm/asahi/gpu.rs index fb5359137b668..4aaabcea9988d 100644 --- a/drivers/gpu/drm/asahi/gpu.rs +++ b/drivers/gpu/drm/asahi/gpu.rs @@ -164,6 +164,9 @@ pub(crate) const IOVA_KERN_GPU_BUFMGR_HIGH: u64 = 0xffffffaeffff0000; /// Timeout for entering the halt state after a fault or request. const HALT_ENTER_TIMEOUT: Delta = Delta::from_millis(100); +/// How long submissions in flight get to finish before a sleep, and how often they are polled. +const SLEEP_DRAIN_MS: i64 = 500; +const SLEEP_DRAIN_POLL_MS: i64 = 5; /// Minimum interval between GPU fault / timeout / channel-error reports. /// @@ -329,6 +332,7 @@ pub(crate) struct GpuManager { /// The firmware's timestamp aperture in the kernel VM, where timestamp buffers are bound. timestamp_range: Range, crashed: AtomicBool, + sleep_parked: AtomicBool, /// Set once the over-limit firmware-private garbage was reported as held. garbage_held: AtomicBool, #[pin] @@ -430,6 +434,17 @@ pub(crate) trait Gpu: Send + Sync { fn ids(&self) -> &SequenceIDs; /// Check whether the GPU is crashed fn is_crashed(&self) -> bool; + /// Whether the firmware can be parked for a firmware-driven sleep and restarted after it. + fn sleep_supported(&self) -> bool { + false + } + /// Parks the firmware and stops its CPU, with user space frozen and before the noirq phase. + /// `asc_cpu_control` is the mapped CPU control word of the GPU's ASC. + fn sleep_suspend(&self, _asc_cpu_control: *mut core::ffi::c_void) -> Result { + Err(EBUSY) + } + /// Restarts a firmware [`Gpu::sleep_suspend`] parked, after a sleep that took its power. + fn sleep_resume(&self, _asc_cpu_control: *mut core::ffi::c_void) {} /// Fill in the hardware description reported to userspace. fn get_params(&self, params: &mut uapi::drm_asahi_params_global) -> Result; /// Whether unmapped GPU reads return zero (fault_control programmed into the firmware). @@ -1130,6 +1145,7 @@ impl GpuManager::ver { next_mmio_iova: IOVA_KERN_MMIO_RANGE.start, rtkit <- new_mutex!(None, "rtkit"), crashed: AtomicBool::new(false), + sleep_parked: AtomicBool::new(false), garbage_held: AtomicBool::new(false), event_manager, stats: stats_snap.clone(), @@ -1730,6 +1746,80 @@ impl Gpu for GpuManager::ver { self.crashed.load(Ordering::Relaxed) } + fn sleep_supported(&self) -> bool { + self.cfg.sleep_restart + } + + fn sleep_suspend(&self, asc_cpu_control: *mut core::ffi::c_void) -> Result { + if !self.sleep_supported() || self.is_crashed() || asc_cpu_control.is_null() { + return Err(EBUSY); + } + + // User space is frozen, so nothing new is submitted: what the firmware still has in + // flight gets SLEEP_DRAIN_MS to finish, then the sleep is refused. + let mut pending = 0u32; + for _ in 0..SLEEP_DRAIN_MS / SLEEP_DRAIN_POLL_MS { + pending = self + .initdata + .globals + .with(|raw, _inner| raw.pending_submissions.load(Ordering::Acquire)); + if pending == 0 { + break; + } + kernel::time::delay::fsleep(Delta::from_millis(SLEEP_DRAIN_POLL_MS)); + } + if pending != 0 { + dev_warn!( + self.dev.as_ref(), + "{} submission(s) still pending: refusing the sleep\n", + pending + ); + return Err(EBUSY); + } + + let mut guard = self.rtkit.lock(); + let rtk = guard.as_mut().as_pin_mut().ok_or(ENODEV)?; + // SAFETY: The driver passes the CPU control word of this GPU's mapped ASC block. + unsafe { rtk.sleep_stop(asc_cpu_control) }?; + self.sleep_parked.store(true, Ordering::Release); + Ok(()) + } + + fn sleep_resume(&self, asc_cpu_control: *mut core::ffi::c_void) { + if !self.sleep_parked.swap(false, Ordering::AcqRel) || asc_cpu_control.is_null() { + return; + } + // SAFETY: Reads a global flag word. + if !unsafe { kernel::bindings::pm_resume_via_firmware() } { + dev_warn!( + self.dev.as_ref(), + "the sleep was refused with the firmware parked: it stays down until a sleep that happens\n" + ); + return; + } + + let ret = { + let mut guard = self.rtkit.lock(); + match guard.as_mut().as_pin_mut() { + // SAFETY: As in `sleep_suspend()`. + Some(rtk) => unsafe { rtk.sleep_return_with_init(asc_cpu_control) }, + None => Err(ENODEV), + } + }; + match ret { + Ok(()) => { + let _ = self.kick_firmware(); + } + Err(e) => { + dev_err!( + self.dev.as_ref(), + "firmware did not restart after the sleep: {:?}\n", + e + ); + } + } + } + fn get_params(&self, params: &mut uapi::drm_asahi_params_global) -> Result { params.gpu_generation = self.dyncfg.id.gpu_gen as u32; params.gpu_variant = self.dyncfg.id.gpu_variant as u32; diff --git a/drivers/gpu/drm/asahi/hw/mod.rs b/drivers/gpu/drm/asahi/hw/mod.rs index 67335112eef0b..77105d2735e3e 100644 --- a/drivers/gpu/drm/asahi/hw/mod.rs +++ b/drivers/gpu/drm/asahi/hw/mod.rs @@ -235,6 +235,9 @@ pub(crate) struct HwClusteringConfig { pub(crate) struct HwConfig { /// Chip ID in hex format (e.g. 0x8103 for t8103). pub(crate) chip_id: u32, + /// A firmware-driven system sleep takes the GPU's power, and its firmware can be parked + /// before the sleep and restarted from scratch after it, see `Gpu::sleep_suspend()`. + pub(crate) sleep_restart: bool, /// GPU generation. pub(crate) gpu_gen: GpuGen, /// GPU variant type. diff --git a/drivers/gpu/drm/asahi/hw/t600x.rs b/drivers/gpu/drm/asahi/hw/t600x.rs index 85cf88054e44c..ea8472cb0bbff 100644 --- a/drivers/gpu/drm/asahi/hw/t600x.rs +++ b/drivers/gpu/drm/asahi/hw/t600x.rs @@ -44,6 +44,7 @@ const fn iomaps(mcc_count: usize, has_die1: bool) -> [Option; 20] { } pub(crate) const HWCONFIG_T6002: super::HwConfig = HwConfig { + sleep_restart: false, chip_id: 0x6002, gpu_gen: GpuGen::G13, gpu_variant: GpuVariant::D, @@ -150,6 +151,8 @@ pub(crate) const HWCONFIG_T6001: super::HwConfig = HwConfig { pub(crate) const HWCONFIG_T6000: super::HwConfig = HwConfig { chip_id: 0x6000, + // Qualified on the M1 Pro alone; T6001 and T6002 have no sleep controller. + sleep_restart: true, gpu_variant: GpuVariant::S, gpu_core: GpuCore::G13S, diff --git a/drivers/gpu/drm/asahi/hw/t602x.rs b/drivers/gpu/drm/asahi/hw/t602x.rs index 6e2033ecb50dd..28839bc31161b 100644 --- a/drivers/gpu/drm/asahi/hw/t602x.rs +++ b/drivers/gpu/drm/asahi/hw/t602x.rs @@ -47,6 +47,7 @@ const fn iomaps(chip_id: u32, mcc_count: usize) -> [Option; 24] { // TODO: Tentative pub(crate) const HWCONFIG_T6022: super::HwConfig = HwConfig { + sleep_restart: false, chip_id: 0x6022, gpu_gen: GpuGen::G14, gpu_variant: GpuVariant::D, diff --git a/drivers/gpu/drm/asahi/hw/t8103.rs b/drivers/gpu/drm/asahi/hw/t8103.rs index 261e51fbc4c83..2ad84a0671db2 100644 --- a/drivers/gpu/drm/asahi/hw/t8103.rs +++ b/drivers/gpu/drm/asahi/hw/t8103.rs @@ -7,6 +7,7 @@ use crate::f32; use super::*; pub(crate) const HWCONFIG: super::HwConfig = HwConfig { + sleep_restart: false, chip_id: 0x8103, gpu_gen: GpuGen::G13, gpu_variant: GpuVariant::G, diff --git a/drivers/gpu/drm/asahi/hw/t8112.rs b/drivers/gpu/drm/asahi/hw/t8112.rs index d22238d90247b..eaaa924fd4de4 100644 --- a/drivers/gpu/drm/asahi/hw/t8112.rs +++ b/drivers/gpu/drm/asahi/hw/t8112.rs @@ -7,6 +7,7 @@ use crate::f32; use super::*; pub(crate) const HWCONFIG: super::HwConfig = HwConfig { + sleep_restart: false, chip_id: 0x8112, gpu_gen: GpuGen::G14, gpu_variant: GpuVariant::G, From d07fba233d1a43d280df7baf1ea51382210237ef Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Sat, 28 Nov 2026 02:49:26 +0000 Subject: [PATCH 24/33] arm64: dts: apple: t600x: add the sleep controller node, disabled Describe the register blocks the T6000 suspend-to-RAM profile writes, and name the internal display coprocessor for the core's display check. The node sits in the die 0 include that T6000, T6001 and T6002 share, since the blocks are the die's. The sequence has only been qualified on T6000 (M1 Pro), so the node is disabled here and a board that sleeps enables it. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- arch/arm64/boot/dts/apple/t600x-die0.dtsi | 44 +++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/arch/arm64/boot/dts/apple/t600x-die0.dtsi b/arch/arm64/boot/dts/apple/t600x-die0.dtsi index 5e1df81ed234e..e5b28dc73f229 100644 --- a/arch/arm64/boot/dts/apple/t600x-die0.dtsi +++ b/arch/arm64/boot/dts/apple/t600x-die0.dtsi @@ -121,6 +121,50 @@ #clock-cells = <1>; }; + /* + * The registers of a suspend-to-RAM, in blocks other drivers also use. + * Only qualified on T6000: a board that sleeps enables it. + */ + sleep: sleep-controller@28e080000 { + compatible = "apple,t6000-sleep"; + reg = <0x2 0x8e080000 0x0 0x1000>, + <0x2 0x8e580000 0x0 0x1000>, + <0x2 0x92280000 0x0 0x1000>, + <0x2 0x8e0d4000 0x0 0x14>, + <0x2 0x8e098000 0x0 0x24>, + <0x2 0x925c4000 0x0 0x4>, + <0x2 0x922b0000 0x0 0x40>, + <0x2 0x92004000 0x0 0x40>, + <0x2 0x8e0cc200 0x0 0x4>, + <0x2 0x10e20000 0x0 0x29000>, + <0x2 0x11e20000 0x0 0x29000>, + <0x2 0x12e20000 0x0 0x29000>, + <0x2 0x8e038000 0x0 0x200>, + <0x2 0x8e224000 0x0 0x4>, + <0x2 0x8e20c800 0x0 0x4>, + <0x5 0x8c008000 0x0 0x300>, + <0x2 0x00000000 0x0 0x160000>, + <0x2 0x01000000 0x0 0x160000>, + <0x2 0x02000000 0x0 0x160000>, + <0x2 0x03000000 0x0 0x160000>, + <0x2 0x8e3c0000 0x0 0x19000>, + <0x2 0x920a1300 0x0 0x100>; + reg-names = "ps", "ps-east", "ps-aon", + "cpu-start", "cluster", "commit", + "wdt", "wake", "sep-pg", + "acc0", "acc1", "acc2", + "clkgen", "pll0", "dcs", "apcie-q", + "amcc0", "amcc1", "amcc2", "amcc3", + "ptd", "spmi"; + apple,wake-enable = <12 14 26 27 + 40 43 49 50 51 52 54 + 85 90 + 126 127 128 129 130 131 132 133>; + apple,display-coprocessors = <&dcp>; + apple,pinctrl = <&pinctrl_ap>; + status = "disabled"; + }; + aic: interrupt-controller@28e100000 { compatible = "apple,t6000-aic", "apple,aic2"; #interrupt-cells = <4>; From 2581889d95120ea0b960226638da22ed886f3dae Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Sat, 28 Nov 2026 02:49:27 +0000 Subject: [PATCH 25/33] arm64: dts: apple: j314s: enable suspend-to-RAM Enable the sleep controller on the MacBook Pro 14" with M1 Pro, the board its sequence was brought up on, and describe what the sequence needs from the other devices: - the SMC writes pmVC = 0x100 before its sleep notice and switches SMC GPIO 26, the SD card reader's power enable, off for the sleep; - the PMP's firmware SRAM is kept across the sleep, which takes its power; - the AP pin controller has every pin restored except 114, 115 and 119; - the four USB-C port controllers are told the system power state, S3 before the sleep and S0 after it. The profile is not marked untested in the driver, so deep sleep is the default sleep state on the default kernel command line, with no apple_sleep parameter. The J316s and the T6001 and T6002 machines keep the node disabled. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- arch/arm64/boot/dts/apple/t6000-j314s.dts | 41 +++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/arch/arm64/boot/dts/apple/t6000-j314s.dts b/arch/arm64/boot/dts/apple/t6000-j314s.dts index 01108aa644dca..c02ee05003383 100644 --- a/arch/arm64/boot/dts/apple/t6000-j314s.dts +++ b/arch/arm64/boot/dts/apple/t6000-j314s.dts @@ -77,6 +77,47 @@ adj-height-mm = <189>; }; +/* + * Suspend-to-RAM, qualified on this board. What follows is what its sequence + * needs from the other devices. + */ +&sleep { + /* + * Left alone across a sleep, as in the board's ADT no-restore-gpio-pins. + */ + apple,pinctrl-skip-pins = <114 115 119>; + status = "okay"; +}; + +&smc { + /* VDD_CIO rail mask before the sleep notice: no port's rail kept up */ + apple,sleep-pmvc = <0x100>; + /* The SD card reader's power enable (port01's pwren), off while asleep */ + apple,sleep-low-pins = <26>; +}; + +&pmp { + /* The sleep takes the firmware's SRAM: all of it is kept and written back */ + apple,sram-keep = <0x0 0x100000>; +}; + +/* macOS moves each port controller to S3 before the sleep and to S0 after it. */ +&hpm0 { + apple,sleep-ssps; +}; + +&hpm1 { + apple,sleep-ssps; +}; + +&hpm2 { + apple,sleep-ssps; +}; + +&hpm5 { + apple,sleep-ssps; +}; + &aop_audio { apple,chassis-name = "J314"; apple,machine-kind = "MacBook Pro"; From 65b5b3ffad7b26d82768191e7f06c0f856068cfa Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Sat, 28 Nov 2026 16:49:07 +0000 Subject: [PATCH 26/33] soc: apple: sleep: make SEP's stop the last refusal where it comes first With .enclave_first, SEP is asked to sleep right after the other cores are held, and ops->display_off follows. Neither can be taken back, but the coprocessors' last checks, the SMC scope check, the SoC's final check and the pending-wakeup check still came after them. A sleep refused there returned with SEP asleep and, on T6000, with the display and GPU domains down, and the resume that followed hung. Run every other check first. SEP's request is then the last refusal, as it is without .enclave_first, and ops->display_off runs past it, where a failure can no longer be returned. On J314s a sleep refused at the last check hung the machine before this, and returns to a running system with it. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/soc/apple/sleep.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 3b19f4f32f498..c59159ae3ca3f 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -846,8 +846,10 @@ struct apple_sleep_soc { /* The SMC gets pmVC and WKTP before slpa (gen 1, as t6000 has them) */ bool smc_gen1_keys; /* - * SEP's sleep request first, right after the other cores are held: on T6000 the - * display's lowering waits for SEP's power gate. ops->before_enclave runs before it. + * SEP's sleep request before ops->display_off: on T6000 the display's lowering waits + * for SEP's power gate. Neither can be taken back, so SEP's request is the last + * refusal, after every other check, and ops->display_off runs past it. + * ops->before_enclave runs before the request. */ bool enclave_first; /* ops->commit_regs stops the parked cores itself, in place of the stop ahead of it. */ @@ -6380,13 +6382,7 @@ static int apple_sleep_enter_prepare(struct apple_sleep *s) if (apple_sleep_dry(s)) return apple_sleep_enter_dry(s); ret = apple_sleep_cpus_hold(s); - if (!ret && s->soc->enclave_first) { - step = "enclave stop"; - if (s->soc->ops->before_enclave) - s->soc->ops->before_enclave(s); - ret = apple_sleep_enclave_stop(s); - } - if (!ret) { + if (!ret && !s->soc->enclave_first) { step = "display prepare"; if (s->soc->regions) s->regs_staged = true; @@ -6428,7 +6424,9 @@ static int apple_sleep_enter_prepare(struct apple_sleep *s) ret = apple_sleep_enclave_stop(s); if (ret) panic("Apple SEP refused after the always-on GPU stop: %d", ret); - } else if (!ret && !s->soc->enclave_first) { + } else if (!ret) { + if (s->soc->enclave_first && s->soc->ops->before_enclave) + s->soc->ops->before_enclave(s); ret = apple_sleep_enclave_stop(s); } if (ret) { @@ -6440,6 +6438,11 @@ static int apple_sleep_enter_prepare(struct apple_sleep *s) /* SEP success belongs to irreversible commitment, never cancellation. */ s->entry_committed = true; /* No returning error path below this point. */ + if (s->soc->enclave_first) { + ret = s->soc->ops->display_off(s); + if (ret) + panic("Apple display power-down after the SEP stop failed: %d", ret); + } if (!s->soc->ops->display_stop) apple_sleep_coprocs_stop(s, true); if (!s->soc->own_wake) { From b7005a921009fb7a67821c94168cadd2795b7055 Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Sat, 28 Nov 2026 16:49:08 +0000 Subject: [PATCH 27/33] dmaengine: apple-sio: leave the T6000 firmware to the sleep's commit T6000 matched the generic data, so its firmware was idled and stopped in the late suspend. A firmware-driven sleep can still be refused after that, and an idled firmware does not come back with its power kept: the sleep engine has no reset for it on T6000 (-ENODEV), so it stayed stopped until a sleep that happened. Give "apple,t6000-sio" the generic words with .stop_at_commit. Where the board has a sleep controller the firmware then runs until the sleep's commit idles it, past the last refusal, as on T6041 and T8140. T6001 and T6002 share the compatible and have no firmware-driven sleep, so nothing changes for them. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/dma/apple-sio.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/drivers/dma/apple-sio.c b/drivers/dma/apple-sio.c index a1800bfe747d2..f1cebb167c6c7 100644 --- a/drivers/dma/apple-sio.c +++ b/drivers/dma/apple-sio.c @@ -643,6 +643,16 @@ static const struct sio_hw_data sio_hw_generic = { .cfg_word4 = 0x800, }; +/* + * The generic words, and stopped by the sleep's commit where the board has a sleep + * controller: idled any earlier, a sleep refused after that would leave it stopped. + */ +static const struct sio_hw_data sio_hw_t6000 = { + .stop_at_commit = true, + .fifo_threshold = 0x800, + .cfg_word4 = 0x800, +}; + static const struct sio_hw_data sio_hw_t6030 = { .fifo_threshold = 0x600, .cfg_word4 = 0x800, @@ -719,6 +729,7 @@ static const struct of_device_id sio_of_match[] = { */ { .compatible = "apple,t8132-sio", .data = &sio_hw_t8140 }, { .compatible = "apple,t6030-sio", .data = &sio_hw_t6030 }, + { .compatible = "apple,t6000-sio", .data = &sio_hw_t6000 }, { .compatible = "apple,sio", .data = &sio_hw_generic }, { } }; From 740f32f7df6359125c54eedc124de1a83ca47dcc Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Sat, 28 Nov 2026 16:49:34 +0000 Subject: [PATCH 28/33] drm/asahi: park the T6000 GPU firmware at the sleep's commit The legacy GPU's .suspend parked the firmware and stopped its CPU. A firmware-driven sleep can still be refused after the devices have suspended, and a parked firmware only comes back through a sleep that takes its power. After a refused sleep the GPU stayed down, and every later sleep failed in .suspend, which tried to park it again ("AP idle not acknowledged: -62"), so it never came back. Leave the stop to the sleep's commit instead. .suspend drains what is in flight and registers a first-pass stop with the sleep core, which runs it with interrupts off once nothing can refuse any more: apple_rtkit_sleep_idle_polled(), the mailbox's receive off, and the run bit cleared twice, as apple_rtkit_sleep_stop() does. The firmware runs until then, so the GPU's domain is held up across the noirq phase as it is across a suspend-to-idle. On an .enclave_first SoC ops->display_off now runs after the first-pass stops: T6000's takes the GPU's domain down with the display's. .resume restarts a firmware the commit parked after a sleep that happened. After a sleep refused before the commit there is nothing to do: the firmware never stopped. One parked by a commit whose sleep did not happen stays parked, and the next sleep neither parks it again nor refuses, and restarts it at its wake. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/gpu/drm/asahi/driver.rs | 8 +- drivers/gpu/drm/asahi/gpu.rs | 183 +++++++++++++++++++++++++++++-- drivers/gpu/drm/asahi/sleep_pd.c | 17 ++- drivers/gpu/drm/asahi/sleep_pd.h | 1 + drivers/soc/apple/sleep.c | 5 +- 5 files changed, 203 insertions(+), 11 deletions(-) diff --git a/drivers/gpu/drm/asahi/driver.rs b/drivers/gpu/drm/asahi/driver.rs index b31e20b21c0ad..c38a5b35d913b 100644 --- a/drivers/gpu/drm/asahi/driver.rs +++ b/drivers/gpu/drm/asahi/driver.rs @@ -342,7 +342,10 @@ unsafe extern "C" fn sleep_suspend(dev: *mut bindings::device) -> core::ffi::c_i let data = unsafe { sleep_data(dev) }; match legacy_sleep(data) { Some((gpu, control)) => { - kernel::error::from_result(|| gpu.sleep_suspend(control).map(|_| 0)) + let ret = kernel::error::from_result(|| gpu.sleep_suspend(control).map(|_| 0)); + // SAFETY: Probe attached the hold state to this device. + unsafe { asahi_sleep_pd_hold_deep(dev.cast(), ret == 0 && gpu.sleep_commit_pending()) }; + ret } None => 0, } @@ -354,6 +357,8 @@ unsafe extern "C" fn sleep_resume(dev: *mut bindings::device) -> core::ffi::c_in let data = unsafe { sleep_data(dev) }; if let Some((gpu, control)) = legacy_sleep(data) { gpu.sleep_resume(control); + // SAFETY: As in `sleep_suspend()`. + unsafe { asahi_sleep_pd_hold_deep(dev.cast(), false) }; } 0 } @@ -362,6 +367,7 @@ extern "C" { fn asahi_sleep_pd_init(dev: *mut core::ffi::c_void) -> core::ffi::c_int; fn asahi_sleep_pd_suspend_noirq(dev: *mut core::ffi::c_void) -> core::ffi::c_int; fn asahi_sleep_pd_resume_noirq(dev: *mut core::ffi::c_void) -> core::ffi::c_int; + fn asahi_sleep_pd_hold_deep(dev: *mut core::ffi::c_void, hold: bool); } /// Allocates per-device suspend-to-idle power-domain hold state tied to `dev`'s binding. diff --git a/drivers/gpu/drm/asahi/gpu.rs b/drivers/gpu/drm/asahi/gpu.rs index 4aaabcea9988d..e1c6b34d3ec65 100644 --- a/drivers/gpu/drm/asahi/gpu.rs +++ b/drivers/gpu/drm/asahi/gpu.rs @@ -16,6 +16,7 @@ use core::ops::Range; use core::slice; use core::sync::atomic::{ AtomicBool, + AtomicPtr, AtomicU32, AtomicU64, Ordering, // @@ -164,6 +165,103 @@ pub(crate) const IOVA_KERN_GPU_BUFMGR_HIGH: u64 = 0xffffffaeffff0000; /// Timeout for entering the halt state after a fault or request. const HALT_ENTER_TIMEOUT: Delta = Delta::from_millis(100); + +/// The run bit of an ASC's CPU control word. +const SLEEP_ASC_RUN: u32 = 0x10; + +/// What parks the firmware at the commit of a firmware-driven sleep. +pub(crate) struct SleepCommit { + rtk: *mut kernel::bindings::apple_rtkit, + control: *mut core::ffi::c_void, + ran: bool, + ret: core::ffi::c_int, +} + +#[cfg(CONFIG_APPLE_SOC_SLEEP)] +#[allow(improper_ctypes)] +unsafe extern "C" { + fn apple_sleep_coproc_register_first( + dev: *mut kernel::bindings::device, + last_stop: Option core::ffi::c_int>, + data: *mut core::ffi::c_void, + ) -> core::ffi::c_int; + fn apple_sleep_coproc_set_ready(dev: *mut kernel::bindings::device, ready: bool); + fn apple_sleep_coproc_stopped(dev: *mut kernel::bindings::device) -> bool; + fn apple_sleep_coproc_unregister(dev: *mut kernel::bindings::device); +} + +/// Interrupts off, one CPU, every refusal behind it: what `apple_rtkit_sleep_stop()` does, +/// polled. It can't refuse: the sleep goes on whatever it returns. +#[cfg(CONFIG_APPLE_SOC_SLEEP)] +unsafe extern "C" fn sleep_commit_stop(data: *mut core::ffi::c_void) -> core::ffi::c_int { + // SAFETY: `data` is the `SleepCommit` registered with this callback, allocated until + // `sleep_commit_unregister()` has returned. + let c = unsafe { &mut *data.cast::() }; + let mut us = 0u32; + + c.ran = true; + // SAFETY: `rtk` and `control` belong to the bound GPU, which the PM core keeps bound + // across the sleep. + unsafe { + c.ret = kernel::bindings::apple_rtkit_sleep_idle_polled(c.rtk, c.control, &mut us); + kernel::bindings::apple_rtkit_mailbox_recv_off(c.rtk); + // Twice, each time from what it reads back. + for _ in 0..2 { + let ctl = kernel::bindings::readl(c.control); + kernel::bindings::writel(ctl & !SLEEP_ASC_RUN, c.control); + } + } + c.ret +} + +/// Leaves the firmware's stop to the commit of the sleep under way. +/// +/// # Safety +/// +/// `dev` must be the bound GPU device, and `data` must stay allocated until +/// [`sleep_commit_unregister`] has returned. +#[cfg(CONFIG_APPLE_SOC_SLEEP)] +unsafe fn sleep_commit_register( + dev: *mut kernel::bindings::device, + data: *mut SleepCommit, +) -> core::ffi::c_int { + // SAFETY: Per the caller. + let ret = + unsafe { apple_sleep_coproc_register_first(dev, Some(sleep_commit_stop), data.cast()) }; + if ret == 0 { + // SAFETY: Registered just above. + unsafe { apple_sleep_coproc_set_ready(dev, true) }; + } + ret +} + +/// Takes the stop back. Returns whether the commit ran it. +/// +/// # Safety +/// +/// `dev` must have been registered with [`sleep_commit_register`]. +#[cfg(CONFIG_APPLE_SOC_SLEEP)] +unsafe fn sleep_commit_unregister(dev: *mut kernel::bindings::device) -> bool { + // SAFETY: Per the caller. + let stopped = unsafe { apple_sleep_coproc_stopped(dev) }; + // SAFETY: Per the caller. + unsafe { apple_sleep_coproc_unregister(dev) }; + stopped +} + +#[cfg(not(CONFIG_APPLE_SOC_SLEEP))] +unsafe fn sleep_commit_register( + _dev: *mut kernel::bindings::device, + _data: *mut SleepCommit, +) -> core::ffi::c_int { + ENODEV.to_errno() +} + +#[cfg(not(CONFIG_APPLE_SOC_SLEEP))] +unsafe fn sleep_commit_unregister(_dev: *mut kernel::bindings::device) -> bool { + false +} + /// How long submissions in flight get to finish before a sleep, and how often they are polled. const SLEEP_DRAIN_MS: i64 = 500; const SLEEP_DRAIN_POLL_MS: i64 = 5; @@ -333,6 +431,8 @@ pub(crate) struct GpuManager { timestamp_range: Range, crashed: AtomicBool, sleep_parked: AtomicBool, + /// What the commit of the sleep under way parks the firmware with, null when none. + sleep_commit: AtomicPtr, /// Set once the over-limit firmware-private garbage was reported as held. garbage_held: AtomicBool, #[pin] @@ -443,7 +543,11 @@ pub(crate) trait Gpu: Send + Sync { fn sleep_suspend(&self, _asc_cpu_control: *mut core::ffi::c_void) -> Result { Err(EBUSY) } - /// Restarts a firmware [`Gpu::sleep_suspend`] parked, after a sleep that took its power. + /// Whether [`Gpu::sleep_suspend`] left the firmware running for the sleep's commit to park. + fn sleep_commit_pending(&self) -> bool { + false + } + /// Restarts a firmware the sleep parked, after a sleep that took its power. fn sleep_resume(&self, _asc_cpu_control: *mut core::ffi::c_void) {} /// Fill in the hardware description reported to userspace. fn get_params(&self, params: &mut uapi::drm_asahi_params_global) -> Result; @@ -1146,6 +1250,7 @@ impl GpuManager::ver { rtkit <- new_mutex!(None, "rtkit"), crashed: AtomicBool::new(false), sleep_parked: AtomicBool::new(false), + sleep_commit: AtomicPtr::new(core::ptr::null_mut()), garbage_held: AtomicBool::new(false), event_manager, stats: stats_snap.clone(), @@ -1755,6 +1860,11 @@ impl Gpu for GpuManager::ver { return Err(EBUSY); } + // Parked for a sleep that was then refused: still down, and this sleep restarts it. + if self.sleep_parked.load(Ordering::Acquire) { + return Ok(()); + } + // User space is frozen, so nothing new is submitted: what the firmware still has in // flight gets SLEEP_DRAIN_MS to finish, then the sleep is refused. let mut pending = 0u32; @@ -1777,16 +1887,74 @@ impl Gpu for GpuManager::ver { return Err(EBUSY); } - let mut guard = self.rtkit.lock(); - let rtk = guard.as_mut().as_pin_mut().ok_or(ENODEV)?; - // SAFETY: The driver passes the CPU control word of this GPU's mapped ASC block. - unsafe { rtk.sleep_stop(asc_cpu_control) }?; - self.sleep_parked.store(true, Ordering::Release); + // The sleep's commit parks the firmware, past the last refusal: parked here, it would + // stay down after a sleep that is then refused. + let rtk = { + let guard = self.rtkit.lock(); + (*guard).as_ref().ok_or(ENODEV)?.as_raw() + }; + let commit = KBox::new( + SleepCommit { + rtk, + control: asc_cpu_control, + ran: false, + ret: 0, + }, + GFP_KERNEL, + )?; + let data = KBox::into_raw(commit); + let pdev: &kernel::platform::Device = self.dev.as_ref(); + let dev: &kernel::device::Device = pdev.as_ref(); + let dev = dev.as_raw(); + // SAFETY: `dev` is this GPU's bound device; `data` stays allocated until + // `sleep_resume()` has unregistered the callback. + let ret = unsafe { sleep_commit_register(dev, data) }; + if ret != 0 { + // SAFETY: Not registered, so nothing else holds `data`. + drop(unsafe { KBox::from_raw(data) }); + dev_warn!( + self.dev.as_ref(), + "no stop at the sleep's commit ({}): refusing the sleep\n", + ret + ); + return Err(Error::from_errno(ret)); + } + self.sleep_commit.store(data, Ordering::Release); Ok(()) } + fn sleep_commit_pending(&self) -> bool { + !self.sleep_commit.load(Ordering::Acquire).is_null() + } + fn sleep_resume(&self, asc_cpu_control: *mut core::ffi::c_void) { - if !self.sleep_parked.swap(false, Ordering::AcqRel) || asc_cpu_control.is_null() { + let data = self.sleep_commit.swap(core::ptr::null_mut(), Ordering::AcqRel); + if !data.is_null() { + let pdev: &kernel::platform::Device = self.dev.as_ref(); + let dev: &kernel::device::Device = pdev.as_ref(); + let dev = dev.as_raw(); + // SAFETY: Registered in `sleep_suspend()`; the callback can no longer run. + let stopped = unsafe { sleep_commit_unregister(dev) }; + // SAFETY: Allocated in `sleep_suspend()` and no longer registered. + let commit = unsafe { KBox::from_raw(data) }; + // SAFETY: Reads a global flag word. + if !stopped && !commit.ran && !unsafe { kernel::bindings::pm_resume_via_firmware() } { + dev_info!( + self.dev.as_ref(), + "sleep refused before the commit: firmware never stopped\n" + ); + return; + } + if commit.ret != 0 { + dev_warn!( + self.dev.as_ref(), + "firmware did not idle at the sleep's commit: {}\n", + commit.ret + ); + } + self.sleep_parked.store(true, Ordering::Release); + } + if !self.sleep_parked.load(Ordering::Acquire) || asc_cpu_control.is_null() { return; } // SAFETY: Reads a global flag word. @@ -1797,6 +1965,7 @@ impl Gpu for GpuManager::ver { ); return; } + self.sleep_parked.store(false, Ordering::Release); let ret = { let mut guard = self.rtkit.lock(); diff --git a/drivers/gpu/drm/asahi/sleep_pd.c b/drivers/gpu/drm/asahi/sleep_pd.c index f3612df8c6f91..007ef6e1763ad 100644 --- a/drivers/gpu/drm/asahi/sleep_pd.c +++ b/drivers/gpu/drm/asahi/sleep_pd.c @@ -27,6 +27,8 @@ struct asahi_sleep_pd { struct notifier_block nb; bool active; + /* The firmware runs until the commit of a firmware-driven sleep stops it. */ + bool deep; }; /* IRQ-safe domains invoke power notifiers in atomic context. */ @@ -66,7 +68,8 @@ int asahi_sleep_pd_suspend_noirq(struct device *dev) struct asahi_sleep_pd *hold; int ret; - if (pm_suspend_via_firmware()) + hold = devres_find(dev, asahi_sleep_pd_release, NULL, NULL); + if (pm_suspend_via_firmware() && !(hold && hold->deep)) return 0; /* @@ -111,6 +114,18 @@ int asahi_sleep_pd_suspend_noirq(struct device *dev) return 0; } +/* + * A firmware-driven sleep whose commit stops the firmware, past the last refusal: the + * domain stays up under it until then, as across a suspend-to-idle. + */ +void asahi_sleep_pd_hold_deep(struct device *dev, bool hold_deep) +{ + struct asahi_sleep_pd *hold = devres_find(dev, asahi_sleep_pd_release, NULL, NULL); + + if (hold) + hold->deep = hold_deep; +} + int asahi_sleep_pd_resume_noirq(struct device *dev) { struct asahi_sleep_pd *hold; diff --git a/drivers/gpu/drm/asahi/sleep_pd.h b/drivers/gpu/drm/asahi/sleep_pd.h index 63fdcd662455f..a4790e53febed 100644 --- a/drivers/gpu/drm/asahi/sleep_pd.h +++ b/drivers/gpu/drm/asahi/sleep_pd.h @@ -8,5 +8,6 @@ struct device; int asahi_sleep_pd_init(struct device *dev); int asahi_sleep_pd_suspend_noirq(struct device *dev); int asahi_sleep_pd_resume_noirq(struct device *dev); +void asahi_sleep_pd_hold_deep(struct device *dev, bool hold); #endif diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index c59159ae3ca3f..a950fc9c5535d 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -6438,13 +6438,14 @@ static int apple_sleep_enter_prepare(struct apple_sleep *s) /* SEP success belongs to irreversible commitment, never cancellation. */ s->entry_committed = true; /* No returning error path below this point. */ + if (!s->soc->ops->display_stop) + apple_sleep_coprocs_stop(s, true); + /* After the first stops: T6000's takes the GPU's domain down with the display's. */ if (s->soc->enclave_first) { ret = s->soc->ops->display_off(s); if (ret) panic("Apple display power-down after the SEP stop failed: %d", ret); } - if (!s->soc->ops->display_stop) - apple_sleep_coprocs_stop(s, true); if (!s->soc->own_wake) { sleep_wake_arm(s); s->wake_masks_saved = true; From 371bf9ea6bc1d8f24ced3521c3596ba373446191 Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Sat, 28 Nov 2026 16:49:09 +0000 Subject: [PATCH 29/33] soc: apple: sleep: t6000: drop the fixed waits of the late commit The late commit waited 2 ms after two power-state writes, 2 ms after the fabric's sleep state, 100 us after the clock muxes went off, and 200 ms before the wake timer was written. None of them waits for anything: the fabric state is already polled for, and J314s sleeps and wakes the same without the others (25 of 25). Drop the two 2 ms waits and the 200 ms one. Poll each clock mux for its busy bit in place of the 100 us, and report one that stays busy with the other incomplete steps. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/soc/apple/sleep-t6000.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/drivers/soc/apple/sleep-t6000.h b/drivers/soc/apple/sleep-t6000.h index d7ec8c23f68f7..598236fff6c23 100644 --- a/drivers/soc/apple/sleep-t6000.h +++ b/drivers/soc/apple/sleep-t6000.h @@ -482,9 +482,6 @@ static const struct { { T6000_PS_EAST, T6000_PS_EAST_ANS2, T6000_PS_WORD(true, 0xf, PS_FLAGS, 0xf) }, }; -/* How long the late commit writes get to take effect before the timer is written */ -#define T6000_LATE_SETTLE_MS 200 - /* * Every PMGR power domain of the device tree: the sleep cuts their power while genpd counts * them on, so each is raised again at the wake, parents first. Not SEP's, afc or afi. @@ -503,6 +500,7 @@ static const struct { #define T6000_INCOMPLETE_DCS_KEPT BIT(3) #define T6000_INCOMPLETE_DCS_BUSY BIT(4) #define T6000_INCOMPLETE_PLL_KEPT BIT(5) +#define T6000_INCOMPLETE_LATE_MUX BIT(6) static const char *const t6000_incomplete_names[] = { "SEP not power-gated", @@ -511,6 +509,7 @@ static const char *const t6000_incomplete_names[] = { "memory clock state not lowered", "memory clock state not reached", "clock mux busy and PLL0 left on", + "late clock mux busy", }; static struct t6000_domain { @@ -1417,7 +1416,6 @@ static void t6000_commit_late_state(struct apple_sleep *s) writel(T6000_PS_WORD(true, 0xf, 0, 0xf), t->ps_east + T6000_PS_EAST_AFNC2_LW1); writel(T6000_PS_WORD(true, 0xf, 0, 0xf), t->ps_east + T6000_PS_EAST_SIO); - udelay(2000); for (i = 0; i < ARRAY_SIZE(t6000_aon_commit_off); i++) writel(t6000_aon_commit_off[i].value, @@ -1441,7 +1439,6 @@ static void t6000_commit_late_state(struct apple_sleep *s) writel(SOC_SLEEP_STATE, t->fabric_ps); sleep_perf_wait(t->fabric_ps, SOC_SLEEP_STATE); } - udelay(2000); writel(T6000_PS_WORD(false, 0xf, 0, 0xf), t->ps_east + T6000_PS_EAST_SIO_CPU); if (!readl_poll_timeout_atomic(t->ps_east + T6000_PS_EAST_SIO_CPU, val, @@ -1454,9 +1451,13 @@ static void t6000_commit_late_state(struct apple_sleep *s) writel(0, s->ps + T6000_PS_PCIE0_REF); writel(0, s->ps + T6000_PS_APCIE_GP_SYS); - for (i = 0; i < ARRAY_SIZE(t6000_clkgen_off_mux); i++) - writel(0, t->clkgen + t6000_clkgen_off_mux[i]); - udelay(100); + for (i = 0; i < ARRAY_SIZE(t6000_clkgen_off_mux); i++) { + void __iomem *reg = t->clkgen + t6000_clkgen_off_mux[i]; + + writel(0, reg); + if (readl_poll_timeout_atomic(reg, val, !(val & T6000_CLKGEN_MUX_BUSY), 1, 100)) + t->incomplete |= T6000_INCOMPLETE_LATE_MUX; + } } static void t6000_sleep_commit_regs(struct apple_sleep *s) @@ -1631,7 +1632,6 @@ static void t6000_sleep_commit_regs(struct apple_sleep *s) /* 10. Final PMGR, clock, fabric p-state and SIO state for the S2R sequencer */ t6000_commit_late_state(s); dsb(sy); - mdelay(T6000_LATE_SETTLE_MS); /* 11. The PMU RTC alarm's SPMI vector: its latched status cleared, then enabled */ for (i = 0; i < T6000_SPMI_VEC_WORDS; i++) From 836ec8b92758eed84236cc2ce85033ef8beb7f30 Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Sat, 28 Nov 2026 19:27:16 +0000 Subject: [PATCH 30/33] soc: apple: sleep: t6000: add a power cycle of the display CPU domain After a refused sleep the display coprocessor's firmware has idled with its power kept, and it only starts again from a power loss. The words that take its CPU domain down are in the sleep's entry, and nothing could run them outside a sleep. Add apple_sleep_display_power_cycle() for the coprocessor's driver. On T6000 it lowers the CPU domain's word with the two writes of the entry and raises it to the word it held, in the wake's order. The front end stays on, and the entry is not changed. The coprocessor's request to the PMP is withdrawn around the cycle. A running PMP takes a block whose request it holds as active, the entry withdraws the request before it lowers the word, and the request's power domain, a child of the CPU domain's, keeps that order everywhere else. Nothing in the report's driver raises or reports a domain lowered under a held request, so only the withdrawal keeps the PMP's view true. On J314s, with the front end on, the word read 0x1f0403ff before, 0x0f040100 off and 0x1f0403ff again in under 1 ms, with the PMP's requests at 0x7200 and acknowledged before it. The firmware then started in about 2 ms, 5 of 5 times with the request withdrawn, and in about 3 ms the one time it was held. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/soc/apple/sleep-t6000.h | 74 +++++++++++++++++++++++++++++++++ drivers/soc/apple/sleep.c | 35 ++++++++++++++++ include/linux/soc/apple/sleep.h | 3 ++ 3 files changed, 112 insertions(+) diff --git a/drivers/soc/apple/sleep-t6000.h b/drivers/soc/apple/sleep-t6000.h index 598236fff6c23..984daa570badb 100644 --- a/drivers/soc/apple/sleep-t6000.h +++ b/drivers/soc/apple/sleep-t6000.h @@ -370,6 +370,19 @@ static const struct t6000_ps_word t6000_disp_ps[6] = { { T6000_PS, T6000_PS_DISPEXT1_FE }, }; +/* disp0's CPU word, the internal display coprocessor's: entry 0 of apple,display-coprocessors */ +#define T6000_DISP0_CPU 2 +/* + * A display coprocessor's CPU word on its way off, the two words t6000_sleep_display_off() + * writes: AUTO_ENABLE off at full power, with a minimum state of clock gating, then the + * target off and the status flags written. + */ +#define T6000_DISP_CPU_WORD_MANUAL T6000_PS_WORD_MIN(false, 0xf, 4, 0, 0xf) +#define T6000_DISP_CPU_WORD_OFF T6000_PS_WORD_MIN(false, 0xf, 4, PS_FLAGS, 0) +/* What that word gets to read off, as at the entry, and full power again, as at the wake */ +#define T6000_DISP_OFF_TIMEOUT_US 10000 +#define T6000_DISP_ON_TIMEOUT_US 20000 + /* The order macOS lowers them in: disp0, dispext0, dispext1, each CPU before its front end */ static const u8 t6000_disp_order[6] = { 2, 3, 0, 1, 4, 5 }; @@ -1215,6 +1228,66 @@ static int t6000_sleep_display_off(struct apple_sleep *s) return 0; } +/* + * The internal display coprocessor's CPU domain off and on again with the system running, + * for apple_sleep_display_power_cycle(). Its word goes off as the entry writes it and comes + * back to the word it holds now, in the wake's order: the target with AUTO_ENABLE off until + * it reads full power, then AUTO_ENABLE. The front end stays on. + * + * The coprocessor's request to the PMP is withdrawn meanwhile, as at the entry: the PMP + * takes a block whose request it holds as running. It goes through the report's driver, + * which serializes it with the requests other devices make at this point. + * + * Returns -EIO if the domain is not on at the end: what is behind it may have no power. Any + * other error leaves it on. + */ +static int t6000_display_cycle(struct apple_sleep *s, unsigned int display) +{ + /* Its power domain where that is its report entry: any other node has no request. */ + struct device_node *request __free(device_node) = + of_parse_phandle(s->display[display].np, "power-domains", 0); + void __iomem *reg = t6000_ps_reg(s, t6000_disp_ps[T6000_DISP0_CPU]); + u32 was, val; + int ret, back; + + if (display) + return -EOPNOTSUPP; + + was = readl(reg); + /* Not on to begin with: nothing to cycle. */ + if (FIELD_GET(PS_TARGET, was) != FIELD_MAX(PS_TARGET)) + return -EIO; + + ret = request ? apple_pmp_report_entry_request(request, false) : 0; + if (ret) { + apple_pmp_report_entry_request(request, true); + return ret; + } + + writel(T6000_DISP_CPU_WORD_MANUAL, reg); + writel(T6000_DISP_CPU_WORD_OFF, reg); + ret = readl_poll_timeout_atomic(reg, val, !FIELD_GET(PS_ACTUAL, val), 1, + T6000_DISP_OFF_TIMEOUT_US); + + writel(was & ~(T6000_PS_LATCHES | PS_ACTUAL | PS_AUTO_ENABLE), reg); + back = readl_poll_timeout_atomic(reg, val, + FIELD_GET(PS_ACTUAL, val) == FIELD_GET(PS_TARGET, was), + 10, T6000_DISP_ON_TIMEOUT_US); + if (back) { + dev_err(s->dev, "T6000: display coprocessor power word not back on: %#x (was %#x)\n", + val, was); + return -EIO; + } + if (was & PS_AUTO_ENABLE) + writel(was & ~(T6000_PS_LATCHES | PS_ACTUAL), reg); + + back = request ? apple_pmp_report_entry_request(request, true) : 0; + if (back) + dev_warn(s->dev, "T6000: the PMP did not take the display's request again: %d\n", + back); + return ret ?: back; +} + static int t6000_sleep_final_check(struct apple_sleep *s) { t6000_wdt_arm(s, 90); @@ -1908,6 +1981,7 @@ static const struct apple_sleep_ops t6000_sleep_ops = { .begin = t6000_sleep_begin, .before_enclave = t6000_before_enclave, .display_off = t6000_sleep_display_off, + .display_cycle = t6000_display_cycle, .final_check = t6000_sleep_final_check, .prepare_regs = t6000_sleep_prepare_regs, .undo_regs = t6000_sleep_undo_regs, diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index a950fc9c5535d..12ecf85b10e8d 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -624,6 +624,8 @@ struct apple_sleep_ops { void (*prepare_regs)(struct apple_sleep *s); void (*undo_regs)(struct apple_sleep *s); int (*display_off)(struct apple_sleep *s); + /* Optional: apple_sleep_display_power_cycle() for display coprocessor @display */ + int (*display_cycle)(struct apple_sleep *s, unsigned int display); int (*display_stop)(struct apple_sleep *s); /* After the always-on GPU stop, right before SEP's sleep request. */ void (*before_enclave)(struct apple_sleep *s); @@ -2182,6 +2184,39 @@ void apple_sleep_display_quiesced(struct device *dev, bool quiesced) } EXPORT_SYMBOL_GPL(apple_sleep_display_quiesced); +/** + * apple_sleep_display_power_cycle() - power a display coprocessor's CPU domain off and on again + * @dev: the coprocessor, one of the sleep controller's apple,display-coprocessors + * + * For its driver's resume after a refused sleep, when the firmware it stopped for the sleep + * only starts again from the power loss a real sleep gives it. The domain goes off as the + * sleep's entry takes it and comes back as it was. The caller has stopped the coprocessor's + * CPU and keeps the coprocessor's registers from being accessed until this returns. + * + * Context: process context, with the system's devices resuming or resumed. + * Return: 0 with the domain power-cycled and on. -ENODEV with no sleep controller, or for a + * device that is not one of its display coprocessors, and -EOPNOTSUPP where the SoC has no + * such sequence: nothing was written. -EIO if the domain is not on: the coprocessor's + * registers may have no power and must not be accessed. Any other error: the cycle did not + * complete, and the domain is on. + */ +int apple_sleep_display_power_cycle(struct device *dev) +{ + struct apple_sleep *s = apple_sleep; + unsigned int i; + + if (!s) + return -ENODEV; + if (!s->soc->ops->display_cycle) + return -EOPNOTSUPP; + for (i = 0; i < s->soc->ndisp; i++) + if (s->display[i].np == dev->of_node) + return s->soc->ops->display_cycle(s, i); + + return -ENODEV; +} +EXPORT_SYMBOL_GPL(apple_sleep_display_power_cycle); + /* * MPIDR's cluster is affinity 2, its core affinity 0, as 0.0.n and 1.1.n on T8140, unless the * SoC names another level for its clusters. diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index 9b7d29e3ef8d9..d13f7b9cc515d 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -78,6 +78,8 @@ int apple_sleep_smc_register_saved(struct device *dev, void apple_sleep_smc_unregister(struct device *dev); /* Driver attests firmware idle AND its DART/GAPF release, never a generic stop request. */ void apple_sleep_display_quiesced(struct device *dev, bool quiesced); +/* A display coprocessor's CPU domain off and on again, for its restart after a refused sleep. */ +int apple_sleep_display_power_cycle(struct device *dev); /* Read-only handoff: provider restored domains/gates, prior errors refuse. */ int apple_sleep_display_v27_power_ready(struct device *dev); /* T6050: the internal display's power words around its coprocessor's restart after a wake. */ @@ -321,6 +323,7 @@ static inline int apple_sleep_smc_register_saved(struct device *dev, const struct apple_sleep_smc_saved_ops *ops, void *data) { return -EOPNOTSUPP; } static inline void apple_sleep_smc_unregister(struct device *dev) {} static inline void apple_sleep_display_quiesced(struct device *dev, bool quiesced) {} +static inline int apple_sleep_display_power_cycle(struct device *dev) { return -EOPNOTSUPP; } static inline int apple_sleep_coproc_register(struct device *dev, void __iomem *asc, bool pcie_ans, int (*last_stop)(void *data), void *data) From 8a52a02a064b47887b80506a171e9ffa41c9fcf5 Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Sat, 28 Nov 2026 18:37:49 +0000 Subject: [PATCH 31/33] drm/apple: iomfb: drop unanswered calls before a firmware restart Without the queue, a call the firmware never answers stays on its channel. After a restart every later command then goes out on the callback context, and an ack can be paired with a call slot that holds no record: dcpep_handle_ack() derives the record's address from a NULL output pointer and reads it. Seen once on T6000, after a restart that followed calls to a stopped firmware: an Oops in iomfb_recv_message() at 0xfffffffffffffff8, the in_len of a record 12 bytes below NULL. Add iomfb_channels_reset() to empty the call stacks before such a firmware starts again. An ack for an empty slot is ignored with a warning, and one on a call stack deeper than its slots marks the firmware crashed. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/gpu/drm/apple/dcp-internal.h | 1 + drivers/gpu/drm/apple/iomfb.c | 48 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index e20ac33694738..e905a295dfcd7 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -1416,6 +1416,7 @@ int iomfb_queue_callback(struct apple_dcp *dcp, dcp_callback_t callback, void iomfb_queue_init(struct apple_dcp *dcp); void iomfb_queue_stop(struct apple_dcp *dcp); void iomfb_reset_transport(struct apple_dcp *dcp); +void iomfb_channels_reset(struct apple_dcp *dcp); bool iomfb_queue_drain(struct apple_dcp *dcp, unsigned long timeout); int iomfb_queue_settle(struct apple_dcp *dcp, unsigned long timeout); void iomfb_pause_presentation(struct apple_dcp *dcp, bool pause); diff --git a/drivers/gpu/drm/apple/iomfb.c b/drivers/gpu/drm/apple/iomfb.c index cfb3f4f984724..60f7227a97896 100644 --- a/drivers/gpu/drm/apple/iomfb.c +++ b/drivers/gpu/drm/apple/iomfb.c @@ -732,6 +732,40 @@ void iomfb_reset_transport(struct apple_dcp *dcp) mutex_unlock(&dcp->iomfb.lock); } +/* + * The call stacks of a firmware without the queue, emptied before it starts again, with its + * mailbox off. A call the last session left unanswered would stay on its channel: every + * command after it then goes out on the callback context (dcp_call_context()), and an ack + * can be paired with a slot whose record is gone. Its callback is not run, as that may send + * the next call of a sequence: what waited for it has timed out, and its cookie is not + * released. + */ +void iomfb_channels_reset(struct apple_dcp *dcp) +{ + struct dcp_channel *channels[] = { + &dcp->ch_cmd, &dcp->ch_oobcmd, &dcp->ch_cb, + &dcp->ch_oobcb, &dcp->ch_async, &dcp->ch_oobasync, + }; + unsigned int i, dropped = 0; + + if (iomfb_uses_queue(dcp)) + return; + + for (i = 0; i < ARRAY_SIZE(channels); i++) { + struct dcp_channel *ch = channels[i]; + + dropped += ch->depth; + memset(ch->callbacks, 0, sizeof(ch->callbacks)); + memset(ch->cookies, 0, sizeof(ch->cookies)); + memset(ch->output, 0, sizeof(ch->output)); + WRITE_ONCE(ch->depth, 0); + ch->warned_busy = false; + } + if (dropped) + dev_warn(dcp->dev, "%u calls the firmware never answered dropped at its restart\n", + dropped); +} + void iomfb_queue_stop(struct apple_dcp *dcp) { struct iomfb_callback *item; @@ -2250,6 +2284,13 @@ static void dcpep_handle_ack(struct apple_dcp *dcp, enum dcp_context_id context) dev_warn(dcp->dev, "ignoring ack on idle context %X\n", context); return; } + /* Its slot would be past the call stack. */ + if (ch->depth > DCP_MAX_CALL_DEPTH) { + dev_err(dcp->dev, "call stack of context %X is %u deep: ack dropped\n", + context, ch->depth); + WRITE_ONCE(dcp->crashed, true); + return; + } dcp_pop_depth(&ch->depth); @@ -2262,6 +2303,13 @@ static void dcpep_handle_ack(struct apple_dcp *dcp, enum dcp_context_id context) ch->cookies[ch->depth] = NULL; ch->output[ch->depth] = NULL; + /* dcp_push() saves a record's address with every command: this slot holds none. */ + if (!out) { + dev_warn(dcp->dev, "ignoring ack for an empty call slot on context %X\n", + context); + return; + } + /* * An ack completes the innermost pending command on its context. The * firmware writes the reply in place into that AP command record, whose From ed7bf13600f2e97caf60e2bebbc295d2ddbe536e Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Sat, 28 Nov 2026 19:28:06 +0000 Subject: [PATCH 32/33] drm/apple: dcp: restart the 13.5 firmware after a refused sleep A sleep refused after the T6000 display coprocessor's stop left the panel dark until a reboot. Firmware 13.5 does not start from an idle with its power kept, and after A500 it completes no page flip even if it is never stopped. The resume cleared s2r_fw_down all the same, and the calls that followed timed out against a firmware that was down. Give a panel's firmware the power loss a real sleep gives it: its CPU domain is power-cycled through the sleep driver, and the firmware starts as after a wake, with its call stacks emptied. Its DARTs are not touched. Both kept their lock word, their stream enables and all 16 translation register pairs across the cycle, and the display subsystem, which maps through them, resumes after the coprocessor. A firmware that does not start, after a refusal or a real sleep, is left stopped and marked crashed, so that nothing calls it. It stays down for the next suspend, and the next sleep that happens starts it again. On J314s, 4 of 4 refused sleeps, two at each of two points of the entry, came back from the resume with the panel lit: about 1 ms for the cycle, 2 ms for the firmware's start and 0.5 s for the whole resume. After 6 refusals in one boot, 10 deep sleeps passed. Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/gpu/drm/apple/dcp.c | 66 ++++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 670e4f3a2ea51..9b433d280e792 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -11337,8 +11337,14 @@ static bool dcp_sleep_13_5_stop(struct apple_dcp *dcp) if (!dcp->hw.sleep_13_5 || !pm_suspend_via_firmware() || apple_sleep_dry_run() || dcp->external || dcp->fw_compat != DCP_FIRMWARE_V_13_5 || - IS_ERR_OR_NULL(dcp->rtk) || !dcp->coproc_reg || READ_ONCE(dcp->crashed) || - !apple_rtkit_is_running(dcp->rtk)) + IS_ERR_OR_NULL(dcp->rtk) || !dcp->coproc_reg) + return false; + /* Not started since an earlier sleep stopped it: a sleep that happens starts it. */ + if (READ_ONCE(dcp->s2r_fw_down)) { + dcp->sleep_fw_runs = false; + return true; + } + if (READ_ONCE(dcp->crashed) || !apple_rtkit_is_running(dcp->rtk)) return false; ret = iomfb_s2r_500_v13_3(dcp); @@ -11570,30 +11576,66 @@ void dcp_hdmi_disconnect(struct apple_dcp *dcp) } /* - * hw.sleep_13_5: a real sleep took the firmware's memory, so it boots from scratch. A - * refused sleep left it parked with its power kept. + * hw.sleep_13_5: a real sleep took the firmware's power, and it starts as from a wake. A + * refused sleep left it parked with its power kept, after A500: it neither starts from that + * nor completes a page flip again. A panel's firmware is then given the power loss of a real + * sleep first, its CPU domain off and on again through the sleep driver. Its DARTs keep + * their registers across that, and nothing maps through them meanwhile: the display + * subsystem resumes after this device. + * + * A firmware that does not start is left stopped, as dcp_sleep_13_5_stop() left it, and + * marked crashed, so that nothing calls it: its display stays dark, and the next sleep that + * happens starts it again from here. Returns whether the firmware runs. */ -static void dcp_sleep_13_5_start(struct apple_dcp *dcp) +static bool dcp_sleep_13_5_start(struct apple_dcp *dcp) { void __iomem *control = dcp->coproc_reg + APPLE_DCP_COPROC_CPU_CONTROL; + bool real = pm_resume_via_firmware(); int ret; if (!dcp->hw.sleep_13_5 || dcp->external || dcp->fw_compat != DCP_FIRMWARE_V_13_5 || !READ_ONCE(dcp->s2r_fw_down)) - return; + return true; - WRITE_ONCE(dcp->s2r_fw_down, false); - if (pm_resume_via_firmware() && dcp_has_panel(dcp)) + if (dcp_has_panel(dcp) && !real) { + ret = apple_sleep_display_power_cycle(dcp->dev); + /* Not started, so still stopped: after -EIO its registers may have no power. */ + if (ret) { + dev_err(dcp->dev, "no power cycle after the refused sleep: %d; the display stays dark until %s\n", + ret, ret == -EIO ? "a reboot" : "a sleep that happens"); + WRITE_ONCE(dcp->crashed, true); + return false; + } + } + + /* No call of the last session is answered by this one. */ + iomfb_channels_reset(dcp); + if (dcp_has_panel(dcp)) ret = apple_rtkit_sleep_return_with_init(dcp->rtk, control); else ret = apple_rtkit_sleep_return(dcp->rtk, control); - if (ret) - dev_err(dcp->dev, "firmware did not start after the sleep: %d\n", ret); + if (ret) { + dev_err(dcp->dev, "firmware did not start after the %s: %d; the display stays dark until a sleep that happens\n", + real ? "sleep" : "refused sleep", ret); + WRITE_ONCE(dcp->crashed, true); + apple_rtkit_mailbox_off(dcp->rtk); + /* Twice, as apple_rtkit_sleep_stop() clears it. */ + writel(readl(control) & ~APPLE_DCP_COPROC_CPU_CONTROL_RUN, control); + writel(readl(control) & ~APPLE_DCP_COPROC_CPU_CONTROL_RUN, control); + return false; + } + WRITE_ONCE(dcp->crashed, false); + WRITE_ONCE(dcp->s2r_fw_down, false); + if (!real) + dev_info(dcp->dev, "sleep refused: firmware restarted after a power cycle\n"); + + return true; } static int dcp_platform_resume(struct device *dev) { struct apple_dcp *dcp = dev_get_drvdata(dev); + bool fw_runs; dcp->sleep_fw_runs = false; if (dcp_takeover_was_aborted(dcp)) @@ -11617,7 +11659,7 @@ static int dcp_platform_resume(struct device *dev) gpiod_set_value_cansleep(dcp->dp2hdmi_pwren, 1); } - dcp_sleep_13_5_start(dcp); + fw_runs = dcp_sleep_13_5_start(dcp); if (iomfb_is_m4(dcp)) { int ret = iomfb_m4_resume(dcp); @@ -11660,7 +11702,7 @@ static int dcp_platform_resume(struct device *dev) if (dcp->external_native) enable_work(&dcp->external_audio_work); - if (dcp->avep) + if (dcp->avep && fw_runs) av_service_connect(dcp); return 0; From 6c8d7e7cffac489b429764a401d7f62e94c39f13 Mon Sep 17 00:00:00 2001 From: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> Date: Sat, 28 Nov 2026 20:40:16 +0000 Subject: [PATCH 33/33] soc: apple: sleep: t6000: do not wait at the wake for domains that cannot follow t6000_domains_restore() raises every PMGR domain that was on before the sleep, waits up to 20 ms for each, and repeats the whole pass up to 30 times while any has not followed. On J314s ten never do, so every wake spent 6.5 s there: - nine are words of the cores and their managers (ecpu0_1, pcpu0_0-2, pcpu1_0-2, pcpm0, pcpm1). They read on with a target of 0: their power is their complex's, not this word's to ask for, and they come back when the cores are started; - one (afnc1_lw1) has AUTO_ENABLE and gates itself as soon as that is written back, which was before the check. The later passes then left it on with AUTO_ENABLE cleared. Skip a domain that was on with no target, and judge whether a domain followed before AUTO_ENABLE goes back. J314s, 25 deep sleeps: the step takes 60 ms (6522 ms before) in one pass with no domain left behind, and the resume ends 0.8 to 1.0 s after the first code that runs at the wake (7.2 to 7.5 s before). Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com> --- drivers/soc/apple/sleep-t6000.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/soc/apple/sleep-t6000.h b/drivers/soc/apple/sleep-t6000.h index 984daa570badb..7b560cefa0e23 100644 --- a/drivers/soc/apple/sleep-t6000.h +++ b/drivers/soc/apple/sleep-t6000.h @@ -745,6 +745,7 @@ static void t6000_domain_on(unsigned int i, unsigned int depth, unsigned long *s u32 saved = d->saved, cur; unsigned int n; int err, parent; + bool followed; if (test_and_set_bit(i, seen) || depth > 8) return; @@ -752,6 +753,9 @@ static void t6000_domain_on(unsigned int i, unsigned int depth, unsigned long *s return; if (!FIELD_GET(PS_ACTUAL, saved)) return; + /* On with no target: not this word's to ask for, as a core's, which its complex powers. */ + if (!FIELD_GET(PS_TARGET, saved)) + return; of_for_each_phandle(&it, err, d->np, "power-domains", "#power-domain-cells", 0) { parent = t6000_domain_find(it.node); if (parent >= 0) @@ -776,7 +780,10 @@ static void t6000_domain_on(unsigned int i, unsigned int depth, unsigned long *s for (n = 0; n < 2000 && FIELD_GET(PS_ACTUAL, readl(d->reg)) < FIELD_GET(PS_ACTUAL, saved); n++) udelay(10); - cur = readl(d->reg) & ~(T6000_PS_LATCHES | PS_ACTUAL); + /* Judged before AUTO_ENABLE goes back: with it, an idle domain gates itself at once. */ + cur = readl(d->reg); + followed = FIELD_GET(PS_ACTUAL, cur) >= FIELD_GET(PS_ACTUAL, saved); + cur &= ~(T6000_PS_LATCHES | PS_ACTUAL); if ((saved & PS_AUTO_ENABLE) && !t->domain_pass) writel(cur | PS_AUTO_ENABLE, d->reg); cur = readl(d->reg); @@ -785,7 +792,7 @@ static void t6000_domain_on(unsigned int i, unsigned int depth, unsigned long *s udelay(1); cur = readl(d->reg); } - if (FIELD_GET(PS_ACTUAL, cur) < FIELD_GET(PS_ACTUAL, saved)) + if (!followed) t->domains_stuck++; }