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