crt0_64.S 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * crt0 - C-runtime startup Code for AArch64 U-Boot
  4. *
  5. * (C) Copyright 2013
  6. * David Feng <fenghua@phytium.com.cn>
  7. *
  8. * (C) Copyright 2012
  9. * Albert ARIBAUD <albert.u.boot@aribaud.net>
  10. */
  11. #include <config.h>
  12. #include <asm-offsets.h>
  13. #include <asm/macro.h>
  14. #include <linux/linkage.h>
  15. /*
  16. * This file handles the target-independent stages of the U-Boot
  17. * start-up where a C runtime environment is needed. Its entry point
  18. * is _main and is branched into from the target's start.S file.
  19. *
  20. * _main execution sequence is:
  21. *
  22. * 1. Set up initial environment for calling board_init_f().
  23. * This environment only provides a stack and a place to store
  24. * the GD ('global data') structure, both located in some readily
  25. * available RAM (SRAM, locked cache...). In this context, VARIABLE
  26. * global data, initialized or not (BSS), are UNAVAILABLE; only
  27. * CONSTANT initialized data are available. GD should be zeroed
  28. * before board_init_f() is called.
  29. *
  30. * 2. Call board_init_f(). This function prepares the hardware for
  31. * execution from system RAM (DRAM, DDR...) As system RAM may not
  32. * be available yet, , board_init_f() must use the current GD to
  33. * store any data which must be passed on to later stages. These
  34. * data include the relocation destination, the future stack, and
  35. * the future GD location.
  36. *
  37. * 3. Set up intermediate environment where the stack and GD are the
  38. * ones allocated by board_init_f() in system RAM, but BSS and
  39. * initialized non-const data are still not available.
  40. *
  41. * 4a.For U-Boot proper (not SPL), call relocate_code(). This function
  42. * relocates U-Boot from its current location into the relocation
  43. * destination computed by board_init_f().
  44. *
  45. * 4b.For SPL, board_init_f() just returns (to crt0). There is no
  46. * code relocation in SPL.
  47. *
  48. * 5. Set up final environment for calling board_init_r(). This
  49. * environment has BSS (initialized to 0), initialized non-const
  50. * data (initialized to their intended value), and stack in system
  51. * RAM (for SPL moving the stack and GD into RAM is optional - see
  52. * CONFIG_SPL_STACK_R). GD has retained values set by board_init_f().
  53. *
  54. * TODO: For SPL, implement stack relocation on AArch64.
  55. *
  56. * 6. For U-Boot proper (not SPL), some CPUs have some work left to do
  57. * at this point regarding memory, so call c_runtime_cpu_setup.
  58. *
  59. * 7. Branch to board_init_r().
  60. *
  61. * For more information see 'Board Initialisation Flow in README.
  62. */
  63. ENTRY(_main)
  64. /*
  65. * Set up initial C runtime environment and call board_init_f(0).
  66. */
  67. #if defined(CONFIG_TPL_BUILD) && defined(CONFIG_TPL_NEEDS_SEPARATE_STACK)
  68. ldr x0, =(CONFIG_TPL_STACK)
  69. #elif defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK)
  70. ldr x0, =(CONFIG_SPL_STACK)
  71. #elif defined(CONFIG_INIT_SP_RELATIVE)
  72. #if CONFIG_POSITION_INDEPENDENT
  73. adrp x0, __bss_start /* x0 <- Runtime &__bss_start */
  74. add x0, x0, #:lo12:__bss_start
  75. #else
  76. adr x0, __bss_start
  77. #endif
  78. add x0, x0, #CONFIG_SYS_INIT_SP_BSS_OFFSET
  79. #else
  80. ldr x0, =(CONFIG_SYS_INIT_SP_ADDR)
  81. #endif
  82. bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */
  83. mov x0, sp
  84. bl board_init_f_alloc_reserve
  85. mov sp, x0
  86. /* set up gd here, outside any C code */
  87. mov x18, x0
  88. bl board_init_f_init_reserve
  89. mov x0, #0
  90. bl board_init_f
  91. #if !defined(CONFIG_SPL_BUILD)
  92. /*
  93. * Set up intermediate environment (new sp and gd) and call
  94. * relocate_code(addr_moni). Trick here is that we'll return
  95. * 'here' but relocated.
  96. */
  97. ldr x0, [x18, #GD_START_ADDR_SP] /* x0 <- gd->start_addr_sp */
  98. bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */
  99. ldr x18, [x18, #GD_NEW_GD] /* x18 <- gd->new_gd */
  100. adr lr, relocation_return
  101. #if CONFIG_POSITION_INDEPENDENT
  102. /* Add in link-vs-runtime offset */
  103. adrp x0, _start /* x0 <- Runtime value of _start */
  104. add x0, x0, #:lo12:_start
  105. ldr x9, _TEXT_BASE /* x9 <- Linked value of _start */
  106. sub x9, x9, x0 /* x9 <- Run-vs-link offset */
  107. add lr, lr, x9
  108. #endif
  109. /* Add in link-vs-relocation offset */
  110. ldr x9, [x18, #GD_RELOC_OFF] /* x9 <- gd->reloc_off */
  111. add lr, lr, x9 /* new return address after relocation */
  112. ldr x0, [x18, #GD_RELOCADDR] /* x0 <- gd->relocaddr */
  113. b relocate_code
  114. relocation_return:
  115. /*
  116. * Set up final (full) environment
  117. */
  118. bl c_runtime_cpu_setup /* still call old routine */
  119. #endif /* !CONFIG_SPL_BUILD */
  120. #if !defined(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(FRAMEWORK)
  121. #if defined(CONFIG_SPL_BUILD)
  122. bl spl_relocate_stack_gd /* may return NULL */
  123. /* set up gd here, outside any C code, if new stack is returned */
  124. cmp x0, #0
  125. csel x18, x0, x18, ne
  126. /*
  127. * Perform 'sp = (x0 != NULL) ? x0 : sp' while working
  128. * around the constraint that conditional moves can not
  129. * have 'sp' as an operand
  130. */
  131. mov x1, sp
  132. cmp x0, #0
  133. csel x0, x0, x1, ne
  134. mov sp, x0
  135. #endif
  136. /*
  137. * Clear BSS section
  138. */
  139. ldr x0, =__bss_start /* this is auto-relocated! */
  140. ldr x1, =__bss_end /* this is auto-relocated! */
  141. clear_loop:
  142. str xzr, [x0], #8
  143. cmp x0, x1
  144. b.lo clear_loop
  145. /* call board_init_r(gd_t *id, ulong dest_addr) */
  146. mov x0, x18 /* gd_t */
  147. ldr x1, [x18, #GD_RELOCADDR] /* dest_addr */
  148. b board_init_r /* PC relative jump */
  149. /* NOTREACHED - board_init_r() does not return */
  150. #endif
  151. ENDPROC(_main)