mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Priyank Rathod <rathodpriyank@google.com>
To: Bjorn Helgaas <helgaas@kernel.org>
Cc: "Bjorn Helgaas" <bhelgaas@google.com>,
	"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
	"Lukas Wunner" <lukas@wunner.de>,
	"Manivannan Sadhasivam" <mani@kernel.org>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Shuah Khan" <shuah@kernel.org>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	linux-pci@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Priyank Rathod" <rathodpriyank@google.com>
Subject: [PATCH v8 1/3] PCI/ASPM: Add internal pcie_aspm_inhibit() helper
Date: Wed, 25 Nov 2026 06:31:33 +0000	[thread overview]
Message-ID: <20261125-pcie-link-endpoints-v8-1-19d49c17c05f@google.com> (raw)
In-Reply-To: <20261125-pcie-link-endpoints-v8-0-19d49c17c05f@google.com>

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:

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

Signed-off-by: Priyank Rathod <rathodpriyank@google.com>
---
 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);

-- 
2.56.1.831.g720f3f5937-goog


  reply	other threads:[~2026-11-25  6:31 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  6:31 [PATCH v8 0/3] PCI: Add Lane Margining at the Receiver debugfs interface Priyank Rathod
2026-11-25  6:31 ` Priyank Rathod [this message]
2026-11-25  6:31 ` [PATCH v8 2/3] PCI/LMR: " Priyank Rathod
2026-11-25  6:31 ` [PATCH v8 3/3] selftests/pcie_lmr: Add tests for the Lane Margining " Priyank Rathod

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261125-pcie-link-endpoints-v8-1-19d49c17c05f@google.com \
    --to=rathodpriyank@google.com \
    --cc=bhelgaas@google.com \
    --cc=corbet@lwn.net \
    --cc=helgaas@kernel.org \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=lukas@wunner.de \
    --cc=mani@kernel.org \
    --cc=rdunlap@infradead.org \
    --cc=shuah@kernel.org \
    --cc=skhan@linuxfoundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®