diff mbox

[U-Boot,v2,01/19] Introduce board_init_f_mem() to handle early memory layout

Message ID 1423335112-24585-2-git-send-email-sjg@chromium.org
State Accepted
Delegated to: Simon Glass
Headers show

Commit Message

Simon Glass Feb. 7, 2015, 6:51 p.m. UTC
At present on some architectures we set up the following before calling
board_init_f():

   - global_data
   - stack
   - early malloc memory

Adding the code to support early malloc and global data setup to every
arch's assembler start-up is a pain. Also this code is not actually
architecture-specific. We can use common code for all architectures and
with a bit of care we can write this code in C.

Add a new function to deal with this. It should be called after memory
is available, with a pointer to the top of the area that should be used
before relocation. The function will set things up and return the lowest
memory address that it allocated/used. That can then be set as the top
of the stack.

Note that on some archs this function will use the stack, so the stack
pointer should be set to same value as is pased to board_init_f_mem().
A margin of 128 bytes will be left for this stack, so that it is not
overwritten. This means that 64 bytes is wasted by this early call.
This is not strictly necessary on several more modern archs, so we could
remove this at the cost of some arch-dependent code.

With this function there is no-longer any need for the assembler code to
zero global_data or set up the early malloc pointers.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

Changes in v2:
- Reduce reserved stack space for board_init_f_mem() to 64 bytes

 common/board_f.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

Comments

Simon Glass Feb. 11, 2015, 7:35 p.m. UTC | #1
On 7 February 2015 at 11:51, Simon Glass <sjg@chromium.org> wrote:
> At present on some architectures we set up the following before calling
> board_init_f():
>
>    - global_data
>    - stack
>    - early malloc memory
>
> Adding the code to support early malloc and global data setup to every
> arch's assembler start-up is a pain. Also this code is not actually
> architecture-specific. We can use common code for all architectures and
> with a bit of care we can write this code in C.
>
> Add a new function to deal with this. It should be called after memory
> is available, with a pointer to the top of the area that should be used
> before relocation. The function will set things up and return the lowest
> memory address that it allocated/used. That can then be set as the top
> of the stack.
>
> Note that on some archs this function will use the stack, so the stack
> pointer should be set to same value as is pased to board_init_f_mem().
> A margin of 128 bytes will be left for this stack, so that it is not
> overwritten. This means that 64 bytes is wasted by this early call.
> This is not strictly necessary on several more modern archs, so we could
> remove this at the cost of some arch-dependent code.
>
> With this function there is no-longer any need for the assembler code to
> zero global_data or set up the early malloc pointers.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
> Changes in v2:
> - Reduce reserved stack space for board_init_f_mem() to 64 bytes
>
>  common/board_f.c | 18 ++++++++++++++++++
>  1 file changed, 18 insertions(+)

Applied to u-boot-dm.
diff mbox

Patch

diff --git a/common/board_f.c b/common/board_f.c
index 7953137..07f5118 100644
--- a/common/board_f.c
+++ b/common/board_f.c
@@ -1075,4 +1075,22 @@  void board_init_f_r(void)
 	/* NOTREACHED - board_init_r() does not return */
 	hang();
 }
+#else
+ulong board_init_f_mem(ulong top)
+{
+	/* Leave space for the stack we are running with now */
+	top -= 0x40;
+
+	top -= sizeof(struct global_data);
+	top = ALIGN(top, 16);
+	gd = (struct global_data *)top;
+	memset((void *)gd, '\0', sizeof(*gd));
+
+#ifdef CONFIG_SYS_MALLOC_F_LEN
+	top -= CONFIG_SYS_MALLOC_F_LEN;
+	gd->malloc_base = top;
+#endif
+
+	return top;
+}
 #endif /* CONFIG_X86 */