Skip to content

PCI: apple: put a function back in D0 before restoring it after S2R - #3142

Merged
RyanTheTide merged 1 commit into
aurora-silicon:mainfrom
joske:pr/t6041-sd-d0-after-s2r
Nov 25, 2026
Merged

RyanTheTide merged 1 commit into
aurora-silicon:mainfrom
joske:pr/t6041-sd-d0-after-s2r

Conversation

@joske

@joske joske commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What: after a deep sleep, set the functions behind a power-lost PCIe port back to D0 before their saved state is restored, so the J614c's SD reader resumes instead of failing with msi -22 and then blocking the next suspend and the reboot.
Tested: J614c (M4 Max) with this commit, fba4ec5, on main 34bdf71 (pcie-apple.c is the same as on this branch's base): 5 deep sleeps over two boots (3 lid, 2 rtcwake), the SD reader back after every wake with no msi or command-timeout errors, the reboot after them 18 s. Without it, the reader died at the first wake, the next suspend was refused twice and the reboot hung until the watchdog.
Risk: two-way · M4 Max (T6041); only functions behind a port that lost its power in a firmware sleep (hib_cold)
Not yet: other T6041 boards (J616c, J575c), and Macs whose ports keep their power, where the new call isn't reached

On main, the SD card reader (GL9755, PCIe port 1) of the J614c doesn't survive a deep sleep. Its resume fails with enable PCI MSI failed, error=-22, and every command after that times out. That breaks more than the reader:

  • the next suspend fails after 20 s with Device or resource busy, because the reader's command timeouts keep a freezable workqueue busy (Freezing remaining freezable tasks failed ... wq_busy=1);
  • a reboot hangs in the kernel's last step, after systemd has finished, until the hardware watchdog resets the machine 2 minutes later.

After a real sleep apple_pcie_resume() trains the conventional port again, but the PCI core's noirq power-up of the functions behind it has already run, with the link still down, and left current_state at D3hot. The port reset brings the function up in D0, but nothing records that, and the reader's resume frees and allocates its MSI vector, which the MSI core refuses for a device that isn't in D0. The fix calls pci_set_power_state(fn->pdev, PCI_D0) for each such function before pci_load_saved_state() and the restore.

#2215 (T6040 deep sleep) reworks the same resume path (s2r_ports_lost, ports set up again in the noirq resume), which would also avoid this. This is the minimal fix for what is on main now; if #2215 lands first, this can be closed.

Evidence

Without the commit (main 34bdf71 plus unrelated local changes), at the first wake:

sdhci-pci 0000:02:00.0: Unable to change power state from D3hot to D0, device inaccessible
mmc0: enable PCI MSI failed, error=-22
sdhci-pci 0000:02:00.0: PM: dpm_run_callback(): pci_pm_resume returns -22
sdhci-pci 0000:02:00.0: PM: failed to resume async: error -22
mmc0: Timeout waiting for hardware cmd interrupt.

and at the next two lid closes:

Freezing remaining freezable tasks failed after 20.007 seconds (0 tasks refusing to freeze, wq_busy=1)
systemd-sleep: Failed to put system to sleep. System resumed again: Device or resource busy

With the commit: the Unable to change power state ... inaccessible line from the noirq power-up still appears, then no msi, resume or command-timeout error over 5 sleeps. checkpatch.pl --strict: 0 errors, 0 warnings, 0 checks.

After a real sleep the host trains a conventional port again in its own
resume, but the PCI core's noirq power-up of the functions behind it ran
earlier, with the link still down ("Unable to change power state from
D3hot to D0, device inaccessible"), and left current_state at D3hot. The
GL9755 SD reader's resume then frees and allocates its MSI vector, which
the MSI core refuses for a device that is not in D0: "enable PCI MSI
failed, error=-22", and from then on every command times out.

On the J614c the dead reader also blocks the next suspend (its command
timeouts keep a freezable workqueue busy, "Device or resource busy") and
hangs the kernel's final reboot step until the hardware watchdog resets
the machine.

The port reset brought the function up in D0: record that with
pci_set_power_state() before its saved state is loaded back.

Signed-off-by: Jos Dehaes <[email protected]>
@joske
joske requested review from a team as code owners November 25, 2026 09:04
@joske
joske requested review from LiamRay11 and malik-na November 25, 2026 09:04
@github-actions github-actions Bot added blast:m4 Macs affected, from Merge risk (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 Nov 25, 2026
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Merge readiness at fba4ec56: not ready yet

Check Detail
CI at head no failing: pr-text / pr-text
Review ok approved by satchlj
Requested changes ok none
Hardware row at head ok in the PR body at fba4ec56
Conflicts ok none
PR text ok Risk: two-way · M4 Max (T6041); only functions behind a port that lost its power in a firmware sleep (hibcold)

Areas: none matched in CODEOWNERS

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: MERGE

Checked: Code review and live CI verification of drivers/pci/controller/pcie-apple.c at fba4ec5680bf against main, including the hib_cold resume path ordering relative to pci_load_saved_state() and pci_restore_state().

Evidence
  • apple_pcie_resume() iterates functions behind ports that lost power during firmware sleep (port->hib_cold), restores the PCIe link, and now records pci_set_power_state(fn->pdev, PCI_D0) before pci_load_saved_state() / pci_restore_state() so PCI core state matches the post-reset D0 hardware state and subsequent driver MSI allocation no longer fails with -EINVAL.
  • Scoped strictly to port->hib_cold functions; ports that retain power across suspend skip the path unchanged.
  • Exact-head J614c (M4 Max) deep-sleep verification row is documented in the PR body at fba4ec5680bf (5 deep sleeps over two boots, SD reader recovers cleanly with no msi -22 or command timeouts, clean reboot).
  • All 20 CI checks completed green at fba4ec5680bf with 0 active and 0 failing checks.

@AxxzyWasTaken

Copy link
Copy Markdown
Contributor

Hardware verification row — Apple M4 Max 16" (T6041 / J616C) at exact HEAD fba4ec5680bf

  • Kernel: 7.3.0-j616c-pr3142+ built from exact HEAD fba4ec5680bf (LLVM 22.1.8, 1000-nit DTB).
  • Pre-sleep: modprobe sdhci-pci binds 0000:02:00.0 (Genesys Logic GL9755 [17a0:9755] rev 02), mmc0 registers (ADMA 64-bit), power_state=D0.
  • 2× consecutive EL2 deep S2R (mem_sleep=[deep], apple-pmu-rtc rtc1 wakealarm + echo mem > /sys/power/state):
    • Cycle 1: suspend_stats success=1, fail=0; pcie-apple re-trained both pci@0,0 and pci@1,0 (PWREN and PERST# cycled as at probe); 0 msi -22 errors, 0 sdhci command-timeout errors; 0000:02:00.0 back in D0 with sdhci-pci and mmc0 intact.
    • Cycle 2: suspend_stats success=2, fail=0; 0 msi -22 / sdhci errors; 0000:02:00.0 in D0, mmc0 intact.
  • Reboot: Clean systemctl reboot back to daily kernel in 44 s (no watchdog hang), 0 failed units.
  • Verdict: PASS (empty slot; no physical SD card inserted).

@github-actions github-actions Bot removed the needs-review No approving review from Core or Omarchy Mac yet (bot) label Nov 25, 2026
@RyanTheTide
RyanTheTide merged commit 3f69c88 into aurora-silicon:main Nov 25, 2026
22 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blast:m4 Macs affected, from Merge risk (bot) ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants