mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v4 0/4] PCI: Add pci_power_t helper functions
@ 2026-11-21 13:12 Habil Eren Türker
  2026-11-21 13:12 ` [PATCH v4 1/4] " Habil Eren Türker
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Habil Eren Türker @ 2026-11-21 13:12 UTC (permalink / raw)
  To: Bjorn Helgaas, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86
  Cc: H . Peter Anvin, andriy.shevchenko, linux-pci, linux-kernel,
	Habil Eren Türker

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.

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

Changes in v2:
- Add helpers for comparing PCI power states.
- Convert PCI core and quirk code to use the new helpers.
- Address review feedback.

Changes in v3:
- Move helpers' positions to upper lines of the pci.h with
  header include.
- Fix indentation in pci.c.
- Rename function `pci_power_eq()` to `pci_power_t_eq()` for
  API name consistency.
- Rewrite pci_power_t_eq() parameters for comparison of
  two `pci_power_t` type variables.
- Modify `pci_power_t_set` to use `u16 *csr` instead of
  u32.
- Use `FIELD_MODIFY()` in `pci_power_t_set()`.
- Remove `pci_power_gt` and `pci_power_lt`.
- Add resolved Sparse warnings to commit messages.
- Link to v2: https://lore.kernel.org/lkml/cover.1795097970.git.habilerenturker@hotmail.com/

Changes in v4:
- Resend series cleanly with proper subject headers (v3 2/4 had a
  malformed Subject line).
- Link to v3: https://lore.kernel.org/lkml/20261121125811.50315-1-habilerenturker@hotmail.com/

Link: https://lore.kernel.org/lkml/20261118161559.63617-1-habilerenturker@hotmail.com/ [1]

Note: 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_t_eq() in apple_airport_reset()

 arch/x86/kernel/early-quirks.c |  6 +++---
 drivers/pci/pci.c              | 28 ++++++++++++----------------
 drivers/pci/quirks.c           |  2 +-
 include/linux/pci.h            | 17 +++++++++++++++++
 4 files changed, 33 insertions(+), 20 deletions(-)

-- 
2.47.3


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v4 1/4] PCI: Add pci_power_t helper functions
  2026-11-21 13:12 [PATCH v4 0/4] PCI: Add pci_power_t helper functions Habil Eren Türker
@ 2026-11-21 13:12 ` Habil Eren Türker
  2026-11-23 10:09   ` andriy.shevchenko
  2026-11-21 13:12 ` [PATCH v4 2/4] PCI: Convert pci.c to pci_power_t helpers Habil Eren Türker
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Habil Eren Türker @ 2026-11-21 13:12 UTC (permalink / raw)
  To: Bjorn Helgaas, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86
  Cc: H . Peter Anvin, andriy.shevchenko, linux-pci, linux-kernel,
	Habil Eren Türker

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

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

diff --git a/include/linux/pci.h b/include/linux/pci.h
index aa3765a20a96..bfaff0ea6c2d 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -40,6 +40,7 @@
 #include <linux/io.h>
 #include <linux/resource_ext.h>
 #include <linux/msi_api.h>
+#include <linux/bitfield.h>
 #include <uapi/linux/pci.h>
 
 #include <linux/pci_ids.h>
@@ -200,6 +201,22 @@ static inline const char *pci_power_name(pci_power_t state)
 	return pci_power_names[1 + (__force int) state];
 }
 
+/* Helper functions for PCI power management */
+static inline pci_power_t pci_power_t_get(u16 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)
+{
+	FIELD_MODIFY(PCI_PM_CTRL_STATE_MASK, csr, (__force u16)state);
+}
+
+static inline bool pci_power_t_eq(pci_power_t state_a, pci_power_t state_b)
+{
+	return state_a == state_b;
+}
+
 /**
  * typedef pci_channel_state_t
  *
-- 
2.47.3


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v4 2/4] PCI: Convert pci.c to pci_power_t helpers
  2026-11-21 13:12 [PATCH v4 0/4] PCI: Add pci_power_t helper functions Habil Eren Türker
  2026-11-21 13:12 ` [PATCH v4 1/4] " Habil Eren Türker
@ 2026-11-21 13:12 ` Habil Eren Türker
  2026-11-23 10:12   ` andriy.shevchenko
  2026-11-21 13:12 ` [PATCH v4 3/4] PCI: Convert quirks.c " Habil Eren Türker
  2026-11-21 13:12 ` [PATCH v4 4/4] x86/quirks: Use pci_power_t_eq() in apple_airport_reset() Habil Eren Türker
  3 siblings, 1 reply; 7+ messages in thread
From: Habil Eren Türker @ 2026-11-21 13:12 UTC (permalink / raw)
  To: Bjorn Helgaas, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86
  Cc: H . Peter Anvin, andriy.shevchenko, linux-pci, linux-kernel,
	Habil Eren Türker

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

Resolved Sparse warnings:

- drivers/pci/pci.c : warning: incorrect type in assignment
  (different base types) (4 warnings)

- drivers/pci/pci.c : warning: invalid assignment: |= (3 warnings)

Signed-off-by: Habil Eren Türker <habilerenturker@hotmail.com>
---
 drivers/pci/pci.c | 28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)

diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 7c6f2aba8144..d39c897f7735 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -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 = !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET) &&
+			(pci_power_t_eq(pci_power_t_get(pmcsr), PCI_D3hot) ||
+			dev->current_state >= PCI_D3hot);
 
-	need_restore = (state == PCI_D3hot || dev->current_state >= PCI_D3hot) &&
-			!(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET);
-
-	if (state == PCI_D0)
+	if (pci_power_t_eq(pci_power_t_get(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_t_eq(pci_power_t_get(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_t_eq(pci_power_t_get(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, bool
 		return -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, 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;
+	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);
 
-- 
2.47.3


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v4 3/4] PCI: Convert quirks.c to pci_power_t helpers
  2026-11-21 13:12 [PATCH v4 0/4] PCI: Add pci_power_t helper functions Habil Eren Türker
  2026-11-21 13:12 ` [PATCH v4 1/4] " Habil Eren Türker
  2026-11-21 13:12 ` [PATCH v4 2/4] PCI: Convert pci.c to pci_power_t helpers Habil Eren Türker
@ 2026-11-21 13:12 ` Habil Eren Türker
  2026-11-21 13:12 ` [PATCH v4 4/4] x86/quirks: Use pci_power_t_eq() in apple_airport_reset() Habil Eren Türker
  3 siblings, 0 replies; 7+ messages in thread
From: Habil Eren Türker @ 2026-11-21 13:12 UTC (permalink / raw)
  To: Bjorn Helgaas, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86
  Cc: H . Peter Anvin, andriy.shevchenko, linux-pci, linux-kernel,
	Habil Eren Türker

Use pci_power_t helper functions when accessing PCI power
states in PCI quirks.

Resolved Sparse warning:

- drivers/pci/quirks.c : warning: restricted pci_power_t
  degrades to integer

Signed-off-by: Habil Eren Türker <habilerenturker@hotmail.com>
---
 drivers/pci/quirks.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/pci/quirks.c b/drivers/pci/quirks.c
index 7f1f47ea2ee4..4417ed6422a9 100644
--- a/drivers/pci/quirks.c
+++ b/drivers/pci/quirks.c
@@ -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_eq(pci_power_t_get(pmcsr), PCI_D0))
 			return;
 	}
 
-- 
2.47.3


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v4 4/4] x86/quirks: Use pci_power_t_eq() in apple_airport_reset()
  2026-11-21 13:12 [PATCH v4 0/4] PCI: Add pci_power_t helper functions Habil Eren Türker
                   ` (2 preceding siblings ...)
  2026-11-21 13:12 ` [PATCH v4 3/4] PCI: Convert quirks.c " Habil Eren Türker
@ 2026-11-21 13:12 ` Habil Eren Türker
  3 siblings, 0 replies; 7+ messages in thread
From: Habil Eren Türker @ 2026-11-21 13:12 UTC (permalink / raw)
  To: Bjorn Helgaas, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86
  Cc: H . Peter Anvin, andriy.shevchenko, linux-pci, linux-kernel,
	Habil Eren Türker

Use pci_power_t_eq() to check the PCI power state in the
Apple Airport reset quirk.

Resolved Sparse warnings:

- arch/x86/kernel/early-quirks.c : warning: restricted pci_power_t
  degrades to integer (2 warnings)

Signed-off-by: Habil Eren Türker <habilerenturker@hotmail.com>
---
 arch/x86/kernel/early-quirks.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kernel/early-quirks.c b/arch/x86/kernel/early-quirks.c
index 7459a073c74d..b10a73fb4bcd 100644
--- a/arch/x86/kernel/early-quirks.c
+++ b/arch/x86/kernel/early-quirks.c
@@ -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_t_eq(pci_power_t_get(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_t_eq(pci_power_t_get(pmcsr), PCI_D0)) {
 			pr_err("pci 0000:%02x:%02x.%d: Cannot power up Apple AirPort card\n",
 			       bus, slot, func);
 			return;
-- 
2.47.3


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 1/4] PCI: Add pci_power_t helper functions
  2026-11-21 13:12 ` [PATCH v4 1/4] " Habil Eren Türker
@ 2026-11-23 10:09   ` andriy.shevchenko
  0 siblings, 0 replies; 7+ messages in thread
From: andriy.shevchenko @ 2026-11-23 10:09 UTC (permalink / raw)
  To: Habil Eren Türker
  Cc: Bjorn Helgaas, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H . Peter Anvin, linux-pci, linux-kernel

On Sat, Nov 21, 2026 at 01:12:32PM +0000, Habil Eren Türker wrote:
> Add helper functions to extract, set and compare PCI power
> states in the Power Management Control/Status register.

...

> +++ b/include/linux/pci.h
> @@ -40,6 +40,7 @@
>  #include <linux/io.h>
>  #include <linux/resource_ext.h>
>  #include <linux/msi_api.h>
> +#include <linux/bitfield.h>

Can we try to make some order here, please? Id est squeeze the new one to the
above to make the longer (but yet sparse) ordered list.

>  #include <uapi/linux/pci.h>

...

Otherwise LGTM.

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 2/4] PCI: Convert pci.c to pci_power_t helpers
  2026-11-21 13:12 ` [PATCH v4 2/4] PCI: Convert pci.c to pci_power_t helpers Habil Eren Türker
@ 2026-11-23 10:12   ` andriy.shevchenko
  0 siblings, 0 replies; 7+ messages in thread
From: andriy.shevchenko @ 2026-11-23 10:12 UTC (permalink / raw)
  To: Habil Eren Türker
  Cc: Bjorn Helgaas, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H . Peter Anvin, linux-pci, linux-kernel

On Sat, Nov 21, 2026 at 01:12:33PM +0000, Habil Eren Türker wrote:
> Convert 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.
> 
> Resolved Sparse warnings:
> 
> - drivers/pci/pci.c : warning: incorrect type in assignment
>   (different base types) (4 warnings)
> 
> - drivers/pci/pci.c : warning: invalid assignment: |= (3 warnings)

...

> -	state = pmcsr & PCI_PM_CTRL_STATE_MASK;
> +	need_restore = !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET) &&
> +			(pci_power_t_eq(pci_power_t_get(pmcsr), PCI_D3hot) ||
> +			dev->current_state >= PCI_D3hot);

Broken indentation. They should start from the same column as '!' above.

...

The rest looks fine.

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-23 10:12 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-21 13:12 [PATCH v4 0/4] PCI: Add pci_power_t helper functions Habil Eren Türker
2026-11-21 13:12 ` [PATCH v4 1/4] " Habil Eren Türker
2026-11-23 10:09   ` andriy.shevchenko
2026-11-21 13:12 ` [PATCH v4 2/4] PCI: Convert pci.c to pci_power_t helpers Habil Eren Türker
2026-11-23 10:12   ` andriy.shevchenko
2026-11-21 13:12 ` [PATCH v4 3/4] PCI: Convert quirks.c " Habil Eren Türker
2026-11-21 13:12 ` [PATCH v4 4/4] x86/quirks: Use pci_power_t_eq() in apple_airport_reset() Habil Eren Türker

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®