board_init.c 4.5 KB

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