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 1412360982SSimon Glass /* 1512360982SSimon Glass * It isn't trivial to figure out whether memcpy() exists. The arch-specific 1612360982SSimon Glass * memcpy() is not normally available in SPL due to code size. 1712360982SSimon Glass */ 1812360982SSimon Glass #if !defined(CONFIG_SPL_BUILD) || \ 1912360982SSimon Glass (defined(CONFIG_SPL_LIBGENERIC_SUPPORT) && \ 2012360982SSimon Glass !defined(CONFIG_USE_ARCH_MEMSET)) 2112360982SSimon Glass #define _USE_MEMCPY 2212360982SSimon Glass #endif 2312360982SSimon Glass 24*adc421e4SAlbert ARIBAUD /* Unfortunately x86 or ARM can't compile this code as gd cannot be assigned */ 25*adc421e4SAlbert ARIBAUD #if !defined(CONFIG_X86) && !defined(CONFIG_ARM) 26af6bbd4dSSimon Glass __weak void arch_setup_gd(struct global_data *gd_ptr) 27af6bbd4dSSimon Glass { 28af6bbd4dSSimon Glass gd = gd_ptr; 29af6bbd4dSSimon Glass } 30*adc421e4SAlbert ARIBAUD #endif /* !CONFIG_X86 && !CONFIG_ARM */ 31af6bbd4dSSimon Glass 32ecc30663SAlbert ARIBAUD /* 33ecc30663SAlbert ARIBAUD * Allocate reserved space for use as 'globals' from 'top' address and 34ecc30663SAlbert ARIBAUD * return 'bottom' address of allocated space 35ecc30663SAlbert ARIBAUD * 36ecc30663SAlbert ARIBAUD * Notes: 37ecc30663SAlbert ARIBAUD * 38ecc30663SAlbert ARIBAUD * Actual reservation cannot be done from within this function as 39ecc30663SAlbert ARIBAUD * it requires altering the C stack pointer, so this will be done by 40ecc30663SAlbert ARIBAUD * the caller upon return from this function. 41ecc30663SAlbert ARIBAUD * 42ecc30663SAlbert ARIBAUD * IMPORTANT: 43ecc30663SAlbert ARIBAUD * 44ecc30663SAlbert ARIBAUD * Alignment constraints may differ for each 'chunk' allocated. For now: 45ecc30663SAlbert ARIBAUD * 46ecc30663SAlbert ARIBAUD * - GD is aligned down on a 16-byte boundary 47ecc30663SAlbert ARIBAUD * 48ecc30663SAlbert ARIBAUD * - the early malloc arena is not aligned, therefore it follows the stack 49ecc30663SAlbert ARIBAUD * alignment constraint of the architecture for which we are bulding. 50ecc30663SAlbert ARIBAUD * 51ecc30663SAlbert ARIBAUD * - GD is allocated last, so that the return value of this functions is 52ecc30663SAlbert ARIBAUD * both the bottom of the reserved area and the address of GD, should 53ecc30663SAlbert ARIBAUD * the calling context need it. 54ecc30663SAlbert ARIBAUD */ 55ecc30663SAlbert ARIBAUD 56ecc30663SAlbert ARIBAUD ulong board_init_f_alloc_reserve(ulong top) 57ecc30663SAlbert ARIBAUD { 58ecc30663SAlbert ARIBAUD /* Reserve early malloc arena */ 59ecc30663SAlbert ARIBAUD #if defined(CONFIG_SYS_MALLOC_F) 60ecc30663SAlbert ARIBAUD top -= CONFIG_SYS_MALLOC_F_LEN; 61ecc30663SAlbert ARIBAUD #endif 62ecc30663SAlbert ARIBAUD /* LAST : reserve GD (rounded up to a multiple of 16 bytes) */ 63ecc30663SAlbert ARIBAUD top = rounddown(top-sizeof(struct global_data), 16); 64ecc30663SAlbert ARIBAUD 65ecc30663SAlbert ARIBAUD return top; 66ecc30663SAlbert ARIBAUD } 67ecc30663SAlbert ARIBAUD 68ecc30663SAlbert ARIBAUD /* 69ecc30663SAlbert ARIBAUD * Initialize reserved space (which has been safely allocated on the C 70ecc30663SAlbert ARIBAUD * stack from the C runtime environment handling code). 71ecc30663SAlbert ARIBAUD * 72ecc30663SAlbert ARIBAUD * Notes: 73ecc30663SAlbert ARIBAUD * 74ecc30663SAlbert ARIBAUD * Actual reservation was done by the caller; the locations from base 75ecc30663SAlbert ARIBAUD * to base+size-1 (where 'size' is the value returned by the allocation 76ecc30663SAlbert ARIBAUD * function above) can be accessed freely without risk of corrupting the 77ecc30663SAlbert ARIBAUD * C runtime environment. 78ecc30663SAlbert ARIBAUD * 79ecc30663SAlbert ARIBAUD * IMPORTANT: 80ecc30663SAlbert ARIBAUD * 81ecc30663SAlbert ARIBAUD * Upon return from the allocation function above, on some architectures 82ecc30663SAlbert ARIBAUD * the caller will set gd to the lowest reserved location. Therefore, in 83ecc30663SAlbert ARIBAUD * this initialization function, the global data MUST be placed at base. 84ecc30663SAlbert ARIBAUD * 85ecc30663SAlbert ARIBAUD * ALSO IMPORTANT: 86ecc30663SAlbert ARIBAUD * 87ecc30663SAlbert ARIBAUD * On some architectures, gd will already be good when entering this 88ecc30663SAlbert ARIBAUD * function. On others, it will only be good once arch_setup_gd() returns. 89ecc30663SAlbert ARIBAUD * Therefore, global data accesses must be done: 90ecc30663SAlbert ARIBAUD * 91ecc30663SAlbert ARIBAUD * - through gd_ptr if before the call to arch_setup_gd(); 92ecc30663SAlbert ARIBAUD * 93ecc30663SAlbert ARIBAUD * - through gd once arch_setup_gd() has been called. 94ecc30663SAlbert ARIBAUD * 95ecc30663SAlbert ARIBAUD * Do not use 'gd->' until arch_setup_gd() has been called! 96ecc30663SAlbert ARIBAUD * 97ecc30663SAlbert ARIBAUD * IMPORTANT TOO: 98ecc30663SAlbert ARIBAUD * 99ecc30663SAlbert ARIBAUD * Initialization for each "chunk" (GD, early malloc arena...) ends with 100ecc30663SAlbert ARIBAUD * an incrementation line of the form 'base += <some size>'. The last of 101ecc30663SAlbert ARIBAUD * these incrementations seems useless, as base will not be used any 102ecc30663SAlbert ARIBAUD * more after this incrementation; but if/when a new "chunk" is appended, 103ecc30663SAlbert ARIBAUD * this increment will be essential as it will give base right value for 104ecc30663SAlbert ARIBAUD * this new chunk (which will have to end with its own incrementation 105ecc30663SAlbert ARIBAUD * statement). Besides, the compiler's optimizer will silently detect 106ecc30663SAlbert ARIBAUD * and remove the last base incrementation, therefore leaving that last 107ecc30663SAlbert ARIBAUD * (seemingly useless) incrementation causes no code increase. 108ecc30663SAlbert ARIBAUD */ 109ecc30663SAlbert ARIBAUD 110ecc30663SAlbert ARIBAUD void board_init_f_init_reserve(ulong base) 111af6bbd4dSSimon Glass { 112af6bbd4dSSimon Glass struct global_data *gd_ptr; 11312360982SSimon Glass #ifndef _USE_MEMCPY 11412360982SSimon Glass int *ptr; 11512360982SSimon Glass #endif 116af6bbd4dSSimon Glass 117ecc30663SAlbert ARIBAUD /* 118ecc30663SAlbert ARIBAUD * clear GD entirely and set it up. 119ecc30663SAlbert ARIBAUD * Use gd_ptr, as gd may not be properly set yet. 120ecc30663SAlbert ARIBAUD */ 121af6bbd4dSSimon Glass 122ecc30663SAlbert ARIBAUD gd_ptr = (struct global_data *)base; 123ecc30663SAlbert ARIBAUD /* zero the area */ 12412360982SSimon Glass #ifdef _USE_MEMCPY 125af6bbd4dSSimon Glass memset(gd_ptr, '\0', sizeof(*gd)); 12612360982SSimon Glass #else 12712360982SSimon Glass for (ptr = (int *)gd_ptr; ptr < (int *)(gd_ptr + 1); ) 12812360982SSimon Glass *ptr++ = 0; 12912360982SSimon Glass #endif 130ecc30663SAlbert ARIBAUD /* set GD unless architecture did it already */ 131*adc421e4SAlbert ARIBAUD #if !defined(CONFIG_X86) && !defined(CONFIG_ARM) 132af6bbd4dSSimon Glass arch_setup_gd(gd_ptr); 133ecc30663SAlbert ARIBAUD #endif 134ecc30663SAlbert ARIBAUD /* next alloc will be higher by one GD plus 16-byte alignment */ 135ecc30663SAlbert ARIBAUD base += roundup(sizeof(struct global_data), 16); 136ecc30663SAlbert ARIBAUD 137ecc30663SAlbert ARIBAUD /* 138ecc30663SAlbert ARIBAUD * record early malloc arena start. 139ecc30663SAlbert ARIBAUD * Use gd as it is now properly set for all architectures. 140ecc30663SAlbert ARIBAUD */ 141af6bbd4dSSimon Glass 1429ac4fc82SFabio Estevam #if defined(CONFIG_SYS_MALLOC_F) 143ecc30663SAlbert ARIBAUD /* go down one 'early malloc arena' */ 144ecc30663SAlbert ARIBAUD gd->malloc_base = base; 145ecc30663SAlbert ARIBAUD /* next alloc will be higher by one 'early malloc arena' size */ 146ecc30663SAlbert ARIBAUD base += CONFIG_SYS_MALLOC_F_LEN; 147af6bbd4dSSimon Glass #endif 148af6bbd4dSSimon Glass } 149