board_init.c 4.1 KB

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