Repository navigation
PM / soc: apple: sleep: go to s2idle when a driver vetoes deep sleep - #4245
Conversation
When the platform knows before a deep sleep starts that it would refuse it, because a driver that can't take part has said so, the sleep still runs until the refusal: the suspend fails and the machine stays awake, although suspend-to-idle, which needs nothing from the platform, would have worked. Add an optional deep_vetoed() callback to platform_suspend_ops. pm_suspend() asks it for PM_SUSPEND_MEM before anything is frozen and, on a veto, suspends to idle for that one sleep. mem_sleep keeps what user space chose, and the next "mem" asks again. A refusal found once the sleep has started still fails it, as before. Signed-off-by: DjDeveloperr <hey@djdev.me>
Some refusals of a deep sleep are known before it starts: a T6020 PMP whose boot loader didn't publish apple,sram-keep refuses every one (linux#4238), and a USB4 port refuses one while a PCIe function behind it can't be removed. They come after devices have suspended (the PMP's suspend_late) or after the freeze has begun (the port's PM notifier), so the suspend fails and the Mac stays awake. Add devm_apple_sleep_deep_veto(). A driver registers with a check that is asked before each deep sleep, or with none to veto every one, until it unbinds. The list is kept out of struct apple_sleep, so a driver may register before this one probes. The sleep driver answers the PM core's deep_vetoed() from that list and from .begin's check for PCIe ports no host sets up again after a deep sleep (T8132). Such a sleep then goes to s2idle. .begin keeps its check, for a host that leaves in between. Signed-off-by: DjDeveloperr <hey@djdev.me>
A deep sleep takes the port's domains down. At PM_SUSPEND_PREPARE its teardown removes the PCIe functions behind it, and refuses the sleep when one can't be removed: a driver other than nvme or igc, such as nouveau and snd_hda_intel for a Thunderbolt eGPU, or a disk in use. The suspend then fails and the Mac stays awake (J413 with a GTX 1080 enclosure, linux#3786). Add apple_pcie_tunnel_removable(), which answers what apple_pcie_tunnel_remove_orderly() would, without removing anything or waiting. The port asks it before each deep sleep starts and vetoes the sleep when the answer is no. That sleep goes to s2idle, which keeps the port and its functions up. Signed-off-by: DjDeveloperr <hey@djdev.me>
Builds pm_suspend(), the sleep driver's veto list and the USB4 port's check from the tree, with the rest stubbed. A deep sleep goes to s2idle while a driver vetoes it (a static veto, orphaned T8132 PCIe ports, an eGPU or a disk in use behind a port) and is deep again once the veto is gone; s2idle and shallow are never changed, and the sleep's error is still returned. Three mutants, the PM core ignoring the veto, the list ignoring a driver's check, and a silent port, each fail it. Signed-off-by: DjDeveloperr <hey@djdev.me>
…heck apple_pcie_tunnel_removable() repeated the checks of apple_pcie_tunnel_remove_orderly() without its wait for a disk that udev opened just after it appeared. A deep sleep asked for in that half second went to s2idle, where before the removal waited and the sleep was deep. Move the checks, wait included, into one helper that both call, so the question always gets the removal's answer. Signed-off-by: DjDeveloperr <hey@djdev.me>
apple_cio_find_pcie_tunnel() finds no host until the tunnel is populated, so the deep-veto check's own test of the requested and populated flags added nothing: drop it. The test's lookup now does the same, and a tunnel that was asked for but isn't up yet no longer vetoes the sleep there either: the port's teardown doesn't refuse it. Signed-off-by: DjDeveloperr <hey@djdev.me>
The check runs under the veto list's lock, which the device's unbind takes to drop the veto. A check that took device_lock() would deadlock against that unbind. Signed-off-by: DjDeveloperr <hey@djdev.me>
kernel-doc -Wall found no description of its return value. Signed-off-by: DjDeveloperr <hey@djdev.me>
DjDeveloperr
left a comment
There was a problem hiding this comment.
Review at 1834a363, fixes pushed up to b089aa8f. One round from me. The design holds. Nothing changes unless something vetoes, and today every case that vetoes fails its deep sleep anyway. It still needs hardware rows before it can leave draft (list at the end).
Is there an Apple-only way with less code? I looked. The sleep driver already lowers mem_sleep_current at probe (binned parts, untested profiles), and #4207 does the same for legacy firmware. For #4238's PMP alone, that would give the same result with no PM-core change. It doesn't work for the vetoes that come and go: an eGPU or a busy disk behind a USB4 port, or T8132 ports whose host driver loads later. It would also change mem_sleep behind the user's back. The other routes don't work either. .valid() turns the sleep into -EINVAL. Running s2idle from .enter comes after the PREPARE notifier and suspend_late that do the refusing. So one optional member and seven lines in pm_suspend() is the smallest route, and this tree already carries PM-core changes (dark wake, pm_set_suspend_no_platform()). Fine as it is.
What I checked and found right:
- Ordering. The question is asked before the sync, the notifiers and the freeze. At that point user space is running, as it still is at
PM_SUSPEND_PREPARE, so the check and the teardown see the same state. - Lock order. The veto lock is taken first, then
acio->lock, thenpcie_tunnel_lock, then the rescan lock. That is the teardown's order. - Probe and unbind. The port registers after its drvdata and both mutexes exist, so a sleep that races its probe gets "no". On unbind, the devres drop runs before the mutexes are destroyed and before drvdata is cleared.
- s2idle and shallow are never asked:
state_store()mapsmemtomem_sleep_currentfirst. - Errors. The s2idle sleep's error is still returned.
- Strict ports. Leaving out
strict_power_cycleports is right: they sleep throughtb_apple_sleep_quiesce(), not the PCIe removal.
Fixed on the branch (4 commits):
d5bf9854:apple_pcie_tunnel_removable()repeated the removal's checks but not its wait (up to 500 ms) for a disk that udev opened just after it appeared. Right after you plug in a drive, a sleep that used to be deep went to s2idle. Both functions now call one helper, so the question always gets the removal's answer.64e55fd3: the port's check tested the requested/populated flags itself, butapple_cio_find_pcie_tunnel()already returns nothing until the tunnel is populated, so I dropped those two lines. The test's lookup stub ignored that. Its case "tunnel asked for, not up yet: s2idle" claimed a veto the driver never makes. The stub and that case now match the driver (deep).7ed82d75: the header now says a check must not takedevice_lock(dev). The unbind takes the list's lock to drop the veto, so a check that took the device lock would deadlock with it.b089aa8f: aReturn:section for the new function (kernel-doc -Wall).
Our rules for merged code:
- No switch or module parameter.
- No new magic constants: the 10 × 50 ms wait was moved, not added.
- No new SoC or board gate: the port registers on the same hw-data flags that decide whether its teardown runs.
- Nothing that the ADT carries.
Does the test fail on main? Yes:
- On all of main it doesn't find the new functions.
- With main's
kernel/power/suspend.cand this branch's drivers, it fails withstatic veto: s2idle instead of deep: asked deep, entered deep. - Each of the 3 mutants fails it.
Follow-up, not blocking. A vetoed sleep is the same sleep as echo freeze with deep as the default. Four places decide "deep or not" from pm_suspend_default_s2idle() alone at PM_SUSPEND_PREPARE, so they already misread that sleep as deep today:
- pcie-apple's Neo radio notifier;
- DCP's J714s crashed-display refusal;
- the T6050 combo PHY;
apple_cio_t6050_sleep().
None of them runs on a Mac that can veto at this head. The vetoes are the T6020 PMP (with #4238), T8132's orphan check, and USB4 ports on T8103/T8112/T6020/T6030/T8142/T6040. If a veto ever reaches J700 or J714s, those four should also check pm_suspend_no_platform(), as dwc3-apple and pcie-apple's other check do.
Tested at b089aa8f:
sleep-deep-veto-tests.pypasses 17/17, and all 3 mutants fail it.- Host tests in the CI image: sleep 58/58, cio 6/6, pcie 10/10, apcie 2/2, tb- 4/4, sep-suspend 22/22, display26 1/1, sio-engine 1/1.
- W=1 (arm64, LLVM, J700
aurora_defconfig) of the four objects: no compiler warnings.kernel-doc -Wallon the touched files gives the same warnings as main. checkpatch --strict: the seven kernel commits are clean.
Owed before it leaves draft:
- J700, deep stays deep with no veto (no-regression, second generation). Kit
work/j700-linux4245-b089aa8f-veto-row-20261203(manifest7ebc4038), handed to the Neo lead. Arms: full kernels of maina0e5cf15and this head, each 3×rtcwake -m mem -s 15under the 60 s watchdog and the 30-min dead-man. Every sleep must logPM: suspend entry (deep)and novetoedline, withmem_sleeps2idle [deep]throughout. - J414s on the old Stage 2 with #4238 plus the PMP veto commit (
lab/j414s-4238-deep-veto1a3cc12d9a9d):memgoes to s2idle and wakes. It goes in the next J414s PEER kit, as a fix arm against #4238 alone. - J413 with the eGPU bound. We don't have a J413, so this one needs a PEER row.
Brings the branch up to main after #3786 (s2idle fallback when deep sleep is refused for retained PCIe-C, J413 fw27 deep by default) and #3540 (thunderbolt: apple: lost-link recheck). Conflicts: - drivers/pci/controller/pcie-apple.c: both sides split the checks out of apple_pcie_tunnel_remove_orderly(). Main's helper, apple_pcie_tunnel_can_remove_orderly() and remove_orderly() are taken as they are. This branch's apple_pcie_tunnel_kept() and apple_pcie_tunnel_removable() stay for now; the next commit drops them. - tools/asahi/run-host-tests.py: both test entries kept. kernel/power/suspend.c merged without a conflict: the platform veto in pm_suspend() runs before enter_state(), and main's fallback in enter_state() still covers a deep sleep refused at PM_SUSPEND_PREPARE. Signed-off-by: DjDeveloperr <hey@djdev.me>
Main gained apple_pcie_tunnel_can_remove_orderly() with #3786: the same non-destructive check as this branch's apple_pcie_tunnel_removable(), with the same returns (0 when removable or already stopped, -EOPNOTSUPP for a driver the teardown doesn't remove, -EBUSY for a disk in use, -ENODEV/-EINVAL for no tunnelled host). Use it for the port's deep veto and drop the copy, so pcie-apple.c and pci-apple.h are main's again. The only difference is that the PCIe host now logs why the hierarchy stays, as it does when the teardown asks. Signed-off-by: DjDeveloperr <hey@djdev.me>
|
Brought this branch up to main Commits
The overlap with #3786
Checks on the merged head (pc)
Kits at this head
The v1 kits ( |
|
Verified the J413 NO-eGPU deep leg of peer-qualification kit v2 ( Kit & Boot Verification
|
|
J700 (MacBook Neo, A18 Pro, T8140) row at the exact head
Both arms are full kernels (Image, every module and the J700 DTB), built with the stock
The whole boot had 0 veto lines on either arm. The J700 was verified back on stock after each boot. Not covered:
|
DjDeveloperr
left a comment
There was a problem hiding this comment.
Independent review at head 9f9a73082144, merged onto main 35ddf947b124. The merge is clean, and its diff against main is the same 8 files as against cacc5d31. Sound, merging.
What I checked in the code
- With no veto, deep sleep is the old path.
pm_suspend()only changesstatewhendeep_vetoedis set and returns true. Every other platform leaves it NULL. The apple-sleep driver is built in, can't be unbound, and setsapple_sleepbeforesuspend_set_ops(), so the callback never sees a NULL driver. - The order of the veto and the fallback. The veto runs in
pm_suspend()beforeenter_state()takessystem_transition_mutex. Nothing is frozen, no notifier has run and no device has suspended yet, so a veto can't leave anything half suspended or skip a resume. It only changes which stateenter_state()gets. #3786's fallback inenter_state()only acts onPM_SUSPEND_MEM, so it never runs again after a veto. In the s2idle sleep,pm_suspend_no_platform()makes the port's PREPARE check and teardown step aside. - Locking. The list lock is taken by register, drop and the veto query. The port check takes
acio->lock, thenpcie_tunnel_lock, then the PCI rescan lock. That is the same order the teardown uses. Nothing takes the list lock while holding one of those three, so this can't deadlock. - Probe and unbind. The veto is registered after drvdata and both mutexes are set up. Before a cable is up,
current_cable_infois 0 and the check returns false. On unbind,removingis checked first. The veto is dropped beforeacioand its mutexes go. Ifdevm_add_action_or_reset()fails, it removes the list entry at once. - The port vetoes on any error from
apple_pcie_tunnel_can_remove_orderly(), the same function and arguments the PREPARE check uses. The teardown refuses a deep sleep on any of those errors, so a veto turns a refusal into s2idle. It never stops a deep sleep that would have worked. - No runtime switch or module parameter, no magic constants, no new SoC or board gate. The port registers on the existing per-compatible
sleep_removes_domainsandstrict_power_cycleflags. No ADT values are involved.
Checks on the merge (pc)
- Host tests in the CI image: 26 suites, 81 of 81 runs pass.
sleep-deep-veto17/17, and each of its 3 mutants fails it.sleep-s2idle-fallbackpasses, and each of its 4 mutants fails it. Every suite that readssleep.c,sleep.h,suspend.c,suspend.horthunderbolt/apple.cpasses, plusapple-wdt12/12 andt6030-sleep-qblkfor main'ssleep.cchanges sincecacc5d31. - W=1, arm64, clang 23 (incremental, on the J414s/J413 kit config), for
suspend.o,power/main.o,sleep.o,thunderbolt/apple.oandpcie-apple.o: 0 compiler warnings on the merge and on main. The 194 kernel-doc lines are the same on both. checkpatch.pl --strict: the kernel commits are clean. The test commit only gets flags on its fixture stubs.- Hosted CI is green at the head.
- Since then main has moved to
bae84a38eab4(#4267 tbnet, #4042 J815 thermal zones). It touches none of this PR's files, and GitHub still reports MERGEABLE.
Rows at this head
- J700 (T8140): PASS. 3 of 3 deep sleeps on each arm, no veto or fallback lines.
- J413 (T8112, macOS 27.2 firmware), no eGPU: PASS (satchlj). Deep sleep stays deep. The two GPU "fails" in that judge come from the kit:
probe_gpu()only acceptsDRIVER=asahi, and 27.2 bindsaurora.
Not yet
- The eGPU veto leg has not run on hardware. No peer has a Thunderbolt eGPU. The host tests cover that path.
- J414s with #4238 and the PMP veto commit.
- A vetoed
memunderpm_testcoreorprocessorsreturns-EAGAIN.
I updated the PR text to match the head. It now names apple_pcie_tunnel_can_remove_orderly(), lists the rows and says the eGPU leg hasn't run on hardware. The head did not change.
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
What: When a driver knows before a deep sleep starts that it would refuse it,
memnow goes to s2idle for that sleep, instead of failing and leaving the Mac awake.platform_suspend_ops.deep_vetoed().pm_suspend()asks it forPM_SUSPEND_MEMbefore anything is frozen. On a veto that one sleep is s2idle./sys/power/mem_sleepstays as the user set it, and the nextmemasks again. With no veto, the path is the same as before.devm_apple_sleep_deep_veto(dev, check). A driver vetoes every deep sleep (no check) or answers before each one, until it unbinds. It may register before the sleep driver probes. The sleep driver also answers from.begin's existing check for T8132 PCIe ports that no host sets up again (.beginkeeps it).thunderbolt/apple.c): a port whose teardown would refuse a deep sleep because a PCIe function behind it can't be removed vetoes the sleep. That covers an eGPU (nouveau, snd_hda_intel) or a disk in use. The port asks main'sapple_pcie_tunnel_can_remove_orderly()(from arm64: dts: apple: t8112-j413-fw27: enable deep sleep by default with PCIe-C s2idle fallback #3786), the same check the teardown uses. This PR no longer touchespcie-apple.c.What it covers:
lab/j414s-4238-deep-veto(1a3cc12d9a9d), for soc: apple: pmp: take T6020 SRAM keep range from DT #4238 to take.-EBUSY,apple_pcie_prepare, the legacy GPU backend from soc: apple: sleep: keep s2idle default on legacy GPU/DCP firmware #4207),.prepare_late,apple_sleep_refuse()at.enter, and a USB4 router that doesn't go to sleep.How it fits with #3786: the veto runs in
pm_suspend()before the sleep starts. #3786's fallback runs inenter_state()when a deep sleep is refused at PM_SUSPEND_PREPARE. Both stay. The fallback still covers what the veto can't see: ports withstrict_power_cycle(they don't register a veto), and an eGPU or busy disk that shows up between the veto and PREPARE.#4207 makes s2idle the boot default when the DT shows legacy firmware. This PR handles a deep sleep that is vetoed at the moment it is asked for. They don't overlap. #4207's GPU refusal could register a veto later.
Tested:
9f9a73082144:mem_sleepunchanged.PM: suspend entry (deep), woke, no veto or fallback lines,mem_sleepunchanged.35ddf947b124, in the CI image (aurora-linux-host-tests:ubuntu2404): 26 suites, 81 of 81 runs pass.sleep-deep-veto17/17, and each of its 3 mutants fails it;sleep-s2idle-fallbackpasses, and each of its 4 mutants fails it. The other 24 are every suite that reads a touched file, plusapple-wdt12/12 andt6030-sleep-qblkfor main'ssleep.cchanges since the last merge: apple-router-sleep, cio-native, dart-cio-owner, dart-sleep-power, dart-sleep-snapshot, dart-sleep-writer, sep-suspend-errors 22/22, sio-engine-reset, t8122-apcie-wake, t8132-sleep-refuse, tb-cio-sleep, display26, dcpext26-suspend-route, mtp-pm, apple-pwm-pm, neo-stability 11/11, pcie-adopted-clkpm, pciec-policy, radio-sleep-power, radio-sleep-snapshot, t8132-apcie-sleep and tb-dock-pm.kernel/power/suspend.o,kernel/power/main.o,drivers/soc/apple/sleep.o,drivers/thunderbolt/apple.oanddrivers/pci/controller/pcie-apple.oon the same merge: 0 compiler warnings, the same as main. The kernel-doc lines are the same as on main.checkpatch.pl --strict: the kernel commits are clean. In the test commit it flags only the fixture's kernel stubs (typedef, EXPORT stubs, macro arguments).Risk: two-way door. Nothing changes unless something vetoes. On
mainonly T8132 with orphaned ports or a USB4 port with an unremovable PCIe function vetoes, and both of those fail their deep sleep or fall back to s2idle at PREPARE today. A wrong veto, such as a disk that udev holds open for a moment, gives an s2idle instead of a deep sleep, never a failed suspend. The core change is one optional member inplatform_suspend_ops.Not yet:
memshould go to s2idle with the veto line and no fallback line. No peer has a Thunderbolt eGPU. The host tests cover this path.lab/j414s-4238-deep-veto1a3cc12d9a9d):echo memgoes to s2idle and wakes. That needs soc: apple: pmp: take T6020 SRAM keep range from DT #4238.pm_testatcoreorprocessorsdoesn't support s2idle, so a vetoedmemunder those test levels returns-EAGAIN.