blob: bf4255b4aebaf2382fddf3d59bd349d33f2584e2 [file] [log] [blame]
Simon Glassaf6bbd42015-10-19 06:49:56 -06001/*
2 * Code shared between SPL and U-Boot proper
3 *
4 * Copyright (c) 2015 Google, Inc
5 * Written by Simon Glass <sjg@chromium.org>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10#include <common.h>
11
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
Albert ARIBAUDecc30662015-11-25 17:56:32 +010022/*
23 * Allocate reserved space for use as 'globals' from 'top' address and
24 * return 'bottom' address of allocated space
25 *
26 * Notes:
27 *
28 * Actual reservation cannot be done from within this function as
29 * it requires altering the C stack pointer, so this will be done by
30 * the caller upon return from this function.
31 *
32 * IMPORTANT:
33 *
34 * Alignment constraints may differ for each 'chunk' allocated. For now:
35 *
36 * - GD is aligned down on a 16-byte boundary
37 *
38 * - the early malloc arena is not aligned, therefore it follows the stack
39 * alignment constraint of the architecture for which we are bulding.
40 *
41 * - GD is allocated last, so that the return value of this functions is
42 * both the bottom of the reserved area and the address of GD, should
43 * the calling context need it.
44 */
45
46ulong board_init_f_alloc_reserve(ulong top)
47{
48 /* Reserve early malloc arena */
49#if defined(CONFIG_SYS_MALLOC_F)
50 top -= CONFIG_SYS_MALLOC_F_LEN;
51#endif
52 /* LAST : reserve GD (rounded up to a multiple of 16 bytes) */
53 top = rounddown(top-sizeof(struct global_data), 16);
54
55 return top;
56}
57
58/*
59 * Initialize reserved space (which has been safely allocated on the C
60 * stack from the C runtime environment handling code).
61 *
62 * Notes:
63 *
64 * Actual reservation was done by the caller; the locations from base
65 * to base+size-1 (where 'size' is the value returned by the allocation
66 * function above) can be accessed freely without risk of corrupting the
67 * C runtime environment.
68 *
69 * IMPORTANT:
70 *
71 * Upon return from the allocation function above, on some architectures
72 * the caller will set gd to the lowest reserved location. Therefore, in
73 * this initialization function, the global data MUST be placed at base.
74 *
75 * ALSO IMPORTANT:
76 *
77 * On some architectures, gd will already be good when entering this
78 * function. On others, it will only be good once arch_setup_gd() returns.
79 * Therefore, global data accesses must be done:
80 *
81 * - through gd_ptr if before the call to arch_setup_gd();
82 *
83 * - through gd once arch_setup_gd() has been called.
84 *
85 * Do not use 'gd->' until arch_setup_gd() has been called!
86 *
87 * IMPORTANT TOO:
88 *
89 * Initialization for each "chunk" (GD, early malloc arena...) ends with
90 * an incrementation line of the form 'base += <some size>'. The last of
91 * these incrementations seems useless, as base will not be used any
92 * more after this incrementation; but if/when a new "chunk" is appended,
93 * this increment will be essential as it will give base right value for
94 * this new chunk (which will have to end with its own incrementation
95 * statement). Besides, the compiler's optimizer will silently detect
96 * and remove the last base incrementation, therefore leaving that last
97 * (seemingly useless) incrementation causes no code increase.
98 */
99
100void board_init_f_init_reserve(ulong base)
Simon Glassaf6bbd42015-10-19 06:49:56 -0600101{
102 struct global_data *gd_ptr;
103
Albert ARIBAUDecc30662015-11-25 17:56:32 +0100104 /*
105 * clear GD entirely and set it up.
106 * Use gd_ptr, as gd may not be properly set yet.
107 */
Simon Glassaf6bbd42015-10-19 06:49:56 -0600108
Albert ARIBAUDecc30662015-11-25 17:56:32 +0100109 gd_ptr = (struct global_data *)base;
110 /* zero the area */
Simon Glassaf6bbd42015-10-19 06:49:56 -0600111 memset(gd_ptr, '\0', sizeof(*gd));
Albert ARIBAUDecc30662015-11-25 17:56:32 +0100112 /* set GD unless architecture did it already */
Simon Glassaf7a5552016-01-15 05:23:23 -0700113#if !defined(CONFIG_ARM)
Simon Glassaf6bbd42015-10-19 06:49:56 -0600114 arch_setup_gd(gd_ptr);
Albert ARIBAUDecc30662015-11-25 17:56:32 +0100115#endif
116 /* next alloc will be higher by one GD plus 16-byte alignment */
117 base += roundup(sizeof(struct global_data), 16);
118
119 /*
120 * record early malloc arena start.
121 * Use gd as it is now properly set for all architectures.
122 */
Simon Glassaf6bbd42015-10-19 06:49:56 -0600123
Fabio Estevam9ac4fc82015-11-12 12:30:19 -0200124#if defined(CONFIG_SYS_MALLOC_F)
Albert ARIBAUDecc30662015-11-25 17:56:32 +0100125 /* go down one 'early malloc arena' */
126 gd->malloc_base = base;
127 /* next alloc will be higher by one 'early malloc arena' size */
128 base += CONFIG_SYS_MALLOC_F_LEN;
Simon Glassaf6bbd42015-10-19 06:49:56 -0600129#endif
Simon Glassaf6bbd42015-10-19 06:49:56 -0600130}
Heiko Schocher496c5482016-06-07 08:31:20 +0200131
132/*
133 * Board-specific Platform code can reimplement show_boot_progress () if needed
134 */
135__weak void show_boot_progress(int val) {}