Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Startup Code for RISC-V Core |
| 4 | * |
| 5 | * Copyright (c) 2017 Microsemi Corporation. |
| 6 | * Copyright (c) 2017 Padmarao Begari <Padmarao.Begari@microsemi.com> |
| 7 | * |
| 8 | * Copyright (C) 2017 Andes Technology Corporation |
| 9 | * Rick Chen, Andes Technology Corporation <rick@andestech.com> |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <asm-offsets.h> |
| 13 | #include <config.h> |
| 14 | #include <common.h> |
| 15 | #include <elf.h> |
Lukas Auer | a359665 | 2019-03-17 19:28:37 +0100 | [diff] [blame] | 16 | #include <asm/csr.h> |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 17 | #include <asm/encoding.h> |
Bin Meng | 89681a7 | 2018-12-12 06:12:45 -0800 | [diff] [blame] | 18 | #include <generated/asm-offsets.h> |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 19 | |
| 20 | #ifdef CONFIG_32BIT |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 21 | #define LREG lw |
| 22 | #define SREG sw |
| 23 | #define REGBYTES 4 |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 24 | #define RELOC_TYPE R_RISCV_32 |
| 25 | #define SYM_INDEX 0x8 |
| 26 | #define SYM_SIZE 0x10 |
| 27 | #else |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 28 | #define LREG ld |
| 29 | #define SREG sd |
| 30 | #define REGBYTES 8 |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 31 | #define RELOC_TYPE R_RISCV_64 |
| 32 | #define SYM_INDEX 0x20 |
| 33 | #define SYM_SIZE 0x18 |
| 34 | #endif |
| 35 | |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 36 | .section .text |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 37 | .globl _start |
| 38 | _start: |
Lukas Auer | 9ebf294 | 2019-03-17 19:28:39 +0100 | [diff] [blame^] | 39 | #ifdef CONFIG_RISCV_MMODE |
| 40 | csrr a0, mhartid |
| 41 | #endif |
| 42 | |
Lukas Auer | 39a652b | 2018-11-22 11:26:29 +0100 | [diff] [blame] | 43 | /* save hart id and dtb pointer */ |
Lukas Auer | 8de4b3e | 2019-03-17 19:28:36 +0100 | [diff] [blame] | 44 | mv tp, a0 |
Lukas Auer | 39a652b | 2018-11-22 11:26:29 +0100 | [diff] [blame] | 45 | mv s1, a1 |
| 46 | |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 47 | la t0, trap_entry |
Anup Patel | 89b3934 | 2018-12-03 10:57:40 +0530 | [diff] [blame] | 48 | csrw MODE_PREFIX(tvec), t0 |
Lukas Auer | 8598e6b | 2018-11-22 11:26:28 +0100 | [diff] [blame] | 49 | |
| 50 | /* mask all interrupts */ |
Anup Patel | 89b3934 | 2018-12-03 10:57:40 +0530 | [diff] [blame] | 51 | csrw MODE_PREFIX(ie), zero |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 52 | |
Lukas Auer | a359665 | 2019-03-17 19:28:37 +0100 | [diff] [blame] | 53 | #ifdef CONFIG_SMP |
| 54 | /* check if hart is within range */ |
| 55 | /* tp: hart id */ |
| 56 | li t0, CONFIG_NR_CPUS |
| 57 | bge tp, t0, hart_out_of_bounds_loop |
| 58 | #endif |
| 59 | |
| 60 | #ifdef CONFIG_SMP |
| 61 | /* set xSIE bit to receive IPIs */ |
| 62 | #ifdef CONFIG_RISCV_MMODE |
| 63 | li t0, MIE_MSIE |
| 64 | #else |
| 65 | li t0, SIE_SSIE |
| 66 | #endif |
| 67 | csrs MODE_PREFIX(ie), t0 |
| 68 | #endif |
| 69 | |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 70 | /* |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 71 | * Set stackpointer in internal/ex RAM to call board_init_f |
| 72 | */ |
| 73 | call_board_init_f: |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 74 | li t0, -16 |
| 75 | li t1, CONFIG_SYS_INIT_SP_ADDR |
| 76 | and sp, t1, t0 /* force 16 byte alignment */ |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 77 | |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 78 | call_board_init_f_0: |
| 79 | mv a0, sp |
| 80 | jal board_init_f_alloc_reserve |
Lukas Auer | a359665 | 2019-03-17 19:28:37 +0100 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * Set global data pointer here for all harts, uninitialized at this |
| 84 | * point. |
| 85 | */ |
| 86 | mv gp, a0 |
| 87 | |
| 88 | /* setup stack */ |
| 89 | #ifdef CONFIG_SMP |
| 90 | /* tp: hart id */ |
| 91 | slli t0, tp, CONFIG_STACK_SIZE_SHIFT |
| 92 | sub sp, a0, t0 |
| 93 | #else |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 94 | mv sp, a0 |
Lukas Auer | a359665 | 2019-03-17 19:28:37 +0100 | [diff] [blame] | 95 | #endif |
| 96 | |
| 97 | /* |
| 98 | * Pick hart to initialize global data and run U-Boot. The other harts |
| 99 | * wait for initialization to complete. |
| 100 | */ |
| 101 | la t0, hart_lottery |
| 102 | li s2, 1 |
| 103 | amoswap.w s2, t1, 0(t0) |
| 104 | bnez s2, wait_for_gd_init |
Lukas Auer | 39a652b | 2018-11-22 11:26:29 +0100 | [diff] [blame] | 105 | |
| 106 | la t0, prior_stage_fdt_address |
| 107 | SREG s1, 0(t0) |
| 108 | |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 109 | jal board_init_f_init_reserve |
| 110 | |
Bin Meng | 89681a7 | 2018-12-12 06:12:45 -0800 | [diff] [blame] | 111 | /* save the boot hart id to global_data */ |
Lukas Auer | 8de4b3e | 2019-03-17 19:28:36 +0100 | [diff] [blame] | 112 | SREG tp, GD_BOOT_HART(gp) |
Bin Meng | 89681a7 | 2018-12-12 06:12:45 -0800 | [diff] [blame] | 113 | |
Lukas Auer | a359665 | 2019-03-17 19:28:37 +0100 | [diff] [blame] | 114 | la t0, available_harts_lock |
| 115 | fence rw, w |
| 116 | amoswap.w zero, zero, 0(t0) |
| 117 | |
| 118 | wait_for_gd_init: |
| 119 | la t0, available_harts_lock |
| 120 | li t1, 1 |
| 121 | 1: amoswap.w t1, t1, 0(t0) |
| 122 | fence r, rw |
| 123 | bnez t1, 1b |
| 124 | |
| 125 | /* register available harts in the available_harts mask */ |
| 126 | li t1, 1 |
| 127 | sll t1, t1, tp |
| 128 | LREG t2, GD_AVAILABLE_HARTS(gp) |
| 129 | or t2, t2, t1 |
| 130 | SREG t2, GD_AVAILABLE_HARTS(gp) |
| 131 | |
| 132 | fence rw, w |
| 133 | amoswap.w zero, zero, 0(t0) |
| 134 | |
| 135 | /* |
| 136 | * Continue on hart lottery winner, others branch to |
| 137 | * secondary_hart_loop. |
| 138 | */ |
| 139 | bnez s2, secondary_hart_loop |
| 140 | |
Lukas Auer | 01558e2 | 2019-03-17 19:28:35 +0100 | [diff] [blame] | 141 | /* Enable cache */ |
| 142 | jal icache_enable |
| 143 | jal dcache_enable |
| 144 | |
| 145 | #ifdef CONFIG_DEBUG_UART |
| 146 | jal debug_uart_init |
| 147 | #endif |
| 148 | |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 149 | mv a0, zero /* a0 <-- boot_flags = 0 */ |
| 150 | la t5, board_init_f |
| 151 | jr t5 /* jump to board_init_f() */ |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * void relocate_code (addr_sp, gd, addr_moni) |
| 155 | * |
| 156 | * This "function" does not return, instead it continues in RAM |
| 157 | * after relocating the monitor code. |
| 158 | * |
| 159 | */ |
| 160 | .globl relocate_code |
| 161 | relocate_code: |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 162 | mv s2, a0 /* save addr_sp */ |
| 163 | mv s3, a1 /* save addr of gd */ |
| 164 | mv s4, a2 /* save addr of destination */ |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | *Set up the stack |
| 168 | */ |
| 169 | stack_setup: |
Lukas Auer | a359665 | 2019-03-17 19:28:37 +0100 | [diff] [blame] | 170 | #ifdef CONFIG_SMP |
| 171 | /* tp: hart id */ |
| 172 | slli t0, tp, CONFIG_STACK_SIZE_SHIFT |
| 173 | sub sp, s2, t0 |
| 174 | #else |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 175 | mv sp, s2 |
Lukas Auer | a359665 | 2019-03-17 19:28:37 +0100 | [diff] [blame] | 176 | #endif |
| 177 | |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 178 | la t0, _start |
| 179 | sub t6, s4, t0 /* t6 <- relocation offset */ |
| 180 | beq t0, s4, clear_bss /* skip relocation */ |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 181 | |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 182 | mv t1, s4 /* t1 <- scratch for copy_loop */ |
| 183 | la t3, __bss_start |
| 184 | sub t3, t3, t0 /* t3 <- __bss_start_ofs */ |
| 185 | add t2, t0, t3 /* t2 <- source end address */ |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 186 | |
| 187 | copy_loop: |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 188 | LREG t5, 0(t0) |
| 189 | addi t0, t0, REGBYTES |
| 190 | SREG t5, 0(t1) |
| 191 | addi t1, t1, REGBYTES |
| 192 | blt t0, t2, copy_loop |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 193 | |
| 194 | /* |
| 195 | * Update dynamic relocations after board_init_f |
| 196 | */ |
| 197 | fix_rela_dyn: |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 198 | la t1, __rel_dyn_start |
| 199 | la t2, __rel_dyn_end |
| 200 | beq t1, t2, clear_bss |
| 201 | add t1, t1, t6 /* t1 <- rela_dyn_start in RAM */ |
| 202 | add t2, t2, t6 /* t2 <- rela_dyn_end in RAM */ |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * skip first reserved entry: address, type, addend |
| 206 | */ |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 207 | bne t1, t2, 7f |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 208 | |
| 209 | 6: |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 210 | LREG t5, -(REGBYTES*2)(t1) /* t5 <-- relocation info:type */ |
| 211 | li t3, R_RISCV_RELATIVE /* reloc type R_RISCV_RELATIVE */ |
| 212 | bne t5, t3, 8f /* skip non-RISCV_RELOC entries */ |
| 213 | LREG t3, -(REGBYTES*3)(t1) |
| 214 | LREG t5, -(REGBYTES)(t1) /* t5 <-- addend */ |
| 215 | add t5, t5, t6 /* t5 <-- location to fix up in RAM */ |
| 216 | add t3, t3, t6 /* t3 <-- location to fix up in RAM */ |
| 217 | SREG t5, 0(t3) |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 218 | 7: |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 219 | addi t1, t1, (REGBYTES*3) |
| 220 | ble t1, t2, 6b |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 221 | |
| 222 | 8: |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 223 | la t4, __dyn_sym_start |
| 224 | add t4, t4, t6 |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 225 | |
| 226 | 9: |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 227 | LREG t5, -(REGBYTES*2)(t1) /* t5 <-- relocation info:type */ |
| 228 | srli t0, t5, SYM_INDEX /* t0 <--- sym table index */ |
| 229 | andi t5, t5, 0xFF /* t5 <--- relocation type */ |
| 230 | li t3, RELOC_TYPE |
| 231 | bne t5, t3, 10f /* skip non-addned entries */ |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 232 | |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 233 | LREG t3, -(REGBYTES*3)(t1) |
| 234 | li t5, SYM_SIZE |
| 235 | mul t0, t0, t5 |
Lukas Auer | 39a652b | 2018-11-22 11:26:29 +0100 | [diff] [blame] | 236 | add s5, t4, t0 |
| 237 | LREG t5, REGBYTES(s5) |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 238 | add t5, t5, t6 /* t5 <-- location to fix up in RAM */ |
| 239 | add t3, t3, t6 /* t3 <-- location to fix up in RAM */ |
| 240 | SREG t5, 0(t3) |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 241 | 10: |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 242 | addi t1, t1, (REGBYTES*3) |
| 243 | ble t1, t2, 9b |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 244 | |
| 245 | /* |
| 246 | * trap update |
| 247 | */ |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 248 | la t0, trap_entry |
| 249 | add t0, t0, t6 |
Anup Patel | 89b3934 | 2018-12-03 10:57:40 +0530 | [diff] [blame] | 250 | csrw MODE_PREFIX(tvec), t0 |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 251 | |
| 252 | clear_bss: |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 253 | la t0, __bss_start /* t0 <- rel __bss_start in FLASH */ |
| 254 | add t0, t0, t6 /* t0 <- rel __bss_start in RAM */ |
| 255 | la t1, __bss_end /* t1 <- rel __bss_end in FLASH */ |
| 256 | add t1, t1, t6 /* t1 <- rel __bss_end in RAM */ |
Lukas Auer | a359665 | 2019-03-17 19:28:37 +0100 | [diff] [blame] | 257 | beq t0, t1, relocate_secondary_harts |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 258 | |
| 259 | clbss_l: |
Lukas Auer | 8598e6b | 2018-11-22 11:26:28 +0100 | [diff] [blame] | 260 | SREG zero, 0(t0) /* clear loop... */ |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 261 | addi t0, t0, REGBYTES |
| 262 | bne t0, t1, clbss_l |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 263 | |
Lukas Auer | a359665 | 2019-03-17 19:28:37 +0100 | [diff] [blame] | 264 | relocate_secondary_harts: |
| 265 | #ifdef CONFIG_SMP |
| 266 | /* send relocation IPI */ |
| 267 | la t0, secondary_hart_relocate |
| 268 | add a0, t0, t6 |
| 269 | |
| 270 | /* store relocation offset */ |
| 271 | mv s5, t6 |
| 272 | |
| 273 | mv a1, s2 |
| 274 | mv a2, s3 |
| 275 | jal smp_call_function |
| 276 | |
| 277 | /* restore relocation offset */ |
| 278 | mv t6, s5 |
| 279 | #endif |
| 280 | |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 281 | /* |
| 282 | * We are done. Do not return, instead branch to second part of board |
| 283 | * initialization, now running from RAM. |
| 284 | */ |
| 285 | call_board_init_r: |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 286 | jal invalidate_icache_all |
| 287 | jal flush_dcache_all |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 288 | la t0, board_init_r |
| 289 | mv t4, t0 /* offset of board_init_r() */ |
| 290 | add t4, t4, t6 /* real address of board_init_r() */ |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 291 | /* |
| 292 | * setup parameters for board_init_r |
| 293 | */ |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 294 | mv a0, s3 /* gd_t */ |
| 295 | mv a1, s4 /* dest_addr */ |
Rick Chen | e76b804 | 2017-12-26 13:55:48 +0800 | [diff] [blame] | 296 | |
| 297 | /* |
| 298 | * jump to it ... |
| 299 | */ |
Lukas Auer | 7cf4368 | 2018-11-22 11:26:24 +0100 | [diff] [blame] | 300 | jr t4 /* jump to board_init_r() */ |
Lukas Auer | a359665 | 2019-03-17 19:28:37 +0100 | [diff] [blame] | 301 | |
| 302 | #ifdef CONFIG_SMP |
| 303 | hart_out_of_bounds_loop: |
| 304 | /* Harts in this loop are out of bounds, increase CONFIG_NR_CPUS. */ |
| 305 | wfi |
| 306 | j hart_out_of_bounds_loop |
| 307 | #endif |
| 308 | |
| 309 | #ifdef CONFIG_SMP |
| 310 | /* SMP relocation entry */ |
| 311 | secondary_hart_relocate: |
| 312 | /* a1: new sp */ |
| 313 | /* a2: new gd */ |
| 314 | /* tp: hart id */ |
| 315 | |
| 316 | /* setup stack */ |
| 317 | slli t0, tp, CONFIG_STACK_SIZE_SHIFT |
| 318 | sub sp, a1, t0 |
| 319 | |
| 320 | /* update global data pointer */ |
| 321 | mv gp, a2 |
| 322 | #endif |
| 323 | |
| 324 | secondary_hart_loop: |
| 325 | wfi |
| 326 | |
| 327 | #ifdef CONFIG_SMP |
| 328 | csrr t0, MODE_PREFIX(ip) |
| 329 | #ifdef CONFIG_RISCV_MMODE |
| 330 | andi t0, t0, MIE_MSIE |
| 331 | #else |
| 332 | andi t0, t0, SIE_SSIE |
| 333 | #endif |
| 334 | beqz t0, secondary_hart_loop |
| 335 | |
| 336 | mv a0, tp |
| 337 | jal handle_ipi |
| 338 | #endif |
| 339 | |
| 340 | j secondary_hart_loop |