1375f538aSAchin Gupta /* 2a27163bcSRoberto Vargas * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved. 3375f538aSAchin Gupta * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5375f538aSAchin Gupta */ 6375f538aSAchin Gupta 797043ac9SDan Handley #ifndef __TSPD_PRIVATE_H__ 897043ac9SDan Handley #define __TSPD_PRIVATE_H__ 9375f538aSAchin Gupta 10375f538aSAchin Gupta #include <arch.h> 1197043ac9SDan Handley #include <context.h> 12b44a4435SAchin Gupta #include <interrupt_mgmt.h> 135f0cdb05SDan Handley #include <platform_def.h> 14375f538aSAchin Gupta #include <psci.h> 15375f538aSAchin Gupta 16375f538aSAchin Gupta /******************************************************************************* 17375f538aSAchin Gupta * Secure Payload PM state information e.g. SP is suspended, uninitialised etc 183ee8a164SAchin Gupta * and macros to access the state information in the per-cpu 'state' flags 19375f538aSAchin Gupta ******************************************************************************/ 203ee8a164SAchin Gupta #define TSP_PSTATE_OFF 0 213ee8a164SAchin Gupta #define TSP_PSTATE_ON 1 223ee8a164SAchin Gupta #define TSP_PSTATE_SUSPEND 2 233ee8a164SAchin Gupta #define TSP_PSTATE_SHIFT 0 243ee8a164SAchin Gupta #define TSP_PSTATE_MASK 0x3 253ee8a164SAchin Gupta #define get_tsp_pstate(state) ((state >> TSP_PSTATE_SHIFT) & TSP_PSTATE_MASK) 263ee8a164SAchin Gupta #define clr_tsp_pstate(state) (state &= ~(TSP_PSTATE_MASK \ 273ee8a164SAchin Gupta << TSP_PSTATE_SHIFT)) 283ee8a164SAchin Gupta #define set_tsp_pstate(st, pst) do { \ 293ee8a164SAchin Gupta clr_tsp_pstate(st); \ 303ee8a164SAchin Gupta st |= (pst & TSP_PSTATE_MASK) << \ 313ee8a164SAchin Gupta TSP_PSTATE_SHIFT; \ 323ee8a164SAchin Gupta } while (0); 333ee8a164SAchin Gupta 343ee8a164SAchin Gupta 353ee8a164SAchin Gupta /* 3616292f54SDavid Cunado * This flag is used by the TSPD to determine if the TSP is servicing a yielding 373ee8a164SAchin Gupta * SMC request prior to programming the next entry into the TSP e.g. if TSP 383ee8a164SAchin Gupta * execution is preempted by a non-secure interrupt and handed control to the 393ee8a164SAchin Gupta * normal world. If another request which is distinct from what the TSP was 403ee8a164SAchin Gupta * previously doing arrives, then this flag will be help the TSPD to either 413ee8a164SAchin Gupta * reject the new request or service it while ensuring that the previous context 423ee8a164SAchin Gupta * is not corrupted. 433ee8a164SAchin Gupta */ 4416292f54SDavid Cunado #define YIELD_SMC_ACTIVE_FLAG_SHIFT 2 4516292f54SDavid Cunado #define YIELD_SMC_ACTIVE_FLAG_MASK 1 4616292f54SDavid Cunado #define get_yield_smc_active_flag(state) \ 4716292f54SDavid Cunado ((state >> YIELD_SMC_ACTIVE_FLAG_SHIFT) \ 4816292f54SDavid Cunado & YIELD_SMC_ACTIVE_FLAG_MASK) 4916292f54SDavid Cunado #define set_yield_smc_active_flag(state) (state |= \ 5016292f54SDavid Cunado 1 << YIELD_SMC_ACTIVE_FLAG_SHIFT) 5116292f54SDavid Cunado #define clr_yield_smc_active_flag(state) (state &= \ 5216292f54SDavid Cunado ~(YIELD_SMC_ACTIVE_FLAG_MASK \ 5316292f54SDavid Cunado << YIELD_SMC_ACTIVE_FLAG_SHIFT)) 54375f538aSAchin Gupta 55375f538aSAchin Gupta /******************************************************************************* 56375f538aSAchin Gupta * Secure Payload execution state information i.e. aarch32 or aarch64 57375f538aSAchin Gupta ******************************************************************************/ 58375f538aSAchin Gupta #define TSP_AARCH32 MODE_RW_32 59375f538aSAchin Gupta #define TSP_AARCH64 MODE_RW_64 60375f538aSAchin Gupta 61375f538aSAchin Gupta /******************************************************************************* 62375f538aSAchin Gupta * The SPD should know the type of Secure Payload. 63375f538aSAchin Gupta ******************************************************************************/ 64375f538aSAchin Gupta #define TSP_TYPE_UP PSCI_TOS_NOT_UP_MIG_CAP 65375f538aSAchin Gupta #define TSP_TYPE_UPM PSCI_TOS_UP_MIG_CAP 66375f538aSAchin Gupta #define TSP_TYPE_MP PSCI_TOS_NOT_PRESENT_MP 67375f538aSAchin Gupta 68375f538aSAchin Gupta /******************************************************************************* 69375f538aSAchin Gupta * Secure Payload migrate type information as known to the SPD. We assume that 70375f538aSAchin Gupta * the SPD is dealing with an MP Secure Payload. 71375f538aSAchin Gupta ******************************************************************************/ 72375f538aSAchin Gupta #define TSP_MIGRATE_INFO TSP_TYPE_MP 73375f538aSAchin Gupta 74375f538aSAchin Gupta /******************************************************************************* 75375f538aSAchin Gupta * Number of cpus that the present on this platform. TODO: Rely on a topology 76375f538aSAchin Gupta * tree to determine this in the future to avoid assumptions about mpidr 77375f538aSAchin Gupta * allocation 78375f538aSAchin Gupta ******************************************************************************/ 79375f538aSAchin Gupta #define TSPD_CORE_COUNT PLATFORM_CORE_COUNT 80375f538aSAchin Gupta 81375f538aSAchin Gupta /******************************************************************************* 82375f538aSAchin Gupta * Constants that allow assembler code to preserve callee-saved registers of the 83375f538aSAchin Gupta * C runtime context while performing a security state switch. 84375f538aSAchin Gupta ******************************************************************************/ 85375f538aSAchin Gupta #define TSPD_C_RT_CTX_X19 0x0 86375f538aSAchin Gupta #define TSPD_C_RT_CTX_X20 0x8 87375f538aSAchin Gupta #define TSPD_C_RT_CTX_X21 0x10 88375f538aSAchin Gupta #define TSPD_C_RT_CTX_X22 0x18 89375f538aSAchin Gupta #define TSPD_C_RT_CTX_X23 0x20 90375f538aSAchin Gupta #define TSPD_C_RT_CTX_X24 0x28 91375f538aSAchin Gupta #define TSPD_C_RT_CTX_X25 0x30 92375f538aSAchin Gupta #define TSPD_C_RT_CTX_X26 0x38 93375f538aSAchin Gupta #define TSPD_C_RT_CTX_X27 0x40 94375f538aSAchin Gupta #define TSPD_C_RT_CTX_X28 0x48 95375f538aSAchin Gupta #define TSPD_C_RT_CTX_X29 0x50 96375f538aSAchin Gupta #define TSPD_C_RT_CTX_X30 0x58 97375f538aSAchin Gupta #define TSPD_C_RT_CTX_SIZE 0x60 98375f538aSAchin Gupta #define TSPD_C_RT_CTX_ENTRIES (TSPD_C_RT_CTX_SIZE >> DWORD_SHIFT) 99375f538aSAchin Gupta 100f4f1ae77SSoby Mathew /******************************************************************************* 101f4f1ae77SSoby Mathew * Constants that allow assembler code to preserve caller-saved registers of the 102f4f1ae77SSoby Mathew * SP context while performing a TSP preemption. 103f4f1ae77SSoby Mathew * Note: These offsets have to match with the offsets for the corresponding 104f4f1ae77SSoby Mathew * registers in cpu_context as we are using memcpy to copy the values from 105f4f1ae77SSoby Mathew * cpu_context to sp_ctx. 106f4f1ae77SSoby Mathew ******************************************************************************/ 107f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X0 0x0 108f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X1 0x8 109f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X2 0x10 110f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X3 0x18 111f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X4 0x20 112f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X5 0x28 113f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X6 0x30 114f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X7 0x38 115f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X8 0x40 116f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X9 0x48 117f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X10 0x50 118f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X11 0x58 119f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X12 0x60 120f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X13 0x68 121f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X14 0x70 122f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X15 0x78 123f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X16 0x80 124f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X17 0x88 125f4f1ae77SSoby Mathew #define TSPD_SP_CTX_SIZE 0x90 126f4f1ae77SSoby Mathew #define TSPD_SP_CTX_ENTRIES (TSPD_SP_CTX_SIZE >> DWORD_SHIFT) 127f4f1ae77SSoby Mathew 128375f538aSAchin Gupta #ifndef __ASSEMBLY__ 129375f538aSAchin Gupta 13097043ac9SDan Handley #include <cassert.h> 13197043ac9SDan Handley #include <stdint.h> 13297043ac9SDan Handley 133239b04faSSoby Mathew /* 134239b04faSSoby Mathew * The number of arguments to save during a SMC call for TSP. 135239b04faSSoby Mathew * Currently only x1 and x2 are used by TSP. 136239b04faSSoby Mathew */ 137239b04faSSoby Mathew #define TSP_NUM_ARGS 0x2 138239b04faSSoby Mathew 139375f538aSAchin Gupta /* AArch64 callee saved general purpose register context structure. */ 140375f538aSAchin Gupta DEFINE_REG_STRUCT(c_rt_regs, TSPD_C_RT_CTX_ENTRIES); 141375f538aSAchin Gupta 142375f538aSAchin Gupta /* 143375f538aSAchin Gupta * Compile time assertion to ensure that both the compiler and linker 144375f538aSAchin Gupta * have the same double word aligned view of the size of the C runtime 145375f538aSAchin Gupta * register context. 146375f538aSAchin Gupta */ 147fb037bfbSDan Handley CASSERT(TSPD_C_RT_CTX_SIZE == sizeof(c_rt_regs_t), \ 148375f538aSAchin Gupta assert_spd_c_rt_regs_size_mismatch); 149375f538aSAchin Gupta 150f4f1ae77SSoby Mathew /* SEL1 Secure payload (SP) caller saved register context structure. */ 151f4f1ae77SSoby Mathew DEFINE_REG_STRUCT(sp_ctx_regs, TSPD_SP_CTX_ENTRIES); 152f4f1ae77SSoby Mathew 153f4f1ae77SSoby Mathew /* 154f4f1ae77SSoby Mathew * Compile time assertion to ensure that both the compiler and linker 155f4f1ae77SSoby Mathew * have the same double word aligned view of the size of the C runtime 156f4f1ae77SSoby Mathew * register context. 157f4f1ae77SSoby Mathew */ 158f4f1ae77SSoby Mathew CASSERT(TSPD_SP_CTX_SIZE == sizeof(sp_ctx_regs_t), \ 159f4f1ae77SSoby Mathew assert_spd_sp_regs_size_mismatch); 160f4f1ae77SSoby Mathew 161375f538aSAchin Gupta /******************************************************************************* 162375f538aSAchin Gupta * Structure which helps the SPD to maintain the per-cpu state of the SP. 16302446137SSoby Mathew * 'saved_spsr_el3' - temporary copy to allow S-EL1 interrupt handling when 16402446137SSoby Mathew * the TSP has been preempted. 16502446137SSoby Mathew * 'saved_elr_el3' - temporary copy to allow S-EL1 interrupt handling when 16602446137SSoby Mathew * the TSP has been preempted. 167375f538aSAchin Gupta * 'state' - collection of flags to track SP state e.g. on/off 168375f538aSAchin Gupta * 'mpidr' - mpidr to associate a context with a cpu 169b44a4435SAchin Gupta * 'c_rt_ctx' - stack address to restore C runtime context from after 170b44a4435SAchin Gupta * returning from a synchronous entry into the SP. 171375f538aSAchin Gupta * 'cpu_ctx' - space to maintain SP architectural state 172239b04faSSoby Mathew * 'saved_tsp_args' - space to store arguments for TSP arithmetic operations 173239b04faSSoby Mathew * which will queried using the TSP_GET_ARGS SMC by TSP. 174f4f1ae77SSoby Mathew * 'sp_ctx' - space to save the SEL1 Secure Payload(SP) caller saved 175f4f1ae77SSoby Mathew * register context after it has been preempted by an EL3 176f4f1ae77SSoby Mathew * routed NS interrupt and when a Secure Interrupt is taken 177f4f1ae77SSoby Mathew * to SP. 178375f538aSAchin Gupta ******************************************************************************/ 179fb037bfbSDan Handley typedef struct tsp_context { 180b44a4435SAchin Gupta uint64_t saved_elr_el3; 181b44a4435SAchin Gupta uint32_t saved_spsr_el3; 182375f538aSAchin Gupta uint32_t state; 183375f538aSAchin Gupta uint64_t mpidr; 184375f538aSAchin Gupta uint64_t c_rt_ctx; 185fb037bfbSDan Handley cpu_context_t cpu_ctx; 186239b04faSSoby Mathew uint64_t saved_tsp_args[TSP_NUM_ARGS]; 18702446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 188f4f1ae77SSoby Mathew sp_ctx_regs_t sp_ctx; 189f4f1ae77SSoby Mathew #endif 190fb037bfbSDan Handley } tsp_context_t; 191375f538aSAchin Gupta 192239b04faSSoby Mathew /* Helper macros to store and retrieve tsp args from tsp_context */ 193239b04faSSoby Mathew #define store_tsp_args(tsp_ctx, x1, x2) do {\ 194239b04faSSoby Mathew tsp_ctx->saved_tsp_args[0] = x1;\ 195239b04faSSoby Mathew tsp_ctx->saved_tsp_args[1] = x2;\ 196239b04faSSoby Mathew } while (0) 197239b04faSSoby Mathew 198239b04faSSoby Mathew #define get_tsp_args(tsp_ctx, x1, x2) do {\ 199239b04faSSoby Mathew x1 = tsp_ctx->saved_tsp_args[0];\ 200239b04faSSoby Mathew x2 = tsp_ctx->saved_tsp_args[1];\ 201239b04faSSoby Mathew } while (0) 202239b04faSSoby Mathew 2037f366605SJeenu Viswambharan /* TSPD power management handlers */ 204fb037bfbSDan Handley extern const spd_pm_ops_t tspd_pm; 2057f366605SJeenu Viswambharan 206375f538aSAchin Gupta /******************************************************************************* 20797043ac9SDan Handley * Forward declarations 20897043ac9SDan Handley ******************************************************************************/ 209a27163bcSRoberto Vargas typedef struct tsp_vectors tsp_vectors_t; 21097043ac9SDan Handley 21197043ac9SDan Handley /******************************************************************************* 212375f538aSAchin Gupta * Function & Data prototypes 213375f538aSAchin Gupta ******************************************************************************/ 214c6bc0710SDan Handley uint64_t tspd_enter_sp(uint64_t *c_rt_ctx); 215c6bc0710SDan Handley void __dead2 tspd_exit_sp(uint64_t c_rt_ctx, uint64_t ret); 216c6bc0710SDan Handley uint64_t tspd_synchronous_sp_entry(tsp_context_t *tsp_ctx); 217c6bc0710SDan Handley void __dead2 tspd_synchronous_sp_exit(tsp_context_t *tsp_ctx, uint64_t ret); 218a27163bcSRoberto Vargas void tspd_init_tsp_ep_state(struct entry_point_info *tsp_entry_point, 219375f538aSAchin Gupta uint32_t rw, 22050e27dadSVikram Kanigiri uint64_t pc, 221fb037bfbSDan Handley tsp_context_t *tsp_ctx); 2223df6012aSDouglas Raillard int tspd_abort_preempted_smc(tsp_context_t *tsp_ctx); 22350e27dadSVikram Kanigiri 224*1a29f938SRoberto Vargas uint64_t tspd_smc_handler(uint32_t smc_fid, 225*1a29f938SRoberto Vargas uint64_t x1, 226*1a29f938SRoberto Vargas uint64_t x2, 227*1a29f938SRoberto Vargas uint64_t x3, 228*1a29f938SRoberto Vargas uint64_t x4, 229*1a29f938SRoberto Vargas void *cookie, 230*1a29f938SRoberto Vargas void *handle, 231*1a29f938SRoberto Vargas uint64_t flags); 232*1a29f938SRoberto Vargas 233*1a29f938SRoberto Vargas int32_t tspd_setup(void); 234*1a29f938SRoberto Vargas uint64_t tspd_handle_sp_preemption(void *handle); 235*1a29f938SRoberto Vargas 236fb037bfbSDan Handley extern tsp_context_t tspd_sp_context[TSPD_CORE_COUNT]; 237a27163bcSRoberto Vargas extern tsp_vectors_t *tsp_vectors; 238375f538aSAchin Gupta #endif /*__ASSEMBLY__*/ 239375f538aSAchin Gupta 24097043ac9SDan Handley #endif /* __TSPD_PRIVATE_H__ */ 241