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