1375f538aSAchin Gupta /* 23df6012aSDouglas Raillard * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved. 3375f538aSAchin Gupta * 4*82cb2c1aSdp-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 /* 363ee8a164SAchin Gupta * This flag is used by the TSPD to determine if the TSP is servicing a standard 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 */ 443ee8a164SAchin Gupta #define STD_SMC_ACTIVE_FLAG_SHIFT 2 453ee8a164SAchin Gupta #define STD_SMC_ACTIVE_FLAG_MASK 1 463ee8a164SAchin Gupta #define get_std_smc_active_flag(state) ((state >> STD_SMC_ACTIVE_FLAG_SHIFT) \ 473ee8a164SAchin Gupta & STD_SMC_ACTIVE_FLAG_MASK) 483ee8a164SAchin Gupta #define set_std_smc_active_flag(state) (state |= \ 493ee8a164SAchin Gupta 1 << STD_SMC_ACTIVE_FLAG_SHIFT) 503ee8a164SAchin Gupta #define clr_std_smc_active_flag(state) (state &= \ 513ee8a164SAchin Gupta ~(STD_SMC_ACTIVE_FLAG_MASK \ 523ee8a164SAchin Gupta << STD_SMC_ACTIVE_FLAG_SHIFT)) 53375f538aSAchin Gupta 54375f538aSAchin Gupta /******************************************************************************* 55375f538aSAchin Gupta * Secure Payload execution state information i.e. aarch32 or aarch64 56375f538aSAchin Gupta ******************************************************************************/ 57375f538aSAchin Gupta #define TSP_AARCH32 MODE_RW_32 58375f538aSAchin Gupta #define TSP_AARCH64 MODE_RW_64 59375f538aSAchin Gupta 60375f538aSAchin Gupta /******************************************************************************* 61375f538aSAchin Gupta * The SPD should know the type of Secure Payload. 62375f538aSAchin Gupta ******************************************************************************/ 63375f538aSAchin Gupta #define TSP_TYPE_UP PSCI_TOS_NOT_UP_MIG_CAP 64375f538aSAchin Gupta #define TSP_TYPE_UPM PSCI_TOS_UP_MIG_CAP 65375f538aSAchin Gupta #define TSP_TYPE_MP PSCI_TOS_NOT_PRESENT_MP 66375f538aSAchin Gupta 67375f538aSAchin Gupta /******************************************************************************* 68375f538aSAchin Gupta * Secure Payload migrate type information as known to the SPD. We assume that 69375f538aSAchin Gupta * the SPD is dealing with an MP Secure Payload. 70375f538aSAchin Gupta ******************************************************************************/ 71375f538aSAchin Gupta #define TSP_MIGRATE_INFO TSP_TYPE_MP 72375f538aSAchin Gupta 73375f538aSAchin Gupta /******************************************************************************* 74375f538aSAchin Gupta * Number of cpus that the present on this platform. TODO: Rely on a topology 75375f538aSAchin Gupta * tree to determine this in the future to avoid assumptions about mpidr 76375f538aSAchin Gupta * allocation 77375f538aSAchin Gupta ******************************************************************************/ 78375f538aSAchin Gupta #define TSPD_CORE_COUNT PLATFORM_CORE_COUNT 79375f538aSAchin Gupta 80375f538aSAchin Gupta /******************************************************************************* 81375f538aSAchin Gupta * Constants that allow assembler code to preserve callee-saved registers of the 82375f538aSAchin Gupta * C runtime context while performing a security state switch. 83375f538aSAchin Gupta ******************************************************************************/ 84375f538aSAchin Gupta #define TSPD_C_RT_CTX_X19 0x0 85375f538aSAchin Gupta #define TSPD_C_RT_CTX_X20 0x8 86375f538aSAchin Gupta #define TSPD_C_RT_CTX_X21 0x10 87375f538aSAchin Gupta #define TSPD_C_RT_CTX_X22 0x18 88375f538aSAchin Gupta #define TSPD_C_RT_CTX_X23 0x20 89375f538aSAchin Gupta #define TSPD_C_RT_CTX_X24 0x28 90375f538aSAchin Gupta #define TSPD_C_RT_CTX_X25 0x30 91375f538aSAchin Gupta #define TSPD_C_RT_CTX_X26 0x38 92375f538aSAchin Gupta #define TSPD_C_RT_CTX_X27 0x40 93375f538aSAchin Gupta #define TSPD_C_RT_CTX_X28 0x48 94375f538aSAchin Gupta #define TSPD_C_RT_CTX_X29 0x50 95375f538aSAchin Gupta #define TSPD_C_RT_CTX_X30 0x58 96375f538aSAchin Gupta #define TSPD_C_RT_CTX_SIZE 0x60 97375f538aSAchin Gupta #define TSPD_C_RT_CTX_ENTRIES (TSPD_C_RT_CTX_SIZE >> DWORD_SHIFT) 98375f538aSAchin Gupta 99f4f1ae77SSoby Mathew /******************************************************************************* 100f4f1ae77SSoby Mathew * Constants that allow assembler code to preserve caller-saved registers of the 101f4f1ae77SSoby Mathew * SP context while performing a TSP preemption. 102f4f1ae77SSoby Mathew * Note: These offsets have to match with the offsets for the corresponding 103f4f1ae77SSoby Mathew * registers in cpu_context as we are using memcpy to copy the values from 104f4f1ae77SSoby Mathew * cpu_context to sp_ctx. 105f4f1ae77SSoby Mathew ******************************************************************************/ 106f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X0 0x0 107f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X1 0x8 108f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X2 0x10 109f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X3 0x18 110f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X4 0x20 111f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X5 0x28 112f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X6 0x30 113f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X7 0x38 114f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X8 0x40 115f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X9 0x48 116f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X10 0x50 117f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X11 0x58 118f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X12 0x60 119f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X13 0x68 120f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X14 0x70 121f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X15 0x78 122f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X16 0x80 123f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X17 0x88 124f4f1ae77SSoby Mathew #define TSPD_SP_CTX_SIZE 0x90 125f4f1ae77SSoby Mathew #define TSPD_SP_CTX_ENTRIES (TSPD_SP_CTX_SIZE >> DWORD_SHIFT) 126f4f1ae77SSoby Mathew 127375f538aSAchin Gupta #ifndef __ASSEMBLY__ 128375f538aSAchin Gupta 12997043ac9SDan Handley #include <cassert.h> 13097043ac9SDan Handley #include <stdint.h> 13197043ac9SDan Handley 132239b04faSSoby Mathew /* 133239b04faSSoby Mathew * The number of arguments to save during a SMC call for TSP. 134239b04faSSoby Mathew * Currently only x1 and x2 are used by TSP. 135239b04faSSoby Mathew */ 136239b04faSSoby Mathew #define TSP_NUM_ARGS 0x2 137239b04faSSoby Mathew 138375f538aSAchin Gupta /* AArch64 callee saved general purpose register context structure. */ 139375f538aSAchin Gupta DEFINE_REG_STRUCT(c_rt_regs, TSPD_C_RT_CTX_ENTRIES); 140375f538aSAchin Gupta 141375f538aSAchin Gupta /* 142375f538aSAchin Gupta * Compile time assertion to ensure that both the compiler and linker 143375f538aSAchin Gupta * have the same double word aligned view of the size of the C runtime 144375f538aSAchin Gupta * register context. 145375f538aSAchin Gupta */ 146fb037bfbSDan Handley CASSERT(TSPD_C_RT_CTX_SIZE == sizeof(c_rt_regs_t), \ 147375f538aSAchin Gupta assert_spd_c_rt_regs_size_mismatch); 148375f538aSAchin Gupta 149f4f1ae77SSoby Mathew /* SEL1 Secure payload (SP) caller saved register context structure. */ 150f4f1ae77SSoby Mathew DEFINE_REG_STRUCT(sp_ctx_regs, TSPD_SP_CTX_ENTRIES); 151f4f1ae77SSoby Mathew 152f4f1ae77SSoby Mathew /* 153f4f1ae77SSoby Mathew * Compile time assertion to ensure that both the compiler and linker 154f4f1ae77SSoby Mathew * have the same double word aligned view of the size of the C runtime 155f4f1ae77SSoby Mathew * register context. 156f4f1ae77SSoby Mathew */ 157f4f1ae77SSoby Mathew CASSERT(TSPD_SP_CTX_SIZE == sizeof(sp_ctx_regs_t), \ 158f4f1ae77SSoby Mathew assert_spd_sp_regs_size_mismatch); 159f4f1ae77SSoby Mathew 160375f538aSAchin Gupta /******************************************************************************* 161375f538aSAchin Gupta * Structure which helps the SPD to maintain the per-cpu state of the SP. 16202446137SSoby Mathew * 'saved_spsr_el3' - temporary copy to allow S-EL1 interrupt handling when 16302446137SSoby Mathew * the TSP has been preempted. 16402446137SSoby Mathew * 'saved_elr_el3' - temporary copy to allow S-EL1 interrupt handling when 16502446137SSoby Mathew * the TSP has been preempted. 166375f538aSAchin Gupta * 'state' - collection of flags to track SP state e.g. on/off 167375f538aSAchin Gupta * 'mpidr' - mpidr to associate a context with a cpu 168b44a4435SAchin Gupta * 'c_rt_ctx' - stack address to restore C runtime context from after 169b44a4435SAchin Gupta * returning from a synchronous entry into the SP. 170375f538aSAchin Gupta * 'cpu_ctx' - space to maintain SP architectural state 171239b04faSSoby Mathew * 'saved_tsp_args' - space to store arguments for TSP arithmetic operations 172239b04faSSoby Mathew * which will queried using the TSP_GET_ARGS SMC by TSP. 173f4f1ae77SSoby Mathew * 'sp_ctx' - space to save the SEL1 Secure Payload(SP) caller saved 174f4f1ae77SSoby Mathew * register context after it has been preempted by an EL3 175f4f1ae77SSoby Mathew * routed NS interrupt and when a Secure Interrupt is taken 176f4f1ae77SSoby Mathew * to SP. 177375f538aSAchin Gupta ******************************************************************************/ 178fb037bfbSDan Handley typedef struct tsp_context { 179b44a4435SAchin Gupta uint64_t saved_elr_el3; 180b44a4435SAchin Gupta uint32_t saved_spsr_el3; 181375f538aSAchin Gupta uint32_t state; 182375f538aSAchin Gupta uint64_t mpidr; 183375f538aSAchin Gupta uint64_t c_rt_ctx; 184fb037bfbSDan Handley cpu_context_t cpu_ctx; 185239b04faSSoby Mathew uint64_t saved_tsp_args[TSP_NUM_ARGS]; 18602446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 187f4f1ae77SSoby Mathew sp_ctx_regs_t sp_ctx; 188f4f1ae77SSoby Mathew #endif 189fb037bfbSDan Handley } tsp_context_t; 190375f538aSAchin Gupta 191239b04faSSoby Mathew /* Helper macros to store and retrieve tsp args from tsp_context */ 192239b04faSSoby Mathew #define store_tsp_args(tsp_ctx, x1, x2) do {\ 193239b04faSSoby Mathew tsp_ctx->saved_tsp_args[0] = x1;\ 194239b04faSSoby Mathew tsp_ctx->saved_tsp_args[1] = x2;\ 195239b04faSSoby Mathew } while (0) 196239b04faSSoby Mathew 197239b04faSSoby Mathew #define get_tsp_args(tsp_ctx, x1, x2) do {\ 198239b04faSSoby Mathew x1 = tsp_ctx->saved_tsp_args[0];\ 199239b04faSSoby Mathew x2 = tsp_ctx->saved_tsp_args[1];\ 200239b04faSSoby Mathew } while (0) 201239b04faSSoby Mathew 2027f366605SJeenu Viswambharan /* TSPD power management handlers */ 203fb037bfbSDan Handley extern const spd_pm_ops_t tspd_pm; 2047f366605SJeenu Viswambharan 205375f538aSAchin Gupta /******************************************************************************* 20697043ac9SDan Handley * Forward declarations 20797043ac9SDan Handley ******************************************************************************/ 208399fb08fSAndrew Thoelke struct tsp_vectors; 20997043ac9SDan Handley 21097043ac9SDan Handley /******************************************************************************* 211375f538aSAchin Gupta * Function & Data prototypes 212375f538aSAchin Gupta ******************************************************************************/ 213c6bc0710SDan Handley uint64_t tspd_enter_sp(uint64_t *c_rt_ctx); 214c6bc0710SDan Handley void __dead2 tspd_exit_sp(uint64_t c_rt_ctx, uint64_t ret); 215c6bc0710SDan Handley uint64_t tspd_synchronous_sp_entry(tsp_context_t *tsp_ctx); 216c6bc0710SDan Handley void __dead2 tspd_synchronous_sp_exit(tsp_context_t *tsp_ctx, uint64_t ret); 21750e27dadSVikram Kanigiri void tspd_init_tsp_ep_state(struct entry_point_info *tsp_ep, 218375f538aSAchin Gupta uint32_t rw, 21950e27dadSVikram Kanigiri uint64_t pc, 220fb037bfbSDan Handley tsp_context_t *tsp_ctx); 2213df6012aSDouglas Raillard int tspd_abort_preempted_smc(tsp_context_t *tsp_ctx); 22250e27dadSVikram Kanigiri 223fb037bfbSDan Handley extern tsp_context_t tspd_sp_context[TSPD_CORE_COUNT]; 224399fb08fSAndrew Thoelke extern struct tsp_vectors *tsp_vectors; 225375f538aSAchin Gupta #endif /*__ASSEMBLY__*/ 226375f538aSAchin Gupta 22797043ac9SDan Handley #endif /* __TSPD_PRIVATE_H__ */ 228