* [PATCH] x86/quirks: Cast PCI_D0 to u16 in apple_airport_reset()
@ 2026-11-18 16:16 Habil Eren Türker
2026-11-18 18:14 ` Bjorn Helgaas
0 siblings, 1 reply; 5+ messages in thread
From: Habil Eren Türker @ 2026-11-18 16:16 UTC (permalink / raw)
To: Bjorn Helgaas, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, x86
Cc: H . Peter Anvin, linux-pci, linux-kernel, Habil Eren Türker
PCI_D0 is defined as pci_power_t, whereas pmcsr & PCI_PM_CTRL_STATE_MASK
evaluates to a u16 integer.
This causes Sparse (make C=1) to report:
warning: restricted pci_power_t degrades to integer
Add (__force u16) casts on PCI_D0 to explicitly eliminate warnings.
Signed-off-by: Habil Eren Türker <habilerenturker@hotmail.com>
---
arch/x86/kernel/early-quirks.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/arch/x86/kernel/early-quirks.c b/arch/x86/kernel/early-quirks.c
index 6b6f32f40cbe..7459a073c74d 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) != PCI_D0) {
+ if ((pmcsr & PCI_PM_CTRL_STATE_MASK) != (__force u16)PCI_D0) {
pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
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) != PCI_D0) {
+ if ((pmcsr & PCI_PM_CTRL_STATE_MASK) != (__force u16)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] 5+ messages in thread* Re: [PATCH] x86/quirks: Cast PCI_D0 to u16 in apple_airport_reset()
2026-11-18 16:16 [PATCH] x86/quirks: Cast PCI_D0 to u16 in apple_airport_reset() Habil Eren Türker
@ 2026-11-18 18:14 ` Bjorn Helgaas
2026-11-19 9:04 ` Andy Shevchenko
0 siblings, 1 reply; 5+ messages in thread
From: Bjorn Helgaas @ 2026-11-18 18:14 UTC (permalink / raw)
To: Habil Eren Türker
Cc: Bjorn Helgaas, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, x86, H . Peter Anvin, Andy Shevchenko, linux-pci,
linux-kernel
[+cc Andy since he's fixed similar issues and may have better ideas]
On Wed, Nov 18, 2026 at 04:16:03PM +0000, Habil Eren Türker wrote:
> PCI_D0 is defined as pci_power_t, whereas pmcsr & PCI_PM_CTRL_STATE_MASK
> evaluates to a u16 integer.
>
> This causes Sparse (make C=1) to report:
> warning: restricted pci_power_t degrades to integer
>
> Add (__force u16) casts on PCI_D0 to explicitly eliminate warnings.
We have a bunch of similar sparse warnings in drivers/pci, but I'm
kind of hesitant to just throw in a cast at each one to avoid the
warning.
About a third of the sparse warnings related to pci_power_t are
similar to this, where we're extracting the power state using
PCI_PM_CTRL_STATE_MASK and assigning to a pci_power_t variable or
comparing for equality with PCI_D0. I think we can fix these with an
accessor something like this (better name ideas welcome):
static inline pci_power_t pci_power_t_get(u32 csr)
{
return (pci_power_t __force) FIELD_GET(PCI_PM_CTRL_STATE_MASK, csr);
}
There are a few more we could fix with an analogous pci_power_t_set()
or similar. Most of the rest are arithmetic comparisons (>, <, >=,
etc).
> Signed-off-by: Habil Eren Türker <habilerenturker@hotmail.com>
> ---
> arch/x86/kernel/early-quirks.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/arch/x86/kernel/early-quirks.c b/arch/x86/kernel/early-quirks.c
> index 6b6f32f40cbe..7459a073c74d 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) != PCI_D0) {
> + if ((pmcsr & PCI_PM_CTRL_STATE_MASK) != (__force u16)PCI_D0) {
> pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
> 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) != PCI_D0) {
> + if ((pmcsr & PCI_PM_CTRL_STATE_MASK) != (__force u16)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] 5+ messages in thread* Re: [PATCH] x86/quirks: Cast PCI_D0 to u16 in apple_airport_reset()
2026-11-18 18:14 ` Bjorn Helgaas
@ 2026-11-19 9:04 ` Andy Shevchenko
2026-11-19 11:21 ` Habil Eren Türker
0 siblings, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2026-11-19 9:04 UTC (permalink / raw)
To: Bjorn Helgaas
Cc: Habil Eren Türker, Bjorn Helgaas, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H . Peter Anvin,
linux-pci, linux-kernel
On Wed, Nov 18, 2026 at 12:14:08PM -0600, Bjorn Helgaas wrote:
> [+cc Andy since he's fixed similar issues and may have better ideas]
Thanks! Yes, bitwise types in sparse are interesting thing, but since we want
to continue using them the easiest way out is to cast to pci_power_t.
> On Wed, Nov 18, 2026 at 04:16:03PM +0000, Habil Eren Türker wrote:
> > PCI_D0 is defined as pci_power_t, whereas pmcsr & PCI_PM_CTRL_STATE_MASK
> > evaluates to a u16 integer.
> >
> > This causes Sparse (make C=1) to report:
> > warning: restricted pci_power_t degrades to integer
> >
> > Add (__force u16) casts on PCI_D0 to explicitly eliminate warnings.
>
> We have a bunch of similar sparse warnings in drivers/pci, but I'm
> kind of hesitant to just throw in a cast at each one to avoid the
> warning.
>
> About a third of the sparse warnings related to pci_power_t are
> similar to this, where we're extracting the power state using
> PCI_PM_CTRL_STATE_MASK and assigning to a pci_power_t variable or
> comparing for equality with PCI_D0. I think we can fix these with an
> accessor something like this (better name ideas welcome):
>
> static inline pci_power_t pci_power_t_get(u32 csr)
> {
> return (pci_power_t __force) FIELD_GET(PCI_PM_CTRL_STATE_MASK, csr);
> }
>
> There are a few more we could fix with an analogous pci_power_t_set()
> or similar. Most of the rest are arithmetic comparisons (>, <, >=,
> etc).
So, we basically need only two (the rest can be derived with negation.
_eq() [equal to] and _gt() [great than]. It may lead to have code in
a better shape from readability point of view.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH] x86/quirks: Cast PCI_D0 to u16 in apple_airport_reset()
2026-11-19 9:04 ` Andy Shevchenko
@ 2026-11-19 11:21 ` Habil Eren Türker
2026-11-19 11:41 ` andriy.shevchenko
0 siblings, 1 reply; 5+ messages in thread
From: Habil Eren Türker @ 2026-11-19 11:21 UTC (permalink / raw)
To: andriy.shevchenko
Cc: bhelgaas, bp, dave.hansen, habilerturker, helgaas, hpa,
linux-kernel, linux-pci, mingo, tglx, x86
Thank you both for the feedback. I agree that helper functions are a much
better approach than spreading (__force u16) casts throughout the code.
These are the main types of warnings (roughly 30 in total) in
drivers/pci, run with `make C=2 drivers/pci/`:
- warning: restricted pci_power_t degrades to integer (~25 instances)
- warning: incorrect type in assignment (different base types)
(~4 instances)
- warning: invalid assignment: |= (~3 instances)
The four helpers pci_power_t_get(), pci_power_t_set(), pci_power_eq()
and pci_power_gt() would cover all three groups.
I will prepare a v2 patch series that adds the helpers to
include/linux/pci.h and converts the call sites in drivers/pci/ and
arch/x86/kernel/early-quirks.c:
- Patch 1: add the helpers
- Patches 2-N: convert the call sites
Regards,
Türker
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] x86/quirks: Cast PCI_D0 to u16 in apple_airport_reset()
2026-11-19 11:21 ` Habil Eren Türker
@ 2026-11-19 11:41 ` andriy.shevchenko
0 siblings, 0 replies; 5+ messages in thread
From: andriy.shevchenko @ 2026-11-19 11:41 UTC (permalink / raw)
To: Habil Eren Türker
Cc: bhelgaas, bp, dave.hansen, habilerturker, helgaas, hpa,
linux-kernel, linux-pci, mingo, tglx, x86
On Thu, Nov 19, 2026 at 11:21:25AM +0000, Habil Eren Türker wrote:
> Thank you both for the feedback. I agree that helper functions are a much
> better approach than spreading (__force u16) casts throughout the code.
>
> These are the main types of warnings (roughly 30 in total) in
> drivers/pci, run with `make C=2 drivers/pci/`:
>
> - warning: restricted pci_power_t degrades to integer (~25 instances)
> - warning: incorrect type in assignment (different base types)
> (~4 instances)
> - warning: invalid assignment: |= (~3 instances)
>
> The four helpers pci_power_t_get(), pci_power_t_set(), pci_power_eq()
> and pci_power_gt() would cover all three groups.
For the completeness the _lt() also needed, with that we will cover all
<, >, <=, and >=.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-19 11:41 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 16:16 [PATCH] x86/quirks: Cast PCI_D0 to u16 in apple_airport_reset() Habil Eren Türker
2026-11-18 18:14 ` Bjorn Helgaas
2026-11-19 9:04 ` Andy Shevchenko
2026-11-19 11:21 ` Habil Eren Türker
2026-11-19 11:41 ` andriy.shevchenko
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®