From 18de84227145c493708e0f8f0a7da53b3c530807 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 20:16:04 +0100 Subject: [PATCH 01/72] dt-bindings: power: apple,pmp-report: add apple,sleep-wakeup-path A display coprocessor whose power domain is a PMP report entry keeps its firmware running through s2idle and puts itself on the wakeup path for it. Let the entry say so, as apple,sleep-wakeup-path says it for a PMGR power state, so that the entry and the power states above it stay on while the firmware runs. Signed-off-by: Andreas Fernandez --- .../bindings/power/apple,t8140-pmp-report.yaml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Documentation/devicetree/bindings/power/apple,t8140-pmp-report.yaml b/Documentation/devicetree/bindings/power/apple,t8140-pmp-report.yaml index 9e0847cb04b0a..2186802c66215 100644 --- a/Documentation/devicetree/bindings/power/apple,t8140-pmp-report.yaml +++ b/Documentation/devicetree/bindings/power/apple,t8140-pmp-report.yaml @@ -258,6 +258,15 @@ patternProperties: switched with interrupts disabled. Setting the state takes a spinlock and polls the acknowledgment atomically. + apple,sleep-wakeup-path: + type: boolean + description: + The consumer of this entry can keep its firmware running through a + system sleep and is then on the wakeup path. The OS keeps the entry, + and the power states it is a child of, on through system sleep while + its consumer is on the wakeup path, as apple,sleep-wakeup-path does + for a PMGR power state. + required: - compatible - reg From 6dcf8029144109e733a994e0517956bb97a26fdf Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 20:16:04 +0100 Subject: [PATCH 02/72] pmdomain: apple: pmp-report: keep an entry on for a wakeup-path consumer genpd keeps a domain on through system sleep for a device on the wakeup path only if the domain is active-wakeup, and only apple,always-on entries were. An entry without it is powered off in the noirq phase although its consumer asked to stay powered: the request is withdrawn and the PMGR states above it go off under a firmware that still runs. Make an entry with apple,sleep-wakeup-path active-wakeup too. Nothing else changes for it: it is not seeded at the PMP start and not requested at probe. Signed-off-by: Andreas Fernandez --- drivers/pmdomain/apple/pmp-report.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/pmdomain/apple/pmp-report.c b/drivers/pmdomain/apple/pmp-report.c index c790606badd8a..d24ac7c4f0f1c 100644 --- a/drivers/pmdomain/apple/pmp-report.c +++ b/drivers/pmdomain/apple/pmp-report.c @@ -3718,6 +3718,14 @@ static int apple_pmp_report_entry_probe(struct platform_device *pdev) ent->sleep_release = false; } } + /* + * A consumer whose firmware keeps running through a system sleep puts itself on the + * wakeup path, as a PMGR state's consumer does for apple,sleep-wakeup-path. Keep the + * entry on with it then, and with the entry its parent power states: powered off in the + * noirq phase, they would take the firmware's power while it runs. + */ + if (of_property_read_bool(node, "apple,sleep-wakeup-path")) + ent->genpd.flags |= GENPD_FLAG_ACTIVE_WAKEUP; ent->genpd.power_on = apple_pmp_report_entry_power_on; ent->genpd.power_off = apple_pmp_report_entry_power_off; From a654145d27dc4270beb15f239cacc339378415b6 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 20:16:05 +0100 Subject: [PATCH 03/72] arm64: dts: apple: t6000: keep dcpext1 powered through s2idle With APPLE_USE_PMP (J314s, J316s) dcpext1 sits in the pmp-dispext1 report entry, which unlike disp0's and dispext0's is not always-on. Its DCP stays on the wakeup path through s2idle, with its firmware running, but genpd powered the entry and dispext1's power states off in the noirq phase all the same. The firmware lost its power unseen, and the next deep sleep could not park it: link sleep timed out, IOP IDLE failed, and the block went down with it for the rest of the boot. Mark the entry apple,sleep-wakeup-path. dcpext1 then goes through every sleep as dcpext0 does. Signed-off-by: Andreas Fernandez --- arch/arm64/boot/dts/apple/t6000.dtsi | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/arm64/boot/dts/apple/t6000.dtsi b/arch/arm64/boot/dts/apple/t6000.dtsi index 07ae672a27c96..07f20a5b445e2 100644 --- a/arch/arm64/boot/dts/apple/t6000.dtsi +++ b/arch/arm64/boot/dts/apple/t6000.dtsi @@ -85,6 +85,16 @@ reg-names = "report", "ane-dvfs"; }; +/* + * The external DCPs' firmware runs through s2idle and a sleep given up, and the DCP puts + * itself on the wakeup path for it. With APPLE_USE_PMP this entry is dcpext1's power domain: + * keep it, and the dispext1 power states under it, on then. dispext0's and disp0's entries + * have that from apple,always-on. + */ +&pmp_report_dispext1 { + apple,sleep-wakeup-path; +}; + #ifdef APPLE_USE_PMP /* * The video encoder, AVD, the JPEG codecs, the scaler and the ProRes codec From 13ca41534c3b736bf7a2f809aee7ce4b906e044e Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Sat, 28 Nov 2026 15:47:10 +0100 Subject: [PATCH 04/72] mmc: sdhci-pci-gli: keep the card across a GL9755 power cut on resume After a deep sleep that cut the GL9755's power, the chip reports an empty slot for most of a second after its resume before Present State shows the card again. The detect the core runs right after resume gets -ENOMEDIUM from the card's re-init and drops a card that never left the slot. The insert interrupt brings it back later as a new card, and a filesystem mounted from it has lost its device. The card state stable bit reads 1 throughout, so it cannot be waited on. When a card sat in the slot before the sleep, wait in the resume hook until the chip reports it, for 1.5 s at most. A resume without a card, or one where the chip kept its power, does not wait. Signed-off-by: Andreas Fernandez --- drivers/mmc/host/sdhci-pci-gli.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/drivers/mmc/host/sdhci-pci-gli.c b/drivers/mmc/host/sdhci-pci-gli.c index 300bfe010e69b..82ebc55abaed7 100644 --- a/drivers/mmc/host/sdhci-pci-gli.c +++ b/drivers/mmc/host/sdhci-pci-gli.c @@ -1930,6 +1930,9 @@ static int sdhci_pci_gli_resume(struct sdhci_pci_chip *chip) static int gl9755_resume(struct sdhci_pci_chip *chip) { struct sdhci_pci_slot *slot = chip->slots[0]; + struct sdhci_host *host = slot->host; + u32 val; + int ret; /* * A sleep that takes the reader's power resets its vendor configuration, @@ -1937,9 +1940,24 @@ static int gl9755_resume(struct sdhci_pci_chip *chip) * at probe before the host resumes. */ gl9755_hw_setting(slot); - gl9755_vendor_init(slot->host); + gl9755_vendor_init(host); + + ret = sdhci_pci_gli_resume(chip); + if (ret) + return ret; + + /* + * After such a power cut the chip reports an empty slot, with the + * card state stable, for most of a second. The detect the core runs + * right after resume would take the card that sat in the slot for + * removed. Wait for that card to show up again. + */ + if (host->mmc->card) + read_poll_timeout(sdhci_readl, val, val & SDHCI_CARD_PRESENT, + 10000, 1500000, false, host, + SDHCI_PRESENT_STATE); - return sdhci_pci_gli_resume(chip); + return 0; } static int gl9763e_resume(struct sdhci_pci_chip *chip) From 49c2fdcfa5f96d4255bff906a3171daa5bd634e3 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Thu, 3 Dec 2026 15:28:26 +0100 Subject: [PATCH 05/72] drm/apple: route a DP-alt port only once its display asserts HPD Outside compositor pairing order, a direct DP-alt port with no pipeline takes the lowest free one as soon as it enters DP mode, whether or not a display is behind it; only Desktop 26.x, T6041 and T8142 wait for HPD. A USB-C dock without a monitor that enters DP mode before the port of a USB-C display takes the pipeline that display needs, and the display then waits for as long as the dock stays plugged in. Which port enters DP mode first changes from boot to boot. At a wake the port controller applies each port's state again, and the dock's port then takes a pipeline an HDMI display left free. Leave a port that owns no pipeline and has no HPD unrouted on every machine, as those three already do. The port controller reports HPD as a new status when a display appears, and the port is routed then. A port that owns a pipeline behaves as before on an HPD drop, and the compositor-ordered path is unchanged. Signed-off-by: Andreas Fernandez --- drivers/gpu/drm/apple/dcp-fabric.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/drivers/gpu/drm/apple/dcp-fabric.c b/drivers/gpu/drm/apple/dcp-fabric.c index 58105cc3ac6c9..a01466c8ddc88 100644 --- a/drivers/gpu/drm/apple/dcp-fabric.c +++ b/drivers/gpu/drm/apple/dcp-fabric.c @@ -2311,14 +2311,14 @@ static int dcp_typec_route_apply(struct typec_mux_dev *mux, } /* - * Desktop 26.x has one Type-C pipeline: only a port with a sink (HPD) - * takes it. A dock or cable in DP alt mode without a display behind it - * would otherwise hold it against the port that has one. + * Only a port with a sink (HPD) takes a pipeline. A dock or cable in DP + * alt mode without a display behind it would otherwise hold one against + * a port that has a display, for as long as it stays plugged in. The + * port controller reports HPD as a new status when a display appears, + * and the port is routed then. What a port that owns a pipeline does on + * an HPD drop is unchanged. */ - if (!port->owner && !hpd && - (route->dcp->fw_compat == DCP_FIRMWARE_V_26_6_DESKTOP || - dcp_t6041_ext_v27(route->dcp) || - dcp_t8142_ext(route->dcp))) { + if (!port->owner && !hpd) { port->applied_valid = true; return 0; } @@ -2333,7 +2333,7 @@ static int dcp_typec_route_apply(struct typec_mux_dev *mux, best = dcp_typec_free_route(port); /* Every pipeline is driving another port's display. */ if (!best) - return hpd ? dcp_typec_port_busy(port, route->dcp) : -ENOSPC; + return dcp_typec_port_busy(port, route->dcp); ret = dcp_typec_route_activate(best, best->xbar); if (ret) From 0a274fa52bcd66a2d8e5fbd93f64596b9fa309ce Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Thu, 3 Dec 2026 12:49:32 +0100 Subject: [PATCH 06/72] drm/apple: av: close an audio instance the firmware replaced while open When a display drops and comes back before the host has turned it off, the DCP firmware announces a new DCPAVAudioInterface while the host still holds the previous one open. The firmware keeps that instance, and the display's link with it, until its client closes it: a later release of the link (a DPTX disconnect) is never answered, and the port answers nothing more until the firmware is restarted. The new announcement retired the open instance on the host side as a superseded singleton, so its Close could no longer be sent. Leave an instance its owner still holds open to the owner (a held() callback in the service ops). The work that opens the successor closes it first and retires it then. Signed-off-by: Andreas Fernandez --- drivers/gpu/drm/apple/afk.c | 10 ++++- drivers/gpu/drm/apple/afk.h | 22 +++++++++++ drivers/gpu/drm/apple/av.c | 74 +++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index 52a59dc0eee57..badc6d7de8484 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -2199,7 +2199,15 @@ static void afk_recv_handle_init(struct apple_dcp_afkep *ep, u32 channel, for (ch_idx = 0; ch_idx < ep->num_channels; ch_idx++) { struct apple_epic_service *old = &ep->services[ch_idx]; - if (old != service && afk_service_supersede(old, ops, channel)) { + if (old == service) + continue; + if (afk_service_held(old, ops)) { + dev_info(ep->dcp->dev, + "AFK[ep:%02x]: %s on channel %u: channel %u is still open, left to its owner\n", + ep->endpoint, service_name, channel, old->channel); + continue; + } + if (afk_service_supersede(old, ops, channel)) { dev_info(ep->dcp->dev, "AFK[ep:%02x]: %s on channel %u supersedes channel %u\n", ep->endpoint, service_name, channel, old->channel); diff --git a/drivers/gpu/drm/apple/afk.h b/drivers/gpu/drm/apple/afk.h index 7e17d5188c218..60151ccaed626 100644 --- a/drivers/gpu/drm/apple/afk.h +++ b/drivers/gpu/drm/apple/afk.h @@ -126,6 +126,13 @@ struct apple_epic_service_ops { /* Compact EPIC: do not send compact start (0x12) on announcement. */ bool defer_start; + /* + * A singleton instance its owner still holds open in the firmware, + * which keeps such an instance after announcing its successor until + * the owner closes it: the successor leaves it to the owner, which + * closes and retires it. Called from the receive path. + */ + bool (*held)(struct apple_epic_service *service); /* * Optional admission, before OPEN/publication. Must not retain service * on failure; teardown must accept a failed/partial preparation. @@ -290,6 +297,21 @@ static inline bool afk_service_supersede(struct apple_epic_service *service, return superseded; } +/* Whether @service, a live instance of @ops, is still held open by its owner. */ +static inline bool afk_service_held(struct apple_epic_service *service, + const struct apple_epic_service_ops *ops) +{ + unsigned long flags; + bool live; + + if (!ops->singleton || !ops->held) + return false; + spin_lock_irqsave(&service->lock, flags); + live = service->ops == ops && service->enabled && !service->torndown; + spin_unlock_irqrestore(&service->lock, flags); + return live && ops->held(service); +} + static inline bool afk_service_matches(struct apple_epic_service *service, u32 channel) { diff --git a/drivers/gpu/drm/apple/av.c b/drivers/gpu/drm/apple/av.c index c979b6b83ec9a..eeb2cbbd21131 100644 --- a/drivers/gpu/drm/apple/av.c +++ b/drivers/gpu/drm/apple/av.c @@ -107,6 +107,11 @@ struct audiosrv_data { struct rw_semaphore srv_rwsem; /* Workqueue for starting the audio service */ struct work_struct start_av_service_wq; + /* + * An instance replaced while it was open, with its owner pin + * (plug_lock): the start work closes it (av_audiosrv_held()). + */ + struct apple_epic_service *replaced; #if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) /* The 14.7 external role: let a terminated audio interface go. */ struct work_struct terminate_wq; @@ -182,6 +187,26 @@ static void av_interface_teardown(struct apple_epic_service *service) mutex_unlock(&asrv->plug_lock); } +/* + * The firmware keeps an audio instance its client has opened, and the + * display's link with it, until the client closes it, also once it has + * announced the instance's successor (a display that drops and comes back + * before the host turned it off). Its successor does not retire such an + * instance: av_audiosrv_init() has it closed first. + */ +static bool av_audiosrv_held(struct apple_epic_service *service) +{ + struct audiosrv_data *asrv = service->ep->dcp->audiosrv; + bool held; + + if (!asrv) + return false; + guard(mutex)(&asrv->plug_lock); + scoped_guard(rwsem_read, &asrv->srv_rwsem) + held = asrv->is_open && asrv->srv == service; + return held; +} + static void av_audiosrv_init(struct apple_epic_service *service, const char *name, const char *class, s64 unit) { @@ -208,6 +233,15 @@ static void av_audiosrv_init(struct apple_epic_service *service, const char *nam #if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) wake_up_all(&asrv->gen_wq); #endif + if (old && old != service && asrv->is_open) { + if (!asrv->replaced && queue_start) { + /* The start work closes it before it opens @service. */ + asrv->replaced = old; + old = NULL; + } else { + afk_service_retire_channel(old, old->channel); + } + } if (old) afk_service_put(old); @@ -1132,6 +1166,7 @@ static const struct apple_epic_service_ops avep_ops[] = { .name = "DCPAVAudioInterface", .reusable = true, .singleton = true, + .held = av_audiosrv_held, .init = av_audiosrv_init, .report = av_audiosrv_report, .teardown = av_audiosrv_teardown, @@ -1443,12 +1478,41 @@ static void av_work_service_terminate(struct work_struct *work) } #endif +/* Close the instance replaced while it was open, then retire it. */ +static void av_audiosrv_close_replaced(struct audiosrv_data *asrv) +{ + struct apple_epic_service *old; + struct device *dev; + u32 channel; + int ret; + + scoped_guard(mutex, &asrv->plug_lock) { + old = asrv->replaced; + asrv->replaced = NULL; + } + if (!old) + return; + dev = old->ep->dcp->dev; + channel = old->channel; + ret = afk_service_call(old, 0, asrv->cmds.close, NULL, 0, 16, NULL, 0, 16); + if (ret == -ENODEV) + dev_dbg(dev, "replaced audio service on channel %u already gone\n", channel); + else if (ret) + dev_err(dev, "error closing the replaced audio service on channel %u: %d\n", + channel, ret); + else + dev_info(dev, "closed the replaced audio service on channel %u\n", channel); + afk_service_retire_channel(old, channel); + afk_service_put(old); +} + static void av_work_service_start(struct work_struct *work) { struct audiosrv_data *audiosrv_data; struct apple_dcp *dcp; audiosrv_data = container_of(work, struct audiosrv_data, start_av_service_wq); + av_audiosrv_close_replaced(audiosrv_data); scoped_guard(rwsem_read, &audiosrv_data->srv_rwsem) { if (!audiosrv_data->srv || @@ -1494,6 +1558,7 @@ static void av_audiosrv_flush_owner(struct audiosrv_data *asrv) /* Drain the captured owner, without following a later dcp publication. */ static bool av_audiosrv_drain_owner(struct audiosrv_data *asrv) { + struct apple_epic_service *old; bool first_stop; if (!asrv || !asrv->works_live) @@ -1520,6 +1585,15 @@ static bool av_audiosrv_drain_owner(struct audiosrv_data *asrv) } else { av_audiosrv_flush_owner(asrv); } + /* A replaced instance whose start work never ran keeps no pin. */ + scoped_guard(mutex, &asrv->plug_lock) { + old = asrv->replaced; + asrv->replaced = NULL; + } + if (old) { + afk_service_retire_channel(old, old->channel); + afk_service_put(old); + } return true; } From 56487b2eeaadd68c604624cf2d589182c4b89ebe Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Mon, 30 Nov 2026 23:43:51 +0100 Subject: [PATCH 07/72] dt-bindings: soc: apple: sleep: add T6000 The M1 Pro's sleep controls: the PMGR's AP wake block, its power-state blocks on both sides, the CPU start block, the watchdog, the S2R timer that commits a sleep, the NUB SPMI vector enables, the PMP telemetry block, the SoC clock generator's mux block and the fabric ports of the GPU and of the three display blocks. A sleep keeps the wake sources it names enabled. The node also points at the blocks the sleep reaches through their own nodes: the PMGR's misc block (apple,pmgr-misc), SIO (apple,sio) and the NCO (apple,nco). apple,sleep-late-domains lists the PMGR power-state nodes written right before the S2R timer, each with the word written to it, in the order written. apple,tunable-gfx-fabric, apple,tunable-disp0-fabric, apple,tunable-dispext0-fabric and apple,tunable-dispext1-fabric hold the setup each fabric port gets before its block powers down: whole words, in the order written. Signed-off-by: Andreas Fernandez --- .../bindings/soc/apple/apple,t8140-sleep.yaml | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml b/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml index ad24b561a8d5b..003c2cd1a61ad 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,t8112-sleep - apple,t8122-sleep @@ -101,6 +102,78 @@ properties: The DCP and the external DCPs, which must be off before a commit, in that order. + apple,pmgr-misc: + $ref: /schemas/types.yaml#/definitions/phandle + description: | + The PMGR misc node (apple,t6000-pmgr-misc), whose "dcs-ps" and "fabric-ps" + words the sleep sets: the memory clock's and the fabric's performance state. + + apple,sio: + $ref: /schemas/types.yaml#/definitions/phandle + description: + The SIO coprocessor. Its CPU (its first reg), its mailbox (mboxes) and + its DARTs (iommus) are stopped in the late steps while its CPU power word + (power-domains) is on. + + apple,sleep-late-domains: + $ref: /schemas/types.yaml#/definitions/phandle-array + items: + items: + - description: a PMGR power-state node + - description: the word written to it + description: + T6000. The PMGR power-state words the late steps write right before the + S2R timer, in that order, each with the value written; a node may appear + more than once. Restored on wake. + + apple,tunable-gfx-fabric: + $ref: /schemas/types.yaml#/definitions/uint32-matrix + items: + items: + - description: Offset in the gfx-fabric reg. + - description: Bits to replace, always the whole word. + const: 0xffffffff + - description: Their value. + description: + The setup of the GPU's fabric port, applied in this order before its + power goes for a deep sleep, each word written as listed. + + apple,tunable-disp0-fabric: + $ref: /schemas/types.yaml#/definitions/uint32-matrix + items: + items: + - description: Offset in the disp0-fabric reg. + - description: Bits to replace, always the whole word. + const: 0xffffffff + - description: Their value. + description: + The setup of the internal display block's fabric port, applied in this order before its + power goes for a deep sleep, each word written as listed. + + apple,tunable-dispext0-fabric: + $ref: /schemas/types.yaml#/definitions/uint32-matrix + items: + items: + - description: Offset in the dispext0-fabric reg. + - description: Bits to replace, always the whole word. + const: 0xffffffff + - description: Their value. + description: + The setup of the first external display block's fabric port, applied in this order before its + power goes for a deep sleep, each word written as listed. + + apple,tunable-dispext1-fabric: + $ref: /schemas/types.yaml#/definitions/uint32-matrix + items: + items: + - description: Offset in the dispext1-fabric reg. + - description: Bits to replace, always the whole word. + const: 0xffffffff + - description: Their value. + description: + The setup of the second external display block's fabric port, applied in this order before its + power goes for a deep sleep, each word written as listed. + apple,nco: $ref: /schemas/types.yaml#/definitions/phandle description: @@ -125,6 +198,47 @@ required: - apple,display-coprocessors allOf: + - if: + properties: + compatible: + contains: + const: apple,t6000-sleep + then: + required: + - apple,wake-enable + - apple,pmgr-misc + - apple,sio + - apple,sleep-late-domains + - apple,nco + - apple,tunable-gfx-fabric + - apple,tunable-disp0-fabric + - apple,tunable-dispext0-fabric + - apple,tunable-dispext1-fabric + properties: + reg: + minItems: 13 + maxItems: 13 + reg-names: + items: + - const: wake + - const: ps + - const: ps-east + - const: cpu-start + - const: wdt + - const: commit + - const: spmi-irq + - const: ptd + - const: clk-gen + - const: gfx-fabric + - const: disp0-fabric + - const: dispext0-fabric + - const: dispext1-fabric + else: + properties: + apple,tunable-gfx-fabric: false + apple,tunable-disp0-fabric: false + apple,tunable-dispext0-fabric: false + apple,tunable-dispext1-fabric: false - if: properties: compatible: From 9742a2ea22daf963c84a84c85df8f400340279d2 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 00:07:40 +0100 Subject: [PATCH 08/72] soc: apple: sleep: add apple_t6000_sleep_described() The T6000 drivers that take part in its firmware-driven sleep need to know whether the device tree describes that sleep. T6001 and T6002 share the T6000 block compatibles (SMC, AIC, DART, NCO) and have no such sleep, so the check looks for an available apple,t6000-sleep node, which only a T6000 machine's tree can carry. Signed-off-by: Andreas Fernandez --- include/linux/soc/apple/sleep.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index 39ab320931c91..c5afd82e607b6 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -55,6 +55,21 @@ static inline bool apple_sleep_smc_policy_described(void) return false; } +/* + * A machine whose device tree describes the T6000 firmware-driven sleep: its sleep node is + * enabled. T6001 and T6002 share T6000's block compatibles, and their trees have no such node. + */ +static inline bool apple_t6000_sleep_described(void) +{ + struct device_node *np; + bool ret; + + np = of_find_compatible_node(NULL, NULL, "apple,t6000-sleep"); + ret = np && of_device_is_available(np); + of_node_put(np); + return ret; +} + /* The SMC's sleep messages: the pmVC value and the spacings between them, used as floors */ struct apple_sleep_smc_policy { u32 pmvc; From 117356600b3a1202e68f0643eb4534cbd6bb2102 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Mon, 30 Nov 2026 23:26:40 +0100 Subject: [PATCH 09/72] soc: apple: rtkit: restart an idled co-processor with its buffers kept On the M1 Pro, SIO, the internal DCP and the GPU firmware are idled before a deep sleep and carry on from their saved context once their power comes back. Their shared buffers stay mapped through it, and the restart is the one that was traced for them: INIT, the endpoint map answered as at a boot, the IOP acknowledgment, every app endpoint started again, and only then the AP state. apple_rtkit_reinit() frees the buffers and apple_rtkit_warm_restart() echoes the endpoint map instead, so add the pieces of that restart: apple_rtkit_reinit_keep() and apple_rtkit_detach_keep() around the power cut, apple_rtkit_idle_iop() and apple_rtkit_ap_power_timeout() for the idle and the AP state with its own wait, and apple_rtkit_wake_keep_ap() with apple_rtkit_restart_started_eps() for the way back. Nothing calls them yet. apple_rtkit_reinit_keep() resets the session through the helper apple_rtkit_reinit() uses, keeping the buffers and the endpoints that were started. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/rtkit.c | 150 ++++++++++++++++++++++++++++++-- include/linux/soc/apple/rtkit.h | 25 ++++++ 2 files changed, 168 insertions(+), 7 deletions(-) diff --git a/drivers/soc/apple/rtkit.c b/drivers/soc/apple/rtkit.c index 8697450b4281a..e3e091e517cc1 100644 --- a/drivers/soc/apple/rtkit.c +++ b/drivers/soc/apple/rtkit.c @@ -2724,7 +2724,12 @@ EXPORT_SYMBOL_GPL(apple_rtkit_port_restart); * 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) +/* + * @keep_started: the firmware resumes from its saved context, so the endpoints it + * had started stay started and what it announced once is not announced again. + */ +static void apple_rtkit_reset_session(struct apple_rtkit *rtk, bool keep_buffers, + bool keep_started) { unsigned long flags; @@ -2755,11 +2760,14 @@ static void apple_rtkit_reset_session(struct apple_rtkit *rtk, bool keep_buffers bitmap_zero(rtk->endpoints, APPLE_RTKIT_MAX_ENDPOINTS); set_bit(APPLE_RTKIT_EP_MGMT, rtk->endpoints); - bitmap_zero(rtk->started, APPLE_RTKIT_MAX_ENDPOINTS); - set_bit(APPLE_RTKIT_EP_MGMT, rtk->started); + if (!keep_started) { + bitmap_zero(rtk->started, APPLE_RTKIT_MAX_ENDPOINTS); + set_bit(APPLE_RTKIT_EP_MGMT, rtk->started); + } reinit_completion(&rtk->epmap_completion); - reinit_completion(&rtk->coreanalytics_completion); + if (!keep_started) + reinit_completion(&rtk->coreanalytics_completion); reinit_completion(&rtk->iop_pwr_ack_completion); reinit_completion(&rtk->ap_pwr_ack_completion); @@ -2780,7 +2788,7 @@ int apple_rtkit_reinit(struct apple_rtkit *rtk) { int ret; - apple_rtkit_reset_session(rtk, false); + apple_rtkit_reset_session(rtk, false, false); ret = apple_mbox_start(rtk->mbox); if (ret) { apple_rtkit_detach_rx(rtk); @@ -2794,7 +2802,7 @@ int apple_rtkit_reinit_retaining_buffers(struct apple_rtkit *rtk) { int ret; - apple_rtkit_reset_session(rtk, true); + apple_rtkit_reset_session(rtk, true, false); ret = apple_mbox_start(rtk->mbox); if (ret) { apple_rtkit_detach_rx(rtk); @@ -2804,6 +2812,34 @@ int apple_rtkit_reinit_retaining_buffers(struct apple_rtkit *rtk) } EXPORT_SYMBOL_GPL(apple_rtkit_reinit_retaining_buffers); +/* + * Like apple_rtkit_reinit(), for a co-processor that was idled with its buffers + * kept and lost its power: the firmware resumes from its saved context, so the + * shared buffers it was given stay mapped. Only the mailbox and the power + * handshake restart; the endpoint map is taken again as at a boot. + */ +int apple_rtkit_reinit_keep(struct apple_rtkit *rtk) +{ + int ret; + + apple_rtkit_reset_session(rtk, true, 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_keep); + +/* Stop receiving before an idled co-processor loses its power; its buffers stay put. */ +void apple_rtkit_detach_keep(struct apple_rtkit *rtk) +{ + apple_rtkit_detach_rx(rtk); + flush_workqueue(rtk->wq); +} +EXPORT_SYMBOL_GPL(apple_rtkit_detach_keep); + static int apple_rtkit_set_ap_power_state(struct apple_rtkit *rtk, unsigned int state) { @@ -3141,13 +3177,50 @@ int apple_rtkit_ap_power_noirq(struct apple_rtkit *rtk, bool on) } EXPORT_SYMBOL_GPL(apple_rtkit_ap_power_noirq); +/* The AP power state alone, IDLE or ON, waiting @ms for the acknowledgment. */ +int apple_rtkit_ap_power_timeout(struct apple_rtkit *rtk, bool on, unsigned int ms) +{ + unsigned int state = on ? APPLE_RTKIT_PWR_STATE_ON : APPLE_RTKIT_PWR_STATE_IDLE; + int ret; + + reinit_completion(&rtk->ap_pwr_ack_completion); + ret = apple_rtkit_management_send(rtk, APPLE_RTKIT_MGMT_SET_AP_PWR_STATE, + FIELD_PREP(APPLE_RTKIT_MGMT_PWR_STATE, state)); + if (ret) + return ret; + if (!wait_for_completion_timeout(&rtk->ap_pwr_ack_completion, msecs_to_jiffies(ms))) { + dev_warn(rtk->dev, "RTKit: AP power %#x not acknowledged within %u ms, state %#x\n", + state, ms, rtk->ap_power_state); + return -ETIME; + } + return 0; +} +EXPORT_SYMBOL_GPL(apple_rtkit_ap_power_timeout); + +/* + * The IOP power state IDLE alone, as macOS sends it after the AP's to a + * co-processor it is about to stop with its context kept. + */ +int apple_rtkit_idle_iop(struct apple_rtkit *rtk) +{ + int ret; + + ret = apple_rtkit_set_iop_power_state(rtk, APPLE_RTKIT_PWR_STATE_IDLE); + if (ret) + return ret; + + rtk->iop_power_state = APPLE_RTKIT_PWR_STATE_IDLE; + return 0; +} +EXPORT_SYMBOL_GPL(apple_rtkit_idle_iop); + /* Power is back, the CPU is stopped, and poweroff() discarded the old session. */ int apple_rtkit_cold_restart(struct apple_rtkit *rtk, int (*start)(void *), void *ctx) { int ret; /* A fresh boot: no warm or port-only restart may answer its endpoint map. */ - apple_rtkit_reset_session(rtk, false); + apple_rtkit_reset_session(rtk, false, false); rtk->warm_restart = false; rtk->port_only = false; rtk->warm_last_mask = U32_MAX; @@ -3292,6 +3365,69 @@ int apple_rtkit_wake_early_ap(struct apple_rtkit *rtk) } EXPORT_SYMBOL_GPL(apple_rtkit_wake_early_ap); +/* + * Wake after apple_rtkit_reinit_keep(): INIT, then the endpoint map and the IOP + * acknowledgment, but no AP power message yet. The app endpoints are started + * again first (apple_rtkit_restart_started_eps()), and the AP state is set + * after them with apple_rtkit_ap_power_timeout(). + */ +int apple_rtkit_wake_keep_ap(struct apple_rtkit *rtk) +{ + u64 msg; + int ret; + + if (apple_rtkit_is_running(rtk)) + return -EINVAL; + + reinit_completion(&rtk->iop_pwr_ack_completion); + 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) { + dev_err(rtk->dev, "RTKit: endpoint discovery failed: %d\n", ret); + return ret; + } + if (rtk->boot_result) + return rtk->boot_result; + + ret = apple_rtkit_wait(rtk, &rtk->iop_pwr_ack_completion); + if (ret) { + dev_err(rtk->dev, "RTKit: IOP power acknowledgment failed: %d\n", ret); + return ret; + } + if (rtk->crashed) + return -EIO; + + /* The app endpoints may be started again; the AP message follows them. */ + rtk->ap_power_state = APPLE_RTKIT_PWR_STATE_ON; + return 0; +} +EXPORT_SYMBOL_GPL(apple_rtkit_wake_keep_ap); + +/* Start again, in ascending order, every app endpoint that was started before the idle. */ +int apple_rtkit_restart_started_eps(struct apple_rtkit *rtk) +{ + DECLARE_BITMAP(eps, APPLE_RTKIT_MAX_ENDPOINTS); + unsigned int ep; + int ret; + + bitmap_copy(eps, rtk->started, APPLE_RTKIT_MAX_ENDPOINTS); + ep = APPLE_RTKIT_APP_ENDPOINT_START; + for_each_set_bit_from(ep, eps, APPLE_RTKIT_MAX_ENDPOINTS) { + ret = apple_rtkit_start_ep(rtk, ep); + if (ret) { + dev_err(rtk->dev, "RTKit: restarting endpoint 0x%02x failed: %d\n", + ep, ret); + return ret; + } + } + return 0; +} +EXPORT_SYMBOL_GPL(apple_rtkit_restart_started_eps); + static int apple_rtkit_sleep_idle(struct apple_rtkit *rtk, void __iomem *cpu_control) { u32 status; diff --git a/include/linux/soc/apple/rtkit.h b/include/linux/soc/apple/rtkit.h index 47e646644b429..936399ea29362 100644 --- a/include/linux/soc/apple/rtkit.h +++ b/include/linux/soc/apple/rtkit.h @@ -240,6 +240,17 @@ int apple_rtkit_reinit(struct apple_rtkit *rtk); */ int apple_rtkit_reinit_retaining_buffers(struct apple_rtkit *rtk); +/* + * A co-processor idled with its context and buffers kept, whose power was then + * cut: apple_rtkit_reinit() without freeing anything. Call it with the CPU + * stopped; then start the CPU, apple_rtkit_wake_keep_ap(), + * apple_rtkit_restart_started_eps() and apple_rtkit_ap_power_timeout(on). + */ +int apple_rtkit_reinit_keep(struct apple_rtkit *rtk); + +/* Stop receiving from an idled co-processor before its power goes, buffers kept. */ +void apple_rtkit_detach_keep(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 @@ -291,6 +302,14 @@ int apple_rtkit_boot_or_wake(struct apple_rtkit *rtk, unsigned int self_ack_ms); /* Wake helpers which require AP readiness before their IOP power acknowledgment. */ int apple_rtkit_wake_early_ap(struct apple_rtkit *rtk); +/* + * After apple_rtkit_reinit_keep() and the CPU started: INIT, endpoint map and + * IOP acknowledgment, with no AP message. Then apple_rtkit_restart_started_eps() + * starts the app endpoints the client had started, and the AP state follows. + */ +int apple_rtkit_wake_keep_ap(struct apple_rtkit *rtk); +int apple_rtkit_restart_started_eps(struct apple_rtkit *rtk); + /* * Wake a co-processor stopped after apple_rtkit_idle() with its firmware, * endpoints and buffers kept: restore the mailbox, resume RX, call @start to @@ -383,6 +402,12 @@ int apple_rtkit_ap_power(struct apple_rtkit *rtk, bool on); /* The same, for a noirq phase: device interrupts are off, so it polls. */ int apple_rtkit_ap_power_noirq(struct apple_rtkit *rtk, bool on); +/* The AP power state alone, waiting @ms for its acknowledgment (not the handshake timeout). */ +int apple_rtkit_ap_power_timeout(struct apple_rtkit *rtk, bool on, unsigned int ms); + +/* The IOP power state IDLE alone, after the AP's, for a co-processor stopped with its context. */ +int apple_rtkit_idle_iop(struct apple_rtkit *rtk); + /* The M5 Pro's AP-only saved-context lane. begin/end run in process context, * with the consumer device locked throughout. Exclude every other RTKit user. * begin synchronizes RX and refuses while accepted jobs are still active; From 7baac87b0ee9247eabc9d3338bb8347b7b9f9c16 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 01:59:29 +0100 Subject: [PATCH 10/72] soc: apple: rtkit: add a polled IOP idle With interrupts off, a caller can already ask for the AP's idle between apple_rtkit_polled_begin() and apple_rtkit_polled_end(). Add the IOP's idle to follow it, for a caller that checks its CPU's state itself afterwards. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/rtkit.c | 7 +++++++ include/linux/soc/apple/rtkit.h | 2 ++ 2 files changed, 9 insertions(+) diff --git a/drivers/soc/apple/rtkit.c b/drivers/soc/apple/rtkit.c index e3e091e517cc1..e0791cb5df8dc 100644 --- a/drivers/soc/apple/rtkit.c +++ b/drivers/soc/apple/rtkit.c @@ -3591,6 +3591,13 @@ int apple_rtkit_polled_ap_idle(struct apple_rtkit *rtk, unsigned int *us) } EXPORT_SYMBOL_GPL(apple_rtkit_polled_ap_idle); +int apple_rtkit_polled_iop_idle(struct apple_rtkit *rtk, unsigned int *us) +{ + return apple_rtkit_polled_state(rtk, APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE, + &rtk->iop_power_state, us); +} +EXPORT_SYMBOL_GPL(apple_rtkit_polled_iop_idle); + int apple_rtkit_sleep_idle_polled(struct apple_rtkit *rtk, void __iomem *cpu_control, unsigned int *us) { diff --git a/include/linux/soc/apple/rtkit.h b/include/linux/soc/apple/rtkit.h index 936399ea29362..4a52e682d8b2c 100644 --- a/include/linux/soc/apple/rtkit.h +++ b/include/linux/soc/apple/rtkit.h @@ -503,6 +503,8 @@ void apple_rtkit_polled_begin(struct apple_rtkit *rtk); void apple_rtkit_polled_end(struct apple_rtkit *rtk); int apple_rtkit_polled_exchange(struct apple_rtkit *rtk, u8 ep, u64 msg, u64 mask, u64 want); int apple_rtkit_polled_ap_idle(struct apple_rtkit *rtk, unsigned int *us); +/* Then the IOP's IDLE, after the AP's: @us adds the time it took */ +int apple_rtkit_polled_iop_idle(struct apple_rtkit *rtk, unsigned int *us); /* * Send a message to the given endpoint. From 0d6876652152dde1fd27cd461c541a51b6671d82 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Mon, 30 Nov 2026 23:53:12 +0100 Subject: [PATCH 11/72] soc: apple: mailbox: add apple_mbox_set_send_enabled() A coprocessor whose power was cut loses its mailbox's control words with it. macOS enables the inbox (bit 0 of the send control word) again before it starts such a coprocessor, if it reads off. Add the setter next to apple_mbox_set_recv_enabled(). It writes only if the bit reads otherwise. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/mailbox.c | 17 +++++++++++++++++ include/linux/soc/apple/mailbox.h | 1 + 2 files changed, 18 insertions(+) diff --git a/drivers/soc/apple/mailbox.c b/drivers/soc/apple/mailbox.c index 295a0ea4308ab..0eb68623db8b3 100644 --- a/drivers/soc/apple/mailbox.c +++ b/drivers/soc/apple/mailbox.c @@ -1100,6 +1100,23 @@ void apple_mbox_set_recv_enabled(struct apple_mbox *mbox, bool enabled) } EXPORT_SYMBOL_GPL(apple_mbox_set_recv_enabled); +/* + * Bit 0 of the send control word, the inbox enable (the ADT's inbox-ctrl-enable-mask). A + * client whose firmware lost its power with the mailbox sets it before the firmware starts + * again, as macOS does when it starts the processor. Written only if it reads otherwise. + */ +void apple_mbox_set_send_enabled(struct apple_mbox *mbox, bool enabled) +{ + u32 val = readl(mbox->regs + mbox->hw->a2i_control); + + if (!!(val & APPLE_ASC_MBOX_CONTROL_RECV_ENABLE) == enabled) + return; + val = enabled ? val | APPLE_ASC_MBOX_CONTROL_RECV_ENABLE : + val & ~APPLE_ASC_MBOX_CONTROL_RECV_ENABLE; + writel(val, mbox->regs + mbox->hw->a2i_control); +} +EXPORT_SYMBOL_GPL(apple_mbox_set_send_enabled); + int apple_mbox_ans_idle_status(struct apple_mbox *mbox, u32 *status) { if (!of_device_is_compatible(mbox->dev->of_node, "apple,t6050-asc-mailbox")) diff --git a/include/linux/soc/apple/mailbox.h b/include/linux/soc/apple/mailbox.h index b0b1083faef03..4c836d18ccceb 100644 --- a/include/linux/soc/apple/mailbox.h +++ b/include/linux/soc/apple/mailbox.h @@ -127,6 +127,7 @@ int apple_mbox_recv_nowait(struct apple_mbox *mbox, struct apple_mbox_msg *msg); /* Bit 0 of the receive control word, for firmware that powers itself down. */ void apple_mbox_set_recv_enabled(struct apple_mbox *mbox, bool enabled); u32 apple_mbox_read_status(struct apple_mbox *mbox); +void apple_mbox_set_send_enabled(struct apple_mbox *mbox, bool enabled); /* * Leave received messages in the hardware FIFO, without an interrupt, until * apple_mbox_release_rx(). For consumers that must not wake the system. From a6a60b349024decca8e11821ed636340df85521c Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Mon, 30 Nov 2026 23:27:50 +0100 Subject: [PATCH 12/72] dmaengine: apple-sio: park the T6000 SIO firmware late for a deep sleep The deep sleep of the M1 Pro cuts the power of the SIO island, and the SoC only powers down once that island has gated. As macOS does, idle the SIO firmware in the late suspend phase, AP state first with a 2 s wait for its answer, then the IOP state, and stop its CPU with its buffers and context kept. After the power cut start it again from there: the mailbox restarted, the CPU run bit, INIT and the endpoint map, its endpoints started again, then the AP state on. The park needs a running firmware in the late phase, so this SIO stays runtime-active, as one stopped at the sleep's commit does: no runtime stop when idle, and system suspend leaves it alone. Its domain is gated by the sleep itself and not earlier. A firmware that does not idle or does not come back is left alone until a reboot rather than refusing the sleep; nothing is sent to it while it is parked or lost. A firmware parked for a sleep that is then given up stays parked until the next deep sleep cuts the power, through any suspend-to-idle in between. Selected by the apple,t6000-sio compatible; other SoCs keep their path. A dry run of the sleep core, which cuts no power, leaves the firmware running as suspend-to-idle does. Signed-off-by: Andreas Fernandez --- drivers/dma/apple-sio.c | 134 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 128 insertions(+), 6 deletions(-) diff --git a/drivers/dma/apple-sio.c b/drivers/dma/apple-sio.c index 81a7ee1da1224..313d6762d8c03 100644 --- a/drivers/dma/apple-sio.c +++ b/drivers/dma/apple-sio.c @@ -225,6 +225,11 @@ struct sio_hw_data { bool commit_early; /* Acknowledge OSLog counters so pending logs do not stall AP idle. */ bool oslog_echo; + /* + * Idled late with its context and buffers kept, and restarted from there once + * the sleep has cut its power: see sio_park_kept(). + */ + bool park_kept; /* DMA arrays owned by the driver, if not protocol 9's */ const struct sio_dma_arrays *arrays; int (*check_boot_tables)(struct device *dev); @@ -232,6 +237,13 @@ struct sio_hw_data { int (*send_dt_params)(struct sio_data *sio); }; +/* The firmware of a park_kept SIO across a deep sleep */ +enum sio_fw_state { + SIO_FW_RUNNING, /* zero, as after probe */ + SIO_FW_PARKED, /* idle and stopped for the sleep: nothing is sent */ + SIO_FW_LOST, /* did not go idle or start again: left alone until a reboot */ +}; + struct sio_data { const struct sio_hw_data *hw; void __iomem *base; @@ -252,6 +264,7 @@ struct sio_data { * sio_synchronize() is bounded (sio_pm_prepare()). */ bool sleeping; + enum sio_fw_state fw_state; bool firmware_only; /* Protocol 10 firmware given the earlier boot tables: no DMA, which would crash it */ bool tables_mismatch; @@ -729,6 +742,16 @@ static const struct sio_hw_data sio_hw_generic = { .cfg_word4 = 0x800, }; +/* + * T6000 (M1 Pro): parked late for a deep sleep, as macOS does, and restarted with its + * context once the sleep has cut its power. + */ +static const struct sio_hw_data sio_hw_t6000 = { + .fifo_threshold = 0x800, + .cfg_word4 = 0x800, + .park_kept = true, +}; + static const struct sio_hw_data sio_hw_t6030 = { .stop_at_commit = true, .commit_no_cpu_down = true, @@ -815,6 +838,7 @@ static const struct of_device_id sio_of_match[] = { */ { .compatible = "apple,t8122-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 }, { } }; @@ -1399,11 +1423,11 @@ static bool sio_fill_in_old_locked(struct sio_chan *siochan) FIELD_PREP(SIOMSG_TYPE, MSG_ISSUE) | FIELD_PREP(SIOMSG_DATA, sio_coproc_desc_slot(sio, d)), sio_handle_issue_ack, (void *)(uintptr_t)tx->vd.tx.cookie); - if (ret < 0) + if (ret >= 0) + siochan->pending++; + else if (ret != -ESHUTDOWN) dev_err_ratelimited(sio->dev, "can't issue on chan %d ninflight %d: %d\n", siochan->no, tx->ninflight, ret); - else - siochan->pending++; return true; } @@ -1425,8 +1449,9 @@ static bool sio_fill_in_ring_locked(struct sio_chan *siochan) FIELD_PREP(SIOMSG_DATA, sio_coproc_desc_slot(sio, d)), sio_handle_issue_ack, NULL); if (ret < 0) { - dev_err_ratelimited(sio->dev, "can't issue on chan %d ninflight %d: %d\n", - siochan->no, siochan->nissued, ret); + if (ret != -ESHUTDOWN) + dev_err_ratelimited(sio->dev, "can't issue on chan %d ninflight %d: %d\n", + siochan->no, siochan->nissued, ret); return false; } /* The ack takes the channel lock, held here. */ @@ -2399,6 +2424,10 @@ static int _sio_send_siomsg(struct sio_data *sio, u64 msg, bool atomic, unsigned long generation; int tag, ret; + /* Not to a firmware parked for a deep sleep, or one that did not start again */ + if (READ_ONCE(sio->fw_state) != SIO_FW_RUNNING) + return -ESHUTDOWN; + tag = sio_alloc_tag(sio, msg, atomic, ack_callback, cookie, &generation); if (tag < 0) return tag; @@ -2859,9 +2888,13 @@ static int sio_commit_stop(void *data) return ret; } +/* + * A park_kept SIO is idled by its own late suspend for a deep sleep and needs a live + * firmware there: it stays runtime-active, as one stopped at the commit does. + */ static bool sio_can_runtime_suspend(const struct sio_data *sio) { - return !sio->hw->commit_stop && !sio->hw->stop_at_commit; + return !sio->hw->commit_stop && !sio->hw->stop_at_commit && !sio->hw->park_kept; } static int sio_probe(struct platform_device *pdev) @@ -3255,6 +3288,83 @@ static __maybe_unused int sio_resume(struct device *dev) return 0; } +/* + * A deep sleep of a SoC whose SIO is parked late with its context kept. Not a dry run, which + * cuts no power: there the SIO stays as for s2idle, as a parked one would stay stopped. + */ +static __maybe_unused bool sio_park_kept(struct sio_data *sio) +{ + return sio->hw->park_kept && pm_suspend_target_state == PM_SUSPEND_MEM && + pm_suspend_via_firmware() && !apple_sleep_dry_run(); +} + +/* + * The deep sleep cuts the SIO's power. As macOS does before it, tell the firmware + * that the AP goes idle, idle it and stop its CPU, with its buffers and its state + * left where they are: after the power cut it carries on from there. In the late + * phase, once every device has suspended, as a stopped firmware only starts again + * after a power cut. + */ +static __maybe_unused int sio_park_kept_suspend_late(struct device *dev) +{ + struct sio_data *sio = dev_get_drvdata(dev); + int ret; + + /* Still stopped from a sleep that was given up, or left alone */ + if (sio->fw_state != SIO_FW_RUNNING) + return 0; + + ret = apple_rtkit_ap_power_timeout(sio->rtk, false, 2000); + if (!ret) + ret = apple_rtkit_idle_iop(sio->rtk); + if (ret) { + dev_err(dev, "the firmware did not go idle (%d), no DisplayPort sound until a reboot\n", + ret); + WRITE_ONCE(sio->fw_state, SIO_FW_LOST); + return 0; + } + apple_rtkit_detach_keep(sio->rtk); + writel(readl(sio->base + REG_CPU_CONTROL) & ~CPU_CONTROL_RUN, sio->base + REG_CPU_CONTROL); + WRITE_ONCE(sio->fw_state, SIO_FW_PARKED); + + return 0; +} + +static __maybe_unused int sio_park_kept_resume(struct device *dev) +{ + struct sio_data *sio = dev_get_drvdata(dev); + int ret; + + /* This sleep's suspend_late did not park it (it gave up, or the firmware is lost) */ + if (sio->fw_state != SIO_FW_PARKED) + return 0; + + /* Given up on the way down: stopped it stays, until a sleep that cuts the power */ + if (!pm_resume_via_firmware()) { + dev_warn(dev, "no DisplayPort sound until the next sleep: this one was given up\n"); + return 0; + } + + ret = apple_rtkit_reinit_keep(sio->rtk); + if (!ret) { + writel(CPU_CONTROL_RUN, sio->base + REG_CPU_CONTROL); + ret = apple_rtkit_wake_keep_ap(sio->rtk); + } + if (!ret) + ret = apple_rtkit_restart_started_eps(sio->rtk); + if (!ret) + ret = apple_rtkit_ap_power_timeout(sio->rtk, true, 2000); + if (ret) { + dev_err(dev, "the firmware did not start again (%d), no DisplayPort sound until a reboot\n", + ret); + WRITE_ONCE(sio->fw_state, SIO_FW_LOST); + return 0; + } + WRITE_ONCE(sio->fw_state, SIO_FW_RUNNING); + + return 0; +} + /* One stopped at the sleep commit stays up through system suspend. */ static __maybe_unused int sio_system_suspend(struct device *dev) { @@ -3267,6 +3377,14 @@ static __maybe_unused int sio_system_resume(struct device *dev) { struct sio_data *sio = dev_get_drvdata(dev); + /* + * As this sleep's late suspend went: sio_park_kept() holds for the whole suspend + * cycle. A firmware still parked by an earlier, refused deep sleep stays stopped + * through an s2idle or a dry run, sends nothing (-ESHUTDOWN) and starts again after + * the next power cut. + */ + if (sio_park_kept(sio)) + return sio_park_kept_resume(dev); return sio_can_runtime_suspend(sio) ? pm_runtime_force_resume(dev) : 0; } @@ -3277,6 +3395,10 @@ static __maybe_unused int sio_suspend_late(struct device *dev) if (!pm_suspend_via_firmware() || sio->asleep || sio->rpm_stopped) return 0; + if (sio_park_kept(sio)) + return sio_park_kept_suspend_late(dev); + if (sio->hw->park_kept) + return 0; for (i = 0; i < sio->nchannels; i++) { if (sio_chan_busy(&sio->channels[i])) { From 08e1d8b547bb41b5bef136e0f2a84734db21ddb7 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Wed, 2 Dec 2026 16:20:18 +0100 Subject: [PATCH 13/72] dt-bindings: nvme: apple,nvme-ans: add the T6000 ANS sleep tunables A T6000 deep sleep resets ANS's AXI2AF block and its storage-lane block (ADT ans reg[4] and reg[5]), and the ANS firmware crashes right after its endpoint discovery without the settings iBoot gave them at cold boot. Allow the two blocks as reg entries on apple,t6000-nvme-ans2, and the ADT's tunables for them (afc-aiu-ans-dual-tunables and storage-lane-common-tunables) as apple,tunable-axi2af and apple,tunable-storage-lane, so the driver can put them back after such a sleep. Other controllers have neither property. Signed-off-by: Andreas Fernandez --- .../bindings/nvme/apple,nvme-ans.yaml | 62 ++++++++++++++++--- 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/Documentation/devicetree/bindings/nvme/apple,nvme-ans.yaml b/Documentation/devicetree/bindings/nvme/apple,nvme-ans.yaml index 7b0906ff0febf..98ca25e2c6391 100644 --- a/Documentation/devicetree/bindings/nvme/apple,nvme-ans.yaml +++ b/Documentation/devicetree/bindings/nvme/apple,nvme-ans.yaml @@ -44,12 +44,7 @@ properties: reg: minItems: 2 - items: - - description: NVMe controller registers, including the NVMMU - registers on SoCs without a separate NVMMU region - - description: ANS2 co-processor control registers - - description: NVMMU registers, a separate region since T8132 (and - on T604x) + maxItems: 4 reg-names: oneOf: @@ -62,6 +57,14 @@ properties: - const: nvme - const: nvmmu - const: ans + - description: T6000. The NVMe controller and ANS2 co-processor + registers, then ANS's AXI2AF block and its storage-lane block + (ADT ans reg[4] and reg[5]). + items: + - const: nvme + - const: ans + - const: axi2af + - const: storage-lane resets: maxItems: 1 @@ -112,6 +115,29 @@ properties: adopts that RTKit session without renegotiating it. Shared firmware buffers must remain reserved and protected by the SART. + apple,tunable-axi2af: + $ref: /schemas/types.yaml#/definitions/uint32-matrix + description: + The ADT's afc-aiu-ans-dual-tunables for the axi2af block, as [offset, + mask, value] entries. A deep sleep resets the block; the driver puts + them back before ANS2 starts again. + items: + items: + - description: register offset + - description: mask of the bits to clear + - description: bits to set + + apple,tunable-storage-lane: + $ref: /schemas/types.yaml#/definitions/uint32-matrix + description: + The ADT's storage-lane-common-tunables for the storage-lane block, as + [offset, mask, value] entries, put back as apple,tunable-axi2af is. + items: + items: + - description: register offset + - description: mask of the bits to clear + - description: bits to set + apple,sleep-wake-delay-ms: description: T6050. After a deep sleep, the time from the storage power being back @@ -184,13 +210,35 @@ allOf: minItems: 3 reg-names: minItems: 3 - else: + + - if: + properties: + compatible: + not: + contains: + enum: + - apple,t6000-nvme-ans2 + - apple,t6041-nvme-ans2 + - apple,t6050-nvme-ans2 + - apple,t8132-nvme-ans2 + - apple,t8140-nvme-ans2 + then: properties: reg: maxItems: 2 reg-names: maxItems: 2 + - if: + properties: + compatible: + contains: + const: apple,t6000-nvme-ans2 + else: + properties: + apple,tunable-axi2af: false + apple,tunable-storage-lane: false + - if: properties: compatible: From 12642fa86bb4707b6244bc1f2de9e044455fcc30 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 01:59:29 +0100 Subject: [PATCH 14/72] nvme: apple: put ANS's sleep-reset tunables back from the device tree A T6000 deep sleep resets ANS's AXI2AF and storage-lane blocks, and the ANS firmware crashes right after its endpoint discovery without the settings the boot loader gave them at cold boot. Where the device tree gives both blocks (reg-names "axi2af" and "storage-lane") with their tunables (apple,tunable-axi2af and apple,tunable-storage-lane), map them and put the tunables back after a firmware-driven sleep, before ANS's run bit, together with the SART's power word as the first good start had it. Only words whose bits differ are written. A controller without them is left as before. After such a sleep ANS is started with one apple_rtkit_boot(), bounded by the handshake wait it already has. Signed-off-by: Andreas Fernandez --- drivers/nvme/host/Kconfig | 1 + drivers/nvme/host/apple.c | 90 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/drivers/nvme/host/Kconfig b/drivers/nvme/host/Kconfig index 971ae44868e60..ac67af8ec4df9 100644 --- a/drivers/nvme/host/Kconfig +++ b/drivers/nvme/host/Kconfig @@ -127,6 +127,7 @@ config NVME_APPLE depends on OF && BLOCK depends on APPLE_RTKIT && APPLE_SART depends on ARCH_APPLE || COMPILE_TEST + select APPLE_TUNABLE select BLK_INLINE_ENCRYPTION select NVME_CORE help diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index f79c0b647af12..fe80fbb591476 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -44,6 +44,7 @@ #include #include #include +#include #include #include #include @@ -225,6 +226,17 @@ struct apple_nvme_iod { unsigned int tcb_stale_stops; }; +/* + * Blocks outside ANS that a firmware sleep resets and whose ADT tunables its + * firmware needs: DT reg name and tunable property of each. + */ +static const char * const apple_nvme_sleep_blocks[][2] = { + { "axi2af", "apple,tunable-axi2af" }, + { "storage-lane", "apple,tunable-storage-lane" }, +}; + +#define APPLE_NVME_TUNABLE_BLOCKS ARRAY_SIZE(apple_nvme_sleep_blocks) + struct apple_nvme_hw { const struct apple_nvme_m5_policy *m5_sleep; bool has_lsq_nvmmu; @@ -293,6 +305,13 @@ struct apple_nvme { bool fw_sleep_idled; /* The SART power word as it was before that sleep */ u32 sart_power; + /* The blocks of apple_nvme_sleep_blocks, where the DT describes all of them */ + bool sleep_restore; + void __iomem *sleep_tunable_regs[APPLE_NVME_TUNABLE_BLOCKS]; + struct apple_tunable *sleep_tunables[APPLE_NVME_TUNABLE_BLOCKS]; + /* ... and the SART power word of the first good start, read at its end */ + u32 sart_power_boot; + bool sleep_boot_saved; bool sleep_failed; /* Run bit set after a sleep, and not yet reported on. */ bool sleep_restarting; @@ -2163,6 +2182,51 @@ static void apple_nvme_stop_crashed_session(struct apple_nvme *anv) anv->ctrl.ctrl_config = 0; } +/* Only the fields that differ: a block the sleep did not reset is left as it is. */ +static void apple_nvme_sleep_tunables(struct apple_nvme *anv) +{ + unsigned int i; + + for (i = 0; i < APPLE_NVME_TUNABLE_BLOCKS; i++) + apple_tunable_apply(anv->sleep_tunable_regs[i], anv->sleep_tunables[i]); +} + +/* + * ANS's AXI2AF and storage-lane blocks (ADT ans reg[4] and reg[5]) are set up with + * the ADT's tunables at cold boot. A firmware sleep resets them, and ANS's firmware + * crashes after its endpoint discovery without them. A controller whose DT gives + * both blocks with their tunables has them put back after such a sleep; one without + * them is left as before. + */ +static int apple_nvme_map_sleep_tunables(struct apple_nvme *anv, + struct platform_device *pdev) +{ + struct device_node *np = anv->dev->of_node; + unsigned int i; + + for (i = 0; i < APPLE_NVME_TUNABLE_BLOCKS; i++) + if (!of_property_present(np, apple_nvme_sleep_blocks[i][1])) + return 0; + + for (i = 0; i < APPLE_NVME_TUNABLE_BLOCKS; i++) { + struct resource *res; + + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, + apple_nvme_sleep_blocks[i][0]); + if (!res) + return -EINVAL; + anv->sleep_tunables[i] = + devm_apple_tunable_parse(anv->dev, np, apple_nvme_sleep_blocks[i][1], res); + if (IS_ERR(anv->sleep_tunables[i])) + return PTR_ERR(anv->sleep_tunables[i]); + anv->sleep_tunable_regs[i] = devm_ioremap_resource(anv->dev, res); + if (IS_ERR(anv->sleep_tunable_regs[i])) + return PTR_ERR(anv->sleep_tunable_regs[i]); + } + anv->sleep_restore = true; + return 0; +} + static bool apple_nvme_sleep_wait_on(struct apple_nvme *anv); static void apple_nvme_reset_work_impl(struct work_struct *work) @@ -2362,8 +2426,19 @@ static void apple_nvme_reset_work_impl(struct work_struct *work) * entries before starting ANS (no-op on cold boot when replay_valid is false). */ if (fw_sleep) { - if (readl(anv->mmio_nvme + APPLE_ANS_SART_POWER) != anv->sart_power) - writel(anv->sart_power, anv->mmio_nvme + APPLE_ANS_SART_POWER); + /* + * Where the DT gives them: the power cut also reset ANS's AXI2AF and + * storage-lane blocks, and its firmware crashes right after its endpoint + * discovery without their tunables. Put them back with the SART's power + * word as the first good start had it. + */ + u32 sart_power = anv->sleep_boot_saved ? + anv->sart_power_boot : anv->sart_power; + + if (readl(anv->mmio_nvme + APPLE_ANS_SART_POWER) != sart_power) + writel(sart_power, anv->mmio_nvme + APPLE_ANS_SART_POWER); + if (anv->sleep_boot_saved) + apple_nvme_sleep_tunables(anv); /* Only after a sleep that didn't happen: the wake wrote them already. */ if (anv->sart) apple_sart_restore_early(anv->sart); @@ -2655,6 +2730,11 @@ static void apple_nvme_reset_work_impl(struct work_struct *work) nvme_start_ctrl(&anv->ctrl); dev_dbg(anv->dev, "ANS boot and NVMe init completed."); + /* What a deep sleep takes from ANS is what its first good start had. */ + if (anv->sleep_restore && !anv->sleep_boot_saved) { + anv->sart_power_boot = readl(anv->mmio_nvme + APPLE_ANS_SART_POWER); + anv->sleep_boot_saved = true; + } return; out_remove_sq: @@ -3123,6 +3203,12 @@ static struct apple_nvme *apple_nvme_alloc(struct platform_device *pdev) anv->mmio_nvmmu = anv->mmio_nvme; } + ret = apple_nvme_map_sleep_tunables(anv, pdev); + if (ret) { + dev_err_probe(dev, ret, "bad sleep tunables\n"); + goto out_detach_genpd; + } + ret = apple_nvme_read_handoff(anv); if (ret) { dev_err_probe(dev, ret, "Invalid live firmware handoff\n"); From a7aa4a351b8e045620689b974f0ea07a14ae99ae Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Mon, 30 Nov 2026 23:33:39 +0100 Subject: [PATCH 15/72] PCI: apple: bring the T6000 internal ports back in the noirq resume A deep sleep of the M1 Pro takes the PCIe complex's power. Its internal ports, Wi-Fi and Bluetooth and the SD reader, came back in the normal resume phase, the T8103 path, after the PCI core had already restored the functions behind them in the noirq phase: a configuration or BAR access to a port without a link stalls the bus. Match apple,t6000-pcie with the T8103 layout and the sleep's noirq bring-up of the ports that lost their link, before their functions resume. Ports the boot loader has already set up again at the wake keep their link and are left alone. Keep probe's 100 ms after PERST# (PCIe r5.0, 6.6.1) before the PHY's ready bit is polled. Signed-off-by: Andreas Fernandez --- drivers/pci/controller/pcie-apple.c | 39 ++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 88f464309cf27..48e3fbe050fdd 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -323,6 +323,11 @@ struct hw_info { */ bool s2r_noirq; bool host_resumes_dart; + /* + * ... and waits 100 ms after PERST# there too, as probe does, before the + * PHY's ready bit is polled. + */ + bool s2r_perst_wait; }; static const struct hw_info t8103_hw = { @@ -336,6 +341,24 @@ static const struct hw_info t8103_hw = { .max_rid2sid = 64, }; +/* + * T6000 (M1 Pro): the T8103 layout. After a deep sleep the internal ports (Wi-Fi and + * Bluetooth, SD reader) come back in the noirq resume, before anything below them is + * touched, with probe's wait after PERST#. + */ +static const struct hw_info t6000_hw = { + .phy_lane_ctl = PHY_LANE_CTL, + .port_msiaddr = PORT_MSIADDR, + .port_msiaddr_hi = 0, + .port_refclk = PORT_REFCLK, + .port_perst = PORT_PERST, + .port_rid2sid = PORT_RID2SID, + .port_msimap = 0, + .max_rid2sid = 64, + .s2r_noirq = true, + .s2r_perst_wait = true, +}; + static const struct hw_info t602x_hw = { .phy_lane_ctl = 0, .port_msiaddr = PORT_T602X_MSIADDR, @@ -6729,6 +6752,12 @@ static int apple_pcie_probe(struct platform_device *pdev) hw = of_device_get_match_data(dev); if (!hw) return -ENODEV; + /* + * The T6000 data is the T8103 data plus the deep sleep's steps. Where that sleep is + * not described (T6001 and T6002 share the compatible), keep the T8103 path. + */ + if (hw == &t6000_hw && !apple_t6000_sleep_described()) + hw = &t8103_hw; /* * A tunneled PCIe-C port has no host GPIO for PERST#: the tunnel @@ -10090,9 +10119,9 @@ static void apple_pcie_hib_ports_phased(struct apple_pcie *pcie) usleep_range(100, 200); /* - * No wait after PERST#: these root ports go beyond 5 GT/s, so configuration requests - * wait for 100 ms after training instead (PCIe r5.0, 6.6.1), below. The PHY's ready bit - * has no interrupt. + * No wait after PERST# unless s2r_perst_wait: these root ports go beyond 5 GT/s, so + * configuration requests wait for 100 ms after training instead (PCIe r5.0, 6.6.1), + * below. The PHY's ready bit has no interrupt. */ list_for_each_entry(port, &pcie->ports, entry) { if (!port->hib_cold) @@ -10102,6 +10131,9 @@ static void apple_pcie_hib_ports_phased(struct apple_pcie *pcie) if (ret) apple_pcie_hib_port_failed(pcie, port, ret); } + /* 100 ms after PERST# (PCIe r5.0, 6.6.1), once for all of them */ + if (pcie->hw->s2r_perst_wait) + msleep(100); list_for_each_entry(port, &pcie->ports, entry) { if (!port->hib_cold) @@ -10521,6 +10553,7 @@ static const struct of_device_id apple_pcie_of_match[] = { { .compatible = "apple,t6020-pcie", .data = &t602x_hw }, { .compatible = "apple,t8140-pcie", .data = &t8140_hw }, { .compatible = "apple,t6050-pcie", .data = &t6050_hw }, + { .compatible = "apple,t6000-pcie", .data = &t6000_hw }, { .compatible = "apple,pcie", .data = &t8103_hw }, { } }; From 4b097f2d6c21515e6165b6a61644e7be5fc2cde2 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 09:54:21 +0100 Subject: [PATCH 16/72] PCI: apple: cut off a T6000 host whose port does not come back from sleep After a deep sleep of the M1 Pro the internal ports that lost their link are set up again in the noirq resume. When that set-up fails for a port (its GPIOs, its reference clock, or its PHY never getting ready), the functions behind the host would still resume, and Wi-Fi or the SD reader would touch their BARs behind a dead link. Then mark every function of the host disconnected, as after a surprise removal, and stop the DARTs its iommu-map points at from taking commands. A link that is set up but does not train in time is only reported, as before. Signed-off-by: Andreas Fernandez --- drivers/pci/controller/pcie-apple.c | 66 +++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 48e3fbe050fdd..a5bd9754341c6 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -328,6 +328,11 @@ struct hw_info { * PHY's ready bit is polled. */ bool s2r_perst_wait; + /* + * ... and a port that cannot be set up again there takes the host's functions with + * it: they are marked disconnected and the DARTs behind the host quiesced. + */ + bool s2r_lost_gone; }; static const struct hw_info t8103_hw = { @@ -357,6 +362,7 @@ static const struct hw_info t6000_hw = { .max_rid2sid = 64, .s2r_noirq = true, .s2r_perst_wait = true, + .s2r_lost_gone = true, }; static const struct hw_info t602x_hw = { @@ -757,6 +763,8 @@ struct apple_pcie { atomic_t hib_l1ss_pending_cnt; bool hib_l1ss_closing; bool hib_saved; + /* A cold port's set-up failed in this noirq bring-up (hw->s2r_lost_gone) */ + bool hib_lost; struct completion event; struct irq_fwspec fwspec; struct irq_domain *msi_domain; @@ -10061,8 +10069,10 @@ static int apple_pcie_hib_port_up(struct apple_pcie *pcie, struct apple_pcie_por int ret; ret = apple_pcie_setup_link(pcie, port, port->np); - if (ret) + if (ret) { + pcie->hib_lost = true; return ret; + } ret = apple_pcie_hib_port_start(pcie, port); if (ret) return ret; @@ -10085,6 +10095,42 @@ static void apple_pcie_hib_port_failed(struct apple_pcie *pcie, struct apple_pci port->hib_cold = false; } +/* ... because its set-up failed, rather than its link not training in time. */ +static void apple_pcie_hib_port_lost(struct apple_pcie *pcie, struct apple_pcie_port *port, + int ret) +{ + apple_pcie_hib_port_failed(pcie, port, ret); + pcie->hib_lost = true; +} + +/* Stop every DART the iommu-map of this host points at from touching hardware. */ +static void apple_pcie_quiesce_mapped_darts(struct apple_pcie *pcie) +{ + struct device_node *np = pcie->dev->of_node; + int n, i; + + n = of_property_count_u32_elems(np, "iommu-map"); + for (i = 0; n > 0 && i + 3 < n;) { + struct platform_device *pdev; + struct device_node *dart; + u32 phandle, cells = 1; + + if (of_property_read_u32_index(np, "iommu-map", i + 1, &phandle)) + break; + dart = of_find_node_by_phandle(phandle); + if (!dart) + break; + of_property_read_u32(dart, "#iommu-cells", &cells); + pdev = of_find_device_by_node(dart); + if (pdev) { + apple_dart_quiesce_commands(&pdev->dev); + put_device(&pdev->dev); + } + of_node_put(dart); + i += 3 + cells; + } +} + /* * apple_pcie_setup_link()'s bring-up of the cold ports taken in phases, so that the * power-on, PERST# and post-training waits run once for all of them rather than once @@ -10110,7 +10156,7 @@ static void apple_pcie_hib_ports_phased(struct apple_pcie *pcie) if (!ret) ret = apple_pcie_setup_refclk(pcie, port); if (ret < 0) - apple_pcie_hib_port_failed(pcie, port, ret); + apple_pcie_hib_port_lost(pcie, port, ret); } /* Tpvperl of at least 100 ms when powering up, Tperst-clk of 100 us otherwise */ if (powered) @@ -10129,7 +10175,7 @@ static void apple_pcie_hib_ports_phased(struct apple_pcie *pcie) rmw_set(PORT_PERST_OFF, port->base + pcie->hw->port_perst); ret = gpiod_set_value_cansleep(port->perst_gpio, 0); if (ret) - apple_pcie_hib_port_failed(pcie, port, ret); + apple_pcie_hib_port_lost(pcie, port, ret); } /* 100 ms after PERST# (PCIe r5.0, 6.6.1), once for all of them */ if (pcie->hw->s2r_perst_wait) @@ -10141,7 +10187,7 @@ static void apple_pcie_hib_ports_phased(struct apple_pcie *pcie) ret = readl_relaxed_poll_timeout(port->base + PORT_STATUS, stat, stat & PORT_STATUS_READY, 100, 250000); if (ret < 0) { - apple_pcie_hib_port_failed(pcie, port, ret); + apple_pcie_hib_port_lost(pcie, port, ret); continue; } reinit_completion(&port->hib_link); @@ -10199,6 +10245,7 @@ static void apple_pcie_hib_ports_up_noirq(struct apple_pcie *pcie) struct apple_pcie_port *port; int ret; + pcie->hib_lost = false; if (apple_pcie_hib_phased_ok(pcie)) { apple_pcie_hib_ports_phased(pcie); } else { @@ -10219,6 +10266,17 @@ static void apple_pcie_hib_ports_up_noirq(struct apple_pcie *pcie) port->hib_cold = false; } apple_pcie_hib_free_fns(pcie); + /* + * Nothing below this host may touch it then: its functions are cut off as after + * a surprise removal and its DARTs stop taking commands, rather than Wi-Fi or + * the SD reader resuming against a dead link. + */ + if (pcie->hw->s2r_lost_gone && pcie->hib_lost) { + dev_warn(pcie->dev, + "S2R: a port did not come back, the host's functions stay down\n"); + apple_pcie_tunnel_hierarchy_gone(pcie->dev); + apple_pcie_quiesce_mapped_darts(pcie); + } } /* The normal phase, after the SMC (the PWREN GPIOs are its) and before the functions. */ From bb447fc1897a07f3bbdf1cd3d41bc040882fb4e5 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 00:09:49 +0100 Subject: [PATCH 17/72] pmdomain: apple: pmp-report: set or clear any request bit for a sleep A sleep profile takes some blocks down itself, with interrupts off, after their drivers have suspended: the T6000 display, whose report entry is always on, is one. Let it set or clear one request bit whoever owns it, the T600x AGX vote's bit 9 too, under the report's lock, and poll the acknowledge while the PMP runs, telling the caller whether the bit was set before so that it can put it back when the sleep is given up. The books are left alone: what the owners asked for is what the report hands the PMP again after the sleep. Signed-off-by: Andreas Fernandez --- drivers/pmdomain/apple/pmp-report.c | 49 ++++++++++++++++++++++++++++ include/linux/soc/apple/pmp-report.h | 16 +++++++++ 2 files changed, 65 insertions(+) diff --git a/drivers/pmdomain/apple/pmp-report.c b/drivers/pmdomain/apple/pmp-report.c index d24ac7c4f0f1c..7f9cd2387d546 100644 --- a/drivers/pmdomain/apple/pmp-report.c +++ b/drivers/pmdomain/apple/pmp-report.c @@ -3257,6 +3257,55 @@ int apple_pmp_report_set_request(const struct device_node *report_np, unsigned i } EXPORT_SYMBOL_GPL(apple_pmp_report_set_request); +/** + * apple_pmp_report_sleep_request() - set or clear a request bit for a sleep, interrupts off + * @report_np: the report's node + * @id: the request bit + * @enable: set it, or clear it + * @timeout_us: how long to wait for the acknowledge to follow + * @was: if not NULL, whether the bit was requested before + * + * For a sleep profile, at its own steps with interrupts off: the bit is written whoever owns + * it, the T600x AGX vote's bit 9 included, under the report's lock. The books (@shadow) are + * left alone, so the owners' requests are what the report hands the PMP again after the + * sleep; the profile itself puts back a bit it cleared when a sleep is given up. The + * acknowledge is polled in 10 us steps whatever the report says of the PMP: one that does not + * answer within @timeout_us fails, and the sleep is not entered over it. + * + * Return: 0, -ENODEV with no report bound to @report_np, -EINVAL for an @id over 63, + * -ETIMEDOUT when the bit was written and the acknowledge did not follow. + */ +int apple_pmp_report_sleep_request(const struct device_node *report_np, unsigned int id, + bool enable, unsigned int timeout_us, bool *was) +{ + struct apple_pmp_report *rep; + unsigned long flags; + int ret = -ENODEV; + u64 bit, val; + + if (id > 63) + return -EINVAL; + bit = BIT_ULL(id); + rcu_read_lock(); + list_for_each_entry_rcu(rep, &apple_pmp_reports, list) { + if (rep->dev->of_node != report_np) + continue; + spin_lock_irqsave(&rep->lock, flags); + val = readq(rep->base + rep->offsets->tgt_read); + if (was) + *was = val & bit; + writeq(enable ? val | bit : val & ~bit, rep->base + rep->offsets->tgt_write); + spin_unlock_irqrestore(&rep->lock, flags); + ret = readq_poll_timeout_atomic(rep->base + rep->offsets->actual, val, + !!(val & bit) == enable, 10, timeout_us); + break; + } + rcu_read_unlock(); + + return ret; +} +EXPORT_SYMBOL_GPL(apple_pmp_report_sleep_request); + static int apple_pmp_report_entry_set_state(struct generic_pm_domain *genpd, bool enable) { struct apple_pmp_report_entry *ent = genpd_to_apple_pmp_report_entry(genpd); diff --git a/include/linux/soc/apple/pmp-report.h b/include/linux/soc/apple/pmp-report.h index d76d580cf3944..68bc4175e11e1 100644 --- a/include/linux/soc/apple/pmp-report.h +++ b/include/linux/soc/apple/pmp-report.h @@ -39,6 +39,15 @@ int apple_pmp_report_entry_withdraw_polled(const struct device_node *entry); */ int apple_pmp_report_set_request(const struct device_node *report_np, unsigned int id, bool enable, bool no_ack); +/* + * For a sleep profile's entry, interrupts off: set or clear request bit @id whatever owns it + * (the T600x AGX vote's bit 9 too), under the report's lock, then poll the acknowledge word + * (10 us steps, at most @timeout_us). @was (may be NULL) gets whether the + * bit was set before. Return 0, -ENODEV (no report bound to @report_np), -EINVAL (@id > 63), + * -ETIMEDOUT (written, not acknowledged). + */ +int apple_pmp_report_sleep_request(const struct device_node *report_np, unsigned int id, + bool enable, unsigned int timeout_us, bool *was); /* * T6001: the GPU driver reports each operation in flight. With the pmp_report.agx_vote parameter set, the AGX request (PS-REQ bit 9) is * set while any operation runs and cleared agx_vote_idle_ms after the last one ends. Parameter off: no effect. Both may be called from @@ -147,6 +156,13 @@ static inline int apple_pmp_report_set_request(const struct device_node *report_ return -ENODEV; } +static inline int apple_pmp_report_sleep_request(const struct device_node *report_np, + unsigned int id, bool enable, + unsigned int timeout_us, bool *was) +{ + return -ENODEV; +} + static inline void apple_pmp_floor_gpu_activity(bool busy) { } From 0a0b335cfe04effe6ddc690d03366c764c497ec1 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 00:10:43 +0100 Subject: [PATCH 18/72] soc: apple: pmp: withdraw the T600x requests and idle DVFS before the stop Before the M1 Pro's PMP is idled for a sleep, macOS's sleep action takes every request back, not only those of the blocks the report knows, sets the CPU and fabric DVFS entries (SOC-DEV-DVFS 0x108-0x10a and 0x10c) to idle and the report's sleep word to 1. Do the same at the sleep's commit, right before the idle, with interrupts off. At the wake the requests come back from the word kept when the PMP's suspend started, and the DVFS entries from what they read before the idle: left at idle, the PMP keeps the fabric clocks at their idle source. Other SoCs' reports are left as they were. Signed-off-by: Andreas Fernandez --- drivers/pmdomain/apple/pmp-report.c | 73 ++++++++++++++++++++++++++++ drivers/soc/apple/pmp-export.c | 2 + include/linux/soc/apple/pmp-report.h | 5 ++ 3 files changed, 80 insertions(+) diff --git a/drivers/pmdomain/apple/pmp-report.c b/drivers/pmdomain/apple/pmp-report.c index 7f9cd2387d546..f9430d9bbb809 100644 --- a/drivers/pmdomain/apple/pmp-report.c +++ b/drivers/pmdomain/apple/pmp-report.c @@ -143,6 +143,9 @@ struct apple_pmp_report { bool defer; /* The request word when a system sleep stopped the PMP. */ u64 sleep_request; + /* T600x: the DVFS entries apple_pmp_report_sleep_tail() idled, to put back at the wake. */ + u64 tail_dvfs[4]; + bool tail_done; /* The last SOC-DEV-DVFS word written to each entry, 0 if none. */ u64 *dvfs_shadow; unsigned int polls; @@ -625,6 +628,69 @@ void apple_pmp_report_sleep(struct device *pmp, bool at_commit) } EXPORT_SYMBOL_GPL(apple_pmp_report_sleep); +/* + * T600x: the CPU and fabric DVFS entries (SOC-DEV-DVFS 0x108-0x10a and 0x10c), read at 16 bytes + * a record and written through the update aperture, the idle value of each and the report's + * sleep word, as the PMP's sleep action leaves them. + */ +static const u16 apple_pmp_t600x_sleep_dvfs[] = { 0x108, 0x109, 0x10a, 0x10c }; +#define PMP_T600X_DVFS_READ(id) (0x10 * (id)) +#define PMP_T600X_DVFS_IDLE BIT_ULL(61) +#define PMP_T600X_SLEEP_WORD 0x18328 + +static void apple_pmp_t600x_dvfs_idle(struct apple_pmp_report *rep, unsigned int n) +{ + unsigned int i; + + for (i = 0; i < n; i++) + writeq(PMP_T600X_DVFS_IDLE, + rep->base + rep->offsets->update + 8 * apple_pmp_t600x_sleep_dvfs[i]); +} + +/** + * apple_pmp_report_sleep_tail() - a T600x PMP's last requests before a sleep stops it + * @pmp: the PMP device, still running + * + * Interrupts off, right before the PMP is idled at the sleep's commit, as macOS's sleep + * action does it: every request withdrawn, the CPU and fabric DVFS entries set to idle + * (twice, and the first three once more after the sleep word) and the sleep word to 1. + * The withdrawn requests come back from the word kept by apple_pmp_report_sleep(), the + * DVFS entries from what they read here. + */ +void apple_pmp_report_sleep_tail(struct device *pmp) +{ + const unsigned int n = ARRAY_SIZE(apple_pmp_t600x_sleep_dvfs); + struct apple_pmp_report *rep; + unsigned long flags; + unsigned int i; + u64 val; + + rcu_read_lock(); + list_for_each_entry_rcu(rep, &apple_pmp_reports, list) { + if (rep->offsets != &apple_pmp_offsets_t600x || !apple_pmp_report_of(rep, pmp)) + continue; + spin_lock_irqsave(&rep->lock, flags); + writeq(0, rep->base + rep->offsets->tgt_write); + spin_unlock_irqrestore(&rep->lock, flags); + /* macOS waits for the low half alone: the high one may stay acknowledged. */ + if (readq_poll_timeout_atomic(rep->base + rep->offsets->actual, val, + !lower_32_bits(val), 1000, 200000)) + dev_warn(rep->dev, "PMP still acknowledges %#llx at the sleep\n", val); + for (i = 0; i < n; i++) { + u32 reg = PMP_T600X_DVFS_READ(apple_pmp_t600x_sleep_dvfs[i]); + + rep->tail_dvfs[i] = readq(rep->base + reg); + } + apple_pmp_t600x_dvfs_idle(rep, n); + apple_pmp_t600x_dvfs_idle(rep, n); + writel(1, rep->base + PMP_T600X_SLEEP_WORD); + apple_pmp_t600x_dvfs_idle(rep, n - 1); + rep->tail_done = true; + } + rcu_read_unlock(); +} +EXPORT_SYMBOL_GPL(apple_pmp_report_sleep_tail); + /** * apple_pmp_report_wake() - hand the PMP every request after a system sleep * @pmp: the PMP device, running again @@ -640,6 +706,7 @@ void apple_pmp_report_wake(struct device *pmp, bool lost_power) unsigned long flags; unsigned int i; u64 val, want; + bool tail; mutex_lock(&apple_pmp_reports_lock); list_for_each_entry(rep, &apple_pmp_reports, list) { @@ -651,6 +718,12 @@ void apple_pmp_report_wake(struct device *pmp, bool lost_power) val = lost_power ? rep->sleep_request : readq(rep->base + offs->tgt_read); writeq(apple_pmp_request_word(val, rep->known, rep->shadow), rep->base + offs->tgt_write); + /* Left at idle, the PMP keeps the fabric clocks at their idle source. */ + tail = rep->tail_done; + for (i = 0; tail && i < ARRAY_SIZE(apple_pmp_t600x_sleep_dvfs); i++) + writeq(rep->tail_dvfs[i], + rep->base + offs->update + 8 * apple_pmp_t600x_sleep_dvfs[i]); + rep->tail_done = false; for (i = 0; rep->dvfs_shadow && i < offs->dvfs_count; i++) if (rep->dvfs_shadow[i]) writeq(rep->dvfs_shadow[i], diff --git a/drivers/soc/apple/pmp-export.c b/drivers/soc/apple/pmp-export.c index 87c5144392ebc..d4655f3a61c50 100644 --- a/drivers/soc/apple/pmp-export.c +++ b/drivers/soc/apple/pmp-export.c @@ -206,6 +206,8 @@ static int apple_pmp_commit_stop_fn(void *unused) size_t i; int ret; + /* A T600x PMP's last requests: withdrawn, DVFS at idle (pmp-report.c) */ + apple_pmp_report_sleep_tail(pmp_device); ret = apple_rtkit_sleep_idle_polled(c->rtk, c->control, &us); apple_rtkit_mailbox_recv_off(c->rtk); if (!c->control) diff --git a/include/linux/soc/apple/pmp-report.h b/include/linux/soc/apple/pmp-report.h index 68bc4175e11e1..53de41d5d4ece 100644 --- a/include/linux/soc/apple/pmp-report.h +++ b/include/linux/soc/apple/pmp-report.h @@ -29,6 +29,7 @@ int apple_pmp_report_check_t6030_ack(const struct device_node *entry); int apple_pmp_report_fast_die_effort(const struct device_node *np, unsigned int lane, u8 *effort); void apple_pmp_report_sleep(struct device *pmp, bool at_commit); +void apple_pmp_report_sleep_tail(struct device *pmp); void apple_pmp_report_wake(struct device *pmp, bool lost_power); int apple_pmp_report_entry_request(const struct device_node *entry, bool enable); int apple_pmp_report_entry_commit_owned(const struct device_node *entry, bool owned); @@ -79,6 +80,10 @@ static inline void apple_pmp_report_sleep(struct device *pmp, bool at_commit) { } +static inline void apple_pmp_report_sleep_tail(struct device *pmp) +{ +} + static inline void apple_pmp_report_wake(struct device *pmp, bool lost_power) { } From 492282ef397bf954fe112483c7f854bb42d250d6 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 00:11:27 +0100 Subject: [PATCH 19/72] soc: apple: pmp: answer a firmware that sets itself up again after a sleep A PMP started again from the SRAM it kept over a sleep may set itself up from the start: it asks for its IOVA table, sets its buffer and registers its ioreg entries again. That was refused as a second table or buffer, which left the firmware without an answer after the wake. Only after such a restart, answer a repeated table request with the table already mapped, take the new buffer, and begin a new ioreg list at the next registration. A firmware that does not ask again sees nothing different. Nothing sets the restart flag yet: the T6000 PMP's power cycle, added later in this series, does. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/pmp.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/drivers/soc/apple/pmp.rs b/drivers/soc/apple/pmp.rs index da82fcd9857be..be4495f5aa93c 100644 --- a/drivers/soc/apple/pmp.rs +++ b/drivers/soc/apple/pmp.rs @@ -147,6 +147,13 @@ struct PmpState { ioreg_ids: KVec, /// Held from a system sleep's start until its end. sram: Option, + /// The firmware was started again after a system sleep took its power. + restarted: bool, + /// After such a restart it asked for its IOVA table again: it sets itself up from the + /// start, its buffer included. + setup_again: bool, + /// ... and its first REGISTER_IOREG from then on begins a new list. + ioreg_new_list: bool, } impl PmpState { @@ -159,6 +166,9 @@ impl PmpState { ioreg_entries: KVec::with_capacity(340, GFP_KERNEL)?, ioreg_ids: KVec::with_capacity(340, GFP_KERNEL)?, sram: None, + restarted: false, + setup_again: false, + ioreg_new_list: false, }) } fn find_alloc(&self, addr: u64) -> Option { @@ -302,7 +312,15 @@ impl PmpData { } fn get_iova_table(&self) -> Result { let mut state = self.state.lock(); - if state.iova_table.is_some() { + if let Some(table) = &state.iova_table { + // A firmware restarted from its kept SRAM can set itself up again: the PIO + // ranges are still mapped from the first time. + if state.restarted { + let msg = (OPC_GET_IOVA_TABLE | OPC_ACK_MASK) << OPC_SHIFT | table.dma_address(); + state.setup_again = true; + state.ioreg_new_list = true; + return Ok(msg); + } dev_err!(self.dev, "Asked for iova table with existing buffer"); return Err(EIO); } @@ -401,7 +419,7 @@ impl PmpData { } fn set_buf(&self, addr: u64) -> Result { let mut state = self.state.lock(); - if state.value_buf.is_some() { + if state.value_buf.is_some() && !state.setup_again { dev_err!(self.dev, "Setting a buffer when one exists"); return Err(EIO); } @@ -422,6 +440,11 @@ impl PmpData { } fn register_ioreg(&self, addr: u64) -> Result { let mut state = self.state.lock(); + if state.ioreg_new_list { + state.ioreg_new_list = false; + state.ioreg_entries.clear(); + state.ioreg_ids.clear(); + } if !pmp_alloc::ioreg_fits(state.ioreg_entries.len()) { dev_err!( self.dev, From 7b77feba66e53fa357133347d9bde9afdde28e6c Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 00:12:33 +0100 Subject: [PATCH 20/72] dt-bindings: soc: apple: pmp: add the reset vector register A sleep that cuts the PMP's power resets the reset vector iBoot pointed at the firmware SRAM and locked. Let the PMP node name it as an optional third register, "iorvbar", so that the OS can put it back before the firmware starts again. Signed-off-by: Andreas Fernandez --- .../devicetree/bindings/soc/apple/apple,pmp.yaml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Documentation/devicetree/bindings/soc/apple/apple,pmp.yaml b/Documentation/devicetree/bindings/soc/apple/apple,pmp.yaml index 33c9d95e38243..4b2bfa130404c 100644 --- a/Documentation/devicetree/bindings/soc/apple/apple,pmp.yaml +++ b/Documentation/devicetree/bindings/soc/apple/apple,pmp.yaml @@ -48,9 +48,15 @@ properties: rather than claim every view as an exclusive resource. reg-names: + minItems: 2 items: - const: pmp - const: asc + - const: iorvbar + description: + The coprocessor's reset vector, which iBoot points at the firmware SRAM + and locks. A sleep that cuts its power resets it; the OS puts it back + before the firmware starts again. mboxes: maxItems: 1 @@ -179,7 +185,7 @@ allOf: else: properties: reg: - maxItems: 2 + maxItems: 3 power-domains: maxItems: 1 interrupts: false From c7202a27f84b27baf8a4f255a73844ca841bf13a Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 00:12:34 +0100 Subject: [PATCH 21/72] soc: apple: pmp: put the reset vector back after a sleep Where the DT names the PMP's reset vector, read what iBoot left there at probe, and after a sleep that cut the power write it back before the firmware starts again, unless it still reads locked. Without it the PMP starts from the reset value instead of its SRAM. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/pmp-export.c | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/drivers/soc/apple/pmp-export.c b/drivers/soc/apple/pmp-export.c index d4655f3a61c50..f3a408c7289f0 100644 --- a/drivers/soc/apple/pmp-export.c +++ b/drivers/soc/apple/pmp-export.c @@ -22,6 +22,38 @@ static struct device *pmp_device; /* The firmware is stopped for a system sleep. */ static bool pmp_asleep; +/* + * The reset vector (DT reg "iorvbar"), where one is described: iBoot points it at the + * firmware SRAM and locks it, and a sleep that cuts the PMP's power resets it. + */ +#define PMP_IORVBAR_LOCK BIT_ULL(0) +static void __iomem *pmp_iorvbar; +static u64 pmp_iorvbar_boot; + +static void apple_pmp_iorvbar_init(struct device *dev) +{ + int idx = of_property_match_string(dev->of_node, "reg-names", "iorvbar"); + + pmp_iorvbar = idx >= 0 ? of_iomap(dev->of_node, idx) : NULL; + if (pmp_iorvbar) + pmp_iorvbar_boot = readq(pmp_iorvbar); +} + +/* Before the firmware starts again: what iBoot left, unless it is still there, locked. */ +static void apple_pmp_iorvbar_restore(struct device *dev) +{ + u64 now; + + if (!pmp_iorvbar) + return; + now = readq(pmp_iorvbar); + if (now & PMP_IORVBAR_LOCK) + return; + writeq(pmp_iorvbar_boot, pmp_iorvbar); + dev_dbg(dev, "reset vector %#llx after the sleep, %#llx written back\n", now, + pmp_iorvbar_boot); +} + int apple_pmp_register(struct device *dev, const void *data) { int ret = 0; @@ -32,6 +64,7 @@ int apple_pmp_register(struct device *dev, const void *data) else { pmp_data = data; pmp_device = dev; + apple_pmp_iorvbar_init(dev); } return ret; } @@ -42,6 +75,9 @@ void apple_pmp_unregister(const void *data) if (pmp_data == data) { pmp_data = NULL; pmp_device = NULL; + if (pmp_iorvbar) + iounmap(pmp_iorvbar); + pmp_iorvbar = NULL; } } @@ -409,6 +445,7 @@ static int apple_pmp_resume_early(struct device *dev) } pmp_asleep = false; pmp_rx_held = false; + apple_pmp_iorvbar_restore(dev); ret = apple_pmp_wake(pmp_data); if (ret) dev_err(dev, "firmware did not come back from sleep: %d\n", ret); From 79a1aee61457df40c6f17ed63567b0b1b736f4d3 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 00:12:35 +0100 Subject: [PATCH 22/72] arm64: dts: apple: t6000: keep the PMP SRAM and reset vector over sleep A deep sleep cuts the M1 Pro PMP's power. Name its reset vector (reg[1] of the ADT's pmp node) and keep its whole firmware SRAM, 1 MiB, read after its stop and written back before it starts again. Signed-off-by: Andreas Fernandez --- arch/arm64/boot/dts/apple/t6000.dtsi | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/arm64/boot/dts/apple/t6000.dtsi b/arch/arm64/boot/dts/apple/t6000.dtsi index 07f20a5b445e2..2b77cd7974512 100644 --- a/arch/arm64/boot/dts/apple/t6000.dtsi +++ b/arch/arm64/boot/dts/apple/t6000.dtsi @@ -34,6 +34,16 @@ <0x2 0x10e70000 0x0 0x90000>, <0x2 0x11e70000 0x0 0x90000>, <0x2 0x12e70000 0x0 0x90000>; + /* + * A deep sleep cuts the PMP's power: its reset vector (reg[1] of the + * ADT's pmp node) and its whole firmware SRAM are put back before it + * starts again. + */ + reg = <0x2 0x8e700000 0x0 0x100000>, + <0x2 0x8ec00000 0x0 0x4000>, + <0x2 0x8e850000 0x0 0x8>; + reg-names = "pmp", "asc", "iorvbar"; + apple,sram-keep = <0x0 0x100000>; }; &gpu { From 712a596846faf5ec5f6631a4dc3391ec5998695f Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 00:13:23 +0100 Subject: [PATCH 23/72] iommu/apple-dart: keep a T6000 DART's address filter over a sleep too The M1 Pro's DARTs have the M1's address filter: 16 entries, 0x40 apart, the same words. A deep sleep clears it just the same. Keep what the loader programmed and write it back after the sleep for a T6000-layout DART that names its filter window, as for a T8020 one. No T6000 DART names one yet. Signed-off-by: Andreas Fernandez --- drivers/iommu/apple-dart.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index 20e82332ade01..6a0af70f689f9 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -6614,7 +6614,8 @@ static int apple_dart_probe(struct platform_device *pdev) ret = apple_dart_dapf_parse(pdev, dart); if (ret) return ret; - if (!dart->dapf_count && dart->hw->type == DART_T8020 && + if (!dart->dapf_count && + (dart->hw->type == DART_T8020 || dart->hw->type == DART_T6000) && platform_get_resource_byname(pdev, IORESOURCE_MEM, "dapf")) { /* Read back once the DART is powered, below. */ dart->dapf = devm_platform_ioremap_resource_byname(pdev, "dapf"); From 56c8d0bae0e535bdd78db338ef73b6d3bace5b6e Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 00:13:23 +0100 Subject: [PATCH 24/72] arm64: dts: apple: t6000: name the PMP DART's address filter Every access of the M1 Pro's PMP passes its DART's address filter (ADT dart-pmp reg[1]), which a deep sleep clears. Name the window, so that the DART driver keeps what the loader programmed and writes it back after the sleep, before the PMP starts again. Signed-off-by: Andreas Fernandez --- arch/arm64/boot/dts/apple/t6000.dtsi | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/arch/arm64/boot/dts/apple/t6000.dtsi b/arch/arm64/boot/dts/apple/t6000.dtsi index 2b77cd7974512..041e03565e5ec 100644 --- a/arch/arm64/boot/dts/apple/t6000.dtsi +++ b/arch/arm64/boot/dts/apple/t6000.dtsi @@ -46,6 +46,17 @@ apple,sram-keep = <0x0 0x100000>; }; +/* + * The PMP's DART filters every access of the PMP (ADT dart-pmp reg[1]). A deep + * sleep clears it: the DART driver keeps what the loader programmed and writes + * it back after the sleep, before the PMP starts again. + */ +&pmp_dart { + reg = <0x2 0x8e300000 0x0 0x4000>, + <0x2 0x8e304000 0x0 0x4000>; + reg-names = "dart", "dapf"; +}; + &gpu { compatible = "apple,agx-t6000", "apple,agx-g13x"; }; From 14914e912b955ceb0c24c5de28888026108f71b3 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 01:59:29 +0100 Subject: [PATCH 25/72] soc: apple: pmp: power-cycle the T6000 PMP for a deep sleep macOS takes the T6000 PMP's own power state and its SRAM's to 0 once the PMP's CPU has stopped, copying the SRAM in between, and the sleep's quiesce steps the memory clock down only once the PMP's power state reads 0: the PMP owns the memory clock while it has power. Leaving the power states up skipped that step on every sleep. At the commit's stop, after the AP and IOP idle, turn the outbox off, wait for the CPU to idle, clear RUN and lower both power states as the PMGR does, with the SRAM copied in between. A CPU that never idles is not powered down. The sleep driver gets no ASC for this PMP, since it would otherwise clear RUN on an unpowered block. At the wake, and after any sleep the stop ran in, bring the SRAM's power up, write the SRAM back, bring the PMP's power up, put the reset vector back and start the firmware cold: RTKit afresh, the outbox on, RUN, the boot, INIT if the firmware does not acknowledge IOP power by itself, and the PMP endpoint. A power state that does not come up ends the start there, with nothing written into its domain. The firmware then sets itself up again, which a restarted session answers. Everything it was given before stays allocated and mapped, as it runs from the kept SRAM and may still point there; each request after the restart gets a new buffer, and the allocation budget counts from the restart, so each sleep keeps one more set of the firmware's setup allocations. The PMP's DART goes down with it, as macOS saves and restores it around the power cut: its interrupt off and its state saved once RUN is clear, and written back, its interrupt on, before the reset vector and RUN. A PMP that does not start again gets its requests back, but not the DVFS entries and the acknowledge wait. Only a PMP whose node has the reset vector register and apple,sram-keep takes this path, and only from a deep sleep's commit; every other PMP keeps the warm restart. Signed-off-by: Andreas Fernandez --- drivers/pmdomain/apple/pmp-report.c | 44 +++- drivers/soc/apple/pmp-export.c | 336 +++++++++++++++++++++++++++ drivers/soc/apple/pmp.rs | 95 +++++++- include/linux/soc/apple/pmp-report.h | 5 + include/linux/soc/apple/pmp.h | 7 + 5 files changed, 475 insertions(+), 12 deletions(-) diff --git a/drivers/pmdomain/apple/pmp-report.c b/drivers/pmdomain/apple/pmp-report.c index f9430d9bbb809..7347057950742 100644 --- a/drivers/pmdomain/apple/pmp-report.c +++ b/drivers/pmdomain/apple/pmp-report.c @@ -691,16 +691,7 @@ void apple_pmp_report_sleep_tail(struct device *pmp) } EXPORT_SYMBOL_GPL(apple_pmp_report_sleep_tail); -/** - * apple_pmp_report_wake() - hand the PMP every request after a system sleep - * @pmp: the PMP device, running again - * @lost_power: the sleep reached the hardware, so the report may have lost its - * contents: bits no entry owns come from the word kept at the sleep - * - * The power requests go first and the performance requests after them, the - * order an entry uses when it powers on. - */ -void apple_pmp_report_wake(struct device *pmp, bool lost_power) +static void apple_pmp_report_requests_back(struct device *pmp, bool lost_power, bool started) { struct apple_pmp_report *rep; unsigned long flags; @@ -719,7 +710,7 @@ void apple_pmp_report_wake(struct device *pmp, bool lost_power) writeq(apple_pmp_request_word(val, rep->known, rep->shadow), rep->base + offs->tgt_write); /* Left at idle, the PMP keeps the fabric clocks at their idle source. */ - tail = rep->tail_done; + tail = rep->tail_done && started; for (i = 0; tail && i < ARRAY_SIZE(apple_pmp_t600x_sleep_dvfs); i++) writeq(rep->tail_dvfs[i], rep->base + offs->update + 8 * apple_pmp_t600x_sleep_dvfs[i]); @@ -731,6 +722,9 @@ void apple_pmp_report_wake(struct device *pmp, bool lost_power) want = rep->shadow & ~rep->no_ack; WRITE_ONCE(rep->defer, false); spin_unlock_irqrestore(&rep->lock, flags); + /* A PMP that did not start again moves no clock and acknowledges nothing. */ + if (!started) + continue; if (want && readq_poll_timeout(rep->base + offs->actual, val, (val & want) == want, 1000, 1000000)) @@ -740,8 +734,36 @@ void apple_pmp_report_wake(struct device *pmp, bool lost_power) mutex_unlock(&apple_pmp_reports_lock); } + +/** + * apple_pmp_report_wake() - hand the PMP every request after a system sleep + * @pmp: the PMP device, running again + * @lost_power: the sleep reached the hardware, so the report may have lost its + * contents: bits no entry owns come from the word kept at the sleep + * + * The power requests go first and the performance requests after them, the + * order an entry uses when it powers on. + */ +void apple_pmp_report_wake(struct device *pmp, bool lost_power) +{ + apple_pmp_report_requests_back(pmp, lost_power, true); +} EXPORT_SYMBOL_GPL(apple_pmp_report_wake); +/** + * apple_pmp_report_wake_unstarted() - the requests back for a T600x PMP that did not start again + * @pmp: the PMP device, its firmware not running after the sleep's power cut + * + * The request words go back as apple_pmp_report_wake() writes them, so its next start finds + * them. The DVFS entries the sleep's tail left at idle stay so, with no wait for the clocks + * and none for an acknowledge: no firmware answers. + */ +void apple_pmp_report_wake_unstarted(struct device *pmp) +{ + apple_pmp_report_requests_back(pmp, true, false); +} +EXPORT_SYMBOL_GPL(apple_pmp_report_wake_unstarted); + /* The unique, enabled and acknowledged T6030 display request (DISP, reg 7). */ static struct device_node *apple_pmp_report_t6030_entry(struct device_node *report) { diff --git a/drivers/soc/apple/pmp-export.c b/drivers/soc/apple/pmp-export.c index f3a408c7289f0..54695941c8daa 100644 --- a/drivers/soc/apple/pmp-export.c +++ b/drivers/soc/apple/pmp-export.c @@ -1,10 +1,16 @@ // SPDX-License-Identifier: GPL-2.0-only OR MIT +#include +#include #include #include +#include #include +#include #include #include #include +#include +#include #include #include #include @@ -54,6 +60,230 @@ static void apple_pmp_iorvbar_restore(struct device *dev) pmp_iorvbar_boot); } +/* + * T6000 power-cycles its PMP for a deep sleep, as macOS does: at the stop, once its + * CPU is idle, its own power state and then its SRAM's go to 0, the SRAM copied in between; + * at the start both come back up, the SRAM first, and the firmware boots from its reset vector + * and sets itself up again. The memory clock is the PMP's while it has power: the sleep's + * quiesce steps it down only once the PMP's power state reads 0. Its DART goes down and comes + * back with it, as macOS saves and restores it around the power cut: its interrupt off and + * its state saved once the CPU stopped, written back before the CPU runs again. + */ +#define PMP_PS_TARGET GENMASK(3, 0) +#define PMP_PS_ACTUAL GENMASK(7, 4) +#define PMP_PS_WAS_GATED GENMASK(9, 8) /* write one to clear */ +#define PMP_PS_DEV_DISABLE BIT(10) +#define PMP_PS_BUSY BIT(11) +#define PMP_PS_AUTO_MIN GENMASK(19, 16) +#define PMP_PS_AUTO_ENABLE BIT(28) +#define PMP_PS_BIT30 BIT(30) +#define PMP_PS_ACTIVE 0xf +/* 0x2ee00 ticks of 24 MHz */ +#define PMP_PS_TIMEOUT_US 8000 + +/* In the ASC, after the CPU control word */ +#define PMP_ASC_CPU_STATUS 4 +#define PMP_ASC_CPU_STATUS_IDLE GENMASK(1, 0) +#define PMP_ASC_CPU_RUN BIT(4) + +struct apple_pmp_ps { + void __iomem *reg; + u32 saved; +}; + +/* What the commit's stop left: the start after the sleep follows it. */ +enum apple_pmp_pc_state { + PMP_PC_NONE, + /* Its idle was refused: the firmware runs on. */ + PMP_PC_RUNNING, + /* Idled, its CPU never idle: not powered down, and it cannot boot again. */ + PMP_PC_IDLED, + /* Idled, stopped, both power states at 0. */ + PMP_PC_DOWN, +}; + +static struct { + struct apple_pmp_ps pmp, sram; + enum apple_pmp_pc_state state; + /* The PMP's DART (its iommus) and that DART's interrupt */ + struct device *dart; + int dart_irq; +} pmp_pc; + +static bool apple_pmp_pc_described(void) +{ + return pmp_pc.pmp.reg && pmp_pc.sram.reg && pmp_pc.dart; +} + +/* + * The DART's own runtime PM callbacks, called directly: its runtime PM state is the system + * sleep's by now, and the stop runs with interrupts off. + */ +static int apple_pmp_dart(bool up) +{ + const struct dev_pm_ops *pm = pmp_pc.dart->driver ? pmp_pc.dart->driver->pm : NULL; + int ret = -ENODEV; + + if (!up) { + /* One CPU, interrupts off: no handler of it can be running */ + disable_irq_nosync(pmp_pc.dart_irq); + if (pm && pm->runtime_suspend) + ret = pm->runtime_suspend(pmp_pc.dart); + return ret; + } + if (pm && pm->runtime_resume) + ret = pm->runtime_resume(pmp_pc.dart); + enable_irq(pmp_pc.dart_irq); + return ret; +} + +static void apple_pmp_dart_init(struct device *dev) +{ + struct device_node *np = of_parse_phandle(dev->of_node, "iommus", 0); + struct platform_device *pdev = np ? of_find_device_by_node(np) : NULL; + int irq; + + of_node_put(np); + if (!pdev) + return; + irq = platform_get_irq(pdev, 0); + if (irq < 0) { + put_device(&pdev->dev); + return; + } + pmp_pc.dart = &pdev->dev; + pmp_pc.dart_irq = irq; +} + +static void __iomem *apple_pmp_ps_map(struct device_node *ps_np) +{ + struct device_node *parent = of_get_parent(ps_np); + struct resource res; + void __iomem *reg = NULL; + u32 off; + + if (parent && !of_address_to_resource(parent, 0, &res) && + !of_property_read_u32(ps_np, "reg", &off)) + reg = ioremap_np(res.start + off, 4); + of_node_put(parent); + return reg; +} + +/* The PMP's power state (its power domain) and, next to it, the one labelled pms_sram. */ +static void apple_pmp_pc_init(struct device *dev) +{ + struct device_node *ps_np, *parent, *child; + const char *label; + + /* A PMP whose reset vector and SRAM the DT says survive its power cut */ + if (of_property_match_string(dev->of_node, "reg-names", "iorvbar") < 0 || + !of_property_present(dev->of_node, "apple,sram-keep")) + return; + ps_np = of_parse_phandle(dev->of_node, "power-domains", 0); + if (!ps_np) + return; + pmp_pc.pmp.reg = apple_pmp_ps_map(ps_np); + parent = of_get_parent(ps_np); + of_node_put(ps_np); + for_each_child_of_node(parent, child) { + if (!of_property_read_string(child, "label", &label) && + !strcmp(label, "pms_sram")) { + pmp_pc.sram.reg = apple_pmp_ps_map(child); + of_node_put(child); + break; + } + } + of_node_put(parent); + if (pmp_pc.pmp.reg && pmp_pc.sram.reg) + apple_pmp_dart_init(dev); + if (apple_pmp_pc_described()) + return; + if (pmp_pc.pmp.reg) + iounmap(pmp_pc.pmp.reg); + if (pmp_pc.sram.reg) + iounmap(pmp_pc.sram.reg); + memset(&pmp_pc, 0, sizeof(pmp_pc)); +} + +static void apple_pmp_pc_exit(void) +{ + if (pmp_pc.pmp.reg) + iounmap(pmp_pc.pmp.reg); + if (pmp_pc.sram.reg) + iounmap(pmp_pc.sram.reg); + if (pmp_pc.dart) + put_device(pmp_pc.dart); + memset(&pmp_pc, 0, sizeof(pmp_pc)); +} + +static int apple_pmp_ps_wait(struct apple_pmp_ps *ps, u32 mask, u32 val) +{ + u32 v; + + return readl_poll_timeout_atomic(ps->reg, v, (v & mask) == val, 1, PMP_PS_TIMEOUT_US); +} + +/* The PMGR waits out BUSY after each write of a lower target, as macOS does. */ +static int apple_pmp_ps_write(struct apple_pmp_ps *ps, u32 val) +{ + writel(val, ps->reg); + if (FIELD_GET(PMP_PS_TARGET, val) == PMP_PS_ACTIVE) + return 0; + return apple_pmp_ps_wait(ps, PMP_PS_BUSY, 0); +} + +/* The lowest state the PMGR may take the domain to by itself */ +static void apple_pmp_ps_set_auto_min(struct apple_pmp_ps *ps, u32 level) +{ + u32 r = readl(ps->reg); + + apple_pmp_ps_write(ps, (r & ~(PMP_PS_AUTO_MIN | PMP_PS_WAS_GATED)) | + FIELD_PREP(PMP_PS_AUTO_MIN, level)); +} + +/* A domain's target state: auto PM is left only from the active state */ +static int apple_pmp_ps_set_level(struct apple_pmp_ps *ps, u32 level, bool auto_enable) +{ + u32 r = readl(ps->reg), w; + int ret; + + if (FIELD_GET(PMP_PS_TARGET, r) == level && !!(r & PMP_PS_AUTO_ENABLE) == auto_enable && + !(r & (PMP_PS_BIT30 | PMP_PS_DEV_DISABLE))) + return 0; + if (r & PMP_PS_AUTO_ENABLE) { + if (FIELD_GET(PMP_PS_TARGET, r) != PMP_PS_ACTIVE) + return -EINVAL; + w = r & ~(PMP_PS_AUTO_ENABLE | PMP_PS_WAS_GATED); + apple_pmp_ps_write(ps, w); + apple_pmp_ps_wait(ps, PMP_PS_ACTUAL, FIELD_PREP(PMP_PS_ACTUAL, PMP_PS_ACTIVE)); + } else { + w = r & ~PMP_PS_WAS_GATED; + } + w = (w & ~PMP_PS_TARGET) | level; + apple_pmp_ps_write(ps, level == PMP_PS_ACTIVE ? w : w | PMP_PS_WAS_GATED); + ret = apple_pmp_ps_wait(ps, PMP_PS_ACTUAL, FIELD_PREP(PMP_PS_ACTUAL, level)); + if (auto_enable) { + w |= PMP_PS_AUTO_ENABLE; + apple_pmp_ps_write(ps, w); + } + if (w & PMP_PS_BIT30) { + w &= ~PMP_PS_BIT30; + apple_pmp_ps_write(ps, w); + } + if (w & PMP_PS_DEV_DISABLE) { + w &= ~PMP_PS_DEV_DISABLE; + apple_pmp_ps_write(ps, w); + } + return ret; +} + +/* As the PMGR powers a device up: its auto-min level, then active */ +static int apple_pmp_ps_power_up(struct apple_pmp_ps *ps) +{ + apple_pmp_ps_set_auto_min(ps, FIELD_GET(PMP_PS_AUTO_MIN, ps->saved)); + return apple_pmp_ps_set_level(ps, PMP_PS_ACTIVE, ps->saved & PMP_PS_AUTO_ENABLE); +} + int apple_pmp_register(struct device *dev, const void *data) { int ret = 0; @@ -65,6 +295,7 @@ int apple_pmp_register(struct device *dev, const void *data) pmp_data = data; pmp_device = dev; apple_pmp_iorvbar_init(dev); + apple_pmp_pc_init(dev); } return ret; } @@ -78,6 +309,7 @@ void apple_pmp_unregister(const void *data) if (pmp_iorvbar) iounmap(pmp_iorvbar); pmp_iorvbar = NULL; + apple_pmp_pc_exit(); } } @@ -234,6 +466,46 @@ EXPORT_SYMBOL_GPL(apple_pmp_t6030_gpu_ready); static struct apple_pmp_commit_stop pmp_commit; static bool pmp_commit_registered, pmp_commit_used, pmp_rx_held; +/* + * T6000, interrupts off: AP and IOP idle, the outbox off, the CPU idle and stopped, its DART + * down, then its power state to 0, the SRAM copied with the SRAM still powered, and the SRAM's + * power state to 0. A CPU that never goes idle is not powered down: without the power cut it + * cannot start again, and macOS panics there. + */ +static int apple_pmp_t6000_stop(struct apple_pmp_commit_stop *c) +{ + void __iomem *status = c->control + PMP_ASC_CPU_STATUS; + unsigned int us; + size_t i; + u32 st; + int ret; + + apple_rtkit_polled_begin(c->rtk); + ret = apple_rtkit_polled_ap_idle(c->rtk, &us); + if (!ret) + ret = apple_rtkit_polled_iop_idle(c->rtk, &us); + apple_rtkit_polled_end(c->rtk); + if (ret) { + pmp_pc.state = PMP_PC_RUNNING; + return ret; + } + pmp_pc.state = PMP_PC_IDLED; + apple_rtkit_mailbox_recv_off(c->rtk); + ret = readl_poll_timeout_atomic(status, st, st & PMP_ASC_CPU_STATUS_IDLE, 1000, 10000); + if (ret) + return ret; + writel(readl(c->control) & ~PMP_ASC_CPU_RUN, c->control); + apple_pmp_dart(false); + pmp_pc.pmp.saved = readl(pmp_pc.pmp.reg); + pmp_pc.sram.saved = readl(pmp_pc.sram.reg); + pmp_pc.state = PMP_PC_DOWN; + apple_pmp_ps_set_level(&pmp_pc.pmp, 0, false); + for (i = 0; c->sram && i < c->count; i++) + c->words[i] = readl_relaxed(c->sram + 4 * i); + apple_pmp_ps_set_level(&pmp_pc.sram, 0, false); + return 0; +} + /* Interrupts off, one CPU: what apple_pmp_sleep() does, through pointers taken before. */ static int apple_pmp_commit_stop_fn(void *unused) { @@ -244,6 +516,8 @@ static int apple_pmp_commit_stop_fn(void *unused) /* A T600x PMP's last requests: withdrawn, DVFS at idle (pmp-report.c) */ apple_pmp_report_sleep_tail(pmp_device); + if (apple_pmp_pc_described()) + return apple_pmp_t6000_stop(c); ret = apple_rtkit_sleep_idle_polled(c->rtk, c->control, &us); apple_rtkit_mailbox_recv_off(c->rtk); if (!c->control) @@ -294,6 +568,16 @@ static bool apple_pmp_commit_available(struct device *dev) if (pmp_commit_registered) return true; + /* + * A T6000 PMP's own stop clears its RUN bit and takes its power away: the sleep driver + * gets no ASC, which it would otherwise stop after it, unpowered. + */ + if (apple_pmp_pc_described()) { + if (apple_sleep_coproc_register(dev, NULL, false, apple_pmp_commit_stop_fn, NULL)) + return false; + pmp_commit_registered = true; + return true; + } /* * On T6030 macOS never reads ASC CPU_STATUS or clears CPU_CONTROL_RUN * at the PMP's commit stop (t29-mac-t331cap-8B9ClHbz, HV recs 1397438..1397459); @@ -405,6 +689,47 @@ static int apple_pmp_suspend_late(struct device *dev) return 0; } +/* + * T6000, after every sleep its stop ran in, given up or not: a PMP that was powered down starts + * cold, its SRAM's power and image first, then its own power, its DART, its reset vector and + * the boot. One whose CPU never went idle cannot boot again; one whose idle was refused runs on. + */ +static int apple_pmp_t6000_start(struct device *dev) +{ + enum apple_pmp_pc_state state = pmp_pc.state; + int ret; + + pmp_pc.state = PMP_PC_NONE; + if (state == PMP_PC_RUNNING) + return 0; + if (state == PMP_PC_IDLED) { + apple_rtkit_set_recv_enabled(pmp_commit.rtk, true); + return -EIO; + } + /* Nothing is written into a domain that did not come up */ + ret = apple_pmp_ps_power_up(&pmp_pc.sram); + if (!ret) + ret = apple_pmp_sram_restore(pmp_data); + if (!ret) + ret = apple_pmp_ps_power_up(&pmp_pc.pmp); + if (ret) { + /* The DART stays down; only its interrupt comes back, as it went */ + enable_irq(pmp_pc.dart_irq); + return ret; + } + apple_pmp_dart(true); + apple_pmp_iorvbar_restore(dev); + if (ret) + return ret; + /* A fresh RTKit state before RUN, so the HELLO cannot be lost */ + ret = apple_pmp_reinit(pmp_data); + if (ret) + return ret; + apple_rtkit_set_recv_enabled(pmp_commit.rtk, true); + writel(readl(pmp_commit.control) | PMP_ASC_CPU_RUN, pmp_commit.control); + return apple_pmp_boot(pmp_data); +} + static int apple_pmp_resume_early(struct device *dev) { int ret; @@ -434,6 +759,17 @@ static int apple_pmp_resume_early(struct device *dev) return 0; } } + if (pmp_pc.state != PMP_PC_NONE) { + pmp_asleep = false; + ret = apple_pmp_t6000_start(dev); + if (ret) { + dev_err(dev, "firmware did not come back from sleep: %d\n", ret); + apple_pmp_report_wake_unstarted(dev); + } else { + apple_pmp_report_wake(dev, true); + } + return 0; + } /* A stopped firmware doesn't restart with its power kept: the next real sleep does. */ if (!pm_resume_via_firmware()) { if (pmp_rx_held) { diff --git a/drivers/soc/apple/pmp.rs b/drivers/soc/apple/pmp.rs index be4495f5aa93c..513ba3ce134d8 100644 --- a/drivers/soc/apple/pmp.rs +++ b/drivers/soc/apple/pmp.rs @@ -116,6 +116,9 @@ const OPTIONAL_BOOTARGS: [(&[u8; 4], &CStr); 5] = [ struct PmpAllocation { addr: u64, alloc: Coherent<[u8]>, + /// Given before the firmware's last restart from its reset vector. It stays allocated and + /// mapped, as the firmware may still point there, but no longer counts in the budget. + held_over: bool, } struct IovaTableEntry { @@ -393,6 +396,7 @@ impl PmpData { PmpAllocation { addr: addr, alloc: iomem, + held_over: false, }, GFP_KERNEL, ) { @@ -405,7 +409,9 @@ impl PmpData { let mut state = self.state.lock(); if let Some(idx) = state.find_alloc(addr) { let freed = state.allocs.swap_remove(idx); - state.alloc_budget.release(freed.alloc.size() as u64); + if !freed.held_over { + state.alloc_budget.release(freed.alloc.size() as u64); + } } else { dev_err!( self.dev, @@ -699,6 +705,44 @@ impl PmpData { // and `control` is inside the ASC mapping held by `self`. to_result(unsafe { bindings::apple_rtkit_sleep_return(rtk, control) }) } + /// T6000, after a power cycle and with the CPU stopped: RTKit afresh, its buffers freed, as + /// for a firmware that boots from its reset vector. That firmware sets itself up again: its + /// IOVA table and buffer are asked for once more, and its registers make a new list. It + /// runs from the SRAM kept over the sleep and may still point into what it was given + /// before, so that stays allocated and mapped and is never handed out again; every new + /// request gets a new buffer. The budget counts from the restart, so the same setup fits + /// again after every sleep. + fn reinit(&self) -> Result { + let rtk = self.rtkit_handle()?; + // SAFETY: the handle stays valid until unbind, which can't run during a system sleep. + to_result(unsafe { bindings::apple_rtkit_reinit(rtk) })?; + let mut state = self.state.lock(); + state.restarted = true; + state.setup_again = true; + state.ioreg_new_list = true; + for a in state.allocs.iter_mut() { + a.held_over = true; + } + state.alloc_budget = AllocBudget::new(); + Ok(()) + } + /// ... and once its CPU runs: the boot, then the PMP endpoint. A restarted firmware + /// acknowledges IOP power by itself; it is asked for it if it does not. + fn boot(&self) -> Result { + let rtk = self.rtkit_handle()?; + // SAFETY: as in reinit(). + let ret = match to_result(unsafe { bindings::apple_rtkit_boot(rtk) }) { + Err(e) if e == ETIME => { + dev_warn!(self.dev, "no IOP power ack after restart, sending INIT\n"); + // SAFETY: as above. + to_result(unsafe { bindings::apple_rtkit_wake(rtk) }) + } + ret => ret, + }; + ret?; + // SAFETY: as above. + to_result(unsafe { bindings::apple_rtkit_start_ep(rtk, PMP_ENDPOINT) }) + } /// CONFIRM_IOREG's value for an UPDATE_IOREG: the length of the entry the firmware names by /// its own index, as registered. macOS 27.2 answers so on the M4 Mac mini (J773g), 8 for each /// of the four entries that image updates. An index that names no registered entry, which @@ -955,6 +999,55 @@ pub unsafe extern "C" fn apple_pmp_wake(data: *const core::ffi::c_void) -> i32 { } } +/// Writes the SRAM kept over a sleep back, with the firmware stopped. +/// +/// # Safety +/// `data` must point to the live PmpData registered by PmpDriver. +#[export] +pub unsafe extern "C" fn apple_pmp_sram_restore(data: *const core::ffi::c_void) -> i32 { + // SAFETY: The C bridge serializes calls against unregister in Drop. + let data = unsafe { &*data.cast::() }; + match data.sram_restore() { + Ok(()) => 0, + Err(error) => error.to_errno(), + } +} + +/// RTKit afresh for a firmware that starts from its reset vector after a power cycle. +/// +/// # Safety +/// `data` must point to the live PmpData registered by PmpDriver. +#[export] +pub unsafe extern "C" fn apple_pmp_reinit(data: *const core::ffi::c_void) -> i32 { + // SAFETY: The C bridge serializes calls against unregister in Drop. + let data = unsafe { &*data.cast::() }; + match data.reinit() { + Ok(()) => 0, + Err(error) => { + data.fail_display(error); + error.to_errno() + } + } +} + +/// Boots that firmware once its CPU runs, and starts the PMP endpoint. A firmware that doesn't +/// come back disables further power commands. +/// +/// # Safety +/// `data` must point to the live PmpData registered by PmpDriver. +#[export] +pub unsafe extern "C" fn apple_pmp_boot(data: *const core::ffi::c_void) -> i32 { + // SAFETY: The C bridge serializes calls against unregister in Drop. + let data = unsafe { &*data.cast::() }; + match data.boot() { + Ok(()) => 0, + Err(error) => { + data.fail_display(error); + error.to_errno() + } + } +} + struct NoBuffer; impl rtkit::Buffer for NoBuffer { fn iova(&self) -> Result { diff --git a/include/linux/soc/apple/pmp-report.h b/include/linux/soc/apple/pmp-report.h index 53de41d5d4ece..7fc16f6b048a8 100644 --- a/include/linux/soc/apple/pmp-report.h +++ b/include/linux/soc/apple/pmp-report.h @@ -31,6 +31,7 @@ int apple_pmp_report_fast_die_effort(const struct device_node *np, void apple_pmp_report_sleep(struct device *pmp, bool at_commit); void apple_pmp_report_sleep_tail(struct device *pmp); void apple_pmp_report_wake(struct device *pmp, bool lost_power); +void apple_pmp_report_wake_unstarted(struct device *pmp); int apple_pmp_report_entry_request(const struct device_node *entry, bool enable); int apple_pmp_report_entry_commit_owned(const struct device_node *entry, bool owned); int apple_pmp_report_entry_withdraw_polled(const struct device_node *entry); @@ -88,6 +89,10 @@ static inline void apple_pmp_report_wake(struct device *pmp, bool lost_power) { } +static inline void apple_pmp_report_wake_unstarted(struct device *pmp) +{ +} + static inline int apple_pmp_report_ane_available(const struct device_node *np) { return -EOPNOTSUPP; diff --git a/include/linux/soc/apple/pmp.h b/include/linux/soc/apple/pmp.h index f860b67ad78d7..1bbc8c3e86aaf 100644 --- a/include/linux/soc/apple/pmp.h +++ b/include/linux/soc/apple/pmp.h @@ -40,6 +40,13 @@ int apple_pmp_send_power_command(const void *data, u8 command, extern const struct dev_pm_ops apple_pmp_pm_ops; int apple_pmp_sleep(const void *data); int apple_pmp_wake(const void *data); +/* + * After a power cycle, with the CPU stopped: its SRAM back, then RTKit afresh (its CPU is + * started between the two), then the boot and the PMP endpoint. + */ +int apple_pmp_sram_restore(const void *data); +int apple_pmp_reinit(const void *data); +int apple_pmp_boot(const void *data); /* * What the sleep's last step needs to idle and stop the PMP with interrupts off: its RTKit and From b4bfb14f123636d349c47f63900e4f376a163c52 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Mon, 30 Nov 2026 23:39:07 +0100 Subject: [PATCH 26/72] soc: apple: sep: take the deep sleep answer wait from the SoC profile The enclave's sleep request is staged for the sleep core's commit with a 5 s wait for its answer, and the wait ends early once the power state reads DEV_DISABLE. On T6000 macOS waits up to 15 s for the answer, and only after the answer waits for the power state's actual field to read 0. An idle enclave that still runs can read the same as one that slept, so on T6000 only the answer ends the first wait. Add a sleep profile to the platform profiles: the answer wait and what ends the wait for the domain. Every SoC but T6000 keeps the current values. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/pm.rs | 10 ++++++++++ drivers/soc/apple/pm_shim.c | 34 +++++++++++++++++++++++++++------- drivers/soc/apple/profile.rs | 30 ++++++++++++++++++++++++++++++ drivers/soc/apple/sep.rs | 7 +++++-- drivers/soc/apple/shim.h | 3 ++- 5 files changed, 74 insertions(+), 10 deletions(-) diff --git a/drivers/soc/apple/pm.rs b/drivers/soc/apple/pm.rs index d479bf866ae4c..74a6f7954489d 100644 --- a/drivers/soc/apple/pm.rs +++ b/drivers/soc/apple/pm.rs @@ -48,6 +48,8 @@ extern "C" { msg1: u32, reply: u64, mask: u64, + answer_ms: u32, + flags: u32, ) -> c_int; fn sep_pm_commit_finish(dev: *mut c_void) -> c_int; pub(crate) static sep_pm_ops: kernel::bindings::dev_pm_ops; @@ -90,15 +92,21 @@ impl SleepGate { } } +/// `SEP_PM_GATE_ACTUAL` in `pm_shim.c`. +const COMMIT_GATE_ACTUAL: u32 = 1 << 0; + /// Stages `request` for the irreversible commit, with RX held until physical wake. /// Unsupported providers/transports refuse here; device suspend never sends SLEEP. +/// `sleep` says how long its answer may take and when the enclave is down after it. pub(crate) fn leave_for_commit( dev: &kernel::device::Device, mbox: *mut kernel::bindings::apple_mbox, request: kernel::soc::apple::mailbox::Message, reply: u64, mask: u64, + sleep: &crate::profile::SleepProfile, ) -> Result<()> { + let flags = if sleep.gate_actual { COMMIT_GATE_ACTUAL } else { 0 }; // SAFETY: `dev` is the bound SEP device and `mbox` its mailbox, which outlives the sleep: // `commit_finish` takes the request back at the resume. let rc = unsafe { @@ -109,6 +117,8 @@ pub(crate) fn leave_for_commit( request.msg1, reply, mask, + sleep.answer_ms, + flags, ) }; if rc == 0 { diff --git a/drivers/soc/apple/pm_shim.c b/drivers/soc/apple/pm_shim.c index a7d965603a5ae..afd8a534112da 100644 --- a/drivers/soc/apple/pm_shim.c +++ b/drivers/soc/apple/pm_shim.c @@ -213,6 +213,13 @@ void sep_pm_dart_restore(void *ptr) #define SEP_PM_POLL_US 20 #define SEP_PM_TIMEOUT_US (5000 * USEC_PER_MSEC) +/* + * Only the answer ends the wait for it, and after it the domain counts as down only once its + * actual state reads 0: what macOS waits for on T6000. Without the flag, DEV_DISABLE ends + * either wait. + */ +#define SEP_PM_GATE_ACTUAL BIT(0) + /* * .suspend only stages this request and holds RX. The platform sends it after * the last legitimate refusal. Once sent, only physical sleep/wake is supported; @@ -225,6 +232,8 @@ static struct sep_pm_commit { void __iomem *ps; struct apple_mbox_msg request; u64 reply, reply_mask; + unsigned int answer_max_us; + u32 flags; void (*rx)(struct apple_mbox *mbox, struct apple_mbox_msg msg, void *cookie); void *cookie; bool registered, sent, answered, gate_valid; @@ -313,8 +322,9 @@ 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) && - c->answer_us < SEP_PM_TIMEOUT_US) { + while (!c->answered && c->answer_us < c->answer_max_us && + ((c->flags & SEP_PM_GATE_ACTUAL) || + !((c->state = readl(c->ps)) & SEP_PS_DEV_DISABLE))) { ret = apple_mbox_poll_one(c->mbox); if (ret < 0) break; @@ -333,7 +343,8 @@ static int sep_pm_commit_stop(void *data) } /* Preserve the failure reason without another hardware query. */ if (ret >= 0 && !c->answered) - ret = c->state & SEP_PS_DEV_DISABLE ? -EHOSTDOWN : -ETIMEDOUT; + ret = !(c->flags & SEP_PM_GATE_ACTUAL) && (c->state & SEP_PS_DEV_DISABLE) ? + -EHOSTDOWN : -ETIMEDOUT; if (c->answered && c->reply_result) ret = -EREMOTEIO; /* Native cmsgSLEEP errors/abort/timeout are fatal, never cancellation. */ @@ -346,8 +357,13 @@ static int sep_pm_commit_stop(void *data) * the native contract, not proved atomic by the preceding PMGR sample. */ apple_mbox_set_recv_enabled(c->mbox, false); - c->gate_ret = readl_poll_timeout_atomic(c->ps, c->state, sep_ps_down(c->state), - SEP_PM_POLL_US, SEP_PM_TIMEOUT_US); + if (c->flags & SEP_PM_GATE_ACTUAL) + c->gate_ret = readl_poll_timeout_atomic(c->ps, c->state, + !FIELD_GET(SEP_PS_ACTUAL, c->state), + SEP_PM_POLL_US, SEP_PM_TIMEOUT_US); + else + c->gate_ret = readl_poll_timeout_atomic(c->ps, c->state, sep_ps_down(c->state), + SEP_PM_POLL_US, SEP_PM_TIMEOUT_US); c->gate_valid = true; if (c->gate_ret) panic("SEP SLEEP did not gate after send: %d state %#x", c->gate_ret, c->state); @@ -358,9 +374,11 @@ static int sep_pm_commit_stop(void *data) /* * From .suspend, with the enclave drained and its receive interrupt held: leaves the * request in @msg0 and @msg1 to the irreversible commit. Its answer is the message - * whose @mask bits read @reply. Unsupported paths must refuse without sending. + * whose @mask bits read @reply, waited for up to @answer_ms; @flags are SEP_PM_*. + * Unsupported paths must refuse without sending. */ -int sep_pm_commit_prepare(void *ptr, void *mbox, u64 msg0, u32 msg1, u64 reply, u64 mask) +int sep_pm_commit_prepare(void *ptr, void *mbox, u64 msg0, u32 msg1, u64 reply, u64 mask, + unsigned int answer_ms, u32 flags) { struct sep_pm_commit *c = &sep_pm_commit; struct device *dev = ptr; @@ -406,6 +424,8 @@ int sep_pm_commit_prepare(void *ptr, void *mbox, u64 msg0, u32 msg1, u64 reply, c->request.msg1 = msg1; c->reply = reply; c->reply_mask = mask; + c->answer_max_us = answer_ms * USEC_PER_MSEC; + c->flags = flags; c->sent = false; /* Resume may follow a pre-tell abort: observations belong to this attempt. */ c->answered = false; diff --git a/drivers/soc/apple/profile.rs b/drivers/soc/apple/profile.rs index 4c026d1bbea91..92627829bdc76 100644 --- a/drivers/soc/apple/profile.rs +++ b/drivers/soc/apple/profile.rs @@ -157,6 +157,29 @@ const DART_T8112: DartWindow = DartWindow { range: [0x0, 0x4000, 0x0, 0xffff_0000], }; +/// How the enclave goes to sleep for a deep sleep, and how it comes back. +#[derive(Clone, Copy)] +pub(crate) struct SleepProfile { + /// How long the enclave gets to answer its sleep request. + pub(crate) answer_ms: u32, + /// Only the answer ends the wait for it, and after it the domain is down once its actual + /// state reads 0; otherwise DEV_DISABLE ends either wait. + pub(crate) gate_actual: bool, +} + +/// Every SoC but T6000. +const SLEEP_DEFAULT: SleepProfile = SleepProfile { + answer_ms: crate::proto::SLEEP_TIMEOUT_MS, + gate_actual: false, +}; + +/// What macOS does on T6000 (AppleSEPManager): it waits 15 s for the answer and matches it by +/// its tag, and polls the actual state to 0 after it. +const SLEEP_T6000: SleepProfile = SleepProfile { + answer_ms: 15000, + gate_actual: true, +}; + pub(crate) struct PlatformProfile { pub(crate) name: &'static str, /// Capacity of the boot shared-memory window. The layout is still checked @@ -188,6 +211,8 @@ pub(crate) struct PlatformProfile { /// The `xart_guard` level when the parameter is left at its default (see /// [`crate::xart_guard`]). pub(crate) xart_guard: u8, + /// The enclave's deep sleep (see [`SleepProfile`]). + pub(crate) sleep: SleepProfile, } pub(crate) const T8103: PlatformProfile = PlatformProfile { @@ -226,6 +251,7 @@ pub(crate) const T8103: PlatformProfile = PlatformProfile { // Off: Touch ID setup on a cold-boot SEP rewrites a session record that // predates Linux, so the guard would only block it. xart_guard: 0, + sleep: SLEEP_DEFAULT, }; /// M1 Pro (J316s). The M1 family cold-boots its SEP like T8103, so the boot @@ -267,6 +293,7 @@ pub(crate) const T6000: PlatformProfile = PlatformProfile { // As T8103. On J316s, creating the identity keybag also rewrites a // session record that predates Linux. xart_guard: 0, + sleep: SLEEP_T6000, }; pub(crate) const T6020: PlatformProfile = PlatformProfile { @@ -300,6 +327,7 @@ pub(crate) const T6020: PlatformProfile = PlatformProfile { nonce_session: false, // A warm-attach enclave shared with macOS, as on T8112. xart_guard: 1, + sleep: SLEEP_DEFAULT, }; /// M2 Pro/Max on system firmware before 26.3. Their enclave refuses the @@ -356,6 +384,7 @@ pub(crate) const T8140: PlatformProfile = PlatformProfile { // Linux records from before the guard's list exist here, and Linux has // left the macOS records unchanged on this platform. xart_guard: 0, + sleep: SLEEP_DEFAULT, }; /// MacBook Air M2 (J413, J415). iBoot leaves the SEP running and the Mesa @@ -401,6 +430,7 @@ pub(crate) const T8112: PlatformProfile = PlatformProfile { nonce_session: false, // Guarded from its first Linux boot (2026-10-16): no record refused. xart_guard: 1, + sleep: SLEEP_DEFAULT, }; /// MacBook Air M3 (J613, J615). The J613 and J615 ADTs (26.6.2, 25G83) are diff --git a/drivers/soc/apple/sep.rs b/drivers/soc/apple/sep.rs index a812a88bc73f6..391114fd27269 100644 --- a/drivers/soc/apple/sep.rs +++ b/drivers/soc/apple/sep.rs @@ -1892,13 +1892,16 @@ impl SepData { /// Stage without sending; the platform's commit owns SLEEP with IRQs off. fn sleep_leave_for_commit(&self) -> Result<()> { let request = proto::encode_control(&proto::op_sleep(), proto::TAG_SLEEP); + let sleep = &self.profile.sleep; + let mask = proto::SLEEP_REPLY_MASK; match self.mbox.lock().as_ref() { Some(mbox) => pm::leave_for_commit( &self.dev, mbox.as_raw(), request, - proto::SLEEP_REPLY, - proto::SLEEP_REPLY_MASK, + proto::SLEEP_REPLY & mask, + mask, + sleep, ), None => Err(ENODEV), }?; diff --git a/drivers/soc/apple/shim.h b/drivers/soc/apple/shim.h index 20ab55c32ef94..7f2213348f7bd 100644 --- a/drivers/soc/apple/shim.h +++ b/drivers/soc/apple/shim.h @@ -35,7 +35,8 @@ int sep_pm_clear_dev_disable(void *dev); void sep_pm_dart_restore(void *dev); int sep_pm_dart_prepare(void *dev); bool sep_pm_platform_commit(void); -int sep_pm_commit_prepare(void *dev, void *mbox, u64 msg0, u32 msg1, u64 reply, u64 mask); +int sep_pm_commit_prepare(void *dev, void *mbox, u64 msg0, u32 msg1, u64 reply, u64 mask, + unsigned int answer_ms, u32 flags); int sep_pm_commit_finish(void *dev); extern const struct dev_pm_ops sep_pm_ops; From 821ce58daa90b6b44623ccd4bcddf9bdbe906afa Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Mon, 30 Nov 2026 23:39:32 +0100 Subject: [PATCH 27/72] soc: apple: sep: resume the T6000 enclave through its SEPROM After a deep sleep that cut the SoC's power, the T6000 enclave does not start again on a doorbell. macOS powers its domain on, at its PMGR's wake, with the minimum state raised and DEV_DISABLE cleared, makes the enclave's DART active again, and then lets the SEPROM resume the sepOS still in RAM through the boot endpoint: TZ0 (0x5), and once the ROM reports its status (0xd2), op 8 in place of the firmware, answered by 0x6c. Do the same on the SoCs whose sleep profile asks for it, T6000 only, after a deep sleep whose request the sleep core's commit sent. A control message that reports sepOS active also ends the wait. If the resume fails, the enclave is left as asleep, its receive interrupt held and its DART gated, and the raised minimum is dropped either way. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/pm.rs | 30 ++++++++ drivers/soc/apple/pm_shim.c | 73 +++++++++++++++++++ drivers/soc/apple/profile.rs | 11 ++- drivers/soc/apple/proto.rs | 8 +++ drivers/soc/apple/sep.rs | 134 ++++++++++++++++++++++++++++++++++- drivers/soc/apple/shim.h | 3 + 6 files changed, 256 insertions(+), 3 deletions(-) diff --git a/drivers/soc/apple/pm.rs b/drivers/soc/apple/pm.rs index 74a6f7954489d..b221625c8a9cd 100644 --- a/drivers/soc/apple/pm.rs +++ b/drivers/soc/apple/pm.rs @@ -52,6 +52,9 @@ extern "C" { flags: u32, ) -> c_int; fn sep_pm_commit_finish(dev: *mut c_void) -> c_int; + fn sep_pm_power_on(dev: *mut c_void) -> c_int; + fn sep_pm_power_release(dev: *mut c_void); + fn sep_pm_dart_gate(dev: *mut c_void) -> c_int; pub(crate) static sep_pm_ops: kernel::bindings::dev_pm_ops; } @@ -181,6 +184,33 @@ pub(crate) fn clear_dev_disable(dev: &kernel::device::Device) -> Result<()> { Ok(()) } +/// Powers the enclave's domain on after a deep sleep: its minimum state raised, DEV_DISABLE +/// cleared. The minimum stays raised until [`power_release`], also on failure. +pub(crate) fn power_on(dev: &kernel::device::Device) -> Result<()> { + // SAFETY: `dev` is the bound SEP device. + let rc = unsafe { sep_pm_power_on(dev.as_raw().cast()) }; + if rc != 0 { + return Err(Error::from_errno(rc)); + } + Ok(()) +} + +/// Drops the minimum state [`power_on`] raised. +pub(crate) fn power_release(dev: &kernel::device::Device) { + // SAFETY: `dev` is the bound SEP device. + unsafe { sep_pm_power_release(dev.as_raw().cast()) }; +} + +/// Keeps the DART driver off the enclave's DART after a wake that failed. +pub(crate) fn dart_gate(dev: &kernel::device::Device) -> Result<()> { + // SAFETY: `dev` is the bound SEP device. + let rc = unsafe { sep_pm_dart_gate(dev.as_raw().cast()) }; + if rc != 0 { + return Err(Error::from_errno(rc)); + } + Ok(()) +} + /// Attaches every power domain of a multi-domain SEP node and keeps them on /// across system sleep; see `pm_shim.c`. Must run before the enclave is first /// spoken to. diff --git a/drivers/soc/apple/pm_shim.c b/drivers/soc/apple/pm_shim.c index afd8a534112da..15091243e1079 100644 --- a/drivers/soc/apple/pm_shim.c +++ b/drivers/soc/apple/pm_shim.c @@ -36,6 +36,8 @@ /* The state word of a PMGR power domain, as pmgr-pwrstate has it. */ #define SEP_PS_DEV_DISABLE BIT(10) #define SEP_PS_ACTUAL GENMASK(7, 4) +#define SEP_PS_FLAGS (BIT(8) | BIT(9)) +#define SEP_PS_MIN GENMASK(19, 16) /* * After its sleep request, the enclave has gone down: DEV_DISABLE where its firmware sets @@ -539,6 +541,77 @@ const struct dev_pm_ops sep_pm_ops = { .suspend_noirq = pm_sleep_ptr(sep_pm_suspend_noirq), }; +/* + * macOS's wake raises the minimum state of the enclave's power state to on and + * waits up to 8 ms for the domain; the target is left as it is. A deep sleep also leaves + * DEV_DISABLE set, which is cleared as a reset deassert does, before anything reads the + * mailbox or the DART. The status bits 8 and 9 are written as 0, which leaves them as they + * are. The minimum stays raised until sep_pm_power_release(). + */ +int sep_pm_power_on(void *ptr) +{ + struct device *dev = ptr; + struct regmap *map; + u32 off, val = 0, raised; + int ret; + + ret = sep_pm_domain(dev, &map, &off); + if (ret) + return ret; + + regmap_update_bits(map, off, SEP_PS_FLAGS | SEP_PS_MIN, SEP_PS_MIN); + ret = regmap_read_poll_timeout(map, off, val, + FIELD_GET(SEP_PS_ACTUAL, val) == 0xf, 100, 20000); + raised = val; + if (ret && (val & SEP_PS_DEV_DISABLE)) { + regmap_update_bits(map, off, SEP_PS_FLAGS | SEP_PS_DEV_DISABLE, 0); + ret = regmap_read_poll_timeout(map, off, val, + FIELD_GET(SEP_PS_ACTUAL, val) == 0xf, 100, 20000); + } + if (!ret && (val & SEP_PS_DEV_DISABLE)) { + regmap_update_bits(map, off, SEP_PS_FLAGS | SEP_PS_DEV_DISABLE, 0); + regmap_read(map, off, &val); + } + if (!ret && (FIELD_GET(SEP_PS_ACTUAL, val) != 0xf || (val & SEP_PS_DEV_DISABLE))) + ret = -EIO; + if (ret) { + /* The word with the minimum raised, and the last one read: which bits stayed */ + dev_warn(dev, "the enclave's domain did not come on: state %#x, then %#x (%d)\n", + raised, val, ret); + sep_pm_power_release(ptr); + } + + return ret; +} + +void sep_pm_power_release(void *ptr) +{ + struct device *dev = ptr; + struct regmap *map; + u32 off; + + if (sep_pm_domain(dev, &map, &off)) + return; + regmap_update_bits(map, off, SEP_PS_FLAGS | SEP_PS_MIN, 0); +} + +/* + * Keeps the DART driver off a DART whose domain the enclave left off, as macOS takes the + * mapper inactive: after a wake that failed to start the enclave again. + */ +int sep_pm_dart_gate(void *ptr) +{ + struct device *dart = sep_pm_dart(ptr); + int ret; + + if (!dart) + return -ENODEV; + ret = apple_dart_quiesce_commands(dart); + put_device(dart); + + return ret; +} + int sep_pm_register(void *ptr, void (*event)(bool entering), int (*sleep)(void), void (*wake)(void), bool (*told)(void)) { diff --git a/drivers/soc/apple/profile.rs b/drivers/soc/apple/profile.rs index 92627829bdc76..93df4647b8d7d 100644 --- a/drivers/soc/apple/profile.rs +++ b/drivers/soc/apple/profile.rs @@ -165,19 +165,26 @@ pub(crate) struct SleepProfile { /// Only the answer ends the wait for it, and after it the domain is down once its actual /// state reads 0; otherwise DEV_DISABLE ends either wait. pub(crate) gate_actual: bool, + /// After the power cut the SEPROM resumes sepOS from RAM: the domain is powered on with + /// its minimum state raised, and the enclave is asked through its boot endpoint (TZ0, + /// then op 8, answered by 0x6c). Otherwise a doorbell wakes it. + pub(crate) seprom_resume: bool, } /// Every SoC but T6000. const SLEEP_DEFAULT: SleepProfile = SleepProfile { answer_ms: crate::proto::SLEEP_TIMEOUT_MS, gate_actual: false, + seprom_resume: false, }; -/// What macOS does on T6000 (AppleSEPManager): it waits 15 s for the answer and matches it by -/// its tag, and polls the actual state to 0 after it. +/// What macOS does on T6000 (AppleSEPManager, AppleSEPBooter, AppleT6000PMGR): it waits 15 s +/// for the answer and matches it by its tag, polls the actual state to 0 after it, and resumes +/// sepOS through the SEPROM after the wake. const SLEEP_T6000: SleepProfile = SleepProfile { answer_ms: 15000, gate_actual: true, + seprom_resume: true, }; pub(crate) struct PlatformProfile { diff --git a/drivers/soc/apple/proto.rs b/drivers/soc/apple/proto.rs index 1bdea86a4c51e..f0a787591407a 100644 --- a/drivers/soc/apple/proto.rs +++ b/drivers/soc/apple/proto.rs @@ -29,6 +29,10 @@ pub(crate) const MSG_SET_SHMEM: u64 = 0x18; pub(crate) const MSG_BOOT_TZ0_ACK1: u64 = 0x69; pub(crate) const MSG_BOOT_TZ0_ACK2: u64 = 0xd2; pub(crate) const MSG_BOOT_IMG4_ACK: u64 = 0x6a; +// The enclave's boot without firmware: op 8 in place of the IMG4 tells the SEPROM to +// resume the sepOS still in RAM; it answers 0x6c ("SEP resumed from RAM"). +pub(crate) const MSG_BOOT_RESUME: u64 = 0x8; +pub(crate) const MSG_BOOT_RESUMED: u64 = 0x6c; // 4 KiB units even though CPU pages are 16 KiB pub(crate) const IOVA_SHIFT: u32 = 12; @@ -87,6 +91,10 @@ pub(crate) fn shmem_registration(iova: u64, size: usize) -> Result { } pub(crate) const CONTROL_REPLY_TYPE: u8 = 0x01; +/// Control messages (tag 0) with which a resumed enclave reports its OS running again. +pub(crate) const CONTROL_OS_ACTIVE: u8 = 0x0d; +/// The same report, in the other form the enclave sends it. +pub(crate) const CONTROL_OS_ACTIVE_2: u8 = 0x15; pub(crate) const TAG_ENTROPY: u8 = 0xE7; diff --git a/drivers/soc/apple/sep.rs b/drivers/soc/apple/sep.rs index 391114fd27269..3d0a7219fd1a2 100644 --- a/drivers/soc/apple/sep.rs +++ b/drivers/soc/apple/sep.rs @@ -747,6 +747,8 @@ struct SepData { /// The enclave didn't answer a sleep request or a wake this boot: no deep sleep until a /// reboot, as a commit over a running enclave resets the SoC. sleep_unanswered: Atomic, + /// Where the SEPROM resume after a deep sleep is (`RESUME_*`). + resume_state: Atomic, #[pin] ool_xarm: Mutex>, @@ -1321,6 +1323,7 @@ impl SepData { sleep_rx_held: Atomic::new(false), sleep_at_suspend: Atomic::new(false), sleep_unanswered: Atomic::new(false), + resume_state: Atomic::new(Self::RESUME_IDLE), ool_xarm <- new_mutex!(Some(ool_xarm)), ool_sbio <- new_mutex!(Some(ool_sbio)), ool_sks <- new_mutex!(Some(ool_sks)), @@ -1486,6 +1489,10 @@ impl SepData { /// Handle a message from the boot endpoint during the cold-boot handshake. fn on_boot(&self, msg: Message) { let ty = (msg.msg0 >> proto::MSG_TYPE_SHIFT) & 0xff; + if self.resume_state.load(Relaxed) != Self::RESUME_IDLE { + self.on_boot_resume(msg, ty); + return; + } match ty { proto::MSG_BOOT_TZ0_ACK1 => { dev_info_ratelimited!(self.dev, "boot: first TZ0 acknowledgement\n"); @@ -1973,7 +1980,7 @@ impl SepData { self.set_sleep_gate(pm::SleepGate::Lost); return false; } - let answered = self.sleep_kick(); + let answered = self.sleep_wake(); match answered { Ok(()) => { self.set_sleep_gate(pm::SleepGate::Open); @@ -1994,6 +2001,121 @@ impl SepData { } } + /// Starts the enclave after a real sleep, as the SoC's [`profile::SleepProfile`] says. + fn sleep_wake(&self) -> Result<()> { + if self.profile.sleep.seprom_resume { + self.sleep_resume() + } else { + self.sleep_kick() + } + } + + const RESUME_IDLE: u32 = 0; + const RESUME_TZ0: u32 = 1; + const RESUME_SENT: u32 = 2; + const RESUME_DONE: u32 = 3; + const RESUME_FAILED: u32 = 4; + + /// How long the SEPROM gets to resume sepOS, in 10 ms steps. + const RESUME_POLLS: u32 = 1000; + + /// The enclave's wake from a deep sleep as macOS takes it (T6000). The domain is powered + /// on as macOS's wake does it and the DART restored, then the SEPROM resumes + /// sepOS from RAM: TZ0, and once the ROM reports its status, op 8 in place of the IMG4, + /// answered by 0x6c. On failure the enclave is left as asleep and its DART gated. + fn sleep_resume(&self) -> Result<()> { + if self.shutting_down.load(Relaxed) { + return Err(ENODEV); + } + pm::power_on(&self.dev)?; + let ret = self.sleep_resume_handshake(); + self.resume_state.store(Self::RESUME_IDLE, Relaxed); + if ret.is_err() { + if let Some(mbox) = self.mbox.lock().as_ref() { + mbox.set_recv_enabled(false); + } + self.sleep_hold_rx(); + if let Err(e) = pm::dart_gate(&self.dev) { + dev_err_ratelimited!(self.dev, "could not gate the DART again ({:?})\n", e); + } + } + pm::power_release(&self.dev); + ret + } + + fn sleep_resume_handshake(&self) -> Result<()> { + pm::dart_restore(&self.dev); + self.resume_state.store(Self::RESUME_TZ0, Relaxed); + self.sleep_release_rx(); + // sepOS stays silent until the boot endpoint rings, and the ROM answers TZ0 within + // milliseconds: ring at once, as macOS's boot of the enclave does. + match self.mbox.lock().as_ref() { + Some(mbox) => { + mbox.set_recv_enabled(true); + let msg = Message { + msg0: u64::from(proto::EP_BOOT) + | (proto::MSG_BOOT_TZ0 << proto::MSG_TYPE_SHIFT), + msg1: 0, + }; + Self::ring(mbox, msg)?; + } + None => return Err(ENODEV), + } + for _ in 0..Self::RESUME_POLLS { + match self.resume_state.load(Relaxed) { + Self::RESUME_DONE => return Ok(()), + Self::RESUME_FAILED => return Err(EIO), + _ => kernel::time::delay::fsleep(kernel::time::Delta::from_millis(10)), + } + } + dev_err_ratelimited!( + self.dev, + "no answer from the SEPROM within 10 s (state {})\n", + self.resume_state.load(Relaxed) + ); + Err(ETIMEDOUT) + } + + /// A boot-endpoint message during [`Self::sleep_resume`], read as + /// macOS reads it: 0x69 accepts TZ0, 0xd2 carries the ROM status in + /// its data, 0x6c reports sepOS resumed from RAM. + fn on_boot_resume(&self, msg: Message, ty: u64) { + match ty { + proto::MSG_BOOT_TZ0_ACK1 => {} + proto::MSG_BOOT_TZ0_ACK2 => { + if self + .resume_state + .cmpxchg(Self::RESUME_TZ0, Self::RESUME_SENT, Relaxed) + .is_err() + { + return; + } + let resume = Message { + msg0: u64::from(proto::EP_BOOT) + | (proto::MSG_BOOT_RESUME << proto::MSG_TYPE_SHIFT), + msg1: 0, + }; + let sent = match self.mbox.lock().as_ref() { + Some(mbox) => Self::ring(mbox, resume), + None => Err(ENODEV), + }; + if let Err(e) = sent { + dev_err_ratelimited!(self.dev, "could not send the resume ({:?})\n", e); + self.resume_state.store(Self::RESUME_FAILED, Relaxed); + } + } + proto::MSG_BOOT_RESUMED => self.resume_state.store(Self::RESUME_DONE, Relaxed), + _ => { + dev_warn_ratelimited!( + self.dev, + "resume: boot message type {:#x} (msg0 {:#018x})\n", + ty, + msg.msg0 + ); + } + } + } + /// Starts a gated enclave and waits for the messages that say it's up. fn sleep_kick(&self) -> Result<()> { pm::clear_dev_disable(&self.dev)?; @@ -3473,6 +3595,16 @@ impl SepData { fn dispatch(&self, msg: Message) { let f = proto::decode(&msg); + // The resumed enclave reports its OS running: the resume is over. While a resume + // runs nothing else waits for these, so they end here. + if f.ep == proto::EP_CONTROL + && f.tag == 0 + && (f.ty == proto::CONTROL_OS_ACTIVE || f.ty == proto::CONTROL_OS_ACTIVE_2) + && self.resume_state.load(Relaxed) != Self::RESUME_IDLE + { + self.resume_state.store(Self::RESUME_DONE, Relaxed); + return; + } match f.ep { proto::EP_DISCOVER => self.on_discovery(msg, f), diff --git a/drivers/soc/apple/shim.h b/drivers/soc/apple/shim.h index 7f2213348f7bd..306b942a95e29 100644 --- a/drivers/soc/apple/shim.h +++ b/drivers/soc/apple/shim.h @@ -38,6 +38,9 @@ bool sep_pm_platform_commit(void); int sep_pm_commit_prepare(void *dev, void *mbox, u64 msg0, u32 msg1, u64 reply, u64 mask, unsigned int answer_ms, u32 flags); int sep_pm_commit_finish(void *dev); +int sep_pm_power_on(void *dev); +void sep_pm_power_release(void *dev); +int sep_pm_dart_gate(void *dev); extern const struct dev_pm_ops sep_pm_ops; /* -- hwrng_shim.c ------------------------------------------------------- */ From 875ba10917aa7aefa656e77ea795ad89cc94bcec Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Mon, 30 Nov 2026 23:53:13 +0100 Subject: [PATCH 28/72] soc: apple: sep: enable the T6000 enclave's inbox before its resume The enclave's mailbox loses its control words with the power cut of a deep sleep. When macOS starts the enclave's CPU it enables the inbox if it reads off, then the outbox, and only then lets the SEPROM resume sepOS. Do the same before the resume handshake. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/pm.rs | 7 +++++++ drivers/soc/apple/pm_shim.c | 6 ++++++ drivers/soc/apple/sep.rs | 9 ++++++++- drivers/soc/apple/shim.h | 1 + 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/drivers/soc/apple/pm.rs b/drivers/soc/apple/pm.rs index b221625c8a9cd..99846a85be05e 100644 --- a/drivers/soc/apple/pm.rs +++ b/drivers/soc/apple/pm.rs @@ -55,6 +55,7 @@ extern "C" { fn sep_pm_power_on(dev: *mut c_void) -> c_int; fn sep_pm_power_release(dev: *mut c_void); fn sep_pm_dart_gate(dev: *mut c_void) -> c_int; + fn sep_pm_inbox_on(mbox: *mut c_void); pub(crate) static sep_pm_ops: kernel::bindings::dev_pm_ops; } @@ -201,6 +202,12 @@ pub(crate) fn power_release(dev: &kernel::device::Device) { unsafe { sep_pm_power_release(dev.as_raw().cast()) }; } +/// Enables the enclave's inbox after a deep sleep, if it reads off. +pub(crate) fn inbox_on(mbox: *mut kernel::bindings::apple_mbox) { + // SAFETY: `mbox` is the enclave's live mailbox. + unsafe { sep_pm_inbox_on(mbox.cast()) }; +} + /// Keeps the DART driver off the enclave's DART after a wake that failed. pub(crate) fn dart_gate(dev: &kernel::device::Device) -> Result<()> { // SAFETY: `dev` is the bound SEP device. diff --git a/drivers/soc/apple/pm_shim.c b/drivers/soc/apple/pm_shim.c index 15091243e1079..852e405c103a6 100644 --- a/drivers/soc/apple/pm_shim.c +++ b/drivers/soc/apple/pm_shim.c @@ -595,6 +595,12 @@ void sep_pm_power_release(void *ptr) regmap_update_bits(map, off, SEP_PS_FLAGS | SEP_PS_MIN, 0); } +/* The enclave's inbox after a deep sleep, enabled as macOS does if it reads off. */ +void sep_pm_inbox_on(void *mbox) +{ + apple_mbox_set_send_enabled(mbox, true); +} + /* * Keeps the DART driver off a DART whose domain the enclave left off, as macOS takes the * mapper inactive: after a wake that failed to start the enclave again. diff --git a/drivers/soc/apple/sep.rs b/drivers/soc/apple/sep.rs index 3d0a7219fd1a2..ea3f7a4db8e3b 100644 --- a/drivers/soc/apple/sep.rs +++ b/drivers/soc/apple/sep.rs @@ -2046,12 +2046,19 @@ impl SepData { fn sleep_resume_handshake(&self) -> Result<()> { pm::dart_restore(&self.dev); self.resume_state.store(Self::RESUME_TZ0, Relaxed); + // As macOS starts the enclave: the inbox, the outbox, then the receive interrupt. + match self.mbox.lock().as_ref() { + Some(mbox) => { + pm::inbox_on(mbox.as_raw()); + mbox.set_recv_enabled(true); + } + None => return Err(ENODEV), + } self.sleep_release_rx(); // sepOS stays silent until the boot endpoint rings, and the ROM answers TZ0 within // milliseconds: ring at once, as macOS's boot of the enclave does. match self.mbox.lock().as_ref() { Some(mbox) => { - mbox.set_recv_enabled(true); let msg = Message { msg0: u64::from(proto::EP_BOOT) | (proto::MSG_BOOT_TZ0 << proto::MSG_TYPE_SHIFT), diff --git a/drivers/soc/apple/shim.h b/drivers/soc/apple/shim.h index 306b942a95e29..a2208fdd1e29c 100644 --- a/drivers/soc/apple/shim.h +++ b/drivers/soc/apple/shim.h @@ -41,6 +41,7 @@ int sep_pm_commit_finish(void *dev); int sep_pm_power_on(void *dev); void sep_pm_power_release(void *dev); int sep_pm_dart_gate(void *dev); +void sep_pm_inbox_on(void *mbox); extern const struct dev_pm_ops sep_pm_ops; /* -- hwrng_shim.c ------------------------------------------------------- */ From 27f23910549b6ead6bd8bf52fbdd9700bdbd1f7f Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 02:22:38 +0100 Subject: [PATCH 29/72] soc: apple: sleep: let a gen-1 SMC's last keys still refuse the sleep On the M1 Pro, macOS writes pmVC and WKTP before slpa, and a failed write there gives the sleep up: nothing that cannot be undone has happened yet. Main sends both with slpa, after the point of no return, where a failure can only panic. Add an optional SMC lane step for them and a SoC flag that runs it as a refusal in the entry, right before the enclave's stop, which stays the last step that may refuse: a failed key write then gives the sleep up before the enclave is asked to sleep. The SMC driver's side, which provides the step and leaves the keys it sent out of its notice after the point of no return, follows separately. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/sleep.c | 18 ++++++++++++++++-- include/linux/soc/apple/sleep.h | 6 ++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index e6eaa106f88a1..b789d15030793 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -887,6 +887,8 @@ struct apple_sleep_soc { unsigned int smc_settle_ms; /* SEP's word: DEV_DISABLE, and not an actual state of 0, says that it is down */ bool sep_dev_disable; + /* ... before the point of no return, where a failed write still refuses the sleep */ + bool smc_keys_refuse; /* Live coprocessors with DRAM buffers require WAKE_SLP_DDR across sleep. */ bool coproc_slp_ddr; /* @@ -6763,6 +6765,14 @@ static int apple_sleep_smc_pmvc(struct apple_sleep *s) return s->smc.saved_ops->pmvc(s->smc.data); } +/* Gen 1, where the SoC asks: the SMC's keys before slpa, while a failure still refuses. */ +static int apple_sleep_smc_keys(struct apple_sleep *s) +{ + if (!s->smc.dev || !s->smc.held || !s->smc.saved_ops || !s->smc.saved_ops->keys) + return -ENODEV; + return s->smc.saved_ops->keys(s->smc.data); +} + static int apple_sleep_smc_commit_notice(struct apple_sleep *s) { if (!s->smc.dev || !s->smc.held || !s->smc.saved_ops || @@ -6905,11 +6915,15 @@ static int apple_sleep_enter_prepare(struct apple_sleep *s) if (ret) panic("Apple SEP refused after the always-on GPU stop: %d", ret); } else if (!ret) { - if (s->soc->ops->before_enclave) { + /* A failed key write is logged by the SMC's driver and refuses before SEP. */ + if (s->soc->smc_keys_refuse && apple_sleep_smc_defer_notice()) + ret = apple_sleep_smc_keys(s); + if (!ret && s->soc->ops->before_enclave) { s->regs_staged = true; s->soc->ops->before_enclave(s); } - ret = apple_sleep_enclave_stop(s); + if (!ret) + ret = apple_sleep_enclave_stop(s); } if (ret) { dev_dbg(s->dev, "refused before any commit coprocessor stop: %d\n", ret); diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index c5afd82e607b6..a8d52192ef434 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -101,6 +101,12 @@ struct apple_sleep_smc_saved_ops { * be taken there too. */ void (*until_before_slpa)(void *data, u32 us); + /* + * Optional, gen 1, for a SoC that asks: the keys macOS writes between slpw and slpa + * (pmVC, WKTP), as the entry's last step that can still refuse. late_notice then + * skips them. + */ + int (*keys)(void *data); }; #if IS_ENABLED(CONFIG_APPLE_SOC_SLEEP) From df7d1b862275d36a9a8cccb5ed3f38866871ed48 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Wed, 2 Dec 2026 22:44:41 +0100 Subject: [PATCH 30/72] soc: apple: sep: T6000: send the sleep request at the commit A T6000 enclave that took its sleep request power-gates its own island, and without the SoC's power cut its domain does not come on again. A sleep given up after the request (a device's late or noirq suspend, a refusal in .enter, a pm_test) would leave the enclave unusable until the next deep sleep or a reboot, so the request can only go out once nothing else may refuse. Stage the T6000 request at device suspend, as on the other SoCs, and let the sleep core's commit send it after the last other refusal. The commit path learns what the T6000 profile asks for: - the enclave's DART goes down with the island: it is saved and its commands gated at device suspend, in process context, let go again without a reset when the request was never sent, and reset and put back before the enclave resumes; - an answer whose result is not 0 refuses the sleep with the enclave awake; - with commit_soft_fail, an answer or a gate that does not come within its bound refuses the sleep instead of panicking; the enclave is then left unused, and every later deep sleep refused, until a reboot; - with wake_after_thaw, the enclave is resumed through its SEPROM once the tasks run again, from the PM notifier, so that a SEPROM that does not answer holds up only the enclave's users; a dark wake's next sleep first runs the wake still pending; - with ps_nonposted, the power-state word is mapped non-posted where the PMGR's bus asks for it. The other SoCs set none of these and keep their path as it was. The enclave step's refusal line no longer says the request was not sent, since with a soft failure it was. The enclave's sysfs ABI text says when a T6000 enclave sleeps, and that one that fails its request blocks deep sleep until a reboot. Signed-off-by: Andreas Fernandez --- .../ABI/testing/sysfs-driver-apple-sep | 8 +- drivers/soc/apple/pm.rs | 85 +++++++++- drivers/soc/apple/pm_shim.c | 149 +++++++++++++++--- drivers/soc/apple/profile.rs | 36 ++++- drivers/soc/apple/sep.rs | 104 +++++++++++- drivers/soc/apple/shim.h | 3 + drivers/soc/apple/sleep.c | 2 +- 7 files changed, 350 insertions(+), 37 deletions(-) diff --git a/Documentation/ABI/testing/sysfs-driver-apple-sep b/Documentation/ABI/testing/sysfs-driver-apple-sep index 56be9de08998b..9073e230f884c 100644 --- a/Documentation/ABI/testing/sysfs-driver-apple-sep +++ b/Documentation/ABI/testing/sysfs-driver-apple-sep @@ -623,7 +623,13 @@ Description: while Touch ID captures and entropy requests are held, and "awake" otherwise. sleeps counts completed prepare/post pairs since probe, aborted suspends included. The enclave's - power domains stay on across sleep, so every other attribute + power domains stay on across sleep, except in a T6000 deep + sleep: there the enclave is asked to sleep at the sleep's + commit, gates its own power island and is resumed through + its SEPROM once the tasks run again. If it does not answer + that request, does not gate its island or does not come + back after the wake, the driver stops using it and refuses + every later deep sleep until a reboot. Every other attribute keeps its value over a suspend; a reader checking that the enclave still answers after a resume reads the hwrng. diff --git a/drivers/soc/apple/pm.rs b/drivers/soc/apple/pm.rs index 99846a85be05e..3a5780bcc01c4 100644 --- a/drivers/soc/apple/pm.rs +++ b/drivers/soc/apple/pm.rs @@ -55,6 +55,9 @@ extern "C" { fn sep_pm_power_on(dev: *mut c_void) -> c_int; fn sep_pm_power_release(dev: *mut c_void); fn sep_pm_dart_gate(dev: *mut c_void) -> c_int; + fn sep_pm_dart_ungate(dev: *mut c_void) -> c_int; + fn sep_pm_dart_island_save(dev: *mut c_void) -> c_int; + fn sep_pm_dart_island_restore(dev: *mut c_void) -> c_int; fn sep_pm_inbox_on(mbox: *mut c_void); pub(crate) static sep_pm_ops: kernel::bindings::dev_pm_ops; } @@ -98,6 +101,25 @@ impl SleepGate { /// `SEP_PM_GATE_ACTUAL` in `pm_shim.c`. const COMMIT_GATE_ACTUAL: u32 = 1 << 0; +/// `SEP_PM_ANSWER_RESULT` in `pm_shim.c`. +const COMMIT_ANSWER_RESULT: u32 = 1 << 1; +/// `SEP_PM_SOFT_FAIL` in `pm_shim.c`. +const COMMIT_SOFT_FAIL: u32 = 1 << 2; +/// `SEP_PM_PS_NONPOSTED` in `pm_shim.c`. +const COMMIT_PS_NONPOSTED: u32 = 1 << 3; + +/// Outcome of a staged sleep request at device resume. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum CommitFinish { + /// No request was staged for this suspend. + Unstaged, + /// Staged, but SLEEP was never sent (or the enclave refused it and stayed awake). + Unsent, + /// SLEEP was sent and the enclave gated its domain. + Sent, + /// SLEEP was sent, then timed out or did not gate (`SEP_PM_SOFT_FAIL`); enclave is lost. + Failed, +} /// Stages `request` for the irreversible commit, with RX held until physical wake. /// Unsupported providers/transports refuse here; device suspend never sends SLEEP. @@ -110,7 +132,19 @@ pub(crate) fn leave_for_commit( mask: u64, sleep: &crate::profile::SleepProfile, ) -> Result<()> { - let flags = if sleep.gate_actual { COMMIT_GATE_ACTUAL } else { 0 }; + let mut flags = 0; + if sleep.gate_actual { + flags |= COMMIT_GATE_ACTUAL; + } + if sleep.answer_result { + flags |= COMMIT_ANSWER_RESULT; + } + if sleep.commit_soft_fail { + flags |= COMMIT_SOFT_FAIL; + } + if sleep.ps_nonposted { + flags |= COMMIT_PS_NONPOSTED; + } // SAFETY: `dev` is the bound SEP device and `mbox` its mailbox, which outlives the sleep: // `commit_finish` takes the request back at the resume. let rc = unsafe { @@ -133,14 +167,18 @@ pub(crate) fn leave_for_commit( } /// Finishes a staged request. A sent request requires a real firmware return; -/// the C boundary terminates an impossible sent-without-sleep unwind. `None` if unstaged. -pub(crate) fn commit_finish(dev: &kernel::device::Device) -> Option { +/// the C boundary terminates an impossible sent-without-sleep unwind. +pub(crate) fn commit_finish(dev: &kernel::device::Device) -> CommitFinish { // SAFETY: `dev` is the bound SEP device. let rc = unsafe { sep_pm_commit_finish(dev.as_raw().cast()) }; - if rc < 0 { - None + if rc == ENOENT.to_errno() { + CommitFinish::Unstaged + } else if rc < 0 { + CommitFinish::Failed + } else if rc == 0 { + CommitFinish::Unsent } else { - Some(rc > 0) + CommitFinish::Sent } } @@ -218,6 +256,37 @@ pub(crate) fn dart_gate(dev: &kernel::device::Device) -> Result<()> { Ok(()) } +/// Allows DART commands again without resetting the DART, after an unsent abort. +pub(crate) fn dart_ungate(dev: &kernel::device::Device) -> Result<()> { + // SAFETY: `dev` is the bound SEP device. + let rc = unsafe { sep_pm_dart_ungate(dev.as_raw().cast()) }; + if rc != 0 { + return Err(Error::from_errno(rc)); + } + Ok(()) +} + +/// Saves the enclave's DART before the request, where it goes down with the enclave's island. +pub(crate) fn dart_island_save(dev: &kernel::device::Device) -> Result<()> { + // SAFETY: `dev` is the bound SEP device. + let rc = unsafe { sep_pm_dart_island_save(dev.as_raw().cast()) }; + if rc != 0 { + return Err(Error::from_errno(rc)); + } + Ok(()) +} + +/// Allows the DART's commands again, then resets it and puts back what +/// [`dart_island_save`] saved. +pub(crate) fn dart_island_restore(dev: &kernel::device::Device) -> Result<()> { + // SAFETY: `dev` is the bound SEP device. + let rc = unsafe { sep_pm_dart_island_restore(dev.as_raw().cast()) }; + if rc != 0 { + return Err(Error::from_errno(rc)); + } + Ok(()) +} + /// Attaches every power domain of a multi-domain SEP node and keeps them on /// across system sleep; see `pm_shim.c`. Must run before the enclave is first /// spoken to. @@ -279,6 +348,10 @@ unsafe extern "C" fn pm_event(entering: bool) { // `unregister` only after `sep_pm_unregister` has waited out any running // notifier call, so it is live for this borrow. let sep = unsafe { as ForeignOwnable>::borrow(ptr) }; + // Before the holds go: requests held for the sleep go to an enclave that is back. + if !entering && sep.sleep_exit_after_thaw() { + SepData::queue_stac_online(sep.into()); + } sleep_event(&*sep, entering); } diff --git a/drivers/soc/apple/pm_shim.c b/drivers/soc/apple/pm_shim.c index 852e405c103a6..9a6931a81e3fc 100644 --- a/drivers/soc/apple/pm_shim.c +++ b/drivers/soc/apple/pm_shim.c @@ -50,6 +50,25 @@ static bool sep_ps_down(u32 state) (!apple_sleep_sep_dev_disable() && !FIELD_GET(SEP_PS_ACTUAL, state)); } +/* + * Only the answer ends the wait for it, and after it the domain counts as down only once its + * actual state reads 0: what macOS waits for on T6000. Without the flag, DEV_DISABLE ends + * either wait. + */ +#define SEP_PM_GATE_ACTUAL BIT(0) +/* + * A nonzero reply result means the enclave refused and stayed awake: refuse the sleep + * cleanly, without panicking. + */ +#define SEP_PM_ANSWER_RESULT BIT(1) +/* + * A post-send timeout or no-gate refuses the sleep instead of panicking, leaving the + * enclave's receive disabled and held so resume marks it lost (EAGAIN). + */ +#define SEP_PM_SOFT_FAIL BIT(2) +/* The power-state word is mapped non-posted where the bus says so. */ +#define SEP_PM_PS_NONPOSTED BIT(3) + /* One-shot precommit test: no SLEEP request or commit coprocessor stop. */ static bool sleep_refuse_before_send; module_param(sleep_refuse_before_send, bool, 0644); @@ -215,13 +234,6 @@ void sep_pm_dart_restore(void *ptr) #define SEP_PM_POLL_US 20 #define SEP_PM_TIMEOUT_US (5000 * USEC_PER_MSEC) -/* - * Only the answer ends the wait for it, and after it the domain counts as down only once its - * actual state reads 0: what macOS waits for on T6000. Without the flag, DEV_DISABLE ends - * either wait. - */ -#define SEP_PM_GATE_ACTUAL BIT(0) - /* * .suspend only stages this request and holds RX. The platform sends it after * the last legitimate refusal. Once sent, only physical sleep/wake is supported; @@ -238,7 +250,7 @@ static struct sep_pm_commit { u32 flags; void (*rx)(struct apple_mbox *mbox, struct apple_mbox_msg msg, void *cookie); void *cookie; - bool registered, sent, answered, gate_valid; + bool registered, sent, failed, answered, gate_valid; u8 reply_result; unsigned int answer_us; int gate_ret; @@ -286,6 +298,7 @@ static int sep_pm_commit_stop(void *data) int ret = 0; c->sent = false; + c->failed = false; c->answered = false; c->answer_us = 0; c->reply_result = 0xff; @@ -347,12 +360,31 @@ static int sep_pm_commit_stop(void *data) if (ret >= 0 && !c->answered) ret = !(c->flags & SEP_PM_GATE_ACTUAL) && (c->state & SEP_PS_DEV_DISABLE) ? -EHOSTDOWN : -ETIMEDOUT; - if (c->answered && c->reply_result) + if (c->answered && c->reply_result) { + if (c->flags & SEP_PM_ANSWER_RESULT) { + dev_err(c->dev, + "sleep refused: the enclave refused its sleep request (result %#04x)\n", + c->reply_result); + c->sent = false; + if (prepared) + apple_dart_sleep_restore(c->dart); + return -EIO; + } ret = -EREMOTEIO; - /* Native cmsgSLEEP errors/abort/timeout are fatal, never cancellation. */ - if (ret < 0) + } + /* Native cmsgSLEEP errors/abort/timeout are fatal unless the profile allows soft fail. */ + if (ret < 0) { + if (c->flags & SEP_PM_SOFT_FAIL) { + apple_mbox_set_recv_enabled(c->mbox, false); + c->failed = true; + dev_err(c->dev, + "sleep refused: SEP SLEEP failed after send (ret %d reply_valid %d result %#x state %#x)\n", + ret, c->answered, c->reply_result, c->state); + return ret; + } panic("SEP SLEEP failed after send: ret %d reply_valid %d result %#x state %#x", ret, c->answered, c->reply_result, c->state); + } /* * Native success-only receive close. No mailbox access follows it. The * held RX IRQ adapts native workloop exclusion; access-until-close is @@ -367,8 +399,16 @@ static int sep_pm_commit_stop(void *data) c->gate_ret = readl_poll_timeout_atomic(c->ps, c->state, sep_ps_down(c->state), SEP_PM_POLL_US, SEP_PM_TIMEOUT_US); c->gate_valid = true; - if (c->gate_ret) + if (c->gate_ret) { + if (c->flags & SEP_PM_SOFT_FAIL) { + c->failed = true; + dev_err(c->dev, + "sleep refused: SEP did not power-gate after send (%d, state %#x)\n", + c->gate_ret, c->state); + return c->gate_ret; + } panic("SEP SLEEP did not gate after send: %d state %#x", c->gate_ret, c->state); + } return 0; } @@ -415,7 +455,14 @@ int sep_pm_commit_prepare(void *ptr, void *mbox, u64 msg0, u32 msg1, u64 reply, ret = -ENODEV; goto err; } - c->ps = ioremap(res.start + offset, 4); + /* + * Where the profile asks for it and the bus is non-posted (T6000, where a posted read + * of the PMGR was seen to end in an SError); every other SoC keeps the posted mapping. + */ + if ((flags & SEP_PM_PS_NONPOSTED) && (res.flags & IORESOURCE_MEM_NONPOSTED)) + c->ps = ioremap_np(res.start + offset, 4); + else + c->ps = ioremap(res.start + offset, 4); if (!c->ps) { ret = -ENOMEM; goto err; @@ -429,6 +476,7 @@ int sep_pm_commit_prepare(void *ptr, void *mbox, u64 msg0, u32 msg1, u64 reply, c->answer_max_us = answer_ms * USEC_PER_MSEC; c->flags = flags; c->sent = false; + c->failed = false; /* Resume may follow a pre-tell abort: observations belong to this attempt. */ c->answered = false; c->answer_us = 0; @@ -453,27 +501,31 @@ int sep_pm_commit_prepare(void *ptr, void *mbox, u64 msg0, u32 msg1, u64 reply, return ret; } -/* After the sleep, refused or not: 1 if the request went out, 0 if not, <0 if none was left. */ +/* + * After the sleep, refused or not: 1 if the request went out and gated, 0 if unsent or + * cleanly refused by the enclave, -EIO if a post-send soft failure left the enclave lost, + * -ENOENT if none was left. + */ int sep_pm_commit_finish(void *ptr) { struct sep_pm_commit *c = &sep_pm_commit; if (!c->registered || c->dev != ptr) return -ENOENT; - if (c->sent && !pm_resume_via_firmware()) + if (c->sent && !c->failed && !pm_resume_via_firmware()) panic("SEP SLEEP was sent without a physical sleep/wake"); apple_sleep_enclave_unregister(c->dev); c->registered = false; dev_dbg(c->dev, - "sleep commit finished: sent=%d reply_valid=%d result=%#x answer_us=%u gate_valid=%d gate_ret=%d state=%#x\n", - c->sent, c->answered, c->reply_result, c->answer_us, + "sleep commit finished: sent=%d failed=%d reply_valid=%d result=%#x answer_us=%u gate_valid=%d gate_ret=%d state=%#x\n", + c->sent, c->failed, c->answered, c->reply_result, c->answer_us, c->gate_valid, c->gate_ret, c->state); iounmap(c->ps); c->ps = NULL; put_device(c->dart); c->dart = NULL; - return c->sent; + return c->failed ? -EIO : c->sent; } /* @@ -618,6 +670,67 @@ int sep_pm_dart_gate(void *ptr) return ret; } +/* + * Allows DART commands again without resetting the block, when a staged sleep was refused + * before the enclave was told and its island kept its power. + */ +int sep_pm_dart_ungate(void *ptr) +{ + struct device *dart = sep_pm_dart(ptr); + int ret; + + if (!dart) + return -ENODEV; + ret = apple_dart_resume_commands(dart); + put_device(dart); + + return ret; +} + +/* + * T6000: the enclave's DART is in the enclave's own power island and loses its registers with + * it. They are saved before the request, and the DART's commands + * are gated once the enclave has answered (sep_pm_dart_gate()), as macOS takes the mapper + * inactive: then no TLB command reaches a block without power, and the DART's own callbacks + * leave it alone. Before the enclave resumes, the commands are allowed again and the DART is + * reset and its translations put back, as after a sleep that cut its power. + */ +static bool sep_pm_island_saved; + +int sep_pm_dart_island_save(void *ptr) +{ + struct device *dart = sep_pm_dart(ptr); + int ret; + + /* Only this sleep's save may be written back: an earlier one's translations are stale. */ + WRITE_ONCE(sep_pm_island_saved, false); + if (!dart) + return -ENODEV; + ret = apple_dart_state_save(dart); + if (!ret) + WRITE_ONCE(sep_pm_island_saved, true); + put_device(dart); + + return ret; +} + +int sep_pm_dart_island_restore(void *ptr) +{ + struct device *dart = sep_pm_dart(ptr); + int ret = -EINVAL; + + if (!dart) + return -ENODEV; + if (READ_ONCE(sep_pm_island_saved)) { + ret = apple_dart_resume_commands(dart); + if (!ret) + ret = apple_dart_state_restore(dart); + } + put_device(dart); + + return ret; +} + int sep_pm_register(void *ptr, void (*event)(bool entering), int (*sleep)(void), void (*wake)(void), bool (*told)(void)) { diff --git a/drivers/soc/apple/profile.rs b/drivers/soc/apple/profile.rs index 93df4647b8d7d..3f288468cf967 100644 --- a/drivers/soc/apple/profile.rs +++ b/drivers/soc/apple/profile.rs @@ -169,6 +169,24 @@ pub(crate) struct SleepProfile { /// its minimum state raised, and the enclave is asked through its boot endpoint (TZ0, /// then op 8, answered by 0x6c). Otherwise a doorbell wakes it. pub(crate) seprom_resume: bool, + /// The answer's result says whether the enclave took the request: any but 0 refuses the + /// sleep at once, with the enclave still awake. Otherwise any answer is taken. + pub(crate) answer_result: bool, + /// The enclave's DART goes down with the enclave's own power island: saved before the + /// request, its commands gated from then on, and before the enclave + /// resumes, reset and its translations put back as a DART that lost its power. Otherwise + /// the DART's apple,sleep-restore list does it. + pub(crate) dart_island: bool, + /// The enclave comes back once the tasks run again (`PM_POST_SUSPEND`), + /// so that a SEPROM that doesn't answer holds up only the enclave's users, not the resume; + /// otherwise in the device's resume. + pub(crate) wake_after_thaw: bool, + /// On the commit-staged path, a post-send timeout or no-gate refuses the sleep and marks the + /// enclave lost (`EAGAIN`) instead of panicking. + pub(crate) commit_soft_fail: bool, + /// The commit reads the power-state word through a non-posted mapping where the PMGR's + /// bus asks for one; otherwise through a posted one. + pub(crate) ps_nonposted: bool, } /// Every SoC but T6000. @@ -176,15 +194,27 @@ const SLEEP_DEFAULT: SleepProfile = SleepProfile { answer_ms: crate::proto::SLEEP_TIMEOUT_MS, gate_actual: false, seprom_resume: false, + answer_result: false, + dart_island: false, + wake_after_thaw: false, + commit_soft_fail: false, + ps_nonposted: false, }; -/// What macOS does on T6000 (AppleSEPManager, AppleSEPBooter, AppleT6000PMGR): it waits 15 s -/// for the answer and matches it by its tag, polls the actual state to 0 after it, and resumes -/// sepOS through the SEPROM after the wake. +/// T6000 deep sleep: the request is staged at device suspend and sent in `.enter` at the +/// commit boundary, after every returnable refusal, because a T6000 enclave that gated its +/// own island will not power back on without a SoC power cut (`ps_sep` stays at actual 0). +/// Waits 15 s for the answer and matches it by its tag, polls actual state to 0 after it, +/// and resumes sepOS through the SEPROM after the wake. const SLEEP_T6000: SleepProfile = SleepProfile { answer_ms: 15000, gate_actual: true, seprom_resume: true, + answer_result: true, + dart_island: true, + wake_after_thaw: true, + commit_soft_fail: true, + ps_nonposted: true, }; pub(crate) struct PlatformProfile { diff --git a/drivers/soc/apple/sep.rs b/drivers/soc/apple/sep.rs index ea3f7a4db8e3b..69e4d4d99090f 100644 --- a/drivers/soc/apple/sep.rs +++ b/drivers/soc/apple/sep.rs @@ -744,6 +744,8 @@ struct SepData { sleep_rx_held: Atomic, /// Told at device suspend (no sleep core commit, T6041) rather than staged for the commit. sleep_at_suspend: Atomic, + /// A commit-staged real wake deferred to `PM_POST_SUSPEND` (`wake_after_thaw`). + sleep_wake_pending: Atomic, /// The enclave didn't answer a sleep request or a wake this boot: no deep sleep until a /// reboot, as a commit over a running enclave resets the SoC. sleep_unanswered: Atomic, @@ -1322,6 +1324,7 @@ impl SepData { sleep_gate: Atomic::new(pm::SleepGate::Open as u32), sleep_rx_held: Atomic::new(false), sleep_at_suspend: Atomic::new(false), + sleep_wake_pending: Atomic::new(false), sleep_unanswered: Atomic::new(false), resume_state: Atomic::new(Self::RESUME_IDLE), ool_xarm <- new_mutex!(Some(ool_xarm)), @@ -1656,6 +1659,11 @@ impl SepData { if !pm::platform_commit() { return self.sleep_enter_at_suspend(); } + // A dark wake's next round comes before the tasks run again: wake the enclave from the + // previous real sleep before staging the next one. + if self.profile.sleep.wake_after_thaw && self.sleep_wake_pending.xchg(false, Relaxed) { + self.sleep_wake_committed(); + } match self.sleep_gate() { // A failed earlier wake is not repaired by another precommit attempt. pm::SleepGate::Lost => return Err(EIO), @@ -1695,12 +1703,36 @@ impl SepData { return Err(EBUSY); } + if self.profile.sleep.dart_island { + if let Err(e) = pm::dart_island_save(&self.dev) { + dev_err_ratelimited!( + self.dev, + "sleep refused: its DART could not be saved ({:?})\n", + e + ); + self.set_sleep_gate(pm::SleepGate::Open); + return Err(e); + } + if let Err(e) = pm::dart_gate(&self.dev) { + dev_err_ratelimited!( + self.dev, + "sleep refused: its DART could not be gated ({:?})\n", + e + ); + self.set_sleep_gate(pm::SleepGate::Open); + return Err(e); + } + } + if let Err(e) = self.sleep_leave_for_commit() { dev_err_ratelimited!( self.dev, "sleep refused: irreversible request could not be staged ({:?})\n", e ); + if self.profile.sleep.dart_island { + let _ = pm::dart_ungate(&self.dev); + } self.set_sleep_gate(pm::SleepGate::Open); return Err(e); } @@ -1955,11 +1987,27 @@ impl SepData { if self.sleep_at_suspend.load(Relaxed) { return self.sleep_exit_at_suspend(); } - // Staged only: an unsent refusal releases RX without a restart. + // Staged only: an unsent refusal releases RX and ungates an island DART without a restart. match pm::commit_finish(&self.dev) { - Some(true) => self.set_sleep_gate(pm::SleepGate::Told), - Some(false) => self.sleep_release_rx(), - None => {} + pm::CommitFinish::Sent => self.set_sleep_gate(pm::SleepGate::Told), + pm::CommitFinish::Unsent => { + self.sleep_release_rx(); + if self.profile.sleep.dart_island { + if let Err(e) = pm::dart_ungate(&self.dev) { + dev_err_ratelimited!(self.dev, "could not ungate the DART ({:?})\n", e); + } + } + } + pm::CommitFinish::Failed => { + dev_err_ratelimited!( + self.dev, + "the enclave sleep request did not finish; it stays unused until a reboot\n" + ); + self.sleep_hold_rx(); + self.set_sleep_gate(pm::SleepGate::Lost); + return false; + } + pm::CommitFinish::Unstaged => {} } let gate = self.sleep_gate(); if gate == pm::SleepGate::Draining { @@ -1980,8 +2028,16 @@ impl SepData { self.set_sleep_gate(pm::SleepGate::Lost); return false; } - let answered = self.sleep_wake(); - match answered { + if self.profile.sleep.wake_after_thaw { + self.sleep_wake_pending.store(true, Relaxed); + return false; + } + self.sleep_wake_committed() + } + + /// Wakes an enclave after a real sleep whose request was sent at the commit. + fn sleep_wake_committed(&self) -> bool { + match self.sleep_wake() { Ok(()) => { self.set_sleep_gate(pm::SleepGate::Open); // One line per sleep it was told of: nothing else says it came back. @@ -2001,6 +2057,16 @@ impl SepData { } } + /// The wake of an enclave deferred to `PM_POST_SUSPEND` once the tasks run again, + /// where the profile says so (T6000). Returns whether the enclave came back from a + /// sleep it was told of. + pub(crate) fn sleep_exit_after_thaw(&self) -> bool { + if !self.profile.sleep.wake_after_thaw { + return false; + } + self.sleep_wake_pending.xchg(false, Relaxed) && self.sleep_wake_committed() + } + /// Starts the enclave after a real sleep, as the SoC's [`profile::SleepProfile`] says. fn sleep_wake(&self) -> Result<()> { if self.profile.sleep.seprom_resume { @@ -2024,10 +2090,20 @@ impl SepData { /// sepOS from RAM: TZ0, and once the ROM reports its status, op 8 in place of the IMG4, /// answered by 0x6c. On failure the enclave is left as asleep and its DART gated. fn sleep_resume(&self) -> Result<()> { + self.sleep_power_on()?; + self.sleep_resume_powered() + } + + /// The domain on, as macOS's wake does. On failure nothing else was touched. + fn sleep_power_on(&self) -> Result<()> { if self.shutting_down.load(Relaxed) { return Err(ENODEV); } - pm::power_on(&self.dev)?; + pm::power_on(&self.dev) + } + + /// The rest of the resume, with the domain on; its minimum state is dropped after. + fn sleep_resume_powered(&self) -> Result<()> { let ret = self.sleep_resume_handshake(); self.resume_state.store(Self::RESUME_IDLE, Relaxed); if ret.is_err() { @@ -2043,8 +2119,20 @@ impl SepData { ret } + /// The enclave's DART back before the enclave runs again. Where it lost its island, the wake + /// ends on an error. + fn sleep_dart_back(&self) -> Result<()> { + if !self.profile.sleep.dart_island { + pm::dart_restore(&self.dev); + return Ok(()); + } + pm::dart_island_restore(&self.dev).inspect_err(|e| { + dev_err_ratelimited!(self.dev, "could not restore the DART ({:?})\n", e); + }) + } + fn sleep_resume_handshake(&self) -> Result<()> { - pm::dart_restore(&self.dev); + self.sleep_dart_back()?; self.resume_state.store(Self::RESUME_TZ0, Relaxed); // As macOS starts the enclave: the inbox, the outbox, then the receive interrupt. match self.mbox.lock().as_ref() { diff --git a/drivers/soc/apple/shim.h b/drivers/soc/apple/shim.h index a2208fdd1e29c..6c3910a637a6e 100644 --- a/drivers/soc/apple/shim.h +++ b/drivers/soc/apple/shim.h @@ -41,6 +41,9 @@ int sep_pm_commit_finish(void *dev); int sep_pm_power_on(void *dev); void sep_pm_power_release(void *dev); int sep_pm_dart_gate(void *dev); +int sep_pm_dart_ungate(void *dev); +int sep_pm_dart_island_save(void *dev); +int sep_pm_dart_island_restore(void *dev); void sep_pm_inbox_on(void *mbox); extern const struct dev_pm_ops sep_pm_ops; diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index b789d15030793..395eebc961040 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -1995,7 +1995,7 @@ static int apple_sleep_enclave_stop(struct apple_sleep *s) return 0; ret = apple_sleep_enclave.stop(apple_sleep_enclave.data); if (ret) - dev_err(s->dev, "refused before SEP SLEEP was sent: %d\n", ret); + dev_err(s->dev, "refused at SEP sleep step: %d\n", ret); return ret; } From 0daf8d0baff73dde1145bf882340760f93db68b9 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 00:08:03 +0100 Subject: [PATCH 31/72] mfd: macsmc: tell a T6000 SMC of the firmware sleep as gen 1, with pmVC On T6000 the SMC hears of the sleep as on T8103: slpw at .prepare, then the keys and the AP idle notice over the saved lane. Its SMC has the pmVC key, which T8103's lacks, and macOS writes it as 0x100 right before WKTP = 2, both between slpw and slpa. So on a T6000 whose device tree describes the sleep: - the saved lane is used, as on T8103, or the late notice cannot run; - with apple,sleep-pmvc, pmVC is written first, then WKTP, slpa and the AP idle notice; - the lane's optional keys step sends pmVC and WKTP on their own, where the sleep driver asks for them while a failure still refuses the sleep; the late notice then skips them; - slpa and the AP idle notice follow each other without T8103's 6 ms and 30 ms gaps, which a T6000 SMC does not need; - a failed slpw refuses the sleep (a timed-out one gets its waka, since .complete does not run after a failed .prepare); - AP ON at the wake gets three tries: an AP the SMC still thinks idle gets every key refused. The J314s device tree gets pmVC = 0x100 and its SMC wake-record bit names from the ADT, so the wake record is read with the right lid, radio and port bits. No other SMC changes: T8103 has no apple,sleep-pmvc, and the rest is selected by the T6000 sleep description. Signed-off-by: Andreas Fernandez --- arch/arm64/boot/dts/apple/t6000-j314s.dts | 12 ++++ drivers/mfd/macsmc.c | 84 ++++++++++++++++++++--- include/linux/mfd/macsmc.h | 7 ++ 3 files changed, 93 insertions(+), 10 deletions(-) diff --git a/arch/arm64/boot/dts/apple/t6000-j314s.dts b/arch/arm64/boot/dts/apple/t6000-j314s.dts index 763a99dba58b0..9b49c22fefd03 100644 --- a/arch/arm64/boot/dts/apple/t6000-j314s.dts +++ b/arch/arm64/boot/dts/apple/t6000-j314s.dts @@ -39,6 +39,18 @@ status = "okay"; }; +/* + * The SMC's sleep keys and wake record: pmVC is written 0x100 between slpw and slpa, as macOS + * does; the wake reasons are the ADT's smc-pmu event_name-bitN. + */ +&smc { + apple,sleep-pmvc = <0x100>; + apple,wake-reasons = <0 8 9 10 11 12 14 40 41 42 43 44 45 48>; + apple,wake-reason-names = "pwrbtn", "lid", "charger", "codec", "wifibt", + "trackpadkeyboard", "sdconn", "acattach", "acdetach", + "USB-C_plug", "USB2_plug", "USB2_wake", "aupo", "rtc"; +}; + /* * From the J314s platform device tree (/arm-io/spi2/mesa): the power line * is gpio0 pin 196 (function-mesa_pwr, active high) and the data-ready line diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 25daa975c1fbe..72e1342d76537 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -774,6 +774,11 @@ static int apple_smc_probe(struct platform_device *pdev) of_device_is_compatible(dev->of_node, "apple,t8103-smc") || of_device_is_compatible(dev->of_node, "apple,t8112-smc")) smc->saved_enabled = true; + /* T6000 is told in the same place as T8103, after the other coprocessors stopped. */ + smc->sleep_t6000 = apple_t6000_sleep_described() && + of_device_is_compatible(dev->of_node, "apple,t6000-smc"); + if (smc->sleep_t6000) + 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"); @@ -1185,7 +1190,12 @@ static int apple_smc_notice_on(struct apple_smc *smc, bool *mbsw, bool later) int ret, error = 0; if (!smc->saved_wake) { - ret = apple_smc_ap_power(smc, true); + /* T6000: an idle AP gets every key refused, so AP ON gets three tries. */ + unsigned int tries = smc->sleep_t6000 ? 3 : 1; + + do { + ret = apple_smc_ap_power(smc, true); + } while (ret && --tries); if (ret) dev_err(smc->dev, "SMC did not take the wake notice: %d\n", ret); apple_smc_set_ap_notified(smc, false); @@ -1579,6 +1589,7 @@ static int apple_smc_sleep_saved_begin(void *data) smc->saved_scope = true; smc->saved_committed = false; smc->saved_pmvc_sent = false; + smc->gen1_keys_sent = false; /* Drain only a bounded prefix before reserving any ID. An old reply * cannot satisfy a fresh command or a stale AP completion a new post. */ @@ -1829,6 +1840,39 @@ static const struct apple_sleep_smc_saved_ops apple_smc_saved_ops = { * resume, except on gen 1, whose are drained once the AP's power-down is * answered. */ +/* Gen 1: pmVC (where the SMC has one), then WKTP, as macOS writes them before slpa. */ +static int apple_smc_sleep_gen1_keys(struct apple_smc *smc) +{ + u8 wktp = 2; + int ret; + + if (smc->sleep_pmvc_set) { + ret = apple_smc_saved_write_u32(smc, SMC_KEY(pmVC), smc->sleep_pmvc); + if (ret) + return ret; + } + return apple_smc_saved_write(smc, SMC_KEY(WKTP), &wktp, sizeof(wktp)); +} + +/* The same, where the sleep driver asks for them while a failure still refuses the sleep */ +static int apple_smc_sleep_keys(void *data) +{ + struct apple_smc *smc = data; + int ret; + + lockdep_assert_held(&smc->mutex); + if (!irqs_disabled() || !smc->saved_scope || !smc->sleep_warned || + smc->saved_committed || smc->ap_notified || !apple_sleep_smc_gen1_keys()) + return -EINVAL; + ret = apple_smc_sleep_gen1_keys(smc); + if (ret) { + dev_err(smc->dev, "SMC sleep keys failed: %d\n", ret); + return ret; + } + smc->gen1_keys_sent = true; + return 0; +} + static int apple_smc_sleep_after_peers(void *data, u64 *sent, u64 *ack) { struct apple_smc *smc = data; @@ -1841,25 +1885,29 @@ 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. */ - if (apple_sleep_smc_gen1_keys()) { - u8 wktp = 2; - - ret = apple_smc_saved_write(smc, SMC_KEY(WKTP), &wktp, sizeof(wktp)); + /* + * Gen 1 (T8103): WKTP between slpw and slpa; this SMC has no pmVC. T6000's has: with + * apple,sleep-pmvc, pmVC goes first, then WKTP, as macOS writes them there. + */ + if (apple_sleep_smc_gen1_keys() && !smc->gen1_keys_sent) { + ret = apple_smc_sleep_gen1_keys(smc); if (ret) goto out; } ret = apple_smc_saved_write_u32(smc, SMC_KEY(MBSE), SMC_KEY(slpa)); if (ret) goto out; - mdelay(SMC_SLEEP_ARM_MS); + /* T6000: the AP idle notice right behind slpa's answer, with no gap after it either. */ + if (!smc->sleep_t6000) + mdelay(SMC_SLEEP_ARM_MS); smc->ap_notified = true; ret = apple_rtkit_m5_saved_ap_power(smc->rtk, false, sent, ack); if (!ret) { /* Gen 1: what the SMC posted with its answer, out of the mailbox first */ if (apple_sleep_smc_gen1_keys()) apple_smc_saved_drain(smc); - mdelay(apple_sleep_smc_settle_ms() ?: SMC_SLEEP_IDLE_MS); + if (!smc->sleep_t6000) + mdelay(apple_sleep_smc_settle_ms() ?: SMC_SLEEP_IDLE_MS); } out: smc->saved_notice_error = ret; @@ -1868,6 +1916,7 @@ static int apple_smc_sleep_after_peers(void *data, u64 *sent, u64 *ack) static const struct apple_sleep_smc_saved_ops apple_smc_j813_saved_ops = { .begin = apple_smc_sleep_saved_begin, + .keys = apple_smc_sleep_keys, .late_notice = apple_smc_sleep_after_peers, .end = apple_smc_j813_saved_end, }; @@ -1958,10 +2007,25 @@ static int apple_smc_sleep_prepare(struct apple_smc *smc) smc->sleep_policy = NULL; return ret; } - if (apple_sleep_debug_link_up()) + if (apple_sleep_debug_link_up()) { smc->sleep_warn_late = true; - else + } else if (smc->sleep_t6000) { + int ret = apple_smc_write_u32(smc, SMC_KEY(MBSE), SMC_KEY(slpw)); + + if (ret) { + /* + * No sleep without the warning. A write that timed out may still have + * reached the SMC, and .complete is not called after a failed .prepare. + */ + dev_err(smc->dev, "Failed to issue MBSE = slpw: %d\n", ret); + if (ret == -ETIMEDOUT) + apple_smc_write_mbse(smc, SMC_KEY(waka)); + return ret; + } + smc->sleep_warned = true; + } else { apple_smc_sleep_warn(smc); + } if (apple_sleep_smc_defer_notice()) return apple_sleep_smc_register_saved(smc->dev, &apple_smc_j813_saved_ops, smc); diff --git a/include/linux/mfd/macsmc.h b/include/linux/mfd/macsmc.h index a6d07b2a7b6bc..1f7e46bd0f0fc 100644 --- a/include/linux/mfd/macsmc.h +++ b/include/linux/mfd/macsmc.h @@ -155,6 +155,13 @@ struct apple_smc { /* apple,sleep-pmvc: when set, the pmVC value written at the sleep commit. */ bool sleep_pmvc_set; u32 sleep_pmvc; + /* Gen 1: pmVC and WKTP went out ahead of slpa, at the last step that can refuse. */ + bool gen1_keys_sent; + /* + * T6000 with its firmware-driven sleep described: a failed slpw refuses the sleep, slpa + * and the AP idle notice follow each other without gaps, and AP ON is tried three times. + */ + bool sleep_t6000; /* 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 b54950ec1158b0c5890cd7edaa1b921f7ef35647 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 00:10:19 +0100 Subject: [PATCH 32/72] irqchip/apple-aic: keep the T6000 AICv2 across a firmware sleep The T6000 firmware-driven sleep powers the AIC down with the rest of the SoC, and it comes back disabled with its configuration at reset values. Match apple,t6000-aic to data that sets up the existing syscore save and restore (the global registers, IRQ_CFG, the masks from the IRQ core's books), where the device tree describes that sleep. T6001 and T6002 share the compatible and keep apple,aic2's data. Unlike on the AICv3 SoCs, the lines armed for wake stay unmasked when the rest is masked at the syscore suspend, so that a wake event that arrives during the entry is still seen as pending. aic_sleep_mask_all_but_wake() does the same for the sleep driver's last step before the WFI, and aic_sleep_restore() puts the masks back after it. Signed-off-by: Andreas Fernandez --- drivers/irqchip/irq-apple-aic.c | 53 +++++++++++++++++++++++++++ include/linux/irqchip/irq-apple-aic.h | 2 + 2 files changed, 55 insertions(+) diff --git a/drivers/irqchip/irq-apple-aic.c b/drivers/irqchip/irq-apple-aic.c index ef8c31817b9ac..1491ca61cd9d7 100644 --- a/drivers/irqchip/irq-apple-aic.c +++ b/drivers/irqchip/irq-apple-aic.c @@ -282,6 +282,8 @@ struct aic_info { bool fw_suspend; /* If set, fw_suspend holds only where this says the sleep is described */ bool (*fw_suspend_described)(void); + /* The wake-armed lines stay unmasked from the syscore suspend to the last WFI */ + bool fw_suspend_wake_open; }; static const struct aic_info aic1_info __initconst = { @@ -349,6 +351,22 @@ static const struct aic_info aic3_info __initconst = { .local_fast_ipi = true, }; +/* + * T6000 (M1 Pro): its AICv2 loses its configuration in the firmware-driven sleep, which is + * set up where the device tree describes it; T6001 and T6002 share the compatible. Lines + * armed for wake stay unmasked to the end, so that a wake event that comes in during the + * entry still shows as pending. + */ +static const struct aic_info aic2_t6000_info __initconst = { + .version = 2, + .irq_cfg = AIC2_IRQ_CFG, + .fast_ipi = true, + .local_fast_ipi = true, + .fw_suspend = true, + .fw_suspend_described = apple_t6000_sleep_described, + .fw_suspend_wake_open = true, +}; + /* * The M5 Pro's sleep: the last steps mask every interrupt and acknowledge events by hand. The * save and restore are set up on the boards whose device tree describes a T6050 sleep; @@ -437,6 +455,10 @@ static const struct of_device_id aic_info_match[] = { .compatible = "apple,t8015-aic", .data = &aic1_fipi_info, }, + { + .compatible = "apple,t6000-aic", + .data = &aic2_t6000_info, + }, { .compatible = "apple,aic", .data = &aic1_info, @@ -537,6 +559,35 @@ void aic_sleep_mask_all(void) ic->sleep_cpu_ids[i] = aic_ic_read(ic, AIC_CPU_BLOCK(i)); } +/* Unmask the lines the IRQ core has armed for wake and not masked, from its books. */ +static void aic_sleep_open_wake_lines(struct aic_irq_chip *ic) +{ + u32 off = 0; + int die, i; + + for (die = 0; die < ic->nr_die; die++, off += ic->info.die_stride) { + for (i = 0; i < ic->nr_irq; i++) { + unsigned int virq = irq_find_mapping(ic->hw_domain, AIC_IRQ_HWIRQ(die, i)); + struct irq_data *d = virq ? irq_get_irq_data(virq) : NULL; + + if (d && irqd_is_wakeup_set(d) && !irqd_irq_masked(d)) + aic_ic_write(ic, ic->info.mask_clr + off + MASK_REG(i), + MASK_BIT(i)); + } + } +} + +/* + * As aic_sleep_mask_all(), for a sleep that leaves the lines armed for wake open to the last + * WFI: a wake event during the entry then ends that WFI instead of waiting in a masked line. + * aic_sleep_restore() puts back the masks as they were before. + */ +void aic_sleep_mask_all_but_wake(void) +{ + aic_sleep_mask_all(); + aic_sleep_open_wake_lines(aic_irqc); +} + void aic_sleep_restore(void) { struct aic_irq_chip *ic = aic_irqc; @@ -1329,6 +1380,8 @@ static int aic_syscore_suspend(void *data) aic_ic_write(ic, ic->info.mask_set + off + i * 4, U32_MAX); off += ic->info.die_stride; } + if (ic->info.fw_suspend_wake_open) + aic_sleep_open_wake_lines(ic); aic_sleep.saved = true; return 0; diff --git a/include/linux/irqchip/irq-apple-aic.h b/include/linux/irqchip/irq-apple-aic.h index 3c3781322a52b..1577bb11012fc 100644 --- a/include/linux/irqchip/irq-apple-aic.h +++ b/include/linux/irqchip/irq-apple-aic.h @@ -6,10 +6,12 @@ #ifdef CONFIG_APPLE_AIC void aic_sleep_mask_all(void); +void aic_sleep_mask_all_but_wake(void); void aic_sleep_restore(void); u32 aic_sleep_ack(void); #else static inline void aic_sleep_mask_all(void) { } +static inline void aic_sleep_mask_all_but_wake(void) { } static inline void aic_sleep_restore(void) { } static inline u32 aic_sleep_ack(void) { return 0; } #endif From ec14d06c4a474be2b682729769c517c9605ade94 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 00:12:13 +0100 Subject: [PATCH 33/72] iommu/apple-dart: poll a T8020-layout DART's TLB busy flag around a sleep apple_dart_sleep_wait_tlb() polled the T8110 TLB_CMD busy flag on every DART. On a DART of the T8020 layout (T8020 and T6000, such as the T6000 SEP's DART) that offset is the REMAP register, and its busy flag is bit 2 of STREAM_COMMAND, which the TLB invalidate of that layout already polls. Poll that register there, with the same timeout. T8110 DARTs, the only ones with sleep lists so far, are unchanged. Signed-off-by: Andreas Fernandez --- drivers/iommu/apple-dart.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index 6a0af70f689f9..c8dca90aa7c8b 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -7674,13 +7674,22 @@ static void apple_dart_sleep_write_kept(struct apple_dart *dart, list->pairs[2 * i]); } +/* + * The TLB command engine's busy flag. The T8020 layout (T8020 and T6000, such as the T6000 + * SEP's DART) has it in STREAM_COMMAND; its register at the T8110 TLB_CMD offset is REMAP. + */ static void apple_dart_sleep_wait_tlb(struct apple_dart *dart) { + u32 reg = DART_T8110_TLB_CMD, busy = DART_T8110_TLB_CMD_BUSY; + u32 timeout = DART_T8110_FLUSH_BUSY_TIMEOUT; u32 status; - if (readl_poll_timeout_atomic(dart->regs + DART_T8110_TLB_CMD, status, - !(status & DART_T8110_TLB_CMD_BUSY), 1, - DART_T8110_FLUSH_BUSY_TIMEOUT)) + if (dart->hw->type != DART_T8110) { + reg = DART_T8020_STREAM_COMMAND; + busy = DART_T8020_STREAM_COMMAND_BUSY; + timeout = DART_STREAM_COMMAND_BUSY_TIMEOUT; + } + if (readl_poll_timeout_atomic(dart->regs + reg, status, !(status & busy), 1, timeout)) dev_err(dart->dev, "TLB command still busy around a sleep: %#x\n", status); } From f6c4c36e69a96cd6b8b6d166cce75b5a338ddc69 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 00:12:50 +0100 Subject: [PATCH 34/72] iommu/apple-dart: restore locked DARTs after a T6000 firmware sleep The boot loader locks some DARTs (the display's, ANS's, ...), and the driver leaves their registers alone: it cannot reprogram them, and they never needed it. The T6000 firmware-driven sleep resets them, though, while the page tables they point to stay in memory, and the wake goes back to the kernel without the boot loader setting them up again. On a T6000 whose device tree describes that sleep, a locked DART now saves its lock word, stream enables, TCRs and TTBRs at every suspend, runtime ones included, since a runtime suspend may be the last before the sleep. When resume finds the state gone, it writes back the TTBRs and TCRs, the stream enables, then the lock, and flushes each stream's TLB. A DART that kept its state is left alone, as before. Every other locked DART is unchanged. Signed-off-by: Andreas Fernandez --- drivers/iommu/apple-dart.c | 49 +++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index c8dca90aa7c8b..d0d43f7cffea6 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -605,6 +605,14 @@ struct apple_dart { u32 save_tcr[DART_MAX_STREAMS]; u32 save_ttbr[DART_MAX_STREAMS][DART_MAX_TTBR]; + /* + * A locked DART that the T6000 firmware-driven sleep resets: its lock word and stream + * enables, saved with the streams, and whether they were. + */ + bool locked_sleep; + bool locked_saved; + u32 save_config; + u32 save_enable[BITS_TO_U32(DART_MAX_STREAMS)]; u64 *locked_ttbr[DART_MAX_STREAMS][DART_MAX_TTBR]; /* Values published by this driver into initially vacant root slots. */ @@ -7009,6 +7017,7 @@ static int apple_dart_probe(struct platform_device *pdev) dart->fw_handoff = apple_dart_fw_handoff_enabled(dart); /* Only a locked DART takes the range invalidates apple_dart_t8110_tlb_wait() waits out. */ dart->display_scanout = dart->locked && apple_dart_display_scanout(dev->of_node); + dart->locked_sleep = dart->locked && apple_t6000_sleep_described(); /* * Preserve our retained-root path when no reserved-table handoff exists. * T6050's boot loader leaves every display DART locked on four-level @@ -7904,10 +7913,25 @@ static __maybe_unused int apple_dart_suspend(struct device *dev) if (dart->power_retained || dart->retained_mtp) return 0; - /* Locked DARTs can't be restored so skip saving their registers. */ - if (dart->locked) + /* + * Locked DARTs can't be restored so skip saving their registers, unless the T6000 sleep + * resets them: the firmware's page tables stay in memory. A runtime suspend saves them + * too, as it may be the last one before the sleep. Resume writes them back only when the + * DART really lost its state. + */ + if (dart->locked && !dart->locked_sleep) return 0; + if (dart->locked) { + unsigned int idx; + + dart->save_config = 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; } @@ -8286,8 +8310,27 @@ 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 && (!dart->locked_saved || apple_dart_state_retained(dart))) + return 0; + + /* + * The T6000 sleep reset it: put back what the boot loader set up, then the lock, then + * drop whatever the TLBs picked up meanwhile. + */ + if (dart->locked) { + 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_config, dart->hw->lock); + apple_dart_invalidate_each_stream(dart); return 0; + } /* * Not for a DART restored at every power-up, as macOS's SPTM does (its From f7e5cb40a0af3e1e5933c3e07dc5878c30567a61 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 00:13:31 +0100 Subject: [PATCH 35/72] HID: spi-hid-apple: no wake interrupt in a T6000 firmware sleep In the T6000 firmware-driven sleep the SoC loses its power, and the keyboard and trackpad wake the machine through the SMC, not through the SPI HID controller's interrupt. An armed interrupt there only turns other wakes into full ones. So on a T6000 whose device tree describes that sleep, a firmware-driven suspend treats the controller as no wakeup source: it is powered off, and resume boots it again, as for a device with wakeup turned off. Suspend-to-idle and every other machine keep the wake-armed path. Signed-off-by: Andreas Fernandez --- drivers/hid/spi-hid/spi-hid-apple-core.c | 15 ++++++++++++++- 1 file changed, 14 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 3f369b99f28c4..b9f76a92f253d 100644 --- a/drivers/hid/spi-hid/spi-hid-apple-core.c +++ b/drivers/hid/spi-hid/spi-hid-apple-core.c @@ -26,8 +26,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -140,6 +142,8 @@ struct spihid_apple { atomic_t power; bool irq_wake_enabled; + /* No wake source in a firmware-driven sleep: the T6000's, where the SMC wakes for it */ + bool fw_sleep_off; }; /** @@ -1261,6 +1265,7 @@ int spihid_apple_core_probe(struct spi_device *spi, struct spihid_apple_ops *ops if (err) dev_warn(dev, "cannot enable wakeup: %d\n", err); } + spihid->fw_sleep_off = apple_t6000_sleep_described(); /* request device information */ dev_dbg(dev, "request device info"); @@ -1332,6 +1337,14 @@ static int spihid_apple_core_suspend(struct device *dev) { int ret; struct spihid_apple *spihid = spi_get_drvdata(to_spi_device(dev)); + /* + * The T6000 firmware-driven sleep cuts the SoC's power. Keys and the trackpad wake the + * machine through the SMC there, and an armed interrupt would turn every other wake + * into a full one. So the controller is powered off and booted again on resume, as + * for a device that is no wakeup source. + */ + bool wake = device_may_wakeup(dev) && + !(spihid->fw_sleep_off && pm_suspend_via_firmware()); if (spihid->tp.hid) { ret = hid_driver_suspend(spihid->tp.hid, PMSG_SUSPEND); @@ -1365,7 +1378,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 dde84a4d4deb0af0bba9db786d3c6a1e76909b8e Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Wed, 2 Dec 2026 16:17:34 +0100 Subject: [PATCH 36/72] dt-bindings: usb: ti,tps6598x: add apple,sleep-ssps-rules for CD321x The tipd driver reads apple,sleep-ssps-rules on every controller it binds, and the J314s's CD321x port controllers need it. Allow it on apple,cd321x next to apple,sleep-ssps, and describe what it does there: an S3 that is not taken refuses the sleep, an S0 that is not taken is set up again by the driver's controller setup recovery, and a command found done on a read past its limit counts as done. Signed-off-by: Andreas Fernandez --- Documentation/devicetree/bindings/usb/ti,tps6598x.yaml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Documentation/devicetree/bindings/usb/ti,tps6598x.yaml b/Documentation/devicetree/bindings/usb/ti,tps6598x.yaml index 08ecfa211ad65..354e8a25a7633 100644 --- a/Documentation/devicetree/bindings/usb/ti,tps6598x.yaml +++ b/Documentation/devicetree/bindings/usb/ti,tps6598x.yaml @@ -62,6 +62,15 @@ properties: Tell the controller the system's power state (SSPS) around a firmware-driven suspend, S3 before it and S0 after. + apple,sleep-ssps-rules: + type: boolean + description: + Talk to the controller under the sleep's rules. A controller that does + not take S3 refuses the sleep. One that does not take S0 after it is + set up again (system power state and interrupt mask) by the driver's + controller setup recovery, and the failure is reported. A command found + done on a read made past its limit counts as done. + firmware-name: description: | Should contain the name of the default patch binary @@ -106,6 +115,7 @@ allOf: then: properties: apple,sleep-ssps: false + apple,sleep-ssps-rules: false additionalProperties: false From c5c82fb432bf948cdbfc4a095c7667420ee80d62 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 00:15:20 +0100 Subject: [PATCH 37/72] arm64: dts: apple: t6000-j314s: put the port controllers in S3 to sleep Without it the CD321x port controllers stay in S0 through the T6000 firmware-driven sleep, keep raising events once their bus is down, and the SMC wakes the machine right away for acdetach. apple,sleep-ssps has the driver move them to S3 before the sleep and back to S0 after it, as on the J313. All four controllers get it, the MagSafe one included. They also get the sleep's rules (apple,sleep-ssps-rules), as the J714s's controllers have them: a controller that does not take S3 refuses the sleep, and one that does not take S0 again is set up again by the driver and reported, instead of staying in S3 unseen. Signed-off-by: Andreas Fernandez --- arch/arm64/boot/dts/apple/t6000-j314s.dts | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/arch/arm64/boot/dts/apple/t6000-j314s.dts b/arch/arm64/boot/dts/apple/t6000-j314s.dts index 9b49c22fefd03..def2af47589d1 100644 --- a/arch/arm64/boot/dts/apple/t6000-j314s.dts +++ b/arch/arm64/boot/dts/apple/t6000-j314s.dts @@ -51,6 +51,32 @@ "USB-C_plug", "USB2_plug", "USB2_wake", "aupo", "rtc"; }; +/* + * The port controllers' system power state: S3 before a firmware-driven sleep, S0 after it. + * Left in S0, a controller keeps raising events after its bus is down, and the SMC wakes the + * machine at once with acdetach. Under the sleep's rules a controller that does not take S3 + * refuses the sleep, and one that does not take S0 again is set up again by the driver. + */ +&hpm0 { + apple,sleep-ssps; + apple,sleep-ssps-rules; +}; + +&hpm1 { + apple,sleep-ssps; + apple,sleep-ssps-rules; +}; + +&hpm2 { + apple,sleep-ssps; + apple,sleep-ssps-rules; +}; + +&hpm5 { + apple,sleep-ssps; + apple,sleep-ssps-rules; +}; + /* * From the J314s platform device tree (/arm-io/spi2/mesa): the power line * is gpio0 pin 196 (function-mesa_pwr, active high) and the data-ready line From d6736db5679179ca5abae85c69cc29bcb54af795 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Mon, 30 Nov 2026 23:36:21 +0100 Subject: [PATCH 38/72] soc: apple: sleep: add optional profile hooks at .prepare and .end T6000 tells the PMP the sleep is coming through a PMP telemetry word, right after the SMC hears slpw and long before the PMP stops, and puts the word back once the sleep is over. slpw goes out in the SMC's device .prepare, after .begin, so .begin is too early for it. Add two optional operations: prepare, called last in the platform's .prepare, where every device has suspended, and end, called last in .end, after WD1 is back as user space had it. Both run only for a suspend through the firmware. SoCs without them are unchanged. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/sleep.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 395eebc961040..ee2d097b83177 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -624,6 +624,10 @@ struct apple_sleep_ops { void (*wfi_again)(struct apple_sleep *s); /* Optional: power domain kept across sleep by the SoC provider */ bool (*domain_kept)(struct apple_sleep *s, int off); + /* Optional: at .prepare, every device suspended and the SMC told the sleep is coming */ + void (*prepare)(struct apple_sleep *s); + /* Optional: at .end, last, once the watchdog is back as user space had it */ + void (*end)(struct apple_sleep *s); /* Pure bounded planning only; no register read/write or entry authorization. */ int (*plan_complex_request)(u32 observed, u32 *request); @@ -7471,6 +7475,8 @@ static void apple_sleep_end(void) } else if (pm_suspend_via_firmware() && !apple_sleep->soc->no_wd2) { apple_sleep_wdt_end(apple_sleep->wdt); } + if (pm_suspend_via_firmware() && apple_sleep->soc->ops->end) + apple_sleep->soc->ops->end(apple_sleep); } /* @@ -7796,6 +7802,8 @@ static int apple_sleep_prepare(void) } } apple_sleep_gate_prepare(s); + if (s->soc->ops->prepare && pm_suspend_via_firmware()) + s->soc->ops->prepare(s); return 0; } From f817f4a71c34d5010c457410288cde4f688eea31 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 09:53:45 +0100 Subject: [PATCH 39/72] soc: apple: sleep: let a driver stop its coprocessor at .prepare Some coprocessors have to be stopped before the noirq phase, as genpd powers their domain off in it, mailbox included, and a firmware stopped after that hangs the machine. Stopping one in its device's own suspend is too early: any later device that fails its suspend, and any wakeup event before the late phase, then gives the sleep up with the firmware stopped, and it does not start again without the power cut. Add a notifier chain called at the end of .prepare, for a sleep that cuts the SoC's power only: after every device's suspend and before any late suspend, which is as late as the noirq phase allows while no late callback of another device comes before it. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/sleep.c | 21 +++++++++++++++++++++ include/linux/soc/apple/sleep.h | 17 +++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index ee2d097b83177..507a4c3c6a2cd 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -1312,6 +1312,21 @@ void apple_sleep_usb_asleep_notifier_unregister(struct notifier_block *nb) } EXPORT_SYMBOL_GPL(apple_sleep_usb_asleep_notifier_unregister); +/* See apple_sleep_prepare() */ +static BLOCKING_NOTIFIER_HEAD(sleep_prepare_chain); + +int apple_sleep_prepare_notifier_register(struct notifier_block *nb) +{ + return blocking_notifier_chain_register(&sleep_prepare_chain, nb); +} +EXPORT_SYMBOL_GPL(apple_sleep_prepare_notifier_register); + +void apple_sleep_prepare_notifier_unregister(struct notifier_block *nb) +{ + blocking_notifier_chain_unregister(&sleep_prepare_chain, nb); +} +EXPORT_SYMBOL_GPL(apple_sleep_prepare_notifier_unregister); + static unsigned long sleep_usb_wake_ports; void apple_sleep_usb_set_wake_ports(unsigned long ports) @@ -7804,6 +7819,12 @@ static int apple_sleep_prepare(void) apple_sleep_gate_prepare(s); if (s->soc->ops->prepare && pm_suspend_via_firmware()) s->soc->ops->prepare(s); + /* + * Last: a coprocessor whose power domain goes off in the noirq phase, with its mailbox, + * stops here, after every device's suspend and before any late one. + */ + if (pm_suspend_via_firmware()) + blocking_notifier_call_chain(&sleep_prepare_chain, 0, NULL); return 0; } diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index a8d52192ef434..54f4b123ea9e4 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -234,6 +234,14 @@ struct notifier_block; int apple_sleep_usb_asleep_notifier_register(struct notifier_block *nb); void apple_sleep_usb_asleep_notifier_unregister(struct notifier_block *nb); +/* + * Called at .prepare of a sleep that cuts the SoC's power, once every device's suspend has run and + * before any late suspend: for a coprocessor whose firmware must be stopped before the noirq phase, + * which powers its domain off with its mailbox, and as late as that allows. + */ +int apple_sleep_prepare_notifier_register(struct notifier_block *nb); +void apple_sleep_prepare_notifier_unregister(struct notifier_block *nb); + /* * The USB host is up, as macOS's USB_SLP_DDR vote has it: from the host's start to its stop, * awake and asleep. It sets bit 0 of the wake block's word 0 for every sleep in between. @@ -531,6 +539,15 @@ static inline void apple_sleep_usb_asleep_notifier_unregister(struct notifier_bl { } +static inline int apple_sleep_prepare_notifier_register(struct notifier_block *nb) +{ + return 0; +} + +static inline void apple_sleep_prepare_notifier_unregister(struct notifier_block *nb) +{ +} + static inline void apple_sleep_usb_slp_ddr(bool up) { } From 4750fcd3dbec3e433ac568882c3411822ad91c59 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 13:43:04 +0100 Subject: [PATCH 40/72] soc: apple: sleep: let a driver start its coprocessor again at .finish A coprocessor stopped from the .prepare notifier would come back in its driver's resume, which runs after the platform's .finish. A SoC that tells the SMC of the wake at .finish, as T6000 will, needs those coprocessors back before that: the PMP and its requests, the AOP's wake notice, the GPU firmware's restart, and only then the SMC's wake record and waka. Add a second notifier chain, called at .finish of a sleep that cuts the SoC's power, after every device's early resume, the PMP's restart among them, and before the SMC's own .finish. The PM core runs .finish after every .prepare, also for a sleep refused later and for a dry run, so a driver that stops its coprocessor from the first chain can start it again from this one on every path, and its resume then finds nothing left to do. Only the entry that runs inside cpu_suspend() calls it, and with nobody registered it does nothing. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/sleep.c | 22 ++++++++++++++++++++++ include/linux/soc/apple/sleep.h | 18 ++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 507a4c3c6a2cd..cc51ca6ee643d 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -1327,6 +1327,21 @@ void apple_sleep_prepare_notifier_unregister(struct notifier_block *nb) } EXPORT_SYMBOL_GPL(apple_sleep_prepare_notifier_unregister); +/* See apple_sleep_late_scope_finish() */ +static BLOCKING_NOTIFIER_HEAD(sleep_finish_chain); + +int apple_sleep_finish_notifier_register(struct notifier_block *nb) +{ + return blocking_notifier_chain_register(&sleep_finish_chain, nb); +} +EXPORT_SYMBOL_GPL(apple_sleep_finish_notifier_register); + +void apple_sleep_finish_notifier_unregister(struct notifier_block *nb) +{ + blocking_notifier_chain_unregister(&sleep_finish_chain, nb); +} +EXPORT_SYMBOL_GPL(apple_sleep_finish_notifier_unregister); + static unsigned long sleep_usb_wake_ports; void apple_sleep_usb_set_wake_ports(unsigned long ports) @@ -7777,6 +7792,13 @@ static void apple_sleep_late_scope_finish(void) /* .finish also runs when noirq suspend fails before platform .wake. */ apple_sleep_smc_scope_end(); sleep_v27_scope_end(); + /* + * What the .prepare chain stopped starts again here, after every early resume (the PMP's + * restart and its requests among them) and before the SMC hears of the wake, on every path + * that ran .prepare: a refused sleep and a dry run too. + */ + if (pm_suspend_via_firmware()) + blocking_notifier_call_chain(&sleep_finish_chain, 0, NULL); /* After device early resume, which consumed the ANS receipt. */ sleep_ans_late_release(false); } diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index 54f4b123ea9e4..96440e75bd94a 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -242,6 +242,15 @@ void apple_sleep_usb_asleep_notifier_unregister(struct notifier_block *nb); int apple_sleep_prepare_notifier_register(struct notifier_block *nb); void apple_sleep_prepare_notifier_unregister(struct notifier_block *nb); +/* + * Called at .finish of every sleep whose .prepare called the chain above, given up or not, after + * every device's early resume and before the SMC is told of the wake: for such a coprocessor to + * start again. Only on a SoC whose entry runs inside cpu_suspend(); elsewhere it is not called, + * and the coprocessor's own resume has to start it. + */ +int apple_sleep_finish_notifier_register(struct notifier_block *nb); +void apple_sleep_finish_notifier_unregister(struct notifier_block *nb); + /* * The USB host is up, as macOS's USB_SLP_DDR vote has it: from the host's start to its stop, * awake and asleep. It sets bit 0 of the wake block's word 0 for every sleep in between. @@ -548,6 +557,15 @@ static inline void apple_sleep_prepare_notifier_unregister(struct notifier_block { } +static inline int apple_sleep_finish_notifier_register(struct notifier_block *nb) +{ + return 0; +} + +static inline void apple_sleep_finish_notifier_unregister(struct notifier_block *nb) +{ +} + static inline void apple_sleep_usb_slp_ddr(bool up) { } From 6aa17a68c4bcfaae3af0e742cc986b25f40ff497 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 02:21:10 +0100 Subject: [PATCH 41/72] soc: apple: aop: stop the T6000 endpoints for a deep sleep at .prepare macOS stops the M1 Pro AOP's AFK endpoints 0x20 to 0x2b for a sleep, started or not, then tells the AOP the AP goes idle. It does this at the platform's .prepare, right after the GPU firmware's stop, and nothing there refuses the sleep. After the sleep the AP on notice goes out only if the idle notice was answered, and every endpoint that took the stop is started again. The driver stops only the started endpoints, in its late suspend, and waits for each, and a missing answer refuses the sleep. Add a third s2r_notice bit for macOS's way and set it for T6000. With it the stops go out together and the idle notice follows once the started endpoints have answered theirs, with one deadline for them all as the shutdown has; one that does not answer is reported and the sleep goes on. The AOP registers on the sleep driver's .prepare notifier, after the GPU's, and on its .finish notifier, before the GPU's, which undoes the stop on every path that ran .prepare; its late suspend and its resume then find nothing to do. The AOP keeps its power through the sleep and its stop is undone by messages alone, so a dry run stops and starts it as the late suspend does on the other SoCs. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/aop.rs | 233 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 230 insertions(+), 3 deletions(-) diff --git a/drivers/soc/apple/aop.rs b/drivers/soc/apple/aop.rs index 3099c44a084d1..5dde2faec02d6 100644 --- a/drivers/soc/apple/aop.rs +++ b/drivers/soc/apple/aop.rs @@ -82,6 +82,7 @@ use kernel::{ }, types::{ ForeignOwnable, + Opaque, ScopeGuard, // }, workqueue::{ @@ -1372,6 +1373,15 @@ struct AopData { sleep_stopped: Atomic, /// The firmware was told the AP is going to sleep. ap_idle: Atomic, + /// [`AOP_S2R_UNANSWERED`]: the sleep's stops go out at the platform's .prepare, through + /// this notifier, instead of in suspend_late. + #[cfg_attr(not(CONFIG_APPLE_SOC_SLEEP), allow(dead_code))] + prepare_nb: SleepNotifier, + /// And are undone at the platform's .finish, through this one, instead of at resume. + #[cfg_attr(not(CONFIG_APPLE_SOC_SLEEP), allow(dead_code))] + finish_nb: SleepNotifier, + /// [`Self::prepare_nb`] and [`Self::finish_nb`] are registered. + stop_at_prepare: Atomic, #[pin] hid_listeners: Mutex>, #[pin] @@ -1572,6 +1582,9 @@ impl AopData { crashed: Atomic::new(false), sleep_stopped: Atomic::new(0), ap_idle: Atomic::new(false), + prepare_nb: SleepNotifier(Opaque::zeroed()), + finish_nb: SleepNotifier(Opaque::zeroed()), + stop_at_prepare: Atomic::new(false), hid_listeners <- new_mutex!(KVec::new()), report_listeners <- new_mutex!(KVec::new()), source_ring <- new_mutex!(None), @@ -1818,6 +1831,138 @@ impl AopData { .ok_or(ETIMEDOUT) } + /// Sends `msg` to the first [`AOP_S2R_UNANSWERED_EPS`] endpoints in `eps` (by index), + /// without waiting for answers. Returns those it went out to, and of those the started + /// ones, whose answers [`Self::sleep_wait_answers`] can wait for. + fn sleep_send_unanswered(&self, eps: u32, msg: u64) -> (u32, u32) { + let mut running = 0u32; + for index in 0..AOP_S2R_UNANSWERED_EPS { + if eps & (1 << index) != 0 && self.endpoints[index as usize].lock().started { + running |= 1 << index; + } + } + let mut guard = self.rtkit.lock(); + let Some(mut rtk) = guard.as_mut().as_pin_mut() else { + return (0, 0); + }; + let mut sent = 0; + for index in 0..AOP_S2R_UNANSWERED_EPS { + if eps & (1 << index) == 0 { + continue; + } + let endpoint = AFK_ENDPOINT_START + index; + // An answer left from an earlier message must not count for this one. + self.ep_sleep[index as usize].reset(); + match rtk.as_mut().send_message(endpoint, msg) { + Ok(()) => sent |= 1 << index, + Err(e) => dev_dbg!( + self.dev, + "Endpoint {:#04x}: {:#x} not sent ({:?})\n", + endpoint, + msg, + e + ), + } + } + (sent, sent & running) + } + + /// Waits for the answers of the endpoints in `eps` (by index), with one deadline for + /// them all, as the shutdown does, so a firmware that stopped answering costs one + /// [`AFK_SLEEP_TIMEOUT_MS`]. Returns those that did not answer. + fn sleep_wait_answers(&self, eps: u32) -> u32 { + let mut left = msecs_to_jiffies(AFK_SLEEP_TIMEOUT_MS); + let mut missing = 0u32; + for index in 0..AOP_S2R_UNANSWERED_EPS { + if eps & (1 << index) == 0 { + continue; + } + let (answer, rest) = self.ep_sleep[index as usize].wait_jiffies(left); + left = rest; + if answer.is_none() { + missing |= 1 << index; + } + } + missing + } + + /// [`AOP_S2R_UNANSWERED`]: the stops, the started endpoints' answers and the idle notice; + /// nothing refuses. + fn sleep_enter_unanswered(&self) { + let all = (1u32 << AOP_S2R_UNANSWERED_EPS) - 1; + let (sent, started) = self.sleep_send_unanswered(all, AFK_MSG_SLEEP); + self.sleep_stopped.store(sent, Relaxed); + let missing = self.sleep_wait_answers(started); + if missing != 0 { + dev_warn!( + self.dev, + "Endpoints did not answer their stop within {} ms (mask {:#x}, bit 0 = endpoint {:#04x}), sleeping anyway\n", + AFK_SLEEP_TIMEOUT_MS, + missing, + AFK_ENDPOINT_START + ); + } + match self.ap_power(false) { + Ok(()) => self.ap_idle.store(true, Relaxed), + Err(e) => dev_warn!( + self.dev, + "Sleep notice not answered ({:?}), sleeping anyway\n", + e + ), + } + } + + /// [`AOP_S2R_UNANSWERED`]: the stops and the idle notice go out at the sleep's .prepare, + /// right after the GPU firmware's stop, as the T6000's sleep has them: from there on + /// nothing gives the sleep up for a wakeup event. The wake notice and the starts go out at + /// the sleep's .finish, which follows every .prepare, given up or not: after the PMP's + /// restart, right before the GPU firmware's, and before the SMC is told of the wake. + /// Without the sleep core they stay in suspend_late and resume. + fn stop_at_prepare_register(&self) { + #[cfg(CONFIG_APPLE_SOC_SLEEP)] + { + let finish = self.finish_nb.0.get(); + let prepare = self.prepare_nb.0.get(); + // SAFETY: `finish` and `prepare` live as long as `self`, whose removal unregisters + // them first; they aren't registered yet, so nothing else reads them. + let ret = unsafe { + (*finish).notifier_call = Some(aop_finish_notify); + // Before the GPU's, which has the default priority 0. + (*finish).priority = 1; + (*prepare).notifier_call = Some(aop_prepare_notify); + // After the GPU's, which has the default priority 0. + (*prepare).priority = -1; + let ret = apple_sleep_finish_notifier_register(finish); + if ret != 0 { + ret + } else { + let ret = apple_sleep_prepare_notifier_register(prepare); + if ret != 0 { + apple_sleep_finish_notifier_unregister(finish); + } + ret + } + }; + if ret == 0 { + self.stop_at_prepare.store(true, Release); + } else { + dev_warn!(self.dev, "Sleep stops stay in suspend_late ({})\n", ret); + } + } + } + + fn stop_at_prepare_unregister(&self) { + #[cfg(CONFIG_APPLE_SOC_SLEEP)] + if self.stop_at_prepare.xchg(false, Acquire) { + // SAFETY: registered by `stop_at_prepare_register()`; each returns once no call of + // it runs. + unsafe { + apple_sleep_prepare_notifier_unregister(self.prepare_nb.0.get()); + apple_sleep_finish_notifier_unregister(self.finish_nb.0.get()); + } + } + } + /// Tells the firmware about the AP's power state. Waits with the RTKit lock /// dropped, as the answer shares a worker with messages that take it. fn ap_power(&self, on: bool) -> Result<()> { @@ -1851,6 +1996,11 @@ impl AopData { dev_err!(self.dev, "Wake notice not answered ({:?})", e); } } + if self.s2r_notice & AOP_S2R_UNANSWERED != 0 { + let _ = self.sleep_send_unanswered(stopped, AFK_MSG_START); + self.sleeping.store(false, Release); + return; + } let order = AfkOrder::new(&self.wake_endpoints); for endpoint in order { let Some(index) = afk_endpoint_index(endpoint) else { @@ -1887,8 +2037,12 @@ impl AopData { drop(turn.lock()); } - let mut result = Ok(()); let notice = self.s2r_notice; + if notice & AOP_S2R_UNANSWERED != 0 { + self.sleep_enter_unanswered(); + return Ok(()); + } + let mut result = Ok(()); // An empty order is every endpoint: without the stops, the loop doesn't run. let stops = AfkOrder::new(&self.sleep_endpoints) .take_while(|_| notice & AOP_S2R_ENDPOINTS != 0); @@ -2506,6 +2660,7 @@ impl AOP for AopData { return; } } + self.stop_at_prepare_unregister(); // No registration is queued after this point, so once the queue is // empty the list of children is complete. self.registration_queue.drain(); @@ -3094,6 +3249,15 @@ struct AopHwConfig { const AOP_S2R_ENDPOINTS: u8 = 1 << 0; /// [`AopHwConfig::s2r_notice`]: the AP's idle and on notices around a sleep. const AOP_S2R_AP_IDLE: u8 = 1 << 1; +/// [`AopHwConfig::s2r_notice`], with both of the above, as macOS does on T6000: every AFK +/// endpoint from 0x20 to 0x2b is stopped, started or not, all at once. The AP's idle notice +/// follows once the started ones have answered their stop, or [`AFK_SLEEP_TIMEOUT_MS`] after +/// the stops for all of them together; the others are not waited for. Neither can refuse the +/// sleep. The on notice only follows an answered idle notice; the endpoints that took the stop +/// start again either way. +const AOP_S2R_UNANSWERED: u8 = 1 << 2; +/// The endpoints [`AOP_S2R_UNANSWERED`] stops, from [`AFK_ENDPOINT_START`]. +const AOP_S2R_UNANSWERED_EPS: u8 = 12; impl AopHwConfig { /// The configuration to start the block with, given whether the @@ -3178,7 +3342,7 @@ const HW_CFG_T6000: AopHwConfig = AopHwConfig { bootargs_word_access: false, halt_on_failed_start: false, without_fw_regions: None, - s2r_notice: AOP_S2R_ENDPOINTS | AOP_S2R_AP_IDLE, + s2r_notice: AOP_S2R_ENDPOINTS | AOP_S2R_AP_IDLE | AOP_S2R_UNANSWERED, }; const HW_CFG_T6020: AopHwConfig = AopHwConfig { ec0p: 0x0100_00000000, @@ -3360,16 +3524,76 @@ unsafe extern "C" fn aop_suspend_late(dev: *mut bindings::device) -> c_int { return 0; } // SAFETY: by this function's contract. - match unsafe { aop_from_dev(dev) }.sleep_enter() { + let data = unsafe { aop_from_dev(dev) }; + // Stopped at the sleep's .prepare already. + if data.stop_at_prepare.load(Acquire) { + return 0; + } + match data.sleep_enter() { Ok(()) => 0, Err(e) => e.to_errno(), } } +/// A notifier block of [`AopData::stop_at_prepare_register()`]. +#[repr(transparent)] +struct SleepNotifier(Opaque); + +// SAFETY: written only before its registration; then only the notifier chain uses it, under the +// chain's own lock. +unsafe impl Send for SleepNotifier {} +// SAFETY: as above. +unsafe impl Sync for SleepNotifier {} + +#[cfg(CONFIG_APPLE_SOC_SLEEP)] +extern "C" { + fn apple_sleep_prepare_notifier_register(nb: *mut bindings::notifier_block) -> c_int; + fn apple_sleep_prepare_notifier_unregister(nb: *mut bindings::notifier_block); + fn apple_sleep_finish_notifier_register(nb: *mut bindings::notifier_block) -> c_int; + fn apple_sleep_finish_notifier_unregister(nb: *mut bindings::notifier_block); +} + +/// # Safety +/// +/// Called by the sleep's .prepare chain with the `prepare_nb` of a registered [`AopData`]. +#[cfg(CONFIG_APPLE_SOC_SLEEP)] +unsafe extern "C" fn aop_prepare_notify( + nb: *mut bindings::notifier_block, + _action: c_ulong, + _data: *mut c_void, +) -> c_int { + // SAFETY: `nb` is the `prepare_nb` of a live `AopData`: its removal unregisters it before + // anything of it goes. + let data = unsafe { &*kernel::container_of!(nb.cast::(), AopData, prepare_nb) }; + // As at suspend_late: for this firmware nothing refuses. + if let Err(e) = data.sleep_enter() { + dev_warn!(data.dev, "Sleep stops failed ({:?})\n", e); + } + bindings::NOTIFY_OK as c_int +} + +/// # Safety +/// +/// Called by the sleep's .finish chain with the `finish_nb` of a registered [`AopData`]. +#[cfg(CONFIG_APPLE_SOC_SLEEP)] +unsafe extern "C" fn aop_finish_notify( + nb: *mut bindings::notifier_block, + _action: c_ulong, + _data: *mut c_void, +) -> c_int { + // SAFETY: `nb` is the `finish_nb` of a live `AopData`: its removal unregisters it before + // anything of it goes. + let data = unsafe { &*kernel::container_of!(nb.cast::(), AopData, finish_nb) }; + // What the .prepare chain stopped; the resume after it then finds nothing to undo. + data.sleep_undo(); + bindings::NOTIFY_OK as c_int +} + /// # Safety /// /// Called by the PM core with a bound AOP device. unsafe extern "C" fn aop_resume(dev: *mut bindings::device) -> c_int { + // Nothing left to undo where the sleep's .finish chain already did it. // SAFETY: by this function's contract. unsafe { aop_from_dev(dev) }.sleep_undo(); 0 @@ -3517,6 +3741,9 @@ impl platform::Driver for AopDriver { } return Err(e); } + if cfg.s2r_notice & AOP_S2R_UNANSWERED != 0 { + data.stop_at_prepare_register(); + } probe_guard.dismiss(); let data = data as Arc; Ok(Self(data)) From 149131d777c6c1bbdce73643582c868009b7b7f8 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 02:23:06 +0100 Subject: [PATCH 42/72] soc: apple: sleep: let a profile act right after the SMC's sleep warning On the M1 Pro, macOS tells the PMP of the coming sleep right after the SMC's slpw, before any device suspends. A gen-1 SMC sends slpw from its own .prepare and registers its sleep lane right after it: give the SoC profile an optional hook there. A debug link that holds slpw back to the noirq phase skips it. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/sleep.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index cc51ca6ee643d..4a6ee95cb7237 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -626,6 +626,8 @@ struct apple_sleep_ops { bool (*domain_kept)(struct apple_sleep *s, int off); /* Optional: at .prepare, every device suspended and the SMC told the sleep is coming */ void (*prepare)(struct apple_sleep *s); + /* Optional: right after the SMC's sleep warning (slpw), from the SMC's own .prepare */ + void (*smc_warned)(struct apple_sleep *s); /* Optional: at .end, last, once the watchdog is back as user space had it */ void (*end)(struct apple_sleep *s); @@ -2258,6 +2260,9 @@ int apple_sleep_smc_register_saved(struct device *dev, s->smc.saved_ops = ops; s->smc.late_notice = ops->late_notice; s->smc.data = data; + /* A gen-1 SMC registers right after slpw, unless a debug link holds slpw back. */ + if (s->soc->ops->smc_warned && !apple_sleep_debug_link_up()) + s->soc->ops->smc_warned(s); return 0; } EXPORT_SYMBOL_GPL(apple_sleep_smc_register_saved); From 5c69320b964ec3f2a32ffa1e9b9812fa05d8f353 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Mon, 30 Nov 2026 23:37:44 +0100 Subject: [PATCH 43/72] soc: apple: sleep: T6000: take the internal display and GFX port down The internal display block (disp0) and the GPU fabric port are taken down before the sleep, while the PMP still runs, in the order macOS 13 does it at sleep entry. The AFR cluster, AFR and GFX are powered up with the PMP GFX request raised, the GPU fabric block gets its setup, the request is dropped, the fabric handshake runs, then GFX and AFR go down. disp0 gets its fabric block setup, its PMP device request is dropped and acknowledged within 200 ms, as macOS waits, its CPU power state goes down, then the fabric handshake and its front end. Both requests go through apple_pmp_report_sleep_request(), which leaves the report's books as they are. disp0 still up after its waits refuses the sleep with -EBUSY. This runs as the profile's display_off, before the enclave and the SMC are told. t6000_sleep_display_undo() raises again the PMP requests that were set and puts the AFR cluster word back for an entry given up after it. The file is for the T6000 profile to include. It touches no external display block. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/sleep-t6000-display.inc | 355 ++++++++++++++++++++++ 1 file changed, 355 insertions(+) create mode 100644 drivers/soc/apple/sleep-t6000-display.inc diff --git a/drivers/soc/apple/sleep-t6000-display.inc b/drivers/soc/apple/sleep-t6000-display.inc new file mode 100644 index 0000000000000..453b1bff9eb3c --- /dev/null +++ b/drivers/soc/apple/sleep-t6000-display.inc @@ -0,0 +1,355 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* + * T6000 (M1 Pro): the internal display block and the GPU's fabric port taken down for a deep + * sleep, in the order macOS 13.7.8 takes them down at sleep entry, while the PMP still runs. + * Included by the T6000 profile, which calls t6000_display_map() from its .map_regs, + * t6000_sleep_display_off() as its .display_off and t6000_sleep_display_undo() when an entry + * that ran .display_off is given up before the SoC went down. + * + * Its driver has stopped the display coprocessor (IDLE, CPU stopped) by then. Each block is + * first dropped from the PMP's device requests and acknowledged, then its power states are + * lowered. Before a power state goes, the fabric block behind it gets the setup macOS writes + * when it powers the block up (Linux leaves it at reset values) and the fabric handshake. A + * display block still up after its waits refuses the sleep, before SEP and the SMC are told. + * + * Only disp0, the internal display. The external display blocks (dispext0, dispext1) are not + * touched here. + */ + +/* Power state words, in the PMGR blocks at 0x28e080000 and 0x28e580000 */ +#define T6000_DISP_PS_EAST 0x28e580000ULL +#define T6000_DISP_PS_EAST_LEN 0x1000 +#define T6000_DISP0_CPU0_PS 0x350 /* east */ +#define T6000_DISP0_FE_PS 0x328 /* east */ +#define T6000_GFX_PS 0x3c0 /* east */ +#define T6000_AFR_PS 0x28e0801e8ULL +/* The AFR cluster's word (cluster 5) of the PMGR cluster power states */ +#define T6000_AFR_CLUSTER_PS (0x28e098000ULL + 4 * 5) +#define T6000_AFR_CLUSTER_TARGET GENMASK(3, 0) +#define T6000_AFR_CLUSTER_UP BIT(5) +#define T6000_AFR_CLUSTER_BUSY BIT(4) +#define T6000_AFR_GATE 0x28e5e4000ULL +/* The fabric blocks set up below: the GPU's and disp0's */ +#define T6000_GFX_BLK 0x405000000ULL +#define T6000_DISP0_BLK 0x38c000000ULL +#define T6000_FABRIC_BLK_LEN 0xc000 +#define T6000_FABRIC_HS_8014 0x8014 +#define T6000_FABRIC_HS_82BC 0x82bc + +/* PMP device requests (bits of SOC-DEV-PS-REQ): the GPU and disp0 */ +#define T6000_PMP_AGX_ID 9 +#define T6000_PMP_DISP0_ID 12 +#define T6000_PMP_ACK_US 200000 + +/* What .display_off found stuck: the bits macOS's order would give them, disp0 refuses */ +#define T6000_DISP0_STUCK_CPU0 BIT(2) +#define T6000_DISP0_STUCK_FE BIT(3) +#define T6000_DISP0_STUCK_PMP BIT(9) +#define T6000_DISP0_STUCK (T6000_DISP0_STUCK_CPU0 | T6000_DISP0_STUCK_FE | \ + T6000_DISP0_STUCK_PMP) + +/* + * The disp0 fabric block's setup, as macOS writes it when it powers the block up (hv trace, + * macOS 13.7.8 boot, 0x38c000000). + */ +static const u32 t6000_disp0_init[][2] = { + { 0x00000, 0x11 }, { 0x00010, 0x610000 }, { 0x00014, 0x8 }, { 0x00018, 0x8 }, + { 0x0001c, 0x18 }, { 0x00020, 0x18 }, { 0x00024, 0x1 }, { 0x00028, 0x1 }, + { 0x0002c, 0x18 }, { 0x00030, 0x18 }, { 0x00034, 0x18 }, { 0x00400, 0x40a10001 }, + { 0x00600, 0xaab }, { 0x00700, 0x1ffffff }, { 0x008a8, 0x2000005 }, { 0x008ac, 0x14d004e }, + { 0x008b0, 0x14d004e }, { 0x008b4, 0x14d004e }, { 0x008b8, 0x14d004e }, + { 0x008bc, 0x14d004e }, { 0x008c0, 0x14d004e }, { 0x008c4, 0x14d004e }, + { 0x008c8, 0x14d004e }, { 0x00410, 0x1100 }, + { 0x00420, 0x1100 }, { 0x00430, 0x1100 }, { 0x08000, 0x9 }, { 0x00a20, 0x2 }, + { 0x08008, 0x7 }, { 0x08014, 0x1 }, { 0x08018, 0x1 }, { 0x00920, 0x3fff0001 }, + { 0x08208, 0x8 }, { 0x08280, 0x20 }, { 0x08288, 0x3 }, { 0x0828c, 0xc }, + { 0x08290, 0x18 }, { 0x08294, 0x30 }, { 0x08298, 0x78 }, { 0x0829c, 0xf0 }, + { 0x082b8, 0x1 }, { 0x082bc, 0x1 }, { 0x082c0, 0x1 }, { 0x00860, 0x1 }, + { 0x0820c, 0x5 }, { 0x08284, 0x20 }, { 0x082a0, 0x3 }, { 0x082a4, 0xc }, + { 0x082a8, 0x18 }, { 0x082ac, 0x30 }, { 0x082b0, 0x78 }, { 0x082b4, 0xf0 }, + { 0x082b8, 0x3 }, { 0x082bc, 0x2 }, { 0x082c0, 0x3 }, { 0x08210, 0x0 }, + { 0x08408, 0x3 }, { 0x08418, 0x3 }, { 0x0841c, 0x0 }, { 0x08420, 0xffffffff }, + { 0x08424, 0x0 }, { 0x08428, 0xfff }, { 0x082b8, 0x7 }, { 0x082bc, 0x4 }, + { 0x082c0, 0x7 }, +}; + +/* The GPU fabric block's setup, macOS 13.7.8 sleep entry, after the GFX power state is on */ +static const u32 t6000_gfx_init[][2] = { + { 0x714, 0x2000000 }, { 0x718, 0x400080 }, { 0x71c, 0x400080 }, { 0x720, 0x400080 }, + { 0x724, 0x400080 }, { 0x728, 0x400080 }, { 0x72c, 0x400080 }, { 0x730, 0x400080 }, + { 0x734, 0x400080 }, { 0x00, 0x11 }, { 0x10, 0xd0000 }, { 0x14, 0x1 }, + { 0x18, 0x1 }, { 0x1c, 0x3 }, { 0x20, 0x3 }, { 0x24, 0x3 }, + { 0x28, 0x3 }, { 0x2c, 0x3 }, { 0x400, 0x40a10001 }, { 0x600, 0x1ffffff }, + { 0x800, 0x101 }, { 0x410, 0x1100 }, { 0x420, 0x1100 }, { 0x430, 0x1100 }, + { 0x8000, 0x9 }, { 0x820, 0x80 }, { 0x8008, 0x7 }, { 0x8014, 0x1 }, + { 0x8018, 0x1 }, { 0x748, 0x1 }, { 0x8208, 0x2 }, { 0x8280, 0x20 }, + { 0x8288, 0x3 }, { 0x828c, 0xc }, { 0x8290, 0x18 }, { 0x8294, 0x30 }, + { 0x8298, 0x78 }, { 0x829c, 0xf0 }, { 0x82b8, 0x7 }, { 0x82bc, 0x1 }, + { 0x82c0, 0x7 }, { 0x7a8, 0x1 }, { 0x820c, 0x5 }, { 0x8284, 0x20 }, + { 0x82a0, 0x3 }, { 0x82a4, 0xc }, { 0x82a8, 0x18 }, { 0x82ac, 0x30 }, + { 0x82b0, 0x78 }, { 0x82b4, 0xf0 }, { 0x82b8, 0x7 }, { 0x82bc, 0x2 }, + { 0x82c0, 0x7 }, { 0x8210, 0x0 }, { 0x8408, 0xd }, { 0x8418, 0x3 }, + { 0x841c, 0x0 }, { 0x8420, 0xffffffff }, { 0x8424, 0x0 }, { 0x8428, 0xfff }, + { 0x82b8, 0x7 }, { 0x82bc, 0x4 }, { 0x82c0, 0x7 }, +}; + +/* macOS's AFR power state writes after GFX goes down, in order */ +static const u32 t6000_gfx_afr_down[] = { + 0x404004f, 0xf0403ff, 0x1f0400ff, 0x140f024f, 0xf0f02ff, 0x4040340, +}; + +static struct t6000_display { + void __iomem *east_ps; + void __iomem *afr_ps; + void __iomem *afr_cluster; + void __iomem *afr_gate; + void __iomem *gfx_blk; + void __iomem *disp0_blk; + /* The PMP report and disp0's entry in it */ + struct device_node *pmp_report; + struct device_node *pmp_disp0; + + /* What the last .display_off changed, for t6000_sleep_display_undo() */ + bool afr_cluster_saved; + u32 afr_cluster_pre; + bool agx_was_requested; + bool disp0_was_requested; + bool undo_due; + + /* For the report */ + u32 gfx_stuck, disp_stuck; + u32 gfx_seen[4], disp_read[2]; +} t6000_display; + +static void __iomem *t6000_display_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 int t6000_display_map(struct apple_sleep *s) +{ + struct t6000_display *d = &t6000_display; + struct device_node *np; + u32 id; + + d->east_ps = t6000_display_ioremap(s, T6000_DISP_PS_EAST, T6000_DISP_PS_EAST_LEN); + d->afr_ps = t6000_display_ioremap(s, T6000_AFR_PS, 4); + d->afr_cluster = t6000_display_ioremap(s, T6000_AFR_CLUSTER_PS, 4); + d->afr_gate = t6000_display_ioremap(s, T6000_AFR_GATE, 4); + d->gfx_blk = t6000_display_ioremap(s, T6000_GFX_BLK, T6000_FABRIC_BLK_LEN); + d->disp0_blk = t6000_display_ioremap(s, T6000_DISP0_BLK, T6000_FABRIC_BLK_LEN); + if (!d->east_ps) + return -ENOMEM; + + /* Kept for good, as the report is */ + d->pmp_report = of_find_compatible_node(NULL, NULL, "apple,t6000-pmp-v2-report"); + for_each_child_of_node(d->pmp_report, np) { + if (!of_property_read_u32(np, "reg", &id) && id == T6000_PMP_DISP0_ID) { + d->pmp_disp0 = np; + break; + } + } + if (!d->pmp_report || !d->pmp_disp0) + dev_warn(s->dev, "T6000: no PMP report entry for disp0, its request stays up\n"); + + return 0; +} + +/* + * The GPU's fabric port as macOS leaves it for the sleep: the AFR cluster, AFR and GFX up, the + * PMP's GFX request raised so that GFX stays up, the fabric block set up, the request dropped + * again, the fabric handshake, then GFX and AFR down. Linux otherwise reaches the sleep with + * GFX at 0xf000b00. Nothing here refuses the sleep: what got stuck is only reported. + */ +static void t6000_sleep_gfx_down(struct apple_sleep *s) +{ + struct t6000_display *d = &t6000_display; + void __iomem *gfx = d->east_ps + T6000_GFX_PS; + bool was = false; + unsigned int j; + int ret; + u32 v; + + d->gfx_stuck = 0; + d->afr_cluster_saved = false; + if (!d->gfx_blk || !d->afr_ps || !d->pmp_report) + return; + d->gfx_seen[0] = readl(gfx); + d->gfx_seen[1] = readl(d->afr_ps); + /* The AFR cluster up first: 0xf, it reads back 0x2f once up, then 0x2f */ + if (d->afr_cluster) { + d->afr_cluster_pre = readl(d->afr_cluster); + d->afr_cluster_saved = true; + writel(0xf, d->afr_cluster); + if (readl_poll_timeout_atomic(d->afr_cluster, v, v & T6000_AFR_CLUSTER_UP, 1, 50000)) + d->gfx_stuck |= BIT(6); + writel(0x2f, d->afr_cluster); + } + if (d->afr_gate) + writel(1, d->afr_gate); + writel(0xf040000, d->afr_ps); + writel(0xf04000f, d->afr_ps); + writel(0x1f04000f, d->afr_ps); + if (readl_poll_timeout_atomic(d->afr_ps, v, FIELD_GET(PS_ACTUAL, v), 1, 50000)) + d->gfx_stuck |= BIT(0); + writel(0xf000000, gfx); + writel(0xf00000f, gfx); + writel(0x1f00000f, gfx); + /* GFX reaches 4, and the PMP drops it again unless its request is up: raise that first. */ + ret = apple_pmp_report_sleep_request(d->pmp_report, T6000_PMP_AGX_ID, true, + T6000_PMP_ACK_US, &was); + d->agx_was_requested = !ret && was; + if (ret) + d->gfx_stuck |= BIT(2); + /* The fabric block is in the GFX domain: never write it unpowered. */ + if (readl_poll_timeout_atomic(gfx, v, FIELD_GET(PS_ACTUAL, v), 1, 50000)) { + d->gfx_stuck |= BIT(1); + } else { + for (j = 0; j < ARRAY_SIZE(t6000_gfx_init); j++) + writel(t6000_gfx_init[j][1], d->gfx_blk + t6000_gfx_init[j][0]); + } + if (apple_pmp_report_sleep_request(d->pmp_report, T6000_PMP_AGX_ID, false, + T6000_PMP_ACK_US, NULL)) + d->gfx_stuck |= BIT(3); + if (!(d->gfx_stuck & BIT(1)) && FIELD_GET(PS_ACTUAL, readl(gfx))) { + writel(1, d->gfx_blk + T6000_FABRIC_HS_8014); + writel(1, d->gfx_blk + T6000_FABRIC_HS_82BC); + writel(2, d->gfx_blk + T6000_FABRIC_HS_82BC); + writel(4, d->gfx_blk + T6000_FABRIC_HS_82BC); + } else { + d->gfx_stuck |= BIT(7); + } + writel(0xf0000ff, gfx); + writel(0xf0003f0, gfx); + if (readl_poll_timeout_atomic(gfx, v, !FIELD_GET(PS_ACTUAL, v), 1, 10000)) + d->gfx_stuck |= BIT(4); + d->gfx_seen[2] = v; + for (j = 0; j < ARRAY_SIZE(t6000_gfx_afr_down); j++) + writel(t6000_gfx_afr_down[j], d->afr_ps); + if (readl_poll_timeout_atomic(d->afr_ps, v, !FIELD_GET(PS_ACTUAL, v), 1, 10000)) + d->gfx_stuck |= BIT(5); + d->gfx_seen[3] = v; + if (d->afr_gate) + writel(0, d->afr_gate); + if (d->afr_cluster) + writel(0, d->afr_cluster); + dev_dbg(s->dev, "T6000: GFX down: stuck %#x, gfx %#x -> %#x, afr %#x -> %#x\n", + d->gfx_stuck, d->gfx_seen[0], d->gfx_seen[2], d->gfx_seen[1], d->gfx_seen[3]); +} + +/* + * disp0 in macOS's order: its CPU's power state, then its front end's (hv trace, macOS 13.7.8 + * sleep entry). Before the CPU's: the fabric block's setup and the PMP's disp0 request dropped + * and acknowledged. Before the front end's: the fabric handshake, +0x8014 = 1, +0x82bc = 1, 2, + * 4. The fabric block only answers while the front end has power: a write to it otherwise is + * an SError. + */ +static void t6000_sleep_disp0_down(struct apple_sleep *s) +{ + struct t6000_display *d = &t6000_display; + void __iomem *cpu0 = d->east_ps + T6000_DISP0_CPU0_PS; + void __iomem *fe = d->east_ps + T6000_DISP0_FE_PS; + void __iomem *blk = d->disp0_blk; + unsigned int j; + u32 v; + + d->disp_stuck = 0; + d->disp0_was_requested = false; + + if (blk && !FIELD_GET(PS_ACTUAL, readl(fe))) + blk = NULL; + if (blk) + for (j = 0; j < ARRAY_SIZE(t6000_disp0_init); j++) + writel(t6000_disp0_init[j][1], blk + t6000_disp0_init[j][0]); + /* + * The request word only: the report's books keep disp0 on, as the PMP's wake rewrites + * them. Acknowledged within 200 ms, as macOS waits. + */ + if (d->pmp_disp0) { + bool was = false; + + if (apple_pmp_report_sleep_request(d->pmp_report, T6000_PMP_DISP0_ID, false, + T6000_PMP_ACK_US, &was)) + d->disp_stuck |= T6000_DISP0_STUCK_PMP; + d->disp0_was_requested = was; + } + writel(0xf0400ff, cpu0); + writel(0xf0403f0, cpu0); + if (readl_poll_timeout_atomic(cpu0, v, !FIELD_GET(PS_ACTUAL, v), 1, 10000)) + d->disp_stuck |= T6000_DISP0_STUCK_CPU0; + d->disp_read[0] = readl(cpu0); + + if (blk && !FIELD_GET(PS_ACTUAL, readl(fe))) + blk = NULL; + if (blk) { + writel(1, blk + T6000_FABRIC_HS_8014); + writel(1, blk + T6000_FABRIC_HS_82BC); + writel(2, blk + T6000_FABRIC_HS_82BC); + writel(4, blk + T6000_FABRIC_HS_82BC); + } + writel(0x3f0, fe); + if (readl_poll_timeout_atomic(fe, v, !FIELD_GET(PS_ACTUAL, v), 1, 10000)) + d->disp_stuck |= T6000_DISP0_STUCK_FE; + d->disp_read[1] = readl(fe); + dev_dbg(s->dev, "T6000: disp0 down: stuck %#x, cpu0 %#x, fe %#x\n", d->disp_stuck, + d->disp_read[0], d->disp_read[1]); +} + +/* + * The GPU's fabric port, then disp0, before SEP and the SMC are told. A display block that + * stays up refuses the sleep: the SoC must never go down around it. + */ +static int t6000_sleep_display_off(struct apple_sleep *s) +{ + struct t6000_display *d = &t6000_display; + + if (!d->east_ps) + return -ENODEV; + d->undo_due = true; + t6000_sleep_gfx_down(s); + t6000_sleep_disp0_down(s); + if (d->disp_stuck & T6000_DISP0_STUCK) { + dev_err(s->dev, "disp0 did not power down (%#x), not sleeping\n", d->disp_stuck); + return -EBUSY; + } + + return 0; +} + +/* + * An entry given up after t6000_sleep_display_off() and before the SoC went down. A running + * PMP keeps a block down until its request is up again; a stopped one gets its requests back + * when it starts. The AFR cluster word goes back to what it was. Raising the power states the + * entry lowered is left to the profile's restore of every power state, after this. + */ +static void t6000_sleep_display_undo(struct apple_sleep *s) +{ + struct t6000_display *d = &t6000_display; + u32 v; + + if (!d->undo_due) + return; + d->undo_due = false; + if (d->disp0_was_requested && + apple_pmp_report_sleep_request(d->pmp_report, T6000_PMP_DISP0_ID, true, + T6000_PMP_ACK_US, NULL)) + dev_warn(s->dev, "T6000: PMP did not take the disp0 request back\n"); + d->disp0_was_requested = false; + if (d->agx_was_requested && + apple_pmp_report_sleep_request(d->pmp_report, T6000_PMP_AGX_ID, true, + T6000_PMP_ACK_US, NULL)) + dev_warn(s->dev, "T6000: PMP did not take the GFX request back\n"); + d->agx_was_requested = false; + if (d->afr_cluster && d->afr_cluster_saved) { + if ((readl(d->afr_cluster) ^ d->afr_cluster_pre) & T6000_AFR_CLUSTER_TARGET) + writel(d->afr_cluster_pre, d->afr_cluster); + readl_poll_timeout_atomic(d->afr_cluster, v, !(v & T6000_AFR_CLUSTER_BUSY), 1, + 10000); + d->afr_cluster_saved = false; + } + dev_info(s->dev, "sleep given up, powering the displays back up\n"); +} From 9fadbf0a3833a52255e804b3fa79ebbcbb949772 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 13:42:59 +0100 Subject: [PATCH 44/72] pinctrl: apple: keep the T6000 AP pins across a deep sleep A T6000 deep sleep cuts the AP pin block's power domain with the SoC, and the block comes back with every pin reset. Neither the pin core nor a consumer sets them up again. The block the sleep controller names in "apple,pinctrl" now saves its pins itself, in its noirq suspend: after every consumer has suspended and after suspend_device_irqs(). Its IRQ chip masks the pins that are not wake sources as the sleep starts, so the saved interrupt modes are the ones the IRQ core unmasks again on resume, and no pin is masked after the save. The input enable word at 0xc48 is saved with them. In the noirq resume, after the sleep has the domain on again and before any consumer resumes, the input enable word goes back first, then every pin that reads different from its saved word, through the regmap so its cache follows. The pins the sleep controller lists in "apple,pinctrl-skip-pins" are not put back, and the IRQ status registers are left alone: a bit latched there is for the IRQ handler. Only a firmware-driven suspend to RAM saves the pins. The block keeps its power domain on through every system sleep until the sleep cuts it, as the sleep's last steps need the GPIO domain on. Only a block with the T6000 compatible, a power domain that can go down and a sleep controller naming it does any of this: the AP block of a T6000 whose sleep controller is enabled. Signed-off-by: Andreas Fernandez --- drivers/pinctrl/pinctrl-apple-gpio.c | 221 ++++++++++++++++++++++++++- 1 file changed, 220 insertions(+), 1 deletion(-) diff --git a/drivers/pinctrl/pinctrl-apple-gpio.c b/drivers/pinctrl/pinctrl-apple-gpio.c index 6494d024e9d54..94e86629fbcf0 100644 --- a/drivers/pinctrl/pinctrl-apple-gpio.c +++ b/drivers/pinctrl/pinctrl-apple-gpio.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -56,6 +57,11 @@ struct apple_gpio_pinctrl { unsigned long *saved_valid; /* Pins this driver has written. Others belong to someone else. */ unsigned long *touched; + /* The pins' words across a deep sleep that cuts this block's power */ + u32 *sleep_cfg; + unsigned long *sleep_skip; + u32 sleep_input_enable; + bool sleep_saved; struct apple_gpio_irq_group irqgrps[]; }; @@ -83,6 +89,12 @@ struct apple_gpio_pinctrl { #define REG_GPIOx_LOCK BIT(21) #define REG_GPIOx_DRIVE_STRENGTH1 GENMASK(23, 22) #define REG_IRQ(g, x) (0x800 + 0x40 * (g) + 4 * ((x) >> 5)) +/* + * Input receivers of the whole block. The boot firmware sets this, and it reads + * 0 once the block has lost power. While it is clear every input reads low, so + * a level-low IRQ fires forever although nothing drives the line. + */ +#define REG_INPUT_ENABLE 0xc48 static const struct regmap_config regmap_config = { .reg_bits = 32, @@ -421,6 +433,27 @@ static const struct irq_chip apple_gpio_irqchip = { GPIOCHIP_IRQ_RESOURCE_HELPERS, }; +/* + * The same for a block whose pins are saved for a deep sleep. Every pin shares + * the group IRQs, which stay open, so the pins that are not wake sources are + * masked as the sleep starts instead of at their first interrupt. The save then + * holds the modes the IRQ core unmasks again, through the regmap, on resume, and + * no pin is masked after it. Wake sources a driver disabled are enabled for the + * sleep. + */ +static const struct irq_chip apple_gpio_irqchip_sleep = { + .name = "Apple-GPIO", + .irq_startup = apple_gpio_irq_startup, + .irq_ack = apple_gpio_irq_ack, + .irq_mask_ack = apple_gpio_irq_mask_ack, + .irq_mask = apple_gpio_irq_mask, + .irq_unmask = apple_gpio_irq_unmask, + .irq_set_type = apple_gpio_irq_set_type, + .flags = IRQCHIP_IMMUTABLE | IRQCHIP_SKIP_SET_WAKE | + IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND, + GPIOCHIP_IRQ_RESOURCE_HELPERS, +}; + /* Probe & register */ struct apple_gpio_hw { @@ -430,6 +463,11 @@ struct apple_gpio_hw { * only within the valid GPIO registers minus gpio-reserved-ranges. */ bool no_regcache; + /* + * The block a sleep controller names as the one whose pins a deep sleep + * resets keeps them itself: saved and put back by this driver. + */ + bool sleep_save; }; /* T8140 (A18 Pro) and T8132 (M4) both have faulting slots in this block. */ @@ -437,6 +475,10 @@ static const struct apple_gpio_hw apple_gpio_uncached_hw = { .no_regcache = true, }; +static const struct apple_gpio_hw apple_gpio_t6000_hw = { + .sleep_save = true, +}; + static int apple_gpio_regmap_access(struct device *dev, unsigned int npins, struct regmap_config *config) { @@ -486,6 +528,70 @@ static int apple_gpio_regmap_access(struct device *dev, unsigned int npins, return 0; } +/* + * Only a block whose power domain can go down loses its pins in a deep sleep: + * on T6000 that is the AP block. The NUB block sits in an always-on domain, and + * the SMC and AOP blocks have no domain at all. + */ +static bool apple_gpio_domain_can_power_down(struct device *dev) +{ + struct device_node *pd; + bool ret; + + pd = of_parse_phandle(dev->of_node, "power-domains", 0); + if (!pd) + return false; + + ret = !of_property_read_bool(pd, "apple,always-on"); + of_node_put(pd); + + return ret; +} + +/* + * A T6000 deep sleep cuts the AP block's power with the SoC, and the block comes + * back with every pin reset. The sleep controller names that block + * ("apple,pinctrl") and the pins to leave alone ("apple,pinctrl-skip-pins", the + * ADT's no-restore-gpio-pins, which macOS does not put back either). + */ +static int apple_gpio_sleep_init(struct apple_gpio_pinctrl *pctl, unsigned int npins) +{ + struct device *dev = pctl->dev; + struct device_node *np, *pins; + u32 pin; + int i, n; + + if (!apple_gpio_domain_can_power_down(dev)) + return 0; + + for_each_node_with_property(np, "apple,pinctrl") { + if (!of_device_is_available(np)) + continue; + pins = of_parse_phandle(np, "apple,pinctrl", 0); + of_node_put(pins); + if (pins == dev->of_node) + break; + } + if (!np) + return 0; + + pctl->sleep_cfg = devm_kcalloc(dev, npins, sizeof(*pctl->sleep_cfg), GFP_KERNEL); + pctl->sleep_skip = devm_bitmap_zalloc(dev, npins, GFP_KERNEL); + if (!pctl->sleep_cfg || !pctl->sleep_skip) { + of_node_put(np); + return -ENOMEM; + } + + n = of_property_count_u32_elems(np, "apple,pinctrl-skip-pins"); + for (i = 0; i < n; i++) + if (!of_property_read_u32_index(np, "apple,pinctrl-skip-pins", i, &pin) && + pin < npins) + set_bit(pin, pctl->sleep_skip); + of_node_put(np); + + return 0; +} + static int apple_gpio_register(struct apple_gpio_pinctrl *pctl) { struct gpio_irq_chip *girq = &pctl->gpio_chip.irq; @@ -507,7 +613,10 @@ static int apple_gpio_register(struct apple_gpio_pinctrl *pctl) if (girq->num_parents) { int i; - gpio_irq_chip_set_chip(girq, &apple_gpio_irqchip); + if (pctl->sleep_cfg) + gpio_irq_chip_set_chip(girq, &apple_gpio_irqchip_sleep); + else + gpio_irq_chip_set_chip(girq, &apple_gpio_irqchip); girq->parent_handler = apple_gpio_irq_handler; girq->parents = kmalloc_array(girq->num_parents, @@ -639,6 +748,12 @@ static int apple_gpio_pinctrl_probe(struct platform_device *pdev) return -ENOMEM; } + if (hw && hw->sleep_save) { + res = apple_gpio_sleep_init(pctl, npins); + if (res) + return res; + } + for (i = 0; i < npins; i++) { pins[i].number = i; pins[i].name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "PIN%u", i); @@ -685,12 +800,114 @@ static const struct of_device_id apple_gpio_pinctrl_of_match[] = { { .compatible = "apple,t8132-pinctrl", .data = &apple_gpio_uncached_hw }, { .compatible = "apple,t6050-pinctrl", .data = &apple_gpio_uncached_hw }, { .compatible = "apple,t6030-pinctrl", .data = &apple_gpio_uncached_hw }, + { .compatible = "apple,t6000-pinctrl", .data = &apple_gpio_t6000_hw }, { .compatible = "apple,t8103-pinctrl", }, { .compatible = "apple,pinctrl", }, { } }; MODULE_DEVICE_TABLE(of, apple_gpio_pinctrl_of_match); +/* Deep sleep */ + +/* + * The bits that tell whether a pin kept its configuration. DATA only counts + * on a GPIO output; on any other pin it follows the pad. + */ +static u32 apple_gpio_sleep_mask(u32 cfg) +{ + if (!FIELD_GET(REG_GPIOx_PERIPH, cfg) && + FIELD_GET(REG_GPIOx_MODE, cfg) == REG_GPIOx_OUT) + return ~0U; + + return ~(u32)REG_GPIOx_DATA; +} + +/* + * The block keeps its power through every system sleep until the sleep itself + * cuts it: the sleep's last steps need the GPIO domain on. + * + * A deep sleep (a firmware-driven suspend to RAM) takes the domain down with the + * SoC. Every pin is saved here, after its consumers' noirq suspend and after + * suspend_device_irqs() masked the pins that are not wake sources, so the saved + * IRQ modes are the ones the IRQ core unmasks again on resume. s2idle stays as + * it was. + */ +static void apple_gpio_sleep_save(struct apple_gpio_pinctrl *pctl) +{ + unsigned int pin; + + if (!pctl->sleep_cfg) + return; + + device_set_awake_path(pctl->dev); + if (pm_suspend_target_state != PM_SUSPEND_MEM || !pm_suspend_via_firmware()) + return; + + for (pin = 0; pin < pctl->pinctrl_desc.npins; pin++) + pctl->sleep_cfg[pin] = readl_relaxed(pctl->base + REG_GPIO(pin)); + pctl->sleep_input_enable = readl_relaxed(pctl->base + REG_INPUT_ENABLE); + pctl->sleep_saved = true; +} + +/* + * By now the sleep's .wake has the domain on again, and no consumer has resumed. + * Only pins that changed are written, so this does nothing when the block kept + * its state. The writes go through the regmap so its cache follows: a + * regcache_sync() would skip every register that equals the cached value. The + * pins the sleep controller lists are left alone, and so are the IRQ status + * registers: a bit latched there is for the IRQ handler to see. + */ +static void apple_gpio_sleep_restore(struct apple_gpio_pinctrl *pctl) +{ + struct device *dev = pctl->dev; + unsigned int pin, first = 0, lost = 0, stuck = 0; + u32 cfg, saved, first_cfg = 0; + + if (!pctl->sleep_saved) + return; + pctl->sleep_saved = false; + + /* First, so the pins below come back with their real input levels */ + cfg = readl_relaxed(pctl->base + REG_INPUT_ENABLE); + if (cfg != pctl->sleep_input_enable) { + writel_relaxed(pctl->sleep_input_enable, pctl->base + REG_INPUT_ENABLE); + dev_dbg(dev, "restored the input enable after a deep sleep (was 0x%x)\n", cfg); + } + + for (pin = 0; pin < pctl->pinctrl_desc.npins; pin++) { + if (test_bit(pin, pctl->sleep_skip)) + continue; + + saved = pctl->sleep_cfg[pin]; + cfg = readl_relaxed(pctl->base + REG_GPIO(pin)); + if (!((cfg ^ saved) & apple_gpio_sleep_mask(saved))) + continue; + + if (!lost++) { + first = pin; + first_cfg = cfg; + } + + if (regmap_write(pctl->map, REG_GPIO(pin), saved)) { + stuck++; + continue; + } + + cfg = readl_relaxed(pctl->base + REG_GPIO(pin)); + if ((cfg ^ saved) & apple_gpio_sleep_mask(saved)) + stuck++; + } + + if (!lost) + dev_dbg(dev, "pins kept their configuration through a deep sleep\n"); + else + dev_dbg(dev, "restored %u pins after a deep sleep (pin %u was 0x%08x, saved 0x%08x)\n", + lost, first, first_cfg, pctl->sleep_cfg[first]); + + if (stuck) + dev_warn(dev, "%u pins did not take their saved configuration\n", stuck); +} + /* * A firmware-driven suspend powers this block off, and neither the pin core nor * a consumer sets a pin up again. Slots that fault aren't readable in the regmap, @@ -701,6 +918,7 @@ static int apple_gpio_pinctrl_suspend_noirq(struct device *dev) struct apple_gpio_pinctrl *pctl = dev_get_drvdata(dev); unsigned int i; + apple_gpio_sleep_save(pctl); if (!pctl->saved || pm_suspend_target_state != PM_SUSPEND_MEM || !pm_suspend_via_firmware()) return 0; @@ -722,6 +940,7 @@ static int apple_gpio_pinctrl_resume_noirq(struct device *dev) struct apple_gpio_pinctrl *pctl = dev_get_drvdata(dev); unsigned int i, val, restored = 0; + apple_gpio_sleep_restore(pctl); if (!pctl->saved || !pm_resume_via_firmware()) return 0; From f06f74c8fe591d305225f07d259dd72c7206f56e Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Mon, 30 Nov 2026 23:36:44 +0100 Subject: [PATCH 45/72] soc: apple: sleep: add the T6000 profile Deep sleep for the M1 Pro (T6000, J314s), in the order macOS 13 takes it, its PMGR's sleep entry and exit and the kernel's last steps: - .begin saves every PMGR word the late power-state image writes and every PMGR power word that has a node, and holds disp0's PMP report entry for the entry's own drop (the PMP's suspend_late leaves it) until .finish. Right after the SMC's slpw the PMP telemetry sleep word goes to 4, as macOS does, and .end puts it back. - The refusals: an interrupt pending at the start of .enter and at the last check. The SEP must be bound with its sleep request staged (a gate entry): its driver stages it at device suspend, and the entry sends it as its last step that may still refuse, after pmVC and WKTP have gone to the SMC. - prepare_regs keeps every NCO channel's enable bit on, as a disabled channel that clocked an MCA cluster keeps the SIO island from power-gating, and reads each channel's control, divider and increments: the sleep resets the rate registers, and the clock driver, which keeps the rate it set, does not program them again. .wake writes them back before the channels it forced on are stopped again, as the T8122 profile does. The NCO is reached through the sleep node's apple,nco phandle. - The commit: the memory controllers drained, the error, counter and voltage-monitor words zeroed, the internal PCIe block's handshake, the CPU complexes quiesced, the memory clock and AFR down, every cluster's target to 0, the SoC clocks onto the reference clock and PLL 0 off. Then the memory controllers' power words compared with the first sleep's and one that changed put back, the parked cores stopped, WD1 at 120 s, the late power-state image written in full with its waits and 200 ms after it, the PMU RTC alarm's SPMI vector on, and WD1 again. - Every core writes back its L1 and powers down without retention, woken by a reset; the boot core is last. - The wake puts back the quiesce (the AFR cluster word as it was before the display file took it down), the SPMI vector enables and the NCO bits; .wake puts back the late image's words, brings every PMGR domain that was on back up, parents first, in up to 30 passes 10 ms apart, and gives WD1 170 s, as macOS does right after the wake. The sleep core's own save of the AP pins is skipped for the T6000 pin controller, whose driver keeps its pins across the sleep itself. The internal display block is taken down by sleep-t6000-display.inc. Its DCP is parked only for a sleep that commits, so a dry run leaves it running (display_no_restart). The wake mask is the device tree's: the 21 sources macOS leaves on. At the last WFI the AIC keeps the lines armed for wake open, as its syscore suspend left them. The SMC is told as a gen-1 SMC: slpa and its AP idle notice before the PMP stops, as macOS orders them. The profile is marked untested until J314s runs on the default command line exist, so deep sleep is offered only with apple_sleep.deep=1 and stops before the commit unless apple_sleep.untested_commit=1. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/sleep-t6000-display.inc | 45 +- drivers/soc/apple/sleep-t6000.h | 1655 +++++++++++++++++++++ drivers/soc/apple/sleep-test.c | 1 + drivers/soc/apple/sleep.c | 5 + 4 files changed, 1696 insertions(+), 10 deletions(-) create mode 100644 drivers/soc/apple/sleep-t6000.h diff --git a/drivers/soc/apple/sleep-t6000-display.inc b/drivers/soc/apple/sleep-t6000-display.inc index 453b1bff9eb3c..a7d63f652d4a3 100644 --- a/drivers/soc/apple/sleep-t6000-display.inc +++ b/drivers/soc/apple/sleep-t6000-display.inc @@ -109,12 +109,16 @@ static struct t6000_display { struct device_node *pmp_report; struct device_node *pmp_disp0; - /* What the last .display_off changed, for t6000_sleep_display_undo() */ - bool afr_cluster_saved; - u32 afr_cluster_pre; + /* + * What the last .display_off changed, for t6000_sleep_display_undo(). The AFR cluster + * word as it was is the profile's t6000_sleep.afr_cl_pre: its quiesce restore after a + * wake uses it too. + */ bool agx_was_requested; bool disp0_was_requested; bool undo_due; + /* disp0's entry is dropped here, not by the PMP's own suspend */ + bool disp0_owned; /* For the report */ u32 gfx_stuck, disp_stuck; @@ -175,15 +179,15 @@ static void t6000_sleep_gfx_down(struct apple_sleep *s) u32 v; d->gfx_stuck = 0; - d->afr_cluster_saved = false; + t6000_sleep.afr_cl_valid = false; if (!d->gfx_blk || !d->afr_ps || !d->pmp_report) return; d->gfx_seen[0] = readl(gfx); d->gfx_seen[1] = readl(d->afr_ps); /* The AFR cluster up first: 0xf, it reads back 0x2f once up, then 0x2f */ if (d->afr_cluster) { - d->afr_cluster_pre = readl(d->afr_cluster); - d->afr_cluster_saved = true; + t6000_sleep.afr_cl_pre = readl(d->afr_cluster); + t6000_sleep.afr_cl_valid = true; writel(0xf, d->afr_cluster); if (readl_poll_timeout_atomic(d->afr_cluster, v, v & T6000_AFR_CLUSTER_UP, 1, 50000)) d->gfx_stuck |= BIT(6); @@ -344,12 +348,33 @@ static void t6000_sleep_display_undo(struct apple_sleep *s) T6000_PMP_ACK_US, NULL)) dev_warn(s->dev, "T6000: PMP did not take the GFX request back\n"); d->agx_was_requested = false; - if (d->afr_cluster && d->afr_cluster_saved) { - if ((readl(d->afr_cluster) ^ d->afr_cluster_pre) & T6000_AFR_CLUSTER_TARGET) - writel(d->afr_cluster_pre, d->afr_cluster); + if (d->afr_cluster && t6000_sleep.afr_cl_valid) { + if ((readl(d->afr_cluster) ^ t6000_sleep.afr_cl_pre) & T6000_AFR_CLUSTER_TARGET) + writel(t6000_sleep.afr_cl_pre, d->afr_cluster); readl_poll_timeout_atomic(d->afr_cluster, v, !(v & T6000_AFR_CLUSTER_BUSY), 1, 10000); - d->afr_cluster_saved = false; + t6000_sleep.afr_cl_valid = false; } dev_info(s->dev, "sleep given up, powering the displays back up\n"); } + +/* + * disp0's request is dropped by t6000_sleep_disp0_down(), in .enter: the PMP's own + * suspend_late must leave it, or disp0's request drops while its coprocessor still runs. + * From the profile's .begin to its .finish. + */ +static int t6000_display_own(struct apple_sleep *s, bool own) +{ + struct t6000_display *d = &t6000_display; + int ret; + + if (!d->pmp_disp0 || d->disp0_owned == own) + return 0; + ret = apple_pmp_report_entry_commit_owned(d->pmp_disp0, own); + if (ret) { + dev_err(s->dev, "T6000: disp0's PMP report entry not held for the commit: %d\n", ret); + return ret; + } + d->disp0_owned = own; + return 0; +} diff --git a/drivers/soc/apple/sleep-t6000.h b/drivers/soc/apple/sleep-t6000.h new file mode 100644 index 0000000000000..f0abc5cd374f5 --- /dev/null +++ b/drivers/soc/apple/sleep-t6000.h @@ -0,0 +1,1655 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* + * T6000 (M1 Pro, J314s: two E cores and two P clusters of three). Included by sleep.c. + * + * The sequence is macOS 13's (its PMGR's sleep entry and exit, and the kernel's last + * steps before the sleep): the boot core quiesces the memory controller, the error and counter + * blocks, the internal PCIe block's handshake, the CPU complexes, the memory clock and the AFR + * fabric, takes every cluster's target to 0 and parks the SoC clocks on the reference clock. + * Only then are the parked cores stopped, the late power-state image written, and the S2R + * timer started. Every core then writes back its L1 and takes a WFI without retention. + * + * Register map: the PMGR power-state blocks, CPU start, wake, watchdog, S2R timer, NUB SPMI + * vector enables and PMP telemetry are the sleep node's reg, the NCO its apple,nco; the CPU + * complexes, memory controllers and the other blocks the sequence touches once are fixed for + * the SoC and mapped here, as on T8103. None of them is claimed: their own drivers map them too. + */ + +/* In "ps" (ADT /arm-io/pmgr reg[0], 0x28e080000) */ +#define T6000_PS_AFR 0x1e8 +#define T6000_PS_PMP 0x2d8 +#define T6000_CLUSTER_PS 0x18000 + +#define T6000_PS_BASE 0x28e080000ULL +#define T6000_PS_EAST_BASE 0x28e580000ULL +#define T6000_PS_SIZE 0x1000 + +/* + * The PMGR's cluster words, one per ADT soc-clusters entry: target in [3:0], bit 4 set while + * it changes. The three CPU clusters go down with their cores. + */ +#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_TARGET GENMASK(3, 0) +#define T6000_CLUSTER_BUSY BIT(4) + +/* The CPU complexes' ACC blocks (64-bit registers): ECPU, PCPU0, PCPU1 */ +#define T6000_ACC_BASE 0x210e20000ULL +#define T6000_ACC_STRIDE 0x1000000ULL +#define T6000_ACCS 3 +#define T6000_ACC_SIZE 0x29000 +#define T6000_ACC_DVFS 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_MACOS 0x40000500101ULL +#define T6000_ACC_LLC_THRTL 0x20240 +#define T6000_ACC_AMX_THRTL 0x20250 +#define T6000_ACC_THRTL_EN BIT_ULL(63) +#define T6000_ACC_PERF 0x00660 +#define T6000_ACC_PERF_EN 0x15ULL +#define T6000_ACC_ENERGY 0x28080 +#define T6000_ACC_ENERGY_EN (BIT_ULL(63) | BIT_ULL(61)) +#define T6000_ACC_PPT_THRTL 0x28400 + +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, + [T6000_ACCR_ENERGY] = T6000_ACC_ENERGY, + [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, +}; + +/* ClkGenSOC: five muxes onto the reference clock, in macOS's order (0x160 stays) */ +#define T6000_CLKGEN_MUX 0x28e03815cULL +#define T6000_CLKGEN_SIZE 0x18 +#define T6000_CLKGEN_MUXES 5 +#define T6000_CLKGEN_ON BIT(31) +#define T6000_CLKGEN_BUSY BIT(30) +#define T6000_CLKGEN_SEL GENMASK(27, 24) +static const u8 t6000_clkgen_mux_off[T6000_CLKGEN_MUXES] = { 0x08, 0x10, 0x0c, 0x00, 0x14 }; + +/* PLL 0, left in off mode 3 as macOS's setPLLOffMode() leaves it */ +#define T6000_PLL0 0x28e224000ULL +#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) + +/* The memory clock's performance state (_setDCSPerfState) */ +#define T6000_DCS_PS 0x28e20c800ULL +#define T6000_DCS_PS_TARGET GENMASK(3, 0) +#define T6000_DCS_PS_BUSY BIT(31) + +/* + * The memory controllers: each AMCC drained (+0x1c00 = 0 under the +0x10000c lock, +0x1c04 + * polled to 0), and each DCS's +0x10 with bit 12 on as XNU sets it at boot. + */ +#define T6000_AMCCS 4 +#define T6000_AMCC_BASE 0x200000000ULL +#define T6000_AMCC_STRIDE 0x1000000ULL +#define T6000_AMCC_MAP 0x160000 +#define T6000_AMCC_DRAIN_CTL 0x1c00 +#define T6000_AMCC_DRAIN_CNT 0x1c04 +#define T6000_AMCC_LOCK 0x10000c +#define T6000_DCS_PER_AMCC 4 +#define T6000_DCS_OFF(k) (0x200000ULL * ((k) + 1)) +#define T6000_DCS_CTL 0x10 +#define T6000_DCS_CTL_IBOOT 0x70a0100 +#define T6000_DCS_CTL_MACOS 0x70a1100 + +/* The internal PCIe block's quiesce handshake: 0x8014 = 1, then 0x82bc = 1, 2, 4 */ +#define T6000_APCIE_Q_8014 0x58c008014ULL +#define T6000_APCIE_Q_82BC 0x58c0082bcULL + +/* In "ptd" (PMP telemetry): the word macOS sets to 4 right after slpw */ +#define T6000_PTD_SLEEP 0x18328 + +/* In "spmi-irq": the NUB SPMI vector enables, the clear words 0x40 above */ +#define T6000_SPMI_WORDS 7 +#define T6000_SPMI_CLEAR 0x40 +/* Vector 2: the PMU RTC's alarm, on before the S2R timer as macOS has it */ +#define T6000_SPMI_VEC_RTC BIT(2) + +/* + * The NCO block (apple,nco): its five channels clock the MCA clusters. Every enable bit + * stays on across the sleep, and the sleep resets each channel's rate registers. + */ +#define T6000_NCO_CHANS 5 +#define T6000_NCO_STRIDE 0x4000 +#define T6000_NCO_REG_CTRL 0x00 +#define T6000_NCO_REG_DIV 0x04 +#define T6000_NCO_REG_INC1 0x08 +#define T6000_NCO_REG_INC2 0x0c +#define T6000_NCO_REG_ACCINIT 0x10 +#define T6000_NCO_CTRL_EN BIT(31) + +/* The NUB PMGR's power words: they keep their state through the sleep */ +#define T6000_NUB_PS 0x292280000ULL + +/* WD1 from the late image to the wake: it keeps counting through the sleep */ +#define T6000_WDT_GO_SECS 120 +/* WD1 from the wake to .end: counted again from 0, for the device resume */ +#define T6000_WDT_WAKE_SECS 170U + +/* After the late image, before the S2R timer */ +#define T6000_PS_LATE_MS 200 +/* The wake's power-domain walk: passes, 10 ms apart, while some are still off */ +#define T6000_PSALL_PASSES 30 +#define T6000_PSALL_MAX 256 +#define T6000_PSALL_BASES 8 + +/* Each core's power-down overrides */ +#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_DISABLE_PIO_ON_WFI BIT_ULL(32) +#define T6000_ACC_DEEP_SLEEP BIT_ULL(34) + +/* + * The head of macOS's PMGR sleep entry, in its order: the error-handler enables, the + * performance counters, then the SoC voltage monitors. Each written 0, and put back in reverse + * at the wake. + */ +static const u64 t6000_zero_pa[] = { + 0x282000a00, 0x304000a00, 0x383000a00, 0x402000a00, + 0x28e0ad000, 0x28e0f8000, 0x28e0f9000, 0x28e0fa000, 0x28e0fb000, 0x28e0fc000, + 0x28e0fd000, 0x28e070000, 0x28e5d8000, + 0x28e5e0000, 0x28e5e4000, 0x28e0c8200, 0x28e0c8400, 0x28e0c8600, 0x28e0c8000, +}; + +/* + * The PMGR as macOS has it at the S2R timer, written raw right before it, without polling: + * address, value. Address 0 waits that many microseconds. + */ +static const u64 t6000_ps_late[][2] = { + { 0x28e580160, 0x1f0000ff }, { 0x28e580180, 0x1f0000ff }, { 0x0, 0x7d0 }, + { 0x2922800d0, 0x300 }, { 0x2922800e0, 0x300 }, { 0x2922800d8, 0x300 }, + { 0x292280088, 0x300 }, { 0x292280098, 0x300 }, { 0x292280090, 0x300 }, + { 0x2922800a0, 0x300 }, { 0x28e080330, 0x300 }, { 0x28e080340, 0x300 }, + { 0x28e080350, 0x300 }, { 0x28e080360, 0x300 }, { 0x28e080238, 0x300 }, + { 0x28e0802f8, 0x300 }, { 0x28e080300, 0x300 }, { 0x28e580250, 0x200 }, + { 0x28e580258, 0x200 }, { 0x28e580260, 0x200 }, { 0x28e580268, 0x200 }, + { 0x28e580270, 0x200 }, { 0x292280078, 0x200 }, { 0x28e080220, 0x1000030f }, + { 0x28e580160, 0x1000030f }, { 0x28e580178, 0x1000030f }, { 0x28e080320, 0x1000030f }, + { 0x28e080138, 0x1400024f }, { 0x28e080140, 0x1400024f }, { 0x28e0802e8, 0x1f0003ff }, + { 0x28e0802f0, 0x1f0003ff }, { 0x28e080148, 0xff }, { 0x28e080120, 0xff }, + { 0x28e080130, 0xff }, { 0x28e580188, 0xf0002ff }, { 0x28e580190, 0x2ff }, + { 0x28e5801e8, 0xff }, { 0x28e580218, 0x1f0000ff }, { 0x28e580220, 0x1f0000ff }, + { 0x28e580228, 0x1f0000ff }, { 0x28e580238, 0xff }, { 0x28e580248, 0xff }, + { 0x28e580278, 0x2ff }, { 0x28e5802d0, 0x3ff }, { 0x28e5803b8, 0x1f0003ff }, + { 0x28e038104, 0x80100000 }, { 0x28e038108, 0x80100000 }, { 0x28e03810c, 0x80100000 }, + { 0x28e038114, 0x8a100100 }, { 0x28e038120, 0x85100100 }, { 0x28e038124, 0x85100100 }, + { 0x28e038128, 0x87100100 }, { 0x28e03812c, 0x87100100 }, { 0x28e038130, 0x87100100 }, + { 0x28e038134, 0x87100100 }, { 0x28e038138, 0x87100100 }, { 0x28e03813c, 0x87100100 }, + { 0x28e038148, 0x80100100 }, { 0x28e03814c, 0x85100100 }, { 0x28e038160, 0x85100100 }, + { 0x28e038180, 0x87100000 }, { 0x28e038184, 0x87100000 }, { 0x28e038188, 0x87100000 }, + { 0x28e03818c, 0x87100100 }, { 0x292240000, 0x80100010 }, { 0x28e20c000, 0x333 }, + { 0x0, 0x7d0 }, { 0x28e580188, 0xf00000f }, { 0x39bc00044, 0x0 }, + { 0x39bc08114, 0x0 }, { 0x39b0040fc, 0x0 }, { 0x39b0080fc, 0x0 }, + { 0x28e080138, 0x0 }, { 0x28e080320, 0x0 }, { 0x28e038098, 0x0 }, + { 0x28e03809c, 0x0 }, { 0x28e0380a0, 0x0 }, { 0x28e0380a4, 0x0 }, + { 0x0, 0x64 }, +}; + +#define T6000_PS_LATE_N ARRAY_SIZE(t6000_ps_late) +#define T6000_PS_LATE_SIO_CPU_PS 0x28e580188ULL +#define T6000_PS_LATE_SIO_ASC 0x39bc00000ULL + +static struct t6000_sleep { + void __iomem *ps_east, *spmi_irq, *nco, *ptd; + void __iomem *cluster_ps, *afr_ps, *pmp_ps; + void __iomem *dcs, *pll, *clkgen, *apcie_8014, *apcie_82bc; + void __iomem *acc[T6000_ACCS]; + void __iomem *amcc[T6000_AMCCS], *dcs_blk[T6000_AMCCS][T6000_DCS_PER_AMCC]; + void __iomem *zero[ARRAY_SIZE(t6000_zero_pa)]; + void __iomem *ps_late_reg[T6000_PS_LATE_N]; + /* The PMP, whose stop lets the commit take the memory clock down */ + struct device_node *pmp_np; + /* The boot core, which takes its WFI last */ + u64 boot_mpidr; + + /* This attempt */ + bool committed, quiesce, pmp_idled; + u32 socq_done; + + /* The cluster words the quiesce wrote, as it found them */ + u32 cluster_written, cluster_saved[T6000_CLUSTERS]; + /* + * The AFR cluster word as it was before the GPU's lowering took it to 0 (written by + * sleep-t6000-display.inc): a quiesce restore of 0 leaves it busy and the next sleep + * stalls. Put back once: by the display's undo after a sleep given up, else by the + * quiesce restore. + */ + u32 afr_cl_pre; + bool afr_cl_valid; + + u64 acc_saved[T6000_ACCS][T6000_ACCR_N]; + u64 relock_saved[2]; + u32 mux_saved[T6000_CLKGEN_MUXES], mux_n; + u32 pll_saved, dcs_saved, afr_saved; + u32 zero_saved[ARRAY_SIZE(t6000_zero_pa)]; + u32 apcie_saved[2]; + u32 mcc_done; + + /* The SPMI vector enables as their irqchip had them */ + u32 spmi_saved[T6000_SPMI_WORDS]; + bool spmi_written; + + /* The NCO channels whose enable bit prepare_regs set */ + u8 nco_forced; + /* Each NCO channel's control, divider and increments as prepare_regs found them */ + u32 nco_saved[T6000_NCO_CHANS][4]; + + /* The PTD sleep word before .prepare set it to 4 */ + u32 ptd_sleep_saved; + bool ptd_noticed; + + /* The late image's words as Linux had them, for the wake */ + u32 ps_late_saved[T6000_PS_LATE_N]; + DECLARE_BITMAP(ps_late_is_ps, T6000_PS_LATE_N); + DECLARE_BITMAP(ps_late_has_saved, T6000_PS_LATE_N); + bool ps_late_applied; + + /* The display blocks are down for this sleep and come back with a given-up one */ + bool disp_undo_due; + + /* Every PMGR power word with a node, as before the sleep, walked back up at the wake */ + struct { + struct device_node *np; + void __iomem *reg; + const char *name; + u64 pa; + u32 saved; + } psall[T6000_PSALL_MAX]; + unsigned int psall_n, psall_pass, psall_stuck; + bool psall_armed; + struct { + resource_size_t start; + void __iomem *base; + } psall_base[T6000_PSALL_BASES]; + unsigned int psall_nbase; + + /* The memory controllers' power words at the first sleep's S2R timer */ + u32 go_base[T6000_PSALL_MAX]; + bool go_base_taken; +} t6000_sleep; + +#define T6000_SOCQ_COUNTERS BIT(0) +#define T6000_SOCQ_THROTTLERS BIT(1) +#define T6000_SOCQ_APSC BIT(2) +#define T6000_SOCQ_CLKGEN BIT(3) +#define T6000_SOCQ_PLL BIT(4) +#define T6000_SOCQ_DCS BIT(5) +#define T6000_SOCQ_AFR BIT(6) +#define T6000_SOCQ_ZEROS BIT(7) +#define T6000_SOCQ_APCIE BIT(8) +#define T6000_SOCQ_RELOCK BIT(9) +#define T6000_SOCQ_MCC BIT(10) +#define T6000_MCC_DCS_LP BIT(2) + +#include "sleep-t6000-display.inc" + +/* Mapped for good, non-posted, 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_ps(u64 pa) +{ + struct apple_sleep *s = apple_sleep; + + if (pa >= T6000_PS_BASE && pa < T6000_PS_BASE + T6000_PS_SIZE) + return s->ps + (pa - T6000_PS_BASE); + if (pa >= T6000_PS_EAST_BASE && pa < T6000_PS_EAST_BASE + T6000_PS_SIZE) + return t6000_sleep.ps_east + (pa - T6000_PS_EAST_BASE); + return NULL; +} + +/* E cores 0.0.n, P cores 0.1.n and 0.2.n: MPIDR's affinity 1 is the cluster */ +static bool t6000_sleep_cpu_valid(u64 mpidr) +{ + unsigned int cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1); + + return !(mpidr & ~MPIDR_HWID_BITMASK) && !MPIDR_AFFINITY_LEVEL(mpidr, 3) && + MPIDR_AFFINITY_LEVEL(mpidr, 2) == !!cluster && cluster < 3; +} + +/* Each core's power-down overrides as Linux has them, back after a refusal */ +struct t6000_cpu_control { + u64 cyc, acc; + bool saved; +}; + +static DEFINE_PER_CPU(struct t6000_cpu_control, t6000_cpu_control); + +static int t6000_sleep_cpu_prepare(u64 mpidr) +{ + struct t6000_cpu_control *c = this_cpu_ptr(&t6000_cpu_control); + + if (!is_kernel_in_hyp_mode()) + return -EOPNOTSUPP; + if (!c->saved) { + c->cyc = read_sysreg_s(T6000_CYC_OVRD); + c->acc = read_sysreg_s(T6000_ACC_OVRD); + c->saved = true; + } + return 0; +} + +static void t6000_sleep_cpu_restore(u64 mpidr) +{ + struct t6000_cpu_control *c = this_cpu_ptr(&t6000_cpu_control); + + if (!c->saved) + return; + write_sysreg_s(c->acc, T6000_ACC_OVRD); + write_sysreg_s(c->cyc, T6000_CYC_OVRD); + isb(); + c->saved = false; +} + +/* + * void t6000_cpu_off(u64 cyc_ovrd, u64 acc_ovrd): a core's last instructions, as XNU takes + * them. The data cache off and L1 written back and invalidated by set and way (the L2 and the + * memory cache are the hardware's), the timers off, ACC_OVRD then CYC_OVRD written, then WFI + * until the power goes. Uses no stack once the cache is off. Never returns. + */ +asm(" .pushsection .text, \"ax\"\n" +" .type t6000_cpu_off, %function\n" +"t6000_cpu_off:\n" +" msr daifset, #0xf\n" +" mrs x3, sctlr_el1\n" +" bic x3, x3, #(1 << 2)\n" +" msr sctlr_el1, x3\n" +" isb\n" +" msr csselr_el1, xzr\n" +" isb\n" +" mrs x3, ccsidr_el1\n" +" and x4, x3, #7\n" +" add x4, x4, #4\n" +" ubfx x5, x3, #3, #10\n" +" ubfx x6, x3, #13, #15\n" +" clz w7, w5\n" +"1: mov x8, x6\n" +"2: lsl x9, x5, x7\n" +" lsl x10, x8, x4\n" +" orr x9, x9, x10\n" +" dc cisw, x9\n" +" subs x8, x8, #1\n" +" b.ge 2b\n" +" subs x5, x5, #1\n" +" b.ge 1b\n" +" dsb sy\n" +" isb\n" +/* A pending timer, even a masked one, keeps the core from powering down */ +" msr cntp_ctl_el0, xzr\n" +" msr cntv_ctl_el0, xzr\n" +" mrs x3, CurrentEL\n" +" cmp x3, #(2 << 2)\n" +" b.ne 3f\n" +" msr s3_5_c14_c2_1, xzr\n" /* CNTP_CTL_EL02 */ +" msr s3_5_c14_c3_1, xzr\n" /* CNTV_CTL_EL02 */ +"3: msr s3_5_c15_c6_0, x1\n" /* ACC_OVRD */ +" msr s3_5_c15_c5_0, x0\n" /* CYC_OVRD */ +" isb\n" +"4: dsb sy\n" +" wfi\n" +" b 4b\n" +" .size t6000_cpu_off, . - t6000_cpu_off\n" +" .popsection\n" +); +void t6000_cpu_off(u64 cyc_ovrd, u64 acc_ovrd); + +/* macOS's enableCluster(false): the target to 0; a CPU cluster goes down with its cores */ +static void t6000_cluster_write(struct t6000_sleep *t, unsigned int i) +{ + void __iomem *reg = t->cluster_ps + 4 * i; + u32 val; + + t->cluster_saved[i] = readl(reg); + writel(t->cluster_saved[i] & ~T6000_CLUSTER_TARGET, reg); + t->cluster_written |= BIT(i); + if (!(T6000_CLUSTERS_CPU & BIT(i))) + readl_poll_timeout_atomic(reg, val, !(val & T6000_CLUSTER_BUSY), 1, 10000); +} + +/* The boot core's own cluster, if the quiesce left it */ +static void t6000_cluster_eacc(struct t6000_sleep *t) +{ + if (!t->quiesce || (t->cluster_written & BIT(T6000_CLUSTER_EACC))) + return; + t6000_cluster_write(t, T6000_CLUSTER_EACC); +} + +/* + * On every core before its last WFI: no retention, woken by a reset; IRQ and FIQ modes 0 so + * a wake interrupt still reaches the core. The boot core, the last one, first gives its own + * cluster's target a last chance, as macOS does after it releases the others. + */ +static void t6000_deep_wfi_arm(void) +{ + u64 cyc = read_sysreg_s(T6000_CYC_OVRD), acc = read_sysreg_s(T6000_ACC_OVRD); + + if ((read_cpuid_mpidr() & MPIDR_HWID_BITMASK) == t6000_sleep.boot_mpidr) + t6000_cluster_eacc(&t6000_sleep); + + 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; + 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_DISABLE_PIO_ON_WFI | T6000_ACC_DEEP_SLEEP; + t6000_cpu_off(cyc, acc); + unreachable(); +} + +/* The late image touches SIO's CPU, mailbox and DARTs only while SIO's CPU word is on. */ +static bool t6000_ps_late_sio_pa(u64 pa) +{ + return (pa >= T6000_PS_LATE_SIO_ASC && pa < T6000_PS_LATE_SIO_ASC + 0x100) || + (pa >= 0x39bc08100ULL && pa < 0x39bc08120ULL) || + (pa >= 0x39b004000ULL && pa < 0x39b004200ULL) || + (pa >= 0x39b008000ULL && pa < 0x39b008200ULL); +} + +/* PMGR registers only, and none that iBoot armed or that must never be touched */ +static bool t6000_restore_pa_ok(u64 pa) +{ + if (pa < 0x28e000000ULL || pa >= 0x293000000ULL) + return false; + if ((pa >= 0x28e200000ULL && pa < 0x28e208000ULL) || + (pa >= 0x28e941000ULL && pa < 0x28e942000ULL) || + (pa >= 0x28e59c000ULL && pa < 0x28e59d000ULL) || + (pa >= 0x28e099000ULL && pa < 0x28e099078ULL) || + (pa >= 0x292000200ULL && pa < 0x292000204ULL) || + (pa >= 0x2922b0000ULL && pa < 0x2922b0100ULL) || + (pa >= 0x2922c0000ULL && pa < 0x2922c0314ULL) || + (pa >= 0x2922dc21cULL && pa < 0x2922dc22cULL) || + (pa >= 0x2925c0000ULL && pa < 0x2925d4000ULL) || + (pa >= 0x292f00000ULL && pa < 0x292f70000ULL)) + return false; + return true; +} + +/* A power word some PMGR power-state node describes */ +static bool t6000_pa_is_ps(u64 pa) +{ + struct device_node *np, *parent; + struct resource res; + bool found = false; + u32 off; + + for_each_compatible_node(np, NULL, "apple,pmgr-pwrstate") { + if (of_property_read_u32(np, "reg", &off)) + continue; + parent = of_get_parent(np); + if (parent && !of_address_to_resource(parent, 0, &res) && res.start + off == pa) + found = true; + of_node_put(parent); + if (found) { + of_node_put(np); + break; + } + } + return found; +} + +static void __iomem *t6000_psall_map(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->psall_nbase; b++) + if (t->psall_base[b].start == res.start) + return t->psall_base[b].base; + if (t->psall_nbase == T6000_PSALL_BASES) + return NULL; + t->psall_base[b].start = res.start; + t->psall_base[b].base = t6000_ioremap(s, res.start, resource_size(&res)); + if (t->psall_base[b].base) + t->psall_nbase++; + return t->psall_base[b].base; +} + +/* Every PMGR power word with a node, but SEP's, AFC's and AFI's */ +static int t6000_psall_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; + u32 off; + + for_each_compatible_node(np, NULL, "apple,pmgr-pwrstate") { + if (t->psall_n == T6000_PSALL_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_psall_map(s, parent, &start) : NULL; + of_node_put(parent); + if (!base || !t6000_restore_pa_ok(start + off)) + continue; + of_node_get(np); + if (devm_add_action_or_reset(s->dev, apple_sleep_put_node, np)) { + of_node_put(np); + return -ENOMEM; + } + t->psall[t->psall_n].np = np; + t->psall[t->psall_n].reg = base + off; + t->psall[t->psall_n].name = label; + t->psall[t->psall_n].pa = start + off; + t->psall_n++; + } + return 0; +} + +/* The NCO block of the sleep node's apple,nco: mapped, not requested, as its driver has it. */ +static void __iomem *t6000_map_nco(struct apple_sleep *s) +{ + struct device_node *np = of_parse_phandle(s->dev->of_node, "apple,nco", 0); + struct resource res; + void __iomem *base; + int ret; + + if (!np) + return IOMEM_ERR_PTR(-ENODEV); + ret = of_address_to_resource(np, 0, &res); + of_node_put(np); + if (ret || resource_size(&res) < T6000_NCO_CHANS * T6000_NCO_STRIDE) + return IOMEM_ERR_PTR(-EINVAL); + base = ioremap_np(res.start, resource_size(&res)); + if (!base || devm_add_action_or_reset(s->dev, apple_sleep_iounmap, (void __force *)base)) + return IOMEM_ERR_PTR(-ENOMEM); + return base; +} + +static int t6000_sleep_map_regs(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int i, a; + int ret; + + s->ps = apple_sleep_map(s->dev, "ps"); + t->ps_east = apple_sleep_map(s->dev, "ps-east"); + s->cpu_start = apple_sleep_map(s->dev, "cpu-start"); + s->wake = apple_sleep_map(s->dev, "wake"); + s->wdt = apple_sleep_map(s->dev, "wdt"); + s->commit = apple_sleep_map(s->dev, "commit"); + t->spmi_irq = apple_sleep_map(s->dev, "spmi-irq"); + t->nco = t6000_map_nco(s); + t->ptd = apple_sleep_map(s->dev, "ptd"); + if (IS_ERR(s->ps) || IS_ERR(t->ps_east) || IS_ERR(s->cpu_start) || IS_ERR(s->wake) || + IS_ERR(s->wdt) || IS_ERR(s->commit) || IS_ERR(t->spmi_irq) || IS_ERR(t->nco) || + IS_ERR(t->ptd)) + return dev_err_probe(s->dev, -EINVAL, + "ps, ps-east, cpu-start, wake, wdt, commit, spmi-irq, ptd and apple,nco must all be mapped\n"); + t->cluster_ps = s->ps + T6000_CLUSTER_PS; + t->afr_ps = s->ps + T6000_PS_AFR; + t->pmp_ps = s->ps + T6000_PS_PMP; + s->cluster = t->cluster_ps; + + /* The blocks the quiesce writes once: none in the sleep node's reg */ + for (i = 0; i < T6000_ACCS; i++) { + t->acc[i] = t6000_ioremap(s, T6000_ACC_BASE + i * T6000_ACC_STRIDE, + T6000_ACC_SIZE); + if (!t->acc[i]) + return dev_err_probe(s->dev, -ENOMEM, "ACC blocks not mapped\n"); + } + for (a = 0; a < T6000_AMCCS; a++) { + t->amcc[a] = t6000_ioremap(s, T6000_AMCC_BASE + a * T6000_AMCC_STRIDE, + T6000_AMCC_MAP); + for (i = 0; i < T6000_DCS_PER_AMCC; i++) + t->dcs_blk[a][i] = t6000_ioremap(s, T6000_AMCC_BASE + + a * T6000_AMCC_STRIDE + + T6000_DCS_OFF(i), 0x1000); + } + for (i = 0; i < ARRAY_SIZE(t6000_zero_pa); i++) + t->zero[i] = t6000_ioremap(s, t6000_zero_pa[i], 4); + t->clkgen = t6000_ioremap(s, T6000_CLKGEN_MUX, T6000_CLKGEN_SIZE); + t->pll = t6000_ioremap(s, T6000_PLL0, 4); + t->dcs = t6000_ioremap(s, T6000_DCS_PS, 4); + t->apcie_8014 = t6000_ioremap(s, T6000_APCIE_Q_8014, 4); + t->apcie_82bc = t6000_ioremap(s, T6000_APCIE_Q_82BC, 4); + for (i = 0; i < T6000_PS_LATE_N; i++) { + u64 pa = t6000_ps_late[i][0]; + + if ((pa >= 0x28e000000ULL && pa < 0x293000000ULL) || t6000_ps_late_sio_pa(pa)) + t->ps_late_reg[i] = t6000_ioremap(s, pa & ~3ULL, 4); + if (t->ps_late_reg[i] && t6000_restore_pa_ok(pa) && t6000_pa_is_ps(pa)) + __set_bit(i, t->ps_late_is_ps); + } + + ret = t6000_psall_init(s); + if (ret) + return ret; + + t->pmp_np = of_find_compatible_node(NULL, NULL, "apple,t6000-pmp-v2"); + if (t->pmp_np && devm_add_action_or_reset(s->dev, apple_sleep_put_node, t->pmp_np)) + return -ENOMEM; + t->boot_mpidr = s->soc->boot_mpidr; + + ret = apple_sleep_read_wake_sources(s); + if (ret) + return ret; + + return t6000_display_map(s); +} + +/* Awake, at .begin: what Linux has in every PMGR register the late image writes */ +static void t6000_ps_late_save(struct t6000_sleep *t) +{ + unsigned int i; + + bitmap_zero(t->ps_late_has_saved, T6000_PS_LATE_N); + t->ps_late_applied = false; + for (i = 0; i < T6000_PS_LATE_N; i++) { + u64 pa = t6000_ps_late[i][0]; + + if (!pa || !t->ps_late_reg[i] || !t6000_restore_pa_ok(pa)) + continue; + t->ps_late_saved[i] = readl(t->ps_late_reg[i]); + __set_bit(i, t->ps_late_has_saved); + } +} + +static void t6000_psall_save(struct t6000_sleep *t) +{ + unsigned int i; + + t->psall_armed = false; + for (i = 0; i < t->psall_n; i++) + t->psall[i].saved = readl(t->psall[i].reg); +} + +static int t6000_sleep_begin(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + + t->committed = false; + t->quiesce = false; + t->pmp_idled = false; + t->socq_done = 0; + t->mcc_done = 0; + t->mux_n = 0; + t->cluster_written = 0; + t->disp_undo_due = false; + t->afr_cl_valid = false; + t6000_ps_late_save(t); + t6000_psall_save(t); + return t6000_display_own(s, true); +} + +/* Right after the SMC's slpw, as macOS does: the PMP told too, long before its own last word */ +static void t6000_sleep_smc_warned(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + + if (t->ptd_noticed) + return; + t->ptd_sleep_saved = readl(t->ptd + T6000_PTD_SLEEP); + writel(4, t->ptd + T6000_PTD_SLEEP); + t->ptd_noticed = true; +} + +/* At .prepare, every device suspended: the PMP told here if a debug link held slpw back */ +static void t6000_sleep_prepare(struct apple_sleep *s) +{ + t6000_sleep_smc_warned(s); +} + +/* An interrupt raised since they were masked would end the last WFI right away. */ +static int t6000_isr_check(struct apple_sleep *s) +{ + if (read_sysreg(isr_el1) & (PSR_I_BIT | PSR_F_BIT)) { + dev_err(s->dev, "refused: an interrupt is pending\n"); + return -EBUSY; + } + return 0; +} + +/* The SoC's first refusals in .enter: nothing has been written yet. */ +static int t6000_sleep_check(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + + t->psall_armed = t->psall_n; + return t6000_isr_check(s); +} + +/* + * The last refusals. SEP's sleep and its power gate are its driver's, at the commit: the gate + * entry (t6000_gate_coprocs) refuses a sleep without them. + */ +static int t6000_sleep_final_check(struct apple_sleep *s) +{ + return t6000_isr_check(s); +} + +/* + * From the display blocks' lowering on, a sleep that is given up puts them back, as one that + * fails halfway does; a real one wakes them. + */ +static int t6000_display_off(struct apple_sleep *s) +{ + t6000_sleep.disp_undo_due = true; + return t6000_sleep_display_off(s); +} + +/* The NCO channels' enable bits restored where prepare_regs set them */ +static void t6000_nco_restore(struct t6000_sleep *t) +{ + unsigned int i; + + for (i = 0; i < T6000_NCO_CHANS; i++) { + void __iomem *reg = t->nco + i * T6000_NCO_STRIDE; + + if (t->nco_forced & BIT(i)) + writel(readl(reg) & ~T6000_NCO_CTRL_EN, reg); + } + t->nco_forced = 0; +} + +/* + * A deep sleep resets the NCO channels' rate registers, and the clock driver, which keeps the + * rate it set, does not program them again: an audio channel would run many times too fast. + * Put back what prepare_regs read, as the T8122 profile does, each channel running until + * t6000_nco_restore() stops the ones prepare_regs started. + */ +static void t6000_nco_put_back(struct t6000_sleep *t) +{ + unsigned int i; + + for (i = 0; i < T6000_NCO_CHANS; i++) { + void __iomem *ch = t->nco + i * T6000_NCO_STRIDE; + u32 ctrl = t->nco_saved[i][0]; + + writel(ctrl & ~T6000_NCO_CTRL_EN, ch + T6000_NCO_REG_CTRL); + writel(t->nco_saved[i][1], ch + T6000_NCO_REG_DIV); + writel(t->nco_saved[i][2], ch + T6000_NCO_REG_INC1); + writel(t->nco_saved[i][3], ch + T6000_NCO_REG_INC2); + writel(BIT(31), ch + T6000_NCO_REG_ACCINIT); + writel(ctrl | T6000_NCO_CTRL_EN, ch + T6000_NCO_REG_CTRL); + } +} + +/* + * After the enclave's stop and the wake mask, before the other coprocessors stop: each NCO + * channel's rate kept for the wake, and, as a channel of the NCO that clocks an MCA cluster + * and was disabled keeps the PMGR from power-gating the SIO island, every channel's enable + * bit on until the wake. + */ +static void t6000_sleep_prepare_regs(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + unsigned int i; + u32 v; + + t->nco_forced = 0; + for (i = 0; i < T6000_NCO_CHANS; i++) { + void __iomem *ch = t->nco + i * T6000_NCO_STRIDE; + + v = readl(ch + T6000_NCO_REG_CTRL); + t->nco_saved[i][0] = v; + t->nco_saved[i][1] = readl(ch + T6000_NCO_REG_DIV); + t->nco_saved[i][2] = readl(ch + T6000_NCO_REG_INC1); + t->nco_saved[i][3] = readl(ch + T6000_NCO_REG_INC2); + if (!(v & T6000_NCO_CTRL_EN)) { + writel(v | T6000_NCO_CTRL_EN, ch + T6000_NCO_REG_CTRL); + t->nco_forced |= BIT(i); + } + } +} + +static void t6000_sleep_undo_regs(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + + t6000_nco_restore(t); +} + +/* The memory controllers first: each DCS's low-power bit as XNU sets it, each AMCC drained */ +static void t6000_quiesce_mcc(struct t6000_sleep *t) +{ + unsigned int a, i; + u32 val; + + for (a = 0; a < T6000_AMCCS; a++) + if (!t->amcc[a]) + return; + + for (a = 0; a < T6000_AMCCS; a++) + for (i = 0; i < T6000_DCS_PER_AMCC; i++) + if (t->dcs_blk[a][i]) + writel(T6000_DCS_CTL_MACOS, t->dcs_blk[a][i] + T6000_DCS_CTL); + t->mcc_done |= T6000_MCC_DCS_LP; + + 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_CNT, val, !val, 1, 20000); + t->socq_done |= T6000_SOCQ_MCC; +} + +/* The same bits off in one register of each CPU complex */ +static void t6000_acc_clear(struct t6000_sleep *t, int r, u64 bits) +{ + unsigned int i; + + for (i = 0; i < T6000_ACCS; i++) { + void __iomem *reg = t->acc[i] + t6000_acc_regs[r]; + + t->acc_saved[i][r] = readq(reg); + writeq(t->acc_saved[i][r] & ~bits, reg); + } +} + +/* quiesceACC: counters, throttlers, the PLL relock of ACC0 and ACC1, then the APSC value */ +static void t6000_quiesce_acc(struct t6000_sleep *t) +{ + unsigned int i, n; + u64 val; + + t6000_acc_clear(t, T6000_ACCR_PERF, T6000_ACC_PERF_EN); + t6000_acc_clear(t, T6000_ACCR_ENERGY, T6000_ACC_ENERGY_EN); + t->socq_done |= T6000_SOCQ_COUNTERS; + + t6000_acc_clear(t, T6000_ACCR_AMX, T6000_ACC_THRTL_EN); + t6000_acc_clear(t, T6000_ACCR_LLC, T6000_ACC_THRTL_EN); + t6000_acc_clear(t, T6000_ACCR_PPT, T6000_ACC_THRTL_EN); + t->socq_done |= T6000_SOCQ_THROTTLERS; + + for (i = 0; i < 2; i++) { + void __iomem *reg = t->acc[i] + T6000_ACC_DVFS; + + t->relock_saved[i] = readq(reg); + writeq(T6000_ACC_DVFS_RELOCK, reg); + readq_poll_timeout_atomic(reg, val, !(val & T6000_ACC_DVFS_BUSY), 1, 8000); + } + t->socq_done |= T6000_SOCQ_RELOCK; + + /* Twice, as macOS writes it */ + for (i = 0; i < T6000_ACCS; i++) { + void __iomem *reg = t->acc[i] + T6000_ACC_DVFS; + + t->acc_saved[i][T6000_ACCR_DVFS] = readq(reg); + for (n = 0; n < 2; n++) { + writeq(T6000_ACC_DVFS_APSC_MACOS, reg); + readq_poll_timeout_atomic(reg, val, !(val & T6000_ACC_DVFS_BUSY), 1, 8000); + } + } + t->socq_done |= T6000_SOCQ_APSC; +} + +static bool t6000_dcs_set(struct t6000_sleep *t, u32 ps) +{ + u32 val = readl(t->dcs); + + writel((val & ~T6000_DCS_PS_TARGET) | FIELD_PREP(T6000_DCS_PS_TARGET, ps), t->dcs); + return !readl_poll_timeout_atomic(t->dcs, val, !(val & T6000_DCS_PS_BUSY), 1, 8000); +} + +/* A stopped PMP: it owns the memory clock while it runs. */ +static bool t6000_pmp_stopped(struct t6000_sleep *t) +{ + int i; + + for (i = 0; t->pmp_np && i < SLEEP_COPROCS; i++) + if (apple_sleep_coprocs[i].dev && apple_sleep_coprocs[i].dev->of_node == t->pmp_np) + return apple_sleep_coprocs[i].stopped; + return false; +} + +/* quiescePerfState, then the start of the T6000 tail: memory clock 7 then 5, AFR's word 0 */ +static void t6000_quiesce_dcs_afr(struct t6000_sleep *t) +{ + u32 val; + + t->pmp_idled = t6000_pmp_stopped(t); + if (t->pmp_idled && !FIELD_GET(PS_ACTUAL, readl(t->pmp_ps))) { + t->dcs_saved = readl(t->dcs); + t->socq_done |= T6000_SOCQ_DCS; + t6000_dcs_set(t, 7); + t6000_dcs_set(t, 5); + } + + t->afr_saved = readl(t->afr_ps); + t->socq_done |= T6000_SOCQ_AFR; + writel(0, t->afr_ps); + readl_poll_timeout_atomic(t->afr_ps, val, !(val & PS_ACTUAL), 1, 8000); +} + +/* The blocks in a cluster, which have to be off before it goes down */ +static bool t6000_cluster_gate(unsigned int cluster) +{ + static const u64 ane0[] = { 0x28e080268, 0x28e0802c8, 0 }; + static const u64 afr[] = { 0x28e0801e8, 0x28e5803c0, 0x28e080268, 0x28e080270, + 0x28e580390, 0x28e580398, 0x28e5803b0, 0 }; + /* disp0, dispext0 and dispext1 */ + static const u64 display[] = { 0x28e580350, 0x28e080280, 0x28e0802a8, 0 }; + static const u64 avemsr[] = { 0x28e080270, 0x28e580398, 0x28e5803b0, 0 }; + const u64 *ps; + + switch (cluster) { + case T6000_CLUSTER_ANE0: + ps = ane0; + break; + case T6000_CLUSTER_AFR: + ps = afr; + break; + case T6000_CLUSTER_DISPLAY: + ps = display; + break; + case T6000_CLUSTER_AVEMSR: + ps = avemsr; + break; + default: + return true; + } + + for (; *ps; ps++) { + void __iomem *reg = t6000_ps(*ps); + + if (!reg || FIELD_GET(PS_ACTUAL, readl(reg))) + return false; + } + return true; +} + +/* The SoC clocks onto the reference clock, then PLL 0 off, twice as macOS does */ +static void t6000_quiesce_clocks(struct t6000_sleep *t) +{ + unsigned int i, n; + u32 val; + + for (i = 0; i < T6000_CLKGEN_MUXES; i++) { + void __iomem *reg = t->clkgen + t6000_clkgen_mux_off[i]; + + t->mux_saved[i] = readl(reg); + writel((t->mux_saved[i] & ~T6000_CLKGEN_SEL) | T6000_CLKGEN_ON, reg); + t->mux_n = i + 1; + if (readl_poll_timeout_atomic(reg, val, !(val & T6000_CLKGEN_BUSY), 1, 8000)) + return; + } + t->socq_done |= T6000_SOCQ_CLKGEN; + + t->pll_saved = readl(t->pll); + t->socq_done |= T6000_SOCQ_PLL; + for (n = 0; n < 2; n++) { + writel((readl(t->pll) & ~T6000_PLL_OFF_MODE) | FIELD_PREP(T6000_PLL_OFF_MODE, 3), + t->pll); + writel(readl(t->pll) | T6000_PLL_OFF_OK, t->pll); + writel(readl(t->pll) & ~T6000_PLL_ON, t->pll); + if (readl_poll_timeout_atomic(t->pll, val, !(val & T6000_PLL_BUSY), 1, 8000)) + break; + } +} + +/* The PMGR's sleep entry, on the boot core alone while the parked cores still wait */ +static void t6000_quiesce(struct t6000_sleep *t) +{ + unsigned int i; + + t->quiesce = true; + t6000_quiesce_mcc(t); + + for (i = 0; i < ARRAY_SIZE(t6000_zero_pa); i++) { + if (!t->zero[i]) + continue; + t->zero_saved[i] = readl(t->zero[i]); + writel(0, t->zero[i]); + } + t->socq_done |= T6000_SOCQ_ZEROS; + /* + * No power-word reads here: reading the PCIe block's three words this late left the SoC + * stuck before the sleep. + */ + if (t->apcie_8014 && t->apcie_82bc) { + t->apcie_saved[0] = readl(t->apcie_8014); + t->apcie_saved[1] = readl(t->apcie_82bc); + writel(1, t->apcie_8014); + writel(1, t->apcie_82bc); + writel(2, t->apcie_82bc); + writel(4, t->apcie_82bc); + t->socq_done |= T6000_SOCQ_APCIE; + } + t6000_quiesce_acc(t); + t6000_quiesce_dcs_afr(t); + + for (i = 0; i < T6000_CLUSTERS; i++) { + if (!(T6000_CLUSTERS_VALID & BIT(i)) || !t6000_cluster_gate(i)) + continue; + t6000_cluster_write(t, i); + } + + t6000_quiesce_clocks(t); +} + +/* + * The wake engines wait for each DCS power word to drop to 0 on the way back, and one that + * goes to sleep in another state holds them until the watchdog. The first sleep keeps the + * words at the S2R timer as the baseline; later ones put a changed one back. + */ +static u32 t6000_go_mask(u32 a, u32 b) +{ + u32 m = PS_TARGET | PS_ACTUAL | BIT(10) | GENMASK(19, 16) | PS_AUTO_ENABLE; + + if (a & b & PS_AUTO_ENABLE) + m &= ~PS_ACTUAL; + return m; +} + +/* + * 100 us after each of the first eight words put back in a sleep, as the sequence that runs on + * the hardware has it, and none after the rest. + */ +#define T6000_GO_SETTLE_N 8 + +static void t6000_go_check(struct t6000_sleep *t) +{ + unsigned int i, put_back = 0; + u32 v, base, d; + + for (i = 0; i < t->psall_n; i++) { + void __iomem *reg = t->psall[i].reg; + + if (strncmp(t->psall[i].name, "dcs_", 4)) + continue; + v = readl(reg); + if (!t->go_base_taken) { + /* A floor there on the first sleep must not end up in the baseline. */ + if (v & GENMASK(19, 16)) { + writel(v & ~(GENMASK(19, 16) | BIT(31) | BIT(9) | BIT(8)), reg); + udelay(100); + v = readl(reg); + } + t->go_base[i] = v; + continue; + } + base = t->go_base[i]; + d = (v ^ base) & t6000_go_mask(v, base); + if (!d || (base & GENMASK(19, 16))) + continue; + writel((v & ~(PS_TARGET | PS_AUTO_ENABLE | GENMASK(19, 16) | BIT(31) | BIT(9) | + BIT(8))) | + (base & (PS_TARGET | PS_AUTO_ENABLE | GENMASK(19, 16))), reg); + if (put_back++ < T6000_GO_SETTLE_N) + udelay(100); + } + if (t->psall_n) + t->go_base_taken = true; +} + +/* The late image, every entry, unconditionally, with its waits; then 200 ms */ +static void t6000_ps_late_write(struct t6000_sleep *t) +{ + void __iomem *sio_ps = NULL; + unsigned int i, j; + + for (j = 0; j < T6000_PS_LATE_N; j++) + if (t6000_ps_late[j][0] == T6000_PS_LATE_SIO_CPU_PS && t->ps_late_reg[j]) + sio_ps = t->ps_late_reg[j]; + + t->ps_late_applied = !bitmap_empty(t->ps_late_has_saved, T6000_PS_LATE_N); + for (i = 0; i < T6000_PS_LATE_N; i++) { + if (!t6000_ps_late[i][0]) { + udelay(min_t(u64, t6000_ps_late[i][1], 20000)); + continue; + } + if (!t->ps_late_reg[i]) + continue; + if (t6000_ps_late_sio_pa(t6000_ps_late[i][0])) { + unsigned int us; + + /* SIO's word needs a moment after its target is raised */ + for (us = 0; sio_ps && us < 2000 && + FIELD_GET(PS_ACTUAL, readl(sio_ps)) != 0xf; us++) + udelay(1); + if (!sio_ps || FIELD_GET(PS_ACTUAL, readl(sio_ps)) != 0xf) + continue; + } + writel((u32)t6000_ps_late[i][1], t->ps_late_reg[i]); + } + dsb(sy); + mdelay(T6000_PS_LATE_MS); +} + +/* The PMU RTC alarm's vector on, unless its driver has it on already */ +static void t6000_spmi_arm(struct t6000_sleep *t) +{ + unsigned int i; + + for (i = 0; i < T6000_SPMI_WORDS; i++) + t->spmi_saved[i] = readl(t->spmi_irq + 4 * i); + t->spmi_written = true; + if (t->spmi_saved[0] & T6000_SPMI_VEC_RTC) + return; + writel(T6000_SPMI_VEC_RTC, t->spmi_irq + T6000_SPMI_CLEAR); + writel(t->spmi_saved[0] | T6000_SPMI_VEC_RTC, t->spmi_irq); + dsb(sy); +} + +/* + * The enables back as the irqchip had them. A latched alarm stays for the irqchip if it has + * the vector on, and is cleared if only the sleep turned it on. + */ +static void t6000_spmi_restore(struct t6000_sleep *t) +{ + unsigned int i; + + if (!t->spmi_written) + return; + t->spmi_written = false; + for (i = 0; i < T6000_SPMI_WORDS; i++) + writel(t->spmi_saved[i], t->spmi_irq + 4 * i); + if (!(t->spmi_saved[0] & T6000_SPMI_VEC_RTC)) + writel(T6000_SPMI_VEC_RTC, t->spmi_irq + T6000_SPMI_CLEAR); + dsb(sy); +} + +/* + * The commit, from the stopped coprocessors to the mailbox, in macOS's order: quiesceHW, the + * memory controllers' words checked, the parked cores stopped and the boot cluster's target, + * WD1, the late image, the RTC alarm's vector and WD1 again. The shared commit then arms the + * mailbox and starts the S2R timer. Nothing printed, nothing can fail. + */ +static void t6000_sleep_commit_regs(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + + t->committed = true; + t6000_quiesce(t); + t6000_go_check(t); + + /* PE_cpu_machine_quiesce: the PMGR has to know each core is down for the sleep */ + s->cpus_stop_ret = spin_table_stop_parked(); + t6000_cluster_eacc(t); + dsb(sy); + + /* A fabric hang in the raw writes below still ends in a reset */ + apple_sleep_set_wd2(s, T6000_WDT_GO_SECS); + dsb(sy); + t6000_ps_late_write(t); + t6000_spmi_arm(t); + /* From here to the wake: it keeps counting through the sleep */ + apple_sleep_set_wd2(s, T6000_WDT_GO_SECS); + dsb(sy); +} + +/* The PMGR's wake: AFR's word first, then the memory clock 7 and back to what the PMP had */ +static void t6000_dcs_afr_restore(struct t6000_sleep *t) +{ + u32 val; + + if (t->socq_done & T6000_SOCQ_AFR) { + writel(t->afr_saved, t->afr_ps); + readl_poll_timeout_atomic(t->afr_ps, val, + FIELD_GET(PS_ACTUAL, val) == FIELD_GET(PS_TARGET, val), + 1, 8000); + } + if (t->socq_done & T6000_SOCQ_DCS) { + t6000_dcs_set(t, 7); + t6000_dcs_set(t, FIELD_GET(T6000_DCS_PS_TARGET, t->dcs_saved)); + } +} + +/* PLL 0 back on and locked before any mux goes back to it */ +static void t6000_clocks_restore(struct t6000_sleep *t) +{ + u32 val = 0; + int i; + + if (t->socq_done & T6000_SOCQ_PLL) { + writel(readl(t->pll) | T6000_PLL_ON, t->pll); + readl_poll_timeout_atomic(t->pll, val, !(val & T6000_PLL_BUSY), 1, 8000); + writel(t->pll_saved, t->pll); + readl_poll_timeout_atomic(t->pll, val, !(val & T6000_PLL_BUSY), 1, 8000); + /* Better slow on the reference clock than on a PLL that never locked */ + if (val & T6000_PLL_BUSY) + return; + } + + for (i = t->mux_n - 1; i >= 0; i--) { + void __iomem *reg = t->clkgen + t6000_clkgen_mux_off[i]; + + writel(t->mux_saved[i], reg); + readl_poll_timeout_atomic(reg, val, !(val & T6000_CLKGEN_BUSY), 1, 8000); + } +} + +/* Once the CPU complexes have power again */ +static void t6000_acc_restore(struct t6000_sleep *t) +{ + u32 regs = 0; + unsigned int i; + int r; + u64 val; + + if (t->socq_done & T6000_SOCQ_COUNTERS) + regs |= BIT(T6000_ACCR_PERF) | BIT(T6000_ACCR_ENERGY); + if (t->socq_done & T6000_SOCQ_THROTTLERS) + regs |= BIT(T6000_ACCR_AMX) | BIT(T6000_ACCR_LLC) | BIT(T6000_ACCR_PPT); + if (t->socq_done & T6000_SOCQ_APSC) + regs |= BIT(T6000_ACCR_DVFS); + + for (i = 0; i < T6000_ACCS; i++) { + for (r = T6000_ACCR_N - 1; r >= 0; r--) { + void __iomem *reg = t->acc[i] + t6000_acc_regs[r]; + + if (!(regs & BIT(r))) + continue; + /* Only the APSC bit: cpufreq owns the rest of the DVFS word */ + if (r == T6000_ACCR_DVFS) { + val = readq(reg) & ~T6000_ACC_DVFS_APSC_OFF; + writeq(val | (t->acc_saved[i][r] & T6000_ACC_DVFS_APSC_OFF), reg); + readq_poll_timeout_atomic(reg, val, !(val & T6000_ACC_DVFS_BUSY), + 1, 8000); + } else { + writeq(t->acc_saved[i][r], reg); + } + } + } +} + +/* The PCIe block's three words: a write to it with any of them off never completes */ +static bool t6000_apcie_on(void) +{ + static const u64 apcie_ps[] = { 0x28e080138ULL, 0x28e080150ULL, 0x28e080320ULL }; + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(apcie_ps); i++) + if (FIELD_GET(PS_ACTUAL, readl(t6000_ps(apcie_ps[i]))) != 0xf) + return false; + return true; +} + +/* The PMGR's wake, before any parked core comes back */ +static void t6000_quiesce_restore(struct t6000_sleep *t) +{ + unsigned int done = t->socq_done, a, i; + u32 val, now; + + if (t->mcc_done & T6000_MCC_DCS_LP) + for (a = 0; a < T6000_AMCCS; a++) + for (i = 0; i < T6000_DCS_PER_AMCC; i++) + if (t->dcs_blk[a][i]) + writel(T6000_DCS_CTL_IBOOT, + t->dcs_blk[a][i] + T6000_DCS_CTL); + t6000_clocks_restore(t); + + for (i = 0; i < T6000_CLUSTERS; i++) { + void __iomem *reg = t->cluster_ps + 4 * i; + + if (!(t->cluster_written & BIT(i))) + continue; + now = readl(reg); + if (i == T6000_CLUSTER_AFR && t->afr_cl_valid && + !(t->cluster_saved[i] & T6000_CLUSTER_TARGET)) { + /* The GPU's lowering took it to 0 first: back to what it was before that */ + if ((now ^ t->afr_cl_pre) & T6000_CLUSTER_TARGET) + writel(t->afr_cl_pre, reg); + } else { + writel(t->cluster_saved[i], reg); + } + readl_poll_timeout_atomic(reg, val, !(val & T6000_CLUSTER_BUSY), 1, 10000); + } + + t6000_dcs_afr_restore(t); + t6000_acc_restore(t); + + for (i = 2; (done & T6000_SOCQ_RELOCK) && i-- > 0;) { + void __iomem *reg = t->acc[i] + T6000_ACC_DVFS; + u64 v; + + writeq(t->relock_saved[i], reg); + readq_poll_timeout_atomic(reg, v, !(v & T6000_ACC_DVFS_BUSY), 1, 8000); + } + /* After a real sleep the block comes back powered off and reset: nothing to restore */ + if ((done & T6000_SOCQ_APCIE) && t6000_apcie_on()) { + writel(t->apcie_saved[1], t->apcie_82bc); + writel(t->apcie_saved[0], t->apcie_8014); + } + + /* The voltage monitors first, then the counters, then the error handlers */ + for (i = ARRAY_SIZE(t6000_zero_pa); (done & T6000_SOCQ_ZEROS) && i-- > 0;) + if (t->zero[i]) + writel(t->zero_saved[i], t->zero[i]); + t->socq_done = 0; + t->mcc_done = 0; + t->mux_n = 0; + t->cluster_written = 0; + t->afr_cl_valid = false; +} + +/* Back on the boot core, the AIC restored, before any other core starts */ +static void t6000_sleep_wake(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + + /* + * Only when the commit armed it: counted again from 0 with the wake's time, so a slow + * device resume has it and a hung one still ends in a reset. The control word stays as + * it is; .end puts user space's back. + */ + if (readl(s->wdt + WDT_WD1_CTL) & WDT_WD1_RESET_EN) { + writel(0, s->wdt + WDT_WD1_COUNT); + writel(T6000_WDT_WAKE_SECS * WDT_TICKS_PER_S, s->wdt + WDT_WD1_BITE); + } + + /* A real sleep woke the display blocks: nothing to undo at .wake */ + t->disp_undo_due = false; + t6000_quiesce_restore(t); + t6000_spmi_restore(t); + t6000_nco_put_back(t); + t6000_nco_restore(t); +} + +/* + * After a sleep, before any device resumes: the late image's registers back as Linux had + * them. Walked backwards, so parents come back before children, each address once. + */ +static void t6000_ps_late_put_back(struct apple_sleep *s, struct t6000_sleep *t) +{ + DECLARE_BITMAP(done, T6000_PS_LATE_N); + unsigned int i, j, us; + + if (!t->ps_late_applied) + return; + t->ps_late_applied = false; + bitmap_zero(done, T6000_PS_LATE_N); + for (i = T6000_PS_LATE_N; i-- > 0;) { + u64 pa = t6000_ps_late[i][0]; + bool again = false; + u32 v; + + if (!test_bit(i, t->ps_late_has_saved)) + continue; + for (j = i + 1; j < T6000_PS_LATE_N; j++) + if (t6000_ps_late[j][0] == pa && test_bit(j, done)) + again = true; + if (again) + continue; + __set_bit(i, done); + v = t->ps_late_saved[i]; + /* One Linux had on comes back through the walk below, parents first. */ + if (test_bit(i, t->ps_late_is_ps) && FIELD_GET(PS_ACTUAL, v)) + continue; + if (!test_bit(i, t->ps_late_is_ps)) { + writel(v, t->ps_late_reg[i]); + continue; + } + /* The was-gated bits are write-one-to-clear, the actual state read only */ + writel(v & ~(BIT(9) | BIT(8) | PS_ACTUAL), t->ps_late_reg[i]); + for (us = 0; us < 10000; us++) { + if (FIELD_GET(PS_ACTUAL, readl(t->ps_late_reg[i])) == + FIELD_GET(PS_TARGET, v)) + break; + udelay(10); + } + if (us == 10000) + dev_warn(s->dev, "T6000: power word %#llx not back at %lu: %#x\n", pa, + FIELD_GET(PS_TARGET, v), readl(t->ps_late_reg[i])); + } +} + +static int t6000_psall_find(struct t6000_sleep *t, struct device_node *np) +{ + unsigned int i; + + for (i = 0; i < t->psall_n; i++) + if (t->psall[i].np == np) + return i; + return -1; +} + +/* One saved power word back on, its parents first */ +static void t6000_psall_on(struct apple_sleep *s, struct t6000_sleep *t, unsigned int i, + unsigned int depth, unsigned long *seen) +{ + struct of_phandle_iterator it; + void __iomem *reg = t->psall[i].reg; + u32 saved = t->psall[i].saved, cur; + unsigned int us; + int err, j; + + if (test_and_set_bit(i, seen) || depth > 8) + return; + /* + * The NUB PMGR keeps its state through the sleep. Its domains are auto-PM: a saved + * actual state only says a transfer was running, and raising one would cost it + * AUTO_ENABLE for the rest of the boot. + */ + if (t->psall[i].pa >= T6000_NUB_PS) + return; + /* Linux had it off: genpd's to bring up */ + if (!FIELD_GET(PS_ACTUAL, saved)) + return; + of_for_each_phandle(&it, err, t->psall[i].np, "power-domains", "#power-domain-cells", 0) { + j = t6000_psall_find(t, it.node); + if (j >= 0) + t6000_psall_on(s, t, j, depth + 1, seen); + } + cur = readl(reg); + /* The minimum state (the DCP CPUs') is lost with the power: back first */ + if ((cur ^ saved) & GENMASK(19, 16)) { + writel((cur & ~(BIT(12) | BIT(11) | BIT(10) | BIT(9) | BIT(8) | GENMASK(19, 16))) | + (saved & GENMASK(19, 16)), reg); + cur = readl(reg); + } + if (FIELD_GET(PS_TARGET, cur) == FIELD_GET(PS_TARGET, saved) && + FIELD_GET(PS_ACTUAL, cur) >= FIELD_GET(PS_ACTUAL, saved)) + return; + cur &= ~(BIT(12) | BIT(11) | BIT(10) | BIT(9) | BIT(8) | BIT(28) | PS_TARGET | + GENMASK(27, 24)); + cur |= saved & GENMASK(27, 24); + if (t->psall_pass && !FIELD_GET(PS_ACTUAL, readl(reg))) { + writel(cur & ~PS_ACTUAL, reg); + udelay(100); + } + writel(cur | (saved & PS_TARGET), reg); + for (us = 0; us < 2000 && FIELD_GET(PS_ACTUAL, readl(reg)) < FIELD_GET(PS_ACTUAL, saved); + us++) + udelay(10); + cur = readl(reg) & ~(BIT(12) | BIT(11) | BIT(10) | BIT(9) | BIT(8) | PS_ACTUAL); + /* + * After the first pass, a word still off comes up without AUTO_ENABLE: with it the PMGR + * keeps an idle fabric domain off even when a DART behind it is accessed. + */ + if ((saved & BIT(28)) && !t->psall_pass) + writel(cur | BIT(28), reg); + cur = readl(reg); + if ((cur & BIT(31)) && !(saved & BIT(31)) && FIELD_GET(PS_ACTUAL, cur)) { + writel(cur & ~(BIT(31) | BIT(11) | BIT(9) | BIT(8) | PS_ACTUAL), reg); + udelay(1); + cur = readl(reg); + } + if (FIELD_GET(PS_ACTUAL, cur) < FIELD_GET(PS_ACTUAL, saved)) { + t->psall_stuck++; + if (t->psall_pass == T6000_PSALL_PASSES - 1) + dev_warn(s->dev, "T6000: %s stays off (%#x, was %#x)\n", t->psall[i].name, + cur, saved); + } +} + +/* + * Every PMGR domain that was on before the sleep back on, parents first, before any device + * resumes (never SEP, AFC or AFI): up to 30 passes 10 ms apart while some are still off, as a + * fabric parent can take longer than one poll. + */ +static void t6000_psall_restore(struct apple_sleep *s, struct t6000_sleep *t) +{ + DECLARE_BITMAP(seen, T6000_PSALL_MAX); + unsigned int i, pass; + + if (!t->psall_armed) + return; + t->psall_armed = false; + for (pass = 0; pass < T6000_PSALL_PASSES; pass++) { + if (pass) + mdelay(10); + t->psall_pass = pass; + t->psall_stuck = 0; + bitmap_zero(seen, T6000_PSALL_MAX); + for (i = 0; i < t->psall_n; i++) + t6000_psall_on(s, t, i, 0, seen); + if (!t->psall_stuck) + break; + } +} + +/* + * At .wake (and again at .end, when each step has already run): the late image put back, the + * display blocks back after a sleep that was given up, then every domain that was on. + */ +static void t6000_sleep_finish(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + + t6000_ps_late_put_back(s, t); + if (t->disp_undo_due) { + t->disp_undo_due = false; + t6000_sleep_display_undo(s); + } + t6000_psall_restore(s, t); + t6000_display_own(s, false); +} + +/* At .end: the PTD sleep word back */ +static void t6000_sleep_end(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + + if (!t->ptd_noticed) + return; + writel(t->ptd_sleep_saved, t->ptd + T6000_PTD_SLEEP); + t->ptd_noticed = false; +} + +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 enclave's driver must be bound, and have told it at its suspend and seen it gate */ +static const struct apple_sleep_gate_coproc t6000_gate_coprocs[] = { + { "/soc/sep@396400000" }, +}; + +static const struct apple_sleep_ops t6000_sleep_ops = { + .deep_wfi_arm = t6000_deep_wfi_arm, + .resume = cpu_resume, + /* Wake-armed lines stay open for the last WFI, as the AIC's syscore suspend left them */ + .aic_mask_all = aic_sleep_mask_all_but_wake, + .aic_ack = aic_sleep_ack, + .aic_restore = aic_sleep_restore, + .map_regs = t6000_sleep_map_regs, + .begin = t6000_sleep_begin, + .prepare = t6000_sleep_prepare, + .smc_warned = t6000_sleep_smc_warned, + .check = t6000_sleep_check, + .final_check = t6000_sleep_final_check, + .display_off = t6000_display_off, + .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, + .end = t6000_sleep_end, + .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, + /* + * Deep sleep offered only with apple_sleep.deep=1, a commit only with + * untested_commit=1, until J314s rows on the default command line exist + */ + .untested = true, + .no_pmu_notes = true, + .early_context = true, + .commit_stops_cpus = true, + /* Its driver parks the DCP only for a sleep that commits: a dry run leaves it running. */ + .display_no_restart = true, + .wdt_wd1 = true, + .smc_gen1_keys = true, + .smc_keys_refuse = true, + .boot_mpidr = 0x0, + .cpu_count = 8, + /* + * MPIDR affinity 1 is the cluster: E 0.0.n, P0 0.1.n, P1 0.2.n. Start bits 4 * cluster + + * core; a binned cluster keeps its cores' numbers, so 4 bounds the core, not the count. + */ + .cpu_cluster_affinity = 1, + .cpu_clusters = 3, + .cpu_cores_per_cluster = 4, + .cpu_start_stride = 4, + .cpu_valid = t6000_sleep_cpu_valid, + .cluster_cpu = { T6000_CLUSTER_EACC * 4, T6000_CLUSTER_PACC0 * 4, + T6000_CLUSTER_PACC1 * 4 }, + .ncluster_cpu = 3, + .ndisp = 1, + .gate_coprocs = t6000_gate_coprocs, + .ngate_coprocs = ARRAY_SIZE(t6000_gate_coprocs), + /* The PMGR's AP wake block: 7 mask words, then 7 status words */ + .wake_mask = 0x08, + .wake_status = 0x24, + .wake_words = 7, + .wake_last_word = 0x00001fff, + .wake_names = t6000_wake_names, + .nwake_names = ARRAY_SIZE(t6000_wake_names), +}; diff --git a/drivers/soc/apple/sleep-test.c b/drivers/soc/apple/sleep-test.c index b86da75784b20..dddf0de50f13b 100644 --- a/drivers/soc/apple/sleep-test.c +++ b/drivers/soc/apple/sleep-test.c @@ -18,6 +18,7 @@ static const char *const apple_sleep_test_profiles[] = { "apple,t8122-sleep", "apple,t6030-sleep", "apple,t6020-sleep", + "apple,t6000-sleep", }; /* diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 4a6ee95cb7237..3a2030a3ba458 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -8503,6 +8503,9 @@ static void apple_sleep_map_pins(struct apple_sleep *s) if (!of_device_is_available(np) || of_property_read_u32(np, "apple,npins", &npins) || !npins || npins > 512) goto out; + /* T6000's pin controller driver keeps its pins across the sleep itself. */ + if (of_device_is_compatible(np, "apple,t6000-pinctrl")) + goto out; /* Without the pins to leave alone none is saved: reading some of them faults. */ if (s->soc->regions && !of_property_present(s->dev->of_node, "apple,pinctrl-skip-pins")) goto out; @@ -10900,6 +10903,7 @@ static const struct apple_sleep_soc t8142_sleep_soc = { #include "sleep-t8112.h" #include "sleep-t6030.h" #include "sleep-t6020.h" +#include "sleep-t6000.h" /* * Qualified profiles expose system sleep. Inventory-only profiles match for a @@ -10918,6 +10922,7 @@ static const struct of_device_id apple_sleep_of_match[] = { { .compatible = "apple,t8122-sleep", .data = &t8122_sleep_soc }, { .compatible = "apple,t6030-sleep", .data = &t6030_sleep_soc }, { .compatible = "apple,t6020-sleep", .data = &t6020_sleep_soc }, + { .compatible = "apple,t6000-sleep", .data = &t6000_sleep_soc }, { } }; From a69dc06f585d37cfb53d44e1dfe4b3d08ae92aa5 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 02:01:13 +0100 Subject: [PATCH 46/72] soc: apple: sleep: T6000: route a core's IRQs away before it goes offline AICv2 can't steer an IRQ: it delivers it to any core whose SIQ_CFG route is not none. One delivered to a core that has already masked interrupts on its way offline stays pending there, its device never sees it handled, and at a deep sleep's commit the core keeps aborting its power down. As XNU does, save SIQ_CFG and set its route to none in a CPU hotplug down_prep callback, on the core going offline while it can still take IRQs, and put it back when the core comes online again, also after an offline that was rolled back. The AIC's own online callback reads the route before this one runs, so the spin table's restore hook puts it back first, before any hotplug callback; a core that a refused sleep sends back to the park keeps the route at none until it comes online. A saved SIQ_CFG of 0 is a route too, so whether one was saved is kept apart from its value. Only at EL2, and only when the T6000 sleep node is enabled. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/sleep-t6000.h | 85 +++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 3 deletions(-) diff --git a/drivers/soc/apple/sleep-t6000.h b/drivers/soc/apple/sleep-t6000.h index f0abc5cd374f5..1a2aa64b1f637 100644 --- a/drivers/soc/apple/sleep-t6000.h +++ b/drivers/soc/apple/sleep-t6000.h @@ -154,6 +154,10 @@ static const u8 t6000_clkgen_mux_off[T6000_CLKGEN_MUXES] = { 0x08, 0x10, 0x0c, 0 #define T6000_PSALL_MAX 256 #define T6000_PSALL_BASES 8 +/* SIQ_CFG (SYS_APL_SIQ_CFG_EL1): whether the AIC delivers IRQs to this core */ +#define T6000_SIQ_CFG_ROUTE GENMASK_ULL(1, 0) +#define T6000_SIQ_CFG_ROUTE_NONE 3 + /* Each core's power-down overrides */ #define T6000_CYC_OVRD sys_reg(3, 5, 15, 5, 0) #define T6000_CYC_DISABLE_WFI_RET BIT_ULL(0) @@ -342,20 +346,71 @@ static bool t6000_sleep_cpu_valid(u64 mpidr) MPIDR_AFFINITY_LEVEL(mpidr, 2) == !!cluster && cluster < 3; } -/* Each core's power-down overrides as Linux has them, back after a refusal */ +/* + * Each core's power-down overrides as Linux has them, back after a refusal, and its SIQ_CFG + * from before it went offline. @held: it answered a sleep's hold and has not been restored + * since. @siq_saved: @siq holds what the core had before it went offline, and is not back + * yet; any value is one, 0 included (route field 0 is a route). + */ struct t6000_cpu_control { - u64 cyc, acc; - bool saved; + u64 cyc, acc, siq; + bool saved, held, siq_saved; }; static DEFINE_PER_CPU(struct t6000_cpu_control, t6000_cpu_control); +/* + * As XNU does for a core going offline: the AIC stops delivering IRQs to it while it can + * still take them. AICv2 can't steer an IRQ, so one delivered after the core masked them + * would stay pending there: its device never sees it handled, and the core keeps aborting + * its power down at the sleep's commit. Runs on that core, interrupts on. + */ +static int t6000_cpu_down_prep(unsigned int cpu) +{ + struct t6000_cpu_control *c = this_cpu_ptr(&t6000_cpu_control); + u64 siq = read_sysreg_s(SYS_APL_SIQ_CFG_EL1); + + c->siq = siq; + c->siq_saved = true; + write_sysreg_s(siq | FIELD_PREP(T6000_SIQ_CFG_ROUTE, T6000_SIQ_CFG_ROUTE_NONE), + SYS_APL_SIQ_CFG_EL1); + isb(); + + return 0; +} + +/* Back online, after an offline that was rolled back or through a reset after a wake */ +static void t6000_cpu_siq_restore(struct t6000_cpu_control *c) +{ + if (!c->siq_saved) + return; + if (FIELD_GET(T6000_SIQ_CFG_ROUTE, read_sysreg_s(SYS_APL_SIQ_CFG_EL1)) == + T6000_SIQ_CFG_ROUTE_NONE) { + write_sysreg_s(c->siq, SYS_APL_SIQ_CFG_EL1); + isb(); + } + c->siq_saved = false; +} + +static int t6000_cpu_online(unsigned int cpu) +{ + t6000_cpu_siq_restore(this_cpu_ptr(&t6000_cpu_control)); + + return 0; +} + +static void t6000_cpuhp_remove(void *data) +{ + cpuhp_remove_state_nocalls((enum cpuhp_state)(uintptr_t)data); +} + static int t6000_sleep_cpu_prepare(u64 mpidr) { struct t6000_cpu_control *c = this_cpu_ptr(&t6000_cpu_control); if (!is_kernel_in_hyp_mode()) return -EOPNOTSUPP; + c->held = true; if (!c->saved) { c->cyc = read_sysreg_s(T6000_CYC_OVRD); c->acc = read_sysreg_s(T6000_ACC_OVRD); @@ -364,10 +419,19 @@ static int t6000_sleep_cpu_prepare(u64 mpidr) return 0; } +/* + * Called by a held core that a refused sleep sends back to the park, and by every core that + * comes online through the spin table, before any CPU hotplug callback of its own runs: the + * AIC's reads the route there. A core still parked keeps the route at none until then. + */ static void t6000_sleep_cpu_restore(u64 mpidr) { struct t6000_cpu_control *c = this_cpu_ptr(&t6000_cpu_control); + bool parked = c->held && !pm_resume_via_firmware(); + c->held = false; + if (!parked) + t6000_cpu_siq_restore(c); if (!c->saved) return; write_sysreg_s(c->acc, T6000_ACC_OVRD); @@ -675,6 +739,21 @@ static int t6000_sleep_map_regs(struct apple_sleep *s) if (ret) return ret; + /* + * Every core that goes offline, for a sleep or not, has its IRQs routed away first. Only + * at EL2, where SIQ_CFG is the kernel's to write and the sleep can run at all. + */ + if (is_kernel_in_hyp_mode()) { + ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "soc/apple-sleep-t6000:siq", + t6000_cpu_online, t6000_cpu_down_prep); + if (ret < 0) + return ret; + ret = devm_add_action_or_reset(s->dev, t6000_cpuhp_remove, + (void *)(uintptr_t)ret); + if (ret) + return ret; + } + return t6000_display_map(s); } From dc0add26ee2f24c2d3a8563790822e53f14b3702 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Mon, 30 Nov 2026 23:44:33 +0100 Subject: [PATCH 47/72] soc: apple: sleep: let a gen-1 SoC tell the SMC before its peers stop On a SoC with gen-1 SMC keys the SMC hears slpa after SEP and every other coprocessor have stopped. macOS on T6000 tells it after SEP but before the PMP stops. Add smc_before_peers to the SoC data: with it set, the late notice goes out once the wake mask and the SoC's registers are prepared, before the other coprocessors stop, and not again after them. A failure there panics, as the notice after the peers' stop does. Set it for T6000; every other SoC keeps the notice after its peers. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/sleep-t6000.h | 2 ++ drivers/soc/apple/sleep.c | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/drivers/soc/apple/sleep-t6000.h b/drivers/soc/apple/sleep-t6000.h index 1a2aa64b1f637..6acff88be0aef 100644 --- a/drivers/soc/apple/sleep-t6000.h +++ b/drivers/soc/apple/sleep-t6000.h @@ -1707,6 +1707,8 @@ static const struct apple_sleep_soc t6000_sleep_soc = { .wdt_wd1 = true, .smc_gen1_keys = true, .smc_keys_refuse = true, + /* slpa and the SMC's AP 0x201 before the PMP's stop, as macOS has them */ + .smc_before_peers = true, .boot_mpidr = 0x0, .cpu_count = 8, /* diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 3a2030a3ba458..69c00b475174e 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -895,6 +895,11 @@ struct apple_sleep_soc { bool sep_dev_disable; /* ... before the point of no return, where a failed write still refuses the sleep */ bool smc_keys_refuse; + /* + * Gen 1: the SMC hears slpa after SEP's stop but before the other coprocessors', the PMP + * among them, as macOS orders them, instead of after every one of them. + */ + bool smc_before_peers; /* Live coprocessors with DRAM buffers require WAKE_SLP_DDR across sleep. */ bool coproc_slp_ddr; /* @@ -6988,12 +6993,18 @@ static int apple_sleep_enter_prepare(struct apple_sleep *s) } s->soc->ops->prepare_regs(s); s->regs_staged = true; + if (s->soc->smc_before_peers && !s->soc->ops->commit_notice && + apple_sleep_smc_defer_notice()) { + ret = apple_sleep_smc_late_notice(s); + if (ret) + panic("Apple SMC sleep notice failed before peer stop: %d", ret); + } apple_sleep_coprocs_stop(s, false); if (s->soc->ops->commit_notice) { ret = s->soc->ops->commit_notice(s); if (ret) panic("Apple committed SMC notice failed after peer stop: %d", ret); - } else if (apple_sleep_smc_defer_notice()) { + } else if (apple_sleep_smc_defer_notice() && !s->soc->smc_before_peers) { /* Gen 1: the SMC hears last, after SEP and the other coprocessors stopped. */ ret = apple_sleep_smc_late_notice(s); if (ret) From a3b959aeb1b5cdd5df1c70b8e5295c5814c1b0a4 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Wed, 2 Dec 2026 23:37:46 +0100 Subject: [PATCH 48/72] drm/apple: add the firmware 13.5 A500 link sleep call The display firmware's power manager call A500 on 13.x firmware drains the IOMFB link to the AP and marks it inactive, so that the firmware acknowledges an AP power IDLE. Add it to the 13.3 method table as dcpep_unnamed_500 and send it with a false (no teardown) argument, waiting up to 2 s for its reply. Nothing sends it yet. Signed-off-by: Andreas Fernandez --- drivers/gpu/drm/apple/iomfb_template.c | 42 ++++++++++++++++++++++++++ drivers/gpu/drm/apple/iomfb_template.h | 3 ++ drivers/gpu/drm/apple/iomfb_v13_3.c | 1 + 3 files changed, 46 insertions(+) diff --git a/drivers/gpu/drm/apple/iomfb_template.c b/drivers/gpu/drm/apple/iomfb_template.c index 36dca5e1e1463..4a95ad8f559be 100644 --- a/drivers/gpu/drm/apple/iomfb_template.c +++ b/drivers/gpu/drm/apple/iomfb_template.c @@ -2682,6 +2682,48 @@ static void dcp_on_set_parameter(struct apple_dcp *dcp, void *out, void *cookie) } #endif +#if DCP_FW_VER == DCP_FW_VERSION(13, 3, 0) +DCP_THUNK_IN(dcp_s2r_link_sleep_call, dcpep_unnamed_500, u32); + +static void dcp_s2r_link_sleep_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); +} + +/* + * The display firmware's power manager call A500 on 13.x firmware: drains the AP link and + * marks it inactive, so that the firmware acknowledges an AP power IDLE; AP power ON after the + * wake activates the link again. Its argument is a teardown (which also unregisters the AP + * power callbacks), not wanted for a sleep. + */ +int DCP_FW_NAME(iomfb_s2r_link_sleep)(struct apple_dcp *dcp) +{ + struct dcp_wait_cookie *cookie; + unsigned long left; + u32 teardown = 0; + + if (!dcp_methods[dcpep_unnamed_500].tag[0]) + return -EOPNOTSUPP; + 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_s2r_link_sleep_call(dcp, false, &teardown, dcp_s2r_link_sleep_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 + void DCP_FW_NAME(iomfb_poweron)(struct apple_dcp *dcp) { struct dcp_wait_cookie *cookie; diff --git a/drivers/gpu/drm/apple/iomfb_template.h b/drivers/gpu/drm/apple/iomfb_template.h index 2cd64bffc3863..2363c689e46a9 100644 --- a/drivers/gpu/drm/apple/iomfb_template.h +++ b/drivers/gpu/drm/apple/iomfb_template.h @@ -398,6 +398,9 @@ void DCP_FW_NAME(iomfb_shutdown)(struct apple_dcp *dcp); int DCP_FW_NAME(iomfb_s2r_500)(struct apple_dcp *dcp); int DCP_FW_NAME(iomfb_system_power)(struct apple_dcp *dcp, bool on); #endif +#if DCP_FW_VER == DCP_FW_VERSION(13, 3, 0) +int DCP_FW_NAME(iomfb_s2r_link_sleep)(struct apple_dcp *dcp); +#endif #if DCP_FW_H17(DCP_FW_VER) int DCP_FW_NAME(iomfb_s2r_500)(struct apple_dcp *dcp); int DCP_FW_NAME(iomfb_s2r_power)(struct apple_dcp *dcp, bool on, bool named_calls, bool oob); diff --git a/drivers/gpu/drm/apple/iomfb_v13_3.c b/drivers/gpu/drm/apple/iomfb_v13_3.c index 3521abccf038e..15c16517f5df4 100644 --- a/drivers/gpu/drm/apple/iomfb_v13_3.c +++ b/drivers/gpu/drm/apple/iomfb_v13_3.c @@ -39,6 +39,7 @@ 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), + IOMFB_METHOD("A500", dcpep_unnamed_500), }; #define DCP_FW v13_3 From 8a789cd2535fa6280902d15d1b4869e7148b9553 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Wed, 2 Dec 2026 23:37:47 +0100 Subject: [PATCH 49/72] drm/apple: remember a poweron that timed out Remember whether the last poweron timed out ("connector will be broken"), so that a firmware sleep can be refused while the firmware is stuck. Only the flag is written here; no firmware path changes. Signed-off-by: Andreas Fernandez --- drivers/gpu/drm/apple/dcp-internal.h | 2 ++ drivers/gpu/drm/apple/iomfb_template.c | 3 +++ 2 files changed, 5 insertions(+) diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index b471e87d8f15f..7b227bf70eb57 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -1085,6 +1085,8 @@ struct apple_dcp { /* Messages from the coprocessor are held in the mailbox for sleep. */ bool rx_held; + /* The last poweron timed out ("connector will be broken"): no firmware sleep */ + bool poweron_timed_out; /* A firmware-driven suspend stops the firmware, see dcp_s2r_suspend(). */ struct apple_dcp_afkep *sleep_v27_ep[0x20]; diff --git a/drivers/gpu/drm/apple/iomfb_template.c b/drivers/gpu/drm/apple/iomfb_template.c index 4a95ad8f559be..443b5dedaa83b 100644 --- a/drivers/gpu/drm/apple/iomfb_template.c +++ b/drivers/gpu/drm/apple/iomfb_template.c @@ -2797,6 +2797,9 @@ void DCP_FW_NAME(iomfb_poweron)(struct apple_dcp *dcp) dcp_backlight_desktop_power(dcp, true); #endif + /* Until a poweron completes again, a firmware sleep is refused (dcp_platform_prepare()). */ + if (ret >= 0) + WRITE_ONCE(dcp->poweron_timed_out, ret == 0); if (ret == 0) { dev_warn(dcp->dev, "wait for power timed out, connector will be broken\n"); } else if (ret > 0) { From f24eb6f8c49ceb73f80c2ed505384d4364af0a56 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 00:25:16 +0100 Subject: [PATCH 50/72] soc: apple: sleep: T6000: power-cycle disp0 after a refused sleep A display coprocessor parked for a deep sleep, as the internal DCP's driver parks its 13.5 firmware from the next change on, starts again only after its block lost power. A sleep given up after the park but before .display_off took disp0 down would leave nothing to restart it: apple_sleep_refusal_cycled() answers only for T6050, and apple_sleep_engine_reset() finds no PMGR word in the DCP's power-domains (it is the PMP report's disp0 entry). Let a profile answer apple_sleep_refusal_cycled() through a new optional op. T6000 answers for disp0's CPU word: true if .display_off took disp0 down in this attempt (its .finish has raised it again), else it takes disp0 down and up now, as the sleep entry does without the GPU's port, with the PMP request back and the power words raised to what they were at .begin. Asked from the DCP's resume, this also covers a refusal in suspend_late, after which the platform's .wake does not run. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/sleep-t6000-display.inc | 3 ++ drivers/soc/apple/sleep-t6000.h | 47 +++++++++++++++++++++++ drivers/soc/apple/sleep-t6050.h | 2 + drivers/soc/apple/sleep.c | 5 +++ 4 files changed, 57 insertions(+) diff --git a/drivers/soc/apple/sleep-t6000-display.inc b/drivers/soc/apple/sleep-t6000-display.inc index a7d63f652d4a3..2a02aa4c32655 100644 --- a/drivers/soc/apple/sleep-t6000-display.inc +++ b/drivers/soc/apple/sleep-t6000-display.inc @@ -119,6 +119,8 @@ static struct t6000_display { bool undo_due; /* disp0's entry is dropped here, not by the PMP's own suspend */ bool disp0_owned; + /* disp0 went down in this attempt: a sleep given up then raises it again */ + bool disp0_cycled; /* For the report */ u32 gfx_stuck, disp_stuck; @@ -263,6 +265,7 @@ static void t6000_sleep_disp0_down(struct apple_sleep *s) d->disp_stuck = 0; d->disp0_was_requested = false; + d->disp0_cycled = true; if (blk && !FIELD_GET(PS_ACTUAL, readl(fe))) blk = NULL; diff --git a/drivers/soc/apple/sleep-t6000.h b/drivers/soc/apple/sleep-t6000.h index 6acff88be0aef..f7f6ebc248ba2 100644 --- a/drivers/soc/apple/sleep-t6000.h +++ b/drivers/soc/apple/sleep-t6000.h @@ -796,6 +796,7 @@ static int t6000_sleep_begin(struct apple_sleep *s) t->cluster_written = 0; t->disp_undo_due = false; t->afr_cl_valid = false; + t6000_display.disp0_cycled = false; t6000_ps_late_save(t); t6000_psall_save(t); return t6000_display_own(s, true); @@ -1639,6 +1640,51 @@ static void t6000_sleep_finish(struct apple_sleep *s) t6000_display_own(s, false); } +/* + * The internal DCP's driver parks its firmware for a sleep that commits, and a parked firmware + * starts again only once disp0 lost its power. Asked by that driver's resume after a sleep + * that was given up: if .display_off took disp0 down, .finish has raised it again. If not, + * disp0 goes down and up now, as macOS's order takes it down without the GPU's port, with its + * request back and its power words as they were at .begin. + */ +static bool t6000_sleep_refusal_cycled(struct apple_sleep *s, struct device_node *pd) +{ + struct t6000_display *d = &t6000_display; + struct t6000_sleep *t = &t6000_sleep; + DECLARE_BITMAP(seen, T6000_PSALL_MAX); + static const u32 disp0_ps[] = { T6000_DISP0_CPU0_PS, T6000_DISP0_FE_PS }; + struct device_node *parent; + struct resource res; + unsigned int k, i; + bool disp0; + u32 off; + + if (!pd || t->committed || !d->east_ps) + return false; + parent = of_get_parent(pd); + disp0 = parent && !of_address_to_resource(parent, 0, &res) && + res.start == T6000_DISP_PS_EAST && !of_property_read_u32(pd, "reg", &off) && + off == T6000_DISP0_CPU0_PS; + of_node_put(parent); + if (!disp0) + return false; + if (d->disp0_cycled) + return true; + + t6000_sleep_disp0_down(s); + d->undo_due = true; + t6000_sleep_display_undo(s); + t->psall_pass = 0; + bitmap_zero(seen, T6000_PSALL_MAX); + for (k = 0; k < ARRAY_SIZE(disp0_ps); k++) + for (i = 0; i < t->psall_n; i++) + if (t->psall[i].pa == T6000_DISP_PS_EAST + disp0_ps[k]) + t6000_psall_on(s, t, i, 0, seen); + + return !(d->disp_stuck & T6000_DISP0_STUCK_CPU0) && + FIELD_GET(PS_ACTUAL, readl(d->east_ps + T6000_DISP0_CPU0_PS)); +} + /* At .end: the PTD sleep word back */ static void t6000_sleep_end(struct apple_sleep *s) { @@ -1688,6 +1734,7 @@ static const struct apple_sleep_ops t6000_sleep_ops = { .wake = t6000_sleep_wake, .finish = t6000_sleep_finish, .end = t6000_sleep_end, + .refusal_cycled = t6000_sleep_refusal_cycled, .cpu_prepare = t6000_sleep_cpu_prepare, .cpu_restore = t6000_sleep_cpu_restore, }; diff --git a/drivers/soc/apple/sleep-t6050.h b/drivers/soc/apple/sleep-t6050.h index 623da36c05a13..570d86b12f65e 100644 --- a/drivers/soc/apple/sleep-t6050.h +++ b/drivers/soc/apple/sleep-t6050.h @@ -3573,6 +3573,8 @@ bool apple_sleep_refusal_cycled(struct device_node *pd) unsigned int i; int word; + if (s && s->soc->ops->refusal_cycled) + return s->soc->ops->refusal_cycled(s, pd); if (!r || !r->refusal_cycled || !pd) return false; word = sleep_region_domain_offset(s, pd); diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 69c00b475174e..cfd39632a1d92 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -630,6 +630,11 @@ struct apple_sleep_ops { void (*smc_warned)(struct apple_sleep *s); /* Optional: at .end, last, once the watchdog is back as user space had it */ void (*end)(struct apple_sleep *s); + /* + * Optional, for apple_sleep_refusal_cycled(): whether @pd's word was switched off and on + * since a refused entry began, which the SoC may do on the asking driver's behalf. + */ + bool (*refusal_cycled)(struct apple_sleep *s, struct device_node *pd); /* Pure bounded planning only; no register read/write or entry authorization. */ int (*plan_complex_request)(u32 observed, u32 *request); From 219efd458c09ec9493c3e8a00c18ab6a5b9ce075 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Mon, 30 Nov 2026 23:40:39 +0100 Subject: [PATCH 51/72] drm/apple: T6000: park the internal DCP for deep sleep A deep sleep on T6000 cuts the internal display block power, and a display firmware that still runs then does not start again. Park it as macOS does: after every device suspend (a new suspend_late), put the IOMFB link to sleep with A500, idle AP and IOP and stop the CPU with the RTKit buffers kept (apple_rtkit_idle_iop(), apple_rtkit_detach_keep() and RUN cleared). The firmware sleep has to be selected and not a dry run, and the firmware has to be 13.5. If the IOP idle fails after the AP went idle, the AP is put back on and the firmware keeps running. On resume the firmware restarts warm: apple_rtkit_reinit_keep(), RUN, apple_rtkit_wake_keep_ap() and apple_rtkit_restart_started_eps(), and the display resume powers the panel on again. After a sleep refused before the block lost power, the firmware has idled with its power kept: it restarts after a power cycle of its block, which apple_sleep_refusal_cycled() asks the sleep driver for; a block that was not cycled is reported. A parked firmware drives no PHY, so for a sleep that parks, the PHYs are no longer put on the wakeup path: genpd takes their domains off before the commit, which lowers their parents, and back on after them at the wake. A sleep that does not park (s2idle, a dry run) holds them as before. Refuse the deep sleep in prepare when the last poweron timed out. Selected by a new apple,t6000-dcp match with hw.s2r_park. T6001 and T6002 share that compatible and match it too, but the park also needs a sleep that stops the coprocessors in its commit, which only a T6000 sleep node provides, so they keep their path; the other SoCs keep theirs. Signed-off-by: Andreas Fernandez --- drivers/gpu/drm/apple/dcp-internal.h | 14 +++ drivers/gpu/drm/apple/dcp.c | 169 ++++++++++++++++++++++++++- 2 files changed, 178 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index 7b227bf70eb57..139c39635ea01 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -433,6 +433,12 @@ struct apple_dcp_hw_data { bool s2r_ext; /* An external display processor (a dcpext block), not the internal display's */ bool external_processor; + /* + * With 13.5 firmware on the internal processor: a deep sleep, which cuts the display + * block's power, parks the firmware (A500, IDLE, CPU stopped, RTKit buffers kept) in + * the late suspend and restarts it warm, see dcp_s2r_park(). + */ + bool s2r_park; /* 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 */ @@ -1085,6 +1091,14 @@ struct apple_dcp { /* Messages from the coprocessor are held in the mailbox for sleep. */ bool rx_held; + /* hw.s2r_park: the suspend got as far as the park, which waits for the late suspend */ + bool s2r_park_due; + /* hw.s2r_park: parked for the sleep (IDLE, CPU stopped), restarted by resume */ + bool s2r_parked; + /* hw.s2r_park: the park set the AP to IDLE, the restart sets it ON again */ + bool s2r_ap_idle; + /* The park released the mailbox hold: resume unpauses the presentation */ + bool s2r_unheld; /* The last poweron timed out ("connector will be broken"): no firmware sleep */ bool poweron_timed_out; diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 69494314b7614..97fd11960e207 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -10141,13 +10141,130 @@ static const char *dcp_external_sleep_vetoed(struct apple_dcp *dcp) return NULL; } -/* dpm_prepare completes for every device before any dpm_suspend callback. - * Veto early so Thunderbolt cannot tear down a working tunnel first. The - * same HPD lock serializes this gate against explicit firmware startup. +/* + * hw.s2r_park (T6000's internal DCP, 13.5 firmware): a deep sleep cuts the display block's + * power, and a firmware still running then does not start again. So it is parked after every + * device's suspend, as macOS does it: A500 puts the IOMFB link to sleep, the AP goes IDLE + * where it did, then the IOP, and the CPU stops with the RTKit buffers kept. Not in a dry + * run, which cuts no power. */ +static bool dcp_s2r_park_selected(struct apple_dcp *dcp) +{ + return IS_ENABLED(CONFIG_APPLE_SOC_SLEEP) && dcp->hw.s2r_park && !dcp->external && + dcp->fw_compat == DCP_FIRMWARE_V_13_5 && apple_sleep_commit_stops_coprocs() && + !apple_sleep_dry_run(); +} + +/* The IDLE acknowledgment comes through the mailbox that dcp_hold_for_sleep() held. */ +static void dcp_s2r_park_unhold(struct apple_dcp *dcp) +{ + if (!dcp->rx_held) + return; + apple_rtkit_resume_rx(dcp->rtk); + dcp->rx_held = false; + dcp->s2r_unheld = true; +} + +static void dcp_s2r_park(struct apple_dcp *dcp) +{ + void __iomem *control = dcp->coproc_reg + APPLE_DCP_COPROC_CPU_CONTROL; + u32 ctrl; + int ret; + + if (dcp->hw.adopt_live_session || IS_ERR_OR_NULL(dcp->rtk) || !dcp->coproc_reg || + READ_ONCE(dcp->crashed) || !apple_rtkit_is_running(dcp->rtk)) + return; + + dcp_s2r_park_unhold(dcp); + ret = iomfb_s2r_link_sleep_v13_3(dcp); + dev_dbg(dcp->dev, "s2r: link sleep (A500): %d\n", ret); + if (!ret) + ret = apple_rtkit_ap_power_timeout(dcp->rtk, false, 2000); + dcp->s2r_ap_idle = !ret; + ret = apple_rtkit_idle_iop(dcp->rtk); + if (ret) { + dev_warn(dcp->dev, "s2r: IOP IDLE failed: %d, not parked\n", ret); + /* + * Not parked, so dcp_s2r_unpark() does nothing: the AP goes back ON here, which + * also makes A500's link active again. Before RX is held again: the answer comes + * through the mailbox. + */ + if (dcp->s2r_ap_idle) { + ret = apple_rtkit_ap_power_timeout(dcp->rtk, true, 2000); + if (ret) + dev_err(dcp->dev, "s2r: AP not back ON after a failed park: %d\n", ret); + dcp->s2r_ap_idle = false; + } + if (dcp->s2r_unheld) { + apple_rtkit_hold_rx(dcp->rtk); + dcp->rx_held = true; + dcp->s2r_unheld = false; + } + return; + } + apple_rtkit_detach_keep(dcp->rtk); + ctrl = readl_relaxed(control); + writel_relaxed(ctrl & ~APPLE_DCP_COPROC_CPU_CONTROL_RUN, control); + dcp->s2r_parked = true; + dcp->sleep_fw_runs = false; + apple_sleep_display_quiesced(dcp->dev, true); + dev_dbg(dcp->dev, "s2r: parked, cpu control %#x\n", ctrl); +} + +/* + * The parked firmware restarts warm, from what IDLE saved: the CPU runs again with the RTKit + * buffers kept, INIT and the endpoint map, the started endpoints started again and the AP ON + * if the park set it IDLE. That needs its display block to have lost its power: a deep sleep + * did that, and after one given up the sleep driver powers the block down and up first. The + * panel comes back with the display's resume (poweron). + */ +static void dcp_s2r_unpark(struct apple_dcp *dcp) +{ + void __iomem *control = dcp->coproc_reg + APPLE_DCP_COPROC_CPU_CONTROL; + struct device_node *ps; + u32 ctrl; + int ret; + + dcp->s2r_park_due = false; + if (!dcp->s2r_parked) + return; + dcp->s2r_parked = false; + apple_sleep_display_quiesced(dcp->dev, false); + if (!pm_resume_via_firmware()) { + ps = of_parse_phandle(dcp->dev->of_node, "resets", 0); + if (!apple_sleep_refusal_cycled(ps)) + dev_warn(dcp->dev, "s2r: sleep given up and the display block not power-cycled: the restart may fail\n"); + of_node_put(ps); + } + + ctrl = readl_relaxed(control); + ret = apple_rtkit_reinit_keep(dcp->rtk); + if (!ret) { + writel_relaxed(ctrl | APPLE_DCP_COPROC_CPU_CONTROL_RUN, control); + ret = apple_rtkit_wake_keep_ap(dcp->rtk); + } + if (!ret) + ret = apple_rtkit_restart_started_eps(dcp->rtk); + if (!ret && dcp->s2r_ap_idle) + ret = apple_rtkit_ap_power_timeout(dcp->rtk, true, 2000); + dcp->s2r_ap_idle = false; + if (ret) + dev_err(dcp->dev, "s2r: restart failed: %d (cpu control was %#x)\n", ret, ctrl); + else + dev_dbg(dcp->dev, "s2r: restarted (cpu control was %#x)\n", ctrl); + if (dcp->s2r_unheld) { + dcp->s2r_unheld = false; + iomfb_pause_presentation(dcp, false); + } +} + static bool dcp_v27_sleep_selected(struct apple_dcp *dcp); static void dcp_v27_sleep_invalidate(struct apple_dcp *dcp); +/* dpm_prepare completes for every device before any dpm_suspend callback. + * Veto early so Thunderbolt cannot tear down a working tunnel first. The + * same HPD lock serializes this gate against explicit firmware startup. + */ static int dcp_platform_prepare(struct device *dev) { struct apple_dcp *dcp = dev_get_drvdata(dev); @@ -10198,6 +10315,14 @@ static int dcp_platform_prepare(struct device *dev) } return 0; } + /* + * A firmware that did not power its display on is stuck: parked for a deep sleep it + * would not answer, and the sleep resets the SoC. Refused here, where the sleep is known. + */ + if (dcp_s2r_park_selected(dcp) && READ_ONCE(dcp->poweron_timed_out)) { + dev_err(dev, "display firmware did not power on, refusing deep sleep\n"); + return -EBUSY; + } if (!dcp->external) return 0; mutex_lock(&dcp->hpd_mutex); @@ -13218,6 +13343,7 @@ static int dcp_platform_suspend(struct device *dev) struct apple_dcp *dcp = dev_get_drvdata(dev); dcp->sleep_fw_runs = false; + dcp->s2r_park_due = false; /* * The display subsystem's suspend already stopped this DCP and lowered its power words: * its registers sit behind a powered-off block, and a read there stalls the CPU. @@ -13454,10 +13580,34 @@ static int dcp_platform_suspend(struct device *dev) */ dcp->sleep_fw_runs = true; device_set_wakeup_path(dev); - dcp_sleep_hold_phys(dcp); + /* + * A firmware parked for the sleep drives no PHY, so the PHY's power domain is left to + * genpd: off before the commit, on again after its parents at the wake. Held on, it + * would keep its state while the commit lowers those parents under it. + */ + dcp->s2r_park_due = dcp_s2r_park_selected(dcp); + if (!dcp->s2r_park_due) + dcp_sleep_hold_phys(dcp); /* Last: the steps above still talk to the coprocessor. */ dcp_hold_for_sleep(dcp); - dcp_commit_stop_arm(dcp); + /* hw.s2r_park: parked in the late suspend and restarted warm, never stopped cold. */ + if (!dcp->hw.s2r_park) + dcp_commit_stop_arm(dcp); + + return 0; +} + +/* + * hw.s2r_park: a parked firmware only starts again once its block lost power. So it is parked + * after every device's suspend: when one of those refuses the sleep, nothing has stopped it. + */ +static int dcp_platform_suspend_late(struct device *dev) +{ + struct apple_dcp *dcp = dev_get_drvdata(dev); + + if (dcp->s2r_park_due) + dcp_s2r_park(dcp); + dcp->s2r_park_due = false; return 0; } @@ -13516,6 +13666,7 @@ static int dcp_platform_resume(struct device *dev) if (READ_ONCE(dcp->ext_dead)) return 0; + dcp_s2r_unpark(dcp); dcp->link_recovery_tries = 0; if (dcp_is_ext26(dcp)) { if (!(readl(dcp->coproc_reg + APPLE_DCP_COPROC_CPU_CONTROL) & APPLE_DCP_COPROC_CPU_CONTROL_RUN) || @@ -13572,6 +13723,7 @@ static const struct dev_pm_ops dcp_platform_pm_ops = { .prepare = pm_sleep_ptr(dcp_platform_prepare), .complete = pm_sleep_ptr(dcp_platform_complete), .suspend = pm_sleep_ptr(dcp_platform_suspend), + .suspend_late = pm_sleep_ptr(dcp_platform_suspend_late), .resume = pm_sleep_ptr(dcp_platform_resume), .freeze = pm_sleep_ptr(dcp_platform_freeze), .thaw = pm_sleep_ptr(dcp_platform_resume), @@ -13691,6 +13843,12 @@ static const struct apple_dcp_hw_data apple_dcp_hw_dcp = { .num_dptx_ports = 0, }; +/* T6000's internal DCP: parked for a deep sleep, see dcp_s2r_park() */ +static const struct apple_dcp_hw_data apple_dcp_hw_t6000 = { + .num_dptx_ports = 0, + .s2r_park = true, +}; + static const struct apple_dcp_hw_data apple_dcp_hw_t6030 = { .iboot_handoff = "apple,t6030-handoff", .num_dptx_ports = 1, @@ -13851,6 +14009,7 @@ static const struct of_device_id of_match[] = { { .compatible = "apple,t6050-dcpext", .data = &apple_dcp_hw_t6050_dcpext }, { .compatible = "apple,t6020-dcp", .data = &apple_dcp_hw_t6020, }, { .compatible = "apple,t6020-dcpext", .data = &apple_dcp_hw_t6020_dcpext, }, + { .compatible = "apple,t6000-dcp", .data = &apple_dcp_hw_t6000, }, { .compatible = "apple,t8103-dcp", .data = &apple_dcp_hw_t8103, }, { .compatible = "apple,t8103-dcpext", .data = &apple_dcp_hw_t8103_dcpext, }, { .compatible = "apple,t8112-dcp", .data = &apple_dcp_hw_t8112, }, From 766445bd922af916a7f1e26f8b9333e39a2af39b Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 01:50:30 +0100 Subject: [PATCH 52/72] soc: apple: sleep: T6000: take the external display blocks down too A T6000 deep sleep takes all three display blocks down, a display attached or not: disp0, then dispext0 and dispext1, as macOS does at sleep entry. The PMGR's sleep engine waits after the commit for what dispext0's fabric handshake sets, so a sleep that leaves dispext0 up may never power off. Each external block goes down the way disp0 does: its fabric port's setup (the sleep node's apple,tunable-dispext0-fabric and apple,tunable-dispext1-fabric) while its front end has power, its PMP request (13, 14) dropped and acknowledged within 200 ms, the CPU's power state lowered in two steps with its poll, the fabric handshake, then the front end's with its poll. A block that stays up refuses the sleep, named by the first block that stuck. The PMP's own late suspend leaves all three requests to this, from .begin to .finish, as it did for disp0. A sleep given up puts back every request the lowering dropped that was up, and the power word restore raises the blocks as before. A display coprocessor parked by its driver starts again only once its block lost power. Let a profile hear of each park and restart through a new optional display_parked op, called from apple_sleep_display_quiesced(). T6000 records the parked blocks, and a sleep given up before .display_off took them down cycles them at .wake, ahead of the noirq resume that writes their DARTs back: cycled later, a block would lose them again under the restarting firmware. A sleep given up in suspend_late or suspend_noirq, where .wake does not run, still has the block cycled from its coprocessor's resume, dispext0's and dispext1's as disp0's. The blocks share one description from here on: each fabric port is a reg of the sleep node named after it, with its setup in apple,tunable-, written whole in the listed order, the GPU's and disp0's included, which had them fixed in the file. The power-state words are reached through the sleep node's "ps" and "ps-east", which the profile maps already, and written field by field. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/sleep-t6000-display.inc | 482 ++++++++++++++-------- drivers/soc/apple/sleep-t6000.h | 113 +++-- drivers/soc/apple/sleep.c | 11 +- 3 files changed, 390 insertions(+), 216 deletions(-) diff --git a/drivers/soc/apple/sleep-t6000-display.inc b/drivers/soc/apple/sleep-t6000-display.inc index 2a02aa4c32655..448c6a52db9df 100644 --- a/drivers/soc/apple/sleep-t6000-display.inc +++ b/drivers/soc/apple/sleep-t6000-display.inc @@ -1,26 +1,29 @@ /* SPDX-License-Identifier: GPL-2.0-only OR MIT */ /* - * T6000 (M1 Pro): the internal display block and the GPU's fabric port taken down for a deep - * sleep, in the order macOS 13.7.8 takes them down at sleep entry, while the PMP still runs. + * T6000 (M1 Pro): the display blocks and the GPU's fabric port taken down for a deep sleep, in + * the order macOS 13.7.8 takes them down at sleep entry, while the PMP still runs. * Included by the T6000 profile, which calls t6000_display_map() from its .map_regs, * t6000_sleep_display_off() as its .display_off and t6000_sleep_display_undo() when an entry * that ran .display_off is given up before the SoC went down. * - * Its driver has stopped the display coprocessor (IDLE, CPU stopped) by then. Each block is - * first dropped from the PMP's device requests and acknowledged, then its power states are - * lowered. Before a power state goes, the fabric block behind it gets the setup macOS writes - * when it powers the block up (Linux leaves it at reset values) and the fabric handshake. A - * display block still up after its waits refuses the sleep, before SEP and the SMC are told. + * Their drivers have stopped the display coprocessors that ran (IDLE, CPU stopped) by then. + * Each block is first dropped from the PMP's device requests and acknowledged, then its power + * states are lowered. Before a power state goes, the fabric block behind it gets its setup + * and the fabric handshake. The setup is the device tree's apple,tunable--fabric list + * for the block's -fabric reg, each word written whole in the list's order, as macOS + * writes it when it powers the block up. Linux leaves the blocks at reset values, so there + * is nothing of the boot loader's to save and write back instead. A display block still up + * after its waits refuses the sleep, before SEP and the SMC are told. * - * Only disp0, the internal display. The external display blocks (dispext0, dispext1) are not - * touched here. + * All three blocks go down on every sleep, a display attached or not: disp0 (the internal + * display) and the external ones, dispext0 and dispext1. The PMGR's sleep engine waits after + * the commit for what dispext0's fabric handshake sets (0x2922c0044 bit 2, hv trace). */ -/* Power state words, in the PMGR blocks at 0x28e080000 and 0x28e580000 */ +/* Power state words, in the PMGR blocks at 0x28e080000 and 0x28e580000 (east) */ +#define T6000_DISP_PS_PMGR 0x28e080000ULL #define T6000_DISP_PS_EAST 0x28e580000ULL -#define T6000_DISP_PS_EAST_LEN 0x1000 -#define T6000_DISP0_CPU0_PS 0x350 /* east */ -#define T6000_DISP0_FE_PS 0x328 /* east */ +#define T6000_DISP_PS_LEN 0x1000 #define T6000_GFX_PS 0x3c0 /* east */ #define T6000_AFR_PS 0x28e0801e8ULL /* The AFR cluster's word (cluster 5) of the PMGR cluster power states */ @@ -29,102 +32,93 @@ #define T6000_AFR_CLUSTER_UP BIT(5) #define T6000_AFR_CLUSTER_BUSY BIT(4) #define T6000_AFR_GATE 0x28e5e4000ULL -/* The fabric blocks set up below: the GPU's and disp0's */ -#define T6000_GFX_BLK 0x405000000ULL -#define T6000_DISP0_BLK 0x38c000000ULL -#define T6000_FABRIC_BLK_LEN 0xc000 +#define T6000_AFR_GATE_EN BIT(0) #define T6000_FABRIC_HS_8014 0x8014 #define T6000_FABRIC_HS_82BC 0x82bc -/* PMP device requests (bits of SOC-DEV-PS-REQ): the GPU and disp0 */ +/* + * A power-state word as these steps write it, field by field: auto-enable, the auto state, the + * minimum state, the two latched flags (written back to clear them), the actual state as read + * back (read-only), and the target. States: on, clock-gated, power-gated. + */ +#define T6000_PS_MIN GENMASK(19, 16) +#define T6000_PS_ON 0xf +#define T6000_PS_CLKGATE 0x4 +#define T6000_PS_OFF 0x0 +#define T6000_PSW(en, autostate, min, flags, actual, target) \ + (((en) ? PS_AUTO_ENABLE : 0) | FIELD_PREP_CONST(PS_AUTO_STATE, autostate) | \ + FIELD_PREP_CONST(T6000_PS_MIN, min) | FIELD_PREP_CONST(PS_FLAGS, flags) | \ + FIELD_PREP_CONST(PS_ACTUAL, actual) | FIELD_PREP_CONST(PS_TARGET, target)) + +/* PMP device requests (bits of SOC-DEV-PS-REQ): the GPU's; the displays' are below */ #define T6000_PMP_AGX_ID 9 -#define T6000_PMP_DISP0_ID 12 #define T6000_PMP_ACK_US 200000 -/* What .display_off found stuck: the bits macOS's order would give them, disp0 refuses */ -#define T6000_DISP0_STUCK_CPU0 BIT(2) -#define T6000_DISP0_STUCK_FE BIT(3) -#define T6000_DISP0_STUCK_PMP BIT(9) -#define T6000_DISP0_STUCK (T6000_DISP0_STUCK_CPU0 | T6000_DISP0_STUCK_FE | \ - T6000_DISP0_STUCK_PMP) - /* - * The disp0 fabric block's setup, as macOS writes it when it powers the block up (hv trace, - * macOS 13.7.8 boot, 0x38c000000). + * The display blocks, numbered as the stuck bits count them: block b's CPU word is bit 2 * b, + * its front end's bit 2 * b + 1, its PMP request bit 8 + b. */ -static const u32 t6000_disp0_init[][2] = { - { 0x00000, 0x11 }, { 0x00010, 0x610000 }, { 0x00014, 0x8 }, { 0x00018, 0x8 }, - { 0x0001c, 0x18 }, { 0x00020, 0x18 }, { 0x00024, 0x1 }, { 0x00028, 0x1 }, - { 0x0002c, 0x18 }, { 0x00030, 0x18 }, { 0x00034, 0x18 }, { 0x00400, 0x40a10001 }, - { 0x00600, 0xaab }, { 0x00700, 0x1ffffff }, { 0x008a8, 0x2000005 }, { 0x008ac, 0x14d004e }, - { 0x008b0, 0x14d004e }, { 0x008b4, 0x14d004e }, { 0x008b8, 0x14d004e }, - { 0x008bc, 0x14d004e }, { 0x008c0, 0x14d004e }, { 0x008c4, 0x14d004e }, - { 0x008c8, 0x14d004e }, { 0x00410, 0x1100 }, - { 0x00420, 0x1100 }, { 0x00430, 0x1100 }, { 0x08000, 0x9 }, { 0x00a20, 0x2 }, - { 0x08008, 0x7 }, { 0x08014, 0x1 }, { 0x08018, 0x1 }, { 0x00920, 0x3fff0001 }, - { 0x08208, 0x8 }, { 0x08280, 0x20 }, { 0x08288, 0x3 }, { 0x0828c, 0xc }, - { 0x08290, 0x18 }, { 0x08294, 0x30 }, { 0x08298, 0x78 }, { 0x0829c, 0xf0 }, - { 0x082b8, 0x1 }, { 0x082bc, 0x1 }, { 0x082c0, 0x1 }, { 0x00860, 0x1 }, - { 0x0820c, 0x5 }, { 0x08284, 0x20 }, { 0x082a0, 0x3 }, { 0x082a4, 0xc }, - { 0x082a8, 0x18 }, { 0x082ac, 0x30 }, { 0x082b0, 0x78 }, { 0x082b4, 0xf0 }, - { 0x082b8, 0x3 }, { 0x082bc, 0x2 }, { 0x082c0, 0x3 }, { 0x08210, 0x0 }, - { 0x08408, 0x3 }, { 0x08418, 0x3 }, { 0x0841c, 0x0 }, { 0x08420, 0xffffffff }, - { 0x08424, 0x0 }, { 0x08428, 0xfff }, { 0x082b8, 0x7 }, { 0x082bc, 0x4 }, - { 0x082c0, 0x7 }, -}; +enum { T6000_DISPEXT0, T6000_DISP0, T6000_DISPEXT1, T6000_DISP_BLOCKS }; +#define T6000_DISP_ALL GENMASK(T6000_DISP_BLOCKS - 1, 0) -/* The GPU fabric block's setup, macOS 13.7.8 sleep entry, after the GFX power state is on */ -static const u32 t6000_gfx_init[][2] = { - { 0x714, 0x2000000 }, { 0x718, 0x400080 }, { 0x71c, 0x400080 }, { 0x720, 0x400080 }, - { 0x724, 0x400080 }, { 0x728, 0x400080 }, { 0x72c, 0x400080 }, { 0x730, 0x400080 }, - { 0x734, 0x400080 }, { 0x00, 0x11 }, { 0x10, 0xd0000 }, { 0x14, 0x1 }, - { 0x18, 0x1 }, { 0x1c, 0x3 }, { 0x20, 0x3 }, { 0x24, 0x3 }, - { 0x28, 0x3 }, { 0x2c, 0x3 }, { 0x400, 0x40a10001 }, { 0x600, 0x1ffffff }, - { 0x800, 0x101 }, { 0x410, 0x1100 }, { 0x420, 0x1100 }, { 0x430, 0x1100 }, - { 0x8000, 0x9 }, { 0x820, 0x80 }, { 0x8008, 0x7 }, { 0x8014, 0x1 }, - { 0x8018, 0x1 }, { 0x748, 0x1 }, { 0x8208, 0x2 }, { 0x8280, 0x20 }, - { 0x8288, 0x3 }, { 0x828c, 0xc }, { 0x8290, 0x18 }, { 0x8294, 0x30 }, - { 0x8298, 0x78 }, { 0x829c, 0xf0 }, { 0x82b8, 0x7 }, { 0x82bc, 0x1 }, - { 0x82c0, 0x7 }, { 0x7a8, 0x1 }, { 0x820c, 0x5 }, { 0x8284, 0x20 }, - { 0x82a0, 0x3 }, { 0x82a4, 0xc }, { 0x82a8, 0x18 }, { 0x82ac, 0x30 }, - { 0x82b0, 0x78 }, { 0x82b4, 0xf0 }, { 0x82b8, 0x7 }, { 0x82bc, 0x2 }, - { 0x82c0, 0x7 }, { 0x8210, 0x0 }, { 0x8408, 0xd }, { 0x8418, 0x3 }, - { 0x841c, 0x0 }, { 0x8420, 0xffffffff }, { 0x8424, 0x0 }, { 0x8428, 0xfff }, - { 0x82b8, 0x7 }, { 0x82bc, 0x4 }, { 0x82c0, 0x7 }, +static const struct t6000_disp_block { + const char *name; + u64 cpu0, fe; + /* Its fabric block's reg-name, and its setup's apple,tunable- */ + const char *fabric; + /* Its PMP device request */ + unsigned int pmp_id; +} t6000_disp_blocks[T6000_DISP_BLOCKS] = { + [T6000_DISPEXT0] = { "dispext0", 0x28e080280, 0x28e080258, "dispext0-fabric", 13 }, + [T6000_DISP0] = { "disp0", 0x28e580350, 0x28e580328, "disp0-fabric", 12 }, + [T6000_DISPEXT1] = { "dispext1", 0x28e0802a8, 0x28e080260, "dispext1-fabric", 14 }, }; +/* macOS's order, disp0, dispext0, dispext1: its requests step 0x7000, 0x6000, 0x4000, 0 */ +static const u8 t6000_disp_order[] = { T6000_DISP0, T6000_DISPEXT0, T6000_DISPEXT1 }; + /* macOS's AFR power state writes after GFX goes down, in order */ static const u32 t6000_gfx_afr_down[] = { - 0x404004f, 0xf0403ff, 0x1f0400ff, 0x140f024f, 0xf0f02ff, 0x4040340, + T6000_PSW(0, T6000_PS_CLKGATE, T6000_PS_CLKGATE, 0, T6000_PS_CLKGATE, T6000_PS_ON), + T6000_PSW(0, T6000_PS_ON, T6000_PS_CLKGATE, 3, T6000_PS_ON, T6000_PS_ON), + T6000_PSW(1, T6000_PS_ON, T6000_PS_CLKGATE, 0, T6000_PS_ON, T6000_PS_ON), + T6000_PSW(1, T6000_PS_CLKGATE, T6000_PS_ON, 2, T6000_PS_CLKGATE, T6000_PS_ON), + T6000_PSW(0, T6000_PS_ON, T6000_PS_ON, 2, T6000_PS_ON, T6000_PS_ON), + T6000_PSW(0, T6000_PS_CLKGATE, T6000_PS_CLKGATE, 3, T6000_PS_CLKGATE, T6000_PS_OFF), }; static struct t6000_display { + void __iomem *pmgr_ps; void __iomem *east_ps; void __iomem *afr_ps; void __iomem *afr_cluster; void __iomem *afr_gate; void __iomem *gfx_blk; - void __iomem *disp0_blk; - /* The PMP report and disp0's entry in it */ + void __iomem *disp_blk[T6000_DISP_BLOCKS]; + /* Their setups, from the device tree */ + struct apple_tunable *gfx_setup; + struct apple_tunable *disp_setup[T6000_DISP_BLOCKS]; + /* The PMP report and each display block's entry in it */ struct device_node *pmp_report; - struct device_node *pmp_disp0; + struct device_node *pmp_entry[T6000_DISP_BLOCKS]; /* - * What the last .display_off changed, for t6000_sleep_display_undo(). The AFR cluster - * word as it was is the profile's t6000_sleep.afr_cl_pre: its quiesce restore after a - * wake uses it too. + * What the last lowering changed, for t6000_sleep_display_undo(): the PMP requests it + * dropped that were up. The AFR cluster word as it was is the profile's + * t6000_sleep.afr_cl_pre: its quiesce restore after a wake uses it too. */ - bool agx_was_requested; - bool disp0_was_requested; + u64 ptd_dropped; bool undo_due; - /* disp0's entry is dropped here, not by the PMP's own suspend */ - bool disp0_owned; - /* disp0 went down in this attempt: a sleep given up then raises it again */ - bool disp0_cycled; + /* The blocks whose entry is dropped here, not by the PMP's own suspend */ + unsigned long owned; + /* The blocks that went down in this attempt: a sleep given up then raises them again */ + unsigned long cycled; + /* The blocks whose display coprocessor its driver has parked, until it starts it again */ + unsigned long parked; /* For the report */ u32 gfx_stuck, disp_stuck; - u32 gfx_seen[4], disp_read[2]; + u32 gfx_seen[4], disp_read[2 * T6000_DISP_BLOCKS]; } t6000_display; static void __iomem *t6000_display_ioremap(struct apple_sleep *s, phys_addr_t pa, size_t len) @@ -136,35 +130,125 @@ static void __iomem *t6000_display_ioremap(struct apple_sleep *s, phys_addr_t pa return base; } +/* + * A fabric block, the sleep node's reg @name, and its setup, apple,tunable-@name: bounded by + * that reg. Without either the profile does not probe, as no block is written without its + * setup. + */ +static void __iomem *t6000_fabric_map(struct apple_sleep *s, const char *name, + struct apple_tunable **setup) +{ + struct resource *res; + char prop[40]; + size_t i; + + res = platform_get_resource_byname(to_platform_device(s->dev), IORESOURCE_MEM, name); + if (!res) { + dev_err(s->dev, "T6000: no %s reg\n", name); + return NULL; + } + snprintf(prop, sizeof(prop), "apple,tunable-%s", name); + *setup = devm_apple_tunable_parse(s->dev, s->dev->of_node, prop, res); + if (IS_ERR(*setup)) { + dev_err(s->dev, "T6000: %s: %pe\n", prop, *setup); + *setup = NULL; + return NULL; + } + for (i = 0; i < (*setup)->sz; i++) { + if ((*setup)->values[i].mask != U32_MAX) { + dev_err(s->dev, "T6000: %s: entry %zu is not a whole word\n", prop, i); + *setup = NULL; + return NULL; + } + } + return t6000_display_ioremap(s, res->start, resource_size(res)); +} + +/* + * A fabric block's setup, in the list's order, each word written as listed. Nothing in the + * block is read: it is at reset, and a read there is not part of the sequence that is known + * to work while the block powers down. apple_tunable_apply() would read each word first and + * skip the ones that already hold the value, among them the stage words written again. + */ +static void t6000_fabric_setup(void __iomem *blk, const struct apple_tunable *setup) +{ + size_t i; + + for (i = 0; i < setup->sz; i++) + writel(setup->values[i].value, blk + setup->values[i].offset); +} + static int t6000_display_map(struct apple_sleep *s) { struct t6000_display *d = &t6000_display; struct device_node *np; + unsigned int b; u32 id; - d->east_ps = t6000_display_ioremap(s, T6000_DISP_PS_EAST, T6000_DISP_PS_EAST_LEN); - d->afr_ps = t6000_display_ioremap(s, T6000_AFR_PS, 4); - d->afr_cluster = t6000_display_ioremap(s, T6000_AFR_CLUSTER_PS, 4); + /* Inside the sleep node's "ps" and "ps-east", mapped by the profile already */ + d->pmgr_ps = s->ps + (T6000_DISP_PS_PMGR - T6000_PS_BASE); + d->east_ps = t6000_sleep.ps_east + (T6000_DISP_PS_EAST - T6000_PS_EAST_BASE); + d->afr_ps = s->ps + (T6000_AFR_PS - T6000_PS_BASE); + d->afr_cluster = s->ps + (T6000_AFR_CLUSTER_PS - T6000_PS_BASE); d->afr_gate = t6000_display_ioremap(s, T6000_AFR_GATE, 4); - d->gfx_blk = t6000_display_ioremap(s, T6000_GFX_BLK, T6000_FABRIC_BLK_LEN); - d->disp0_blk = t6000_display_ioremap(s, T6000_DISP0_BLK, T6000_FABRIC_BLK_LEN); - if (!d->east_ps) + d->gfx_blk = t6000_fabric_map(s, "gfx-fabric", &d->gfx_setup); + if (!d->gfx_blk) + return -ENODEV; + for (b = 0; b < T6000_DISP_BLOCKS; b++) { + d->disp_blk[b] = t6000_fabric_map(s, t6000_disp_blocks[b].fabric, + &d->disp_setup[b]); + if (!d->disp_blk[b]) + return -ENODEV; + } + if (!d->pmgr_ps || !d->east_ps) return -ENOMEM; /* Kept for good, as the report is */ d->pmp_report = of_find_compatible_node(NULL, NULL, "apple,t6000-pmp-v2-report"); for_each_child_of_node(d->pmp_report, np) { - if (!of_property_read_u32(np, "reg", &id) && id == T6000_PMP_DISP0_ID) { - d->pmp_disp0 = np; - break; - } + if (of_property_read_u32(np, "reg", &id)) + continue; + for (b = 0; b < T6000_DISP_BLOCKS; b++) + if (id == t6000_disp_blocks[b].pmp_id && !d->pmp_entry[b]) + d->pmp_entry[b] = of_node_get(np); } - if (!d->pmp_report || !d->pmp_disp0) - dev_warn(s->dev, "T6000: no PMP report entry for disp0, its request stays up\n"); + for (b = 0; b < T6000_DISP_BLOCKS; b++) + if (!d->pmp_entry[b]) + dev_warn(s->dev, + "T6000: no PMP report entry for %s, not held for the commit\n", + t6000_disp_blocks[b].name); return 0; } +/* A display block's power word, or NULL */ +static void __iomem *t6000_disp_ps(struct t6000_display *d, u64 pa) +{ + if (pa >= T6000_DISP_PS_PMGR && pa < T6000_DISP_PS_PMGR + T6000_DISP_PS_LEN) + return d->pmgr_ps ? d->pmgr_ps + (pa - T6000_DISP_PS_PMGR) : NULL; + if (pa >= T6000_DISP_PS_EAST && pa < T6000_DISP_PS_EAST + T6000_DISP_PS_LEN) + return d->east_ps ? d->east_ps + (pa - T6000_DISP_PS_EAST) : NULL; + return NULL; +} + +/* The display block whose CPU power word is @pd (a PMGR power-state node), or -1 */ +static int t6000_disp_block_of(struct device_node *pd) +{ + struct device_node *parent = of_get_parent(pd); + struct resource res; + unsigned int b; + int found = -1; + u32 off; + + if (parent && !of_address_to_resource(parent, 0, &res) && + !of_property_read_u32(pd, "reg", &off)) + for (b = 0; b < T6000_DISP_BLOCKS; b++) + if (res.start + off == t6000_disp_blocks[b].cpu0) + found = b; + of_node_put(parent); + return found; +} + /* * The GPU's fabric port as macOS leaves it for the sleep: the AFR cluster, AFR and GFX up, the * PMP's GFX request raised so that GFX stays up, the fabric block set up, the request dropped @@ -190,33 +274,36 @@ static void t6000_sleep_gfx_down(struct apple_sleep *s) if (d->afr_cluster) { t6000_sleep.afr_cl_pre = readl(d->afr_cluster); t6000_sleep.afr_cl_valid = true; - writel(0xf, d->afr_cluster); + writel(FIELD_PREP(T6000_AFR_CLUSTER_TARGET, T6000_PS_ON), d->afr_cluster); if (readl_poll_timeout_atomic(d->afr_cluster, v, v & T6000_AFR_CLUSTER_UP, 1, 50000)) d->gfx_stuck |= BIT(6); - writel(0x2f, d->afr_cluster); + writel(T6000_AFR_CLUSTER_UP | FIELD_PREP(T6000_AFR_CLUSTER_TARGET, T6000_PS_ON), + d->afr_cluster); } if (d->afr_gate) - writel(1, d->afr_gate); - writel(0xf040000, d->afr_ps); - writel(0xf04000f, d->afr_ps); - writel(0x1f04000f, d->afr_ps); + writel(T6000_AFR_GATE_EN, d->afr_gate); + /* AFR on, with clock gating as its floor, then auto */ + writel(T6000_PSW(0, T6000_PS_ON, T6000_PS_CLKGATE, 0, 0, T6000_PS_OFF), d->afr_ps); + writel(T6000_PSW(0, T6000_PS_ON, T6000_PS_CLKGATE, 0, 0, T6000_PS_ON), d->afr_ps); + writel(T6000_PSW(1, T6000_PS_ON, T6000_PS_CLKGATE, 0, 0, T6000_PS_ON), d->afr_ps); if (readl_poll_timeout_atomic(d->afr_ps, v, FIELD_GET(PS_ACTUAL, v), 1, 50000)) d->gfx_stuck |= BIT(0); - writel(0xf000000, gfx); - writel(0xf00000f, gfx); - writel(0x1f00000f, gfx); + /* GFX on, then auto */ + writel(T6000_PSW(0, T6000_PS_ON, 0, 0, 0, T6000_PS_OFF), gfx); + writel(T6000_PSW(0, T6000_PS_ON, 0, 0, 0, T6000_PS_ON), gfx); + writel(T6000_PSW(1, T6000_PS_ON, 0, 0, 0, T6000_PS_ON), gfx); /* GFX reaches 4, and the PMP drops it again unless its request is up: raise that first. */ ret = apple_pmp_report_sleep_request(d->pmp_report, T6000_PMP_AGX_ID, true, T6000_PMP_ACK_US, &was); - d->agx_was_requested = !ret && was; + if (was) + d->ptd_dropped |= BIT_ULL(T6000_PMP_AGX_ID); if (ret) d->gfx_stuck |= BIT(2); /* The fabric block is in the GFX domain: never write it unpowered. */ if (readl_poll_timeout_atomic(gfx, v, FIELD_GET(PS_ACTUAL, v), 1, 50000)) { d->gfx_stuck |= BIT(1); } else { - for (j = 0; j < ARRAY_SIZE(t6000_gfx_init); j++) - writel(t6000_gfx_init[j][1], d->gfx_blk + t6000_gfx_init[j][0]); + t6000_fabric_setup(d->gfx_blk, d->gfx_setup); } if (apple_pmp_report_sleep_request(d->pmp_report, T6000_PMP_AGX_ID, false, T6000_PMP_ACK_US, NULL)) @@ -229,8 +316,9 @@ static void t6000_sleep_gfx_down(struct apple_sleep *s) } else { d->gfx_stuck |= BIT(7); } - writel(0xf0000ff, gfx); - writel(0xf0003f0, gfx); + /* GFX off: auto off, then power-gated with the latched flags cleared */ + writel(T6000_PSW(0, T6000_PS_ON, 0, 0, T6000_PS_ON, T6000_PS_ON), gfx); + writel(T6000_PSW(0, T6000_PS_ON, 0, 3, T6000_PS_ON, T6000_PS_OFF), gfx); if (readl_poll_timeout_atomic(gfx, v, !FIELD_GET(PS_ACTUAL, v), 1, 10000)) d->gfx_stuck |= BIT(4); d->gfx_seen[2] = v; @@ -242,85 +330,106 @@ static void t6000_sleep_gfx_down(struct apple_sleep *s) if (d->afr_gate) writel(0, d->afr_gate); if (d->afr_cluster) - writel(0, d->afr_cluster); + writel(FIELD_PREP(T6000_AFR_CLUSTER_TARGET, T6000_PS_OFF), d->afr_cluster); dev_dbg(s->dev, "T6000: GFX down: stuck %#x, gfx %#x -> %#x, afr %#x -> %#x\n", d->gfx_stuck, d->gfx_seen[0], d->gfx_seen[2], d->gfx_seen[1], d->gfx_seen[3]); } /* - * disp0 in macOS's order: its CPU's power state, then its front end's (hv trace, macOS 13.7.8 - * sleep entry). Before the CPU's: the fabric block's setup and the PMP's disp0 request dropped - * and acknowledged. Before the front end's: the fabric handshake, +0x8014 = 1, +0x82bc = 1, 2, - * 4. The fabric block only answers while the front end has power: a write to it otherwise is - * an SError. + * The display blocks in @blocks, in macOS's order, the GPU's port first if @gfx (hv trace, + * macOS 13.7.8 sleep entry). Per block: the fabric block's setup, the PMP's request for the + * block dropped and acknowledged within 200 ms, as macOS waits, the CPU's power state, the + * fabric handshake (+0x8014 = 1, +0x82bc = 1, 2, 4), then the front end's power state. The + * fabric block only answers while the front end has power: a write to it otherwise is an + * SError. The request word only: the report's books keep the block on, as the PMP's wake + * rewrites them. */ -static void t6000_sleep_disp0_down(struct apple_sleep *s) +static void t6000_sleep_disp_down(struct apple_sleep *s, unsigned long blocks, bool gfx) { struct t6000_display *d = &t6000_display; - void __iomem *cpu0 = d->east_ps + T6000_DISP0_CPU0_PS; - void __iomem *fe = d->east_ps + T6000_DISP0_FE_PS; - void __iomem *blk = d->disp0_blk; - unsigned int j; + unsigned int k, b; u32 v; + d->ptd_dropped = 0; + d->cycled |= blocks; + if (gfx) + t6000_sleep_gfx_down(s); d->disp_stuck = 0; - d->disp0_was_requested = false; - d->disp0_cycled = true; - - if (blk && !FIELD_GET(PS_ACTUAL, readl(fe))) - blk = NULL; - if (blk) - for (j = 0; j < ARRAY_SIZE(t6000_disp0_init); j++) - writel(t6000_disp0_init[j][1], blk + t6000_disp0_init[j][0]); - /* - * The request word only: the report's books keep disp0 on, as the PMP's wake rewrites - * them. Acknowledged within 200 ms, as macOS waits. - */ - if (d->pmp_disp0) { - bool was = false; + for (k = 0; k < ARRAY_SIZE(t6000_disp_order); k++) { + const struct t6000_disp_block *db; + void __iomem *cpu0, *fe, *blk; - if (apple_pmp_report_sleep_request(d->pmp_report, T6000_PMP_DISP0_ID, false, - T6000_PMP_ACK_US, &was)) - d->disp_stuck |= T6000_DISP0_STUCK_PMP; - d->disp0_was_requested = was; - } - writel(0xf0400ff, cpu0); - writel(0xf0403f0, cpu0); - if (readl_poll_timeout_atomic(cpu0, v, !FIELD_GET(PS_ACTUAL, v), 1, 10000)) - d->disp_stuck |= T6000_DISP0_STUCK_CPU0; - d->disp_read[0] = readl(cpu0); - - if (blk && !FIELD_GET(PS_ACTUAL, readl(fe))) - blk = NULL; - if (blk) { - writel(1, blk + T6000_FABRIC_HS_8014); - writel(1, blk + T6000_FABRIC_HS_82BC); - writel(2, blk + T6000_FABRIC_HS_82BC); - writel(4, blk + T6000_FABRIC_HS_82BC); + b = t6000_disp_order[k]; + if (!(blocks & BIT(b))) + continue; + db = &t6000_disp_blocks[b]; + cpu0 = t6000_disp_ps(d, db->cpu0); + fe = t6000_disp_ps(d, db->fe); + if (!cpu0 || !fe) + continue; + + blk = d->disp_blk[b]; + if (blk && !FIELD_GET(PS_ACTUAL, readl(fe))) + blk = NULL; + if (blk) + t6000_fabric_setup(blk, d->disp_setup[b]); + if (d->pmp_report) { + bool was = false; + + if (apple_pmp_report_sleep_request(d->pmp_report, db->pmp_id, false, + T6000_PMP_ACK_US, &was)) + d->disp_stuck |= BIT(8 + b); + if (was) + d->ptd_dropped |= BIT_ULL(db->pmp_id); + } + /* The CPU off: auto off, then power-gated with the latched flags cleared */ + writel(T6000_PSW(0, T6000_PS_ON, T6000_PS_CLKGATE, 0, T6000_PS_ON, T6000_PS_ON), + cpu0); + writel(T6000_PSW(0, T6000_PS_ON, T6000_PS_CLKGATE, 3, T6000_PS_ON, T6000_PS_OFF), + cpu0); + if (readl_poll_timeout_atomic(cpu0, v, !FIELD_GET(PS_ACTUAL, v), 1, 10000)) + d->disp_stuck |= BIT(2 * b); + d->disp_read[2 * b] = readl(cpu0); + + blk = d->disp_blk[b]; + if (blk && !FIELD_GET(PS_ACTUAL, readl(fe))) + blk = NULL; + if (blk) { + writel(1, blk + T6000_FABRIC_HS_8014); + writel(1, blk + T6000_FABRIC_HS_82BC); + writel(2, blk + T6000_FABRIC_HS_82BC); + writel(4, blk + T6000_FABRIC_HS_82BC); + } + /* The front end power-gated, the latched flags cleared */ + writel(T6000_PSW(0, 0, 0, 3, T6000_PS_ON, T6000_PS_OFF), fe); + if (readl_poll_timeout_atomic(fe, v, !FIELD_GET(PS_ACTUAL, v), 1, 10000)) + d->disp_stuck |= BIT(2 * b + 1); + d->disp_read[2 * b + 1] = readl(fe); } - writel(0x3f0, fe); - if (readl_poll_timeout_atomic(fe, v, !FIELD_GET(PS_ACTUAL, v), 1, 10000)) - d->disp_stuck |= T6000_DISP0_STUCK_FE; - d->disp_read[1] = readl(fe); - dev_dbg(s->dev, "T6000: disp0 down: stuck %#x, cpu0 %#x, fe %#x\n", d->disp_stuck, - d->disp_read[0], d->disp_read[1]); + dev_dbg(s->dev, "T6000: displays down: stuck %#x, ps %#x %#x %#x %#x %#x %#x\n", + d->disp_stuck, d->disp_read[0], d->disp_read[1], d->disp_read[2], d->disp_read[3], + d->disp_read[4], d->disp_read[5]); } /* - * The GPU's fabric port, then disp0, before SEP and the SMC are told. A display block that - * stays up refuses the sleep: the SoC must never go down around it. + * The GPU's fabric port, then every display block, before SEP and the SMC are told. A display + * block that stays up refuses the sleep: the SoC must never go down around it. */ static int t6000_sleep_display_off(struct apple_sleep *s) { struct t6000_display *d = &t6000_display; + unsigned int b; - if (!d->east_ps) + if (!d->pmgr_ps || !d->east_ps) return -ENODEV; d->undo_due = true; - t6000_sleep_gfx_down(s); - t6000_sleep_disp0_down(s); - if (d->disp_stuck & T6000_DISP0_STUCK) { - dev_err(s->dev, "disp0 did not power down (%#x), not sleeping\n", d->disp_stuck); + t6000_sleep_disp_down(s, T6000_DISP_ALL, true); + if (d->disp_stuck) { + for (b = 0; b < T6000_DISP_BLOCKS - 1; b++) + if (d->disp_stuck & (BIT(2 * b) | BIT(2 * b + 1) | BIT(8 + b))) + break; + dev_err(s->dev, "%s did not power down (%#x), not sleeping\n", + t6000_disp_blocks[b].name, d->disp_stuck); return -EBUSY; } @@ -336,21 +445,20 @@ static int t6000_sleep_display_off(struct apple_sleep *s) static void t6000_sleep_display_undo(struct apple_sleep *s) { struct t6000_display *d = &t6000_display; + unsigned long dropped = d->ptd_dropped; + u64 failed = 0; + unsigned int id; u32 v; if (!d->undo_due) return; d->undo_due = false; - if (d->disp0_was_requested && - apple_pmp_report_sleep_request(d->pmp_report, T6000_PMP_DISP0_ID, true, - T6000_PMP_ACK_US, NULL)) - dev_warn(s->dev, "T6000: PMP did not take the disp0 request back\n"); - d->disp0_was_requested = false; - if (d->agx_was_requested && - apple_pmp_report_sleep_request(d->pmp_report, T6000_PMP_AGX_ID, true, - T6000_PMP_ACK_US, NULL)) - dev_warn(s->dev, "T6000: PMP did not take the GFX request back\n"); - d->agx_was_requested = false; + d->ptd_dropped = 0; + for_each_set_bit(id, &dropped, BITS_PER_LONG) + if (apple_pmp_report_sleep_request(d->pmp_report, id, true, T6000_PMP_ACK_US, NULL)) + failed |= BIT_ULL(id); + if (failed) + dev_warn(s->dev, "T6000: PMP did not take device requests %#llx back\n", failed); if (d->afr_cluster && t6000_sleep.afr_cl_valid) { if ((readl(d->afr_cluster) ^ t6000_sleep.afr_cl_pre) & T6000_AFR_CLUSTER_TARGET) writel(t6000_sleep.afr_cl_pre, d->afr_cluster); @@ -362,22 +470,32 @@ static void t6000_sleep_display_undo(struct apple_sleep *s) } /* - * disp0's request is dropped by t6000_sleep_disp0_down(), in .enter: the PMP's own - * suspend_late must leave it, or disp0's request drops while its coprocessor still runs. - * From the profile's .begin to its .finish. + * The display blocks' requests are dropped by t6000_sleep_disp_down(), in .enter: the PMP's + * own suspend_late must leave them, or a block's request drops while its coprocessor still + * runs. From the profile's .begin to its .finish; a failure leaves none held. */ static int t6000_display_own(struct apple_sleep *s, bool own) { struct t6000_display *d = &t6000_display; + unsigned int b; int ret; - if (!d->pmp_disp0 || d->disp0_owned == own) - return 0; - ret = apple_pmp_report_entry_commit_owned(d->pmp_disp0, own); - if (ret) { - dev_err(s->dev, "T6000: disp0's PMP report entry not held for the commit: %d\n", ret); - return ret; + for (b = 0; b < T6000_DISP_BLOCKS; b++) { + if (!d->pmp_entry[b] || test_bit(b, &d->owned) == own) + continue; + ret = apple_pmp_report_entry_commit_owned(d->pmp_entry[b], own); + if (ret) { + dev_err(s->dev, + "T6000: %s's PMP report entry not held for the commit: %d\n", + t6000_disp_blocks[b].name, ret); + if (own) + t6000_display_own(s, false); + return ret; + } + if (own) + __set_bit(b, &d->owned); + else + __clear_bit(b, &d->owned); } - d->disp0_owned = own; return 0; } diff --git a/drivers/soc/apple/sleep-t6000.h b/drivers/soc/apple/sleep-t6000.h index f7f6ebc248ba2..e16f03e3de9bd 100644 --- a/drivers/soc/apple/sleep-t6000.h +++ b/drivers/soc/apple/sleep-t6000.h @@ -796,7 +796,7 @@ static int t6000_sleep_begin(struct apple_sleep *s) t->cluster_written = 0; t->disp_undo_due = false; t->afr_cl_valid = false; - t6000_display.disp0_cycled = false; + t6000_display.cycled = 0; t6000_ps_late_save(t); t6000_psall_save(t); return t6000_display_own(s, true); @@ -1623,9 +1623,55 @@ static void t6000_psall_restore(struct apple_sleep *s, struct t6000_sleep *t) } } +/* + * Display blocks whose coprocessor its driver parked, and that no .display_off took down, down + * and up: the lowering without the GPU's port, the PMP requests back, then the blocks' CPU and + * front-end words raised to what they were at .begin, block by block. + */ +static void t6000_sleep_display_cycle(struct apple_sleep *s, unsigned long blocks) +{ + struct t6000_display *d = &t6000_display; + struct t6000_sleep *t = &t6000_sleep; + DECLARE_BITMAP(seen, T6000_PSALL_MAX); + unsigned int b, i; + + t6000_sleep_disp_down(s, blocks, false); + d->undo_due = true; + t6000_sleep_display_undo(s); + t->psall_pass = 0; + bitmap_zero(seen, T6000_PSALL_MAX); + for_each_set_bit(b, &blocks, T6000_DISP_BLOCKS) { + const struct t6000_disp_block *db = &t6000_disp_blocks[b]; + + for (i = 0; i < t->psall_n; i++) + if (t->psall[i].pa == db->cpu0) + t6000_psall_on(s, t, i, 0, seen); + for (i = 0; i < t->psall_n; i++) + if (t->psall[i].pa == db->fe) + t6000_psall_on(s, t, i, 0, seen); + } +} + +/* + * A sleep given up after a display coprocessor was parked and before .display_off took its + * block down: the parked firmware starts again only once its block lost power. Done here, at + * .wake, ahead of resume_noirq, which writes the blocks' DARTs back: a block cycled after that + * loses them again under the restarting firmware. + */ +static void t6000_sleep_restart_displays(struct apple_sleep *s) +{ + struct t6000_display *d = &t6000_display; + unsigned long blocks = d->parked & ~d->cycled; + + if (t6000_sleep.committed || !blocks || !d->pmgr_ps || !d->east_ps) + return; + t6000_sleep_display_cycle(s, blocks); +} + /* * At .wake (and again at .end, when each step has already run): the late image put back, the - * display blocks back after a sleep that was given up, then every domain that was on. + * display blocks back after a sleep that was given up, then every domain that was on, then the + * blocks of the parked display coprocessors that the entry did not take down. */ static void t6000_sleep_finish(struct apple_sleep *s) { @@ -1637,52 +1683,52 @@ static void t6000_sleep_finish(struct apple_sleep *s) t6000_sleep_display_undo(s); } t6000_psall_restore(s, t); + t6000_sleep_restart_displays(s); t6000_display_own(s, false); } +/* The display coprocessor of a block parked, or started again, by its driver */ +static void t6000_sleep_display_parked(struct apple_sleep *s, struct device *dev, bool parked) +{ + struct device_node *pd = of_parse_phandle(dev->of_node, "resets", 0); + int b = pd ? t6000_disp_block_of(pd) : -1; + + of_node_put(pd); + if (b < 0) + return; + if (parked) + set_bit(b, &t6000_display.parked); + else + clear_bit(b, &t6000_display.parked); +} + /* - * The internal DCP's driver parks its firmware for a sleep that commits, and a parked firmware - * starts again only once disp0 lost its power. Asked by that driver's resume after a sleep - * that was given up: if .display_off took disp0 down, .finish has raised it again. If not, - * disp0 goes down and up now, as macOS's order takes it down without the GPU's port, with its - * request back and its power words as they were at .begin. + * A display coprocessor's driver parks its firmware for a sleep that commits, and a parked + * firmware starts again only once its display block lost its power. Asked by that driver's + * resume after a sleep that was given up: if .display_off or .wake took the block down, it is + * up again. If not (the sleep was given up in suspend_late or suspend_noirq, where .wake does + * not run), the block goes down and up now. */ static bool t6000_sleep_refusal_cycled(struct apple_sleep *s, struct device_node *pd) { struct t6000_display *d = &t6000_display; struct t6000_sleep *t = &t6000_sleep; - DECLARE_BITMAP(seen, T6000_PSALL_MAX); - static const u32 disp0_ps[] = { T6000_DISP0_CPU0_PS, T6000_DISP0_FE_PS }; - struct device_node *parent; - struct resource res; - unsigned int k, i; - bool disp0; - u32 off; + const struct t6000_disp_block *db; + int b; - if (!pd || t->committed || !d->east_ps) + if (!pd || t->committed || !d->pmgr_ps || !d->east_ps) return false; - parent = of_get_parent(pd); - disp0 = parent && !of_address_to_resource(parent, 0, &res) && - res.start == T6000_DISP_PS_EAST && !of_property_read_u32(pd, "reg", &off) && - off == T6000_DISP0_CPU0_PS; - of_node_put(parent); - if (!disp0) + b = t6000_disp_block_of(pd); + if (b < 0) return false; - if (d->disp0_cycled) + if (test_bit(b, &d->cycled)) return true; - t6000_sleep_disp0_down(s); - d->undo_due = true; - t6000_sleep_display_undo(s); - t->psall_pass = 0; - bitmap_zero(seen, T6000_PSALL_MAX); - for (k = 0; k < ARRAY_SIZE(disp0_ps); k++) - for (i = 0; i < t->psall_n; i++) - if (t->psall[i].pa == T6000_DISP_PS_EAST + disp0_ps[k]) - t6000_psall_on(s, t, i, 0, seen); + db = &t6000_disp_blocks[b]; + t6000_sleep_display_cycle(s, BIT(b)); - return !(d->disp_stuck & T6000_DISP0_STUCK_CPU0) && - FIELD_GET(PS_ACTUAL, readl(d->east_ps + T6000_DISP0_CPU0_PS)); + return !(d->disp_stuck & BIT(2 * b)) && + FIELD_GET(PS_ACTUAL, readl(t6000_disp_ps(d, db->cpu0))); } /* At .end: the PTD sleep word back */ @@ -1735,6 +1781,7 @@ static const struct apple_sleep_ops t6000_sleep_ops = { .finish = t6000_sleep_finish, .end = t6000_sleep_end, .refusal_cycled = t6000_sleep_refusal_cycled, + .display_parked = t6000_sleep_display_parked, .cpu_prepare = t6000_sleep_cpu_prepare, .cpu_restore = t6000_sleep_cpu_restore, }; diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index cfd39632a1d92..2ebee084101f2 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -635,6 +635,11 @@ struct apple_sleep_ops { * since a refused entry began, which the SoC may do on the asking driver's behalf. */ bool (*refusal_cycled)(struct apple_sleep *s, struct device_node *pd); + /* + * Optional: a display coprocessor's driver parked its firmware for the sleep (@parked), + * or started it again, as it tells apple_sleep_display_quiesced(). + */ + void (*display_parked)(struct apple_sleep *s, struct device *dev, bool parked); /* Pure bounded planning only; no register read/write or entry authorization. */ int (*plan_complex_request)(u32 observed, u32 *request); @@ -2415,7 +2420,11 @@ void apple_sleep_display_quiesced(struct device *dev, bool quiesced) struct apple_sleep *s = apple_sleep; unsigned int i; - if (!s || !s->soc->regions) + if (!s) + return; + if (s->soc->ops->display_parked) + s->soc->ops->display_parked(s, dev, quiesced); + if (!s->soc->regions) return; for (i = 0; i < s->soc->ndisp; i++) if (s->display[i].np == dev->of_node) From f7b1c0ecf8fbac89f5996b9063150e830e098d0c Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Wed, 2 Dec 2026 19:52:20 +0100 Subject: [PATCH 53/72] soc: apple: sleep: add a step right after the first coprocessor stops A coprocessor registered with apple_sleep_coproc_register_first() is stopped by its own driver in the commit's first pass, past the last refusal. On a SoC whose sleep takes that coprocessor's port down with its own sequence, the port has to stay up until that stop has run, and nothing that early can refuse any more. Add an optional first_stopped step, called right after the first pass of coprocessor stops in the early_context entry, for a SoC without display_stop. It can't refuse. No SoC sets it yet; the T6000 profile will take the GPU's fabric port down there. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/sleep.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 2ebee084101f2..a5a04f06028d8 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -650,6 +650,12 @@ struct apple_sleep_ops { 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); + /* + * Optional, for a SoC with early_context and no display_stop: right after the first + * pass of coprocessor stops, past the last refusal. A port that a coprocessor's own + * stop leaves up goes down here. It can't refuse. + */ + void (*first_stopped)(struct apple_sleep *s); /* After cpu_suspend saved context, before any stopped core loses power. */ void (*commit_begin)(struct apple_sleep *s); int (*check)(struct apple_sleep *s); @@ -6992,8 +6998,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->ops->display_stop) + if (!s->soc->ops->display_stop) { apple_sleep_coprocs_stop(s, true); + if (s->soc->ops->first_stopped) + s->soc->ops->first_stopped(s); + } /* Its driver's stop ran just above; past the last refusal, a failure goes on. */ if (late_display) { ret = s->soc->ops->display_off(s); From bd4da8afa356364311178733a28cb5135e579fb0 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Wed, 2 Dec 2026 19:52:58 +0100 Subject: [PATCH 54/72] soc: apple: sleep: T6000: take the GPU port down past the last refusal The T6000 took the GPU's fabric port down at the start of .display_off, before the display blocks, as macOS does. The GPU's firmware can only be stopped at the commit, by its driver, in the commit's first pass of coprocessor stops: with the port down in .display_off, every refusal after it (a display block that stays up, the coprocessors' last checks, the SMC's keys) would leave GFX cycled under a firmware that still runs. Until the GPU's driver has that stop, it refuses a deep sleep at .prepare, so no sleep gets as far as .display_off with the firmware running. Take the port down in the first_stopped step instead: right after the commit's first pass of coprocessor stops, and still before the wake is armed, slpa, the PMP's stop and the SoC's last words. .display_off now takes down only the display blocks. The sequence itself is unchanged. Since genpd holds GFX, and so AFR, on across a sleep whose commit stops the GPU's firmware and writes neither word again at the wake, the GFX and AFR words are read right before the port goes down and written back at .wake, AFR first, after every domain that was on at .begin and before any device resumes, as the T8112 profile does with its GFX word. A sleep that never reached the commit writes nothing. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/sleep-t6000-display.inc | 76 +++++++++++++++++++---- drivers/soc/apple/sleep-t6000.h | 10 ++- 2 files changed, 70 insertions(+), 16 deletions(-) diff --git a/drivers/soc/apple/sleep-t6000-display.inc b/drivers/soc/apple/sleep-t6000-display.inc index 448c6a52db9df..debeb4e895235 100644 --- a/drivers/soc/apple/sleep-t6000-display.inc +++ b/drivers/soc/apple/sleep-t6000-display.inc @@ -1,10 +1,10 @@ /* SPDX-License-Identifier: GPL-2.0-only OR MIT */ /* - * T6000 (M1 Pro): the display blocks and the GPU's fabric port taken down for a deep sleep, in - * the order macOS 13.7.8 takes them down at sleep entry, while the PMP still runs. - * Included by the T6000 profile, which calls t6000_display_map() from its .map_regs, - * t6000_sleep_display_off() as its .display_off and t6000_sleep_display_undo() when an entry - * that ran .display_off is given up before the SoC went down. + * T6000 (M1 Pro): the display blocks and the GPU's fabric port taken down for a deep sleep, + * while the PMP still runs. Included by the T6000 profile, which calls t6000_display_map() from + * its .map_regs, t6000_sleep_display_off() as its .display_off, t6000_sleep_display_undo() when + * an entry that ran .display_off is given up before the SoC went down, and + * t6000_sleep_gpu_port_off() as its .first_stopped, past the last refusal. * * Their drivers have stopped the display coprocessors that ran (IDLE, CPU stopped) by then. * Each block is first dropped from the PMP's device requests and acknowledged, then its power @@ -115,6 +115,12 @@ static struct t6000_display { unsigned long cycled; /* The blocks whose display coprocessor its driver has parked, until it starts it again */ unsigned long parked; + /* + * GFX and AFR as the commit found them, before the GPU's port went down, for the wake: + * genpd keeps both on across a sleep whose commit stops the GPU's firmware. + */ + u32 gfx_commit, afr_commit; + bool gpu_port_down; /* For the report */ u32 gfx_stuck, disp_stuck; @@ -336,15 +342,15 @@ static void t6000_sleep_gfx_down(struct apple_sleep *s) } /* - * The display blocks in @blocks, in macOS's order, the GPU's port first if @gfx (hv trace, - * macOS 13.7.8 sleep entry). Per block: the fabric block's setup, the PMP's request for the + * The display blocks in @blocks, in macOS's order (hv trace, macOS 13.7.8 sleep entry). Per + * block: the fabric block's setup, the PMP's request for the * block dropped and acknowledged within 200 ms, as macOS waits, the CPU's power state, the * fabric handshake (+0x8014 = 1, +0x82bc = 1, 2, 4), then the front end's power state. The * fabric block only answers while the front end has power: a write to it otherwise is an * SError. The request word only: the report's books keep the block on, as the PMP's wake * rewrites them. */ -static void t6000_sleep_disp_down(struct apple_sleep *s, unsigned long blocks, bool gfx) +static void t6000_sleep_disp_down(struct apple_sleep *s, unsigned long blocks) { struct t6000_display *d = &t6000_display; unsigned int k, b; @@ -352,8 +358,6 @@ static void t6000_sleep_disp_down(struct apple_sleep *s, unsigned long blocks, b d->ptd_dropped = 0; d->cycled |= blocks; - if (gfx) - t6000_sleep_gfx_down(s); d->disp_stuck = 0; for (k = 0; k < ARRAY_SIZE(t6000_disp_order); k++) { const struct t6000_disp_block *db; @@ -412,8 +416,9 @@ static void t6000_sleep_disp_down(struct apple_sleep *s, unsigned long blocks, b } /* - * The GPU's fabric port, then every display block, before SEP and the SMC are told. A display - * block that stays up refuses the sleep: the SoC must never go down around it. + * Every display block, before the SMC is told. A display block that stays up refuses the sleep: + * the SoC must never go down around it. The GPU's port is not among them: it goes down only + * past the last refusal, see t6000_sleep_gpu_port_off(). */ static int t6000_sleep_display_off(struct apple_sleep *s) { @@ -423,7 +428,7 @@ static int t6000_sleep_display_off(struct apple_sleep *s) if (!d->pmgr_ps || !d->east_ps) return -ENODEV; d->undo_due = true; - t6000_sleep_disp_down(s, T6000_DISP_ALL, true); + t6000_sleep_disp_down(s, T6000_DISP_ALL); if (d->disp_stuck) { for (b = 0; b < T6000_DISP_BLOCKS - 1; b++) if (d->disp_stuck & (BIT(2 * b) | BIT(2 * b + 1) | BIT(8 + b))) @@ -436,6 +441,51 @@ static int t6000_sleep_display_off(struct apple_sleep *s) return 0; } +/* + * The GPU's fabric port, right after the commit's first pass of coprocessor stops, in which the + * GPU's driver stopped its firmware (apple_sleep_coproc_register_first()). macOS takes it down + * before the display blocks; here it waits for the GPU's stop, which comes only once nothing can + * refuse the sleep any more, so a sleep given up before that leaves the GPU running and its port + * as its driver had it. Still before the PMP's stop and the SoC's last words. + */ +static void t6000_sleep_gpu_port_off(struct apple_sleep *s) +{ + struct t6000_display *d = &t6000_display; + + if (!d->east_ps || !d->afr_ps) + return; + d->gfx_commit = readl(d->east_ps + T6000_GFX_PS); + d->afr_commit = readl(d->afr_ps); + d->gpu_port_down = true; + t6000_sleep_gfx_down(s); +} + +/* + * After a sleep whose commit took the GPU's port down: AFR, then GFX, back as the commit found + * them, before any device resumes and before the GPU's driver starts its firmware again. genpd + * counted both on across the sleep and writes neither again; with them on, the media blocks + * under AFR come back through genpd as before. + */ +static void t6000_sleep_gpu_port_back(struct apple_sleep *s) +{ + struct t6000_display *d = &t6000_display; + void __iomem *gfx = d->east_ps + T6000_GFX_PS; + int ret; + + if (!d->gpu_port_down) + return; + d->gpu_port_down = false; + ret = sleep_ps_restore(d->afr_ps, d->afr_commit); + if (!ret) + ret = sleep_ps_restore(gfx, d->gfx_commit); + if (ret) + dev_warn(s->dev, "T6000: GPU port not back after the sleep: gfx %#x (was %#x), afr %#x (was %#x)\n", + readl(gfx), d->gfx_commit, readl(d->afr_ps), d->afr_commit); + else + dev_dbg(s->dev, "T6000: GPU port back: gfx %#x, afr %#x\n", readl(gfx), + readl(d->afr_ps)); +} + /* * An entry given up after t6000_sleep_display_off() and before the SoC went down. A running * PMP keeps a block down until its request is up again; a stopped one gets its requests back diff --git a/drivers/soc/apple/sleep-t6000.h b/drivers/soc/apple/sleep-t6000.h index e16f03e3de9bd..870c461bcf9a5 100644 --- a/drivers/soc/apple/sleep-t6000.h +++ b/drivers/soc/apple/sleep-t6000.h @@ -797,6 +797,7 @@ static int t6000_sleep_begin(struct apple_sleep *s) t->disp_undo_due = false; t->afr_cl_valid = false; t6000_display.cycled = 0; + t6000_display.gpu_port_down = false; t6000_ps_late_save(t); t6000_psall_save(t); return t6000_display_own(s, true); @@ -1635,7 +1636,7 @@ static void t6000_sleep_display_cycle(struct apple_sleep *s, unsigned long block DECLARE_BITMAP(seen, T6000_PSALL_MAX); unsigned int b, i; - t6000_sleep_disp_down(s, blocks, false); + t6000_sleep_disp_down(s, blocks); d->undo_due = true; t6000_sleep_display_undo(s); t->psall_pass = 0; @@ -1670,8 +1671,9 @@ static void t6000_sleep_restart_displays(struct apple_sleep *s) /* * At .wake (and again at .end, when each step has already run): the late image put back, the - * display blocks back after a sleep that was given up, then every domain that was on, then the - * blocks of the parked display coprocessors that the entry did not take down. + * display blocks back after a sleep that was given up, then every domain that was on, the GPU's + * port as the commit found it, then the blocks of the parked display coprocessors that the entry + * did not take down. */ static void t6000_sleep_finish(struct apple_sleep *s) { @@ -1683,6 +1685,7 @@ static void t6000_sleep_finish(struct apple_sleep *s) t6000_sleep_display_undo(s); } t6000_psall_restore(s, t); + t6000_sleep_gpu_port_back(s); t6000_sleep_restart_displays(s); t6000_display_own(s, false); } @@ -1774,6 +1777,7 @@ static const struct apple_sleep_ops t6000_sleep_ops = { .check = t6000_sleep_check, .final_check = t6000_sleep_final_check, .display_off = t6000_display_off, + .first_stopped = t6000_sleep_gpu_port_off, .prepare_regs = t6000_sleep_prepare_regs, .undo_regs = t6000_sleep_undo_regs, .commit_regs = t6000_sleep_commit_regs, From cedc6c1f952fdd0a5e3a7cd2494f0cbe4e145b36 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Mon, 30 Nov 2026 23:52:11 +0100 Subject: [PATCH 55/72] drm/asahi: stop the T6000 GPU firmware at the sleep's commit A deep sleep cuts the power of the SoC, and the GPU's firmware with it. The driver of the G13 GPUs has no firmware sleep, so it refuses such a sleep. On T6000 the firmware can be stopped as main stops the other GPUs for it: at the commit, past the last refusal, and restarted warm after the sleep, its buffers kept. The sleep driver's .prepare notifier only registers the stop (apple_sleep_coproc_register_first()), and the GPU's power domain stays on through the noirq phase, as in a dry run. At the commit, with interrupts off, the firmware gets AP idle and IOP idle over the polled mailbox, receiving goes off and RUN is cleared. The .finish notifier unregisters the stop; after a real sleep it detaches the mailbox and starts the firmware again, after the PMP's restart and before the SMC hears of the wake. A sleep given up before the commit never stopped the firmware, and nothing is done. Without a registered stop the GPU's noirq suspend gives the sleep up. Only a GPU whose hardware data asks for it (T6000) registers. The T6000 sleep profile already takes the GPU's fabric port down only after this stop, in its first_stopped step. Signed-off-by: Andreas Fernandez --- drivers/gpu/drm/asahi/driver.rs | 88 ++++++++++ drivers/gpu/drm/asahi/gpu.rs | 29 ++++ drivers/gpu/drm/asahi/hw/mod.rs | 3 + drivers/gpu/drm/asahi/hw/t600x.rs | 2 + drivers/gpu/drm/asahi/hw/t602x.rs | 1 + drivers/gpu/drm/asahi/hw/t8103.rs | 1 + drivers/gpu/drm/asahi/hw/t8112.rs | 1 + drivers/gpu/drm/asahi/sleep_pd.c | 264 +++++++++++++++++++++++++++++- drivers/gpu/drm/asahi/sleep_pd.h | 5 + 9 files changed, 393 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/asahi/driver.rs b/drivers/gpu/drm/asahi/driver.rs index ddaa10e627c47..971ff1d61110e 100644 --- a/drivers/gpu/drm/asahi/driver.rs +++ b/drivers/gpu/drm/asahi/driver.rs @@ -50,6 +50,9 @@ pub(crate) struct AsahiPlatformDriverData<'bound> { _reg: drm::Registration<'bound, AsahiDrmDriver>, /// The G17 GPU, whose coprocessors must be stopped when the driver is unbound. g17: Option>, + /// A GPU whose firmware is stopped at the commit of a sleep that cuts the SoC's power, as its + /// hardware data says ([`hw::HwConfig::fw_park_for_sleep`]). + legacy: Option>, #[cfg(CONFIG_DRM_ASAHI_G16)] pub(crate) g16: Option>, m3: Option, @@ -288,12 +291,77 @@ 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)), + // Stopped at the sleep's commit: see `sleep_park_at_prepare`. + None if data.legacy.is_some() => 0, None => EBUSY.to_errno(), } } +/// For a sleep that cuts the SoC's power, has the firmware of a GPU whose hardware data asks for +/// it stopped at the sleep's commit, past its last refusal, registered from the sleep's .prepare. +/// The firmware runs until then, and its power domain stays on through the noirq phase, so a +/// sleep given up before the commit leaves it running. +/// +/// # Safety +/// +/// `dev` must be the GPU device registered with `asahi_sleep_fw_park_at_prepare`. +unsafe extern "C" fn sleep_park_at_prepare(dev: *mut bindings::device) { + // SAFETY: Reads a global flag word. + if !unsafe { bindings::pm_suspend_via_firmware() } { + return; + } + // SAFETY: Registered in probe, unregistered with the binding: `dev` is bound, and no sleep + // runs while a probe does, so its probe has completed. + if unsafe { bindings::dev_get_drvdata(dev) }.is_null() { + return; + } + // SAFETY: As above. + let data = unsafe { sleep_data(dev) }; + if let Some(mgr) = data.legacy.as_ref().and_then(|gpu| gpu.manager()) { + // Without a stop registered, the GPU's suspend_noirq gives the sleep up. + let _ = mgr.sleep_park(dev); + } +} + +/// Starts a firmware stopped at the commit [`sleep_park_at_prepare`] armed again, from the sleep's +/// .finish, which follows every .prepare, given up or not: after the PMP's restart and the AOP's +/// wake notice, and before the SMC is told of the wake. After a sleep given up before its commit +/// the firmware is still running, and only the stop is unregistered. +/// +/// # Safety +/// +/// `dev` must be the GPU device registered with `asahi_sleep_fw_park_at_prepare`. +unsafe extern "C" fn sleep_unpark_at_finish(dev: *mut bindings::device) { + // SAFETY: Registered in probe, unregistered with the binding: `dev` is bound, and no sleep + // runs while a probe does, so its probe has completed. + if unsafe { bindings::dev_get_drvdata(dev) }.is_null() { + return; + } + // SAFETY: As above. + let data = unsafe { sleep_data(dev) }; + if let Some(mgr) = data.legacy.as_ref().and_then(|gpu| gpu.manager()) { + mgr.sleep_unpark(dev); + } +} + +/// The same as [`sleep_unpark_at_finish`], which already ran for every sleep that armed the +/// stop: then it finds nothing to do. +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(mgr) = data.legacy.as_ref().and_then(|gpu| gpu.manager()) { + mgr.sleep_unpark(dev); + } + 0 +} + extern "C" { fn asahi_sleep_pd_init(dev: *mut core::ffi::c_void) -> core::ffi::c_int; + fn asahi_sleep_fw_park_at_prepare( + dev: *mut core::ffi::c_void, + park: Option, + unpark: Option, + ) -> core::ffi::c_int; fn asahi_sleep_pd_suspend_noirq(dev: *mut core::ffi::c_void) -> core::ffi::c_int; fn asahi_sleep_pd_suspend_noirq_hold( dev: *mut core::ffi::c_void, @@ -368,6 +436,7 @@ 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), + resume: Some(sleep_resume), suspend_noirq: Some(sleep_suspend_noirq_driver), resume_noirq: Some(sleep_resume_noirq), // SAFETY: All-zero is the absence of every other callback. @@ -527,6 +596,17 @@ impl platform::Driver for AsahiPlatformDriver { } }; gpu.init()?; + let legacy = cfg.fw_park_for_sleep.then(|| gpu.clone()); + if legacy.is_some() { + // SAFETY: `pdev` is being probed, and `sleep_pd_init` gave it the state this takes. + kernel::error::to_result(unsafe { + asahi_sleep_fw_park_at_prepare( + pdev.as_ref().as_raw().cast(), + Some(sleep_park_at_prepare), + Some(sleep_unpark_at_finish), + ) + })?; + } let stats = gpu.stats_snapshot(); let debug_stats = gpu.raw_stats_snapshot(); @@ -558,6 +638,7 @@ impl platform::Driver for AsahiPlatformDriver { _device: reg.device().into(), _reg: reg, g17: None, + legacy, m3: None, #[cfg(CONFIG_DRM_ASAHI_G14_V27_2)] g14_v27_2: None, @@ -603,6 +684,7 @@ impl AsahiPlatformDriver { _device: reg.device().into(), _reg: reg, g17: None, + legacy: None, #[cfg(CONFIG_DRM_ASAHI_G16)] g16: None, m3: None, @@ -639,6 +721,7 @@ impl AsahiPlatformDriver { _device: reg.device().into(), _reg: reg, g17: None, + legacy: None, m3: None, #[cfg(CONFIG_DRM_ASAHI_G14_V27_2)] g14_v27_2: None, @@ -683,6 +766,7 @@ impl AsahiPlatformDriver { _device: reg.device().into(), _reg: reg, g17: None, + legacy: None, m3: Some(backend), #[cfg(CONFIG_DRM_ASAHI_G14_V27_2)] g14_v27_2: None, @@ -766,6 +850,7 @@ impl AsahiPlatformDriver { _device: reg.device().into(), _reg: reg, g17: Some(gpu), + legacy: None, m3: None, #[cfg(CONFIG_DRM_ASAHI_G14_V27_2)] g14_v27_2: None, @@ -815,6 +900,7 @@ impl AsahiPlatformDriver { _device: reg.device().into(), _reg: reg, g17: None, + legacy: None, m3: None, #[cfg(CONFIG_DRM_ASAHI_G14_V27_2)] g14_v27_2: None, @@ -857,6 +943,7 @@ impl AsahiPlatformDriver { _device: reg.device().into(), _reg: reg, g17: None, + legacy: None, m3: None, #[cfg(CONFIG_DRM_ASAHI_G14_V27_2)] g14_v27_2: None, @@ -915,6 +1002,7 @@ impl AsahiPlatformDriver { _device: reg.device().into(), _reg: reg, g17: None, + legacy: None, m3: None, #[cfg(CONFIG_DRM_ASAHI_G14_V27_2)] g14_v27_2: None, diff --git a/drivers/gpu/drm/asahi/gpu.rs b/drivers/gpu/drm/asahi/gpu.rs index 8da8a428ad72a..00c128b1260e9 100644 --- a/drivers/gpu/drm/asahi/gpu.rs +++ b/drivers/gpu/drm/asahi/gpu.rs @@ -610,6 +610,13 @@ pub(crate) trait GpuManager: Send + Sync { /// Release the queue structures of invalidated contexts after firmware reports that all data /// masters are powered off. fn fw_power_off(&self); + /// Have the firmware stopped at the commit of a sleep that cuts the SoC's power, from the + /// sleep's .prepare: see [`hw::HwConfig::fw_park_for_sleep`]. `dev` is the GPU's platform + /// device. + fn sleep_park(&self, dev: *mut kernel::bindings::device) -> Result; + /// Start the firmware stopped at the commit again, from the sleep's .finish; + /// the GPU's resume calls it again and finds nothing to do. + fn sleep_unpark(&self, dev: *mut kernel::bindings::device); } /// Private generic trait for functions that don't need to escape this module. @@ -1681,6 +1688,10 @@ impl GpuManager::ver { extern "C" { fn asahi_min_cpu_capacity() -> u32; + fn asahi_sleep_fw_park(dev: *mut core::ffi::c_void, rtk: *mut core::ffi::c_void) + -> core::ffi::c_int; + fn asahi_sleep_fw_unpark(dev: *mut core::ffi::c_void, rtk: *mut core::ffi::c_void) + -> core::ffi::c_int; } #[versions(AGX)] @@ -2221,6 +2232,24 @@ impl GpuManager for GpuManager::ver { } } + fn sleep_park(&self, dev: *mut kernel::bindings::device) -> Result { + let mut guard = self.rtkit.lock(); + let rtk = guard.as_mut().as_pin_mut().ok_or(ENODEV)?; + // SAFETY: `dev` is the bound GPU platform device and `rtk` its live RTKit, set once at + // probe and kept for the binding, so it outlives the sleep's commit, which uses it with + // every other CPU stopped and interrupts off. + kernel::error::to_result(unsafe { asahi_sleep_fw_park(dev.cast(), rtk.as_raw().cast()) }) + } + + fn sleep_unpark(&self, dev: *mut kernel::bindings::device) { + let mut guard = self.rtkit.lock(); + if let Some(rtk) = guard.as_mut().as_pin_mut() { + // SAFETY: As in `sleep_park`. The helper logs its own failure: the firmware then + // stays stopped until the next sleep that cuts the power. + unsafe { asahi_sleep_fw_unpark(dev.cast(), rtk.as_raw().cast()) }; + } + } + fn fw_power_off(&self) { let mut released = KVec::new(); core::mem::swap(&mut *self.powered_contexts.lock(), &mut released); diff --git a/drivers/gpu/drm/asahi/hw/mod.rs b/drivers/gpu/drm/asahi/hw/mod.rs index aee820abaef56..d38e1145a71cf 100644 --- a/drivers/gpu/drm/asahi/hw/mod.rs +++ b/drivers/gpu/drm/asahi/hw/mod.rs @@ -342,6 +342,9 @@ pub(crate) struct HwConfig { /// What firmware 27.2 needs, on the SoCs the driver starts it on. #[cfg(CONFIG_DRM_ASAHI_G14_V27_2)] pub(crate) v27_2: Option, + /// For a sleep that cuts the SoC's power, the firmware is stopped at the sleep's commit and + /// started again at its .finish, its RTKit kept: T6000 (G13S). + pub(crate) fw_park_for_sleep: bool, } /// Dynamic (fetched from hardware/DT) configuration. diff --git a/drivers/gpu/drm/asahi/hw/t600x.rs b/drivers/gpu/drm/asahi/hw/t600x.rs index 702474e47b1d7..32f147f6bdd49 100644 --- a/drivers/gpu/drm/asahi/hw/t600x.rs +++ b/drivers/gpu/drm/asahi/hw/t600x.rs @@ -134,6 +134,7 @@ pub(crate) const HWCONFIG_T6002: super::HwConfig = HwConfig { sram_size: None, #[cfg(CONFIG_DRM_ASAHI_G14_V27_2)] v27_2: None, + fw_park_for_sleep: false, }; pub(crate) const HWCONFIG_T6001: super::HwConfig = HwConfig { @@ -160,5 +161,6 @@ pub(crate) const HWCONFIG_T6000: super::HwConfig = HwConfig { fast_sensor_mask_alt: [0x9090, 0], fast_die0_sensor_present: 0x03, io_mappings: &iomaps(4, false), + fw_park_for_sleep: true, ..HWCONFIG_T6001 }; diff --git a/drivers/gpu/drm/asahi/hw/t602x.rs b/drivers/gpu/drm/asahi/hw/t602x.rs index 9047e8c609846..069737a4f4af8 100644 --- a/drivers/gpu/drm/asahi/hw/t602x.rs +++ b/drivers/gpu/drm/asahi/hw/t602x.rs @@ -147,6 +147,7 @@ pub(crate) const HWCONFIG_T6022: super::HwConfig = HwConfig { sram_size: Some(0x20000), #[cfg(CONFIG_DRM_ASAHI_G14_V27_2)] v27_2: None, + fw_park_for_sleep: false, }; pub(crate) const HWCONFIG_T6021: super::HwConfig = HwConfig { diff --git a/drivers/gpu/drm/asahi/hw/t8103.rs b/drivers/gpu/drm/asahi/hw/t8103.rs index 62771231e57c0..a4e93cff2291d 100644 --- a/drivers/gpu/drm/asahi/hw/t8103.rs +++ b/drivers/gpu/drm/asahi/hw/t8103.rs @@ -96,6 +96,7 @@ pub(crate) const HWCONFIG: super::HwConfig = HwConfig { writable: WRITABLE_V27_2, active_mccs: None, }), + fw_park_for_sleep: false, }; #[cfg(CONFIG_DRM_ASAHI_G14_V27_2)] diff --git a/drivers/gpu/drm/asahi/hw/t8112.rs b/drivers/gpu/drm/asahi/hw/t8112.rs index 0f99285abc4f6..a32c40dc032af 100644 --- a/drivers/gpu/drm/asahi/hw/t8112.rs +++ b/drivers/gpu/drm/asahi/hw/t8112.rs @@ -109,6 +109,7 @@ pub(crate) const HWCONFIG: super::HwConfig = HwConfig { writable: WRITABLE_V27_2, active_mccs: None, }), + fw_park_for_sleep: false, }; #[cfg(CONFIG_DRM_ASAHI_G14_V27_2)] diff --git a/drivers/gpu/drm/asahi/sleep_pd.c b/drivers/gpu/drm/asahi/sleep_pd.c index 059269f84c847..966f77ea74954 100644 --- a/drivers/gpu/drm/asahi/sleep_pd.c +++ b/drivers/gpu/drm/asahi/sleep_pd.c @@ -14,23 +14,54 @@ * Register a temporary GENPD_NOTIFY_PRE_OFF veto notifier on the GPU device's * own genpd for the noirq sleep window, matching pcie-apple's consumer-side * hold without changing any other power domain. + * + * Also stop the GPU firmware for a deep sleep, on the GPUs whose driver has no + * firmware sleep of its own (T6000), at the sleep's commit, and start it again from the + * sleep's .finish: see asahi_sleep_fw_park_at_prepare(). */ #include #include #include #include +#include #include #include +#include #include +#include +#include #include #include #include "sleep_pd.h" +/* The ASC's CPU control word, and its status word after it. */ +#define ASAHI_ASC_CPU_CONTROL 0x44 +#define ASAHI_ASC_CPU_SIZE 8 +#define ASAHI_ASC_CPU_RUN BIT(4) +/* The AP's ON after the endpoints are started again, as for the display coprocessors */ +#define ASAHI_FW_AP_ON_MS 2000 + struct asahi_sleep_pd { struct notifier_block nb; bool active; + void __iomem *cpu_control; + /* Its stop registered for this sleep's commit, with the RTKit it stops */ + struct apple_rtkit *rtk; + bool armed; + /* The commit ran that stop, and what the firmware answered to its idle */ + bool commit_ran; + int commit_ret; + /* It did not start again: only the next deep sleep's power cut brings it back. */ + bool stopped; + /* Parks it from the sleep's .prepare: see asahi_sleep_fw_park_at_prepare() */ + struct notifier_block prepare_nb; + struct device *dev; + void (*park)(struct device *dev); + /* And starts it again from the sleep's .finish */ + struct notifier_block finish_nb; + void (*unpark)(struct device *dev); }; /* IRQ-safe domains invoke power notifiers in atomic context. */ @@ -46,10 +77,20 @@ static void asahi_sleep_pd_release(struct device *dev, void *res) { struct asahi_sleep_pd *hold = res; + if (hold->park) { + apple_sleep_prepare_notifier_unregister(&hold->prepare_nb); + apple_sleep_finish_notifier_unregister(&hold->finish_nb); + } + if (hold->armed) { + apple_sleep_coproc_unregister(dev); + hold->armed = false; + } if (hold->active) { dev_pm_genpd_remove_notifier(dev); hold->active = false; } + if (hold->cpu_control) + iounmap(hold->cpu_control); } int asahi_sleep_pd_init(struct device *dev) @@ -70,6 +111,16 @@ int asahi_sleep_pd_suspend_noirq_hold(struct device *dev, bool hold_firmware) struct asahi_sleep_pd *hold; int ret; + hold = devres_find(dev, asahi_sleep_pd_release, NULL, NULL); + /* + * A GPU whose firmware the sleep's commit stops: it runs until then, so its domain must + * not lose power before. Without a stop registered, the sleep must not go on. + */ + if (pm_suspend_via_firmware() && hold && hold->park) { + if (!hold->armed) + return -EBUSY; + hold_firmware = true; + } if (pm_suspend_via_firmware() && !hold_firmware) return 0; @@ -96,7 +147,6 @@ int asahi_sleep_pd_suspend_noirq_hold(struct device *dev, bool hold_firmware) if (!dev_pm_genpd_is_on(dev)) return 0; - hold = devres_find(dev, asahi_sleep_pd_release, NULL, NULL); if (WARN_ON_ONCE(!hold)) return -EBUSY; @@ -120,6 +170,218 @@ int asahi_sleep_pd_suspend_noirq(struct device *dev) return asahi_sleep_pd_suspend_noirq_hold(dev, false); } +static struct asahi_sleep_pd *asahi_sleep_pd_get(struct device *dev) +{ + return devres_find(dev, asahi_sleep_pd_release, NULL, NULL); +} + +static void __iomem *asahi_sleep_cpu_control(struct device *dev, struct asahi_sleep_pd *hold) +{ + struct resource *res; + + if (hold->cpu_control) + return hold->cpu_control; + res = platform_get_resource_byname(to_platform_device(dev), IORESOURCE_MEM, "asc"); + if (!res || resource_size(res) < ASAHI_ASC_CPU_CONTROL + ASAHI_ASC_CPU_SIZE) + return NULL; + /* As the bus asks: a posted access to the ASC ends in an SError on these SoCs */ + if (res->flags & IORESOURCE_MEM_NONPOSTED) + hold->cpu_control = ioremap_np(res->start + ASAHI_ASC_CPU_CONTROL, + ASAHI_ASC_CPU_SIZE); + else + hold->cpu_control = ioremap(res->start + ASAHI_ASC_CPU_CONTROL, ASAHI_ASC_CPU_SIZE); + return hold->cpu_control; +} + +/* + * The sleep's commit, past its last refusal: interrupts off, the other CPUs stopped, nothing + * else running. AP idle and IOP idle over the polled mailbox, as at a park, then receiving off + * and RUN cleared. The mailbox is detached at .finish, after the wake, as nothing here may + * sleep. One that did not start again after an earlier sleep has nothing to be told and is + * stopped as it is. A failed idle can't refuse the sleep any more: the firmware is stopped all + * the same and is not started again after the wake. + */ +static int asahi_sleep_fw_commit_stop(void *data) +{ + struct asahi_sleep_pd *hold = data; + unsigned int us = 0; + int ret = 0; + + hold->commit_ran = true; + if (!hold->stopped) { + apple_rtkit_polled_begin(hold->rtk); + ret = apple_rtkit_polled_ap_idle(hold->rtk, &us); + if (!ret) + ret = apple_rtkit_polled_iop_idle(hold->rtk, &us); + apple_rtkit_polled_end(hold->rtk); + } + apple_rtkit_mailbox_recv_off(hold->rtk); + writel_relaxed(readl_relaxed(hold->cpu_control) & ~ASAHI_ASC_CPU_RUN, hold->cpu_control); + hold->commit_ret = ret; + return ret; +} + +/** + * asahi_sleep_fw_park() - Have the GPU firmware stopped at the deep sleep's commit + * @dev: the GPU + * @rtk: its RTKit + * + * From the sleep's .prepare. The firmware keeps running, and its power domain stays on through + * the noirq phase. Its stop is registered with the sleep driver as one that runs first at the + * commit, after the last refusal (apple_sleep_coproc_register_first()), and the GPU's port goes + * down only after it. So a sleep given up anywhere before the commit, in a dry run too, leaves + * the firmware running and has nothing to start again. + * + * Return: 0, or the error with which nothing is registered: the GPU's suspend_noirq then gives + * the sleep up, before its domain could lose power under the running firmware. + */ +int asahi_sleep_fw_park(struct device *dev, struct apple_rtkit *rtk) +{ + struct asahi_sleep_pd *hold = asahi_sleep_pd_get(dev); + int ret; + + if (!hold) + return -ENODEV; + if (hold->armed) + return 0; + if (!asahi_sleep_cpu_control(dev, hold)) + return -ENODEV; + hold->rtk = rtk; + hold->commit_ran = false; + hold->commit_ret = 0; + ret = apple_sleep_coproc_register_first(dev, asahi_sleep_fw_commit_stop, hold); + if (ret) { + dev_err(dev, "GPU firmware can't be stopped at the sleep's commit: %d\n", ret); + return ret; + } + apple_sleep_coproc_set_ready(dev, true); + hold->armed = true; + return 0; +} + +static int asahi_sleep_prepare_notify(struct notifier_block *nb, unsigned long action, void *data) +{ + struct asahi_sleep_pd *hold = container_of(nb, struct asahi_sleep_pd, prepare_nb); + + hold->park(hold->dev); + return NOTIFY_OK; +} + +static int asahi_sleep_finish_notify(struct notifier_block *nb, unsigned long action, void *data) +{ + struct asahi_sleep_pd *hold = container_of(nb, struct asahi_sleep_pd, finish_nb); + + hold->unpark(hold->dev); + return NOTIFY_OK; +} + +/** + * asahi_sleep_fw_park_at_prepare() - Have the GPU firmware stopped at the sleep's commit + * @dev: the GPU, being probed + * @park: registers the stop at the sleep's .prepare, through asahi_sleep_fw_park() with its + * RTKit + * @unpark: starts it again, or leaves it running, through asahi_sleep_fw_unpark() with its RTKit + * + * The sleep's .finish, which follows every .prepare, given up or not, starts a firmware stopped + * at the commit again: after the PMP's restart and the AOP's wake notice, before the SMC is told + * of the wake. Unregistered with the GPU's binding. + * + * Return: 0 or a negative error. + */ +int asahi_sleep_fw_park_at_prepare(struct device *dev, void (*park)(struct device *dev), + void (*unpark)(struct device *dev)) +{ + struct asahi_sleep_pd *hold = asahi_sleep_pd_get(dev); + int ret; + + if (!hold) + return -ENODEV; + if (hold->park) + return 0; + hold->dev = dev; + hold->prepare_nb.notifier_call = asahi_sleep_prepare_notify; + hold->finish_nb.notifier_call = asahi_sleep_finish_notify; + hold->park = park; + hold->unpark = unpark; + ret = apple_sleep_finish_notifier_register(&hold->finish_nb); + if (!ret) { + ret = apple_sleep_prepare_notifier_register(&hold->prepare_nb); + if (ret) + apple_sleep_finish_notifier_unregister(&hold->finish_nb); + } + if (ret) + hold->park = NULL; + return ret; +} + +/* + * After the power cut, with the CPU still stopped: the mailbox and handshake afresh with the + * buffers kept, RUN, the IOP's INIT and endpoint map, every endpoint started again, then the + * AP's ON. + */ +static int asahi_sleep_fw_restart(struct apple_rtkit *rtk, void __iomem *ctl) +{ + u32 control = readl_relaxed(ctl); + int ret; + + ret = apple_rtkit_reinit_keep(rtk); + if (ret) + return ret; + writel_relaxed(control | ASAHI_ASC_CPU_RUN, ctl); + ret = apple_rtkit_wake_keep_ap(rtk); + if (ret) + return ret; + ret = apple_rtkit_restart_started_eps(rtk); + if (ret) + return ret; + return apple_rtkit_ap_power_timeout(rtk, true, ASAHI_FW_AP_ON_MS); +} + +/** + * asahi_sleep_fw_unpark() - Start the GPU firmware stopped at the sleep's commit again + * @dev: the GPU + * @rtk: its RTKit + * + * From the sleep's .finish, which follows every sleep that reached its .prepare, given up or + * not, and before the SMC is told of the wake. The stop is unregistered. A sleep given up before + * its commit never ran it: the firmware is running, and nothing is done. After the commit only a + * real sleep comes back here (else the machine restarts): the mailbox is detached and the + * firmware started again with its buffers kept. The GPU's .resume calls it again, and then finds + * nothing to do. + * + * Return: 0, or the error with which the firmware stays stopped. + */ +int asahi_sleep_fw_unpark(struct device *dev, struct apple_rtkit *rtk) +{ + struct asahi_sleep_pd *hold = asahi_sleep_pd_get(dev); + int ret; + + if (!hold || !hold->armed) + return 0; + hold->armed = false; + apple_sleep_coproc_unregister(dev); + if (!hold->commit_ran) { + dev_info(dev, "sleep ended before its commit: GPU firmware left running\n"); + return 0; + } + hold->commit_ran = false; + apple_rtkit_detach_keep(rtk); + if (hold->commit_ret) { + dev_err(dev, "GPU firmware did not idle at the sleep's commit: %d; the next deep sleep starts it\n", + hold->commit_ret); + hold->stopped = true; + return hold->commit_ret; + } + ret = asahi_sleep_fw_restart(rtk, hold->cpu_control); + hold->stopped = ret; + if (ret) + dev_err(dev, "GPU firmware did not start again: %d; the next deep sleep starts it\n", + ret); + else + dev_info(dev, "GPU firmware stopped at the sleep's commit and started again\n"); + return ret; +} + 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 f9ce7f989d069..fdd22ff12ac50 100644 --- a/drivers/gpu/drm/asahi/sleep_pd.h +++ b/drivers/gpu/drm/asahi/sleep_pd.h @@ -5,11 +5,16 @@ #include +struct apple_rtkit; struct device; int asahi_sleep_pd_init(struct device *dev); int asahi_sleep_pd_suspend_noirq(struct device *dev); int asahi_sleep_pd_suspend_noirq_hold(struct device *dev, bool hold_firmware); int asahi_sleep_pd_resume_noirq(struct device *dev); +int asahi_sleep_fw_park(struct device *dev, struct apple_rtkit *rtk); +int asahi_sleep_fw_park_at_prepare(struct device *dev, void (*park)(struct device *dev), + void (*unpark)(struct device *dev)); +int asahi_sleep_fw_unpark(struct device *dev, struct apple_rtkit *rtk); #endif From 1bbf25efde77e8ac482e739a5f1d0ae21508504c Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 01:56:21 +0100 Subject: [PATCH 56/72] drm/apple: T6000: park the external DCPs for deep sleep too The J314s has two external display coprocessors, dcpext0 and dcpext1, and their firmware is started at boot whether a display is attached or not, so it runs on every sleep. A T6000 deep sleep takes dispext0 and dispext1 down as well as disp0, also with nothing attached, as macOS does, and a dcpext firmware still running then does not start again. Park a running dcpext0 or dcpext1 firmware the way the internal one is parked, in the late suspend, and restart it warm at resume. The new entry keeps what the generic apple,dcpext entry these nodes matched before set (external_processor, two DP ports). T6001 and T6002 share the compatible and match it too; without a sleep description they never park. dcp->external, which keeps an external processor off this path, is set only for the T6030's. A dcpext firmware that never started is left alone and its block goes down cold. A dcpext whose firmware timed out powering on now refuses a deep sleep too, as the internal one does. Signed-off-by: Andreas Fernandez --- drivers/gpu/drm/apple/dcp-internal.h | 4 ++-- drivers/gpu/drm/apple/dcp.c | 21 ++++++++++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index 139c39635ea01..e4af9ec5d3f47 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -434,8 +434,8 @@ struct apple_dcp_hw_data { /* An external display processor (a dcpext block), not the internal display's */ bool external_processor; /* - * With 13.5 firmware on the internal processor: a deep sleep, which cuts the display - * block's power, parks the firmware (A500, IDLE, CPU stopped, RTKit buffers kept) in + * With 13.5 firmware: a deep sleep, which cuts the display block's power, parks a + * running firmware (A500, IDLE, CPU stopped, RTKit buffers kept) in * the late suspend and restarts it warm, see dcp_s2r_park(). */ bool s2r_park; diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 97fd11960e207..97c18ec6fb29e 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -10142,11 +10142,11 @@ static const char *dcp_external_sleep_vetoed(struct apple_dcp *dcp) } /* - * hw.s2r_park (T6000's internal DCP, 13.5 firmware): a deep sleep cuts the display block's - * power, and a firmware still running then does not start again. So it is parked after every - * device's suspend, as macOS does it: A500 puts the IOMFB link to sleep, the AP goes IDLE - * where it did, then the IOP, and the CPU stops with the RTKit buffers kept. Not in a dry - * run, which cuts no power. + * hw.s2r_park (T6000's DCPs, 13.5 firmware): a deep sleep cuts the display blocks' power, the + * external ones' too, and a firmware still running then does not start again. So it is parked + * after every device's suspend, as macOS does it: A500 puts the IOMFB link to sleep, the AP + * goes IDLE where it did, then the IOP, and the CPU stops with the RTKit buffers kept. Not in + * a dry run, which cuts no power. */ static bool dcp_s2r_park_selected(struct apple_dcp *dcp) { @@ -13849,6 +13849,16 @@ static const struct apple_dcp_hw_data apple_dcp_hw_t6000 = { .s2r_park = true, }; +/* + * T6000's external DCPs: the generic apple,dcpext entry they matched before, and a running + * firmware parked for a deep sleep as the internal one's + */ +static const struct apple_dcp_hw_data apple_dcp_hw_t6000_dcpext = { + .external_processor = true, + .num_dptx_ports = 2, + .s2r_park = true, +}; + static const struct apple_dcp_hw_data apple_dcp_hw_t6030 = { .iboot_handoff = "apple,t6030-handoff", .num_dptx_ports = 1, @@ -14010,6 +14020,7 @@ static const struct of_device_id of_match[] = { { .compatible = "apple,t6020-dcp", .data = &apple_dcp_hw_t6020, }, { .compatible = "apple,t6020-dcpext", .data = &apple_dcp_hw_t6020_dcpext, }, { .compatible = "apple,t6000-dcp", .data = &apple_dcp_hw_t6000, }, + { .compatible = "apple,t6000-dcpext", .data = &apple_dcp_hw_t6000_dcpext, }, { .compatible = "apple,t8103-dcp", .data = &apple_dcp_hw_t8103, }, { .compatible = "apple,t8103-dcpext", .data = &apple_dcp_hw_t8103_dcpext, }, { .compatible = "apple,t8112-dcp", .data = &apple_dcp_hw_t8112, }, From 44f07e7a0e4d14deb3bcc19eb92d3981b527d410 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 10:01:49 +0100 Subject: [PATCH 57/72] soc: apple: sleep: T6000: check for an interrupt again after the quiesce macOS looks for a raised interrupt once more right after its quiesce, before the memory words are checked and the parked cores are stopped, and gives the sleep up if there is one: it would end the boot core's last WFI at once. The T6000 commit had only the check in .enter, before the enclave, the SMC and the PMP were told. Add an optional profile step right before commit_regs that may still give up, for a SoC that stops its parked cores in commit_regs, and use it on T6000 for its quiesce followed by that check. If an interrupt is pending, the profile puts its quiesce back, the commit releases the parked cores as a refusal does and returns, and the entry returns the error instead of panicking as for a committed sleep that came back. Such a sleep has sent slpa and stopped the PMP: the SMC and the PMP wait for a power cut that never comes, and the NVMe, the PCIe ports and the other cores do not come back from that. So the entry restarts the machine as soon as cpu_suspend() returns, still on the boot core with interrupts off, with the watchdog set to 5 s in case the restart needs the SMC, while the disk is still in order; .wake checks again. Every other SoC has no such step. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/sleep-t6000.h | 32 ++++++++++++--- drivers/soc/apple/sleep.c | 70 ++++++++++++++++++++++++++++++++- 2 files changed, 95 insertions(+), 7 deletions(-) diff --git a/drivers/soc/apple/sleep-t6000.h b/drivers/soc/apple/sleep-t6000.h index 870c461bcf9a5..e1b77595be5fa 100644 --- a/drivers/soc/apple/sleep-t6000.h +++ b/drivers/soc/apple/sleep-t6000.h @@ -1275,17 +1275,15 @@ static void t6000_spmi_restore(struct t6000_sleep *t) } /* - * The commit, from the stopped coprocessors to the mailbox, in macOS's order: quiesceHW, the - * memory controllers' words checked, the parked cores stopped and the boot cluster's target, - * WD1, the late image, the RTC alarm's vector and WD1 again. The shared commit then arms the - * mailbox and starts the S2R timer. Nothing printed, nothing can fail. + * The commit, from the quiesced SoC (t6000_sleep_commit_quiesce()) to the mailbox, in macOS's + * order: the memory controllers' words checked, the parked cores stopped and the boot + * cluster's target, WD1, the late image, the RTC alarm's vector and WD1 again. The shared + * commit then arms the mailbox and starts the S2R timer. Nothing printed, nothing can fail. */ static void t6000_sleep_commit_regs(struct apple_sleep *s) { struct t6000_sleep *t = &t6000_sleep; - t->committed = true; - t6000_quiesce(t); t6000_go_check(t); /* PE_cpu_machine_quiesce: the PMGR has to know each core is down for the sleep */ @@ -1448,6 +1446,27 @@ static void t6000_quiesce_restore(struct t6000_sleep *t) t->afr_cl_valid = false; } +/* + * The commit's first step: the PMGR's sleep entry, from the stopped coprocessors, then the + * interrupt check once more. One raised since .enter's checks would end the last WFI at once, + * with the parked cores not yet stopped: give up there, the quiesce put back first. Nothing + * printed. + */ +static int t6000_sleep_commit_quiesce(struct apple_sleep *s) +{ + struct t6000_sleep *t = &t6000_sleep; + + t->committed = true; + t6000_quiesce(t); + if (read_sysreg(isr_el1) & (PSR_I_BIT | PSR_F_BIT)) { + t6000_quiesce_restore(t); + /* Given up: as before the commit, until .enter restarts the machine */ + t->committed = false; + return -EBUSY; + } + return 0; +} + /* Back on the boot core, the AIC restored, before any other core starts */ static void t6000_sleep_wake(struct apple_sleep *s) { @@ -1780,6 +1799,7 @@ static const struct apple_sleep_ops t6000_sleep_ops = { .first_stopped = t6000_sleep_gpu_port_off, .prepare_regs = t6000_sleep_prepare_regs, .undo_regs = t6000_sleep_undo_regs, + .commit_quiesce = t6000_sleep_commit_quiesce, .commit_regs = t6000_sleep_commit_regs, .wake = t6000_sleep_wake, .finish = t6000_sleep_finish, diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index a5a04f06028d8..4c10578d68562 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -72,6 +72,7 @@ #include #include #include +#include #include #include #include @@ -591,6 +592,14 @@ struct apple_sleep_ops { * mailbox. No protocol/lifecycle duplication here. */ void (*commit_regs)(struct apple_sleep *s); + /* + * Optional, right before commit_regs, for a SoC with commit_stops_cpus and early_context: + * the SoC's quiesce, after which it can still give up, as macOS checks for a raised + * interrupt once more there. Nonzero: it has put its quiesce back; the parked cores are + * released and the entry returns the error, its peers already stopped, and .enter + * restarts the machine. + */ + int (*commit_quiesce)(struct apple_sleep *s); /* Where the wake enters the kernel with the MMU off: cpu_resume, or a stub ending there */ void (*resume)(void); /* Optional, once the mailbox is armed: the last step before the commit word */ @@ -1152,6 +1161,8 @@ struct apple_sleep { } smc; /* This sleep's wake came from the SMC's sources alone. */ bool smc_woke_alone; + /* The commit gave up after the SMC had slpa: only a restart leads back. */ + bool no_return; /* A power word the DRAM-kept wake polls stayed latched: this sleep went deep. */ bool deep_once; /* Each latched word the commit cleared, as it found it */ @@ -6355,6 +6366,20 @@ static int apple_sleep_commit(unsigned long unused) if (early) udelay(SLEEP_EARLY_WFI_SETTLE_US); + if (s->soc->ops->commit_quiesce) { + int ret = s->soc->ops->commit_quiesce(s); + + /* + * Given up with its parked cores still held (the SoC stops them in commit_regs): + * they go back as at a refusal, and the entry returns as an uncommitted one. Its + * peers were told and stopped: .enter restarts the machine. + */ + if (ret) { + s->entry_committed = false; + spin_table_unhold_parked(); + return ret; + } + } if (s->soc->ops->commit_regs) { /* A SoC whose irreversible steps share nothing with the ones below. */ s->soc->ops->commit_regs(s); @@ -7042,6 +7067,8 @@ static int apple_sleep_enter_prepare(struct apple_sleep *s) return 0; } +static void apple_sleep_check_no_return(struct apple_sleep *s); + static int apple_sleep_enter_saved(unsigned long unused) { struct apple_sleep *s = apple_sleep; @@ -7061,17 +7088,34 @@ static int apple_sleep_enter_saved(unsigned long unused) if (ret) return ret; /* cpu_suspend has already saved boot context. Commit never returns. */ - return apple_sleep_commit(0); + ret = apple_sleep_commit(0); + /* Unless it gave up, after slpa: see apple_sleep_check_no_return() */ + s->no_return = true; + return ret; } /* A SoC with early_context: the whole entry runs inside cpu_suspend(), on the saved context. */ static int apple_sleep_enter_early(struct apple_sleep *s) { + u64 resumes = READ_ONCE(s->mbox->resumes); int ret; s->entry_committed = false; s->cntkctl = read_sysreg(cntkctl_el1); ret = cpu_suspend(0, apple_sleep_enter_saved); + /* Back through aura's resume, or it counted one meanwhile: the SoC was down after all */ + if (!ret) { + s->no_return = false; + } else if (s->no_return) { + dcache_inval_poc((unsigned long)s->mbox, (unsigned long)(s->mbox + 1)); + if (READ_ONCE(s->mbox->resumes) != resumes) + s->no_return = false; + } + /* + * Otherwise restart here, still on this core alone with IRQs off: nothing may run on the + * blocks the entry already stopped, as IRQ handlers and the other cores' start would. + */ + apple_sleep_check_no_return(s); if (ret && s->entry_committed) panic("Apple committed sleep returned without physical wake: %d", ret); if (!ret) { @@ -7484,6 +7528,7 @@ static int apple_sleep_begin(suspend_state_t state) } apple_sleep->dry_run = apple_sleep->untested && !READ_ONCE(sleep_untested_commit); + apple_sleep->no_return = false; if (apple_sleep->soc->ops->begin) { ret = apple_sleep->soc->ops->begin(apple_sleep); @@ -7921,12 +7966,35 @@ static bool apple_sleep_smc_woke_alone(struct apple_sleep *s) return any; } +/* + * A sleep given up in the commit leaves the SMC, which has slpa, and the stopped PMP waiting + * for a power cut that never comes. The NVMe, the PCIe ports and the other cores do not come + * back from that, so restart while the disk is still in order rather than run on without one. + */ +static void apple_sleep_check_no_return(struct apple_sleep *s) +{ + if (!s->no_return) + return; + + dev_emerg(s->dev, "deep sleep was given up after the point of no return, restarting\n"); + /* In case the restart needs the SMC */ + if (s->wdt) { + writel(0, s->wdt + WDT_WD1_CTL); + writel(0, s->wdt + WDT_WD1_COUNT); + writel(5 * WDT_TICKS_PER_S, s->wdt + WDT_WD1_BITE); + writel(WDT_WD1_RESET_EN, s->wdt + WDT_WD1_CTL); + } + emergency_restart(); +} + static void apple_sleep_report(void) { struct apple_sleep *s = apple_sleep; const struct apple_sleep_wake_name *names = s->soc->wake_names; int i; + /* Given up in the commit: .enter restarts before this, which is only a second net */ + apple_sleep_check_no_return(s); /* Back in Linux, before any device resumes: a reset after this is in the resume. */ sleep_note(s, 0x7e); if (s->soc->ops->finish) From d2c129cbdc2c38796d07b27edc734c2fe6332b6a Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 10:08:45 +0100 Subject: [PATCH 58/72] mfd: macsmc: T6000: take the wake record at the platform finish On T6000 the SMC's wake record (MBSE and MBSW read) and waka went out in its noirq resume, right after AP ON, before any coprocessor had started again. The SMC needs AP ON before any key, but the record and waka belong after the coprocessors: the T6000 sleep starts the PMP, the AOP, the GPU and the displays again first, and tells the SMC of the wake last. At the noirq resume, send only AP ON on T6000, still before any key. Let a saved-context SMC driver take a callback at the platform finish, which runs after every device's early resume, and take the record, waka and what follows them there. Should that callback not run, the SMC's .complete takes them. Every other SoC keeps the record in the noirq resume. The PM core consumes the dark-wake interrupt the SMC names right after the noirq resume, which on T6000 is now before the SMC has named it. Consume it once more right after the SMC's finish callback, so that a dark wake can still stay dark; anything else pending still makes the wake a full one. Signed-off-by: Andreas Fernandez --- drivers/mfd/macsmc.c | 57 ++++++++++++++++++++++++++------- drivers/soc/apple/sleep.c | 14 ++++++++ include/linux/mfd/macsmc.h | 2 ++ include/linux/soc/apple/sleep.h | 5 +++ 4 files changed, 66 insertions(+), 12 deletions(-) diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 72e1342d76537..0b87f03d4defa 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -1178,6 +1178,20 @@ static int apple_smc_wake_reason_names(const struct apple_smc *smc, u64 record, return len; } +static void apple_smc_wake_ap_on(struct apple_smc *smc) +{ + /* T6000: an idle AP gets every key refused, so AP ON gets three tries. */ + unsigned int tries = smc->sleep_t6000 ? 3 : 1; + int ret; + + do { + ret = apple_smc_ap_power(smc, true); + } while (ret && --tries); + if (ret) + dev_err(smc->dev, "SMC did not take the wake notice: %d\n", ret); + apple_smc_set_ap_notified(smc, false); +} + /* * Returns the first error reading the wake record; *mbsw: whether MBSW was read. With @later, a * failed read logs nothing; the noirq resume logs the record if it reads one. @@ -1189,17 +1203,9 @@ static int apple_smc_notice_on(struct apple_smc *smc, bool *mbsw, bool later) u32 event = 0; int ret, error = 0; - if (!smc->saved_wake) { - /* T6000: an idle AP gets every key refused, so AP ON gets three tries. */ - unsigned int tries = smc->sleep_t6000 ? 3 : 1; - - do { - ret = apple_smc_ap_power(smc, true); - } while (ret && --tries); - if (ret) - dev_err(smc->dev, "SMC did not take the wake notice: %d\n", ret); - apple_smc_set_ap_notified(smc, false); - } + /* T6000: AP ON went out at the noirq resume already */ + if (!smc->saved_wake && !smc->wake_at_finish) + apple_smc_wake_ap_on(smc); ret = apple_smc_read_u32(smc, SMC_KEY(MBSE), &event); if (ret) { @@ -1699,6 +1705,21 @@ static int apple_smc_j813_saved_end(void *data) return apple_smc_sleep_saved_release(data, false); } +/* + * T6000, at the platform finish: the wake record and waka, and what follows them, once the PMP, + * the AOP and the other coprocessors are back, as the sequence that runs on the hardware has + * them. AP ON went out at the noirq resume, before any key. + */ +static void apple_smc_j813_saved_finish(void *data) +{ + struct apple_smc *smc = data; + + if (!smc->wake_at_finish) + return; + apple_smc_sleep_wake(smc, false); + smc->wake_at_finish = false; +} + /* Readiness is a returning operation BEFORE SEP/ANS/PMP stop. A drained * notification remains retained and can cancel only on this side of the boundary. */ @@ -1919,6 +1940,7 @@ static const struct apple_sleep_smc_saved_ops apple_smc_j813_saved_ops = { .keys = apple_smc_sleep_keys, .late_notice = apple_smc_sleep_after_peers, .end = apple_smc_j813_saved_end, + .finish = apple_smc_j813_saved_finish, }; static int apple_smc_prepare_policy(struct apple_smc *smc) @@ -2071,6 +2093,11 @@ static void apple_smc_complete(struct device *dev) struct apple_smc *smc = dev_get_drvdata(dev); apple_sleep_smc_unregister(dev); + /* T6000: a platform finish that did not take the wake record leaves it here */ + if (smc->wake_at_finish) { + apple_smc_sleep_wake(smc, false); + smc->wake_at_finish = false; + } /* A full wake ends the run of dark ones. */ if (!pm_dark_wake()) smc->dark_wakes = smc->radio_dark_wakes = 0; @@ -2220,7 +2247,13 @@ static int apple_smc_resume_noirq(struct device *dev) } if (smc->ap_notified) { apple_smc_set_polled(smc, true); - apple_smc_sleep_wake(smc, false); + if (smc->sleep_t6000) { + /* AP ON before any key; the record at the platform finish */ + apple_smc_wake_ap_on(smc); + smc->wake_at_finish = true; + } else { + apple_smc_sleep_wake(smc, false); + } apple_smc_set_polled(smc, false); } diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 4c10578d68562..c88918d67274e 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -7873,6 +7873,8 @@ static int apple_sleep_late_scope_begin(void) static void apple_sleep_late_scope_finish(void) { + struct apple_sleep *s = apple_sleep; + /* .finish also runs when noirq suspend fails before platform .wake. */ apple_sleep_smc_scope_end(); sleep_v27_scope_end(); @@ -7885,6 +7887,18 @@ static void apple_sleep_late_scope_finish(void) blocking_notifier_call_chain(&sleep_finish_chain, 0, NULL); /* After device early resume, which consumed the ANS receipt. */ sleep_ans_late_release(false); + /* Registered until the SMC's .complete: its wake commands, the coprocessors back */ + scoped_guard(mutex, &apple_sleep_smc_lock) { + if (s && s->smc.saved_ops && s->smc.saved_ops->finish) { + s->smc.saved_ops->finish(s->smc.data); + /* + * The SMC named the wake only now, after the PM core's own pass right after + * the noirq resume: consume its IRQ here, or the wake can never stay dark. + * Anything else pending still makes it a full one. + */ + pm_consume_dark_wake(); + } + } } static int apple_sleep_prepare(void) diff --git a/include/linux/mfd/macsmc.h b/include/linux/mfd/macsmc.h index 1f7e46bd0f0fc..76bb42b0a4b88 100644 --- a/include/linux/mfd/macsmc.h +++ b/include/linux/mfd/macsmc.h @@ -162,6 +162,8 @@ struct apple_smc { * and the AP idle notice follow each other without gaps, and AP ON is tried three times. */ bool sleep_t6000; + /* T6000: AP ON went out at the noirq resume, the wake record waits for .finish */ + bool wake_at_finish; /* 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. */ diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index 96440e75bd94a..5e0d94208a772 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -93,6 +93,11 @@ struct apple_sleep_smc_saved_ops { * the SMC's first wake commands (its wake record, waka). */ void (*woken)(void *data); + /* + * Optional, at the platform .finish, after every device's early resume (the coprocessors + * back): the SMC's wake commands a SoC sends there rather than at the noirq resume. + */ + void (*finish)(void *data); /* Optional, after the irreversible stops: pmVC alone, before late_notice (which then skips it). */ int (*pmvc)(void *data); /* From d62f9bb729ef80030e8384d7c521ef2496584b5d Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 10:18:08 +0100 Subject: [PATCH 59/72] PM: wakeup: Add pm_wakeup_hold() Some platforms reach a point in their suspend from which it cannot be turned around: Apple's T6000 parks its SIO firmware in the late suspend phase, and that firmware only starts again after the power cut of the sleep itself. A wakeup event that aborts the suspend after that point leaves the SIO, and with it DisplayPort sound, stopped until the next sleep. Let such a platform hold wakeup events from that point on: while held, pm_wakeup_pending() returns false, so neither the PM core nor the platform aborts the transition. The events are still counted, so once the platform lets go it sees them and can make the wake a full one. Signed-off-by: Andreas Fernandez --- drivers/base/power/wakeup.c | 20 ++++++++++++++++++++ include/linux/suspend.h | 2 ++ 2 files changed, 22 insertions(+) diff --git a/drivers/base/power/wakeup.c b/drivers/base/power/wakeup.c index e1f6438379631..7355021801559 100644 --- a/drivers/base/power/wakeup.c +++ b/drivers/base/power/wakeup.c @@ -35,6 +35,9 @@ static DEFINE_RAW_SPINLOCK(wakeup_irq_lock); /* If greater than 0 and the system is suspending, terminate the suspend. */ static atomic_t pm_abort_suspend __read_mostly; +/* The transition in progress can no longer be aborted, see pm_wakeup_hold() */ +static bool pm_wakeup_held __read_mostly; + /* * Combined counters of registered wakeup events and wakeup events in progress. * They need to be modified together atomically, so it's better to use one @@ -873,6 +876,9 @@ bool pm_wakeup_pending(void) unsigned long flags; bool ret = false; + if (READ_ONCE(pm_wakeup_held)) + return false; + raw_spin_lock_irqsave(&events_lock, flags); if (events_check_enabled) { unsigned int cnt, inpr; @@ -904,6 +910,20 @@ void pm_system_cancel_wakeup(void) atomic_dec_if_positive(&pm_abort_suspend); } +/** + * pm_wakeup_hold - Keep wakeup events from aborting the transition in progress. + * @hold: Whether to hold them. + * + * For a platform whose suspend cannot be turned around from some point on. + * While held, pm_wakeup_pending() returns false. The events are still + * registered, so the platform sees them once it lets go and can then make + * the sleep a short one. + */ +void pm_wakeup_hold(bool hold) +{ + WRITE_ONCE(pm_wakeup_held, hold); +} + void pm_wakeup_clear(unsigned int irq_number) { raw_spin_lock_irq(&wakeup_irq_lock); diff --git a/include/linux/suspend.h b/include/linux/suspend.h index 2fcb352878229..9ac34a49f2288 100644 --- a/include/linux/suspend.h +++ b/include/linux/suspend.h @@ -514,6 +514,7 @@ static inline bool pm_suspended_storage(void) extern bool pm_wakeup_pending(void); extern void pm_system_wakeup(void); extern void pm_system_cancel_wakeup(void); +extern void pm_wakeup_hold(bool hold); extern void pm_wakeup_clear(unsigned int irq_number); void pm_wakeup_consume_irq(unsigned int irq_number); extern void pm_system_irq_wakeup(unsigned int irq_number); @@ -554,6 +555,7 @@ static inline void ksys_sync_helper(void) {} static inline bool pm_suspended_storage(void) { return false; } static inline bool pm_wakeup_pending(void) { return false; } static inline void pm_system_wakeup(void) {} +static inline void pm_wakeup_hold(bool hold) {} static inline void pm_wakeup_clear(bool reset) {} static inline void pm_wakeup_consume_irq(unsigned int irq_number) {} static inline void pm_system_irq_wakeup(unsigned int irq_number) {} From 2faeb306e2bd6b5e7419a1421e2f7622b7cca015 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 10:19:09 +0100 Subject: [PATCH 60/72] soc: apple: sleep: T6000: hold wakeup events from .prepare on The T6000 stops its AOP at .prepare, and its display coprocessors and SIO park in their late suspend. A sleep given up after that has to undo each of them, and the SIO does not start again without the power cut of the sleep itself. So from there on a wakeup event no longer gives the sleep up: the SoC holds wakeup events (pm_wakeup_hold()) right before those stops, and lets go once the entry returns, at .finish and at .end, whichever comes first. An event that came in between stays pending, and the PM core then makes the wake a full one even when the SMC names a dark one; the release logs it. A dry run stops none of them and holds nothing. Other SoCs leave the flag clear and are unchanged. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/sleep-t6000.h | 5 +++++ drivers/soc/apple/sleep.c | 40 +++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/drivers/soc/apple/sleep-t6000.h b/drivers/soc/apple/sleep-t6000.h index e1b77595be5fa..cdc8c1247547a 100644 --- a/drivers/soc/apple/sleep-t6000.h +++ b/drivers/soc/apple/sleep-t6000.h @@ -1827,6 +1827,11 @@ static const struct apple_sleep_soc t6000_sleep_soc = { .smc_keys_refuse = true, /* slpa and the SMC's AP 0x201 before the PMP's stop, as macOS has them */ .smc_before_peers = true, + /* + * The display coprocessors and the SIO park in suspend_late; the enclave and the GPU + * firmware stop only at the commit, past the last refusal. + */ + .wakeup_hold = true, .boot_mpidr = 0x0, .cpu_count = 8, /* diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index c88918d67274e..967977da5986e 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -925,6 +925,12 @@ struct apple_sleep_soc { * among them, as macOS orders them, instead of after every one of them. */ bool smc_before_peers; + /* + * A coprocessor stopped at .prepare comes back only through the power cut, so from there + * on a wakeup event is held for after the sleep (pm_wakeup_hold()) and the wake is a full + * one, instead of a refusal that would leave it stopped. + */ + bool wakeup_hold; /* Live coprocessors with DRAM buffers require WAKE_SLP_DDR across sleep. */ bool coproc_slp_ddr; /* @@ -1161,6 +1167,8 @@ struct apple_sleep { } smc; /* This sleep's wake came from the SMC's sources alone. */ bool smc_woke_alone; + /* Wakeup events are held (wakeup_hold) from .prepare until the entry returns. */ + bool wakeups_held; /* The commit gave up after the SMC had slpa: only a restart leads back. */ bool no_return; /* A power word the DRAM-kept wake polls stayed latched: this sleep went deep. */ @@ -7094,6 +7102,21 @@ static int apple_sleep_enter_saved(unsigned long unused) return ret; } +/* + * Wakeup events count again. One that came meanwhile stays pending, and the PM core then + * makes the wake a full one (pm_consume_dark_wake()), whatever the SMC calls it. + */ +static void apple_sleep_wakeups_release(struct apple_sleep *s) +{ + if (!s || !s->wakeups_held) + return; + + s->wakeups_held = false; + pm_wakeup_hold(false); + if (pm_wakeup_pending()) + dev_info(s->dev, "full wake: a wakeup event on the way down\n"); +} + /* A SoC with early_context: the whole entry runs inside cpu_suspend(), on the saved context. */ static int apple_sleep_enter_early(struct apple_sleep *s) { @@ -7126,6 +7149,8 @@ static int apple_sleep_enter_early(struct apple_sleep *s) if (s->entry_prepared) apple_sleep_enter_cleanup(s); s->soc->ops->cpu_restore(s->mbox->boot_mpidr); + /* Back up, or never down */ + apple_sleep_wakeups_release(s); return ret; } @@ -7564,6 +7589,7 @@ static int apple_sleep_begin(suspend_state_t state) static void apple_sleep_end(void) { + apple_sleep_wakeups_release(apple_sleep); WRITE_ONCE(sleep_deep_requested, false); sleep_ans_late_release(true); sleep_v27_scope_end(); @@ -7875,6 +7901,7 @@ static void apple_sleep_late_scope_finish(void) { struct apple_sleep *s = apple_sleep; + apple_sleep_wakeups_release(s); /* .finish also runs when noirq suspend fails before platform .wake. */ apple_sleep_smc_scope_end(); sleep_v27_scope_end(); @@ -7940,8 +7967,17 @@ static int apple_sleep_prepare(void) if (s->soc->ops->prepare && pm_suspend_via_firmware()) s->soc->ops->prepare(s); /* - * Last: a coprocessor whose power domain goes off in the noirq phase, with its mailbox, - * stops here, after every device's suspend and before any late one. + * A coprocessor stopped from here on, by the chain below or in its late suspend, may not + * start again without the power cut: a wakeup event from here on is kept for after the + * sleep. Not in a dry run, which stops none of them. + */ + if (s->soc->wakeup_hold && pm_suspend_via_firmware() && !apple_sleep_dry(s)) { + s->wakeups_held = true; + pm_wakeup_hold(true); + } + /* + * Last, after every device's suspend and before any late one: a coprocessor that stops + * here, or registers its stop for the commit. */ if (pm_suspend_via_firmware()) blocking_notifier_call_chain(&sleep_prepare_chain, 0, NULL); From c263886dc07805f65b1beb1316c4155923ba1657 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 13:38:34 +0100 Subject: [PATCH 61/72] soc: apple: sleep: T6000: give up when the last WFI falls through An interrupt already pending when the boot core reaches its last WFI keeps the core from powering down, and the WFI falls through. The T6000 boot core then looped in WFI with its data cache off until the watchdog armed in the commit reset the machine, up to 120 s later and with nothing in the log. Add an optional step for the boot core's last WFI that may return, and use it on T6000: the core's last instructions get a second entry, as the other cores' but for the boot core, which turns its data cache back on and returns -EBUSY when its WFI falls through. The quiesce and the RTC alarm's SPMI vector go back, as after a wake, and the commit returns the error as an uncommitted one: the entry is given up after the point of no return, and .enter restarts the machine with the watchdog set to 5 s. The other cores still wait in WFI for the power to go. Every other SoC has no such step. The boot core's MPIDR copy that the old loop compared against has no reader left and goes. A fast IPI pending at a core's last WFI ends that WFI at once as well, as on T8103. Each WFI of the sleep loop first clears it in IPI_SR, as the T8103 sequence does, so that only another interrupt already pending makes the boot core give the sleep up. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/sleep-t6000.h | 79 ++++++++++++++++++++++++++------- drivers/soc/apple/sleep.c | 25 +++++++++++ 2 files changed, 87 insertions(+), 17 deletions(-) diff --git a/drivers/soc/apple/sleep-t6000.h b/drivers/soc/apple/sleep-t6000.h index cdc8c1247547a..fd03dc14a1fdd 100644 --- a/drivers/soc/apple/sleep-t6000.h +++ b/drivers/soc/apple/sleep-t6000.h @@ -233,7 +233,6 @@ static struct t6000_sleep { /* The PMP, whose stop lets the commit take the memory clock down */ struct device_node *pmp_np; /* The boot core, which takes its WFI last */ - u64 boot_mpidr; /* This attempt */ bool committed, quiesce, pmp_idled; @@ -445,11 +444,22 @@ static void t6000_sleep_cpu_restore(u64 mpidr) * them. The data cache off and L1 written back and invalidated by set and way (the L2 and the * memory cache are the hardware's), the timers off, ACC_OVRD then CYC_OVRD written, then WFI * until the power goes. Uses no stack once the cache is off. Never returns. + * + * int t6000_cpu_sleep(u64 cyc_ovrd, u64 acc_ovrd): the same for the boot core, the last one. + * A pending fast IPI is acknowledged before each WFI, as on T8103. Another interrupt already + * pending keeps the core from powering down and its WFI returns at once: the data cache goes + * back on and it returns -EBUSY rather than wait there for the watchdog. */ asm(" .pushsection .text, \"ax\"\n" " .type t6000_cpu_off, %function\n" "t6000_cpu_off:\n" -" msr daifset, #0xf\n" +" mov x11, #0\n" +" b 0f\n" +" .size t6000_cpu_off, . - t6000_cpu_off\n" +" .type t6000_cpu_sleep, %function\n" +"t6000_cpu_sleep:\n" +" mov x11, #1\n" +"0: msr daifset, #0xf\n" " mrs x3, sctlr_el1\n" " bic x3, x3, #(1 << 2)\n" " msr sctlr_el1, x3\n" @@ -484,13 +494,24 @@ asm(" .pushsection .text, \"ax\"\n" "3: msr s3_5_c15_c6_0, x1\n" /* ACC_OVRD */ " msr s3_5_c15_c5_0, x0\n" /* CYC_OVRD */ " isb\n" -"4: dsb sy\n" +" mov x9, #1\n" +"4: msr s3_5_c15_c1_1, x9\n" /* IPI_SR: a fast IPI ends WFI */ +" dsb sy\n" +" isb\n" " wfi\n" +" cbnz x11, 5f\n" " b 4b\n" -" .size t6000_cpu_off, . - t6000_cpu_off\n" +"5: mrs x3, sctlr_el1\n" +" orr x3, x3, #(1 << 2)\n" +" msr sctlr_el1, x3\n" +" isb\n" +" mov x0, #-16\n" /* -EBUSY */ +" ret\n" +" .size t6000_cpu_sleep, . - t6000_cpu_sleep\n" " .popsection\n" ); void t6000_cpu_off(u64 cyc_ovrd, u64 acc_ovrd); +int t6000_cpu_sleep(u64 cyc_ovrd, u64 acc_ovrd); /* macOS's enableCluster(false): the target to 0; a CPU cluster goes down with its cores */ static void t6000_cluster_write(struct t6000_sleep *t, unsigned int i) @@ -515,27 +536,51 @@ static void t6000_cluster_eacc(struct t6000_sleep *t) /* * On every core before its last WFI: no retention, woken by a reset; IRQ and FIQ modes 0 so - * a wake interrupt still reaches the core. The boot core, the last one, first gives its own - * cluster's target a last chance, as macOS does after it releases the others. + * a wake interrupt still reaches the core. */ +static void t6000_deep_ovrd(u64 *cyc, u64 *acc) +{ + *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; + *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_DISABLE_PIO_ON_WFI | T6000_ACC_DEEP_SLEEP; +} + +/* A stopped core's last WFI; the boot core's is t6000_sleep_boot_sleep() */ static void t6000_deep_wfi_arm(void) { u64 cyc = read_sysreg_s(T6000_CYC_OVRD), acc = read_sysreg_s(T6000_ACC_OVRD); - if ((read_cpuid_mpidr() & MPIDR_HWID_BITMASK) == t6000_sleep.boot_mpidr) - t6000_cluster_eacc(&t6000_sleep); - - 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; - 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_DISABLE_PIO_ON_WFI | T6000_ACC_DEEP_SLEEP; + t6000_deep_ovrd(&cyc, &acc); t6000_cpu_off(cyc, acc); unreachable(); } +static void t6000_quiesce_restore(struct t6000_sleep *t); +static void t6000_spmi_restore(struct t6000_sleep *t); + +/* + * The boot core, the last one, first gives its own cluster's target a last chance, as macOS + * does after it releases the others. Back only if an interrupt already pending ended its WFI: + * the sleep is given up, and the quiesce and the RTC alarm's vector go back first. + */ +static int t6000_sleep_boot_sleep(void) +{ + struct t6000_sleep *t = &t6000_sleep; + u64 cyc = read_sysreg_s(T6000_CYC_OVRD), acc = read_sysreg_s(T6000_ACC_OVRD); + int ret; + + t6000_cluster_eacc(t); + t6000_deep_ovrd(&cyc, &acc); + ret = t6000_cpu_sleep(cyc, acc); + t6000_quiesce_restore(t); + t6000_spmi_restore(t); + return ret; +} + /* The late image touches SIO's CPU, mailbox and DARTs only while SIO's CPU word is on. */ static bool t6000_ps_late_sio_pa(u64 pa) { @@ -733,7 +778,6 @@ static int t6000_sleep_map_regs(struct apple_sleep *s) t->pmp_np = of_find_compatible_node(NULL, NULL, "apple,t6000-pmp-v2"); if (t->pmp_np && devm_add_action_or_reset(s->dev, apple_sleep_put_node, t->pmp_np)) return -ENOMEM; - t->boot_mpidr = s->soc->boot_mpidr; ret = apple_sleep_read_wake_sources(s); if (ret) @@ -1784,6 +1828,7 @@ static const struct apple_sleep_gate_coproc t6000_gate_coprocs[] = { static const struct apple_sleep_ops t6000_sleep_ops = { .deep_wfi_arm = t6000_deep_wfi_arm, + .boot_sleep = t6000_sleep_boot_sleep, .resume = cpu_resume, /* Wake-armed lines stay open for the last WFI, as the AIC's syscore suspend left them */ .aic_mask_all = aic_sleep_mask_all_but_wake, diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 967977da5986e..2b31943376f99 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -578,6 +578,12 @@ struct apple_sleep_extra_off { struct apple_sleep_ops { /* What makes a core's last WFI end in a reset, after its timers are masked */ void (*deep_wfi_arm)(void); + /* + * Optional: the boot core's last WFI, in place of deep_wfi_arm's, for a SoC whose commit + * gives up when an interrupt already pending ends it. Returns only then, with the data + * cache back on; .enter then restarts the machine. + */ + int (*boot_sleep)(void); /* Once the other cores stop: the I/O domains the commit lowers itself, ANS's last */ void (*lower_io)(struct apple_sleep *s); /* @@ -6320,6 +6326,23 @@ static void __noreturn sleep_boot_wfi(struct apple_sleep *s) } } +/* + * On a SoC with boot_sleep: the same, but an interrupt already pending that ends the WFI gives + * the sleep up, its peers told and stopped and the other cores down. The entry then returns as + * an uncommitted one. + */ +static int sleep_boot_sleep(struct apple_sleep *s) +{ + int ret; + + /* As sleep_deep_wfi_arm(): a timer left running ends the WFI. */ + write_sysreg(ARCH_TIMER_CTRL_IT_MASK, cntp_ctl_el0); + write_sysreg(ARCH_TIMER_CTRL_IT_MASK, cntv_ctl_el0); + ret = s->soc->ops->boot_sleep(); + s->entry_committed = false; + return ret; +} + /* XNU polls these off just before its complex words. The sleep goes on whatever they read. */ static void apple_sleep_wait_still_on(struct apple_sleep *s) { @@ -6431,6 +6454,8 @@ static int apple_sleep_commit(unsigned long unused) mdelay(s->soc->boot_wfi_delay_ms); if (s->soc->ops->last_mile == t8142_last_mile && READ_ONCE(sleep_post_commit_ms)) t8142_post_commit_probe(s, READ_ONCE(sleep_post_commit_ms)); + if (s->soc->ops->boot_sleep) + return sleep_boot_sleep(s); sleep_boot_wfi(s); } From 8c22cabaafe80c2cc5d33f62a11b2afcccd6c9de Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Wed, 2 Dec 2026 03:46:51 +0100 Subject: [PATCH 62/72] arm64: dts: apple: t6000: name the ISP DART's address filter The first of the camera coprocessor's three DARTs has an address filter (ADT dart-isp0 reg[5]) that the loader programs at boot. A deep sleep clears it. The ISP firmware then never answers its first handshake, and the camera does not start again until the next boot: apple-isp 384000000.isp: failed to boot firmware: -19 Name the filter's registers on that DART, as on the PMP's. The DART driver keeps what the loader programmed and writes it back when the system resumes, with the DART's other registers. Signed-off-by: Andreas Fernandez --- arch/arm64/boot/dts/apple/t6000.dtsi | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/arch/arm64/boot/dts/apple/t6000.dtsi b/arch/arm64/boot/dts/apple/t6000.dtsi index 041e03565e5ec..fd1c6ec1fba07 100644 --- a/arch/arm64/boot/dts/apple/t6000.dtsi +++ b/arch/arm64/boot/dts/apple/t6000.dtsi @@ -57,6 +57,17 @@ reg-names = "dart", "dapf"; }; +/* + * The camera coprocessor's first DART has an address filter (ADT dart-isp0 + * reg[5]) that the loader programs. A deep sleep clears it: the DART driver + * keeps what the loader programmed and writes it back on resume. + */ +&isp_dart0 { + reg = <0x3 0x860e8000 0x0 0x4000>, + <0x3 0x860ec000 0x0 0x4000>; + reg-names = "dart", "dapf"; +}; + &gpu { compatible = "apple,agx-t6000", "apple,agx-g13x"; }; From 49b66ced52a67ec094c8430ce725a3d2354ebdac Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Wed, 2 Dec 2026 16:20:19 +0100 Subject: [PATCH 63/72] arm64: dts: apple: t6000: give ANS its sleep tunables Describe ANS's AXI2AF and storage-lane blocks (ADT ans reg[4] and reg[5]) on the T6000's NVMe node, with this SoC's ADT values of afc-aiu-ans-dual-tunables and storage-lane-common-tunables. A deep sleep resets both blocks, and the NVMe driver puts the tunables back before ANS starts again. Only in the T6000 tree: the values were read from a T6000 ADT, and T6001 and T6002 share the compatible. Signed-off-by: Andreas Fernandez --- arch/arm64/boot/dts/apple/t6000.dtsi | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/arch/arm64/boot/dts/apple/t6000.dtsi b/arch/arm64/boot/dts/apple/t6000.dtsi index fd1c6ec1fba07..63c0ef1c70f81 100644 --- a/arch/arm64/boot/dts/apple/t6000.dtsi +++ b/arch/arm64/boot/dts/apple/t6000.dtsi @@ -68,6 +68,26 @@ reg-names = "dart", "dapf"; }; +/* + * ANS's AXI2AF and storage-lane blocks (ADT ans reg[4] and reg[5]) with the ADT's + * afc-aiu-ans-dual-tunables and storage-lane-common-tunables. A deep sleep resets + * both, and the NVMe driver puts the tunables back before ANS starts again. + */ +&nvme { + reg = <0x3 0x93cc0000 0x0 0x40000>, <0x3 0x8f400000 0x0 0x4000>, + <0x3 0x91000000 0x0 0x18000>, <0x3 0x93b90000 0x0 0xc000>; + reg-names = "nvme", "ans", "axi2af", "storage-lane"; + apple,tunable-axi2af = <0x000 0x00000003 0x00000001>, + <0x010 0x0fff0000 0x000d0000>, <0x014 0x0000003f 0x00000001>, + <0x018 0x0000003f 0x00000001>, <0x01c 0x0000003f 0x00000003>, + <0x020 0x0000003f 0x00000003>, <0x024 0x0000003f 0x00000003>, + <0x028 0x0000003f 0x00000003>, <0x02c 0x0000003f 0x00000003>, + <0x030 0x0000003f 0x00000003>, <0x034 0x0000003f 0x00000003>, + <0x400 0x400103ff 0x40010001>, <0x600 0x01ffffff 0x01ffffff>; + apple,tunable-storage-lane = <0x4100 0x00080000 0x0>, + <0x4104 0x00001000 0x0>; +}; + &gpu { compatible = "apple,agx-t6000", "apple,agx-g13x"; }; From abb3e06dfb855e9d912427704e289772459a027f Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 19:22:11 +0100 Subject: [PATCH 64/72] soc: apple: sleep: T6000: refuse a sleep without the enclave in .begin Without the enclave's driver bound (main loads it from user space, it has no module alias) the T6000 deep sleep was refused in .enter, by apple_sleep_gate_failed(). By then the AOP was stopped at .prepare and the display coprocessors and the SIO parked in suspend_late, which the refusal then has to undo. Make that refusal, and the one for an unbound internal display coprocessor, in the profile's .begin as well, before any device suspends, where both are already known. .enter keeps them. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/sleep-t6000.h | 42 ++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/drivers/soc/apple/sleep-t6000.h b/drivers/soc/apple/sleep-t6000.h index fd03dc14a1fdd..6137e6bfe72ff 100644 --- a/drivers/soc/apple/sleep-t6000.h +++ b/drivers/soc/apple/sleep-t6000.h @@ -827,9 +827,49 @@ static void t6000_psall_save(struct t6000_sleep *t) t->psall[i].saved = readl(t->psall[i].reg); } +/* + * Two of .enter's refusals (apple_sleep_refuse()) are known before anything is stopped, and + * are made here too, before the AOP is stopped at .prepare and the display coprocessors and + * the SIO are parked in suspend_late, which a refusal after them has to undo. + * The enclave's driver bound (apple_sleep_gate_failed(); its sleep request is registered only + * when that driver suspends, so that half stays with .enter), and a driver bound to the + * internal display coprocessor. + */ +static int t6000_sleep_refuse_early(struct apple_sleep *s) +{ + struct platform_device *pdev; + bool bound = false; + unsigned int i; + + apple_sleep_gate_prepare(s); + for (i = 0; i < s->gate_count; i++) { + if (s->gate[i].sep && !s->gate[i].bound) { + dev_err(s->dev, "refused: SEP has no prepared irreversible sleep request\n"); + return -EBUSY; + } + } + if (!s->soc->ndisp) + return 0; + pdev = s->display[0].np ? of_find_device_by_node(s->display[0].np) : NULL; + if (pdev) { + bound = device_is_bound(&pdev->dev); + put_device(&pdev->dev); + } + if (!bound) { + dev_err(s->dev, "refused: no driver bound to the display coprocessor\n"); + return -EBUSY; + } + return 0; +} + static int t6000_sleep_begin(struct apple_sleep *s) { struct t6000_sleep *t = &t6000_sleep; + int ret; + + ret = t6000_sleep_refuse_early(s); + if (ret) + return ret; t->committed = false; t->quiesce = false; @@ -1821,7 +1861,7 @@ static const struct apple_sleep_wake_name t6000_wake_names[] = { { 132, "ATC2.USBWakeup" }, { 133, "ATC3.USBWakeup" }, }; -/* The enclave's driver must be bound, and have told it at its suspend and seen it gate */ +/* The enclave's driver must be bound and have staged its sleep request for the commit */ static const struct apple_sleep_gate_coproc t6000_gate_coprocs[] = { { "/soc/sep@396400000" }, }; From 31bba74ad9108d506dd6dfcd9c03d7d66776d830 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Wed, 2 Dec 2026 16:13:40 +0100 Subject: [PATCH 65/72] soc: apple: sleep: T6000: take the late steps' words from the device tree The late power-state image was a table of physical addresses and values in the profile. Describe it instead: the PMGR power words and the values macOS writes to them come from the sleep node's apple,sleep-late-domains, each found through its power-state node, and the steps between them (their waits, the clock generator's words, the PMGR misc block's fabric word, SIO's words and the one NUB word no node describes yet) stay a short list in the profile that names them by kind. SIO's CPU, mailbox, DARTs and power word are reached through apple,sio, the misc block through apple,pmgr-misc; every block is mapped as the sleep node's own, shared and not claimed. The words written, their values and order are the same as before, and so are the waits but one: the memory controllers' power words that the commit puts back to the first sleep's got a fixed 100 us each, for the first eight only, as the traced sequence had it. Each is now waited for until its actual state is its target, bounded as every other power-word wait in the sleep; a word the PMGR manages itself (AUTO_ENABLE) is the PMGR's to move and is not waited for. Words that missed the bound are counted and reported once at the wake. The wake's put-back names a word that did not come back by its step's index instead of its address. Signed-off-by: Andreas Fernandez --- drivers/soc/apple/sleep-t6000.h | 501 +++++++++++++++++++++----------- 1 file changed, 335 insertions(+), 166 deletions(-) diff --git a/drivers/soc/apple/sleep-t6000.h b/drivers/soc/apple/sleep-t6000.h index 6137e6bfe72ff..6c21e80e4a534 100644 --- a/drivers/soc/apple/sleep-t6000.h +++ b/drivers/soc/apple/sleep-t6000.h @@ -75,9 +75,11 @@ static const u32 t6000_acc_regs[T6000_ACCR_N] = { [T6000_ACCR_DVFS] = T6000_ACC_DVFS, }; -/* ClkGenSOC: five muxes onto the reference clock, in macOS's order (0x160 stays) */ -#define T6000_CLKGEN_MUX 0x28e03815cULL -#define T6000_CLKGEN_SIZE 0x18 +/* + * In "clk-gen" (ClkGenSOC): five muxes from here onto the reference clock, in macOS's order + * (0x160 stays for the late steps) + */ +#define T6000_CLKGEN_QUIESCE 0x15c #define T6000_CLKGEN_MUXES 5 #define T6000_CLKGEN_ON BIT(31) #define T6000_CLKGEN_BUSY BIT(30) @@ -91,9 +93,12 @@ static const u8 t6000_clkgen_mux_off[T6000_CLKGEN_MUXES] = { 0x08, 0x10, 0x0c, 0 #define T6000_PLL_OFF_MODE GENMASK(5, 3) #define T6000_PLL_BUSY BIT(31) -/* The memory clock's performance state (_setDCSPerfState) */ -#define T6000_DCS_PS 0x28e20c800ULL +/* + * The memory clock's and the fabric's performance-state words: pmgr_misc's "dcs-ps" and + * "fabric-ps" (apple,t6000-pmgr-misc). Fields as drivers/soc/apple/apple-pmgr-misc.c has them. + */ #define T6000_DCS_PS_TARGET GENMASK(3, 0) +#define T6000_DCS_PS_ACTUAL GENMASK(7, 4) #define T6000_DCS_PS_BUSY BIT(31) /* @@ -142,13 +147,11 @@ static const u8 t6000_clkgen_mux_off[T6000_CLKGEN_MUXES] = { 0x08, 0x10, 0x0c, 0 /* The NUB PMGR's power words: they keep their state through the sleep */ #define T6000_NUB_PS 0x292280000ULL -/* WD1 from the late image to the wake: it keeps counting through the sleep */ +/* WD1 from the late steps to the wake: it keeps counting through the sleep */ #define T6000_WDT_GO_SECS 120 /* WD1 from the wake to .end: counted again from 0, for the device resume */ #define T6000_WDT_WAKE_SECS 170U -/* After the late image, before the S2R timer */ -#define T6000_PS_LATE_MS 200 /* The wake's power-domain walk: passes, 10 ms apart, while some are still off */ #define T6000_PSALL_PASSES 30 #define T6000_PSALL_MAX 256 @@ -185,51 +188,77 @@ static const u64 t6000_zero_pa[] = { }; /* - * The PMGR as macOS has it at the S2R timer, written raw right before it, without polling: - * address, value. Address 0 waits that many microseconds. + * The late steps, right before the S2R timer, exactly as macOS takes them (our hv traces of + * macOS on J314s): every word gets the value macOS writes, in its order, with its waits, and + * nothing is polled but SIO's CPU word. The PMGR words and their values are the sleep node's + * apple,sleep-late-domains, in its order; these steps say where the other words fall between + * them. Each of those is a named block's: "clk-gen", pmgr_misc's "fabric-ps", SIO's CPU, + * mailbox and DARTs (apple,sio), and one word in the NUB not described yet. */ -static const u64 t6000_ps_late[][2] = { - { 0x28e580160, 0x1f0000ff }, { 0x28e580180, 0x1f0000ff }, { 0x0, 0x7d0 }, - { 0x2922800d0, 0x300 }, { 0x2922800e0, 0x300 }, { 0x2922800d8, 0x300 }, - { 0x292280088, 0x300 }, { 0x292280098, 0x300 }, { 0x292280090, 0x300 }, - { 0x2922800a0, 0x300 }, { 0x28e080330, 0x300 }, { 0x28e080340, 0x300 }, - { 0x28e080350, 0x300 }, { 0x28e080360, 0x300 }, { 0x28e080238, 0x300 }, - { 0x28e0802f8, 0x300 }, { 0x28e080300, 0x300 }, { 0x28e580250, 0x200 }, - { 0x28e580258, 0x200 }, { 0x28e580260, 0x200 }, { 0x28e580268, 0x200 }, - { 0x28e580270, 0x200 }, { 0x292280078, 0x200 }, { 0x28e080220, 0x1000030f }, - { 0x28e580160, 0x1000030f }, { 0x28e580178, 0x1000030f }, { 0x28e080320, 0x1000030f }, - { 0x28e080138, 0x1400024f }, { 0x28e080140, 0x1400024f }, { 0x28e0802e8, 0x1f0003ff }, - { 0x28e0802f0, 0x1f0003ff }, { 0x28e080148, 0xff }, { 0x28e080120, 0xff }, - { 0x28e080130, 0xff }, { 0x28e580188, 0xf0002ff }, { 0x28e580190, 0x2ff }, - { 0x28e5801e8, 0xff }, { 0x28e580218, 0x1f0000ff }, { 0x28e580220, 0x1f0000ff }, - { 0x28e580228, 0x1f0000ff }, { 0x28e580238, 0xff }, { 0x28e580248, 0xff }, - { 0x28e580278, 0x2ff }, { 0x28e5802d0, 0x3ff }, { 0x28e5803b8, 0x1f0003ff }, - { 0x28e038104, 0x80100000 }, { 0x28e038108, 0x80100000 }, { 0x28e03810c, 0x80100000 }, - { 0x28e038114, 0x8a100100 }, { 0x28e038120, 0x85100100 }, { 0x28e038124, 0x85100100 }, - { 0x28e038128, 0x87100100 }, { 0x28e03812c, 0x87100100 }, { 0x28e038130, 0x87100100 }, - { 0x28e038134, 0x87100100 }, { 0x28e038138, 0x87100100 }, { 0x28e03813c, 0x87100100 }, - { 0x28e038148, 0x80100100 }, { 0x28e03814c, 0x85100100 }, { 0x28e038160, 0x85100100 }, - { 0x28e038180, 0x87100000 }, { 0x28e038184, 0x87100000 }, { 0x28e038188, 0x87100000 }, - { 0x28e03818c, 0x87100100 }, { 0x292240000, 0x80100010 }, { 0x28e20c000, 0x333 }, - { 0x0, 0x7d0 }, { 0x28e580188, 0xf00000f }, { 0x39bc00044, 0x0 }, - { 0x39bc08114, 0x0 }, { 0x39b0040fc, 0x0 }, { 0x39b0080fc, 0x0 }, - { 0x28e080138, 0x0 }, { 0x28e080320, 0x0 }, { 0x28e038098, 0x0 }, - { 0x28e03809c, 0x0 }, { 0x28e0380a0, 0x0 }, { 0x28e0380a4, 0x0 }, - { 0x0, 0x64 }, +enum t6000_late_op { + T6000_LATE_PS, /* the next arg words of apple,sleep-late-domains */ + T6000_LATE_WAIT, /* arg microseconds */ + T6000_LATE_CLKGEN, /* "clk-gen" + arg */ + T6000_LATE_NUB, /* T6000_LATE_NUB_PA */ + T6000_LATE_FABRIC, /* pmgr_misc's "fabric-ps" */ + T6000_LATE_SIO, /* SIO's word arg, written only while SIO's CPU word is on */ +}; + +enum { + T6000_LATE_SIO_ASC, /* its CPU's control word */ + T6000_LATE_SIO_MBOX, /* its mailbox's I2A control */ + T6000_LATE_SIO_DART0, /* each DART's stream enables */ + T6000_LATE_SIO_DART1, + T6000_LATE_SIO_N, +}; + +static const struct { + u8 op; + u16 arg; + u32 val; +} t6000_late_steps[] = { + { T6000_LATE_PS, 2 }, { T6000_LATE_WAIT, 2000 }, + { T6000_LATE_PS, 42 }, { T6000_LATE_CLKGEN, 0x104, 0x80100000 }, + { T6000_LATE_CLKGEN, 0x108, 0x80100000 }, { T6000_LATE_CLKGEN, 0x10c, 0x80100000 }, + { T6000_LATE_CLKGEN, 0x114, 0x8a100100 }, { T6000_LATE_CLKGEN, 0x120, 0x85100100 }, + { T6000_LATE_CLKGEN, 0x124, 0x85100100 }, { T6000_LATE_CLKGEN, 0x128, 0x87100100 }, + { T6000_LATE_CLKGEN, 0x12c, 0x87100100 }, { T6000_LATE_CLKGEN, 0x130, 0x87100100 }, + { T6000_LATE_CLKGEN, 0x134, 0x87100100 }, { T6000_LATE_CLKGEN, 0x138, 0x87100100 }, + { T6000_LATE_CLKGEN, 0x13c, 0x87100100 }, { T6000_LATE_CLKGEN, 0x148, 0x80100100 }, + { T6000_LATE_CLKGEN, 0x14c, 0x85100100 }, { T6000_LATE_CLKGEN, 0x160, 0x85100100 }, + { T6000_LATE_CLKGEN, 0x180, 0x87100000 }, { T6000_LATE_CLKGEN, 0x184, 0x87100000 }, + { T6000_LATE_CLKGEN, 0x188, 0x87100000 }, { T6000_LATE_CLKGEN, 0x18c, 0x87100100 }, + { T6000_LATE_NUB, 0, 0x80100010 }, { T6000_LATE_FABRIC, 0, 0x333 }, + { T6000_LATE_WAIT, 2000 }, { T6000_LATE_PS, 1 }, + { T6000_LATE_SIO, T6000_LATE_SIO_ASC, 0x0 }, { T6000_LATE_SIO, T6000_LATE_SIO_MBOX, 0x0 }, + { T6000_LATE_SIO, T6000_LATE_SIO_DART0, 0x0 }, { T6000_LATE_SIO, T6000_LATE_SIO_DART1, 0x0 }, + { T6000_LATE_PS, 2 }, { T6000_LATE_CLKGEN, 0x098, 0x00000000 }, + { T6000_LATE_CLKGEN, 0x09c, 0x00000000 }, { T6000_LATE_CLKGEN, 0x0a0, 0x00000000 }, + { T6000_LATE_CLKGEN, 0x0a4, 0x00000000 }, { T6000_LATE_WAIT, 100 } }; -#define T6000_PS_LATE_N ARRAY_SIZE(t6000_ps_late) -#define T6000_PS_LATE_SIO_CPU_PS 0x28e580188ULL -#define T6000_PS_LATE_SIO_ASC 0x39bc00000ULL +/* After the late steps, before the S2R timer */ +#define T6000_LATE_MS 200 +/* The NUB word macOS writes with the late steps; no node describes it yet */ +#define T6000_LATE_NUB_PA 0x292240000ULL + +#define T6000_LATE_MAX 96 + +/* SIO (apple,sio): its mailbox's I2A control, its DARTs' stream enables */ +#define T6000_MBOX_I2A_CONTROL 0x114 +#define T6000_DART_STREAMS_ENABLE 0xfc static struct t6000_sleep { void __iomem *ps_east, *spmi_irq, *nco, *ptd; void __iomem *cluster_ps, *afr_ps, *pmp_ps; - void __iomem *dcs, *pll, *clkgen, *apcie_8014, *apcie_82bc; + void __iomem *dcs, *fabric_ps, *pll, *clkgen_blk, *clkgen, *apcie_8014, *apcie_82bc; void __iomem *acc[T6000_ACCS]; void __iomem *amcc[T6000_AMCCS], *dcs_blk[T6000_AMCCS][T6000_DCS_PER_AMCC]; void __iomem *zero[ARRAY_SIZE(t6000_zero_pa)]; - void __iomem *ps_late_reg[T6000_PS_LATE_N]; + /* SIO's CPU, mailbox and DARTs, and its CPU's power word */ + void __iomem *sio_asc, *sio_mbox, *sio_dart[2], *sio_cpu_ps; + /* The late steps' NUB word */ + void __iomem *late_nub; /* The PMP, whose stop lets the commit take the memory clock down */ struct device_node *pmp_np; /* The boot core, which takes its WFI last */ @@ -270,11 +299,14 @@ static struct t6000_sleep { u32 ptd_sleep_saved; bool ptd_noticed; - /* The late image's words as Linux had them, for the wake */ - u32 ps_late_saved[T6000_PS_LATE_N]; - DECLARE_BITMAP(ps_late_is_ps, T6000_PS_LATE_N); - DECLARE_BITMAP(ps_late_has_saved, T6000_PS_LATE_N); - bool ps_late_applied; + /* The late steps' words, in the order they are written, as Linux had them at .begin */ + struct t6000_late_word { + void __iomem *reg; + u8 op; + u32 val, saved; + } late[T6000_LATE_MAX]; + unsigned int late_n; + bool late_applied; /* The display blocks are down for this sleep and come back with a given-up one */ bool disp_undo_due; @@ -298,6 +330,8 @@ static struct t6000_sleep { /* The memory controllers' power words at the first sleep's S2R timer */ u32 go_base[T6000_PSALL_MAX]; bool go_base_taken; + /* DCS words that did not settle within the bound, this sleep (for the wake's report) */ + u8 go_timeouts; } t6000_sleep; #define T6000_SOCQ_COUNTERS BIT(0) @@ -581,15 +615,6 @@ static int t6000_sleep_boot_sleep(void) return ret; } -/* The late image touches SIO's CPU, mailbox and DARTs only while SIO's CPU word is on. */ -static bool t6000_ps_late_sio_pa(u64 pa) -{ - return (pa >= T6000_PS_LATE_SIO_ASC && pa < T6000_PS_LATE_SIO_ASC + 0x100) || - (pa >= 0x39bc08100ULL && pa < 0x39bc08120ULL) || - (pa >= 0x39b004000ULL && pa < 0x39b004200ULL) || - (pa >= 0x39b008000ULL && pa < 0x39b008200ULL); -} - /* PMGR registers only, and none that iBoot armed or that must never be touched */ static bool t6000_restore_pa_ok(u64 pa) { @@ -609,29 +634,6 @@ static bool t6000_restore_pa_ok(u64 pa) return true; } -/* A power word some PMGR power-state node describes */ -static bool t6000_pa_is_ps(u64 pa) -{ - struct device_node *np, *parent; - struct resource res; - bool found = false; - u32 off; - - for_each_compatible_node(np, NULL, "apple,pmgr-pwrstate") { - if (of_property_read_u32(np, "reg", &off)) - continue; - parent = of_get_parent(np); - if (parent && !of_address_to_resource(parent, 0, &res) && res.start + off == pa) - found = true; - of_node_put(parent); - if (found) { - of_node_put(np); - break; - } - } - return found; -} - static void __iomem *t6000_psall_map(struct apple_sleep *s, struct device_node *parent, resource_size_t *start) { @@ -695,6 +697,180 @@ static int t6000_psall_init(struct apple_sleep *s) return 0; } +/* Another node's reg, by index, mapped as the sleep node's own: shared, not claimed */ +static void __iomem *t6000_map_node(struct apple_sleep *s, struct device_node *np, int index) +{ + struct resource res; + + if (index < 0 || of_address_to_resource(np, index, &res)) + return NULL; + return t6000_ioremap(s, res.start, resource_size(&res)); +} + +static void __iomem *t6000_map_named(struct apple_sleep *s, struct device_node *np, + const char *name) +{ + return t6000_map_node(s, np, of_property_match_string(np, "reg-names", name)); +} + +/* apple,pmgr-misc: the memory clock's and the fabric's performance-state words */ +static int t6000_pmgr_misc_map(struct apple_sleep *s, struct t6000_sleep *t) +{ + struct device_node *misc = of_parse_phandle(s->dev->of_node, "apple,pmgr-misc", 0); + + if (misc) { + t->dcs = t6000_map_named(s, misc, "dcs-ps"); + t->fabric_ps = t6000_map_named(s, misc, "fabric-ps"); + of_node_put(misc); + } + if (!t->dcs || !t->fabric_ps) + return dev_err_probe(s->dev, -EINVAL, + "apple,pmgr-misc: dcs-ps and fabric-ps must both be mapped\n"); + return 0; +} + +static int t6000_psall_find(struct t6000_sleep *t, struct device_node *np); + +/* A power-state node's word, from the walk of every PMGR word (t6000_psall_init()) */ +static void __iomem *t6000_ps_reg(struct t6000_sleep *t, struct device_node *np) +{ + int i = np ? t6000_psall_find(t, np) : -1; + + return i < 0 ? NULL : t->psall[i].reg; +} + +static int t6000_late_add(struct apple_sleep *s, struct t6000_sleep *t, void __iomem *reg, + u8 op, u32 val) +{ + if (t->late_n == T6000_LATE_MAX) + return dev_err_probe(s->dev, -E2BIG, "more than %u late-step words\n", + T6000_LATE_MAX); + t->late[t->late_n].reg = reg; + t->late[t->late_n].op = op; + t->late[t->late_n].val = val; + t->late_n++; + return 0; +} + +/* apple,sio: SIO's CPU (its reg), mailbox, two DARTs and CPU power word, by its node */ +static int t6000_sio_map(struct apple_sleep *s, struct t6000_sleep *t) +{ + struct device_node *sio = of_parse_phandle(s->dev->of_node, "apple,sio", 0), *np; + struct of_phandle_args args; + unsigned int i; + + if (!sio) + return dev_err_probe(s->dev, -EINVAL, "apple,sio is missing\n"); + t->sio_asc = t6000_map_node(s, sio, 0); + np = of_parse_phandle(sio, "mboxes", 0); + if (np) { + t->sio_mbox = t6000_map_node(s, np, 0); + of_node_put(np); + } + for (i = 0; i < ARRAY_SIZE(t->sio_dart); i++) { + if (of_parse_phandle_with_args(sio, "iommus", "#iommu-cells", i, &args)) + break; + t->sio_dart[i] = t6000_map_node(s, args.np, 0); + of_node_put(args.np); + } + np = of_parse_phandle(sio, "power-domains", 0); + t->sio_cpu_ps = t6000_ps_reg(t, np); + of_node_put(np); + of_node_put(sio); + if (!t->sio_asc || !t->sio_mbox || !t->sio_dart[0] || !t->sio_dart[1] || !t->sio_cpu_ps) + return dev_err_probe(s->dev, -EINVAL, + "apple,sio: CPU, mailbox, DARTs or power word not mapped\n"); + return 0; +} + +/* The next n entries of apple,sleep-late-domains: a PMGR power word and the value it gets */ +static int t6000_late_add_ps(struct apple_sleep *s, struct t6000_sleep *t, + struct of_phandle_iterator *it, unsigned int n) +{ + void __iomem *reg; + u32 val; + int ret; + + while (n--) { + ret = of_phandle_iterator_next(it); + if (ret) + return dev_err_probe(s->dev, -EINVAL, + "apple,sleep-late-domains: fewer entries than the steps\n"); + reg = t6000_ps_reg(t, it->node); + if (!reg || of_phandle_iterator_args(it, &val, 1) != 1) + ret = dev_err_probe(s->dev, -EINVAL, + "apple,sleep-late-domains: %pOF is not a PMGR power word\n", + it->node); + else + ret = t6000_late_add(s, t, reg, T6000_LATE_PS, val); + if (ret) { + of_node_put(it->node); + it->node = NULL; + return ret; + } + } + return 0; +} + +/* + * The late steps from the sleep node: t6000_late_steps[] with each PMGR step's words taken in + * order from apple,sleep-late-domains, which must have exactly as many. A missing block or a + * phandle that is not a PMGR power word fails the probe. + */ +static int t6000_late_init(struct apple_sleep *s, struct t6000_sleep *t) +{ + void __iomem *sio_reg[T6000_LATE_SIO_N]; + struct of_phandle_iterator it; + void __iomem *reg; + unsigned int i; + int ret; + + ret = t6000_sio_map(s, t); + if (ret) + return ret; + sio_reg[T6000_LATE_SIO_ASC] = t->sio_asc + ASC_CPU_CONTROL; + sio_reg[T6000_LATE_SIO_MBOX] = t->sio_mbox + T6000_MBOX_I2A_CONTROL; + sio_reg[T6000_LATE_SIO_DART0] = t->sio_dart[0] + T6000_DART_STREAMS_ENABLE; + sio_reg[T6000_LATE_SIO_DART1] = t->sio_dart[1] + T6000_DART_STREAMS_ENABLE; + t->late_nub = t6000_ioremap(s, T6000_LATE_NUB_PA, 4); + if (!t->late_nub) + return dev_err_probe(s->dev, -ENOMEM, "the late steps' NUB word not mapped\n"); + + ret = of_phandle_iterator_init(&it, s->dev->of_node, "apple,sleep-late-domains", NULL, 1); + if (ret) + return dev_err_probe(s->dev, ret, "apple,sleep-late-domains is missing\n"); + for (i = 0; i < ARRAY_SIZE(t6000_late_steps); i++) { + u8 op = t6000_late_steps[i].op; + u16 arg = t6000_late_steps[i].arg; + u32 val = t6000_late_steps[i].val; + + switch (op) { + case T6000_LATE_PS: + ret = t6000_late_add_ps(s, t, &it, arg); + break; + case T6000_LATE_WAIT: + ret = t6000_late_add(s, t, NULL, op, arg); + break; + default: + reg = op == T6000_LATE_CLKGEN ? t->clkgen_blk + arg : + op == T6000_LATE_NUB ? t->late_nub : + op == T6000_LATE_FABRIC ? t->fabric_ps : sio_reg[arg]; + ret = t6000_late_add(s, t, reg, op, val); + break; + } + if (ret) { + of_node_put(it.node); + return ret; + } + } + if (of_phandle_iterator_next(&it) != -ENOENT) { + of_node_put(it.node); + return dev_err_probe(s->dev, -EINVAL, + "apple,sleep-late-domains: more entries than the steps\n"); + } + return 0; +} + /* The NCO block of the sleep node's apple,nco: mapped, not requested, as its driver has it. */ static void __iomem *t6000_map_nco(struct apple_sleep *s) { @@ -757,21 +933,21 @@ static int t6000_sleep_map_regs(struct apple_sleep *s) } for (i = 0; i < ARRAY_SIZE(t6000_zero_pa); i++) t->zero[i] = t6000_ioremap(s, t6000_zero_pa[i], 4); - t->clkgen = t6000_ioremap(s, T6000_CLKGEN_MUX, T6000_CLKGEN_SIZE); + t->clkgen_blk = apple_sleep_map(s->dev, "clk-gen"); + if (IS_ERR(t->clkgen_blk)) + return dev_err_probe(s->dev, PTR_ERR(t->clkgen_blk), "clk-gen must be mapped\n"); + t->clkgen = t->clkgen_blk + T6000_CLKGEN_QUIESCE; t->pll = t6000_ioremap(s, T6000_PLL0, 4); - t->dcs = t6000_ioremap(s, T6000_DCS_PS, 4); + ret = t6000_pmgr_misc_map(s, t); + if (ret) + return ret; t->apcie_8014 = t6000_ioremap(s, T6000_APCIE_Q_8014, 4); t->apcie_82bc = t6000_ioremap(s, T6000_APCIE_Q_82BC, 4); - for (i = 0; i < T6000_PS_LATE_N; i++) { - u64 pa = t6000_ps_late[i][0]; - - if ((pa >= 0x28e000000ULL && pa < 0x293000000ULL) || t6000_ps_late_sio_pa(pa)) - t->ps_late_reg[i] = t6000_ioremap(s, pa & ~3ULL, 4); - if (t->ps_late_reg[i] && t6000_restore_pa_ok(pa) && t6000_pa_is_ps(pa)) - __set_bit(i, t->ps_late_is_ps); - } ret = t6000_psall_init(s); + if (ret) + return ret; + ret = t6000_late_init(s, t); if (ret) return ret; @@ -801,21 +977,15 @@ static int t6000_sleep_map_regs(struct apple_sleep *s) return t6000_display_map(s); } -/* Awake, at .begin: what Linux has in every PMGR register the late image writes */ -static void t6000_ps_late_save(struct t6000_sleep *t) +/* Awake, at .begin: what Linux has in every word the late steps write */ +static void t6000_late_save(struct t6000_sleep *t) { unsigned int i; - bitmap_zero(t->ps_late_has_saved, T6000_PS_LATE_N); - t->ps_late_applied = false; - for (i = 0; i < T6000_PS_LATE_N; i++) { - u64 pa = t6000_ps_late[i][0]; - - if (!pa || !t->ps_late_reg[i] || !t6000_restore_pa_ok(pa)) - continue; - t->ps_late_saved[i] = readl(t->ps_late_reg[i]); - __set_bit(i, t->ps_late_has_saved); - } + t->late_applied = false; + for (i = 0; i < t->late_n; i++) + if (t->late[i].op != T6000_LATE_WAIT && t->late[i].op != T6000_LATE_SIO) + t->late[i].saved = readl(t->late[i].reg); } static void t6000_psall_save(struct t6000_sleep *t) @@ -882,7 +1052,7 @@ static int t6000_sleep_begin(struct apple_sleep *s) t->afr_cl_valid = false; t6000_display.cycled = 0; t6000_display.gpu_port_down = false; - t6000_ps_late_save(t); + t6000_late_save(t); t6000_psall_save(t); return t6000_display_own(s, true); } @@ -1251,16 +1421,27 @@ static u32 t6000_go_mask(u32 a, u32 b) } /* - * 100 us after each of the first eight words put back in a sleep, as the sequence that runs on - * the hardware has it, and none after the rest. + * A DCS word just written has settled once its actual state is its target: the status the + * fixed 100 us stood in for, bounded as every other power-word wait in the sleep. One the + * PMGR manages (AUTO_ENABLE) is the PMGR's to move, and its actual state is outside the + * baseline's compare (t6000_go_mask()): nothing to wait for. */ -#define T6000_GO_SETTLE_N 8 +static int t6000_go_settle(void __iomem *reg) +{ + u32 v = readl(reg); + + if (v & PS_AUTO_ENABLE) + return 0; + return readl_poll_timeout_atomic(reg, v, FIELD_GET(PS_ACTUAL, v) == FIELD_GET(PS_TARGET, v), + 1, PS_OFF_TIMEOUT_US); +} static void t6000_go_check(struct t6000_sleep *t) { - unsigned int i, put_back = 0; + unsigned int i; u32 v, base, d; + t->go_timeouts = 0; for (i = 0; i < t->psall_n; i++) { void __iomem *reg = t->psall[i].reg; @@ -1271,7 +1452,8 @@ static void t6000_go_check(struct t6000_sleep *t) /* A floor there on the first sleep must not end up in the baseline. */ if (v & GENMASK(19, 16)) { writel(v & ~(GENMASK(19, 16) | BIT(31) | BIT(9) | BIT(8)), reg); - udelay(100); + if (t6000_go_settle(reg)) + t->go_timeouts++; v = readl(reg); } t->go_base[i] = v; @@ -1284,45 +1466,38 @@ static void t6000_go_check(struct t6000_sleep *t) writel((v & ~(PS_TARGET | PS_AUTO_ENABLE | GENMASK(19, 16) | BIT(31) | BIT(9) | BIT(8))) | (base & (PS_TARGET | PS_AUTO_ENABLE | GENMASK(19, 16))), reg); - if (put_back++ < T6000_GO_SETTLE_N) - udelay(100); + if (t6000_go_settle(reg)) + t->go_timeouts++; } if (t->psall_n) t->go_base_taken = true; } -/* The late image, every entry, unconditionally, with its waits; then 200 ms */ -static void t6000_ps_late_write(struct t6000_sleep *t) +/* The late steps, every word, unconditionally, with their waits; then 200 ms */ +static void t6000_late_write(struct t6000_sleep *t) { - void __iomem *sio_ps = NULL; - unsigned int i, j; + unsigned int i, us; - for (j = 0; j < T6000_PS_LATE_N; j++) - if (t6000_ps_late[j][0] == T6000_PS_LATE_SIO_CPU_PS && t->ps_late_reg[j]) - sio_ps = t->ps_late_reg[j]; + t->late_applied = t->late_n != 0; + for (i = 0; i < t->late_n; i++) { + const struct t6000_late_word *w = &t->late[i]; - t->ps_late_applied = !bitmap_empty(t->ps_late_has_saved, T6000_PS_LATE_N); - for (i = 0; i < T6000_PS_LATE_N; i++) { - if (!t6000_ps_late[i][0]) { - udelay(min_t(u64, t6000_ps_late[i][1], 20000)); + if (w->op == T6000_LATE_WAIT) { + udelay(w->val); continue; } - if (!t->ps_late_reg[i]) - continue; - if (t6000_ps_late_sio_pa(t6000_ps_late[i][0])) { - unsigned int us; - + if (w->op == T6000_LATE_SIO) { /* SIO's word needs a moment after its target is raised */ - for (us = 0; sio_ps && us < 2000 && - FIELD_GET(PS_ACTUAL, readl(sio_ps)) != 0xf; us++) + for (us = 0; us < 2000 && + FIELD_GET(PS_ACTUAL, readl(t->sio_cpu_ps)) != 0xf; us++) udelay(1); - if (!sio_ps || FIELD_GET(PS_ACTUAL, readl(sio_ps)) != 0xf) + if (FIELD_GET(PS_ACTUAL, readl(t->sio_cpu_ps)) != 0xf) continue; } - writel((u32)t6000_ps_late[i][1], t->ps_late_reg[i]); + writel(w->val, w->reg); } dsb(sy); - mdelay(T6000_PS_LATE_MS); + mdelay(T6000_LATE_MS); } /* The PMU RTC alarm's vector on, unless its driver has it on already */ @@ -1375,10 +1550,10 @@ static void t6000_sleep_commit_regs(struct apple_sleep *s) t6000_cluster_eacc(t); dsb(sy); - /* A fabric hang in the raw writes below still ends in a reset */ + /* A fabric hang in the late steps below still ends in a reset */ apple_sleep_set_wd2(s, T6000_WDT_GO_SECS); dsb(sy); - t6000_ps_late_write(t); + t6000_late_write(t); t6000_spmi_arm(t); /* From here to the wake: it keeps counting through the sleep */ apple_sleep_set_wd2(s, T6000_WDT_GO_SECS); @@ -1575,50 +1750,44 @@ static void t6000_sleep_wake(struct apple_sleep *s) } /* - * After a sleep, before any device resumes: the late image's registers back as Linux had - * them. Walked backwards, so parents come back before children, each address once. + * After a sleep, before any device resumes: the late steps' words back as Linux had them. + * Walked backwards, so parents come back before children, each word once. */ -static void t6000_ps_late_put_back(struct apple_sleep *s, struct t6000_sleep *t) +static void t6000_late_put_back(struct apple_sleep *s, struct t6000_sleep *t) { - DECLARE_BITMAP(done, T6000_PS_LATE_N); - unsigned int i, j, us; + unsigned int i, j; + u32 v, now; - if (!t->ps_late_applied) + if (!t->late_applied) return; - t->ps_late_applied = false; - bitmap_zero(done, T6000_PS_LATE_N); - for (i = T6000_PS_LATE_N; i-- > 0;) { - u64 pa = t6000_ps_late[i][0]; + t->late_applied = false; + if (t->go_timeouts) + dev_warn(s->dev, "T6000: %u DCS words missed their bound\n", t->go_timeouts); + for (i = t->late_n; i-- > 0;) { + const struct t6000_late_word *w = &t->late[i]; bool again = false; - u32 v; - if (!test_bit(i, t->ps_late_has_saved)) + if (w->op == T6000_LATE_WAIT || w->op == T6000_LATE_SIO) continue; - for (j = i + 1; j < T6000_PS_LATE_N; j++) - if (t6000_ps_late[j][0] == pa && test_bit(j, done)) + for (j = i + 1; j < t->late_n; j++) + if (t->late[j].reg == w->reg) again = true; if (again) continue; - __set_bit(i, done); - v = t->ps_late_saved[i]; - /* One Linux had on comes back through the walk below, parents first. */ - if (test_bit(i, t->ps_late_is_ps) && FIELD_GET(PS_ACTUAL, v)) - continue; - if (!test_bit(i, t->ps_late_is_ps)) { - writel(v, t->ps_late_reg[i]); + v = w->saved; + if (w->op != T6000_LATE_PS) { + writel(v, w->reg); continue; } + /* One Linux had on comes back through the walk below, parents first. */ + if (FIELD_GET(PS_ACTUAL, v)) + continue; /* The was-gated bits are write-one-to-clear, the actual state read only */ - writel(v & ~(BIT(9) | BIT(8) | PS_ACTUAL), t->ps_late_reg[i]); - for (us = 0; us < 10000; us++) { - if (FIELD_GET(PS_ACTUAL, readl(t->ps_late_reg[i])) == - FIELD_GET(PS_TARGET, v)) - break; - udelay(10); - } - if (us == 10000) - dev_warn(s->dev, "T6000: power word %#llx not back at %lu: %#x\n", pa, - FIELD_GET(PS_TARGET, v), readl(t->ps_late_reg[i])); + writel(v & ~(PS_FLAGS | PS_ACTUAL), w->reg); + if (readl_poll_timeout_atomic(w->reg, now, FIELD_GET(PS_ACTUAL, now) == + FIELD_GET(PS_TARGET, v), 10, 100000)) + dev_warn(s->dev, "T6000: late-step word %u not back at %lu: %#x\n", i, + FIELD_GET(PS_TARGET, v), now); } } @@ -1773,7 +1942,7 @@ static void t6000_sleep_restart_displays(struct apple_sleep *s) } /* - * At .wake (and again at .end, when each step has already run): the late image put back, the + * At .wake (and again at .end, when each step has already run): the late steps put back, the * display blocks back after a sleep that was given up, then every domain that was on, the GPU's * port as the commit found it, then the blocks of the parked display coprocessors that the entry * did not take down. @@ -1782,7 +1951,7 @@ static void t6000_sleep_finish(struct apple_sleep *s) { struct t6000_sleep *t = &t6000_sleep; - t6000_ps_late_put_back(s, t); + t6000_late_put_back(s, t); if (t->disp_undo_due) { t->disp_undo_due = false; t6000_sleep_display_undo(s); From 3323db846cbf681f5985432076347ed857a24128 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Tue, 1 Dec 2026 19:23:51 +0100 Subject: [PATCH 66/72] drm/apple: T6000: stop a Type-C DCP's Type-C work at suspend prepare A T6000 DCP with Type-C outputs stopped its Type-C work in its own .suspend. The USB controllers take the Type-C PHYs down in their suspend, and device suspend sets no order between them and the DCP, so the work could still act on a PHY that is going away. For a deep sleep, stop the work at PM_SUSPEND_PREPARE instead, before any device suspends, and let .suspend skip it then. A sleep that fails before the DCP suspends starts the work again from .complete or PM_POST_SUSPEND. PM_SUSPEND_PREPARE has no target state yet, so a deep sleep is taken to be one where s2idle is not the default; an explicit freeze on such a system also stops the work there, and .complete starts it again. DCPs with hw.s2r_park register for this: T6000's, and T6001's and T6002's, which share the compatible. Those have no deep sleep, so s2idle stays their default and nothing changes for them. Signed-off-by: Andreas Fernandez --- drivers/gpu/drm/apple/dcp-internal.h | 2 ++ drivers/gpu/drm/apple/dcp.c | 31 ++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index e4af9ec5d3f47..4190ff47db711 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -1093,6 +1093,8 @@ struct apple_dcp { bool rx_held; /* hw.s2r_park: the suspend got as far as the park, which waits for the late suspend */ bool s2r_park_due; + /* hw.s2r_park, a Type-C output: its Type-C work stopped at PM_SUSPEND_PREPARE */ + bool s2r_typec_off; /* hw.s2r_park: parked for the sleep (IDLE, CPU stopped), restarted by resume */ bool s2r_parked; /* hw.s2r_park: the park set the AP to IDLE, the restart sets it ON again */ diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 97c18ec6fb29e..216bd022f1fcc 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -10347,6 +10347,12 @@ static void dcp_platform_complete(struct device *dev) { struct apple_dcp *dcp = dev_get_drvdata(dev); + /* The sleep failed before this device suspended (dcp_s2r_pm_notify()). */ + if (dcp->s2r_typec_off) { + dcp->s2r_typec_off = false; + dcp_enable_typec_work(dcp); + } + /* The iBoot interface's display stays powered across system sleep. */ if (dcp_is_iboot(dcp) || READ_ONCE(dcp->sleep_cold_stopped)) return; @@ -12850,6 +12856,23 @@ static int dcp_s2r_pm_notify(struct notifier_block *nb, unsigned long action, vo dcp_v27_dark_replug_owed(dcp); if (dcp_ext_idle_capable(dcp)) dcp_ext_idle_kick(dcp); + /* The sleep failed before this device suspended */ + if (dcp->s2r_typec_off) { + dcp->s2r_typec_off = false; + dcp_enable_typec_work(dcp); + } + } + /* + * hw.s2r_park, a Type-C output: for a deep sleep its Type-C work stops before the sleep + * starts, so that it does not run while the USB controllers take the Type-C PHYs down in + * their own suspend, which has no set order against this device's. PM_SUSPEND_PREPARE + * has no target state yet: a deep sleep is "mem" when s2idle is not the default. + */ + if (action == PM_SUSPEND_PREPARE && dcp->hw.s2r_park && dcp_is_typec_output(dcp) && + !pm_suspend_default_s2idle() && !dcp_takeover_was_aborted(dcp) && + !dcp->s2r_typec_off) { + dcp_disable_typec_work(dcp, false); + dcp->s2r_typec_off = true; } /* * A J714s external display processor that has crashed cannot take its sleep calls, and @@ -12877,7 +12900,7 @@ static int dcp_s2r_pm_register(struct apple_dcp *dcp) { int ret; - if (!dcp_s2r_ext_capable(dcp) && !dcp_j714s_ext(dcp->dev)) + if (!dcp_s2r_ext_capable(dcp) && !dcp_j714s_ext(dcp->dev) && !dcp->hw.s2r_park) return 0; dcp->s2r_pm_nb.notifier_call = dcp_s2r_pm_notify; ret = register_pm_notifier(&dcp->s2r_pm_nb); @@ -13549,7 +13572,11 @@ static int dcp_platform_suspend(struct device *dev) * connected, and resume powers the CRTC back up through dcp_poweron() * as after DPMS off. */ - dcp_disable_typec_work(dcp, false); + /* Stopped already for a deep sleep, see dcp_s2r_pm_notify() */ + if (dcp->s2r_typec_off) + dcp->s2r_typec_off = false; + else + dcp_disable_typec_work(dcp, false); cancel_delayed_work_sync(&dcp->swap_watchdog_wq); if (dcp->external_native) disable_work_sync(&dcp->external_audio_work); From 922cd8519fb9217d792e2dfc04c5702b7e9388c5 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Mon, 30 Nov 2026 23:43:51 +0100 Subject: [PATCH 67/72] arm64: dts: apple: t6000: add the sleep controller Add the M1 Pro's sleep controller to the T6000 tree only: T6001 and T6002 share the die's include but have no such sleep. Disabled there, and enabled for J314s, the one T6000 machine whose sleep sequence is described. The wake sources are the ones macOS leaves on: the AOP's, the NUB's (the PMU's RTC alarm among them), the SMC's and the Type-C ports'. The AP pins are put back after a sleep, but for the three the ADT lists as no-restore-gpio-pins. The node names the PMGR's misc block, SIO and the NCO by phandle, lists the PMGR words of the late steps before the S2R timer with the values macOS writes (apple,sleep-late-domains, from our traces of macOS on a J314s), and gives the GPU's and the display blocks' fabric ports with the setup macOS writes before each block powers down (apple,tunable-*-fabric, from the same traces). Signed-off-by: Andreas Fernandez --- arch/arm64/boot/dts/apple/t6000-j314s.dts | 4 + arch/arm64/boot/dts/apple/t6000.dtsi | 337 ++++++++++++++++++++++ 2 files changed, 341 insertions(+) diff --git a/arch/arm64/boot/dts/apple/t6000-j314s.dts b/arch/arm64/boot/dts/apple/t6000-j314s.dts index def2af47589d1..4446fc246c1a5 100644 --- a/arch/arm64/boot/dts/apple/t6000-j314s.dts +++ b/arch/arm64/boot/dts/apple/t6000-j314s.dts @@ -129,3 +129,7 @@ &apciec3_piodma { status = "disabled"; }; + +&sleep { + status = "okay"; +}; diff --git a/arch/arm64/boot/dts/apple/t6000.dtsi b/arch/arm64/boot/dts/apple/t6000.dtsi index 63c0ef1c70f81..0f77a5718e1af 100644 --- a/arch/arm64/boot/dts/apple/t6000.dtsi +++ b/arch/arm64/boot/dts/apple/t6000.dtsi @@ -88,6 +88,343 @@ <0x4104 0x00001000 0x0>; }; +&{/soc} { + sleep: sleep-controller@292004000 { + compatible = "apple,t6000-sleep"; + reg = <0x2 0x92004000 0x0 0x40>, + <0x2 0x8e080000 0x0 0x50000>, + <0x2 0x8e580000 0x0 0x1000>, + <0x2 0x8e0d4000 0x0 0x18>, + <0x2 0x922b0000 0x0 0x30>, + <0x2 0x925c4000 0x0 0x4>, + <0x2 0x920a1320 0x0 0x44>, + <0x2 0x8e3c0000 0x0 0x19000>, + <0x2 0x8e038000 0x0 0x200>, + <0x4 0x05000000 0x0 0xc000>, + <0x3 0x8c000000 0x0 0xc000>, + <0x2 0x8a000000 0x0 0xc000>, + <0x2 0x8d000000 0x0 0xc000>; + reg-names = "wake", "ps", "ps-east", "cpu-start", "wdt", "commit", + "spmi-irq", "ptd", "clk-gen", "gfx-fabric", + "disp0-fabric", "dispext0-fabric", "dispext1-fabric"; + apple,pmgr-misc = <&pmgr_misc>; + apple,sio = <&sio>; + /* + * The PMGR words of the late steps before the S2R timer, in macOS's order, + * each with the value macOS writes (our traces of macOS on J314s). + */ + apple,sleep-late-domains = + <&ps_afnc2_lw1 0x1f0000ff>, <&ps_sio 0x1f0000ff>, + <&ps_atc0_usb 0x300>, <&ps_atc2_usb 0x300>, + <&ps_atc1_usb 0x300>, <&ps_atc0_usb_aon 0x300>, + <&ps_atc2_usb_aon 0x300>, <&ps_atc1_usb_aon 0x300>, + <&ps_atc3_usb_aon 0x300>, <&ps_atc0_pcie 0x300>, + <&ps_atc1_pcie 0x300>, <&ps_atc2_pcie 0x300>, + <&ps_atc3_pcie 0x300>, <&ps_atc0_common 0x300>, + <&ps_atc2_common 0x300>, <&ps_atc3_common 0x300>, + <&ps_uart2 0x200>, <&ps_uart3 0x200>, + <&ps_uart4 0x200>, <&ps_uart6 0x200>, + <&ps_uart7 0x200>, <&ps_msg 0x200>, + <&ps_afnc1_lw2 0x1000030f>, <&ps_afnc2_lw1 0x1000030f>, + <&ps_afnc3_lw0 0x1000030f>, <&ps_apcie_gp_sys 0x1000030f>, + <&ps_pcie0_ref 0x1400024f>, <&ps_pcie1_ref 0x1400024f>, + <&ps_apcie_st_sys 0x1f0003ff>, <&ps_apcie_st1_sys 0x1f0003ff>, + <&ps_apcie_st 0xff>, <&ps_gpio 0xff>, + <&ps_pmgr_soc_ocla 0xff>, <&ps_sio_cpu 0xf0002ff>, + <&ps_fpwm0 0x2ff>, <&ps_spi_p 0xff>, + <&ps_sio_spmi0 0x1f0000ff>, <&ps_sio_spmi1 0x1f0000ff>, + <&ps_sio_spmi2 0x1f0000ff>, <&ps_uart_n 0xff>, + <&ps_uart1 0xff>, <&ps_audio_p 0x2ff>, + <&ps_aes 0x3ff>, <&ps_ans2 0x1f0003ff>, + <&ps_sio_cpu 0xf00000f>, <&ps_pcie0_ref 0x0>, + <&ps_apcie_gp_sys 0x0>; + /* + * The sources macOS leaves on: AOP.GPIO.IRQ4/6, AOP.OutboxNotEmpty, + * AOP.CPUWakeupAP, NUB.WakeupAP, NUB.GPIO.IRQ2, NUB.APWatchdog, + * NUB.APWakeupTime, NUB.SPMISw0/1/3IRQ, SMC.CPUWakeupAP, + * SMC.OutboxNotEmpty, ATC0-3.CIOWakeup, ATC0-3.USBWakeup. + */ + 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,nco = <&nco>; + + /* + * The setup of the GPU's and the display blocks' fabric ports, as macOS + * writes it in this order when it powers each block up (own hypervisor + * trace, macOS 13.7.8 on a J314s). Linux leaves the blocks at their reset + * values; the sleep entry applies these, then the fabric handshake, before + * the block's power goes. Full masks: each word is written as traced. + */ + apple,tunable-gfx-fabric = + <0x714 0xffffffff 0x2000000>, + <0x718 0xffffffff 0x400080>, + <0x71c 0xffffffff 0x400080>, + <0x720 0xffffffff 0x400080>, + <0x724 0xffffffff 0x400080>, + <0x728 0xffffffff 0x400080>, + <0x72c 0xffffffff 0x400080>, + <0x730 0xffffffff 0x400080>, + <0x734 0xffffffff 0x400080>, + <0x0 0xffffffff 0x11>, + <0x10 0xffffffff 0xd0000>, + <0x14 0xffffffff 0x1>, + <0x18 0xffffffff 0x1>, + <0x1c 0xffffffff 0x3>, + <0x20 0xffffffff 0x3>, + <0x24 0xffffffff 0x3>, + <0x28 0xffffffff 0x3>, + <0x2c 0xffffffff 0x3>, + <0x400 0xffffffff 0x40a10001>, + <0x600 0xffffffff 0x1ffffff>, + <0x800 0xffffffff 0x101>, + <0x410 0xffffffff 0x1100>, + <0x420 0xffffffff 0x1100>, + <0x430 0xffffffff 0x1100>, + <0x8000 0xffffffff 0x9>, + <0x820 0xffffffff 0x80>, + <0x8008 0xffffffff 0x7>, + <0x8014 0xffffffff 0x1>, + <0x8018 0xffffffff 0x1>, + <0x748 0xffffffff 0x1>, + <0x8208 0xffffffff 0x2>, + <0x8280 0xffffffff 0x20>, + <0x8288 0xffffffff 0x3>, + <0x828c 0xffffffff 0xc>, + <0x8290 0xffffffff 0x18>, + <0x8294 0xffffffff 0x30>, + <0x8298 0xffffffff 0x78>, + <0x829c 0xffffffff 0xf0>, + <0x82b8 0xffffffff 0x7>, + <0x82bc 0xffffffff 0x1>, + <0x82c0 0xffffffff 0x7>, + <0x7a8 0xffffffff 0x1>, + <0x820c 0xffffffff 0x5>, + <0x8284 0xffffffff 0x20>, + <0x82a0 0xffffffff 0x3>, + <0x82a4 0xffffffff 0xc>, + <0x82a8 0xffffffff 0x18>, + <0x82ac 0xffffffff 0x30>, + <0x82b0 0xffffffff 0x78>, + <0x82b4 0xffffffff 0xf0>, + <0x82b8 0xffffffff 0x7>, + <0x82bc 0xffffffff 0x2>, + <0x82c0 0xffffffff 0x7>, + <0x8210 0xffffffff 0x0>, + <0x8408 0xffffffff 0xd>, + <0x8418 0xffffffff 0x3>, + <0x841c 0xffffffff 0x0>, + <0x8420 0xffffffff 0xffffffff>, + <0x8424 0xffffffff 0x0>, + <0x8428 0xffffffff 0xfff>, + <0x82b8 0xffffffff 0x7>, + <0x82bc 0xffffffff 0x4>, + <0x82c0 0xffffffff 0x7>; + apple,tunable-disp0-fabric = + <0x0 0xffffffff 0x11>, + <0x10 0xffffffff 0x610000>, + <0x14 0xffffffff 0x8>, + <0x18 0xffffffff 0x8>, + <0x1c 0xffffffff 0x18>, + <0x20 0xffffffff 0x18>, + <0x24 0xffffffff 0x1>, + <0x28 0xffffffff 0x1>, + <0x2c 0xffffffff 0x18>, + <0x30 0xffffffff 0x18>, + <0x34 0xffffffff 0x18>, + <0x400 0xffffffff 0x40a10001>, + <0x600 0xffffffff 0xaab>, + <0x700 0xffffffff 0x1ffffff>, + <0x8a8 0xffffffff 0x2000005>, + <0x8ac 0xffffffff 0x14d004e>, + <0x8b0 0xffffffff 0x14d004e>, + <0x8b4 0xffffffff 0x14d004e>, + <0x8b8 0xffffffff 0x14d004e>, + <0x8bc 0xffffffff 0x14d004e>, + <0x8c0 0xffffffff 0x14d004e>, + <0x8c4 0xffffffff 0x14d004e>, + <0x8c8 0xffffffff 0x14d004e>, + <0x410 0xffffffff 0x1100>, + <0x420 0xffffffff 0x1100>, + <0x430 0xffffffff 0x1100>, + <0x8000 0xffffffff 0x9>, + <0xa20 0xffffffff 0x2>, + <0x8008 0xffffffff 0x7>, + <0x8014 0xffffffff 0x1>, + <0x8018 0xffffffff 0x1>, + <0x920 0xffffffff 0x3fff0001>, + <0x8208 0xffffffff 0x8>, + <0x8280 0xffffffff 0x20>, + <0x8288 0xffffffff 0x3>, + <0x828c 0xffffffff 0xc>, + <0x8290 0xffffffff 0x18>, + <0x8294 0xffffffff 0x30>, + <0x8298 0xffffffff 0x78>, + <0x829c 0xffffffff 0xf0>, + <0x82b8 0xffffffff 0x1>, + <0x82bc 0xffffffff 0x1>, + <0x82c0 0xffffffff 0x1>, + <0x860 0xffffffff 0x1>, + <0x820c 0xffffffff 0x5>, + <0x8284 0xffffffff 0x20>, + <0x82a0 0xffffffff 0x3>, + <0x82a4 0xffffffff 0xc>, + <0x82a8 0xffffffff 0x18>, + <0x82ac 0xffffffff 0x30>, + <0x82b0 0xffffffff 0x78>, + <0x82b4 0xffffffff 0xf0>, + <0x82b8 0xffffffff 0x3>, + <0x82bc 0xffffffff 0x2>, + <0x82c0 0xffffffff 0x3>, + <0x8210 0xffffffff 0x0>, + <0x8408 0xffffffff 0x3>, + <0x8418 0xffffffff 0x3>, + <0x841c 0xffffffff 0x0>, + <0x8420 0xffffffff 0xffffffff>, + <0x8424 0xffffffff 0x0>, + <0x8428 0xffffffff 0xfff>, + <0x82b8 0xffffffff 0x7>, + <0x82bc 0xffffffff 0x4>, + <0x82c0 0xffffffff 0x7>; + apple,tunable-dispext0-fabric = + <0x0 0xffffffff 0x11>, + <0x10 0xffffffff 0x6d0000>, + <0x14 0xffffffff 0x9>, + <0x18 0xffffffff 0x9>, + <0x1c 0xffffffff 0x1b>, + <0x20 0xffffffff 0x1b>, + <0x24 0xffffffff 0x1>, + <0x28 0xffffffff 0x1>, + <0x2c 0xffffffff 0x1b>, + <0x30 0xffffffff 0x1b>, + <0x34 0xffffffff 0x1b>, + <0x400 0xffffffff 0x40a10001>, + <0x600 0xffffffff 0xaab>, + <0x700 0xffffffff 0x1ffffff>, + <0x8a8 0xffffffff 0x2000005>, + <0x8ac 0xffffffff 0x14d004e>, + <0x8b0 0xffffffff 0x14d004e>, + <0x8b4 0xffffffff 0x14d004e>, + <0x8b8 0xffffffff 0x14d004e>, + <0x8bc 0xffffffff 0x14d004e>, + <0x8c0 0xffffffff 0x14d004e>, + <0x8c4 0xffffffff 0x14d004e>, + <0x8c8 0xffffffff 0x14d004e>, + <0x410 0xffffffff 0x1100>, + <0x420 0xffffffff 0x1100>, + <0x430 0xffffffff 0x1100>, + <0x8000 0xffffffff 0x9>, + <0xa20 0xffffffff 0x2>, + <0x8008 0xffffffff 0x7>, + <0x8014 0xffffffff 0x1>, + <0x8018 0xffffffff 0x1>, + <0x860 0xffffffff 0x1>, + <0x8208 0xffffffff 0x4>, + <0x8280 0xffffffff 0x20>, + <0x8288 0xffffffff 0x3>, + <0x828c 0xffffffff 0xc>, + <0x8290 0xffffffff 0x18>, + <0x8294 0xffffffff 0x30>, + <0x8298 0xffffffff 0x78>, + <0x829c 0xffffffff 0xf0>, + <0x82b8 0xffffffff 0x1>, + <0x82bc 0xffffffff 0x1>, + <0x82c0 0xffffffff 0x1>, + <0x800 0xffffffff 0x1>, + <0x820c 0xffffffff 0x3>, + <0x8284 0xffffffff 0x20>, + <0x82a0 0xffffffff 0x3>, + <0x82a4 0xffffffff 0xc>, + <0x82a8 0xffffffff 0x18>, + <0x82ac 0xffffffff 0x30>, + <0x82b0 0xffffffff 0x78>, + <0x82b4 0xffffffff 0xf0>, + <0x82b8 0xffffffff 0x3>, + <0x82bc 0xffffffff 0x2>, + <0x82c0 0xffffffff 0x3>, + <0x8210 0xffffffff 0x0>, + <0x8408 0xffffffff 0x3>, + <0x8418 0xffffffff 0x3>, + <0x841c 0xffffffff 0x0>, + <0x8420 0xffffffff 0xffffffff>, + <0x8424 0xffffffff 0x0>, + <0x8428 0xffffffff 0xfff>, + <0x82b8 0xffffffff 0x7>, + <0x82bc 0xffffffff 0x4>, + <0x82c0 0xffffffff 0x7>; + apple,tunable-dispext1-fabric = + <0x0 0xffffffff 0x11>, + <0x10 0xffffffff 0x6d0000>, + <0x14 0xffffffff 0x9>, + <0x18 0xffffffff 0x9>, + <0x1c 0xffffffff 0x1b>, + <0x20 0xffffffff 0x1b>, + <0x24 0xffffffff 0x1>, + <0x28 0xffffffff 0x1>, + <0x2c 0xffffffff 0x1b>, + <0x30 0xffffffff 0x1b>, + <0x34 0xffffffff 0x1b>, + <0x400 0xffffffff 0x40a10001>, + <0x600 0xffffffff 0xaab>, + <0x700 0xffffffff 0x1ffffff>, + <0x8a8 0xffffffff 0x2000005>, + <0x8ac 0xffffffff 0x14d004e>, + <0x8b0 0xffffffff 0x14d004e>, + <0x8b4 0xffffffff 0x14d004e>, + <0x8b8 0xffffffff 0x14d004e>, + <0x8bc 0xffffffff 0x14d004e>, + <0x8c0 0xffffffff 0x14d004e>, + <0x8c4 0xffffffff 0x14d004e>, + <0x8c8 0xffffffff 0x14d004e>, + <0x410 0xffffffff 0x1100>, + <0x420 0xffffffff 0x1100>, + <0x430 0xffffffff 0x1100>, + <0x8000 0xffffffff 0x9>, + <0xa20 0xffffffff 0x2>, + <0x8008 0xffffffff 0x7>, + <0x8014 0xffffffff 0x1>, + <0x8018 0xffffffff 0x1>, + <0x860 0xffffffff 0x1>, + <0x8208 0xffffffff 0x4>, + <0x8280 0xffffffff 0x20>, + <0x8288 0xffffffff 0x3>, + <0x828c 0xffffffff 0xc>, + <0x8290 0xffffffff 0x18>, + <0x8294 0xffffffff 0x30>, + <0x8298 0xffffffff 0x78>, + <0x829c 0xffffffff 0xf0>, + <0x82b8 0xffffffff 0x1>, + <0x82bc 0xffffffff 0x1>, + <0x82c0 0xffffffff 0x1>, + <0x800 0xffffffff 0x1>, + <0x820c 0xffffffff 0x3>, + <0x8284 0xffffffff 0x20>, + <0x82a0 0xffffffff 0x3>, + <0x82a4 0xffffffff 0xc>, + <0x82a8 0xffffffff 0x18>, + <0x82ac 0xffffffff 0x30>, + <0x82b0 0xffffffff 0x78>, + <0x82b4 0xffffffff 0xf0>, + <0x82b8 0xffffffff 0x3>, + <0x82bc 0xffffffff 0x2>, + <0x82c0 0xffffffff 0x3>, + <0x8210 0xffffffff 0x0>, + <0x8408 0xffffffff 0x3>, + <0x8418 0xffffffff 0x3>, + <0x841c 0xffffffff 0x0>, + <0x8420 0xffffffff 0xffffffff>, + <0x8424 0xffffffff 0x0>, + <0x8428 0xffffffff 0xfff>, + <0x82b8 0xffffffff 0x7>, + <0x82bc 0xffffffff 0x4>, + <0x82c0 0xffffffff 0x7>; + /* The ADT's no-restore-gpio-pins of gpio0 */ + apple,pinctrl = <&pinctrl_ap>; + apple,pinctrl-skip-pins = <114 115 119>; + status = "disabled"; + }; +}; + &gpu { compatible = "apple,agx-t6000", "apple,agx-g13x"; }; From 882f6fe23ccefbc10de597d20b3d255c7c25613e Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Thu, 3 Dec 2026 10:26:43 +0100 Subject: [PATCH 68/72] drm/apple: T6000: refuse a deep sleep when a DCP is not parked The park of a T6000 DCP in the late suspend can fail (the IOP does not go idle), and its firmware then keeps running. The sleep went on regardless: the sleep driver checks only the internal DCP's CPU before the commit, so an external DCP left running had its display block taken down under it by the entry. Return the park's result and refuse the deep sleep from the late suspend when it failed, with an error that says so. Nothing is powered down yet at that point, and the resume after the refusal finds the firmware still running, as after a failed park before. A park whose AP comes back ON after the failed IOP idle still refuses: that answer no longer overwrites the park's error. Signed-off-by: Andreas Fernandez --- drivers/gpu/drm/apple/dcp.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 216bd022f1fcc..e5e14b8270edd 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -10165,7 +10165,8 @@ static void dcp_s2r_park_unhold(struct apple_dcp *dcp) dcp->s2r_unheld = true; } -static void dcp_s2r_park(struct apple_dcp *dcp) +/* 0 once parked, or with no running firmware to park; else why it is still running */ +static int dcp_s2r_park(struct apple_dcp *dcp) { void __iomem *control = dcp->coproc_reg + APPLE_DCP_COPROC_CPU_CONTROL; u32 ctrl; @@ -10173,7 +10174,7 @@ static void dcp_s2r_park(struct apple_dcp *dcp) if (dcp->hw.adopt_live_session || IS_ERR_OR_NULL(dcp->rtk) || !dcp->coproc_reg || READ_ONCE(dcp->crashed) || !apple_rtkit_is_running(dcp->rtk)) - return; + return 0; dcp_s2r_park_unhold(dcp); ret = iomfb_s2r_link_sleep_v13_3(dcp); @@ -10190,9 +10191,10 @@ static void dcp_s2r_park(struct apple_dcp *dcp) * through the mailbox. */ if (dcp->s2r_ap_idle) { - ret = apple_rtkit_ap_power_timeout(dcp->rtk, true, 2000); - if (ret) - dev_err(dcp->dev, "s2r: AP not back ON after a failed park: %d\n", ret); + int err = apple_rtkit_ap_power_timeout(dcp->rtk, true, 2000); + + if (err) + dev_err(dcp->dev, "s2r: AP not back ON after a failed park: %d\n", err); dcp->s2r_ap_idle = false; } if (dcp->s2r_unheld) { @@ -10200,7 +10202,7 @@ static void dcp_s2r_park(struct apple_dcp *dcp) dcp->rx_held = true; dcp->s2r_unheld = false; } - return; + return ret; } apple_rtkit_detach_keep(dcp->rtk); ctrl = readl_relaxed(control); @@ -10209,6 +10211,7 @@ static void dcp_s2r_park(struct apple_dcp *dcp) dcp->sleep_fw_runs = false; apple_sleep_display_quiesced(dcp->dev, true); dev_dbg(dcp->dev, "s2r: parked, cpu control %#x\n", ctrl); + return 0; } /* @@ -13627,14 +13630,21 @@ static int dcp_platform_suspend(struct device *dev) /* * hw.s2r_park: a parked firmware only starts again once its block lost power. So it is parked * after every device's suspend: when one of those refuses the sleep, nothing has stopped it. + * A firmware that could not be parked refuses the sleep: the sleep driver checks only the + * internal DCP's CPU before the commit, and the display blocks go down under a running one. */ static int dcp_platform_suspend_late(struct device *dev) { struct apple_dcp *dcp = dev_get_drvdata(dev); + int ret = 0; if (dcp->s2r_park_due) - dcp_s2r_park(dcp); + ret = dcp_s2r_park(dcp); dcp->s2r_park_due = false; + if (ret) { + dev_err(dev, "display firmware not parked (%d), refusing deep sleep\n", ret); + return -EBUSY; + } return 0; } From 754dcee721492f8ff7c4d6f20244b3317c89e677 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Thu, 3 Dec 2026 10:26:49 +0100 Subject: [PATCH 69/72] drm/apple: T6000: set the HDMI output up again after a deep sleep A T6000 DCP parked for a deep sleep no longer holds its PHYs, so the sleep takes the HDMI output's ATC PHY and its display crossbar down with their power domain. The PHY sets its mode up again only at its next operation, and the crossbar driver keeps no state across the sleep, while the mux core still counts the HDMI route as selected. A display plugged in after such a sleep then gets no link. Set the fixed HDMI output up again at resume after a sleep that left the PHYs to genpd, with dcp_fabric_hdmi_reinit(), as main does after a deep sleep for an s2r_ext DCP: the converter powered, the PHY taken out of DP and back into it, and the crossbar output selected again. It runs with or without a display attached, and after a sleep given up too, where it only repeats the setup. Only a DCP with a fixed PHY and an HDMI HPD line in the device tree has an output to set up; s2idle and dry runs hold the PHYs and are unchanged. Signed-off-by: Andreas Fernandez --- 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 4190ff47db711..c7b270140b484 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -1093,6 +1093,8 @@ struct apple_dcp { bool rx_held; /* hw.s2r_park: the suspend got as far as the park, which waits for the late suspend */ bool s2r_park_due; + /* hw.s2r_park: the PHYs were left to genpd for the sleep, see dcp_platform_resume() */ + bool s2r_phys_unheld; /* hw.s2r_park, a Type-C output: its Type-C work stopped at PM_SUSPEND_PREPARE */ bool s2r_typec_off; /* hw.s2r_park: parked for the sleep (IDLE, CPU stopped), restarted by resume */ diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index e5e14b8270edd..7a68fa88bf9a1 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -13616,6 +13616,7 @@ static int dcp_platform_suspend(struct device *dev) * would keep its state while the commit lowers those parents under it. */ dcp->s2r_park_due = dcp_s2r_park_selected(dcp); + dcp->s2r_phys_unheld = dcp->s2r_park_due; if (!dcp->s2r_park_due) dcp_sleep_hold_phys(dcp); /* Last: the steps above still talk to the coprocessor. */ @@ -13727,6 +13728,16 @@ static int dcp_platform_resume(struct device *dev) /* The HDMI output's PHY and crossbar may have lost their setup in sleep. */ if (dcp->external_native) dcp_fabric_hdmi_reinit(dcp, "after system sleep"); + /* + * hw.s2r_park: the PHYs were not held, so a deep sleep took the fixed HDMI output's PHY + * and crossbar down with their power domain, and the crossbar driver keeps no state + * across it. Set them up again, a display attached or not, as main does after a deep + * sleep for an s2r_ext DCP. Also after a sleep given up: there it only repeats the + * setup. + */ + else if (dcp->s2r_phys_unheld) + dcp_fabric_hdmi_reinit(dcp, "after deep sleep"); + dcp->s2r_phys_unheld = false; /* Before the HPD IRQ comes back: an edge from now on is replayed. */ if (dcp->hdmi_hpd_irq) dcp_hdmi_unplugged_in_sleep(dcp); From a9d50dd3edc89cc01d7f2c9ec3cf08e3578d737e Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Thu, 3 Dec 2026 10:51:19 +0100 Subject: [PATCH 70/72] drm/apple: T6000: release an attached HDMI display for a deep sleep A T6000 deep sleep with an HDMI display attached never came back: the SoC reset during the entry. The DCP's suspend kept the display's link up, as for a sleep in which the firmware runs on, and then parked the firmware and let the sleep cut the PHY and the display block under that link, with the DP-to-HDMI converter still powered. For a sleep that parks the DCP, release the link of an attached display while the firmware still runs, report the display gone, and power the converter down, as macOS keeps it in sleep. A release whose answer comes late, after the firmware reported the display unplugged and set the PHY rate to 0, has taken the link down and counts as done. A link the firmware does not release refuses the deep sleep there, before anything is parked, with an error that says so. A release that timed out still goes on in the firmware, so the display is connected again then, as after a wake, and does not stay dark. At resume the converter is powered first, the output is set up again, and the display is connected again with dcp_fixed_hdmi_reconnect(), as main's dcp_s2r_restart_done() does for an s2r_ext DCP; a display unplugged meanwhile is reported gone. That runs from a work, as the T6040 link work does after its wake: the connect of a converter output waits out a link configuration this firmware does not send for it, and the resume does not wait for that. The next suspend lets a running connect finish first. The same runs after a sleep given up in any later step, so the display comes back then too. With no display attached, or for s2idle and dry runs, nothing changes. Signed-off-by: Andreas Fernandez --- drivers/gpu/drm/apple/dcp-fabric.c | 28 +++++++++ drivers/gpu/drm/apple/dcp-internal.h | 4 ++ drivers/gpu/drm/apple/dcp.c | 88 ++++++++++++++++++++++++++-- drivers/gpu/drm/apple/dcp.h | 2 + 4 files changed, 117 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/apple/dcp-fabric.c b/drivers/gpu/drm/apple/dcp-fabric.c index a01466c8ddc88..a3da98777a46e 100644 --- a/drivers/gpu/drm/apple/dcp-fabric.c +++ b/drivers/gpu/drm/apple/dcp-fabric.c @@ -1384,6 +1384,34 @@ void dcp_hdmi_suspend(struct apple_dcp *dcp) dcp_hdmi_release_unplugged_locked(dcp, NULL); } +/* + * hw.s2r_park: an HDMI display whose link is up as its firmware is parked for a deep sleep. + * The sleep cuts the PHY and the display block under the parked firmware, and with the link + * still up there the SoC resets. Release the link while the firmware runs, as for a T6030 + * native output, and power the converter down, as macOS keeps it in sleep. + * dcp_platform_resume() sets the output up again and connects the display, also after a + * sleep given up. A display that is gone is left to dcp_hdmi_suspend(). Returns 1 if the + * link was released, 0 if none was up, or the error that kept it up. + */ +int dcp_hdmi_sleep_release(struct apple_dcp *dcp) +{ + int ret; + + guard(mutex)(&dcp_typec_fabric_lock); + + if (dcp->active_typec_route || !READ_ONCE(dcp->dptxport[0].connected) || + !dcp->hdmi_hpd || gpiod_get_value_cansleep(dcp->hdmi_hpd) == 0) + return 0; + + ret = dcp_hdmi_link_release(dcp); + if (ret) + return ret; + if (dcp->dp2hdmi_pwren) + gpiod_set_value_cansleep(dcp->dp2hdmi_pwren, 0); + dev_dbg(dcp->dev, "HDMI: link released and converter off for the deep sleep\n"); + return 1; +} + /* * Release a fixed HDMI link unplugged while its HPD IRQ was masked. Hold * the fabric lock from the ownership check through the presence update so diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index c7b270140b484..cd5e436870f1a 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -1095,6 +1095,10 @@ struct apple_dcp { bool s2r_park_due; /* hw.s2r_park: the PHYs were left to genpd for the sleep, see dcp_platform_resume() */ bool s2r_phys_unheld; + /* hw.s2r_park: an HDMI display released for the sleep, see dcp_hdmi_sleep_release() */ + bool s2r_hdmi_released; + /* hw.s2r_park: connects that display again after the wake or a refused release */ + struct work_struct s2r_hdmi_link_work; /* hw.s2r_park, a Type-C output: its Type-C work stopped at PM_SUSPEND_PREPARE */ bool s2r_typec_off; /* hw.s2r_park: parked for the sleep (IDLE, CPU stopped), restarted by resume */ diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 7a68fa88bf9a1..7b97c0a142709 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -5618,6 +5618,22 @@ static void dcp_fixed_hdmi_reconnect(struct apple_dcp *dcp, bool poweroff_unplug } } +/* + * hw.s2r_park: the HDMI display released for a deep sleep (dcp_hdmi_sleep_release()), or one + * whose release was refused. Its HPD stayed high, so no edge connects it. As main's + * dcp_s2r_restart_done() does for an s2r_ext DCP, but from a work, as the T6040 link work runs + * after its wake: the connect of a converter output waits out the link configuration this + * firmware does not send for it (dcp_dptx_connect_raw()), which the resume must not wait for. + * A display unplugged meanwhile is reported gone instead. + */ +static void dcp_s2r_hdmi_link_work(struct work_struct *work) +{ + struct apple_dcp *dcp = container_of(work, struct apple_dcp, s2r_hdmi_link_work); + + dev_dbg(dcp->dev, "HDMI: connecting the display released for the sleep\n"); + dcp_fixed_hdmi_reconnect(dcp, false); +} + static int dcp_enable_dp2hdmi_hpd(struct apple_dcp *dcp) { if (dcp_is_typec_output(dcp)) { @@ -9281,6 +9297,7 @@ static int dcp_platform_probe_pipe(struct platform_device *pdev) INIT_WORK(&dcp->s2r_prewarm_work, dcp_s2r_prewarm_work); INIT_WORK(&dcp->t6040_link_work, dcp_t6040_link_work); INIT_DELAYED_WORK(&dcp->t6040_plug_wq, dcp_t6040_plug_work); + INIT_WORK(&dcp->s2r_hdmi_link_work, dcp_s2r_hdmi_link_work); INIT_DELAYED_WORK(&dcp->s2r_prewarm_off_wq, dcp_s2r_prewarm_off_work); mutex_init(&dcp->s2r_prewarm_lock); INIT_WORK(&dcp->bl_register_wq, dcp_work_register_backlight); @@ -9862,6 +9879,7 @@ static void dcp_platform_remove(struct platform_device *pdev) if (dcp) { cancel_work_sync(&dcp->s2r_prewarm_work); cancel_work_sync(&dcp->t6040_link_work); + cancel_work_sync(&dcp->s2r_hdmi_link_work); cancel_delayed_work_sync(&dcp->t6040_plug_wq); cancel_delayed_work_sync(&dcp->s2r_prewarm_off_wq); if (system_state == SYSTEM_RUNNING && dcp->sleep_host_stop && @@ -13588,13 +13606,38 @@ static int dcp_platform_suspend(struct device *dev) av_service_disconnect(dcp); if (dcp->hdmi_hpd_irq) { + int ret = 0; + + /* A display still being connected after the last wake first. */ + flush_work(&dcp->s2r_hdmi_link_work); disable_irq(dcp->hdmi_hpd_irq); /* - * The firmware runs through this sleep, so the HDMI output - * keeps its link as through DPMS off (see dcp_poweroff()), and - * resume lights it again through dcp_poweron(). Only a display - * that is gone is reported and released here. Releasing one - * that stays attached reports it unplugged, and nothing + * hw.s2r_park: a firmware parked for a deep sleep drives no link, + * so an attached display is released and its converter powered + * down (dcp_hdmi_sleep_release()); resume connects it again. A + * link that cannot be released refuses the sleep here, before + * anything is parked or powered down. The firmware may have + * dropped the display on the way (dcp_hdmi_link_release()): it is + * connected again as after a wake, and kept as it is otherwise. + */ + if (dcp_s2r_park_selected(dcp)) + ret = dcp_hdmi_sleep_release(dcp); + if (ret < 0) { + dev_err(dev, "HDMI link not released (%d), refusing deep sleep\n", ret); + enable_irq(dcp->hdmi_hpd_irq); + if (dcp->avep) + av_service_connect(dcp); + dcp_enable_typec_work(dcp); + queue_work(system_dfl_wq, &dcp->s2r_hdmi_link_work); + return -EBUSY; + } + dcp->s2r_hdmi_released = ret > 0; + /* + * Otherwise the firmware runs through this sleep, so the HDMI + * output keeps its link as through DPMS off (see dcp_poweroff()), + * and resume lights it again through dcp_poweron(). Only a + * display that is gone is reported and released here. Releasing + * one that stays attached reports it unplugged, and nothing * connects it again after the wake: its HPD stays high, so no * edge comes, and a CRTC userspace turned off for the unplug * never runs dcp_poweron(). A T6030 native output's resume sets @@ -13656,6 +13699,33 @@ void dcp_hdmi_disconnect(struct apple_dcp *dcp) dcp_dptx_disconnect(dcp, 0); } +/* + * As dcp_hdmi_disconnect(), but reported gone only once the firmware has released the link. + * In a release the firmware reports the display unplugged and sets the PHY rate to 0 + * (SET_LINK_RATE) before it answers: a call that timed out after both has released the link, + * and only its answer is late (AFK frees it then). A release that timed out is still asked + * for and goes on in the firmware, so the port takes a connect again either way. + * Returns 0 once released. + */ +int dcp_hdmi_link_release(struct apple_dcp *dcp) +{ + struct dptx_port *port = &dcp->dptxport[0]; + u32 unplugs = dcp_typec_fw_unplugs(dcp); + int ret = dcp_dptx_disconnect(dcp, 0); + + if (ret == -ETIMEDOUT) { + scoped_guard(mutex, &dcp->hpd_mutex) + port->connected = false; + if (dcp_typec_fw_unplugs(dcp) != unplugs && !READ_ONCE(port->link_rate)) { + dev_info(dcp->dev, "HDMI: link down before the release was answered\n"); + ret = 0; + } + } + if (!ret) + disconnected_hpd_event(dcp->connector); + return ret; +} + static int dcp_platform_resume(struct device *dev) { struct apple_dcp *dcp = dev_get_drvdata(dev); @@ -13704,6 +13774,9 @@ static int dcp_platform_resume(struct device *dev) if (READ_ONCE(dcp->ext_dead)) return 0; + /* A released display's converter first: it starts up while the firmware restarts. */ + if (dcp->s2r_hdmi_released && dcp->dp2hdmi_pwren) + gpiod_set_value_cansleep(dcp->dp2hdmi_pwren, 1); dcp_s2r_unpark(dcp); dcp->link_recovery_tries = 0; if (dcp_is_ext26(dcp)) { @@ -13738,6 +13811,11 @@ static int dcp_platform_resume(struct device *dev) else if (dcp->s2r_phys_unheld) dcp_fabric_hdmi_reinit(dcp, "after deep sleep"); dcp->s2r_phys_unheld = false; + /* The display released for the sleep, on the output just set up; not in the resume. */ + if (dcp->s2r_hdmi_released) { + dcp->s2r_hdmi_released = false; + queue_work(system_dfl_wq, &dcp->s2r_hdmi_link_work); + } /* Before the HPD IRQ comes back: an edge from now on is replayed. */ if (dcp->hdmi_hpd_irq) dcp_hdmi_unplugged_in_sleep(dcp); diff --git a/drivers/gpu/drm/apple/dcp.h b/drivers/gpu/drm/apple/dcp.h index c68f55fb41835..cad9c4aa7ec1a 100644 --- a/drivers/gpu/drm/apple/dcp.h +++ b/drivers/gpu/drm/apple/dcp.h @@ -213,8 +213,10 @@ bool apple_t8122_25g83_board(void); void dcp_typec_hybrid_first_dt(void); void dcp_fabric_hdmi_unplugged(struct apple_dcp *dcp); void dcp_hdmi_suspend(struct apple_dcp *dcp); +int dcp_hdmi_sleep_release(struct apple_dcp *dcp); void dcp_hdmi_unplugged_in_sleep(struct apple_dcp *dcp); void dcp_hdmi_disconnect(struct apple_dcp *dcp); +int dcp_hdmi_link_release(struct apple_dcp *dcp); void dcp_fabric_hybrid_kick(struct apple_dcp *dcp, unsigned int delay_ms); void dcp_hdmi_yield_link(struct apple_dcp *dcp); void dcp_link_recovery_schedule(struct apple_dcp *dcp); From 7da339113cba125eb49f099833993a42d1b48580 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Thu, 3 Dec 2026 12:06:44 +0100 Subject: [PATCH 71/72] drm/apple: T6000: park a Type-C display's crossbar output for a deep sleep A T6000 DCP parked for a deep sleep no longer holds its PHYs, so the sleep takes the ATC power domains down, and with them the display crossbar of a Type-C display's route. The crossbar wakes with its output lost while the mux core still holds the selection, and nothing selects it again: the link after the wake has no path to the PHY, the firmware reports link faults and the display does not come back. Take the route's crossbar output down at suspend while it still has power, and select it again at resume before the Type-C work connects the link, with dcp_typec_route_park() and dcp_typec_route_unpark(), as main does for an s2r_ext DCP and for an H17P DCP in s2idle. A sleep given up after the suspend selects it again the same way. Without a Type-C route, or for s2idle and dry runs, which hold the PHYs, nothing changes. Signed-off-by: Andreas Fernandez --- drivers/gpu/drm/apple/dcp.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 7b97c0a142709..822225881ca2a 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -13656,12 +13656,17 @@ static int dcp_platform_suspend(struct device *dev) /* * A firmware parked for the sleep drives no PHY, so the PHY's power domain is left to * genpd: off before the commit, on again after its parents at the wake. Held on, it - * would keep its state while the commit lowers those parents under it. + * would keep its state while the commit lowers those parents under it. The crossbar of + * a Type-C display's route shares that domain and wakes with its output lost, while the + * mux core still holds the selection: take the output down while it has power, and + * resume selects it again before the link connects, as for an s2r_ext DCP. */ dcp->s2r_park_due = dcp_s2r_park_selected(dcp); dcp->s2r_phys_unheld = dcp->s2r_park_due; if (!dcp->s2r_park_due) dcp_sleep_hold_phys(dcp); + else + dcp_typec_route_park(dcp); /* Last: the steps above still talk to the coprocessor. */ dcp_hold_for_sleep(dcp); /* hw.s2r_park: parked in the late suspend and restarted warm, never stopped cold. */ @@ -13795,7 +13800,7 @@ static int dcp_platform_resume(struct device *dev) dcp_release_after_sleep(dcp); iomfb_v14_7_backlight_resume(dcp); /* Its crossbar output, parked by the suspend, before the link connects again. */ - if (dcp_is_external_h17p(dcp)) + if (dcp_is_external_h17p(dcp) || dcp->s2r_phys_unheld) dcp_typec_route_unpark(dcp); dcp_enable_typec_work(dcp); /* The HDMI output's PHY and crossbar may have lost their setup in sleep. */ From 538b824fcbe0279e1d230d334a9ac54df52920e1 Mon Sep 17 00:00:00 2001 From: Andreas Fernandez Date: Thu, 3 Dec 2026 14:45:34 +0100 Subject: [PATCH 72/72] dmaengine: apple-sio: start the parked T6000 SIO again after a sleep given up The T6000 SIO firmware is idled and its CPU stopped in the late suspend phase, and only starts again from reset, which the deep sleep's power cut gives. A sleep given up after that point cuts no power, so the firmware stayed stopped until the next deep sleep, and DisplayPort and HDMI sound with it. On the resume of a sleep given up, pulse the SIO's engine reset, as the runtime resume does, and start the firmware the way the wake does. If the reset fails, the firmware stays parked as before and the log says so. With the sleep controller bound, the engine reset took only power domains in the PMGR block mapped as its "ps", and found none for the T6000 SIO, whose domain is in the east block. Map such a domain through its own PMGR, as the reset already does where no sleep controller is bound. Signed-off-by: Andreas Fernandez --- drivers/dma/apple-sio.c | 23 ++++++++++++++++------- drivers/soc/apple/sleep.c | 10 ++++++---- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/drivers/dma/apple-sio.c b/drivers/dma/apple-sio.c index 313d6762d8c03..b58858a0a08e2 100644 --- a/drivers/dma/apple-sio.c +++ b/drivers/dma/apple-sio.c @@ -3310,7 +3310,7 @@ static __maybe_unused int sio_park_kept_suspend_late(struct device *dev) struct sio_data *sio = dev_get_drvdata(dev); int ret; - /* Still stopped from a sleep that was given up, or left alone */ + /* Still stopped from a sleep whose restart failed, or left alone */ if (sio->fw_state != SIO_FW_RUNNING) return 0; @@ -3339,10 +3339,19 @@ static __maybe_unused int sio_park_kept_resume(struct device *dev) if (sio->fw_state != SIO_FW_PARKED) return 0; - /* Given up on the way down: stopped it stays, until a sleep that cuts the power */ + /* + * Given up on the way down, the power was not cut, and a stopped firmware only + * starts again from reset: pulse the engine reset, as the runtime resume does, + * then start it as after the power cut. + */ if (!pm_resume_via_firmware()) { - dev_warn(dev, "no DisplayPort sound until the next sleep: this one was given up\n"); - return 0; + ret = apple_sleep_engine_reset(dev); + if (ret) { + dev_err(dev, "sleep given up and the firmware not reset (%d): no DisplayPort sound until the next sleep\n", + ret); + return 0; + } + dev_info(dev, "sleep given up: firmware reset, starting it again\n"); } ret = apple_rtkit_reinit_keep(sio->rtk); @@ -3379,9 +3388,9 @@ static __maybe_unused int sio_system_resume(struct device *dev) /* * As this sleep's late suspend went: sio_park_kept() holds for the whole suspend - * cycle. A firmware still parked by an earlier, refused deep sleep stays stopped - * through an s2idle or a dry run, sends nothing (-ESHUTDOWN) and starts again after - * the next power cut. + * cycle. A firmware still parked by an earlier deep sleep whose restart failed stays + * stopped through an s2idle or a dry run, sends nothing (-ESHUTDOWN) and starts again + * after the next power cut. */ if (sio_park_kept(sio)) return sio_park_kept_resume(dev); diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 2b31943376f99..089ea6744ee9b 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -3527,7 +3527,10 @@ static bool sleep_ps_domain(struct apple_sleep *s, struct device_node *pd, u32 * return ours; } -/* Map a PMGR power-state word directly when apple_sleep is not bound on this board. */ +/* + * Map a PMGR power-state word directly: when apple_sleep is not bound on this board, or + * the domain is in another PMGR block than the one mapped here as "ps". + */ static void __iomem *sleep_map_pmgr_domain(struct device_node *pd, void __iomem **map) { struct device_node *parent; @@ -3592,11 +3595,10 @@ int apple_sleep_engine_reset(struct device *dev) void __iomem *map = NULL; void __iomem *reg; - if (s) { - if (!sleep_ps_domain(s, it.node, &val)) - continue; + if (s && sleep_ps_domain(s, it.node, &val)) { reg = sleep_power_reg(s, val); } else { + /* No sleep engine here, or a domain outside its block (T6000 SIO: east) */ reg = sleep_map_pmgr_domain(it.node, &map); if (!reg) continue;