blob: d17bb298d72edc13cec4fd40083fd66f89fe943a [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
Simon Glass12360982015-10-19 06:49:57 -060014/*
15 * It isn't trivial to figure out whether memcpy() exists. The arch-specific
16 * memcpy() is not normally available in SPL due to code size.
17 */
18#if !defined(CONFIG_SPL_BUILD) || \
19 (defined(CONFIG_SPL_LIBGENERIC_SUPPORT) && \
20 !defined(CONFIG_USE_ARCH_MEMSET))
21#define _USE_MEMCPY
22#endif
23
Albert ARIBAUDadc421e2015-11-25 17:56:33 +010024/* Unfortunately x86 or ARM can't compile this code as gd cannot be assigned */
25#if !defined(CONFIG_X86) && !defined(CONFIG_ARM)
Simon Glassaf6bbd42015-10-19 06:49:56 -060026__weak void arch_setup_gd(struct global_data *gd_ptr)
27{
28 gd = gd_ptr;
29}
Albert ARIBAUDadc421e2015-11-25 17:56:33 +010030#endif /* !CONFIG_X86 && !CONFIG_ARM */
Simon Glassaf6bbd42015-10-19 06:49:56 -060031
Albert ARIBAUDecc30662015-11-25 17:56:32 +010032/*
33 * Allocate reserved space for use as 'globals' from 'top' address and
34 * return 'bottom' address of allocated space
35 *
36 * Notes:
37 *
38 * Actual reservation cannot be done from within this function as
39 * it requires altering the C stack pointer, so this will be done by
40 * the caller upon return from this function.
41 *
42 * IMPORTANT:
43 *
44 * Alignment constraints may differ for each 'chunk' allocated. For now:
45 *
46 * - GD is aligned down on a 16-byte boundary
47 *
48 * - the early malloc arena is not aligned, therefore it follows the stack
49 * alignment constraint of the architecture for which we are bulding.
50 *
51 * - GD is allocated last, so that the return value of this functions is
52 * both the bottom of the reserved area and the address of GD, should
53 * the calling context need it.
54 */
55
56ulong board_init_f_alloc_reserve(ulong top)
57{
58 /* Reserve early malloc arena */
59#if defined(CONFIG_SYS_MALLOC_F)
60 top -= CONFIG_SYS_MALLOC_F_LEN;
61#endif
62 /* LAST : reserve GD (rounded up to a multiple of 16 bytes) */
63 top = rounddown(top-sizeof(struct global_data), 16);
64
65 return top;
66}
67
68/*
69 * Initialize reserved space (which has been safely allocated on the C
70 * stack from the C runtime environment handling code).
71 *
72 * Notes:
73 *
74 * Actual reservation was done by the caller; the locations from base
75 * to base+size-1 (where 'size' is the value returned by the allocation
76 * function above) can be accessed freely without risk of corrupting the
77 * C runtime environment.
78 *
79 * IMPORTANT:
80 *
81 * Upon return from the allocation function above, on some architectures
82 * the caller will set gd to the lowest reserved location. Therefore, in
83 * this initialization function, the global data MUST be placed at base.
84 *
85 * ALSO IMPORTANT:
86 *
87 * On some architectures, gd will already be good when entering this
88 * function. On others, it will only be good once arch_setup_gd() returns.
89 * Therefore, global data accesses must be done:
90 *
91 * - through gd_ptr if before the call to arch_setup_gd();
92 *
93 * - through gd once arch_setup_gd() has been called.
94 *
95 * Do not use 'gd->' until arch_setup_gd() has been called!
96 *
97 * IMPORTANT TOO:
98 *
99 * Initialization for each "chunk" (GD, early malloc arena...) ends with
100 * an incrementation line of the form 'base += <some size>'. The last of
101 * these incrementations seems useless, as base will not be used any
102 * more after this incrementation; but if/when a new "chunk" is appended,
103 * this increment will be essential as it will give base right value for
104 * this new chunk (which will have to end with its own incrementation
105 * statement). Besides, the compiler's optimizer will silently detect
106 * and remove the last base incrementation, therefore leaving that last
107 * (seemingly useless) incrementation causes no code increase.
108 */
109
110void board_init_f_init_reserve(ulong base)
Simon Glassaf6bbd42015-10-19 06:49:56 -0600111{
112 struct global_data *gd_ptr;
Simon Glass12360982015-10-19 06:49:57 -0600113#ifndef _USE_MEMCPY
114 int *ptr;
115#endif
Simon Glassaf6bbd42015-10-19 06:49:56 -0600116
Albert ARIBAUDecc30662015-11-25 17:56:32 +0100117 /*
118 * clear GD entirely and set it up.
119 * Use gd_ptr, as gd may not be properly set yet.
120 */
Simon Glassaf6bbd42015-10-19 06:49:56 -0600121
Albert ARIBAUDecc30662015-11-25 17:56:32 +0100122 gd_ptr = (struct global_data *)base;
123 /* zero the area */
Simon Glass12360982015-10-19 06:49:57 -0600124#ifdef _USE_MEMCPY
Simon Glassaf6bbd42015-10-19 06:49:56 -0600125 memset(gd_ptr, '\0', sizeof(*gd));
Simon Glass12360982015-10-19 06:49:57 -0600126#else
127 for (ptr = (int *)gd_ptr; ptr < (int *)(gd_ptr + 1); )
128 *ptr++ = 0;
129#endif
Albert ARIBAUDecc30662015-11-25 17:56:32 +0100130 /* set GD unless architecture did it already */
Simon Glassaf7a5552016-01-15 05:23:23 -0700131#if !defined(CONFIG_ARM)
Simon Glassaf6bbd42015-10-19 06:49:56 -0600132 arch_setup_gd(gd_ptr);
Albert ARIBAUDecc30662015-11-25 17:56:32 +0100133#endif
134 /* next alloc will be higher by one GD plus 16-byte alignment */
135 base += roundup(sizeof(struct global_data), 16);
136
137 /*
138 * record early malloc arena start.
139 * Use gd as it is now properly set for all architectures.
140 */
Simon Glassaf6bbd42015-10-19 06:49:56 -0600141
Fabio Estevam9ac4fc82015-11-12 12:30:19 -0200142#if defined(CONFIG_SYS_MALLOC_F)
Albert ARIBAUDecc30662015-11-25 17:56:32 +0100143 /* go down one 'early malloc arena' */
144 gd->malloc_base = base;
145 /* next alloc will be higher by one 'early malloc arena' size */
146 base += CONFIG_SYS_MALLOC_F_LEN;
Simon Glassaf6bbd42015-10-19 06:49:56 -0600147#endif
Simon Glassaf6bbd42015-10-19 06:49:56 -0600148}