[RFC PATCH v2 18/48] lmb: introduce a function to add memory to the lmb memory map

Sughosh Ganu sughosh.ganu at linaro.org
Thu Jul 4 09:35:14 CEST 2024


Introduce a function lmb_add_memory() to add available memory to the
LMB memory map. Call this function during board init once the LMB data
structures have been initialised.

Signed-off-by: Sughosh Ganu <sughosh.ganu at linaro.org>
---
Changes since V1:
* Add memory only till ram_top to the LMB memory map, instead of all
  enumerated memory.

 include/lmb.h | 11 +++++++++++
 lib/lmb.c     | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)

diff --git a/include/lmb.h b/include/lmb.h
index 27cdb18c37..d0c094107c 100644
--- a/include/lmb.h
+++ b/include/lmb.h
@@ -36,6 +36,17 @@ struct lmb_region {
 	enum lmb_flags flags;
 };
 
+/**
+ * lmb_add_memory() - Add memory range for LMB allocations
+ *
+ * Add the entire available memory range to the pool of memory that
+ * can be used by the LMB module for allocations.
+ *
+ * Return: None
+ *
+ */
+void lmb_add_memory(void);
+
 long lmb_add(phys_addr_t base, phys_size_t size);
 long lmb_reserve(phys_addr_t base, phys_size_t size);
 /**
diff --git a/lib/lmb.c b/lib/lmb.c
index a46bc8a7a3..e4733b82ac 100644
--- a/lib/lmb.c
+++ b/lib/lmb.c
@@ -239,6 +239,46 @@ void lmb_init_and_reserve(struct bd_info *bd, void *fdt_blob)
 	lmb_reserve_common(fdt_blob);
 }
 
+/**
+ * lmb_add_memory() - Add memory range for LMB allocations
+ *
+ * Add the entire available memory range to the pool of memory that
+ * can be used by the LMB module for allocations.
+ *
+ * This can be overridden for specific boards/architectures.
+ *
+ * Return: None
+ *
+ */
+__weak void lmb_add_memory(void)
+{
+	int i;
+	phys_size_t size;
+	phys_addr_t rgn_top;
+	u64 ram_top = gd->ram_top;
+	struct bd_info *bd = gd->bd;
+
+	/* Assume a 4GB ram_top if not defined */
+	if (!ram_top)
+		ram_top = 0x100000000ULL;
+
+	for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
+		size = bd->bi_dram[i].size;
+		if (size) {
+			if (bd->bi_dram[i].start > ram_top)
+				continue;
+
+			rgn_top = bd->bi_dram[i].start +
+				bd->bi_dram[i].size;
+
+			if (rgn_top > ram_top )
+				size -= rgn_top - ram_top;
+
+			lmb_add(bd->bi_dram[i].start, size);
+		}
+	}
+}
+
 /* Initialize the struct, add memory and call arch/board reserve functions */
 void lmb_init_and_reserve_range(phys_addr_t base, phys_size_t size,
 				void *fdt_blob)
@@ -710,5 +750,7 @@ int lmb_mem_regions_init(void)
 		return -1;
 	}
 
+	lmb_add_memory();
+
 	return 0;
 }
-- 
2.34.1



More information about the U-Boot mailing list