board_init.c 4.1 KB

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