1 // SPDX-License-Identifier: GPL-2.0+
3 * Code shared between SPL and U-Boot proper
5 * Copyright (c) 2015 Google, Inc
10 #include <bootstage.h>
12 #include <asm/global_data.h>
14 DECLARE_GLOBAL_DATA_PTR;
16 /* Unfortunately x86 or ARM can't compile this code as gd cannot be assigned */
17 #if !defined(CONFIG_X86) && !defined(CONFIG_ARM)
18 __weak void arch_setup_gd(struct global_data *gd_ptr)
22 #endif /* !CONFIG_X86 && !CONFIG_ARM */
25 * This function is called from board_init_f_init_reserve to set up
26 * gd->start_addr_sp for stack protection if not already set otherwise
28 __weak void board_init_f_init_stack_protection_addr(ulong base)
30 #if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
31 /* set up stack pointer for stack usage if not set yet */
32 if (!gd->start_addr_sp)
33 gd->start_addr_sp = base;
38 * This function is called after the position of the initial stack is
39 * determined in gd->start_addr_sp. Boards can override it to set up
40 * stack-checking markers.
42 __weak void board_init_f_init_stack_protection(void)
44 #if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
45 ulong stack_bottom = gd->start_addr_sp -
46 CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK);
48 /* substact some safety margin (0x20) since stack is in use here */
49 memset((void *)stack_bottom, CONFIG_VAL(SYS_STACK_F_CHECK_BYTE),
50 CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK) - 0x20);
55 * Allocate reserved space for use as 'globals' from 'top' address and
56 * return 'bottom' address of allocated space
60 * Actual reservation cannot be done from within this function as
61 * it requires altering the C stack pointer, so this will be done by
62 * the caller upon return from this function.
66 * Alignment constraints may differ for each 'chunk' allocated. For now:
68 * - GD is aligned down on a 16-byte boundary
70 * - the early malloc arena is not aligned, therefore it follows the stack
71 * alignment constraint of the architecture for which we are bulding.
73 * - GD is allocated last, so that the return value of this functions is
74 * both the bottom of the reserved area and the address of GD, should
75 * the calling context need it.
78 ulong board_init_f_alloc_reserve(ulong top)
80 /* Reserve early malloc arena */
81 #ifndef CFG_MALLOC_F_ADDR
82 #if CONFIG_IS_ENABLED(SYS_MALLOC_F)
83 top -= CONFIG_VAL(SYS_MALLOC_F_LEN);
86 /* LAST : reserve GD (rounded up to a multiple of 16 bytes) */
87 top = rounddown(top-sizeof(struct global_data), 16);
93 * Initialize reserved space (which has been safely allocated on the C
94 * stack from the C runtime environment handling code).
98 * Actual reservation was done by the caller; the locations from base
99 * to base+size-1 (where 'size' is the value returned by the allocation
100 * function above) can be accessed freely without risk of corrupting the
101 * C runtime environment.
105 * Upon return from the allocation function above, on some architectures
106 * the caller will set gd to the lowest reserved location. Therefore, in
107 * this initialization function, the global data MUST be placed at base.
111 * On some architectures, gd will already be good when entering this
112 * function. On others, it will only be good once arch_setup_gd() returns.
113 * Therefore, global data accesses must be done:
115 * - through gd_ptr if before the call to arch_setup_gd();
117 * - through gd once arch_setup_gd() has been called.
119 * Do not use 'gd->' until arch_setup_gd() has been called!
123 * Initialization for each "chunk" (GD, early malloc arena...) ends with
124 * an incrementation line of the form 'base += <some size>'. The last of
125 * these incrementations seems useless, as base will not be used any
126 * more after this incrementation; but if/when a new "chunk" is appended,
127 * this increment will be essential as it will give base right value for
128 * this new chunk (which will have to end with its own incrementation
129 * statement). Besides, the compiler's optimizer will silently detect
130 * and remove the last base incrementation, therefore leaving that last
131 * (seemingly useless) incrementation causes no code increase.
134 void board_init_f_init_reserve(ulong base)
136 struct global_data *gd_ptr;
139 * clear GD entirely and set it up.
140 * Use gd_ptr, as gd may not be properly set yet.
143 gd_ptr = (struct global_data *)base;
145 memset(gd_ptr, '\0', sizeof(*gd));
146 /* set GD unless architecture did it already */
147 #if !defined(CONFIG_ARM)
148 arch_setup_gd(gd_ptr);
151 if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
152 board_init_f_init_stack_protection_addr(base);
154 /* next alloc will be higher by one GD plus 16-byte alignment */
155 base += roundup(sizeof(struct global_data), 16);
158 * record early malloc arena start.
159 * Use gd as it is now properly set for all architectures.
162 #if CONFIG_IS_ENABLED(SYS_MALLOC_F)
163 /* go down one 'early malloc arena' */
164 gd->malloc_base = base;
165 #if CONFIG_IS_ENABLED(ZERO_MEM_BEFORE_USE)
166 memset((void *)base, '\0', CONFIG_VAL(SYS_MALLOC_F_LEN));
170 if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
171 board_init_f_init_stack_protection();
174 #if CONFIG_IS_ENABLED(SHOW_BOOT_PROGRESS)
176 * Board-specific Platform code can reimplement show_boot_progress () if needed
178 __weak void show_boot_progress(int val) {}