PCI: Add pci_power_t helper functions
v2 cover PCI: Add pci_power_t helper functions
This series introduces helper functions for accessing PCI power states in the Power Management Control/Status register and converts existing PMCSR accesses to use these helpers.
The helpers provide typed access to the PCI power state field, including extraction, setting, and comparison operations. The series also updates the Apple Airport reset quirk to use the new comparison helper.
**Scope**
This series focuses on operations involving raw PCI PMCSR values. Comparisons between values already typed as pci_power_t are intentionally left unchanged, as they do not involve raw register values.
If there is interest in introducing a separate set of helpers for comparisons between pci_power_t values, I can prepare that as a follow-up series.
The changes follow the discussion with Andy Shevchenko and Bjorn Helgaas in response to the original Apple Airport quirk patch.
Changes in v2:
- Add helpers for comparing PCI power states.
- Convert PCI core and quirk code to use the new helpers.
- Address review feedback.
This series is based on the discussion following the original Apple Airport quirk patch:
https://lore.kernel.org/lkml/[email protected]/
LLM assistance was used for editing the cover letter and commit descriptions. All changes were reviewed by the submitter.
Habil Eren Türker (4):
PCI: Add pci_power_t helper functions
PCI: Convert pci.c to pci_power_t helpers
PCI: Convert quirks.c to pci_power_t helpers x86/quirks: Use pci_power_eq() in apple_airport_reset()
arch/x86/kernel/early-quirks.c | 6 +++--- drivers/pci/pci.c | 26 +++++++++++--------------- drivers/pci/quirks.c | 2 +- include/linux/pci.h | 28 ++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 19 deletions(-)
base-commit: c054fccfff7ba1ad9d8913921f5a952bb32513cd
Signature · 1 line
2.47.3
PATCH v2 1/4 PCI: Add pci_power_t helper functions
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/[email protected]/
--- include/linux/pci.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
include/linux/pci.h +28 -0
@@ -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 */
Signature · 1 line
2.47.3
Quoted 8 lines · Habil Eren Türker
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_D0to u16 inapple_airport_reset()"
https://lore.kernel.org/lkml/[email protected]/
Make it a Link tag with reference
This follows the discussion with Andy Shevchenko and Bjorn Helgaas in response to "x86/quirks: Cast PCI_D0 to u16 in apple_airport_reset()" [1].
Quoted 1 line
Signed-off-by Habil Eren Türker <[email protected]>
...
Quoted 7 lines
+++
b/include/linux/pci.h#include <
uapi/linux/pci.h>#include <
linux/pci_ids.h>+#include <
linux/bitfield.h>
Put it upper to the proper group of headers (they are grouped from the generic to custom).
...
include/linux/pci.h void pci_uevent_ers(struct pci_dev *pdev, enum pci_ers_result err_type); · line 2958
Quoted 2 lines
include/linux/pci.h +2
-0+/* Helper functions for PCI power management */ +static inline pci_power_t pci_power_t_get (u32 csr)
Why u32 as it seems the value is 16-bit?
include/linux/pci.h void pci_uevent_ers(struct pci_dev *pdev, enum pci_ers_result err_type); · line 2966
Quoted 8 lines
include/linux/pci.h +8
-0+{ +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);
Why u32 and not u16?
Quoted 6 lines
include/linux/pci.h +6
-0+} + +static inline bool pci_power_eq (u32 csr,pci_power_t state) +{ +return pci_power_t_get (csr) == state; +}
include/linux/pci.h void pci_uevent_ers(struct pci_dev *pdev, enum pci_ers_result err_type); · line 2982
Quoted 10 lines
include/linux/pci.h +10
-0+ +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; +}
Same questions.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 9 lines · On Thu, Nov 19, 2026 at 06:51:06PM +0200, [email protected] wrote:
Quoted 2 lines · Habil Eren Türker
Add helper functions to extract, set and compare PCI power states in the Power Management Control/Status register.
...
include/linux/pci.h void pci_uevent_ers(struct pci_dev *pdev, enum pci_ers_result err_type); · line 2958
Quoted 2 lines
include/linux/pci.h +2
-0+/* Helper functions for PCI power management */ +static inline pci_power_t pci_power_t_get (u32 csr)Why u32 as it seems the value is 16-bit?
I think there's one reader of PCI_PM_CTRL that does a 32-bit read, and I figured a u16->u32 promotion would be better than a u32->u16 truncation. But maybe that reader should be changed to a 16-bit read.
Quoted 4 lines · Bjorn Helgaas
Quoted 3 lines · On Thu, Nov 19, 2026 at 06:51:06PM +0200, [email protected] wrote:
Quoted 2 lines · Habil Eren Türker
Add helper functions to extract, set and compare PCI power states in the Power Management Control/Status register.
...
Quoted 8 lines
Quoted 4 lines
include/linux/pci.h void pci_uevent_ers(struct pci_dev *pdev, enum pci_ers_result err_type); · line 2958
Quoted 2 lines
include/linux/pci.h +2
-0+/* Helper functions for PCI power management */ +static inline pci_power_t pci_power_t_get (u32 csr)Why u32 as it seems the value is 16-bit?
I think there's one reader of
PCI_PM_CTRLthat does a 32-bit read, and I figured au16->u32promotion would be better than au32->u16truncation. But maybe that reader should be changed to a 16-bit read.
Is it specific read on the specific HW that is always done as 32-bit? (Personally I do not believe there is such a requirement to even weirdest
PCI HW. This register is way too standard.)
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 13 lines · On Thu, Nov 19, 2026 at 07:18:44PM +0200, [email protected] wrote:
Quoted 4 lines · Bjorn Helgaas
Quoted 3 lines · On Thu, Nov 19, 2026 at 06:51:06PM +0200, [email protected] wrote:
Quoted 2 lines · Habil Eren Türker
Add helper functions to extract, set and compare PCI power states in the Power Management Control/Status register.
...
Quoted 5 lines
Quoted 4 lines
include/linux/pci.h void pci_uevent_ers(struct pci_dev *pdev, enum pci_ers_result err_type); · line 2958
Quoted 2 lines
include/linux/pci.h +2
-0+/* Helper functions for PCI power management */ +static inline pci_power_t pci_power_t_get (u32 csr)Why u32 as it seems the value is 16-bit?
Quoted 7 lines
Quoted 3 lines
I think there's one reader of
PCI_PM_CTRLthat does a 32-bit read, and I figured au16->u32promotion would be better than au32->u16truncation. But maybe that reader should be changed to a 16-bit read.Is it specific read on the specific HW that is always done as 32-bit? (Personally I do not believe there is such a requirement to even weirdest
PCI HW. This register is way too standard.)
Hi,
The reason why I put u32 is that in drivers/net/ethernet/broadcom/bnx2.c, there is a u32 read:pci_read_config_dword(bp->pdev, bp->pm_cap + PCI_PM_CTRL, &val1);
However, using u16 is much better solution since the PCI core uses u16 reads.
I will change it to u16 in v3.
Thanks for the clarification.
Regards,
Türker
Quoted 8 lines · Habil Eren Türker
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_D0to u16 inapple_airport_reset()"
https://lore.kernel.org/lkml/[email protected]/
Andy had good comments about the style of references like this, but IMO if your cover letter includes links to previous versions of the series, that's enough of a breadcrumb, and previous review comments and suggestions don't need to be explicitly called out.
https://lore.kernel.org/all/[email protected]
is a good example of the previous version link documentation in the cover letter.
include/linux/pci.h void pci_uevent_ers(struct pci_dev *pdev, enum pci_ers_result err_type); · line 2957
Quoted 5 lines
include/linux/pci.h +1
-0@@ -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 */
I think these should go right next to the pci_power_t definitions, e.g., right after pci_power_name(), so they are easy to find when using PCI_D0 etc.
include/linux/pci.h void pci_uevent_ers(struct pci_dev *pdev, enum pci_ers_result err_type); · line 2966
Quoted 9 lines
include/linux/pci.h +9
-0+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);
FIELD_MODIFY().
include/linux/pci.h void pci_uevent_ers(struct pci_dev *pdev, enum pci_ers_result err_type); · line 2976
Quoted 10 lines
include/linux/pci.h +10
-0+} + +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;
I'm not a __bitwise expert, but I'm surprised this works. I expected that __bitwise values would not be ordered, so only testing for equality would be legit.
I do think these should take two pci_power_t values instead of a PMCSR value and a pci_power_t. The extra internal conversion on one argument feels a little bit non-intuitive.
Quoted 11 lines
include/linux/pci.h +7
-0+} + +static inline bool pci_power_lt (u32 csr,pci_power_t state) +{ +return pci_power_t_get (csr) < state; +} +#endif /* LINUX_PCI_H */ Signature · 1 line
2.47.3
Hi Bjorn,
Thanks for the review.
I moved the helper functions next to the pci_power_t definitions, as suggested.
I also changed the comparison helper to take two pci_power_t values instead of a PMCSR value and a pci_power_t.
Regarding the __bitwise comparisons, I removed pci_power_gt() and pci_power_lt(). The current conversions only need equality comparisons, so I kept pci_power_t_eq().
I also simplified the cover letter to include a link to the previous version.
Thanks for the suggestions.
Regards,
Türker
PATCH v2 2/4 PCI: Convert pci.c to pci_power_t helpers
In reply to Habil Eren TürkerConvert PCI core code to use the pci_power_t helper functions when accessing the Power Management Control/Status register.
Keep comparisons between values already typed as pci_power_t as direct typed comparisons.
--- drivers/pci/pci.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-)
drivers/pci/pci.c +11 -15
@@ -1171,7 +1171,7 @@ void pci_update_current_state(struct pci_dev *dev, pci_power_t state) dev->current_state = PCI_D3cold;return ; } - dev->current_state = pmcsr &PCI_PM_CTRL_STATE_MASK ; + dev->current_state =pci_power_t_get (pmcsr); }else { dev->current_state = state; }
@@ -1368,12 +1368,11 @@ int pci_power_up(struct pci_dev *dev)return -EIO ; } - state = pmcsr &PCI_PM_CTRL_STATE_MASK ; - - need_restore = (state == PCI_D3hot || dev->current_state >= PCI_D3hot) && + need_restore = (pci_power_eq (pmcsr, PCI_D3hot) || + dev->current_state >= PCI_D3hot) && !(pmcsr &PCI_PM_CTRL_NO_SOFT_RESET ); -if (state ==PCI_D0 ) +if (pci_power_eq (pmcsr,PCI_D0 )) goto end;/*
@@ -1383,7 +1382,7 @@ int pci_power_up(struct pci_dev *dev)pci_write_config_word (dev, dev->pm_cap +PCI_PM_CTRL ,0 );/* Mandatory transition delays; see PCI PM 1.2. */ -if (state == PCI_D3hot) { +if (pci_power_eq (pmcsr, PCI_D3hot)) {pci_dev_d3_sleep (dev);if (!(pmcsr &PCI_PM_CTRL_NO_SOFT_RESET )) { ret = pci_dev_wait(dev, "power up D3hot->D0uninitialized",
@@ -1393,7 +1392,7 @@ int pci_power_up(struct pci_dev *dev)return -EIO ; } } - }else if (state ==PCI_D2 ) { + }else if (pci_power_eq (pmcsr,PCI_D2 )) {udelay (PCI_PM_D2_DELAY ); }
@@ -1432,7 +1431,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 ; + dev->current_state =pci_power_t_get (pmcsr);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));
@@ -1546,8 +1545,7 @@ static int pci_set_low_power_state(struct pci_dev *dev, pci_power_t state, boolreturn -EIO ; } - pmcsr &= ~PCI_PM_CTRL_STATE_MASK ; - pmcsr |= state; +pci_power_t_set (&pmcsr, state);/* Enter specified state */ pci_write_config_word (dev, dev->pm_cap +PCI_PM_CTRL , pmcsr);
@@ -1559,7 +1557,7 @@ static int pci_set_low_power_state(struct pci_dev *dev, pci_power_t state, booludelay (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 ; + dev->current_state =pci_power_t_get (pmcsr);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),
@@ -4590,13 +4588,11 @@ static int pci_pm_reset(struct pci_dev *dev, bool probe)return ret; } - csr &= ~PCI_PM_CTRL_STATE_MASK ; - csr |= PCI_D3hot; +pci_power_t_set (&csr, PCI_D3hot);pci_write_config_word (dev, dev->pm_cap +PCI_PM_CTRL , csr);pci_dev_d3_sleep (dev); - csr &= ~PCI_PM_CTRL_STATE_MASK ; - csr |=PCI_D0 ; +pci_power_t_set (&csr,PCI_D0 );pci_write_config_word (dev, dev->pm_cap +PCI_PM_CTRL , csr);pci_dev_d3_sleep (dev);
Signature · 1 line
2.47.3
Re: [PATCH v2 2/4] PCI: Convert pci.c to pci_power_t helpers
Quoted 5 lines · Habil Eren Türker
Convert PCI core code to use the
pci_power_thelper functions when accessing the Power Management Control/Status register.Keep comparisons between values already typed as
pci_power_tas direct typed comparisons.
Please, add a couple of examples of the sparse warnings.
...
drivers/pci/pci.c int pci_power_up(struct pci_dev *dev) · line 1373
Quoted 6 lines
drivers/pci/pci.c +2
-3- state = pmcsr &PCI_PM_CTRL_STATE_MASK ; - - need_restore = (state == PCI_D3hot || dev->current_state >= PCI_D3hot) && + need_restore = (pci_power_eq (pmcsr, PCI_D3hot) || + dev->current_state >= PCI_D3hot) && !(pmcsr &PCI_PM_CTRL_NO_SOFT_RESET );
Please, fix indentation.
need_restore = !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET) && (pci_power_eq(pmcsr, PCI_D3hot) || dev->current_state >= PCI_D3hot);
Or create an additional temporary variable.
in_d3 = pci_power_eq(pmcsr, PCI_D3hot) || dev->current_state >= PCI_D3hot; need_restore = in_d3 && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET);
Quoted 3 lines
drivers/pci/pci.c +1
-1-if (state ==PCI_D0 ) +if (pci_power_eq (pmcsr,PCI_D0 )) goto end;
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 2/4] PCI: Convert pci.c to pci_power_t helpers
Hi Andy,
I added resolved Sparse warnings to the commit message.
I also fixed indentation of the need_restore.
Both changes are included in v3.
Thanks for the review and suggestions.
Regards,
Türker
PATCH v2 3/4 PCI: Convert quirks.c to pci_power_t helpers
In reply to Habil Eren TürkerUse pci_power_t helper functions when accessing PCI power states in PCI quirks.
--- drivers/pci/quirks.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/quirks.c +1 -1
@@ -2487,7 +2487,7 @@ static void quirk_e100_interrupt(struct pci_dev *dev)*/ if (dev->pm_cap) {pci_read_config_word (dev, dev->pm_cap +PCI_PM_CTRL , &pmcsr); -if ((pmcsr &PCI_PM_CTRL_STATE_MASK ) !=PCI_D0 ) +if (pci_power_t_get (pmcsr) !=PCI_D0 )return ; }
Signature · 1 line
2.47.3
Re: [PATCH v2 3/4] PCI: Convert quirks.c to pci_power_t helpers
Quoted 2 lines · Habil Eren Türker
Use
pci_power_thelper functions when accessing PCI power states in PCI quirks.
It's a single case, so you may put a oneline sparse warning here.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 3/4] PCI: Convert quirks.c to pci_power_t helpers
Hi Andy,
I added a one-line example of the resolved Sparse warning to the commit message in v3.
Thanks for the suggestion.
Regards,
Türker
PATCH v2 4/4 x86/quirks: Use pci_power_eq() in apple_airport_reset()
In reply to Habil Eren TürkerUse pci_power_eq() to check the PCI power state in the Apple Airport reset quirk.
--- arch/x86/kernel/early-quirks.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/early-quirks.c +3 -3
@@ -644,13 +644,13 @@ static void __init apple_airport_reset(int bus, int slot, int func)/* Card may have been put into PCI_D3hot by grub quirk */ pmcsr =read_pci_config_16 (bus, slot, func,BCM4331_PM_CAP +PCI_PM_CTRL ); -if ((pmcsr &PCI_PM_CTRL_STATE_MASK ) != (__force u16 )PCI_D0 ) { - pmcsr &= ~PCI_PM_CTRL_STATE_MASK ; +if (!pci_power_eq (pmcsr,PCI_D0 )) { +pci_power_t_set (&pmcsr,PCI_D0 );write_pci_config_16 (bus, slot, func,BCM4331_PM_CAP +PCI_PM_CTRL , pmcsr);mdelay (10 ); pmcsr =read_pci_config_16 (bus, slot, func,BCM4331_PM_CAP +PCI_PM_CTRL ); -if ((pmcsr &PCI_PM_CTRL_STATE_MASK ) != (__force u16 )PCI_D0 ) { +if (!pci_power_eq (pmcsr,PCI_D0 )) {pr_err ("pci 0000:%02x:%02x.%d: Cannot power up Apple AirPort card\n" , bus, slot, func);return ;
Signature · 1 line
2.47.3
Re: [PATCH v2 4/4] x86/quirks: Use pci_power_eq() in apple_airport_reset()
Quoted 2 lines · Habil Eren Türker
Use
pci_power_eq()to check the PCI power state in the Apple Airport reset quirk.
...
arch/x86/kernel/early-quirks.c static void __init apple_airport_reset(int bus, int slot, int func) · line 648
Quoted 4 lines
arch/x86/kernel/early-quirks.c +2
-2-if ((pmcsr &PCI_PM_CTRL_STATE_MASK ) != (__force u16 )PCI_D0 ) { - pmcsr &= ~PCI_PM_CTRL_STATE_MASK ; +if (!pci_power_eq (pmcsr,PCI_D0 )) { +pci_power_t_set (&pmcsr,PCI_D0 );
Haven't checked the code, but consider (if enough users) also have
// update state if it wasn't equal to the required static inline bool pci_power_t_update(...)
{
if (pci_power_eq(pmcsr, state))
return false;
pci_power_t_set(&pmcsr, state);
return true;
} if (pci_power_t_update(&pmcsr, PCI_D0)) {Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 4/4] x86/quirks: Use pci_power_eq() in apple_airport_reset()
Hi Andy,
Thanks for the suggestion.
I have considered using pci_power_t_update() helper in the code. However, there aren't many places in the code where this function could be used (there is only one place that is suitable).
Therefore, I believe it's better not to include it.
Regards,
Türker
Re: [PATCH v2 4/4] x86/quirks: Use pci_power_eq() in apple_airport_reset()
Quoted 7 lines · Habil Eren Türker
Hi Andy,
Thanks for the suggestion.
I have considered using
pci_power_t_update()helper in the code. However, there aren't many places in the code where this function could be used (there is only one place that is suitable).
It's one place in _your_ code, but have you checked if there are other potential users elsewhere in the kernel?
Quoted 1 line
Therefore, I believe it's better not to include it.
Signature · 2 lines
With Best Regards, Andy Shevchenko