* [PATCH] PCI: mediatek: Fix integer truncation in fls() for resource size
@ 2026-09-30 9:49 Sreeraj S Kurup
0 siblings, 0 replies; only message in thread
From: Sreeraj S Kurup @ 2026-09-30 9:49 UTC (permalink / raw)
To: Ryder Lee, Lorenzo Pieralisi, Krzysztof Wilczyński,
Manivannan Sadhasivam, Rob Herring, Bjorn Helgaas,
Matthias Brugger, AngeloGioacchino Del Regno
Cc: linux-pci, linux-mediatek, linux-kernel, linux-arm-kernel,
Sreeraj S Kurup
resource_size() returns a resource_size_t, which is 64-bit on 64-bit
architectures or 32-bit systems with LPAE/PAE enabled. Passing this
directly to fls(), which accepts an unsigned int, implicitly truncates
the upper 32 bits.
If a memory resource window of 4 GiB or larger is passed, the size
truncates to 0, resulting in invalid AHB-to-PCIe translation window
sizes.
Fix this by using fls64() to safely handle 64-bit resource sizes.
Signed-off-by: Sreeraj S Kurup <sreekuttan2156239@gmail.com>
---
drivers/pci/controller/pcie-mediatek.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/drivers/pci/controller/pcie-mediatek.c b/drivers/pci/controller/pcie-mediatek.c
index a60d1ae076f8..61591d0176bc 100644
--- a/drivers/pci/controller/pcie-mediatek.c
+++ b/drivers/pci/controller/pcie-mediatek.c
@@ -8,6 +8,7 @@
*/
#include <linux/bitfield.h>
+#include <linux/bitops.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/errno.h>
@@ -754,7 +755,7 @@ static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port)
/* Set AHB to PCIe translation windows */
val = lower_32_bits(mem->start) |
- AHB2PCIE_SIZE(fls(resource_size(mem)));
+ AHB2PCIE_SIZE(fls64(resource_size(mem)));
writel(val, port->base + PCIE_AHB_TRANS_BASE0_L);
val = upper_32_bits(mem->start);
@@ -830,7 +831,7 @@ static int mtk_pcie_startup_port_en7528(struct mtk_pcie_port *port)
/* Set AHB to PCIe translation windows */
val = lower_32_bits(mem->start) |
- AHB2PCIE_SIZE(fls(resource_size(mem)));
+ AHB2PCIE_SIZE(fls64(resource_size(mem)));
writel(val, port->base + PCIE_AHB_TRANS_BASE0_L);
val = upper_32_bits(mem->start);
--
2.55.0
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-09-30 9:50 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-30 9:49 [PATCH] PCI: mediatek: Fix integer truncation in fls() for resource size Sreeraj S Kurup
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox