1*91f16700Schasinglulu /* 2*91f16700Schasinglulu * Copyright (c) 2013-2021, ARM Limited and Contributors. All rights reserved. 3*91f16700Schasinglulu * 4*91f16700Schasinglulu * SPDX-License-Identifier: BSD-3-Clause 5*91f16700Schasinglulu */ 6*91f16700Schasinglulu 7*91f16700Schasinglulu #ifndef TSPD_PRIVATE_H 8*91f16700Schasinglulu #define TSPD_PRIVATE_H 9*91f16700Schasinglulu 10*91f16700Schasinglulu #include <platform_def.h> 11*91f16700Schasinglulu 12*91f16700Schasinglulu #include <arch.h> 13*91f16700Schasinglulu #include <bl31/interrupt_mgmt.h> 14*91f16700Schasinglulu #include <context.h> 15*91f16700Schasinglulu #include <lib/psci/psci.h> 16*91f16700Schasinglulu 17*91f16700Schasinglulu /******************************************************************************* 18*91f16700Schasinglulu * Secure Payload PM state information e.g. SP is suspended, uninitialised etc 19*91f16700Schasinglulu * and macros to access the state information in the per-cpu 'state' flags 20*91f16700Schasinglulu ******************************************************************************/ 21*91f16700Schasinglulu #define TSP_PSTATE_OFF 0 22*91f16700Schasinglulu #define TSP_PSTATE_ON 1 23*91f16700Schasinglulu #define TSP_PSTATE_SUSPEND 2 24*91f16700Schasinglulu #define TSP_PSTATE_SHIFT 0 25*91f16700Schasinglulu #define TSP_PSTATE_MASK 0x3 26*91f16700Schasinglulu #define get_tsp_pstate(state) ((state >> TSP_PSTATE_SHIFT) & TSP_PSTATE_MASK) 27*91f16700Schasinglulu #define clr_tsp_pstate(state) (state &= ~(TSP_PSTATE_MASK \ 28*91f16700Schasinglulu << TSP_PSTATE_SHIFT)) 29*91f16700Schasinglulu #define set_tsp_pstate(st, pst) do { \ 30*91f16700Schasinglulu clr_tsp_pstate(st); \ 31*91f16700Schasinglulu st |= (pst & TSP_PSTATE_MASK) << \ 32*91f16700Schasinglulu TSP_PSTATE_SHIFT; \ 33*91f16700Schasinglulu } while (0); 34*91f16700Schasinglulu 35*91f16700Schasinglulu 36*91f16700Schasinglulu /* 37*91f16700Schasinglulu * This flag is used by the TSPD to determine if the TSP is servicing a yielding 38*91f16700Schasinglulu * SMC request prior to programming the next entry into the TSP e.g. if TSP 39*91f16700Schasinglulu * execution is preempted by a non-secure interrupt and handed control to the 40*91f16700Schasinglulu * normal world. If another request which is distinct from what the TSP was 41*91f16700Schasinglulu * previously doing arrives, then this flag will be help the TSPD to either 42*91f16700Schasinglulu * reject the new request or service it while ensuring that the previous context 43*91f16700Schasinglulu * is not corrupted. 44*91f16700Schasinglulu */ 45*91f16700Schasinglulu #define YIELD_SMC_ACTIVE_FLAG_SHIFT 2 46*91f16700Schasinglulu #define YIELD_SMC_ACTIVE_FLAG_MASK 1 47*91f16700Schasinglulu #define get_yield_smc_active_flag(state) \ 48*91f16700Schasinglulu ((state >> YIELD_SMC_ACTIVE_FLAG_SHIFT) \ 49*91f16700Schasinglulu & YIELD_SMC_ACTIVE_FLAG_MASK) 50*91f16700Schasinglulu #define set_yield_smc_active_flag(state) (state |= \ 51*91f16700Schasinglulu 1 << YIELD_SMC_ACTIVE_FLAG_SHIFT) 52*91f16700Schasinglulu #define clr_yield_smc_active_flag(state) (state &= \ 53*91f16700Schasinglulu ~(YIELD_SMC_ACTIVE_FLAG_MASK \ 54*91f16700Schasinglulu << YIELD_SMC_ACTIVE_FLAG_SHIFT)) 55*91f16700Schasinglulu 56*91f16700Schasinglulu /******************************************************************************* 57*91f16700Schasinglulu * Secure Payload execution state information i.e. aarch32 or aarch64 58*91f16700Schasinglulu ******************************************************************************/ 59*91f16700Schasinglulu #define TSP_AARCH32 MODE_RW_32 60*91f16700Schasinglulu #define TSP_AARCH64 MODE_RW_64 61*91f16700Schasinglulu 62*91f16700Schasinglulu /******************************************************************************* 63*91f16700Schasinglulu * The SPD should know the type of Secure Payload. 64*91f16700Schasinglulu ******************************************************************************/ 65*91f16700Schasinglulu #define TSP_TYPE_UP PSCI_TOS_NOT_UP_MIG_CAP 66*91f16700Schasinglulu #define TSP_TYPE_UPM PSCI_TOS_UP_MIG_CAP 67*91f16700Schasinglulu #define TSP_TYPE_MP PSCI_TOS_NOT_PRESENT_MP 68*91f16700Schasinglulu 69*91f16700Schasinglulu /******************************************************************************* 70*91f16700Schasinglulu * Secure Payload migrate type information as known to the SPD. We assume that 71*91f16700Schasinglulu * the SPD is dealing with an MP Secure Payload. 72*91f16700Schasinglulu ******************************************************************************/ 73*91f16700Schasinglulu #define TSP_MIGRATE_INFO TSP_TYPE_MP 74*91f16700Schasinglulu 75*91f16700Schasinglulu /******************************************************************************* 76*91f16700Schasinglulu * Number of cpus that the present on this platform. TODO: Rely on a topology 77*91f16700Schasinglulu * tree to determine this in the future to avoid assumptions about mpidr 78*91f16700Schasinglulu * allocation 79*91f16700Schasinglulu ******************************************************************************/ 80*91f16700Schasinglulu #define TSPD_CORE_COUNT PLATFORM_CORE_COUNT 81*91f16700Schasinglulu 82*91f16700Schasinglulu /******************************************************************************* 83*91f16700Schasinglulu * Constants that allow assembler code to preserve callee-saved registers of the 84*91f16700Schasinglulu * C runtime context while performing a security state switch. 85*91f16700Schasinglulu ******************************************************************************/ 86*91f16700Schasinglulu #define TSPD_C_RT_CTX_X19 0x0 87*91f16700Schasinglulu #define TSPD_C_RT_CTX_X20 0x8 88*91f16700Schasinglulu #define TSPD_C_RT_CTX_X21 0x10 89*91f16700Schasinglulu #define TSPD_C_RT_CTX_X22 0x18 90*91f16700Schasinglulu #define TSPD_C_RT_CTX_X23 0x20 91*91f16700Schasinglulu #define TSPD_C_RT_CTX_X24 0x28 92*91f16700Schasinglulu #define TSPD_C_RT_CTX_X25 0x30 93*91f16700Schasinglulu #define TSPD_C_RT_CTX_X26 0x38 94*91f16700Schasinglulu #define TSPD_C_RT_CTX_X27 0x40 95*91f16700Schasinglulu #define TSPD_C_RT_CTX_X28 0x48 96*91f16700Schasinglulu #define TSPD_C_RT_CTX_X29 0x50 97*91f16700Schasinglulu #define TSPD_C_RT_CTX_X30 0x58 98*91f16700Schasinglulu #define TSPD_C_RT_CTX_SIZE 0x60 99*91f16700Schasinglulu #define TSPD_C_RT_CTX_ENTRIES (TSPD_C_RT_CTX_SIZE >> DWORD_SHIFT) 100*91f16700Schasinglulu 101*91f16700Schasinglulu /******************************************************************************* 102*91f16700Schasinglulu * Constants that allow assembler code to preserve caller-saved registers of the 103*91f16700Schasinglulu * SP context while performing a TSP preemption. 104*91f16700Schasinglulu * Note: These offsets have to match with the offsets for the corresponding 105*91f16700Schasinglulu * registers in cpu_context as we are using memcpy to copy the values from 106*91f16700Schasinglulu * cpu_context to sp_ctx. 107*91f16700Schasinglulu ******************************************************************************/ 108*91f16700Schasinglulu #define TSPD_SP_CTX_X0 0x0 109*91f16700Schasinglulu #define TSPD_SP_CTX_X1 0x8 110*91f16700Schasinglulu #define TSPD_SP_CTX_X2 0x10 111*91f16700Schasinglulu #define TSPD_SP_CTX_X3 0x18 112*91f16700Schasinglulu #define TSPD_SP_CTX_X4 0x20 113*91f16700Schasinglulu #define TSPD_SP_CTX_X5 0x28 114*91f16700Schasinglulu #define TSPD_SP_CTX_X6 0x30 115*91f16700Schasinglulu #define TSPD_SP_CTX_X7 0x38 116*91f16700Schasinglulu #define TSPD_SP_CTX_X8 0x40 117*91f16700Schasinglulu #define TSPD_SP_CTX_X9 0x48 118*91f16700Schasinglulu #define TSPD_SP_CTX_X10 0x50 119*91f16700Schasinglulu #define TSPD_SP_CTX_X11 0x58 120*91f16700Schasinglulu #define TSPD_SP_CTX_X12 0x60 121*91f16700Schasinglulu #define TSPD_SP_CTX_X13 0x68 122*91f16700Schasinglulu #define TSPD_SP_CTX_X14 0x70 123*91f16700Schasinglulu #define TSPD_SP_CTX_X15 0x78 124*91f16700Schasinglulu #define TSPD_SP_CTX_X16 0x80 125*91f16700Schasinglulu #define TSPD_SP_CTX_X17 0x88 126*91f16700Schasinglulu #define TSPD_SP_CTX_SIZE 0x90 127*91f16700Schasinglulu #define TSPD_SP_CTX_ENTRIES (TSPD_SP_CTX_SIZE >> DWORD_SHIFT) 128*91f16700Schasinglulu 129*91f16700Schasinglulu #ifndef __ASSEMBLER__ 130*91f16700Schasinglulu 131*91f16700Schasinglulu #include <stdint.h> 132*91f16700Schasinglulu 133*91f16700Schasinglulu #include <lib/cassert.h> 134*91f16700Schasinglulu 135*91f16700Schasinglulu /* 136*91f16700Schasinglulu * The number of arguments to save during a SMC call for TSP. 137*91f16700Schasinglulu * Currently only x1 and x2 are used by TSP. 138*91f16700Schasinglulu */ 139*91f16700Schasinglulu #define TSP_NUM_ARGS 0x2 140*91f16700Schasinglulu 141*91f16700Schasinglulu /* AArch64 callee saved general purpose register context structure. */ 142*91f16700Schasinglulu DEFINE_REG_STRUCT(c_rt_regs, TSPD_C_RT_CTX_ENTRIES); 143*91f16700Schasinglulu 144*91f16700Schasinglulu /* 145*91f16700Schasinglulu * Compile time assertion to ensure that both the compiler and linker 146*91f16700Schasinglulu * have the same double word aligned view of the size of the C runtime 147*91f16700Schasinglulu * register context. 148*91f16700Schasinglulu */ 149*91f16700Schasinglulu CASSERT(TSPD_C_RT_CTX_SIZE == sizeof(c_rt_regs_t), 150*91f16700Schasinglulu assert_spd_c_rt_regs_size_mismatch); 151*91f16700Schasinglulu 152*91f16700Schasinglulu /* SEL1 Secure payload (SP) caller saved register context structure. */ 153*91f16700Schasinglulu DEFINE_REG_STRUCT(sp_ctx_regs, TSPD_SP_CTX_ENTRIES); 154*91f16700Schasinglulu 155*91f16700Schasinglulu /* 156*91f16700Schasinglulu * Compile time assertion to ensure that both the compiler and linker 157*91f16700Schasinglulu * have the same double word aligned view of the size of the C runtime 158*91f16700Schasinglulu * register context. 159*91f16700Schasinglulu */ 160*91f16700Schasinglulu CASSERT(TSPD_SP_CTX_SIZE == sizeof(sp_ctx_regs_t), 161*91f16700Schasinglulu assert_spd_sp_regs_size_mismatch); 162*91f16700Schasinglulu 163*91f16700Schasinglulu /******************************************************************************* 164*91f16700Schasinglulu * Structure which helps the SPD to maintain the per-cpu state of the SP. 165*91f16700Schasinglulu * 'saved_spsr_el3' - temporary copy to allow S-EL1 interrupt handling when 166*91f16700Schasinglulu * the TSP has been preempted. 167*91f16700Schasinglulu * 'saved_elr_el3' - temporary copy to allow S-EL1 interrupt handling when 168*91f16700Schasinglulu * the TSP has been preempted. 169*91f16700Schasinglulu * 'state' - collection of flags to track SP state e.g. on/off 170*91f16700Schasinglulu * 'mpidr' - mpidr to associate a context with a cpu 171*91f16700Schasinglulu * 'c_rt_ctx' - stack address to restore C runtime context from after 172*91f16700Schasinglulu * returning from a synchronous entry into the SP. 173*91f16700Schasinglulu * 'cpu_ctx' - space to maintain SP architectural state 174*91f16700Schasinglulu * 'saved_tsp_args' - space to store arguments for TSP arithmetic operations 175*91f16700Schasinglulu * which will queried using the TSP_GET_ARGS SMC by TSP. 176*91f16700Schasinglulu * 'sp_ctx' - space to save the SEL1 Secure Payload(SP) caller saved 177*91f16700Schasinglulu * register context after it has been preempted by an EL3 178*91f16700Schasinglulu * routed NS interrupt and when a Secure Interrupt is taken 179*91f16700Schasinglulu * to SP. 180*91f16700Schasinglulu ******************************************************************************/ 181*91f16700Schasinglulu typedef struct tsp_context { 182*91f16700Schasinglulu uint64_t saved_elr_el3; 183*91f16700Schasinglulu uint32_t saved_spsr_el3; 184*91f16700Schasinglulu uint32_t state; 185*91f16700Schasinglulu uint64_t mpidr; 186*91f16700Schasinglulu uint64_t c_rt_ctx; 187*91f16700Schasinglulu cpu_context_t cpu_ctx; 188*91f16700Schasinglulu uint64_t saved_tsp_args[TSP_NUM_ARGS]; 189*91f16700Schasinglulu #if TSP_NS_INTR_ASYNC_PREEMPT 190*91f16700Schasinglulu sp_ctx_regs_t sp_ctx; 191*91f16700Schasinglulu bool preempted_by_sel1_intr; 192*91f16700Schasinglulu #endif 193*91f16700Schasinglulu } tsp_context_t; 194*91f16700Schasinglulu 195*91f16700Schasinglulu /* Helper macros to store and retrieve tsp args from tsp_context */ 196*91f16700Schasinglulu #define store_tsp_args(_tsp_ctx, _x1, _x2) do {\ 197*91f16700Schasinglulu _tsp_ctx->saved_tsp_args[0] = _x1;\ 198*91f16700Schasinglulu _tsp_ctx->saved_tsp_args[1] = _x2;\ 199*91f16700Schasinglulu } while (0) 200*91f16700Schasinglulu 201*91f16700Schasinglulu #define get_tsp_args(_tsp_ctx, _x1, _x2) do {\ 202*91f16700Schasinglulu _x1 = _tsp_ctx->saved_tsp_args[0];\ 203*91f16700Schasinglulu _x2 = _tsp_ctx->saved_tsp_args[1];\ 204*91f16700Schasinglulu } while (0) 205*91f16700Schasinglulu 206*91f16700Schasinglulu /* TSPD power management handlers */ 207*91f16700Schasinglulu extern const spd_pm_ops_t tspd_pm; 208*91f16700Schasinglulu 209*91f16700Schasinglulu /******************************************************************************* 210*91f16700Schasinglulu * Forward declarations 211*91f16700Schasinglulu ******************************************************************************/ 212*91f16700Schasinglulu typedef struct tsp_vectors tsp_vectors_t; 213*91f16700Schasinglulu 214*91f16700Schasinglulu /******************************************************************************* 215*91f16700Schasinglulu * Function & Data prototypes 216*91f16700Schasinglulu ******************************************************************************/ 217*91f16700Schasinglulu uint64_t tspd_enter_sp(uint64_t *c_rt_ctx); 218*91f16700Schasinglulu void __dead2 tspd_exit_sp(uint64_t c_rt_ctx, uint64_t ret); 219*91f16700Schasinglulu uint64_t tspd_synchronous_sp_entry(tsp_context_t *tsp_ctx); 220*91f16700Schasinglulu void __dead2 tspd_synchronous_sp_exit(tsp_context_t *tsp_ctx, uint64_t ret); 221*91f16700Schasinglulu void tspd_init_tsp_ep_state(struct entry_point_info *tsp_entry_point, 222*91f16700Schasinglulu uint32_t rw, 223*91f16700Schasinglulu uint64_t pc, 224*91f16700Schasinglulu tsp_context_t *tsp_ctx); 225*91f16700Schasinglulu int tspd_abort_preempted_smc(tsp_context_t *tsp_ctx); 226*91f16700Schasinglulu 227*91f16700Schasinglulu uint64_t tspd_handle_sp_preemption(void *handle); 228*91f16700Schasinglulu 229*91f16700Schasinglulu extern tsp_context_t tspd_sp_context[TSPD_CORE_COUNT]; 230*91f16700Schasinglulu extern tsp_vectors_t *tsp_vectors; 231*91f16700Schasinglulu #endif /*__ASSEMBLER__*/ 232*91f16700Schasinglulu 233*91f16700Schasinglulu #endif /* TSPD_PRIVATE_H */ 234