Skip to content

soc: apple: sleep: refuse a T8132 deep sleep without a PCIe port recovery - #4181

Merged
DjDeveloperr merged 5 commits into
mainfrom
fix/t8132-sleep-refuse-no-port-recovery
Dec 3, 2026
Merged

DjDeveloperr merged 5 commits into
mainfrom
fix/t8132-sleep-refuse-no-port-recovery

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

What: If #4176's port recovery doesn't register on a T8132 Mac (the PCIe host's probe failed, its device tree leaves something out, or no driver is bound), a deep sleep still goes through, and the wake would probably reset the Mac with its unsaved work. This refuses that sleep before any device suspends. systemd-sleep's default list (mem standby freeze) should then go on to suspend-to-idle; where sleep.conf names only SuspendState=mem, it doesn't (seen on the J773g), and the Mac stays up. Stacked on #4176; meant to land before #4007, which makes deep sleep the default on the J773g.

Tested: J773g (M4 Mac mini, 27.2) at 2977a1a31237: with the recovery taken out, two mem writes returned -EBUSY and s2idle worked; with it, one deep sleep in the same boot, Ethernet, display and GPU back (receipt). Host tests and sleep.o W=1 at this head.

Risk: two-way · J773g. No change there once the recovery is registered. Without it, a plain mem write gets -EBUSY while mem_sleep stays at deep.

Not yet: a Mac whose systemd uses the default sleep list, to see it fall back to suspend-to-idle.

Details

Where the check sits. apple_sleep_begin() in drivers/soc/apple/sleep.c, after the profile check that can already refuse there. It's before the SoC's own .begin, the pins' save, the watchdog and pm_set_suspend_via_firmware(), so the failed sleep has nothing to undo: .end finds no firmware sleep and returns. The write to /sys/power/state gets -EBUSY. User space is frozen at that point and pcie-apple has no sysfs unbind, so the host can't go away between the check and the commit.

What it asks. The question the T8132 wake already asks (apcie_ports_unowned && !apcie_ports_host_owned), and that the host answers with apple_sleep_apcie_ports_owned() since #4176. The new field in the SoC's data, apcie_ports_host, says that this SoC's wake leaves the ports to their host; only t8132_sleep_soc sets it, because other SoCs bring their ports back in other ways and no host of theirs makes that call.

The default. Not touched: the first version lowered it and gave it back when the host registered, which could replace an s2idle that a user had written meanwhile (Ryan's review). What's in /sys/power/mem_sleep stays the user's.

What doesn't change

  • J773g with the recovery registered: same default, same entry, same wake, same boot log.
  • J713, J715, J604, J623, J624: their trees have apple,managed-sids, so apcie_ports_unowned is false and neither check applies. Their host refuses by itself when the radio can't be recovered.
  • Suspend-to-idle: .begin returns before the check.

Left as it is. The wake's own clocking of ports without a host (sleep_apcie_port_clock()) isn't reached any more on T8132. Removing it is a separate change, so that this one doesn't touch the wake.

Evidence

Base: #4176 at 40959794. Head: 2977a1a31237.

  • t8132-sleep-refuse-tests.py at this head, with the production apple_sleep_begin()/apple_sleep_end() and the host's apple_pcie_t8132_sleep_probe(): PASS. Host before and after apple-sleep; host gone and back; the probe failing at each port's preparation, at each DART, at both devres steps, with no ports, and with other host data, each in both probe orders; no host driver at all; managed radio; another SoC; no sleep driver; a chosen mem_sleep kept over the host's registration and removal. A refused sleep arms no watchdog, saves no pin, runs no SoC step, sets no firmware sleep, can be repeated, and leaves s2idle working.
  • 6 negative controls at this head, all caught: no refusal, the field missing on T8132, no SoC field, managed hosts included, the question inverted, the registration writing mem_sleep_current.
  • At 215ffcc8 (this head only removes lines from it): run-host-tests.py --only t8132 6/6, radio 5/5, sleep 51/51, dart- 32/32, pcie 8/8, apple-wdt 10/10, sio-engine 1/1; sleep.o and pcie-apple.o without warnings at W=1 (gcc 16.2.0 and clang, KUnit tests built in). Not rebuilt at this head yet; the J773g row's build does that.
  • checkpatch.pl --strict at 215ffcc8: sleep.c 0/0/0; the test file 0 errors, 4 warnings, 4 checks (its stub macros, a typedef, MAINTAINERS).
  • No bindings touched.

Disabling the recovery for the hardware row needs no code: a one-cell apple,tunable-port on port 2 in the DTB (pci@2,0 has none today, and on this host only the recovery reads it) can't be parsed, so the host logs no deep sleep recovery: -EINVAL and everything else probes as usual. Read from the code, not tried.

apple_dart_set_host_resumes() leaves a DART's restore after a deep sleep
to the PCIe host that clocks it. A host that claimed one port's DART and
then can't claim the next one's has to give the first one back, or
nothing restores it: the DART's own resume skips it for good.

Add the opposite call. Nothing uses it yet.

Signed-off-by: DjDeveloperr <hey@djdev.me>
The managed radio's recovery has the T8132 port's reset values inside
its own function. A host that sets its ports up itself after a deep
sleep writes the same ones: move them out, in the same order and with
the same values. No functional change.

Signed-off-by: DjDeveloperr <hey@djdev.me>
A T8132 host without the managed radio contract, as the J773g's with its
Wi-Fi/Bluetooth and Ethernet ports, has nothing that brings its ports
back after a deep sleep. apple-sleep can stop them and knows about a
host that owns them, but no host says so. The port DARTs, clocked by
their ports, are then read by their own noirq resume without a clock:
the J773g takes an SError in the resume of iommu@490000000, 15 ms into
the wake, and resets.

Take the ports over: save what only their registers hold in the noirq
suspend, and in the noirq resume write the reset values, the loader's
tunables and link limits, the clocks, the RID-to-SID and MSI routes,
then the port's DART and the root port's config space, and train the
link, each before anything that depends on it. The link's width and
speed limits are the ones the loader left. apple-sleep learns about
every port that is back, so that the next sleep stops only those.

The host takes the ports over only once every port has what its recovery
needs, and gives back what it claimed otherwise.

A port that doesn't come back is lost until a restart: its endpoint goes
back into reset, its functions are marked disconnected, its config space
and interrupt stay closed, and the next sleeps neither save over the
last good copy nor touch it. The other ports are not affected.

Hosts with apple,managed-sids (J713, J715, J604) return from the new
probe first and keep their own recovery.

Signed-off-by: DjDeveloperr <hey@djdev.me>
…very

A T8132 wake leaves the APCIe ports to their host. A board without the
managed radio contract, the J773g, has a host that says whether it sets
them up again, but nothing asks: when its probe failed, its device tree
leaves out what the recovery needs, or no driver is bound, a deep sleep
still goes through. The wake then reads the port DARTs without a clock
and the machine resets with its unsaved work.

Refuse it in .begin, before any device suspends. Suspend-to-idle takes
another path and is not affected.

Deep sleep shouldn't be the default there either. The host probes after
apple-sleep on the J773g (2.95 s and 0.05 s), so start with s2idle as
the default and make it deep sleep when the host registers. A default
that was chosen with mem_sleep_default=, or that another missing part
took away, stays as it is.

With the recovery registered, and on every host with apple,managed-sids
(J713, J715, J604, J623, J624), deep sleep and its default are as
before. The other SoCs don't set the new field.

Signed-off-by: DjDeveloperr <hey@djdev.me>

@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.

Confirmed the modular-host case called out in the description. If apple-sleep starts first, an administrator can explicitly write s2idle to /sys/power/mem_sleep before the PCIe module loads. When the host registers, apcie_default_held remains set and sleep_apcie_ports_default() overwrites that selection with deep. The next ordinary mem suspend then takes the deeper path despite the explicit selection. The exact host fixture passed unchanged. Adding that one sysfs-equivalent store before the late probe failed the assertion that s2idle stays selected.

When the PCIe host registered after apple-sleep, the driver made deep sleep
the default again, also over an s2idle that was written to
/sys/power/mem_sleep in the meantime. Drop the change of the default and
keep the refusal, which is what protects the machine. systemd-sleep tries
"freeze" when "mem" fails, so a Mac without the recovery still suspends
to idle there.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Right, and the change of the default wasn't needed. 2977a1a31237 drops it: what's in /sys/power/mem_sleep stays the user's, and only the refusal is left (27 lines fewer in sleep.c). The host test now checks that a chosen s2idle or deep survives the host's registration and removal. J773g row still owed.

RyanTheTide
RyanTheTide previously approved these changes Dec 3, 2026

@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.

The late-PCIe-probe case is fixed. Registration now updates only recovery ownership, so the selected sleep mode survives probe and removal. The early deep-sleep refusal remains in place. I ran the exact-head host fixture and it passed, including both explicit selections. The J773g hardware row remains pending.

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Hardware verification receipt at exact head 2977a1a31237 (J773g / M4 Mac mini, Mac16,10, os-fw 27.2 / 26B123):

  • Result: PASS on both boots (a) and (b).
  • Build & W=1 check: 7.3.0-j773g-riders+ (origin/main dc4d6f065001 + #4176 40959794ea75 + #4164 0bc1cc98c029 + #4007 c77edccad388 + #4181 2977a1a31237 + riders #3485/#3486/#4175, Image 37ad50d13179…). make LLVM=1 W=1 drivers/soc/apple/sleep.o: 0 warnings.
  • Boot (a) — one-cell apple,tunable-port = <0x00>; on pci@2,0 (t8132-j773g-no-pcie-rec.dtb be9fb27cae65…, 1-line DT diff from normal DTB; boot_id=d88bbb9f-e43d-49d3-bf45-c0190d8c174f):
    • Only recovery registration failed: pcie-apple 1cb0000000.pcie: no deep sleep recovery: -EINVAL (both pci@0,0 and pci@2,0 links came up normally).
    • /sys/power/mem_sleep stayed s2idle [deep] (default left untouched).
    • Plain echo mem > /sys/power/state twice: both immediately failed with -EBUSY (write error: Device or resource busy), logging apple-sleep 380700000.sleep-controller: refused: no host sets the PCIe ports up again after a deep sleep (suspend_stats success=0).
    • rtcwake -d /dev/rtc1 -m freeze -s 10 (s2idle): PASS (RC=0, PM: suspend entry (s2idle) -> PM: suspend exit, suspend_stats success=1, end0 10.55.0.5 up, same boot_id).
    • systemctl suspend note: on this host (systemd 262.2-1-arch, /etc/systemd/sleep.conf sets SuspendState=mem, MemorySleepMode= empty), systemd-sleep attempted mem, hit -EBUSY (apple-sleep: refused), and exited 1 (Failed to put system to sleep. System resumed again: Device or resource busy) without falling back to s2idle — the Mac stayed up with end0 intact and was protected from entering deep sleep.
  • Boot (b) — normal t8132-j773g.dtb (172790e3f865…, same Image; boot_id=c6cc5648-8b9f-4d5d-bfae-93852240f15f):
    • pcie-apple 1cb0000000.pcie: T8132 multi-port deep sleep recovery registered; /sys/power/mem_sleep = s2idle [deep].
    • Committed deep x1 (rtcwake -d /dev/rtc1 -m mem -s 15): PASS (RC=0, suspend_stats success=1 fail=0, same boot_id), SSH over end0 up, card1-DP-3 connected/enabled, PCIe LnkSta (5GT/s x1 + 2.5GT/s x1), post-wake gpubench-vk --verify (compute.heavy depths 1,4) status=ok / valid=true, 0 SError, 0 WARNING/BUG/Oops.

@DjDeveloperr
DjDeveloperr changed the base branch from fix/t8132-apcie-deep-sleep to main December 3, 2026 11:51
@DjDeveloperr
DjDeveloperr dismissed RyanTheTide’s stale review December 3, 2026 11:51

The base branch was changed.

@DjDeveloperr
DjDeveloperr marked this pull request as ready for review December 3, 2026 11:51
@DjDeveloperr
DjDeveloperr requested review from a team as code owners December 3, 2026 11:51
@github-actions github-actions Bot added area:gpu Area from CODEOWNERS (bot) area:platform Area from CODEOWNERS (bot) ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 3, 2026
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

Merge readiness at 2977a1a3: not ready yet

Check Detail
CI at head no failing: pr-text / pr-text
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head ok in the PR body at 2977a1a3
Conflicts ok none
PR text ok Risk: two-way · J773g. No change there once the recovery is registered. Without it, a plain mem write gets -EBUSY while memsleep stays at deep.

Areas: area:gpu, area:platform

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

The merge of main dbfe45ac89b6 + #4176 40959794 + this head is clean. Built and tested on the same merge with main c5bdd6820568 (without #4183 and #4070, which change the scaler driver, afk.c and dcp.c only): sleep.o W=1 with and without the KUnit cases, 0 warnings (clang 23.1.3, arm64). Host tests that read these files, in the Ubuntu 24.04 CI toolchain image, with #4164 on top as well: 118/118. t8132-sleep-refuse-tests is among them.

Hosted KUnit, apple-build and host-tests never ran on this head (it was a draft on a branch base), and this merge doesn't wait for their queue (hosted CI override, Dj). Runtime evidence is the J773g row at this head. Its kernel also carried #4164, #4007, #3485, #3486 and #4175.

From that row, and the body now says so: with SuspendState=mem in sleep.conf, systemctl suspend returned an error after the refusal and didn't go on to suspend-to-idle. The Mac stayed up. The fallback with systemd's default list wasn't tried.

The PR was retargeted to main so that #4176's merge didn't close it. GitHub then dismissed Ryan's approval of 10:58Z on its own ("The base branch was changed"). The head is the approved one, 2977a1a31237, and with #4176 merged (65ce0ad9be87) the change is the reviewed two commits again. It's merged with the maintainer admin merge on that basis.

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

Labels

area:gpu Area from CODEOWNERS (bot) area:platform Area from CODEOWNERS (bot) ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants