Skip to contentSkip to conversation

image: fdt: Fix DT relocation handling with multiple DRAM banks with gap

RFC PATCH Superseded Marek Vasut 2 participants 5 messages Jul 22, 2021 – Mar 12, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 image: fdt: Fix DT relocation handling with multiple DRAM banks with gap

The current implementation of boot_relocate_fdt() places DT at the highest usable DRAM address, which is calculated as:
env_get_bootm_low() + env_get_bootm_mapsize()
which by default becomes gd->ram_base + gd->ram_size.

Systems like i.MX53 can have multiple DRAM banks with gap between them, e.g. have DRAM at 0x70000000-0x8fffffff and 0xb0000000-0xcfffffff , so for them the calculated highest DRAM address is 0xafffffff, which is exactly in the gap and thus not usable.

Fix this by iterating over all DRAM banks and tracking the remaining amount of the total mapping size obtained from env_get_bootm_mapsize(). Limit the maximum LMB area size to each bank, to avoid using nonexistent DRAM.

Cc Heinrich Schuchardt <[email protected]>
Cc Simon Glass <[email protected]>
Cc Tom Rini <[email protected]>
---
 common/image-fdt.c | 40 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 36 insertions(+), 4 deletions(-)
common/image-fdt.c +36 -4
index 327a8c4c395..2b199ffc2b1 100644
@@ -157,8 +157,11 @@ int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
 {
 	void	*fdt_blob = *of_flat_tree;
 	void	*of_start = NULL;
+	u64	start, size, usable;
 	char	*fdt_high;
+	ulong	mapsize, low;
 	ulong	of_len = 0;
+	int	bank;
 	int	err;
 	int	disable_relocation = 0;
 
@@ -198,10 +201,39 @@ int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
 			    (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
 		}
 	} else {
-		of_start =
-		    (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
-						   env_get_bootm_mapsize()
-						   + env_get_bootm_low());
+		mapsize = env_get_bootm_mapsize();
+		low = env_get_bootm_low();
+		of_start = NULL;
+
+		for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
+			start = gd->bd->bi_dram[bank].start;
+			size = gd->bd->bi_dram[bank].size;
+
+			/* DRAM bank addresses are too low, skip it. */
+			if (start + size < low)
+				continue;
+
+			usable = min(size, (u64)mapsize);
+
+			/*
+			 * At least part of this DRAM bank is usable, try
+			 * using it for LMB allocation.
+			 */
+			of_start =
+			    (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
+							   start + usable);
+			/* Allocation succeeded, use this block. */
+			if (of_start != NULL)
+				break;
+
+			/*
+			 * Reduce the mapping size in the next bank
+			 * by the size of attempt in current bank.
+			 */
+			mapsize -= usable - max(start, (u64)low);
+			if (!mapsize)
+				break;
+		}
 	}
 
 	if (of_start == NULL) {
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw

Hi Marek,

Quoted 16 lines · Marek Vasut

The current implementation of boot_relocate_fdt() places DT at the highest usable DRAM address, which is calculated as:
env_get_bootm_low() + env_get_bootm_mapsize()
which by default becomes gd->ram_base + gd->ram_size.

Systems like i.MX53 can have multiple DRAM banks with gap between them, e.g. have DRAM at 0x70000000-0x8fffffff and 0xb0000000-0xcfffffff , so for them the calculated highest DRAM address is 0xafffffff, which is exactly in the gap and thus not usable.

Fix this by iterating over all DRAM banks and tracking the remaining amount of the total mapping size obtained from env_get_bootm_mapsize(). Limit the maximum LMB area size to each bank, to avoid using nonexistent DRAM.

Quoted 7 lines
Cc Heinrich Schuchardt <[email protected]>
Cc Simon Glass <[email protected]>
Cc Tom Rini <[email protected]>
---
 common/image-fdt.c | 40 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 36 insertions(+), 4 deletions(-)

Should we put this behind a Kconfig option to reduce code size?

Quoted 62 lines
common/image-fdt.c +36 -5
index 327a8c4c395..2b199ffc2b1 100644
@@ -157,8 +157,11 @@ int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
 {
        void    *fdt_blob = *of_flat_tree;
        void    *of_start = NULL;
+       u64     start, size, usable;
        char    *fdt_high;
+       ulong   mapsize, low;
        ulong   of_len = 0;
+       int     bank;
        int     err;
        int     disable_relocation = 0;
 
@@ -198,10 +201,39 @@ int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
                            (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
                }
        } else {
-               of_start =
-                   (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
-                                                  env_get_bootm_mapsize()
-                                                  + env_get_bootm_low());
+               mapsize = env_get_bootm_mapsize();
+               low = env_get_bootm_low();
+               of_start = NULL;
+
+               for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
+                       start = gd->bd->bi_dram[bank].start;
+                       size = gd->bd->bi_dram[bank].size;
+
+                       /* DRAM bank addresses are too low, skip it. */
+                       if (start + size < low)
+                               continue;
+
+                       usable = min(size, (u64)mapsize);
+
+                       /*
+                        * At least part of this DRAM bank is usable, try
+                        * using it for LMB allocation.
+                        */
+                       of_start =
+                           (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
+                                                          start + usable);
+                       /* Allocation succeeded, use this block. */
+                       if (of_start != NULL)
+                               break;
+
+                       /*
+                        * Reduce the mapping size in the next bank
+                        * by the size of attempt in current bank.
+                        */
+                       mapsize -= usable - max(start, (u64)low);
+                       if (!mapsize)
+                               break;
+               }
        }
 
        if (of_start == NULL) {
--
Quoted 2 lines

2.30.2

Regards,
Simon

#2 lore raw
Quoted 27 lines · Simon Glass

Hi Marek,

Quoted 16 lines · Marek Vasut

The current implementation of boot_relocate_fdt() places DT at the highest usable DRAM address, which is calculated as:
env_get_bootm_low() + env_get_bootm_mapsize()
which by default becomes gd->ram_base + gd->ram_size.

Systems like i.MX53 can have multiple DRAM banks with gap between them, e.g. have DRAM at 0x70000000-0x8fffffff and 0xb0000000-0xcfffffff , so for them the calculated highest DRAM address is 0xafffffff, which is exactly in the gap and thus not usable.

Fix this by iterating over all DRAM banks and tracking the remaining amount of the total mapping size obtained from env_get_bootm_mapsize(). Limit the maximum LMB area size to each bank, to avoid using nonexistent DRAM.

Quoted 7 lines
Cc Heinrich Schuchardt <[email protected]>
Cc Simon Glass <[email protected]>
Cc Tom Rini <[email protected]>
---
  common/image-fdt.c | 40 ++++++++++++++++++++++++++++++++++++----
  1 file changed, 36 insertions(+), 4 deletions(-)
Quoted 3 lines

Should we put this behind a Kconfig option to reduce code size?

Since this depends on DT content, we cannot predict what kind of DT will be passed to U-Boot, so no, we cannot put this behind a Kconfig option.

#3 lore raw

Hi Marek,

Quoted 29 lines · Marek Vasut
Quoted 27 lines · Simon Glass

Hi Marek,

Quoted 23 lines · Marek Vasut

The current implementation of boot_relocate_fdt() places DT at the highest usable DRAM address, which is calculated as:
env_get_bootm_low() + env_get_bootm_mapsize()
which by default becomes gd->ram_base + gd->ram_size.

Systems like i.MX53 can have multiple DRAM banks with gap between them, e.g. have DRAM at 0x70000000-0x8fffffff and 0xb0000000-0xcfffffff , so for them the calculated highest DRAM address is 0xafffffff, which is exactly in the gap and thus not usable.

Fix this by iterating over all DRAM banks and tracking the remaining amount of the total mapping size obtained from env_get_bootm_mapsize(). Limit the maximum LMB area size to each bank, to avoid using nonexistent DRAM.

Cc Heinrich Schuchardt <[email protected]>
Cc Simon Glass <[email protected]>
Cc Tom Rini <[email protected]>
---
  common/image-fdt.c | 40 ++++++++++++++++++++++++++++++++++++----
  1 file changed, 36 insertions(+), 4 deletions(-)
Quoted 6 lines
Quoted 3 lines

Should we put this behind a Kconfig option to reduce code size?

Since this depends on DT content, we cannot predict what kind of DT will be passed to U-Boot, so no, we cannot put this behind a Kconfig option.

Doesn't it only affect boards with disjoint memory?

Regards,
Simon

#4 lore raw
Quoted 32 lines · Simon Glass

Hi Marek,

Quoted 29 lines · Marek Vasut
Quoted 27 lines · Simon Glass

Hi Marek,

Quoted 23 lines · Marek Vasut

The current implementation of boot_relocate_fdt() places DT at the highest usable DRAM address, which is calculated as:
env_get_bootm_low() + env_get_bootm_mapsize()
which by default becomes gd->ram_base + gd->ram_size.

Systems like i.MX53 can have multiple DRAM banks with gap between them, e.g. have DRAM at 0x70000000-0x8fffffff and 0xb0000000-0xcfffffff , so for them the calculated highest DRAM address is 0xafffffff, which is exactly in the gap and thus not usable.

Fix this by iterating over all DRAM banks and tracking the remaining amount of the total mapping size obtained from env_get_bootm_mapsize(). Limit the maximum LMB area size to each bank, to avoid using nonexistent DRAM.

Cc Heinrich Schuchardt <[email protected]>
Cc Simon Glass <[email protected]>
Cc Tom Rini <[email protected]>
---
   common/image-fdt.c | 40 ++++++++++++++++++++++++++++++++++++----
   1 file changed, 36 insertions(+), 4 deletions(-)
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Should we put this behind a Kconfig option to reduce code size?

Since this depends on DT content, we cannot predict what kind of DT will be passed to U-Boot, so no, we cannot put this behind a Kconfig option.

Doesn't it only affect boards with disjoint memory?

Sure, it does trigger nasty unexpected failures on some systems. And you cannot really tell whether a board may or may not be populated with such a memory setup, because you cannot predict what will be in the DT.

Besides, there is far more code which correctly does handle multiple memory areas and it is also not ifdef'd out, so I don't see why we should special case this one. That would only lead to inconsistency and more people running into this kind of problem and wasting a lot of time trying to figure out a fix, and then arriving at some loadaddr workaround instead.