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