crt0_64.S 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. adr x0, __bss_start
  73. add x0, x0, #CONFIG_SYS_INIT_SP_BSS_OFFSET
  74. #else
  75. ldr x0, =(CONFIG_SYS_INIT_SP_ADDR)
  76. #endif
  77. bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */
  78. mov x0, sp
  79. bl board_init_f_alloc_reserve
  80. mov sp, x0
  81. /* set up gd here, outside any C code */
  82. mov x18, x0
  83. bl board_init_f_init_reserve
  84. mov x0, #0
  85. bl board_init_f
  86. #if !defined(CONFIG_SPL_BUILD)
  87. /*
  88. * Set up intermediate environment (new sp and gd) and call
  89. * relocate_code(addr_moni). Trick here is that we'll return
  90. * 'here' but relocated.
  91. */
  92. ldr x0, [x18, #GD_START_ADDR_SP] /* x0 <- gd->start_addr_sp */
  93. bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */
  94. ldr x18, [x18, #GD_NEW_GD] /* x18 <- gd->new_gd */
  95. adr lr, relocation_return
  96. #if CONFIG_POSITION_INDEPENDENT
  97. /* Add in link-vs-runtime offset */
  98. adr x0, _start /* x0 <- Runtime value of _start */
  99. ldr x9, _TEXT_BASE /* x9 <- Linked value of _start */
  100. sub x9, x9, x0 /* x9 <- Run-vs-link offset */
  101. add lr, lr, x9
  102. #endif
  103. /* Add in link-vs-relocation offset */
  104. ldr x9, [x18, #GD_RELOC_OFF] /* x9 <- gd->reloc_off */
  105. add lr, lr, x9 /* new return address after relocation */
  106. ldr x0, [x18, #GD_RELOCADDR] /* x0 <- gd->relocaddr */
  107. b relocate_code
  108. relocation_return:
  109. /*
  110. * Set up final (full) environment
  111. */
  112. bl c_runtime_cpu_setup /* still call old routine */
  113. #endif /* !CONFIG_SPL_BUILD */
  114. #if !defined(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(FRAMEWORK)
  115. #if defined(CONFIG_SPL_BUILD)
  116. bl spl_relocate_stack_gd /* may return NULL */
  117. /* set up gd here, outside any C code, if new stack is returned */
  118. cmp x0, #0
  119. csel x18, x0, x18, ne
  120. /*
  121. * Perform 'sp = (x0 != NULL) ? x0 : sp' while working
  122. * around the constraint that conditional moves can not
  123. * have 'sp' as an operand
  124. */
  125. mov x1, sp
  126. cmp x0, #0
  127. csel x0, x0, x1, ne
  128. mov sp, x0
  129. #endif
  130. /*
  131. * Clear BSS section
  132. */
  133. ldr x0, =__bss_start /* this is auto-relocated! */
  134. ldr x1, =__bss_end /* this is auto-relocated! */
  135. clear_loop:
  136. str xzr, [x0], #8
  137. cmp x0, x1
  138. b.lo clear_loop
  139. /* call board_init_r(gd_t *id, ulong dest_addr) */
  140. mov x0, x18 /* gd_t */
  141. ldr x1, [x18, #GD_RELOCADDR] /* dest_addr */
  142. b board_init_r /* PC relative jump */
  143. /* NOTREACHED - board_init_r() does not return */
  144. #endif
  145. ENDPROC(_main)