[PATCH v2 1/4] PCI: Add pci_power_t helper functions

From: Habil Eren Türker

Date: Thu Nov 19 2026 - 09:36:09 EST


Add helper functions to extract, set and compare PCI power
states in the Power Management Control/Status register.

This follows the discussion with Andy Shevchenko and
Bjorn Helgaas in response to:

"[PATCH] x86/quirks: Cast PCI_D0 to u16 in apple_airport_reset()"
https://lore.kernel.org/lkml/20261118161559.63617-1-habilerenturker@xxxxxxxxxxx/

Signed-off-by: Habil Eren Türker <habilerenturker@xxxxxxxxxxx>
---
include/linux/pci.h | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)

diff --git a/include/linux/pci.h b/include/linux/pci.h
index aa3765a20a96..6efd13e760d7 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -43,6 +43,7 @@
#include <uapi/linux/pci.h>

#include <linux/pci_ids.h>
+#include <linux/bitfield.h>

#define PCI_STATUS_ERROR_BITS (PCI_STATUS_DETECTED_PARITY | \
PCI_STATUS_SIG_SYSTEM_ERROR | \
@@ -2953,4 +2954,31 @@ void pci_uevent_ers(struct pci_dev *pdev, enum pci_ers_result err_type);
WARN_ONCE(condition, "%s %s: " fmt, \
dev_driver_string(&(pdev)->dev), pci_name(pdev), ##arg)

+/* Helper functions for PCI power management */
+static inline pci_power_t pci_power_t_get(u32 csr)
+{
+ return (pci_power_t __force)FIELD_GET(PCI_PM_CTRL_STATE_MASK, csr);
+}
+
+static inline void pci_power_t_set(u16 *csr, pci_power_t state)
+{
+ *csr &= ~PCI_PM_CTRL_STATE_MASK;
+ *csr |= FIELD_PREP(PCI_PM_CTRL_STATE_MASK, (__force u32)state);
+}
+
+static inline bool pci_power_eq(u32 csr, pci_power_t state)
+{
+ return pci_power_t_get(csr) == state;
+}
+
+static inline bool pci_power_gt(u32 csr, pci_power_t state)
+{
+ return pci_power_t_get(csr) > state;
+}
+
+static inline bool pci_power_lt(u32 csr, pci_power_t state)
+{
+ return pci_power_t_get(csr) < state;
+}
+
#endif /* LINUX_PCI_H */
--
2.47.3