Skip to contentSkip to conversation

Allow showing the memory map

PATCH In review Simon Glass 3 participants 12 messages Oct 21, 2024 – Oct 26, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v4 cover Allow showing the memory map

This little series adds a new 'memmap' command, intended to show the layout of memory within U-Boot and how much memory is available for loading images.

Changes in v4:
- Drop am65x patch as it is not needed
- Add patch to export the lmb data structure
- Add new patch to show the lmb records

Changes in v3:
- Add a new Kconfig for the extra functionality

Changes in v2:
- Split into its own patch
- Modify the existing 'meminfo' command instead

Simon Glass (8):
common: Fix up malloc() comment in reserve_noncached()
common: Tidy up how malloc() is inited
global_data: Add some more accessors
bootstage: Allow counting memory without strings
cmd: Move meminfo command into its own file
cmd: Update the meminfo command to show the memory map
lmb: Export the lmb data structure
meminfo: Show the lmb records

 cmd/Kconfig                       |  12 +++
 cmd/Makefile                      |   1 +
 cmd/mem.c                         |  19 -----
 cmd/meminfo.c                     |  99 +++++++++++++++++++++++
 common/board_f.c                  |   8 +-
 common/board_r.c                  |   3 +-
 common/bootstage.c                |  16 ++--
 common/dlmalloc.c                 |   8 +-
 common/spl/spl.c                  |   4 +-
 doc/usage/cmd/meminfo.rst         | 128 ++++++++++++++++++++++++++++++
 doc/usage/index.rst               |   1 +
 include/asm-generic/global_data.h |  30 +++++++
 include/bootstage.h               |   5 +-
 include/malloc.h                  |   8 ++
 lib/lmb.c                         |   2 +-
 test/cmd/Makefile                 |   3 +-
 test/cmd/meminfo.c                |  42 ++++++++++
 17 files changed, 347 insertions(+), 42 deletions(-)
 create mode 100644 cmd/meminfo.c
 create mode 100644 doc/usage/cmd/meminfo.rst
 create mode 100644 test/cmd/meminfo.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH v4 1/8 common: Fix up malloc() comment in reserve_noncached()

The function name has changed, so update it.

---

(no changes since v1)

 common/board_f.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
common/board_f.c +3 -3
index f1bd70fdd6c..a87411ee350 100644
@@ -501,9 +501,9 @@ static unsigned long reserve_stack_aligned(size_t size)
 static int reserve_noncached(void)
 {
 	/*
-	 * The value of gd->start_addr_sp must match the value of malloc_start
-	 * calculated in board_r.c:initr_malloc(), which is passed to
-	 * dlmalloc.c:mem_malloc_init() and then used by
+	 * The value of gd->start_addr_sp must match the value of
+	 * mem_malloc_start calculated in board_r.c:initr_malloc(), which is
+	 * passed to dlmalloc.c:mem_malloc_init() and then used by
 	 * cache.c:noncached_init()
 	 *
 	 * These calculations must match the code in cache.c:noncached_init()
Signature · 1 line
2.43.0
#2 lore raw

PATCH v4 2/8 common: Tidy up how malloc() is inited

In reply to Simon Glass

The call to malloc() is a bit strange. The naming of the arguments suggests that an address is passed, but in fact it is a pointer, at least in the board_init_r() function and SPL equivalent.

Update it to work as described. Add a function comment as well.

Note that this does adjustment does not extend into the malloc() implementation itself, apart from changing mem_malloc_init(), since there are lots of casts and pointers and integers are used interchangeably.

---

(no changes since v1)

 common/board_r.c  | 3 +--
 common/dlmalloc.c | 8 +++++---
 common/spl/spl.c  | 4 +---
 include/malloc.h  | 8 ++++++++
 4 files changed, 15 insertions(+), 8 deletions(-)
common/board_r.c +1 -2
index e5f33f40643..8a19817fa39 100644
@@ -204,8 +204,7 @@ static int initr_malloc(void)
 	 */
 	start = gd->relocaddr - TOTAL_MALLOC_LEN;
 	gd_set_malloc_start(start);
-	mem_malloc_init((ulong)map_sysmem(start, TOTAL_MALLOC_LEN),
-			TOTAL_MALLOC_LEN);
+	mem_malloc_init(start, TOTAL_MALLOC_LEN);
 	return 0;
 }
 
common/dlmalloc.c +5 -3
index 1ac7ce3f43c..cc4d3a0a028 100644
@@ -16,6 +16,8 @@
 #include <asm/global_data.h>
 
 #include <malloc.h>
+#include <mapmem.h>
+#include <string.h>
 #include <asm/io.h>
 #include <valgrind/memcheck.h>
 
@@ -598,9 +600,9 @@ void *sbrk(ptrdiff_t increment)
 
 void mem_malloc_init(ulong start, ulong size)
 {
-	mem_malloc_start = start;
-	mem_malloc_end = start + size;
-	mem_malloc_brk = start;
+	mem_malloc_start = (ulong)map_sysmem(start, size);
+	mem_malloc_end = mem_malloc_start + size;
+	mem_malloc_brk = mem_malloc_start;
 
 #ifdef CONFIG_SYS_MALLOC_DEFAULT_TO_INIT
 	malloc_init();
common/spl/spl.c +1 -3
index 94657d00591..1ceb63daf31 100644
@@ -678,9 +678,7 @@ void board_init_r(gd_t *dummy1, ulong dummy2)
 	spl_set_bd();
 
 	if (IS_ENABLED(CONFIG_SPL_SYS_MALLOC)) {
-		mem_malloc_init((ulong)map_sysmem(SPL_SYS_MALLOC_START,
-						  SPL_SYS_MALLOC_SIZE),
-				SPL_SYS_MALLOC_SIZE);
+		mem_malloc_init(SPL_SYS_MALLOC_START, SPL_SYS_MALLOC_SIZE);
 		gd->flags |= GD_FLG_FULL_MALLOC_INIT;
 	}
 	if (!(gd->flags & GD_FLG_SPL_INIT)) {
include/malloc.h +8 -0
index 07d3e90a855..9e0be482416 100644
@@ -981,6 +981,14 @@ extern ulong mem_malloc_start;
 extern ulong mem_malloc_end;
 extern ulong mem_malloc_brk;
 
+/**
+ * mem_malloc_init() - Set up the malloc() pool
+ *
+ * Sets the region of memory to be used for all future calls to malloc(), etc.
+ *
+ * @start: Start address
+ * @size: Size in bytes
+ */
 void mem_malloc_init(ulong start, ulong size);
 
 #ifdef __cplusplus
Signature · 1 line
2.43.0
#3 lore raw

PATCH v4 3/8 global_data: Add some more accessors

In reply to Simon Glass

Add accessors for bloblist, bootstage, trace and video to avoid needing more #ifdefs in the C code.

---

(no changes since v1)

 include/asm-generic/global_data.h | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
include/asm-generic/global_data.h +30 -0
index 644a0d77873..9dc0f4308cc 100644
@@ -544,6 +544,36 @@ static_assert(sizeof(struct global_data) == GD_SIZE);
 #define gd_set_upl(val)
 #endif
 
+#if CONFIG_IS_ENABLED(BLOBLIST)
+#define gd_bloblist()		gd->bloblist
+#else
+#define gd_bloblist()		NULL
+#endif
+
+#if CONFIG_IS_ENABLED(BOOTSTAGE)
+#define gd_bootstage()		gd->bootstage
+#else
+#define gd_bootstage()		NULL
+#endif
+
+#if CONFIG_IS_ENABLED(TRACE)
+#define gd_trace_buff()		gd->trace_buff
+#define gd_trace_size()		CONFIG_TRACE_BUFFER_SIZE
+#else
+#define gd_trace_buff()		NULL
+#define gd_trace_size()		0
+#endif
+
+#if CONFIG_IS_ENABLED(VIDEO)
+#define gd_video_top()		gd->video_top
+#define gd_video_bottom()	gd->video_bottom
+#define gd_video_size()		(gd->video_top - gd->video_bottom)
+#else
+#define gd_video_top()		0
+#define gd_video_bottom()	0
+#define gd_video_size()		0
+#endif
+
 /**
  * enum gd_flags - global data flags
  *
Signature · 1 line
2.43.0
#4 lore raw

PATCH v4 4/8 bootstage: Allow counting memory without strings

In reply to Simon Glass

The bootstage array includes pointers to strings but not the strings themselves. The strings are added when stashing, but including them in the size calculation gives an inflated view of the amount of space used by the array.

Update this function so it can return the amount of memory used by the bootstage structures themselves, without the strings which they point to.

---

(no changes since v1)

 common/board_f.c    |  2 +-
 common/bootstage.c  | 16 +++++++++-------
 include/bootstage.h |  5 +++--
 3 files changed, 13 insertions(+), 10 deletions(-)
common/board_f.c +1 -1
index a87411ee350..98dc2591e1d 100644
@@ -582,7 +582,7 @@ static int reserve_fdt(void)
 static int reserve_bootstage(void)
 {
 #ifdef CONFIG_BOOTSTAGE
-	int size = bootstage_get_size();
+	int size = bootstage_get_size(true);
 
 	gd->start_addr_sp = reserve_stack_aligned(size);
 	gd->boardf->new_bootstage = map_sysmem(gd->start_addr_sp, size);
common/bootstage.c +9 -7
index dd6aed7c2fd..c7bb204501a 100644
@@ -520,17 +520,19 @@ int _bootstage_unstash_default(void)
 }
 #endif
 
-int bootstage_get_size(void)
+int bootstage_get_size(bool add_strings)
 {
-	struct bootstage_data *data = gd->bootstage;
-	struct bootstage_record *rec;
 	int size;
-	int i;
 
 	size = sizeof(struct bootstage_data);
-	for (rec = data->record, i = 0; i < data->rec_count;
-	     i++, rec++)
-		size += strlen(rec->name) + 1;
+	if (add_strings) {
+		struct bootstage_data *data = gd->bootstage;
+		struct bootstage_record *rec;
+		int i;
+
+		for (rec = data->record, i = 0; i < data->rec_count; i++, rec++)
+			size += strlen(rec->name) + 1;
+	}
 
 	return size;
 }
include/bootstage.h +3 -2
index 57792648c49..3300ca0248a 100644
@@ -371,9 +371,10 @@ int bootstage_unstash(const void *base, int size);
 /**
  * bootstage_get_size() - Get the size of the bootstage data
  *
+ * @add_strings: true to add the size of attached strings (for stashing)
  * Return: size of boostage data in bytes
  */
-int bootstage_get_size(void);
+int bootstage_get_size(bool add_strings);
 
 /**
  * bootstage_init() - Prepare bootstage for use
@@ -444,7 +445,7 @@ static inline int bootstage_unstash(const void *base, int size)
 	return 0;	/* Pretend to succeed */
 }
 
-static inline int bootstage_get_size(void)
+static inline int bootstage_get_size(bool add_strings)
 {
 	return 0;
 }
Signature · 1 line
2.43.0
#5 lore raw

PATCH v4 5/8 cmd: Move meminfo command into its own file

In reply to Simon Glass

In preparation for expanding this command, move it into a separate file. Rename the function to remove the extra underscore. Update the number of arguments to 1, since 3 is incorrect.

---

(no changes since v2)

Changes in v2:
- Split into its own patch

 cmd/Kconfig   |  1 +
 cmd/Makefile  |  1 +
 cmd/mem.c     | 19 -------------------
 cmd/meminfo.c | 26 ++++++++++++++++++++++++++
 4 files changed, 28 insertions(+), 19 deletions(-)
 create mode 100644 cmd/meminfo.c
cmd/Kconfig +1 -0
index a4ca61c37dd..05bd3e8a5e2 100644
@@ -885,6 +885,7 @@ config MD5SUM_VERIFY
 
 config CMD_MEMINFO
 	bool "meminfo"
+	default y if SANDBOX
 	help
 	  Display memory information.
 
cmd/Makefile +1 -0
index 3c5bd56e912..16e275eba63 100644
@@ -110,6 +110,7 @@ obj-$(CONFIG_CMD_LOG) += log.o
 obj-$(CONFIG_CMD_LSBLK) += lsblk.o
 obj-$(CONFIG_CMD_MD5SUM) += md5sum.o
 obj-$(CONFIG_CMD_MEMORY) += mem.o
+obj-$(CONFIG_CMD_MEMINFO) += meminfo.o
 obj-$(CONFIG_CMD_IO) += io.o
 obj-$(CONFIG_CMD_MII) += mii.o
 obj-$(CONFIG_CMD_MISC) += misc.o
cmd/mem.c +0 -19
index 4d6fde28531..9e716776393 100644
@@ -1379,17 +1379,6 @@ U_BOOT_CMD(
 
 #endif
 
-#ifdef CONFIG_CMD_MEMINFO
-static int do_mem_info(struct cmd_tbl *cmdtp, int flag, int argc,
-		       char *const argv[])
-{
-	puts("DRAM:  ");
-	print_size(gd->ram_size, "\n");
-
-	return 0;
-}
-#endif
-
 U_BOOT_CMD(
 	base,	2,	1,	do_mem_base,
 	"print or set address offset",
@@ -1433,14 +1422,6 @@ U_BOOT_CMD(
 );
 #endif /* CONFIG_CMD_MX_CYCLIC */
 
-#ifdef CONFIG_CMD_MEMINFO
-U_BOOT_CMD(
-	meminfo,	3,	1,	do_mem_info,
-	"display memory information",
-	""
-);
-#endif
-
 #ifdef CONFIG_CMD_RANDOM
 U_BOOT_CMD(
 	random,	4,	0,	do_random,
cmd/meminfo.c new file +26 -0
new file mode 100644
index 00000000000..bb9bcec2e3f
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2024 Google LLC
+ * Written by Simon Glass <[email protected]>
+ */
+
+#include <command.h>
+#include <display_options.h>
+#include <asm/global_data.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static int do_meminfo(struct cmd_tbl *cmdtp, int flag, int argc,
+		       char *const argv[])
+{
+	puts("DRAM:  ");
+	print_size(gd->ram_size, "\n");
+
+	return 0;
+}
+
+U_BOOT_CMD(
+	meminfo,	1,	1,	do_meminfo,
+	"display memory information",
+	""
+);
Signature · 1 line
2.43.0
Reviewed-by on v4 5/8
#6 lore raw

Re: [PATCH v4 5/8] cmd: Move meminfo command into its own file

Quoted 114 lines · Simon Glass

In preparation for expanding this command, move it into a separate file. Rename the function to remove the extra underscore. Update the number of arguments to 1, since 3 is incorrect.

---

(no changes since v2)

Changes in v2:
- Split into its own patch

 cmd/Kconfig   |  1 +
 cmd/Makefile  |  1 +
 cmd/mem.c     | 19 -------------------
 cmd/meminfo.c | 26 ++++++++++++++++++++++++++
 4 files changed, 28 insertions(+), 19 deletions(-)
 create mode 100644 cmd/meminfo.c
cmd/Kconfig +1 -0
index a4ca61c37dd..05bd3e8a5e2 100644
@@ -885,6 +885,7 @@ config MD5SUM_VERIFY
 
 config CMD_MEMINFO
        bool "meminfo"
+       default y if SANDBOX
        help
          Display memory information.
 
cmd/Makefile +1 -0
index 3c5bd56e912..16e275eba63 100644
@@ -110,6 +110,7 @@ obj-$(CONFIG_CMD_LOG) += log.o
 obj-$(CONFIG_CMD_LSBLK) += lsblk.o
 obj-$(CONFIG_CMD_MD5SUM) += md5sum.o
 obj-$(CONFIG_CMD_MEMORY) += mem.o
+obj-$(CONFIG_CMD_MEMINFO) += meminfo.o
 obj-$(CONFIG_CMD_IO) += io.o
 obj-$(CONFIG_CMD_MII) += mii.o
 obj-$(CONFIG_CMD_MISC) += misc.o
cmd/mem.c +0 -19
index 4d6fde28531..9e716776393 100644
@@ -1379,17 +1379,6 @@ U_BOOT_CMD(
 
 #endif
 
-#ifdef CONFIG_CMD_MEMINFO
-static int do_mem_info(struct cmd_tbl *cmdtp, int flag, int argc,
-                      char *const argv[])
-{
-       puts("DRAM:  ");
-       print_size(gd->ram_size, "\n");
-
-       return 0;
-}
-#endif
-
 U_BOOT_CMD(
        base,   2,      1,      do_mem_base,
        "print or set address offset",
@@ -1433,14 +1422,6 @@ U_BOOT_CMD(
 );
 #endif /* CONFIG_CMD_MX_CYCLIC */
 
-#ifdef CONFIG_CMD_MEMINFO
-U_BOOT_CMD(
-       meminfo,        3,      1,      do_mem_info,
-       "display memory information",
-       ""
-);
-#endif
-
 #ifdef CONFIG_CMD_RANDOM
 U_BOOT_CMD(
        random, 4,      0,      do_random,
cmd/meminfo.c new file +26 -1
new file mode 100644
index 00000000000..bb9bcec2e3f
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2024 Google LLC
+ * Written by Simon Glass <[email protected]>
+ */
+
+#include <command.h>
+#include <display_options.h>
+#include <asm/global_data.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static int do_meminfo(struct cmd_tbl *cmdtp, int flag, int argc,
+                      char *const argv[])
+{
+       puts("DRAM:  ");
+       print_size(gd->ram_size, "\n");
+
+       return 0;
+}
+
+U_BOOT_CMD(
+       meminfo,        1,      1,      do_meminfo,
+       "display memory information",
+       ""
+);
--
Quoted 2 lines

2.43.0

#7 lore raw

PATCH v4 6/8 cmd: Update the meminfo command to show the memory map

In reply to Simon Glass

U-Boot has a fairly rigid memory map which is normally not visible unless debugging is enabled in board_f.c

Update the 'meminfo' command to show it. This command does not cover arch-specific pieces but gives a good overview of where things are.

---

Changes in v4:
- Drop am65x patch as it is not needed

Changes in v3:
- Add a new Kconfig for the extra functionality

Changes in v2:
- Modify the existing 'meminfo' command instead

 cmd/Kconfig               |  11 ++++
 cmd/meminfo.c             |  51 ++++++++++++++-
 doc/usage/cmd/meminfo.rst | 128 ++++++++++++++++++++++++++++++++++++++
 doc/usage/index.rst       |   1 +
 test/cmd/Makefile         |   3 +-
 test/cmd/meminfo.c        |  38 +++++++++++
 6 files changed, 230 insertions(+), 2 deletions(-)
 create mode 100644 doc/usage/cmd/meminfo.rst
 create mode 100644 test/cmd/meminfo.c
cmd/Kconfig +11 -0
index 05bd3e8a5e2..2c5188daff2 100644
@@ -889,6 +889,17 @@ config CMD_MEMINFO
 	help
 	  Display memory information.
 
+config CMD_MEMINFO_MAP
+	bool "- with memory map"
+	depends on CMD_MEMINFO
+	default y if SANDBOX
+	help
+	  Shows a memory map, in addition to just the DRAM size. This allows
+	  seeing where U-Boot's memory area is, at the top of DRAM, as well as
+	  detail about each piece of it.
+
+	  See doc/usage/cmd/meminfo.rst for more information.
+
 config CMD_MEMORY
 	bool "md, mm, nm, mw, cp, cmp, base, loop"
 	default y
cmd/meminfo.c +50 -1

68 lines not on this page · raw message

doc/usage/cmd/meminfo.rst new file +128 -0

128 lines not on this page · raw message

doc/usage/index.rst +1 -0
index b84d8ee909f..db71711c393 100644
@@ -84,6 +84,7 @@ Shell commands
    cmd/loads
    cmd/loadx
    cmd/loady
+   cmd/meminfo
    cmd/mbr
    cmd/md
    cmd/mmc
test/cmd/Makefile +2 -1
index 40808350962..4b487c1d2cb 100644
@@ -19,8 +19,9 @@ obj-$(CONFIG_CMD_FDT) += fdt.o
 obj-$(CONFIG_CONSOLE_TRUETYPE) += font.o
 obj-$(CONFIG_CMD_HISTORY) += history.o
 obj-$(CONFIG_CMD_LOADM) += loadm.o
-obj-$(CONFIG_CMD_MEM_SEARCH) += mem_search.o
+obj-$(CONFIG_CMD_MEMINFO) += meminfo.o
 obj-$(CONFIG_CMD_MEMORY) += mem_copy.o
+obj-$(CONFIG_CMD_MEM_SEARCH) += mem_search.o
 ifdef CONFIG_CMD_PCI
 obj-$(CONFIG_CMD_PCI_MPS) += pci_mps.o
 endif
test/cmd/meminfo.c new file +38 -0
new file mode 100644
index 00000000000..84981305bf0
@@ -0,0 +1,38 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Test for 'meminfo' command
+ *
+ * Copyright 2024 Google LLC
+ * Written by Simon Glass <[email protected]>
+ */
+
+#include <dm/test.h>
+#include <test/cmd.h>
+#include <test/ut.h>
+
+/* Test 'meminfo' command */
+static int cmd_test_meminfo(struct unit_test_state *uts)
+{
+	ut_assertok(run_command("meminfo", 0));
+	ut_assert_nextline("DRAM:  256 MiB");
+	ut_assert_nextline_empty();
+
+	ut_assert_nextline("Region           Base     Size      End      Gap");
+	ut_assert_nextlinen("-");
+
+	/* For now we don't worry about checking the values */
+	ut_assert_nextlinen("video");
+	ut_assert_nextlinen("code");
+	ut_assert_nextlinen("malloc");
+	ut_assert_nextlinen("board_info");
+	ut_assert_nextlinen("global_data");
+	ut_assert_nextlinen("devicetree");
+	ut_assert_nextlinen("bootstage");
+	ut_assert_nextlinen("bloblist");
+	ut_assert_nextlinen("stack");
+	ut_assert_nextlinen("free");
+	ut_assert_console_end();
+
+	return 0;
+}
+CMD_TEST(cmd_test_meminfo, UTF_CONSOLE);
Signature · 1 line
2.43.0
#8 lore raw

PATCH v4 7/8 lmb: Export the lmb data structure

In reply to Simon Glass

Provide a way to access this data structure so that the meminfo command can use it.

---

Changes in v4:
- Add patch to export the lmb data structure

 lib/lmb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/lmb.c +1 -1
index 7e90f178763..eec99c185ee 100644
@@ -887,12 +887,12 @@ int lmb_init(void)
 	return 0;
 }
 
-#if CONFIG_IS_ENABLED(UNIT_TEST)
 struct lmb *lmb_get(void)
 {
 	return &lmb;
 }
 
+#if CONFIG_IS_ENABLED(UNIT_TEST)
 int lmb_push(struct lmb *store)
 {
 	int ret;
Signature · 1 line
2.43.0
Reviewed-by on v4 7/8
#9 lore raw

Re: [PATCH v4 7/8] lmb: Export the lmb data structure

Quoted 32 lines · Simon Glass

Provide a way to access this data structure so that the meminfo command can use it.

---

Changes in v4:
- Add patch to export the lmb data structure

 lib/lmb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/lmb.c +1 -2
index 7e90f178763..eec99c185ee 100644
@@ -887,12 +887,12 @@ int lmb_init(void)
        return 0;
 }
 
-#if CONFIG_IS_ENABLED(UNIT_TEST)
 struct lmb *lmb_get(void)
 {
        return &lmb;
 }
 
+#if CONFIG_IS_ENABLED(UNIT_TEST)
 int lmb_push(struct lmb *store)
 {
        int ret;
--
Quoted 2 lines

2.43.0

#10 lore raw

PATCH v4 8/8 meminfo: Show the lmb records

In reply to Simon Glass

Add the lmb records onto the end of the memory map.

---

Changes in v4:
- Add new patch to show the lmb records

 cmd/meminfo.c      | 26 +++++++++++++++++++++++++-
 test/cmd/meminfo.c |  6 +++++-
 2 files changed, 30 insertions(+), 2 deletions(-)
cmd/meminfo.c +25 -1
index 0e6d2f9cc2c..5e83d61c2dd 100644
@@ -8,6 +8,7 @@
 #include <bootstage.h>
 #include <command.h>
 #include <display_options.h>
+#include <lmb.h>
 #include <malloc.h>
 #include <mapmem.h>
 #include <asm/global_data.h>
@@ -25,6 +26,27 @@ static void print_region(const char *name, ulong base, ulong size, ulong *uptop)
 	*uptop = base;
 }
 
+static void show_lmb(const struct lmb *lmb, ulong *uptop)
+{
+	int i;
+
+	for (i = lmb->used_mem.count - 1; i >= 0; i--) {
+		const struct lmb_region *rgn = alist_get(&lmb->used_mem, i,
+							 struct lmb_region);
+
+		/*
+		 * Assume that the top lmb region is the U-Boot region, so just
+		 * take account of the memory not already reported
+		 */
+		if (lmb->used_mem.count - 1)
+			print_region("lmb", rgn->base, *uptop - rgn->base,
+				     uptop);
+		else
+			print_region("lmb", rgn->base, rgn->size, uptop);
+		*uptop = rgn->base;
+	}
+}
+
 static int do_meminfo(struct cmd_tbl *cmdtp, int flag, int argc,
 		      char *const argv[])
 {
@@ -63,7 +85,9 @@ static int do_meminfo(struct cmd_tbl *cmdtp, int flag, int argc,
 			     bloblist_get_total_size(), &upto);
 	stk_bot = gd->start_addr_sp - CONFIG_STACK_SIZE;
 	print_region("stack", stk_bot, CONFIG_STACK_SIZE, &upto);
-	print_region("free", gd->ram_base, stk_bot, &upto);
+	if (IS_ENABLED(CONFIG_LMB))
+		show_lmb(lmb_get(), &upto);
+	print_region("free", gd->ram_base, upto, &upto);
 
 	return 0;
 }
test/cmd/meminfo.c +5 -1
index 84981305bf0..53b41e3b49e 100644
@@ -30,7 +30,11 @@ static int cmd_test_meminfo(struct unit_test_state *uts)
 	ut_assert_nextlinen("bootstage");
 	ut_assert_nextlinen("bloblist");
 	ut_assert_nextlinen("stack");
-	ut_assert_nextlinen("free");
+
+	/* we expect at least one lmb line, but don't know how many */
+	ut_assert_nextlinen("lmb");
+	ut_assert_skip_to_linen("free");
+
 	ut_assert_console_end();
 
 	return 0;
Signature · 1 line
2.43.0
#11 lore raw
In reply to Simon Glass
Quoted 10 lines · Simon Glass

This little series adds a new 'memmap' command, intended to show the layout of memory within U-Boot and how much memory is available for loading images.

Changes in v4:
- Drop am65x patch as it is not needed
- Add patch to export the lmb data structure
- Add new patch to show the lmb records

[...]

Applied to u-boot/master, thanks!

Signature · 1 line
Tom