Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
95 changes: 91 additions & 4 deletions drivers/pci/pcie/aspm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions include/linux/pci.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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; }
Expand Down
62 changes: 56 additions & 6 deletions tools/asahi/brcmf-suspend-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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; };
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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=&regs;
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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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')}
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tools/asahi/pciec-policy-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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);'),
Expand Down
2 changes: 1 addition & 1 deletion tools/asahi/run-host-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
Expand Down