xref: /rk3399_ARM-atf/services/spd/tlkd/tlkd_private.h (revision 51faada71a219a8b94cd8d8e423f0f22e9da4d8f)
1 /*
2  * Copyright (c) 2015, 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 __TLKD_PRIVATE_H__
32 #define __TLKD_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  * This flag is used by the TLKD to determine if the SP is servicing a standard
42  * SMC request prior to programming the next entry into the SP e.g. if SP
43  * execution is preempted by a non-secure interrupt and handed control to the
44  * normal world. If another request which is distinct from what the SP was
45  * previously doing arrives, then this flag will be help the TLKD to either
46  * reject the new request or service it while ensuring that the previous context
47  * is not corrupted.
48  */
49 #define STD_SMC_ACTIVE_FLAG_SHIFT	2
50 #define STD_SMC_ACTIVE_FLAG_MASK	1
51 #define get_std_smc_active_flag(state)	(((state) >> STD_SMC_ACTIVE_FLAG_SHIFT) \
52 					 & STD_SMC_ACTIVE_FLAG_MASK)
53 #define set_std_smc_active_flag(state)	((state) |=                           \
54 					 (1 << STD_SMC_ACTIVE_FLAG_SHIFT))
55 #define clr_std_smc_active_flag(state)	((state) &=                           \
56 					 ~(STD_SMC_ACTIVE_FLAG_MASK           \
57 					   << STD_SMC_ACTIVE_FLAG_SHIFT))
58 
59 /*******************************************************************************
60  * Translate virtual address received from the NS world
61  ******************************************************************************/
62 #define TLK_TRANSLATE_NS_VADDR		4
63 
64 /*******************************************************************************
65  * Secure Payload execution state information i.e. aarch32 or aarch64
66  ******************************************************************************/
67 #define SP_AARCH32		MODE_RW_32
68 #define SP_AARCH64		MODE_RW_64
69 
70 /*******************************************************************************
71  * Number of cpus that the present on this platform. TODO: Rely on a topology
72  * tree to determine this in the future to avoid assumptions about mpidr
73  * allocation
74  ******************************************************************************/
75 #define TLKD_CORE_COUNT		PLATFORM_CORE_COUNT
76 
77 /*******************************************************************************
78  * Constants that allow assembler code to preserve callee-saved registers of the
79  * C runtime context while performing a security state switch.
80  ******************************************************************************/
81 #define TLKD_C_RT_CTX_X19		0x0
82 #define TLKD_C_RT_CTX_X20		0x8
83 #define TLKD_C_RT_CTX_X21		0x10
84 #define TLKD_C_RT_CTX_X22		0x18
85 #define TLKD_C_RT_CTX_X23		0x20
86 #define TLKD_C_RT_CTX_X24		0x28
87 #define TLKD_C_RT_CTX_X25		0x30
88 #define TLKD_C_RT_CTX_X26		0x38
89 #define TLKD_C_RT_CTX_X27		0x40
90 #define TLKD_C_RT_CTX_X28		0x48
91 #define TLKD_C_RT_CTX_X29		0x50
92 #define TLKD_C_RT_CTX_X30		0x58
93 #define TLKD_C_RT_CTX_SIZE		0x60
94 #define TLKD_C_RT_CTX_ENTRIES		(TLKD_C_RT_CTX_SIZE >> DWORD_SHIFT)
95 
96 #ifndef __ASSEMBLY__
97 
98 #include <cassert.h>
99 #include <stdint.h>
100 
101 /* AArch64 callee saved general purpose register context structure. */
102 DEFINE_REG_STRUCT(c_rt_regs, TLKD_C_RT_CTX_ENTRIES);
103 
104 /*
105  * Compile time assertion to ensure that both the compiler and linker
106  * have the same double word aligned view of the size of the C runtime
107  * register context.
108  */
109 CASSERT(TLKD_C_RT_CTX_SIZE == sizeof(c_rt_regs_t),	\
110 	assert_tlkd_c_rt_regs_size_mismatch);
111 
112 /*******************************************************************************
113  * Structure which helps the SPD to maintain the per-cpu state of the SP.
114  * 'state'          - collection of flags to track SP state e.g. on/off
115  * 'mpidr'          - mpidr to associate a context with a cpu
116  * 'c_rt_ctx'       - stack address to restore C runtime context from after
117  *                    returning from a synchronous entry into the SP.
118  * 'cpu_ctx'        - space to maintain SP architectural state
119  * 'saved_tsp_args' - space to store arguments for TSP arithmetic operations
120  *                    which will queried using the TSP_GET_ARGS SMC by TSP.
121  ******************************************************************************/
122 typedef struct tlk_context {
123 	uint32_t state;
124 	uint64_t mpidr;
125 	uint64_t c_rt_ctx;
126 	cpu_context_t cpu_ctx;
127 } tlk_context_t;
128 
129 /*******************************************************************************
130  * Function & Data prototypes
131  ******************************************************************************/
132 uint64_t tlkd_va_translate(uintptr_t va, int type);
133 uint64_t tlkd_enter_sp(uint64_t *c_rt_ctx);
134 void __dead2 tlkd_exit_sp(uint64_t c_rt_ctx, uint64_t ret);
135 uint64_t tlkd_synchronous_sp_entry(tlk_context_t *tlk_ctx);
136 void __dead2 tlkd_synchronous_sp_exit(tlk_context_t *tlk_ctx,
137 			uint64_t ret);
138 void tlkd_init_tlk_ep_state(struct entry_point_info *tlk_entry_point,
139 				uint32_t rw,
140 				uint64_t pc,
141 				tlk_context_t *tlk_ctx);
142 
143 #endif /*__ASSEMBLY__*/
144 
145 #endif /* __TLKD_PRIVATE_H__ */
146