xref: /optee_os/core/arch/arm/include/kernel/stmm_sp.h (revision 1c0c7c06563e9be0ebc55b47cdb71422b02906a6)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2019-2020, Linaro Limited
4  * Copyright (c) 2020, Arm Limited.
5  */
6 
7 #ifndef __KERNEL_STMM_SP_H
8 #define __KERNEL_STMM_SP_H
9 
10 #include <assert.h>
11 #include <config.h>
12 #include <kernel/tee_ta_manager.h>
13 #include <kernel/thread.h>
14 #include <kernel/user_mode_ctx_struct.h>
15 #include <types_ext.h>
16 #include <util.h>
17 
18 #define STMM_RET_INVALID_PARAM	        -2
19 /*
20  * Used for EDK2 StMM communication. Since StMM can be launched on an arbitrary
21  * address it uses these 2 syscalls to define the memory attributes for the
22  * data and code segments after dispatching the binaries.
23  *
24  * FFA_SVC_MEMORY_ATTRIBUTES_SET_64/FFA_SVC_MEMORY_ATTRIBUTES_SET_32:
25  *  - x4: base address
26  *  - x5: number of pages
27  *  - x6: attributes of the remapping (described above)
28  *
29  * FFA_SVC_MEMORY_ATTRIBUTES_GET_32/FFA_SVC_MEMORY_ATTRIBUTES_GET_64:
30  *  - x4: base address, currently only a single page is requested.
31  */
32 #define FFA_SVC_MEMORY_ATTRIBUTES_GET_64	UINT32_C(0xC4000064)
33 #define FFA_SVC_MEMORY_ATTRIBUTES_SET_64	UINT32_C(0xC4000065)
34 
35 #define FFA_SVC_MEMORY_ATTRIBUTES_GET_32	UINT32_C(0x84000064)
36 #define FFA_SVC_MEMORY_ATTRIBUTES_SET_32	UINT32_C(0x84000065)
37 
38 /*
39  * We need to define the RPMB IDs formally, since the plan is
40  * for them to be included in the FFA spec (for SP-to-SP future communication).
41  * This is fine for now as it represents the internal contract between the
42  * EDK2 RPMB driver and Secure Partition
43  *
44  * FFA_SVC_RPMB_WRITE/FFA_SVC_RPMB_WRITE_32:
45  *  - x4: virtual address of the buffer to write in the device
46  *  - x5: buffer byte length
47  *  - x6: byte offset in the device
48  * FFA_SVC_RPMB_READ/FFA_SVC_RPMB_READ_32:
49  *  - x4: virtual address of the buffer were RPMB contents are copied
50  *  - x5: buffer byte length to read
51  *  - x6: byte offset in the device
52  */
53 #define FFA_SVC_RPMB_READ		UINT32_C(0xC4000066)
54 #define FFA_SVC_RPMB_WRITE		UINT32_C(0xC4000067)
55 
56 #define FFA_SVC_RPMB_READ_32		UINT32_C(0x84000066)
57 #define FFA_SVC_RPMB_WRITE_32		UINT32_C(0x84000067)
58 
59 /* Param header types */
60 #define STMM_PARAM_EP			UINT8_C(0x01)
61 #define STMM_PARAM_IMAGE_BINARY		UINT8_C(0x02)
62 #define STMM_PARAM_BL31			UINT8_C(0x03)
63 #define STMM_PARAM_BL_LOAD_INFO		UINT8_C(0x04)
64 #define STMM_PARAM_BL_PARAMS		UINT8_C(0x05)
65 #define STMM_PARAM_PSCI_LIB_ARGS	UINT8_C(0x06)
66 #define STMM_PARAM_SP_IMAGE_BOOT_INFO	UINT8_C(0x07)
67 
68 /* Param header version */
69 #define STMM_PARAM_VERSION_1		UINT8_C(0x01)
70 #define STMM_PARAM_VERSION_2		UINT8_C(0x02)
71 
72 /*
73  * This structure provides information on format used to describe
74  * secure partition invocation parameters.
75  */
76 struct stmm_param_header {
77 	uint8_t type;		/* type of the structure */
78 	uint8_t version;	/* version of this structure */
79 	uint16_t size;		/* size of this structure in bytes */
80 	uint32_t attr;		/* attributes: unused bits SBZ */
81 };
82 
83 /*
84  * Flags used by the stmm_mp_info structure to describe the
85  * characteristics of a cpu. Only a single flag is defined at the moment to
86  * indicate the primary cpu.
87  */
88 #define MP_INFO_FLAG_PRIMARY_CPU	UINT32_C(0x00000001)
89 
90 /*
91  * This structure is used to provide information required to initialise a S-EL0
92  * partition.
93  */
94 struct stmm_mp_info {
95 	uint64_t mpidr;
96 	uint32_t linear_id;
97 	uint32_t flags;
98 };
99 
100 struct stmm_boot_info {
101 	struct stmm_param_header	h;
102 	uint64_t sp_mem_base;
103 	uint64_t sp_mem_limit;
104 	uint64_t sp_image_base;
105 	uint64_t sp_stack_base;
106 	uint64_t sp_heap_base;
107 	uint64_t sp_ns_comm_buf_base;
108 	uint64_t sp_shared_buf_base;
109 	uint64_t sp_image_size;
110 	uint64_t sp_pcpu_stack_size;
111 	uint64_t sp_heap_size;
112 	uint64_t sp_ns_comm_buf_size;
113 	uint64_t sp_shared_buf_size;
114 	uint32_t num_sp_mem_regions;
115 	uint32_t num_cpus;
116 	struct stmm_mp_info	*mp_info;
117 };
118 
119 struct stmm_ctx {
120 	struct user_mode_ctx uctx;
121 	struct tee_ta_ctx ta_ctx;
122 	struct thread_ctx_regs regs;
123 	vaddr_t ns_comm_buf_addr;
124 	unsigned int ns_comm_buf_size;
125 };
126 
127 extern const struct ts_ops stmm_sp_ops;
128 
is_stmm_ctx(struct ts_ctx * ctx __maybe_unused)129 static inline bool is_stmm_ctx(struct ts_ctx *ctx __maybe_unused)
130 {
131 	return IS_ENABLED(CFG_WITH_STMM_SP) && ctx && ctx->ops == &stmm_sp_ops;
132 }
133 
to_stmm_ctx(struct ts_ctx * ctx)134 static inline struct stmm_ctx *to_stmm_ctx(struct ts_ctx *ctx)
135 {
136 	assert(is_stmm_ctx(ctx));
137 	return container_of(ctx, struct stmm_ctx, ta_ctx.ts_ctx);
138 }
139 
140 #ifdef CFG_WITH_STMM_SP
141 /*
142  * Setup session context for the StMM application
143  * @uuid: TA UUID
144  * @sess: Session for which to setup the StMM context
145  *
146  * This function must be called with tee_ta_mutex locked.
147  */
148 TEE_Result stmm_init_session(const TEE_UUID *uuid,
149 			     struct tee_ta_session *s);
150 
151 /*
152  * Finalize session context initialization the StMM application
153  * @sess: Session for which to finalize StMM context
154  */
155 TEE_Result stmm_complete_session(struct tee_ta_session *s);
156 #else
157 static inline TEE_Result
stmm_init_session(const TEE_UUID * uuid __unused,struct tee_ta_session * s __unused)158 stmm_init_session(const TEE_UUID *uuid __unused,
159 		  struct tee_ta_session *s __unused)
160 {
161 	return TEE_ERROR_ITEM_NOT_FOUND;
162 }
163 
164 static inline TEE_Result
stmm_complete_session(struct tee_ta_session * s __unused)165 stmm_complete_session(struct tee_ta_session *s __unused)
166 {
167 	return TEE_ERROR_GENERIC;
168 }
169 #endif
170 
171 #ifdef CFG_WITH_STMM_SP
172 const TEE_UUID *stmm_get_uuid(void);
173 #else
stmm_get_uuid(void)174 static inline const TEE_UUID *stmm_get_uuid(void) { return NULL; }
175 #endif
176 
177 #endif /*__KERNEL_STMM_SP_H*/
178