xref: /rk3399_ARM-atf/services/std_svc/spm/el3_spmc/spmc.h (revision 742c23aab79a21803472c5b4314b43057f1d3e84)
1 /*
2  * Copyright (c) 2022, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef SPMC_H
8 #define SPMC_H
9 
10 #include <stdint.h>
11 
12 #include <lib/psci/psci.h>
13 #include <lib/spinlock.h>
14 #include <services/el3_spmc_logical_sp.h>
15 #include "spm_common.h"
16 
17 /*
18  * Ranges of FF-A IDs for Normal world and Secure world components. The
19  * convention matches that used by other SPMCs i.e. Hafnium and OP-TEE.
20  */
21 #define FFA_NWD_ID_BASE		0x0
22 #define FFA_NWD_ID_LIMIT	0x7FFF
23 #define FFA_SWD_ID_BASE		0x8000
24 #define FFA_SWD_ID_LIMIT	SPMD_DIRECT_MSG_ENDPOINT_ID - 1
25 #define FFA_SWD_ID_MASK		0x8000
26 
27 /* ID 0 is reserved for the normal world entity, (Hypervisor or OS Kernel). */
28 #define FFA_NWD_ID		U(0)
29 /* First ID is reserved for the SPMC */
30 #define FFA_SPMC_ID		U(FFA_SWD_ID_BASE)
31 /* SP IDs are allocated after the SPMC ID */
32 #define FFA_SP_ID_BASE		(FFA_SPMC_ID + 1)
33 /* Align with Hafnium implementation */
34 #define INV_SP_ID		0x7FFF
35 
36 /* FF-A Related helper macros. */
37 #define FFA_ID_MASK			U(0xFFFF)
38 #define FFA_PARTITION_ID_SHIFT		U(16)
39 #define FFA_FEATURES_BIT31_MASK		U(0x1u << 31)
40 #define FFA_FEATURES_RET_REQ_NS_BIT	U(0x1 << 1)
41 
42 #define FFA_RUN_EP_ID(ep_vcpu_ids) \
43 		((ep_vcpu_ids >> FFA_PARTITION_ID_SHIFT) & FFA_ID_MASK)
44 #define FFA_RUN_VCPU_ID(ep_vcpu_ids) \
45 		(ep_vcpu_ids & FFA_ID_MASK)
46 
47 #define FFA_PAGE_SIZE (4096)
48 #define FFA_RXTX_PAGE_COUNT_MASK 0x1F
49 
50 /* Ensure that the page size used by TF-A is 4k aligned. */
51 CASSERT((PAGE_SIZE % FFA_PAGE_SIZE) == 0, assert_aligned_page_size);
52 
53 /*
54  * Defines to allow an SP to subscribe for power management messages
55  */
56 #define FFA_PM_MSG_SUB_CPU_OFF			U(1 << 0)
57 #define FFA_PM_MSG_SUB_CPU_SUSPEND		U(1 << 1)
58 #define FFA_PM_MSG_SUB_CPU_SUSPEND_RESUME	U(1 << 2)
59 
60 /*
61  * Runtime states of an execution context as per the FF-A v1.1 specification.
62  */
63 enum sp_runtime_states {
64 	RT_STATE_WAITING,
65 	RT_STATE_RUNNING,
66 	RT_STATE_PREEMPTED,
67 	RT_STATE_BLOCKED
68 };
69 
70 /*
71  * Runtime model of an execution context as per the FF-A v1.1 specification. Its
72  * value is valid only if the execution context is not in the waiting state.
73  */
74 enum sp_runtime_model {
75 	RT_MODEL_DIR_REQ,
76 	RT_MODEL_RUN,
77 	RT_MODEL_INIT,
78 	RT_MODEL_INTR
79 };
80 
81 enum sp_runtime_el {
82 	EL1 = 0,
83 	S_EL0,
84 	S_EL1
85 };
86 
87 enum sp_execution_state {
88 	SP_STATE_AARCH64 = 0,
89 	SP_STATE_AARCH32
90 };
91 
92 enum mailbox_state {
93 	/* There is no message in the mailbox. */
94 	MAILBOX_STATE_EMPTY,
95 
96 	/* There is a message that has been populated in the mailbox. */
97 	MAILBOX_STATE_FULL,
98 };
99 
100 struct mailbox {
101 	enum mailbox_state state;
102 
103 	/* RX/TX Buffers. */
104 	void *rx_buffer;
105 	const void *tx_buffer;
106 
107 	/* Size of RX/TX Buffer. */
108 	uint32_t rxtx_page_count;
109 
110 	/* Lock access to mailbox. */
111 	spinlock_t lock;
112 };
113 
114 /*
115  * Execution context members for an SP. This is a bit like struct
116  * vcpu in a hypervisor.
117  */
118 struct sp_exec_ctx {
119 	/*
120 	 * Store the stack address to restore C runtime context from after
121 	 * returning from a synchronous entry into the SP.
122 	 */
123 	uint64_t c_rt_ctx;
124 
125 	/* Space to maintain the architectural state of an SP. */
126 	cpu_context_t cpu_ctx;
127 
128 	/* Track the current runtime state of the SP. */
129 	enum sp_runtime_states rt_state;
130 
131 	/* Track the current runtime model of the SP. */
132 	enum sp_runtime_model rt_model;
133 };
134 
135 /*
136  * Structure to describe the cumulative properties of an SP.
137  */
138 struct secure_partition_desc {
139 	/*
140 	 * Execution contexts allocated to this endpoint. Ideally,
141 	 * we need as many contexts as there are physical cpus only
142 	 * for a S-EL1 SP which is MP-pinned.
143 	 */
144 	struct sp_exec_ctx ec[PLATFORM_CORE_COUNT];
145 
146 	/* ID of the Secure Partition. */
147 	uint16_t sp_id;
148 
149 	/* Runtime EL. */
150 	enum sp_runtime_el runtime_el;
151 
152 	/* Partition UUID. */
153 	uint32_t uuid[4];
154 
155 	/* Partition Properties. */
156 	uint32_t properties;
157 
158 	/* Supported FF-A Version. */
159 	uint32_t ffa_version;
160 
161 	/* Execution State. */
162 	enum sp_execution_state execution_state;
163 
164 	/* Mailbox tracking. */
165 	struct mailbox mailbox;
166 
167 	/* Secondary entrypoint. Only valid for a S-EL1 SP. */
168 	uintptr_t secondary_ep;
169 
170 	/*
171 	 * Store whether the SP has subscribed to any power management messages.
172 	 */
173 	uint16_t pwr_mgmt_msgs;
174 
175 	/*
176 	 * Store whether the SP has requested the use of the NS bit for memory
177 	 * management transactions if it is using FF-A v1.0.
178 	 */
179 	bool ns_bit_requested;
180 };
181 
182 /*
183  * This define identifies the only SP that will be initialised and participate
184  * in FF-A communication. The implementation leaves the door open for more SPs
185  * to be managed in future but for now it is reasonable to assume that either a
186  * single S-EL0 or a single S-EL1 SP will be supported. This define will be used
187  * to identify which SP descriptor to initialise and manage during SP runtime.
188  */
189 #define ACTIVE_SP_DESC_INDEX	0
190 
191 /*
192  * Structure to describe the cumulative properties of the Hypervisor and
193  * NS-Endpoints.
194  */
195 struct ns_endpoint_desc {
196 	/*
197 	 * ID of the NS-Endpoint or Hypervisor.
198 	 */
199 	uint16_t ns_ep_id;
200 
201 	/*
202 	 * Mailbox tracking.
203 	 */
204 	struct mailbox mailbox;
205 
206 	/*
207 	 * Supported FF-A Version
208 	 */
209 	uint32_t ffa_version;
210 };
211 
212 /**
213  * Holds information returned for each partition by the FFA_PARTITION_INFO_GET
214  * interface.
215  */
216 struct ffa_partition_info_v1_0 {
217 	uint16_t ep_id;
218 	uint16_t execution_ctx_count;
219 	uint32_t properties;
220 };
221 
222 /* Extended structure for v1.1. */
223 struct ffa_partition_info_v1_1 {
224 	uint16_t ep_id;
225 	uint16_t execution_ctx_count;
226 	uint32_t properties;
227 	uint32_t uuid[4];
228 };
229 
230 /* Reference to power management hooks */
231 extern const spd_pm_ops_t spmc_pm;
232 
233 /* Setup Function for different SP types. */
234 void spmc_sp_common_setup(struct secure_partition_desc *sp,
235 			  entry_point_info_t *ep_info,
236 			  int32_t boot_info_reg);
237 void spmc_el1_sp_setup(struct secure_partition_desc *sp,
238 		       entry_point_info_t *ep_info);
239 void spmc_sp_common_ep_commit(struct secure_partition_desc *sp,
240 			      entry_point_info_t *ep_info);
241 
242 /*
243  * Helper function to perform a synchronous entry into a SP.
244  */
245 uint64_t spmc_sp_synchronous_entry(struct sp_exec_ctx *ec);
246 
247 /*
248  * Helper function to obtain the descriptor of the current SP on a physical cpu.
249  */
250 struct secure_partition_desc *spmc_get_current_sp_ctx(void);
251 
252 /*
253  * Helper function to obtain the execution context of an SP on a
254  * physical cpu.
255  */
256 struct sp_exec_ctx *spmc_get_sp_ec(struct secure_partition_desc *sp);
257 
258 /*
259  * Helper function to obtain the index of the execution context of an SP on a
260  * physical cpu.
261  */
262 unsigned int get_ec_index(struct secure_partition_desc *sp);
263 
264 uint64_t spmc_ffa_error_return(void *handle, int error_code);
265 
266 /*
267  * Ensure a partition ID does not clash and follows the secure world convention.
268  */
269 bool is_ffa_secure_id_valid(uint16_t partition_id);
270 
271 /*
272  * Helper function to obtain the array storing the EL3
273  * Logical Partition descriptors.
274  */
275 struct el3_lp_desc *get_el3_lp_array(void);
276 
277 /*
278  * Helper function to obtain the RX/TX buffer pair descriptor of the Hypervisor
279  * or OS kernel in the normal world or the last SP that was run.
280  */
281 struct mailbox *spmc_get_mbox_desc(bool secure_origin);
282 
283 /*
284  * Helper function to obtain the context of an SP with a given partition ID.
285  */
286 struct secure_partition_desc *spmc_get_sp_ctx(uint16_t id);
287 
288 /*
289  * Add helper function to obtain the FF-A version of the calling
290  * partition.
291  */
292 uint32_t get_partition_ffa_version(bool secure_origin);
293 
294 
295 #endif /* SPMC_H */
296