[PATCH 02/11] PCI: Cleanup pci_rebar_bytes_to_size() and move into rebar.c

Ilpo Järvinen posted 11 patches 3 weeks ago
There is a newer version of this series
[PATCH 02/11] PCI: Cleanup pci_rebar_bytes_to_size() and move into rebar.c
Posted by Ilpo Järvinen 3 weeks ago
Move pci_rebar_bytes_to_size() from include/linux/pci.h into rebar.c as
it does not look very trivial and is not expected to be performance
critical.

Convert literals to use a newly added PCI_REBAR_MIN_SIZE define.

Also add kernel doc for the function as the function is exported.

Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
---
 drivers/pci/rebar.c | 23 +++++++++++++++++++++++
 include/linux/pci.h | 10 +++-------
 2 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/drivers/pci/rebar.c b/drivers/pci/rebar.c
index b87cfa6fb3ef..961bd43be02b 100644
--- a/drivers/pci/rebar.c
+++ b/drivers/pci/rebar.c
@@ -7,11 +7,34 @@
 #include <linux/errno.h>
 #include <linux/export.h>
 #include <linux/ioport.h>
+#include <linux/log2.h>
 #include <linux/pci.h>
+#include <linux/sizes.h>
 #include <linux/types.h>
 
 #include "pci.h"
 
+#define PCI_REBAR_MIN_SIZE	((resource_size_t)SZ_1M)
+
+/**
+ * pci_rebar_bytes_to_size - Convert size in bytes to PCI BAR Size
+ * @bytes: size in bytes
+ *
+ * Convert bytes to BAR Size in Resizable BAR Capability (PCIe r6.2,
+ * sec. 7.8.6.3).
+ *
+ * Return: BAR Size as defined in the PCIe spec (0=1MB, bit 31=128TB).
+ */
+int pci_rebar_bytes_to_size(u64 bytes)
+{
+	int rebar_minsize = ilog2(PCI_REBAR_MIN_SIZE);
+
+	bytes = roundup_pow_of_two(bytes);
+
+	return max(ilog2(bytes), rebar_minsize) - rebar_minsize;
+}
+EXPORT_SYMBOL_GPL(pci_rebar_bytes_to_size);
+
 void pci_rebar_init(struct pci_dev *pdev)
 {
 	pdev->rebar_cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_REBAR);
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 59876de13860..894e9020b07d 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -1418,16 +1418,12 @@ void pcibios_reset_secondary_bus(struct pci_dev *dev);
 void pci_update_resource(struct pci_dev *dev, int resno);
 int __must_check pci_assign_resource(struct pci_dev *dev, int i);
 void pci_release_resource(struct pci_dev *dev, int resno);
-static inline int pci_rebar_bytes_to_size(u64 bytes)
-{
-	bytes = roundup_pow_of_two(bytes);
-
-	/* Return BAR size as defined in the resizable BAR specification */
-	return max(ilog2(bytes), 20) - 20;
-}
 
+/* Resizable BAR related routines */
+int pci_rebar_bytes_to_size(u64 bytes);
 u32 pci_rebar_get_possible_sizes(struct pci_dev *pdev, int bar);
 int __must_check pci_resize_resource(struct pci_dev *dev, int i, int size);
+
 int pci_select_bars(struct pci_dev *dev, unsigned long flags);
 bool pci_device_is_present(struct pci_dev *pdev);
 void pci_ignore_hotplug(struct pci_dev *dev);
-- 
2.39.5

RE: [PATCH 02/11] PCI: Cleanup pci_rebar_bytes_to_size() and move into rebar.c
Posted by Ruhl, Michael J 3 weeks ago
>-----Original Message-----
>From: dri-devel <dri-devel-bounces@lists.freedesktop.org> On Behalf Of Ilpo
>Järvinen
>Sent: Thursday, September 11, 2025 3:56 AM
>To: linux-pci@vger.kernel.org; Bjorn Helgaas <bhelgaas@google.com>;
>Krzysztof Wilczyński <kw@linux.com>; Christian König
><christian.koenig@amd.com>; Winiarski, Michal <michal.winiarski@intel.com>;
>Alex Deucher <alexander.deucher@amd.com>; amd-gfx@lists.freedesktop.org;
>David Airlie <airlied@gmail.com>; dri-devel@lists.freedesktop.org; intel-
>gfx@lists.freedesktop.org; intel-xe@lists.freedesktop.org; Jani Nikula
><jani.nikula@linux.intel.com>; Joonas Lahtinen
><joonas.lahtinen@linux.intel.com>; De Marchi, Lucas
><lucas.demarchi@intel.com>; Vivi, Rodrigo <rodrigo.vivi@intel.com>; Simona
>Vetter <simona@ffwll.ch>; Tvrtko Ursulin <tursulin@ursulin.net>; ?UTF-
>8?q?Thomas=20Hellstr=C3=B6m?= <thomas.hellstrom@linux.intel.com>; linux-
>kernel@vger.kernel.org
>Cc: linux-doc@vger.kernel.org; Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
>Subject: [PATCH 02/11] PCI: Cleanup pci_rebar_bytes_to_size() and move into
>rebar.c
>
>Move pci_rebar_bytes_to_size() from include/linux/pci.h into rebar.c as
>it does not look very trivial and is not expected to be performance
>critical.
>
>Convert literals to use a newly added PCI_REBAR_MIN_SIZE define.
>
>Also add kernel doc for the function as the function is exported.
>
>Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
>---
> drivers/pci/rebar.c | 23 +++++++++++++++++++++++
> include/linux/pci.h | 10 +++-------
> 2 files changed, 26 insertions(+), 7 deletions(-)
>
>diff --git a/drivers/pci/rebar.c b/drivers/pci/rebar.c
>index b87cfa6fb3ef..961bd43be02b 100644
>--- a/drivers/pci/rebar.c
>+++ b/drivers/pci/rebar.c
>@@ -7,11 +7,34 @@
> #include <linux/errno.h>
> #include <linux/export.h>
> #include <linux/ioport.h>
>+#include <linux/log2.h>
> #include <linux/pci.h>
>+#include <linux/sizes.h>
> #include <linux/types.h>
>
> #include "pci.h"
>
>+#define PCI_REBAR_MIN_SIZE	((resource_size_t)SZ_1M)
>+
>+/**
>+ * pci_rebar_bytes_to_size - Convert size in bytes to PCI BAR Size
>+ * @bytes: size in bytes
>+ *
>+ * Convert bytes to BAR Size in Resizable BAR Capability (PCIe r6.2,
>+ * sec. 7.8.6.3).
>+ *
>+ * Return: BAR Size as defined in the PCIe spec (0=1MB, bit 31=128TB).

Thank you for this helper and documentation.  I wasted a lot of time to trying to understand
this usage a few years ago.

Regardless of the rest of the patch set, this update should be used.

Reviewed-by: Michael J. Ruhl <mjruhl@habana.ai

Mike

>+ */
>+int pci_rebar_bytes_to_size(u64 bytes)
>+{
>+	int rebar_minsize = ilog2(PCI_REBAR_MIN_SIZE);
>+
>+	bytes = roundup_pow_of_two(bytes);
>+
>+	return max(ilog2(bytes), rebar_minsize) - rebar_minsize;
>+}
>+EXPORT_SYMBOL_GPL(pci_rebar_bytes_to_size);
>+
> void pci_rebar_init(struct pci_dev *pdev)
> {
> 	pdev->rebar_cap = pci_find_ext_capability(pdev,
>PCI_EXT_CAP_ID_REBAR);
>diff --git a/include/linux/pci.h b/include/linux/pci.h
>index 59876de13860..894e9020b07d 100644
>--- a/include/linux/pci.h
>+++ b/include/linux/pci.h
>@@ -1418,16 +1418,12 @@ void pcibios_reset_secondary_bus(struct pci_dev
>*dev);
> void pci_update_resource(struct pci_dev *dev, int resno);
> int __must_check pci_assign_resource(struct pci_dev *dev, int i);
> void pci_release_resource(struct pci_dev *dev, int resno);
>-static inline int pci_rebar_bytes_to_size(u64 bytes)
>-{
>-	bytes = roundup_pow_of_two(bytes);
>-
>-	/* Return BAR size as defined in the resizable BAR specification */
>-	return max(ilog2(bytes), 20) - 20;
>-}
>
>+/* Resizable BAR related routines */
>+int pci_rebar_bytes_to_size(u64 bytes);
> u32 pci_rebar_get_possible_sizes(struct pci_dev *pdev, int bar);
> int __must_check pci_resize_resource(struct pci_dev *dev, int i, int size);
>+
> int pci_select_bars(struct pci_dev *dev, unsigned long flags);
> bool pci_device_is_present(struct pci_dev *pdev);
> void pci_ignore_hotplug(struct pci_dev *dev);
>--
>2.39.5