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