board_init.c 5.1 KB

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