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