xref: /optee_os/core/arch/arm/kernel/thread_spmc.c (revision 19ad526cb1391f8489ea75c89ae7f97c8a630ffa)
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, TEE_IMPL_GIT_SHA1);
752 		break;
753 	case OPTEE_FFA_EXCHANGE_CAPABILITIES:
754 		sec_caps = OPTEE_FFA_SEC_CAP_ARG_OFFSET;
755 		if (spmc_notif_is_ready)
756 			sec_caps |= OPTEE_FFA_SEC_CAP_ASYNC_NOTIF;
757 		spmc_set_args(args, direct_resp_fid,
758 			      swap_src_dst(args->a1), 0, 0,
759 			      THREAD_RPC_MAX_NUM_PARAMS, sec_caps);
760 		break;
761 	case OPTEE_FFA_UNREGISTER_SHM:
762 		spmc_set_args(args, direct_resp_fid, swap_src_dst(args->a1), 0,
763 			      handle_unregister_shm(args->a4, args->a5), 0, 0);
764 		break;
765 	case OPTEE_FFA_ENABLE_ASYNC_NOTIF:
766 		spmc_set_args(args, direct_resp_fid,
767 			      swap_src_dst(args->a1), 0,
768 			      spmc_enable_async_notif(args->a4,
769 						      FFA_SRC(args->a1)),
770 			      0, 0);
771 		break;
772 	default:
773 		EMSG("Unhandled blocking service ID %#"PRIx32,
774 		     (uint32_t)args->a3);
775 		spmc_set_args(args, direct_resp_fid, swap_src_dst(args->a1), 0,
776 			      TEE_ERROR_BAD_PARAMETERS, 0, 0);
777 	}
778 }
779 
780 static void handle_framework_direct_request(struct thread_smc_args *args,
781 					    struct ffa_rxtx *rxtx,
782 					    uint32_t direct_resp_fid)
783 {
784 	uint32_t w0 = FFA_ERROR;
785 	uint32_t w1 = FFA_PARAM_MBZ;
786 	uint32_t w2 = FFA_NOT_SUPPORTED;
787 	uint32_t w3 = FFA_PARAM_MBZ;
788 
789 	switch (args->a2 & FFA_MSG_TYPE_MASK) {
790 	case FFA_MSG_SEND_VM_CREATED:
791 		if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) {
792 			uint16_t guest_id = args->a5;
793 			TEE_Result res = virt_guest_created(guest_id);
794 
795 			w0 = direct_resp_fid;
796 			w1 = swap_src_dst(args->a1);
797 			w2 = FFA_MSG_FLAG_FRAMEWORK | FFA_MSG_RESP_VM_CREATED;
798 			if (res == TEE_SUCCESS)
799 				w3 = FFA_OK;
800 			else if (res == TEE_ERROR_OUT_OF_MEMORY)
801 				w3 = FFA_DENIED;
802 			else
803 				w3 = FFA_INVALID_PARAMETERS;
804 		}
805 		break;
806 	case FFA_MSG_SEND_VM_DESTROYED:
807 		if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) {
808 			uint16_t guest_id = args->a5;
809 			TEE_Result res = virt_guest_destroyed(guest_id);
810 
811 			w0 = direct_resp_fid;
812 			w1 = swap_src_dst(args->a1);
813 			w2 = FFA_MSG_FLAG_FRAMEWORK | FFA_MSG_RESP_VM_DESTROYED;
814 			if (res == TEE_SUCCESS)
815 				w3 = FFA_OK;
816 			else
817 				w3 = FFA_INVALID_PARAMETERS;
818 		}
819 		break;
820 	case FFA_MSG_VERSION_REQ:
821 		w0 = direct_resp_fid;
822 		w1 = swap_src_dst(args->a1);
823 		w2 = FFA_MSG_FLAG_FRAMEWORK | FFA_MSG_VERSION_RESP;
824 		w3 = spmc_exchange_version(args->a3, rxtx);
825 		break;
826 	default:
827 		break;
828 	}
829 	spmc_set_args(args, w0, w1, w2, w3, FFA_PARAM_MBZ, FFA_PARAM_MBZ);
830 }
831 
832 static void handle_direct_request(struct thread_smc_args *args,
833 				  struct ffa_rxtx *rxtx)
834 {
835 	uint32_t direct_resp_fid = 0;
836 
837 	if (IS_ENABLED(CFG_SECURE_PARTITION) &&
838 	    FFA_DST(args->a1) != optee_endpoint_id) {
839 		spmc_sp_start_thread(args);
840 		return;
841 	}
842 
843 	if (OPTEE_SMC_IS_64(args->a0))
844 		direct_resp_fid = FFA_MSG_SEND_DIRECT_RESP_64;
845 	else
846 		direct_resp_fid = FFA_MSG_SEND_DIRECT_RESP_32;
847 
848 	if (args->a2 & FFA_MSG_FLAG_FRAMEWORK) {
849 		handle_framework_direct_request(args, rxtx, direct_resp_fid);
850 		return;
851 	}
852 
853 	if (IS_ENABLED(CFG_NS_VIRTUALIZATION) &&
854 	    virt_set_guest(get_sender_id(args->a1))) {
855 		spmc_set_args(args, direct_resp_fid, swap_src_dst(args->a1), 0,
856 			      TEE_ERROR_ITEM_NOT_FOUND, 0, 0);
857 		return;
858 	}
859 
860 	if (args->a3 & BIT32(OPTEE_FFA_YIELDING_CALL_BIT))
861 		handle_yielding_call(args, direct_resp_fid);
862 	else
863 		handle_blocking_call(args, direct_resp_fid);
864 
865 	/*
866 	 * Note that handle_yielding_call() typically only returns if a
867 	 * thread cannot be allocated or found. virt_unset_guest() is also
868 	 * called from thread_state_suspend() and thread_state_free().
869 	 */
870 	virt_unset_guest();
871 }
872 
873 int spmc_read_mem_transaction(uint32_t ffa_vers, void *buf, size_t blen,
874 			      struct ffa_mem_transaction_x *trans)
875 {
876 	uint16_t mem_reg_attr = 0;
877 	uint32_t flags = 0;
878 	uint32_t count = 0;
879 	uint32_t offs = 0;
880 	uint32_t size = 0;
881 	size_t n = 0;
882 
883 	if (!IS_ALIGNED_WITH_TYPE(buf, uint64_t))
884 		return FFA_INVALID_PARAMETERS;
885 
886 	if (ffa_vers >= FFA_VERSION_1_1) {
887 		struct ffa_mem_transaction_1_1 *descr = NULL;
888 
889 		if (blen < sizeof(*descr))
890 			return FFA_INVALID_PARAMETERS;
891 
892 		descr = buf;
893 		trans->sender_id = READ_ONCE(descr->sender_id);
894 		mem_reg_attr = READ_ONCE(descr->mem_reg_attr);
895 		flags = READ_ONCE(descr->flags);
896 		trans->global_handle = READ_ONCE(descr->global_handle);
897 		trans->tag = READ_ONCE(descr->tag);
898 
899 		count = READ_ONCE(descr->mem_access_count);
900 		size = READ_ONCE(descr->mem_access_size);
901 		offs = READ_ONCE(descr->mem_access_offs);
902 	} else {
903 		struct ffa_mem_transaction_1_0 *descr = NULL;
904 
905 		if (blen < sizeof(*descr))
906 			return FFA_INVALID_PARAMETERS;
907 
908 		descr = buf;
909 		trans->sender_id = READ_ONCE(descr->sender_id);
910 		mem_reg_attr = READ_ONCE(descr->mem_reg_attr);
911 		flags = READ_ONCE(descr->flags);
912 		trans->global_handle = READ_ONCE(descr->global_handle);
913 		trans->tag = READ_ONCE(descr->tag);
914 
915 		count = READ_ONCE(descr->mem_access_count);
916 		size = sizeof(struct ffa_mem_access);
917 		offs = offsetof(struct ffa_mem_transaction_1_0,
918 				mem_access_array);
919 	}
920 
921 	if (mem_reg_attr > UINT8_MAX || flags > UINT8_MAX ||
922 	    size > UINT8_MAX || count > UINT8_MAX || offs > UINT16_MAX)
923 		return FFA_INVALID_PARAMETERS;
924 
925 	/* Check that the endpoint memory access descriptor array fits */
926 	if (MUL_OVERFLOW(size, count, &n) || ADD_OVERFLOW(offs, n, &n) ||
927 	    n > blen)
928 		return FFA_INVALID_PARAMETERS;
929 
930 	trans->mem_reg_attr = mem_reg_attr;
931 	trans->flags = flags;
932 	trans->mem_access_size = size;
933 	trans->mem_access_count = count;
934 	trans->mem_access_offs = offs;
935 	return 0;
936 }
937 
938 #if defined(CFG_CORE_SEL1_SPMC)
939 static int get_acc_perms(vaddr_t mem_acc_base, unsigned int mem_access_size,
940 			 unsigned int mem_access_count, uint8_t *acc_perms,
941 			 unsigned int *region_offs)
942 {
943 	struct ffa_mem_access_perm *descr = NULL;
944 	struct ffa_mem_access *mem_acc = NULL;
945 	unsigned int n = 0;
946 
947 	for (n = 0; n < mem_access_count; n++) {
948 		mem_acc = (void *)(mem_acc_base + mem_access_size * n);
949 		descr = &mem_acc->access_perm;
950 		if (READ_ONCE(descr->endpoint_id) == optee_endpoint_id) {
951 			*acc_perms = READ_ONCE(descr->perm);
952 			*region_offs = READ_ONCE(mem_acc[n].region_offs);
953 			return 0;
954 		}
955 	}
956 
957 	return FFA_INVALID_PARAMETERS;
958 }
959 
960 static int mem_share_init(struct ffa_mem_transaction_x *mem_trans, void *buf,
961 			  size_t blen, unsigned int *page_count,
962 			  unsigned int *region_count, size_t *addr_range_offs)
963 {
964 	const uint16_t exp_mem_reg_attr = FFA_NORMAL_MEM_REG_ATTR;
965 	const uint8_t exp_mem_acc_perm = FFA_MEM_ACC_RW;
966 	struct ffa_mem_region *region_descr = NULL;
967 	unsigned int region_descr_offs = 0;
968 	uint8_t mem_acc_perm = 0;
969 	size_t n = 0;
970 
971 	if (mem_trans->mem_reg_attr != exp_mem_reg_attr)
972 		return FFA_INVALID_PARAMETERS;
973 
974 	/* Check that the access permissions matches what's expected */
975 	if (get_acc_perms((vaddr_t)buf + mem_trans->mem_access_offs,
976 			  mem_trans->mem_access_size,
977 			  mem_trans->mem_access_count,
978 			  &mem_acc_perm, &region_descr_offs) ||
979 	    mem_acc_perm != exp_mem_acc_perm)
980 		return FFA_INVALID_PARAMETERS;
981 
982 	/* Check that the Composite memory region descriptor fits */
983 	if (ADD_OVERFLOW(region_descr_offs, sizeof(*region_descr), &n) ||
984 	    n > blen)
985 		return FFA_INVALID_PARAMETERS;
986 
987 	if (!IS_ALIGNED_WITH_TYPE((vaddr_t)buf + region_descr_offs,
988 				  struct ffa_mem_region))
989 		return FFA_INVALID_PARAMETERS;
990 
991 	region_descr = (struct ffa_mem_region *)((vaddr_t)buf +
992 						 region_descr_offs);
993 	*page_count = READ_ONCE(region_descr->total_page_count);
994 	*region_count = READ_ONCE(region_descr->address_range_count);
995 	*addr_range_offs = n;
996 	return 0;
997 }
998 
999 static int add_mem_share_helper(struct mem_share_state *s, void *buf,
1000 				size_t flen)
1001 {
1002 	unsigned int region_count = flen / sizeof(struct ffa_address_range);
1003 	struct ffa_address_range *arange = NULL;
1004 	unsigned int n = 0;
1005 
1006 	if (region_count > s->region_count)
1007 		region_count = s->region_count;
1008 
1009 	if (!IS_ALIGNED_WITH_TYPE(buf, struct ffa_address_range))
1010 		return FFA_INVALID_PARAMETERS;
1011 	arange = buf;
1012 
1013 	for (n = 0; n < region_count; n++) {
1014 		unsigned int page_count = READ_ONCE(arange[n].page_count);
1015 		uint64_t addr = READ_ONCE(arange[n].address);
1016 
1017 		if (mobj_ffa_add_pages_at(s->mf, &s->current_page_idx,
1018 					  addr, page_count))
1019 			return FFA_INVALID_PARAMETERS;
1020 	}
1021 
1022 	s->region_count -= region_count;
1023 	if (s->region_count)
1024 		return region_count * sizeof(*arange);
1025 
1026 	if (s->current_page_idx != s->page_count)
1027 		return FFA_INVALID_PARAMETERS;
1028 
1029 	return 0;
1030 }
1031 
1032 static int add_mem_share_frag(struct mem_frag_state *s, void *buf, size_t flen)
1033 {
1034 	int rc = 0;
1035 
1036 	rc = add_mem_share_helper(&s->share, buf, flen);
1037 	if (rc >= 0) {
1038 		if (!ADD_OVERFLOW(s->frag_offset, rc, &s->frag_offset)) {
1039 			/* We're not at the end of the descriptor yet */
1040 			if (s->share.region_count)
1041 				return s->frag_offset;
1042 
1043 			/* We're done */
1044 			rc = 0;
1045 		} else {
1046 			rc = FFA_INVALID_PARAMETERS;
1047 		}
1048 	}
1049 
1050 	SLIST_REMOVE(&frag_state_head, s, mem_frag_state, link);
1051 	if (rc < 0)
1052 		mobj_ffa_sel1_spmc_delete(s->share.mf);
1053 	else
1054 		mobj_ffa_push_to_inactive(s->share.mf);
1055 	free(s);
1056 
1057 	return rc;
1058 }
1059 
1060 static bool is_sp_share(struct ffa_mem_transaction_x *mem_trans,
1061 			void *buf)
1062 {
1063 	struct ffa_mem_access_perm *perm = NULL;
1064 	struct ffa_mem_access *mem_acc = NULL;
1065 
1066 	if (!IS_ENABLED(CFG_SECURE_PARTITION))
1067 		return false;
1068 
1069 	if (mem_trans->mem_access_count < 1)
1070 		return false;
1071 
1072 	mem_acc = (void *)((vaddr_t)buf + mem_trans->mem_access_offs);
1073 	perm = &mem_acc->access_perm;
1074 
1075 	/*
1076 	 * perm->endpoint_id is read here only to check if the endpoint is
1077 	 * OP-TEE. We do read it later on again, but there are some additional
1078 	 * checks there to make sure that the data is correct.
1079 	 */
1080 	return READ_ONCE(perm->endpoint_id) != optee_endpoint_id;
1081 }
1082 
1083 static int add_mem_share(struct ffa_mem_transaction_x *mem_trans,
1084 			 tee_mm_entry_t *mm, void *buf, size_t blen,
1085 			 size_t flen, uint64_t *global_handle)
1086 {
1087 	int rc = 0;
1088 	struct mem_share_state share = { };
1089 	size_t addr_range_offs = 0;
1090 	uint64_t cookie = OPTEE_MSG_FMEM_INVALID_GLOBAL_ID;
1091 	size_t n = 0;
1092 
1093 	rc = mem_share_init(mem_trans, buf, flen, &share.page_count,
1094 			    &share.region_count, &addr_range_offs);
1095 	if (rc)
1096 		return rc;
1097 
1098 	if (MUL_OVERFLOW(share.region_count,
1099 			 sizeof(struct ffa_address_range), &n) ||
1100 	    ADD_OVERFLOW(n, addr_range_offs, &n) || n > blen)
1101 		return FFA_INVALID_PARAMETERS;
1102 
1103 	if (mem_trans->global_handle)
1104 		cookie = mem_trans->global_handle;
1105 	share.mf = mobj_ffa_sel1_spmc_new(cookie, share.page_count);
1106 	if (!share.mf)
1107 		return FFA_NO_MEMORY;
1108 
1109 	if (flen != blen) {
1110 		struct mem_frag_state *s = calloc(sizeof(*s), 1);
1111 
1112 		if (!s) {
1113 			rc = FFA_NO_MEMORY;
1114 			goto err;
1115 		}
1116 		s->share = share;
1117 		s->mm = mm;
1118 		s->frag_offset = addr_range_offs;
1119 
1120 		SLIST_INSERT_HEAD(&frag_state_head, s, link);
1121 		rc = add_mem_share_frag(s, (char *)buf + addr_range_offs,
1122 					flen - addr_range_offs);
1123 
1124 		if (rc >= 0)
1125 			*global_handle = mobj_ffa_get_cookie(share.mf);
1126 
1127 		return rc;
1128 	}
1129 
1130 	rc = add_mem_share_helper(&share, (char *)buf + addr_range_offs,
1131 				  flen - addr_range_offs);
1132 	if (rc) {
1133 		/*
1134 		 * Number of consumed bytes may be returned instead of 0 for
1135 		 * done.
1136 		 */
1137 		rc = FFA_INVALID_PARAMETERS;
1138 		goto err;
1139 	}
1140 
1141 	*global_handle = mobj_ffa_push_to_inactive(share.mf);
1142 
1143 	return 0;
1144 err:
1145 	mobj_ffa_sel1_spmc_delete(share.mf);
1146 	return rc;
1147 }
1148 
1149 static int handle_mem_share_tmem(paddr_t pbuf, size_t blen, size_t flen,
1150 				 unsigned int page_count,
1151 				 uint64_t *global_handle, struct ffa_rxtx *rxtx)
1152 {
1153 	struct ffa_mem_transaction_x mem_trans = { };
1154 	int rc = 0;
1155 	size_t len = 0;
1156 	void *buf = NULL;
1157 	tee_mm_entry_t *mm = NULL;
1158 	vaddr_t offs = pbuf & SMALL_PAGE_MASK;
1159 
1160 	if (MUL_OVERFLOW(page_count, SMALL_PAGE_SIZE, &len))
1161 		return FFA_INVALID_PARAMETERS;
1162 	if (!core_pbuf_is(CORE_MEM_NON_SEC, pbuf, len))
1163 		return FFA_INVALID_PARAMETERS;
1164 
1165 	/*
1166 	 * Check that the length reported in flen is covered by len even
1167 	 * if the offset is taken into account.
1168 	 */
1169 	if (len < flen || len - offs < flen)
1170 		return FFA_INVALID_PARAMETERS;
1171 
1172 	mm = tee_mm_alloc(&tee_mm_shm, len);
1173 	if (!mm)
1174 		return FFA_NO_MEMORY;
1175 
1176 	if (core_mmu_map_contiguous_pages(tee_mm_get_smem(mm), pbuf,
1177 					  page_count, MEM_AREA_NSEC_SHM)) {
1178 		rc = FFA_INVALID_PARAMETERS;
1179 		goto out;
1180 	}
1181 	buf = (void *)(tee_mm_get_smem(mm) + offs);
1182 
1183 	cpu_spin_lock(&rxtx->spinlock);
1184 	rc = spmc_read_mem_transaction(rxtx->ffa_vers, buf, flen, &mem_trans);
1185 	if (!rc && IS_ENABLED(CFG_NS_VIRTUALIZATION) &&
1186 	    virt_set_guest(mem_trans.sender_id))
1187 		rc = FFA_DENIED;
1188 	if (!rc)
1189 		rc = add_mem_share(&mem_trans, mm, buf, blen, flen,
1190 				   global_handle);
1191 	virt_unset_guest();
1192 	cpu_spin_unlock(&rxtx->spinlock);
1193 	if (rc > 0)
1194 		return rc;
1195 
1196 	core_mmu_unmap_pages(tee_mm_get_smem(mm), page_count);
1197 out:
1198 	tee_mm_free(mm);
1199 	return rc;
1200 }
1201 
1202 static int handle_mem_share_rxbuf(size_t blen, size_t flen,
1203 				  uint64_t *global_handle,
1204 				  struct ffa_rxtx *rxtx)
1205 {
1206 	struct ffa_mem_transaction_x mem_trans = { };
1207 	int rc = FFA_DENIED;
1208 
1209 	cpu_spin_lock(&rxtx->spinlock);
1210 
1211 	if (!rxtx->rx || flen > rxtx->size)
1212 		goto out;
1213 
1214 	rc = spmc_read_mem_transaction(rxtx->ffa_vers, rxtx->rx, flen,
1215 				       &mem_trans);
1216 	if (rc)
1217 		goto out;
1218 	if (is_sp_share(&mem_trans, rxtx->rx)) {
1219 		rc = spmc_sp_add_share(&mem_trans, rxtx, blen,
1220 				       global_handle, NULL);
1221 		goto out;
1222 	}
1223 
1224 	if (IS_ENABLED(CFG_NS_VIRTUALIZATION) &&
1225 	    virt_set_guest(mem_trans.sender_id))
1226 		goto out;
1227 
1228 	rc = add_mem_share(&mem_trans, NULL, rxtx->rx, blen, flen,
1229 			   global_handle);
1230 
1231 	virt_unset_guest();
1232 
1233 out:
1234 	cpu_spin_unlock(&rxtx->spinlock);
1235 
1236 	return rc;
1237 }
1238 
1239 static void handle_mem_share(struct thread_smc_args *args,
1240 			     struct ffa_rxtx *rxtx)
1241 {
1242 	uint32_t tot_len = args->a1;
1243 	uint32_t frag_len = args->a2;
1244 	uint64_t addr = args->a3;
1245 	uint32_t page_count = args->a4;
1246 	uint32_t ret_w1 = 0;
1247 	uint32_t ret_w2 = FFA_INVALID_PARAMETERS;
1248 	uint32_t ret_w3 = 0;
1249 	uint32_t ret_fid = FFA_ERROR;
1250 	uint64_t global_handle = 0;
1251 	int rc = 0;
1252 
1253 	/* Check that the MBZs are indeed 0 */
1254 	if (args->a5 || args->a6 || args->a7)
1255 		goto out;
1256 
1257 	/* Check that fragment length doesn't exceed total length */
1258 	if (frag_len > tot_len)
1259 		goto out;
1260 
1261 	/* Check for 32-bit calling convention */
1262 	if (args->a0 == FFA_MEM_SHARE_32)
1263 		addr &= UINT32_MAX;
1264 
1265 	if (!addr) {
1266 		/*
1267 		 * The memory transaction descriptor is passed via our rx
1268 		 * buffer.
1269 		 */
1270 		if (page_count)
1271 			goto out;
1272 		rc = handle_mem_share_rxbuf(tot_len, frag_len, &global_handle,
1273 					    rxtx);
1274 	} else {
1275 		rc = handle_mem_share_tmem(addr, tot_len, frag_len, page_count,
1276 					   &global_handle, rxtx);
1277 	}
1278 	if (rc < 0) {
1279 		ret_w2 = rc;
1280 	} else if (rc > 0) {
1281 		ret_fid = FFA_MEM_FRAG_RX;
1282 		ret_w3 = rc;
1283 		reg_pair_from_64(global_handle, &ret_w2, &ret_w1);
1284 	} else {
1285 		ret_fid = FFA_SUCCESS_32;
1286 		reg_pair_from_64(global_handle, &ret_w3, &ret_w2);
1287 	}
1288 out:
1289 	spmc_set_args(args, ret_fid, ret_w1, ret_w2, ret_w3, 0, 0);
1290 }
1291 
1292 static struct mem_frag_state *get_frag_state(uint64_t global_handle)
1293 {
1294 	struct mem_frag_state *s = NULL;
1295 
1296 	SLIST_FOREACH(s, &frag_state_head, link)
1297 		if (mobj_ffa_get_cookie(s->share.mf) == global_handle)
1298 			return s;
1299 
1300 	return NULL;
1301 }
1302 
1303 static void handle_mem_frag_tx(struct thread_smc_args *args,
1304 			       struct ffa_rxtx *rxtx)
1305 {
1306 	uint64_t global_handle = reg_pair_to_64(args->a2, args->a1);
1307 	size_t flen = args->a3;
1308 	uint32_t endpoint_id = args->a4;
1309 	struct mem_frag_state *s = NULL;
1310 	tee_mm_entry_t *mm = NULL;
1311 	unsigned int page_count = 0;
1312 	void *buf = NULL;
1313 	uint32_t ret_w1 = 0;
1314 	uint32_t ret_w2 = 0;
1315 	uint32_t ret_w3 = 0;
1316 	uint32_t ret_fid = 0;
1317 	int rc = 0;
1318 
1319 	if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) {
1320 		uint16_t guest_id = endpoint_id >> 16;
1321 
1322 		if (!guest_id || virt_set_guest(guest_id)) {
1323 			rc = FFA_INVALID_PARAMETERS;
1324 			goto out_set_rc;
1325 		}
1326 	}
1327 
1328 	/*
1329 	 * Currently we're only doing this for fragmented FFA_MEM_SHARE_*
1330 	 * requests.
1331 	 */
1332 
1333 	cpu_spin_lock(&rxtx->spinlock);
1334 
1335 	s = get_frag_state(global_handle);
1336 	if (!s) {
1337 		rc = FFA_INVALID_PARAMETERS;
1338 		goto out;
1339 	}
1340 
1341 	mm = s->mm;
1342 	if (mm) {
1343 		if (flen > tee_mm_get_bytes(mm)) {
1344 			rc = FFA_INVALID_PARAMETERS;
1345 			goto out;
1346 		}
1347 		page_count = s->share.page_count;
1348 		buf = (void *)tee_mm_get_smem(mm);
1349 	} else {
1350 		if (flen > rxtx->size) {
1351 			rc = FFA_INVALID_PARAMETERS;
1352 			goto out;
1353 		}
1354 		buf = rxtx->rx;
1355 	}
1356 
1357 	rc = add_mem_share_frag(s, buf, flen);
1358 out:
1359 	virt_unset_guest();
1360 	cpu_spin_unlock(&rxtx->spinlock);
1361 
1362 	if (rc <= 0 && mm) {
1363 		core_mmu_unmap_pages(tee_mm_get_smem(mm), page_count);
1364 		tee_mm_free(mm);
1365 	}
1366 
1367 out_set_rc:
1368 	if (rc < 0) {
1369 		ret_fid = FFA_ERROR;
1370 		ret_w2 = rc;
1371 	} else if (rc > 0) {
1372 		ret_fid = FFA_MEM_FRAG_RX;
1373 		ret_w3 = rc;
1374 		reg_pair_from_64(global_handle, &ret_w2, &ret_w1);
1375 	} else {
1376 		ret_fid = FFA_SUCCESS_32;
1377 		reg_pair_from_64(global_handle, &ret_w3, &ret_w2);
1378 	}
1379 
1380 	spmc_set_args(args, ret_fid, ret_w1, ret_w2, ret_w3, 0, 0);
1381 }
1382 
1383 static void handle_mem_reclaim(struct thread_smc_args *args)
1384 {
1385 	int rc = FFA_INVALID_PARAMETERS;
1386 	uint64_t cookie = 0;
1387 
1388 	if (args->a3 || args->a4 || args->a5 || args->a6 || args->a7)
1389 		goto out;
1390 
1391 	cookie = reg_pair_to_64(args->a2, args->a1);
1392 	if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) {
1393 		uint16_t guest_id = 0;
1394 
1395 		if (cookie & FFA_MEMORY_HANDLE_HYPERVISOR_BIT) {
1396 			guest_id = virt_find_guest_by_cookie(cookie);
1397 		} else {
1398 			guest_id = (cookie >> FFA_MEMORY_HANDLE_PRTN_SHIFT) &
1399 				   FFA_MEMORY_HANDLE_PRTN_MASK;
1400 		}
1401 		if (!guest_id)
1402 			goto out;
1403 		if (virt_set_guest(guest_id)) {
1404 			if (!virt_reclaim_cookie_from_destroyed_guest(guest_id,
1405 								      cookie))
1406 				rc = FFA_OK;
1407 			goto out;
1408 		}
1409 	}
1410 
1411 	switch (mobj_ffa_sel1_spmc_reclaim(cookie)) {
1412 	case TEE_SUCCESS:
1413 		rc = FFA_OK;
1414 		break;
1415 	case TEE_ERROR_ITEM_NOT_FOUND:
1416 		DMSG("cookie %#"PRIx64" not found", cookie);
1417 		rc = FFA_INVALID_PARAMETERS;
1418 		break;
1419 	default:
1420 		DMSG("cookie %#"PRIx64" busy", cookie);
1421 		rc = FFA_DENIED;
1422 		break;
1423 	}
1424 
1425 	virt_unset_guest();
1426 
1427 out:
1428 	set_simple_ret_val(args, rc);
1429 }
1430 
1431 static void handle_notification_bitmap_create(struct thread_smc_args *args)
1432 {
1433 	uint32_t ret_val = FFA_INVALID_PARAMETERS;
1434 	uint32_t ret_fid = FFA_ERROR;
1435 	uint32_t old_itr_status = 0;
1436 
1437 	if (!FFA_TARGET_INFO_GET_SP_ID(args->a1) && !args->a3 && !args->a4 &&
1438 	    !args->a5 && !args->a6 && !args->a7) {
1439 		uint16_t vm_id = args->a1;
1440 
1441 		old_itr_status = cpu_spin_lock_xsave(&spmc_notif_lock);
1442 
1443 		if (notif_vm_id_valid) {
1444 			if (vm_id == notif_vm_id)
1445 				ret_val = FFA_DENIED;
1446 			else
1447 				ret_val = FFA_NO_MEMORY;
1448 		} else {
1449 			notif_vm_id = vm_id;
1450 			notif_vm_id_valid = true;
1451 			ret_val = FFA_OK;
1452 			ret_fid = FFA_SUCCESS_32;
1453 		}
1454 
1455 		cpu_spin_unlock_xrestore(&spmc_notif_lock, old_itr_status);
1456 	}
1457 
1458 	spmc_set_args(args, ret_fid, 0, ret_val, 0, 0, 0);
1459 }
1460 
1461 static void handle_notification_bitmap_destroy(struct thread_smc_args *args)
1462 {
1463 	uint32_t ret_val = FFA_INVALID_PARAMETERS;
1464 	uint32_t ret_fid = FFA_ERROR;
1465 	uint32_t old_itr_status = 0;
1466 
1467 	if (!FFA_TARGET_INFO_GET_SP_ID(args->a1) && !args->a3 && !args->a4 &&
1468 	    !args->a5 && !args->a6 && !args->a7) {
1469 		uint16_t vm_id = args->a1;
1470 
1471 		old_itr_status = cpu_spin_lock_xsave(&spmc_notif_lock);
1472 
1473 		if (notif_vm_id_valid && vm_id == notif_vm_id) {
1474 			if (notif_pending_bitmap || notif_bound_bitmap) {
1475 				ret_val = FFA_DENIED;
1476 			} else {
1477 				notif_vm_id_valid = false;
1478 				ret_val = FFA_OK;
1479 				ret_fid = FFA_SUCCESS_32;
1480 			}
1481 		}
1482 
1483 		cpu_spin_unlock_xrestore(&spmc_notif_lock, old_itr_status);
1484 	}
1485 
1486 	spmc_set_args(args, ret_fid, 0, ret_val, 0, 0, 0);
1487 }
1488 
1489 static void handle_notification_bind(struct thread_smc_args *args)
1490 {
1491 	uint32_t ret_val = FFA_INVALID_PARAMETERS;
1492 	uint32_t ret_fid = FFA_ERROR;
1493 	uint32_t old_itr_status = 0;
1494 	uint64_t bitmap = 0;
1495 	uint16_t vm_id = 0;
1496 
1497 	if (args->a5 || args->a6 || args->a7)
1498 		goto out;
1499 	if (args->a2) {
1500 		/* We only deal with global notifications for now */
1501 		ret_val = FFA_NOT_SUPPORTED;
1502 		goto out;
1503 	}
1504 
1505 	/* The destination of the eventual notification */
1506 	vm_id = FFA_DST(args->a1);
1507 	bitmap = reg_pair_to_64(args->a4, args->a3);
1508 
1509 	old_itr_status = cpu_spin_lock_xsave(&spmc_notif_lock);
1510 
1511 	if (notif_vm_id_valid && vm_id == notif_vm_id) {
1512 		if (bitmap & notif_bound_bitmap) {
1513 			ret_val = FFA_DENIED;
1514 		} else {
1515 			notif_bound_bitmap |= bitmap;
1516 			ret_val = FFA_OK;
1517 			ret_fid = FFA_SUCCESS_32;
1518 		}
1519 	}
1520 
1521 	cpu_spin_unlock_xrestore(&spmc_notif_lock, old_itr_status);
1522 out:
1523 	spmc_set_args(args, ret_fid, 0, ret_val, 0, 0, 0);
1524 }
1525 
1526 static void handle_notification_unbind(struct thread_smc_args *args)
1527 {
1528 	uint32_t ret_val = FFA_INVALID_PARAMETERS;
1529 	uint32_t ret_fid = FFA_ERROR;
1530 	uint32_t old_itr_status = 0;
1531 	uint64_t bitmap = 0;
1532 	uint16_t vm_id = 0;
1533 
1534 	if (args->a2 || args->a5 || args->a6 || args->a7)
1535 		goto out;
1536 
1537 	/* The destination of the eventual notification */
1538 	vm_id = FFA_DST(args->a1);
1539 	bitmap = reg_pair_to_64(args->a4, args->a3);
1540 
1541 	old_itr_status = cpu_spin_lock_xsave(&spmc_notif_lock);
1542 
1543 	if (notif_vm_id_valid && vm_id == notif_vm_id) {
1544 		/*
1545 		 * Spec says:
1546 		 * At least one notification is bound to another Sender or
1547 		 * is currently pending.
1548 		 *
1549 		 * Not sure what the intention is.
1550 		 */
1551 		if (bitmap & notif_pending_bitmap) {
1552 			ret_val = FFA_DENIED;
1553 		} else {
1554 			notif_bound_bitmap &= ~bitmap;
1555 			ret_val = FFA_OK;
1556 			ret_fid = FFA_SUCCESS_32;
1557 		}
1558 	}
1559 
1560 	cpu_spin_unlock_xrestore(&spmc_notif_lock, old_itr_status);
1561 out:
1562 	spmc_set_args(args, ret_fid, 0, ret_val, 0, 0, 0);
1563 }
1564 
1565 static void handle_notification_get(struct thread_smc_args *args)
1566 {
1567 	uint32_t w2 = FFA_INVALID_PARAMETERS;
1568 	uint32_t ret_fid = FFA_ERROR;
1569 	uint32_t old_itr_status = 0;
1570 	uint16_t vm_id = 0;
1571 	uint32_t w3 = 0;
1572 
1573 	if (args->a5 || args->a6 || args->a7)
1574 		goto out;
1575 	if (!(args->a2 & 0x1)) {
1576 		ret_fid = FFA_SUCCESS_32;
1577 		w2 = 0;
1578 		goto out;
1579 	}
1580 	vm_id = FFA_DST(args->a1);
1581 
1582 	old_itr_status = cpu_spin_lock_xsave(&spmc_notif_lock);
1583 
1584 	if (notif_vm_id_valid && vm_id == notif_vm_id) {
1585 		reg_pair_from_64(notif_pending_bitmap, &w3, &w2);
1586 		notif_pending_bitmap = 0;
1587 		ret_fid = FFA_SUCCESS_32;
1588 	}
1589 
1590 	cpu_spin_unlock_xrestore(&spmc_notif_lock, old_itr_status);
1591 out:
1592 	spmc_set_args(args, ret_fid, 0, w2, w3, 0, 0);
1593 }
1594 
1595 static void handle_notification_info_get(struct thread_smc_args *args)
1596 {
1597 	uint32_t w2 = FFA_INVALID_PARAMETERS;
1598 	uint32_t ret_fid = FFA_ERROR;
1599 
1600 	if (args->a1 || args->a2 || args->a3 || args->a4 || args->a5 ||
1601 	    args->a6 || args->a7)
1602 		goto out;
1603 
1604 	if (OPTEE_SMC_IS_64(args->a0))
1605 		ret_fid = FFA_SUCCESS_64;
1606 	else
1607 		ret_fid = FFA_SUCCESS_32;
1608 
1609 	/*
1610 	 * Note, we're only supporting physical OS kernel in normal world
1611 	 * with Global Notifications.
1612 	 * So one list of ID list registers (BIT[11:7])
1613 	 * and one count of IDs (BIT[13:12] + 1)
1614 	 * and the VM is always 0.
1615 	 */
1616 	w2 = SHIFT_U32(1, 7);
1617 out:
1618 	spmc_set_args(args, ret_fid, 0, w2, 0, 0, 0);
1619 }
1620 
1621 void thread_spmc_set_async_notif_intid(int intid)
1622 {
1623 	assert(interrupt_can_raise_sgi(interrupt_get_main_chip()));
1624 	notif_intid = intid;
1625 	spmc_notif_is_ready = true;
1626 	DMSG("Asynchronous notifications are ready");
1627 }
1628 
1629 void notif_send_async(uint32_t value)
1630 {
1631 	uint32_t old_itr_status = 0;
1632 
1633 	old_itr_status = cpu_spin_lock_xsave(&spmc_notif_lock);
1634 	assert(value == NOTIF_VALUE_DO_BOTTOM_HALF && spmc_notif_is_ready &&
1635 	       do_bottom_half_value >= 0 && notif_intid >= 0);
1636 	notif_pending_bitmap |= BIT64(do_bottom_half_value);
1637 	interrupt_raise_sgi(interrupt_get_main_chip(), notif_intid,
1638 			    ITR_CPU_MASK_TO_THIS_CPU);
1639 	cpu_spin_unlock_xrestore(&spmc_notif_lock, old_itr_status);
1640 }
1641 #else
1642 void notif_send_async(uint32_t value)
1643 {
1644 	/* global notification, delay notification interrupt */
1645 	uint32_t flags = BIT32(1);
1646 	int res = 0;
1647 
1648 	assert(value == NOTIF_VALUE_DO_BOTTOM_HALF && spmc_notif_is_ready &&
1649 	       do_bottom_half_value >= 0);
1650 	res = ffa_set_notification(notif_vm_id, optee_endpoint_id, flags,
1651 				   BIT64(do_bottom_half_value));
1652 	if (res) {
1653 		EMSG("notification set failed with error %d", res);
1654 		panic();
1655 	}
1656 }
1657 #endif
1658 
1659 /* Only called from assembly */
1660 void thread_spmc_msg_recv(struct thread_smc_args *args);
1661 void thread_spmc_msg_recv(struct thread_smc_args *args)
1662 {
1663 	assert((thread_get_exceptions() & THREAD_EXCP_ALL) == THREAD_EXCP_ALL);
1664 	switch (args->a0) {
1665 #if defined(CFG_CORE_SEL1_SPMC)
1666 	case FFA_FEATURES:
1667 		handle_features(args);
1668 		break;
1669 	case FFA_SPM_ID_GET:
1670 		spmc_handle_spm_id_get(args);
1671 		break;
1672 #ifdef ARM64
1673 	case FFA_RXTX_MAP_64:
1674 #endif
1675 	case FFA_RXTX_MAP_32:
1676 		spmc_handle_rxtx_map(args, &my_rxtx);
1677 		break;
1678 	case FFA_RXTX_UNMAP:
1679 		spmc_handle_rxtx_unmap(args, &my_rxtx);
1680 		break;
1681 	case FFA_RX_RELEASE:
1682 		spmc_handle_rx_release(args, &my_rxtx);
1683 		break;
1684 	case FFA_PARTITION_INFO_GET:
1685 		spmc_handle_partition_info_get(args, &my_rxtx);
1686 		break;
1687 	case FFA_RUN:
1688 		spmc_handle_run(args);
1689 		break;
1690 #endif /*CFG_CORE_SEL1_SPMC*/
1691 	case FFA_INTERRUPT:
1692 		if (IS_ENABLED(CFG_CORE_SEL1_SPMC))
1693 			spmc_set_args(args, FFA_NORMAL_WORLD_RESUME, 0, 0, 0,
1694 				      0, 0);
1695 		else
1696 			spmc_set_args(args, FFA_MSG_WAIT, 0, 0, 0, 0, 0);
1697 		break;
1698 #ifdef ARM64
1699 	case FFA_MSG_SEND_DIRECT_REQ_64:
1700 #endif
1701 	case FFA_MSG_SEND_DIRECT_REQ_32:
1702 		handle_direct_request(args, &my_rxtx);
1703 		break;
1704 #if defined(CFG_CORE_SEL1_SPMC)
1705 #ifdef ARM64
1706 	case FFA_MEM_SHARE_64:
1707 #endif
1708 	case FFA_MEM_SHARE_32:
1709 		handle_mem_share(args, &my_rxtx);
1710 		break;
1711 	case FFA_MEM_RECLAIM:
1712 		if (!IS_ENABLED(CFG_SECURE_PARTITION) ||
1713 		    !ffa_mem_reclaim(args, NULL))
1714 			handle_mem_reclaim(args);
1715 		break;
1716 	case FFA_MEM_FRAG_TX:
1717 		handle_mem_frag_tx(args, &my_rxtx);
1718 		break;
1719 	case FFA_NOTIFICATION_BITMAP_CREATE:
1720 		handle_notification_bitmap_create(args);
1721 		break;
1722 	case FFA_NOTIFICATION_BITMAP_DESTROY:
1723 		handle_notification_bitmap_destroy(args);
1724 		break;
1725 	case FFA_NOTIFICATION_BIND:
1726 		handle_notification_bind(args);
1727 		break;
1728 	case FFA_NOTIFICATION_UNBIND:
1729 		handle_notification_unbind(args);
1730 		break;
1731 	case FFA_NOTIFICATION_GET:
1732 		handle_notification_get(args);
1733 		break;
1734 #ifdef ARM64
1735 	case FFA_NOTIFICATION_INFO_GET_64:
1736 #endif
1737 	case FFA_NOTIFICATION_INFO_GET_32:
1738 		handle_notification_info_get(args);
1739 		break;
1740 #endif /*CFG_CORE_SEL1_SPMC*/
1741 	case FFA_ERROR:
1742 		EMSG("Cannot handle FFA_ERROR(%d)", (int)args->a2);
1743 		if (!IS_ENABLED(CFG_CORE_SEL1_SPMC)) {
1744 			/*
1745 			 * The SPMC will return an FFA_ERROR back so better
1746 			 * panic() now than flooding the log.
1747 			 */
1748 			panic("FFA_ERROR from SPMC is fatal");
1749 		}
1750 		spmc_set_args(args, FFA_ERROR, FFA_PARAM_MBZ, FFA_NOT_SUPPORTED,
1751 			      FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ);
1752 		break;
1753 	default:
1754 		EMSG("Unhandled FFA function ID %#"PRIx32, (uint32_t)args->a0);
1755 		set_simple_ret_val(args, FFA_NOT_SUPPORTED);
1756 	}
1757 }
1758 
1759 static TEE_Result yielding_call_with_arg(uint64_t cookie, uint32_t offset)
1760 {
1761 	size_t sz_rpc = OPTEE_MSG_GET_ARG_SIZE(THREAD_RPC_MAX_NUM_PARAMS);
1762 	struct thread_ctx *thr = threads + thread_get_id();
1763 	TEE_Result res = TEE_ERROR_BAD_PARAMETERS;
1764 	struct optee_msg_arg *arg = NULL;
1765 	struct mobj *mobj = NULL;
1766 	uint32_t num_params = 0;
1767 	size_t sz = 0;
1768 
1769 	mobj = mobj_ffa_get_by_cookie(cookie, 0);
1770 	if (!mobj) {
1771 		EMSG("Can't find cookie %#"PRIx64, cookie);
1772 		return TEE_ERROR_BAD_PARAMETERS;
1773 	}
1774 
1775 	res = mobj_inc_map(mobj);
1776 	if (res)
1777 		goto out_put_mobj;
1778 
1779 	res = TEE_ERROR_BAD_PARAMETERS;
1780 	arg = mobj_get_va(mobj, offset, sizeof(*arg));
1781 	if (!arg)
1782 		goto out_dec_map;
1783 
1784 	num_params = READ_ONCE(arg->num_params);
1785 	if (num_params > OPTEE_MSG_MAX_NUM_PARAMS)
1786 		goto out_dec_map;
1787 
1788 	sz = OPTEE_MSG_GET_ARG_SIZE(num_params);
1789 
1790 	thr->rpc_arg = mobj_get_va(mobj, offset + sz, sz_rpc);
1791 	if (!thr->rpc_arg)
1792 		goto out_dec_map;
1793 
1794 	virt_on_stdcall();
1795 	res = tee_entry_std(arg, num_params);
1796 
1797 	thread_rpc_shm_cache_clear(&thr->shm_cache);
1798 	thr->rpc_arg = NULL;
1799 
1800 out_dec_map:
1801 	mobj_dec_map(mobj);
1802 out_put_mobj:
1803 	mobj_put(mobj);
1804 	return res;
1805 }
1806 
1807 /*
1808  * Helper routine for the assembly function thread_std_smc_entry()
1809  *
1810  * Note: this function is weak just to make link_dummies_paged.c happy.
1811  */
1812 uint32_t __weak __thread_std_smc_entry(uint32_t a0, uint32_t a1,
1813 				       uint32_t a2, uint32_t a3,
1814 				       uint32_t a4, uint32_t a5 __unused)
1815 {
1816 	/*
1817 	 * Arguments are supplied from handle_yielding_call() as:
1818 	 * a0 <- w1
1819 	 * a1 <- w3
1820 	 * a2 <- w4
1821 	 * a3 <- w5
1822 	 * a4 <- w6
1823 	 * a5 <- w7
1824 	 */
1825 	thread_get_tsd()->rpc_target_info = swap_src_dst(a0);
1826 	if (a1 == OPTEE_FFA_YIELDING_CALL_WITH_ARG)
1827 		return yielding_call_with_arg(reg_pair_to_64(a3, a2), a4);
1828 	return FFA_DENIED;
1829 }
1830 
1831 static bool set_fmem(struct optee_msg_param *param, struct thread_param *tpm)
1832 {
1833 	uint64_t offs = tpm->u.memref.offs;
1834 
1835 	param->attr = tpm->attr - THREAD_PARAM_ATTR_MEMREF_IN +
1836 		      OPTEE_MSG_ATTR_TYPE_FMEM_INPUT;
1837 
1838 	param->u.fmem.offs_low = offs;
1839 	param->u.fmem.offs_high = offs >> 32;
1840 	if (param->u.fmem.offs_high != offs >> 32)
1841 		return false;
1842 
1843 	param->u.fmem.size = tpm->u.memref.size;
1844 	if (tpm->u.memref.mobj) {
1845 		uint64_t cookie = mobj_get_cookie(tpm->u.memref.mobj);
1846 
1847 		/* If a mobj is passed it better be one with a valid cookie. */
1848 		if (cookie == OPTEE_MSG_FMEM_INVALID_GLOBAL_ID)
1849 			return false;
1850 		param->u.fmem.global_id = cookie;
1851 	} else {
1852 		param->u.fmem.global_id = OPTEE_MSG_FMEM_INVALID_GLOBAL_ID;
1853 	}
1854 
1855 	return true;
1856 }
1857 
1858 static uint32_t get_rpc_arg(uint32_t cmd, size_t num_params,
1859 			    struct thread_param *params,
1860 			    struct optee_msg_arg **arg_ret)
1861 {
1862 	size_t sz = OPTEE_MSG_GET_ARG_SIZE(THREAD_RPC_MAX_NUM_PARAMS);
1863 	struct thread_ctx *thr = threads + thread_get_id();
1864 	struct optee_msg_arg *arg = thr->rpc_arg;
1865 
1866 	if (num_params > THREAD_RPC_MAX_NUM_PARAMS)
1867 		return TEE_ERROR_BAD_PARAMETERS;
1868 
1869 	if (!arg) {
1870 		EMSG("rpc_arg not set");
1871 		return TEE_ERROR_GENERIC;
1872 	}
1873 
1874 	memset(arg, 0, sz);
1875 	arg->cmd = cmd;
1876 	arg->num_params = num_params;
1877 	arg->ret = TEE_ERROR_GENERIC; /* in case value isn't updated */
1878 
1879 	for (size_t n = 0; n < num_params; n++) {
1880 		switch (params[n].attr) {
1881 		case THREAD_PARAM_ATTR_NONE:
1882 			arg->params[n].attr = OPTEE_MSG_ATTR_TYPE_NONE;
1883 			break;
1884 		case THREAD_PARAM_ATTR_VALUE_IN:
1885 		case THREAD_PARAM_ATTR_VALUE_OUT:
1886 		case THREAD_PARAM_ATTR_VALUE_INOUT:
1887 			arg->params[n].attr = params[n].attr -
1888 					      THREAD_PARAM_ATTR_VALUE_IN +
1889 					      OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
1890 			arg->params[n].u.value.a = params[n].u.value.a;
1891 			arg->params[n].u.value.b = params[n].u.value.b;
1892 			arg->params[n].u.value.c = params[n].u.value.c;
1893 			break;
1894 		case THREAD_PARAM_ATTR_MEMREF_IN:
1895 		case THREAD_PARAM_ATTR_MEMREF_OUT:
1896 		case THREAD_PARAM_ATTR_MEMREF_INOUT:
1897 			if (!set_fmem(arg->params + n, params + n))
1898 				return TEE_ERROR_BAD_PARAMETERS;
1899 			break;
1900 		default:
1901 			return TEE_ERROR_BAD_PARAMETERS;
1902 		}
1903 	}
1904 
1905 	if (arg_ret)
1906 		*arg_ret = arg;
1907 
1908 	return TEE_SUCCESS;
1909 }
1910 
1911 static uint32_t get_rpc_arg_res(struct optee_msg_arg *arg, size_t num_params,
1912 				struct thread_param *params)
1913 {
1914 	for (size_t n = 0; n < num_params; n++) {
1915 		switch (params[n].attr) {
1916 		case THREAD_PARAM_ATTR_VALUE_OUT:
1917 		case THREAD_PARAM_ATTR_VALUE_INOUT:
1918 			params[n].u.value.a = arg->params[n].u.value.a;
1919 			params[n].u.value.b = arg->params[n].u.value.b;
1920 			params[n].u.value.c = arg->params[n].u.value.c;
1921 			break;
1922 		case THREAD_PARAM_ATTR_MEMREF_OUT:
1923 		case THREAD_PARAM_ATTR_MEMREF_INOUT:
1924 			params[n].u.memref.size = arg->params[n].u.fmem.size;
1925 			break;
1926 		default:
1927 			break;
1928 		}
1929 	}
1930 
1931 	return arg->ret;
1932 }
1933 
1934 uint32_t thread_rpc_cmd(uint32_t cmd, size_t num_params,
1935 			struct thread_param *params)
1936 {
1937 	struct thread_rpc_arg rpc_arg = { .call = {
1938 			.w1 = thread_get_tsd()->rpc_target_info,
1939 			.w4 = OPTEE_FFA_YIELDING_CALL_RETURN_RPC_CMD,
1940 		},
1941 	};
1942 	struct optee_msg_arg *arg = NULL;
1943 	uint32_t ret = 0;
1944 
1945 	ret = get_rpc_arg(cmd, num_params, params, &arg);
1946 	if (ret)
1947 		return ret;
1948 
1949 	thread_rpc(&rpc_arg);
1950 
1951 	return get_rpc_arg_res(arg, num_params, params);
1952 }
1953 
1954 static void thread_rpc_free(unsigned int bt, uint64_t cookie, struct mobj *mobj)
1955 {
1956 	struct thread_rpc_arg rpc_arg = { .call = {
1957 			.w1 = thread_get_tsd()->rpc_target_info,
1958 			.w4 = OPTEE_FFA_YIELDING_CALL_RETURN_RPC_CMD,
1959 		},
1960 	};
1961 	struct thread_param param = THREAD_PARAM_VALUE(IN, bt, cookie, 0);
1962 	uint32_t res2 = 0;
1963 	uint32_t res = 0;
1964 
1965 	DMSG("freeing cookie %#"PRIx64, cookie);
1966 
1967 	res = get_rpc_arg(OPTEE_RPC_CMD_SHM_FREE, 1, &param, NULL);
1968 
1969 	mobj_put(mobj);
1970 	res2 = mobj_ffa_unregister_by_cookie(cookie);
1971 	if (res2)
1972 		DMSG("mobj_ffa_unregister_by_cookie(%#"PRIx64"): %#"PRIx32,
1973 		     cookie, res2);
1974 	if (!res)
1975 		thread_rpc(&rpc_arg);
1976 }
1977 
1978 static struct mobj *thread_rpc_alloc(size_t size, size_t align, unsigned int bt)
1979 {
1980 	struct thread_rpc_arg rpc_arg = { .call = {
1981 			.w1 = thread_get_tsd()->rpc_target_info,
1982 			.w4 = OPTEE_FFA_YIELDING_CALL_RETURN_RPC_CMD,
1983 		},
1984 	};
1985 	struct thread_param param = THREAD_PARAM_VALUE(IN, bt, size, align);
1986 	struct optee_msg_arg *arg = NULL;
1987 	unsigned int internal_offset = 0;
1988 	struct mobj *mobj = NULL;
1989 	uint64_t cookie = 0;
1990 
1991 	if (get_rpc_arg(OPTEE_RPC_CMD_SHM_ALLOC, 1, &param, &arg))
1992 		return NULL;
1993 
1994 	thread_rpc(&rpc_arg);
1995 
1996 	if (arg->num_params != 1 ||
1997 	    arg->params->attr != OPTEE_MSG_ATTR_TYPE_FMEM_OUTPUT)
1998 		return NULL;
1999 
2000 	internal_offset = READ_ONCE(arg->params->u.fmem.internal_offs);
2001 	cookie = READ_ONCE(arg->params->u.fmem.global_id);
2002 	mobj = mobj_ffa_get_by_cookie(cookie, internal_offset);
2003 	if (!mobj) {
2004 		DMSG("mobj_ffa_get_by_cookie(%#"PRIx64", %#x): failed",
2005 		     cookie, internal_offset);
2006 		return NULL;
2007 	}
2008 
2009 	assert(mobj_is_nonsec(mobj));
2010 
2011 	if (mobj->size < size) {
2012 		DMSG("Mobj %#"PRIx64": wrong size", cookie);
2013 		mobj_put(mobj);
2014 		return NULL;
2015 	}
2016 
2017 	if (mobj_inc_map(mobj)) {
2018 		DMSG("mobj_inc_map(%#"PRIx64"): failed", cookie);
2019 		mobj_put(mobj);
2020 		return NULL;
2021 	}
2022 
2023 	return mobj;
2024 }
2025 
2026 struct mobj *thread_rpc_alloc_payload(size_t size)
2027 {
2028 	return thread_rpc_alloc(size, 8, OPTEE_RPC_SHM_TYPE_APPL);
2029 }
2030 
2031 struct mobj *thread_rpc_alloc_kernel_payload(size_t size)
2032 {
2033 	return thread_rpc_alloc(size, 8, OPTEE_RPC_SHM_TYPE_KERNEL);
2034 }
2035 
2036 void thread_rpc_free_kernel_payload(struct mobj *mobj)
2037 {
2038 	if (mobj)
2039 		thread_rpc_free(OPTEE_RPC_SHM_TYPE_KERNEL,
2040 				mobj_get_cookie(mobj), mobj);
2041 }
2042 
2043 void thread_rpc_free_payload(struct mobj *mobj)
2044 {
2045 	if (mobj)
2046 		thread_rpc_free(OPTEE_RPC_SHM_TYPE_APPL, mobj_get_cookie(mobj),
2047 				mobj);
2048 }
2049 
2050 struct mobj *thread_rpc_alloc_global_payload(size_t size)
2051 {
2052 	return thread_rpc_alloc(size, 8, OPTEE_RPC_SHM_TYPE_GLOBAL);
2053 }
2054 
2055 void thread_rpc_free_global_payload(struct mobj *mobj)
2056 {
2057 	if (mobj)
2058 		thread_rpc_free(OPTEE_RPC_SHM_TYPE_GLOBAL,
2059 				mobj_get_cookie(mobj), mobj);
2060 }
2061 
2062 void thread_spmc_register_secondary_ep(vaddr_t ep)
2063 {
2064 	unsigned long ret = 0;
2065 
2066 	/* Let the SPM know the entry point for secondary CPUs */
2067 	ret = thread_smc(FFA_SECONDARY_EP_REGISTER_64, ep, 0, 0);
2068 
2069 	if (ret != FFA_SUCCESS_32 && ret != FFA_SUCCESS_64)
2070 		EMSG("FFA_SECONDARY_EP_REGISTER_64 ret %#lx", ret);
2071 }
2072 
2073 static uint16_t ffa_id_get(void)
2074 {
2075 	/*
2076 	 * Ask the SPM component running at a higher EL to return our FF-A ID.
2077 	 * This can either be the SPMC ID (if the SPMC is enabled in OP-TEE) or
2078 	 * the partition ID (if not).
2079 	 */
2080 	struct thread_smc_args args = {
2081 		.a0 = FFA_ID_GET,
2082 	};
2083 
2084 	thread_smccc(&args);
2085 	if (!is_ffa_success(args.a0)) {
2086 		if (args.a0 == FFA_ERROR)
2087 			EMSG("Get id failed with error %ld", args.a2);
2088 		else
2089 			EMSG("Get id failed");
2090 		panic();
2091 	}
2092 
2093 	return args.a2;
2094 }
2095 
2096 static uint16_t ffa_spm_id_get(void)
2097 {
2098 	/*
2099 	 * Ask the SPM component running at a higher EL to return its ID.
2100 	 * If OP-TEE implements the S-EL1 SPMC, this will get the SPMD ID.
2101 	 * If not, the ID of the SPMC will be returned.
2102 	 */
2103 	struct thread_smc_args args = {
2104 		.a0 = FFA_SPM_ID_GET,
2105 	};
2106 
2107 	thread_smccc(&args);
2108 	if (!is_ffa_success(args.a0)) {
2109 		if (args.a0 == FFA_ERROR)
2110 			EMSG("Get spm id failed with error %ld", args.a2);
2111 		else
2112 			EMSG("Get spm id failed");
2113 		panic();
2114 	}
2115 
2116 	return args.a2;
2117 }
2118 
2119 #if defined(CFG_CORE_SEL1_SPMC)
2120 static TEE_Result spmc_init(void)
2121 {
2122 	spmd_id = ffa_spm_id_get();
2123 	DMSG("SPMD ID %#"PRIx16, spmd_id);
2124 
2125 	spmc_id = ffa_id_get();
2126 	DMSG("SPMC ID %#"PRIx16, spmc_id);
2127 
2128 	optee_endpoint_id = FFA_SWD_ID_MIN;
2129 	while (optee_endpoint_id == spmd_id || optee_endpoint_id == spmc_id)
2130 		optee_endpoint_id++;
2131 
2132 	DMSG("OP-TEE endpoint ID %#"PRIx16, optee_endpoint_id);
2133 
2134 	/*
2135 	 * If SPMD think we are version 1.0 it will report version 1.0 to
2136 	 * normal world regardless of what version we query the SPM with.
2137 	 * However, if SPMD think we are version 1.1 it will forward
2138 	 * queries from normal world to let us negotiate version. So by
2139 	 * setting version 1.0 here we should be compatible.
2140 	 *
2141 	 * Note that disagreement on negotiated version means that we'll
2142 	 * have communication problems with normal world.
2143 	 */
2144 	my_rxtx.ffa_vers = FFA_VERSION_1_0;
2145 
2146 	return TEE_SUCCESS;
2147 }
2148 #else /* !defined(CFG_CORE_SEL1_SPMC) */
2149 static void spmc_rxtx_map(struct ffa_rxtx *rxtx)
2150 {
2151 	struct thread_smc_args args = {
2152 #ifdef ARM64
2153 		.a0 = FFA_RXTX_MAP_64,
2154 #else
2155 		.a0 = FFA_RXTX_MAP_32,
2156 #endif
2157 		.a1 = virt_to_phys(rxtx->tx),
2158 		.a2 = virt_to_phys(rxtx->rx),
2159 		.a3 = 1,
2160 	};
2161 
2162 	thread_smccc(&args);
2163 	if (!is_ffa_success(args.a0)) {
2164 		if (args.a0 == FFA_ERROR)
2165 			EMSG("rxtx map failed with error %ld", args.a2);
2166 		else
2167 			EMSG("rxtx map failed");
2168 		panic();
2169 	}
2170 }
2171 
2172 static uint32_t get_ffa_version(uint32_t my_version)
2173 {
2174 	struct thread_smc_args args = {
2175 		.a0 = FFA_VERSION,
2176 		.a1 = my_version,
2177 	};
2178 
2179 	thread_smccc(&args);
2180 	if (args.a0 & BIT(31)) {
2181 		EMSG("FF-A version failed with error %ld", args.a0);
2182 		panic();
2183 	}
2184 
2185 	return args.a0;
2186 }
2187 
2188 static void *spmc_retrieve_req(uint64_t cookie,
2189 			       struct ffa_mem_transaction_x *trans)
2190 {
2191 	struct ffa_mem_access *acc_descr_array = NULL;
2192 	struct ffa_mem_access_perm *perm_descr = NULL;
2193 	struct thread_smc_args args = {
2194 		.a0 = FFA_MEM_RETRIEVE_REQ_32,
2195 		.a3 =	0,	/* Address, Using TX -> MBZ */
2196 		.a4 =   0,	/* Using TX -> MBZ */
2197 	};
2198 	size_t size = 0;
2199 	int rc = 0;
2200 
2201 	if (my_rxtx.ffa_vers == FFA_VERSION_1_0) {
2202 		struct ffa_mem_transaction_1_0 *trans_descr = my_rxtx.tx;
2203 
2204 		size = sizeof(*trans_descr) + 1 * sizeof(struct ffa_mem_access);
2205 		memset(trans_descr, 0, size);
2206 		trans_descr->sender_id = thread_get_tsd()->rpc_target_info;
2207 		trans_descr->mem_reg_attr = FFA_NORMAL_MEM_REG_ATTR;
2208 		trans_descr->global_handle = cookie;
2209 		trans_descr->flags = FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE |
2210 				     FFA_MEMORY_REGION_FLAG_ANY_ALIGNMENT;
2211 		trans_descr->mem_access_count = 1;
2212 		acc_descr_array = trans_descr->mem_access_array;
2213 	} else {
2214 		struct ffa_mem_transaction_1_1 *trans_descr = my_rxtx.tx;
2215 
2216 		size = sizeof(*trans_descr) + 1 * sizeof(struct ffa_mem_access);
2217 		memset(trans_descr, 0, size);
2218 		trans_descr->sender_id = thread_get_tsd()->rpc_target_info;
2219 		trans_descr->mem_reg_attr = FFA_NORMAL_MEM_REG_ATTR;
2220 		trans_descr->global_handle = cookie;
2221 		trans_descr->flags = FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE |
2222 				     FFA_MEMORY_REGION_FLAG_ANY_ALIGNMENT;
2223 		trans_descr->mem_access_count = 1;
2224 		trans_descr->mem_access_offs = sizeof(*trans_descr);
2225 		trans_descr->mem_access_size = sizeof(struct ffa_mem_access);
2226 		acc_descr_array = (void *)((vaddr_t)my_rxtx.tx +
2227 					   sizeof(*trans_descr));
2228 	}
2229 	acc_descr_array->region_offs = 0;
2230 	acc_descr_array->reserved = 0;
2231 	perm_descr = &acc_descr_array->access_perm;
2232 	perm_descr->endpoint_id = optee_endpoint_id;
2233 	perm_descr->perm = FFA_MEM_ACC_RW;
2234 	perm_descr->flags = 0;
2235 
2236 	args.a1 = size; /* Total Length */
2237 	args.a2 = size; /* Frag Length == Total length */
2238 	thread_smccc(&args);
2239 	if (args.a0 != FFA_MEM_RETRIEVE_RESP) {
2240 		if (args.a0 == FFA_ERROR)
2241 			EMSG("Failed to fetch cookie %#"PRIx64" error code %d",
2242 			     cookie, (int)args.a2);
2243 		else
2244 			EMSG("Failed to fetch cookie %#"PRIx64" a0 %#"PRIx64,
2245 			     cookie, args.a0);
2246 		return NULL;
2247 	}
2248 	rc = spmc_read_mem_transaction(my_rxtx.ffa_vers, my_rxtx.rx,
2249 				       my_rxtx.size, trans);
2250 	if (rc) {
2251 		EMSG("Memory transaction failure for cookie %#"PRIx64" rc %d",
2252 		     cookie, rc);
2253 		return NULL;
2254 	}
2255 
2256 	return my_rxtx.rx;
2257 }
2258 
2259 void thread_spmc_relinquish(uint64_t cookie)
2260 {
2261 	struct ffa_mem_relinquish *relinquish_desc = my_rxtx.tx;
2262 	struct thread_smc_args args = {
2263 		.a0 = FFA_MEM_RELINQUISH,
2264 	};
2265 
2266 	memset(relinquish_desc, 0, sizeof(*relinquish_desc));
2267 	relinquish_desc->handle = cookie;
2268 	relinquish_desc->flags = 0;
2269 	relinquish_desc->endpoint_count = 1;
2270 	relinquish_desc->endpoint_id_array[0] = optee_endpoint_id;
2271 	thread_smccc(&args);
2272 	if (!is_ffa_success(args.a0))
2273 		EMSG("Failed to relinquish cookie %#"PRIx64, cookie);
2274 }
2275 
2276 static int set_pages(struct ffa_address_range *regions,
2277 		     unsigned int num_regions, unsigned int num_pages,
2278 		     struct mobj_ffa *mf)
2279 {
2280 	unsigned int n = 0;
2281 	unsigned int idx = 0;
2282 
2283 	for (n = 0; n < num_regions; n++) {
2284 		unsigned int page_count = READ_ONCE(regions[n].page_count);
2285 		uint64_t addr = READ_ONCE(regions[n].address);
2286 
2287 		if (mobj_ffa_add_pages_at(mf, &idx, addr, page_count))
2288 			return FFA_INVALID_PARAMETERS;
2289 	}
2290 
2291 	if (idx != num_pages)
2292 		return FFA_INVALID_PARAMETERS;
2293 
2294 	return 0;
2295 }
2296 
2297 struct mobj_ffa *thread_spmc_populate_mobj_from_rx(uint64_t cookie)
2298 {
2299 	struct mobj_ffa *ret = NULL;
2300 	struct ffa_mem_transaction_x retrieve_desc = { };
2301 	struct ffa_mem_access *descr_array = NULL;
2302 	struct ffa_mem_region *descr = NULL;
2303 	struct mobj_ffa *mf = NULL;
2304 	unsigned int num_pages = 0;
2305 	unsigned int offs = 0;
2306 	void *buf = NULL;
2307 	struct thread_smc_args ffa_rx_release_args = {
2308 		.a0 = FFA_RX_RELEASE
2309 	};
2310 
2311 	/*
2312 	 * OP-TEE is only supporting a single mem_region while the
2313 	 * specification allows for more than one.
2314 	 */
2315 	buf = spmc_retrieve_req(cookie, &retrieve_desc);
2316 	if (!buf) {
2317 		EMSG("Failed to retrieve cookie from rx buffer %#"PRIx64,
2318 		     cookie);
2319 		return NULL;
2320 	}
2321 
2322 	descr_array = (void *)((vaddr_t)buf + retrieve_desc.mem_access_offs);
2323 	offs = READ_ONCE(descr_array->region_offs);
2324 	descr = (struct ffa_mem_region *)((vaddr_t)buf + offs);
2325 
2326 	num_pages = READ_ONCE(descr->total_page_count);
2327 	mf = mobj_ffa_spmc_new(cookie, num_pages);
2328 	if (!mf)
2329 		goto out;
2330 
2331 	if (set_pages(descr->address_range_array,
2332 		      READ_ONCE(descr->address_range_count), num_pages, mf)) {
2333 		mobj_ffa_spmc_delete(mf);
2334 		goto out;
2335 	}
2336 
2337 	ret = mf;
2338 
2339 out:
2340 	/* Release RX buffer after the mem retrieve request. */
2341 	thread_smccc(&ffa_rx_release_args);
2342 
2343 	return ret;
2344 }
2345 
2346 static TEE_Result spmc_init(void)
2347 {
2348 	unsigned int major = 0;
2349 	unsigned int minor __maybe_unused = 0;
2350 	uint32_t my_vers = 0;
2351 	uint32_t vers = 0;
2352 
2353 	my_vers = MAKE_FFA_VERSION(FFA_VERSION_MAJOR, FFA_VERSION_MINOR);
2354 	vers = get_ffa_version(my_vers);
2355 	major = (vers >> FFA_VERSION_MAJOR_SHIFT) & FFA_VERSION_MAJOR_MASK;
2356 	minor = (vers >> FFA_VERSION_MINOR_SHIFT) & FFA_VERSION_MINOR_MASK;
2357 	DMSG("SPMC reported version %u.%u", major, minor);
2358 	if (major != FFA_VERSION_MAJOR) {
2359 		EMSG("Incompatible major version %u, expected %u",
2360 		     major, FFA_VERSION_MAJOR);
2361 		panic();
2362 	}
2363 	if (vers < my_vers)
2364 		my_vers = vers;
2365 	DMSG("Using version %u.%u",
2366 	     (my_vers >> FFA_VERSION_MAJOR_SHIFT) & FFA_VERSION_MAJOR_MASK,
2367 	     (my_vers >> FFA_VERSION_MINOR_SHIFT) & FFA_VERSION_MINOR_MASK);
2368 	my_rxtx.ffa_vers = my_vers;
2369 
2370 	spmc_rxtx_map(&my_rxtx);
2371 
2372 	spmc_id = ffa_spm_id_get();
2373 	DMSG("SPMC ID %#"PRIx16, spmc_id);
2374 
2375 	optee_endpoint_id = ffa_id_get();
2376 	DMSG("OP-TEE endpoint ID %#"PRIx16, optee_endpoint_id);
2377 
2378 	if (!ffa_features(FFA_NOTIFICATION_SET)) {
2379 		spmc_notif_is_ready = true;
2380 		DMSG("Asynchronous notifications are ready");
2381 	}
2382 
2383 	return TEE_SUCCESS;
2384 }
2385 #endif /* !defined(CFG_CORE_SEL1_SPMC) */
2386 
2387 /*
2388  * boot_final() is always done before exiting at end of boot
2389  * initialization.  In case of virtualization the init-calls are done only
2390  * once a OP-TEE partition has been created. So with virtualization we have
2391  * to initialize via boot_final() to make sure we have a value assigned
2392  * before it's used the first time.
2393  */
2394 #ifdef CFG_NS_VIRTUALIZATION
2395 boot_final(spmc_init);
2396 #else
2397 service_init(spmc_init);
2398 #endif
2399