1 /*
2  * Copyright (c) 2013-2023, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef OPTEED_PRIVATE_H
8 #define OPTEED_PRIVATE_H
9 
10 #include <platform_def.h>
11 
12 #include <arch.h>
13 #include <bl31/interrupt_mgmt.h>
14 #include <context.h>
15 #include <lib/psci/psci.h>
16 
17 /*******************************************************************************
18  * OPTEE PM state information e.g. OPTEE is suspended, uninitialised etc
19  * and macros to access the state information in the per-cpu 'state' flags
20  ******************************************************************************/
21 #define OPTEE_PSTATE_OFF		1
22 #define OPTEE_PSTATE_ON			2
23 #define OPTEE_PSTATE_SUSPEND		3
24 #define OPTEE_PSTATE_UNKNOWN		0
25 #define OPTEE_PSTATE_SHIFT		0
26 #define OPTEE_PSTATE_MASK		0x3
27 #define get_optee_pstate(state)	((state >> OPTEE_PSTATE_SHIFT) & \
28 				 OPTEE_PSTATE_MASK)
29 #define clr_optee_pstate(state)	(state &= ~(OPTEE_PSTATE_MASK \
30 					    << OPTEE_PSTATE_SHIFT))
31 #define set_optee_pstate(st, pst) do {					       \
32 					clr_optee_pstate(st);		       \
33 					st |= (pst & OPTEE_PSTATE_MASK) <<     \
34 						OPTEE_PSTATE_SHIFT;	       \
35 				} while (0)
36 
37 
38 /*******************************************************************************
39  * OPTEE execution state information i.e. aarch32 or aarch64
40  ******************************************************************************/
41 #define OPTEE_AARCH32		MODE_RW_32
42 #define OPTEE_AARCH64		MODE_RW_64
43 
44 /*******************************************************************************
45  * The OPTEED should know the type of OPTEE
46  ******************************************************************************/
47 #define OPTEE_TYPE_UP		PSCI_TOS_NOT_UP_MIG_CAP
48 #define OPTEE_TYPE_UPM		PSCI_TOS_UP_MIG_CAP
49 #define OPTEE_TYPE_MP		PSCI_TOS_NOT_PRESENT_MP
50 
51 /*******************************************************************************
52  * OPTEE migrate type information as known to the OPTEED. We assume that
53  * the OPTEED is dealing with an MP Secure Payload.
54  ******************************************************************************/
55 #define OPTEE_MIGRATE_INFO		OPTEE_TYPE_MP
56 
57 /*******************************************************************************
58  * Number of cpus that the present on this platform. TODO: Rely on a topology
59  * tree to determine this in the future to avoid assumptions about mpidr
60  * allocation
61  ******************************************************************************/
62 #define OPTEED_CORE_COUNT		PLATFORM_CORE_COUNT
63 
64 /*******************************************************************************
65  * Constants that allow assembler code to preserve callee-saved registers of the
66  * C runtime context while performing a security state switch.
67  ******************************************************************************/
68 #define OPTEED_C_RT_CTX_X19		0x0
69 #define OPTEED_C_RT_CTX_X20		0x8
70 #define OPTEED_C_RT_CTX_X21		0x10
71 #define OPTEED_C_RT_CTX_X22		0x18
72 #define OPTEED_C_RT_CTX_X23		0x20
73 #define OPTEED_C_RT_CTX_X24		0x28
74 #define OPTEED_C_RT_CTX_X25		0x30
75 #define OPTEED_C_RT_CTX_X26		0x38
76 #define OPTEED_C_RT_CTX_X27		0x40
77 #define OPTEED_C_RT_CTX_X28		0x48
78 #define OPTEED_C_RT_CTX_X29		0x50
79 #define OPTEED_C_RT_CTX_X30		0x58
80 #define OPTEED_C_RT_CTX_SIZE		0x60
81 #define OPTEED_C_RT_CTX_ENTRIES		(OPTEED_C_RT_CTX_SIZE >> DWORD_SHIFT)
82 
83 #ifndef __ASSEMBLER__
84 
85 #include <stdint.h>
86 
87 #include <lib/cassert.h>
88 
89 typedef uint32_t optee_vector_isn_t;
90 
91 typedef struct optee_vectors {
92 	optee_vector_isn_t yield_smc_entry;
93 	optee_vector_isn_t fast_smc_entry;
94 	optee_vector_isn_t cpu_on_entry;
95 	optee_vector_isn_t cpu_off_entry;
96 	optee_vector_isn_t cpu_resume_entry;
97 	optee_vector_isn_t cpu_suspend_entry;
98 	optee_vector_isn_t fiq_entry;
99 	optee_vector_isn_t system_off_entry;
100 	optee_vector_isn_t system_reset_entry;
101 } optee_vectors_t;
102 
103 /*
104  * The number of arguments to save during a SMC call for OPTEE.
105  * Currently only x1 and x2 are used by OPTEE.
106  */
107 #define OPTEE_NUM_ARGS	0x2
108 
109 /* AArch64 callee saved general purpose register context structure. */
110 DEFINE_REG_STRUCT(c_rt_regs, OPTEED_C_RT_CTX_ENTRIES);
111 
112 /*
113  * Compile time assertion to ensure that both the compiler and linker
114  * have the same double word aligned view of the size of the C runtime
115  * register context.
116  */
117 CASSERT(OPTEED_C_RT_CTX_SIZE == sizeof(c_rt_regs_t),
118 	assert_spd_c_rt_regs_size_mismatch);
119 
120 /*******************************************************************************
121  * Structure which helps the OPTEED to maintain the per-cpu state of OPTEE.
122  * 'state'          - collection of flags to track OPTEE state e.g. on/off
123  * 'mpidr'          - mpidr to associate a context with a cpu
124  * 'c_rt_ctx'       - stack address to restore C runtime context from after
125  *                    returning from a synchronous entry into OPTEE.
126  * 'cpu_ctx'        - space to maintain OPTEE architectural state
127  ******************************************************************************/
128 typedef struct optee_context {
129 	uint32_t state;
130 	uint64_t mpidr;
131 	uint64_t c_rt_ctx;
132 	cpu_context_t cpu_ctx;
133 } optee_context_t;
134 
135 /* OPTEED power management handlers */
136 extern const spd_pm_ops_t opteed_pm;
137 
138 /*******************************************************************************
139  * Forward declarations
140  ******************************************************************************/
141 struct optee_vectors;
142 
143 /*******************************************************************************
144  * Function & Data prototypes
145  ******************************************************************************/
146 uint64_t opteed_enter_sp(uint64_t *c_rt_ctx);
147 void __dead2 opteed_exit_sp(uint64_t c_rt_ctx, uint64_t ret);
148 uint64_t opteed_synchronous_sp_entry(optee_context_t *optee_ctx);
149 void __dead2 opteed_synchronous_sp_exit(optee_context_t *optee_ctx, uint64_t ret);
150 void opteed_init_optee_ep_state(struct entry_point_info *optee_entry_point,
151 				uint32_t rw,
152 				uint64_t pc,
153 				uint64_t pageable_part,
154 				uint64_t mem_limit,
155 				uint64_t dt_addr,
156 				optee_context_t *optee_ctx);
157 void opteed_cpu_on_finish_handler(u_register_t unused);
158 
159 extern optee_context_t opteed_sp_context[OPTEED_CORE_COUNT];
160 extern uint32_t opteed_rw;
161 extern struct optee_vectors *optee_vector_table;
162 #endif /*__ASSEMBLER__*/
163 
164 #endif /* OPTEED_PRIVATE_H */
165