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