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

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] 6+ messages in thread

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

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

Functions added:
- pci_power_t_get()
- pci_power_t_set()
- pci_power_t_eq()

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 related	[flat|nested] 6+ messages in thread

* [PATCH] PCI: Convert pci.c to pci_power_t helpers
  2026-11-21 12:59 [PATCH v3 0/4] PCI: Add pci_power_t helper functions Habil Eren Türker
  2026-11-21 12:59 ` [PATCH v3 1/4] " Habil Eren Türker
@ 2026-11-21 12:59 ` Habil Eren Türker
  2026-11-21 13:06   ` sashiko-bot
  2026-11-21 12:59 ` [PATCH v3 3/4] PCI: Convert quirks.c " Habil Eren Türker
  2026-11-21 12:59 ` [PATCH v3 4/4] x86/quirks: Use pci_power_t_eq() in apple_airport_reset() Habil Eren Türker
  3 siblings, 1 reply; 6+ messages in thread
From: Habil Eren Türker @ 2026-11-21 12:59 UTC (permalink / raw)
  To: Bjorn Helgaas, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86@kernel.org
  Cc: H . Peter Anvin, andriy.shevchenko@intel.com,
	linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org,
	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 related	[flat|nested] 6+ messages in thread

* [PATCH v3 3/4] PCI: Convert quirks.c to pci_power_t helpers
  2026-11-21 12:59 [PATCH v3 0/4] PCI: Add pci_power_t helper functions Habil Eren Türker
  2026-11-21 12:59 ` [PATCH v3 1/4] " Habil Eren Türker
  2026-11-21 12:59 ` [PATCH] PCI: Convert pci.c to pci_power_t helpers Habil Eren Türker
@ 2026-11-21 12:59 ` Habil Eren Türker
  2026-11-21 12:59 ` [PATCH v3 4/4] x86/quirks: Use pci_power_t_eq() in apple_airport_reset() Habil Eren Türker
  3 siblings, 0 replies; 6+ messages in thread
From: Habil Eren Türker @ 2026-11-21 12:59 UTC (permalink / raw)
  To: Bjorn Helgaas, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86@kernel.org
  Cc: H . Peter Anvin, andriy.shevchenko@intel.com,
	linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org,
	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 related	[flat|nested] 6+ messages in thread

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

* Re: [PATCH] PCI: Convert pci.c to pci_power_t helpers
  2026-11-21 12:59 ` [PATCH] PCI: Convert pci.c to pci_power_t helpers Habil Eren Türker
@ 2026-11-21 13:06   ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-21 13:06 UTC (permalink / raw)
  To: Habil Eren Türker; +Cc: linux-pci@vger.kernel.org

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] PCI: calls to undeclared pci_power_t helpers in
  drivers/pci/pci.c break build
- [Low] PCI: redundant pci_power_t_eq() calls in pci_power_up() instead
  of using state
--

commit fce5ad9abe7af15ce366f015c47398276c333e5f
Author: Habil Eren Türker <habilerenturker@hotmail.com>

PCI: Convert pci.c to pci_power_t helpers

This commit converts Power Management Control/Status register accesses in
drivers/pci/pci.c to use pci_power_t_get(), pci_power_t_set(), and
pci_power_t_eq() helpers.

> diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
> index 8a0d9d4c71ed4..f947458391a83 100644
> --- a/drivers/pci/pci.c
> +++ b/drivers/pci/pci.c
> @@ -1328,7 +1328,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;
>  	}

[Severity: High]
Are pci_power_t_get(), pci_power_t_set(), and pci_power_t_eq() missing a
prerequisite patch?  None of these helpers are declared in the tree, so
drivers/pci/pci.c fails to compile.

> @@ -1525,12 +1525,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;
>  
>  	/*
> @@ -1540,7 +1539,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",
> @@ -1550,7 +1549,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);
>  	}

[Severity: Low]
This isn't a bug, but local variable state in pci_power_up() is already
declared as pci_power_t.  Assigning state = pci_power_t_get(pmcsr) would
keep the existing state == PCI_D* checks instead of repeating
pci_power_t_eq(pci_power_t_get(pmcsr), ...) four times, matching the
commit message note about keeping direct comparisons on pci_power_t
values.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261121125811.50315-3-habilerenturker@hotmail.com?part=1

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

end of thread, other threads:[~2026-11-21 13:06 UTC | newest]

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.