blob: 4c4f0ced535cffece112215615accc9690276d1c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassaf6bbd42015-10-19 06:49:56 -06002/*
3 * Code shared between SPL and U-Boot proper
4 *
5 * Copyright (c) 2015 Google, Inc
6 * Written by Simon Glass <sjg@chromium.org>
Simon Glassaf6bbd42015-10-19 06:49:56 -06007 */
8
9#include <common.h>
Simon Glass52f24232020-05-10 11:40:00 -060010#include <bootstage.h>
Simon Glassaf6bbd42015-10-19 06:49:56 -060011
12DECLARE_GLOBAL_DATA_PTR;
13
Albert ARIBAUDadc421e2015-11-25 17:56:33 +010014/* Unfortunately x86 or ARM can't compile this code as gd cannot be assigned */
15#if !defined(CONFIG_X86) && !defined(CONFIG_ARM)
Simon Glassaf6bbd42015-10-19 06:49:56 -060016__weak void arch_setup_gd(struct global_data *gd_ptr)
17{
18 gd = gd_ptr;
19}
Albert ARIBAUDadc421e2015-11-25 17:56:33 +010020#endif /* !CONFIG_X86 && !CONFIG_ARM */
Simon Glassaf6bbd42015-10-19 06:49:56 -060021
Simon Goldschmidtd8c03322019-07-16 22:30:36 +020022/**
Simon Goldschmidtc82abaa2019-11-11 22:30:46 +010023 * This function is called from board_init_f_init_reserve to set up
24 * gd->start_addr_sp for stack protection if not already set otherwise
25 */
26__weak void board_init_f_init_stack_protection_addr(ulong base)
27{
28#if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
29 /* set up stack pointer for stack usage if not set yet */
30 if (!gd->start_addr_sp)
31 gd->start_addr_sp = base;
32#endif
33}
34
35/**
Simon Goldschmidtd8c03322019-07-16 22:30:36 +020036 * This function is called after the position of the initial stack is
37 * determined in gd->start_addr_sp. Boards can override it to set up
38 * stack-checking markers.
39 */
40__weak void board_init_f_init_stack_protection(void)
41{
42#if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
43 ulong stack_bottom = gd->start_addr_sp -
44 CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK);
45
46 /* substact some safety margin (0x20) since stack is in use here */
47 memset((void *)stack_bottom, CONFIG_VAL(SYS_STACK_F_CHECK_BYTE),
48 CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK) - 0x20);
49#endif
50}
51
Albert ARIBAUDecc30662015-11-25 17:56:32 +010052/*
53 * Allocate reserved space for use as 'globals' from 'top' address and
54 * return 'bottom' address of allocated space
55 *
56 * Notes:
57 *
58 * Actual reservation cannot be done from within this function as
59 * it requires altering the C stack pointer, so this will be done by
60 * the caller upon return from this function.
61 *
62 * IMPORTANT:
63 *
64 * Alignment constraints may differ for each 'chunk' allocated. For now:
65 *
66 * - GD is aligned down on a 16-byte boundary
67 *
68 * - the early malloc arena is not aligned, therefore it follows the stack
69 * alignment constraint of the architecture for which we are bulding.
70 *
71 * - GD is allocated last, so that the return value of this functions is
72 * both the bottom of the reserved area and the address of GD, should
73 * the calling context need it.
74 */
75
76ulong board_init_f_alloc_reserve(ulong top)
77{
78 /* Reserve early malloc arena */
Andy Yanf1896c42017-07-24 17:43:34 +080079#if CONFIG_VAL(SYS_MALLOC_F_LEN)
80 top -= CONFIG_VAL(SYS_MALLOC_F_LEN);
Albert ARIBAUDecc30662015-11-25 17:56:32 +010081#endif
82 /* LAST : reserve GD (rounded up to a multiple of 16 bytes) */
83 top = rounddown(top-sizeof(struct global_data), 16);
84
85 return top;
86}
87
88/*
89 * Initialize reserved space (which has been safely allocated on the C
90 * stack from the C runtime environment handling code).
91 *
92 * Notes:
93 *
94 * Actual reservation was done by the caller; the locations from base
95 * to base+size-1 (where 'size' is the value returned by the allocation
96 * function above) can be accessed freely without risk of corrupting the
97 * C runtime environment.
98 *
99 * IMPORTANT:
100 *
101 * Upon return from the allocation function above, on some architectures
102 * the caller will set gd to the lowest reserved location. Therefore, in
103 * this initialization function, the global data MUST be placed at base.
104 *
105 * ALSO IMPORTANT:
106 *
107 * On some architectures, gd will already be good when entering this
108 * function. On others, it will only be good once arch_setup_gd() returns.
109 * Therefore, global data accesses must be done:
110 *
111 * - through gd_ptr if before the call to arch_setup_gd();
112 *
113 * - through gd once arch_setup_gd() has been called.
114 *
115 * Do not use 'gd->' until arch_setup_gd() has been called!
116 *
117 * IMPORTANT TOO:
118 *
119 * Initialization for each "chunk" (GD, early malloc arena...) ends with
120 * an incrementation line of the form 'base += <some size>'. The last of
121 * these incrementations seems useless, as base will not be used any
122 * more after this incrementation; but if/when a new "chunk" is appended,
123 * this increment will be essential as it will give base right value for
124 * this new chunk (which will have to end with its own incrementation
125 * statement). Besides, the compiler's optimizer will silently detect
126 * and remove the last base incrementation, therefore leaving that last
127 * (seemingly useless) incrementation causes no code increase.
128 */
129
130void board_init_f_init_reserve(ulong base)
Simon Glassaf6bbd42015-10-19 06:49:56 -0600131{
132 struct global_data *gd_ptr;
133
Albert ARIBAUDecc30662015-11-25 17:56:32 +0100134 /*
135 * clear GD entirely and set it up.
136 * Use gd_ptr, as gd may not be properly set yet.
137 */
Simon Glassaf6bbd42015-10-19 06:49:56 -0600138
Albert ARIBAUDecc30662015-11-25 17:56:32 +0100139 gd_ptr = (struct global_data *)base;
140 /* zero the area */
Simon Glassaf6bbd42015-10-19 06:49:56 -0600141 memset(gd_ptr, '\0', sizeof(*gd));
Albert ARIBAUDecc30662015-11-25 17:56:32 +0100142 /* set GD unless architecture did it already */
Simon Glassaf7a5552016-01-15 05:23:23 -0700143#if !defined(CONFIG_ARM)
Simon Glassaf6bbd42015-10-19 06:49:56 -0600144 arch_setup_gd(gd_ptr);
Albert ARIBAUDecc30662015-11-25 17:56:32 +0100145#endif
Simon Goldschmidtc82abaa2019-11-11 22:30:46 +0100146
147 if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
148 board_init_f_init_stack_protection_addr(base);
149
Albert ARIBAUDecc30662015-11-25 17:56:32 +0100150 /* next alloc will be higher by one GD plus 16-byte alignment */
151 base += roundup(sizeof(struct global_data), 16);
152
153 /*
154 * record early malloc arena start.
155 * Use gd as it is now properly set for all architectures.
156 */
Simon Glassaf6bbd42015-10-19 06:49:56 -0600157
Andy Yanf1896c42017-07-24 17:43:34 +0800158#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Albert ARIBAUDecc30662015-11-25 17:56:32 +0100159 /* go down one 'early malloc arena' */
160 gd->malloc_base = base;
Simon Glassaf6bbd42015-10-19 06:49:56 -0600161#endif
Simon Goldschmidtd8c03322019-07-16 22:30:36 +0200162
163 if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
164 board_init_f_init_stack_protection();
Simon Glassaf6bbd42015-10-19 06:49:56 -0600165}
Heiko Schocher496c5482016-06-07 08:31:20 +0200166
167/*
168 * Board-specific Platform code can reimplement show_boot_progress () if needed
169 */
170__weak void show_boot_progress(int val) {}