blob: 526fee35ff679876aa9a14e6bdf8be674e93c69d [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
9#include <common.h>
10
11DECLARE_GLOBAL_DATA_PTR;
12
Albert ARIBAUD85260bb2015-11-25 17:56:33 +010013/* Unfortunately x86 or ARM can't compile this code as gd cannot be assigned */
14#if !defined(CONFIG_X86) && !defined(CONFIG_ARM)
Simon Glassb1beb112015-10-19 06:49:56 -060015__weak void arch_setup_gd(struct global_data *gd_ptr)
16{
17 gd = gd_ptr;
18}
Albert ARIBAUD85260bb2015-11-25 17:56:33 +010019#endif /* !CONFIG_X86 && !CONFIG_ARM */
Simon Glassb1beb112015-10-19 06:49:56 -060020
Albert ARIBAUD6cb4c462015-11-25 17:56:32 +010021/*
22 * Allocate reserved space for use as 'globals' from 'top' address and
23 * return 'bottom' address of allocated space
24 *
25 * Notes:
26 *
27 * Actual reservation cannot be done from within this function as
28 * it requires altering the C stack pointer, so this will be done by
29 * the caller upon return from this function.
30 *
31 * IMPORTANT:
32 *
33 * Alignment constraints may differ for each 'chunk' allocated. For now:
34 *
35 * - GD is aligned down on a 16-byte boundary
36 *
37 * - the early malloc arena is not aligned, therefore it follows the stack
38 * alignment constraint of the architecture for which we are bulding.
39 *
40 * - GD is allocated last, so that the return value of this functions is
41 * both the bottom of the reserved area and the address of GD, should
42 * the calling context need it.
43 */
44
45ulong board_init_f_alloc_reserve(ulong top)
46{
47 /* Reserve early malloc arena */
Andy Yan1fa20e4d2017-07-24 17:43:34 +080048#if CONFIG_VAL(SYS_MALLOC_F_LEN)
49 top -= CONFIG_VAL(SYS_MALLOC_F_LEN);
Albert ARIBAUD6cb4c462015-11-25 17:56:32 +010050#endif
51 /* LAST : reserve GD (rounded up to a multiple of 16 bytes) */
52 top = rounddown(top-sizeof(struct global_data), 16);
53
54 return top;
55}
56
57/*
58 * Initialize reserved space (which has been safely allocated on the C
59 * stack from the C runtime environment handling code).
60 *
61 * Notes:
62 *
63 * Actual reservation was done by the caller; the locations from base
64 * to base+size-1 (where 'size' is the value returned by the allocation
65 * function above) can be accessed freely without risk of corrupting the
66 * C runtime environment.
67 *
68 * IMPORTANT:
69 *
70 * Upon return from the allocation function above, on some architectures
71 * the caller will set gd to the lowest reserved location. Therefore, in
72 * this initialization function, the global data MUST be placed at base.
73 *
74 * ALSO IMPORTANT:
75 *
76 * On some architectures, gd will already be good when entering this
77 * function. On others, it will only be good once arch_setup_gd() returns.
78 * Therefore, global data accesses must be done:
79 *
80 * - through gd_ptr if before the call to arch_setup_gd();
81 *
82 * - through gd once arch_setup_gd() has been called.
83 *
84 * Do not use 'gd->' until arch_setup_gd() has been called!
85 *
86 * IMPORTANT TOO:
87 *
88 * Initialization for each "chunk" (GD, early malloc arena...) ends with
89 * an incrementation line of the form 'base += <some size>'. The last of
90 * these incrementations seems useless, as base will not be used any
91 * more after this incrementation; but if/when a new "chunk" is appended,
92 * this increment will be essential as it will give base right value for
93 * this new chunk (which will have to end with its own incrementation
94 * statement). Besides, the compiler's optimizer will silently detect
95 * and remove the last base incrementation, therefore leaving that last
96 * (seemingly useless) incrementation causes no code increase.
97 */
98
99void board_init_f_init_reserve(ulong base)
Simon Glassb1beb112015-10-19 06:49:56 -0600100{
101 struct global_data *gd_ptr;
102
Albert ARIBAUD6cb4c462015-11-25 17:56:32 +0100103 /*
104 * clear GD entirely and set it up.
105 * Use gd_ptr, as gd may not be properly set yet.
106 */
Simon Glassb1beb112015-10-19 06:49:56 -0600107
Albert ARIBAUD6cb4c462015-11-25 17:56:32 +0100108 gd_ptr = (struct global_data *)base;
109 /* zero the area */
Simon Glassb1beb112015-10-19 06:49:56 -0600110 memset(gd_ptr, '\0', sizeof(*gd));
Albert ARIBAUD6cb4c462015-11-25 17:56:32 +0100111 /* set GD unless architecture did it already */
Simon Glasscfac2e72016-01-15 05:23:23 -0700112#if !defined(CONFIG_ARM)
Simon Glassb1beb112015-10-19 06:49:56 -0600113 arch_setup_gd(gd_ptr);
Albert ARIBAUD6cb4c462015-11-25 17:56:32 +0100114#endif
115 /* next alloc will be higher by one GD plus 16-byte alignment */
116 base += roundup(sizeof(struct global_data), 16);
117
118 /*
119 * record early malloc arena start.
120 * Use gd as it is now properly set for all architectures.
121 */
Simon Glassb1beb112015-10-19 06:49:56 -0600122
Andy Yan1fa20e4d2017-07-24 17:43:34 +0800123#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Albert ARIBAUD6cb4c462015-11-25 17:56:32 +0100124 /* go down one 'early malloc arena' */
125 gd->malloc_base = base;
126 /* next alloc will be higher by one 'early malloc arena' size */
Andy Yan1fa20e4d2017-07-24 17:43:34 +0800127 base += CONFIG_VAL(SYS_MALLOC_F_LEN);
Simon Glassb1beb112015-10-19 06:49:56 -0600128#endif
Simon Glassb1beb112015-10-19 06:49:56 -0600129}
Heiko Schocher747ceee2016-06-07 08:31:20 +0200130
131/*
132 * Board-specific Platform code can reimplement show_boot_progress () if needed
133 */
134__weak void show_boot_progress(int val) {}