[PATCH v1 2/3] fdtdec: Introduce fdtdec_setup_mem_ram_top() to determine end of topmost DRAM bank

Ilias Apalodimas ilias.apalodimas at linaro.org
Thu Mar 26 12:51:08 CET 2026


From: Marek Vasut <marek.vasut+renesas at mailbox.org>

Introduce function fdtdec_setup_mem_ram_top(), which determines the topmost
address in the last DRAM bank, to be assigned into gd->ram_top in order to
relocate U-Boot to the actual end of all DRAM. This makes use of iterator
function fdtdec_setup_mem_for_each_bank() introduced in previous commit.

Reviewed-by: Ilias Apalodimas <ilias.apalodimas at linaro.org>
Signed-off-by: Ilias Apalodimas <ilias.apalodimas at linaro.org>
Signed-off-by: Marek Vasut <marek.vasut+renesas at mailbox.org>
---
Changhes since RFC:
- Account for the ram_size as well

 include/fdtdec.h | 16 ++++++++++++++++
 lib/fdtdec.c     | 20 ++++++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/include/fdtdec.h b/include/fdtdec.h
index d9fcd037ed26..26e40c3cdb46 100644
--- a/include/fdtdec.h
+++ b/include/fdtdec.h
@@ -933,6 +933,22 @@ int fdtdec_setup_mem_size_base(void);
  */
 int fdtdec_setup_mem_size_base_lowest(void);
 
+/**
+ * fdtdec_setup_mem_ram_top() - decode and setup gd->ram_top to highest address
+ * available in any memory bank
+ *
+ * Decode the /memory 'reg' property to determine the highest end of the memory
+ * bank and populate the global data ram_top with it.
+ *
+ * This function should be called from a boards board_get_usable_ram_top().
+ * This helper function allows for boards to query the device tree for topmost
+ * DRAM address.
+ *
+ * Return: 0 if OK, -EINVAL if the /memory node or reg property is missing or
+ * invalid
+ */
+int fdtdec_setup_mem_ram_top(void);
+
 /**
  * fdtdec_setup_memory_banksize() - decode and populate gd->bd->bi_dram
  *
diff --git a/lib/fdtdec.c b/lib/fdtdec.c
index d820f75b031d..c5ee2ecb3a49 100644
--- a/lib/fdtdec.c
+++ b/lib/fdtdec.c
@@ -1162,6 +1162,26 @@ int fdtdec_setup_mem_size_base_lowest(void)
 	return fdtdec_setup_mem_for_each_bank(fdtdec_setup_mem_size_base_lowest_bankfn);
 }
 
+static void fdtdec_setup_mem_ram_top_bankfn(struct resource *res, int bank)
+{
+	if ((res->end + 1) > gd->ram_top) {
+		gd->ram_top = res->end + 1;
+		gd->ram_size += res->end - res->start;
+	}
+}
+
+int fdtdec_setup_mem_ram_top(void)
+{
+	phys_addr_t old_top = gd->ram_top;
+	int ret;
+
+	ret = fdtdec_setup_mem_for_each_bank(fdtdec_setup_mem_ram_top_bankfn);
+	if (ret)
+		gd->ram_top = old_top;
+
+	return ret;
+}
+
 static int uncompress_blob(const void *src, ulong sz_src, void **dstp)
 {
 #if CONFIG_IS_ENABLED(MULTI_DTB_FIT_GZIP) ||\
-- 
2.51.0



More information about the U-Boot mailing list