Skip to content

J616s (M4 Pro): hibernation (suspend to disk) under aura's fixed boot layout - #906

Closed
fbalazsy wants to merge 24 commits into
aurora-silicon:integration/aurorafrom
fbalazsy:j616s/s4-hibernation
Closed

fbalazsy wants to merge 24 commits into
aurora-silicon:integration/aurorafrom
fbalazsy:j616s/s4-hibernation

Conversation

@fbalazsy

@fbalazsy fbalazsy commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Hibernation is refused on every Apple platform (f4d4f79), and tools/aurora-sep/HIBERNATION.md lists what is missing. This series adds hibernation for the MacBook Pro 16" M4 Pro (J616s, T6040/T6041) when it is booted by aura with its fixed hibernation layout, and keeps it refused everywhere else. Most of the work is in the drivers: a resume from disk is a cold boot, so every co-processor the boot loader restarts has to be brought back in place by the restored kernel, without a reboot and without losing the desktop.

Depends on aurora-silicon/aura#66 (fixed hibernation layout published as /chosen/aura,hibernate-layout, the device tree slot /chosen/aura,hibernate-dt, PSCI CPU_OFF/CPU_ON). Without that aura nothing in this series changes behaviour: hibernation_available() stays false and the new restore paths are never reached.

Series. 24 commits on integration/aurora (21f7ffc), by subsystem:

  1. soc: apple: allow hibernation under aura's fixed boot layout: new aura-hibernate (CONFIG_APPLE_AURA_HIBERNATE). On a boot with /chosen/aura,hibernate-layout every /memreserve/ and non-reusable /reserved-memory range except the kernel image is kept out of the image (arm64 pfn_is_nosave()), and hibernation_available() allows hibernation. Without the property hibernation and resume stay refused on Apple platforms; nothing else (no aura,monitor or other boot loader detection) is looked at.
  2. PM: hibernate: make the image signature handling durable: the restore-time signature reset is written with FUA; once the image is in memory the signature is cleared again durably before the device restore (a restore that hangs cannot be resumed into again); the image is flushed in swap_writer_finish(), and a failed flush drops the signature and fails the hibernation. Generic code, no platform conditions.
  3. arm64: hibernate: clean copied pages to the PoC: the copy loop in swsusp_arch_suspend_exit() cleans to the PoC instead of the PoU, for devices that are not cache coherent.
  4. PM: hibernate: clean the loaded image to the PoC on arm64: pages loaded straight into their frames through the linear map are cleaned and invalidated to the PoC before the jump; frames outside the boot kernel's linear map (the linear alias of the kernel image) are left to the copy, which cleans them.
  5. irqchip/apple-aic: on syscore resume, the hardware masks are made to match the restored IRQ descriptors (the boot kernel left most IRQs masked).
  6. spmi: apple: in restore_noirq, pending interrupts left by the boot kernel are acknowledged and the masks rewritten from the cache.
  7. pmdomain: apple: domains genpd recorded on but found gated after the cold boot are powered on, parents first.
  8. iommu: apple-dart: locked DARTs come back from the boot loader with fresh firmware roots; .restore re-imports them (two passes, as attach) and republishes the streams' page tables. Without it every DMA mapping of the PMP and the DCPs failed.
  9. soc: apple: rtkit-helper: an adopted session is re-adopted after the resume; a self-started one is left for a reload from userspace; a co-processor found halted at remove is released without a handshake.
  10. soc: apple: pmp: the firmware session is rebuilt in place on PM_POST_HIBERNATION, ahead of the other restore notifiers, when the co-processor was found halted; MMIO guards are not held across the sleeping handshake. Rust RTKit gains reinit().
  11. pmdomain: apple: pmp-report: requests are held for a PMP that the cold boot restarted and handed over once it runs; apple_pmp_report_wait_handed() for the GPU.
  12. cpufreq: apple-soc: the fast die-temperature wait starts over when the PMP stops.
  13. mfd: macsmc: the image-loading kernel hands the SMC over quiesced, the restored kernel starts a new RTKit session (.restore), and a hand-over that could not enter the image starts one in .thaw. Without it battery, keyboard and trackpad (SMC-backed preflight) and the power-off were lost after the resume.
  14. power: reset: macsmc-reboot: a refused off1 is retried and an accepted one gets 5 s before the PSCI handler, which resets the machine under a monitor without a power-off path.
  15. nvme-apple: post-M4 ANS gets explicit hibernation callbacks; .restore stops the controller, releases the stale session and adopts the one the boot firmware started, as probe does.
  16. phy: apple: t6040: restore_early resets the PHY state the image still records to what probe leaves; consumers' counts stay balanced.
  17. usb: typec: tipd: sn201202x: in-place restore, driving the controller polled (selections, wake and sleep) from the cold boot until it is probed again, and tipd_reinit_after_cold_boot(); an interrupt-driven selection that timed out checks whether it completed anyway; revalidation waits for the new USB role switch, started from userspace through the new hib_resume attribute (documented in Documentation/ABI/testing/sysfs-driver-sn201202x). A failed restore reads failed and is re-probed by userspace.
  18. soc: apple: aop: a co-processor found halted at unbind is released without a shutdown; the next (cold) probe takes this boot's firmware regions from aura's device tree slot (aura_hibernate_boot_fw_regions()) and remaps the moved __OS_LOG segment.
  19. accel/ane: the H16 engine takes its cold sleep cycle for hibernation; a quarantined engine no longer vetoes it.
  20. drm: apple: track piodma mappings on desktop 26.x firmware too: without it a second resume from disk in one boot failed every piodma mapping with -EEXIST; an unmap of a buffer that is not mapped is refused as on H17P.
  21. drm/apple: dcp: hibernation callbacks for the desktop 26.x internal DCP and the T6041 external processors. The image is written with the display left as it is; after the resume the firmware session is rebuilt in place (RTKit receive held from freeze, old host session dropped in restore, restart and a modeset commit of the current DRM state on PM_POST_HIBERNATION after the PMP), external links come back through the runtime hotplug path once every external processor runs, and the HDMI converter is powered again. Any failing step falls back to the old behaviour (display left to the boot loader until reboot).
  22. drm/apple: bound kernel retrains of an active CRTC after hotplug: a sink that drops its link after every training no longer causes an endless retrain/hotplug loop.
  23. drm: apple: dptxep: link faults without a sink are logged at debug level.
  24. drm/asahi: g17: hibernation gate (asahi.hibernate_cold_init, default on) with a drain before the image; after the resume a cold-restarted firmware is republished in place: MMU handoff/TTBAT rewritten from the host shadow and cleaned to the PoC, boot-time data reset, fabric bridge enabled again, start after the PMP took the GPU power request, live queues reset to fresh installs (render kick rings at the first timestamp, pass origins reset).

Boot loader contract (aurora-silicon/aura#66).

  • /chosen/aura,hibernate-layout = <start end> (two u64: start and end of the RAM kept from Linux): published only when the boot's memory layout below Linux is the same on every boot. It is the only property the kernel gates on; only then is hibernation allowed.
  • /chosen/aura,hibernate-dt (u64): aura copies each boot's device tree, as the last step before the handoff, to a fixed 2 MiB slot inside the range it keeps from Linux. The kernel uses it only when it was started with its tree at that address. The image does not save it, so after a resume the restored kernel reads this boot's firmware regions from it (used by the AOP).
  • The fixed layout is selected in aura with the hib=1 configuration line and is T6040 only; every boot must use the same boot object, kernel and modules while an image is pending.
  • CPUs are parked and started through PSCI CPU_OFF/CPU_ON provided by aura. aura's PSCI SYSTEM_OFF halts instead of resetting, so a power-off that falls through macsmc does not boot straight back into the image just written.
  • Co-processors are restarted by the boot firmware on every boot; the kernel that loads the image binds its drivers like any boot. The drivers in this series do not depend on what that kernel did, apart from the SMC hand-over (commit 13), which that kernel performs itself.

Userspace contract. Some devices are brought back by userspace around the transition, e.g. from a systemd sleep hook that runs before systemd-hibernate.service freezes user space and again after it finished (also when the hibernation failed):

  • before the image: refuse when a USB block device is mounted; stop the audio users (PipeWire) and speakersafetyd; unbind the macaudio sound cards and the tas2764/cs42l84 codecs, Bluetooth (hci_bcm4377) and the dwc3-apple USB controllers; unload apple_isp;
  • after the transition: decide whether the boot behind it was cold from the kernel log line PMP restore: ASC CPU_RUN=0 (CPU_RUN=1 or no line: no cold boot, leave the following two steps out);
    • cold only: reload the MTP stack (dockchannel_hid, apple_dockchannel, apple_rtkit_helper) and rebind the AOP;
    • bind the dwc3-apple controllers again, then for each sn201202x port controller write roles to hib_resume when it reads roles-pending, otherwise unbind and bind it;
    • load apple_isp, bind Bluetooth, the codecs and macaudio again, start speakersafetyd and the audio users;
    • cold only: if no Wi-Fi interface appears, unbind and bind brcmfmac (and let NetworkManager manage the new interface).
      The rest (GPU, display, NVMe, SMC, PMP, DART, ANE, the Type-C PHY) is restored by the kernel in place.

Relation to tools/aurora-sep/HIBERNATION.md.

  • CPU park and restart: provided by aura's PSCI (CPU_OFF/CPU_ON), not by spin-table; the arm64 CPU offlining and image checks are unchanged. Not part of this series.
  • Devices after a cold boot: commits 5-24. DCP and GPU firmware sessions and DMA ownership are rebuilt after real power loss instead of relying on retained firmware; NVMe and DART get explicit restore paths (their s2idle callbacks were not enough).
  • Boot and image compatibility: commit 1 keeps the boot's reserved memory out of the image and allows hibernation only with aura's fixed layout; aura's device tree slot gives the restored kernel this boot's firmware regions. The arm64 header check (same kernel build) is unchanged; the same kernel, modules and boot entry must stay installed while an image is pending.
  • Storage and authentication: tested with a swap file on the internal NVMe, resume= and resume_offset= in the initramfs. Commit 2 makes the signature reset and the image durable. Not tested: encrypted swap, zram (not usable for an image), cold-resume Touch ID or secure sessions (SEP).

Hardware tested on. MacBook Pro 16" M4 Pro (J616s), boot through aura with the fixed hibernation layout and the device tree slot, swap file resume; Samsung G95NC on HDMI and USB-C DP (dual pipe), USB-C hub with RTL8153 Ethernet. Kernels: (a) integration/aurora 9ff2b9a with the earlier states of this series, and (b) this series as submitted (head of this PR on 21f7ffc). Both carried a local J616s platform layer (NVMe write guard, DART filter, device tree; not for aurora); (b) also carried a local revert of "drm/asahi: present the GPU driver as Aurora", because the userspace on the test machine still waits for a DRM driver named asahi before it starts the desktop. The series is the code of (a) with the bring-up diagnostics removed (a resume watchdog, stage markers, a write/power-off progress guard and polling idle during the image write, RCU and TTBR logging).

Kernel state Cycle Mode Result
without commit 13 (SMC hand-over) 1st in boot shutdown, power-on by key resume OK; SMC session lost (-62): no keyboard, trackpad or battery; AOP and audio back
without the HDMI converter power-on (part of 21) 1st in boot shutdown, power-on by key everything back except HDMI (converter supplies left off by the cold boot)
series without commits 20 and the polled sn201202x restore (part of 17) 1st in boot shutdown, power-on by key everything back: eDP, HDMI, USB-C DP, Ethernet, Wi-Fi, audio, keyboard, trackpad; the Type-C in-place restore failed and the userspace re-probe fallback took over
same 2nd in the same boot reboot the Mac stayed off after writing the image (a key press powered it on); resume OK; the session stayed locked until authentication; eDP without a picture (piodma -EEXIST, fixed by commit 20), USB-C DP lost after the Type-C re-probe (addressed by the polled restore in commit 17)
same after the two resumes - camera (apple-isp) captures a correct frame; an ANE test program runs 50/50 times with exact results
earlier tree with all of this series' code (commits 17 and 20 included) 1st in boot shutdown, power-on by key everything back: eDP, HDMI, USB-C DP, Ethernet, Wi-Fi, audio, keyboard, trackpad; all four Type-C controllers restored in place (no re-probe, the USB-C display stays up)
same 2nd in the same boot shutdown, power-on by key everything back as above; no piodma error
same, without the "polled until probed again" part of 17 2nd in the same boot shutdown hibernation aborted at freeze: the cold-restored controller missed its sleep interrupt (-110), which is what that part fixes
this series (head of this PR) 1st in boot shutdown, power-on by key everything back: eDP, HDMI, USB-C DP, Ethernet, Wi-Fi, audio, keyboard, trackpad, GPU (screen copy of the restored desktop); all four Type-C controllers restored in place
same 2nd in the same boot shutdown, power-on by key everything back as above
same after the two resumes - camera (apple-isp) captures a correct frame; ANE not checked (see below)

Not done / not tested.

  • hibernation_mode=reboot left the machine powered off after the image was written instead of rebooting it. Cause unknown; no boot on this aura path has logged a Linux restart so far (only power-offs), so a plain reboot there is unverified as well and this may be a general restart issue of the boot path. Advice welcome.
  • ANE after resume was checked on kernel (a) only (50/50 exact runs). On (b) the ANE does not start at all on this machine, also without any hibernation (H16 preload matches none of 2 firmware profiles): the firmware installed here (macOS 26.7.1) matches neither profile in integration/aurora. That is independent of this series.
  • Only one machine (J616s) and one boot loader (aura) were tested. m1n1 and m4-loader boots keep hibernation refused.
  • Not tested: hibernation_mode=platform and suspend (suspend to both), compressed vs. uncompressed images, the kernel's staged PM tests (pm_test), corrupt or incompatible images, encrypted swap, Touch ID / SEP after a cold resume, more than two hibernation cycles in one boot, long battery idle.
  • The USB controllers (dwc3-apple) have no restore path of their own; USB comes back through the userspace rebind above.
  • The DCP keeps the old endpoint objects allocated until unbind (a bounded leak per resume).
  • Build: Image modules builds at every commit and Image modules dtbs at the series head (arm64, J616s configuration with CONFIG_HIBERNATION=y, CONFIG_APPLE_AURA_HIBERNATE=y, DRM_ASAHI=m); no new compiler warnings. checkpatch: no errors; the remaining warnings are style notes (one msleep(10) poll, one memory barrier comment). KUnit and W=1 were not run.

Hibernation is refused on every Apple platform, since the memory that the
boot loader and the firmware keep alive moves between boots. A hibernation
image restores every saved page to the frame it was taken from, and swsusp
saves reserved pages as long as they are mapped: under aura the memory
below Linux's own allocations holds aura itself, its heap with the firmware
buffers it handed over, the device tree, the initramfs and the ranges the
boot firmware placed for the co-processors. Restoring old contents there
overwrites the live state of the resuming boot.

aura can boot with a layout that is the same on every boot and marks such
a boot with /chosen/aura,hibernate-layout. Add aura-hibernate: on such a
boot it keeps every /memreserve/ range and every non-reusable
/reserved-memory range (except the kernel image) out of the image, through
a static table that arm64's pfn_is_nosave() consults, and lets
hibernation_available() allow hibernation. Without the property nothing
changes: hibernation and resume stay unavailable on Apple platforms.

register_nosave_region() is not used because it allocates from memblock
and is meant for setup_arch() time.

Signed-off-by: František Balázsy <frantisekbalazsy@gmail.com>
Three places where the hibernation signature on disk can end up wrong
after a power loss, all seen while bringing up hibernation on a machine
whose resume could hang in a device restore:

- swsusp_check() resets the signature with a REQ_SYNC write. That write
  can sit in the device's volatile cache, and a hard power-off of a hung
  restore then loses it: the next boot resumes the same image again. Write
  it with FUA.

- Once the image is in memory, clear the signature again before the device
  restore, durably (FUA write and an explicit cache flush), so a restore
  that hangs cannot leave a valid image for the next boot to resume into
  the same hang. The new swsusp_invalidate_sig() only rewrites the header
  and keeps the swap pages: the resuming kernel jumps into the image, and
  freeing swap there would desync the restored kernel's swap accounting.

- After the image is written, flush the device cache in
  swap_writer_finish() rather than relying on the device shutdown in the
  power-off that follows. If that flush fails there is no durable image:
  drop the signature durably and fail the hibernation, so no later boot
  resumes from freed swap.

Signed-off-by: František Balázsy <frantisekbalazsy@gmail.com>
swsusp_arch_suspend_exit() cleans each page it copies into place to the
PoU, which is enough for the instruction side. Devices that are not cache
coherent with the CPUs read the restored pages from DRAM, and on Apple
SoCs every co-processor and the GPU page table walker are such devices.
Clean and invalidate the copied pages to the PoC instead, which also
covers what the PoU clean did.

Signed-off-by: František Balázsy <frantisekbalazsy@gmail.com>
Most image pages are loaded straight into their original frames through
the cacheable linear map; only pages that conflict with the loader go
through restore_pblist and the copy in swsusp_arch_suspend_exit(). A
non-coherent DMA master that reads such a page after the jump sees stale
DRAM, and a later eviction of a dirty line overwrites what a device wrote
in the meantime.

On arm64, clean and invalidate every image page to the PoC in
snapshot_write_finalize(), while orig_bm still lists the image (recycling
overwrites it). Skip frames that are not in the boot kernel's linear map,
such as the linear alias of the kernel image, which faults when touched:
nothing was loaded there through the linear map, and those frames are
restored by the copy, which cleans each page itself.

Signed-off-by: František Balázsy <frantisekbalazsy@gmail.com>
…me from disk

After a resume from disk the controller holds what the boot kernel left:
its init masked every IRQ and it unmasked only the ones it used itself.
The IRQ core disables lazily and unmasks only an IRQ it recorded as
masked, so on its own it never unmasks the restored kernel's other IRQs
again.

On syscore resume, make every hardware mask match the restored descriptor:
unmasked unless the core recorded it masked, masked when this kernel has no
mapping for it. On the thaw path the masks already match and this rewrites
the same values.

Signed-off-by: František Balázsy <frantisekbalazsy@gmail.com>
After a resume from disk the controller holds the boot kernel's masks and
pending interrupts, not the restored kernel's. Interrupts left pending
complete register selections of the devices restored next before their own
answer arrives, which made the in-place restore of the Type-C port
controllers time out ("selection not complete").

In restore_noirq, acknowledge what is latched, as probe does, and rewrite
the masks from the cache the mask/unmask paths keep, notification IRQ
included.

Signed-off-by: František Balázsy <frantisekbalazsy@gmail.com>
…e from disk

After a resume from disk genpd still records the domains as they were when
the image was taken, but the cold boot behind the resume left the hardware
in the boot loader's state, and genpd powers on only the domains it
recorded as off. A domain recorded on but found gated makes its consumers'
restore touch an unpowered block.

In restore_noirq, bring each such domain up, parents first. Firmware-owned
and inherited domains are left alone.

Signed-off-by: František Balázsy <frantisekbalazsy@gmail.com>
…m disk

A resume from disk is a cold boot: the boot loader locks every locked DART
again 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,
re-import the live roots the way attach maps them, 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 publish the stream's page tables into them
again. 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: František Balázsy <frantisekbalazsy@gmail.com>
The image is written while the firmware behind an adopted session keeps
running; its buffers live in SRAM or in no-map reserved memory, so they are
not part of the image and the host side has nothing to quiesce. After the
resume the boot loader has started a new instance at the same SRAM and
no-map addresses: re-read the handoff and adopt that instance, as probe
adopted the first one.

A session this driver started itself is left stale on restore; the resume
boot left the co-processor in a state the driver cannot know, and starting
it blind is not safe, so userspace reloads the driver after the restore.
That is logged at info level, not as a warning.

When the co-processor was found halted at remove (the boot loader leaves it
so after a resume from disk), nothing would answer a quiesce: drop the
session without the handshake so that the next probe starts it again.

Signed-off-by: František Balázsy <frantisekbalazsy@gmail.com>
After a resume from disk 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: a running firmware (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.

The Rust RTKit abstraction gains a reinit() for the restart.

Signed-off-by: František Balázsy <frantisekbalazsy@gmail.com>
…the driver

After a resume from disk the boot loader has cold-restarted the PMP, and
its report region is reset when it starts, while the restored driver still
believes its requests were handed. Requests written to the stopped PMP
are lost.

In restore_noirq, hold the requests again when the PMP does not run: they
stay in the shadow and the ready work hands them over once it runs, as at
probe. A request made while the PMP is stopped is held the same way.

Add apple_pmp_report_wait_handed() for a consumer that restarts its own
firmware after the resume and must not run it before the PMP has taken the
power state requests made for it (the GPU's), and warn while requests stay
held.

Signed-off-by: František Balázsy <frantisekbalazsy@gmail.com>
…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: František Balázsy <frantisekbalazsy@gmail.com>
After a resume from disk the boot firmware has restarted the SMC
co-processor, so the RTKit session the restored driver describes belongs
to a firmware instance that no longer exists, 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. So:

- the loading kernel (PM_RESTORE_PREPARE seen) quiesces the SMC in .freeze,
  leaving it in 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;
- .thaw after a hand-over that could not enter the image starts a new
  session the same way.

The PM notifier is registered first in probe, so that a late probe still
sees the start of a resume.

Signed-off-by: František Balázsy <frantisekbalazsy@gmail.com>
…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: František Balázsy <frantisekbalazsy@gmail.com>
On post-M4 ANS the resume from disk 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.
.restore 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.

Signed-off-by: František Balázsy <frantisekbalazsy@gmail.com>
A resume from disk is a cold boot: the PHY, its co-processor and the pipe
handler come back reset, while the driver's state comes from the image.
Its consumers give it back before the image (USB is unbound, the DCP
powers the DP PHY off at hibernation prepare); what is left set describes
hardware that no longer exists and, with pin assignment D, made the next
DP power-on take the USB3 PHY for running.

In restore_early, clear that state to what probe leaves, so the next
power-on programs the PHY afresh. What consumers still hold is theirs to
release and is kept, so every count stays balanced (a PHY still held is
reported); the Type-C partner state stays for the next power-on. A
co-processor RTKit session still running is reset in .restore, after the
DARTs its buffers live behind are restored. s2idle is unchanged.

Signed-off-by: František Balázsy <frantisekbalazsy@gmail.com>
A resume from disk is a cold boot: the boot loader and the loading kernel
have taken the controller through their own bring-up, so the state saved
in the image no longer exists and waking it the suspend way times out.
Probing it again from scratch takes the port, and a display on it, down
right after its link came up.

Restore it in place instead. The cold boot leaves it awake, so a wakeup
command raises no interrupt and its register selections do not complete
by interrupt reliably: drive it polled (selections, wake and sleep)
until it is probed again, and repeat what probe sets up (system power
state S0, application firmware, interrupt mask) through
tipd_reinit_after_cold_boot(). Since the event interrupt is
edge-triggered and one lost selection would leave the port without
events, an interrupt-driven selection that timed out also checks whether
it completed anyway.

The USB controller that provides the port's role switch is bound again
after the resume, which registers a new switch. Revalidation of a restored
port waits until userspace has bound the USB controller and writes "roles"
to the new hib_resume attribute, which looks the role switch up again. A
controller whose restore failed reads "failed" there and is probed again
by userspace; until then it is left alone with its interrupt masked and is
not suspended at the next sleep. A port still waiting at the next system
sleep gives up the wait.

The attribute is documented in Documentation/ABI/testing/.

Signed-off-by: František Balázsy <frantisekbalazsy@gmail.com>
After a resume from disk the boot loader leaves the AOP halted with new
firmware staged, while the restored driver still holds the session of the
instance that probed before the image. Two things then went wrong:

- remove sent the firmware a shutdown that a halted co-processor never
  answers. When the co-processor's run bit reads 0 at unbind, release the
  old session without any handshake (a halted CPU neither answers nor
  does DMA) and mark the next probe as a cold one;

- the boot firmware places some of the AOP's DRAM segments (the __OS_LOG)
  anew on every boot, so the restored mappings point at the previous
  boot's ranges. The restored kernel only knows the device tree of the
  boot it was started on. aura hands each boot's device tree over at a
  fixed address inside the range it keeps from Linux
  (/chosen/aura,hibernate-dt), which the image does not save;
  aura_hibernate_boot_fw_regions() reads a device's firmware regions from
  it, and a cold probe unmaps the previous set's moved regions, maps this
  boot's and checks the result as at boot. Nothing is copied. Without the
  slot the current set is only checked, as before.

The AOP stays bound across the hibernation (its firmware does not
acknowledge a shutdown, and an AOP put to sleep did not answer a new
probe); userspace rebinds it after a real resume from disk.

Signed-off-by: František Balázsy <frantisekbalazsy@gmail.com>
Hibernation of a bound H16 engine was refused. Take the cold sleep cycle
for it on platforms without a warm sleep path: power the engine down
before the snapshot and, after the image is restored or the attempt
aborted, boot it again from the preload with every program replayed. The
preload stays out of the image, the verified copy in it. A loading kernel
that booted the engine powers it down the same way before it jumps into
the image.

A quarantined engine never runs a program again in this boot, and the
resume is a cold boot of it anyway: it no longer vetoes the hibernation
(a startup timeout at boot otherwise refused every later attempt). It is
powered off and stays quarantined.

Signed-off-by: František Balázsy <frantisekbalazsy@gmail.com>
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: František Balázsy <frantisekbalazsy@gmail.com>
…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: František Balázsy <frantisekbalazsy@gmail.com>
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: František Balázsy <frantisekbalazsy@gmail.com>
…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: František Balázsy <frantisekbalazsy@gmail.com>
…om disk

Hibernation with a bound G17 GPU was not possible: nothing brought the
GPU firmware back after the cold boot behind a resume, and the GPU owns
DMA into memory the image restores.

Gate hibernation (asahi.hibernate_cold_init, default on): at
PM_HIBERNATION_PREPARE drain the GPU (publication closed, the event worker
quiet until the thaw) before the image and before the display prepares;
refuse the hibernation with the reason logged while the GPU is not idle.
After the resume, at PM_POST_HIBERNATION, find out whether the firmware
was cold-restarted (the co-processor run bit, plus a second witness: the
no-map handoff and TTBAT words no longer hold the host's) and, if so,
republish it against the restored host state:

- the MMU rewrites the no-map handoff region and the TTBAT from the host's
  shadow and cleans the GPU page tables and mapped pages to the PoC;
- the mailboxes and the data the firmware reads at boot are reset to
  their first-boot contents, in place (from a pristine copy taken at
  boot), the GPU-to-fabric bridge is enabled again, and the firmware is
  started only after the PMP has taken the GPU's power request again
  (bounded wait);
- live queues become fresh installs: compute queues and render pairs are
  reset to their construction state, render kick rings start over at the
  first timestamp with no parent and render pass origins restart, as the
  restarted firmware expects; idle compute queues without an owner are
  retired and contexts closed in the meantime are settled.

A warm thaw (the image was not entered) only reopens publication. A
crashed thaw ends the hibernation closure.

Signed-off-by: František Balázsy <frantisekbalazsy@gmail.com>
@RyanTheTide

Copy link
Copy Markdown
Contributor

Merge train (MTL, 06/11): this no longer merges into integration/aurora. It conflicts in drivers/gpu/drm/apple/dcp.c, drivers/gpu/drm/apple/iomfb.c, drivers/gpu/drm/asahi/g17/{initdata,mod,port}.rs, drivers/usb/typec/tipd/spmi.c and drivers/usb/typec/tipd/tps6598x.h. With 24 commits across that many shared drivers I'd rather not resolve it on your behalf. Could you rebase it onto the current integration/aurora? Sign-offs are all fine. Once it's rebased we'll gate it (host selftests, KUnit, W=1) and give it a J616s run, since we have one here, plus regression boots on our M1 Pro, M2 and J700.

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Full review of 0c79928 (24 commits on 21f7ffc). Sign-offs are all fine and the diff has no unexplained hardware constants.

1. Rebase. This still conflicts with integration/aurora (a45bea1). It now hits drivers/gpu/drm/apple/{dcp.c,iomfb.c}, drivers/gpu/drm/asahi/g17/{initdata,mod,port}.rs, drivers/gpu/drm/asahi/{sysfs.c,sysfs.h,sysfs_exports.rs}, drivers/usb/typec/tipd/{spmi.c,tps6598x.h}, kernel/power/hibernate.c and rust/bindings/bindings_helper.h.

The hibernate.c conflict is with 349bc3b ("PM: hibernate: allow pm_test dry runs on Apple silicon"), which split hibernation_available() into hibernation_platform_blocked() / hibernation_generic_available(). Put the aura_hibernate_layout_fixed() check in hibernation_platform_blocked().

That commit has a second consequence for this series: freeze/thaw and the PM_HIBERNATION_PREPARE/PM_POST_HIBERNATION notifiers now run on every Apple board through pm_test, aura or not. That covers dcp_hib_prepare()/dcp_platform_freeze()/thaw, the g17 hibernation drain, the ANE cold cycle, the rtkit-helper freeze/thaw and, at the core level, aic_syscore_resume(). They all need to be correct on boards without the fixed layout. See the test list below.

2. Changes that are not limited to hibernation (they run on every boot or every board):

  • power: reset: macsmc-reboot (3c78e6d): every Mac with macsmc now busy-waits 5 s after an accepted off1, where it used to wait 100 ms, and retries a refused one 3 times. That covers the M1, M2, M3 and M4 laptops, not only aura boots. Please either scope the long wait to boots that need it (aura_hibernate_layout_fixed(), or the PSCI SYSTEM_OFF fallback being a reset) or show that a normal power-off is unchanged on older SoCs.
  • drivers/gpu/drm/asahi/mmu.rs (0c79928): execution_roots/execution_vms are filled on every bind on every GPU generation. In ExecutionRoot::drop(), when release is false (the slot holds another owner's root), execution_vms[id] is never taken, so that Vm stays referenced until the slot is reused. That contradicts the "never outlives the context's own reference" comment. When bind reuses the slot, inner.execution_vms[id] = Some(vm.clone()) drops the previous Vm while the shared lock is held, which the comment also rules out. Please clear the shadow on every drop path, and take the old Vm out and drop it after the guard. Alternatively, keep execution_vms only while a hibernation is in progress.
  • drm/apple: bound kernel retrains of an active CRTC after hotplug (fdf58ea) and the dptxep log change (3a176c5) apply to every DCP generation with external outputs. They are fine as they are, but as standalone fixes they belong in their own PR, with a result from a board other than T6041.
  • pmdomain: apple: pmp-report (04665fe): apple_pmp_report_set_state() now goes through apple_pmp_report_hold_locked(). On T6041 that holds requests whenever the PMP is not running, not only after a restore. Please say that this is intended for a runtime PMP stop, or limit it to the restore window.
  • cpufreq: apple-soc (c163c0e) restarts the fast-die wait whenever the PMP stops after having run, on every PMP board. That's fine, but say it in the commit message.
  • rtkit-helper remove (02e9348) now skips the quiesce whenever CPU_CONTROL.RUN is clear, which also covers a coprocessor that crashed or was never started. Please confirm that the retained buffers are still released correctly on that path.

3. Smaller points

  • iomfb_hib_clean_shmem()'s comment says the restore cleaned only to the PoU (hibernate-asm.S), but d7201a4/eff3cf582304 now clean every restored page to the PoC. Either drop the extra clean or fix the comment to say why it is still needed.
  • aura_hibernate_init() takes only reg index 0 of each /reserved-memory child. A node with several reg entries leaves the others in the image. Please loop over all entries.
  • swsusp_invalidate_sig() and the blkdev_issue_flush() in swap_writer_finish() are generic. Please run them once on a non-Apple arm64 or x86 machine (or QEMU) with a swap partition: a hibernate/resume cycle, plus a resume with a flush error injected.
  • Subject prefixes: 3a176c5 and aa1d81c use drm: apple:; the tree uses drm/apple:.
  • The DCP endpoint objects that stay allocated per resume (listed in "Not done") should be bounded or freed before merge, or at least documented in the code.

4. Testing needed after the rebase

  • J616s, aura with aurora-silicon/aura#66 and hib=1, PR head: three or more hibernate/resume cycles in one boot (shutdown mode, swap file). After each one: eDP, HDMI, USB-C DP, keyboard/trackpad, battery, Wi-Fi, NVMe, GPU (a GL and a Vulkan client running across the cycle), camera, ANE and audio. Also: a resume with a corrupted image (it must boot cold and not loop), a hung restore followed by a forced power-off (the next boot must not resume the same image), and pm_test at freezer, devices, platform, processors and core.
  • J616s booted without the fixed layout: /sys/power/disk refuses, and the pm_test levels pass.
  • M1 Pro, M2 and A18 Pro: pm_test at every level (with the display on and with an external display), GPU clients killed during and after the test, power-off and reboot, s2idle. None of these may regress, since the callbacks above now run there too.
  • hibernation_mode=reboot is still open. With the 5 s macsmc wait in place, please check that a plain reboot works on the aura path before merge.

@fbalazsy

Copy link
Copy Markdown
Contributor Author

Closing as superseded. aura #77 removed the monitor, so the boot-object/fixed-layout path this series was built for no longer exists upstream. The J616s work continues on Linux at EL2 (hibernation, deep sleep and the device restore paths) in a new PR on the current integration/aurora. Thanks for the review.

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