Skip to contentSkip to conversation

Add ALIGN() macro

PATCH New Andy Fleming 2 participants 8 messages Jun 16, 2008 – Jul 5, 2008

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/4 Add ALIGN() macro

ALIGN() returns the smallest aligned value greater than the passed in address or size. Taken from Linux.

---
Rebased off the top of tree to ease application.

 include/common.h |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
include/common.h +3 -0
index fd5adb3..3190781 100644
@@ -682,6 +682,9 @@ void __attribute__((weak)) show_boot_progress (int val);
 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
 
+#define ALIGN(x,a)		__ALIGN_MASK((x),(typeof(x))(a)-1)
+#define __ALIGN_MASK(x,mask)	(((x)+(mask))&~(mask))
+
 /* Multicore arch functions */
 #ifdef CONFIG_MP
 int cpu_status(int nr);
Signature · 1 line
1.5.4.GIT
#1 lore raw

PATCH v3 2/4 Add lmb_free

lmb_free allows us to unreserve some memory so we can use lmb_alloc_base or lmb_reserve to temporarily reserve some memory.

---
Rebased off the top of tree to ease application.

 lib_generic/lmb.c |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 49 insertions(+), 0 deletions(-)
lib_generic/lmb.c +49 -0
index afe3319..3240f66 100644
@@ -181,6 +181,55 @@ long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size)
 	return lmb_add_region(_rgn, base, size);
 }
 
+long lmb_free(struct lmb *lmb, u64 base, u64 size)
+{
+	struct lmb_region *rgn = &(lmb->reserved);
+	u64 rgnbegin, rgnend;
+	u64 end = base + size;
+	int i;
+
+	rgnbegin = rgnend = 0; /* supress gcc warnings */
+
+	/* Find the region where (base, size) belongs to */
+	for (i=0; i < rgn->cnt; i++) {
+		rgnbegin = rgn->region[i].base;
+		rgnend = rgnbegin + rgn->region[i].size;
+
+		if ((rgnbegin <= base) && (end <= rgnend))
+			break;
+	}
+
+	/* Didn't find the region */
+	if (i == rgn->cnt)
+		return -1;
+
+	/* Check to see if we are removing entire region */
+	if ((rgnbegin == base) && (rgnend == end)) {
+		lmb_remove_region(rgn, i);
+		return 0;
+	}
+
+	/* Check to see if region is matching at the front */
+	if (rgnbegin == base) {
+		rgn->region[i].base = end;
+		rgn->region[i].size -= size;
+		return 0;
+	}
+
+	/* Check to see if the region is matching at the end */
+	if (rgnend == end) {
+		rgn->region[i].size -= size;
+		return 0;
+	}
+
+	/*
+	 * We need to split the entry -  adjust the current one to the
+	 * beginging of the hole and add the region after hole.
+	 */
+	rgn->region[i].size = base - rgn->region[i].base;
+	return lmb_add_region(rgn, end, rgnend - end);
+}
+
 long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
 {
 	struct lmb_region *_rgn = &(lmb->reserved);
Signature · 1 line
1.5.4.GIT
#2 lore raw

PATCH v3 3/4 Fix an underflow bug in __lmb_alloc_base

__lmb_alloc_base can underflow if it fails to find free space. This was fixed in linux with commit d9024df02ffe74d723d97d552f86de3b34beb8cc. This patch merely updates __lmb_alloc_base to resemble the current version in Linux.

---
Rebased off the top of tree to ease application.

 lib_generic/lmb.c |   34 +++++++++++++++++++---------------
 1 files changed, 19 insertions(+), 15 deletions(-)
lib_generic/lmb.c +19 -15
index 3240f66..93264c1 100644
@@ -285,11 +285,14 @@ phys_addr_t __lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phy
 {
 	long i, j;
 	phys_addr_t base = 0;
+	phys_addr_t res_base;
 
 	for (i = lmb->memory.cnt-1; i >= 0; i--) {
 		phys_addr_t lmbbase = lmb->memory.region[i].base;
 		phys_size_t lmbsize = lmb->memory.region[i].size;
 
+		if (lmbsize < size)
+			continue;
 		if (max_addr == LMB_ALLOC_ANYWHERE)
 			base = lmb_align_down(lmbbase + lmbsize - size, align);
 		else if (lmbbase < max_addr) {
@@ -298,22 +301,23 @@ phys_addr_t __lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phy
 		} else
 			continue;
 
-		while ((lmbbase <= base) &&
-		       ((j = lmb_overlaps_region(&(lmb->reserved), base, size)) >= 0) )
-			base = lmb_align_down(lmb->reserved.region[j].base - size,
-					      align);
-
-		if ((base != 0) && (lmbbase <= base))
-			break;
+		while (base && lmbbase <= base) {
+			j = lmb_overlaps_region(&lmb->reserved, base, size);
+			if (j < 0) {
+				/* This area isn't reserved, take it */
+				if (lmb_add_region(&lmb->reserved, base,
+							lmb_align_up(size,
+								align)) < 0)
+					return 0;
+				return base;
+			}
+			res_base = lmb->reserved.region[j].base;
+			if (res_base < size)
+				break;
+			base = lmb_align_down(res_base - size, align);
+		}
 	}
-
-	if (i < 0)
-		return 0;
-
-	if (lmb_add_region(&(lmb->reserved), base, lmb_align_up(size, align)) < 0)
-		return 0;
-
-	return base;
+	return 0;
 }
 
 int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr)
Signature · 1 line
1.5.4.GIT
#3 lore raw

PATCH v3 4/4 Resize device tree to allow space for board changes and the chosen node

Current code requires that a compiled device tree have space added to the end to leave room for extra nodes added by board code (and the chosen node). This requires that device tree creators anticipate how much space U-Boot will add to the tree, which is absurd. Ideally, the code would resize and/or relocate the tree when it needed more space, but this would require a systemic change to the fdt code, which is non-trivial. Instead, we resize the tree inside boot_relocate_fdt, reserving either the remainder of the bootmap (in the case where the fdt is inside the bootmap), or adding CFG_FDT_PAD bytes to the size.

---
Rebased off the top of tree to ease application.

 lib_ppc/bootm.c |   66 ++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 58 insertions(+), 8 deletions(-)
lib_ppc/bootm.c +58 -8
index 10a0b12..9db1c2e 100644
@@ -51,6 +51,10 @@ static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
 #include <asm/cache.h>
 #endif
 
+#ifndef CFG_FDT_PAD
+#define CFG_FDT_PAD 0x3000
+#endif
+
 DECLARE_GLOBAL_DATA_PTR;
 
 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
@@ -191,6 +195,44 @@ do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
 		ft_board_setup(of_flat_tree, gd->bd);
 #endif
 	}
+
+	/* Fixup the fdt memreserve now that we know how big it is */
+	if (of_flat_tree) {
+		int j;
+		uint64_t addr, size;
+		int total = fdt_num_mem_rsv(of_flat_tree);
+		uint actualsize;
+
+		for (j = 0; j < total; j++) {
+			fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
+			if (addr == (uint64_t)of_flat_tree) {
+				fdt_del_mem_rsv(of_flat_tree, j);
+				break;
+			}
+		}
+
+		/* Delete the old LMB reservation */
+		lmb_free(lmb, of_flat_tree, fdt_totalsize(of_flat_tree));
+
+		/* Calculate the actual size of the fdt */
+		actualsize = fdt_off_dt_strings(of_flat_tree) +
+			fdt_size_dt_strings(of_flat_tree);
+
+		/* Make it so the fdt ends on a page boundary */
+		actualsize = ALIGN(actualsize, 0x1000);
+		actualsize = actualsize - ((uint)of_flat_tree & 0xfff);
+
+		/* Change the fdt header to reflect the correct size */
+		fdt_set_totalsize(of_flat_tree, actualsize);
+		of_size = actualsize;
+
+		/* Add the new reservation */
+		ret = fdt_add_mem_rsv(of_flat_tree, (uint)of_flat_tree,
+				of_size);
+
+		/* Create a new LMB reservation */
+		lmb_reserve(lmb, (ulong)of_flat_tree, of_size);
+	}
 #endif	/* CONFIG_OF_LIBFDT */
 
 	ret = boot_ramdisk_high (lmb, rd_data_start, rd_len, &initrd_start, &initrd_end);
@@ -713,22 +755,25 @@ static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
 #endif
 
 	/*
-	 * The blob must be within CFG_BOOTMAPSZ,
-	 * so we flag it to be copied if it is not.
+	 * The blob needs to be inside the boot mapping.
 	 */
-	if (fdt_blob >= (char *)CFG_BOOTMAPSZ)
+	if (fdt_blob < (char *)bootmap_base)
 		relocate = 1;
 
-	of_len = be32_to_cpu (fdt_totalsize (fdt_blob));
+	if ((fdt_blob + *of_size + CFG_FDT_PAD) >=
+			((char *)CFG_BOOTMAPSZ + bootmap_base))
+		relocate = 1;
 
 	/* move flattend device tree if needed */
 	if (relocate) {
 		int err;
-		ulong of_start;
+		ulong of_start = 0;
 
 		/* position on a 4K boundary before the alloc_current */
+		/* Pad the FDT by a specified amount */
+		of_len = *of_size + CFG_FDT_PAD;
 		of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
-					 (CFG_BOOTMAPSZ + bootmap_base));
+				(CFG_BOOTMAPSZ + bootmap_base));
 
 		if (of_start == 0) {
 			puts("device tree - allocation error\n");
@@ -736,7 +781,7 @@ static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
 		}
 
 		debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
-			(ulong)fdt_blob, (ulong)fdt_blob + of_len - 1,
+			(ulong)fdt_blob, (ulong)fdt_blob + *of_size - 1,
 			of_len, of_len);
 
 		printf ("   Loading Device Tree to %08lx, end %08lx ... ",
@@ -750,9 +795,14 @@ static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
 		puts ("OK\n");
 
 		*of_flat_tree = (char *)of_start;
+		*of_size = of_len;
 	} else {
 		*of_flat_tree = fdt_blob;
-		lmb_reserve(lmb, (ulong)working_fdt, of_len);
+		of_len = (CFG_BOOTMAPSZ + bootmap_base) - (ulong)fdt_blob;
+		lmb_reserve(lmb, (ulong)fdt_blob, of_len);
+		fdt_set_totalsize(*of_flat_tree, of_len);
+
+		*of_size = of_len;
 	}
 
 	return 0;
Signature · 1 line
1.5.4.GIT
#4 lore raw

PATCH v3 4/4 Resize device tree to allow space for board changes and the chosen node

Quoted 9 lines · In message <[email protected]> you wrote:

Current code requires that a compiled device tree have space added to the end to leave room for extra nodes added by board code (and the chosen node). This requires that device tree creators anticipate how much space U-Boot will add to the tree, which is absurd. Ideally, the code would resize and/or relocate the tree when it needed more space, but this would require a systemic change to the fdt code, which is non-trivial. Instead, we resize the tree inside boot_relocate_fdt, reserving either the remainder of the bootmap (in the case where the fdt is inside the bootmap), or adding CFG_FDT_PAD bytes to the size.

Quoted 6 lines

---
Rebased off the top of tree to ease application.

 lib_ppc/bootm.c |   66 ++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 58 insertions(+), 8 deletions(-)

Applied, thanks.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
CAUTION:  The Mass of This Product Contains the Energy Equivalent  of
85 Million Tons of TNT per Net Ounce of Weight.
#5 lore raw

PATCH v3 3/4 Fix an underflow bug in __lmb_alloc_base

In reply to Andy Fleming
Quoted 10 lines · In message <[email protected]> you wrote:

__lmb_alloc_base can underflow if it fails to find free space. This was fixed in linux with commit d9024df02ffe74d723d97d552f86de3b34beb8cc. This patch merely updates __lmb_alloc_base to resemble the current version in Linux.

---
Rebased off the top of tree to ease application.

 lib_generic/lmb.c |   34 +++++++++++++++++++---------------
 1 files changed, 19 insertions(+), 15 deletions(-)

Applied, thanks.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Vulcans believe peace should not depend on force.
	-- Amanda, "Journey to Babel", stardate 3842.3
#6 lore raw

PATCH v3 2/4 Add lmb_free

In reply to Andy Fleming
Quoted 9 lines · In message <[email protected]> you wrote:

lmb_free allows us to unreserve some memory so we can use lmb_alloc_base or lmb_reserve to temporarily reserve some memory.

---
Rebased off the top of tree to ease application.

 lib_generic/lmb.c |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 49 insertions(+), 0 deletions(-)

Applied, thanks.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
"There is such a fine line between genius and stupidity."
- David St. Hubbins, "Spinal Tap"
#7 lore raw

PATCH v3 1/4 Add ALIGN() macro

In reply to Andy Fleming
Quoted 9 lines · In message <[email protected]> you wrote:

ALIGN() returns the smallest aligned value greater than the passed in address or size. Taken from Linux.

---
Rebased off the top of tree to ease application.

 include/common.h |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)

Applied, thanks.

Best regards,

Wolfgang Denk

Signature · 9 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
'What shall we do?' said Twoflower.  'Panic?'  said  Rincewind  hope-
fully. He always held that panic was the best means of survival; back
in  the  olden days, his theory went, people faced with hungry sabre-
toothed tigers could be divided very simply in those who panicked and
those who stood there saying 'What a magnificent  brute!'  or  'Here,
pussy.'                      - Terry Pratchett, _The Light Fantastic_