Skip to content

soc: apple: T8142 (M5, J815/J813) hibernation support - #4187

Draft
m0d16l14n1 wants to merge 110 commits into
aurora-silicon:mainfrom
m0d16l14n1:j815/t8142-hibernation
Draft

m0d16l14n1 wants to merge 110 commits into
aurora-silicon:mainfrom
m0d16l14n1:j815/t8142-hibernation

Conversation

@m0d16l14n1

Copy link
Copy Markdown
Contributor

What: Add full S4 hibernation (suspend-to-disk) driver support for T8142 (M5 MacBook Air J815/J813) across GPU (G17G M5), Display (DCP firmware-27), PMP/RTKit/AIC/PMC/MCC/cluster-pwrgate, N1 Wi-Fi/BT + MSI, MTP/DockChannel HID, USB-C PHY/PD/Thunderbolt, Audio/AOP (under speakerguardd v2), H17 ANE, SEP, and peripheral buses (I2C/SPI/PWM/CPUFreq/SMC). Stacked on #2977 (merged at 3f03f90e1ccc in d6819c59956b; please review only the 32 commits above d6819c59956b).
Tested: Build and host-test verified at 86ece55ea32a: full arm64 kernel + modules + DTBs build (Image a1d1cb7927d9002c, 0 new warnings, clean depmod), scripts/checkpatch.pl 0 errors / 0 warnings on the audit series, and 29/29 runnable host test suites under tools/asahi/ PASS (12/12 re-verified after the final commit).
Risk: two-way door · S4 hibernation paths primarily on T8142 (M5 J815/J813) plus shared Apple Silicon hibernation restore helpers (irq-apple-aic, pmp, pmp-report, rtkit, apple-cluster-pwrgate, mcc, i2c-pasemi, spi-apple, pwm-apple, dcp, asahi, tas2764, cs42l84, mca, admac, nco, centauri).
Not yet: Live J815/J813 on-device test_resume, reboot, and shutdown hibernation runs (with speakerguardd-v2 and regression checks for 0 SELinux denials, 0 failed units, and 0 new kernel errors), and rebasing onto the latest #2977 head (03ac74762d18) before leaving draft.

Details

This draft series builds on the core hibernation infrastructure in #2977 (3f03f90e1ccc) and adds the remaining driver freeze/poweroff/thaw/restore paths required for a complete T8142 (J815/J813) cold wake:

  • Platform core (312688326ba6, 738126b53beb, 388fdad4bc2d, e93ba184d112, 86ece55ea32a):
    • pmc: restore voter enables and floors in .restore_early.
    • apple-cluster-pwrgate: add syscore .resume to restore cluster power-gate words after cold hibernation wake so post-hibernation S2R does not hang.
    • mcc: add .restore_noirq to re-enable SLC allocation on cold restore.
    • irq-apple-aic: restore global config and boot CPU FIQ mask when pm_hibernate_restored_cold() is set; clear stale mbox_lost in apple-sleep on valid remap.
    • pmp / pmp-report / pmp-export: replay driver-owned power votes and DVFS floors via apple_pmp_report_wake(pmp_device, false) after cold PMP restart; keep apple,release-for-sleep entries off hibernation .restore_noirq while PMP is still halted; guard apple,pio-ranges against -EEXIST re-mapping on cold restore.
    • rtkit: call apple_mbox_restore() after apple_mbox_start() in apple_rtkit_reinit*() so mailboxes with hardware enable/IRQ bits re-enable them when mbox->active is already set in the restored image.
  • M5 GPU G17G (828ea30814e5, bb3e60aac355, bfe8c4a36cfc, e89b40227db9):
    • Implement hibernation freeze and M5 cold-boot firmware republish (asahi_j813_power_on_gfx(), region_a reset, cached effort/hold/governor reset, published_completions reset).
    • Mirror live ExecutionContext TTBs in UatShared with a cold_booting gate armed before thaw_processes() so concurrent client closes cannot race republish_user_contexts() or poison slots.
    • Settle ascwrap_v6_cpu_running via stopped_elsewhere() in Coprocessor::cold_boot() so cpu.start() does not return -EBUSY, and clear LAST_FRAGMENT / LAST_TASK_FRAGMENT + Pair/ParkedPair::executed on cold boot to prevent cross-pair render deadlocks on J813/J815.
  • Display v27 DCP (2ab4584f43fc, 844507a00fe7, d23fb7d89c0b, cd99b885d6e5, 0d963464e33b, dd4b1cdb1f75):
    • Pause live-session DCP on freeze and preserve across warm test_resume; add in-place hibernation restore for firmware-27 panels; stop EDR ramp timer on freeze; add explicit apple_platform_poweroff() avoiding double drm_mode_config_helper_suspend(); clear secure_answered on quiesce and hib_poweroff_entered on thaw; delegate dcp->external restore to dcp_platform_resume().
  • N1 Wi-Fi / BT & PCIe MSI (6c475319d3e0, 9381eb6354de, 9587c034cbde):
    • Restore pcie-apple-msi doorbell/map in .restore_noirq; support Centauri Wi-Fi/BT hibernation freeze and cold-ROM recovery (skip absent secondary functions 1/2 in single-function ROM personality, free BT arena cleanly).
  • Keyboard & Trackpad (29c4098b6194, cc02f2ea7a33):
    • Restore DockChannel IRQ masks and guard receive across hibernation; use an ordered freezable reprobe workqueue in aura-hibernate to restart halted self-started MTP (rtkit-helper) and follow-on dockchannel-hid reprobe (re-uploading touch firmware).
  • Audio & AOP (4edc6aa0ea25, 23d1f70d298b, 5d0565aaec82, 851e410c4b52, 942803c26f15):
    • Save/restore NCO channels, mark ADMAC channels lost on cold restore, save/restore active MCA clusters, fail-closed POR restore for CS42L84 and TAS2764 (keeping speaker protection under speakerguardd-v2), and reprobe halted AOP after cold restore.
  • USB-C / Thunderbolt / ANE / SEP / Buses (acac575d796c, 744af9e43306, 84b488dfc10b, abc4e033718f, 2b7161347524, af9bdf6c452b, cc5ba7f64180):
    • Restore ATCPHY state, refresh SN201202x LDCM/wake, cycle H17 ANE across hibernation, gate SEP rebind to cold restore, clear Thunderbolt keep-alive quirks on cold restore, add system sleep/hibernation restore for i2c-pasemi, spi-apple, and pwm-apple (with PWM update latch), refresh CPUFreq expected_pstate / fast_die timer, and save/restore macsmc-power charge limits.
Evidence
  • Branch base: origin/main + merged soc: apple: T6040 (M4 Pro, J616s) hibernation #2977 (3f03f90e1ccc, merge commit d6819c59956b), followed by 32 commits (312688326ba6..86ece55ea32a).
  • Companion bootloader PR: kboot: enable opt-in hib=1 fixed hibernation layout on T8142 aura#660 (T8142 hib=1 fixed layout).
  • Full arm64 Docker kernel build (7.3.0-aurora-j815-dev, CONFIG_HIBERNATION=y, CONFIG_APPLE_AURA_HIBERNATE=y, CONFIG_DRM_ASAHI_M5=y):
    • Image sha256: a1d1cb7927d9002ca9c2f8940647be1a49a5bbc76528a6d64efa74b9d5d69608
    • dtbs/t8142-j815.dtb sha256: 617b5ed2324f3b785f6998506340ee58d423f77b97e07118123157e251df236c
    • dtbs/t8142-j813.dtb sha256: 07e31f1adf0d3830a5e7bff1e0497a82b9c5b7b374e40bf678d8e7ae34ed75db
    • Clean depmod, 0 new compiler/Rust warnings across all touched files.
  • scripts/checkpatch.pl on the 15-commit audit series (bb3e60aac355..86ece55ea32a): 0 errors, 0 warnings.
  • Host test suites (tools/asahi/*-tests.py): 29/29 runnable suites PASS, including 12/12 re-verified at 86ece55ea32a (g17-m5-effort-tests.py, g17-m5-governor-stop-tests.py 5/5, g17-m5-idle-housekeeping-tests.py, g17-m5-render-fast-done-tests.py, m5-client-handoff-tests.py, pmp-native26-tests.py, pmp-floor-tests.py, display26-tests.py, rtkit-retained-tests.py, apple-router-sleep-tests.py, apple-wdt-tests.py, pcie-adopted-clkpm-tests.py).

The kernel hibernates on Apple silicon only where the boot object says,
in /chosen, that it keeps the same memory layout on every boot, and it
reads the device tree of the resuming boot at the address the boot
object gives there. Neither property had a binding, and their prefix
was not a registered one.

Register the prefix and describe the two properties. The prefix is the
name of the boot loader that defined them, as u-boot's is: its released
versions publish the properties under this name, so a kernel that
looked for another one would not hibernate with any of them.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit c2119e1)
A REQ_SYNC write can sit in a volatile device cache, so the signature
reset at resume and the end of the image are not on the medium when the
write completes. A power loss after the restore then resumes the same
image again, or the image is incomplete.

Write the signature reset with FUA, flush the image in
swap_writer_finish() and fail the hibernation (dropping the signature)
when the flush fails. Once the image is in memory, clear the signature
again before the devices are restored, so a restore that hangs cannot be
resumed into a second time.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit faaf017)
The devices of a kernel restored from an image get PMSG_RESTORE whether
the image was loaded by another boot (the firmware and the boot loader
reset the hardware) or by the kernel that wrote it (test_resume: the
hardware kept its state). Drivers of devices that a boot resets need to
tell the two apart.

The kernel that loads an image leaves a record in the no-save section,
which the image neither saves nor overwrites, and the restored kernel
reads it: pm_hibernate_restored_cold() returns 1 after a load by another
boot, 0 after a load of the own image. pm_hibernate_is_restored() says
whether the kernel was restored at all, and pm_hibernate_is_loading()
whether this kernel is loading an image, so freeze callbacks can tell a
quiesce for the jump into an image from a freeze for an image of their
own. A hibernation aborted before the image exists resumes the devices
with PMSG_RECOVER.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit 394d4a1)
Hibernation is refused on every Apple platform, because the drivers of
their co-processors have no path to rebuild their state after the cold
boot of a restore. Add hibernation_set_platform_check() so platform code
can tell when that is handled for the running boot. Without a registered
check Apple platforms keep refusing images as before.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit 3891bc4)
Most image pages are loaded straight into their original frames through
the cacheable linear map, not through the copy loop. Add an architecture
hook, called for every valid image frame once the whole image is loaded
and while the list of those frames is still intact, and implement it on
arm64 by cleaning and invalidating the page to the PoC. Frames outside
the boot kernel's linear map are left to the copy, which cleans them.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit 7089f48)
swsusp_arch_suspend_exit() cleans every copied page to the PoU, which is
enough for instruction fetch but not for DMA masters that do not snoop
the CPU caches. A later eviction of a dirty line can then overwrite what
such a device wrote since the restore. Clean to the PoC instead.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit d98e601)
…ayout

The image restores every page to the frame it came from, so the boot
that resumes it must give the kernel what the boot that wrote it did:
the same RAM, the same CPUs started the same way, and any fixed layout
of the boot loader. The kernel build check of the image header cannot
see these.

Let one platform register hooks: pfn_nosave() keeps ranges that the boot
owns out of the image, and layout() mixes the platform's boot facts into
a boot layout cookie that is stored in the image header next to the
memory regions and the spin-table release addresses. A restore under
another layout is refused.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit 54d9b8e)
…ernation

Spin-table CPUs cannot be powered off; a parked CPU spins in kernel text
that the boot kernel overwrites while it loads an image. Let them wait in
a small pen outside the image instead, entered with the image's page
tables, and have the restored kernel release them from it. A boot CPU
whose node has no enable-method is treated as one that can leave the
kernel.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit b6838fb)
… sleep

A guest's counters are the physical counter minus an offset fixed when
the VM is made. The physical counter starts again across a resume from
disk, which is a cold boot, so the guest's counters fall back and its
clock stands still until the counter has caught up.

Record the counter at syscore suspend and, if it went backwards at
resume, move the offsets of every VM by what it lost, so the guest's
counters continue as for a VM that was stopped that long. A counter that
keeps running through the sleep is left alone.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit ac2fa3d)
A co-processor that stays up across a resume from disk keeps the shared
buffers of its session, which the restored kernel has to adopt. Add
apple_rtkit_describe_buffers() and a small hand-over area in the no-save
section in which the boot kernel leaves, per tagged session, what the
restored kernel needs to take it over.

Also let the polled power-state exchange used with interrupts off put the
co-processor to idle for clients that stay up, and add
apple_rtkit_reinit() to the Rust bindings.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit f986895)
The boot kernel of a resume from disk holds RX of an adopted session and
hands it over with its buffers; the restored kernel adopts it again in
.restore. A self-started session is left for a reload, and a
co-processor that the boot left halted is released without a handshake.
Add .thaw and .restore callbacks and apple_rtkit_helper_readopted().

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit c8d3761)
aura publishes /chosen/aura,hibernate-layout on a boot whose memory
layout below Linux is the same on every boot, and copies the device tree
of each boot to the slot named by /chosen/aura,hibernate-dt.

On such a boot, keep every /memreserve/ and non-reusable /reserved-memory
range except the kernel image out of the image, add the layout and the
boot facts of /chosen to the boot layout cookie, and let hibernation
through. Without the property hibernation and resume stay refused on
Apple platforms. Also provide this boot's /chosen values and a deferred
re-probe for drivers whose co-processor the resuming boot left halted.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit 74accee)
After a resume from disk through a boot the boot loader has cold-restarted
the PMP (its image resident in SRAM, the co-processor halted) and nothing
replays the probe-time bring-up, so the firmware stays down: the display
gate and cpufreq wait for a PMP that never reports running. The module
cannot be reloaded, since the power-vote bridge is retained until reboot.

Rebuild the session in place on PM_POST_HIBERNATION, the event the
restored kernel sends (PM_POST_RESTORE only reaches the loading kernel when
it fails to restore), from a notifier registered ahead of every consumer's
restore notifier (ANE, DCP, GPU). The decision follows the co-processor's
run bit, the state the loading kernel or the boot left: a running firmware
(restored by the kernel that wrote the image, or an aborted hibernation)
is left alone; a halted one gets its RTKit session reinitialised, its SRAM
boot arguments patched again and the boot handshake driven as at probe.

The notifier is registered outside pmp_lock, since the notifier chain lock
is taken around the callback, which takes pmp_lock. The revocable MMIO
guards are held only around the register accesses: they hold an RCU
read-side lock, which must not be kept across the sleeping RTKit
handshake.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit 0833903)
…m disk

A resume from disk through a boot finds every locked DART locked again by
the boot loader with fresh firmware-owned roots, while the restored kernel
still describes the roots of the image (their mapping, their firmware
snapshot and the slots it had published). Every publication then fails
with -EBUSY, and with it every DMA mapping through the DART; on the M4 Pro
the PMP and every DCP failed their buffer requests with -ENOMEM.

Remember the physical address of each mapped locked root and the page
table configuration last published into each locked stream. In .restore,
read the live roots back: a root that moved is re-imported the way attach
maps it, in two passes as attach does (every stream's roots are re-read
and snapshotted before any of them is republished, so a root shared by
several streams is snapshotted before Linux slots enter it), then the
stream's page tables are published into it again. A root that did not
move, as when the kernel that wrote the image loads it, is the one the
image describes and is kept. Firmware-owned memory is never written: the
old roots' published slots are forgotten, not retired. A failed stream is
logged and does not fail the whole restore.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit b7fa3a1)
After a resume from disk the RTKit session the restored driver describes
is not the one the SMC is in, and every SMC command times out (battery,
keyboard and trackpad preflight, power-off).

The kernel that loads the image binds this driver like any boot and holds
a live session when it jumps into the image; the restored kernel then asks
the firmware to start and waits for a HELLO that a firmware already in a
session never sends. Following Documentation/driver-api/pm/devices.rst,
leave the restored kernel a defined state:

- the loading kernel (pm_hibernate_is_loading()) quiesces the SMC in
  .freeze, the state the normal teardown leaves it in, from which a wake
  starts a new session. A failed quiesce still enters the image, whose
  signature is already gone; its restore tries a new session;
- .restore reinitialises the RTKit transport, replays probe's bring-up
  handshake, re-reads the key count and re-arms the notification flag,
  the same whether the loading kernel wrote the image or was booted;
- .thaw after a hand-over that could not enter the image starts a new
  session the same way.

Probe's handshake and key count read move into helpers both use.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit 216072f)
…effect

The next power-off handler is PSCI SYSTEM_OFF. Under a boot monitor
without a power-off path it resets the machine instead, so a slow or
refused off1 must not fall through after only 100 ms: the image written
for hibernation would be resumed at once, or the machine would reboot
instead of powering off.

Retry a refused off1 up to three times, and give an accepted one five
seconds before warning and falling through.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit 6ea1f04)
The prepare callback refuses every transition that is neither
suspend-to-idle nor a firmware-driven suspend, because only those keep
a way to bring the alarm back. Hibernation runs the same callback for
its freeze, but with no suspend target set, so it is refused as well:
on the M4 Pro (J616s) a hibernation pm_test run stops at this device
("apple_pmu_rtc_prepare returns -16") before any device is frozen.

A hibernated machine comes back through a cold boot, which needs
nothing from the alarm. Refuse only a suspend state that cannot keep
the alarm, and let a transition without a suspend target through.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit 0c1fbf5)
…P stops

A resume from disk restarts the PMP after it had run. The fast
die-temperature wait then reported the PMP as late, measured from the
first boot. Start the wait, and its warning, over when the PMP stops.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit 0148e6b)
On post-M4 ANS a resume from disk through a boot is a cold boot: the boot
firmware restarted ANS, the boot stages kept it running, and the loading
kernel adopted that session to read the image. In the restored kernel
anv->rtk describes a firmware instance that no longer exists, and the live
session on the mailbox has never been adopted by this kernel.

Give the driver explicit hibernation callbacks: freeze and poweroff
disable the controller and keep the session like suspend, thaw resumes.
A .restore through a boot (pm_hibernate_restored_cold()) stops the
controller first (until CC.EN is clear the firmware may fetch from the
loading kernel's queues), releases the stale session without a shutdown
handshake (the new instance would receive it), checks that a running,
booted, enabled-off controller is there and adopts it, as probe adopts
the boot loader's session, before the usual resume. A failure
quarantines the controller. A .restore from the kernel that wrote the
image finds the controller as its .freeze left it and resumes.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit 594d672)
An H16 engine whose firmware has been started refuses hibernation and
restore from its PM notifier, even on a platform that sleeps it cold.
On the M4 Pro (J616s) that refusal is the first thing that stops a
hibernation pm_test run, before the freezer level is reached.

The cold cycle leaves nothing of the old firmware behind: the engine is
powered off with nothing in flight, and after the transition the
firmware boots again from the preload and every open program is
replayed. Across suspend this already runs on the J616s ("powered down
for system sleep", then "resume ok: cold" after every wake). An image
needs no more than that, so on a platform without a warm sleep map
PM_HIBERNATION_PREPARE and PM_RESTORE_PREPARE to the suspend prepare
step and PM_POST_HIBERNATION and PM_POST_RESTORE to the wake step.

A platform that sleeps the engine warm keeps firmware state across the
transition that an image cannot carry; there both stay refused.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit 78dac96)
The DCPs freeze and thaw through their system sleep callbacks, but the
DRM device had only .suspend and .resume. A hibernation freeze therefore
stopped the display firmware under an active mode configuration: the
HDMI link of the external DCP went down in .freeze, the CRTC was never
committed again on .thaw, and the HDMI output stayed disconnected until
the next suspend and resume.

Use the system sleep callbacks for the hibernation phases as well, so
the DRM state is saved before the DCPs stop and committed again after
they restart, as for suspend to RAM.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit 1d4dbc5)
Only the H17P handler marked a buffer mapped for piodma. On desktop 26.x
the mark was never set, so neither a release nor the session drop after a
resume from disk took the translation down, and the second resume from
disk in one boot failed every piodma mapping with -EEXIST: no picture on
the internal panel. Mark it there too, and refuse an unmap of a buffer
that is not mapped, as on H17P, instead of reaching the page table.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit dbbd96e)
…ttached

Without a sink the firmware's probing of a DP port fails by design, which
logged a warning on every external processor start, including each
restart after a resume from disk. Log those at debug level and keep the
warning when a sink is attached.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit 4e27f7d)
…from disk

A resume from disk is a cold boot: the boot loader restarts the DCP
firmware and lights the panel with its boot framebuffer, while the
restored driver still holds the session of the previous firmware
instance. Until now the driver had no hibernation callbacks, and the
display was lost until a reboot.

Rebuild the session in place, the way probe builds it, for the desktop
26.x internal DCP and for the T6041 external display processors:

- prepare (hibernation only): stop the link work and the HDMI HPD
  interrupt, stop a merged dual pipe as a pair, and give the external
  display link and the DP PHYs back as at a runtime unplug, since the
  cold boot resets the PHY and the image must record it off;
- freeze: hold RTKit receive, so whatever the new firmware sends before
  .restore stays in the mailbox instead of reaching the old session;
- restore_early/restore: drop the old session on the host side (mode
  invalid, endpoints quiesced, IOMFB channels, firmware mappings, retained
  framebuffers and pending flips released; DMA only after the DARTs are
  restored). From here the DRM entry points leave the firmware alone and a
  flip completes without a swap; a shown external display is reported
  gone so that its return is a hotplug;
- PM_POST_HIBERNATION, after the PMP is rebuilt and before the GPU drain,
  in a work item: run the co-processor, reinitialise RTKit and start the
  firmware as probe does, power the pipe on and commit the current DRM
  state. External processors with a display wait until every external
  processor runs, so a display wider than one pipe finds its peer, and
  then connect their link through the runtime hotplug path; the HDMI
  converter is powered again when the link comes back.

Every devm allocation, the DMA arena (cleaned to the PoC before the
restart), the IOMMU mappings, clocks and registers are reused; no probe
primitive runs again. The old endpoint objects stay allocated until unbind
(a bounded leak per resume) because their services may still be
referenced. Any failing step falls back to the previous behaviour: IOMFB
traffic stopped, display left to the boot loader, until the reboot. The
teardown at the hibernation power-off is skipped, since the firmware's own
power sequence may crash the DCP; the panel keeps the last frame until the
machine powers off. If the hibernation fails, PM_POST_HIBERNATION replays
the external links once every DCP is thawed.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit 0dfc337)
A sink that drops its link and raises IRQ_HPD after every training (seen
with a dual-pipe monitor after a modeset from the kernel console during a
hibernation entry) turns each hotplug into a retrain and each retrain into
a hotplug, every 0.4-1.2 s, until the Type-C controller gives up on the
cable.

Retrain at most DCP_HOTPLUG_RETRAIN_MAX times in a
DCP_HOTPLUG_RETRAIN_WINDOW that starts with the first retrain and is not
extended by refused requests; at the end of the window one more retrain
is tried. A real replug resets the bound. Past the limit the link status
stays as it is and userspace still gets the hotplug events.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit 1836402)
Hibernation restored the display firmware with a new handshake only, which
assumes the boot loader started it anew. A restore by the kernel that wrote
the image (test_resume) finds the firmware still in the session it ran on
after the image was taken, and the handshake times out on every DCP:

  RTKit: endpoint discovery failed: -62
  hibernation restore: firmware restart failed at start (-62)

Per Documentation/driver-api/pm/devices.rst, .restore has to cope with
whatever state the loading kernel left. Stop the display firmware at
.freeze with the stop the suspend-to-RAM path already uses on the T604x
display complex (dcp_t6041_s2r_all(), selected by the apple,display-power
node), so the image holds it stopped by Linux, and keep it stopped through
the image write, as amdgpu's .thaw does: .thaw brings it back only when
the hibernation failed (pm_hibernate_is_recovering()) or the machine
suspends instead (pm_hibernation_mode_is_suspend()), and
PM_POST_HIBERNATION does when this kernel goes on after anything else.
The firmware then is in exactly the state the image describes:

 - a .restore by the kernel that wrote the image (test_resume), or after
   a failed power-off, takes the sleep's warm return;
 - a .restore through a boot (pm_hibernate_restored_cold()) powers the
   domains and DARTs the same way, then gives the firmware the boot loader
   started the new handshake after PM_POST_HIBERNATION.

A thaw that restarted the firmware for the image write left it in a
newer session than the image: the restored kernel's warm return then
crashed all three DCPs with ASSERT afk_messenger_common.c:67.

The link release at hibernation prepare is not needed then, as the stop
takes the external links down itself. Factor the host-side session drop
into dcp_hib_drop_session() for both restore paths.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit 223277a)
A test_resume load restores the image in the kernel that wrote it: the
SoC was not powered down and the PHY, its coprocessor and the DP and USB
sides its consumers kept are as the image records them. Resetting that
state, as after a resume through a boot, made the DP side of a Type-C
display look down while the display controller still held it, and the
next mode set of that display failed.

Only reset the state, and the coprocessor's RTKit session, when
pm_hibernate_restored_cold() says the restore went through a boot (or
cannot tell), as a resume from a sleep does only after the firmware took
the SoC down (pm_resume_via_firmware()).

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit 2786460)
In the test_resume mode, hibernate() loads the image it has just written
and restores it while the tasks are still frozen from the snapshot.  The
resume path clears the wakeup state in freeze_processes() before it
quiesces the devices, but this path does not, so a wakeup that a wake
IRQ latched during the image creation or write (a Bluetooth controller
with wakeup allowed does this in the thaw) is still pending when
dpm_suspend() runs for PMSG_QUIESCE.  The quiesce then fails with -EBUSY
before any device is called, without a message naming a device, and the
restore is abandoned with "Failed to load image, recovering".

Clear the wakeup state before the image is loaded, as the resume path
does.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit 88b3625)
… fails

In the test_resume mode the kernel that wrote the image loads it back and
jumps into it. The swap pages that hold the image are allocated in this
kernel's swap map; the restored kernel's map predates them, so a restore
that succeeds needs nothing freed. A restore that fails ("Failed to load
image, recovering") leaves this kernel running with those pages still
allocated and no owner for them, and nothing frees them: a later swapoff
of that swap area never finishes, which stops the shutdown at
"Deactivating swap" until a watchdog resets the machine.

Free them when hibernate() comes back from the test restore.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit ca6cc4c)
The rings and the bus state in a hibernation image date from its freeze,
but the firmware does not stay with them: in a test_resume it runs on
through the image write, and after a boot the kernel that loads the image
may have probed the chip and started its own firmware session. Either
way the kernel that loads the image quiesces it with the D3 hand-shake
of the freeze callback.

The restore callback was the resume one, which finds the interrupt mask
set and resumes the firmware in place on rings that no longer match. The
firmware then stops answering a few seconds later ("firmware died (no
response to control requests)"), and the chip reset that follows takes
the machine down: a watchdog reset of a chip whose firmware waits in its
D3 state does not complete.

Give the restore its own callback that takes the path the resume already
takes after a power loss: detach from the chip without touching it, so
that brcmf_pcie_remove() does not reset it, free the host state and probe
again. The probe brings the chip up from whatever state it was left in.

Signed-off-by: fbalazsy <frantisekbalazsy@gmail.com>
(cherry picked from commit def5653)
…eze and cold restore

Centauri Wi-Fi's suspend path only ran when pm_suspend_via_firmware() was
true and refused while connected, leaving Alpha active across hibernation
freeze and touching stale DMA rings after a cold restore. On cold boot,
the boot loader resets the N1 chip into its single-function ROM
personality so functions 1 and 2 do not exist yet until IPC startup
rescans them.

Add explicit hibernation callbacks:
- .freeze/.poweroff quiesce Alpha (even when connected) and Beta cleanly;
- .thaw resumes them in place;
- .restore resumes in place on test_resume (!pm_hibernate_restored_cold()),
  and on a cold restore keeps both transports paused without touching
  stale fn1/fn2 BARs or MSI while scheduling function 0's recovery worker
  (which runs after workqueues unfreeze) to warm-reset N1 from ROM,
  re-upload firmware, restart IPC/Wi-Fi, and re-attach btcentauri.

Signed-off-by: Nick <nekreshen@yandex.ru>
…ss test_resume

On live-session DCPs that cannot restart in place (such as T8142 v27),
hold presentation and flush RTKit RX during .freeze/.poweroff and release
them on .thaw. On an in-place test_resume (!pm_hibernate_restored_cold()),
the boot loader never restarted the firmware, so thaw instead of marking
the display dead. On a cold restore, mark both crashed and ext_dead so
IOMFB traffic stops cleanly.

Signed-off-by: Nick <nekreshen@yandex.ru>
Follow pr/linux-2977's rename of struct dcp_t6041_power.t6040 to
relink_ext (8dbf73e) in dcp_t6041_internal_capable() and its KUnit
test added on origin/main.

Signed-off-by: Nick <nekreshen@yandex.ru>
Cold boot resets the NCO audio clock channels while MCA and ASoC resume
expect enabled channels to keep their programmed divisor and increments.
Save each channel's control, divisor, and increment registers on
.freeze_noirq/.poweroff_noirq and reprogram them on .restore_noirq when
pm_hibernate_restored_cold() is true.

Signed-off-by: Nick <nekreshen@yandex.ru>
admac_resume_early() only marks controller and channel register state
lost when pm_suspend_via_firmware() is true. Add .restore_early that
marks them lost when pm_hibernate_restored_cold() is true so active
audio DMA channels are cleanly reprogrammed after a cold boot.

Signed-off-by: Nick <nekreshen@yandex.ru>
apple_mca_suspend_late() and apple_mca_resume_early() gate on
pm_suspend_via_firmware() / pm_resume_via_firmware(), skipping register
save/restore across hibernation. Add .freeze_late/.poweroff_late,
.thaw_early, and .restore_early (gated on pm_hibernate_restored_cold())
so active MCA clusters and SERDES settings survive a cold restore.

Signed-off-by: Nick <nekreshen@yandex.ru>
…d hibernation

cs42l84 gated its suspend/reset sequence on pm_suspend_via_firmware(),
skipping cache-only reset and post-resume re-init across hibernation.
Spell out freeze/thaw/poweroff/restore so cs42l84 resets cleanly before
the image and re-initializes + re-detects the jack on restore.

tas2764 gated power-on reset recovery on pm_resume_via_firmware(); also
treat pm_hibernate_restored_cold() as a power-on reset so the amps run
the full SW_RST, regcache_sync, init quirks, and fail-closed restore
check after a cold boot.

Signed-off-by: Nick <nekreshen@yandex.ru>
…ation restore

Export aura_hibernate_reprobe_after_thaw() to queue device_reprobe() on
system_freezable_wq once all devices finish .restore() and tasks thaw.

In apple_aop, add a .restore callback that checks the ASC CPU run bit:
when a cold boot halted and restaged the co-processor, close transport
immediately and queue a clean reprobe. Unbind detects the halted CPU and
releases buffers without shutdown IPC, and the fresh probe starts the
restaged AOP firmware and re-registers all child devices (sensors, lid,
AOP audio).

Signed-off-by: Nick <nekreshen@yandex.ru>
An in-place test_resume sets pm_hibernate_is_restored() without
restarting the enclave. Gate the hibernation rebind phase on
pm_hibernate_restored_cold() so test_resume keeps the live SEP session
while real cold restores still rebind the driver.

Signed-off-by: Nick <nekreshen@yandex.ru>
After a cold hibernation restore, the boot loader has reset the CIO
controller and host router. Add .restore_noirq that clears any
QUIRK_NO_SYSTEM_SLEEP and QUIRK_KEEP_TUNNELS flags left from an earlier
s2idle when pm_hibernate_restored_cold() is true, so
tb_domain_resume_noirq() restarts the NHI rings cleanly.

Signed-off-by: Nick <nekreshen@yandex.ru>
…panels

Extend the in-place hibernation firmware restart path to adopted internal
panel processors running firmware 27 (such as T8142/M5):

- Drain pending swaps before holding RTKit RX at freeze, and stop secure
  mailbox polling when present; keep test_resume thawing in place when the
  bootloader did not restart the firmware.
- Add iomfb_hib_quiesce() to stop the queued IOMFB transport, reset pending
  present state, and release stale scanout framebuffer references so the
  fresh firmware session starts with a clean queue.
- Generalize dcp_hib_quiesce() and dcp_hib_recommit() for non-desktop
  firmware-27 panels (clearing panelav/avep endpoints, pipe_enabled_h17p,
  soft-DPMS flags, boot_surface_pending, and pending_acks; powering the
  pipe via iomfb_poweron_v27(); and preparing nits backlight restore via
  dcp_backlight_hib_prepare()).
- In dcp_hib_restore_work(), verify CPU_CONTROL_RUN without toggling the
  ASC, reinit and wake RTKit against the bootloader-started firmware,
  restart the secure port when present, run dcp_start()/dcp_wait_ready(),
  and recommit the active CRTC state. Any failure falls back to
  dcp_hib_give_up() so the kernel stays up.

Signed-off-by: Nick <nekreshen@yandex.ru>
Implement S4 hibernation freeze/thaw/poweroff/restore and cold-boot
firmware republish for M5 (T8142) published-roots GPUs, while preserving
in-place test_resume across all generations:

- Gate sleep_restore_noirq containment on pm_hibernate_restored_cold() so
  in-place test_resume without a cold boot does not mark a live GPU lost.
- At freeze/poweroff on M5, close preparations, drain in-flight GPU work,
  pause event workers, and snapshot driver-owned L1/L2 links under the
  bootloader-reserved pagetables/l2-pagetables plus active user TTBAT
  context slots.
- On cold restore, defer M5 republish to a PM_POST_HIBERNATION notifier
  (priority -10, after PMP rebuild and DCP): re-initialize handoff/TTBs,
  cold-boot GFX1 and GFX via apple_rtkit_reinit() + cpu.start() + boot(),
  re-enable Fender gating, adopt the new firmware roots and re-attach
  preserved System-RAM leaf page tables (failing closed if any target
  slot is occupied), re-encode M5 InitData in place while preserving
  descriptor tables and stamps, reset idle compute/render queue install
  state, republish log buffers and InitData/POWER_ON_DONE, retire opening
  records, and republish saved user execution contexts.
- Any failure at any step stops both coprocessors and falls back cleanly
  to lose_device_quietly() so the kernel stays up.

Signed-off-by: Nick <nekreshen@yandex.ru>
… restore

After a cold hibernation restore on T8142, the GPU power domain starts in
auto-enable with actual state 0 (as at cold boot), so call
asahi_j813_power_on_gfx() before starting the M5 coprocessors.

Also clear published_completions on render queue reset, reset the cached
effort/hold/governor state to match the freshly reinitialized Region C,
zero Region A during InitData re-encoding, and use kzalloc_obj() for the
PM notifier handle.

Signed-off-by: Nick <nekreshen@yandex.ru>
On cold hibernation restore the bootloader re-trains the PCIe port
without programming PORT_MSIADDR, PORT_MSIMAP, or PORT_MSICFG_EN.
Add a .restore_noirq callback that calls apple_msi_hw_init() when
pm_hibernate_restored_cold() is set, before PCI devices resume and
before any port cycle saves the MSI table.

Signed-off-by: Nick <nekreshen@yandex.ru>
…rnation restore

After a cold hibernation restore the N1 is already back in its
single-function ROM personality, so secondary functions (1 and 2)
return all-ones on PCI config reads until recovery re-boots the chip.

Check PCI_VENDOR_ID before touching secondary functions in
centauri_stop_ipc() and centauri_stop_dma() so centauri_free_boot()
does not mistake the all-ones PCI_COMMAND_MASTER bit for live DMA and
abort recovery with -EBUSY.

In btcentauri, remember cold hibernation restore so cen_stop() frees
the unused 1 MiB DMA arena without touching the absent function 2.

Signed-off-by: Nick <nekreshen@yandex.ru>
… PWM

Three Apple peripheral bus drivers lacked system sleep / hibernation
restore coverage for registers lost across a cold reboot:

- i2c-pasemi-platform: add LATE_SYSTEM_SLEEP_PM_OPS using
  pm_runtime_force_suspend/resume so buses that were runtime-active at
  freeze re-run pasemi_i2c_init() in restore_early before I2C clients
  (such as audio codecs and speaker amplifiers) issue restore transfers.
- spi-apple: add runtime_resume calling apple_spi_init() and system
  sleep callbacks wrapping spi_controller_suspend/resume with
  pm_runtime_force_suspend/resume so SPI buses re-initialize PIN/CFG
  registers after domain power loss or cold hibernation restore.
- pwm-apple: add LATE_SYSTEM_SLEEP_PM_OPS using
  pm_runtime_force_suspend/resume so active PWM outputs (keyboard
  backlight) restore ON_CYCLES/OFF_CYCLES/CTRL after cold reboot, and
  set APPLE_PWM_CTRL_UPDATE when re-enabling to latch shadowed cycles.

Signed-off-by: Nick <nekreshen@yandex.ru>
…tch nits

Cancel any pending EDR ramp timer after dcp_drain_for_sleep() in
dcp_platform_freeze() so an active ramp tick cannot re-arm a present
while RTKit RX is held.

Also document the smp_store_release(&dcp->avep, NULL) pairing and use
kmalloc_obj() in aura_hibernate_reprobe_after_thaw().

Signed-off-by: Nick <nekreshen@yandex.ru>
…er cold hibernation

In apple-soc-cpufreq, refresh expected_pstate from APPLE_DVFS_CMD after
a cold hibernation restore (pm_hibernate_restored_cold()) just like
firmware-driven sleep, and restart the fast die-temperature grace timer
on hibernation restore while PMP re-initializes in PM_POST_HIBERNATION.

In macsmc-power, save CHLS/CHWA on freeze/poweroff and restore the charge
limit on cold hibernation restore so standard hibernation preserves the
configured battery charge limit without userspace help.

Signed-off-by: Nick <nekreshen@yandex.ru>
…D after cold hibernation

On boards where rtkit-helper starts MTP itself (such as T8142 J813/J815),
a cold hibernation wake leaves the MTP coprocessor halted when the boot
kernel does not load rtkit-helper before resuming. Previously
apple_rtkit_helper_restore_own() left MTP halted and dockchannel-hid
never re-initialized HID or touchpad firmware, leaving keyboard and
trackpad dead after hibernation.

Switch aura_hibernate_reprobe_after_thaw() to an ordered freezable
workqueue so supplier-before-consumer ordering is preserved, queue a
reprobe of rtkit-helper when cold-restored halted (freeing non-adopted
coherent shmem buffers in shmem_destroy), and queue a follow-on reprobe
of dockchannel-hid when its supplier reports halted_underneath.

Signed-off-by: Nick <nekreshen@yandex.ru>
…r cold hibernation

After a cold hibernation restore, the boot ROM leaves the boot CPU's
cluster power-gate word clear (preventing CPU 0's cluster from gating
and causing subsequent suspend-to-RAM to hang in the sleep sequencer),
and iBoot leaves MCC system-level cache allocation disabled.

- apple-cluster-pwrgate: retain the mapped gate registers and register a
  syscore resume callback to re-set CLUSTER_PWRGATE_EN before secondary
  CPUs online.
- apple-mcc: add .restore_noirq to restore ways and re-enable system
  level cache allocation on cold hibernation restore.

Signed-off-by: Nick <nekreshen@yandex.ru>
…on restore

Match dcp_platform_freeze() and dcp_platform_thaw() by delegating
dcp->external processors to dcp_platform_resume() in
dcp_platform_restore() instead of falling through and marking idle
external pipes dead.

Also log firmware-27 session-up and display-restored completion at
dev_info() level so hibernation restore progress is visible in dmesg.

Signed-off-by: Nick <nekreshen@yandex.ru>
During hibernation restore, thaw_processes() resumes userspace before
PM_POST_HIBERNATION runs g17 hib_cold_republish(). A thawed client
closing a queue in that window calls ExecutionRoot::release() without
waiting on the submission preparation gate, releasing the slot and
freeing its VM page tables. Replaying a freeze-time TTBAT snapshot
would then write a freed root back into the hardware table and leave
the slot permanently returning -EBUSY on future allocations.

Keep an authoritative System-RAM mirror of live ExecutionContext TTBAT
pairs in UatShared alongside a cold_booting gate set in .restore before
tasks thaw. While cold_booting is set, ExecutionRoot::release() updates
only the mirror and allocator bookkeeping without touching no-map
handoff or TTBAT pages, and republish_user_contexts() writes only the
still-live mirrored pairs after cold firmware boot before lifting the
gate. Also clear hib_poweroff_entered on freeze and thaw so a recovered
poweroff failure cannot leak into a later hibernation snapshot.

Signed-off-by: Nick <nekreshen@yandex.ru>
…P restore

apple_platform_thaw() keeps outputs suspended (hib_thaw_skipped) while
writing the hibernation image, so wiring .poweroff directly to
apple_platform_freeze() called drm_mode_config_helper_suspend() a second
time while suspend_state was already populated. Add an explicit
apple_platform_poweroff() (and guard apple_platform_freeze()) to return
early when outputs are already suspended.

In dcp.c, also clear secure_answered when dcp_hib_quiesce() drops the
old session, clear hib_poweroff_entered in dcp_platform_thaw(), and skip
dcp_platform_restore_early() session teardown on warm in-place
test_resume restores.

Signed-off-by: Nick <nekreshen@yandex.ru>
…ernation

pm_hibernate_restored_cold() is already set before syscore_resume()
runs in create_image(), yet aic_syscore_resume() only restored
AIC2_TIMEOUT, AIC2_CLUSTER_PRIO, AIC2_CONFIG, and aic_resume_boot_cpu()
for pm_resume_via_firmware(). Include pm_hibernate_restored_cold() so
CPU 0's VM timer FIQ mask stays synchronized with per-CPU state after a
cold hibernation restore.

In apple-sleep, also clear mbox_lost once a valid S2R mailbox is
re-mapped on restore and re-validate if mbox_lost was previously set.

Signed-off-by: Nick <nekreshen@yandex.ru>
…old hibernation

hib_freeze() leaves both M5 GPU coprocessors running, so in the
restored hibernation image apple_mbox_ascwrap_v6_start_cpu() still sees
ascwrap_v6_cpu_running set and returns -EBUSY on cold_boot(). Settle
the provider's books with cpu.stopped_elsewhere() first so cold_boot()
balances runtime PM and starts the cold-reset ASC CPU cleanly.

Also clear LAST_FRAGMENT and LAST_TASK_FRAGMENT when resetting render
queues for a cold boot so the first cross-pair render after resume
never waits on a pre-hibernation fragment kick that the fresh firmware
never executed, and reset cached Pair/ParkedPair executed scheduler VAs.

Signed-off-by: Nick <nekreshen@yandex.ru>
After apple_pmp_restore() restarts a cold-booted PMP at
PM_POST_HIBERNATION, call apple_pmp_report_wake(pmp_device, false) so
all driver-owned power votes and DVFS floors are re-asserted onto the
freshly booted PMP before display and GPU consumers wake up.

Restrict apple_pmp_report_entry_pm_ops to .suspend_noirq/.resume_noirq
so release-for-sleep entries do not poll a still-halted PMP during
hibernation .restore_noirq, and remember pio_mapped on PmpData so
apple,pio-ranges are not re-mapped with -EEXIST across cold restore.

Signed-off-by: Nick <nekreshen@yandex.ru>
apple_rtkit_reinit() and apple_rtkit_reinit_retaining_buffers() call
apple_mbox_start(), which returns early without touching hardware when
mbox->active is already set in a restored hibernation image. Follow
apple_mbox_start() with apple_mbox_restore() so mailboxes with hardware
enable or IRQ control bits re-enable them before the firmware boots.

Signed-off-by: Nick <nekreshen@yandex.ru>
@fbalazsy

fbalazsy commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Hi @m0d16l14n1, a coordination note from #2977.

New base. #2977 had a one-time history cleanup: one comment was reworded in an early commit and the branch was force-pushed, so the head this branch merged (3f03f90e1ccc, merge d6819c59956b) is no longer on it. Please rebase your 32 commits onto 5f4a9facdab3 (current #2977 head) or re-merge it. The trees match up to the old head, so only the merge itself should change. There may be conflicts in the SIO driver and the watchdog, which changed since 3f03f90e (see below).

What I took from this PR. Two of your commits are now in #2977, cherry-picked with your authorship:

  • soc: apple: dockchannel, HID: dockchannel: restore IRQ masks and guard receive after hibernation (29c4098b6194);
  • soc/apple: rtkit: restore mailbox hardware enables on reinit (86ece55ea32a).

When you rebase, they drop out of your series as already applied. Both ran in real power-off cycles on a J616s (T6040). The DockChannel change does not cure everything there: the MTP ring still reports Receive ring failed: -71 in some cycles when the loading boot had started the MTP. That looks like what your cc02f2ea7a33 (restart a halted MTP and re-probe dockchannel-hid) addresses. I'd like to look at it for T6040 next, unless you would rather keep that path on your side.

Changes in #2977 that touch your stack.

  • SIO: the restored kernel boots the SIO firmware again only if the boot loader left it staged and halted. A firmware the loading kernel started does not take a second boot (it answers the handshake, then refuses its first parameter). So the loading kernel idles it and leaves its run bit set, and the restored kernel leaves the SIO off until reboot. This replaces the engine-reset restart that your d00e864c builds on. For audio after a restore, the loading kernel must not bind the SIO.
  • Watchdog: the reconfig and SMC AP deadline registers now come from the DT (apple,wdt reg-names "reconfig", "deadline"), not from per-SoC match data. The T8142 entry is gone, so t8142.dtsi needs the two extra reg entries for the backstop.
  • r8152: -ESHUTDOWN is handled like -ENODEV on tx and interrupt resubmission.

Ownership. I suggest #2977 keeps the shared core (PM core, RTKit, DART, SIO, watchdog, DCP common paths) and T6040, and this PR keeps T8142/M5 (G17G republish, firmware-27 DCP, Centauri, codecs). A fix to a shared path that works on both goes into #2977 with credit. Your M5 UAT context mirror is interesting for keeping GPU contexts on T6040 too. Has it been run on a J815/J813 yet? If it holds there, I will try the same idea on the T6040 path and credit you.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants