[PATCH v2 4/4] PCI: Introduce ALIGN_DOWN_IF_NONZERO() helper locally

From: Ilpo Järvinen
Date: Fri Jun 14 2024 - 06:08:48 EST


pci_bus_distribute_available_resources() performs alignment in case of
non-zero alignment requirement on 3 occasions. Introduce
ALIGN_DOWN_IF_NONZERO() helper to avoid coding the non-zero check 3
times.

Suggested-by: Jonathan Cameron <Jonathan.Cameron@xxxxxxxxxx>
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@xxxxxxxxxxxxxxx>
---

I tried to look other similar cases for both ALIGN() and ALIGN_DOWN()
kernel-wide but it seems this is not very common so I did not put
ALIGN_DOWN_IF_NONZERO() into the generic header.
---
drivers/pci/setup-bus.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index 004405edf290..39552d1a1793 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -1819,6 +1819,9 @@ static void remove_dev_resources(struct pci_dev *dev, struct resource *io,
}
}

+#define ALIGN_DOWN_IF_NONZERO(addr, align) \
+ ((align) ? ALIGN_DOWN((addr), (align)) : (addr))
+
/*
* io, mmio and mmio_pref contain the total amount of bridge window space
* available. This includes the minimal space needed to cover all the
@@ -1930,8 +1933,7 @@ static void pci_bus_distribute_available_resources(struct pci_bus *bus,
* what is available).
*/
align = pci_resource_alignment(dev, res);
- resource_set_size(&io, align ? ALIGN_DOWN(io_per_b, align)
- : io_per_b);
+ resource_set_size(&io, ALIGN_DOWN_IF_NONZERO(io_per_b, align));

/*
* The x_per_b holds the extra resource space that can be
@@ -1943,15 +1945,13 @@ static void pci_bus_distribute_available_resources(struct pci_bus *bus,

res = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
align = pci_resource_alignment(dev, res);
- resource_set_size(&mmio, align ? ALIGN_DOWN(mmio_per_b, align)
- : mmio_per_b);
+ resource_set_size(&mmio, ALIGN_DOWN_IF_NONZERO(mmio_per_b, align));
mmio.start -= resource_size(res);

res = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
align = pci_resource_alignment(dev, res);
resource_set_size(&mmio_pref,
- align ? ALIGN_DOWN(mmio_pref_per_b, align)
- : mmio_pref_per_b);
+ ALIGN_DOWN_IF_NONZERO(mmio_pref_per_b, align));
mmio_pref.start -= resource_size(res);

pci_bus_distribute_available_resources(b, add_list, io, mmio,
--
2.39.2