xref: /rk3399_ARM-atf/services/spd/tspd/tspd_private.h (revision fcb7b26021c842c843db5d2df5aeb553785c0084)
1375f538aSAchin Gupta /*
2*cc1c867dSBoyan Karatotev  * Copyright (c) 2013-2025, Arm Limited and Contributors. All rights reserved.
3375f538aSAchin Gupta  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5375f538aSAchin Gupta  */
6375f538aSAchin Gupta 
7c3cf06f1SAntonio Nino Diaz #ifndef TSPD_PRIVATE_H
8c3cf06f1SAntonio Nino Diaz #define TSPD_PRIVATE_H
9375f538aSAchin Gupta 
105f0cdb05SDan Handley #include <platform_def.h>
1109d40e0eSAntonio Nino Diaz 
1209d40e0eSAntonio Nino Diaz #include <arch.h>
1309d40e0eSAntonio Nino Diaz #include <bl31/interrupt_mgmt.h>
1409d40e0eSAntonio Nino Diaz #include <context.h>
1509d40e0eSAntonio Nino Diaz #include <lib/psci/psci.h>
16375f538aSAchin Gupta 
17375f538aSAchin Gupta /*******************************************************************************
18375f538aSAchin Gupta  * Secure Payload PM state information e.g. SP is suspended, uninitialised etc
193ee8a164SAchin Gupta  * and macros to access the state information in the per-cpu 'state' flags
20375f538aSAchin Gupta  ******************************************************************************/
213ee8a164SAchin Gupta #define TSP_PSTATE_OFF		0
223ee8a164SAchin Gupta #define TSP_PSTATE_ON		1
233ee8a164SAchin Gupta #define TSP_PSTATE_SUSPEND	2
243ee8a164SAchin Gupta #define TSP_PSTATE_SHIFT	0
253ee8a164SAchin Gupta #define TSP_PSTATE_MASK	0x3
263ee8a164SAchin Gupta #define get_tsp_pstate(state)	((state >> TSP_PSTATE_SHIFT) & TSP_PSTATE_MASK)
273ee8a164SAchin Gupta #define clr_tsp_pstate(state)	(state &= ~(TSP_PSTATE_MASK \
283ee8a164SAchin Gupta 					    << TSP_PSTATE_SHIFT))
293ee8a164SAchin Gupta #define set_tsp_pstate(st, pst)	do {					       \
303ee8a164SAchin Gupta 					clr_tsp_pstate(st);		       \
313ee8a164SAchin Gupta 					st |= (pst & TSP_PSTATE_MASK) <<       \
323ee8a164SAchin Gupta 						TSP_PSTATE_SHIFT;	       \
333ee8a164SAchin Gupta 				} while (0);
343ee8a164SAchin Gupta 
353ee8a164SAchin Gupta 
363ee8a164SAchin Gupta /*
3716292f54SDavid Cunado  * This flag is used by the TSPD to determine if the TSP is servicing a yielding
383ee8a164SAchin Gupta  * SMC request prior to programming the next entry into the TSP e.g. if TSP
393ee8a164SAchin Gupta  * execution is preempted by a non-secure interrupt and handed control to the
403ee8a164SAchin Gupta  * normal world. If another request which is distinct from what the TSP was
413ee8a164SAchin Gupta  * previously doing arrives, then this flag will be help the TSPD to either
423ee8a164SAchin Gupta  * reject the new request or service it while ensuring that the previous context
433ee8a164SAchin Gupta  * is not corrupted.
443ee8a164SAchin Gupta  */
4516292f54SDavid Cunado #define YIELD_SMC_ACTIVE_FLAG_SHIFT	2
4616292f54SDavid Cunado #define YIELD_SMC_ACTIVE_FLAG_MASK	1
4716292f54SDavid Cunado #define get_yield_smc_active_flag(state)				\
4816292f54SDavid Cunado 				((state >> YIELD_SMC_ACTIVE_FLAG_SHIFT) \
4916292f54SDavid Cunado 				& YIELD_SMC_ACTIVE_FLAG_MASK)
5016292f54SDavid Cunado #define set_yield_smc_active_flag(state)	(state |=		\
5116292f54SDavid Cunado 					1 << YIELD_SMC_ACTIVE_FLAG_SHIFT)
5216292f54SDavid Cunado #define clr_yield_smc_active_flag(state)	(state &=		\
5316292f54SDavid Cunado 					~(YIELD_SMC_ACTIVE_FLAG_MASK	\
5416292f54SDavid Cunado 					<< YIELD_SMC_ACTIVE_FLAG_SHIFT))
55375f538aSAchin Gupta 
56375f538aSAchin Gupta /*******************************************************************************
57375f538aSAchin Gupta  * Secure Payload execution state information i.e. aarch32 or aarch64
58375f538aSAchin Gupta  ******************************************************************************/
59375f538aSAchin Gupta #define TSP_AARCH32		MODE_RW_32
60375f538aSAchin Gupta #define TSP_AARCH64		MODE_RW_64
61375f538aSAchin Gupta 
62375f538aSAchin Gupta /*******************************************************************************
63375f538aSAchin Gupta  * The SPD should know the type of Secure Payload.
64375f538aSAchin Gupta  ******************************************************************************/
65375f538aSAchin Gupta #define TSP_TYPE_UP		PSCI_TOS_NOT_UP_MIG_CAP
66375f538aSAchin Gupta #define TSP_TYPE_UPM		PSCI_TOS_UP_MIG_CAP
67375f538aSAchin Gupta #define TSP_TYPE_MP		PSCI_TOS_NOT_PRESENT_MP
68375f538aSAchin Gupta 
69375f538aSAchin Gupta /*******************************************************************************
70375f538aSAchin Gupta  * Secure Payload migrate type information as known to the SPD. We assume that
71375f538aSAchin Gupta  * the SPD is dealing with an MP Secure Payload.
72375f538aSAchin Gupta  ******************************************************************************/
73375f538aSAchin Gupta #define TSP_MIGRATE_INFO		TSP_TYPE_MP
74375f538aSAchin Gupta 
75375f538aSAchin Gupta /*******************************************************************************
76375f538aSAchin Gupta  * Number of cpus that the present on this platform. TODO: Rely on a topology
77375f538aSAchin Gupta  * tree to determine this in the future to avoid assumptions about mpidr
78375f538aSAchin Gupta  * allocation
79375f538aSAchin Gupta  ******************************************************************************/
80375f538aSAchin Gupta #define TSPD_CORE_COUNT		PLATFORM_CORE_COUNT
81375f538aSAchin Gupta 
82375f538aSAchin Gupta /*******************************************************************************
83375f538aSAchin Gupta  * Constants that allow assembler code to preserve callee-saved registers of the
84375f538aSAchin Gupta  * C runtime context while performing a security state switch.
85375f538aSAchin Gupta  ******************************************************************************/
86375f538aSAchin Gupta #define TSPD_C_RT_CTX_X19		0x0
87375f538aSAchin Gupta #define TSPD_C_RT_CTX_X20		0x8
88375f538aSAchin Gupta #define TSPD_C_RT_CTX_X21		0x10
89375f538aSAchin Gupta #define TSPD_C_RT_CTX_X22		0x18
90375f538aSAchin Gupta #define TSPD_C_RT_CTX_X23		0x20
91375f538aSAchin Gupta #define TSPD_C_RT_CTX_X24		0x28
92375f538aSAchin Gupta #define TSPD_C_RT_CTX_X25		0x30
93375f538aSAchin Gupta #define TSPD_C_RT_CTX_X26		0x38
94375f538aSAchin Gupta #define TSPD_C_RT_CTX_X27		0x40
95375f538aSAchin Gupta #define TSPD_C_RT_CTX_X28		0x48
96375f538aSAchin Gupta #define TSPD_C_RT_CTX_X29		0x50
97375f538aSAchin Gupta #define TSPD_C_RT_CTX_X30		0x58
98375f538aSAchin Gupta #define TSPD_C_RT_CTX_SIZE		0x60
99375f538aSAchin Gupta #define TSPD_C_RT_CTX_ENTRIES		(TSPD_C_RT_CTX_SIZE >> DWORD_SHIFT)
100375f538aSAchin Gupta 
101f4f1ae77SSoby Mathew /*******************************************************************************
102f4f1ae77SSoby Mathew  * Constants that allow assembler code to preserve caller-saved registers of the
103f4f1ae77SSoby Mathew  * SP context while performing a TSP preemption.
104f4f1ae77SSoby Mathew  * Note: These offsets have to match with the offsets for the corresponding
105f4f1ae77SSoby Mathew  * registers in cpu_context as we are using memcpy to copy the values from
106f4f1ae77SSoby Mathew  * cpu_context to sp_ctx.
107f4f1ae77SSoby Mathew  ******************************************************************************/
108f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X0		0x0
109f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X1		0x8
110f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X2		0x10
111f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X3		0x18
112f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X4		0x20
113f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X5		0x28
114f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X6		0x30
115f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X7		0x38
116f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X8		0x40
117f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X9		0x48
118f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X10		0x50
119f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X11		0x58
120f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X12		0x60
121f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X13		0x68
122f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X14		0x70
123f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X15		0x78
124f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X16		0x80
125f4f1ae77SSoby Mathew #define TSPD_SP_CTX_X17		0x88
126f4f1ae77SSoby Mathew #define TSPD_SP_CTX_SIZE	0x90
127f4f1ae77SSoby Mathew #define TSPD_SP_CTX_ENTRIES		(TSPD_SP_CTX_SIZE >> DWORD_SHIFT)
128f4f1ae77SSoby Mathew 
129d5dfdeb6SJulius Werner #ifndef __ASSEMBLER__
130375f538aSAchin Gupta 
13197043ac9SDan Handley #include <stdint.h>
13297043ac9SDan Handley 
13309d40e0eSAntonio Nino Diaz #include <lib/cassert.h>
13409d40e0eSAntonio Nino Diaz 
135239b04faSSoby Mathew /*
136239b04faSSoby Mathew  * The number of arguments to save during a SMC call for TSP.
137239b04faSSoby Mathew  * Currently only x1 and x2 are used by TSP.
138239b04faSSoby Mathew  */
139239b04faSSoby Mathew #define TSP_NUM_ARGS	0x2
140239b04faSSoby Mathew 
141375f538aSAchin Gupta /* AArch64 callee saved general purpose register context structure. */
142375f538aSAchin Gupta DEFINE_REG_STRUCT(c_rt_regs, TSPD_C_RT_CTX_ENTRIES);
143375f538aSAchin Gupta 
144375f538aSAchin Gupta /*
145375f538aSAchin Gupta  * Compile time assertion to ensure that both the compiler and linker
146375f538aSAchin Gupta  * have the same double word aligned view of the size of the C runtime
147375f538aSAchin Gupta  * register context.
148375f538aSAchin Gupta  */
1499a90d720SElyes Haouas CASSERT(TSPD_C_RT_CTX_SIZE == sizeof(c_rt_regs_t),
150375f538aSAchin Gupta 	assert_spd_c_rt_regs_size_mismatch);
151375f538aSAchin Gupta 
152f4f1ae77SSoby Mathew /* SEL1 Secure payload (SP) caller saved register context structure. */
153f4f1ae77SSoby Mathew DEFINE_REG_STRUCT(sp_ctx_regs, TSPD_SP_CTX_ENTRIES);
154f4f1ae77SSoby Mathew 
155f4f1ae77SSoby Mathew /*
156f4f1ae77SSoby Mathew  * Compile time assertion to ensure that both the compiler and linker
157f4f1ae77SSoby Mathew  * have the same double word aligned view of the size of the C runtime
158f4f1ae77SSoby Mathew  * register context.
159f4f1ae77SSoby Mathew  */
1609a90d720SElyes Haouas CASSERT(TSPD_SP_CTX_SIZE == sizeof(sp_ctx_regs_t),
161f4f1ae77SSoby Mathew 	assert_spd_sp_regs_size_mismatch);
162f4f1ae77SSoby Mathew 
163375f538aSAchin Gupta /*******************************************************************************
164375f538aSAchin Gupta  * Structure which helps the SPD to maintain the per-cpu state of the SP.
16502446137SSoby Mathew  * 'saved_spsr_el3' - temporary copy to allow S-EL1 interrupt handling when
16602446137SSoby Mathew  *                    the TSP has been preempted.
16702446137SSoby Mathew  * 'saved_elr_el3'  - temporary copy to allow S-EL1 interrupt handling when
16802446137SSoby Mathew  *                    the TSP has been preempted.
169375f538aSAchin Gupta  * 'state'          - collection of flags to track SP state e.g. on/off
170375f538aSAchin Gupta  * 'mpidr'          - mpidr to associate a context with a cpu
171b44a4435SAchin Gupta  * 'c_rt_ctx'       - stack address to restore C runtime context from after
172b44a4435SAchin Gupta  *                    returning from a synchronous entry into the SP.
173375f538aSAchin Gupta  * 'cpu_ctx'        - space to maintain SP architectural state
174239b04faSSoby Mathew  * 'saved_tsp_args' - space to store arguments for TSP arithmetic operations
175239b04faSSoby Mathew  *                    which will queried using the TSP_GET_ARGS SMC by TSP.
176f4f1ae77SSoby Mathew  * 'sp_ctx'         - space to save the SEL1 Secure Payload(SP) caller saved
177f4f1ae77SSoby Mathew  *                    register context after it has been preempted by an EL3
178f4f1ae77SSoby Mathew  *                    routed NS interrupt and when a Secure Interrupt is taken
179f4f1ae77SSoby Mathew  *                    to SP.
180375f538aSAchin Gupta  ******************************************************************************/
181fb037bfbSDan Handley typedef struct tsp_context {
182b44a4435SAchin Gupta 	uint64_t saved_elr_el3;
183b44a4435SAchin Gupta 	uint32_t saved_spsr_el3;
184375f538aSAchin Gupta 	uint32_t state;
185375f538aSAchin Gupta 	uint64_t mpidr;
186375f538aSAchin Gupta 	uint64_t c_rt_ctx;
187fb037bfbSDan Handley 	cpu_context_t cpu_ctx;
188239b04faSSoby Mathew 	uint64_t saved_tsp_args[TSP_NUM_ARGS];
18902446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT
190f4f1ae77SSoby Mathew 	sp_ctx_regs_t sp_ctx;
19151bb1d73SMadhukar Pappireddy 	bool preempted_by_sel1_intr;
192f4f1ae77SSoby Mathew #endif
193fb037bfbSDan Handley } tsp_context_t;
194375f538aSAchin Gupta 
195239b04faSSoby Mathew /* Helper macros to store and retrieve tsp args from tsp_context */
1967c934242SDaniel Boulby #define store_tsp_args(_tsp_ctx, _x1, _x2)		do {\
1977c934242SDaniel Boulby 				_tsp_ctx->saved_tsp_args[0] = _x1;\
1987c934242SDaniel Boulby 				_tsp_ctx->saved_tsp_args[1] = _x2;\
199239b04faSSoby Mathew 			} while (0)
200239b04faSSoby Mathew 
2017c934242SDaniel Boulby #define get_tsp_args(_tsp_ctx, _x1, _x2)	do {\
2027c934242SDaniel Boulby 				_x1 = _tsp_ctx->saved_tsp_args[0];\
2037c934242SDaniel Boulby 				_x2 = _tsp_ctx->saved_tsp_args[1];\
204239b04faSSoby Mathew 			} while (0)
205239b04faSSoby Mathew 
2067f366605SJeenu Viswambharan /* TSPD power management handlers */
207fb037bfbSDan Handley extern const spd_pm_ops_t tspd_pm;
2087f366605SJeenu Viswambharan 
209375f538aSAchin Gupta /*******************************************************************************
210375f538aSAchin Gupta  * Function & Data prototypes
211375f538aSAchin Gupta  ******************************************************************************/
212c6bc0710SDan Handley uint64_t tspd_enter_sp(uint64_t *c_rt_ctx);
213c6bc0710SDan Handley void __dead2 tspd_exit_sp(uint64_t c_rt_ctx, uint64_t ret);
214c6bc0710SDan Handley uint64_t tspd_synchronous_sp_entry(tsp_context_t *tsp_ctx);
215c6bc0710SDan Handley void __dead2 tspd_synchronous_sp_exit(tsp_context_t *tsp_ctx, uint64_t ret);
216a27163bcSRoberto Vargas void tspd_init_tsp_ep_state(struct entry_point_info *tsp_entry_point,
217375f538aSAchin Gupta 				uint32_t rw,
21850e27dadSVikram Kanigiri 				uint64_t pc,
219fb037bfbSDan Handley 				tsp_context_t *tsp_ctx);
2203df6012aSDouglas Raillard int tspd_abort_preempted_smc(tsp_context_t *tsp_ctx);
22150e27dadSVikram Kanigiri 
2221a29f938SRoberto Vargas uint64_t tspd_handle_sp_preemption(void *handle);
2231a29f938SRoberto Vargas 
224fb037bfbSDan Handley extern tsp_context_t tspd_sp_context[TSPD_CORE_COUNT];
225a27163bcSRoberto Vargas extern tsp_vectors_t *tsp_vectors;
226d5dfdeb6SJulius Werner #endif /*__ASSEMBLER__*/
227375f538aSAchin Gupta 
228c3cf06f1SAntonio Nino Diaz #endif /* TSPD_PRIVATE_H */
229