blob: 2a6f39f51adbc2461f564ef071af774d943f72b3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb1beb112015-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 Glassb1beb112015-10-19 06:49:56 -06007 */
8
Tom Rinidec7ea02024-05-20 13:35:03 -06009#include <config.h>
Simon Glass1ea97892020-05-10 11:40:00 -060010#include <bootstage.h>
Simon Glass97589732020-05-10 11:40:02 -060011#include <init.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Simon Glassb1beb112015-10-19 06:49:56 -060013
14DECLARE_GLOBAL_DATA_PTR;
15
Yao Zi73b4c2a2025-04-27 14:50:11 +000016/*
17 * Unfortunately x86, ARM and RISC-V can't compile this code as gd is defined
18 * as macro and cannot be assigned.
19 */
20#if !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
Simon Glassb1beb112015-10-19 06:49:56 -060021__weak void arch_setup_gd(struct global_data *gd_ptr)
22{
23 gd = gd_ptr;
24}
Albert ARIBAUD85260bb2015-11-25 17:56:33 +010025#endif /* !CONFIG_X86 && !CONFIG_ARM */
Simon Glassb1beb112015-10-19 06:49:56 -060026
Simon Goldschmidt0b820852019-07-16 22:30:36 +020027/**
Simon Goldschmidt3528eab2019-11-11 22:30:46 +010028 * This function is called from board_init_f_init_reserve to set up
29 * gd->start_addr_sp for stack protection if not already set otherwise
30 */
31__weak void board_init_f_init_stack_protection_addr(ulong base)
32{
33#if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
34 /* set up stack pointer for stack usage if not set yet */
35 if (!gd->start_addr_sp)
36 gd->start_addr_sp = base;
37#endif
38}
39
40/**
Simon Goldschmidt0b820852019-07-16 22:30:36 +020041 * This function is called after the position of the initial stack is
42 * determined in gd->start_addr_sp. Boards can override it to set up
43 * stack-checking markers.
44 */
45__weak void board_init_f_init_stack_protection(void)
46{
47#if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
48 ulong stack_bottom = gd->start_addr_sp -
49 CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK);
50
51 /* substact some safety margin (0x20) since stack is in use here */
52 memset((void *)stack_bottom, CONFIG_VAL(SYS_STACK_F_CHECK_BYTE),
53 CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK) - 0x20);
54#endif
55}
56
Albert ARIBAUD6cb4c462015-11-25 17:56:32 +010057/*
58 * Allocate reserved space for use as 'globals' from 'top' address and
59 * return 'bottom' address of allocated space
60 *
61 * Notes:
62 *
63 * Actual reservation cannot be done from within this function as
64 * it requires altering the C stack pointer, so this will be done by
65 * the caller upon return from this function.
66 *
67 * IMPORTANT:
68 *
69 * Alignment constraints may differ for each 'chunk' allocated. For now:
70 *
71 * - GD is aligned down on a 16-byte boundary
72 *
73 * - the early malloc arena is not aligned, therefore it follows the stack
74 * alignment constraint of the architecture for which we are bulding.
75 *
76 * - GD is allocated last, so that the return value of this functions is
77 * both the bottom of the reserved area and the address of GD, should
78 * the calling context need it.
79 */
80
81ulong board_init_f_alloc_reserve(ulong top)
82{
83 /* Reserve early malloc arena */
Tom Rinifb52b942022-12-04 10:04:49 -050084#ifndef CFG_MALLOC_F_ADDR
Simon Glassadad2d02023-09-26 08:14:27 -060085#if CONFIG_IS_ENABLED(SYS_MALLOC_F)
Andy Yan1fa20e4d2017-07-24 17:43:34 +080086 top -= CONFIG_VAL(SYS_MALLOC_F_LEN);
Albert ARIBAUD6cb4c462015-11-25 17:56:32 +010087#endif
Marek Vasut77e22702022-06-25 19:58:24 +020088#endif
Albert ARIBAUD6cb4c462015-11-25 17:56:32 +010089 /* LAST : reserve GD (rounded up to a multiple of 16 bytes) */
90 top = rounddown(top-sizeof(struct global_data), 16);
91
92 return top;
93}
94
95/*
96 * Initialize reserved space (which has been safely allocated on the C
97 * stack from the C runtime environment handling code).
98 *
99 * Notes:
100 *
101 * Actual reservation was done by the caller; the locations from base
102 * to base+size-1 (where 'size' is the value returned by the allocation
103 * function above) can be accessed freely without risk of corrupting the
104 * C runtime environment.
105 *
106 * IMPORTANT:
107 *
108 * Upon return from the allocation function above, on some architectures
109 * the caller will set gd to the lowest reserved location. Therefore, in
110 * this initialization function, the global data MUST be placed at base.
111 *
112 * ALSO IMPORTANT:
113 *
114 * On some architectures, gd will already be good when entering this
115 * function. On others, it will only be good once arch_setup_gd() returns.
116 * Therefore, global data accesses must be done:
117 *
118 * - through gd_ptr if before the call to arch_setup_gd();
119 *
120 * - through gd once arch_setup_gd() has been called.
121 *
122 * Do not use 'gd->' until arch_setup_gd() has been called!
123 *
124 * IMPORTANT TOO:
125 *
126 * Initialization for each "chunk" (GD, early malloc arena...) ends with
127 * an incrementation line of the form 'base += <some size>'. The last of
128 * these incrementations seems useless, as base will not be used any
129 * more after this incrementation; but if/when a new "chunk" is appended,
130 * this increment will be essential as it will give base right value for
131 * this new chunk (which will have to end with its own incrementation
132 * statement). Besides, the compiler's optimizer will silently detect
133 * and remove the last base incrementation, therefore leaving that last
134 * (seemingly useless) incrementation causes no code increase.
135 */
136
137void board_init_f_init_reserve(ulong base)
Simon Glassb1beb112015-10-19 06:49:56 -0600138{
139 struct global_data *gd_ptr;
140
Albert ARIBAUD6cb4c462015-11-25 17:56:32 +0100141 /*
142 * clear GD entirely and set it up.
143 * Use gd_ptr, as gd may not be properly set yet.
144 */
Simon Glassb1beb112015-10-19 06:49:56 -0600145
Albert ARIBAUD6cb4c462015-11-25 17:56:32 +0100146 gd_ptr = (struct global_data *)base;
147 /* zero the area */
Simon Glassb1beb112015-10-19 06:49:56 -0600148 memset(gd_ptr, '\0', sizeof(*gd));
Albert ARIBAUD6cb4c462015-11-25 17:56:32 +0100149 /* set GD unless architecture did it already */
Simon Glasscfac2e72016-01-15 05:23:23 -0700150#if !defined(CONFIG_ARM)
Simon Glassb1beb112015-10-19 06:49:56 -0600151 arch_setup_gd(gd_ptr);
Albert ARIBAUD6cb4c462015-11-25 17:56:32 +0100152#endif
Simon Goldschmidt3528eab2019-11-11 22:30:46 +0100153
154 if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
155 board_init_f_init_stack_protection_addr(base);
156
Albert ARIBAUD6cb4c462015-11-25 17:56:32 +0100157 /* next alloc will be higher by one GD plus 16-byte alignment */
158 base += roundup(sizeof(struct global_data), 16);
159
160 /*
161 * record early malloc arena start.
162 * Use gd as it is now properly set for all architectures.
163 */
Simon Glassb1beb112015-10-19 06:49:56 -0600164
Simon Glassadad2d02023-09-26 08:14:27 -0600165#if CONFIG_IS_ENABLED(SYS_MALLOC_F)
Albert ARIBAUD6cb4c462015-11-25 17:56:32 +0100166 /* go down one 'early malloc arena' */
167 gd->malloc_base = base;
Shengyu Qu62b89a12023-08-09 21:11:32 +0800168#if CONFIG_IS_ENABLED(ZERO_MEM_BEFORE_USE)
169 memset((void *)base, '\0', CONFIG_VAL(SYS_MALLOC_F_LEN));
170#endif
Simon Glassb1beb112015-10-19 06:49:56 -0600171#endif
Simon Goldschmidt0b820852019-07-16 22:30:36 +0200172
173 if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
174 board_init_f_init_stack_protection();
Simon Glassb1beb112015-10-19 06:49:56 -0600175}
Heiko Schocher747ceee2016-06-07 08:31:20 +0200176
Marek Vasut98154342021-10-23 03:06:03 +0200177#if CONFIG_IS_ENABLED(SHOW_BOOT_PROGRESS)
Heiko Schocher747ceee2016-06-07 08:31:20 +0200178/*
179 * Board-specific Platform code can reimplement show_boot_progress () if needed
180 */
181__weak void show_boot_progress(int val) {}
Tom Rinia9765d02021-05-03 16:48:58 -0400182#endif