Skip to contentSkip to conversation

Enable dynamic MMU page table sizing

PATCH Applied Pranav Sanwal 3 participants 6 messages Dec 12, 2025 – Dec 27, 2025

Subsystems (2)

v4 of 5 Applied Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover Enable dynamic MMU page table sizing

This patch series fixes a boot sequence issue in AMD Versal Gen 2, where DRAM banks are populated too late during MMU initialization. The fdtdec-based approach doesn't provide memory information early enough.

Changes in v4:
-Better debug logs alignment.

Changes in v3:
-Fix DRAM base detection boundary condition (text >= bank_phys)

Changes in v2:
-Address boundary case where U-Boot is loaded at the
beginning of a memory region.
-Export get_next_memory_node() helper from fdtdec for reuse -Split previous monolithic patch into two focused patches -Add comprehensive documentation for new functions -Include proper header dependencies (dm/ofnode_decl.h)

Pranav Sanwal (2):
fdtdec: Add declaration for get_next_memory_node() helper
arm64: versal2: Populate DRAM banks before page table size calculation

 arch/arm/mach-versal2/cpu.c                   | 57 ++++++++++++---
 .../arm/mach-versal2/include/mach/sys_proto.h |  4 +-
 board/amd/versal2/board.c                     | 70 +++++++++++++++----
 include/fdtdec.h                              | 23 ++++++
 4 files changed, 130 insertions(+), 24 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 1/2 fdtdec: Add declaration for get_next_memory_node() helper

Add get_next_memory_node() function declaration to fdtdec.h to support iterating through multiple memory nodes in device tree. This function is used to enumerate memory banks when the system has non-contiguous or multiple memory regions defined with device_type = "memory".

The function implementation already exists in lib/fdtdec.c (lines 1298-1305) but was missing the public declaration in the header file. This patch adds the declaration and includes dm/ofnode_decl.h for the ofnode type definition.

This is needed for platforms that require early memory enumeration before standard fdtdec_setup_memory_banksize() is called, particularly for dynamic MMU page table size calculation based on actual DRAM configuration.

---
 include/fdtdec.h | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
include/fdtdec.h +23 -0
index d9fcd037ed2..87e97635ce1 100644
@@ -16,6 +16,7 @@
 
 #include <linux/libfdt.h>
 #include <pci.h>
+#include <dm/ofnode_decl.h>
 
 /*
  * Support for 64bit fdt addresses.
@@ -198,6 +199,28 @@ struct fdtdec_phandle_args {
 	uint32_t args[MAX_PHANDLE_ARGS];
 };
 
+/**
+ * get_next_memory_node() - Get the next enabled memory node from device tree
+ * @mem: Current memory node to start search from, or ofnode_null() to get first node
+ *
+ * This function iterates through device tree nodes with device_type = "memory"
+ * property, automatically skipping disabled nodes (status != "okay").
+ *
+ * It is used to enumerate multiple memory regions when the system has
+ * non-contiguous or multiple memory banks defined in the device tree.
+ * The function continues searching from the given node onwards, looking
+ * for the next node with the "memory" device_type property and checking
+ * its status property.
+ *
+ * Context: Can be called multiple times to iterate through all memory nodes.
+ *          Pass ofnode_null() on first call, then pass the returned node
+ *          on subsequent calls until an invalid node is returned.
+ *
+ * Return: Next valid, enabled memory ofnode, or invalid ofnode if no more
+ *         memory nodes exist
+ */
+ofnode get_next_memory_node(ofnode mem);
+
 /**
  * fdtdec_parse_phandle_with_args() - Find a node pointed by phandle in a list
  *
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v4 1/2] fdtdec: Add declaration for get_next_memory_node() helper

Hi Pranav,

Quoted 34 lines · Pranav Sanwal

Add get_next_memory_node() function declaration to fdtdec.h to support iterating through multiple memory nodes in device tree. This function is used to enumerate memory banks when the system has non-contiguous or multiple memory regions defined with device_type = "memory".

The function implementation already exists in lib/fdtdec.c (lines 1298-1305) but was missing the public declaration in the header file. This patch adds the declaration and includes dm/ofnode_decl.h for the ofnode type definition.

This is needed for platforms that require early memory enumeration before standard fdtdec_setup_memory_banksize() is called, particularly for dynamic MMU page table size calculation based on actual DRAM configuration.

---
 include/fdtdec.h | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
include/fdtdec.h +1 -0
index d9fcd037ed2..87e97635ce1 100644
@@ -16,6 +16,7 @@
 
 #include <linux/libfdt.h>
 #include <pci.h>
+#include <dm/ofnode_decl.h>
 
 /*
  * Support for 64bit fdt addresses.

include/fdtdec.h struct fdtdec_phandle_args { · line 203

Quoted 5 lines
include/fdtdec.h +2 -0
@@ -198,6 +199,28 @@ struct fdtdec_phandle_args {
        uint32_t args[MAX_PHANDLE_ARGS];
 };
 
+/**
+ * get_next_memory_node() - Get the next enabled memory node from device tree

* (blank line here)

include/fdtdec.h struct fdtdec_phandle_args { · line 204

Quoted 1 line
include/fdtdec.h +1 -0
+ * @mem: Current memory node to start search from, or ofnode_null() to get first node

If this is to be exported it should have a fdtdec_ prefix, e.g. fdtdec_next_mem_node()

Quoted 23 lines
include/fdtdec.h +19 -1
+ *
+ * This function iterates through device tree nodes with device_type = "memory"
+ * property, automatically skipping disabled nodes (status != "okay").
+ *
+ * It is used to enumerate multiple memory regions when the system has
+ * non-contiguous or multiple memory banks defined in the device tree.
+ * The function continues searching from the given node onwards, looking
+ * for the next node with the "memory" device_type property and checking
+ * its status property.
+ *
+ * Context: Can be called multiple times to iterate through all memory nodes.
+ *          Pass ofnode_null() on first call, then pass the returned node
+ *          on subsequent calls until an invalid node is returned.
+ *
+ * Return: Next valid, enabled memory ofnode, or invalid ofnode if no more
+ *         memory nodes exist
+ */
+ofnode get_next_memory_node(ofnode mem);
+
 /**
  * fdtdec_parse_phandle_with_args() - Find a node pointed by phandle in a list
  *
--
Quoted 2 lines

2.34.1

Regards,
Simon

#3 lore raw

PATCH v4 2/2 arm64: versal2: Populate DRAM banks before page table size calculation

In reply to Pranav Sanwal

Move DRAM bank detection from fdtdec to custom implementation to ensure memory banks are populated before get_page_table_size() is called during MMU initialization.

The current fdtdec-based approach populates gd->bd->bi_dram[] too late in the boot sequence, causing get_page_table_size() to be called with unpopulated DRAM information. This prevents dynamic page table sizing based on actual memory configuration.

Parse /memory nodes in dram_init() to fill versal2_mem_map[] early enough for MMU setup. Supports up to CONFIG_NR_DRAM_BANKS (36) non-contiguous banks with high memory regions (>4GB) and use __weak get_page_table_size implementation to estimate page table size based on the populated DRAM banks.

---
 arch/arm/mach-versal2/cpu.c                   | 57 ++++++++++++---
 .../arm/mach-versal2/include/mach/sys_proto.h |  4 +-
 board/amd/versal2/board.c                     | 70 +++++++++++++++----
 3 files changed, 107 insertions(+), 24 deletions(-)
arch/arm/mach-versal2/cpu.c +49 -8
index 2dfcadb369e..896ff958bc0 100644
@@ -63,30 +63,71 @@ static struct mm_region versal2_mem_map[VERSAL2_MEM_MAP_MAX] = {
 	}
 };
 
-void mem_map_fill(void)
+/**
+ * mem_map_fill() - Populate global memory map with DRAM banks
+ * @bank_info: Array of memory regions parsed from device tree
+ * @num_banks: Number of valid DRAM banks in bank_info array
+ *
+ * Copies DRAM bank information into the global versal2_mem_map[] array
+ * starting at index VERSAL2_MEM_MAP_USED (5), which is after the fixed
+ * device mappings. This must be called early in boot before MMU
+ * initialization so that get_page_table_size() can calculate the
+ * required page table size based on actual memory configuration.
+ */
+void mem_map_fill(struct mm_region *bank_info, u32 num_banks)
 {
 	int banks = VERSAL2_MEM_MAP_USED;
 
-	for (int i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
-		/* Zero size means no more DDR that's this is end */
-		if (!gd->bd->bi_dram[i].size)
-			break;
+	for (int i = 0; i < num_banks; i++) {
+		if (banks > VERSAL2_MEM_MAP_MAX)
+			return;
 
-		versal2_mem_map[banks].virt = gd->bd->bi_dram[i].start;
-		versal2_mem_map[banks].phys = gd->bd->bi_dram[i].start;
-		versal2_mem_map[banks].size = gd->bd->bi_dram[i].size;
+		versal2_mem_map[banks].virt = bank_info[i].phys;
+		versal2_mem_map[banks].phys = bank_info[i].phys;
+		versal2_mem_map[banks].size = bank_info[i].size;
 		versal2_mem_map[banks].attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
 					      PTE_BLOCK_INNER_SHARE;
 		banks = banks + 1;
 	}
 }
 
+/**
+ * fill_gd_mem_info() - Copy DRAM banks from mem_map to bd_info
+ *
+ * Transfers DRAM bank information from the global versal2_mem_map[]
+ * array to bd->bi_dram[] for passing memory configuration to the
+ * Linux kernel via boot parameters (ATAGS/FDT). Each bank's physical
+ * address and size are copied.
+ *
+ * This is called during dram_init_banksize() after the memory map
+ * has been populated by mem_map_fill() in dram_init().
+ *
+ * Context: Called after dram_init() but before kernel handoff
+ * Side effects: Modifies bd->bi_dram[] array
+ */
+void fill_bd_mem_info(void)
+{
+	struct bd_info *bd = gd->bd;
+	int banks = VERSAL2_MEM_MAP_USED;
+
+	for (int i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
+		if (!versal2_mem_map[banks].size)
+			break;
+
+		bd->bi_dram[i].start = versal2_mem_map[banks].phys;
+		bd->bi_dram[i].size = versal2_mem_map[banks].size;
+		banks++;
+	}
+}
+
 struct mm_region *mem_map = versal2_mem_map;
 
+#if CONFIG_IS_ENABLED(SYS_MEM_RSVD_FOR_MMU)
 u64 get_page_table_size(void)
 {
 	return 0x14000;
 }
+#endif
 
 U_BOOT_DRVINFO(soc_amd_versal2) = {
 	.name = "soc_amd_versal2",
arch/arm/mach-versal2/include/mach/sys_proto.h +3 -1
index 7b1726a7ef4..cee13488620 100644
@@ -5,5 +5,7 @@
  */
 
 #include <linux/build_bug.h>
+#include <asm/armv8/mmu.h>
 
-void mem_map_fill(void);
+void mem_map_fill(struct mm_region *bank_info, u32 num_banks);
+void fill_bd_mem_info(void);
board/amd/versal2/board.c +55 -15

95 lines not on this page · raw message

Signature · 1 line
2.34.1
Applied
#4 lore raw
In reply to Pranav Sanwal
Quoted 23 lines · Pranav Sanwal

This patch series fixes a boot sequence issue in AMD Versal Gen 2, where DRAM banks are populated too late during MMU initialization. The fdtdec-based approach doesn't provide memory information early enough.

Changes in v4:
-Better debug logs alignment.

Changes in v3:
-Fix DRAM base detection boundary condition (text >= bank_phys)

Changes in v2:

(quoted diff) +0 -5
-Address boundary case where U-Boot is loaded at the
  beginning of a memory region.
-Export get_next_memory_node() helper from fdtdec for reuse
-Split previous monolithic patch into two focused patches
-Add comprehensive documentation for new functions
-Include proper header dependencies (dm/ofnode_decl.h)

Pranav Sanwal (2):
fdtdec: Add declaration for get_next_memory_node() helper
arm64: versal2: Populate DRAM banks before page table size calculation

Quoted 6 lines
  arch/arm/mach-versal2/cpu.c                   | 57 ++++++++++++---
  .../arm/mach-versal2/include/mach/sys_proto.h |  4 +-
  board/amd/versal2/board.c                     | 70 +++++++++++++++----
  include/fdtdec.h                              | 23 ++++++
  4 files changed, 130 insertions(+), 24 deletions(-)

Applied.
M

#5 lore raw
Quoted 26 lines · Michal Simek
Quoted 23 lines · Pranav Sanwal

This patch series fixes a boot sequence issue in AMD Versal Gen 2, where DRAM banks are populated too late during MMU initialization. The fdtdec-based approach doesn't provide memory information early enough.

Changes in v4:
-Better debug logs alignment.

Changes in v3:
-Fix DRAM base detection boundary condition (text >= bank_phys)

Changes in v2:
-Address boundary case where U-Boot is loaded at the beginning of a memory region.

(quoted diff) +0 -4
-Export get_next_memory_node() helper from fdtdec for reuse
-Split previous monolithic patch into two focused patches
-Add comprehensive documentation for new functions
-Include proper header dependencies (dm/ofnode_decl.h)

Pranav Sanwal (2):
   fdtdec: Add declaration for get_next_memory_node() helper
   arm64: versal2: Populate DRAM banks before page table size calculation

Quoted 9 lines
Quoted 6 lines

  arch/arm/mach-versal2/cpu.c                   | 57 ++++++++++++--- .../arm/mach-versal2/include/mach/sys_proto.h |  4 +- board/amd/versal2/board.c                     | 70 +++++++++++++++---- include/fdtdec.h                              | 23 ++++++

  4 files changed, 130 insertions(+), 24 deletions(-)

Applied.
M

Actually I have to remove it from my queue because there is compilation failure on xenguest_arm64.

+board/xen/xenguest_arm64/xenguest_arm64.c:63:12: error: conflicting types for 'get_next_memory_node'; have 'int(const void *, int)'
+ 63 | static int get_next_memory_node(const void *blob, int mem)
+ | ^~~~~~~~~~~~~~~~~~~~
+In file included from include/dm/ofnode.h:11,

+                 from include/dm/device.h:13,
+                 from include/dm.h:13,
+                 from board/xen/xenguest_arm64/xenguest_arm64.c:12:

+include/fdtdec.h:222:8: note: previous declaration of 'get_next_memory_node' with type 'ofnode(ofnode)' {aka 'union ofnode_union(union ofnode_union)'}
+ 222 | ofnode get_next_memory_node(ofnode mem);
+ | ^~~~~~~~~~~~~~~~~~~~
+make[2]: *** [scripts/Makefile.build:297: board/xen/xenguest_arm64/xenguest_arm64.o] Error 1
+make[1]: *** [Makefile:2133: board/xen/xenguest_arm64] Error 2
Please fix it and send v5.

Thanks,
Michal