1 /*
2  * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of ARM nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific
16  * prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef __OPTEED_PRIVATE_H__
32 #define __OPTEED_PRIVATE_H__
33 
34 #include <arch.h>
35 #include <context.h>
36 #include <interrupt_mgmt.h>
37 #include <platform_def.h>
38 #include <psci.h>
39 
40 /*******************************************************************************
41  * OPTEE PM state information e.g. OPTEE is suspended, uninitialised etc
42  * and macros to access the state information in the per-cpu 'state' flags
43  ******************************************************************************/
44 #define OPTEE_PSTATE_OFF		0
45 #define OPTEE_PSTATE_ON			1
46 #define OPTEE_PSTATE_SUSPEND		2
47 #define OPTEE_PSTATE_SHIFT		0
48 #define OPTEE_PSTATE_MASK		0x3
49 #define get_optee_pstate(state)	((state >> OPTEE_PSTATE_SHIFT) & \
50 				 OPTEE_PSTATE_MASK)
51 #define clr_optee_pstate(state)	(state &= ~(OPTEE_PSTATE_MASK \
52 					    << OPTEE_PSTATE_SHIFT))
53 #define set_optee_pstate(st, pst) do {					       \
54 					clr_optee_pstate(st);		       \
55 					st |= (pst & OPTEE_PSTATE_MASK) <<     \
56 						OPTEE_PSTATE_SHIFT;	       \
57 				} while (0)
58 
59 
60 /*******************************************************************************
61  * OPTEE execution state information i.e. aarch32 or aarch64
62  ******************************************************************************/
63 #define OPTEE_AARCH32		MODE_RW_32
64 #define OPTEE_AARCH64		MODE_RW_64
65 
66 /*******************************************************************************
67  * The OPTEED should know the type of OPTEE
68  ******************************************************************************/
69 #define OPTEE_TYPE_UP		PSCI_TOS_NOT_UP_MIG_CAP
70 #define OPTEE_TYPE_UPM		PSCI_TOS_UP_MIG_CAP
71 #define OPTEE_TYPE_MP		PSCI_TOS_NOT_PRESENT_MP
72 
73 /*******************************************************************************
74  * OPTEE migrate type information as known to the OPTEED. We assume that
75  * the OPTEED is dealing with an MP Secure Payload.
76  ******************************************************************************/
77 #define OPTEE_MIGRATE_INFO		OPTEE_TYPE_MP
78 
79 /*******************************************************************************
80  * Number of cpus that the present on this platform. TODO: Rely on a topology
81  * tree to determine this in the future to avoid assumptions about mpidr
82  * allocation
83  ******************************************************************************/
84 #define OPTEED_CORE_COUNT		PLATFORM_CORE_COUNT
85 
86 /*******************************************************************************
87  * Constants that allow assembler code to preserve callee-saved registers of the
88  * C runtime context while performing a security state switch.
89  ******************************************************************************/
90 #define OPTEED_C_RT_CTX_X19		0x0
91 #define OPTEED_C_RT_CTX_X20		0x8
92 #define OPTEED_C_RT_CTX_X21		0x10
93 #define OPTEED_C_RT_CTX_X22		0x18
94 #define OPTEED_C_RT_CTX_X23		0x20
95 #define OPTEED_C_RT_CTX_X24		0x28
96 #define OPTEED_C_RT_CTX_X25		0x30
97 #define OPTEED_C_RT_CTX_X26		0x38
98 #define OPTEED_C_RT_CTX_X27		0x40
99 #define OPTEED_C_RT_CTX_X28		0x48
100 #define OPTEED_C_RT_CTX_X29		0x50
101 #define OPTEED_C_RT_CTX_X30		0x58
102 #define OPTEED_C_RT_CTX_SIZE		0x60
103 #define OPTEED_C_RT_CTX_ENTRIES		(OPTEED_C_RT_CTX_SIZE >> DWORD_SHIFT)
104 
105 #ifndef __ASSEMBLY__
106 
107 #include <cassert.h>
108 #include <stdint.h>
109 
110 typedef uint32_t optee_vector_isn_t;
111 
112 typedef struct optee_vectors {
113 	optee_vector_isn_t std_smc_entry;
114 	optee_vector_isn_t fast_smc_entry;
115 	optee_vector_isn_t cpu_on_entry;
116 	optee_vector_isn_t cpu_off_entry;
117 	optee_vector_isn_t cpu_resume_entry;
118 	optee_vector_isn_t cpu_suspend_entry;
119 	optee_vector_isn_t fiq_entry;
120 	optee_vector_isn_t system_off_entry;
121 	optee_vector_isn_t system_reset_entry;
122 } optee_vectors_t;
123 
124 /*
125  * The number of arguments to save during a SMC call for OPTEE.
126  * Currently only x1 and x2 are used by OPTEE.
127  */
128 #define OPTEE_NUM_ARGS	0x2
129 
130 /* AArch64 callee saved general purpose register context structure. */
131 DEFINE_REG_STRUCT(c_rt_regs, OPTEED_C_RT_CTX_ENTRIES);
132 
133 /*
134  * Compile time assertion to ensure that both the compiler and linker
135  * have the same double word aligned view of the size of the C runtime
136  * register context.
137  */
138 CASSERT(OPTEED_C_RT_CTX_SIZE == sizeof(c_rt_regs_t),	\
139 	assert_spd_c_rt_regs_size_mismatch);
140 
141 /*******************************************************************************
142  * Structure which helps the OPTEED to maintain the per-cpu state of OPTEE.
143  * 'state'          - collection of flags to track OPTEE state e.g. on/off
144  * 'mpidr'          - mpidr to associate a context with a cpu
145  * 'c_rt_ctx'       - stack address to restore C runtime context from after
146  *                    returning from a synchronous entry into OPTEE.
147  * 'cpu_ctx'        - space to maintain OPTEE architectural state
148  ******************************************************************************/
149 typedef struct optee_context {
150 	uint32_t state;
151 	uint64_t mpidr;
152 	uint64_t c_rt_ctx;
153 	cpu_context_t cpu_ctx;
154 } optee_context_t;
155 
156 /* OPTEED power management handlers */
157 extern const spd_pm_ops_t opteed_pm;
158 
159 /*******************************************************************************
160  * Forward declarations
161  ******************************************************************************/
162 struct optee_vectors;
163 
164 /*******************************************************************************
165  * Function & Data prototypes
166  ******************************************************************************/
167 uint64_t opteed_enter_sp(uint64_t *c_rt_ctx);
168 void __dead2 opteed_exit_sp(uint64_t c_rt_ctx, uint64_t ret);
169 uint64_t opteed_synchronous_sp_entry(optee_context_t *optee_ctx);
170 void __dead2 opteed_synchronous_sp_exit(optee_context_t *optee_ctx, uint64_t ret);
171 void opteed_init_optee_ep_state(struct entry_point_info *optee_ep,
172 				uint32_t rw, uint64_t pc,
173 				uint64_t paged_part, uint64_t mem_limit,
174 				optee_context_t *optee_ctx);
175 
176 extern optee_context_t opteed_sp_context[OPTEED_CORE_COUNT];
177 extern uint32_t opteed_rw;
178 extern struct optee_vectors *optee_vectors;
179 #endif /*__ASSEMBLY__*/
180 
181 #endif /* __OPTEED_PRIVATE_H__ */
182