diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 3bc0a7eee2bd5..134c552f6899a 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -656,7 +656,14 @@ struct apple_pcie { struct list_head ports; struct list_head hib_fns; struct list_head hib_l1ss; + struct list_head hib_l1ss_pending; struct list_head hib_l1ss_rpm; + /* Protects hib_l1ss, hib_l1ss_pending, and hib_l1ss_rpm. */ + struct mutex hib_l1ss_lock; + struct work_struct hib_l1ss_work; + struct notifier_block hib_l1ss_pci_nb; + atomic_t hib_l1ss_pending_cnt; + bool hib_l1ss_closing; bool hib_saved; struct completion event; struct irq_fwspec fwspec; @@ -711,6 +718,13 @@ static void __noreturn apple_radio_terminal(void); static int apple_pcie_managed_admit(struct apple_pcie_port *port); static struct apple_pcie *apple_pcie_lookup(struct device *dev); static int apple_pcie_radio_check_cold(struct apple_pcie_port *port); +static bool apple_pcie_s2r_reinit(struct apple_pcie *pcie); +static void apple_pcie_pm_state_change(struct pci_host_bridge *bridge, + struct pci_dev *pdev); +static void apple_pcie_hib_l1ss_work_fn(struct work_struct *work); +static int apple_pcie_hib_l1ss_pci_notify(struct notifier_block *nb, + unsigned long action, void *data); +static void apple_pcie_hib_l1ss_pci_unregister(void *data); static void apple_pcie_hib_free_l1ss(struct apple_pcie *pcie); static void rmw_set(u32 set, void __iomem *addr) @@ -3651,6 +3665,8 @@ static void apple_pcie_cleanup(void *data) struct apple_pcie *pcie = data; struct apple_pcie_port *port, *tmp; + WRITE_ONCE(pcie->hib_l1ss_closing, true); + cancel_work_sync(&pcie->hib_l1ss_work); apple_pcie_hib_free_l1ss(pcie); list_for_each_entry_safe(port, tmp, &pcie->ports, entry) apple_pcie_port_teardown(port); @@ -6015,7 +6031,11 @@ static int apple_pcie_probe(struct platform_device *pdev) INIT_LIST_HEAD(&pcie->ports); INIT_LIST_HEAD(&pcie->hib_fns); INIT_LIST_HEAD(&pcie->hib_l1ss); + INIT_LIST_HEAD(&pcie->hib_l1ss_pending); INIT_LIST_HEAD(&pcie->hib_l1ss_rpm); + mutex_init(&pcie->hib_l1ss_lock); + INIT_WORK(&pcie->hib_l1ss_work, apple_pcie_hib_l1ss_work_fn); + atomic_set(&pcie->hib_l1ss_pending_cnt, 0); /* Resolve PM dependencies before publishing the PCI hierarchy. */ if (pcie->hw->tunneled) { @@ -6051,6 +6071,21 @@ static int apple_pcie_probe(struct platform_device *pdev) if (hw->adopt_link) bridge->reset_root_port = apple_pcie_t6050_reset_root_port; + if (apple_pcie_s2r_reinit(pcie)) { + bridge->pm_state_change = apple_pcie_pm_state_change; + pcie->hib_l1ss_pci_nb.notifier_call = apple_pcie_hib_l1ss_pci_notify; + ret = bus_register_notifier(&pci_bus_type, &pcie->hib_l1ss_pci_nb); + if (ret) { + apple_pcie_cleanup(pcie); + return ret; + } + ret = devm_add_action_or_reset(dev, apple_pcie_hib_l1ss_pci_unregister, + pcie); + if (ret) { + apple_pcie_cleanup(pcie); + return ret; + } + } ret = apple_pcie_host_init(pdev, bridge); if (ret) { apple_pcie_cleanup(pcie); @@ -6679,6 +6714,8 @@ static void apple_pcie_remove(struct platform_device *pdev) * surprise-removal path. In particular, this keeps NVMe teardown from * issuing MMIO to the dead tunneled aperture and wedging the SoC. */ + WRITE_ONCE(pcie->hib_l1ss_closing, true); + cancel_work_sync(&pcie->hib_l1ss_work); pci_lock_rescan_remove(); apple_pcie_hib_free_l1ss(pcie); if (!pcie->bus_stopped) { @@ -7742,6 +7779,7 @@ struct apple_pcie_hib_fn { struct apple_pcie_hib_l1ss { struct list_head entry; + struct apple_pcie *pcie; struct pci_dev *parent; struct pci_dev *child; u32 pl_ctl2; @@ -7752,6 +7790,10 @@ struct apple_pcie_hib_l1ss { u16 fn_lnkctl[8]; u8 fn_devfn[8]; unsigned int nfn; + bool active; + bool hw_cleared; + bool timing_pending; + bool rollback_pending; }; struct apple_pcie_hib_rpm { @@ -7766,6 +7808,47 @@ struct apple_pcie_l1ss_walk { bool added; }; +struct apple_pcie_l1ss_restore_ctx { + struct apple_pcie_hib_l1ss *saved; + bool rollback; + bool from_work; + bool keep_pending; +}; + +struct apple_pcie_l1ss_hw_ctx { + const struct apple_pcie_hib_l1ss *saved; + bool enable; + bool rollback; + bool core_managed; + bool restore_aspm_on_err; + bool deferred; + bool aspm_lost; + u16 restore_plnkctl; + u16 restore_fn_lnkctl[8]; + u8 restore_fn_devfn[8]; + unsigned int restore_nfn; +}; + +struct apple_pcie_l1ss_disable_hw_ctx { + struct apple_pcie_hib_l1ss *saved; + struct apple_pcie_hib_l1ss pend; + bool pend_has_timing; + bool pend_has_pcipm; + bool pend_rollback; + bool core_managed; +}; + +struct apple_pcie_l1ss_recheck_hw_ctx { + struct apple_pcie_hib_l1ss cur; + bool had_l1ss; +}; + +struct apple_pcie_l1ss_recheck_ctx { + struct apple_pcie *pcie; + struct pci_dev *parent; + struct pci_dev *child; +}; + static int apple_pcie_cfg_read_u32(struct pci_dev *pdev, int pos, u32 *val) { int ret = pci_read_config_dword(pdev, pos, val); @@ -7868,16 +7951,22 @@ static int apple_pcie_hib_rpm_add(struct apple_pcie *pcie, struct pci_dev *pdev, { struct apple_pcie_hib_rpm *rpm; + mutex_lock(&pcie->hib_l1ss_lock); list_for_each_entry(rpm, &pcie->hib_l1ss_rpm, entry) { - if (rpm->pdev == pdev) + if (rpm->pdev == pdev) { + mutex_unlock(&pcie->hib_l1ss_lock); return 0; + } } rpm = kzalloc_obj(*rpm); - if (!rpm) + if (!rpm) { + mutex_unlock(&pcie->hib_l1ss_lock); return -ENOMEM; + } pm_runtime_get_noresume(&pdev->dev); rpm->pdev = pci_dev_get(pdev); list_add_tail(&rpm->entry, &pcie->hib_l1ss_rpm); + mutex_unlock(&pcie->hib_l1ss_lock); *added = true; return 0; } @@ -7918,8 +8007,13 @@ static int apple_pcie_hib_rpm_pin_cb(struct pci_dev *parent, void *data) static void apple_pcie_hib_rpm_release(struct apple_pcie *pcie) { struct apple_pcie_hib_rpm *rpm, *tmp; + LIST_HEAD(release); - list_for_each_entry_safe(rpm, tmp, &pcie->hib_l1ss_rpm, entry) { + mutex_lock(&pcie->hib_l1ss_lock); + list_splice_init(&pcie->hib_l1ss_rpm, &release); + mutex_unlock(&pcie->hib_l1ss_lock); + + list_for_each_entry_safe(rpm, tmp, &release, entry) { list_del(&rpm->entry); pm_runtime_put(&rpm->pdev->dev); pci_dev_put(rpm->pdev); @@ -7927,9 +8021,36 @@ static void apple_pcie_hib_rpm_release(struct apple_pcie *pcie) } } -static int apple_pcie_link_set_l1ss(const struct apple_pcie_hib_l1ss *saved, - bool enable, bool rollback) +static void apple_pcie_hib_restore_aspm(struct pci_dev *parent, + const struct apple_pcie_hib_l1ss *saved) +{ + struct pci_dev *fn; + + if (!parent->subordinate) + return; + if (FIELD_GET(PCI_EXP_LNKCTL_ASPMC, saved->pl_lnkctl)) + apple_pcie_lnkctl_write(parent, saved->pl_lnkctl); + list_for_each_entry(fn, &parent->subordinate->devices, bus_list) { + u16 orig = apple_pcie_hib_orig_fn_lnkctl(saved, fn->devfn, 0); + + if (FIELD_GET(PCI_EXP_LNKCTL_ASPMC, orig)) + apple_pcie_lnkctl_write(fn, orig); + } +} + +/* + * Executed under aspm_l1ss_hw_lock via pcie_aspm_l1ss_hw_exec() so LNKCTL, + * L1SS, and unmanaged D0 checks serialize against pci_save_pcie_state(), + * pci_restore_pcie_state(), pci_set_low_power_state(), pci_pm_reset(), and + * pcie_config_aspm_link() in both CONFIG_PCIEASPM=y and CONFIG_PCIEASPM=n. + */ +static int apple_pcie_link_set_l1ss_hw(void *data) { + struct apple_pcie_l1ss_hw_ctx *hw = data; + const struct apple_pcie_hib_l1ss *saved = hw->saved; + bool enable = hw->enable; + bool rollback = hw->rollback; + bool core_managed = hw->core_managed; struct pci_dev *parent = saved->parent; struct pci_dev *child = saved->child; u32 pl_ctl2 = saved->pl_ctl2; @@ -7946,6 +8067,10 @@ static int apple_pcie_link_set_l1ss(const struct apple_pcie_hib_l1ss *saved, struct pci_dev *fns[8]; int ret; + hw->deferred = false; + hw->aspm_lost = false; + hw->restore_nfn = 0; + if (enable) { ret = apple_pcie_cfg_read_u32(parent, parent->l1ss + PCI_L1SS_CTL1, &cur_pl_ctl1) ?: @@ -7953,11 +8078,6 @@ static int apple_pcie_link_set_l1ss(const struct apple_pcie_hib_l1ss *saved, &cur_cl_ctl1); if (ret) return ret; - if (parent->current_state != PCI_D0 || - child->current_state != PCI_D0) { - pl_ctl1 &= ~APPLE_PCIE_L1SS_PCIPM_MASK; - cl_ctl1 &= ~APPLE_PCIE_L1SS_PCIPM_MASK; - } ret = apple_pcie_lnkctl_read(parent, &plnkctl); if (ret) return ret; @@ -8034,23 +8154,53 @@ static int apple_pcie_link_set_l1ss(const struct apple_pcie_hib_l1ss *saved, ret = apple_pcie_cfg_write_u32(child, child->l1ss + PCI_L1SS_CTL2, cl_ctl2); if (ret) goto unwind_l1ss; - ret = apple_pcie_cfg_write_u32(parent, parent->l1ss + PCI_L1SS_CTL1, - pl_ctl1 & ~PCI_L1SS_CTL1_L1_2_MASK); - if (ret) - goto unwind_l1ss; - ret = apple_pcie_cfg_write_u32(child, child->l1ss + PCI_L1SS_CTL1, - cl_ctl1 & ~PCI_L1SS_CTL1_L1_2_MASK); - if (ret) - goto unwind_l1ss; - if ((pl_ctl1 | cl_ctl1) & PCI_L1SS_CTL1_L1_2_MASK) { + if (core_managed) { + /* + * Restore timing fields only; the PCI core enables + * policy-permitted L1SS bits and defers PCI-PM L1.1/L1.2 + * until both ends of the link are in PCI_D0. + */ + ret = apple_pcie_cfg_write_u32(parent, parent->l1ss + PCI_L1SS_CTL1, + pl_ctl1 & ~PCI_L1SS_CTL1_L1SS_MASK); + if (ret) + goto unwind_l1ss; + ret = apple_pcie_cfg_write_u32(child, child->l1ss + PCI_L1SS_CTL1, + cl_ctl1 & ~PCI_L1SS_CTL1_L1SS_MASK); + if (ret) + goto unwind_l1ss; + } else { + /* + * Both ports must be in D0 before enabling PCI-PM + * L1.1/L1.2. Check current_state under aspm_l1ss_hw_lock + * so pci_set_low_power_state() and pci_pm_reset() + * cannot transition an endpoint out of D0 before PCI-PM + * bits are written. + */ + if (((pl_ctl1 | cl_ctl1) & APPLE_PCIE_L1SS_PCIPM_MASK) && + (READ_ONCE(parent->current_state) != PCI_D0 || + READ_ONCE(child->current_state) != PCI_D0)) { + hw->deferred = true; + pl_ctl1 &= ~APPLE_PCIE_L1SS_PCIPM_MASK; + cl_ctl1 &= ~APPLE_PCIE_L1SS_PCIPM_MASK; + } ret = apple_pcie_cfg_write_u32(parent, parent->l1ss + PCI_L1SS_CTL1, - pl_ctl1); + pl_ctl1 & ~PCI_L1SS_CTL1_L1_2_MASK); if (ret) goto unwind_l1ss; ret = apple_pcie_cfg_write_u32(child, child->l1ss + PCI_L1SS_CTL1, - cl_ctl1); + cl_ctl1 & ~PCI_L1SS_CTL1_L1_2_MASK); if (ret) goto unwind_l1ss; + if ((pl_ctl1 | cl_ctl1) & PCI_L1SS_CTL1_L1_2_MASK) { + ret = apple_pcie_cfg_write_u32(parent, parent->l1ss + PCI_L1SS_CTL1, + pl_ctl1); + if (ret) + goto unwind_l1ss; + ret = apple_pcie_cfg_write_u32(child, child->l1ss + PCI_L1SS_CTL1, + cl_ctl1); + if (ret) + goto unwind_l1ss; + } } } goto restore_aspm; @@ -8067,7 +8217,9 @@ static int apple_pcie_link_set_l1ss(const struct apple_pcie_hib_l1ss *saved, int r = apple_pcie_lnkctl_write(parent, restore_plnkctl); if (r && enable) - apple_pcie_lnkctl_write(parent, restore_plnkctl); + r = apple_pcie_lnkctl_write(parent, restore_plnkctl); + if (r && enable) + hw->aspm_lost = true; if (!ret) ret = r; } @@ -8076,21 +8228,262 @@ static int apple_pcie_link_set_l1ss(const struct apple_pcie_hib_l1ss *saved, int r = apple_pcie_lnkctl_write(fns[i], restore_fn_lnkctl[i]); if (r && enable) - apple_pcie_lnkctl_write(fns[i], restore_fn_lnkctl[i]); + r = apple_pcie_lnkctl_write(fns[i], restore_fn_lnkctl[i]); + if (r && enable) + hw->aspm_lost = true; if (!ret) ret = r; } } + if (hw->aspm_lost) { + hw->restore_plnkctl = restore_plnkctl; + hw->restore_nfn = nfn; + for (i = 0; i < nfn; i++) { + hw->restore_fn_lnkctl[i] = restore_fn_lnkctl[i]; + hw->restore_fn_devfn[i] = fns[i]->devfn; + } + } + if (ret && hw->restore_aspm_on_err) + apple_pcie_hib_restore_aspm(parent, saved); + return ret; +} + +static int apple_pcie_link_set_l1ss(struct apple_pcie_l1ss_hw_ctx *hw, + bool save_state) +{ + return pcie_aspm_l1ss_hw_exec(hw->saved->child, save_state, + apple_pcie_link_set_l1ss_hw, hw); +} + +/* + * Executed under aspm_l1ss_hw_lock so parent/child CTL2, CTL1, LNKCTL snapshot + * and hardware teardown are atomic against pci_restore_pcie_state(). + */ +static int apple_pcie_hib_disable_l1ss_hw(void *data) +{ + struct apple_pcie_l1ss_disable_hw_ctx *dhw = data; + struct apple_pcie_hib_l1ss *saved = dhw->saved; + struct pci_dev *parent = saved->parent; + struct pci_dev *child = saved->child; + struct apple_pcie_l1ss_hw_ctx hw; + unsigned int i; + int ret; + + ret = apple_pcie_cfg_read_u32(parent, parent->l1ss + PCI_L1SS_CTL2, + &saved->pl_ctl2) ?: + apple_pcie_cfg_read_u32(parent, parent->l1ss + PCI_L1SS_CTL1, + &saved->pl_ctl1) ?: + apple_pcie_cfg_read_u32(child, child->l1ss + PCI_L1SS_CTL2, + &saved->cl_ctl2) ?: + apple_pcie_cfg_read_u32(child, child->l1ss + PCI_L1SS_CTL1, + &saved->cl_ctl1); + if (ret) + return ret; + + ret = apple_pcie_hib_save_lnkctl(parent, saved); + if (ret) + return ret; + + if (dhw->pend_has_timing) { + saved->pl_ctl2 = dhw->pend.pl_ctl2; + saved->pl_ctl1 = dhw->pend.pl_ctl1; + saved->cl_ctl2 = dhw->pend.cl_ctl2; + saved->cl_ctl1 = dhw->pend.cl_ctl1; + } else if (dhw->pend_has_pcipm) { + saved->pl_ctl1 |= dhw->pend.pl_ctl1 & APPLE_PCIE_L1SS_PCIPM_MASK; + saved->cl_ctl1 |= dhw->pend.cl_ctl1 & APPLE_PCIE_L1SS_PCIPM_MASK; + } + if (dhw->pend_rollback) { + saved->pl_lnkctl = apple_pcie_hib_merge_aspm(saved->pl_lnkctl, + dhw->pend.pl_lnkctl); + for (i = 0; i < saved->nfn; i++) { + u16 orig = apple_pcie_hib_orig_fn_lnkctl(&dhw->pend, + saved->fn_devfn[i], + saved->fn_lnkctl[i]); + + saved->fn_lnkctl[i] = + apple_pcie_hib_merge_aspm(saved->fn_lnkctl[i], orig); + } + saved->rollback_pending = true; + } + + if (!((saved->pl_ctl1 | saved->cl_ctl1) & PCI_L1SS_CTL1_L1SS_MASK)) { + saved->active = dhw->core_managed || dhw->pend_rollback; + saved->hw_cleared = dhw->pend_rollback; + return 0; + } + + saved->active = true; + saved->hw_cleared = true; + hw = (struct apple_pcie_l1ss_hw_ctx){ + .saved = saved, + .enable = false, + }; + return apple_pcie_link_set_l1ss_hw(&hw); +} + +static int apple_pcie_hib_disable_l1ss_link(void *data, bool core_managed) +{ + struct apple_pcie_hib_l1ss *saved = data; + struct apple_pcie *pcie = saved->pcie; + struct pci_dev *parent = saved->parent; + struct pci_dev *child = saved->child; + struct apple_pcie_hib_l1ss *pend, *tmp; + struct apple_pcie_l1ss_disable_hw_ctx dhw = { + .saved = saved, + .core_managed = core_managed, + }; + int ret; + + /* + * Hold hib_l1ss_lock across the pending-record inspection, atomic + * hardware snapshot+teardown (under aspm_l1ss_hw_lock), and pending + * list drain so a concurrent D0 worker cannot race the snapshot. + */ + mutex_lock(&pcie->hib_l1ss_lock); + list_for_each_entry(pend, &pcie->hib_l1ss_pending, entry) { + if (pend->parent != parent || pend->child != child) + continue; + if (pend->timing_pending && pend->hw_cleared) { + dhw.pend_has_timing = true; + dhw.pend.pl_ctl2 = pend->pl_ctl2; + dhw.pend.pl_ctl1 = pend->pl_ctl1; + dhw.pend.cl_ctl2 = pend->cl_ctl2; + dhw.pend.cl_ctl1 = pend->cl_ctl1; + } else if (pend->hw_cleared) { + dhw.pend_has_pcipm = true; + dhw.pend.pl_ctl1 |= pend->pl_ctl1 & APPLE_PCIE_L1SS_PCIPM_MASK; + dhw.pend.cl_ctl1 |= pend->cl_ctl1 & APPLE_PCIE_L1SS_PCIPM_MASK; + } + if (pend->rollback_pending) { + dhw.pend_rollback = true; + dhw.pend.pl_lnkctl = pend->pl_lnkctl; + memcpy(dhw.pend.fn_lnkctl, pend->fn_lnkctl, + sizeof(dhw.pend.fn_lnkctl)); + memcpy(dhw.pend.fn_devfn, pend->fn_devfn, + sizeof(dhw.pend.fn_devfn)); + dhw.pend.nfn = pend->nfn; + } + } + + ret = pcie_aspm_l1ss_hw_exec(child, true, apple_pcie_hib_disable_l1ss_hw, + &dhw); + if (!ret) { + list_for_each_entry_safe(pend, tmp, &pcie->hib_l1ss_pending, entry) { + if (pend->parent != parent || pend->child != child) + continue; + list_del_init(&pend->entry); + atomic_dec(&pcie->hib_l1ss_pending_cnt); + pci_dev_put(pend->child); + pci_dev_put(pend->parent); + kfree(pend); + } + } + mutex_unlock(&pcie->hib_l1ss_lock); return ret; } +static int apple_pcie_hib_restore_l1ss_link(void *data, bool core_managed) +{ + struct apple_pcie_l1ss_restore_ctx *ctx = data; + struct apple_pcie_hib_l1ss *saved = ctx->saved; + struct apple_pcie *pcie = saved->pcie; + struct pci_dev *parent = saved->parent; + struct pci_dev *child = saved->child; + struct apple_pcie_l1ss_hw_ctx hw = { + .saved = saved, + .enable = true, + .rollback = ctx->rollback, + .core_managed = core_managed, + }; + bool requeue_d0 = false; + u32 id; + int ret; + + if (!saved->hw_cleared) + return 0; + + /* + * Only confirmed topology removal returns -ENODEV (dropping the record + * and clearing core suppression). Config errors and D3cold 0xffffffff + * reads on a still-enumerated link retain recovery state. + */ + if (!apple_pcie_hib_link_present(parent, child)) + return -ENODEV; + ret = pcibios_err_to_errno(pci_read_config_dword(parent, PCI_VENDOR_ID, &id)); + if (ret) + return ret == -ENODEV ? -EIO : ret; + if (id == U32_MAX) + return -EAGAIN; + ret = pcibios_err_to_errno(pci_read_config_dword(child, PCI_VENDOR_ID, &id)); + if (ret) + return ret == -ENODEV ? -EIO : ret; + if (id == U32_MAX) + return -EAGAIN; + + /* + * Hold hib_l1ss_lock across the hardware restore and pending-record + * publication. When called from hib_l1ss_work (ctx->from_work), + * keep_pending hands ownership back to the worker's retry list while + * hib_l1ss_pending_cnt remains elevated for the entire lifetime of + * the record. + */ + mutex_lock(&pcie->hib_l1ss_lock); + if (core_managed) { + ret = apple_pcie_link_set_l1ss(&hw, false); + if (ret) + goto err_unlock; + saved->timing_pending = false; + saved->rollback_pending = false; + mutex_unlock(&pcie->hib_l1ss_lock); + return 0; + } + + ret = apple_pcie_link_set_l1ss(&hw, true); + if (ret) + goto err_unlock; + + saved->timing_pending = false; + saved->rollback_pending = false; + if (hw.deferred && !READ_ONCE(pcie->hib_l1ss_closing)) { + ctx->keep_pending = true; + if (!ctx->from_work) { + list_add_tail(&saved->entry, &pcie->hib_l1ss_pending); + atomic_inc(&pcie->hib_l1ss_pending_cnt); + /* Pair with smp_mb() in pci_pm_notify_host(). */ + smp_mb__after_atomic(); + if (READ_ONCE(parent->current_state) == PCI_D0 && + READ_ONCE(child->current_state) == PCI_D0) + requeue_d0 = true; + } + } + mutex_unlock(&pcie->hib_l1ss_lock); + + if (requeue_d0) + queue_work(system_freezable_wq, &pcie->hib_l1ss_work); + return 0; + +err_unlock: + if (hw.aspm_lost) { + saved->pl_lnkctl = hw.restore_plnkctl; + saved->nfn = hw.restore_nfn; + memcpy(saved->fn_lnkctl, hw.restore_fn_lnkctl, + sizeof(saved->fn_lnkctl)); + memcpy(saved->fn_devfn, hw.restore_fn_devfn, + sizeof(saved->fn_devfn)); + saved->rollback_pending = true; + } + mutex_unlock(&pcie->hib_l1ss_lock); + return ret == -ENODEV ? -EIO : ret; +} + static int apple_pcie_hib_disable_l1ss_cb(struct pci_dev *parent, void *data) { struct apple_pcie_l1ss_walk *walk = data; struct apple_pcie *pcie = walk->pcie; struct pci_dev *child = NULL, *fn; struct apple_pcie_hib_l1ss *saved; - u32 pl_ctl2, pl_ctl1, cl_ctl2, cl_ctl1, id; + u32 id; int ret; if (!pcie_downstream_port(parent) || !parent->l1ss || !parent->subordinate) @@ -8122,43 +8515,38 @@ static int apple_pcie_hib_disable_l1ss_cb(struct pci_dev *parent, void *data) if (id == U32_MAX) return 0; - ret = apple_pcie_cfg_read_u32(parent, parent->l1ss + PCI_L1SS_CTL2, &pl_ctl2) ?: - apple_pcie_cfg_read_u32(parent, parent->l1ss + PCI_L1SS_CTL1, &pl_ctl1) ?: - apple_pcie_cfg_read_u32(child, child->l1ss + PCI_L1SS_CTL2, &cl_ctl2) ?: - apple_pcie_cfg_read_u32(child, child->l1ss + PCI_L1SS_CTL1, &cl_ctl1); - if (ret) { - walk->err = ret; - return 1; - } - if (!((pl_ctl1 | cl_ctl1) & PCI_L1SS_CTL1_L1SS_MASK)) - return 0; - saved = kzalloc_obj(*saved); if (!saved) { walk->err = -ENOMEM; return 1; } - saved->pl_ctl2 = pl_ctl2; - saved->pl_ctl1 = pl_ctl1; - saved->cl_ctl2 = cl_ctl2; - saved->cl_ctl1 = cl_ctl1; - ret = apple_pcie_hib_save_lnkctl(parent, saved); - if (ret) { - kfree(saved); - walk->err = ret; - return 1; - } + saved->pcie = pcie; saved->parent = pci_dev_get(parent); saved->child = pci_dev_get(child); - list_add_tail(&saved->entry, &pcie->hib_l1ss); /* - * Clear L1SS downstream before upstream so endpoints entering D3hot do - * not drop the link into L1.2 (CLKREQ# deasserted) ahead of the sleep - * commit. Added to hib_l1ss first so a partial failure rolls back this - * link together with earlier links. + * Snapshot CTL1/LNKCTL and clear L1SS downstream before upstream under + * pci_rescan_remove_lock -> pci_bus_sem -> aspm_lock -> hib_l1ss_lock + * -> aspm_l1ss_hw_lock. Added to hib_l1ss first so a partial failure + * rolls back this link together with earlier links. */ - ret = apple_pcie_link_set_l1ss(saved, false, false); + mutex_lock(&pcie->hib_l1ss_lock); + list_add_tail(&saved->entry, &pcie->hib_l1ss); + mutex_unlock(&pcie->hib_l1ss_lock); + + ret = pcie_aspm_l1ss_sleep_begin_locked(parent, child, + apple_pcie_hib_disable_l1ss_link, + saved); + if (!saved->active) { + mutex_lock(&pcie->hib_l1ss_lock); + if (!list_empty(&saved->entry)) { + list_del_init(&saved->entry); + pci_dev_put(child); + pci_dev_put(parent); + kfree(saved); + } + mutex_unlock(&pcie->hib_l1ss_lock); + } if (ret) { walk->err = ret; return 1; @@ -8170,63 +8558,371 @@ static void apple_pcie_hib_free_l1ss(struct apple_pcie *pcie) { struct apple_pcie_hib_l1ss *saved, *tmp; + mutex_lock(&pcie->hib_l1ss_lock); list_for_each_entry_safe(saved, tmp, &pcie->hib_l1ss, entry) { - list_del(&saved->entry); + list_del_init(&saved->entry); pci_dev_put(saved->child); pci_dev_put(saved->parent); kfree(saved); } + list_for_each_entry_safe(saved, tmp, &pcie->hib_l1ss_pending, entry) { + list_del_init(&saved->entry); + atomic_dec(&pcie->hib_l1ss_pending_cnt); + pci_dev_put(saved->child); + pci_dev_put(saved->parent); + kfree(saved); + } + mutex_unlock(&pcie->hib_l1ss_lock); apple_pcie_hib_rpm_release(pcie); } +static void apple_pcie_hib_retain_failed(struct apple_pcie *pcie, + struct apple_pcie_hib_l1ss *saved, + struct apple_pcie_l1ss_restore_ctx *ctx, + int ret) +{ + bool requeue_d0 = false; + + mutex_lock(&pcie->hib_l1ss_lock); + if (!READ_ONCE(pcie->hib_l1ss_closing)) { + saved->timing_pending = true; + if (ctx->rollback) + saved->rollback_pending = true; + ctx->keep_pending = true; + if (!ctx->from_work) { + list_add_tail(&saved->entry, &pcie->hib_l1ss_pending); + atomic_inc(&pcie->hib_l1ss_pending_cnt); + /* Pair with smp_mb() in pci_pm_notify_host(). */ + smp_mb__after_atomic(); + if (READ_ONCE(saved->parent->current_state) == PCI_D0 && + READ_ONCE(saved->child->current_state) == PCI_D0) + requeue_d0 = true; + } + } + mutex_unlock(&pcie->hib_l1ss_lock); + if (requeue_d0) + queue_work(system_freezable_wq, &pcie->hib_l1ss_work); + if (!(ret == -EAGAIN && + (READ_ONCE(saved->parent->current_state) == PCI_D3cold || + READ_ONCE(saved->child->current_state) == PCI_D3cold))) + pci_warn(saved->child, + "failed to restore PCIe L1SS after sleep: %d\n", ret); +} + static void apple_pcie_hib_restore_l1ss_locked(struct apple_pcie *pcie, bool rollback) { - struct apple_pcie_hib_l1ss *saved, *tmp; + for (;;) { + struct apple_pcie_hib_l1ss *saved; + struct pci_dev *parent, *child; + struct apple_pcie_l1ss_restore_ctx ctx; + int ret; - list_for_each_entry_safe(saved, tmp, &pcie->hib_l1ss, entry) { - struct pci_dev *parent = saved->parent; - struct pci_dev *child = saved->child; - u32 id; + mutex_lock(&pcie->hib_l1ss_lock); + saved = list_first_entry_or_null(&pcie->hib_l1ss, + struct apple_pcie_hib_l1ss, + entry); + if (saved) + list_del_init(&saved->entry); + mutex_unlock(&pcie->hib_l1ss_lock); + if (!saved) + break; - list_del(&saved->entry); - if (apple_pcie_hib_link_present(parent, child) && - !apple_pcie_cfg_read_u32(parent, PCI_VENDOR_ID, &id) && - !apple_pcie_cfg_read_u32(child, PCI_VENDOR_ID, &id)) { - int ret = apple_pcie_link_set_l1ss(saved, true, rollback); + parent = saved->parent; + child = saved->child; + ctx = (struct apple_pcie_l1ss_restore_ctx){ + .saved = saved, + .rollback = rollback || saved->rollback_pending, + }; - if (ret) - pci_warn(child, "failed to restore PCIe L1SS after sleep: %d\n", - ret); + ret = pcie_aspm_l1ss_sleep_end(parent, child, + apple_pcie_hib_restore_l1ss_link, + &ctx); + if (ret && ret != -ENODEV && !ctx.keep_pending) + apple_pcie_hib_retain_failed(pcie, saved, &ctx, ret); + if (!ctx.keep_pending) { + pci_dev_put(child); + pci_dev_put(parent); + kfree(saved); } - pci_dev_put(child); - pci_dev_put(parent); - kfree(saved); } } static void apple_pcie_hib_restore_l1ss(struct apple_pcie *pcie, bool rollback) { + bool empty; + + mutex_lock(&pcie->hib_l1ss_lock); + empty = list_empty(&pcie->hib_l1ss); + mutex_unlock(&pcie->hib_l1ss_lock); + if (empty) + return; + pci_lock_rescan_remove(); apple_pcie_hib_restore_l1ss_locked(pcie, rollback); pci_unlock_rescan_remove(); } -static void apple_pcie_hib_restore_aspm(struct pci_dev *parent, - const struct apple_pcie_hib_l1ss *saved) +static void apple_pcie_hib_l1ss_work_fn(struct work_struct *work) { - struct pci_dev *fn; + struct apple_pcie *pcie = container_of(work, struct apple_pcie, + hib_l1ss_work); + struct apple_pcie_hib_l1ss *saved, *tmp; + bool requeue_d0 = false; + LIST_HEAD(retry); - if (!parent->subordinate) + pci_lock_rescan_remove(); + for (;;) { + struct pci_dev *parent, *child; + struct apple_pcie_l1ss_restore_ctx ctx; + + /* + * Keep hib_l1ss_pending_cnt elevated while the record is off + * hib_l1ss_pending so a concurrent D0 notification still sees + * a live pending entry and re-queues this work item. + */ + mutex_lock(&pcie->hib_l1ss_lock); + saved = list_first_entry_or_null(&pcie->hib_l1ss_pending, + struct apple_pcie_hib_l1ss, + entry); + if (saved) + list_del_init(&saved->entry); + mutex_unlock(&pcie->hib_l1ss_lock); + if (!saved) + break; + + parent = saved->parent; + child = saved->child; + if (READ_ONCE(pcie->hib_l1ss_closing) || + !apple_pcie_hib_link_present(parent, child)) { + atomic_dec(&pcie->hib_l1ss_pending_cnt); + pci_dev_put(child); + pci_dev_put(parent); + kfree(saved); + continue; + } + if (!saved->timing_pending && + (READ_ONCE(parent->current_state) != PCI_D0 || + READ_ONCE(child->current_state) != PCI_D0)) { + list_add_tail(&saved->entry, &retry); + continue; + } + + int ret; + + ctx = (struct apple_pcie_l1ss_restore_ctx){ + .saved = saved, + .rollback = saved->rollback_pending, + .from_work = true, + }; + ret = pcie_aspm_l1ss_sleep_end(parent, child, + apple_pcie_hib_restore_l1ss_link, + &ctx); + if (ret && ret != -ENODEV && !ctx.keep_pending) + apple_pcie_hib_retain_failed(pcie, saved, &ctx, ret); + if (ctx.keep_pending) { + list_add_tail(&saved->entry, &retry); + } else { + atomic_dec(&pcie->hib_l1ss_pending_cnt); + pci_dev_put(child); + pci_dev_put(parent); + kfree(saved); + } + } + + mutex_lock(&pcie->hib_l1ss_lock); + list_for_each_entry_safe(saved, tmp, &retry, entry) { + list_del_init(&saved->entry); + if (READ_ONCE(pcie->hib_l1ss_closing)) { + atomic_dec(&pcie->hib_l1ss_pending_cnt); + pci_dev_put(saved->child); + pci_dev_put(saved->parent); + kfree(saved); + } else { + list_add_tail(&saved->entry, &pcie->hib_l1ss_pending); + /* Pair with smp_mb() in pci_pm_notify_host(). */ + smp_mb(); + if (!saved->timing_pending && + READ_ONCE(saved->parent->current_state) == PCI_D0 && + READ_ONCE(saved->child->current_state) == PCI_D0) + requeue_d0 = true; + } + } + mutex_unlock(&pcie->hib_l1ss_lock); + pci_unlock_rescan_remove(); + + if (requeue_d0 && !READ_ONCE(pcie->hib_l1ss_closing)) + queue_work(system_freezable_wq, &pcie->hib_l1ss_work); +} + +static void apple_pcie_pm_state_change(struct pci_host_bridge *bridge, + struct pci_dev *pdev) +{ + struct apple_pcie *pcie = pci_host_bridge_priv(bridge); + + if (READ_ONCE(pdev->current_state) != PCI_D0) + return; + if (READ_ONCE(pcie->hib_l1ss_closing) || + !atomic_read(&pcie->hib_l1ss_pending_cnt)) return; - if (FIELD_GET(PCI_EXP_LNKCTL_ASPMC, saved->pl_lnkctl)) - apple_pcie_lnkctl_write(parent, saved->pl_lnkctl); - list_for_each_entry(fn, &parent->subordinate->devices, bus_list) { - u16 orig = apple_pcie_hib_orig_fn_lnkctl(saved, fn->devfn, 0); - if (FIELD_GET(PCI_EXP_LNKCTL_ASPMC, orig)) - apple_pcie_lnkctl_write(fn, orig); + queue_work(system_freezable_wq, &pcie->hib_l1ss_work); +} + +static int apple_pcie_hib_l1ss_pci_notify(struct notifier_block *nb, + unsigned long action, void *data) +{ + struct apple_pcie *pcie = container_of(nb, struct apple_pcie, + hib_l1ss_pci_nb); + struct pci_dev *pdev = to_pci_dev(data); + struct apple_pcie_hib_l1ss *saved, *tmp; + + if (action != BUS_NOTIFY_DEL_DEVICE) + return NOTIFY_DONE; + + mutex_lock(&pcie->hib_l1ss_lock); + list_for_each_entry_safe(saved, tmp, &pcie->hib_l1ss, entry) { + if (saved->parent != pdev && saved->child != pdev) + continue; + list_del_init(&saved->entry); + pci_dev_put(saved->child); + pci_dev_put(saved->parent); + kfree(saved); + } + list_for_each_entry_safe(saved, tmp, &pcie->hib_l1ss_pending, entry) { + if (saved->parent != pdev && saved->child != pdev) + continue; + list_del_init(&saved->entry); + atomic_dec(&pcie->hib_l1ss_pending_cnt); + pci_dev_put(saved->child); + pci_dev_put(saved->parent); + kfree(saved); + } + mutex_unlock(&pcie->hib_l1ss_lock); + return NOTIFY_DONE; +} + +static void apple_pcie_hib_l1ss_pci_unregister(void *data) +{ + struct apple_pcie *pcie = data; + + WRITE_ONCE(pcie->hib_l1ss_closing, true); + bus_unregister_notifier(&pci_bus_type, &pcie->hib_l1ss_pci_nb); + cancel_work_sync(&pcie->hib_l1ss_work); + apple_pcie_hib_free_l1ss(pcie); +} + +/* + * Executed under aspm_l1ss_hw_lock so suspend-recheck snapshot and hardware + * teardown are atomic against pci_restore_pcie_state(). + */ +static int apple_pcie_hib_recheck_l1ss_hw(void *data) +{ + struct apple_pcie_l1ss_recheck_hw_ctx *rhw = data; + struct apple_pcie_hib_l1ss *cur = &rhw->cur; + struct pci_dev *parent = cur->parent; + struct pci_dev *child = cur->child; + struct apple_pcie_l1ss_hw_ctx hw; + int ret; + + rhw->had_l1ss = false; + ret = apple_pcie_cfg_read_u32(parent, parent->l1ss + PCI_L1SS_CTL2, + &cur->pl_ctl2) ?: + apple_pcie_cfg_read_u32(parent, parent->l1ss + PCI_L1SS_CTL1, + &cur->pl_ctl1) ?: + apple_pcie_cfg_read_u32(child, child->l1ss + PCI_L1SS_CTL2, + &cur->cl_ctl2) ?: + apple_pcie_cfg_read_u32(child, child->l1ss + PCI_L1SS_CTL1, + &cur->cl_ctl1); + if (ret) + return ret; + if (!((cur->pl_ctl1 | cur->cl_ctl1) & PCI_L1SS_CTL1_L1SS_MASK)) + return 0; + + ret = apple_pcie_hib_save_lnkctl(parent, cur); + if (ret) + return ret; + + rhw->had_l1ss = true; + hw = (struct apple_pcie_l1ss_hw_ctx){ + .saved = cur, + .enable = false, + .restore_aspm_on_err = true, + }; + return apple_pcie_link_set_l1ss_hw(&hw); +} + +static int apple_pcie_hib_recheck_l1ss_link(void *data, bool core_managed) +{ + struct apple_pcie_l1ss_recheck_ctx *rctx = data; + struct apple_pcie *pcie = rctx->pcie; + struct pci_dev *parent = rctx->parent; + struct pci_dev *child = rctx->child; + struct apple_pcie_hib_l1ss *saved = NULL, *alloc = NULL, *entry; + struct apple_pcie_l1ss_recheck_hw_ctx rhw; + int ret; + + memset(&rhw, 0, sizeof(rhw)); + rhw.cur.pcie = pcie; + rhw.cur.parent = parent; + rhw.cur.child = child; + + mutex_lock(&pcie->hib_l1ss_lock); + list_for_each_entry(entry, &pcie->hib_l1ss, entry) { + if (entry->parent == parent && entry->child == child) { + saved = entry; + break; + } + } + /* + * Pre-allocate before modifying hardware so a post-clear -ENOMEM or + * partial hardware error never drops the recovery snapshot. + */ + if (!saved) { + alloc = kzalloc_obj(*alloc); + if (!alloc) { + mutex_unlock(&pcie->hib_l1ss_lock); + return -ENOMEM; + } } + + ret = pcie_aspm_l1ss_hw_exec(child, true, apple_pcie_hib_recheck_l1ss_hw, + &rhw); + + if (rhw.had_l1ss) { + if (!saved) { + *alloc = rhw.cur; + alloc->parent = pci_dev_get(parent); + alloc->child = pci_dev_get(child); + alloc->active = true; + alloc->hw_cleared = true; + list_add_tail(&alloc->entry, &pcie->hib_l1ss); + alloc = NULL; + } else if (!saved->hw_cleared) { + saved->pl_ctl2 = rhw.cur.pl_ctl2; + saved->pl_ctl1 = rhw.cur.pl_ctl1; + saved->cl_ctl2 = rhw.cur.cl_ctl2; + saved->cl_ctl1 = rhw.cur.cl_ctl1; + saved->pl_lnkctl = rhw.cur.pl_lnkctl; + memcpy(saved->fn_lnkctl, rhw.cur.fn_lnkctl, + sizeof(saved->fn_lnkctl)); + memcpy(saved->fn_devfn, rhw.cur.fn_devfn, + sizeof(saved->fn_devfn)); + saved->nfn = rhw.cur.nfn; + saved->active = true; + saved->hw_cleared = true; + } + } else if (!ret && core_managed && !saved) { + *alloc = rhw.cur; + alloc->parent = pci_dev_get(parent); + alloc->child = pci_dev_get(child); + alloc->active = true; + list_add_tail(&alloc->entry, &pcie->hib_l1ss); + alloc = NULL; + } + kfree(alloc); + mutex_unlock(&pcie->hib_l1ss_lock); + return ret; } static int apple_pcie_hib_recheck_l1ss_cb(struct pci_dev *parent, void *data) @@ -8234,9 +8930,8 @@ static int apple_pcie_hib_recheck_l1ss_cb(struct pci_dev *parent, void *data) struct apple_pcie_l1ss_walk *walk = data; struct apple_pcie *pcie = walk->pcie; struct pci_dev *child = NULL, *fn; - struct apple_pcie_hib_l1ss *saved = NULL, *entry; - struct apple_pcie_hib_l1ss cur; - u32 pl_ctl2, pl_ctl1, cl_ctl2, cl_ctl1, id; + struct apple_pcie_l1ss_recheck_ctx rctx; + u32 id; int ret; if (!pcie_downstream_port(parent) || !parent->l1ss || !parent->subordinate) @@ -8267,52 +8962,15 @@ static int apple_pcie_hib_recheck_l1ss_cb(struct pci_dev *parent, void *data) if (id == U32_MAX) return 0; - ret = apple_pcie_cfg_read_u32(parent, parent->l1ss + PCI_L1SS_CTL2, &pl_ctl2) ?: - apple_pcie_cfg_read_u32(parent, parent->l1ss + PCI_L1SS_CTL1, &pl_ctl1) ?: - apple_pcie_cfg_read_u32(child, child->l1ss + PCI_L1SS_CTL2, &cl_ctl2) ?: - apple_pcie_cfg_read_u32(child, child->l1ss + PCI_L1SS_CTL1, &cl_ctl1); - if (ret) { - walk->err = ret; - return 1; - } - if (!((pl_ctl1 | cl_ctl1) & PCI_L1SS_CTL1_L1SS_MASK)) - return 0; - - list_for_each_entry(entry, &pcie->hib_l1ss, entry) { - if (entry->parent == parent && entry->child == child) { - saved = entry; - break; - } - } - - memset(&cur, 0, sizeof(cur)); - cur.parent = parent; - cur.child = child; - cur.pl_ctl2 = pl_ctl2; - cur.pl_ctl1 = pl_ctl1; - cur.cl_ctl2 = cl_ctl2; - cur.cl_ctl1 = cl_ctl1; - ret = apple_pcie_hib_save_lnkctl(parent, &cur); - if (ret) { - walk->err = ret; - return 1; - } - - if (!saved) { - saved = kzalloc_obj(*saved); - if (!saved) { - walk->err = -ENOMEM; - return 1; - } - *saved = cur; - saved->parent = pci_dev_get(parent); - saved->child = pci_dev_get(child); - list_add_tail(&saved->entry, &pcie->hib_l1ss); - } - - ret = apple_pcie_link_set_l1ss(&cur, false, false); + rctx = (struct apple_pcie_l1ss_recheck_ctx){ + .pcie = pcie, + .parent = parent, + .child = child, + }; + ret = pcie_aspm_l1ss_sleep_begin_locked(parent, child, + apple_pcie_hib_recheck_l1ss_link, + &rctx); if (ret) { - apple_pcie_hib_restore_aspm(parent, &cur); walk->err = ret; return 1; } @@ -8582,55 +9240,56 @@ static int apple_pcie_prepare(struct device *dev) struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); if (bridge && bridge->bus) { + struct apple_pcie_l1ss_walk walk = { .pcie = pcie }; + struct apple_pcie_hib_rpm *rpm; unsigned int tries; /* - * Pin runtime PM on every L1SS link and drain in-flight - * runtime-PM callbacks outside PCI locks. Re-check under - * pci_lock_rescan_remove() until no new hotplugged link - * appears, then clear L1SS while still holding the lock. + * Pin runtime PM on all L1SS links and drain in-flight + * runtime-PM callbacks outside PCI locks before reading + * or clearing L1SS so a running runtime suspend cannot + * snapshot temporary L1SS-disabled registers. Repeat if + * hotplug adds a new L1SS link while PCI locks are dropped, + * then keep pci_lock_rescan_remove() held into the clear walk. */ for (tries = 0; tries < 8; tries++) { - struct apple_pcie_l1ss_walk walk = { .pcie = pcie }; - struct apple_pcie_hib_rpm *rpm; - + walk.added = false; + walk.err = 0; pci_lock_rescan_remove(); - pci_walk_bus(bridge->bus, apple_pcie_hib_rpm_pin_cb, - &walk); + pci_walk_bus(bridge->bus, + apple_pcie_hib_rpm_pin_cb, &walk); if (walk.err) { pci_unlock_rescan_remove(); apple_pcie_hib_rpm_release(pcie); apple_radio_complete(dev); return walk.err; } - if (!walk.added) { - pci_walk_bus(bridge->bus, - apple_pcie_hib_disable_l1ss_cb, - &walk); - if (walk.err) { - apple_pcie_hib_restore_l1ss_locked(pcie, - true); - pci_unlock_rescan_remove(); - apple_pcie_hib_rpm_release(pcie); - apple_radio_complete(dev); - return walk.err; - } - pci_unlock_rescan_remove(); + if (!walk.added) break; - } pci_unlock_rescan_remove(); - list_for_each_entry(rpm, &pcie->hib_l1ss_rpm, entry) { + list_for_each_entry(rpm, &pcie->hib_l1ss_rpm, + entry) { if (!rpm->drained) { pm_runtime_barrier(&rpm->pdev->dev); rpm->drained = true; } } } - if (tries == 8) { + if (walk.added) { apple_pcie_hib_rpm_release(pcie); apple_radio_complete(dev); return -EBUSY; } + + pci_walk_bus(bridge->bus, apple_pcie_hib_disable_l1ss_cb, &walk); + if (walk.err) { + apple_pcie_hib_restore_l1ss_locked(pcie, true); + pci_unlock_rescan_remove(); + apple_pcie_hib_rpm_release(pcie); + apple_radio_complete(dev); + return walk.err; + } + pci_unlock_rescan_remove(); } } return 0; @@ -8651,10 +9310,10 @@ static void apple_pcie_complete(struct device *dev) { struct apple_pcie *pcie = apple_pcie_lookup(dev); - if (pcie && !list_empty(&pcie->hib_l1ss)) + if (pcie && apple_pcie_s2r_reinit(pcie)) { apple_pcie_hib_restore_l1ss(pcie, false); - if (pcie && !list_empty(&pcie->hib_l1ss_rpm)) apple_pcie_hib_rpm_release(pcie); + } apple_radio_complete(dev); } diff --git a/drivers/pci/pci-driver.c b/drivers/pci/pci-driver.c index e16aa59dd7ac8..52e56934b1b4c 100644 --- a/drivers/pci/pci-driver.c +++ b/drivers/pci/pci-driver.c @@ -595,6 +595,7 @@ static int pci_restore_standard_config(struct pci_dev *pci_dev) pci_restore_state(pci_dev); pci_pme_restore(pci_dev); + pci_pm_notify_resume(pci_dev); return 0; } #endif /* CONFIG_PM_SLEEP */ @@ -614,6 +615,7 @@ static void pci_pm_default_resume_early(struct pci_dev *pci_dev) pci_pm_power_up_and_verify_state(pci_dev); pci_restore_state(pci_dev); pci_pme_restore(pci_dev); + pci_pm_notify_resume(pci_dev); } static void pci_pm_bridge_power_up_actions(struct pci_dev *pci_dev) diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index d79d2ee4ac69b..b25483c2584f7 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -1405,6 +1405,49 @@ int pci_power_up(struct pci_dev *dev) return 0; } +static void pci_pm_notify_host(struct pci_dev *dev) +{ + struct pci_host_bridge *bridge; + + /* Order dev->current_state = PCI_D0 before host pending checks. */ + smp_mb(); + bridge = dev->bus ? pci_find_host_bridge(dev->bus) : NULL; + if (bridge && bridge->pm_state_change) + bridge->pm_state_change(bridge, dev); +} + +/* + * Tell ASPM and the host bridge that @dev reached D0. @locked is true when + * the caller holds pci_bus_sem. + */ +void pci_pm_notify_state_change(struct pci_dev *dev, bool locked) +{ + if (dev->current_state != PCI_D0) + return; + + if (dev->bus && dev->bus->self) + pcie_aspm_pm_state_change(dev->bus->self, locked); + pcie_aspm_pm_state_change(dev, locked); + pci_pm_notify_host(dev); +} + +/* + * Same notification for resume paths that may run with pci_bus_sem held for + * read (runtime resume from pci_walk_bus() in error recovery). The ASPM update + * needs pci_bus_sem, so it is deferred to a freezable work item when a link + * has deferred PCI-PM L1SS; the host bridge hook takes no PCI core lock. + */ +void pci_pm_notify_resume(struct pci_dev *dev) +{ + if (dev->current_state != PCI_D0) + return; + + if (dev->bus && dev->bus->self) + pcie_aspm_pm_state_change_deferred(dev->bus->self); + pcie_aspm_pm_state_change_deferred(dev); + pci_pm_notify_host(dev); +} + /** * pci_set_full_power_state - Put a PCI device into D0 and update its state * @dev: PCI device to power up @@ -1432,7 +1475,7 @@ static int pci_set_full_power_state(struct pci_dev *dev, bool locked) } pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr); - dev->current_state = pmcsr & PCI_PM_CTRL_STATE_MASK; + WRITE_ONCE(dev->current_state, pmcsr & PCI_PM_CTRL_STATE_MASK); if (dev->current_state != PCI_D0) { pci_info_ratelimited(dev, "Refused to change power state from %s to D0\n", pci_power_name(dev->current_state)); @@ -1453,7 +1496,9 @@ static int pci_set_full_power_state(struct pci_dev *dev, bool locked) pci_restore_bars(dev); } - if (dev->bus->self) + if (dev->current_state == PCI_D0) + pci_pm_notify_state_change(dev, locked); + else if (dev->bus->self) pcie_aspm_pm_state_change(dev->bus->self, locked); return 0; @@ -1511,6 +1556,7 @@ static void __pci_bus_set_current_state(struct pci_bus *bus, pci_power_t state, */ static int pci_set_low_power_state(struct pci_dev *dev, pci_power_t state, bool locked) { + unsigned long flags; u16 pmcsr; if (!dev->pm_cap) @@ -1549,8 +1595,15 @@ static int pci_set_low_power_state(struct pci_dev *dev, pci_power_t state, bool pmcsr &= ~PCI_PM_CTRL_STATE_MASK; pmcsr |= state; - /* Enter specified state */ + /* + * Mark the device out of D0 and write PMCSR under aspm_l1ss_hw_lock so + * concurrent L1SS / PCI-PM enable sequences cannot see D0 while the + * device enters a low-power state. + */ + flags = pcie_aspm_l1ss_hw_lock_irqsave(); + WRITE_ONCE(dev->current_state, state); pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr); + pcie_aspm_l1ss_hw_unlock_irqrestore(flags); /* Mandatory power management transition delays; see PCI PM 1.2. */ if (state == PCI_D3hot) @@ -1559,11 +1612,14 @@ static int pci_set_low_power_state(struct pci_dev *dev, pci_power_t state, bool udelay(PCI_PM_D2_DELAY); pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr); - dev->current_state = pmcsr & PCI_PM_CTRL_STATE_MASK; - if (dev->current_state != state) + WRITE_ONCE(dev->current_state, pmcsr & PCI_PM_CTRL_STATE_MASK); + if (dev->current_state != state) { pci_info_ratelimited(dev, "Refused to change power state from %s to %s\n", pci_power_name(dev->current_state), pci_power_name(state)); + if (dev->current_state == PCI_D0) + pci_pm_notify_state_change(dev, locked); + } return 0; } @@ -1683,6 +1739,7 @@ static int pci_save_pcie_state(struct pci_dev *dev) { int i = 0; struct pci_cap_saved_state *save_state; + unsigned long flags; u16 *cap; if (!pci_is_pcie(dev)) @@ -1696,14 +1753,21 @@ static int pci_save_pcie_state(struct pci_dev *dev) cap = (u16 *)&save_state->cap.data[0]; pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &cap[i++]); + /* + * Snapshot LNKCTL and L1SS together under aspm_l1ss_hw_lock so a + * concurrent L1SS programming sequence cannot expose transient + * ASPM-disabled LNKCTL bits. + */ + flags = pcie_aspm_l1ss_hw_lock_irqsave(); pcie_capability_read_word(dev, PCI_EXP_LNKCTL, &cap[i++]); + pci_save_aspm_l1ss_state_locked(dev); + pcie_aspm_l1ss_hw_unlock_irqrestore(flags); pcie_capability_read_word(dev, PCI_EXP_SLTCTL, &cap[i++]); pcie_capability_read_word(dev, PCI_EXP_RTCTL, &cap[i++]); pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &cap[i++]); pcie_capability_read_word(dev, PCI_EXP_LNKCTL2, &cap[i++]); pcie_capability_read_word(dev, PCI_EXP_SLTCTL2, &cap[i++]); - pci_save_aspm_l1ss_state(dev); pci_save_ltr_state(dev); return 0; @@ -1713,6 +1777,7 @@ static void pci_restore_pcie_state(struct pci_dev *dev) { int i = 0; struct pci_cap_saved_state *save_state; + unsigned long flags; u16 *cap; /* @@ -1720,11 +1785,19 @@ static void pci_restore_pcie_state(struct pci_dev *dev) * LTR itself in PCI_EXP_DEVCTL2. */ pci_restore_ltr_state(dev); - pci_restore_aspm_l1ss_state(dev); + + /* + * Serialize the complete L1SS + LNKCTL restore sequence against + * concurrent ASPM / L1SS programming in both CONFIG_PCIEASPM=y and =n. + */ + flags = pcie_aspm_l1ss_hw_lock_irqsave(); + pci_restore_aspm_l1ss_state_locked(dev); save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP); - if (!save_state) + if (!save_state) { + pcie_aspm_l1ss_hw_unlock_irqrestore(flags); return; + } /* * Downstream ports reset the LTR enable bit when link goes down. @@ -1736,6 +1809,7 @@ static void pci_restore_pcie_state(struct pci_dev *dev) cap = (u16 *)&save_state->cap.data[0]; pcie_capability_write_word(dev, PCI_EXP_DEVCTL, cap[i++]); pcie_capability_write_word(dev, PCI_EXP_LNKCTL, cap[i++]); + pcie_aspm_l1ss_hw_unlock_irqrestore(flags); pcie_capability_write_word(dev, PCI_EXP_SLTCTL, cap[i++]); pcie_capability_write_word(dev, PCI_EXP_RTCTL, cap[i++]); pcie_capability_write_word(dev, PCI_EXP_DEVCTL2, cap[i++]); @@ -4542,6 +4616,7 @@ static int pci_af_flr(struct pci_dev *dev, bool probe) */ static int pci_pm_reset(struct pci_dev *dev, bool probe) { + unsigned long flags; u16 csr; int ret; @@ -4564,9 +4639,16 @@ static int pci_pm_reset(struct pci_dev *dev, bool probe) return ret; } + /* + * Serialize D3hot entry with aspm_l1ss_hw_lock so concurrent L1SS + * restores cannot enable PCI-PM substates while the device is in D3hot. + */ + flags = pcie_aspm_l1ss_hw_lock_irqsave(); + WRITE_ONCE(dev->current_state, PCI_D3hot); csr &= ~PCI_PM_CTRL_STATE_MASK; csr |= PCI_D3hot; pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr); + pcie_aspm_l1ss_hw_unlock_irqrestore(flags); pci_dev_d3_sleep(dev); csr &= ~PCI_PM_CTRL_STATE_MASK; @@ -4576,6 +4658,12 @@ static int pci_pm_reset(struct pci_dev *dev, bool probe) ret = pci_dev_wait(dev, "PM D3hot->D0", PCIE_RESET_READY_POLL_MS); pci_dev_reset_iommu_done(dev); + if (!ret) { + flags = pcie_aspm_l1ss_hw_lock_irqsave(); + WRITE_ONCE(dev->current_state, PCI_D0); + pcie_aspm_l1ss_hw_unlock_irqrestore(flags); + pci_pm_notify_resume(dev); + } return ret; } diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index fbddbf9ef365f..a353cfc217508 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -278,6 +278,8 @@ void pci_dev_complete_resume(struct pci_dev *pci_dev); void pci_config_pm_runtime_get(struct pci_dev *dev); void pci_config_pm_runtime_put(struct pci_dev *dev); void pci_pm_power_up_and_verify_state(struct pci_dev *pci_dev); +void pci_pm_notify_state_change(struct pci_dev *dev, bool locked); +void pci_pm_notify_resume(struct pci_dev *dev); void pci_pm_init(struct pci_dev *dev); void pci_ea_init(struct pci_dev *dev); bool pci_ea_fixed_busnrs(struct pci_dev *dev, u8 *sec, u8 *sub); @@ -1133,22 +1135,30 @@ void pci_save_ltr_state(struct pci_dev *dev); void pci_restore_ltr_state(struct pci_dev *dev); void pci_configure_aspm_l1ss(struct pci_dev *dev); void pci_save_aspm_l1ss_state(struct pci_dev *dev); +void pci_save_aspm_l1ss_state_locked(struct pci_dev *dev); void pci_restore_aspm_l1ss_state(struct pci_dev *dev); +void pci_restore_aspm_l1ss_state_locked(struct pci_dev *dev); +unsigned long pcie_aspm_l1ss_hw_lock_irqsave(void); +void pcie_aspm_l1ss_hw_unlock_irqrestore(unsigned long flags); #ifdef CONFIG_PCIEASPM void pcie_aspm_remove_cap(struct pci_dev *pdev, u32 lnkcap); +void pcie_aspm_init_dev(struct pci_dev *pdev); void pcie_aspm_init_link_state(struct pci_dev *pdev); void pcie_aspm_exit_link_state(struct pci_dev *pdev); void pcie_aspm_pm_state_change(struct pci_dev *pdev, bool locked); +void pcie_aspm_pm_state_change_deferred(struct pci_dev *pdev); void pcie_aspm_powersave_config_link(struct pci_dev *pdev); void pci_configure_ltr(struct pci_dev *pdev); void pci_bridge_reconfigure_ltr(struct pci_dev *pdev); void pcie_encode_t_power_on(u32 t_power_on_us, u8 *scale, u8 *value); #else static inline void pcie_aspm_remove_cap(struct pci_dev *pdev, u32 lnkcap) { } +static inline void pcie_aspm_init_dev(struct pci_dev *pdev) { } static inline void pcie_aspm_init_link_state(struct pci_dev *pdev) { } static inline void pcie_aspm_exit_link_state(struct pci_dev *pdev) { } static inline void pcie_aspm_pm_state_change(struct pci_dev *pdev, bool locked) { } +static inline void pcie_aspm_pm_state_change_deferred(struct pci_dev *pdev) { } static inline void pcie_aspm_powersave_config_link(struct pci_dev *pdev) { } static inline void pci_configure_ltr(struct pci_dev *pdev) { } static inline void pci_bridge_reconfigure_ltr(struct pci_dev *pdev) { } diff --git a/drivers/pci/pcie/aspm.c b/drivers/pci/pcie/aspm.c index 417d5b4586839..8e8eaa1370c87 100644 --- a/drivers/pci/pcie/aspm.c +++ b/drivers/pci/pcie/aspm.c @@ -23,7 +23,9 @@ #include #include #include +#include #include +#include #include "../pci.h" @@ -80,10 +82,53 @@ void pci_configure_aspm_l1ss(struct pci_dev *pdev) ERR_PTR(rc)); } -void pci_save_aspm_l1ss_state(struct pci_dev *pdev) +/* + * Atomic-safe leaf lock serializing L1SS/LNKCTL/PMCSR hardware transactions + * and saved-state updates across CONFIG_PCIEASPM=y and CONFIG_PCIEASPM=n, + * including callers such as pci_save_state()/pci_restore_state() that may run + * with interrupts disabled. + * + * Lock order: pci_rescan_remove_lock -> pci_bus_sem -> aspm_lock -> + * driver lock -> aspm_l1ss_hw_lock -> pci_lock. + */ +static DEFINE_SPINLOCK(aspm_l1ss_hw_lock); + +unsigned long pcie_aspm_l1ss_hw_lock_irqsave(void) +{ + unsigned long flags; + + spin_lock_irqsave(&aspm_l1ss_hw_lock, flags); + return flags; +} + +void pcie_aspm_l1ss_hw_unlock_irqrestore(unsigned long flags) +{ + spin_unlock_irqrestore(&aspm_l1ss_hw_lock, flags); +} + +#ifdef CONFIG_PCIEASPM +static DEFINE_MUTEX(aspm_lock); + +static void pcie_aspm_restore_l1ss_mask(struct pci_dev *parent, + struct pci_dev *pdev, + u32 *pl_ctl1, u32 *cl_ctl1); +static void pcie_aspm_restore_l1ss_sync(struct pci_dev *parent, + struct pci_dev *pdev, + u32 pl_ctl1, u32 cl_ctl1); +#else +static inline void pcie_aspm_restore_l1ss_mask(struct pci_dev *parent, + struct pci_dev *pdev, + u32 *pl_ctl1, u32 *cl_ctl1) { } +static inline void pcie_aspm_restore_l1ss_sync(struct pci_dev *parent, + struct pci_dev *pdev, + u32 pl_ctl1, u32 cl_ctl1) { } +#endif + +void pci_save_aspm_l1ss_state_locked(struct pci_dev *pdev) { struct pci_dev *parent = pdev->bus->self; - struct pci_cap_saved_state *save_state; + struct pci_cap_saved_state *cl_save_state, *pl_save_state; + u32 cl_ctl2, cl_ctl1, pl_ctl2, pl_ctl1; u32 *cap; /* @@ -101,28 +146,51 @@ void pci_save_aspm_l1ss_state(struct pci_dev *pdev) * Save L1 substate configuration. The ASPM L0s/L1 configuration * in PCI_EXP_LNKCTL_ASPMC is saved by pci_save_pcie_state(). */ - save_state = pci_find_saved_ext_cap(pdev, PCI_EXT_CAP_ID_L1SS); - if (!save_state) + cl_save_state = pci_find_saved_ext_cap(pdev, PCI_EXT_CAP_ID_L1SS); + pl_save_state = pci_find_saved_ext_cap(parent, PCI_EXT_CAP_ID_L1SS); + if (!cl_save_state || !pl_save_state) return; - cap = &save_state->cap.data[0]; - pci_read_config_dword(pdev, pdev->l1ss + PCI_L1SS_CTL2, cap++); - pci_read_config_dword(pdev, pdev->l1ss + PCI_L1SS_CTL1, cap++); + /* + * Validate all four reads before committing so an inaccessible D3cold + * or failing link cannot overwrite valid timing with 0xffffffff. + */ + if (pci_read_config_dword(pdev, pdev->l1ss + PCI_L1SS_CTL2, &cl_ctl2) || + pci_read_config_dword(pdev, pdev->l1ss + PCI_L1SS_CTL1, &cl_ctl1) || + pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL2, &pl_ctl2) || + pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1, &pl_ctl1) || + PCI_POSSIBLE_ERROR(cl_ctl2) || PCI_POSSIBLE_ERROR(cl_ctl1) || + PCI_POSSIBLE_ERROR(pl_ctl2) || PCI_POSSIBLE_ERROR(pl_ctl1)) + return; + + cap = &cl_save_state->cap.data[0]; + *cap++ = cl_ctl2; + *cap = cl_ctl1; /* * Save parent's L1 substate configuration so we have it for * pci_restore_aspm_l1ss_state(pdev) to restore. */ - save_state = pci_find_saved_ext_cap(parent, PCI_EXT_CAP_ID_L1SS); - if (!save_state) + cap = &pl_save_state->cap.data[0]; + *cap++ = pl_ctl2; + *cap = pl_ctl1; +} + +void pci_save_aspm_l1ss_state(struct pci_dev *pdev) +{ + struct pci_dev *parent = pdev->bus->self; + unsigned long flags; + + if (pcie_downstream_port(pdev) || !parent || !pdev->l1ss || !parent->l1ss) return; - cap = &save_state->cap.data[0]; - pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL2, cap++); - pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1, cap++); + spin_lock_irqsave(&aspm_l1ss_hw_lock, flags); + pci_save_aspm_l1ss_state_locked(pdev); + spin_unlock_irqrestore(&aspm_l1ss_hw_lock, flags); } +EXPORT_SYMBOL_GPL(pci_save_aspm_l1ss_state); -void pci_restore_aspm_l1ss_state(struct pci_dev *pdev) +void pci_restore_aspm_l1ss_state_locked(struct pci_dev *pdev) { struct pci_cap_saved_state *pl_save_state, *cl_save_state; struct pci_dev *parent = pdev->bus->self; @@ -153,6 +221,8 @@ void pci_restore_aspm_l1ss_state(struct pci_dev *pdev) pl_ctl2 = *cap++; pl_ctl1 = *cap; + pcie_aspm_restore_l1ss_mask(parent, pdev, &pl_ctl1, &cl_ctl1); + /* Make sure L0s/L1 are disabled before updating L1SS config */ pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &clnkctl); pcie_capability_read_word(parent, PCI_EXP_LNKCTL, &plnkctl); @@ -203,6 +273,22 @@ void pci_restore_aspm_l1ss_state(struct pci_dev *pdev) pcie_capability_write_word(parent, PCI_EXP_LNKCTL, plnkctl); pcie_capability_write_word(pdev, PCI_EXP_LNKCTL, clnkctl); } + + pcie_aspm_restore_l1ss_sync(parent, pdev, pl_ctl1 | pl_l1_2_enable, + cl_ctl1 | cl_l1_2_enable); +} + +void pci_restore_aspm_l1ss_state(struct pci_dev *pdev) +{ + struct pci_dev *parent = pdev->bus->self; + unsigned long flags; + + if (pcie_downstream_port(pdev) || !parent || !pdev->l1ss || !parent->l1ss) + return; + + spin_lock_irqsave(&aspm_l1ss_hw_lock, flags); + pci_restore_aspm_l1ss_state_locked(pdev); + spin_unlock_irqrestore(&aspm_l1ss_hw_lock, flags); } #ifdef CONFIG_PCIEASPM @@ -245,11 +331,17 @@ struct pcie_link_state { u32 clkpm_enabled:1; /* Current Clock PM state */ u32 clkpm_default:1; /* Default Clock PM state by BIOS */ u32 clkpm_disable:1; /* Clock PM disabled */ + + /* + * Separate bytes outside the aspm_lock bitfield word so atomic restore + * under aspm_l1ss_hw_lock never shares RMW storage with aspm_capable. + */ + u8 aspm_sleep_disable; /* Temporarily disabled during sleep */ + u8 aspm_hw_l1ss; /* Policy-programmed L1SS shadow */ }; static bool aspm_disabled, aspm_force; static bool aspm_support_enabled = true; -static DEFINE_MUTEX(aspm_lock); static LIST_HEAD(link_list); #define POLICY_DEFAULT 0 /* BIOS default setting */ @@ -839,11 +931,30 @@ static void aspm_l1ss_init(struct pcie_link_state *link) #define FLAG(x, y, d) (((x) & (PCIE_LINK_STATE_##y)) ? d : "") +static int pcie_l1ss_ctl1_update(struct pci_dev *pdev, u32 clear, u32 set) +{ + u32 val; + int ret; + + ret = pcibios_err_to_errno(pci_read_config_dword(pdev, + pdev->l1ss + PCI_L1SS_CTL1, + &val)); + if (ret) + return ret; + if (PCI_POSSIBLE_ERROR(val)) + return -EIO; + val = (val & ~clear) | set; + return pcibios_err_to_errno(pci_write_config_dword(pdev, + pdev->l1ss + PCI_L1SS_CTL1, + val)); +} + /* Configure the ASPM L1 substates. Caller must disable L1 first. */ -static void pcie_config_aspm_l1ss(struct pcie_link_state *link, u32 state) +static int pcie_config_aspm_l1ss(struct pcie_link_state *link, u32 state) { u32 val = 0; struct pci_dev *child = link->downstream, *parent = link->pdev; + int ret; if (state & PCIE_LINK_STATE_L1_1) val |= PCI_L1SS_CTL1_ASPM_L1_1; @@ -863,16 +974,28 @@ static void pcie_config_aspm_l1ss(struct pcie_link_state *link, u32 state) */ /* Disable all L1 substates */ - pci_clear_and_set_config_dword(child, child->l1ss + PCI_L1SS_CTL1, - PCI_L1SS_CTL1_L1SS_MASK, 0); - pci_clear_and_set_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1, - PCI_L1SS_CTL1_L1SS_MASK, 0); + ret = pcie_l1ss_ctl1_update(child, PCI_L1SS_CTL1_L1SS_MASK, 0); + if (ret) + goto unwind; + ret = pcie_l1ss_ctl1_update(parent, PCI_L1SS_CTL1_L1SS_MASK, 0); + if (ret) + goto unwind; /* Enable what we need to enable */ - pci_clear_and_set_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1, - PCI_L1SS_CTL1_L1SS_MASK, val); - pci_clear_and_set_config_dword(child, child->l1ss + PCI_L1SS_CTL1, - PCI_L1SS_CTL1_L1SS_MASK, val); + ret = pcie_l1ss_ctl1_update(parent, PCI_L1SS_CTL1_L1SS_MASK, val); + if (ret) + goto unwind; + ret = pcie_l1ss_ctl1_update(child, PCI_L1SS_CTL1_L1SS_MASK, val); + if (ret) + goto unwind; + + return 0; + +unwind: + /* Keep both ends symmetrically disabled child-first on partial failure. */ + pcie_l1ss_ctl1_update(child, PCI_L1SS_CTL1_L1SS_MASK, 0); + pcie_l1ss_ctl1_update(parent, PCI_L1SS_CTL1_L1SS_MASK, 0); + return ret; } static bool pcie_link_has_aspm_override(const struct pcie_link_state *link, @@ -1030,6 +1153,7 @@ static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist) link->aspm_default = link->aspm_enabled; pcie_aspm_override_default_link_state(link); + WRITE_ONCE(link->aspm_hw_l1ss, link->aspm_enabled & PCIE_LINK_STATE_L1SS); /* * Restore L0s/L1 if they were enabled, but don't restore any @@ -1067,34 +1191,144 @@ static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist) } } -static void pcie_config_aspm_dev(struct pci_dev *pdev, u32 val) +static int pcie_config_aspm_dev(struct pci_dev *pdev, u32 val) { - pcie_capability_clear_and_set_word(pdev, PCI_EXP_LNKCTL, - PCI_EXP_LNKCTL_ASPMC, val); + return pcibios_err_to_errno(pcie_capability_clear_and_set_word(pdev, + PCI_EXP_LNKCTL, PCI_EXP_LNKCTL_ASPMC, + val)); } -static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state) +static u32 pcie_aspm_l1ss_ctl1_to_state(u32 ctl1) +{ + u32 state = 0; + + if (ctl1 & PCI_L1SS_CTL1_ASPM_L1_1) + state |= PCIE_LINK_STATE_L1_1; + if (ctl1 & PCI_L1SS_CTL1_ASPM_L1_2) + state |= PCIE_LINK_STATE_L1_2; + if (ctl1 & PCI_L1SS_CTL1_PCIPM_L1_1) + state |= PCIE_LINK_STATE_L1_1_PCIPM; + if (ctl1 & PCI_L1SS_CTL1_PCIPM_L1_2) + state |= PCIE_LINK_STATE_L1_2_PCIPM; + return state; +} + +static void pcie_aspm_restore_l1ss_mask(struct pci_dev *parent, + struct pci_dev *pdev, + u32 *pl_ctl1, u32 *cl_ctl1) { - u32 upstream = 0, dwstream = 0; + struct pcie_link_state *link = parent->link_state; + + if (aspm_disabled || !link || link->downstream != pdev) + return; + + if (READ_ONCE(link->aspm_sleep_disable) & PCIE_LINK_STATE_L1SS) { + *pl_ctl1 &= ~PCI_L1SS_CTL1_L1SS_MASK; + *cl_ctl1 &= ~PCI_L1SS_CTL1_L1SS_MASK; + return; + } + + if (READ_ONCE(parent->current_state) != PCI_D0 || + READ_ONCE(pdev->current_state) != PCI_D0) { + *pl_ctl1 &= ~(PCI_L1SS_CTL1_PCIPM_L1_1 | PCI_L1SS_CTL1_PCIPM_L1_2); + *cl_ctl1 &= ~(PCI_L1SS_CTL1_PCIPM_L1_1 | PCI_L1SS_CTL1_PCIPM_L1_2); + } +} + +static void pcie_aspm_restore_l1ss_sync(struct pci_dev *parent, + struct pci_dev *pdev, + u32 pl_ctl1, u32 cl_ctl1) +{ + struct pcie_link_state *link = parent->link_state; + u8 pl_en, cl_en, expected; + + if (aspm_disabled || !link || link->downstream != pdev) + return; + + /* + * Defer cache/policy reconciliation out of the atomic restore path so + * link->aspm_enabled (which shares bitfield storage with aspm_capable + * under aspm_lock) is never mutated without aspm_lock. Compare both + * ends against the policy-programmed shadow byte so both missing and + * stale-enabled substates trigger forced reprogramming. + */ + pl_en = pcie_aspm_l1ss_ctl1_to_state(pl_ctl1); + cl_en = pcie_aspm_l1ss_ctl1_to_state(cl_ctl1); + expected = READ_ONCE(link->aspm_hw_l1ss); + 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)) { + WRITE_ONCE(parent->aspm_pcipm_deferred, 1); + /* + * Pairs with smp_mb() in + * pcie_aspm_pm_state_change_deferred(). + */ + smp_mb(); + if (READ_ONCE(parent->current_state) == PCI_D0 && + READ_ONCE(pdev->current_state) == PCI_D0) + pcie_aspm_pm_state_change_deferred(parent); + } + } +} + +static int pcie_config_aspm_link_locked(struct pcie_link_state *link, u32 state) +{ + u32 upstream = 0, dwstream = 0, wanted; struct pci_dev *child = link->downstream, *parent = link->pdev; struct pci_bus *linkbus = parent->subordinate; + bool resync = READ_ONCE(parent->aspm_l1ss_resync); + bool l1ss_programmed = false; + int ret = 0, r; + + if (resync) + WRITE_ONCE(parent->aspm_l1ss_resync, 0); /* Enable only the states that were not explicitly disabled */ - state &= (link->aspm_capable & ~link->aspm_disable); + state &= (link->aspm_capable & ~link->aspm_disable & + ~READ_ONCE(link->aspm_sleep_disable)); /* Can't enable any substates if L1 is not enabled */ if (!(state & PCIE_LINK_STATE_L1)) state &= ~PCIE_LINK_STATE_L1SS; + wanted = state; + /* Spec says both ports must be in D0 before enabling PCI PM substates*/ - if (parent->current_state != PCI_D0 || child->current_state != PCI_D0) { - state &= ~PCIE_LINK_STATE_L1_SS_PCIPM; - state |= (link->aspm_enabled & PCIE_LINK_STATE_L1_SS_PCIPM); + if (READ_ONCE(parent->current_state) != PCI_D0 || + READ_ONCE(child->current_state) != PCI_D0) { + u32 deferred_state; + + if (!resync && link->aspm_enabled == state) { + WRITE_ONCE(parent->aspm_pcipm_deferred, 0); + return 0; + } + deferred_state = state & ~PCIE_LINK_STATE_L1_SS_PCIPM; + if (wanted & PCIE_LINK_STATE_L1_SS_PCIPM) { + WRITE_ONCE(parent->aspm_pcipm_deferred, 1); + /* + * Order publishing aspm_pcipm_deferred before re-checking + * current_state; pairs with smp_mb() in + * pcie_aspm_pm_state_change_deferred(). + */ + smp_mb(); + if (READ_ONCE(parent->current_state) == PCI_D0 && + READ_ONCE(child->current_state) == PCI_D0) { + WRITE_ONCE(parent->aspm_pcipm_deferred, 0); + } else { + state = deferred_state; + } + } else { + WRITE_ONCE(parent->aspm_pcipm_deferred, 0); + state = deferred_state; + } + } else { + WRITE_ONCE(parent->aspm_pcipm_deferred, 0); } - /* Nothing to do if the link is already in the requested state */ - if (link->aspm_enabled == state) - return; + /* Nothing to do if hardware and cache already match the requested state */ + if (!resync && link->aspm_enabled == state) + return 0; /* Convert ASPM state to upstream/downstream ASPM register state */ if (state & PCIE_LINK_STATE_L0S_UP) dwstream |= PCI_EXP_LNKCTL_ASPM_L0S; @@ -1119,24 +1353,74 @@ static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state) * Sec 7.5.3.7 also recommends programming the same ASPM Control * value for all functions of a multi-function device. */ - list_for_each_entry(child, &linkbus->devices, bus_list) - pcie_config_aspm_dev(child, 0); - pcie_config_aspm_dev(parent, 0); + list_for_each_entry(child, &linkbus->devices, bus_list) { + r = pcie_config_aspm_dev(child, 0); + if (!ret) + ret = r; + } + r = pcie_config_aspm_dev(parent, 0); + if (!ret) + ret = r; + + if (!ret && (link->aspm_capable & PCIE_LINK_STATE_L1SS)) { + ret = pcie_config_aspm_l1ss(link, state); + if (!ret && (state & PCIE_LINK_STATE_L1SS)) + l1ss_programmed = true; + } - if (link->aspm_capable & PCIE_LINK_STATE_L1SS) - pcie_config_aspm_l1ss(link, state); + r = pcie_config_aspm_dev(parent, upstream); + if (r) + r = pcie_config_aspm_dev(parent, upstream); + if (!ret) + ret = r; + list_for_each_entry(child, &linkbus->devices, bus_list) { + r = pcie_config_aspm_dev(child, dwstream); + if (r) + r = pcie_config_aspm_dev(child, dwstream); + if (!ret) + ret = r; + } - pcie_config_aspm_dev(parent, upstream); - list_for_each_entry(child, &linkbus->devices, bus_list) - pcie_config_aspm_dev(child, dwstream); + if (ret) { + if (l1ss_programmed) { + list_for_each_entry(child, &linkbus->devices, bus_list) + pcie_config_aspm_dev(child, 0); + pcie_config_aspm_dev(parent, 0); + pcie_l1ss_ctl1_update(link->downstream, + PCI_L1SS_CTL1_L1SS_MASK, 0); + pcie_l1ss_ctl1_update(parent, + PCI_L1SS_CTL1_L1SS_MASK, 0); + pcie_config_aspm_dev(parent, upstream); + list_for_each_entry(child, &linkbus->devices, bus_list) + pcie_config_aspm_dev(child, dwstream); + } + link->aspm_enabled &= ~PCIE_LINK_STATE_L1SS; + 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)) + WRITE_ONCE(parent->aspm_pcipm_deferred, 1); + return ret; + } link->aspm_enabled = state; + WRITE_ONCE(link->aspm_hw_l1ss, state & PCIE_LINK_STATE_L1SS); /* Update latest ASPM configuration in saved context */ - pci_save_aspm_l1ss_state(link->downstream); + pci_save_aspm_l1ss_state_locked(link->downstream); pci_update_aspm_saved_state(link->downstream); - pci_save_aspm_l1ss_state(parent); + pci_save_aspm_l1ss_state_locked(parent); pci_update_aspm_saved_state(parent); + return 0; +} + +static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state) +{ + unsigned long flags; + + spin_lock_irqsave(&aspm_l1ss_hw_lock, flags); + pcie_config_aspm_link_locked(link, state); + spin_unlock_irqrestore(&aspm_l1ss_hw_lock, flags); } static void pcie_config_aspm_path(struct pcie_link_state *link) @@ -1149,7 +1433,13 @@ static void pcie_config_aspm_path(struct pcie_link_state *link) static void free_link_state(struct pcie_link_state *link) { + unsigned long flags; + + spin_lock_irqsave(&aspm_l1ss_hw_lock, flags); + WRITE_ONCE(link->pdev->aspm_l1ss_resync, 0); + WRITE_ONCE(link->pdev->aspm_pcipm_deferred, 0); link->pdev->link_state = NULL; + spin_unlock_irqrestore(&aspm_l1ss_hw_lock, flags); kfree(link); } @@ -1451,9 +1741,10 @@ void pcie_aspm_exit_link_state(struct pci_dev *pdev) */ void pcie_aspm_pm_state_change(struct pci_dev *pdev, bool locked) { - struct pcie_link_state *link = pdev->link_state; + struct pcie_link_state *link; - if (aspm_disabled || !link) + /* Unlocked hint only; pdev->link_state is validated under aspm_lock. */ + if (aspm_disabled || !READ_ONCE(pdev->link_state)) return; /* * Devices changed PM state, we should recheck if latency @@ -1462,13 +1753,57 @@ void pcie_aspm_pm_state_change(struct pci_dev *pdev, bool locked) if (!locked) down_read(&pci_bus_sem); mutex_lock(&aspm_lock); - pcie_update_aspm_capable(link->root); - pcie_config_aspm_path(link); + /* free_link_state() clears pdev->link_state under aspm_lock. */ + link = pdev->link_state; + if (link) { + pcie_update_aspm_capable(link->root); + pcie_config_aspm_path(link); + } mutex_unlock(&aspm_lock); if (!locked) up_read(&pci_bus_sem); } +static void pcie_aspm_pm_work_fn(struct work_struct *work) +{ + struct pci_dev *pdev = container_of(work, struct pci_dev, aspm_pm_work); + + /* + * Runs on system_freezable_wq with no PCI lock held. @pdev is pinned by + * the queueing caller and pcie_aspm_pm_state_change() revalidates + * pdev->link_state under aspm_lock, so a link freed meanwhile is + * skipped safely without cancel_work_sync() under PCI locks. + */ + pcie_aspm_pm_state_change(pdev, false); + pci_dev_put(pdev); +} + +void pcie_aspm_init_dev(struct pci_dev *pdev) +{ + INIT_WORK(&pdev->aspm_pm_work, pcie_aspm_pm_work_fn); +} + +/** + * pcie_aspm_pm_state_change_deferred - Recheck ASPM from a freezable work item + * @pdev: the root port or switch downstream port + * + * For callers that may run with pci_bus_sem held for read, such as runtime + * resume reached from pci_walk_bus() in error recovery. Only queues work when + * the port has deferred PCI-PM L1SS waiting for D0; work is preallocated in + * struct pci_dev and coalesced per port on system_freezable_wq. + */ +void pcie_aspm_pm_state_change_deferred(struct pci_dev *pdev) +{ + /* Order dev->current_state = PCI_D0 before checking aspm_pcipm_deferred. */ + smp_mb(); + if (aspm_disabled || !READ_ONCE(pdev->aspm_pcipm_deferred)) + return; + + pci_dev_get(pdev); + if (!queue_work(system_freezable_wq, &pdev->aspm_pm_work)) + pci_dev_put(pdev); +} + void pcie_aspm_powersave_config_link(struct pci_dev *pdev) { struct pcie_link_state *link = pdev->link_state; @@ -1908,3 +2243,191 @@ bool pcie_aspm_support_enabled(void) } #endif /* CONFIG_PCIEASPM */ + +int pcie_aspm_l1ss_hw_exec(struct pci_dev *child, bool save_state, + int (*fn)(void *data), void *data) +{ + unsigned long flags; + int ret; + + spin_lock_irqsave(&aspm_l1ss_hw_lock, flags); + ret = fn(data); + if (!ret && save_state && child) + pci_save_aspm_l1ss_state_locked(child); + spin_unlock_irqrestore(&aspm_l1ss_hw_lock, flags); + + return ret; +} +EXPORT_SYMBOL_GPL(pcie_aspm_l1ss_hw_exec); + +/** + * pcie_aspm_l1ss_sleep_begin_locked - Serialize sleep L1SS teardown with ASPM + * @pdev: Upstream port (Root Port or Switch Downstream Port) of the link + * @child: Downstream function holding the link's L1SS capability, or NULL + * @disable_fn: Driver callback that clears hardware L1SS enables + * @data: Opaque argument passed to @disable_fn + * + * Serializes hardware L1SS teardown with aspm_lock (when CONFIG_PCIEASPM is + * enabled), records temporary sleep suppression without altering + * link->aspm_disable or link->aspm_default, and keeps link->aspm_enabled and + * saved L1SS state in sync across suspend/resume in both CONFIG_PCIEASPM=y + * and CONFIG_PCIEASPM=n builds. + * + * Context: Caller holds pci_bus_sem read lock. + */ +int pcie_aspm_l1ss_sleep_begin_locked(struct pci_dev *pdev, + struct pci_dev *child, + int (*disable_fn)(void *data, + bool core_managed), + void *data) +{ + bool core_managed = false; + unsigned long flags; + int ret; +#ifdef CONFIG_PCIEASPM + struct pcie_link_state *link; + bool was_suppressed = false; +#endif + + lockdep_assert_held_read(&pci_bus_sem); + +#ifdef CONFIG_PCIEASPM + mutex_lock(&aspm_lock); + link = pdev->link_state; + core_managed = !aspm_disabled && link && link->downstream; + if (core_managed) { + spin_lock_irqsave(&aspm_l1ss_hw_lock, flags); + was_suppressed = !!(READ_ONCE(link->aspm_sleep_disable) & + PCIE_LINK_STATE_L1SS); + WRITE_ONCE(link->aspm_sleep_disable, + READ_ONCE(link->aspm_sleep_disable) | + PCIE_LINK_STATE_L1SS); + spin_unlock_irqrestore(&aspm_l1ss_hw_lock, flags); + } +#endif + ret = disable_fn(data, core_managed); + if (!ret) { + spin_lock_irqsave(&aspm_l1ss_hw_lock, flags); +#ifdef CONFIG_PCIEASPM + if (core_managed) { + link->aspm_enabled &= ~PCIE_LINK_STATE_L1SS; + WRITE_ONCE(link->aspm_hw_l1ss, 0); + WRITE_ONCE(pdev->aspm_l1ss_resync, 0); + WRITE_ONCE(pdev->aspm_pcipm_deferred, 0); + pci_save_aspm_l1ss_state_locked(link->downstream); + pci_save_aspm_l1ss_state_locked(link->pdev); + } else if (child) { + pci_save_aspm_l1ss_state_locked(child); + } +#else + if (child) + pci_save_aspm_l1ss_state_locked(child); +#endif + spin_unlock_irqrestore(&aspm_l1ss_hw_lock, flags); + } else { +#ifdef CONFIG_PCIEASPM + if (core_managed && !was_suppressed) { + spin_lock_irqsave(&aspm_l1ss_hw_lock, flags); + WRITE_ONCE(link->aspm_sleep_disable, + READ_ONCE(link->aspm_sleep_disable) & + ~PCIE_LINK_STATE_L1SS); + spin_unlock_irqrestore(&aspm_l1ss_hw_lock, flags); + } +#endif + } +#ifdef CONFIG_PCIEASPM + mutex_unlock(&aspm_lock); +#endif + + return ret; +} +EXPORT_SYMBOL_GPL(pcie_aspm_l1ss_sleep_begin_locked); + +/** + * pcie_aspm_l1ss_sleep_end - Restore L1SS after sleep via the PCI core ASPM path + * @pdev: Upstream port (Root Port or Switch Downstream Port) of the link + * @child: Downstream function holding the link's L1SS capability, or NULL + * @restore_fn: Driver callback restoring L1SS timing (and enables if unmanaged) + * @data: Opaque argument passed to @restore_fn + * + * Serializes restoration with pci_bus_sem and aspm_lock (in both + * CONFIG_PCIEASPM=y and CONFIG_PCIEASPM=n). When the link is managed by the + * ASPM core and @restore_fn succeeds, clears temporary sleep suppression and + * lets pcie_config_aspm_link() re-apply policy-permitted L1SS enables, + * deferring PCI-PM L1.1/L1.2 until both ends of the link are in PCI_D0. If + * @restore_fn fails transiently (ret != 0 && ret != -ENODEV), keeps + * link->aspm_sleep_disable armed so policy rechecks cannot enable L1.2 + * against incomplete timing. + */ +int pcie_aspm_l1ss_sleep_end(struct pci_dev *pdev, struct pci_dev *child, + int (*restore_fn)(void *data, bool core_managed), + void *data) +{ + bool core_managed = false; + unsigned long flags; + int ret; +#ifdef CONFIG_PCIEASPM + struct pcie_link_state *link; +#endif + + down_read(&pci_bus_sem); +#ifdef CONFIG_PCIEASPM + mutex_lock(&aspm_lock); + link = pdev->link_state; + core_managed = !aspm_disabled && link && link->downstream; +#endif + ret = restore_fn(data, core_managed); +#ifdef CONFIG_PCIEASPM + if (core_managed) { + spin_lock_irqsave(&aspm_l1ss_hw_lock, flags); + link->aspm_enabled &= ~PCIE_LINK_STATE_L1SS; + WRITE_ONCE(link->aspm_hw_l1ss, 0); + if (!ret) { + WRITE_ONCE(link->aspm_sleep_disable, + READ_ONCE(link->aspm_sleep_disable) & + ~PCIE_LINK_STATE_L1SS); + ret = pcie_config_aspm_link_locked(link, + policy_to_aspm_state(link)); + if (ret) { + if (ret == -ENODEV) + ret = -EIO; + WRITE_ONCE(link->aspm_sleep_disable, + READ_ONCE(link->aspm_sleep_disable) | + PCIE_LINK_STATE_L1SS); + WRITE_ONCE(pdev->aspm_pcipm_deferred, 0); + } + pci_save_aspm_l1ss_state_locked(link->downstream); + pci_save_aspm_l1ss_state_locked(link->pdev); + } else if (ret == -ENODEV) { + WRITE_ONCE(link->aspm_sleep_disable, + READ_ONCE(link->aspm_sleep_disable) & + ~PCIE_LINK_STATE_L1SS); + WRITE_ONCE(pdev->aspm_l1ss_resync, 0); + WRITE_ONCE(pdev->aspm_pcipm_deferred, 0); + } else { + WRITE_ONCE(link->aspm_sleep_disable, + READ_ONCE(link->aspm_sleep_disable) | + PCIE_LINK_STATE_L1SS); + WRITE_ONCE(pdev->aspm_pcipm_deferred, 0); + pci_save_aspm_l1ss_state_locked(link->downstream); + pci_save_aspm_l1ss_state_locked(link->pdev); + } + spin_unlock_irqrestore(&aspm_l1ss_hw_lock, flags); + } else if (!ret && child) { + spin_lock_irqsave(&aspm_l1ss_hw_lock, flags); + pci_save_aspm_l1ss_state_locked(child); + spin_unlock_irqrestore(&aspm_l1ss_hw_lock, flags); + } + mutex_unlock(&aspm_lock); +#else + if (!ret && child) { + spin_lock_irqsave(&aspm_l1ss_hw_lock, flags); + pci_save_aspm_l1ss_state_locked(child); + spin_unlock_irqrestore(&aspm_l1ss_hw_lock, flags); + } +#endif + up_read(&pci_bus_sem); + + return ret; +} +EXPORT_SYMBOL_GPL(pcie_aspm_l1ss_sleep_end); diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c index c38a60df06d81..44f6087e0dab2 100644 --- a/drivers/pci/probe.c +++ b/drivers/pci/probe.c @@ -2517,6 +2517,7 @@ struct pci_dev *pci_alloc_dev(struct pci_bus *bus) }; spin_lock_init(&dev->pcie_cap_lock); + pcie_aspm_init_dev(dev); #ifdef CONFIG_PCI_MSI raw_spin_lock_init(&dev->msi_lock); #endif diff --git a/include/linux/pci.h b/include/linux/pci.h index eb2ccfc21b750..77778ce41ae36 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -438,6 +438,9 @@ struct pci_dev { u16 l1ss; /* L1SS Capability pointer */ #ifdef CONFIG_PCIEASPM struct pcie_link_state *link_state; /* ASPM link state */ + struct work_struct aspm_pm_work; /* Deferred D0 ASPM recheck */ + u8 aspm_pcipm_deferred; /* PCI-PM L1SS awaiting D0 */ + u8 aspm_l1ss_resync; /* Hardware L1SS changed in atomic restore */ unsigned int aspm_l0s_support:1; /* ASPM L0s support */ unsigned int aspm_l1_support:1; /* ASPM L1 support */ unsigned int ltr_path:1; /* Latency Tolerance Reporting @@ -654,6 +657,12 @@ struct pci_host_bridge { int (*enable_device)(struct pci_host_bridge *bridge, struct pci_dev *dev); void (*disable_device)(struct pci_host_bridge *bridge, struct pci_dev *dev); int (*reset_root_port)(struct pci_host_bridge *bridge, struct pci_dev *dev); + /* + * Called when a device reaches PCI_D0. May be invoked with pci_bus_sem + * held or from PM resume paths, so implementations must not take PCI + * core locks inline (schedule work instead). + */ + void (*pm_state_change)(struct pci_host_bridge *bridge, struct pci_dev *dev); void *release_data; unsigned int ignore_reset_delay:1; /* For entire hierarchy */ unsigned int no_ext_tags:1; /* No Extended Tags */ @@ -1978,6 +1987,17 @@ static inline bool pcie_aspm_support_enabled(void) { return false; } static inline bool pcie_aspm_enabled(struct pci_dev *pdev) { return false; } #endif +int pcie_aspm_l1ss_sleep_begin_locked(struct pci_dev *pdev, + struct pci_dev *child, + int (*disable_fn)(void *data, + bool core_managed), + void *data); +int pcie_aspm_l1ss_sleep_end(struct pci_dev *pdev, struct pci_dev *child, + int (*restore_fn)(void *data, bool core_managed), + void *data); +int pcie_aspm_l1ss_hw_exec(struct pci_dev *child, bool save_state, + int (*fn)(void *data), void *data); + #ifdef CONFIG_HOTPLUG_PCI void pci_hp_ignore_link_change(struct pci_dev *pdev); void pci_hp_unignore_link_change(struct pci_dev *pdev); diff --git a/tools/asahi/pciec-policy-tests.py b/tools/asahi/pciec-policy-tests.py index c253f04eff9ed..5f6cd5c4c8717 100644 --- a/tools/asahi/pciec-policy-tests.py +++ b/tools/asahi/pciec-policy-tests.py @@ -11,7 +11,7 @@ def function(source, name): - start = re.search(r'^(?:static\s+)?(?:bool|int|u16|void)\s+' + name + r'\([^{;]*\)\s*\{', + start = re.search(r'^(?:static\s+)?(?:bool|int|u16|u32|unsigned long|void)\s+' + name + r'\([^{;]*\)\s*\{', source, re.M).start() end = source.index('{', start) + 1 depth = 1 @@ -158,6 +158,10 @@ def function(source, name): #define U16_MAX ((u16)~0U) #define U32_MAX ((u32)~0U) #define READ_ONCE(x) (x) +#define WRITE_ONCE(x, val) ((x) = (val)) +#define smp_mb() ((void)0) +#define smp_mb__after_atomic() ((void)0) +#define CONFIG_PCIEASPM 1 #define container_of(ptr, type, member) \ ((type *)((char *)(ptr) - offsetof(type, member))) #define kzalloc_obj(x) calloc(1, sizeof(x)) @@ -170,7 +174,23 @@ def function(source, name): #define PM_SUSPEND_TO_IDLE 1 #define PM_SUSPEND_MEM 3 #define PCI_VENDOR_ID 0x00 +#define PCI_PM_CTRL 4 +#define PCI_PM_CTRL_STATE_MASK 0x0003 +#define PCI_PM_CTRL_NO_SOFT_RESET 0x0008 +#define PCI_DEV_FLAGS_NO_PM_RESET (1U << 7) +#define PCIE_RESET_READY_POLL_MS 60000 +#define PCI_PM_D3_WAIT 10 +#define PCI_PM_D2_DELAY 200 +#define PCI_CAP_ID_EXP 0x10 +#define PCI_EXP_DEVCTL 0x08 #define PCI_EXP_LNKCTL 0x10 +#define PCI_EXP_SLTCTL 0x18 +#define PCI_EXP_RTCTL 0x1c +#define PCI_EXP_DEVCTL2 0x28 +#define PCI_EXP_LNKCTL2 0x30 +#define PCI_EXP_SLTCTL2 0x38 +#define PCI_EXP_LNKCTL_ASPM_L0S 0x0001 +#define PCI_EXP_LNKCTL_ASPM_L1 0x0002 #define PCI_EXP_LNKCTL_ASPMC 0x0003 #define PCI_L1SS_CTL1 0x08 #define PCI_L1SS_CTL2 0x0c @@ -180,9 +200,27 @@ def function(source, name): #define PCI_L1SS_CTL1_ASPM_L1_1 0x00000008 #define PCI_L1SS_CTL1_L1SS_MASK 0x0000000f #define PCI_L1SS_CTL1_L1_2_MASK 0x00000005 +#define PCI_EXT_CAP_ID_L1SS 0x001e typedef int pci_power_t; #define PCI_D0 0 +#define PCI_D1 1 +#define PCI_D2 2 #define PCI_D3hot 3 +#define PCI_D3cold 4 +#define BUS_NOTIFY_DEL_DEVICE 0x0002 +#define NOTIFY_DONE 0x0000 +#define pci_fixup_resume_early 0 +#define PCIE_LINK_STATE_L0S_UP BIT(0) +#define PCIE_LINK_STATE_L0S_DW BIT(1) +#define PCIE_LINK_STATE_L0S (PCIE_LINK_STATE_L0S_UP | PCIE_LINK_STATE_L0S_DW) +#define PCIE_LINK_STATE_L1 BIT(2) +#define PCIE_LINK_STATE_L1_1 BIT(3) +#define PCIE_LINK_STATE_L1_2 BIT(4) +#define PCIE_LINK_STATE_L1_1_PCIPM BIT(5) +#define PCIE_LINK_STATE_L1_2_PCIPM BIT(6) +#define PCIE_LINK_STATE_L1_SS_PCIPM (PCIE_LINK_STATE_L1_1_PCIPM | PCIE_LINK_STATE_L1_2_PCIPM) +#define PCIE_LINK_STATE_L1SS (PCIE_LINK_STATE_L1_1 | PCIE_LINK_STATE_L1_1_PCIPM | \ + PCIE_LINK_STATE_L1_2 | PCIE_LINK_STATE_L1_2_PCIPM) static inline int notifier_from_errno(int err) { return err ? (NOTIFY_STOP_MASK | (NOTIFY_OK - err)) : NOTIFY_OK; @@ -197,9 +235,17 @@ def function(source, name): typedef int (*notifier_fn_t)(struct notifier_block *nb, unsigned long action, void *data); struct notifier_block { notifier_fn_t notifier_call; }; +struct device; +struct dev_pm_ops { + int (*runtime_resume)(struct device *dev); +}; +struct device_driver { + const struct dev_pm_ops *pm; +}; struct device { struct apple_pcie *pcie; struct notifier_block *genpd_nb; + const struct device_driver *driver; bool genpd_on; int genpd_add_ret; int rpm_usage; @@ -208,6 +254,7 @@ def function(source, name): unsigned int add_calls, remove_calls; }; static int pci_rescan_remove_depth; +static int pci_bus_sem_depth; static void pm_runtime_get_noresume(struct device *dev) { dev->rpm_usage++; @@ -220,7 +267,7 @@ def function(source, name): } static int pm_runtime_barrier(struct device *dev) { - assert(pci_rescan_remove_depth == 0); + assert(pci_rescan_remove_depth == 0 && pci_bus_sem_depth == 0); assert(dev->rpm_usage > 0); dev->rpm_barriers++; if (dev->rpm_inflight_cb) { @@ -266,6 +313,85 @@ def function(source, name): entry->next = NULL; entry->prev = NULL; } +static void list_del_init(struct list_head *entry) +{ + entry->next->prev = entry->prev; + entry->prev->next = entry->next; + list_init(entry); +} +static void list_splice_init(struct list_head *list, struct list_head *head) +{ + if (!list_empty(list)) { + struct list_head *first = list->next; + struct list_head *last = list->prev; + struct list_head *at = head->next; + + first->prev = head; + head->next = first; + last->next = at; + at->prev = last; + list_init(list); + } +} +#define LIST_HEAD(name) struct list_head name = { &(name), &(name) } +#define list_first_entry_or_null(ptr, type, member) \ + (!list_empty(ptr) ? container_of((ptr)->next, type, member) : NULL) +typedef struct { int v; } atomic_t; +static void atomic_set(atomic_t *a, int v) { a->v = v; } +static int atomic_read(const atomic_t *a) { return a->v; } +static void atomic_inc(atomic_t *a) { a->v++; } +static void atomic_dec(atomic_t *a) { assert(a->v > 0); a->v--; } +typedef struct { bool held; } spinlock_t; +#define DEFINE_SPINLOCK(x) static spinlock_t x = { false } +static int atomic_ctx_depth; +static bool spin_is_locked(const spinlock_t *l) { return l->held; } +#define spin_lock_irqsave(lock, flags) do { \ + (flags) = 0; \ + assert(!(lock)->held); \ + (lock)->held = true; \ + atomic_ctx_depth++; \ +} while (0) +#define spin_unlock_irqrestore(lock, flags) do { \ + (void)(flags); \ + assert((lock)->held && atomic_ctx_depth > 0); \ + (lock)->held = false; \ + atomic_ctx_depth--; \ +} while (0) +struct mutex { + bool held; +}; +static void mutex_init(struct mutex *lock) +{ + lock->held = false; +} +struct work_struct { + void (*func)(struct work_struct *work); + struct work_struct *next; + bool pending; +}; +struct workqueue_struct { int id; }; +static struct workqueue_struct system_freezable_wq_obj = { 1 }; +static __attribute__((unused)) struct workqueue_struct system_dfl_wq_obj = { 2 }; +#define system_freezable_wq (&system_freezable_wq_obj) +#define system_dfl_wq (&system_dfl_wq_obj) +#define INIT_WORK(w, f) do { (w)->func = (f); (w)->next = NULL; (w)->pending = false; } while (0) +static struct work_struct *work_head, *work_tail; +static unsigned int queued_works; +static bool queue_work(struct workqueue_struct *wq, struct work_struct *work) +{ + assert(wq == system_freezable_wq); + if (work->pending) + return false; + work->pending = true; + work->next = NULL; + if (work_tail) + work_tail->next = work; + else + work_head = work; + work_tail = work; + queued_works++; + return true; +} struct apple_pcie_port { struct list_head entry; unsigned int idx; @@ -279,7 +405,13 @@ def function(source, name): u8 *base; struct list_head ports; struct list_head hib_l1ss; + struct list_head hib_l1ss_pending; struct list_head hib_l1ss_rpm; + struct mutex hib_l1ss_lock; + struct work_struct hib_l1ss_work; + atomic_t hib_l1ss_pending_cnt; + bool hib_l1ss_closing; + struct notifier_block hib_l1ss_pci_nb; struct pci_host_bridge *bridge; bool power_retained, kernel_init, tunnel_cold_init; bool bus_stopped, resume_failed, link_kept; @@ -340,37 +472,81 @@ def function(source, name): (void)dev; radio_complete_calls++; } +struct pci_host_bridge; struct pci_bus { struct list_head devices; + struct pci_dev *self; + struct pci_host_bridge *bridge; }; +struct pci_cap_saved_data { + u32 data[7]; +}; +struct pci_cap_saved_state { + struct pci_cap_saved_data cap; +}; +struct pcie_link_state; struct pci_dev { struct list_head bus_list; + struct pci_bus *bus; struct pci_bus *subordinate; + struct pcie_link_state *link_state; + struct work_struct aspm_pm_work; + u8 aspm_pcipm_deferred; + u8 aspm_l1ss_resync; struct device dev; + void *driver; pci_power_t current_state; + u32 dev_flags; u8 devfn; + u8 pm_cap; u16 l1ss; u16 lnkctl; + u16 pmcsr; + bool d1_support, d2_support; + bool refuse_low_power; bool downstream_port; bool disconnected; + bool power_up_fail; + int cfg_read_err; unsigned int refs; + unsigned int restore_calls; u32 cfg[128]; + struct pci_cap_saved_state saved_l1ss; + struct pci_cap_saved_state saved_exp; u32 first_ctl1_write; bool saw_ctl1_write; unsigned int lnkctl_write_calls; unsigned int fail_lnkctl_write_on_call; + unsigned int fail_lnkctl_write_count; unsigned int cfg_read_calls; unsigned int fail_cfg_read_on_call; unsigned int cfg_write_calls; unsigned int fail_cfg_write_on_call; }; +#define to_pci_dev(d) container_of(d, struct pci_dev, dev) struct pci_host_bridge { struct pci_bus *bus; + void *priv; + void (*pm_state_change)(struct pci_host_bridge *bridge, struct pci_dev *dev); }; +static int pci_bus_sem; +static struct mutex aspm_lock; +DEFINE_SPINLOCK(aspm_l1ss_hw_lock); +static bool aspm_disabled; static bool require_topology_lock; +static bool require_hw_lock_on_lnkctl; +static bool require_hw_lock_on_lnkctl_read; +static bool require_hw_lock_on_pmcsr; +static bool require_hw_lock_on_l1ss_read; +static void (*aspm_lock_pre_hook)(void); +static void (*cfg_read_hook)(struct pci_dev *pdev, int pos); +static void (*cfg_write_hook)(struct pci_dev *pdev, int pos); +static void (*pmcsr_write_hook)(struct pci_dev *pdev); static void pci_lock_rescan_remove(void) { + assert(atomic_ctx_depth == 0); assert(pci_rescan_remove_depth == 0); + assert(pci_bus_sem_depth == 0 && !aspm_lock.held); pci_rescan_remove_depth++; } static void pci_unlock_rescan_remove(void) @@ -378,6 +554,55 @@ def function(source, name): assert(pci_rescan_remove_depth == 1); pci_rescan_remove_depth--; } +static void down_read(int *sem) +{ + assert(atomic_ctx_depth == 0); + assert(sem == &pci_bus_sem); + assert(pci_bus_sem_depth == 0); + pci_bus_sem_depth++; +} +static void up_read(int *sem) +{ + assert(sem == &pci_bus_sem); + assert(pci_bus_sem_depth == 1); + pci_bus_sem_depth--; +} +static void lockdep_assert_held_read(int *sem) +{ + assert(sem == &pci_bus_sem && pci_bus_sem_depth > 0); +} +static void mutex_lock(struct mutex *lock) +{ + assert(atomic_ctx_depth == 0); + if (lock == &aspm_lock && aspm_lock_pre_hook) { + void (*hook)(void) = aspm_lock_pre_hook; + + aspm_lock_pre_hook = NULL; + hook(); + } + assert(!lock->held); + lock->held = true; +} +static void mutex_unlock(struct mutex *lock) +{ + assert(lock->held); + lock->held = false; +} +static void run_queued_work(void) +{ + assert(pci_rescan_remove_depth == 0 && pci_bus_sem_depth == 0 && + !aspm_lock.held && atomic_ctx_depth == 0); + while (work_head) { + struct work_struct *work = work_head; + + work_head = work->next; + if (!work_head) + work_tail = NULL; + work->next = NULL; + work->pending = false; + work->func(work); + } +} static bool pci_dev_is_disconnected(const struct pci_dev *pdev) { return pdev->disconnected; @@ -386,6 +611,26 @@ def function(source, name): { return pcie->bridge; } +static void *pci_host_bridge_priv(struct pci_host_bridge *bridge) +{ + return bridge->priv; +} +static struct pci_host_bridge *pci_find_host_bridge(struct pci_bus *bus) +{ + while (bus && bus->self && bus->self->bus) + bus = bus->self->bus; + return bus ? bus->bridge : NULL; +} +static struct pci_cap_saved_state *pci_find_saved_cap(struct pci_dev *dev, char cap) +{ + assert(cap == PCI_CAP_ID_EXP); + return &dev->saved_exp; +} +static struct pci_cap_saved_state *pci_find_saved_ext_cap(struct pci_dev *dev, u16 cap) +{ + assert(cap == PCI_EXT_CAP_ID_L1SS); + return dev->l1ss ? &dev->saved_l1ss : NULL; +} static bool pcie_downstream_port(const struct pci_dev *pdev) { return pdev->downstream_port; @@ -407,15 +652,37 @@ def function(source, name): { return err; } +static int pci_read_config_word(struct pci_dev *pdev, int pos, u16 *val) +{ + assert(pdev->pm_cap && pos == pdev->pm_cap + PCI_PM_CTRL); + *val = pdev->pmcsr; + return 0; +} +static int pci_write_config_word(struct pci_dev *pdev, int pos, u16 val) +{ + assert(pdev->pm_cap && pos == pdev->pm_cap + PCI_PM_CTRL); + if (require_hw_lock_on_pmcsr) + assert(spin_is_locked(&aspm_l1ss_hw_lock)); + if (pmcsr_write_hook) + pmcsr_write_hook(pdev); + if (!pdev->refuse_low_power) + pdev->pmcsr = val; + return 0; +} static int pci_read_config_dword(struct pci_dev *pdev, int pos, u32 *val) { if (require_topology_lock) assert(pci_rescan_remove_depth > 0); + if (require_hw_lock_on_l1ss_read && pdev->l1ss && + (pos == pdev->l1ss + PCI_L1SS_CTL1 || pos == pdev->l1ss + PCI_L1SS_CTL2)) + assert(spin_is_locked(&aspm_l1ss_hw_lock)); + if (cfg_read_hook) + cfg_read_hook(pdev, pos); assert((pos & 3) == 0 && (size_t)(pos >> 2) < ARRAY_SIZE(pdev->cfg)); pdev->cfg_read_calls++; if (pdev->fail_cfg_read_on_call && pdev->cfg_read_calls == pdev->fail_cfg_read_on_call) - return -EIO; + return pdev->cfg_read_err ? pdev->cfg_read_err : -EIO; *val = pdev->cfg[pos >> 2]; return 0; } @@ -423,6 +690,8 @@ def function(source, name): { if (require_topology_lock) assert(pci_rescan_remove_depth > 0); + if (cfg_write_hook) + cfg_write_hook(pdev, pos); assert((pos & 3) == 0 && (size_t)(pos >> 2) < ARRAY_SIZE(pdev->cfg)); pdev->cfg_write_calls++; if (pdev->l1ss && pos == pdev->l1ss + PCI_L1SS_CTL1 && !pdev->saw_ctl1_write) { @@ -435,22 +704,98 @@ def function(source, name): pdev->cfg[pos >> 2] = val; return 0; } +static int pci_clear_and_set_config_dword(struct pci_dev *pdev, int pos, + u32 clear, u32 set) +{ + u32 val; + int ret = pci_read_config_dword(pdev, pos, &val); + + if (ret) + return ret; + val = (val & ~clear) | set; + return pci_write_config_dword(pdev, pos, val); +} static int pcie_capability_read_word(struct pci_dev *pdev, int pos, u16 *val) { - assert(pos == PCI_EXP_LNKCTL); - *val = pdev->lnkctl; + if (pos == PCI_EXP_LNKCTL) { + if (require_hw_lock_on_lnkctl_read) + assert(spin_is_locked(&aspm_l1ss_hw_lock)); + *val = pdev->lnkctl; + } else { + *val = 0; + } return 0; } static int pcie_capability_write_word(struct pci_dev *pdev, int pos, u16 val) { - assert(pos == PCI_EXP_LNKCTL); - pdev->lnkctl_write_calls++; - if (pdev->fail_lnkctl_write_on_call && - pdev->lnkctl_write_calls == pdev->fail_lnkctl_write_on_call) - return -EIO; - pdev->lnkctl = val; + if (pos == PCI_EXP_LNKCTL) { + if (require_hw_lock_on_lnkctl) + assert(spin_is_locked(&aspm_l1ss_hw_lock)); + pdev->lnkctl_write_calls++; + if (pdev->fail_lnkctl_write_on_call) { + unsigned int n = pdev->fail_lnkctl_write_count ? pdev->fail_lnkctl_write_count : 1; + if (pdev->lnkctl_write_calls >= pdev->fail_lnkctl_write_on_call && + pdev->lnkctl_write_calls < pdev->fail_lnkctl_write_on_call + n) + return -EIO; + } + pdev->lnkctl = val; + } return 0; } +static int pcie_capability_clear_and_set_word(struct pci_dev *pdev, int pos, + u16 clear, u16 set) +{ + u16 val; + int ret = pcie_capability_read_word(pdev, pos, &val); + + if (ret) + return ret; + val = (val & ~clear) | set; + return pcie_capability_write_word(pdev, pos, val); +} +static void pci_save_ltr_state(struct pci_dev *dev) { (void)dev; } +static void pci_restore_ltr_state(struct pci_dev *dev) { (void)dev; } +static void pci_bridge_reconfigure_ltr(struct pci_dev *dev) { (void)dev; } +static bool pci_is_pcie(const struct pci_dev *dev) { (void)dev; return true; } +static int pci_dev_reset_iommu_prepare(struct pci_dev *dev) { (void)dev; return 0; } +static void pci_dev_reset_iommu_done(struct pci_dev *dev) { (void)dev; } +static int pci_dev_wait(struct pci_dev *dev, char *reset_type, int timeout) +{ + (void)dev; + (void)reset_type; + (void)timeout; + return 0; +} +static int pci_save_pcie_state(struct pci_dev *dev); +static void pci_restore_pcie_state(struct pci_dev *dev); +static void pci_update_aspm_saved_state(struct pci_dev *pdev) { (void)pdev; } +static void pci_dev_d3_sleep(struct pci_dev *dev) { (void)dev; } +static void udelay(unsigned long usecs) { (void)usecs; } +static const char *pci_power_name(pci_power_t state) +{ + return state == PCI_D0 ? "D0" : "D3hot"; +} +#define PCI_POSSIBLE_ERROR(val) ((val) == (typeof(val))~0U) +#define pci_dbg(dev, fmt, ...) ((void)(dev)) +#define pci_err(dev, fmt, ...) ((void)(dev)) +#define pci_info_ratelimited(dev, fmt, ...) ((void)(dev)) +static void pci_pm_power_up_and_verify_state(struct pci_dev *pci_dev) +{ + if (!pci_dev->power_up_fail) + pci_dev->current_state = PCI_D0; + else + pci_dev->current_state = PCI_D3cold; +} +static void pci_restore_state(struct pci_dev *pci_dev) +{ + pci_dev->restore_calls++; + pci_restore_pcie_state(pci_dev); +} +static void pci_pme_restore(struct pci_dev *pci_dev) { (void)pci_dev; } +static void pci_resume_ptm(struct pci_dev *pci_dev) { (void)pci_dev; } +static void pci_fixup_device(int pass, struct pci_dev *pci_dev) { (void)pass; (void)pci_dev; } +static void pci_pm_default_resume(struct pci_dev *pci_dev) { (void)pci_dev; } +static void pci_pm_bridge_power_up_actions(struct pci_dev *pci_dev) { (void)pci_dev; } static void pci_warn(const struct pci_dev *pdev, const char *fmt, ...) { (void)pdev; @@ -473,7 +818,9 @@ def function(source, name): static void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *), void *userdata) { + down_read(&pci_bus_sem); pci_walk_bus_rec(top, cb, userdata); + up_read(&pci_bus_sem); } static int dev_pm_genpd_add_notifier(struct device *dev, struct notifier_block *nb) @@ -531,7 +878,12 @@ def function(source, name): pcie->cio_dart = cio_dart; list_init(&pcie->ports); list_init(&pcie->hib_l1ss); + list_init(&pcie->hib_l1ss_pending); list_init(&pcie->hib_l1ss_rpm); + mutex_init(&pcie->hib_l1ss_lock); + INIT_WORK(&pcie->hib_l1ss_work, apple_pcie_hib_l1ss_work_fn); + atomic_set(&pcie->hib_l1ss_pending_cnt, 0); + pcie->hib_l1ss_closing = false; s2idle_keep_link = true; pm_suspend_target_state = PM_SUSPEND_TO_IDLE; suspend_via_firmware = false; @@ -539,6 +891,65 @@ def function(source, name): radio_prepare_calls = 0; radio_complete_calls = 0; warn_calls = 0; + aspm_disabled = false; + pci_bus_sem_depth = 0; + pci_rescan_remove_depth = 0; + atomic_ctx_depth = 0; + require_topology_lock = false; + require_hw_lock_on_lnkctl = false; + require_hw_lock_on_lnkctl_read = false; + require_hw_lock_on_pmcsr = false; + require_hw_lock_on_l1ss_read = false; + aspm_lock.held = false; + aspm_l1ss_hw_lock.held = false; + work_head = NULL; + work_tail = NULL; + queued_works = 0; + aspm_lock_pre_hook = NULL; + cfg_read_hook = NULL; + cfg_write_hook = NULL; + pmcsr_write_hook = NULL; +} +/* Concurrent events, injected at the point the real code would race. */ +static struct pci_dev *race_pdev; +static struct pcie_link_state *race_link; +static struct apple_pcie *race_pcie; +static struct pci_dev *race_dev; +static bool race_saw_lock; +static bool race_saw_rescan_lock; +static int race_pending_cnt_on_retry; +static void race_free_link_hook(void) +{ + memset(race_link, 0, sizeof(*race_link)); + race_pdev->link_state = NULL; +} +static void race_d0_cfg_write_hook(struct pci_dev *pdev, int pos) +{ + if (pdev != race_dev || pos != pdev->l1ss + PCI_L1SS_CTL1 || + pdev->cfg_write_calls < 2) + return; + cfg_write_hook = NULL; + race_dev->current_state = PCI_D0; + apple_pcie_pm_state_change(race_pcie->bridge, race_dev); +} +static void race_snapshot_read_hook(struct pci_dev *pdev, int pos) +{ + if (!pdev->l1ss || pos != pdev->l1ss + PCI_L1SS_CTL2) + return; + cfg_read_hook = NULL; + /* Verify hib_l1ss_lock and rescan_remove lock are held before reading CTL2/CTL1. */ + race_saw_lock = race_pcie->hib_l1ss_lock.held; + race_saw_rescan_lock = (pci_rescan_remove_depth > 0); +} +static void race_retry_d0_read_hook(struct pci_dev *pdev, int pos) +{ + /* Fire when worker checks link1 (ep1) while link0 (fn0) was moved to retry. */ + if (pdev != race_pdev || pos != PCI_VENDOR_ID) + return; + cfg_read_hook = NULL; + race_pending_cnt_on_retry = atomic_read(&race_pcie->hib_l1ss_pending_cnt); + race_dev->current_state = PCI_D0; + apple_pcie_pm_state_change(race_pcie->bridge, race_dev); } static u32 inflight_rpm_saved_ctl1; static struct pci_dev *inflight_rpm_pdev; @@ -565,6 +976,14 @@ def function(source, name): list_add_tail(&hotplug_chain_rps[hotplug_chain_next++]->bus_list, &hotplug_root_bus->devices); } +static void race_pmcsr_check_hook(struct pci_dev *pdev) +{ + /* Verify dev->current_state is already updated out of D0 before writing PMCSR. */ + assert(pdev->current_state == PCI_D3hot); + assert(spin_is_locked(&aspm_l1ss_hw_lock)); + pmcsr_write_hook = NULL; + require_hw_lock_on_pmcsr = false; +} int main(void) { struct apple_dart_cio cio_dart = { 1 }; @@ -807,17 +1226,40 @@ def function(source, name): list_init(&root_bus.devices); \ list_init(&sub0_bus.devices); \ list_init(&sub1_bus.devices); \ + root_bus.self = NULL; \ + root_bus.bridge = &bridge; \ + sub0_bus.self = &rp0; \ + sub0_bus.bridge = NULL; \ + sub1_bus.self = &rp1; \ + sub1_bus.bridge = NULL; \ bridge.bus = &root_bus; \ + bridge.priv = &pcie; \ + bridge.pm_state_change = apple_pcie_pm_state_change; \ pcie.bridge = &bridge; \ + pcie.hib_l1ss_pci_nb.notifier_call = apple_pcie_hib_l1ss_pci_notify; \ INIT_DEV(rp0, 0x00, 0x150, &sub0_bus, true, 0x106b, 0x000000ff, 0x28, 0x0042); \ INIT_DEV(fn0, 0x00, 0x160, NULL, false, 0x14e4, 0x000000ff, 0x28, 0x0042); \ INIT_DEV(fn1, 0x01, 0, NULL, false, 0x14e4, 0, 0, 0x0043); \ + rp0.bus = &root_bus; \ + fn0.bus = &sub0_bus; \ + fn1.bus = &sub0_bus; \ + fn0.pm_cap = 0x48; \ + ((u16 *)&fn0.saved_exp.cap.data[0])[1] = 0x0042; \ + pcie_aspm_init_dev(&rp0); \ + pcie_aspm_init_dev(&fn0); \ + pcie_aspm_init_dev(&fn1); \ list_add_tail(&rp0.bus_list, &root_bus.devices); \ list_add_tail(&fn0.bus_list, &sub0_bus.devices); \ list_add_tail(&fn1.bus_list, &sub0_bus.devices); \ if (two_links) { \ INIT_DEV(rp1, 0x08, 0x150, &sub1_bus, true, 0x106b, 0x0000005f, 0x14, 0x0042); \ INIT_DEV(ep1, 0x00, 0x160, NULL, false, 0x14e4, 0x0000005f, 0x14, 0x0042); \ + rp1.bus = &root_bus; \ + ep1.bus = &sub1_bus; \ + ep1.pm_cap = 0x48; \ + ((u16 *)&ep1.saved_exp.cap.data[0])[1] = 0x0042; \ + pcie_aspm_init_dev(&rp1); \ + pcie_aspm_init_dev(&ep1); \ list_add_tail(&rp1.bus_list, &root_bus.devices); \ list_add_tail(&ep1.bus_list, &sub1_bus.devices); \ } \ @@ -950,8 +1392,11 @@ def function(source, name): assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000fc); assert(rp0.cfg[(0x150 + PCI_L1SS_CTL2) >> 2] == 0x28); assert(fn0.cfg[(0x160 + PCI_L1SS_CTL2) >> 2] == 0x28); + apple_pcie_hib_free_l1ss(&pcie); + assert(list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 0); + assert(rp0.refs == 1 && fn0.refs == 1); - /* One-shot failure on child's final CTL1 write unwinds L1SS symmetrically on both ends. */ + /* One-shot failure on child's final CTL1 write unwinds L1SS symmetrically and retains recovery record. */ RESET_L1SS(false); assert(apple_pcie_prepare(&dev) == 0); fn0.cfg_write_calls = 0; @@ -959,10 +1404,18 @@ def function(source, name): fn0.fail_cfg_write_on_call = 4; apple_pcie_complete(&dev); assert(list_empty(&pcie.hib_l1ss)); + assert(!list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 1); assert(warn_calls == 1); assert((rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] & PCI_L1SS_CTL1_L1SS_MASK) == 0); assert((fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] & PCI_L1SS_CTL1_L1SS_MASK) == 0); assert(rp0.lnkctl == 0x0042 && fn0.lnkctl == 0x0042 && fn1.lnkctl == 0x0043); + /* Retry via D0 notification recovers L1SS cleanly and frees the pending record. */ + fn0.fail_cfg_write_on_call = 0; + apple_pcie_pm_state_change(&bridge, &fn0); + run_queued_work(); + assert(list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 0); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); assert(rp0.refs == 1 && fn0.refs == 1); /* Removed link (cleared subordinate or unlinked child) is skipped safely during complete(). */ @@ -972,7 +1425,7 @@ def function(source, name): rp0.cfg_write_calls = 0; fn0.cfg_write_calls = 0; apple_pcie_complete(&dev); - assert(list_empty(&pcie.hib_l1ss)); + assert(list_empty(&pcie.hib_l1ss) && list_empty(&pcie.hib_l1ss_pending)); assert(rp0.cfg_write_calls == 0 && fn0.cfg_write_calls == 0); assert(rp0.refs == 1 && fn0.refs == 1); @@ -982,7 +1435,7 @@ def function(source, name): rp0.cfg_write_calls = 0; fn0.cfg_write_calls = 0; apple_pcie_complete(&dev); - assert(list_empty(&pcie.hib_l1ss)); + assert(list_empty(&pcie.hib_l1ss) && list_empty(&pcie.hib_l1ss_pending)); assert(rp0.cfg_write_calls == 0 && fn0.cfg_write_calls == 0); assert(rp0.refs == 1 && fn0.refs == 1); @@ -1022,61 +1475,400 @@ def function(source, name): /* Suspend recheck propagates config read errors on a present link instead of skipping it. */ RESET_L1SS(false); assert(apple_pcie_prepare(&dev) == 0); - assert(rp0.dev.rpm_usage == 1 && fn0.dev.rpm_usage == 1 && fn1.dev.rpm_usage == 1); fn0.cfg_read_calls = 0; fn0.fail_cfg_read_on_call = 1; /* PCI_VENDOR_ID read error */ assert(apple_pcie_suspend(&dev) == -EIO); fn0.fail_cfg_read_on_call = 0; apple_pcie_complete(&dev); assert(list_empty(&pcie.hib_l1ss)); - assert(rp0.dev.rpm_usage == 0 && fn0.dev.rpm_usage == 0 && fn1.dev.rpm_usage == 0); + assert(rp0.refs == 1 && fn0.refs == 1); + + /* Unmanaged fallback: D3hot endpoint syncs saved state, defers PCI-PM, and restores via freezable work. */ + RESET_L1SS(false); + aspm_disabled = true; + fn0.current_state = PCI_D3hot; + rp0.saved_l1ss.cap.data[0] = 0x28; + rp0.saved_l1ss.cap.data[1] = 0x000000ff; + fn0.saved_l1ss.cap.data[0] = 0x28; + fn0.saved_l1ss.cap.data[1] = 0x000000ff; + assert(apple_pcie_prepare(&dev) == 0); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000f0); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000f0); + assert(rp0.saved_l1ss.cap.data[1] == 0x000000f0 && fn0.saved_l1ss.cap.data[1] == 0x000000f0); + /* A PCI restore during the sleep window (even from atomic context!) must not sleep or replay stale PCI-PM enables. */ + atomic_ctx_depth = 1; + require_hw_lock_on_lnkctl = true; + pci_save_aspm_l1ss_state(&fn0); + pci_restore_state(&fn0); + require_hw_lock_on_lnkctl = false; + atomic_ctx_depth = 0; + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000f0); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000f0); + apple_pcie_complete(&dev); + assert(list_empty(&pcie.hib_l1ss)); + assert(!list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 1); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000fc); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000fc); + assert(rp0.saved_l1ss.cap.data[1] == 0x000000fc && fn0.saved_l1ss.cap.data[1] == 0x000000fc); + /* Runtime resume under pci_bus_sem queues hib_l1ss_work without taking locks inline. */ + pci_bus_sem_depth = 1; + assert(pci_pm_runtime_resume(&fn0.dev) == 0); + pci_bus_sem_depth = 0; + assert(fn0.current_state == PCI_D0 && queued_works == 1); + require_topology_lock = true; + require_hw_lock_on_lnkctl = true; + run_queued_work(); + require_hw_lock_on_lnkctl = false; + require_topology_lock = false; + assert(list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 0); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(rp0.saved_l1ss.cap.data[1] == 0x000000ff && fn0.saved_l1ss.cap.data[1] == 0x000000ff); assert(rp0.refs == 1 && fn0.refs == 1 && fn1.refs == 1); - /* One-shot LNKCTL restore failure in complete() retries parent/function ASPM instead of leaving it disabled. */ + /* pci_set_low_power_state() holds aspm_l1ss_hw_lock across current_state + PMCSR write and notifies on refused D0. */ RESET_L1SS(false); + fn0.current_state = PCI_D0; + fn0.pmcsr = PCI_D0; + require_hw_lock_on_pmcsr = true; + pmcsr_write_hook = race_pmcsr_check_hook; + assert(pci_set_low_power_state(&fn0, PCI_D3hot, false) == 0); + pmcsr_write_hook = NULL; + require_hw_lock_on_pmcsr = false; + assert(fn0.current_state == PCI_D3hot && (fn0.pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot); + /* Refused D3hot leaving device in D0 triggers pm_state_change notification. */ + fn0.current_state = PCI_D0; + fn0.pmcsr = PCI_D0; + fn0.refuse_low_power = true; + aspm_disabled = true; + fn0.current_state = PCI_D3hot; assert(apple_pcie_prepare(&dev) == 0); - rp0.lnkctl_write_calls = 0; - rp0.fail_lnkctl_write_on_call = 2; /* 1=clear ASPM, 2=restore_aspm fails once */ apple_pcie_complete(&dev); - assert(list_empty(&pcie.hib_l1ss) && warn_calls == 1); - assert(rp0.lnkctl == 0x0042 && fn0.lnkctl == 0x0042 && fn1.lnkctl == 0x0043); - assert(rp0.dev.rpm_usage == 0 && fn0.dev.rpm_usage == 0 && fn1.dev.rpm_usage == 0); + assert(atomic_read(&pcie.hib_l1ss_pending_cnt) == 1); + fn0.current_state = PCI_D0; + queued_works = 0; + assert(pci_set_low_power_state(&fn0, PCI_D3hot, false) == 0); + assert(fn0.current_state == PCI_D0 && queued_works == 1); + run_queued_work(); + assert(atomic_read(&pcie.hib_l1ss_pending_cnt) == 0); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + + /* Unmanaged fallback across two sleeps in D3hot preserves original PCI-PM bits. */ + RESET_L1SS(false); + aspm_disabled = true; + fn0.current_state = PCI_D3hot; + assert(apple_pcie_prepare(&dev) == 0); + apple_pcie_complete(&dev); + assert(!list_empty(&pcie.hib_l1ss_pending)); + assert(apple_pcie_prepare(&dev) == 0); + apple_pcie_complete(&dev); + assert(!list_empty(&pcie.hib_l1ss_pending)); + assert(pci_pm_runtime_resume(&fn0.dev) == 0); + run_queued_work(); + assert(list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 0); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(rp0.refs == 1 && fn0.refs == 1); + + /* Snapshot vs D0-worker & hot-removal race: rescan lock + hib_l1ss_lock are held before reading CTL1. */ + RESET_L1SS(false); + aspm_disabled = true; + rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] = 0x000000f3; + fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] = 0x000000f3; + fn0.current_state = PCI_D3hot; + assert(apple_pcie_prepare(&dev) == 0); + apple_pcie_complete(&dev); + assert(!list_empty(&pcie.hib_l1ss_pending)); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000f0); + race_pcie = &pcie; + race_dev = &fn0; + race_saw_lock = false; + race_saw_rescan_lock = false; + cfg_read_hook = race_snapshot_read_hook; + assert(apple_pcie_prepare(&dev) == 0); + assert(race_saw_lock && race_saw_rescan_lock); + assert(!list_empty(&pcie.hib_l1ss) && list_empty(&pcie.hib_l1ss_pending)); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000f0); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000f0); + fn0.current_state = PCI_D0; + apple_pcie_complete(&dev); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000f3); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000f3); + + /* Worker retry-list race: hib_l1ss_pending_cnt stays >0 while a D3hot record sits on retry so D0 is never lost. */ + RESET_L1SS(true); + aspm_disabled = true; + fn0.current_state = PCI_D3hot; + ep1.current_state = PCI_D3hot; + assert(apple_pcie_prepare(&dev) == 0); + apple_pcie_complete(&dev); + assert(atomic_read(&pcie.hib_l1ss_pending_cnt) == 2); + /* Wake ep1 first; while worker handles ep1 and fn0 is on retry, wake fn0 and fire pm_state_change! */ + ep1.current_state = PCI_D0; + race_pcie = &pcie; + race_pdev = &ep1; + race_dev = &fn0; + race_pending_cnt_on_retry = -1; + cfg_read_hook = race_retry_d0_read_hook; + queued_works = 0; + apple_pcie_pm_state_change(&bridge, &ep1); + run_queued_work(); + assert(race_pending_cnt_on_retry == 2); + assert(list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 0); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(rp1.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x0000005f); + assert(ep1.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x0000005f); + assert(rp0.refs == 1 && fn0.refs == 1 && rp1.refs == 1 && ep1.refs == 1); + + /* BUS_NOTIFY_DEL_DEVICE purges BOTH hib_l1ss and hib_l1ss_pending cleanly; hib_l1ss_rpm pins release on complete/free. */ + RESET_L1SS(false); + aspm_disabled = true; + assert(apple_pcie_prepare(&dev) == 0); + assert(!list_empty(&pcie.hib_l1ss) && rp0.refs == 3 && fn0.refs == 3); + assert(rp0.dev.rpm_usage == 1 && fn0.dev.rpm_usage == 1 && fn1.dev.rpm_usage == 1); + assert(apple_pcie_hib_l1ss_pci_notify(&pcie.hib_l1ss_pci_nb, BUS_NOTIFY_DEL_DEVICE, &fn0.dev) == NOTIFY_DONE); + assert(list_empty(&pcie.hib_l1ss) && rp0.refs == 2 && fn0.refs == 2 && fn1.refs == 2); + apple_pcie_complete(&dev); assert(rp0.refs == 1 && fn0.refs == 1 && fn1.refs == 1); + assert(rp0.dev.rpm_usage == 0 && fn0.dev.rpm_usage == 0 && fn1.dev.rpm_usage == 0); + rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] = 0x000000ff; + fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] = 0x000000ff; + fn0.current_state = PCI_D3hot; + assert(apple_pcie_prepare(&dev) == 0); + apple_pcie_complete(&dev); + assert(!list_empty(&pcie.hib_l1ss_pending) && rp0.refs == 2 && fn0.refs == 2); + assert(apple_pcie_hib_l1ss_pci_notify(&pcie.hib_l1ss_pci_nb, BUS_NOTIFY_DEL_DEVICE, &fn0.dev) == NOTIFY_DONE); + assert(list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 0); + assert(rp0.refs == 1 && fn0.refs == 1); + /* Core-managed link tests: sleep suppression, D3hot deferral, coalesced freezable work, restore sync, timing error retention. */ + struct pcie_link_state link0; + #define ATTACH_LINK(l, up, dn, sup, en, def, dis) do { \ + memset(&(l), 0, sizeof(l)); \ + (l).pdev = (up); \ + (l).downstream = (dn); \ + (l).root = &(l); \ + (l).aspm_support = (sup); \ + (l).aspm_capable = (sup); \ + (l).aspm_enabled = (en); \ + (l).aspm_hw_l1ss = (en) & PCIE_LINK_STATE_L1SS; \ + (l).aspm_default = (def); \ + (l).aspm_disable = (dis); \ + (up)->link_state = &(l); \ + (up)->aspm_pcipm_deferred = 0; \ + } while (0) + #define ALL_L1_STATES (PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_L1SS) + + /* Core-managed normal D0 sleep/resume preserves policy and blocks mid-sleep restore replay. */ RESET_L1SS(false); + ATTACH_LINK(link0, &rp0, &fn0, ALL_L1_STATES, ALL_L1_STATES, ALL_L1_STATES, 0); assert(apple_pcie_prepare(&dev) == 0); - fn1.lnkctl_write_calls = 0; - fn1.fail_lnkctl_write_on_call = 2; /* 1=clear ASPM, 2=restore_aspm fails once on downstream fn */ + assert(link0.aspm_sleep_disable == PCIE_LINK_STATE_L1SS); + assert(link0.aspm_enabled == PCIE_LINK_STATE_L1); + assert(rp0.saved_l1ss.cap.data[1] == 0x000000f0 && fn0.saved_l1ss.cap.data[1] == 0x000000f0); + /* Even if stale saved_l1ss had enables, pci_restore_state() masks them during sleep (and is atomic-safe). */ + rp0.saved_l1ss.cap.data[1] = 0x000000ff; + fn0.saved_l1ss.cap.data[1] = 0x000000ff; + atomic_ctx_depth = 1; + pci_restore_state(&fn0); + atomic_ctx_depth = 0; + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000f0); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000f0); + assert(link0.aspm_enabled == PCIE_LINK_STATE_L1); + pcie_aspm_pm_state_change(&rp0, false); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000f0); apple_pcie_complete(&dev); - assert(list_empty(&pcie.hib_l1ss) && warn_calls == 1); - assert(rp0.lnkctl == 0x0042 && fn0.lnkctl == 0x0042 && fn1.lnkctl == 0x0043); + assert(link0.aspm_sleep_disable == 0 && link0.aspm_enabled == ALL_L1_STATES); + assert(rp0.aspm_pcipm_deferred == 0); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + /* When aspm_pcipm_deferred == 0, runtime resume queues ZERO ASPM work items. */ + queued_works = 0; + assert(pci_pm_runtime_resume(&fn0.dev) == 0); + assert(queued_works == 0); + + /* Core-managed D3hot endpoint sets aspm_pcipm_deferred, coalesces work, and avoids desync with pci_restore_state(). */ + RESET_L1SS(false); + ATTACH_LINK(link0, &rp0, &fn0, ALL_L1_STATES, ALL_L1_STATES, ALL_L1_STATES, 0); + assert(apple_pcie_prepare(&dev) == 0); + fn0.current_state = PCI_D3hot; + apple_pcie_complete(&dev); + assert(link0.aspm_sleep_disable == 0); + assert(rp0.aspm_pcipm_deferred == 1); + assert(link0.aspm_enabled == (PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_L1_1 | PCIE_LINK_STATE_L1_2)); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000fc); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000fc); + /* Failed power-up leaves device out of D0 and queues no work. */ + fn0.power_up_fail = true; + assert(pci_pm_runtime_resume(&fn0.dev) == 0); + assert(fn0.current_state == PCI_D3cold && queued_works == 0); + fn0.power_up_fail = false; + fn0.restore_calls = 0; + /* Error recovery runtime resume with pci_bus_sem held queues preallocated coalesced work. */ + pci_bus_sem_depth = 1; + assert(pci_pm_runtime_resume(&fn0.dev) == 0); + assert(pci_pm_runtime_resume(&fn0.dev) == 0); + pci_bus_sem_depth = 0; + assert(fn0.current_state == PCI_D0 && fn0.restore_calls == 2); + assert(queued_works == 1 && rp0.refs == 2); + run_queued_work(); + assert(rp0.refs == 1 && fn0.refs == 1 && rp0.aspm_pcipm_deferred == 0); + assert(link0.aspm_enabled == ALL_L1_STATES); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + /* If a later pci_restore_state() replays stale masked CTL1, atomic restore sets aspm_l1ss_resync (without mutating bitfields) and queues deferred recheck! */ + rp0.saved_l1ss.cap.data[1] = 0x000000fc; + fn0.saved_l1ss.cap.data[1] = 0x000000fc; + queued_works = 0; + pci_restore_state(&fn0); + assert(link0.aspm_enabled == ALL_L1_STATES); + assert(rp0.aspm_l1ss_resync == 1 && rp0.aspm_pcipm_deferred == 1 && queued_works == 1); + run_queued_work(); + assert(link0.aspm_enabled == ALL_L1_STATES && rp0.aspm_l1ss_resync == 0 && rp0.aspm_pcipm_deferred == 0); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + + /* Core-managed transient vendor-ID -EIO with both ends ALREADY in D0 queues bounded retry automatically! */ + RESET_L1SS(false); + ATTACH_LINK(link0, &rp0, &fn0, ALL_L1_STATES, ALL_L1_STATES, ALL_L1_STATES, 0); + assert(apple_pcie_prepare(&dev) == 0); + assert(rp0.dev.rpm_usage == 1 && fn0.dev.rpm_usage == 1 && fn1.dev.rpm_usage == 1); + fn0.cfg_read_calls = 0; + fn0.fail_cfg_read_on_call = 1; /* transient PCI_VENDOR_ID -EIO while both ends are already D0 */ + queued_works = 0; + apple_pcie_complete(&dev); + assert(warn_calls == 1); assert(rp0.dev.rpm_usage == 0 && fn0.dev.rpm_usage == 0 && fn1.dev.rpm_usage == 0); + assert(link0.aspm_sleep_disable == PCIE_LINK_STATE_L1SS); + assert(!list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 1); + assert(queued_works == 1); /* bounded retry queued without requiring another D0 transition */ + fn0.fail_cfg_read_on_call = 0; + run_queued_work(); + assert(list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 0); + assert(link0.aspm_sleep_disable == 0 && link0.aspm_enabled == ALL_L1_STATES); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); assert(rp0.refs == 1 && fn0.refs == 1 && fn1.refs == 1); - /* Runtime-PM pins balance on rollback, remove/free, and suspend-recheck discovery. */ + /* Config-read -ENODEV on a still-present link retains recovery and keeps aspm_sleep_disable armed! */ RESET_L1SS(false); - rp0.fail_lnkctl_write_on_call = 2; + ATTACH_LINK(link0, &rp0, &fn0, ALL_L1_STATES, ALL_L1_STATES, ALL_L1_STATES, 0); + assert(apple_pcie_prepare(&dev) == 0); + fn0.cfg_read_calls = 0; + fn0.cfg_read_err = -ENODEV; + fn0.fail_cfg_read_on_call = 1; + queued_works = 0; + apple_pcie_complete(&dev); + assert(link0.aspm_sleep_disable == PCIE_LINK_STATE_L1SS); + assert(!list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 1); + assert(queued_works == 1); + fn0.fail_cfg_read_on_call = 0; + fn0.cfg_read_err = 0; + run_queued_work(); + assert(list_empty(&pcie.hib_l1ss_pending) && link0.aspm_sleep_disable == 0); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(rp0.refs == 1 && fn0.refs == 1); + + /* Still-enumerated D3cold endpoint returning 0xffffffff retains recovery until D0 resume! */ + RESET_L1SS(false); + ATTACH_LINK(link0, &rp0, &fn0, ALL_L1_STATES, ALL_L1_STATES, ALL_L1_STATES, 0); + assert(apple_pcie_prepare(&dev) == 0); + fn0.current_state = PCI_D3cold; + fn0.cfg[PCI_VENDOR_ID >> 2] = U32_MAX; + queued_works = 0; + apple_pcie_complete(&dev); + assert(warn_calls == 0); + assert(link0.aspm_sleep_disable == PCIE_LINK_STATE_L1SS); + assert(!list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 1); + assert(queued_works == 0); + /* Endpoint powers back up to D0 and becomes readable again. */ + fn0.cfg[PCI_VENDOR_ID >> 2] = 0x14e4; + assert(pci_pm_runtime_resume(&fn0.dev) == 0); + run_queued_work(); + assert(list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 0); + assert(link0.aspm_sleep_disable == 0 && link0.aspm_enabled == ALL_L1_STATES); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(rp0.refs == 1 && fn0.refs == 1); + + /* Unmanaged failed prepare LNKCTL restore + transient rollback failure preserves rollback intent in worker! */ + RESET_L1SS(false); + aspm_disabled = true; + /* Link0 prepare fails on parent ASPM restore (leaves rp0.lnkctl == 0x0040); rollback fails on VID read! */ + rp0.lnkctl_write_calls = 0; + rp0.fail_lnkctl_write_on_call = 2; /* 1=clear ASPM, 2=prepare restore_aspm fails */ + fn0.cfg_read_calls = 0; + fn0.fail_cfg_read_on_call = 4; /* 1=prepare VID, 2=CTL2, 3=CTL1, 4=rollback VID fails */ + queued_works = 0; assert(apple_pcie_prepare(&dev) == -EIO); - assert(rp0.dev.rpm_usage == 0 && fn0.dev.rpm_usage == 0 && fn1.dev.rpm_usage == 0); + assert(rp0.lnkctl == 0x0040); + assert(!list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 1); + assert(queued_works == 1); + fn0.fail_cfg_read_on_call = 0; + rp0.fail_lnkctl_write_on_call = 0; + run_queued_work(); + assert(list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 0); + assert(rp0.lnkctl == 0x0042 && fn0.lnkctl == 0x0042 && fn1.lnkctl == 0x0043); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); assert(rp0.refs == 1 && fn0.refs == 1 && fn1.refs == 1); + /* pci_save_pcie_state() and prepare/recheck L1SS snapshots hold aspm_l1ss_hw_lock across LNKCTL + CTL2/CTL1 reads! */ RESET_L1SS(false); + require_hw_lock_on_lnkctl_read = true; + require_hw_lock_on_l1ss_read = true; + assert(pci_save_pcie_state(&fn0) == 0); assert(apple_pcie_prepare(&dev) == 0); - assert(rp0.dev.rpm_usage == 1 && fn0.dev.rpm_usage == 1 && fn1.dev.rpm_usage == 1); - apple_pcie_hib_free_l1ss(&pcie); - assert(rp0.dev.rpm_usage == 0 && fn0.dev.rpm_usage == 0 && fn1.dev.rpm_usage == 0); - assert(rp0.refs == 1 && fn0.refs == 1 && fn1.refs == 1); + rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] = 0x000000ff; + fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] = 0x000000ff; + assert(apple_pcie_suspend(&dev) == 0); + require_hw_lock_on_lnkctl_read = false; + require_hw_lock_on_l1ss_read = false; + apple_pcie_complete(&dev); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + /* pci_pm_reset() serializes D3hot entry under aspm_l1ss_hw_lock and notifies D0 when ready again! */ RESET_L1SS(false); + aspm_disabled = true; + fn0.current_state = PCI_D3hot; + assert(apple_pcie_prepare(&dev) == 0); + apple_pcie_complete(&dev); + assert(atomic_read(&pcie.hib_l1ss_pending_cnt) == 1); + fn0.current_state = PCI_D0; + fn0.pmcsr = PCI_D0; + queued_works = 0; + require_hw_lock_on_pmcsr = true; + pmcsr_write_hook = race_pmcsr_check_hook; + assert(pci_pm_reset(&fn0, false) == 0); + pmcsr_write_hook = NULL; + require_hw_lock_on_pmcsr = false; + assert(fn0.current_state == PCI_D0 && queued_works == 1); + run_queued_work(); + assert(atomic_read(&pcie.hib_l1ss_pending_cnt) == 0); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + + /* Suspend recheck partial error after clearing L1SS on a link missed by prepare() MUST preserve recovery snapshot! */ + RESET_L1SS(false); + aspm_disabled = true; rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] = 0x000000f0; fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] = 0x000000f0; assert(apple_pcie_prepare(&dev) == 0); - assert(rp0.dev.rpm_usage == 1 && fn0.dev.rpm_usage == 1 && fn1.dev.rpm_usage == 1); + assert(list_empty(&pcie.hib_l1ss)); + /* Endpoint replayed L1SS after prepare; suspend recheck clears child+parent CTL1 then fails on ASPM restore! */ rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] = 0x000000ff; fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] = 0x000000ff; - assert(apple_pcie_suspend(&dev) == 0); - assert(rp0.dev.rpm_usage == 1 && fn0.dev.rpm_usage == 1 && fn1.dev.rpm_usage == 1); + rp0.lnkctl_write_calls = 0; + rp0.fail_lnkctl_write_on_call = 2; /* 1=clear ASPM, 2=restore_aspm fails once */ + assert(apple_pcie_suspend(&dev) == -EIO); + assert(!list_empty(&pcie.hib_l1ss)); /* recovery snapshot MUST NOT be dropped after modifying hardware! */ + assert(rp0.lnkctl == 0x0042 && fn0.lnkctl == 0x0042 && fn1.lnkctl == 0x0043); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000f0); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000f0); + rp0.fail_lnkctl_write_on_call = 0; apple_pcie_complete(&dev); + assert(list_empty(&pcie.hib_l1ss)); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); assert(rp0.dev.rpm_usage == 0 && fn0.dev.rpm_usage == 0 && fn1.dev.rpm_usage == 0); assert(rp0.refs == 1 && fn0.refs == 1 && fn1.refs == 1); @@ -1087,15 +1879,148 @@ def function(source, name): fn0.dev.rpm_inflight_cb = inflight_rpm_save_ctl1; assert(apple_pcie_prepare(&dev) == 0); assert(fn0.dev.rpm_barriers == 1 && rp0.dev.rpm_barriers == 1 && fn1.dev.rpm_barriers == 1); - assert(inflight_rpm_saved_ctl1 == 0x000000ff); /* drained BEFORE prepare cleared L1SS to 0xf0 */ + assert(inflight_rpm_saved_ctl1 == 0x000000ff); assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000f0); apple_pcie_complete(&dev); assert(rp0.dev.rpm_usage == 0 && fn0.dev.rpm_usage == 0 && fn1.dev.rpm_usage == 0); assert(rp0.refs == 1 && fn0.refs == 1 && fn1.refs == 1); - /* Hotplugging a new L1SS link during the unlocked barrier window re-pins and drains it before clearing L1SS! */ + /* One-shot LNKCTL restore failure in complete() retries parent and downstream function ASPM. */ + RESET_L1SS(false); + assert(apple_pcie_prepare(&dev) == 0); + rp0.lnkctl_write_calls = 0; + rp0.fail_lnkctl_write_on_call = 2; + apple_pcie_complete(&dev); + assert(rp0.lnkctl == 0x0042 && fn0.lnkctl == 0x0042 && fn1.lnkctl == 0x0043); + assert(rp0.dev.rpm_usage == 0 && fn0.dev.rpm_usage == 0 && fn1.dev.rpm_usage == 0); + apple_pcie_hib_free_l1ss(&pcie); + + RESET_L1SS(false); + assert(apple_pcie_prepare(&dev) == 0); + fn1.lnkctl_write_calls = 0; + fn1.fail_lnkctl_write_on_call = 2; + apple_pcie_complete(&dev); + assert(rp0.lnkctl == 0x0042 && fn0.lnkctl == 0x0042 && fn1.lnkctl == 0x0043); + apple_pcie_hib_free_l1ss(&pcie); + + /* Failed prepare rollback ALSO retains recovery record and keeps aspm_sleep_disable armed until recovery! */ + RESET_L1SS(true); + ATTACH_LINK(link0, &rp0, &fn0, ALL_L1_STATES, ALL_L1_STATES, ALL_L1_STATES, 0); + /* Fail link1 prepare -> triggers rollback of link0, whose CTL2 timing write fails transiently. */ + rp1.fail_lnkctl_write_on_call = 2; + fn0.cfg_write_calls = 0; + fn0.fail_cfg_write_on_call = 3; /* 1=prepare clear CTL1, 2=rollback clear CTL1, 3=rollback CTL2 write */ + assert(apple_pcie_prepare(&dev) == -EIO); + assert(link0.aspm_sleep_disable == PCIE_LINK_STATE_L1SS); + assert(!list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 1); + fn0.fail_cfg_write_on_call = 0; + apple_pcie_pm_state_change(&bridge, &fn0); + run_queued_work(); + assert(list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 0); + assert(link0.aspm_sleep_disable == 0 && link0.aspm_enabled == ALL_L1_STATES); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL2) >> 2] == 0x28); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL2) >> 2] == 0x28); + assert(rp0.refs == 1 && fn0.refs == 1 && rp1.refs == 1 && ep1.refs == 1); + + /* Core-managed timing restore failure keeps aspm_sleep_disable armed and retains recovery record! */ + RESET_L1SS(false); + ATTACH_LINK(link0, &rp0, &fn0, ALL_L1_STATES, ALL_L1_STATES, ALL_L1_STATES, 0); + assert(apple_pcie_prepare(&dev) == 0); + fn0.cfg_write_calls = 0; + fn0.fail_cfg_write_on_call = 2; /* fail child CTL2 timing write */ + apple_pcie_complete(&dev); + assert(warn_calls == 1); + assert(link0.aspm_sleep_disable == PCIE_LINK_STATE_L1SS); + assert(link0.aspm_enabled == PCIE_LINK_STATE_L1); + assert(!list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 1); + /* A policy recheck must NOT enable L1.2 against incomplete timing. */ + pcie_aspm_pm_state_change(&rp0, false); + assert((rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] & PCI_L1SS_CTL1_L1SS_MASK) == 0); + assert((fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] & PCI_L1SS_CTL1_L1SS_MASK) == 0); + /* Subsequent D0 work retries timing restore, clears aspm_sleep_disable, and enables full L1SS. */ + fn0.fail_cfg_write_on_call = 0; + apple_pcie_pm_state_change(&bridge, &fn0); + run_queued_work(); + assert(list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 0); + assert(link0.aspm_sleep_disable == 0 && link0.aspm_enabled == ALL_L1_STATES); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL2) >> 2] == 0x28); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL2) >> 2] == 0x28); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(rp0.refs == 1 && fn0.refs == 1); + + /* Core-managed link starting with L1SS disabled still installs sleep mask against mid-sleep enable. */ + RESET_L1SS(false); + rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] = 0x000000f0; + fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] = 0x000000f0; + ATTACH_LINK(link0, &rp0, &fn0, ALL_L1_STATES, PCIE_LINK_STATE_L1, ALL_L1_STATES, 0); + assert(apple_pcie_prepare(&dev) == 0); + assert(!list_empty(&pcie.hib_l1ss) && link0.aspm_sleep_disable == PCIE_LINK_STATE_L1SS); + pcie_aspm_pm_state_change(&rp0, false); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000f0); + apple_pcie_complete(&dev); + assert(link0.aspm_sleep_disable == 0 && link0.aspm_enabled == ALL_L1_STATES); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + + /* Core-managed link: pre-existing aspm_disable bits stay disabled after resume. */ + RESET_L1SS(false); + rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] = 0x000000fe; + fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] = 0x000000fe; + ATTACH_LINK(link0, &rp0, &fn0, ALL_L1_STATES, + ALL_L1_STATES & ~PCIE_LINK_STATE_L1_2_PCIPM, + ALL_L1_STATES, PCIE_LINK_STATE_L1_2_PCIPM); + assert(apple_pcie_prepare(&dev) == 0); + apple_pcie_complete(&dev); + assert(link0.aspm_disable == PCIE_LINK_STATE_L1_2_PCIPM); + assert(link0.aspm_enabled == (ALL_L1_STATES & ~PCIE_LINK_STATE_L1_2_PCIPM)); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000fe); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000fe); + + /* A link freed between queueing and running the work item is skipped, not dereferenced. */ + RESET_L1SS(false); + ATTACH_LINK(link0, &rp0, &fn0, ALL_L1_STATES, ALL_L1_STATES, ALL_L1_STATES, 0); + rp0.aspm_pcipm_deferred = 1; + queued_works = 0; + fn0.current_state = PCI_D0; + assert(pci_pm_runtime_resume(&fn0.dev) == 0); + assert(queued_works == 1 && rp0.refs == 2); + race_pdev = &rp0; + race_link = &link0; + aspm_lock_pre_hook = race_free_link_hook; + run_queued_work(); + assert(rp0.link_state == NULL && rp0.refs == 1 && !aspm_lock.held); + assert(aspm_lock_pre_hook == NULL); + + /* Removal of the link between unlocked hint and aspm_lock must not be dereferenced. */ + RESET_L1SS(false); + ATTACH_LINK(link0, &rp0, &fn0, ALL_L1_STATES, ALL_L1_STATES, ALL_L1_STATES, 0); + race_pdev = &rp0; + race_link = &link0; + aspm_lock_pre_hook = race_free_link_hook; + pcie_aspm_pm_state_change(&rp0, false); + assert(rp0.link_state == NULL && aspm_lock_pre_hook == NULL && !aspm_lock.held); + + /* Unmanaged D0 transition mid-restore during complete() is caught by post-publish D0 recheck. */ + RESET_L1SS(false); + aspm_disabled = true; + fn0.current_state = PCI_D3hot; + assert(apple_pcie_prepare(&dev) == 0); + race_pcie = &pcie; + race_dev = &fn0; + cfg_write_hook = race_d0_cfg_write_hook; + apple_pcie_complete(&dev); + assert(cfg_write_hook == NULL && fn0.current_state == PCI_D0); + assert(queued_works == 1); + run_queued_work(); + assert(list_empty(&pcie.hib_l1ss) && list_empty(&pcie.hib_l1ss_pending)); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(rp0.refs == 1 && fn0.refs == 1 && !pcie.hib_l1ss_lock.held); + + /* Hotplugging a new L1SS link during the unlocked barrier window re-pins and drains it before clearing L1SS. */ RESET_L1SS(true); - list_del(&rp1.bus_list); /* rp1+ep1 not present on initial pin walk */ + list_del(&rp1.bus_list); hotplug_root_bus = &root_bus; hotplug_rp = &rp1; fn0.dev.rpm_inflight_cb = hotplug_during_barrier; @@ -1109,7 +2034,206 @@ def function(source, name): assert(rp1.dev.rpm_usage == 0 && ep1.dev.rpm_usage == 0); assert(rp0.refs == 1 && fn0.refs == 1 && fn1.refs == 1 && rp1.refs == 1 && ep1.refs == 1); - /* Sustained hotplug churn exhausting the 8-pass pin/drain limit returns -EBUSY (not -EAGAIN, which dpm_prepare retries). */ + /* R4-1: Double-failed LNKCTL restore during normal complete() retains intended ASPM and recovers on retry. */ + RESET_L1SS(false); + aspm_disabled = true; + assert(apple_pcie_prepare(&dev) == 0); + /* Resume policy kept L1 (0x0042) on rp0 and L0s+L1 (0x0043) on fn1. */ + rp0.lnkctl_write_calls = 0; + rp0.fail_lnkctl_write_on_call = 2; /* 1=clear, 2=first restore, 3=immediate retry */ + rp0.fail_lnkctl_write_count = 2; + apple_pcie_complete(&dev); + /* Both restore attempts failed so rp0 LNKCTL is temporarily cleared, but record is retained and queued. */ + assert(rp0.lnkctl == 0x0040); + assert(!list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 1); + assert(queued_works == 1); + rp0.fail_lnkctl_write_on_call = 0; + rp0.fail_lnkctl_write_count = 0; + run_queued_work(); + assert(list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 0); + assert(rp0.lnkctl == 0x0042 && fn0.lnkctl == 0x0042 && fn1.lnkctl == 0x0043); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(rp0.refs == 1 && fn0.refs == 1 && fn1.refs == 1); + + /* R4-2: System resume_noirq / restore_noirq (pci_pm_default_resume_early) wakes deferred unmanaged + core work. */ + RESET_L1SS(false); + aspm_disabled = true; + fn0.current_state = PCI_D3hot; + assert(apple_pcie_prepare(&dev) == 0); + apple_pcie_complete(&dev); + assert(!list_empty(&pcie.hib_l1ss_pending) && queued_works == 0); + pci_pm_default_resume_early(&fn0); + assert(fn0.current_state == PCI_D0 && queued_works == 1); + run_queued_work(); + assert(list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 0); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + + /* R4-3: D3cold 0xffffffff reads in pci_save_aspm_l1ss_state_locked() must not corrupt valid saved timing. */ + RESET_L1SS(false); + ATTACH_LINK(link0, &rp0, &fn0, ALL_L1_STATES, ALL_L1_STATES, ALL_L1_STATES, 0); + pci_save_aspm_l1ss_state(&fn0); + assert(fn0.saved_l1ss.cap.data[0] == 0x28 && fn0.saved_l1ss.cap.data[1] == 0x000000ff); + assert(rp0.saved_l1ss.cap.data[0] == 0x28 && rp0.saved_l1ss.cap.data[1] == 0x000000ff); + fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] = 0xffffffffu; + pci_save_aspm_l1ss_state(&fn0); + assert(fn0.saved_l1ss.cap.data[0] == 0x28 && fn0.saved_l1ss.cap.data[1] == 0x000000ff); + assert(rp0.saved_l1ss.cap.data[0] == 0x28 && rp0.saved_l1ss.cap.data[1] == 0x000000ff); + + /* R4-4: Core-managed pcie_config_aspm_l1ss() child CTL1 write failure unwinds, re-arms, retains, and retries. */ + RESET_L1SS(false); + ATTACH_LINK(link0, &rp0, &fn0, ALL_L1_STATES, ALL_L1_STATES, ALL_L1_STATES, 0); + assert(apple_pcie_prepare(&dev) == 0); + /* During complete(): driver writes child CTL1(clear), CTL2, CTL1(timing) = 3 writes; + * core pcie_config_aspm_l1ss() writes child CTL1(disable) = 4, child CTL1(enable) = 5. */ + fn0.cfg_write_calls = 0; + fn0.fail_cfg_write_on_call = 5; + apple_pcie_complete(&dev); + /* Core unwound parent CTL1, re-armed aspm_sleep_disable, and driver retained record + queued worker. */ + assert((rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] & PCI_L1SS_CTL1_L1SS_MASK) == 0); + assert((fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] & PCI_L1SS_CTL1_L1SS_MASK) == 0); + assert(link0.aspm_sleep_disable == PCIE_LINK_STATE_L1SS); + assert(!list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 1); + assert(queued_works == 1); + fn0.fail_cfg_write_on_call = 0; + run_queued_work(); + assert(list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 0); + assert(link0.aspm_sleep_disable == 0); + assert(link0.aspm_enabled == ALL_L1_STATES && link0.aspm_hw_l1ss == PCIE_LINK_STATE_L1SS); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(rp0.refs == 1 && fn0.refs == 1); + + /* R4-5: Stale saved_l1ss restoring ASPM L1.1 when policy permits plain L1 only forces resync reprogramming. */ + RESET_L1SS(false); + rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] = 0x000000f0; + fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] = 0x000000f0; + ATTACH_LINK(link0, &rp0, &fn0, ALL_L1_STATES, PCIE_LINK_STATE_L1, PCIE_LINK_STATE_L1, 0); + rp0.saved_l1ss.cap.data[0] = 0x28; + rp0.saved_l1ss.cap.data[1] = 0x000000f0 | PCI_L1SS_CTL1_ASPM_L1_1; + fn0.saved_l1ss.cap.data[0] = 0x28; + fn0.saved_l1ss.cap.data[1] = 0x000000f0 | PCI_L1SS_CTL1_ASPM_L1_1; + queued_works = 0; + atomic_ctx_depth = 1; + pci_restore_aspm_l1ss_state(&fn0); + atomic_ctx_depth = 0; + assert(rp0.aspm_l1ss_resync == 1 && rp0.aspm_pcipm_deferred == 1 && queued_works == 1); + assert((rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] & PCI_L1SS_CTL1_L1SS_MASK) == PCI_L1SS_CTL1_ASPM_L1_1); + run_queued_work(); + assert(rp0.aspm_l1ss_resync == 0 && rp0.aspm_pcipm_deferred == 0); + assert(link0.aspm_enabled == PCIE_LINK_STATE_L1 && link0.aspm_hw_l1ss == 0); + assert((rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] & PCI_L1SS_CTL1_L1SS_MASK) == 0); + assert((fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] & PCI_L1SS_CTL1_L1SS_MASK) == 0); + + /* R5-1: D3hot policy recheck after atomic restore masked PCI-PM must NOT re-enable PCI-PM in D3hot. */ + RESET_L1SS(false); + ATTACH_LINK(link0, &rp0, &fn0, ALL_L1_STATES, ALL_L1_STATES, ALL_L1_STATES, 0); + pci_save_aspm_l1ss_state(&fn0); + fn0.current_state = PCI_D3hot; + atomic_ctx_depth = 1; + pci_restore_aspm_l1ss_state(&fn0); + atomic_ctx_depth = 0; + assert(rp0.aspm_l1ss_resync == 1 && rp0.aspm_pcipm_deferred == 1); + assert((rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] & APPLE_PCIE_L1SS_PCIPM_MASK) == 0); + assert((fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] & APPLE_PCIE_L1SS_PCIPM_MASK) == 0); + /* Policy recheck while fn0 is still in D3hot must keep PCI-PM off and keep aspm_pcipm_deferred armed. */ + pcie_aspm_pm_state_change(&rp0, false); + assert((rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] & APPLE_PCIE_L1SS_PCIPM_MASK) == 0); + assert((fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] & APPLE_PCIE_L1SS_PCIPM_MASK) == 0); + assert(rp0.aspm_pcipm_deferred == 1); + /* Once fn0 enters D0, deferred D0 notification restores PCI-PM L1.1/L1.2 cleanly. */ + queued_works = 0; + pci_pm_default_resume_early(&fn0); + assert(fn0.current_state == PCI_D0 && queued_works == 1); + run_queued_work(); + assert(rp0.aspm_pcipm_deferred == 0 && link0.aspm_enabled == ALL_L1_STATES); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + + /* R5-2: Transient CTL1 error in deferred D0 worker preserves aspm_pcipm_deferred for the next D0 notify. */ + RESET_L1SS(false); + ATTACH_LINK(link0, &rp0, &fn0, ALL_L1_STATES, ALL_L1_STATES, ALL_L1_STATES, 0); + assert(apple_pcie_prepare(&dev) == 0); + fn0.current_state = PCI_D3hot; + apple_pcie_complete(&dev); + assert(list_empty(&pcie.hib_l1ss_pending) && rp0.aspm_pcipm_deferred == 1); + queued_works = 0; + pci_pm_default_resume_early(&fn0); + assert(fn0.current_state == PCI_D0 && queued_works == 1); + /* Inject transient failure on child CTL1 enable write during deferred D0 worker. */ + fn0.cfg_write_calls = 0; + fn0.fail_cfg_write_on_call = 2; /* 1=disable, 2=enable */ + run_queued_work(); + assert(rp0.aspm_l1ss_resync == 1 && rp0.aspm_pcipm_deferred == 1); + assert((rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] & PCI_L1SS_CTL1_L1SS_MASK) == 0); + assert((fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] & PCI_L1SS_CTL1_L1SS_MASK) == 0); + /* Second D0 notification must still trigger worker and succeed once error clears. */ + fn0.fail_cfg_write_on_call = 0; + queued_works = 0; + pci_pm_default_resume_early(&fn0); + assert(queued_works == 1); + run_queued_work(); + assert(rp0.aspm_pcipm_deferred == 0 && rp0.aspm_l1ss_resync == 0); + assert(link0.aspm_enabled == ALL_L1_STATES && link0.aspm_hw_l1ss == PCIE_LINK_STATE_L1SS); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + + /* R5-3: Core LNKCTL double-failure after pcie_config_aspm_l1ss() succeeded unwinds CTL1 child-first under suppression. */ + RESET_L1SS(false); + ATTACH_LINK(link0, &rp0, &fn0, ALL_L1_STATES, ALL_L1_STATES, ALL_L1_STATES, 0); + assert(apple_pcie_prepare(&dev) == 0); + /* During complete(): driver writes rp0 LNKCTL 2x (clear+restore); core writes rp0 LNKCTL clear (3), restore (4), retry (5). */ + rp0.lnkctl_write_calls = 0; + rp0.fail_lnkctl_write_on_call = 4; + rp0.fail_lnkctl_write_count = 2; + apple_pcie_complete(&dev); + /* Both CTL1 registers must be unwound to 0 while aspm_sleep_disable is re-armed, and driver retains record. */ + assert((rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] & PCI_L1SS_CTL1_L1SS_MASK) == 0); + assert((fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] & PCI_L1SS_CTL1_L1SS_MASK) == 0); + assert(link0.aspm_sleep_disable == PCIE_LINK_STATE_L1SS); + assert(!list_empty(&pcie.hib_l1ss_pending) && queued_works == 1); + rp0.fail_lnkctl_write_on_call = 0; + rp0.fail_lnkctl_write_count = 0; + run_queued_work(); + assert(list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 0); + assert(link0.aspm_sleep_disable == 0 && link0.aspm_enabled == ALL_L1_STATES); + assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); + assert(rp0.lnkctl == 0x0042 && fn0.lnkctl == 0x0042); + + /* R6-1: Preserve aspm_l1ss_resync across sleep_end() retry when endpoint enters D3hot after LNKCTL failure. */ + RESET_L1SS(false); + ATTACH_LINK(link0, &rp0, &fn0, + PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_L1_2_PCIPM, + PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_L1_2_PCIPM, + PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_L1_2_PCIPM, 0); + assert(apple_pcie_prepare(&dev) == 0); + /* Fail core's parent LNKCTL restore (calls 4, 5, and unwind restore call 7) so rp0 ends at 0x0040. */ + rp0.lnkctl_write_calls = 0; + rp0.fail_lnkctl_write_on_call = 4; + rp0.fail_lnkctl_write_count = 4; /* 4=restore, 5=retry, 6=unwind clear, 7=unwind restore */ + apple_pcie_complete(&dev); + assert(rp0.lnkctl == 0x0040 && rp0.aspm_l1ss_resync == 1); + assert(!list_empty(&pcie.hib_l1ss_pending) && queued_works == 1); + /* Endpoint enters D3hot before worker retry; masking PCI-PM leaves state == PCIE_LINK_STATE_L1 == aspm_enabled. */ + rp0.fail_lnkctl_write_on_call = 0; + rp0.fail_lnkctl_write_count = 0; + fn0.current_state = PCI_D3hot; + run_queued_work(); + assert(list_empty(&pcie.hib_l1ss_pending) && atomic_read(&pcie.hib_l1ss_pending_cnt) == 0); + assert(rp0.lnkctl == 0x0042 && fn0.lnkctl == 0x0042); + assert(rp0.aspm_pcipm_deferred == 1 && rp0.aspm_l1ss_resync == 0); + /* Returning to D0 completes PCI-PM L1.2 enable. */ + queued_works = 0; + pci_pm_default_resume_early(&fn0); + assert(queued_works == 1); + run_queued_work(); + assert(rp0.aspm_pcipm_deferred == 0); + assert((rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] & PCI_L1SS_CTL1_PCIPM_L1_2) != 0); + assert((fn0.cfg[(0x160 + PCI_L1SS_CTL1) >> 2] & PCI_L1SS_CTL1_PCIPM_L1_2) != 0); + + /* Sustained hotplug churn exhausting the 8-pass pin/drain limit returns -EBUSY (not -EAGAIN). */ { struct pci_bus ch_sub[8]; struct pci_dev ch_rp[8], ch_ep[8]; @@ -1120,6 +2244,8 @@ def function(source, name): list_init(&ch_sub[ci].devices); INIT_DEV(ch_rp[ci], (u8)(0x10 + ci * 8), 0x150, &ch_sub[ci], true, 0x106b, 0x0000005f, 0x14, 0x0042); INIT_DEV(ch_ep[ci], 0x00, 0x160, NULL, false, 0x14e4, 0x0000005f, 0x14, 0x0042); + ch_rp[ci].bus = &root_bus; + ch_ep[ci].bus = &ch_sub[ci]; list_add_tail(&ch_ep[ci].bus_list, &ch_sub[ci].devices); ch_ep[ci].dev.rpm_inflight_cb = hotplug_chain_during_barrier; hotplug_chain_rps[ci] = &ch_rp[ci]; @@ -1130,14 +2256,14 @@ def function(source, name): assert(list_empty(&pcie.hib_l1ss) && list_empty(&pcie.hib_l1ss_rpm)); assert(rp0.dev.rpm_usage == 0 && fn0.dev.rpm_usage == 0 && fn1.dev.rpm_usage == 0); assert(rp0.refs == 1 && fn0.refs == 1 && fn1.refs == 1); - assert(rp0.cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x000000ff); for (int ci = 0; ci < 8; ci++) { assert(ch_rp[ci].dev.rpm_usage == 0 && ch_ep[ci].dev.rpm_usage == 0); assert(ch_rp[ci].refs == 1 && ch_ep[ci].refs == 1); - assert(ch_rp[ci].cfg[(0x150 + PCI_L1SS_CTL1) >> 2] == 0x0000005f); } } + #undef ALL_L1_STATES + #undef ATTACH_LINK #undef RESET_L1SS #undef INIT_DEV @@ -1597,6 +2723,9 @@ def main(): parser.add_argument('--out', type=Path) args = parser.parse_args() pci = (ROOT / 'drivers/pci/controller/pcie-apple.c').read_text() + aspm = (ROOT / 'drivers/pci/pcie/aspm.c').read_text() + pci_core = (ROOT / 'drivers/pci/pci.c').read_text() + pci_drv = (ROOT / 'drivers/pci/pci-driver.c').read_text() acio = (ROOT / 'drivers/thunderbolt/apple.c').read_text() defines = '\n'.join(re.findall(r'^#define (?:[^\n]*\\\n)*[^\n]*$', pci, re.M)) info = pci[pci.index('struct hw_info {'):pci.index('static const struct of_device_id apple_pcie_of_match[];')] @@ -1606,9 +2735,62 @@ def main(): function(acio, 'apple_cio_pciec_eligible'), function(acio, 'apple_cio_pcie_tunnel_is_preinitialized'))) code = FIXTURE.replace('/* PRODUCTION */', production) + aspm_struct = re.search(r'^struct pcie_link_state \{.*?\n\};', aspm, re.M | re.S)[0] + aspm_helpers = ''' +static int policy_to_aspm_state(struct pcie_link_state *link) +{ + return link->aspm_default; +} +static void pcie_update_aspm_capable(struct pcie_link_state *root) +{ + /* A link freed before aspm_lock was taken has no root left. */ + assert(root && aspm_lock.held); +} +static void pcie_aspm_restore_l1ss_mask(struct pci_dev *parent, + struct pci_dev *pdev, + u32 *pl_ctl1, u32 *cl_ctl1); +static void pcie_aspm_restore_l1ss_sync(struct pci_dev *parent, + struct pci_dev *pdev, + u32 pl_ctl1, u32 cl_ctl1); +static void pcie_aspm_pm_state_change_deferred(struct pci_dev *pdev); +static void apple_pcie_hib_l1ss_work_fn(struct work_struct *work); +''' + aspm_prod = '\n'.join((aspm_struct, aspm_helpers, + *(function(aspm, n) for n in ( + 'pcie_aspm_l1ss_hw_lock_irqsave', + 'pcie_aspm_l1ss_hw_unlock_irqrestore', + 'pci_save_aspm_l1ss_state_locked', + 'pci_save_aspm_l1ss_state', + 'pci_restore_aspm_l1ss_state_locked', + 'pci_restore_aspm_l1ss_state', + 'pcie_aspm_l1ss_ctl1_to_state', + 'pcie_aspm_restore_l1ss_mask', + 'pcie_aspm_restore_l1ss_sync', + 'pcie_config_aspm_dev', + 'pcie_l1ss_ctl1_update', + 'pcie_config_aspm_l1ss', + 'pcie_config_aspm_link_locked', + 'pcie_config_aspm_link', + 'pcie_config_aspm_path', + 'pcie_aspm_pm_state_change', + 'pcie_aspm_pm_work_fn', + 'pcie_aspm_init_dev', + 'pcie_aspm_pm_state_change_deferred', + 'pcie_aspm_l1ss_hw_exec', + 'pcie_aspm_l1ss_sleep_begin_locked', + 'pcie_aspm_l1ss_sleep_end')), + function(pci_core, 'pci_pm_notify_host'), + function(pci_core, 'pci_pm_notify_state_change'), + function(pci_core, 'pci_pm_notify_resume'), + function(pci_core, 'pci_set_low_power_state'), + function(pci_core, 'pci_save_pcie_state'), + function(pci_core, 'pci_restore_pcie_state'), + function(pci_core, 'pci_pm_reset'), + function(pci_drv, 'pci_pm_default_resume_early'), + function(pci_drv, 'pci_pm_runtime_resume'))) l1ss_structs = pci[pci.index('struct apple_pcie_hib_l1ss {'): pci.index('static int apple_pcie_cfg_read_u32(')] - s2idle_prod = '\n'.join((defines, info, matches, l1ss_structs, + s2idle_prod = '\n'.join((defines, info, matches, aspm_prod, l1ss_structs, *(function(pci, n) for n in ( 'apple_pcie_pd_notify', 'apple_pcie_release_domains', @@ -1628,12 +2810,22 @@ def main(): 'apple_pcie_hib_rpm_add', 'apple_pcie_hib_rpm_pin_cb', 'apple_pcie_hib_rpm_release', + 'apple_pcie_hib_restore_aspm', + 'apple_pcie_link_set_l1ss_hw', 'apple_pcie_link_set_l1ss', + 'apple_pcie_hib_disable_l1ss_hw', + 'apple_pcie_hib_disable_l1ss_link', + 'apple_pcie_hib_restore_l1ss_link', 'apple_pcie_hib_disable_l1ss_cb', 'apple_pcie_hib_free_l1ss', + 'apple_pcie_hib_retain_failed', 'apple_pcie_hib_restore_l1ss_locked', 'apple_pcie_hib_restore_l1ss', - 'apple_pcie_hib_restore_aspm', + 'apple_pcie_hib_l1ss_work_fn', + 'apple_pcie_pm_state_change', + 'apple_pcie_hib_l1ss_pci_notify', + 'apple_pcie_hib_recheck_l1ss_hw', + 'apple_pcie_hib_recheck_l1ss_link', 'apple_pcie_hib_recheck_l1ss_cb', 'apple_pcie_hib_recheck_l1ss', 'apple_pcie_s2r_reinit', @@ -1708,9 +2900,10 @@ def main(): 'if (false)\n' '\t\treturn apple_pcie_hib_recheck_l1ss(pcie);'), 'drop-recheck-aspm-restore': ( - '\tif (ret) {\n' - '\t\tapple_pcie_hib_restore_aspm(parent, &cur);', - '\tif (ret) {'), + '\tif (ret && hw->restore_aspm_on_err)\n' + '\t\tapple_pcie_hib_restore_aspm(parent, saved);', + '\tif (false)\n' + '\t\tapple_pcie_hib_restore_aspm(parent, saved);'), 'drop-recheck-vid-err': ( '\tret = pcibios_err_to_errno(pci_read_config_dword(child, PCI_VENDOR_ID, &id));\n' '\tif (ret) {\n' @@ -1719,35 +2912,16 @@ def main(): '\t}\n' '\tif (id == U32_MAX)\n' '\t\treturn 0;\n\n' - '\tret = apple_pcie_cfg_read_u32(parent, parent->l1ss + PCI_L1SS_CTL2, &pl_ctl2) ?:\n' - '\t apple_pcie_cfg_read_u32(parent, parent->l1ss + PCI_L1SS_CTL1, &pl_ctl1) ?:\n' - '\t apple_pcie_cfg_read_u32(child, child->l1ss + PCI_L1SS_CTL2, &cl_ctl2) ?:\n' - '\t apple_pcie_cfg_read_u32(child, child->l1ss + PCI_L1SS_CTL1, &cl_ctl1);\n' - '\tif (ret) {\n' - '\t\twalk->err = ret;\n' - '\t\treturn 1;\n' - '\t}\n' - '\tif (!((pl_ctl1 | cl_ctl1) & PCI_L1SS_CTL1_L1SS_MASK))\n' - '\t\treturn 0;\n\n' - '\tlist_for_each_entry(entry, &pcie->hib_l1ss, entry) {', + '\trctx = (struct apple_pcie_l1ss_recheck_ctx){', '\tret = pcibios_err_to_errno(pci_read_config_dword(child, PCI_VENDOR_ID, &id));\n' '\tif (ret || id == U32_MAX)\n' '\t\treturn 0;\n\n' - '\tret = apple_pcie_cfg_read_u32(parent, parent->l1ss + PCI_L1SS_CTL2, &pl_ctl2) ?:\n' - '\t apple_pcie_cfg_read_u32(parent, parent->l1ss + PCI_L1SS_CTL1, &pl_ctl1) ?:\n' - '\t apple_pcie_cfg_read_u32(child, child->l1ss + PCI_L1SS_CTL2, &cl_ctl2) ?:\n' - '\t apple_pcie_cfg_read_u32(child, child->l1ss + PCI_L1SS_CTL1, &cl_ctl1);\n' - '\tif (ret) {\n' - '\t\twalk->err = ret;\n' - '\t\treturn 1;\n' - '\t}\n' - '\tif (!((pl_ctl1 | cl_ctl1) & PCI_L1SS_CTL1_L1SS_MASK))\n' - '\t\treturn 0;\n\n' - '\tlist_for_each_entry(entry, &pcie->hib_l1ss, entry) {'), + '\trctx = (struct apple_pcie_l1ss_recheck_ctx){'), 'drop-pcipm-d0-mask': ( - 'if (parent->current_state != PCI_D0 ||\n' - '\t\t child->current_state != PCI_D0) {', - 'if (false) {'), + '\t\t\tif (((pl_ctl1 | cl_ctl1) & APPLE_PCIE_L1SS_PCIPM_MASK) &&\n' + '\t\t\t (READ_ONCE(parent->current_state) != PCI_D0 ||\n' + '\t\t\t READ_ONCE(child->current_state) != PCI_D0)) {', + '\t\t\tif (false) {'), 'drop-live-ctl1-clear': ( 'ret = apple_pcie_cfg_write_u32(child, child->l1ss + PCI_L1SS_CTL1,\n' '\t\t\t\t\t cur_cl_ctl1 & ~PCI_L1SS_CTL1_L1SS_MASK);', @@ -1766,21 +2940,267 @@ def main(): '\tapple_pcie_hib_restore_l1ss_locked(pcie, rollback);', '\tapple_pcie_hib_restore_l1ss_locked(pcie, rollback);'), 'drop-restore-link-present': ( - 'if (apple_pcie_hib_link_present(parent, child) &&', - 'if (parent->subordinate &&'), + '\tif (!apple_pcie_hib_link_present(parent, child))\n' + '\t\treturn -ENODEV;', + '\tif (!parent->subordinate)\n' + '\t\treturn -ENODEV;'), + 'drop-restore-vid-err-retain': ( + '\tret = pcibios_err_to_errno(pci_read_config_dword(child, PCI_VENDOR_ID, &id));\n' + '\tif (ret)\n' + '\t\treturn ret == -ENODEV ? -EIO : ret;', + '\tret = pcibios_err_to_errno(pci_read_config_dword(child, PCI_VENDOR_ID, &id));\n' + '\tif (ret)\n' + '\t\treturn -ENODEV;'), + 'drop-config-enodev-remap': ( + '\tret = pcibios_err_to_errno(pci_read_config_dword(child, PCI_VENDOR_ID, &id));\n' + '\tif (ret)\n' + '\t\treturn ret == -ENODEV ? -EIO : ret;\n' + '\tif (id == U32_MAX)', + '\tret = pcibios_err_to_errno(pci_read_config_dword(child, PCI_VENDOR_ID, &id));\n' + '\tif (ret)\n' + '\t\treturn ret;\n' + '\tif (id == U32_MAX)'), + 'drop-d3cold-ff-retain': ( + '\tif (id == U32_MAX)\n' + '\t\treturn -EAGAIN;\n\n' + '\t/*', + '\tif (id == U32_MAX)\n' + '\t\treturn -ENODEV;\n\n' + '\t/*'), + 'drop-d0-timing-requeue': ( + '\t\t\tsmp_mb__after_atomic();\n' + '\t\t\tif (READ_ONCE(saved->parent->current_state) == PCI_D0 &&\n' + '\t\t\t READ_ONCE(saved->child->current_state) == PCI_D0)\n' + '\t\t\t\trequeue_d0 = true;', + '\t\t\t(void)requeue_d0;'), + 'drop-worker-rollback-intent': ( + '\t\tctx = (struct apple_pcie_l1ss_restore_ctx){\n' + '\t\t\t.saved = saved,\n' + '\t\t\t.rollback = saved->rollback_pending,\n' + '\t\t\t.from_work = true,\n' + '\t\t};', + '\t\tctx = (struct apple_pcie_l1ss_restore_ctx){\n' + '\t\t\t.saved = saved,\n' + '\t\t\t.from_work = true,\n' + '\t\t};'), + 'drop-rollback-retain-timing': ( + '\tif (!READ_ONCE(pcie->hib_l1ss_closing)) {\n' + '\t\tsaved->timing_pending = true;\n' + '\t\tif (ctx->rollback)\n' + '\t\t\tsaved->rollback_pending = true;', + '\tif (!ctx->rollback && !READ_ONCE(pcie->hib_l1ss_closing)) {\n' + '\t\tsaved->timing_pending = true;\n' + '\t\tif (ctx->rollback)\n' + '\t\t\tsaved->rollback_pending = true;'), + 'drop-core-d0-check': ( + '\tif (READ_ONCE(parent->current_state) != PCI_D0 ||\n' + '\t READ_ONCE(child->current_state) != PCI_D0) {\n' + '\t\tu32 deferred_state;', + '\tif (false) {\n' + '\t\tu32 deferred_state;'), + 'copy-stale-pcipm-on-d3hot-resync': ( + '\t\tdeferred_state = state & ~PCIE_LINK_STATE_L1_SS_PCIPM;\n' + '\t\tif (wanted & PCIE_LINK_STATE_L1_SS_PCIPM) {', + '\t\tdeferred_state = (state & ~PCIE_LINK_STATE_L1_SS_PCIPM) |\n' + '\t\t\t\t (link->aspm_enabled & PCIE_LINK_STATE_L1_SS_PCIPM);\n' + '\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\tWRITE_ONCE(parent->aspm_pcipm_deferred, 1);', + '\t\tif (false)\n' + '\t\t\tWRITE_ONCE(parent->aspm_pcipm_deferred, 1);'), + 'drop-lnkctl-err-l1ss-unwind': ( + '\t\tif (l1ss_programmed) {', + '\t\tif (false && l1ss_programmed) {'), + 'clear-resync-before-sleep-end-retry': ( + '\t\tlink->aspm_enabled &= ~PCIE_LINK_STATE_L1SS;\n' + '\t\tWRITE_ONCE(link->aspm_hw_l1ss, 0);\n' + '\t\tif (!ret) {', + '\t\tlink->aspm_enabled &= ~PCIE_LINK_STATE_L1SS;\n' + '\t\tWRITE_ONCE(link->aspm_hw_l1ss, 0);\n' + '\t\tWRITE_ONCE(pdev->aspm_l1ss_resync, 0);\n' + '\t\tif (!ret) {'), + 'drop-aspm-sleep-disable-mask': ( + 'state &= (link->aspm_capable & ~link->aspm_disable &\n' + '\t\t ~READ_ONCE(link->aspm_sleep_disable));', + 'state &= (link->aspm_capable & ~link->aspm_disable);'), + 'drop-default-resume-early-notify': ( + '\tpci_pme_restore(pci_dev);\n' + '\tpci_pm_notify_resume(pci_dev);', + '\tpci_pme_restore(pci_dev);'), + 'sync-default-resume-early-notify': ( + '\tpci_pme_restore(pci_dev);\n' + '\tpci_pm_notify_resume(pci_dev);', + '\tpci_pme_restore(pci_dev);\n' + '\tpci_pm_notify_state_change(pci_dev, false);'), + 'prelock-link-read': [ + ('\tstruct pcie_link_state *link;\n\n' + '\t/* Unlocked hint only; pdev->link_state is validated under aspm_lock. */\n' + '\tif (aspm_disabled || !READ_ONCE(pdev->link_state))', + '\tstruct pcie_link_state *link = pdev->link_state;\n\n' + '\tif (aspm_disabled || !link)'), + ('\t/* free_link_state() clears pdev->link_state under aspm_lock. */\n' + '\tlink = pdev->link_state;\n', + ''), + ], + 'drop-restore-l1ss-sync': ( + '\tpcie_aspm_restore_l1ss_sync(parent, pdev, pl_ctl1 | pl_l1_2_enable,\n' + '\t\t\t\t cl_ctl1 | cl_l1_2_enable);', + ''), + 'drop-restore-l1ss-mask': ( + '\tpcie_aspm_restore_l1ss_mask(parent, pdev, &pl_ctl1, &cl_ctl1);', + ''), + 'drop-deferred-flag-gate': ( + '\tif (aspm_disabled || !READ_ONCE(pdev->aspm_pcipm_deferred))\n' + '\t\treturn;', + '\tif (aspm_disabled || !READ_ONCE(pdev->link_state))\n' + '\t\treturn;'), + 'drop-freezable-wq': ( + '\tif (!queue_work(system_freezable_wq, &pdev->aspm_pm_work))', + '\tif (!queue_work(system_dfl_wq, &pdev->aspm_pm_work))'), + 'clear-sleep-disable-on-timing-err': ( + '\t\t} else {\n' + '\t\t\tWRITE_ONCE(link->aspm_sleep_disable,\n' + '\t\t\t\t READ_ONCE(link->aspm_sleep_disable) |\n' + '\t\t\t\t PCIE_LINK_STATE_L1SS);', + '\t\t} else {\n' + '\t\t\tWRITE_ONCE(link->aspm_sleep_disable,\n' + '\t\t\t\t READ_ONCE(link->aspm_sleep_disable) &\n' + '\t\t\t\t ~PCIE_LINK_STATE_L1SS);'), + 'drop-retain-timing-pending': ( + '\tif (!READ_ONCE(pcie->hib_l1ss_closing)) {\n' + '\t\tsaved->timing_pending = true;', + '\tif (false) {\n' + '\t\tsaved->timing_pending = true;'), + 'drop-notify-purge-hib-l1ss': ( + '\tlist_for_each_entry_safe(saved, tmp, &pcie->hib_l1ss, entry) {\n' + '\t\tif (saved->parent != pdev && saved->child != pdev)\n' + '\t\t\tcontinue;\n' + '\t\tlist_del_init(&saved->entry);\n' + '\t\tpci_dev_put(saved->child);\n' + '\t\tpci_dev_put(saved->parent);\n' + '\t\tkfree(saved);\n' + '\t}', + ''), + 'drop-work-rescan-lock': ( + '\tpci_lock_rescan_remove();\n' + '\tfor (;;) {\n' + '\t\tstruct pci_dev *parent, *child;\n' + '\t\tstruct apple_pcie_l1ss_restore_ctx ctx;\n\n' + '\t\t/*', + '\tfor (;;) {\n' + '\t\tstruct pci_dev *parent, *child;\n' + '\t\tstruct apple_pcie_l1ss_restore_ctx ctx;\n\n' + '\t\t/*'), + 'drop-retry-pending-cnt': ( + '\t\tsaved = list_first_entry_or_null(&pcie->hib_l1ss_pending,\n' + '\t\t\t\t\t\t struct apple_pcie_hib_l1ss,\n' + '\t\t\t\t\t\t entry);\n' + '\t\tif (saved)\n' + '\t\t\tlist_del_init(&saved->entry);\n' + '\t\tmutex_unlock(&pcie->hib_l1ss_lock);', + '\t\tsaved = list_first_entry_or_null(&pcie->hib_l1ss_pending,\n' + '\t\t\t\t\t\t struct apple_pcie_hib_l1ss,\n' + '\t\t\t\t\t\t entry);\n' + '\t\tif (saved) {\n' + '\t\t\tlist_del_init(&saved->entry);\n' + '\t\t\tatomic_dec(&pcie->hib_l1ss_pending_cnt);\n' + '\t\t}\n' + '\t\tmutex_unlock(&pcie->hib_l1ss_lock);'), + 'drop-pmcsr-hw-lock': ( + '\tflags = pcie_aspm_l1ss_hw_lock_irqsave();\n' + '\tWRITE_ONCE(dev->current_state, state);\n' + '\tpci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);\n' + '\tpcie_aspm_l1ss_hw_unlock_irqrestore(flags);', + '\t(void)flags;\n' + '\tpci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);'), + 'drop-pm-reset-d0-interlock': ( + '\tflags = pcie_aspm_l1ss_hw_lock_irqsave();\n' + '\tWRITE_ONCE(dev->current_state, PCI_D3hot);\n' + '\tcsr &= ~PCI_PM_CTRL_STATE_MASK;\n' + '\tcsr |= PCI_D3hot;\n' + '\tpci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);\n' + '\tpcie_aspm_l1ss_hw_unlock_irqrestore(flags);', + '\t(void)flags;\n' + '\tcsr &= ~PCI_PM_CTRL_STATE_MASK;\n' + '\tcsr |= PCI_D3hot;\n' + '\tpci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);'), + 'drop-save-pcie-hw-lock': ( + '\tflags = pcie_aspm_l1ss_hw_lock_irqsave();\n' + '\tpcie_capability_read_word(dev, PCI_EXP_LNKCTL, &cap[i++]);\n' + '\tpci_save_aspm_l1ss_state_locked(dev);\n' + '\tpcie_aspm_l1ss_hw_unlock_irqrestore(flags);', + '\t(void)flags;\n' + '\tpcie_capability_read_word(dev, PCI_EXP_LNKCTL, &cap[i++]);\n' + '\tpci_save_aspm_l1ss_state(dev);'), + 'drop-restore-pcie-hw-lock': ( + '\tpcie_capability_write_word(dev, PCI_EXP_DEVCTL, cap[i++]);\n' + '\tpcie_capability_write_word(dev, PCI_EXP_LNKCTL, cap[i++]);\n' + '\tpcie_aspm_l1ss_hw_unlock_irqrestore(flags);', + '\tpcie_aspm_l1ss_hw_unlock_irqrestore(flags);\n' + '\tpcie_capability_write_word(dev, PCI_EXP_DEVCTL, cap[i++]);\n' + '\tpcie_capability_write_word(dev, PCI_EXP_LNKCTL, cap[i++]);'), + 'mutex-in-save-aspm-l1ss': ( + '\tspin_lock_irqsave(&aspm_l1ss_hw_lock, flags);\n' + '\tpci_save_aspm_l1ss_state_locked(pdev);\n' + '\tspin_unlock_irqrestore(&aspm_l1ss_hw_lock, flags);', + '\t(void)flags;\n' + '\tmutex_lock(&aspm_lock);\n' + '\tpci_save_aspm_l1ss_state_locked(pdev);\n' + '\tmutex_unlock(&aspm_lock);'), + 'drop-disable-hw-lock': ( + '\tret = pcie_aspm_l1ss_hw_exec(child, true, apple_pcie_hib_disable_l1ss_hw,\n' + '\t\t\t\t &dhw);', + '\tret = apple_pcie_hib_disable_l1ss_hw(&dhw);'), 'drop-complete-parent-aspm-retry': ( '\t\tif (r && enable)\n' - '\t\t\tapple_pcie_lnkctl_write(parent, restore_plnkctl);', + '\t\t\tr = apple_pcie_lnkctl_write(parent, restore_plnkctl);', '\t\tif (false)\n' - '\t\t\tapple_pcie_lnkctl_write(parent, restore_plnkctl);'), + '\t\t\tr = apple_pcie_lnkctl_write(parent, restore_plnkctl);'), 'drop-complete-fn-aspm-retry': ( '\t\t\tif (r && enable)\n' - '\t\t\t\tapple_pcie_lnkctl_write(fns[i], restore_fn_lnkctl[i]);', + '\t\t\t\tr = apple_pcie_lnkctl_write(fns[i], restore_fn_lnkctl[i]);', '\t\t\tif (false)\n' - '\t\t\t\tapple_pcie_lnkctl_write(fns[i], restore_fn_lnkctl[i]);'), + '\t\t\t\tr = apple_pcie_lnkctl_write(fns[i], restore_fn_lnkctl[i]);'), + 'drop-complete-aspm-lost-save': ( + '\tif (hw.aspm_lost) {\n' + '\t\tsaved->pl_lnkctl = hw.restore_plnkctl;', + '\tif (false) {\n' + '\t\tsaved->pl_lnkctl = hw.restore_plnkctl;'), + 'drop-save-l1ss-validate': ( + '\t PCI_POSSIBLE_ERROR(cl_ctl2) || PCI_POSSIBLE_ERROR(cl_ctl1) ||\n' + '\t PCI_POSSIBLE_ERROR(pl_ctl2) || PCI_POSSIBLE_ERROR(pl_ctl1))', + '\t false)'), + 'drop-core-enable-err-retain': ( + '\t\tif (ret && ret != -ENODEV && !ctx.keep_pending)\n' + '\t\t\tapple_pcie_hib_retain_failed(pcie, saved, &ctx, ret);\n' + '\t\tif (!ctx.keep_pending) {', + '\t\t(void)ret;\n' + '\t\tif (!ctx.keep_pending) {'), + 'drop-core-l1ss-err-rearm': ( + '\t\t\tif (ret) {\n' + '\t\t\t\tif (ret == -ENODEV)\n' + '\t\t\t\t\tret = -EIO;\n' + '\t\t\t\tWRITE_ONCE(link->aspm_sleep_disable,\n' + '\t\t\t\t\t READ_ONCE(link->aspm_sleep_disable) |\n' + '\t\t\t\t\t PCIE_LINK_STATE_L1SS);\n' + '\t\t\t\tWRITE_ONCE(pdev->aspm_pcipm_deferred, 0);\n' + '\t\t\t}', + '\t\t\tif (false) {\n' + '\t\t\t\tWRITE_ONCE(link->aspm_sleep_disable,\n' + '\t\t\t\t\t READ_ONCE(link->aspm_sleep_disable) |\n' + '\t\t\t\t\t PCIE_LINK_STATE_L1SS);\n' + '\t\t\t\tWRITE_ONCE(pdev->aspm_pcipm_deferred, 0);\n' + '\t\t\t}'), + 'drop-resync-force-reprogram': ( + '\tif (!resync && link->aspm_enabled == state)\n' + '\t\treturn 0;', + '\tif (link->aspm_enabled == state)\n' + '\t\treturn 0;'), 'drop-hib-rpm-get': ( - '\t\t\t\tpci_walk_bus(bridge->bus, apple_pcie_hib_rpm_pin_cb,\n' - '\t\t\t\t\t &walk);', + '\t\t\t\tpci_walk_bus(bridge->bus,\n' + '\t\t\t\t\t apple_pcie_hib_rpm_pin_cb, &walk);', '\t\t\t\t(void)rpm;'), 'drop-hib-rpm-barrier': ( '\t\t\t\t\tif (!rpm->drained) {\n' @@ -1789,24 +3209,46 @@ def main(): '\t\t\t\t\t}', '\t\t\t\t\trpm->drained = true;'), 'drop-hib-rpm-recheck-loop': ( - '\t\t\t\tif (!walk.added) {', - '\t\t\t\tif (tries > 0 || !walk.added) {'), + '\t\t\t\tif (!walk.added)\n' + '\t\t\t\t\tbreak;', + '\t\t\t\tif (tries > 0 || !walk.added)\n' + '\t\t\t\t\tbreak;'), 'exhaust-returns-eagain': ( - '\t\t\tif (tries == 8) {\n' + '\t\t\tif (walk.added) {\n' '\t\t\t\tapple_pcie_hib_rpm_release(pcie);\n' '\t\t\t\tapple_radio_complete(dev);\n' '\t\t\t\treturn -EBUSY;\n' '\t\t\t}', - '\t\t\tif (tries == 8) {\n' + '\t\t\tif (walk.added) {\n' '\t\t\t\tapple_pcie_hib_rpm_release(pcie);\n' '\t\t\t\tapple_radio_complete(dev);\n' '\t\t\t\treturn -EAGAIN;\n' '\t\t\t}'), + 'drop-recheck-keep-snapshot-on-err': ( + '\tif (rhw.had_l1ss) {\n' + '\t\tif (!saved) {', + '\tif (!ret && rhw.had_l1ss) {\n' + '\t\tif (!saved) {'), + 'drop-d0-recheck-after-publish': ( + '\t\t\t/* Pair with smp_mb() in pci_pm_notify_host(). */\n' + '\t\t\tsmp_mb__after_atomic();\n' + '\t\t\tif (READ_ONCE(parent->current_state) == PCI_D0 &&\n' + '\t\t\t READ_ONCE(child->current_state) == PCI_D0)\n' + '\t\t\t\trequeue_d0 = true;', + ''), + 'drop-zero-l1ss-sleep-suppress': ( + '\tif (!((saved->pl_ctl1 | saved->cl_ctl1) & PCI_L1SS_CTL1_L1SS_MASK)) {\n' + '\t\tsaved->active = dhw->core_managed || dhw->pend_rollback;', + '\tif (!((saved->pl_ctl1 | saved->cl_ctl1) & PCI_L1SS_CTL1_L1SS_MASK)) {\n' + '\t\tsaved->active = false;'), } s2idle_cases = {'s2idle-production': s2idle_code} - for name, (old, new) in s2idle_muts.items(): - assert s2idle_code.count(old) == 1, name - s2idle_cases[name] = s2idle_code.replace(old, new) + for name, edits in s2idle_muts.items(): + mutated = s2idle_code + for old, new in ([edits] if isinstance(edits, tuple) else edits): + assert mutated.count(old) == 1, name + mutated = mutated.replace(old, new) + s2idle_cases[name] = mutated with tempfile.TemporaryDirectory(prefix='pciec-policy-') as tmp: out = args.out or Path(tmp) out.mkdir(parents=True, exist_ok=True)