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