Re: [PATCH v8 1/3] PCI/ASPM: Add internal pcie_aspm_inhibit() helper

From: Ilpo Järvinen

Date: Wed Nov 25 2026 - 08:36:22 EST


On Wed, 25 Nov 2026, Priyank Rathod wrote:

> Internal PCIe services such as Lane Margining at the Receiver need to
> temporarily keep ASPM disabled on a link and restore its prior policy
> afterward without clobbering link->aspm_disable, link->aspm_default,
> L1 PM Substates, or Clock PM state.
>
> pci_disable_link_state() permanently sets link->aspm_disable, and
> pci_enable_link_state() intentionally refuses to clear it. Introduce a
> PCI-internal, reference-counted pcie_aspm_inhibit() helper:

As you've discovered, pci_disable_link_state() and pci_enable_link_state()
are not symmetric pairs, which is problematic for some use cases like
this one.

There's a series to fix that and to me it looks a prerequisite for this
LMR series so you shouldn't be duplicate that effort with this inhibit
thing.

Perhaps help in reviewing that ASPM series, and/or, if the work has
stalled with things left to address, ask the previous submitter if you
could take over in finishing that series.

> - Track temporary inhibitors via aspm_inhibit_cnt on struct pcie_link_state
> under pci_bus_sem and aspm_lock.
> - Force state = 0 in pcie_config_aspm_link() whenever aspm_inhibit_cnt is
> non-zero, so concurrent policy or sysfs changes still update the saved
> state cleanly and take effect once all inhibitors release.
> - Return -EPERM rather than overriding firmware-enabled ASPM when the OS
> lacks ASPM control, and return 0 when ASPM support is disabled.

Generally, don't describe things that are not real lists using bullet
points (LLM tend to overuse bullets).

--
i.

>
> Signed-off-by: Priyank Rathod <rathodpriyank@xxxxxxxxxx>
> ---
> drivers/pci/pci.h | 6 +++++
> drivers/pci/pcie/aspm.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 76 insertions(+)
>
> diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
> index 77efa4c5c960..e24a6dbf48ce 100644
> --- a/drivers/pci/pci.h
> +++ b/drivers/pci/pci.h
> @@ -1150,6 +1150,7 @@ 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);
> +int pcie_aspm_inhibit(struct pci_dev *pdev, bool inhibit);
> #else
> static inline void pcie_aspm_remove_cap(struct pci_dev *pdev, u32 lnkcap) { }
> static inline void pcie_aspm_init_link_state(struct pci_dev *pdev) { }
> @@ -1163,6 +1164,11 @@ static inline void pcie_encode_t_power_on(u32 t_power_on_us, u8 *scale, u8 *valu
> *scale = 0;
> *value = 0;
> }
> +
> +static inline int pcie_aspm_inhibit(struct pci_dev *pdev, bool inhibit)
> +{
> + return 0;
> +}
> #endif
>
> #ifdef CONFIG_PCIE_ECRC
> diff --git a/drivers/pci/pcie/aspm.c b/drivers/pci/pcie/aspm.c
> index b039b57f8466..0d43dc517098 100644
> --- a/drivers/pci/pcie/aspm.c
> +++ b/drivers/pci/pcie/aspm.c
> @@ -245,6 +245,8 @@ 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 */
> +
> + unsigned int aspm_inhibit_cnt; /* Reference count of inhibitors */
> };
>
> static bool aspm_disabled, aspm_force;
> @@ -1055,6 +1057,10 @@ static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state)
> /* Enable only the states that were not explicitly disabled */
> state &= (link->aspm_capable & ~link->aspm_disable);
>
> + /* Temporary inhibition overrides configured policy without mutating it */
> + if (link->aspm_inhibit_cnt)
> + state = 0;
> +
> /* Can't enable any substates if L1 is not enabled */
> if (!(state & PCIE_LINK_STATE_L1))
> state &= ~PCIE_LINK_STATE_L1SS;
> @@ -1538,6 +1544,70 @@ static int __pci_disable_link_state(struct pci_dev *pdev, int state, bool locked
> return 0;
> }
>
> +/**
> + * pcie_aspm_inhibit - Temporarily inhibit or re-allow ASPM on a link
> + * @pdev: PCI device below the link (Downstream component of the link)
> + * @inhibit: True to inhibit ASPM, false to release the inhibition
> + *
> + * Maintain a reference count of temporary ASPM inhibitions on @pdev's
> + * upstream link without altering link->aspm_disable, link->aspm_default,
> + * or Clock PM state. On the first inhibitor, ASPM is disabled in LNKCTL
> + * (Downstream component before Upstream component per PCIe r6.0 sec 7.5.3.7).
> + * When the last inhibitor releases, the configured ASPM policy is restored.
> + *
> + * Return: 0 on success, -EINVAL if @pdev has no ASPM link state or on an
> + * unbalanced release, -EBUSY on counter overflow, or -EPERM if the OS
> + * lacks ASPM control while firmware has ASPM enabled on the link.
> + */
> +int pcie_aspm_inhibit(struct pci_dev *pdev, bool inhibit)
> +{
> + struct pcie_link_state *link;
> + int ret = 0;
> +
> + if (!pdev)
> + return -EINVAL;
> +
> + if (!aspm_support_enabled)
> + return 0;
> +
> + down_read(&pci_bus_sem);
> + mutex_lock(&aspm_lock);
> +
> + link = pcie_aspm_get_link(pdev);
> + if (!link) {
> + ret = -EINVAL;
> + goto unlock;
> + }
> +
> + if (inhibit) {
> + if (aspm_disabled && link->aspm_enabled) {
> + pci_warn_once(pdev,
> + "can't inhibit ASPM; OS doesn't have ASPM control\n");
> + ret = -EPERM;
> + goto unlock;
> + }
> + if (link->aspm_inhibit_cnt == UINT_MAX) {
> + ret = -EBUSY;
> + goto unlock;
> + }
> + if (++link->aspm_inhibit_cnt == 1)
> + pcie_config_aspm_link(link, 0);
> + } else {
> + if (WARN_ON_ONCE(!link->aspm_inhibit_cnt)) {
> + ret = -EINVAL;
> + goto unlock;
> + }
> + if (--link->aspm_inhibit_cnt == 0 && !aspm_disabled)
> + pcie_config_aspm_link(link, policy_to_aspm_state(link));
> + }
> +
> +unlock:
> + mutex_unlock(&aspm_lock);
> + up_read(&pci_bus_sem);
> +
> + return ret;
> +}
> +
> int pci_disable_link_state_locked(struct pci_dev *pdev, int state)
> {
> lockdep_assert_held_read(&pci_bus_sem);
>
>