1/* 2 * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 7 * Redistributions of source code must retain the above copyright notice, this 8 * list of conditions and the following disclaimer. 9 * 10 * Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 14 * Neither the name of ARM nor the names of its contributors may be used 15 * to endorse or promote products derived from this software without specific 16 * prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31#include <arch.h> 32#include <asm_macros.S> 33#include <tsp.h> 34#include <xlat_tables.h> 35#include "../tsp_private.h" 36 37 38 .globl tsp_entrypoint 39 .globl tsp_vector_table 40 41 42 43 /* --------------------------------------------- 44 * Populate the params in x0-x7 from the pointer 45 * to the smc args structure in x0. 46 * --------------------------------------------- 47 */ 48 .macro restore_args_call_smc 49 ldp x6, x7, [x0, #TSP_ARG6] 50 ldp x4, x5, [x0, #TSP_ARG4] 51 ldp x2, x3, [x0, #TSP_ARG2] 52 ldp x0, x1, [x0, #TSP_ARG0] 53 smc #0 54 .endm 55 56 .macro save_eret_context reg1 reg2 57 mrs \reg1, elr_el1 58 mrs \reg2, spsr_el1 59 stp \reg1, \reg2, [sp, #-0x10]! 60 stp x30, x18, [sp, #-0x10]! 61 .endm 62 63 .macro restore_eret_context reg1 reg2 64 ldp x30, x18, [sp], #0x10 65 ldp \reg1, \reg2, [sp], #0x10 66 msr elr_el1, \reg1 67 msr spsr_el1, \reg2 68 .endm 69 70 .section .text, "ax" 71 .align 3 72 73func tsp_entrypoint 74 75 /* --------------------------------------------- 76 * Set the exception vector to something sane. 77 * --------------------------------------------- 78 */ 79 adr x0, tsp_exceptions 80 msr vbar_el1, x0 81 isb 82 83 /* --------------------------------------------- 84 * Enable the SError interrupt now that the 85 * exception vectors have been setup. 86 * --------------------------------------------- 87 */ 88 msr daifclr, #DAIF_ABT_BIT 89 90 /* --------------------------------------------- 91 * Enable the instruction cache, stack pointer 92 * and data access alignment checks 93 * --------------------------------------------- 94 */ 95 mov x1, #(SCTLR_I_BIT | SCTLR_A_BIT | SCTLR_SA_BIT) 96 mrs x0, sctlr_el1 97 orr x0, x0, x1 98 msr sctlr_el1, x0 99 isb 100 101 /* --------------------------------------------- 102 * Zero out NOBITS sections. There are 2 of them: 103 * - the .bss section; 104 * - the coherent memory section. 105 * --------------------------------------------- 106 */ 107 ldr x0, =__BSS_START__ 108 ldr x1, =__BSS_SIZE__ 109 bl zeromem16 110 111 ldr x0, =__COHERENT_RAM_START__ 112 ldr x1, =__COHERENT_RAM_UNALIGNED_SIZE__ 113 bl zeromem16 114 115 /* -------------------------------------------- 116 * Allocate a stack whose memory will be marked 117 * as Normal-IS-WBWA when the MMU is enabled. 118 * There is no risk of reading stale stack 119 * memory after enabling the MMU as only the 120 * primary cpu is running at the moment. 121 * -------------------------------------------- 122 */ 123 mrs x0, mpidr_el1 124 bl platform_set_stack 125 126 /* --------------------------------------------- 127 * Perform early platform setup & platform 128 * specific early arch. setup e.g. mmu setup 129 * --------------------------------------------- 130 */ 131 bl tsp_early_platform_setup 132 bl tsp_plat_arch_setup 133 134 /* --------------------------------------------- 135 * Jump to main function. 136 * --------------------------------------------- 137 */ 138 bl tsp_main 139 140 /* --------------------------------------------- 141 * Tell TSPD that we are done initialising 142 * --------------------------------------------- 143 */ 144 mov x1, x0 145 mov x0, #TSP_ENTRY_DONE 146 smc #0 147 148tsp_entrypoint_panic: 149 b tsp_entrypoint_panic 150 151 152 /* ------------------------------------------- 153 * Table of entrypoint vectors provided to the 154 * TSPD for the various entrypoints 155 * ------------------------------------------- 156 */ 157func tsp_vector_table 158 b tsp_std_smc_entry 159 b tsp_fast_smc_entry 160 b tsp_cpu_on_entry 161 b tsp_cpu_off_entry 162 b tsp_cpu_resume_entry 163 b tsp_cpu_suspend_entry 164 b tsp_fiq_entry 165 b tsp_system_off_entry 166 b tsp_system_reset_entry 167 168 /*--------------------------------------------- 169 * This entrypoint is used by the TSPD when this 170 * cpu is to be turned off through a CPU_OFF 171 * psci call to ask the TSP to perform any 172 * bookeeping necessary. In the current 173 * implementation, the TSPD expects the TSP to 174 * re-initialise its state so nothing is done 175 * here except for acknowledging the request. 176 * --------------------------------------------- 177 */ 178func tsp_cpu_off_entry 179 bl tsp_cpu_off_main 180 restore_args_call_smc 181 182 /*--------------------------------------------- 183 * This entrypoint is used by the TSPD when the 184 * system is about to be switched off (through 185 * a SYSTEM_OFF psci call) to ask the TSP to 186 * perform any necessary bookkeeping. 187 * --------------------------------------------- 188 */ 189func tsp_system_off_entry 190 bl tsp_system_off_main 191 restore_args_call_smc 192 193 /*--------------------------------------------- 194 * This entrypoint is used by the TSPD when the 195 * system is about to be reset (through a 196 * SYSTEM_RESET psci call) to ask the TSP to 197 * perform any necessary bookkeeping. 198 * --------------------------------------------- 199 */ 200func tsp_system_reset_entry 201 bl tsp_system_reset_main 202 restore_args_call_smc 203 204 /*--------------------------------------------- 205 * This entrypoint is used by the TSPD when this 206 * cpu is turned on using a CPU_ON psci call to 207 * ask the TSP to initialise itself i.e. setup 208 * the mmu, stacks etc. Minimal architectural 209 * state will be initialised by the TSPD when 210 * this function is entered i.e. Caches and MMU 211 * will be turned off, the execution state 212 * will be aarch64 and exceptions masked. 213 * --------------------------------------------- 214 */ 215func tsp_cpu_on_entry 216 /* --------------------------------------------- 217 * Set the exception vector to something sane. 218 * --------------------------------------------- 219 */ 220 adr x0, tsp_exceptions 221 msr vbar_el1, x0 222 isb 223 224 /* Enable the SError interrupt */ 225 msr daifclr, #DAIF_ABT_BIT 226 227 /* --------------------------------------------- 228 * Enable the instruction cache, stack pointer 229 * and data access alignment checks 230 * --------------------------------------------- 231 */ 232 mov x1, #(SCTLR_I_BIT | SCTLR_A_BIT | SCTLR_SA_BIT) 233 mrs x0, sctlr_el1 234 orr x0, x0, x1 235 msr sctlr_el1, x0 236 isb 237 238 /* -------------------------------------------- 239 * Give ourselves a stack whose memory will be 240 * marked as Normal-IS-WBWA when the MMU is 241 * enabled. 242 * -------------------------------------------- 243 */ 244 mrs x0, mpidr_el1 245 bl platform_set_stack 246 247 /* -------------------------------------------- 248 * Enable the MMU with the DCache disabled. It 249 * is safe to use stacks allocated in normal 250 * memory as a result. All memory accesses are 251 * marked nGnRnE when the MMU is disabled. So 252 * all the stack writes will make it to memory. 253 * All memory accesses are marked Non-cacheable 254 * when the MMU is enabled but D$ is disabled. 255 * So used stack memory is guaranteed to be 256 * visible immediately after the MMU is enabled 257 * Enabling the DCache at the same time as the 258 * MMU can lead to speculatively fetched and 259 * possibly stale stack memory being read from 260 * other caches. This can lead to coherency 261 * issues. 262 * -------------------------------------------- 263 */ 264 mov x0, #DISABLE_DCACHE 265 bl bl32_plat_enable_mmu 266 267 /* --------------------------------------------- 268 * Enable the Data cache now that the MMU has 269 * been enabled. The stack has been unwound. It 270 * will be written first before being read. This 271 * will invalidate any stale cache lines resi- 272 * -dent in other caches. We assume that 273 * interconnect coherency has been enabled for 274 * this cluster by EL3 firmware. 275 * --------------------------------------------- 276 */ 277 mrs x0, sctlr_el1 278 orr x0, x0, #SCTLR_C_BIT 279 msr sctlr_el1, x0 280 isb 281 282 /* --------------------------------------------- 283 * Enter C runtime to perform any remaining 284 * book keeping 285 * --------------------------------------------- 286 */ 287 bl tsp_cpu_on_main 288 restore_args_call_smc 289 290 /* Should never reach here */ 291tsp_cpu_on_entry_panic: 292 b tsp_cpu_on_entry_panic 293 294 /*--------------------------------------------- 295 * This entrypoint is used by the TSPD when this 296 * cpu is to be suspended through a CPU_SUSPEND 297 * psci call to ask the TSP to perform any 298 * bookeeping necessary. In the current 299 * implementation, the TSPD saves and restores 300 * the EL1 state. 301 * --------------------------------------------- 302 */ 303func tsp_cpu_suspend_entry 304 bl tsp_cpu_suspend_main 305 restore_args_call_smc 306 307 /*--------------------------------------------- 308 * This entrypoint is used by the TSPD to pass 309 * control for handling a pending S-EL1 FIQ. 310 * 'x0' contains a magic number which indicates 311 * this. TSPD expects control to be handed back 312 * at the end of FIQ processing. This is done 313 * through an SMC. The handover agreement is: 314 * 315 * 1. PSTATE.DAIF are set upon entry. 'x1' has 316 * the ELR_EL3 from the non-secure state. 317 * 2. TSP has to preserve the callee saved 318 * general purpose registers, SP_EL1/EL0 and 319 * LR. 320 * 3. TSP has to preserve the system and vfp 321 * registers (if applicable). 322 * 4. TSP can use 'x0-x18' to enable its C 323 * runtime. 324 * 5. TSP returns to TSPD using an SMC with 325 * 'x0' = TSP_HANDLED_S_EL1_FIQ 326 * --------------------------------------------- 327 */ 328func tsp_fiq_entry 329#if DEBUG 330 mov x2, #(TSP_HANDLE_FIQ_AND_RETURN & ~0xffff) 331 movk x2, #(TSP_HANDLE_FIQ_AND_RETURN & 0xffff) 332 cmp x0, x2 333 b.ne tsp_fiq_entry_panic 334#endif 335 /*--------------------------------------------- 336 * Save any previous context needed to perform 337 * an exception return from S-EL1 e.g. context 338 * from a previous IRQ. Update statistics and 339 * handle the FIQ before returning to the TSPD. 340 * IRQ/FIQs are not enabled since that will 341 * complicate the implementation. Execution 342 * will be transferred back to the normal world 343 * in any case. A non-zero return value from the 344 * fiq handler is an error. 345 * --------------------------------------------- 346 */ 347 save_eret_context x2 x3 348 bl tsp_update_sync_fiq_stats 349 bl tsp_fiq_handler 350 cbnz x0, tsp_fiq_entry_panic 351 restore_eret_context x2 x3 352 mov x0, #(TSP_HANDLED_S_EL1_FIQ & ~0xffff) 353 movk x0, #(TSP_HANDLED_S_EL1_FIQ & 0xffff) 354 smc #0 355 356tsp_fiq_entry_panic: 357 b tsp_fiq_entry_panic 358 359 /*--------------------------------------------- 360 * This entrypoint is used by the TSPD when this 361 * cpu resumes execution after an earlier 362 * CPU_SUSPEND psci call to ask the TSP to 363 * restore its saved context. In the current 364 * implementation, the TSPD saves and restores 365 * EL1 state so nothing is done here apart from 366 * acknowledging the request. 367 * --------------------------------------------- 368 */ 369func tsp_cpu_resume_entry 370 bl tsp_cpu_resume_main 371 restore_args_call_smc 372tsp_cpu_resume_panic: 373 b tsp_cpu_resume_panic 374 375 /*--------------------------------------------- 376 * This entrypoint is used by the TSPD to ask 377 * the TSP to service a fast smc request. 378 * --------------------------------------------- 379 */ 380func tsp_fast_smc_entry 381 bl tsp_smc_handler 382 restore_args_call_smc 383tsp_fast_smc_entry_panic: 384 b tsp_fast_smc_entry_panic 385 386 /*--------------------------------------------- 387 * This entrypoint is used by the TSPD to ask 388 * the TSP to service a std smc request. 389 * We will enable preemption during execution 390 * of tsp_smc_handler. 391 * --------------------------------------------- 392 */ 393func tsp_std_smc_entry 394 msr daifclr, #DAIF_FIQ_BIT | DAIF_IRQ_BIT 395 bl tsp_smc_handler 396 msr daifset, #DAIF_FIQ_BIT | DAIF_IRQ_BIT 397 restore_args_call_smc 398tsp_std_smc_entry_panic: 399 b tsp_std_smc_entry_panic 400