xref: /rk3399_ARM-atf/services/std_svc/spm/el3_spmc/spmc_shared_mem.c (revision 27c02425089548786a18d355b15acccd51880676)
1 /*
2  * Copyright (c) 2022-2023, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 #include <assert.h>
7 #include <errno.h>
8 #include <inttypes.h>
9 
10 #include <common/debug.h>
11 #include <common/runtime_svc.h>
12 #include <lib/object_pool.h>
13 #include <lib/spinlock.h>
14 #include <lib/xlat_tables/xlat_tables_v2.h>
15 #include <services/ffa_svc.h>
16 #include "spmc.h"
17 #include "spmc_shared_mem.h"
18 
19 #include <platform_def.h>
20 
21 /**
22  * struct spmc_shmem_obj - Shared memory object.
23  * @desc_size:      Size of @desc.
24  * @desc_filled:    Size of @desc already received.
25  * @in_use:         Number of clients that have called ffa_mem_retrieve_req
26  *                  without a matching ffa_mem_relinquish call.
27  * @desc:           FF-A memory region descriptor passed in ffa_mem_share.
28  */
29 struct spmc_shmem_obj {
30 	size_t desc_size;
31 	size_t desc_filled;
32 	size_t in_use;
33 	struct ffa_mtd desc;
34 };
35 
36 /*
37  * Declare our data structure to store the metadata of memory share requests.
38  * The main datastore is allocated on a per platform basis to ensure enough
39  * storage can be made available.
40  * The address of the data store will be populated by the SPMC during its
41  * initialization.
42  */
43 
44 struct spmc_shmem_obj_state spmc_shmem_obj_state = {
45 	/* Set start value for handle so top 32 bits are needed quickly. */
46 	.next_handle = 0xffffffc0U,
47 };
48 
49 /**
50  * spmc_shmem_obj_size - Convert from descriptor size to object size.
51  * @desc_size:  Size of struct ffa_memory_region_descriptor object.
52  *
53  * Return: Size of struct spmc_shmem_obj object.
54  */
55 static size_t spmc_shmem_obj_size(size_t desc_size)
56 {
57 	return desc_size + offsetof(struct spmc_shmem_obj, desc);
58 }
59 
60 /**
61  * spmc_shmem_obj_alloc - Allocate struct spmc_shmem_obj.
62  * @state:      Global state.
63  * @desc_size:  Size of struct ffa_memory_region_descriptor object that
64  *              allocated object will hold.
65  *
66  * Return: Pointer to newly allocated object, or %NULL if there not enough space
67  *         left. The returned pointer is only valid while @state is locked, to
68  *         used it again after unlocking @state, spmc_shmem_obj_lookup must be
69  *         called.
70  */
71 static struct spmc_shmem_obj *
72 spmc_shmem_obj_alloc(struct spmc_shmem_obj_state *state, size_t desc_size)
73 {
74 	struct spmc_shmem_obj *obj;
75 	size_t free = state->data_size - state->allocated;
76 	size_t obj_size;
77 
78 	if (state->data == NULL) {
79 		ERROR("Missing shmem datastore!\n");
80 		return NULL;
81 	}
82 
83 	obj_size = spmc_shmem_obj_size(desc_size);
84 
85 	/* Ensure the obj size has not overflowed. */
86 	if (obj_size < desc_size) {
87 		WARN("%s(0x%zx) desc_size overflow\n",
88 		     __func__, desc_size);
89 		return NULL;
90 	}
91 
92 	if (obj_size > free) {
93 		WARN("%s(0x%zx) failed, free 0x%zx\n",
94 		     __func__, desc_size, free);
95 		return NULL;
96 	}
97 	obj = (struct spmc_shmem_obj *)(state->data + state->allocated);
98 	obj->desc = (struct ffa_mtd) {0};
99 	obj->desc_size = desc_size;
100 	obj->desc_filled = 0;
101 	obj->in_use = 0;
102 	state->allocated += obj_size;
103 	return obj;
104 }
105 
106 /**
107  * spmc_shmem_obj_free - Free struct spmc_shmem_obj.
108  * @state:      Global state.
109  * @obj:        Object to free.
110  *
111  * Release memory used by @obj. Other objects may move, so on return all
112  * pointers to struct spmc_shmem_obj object should be considered invalid, not
113  * just @obj.
114  *
115  * The current implementation always compacts the remaining objects to simplify
116  * the allocator and to avoid fragmentation.
117  */
118 
119 static void spmc_shmem_obj_free(struct spmc_shmem_obj_state *state,
120 				  struct spmc_shmem_obj *obj)
121 {
122 	size_t free_size = spmc_shmem_obj_size(obj->desc_size);
123 	uint8_t *shift_dest = (uint8_t *)obj;
124 	uint8_t *shift_src = shift_dest + free_size;
125 	size_t shift_size = state->allocated - (shift_src - state->data);
126 
127 	if (shift_size != 0U) {
128 		memmove(shift_dest, shift_src, shift_size);
129 	}
130 	state->allocated -= free_size;
131 }
132 
133 /**
134  * spmc_shmem_obj_lookup - Lookup struct spmc_shmem_obj by handle.
135  * @state:      Global state.
136  * @handle:     Unique handle of object to return.
137  *
138  * Return: struct spmc_shmem_obj_state object with handle matching @handle.
139  *         %NULL, if not object in @state->data has a matching handle.
140  */
141 static struct spmc_shmem_obj *
142 spmc_shmem_obj_lookup(struct spmc_shmem_obj_state *state, uint64_t handle)
143 {
144 	uint8_t *curr = state->data;
145 
146 	while (curr - state->data < state->allocated) {
147 		struct spmc_shmem_obj *obj = (struct spmc_shmem_obj *)curr;
148 
149 		if (obj->desc.handle == handle) {
150 			return obj;
151 		}
152 		curr += spmc_shmem_obj_size(obj->desc_size);
153 	}
154 	return NULL;
155 }
156 
157 /**
158  * spmc_shmem_obj_get_next - Get the next memory object from an offset.
159  * @offset:     Offset used to track which objects have previously been
160  *              returned.
161  *
162  * Return: the next struct spmc_shmem_obj_state object from the provided
163  *	   offset.
164  *	   %NULL, if there are no more objects.
165  */
166 static struct spmc_shmem_obj *
167 spmc_shmem_obj_get_next(struct spmc_shmem_obj_state *state, size_t *offset)
168 {
169 	uint8_t *curr = state->data + *offset;
170 
171 	if (curr - state->data < state->allocated) {
172 		struct spmc_shmem_obj *obj = (struct spmc_shmem_obj *)curr;
173 
174 		*offset += spmc_shmem_obj_size(obj->desc_size);
175 
176 		return obj;
177 	}
178 	return NULL;
179 }
180 
181 /*******************************************************************************
182  * FF-A memory descriptor helper functions.
183  ******************************************************************************/
184 /**
185  * spmc_shmem_obj_get_emad - Get the emad from a given index depending on the
186  *                           clients FF-A version.
187  * @desc:         The memory transaction descriptor.
188  * @index:        The index of the emad element to be accessed.
189  * @ffa_version:  FF-A version of the provided structure.
190  * @emad_size:    Will be populated with the size of the returned emad
191  *                descriptor.
192  * Return: A pointer to the requested emad structure.
193  */
194 static void *
195 spmc_shmem_obj_get_emad(const struct ffa_mtd *desc, uint32_t index,
196 			uint32_t ffa_version, size_t *emad_size)
197 {
198 	uint8_t *emad;
199 
200 	assert(index < desc->emad_count);
201 
202 	/*
203 	 * If the caller is using FF-A v1.0 interpret the descriptor as a v1.0
204 	 * format, otherwise assume it is a v1.1 format.
205 	 */
206 	if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
207 		emad = (uint8_t *)desc + offsetof(struct ffa_mtd_v1_0, emad);
208 		*emad_size = sizeof(struct ffa_emad_v1_0);
209 	} else {
210 		assert(is_aligned(desc->emad_offset, 16));
211 		emad = ((uint8_t *) desc + desc->emad_offset);
212 		*emad_size = desc->emad_size;
213 	}
214 
215 	assert(((uint64_t)index * (uint64_t)*emad_size) <= UINT32_MAX);
216 	return (emad + (*emad_size * index));
217 }
218 
219 /**
220  * spmc_shmem_obj_get_comp_mrd - Get comp_mrd from a mtd struct based on the
221  *				 FF-A version of the descriptor.
222  * @obj:    Object containing ffa_memory_region_descriptor.
223  *
224  * Return: struct ffa_comp_mrd object corresponding to the composite memory
225  *	   region descriptor.
226  */
227 static struct ffa_comp_mrd *
228 spmc_shmem_obj_get_comp_mrd(struct spmc_shmem_obj *obj, uint32_t ffa_version)
229 {
230 	size_t emad_size;
231 	/*
232 	 * The comp_mrd_offset field of the emad descriptor remains consistent
233 	 * between FF-A versions therefore we can use the v1.0 descriptor here
234 	 * in all cases.
235 	 */
236 	struct ffa_emad_v1_0 *emad = spmc_shmem_obj_get_emad(&obj->desc, 0,
237 							     ffa_version,
238 							     &emad_size);
239 
240 	/* Ensure the composite descriptor offset is aligned. */
241 	if (!is_aligned(emad->comp_mrd_offset, 8)) {
242 		WARN("Unaligned composite memory region descriptor offset.\n");
243 		return NULL;
244 	}
245 
246 	return (struct ffa_comp_mrd *)
247 	       ((uint8_t *)(&obj->desc) + emad->comp_mrd_offset);
248 }
249 
250 /**
251  * spmc_shmem_obj_validate_id - Validate a partition ID is participating in
252  *				a given memory transaction.
253  * @sp_id:      Partition ID to validate.
254  * @obj:        The shared memory object containing the descriptor
255  *              of the memory transaction.
256  * Return: true if ID is valid, else false.
257  */
258 bool spmc_shmem_obj_validate_id(struct spmc_shmem_obj *obj, uint16_t sp_id)
259 {
260 	bool found = false;
261 	struct ffa_mtd *desc = &obj->desc;
262 	size_t desc_size = obj->desc_size;
263 
264 	/* Validate the partition is a valid participant. */
265 	for (unsigned int i = 0U; i < desc->emad_count; i++) {
266 		size_t emad_size;
267 		struct ffa_emad_v1_0 *emad;
268 
269 		emad = spmc_shmem_obj_get_emad(desc, i,
270 					       MAKE_FFA_VERSION(1, 1),
271 					       &emad_size);
272 		/*
273 		 * Validate the calculated emad address resides within the
274 		 * descriptor.
275 		 */
276 		if ((emad == NULL) || (uintptr_t) emad >=
277 		    (uintptr_t)((uint8_t *) desc + desc_size)) {
278 			VERBOSE("Invalid emad.\n");
279 			break;
280 		}
281 		if (sp_id == emad->mapd.endpoint_id) {
282 			found = true;
283 			break;
284 		}
285 	}
286 	return found;
287 }
288 
289 /*
290  * Compare two memory regions to determine if any range overlaps with another
291  * ongoing memory transaction.
292  */
293 static bool
294 overlapping_memory_regions(struct ffa_comp_mrd *region1,
295 			   struct ffa_comp_mrd *region2)
296 {
297 	uint64_t region1_start;
298 	uint64_t region1_size;
299 	uint64_t region1_end;
300 	uint64_t region2_start;
301 	uint64_t region2_size;
302 	uint64_t region2_end;
303 
304 	assert(region1 != NULL);
305 	assert(region2 != NULL);
306 
307 	if (region1 == region2) {
308 		return true;
309 	}
310 
311 	/*
312 	 * Check each memory region in the request against existing
313 	 * transactions.
314 	 */
315 	for (size_t i = 0; i < region1->address_range_count; i++) {
316 
317 		region1_start = region1->address_range_array[i].address;
318 		region1_size =
319 			region1->address_range_array[i].page_count *
320 			PAGE_SIZE_4KB;
321 		region1_end = region1_start + region1_size;
322 
323 		for (size_t j = 0; j < region2->address_range_count; j++) {
324 
325 			region2_start = region2->address_range_array[j].address;
326 			region2_size =
327 				region2->address_range_array[j].page_count *
328 				PAGE_SIZE_4KB;
329 			region2_end = region2_start + region2_size;
330 
331 			/* Check if regions are not overlapping. */
332 			if (!((region2_end <= region1_start) ||
333 			      (region1_end <= region2_start))) {
334 				WARN("Overlapping mem regions 0x%lx-0x%lx & 0x%lx-0x%lx\n",
335 				     region1_start, region1_end,
336 				     region2_start, region2_end);
337 				return true;
338 			}
339 		}
340 	}
341 	return false;
342 }
343 
344 /*******************************************************************************
345  * FF-A v1.0 Memory Descriptor Conversion Helpers.
346  ******************************************************************************/
347 /**
348  * spmc_shm_get_v1_1_descriptor_size - Calculate the required size for a v1.1
349  *                                     converted descriptor.
350  * @orig:       The original v1.0 memory transaction descriptor.
351  * @desc_size:  The size of the original v1.0 memory transaction descriptor.
352  *
353  * Return: the size required to store the descriptor store in the v1.1 format.
354  */
355 static uint64_t
356 spmc_shm_get_v1_1_descriptor_size(struct ffa_mtd_v1_0 *orig, size_t desc_size)
357 {
358 	uint64_t size = 0;
359 	struct ffa_comp_mrd *mrd;
360 	struct ffa_emad_v1_0 *emad_array = orig->emad;
361 
362 	/* Get the size of the v1.1 descriptor. */
363 	size += sizeof(struct ffa_mtd);
364 
365 	/* Add the size of the emad descriptors. */
366 	size += orig->emad_count * sizeof(struct ffa_emad_v1_0);
367 
368 	/* Add the size of the composite mrds. */
369 	size += sizeof(struct ffa_comp_mrd);
370 
371 	/* Add the size of the constituent mrds. */
372 	mrd = (struct ffa_comp_mrd *) ((uint8_t *) orig +
373 	      emad_array[0].comp_mrd_offset);
374 
375 	/* Add the size of the memory region descriptors. */
376 	size += mrd->address_range_count * sizeof(struct ffa_cons_mrd);
377 
378 	return size;
379 }
380 
381 /**
382  * spmc_shm_get_v1_0_descriptor_size - Calculate the required size for a v1.0
383  *                                     converted descriptor.
384  * @orig:       The original v1.1 memory transaction descriptor.
385  * @desc_size:  The size of the original v1.1 memory transaction descriptor.
386  *
387  * Return: the size required to store the descriptor store in the v1.0 format.
388  */
389 static size_t
390 spmc_shm_get_v1_0_descriptor_size(struct ffa_mtd *orig, size_t desc_size)
391 {
392 	size_t size = 0;
393 	struct ffa_comp_mrd *mrd;
394 	struct ffa_emad_v1_0 *emad_array = (struct ffa_emad_v1_0 *)
395 					   ((uint8_t *) orig +
396 					    orig->emad_offset);
397 
398 	/* Get the size of the v1.0 descriptor. */
399 	size += sizeof(struct ffa_mtd_v1_0);
400 
401 	/* Add the size of the v1.0 emad descriptors. */
402 	size += orig->emad_count * sizeof(struct ffa_emad_v1_0);
403 
404 	/* Add the size of the composite mrds. */
405 	size += sizeof(struct ffa_comp_mrd);
406 
407 	/* Add the size of the constituent mrds. */
408 	mrd = (struct ffa_comp_mrd *) ((uint8_t *) orig +
409 	      emad_array[0].comp_mrd_offset);
410 
411 	/* Check the calculated address is within the memory descriptor. */
412 	if (((uintptr_t) mrd + sizeof(struct ffa_comp_mrd)) >
413 	    (uintptr_t)((uint8_t *) orig + desc_size)) {
414 		return 0;
415 	}
416 	size += mrd->address_range_count * sizeof(struct ffa_cons_mrd);
417 
418 	return size;
419 }
420 
421 /**
422  * spmc_shm_convert_shmem_obj_from_v1_0 - Converts a given v1.0 memory object.
423  * @out_obj:	The shared memory object to populate the converted descriptor.
424  * @orig:	The shared memory object containing the v1.0 descriptor.
425  *
426  * Return: true if the conversion is successful else false.
427  */
428 static bool
429 spmc_shm_convert_shmem_obj_from_v1_0(struct spmc_shmem_obj *out_obj,
430 				     struct spmc_shmem_obj *orig)
431 {
432 	struct ffa_mtd_v1_0 *mtd_orig = (struct ffa_mtd_v1_0 *) &orig->desc;
433 	struct ffa_mtd *out = &out_obj->desc;
434 	struct ffa_emad_v1_0 *emad_array_in;
435 	struct ffa_emad_v1_0 *emad_array_out;
436 	struct ffa_comp_mrd *mrd_in;
437 	struct ffa_comp_mrd *mrd_out;
438 
439 	size_t mrd_in_offset;
440 	size_t mrd_out_offset;
441 	size_t mrd_size = 0;
442 
443 	/* Populate the new descriptor format from the v1.0 struct. */
444 	out->sender_id = mtd_orig->sender_id;
445 	out->memory_region_attributes = mtd_orig->memory_region_attributes;
446 	out->flags = mtd_orig->flags;
447 	out->handle = mtd_orig->handle;
448 	out->tag = mtd_orig->tag;
449 	out->emad_count = mtd_orig->emad_count;
450 	out->emad_size = sizeof(struct ffa_emad_v1_0);
451 
452 	/*
453 	 * We will locate the emad descriptors directly after the ffa_mtd
454 	 * struct. This will be 8-byte aligned.
455 	 */
456 	out->emad_offset = sizeof(struct ffa_mtd);
457 
458 	emad_array_in = mtd_orig->emad;
459 	emad_array_out = (struct ffa_emad_v1_0 *)
460 			 ((uint8_t *) out + out->emad_offset);
461 
462 	/* Copy across the emad structs. */
463 	for (unsigned int i = 0U; i < out->emad_count; i++) {
464 		/* Bound check for emad array. */
465 		if (((uint8_t *)emad_array_in + sizeof(struct ffa_emad_v1_0)) >
466 		    ((uint8_t *) mtd_orig + orig->desc_size)) {
467 			VERBOSE("%s: Invalid mtd structure.\n", __func__);
468 			return false;
469 		}
470 		memcpy(&emad_array_out[i], &emad_array_in[i],
471 		       sizeof(struct ffa_emad_v1_0));
472 	}
473 
474 	/* Place the mrd descriptors after the end of the emad descriptors.*/
475 	mrd_in_offset = emad_array_in->comp_mrd_offset;
476 	mrd_out_offset = out->emad_offset + (out->emad_size * out->emad_count);
477 	mrd_out = (struct ffa_comp_mrd *) ((uint8_t *) out + mrd_out_offset);
478 
479 	/* Add the size of the composite memory region descriptor. */
480 	mrd_size += sizeof(struct ffa_comp_mrd);
481 
482 	/* Find the mrd descriptor. */
483 	mrd_in = (struct ffa_comp_mrd *) ((uint8_t *) mtd_orig + mrd_in_offset);
484 
485 	/* Add the size of the constituent memory region descriptors. */
486 	mrd_size += mrd_in->address_range_count * sizeof(struct ffa_cons_mrd);
487 
488 	/*
489 	 * Update the offset in the emads by the delta between the input and
490 	 * output addresses.
491 	 */
492 	for (unsigned int i = 0U; i < out->emad_count; i++) {
493 		emad_array_out[i].comp_mrd_offset =
494 			emad_array_in[i].comp_mrd_offset +
495 			(mrd_out_offset - mrd_in_offset);
496 	}
497 
498 	/* Verify that we stay within bound of the memory descriptors. */
499 	if ((uintptr_t)((uint8_t *) mrd_in + mrd_size) >
500 	     (uintptr_t)((uint8_t *) mtd_orig + orig->desc_size) ||
501 	    ((uintptr_t)((uint8_t *) mrd_out + mrd_size) >
502 	     (uintptr_t)((uint8_t *) out + out_obj->desc_size))) {
503 		ERROR("%s: Invalid mrd structure.\n", __func__);
504 		return false;
505 	}
506 
507 	/* Copy the mrd descriptors directly. */
508 	memcpy(mrd_out, mrd_in, mrd_size);
509 
510 	return true;
511 }
512 
513 /**
514  * spmc_shm_convert_mtd_to_v1_0 - Converts a given v1.1 memory object to
515  *                                v1.0 memory object.
516  * @out_obj:    The shared memory object to populate the v1.0 descriptor.
517  * @orig:       The shared memory object containing the v1.1 descriptor.
518  *
519  * Return: true if the conversion is successful else false.
520  */
521 static bool
522 spmc_shm_convert_mtd_to_v1_0(struct spmc_shmem_obj *out_obj,
523 			     struct spmc_shmem_obj *orig)
524 {
525 	struct ffa_mtd *mtd_orig = &orig->desc;
526 	struct ffa_mtd_v1_0 *out = (struct ffa_mtd_v1_0 *) &out_obj->desc;
527 	struct ffa_emad_v1_0 *emad_in;
528 	struct ffa_emad_v1_0 *emad_array_in;
529 	struct ffa_emad_v1_0 *emad_array_out;
530 	struct ffa_comp_mrd *mrd_in;
531 	struct ffa_comp_mrd *mrd_out;
532 
533 	size_t mrd_in_offset;
534 	size_t mrd_out_offset;
535 	size_t emad_out_array_size;
536 	size_t mrd_size = 0;
537 	size_t orig_desc_size = orig->desc_size;
538 
539 	/* Populate the v1.0 descriptor format from the v1.1 struct. */
540 	out->sender_id = mtd_orig->sender_id;
541 	out->memory_region_attributes = mtd_orig->memory_region_attributes;
542 	out->flags = mtd_orig->flags;
543 	out->handle = mtd_orig->handle;
544 	out->tag = mtd_orig->tag;
545 	out->emad_count = mtd_orig->emad_count;
546 
547 	/* Determine the location of the emad array in both descriptors. */
548 	emad_array_in = (struct ffa_emad_v1_0 *)
549 			((uint8_t *) mtd_orig + mtd_orig->emad_offset);
550 	emad_array_out = out->emad;
551 
552 	/* Copy across the emad structs. */
553 	emad_in = emad_array_in;
554 	for (unsigned int i = 0U; i < out->emad_count; i++) {
555 		/* Bound check for emad array. */
556 		if (((uint8_t *)emad_in + sizeof(struct ffa_emad_v1_0)) >
557 				((uint8_t *) mtd_orig + orig_desc_size)) {
558 			VERBOSE("%s: Invalid mtd structure.\n", __func__);
559 			return false;
560 		}
561 		memcpy(&emad_array_out[i], emad_in,
562 		       sizeof(struct ffa_emad_v1_0));
563 
564 		emad_in +=  mtd_orig->emad_size;
565 	}
566 
567 	/* Place the mrd descriptors after the end of the emad descriptors. */
568 	emad_out_array_size = sizeof(struct ffa_emad_v1_0) * out->emad_count;
569 
570 	mrd_out_offset =  (uint8_t *) out->emad - (uint8_t *) out +
571 			  emad_out_array_size;
572 
573 	mrd_out = (struct ffa_comp_mrd *) ((uint8_t *) out + mrd_out_offset);
574 
575 	mrd_in_offset = mtd_orig->emad_offset +
576 			(mtd_orig->emad_size * mtd_orig->emad_count);
577 
578 	/* Add the size of the composite memory region descriptor. */
579 	mrd_size += sizeof(struct ffa_comp_mrd);
580 
581 	/* Find the mrd descriptor. */
582 	mrd_in = (struct ffa_comp_mrd *) ((uint8_t *) mtd_orig + mrd_in_offset);
583 
584 	/* Add the size of the constituent memory region descriptors. */
585 	mrd_size += mrd_in->address_range_count * sizeof(struct ffa_cons_mrd);
586 
587 	/*
588 	 * Update the offset in the emads by the delta between the input and
589 	 * output addresses.
590 	 */
591 	emad_in = emad_array_in;
592 
593 	for (unsigned int i = 0U; i < out->emad_count; i++) {
594 		emad_array_out[i].comp_mrd_offset = emad_in->comp_mrd_offset +
595 						    (mrd_out_offset -
596 						     mrd_in_offset);
597 		emad_in +=  mtd_orig->emad_size;
598 	}
599 
600 	/* Verify that we stay within bound of the memory descriptors. */
601 	if ((uintptr_t)((uint8_t *) mrd_in + mrd_size) >
602 	     (uintptr_t)((uint8_t *) mtd_orig + orig->desc_size) ||
603 	    ((uintptr_t)((uint8_t *) mrd_out + mrd_size) >
604 	     (uintptr_t)((uint8_t *) out + out_obj->desc_size))) {
605 		ERROR("%s: Invalid mrd structure.\n", __func__);
606 		return false;
607 	}
608 
609 	/* Copy the mrd descriptors directly. */
610 	memcpy(mrd_out, mrd_in, mrd_size);
611 
612 	return true;
613 }
614 
615 /**
616  * spmc_populate_ffa_v1_0_descriptor - Converts a given v1.1 memory object to
617  *                                     the v1.0 format and populates the
618  *                                     provided buffer.
619  * @dst:	    Buffer to populate v1.0 ffa_memory_region_descriptor.
620  * @orig_obj:	    Object containing v1.1 ffa_memory_region_descriptor.
621  * @buf_size:	    Size of the buffer to populate.
622  * @offset:	    The offset of the converted descriptor to copy.
623  * @copy_size:	    Will be populated with the number of bytes copied.
624  * @out_desc_size:  Will be populated with the total size of the v1.0
625  *                  descriptor.
626  *
627  * Return: 0 if conversion and population succeeded.
628  * Note: This function invalidates the reference to @orig therefore
629  * `spmc_shmem_obj_lookup` must be called if further usage is required.
630  */
631 static uint32_t
632 spmc_populate_ffa_v1_0_descriptor(void *dst, struct spmc_shmem_obj *orig_obj,
633 				 size_t buf_size, size_t offset,
634 				 size_t *copy_size, size_t *v1_0_desc_size)
635 {
636 		struct spmc_shmem_obj *v1_0_obj;
637 
638 		/* Calculate the size that the v1.0 descriptor will require. */
639 		*v1_0_desc_size = spmc_shm_get_v1_0_descriptor_size(
640 					&orig_obj->desc, orig_obj->desc_size);
641 
642 		if (*v1_0_desc_size == 0) {
643 			ERROR("%s: cannot determine size of descriptor.\n",
644 			      __func__);
645 			return FFA_ERROR_INVALID_PARAMETER;
646 		}
647 
648 		/* Get a new obj to store the v1.0 descriptor. */
649 		v1_0_obj = spmc_shmem_obj_alloc(&spmc_shmem_obj_state,
650 						*v1_0_desc_size);
651 
652 		if (!v1_0_obj) {
653 			return FFA_ERROR_NO_MEMORY;
654 		}
655 
656 		/* Perform the conversion from v1.1 to v1.0. */
657 		if (!spmc_shm_convert_mtd_to_v1_0(v1_0_obj, orig_obj)) {
658 			spmc_shmem_obj_free(&spmc_shmem_obj_state, v1_0_obj);
659 			return FFA_ERROR_INVALID_PARAMETER;
660 		}
661 
662 		*copy_size = MIN(v1_0_obj->desc_size - offset, buf_size);
663 		memcpy(dst, (uint8_t *) &v1_0_obj->desc + offset, *copy_size);
664 
665 		/*
666 		 * We're finished with the v1.0 descriptor for now so free it.
667 		 * Note that this will invalidate any references to the v1.1
668 		 * descriptor.
669 		 */
670 		spmc_shmem_obj_free(&spmc_shmem_obj_state, v1_0_obj);
671 
672 		return 0;
673 }
674 
675 static int
676 spmc_validate_mtd_start(struct ffa_mtd *desc, uint32_t ffa_version,
677 			size_t fragment_length, size_t total_length)
678 {
679 	unsigned long long emad_end;
680 	unsigned long long emad_size;
681 	unsigned long long emad_offset;
682 	unsigned int min_desc_size;
683 
684 	/* Determine the appropriate minimum descriptor size. */
685 	if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
686 		min_desc_size = sizeof(struct ffa_mtd_v1_0);
687 	} else if (ffa_version == MAKE_FFA_VERSION(1, 1)) {
688 		min_desc_size = sizeof(struct ffa_mtd);
689 	} else {
690 		return FFA_ERROR_INVALID_PARAMETER;
691 	}
692 	if (fragment_length < min_desc_size) {
693 		WARN("%s: invalid length %zu < %u\n", __func__, fragment_length,
694 		     min_desc_size);
695 		return FFA_ERROR_INVALID_PARAMETER;
696 	}
697 
698 	if (desc->emad_count == 0U) {
699 		WARN("%s: unsupported attribute desc count %u.\n",
700 		     __func__, desc->emad_count);
701 		return FFA_ERROR_INVALID_PARAMETER;
702 	}
703 
704 	/*
705 	 * If the caller is using FF-A v1.0 interpret the descriptor as a v1.0
706 	 * format, otherwise assume it is a v1.1 format.
707 	 */
708 	if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
709 		emad_offset = emad_size = sizeof(struct ffa_emad_v1_0);
710 	} else {
711 		if (!is_aligned(desc->emad_offset, 16)) {
712 			WARN("%s: Emad offset %" PRIx32 " is not 16-byte aligned.\n",
713 			     __func__, desc->emad_offset);
714 			return FFA_ERROR_INVALID_PARAMETER;
715 		}
716 		if (desc->emad_offset < sizeof(struct ffa_mtd)) {
717 			WARN("%s: Emad offset too small: 0x%" PRIx32 " < 0x%zx.\n",
718 			     __func__, desc->emad_offset,
719 			     sizeof(struct ffa_mtd));
720 			return FFA_ERROR_INVALID_PARAMETER;
721 		}
722 		emad_offset = desc->emad_offset;
723 		if (desc->emad_size < sizeof(struct ffa_emad_v1_0)) {
724 			WARN("%s: Bad emad size (%" PRIu32 " < %zu).\n", __func__,
725 			     desc->emad_size, sizeof(struct ffa_emad_v1_0));
726 			return FFA_ERROR_INVALID_PARAMETER;
727 		}
728 		if (!is_aligned(desc->emad_size, 16)) {
729 			WARN("%s: Emad size 0x%" PRIx32 " is not 16-byte aligned.\n",
730 			     __func__, desc->emad_size);
731 			return FFA_ERROR_INVALID_PARAMETER;
732 		}
733 		emad_size = desc->emad_size;
734 	}
735 
736 	/*
737 	 * Overflow is impossible: the arithmetic happens in at least 64-bit
738 	 * precision, but all of the operands are bounded by UINT32_MAX, and
739 	 * ((2^32 - 1)^2 + (2^32 - 1) + (2^32 - 1)) = ((2^32 - 1) * (2^32 + 1))
740 	 * = (2^64 - 1).
741 	 */
742 	CASSERT(sizeof(desc->emad_count == 4), assert_emad_count_max_too_large);
743 	emad_end = (desc->emad_count * (unsigned long long)emad_size) +
744 		   (unsigned long long)sizeof(struct ffa_comp_mrd) +
745 		   (unsigned long long)emad_offset;
746 
747 	if (emad_end > total_length) {
748 		WARN("%s: Composite memory region extends beyond descriptor: 0x%llx > 0x%zx\n",
749 		     __func__, emad_end, total_length);
750 		return FFA_ERROR_INVALID_PARAMETER;
751 	}
752 
753 	return 0;
754 }
755 
756 /**
757  * spmc_shmem_check_obj - Check that counts in descriptor match overall size.
758  * @obj:	  Object containing ffa_memory_region_descriptor.
759  * @ffa_version:  FF-A version of the provided descriptor.
760  *
761  * Return: 0 if object is valid, -EINVAL if constituent_memory_region_descriptor
762  * offset or count is invalid.
763  */
764 static int spmc_shmem_check_obj(struct spmc_shmem_obj *obj,
765 				uint32_t ffa_version)
766 {
767 	const struct ffa_emad_v1_0 *emad;
768 	size_t emad_size;
769 	uint32_t comp_mrd_offset = 0;
770 
771 	if (obj->desc_filled != obj->desc_size) {
772 		ERROR("BUG: %s called on incomplete object (%zu != %zu)\n",
773 		      __func__, obj->desc_filled, obj->desc_size);
774 		panic();
775 	}
776 
777 	if (spmc_validate_mtd_start(&obj->desc, ffa_version,
778 				    obj->desc_filled, obj->desc_size)) {
779 		ERROR("BUG: %s called on object with corrupt memory region descriptor\n",
780 		      __func__);
781 		panic();
782 	}
783 
784 	emad = spmc_shmem_obj_get_emad(&obj->desc, 0,
785 				       ffa_version, &emad_size);
786 
787 	for (size_t emad_num = 0; emad_num < obj->desc.emad_count; emad_num++) {
788 		size_t size;
789 		size_t count;
790 		size_t expected_size;
791 		uint64_t total_page_count;
792 		size_t header_emad_size;
793 		uint32_t offset;
794 		struct ffa_comp_mrd *comp;
795 		ffa_endpoint_id16_t ep_id;
796 
797 		/*
798 		 * Validate the calculated emad address resides within the
799 		 * descriptor.
800 		 */
801 		if ((uintptr_t) emad >
802 		    ((uintptr_t) &obj->desc + obj->desc_size - emad_size)) {
803 			ERROR("BUG: Invalid emad access not detected earlier.\n");
804 			panic();
805 		}
806 
807 		emad = (const struct ffa_emad_v1_0 *)((const uint8_t *)emad + emad_size);
808 		offset = emad->comp_mrd_offset;
809 
810 		/*
811 		 * If a partition ID resides in the secure world validate that
812 		 * the partition ID is for a known partition. Ignore any
813 		 * partition ID belonging to the normal world as it is assumed
814 		 * the Hypervisor will have validated these.
815 		 */
816 		ep_id = emad->mapd.endpoint_id;
817 		if (ffa_is_secure_world_id(ep_id)) {
818 			if (spmc_get_sp_ctx(ep_id) == NULL) {
819 				WARN("%s: Invalid receiver id 0x%x\n",
820 				     __func__, ep_id);
821 				return -EINVAL;
822 			}
823 		}
824 
825 		/*
826 		 * The offset provided to the composite memory region descriptor
827 		 * should be consistent across endpoint descriptors. Store the
828 		 * first entry and compare against subsequent entries.
829 		 */
830 		if (comp_mrd_offset == 0) {
831 			comp_mrd_offset = offset;
832 		} else {
833 			if (comp_mrd_offset != offset) {
834 				ERROR("%s: mismatching offsets provided, %u != %u\n",
835 				       __func__, offset, comp_mrd_offset);
836 				return -EINVAL;
837 			}
838 			continue; /* Remainder only executed on first iteration. */
839 		}
840 
841 		header_emad_size = (size_t)((uint8_t *)emad - (uint8_t *)&obj->desc) +
842 			(obj->desc.emad_count * emad_size);
843 
844 		if (offset < header_emad_size) {
845 			WARN("%s: invalid object, offset %u < header + emad %zu\n",
846 			     __func__, offset, header_emad_size);
847 			return -EINVAL;
848 		}
849 
850 		size = obj->desc_size;
851 
852 		if (offset > size) {
853 			WARN("%s: invalid object, offset %u > total size %zu\n",
854 			     __func__, offset, obj->desc_size);
855 			return -EINVAL;
856 		}
857 		size -= offset;
858 
859 		if (size < sizeof(struct ffa_comp_mrd)) {
860 			WARN("%s: invalid object, offset %u, total size %zu, no header space.\n",
861 			     __func__, offset, obj->desc_size);
862 			return -EINVAL;
863 		}
864 		size -= sizeof(struct ffa_comp_mrd);
865 
866 		count = size / sizeof(struct ffa_cons_mrd);
867 
868 		comp = spmc_shmem_obj_get_comp_mrd(obj, ffa_version);
869 
870 		if (comp == NULL) {
871 			WARN("%s: invalid comp_mrd offset\n", __func__);
872 			return -EINVAL;
873 		}
874 
875 		if (comp->address_range_count != count) {
876 			WARN("%s: invalid object, desc count %u != %zu\n",
877 			     __func__, comp->address_range_count, count);
878 			return -EINVAL;
879 		}
880 
881 		expected_size = offset + sizeof(*comp) +
882 			count * sizeof(struct ffa_cons_mrd);
883 
884 		if (expected_size != obj->desc_size) {
885 			WARN("%s: invalid object, computed size %zu != size %zu\n",
886 			       __func__, expected_size, obj->desc_size);
887 			return -EINVAL;
888 		}
889 
890 		total_page_count = 0;
891 
892 		for (size_t i = 0; i < count; i++) {
893 			total_page_count +=
894 				comp->address_range_array[i].page_count;
895 		}
896 		if (comp->total_page_count != total_page_count) {
897 			WARN("%s: invalid object, desc total_page_count %u != %" PRIu64 "\n",
898 			     __func__, comp->total_page_count,
899 			total_page_count);
900 			return -EINVAL;
901 		}
902 	}
903 	return 0;
904 }
905 
906 /**
907  * spmc_shmem_check_state_obj - Check if the descriptor describes memory
908  *				regions that are currently involved with an
909  *				existing memory transactions. This implies that
910  *				the memory is not in a valid state for lending.
911  * @obj:    Object containing ffa_memory_region_descriptor.
912  *
913  * Return: 0 if object is valid, -EINVAL if invalid memory state.
914  */
915 static int spmc_shmem_check_state_obj(struct spmc_shmem_obj *obj,
916 				      uint32_t ffa_version)
917 {
918 	size_t obj_offset = 0;
919 	struct spmc_shmem_obj *inflight_obj;
920 
921 	struct ffa_comp_mrd *other_mrd;
922 	struct ffa_comp_mrd *requested_mrd = spmc_shmem_obj_get_comp_mrd(obj,
923 								  ffa_version);
924 
925 	if (requested_mrd == NULL) {
926 		return -EINVAL;
927 	}
928 
929 	inflight_obj = spmc_shmem_obj_get_next(&spmc_shmem_obj_state,
930 					       &obj_offset);
931 
932 	while (inflight_obj != NULL) {
933 		/*
934 		 * Don't compare the transaction to itself or to partially
935 		 * transmitted descriptors.
936 		 */
937 		if ((obj->desc.handle != inflight_obj->desc.handle) &&
938 		    (obj->desc_size == obj->desc_filled)) {
939 			other_mrd = spmc_shmem_obj_get_comp_mrd(inflight_obj,
940 							  FFA_VERSION_COMPILED);
941 			if (other_mrd == NULL) {
942 				return -EINVAL;
943 			}
944 			if (overlapping_memory_regions(requested_mrd,
945 						       other_mrd)) {
946 				return -EINVAL;
947 			}
948 		}
949 
950 		inflight_obj = spmc_shmem_obj_get_next(&spmc_shmem_obj_state,
951 						       &obj_offset);
952 	}
953 	return 0;
954 }
955 
956 static long spmc_ffa_fill_desc(struct mailbox *mbox,
957 			       struct spmc_shmem_obj *obj,
958 			       uint32_t fragment_length,
959 			       ffa_mtd_flag32_t mtd_flag,
960 			       uint32_t ffa_version,
961 			       void *smc_handle)
962 {
963 	int ret;
964 	size_t emad_size;
965 	uint32_t handle_low;
966 	uint32_t handle_high;
967 	struct ffa_emad_v1_0 *emad;
968 	struct ffa_emad_v1_0 *other_emad;
969 
970 	if (mbox->rxtx_page_count == 0U) {
971 		WARN("%s: buffer pair not registered.\n", __func__);
972 		ret = FFA_ERROR_INVALID_PARAMETER;
973 		goto err_arg;
974 	}
975 
976 	CASSERT(sizeof(mbox->rxtx_page_count) == 4, assert_bogus_page_count);
977 	if (fragment_length > (uint64_t)mbox->rxtx_page_count * PAGE_SIZE_4KB) {
978 		WARN("%s: bad fragment size %u > %" PRIu64 " buffer size\n", __func__,
979 		     fragment_length, (uint64_t)mbox->rxtx_page_count * PAGE_SIZE_4KB);
980 		ret = FFA_ERROR_INVALID_PARAMETER;
981 		goto err_arg;
982 	}
983 
984 	if (fragment_length > obj->desc_size - obj->desc_filled) {
985 		WARN("%s: bad fragment size %u > %zu remaining\n", __func__,
986 		     fragment_length, obj->desc_size - obj->desc_filled);
987 		ret = FFA_ERROR_INVALID_PARAMETER;
988 		goto err_arg;
989 	}
990 
991 	memcpy((uint8_t *)&obj->desc + obj->desc_filled,
992 	       (uint8_t *) mbox->tx_buffer, fragment_length);
993 
994 	/* Ensure that the sender ID resides in the normal world. */
995 	if (ffa_is_secure_world_id(obj->desc.sender_id)) {
996 		WARN("%s: Invalid sender ID 0x%x.\n",
997 		     __func__, obj->desc.sender_id);
998 		ret = FFA_ERROR_DENIED;
999 		goto err_arg;
1000 	}
1001 
1002 	/* Ensure the NS bit is set to 0. */
1003 	if ((obj->desc.memory_region_attributes & FFA_MEM_ATTR_NS_BIT) != 0U) {
1004 		WARN("%s: NS mem attributes flags MBZ.\n", __func__);
1005 		ret = FFA_ERROR_INVALID_PARAMETER;
1006 		goto err_arg;
1007 	}
1008 
1009 	/*
1010 	 * We don't currently support any optional flags so ensure none are
1011 	 * requested.
1012 	 */
1013 	if (obj->desc.flags != 0U && mtd_flag != 0U &&
1014 	    (obj->desc.flags != mtd_flag)) {
1015 		WARN("%s: invalid memory transaction flags %u != %u\n",
1016 		     __func__, obj->desc.flags, mtd_flag);
1017 		ret = FFA_ERROR_INVALID_PARAMETER;
1018 		goto err_arg;
1019 	}
1020 
1021 	if (obj->desc_filled == 0U) {
1022 		/* First fragment, descriptor header has been copied */
1023 		ret = spmc_validate_mtd_start(&obj->desc, ffa_version,
1024 					      fragment_length, obj->desc_size);
1025 		if (ret != 0) {
1026 			goto err_bad_desc;
1027 		}
1028 
1029 		obj->desc.handle = spmc_shmem_obj_state.next_handle++;
1030 		obj->desc.flags |= mtd_flag;
1031 	}
1032 
1033 	obj->desc_filled += fragment_length;
1034 
1035 	handle_low = (uint32_t)obj->desc.handle;
1036 	handle_high = obj->desc.handle >> 32;
1037 
1038 	if (obj->desc_filled != obj->desc_size) {
1039 		SMC_RET8(smc_handle, FFA_MEM_FRAG_RX, handle_low,
1040 			 handle_high, obj->desc_filled,
1041 			 (uint32_t)obj->desc.sender_id << 16, 0, 0, 0);
1042 	}
1043 
1044 	/* The full descriptor has been received, perform any final checks. */
1045 
1046 	ret = spmc_shmem_check_obj(obj, ffa_version);
1047 	if (ret != 0) {
1048 		ret = FFA_ERROR_INVALID_PARAMETER;
1049 		goto err_bad_desc;
1050 	}
1051 
1052 	/* Ensure partition IDs are not duplicated. */
1053 	for (size_t i = 0; i < obj->desc.emad_count; i++) {
1054 		emad = spmc_shmem_obj_get_emad(&obj->desc, i, ffa_version,
1055 					       &emad_size);
1056 
1057 		for (size_t j = i + 1; j < obj->desc.emad_count; j++) {
1058 			other_emad = spmc_shmem_obj_get_emad(&obj->desc, j,
1059 							     ffa_version,
1060 							     &emad_size);
1061 
1062 			if (emad->mapd.endpoint_id ==
1063 				other_emad->mapd.endpoint_id) {
1064 				WARN("%s: Duplicated endpoint id 0x%x\n",
1065 				     __func__, emad->mapd.endpoint_id);
1066 				ret = FFA_ERROR_INVALID_PARAMETER;
1067 				goto err_bad_desc;
1068 			}
1069 		}
1070 	}
1071 
1072 	ret = spmc_shmem_check_state_obj(obj, ffa_version);
1073 	if (ret) {
1074 		ERROR("%s: invalid memory region descriptor.\n", __func__);
1075 		ret = FFA_ERROR_INVALID_PARAMETER;
1076 		goto err_bad_desc;
1077 	}
1078 
1079 	/*
1080 	 * Everything checks out, if the sender was using FF-A v1.0, convert
1081 	 * the descriptor format to use the v1.1 structures.
1082 	 */
1083 	if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1084 		struct spmc_shmem_obj *v1_1_obj;
1085 		uint64_t mem_handle;
1086 
1087 		/* Calculate the size that the v1.1 descriptor will required. */
1088 		uint64_t v1_1_desc_size =
1089 		    spmc_shm_get_v1_1_descriptor_size((void *) &obj->desc,
1090 						      obj->desc_size);
1091 
1092 		if (v1_1_desc_size > UINT32_MAX) {
1093 			ret = FFA_ERROR_NO_MEMORY;
1094 			goto err_arg;
1095 		}
1096 
1097 		/* Get a new obj to store the v1.1 descriptor. */
1098 		v1_1_obj =
1099 		    spmc_shmem_obj_alloc(&spmc_shmem_obj_state, (size_t)v1_1_desc_size);
1100 
1101 		if (!v1_1_obj) {
1102 			ret = FFA_ERROR_NO_MEMORY;
1103 			goto err_arg;
1104 		}
1105 
1106 		/* Perform the conversion from v1.0 to v1.1. */
1107 		v1_1_obj->desc_size = (uint32_t)v1_1_desc_size;
1108 		v1_1_obj->desc_filled = (uint32_t)v1_1_desc_size;
1109 		if (!spmc_shm_convert_shmem_obj_from_v1_0(v1_1_obj, obj)) {
1110 			ERROR("%s: Could not convert mtd!\n", __func__);
1111 			spmc_shmem_obj_free(&spmc_shmem_obj_state, v1_1_obj);
1112 			goto err_arg;
1113 		}
1114 
1115 		/*
1116 		 * We're finished with the v1.0 descriptor so free it
1117 		 * and continue our checks with the new v1.1 descriptor.
1118 		 */
1119 		mem_handle = obj->desc.handle;
1120 		spmc_shmem_obj_free(&spmc_shmem_obj_state, obj);
1121 		obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, mem_handle);
1122 		if (obj == NULL) {
1123 			ERROR("%s: Failed to find converted descriptor.\n",
1124 			     __func__);
1125 			ret = FFA_ERROR_INVALID_PARAMETER;
1126 			return spmc_ffa_error_return(smc_handle, ret);
1127 		}
1128 	}
1129 
1130 	/* Allow for platform specific operations to be performed. */
1131 	ret = plat_spmc_shmem_begin(&obj->desc);
1132 	if (ret != 0) {
1133 		goto err_arg;
1134 	}
1135 
1136 	SMC_RET8(smc_handle, FFA_SUCCESS_SMC32, 0, handle_low, handle_high, 0,
1137 		 0, 0, 0);
1138 
1139 err_bad_desc:
1140 err_arg:
1141 	spmc_shmem_obj_free(&spmc_shmem_obj_state, obj);
1142 	return spmc_ffa_error_return(smc_handle, ret);
1143 }
1144 
1145 /**
1146  * spmc_ffa_mem_send - FFA_MEM_SHARE/LEND implementation.
1147  * @client:             Client state.
1148  * @total_length:       Total length of shared memory descriptor.
1149  * @fragment_length:    Length of fragment of shared memory descriptor passed in
1150  *                      this call.
1151  * @address:            Not supported, must be 0.
1152  * @page_count:         Not supported, must be 0.
1153  * @smc_handle:         Handle passed to smc call. Used to return
1154  *                      FFA_MEM_FRAG_RX or SMC_FC_FFA_SUCCESS.
1155  *
1156  * Implements a subset of the FF-A FFA_MEM_SHARE and FFA_MEM_LEND calls needed
1157  * to share or lend memory from non-secure os to secure os (with no stream
1158  * endpoints).
1159  *
1160  * Return: 0 on success, error code on failure.
1161  */
1162 long spmc_ffa_mem_send(uint32_t smc_fid,
1163 			bool secure_origin,
1164 			uint64_t total_length,
1165 			uint32_t fragment_length,
1166 			uint64_t address,
1167 			uint32_t page_count,
1168 			void *cookie,
1169 			void *handle,
1170 			uint64_t flags)
1171 
1172 {
1173 	long ret;
1174 	struct spmc_shmem_obj *obj;
1175 	struct mailbox *mbox = spmc_get_mbox_desc(secure_origin);
1176 	ffa_mtd_flag32_t mtd_flag;
1177 	uint32_t ffa_version = get_partition_ffa_version(secure_origin);
1178 	size_t min_desc_size;
1179 
1180 	if (address != 0U || page_count != 0U) {
1181 		WARN("%s: custom memory region for message not supported.\n",
1182 		     __func__);
1183 		return spmc_ffa_error_return(handle,
1184 					     FFA_ERROR_INVALID_PARAMETER);
1185 	}
1186 
1187 	if (secure_origin) {
1188 		WARN("%s: unsupported share direction.\n", __func__);
1189 		return spmc_ffa_error_return(handle,
1190 					     FFA_ERROR_INVALID_PARAMETER);
1191 	}
1192 
1193 	if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1194 		min_desc_size = sizeof(struct ffa_mtd_v1_0);
1195 	} else if (ffa_version == MAKE_FFA_VERSION(1, 1)) {
1196 		min_desc_size = sizeof(struct ffa_mtd);
1197 	} else {
1198 		WARN("%s: bad FF-A version.\n", __func__);
1199 		return spmc_ffa_error_return(handle,
1200 					     FFA_ERROR_INVALID_PARAMETER);
1201 	}
1202 
1203 	/* Check if the descriptor is too small for the FF-A version. */
1204 	if (fragment_length < min_desc_size) {
1205 		WARN("%s: bad first fragment size %u < %zu\n",
1206 		     __func__, fragment_length, sizeof(struct ffa_mtd_v1_0));
1207 		return spmc_ffa_error_return(handle,
1208 					     FFA_ERROR_INVALID_PARAMETER);
1209 	}
1210 
1211 	if ((smc_fid & FUNCID_NUM_MASK) == FFA_FNUM_MEM_SHARE) {
1212 		mtd_flag = FFA_MTD_FLAG_TYPE_SHARE_MEMORY;
1213 	} else if ((smc_fid & FUNCID_NUM_MASK) == FFA_FNUM_MEM_LEND) {
1214 		mtd_flag = FFA_MTD_FLAG_TYPE_LEND_MEMORY;
1215 	} else {
1216 		WARN("%s: invalid memory management operation.\n", __func__);
1217 		return spmc_ffa_error_return(handle,
1218 					     FFA_ERROR_INVALID_PARAMETER);
1219 	}
1220 
1221 	spin_lock(&spmc_shmem_obj_state.lock);
1222 	obj = spmc_shmem_obj_alloc(&spmc_shmem_obj_state, total_length);
1223 	if (obj == NULL) {
1224 		ret = FFA_ERROR_NO_MEMORY;
1225 		goto err_unlock;
1226 	}
1227 
1228 	spin_lock(&mbox->lock);
1229 	ret = spmc_ffa_fill_desc(mbox, obj, fragment_length, mtd_flag,
1230 				 ffa_version, handle);
1231 	spin_unlock(&mbox->lock);
1232 
1233 	spin_unlock(&spmc_shmem_obj_state.lock);
1234 	return ret;
1235 
1236 err_unlock:
1237 	spin_unlock(&spmc_shmem_obj_state.lock);
1238 	return spmc_ffa_error_return(handle, ret);
1239 }
1240 
1241 /**
1242  * spmc_ffa_mem_frag_tx - FFA_MEM_FRAG_TX implementation.
1243  * @client:             Client state.
1244  * @handle_low:         Handle_low value returned from FFA_MEM_FRAG_RX.
1245  * @handle_high:        Handle_high value returned from FFA_MEM_FRAG_RX.
1246  * @fragment_length:    Length of fragments transmitted.
1247  * @sender_id:          Vmid of sender in bits [31:16]
1248  * @smc_handle:         Handle passed to smc call. Used to return
1249  *                      FFA_MEM_FRAG_RX or SMC_FC_FFA_SUCCESS.
1250  *
1251  * Return: @smc_handle on success, error code on failure.
1252  */
1253 long spmc_ffa_mem_frag_tx(uint32_t smc_fid,
1254 			  bool secure_origin,
1255 			  uint64_t handle_low,
1256 			  uint64_t handle_high,
1257 			  uint32_t fragment_length,
1258 			  uint32_t sender_id,
1259 			  void *cookie,
1260 			  void *handle,
1261 			  uint64_t flags)
1262 {
1263 	long ret;
1264 	uint32_t desc_sender_id;
1265 	uint32_t ffa_version = get_partition_ffa_version(secure_origin);
1266 	struct mailbox *mbox = spmc_get_mbox_desc(secure_origin);
1267 
1268 	struct spmc_shmem_obj *obj;
1269 	uint64_t mem_handle = handle_low | (((uint64_t)handle_high) << 32);
1270 
1271 	spin_lock(&spmc_shmem_obj_state.lock);
1272 
1273 	obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, mem_handle);
1274 	if (obj == NULL) {
1275 		WARN("%s: invalid handle, 0x%lx, not a valid handle.\n",
1276 		     __func__, mem_handle);
1277 		ret = FFA_ERROR_INVALID_PARAMETER;
1278 		goto err_unlock;
1279 	}
1280 
1281 	desc_sender_id = (uint32_t)obj->desc.sender_id << 16;
1282 	if (sender_id != desc_sender_id) {
1283 		WARN("%s: invalid sender_id 0x%x != 0x%x\n", __func__,
1284 		     sender_id, desc_sender_id);
1285 		ret = FFA_ERROR_INVALID_PARAMETER;
1286 		goto err_unlock;
1287 	}
1288 
1289 	if (obj->desc_filled == obj->desc_size) {
1290 		WARN("%s: object desc already filled, %zu\n", __func__,
1291 		     obj->desc_filled);
1292 		ret = FFA_ERROR_INVALID_PARAMETER;
1293 		goto err_unlock;
1294 	}
1295 
1296 	spin_lock(&mbox->lock);
1297 	ret = spmc_ffa_fill_desc(mbox, obj, fragment_length, 0, ffa_version,
1298 				 handle);
1299 	spin_unlock(&mbox->lock);
1300 
1301 	spin_unlock(&spmc_shmem_obj_state.lock);
1302 	return ret;
1303 
1304 err_unlock:
1305 	spin_unlock(&spmc_shmem_obj_state.lock);
1306 	return spmc_ffa_error_return(handle, ret);
1307 }
1308 
1309 /**
1310  * spmc_ffa_mem_retrieve_set_ns_bit - Set the NS bit in the response descriptor
1311  *				      if the caller implements a version greater
1312  *				      than FF-A 1.0 or if they have requested
1313  *				      the functionality.
1314  *				      TODO: We are assuming that the caller is
1315  *				      an SP. To support retrieval from the
1316  *				      normal world this function will need to be
1317  *				      expanded accordingly.
1318  * @resp:       Descriptor populated in callers RX buffer.
1319  * @sp_ctx:     Context of the calling SP.
1320  */
1321 void spmc_ffa_mem_retrieve_set_ns_bit(struct ffa_mtd *resp,
1322 			 struct secure_partition_desc *sp_ctx)
1323 {
1324 	if (sp_ctx->ffa_version > MAKE_FFA_VERSION(1, 0) ||
1325 	    sp_ctx->ns_bit_requested) {
1326 		/*
1327 		 * Currently memory senders must reside in the normal
1328 		 * world, and we do not have the functionlaity to change
1329 		 * the state of memory dynamically. Therefore we can always set
1330 		 * the NS bit to 1.
1331 		 */
1332 		resp->memory_region_attributes |= FFA_MEM_ATTR_NS_BIT;
1333 	}
1334 }
1335 
1336 /**
1337  * spmc_ffa_mem_retrieve_req - FFA_MEM_RETRIEVE_REQ implementation.
1338  * @smc_fid:            FID of SMC
1339  * @total_length:       Total length of retrieve request descriptor if this is
1340  *                      the first call. Otherwise (unsupported) must be 0.
1341  * @fragment_length:    Length of fragment of retrieve request descriptor passed
1342  *                      in this call. Only @fragment_length == @length is
1343  *                      supported by this implementation.
1344  * @address:            Not supported, must be 0.
1345  * @page_count:         Not supported, must be 0.
1346  * @smc_handle:         Handle passed to smc call. Used to return
1347  *                      FFA_MEM_RETRIEVE_RESP.
1348  *
1349  * Implements a subset of the FF-A FFA_MEM_RETRIEVE_REQ call.
1350  * Used by secure os to retrieve memory already shared by non-secure os.
1351  * If the data does not fit in a single FFA_MEM_RETRIEVE_RESP message,
1352  * the client must call FFA_MEM_FRAG_RX until the full response has been
1353  * received.
1354  *
1355  * Return: @handle on success, error code on failure.
1356  */
1357 long
1358 spmc_ffa_mem_retrieve_req(uint32_t smc_fid,
1359 			  bool secure_origin,
1360 			  uint32_t total_length,
1361 			  uint32_t fragment_length,
1362 			  uint64_t address,
1363 			  uint32_t page_count,
1364 			  void *cookie,
1365 			  void *handle,
1366 			  uint64_t flags)
1367 {
1368 	int ret;
1369 	size_t buf_size;
1370 	size_t copy_size = 0;
1371 	size_t min_desc_size;
1372 	size_t out_desc_size = 0;
1373 
1374 	/*
1375 	 * Currently we are only accessing fields that are the same in both the
1376 	 * v1.0 and v1.1 mtd struct therefore we can use a v1.1 struct directly
1377 	 * here. We only need validate against the appropriate struct size.
1378 	 */
1379 	struct ffa_mtd *resp;
1380 	const struct ffa_mtd *req;
1381 	struct spmc_shmem_obj *obj = NULL;
1382 	struct mailbox *mbox = spmc_get_mbox_desc(secure_origin);
1383 	uint32_t ffa_version = get_partition_ffa_version(secure_origin);
1384 	struct secure_partition_desc *sp_ctx = spmc_get_current_sp_ctx();
1385 
1386 	if (!secure_origin) {
1387 		WARN("%s: unsupported retrieve req direction.\n", __func__);
1388 		return spmc_ffa_error_return(handle,
1389 					     FFA_ERROR_INVALID_PARAMETER);
1390 	}
1391 
1392 	if (address != 0U || page_count != 0U) {
1393 		WARN("%s: custom memory region not supported.\n", __func__);
1394 		return spmc_ffa_error_return(handle,
1395 					     FFA_ERROR_INVALID_PARAMETER);
1396 	}
1397 
1398 	spin_lock(&mbox->lock);
1399 
1400 	req = mbox->tx_buffer;
1401 	resp = mbox->rx_buffer;
1402 	buf_size = mbox->rxtx_page_count * FFA_PAGE_SIZE;
1403 
1404 	if (mbox->rxtx_page_count == 0U) {
1405 		WARN("%s: buffer pair not registered.\n", __func__);
1406 		ret = FFA_ERROR_INVALID_PARAMETER;
1407 		goto err_unlock_mailbox;
1408 	}
1409 
1410 	if (mbox->state != MAILBOX_STATE_EMPTY) {
1411 		WARN("%s: RX Buffer is full! %d\n", __func__, mbox->state);
1412 		ret = FFA_ERROR_DENIED;
1413 		goto err_unlock_mailbox;
1414 	}
1415 
1416 	if (fragment_length != total_length) {
1417 		WARN("%s: fragmented retrieve request not supported.\n",
1418 		     __func__);
1419 		ret = FFA_ERROR_INVALID_PARAMETER;
1420 		goto err_unlock_mailbox;
1421 	}
1422 
1423 	if (req->emad_count == 0U) {
1424 		WARN("%s: unsupported attribute desc count %u.\n",
1425 		     __func__, obj->desc.emad_count);
1426 		ret = FFA_ERROR_INVALID_PARAMETER;
1427 		goto err_unlock_mailbox;
1428 	}
1429 
1430 	/* Determine the appropriate minimum descriptor size. */
1431 	if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1432 		min_desc_size = sizeof(struct ffa_mtd_v1_0);
1433 	} else {
1434 		min_desc_size = sizeof(struct ffa_mtd);
1435 	}
1436 	if (total_length < min_desc_size) {
1437 		WARN("%s: invalid length %u < %zu\n", __func__, total_length,
1438 		     min_desc_size);
1439 		ret = FFA_ERROR_INVALID_PARAMETER;
1440 		goto err_unlock_mailbox;
1441 	}
1442 
1443 	spin_lock(&spmc_shmem_obj_state.lock);
1444 
1445 	obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, req->handle);
1446 	if (obj == NULL) {
1447 		ret = FFA_ERROR_INVALID_PARAMETER;
1448 		goto err_unlock_all;
1449 	}
1450 
1451 	if (obj->desc_filled != obj->desc_size) {
1452 		WARN("%s: incomplete object desc filled %zu < size %zu\n",
1453 		     __func__, obj->desc_filled, obj->desc_size);
1454 		ret = FFA_ERROR_INVALID_PARAMETER;
1455 		goto err_unlock_all;
1456 	}
1457 
1458 	if (req->emad_count != 0U && req->sender_id != obj->desc.sender_id) {
1459 		WARN("%s: wrong sender id 0x%x != 0x%x\n",
1460 		     __func__, req->sender_id, obj->desc.sender_id);
1461 		ret = FFA_ERROR_INVALID_PARAMETER;
1462 		goto err_unlock_all;
1463 	}
1464 
1465 	if (req->emad_count != 0U && req->tag != obj->desc.tag) {
1466 		WARN("%s: wrong tag 0x%lx != 0x%lx\n",
1467 		     __func__, req->tag, obj->desc.tag);
1468 		ret = FFA_ERROR_INVALID_PARAMETER;
1469 		goto err_unlock_all;
1470 	}
1471 
1472 	if (req->emad_count != 0U && req->emad_count != obj->desc.emad_count) {
1473 		WARN("%s: mistmatch of endpoint counts %u != %u\n",
1474 		     __func__, req->emad_count, obj->desc.emad_count);
1475 		ret = FFA_ERROR_INVALID_PARAMETER;
1476 		goto err_unlock_all;
1477 	}
1478 
1479 	/* Ensure the NS bit is set to 0 in the request. */
1480 	if ((req->memory_region_attributes & FFA_MEM_ATTR_NS_BIT) != 0U) {
1481 		WARN("%s: NS mem attributes flags MBZ.\n", __func__);
1482 		ret = FFA_ERROR_INVALID_PARAMETER;
1483 		goto err_unlock_all;
1484 	}
1485 
1486 	if (req->flags != 0U) {
1487 		if ((req->flags & FFA_MTD_FLAG_TYPE_MASK) !=
1488 		    (obj->desc.flags & FFA_MTD_FLAG_TYPE_MASK)) {
1489 			/*
1490 			 * If the retrieve request specifies the memory
1491 			 * transaction ensure it matches what we expect.
1492 			 */
1493 			WARN("%s: wrong mem transaction flags %x != %x\n",
1494 			__func__, req->flags, obj->desc.flags);
1495 			ret = FFA_ERROR_INVALID_PARAMETER;
1496 			goto err_unlock_all;
1497 		}
1498 
1499 		if (req->flags != FFA_MTD_FLAG_TYPE_SHARE_MEMORY &&
1500 		    req->flags != FFA_MTD_FLAG_TYPE_LEND_MEMORY) {
1501 			/*
1502 			 * Current implementation does not support donate and
1503 			 * it supports no other flags.
1504 			 */
1505 			WARN("%s: invalid flags 0x%x\n", __func__, req->flags);
1506 			ret = FFA_ERROR_INVALID_PARAMETER;
1507 			goto err_unlock_all;
1508 		}
1509 	}
1510 
1511 	/* Validate the caller is a valid participant. */
1512 	if (!spmc_shmem_obj_validate_id(obj, sp_ctx->sp_id)) {
1513 		WARN("%s: Invalid endpoint ID (0x%x).\n",
1514 			__func__, sp_ctx->sp_id);
1515 		ret = FFA_ERROR_INVALID_PARAMETER;
1516 		goto err_unlock_all;
1517 	}
1518 
1519 	/* Validate that the provided emad offset and structure is valid.*/
1520 	for (size_t i = 0; i < req->emad_count; i++) {
1521 		size_t emad_size;
1522 		struct ffa_emad_v1_0 *emad;
1523 
1524 		emad = spmc_shmem_obj_get_emad(req, i, ffa_version,
1525 					       &emad_size);
1526 
1527 		if ((uintptr_t) emad >= (uintptr_t)
1528 					((uint8_t *) req + total_length)) {
1529 			WARN("Invalid emad access.\n");
1530 			ret = FFA_ERROR_INVALID_PARAMETER;
1531 			goto err_unlock_all;
1532 		}
1533 	}
1534 
1535 	/*
1536 	 * Validate all the endpoints match in the case of multiple
1537 	 * borrowers. We don't mandate that the order of the borrowers
1538 	 * must match in the descriptors therefore check to see if the
1539 	 * endpoints match in any order.
1540 	 */
1541 	for (size_t i = 0; i < req->emad_count; i++) {
1542 		bool found = false;
1543 		size_t emad_size;
1544 		struct ffa_emad_v1_0 *emad;
1545 		struct ffa_emad_v1_0 *other_emad;
1546 
1547 		emad = spmc_shmem_obj_get_emad(req, i, ffa_version,
1548 					       &emad_size);
1549 
1550 		for (size_t j = 0; j < obj->desc.emad_count; j++) {
1551 			other_emad = spmc_shmem_obj_get_emad(
1552 					&obj->desc, j, MAKE_FFA_VERSION(1, 1),
1553 					&emad_size);
1554 
1555 			if (req->emad_count &&
1556 			    emad->mapd.endpoint_id ==
1557 			    other_emad->mapd.endpoint_id) {
1558 				found = true;
1559 				break;
1560 			}
1561 		}
1562 
1563 		if (!found) {
1564 			WARN("%s: invalid receiver id (0x%x).\n",
1565 			     __func__, emad->mapd.endpoint_id);
1566 			ret = FFA_ERROR_INVALID_PARAMETER;
1567 			goto err_unlock_all;
1568 		}
1569 	}
1570 
1571 	mbox->state = MAILBOX_STATE_FULL;
1572 
1573 	if (req->emad_count != 0U) {
1574 		obj->in_use++;
1575 	}
1576 
1577 	/*
1578 	 * If the caller is v1.0 convert the descriptor, otherwise copy
1579 	 * directly.
1580 	 */
1581 	if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1582 		ret = spmc_populate_ffa_v1_0_descriptor(resp, obj, buf_size, 0,
1583 							&copy_size,
1584 							&out_desc_size);
1585 		if (ret != 0U) {
1586 			ERROR("%s: Failed to process descriptor.\n", __func__);
1587 			goto err_unlock_all;
1588 		}
1589 	} else {
1590 		copy_size = MIN(obj->desc_size, buf_size);
1591 		out_desc_size = obj->desc_size;
1592 
1593 		memcpy(resp, &obj->desc, copy_size);
1594 	}
1595 
1596 	/* Set the NS bit in the response if applicable. */
1597 	spmc_ffa_mem_retrieve_set_ns_bit(resp, sp_ctx);
1598 
1599 	spin_unlock(&spmc_shmem_obj_state.lock);
1600 	spin_unlock(&mbox->lock);
1601 
1602 	SMC_RET8(handle, FFA_MEM_RETRIEVE_RESP, out_desc_size,
1603 		 copy_size, 0, 0, 0, 0, 0);
1604 
1605 err_unlock_all:
1606 	spin_unlock(&spmc_shmem_obj_state.lock);
1607 err_unlock_mailbox:
1608 	spin_unlock(&mbox->lock);
1609 	return spmc_ffa_error_return(handle, ret);
1610 }
1611 
1612 /**
1613  * spmc_ffa_mem_frag_rx - FFA_MEM_FRAG_RX implementation.
1614  * @client:             Client state.
1615  * @handle_low:         Handle passed to &FFA_MEM_RETRIEVE_REQ. Bit[31:0].
1616  * @handle_high:        Handle passed to &FFA_MEM_RETRIEVE_REQ. Bit[63:32].
1617  * @fragment_offset:    Byte offset in descriptor to resume at.
1618  * @sender_id:          Bit[31:16]: Endpoint id of sender if client is a
1619  *                      hypervisor. 0 otherwise.
1620  * @smc_handle:         Handle passed to smc call. Used to return
1621  *                      FFA_MEM_FRAG_TX.
1622  *
1623  * Return: @smc_handle on success, error code on failure.
1624  */
1625 long spmc_ffa_mem_frag_rx(uint32_t smc_fid,
1626 			  bool secure_origin,
1627 			  uint32_t handle_low,
1628 			  uint32_t handle_high,
1629 			  uint32_t fragment_offset,
1630 			  uint32_t sender_id,
1631 			  void *cookie,
1632 			  void *handle,
1633 			  uint64_t flags)
1634 {
1635 	int ret;
1636 	void *src;
1637 	size_t buf_size;
1638 	size_t copy_size;
1639 	size_t full_copy_size;
1640 	uint32_t desc_sender_id;
1641 	struct mailbox *mbox = spmc_get_mbox_desc(secure_origin);
1642 	uint64_t mem_handle = handle_low | (((uint64_t)handle_high) << 32);
1643 	struct spmc_shmem_obj *obj;
1644 	uint32_t ffa_version = get_partition_ffa_version(secure_origin);
1645 
1646 	if (!secure_origin) {
1647 		WARN("%s: can only be called from swld.\n",
1648 		     __func__);
1649 		return spmc_ffa_error_return(handle,
1650 					     FFA_ERROR_INVALID_PARAMETER);
1651 	}
1652 
1653 	spin_lock(&spmc_shmem_obj_state.lock);
1654 
1655 	obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, mem_handle);
1656 	if (obj == NULL) {
1657 		WARN("%s: invalid handle, 0x%lx, not a valid handle.\n",
1658 		     __func__, mem_handle);
1659 		ret = FFA_ERROR_INVALID_PARAMETER;
1660 		goto err_unlock_shmem;
1661 	}
1662 
1663 	desc_sender_id = (uint32_t)obj->desc.sender_id << 16;
1664 	if (sender_id != 0U && sender_id != desc_sender_id) {
1665 		WARN("%s: invalid sender_id 0x%x != 0x%x\n", __func__,
1666 		     sender_id, desc_sender_id);
1667 		ret = FFA_ERROR_INVALID_PARAMETER;
1668 		goto err_unlock_shmem;
1669 	}
1670 
1671 	if (fragment_offset >= obj->desc_size) {
1672 		WARN("%s: invalid fragment_offset 0x%x >= 0x%zx\n",
1673 		     __func__, fragment_offset, obj->desc_size);
1674 		ret = FFA_ERROR_INVALID_PARAMETER;
1675 		goto err_unlock_shmem;
1676 	}
1677 
1678 	spin_lock(&mbox->lock);
1679 
1680 	if (mbox->rxtx_page_count == 0U) {
1681 		WARN("%s: buffer pair not registered.\n", __func__);
1682 		ret = FFA_ERROR_INVALID_PARAMETER;
1683 		goto err_unlock_all;
1684 	}
1685 
1686 	if (mbox->state != MAILBOX_STATE_EMPTY) {
1687 		WARN("%s: RX Buffer is full!\n", __func__);
1688 		ret = FFA_ERROR_DENIED;
1689 		goto err_unlock_all;
1690 	}
1691 
1692 	buf_size = mbox->rxtx_page_count * FFA_PAGE_SIZE;
1693 
1694 	mbox->state = MAILBOX_STATE_FULL;
1695 
1696 	/*
1697 	 * If the caller is v1.0 convert the descriptor, otherwise copy
1698 	 * directly.
1699 	 */
1700 	if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1701 		size_t out_desc_size;
1702 
1703 		ret = spmc_populate_ffa_v1_0_descriptor(mbox->rx_buffer, obj,
1704 							buf_size,
1705 							fragment_offset,
1706 							&copy_size,
1707 							&out_desc_size);
1708 		if (ret != 0U) {
1709 			ERROR("%s: Failed to process descriptor.\n", __func__);
1710 			goto err_unlock_all;
1711 		}
1712 	} else {
1713 		full_copy_size = obj->desc_size - fragment_offset;
1714 		copy_size = MIN(full_copy_size, buf_size);
1715 
1716 		src = &obj->desc;
1717 
1718 		memcpy(mbox->rx_buffer, src + fragment_offset, copy_size);
1719 	}
1720 
1721 	spin_unlock(&mbox->lock);
1722 	spin_unlock(&spmc_shmem_obj_state.lock);
1723 
1724 	SMC_RET8(handle, FFA_MEM_FRAG_TX, handle_low, handle_high,
1725 		 copy_size, sender_id, 0, 0, 0);
1726 
1727 err_unlock_all:
1728 	spin_unlock(&mbox->lock);
1729 err_unlock_shmem:
1730 	spin_unlock(&spmc_shmem_obj_state.lock);
1731 	return spmc_ffa_error_return(handle, ret);
1732 }
1733 
1734 /**
1735  * spmc_ffa_mem_relinquish - FFA_MEM_RELINQUISH implementation.
1736  * @client:             Client state.
1737  *
1738  * Implements a subset of the FF-A FFA_MEM_RELINQUISH call.
1739  * Used by secure os release previously shared memory to non-secure os.
1740  *
1741  * The handle to release must be in the client's (secure os's) transmit buffer.
1742  *
1743  * Return: 0 on success, error code on failure.
1744  */
1745 int spmc_ffa_mem_relinquish(uint32_t smc_fid,
1746 			    bool secure_origin,
1747 			    uint32_t handle_low,
1748 			    uint32_t handle_high,
1749 			    uint32_t fragment_offset,
1750 			    uint32_t sender_id,
1751 			    void *cookie,
1752 			    void *handle,
1753 			    uint64_t flags)
1754 {
1755 	int ret;
1756 	struct mailbox *mbox = spmc_get_mbox_desc(secure_origin);
1757 	struct spmc_shmem_obj *obj;
1758 	const struct ffa_mem_relinquish_descriptor *req;
1759 	struct secure_partition_desc *sp_ctx = spmc_get_current_sp_ctx();
1760 
1761 	if (!secure_origin) {
1762 		WARN("%s: unsupported relinquish direction.\n", __func__);
1763 		return spmc_ffa_error_return(handle,
1764 					     FFA_ERROR_INVALID_PARAMETER);
1765 	}
1766 
1767 	spin_lock(&mbox->lock);
1768 
1769 	if (mbox->rxtx_page_count == 0U) {
1770 		WARN("%s: buffer pair not registered.\n", __func__);
1771 		ret = FFA_ERROR_INVALID_PARAMETER;
1772 		goto err_unlock_mailbox;
1773 	}
1774 
1775 	req = mbox->tx_buffer;
1776 
1777 	if (req->flags != 0U) {
1778 		WARN("%s: unsupported flags 0x%x\n", __func__, req->flags);
1779 		ret = FFA_ERROR_INVALID_PARAMETER;
1780 		goto err_unlock_mailbox;
1781 	}
1782 
1783 	if (req->endpoint_count == 0) {
1784 		WARN("%s: endpoint count cannot be 0.\n", __func__);
1785 		ret = FFA_ERROR_INVALID_PARAMETER;
1786 		goto err_unlock_mailbox;
1787 	}
1788 
1789 	spin_lock(&spmc_shmem_obj_state.lock);
1790 
1791 	obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, req->handle);
1792 	if (obj == NULL) {
1793 		ret = FFA_ERROR_INVALID_PARAMETER;
1794 		goto err_unlock_all;
1795 	}
1796 
1797 	/*
1798 	 * Validate the endpoint ID was populated correctly. We don't currently
1799 	 * support proxy endpoints so the endpoint count should always be 1.
1800 	 */
1801 	if (req->endpoint_count != 1U) {
1802 		WARN("%s: unsupported endpoint count %u != 1\n", __func__,
1803 		     req->endpoint_count);
1804 		ret = FFA_ERROR_INVALID_PARAMETER;
1805 		goto err_unlock_all;
1806 	}
1807 
1808 	/* Validate provided endpoint ID matches the partition ID. */
1809 	if (req->endpoint_array[0] != sp_ctx->sp_id) {
1810 		WARN("%s: invalid endpoint ID %u != %u\n", __func__,
1811 		     req->endpoint_array[0], sp_ctx->sp_id);
1812 		ret = FFA_ERROR_INVALID_PARAMETER;
1813 		goto err_unlock_all;
1814 	}
1815 
1816 	/* Validate the caller is a valid participant. */
1817 	if (!spmc_shmem_obj_validate_id(obj, sp_ctx->sp_id)) {
1818 		WARN("%s: Invalid endpoint ID (0x%x).\n",
1819 			__func__, req->endpoint_array[0]);
1820 		ret = FFA_ERROR_INVALID_PARAMETER;
1821 		goto err_unlock_all;
1822 	}
1823 
1824 	if (obj->in_use == 0U) {
1825 		ret = FFA_ERROR_INVALID_PARAMETER;
1826 		goto err_unlock_all;
1827 	}
1828 	obj->in_use--;
1829 
1830 	spin_unlock(&spmc_shmem_obj_state.lock);
1831 	spin_unlock(&mbox->lock);
1832 
1833 	SMC_RET1(handle, FFA_SUCCESS_SMC32);
1834 
1835 err_unlock_all:
1836 	spin_unlock(&spmc_shmem_obj_state.lock);
1837 err_unlock_mailbox:
1838 	spin_unlock(&mbox->lock);
1839 	return spmc_ffa_error_return(handle, ret);
1840 }
1841 
1842 /**
1843  * spmc_ffa_mem_reclaim - FFA_MEM_RECLAIM implementation.
1844  * @client:         Client state.
1845  * @handle_low:     Unique handle of shared memory object to reclaim. Bit[31:0].
1846  * @handle_high:    Unique handle of shared memory object to reclaim.
1847  *                  Bit[63:32].
1848  * @flags:          Unsupported, ignored.
1849  *
1850  * Implements a subset of the FF-A FFA_MEM_RECLAIM call.
1851  * Used by non-secure os reclaim memory previously shared with secure os.
1852  *
1853  * Return: 0 on success, error code on failure.
1854  */
1855 int spmc_ffa_mem_reclaim(uint32_t smc_fid,
1856 			 bool secure_origin,
1857 			 uint32_t handle_low,
1858 			 uint32_t handle_high,
1859 			 uint32_t mem_flags,
1860 			 uint64_t x4,
1861 			 void *cookie,
1862 			 void *handle,
1863 			 uint64_t flags)
1864 {
1865 	int ret;
1866 	struct spmc_shmem_obj *obj;
1867 	uint64_t mem_handle = handle_low | (((uint64_t)handle_high) << 32);
1868 
1869 	if (secure_origin) {
1870 		WARN("%s: unsupported reclaim direction.\n", __func__);
1871 		return spmc_ffa_error_return(handle,
1872 					     FFA_ERROR_INVALID_PARAMETER);
1873 	}
1874 
1875 	if (mem_flags != 0U) {
1876 		WARN("%s: unsupported flags 0x%x\n", __func__, mem_flags);
1877 		return spmc_ffa_error_return(handle,
1878 					     FFA_ERROR_INVALID_PARAMETER);
1879 	}
1880 
1881 	spin_lock(&spmc_shmem_obj_state.lock);
1882 
1883 	obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, mem_handle);
1884 	if (obj == NULL) {
1885 		ret = FFA_ERROR_INVALID_PARAMETER;
1886 		goto err_unlock;
1887 	}
1888 	if (obj->in_use != 0U) {
1889 		ret = FFA_ERROR_DENIED;
1890 		goto err_unlock;
1891 	}
1892 
1893 	if (obj->desc_filled != obj->desc_size) {
1894 		WARN("%s: incomplete object desc filled %zu < size %zu\n",
1895 		     __func__, obj->desc_filled, obj->desc_size);
1896 		ret = FFA_ERROR_INVALID_PARAMETER;
1897 		goto err_unlock;
1898 	}
1899 
1900 	/* Allow for platform specific operations to be performed. */
1901 	ret = plat_spmc_shmem_reclaim(&obj->desc);
1902 	if (ret != 0) {
1903 		goto err_unlock;
1904 	}
1905 
1906 	spmc_shmem_obj_free(&spmc_shmem_obj_state, obj);
1907 	spin_unlock(&spmc_shmem_obj_state.lock);
1908 
1909 	SMC_RET1(handle, FFA_SUCCESS_SMC32);
1910 
1911 err_unlock:
1912 	spin_unlock(&spmc_shmem_obj_state.lock);
1913 	return spmc_ffa_error_return(handle, ret);
1914 }
1915