From 9b4f6882dd11816017c8665a2440dac8db88523a Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Wed, 2 Dec 2026 18:24:40 +0100 Subject: [PATCH] wifi: brcmfmac: pcie: suppress ASPM L1 across s2idle on BCM4378 On Apple M1 (T8103) systems running macOS 27.2 (26B123) BCM4378 firmware (18.20.511.0.7.8.207), allowing the BCM4378 PCIe link into ASPM L1 across s2idle causes a hard SoC reset during synchronous device suspend/resume (reproducible even under pm_test=devices and pm_async=0) while the radio transitions through MPC with the function kept in D0. Disabling ASPM L1 permanently would forfeit runtime L1/L1SS power savings while awake. Add pcie_aspm_sleep_disable() and pcie_aspm_sleep_restore() in the PCIe ASPM core to temporarily mask ASPM states via link->aspm_sleep_disable without altering link->aspm_disable, link->aspm_default, or sysfs overrides. Also extend the L1SS restore/sync sleep-suppression checks so suppressing PCIE_LINK_STATE_L1 consistently suppresses L1SS as well, and update saved LNKCTL state across all downstream functions on the link. Use these helpers from brcmf_pcie_pm_prepare() and brcmf_pcie_pm_complete() for Apple-seeded BCM4378 s2idle sleeps so ASPM L1 (and L1SS) are cleared before wiphy_suspend() / brcmf_pcie_s2idle_sleep() and restored cleanly after brcmf_pcie_pm_leave_D3() / wiphy_resume(), keeping runtime ASPM L1/L1SS/CLKPM savings intact while awake and leaving non-BCM4378 chips and deeper sleeps unchanged. Extend brcmf-suspend-tests with prepare/complete coverage and two mutations (s2idle-l1-prepare, s2idle-l1-complete). Signed-off-by: Thomas Glanzmann --- .../broadcom/brcm80211/brcmfmac/pcie.c | 54 +++++++++++ drivers/pci/pcie/aspm.c | 95 ++++++++++++++++++- include/linux/pci.h | 5 + tools/asahi/brcmf-suspend-tests.py | 62 ++++++++++-- tools/asahi/pciec-policy-tests.py | 2 +- tools/asahi/run-host-tests.py | 2 +- 6 files changed, 208 insertions(+), 12 deletions(-) diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c index 716aa8e69d670..04d998744d6a5 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c @@ -447,6 +447,7 @@ struct brcmf_pciedev_info { bool irq_wake_enabled; bool wowl_sleeping; bool s2idle_d0; + bool s2idle_l1_suppressed; bool wakeup_created; bool wakeup_capable; u8 dma_idx_sz; @@ -3443,6 +3444,11 @@ brcmf_pcie_remove(struct pci_dev *pdev) brcmf_pcie_release_irq(devinfo); + if (devinfo->s2idle_l1_suppressed) { + devinfo->s2idle_l1_suppressed = false; + pcie_aspm_sleep_restore(pdev, PCIE_LINK_STATE_L1); + } + dev_set_drvdata(&pdev->dev, NULL); kfree(bus->bus_priv.pcie); kfree(bus->msgbuf->flowrings); @@ -3743,7 +3749,55 @@ static int brcmf_pcie_pm_leave_D3(struct device *dev) } +/* + * On BCM4378 (Apple M1 / T8103), allowing the PCIe link into ASPM L1 across + * s2idle causes a hard SoC reset while the radio transitions through MPC with + * the function kept in D0. Suppress ASPM L1 (and its L1SS substates) from + * prepare() through complete() so runtime ASPM L1/L1SS savings remain active + * while awake. + */ +static int brcmf_pcie_pm_prepare(struct device *dev) +{ + struct brcmf_bus *bus = dev_get_drvdata(dev); + struct brcmf_pciedev_info *devinfo; + int ret; + + if (!bus) + return 0; + + devinfo = bus->bus_priv.pcie->devinfo; + if (!devinfo->fwseed || + devinfo->pdev->device != BRCM_PCIE_4378_DEVICE_ID || + pm_suspend_target_state != PM_SUSPEND_TO_IDLE) + return 0; + + ret = pcie_aspm_sleep_disable(devinfo->pdev, PCIE_LINK_STATE_L1); + if (ret) + return ret; + + devinfo->s2idle_l1_suppressed = true; + return 0; +} + +static void brcmf_pcie_pm_complete(struct device *dev) +{ + struct brcmf_bus *bus = dev_get_drvdata(dev); + struct brcmf_pciedev_info *devinfo; + + if (!bus) + return; + + devinfo = bus->bus_priv.pcie->devinfo; + if (!devinfo->s2idle_l1_suppressed) + return; + + devinfo->s2idle_l1_suppressed = false; + pcie_aspm_sleep_restore(devinfo->pdev, PCIE_LINK_STATE_L1); +} + static const struct dev_pm_ops brcmf_pciedrvr_pm = { + .prepare = brcmf_pcie_pm_prepare, + .complete = brcmf_pcie_pm_complete, .suspend = brcmf_pcie_pm_enter_D3, .resume = brcmf_pcie_pm_leave_D3, .freeze = brcmf_pcie_pm_enter_D3, diff --git a/drivers/pci/pcie/aspm.c b/drivers/pci/pcie/aspm.c index 8e8eaa1370c87..5b1ff7c3b4e52 100644 --- a/drivers/pci/pcie/aspm.c +++ b/drivers/pci/pcie/aspm.c @@ -1222,7 +1222,8 @@ static void pcie_aspm_restore_l1ss_mask(struct pci_dev *parent, if (aspm_disabled || !link || link->downstream != pdev) return; - if (READ_ONCE(link->aspm_sleep_disable) & PCIE_LINK_STATE_L1SS) { + if (READ_ONCE(link->aspm_sleep_disable) & + (PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_L1SS)) { *pl_ctl1 &= ~PCI_L1SS_CTL1_L1SS_MASK; *cl_ctl1 &= ~PCI_L1SS_CTL1_L1SS_MASK; return; @@ -1258,7 +1259,7 @@ static void pcie_aspm_restore_l1ss_sync(struct pci_dev *parent, if (pl_en != expected || cl_en != expected) { WRITE_ONCE(parent->aspm_l1ss_resync, 1); if (!(READ_ONCE(link->aspm_sleep_disable) & - PCIE_LINK_STATE_L1SS)) { + (PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_L1SS))) { WRITE_ONCE(parent->aspm_pcipm_deferred, 1); /* * Pairs with smp_mb() in @@ -1398,7 +1399,7 @@ static int pcie_config_aspm_link_locked(struct pcie_link_state *link, u32 state) WRITE_ONCE(link->aspm_hw_l1ss, 0); WRITE_ONCE(parent->aspm_l1ss_resync, 1); if (!(READ_ONCE(link->aspm_sleep_disable) & - PCIE_LINK_STATE_L1SS)) + (PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_L1SS))) WRITE_ONCE(parent->aspm_pcipm_deferred, 1); return ret; } @@ -1408,7 +1409,8 @@ static int pcie_config_aspm_link_locked(struct pcie_link_state *link, u32 state) /* Update latest ASPM configuration in saved context */ pci_save_aspm_l1ss_state_locked(link->downstream); - pci_update_aspm_saved_state(link->downstream); + list_for_each_entry(child, &linkbus->devices, bus_list) + pci_update_aspm_saved_state(child); pci_save_aspm_l1ss_state_locked(parent); pci_update_aspm_saved_state(parent); return 0; @@ -2242,6 +2244,91 @@ bool pcie_aspm_support_enabled(void) return aspm_support_enabled; } +/** + * pcie_aspm_sleep_disable - Temporarily disable ASPM states across system sleep + * @pdev: PCI device whose upstream link should avoid @state during sleep + * @state: ASPM link states to suppress (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1) + * + * Temporarily masks @state out of the link's active ASPM configuration via + * link->aspm_sleep_disable without altering link->aspm_disable, + * link->aspm_default, or sysfs overrides. Suppressing PCIE_LINK_STATE_L1 also + * disables L1 PM Substates per PCIe r6.2 sec 5.5.4 while preserving + * L1SS-only suppression owned by pcie_aspm_l1ss_sleep_begin_locked(). + * + * Return: 0 on success (including when ASPM is disabled or unavailable), + * or a negative errno on hardware error. + */ +int pcie_aspm_sleep_disable(struct pci_dev *pdev, int state) +{ + struct pcie_link_state *link; + unsigned long flags; + u8 mask, added; + int ret = 0; + + mask = state & (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1); + if (!mask) + return 0; + + down_read(&pci_bus_sem); + mutex_lock(&aspm_lock); + link = pcie_aspm_get_link(pdev); + if (aspm_disabled || !link || !link->downstream) + goto out_unlock; + + spin_lock_irqsave(&aspm_l1ss_hw_lock, flags); + added = mask & ~READ_ONCE(link->aspm_sleep_disable); + WRITE_ONCE(link->aspm_sleep_disable, + READ_ONCE(link->aspm_sleep_disable) | mask); + ret = pcie_config_aspm_link_locked(link, policy_to_aspm_state(link)); + if (ret && added) + WRITE_ONCE(link->aspm_sleep_disable, + READ_ONCE(link->aspm_sleep_disable) & ~added); + spin_unlock_irqrestore(&aspm_l1ss_hw_lock, flags); + +out_unlock: + mutex_unlock(&aspm_lock); + up_read(&pci_bus_sem); + return ret; +} +EXPORT_SYMBOL_GPL(pcie_aspm_sleep_disable); + +/** + * pcie_aspm_sleep_restore - Restore ASPM states suppressed across system sleep + * @pdev: PCI device whose upstream link was suppressed + * @state: ASPM link states previously passed to pcie_aspm_sleep_disable() + * + * Clears @state from link->aspm_sleep_disable and re-applies the link's + * configured ASPM policy (including L1SS when permitted and both link ends + * are in PCI_D0). + */ +void pcie_aspm_sleep_restore(struct pci_dev *pdev, int state) +{ + struct pcie_link_state *link; + unsigned long flags; + u8 mask; + + mask = state & (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1); + if (!mask) + return; + + down_read(&pci_bus_sem); + mutex_lock(&aspm_lock); + link = pcie_aspm_get_link(pdev); + if (aspm_disabled || !link || !link->downstream) + goto out_unlock; + + spin_lock_irqsave(&aspm_l1ss_hw_lock, flags); + WRITE_ONCE(link->aspm_sleep_disable, + READ_ONCE(link->aspm_sleep_disable) & ~mask); + pcie_config_aspm_link_locked(link, policy_to_aspm_state(link)); + spin_unlock_irqrestore(&aspm_l1ss_hw_lock, flags); + +out_unlock: + mutex_unlock(&aspm_lock); + up_read(&pci_bus_sem); +} +EXPORT_SYMBOL_GPL(pcie_aspm_sleep_restore); + #endif /* CONFIG_PCIEASPM */ int pcie_aspm_l1ss_hw_exec(struct pci_dev *child, bool save_state, diff --git a/include/linux/pci.h b/include/linux/pci.h index 77778ce41ae36..319d2328295e1 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -1970,6 +1970,8 @@ int pci_disable_link_state(struct pci_dev *pdev, int state); int pci_disable_link_state_locked(struct pci_dev *pdev, int state); int pci_enable_link_state(struct pci_dev *pdev, int state); int pci_enable_link_state_locked(struct pci_dev *pdev, int state); +int pcie_aspm_sleep_disable(struct pci_dev *pdev, int state); +void pcie_aspm_sleep_restore(struct pci_dev *pdev, int state); void pcie_no_aspm(void); bool pcie_aspm_support_enabled(void); bool pcie_aspm_enabled(struct pci_dev *pdev); @@ -1982,6 +1984,9 @@ static inline int pci_enable_link_state(struct pci_dev *pdev, int state) { return 0; } static inline int pci_enable_link_state_locked(struct pci_dev *pdev, int state) { return 0; } +static inline int pcie_aspm_sleep_disable(struct pci_dev *pdev, int state) +{ return 0; } +static inline void pcie_aspm_sleep_restore(struct pci_dev *pdev, int state) { } static inline void pcie_no_aspm(void) { } static inline bool pcie_aspm_support_enabled(void) { return false; } static inline bool pcie_aspm_enabled(struct pci_dev *pdev) { return false; } diff --git a/tools/asahi/brcmf-suspend-tests.py b/tools/asahi/brcmf-suspend-tests.py index 6ffce5c538f8f..edcb4b24e654f 100644 --- a/tools/asahi/brcmf-suspend-tests.py +++ b/tools/asahi/brcmf-suspend-tests.py @@ -68,9 +68,9 @@ def function(source, name): struct brcmf_if { struct brcmf_cfg80211_vif *vif; struct brcmf_pub *drvr; struct net_device *ndev; unsigned netif_stop; int netif_stop_lock, bsscfgidx; }; struct device { void *data; }; -struct pci_dev { struct device dev; int irq; bool state_saved; }; +struct pci_dev { struct device dev; int irq; bool state_saved; unsigned short device; }; struct brcmf_pciedev_info { bool mbdata_completed, intr, ready, timer, sleep_reset_blocked, setup_complete; - bool wowl_irq, wowl_enabled, irq_wake_enabled, wowl_sleeping, fwseed, s2idle_d0; + bool wowl_irq, wowl_enabled, irq_wake_enabled, wowl_sleeping, fwseed, s2idle_d0, s2idle_l1_suppressed; bool irq_allocated, unmasked; int state, mbdata_resp_wait; void *ci; struct pci_dev *pdev; struct { int intmask, mailboxmask; } *reginfo; }; @@ -157,9 +157,22 @@ def function(source, name): static void brcmf_pcie_fwcon_timer(struct brcmf_pciedev_info *d,bool on) {d->timer=on;} #define PM_SUSPEND_TO_IDLE 1 #define PM_SUSPEND_ON 0 +#define BRCM_PCIE_4378_DEVICE_ID 0x4425 +#define PCIE_LINK_STATE_L1 BIT(1) static int pm_suspend_target_state = PM_SUSPEND_TO_IDLE; static bool device_may_wakeup(struct device *d) {(void)d;return true;} -static int wake_error, save_error; static unsigned d3_informs, d0_informs; +static int wake_error, save_error, aspm_disable_error; +static unsigned d3_informs, d0_informs, aspm_disables, aspm_restores; +static int aspm_suppressed_mask; +static int pcie_aspm_sleep_disable(struct pci_dev *p, int state) { + assert(p == &pdev); + if (aspm_disable_error) return aspm_disable_error; + aspm_disables++; aspm_suppressed_mask |= state; return 0; +} +static void pcie_aspm_sleep_restore(struct pci_dev *p, int state) { + assert(p == &pdev); + aspm_restores++; aspm_suppressed_mask &= ~state; +} static int enable_irq_wake(int irq) {(void)irq;return wake_error;} static int pci_save_state(struct pci_dev *p) {if(save_error)return save_error;p->state_saved=true;return 0;} static int irq_depth; static unsigned kicks; @@ -295,9 +308,10 @@ def function(source, name): cfg.pub=&pub;cfg.wiphy=&wiphy;wiphy.cfg=&cfg;vif_up=true;device.data=&bus;devinfo.pdev=&pdev; static typeof(*devinfo.reginfo) regs;devinfo.reginfo=®s; devinfo.state=BRCMFMAC_PCIE_STATE_UP;devinfo.intr=devinfo.ready=devinfo.timer=true; - devinfo.irq_allocated=devinfo.unmasked=true;irq_depth=0;kicks=0; + devinfo.irq_allocated=devinfo.unmasked=true;irq_depth=0;kicks=0;pdev.device=0; country_gets=sets=bands=txflows=creates=cancels=removes=probes=0; - failure=translation=d0_error=probe_error=wake_error=save_error=0;ack=true;retain=1;d3_informs=d0_informs=0; + failure=translation=d0_error=probe_error=wake_error=save_error=aspm_disable_error=0; + ack=true;retain=1;d3_informs=d0_informs=aspm_disables=aspm_restores=0;aspm_suppressed_mask=0; pm_suspend_target_state=PM_SUSPEND_TO_IDLE;feats=0; } static void hint(const char *a) { @@ -528,6 +542,35 @@ def function(source, name): assert(!brcmf_cfg80211_suspend(&wiphy,NULL) && cfg.suspended); assert(link_downs==2 && link_waits==1 && conn.link_down.done && !off.link_down.done);cases++; } + /* BCM4378 (Apple M1) suppresses ASPM L1 from prepare() through complete() + * on s2idle only, leaving runtime ASPM intact while awake and leaving other + * chips and non-s2idle sleeps untouched. */ + for(int fail=0;fail<2;fail++) { + reset();devinfo.fwseed=true;pdev.device=BRCM_PCIE_4378_DEVICE_ID; + aspm_disable_error=fail?-EIO:0; + int ret=brcmf_pcie_pm_prepare(&device); + if(fail) { + assert(ret==-EIO && !devinfo.s2idle_l1_suppressed && !aspm_suppressed_mask); + brcmf_pcie_pm_complete(&device); + assert(!aspm_restores);cases++;continue; + } + assert(!ret && devinfo.s2idle_l1_suppressed && aspm_disables==1); + assert(aspm_suppressed_mask==PCIE_LINK_STATE_L1); + brcmf_pcie_pm_complete(&device); + assert(!devinfo.s2idle_l1_suppressed && aspm_restores==1 && !aspm_suppressed_mask); + brcmf_pcie_pm_complete(&device); + assert(aspm_restores==1);cases++; + } + for(int mode=0;mode<3;mode++) { + reset(); + devinfo.fwseed=(mode!=0); + pdev.device=(mode==1)?0x4433:BRCM_PCIE_4378_DEVICE_ID; + pm_suspend_target_state=(mode==2)?PM_SUSPEND_ON:PM_SUSPEND_TO_IDLE; + assert(!brcmf_pcie_pm_prepare(&device)); + assert(!devinfo.s2idle_l1_suppressed && !aspm_disables && !aspm_suppressed_mask); + brcmf_pcie_pm_complete(&device); + assert(!aspm_restores);cases++; + } printf("%u regulatory, TX queue, worker retention and PCIe PM controls passed\n",cases); return 0; } @@ -541,7 +584,8 @@ def main(): 'resume-order', 'tx-drain', 'lpas-resume', 's2idle-d3', 'wowl-s2idle-only', 'restore-pm', 'resume-join', 'down-error-fence', - 'd0-quiesce', 'd0-restore-irq', 'd0-kick']) + 'd0-quiesce', 'd0-restore-irq', 'd0-kick', + 's2idle-l1-prepare', 's2idle-l1-complete']) args = parser.parse_args() sources = {name: (DRIVER / (name + '.c')).read_text() for name in ('cfg80211', 'core', 'pcie', 'msgbuf')} @@ -554,6 +598,7 @@ def main(): 'msgbuf': ['brcmf_msgbuf_flowring_worker', 'brcmf_msgbuf_txflow_worker', 'brcmf_msgbuf_suspend', 'brcmf_msgbuf_resume'], 'pcie': ['brcmf_pcie_wowl_wake', 'brcmf_pcie_wowl_sleep', 'brcmf_pcie_s2idle_sleep', + 'brcmf_pcie_pm_prepare', 'brcmf_pcie_pm_complete', 'brcmf_pcie_pm_enter_D3', 'brcmf_pcie_pm_leave_D3'], } body = ''.join(function(sources[group], name) @@ -592,6 +637,11 @@ def main(): body = body.replace('\t\tenable_irq(devinfo->pdev->irq);\n', '') elif args.mutation == 'd0-kick': body = body.replace('irq_wake_thread(devinfo->pdev->irq, devinfo);', ';') + elif args.mutation == 's2idle-l1-prepare': + body = body.replace('ret = pcie_aspm_sleep_disable(devinfo->pdev, PCIE_LINK_STATE_L1);', + 'ret = 0;') + elif args.mutation == 's2idle-l1-complete': + body = body.replace('pcie_aspm_sleep_restore(devinfo->pdev, PCIE_LINK_STATE_L1);', ';') elif args.mutation == 'tx-drain': body = body.replace('netif_tx_disable(ifp->ndev);', '(void)ifp;') code = PREFIX.replace('/* STOP_REASONS */', reasons) + CALLBACK_STUBS + body + MAIN diff --git a/tools/asahi/pciec-policy-tests.py b/tools/asahi/pciec-policy-tests.py index 2f273b7bb9041..d5adc30ef8d86 100644 --- a/tools/asahi/pciec-policy-tests.py +++ b/tools/asahi/pciec-policy-tests.py @@ -3044,7 +3044,7 @@ def main(): '\t\tif ((wanted & PCIE_LINK_STATE_L1_SS_PCIPM) & ~deferred_state) {'), 'drop-worker-err-rearm-deferred': ( '\t\tif (!(READ_ONCE(link->aspm_sleep_disable) &\n' - '\t\t PCIE_LINK_STATE_L1SS))\n' + '\t\t (PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_L1SS)))\n' '\t\t\tWRITE_ONCE(parent->aspm_pcipm_deferred, 1);', '\t\tif (false)\n' '\t\t\tWRITE_ONCE(parent->aspm_pcipm_deferred, 1);'), diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index 03004b00c58cb..eea2f379bff0e 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -254,7 +254,7 @@ def mutations(flag, names, base, expect=KILL): 'resume-order', 'tx-drain', 'lpas-resume', 'wowl-s2idle-only', 'restore-pm', 'resume-join', 'down-error-fence', 'd0-quiesce', 'd0-restore-irq', - 'd0-kick'), out()), + 'd0-kick', 's2idle-l1-prepare', 's2idle-l1-complete'), out()), 'brcmf-wowl-filter-tests.py': [(out(), PASS)] + mutations('--mutation', ('base-order', 'mask-last', 'totsize', 'no-track'), out()), 'brcmfmac-scan-timeout-tests.py': [(out('--output'), PASS)],