Skip to contentSkip to conversation

PCI: brcmstb: Use BIT() as __GENMASK() is for internal use only

PATCH Applied Andy Shevchenko 8 participants 15 messages Nov 15, 2021 – Dec 1, 2021

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 PCI: brcmstb: Use BIT() as __GENMASK() is for internal use only

Use BIT() as __GENMASK() is for internal use only. The rationale of switching to BIT() is to provide better generated code. The GENMASK() against non-constant numbers may produce an ugly assembler code. On contrary the BIT() is simply converted to corresponding shift operation.

Note, it's the only user of __GENMASK() in the kernel outside of its own realm.

Fixes 3baec684a531 ("PCI: brcmstb: Accommodate MSI for older chips")

---
v2: switched to BIT() and elaborated why, hence not included tag

 drivers/pci/controller/pcie-brcmstb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/controller/pcie-brcmstb.c +1 -1
index 1fc7bd49a7ad..0c49fc65792c 100644
@@ -619,7 +619,7 @@ static void brcm_msi_remove(struct brcm_pcie *pcie)
 
 static void brcm_msi_set_regs(struct brcm_msi *msi)
 {
-	u32 val = __GENMASK(31, msi->legacy_shift);
+	u32 val = ~(BIT(msi->legacy_shift) - 1);
 
 	writel(val, msi->intr_base + MSI_INT_MASK_CLR);
 	writel(val, msi->intr_base + MSI_INT_CLR);
Signature · 7 lines
2.33.0


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Reviewed-by on v2 1/1
#1 lore raw

Hi Andy,

Quoted 20 lines

Use BIT() as __GENMASK() is for internal use only. The rationale of switching to BIT() is to provide better generated code. The GENMASK() against non-constant numbers may produce an ugly assembler code. On contrary the BIT() is simply converted to corresponding shift operation.

Note, it's the only user of __GENMASK() in the kernel outside of its own realm.

Fixes 3baec684a531 ("PCI: brcmstb: Accommodate MSI for older chips")

---
v2: switched to BIT() and elaborated why, hence not included tag

 drivers/pci/controller/pcie-brcmstb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/controller/pcie-brcmstb.c +0 -0
index 1fc7bd49a7ad..0c49fc65792c 100644

drivers/pci/controller/pcie-brcmstb.c static void brcm_msi_remove(struct brcm_pcie *pcie) · line 622

Quoted 5 lines
drivers/pci/controller/pcie-brcmstb.c +1 -1
@@ -619,7 +619,7 @@ static void brcm_msi_remove(struct brcm_pcie *pcie)
 
 static void brcm_msi_set_regs(struct brcm_msi *msi)
 {
-	u32 val = __GENMASK(31, msi->legacy_shift);
+	u32 val = ~(BIT(msi->legacy_shift) - 1);

Thank you! I also personally prefer using BIT() macro here.

Krzysztof

#2 lore raw
In reply to Andy Shevchenko
Quoted 5 lines · Andy Shevchenko

Use BIT() as __GENMASK() is for internal use only. The rationale of switching to BIT() is to provide better generated code. The GENMASK() against non-constant numbers may produce an ugly assembler code. On contrary the BIT() is simply converted to corresponding shift operation.

FWIW, If you care about code quality and want the compiler to do the obvious thing, why not specify it as the obvious thing:

	u32 val = ~0 << msi->legacy_shift;

Personally I don't think that abusing BIT() in the context of setting multiple bits is any better than abusing __GENMASK()...

Robin.

Quoted 23 lines

Note, it's the only user of __GENMASK() in the kernel outside of its own realm.

Fixes 3baec684a531 ("PCI: brcmstb: Accommodate MSI for older chips")

---
v2: switched to BIT() and elaborated why, hence not included tag

  drivers/pci/controller/pcie-brcmstb.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/controller/pcie-brcmstb.c +1 -1
index 1fc7bd49a7ad..0c49fc65792c 100644
@@ -619,7 +619,7 @@ static void brcm_msi_remove(struct brcm_pcie *pcie)
 
  static void brcm_msi_set_regs(struct brcm_msi *msi)
  {
-	u32 val = __GENMASK(31, msi->legacy_shift);
+	u32 val = ~(BIT(msi->legacy_shift) - 1);
 
  	writel(val, msi->intr_base + MSI_INT_MASK_CLR);
  	writel(val, msi->intr_base + MSI_INT_CLR);
#3 lore raw
Quoted 11 lines · Robin Murphy
Quoted 5 lines · Andy Shevchenko

Use BIT() as __GENMASK() is for internal use only. The rationale of switching to BIT() is to provide better generated code. The GENMASK() against non-constant numbers may produce an ugly assembler code. On contrary the BIT() is simply converted to corresponding shift operation.

FWIW, If you care about code quality and want the compiler to do the obvious thing, why not specify it as the obvious thing:

        u32 val = ~0 << msi->legacy_shift;

Obvious and buggy (from the C standard point of view)? :-)

Quoted 2 lines

Personally I don't think that abusing BIT() in the context of setting multiple bits is any better than abusing __GENMASK()...

No, BIT() is not abused here, but __GENMASK().

After all it's up to you, folks, consider that as a bug report.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
Quoted 8 lines · Andy Shevchenko
Quoted 7 lines · Robin Murphy
Quoted 5 lines · Andy Shevchenko

Use BIT() as __GENMASK() is for internal use only. The rationale of switching to BIT() is to provide better generated code. The GENMASK() against non-constant numbers may produce an ugly assembler code. On contrary the BIT() is simply converted to corresponding shift operation.

Quoted 6 lines
Quoted 4 lines

FWIW, If you care about code quality and want the compiler to do the obvious thing, why not specify it as the obvious thing:

        u32 val = ~0 << msi->legacy_shift;

Obvious and buggy (from the C standard point of view)? :-)

Forgot to mention that BIT() is also makes it easy to avoid such mistake.

Quoted 6 lines
Quoted 2 lines

Personally I don't think that abusing BIT() in the context of setting multiple bits is any better than abusing __GENMASK()...

No, BIT() is not abused here, but __GENMASK().

After all it's up to you, folks, consider that as a bug report.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
reviewer
#5 lore raw

+Marc Z

Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Andy Shevchenko
Quoted 7 lines · Robin Murphy
Quoted 5 lines · Andy Shevchenko

Use BIT() as __GENMASK() is for internal use only. The rationale of switching to BIT() is to provide better generated code. The GENMASK() against non-constant numbers may produce an ugly assembler code. On contrary the BIT() is simply converted to corresponding shift operation.

Quoted 15 lines
Quoted 6 lines
Quoted 4 lines

FWIW, If you care about code quality and want the compiler to do the obvious thing, why not specify it as the obvious thing:

        u32 val = ~0 << msi->legacy_shift;

Obvious and buggy (from the C standard point of view)? :-)

Forgot to mention that BIT() is also makes it easy to avoid such mistake.

Quoted 6 lines
Quoted 2 lines

Personally I don't think that abusing BIT() in the context of setting multiple bits is any better than abusing __GENMASK()...

No, BIT() is not abused here, but __GENMASK().

After all it's up to you, folks, consider that as a bug report.

Couldn't we get rid of legacy_shift entirely if the legacy case sets up 'hwirq' as 24-31 rather than 0-7? Though the data for the MSI msg uses the hwirq.

Rob

maintainer
#6 lore raw
Quoted 26 lines · Rob Herring

+Marc Z

Quoted 22 lines · Andy Shevchenko
Quoted 8 lines · Andy Shevchenko
Quoted 7 lines · Robin Murphy
Quoted 5 lines · Andy Shevchenko

Use BIT() as __GENMASK() is for internal use only. The rationale of switching to BIT() is to provide better generated code. The GENMASK() against non-constant numbers may produce an ugly assembler code. On contrary the BIT() is simply converted to corresponding shift operation.

Quoted 6 lines
Quoted 4 lines

FWIW, If you care about code quality and want the compiler to do the obvious thing, why not specify it as the obvious thing:

        u32 val = ~0 << msi->legacy_shift;

Obvious and buggy (from the C standard point of view)? :-)

Forgot to mention that BIT() is also makes it easy to avoid such mistake.

Quoted 3 lines
Quoted 2 lines

Personally I don't think that abusing BIT() in the context of setting multiple bits is any better than abusing __GENMASK()...

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

No, BIT() is not abused here, but __GENMASK().

After all it's up to you, folks, consider that as a bug report.

Couldn't we get rid of legacy_shift entirely if the legacy case sets up 'hwirq' as 24-31 rather than 0-7? Though the data for the MSI msg uses the hwirq.

I personally find it clearer and easier to reason about with the current code though I suppose that with an appropriate xlate method we could sort of set up the hwirq the way we want them to be to avoid any shifting in brcm_pcie_msi_isr().

Signature · 1 line
Florian
#7 lore raw
Quoted 31 lines · Florian Fainelli
Quoted 30 lines · Rob Herring

+Marc Z

Quoted 25 lines · Andy Shevchenko
Quoted 8 lines · Andy Shevchenko
Quoted 7 lines · Robin Murphy
Quoted 5 lines · Andy Shevchenko

Use BIT() as __GENMASK() is for internal use only. The rationale of switching to BIT() is to provide better generated code. The GENMASK() against non-constant numbers may produce an ugly assembler code. On contrary the BIT() is simply converted to corresponding shift operation.

Quoted 6 lines
Quoted 4 lines

FWIW, If you care about code quality and want the compiler to do the obvious thing, why not specify it as the obvious thing:

        u32 val = ~0 << msi->legacy_shift;

Obvious and buggy (from the C standard point of view)? :-)

Forgot to mention that BIT() is also makes it easy to avoid such mistake.

Quoted 6 lines
Quoted 2 lines

Personally I don't think that abusing BIT() in the context of setting multiple bits is any better than abusing __GENMASK()...

No, BIT() is not abused here, but __GENMASK().

After all it's up to you, folks, consider that as a bug report.

Quoted 8 lines
Quoted 3 lines

Couldn't we get rid of legacy_shift entirely if the legacy case sets up 'hwirq' as 24-31 rather than 0-7? Though the data for the MSI msg uses the hwirq.

I personally find it clearer and easier to reason about with the current code though I suppose that with an appropriate xlate method we could sort of set up the hwirq the way we want them to be to avoid any shifting in brcm_pcie_msi_isr().

Something like the following maybe? Completely untested as I don't believe I have a device with that legacy controller available at the moment:

(quoted diff) +0 -0

b/drivers/pci/controller/pcie-brcmstb.c
index 1fc7bd49a7ad..41404b268fa3 100644

drivers/pci/controller/pcie-brcmstb.c +2 -0
@@ -144,6 +144,8 @@
 #define BRCM_INT_PCI_MSI_NR		32
 #define BRCM_INT_PCI_MSI_LEGACY_NR	8
 #define BRCM_INT_PCI_MSI_SHIFT		0
+#define BRCM_INT_PCI_MSI_MASK		GENMASK(BRCM_INT_PCI_MSI_NR - 1, 0)
+#define BRCM_INT_PCI_MSI_LEGACY_MASK	GENMASK(31, 32 -

BRCM_INT_PCI_MSI_LEGACY_NR)

/* MSI target addresses */
#define BRCM_MSI_TARGET_ADDR_LT_4GB 0x0fffffffcULL
@@ -269,8 +271,6 @@ struct brcm_msi {
/* used indicates which MSI interrupts have been alloc'd */
unsigned long used;
bool legacy;
- /* Some chips have MSIs in bits [31..24] of a shared register. */
- int legacy_shift;
int nr; /* No. of MSI available, depends on chip */
/* This is the base pointer for interrupt status/set/clr regs */
void __iomem *intr_base;
@@ -486,7 +486,6 @@ static void brcm_pcie_msi_isr(struct irq_desc *desc)
dev = msi->dev;

status = readl(msi->intr_base + MSI_INT_STATUS);
- status >>= msi->legacy_shift;

for_each_set_bit(bit, &status, msi->nr) {
int ret;
@@ -516,9 +515,8 @@ static int brcm_msi_set_affinity(struct irq_data *irq_data,
static void brcm_msi_ack_irq(struct irq_data *data)
{
struct brcm_msi *msi = irq_data_get_irq_chip_data(data);
- const int shift_amt = data->hwirq + msi->legacy_shift;

- writel(1 << shift_amt, msi->intr_base + MSI_INT_CLR);
+ writel(BIT(data->hwirq), msi->intr_base + MSI_INT_CLR);
}

@@ -573,9 +571,31 @@ static void brcm_irq_domain_free(struct irq_domain *domain,
brcm_msi_free(msi, d->hwirq);
}

+static int brcm_irq_domain_xlate(struct irq_domain *d,
+ struct device_node *node,
+ const u32 *intspec, unsigned int intsize,
+ unsigned long *out_hwirq,
+ unsigned int *out_type)
+{
+ struct brcm_msi *msi = d->host_data;
+
+ if (WARN_ON(intsize < 1))
+ return -EINVAL;
+
+ if (msi->legacy) {
+ *out_hwirq = intspec[0] + BRCM_INT_PCI_MSI_SHIFT;
+ *out_type = IRQ_TYPE_NONE;
+ return 0;
+ }
+
+ return irq_domain_xlate_onecell(d, node, intspec, intsize,
+ out_hwirq, out_type);
+}
+
static const struct irq_domain_ops msi_domain_ops = {
.alloc = brcm_irq_domain_alloc,
.free = brcm_irq_domain_free,
+ .xlate = brcm_irq_domain_xlate,
};

static int brcm_allocate_domains(struct brcm_msi *msi)
@@ -619,7 +639,8 @@ static void brcm_msi_remove(struct brcm_pcie *pcie)

static void brcm_msi_set_regs(struct brcm_msi *msi) {
- u32 val = __GENMASK(31, msi->legacy_shift);
+ u32 val = msi->legacy ? BRCM_INT_PCI_MSI_MASK :
+ BRCM_INT_PCI_MSI_LEGACY_MASK;

writel(val, msi->intr_base + MSI_INT_MASK_CLR); writel(val, msi->intr_base + MSI_INT_CLR);
@@ -664,11 +685,9 @@ static int brcm_pcie_enable_msi(struct brcm_pcie *pcie)
if (msi->legacy) {
msi->intr_base = msi->base + PCIE_INTR2_CPU_BASE; msi->nr = BRCM_INT_PCI_MSI_LEGACY_NR;
- msi->legacy_shift = 24;
} else {
msi->intr_base = msi->base + PCIE_MSI_INTR2_BASE; msi->nr = BRCM_INT_PCI_MSI_NR;
- msi->legacy_shift = 0;
}

ret = brcm_allocate_domains(msi);

Signature · 1 line
Florian
#8 lore raw
Quoted 37 lines · Florian Fainelli
Quoted 35 lines · Florian Fainelli
Quoted 33 lines · Rob Herring

+Marc Z

Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Andy Shevchenko
Quoted 7 lines · Robin Murphy
Quoted 5 lines · Andy Shevchenko

Use BIT() as __GENMASK() is for internal use only. The rationale of switching to BIT() is to provide better generated code. The GENMASK() against non-constant numbers may produce an ugly assembler code. On contrary the BIT() is simply converted to corresponding shift operation.

Quoted 15 lines
Quoted 6 lines
Quoted 4 lines

FWIW, If you care about code quality and want the compiler to do the obvious thing, why not specify it as the obvious thing:

        u32 val = ~0 << msi->legacy_shift;

Obvious and buggy (from the C standard point of view)? :-)

Forgot to mention that BIT() is also makes it easy to avoid such mistake.

Quoted 6 lines
Quoted 2 lines

Personally I don't think that abusing BIT() in the context of setting multiple bits is any better than abusing __GENMASK()...

No, BIT() is not abused here, but __GENMASK().

After all it's up to you, folks, consider that as a bug report.

Couldn't we get rid of legacy_shift entirely if the legacy case sets up 'hwirq' as 24-31 rather than 0-7? Though the data for the MSI msg uses the hwirq.

Quoted 7 lines
Quoted 4 lines

I personally find it clearer and easier to reason about with the current code though I suppose that with an appropriate xlate method we could sort of set up the hwirq the way we want them to be to avoid any shifting in brcm_pcie_msi_isr().

Something like the following maybe? Completely untested as I don't believe I have a device with that legacy controller available at the moment:

Since it gets rid of __GENMASK (ab)use, I'm fine to see it applied at some point.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
reviewer
#9 lore raw
In reply to Florian Fainelli
Quoted 61 lines · Florian Fainelli
Quoted 31 lines · Florian Fainelli
Quoted 30 lines · Rob Herring

+Marc Z

Quoted 25 lines · Andy Shevchenko
Quoted 8 lines · Andy Shevchenko
Quoted 7 lines · Robin Murphy
Quoted 5 lines · Andy Shevchenko

Use BIT() as __GENMASK() is for internal use only. The rationale of switching to BIT() is to provide better generated code. The GENMASK() against non-constant numbers may produce an ugly assembler code. On contrary the BIT() is simply converted to corresponding shift operation.

Quoted 6 lines
Quoted 4 lines

FWIW, If you care about code quality and want the compiler to do the obvious thing, why not specify it as the obvious thing:

        u32 val = ~0 << msi->legacy_shift;

Obvious and buggy (from the C standard point of view)? :-)

Forgot to mention that BIT() is also makes it easy to avoid such mistake.

Quoted 6 lines
Quoted 2 lines

Personally I don't think that abusing BIT() in the context of setting multiple bits is any better than abusing __GENMASK()...

No, BIT() is not abused here, but __GENMASK().

After all it's up to you, folks, consider that as a bug report.

Quoted 8 lines
Quoted 3 lines

Couldn't we get rid of legacy_shift entirely if the legacy case sets up 'hwirq' as 24-31 rather than 0-7? Though the data for the MSI msg uses the hwirq.

I personally find it clearer and easier to reason about with the current code though I suppose that with an appropriate xlate method we could sort of set up the hwirq the way we want them to be to avoid any shifting in brcm_pcie_msi_isr().

Something like the following maybe? Completely untested as I don't believe I have a device with that legacy controller available at the moment:

drivers/pci/controller/pcie-brcmstb.c +2 -0
index 1fc7bd49a7ad..41404b268fa3 100644
@@ -144,6 +144,8 @@
 #define BRCM_INT_PCI_MSI_NR            32
 #define BRCM_INT_PCI_MSI_LEGACY_NR     8
 #define BRCM_INT_PCI_MSI_SHIFT         0
+#define BRCM_INT_PCI_MSI_MASK          GENMASK(BRCM_INT_PCI_MSI_NR - 1, 0)
+#define BRCM_INT_PCI_MSI_LEGACY_MASK   GENMASK(31, 32 -
 BRCM_INT_PCI_MSI_LEGACY_NR)
 
 /* MSI target addresses */

#define BRCM_MSI_TARGET_ADDR_LT_4GB 0x0fffffffcULL

drivers/pci/controller/pcie-brcmstb.c struct brcm_msi { · line 274

Quoted 6 lines
drivers/pci/controller/pcie-brcmstb.c +0 -2
@@ -269,8 +271,6 @@ struct brcm_msi {
        /* used indicates which MSI interrupts have been alloc'd */
        unsigned long           used;
        bool                    legacy;
-       /* Some chips have MSIs in bits [31..24] of a shared register. */
-       int                     legacy_shift;
        int                     nr; /* No. of MSI available, depends on chip */

Can get rid of this too I think.

drivers/pci/controller/pcie-brcmstb.c static void brcm_pcie_msi_isr(struct irq_desc *desc) · line 490

Quoted 9 lines
drivers/pci/controller/pcie-brcmstb.c +0 -1
        /* This is the base pointer for interrupt status/set/clr regs */
        void __iomem            *intr_base;
@@ -486,7 +486,6 @@ static void brcm_pcie_msi_isr(struct irq_desc *desc)
        dev = msi->dev;
 
        status = readl(msi->intr_base + MSI_INT_STATUS);
-       status >>= msi->legacy_shift;
 
        for_each_set_bit(bit, &status, msi->nr) {

'nr' needs to be 32 here.

Quoted 27 lines
drivers/pci/controller/pcie-brcmstb.c +10 -2
                int ret;
@@ -516,9 +515,8 @@ static int brcm_msi_set_affinity(struct irq_data
 *irq_data,
 static void brcm_msi_ack_irq(struct irq_data *data)
 {
        struct brcm_msi *msi = irq_data_get_irq_chip_data(data);
-       const int shift_amt = data->hwirq + msi->legacy_shift;
 
-       writel(1 << shift_amt, msi->intr_base + MSI_INT_CLR);
+       writel(BIT(data->hwirq), msi->intr_base + MSI_INT_CLR);
 }
 
@@ -573,9 +571,31 @@ static void brcm_irq_domain_free(struct irq_domain
 *domain,
        brcm_msi_free(msi, d->hwirq);
 }
 
+static int brcm_irq_domain_xlate(struct irq_domain *d,
+                                struct device_node *node,
+                                const u32 *intspec, unsigned int intsize,
+                                unsigned long *out_hwirq,
+                                unsigned int *out_type)
+{
+       struct brcm_msi *msi = d->host_data;
+
+       if (WARN_ON(intsize < 1))

drivers/pci/controller/pcie-brcmstb.c static void brcm_irq_domain_free(struct irq_domain · line 593

Quoted 10 lines
drivers/pci/controller/pcie-brcmstb.c +10 -0
+               return -EINVAL;
+
+       if (msi->legacy) {
+               *out_hwirq = intspec[0] + BRCM_INT_PCI_MSI_SHIFT;
+               *out_type = IRQ_TYPE_NONE;
+               return 0;
+       }
+
+       return irq_domain_xlate_onecell(d, node, intspec, intsize,
+                                       out_hwirq, out_type);

When would xlate get called? You don't have an intspec from DT. Wouldn't it be enough to set bits 0-23 in 'used' bitmap so that only 24-31 can be allocated?

I'm not really sure though with how all the MSI stuff works.

Quoted 10 lines
drivers/pci/controller/pcie-brcmstb.c +3 -0
+}
+
 static const struct irq_domain_ops msi_domain_ops = {
        .alloc  = brcm_irq_domain_alloc,
        .free   = brcm_irq_domain_free,
+       .xlate  = brcm_irq_domain_xlate,
 };
 

static int brcm_allocate_domains(struct brcm_msi *msi)

drivers/pci/controller/pcie-brcmstb.c static void brcm_msi_remove(struct brcm_pcie *pcie) · line 643

Quoted 6 lines
drivers/pci/controller/pcie-brcmstb.c +2 -1
@@ -619,7 +639,8 @@ static void brcm_msi_remove(struct brcm_pcie *pcie)
 
 static void brcm_msi_set_regs(struct brcm_msi *msi)
 {
-       u32 val = __GENMASK(31, msi->legacy_shift);
+       u32 val = msi->legacy ? BRCM_INT_PCI_MSI_MASK :
+                               BRCM_INT_PCI_MSI_LEGACY_MASK;

Perhaps just change legacy to a mask.

Quoted 18 lines
drivers/pci/controller/pcie-brcmstb.c +0 -3
        writel(val, msi->intr_base + MSI_INT_MASK_CLR);
        writel(val, msi->intr_base + MSI_INT_CLR);
@@ -664,11 +685,9 @@ static int brcm_pcie_enable_msi(struct brcm_pcie *pcie)
        if (msi->legacy) {
                msi->intr_base = msi->base + PCIE_INTR2_CPU_BASE;
                msi->nr = BRCM_INT_PCI_MSI_LEGACY_NR;
-               msi->legacy_shift = 24;
        } else {
                msi->intr_base = msi->base + PCIE_MSI_INTR2_BASE;
                msi->nr = BRCM_INT_PCI_MSI_NR;
-               msi->legacy_shift = 0;
        }
 
        ret = brcm_allocate_domains(msi);
 
--

Florian

#10 lore raw
In reply to Andy Shevchenko
Quoted 5 lines · Andy Shevchenko

Use BIT() as __GENMASK() is for internal use only. The rationale of switching to BIT() is to provide better generated code. The GENMASK() against non-constant numbers may produce an ugly assembler code. On contrary the BIT() is simply converted to corresponding shift operation.

The code is not necessarily any different on ARMv8 as far as I can tell, before:

static void brcm_msi_set_regs(struct brcm_msi *msi)

{
        u32 val = __GENMASK(31, msi->legacy_shift);
      84:       b9406402        ldr     w2, [x0,#100]
      88:       d2800021        mov     x1, #0x1
// #1
      8c:       9ac22021        lsl     x1, x1, x2
      90:       4b0103e1        neg     w1, w1

after:

static void brcm_msi_set_regs(struct brcm_msi *msi)

{
        u32 val = ~(BIT(msi->legacy_shift) - 1);
      84:       b9406402        ldr     w2, [x0,#100]
      88:       d2800021        mov     x1, #0x1
// #1
      8c:       9ac22021        lsl     x1, x1, x2
      90:       4b0103e1        neg     w1, w1

and the usage of BIT() does not make this any clearer.

How about this instead:

(quoted diff) +0 -0

b/drivers/pci/controller/pcie-brcmstb.c
index 1fc7bd49a7ad..f832c07337fa 100644

drivers/pci/controller/pcie-brcmstb.c +2 -0
@@ -144,6 +144,8 @@
 #define BRCM_INT_PCI_MSI_NR            32
 #define BRCM_INT_PCI_MSI_LEGACY_NR     8
 #define BRCM_INT_PCI_MSI_SHIFT         0
+#define BRCM_INT_PCI_MSI_MASK          GENMASK(BRCM_INT_PCI_MSI_NR - 1, 0)
+#define BRCM_INT_PCI_MSI_LEGACY_MASK   GENMASK(31, 32 -

BRCM_INT_PCI_MSI_LEGACY_NR)

/* MSI target addresses */
#define BRCM_MSI_TARGET_ADDR_LT_4GB 0x0fffffffcULL
@@ -619,7 +621,8 @@ static void brcm_msi_remove(struct brcm_pcie *pcie)

static void brcm_msi_set_regs(struct brcm_msi *msi) {
- u32 val = __GENMASK(31, msi->legacy_shift);
+ u32 val = msi->legacy ? BRCM_INT_PCI_MSI_MASK :
+ BRCM_INT_PCI_MSI_LEGACY_MASK;

        writel(val, msi->intr_base + MSI_INT_MASK_CLR);
        writel(val, msi->intr_base + MSI_INT_CLR);
Quoted 24 lines

Note, it's the only user of __GENMASK() in the kernel outside of its own realm.

Fixes 3baec684a531 ("PCI: brcmstb: Accommodate MSI for older chips")

---
v2: switched to BIT() and elaborated why, hence not included tag

 drivers/pci/controller/pcie-brcmstb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/controller/pcie-brcmstb.c +1 -1
index 1fc7bd49a7ad..0c49fc65792c 100644
@@ -619,7 +619,7 @@ static void brcm_msi_remove(struct brcm_pcie *pcie)
 
 static void brcm_msi_set_regs(struct brcm_msi *msi)
 {
-	u32 val = __GENMASK(31, msi->legacy_shift);
+	u32 val = ~(BIT(msi->legacy_shift) - 1);
 
 	writel(val, msi->intr_base + MSI_INT_MASK_CLR);
 	writel(val, msi->intr_base + MSI_INT_CLR);
Signature · 1 line
Florian
#11 lore raw
Quoted 31 lines · Florian Fainelli
Quoted 5 lines · Andy Shevchenko

Use BIT() as __GENMASK() is for internal use only. The rationale of switching to BIT() is to provide better generated code. The GENMASK() against non-constant numbers may produce an ugly assembler code. On contrary the BIT() is simply converted to corresponding shift operation.

The code is not necessarily any different on ARMv8 as far as I can tell, before:

static void brcm_msi_set_regs(struct brcm_msi *msi)

{
        u32 val = __GENMASK(31, msi->legacy_shift);
      84:       b9406402        ldr     w2, [x0,#100]
      88:       d2800021        mov     x1, #0x1
// #1
      8c:       9ac22021        lsl     x1, x1, x2
      90:       4b0103e1        neg     w1, w1

after:

static void brcm_msi_set_regs(struct brcm_msi *msi)

{
        u32 val = ~(BIT(msi->legacy_shift) - 1);
      84:       b9406402        ldr     w2, [x0,#100]
      88:       d2800021        mov     x1, #0x1
// #1
      8c:       9ac22021        lsl     x1, x1, x2
      90:       4b0103e1        neg     w1, w1
Quoted 1 line

and the usage of BIT() does not make this any clearer.

While I disagree on the conclusion it's good that assembly isn't bad. Last time I have tried to compile just GENMASK() excerpts for arm32 the non-constant variants were quite bad. And it was obvious win for BIT() over GENMASK().

Actually it maybe that I have tested something like

GENMASK(C1 + var, C2 + var) vs. GENMASK(C1, C2) << var

that time.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#12 lore raw
In reply to Andy Shevchenko
Quoted 9 lines · Andy Shevchenko

Use BIT() as __GENMASK() is for internal use only. The rationale of switching to BIT() is to provide better generated code. The GENMASK() against non-constant numbers may produce an ugly assembler code. On contrary the BIT() is simply converted to corresponding shift operation.

Note, it's the only user of __GENMASK() in the kernel outside of its own realm.

[...]

Applied to pci/brcmstb, thanks!

[1/1] PCI: brcmstb: Use BIT() as __GENMASK() is for internal use only

      https://git.kernel.org/lpieralisi/pci/c/6ec6eb949d

Thanks,
Lorenzo

#13 lore raw
Quoted 7 lines · Lorenzo Pieralisi
Quoted 6 lines · Andy Shevchenko

Use BIT() as __GENMASK() is for internal use only. The rationale of switching to BIT() is to provide better generated code. The GENMASK() against non-constant numbers may produce an ugly assembler code. On contrary the BIT() is simply converted to corresponding shift operation.

Quoted 8 lines
Quoted 3 lines

Note, it's the only user of __GENMASK() in the kernel outside of its own realm.

[...]

Applied to pci/brcmstb, thanks!

[1/1] PCI: brcmstb: Use BIT() as __GENMASK() is for internal use only

      https://git.kernel.org/lpieralisi/pci/c/6ec6eb949d

Thanks, but there is another patch which changes the logic a bit and cleans up more.

From: Florian Fainelli <[email protected]>
Subject: [PATCH] PCI: brcmstb: Do not use __GENMASK

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#14 lore raw
Quoted 14 lines · Andy Shevchenko
Quoted 13 lines · Lorenzo Pieralisi
Quoted 9 lines · Andy Shevchenko

Use BIT() as __GENMASK() is for internal use only. The rationale of switching to BIT() is to provide better generated code. The GENMASK() against non-constant numbers may produce an ugly assembler code. On contrary the BIT() is simply converted to corresponding shift operation.

Note, it's the only user of __GENMASK() in the kernel outside of its own realm.

[...]

Applied to pci/brcmstb, thanks!

Quoted 8 lines
Quoted 2 lines

[1/1] PCI: brcmstb: Use BIT() as __GENMASK() is for internal use only

      https://git.kernel.org/lpieralisi/pci/c/6ec6eb949d

Thanks, but there is another patch which changes the logic a bit and cleans up more.

From: Florian Fainelli <[email protected]>
Subject: [PATCH] PCI: brcmstb: Do not use __GENMASK

Right,

I pulled that one instead, thanks for the heads-up.

Lorenzo