xref: /optee_os/core/arch/arm/kernel/thread_spmc.c (revision 25675979615c01f3c6bfbe105f53e07e939dd739)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2020-2023, Linaro Limited.
4  * Copyright (c) 2019-2024, Arm Limited. All rights reserved.
5  */
6 
7 #include <assert.h>
8 #include <ffa.h>
9 #include <initcall.h>
10 #include <io.h>
11 #include <kernel/interrupt.h>
12 #include <kernel/notif.h>
13 #include <kernel/panic.h>
14 #include <kernel/secure_partition.h>
15 #include <kernel/spinlock.h>
16 #include <kernel/spmc_sp_handler.h>
17 #include <kernel/tee_misc.h>
18 #include <kernel/thread.h>
19 #include <kernel/thread_private.h>
20 #include <kernel/thread_spmc.h>
21 #include <kernel/virtualization.h>
22 #include <mm/core_mmu.h>
23 #include <mm/mobj.h>
24 #include <optee_ffa.h>
25 #include <optee_msg.h>
26 #include <optee_rpc_cmd.h>
27 #include <sm/optee_smc.h>
28 #include <string.h>
29 #include <sys/queue.h>
30 #include <tee/entry_std.h>
31 #include <tee/uuid.h>
32 #include <util.h>
33 
34 #if defined(CFG_CORE_SEL1_SPMC)
35 struct mem_share_state {
36 	struct mobj_ffa *mf;
37 	unsigned int page_count;
38 	unsigned int region_count;
39 	unsigned int current_page_idx;
40 };
41 
42 struct mem_frag_state {
43 	struct mem_share_state share;
44 	tee_mm_entry_t *mm;
45 	unsigned int frag_offset;
46 	SLIST_ENTRY(mem_frag_state) link;
47 };
48 #endif
49 
50 static unsigned int spmc_notif_lock = SPINLOCK_UNLOCK;
51 static int do_bottom_half_value = -1;
52 static uint16_t notif_vm_id;
53 static bool spmc_notif_is_ready;
54 
55 /* Initialized in spmc_init() below */
56 uint16_t optee_endpoint_id __nex_bss;
57 uint16_t spmc_id __nex_bss;
58 #ifdef CFG_CORE_SEL1_SPMC
59 uint16_t spmd_id __nex_bss;
60 static const uint32_t my_part_props = FFA_PART_PROP_DIRECT_REQ_RECV |
61 				      FFA_PART_PROP_DIRECT_REQ_SEND |
62 #ifdef CFG_NS_VIRTUALIZATION
63 				      FFA_PART_PROP_NOTIF_CREATED |
64 				      FFA_PART_PROP_NOTIF_DESTROYED |
65 #endif
66 #ifdef ARM64
67 				      FFA_PART_PROP_AARCH64_STATE |
68 #endif
69 				      FFA_PART_PROP_IS_PE_ID;
70 
71 static uint32_t my_uuid_words[] = {
72 	/*
73 	 * - if the SPMC is in S-EL2 this UUID describes OP-TEE as a S-EL1
74 	 *   SP, or
75 	 * - if the SPMC is in S-EL1 then this UUID is for OP-TEE as a
76 	 *   logical partition, residing in the same exception level as the
77 	 *   SPMC
78 	 * UUID 486178e0-e7f8-11e3-bc5e-0002a5d5c51b
79 	 */
80 	0xe0786148, 0xe311f8e7, 0x02005ebc, 0x1bc5d5a5,
81 };
82 
83 /*
84  * If struct ffa_rxtx::size is 0 RX/TX buffers are not mapped or initialized.
85  *
86  * struct ffa_rxtx::spin_lock protects the variables below from concurrent
87  * access this includes the use of content of struct ffa_rxtx::rx and
88  * @frag_state_head.
89  *
90  * struct ffa_rxtx::tx_buf_is_mine is true when we may write to struct
91  * ffa_rxtx::tx and false when it is owned by normal world.
92  *
93  * Note that we can't prevent normal world from updating the content of
94  * these buffers so we must always be careful when reading. while we hold
95  * the lock.
96  */
97 
98 static struct ffa_rxtx my_rxtx __nex_bss;
99 
100 static bool is_nw_buf(struct ffa_rxtx *rxtx)
101 {
102 	return rxtx == &my_rxtx;
103 }
104 
105 static SLIST_HEAD(mem_frag_state_head, mem_frag_state) frag_state_head =
106 	SLIST_HEAD_INITIALIZER(&frag_state_head);
107 
108 static uint64_t notif_pending_bitmap;
109 static uint64_t notif_bound_bitmap;
110 static bool notif_vm_id_valid;
111 static int notif_intid = -1;
112 #else
113 static uint8_t __rx_buf[SMALL_PAGE_SIZE] __aligned(SMALL_PAGE_SIZE);
114 static uint8_t __tx_buf[SMALL_PAGE_SIZE] __aligned(SMALL_PAGE_SIZE);
115 static struct ffa_rxtx my_rxtx = {
116 	.rx = __rx_buf,
117 	.tx = __tx_buf,
118 	.size = sizeof(__rx_buf),
119 };
120 #endif
121 
122 static uint32_t swap_src_dst(uint32_t src_dst)
123 {
124 	return (src_dst >> 16) | (src_dst << 16);
125 }
126 
127 static uint16_t get_sender_id(uint32_t src_dst)
128 {
129 	return src_dst >> 16;
130 }
131 
132 void spmc_set_args(struct thread_smc_args *args, uint32_t fid, uint32_t src_dst,
133 		   uint32_t w2, uint32_t w3, uint32_t w4, uint32_t w5)
134 {
135 	*args = (struct thread_smc_args){ .a0 = fid,
136 					  .a1 = src_dst,
137 					  .a2 = w2,
138 					  .a3 = w3,
139 					  .a4 = w4,
140 					  .a5 = w5, };
141 }
142 
143 static void set_simple_ret_val(struct thread_smc_args *args, int ffa_ret)
144 {
145 	if (ffa_ret)
146 		spmc_set_args(args, FFA_ERROR, 0, ffa_ret, 0, 0, 0);
147 	else
148 		spmc_set_args(args, FFA_SUCCESS_32, 0, 0, 0, 0, 0);
149 }
150 
151 uint32_t spmc_exchange_version(uint32_t vers, struct ffa_rxtx *rxtx)
152 {
153 	/*
154 	 * No locking, if the caller does concurrent calls to this it's
155 	 * only making a mess for itself. We must be able to renegotiate
156 	 * the FF-A version in order to support differing versions between
157 	 * the loader and the driver.
158 	 */
159 	if (vers < FFA_VERSION_1_1)
160 		rxtx->ffa_vers = FFA_VERSION_1_0;
161 	else
162 		rxtx->ffa_vers = FFA_VERSION_1_1;
163 
164 	return rxtx->ffa_vers;
165 }
166 
167 static bool is_ffa_success(uint32_t fid)
168 {
169 #ifdef ARM64
170 	if (fid == FFA_SUCCESS_64)
171 		return true;
172 #endif
173 	return fid == FFA_SUCCESS_32;
174 }
175 
176 static int32_t get_ffa_ret_code(const struct thread_smc_args *args)
177 {
178 	if (is_ffa_success(args->a0))
179 		return FFA_OK;
180 	if (args->a0 == FFA_ERROR && args->a2)
181 		return args->a2;
182 	return FFA_NOT_SUPPORTED;
183 }
184 
185 static int ffa_simple_call(uint32_t fid, unsigned long a1, unsigned long a2,
186 			   unsigned long a3, unsigned long a4)
187 {
188 	struct thread_smc_args args = {
189 		.a0 = fid,
190 		.a1 = a1,
191 		.a2 = a2,
192 		.a3 = a3,
193 		.a4 = a4,
194 	};
195 
196 	thread_smccc(&args);
197 
198 	return get_ffa_ret_code(&args);
199 }
200 
201 static int __maybe_unused ffa_features(uint32_t id)
202 {
203 	return ffa_simple_call(FFA_FEATURES, id, 0, 0, 0);
204 }
205 
206 static int __maybe_unused ffa_set_notification(uint16_t dst, uint16_t src,
207 					       uint32_t flags, uint64_t bitmap)
208 {
209 	return ffa_simple_call(FFA_NOTIFICATION_SET,
210 			       SHIFT_U32(src, 16) | dst, flags,
211 			       low32_from_64(bitmap), high32_from_64(bitmap));
212 }
213 
214 #if defined(CFG_CORE_SEL1_SPMC)
215 static void handle_features(struct thread_smc_args *args)
216 {
217 	uint32_t ret_fid = FFA_ERROR;
218 	uint32_t ret_w2 = FFA_NOT_SUPPORTED;
219 
220 	switch (args->a1) {
221 	case FFA_FEATURE_SCHEDULE_RECV_INTR:
222 		if (spmc_notif_is_ready) {
223 			ret_fid = FFA_SUCCESS_32;
224 			ret_w2 = notif_intid;
225 		}
226 		break;
227 
228 #ifdef ARM64
229 	case FFA_RXTX_MAP_64:
230 #endif
231 	case FFA_RXTX_MAP_32:
232 		ret_fid = FFA_SUCCESS_32;
233 		ret_w2 = 0; /* 4kB Minimum buffer size and alignment boundary */
234 		break;
235 #ifdef ARM64
236 	case FFA_MEM_SHARE_64:
237 #endif
238 	case FFA_MEM_SHARE_32:
239 		ret_fid = FFA_SUCCESS_32;
240 		/*
241 		 * Partition manager supports transmission of a memory
242 		 * transaction descriptor in a buffer dynamically allocated
243 		 * by the endpoint.
244 		 */
245 		ret_w2 = BIT(0);
246 		break;
247 
248 	case FFA_ERROR:
249 	case FFA_VERSION:
250 	case FFA_SUCCESS_32:
251 #ifdef ARM64
252 	case FFA_SUCCESS_64:
253 #endif
254 	case FFA_FEATURES:
255 	case FFA_SPM_ID_GET:
256 	case FFA_MEM_FRAG_TX:
257 	case FFA_MEM_RECLAIM:
258 	case FFA_MSG_SEND_DIRECT_REQ_64:
259 	case FFA_MSG_SEND_DIRECT_REQ_32:
260 	case FFA_INTERRUPT:
261 	case FFA_PARTITION_INFO_GET:
262 	case FFA_RXTX_UNMAP:
263 	case FFA_RX_RELEASE:
264 	case FFA_FEATURE_MANAGED_EXIT_INTR:
265 	case FFA_NOTIFICATION_BITMAP_CREATE:
266 	case FFA_NOTIFICATION_BITMAP_DESTROY:
267 	case FFA_NOTIFICATION_BIND:
268 	case FFA_NOTIFICATION_UNBIND:
269 	case FFA_NOTIFICATION_SET:
270 	case FFA_NOTIFICATION_GET:
271 	case FFA_NOTIFICATION_INFO_GET_32:
272 #ifdef ARM64
273 	case FFA_NOTIFICATION_INFO_GET_64:
274 #endif
275 		ret_fid = FFA_SUCCESS_32;
276 		ret_w2 = FFA_PARAM_MBZ;
277 		break;
278 	default:
279 		break;
280 	}
281 
282 	spmc_set_args(args, ret_fid, FFA_PARAM_MBZ, ret_w2, FFA_PARAM_MBZ,
283 		      FFA_PARAM_MBZ, FFA_PARAM_MBZ);
284 }
285 
286 static int map_buf(paddr_t pa, unsigned int sz, void **va_ret)
287 {
288 	tee_mm_entry_t *mm = NULL;
289 
290 	if (!core_pbuf_is(CORE_MEM_NON_SEC, pa, sz))
291 		return FFA_INVALID_PARAMETERS;
292 
293 	mm = tee_mm_alloc(&tee_mm_shm, sz);
294 	if (!mm)
295 		return FFA_NO_MEMORY;
296 
297 	if (core_mmu_map_contiguous_pages(tee_mm_get_smem(mm), pa,
298 					  sz / SMALL_PAGE_SIZE,
299 					  MEM_AREA_NSEC_SHM)) {
300 		tee_mm_free(mm);
301 		return FFA_INVALID_PARAMETERS;
302 	}
303 
304 	*va_ret = (void *)tee_mm_get_smem(mm);
305 	return 0;
306 }
307 
308 void spmc_handle_spm_id_get(struct thread_smc_args *args)
309 {
310 	spmc_set_args(args, FFA_SUCCESS_32, FFA_PARAM_MBZ, spmc_id,
311 		      FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ);
312 }
313 
314 static void unmap_buf(void *va, size_t sz)
315 {
316 	tee_mm_entry_t *mm = tee_mm_find(&tee_mm_shm, (vaddr_t)va);
317 
318 	assert(mm);
319 	core_mmu_unmap_pages(tee_mm_get_smem(mm), sz / SMALL_PAGE_SIZE);
320 	tee_mm_free(mm);
321 }
322 
323 void spmc_handle_rxtx_map(struct thread_smc_args *args, struct ffa_rxtx *rxtx)
324 {
325 	int rc = 0;
326 	unsigned int sz = 0;
327 	paddr_t rx_pa = 0;
328 	paddr_t tx_pa = 0;
329 	void *rx = NULL;
330 	void *tx = NULL;
331 
332 	cpu_spin_lock(&rxtx->spinlock);
333 
334 	if (args->a3 & GENMASK_64(63, 6)) {
335 		rc = FFA_INVALID_PARAMETERS;
336 		goto out;
337 	}
338 
339 	sz = args->a3 * SMALL_PAGE_SIZE;
340 	if (!sz) {
341 		rc = FFA_INVALID_PARAMETERS;
342 		goto out;
343 	}
344 	/* TX/RX are swapped compared to the caller */
345 	tx_pa = args->a2;
346 	rx_pa = args->a1;
347 
348 	if (rxtx->size) {
349 		rc = FFA_DENIED;
350 		goto out;
351 	}
352 
353 	/*
354 	 * If the buffer comes from a SP the address is virtual and already
355 	 * mapped.
356 	 */
357 	if (is_nw_buf(rxtx)) {
358 		if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) {
359 			enum teecore_memtypes mt = MEM_AREA_NEX_NSEC_SHM;
360 			bool tx_alloced = false;
361 
362 			/*
363 			 * With virtualization we establish this mapping in
364 			 * the nexus mapping which then is replicated to
365 			 * each partition.
366 			 *
367 			 * This means that this mapping must be done before
368 			 * any partition is created and then must not be
369 			 * changed.
370 			 */
371 
372 			/*
373 			 * core_mmu_add_mapping() may reuse previous
374 			 * mappings. First check if there's any mappings to
375 			 * reuse so we know how to clean up in case of
376 			 * failure.
377 			 */
378 			tx = phys_to_virt(tx_pa, mt, sz);
379 			rx = phys_to_virt(rx_pa, mt, sz);
380 			if (!tx) {
381 				tx = core_mmu_add_mapping(mt, tx_pa, sz);
382 				if (!tx) {
383 					rc = FFA_NO_MEMORY;
384 					goto out;
385 				}
386 				tx_alloced = true;
387 			}
388 			if (!rx)
389 				rx = core_mmu_add_mapping(mt, rx_pa, sz);
390 
391 			if (!rx) {
392 				if (tx_alloced && tx)
393 					core_mmu_remove_mapping(mt, tx, sz);
394 				rc = FFA_NO_MEMORY;
395 				goto out;
396 			}
397 		} else {
398 			rc = map_buf(tx_pa, sz, &tx);
399 			if (rc)
400 				goto out;
401 			rc = map_buf(rx_pa, sz, &rx);
402 			if (rc) {
403 				unmap_buf(tx, sz);
404 				goto out;
405 			}
406 		}
407 		rxtx->tx = tx;
408 		rxtx->rx = rx;
409 	} else {
410 		if ((tx_pa & SMALL_PAGE_MASK) || (rx_pa & SMALL_PAGE_MASK)) {
411 			rc = FFA_INVALID_PARAMETERS;
412 			goto out;
413 		}
414 
415 		if (!virt_to_phys((void *)tx_pa) ||
416 		    !virt_to_phys((void *)rx_pa)) {
417 			rc = FFA_INVALID_PARAMETERS;
418 			goto out;
419 		}
420 
421 		rxtx->tx = (void *)tx_pa;
422 		rxtx->rx = (void *)rx_pa;
423 	}
424 
425 	rxtx->size = sz;
426 	rxtx->tx_is_mine = true;
427 	DMSG("Mapped tx %#"PRIxPA" size %#x @ %p", tx_pa, sz, tx);
428 	DMSG("Mapped rx %#"PRIxPA" size %#x @ %p", rx_pa, sz, rx);
429 out:
430 	cpu_spin_unlock(&rxtx->spinlock);
431 	set_simple_ret_val(args, rc);
432 }
433 
434 void spmc_handle_rxtx_unmap(struct thread_smc_args *args, struct ffa_rxtx *rxtx)
435 {
436 	int rc = FFA_INVALID_PARAMETERS;
437 
438 	cpu_spin_lock(&rxtx->spinlock);
439 
440 	if (!rxtx->size)
441 		goto out;
442 
443 	/* We don't unmap the SP memory as the SP might still use it */
444 	if (is_nw_buf(rxtx)) {
445 		unmap_buf(rxtx->rx, rxtx->size);
446 		unmap_buf(rxtx->tx, rxtx->size);
447 	}
448 	rxtx->size = 0;
449 	rxtx->rx = NULL;
450 	rxtx->tx = NULL;
451 	rc = 0;
452 out:
453 	cpu_spin_unlock(&rxtx->spinlock);
454 	set_simple_ret_val(args, rc);
455 }
456 
457 void spmc_handle_rx_release(struct thread_smc_args *args, struct ffa_rxtx *rxtx)
458 {
459 	int rc = 0;
460 
461 	cpu_spin_lock(&rxtx->spinlock);
462 	/* The senders RX is our TX */
463 	if (!rxtx->size || rxtx->tx_is_mine) {
464 		rc = FFA_DENIED;
465 	} else {
466 		rc = 0;
467 		rxtx->tx_is_mine = true;
468 	}
469 	cpu_spin_unlock(&rxtx->spinlock);
470 
471 	set_simple_ret_val(args, rc);
472 }
473 
474 static bool is_nil_uuid(uint32_t w0, uint32_t w1, uint32_t w2, uint32_t w3)
475 {
476 	return !w0 && !w1 && !w2 && !w3;
477 }
478 
479 static bool is_my_uuid(uint32_t w0, uint32_t w1, uint32_t w2, uint32_t w3)
480 {
481 	/*
482 	 * This depends on which UUID we have been assigned.
483 	 * TODO add a generic mechanism to obtain our UUID.
484 	 *
485 	 * The test below is for the hard coded UUID
486 	 * 486178e0-e7f8-11e3-bc5e-0002a5d5c51b
487 	 */
488 	return w0 == my_uuid_words[0] && w1 == my_uuid_words[1] &&
489 	       w2 == my_uuid_words[2] && w3 == my_uuid_words[3];
490 }
491 
492 TEE_Result spmc_fill_partition_entry(uint32_t ffa_vers, void *buf, size_t blen,
493 				     size_t idx, uint16_t endpoint_id,
494 				     uint16_t execution_context,
495 				     uint32_t part_props,
496 				     const uint32_t uuid_words[4])
497 {
498 	struct ffa_partition_info_x *fpi = NULL;
499 	size_t fpi_size = sizeof(*fpi);
500 
501 	if (ffa_vers >= FFA_VERSION_1_1)
502 		fpi_size += FFA_UUID_SIZE;
503 
504 	if ((idx + 1) * fpi_size > blen)
505 		return TEE_ERROR_OUT_OF_MEMORY;
506 
507 	fpi = (void *)((vaddr_t)buf + idx * fpi_size);
508 	fpi->id = endpoint_id;
509 	/* Number of execution contexts implemented by this partition */
510 	fpi->execution_context = execution_context;
511 
512 	fpi->partition_properties = part_props;
513 
514 	if (ffa_vers >= FFA_VERSION_1_1) {
515 		if (uuid_words)
516 			memcpy(fpi->uuid, uuid_words, FFA_UUID_SIZE);
517 		else
518 			memset(fpi->uuid, 0, FFA_UUID_SIZE);
519 	}
520 
521 	return TEE_SUCCESS;
522 }
523 
524 static int handle_partition_info_get_all(size_t *elem_count,
525 					 struct ffa_rxtx *rxtx, bool count_only)
526 {
527 	if (!count_only) {
528 		/* Add OP-TEE SP */
529 		if (spmc_fill_partition_entry(rxtx->ffa_vers, rxtx->tx,
530 					      rxtx->size, 0, optee_endpoint_id,
531 					      CFG_TEE_CORE_NB_CORE,
532 					      my_part_props, my_uuid_words))
533 			return FFA_NO_MEMORY;
534 	}
535 	*elem_count = 1;
536 
537 	if (IS_ENABLED(CFG_SECURE_PARTITION)) {
538 		if (sp_partition_info_get(rxtx->ffa_vers, rxtx->tx, rxtx->size,
539 					  NULL, elem_count, count_only))
540 			return FFA_NO_MEMORY;
541 	}
542 
543 	return FFA_OK;
544 }
545 
546 void spmc_handle_partition_info_get(struct thread_smc_args *args,
547 				    struct ffa_rxtx *rxtx)
548 {
549 	TEE_Result res = TEE_SUCCESS;
550 	uint32_t ret_fid = FFA_ERROR;
551 	uint32_t fpi_size = 0;
552 	uint32_t rc = 0;
553 	bool count_only = args->a5 & FFA_PARTITION_INFO_GET_COUNT_FLAG;
554 
555 	if (!count_only) {
556 		cpu_spin_lock(&rxtx->spinlock);
557 
558 		if (!rxtx->size || !rxtx->tx_is_mine) {
559 			rc = FFA_BUSY;
560 			goto out;
561 		}
562 	}
563 
564 	if (is_nil_uuid(args->a1, args->a2, args->a3, args->a4)) {
565 		size_t elem_count = 0;
566 
567 		ret_fid = handle_partition_info_get_all(&elem_count, rxtx,
568 							count_only);
569 
570 		if (ret_fid) {
571 			rc = ret_fid;
572 			ret_fid = FFA_ERROR;
573 		} else {
574 			ret_fid = FFA_SUCCESS_32;
575 			rc = elem_count;
576 		}
577 
578 		goto out;
579 	}
580 
581 	if (is_my_uuid(args->a1, args->a2, args->a3, args->a4)) {
582 		if (!count_only) {
583 			res = spmc_fill_partition_entry(rxtx->ffa_vers,
584 							rxtx->tx, rxtx->size, 0,
585 							optee_endpoint_id,
586 							CFG_TEE_CORE_NB_CORE,
587 							my_part_props,
588 							my_uuid_words);
589 			if (res) {
590 				ret_fid = FFA_ERROR;
591 				rc = FFA_INVALID_PARAMETERS;
592 				goto out;
593 			}
594 		}
595 		rc = 1;
596 	} else if (IS_ENABLED(CFG_SECURE_PARTITION)) {
597 		uint32_t uuid_array[4] = { 0 };
598 		TEE_UUID uuid = { };
599 		size_t count = 0;
600 
601 		uuid_array[0] = args->a1;
602 		uuid_array[1] = args->a2;
603 		uuid_array[2] = args->a3;
604 		uuid_array[3] = args->a4;
605 		tee_uuid_from_octets(&uuid, (uint8_t *)uuid_array);
606 
607 		res = sp_partition_info_get(rxtx->ffa_vers, rxtx->tx,
608 					    rxtx->size, &uuid, &count,
609 					    count_only);
610 		if (res != TEE_SUCCESS) {
611 			ret_fid = FFA_ERROR;
612 			rc = FFA_INVALID_PARAMETERS;
613 			goto out;
614 		}
615 		rc = count;
616 	} else {
617 		ret_fid = FFA_ERROR;
618 		rc = FFA_INVALID_PARAMETERS;
619 		goto out;
620 	}
621 
622 	ret_fid = FFA_SUCCESS_32;
623 
624 out:
625 	if (ret_fid == FFA_SUCCESS_32 && !count_only &&
626 	    rxtx->ffa_vers >= FFA_VERSION_1_1)
627 		fpi_size = sizeof(struct ffa_partition_info_x) + FFA_UUID_SIZE;
628 
629 	spmc_set_args(args, ret_fid, FFA_PARAM_MBZ, rc, fpi_size,
630 		      FFA_PARAM_MBZ, FFA_PARAM_MBZ);
631 	if (!count_only) {
632 		rxtx->tx_is_mine = false;
633 		cpu_spin_unlock(&rxtx->spinlock);
634 	}
635 }
636 
637 static void spmc_handle_run(struct thread_smc_args *args)
638 {
639 	uint16_t endpoint = FFA_TARGET_INFO_GET_SP_ID(args->a1);
640 	uint16_t thread_id = FFA_TARGET_INFO_GET_VCPU_ID(args->a1);
641 	uint32_t rc = FFA_OK;
642 
643 	if (endpoint != optee_endpoint_id) {
644 		/*
645 		 * The endpoint should be an SP, try to resume the SP from
646 		 * preempted into busy state.
647 		 */
648 		rc = spmc_sp_resume_from_preempted(endpoint);
649 		if (rc)
650 			goto out;
651 	}
652 
653 	thread_resume_from_rpc(thread_id, 0, 0, 0, 0);
654 
655 	/* thread_resume_from_rpc return only of the thread_id is invalid */
656 	rc = FFA_INVALID_PARAMETERS;
657 
658 out:
659 	set_simple_ret_val(args, rc);
660 }
661 #endif /*CFG_CORE_SEL1_SPMC*/
662 
663 static uint32_t spmc_enable_async_notif(uint32_t bottom_half_value,
664 					uint16_t vm_id)
665 {
666 	uint32_t old_itr_status = 0;
667 
668 	if (!spmc_notif_is_ready) {
669 		/*
670 		 * This should never happen, not if normal world respects the
671 		 * exchanged capabilities.
672 		 */
673 		EMSG("Asynchronous notifications are not ready");
674 		return TEE_ERROR_NOT_IMPLEMENTED;
675 	}
676 
677 	if (bottom_half_value >= OPTEE_FFA_MAX_ASYNC_NOTIF_VALUE) {
678 		EMSG("Invalid bottom half value %"PRIu32, bottom_half_value);
679 		return TEE_ERROR_BAD_PARAMETERS;
680 	}
681 
682 	old_itr_status = cpu_spin_lock_xsave(&spmc_notif_lock);
683 	do_bottom_half_value = bottom_half_value;
684 	if (!IS_ENABLED(CFG_CORE_SEL1_SPMC))
685 		notif_vm_id = vm_id;
686 	cpu_spin_unlock_xrestore(&spmc_notif_lock, old_itr_status);
687 
688 	notif_deliver_atomic_event(NOTIF_EVENT_STARTED);
689 	return TEE_SUCCESS;
690 }
691 
692 static void handle_yielding_call(struct thread_smc_args *args,
693 				 uint32_t direct_resp_fid)
694 {
695 	TEE_Result res = 0;
696 
697 	thread_check_canaries();
698 
699 #ifdef ARM64
700 	/* Saving this for an eventual RPC */
701 	thread_get_core_local()->direct_resp_fid = direct_resp_fid;
702 #endif
703 
704 	if (args->a3 == OPTEE_FFA_YIELDING_CALL_RESUME) {
705 		/* Note connection to struct thread_rpc_arg::ret */
706 		thread_resume_from_rpc(args->a7, args->a4, args->a5, args->a6,
707 				       0);
708 		res = TEE_ERROR_BAD_PARAMETERS;
709 	} else {
710 		thread_alloc_and_run(args->a1, args->a3, args->a4, args->a5,
711 				     args->a6, args->a7);
712 		res = TEE_ERROR_BUSY;
713 	}
714 	spmc_set_args(args, direct_resp_fid, swap_src_dst(args->a1),
715 		      0, res, 0, 0);
716 }
717 
718 static uint32_t handle_unregister_shm(uint32_t a4, uint32_t a5)
719 {
720 	uint64_t cookie = reg_pair_to_64(a5, a4);
721 	uint32_t res = 0;
722 
723 	res = mobj_ffa_unregister_by_cookie(cookie);
724 	switch (res) {
725 	case TEE_SUCCESS:
726 	case TEE_ERROR_ITEM_NOT_FOUND:
727 		return 0;
728 	case TEE_ERROR_BUSY:
729 		EMSG("res %#"PRIx32, res);
730 		return FFA_BUSY;
731 	default:
732 		EMSG("res %#"PRIx32, res);
733 		return FFA_INVALID_PARAMETERS;
734 	}
735 }
736 
737 static void handle_blocking_call(struct thread_smc_args *args,
738 				 uint32_t direct_resp_fid)
739 {
740 	uint32_t sec_caps = 0;
741 
742 	switch (args->a3) {
743 	case OPTEE_FFA_GET_API_VERSION:
744 		spmc_set_args(args, direct_resp_fid, swap_src_dst(args->a1), 0,
745 			      OPTEE_FFA_VERSION_MAJOR, OPTEE_FFA_VERSION_MINOR,
746 			      0);
747 		break;
748 	case OPTEE_FFA_GET_OS_VERSION:
749 		spmc_set_args(args, direct_resp_fid, swap_src_dst(args->a1), 0,
750 			      CFG_OPTEE_REVISION_MAJOR,
751 			      CFG_OPTEE_REVISION_MINOR,
752 			      TEE_IMPL_GIT_SHA1 >> 32);
753 		break;
754 	case OPTEE_FFA_EXCHANGE_CAPABILITIES:
755 		sec_caps = OPTEE_FFA_SEC_CAP_ARG_OFFSET;
756 		if (spmc_notif_is_ready)
757 			sec_caps |= OPTEE_FFA_SEC_CAP_ASYNC_NOTIF;
758 		spmc_set_args(args, direct_resp_fid,
759 			      swap_src_dst(args->a1), 0, 0,
760 			      THREAD_RPC_MAX_NUM_PARAMS, sec_caps);
761 		break;
762 	case OPTEE_FFA_UNREGISTER_SHM:
763 		spmc_set_args(args, direct_resp_fid, swap_src_dst(args->a1), 0,
764 			      handle_unregister_shm(args->a4, args->a5), 0, 0);
765 		break;
766 	case OPTEE_FFA_ENABLE_ASYNC_NOTIF:
767 		spmc_set_args(args, direct_resp_fid,
768 			      swap_src_dst(args->a1), 0,
769 			      spmc_enable_async_notif(args->a4,
770 						      FFA_SRC(args->a1)),
771 			      0, 0);
772 		break;
773 	default:
774 		EMSG("Unhandled blocking service ID %#"PRIx32,
775 		     (uint32_t)args->a3);
776 		spmc_set_args(args, direct_resp_fid, swap_src_dst(args->a1), 0,
777 			      TEE_ERROR_BAD_PARAMETERS, 0, 0);
778 	}
779 }
780 
781 static void handle_framework_direct_request(struct thread_smc_args *args,
782 					    struct ffa_rxtx *rxtx,
783 					    uint32_t direct_resp_fid)
784 {
785 	uint32_t w0 = FFA_ERROR;
786 	uint32_t w1 = FFA_PARAM_MBZ;
787 	uint32_t w2 = FFA_NOT_SUPPORTED;
788 	uint32_t w3 = FFA_PARAM_MBZ;
789 
790 	switch (args->a2 & FFA_MSG_TYPE_MASK) {
791 	case FFA_MSG_SEND_VM_CREATED:
792 		if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) {
793 			uint16_t guest_id = args->a5;
794 			TEE_Result res = virt_guest_created(guest_id);
795 
796 			w0 = direct_resp_fid;
797 			w1 = swap_src_dst(args->a1);
798 			w2 = FFA_MSG_FLAG_FRAMEWORK | FFA_MSG_RESP_VM_CREATED;
799 			if (res == TEE_SUCCESS)
800 				w3 = FFA_OK;
801 			else if (res == TEE_ERROR_OUT_OF_MEMORY)
802 				w3 = FFA_DENIED;
803 			else
804 				w3 = FFA_INVALID_PARAMETERS;
805 		}
806 		break;
807 	case FFA_MSG_SEND_VM_DESTROYED:
808 		if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) {
809 			uint16_t guest_id = args->a5;
810 			TEE_Result res = virt_guest_destroyed(guest_id);
811 
812 			w0 = direct_resp_fid;
813 			w1 = swap_src_dst(args->a1);
814 			w2 = FFA_MSG_FLAG_FRAMEWORK | FFA_MSG_RESP_VM_DESTROYED;
815 			if (res == TEE_SUCCESS)
816 				w3 = FFA_OK;
817 			else
818 				w3 = FFA_INVALID_PARAMETERS;
819 		}
820 		break;
821 	case FFA_MSG_VERSION_REQ:
822 		w0 = direct_resp_fid;
823 		w1 = swap_src_dst(args->a1);
824 		w2 = FFA_MSG_FLAG_FRAMEWORK | FFA_MSG_VERSION_RESP;
825 		w3 = spmc_exchange_version(args->a3, rxtx);
826 		break;
827 	default:
828 		break;
829 	}
830 	spmc_set_args(args, w0, w1, w2, w3, FFA_PARAM_MBZ, FFA_PARAM_MBZ);
831 }
832 
833 static void handle_direct_request(struct thread_smc_args *args,
834 				  struct ffa_rxtx *rxtx)
835 {
836 	uint32_t direct_resp_fid = 0;
837 
838 	if (IS_ENABLED(CFG_SECURE_PARTITION) &&
839 	    FFA_DST(args->a1) != optee_endpoint_id) {
840 		spmc_sp_start_thread(args);
841 		return;
842 	}
843 
844 	if (OPTEE_SMC_IS_64(args->a0))
845 		direct_resp_fid = FFA_MSG_SEND_DIRECT_RESP_64;
846 	else
847 		direct_resp_fid = FFA_MSG_SEND_DIRECT_RESP_32;
848 
849 	if (args->a2 & FFA_MSG_FLAG_FRAMEWORK) {
850 		handle_framework_direct_request(args, rxtx, direct_resp_fid);
851 		return;
852 	}
853 
854 	if (IS_ENABLED(CFG_NS_VIRTUALIZATION) &&
855 	    virt_set_guest(get_sender_id(args->a1))) {
856 		spmc_set_args(args, direct_resp_fid, swap_src_dst(args->a1), 0,
857 			      TEE_ERROR_ITEM_NOT_FOUND, 0, 0);
858 		return;
859 	}
860 
861 	if (args->a3 & BIT32(OPTEE_FFA_YIELDING_CALL_BIT))
862 		handle_yielding_call(args, direct_resp_fid);
863 	else
864 		handle_blocking_call(args, direct_resp_fid);
865 
866 	/*
867 	 * Note that handle_yielding_call() typically only returns if a
868 	 * thread cannot be allocated or found. virt_unset_guest() is also
869 	 * called from thread_state_suspend() and thread_state_free().
870 	 */
871 	virt_unset_guest();
872 }
873 
874 int spmc_read_mem_transaction(uint32_t ffa_vers, void *buf, size_t blen,
875 			      struct ffa_mem_transaction_x *trans)
876 {
877 	uint16_t mem_reg_attr = 0;
878 	uint32_t flags = 0;
879 	uint32_t count = 0;
880 	uint32_t offs = 0;
881 	uint32_t size = 0;
882 	size_t n = 0;
883 
884 	if (!IS_ALIGNED_WITH_TYPE(buf, uint64_t))
885 		return FFA_INVALID_PARAMETERS;
886 
887 	if (ffa_vers >= FFA_VERSION_1_1) {
888 		struct ffa_mem_transaction_1_1 *descr = NULL;
889 
890 		if (blen < sizeof(*descr))
891 			return FFA_INVALID_PARAMETERS;
892 
893 		descr = buf;
894 		trans->sender_id = READ_ONCE(descr->sender_id);
895 		mem_reg_attr = READ_ONCE(descr->mem_reg_attr);
896 		flags = READ_ONCE(descr->flags);
897 		trans->global_handle = READ_ONCE(descr->global_handle);
898 		trans->tag = READ_ONCE(descr->tag);
899 
900 		count = READ_ONCE(descr->mem_access_count);
901 		size = READ_ONCE(descr->mem_access_size);
902 		offs = READ_ONCE(descr->mem_access_offs);
903 	} else {
904 		struct ffa_mem_transaction_1_0 *descr = NULL;
905 
906 		if (blen < sizeof(*descr))
907 			return FFA_INVALID_PARAMETERS;
908 
909 		descr = buf;
910 		trans->sender_id = READ_ONCE(descr->sender_id);
911 		mem_reg_attr = READ_ONCE(descr->mem_reg_attr);
912 		flags = READ_ONCE(descr->flags);
913 		trans->global_handle = READ_ONCE(descr->global_handle);
914 		trans->tag = READ_ONCE(descr->tag);
915 
916 		count = READ_ONCE(descr->mem_access_count);
917 		size = sizeof(struct ffa_mem_access);
918 		offs = offsetof(struct ffa_mem_transaction_1_0,
919 				mem_access_array);
920 	}
921 
922 	if (mem_reg_attr > UINT8_MAX || flags > UINT8_MAX ||
923 	    size > UINT8_MAX || count > UINT8_MAX || offs > UINT16_MAX)
924 		return FFA_INVALID_PARAMETERS;
925 
926 	/* Check that the endpoint memory access descriptor array fits */
927 	if (MUL_OVERFLOW(size, count, &n) || ADD_OVERFLOW(offs, n, &n) ||
928 	    n > blen)
929 		return FFA_INVALID_PARAMETERS;
930 
931 	trans->mem_reg_attr = mem_reg_attr;
932 	trans->flags = flags;
933 	trans->mem_access_size = size;
934 	trans->mem_access_count = count;
935 	trans->mem_access_offs = offs;
936 	return 0;
937 }
938 
939 #if defined(CFG_CORE_SEL1_SPMC)
940 static int get_acc_perms(vaddr_t mem_acc_base, unsigned int mem_access_size,
941 			 unsigned int mem_access_count, uint8_t *acc_perms,
942 			 unsigned int *region_offs)
943 {
944 	struct ffa_mem_access_perm *descr = NULL;
945 	struct ffa_mem_access *mem_acc = NULL;
946 	unsigned int n = 0;
947 
948 	for (n = 0; n < mem_access_count; n++) {
949 		mem_acc = (void *)(mem_acc_base + mem_access_size * n);
950 		descr = &mem_acc->access_perm;
951 		if (READ_ONCE(descr->endpoint_id) == optee_endpoint_id) {
952 			*acc_perms = READ_ONCE(descr->perm);
953 			*region_offs = READ_ONCE(mem_acc[n].region_offs);
954 			return 0;
955 		}
956 	}
957 
958 	return FFA_INVALID_PARAMETERS;
959 }
960 
961 static int mem_share_init(struct ffa_mem_transaction_x *mem_trans, void *buf,
962 			  size_t blen, unsigned int *page_count,
963 			  unsigned int *region_count, size_t *addr_range_offs)
964 {
965 	const uint16_t exp_mem_reg_attr = FFA_NORMAL_MEM_REG_ATTR;
966 	const uint8_t exp_mem_acc_perm = FFA_MEM_ACC_RW;
967 	struct ffa_mem_region *region_descr = NULL;
968 	unsigned int region_descr_offs = 0;
969 	uint8_t mem_acc_perm = 0;
970 	size_t n = 0;
971 
972 	if (mem_trans->mem_reg_attr != exp_mem_reg_attr)
973 		return FFA_INVALID_PARAMETERS;
974 
975 	/* Check that the access permissions matches what's expected */
976 	if (get_acc_perms((vaddr_t)buf + mem_trans->mem_access_offs,
977 			  mem_trans->mem_access_size,
978 			  mem_trans->mem_access_count,
979 			  &mem_acc_perm, &region_descr_offs) ||
980 	    mem_acc_perm != exp_mem_acc_perm)
981 		return FFA_INVALID_PARAMETERS;
982 
983 	/* Check that the Composite memory region descriptor fits */
984 	if (ADD_OVERFLOW(region_descr_offs, sizeof(*region_descr), &n) ||
985 	    n > blen)
986 		return FFA_INVALID_PARAMETERS;
987 
988 	if (!IS_ALIGNED_WITH_TYPE((vaddr_t)buf + region_descr_offs,
989 				  struct ffa_mem_region))
990 		return FFA_INVALID_PARAMETERS;
991 
992 	region_descr = (struct ffa_mem_region *)((vaddr_t)buf +
993 						 region_descr_offs);
994 	*page_count = READ_ONCE(region_descr->total_page_count);
995 	*region_count = READ_ONCE(region_descr->address_range_count);
996 	*addr_range_offs = n;
997 	return 0;
998 }
999 
1000 static int add_mem_share_helper(struct mem_share_state *s, void *buf,
1001 				size_t flen)
1002 {
1003 	unsigned int region_count = flen / sizeof(struct ffa_address_range);
1004 	struct ffa_address_range *arange = NULL;
1005 	unsigned int n = 0;
1006 
1007 	if (region_count > s->region_count)
1008 		region_count = s->region_count;
1009 
1010 	if (!IS_ALIGNED_WITH_TYPE(buf, struct ffa_address_range))
1011 		return FFA_INVALID_PARAMETERS;
1012 	arange = buf;
1013 
1014 	for (n = 0; n < region_count; n++) {
1015 		unsigned int page_count = READ_ONCE(arange[n].page_count);
1016 		uint64_t addr = READ_ONCE(arange[n].address);
1017 
1018 		if (mobj_ffa_add_pages_at(s->mf, &s->current_page_idx,
1019 					  addr, page_count))
1020 			return FFA_INVALID_PARAMETERS;
1021 	}
1022 
1023 	s->region_count -= region_count;
1024 	if (s->region_count)
1025 		return region_count * sizeof(*arange);
1026 
1027 	if (s->current_page_idx != s->page_count)
1028 		return FFA_INVALID_PARAMETERS;
1029 
1030 	return 0;
1031 }
1032 
1033 static int add_mem_share_frag(struct mem_frag_state *s, void *buf, size_t flen)
1034 {
1035 	int rc = 0;
1036 
1037 	rc = add_mem_share_helper(&s->share, buf, flen);
1038 	if (rc >= 0) {
1039 		if (!ADD_OVERFLOW(s->frag_offset, rc, &s->frag_offset)) {
1040 			/* We're not at the end of the descriptor yet */
1041 			if (s->share.region_count)
1042 				return s->frag_offset;
1043 
1044 			/* We're done */
1045 			rc = 0;
1046 		} else {
1047 			rc = FFA_INVALID_PARAMETERS;
1048 		}
1049 	}
1050 
1051 	SLIST_REMOVE(&frag_state_head, s, mem_frag_state, link);
1052 	if (rc < 0)
1053 		mobj_ffa_sel1_spmc_delete(s->share.mf);
1054 	else
1055 		mobj_ffa_push_to_inactive(s->share.mf);
1056 	free(s);
1057 
1058 	return rc;
1059 }
1060 
1061 static bool is_sp_share(struct ffa_mem_transaction_x *mem_trans,
1062 			void *buf)
1063 {
1064 	struct ffa_mem_access_perm *perm = NULL;
1065 	struct ffa_mem_access *mem_acc = NULL;
1066 
1067 	if (!IS_ENABLED(CFG_SECURE_PARTITION))
1068 		return false;
1069 
1070 	if (mem_trans->mem_access_count < 1)
1071 		return false;
1072 
1073 	mem_acc = (void *)((vaddr_t)buf + mem_trans->mem_access_offs);
1074 	perm = &mem_acc->access_perm;
1075 
1076 	/*
1077 	 * perm->endpoint_id is read here only to check if the endpoint is
1078 	 * OP-TEE. We do read it later on again, but there are some additional
1079 	 * checks there to make sure that the data is correct.
1080 	 */
1081 	return READ_ONCE(perm->endpoint_id) != optee_endpoint_id;
1082 }
1083 
1084 static int add_mem_share(struct ffa_mem_transaction_x *mem_trans,
1085 			 tee_mm_entry_t *mm, void *buf, size_t blen,
1086 			 size_t flen, uint64_t *global_handle)
1087 {
1088 	int rc = 0;
1089 	struct mem_share_state share = { };
1090 	size_t addr_range_offs = 0;
1091 	uint64_t cookie = OPTEE_MSG_FMEM_INVALID_GLOBAL_ID;
1092 	size_t n = 0;
1093 
1094 	rc = mem_share_init(mem_trans, buf, flen, &share.page_count,
1095 			    &share.region_count, &addr_range_offs);
1096 	if (rc)
1097 		return rc;
1098 
1099 	if (MUL_OVERFLOW(share.region_count,
1100 			 sizeof(struct ffa_address_range), &n) ||
1101 	    ADD_OVERFLOW(n, addr_range_offs, &n) || n > blen)
1102 		return FFA_INVALID_PARAMETERS;
1103 
1104 	if (mem_trans->global_handle)
1105 		cookie = mem_trans->global_handle;
1106 	share.mf = mobj_ffa_sel1_spmc_new(cookie, share.page_count);
1107 	if (!share.mf)
1108 		return FFA_NO_MEMORY;
1109 
1110 	if (flen != blen) {
1111 		struct mem_frag_state *s = calloc(sizeof(*s), 1);
1112 
1113 		if (!s) {
1114 			rc = FFA_NO_MEMORY;
1115 			goto err;
1116 		}
1117 		s->share = share;
1118 		s->mm = mm;
1119 		s->frag_offset = addr_range_offs;
1120 
1121 		SLIST_INSERT_HEAD(&frag_state_head, s, link);
1122 		rc = add_mem_share_frag(s, (char *)buf + addr_range_offs,
1123 					flen - addr_range_offs);
1124 
1125 		if (rc >= 0)
1126 			*global_handle = mobj_ffa_get_cookie(share.mf);
1127 
1128 		return rc;
1129 	}
1130 
1131 	rc = add_mem_share_helper(&share, (char *)buf + addr_range_offs,
1132 				  flen - addr_range_offs);
1133 	if (rc) {
1134 		/*
1135 		 * Number of consumed bytes may be returned instead of 0 for
1136 		 * done.
1137 		 */
1138 		rc = FFA_INVALID_PARAMETERS;
1139 		goto err;
1140 	}
1141 
1142 	*global_handle = mobj_ffa_push_to_inactive(share.mf);
1143 
1144 	return 0;
1145 err:
1146 	mobj_ffa_sel1_spmc_delete(share.mf);
1147 	return rc;
1148 }
1149 
1150 static int handle_mem_share_tmem(paddr_t pbuf, size_t blen, size_t flen,
1151 				 unsigned int page_count,
1152 				 uint64_t *global_handle, struct ffa_rxtx *rxtx)
1153 {
1154 	struct ffa_mem_transaction_x mem_trans = { };
1155 	int rc = 0;
1156 	size_t len = 0;
1157 	void *buf = NULL;
1158 	tee_mm_entry_t *mm = NULL;
1159 	vaddr_t offs = pbuf & SMALL_PAGE_MASK;
1160 
1161 	if (MUL_OVERFLOW(page_count, SMALL_PAGE_SIZE, &len))
1162 		return FFA_INVALID_PARAMETERS;
1163 	if (!core_pbuf_is(CORE_MEM_NON_SEC, pbuf, len))
1164 		return FFA_INVALID_PARAMETERS;
1165 
1166 	/*
1167 	 * Check that the length reported in flen is covered by len even
1168 	 * if the offset is taken into account.
1169 	 */
1170 	if (len < flen || len - offs < flen)
1171 		return FFA_INVALID_PARAMETERS;
1172 
1173 	mm = tee_mm_alloc(&tee_mm_shm, len);
1174 	if (!mm)
1175 		return FFA_NO_MEMORY;
1176 
1177 	if (core_mmu_map_contiguous_pages(tee_mm_get_smem(mm), pbuf,
1178 					  page_count, MEM_AREA_NSEC_SHM)) {
1179 		rc = FFA_INVALID_PARAMETERS;
1180 		goto out;
1181 	}
1182 	buf = (void *)(tee_mm_get_smem(mm) + offs);
1183 
1184 	cpu_spin_lock(&rxtx->spinlock);
1185 	rc = spmc_read_mem_transaction(rxtx->ffa_vers, buf, flen, &mem_trans);
1186 	if (!rc && IS_ENABLED(CFG_NS_VIRTUALIZATION) &&
1187 	    virt_set_guest(mem_trans.sender_id))
1188 		rc = FFA_DENIED;
1189 	if (!rc)
1190 		rc = add_mem_share(&mem_trans, mm, buf, blen, flen,
1191 				   global_handle);
1192 	virt_unset_guest();
1193 	cpu_spin_unlock(&rxtx->spinlock);
1194 	if (rc > 0)
1195 		return rc;
1196 
1197 	core_mmu_unmap_pages(tee_mm_get_smem(mm), page_count);
1198 out:
1199 	tee_mm_free(mm);
1200 	return rc;
1201 }
1202 
1203 static int handle_mem_share_rxbuf(size_t blen, size_t flen,
1204 				  uint64_t *global_handle,
1205 				  struct ffa_rxtx *rxtx)
1206 {
1207 	struct ffa_mem_transaction_x mem_trans = { };
1208 	int rc = FFA_DENIED;
1209 
1210 	cpu_spin_lock(&rxtx->spinlock);
1211 
1212 	if (!rxtx->rx || flen > rxtx->size)
1213 		goto out;
1214 
1215 	rc = spmc_read_mem_transaction(rxtx->ffa_vers, rxtx->rx, flen,
1216 				       &mem_trans);
1217 	if (rc)
1218 		goto out;
1219 	if (is_sp_share(&mem_trans, rxtx->rx)) {
1220 		rc = spmc_sp_add_share(&mem_trans, rxtx, blen,
1221 				       global_handle, NULL);
1222 		goto out;
1223 	}
1224 
1225 	if (IS_ENABLED(CFG_NS_VIRTUALIZATION) &&
1226 	    virt_set_guest(mem_trans.sender_id))
1227 		goto out;
1228 
1229 	rc = add_mem_share(&mem_trans, NULL, rxtx->rx, blen, flen,
1230 			   global_handle);
1231 
1232 	virt_unset_guest();
1233 
1234 out:
1235 	cpu_spin_unlock(&rxtx->spinlock);
1236 
1237 	return rc;
1238 }
1239 
1240 static void handle_mem_share(struct thread_smc_args *args,
1241 			     struct ffa_rxtx *rxtx)
1242 {
1243 	uint32_t tot_len = args->a1;
1244 	uint32_t frag_len = args->a2;
1245 	uint64_t addr = args->a3;
1246 	uint32_t page_count = args->a4;
1247 	uint32_t ret_w1 = 0;
1248 	uint32_t ret_w2 = FFA_INVALID_PARAMETERS;
1249 	uint32_t ret_w3 = 0;
1250 	uint32_t ret_fid = FFA_ERROR;
1251 	uint64_t global_handle = 0;
1252 	int rc = 0;
1253 
1254 	/* Check that the MBZs are indeed 0 */
1255 	if (args->a5 || args->a6 || args->a7)
1256 		goto out;
1257 
1258 	/* Check that fragment length doesn't exceed total length */
1259 	if (frag_len > tot_len)
1260 		goto out;
1261 
1262 	/* Check for 32-bit calling convention */
1263 	if (args->a0 == FFA_MEM_SHARE_32)
1264 		addr &= UINT32_MAX;
1265 
1266 	if (!addr) {
1267 		/*
1268 		 * The memory transaction descriptor is passed via our rx
1269 		 * buffer.
1270 		 */
1271 		if (page_count)
1272 			goto out;
1273 		rc = handle_mem_share_rxbuf(tot_len, frag_len, &global_handle,
1274 					    rxtx);
1275 	} else {
1276 		rc = handle_mem_share_tmem(addr, tot_len, frag_len, page_count,
1277 					   &global_handle, rxtx);
1278 	}
1279 	if (rc < 0) {
1280 		ret_w2 = rc;
1281 	} else if (rc > 0) {
1282 		ret_fid = FFA_MEM_FRAG_RX;
1283 		ret_w3 = rc;
1284 		reg_pair_from_64(global_handle, &ret_w2, &ret_w1);
1285 	} else {
1286 		ret_fid = FFA_SUCCESS_32;
1287 		reg_pair_from_64(global_handle, &ret_w3, &ret_w2);
1288 	}
1289 out:
1290 	spmc_set_args(args, ret_fid, ret_w1, ret_w2, ret_w3, 0, 0);
1291 }
1292 
1293 static struct mem_frag_state *get_frag_state(uint64_t global_handle)
1294 {
1295 	struct mem_frag_state *s = NULL;
1296 
1297 	SLIST_FOREACH(s, &frag_state_head, link)
1298 		if (mobj_ffa_get_cookie(s->share.mf) == global_handle)
1299 			return s;
1300 
1301 	return NULL;
1302 }
1303 
1304 static void handle_mem_frag_tx(struct thread_smc_args *args,
1305 			       struct ffa_rxtx *rxtx)
1306 {
1307 	uint64_t global_handle = reg_pair_to_64(args->a2, args->a1);
1308 	size_t flen = args->a3;
1309 	uint32_t endpoint_id = args->a4;
1310 	struct mem_frag_state *s = NULL;
1311 	tee_mm_entry_t *mm = NULL;
1312 	unsigned int page_count = 0;
1313 	void *buf = NULL;
1314 	uint32_t ret_w1 = 0;
1315 	uint32_t ret_w2 = 0;
1316 	uint32_t ret_w3 = 0;
1317 	uint32_t ret_fid = 0;
1318 	int rc = 0;
1319 
1320 	if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) {
1321 		uint16_t guest_id = endpoint_id >> 16;
1322 
1323 		if (!guest_id || virt_set_guest(guest_id)) {
1324 			rc = FFA_INVALID_PARAMETERS;
1325 			goto out_set_rc;
1326 		}
1327 	}
1328 
1329 	/*
1330 	 * Currently we're only doing this for fragmented FFA_MEM_SHARE_*
1331 	 * requests.
1332 	 */
1333 
1334 	cpu_spin_lock(&rxtx->spinlock);
1335 
1336 	s = get_frag_state(global_handle);
1337 	if (!s) {
1338 		rc = FFA_INVALID_PARAMETERS;
1339 		goto out;
1340 	}
1341 
1342 	mm = s->mm;
1343 	if (mm) {
1344 		if (flen > tee_mm_get_bytes(mm)) {
1345 			rc = FFA_INVALID_PARAMETERS;
1346 			goto out;
1347 		}
1348 		page_count = s->share.page_count;
1349 		buf = (void *)tee_mm_get_smem(mm);
1350 	} else {
1351 		if (flen > rxtx->size) {
1352 			rc = FFA_INVALID_PARAMETERS;
1353 			goto out;
1354 		}
1355 		buf = rxtx->rx;
1356 	}
1357 
1358 	rc = add_mem_share_frag(s, buf, flen);
1359 out:
1360 	virt_unset_guest();
1361 	cpu_spin_unlock(&rxtx->spinlock);
1362 
1363 	if (rc <= 0 && mm) {
1364 		core_mmu_unmap_pages(tee_mm_get_smem(mm), page_count);
1365 		tee_mm_free(mm);
1366 	}
1367 
1368 out_set_rc:
1369 	if (rc < 0) {
1370 		ret_fid = FFA_ERROR;
1371 		ret_w2 = rc;
1372 	} else if (rc > 0) {
1373 		ret_fid = FFA_MEM_FRAG_RX;
1374 		ret_w3 = rc;
1375 		reg_pair_from_64(global_handle, &ret_w2, &ret_w1);
1376 	} else {
1377 		ret_fid = FFA_SUCCESS_32;
1378 		reg_pair_from_64(global_handle, &ret_w3, &ret_w2);
1379 	}
1380 
1381 	spmc_set_args(args, ret_fid, ret_w1, ret_w2, ret_w3, 0, 0);
1382 }
1383 
1384 static void handle_mem_reclaim(struct thread_smc_args *args)
1385 {
1386 	int rc = FFA_INVALID_PARAMETERS;
1387 	uint64_t cookie = 0;
1388 
1389 	if (args->a3 || args->a4 || args->a5 || args->a6 || args->a7)
1390 		goto out;
1391 
1392 	cookie = reg_pair_to_64(args->a2, args->a1);
1393 	if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) {
1394 		uint16_t guest_id = 0;
1395 
1396 		if (cookie & FFA_MEMORY_HANDLE_HYPERVISOR_BIT) {
1397 			guest_id = virt_find_guest_by_cookie(cookie);
1398 		} else {
1399 			guest_id = (cookie >> FFA_MEMORY_HANDLE_PRTN_SHIFT) &
1400 				   FFA_MEMORY_HANDLE_PRTN_MASK;
1401 		}
1402 		if (!guest_id)
1403 			goto out;
1404 		if (virt_set_guest(guest_id)) {
1405 			if (!virt_reclaim_cookie_from_destroyed_guest(guest_id,
1406 								      cookie))
1407 				rc = FFA_OK;
1408 			goto out;
1409 		}
1410 	}
1411 
1412 	switch (mobj_ffa_sel1_spmc_reclaim(cookie)) {
1413 	case TEE_SUCCESS:
1414 		rc = FFA_OK;
1415 		break;
1416 	case TEE_ERROR_ITEM_NOT_FOUND:
1417 		DMSG("cookie %#"PRIx64" not found", cookie);
1418 		rc = FFA_INVALID_PARAMETERS;
1419 		break;
1420 	default:
1421 		DMSG("cookie %#"PRIx64" busy", cookie);
1422 		rc = FFA_DENIED;
1423 		break;
1424 	}
1425 
1426 	virt_unset_guest();
1427 
1428 out:
1429 	set_simple_ret_val(args, rc);
1430 }
1431 
1432 static void handle_notification_bitmap_create(struct thread_smc_args *args)
1433 {
1434 	uint32_t ret_val = FFA_INVALID_PARAMETERS;
1435 	uint32_t ret_fid = FFA_ERROR;
1436 	uint32_t old_itr_status = 0;
1437 
1438 	if (!FFA_TARGET_INFO_GET_SP_ID(args->a1) && !args->a3 && !args->a4 &&
1439 	    !args->a5 && !args->a6 && !args->a7) {
1440 		uint16_t vm_id = args->a1;
1441 
1442 		old_itr_status = cpu_spin_lock_xsave(&spmc_notif_lock);
1443 
1444 		if (notif_vm_id_valid) {
1445 			if (vm_id == notif_vm_id)
1446 				ret_val = FFA_DENIED;
1447 			else
1448 				ret_val = FFA_NO_MEMORY;
1449 		} else {
1450 			notif_vm_id = vm_id;
1451 			notif_vm_id_valid = true;
1452 			ret_val = FFA_OK;
1453 			ret_fid = FFA_SUCCESS_32;
1454 		}
1455 
1456 		cpu_spin_unlock_xrestore(&spmc_notif_lock, old_itr_status);
1457 	}
1458 
1459 	spmc_set_args(args, ret_fid, 0, ret_val, 0, 0, 0);
1460 }
1461 
1462 static void handle_notification_bitmap_destroy(struct thread_smc_args *args)
1463 {
1464 	uint32_t ret_val = FFA_INVALID_PARAMETERS;
1465 	uint32_t ret_fid = FFA_ERROR;
1466 	uint32_t old_itr_status = 0;
1467 
1468 	if (!FFA_TARGET_INFO_GET_SP_ID(args->a1) && !args->a3 && !args->a4 &&
1469 	    !args->a5 && !args->a6 && !args->a7) {
1470 		uint16_t vm_id = args->a1;
1471 
1472 		old_itr_status = cpu_spin_lock_xsave(&spmc_notif_lock);
1473 
1474 		if (notif_vm_id_valid && vm_id == notif_vm_id) {
1475 			if (notif_pending_bitmap || notif_bound_bitmap) {
1476 				ret_val = FFA_DENIED;
1477 			} else {
1478 				notif_vm_id_valid = false;
1479 				ret_val = FFA_OK;
1480 				ret_fid = FFA_SUCCESS_32;
1481 			}
1482 		}
1483 
1484 		cpu_spin_unlock_xrestore(&spmc_notif_lock, old_itr_status);
1485 	}
1486 
1487 	spmc_set_args(args, ret_fid, 0, ret_val, 0, 0, 0);
1488 }
1489 
1490 static void handle_notification_bind(struct thread_smc_args *args)
1491 {
1492 	uint32_t ret_val = FFA_INVALID_PARAMETERS;
1493 	uint32_t ret_fid = FFA_ERROR;
1494 	uint32_t old_itr_status = 0;
1495 	uint64_t bitmap = 0;
1496 	uint16_t vm_id = 0;
1497 
1498 	if (args->a5 || args->a6 || args->a7)
1499 		goto out;
1500 	if (args->a2) {
1501 		/* We only deal with global notifications for now */
1502 		ret_val = FFA_NOT_SUPPORTED;
1503 		goto out;
1504 	}
1505 
1506 	/* The destination of the eventual notification */
1507 	vm_id = FFA_DST(args->a1);
1508 	bitmap = reg_pair_to_64(args->a4, args->a3);
1509 
1510 	old_itr_status = cpu_spin_lock_xsave(&spmc_notif_lock);
1511 
1512 	if (notif_vm_id_valid && vm_id == notif_vm_id) {
1513 		if (bitmap & notif_bound_bitmap) {
1514 			ret_val = FFA_DENIED;
1515 		} else {
1516 			notif_bound_bitmap |= bitmap;
1517 			ret_val = FFA_OK;
1518 			ret_fid = FFA_SUCCESS_32;
1519 		}
1520 	}
1521 
1522 	cpu_spin_unlock_xrestore(&spmc_notif_lock, old_itr_status);
1523 out:
1524 	spmc_set_args(args, ret_fid, 0, ret_val, 0, 0, 0);
1525 }
1526 
1527 static void handle_notification_unbind(struct thread_smc_args *args)
1528 {
1529 	uint32_t ret_val = FFA_INVALID_PARAMETERS;
1530 	uint32_t ret_fid = FFA_ERROR;
1531 	uint32_t old_itr_status = 0;
1532 	uint64_t bitmap = 0;
1533 	uint16_t vm_id = 0;
1534 
1535 	if (args->a2 || args->a5 || args->a6 || args->a7)
1536 		goto out;
1537 
1538 	/* The destination of the eventual notification */
1539 	vm_id = FFA_DST(args->a1);
1540 	bitmap = reg_pair_to_64(args->a4, args->a3);
1541 
1542 	old_itr_status = cpu_spin_lock_xsave(&spmc_notif_lock);
1543 
1544 	if (notif_vm_id_valid && vm_id == notif_vm_id) {
1545 		/*
1546 		 * Spec says:
1547 		 * At least one notification is bound to another Sender or
1548 		 * is currently pending.
1549 		 *
1550 		 * Not sure what the intention is.
1551 		 */
1552 		if (bitmap & notif_pending_bitmap) {
1553 			ret_val = FFA_DENIED;
1554 		} else {
1555 			notif_bound_bitmap &= ~bitmap;
1556 			ret_val = FFA_OK;
1557 			ret_fid = FFA_SUCCESS_32;
1558 		}
1559 	}
1560 
1561 	cpu_spin_unlock_xrestore(&spmc_notif_lock, old_itr_status);
1562 out:
1563 	spmc_set_args(args, ret_fid, 0, ret_val, 0, 0, 0);
1564 }
1565 
1566 static void handle_notification_get(struct thread_smc_args *args)
1567 {
1568 	uint32_t w2 = FFA_INVALID_PARAMETERS;
1569 	uint32_t ret_fid = FFA_ERROR;
1570 	uint32_t old_itr_status = 0;
1571 	uint16_t vm_id = 0;
1572 	uint32_t w3 = 0;
1573 
1574 	if (args->a5 || args->a6 || args->a7)
1575 		goto out;
1576 	if (!(args->a2 & 0x1)) {
1577 		ret_fid = FFA_SUCCESS_32;
1578 		w2 = 0;
1579 		goto out;
1580 	}
1581 	vm_id = FFA_DST(args->a1);
1582 
1583 	old_itr_status = cpu_spin_lock_xsave(&spmc_notif_lock);
1584 
1585 	if (notif_vm_id_valid && vm_id == notif_vm_id) {
1586 		reg_pair_from_64(notif_pending_bitmap, &w3, &w2);
1587 		notif_pending_bitmap = 0;
1588 		ret_fid = FFA_SUCCESS_32;
1589 	}
1590 
1591 	cpu_spin_unlock_xrestore(&spmc_notif_lock, old_itr_status);
1592 out:
1593 	spmc_set_args(args, ret_fid, 0, w2, w3, 0, 0);
1594 }
1595 
1596 static void handle_notification_info_get(struct thread_smc_args *args)
1597 {
1598 	uint32_t w2 = FFA_INVALID_PARAMETERS;
1599 	uint32_t ret_fid = FFA_ERROR;
1600 
1601 	if (args->a1 || args->a2 || args->a3 || args->a4 || args->a5 ||
1602 	    args->a6 || args->a7)
1603 		goto out;
1604 
1605 	if (OPTEE_SMC_IS_64(args->a0))
1606 		ret_fid = FFA_SUCCESS_64;
1607 	else
1608 		ret_fid = FFA_SUCCESS_32;
1609 
1610 	/*
1611 	 * Note, we're only supporting physical OS kernel in normal world
1612 	 * with Global Notifications.
1613 	 * So one list of ID list registers (BIT[11:7])
1614 	 * and one count of IDs (BIT[13:12] + 1)
1615 	 * and the VM is always 0.
1616 	 */
1617 	w2 = SHIFT_U32(1, 7);
1618 out:
1619 	spmc_set_args(args, ret_fid, 0, w2, 0, 0, 0);
1620 }
1621 
1622 void thread_spmc_set_async_notif_intid(int intid)
1623 {
1624 	assert(interrupt_can_raise_sgi(interrupt_get_main_chip()));
1625 	notif_intid = intid;
1626 	spmc_notif_is_ready = true;
1627 	DMSG("Asynchronous notifications are ready");
1628 }
1629 
1630 void notif_send_async(uint32_t value)
1631 {
1632 	uint32_t old_itr_status = 0;
1633 
1634 	old_itr_status = cpu_spin_lock_xsave(&spmc_notif_lock);
1635 	assert(value == NOTIF_VALUE_DO_BOTTOM_HALF && spmc_notif_is_ready &&
1636 	       do_bottom_half_value >= 0 && notif_intid >= 0);
1637 	notif_pending_bitmap |= BIT64(do_bottom_half_value);
1638 	interrupt_raise_sgi(interrupt_get_main_chip(), notif_intid,
1639 			    ITR_CPU_MASK_TO_THIS_CPU);
1640 	cpu_spin_unlock_xrestore(&spmc_notif_lock, old_itr_status);
1641 }
1642 #else
1643 void notif_send_async(uint32_t value)
1644 {
1645 	/* global notification, delay notification interrupt */
1646 	uint32_t flags = BIT32(1);
1647 	int res = 0;
1648 
1649 	assert(value == NOTIF_VALUE_DO_BOTTOM_HALF && spmc_notif_is_ready &&
1650 	       do_bottom_half_value >= 0);
1651 	res = ffa_set_notification(notif_vm_id, optee_endpoint_id, flags,
1652 				   BIT64(do_bottom_half_value));
1653 	if (res) {
1654 		EMSG("notification set failed with error %d", res);
1655 		panic();
1656 	}
1657 }
1658 #endif
1659 
1660 /* Only called from assembly */
1661 void thread_spmc_msg_recv(struct thread_smc_args *args);
1662 void thread_spmc_msg_recv(struct thread_smc_args *args)
1663 {
1664 	assert((thread_get_exceptions() & THREAD_EXCP_ALL) == THREAD_EXCP_ALL);
1665 	switch (args->a0) {
1666 #if defined(CFG_CORE_SEL1_SPMC)
1667 	case FFA_FEATURES:
1668 		handle_features(args);
1669 		break;
1670 	case FFA_SPM_ID_GET:
1671 		spmc_handle_spm_id_get(args);
1672 		break;
1673 #ifdef ARM64
1674 	case FFA_RXTX_MAP_64:
1675 #endif
1676 	case FFA_RXTX_MAP_32:
1677 		spmc_handle_rxtx_map(args, &my_rxtx);
1678 		break;
1679 	case FFA_RXTX_UNMAP:
1680 		spmc_handle_rxtx_unmap(args, &my_rxtx);
1681 		break;
1682 	case FFA_RX_RELEASE:
1683 		spmc_handle_rx_release(args, &my_rxtx);
1684 		break;
1685 	case FFA_PARTITION_INFO_GET:
1686 		spmc_handle_partition_info_get(args, &my_rxtx);
1687 		break;
1688 	case FFA_RUN:
1689 		spmc_handle_run(args);
1690 		break;
1691 #endif /*CFG_CORE_SEL1_SPMC*/
1692 	case FFA_INTERRUPT:
1693 		if (IS_ENABLED(CFG_CORE_SEL1_SPMC))
1694 			spmc_set_args(args, FFA_NORMAL_WORLD_RESUME, 0, 0, 0,
1695 				      0, 0);
1696 		else
1697 			spmc_set_args(args, FFA_MSG_WAIT, 0, 0, 0, 0, 0);
1698 		break;
1699 #ifdef ARM64
1700 	case FFA_MSG_SEND_DIRECT_REQ_64:
1701 #endif
1702 	case FFA_MSG_SEND_DIRECT_REQ_32:
1703 		handle_direct_request(args, &my_rxtx);
1704 		break;
1705 #if defined(CFG_CORE_SEL1_SPMC)
1706 #ifdef ARM64
1707 	case FFA_MEM_SHARE_64:
1708 #endif
1709 	case FFA_MEM_SHARE_32:
1710 		handle_mem_share(args, &my_rxtx);
1711 		break;
1712 	case FFA_MEM_RECLAIM:
1713 		if (!IS_ENABLED(CFG_SECURE_PARTITION) ||
1714 		    !ffa_mem_reclaim(args, NULL))
1715 			handle_mem_reclaim(args);
1716 		break;
1717 	case FFA_MEM_FRAG_TX:
1718 		handle_mem_frag_tx(args, &my_rxtx);
1719 		break;
1720 	case FFA_NOTIFICATION_BITMAP_CREATE:
1721 		handle_notification_bitmap_create(args);
1722 		break;
1723 	case FFA_NOTIFICATION_BITMAP_DESTROY:
1724 		handle_notification_bitmap_destroy(args);
1725 		break;
1726 	case FFA_NOTIFICATION_BIND:
1727 		handle_notification_bind(args);
1728 		break;
1729 	case FFA_NOTIFICATION_UNBIND:
1730 		handle_notification_unbind(args);
1731 		break;
1732 	case FFA_NOTIFICATION_GET:
1733 		handle_notification_get(args);
1734 		break;
1735 #ifdef ARM64
1736 	case FFA_NOTIFICATION_INFO_GET_64:
1737 #endif
1738 	case FFA_NOTIFICATION_INFO_GET_32:
1739 		handle_notification_info_get(args);
1740 		break;
1741 #endif /*CFG_CORE_SEL1_SPMC*/
1742 	case FFA_ERROR:
1743 		EMSG("Cannot handle FFA_ERROR(%d)", (int)args->a2);
1744 		if (!IS_ENABLED(CFG_CORE_SEL1_SPMC)) {
1745 			/*
1746 			 * The SPMC will return an FFA_ERROR back so better
1747 			 * panic() now than flooding the log.
1748 			 */
1749 			panic("FFA_ERROR from SPMC is fatal");
1750 		}
1751 		spmc_set_args(args, FFA_ERROR, FFA_PARAM_MBZ, FFA_NOT_SUPPORTED,
1752 			      FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ);
1753 		break;
1754 	default:
1755 		EMSG("Unhandled FFA function ID %#"PRIx32, (uint32_t)args->a0);
1756 		set_simple_ret_val(args, FFA_NOT_SUPPORTED);
1757 	}
1758 }
1759 
1760 static TEE_Result yielding_call_with_arg(uint64_t cookie, uint32_t offset)
1761 {
1762 	size_t sz_rpc = OPTEE_MSG_GET_ARG_SIZE(THREAD_RPC_MAX_NUM_PARAMS);
1763 	struct thread_ctx *thr = threads + thread_get_id();
1764 	TEE_Result res = TEE_ERROR_BAD_PARAMETERS;
1765 	struct optee_msg_arg *arg = NULL;
1766 	struct mobj *mobj = NULL;
1767 	uint32_t num_params = 0;
1768 	size_t sz = 0;
1769 
1770 	mobj = mobj_ffa_get_by_cookie(cookie, 0);
1771 	if (!mobj) {
1772 		EMSG("Can't find cookie %#"PRIx64, cookie);
1773 		return TEE_ERROR_BAD_PARAMETERS;
1774 	}
1775 
1776 	res = mobj_inc_map(mobj);
1777 	if (res)
1778 		goto out_put_mobj;
1779 
1780 	res = TEE_ERROR_BAD_PARAMETERS;
1781 	arg = mobj_get_va(mobj, offset, sizeof(*arg));
1782 	if (!arg)
1783 		goto out_dec_map;
1784 
1785 	num_params = READ_ONCE(arg->num_params);
1786 	if (num_params > OPTEE_MSG_MAX_NUM_PARAMS)
1787 		goto out_dec_map;
1788 
1789 	sz = OPTEE_MSG_GET_ARG_SIZE(num_params);
1790 
1791 	thr->rpc_arg = mobj_get_va(mobj, offset + sz, sz_rpc);
1792 	if (!thr->rpc_arg)
1793 		goto out_dec_map;
1794 
1795 	virt_on_stdcall();
1796 	res = tee_entry_std(arg, num_params);
1797 
1798 	thread_rpc_shm_cache_clear(&thr->shm_cache);
1799 	thr->rpc_arg = NULL;
1800 
1801 out_dec_map:
1802 	mobj_dec_map(mobj);
1803 out_put_mobj:
1804 	mobj_put(mobj);
1805 	return res;
1806 }
1807 
1808 /*
1809  * Helper routine for the assembly function thread_std_smc_entry()
1810  *
1811  * Note: this function is weak just to make link_dummies_paged.c happy.
1812  */
1813 uint32_t __weak __thread_std_smc_entry(uint32_t a0, uint32_t a1,
1814 				       uint32_t a2, uint32_t a3,
1815 				       uint32_t a4, uint32_t a5 __unused)
1816 {
1817 	/*
1818 	 * Arguments are supplied from handle_yielding_call() as:
1819 	 * a0 <- w1
1820 	 * a1 <- w3
1821 	 * a2 <- w4
1822 	 * a3 <- w5
1823 	 * a4 <- w6
1824 	 * a5 <- w7
1825 	 */
1826 	thread_get_tsd()->rpc_target_info = swap_src_dst(a0);
1827 	if (a1 == OPTEE_FFA_YIELDING_CALL_WITH_ARG)
1828 		return yielding_call_with_arg(reg_pair_to_64(a3, a2), a4);
1829 	return FFA_DENIED;
1830 }
1831 
1832 static bool set_fmem(struct optee_msg_param *param, struct thread_param *tpm)
1833 {
1834 	uint64_t offs = tpm->u.memref.offs;
1835 
1836 	param->attr = tpm->attr - THREAD_PARAM_ATTR_MEMREF_IN +
1837 		      OPTEE_MSG_ATTR_TYPE_FMEM_INPUT;
1838 
1839 	param->u.fmem.offs_low = offs;
1840 	param->u.fmem.offs_high = offs >> 32;
1841 	if (param->u.fmem.offs_high != offs >> 32)
1842 		return false;
1843 
1844 	param->u.fmem.size = tpm->u.memref.size;
1845 	if (tpm->u.memref.mobj) {
1846 		uint64_t cookie = mobj_get_cookie(tpm->u.memref.mobj);
1847 
1848 		/* If a mobj is passed it better be one with a valid cookie. */
1849 		if (cookie == OPTEE_MSG_FMEM_INVALID_GLOBAL_ID)
1850 			return false;
1851 		param->u.fmem.global_id = cookie;
1852 	} else {
1853 		param->u.fmem.global_id = OPTEE_MSG_FMEM_INVALID_GLOBAL_ID;
1854 	}
1855 
1856 	return true;
1857 }
1858 
1859 static uint32_t get_rpc_arg(uint32_t cmd, size_t num_params,
1860 			    struct thread_param *params,
1861 			    struct optee_msg_arg **arg_ret)
1862 {
1863 	size_t sz = OPTEE_MSG_GET_ARG_SIZE(THREAD_RPC_MAX_NUM_PARAMS);
1864 	struct thread_ctx *thr = threads + thread_get_id();
1865 	struct optee_msg_arg *arg = thr->rpc_arg;
1866 
1867 	if (num_params > THREAD_RPC_MAX_NUM_PARAMS)
1868 		return TEE_ERROR_BAD_PARAMETERS;
1869 
1870 	if (!arg) {
1871 		EMSG("rpc_arg not set");
1872 		return TEE_ERROR_GENERIC;
1873 	}
1874 
1875 	memset(arg, 0, sz);
1876 	arg->cmd = cmd;
1877 	arg->num_params = num_params;
1878 	arg->ret = TEE_ERROR_GENERIC; /* in case value isn't updated */
1879 
1880 	for (size_t n = 0; n < num_params; n++) {
1881 		switch (params[n].attr) {
1882 		case THREAD_PARAM_ATTR_NONE:
1883 			arg->params[n].attr = OPTEE_MSG_ATTR_TYPE_NONE;
1884 			break;
1885 		case THREAD_PARAM_ATTR_VALUE_IN:
1886 		case THREAD_PARAM_ATTR_VALUE_OUT:
1887 		case THREAD_PARAM_ATTR_VALUE_INOUT:
1888 			arg->params[n].attr = params[n].attr -
1889 					      THREAD_PARAM_ATTR_VALUE_IN +
1890 					      OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
1891 			arg->params[n].u.value.a = params[n].u.value.a;
1892 			arg->params[n].u.value.b = params[n].u.value.b;
1893 			arg->params[n].u.value.c = params[n].u.value.c;
1894 			break;
1895 		case THREAD_PARAM_ATTR_MEMREF_IN:
1896 		case THREAD_PARAM_ATTR_MEMREF_OUT:
1897 		case THREAD_PARAM_ATTR_MEMREF_INOUT:
1898 			if (!set_fmem(arg->params + n, params + n))
1899 				return TEE_ERROR_BAD_PARAMETERS;
1900 			break;
1901 		default:
1902 			return TEE_ERROR_BAD_PARAMETERS;
1903 		}
1904 	}
1905 
1906 	if (arg_ret)
1907 		*arg_ret = arg;
1908 
1909 	return TEE_SUCCESS;
1910 }
1911 
1912 static uint32_t get_rpc_arg_res(struct optee_msg_arg *arg, size_t num_params,
1913 				struct thread_param *params)
1914 {
1915 	for (size_t n = 0; n < num_params; n++) {
1916 		switch (params[n].attr) {
1917 		case THREAD_PARAM_ATTR_VALUE_OUT:
1918 		case THREAD_PARAM_ATTR_VALUE_INOUT:
1919 			params[n].u.value.a = arg->params[n].u.value.a;
1920 			params[n].u.value.b = arg->params[n].u.value.b;
1921 			params[n].u.value.c = arg->params[n].u.value.c;
1922 			break;
1923 		case THREAD_PARAM_ATTR_MEMREF_OUT:
1924 		case THREAD_PARAM_ATTR_MEMREF_INOUT:
1925 			params[n].u.memref.size = arg->params[n].u.fmem.size;
1926 			break;
1927 		default:
1928 			break;
1929 		}
1930 	}
1931 
1932 	return arg->ret;
1933 }
1934 
1935 uint32_t thread_rpc_cmd(uint32_t cmd, size_t num_params,
1936 			struct thread_param *params)
1937 {
1938 	struct thread_rpc_arg rpc_arg = { .call = {
1939 			.w1 = thread_get_tsd()->rpc_target_info,
1940 			.w4 = OPTEE_FFA_YIELDING_CALL_RETURN_RPC_CMD,
1941 		},
1942 	};
1943 	struct optee_msg_arg *arg = NULL;
1944 	uint32_t ret = 0;
1945 
1946 	ret = get_rpc_arg(cmd, num_params, params, &arg);
1947 	if (ret)
1948 		return ret;
1949 
1950 	thread_rpc(&rpc_arg);
1951 
1952 	return get_rpc_arg_res(arg, num_params, params);
1953 }
1954 
1955 static void thread_rpc_free(unsigned int bt, uint64_t cookie, struct mobj *mobj)
1956 {
1957 	struct thread_rpc_arg rpc_arg = { .call = {
1958 			.w1 = thread_get_tsd()->rpc_target_info,
1959 			.w4 = OPTEE_FFA_YIELDING_CALL_RETURN_RPC_CMD,
1960 		},
1961 	};
1962 	struct thread_param param = THREAD_PARAM_VALUE(IN, bt, cookie, 0);
1963 	uint32_t res2 = 0;
1964 	uint32_t res = 0;
1965 
1966 	DMSG("freeing cookie %#"PRIx64, cookie);
1967 
1968 	res = get_rpc_arg(OPTEE_RPC_CMD_SHM_FREE, 1, &param, NULL);
1969 
1970 	mobj_put(mobj);
1971 	res2 = mobj_ffa_unregister_by_cookie(cookie);
1972 	if (res2)
1973 		DMSG("mobj_ffa_unregister_by_cookie(%#"PRIx64"): %#"PRIx32,
1974 		     cookie, res2);
1975 	if (!res)
1976 		thread_rpc(&rpc_arg);
1977 }
1978 
1979 static struct mobj *thread_rpc_alloc(size_t size, size_t align, unsigned int bt)
1980 {
1981 	struct thread_rpc_arg rpc_arg = { .call = {
1982 			.w1 = thread_get_tsd()->rpc_target_info,
1983 			.w4 = OPTEE_FFA_YIELDING_CALL_RETURN_RPC_CMD,
1984 		},
1985 	};
1986 	struct thread_param param = THREAD_PARAM_VALUE(IN, bt, size, align);
1987 	struct optee_msg_arg *arg = NULL;
1988 	unsigned int internal_offset = 0;
1989 	struct mobj *mobj = NULL;
1990 	uint64_t cookie = 0;
1991 
1992 	if (get_rpc_arg(OPTEE_RPC_CMD_SHM_ALLOC, 1, &param, &arg))
1993 		return NULL;
1994 
1995 	thread_rpc(&rpc_arg);
1996 
1997 	if (arg->num_params != 1 ||
1998 	    arg->params->attr != OPTEE_MSG_ATTR_TYPE_FMEM_OUTPUT)
1999 		return NULL;
2000 
2001 	internal_offset = READ_ONCE(arg->params->u.fmem.internal_offs);
2002 	cookie = READ_ONCE(arg->params->u.fmem.global_id);
2003 	mobj = mobj_ffa_get_by_cookie(cookie, internal_offset);
2004 	if (!mobj) {
2005 		DMSG("mobj_ffa_get_by_cookie(%#"PRIx64", %#x): failed",
2006 		     cookie, internal_offset);
2007 		return NULL;
2008 	}
2009 
2010 	assert(mobj_is_nonsec(mobj));
2011 
2012 	if (mobj->size < size) {
2013 		DMSG("Mobj %#"PRIx64": wrong size", cookie);
2014 		mobj_put(mobj);
2015 		return NULL;
2016 	}
2017 
2018 	if (mobj_inc_map(mobj)) {
2019 		DMSG("mobj_inc_map(%#"PRIx64"): failed", cookie);
2020 		mobj_put(mobj);
2021 		return NULL;
2022 	}
2023 
2024 	return mobj;
2025 }
2026 
2027 struct mobj *thread_rpc_alloc_payload(size_t size)
2028 {
2029 	return thread_rpc_alloc(size, 8, OPTEE_RPC_SHM_TYPE_APPL);
2030 }
2031 
2032 struct mobj *thread_rpc_alloc_kernel_payload(size_t size)
2033 {
2034 	return thread_rpc_alloc(size, 8, OPTEE_RPC_SHM_TYPE_KERNEL);
2035 }
2036 
2037 void thread_rpc_free_kernel_payload(struct mobj *mobj)
2038 {
2039 	if (mobj)
2040 		thread_rpc_free(OPTEE_RPC_SHM_TYPE_KERNEL,
2041 				mobj_get_cookie(mobj), mobj);
2042 }
2043 
2044 void thread_rpc_free_payload(struct mobj *mobj)
2045 {
2046 	if (mobj)
2047 		thread_rpc_free(OPTEE_RPC_SHM_TYPE_APPL, mobj_get_cookie(mobj),
2048 				mobj);
2049 }
2050 
2051 struct mobj *thread_rpc_alloc_global_payload(size_t size)
2052 {
2053 	return thread_rpc_alloc(size, 8, OPTEE_RPC_SHM_TYPE_GLOBAL);
2054 }
2055 
2056 void thread_rpc_free_global_payload(struct mobj *mobj)
2057 {
2058 	if (mobj)
2059 		thread_rpc_free(OPTEE_RPC_SHM_TYPE_GLOBAL,
2060 				mobj_get_cookie(mobj), mobj);
2061 }
2062 
2063 void thread_spmc_register_secondary_ep(vaddr_t ep)
2064 {
2065 	unsigned long ret = 0;
2066 
2067 	/* Let the SPM know the entry point for secondary CPUs */
2068 	ret = thread_smc(FFA_SECONDARY_EP_REGISTER_64, ep, 0, 0);
2069 
2070 	if (ret != FFA_SUCCESS_32 && ret != FFA_SUCCESS_64)
2071 		EMSG("FFA_SECONDARY_EP_REGISTER_64 ret %#lx", ret);
2072 }
2073 
2074 static uint16_t ffa_id_get(void)
2075 {
2076 	/*
2077 	 * Ask the SPM component running at a higher EL to return our FF-A ID.
2078 	 * This can either be the SPMC ID (if the SPMC is enabled in OP-TEE) or
2079 	 * the partition ID (if not).
2080 	 */
2081 	struct thread_smc_args args = {
2082 		.a0 = FFA_ID_GET,
2083 	};
2084 
2085 	thread_smccc(&args);
2086 	if (!is_ffa_success(args.a0)) {
2087 		if (args.a0 == FFA_ERROR)
2088 			EMSG("Get id failed with error %ld", args.a2);
2089 		else
2090 			EMSG("Get id failed");
2091 		panic();
2092 	}
2093 
2094 	return args.a2;
2095 }
2096 
2097 static uint16_t ffa_spm_id_get(void)
2098 {
2099 	/*
2100 	 * Ask the SPM component running at a higher EL to return its ID.
2101 	 * If OP-TEE implements the S-EL1 SPMC, this will get the SPMD ID.
2102 	 * If not, the ID of the SPMC will be returned.
2103 	 */
2104 	struct thread_smc_args args = {
2105 		.a0 = FFA_SPM_ID_GET,
2106 	};
2107 
2108 	thread_smccc(&args);
2109 	if (!is_ffa_success(args.a0)) {
2110 		if (args.a0 == FFA_ERROR)
2111 			EMSG("Get spm id failed with error %ld", args.a2);
2112 		else
2113 			EMSG("Get spm id failed");
2114 		panic();
2115 	}
2116 
2117 	return args.a2;
2118 }
2119 
2120 #if defined(CFG_CORE_SEL1_SPMC)
2121 static TEE_Result spmc_init(void)
2122 {
2123 	spmd_id = ffa_spm_id_get();
2124 	DMSG("SPMD ID %#"PRIx16, spmd_id);
2125 
2126 	spmc_id = ffa_id_get();
2127 	DMSG("SPMC ID %#"PRIx16, spmc_id);
2128 
2129 	optee_endpoint_id = FFA_SWD_ID_MIN;
2130 	while (optee_endpoint_id == spmd_id || optee_endpoint_id == spmc_id)
2131 		optee_endpoint_id++;
2132 
2133 	DMSG("OP-TEE endpoint ID %#"PRIx16, optee_endpoint_id);
2134 
2135 	/*
2136 	 * If SPMD think we are version 1.0 it will report version 1.0 to
2137 	 * normal world regardless of what version we query the SPM with.
2138 	 * However, if SPMD think we are version 1.1 it will forward
2139 	 * queries from normal world to let us negotiate version. So by
2140 	 * setting version 1.0 here we should be compatible.
2141 	 *
2142 	 * Note that disagreement on negotiated version means that we'll
2143 	 * have communication problems with normal world.
2144 	 */
2145 	my_rxtx.ffa_vers = FFA_VERSION_1_0;
2146 
2147 	return TEE_SUCCESS;
2148 }
2149 #else /* !defined(CFG_CORE_SEL1_SPMC) */
2150 static void spmc_rxtx_map(struct ffa_rxtx *rxtx)
2151 {
2152 	struct thread_smc_args args = {
2153 #ifdef ARM64
2154 		.a0 = FFA_RXTX_MAP_64,
2155 #else
2156 		.a0 = FFA_RXTX_MAP_32,
2157 #endif
2158 		.a1 = virt_to_phys(rxtx->tx),
2159 		.a2 = virt_to_phys(rxtx->rx),
2160 		.a3 = 1,
2161 	};
2162 
2163 	thread_smccc(&args);
2164 	if (!is_ffa_success(args.a0)) {
2165 		if (args.a0 == FFA_ERROR)
2166 			EMSG("rxtx map failed with error %ld", args.a2);
2167 		else
2168 			EMSG("rxtx map failed");
2169 		panic();
2170 	}
2171 }
2172 
2173 static uint32_t get_ffa_version(uint32_t my_version)
2174 {
2175 	struct thread_smc_args args = {
2176 		.a0 = FFA_VERSION,
2177 		.a1 = my_version,
2178 	};
2179 
2180 	thread_smccc(&args);
2181 	if (args.a0 & BIT(31)) {
2182 		EMSG("FF-A version failed with error %ld", args.a0);
2183 		panic();
2184 	}
2185 
2186 	return args.a0;
2187 }
2188 
2189 static void *spmc_retrieve_req(uint64_t cookie,
2190 			       struct ffa_mem_transaction_x *trans)
2191 {
2192 	struct ffa_mem_access *acc_descr_array = NULL;
2193 	struct ffa_mem_access_perm *perm_descr = NULL;
2194 	struct thread_smc_args args = {
2195 		.a0 = FFA_MEM_RETRIEVE_REQ_32,
2196 		.a3 =	0,	/* Address, Using TX -> MBZ */
2197 		.a4 =   0,	/* Using TX -> MBZ */
2198 	};
2199 	size_t size = 0;
2200 	int rc = 0;
2201 
2202 	if (my_rxtx.ffa_vers == FFA_VERSION_1_0) {
2203 		struct ffa_mem_transaction_1_0 *trans_descr = my_rxtx.tx;
2204 
2205 		size = sizeof(*trans_descr) + 1 * sizeof(struct ffa_mem_access);
2206 		memset(trans_descr, 0, size);
2207 		trans_descr->sender_id = thread_get_tsd()->rpc_target_info;
2208 		trans_descr->mem_reg_attr = FFA_NORMAL_MEM_REG_ATTR;
2209 		trans_descr->global_handle = cookie;
2210 		trans_descr->flags = FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE |
2211 				     FFA_MEMORY_REGION_FLAG_ANY_ALIGNMENT;
2212 		trans_descr->mem_access_count = 1;
2213 		acc_descr_array = trans_descr->mem_access_array;
2214 	} else {
2215 		struct ffa_mem_transaction_1_1 *trans_descr = my_rxtx.tx;
2216 
2217 		size = sizeof(*trans_descr) + 1 * sizeof(struct ffa_mem_access);
2218 		memset(trans_descr, 0, size);
2219 		trans_descr->sender_id = thread_get_tsd()->rpc_target_info;
2220 		trans_descr->mem_reg_attr = FFA_NORMAL_MEM_REG_ATTR;
2221 		trans_descr->global_handle = cookie;
2222 		trans_descr->flags = FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE |
2223 				     FFA_MEMORY_REGION_FLAG_ANY_ALIGNMENT;
2224 		trans_descr->mem_access_count = 1;
2225 		trans_descr->mem_access_offs = sizeof(*trans_descr);
2226 		trans_descr->mem_access_size = sizeof(struct ffa_mem_access);
2227 		acc_descr_array = (void *)((vaddr_t)my_rxtx.tx +
2228 					   sizeof(*trans_descr));
2229 	}
2230 	acc_descr_array->region_offs = 0;
2231 	acc_descr_array->reserved = 0;
2232 	perm_descr = &acc_descr_array->access_perm;
2233 	perm_descr->endpoint_id = optee_endpoint_id;
2234 	perm_descr->perm = FFA_MEM_ACC_RW;
2235 	perm_descr->flags = 0;
2236 
2237 	args.a1 = size; /* Total Length */
2238 	args.a2 = size; /* Frag Length == Total length */
2239 	thread_smccc(&args);
2240 	if (args.a0 != FFA_MEM_RETRIEVE_RESP) {
2241 		if (args.a0 == FFA_ERROR)
2242 			EMSG("Failed to fetch cookie %#"PRIx64" error code %d",
2243 			     cookie, (int)args.a2);
2244 		else
2245 			EMSG("Failed to fetch cookie %#"PRIx64" a0 %#"PRIx64,
2246 			     cookie, args.a0);
2247 		return NULL;
2248 	}
2249 	rc = spmc_read_mem_transaction(my_rxtx.ffa_vers, my_rxtx.rx,
2250 				       my_rxtx.size, trans);
2251 	if (rc) {
2252 		EMSG("Memory transaction failure for cookie %#"PRIx64" rc %d",
2253 		     cookie, rc);
2254 		return NULL;
2255 	}
2256 
2257 	return my_rxtx.rx;
2258 }
2259 
2260 void thread_spmc_relinquish(uint64_t cookie)
2261 {
2262 	struct ffa_mem_relinquish *relinquish_desc = my_rxtx.tx;
2263 	struct thread_smc_args args = {
2264 		.a0 = FFA_MEM_RELINQUISH,
2265 	};
2266 
2267 	memset(relinquish_desc, 0, sizeof(*relinquish_desc));
2268 	relinquish_desc->handle = cookie;
2269 	relinquish_desc->flags = 0;
2270 	relinquish_desc->endpoint_count = 1;
2271 	relinquish_desc->endpoint_id_array[0] = optee_endpoint_id;
2272 	thread_smccc(&args);
2273 	if (!is_ffa_success(args.a0))
2274 		EMSG("Failed to relinquish cookie %#"PRIx64, cookie);
2275 }
2276 
2277 static int set_pages(struct ffa_address_range *regions,
2278 		     unsigned int num_regions, unsigned int num_pages,
2279 		     struct mobj_ffa *mf)
2280 {
2281 	unsigned int n = 0;
2282 	unsigned int idx = 0;
2283 
2284 	for (n = 0; n < num_regions; n++) {
2285 		unsigned int page_count = READ_ONCE(regions[n].page_count);
2286 		uint64_t addr = READ_ONCE(regions[n].address);
2287 
2288 		if (mobj_ffa_add_pages_at(mf, &idx, addr, page_count))
2289 			return FFA_INVALID_PARAMETERS;
2290 	}
2291 
2292 	if (idx != num_pages)
2293 		return FFA_INVALID_PARAMETERS;
2294 
2295 	return 0;
2296 }
2297 
2298 struct mobj_ffa *thread_spmc_populate_mobj_from_rx(uint64_t cookie)
2299 {
2300 	struct mobj_ffa *ret = NULL;
2301 	struct ffa_mem_transaction_x retrieve_desc = { };
2302 	struct ffa_mem_access *descr_array = NULL;
2303 	struct ffa_mem_region *descr = NULL;
2304 	struct mobj_ffa *mf = NULL;
2305 	unsigned int num_pages = 0;
2306 	unsigned int offs = 0;
2307 	void *buf = NULL;
2308 	struct thread_smc_args ffa_rx_release_args = {
2309 		.a0 = FFA_RX_RELEASE
2310 	};
2311 
2312 	/*
2313 	 * OP-TEE is only supporting a single mem_region while the
2314 	 * specification allows for more than one.
2315 	 */
2316 	buf = spmc_retrieve_req(cookie, &retrieve_desc);
2317 	if (!buf) {
2318 		EMSG("Failed to retrieve cookie from rx buffer %#"PRIx64,
2319 		     cookie);
2320 		return NULL;
2321 	}
2322 
2323 	descr_array = (void *)((vaddr_t)buf + retrieve_desc.mem_access_offs);
2324 	offs = READ_ONCE(descr_array->region_offs);
2325 	descr = (struct ffa_mem_region *)((vaddr_t)buf + offs);
2326 
2327 	num_pages = READ_ONCE(descr->total_page_count);
2328 	mf = mobj_ffa_spmc_new(cookie, num_pages);
2329 	if (!mf)
2330 		goto out;
2331 
2332 	if (set_pages(descr->address_range_array,
2333 		      READ_ONCE(descr->address_range_count), num_pages, mf)) {
2334 		mobj_ffa_spmc_delete(mf);
2335 		goto out;
2336 	}
2337 
2338 	ret = mf;
2339 
2340 out:
2341 	/* Release RX buffer after the mem retrieve request. */
2342 	thread_smccc(&ffa_rx_release_args);
2343 
2344 	return ret;
2345 }
2346 
2347 static TEE_Result spmc_init(void)
2348 {
2349 	unsigned int major = 0;
2350 	unsigned int minor __maybe_unused = 0;
2351 	uint32_t my_vers = 0;
2352 	uint32_t vers = 0;
2353 
2354 	my_vers = MAKE_FFA_VERSION(FFA_VERSION_MAJOR, FFA_VERSION_MINOR);
2355 	vers = get_ffa_version(my_vers);
2356 	major = (vers >> FFA_VERSION_MAJOR_SHIFT) & FFA_VERSION_MAJOR_MASK;
2357 	minor = (vers >> FFA_VERSION_MINOR_SHIFT) & FFA_VERSION_MINOR_MASK;
2358 	DMSG("SPMC reported version %u.%u", major, minor);
2359 	if (major != FFA_VERSION_MAJOR) {
2360 		EMSG("Incompatible major version %u, expected %u",
2361 		     major, FFA_VERSION_MAJOR);
2362 		panic();
2363 	}
2364 	if (vers < my_vers)
2365 		my_vers = vers;
2366 	DMSG("Using version %u.%u",
2367 	     (my_vers >> FFA_VERSION_MAJOR_SHIFT) & FFA_VERSION_MAJOR_MASK,
2368 	     (my_vers >> FFA_VERSION_MINOR_SHIFT) & FFA_VERSION_MINOR_MASK);
2369 	my_rxtx.ffa_vers = my_vers;
2370 
2371 	spmc_rxtx_map(&my_rxtx);
2372 
2373 	spmc_id = ffa_spm_id_get();
2374 	DMSG("SPMC ID %#"PRIx16, spmc_id);
2375 
2376 	optee_endpoint_id = ffa_id_get();
2377 	DMSG("OP-TEE endpoint ID %#"PRIx16, optee_endpoint_id);
2378 
2379 	if (!ffa_features(FFA_NOTIFICATION_SET)) {
2380 		spmc_notif_is_ready = true;
2381 		DMSG("Asynchronous notifications are ready");
2382 	}
2383 
2384 	return TEE_SUCCESS;
2385 }
2386 #endif /* !defined(CFG_CORE_SEL1_SPMC) */
2387 
2388 /*
2389  * boot_final() is always done before exiting at end of boot
2390  * initialization.  In case of virtualization the init-calls are done only
2391  * once a OP-TEE partition has been created. So with virtualization we have
2392  * to initialize via boot_final() to make sure we have a value assigned
2393  * before it's used the first time.
2394  */
2395 #ifdef CFG_NS_VIRTUALIZATION
2396 boot_final(spmc_init);
2397 #else
2398 service_init(spmc_init);
2399 #endif
2400