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