Skip to content

soc: apple: sleep: fix APCIe auto-PM teardown and BUSY-latch lowering - #3950

Merged
DjDeveloperr merged 1 commit into
aurora-silicon:mainfrom
IronsideXXVI:j604-sleep-apcie-autopm-teardown
Dec 3, 2026
Merged

DjDeveloperr merged 1 commit into
aurora-silicon:mainfrom
IronsideXXVI:j604-sleep-apcie-autopm-teardown

Conversation

@IronsideXXVI

@IronsideXXVI IronsideXXVI commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

What: Fix three interacting APCIe auto-PM teardown and PS_BUSY-latch defects in drivers/soc/apple/sleep.c seen during repeated T8132 deep-sleep cycles with hardware PS_AUTO_ENABLE active on apcie_sys_st and apcie_phy_sw:

  1. In sleep_ps_wait_on(), wait for FIELD_GET(PS_ACTUAL, val) == target rather than merely non-zero.
  2. Mask hardware-owned PS_BUSY (BIT(11)) in sleep_ps_lower_start() and sleep_ps_lower_finish(), and make sleep_ps_lower() wait via sleep_ps_wait_on() after clearing PS_AUTO_ENABLE before writing PS_TARGET = 0 so lowering an auto-gated domain does not latch PS_BUSY.
  3. In apple_sleep_lower_apcie() (non-gen4 path; apcie_gen4 early return preserved unchanged), clear PS_AUTO_ENABLE and wait for full power on apcie_sys_st, apcie_sys_gp, and apcie_phy_sw before stopping ports, powering off the PHY/clkgen, and disabling AXI links, then lower each domain in order via sleep_ps_lower_finish(). This prevents skipped PHY/clkgen teardown when apcie_sys_st is auto-gated (PS_ACTUAL = 0), mid-MMIO auto-gating races, and PS_BUSY latches.

Tested:

  • Offline on aurora-silicon/linux main (bf2931e57045, branch j604-sleep-apcie-autopm-teardown, commit 255b646d48d4):
    • scripts/checkpatch.pl --strict: 0 errors, 0 warnings, 0 checks (132 lines checked).
    • Clang 18.1.8 ARM64 LLVM=1 W=1: drivers/soc/apple/sleep.o 0 warnings (2488664 bytes).
    • ARM64 QEMU KUnit: apple-sleep-soc-data 16/16 passed (0 failed).
  • Full-kernel release build (linux-aurora 7.3.0.aurora1-1.1685, commit 89d82de1c143): 0 compiler warnings in drivers/soc/apple/sleep.c, ARM64 QEMU KUnit apple-sleep-soc-data 6/6 passed (202/202 total).
  • Live Apple M4 MacBook Pro 14" (J604, T8132, OS FW 27.2 / 26B123) hardware verification on integrated kernels carrying the identical sleep.c diff (89d82de1c143):
    • 1.1685 (boot b650c46a): 1/1 deep-sleep suspend/resume wake passed (rc=0, 0 SErrors, 0 kernel faults), 0 PMP events (600 s idle), 33.5 C idle thermal gate, 1582.7 fps ProRes decode gate.
    • 1.1686 (1e959da5e66c, boot b14ae371): 10/10 deep-sleep (10x 600 s rtcwake -m mem) + 10/10 s2idle (10x 300 s) soak passed (150.0 mW deep / 799.5 mW s2idle median asleep, 1.65 s awake, 1.35 s Wi-Fi recover, 10/10 AOP IIO sensors + MTP keyboard intact, 0 SErrors, 0 L2C/kernel/GPU faults, 0 resets).

Risk: two-way door · all Apple Silicon SoCs with deep sleep enabled (drivers/soc/apple/sleep.c, +51 / -23 lines; apple_sleep_lower_apcie() teardown ordering applies to non-gen4 APCIe while sleep_ps_lower(), sleep_ps_wait_on(), and PS_BUSY masking apply to every sleep_ps_lower() caller including gen4 APCIe, USB, ATC0, and PMP) — every sleep_ps_wait_on() wait is bounded by PS_OFF_TIMEOUT_US (5000 us), domains without PS_AUTO_ENABLE take the exact same immediate lowering path with PS_BUSY masked, and a plain revert restores prior lowering behavior.

Not yet:

  • Standalone exact-head 255b646d48d4 (main bf2931e57045) multi-cycle deep-sleep row on J604 (T8132); second non-gen4 controls are covered by the J413 (T8112) and J700 (T8140) exact-head rows in comments below.
Details and verification

Scope (j604-sleep-apcie-autopm-teardown, head 255b646d48d4 on main bf2931e57045)

  • drivers/soc/apple/sleep.c (+51 / -23)
  • Sign-off: Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
  • scripts/checkpatch.pl --strict: 0 errors, 0 warnings, 0 checks (132 lines checked).
  • Clang 18.1.8 ARM64 LLVM=1 W=1: drivers/soc/apple/sleep.o 0 warnings (2488664 bytes).
  • ARM64 QEMU KUnit: apple-sleep-soc-data 16/16 passed (0 failed).

Mechanism summary

  • Under hardware auto-PM (PS_AUTO_ENABLE), idle APCIe domains (apcie_sys_st, apcie_phy_sw, apcie_sys_gp) can report PS_ACTUAL = 0 while PS_TARGET = 0xf.
  • Previously apple_sleep_lower_apcie() checked sleep_ps_on() (which checks PS_ACTUAL != 0) before calling sleep_apcie_phy_off() / sleep_apcie_clk_off(), silently skipping PHY/clkgen teardown when apcie_sys_st or apcie_phy_sw happened to be auto-gated, and sleep_ps_lower() wrote PS_TARGET = 0 immediately after clearing PS_AUTO_ENABLE without waiting for PS_ACTUAL == target or masking hardware-owned PS_BUSY.
  • Clearing PS_AUTO_ENABLE and waiting for full PS_ACTUAL == target on apcie_sys_st, apcie_sys_gp, and apcie_phy_sw before port stop / PHY / clkgen / AXI teardown, then finishing each lower in order with PS_BUSY masked, eliminates the skipped teardown and PS_BUSY latch.

During repeated T8132 deep-sleep cycles with hardware PS_AUTO_ENABLE
active on apcie_sys_st and apcie_phy_sw, an idle auto-gated domain
reads PS_ACTUAL = 0 and causes apple_sleep_lower_apcie() to skip
PHY/clkgen teardown or race against mid-MMIO auto-gating, and writing
PS_TARGET = 0 without first waiting for the domain to wake after
clearing PS_AUTO_ENABLE can latch PS_BUSY.

Fix all three defects in drivers/soc/apple/sleep.c:
- In sleep_ps_wait_on(), wait for FIELD_GET(PS_ACTUAL, val) to reach
  the full target state rather than merely non-zero.
- Mask hardware-owned PS_BUSY in sleep_ps_lower_start() and
  sleep_ps_lower_finish(), and make sleep_ps_lower() wait for the
  domain to wake after clearing PS_AUTO_ENABLE before writing
  PS_TARGET = 0.
- In apple_sleep_lower_apcie(), clear PS_AUTO_ENABLE and wait for full
  power on apcie_sys_st, apcie_sys_gp, and apcie_phy_sw before stopping
  ports, powering off the PHY/clkgen, and disabling AXI links, then
  lower each domain in order via sleep_ps_lower_finish().

Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
@IronsideXXVI
IronsideXXVI requested review from a team as code owners December 2, 2026 14:58
@github-actions github-actions Bot added area:platform Area from CODEOWNERS (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 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Merge readiness at 255b646d: not ready yet

Check Detail
CI at head ok 9 checks passed
Review ok approved by DjDeveloperr
Requested changes no review verdict FINISH at 255b646d
Hardware row at head ok in the PR body at 255b646d
Conflicts ok none
PR text ok Risk: two-way · all Apple Silicon SoCs with deep sleep enabled (drivers/soc/apple/sleep.c, +51 / -23 lines; applesleeplowerapcie() teardown ordering applies to non-gen4 APCIe while sleeppslower(), sleeppswaiton(), and PSBUSY masking apply to every sleeppslower() caller including gen4 APCIe, USB, ATC0, and PMP) — every sleeppswaiton() wait is bounded by PSOFFTIMEOUTUS (5000 us), domains without PSAUTOENABLE take the exact same immediate lowering path with PSBUSY masked, and a plain revert restores prior lowering behavior.

Areas: area:platform

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

@satchlj satchlj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Verdict: FINISH, then merge at 255b646

Blockers:

  1. Wait for the approved Actions CI runs (apple-build, host-tests, pr-text, readiness) to complete green at 255b646d48d4.
  2. Post an exact-head 255b646d48d4 hardware deep-sleep row (and second non-gen4 SoC control row, per merge policy §1/§3 — currently listed under Not yet).

Checked:

  • Audited the full 74-line diff in drivers/soc/apple/sleep.c at 255b646d48d4: no code blockers found.
  • sleep_ps_wait_on() polling FIELD_GET(PS_ACTUAL, val) == target (bounded by PS_OFF_TIMEOUT_US) and masking hardware-owned PS_BUSY (BIT(11)) in sleep_ps_lower_start()/sleep_ps_lower_finish()/sleep_ps_lower() cleanly prevents latching PS_BUSY when lowering an auto-gated domain.
  • In apple_sleep_lower_apcie() (non-gen4 path; apcie_gen4 untouched), clearing PS_AUTO_ENABLE and waiting for full power on apcie_sys_st, apcie_sys_gp, and apcie_phy_sw before sleep_apcie_port_stop_once(), sleep_apcie_phy_off(), sleep_apcie_clk_off(), and APCIE_AXI_EN0/1 teardown prevents silent teardown skips and mid-MMIO auto-gating races, then lowers each domain in order via sleep_ps_lower_finish().
  • Non-blocking note: sleep_apcie_port_stop_once() also checks sleep_ps_on(s, s->soc->ps.apcie_gp); if apcie_gp itself ever uses PS_AUTO_ENABLE on any non-gen4 SoC, it would still be checked via raw PS_ACTUAL != 0.

@github-actions github-actions Bot added the changes-requested A reviewer requested changes (bot) label Dec 2, 2026
@satchlj

satchlj commented Dec 2, 2026

Copy link
Copy Markdown
Member

Row: 255b646d48d4 on J413 (MacBook Air 13" M2, T8112, 27.2 stub) + default cmdline (s2idle) + PASS (regression control).

  • Kernel = exact PR head 255b646d48d4 (aurora_defconfig, LLVM=1), Stage 2 = Aura 1.0.0b18 + uboot-aurora 2026.07.aurora13-1
  • Boot: 0 failed units, pcie-apple 690000000.pcie probed cleanly, wlan0 (BCM4387/7) UP, Bluetooth (hci_bcm4377) UP, 3 MTP input devices, 0 SErrors/warnings
  • Default cmdline ([s2idle]): 2/2 rtcwake -d rtc1 -m freeze -s 12 PASS (wlan0 UP, BT powered, 3 MTP inputs, 0 failed units, 0 SErrors/warnings)
  • Follow-on committed deep sleep (apple_sleep.deep=1 apple_sleep.untested_commit=1, mem_sleep=deep): 2/2 rtcwake -d rtc1 -m mem -s 15 PASS (boot object resumes 0 -> 1 -> 2, agx=1 gfx_err=0, pcie-apple 690000000.pcie: S2R: /soc/pcie@690000000/pci@0,0 link back, PWREN and PERST# cycled as at probe, wlan0 and hci_bcm4377 recovered cleanly, 0 failed units, 0 SErrors/warnings)

Note: J413 is a T8112 regression control only; this does not exercise the T6040/T6041 apcie_st/apcie_gp busy-clear path, which still needs an exact-head hardware row on a target board.

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

Core review at 255b646d48d4: approve. Merge waits on the exact-head T8132 deep-sleep row from your "Not yet" list.

Checked:

  • The new sleep_ps_wait_on() waits for PS_ACTUAL == target, and every wait is bounded by PS_OFF_TIMEOUT_US. A domain that never settles costs at most 5 ms and then takes the old skip path.
  • PS_BUSY is masked in all three write sites, and moving the define up is a plain move.
  • apple_sleep_lower_apcie(): waking apcie_sys_st, apcie_sys_gp and apcie_phy_sw before teardown fixes the skipped PHY and clkgen teardown when apcie_sys_st reads PS_ACTUAL = 0 under auto-PM. The phy_on gate on sleep_apcie_port_stop_once() is safe, because that helper already returns early when apcie_phy_sw or apcie_gp is off.

Notes, not blockers:

  • The Risk line says the gen4 path is untouched. apple_sleep_lower_apcie_gen4() and every other sleep_ps_lower() caller (USB, ATC0, PMP, the macOS-on domains) now also wait for the domain to come up after PS_AUTO_ENABLE is cleared. That's bounded and looks right, but the blast radius is every deep-sleep SoC, not only non-gen4 APCIe. Please reword the Risk line.
  • sleep_apcie_port_stop_once() still skips the port stop when apcie_gp itself is auto-gated (PS_ACTUAL = 0), because only sys_st, sys_gp and phy_sw are woken here. If J604 never shows apcie_gp idled at that point, it's fine as is.

Rows:

  • Second generation: satchlj's exact-head J413 (T8112) PASS covers it (comment 7353357139).
  • We're adding an exact-head J700 (T8140) deep-sleep control from our fleet, because this path runs on every non-gen4 deep-sleep SoC.
  • Still needed: your exact-head T8132 multi-cycle deep-sleep row. Once it posts, the frontier merges.

@github-actions github-actions Bot removed the needs-review No approving review from Core or Omarchy Mac yet (bot) label Dec 2, 2026
@DjDeveloperr

Copy link
Copy Markdown
Contributor

J700 row at exact head 255b646d48d4: the sleep path passes, and one kit gate fails on a main-kernel baseline line (not caused by this PR)

Setup

  • Board: J700 MacBook Neo (T8140, non-gen4).
  • Kernel: the exact PR head 255b646d48d4 (tree de9dc23d4c56, one commit on bf2931e5, sleep.c only), stock aurora_defconfig, built as 7.3.0-pr3950-255b646d. sleep.o was confirmed to be the PR's: the new PS_BUSY mask appears 4 times against 0 in base, and sleep_ps_lower grew from 0x12c to 0x1bc.
  • Boot: one boot through an aura-try one-shot trial Stage 2 (1.0.0b18-g16648d69), so aura's hardware trial watchdog was armed before the kernel started. The trial entry had the stock options plus exactly panic=10, and the kernel kept the watchdog fed.
  • Debug: dynamic debug was on for drivers/soc/apple/sleep.c at runtime on the trial boot only, to capture the APCIe lowering.

Results

Check Result
Two deep sleeps (rtcwake -d rtc0 -m mem -s 30 ×2) PASS. suspend_stats success 0 → 1 → 2 with fail unchanged, PM suspend entry (deep) and exit each time, boot object resumes counting up, same boot ID.
After each wake PASS. The three PCIe functions (root port, mt7932 Wi-Fi, mt7932 BT) were present on the same drivers at the same width. Wi-Fi reached its gateway and hci0 came back. The link retrained 5.0 → 2.5 GT/s, the same as the stock -41 deep sleep on this J700.
Faults PASS. 0 SError, unhandled faults, DART faults, pcie-apple or apple-dart errors, co-processor crashes, WARN, BUG or Oops, and 0 failed units after the wakes.
APCIe lowering, from sleep.c's existing failure lines (the PR adds none) PASS. Per cycle, 23 power words were written back and 0 were "not on in time". There was no BUSY-latch-at-commit, deep-path, ANS-not-off, PHY-clock or radio-port-stop line, and no refusal.
PMP events (kit gate e) FAIL by the kit's definition, see below.

About gate (e). The kit treats any PMP line after a wake as new if it isn't in stock -41's deep-sleep set. Each wake here logged one such line: apple_pmp 300500000.pmp: RTKit: sleep return: ASC control 0x0, setting RUN. It comes right before the usual RTKit: Initializing (protocol version 12). This line is not from this PR:

  • A main-based J700 kernel without this PR (the M3 lead's shared-control trial kernel, run three times today) logs the identical line on every wake, for apple_pmp and also for apple-sio and apple-dcp.
  • It comes from main's RTKit resume path. Stock -41 only re-initialises.
  • There were no PMP error lines.

So the gate fails on a baseline the kit took from the older stock kernel, not on a PMP event caused by #3950.

Stock restored: the return boot ran the configured Stage 2 and stock 7.3.0-41. boot-ctxr.bin is byte-identical to before, no marker is left, the stock and snapshot diffs are empty, and the PMU boot-error count is 00.

@DjDeveloperr

Copy link
Copy Markdown
Contributor

The exact-head T8132 deep-sleep row (2x rtcwake -m mem -s 20 at 255b646d48d4) has to come from your J604. Our J773g (T8132, OS FW 27.2) can't commit mem: apple-dcp 48ae00000.dcp: no firmware stop for a deep sleep: refusing it (rtcwake returns -EBUSY), and boot logs apple-sleep 380700000.sleep-controller: no PCIe tunables for ANS: suspend-to-RAM with it stopped will be refused. That holds on both main 048505b2 and #3717. Only s2idle commits there. The second non-gen4 control is covered by the J413 and J700 rows above. Once the J604 row is posted, this is ready to merge.

@IronsideXXVI

Copy link
Copy Markdown
Contributor Author

Thanks @DjDeveloperr and @satchlj for the review and for running the exact-head J700 (T8140) and J413 (T8112) controls. I also updated the Risk line in the PR description to reflect that sleep_ps_lower(), sleep_ps_wait_on(), and PS_BUSY masking apply across all deep-sleep SoCs (including gen4 APCIe, USB, ATC0, and PMP), while the apple_sleep_lower_apcie() teardown ordering applies to non-gen4 APCIe.

Update from J604 (Mac16,1, M4 MacBook Pro 14", T8132, OS FW 27.2 / 26B123): our 10-cycle repeated deep-sleep + s2idle soak just finished cleanly:

  • Deep sleep (10x 600 s rtcwake -m mem): 10/10 wakes PASS (macsmc-rtc RTC-only on time 10/10), 150.0 mW median asleep (144.0–174.0 mW, whole-window 155.1 mW), 1.65 s (1.6–1.7 s) awake transition, 1.35 s (1.1–3.3 s) Wi-Fi recovery, 10/10 all 6 AOP IIO sensors + Apple MTP keyboard intact, 0 SErrors, 0 L2C/kernel/GPU faults, 0 PMP errors, 0 resets, post-run ESP integrity check PASS.
  • s2idle (10x 300 s): 10/10 wakes PASS, 799.5 mW median asleep (793.0–809.0 mW, whole-window 802.9 mW), 1.1 s awake, 1.3 s Wi-Fi recovery, 0 faults.

To be completely transparent about head isolation:

  • That 10/10 soak ran on our integrated J604 kernel (linux-aurora-next 1.1686, head 1e959da5e66c, carrying commit 89d82de1c143), not yet a standalone build at exact main head 255b646d48d4.
  • The drivers/soc/apple/sleep.c diff in 89d82de1c143 is functionally identical line-for-line to 255b646d48d4 (the only textual differences are base-tree context: s->ps + offset vs main's sleep_power_reg(s, offset) helper and main's pre-existing apcie_gen4 early return); 89d82de1c143 also carried a pcie-apple.c relink/apple_pcie_map_bus() guard and the wifi: brcmfmac: pcie: guard D3 suspend IRQ and verify device presence before resume BAR0 read #3951 brcmfmac/pcie.c D3 guard alongside sleep.c.
  • If merge still strictly requires exact-head isolation under merge policy §1/§3, I can run a quick standalone 2x rtcwake -m mem -s 20 verification row on J604 built directly at 255b646d48d4 and post it here.

@DjDeveloperr

Copy link
Copy Markdown
Contributor

PASS J714s (M5 Pro, T6050) linux#3950 head 255b646 in composite2 099704f2 (7.3.0-m5-comp2, vmlinux 1e556b87): 2x rtcwake -d /dev/rtc1 -m mem -s 20 clean deep sleep resumes (20.46s/21.12s, boot_id unchanged, suspend_success 0->2, suspend_fail 0, 14 coprocessors stopped, AUSS 0x3ff->0x300, 0 prepare/APCIe/PCIe/DART/GPU/Oops/WARN/BUG faults), PCIe+NVMe+Wi-Fi+BT+headless Vulkan/GL smoke all pass post-resume, WDT armed, clean stock handback (a70fc871).

@DjDeveloperr

Copy link
Copy Markdown
Contributor

Merging at 255b646d48d4. The T8132 requirement is met by equivalence; no exact-head T8132 row is needed.

Same diff as the J604 soak kernel. 89d82de1c143 isn't pushed, so I compared against the published series (omarchy-j604 at 8b117645f31f, patch 1039). Every added and removed sleep.c line matches after replacing sleep_power_reg(s, offset) with s->ps + offset, which is what that helper returns on T8132. I rebuilt the soak kernel's sleep.c from da8e1acf3656 plus the series: the PS and PHY/clkgen helpers, t8132_lower_io() and T8132's .ps table are identical to this head. apple_sleep_lower_apcie() differs only by the apcie_gen4 early return. One helper it calls differs because of the base, not this diff: sleep_apcie_port_stop_once(). Not checked: the hash of the rebuilt sleep.c does not match the patch's index line (the series notes nine differing comment lines; the two PCIe files do match).

Could the extra guards hide a fault of this diff?

  • They run on device suspend/resume, not in the lowering, so they can't hide a hang, refusal or BUSY latch there.
  • The apple_pcie_map_bus() and brcmfmac absent-device guards only act on a dead or missing endpoint, and Wi-Fi came back 10/10.
  • The relink retry can turn a failed first train into a quiet pass (one dev_info line). It costs at least one 500 ms link_up_timeout. The 1.6-1.7 s awake time over the ten cycles, against 1.6 s on your 1.926 kernel without the retry (600.5 s asleep of 602.1 s), leaves no room for one. That is an inference from the timing: if your soak log has any relink down line, please post it.

Why a standalone J604 run wouldn't add to this. At this head J604 doesn't enable the apple,t8132-sleep node, so rtcwake -m mem would not reach sleep.c. This base also predates #3816 (merged 23:54Z), so T8132 doesn't arm AUTO_ENABLE on apcie_sys_st/apcie_phy_sw; current main does, as the soak kernel did.

What the rows cover. The rewritten non-gen4 body runs only on T8140 and T8132. J700 (T8140) ran it at this exact head, 2/2, without these guards. J413 and J714s cover the shared sleep_ps_lower(). T8103 and T8122 use that helper too and have no row here; each wait is bounded at 5 ms.

Merge with main (2b09f588; sleep.c unchanged through 5da70f68): clean, and the applied diff is unchanged. What landed there since bf2931e5 doesn't touch these helpers. On the merge tree: make ARCH=arm64 LLVM=1 W=1 drivers/soc/apple/sleep.o 0 warnings (clang 23.1.3), gcc 16.2.0 W=1 0 warnings, CONFIG_APPLE_SOC_SLEEP_KUNIT_TEST=y object builds (not run here), and the five host tests that read sleep.c pass.

@DjDeveloperr
DjDeveloperr merged commit 14b62ad into aurora-silicon:main Dec 3, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:platform Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) changes-requested A reviewer requested changes (bot) door:two-way Revertible or not, from Merge risk (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants