[PATCH v3 7/8] PCI: Make minimum bridge window alignment reference more obvious

Ilpo Järvinen posted 8 patches 1 year, 4 months ago
[PATCH v3 7/8] PCI: Make minimum bridge window alignment reference more obvious
Posted by Ilpo Järvinen 1 year, 4 months ago
Calculations related to bridge window size contain literal 20 that is
the minimum alignment for a bridge window. Make the code more obvious
by converting the literal 20 to __ffs(SZ_1MB).

Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
---
 drivers/pci/setup-bus.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index 141d6b31959b..bca1df46f19c 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -14,6 +14,7 @@
  *	     tighter packing. Prefetchable range support.
  */
 
+#include <linux/bitops.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
@@ -21,6 +22,7 @@
 #include <linux/errno.h>
 #include <linux/ioport.h>
 #include <linux/cache.h>
+#include <linux/sizes.h>
 #include <linux/slab.h>
 #include <linux/acpi.h>
 #include "pci.h"
@@ -957,7 +959,7 @@ static inline resource_size_t calculate_mem_align(resource_size_t *aligns,
 	for (order = 0; order <= max_order; order++) {
 		resource_size_t align1 = 1;
 
-		align1 <<= (order + 20);
+		align1 <<= (order + __ffs(SZ_1M));
 
 		if (!align)
 			min_align = align1;
@@ -1047,7 +1049,7 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
 			 * resources.
 			 */
 			align = pci_resource_alignment(dev, r);
-			order = __ffs(align) - 20;
+			order = __ffs(align) - __ffs(SZ_1M);
 			if (order < 0)
 				order = 0;
 			if (order >= ARRAY_SIZE(aligns)) {
-- 
2.39.2

Re: [PATCH v3 7/8] PCI: Make minimum bridge window alignment reference more obvious
Posted by Andy Shevchenko 1 year, 4 months ago
On Tue, May 07, 2024 at 01:25:22PM +0300, Ilpo Järvinen wrote:
> Calculations related to bridge window size contain literal 20 that is
> the minimum alignment for a bridge window. Make the code more obvious
> by converting the literal 20 to __ffs(SZ_1MB).

...

> -		align1 <<= (order + 20);
> +		align1 <<= (order + __ffs(SZ_1M));

No need for outer parentheses.

...

> +			order = __ffs(align) - __ffs(SZ_1M);

Yeah, would be nice to have something like

#define bit_distance(a, b)	(ffs(a) - fls(b))

in bitops.h as we have a few users and I have heard about one more coming,
but this is topic to another discussion. (Yuri, just FYI.)

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v3 7/8] PCI: Make minimum bridge window alignment reference more obvious
Posted by Mika Westerberg 1 year, 4 months ago
On Tue, May 07, 2024 at 01:25:22PM +0300, Ilpo Järvinen wrote:
> Calculations related to bridge window size contain literal 20 that is
> the minimum alignment for a bridge window. Make the code more obvious
> by converting the literal 20 to __ffs(SZ_1MB).

I think that's SZ_1M not SZ_1MB :)

> Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>

Looks good, may be even add a #define for this but either way,

Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Re: [PATCH v3 7/8] PCI: Make minimum bridge window alignment reference more obvious
Posted by Ilpo Järvinen 1 year, 4 months ago
On Tue, 7 May 2024, Mika Westerberg wrote:

> On Tue, May 07, 2024 at 01:25:22PM +0300, Ilpo Järvinen wrote:
> > Calculations related to bridge window size contain literal 20 that is
> > the minimum alignment for a bridge window. Make the code more obvious
> > by converting the literal 20 to __ffs(SZ_1MB).
> 
> I think that's SZ_1M not SZ_1MB :)

Of course, that's the only place which is not checked by the compiler so 
it's the place where I type it wrong and forget to use backspace. :-)

> > Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
> 
> Looks good, may be even add a #define for this but either way,

I considered that but I could not find a good name for the whole construct 
(with the __ffs() I mean). Perhaps PCI_BRIDGE_WINDOW_LSB could be an 
option but that feels somewhat clumsy to me.

-- 
 i.