Skip to content

PCI: apple: disable L1SS around deep sleep and enable CLKREQ on T8103 - #3221

Closed
erikwb wants to merge 7 commits into
mainfrom
feat/t8103-pcie-aspm-l1ss
Closed

erikwb wants to merge 7 commits into
mainfrom
feat/t8103-pcie-aspm-l1ss

Conversation

@erikwb

@erikwb erikwb commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What: Disable PCIe L1.1/L1.2 substates per link around deep sleep on reinit hosts (apple_pcie_s2r_reinit()) via ASPM-core-serialized sleep hooks (pcie_aspm_l1ss_sleep_begin_locked() / pcie_aspm_l1ss_sleep_end()), retry deferred PCI-PM L1.1/L1.2 enables once both ends reach PCI_D0 (including via pci_pm_runtime_resume(), without taking pci_bus_sem again, and for unmanaged / CONFIG_PCIEASPM=n links), and enable supports-clkreq on J313 (T8103) port00, saving 167 mW Screen-ON (and 292–330 mW with Wi-Fi power save at DPMS-OFF) while keeping deep S2R clean.
Tested: Live hardware and local checks at head 83e82f78ee02 (stacked on 9d86c6538395, which moves supports-clkreq; from t8103.dtsi to t8103-j313.dts; default kernel command line):

  • J313 (M1 MacBook Air, T8103 — s2r_reinit=true, primary target): Root port 00:00.0 + 01:00.0 (Wi-Fi) + 01:00.1 (BT) have L1.1/L1.2 enabled in L1SubCtl1 (ASPM and PCI-PM), T_PwrOn=6us, ClockPM+ on both endpoints, and all 6 sysfs link attributes (clkpm, l1_aspm, l1_1_aspm, l1_2_aspm, l1_1_pcipm, l1_2_pcipm) = 1. Passed 5× s2idle + 5× deep S2R (including a 4-sleep chain deep -> s2idle -> deep -> deep): lspci -vvv L1SubCtl1/2, LnkCtl, and ClockPM restored identically to boot after every wake, link stayed 5GT/s x1, AER clean, 0 Oops/BUG/WARN/SError.
  • J713 (M4 MacBook Air, T8132 — s2r_reinit=false, unaffected-Mac check for shared PCI core change): Clean boot on default command line (ASPM L1 + ClockPM+ + L1.1/L1.2 active; CalDigit TS5 authorized; 0 Oops/BUG/WARN/SError). Runtime-PM churn across 5× power/control toggle cycles on 3 idle dock bridges (0001:02:01.0, 0001:02:02.0, 0001:02:03.0) recorded 15 pci_pm_runtime_resume, 15 pci_pm_runtime_suspend, and 30 pcie_aspm_pm_state_change_deferred kprobe hits with 0 dmesg errors. Passed 3× s2idle (rtcwake -d /dev/rtc1 -m freeze -s 5): 3/3 successes, Wi-Fi + Ethernet + CalDigit TS5 + eDP-1 intact, 0 new PCI/ASPM/AER/lockdep/Oops/BUG/WARN/SError lines.
  • J716c (M5 Max 16" MacBook Pro, T6050 — non-CLKREQ host check): Logged Clock PM off: the host does not handle CLKREQ#; lspci -vvv ASPM/ClockPM/L1SS state matched baseline before and after 2× pm_test=devices cycles (mem + freeze), Wi-Fi recovered cleanly, 0 PCIe/ASPM/AER/Oops/WARN lines.
  • Host tests, style, and arm64 builds at 83e82f78ee02: pciec-policy-tests.py 39/39 PASS (4 positive + 35 negative controls exercising extracted production C helpers from pcie-apple.c, aspm.c, pci.c, and pci-driver.c including pci_pm_runtime_resume() with pci_bus_sem held, link removal racing the ASPM notification, and a D0 transition racing the unmanaged restore), radio-{pm,sleep-fence,sleep-power,sleep-snapshot}-tests.py 4/4 scripts pass (5/5 PASS lines), binding-lifetime-tests.py 3/3 pass, checkpatch.pl --strict --git 5c89fbdf4ddc..83e82f78ee02 0 errors/0 warnings/0 checks (full PR range origin/main..83e82f78ee02: 0 errors, 2 warnings in first commit d5f09ab9bc6d, both superseded in-tree by 67a81e9c2593), ARCH=arm64 LLVM=1 full vmlinux + modules build through modpost with CONFIG_PCIE_APPLE=m for both CONFIG_PCIEASPM=y and CONFIG_PCIEASPM=n (pcie-apple.ko, 0 undefined symbols, 0 warnings), W=1 CHECK_DTBS=y build with CONFIG_PCIEASPM=y (drivers/pci/pcie/aspm.o, drivers/pci/pci-driver.o, drivers/pci/pci.o, drivers/pci/controller/pcie-apple.o, and apple/{t8103-j313,t8103-j274,t8103-j293,t8103-j456,t8103-j457,t6050-j716c}.dtb; no warnings from the supports-clkreq / port00 change, only pre-existing dtc and schema warnings in t8103.dtsi, the T8103 board files, and t6050-*.dtsi), and CONFIG_PCIEASPM=n W=1 build of aspm.o, pci-driver.o, pci.o, and pcie-apple.o (0 warnings). Earlier-head power A/B and docked candidate runs (5c89fbdf4ddc and pre-hardening J313 baseline) are preserved under Evidence.
    Risk: two-way door · all Macs (PCI core runtime-resume / D0 ASPM recheck); Apple s2r_reinit hosts for the sleep hooks
    Not yet: Live D3hot->D0 pci_pm_runtime_resume() on a J313 s2r_reinit endpoint (J313's only two endpoints are Wi-Fi/BT carrying SSH with runtime PM off; the pci_pm_runtime_resume() -> pcie_aspm_pm_state_change_deferred() path was exercised live on J713 dock bridges via kprobes and in host race tests), PROVE_LOCKING lockdep boot, unattended real sleep on J716c (exercised via pm_test=devices; unattended RTC wake is blocked because apple,t6050-pmu-rtc has no interrupts in t6050-j716c-sleep.dtsi / rtc-apple-pmu.c), and other T8103/T600x Macs not in this lab (supports-clkreq is scoped to t8103-j313.dts until ADT function-clkreq evidence or board verification is available for J274, J293, J456, and J457).
Details
  • Controlled A/B testing on J313 showed that enabling PCIe L1.1/L1.2 substates on port00 caused a WDT2 reset during deep S2R (wake sources 00000000 00400000) when endpoints entered D3hot and dropped into PCI-PM L1.2 (CLKREQ# deasserted) ahead of the sleep commit.
  • In apple_pcie_prepare(), for apple_pcie_s2r_reinit() hosts entering non-s2idle sleep, walk each downstream/root port link once and call pcie_aspm_l1ss_sleep_begin_locked(), which takes aspm_lock before looking up and validating pdev->link_state. Inside apple_pcie_hib_disable_l1ss_link() (under aspm_lock), snapshot PCI_L1SS_CTL1/CTL2 and PCI_EXP_LNKCTL across the root port and all downstream functions, clear ASPM across all downstream functions and the root port, clear PCI_L1SS_CTL1 downstream-before-upstream, and restore ASPM. On eligible core-managed links where L1SS starts disabled in hardware, keep link->aspm_sleep_disable armed across the sleep window so concurrent ASPM updates cannot enable L1SS before sleep. Check every config read/write and pass rollback = true to apple_pcie_hib_restore_l1ss() on prepare failure unwind (plus apple_radio_complete()) so apple_pcie_hib_merge_aspm() restores the saved ASPM bits if a one-shot ASPM restore write failed.
  • Why pci_disable_link_state() / pci_enable_link_state_locked() are not used directly (also documented in commit 83e82f78ee02): in drivers/pci/pcie/aspm.c, __pci_disable_link_state() sets bits in link->aspm_disable persistently (pcie_config_aspm_link() masks against ~link->aspm_disable), while __pci_enable_link_state() neither clears link->aspm_disable nor preserves link->aspm_default (it overwrites link->aspm_default = pci_calc_aspm_enable_mask(state)). Also pci_walk_bus() holds down_read(&pci_bus_sem), and unmanaged links (pcie_aspm=off, absent link_state, or CONFIG_PCIEASPM=n) still need checked hardware teardown and restore.
  • pcie_aspm_l1ss_sleep_begin_locked() and pcie_aspm_l1ss_sleep_end() (using link->aspm_sleep_disable in struct pcie_link_state) serialize L1SS teardown and restore with pci_bus_sem and aspm_lock, clear link->aspm_enabled L1SS bits and sync pci_save_aspm_l1ss_state() across .prepare() / .complete() so pci_restore_aspm_l1ss_state() at resume_noirq (and pcie_aspm_pm_state_change() during apple_pcie_resume()) keep L1SS suppressed until .complete().
  • At apple_pcie_complete(), apple_pcie_hib_restore_l1ss() restores PCI_L1SS_CTL2 and PCI_L1SS_CTL1 timing parameters first, then clears link->aspm_sleep_disable and calls pcie_config_aspm_link() so the PCI core enables policy-permitted L1SS while deferring PCI-PM L1.1/L1.2 whenever either end of the link is not in PCI_D0 (e.g. a runtime-suspended endpoint in D3hot) until both ends reach PCI_D0.
  • pci_set_full_power_state() calls pci_pm_notify_state_change() whenever dev->current_state == PCI_D0, invoking pcie_aspm_pm_state_change() on both dev->bus->self and dev, plus bridge->pm_state_change(), so deferred PCI-PM L1.1/L1.2 enables are retried once both ends reach PCI_D0. pci_pm_runtime_resume() and pci_restore_standard_config() call pci_pm_notify_resume() instead, because runtime resume can run inside pci_walk_bus() in PCIe error recovery with pci_bus_sem held for read and pcie_aspm_pm_state_change() would take it again (deadlock behind a queued writer): the ASPM update is queued as a work item that pins the pci_dev and revalidates pdev->link_state under aspm_lock (a link freed meanwhile is skipped, so no cancellation is needed on removal), while the host-bridge hook, which takes no PCI core lock, runs inline.
  • pcie_aspm_pm_state_change() reads pdev->link_state only under aspm_lock (acquired after pci_bus_sem), so removal of the downstream function cannot free the link before link->root is dereferenced.
  • For unmanaged links (!core_managed, including CONFIG_PCIEASPM=n and pcie_aspm=off), apple_pcie updates pci_save_aspm_l1ss_state() (exported with EXPORT_SYMBOL_GPL so CONFIG_PCIE_APPLE=m links) across the sleep window so pci_restore_state() never replays stale PCI-PM enables, masks PCI-PM L1.1/L1.2 while either end is not in PCI_D0, retains pending restore records in hib_l1ss_pending (merging deferred PCI-PM bits across consecutive sleeps) until apple_pcie_pm_state_change() applies them once both ends reach PCI_D0, and frees records on BUS_NOTIFY_DEL_DEVICE and apple_pcie_remove() / apple_pcie_cleanup(). The D0 decision, the masked write and the pending-record publish happen under one hib_l1ss_lock hold that the D0 notification also takes, so an endpoint reaching PCI_D0 in between is not missed. Lock order: pci_bus_sem, aspm_lock, hib_l1ss_lock.
  • Add supports-clkreq; to &port00 in arch/arm64/boot/dts/apple/t8103-j313.dts (commit 9d86c6538395 scoped it out of t8103.dtsi so J274/J293/J456/J457 remain untouched until verified).
  • On t6050 J716c (apple,t6050-pcie), tunneled, managed_sids, piodma_supplier, and root_bus_only are all unset/false, so apple_pcie_s2r_reinit() is true and deep sleep exercises the shared apple_pcie_prepare() path (whereas t8132 J713 uses apple,managed-sids, so apple_pcie_s2r_reinit() is false there).
  • Remove the L1SS stubs in tools/asahi/pciec-policy-tests.py, extract production drivers/pci/pcie/aspm.c, drivers/pci/pci.c (pci_pm_notify_state_change()), and drivers/pci/pci-driver.c (pci_pm_default_resume_early(), pci_pm_runtime_resume()) helpers alongside pcie-apple.c, and test normal completion ASPM preservation, one-shot ASPM restore write failures on both parent and downstream function, core-managed and unmanaged D3hot endpoint defer + real pci_pm_runtime_resume() D0 transition re-enable, multi-sleep pending retention, runtime resume with pci_bus_sem held, link removal racing the ASPM notification, a D0 transition racing the unmanaged restore, device-removal cleanup, initially-disabled core-managed link suppression, aspm_disable preservation, and negative controls.
Evidence

Live hardware at head 83e82f78ee02 (default command line)

  • J313 (M1 MacBook Air, T8103 — s2r_reinit=true, primary target):
    • Root port 00:00.0 + 01:00.0 (Wi-Fi) + 01:00.1 (BT): L1.1/L1.2 enabled in L1SubCtl1 (ASPM and PCI-PM), T_PwrOn=6us, ClockPM+ on both endpoints; all 6 sysfs link attributes (clkpm, l1_aspm, l1_1_aspm, l1_2_aspm, l1_1_pcipm, l1_2_pcipm) = 1.
    • 5× s2idle + 5× deep S2R (including 4-sleep chain deep -> s2idle -> deep -> deep): lspci -vvv L1SubCtl1/2, LnkCtl, and ClockPM restored identically to boot after every wake, link stayed 5GT/s x1, AER clean, 0 Oops/BUG/WARN/SError.
  • J713 (M4 MacBook Air, T8132 — s2r_reinit=false, unaffected-Mac check for shared PCI core change):
    • Clean boot on default command line: ASPM L1 + ClockPM+ + L1.1/L1.2 active, CalDigit TS5 authorized, 0 Oops/BUG/WARN/SError.
    • Runtime-PM churn: 5× power/control toggle cycles on 3 idle dock bridges (0001:02:01.0, 0001:02:02.0, 0001:02:03.0); kprobes recorded 15 pci_pm_runtime_resume, 15 pci_pm_runtime_suspend, and 30 pcie_aspm_pm_state_change_deferred hits with 0 dmesg errors.
    • 3× s2idle (rtcwake -d /dev/rtc1 -m freeze -s 5): 3/3 successes, Wi-Fi + Ethernet + CalDigit TS5 + eDP-1 intact, 0 new PCI/ASPM/AER/lockdep/Oops/BUG/WARN/SError lines.
  • J716c (M5 Max 16" MacBook Pro, T6050 — non-CLKREQ host check):
    • Logged Clock PM off: the host does not handle CLKREQ#; lspci -vvv ASPM/ClockPM/L1SS state matched baseline before and after 2× pm_test=devices cycles (mem + freeze), Wi-Fi recovered cleanly, 0 PCIe/ASPM/AER/Oops/WARN lines.

Build, style, and host tests at head 83e82f78ee02

  • ./scripts/checkpatch.pl --strict --git 5c89fbdf4ddc..83e82f78ee02: 0 errors, 0 warnings, 0 checks (17 + 1601 lines checked).
  • ./scripts/checkpatch.pl --strict --git origin/main..83e82f78ee02: 0 errors, 2 warnings in first commit d5f09ab9bc6d (kzalloc vs kzalloc_obj and mid-block int ret), both superseded in-tree by 67a81e9c2593; 0 errors, 0 warnings, 0 checks across all subsequent commits.
  • python3 tools/asahi/pciec-policy-tests.py: 39/39 pass with ASAN/UBSAN (4 positive + 35 negative controls).
  • python3 tools/asahi/radio-{pm,sleep-fence,sleep-power,sleep-snapshot}-tests.py: 4/4 scripts pass (5/5 PASS lines).
  • python3 tools/asahi/binding-lifetime-tests.py: 3/3 pass.
  • ARCH=arm64 LLVM=1 full vmlinux + modules build with CONFIG_PCIE_APPLE=m through modpost for CONFIG_PCIEASPM=y and for CONFIG_PCIEASPM=n: drivers/pci/controller/pcie-apple.ko, 0 undefined symbols, 0 warnings.
  • ARCH=arm64 LLVM=1 W=1 CHECK_DTBS=y build with CONFIG_PCIEASPM=y of drivers/pci/pcie/aspm.o, drivers/pci/pci-driver.o, drivers/pci/pci.o, drivers/pci/controller/pcie-apple.o, and apple/{t8103-j313,t8103-j274,t8103-j293,t8103-j456,t8103-j457,t6050-j716c}.dtb: no warnings from the supports-clkreq / port00 change, only pre-existing dtc and schema warnings in t8103.dtsi, the T8103 board files, and t6050-*.dtsi.
  • ARCH=arm64 LLVM=1 W=1 build with CONFIG_PCIEASPM=n of drivers/pci/pcie/aspm.o, drivers/pci/pci-driver.o, drivers/pci/pci.o, and drivers/pci/controller/pcie-apple.o: 0 warnings.

Earlier-head and baseline hardware evidence

  • Pre-hardening baseline hardware evidence on J313 (t8103, default command line):
    • Screen-ON (eDP-1 @ 118/420, kbd=0, wifi_ps=off): 4.130 W -> 3.963 W (-167 mW: -118 mW ClockPM + -49 mW L1SS).
    • DPMS-OFF with wlan0 power_save=on: -292 to -330 mW combined savings.
    • Controlled deep S2R A/B: default PASS, powersave (ClockPM only) PASS, powersupersave (L1.1/L1.2) FAIL (WDT2 reset), powersupersave + L1SS clear/restore PASS.
    • Post-S2R lspci -vv -s 0000:01:00.0: ClockPM+, L1SubCtl1: PCI-PM_L1.2+ PCI-PM_L1.1+ ASPM_L1.2+ ASPM_L1.1+.
  • Earlier-head (5c89fbdf4ddc, prior to 9d86c6538395 and 83e82f78ee02) hardware evidence on J313 (M1 Air) in fleet candidate 5509a254a6bd (main fcb124279183 plus unchanged heads 038ab73f52a6 (arm64: dts: apple: t8103: wire USB4 DP-IN AIC interrupts #3220), 5c89fbdf4ddc (PCI: apple: disable L1SS around deep sleep and enable CLKREQ on T8103 #3221), 2b1bab104938 (pmdomain: apple-pmc: resolve display interfaces via consumer-switched power-domains #3297), 4b09f0691c2d (drm/apple: toggle display pipe PMC interfaces across S2R domain transitions #3300), d3cf4ca61518 (arm64: dts: apple: t6050: allow idle ps_isp_sys domain to power off #3329), b8530d17c96b (soc: apple: pmgr-misc: tiered active fabric/DCS scaling and duty-cycle GPU tracking #3332), 262e263dae4c (arm64: dts: apple: mark T8103 and T600x CPU OPP tables opp-shared #3333) and 77114190bfb4 (soc: apple: sep: report a refused identity unlock, and stop aurora-sep-load on it #3274), merged without conflicts); default command line, J313 device tree built from the candidate on default Stage 2 (1.0.0b17-gf5ddfff2) as a one-shot aura-try, kernel installed next to the default and removed afterwards, Mac docked (Thunderbolt 5 dock with Ethernet and a 3840x2160@120 external display on dcpext). After boot and after every sleep:
    LnkCap: ... ClockPM+ ...                LnkCtl: ASPM L1 Enabled; ... ClockPM+
    L1SubCap:  PCI-PM_L1.2+ PCI-PM_L1.1+ ASPM_L1.2+ ASPM_L1.1+ L1_PM_Substates+
    L1SubCtl1: PCI-PM_L1.2+ PCI-PM_L1.1+ ASPM_L1.2+ ASPM_L1.1+
    
    Wi-Fi reassociated about 1 s after each sleep with 10/10 pings over wlan0 (0% loss) and Bluetooth discovered 62-104 devices in 10 s after each, over the fresh-boot run and its repeat (6 sleeps); in one full run (sleeps 4 and 5 of that boot) Wi-Fi did not reassociate within 30 s after two deep sleeps (iwd connect-failed, status 16 on every BSS; Ethernet and Bluetooth were fine) and did not reproduce. Sleeps: 2 deep sleeps (rtcwake -d rtc1 -m mem -s 30, outputs DPMS-off first) and an s2idle resumed on a fresh boot in 3 separate runs and in a repeat on one of those boots. One full run of the sequence hung in s2idle instead (PM: suspend entry (s2idle), no resume, hardware watchdog reset the Mac about 80 s later and it came back on the default boot); later runs did not reproduce it, and kernels built from arm64: dts: apple: t6050: allow idle ps_isp_sys domain to power off #3329 alone, soc: apple: pmgr-misc: tiered active fabric/DCS scaling and duty-cycle GPU tracking #3332 alone, and the other six PRs alone each passed the same docked sequence, so it is not attributed to a PR. With the default Stage 2's device tree the kernel instead refused s2idle (-EBUSY: a DP tunnel to the external display cannot be kept).
    System: 0 failed units after every sequence, 0 AppArmor denials (SELinux N/A on Arch ARM), 0 Oops/BUG/WARNING/SError. The kernel err lines are the 5 that a control boot prints with this dock (apple-dcp ... failed to extract init props x2, DCPDP2XService admission failed, and of_irq_parse_pci for the dock's bridge and Ethernet), the deep-sleep resume lines (brcmfmac/hci_bcm4377 Unable to change power state from D3hot to D0, PS fpwm1: Failed to reach power state, more DCPDP2XService admission failed), and at one boot brcmfmac ... ring 4: r_ptr 0 outside depth 0, which kernels without PCI: apple: disable L1SS around deep sleep and enable CLKREQ on T8103 #3221 also print after deep sleeps.
  • Earlier-head (5c89fbdf4ddc, prior to 83e82f78ee02) hardware evidence on J716c (t6050) in candidate 3e4078f656d5 (main fcb124279183 plus unchanged 5c89fbdf4ddc and sibling PRs, default command line): booted cleanly to Hyprland (wlan0 associated, 5 GHz, 0% gateway loss, hci0 powered, 0 new failed units, 0 LSM denials); after manual wake from a 4h52m s2idle, wlan0 reassociated with 0% loss and Bluetooth scanned cleanly; rtcwake -m no -s 5 confirmed apple,t6050-pmu-rtc alarm IRQ never fires (alrm_pending: no), leaving J716c deep S2R wake under **Not yet:**.

On conventional Apple PCIe hosts whose links are reinitialized after
deep sleep (apple_pcie_s2r_reinit()), leaving PCI-PM/ASPM L1.1/L1.2
substates enabled while endpoints enter D3hot lets the link drop into
L1.2 (CLKREQ# deasserted) ahead of the sleep commit, causing a WDT2
reset during deep sleep on T8103.

Clear L1SS per physical link in apple_pcie_prepare() for non-s2idle
sleeps (after clearing ASPM across all downstream functions and the
root port in PCIe spec order, with checked config accesses and full
unwind on failure) and restore the saved L1SS configuration in
apple_pcie_complete() after wake. s2idle is left untouched.

Signed-off-by: Erik Bourget <[email protected]>
Enable supports-clkreq on port00 so PCIe Clock PM and L1.1/L1.2
substates activate by default on the BCM4378 Wi-Fi/BT link.

Measured on J313 (M1 MacBook Air, T8103): saves 167 mW Screen-ON
(118 mW Clock PM + 49 mW L1SS) and 292-330 mW combined with Wi-Fi
power save at DPMS-OFF, while deep sleep resumes cleanly with L1SS
restored.

Signed-off-by: Erik Bourget <[email protected]>
@erikwb
erikwb requested review from a team as code owners November 26, 2026 09:40
@erikwb
erikwb requested review from Acelogic and malik-na November 26, 2026 09:40
@github-actions github-actions Bot added area:platform Area from CODEOWNERS (bot) blast:m1 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 26, 2026
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Merge readiness at 83e82f78: not ready yet

Check Detail
CI at head running running: KUnit isp, KUnit jpeg, KUnit prores, KUnit scaler, KUnit macsmc, KUnit clpc
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 83e82f78
Conflicts ok none
PR text ok Risk: two-way · all Macs (PCI core runtime-resume / D0 ASPM recheck); Apple s2rreinit hosts for the sleep hooks

Areas: area:gpu, area:platform

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

Provide stubs for the PCIe L1SS teardown helpers referenced by
apple_pcie_prepare() so the s2idle host test fixture compiles cleanly.

Signed-off-by: Erik Bourget <[email protected]>
@erikwb
erikwb requested a review from DjDeveloperr as a code owner November 27, 2026 02:20
@github-actions github-actions Bot added the area:gpu Area from CODEOWNERS (bot) label Nov 27, 2026
Use kzalloc_obj(*saved) for the L1SS sleep state allocation and declare
ret once at the top of apple_pcie_prepare() instead of mixing a
mid-block declaration before apple_radio_prepare().

Signed-off-by: Erik Bourget <[email protected]>
Save each link's original PCI_EXP_LNKCTL (root port and all downstream
functions) in struct apple_pcie_hib_l1ss before clearing ASPM so a
one-shot failure on the ASPM restore write does not leave rollback
rereading the cleared Link Control register and dropping ASPM.

Merge the saved ASPM bits only when unwinding a failed prepare() via
apple_pcie_hib_restore_l1ss(pcie, true); normal completion in
apple_pcie_complete() restores L1SS while preserving whatever ASPM
configuration the PCI core selected during resume.

Remove the L1SS prepare/restore stubs in
tools/asahi/pciec-policy-tests.py, wire the production L1SS teardown
and restore helpers into the s2idle/sleep test fixture, and test normal
completion ASPM preservation as well as one-shot ASPM restore write
failures on both the parent port and a downstream function along with
negative controls.

Signed-off-by: Erik Bourget <[email protected]>

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

Verdict at 5c89fbd: finish, then merge. Nothing here crashes or loses data. Three items before it lands:

Blockers:

  1. supports-clkreq now sits in t8103.dtsi, so it turns CLKREQ on for port00 on every T8103 board (J274, J293, J313, J456/J457). Only the J313 has been booted. The J313 ADT backs it: pci-bridge0 has function-clkreq = GPIO(0x96) next to function-perst = GPIO(0x98), which is the existing reset-gpios pin 152. Please show the same function-clkreq on pci-bridge0 in one more T8103 board's ADT (a mini or MBP dump), or a regression row on one of them. Otherwise, move the property to the J313 .dts for now. Better still, have the loader set it from function-clkreq, so no per-board line is needed.
  2. apple-build failed on an infra error: apt-get update got a 403 from packages.microsoft.com, so the PR is not at fault. I re-ran that one job; it needs to come back green at this head.

Question (please answer on the PR, not a blocker by itself): the ASPM core already has pci_disable_link_state() / pci_enable_link_state_locked() for turning L1.1/L1.2 off for a window. The 330 hand-written lines of L1SS/LNKCTL writes bypass link->aspm_disable/aspm_enabled, so sysfs link/l1_2_aspm and the core's own restore (pci_restore_aspm_l1ss_state() at resume_noirq) can see a different state than the hardware between prepare and complete. If the core API failed here (locking from .prepare, the reinit re-probe), please say why in the commit message.

Checked:

  • The walk does nothing when neither end has L1SS enabled, so hosts that never enable L1SS (no supports-clkreq) keep today's path. It only runs for apple_pcie_s2r_reinit() hosts on a non-s2idle target.
  • Rollback order: clear downstream before upstream, ASPM off around the CTL1/CTL2 edits, L1.2 enable bits last. The original LNKCTL is kept across a partial-failure rollback (5c89fbd), and refcounts are balanced on every exit.
  • pciec-policy-tests.py additions cover the new helpers.
Regression row offer

The prepare/complete change runs on every reinit host (T600x, T602x, T8112 too). We can add a no-regression deep-sleep + s2idle row on our M1 Pro (T6000) or J473 (T8112) at this head if that helps. Say which.

Move supports-clkreq on port00 from shared t8103.dtsi into
t8103-j313.dts until other T8103 boards (J274, J293, J456, J457)
have matching ADT function-clkreq evidence or hardware verification.

Signed-off-by: Erik Bourget <[email protected]>
Serialize sleep L1SS teardown and restore with the PCI core ASPM lock
and defer PCI-PM L1.1/L1.2 enables until both ends of the link are in
PCI_D0.

Why pci_disable_link_state() / pci_enable_link_state_locked() are not
used directly for temporary sleep suppression: in
drivers/pci/pcie/aspm.c, __pci_disable_link_state() sets bits in
link->aspm_disable persistently, while __pci_enable_link_state() neither
clears link->aspm_disable nor preserves link->aspm_default (it overwrites
link->aspm_default = pci_calc_aspm_enable_mask(state), clobbering DT,
policy, and sysfs settings). In addition, pci_walk_bus() already holds
pci_bus_sem for read, and unmanaged links (pcie_aspm=off, absent
link_state, or CONFIG_PCIEASPM=n) still need checked hardware teardown
and restore.

Look up and validate pdev->link_state under aspm_lock in
pcie_aspm_l1ss_sleep_begin_locked(), and snapshot L1SS CTL1/CTL2 and
LNKCTL inside the same aspm_lock-protected callback. For eligible
core-managed links, keep L1SS sleep suppression armed across the sleep
window even when L1SS starts disabled in hardware. On sleep end,
pcie_aspm_l1ss_sleep_end() clears temporary sleep suppression, restores
timing parameters first, and lets pcie_config_aspm_link() enable
policy-permitted L1SS while deferring PCI-PM L1.1/L1.2 until both
parent and child are in PCI_D0.

Retry deferred enables once a device reaches PCI_D0. For
pci_set_full_power_state() this is pci_pm_notify_state_change(), which
updates ASPM and calls the new host bridge pm_state_change() hook. For
pci_pm_runtime_resume() and pci_restore_standard_config() it is
pci_pm_notify_resume(): runtime resume can be reached from pci_walk_bus()
in PCIe error recovery with pci_bus_sem held for read, and taking it
again there deadlocks behind a queued writer. The ASPM update is queued
as a work item that pins the pci_dev and revalidates pdev->link_state
under aspm_lock, so a link freed in the meantime is skipped and no
cancellation is needed on removal. The host bridge hook takes no PCI core
lock and runs inline.

In pcie_aspm_pm_state_change(), read pdev->link_state only under
aspm_lock (after pci_bus_sem, preserving the lock order), so removal of
the downstream function cannot free the link before link->root is
dereferenced.

For unmanaged links (including CONFIG_PCIEASPM=n), sync saved L1SS state
across the sleep window with pci_save_aspm_l1ss_state(), now exported for
modular pcie-apple. The restore decides whether to defer PCI-PM
L1.1/L1.2, writes the masked configuration and publishes the pending
record under one hib_l1ss_lock hold, which the D0 notification also
takes, so an endpoint reaching D0 in between is not missed. Pending
records are merged across consecutive sleeps and cleaned up on device
removal or host teardown. Lock order is pci_bus_sem, aspm_lock,
hib_l1ss_lock.

Extend tools/asahi/pciec-policy-tests.py with the production ASPM L1SS
and PCI runtime-resume helpers to test core-managed and unmanaged D3hot
defer, real pci_pm_runtime_resume() D0 re-enable with pci_bus_sem held,
link removal racing the notification, a D0 transition racing the
unmanaged restore, multi-sleep pending retention, device removal
cleanup, initially-disabled link suppression, policy/aspm_disable
preservation, and negative controls.

Signed-off-by: Erik Bourget <[email protected]>
@erikwb

erikwb commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

Updated the branch with 9d86c6538395 and 83e82f78ee02 to address all review items:

  • Resolved DT scoping: 9d86c6538395 moves supports-clkreq; from arch/arm64/boot/dts/apple/t8103.dtsi into arch/arm64/boot/dts/apple/t8103-j313.dts (&port00), leaving untested T8103 boards (J274, J293, J456, J457) untouched until ADT function-clkreq evidence or board verification is available.
  • ASPM core, runtime-resume, locking, and unmanaged D3hot handling (83e82f78ee02):
    • Why pci_disable_link_state() / pci_enable_link_state_locked() is not used directly (also documented in commit 83e82f78ee02): in drivers/pci/pcie/aspm.c, __pci_disable_link_state() sets bits in link->aspm_disable persistently (pcie_config_aspm_link() masks against ~link->aspm_disable), whereas __pci_enable_link_state() does not clear link->aspm_disable and overwrites link->aspm_default = pci_calc_aspm_enable_mask(state), clobbering prior DT/policy/sysfs configuration. Also, pci_walk_bus() already holds down_read(&pci_bus_sem), and unmanaged links (pcie_aspm=off, absent link_state, or CONFIG_PCIEASPM=n) still need checked hardware teardown and restore.
    • In pcie_aspm_l1ss_sleep_begin_locked(), aspm_lock is now acquired before reading and validating pdev->link_state. Inside apple_pcie_hib_disable_l1ss_link() under aspm_lock, CTL1/CTL2/LNKCTL are snapshotted atomically, and eligible core-managed links keep link->aspm_sleep_disable armed across the sleep window even when L1SS starts disabled in hardware.
    • Reaching PCI_D0 retries deferred PCI-PM L1.1/L1.2 enables. pci_set_full_power_state() calls pci_pm_notify_state_change() (ASPM update plus the new host-bridge pm_state_change() hook). pci_pm_runtime_resume() and pci_restore_standard_config() call pci_pm_notify_resume() instead: runtime resume is reachable from pci_walk_bus() in PCIe error recovery with pci_bus_sem held for read, so calling pcie_aspm_pm_state_change() there would take pci_bus_sem recursively and deadlock behind a queued writer. The ASPM update is now queued as a work item that pins the pci_dev and revalidates pdev->link_state under aspm_lock, so a link freed after queueing is skipped and no cancellation is needed on hot-unplug; the host-bridge hook takes no PCI core lock and runs inline.
    • pcie_aspm_pm_state_change() now reads pdev->link_state only under aspm_lock (taken after pci_bus_sem, the order used everywhere in aspm.c), so removal of the downstream function cannot free the link before link->root is dereferenced. The pre-lock read is only an unlocked hint.
    • pci_save_aspm_l1ss_state() is exported (EXPORT_SYMBOL_GPL) so CONFIG_PCIE_APPLE=m links; pcie-apple.ko was built through modpost with CONFIG_PCIEASPM=y and =n.
    • For unmanaged links (!core_managed, including CONFIG_PCIEASPM=n and pcie_aspm=off), saved L1SS state stays synced across the sleep window so pci_restore_state() cannot replay stale enables. The restore now decides whether to defer PCI-PM L1.1/L1.2, writes the masked configuration and publishes the hib_l1ss_pending record under one hib_l1ss_lock hold, which the D0 notification also takes; an endpoint reaching PCI_D0 between the mask and the publish can no longer be missed. Pending records are merged across consecutive sleeps and freed on BUS_NOTIFY_DEL_DEVICE and apple_pcie_remove() / apple_pcie_cleanup(). Lock order is pci_bus_sem, aspm_lock, hib_l1ss_lock; no path takes them the other way round.

Completed local checks at 83e82f78ee02:

  • python3 tools/asahi/pciec-policy-tests.py: 39/39 pass (4 positive + 35 negative controls with ASAN/UBSAN), now also covering runtime resume with pci_bus_sem held (no recursive down_read()), a link freed before aspm_lock is taken, and an endpoint reaching PCI_D0 during the unmanaged restore.
  • python3 tools/asahi/radio-{pm,sleep-fence,sleep-power,sleep-snapshot}-tests.py: 4/4 scripts pass (5/5 PASS lines).
  • python3 tools/asahi/binding-lifetime-tests.py: 3/3 pass.
  • ./scripts/checkpatch.pl --strict --git 5c89fbdf4ddc..83e82f78ee02: 0 errors, 0 warnings, 0 checks (origin/main..83e82f78ee02: 0 errors, 2 warnings in first commit d5f09ab9bc6d, both superseded in-tree by 67a81e9c2593).
  • ARCH=arm64 LLVM=1 CONFIG_PCIE_APPLE=m full vmlinux + modules build through modpost (Module.symvers), once with CONFIG_PCIEASPM=y and once with CONFIG_PCIEASPM=n: drivers/pci/controller/pcie-apple.ko built, 0 undefined symbols, 0 warnings.
  • ARCH=arm64 LLVM=1 W=1 CHECK_DTBS=y (CONFIG_PCIEASPM=y): drivers/pci/pcie/aspm.o, drivers/pci/pci-driver.o, drivers/pci/pci.o, drivers/pci/controller/pcie-apple.o, and apple/{t8103-j313,t8103-j274,t8103-j293,t8103-j456,t8103-j457,t6050-j716c}.dtb built; no warnings from the supports-clkreq / port00 change, only the dtc and schema warnings that already exist in t8103.dtsi, the T8103 board files and t6050-*.dtsi.
  • ARCH=arm64 LLVM=1 W=1 (CONFIG_PCIEASPM=n): drivers/pci/pcie/aspm.o, drivers/pci/pci-driver.o, drivers/pci/pci.o, and drivers/pci/controller/pcie-apple.o built with 0 warnings.

Live hardware at 83e82f78ee02 (default command line):

  • J313 (M1 MacBook Air, T8103 — s2r_reinit=true, primary target): Root port 00:00.0 + 01:00.0 (Wi-Fi) + 01:00.1 (BT) have L1.1/L1.2 enabled in L1SubCtl1 (ASPM and PCI-PM), T_PwrOn=6us, ClockPM+ on both endpoints, and all 6 sysfs link attributes (clkpm, l1_aspm, l1_1_aspm, l1_2_aspm, l1_1_pcipm, l1_2_pcipm) = 1. Passed 5× s2idle + 5× deep S2R (including 4-sleep chain deep -> s2idle -> deep -> deep): lspci -vvv L1SubCtl1/2, LnkCtl, and ClockPM restored identically to boot after every wake, link stayed 5GT/s x1, AER clean, 0 Oops/BUG/WARN/SError.
  • J713 (M4 MacBook Air, T8132 — s2r_reinit=false, unaffected-Mac check for shared PCI core change): Clean boot on default command line (ASPM L1 + ClockPM+ + L1.1/L1.2 active; CalDigit TS5 authorized; 0 Oops/BUG/WARN/SError). 5× power/control toggle cycles on 3 idle dock bridges (0001:02:01.0, 0001:02:02.0, 0001:02:03.0) recorded 15 pci_pm_runtime_resume, 15 pci_pm_runtime_suspend, and 30 pcie_aspm_pm_state_change_deferred kprobe hits with 0 dmesg errors. Passed 3× s2idle (rtcwake -d /dev/rtc1 -m freeze -s 5): 3/3 successes, Wi-Fi + Ethernet + CalDigit TS5 + eDP-1 intact, 0 new PCI/ASPM/AER/lockdep/Oops/BUG/WARN/SError lines.
  • J716c (M5 Max 16" MacBook Pro, T6050 — non-CLKREQ host check): Logged Clock PM off: the host does not handle CLKREQ#; lspci -vvv ASPM/ClockPM/L1SS state matched baseline before and after 2× pm_test=devices cycles (mem + freeze), Wi-Fi recovered cleanly, 0 PCIe/ASPM/AER/Oops/WARN lines.

CI (apple-build) runs after the push.

Regression row offer:
A no-regression deep + s2idle row on M1 Pro (t6000) or J473 (t8112) would be great once the updated head is pushed, since both exercise the apple_pcie_s2r_reinit() path on non-s2idle sleep.

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

Verdict at 83e82f7: split, then merge the driver part.

Thanks: the J313-only supports-clkreq (9d86c65) resolves item 1, and your explanation of why pci_disable_link_state() / pci_enable_link_state_locked() do not fit (persistent aspm_disable, aspm_default overwritten, pci_bus_sem already held by pci_walk_bus(), unmanaged links) answers my question. With that reason in the commit message, the driver-local save/restore from 5c89fbd is fine.

Blocker: 83e82f7 now changes PCI core for every Mac: aspm.c (+142), pci.c (+44), the runtime-resume path in pci-driver.c, and a new host-bridge pm_state_change() hook in include/linux/pci.h. That is a separate change with a much wider reach than "L1SS around deep sleep on T8103". It needs its own review by someone who knows the ASPM core, plus rows on several generations. Please split it:

  1. This PR: 5c89fbd's driver-local L1SS handling, the rationale above in its commit message, and 9d86c65 (J313 supports-clkreq). The J313 rows you already have cover it, so it can merge on CI.
  2. New PR: the core ASPM, D0 recheck and host-bridge hook work, with its own Tested/Risk. We can add M1 Pro (T6000) and J473 (T8112) no-regression rows there.

If the driver part really cannot be correct without the core changes, please say which failure 5c89fbd has (a log or a host-test case), and we will review them together.

@github-actions github-actions Bot added blast:all Macs affected, from Merge risk (bot) and removed blast:m1 Macs affected, from Merge risk (bot) blast:m5 Macs affected, from Merge risk (bot) ci-failing A check failed at the PR head (bot) labels Nov 28, 2026
@erikwb

erikwb commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

Split #3221 cleanly into two PRs per review feedback, without rewriting #3221's branch history:

  1. Driver-local L1SS sleep handling + J313 CLKREQ (feat/t8103-pcie-l1ss-sleep, head a90a2bfab82d -> main): keeps all changes inside drivers/pci/controller/pcie-apple.c (with zero PCI core changes), adds the rationale for not using pci_disable_link_state() / pci_enable_link_state_locked() in the commit message, includes host tests in tools/asahi/pciec-policy-tests.py, and enables supports-clkreq strictly on J313 port00 (arch/arm64/boot/dts/apple/t8103-j313.dts).
  2. Shared PCI/ASPM sleep suppression + deferred D0 PM recheck (feat/pci-aspm-l1ss-sleep-core, head b886f11afb33 -> feat/t8103-pcie-l1ss-sleep): carries the shared PCI core changes (drivers/pci/{pci,pci-driver,probe,pci.h,pcie/aspm}.c, include/linux/pci.h) and the Apple driver integration stacked on PR 1, with its own hardware testing across J313, J713, and J716c and its own risk scope.

Closing #3221 as superseded by the two split PRs (#3494 and #3495).

@erikwb erikwb closed this Nov 28, 2026
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) blast:all Macs affected, from Merge risk (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