blob: b5cb734dcbf82c8b9b1ebbf00674baa0f8d60080 [file] [log] [blame]
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001/*
Demi Marie Obenour1f9f8302022-12-30 19:14:18 -05002 * Copyright (c) 2022-2023, ARM Limited and Contributors. All rights reserved.
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
Marc Bonnicic31ec9e2022-01-21 10:34:55 +00006#include <assert.h>
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01007#include <errno.h>
Demi Marie Obenour4ed9df42022-12-30 19:30:58 -05008#include <inttypes.h>
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01009
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 */
29struct spmc_shmem_obj {
30 size_t desc_size;
31 size_t desc_filled;
32 size_t in_use;
Marc Bonnicid1907f02022-04-19 17:42:53 +010033 struct ffa_mtd desc;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +010034};
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
44struct 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 */
55static 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 */
71static struct spmc_shmem_obj *
72spmc_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;
Marc Bonnicib774f562022-10-18 14:03:13 +010076 size_t obj_size;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +010077
78 if (state->data == NULL) {
79 ERROR("Missing shmem datastore!\n");
80 return NULL;
81 }
82
Marc Bonnicib774f562022-10-18 14:03:13 +010083 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) {
Marc Bonnici9f23c8d2021-10-01 16:06:04 +010093 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);
Marc Bonnicid1907f02022-04-19 17:42:53 +010098 obj->desc = (struct ffa_mtd) {0};
Marc Bonnici9f23c8d2021-10-01 16:06:04 +010099 obj->desc_size = desc_size;
100 obj->desc_filled = 0;
101 obj->in_use = 0;
Marc Bonnicib774f562022-10-18 14:03:13 +0100102 state->allocated += obj_size;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100103 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
119static 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 */
141static struct spmc_shmem_obj *
142spmc_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
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000157/**
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 */
166static struct spmc_shmem_obj *
167spmc_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
Marc Bonnicid1907f02022-04-19 17:42:53 +0100181/*******************************************************************************
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 */
194static void *
195spmc_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;
Demi Marie Obenour32167a02023-01-11 10:51:01 -0500199
200 assert(index < desc->emad_count);
201
Marc Bonnicid1907f02022-04-19 17:42:53 +0100202 /*
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)) {
Demi Marie Obenour57bf10c2022-12-31 11:11:18 -0500207 emad = (uint8_t *)desc + offsetof(struct ffa_mtd_v1_0, emad);
Marc Bonnicid1907f02022-04-19 17:42:53 +0100208 *emad_size = sizeof(struct ffa_emad_v1_0);
209 } else {
Demi Marie Obenour57bf10c2022-12-31 11:11:18 -0500210 assert(is_aligned(desc->emad_offset, 16));
Marc Bonnicid1907f02022-04-19 17:42:53 +0100211 emad = ((uint8_t *) desc + desc->emad_offset);
212 *emad_size = desc->emad_size;
213 }
Demi Marie Obenour57bf10c2022-12-31 11:11:18 -0500214
215 assert(((uint64_t)index * (uint64_t)*emad_size) <= UINT32_MAX);
Marc Bonnicid1907f02022-04-19 17:42:53 +0100216 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 */
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100227static struct ffa_comp_mrd *
Marc Bonnicid1907f02022-04-19 17:42:53 +0100228spmc_shmem_obj_get_comp_mrd(struct spmc_shmem_obj *obj, uint32_t ffa_version)
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100229{
Marc Bonnicid1907f02022-04-19 17:42:53 +0100230 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);
Marc Bonnicid1907f02022-04-19 17:42:53 +0100239
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
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100246 return (struct ffa_comp_mrd *)
Marc Bonnicid1907f02022-04-19 17:42:53 +0100247 ((uint8_t *)(&obj->desc) + emad->comp_mrd_offset);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100248}
249
250/**
251 * spmc_shmem_obj_ffa_constituent_size - Calculate variable size part of obj.
252 * @obj: Object containing ffa_memory_region_descriptor.
253 *
254 * Return: Size of ffa_constituent_memory_region_descriptors in @obj.
255 */
256static size_t
Marc Bonnicid1907f02022-04-19 17:42:53 +0100257spmc_shmem_obj_ffa_constituent_size(struct spmc_shmem_obj *obj,
258 uint32_t ffa_version)
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100259{
Marc Bonnicid1907f02022-04-19 17:42:53 +0100260 struct ffa_comp_mrd *comp_mrd;
261
262 comp_mrd = spmc_shmem_obj_get_comp_mrd(obj, ffa_version);
263 if (comp_mrd == NULL) {
264 return 0;
265 }
266 return comp_mrd->address_range_count * sizeof(struct ffa_cons_mrd);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100267}
268
Marc Bonnici9bdcb742022-06-06 14:37:57 +0100269/**
270 * spmc_shmem_obj_validate_id - Validate a partition ID is participating in
271 * a given memory transaction.
272 * @sp_id: Partition ID to validate.
Shruti Gupta20ce06c2022-08-25 14:22:53 +0100273 * @obj: The shared memory object containing the descriptor
274 * of the memory transaction.
Marc Bonnici9bdcb742022-06-06 14:37:57 +0100275 * Return: true if ID is valid, else false.
276 */
Shruti Gupta20ce06c2022-08-25 14:22:53 +0100277bool spmc_shmem_obj_validate_id(struct spmc_shmem_obj *obj, uint16_t sp_id)
Marc Bonnici9bdcb742022-06-06 14:37:57 +0100278{
279 bool found = false;
Shruti Gupta20ce06c2022-08-25 14:22:53 +0100280 struct ffa_mtd *desc = &obj->desc;
281 size_t desc_size = obj->desc_size;
Marc Bonnici9bdcb742022-06-06 14:37:57 +0100282
283 /* Validate the partition is a valid participant. */
284 for (unsigned int i = 0U; i < desc->emad_count; i++) {
285 size_t emad_size;
286 struct ffa_emad_v1_0 *emad;
287
288 emad = spmc_shmem_obj_get_emad(desc, i,
289 MAKE_FFA_VERSION(1, 1),
290 &emad_size);
Shruti Gupta20ce06c2022-08-25 14:22:53 +0100291 /*
292 * Validate the calculated emad address resides within the
293 * descriptor.
294 */
295 if ((emad == NULL) || (uintptr_t) emad >=
296 (uintptr_t)((uint8_t *) desc + desc_size)) {
297 VERBOSE("Invalid emad.\n");
298 break;
299 }
Marc Bonnici9bdcb742022-06-06 14:37:57 +0100300 if (sp_id == emad->mapd.endpoint_id) {
301 found = true;
302 break;
303 }
304 }
305 return found;
306}
307
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000308/*
309 * Compare two memory regions to determine if any range overlaps with another
310 * ongoing memory transaction.
311 */
312static bool
313overlapping_memory_regions(struct ffa_comp_mrd *region1,
314 struct ffa_comp_mrd *region2)
315{
316 uint64_t region1_start;
317 uint64_t region1_size;
318 uint64_t region1_end;
319 uint64_t region2_start;
320 uint64_t region2_size;
321 uint64_t region2_end;
322
323 assert(region1 != NULL);
324 assert(region2 != NULL);
325
326 if (region1 == region2) {
327 return true;
328 }
329
330 /*
331 * Check each memory region in the request against existing
332 * transactions.
333 */
334 for (size_t i = 0; i < region1->address_range_count; i++) {
335
336 region1_start = region1->address_range_array[i].address;
337 region1_size =
338 region1->address_range_array[i].page_count *
339 PAGE_SIZE_4KB;
340 region1_end = region1_start + region1_size;
341
342 for (size_t j = 0; j < region2->address_range_count; j++) {
343
344 region2_start = region2->address_range_array[j].address;
345 region2_size =
346 region2->address_range_array[j].page_count *
347 PAGE_SIZE_4KB;
348 region2_end = region2_start + region2_size;
349
Marc Bonnici79669bb2022-10-18 13:50:04 +0100350 /* Check if regions are not overlapping. */
351 if (!((region2_end <= region1_start) ||
352 (region1_end <= region2_start))) {
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000353 WARN("Overlapping mem regions 0x%lx-0x%lx & 0x%lx-0x%lx\n",
354 region1_start, region1_end,
355 region2_start, region2_end);
356 return true;
357 }
358 }
359 }
360 return false;
361}
362
Marc Bonnicid1907f02022-04-19 17:42:53 +0100363/*******************************************************************************
364 * FF-A v1.0 Memory Descriptor Conversion Helpers.
365 ******************************************************************************/
366/**
367 * spmc_shm_get_v1_1_descriptor_size - Calculate the required size for a v1.1
368 * converted descriptor.
369 * @orig: The original v1.0 memory transaction descriptor.
370 * @desc_size: The size of the original v1.0 memory transaction descriptor.
371 *
372 * Return: the size required to store the descriptor store in the v1.1 format.
373 */
374static size_t
375spmc_shm_get_v1_1_descriptor_size(struct ffa_mtd_v1_0 *orig, size_t desc_size)
376{
377 size_t size = 0;
378 struct ffa_comp_mrd *mrd;
379 struct ffa_emad_v1_0 *emad_array = orig->emad;
380
381 /* Get the size of the v1.1 descriptor. */
382 size += sizeof(struct ffa_mtd);
383
384 /* Add the size of the emad descriptors. */
385 size += orig->emad_count * sizeof(struct ffa_emad_v1_0);
386
387 /* Add the size of the composite mrds. */
388 size += sizeof(struct ffa_comp_mrd);
389
390 /* Add the size of the constituent mrds. */
391 mrd = (struct ffa_comp_mrd *) ((uint8_t *) orig +
392 emad_array[0].comp_mrd_offset);
393
394 /* Check the calculated address is within the memory descriptor. */
Marc Bonnicif744c992022-10-18 18:01:44 +0100395 if (((uintptr_t) mrd + sizeof(struct ffa_comp_mrd)) >
396 (uintptr_t)((uint8_t *) orig + desc_size)) {
Marc Bonnicid1907f02022-04-19 17:42:53 +0100397 return 0;
398 }
399 size += mrd->address_range_count * sizeof(struct ffa_cons_mrd);
400
401 return size;
402}
403
404/**
405 * spmc_shm_get_v1_0_descriptor_size - Calculate the required size for a v1.0
406 * converted descriptor.
407 * @orig: The original v1.1 memory transaction descriptor.
408 * @desc_size: The size of the original v1.1 memory transaction descriptor.
409 *
410 * Return: the size required to store the descriptor store in the v1.0 format.
411 */
412static size_t
413spmc_shm_get_v1_0_descriptor_size(struct ffa_mtd *orig, size_t desc_size)
414{
415 size_t size = 0;
416 struct ffa_comp_mrd *mrd;
417 struct ffa_emad_v1_0 *emad_array = (struct ffa_emad_v1_0 *)
418 ((uint8_t *) orig +
419 orig->emad_offset);
420
421 /* Get the size of the v1.0 descriptor. */
422 size += sizeof(struct ffa_mtd_v1_0);
423
424 /* Add the size of the v1.0 emad descriptors. */
425 size += orig->emad_count * sizeof(struct ffa_emad_v1_0);
426
427 /* Add the size of the composite mrds. */
428 size += sizeof(struct ffa_comp_mrd);
429
430 /* Add the size of the constituent mrds. */
431 mrd = (struct ffa_comp_mrd *) ((uint8_t *) orig +
432 emad_array[0].comp_mrd_offset);
433
434 /* Check the calculated address is within the memory descriptor. */
Marc Bonnicif744c992022-10-18 18:01:44 +0100435 if (((uintptr_t) mrd + sizeof(struct ffa_comp_mrd)) >
436 (uintptr_t)((uint8_t *) orig + desc_size)) {
Marc Bonnicid1907f02022-04-19 17:42:53 +0100437 return 0;
438 }
439 size += mrd->address_range_count * sizeof(struct ffa_cons_mrd);
440
441 return size;
442}
443
444/**
445 * spmc_shm_convert_shmem_obj_from_v1_0 - Converts a given v1.0 memory object.
446 * @out_obj: The shared memory object to populate the converted descriptor.
447 * @orig: The shared memory object containing the v1.0 descriptor.
448 *
449 * Return: true if the conversion is successful else false.
450 */
451static bool
452spmc_shm_convert_shmem_obj_from_v1_0(struct spmc_shmem_obj *out_obj,
453 struct spmc_shmem_obj *orig)
454{
455 struct ffa_mtd_v1_0 *mtd_orig = (struct ffa_mtd_v1_0 *) &orig->desc;
456 struct ffa_mtd *out = &out_obj->desc;
457 struct ffa_emad_v1_0 *emad_array_in;
458 struct ffa_emad_v1_0 *emad_array_out;
459 struct ffa_comp_mrd *mrd_in;
460 struct ffa_comp_mrd *mrd_out;
461
462 size_t mrd_in_offset;
463 size_t mrd_out_offset;
464 size_t mrd_size = 0;
465
466 /* Populate the new descriptor format from the v1.0 struct. */
467 out->sender_id = mtd_orig->sender_id;
468 out->memory_region_attributes = mtd_orig->memory_region_attributes;
469 out->flags = mtd_orig->flags;
470 out->handle = mtd_orig->handle;
471 out->tag = mtd_orig->tag;
472 out->emad_count = mtd_orig->emad_count;
473 out->emad_size = sizeof(struct ffa_emad_v1_0);
474
475 /*
476 * We will locate the emad descriptors directly after the ffa_mtd
477 * struct. This will be 8-byte aligned.
478 */
479 out->emad_offset = sizeof(struct ffa_mtd);
480
481 emad_array_in = mtd_orig->emad;
482 emad_array_out = (struct ffa_emad_v1_0 *)
483 ((uint8_t *) out + out->emad_offset);
484
485 /* Copy across the emad structs. */
486 for (unsigned int i = 0U; i < out->emad_count; i++) {
Shruti Gupta20ce06c2022-08-25 14:22:53 +0100487 /* Bound check for emad array. */
488 if (((uint8_t *)emad_array_in + sizeof(struct ffa_emad_v1_0)) >
489 ((uint8_t *) mtd_orig + orig->desc_size)) {
490 VERBOSE("%s: Invalid mtd structure.\n", __func__);
491 return false;
492 }
Marc Bonnicid1907f02022-04-19 17:42:53 +0100493 memcpy(&emad_array_out[i], &emad_array_in[i],
494 sizeof(struct ffa_emad_v1_0));
495 }
496
497 /* Place the mrd descriptors after the end of the emad descriptors.*/
498 mrd_in_offset = emad_array_in->comp_mrd_offset;
499 mrd_out_offset = out->emad_offset + (out->emad_size * out->emad_count);
500 mrd_out = (struct ffa_comp_mrd *) ((uint8_t *) out + mrd_out_offset);
501
502 /* Add the size of the composite memory region descriptor. */
503 mrd_size += sizeof(struct ffa_comp_mrd);
504
505 /* Find the mrd descriptor. */
506 mrd_in = (struct ffa_comp_mrd *) ((uint8_t *) mtd_orig + mrd_in_offset);
507
508 /* Add the size of the constituent memory region descriptors. */
509 mrd_size += mrd_in->address_range_count * sizeof(struct ffa_cons_mrd);
510
511 /*
512 * Update the offset in the emads by the delta between the input and
513 * output addresses.
514 */
515 for (unsigned int i = 0U; i < out->emad_count; i++) {
516 emad_array_out[i].comp_mrd_offset =
517 emad_array_in[i].comp_mrd_offset +
518 (mrd_out_offset - mrd_in_offset);
519 }
520
521 /* Verify that we stay within bound of the memory descriptors. */
522 if ((uintptr_t)((uint8_t *) mrd_in + mrd_size) >
523 (uintptr_t)((uint8_t *) mtd_orig + orig->desc_size) ||
524 ((uintptr_t)((uint8_t *) mrd_out + mrd_size) >
525 (uintptr_t)((uint8_t *) out + out_obj->desc_size))) {
526 ERROR("%s: Invalid mrd structure.\n", __func__);
527 return false;
528 }
529
530 /* Copy the mrd descriptors directly. */
531 memcpy(mrd_out, mrd_in, mrd_size);
532
533 return true;
534}
535
536/**
537 * spmc_shm_convert_mtd_to_v1_0 - Converts a given v1.1 memory object to
538 * v1.0 memory object.
539 * @out_obj: The shared memory object to populate the v1.0 descriptor.
540 * @orig: The shared memory object containing the v1.1 descriptor.
541 *
542 * Return: true if the conversion is successful else false.
543 */
544static bool
545spmc_shm_convert_mtd_to_v1_0(struct spmc_shmem_obj *out_obj,
546 struct spmc_shmem_obj *orig)
547{
548 struct ffa_mtd *mtd_orig = &orig->desc;
549 struct ffa_mtd_v1_0 *out = (struct ffa_mtd_v1_0 *) &out_obj->desc;
550 struct ffa_emad_v1_0 *emad_in;
551 struct ffa_emad_v1_0 *emad_array_in;
552 struct ffa_emad_v1_0 *emad_array_out;
553 struct ffa_comp_mrd *mrd_in;
554 struct ffa_comp_mrd *mrd_out;
555
556 size_t mrd_in_offset;
557 size_t mrd_out_offset;
558 size_t emad_out_array_size;
559 size_t mrd_size = 0;
Shruti Gupta20ce06c2022-08-25 14:22:53 +0100560 size_t orig_desc_size = orig->desc_size;
Marc Bonnicid1907f02022-04-19 17:42:53 +0100561
562 /* Populate the v1.0 descriptor format from the v1.1 struct. */
563 out->sender_id = mtd_orig->sender_id;
564 out->memory_region_attributes = mtd_orig->memory_region_attributes;
565 out->flags = mtd_orig->flags;
566 out->handle = mtd_orig->handle;
567 out->tag = mtd_orig->tag;
568 out->emad_count = mtd_orig->emad_count;
569
570 /* Determine the location of the emad array in both descriptors. */
571 emad_array_in = (struct ffa_emad_v1_0 *)
572 ((uint8_t *) mtd_orig + mtd_orig->emad_offset);
573 emad_array_out = out->emad;
574
575 /* Copy across the emad structs. */
576 emad_in = emad_array_in;
577 for (unsigned int i = 0U; i < out->emad_count; i++) {
Shruti Gupta20ce06c2022-08-25 14:22:53 +0100578 /* Bound check for emad array. */
579 if (((uint8_t *)emad_in + sizeof(struct ffa_emad_v1_0)) >
580 ((uint8_t *) mtd_orig + orig_desc_size)) {
581 VERBOSE("%s: Invalid mtd structure.\n", __func__);
582 return false;
583 }
Marc Bonnicid1907f02022-04-19 17:42:53 +0100584 memcpy(&emad_array_out[i], emad_in,
585 sizeof(struct ffa_emad_v1_0));
586
587 emad_in += mtd_orig->emad_size;
588 }
589
590 /* Place the mrd descriptors after the end of the emad descriptors. */
591 emad_out_array_size = sizeof(struct ffa_emad_v1_0) * out->emad_count;
592
593 mrd_out_offset = (uint8_t *) out->emad - (uint8_t *) out +
594 emad_out_array_size;
595
596 mrd_out = (struct ffa_comp_mrd *) ((uint8_t *) out + mrd_out_offset);
597
598 mrd_in_offset = mtd_orig->emad_offset +
599 (mtd_orig->emad_size * mtd_orig->emad_count);
600
601 /* Add the size of the composite memory region descriptor. */
602 mrd_size += sizeof(struct ffa_comp_mrd);
603
604 /* Find the mrd descriptor. */
605 mrd_in = (struct ffa_comp_mrd *) ((uint8_t *) mtd_orig + mrd_in_offset);
606
607 /* Add the size of the constituent memory region descriptors. */
608 mrd_size += mrd_in->address_range_count * sizeof(struct ffa_cons_mrd);
609
610 /*
611 * Update the offset in the emads by the delta between the input and
612 * output addresses.
613 */
614 emad_in = emad_array_in;
615
616 for (unsigned int i = 0U; i < out->emad_count; i++) {
617 emad_array_out[i].comp_mrd_offset = emad_in->comp_mrd_offset +
618 (mrd_out_offset -
619 mrd_in_offset);
620 emad_in += mtd_orig->emad_size;
621 }
622
623 /* Verify that we stay within bound of the memory descriptors. */
624 if ((uintptr_t)((uint8_t *) mrd_in + mrd_size) >
625 (uintptr_t)((uint8_t *) mtd_orig + orig->desc_size) ||
626 ((uintptr_t)((uint8_t *) mrd_out + mrd_size) >
627 (uintptr_t)((uint8_t *) out + out_obj->desc_size))) {
628 ERROR("%s: Invalid mrd structure.\n", __func__);
629 return false;
630 }
631
632 /* Copy the mrd descriptors directly. */
633 memcpy(mrd_out, mrd_in, mrd_size);
634
635 return true;
636}
637
638/**
639 * spmc_populate_ffa_v1_0_descriptor - Converts a given v1.1 memory object to
640 * the v1.0 format and populates the
641 * provided buffer.
642 * @dst: Buffer to populate v1.0 ffa_memory_region_descriptor.
643 * @orig_obj: Object containing v1.1 ffa_memory_region_descriptor.
644 * @buf_size: Size of the buffer to populate.
645 * @offset: The offset of the converted descriptor to copy.
646 * @copy_size: Will be populated with the number of bytes copied.
647 * @out_desc_size: Will be populated with the total size of the v1.0
648 * descriptor.
649 *
650 * Return: 0 if conversion and population succeeded.
651 * Note: This function invalidates the reference to @orig therefore
652 * `spmc_shmem_obj_lookup` must be called if further usage is required.
653 */
654static uint32_t
655spmc_populate_ffa_v1_0_descriptor(void *dst, struct spmc_shmem_obj *orig_obj,
656 size_t buf_size, size_t offset,
657 size_t *copy_size, size_t *v1_0_desc_size)
658{
659 struct spmc_shmem_obj *v1_0_obj;
660
661 /* Calculate the size that the v1.0 descriptor will require. */
662 *v1_0_desc_size = spmc_shm_get_v1_0_descriptor_size(
663 &orig_obj->desc, orig_obj->desc_size);
664
665 if (*v1_0_desc_size == 0) {
666 ERROR("%s: cannot determine size of descriptor.\n",
667 __func__);
668 return FFA_ERROR_INVALID_PARAMETER;
669 }
670
671 /* Get a new obj to store the v1.0 descriptor. */
672 v1_0_obj = spmc_shmem_obj_alloc(&spmc_shmem_obj_state,
673 *v1_0_desc_size);
674
675 if (!v1_0_obj) {
676 return FFA_ERROR_NO_MEMORY;
677 }
678
679 /* Perform the conversion from v1.1 to v1.0. */
680 if (!spmc_shm_convert_mtd_to_v1_0(v1_0_obj, orig_obj)) {
681 spmc_shmem_obj_free(&spmc_shmem_obj_state, v1_0_obj);
682 return FFA_ERROR_INVALID_PARAMETER;
683 }
684
685 *copy_size = MIN(v1_0_obj->desc_size - offset, buf_size);
686 memcpy(dst, (uint8_t *) &v1_0_obj->desc + offset, *copy_size);
687
688 /*
689 * We're finished with the v1.0 descriptor for now so free it.
690 * Note that this will invalidate any references to the v1.1
691 * descriptor.
692 */
693 spmc_shmem_obj_free(&spmc_shmem_obj_state, v1_0_obj);
694
695 return 0;
696}
697
Demi Marie Obenour4ed9df42022-12-30 19:30:58 -0500698static int
699spmc_validate_mtd_start(struct ffa_mtd *desc, uint32_t ffa_version,
700 size_t fragment_length, size_t total_length)
701{
702 unsigned long long emad_end;
703 unsigned long long emad_size;
704 unsigned long long emad_offset;
705 unsigned int min_desc_size;
706
707 /* Determine the appropriate minimum descriptor size. */
708 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
709 min_desc_size = sizeof(struct ffa_mtd_v1_0);
710 } else if (ffa_version == MAKE_FFA_VERSION(1, 1)) {
711 min_desc_size = sizeof(struct ffa_mtd);
712 } else {
713 return FFA_ERROR_INVALID_PARAMETER;
714 }
715 if (fragment_length < min_desc_size) {
716 WARN("%s: invalid length %zu < %u\n", __func__, fragment_length,
717 min_desc_size);
718 return FFA_ERROR_INVALID_PARAMETER;
719 }
720
721 if (desc->emad_count == 0U) {
722 WARN("%s: unsupported attribute desc count %u.\n",
723 __func__, desc->emad_count);
724 return FFA_ERROR_INVALID_PARAMETER;
725 }
726
727 /*
728 * If the caller is using FF-A v1.0 interpret the descriptor as a v1.0
729 * format, otherwise assume it is a v1.1 format.
730 */
731 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
732 emad_offset = emad_size = sizeof(struct ffa_emad_v1_0);
733 } else {
734 if (!is_aligned(desc->emad_offset, 16)) {
735 WARN("%s: Emad offset %" PRIx32 " is not 16-byte aligned.\n",
736 __func__, desc->emad_offset);
737 return FFA_ERROR_INVALID_PARAMETER;
738 }
739 if (desc->emad_offset < sizeof(struct ffa_mtd)) {
740 WARN("%s: Emad offset too small: 0x%" PRIx32 " < 0x%zx.\n",
741 __func__, desc->emad_offset,
742 sizeof(struct ffa_mtd));
743 return FFA_ERROR_INVALID_PARAMETER;
744 }
745 emad_offset = desc->emad_offset;
746 if (desc->emad_size < sizeof(struct ffa_emad_v1_0)) {
747 WARN("%s: Bad emad size (%" PRIu32 " < %zu).\n", __func__,
748 desc->emad_size, sizeof(struct ffa_emad_v1_0));
749 return FFA_ERROR_INVALID_PARAMETER;
750 }
751 if (!is_aligned(desc->emad_size, 16)) {
752 WARN("%s: Emad size 0x%" PRIx32 " is not 16-byte aligned.\n",
753 __func__, desc->emad_size);
754 return FFA_ERROR_INVALID_PARAMETER;
755 }
756 emad_size = desc->emad_size;
757 }
758
759 /*
760 * Overflow is impossible: the arithmetic happens in at least 64-bit
761 * precision, but all of the operands are bounded by UINT32_MAX, and
762 * ((2^32 - 1)^2 + (2^32 - 1) + (2^32 - 1)) = ((2^32 - 1) * (2^32 + 1))
763 * = (2^64 - 1).
764 */
765 CASSERT(sizeof(desc->emad_count == 4), assert_emad_count_max_too_large);
766 emad_end = (desc->emad_count * (unsigned long long)emad_size) +
767 (unsigned long long)sizeof(struct ffa_comp_mrd) +
768 (unsigned long long)emad_offset;
769
770 if (emad_end > total_length) {
771 WARN("%s: Composite memory region extends beyond descriptor: 0x%llx > 0x%zx\n",
772 __func__, emad_end, total_length);
773 return FFA_ERROR_INVALID_PARAMETER;
774 }
775
776 return 0;
777}
778
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100779/**
780 * spmc_shmem_check_obj - Check that counts in descriptor match overall size.
Marc Bonnicid1907f02022-04-19 17:42:53 +0100781 * @obj: Object containing ffa_memory_region_descriptor.
782 * @ffa_version: FF-A version of the provided descriptor.
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100783 *
Marc Bonnici336630f2022-01-13 11:39:10 +0000784 * Return: 0 if object is valid, -EINVAL if constituent_memory_region_descriptor
785 * offset or count is invalid.
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100786 */
Marc Bonnicid1907f02022-04-19 17:42:53 +0100787static int spmc_shmem_check_obj(struct spmc_shmem_obj *obj,
788 uint32_t ffa_version)
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100789{
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000790 uint32_t comp_mrd_offset = 0;
791
Marc Bonnici336630f2022-01-13 11:39:10 +0000792 if (obj->desc.emad_count == 0U) {
793 WARN("%s: unsupported attribute desc count %u.\n",
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100794 __func__, obj->desc.emad_count);
795 return -EINVAL;
796 }
797
798 for (size_t emad_num = 0; emad_num < obj->desc.emad_count; emad_num++) {
799 size_t size;
800 size_t count;
801 size_t expected_size;
802 size_t total_page_count;
Marc Bonnicid1907f02022-04-19 17:42:53 +0100803 size_t emad_size;
804 size_t desc_size;
805 size_t header_emad_size;
806 uint32_t offset;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100807 struct ffa_comp_mrd *comp;
Marc Bonnicid1907f02022-04-19 17:42:53 +0100808 struct ffa_emad_v1_0 *emad;
809
810 emad = spmc_shmem_obj_get_emad(&obj->desc, emad_num,
811 ffa_version, &emad_size);
Marc Bonnicid1907f02022-04-19 17:42:53 +0100812
813 /*
814 * Validate the calculated emad address resides within the
815 * descriptor.
816 */
817 if ((uintptr_t) emad >=
818 (uintptr_t)((uint8_t *) &obj->desc + obj->desc_size)) {
819 WARN("Invalid emad access.\n");
820 return -EINVAL;
821 }
822
823 offset = emad->comp_mrd_offset;
824
Demi Marie Obenour8711be32023-01-11 14:20:07 -0500825 /*
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
Marc Bonnicid1907f02022-04-19 17:42:53 +0100841 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
842 desc_size = sizeof(struct ffa_mtd_v1_0);
843 } else {
844 desc_size = sizeof(struct ffa_mtd);
845 }
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100846
Marc Bonnicid1907f02022-04-19 17:42:53 +0100847 header_emad_size = desc_size +
848 (obj->desc.emad_count * emad_size);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100849
850 if (offset < header_emad_size) {
851 WARN("%s: invalid object, offset %u < header + emad %zu\n",
852 __func__, offset, header_emad_size);
853 return -EINVAL;
854 }
855
856 size = obj->desc_size;
857
858 if (offset > size) {
859 WARN("%s: invalid object, offset %u > total size %zu\n",
860 __func__, offset, obj->desc_size);
861 return -EINVAL;
862 }
863 size -= offset;
864
865 if (size < sizeof(struct ffa_comp_mrd)) {
866 WARN("%s: invalid object, offset %u, total size %zu, no header space.\n",
867 __func__, offset, obj->desc_size);
868 return -EINVAL;
869 }
870 size -= sizeof(struct ffa_comp_mrd);
871
872 count = size / sizeof(struct ffa_cons_mrd);
873
Marc Bonnicid1907f02022-04-19 17:42:53 +0100874 comp = spmc_shmem_obj_get_comp_mrd(obj, ffa_version);
875
876 if (comp == NULL) {
877 WARN("%s: invalid comp_mrd offset\n", __func__);
878 return -EINVAL;
879 }
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100880
881 if (comp->address_range_count != count) {
882 WARN("%s: invalid object, desc count %u != %zu\n",
883 __func__, comp->address_range_count, count);
884 return -EINVAL;
885 }
886
887 expected_size = offset + sizeof(*comp) +
Marc Bonnicid1907f02022-04-19 17:42:53 +0100888 spmc_shmem_obj_ffa_constituent_size(obj,
889 ffa_version);
890
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100891 if (expected_size != obj->desc_size) {
892 WARN("%s: invalid object, computed size %zu != size %zu\n",
893 __func__, expected_size, obj->desc_size);
894 return -EINVAL;
895 }
896
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100897 total_page_count = 0;
898
899 for (size_t i = 0; i < count; i++) {
900 total_page_count +=
901 comp->address_range_array[i].page_count;
902 }
903 if (comp->total_page_count != total_page_count) {
904 WARN("%s: invalid object, desc total_page_count %u != %zu\n",
905 __func__, comp->total_page_count,
906 total_page_count);
907 return -EINVAL;
908 }
909 }
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000910 return 0;
911}
912
913/**
914 * spmc_shmem_check_state_obj - Check if the descriptor describes memory
915 * regions that are currently involved with an
916 * existing memory transactions. This implies that
917 * the memory is not in a valid state for lending.
918 * @obj: Object containing ffa_memory_region_descriptor.
919 *
920 * Return: 0 if object is valid, -EINVAL if invalid memory state.
921 */
Marc Bonnicid1907f02022-04-19 17:42:53 +0100922static int spmc_shmem_check_state_obj(struct spmc_shmem_obj *obj,
923 uint32_t ffa_version)
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000924{
925 size_t obj_offset = 0;
926 struct spmc_shmem_obj *inflight_obj;
927
928 struct ffa_comp_mrd *other_mrd;
Marc Bonnicid1907f02022-04-19 17:42:53 +0100929 struct ffa_comp_mrd *requested_mrd = spmc_shmem_obj_get_comp_mrd(obj,
930 ffa_version);
931
932 if (requested_mrd == NULL) {
933 return -EINVAL;
934 }
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100935
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000936 inflight_obj = spmc_shmem_obj_get_next(&spmc_shmem_obj_state,
937 &obj_offset);
938
939 while (inflight_obj != NULL) {
940 /*
941 * Don't compare the transaction to itself or to partially
942 * transmitted descriptors.
943 */
944 if ((obj->desc.handle != inflight_obj->desc.handle) &&
945 (obj->desc_size == obj->desc_filled)) {
Marc Bonnicid1907f02022-04-19 17:42:53 +0100946 other_mrd = spmc_shmem_obj_get_comp_mrd(inflight_obj,
Marc Bonnici344ca9d2022-05-20 14:38:55 +0100947 FFA_VERSION_COMPILED);
Marc Bonnicid1907f02022-04-19 17:42:53 +0100948 if (other_mrd == NULL) {
949 return -EINVAL;
950 }
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000951 if (overlapping_memory_regions(requested_mrd,
952 other_mrd)) {
953 return -EINVAL;
954 }
955 }
956
957 inflight_obj = spmc_shmem_obj_get_next(&spmc_shmem_obj_state,
958 &obj_offset);
959 }
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100960 return 0;
961}
962
963static long spmc_ffa_fill_desc(struct mailbox *mbox,
964 struct spmc_shmem_obj *obj,
965 uint32_t fragment_length,
966 ffa_mtd_flag32_t mtd_flag,
Marc Bonnicid1907f02022-04-19 17:42:53 +0100967 uint32_t ffa_version,
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100968 void *smc_handle)
969{
970 int ret;
Marc Bonnicid1907f02022-04-19 17:42:53 +0100971 size_t emad_size;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100972 uint32_t handle_low;
973 uint32_t handle_high;
Marc Bonnicid1907f02022-04-19 17:42:53 +0100974 struct ffa_emad_v1_0 *emad;
975 struct ffa_emad_v1_0 *other_emad;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100976
977 if (mbox->rxtx_page_count == 0U) {
978 WARN("%s: buffer pair not registered.\n", __func__);
Marc Bonnicid1907f02022-04-19 17:42:53 +0100979 ret = FFA_ERROR_INVALID_PARAMETER;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100980 goto err_arg;
981 }
982
983 if (fragment_length > mbox->rxtx_page_count * PAGE_SIZE_4KB) {
984 WARN("%s: bad fragment size %u > %u buffer size\n", __func__,
985 fragment_length, mbox->rxtx_page_count * PAGE_SIZE_4KB);
Marc Bonnicid1907f02022-04-19 17:42:53 +0100986 ret = FFA_ERROR_INVALID_PARAMETER;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100987 goto err_arg;
988 }
989
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100990 if (fragment_length > obj->desc_size - obj->desc_filled) {
991 WARN("%s: bad fragment size %u > %zu remaining\n", __func__,
992 fragment_length, obj->desc_size - obj->desc_filled);
Marc Bonnicid1907f02022-04-19 17:42:53 +0100993 ret = FFA_ERROR_INVALID_PARAMETER;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100994 goto err_arg;
995 }
996
Marc Bonnicif0f45dc2022-10-18 13:57:16 +0100997 memcpy((uint8_t *)&obj->desc + obj->desc_filled,
998 (uint8_t *) mbox->tx_buffer, fragment_length);
999
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001000 /* Ensure that the sender ID resides in the normal world. */
1001 if (ffa_is_secure_world_id(obj->desc.sender_id)) {
1002 WARN("%s: Invalid sender ID 0x%x.\n",
1003 __func__, obj->desc.sender_id);
1004 ret = FFA_ERROR_DENIED;
1005 goto err_arg;
1006 }
1007
Marc Bonnici08f28ef2022-04-19 16:52:59 +01001008 /* Ensure the NS bit is set to 0. */
1009 if ((obj->desc.memory_region_attributes & FFA_MEM_ATTR_NS_BIT) != 0U) {
1010 WARN("%s: NS mem attributes flags MBZ.\n", __func__);
1011 ret = FFA_ERROR_INVALID_PARAMETER;
1012 goto err_arg;
1013 }
1014
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001015 /*
1016 * We don't currently support any optional flags so ensure none are
1017 * requested.
1018 */
1019 if (obj->desc.flags != 0U && mtd_flag != 0U &&
1020 (obj->desc.flags != mtd_flag)) {
1021 WARN("%s: invalid memory transaction flags %u != %u\n",
1022 __func__, obj->desc.flags, mtd_flag);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001023 ret = FFA_ERROR_INVALID_PARAMETER;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001024 goto err_arg;
1025 }
1026
1027 if (obj->desc_filled == 0U) {
1028 /* First fragment, descriptor header has been copied */
Demi Marie Obenour4ed9df42022-12-30 19:30:58 -05001029 ret = spmc_validate_mtd_start(&obj->desc, ffa_version,
1030 fragment_length, obj->desc_size);
1031 if (ret != 0) {
1032 goto err_bad_desc;
1033 }
1034
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001035 obj->desc.handle = spmc_shmem_obj_state.next_handle++;
1036 obj->desc.flags |= mtd_flag;
1037 }
1038
1039 obj->desc_filled += fragment_length;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001040
1041 handle_low = (uint32_t)obj->desc.handle;
1042 handle_high = obj->desc.handle >> 32;
1043
1044 if (obj->desc_filled != obj->desc_size) {
1045 SMC_RET8(smc_handle, FFA_MEM_FRAG_RX, handle_low,
1046 handle_high, obj->desc_filled,
1047 (uint32_t)obj->desc.sender_id << 16, 0, 0, 0);
1048 }
1049
Marc Bonnici336630f2022-01-13 11:39:10 +00001050 /* The full descriptor has been received, perform any final checks. */
1051
Demi Marie Obenourcdd3e722023-01-11 14:16:37 -05001052 ret = spmc_shmem_check_obj(obj, ffa_version);
1053 if (ret != 0) {
1054 ret = FFA_ERROR_INVALID_PARAMETER;
1055 goto err_bad_desc;
1056 }
1057
Marc Bonnici336630f2022-01-13 11:39:10 +00001058 /*
1059 * If a partition ID resides in the secure world validate that the
1060 * partition ID is for a known partition. Ignore any partition ID
1061 * belonging to the normal world as it is assumed the Hypervisor will
1062 * have validated these.
1063 */
1064 for (size_t i = 0; i < obj->desc.emad_count; i++) {
Marc Bonnicid1907f02022-04-19 17:42:53 +01001065 emad = spmc_shmem_obj_get_emad(&obj->desc, i, ffa_version,
1066 &emad_size);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001067
1068 ffa_endpoint_id16_t ep_id = emad->mapd.endpoint_id;
Marc Bonnici336630f2022-01-13 11:39:10 +00001069
1070 if (ffa_is_secure_world_id(ep_id)) {
1071 if (spmc_get_sp_ctx(ep_id) == NULL) {
1072 WARN("%s: Invalid receiver id 0x%x\n",
1073 __func__, ep_id);
1074 ret = FFA_ERROR_INVALID_PARAMETER;
1075 goto err_bad_desc;
1076 }
1077 }
1078 }
1079
1080 /* Ensure partition IDs are not duplicated. */
1081 for (size_t i = 0; i < obj->desc.emad_count; i++) {
Marc Bonnicid1907f02022-04-19 17:42:53 +01001082 emad = spmc_shmem_obj_get_emad(&obj->desc, i, ffa_version,
1083 &emad_size);
Demi Marie Obenour57bf10c2022-12-31 11:11:18 -05001084
Marc Bonnici336630f2022-01-13 11:39:10 +00001085 for (size_t j = i + 1; j < obj->desc.emad_count; j++) {
Marc Bonnicid1907f02022-04-19 17:42:53 +01001086 other_emad = spmc_shmem_obj_get_emad(&obj->desc, j,
1087 ffa_version,
1088 &emad_size);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001089
1090 if (emad->mapd.endpoint_id ==
1091 other_emad->mapd.endpoint_id) {
1092 WARN("%s: Duplicated endpoint id 0x%x\n",
1093 __func__, emad->mapd.endpoint_id);
1094 ret = FFA_ERROR_INVALID_PARAMETER;
1095 goto err_bad_desc;
1096 }
Marc Bonnici336630f2022-01-13 11:39:10 +00001097 }
1098 }
1099
Marc Bonnicid1907f02022-04-19 17:42:53 +01001100 ret = spmc_shmem_check_state_obj(obj, ffa_version);
Marc Bonnicic31ec9e2022-01-21 10:34:55 +00001101 if (ret) {
1102 ERROR("%s: invalid memory region descriptor.\n", __func__);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001103 ret = FFA_ERROR_INVALID_PARAMETER;
Marc Bonnicic31ec9e2022-01-21 10:34:55 +00001104 goto err_bad_desc;
1105 }
1106
Marc Bonnicid1907f02022-04-19 17:42:53 +01001107 /*
1108 * Everything checks out, if the sender was using FF-A v1.0, convert
1109 * the descriptor format to use the v1.1 structures.
1110 */
1111 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1112 struct spmc_shmem_obj *v1_1_obj;
1113 uint64_t mem_handle;
1114
1115 /* Calculate the size that the v1.1 descriptor will required. */
1116 size_t v1_1_desc_size =
1117 spmc_shm_get_v1_1_descriptor_size((void *) &obj->desc,
vallau0146dbac22022-08-08 14:10:14 +02001118 obj->desc_size);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001119
1120 if (v1_1_desc_size == 0U) {
1121 ERROR("%s: cannot determine size of descriptor.\n",
1122 __func__);
1123 goto err_arg;
1124 }
1125
1126 /* Get a new obj to store the v1.1 descriptor. */
1127 v1_1_obj =
1128 spmc_shmem_obj_alloc(&spmc_shmem_obj_state, v1_1_desc_size);
1129
vallau018f830992022-08-09 18:03:28 +02001130 if (!v1_1_obj) {
Marc Bonnicid1907f02022-04-19 17:42:53 +01001131 ret = FFA_ERROR_NO_MEMORY;
1132 goto err_arg;
1133 }
1134
1135 /* Perform the conversion from v1.0 to v1.1. */
1136 v1_1_obj->desc_size = v1_1_desc_size;
1137 v1_1_obj->desc_filled = v1_1_desc_size;
1138 if (!spmc_shm_convert_shmem_obj_from_v1_0(v1_1_obj, obj)) {
1139 ERROR("%s: Could not convert mtd!\n", __func__);
1140 spmc_shmem_obj_free(&spmc_shmem_obj_state, v1_1_obj);
1141 goto err_arg;
1142 }
1143
1144 /*
1145 * We're finished with the v1.0 descriptor so free it
1146 * and continue our checks with the new v1.1 descriptor.
1147 */
1148 mem_handle = obj->desc.handle;
1149 spmc_shmem_obj_free(&spmc_shmem_obj_state, obj);
1150 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, mem_handle);
1151 if (obj == NULL) {
1152 ERROR("%s: Failed to find converted descriptor.\n",
1153 __func__);
1154 ret = FFA_ERROR_INVALID_PARAMETER;
1155 return spmc_ffa_error_return(smc_handle, ret);
1156 }
1157 }
1158
Marc Bonnici503320e2022-02-21 15:02:36 +00001159 /* Allow for platform specific operations to be performed. */
1160 ret = plat_spmc_shmem_begin(&obj->desc);
1161 if (ret != 0) {
1162 goto err_arg;
1163 }
1164
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001165 SMC_RET8(smc_handle, FFA_SUCCESS_SMC32, 0, handle_low, handle_high, 0,
1166 0, 0, 0);
1167
1168err_bad_desc:
1169err_arg:
1170 spmc_shmem_obj_free(&spmc_shmem_obj_state, obj);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001171 return spmc_ffa_error_return(smc_handle, ret);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001172}
1173
1174/**
1175 * spmc_ffa_mem_send - FFA_MEM_SHARE/LEND implementation.
1176 * @client: Client state.
1177 * @total_length: Total length of shared memory descriptor.
1178 * @fragment_length: Length of fragment of shared memory descriptor passed in
1179 * this call.
1180 * @address: Not supported, must be 0.
1181 * @page_count: Not supported, must be 0.
1182 * @smc_handle: Handle passed to smc call. Used to return
1183 * FFA_MEM_FRAG_RX or SMC_FC_FFA_SUCCESS.
1184 *
1185 * Implements a subset of the FF-A FFA_MEM_SHARE and FFA_MEM_LEND calls needed
1186 * to share or lend memory from non-secure os to secure os (with no stream
1187 * endpoints).
1188 *
1189 * Return: 0 on success, error code on failure.
1190 */
1191long spmc_ffa_mem_send(uint32_t smc_fid,
1192 bool secure_origin,
1193 uint64_t total_length,
1194 uint32_t fragment_length,
1195 uint64_t address,
1196 uint32_t page_count,
1197 void *cookie,
1198 void *handle,
1199 uint64_t flags)
1200
1201{
1202 long ret;
1203 struct spmc_shmem_obj *obj;
1204 struct mailbox *mbox = spmc_get_mbox_desc(secure_origin);
1205 ffa_mtd_flag32_t mtd_flag;
Marc Bonnicid1907f02022-04-19 17:42:53 +01001206 uint32_t ffa_version = get_partition_ffa_version(secure_origin);
Demi Marie Obenour1f9f8302022-12-30 19:14:18 -05001207 size_t min_desc_size;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001208
1209 if (address != 0U || page_count != 0U) {
1210 WARN("%s: custom memory region for message not supported.\n",
1211 __func__);
1212 return spmc_ffa_error_return(handle,
1213 FFA_ERROR_INVALID_PARAMETER);
1214 }
1215
1216 if (secure_origin) {
1217 WARN("%s: unsupported share direction.\n", __func__);
1218 return spmc_ffa_error_return(handle,
1219 FFA_ERROR_INVALID_PARAMETER);
1220 }
1221
Demi Marie Obenour1f9f8302022-12-30 19:14:18 -05001222 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1223 min_desc_size = sizeof(struct ffa_mtd_v1_0);
1224 } else if (ffa_version == MAKE_FFA_VERSION(1, 1)) {
1225 min_desc_size = sizeof(struct ffa_mtd);
1226 } else {
1227 WARN("%s: bad FF-A version.\n", __func__);
1228 return spmc_ffa_error_return(handle,
1229 FFA_ERROR_INVALID_PARAMETER);
1230 }
1231
1232 /* Check if the descriptor is too small for the FF-A version. */
1233 if (fragment_length < min_desc_size) {
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001234 WARN("%s: bad first fragment size %u < %zu\n",
Marc Bonnicid1907f02022-04-19 17:42:53 +01001235 __func__, fragment_length, sizeof(struct ffa_mtd_v1_0));
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001236 return spmc_ffa_error_return(handle,
1237 FFA_ERROR_INVALID_PARAMETER);
1238 }
1239
1240 if ((smc_fid & FUNCID_NUM_MASK) == FFA_FNUM_MEM_SHARE) {
1241 mtd_flag = FFA_MTD_FLAG_TYPE_SHARE_MEMORY;
1242 } else if ((smc_fid & FUNCID_NUM_MASK) == FFA_FNUM_MEM_LEND) {
1243 mtd_flag = FFA_MTD_FLAG_TYPE_LEND_MEMORY;
1244 } else {
1245 WARN("%s: invalid memory management operation.\n", __func__);
1246 return spmc_ffa_error_return(handle,
1247 FFA_ERROR_INVALID_PARAMETER);
1248 }
1249
1250 spin_lock(&spmc_shmem_obj_state.lock);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001251 obj = spmc_shmem_obj_alloc(&spmc_shmem_obj_state, total_length);
1252 if (obj == NULL) {
1253 ret = FFA_ERROR_NO_MEMORY;
1254 goto err_unlock;
1255 }
1256
1257 spin_lock(&mbox->lock);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001258 ret = spmc_ffa_fill_desc(mbox, obj, fragment_length, mtd_flag,
1259 ffa_version, handle);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001260 spin_unlock(&mbox->lock);
1261
1262 spin_unlock(&spmc_shmem_obj_state.lock);
1263 return ret;
1264
1265err_unlock:
1266 spin_unlock(&spmc_shmem_obj_state.lock);
1267 return spmc_ffa_error_return(handle, ret);
1268}
1269
1270/**
1271 * spmc_ffa_mem_frag_tx - FFA_MEM_FRAG_TX implementation.
1272 * @client: Client state.
1273 * @handle_low: Handle_low value returned from FFA_MEM_FRAG_RX.
1274 * @handle_high: Handle_high value returned from FFA_MEM_FRAG_RX.
1275 * @fragment_length: Length of fragments transmitted.
1276 * @sender_id: Vmid of sender in bits [31:16]
1277 * @smc_handle: Handle passed to smc call. Used to return
1278 * FFA_MEM_FRAG_RX or SMC_FC_FFA_SUCCESS.
1279 *
1280 * Return: @smc_handle on success, error code on failure.
1281 */
1282long spmc_ffa_mem_frag_tx(uint32_t smc_fid,
1283 bool secure_origin,
1284 uint64_t handle_low,
1285 uint64_t handle_high,
1286 uint32_t fragment_length,
1287 uint32_t sender_id,
1288 void *cookie,
1289 void *handle,
1290 uint64_t flags)
1291{
1292 long ret;
1293 uint32_t desc_sender_id;
Marc Bonnicid1907f02022-04-19 17:42:53 +01001294 uint32_t ffa_version = get_partition_ffa_version(secure_origin);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001295 struct mailbox *mbox = spmc_get_mbox_desc(secure_origin);
1296
1297 struct spmc_shmem_obj *obj;
1298 uint64_t mem_handle = handle_low | (((uint64_t)handle_high) << 32);
1299
1300 spin_lock(&spmc_shmem_obj_state.lock);
1301
1302 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, mem_handle);
1303 if (obj == NULL) {
1304 WARN("%s: invalid handle, 0x%lx, not a valid handle.\n",
1305 __func__, mem_handle);
1306 ret = FFA_ERROR_INVALID_PARAMETER;
1307 goto err_unlock;
1308 }
1309
1310 desc_sender_id = (uint32_t)obj->desc.sender_id << 16;
1311 if (sender_id != desc_sender_id) {
1312 WARN("%s: invalid sender_id 0x%x != 0x%x\n", __func__,
1313 sender_id, desc_sender_id);
1314 ret = FFA_ERROR_INVALID_PARAMETER;
1315 goto err_unlock;
1316 }
1317
1318 if (obj->desc_filled == obj->desc_size) {
1319 WARN("%s: object desc already filled, %zu\n", __func__,
1320 obj->desc_filled);
1321 ret = FFA_ERROR_INVALID_PARAMETER;
1322 goto err_unlock;
1323 }
1324
1325 spin_lock(&mbox->lock);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001326 ret = spmc_ffa_fill_desc(mbox, obj, fragment_length, 0, ffa_version,
1327 handle);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001328 spin_unlock(&mbox->lock);
1329
1330 spin_unlock(&spmc_shmem_obj_state.lock);
1331 return ret;
1332
1333err_unlock:
1334 spin_unlock(&spmc_shmem_obj_state.lock);
1335 return spmc_ffa_error_return(handle, ret);
1336}
1337
1338/**
Marc Bonnici08f28ef2022-04-19 16:52:59 +01001339 * spmc_ffa_mem_retrieve_set_ns_bit - Set the NS bit in the response descriptor
1340 * if the caller implements a version greater
1341 * than FF-A 1.0 or if they have requested
1342 * the functionality.
1343 * TODO: We are assuming that the caller is
1344 * an SP. To support retrieval from the
1345 * normal world this function will need to be
1346 * expanded accordingly.
1347 * @resp: Descriptor populated in callers RX buffer.
1348 * @sp_ctx: Context of the calling SP.
1349 */
1350void spmc_ffa_mem_retrieve_set_ns_bit(struct ffa_mtd *resp,
1351 struct secure_partition_desc *sp_ctx)
1352{
1353 if (sp_ctx->ffa_version > MAKE_FFA_VERSION(1, 0) ||
1354 sp_ctx->ns_bit_requested) {
1355 /*
1356 * Currently memory senders must reside in the normal
1357 * world, and we do not have the functionlaity to change
1358 * the state of memory dynamically. Therefore we can always set
1359 * the NS bit to 1.
1360 */
1361 resp->memory_region_attributes |= FFA_MEM_ATTR_NS_BIT;
1362 }
1363}
1364
1365/**
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001366 * spmc_ffa_mem_retrieve_req - FFA_MEM_RETRIEVE_REQ implementation.
1367 * @smc_fid: FID of SMC
1368 * @total_length: Total length of retrieve request descriptor if this is
1369 * the first call. Otherwise (unsupported) must be 0.
1370 * @fragment_length: Length of fragment of retrieve request descriptor passed
1371 * in this call. Only @fragment_length == @length is
1372 * supported by this implementation.
1373 * @address: Not supported, must be 0.
1374 * @page_count: Not supported, must be 0.
1375 * @smc_handle: Handle passed to smc call. Used to return
1376 * FFA_MEM_RETRIEVE_RESP.
1377 *
1378 * Implements a subset of the FF-A FFA_MEM_RETRIEVE_REQ call.
1379 * Used by secure os to retrieve memory already shared by non-secure os.
1380 * If the data does not fit in a single FFA_MEM_RETRIEVE_RESP message,
1381 * the client must call FFA_MEM_FRAG_RX until the full response has been
1382 * received.
1383 *
1384 * Return: @handle on success, error code on failure.
1385 */
1386long
1387spmc_ffa_mem_retrieve_req(uint32_t smc_fid,
1388 bool secure_origin,
1389 uint32_t total_length,
1390 uint32_t fragment_length,
1391 uint64_t address,
1392 uint32_t page_count,
1393 void *cookie,
1394 void *handle,
1395 uint64_t flags)
1396{
1397 int ret;
1398 size_t buf_size;
Marc Bonnicid1907f02022-04-19 17:42:53 +01001399 size_t copy_size = 0;
1400 size_t min_desc_size;
1401 size_t out_desc_size = 0;
1402
1403 /*
1404 * Currently we are only accessing fields that are the same in both the
1405 * v1.0 and v1.1 mtd struct therefore we can use a v1.1 struct directly
1406 * here. We only need validate against the appropriate struct size.
1407 */
1408 struct ffa_mtd *resp;
1409 const struct ffa_mtd *req;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001410 struct spmc_shmem_obj *obj = NULL;
1411 struct mailbox *mbox = spmc_get_mbox_desc(secure_origin);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001412 uint32_t ffa_version = get_partition_ffa_version(secure_origin);
Marc Bonnici08f28ef2022-04-19 16:52:59 +01001413 struct secure_partition_desc *sp_ctx = spmc_get_current_sp_ctx();
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001414
1415 if (!secure_origin) {
1416 WARN("%s: unsupported retrieve req direction.\n", __func__);
1417 return spmc_ffa_error_return(handle,
1418 FFA_ERROR_INVALID_PARAMETER);
1419 }
1420
1421 if (address != 0U || page_count != 0U) {
1422 WARN("%s: custom memory region not supported.\n", __func__);
1423 return spmc_ffa_error_return(handle,
1424 FFA_ERROR_INVALID_PARAMETER);
1425 }
1426
1427 spin_lock(&mbox->lock);
1428
1429 req = mbox->tx_buffer;
1430 resp = mbox->rx_buffer;
1431 buf_size = mbox->rxtx_page_count * FFA_PAGE_SIZE;
1432
1433 if (mbox->rxtx_page_count == 0U) {
1434 WARN("%s: buffer pair not registered.\n", __func__);
1435 ret = FFA_ERROR_INVALID_PARAMETER;
1436 goto err_unlock_mailbox;
1437 }
1438
1439 if (mbox->state != MAILBOX_STATE_EMPTY) {
1440 WARN("%s: RX Buffer is full! %d\n", __func__, mbox->state);
1441 ret = FFA_ERROR_DENIED;
1442 goto err_unlock_mailbox;
1443 }
1444
1445 if (fragment_length != total_length) {
1446 WARN("%s: fragmented retrieve request not supported.\n",
1447 __func__);
1448 ret = FFA_ERROR_INVALID_PARAMETER;
1449 goto err_unlock_mailbox;
1450 }
1451
Marc Bonnici336630f2022-01-13 11:39:10 +00001452 if (req->emad_count == 0U) {
1453 WARN("%s: unsupported attribute desc count %u.\n",
1454 __func__, obj->desc.emad_count);
vallau01460d3962022-08-09 17:06:53 +02001455 ret = FFA_ERROR_INVALID_PARAMETER;
1456 goto err_unlock_mailbox;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001457 }
1458
Marc Bonnicid1907f02022-04-19 17:42:53 +01001459 /* Determine the appropriate minimum descriptor size. */
1460 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1461 min_desc_size = sizeof(struct ffa_mtd_v1_0);
1462 } else {
1463 min_desc_size = sizeof(struct ffa_mtd);
1464 }
1465 if (total_length < min_desc_size) {
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001466 WARN("%s: invalid length %u < %zu\n", __func__, total_length,
Marc Bonnicid1907f02022-04-19 17:42:53 +01001467 min_desc_size);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001468 ret = FFA_ERROR_INVALID_PARAMETER;
1469 goto err_unlock_mailbox;
1470 }
1471
1472 spin_lock(&spmc_shmem_obj_state.lock);
1473
1474 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, req->handle);
1475 if (obj == NULL) {
1476 ret = FFA_ERROR_INVALID_PARAMETER;
1477 goto err_unlock_all;
1478 }
1479
1480 if (obj->desc_filled != obj->desc_size) {
1481 WARN("%s: incomplete object desc filled %zu < size %zu\n",
1482 __func__, obj->desc_filled, obj->desc_size);
1483 ret = FFA_ERROR_INVALID_PARAMETER;
1484 goto err_unlock_all;
1485 }
1486
1487 if (req->emad_count != 0U && req->sender_id != obj->desc.sender_id) {
1488 WARN("%s: wrong sender id 0x%x != 0x%x\n",
1489 __func__, req->sender_id, obj->desc.sender_id);
1490 ret = FFA_ERROR_INVALID_PARAMETER;
1491 goto err_unlock_all;
1492 }
1493
1494 if (req->emad_count != 0U && req->tag != obj->desc.tag) {
1495 WARN("%s: wrong tag 0x%lx != 0x%lx\n",
1496 __func__, req->tag, obj->desc.tag);
1497 ret = FFA_ERROR_INVALID_PARAMETER;
1498 goto err_unlock_all;
1499 }
1500
Marc Bonnici336630f2022-01-13 11:39:10 +00001501 if (req->emad_count != 0U && req->emad_count != obj->desc.emad_count) {
1502 WARN("%s: mistmatch of endpoint counts %u != %u\n",
1503 __func__, req->emad_count, obj->desc.emad_count);
1504 ret = FFA_ERROR_INVALID_PARAMETER;
1505 goto err_unlock_all;
1506 }
1507
Marc Bonnici08f28ef2022-04-19 16:52:59 +01001508 /* Ensure the NS bit is set to 0 in the request. */
1509 if ((req->memory_region_attributes & FFA_MEM_ATTR_NS_BIT) != 0U) {
1510 WARN("%s: NS mem attributes flags MBZ.\n", __func__);
1511 ret = FFA_ERROR_INVALID_PARAMETER;
1512 goto err_unlock_all;
1513 }
1514
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001515 if (req->flags != 0U) {
1516 if ((req->flags & FFA_MTD_FLAG_TYPE_MASK) !=
1517 (obj->desc.flags & FFA_MTD_FLAG_TYPE_MASK)) {
1518 /*
1519 * If the retrieve request specifies the memory
1520 * transaction ensure it matches what we expect.
1521 */
1522 WARN("%s: wrong mem transaction flags %x != %x\n",
1523 __func__, req->flags, obj->desc.flags);
1524 ret = FFA_ERROR_INVALID_PARAMETER;
1525 goto err_unlock_all;
1526 }
1527
1528 if (req->flags != FFA_MTD_FLAG_TYPE_SHARE_MEMORY &&
1529 req->flags != FFA_MTD_FLAG_TYPE_LEND_MEMORY) {
1530 /*
1531 * Current implementation does not support donate and
1532 * it supports no other flags.
1533 */
1534 WARN("%s: invalid flags 0x%x\n", __func__, req->flags);
1535 ret = FFA_ERROR_INVALID_PARAMETER;
1536 goto err_unlock_all;
1537 }
1538 }
1539
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001540 /* Validate the caller is a valid participant. */
Shruti Gupta20ce06c2022-08-25 14:22:53 +01001541 if (!spmc_shmem_obj_validate_id(obj, sp_ctx->sp_id)) {
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001542 WARN("%s: Invalid endpoint ID (0x%x).\n",
1543 __func__, sp_ctx->sp_id);
1544 ret = FFA_ERROR_INVALID_PARAMETER;
1545 goto err_unlock_all;
1546 }
1547
Marc Bonnicid1907f02022-04-19 17:42:53 +01001548 /* Validate that the provided emad offset and structure is valid.*/
1549 for (size_t i = 0; i < req->emad_count; i++) {
1550 size_t emad_size;
1551 struct ffa_emad_v1_0 *emad;
1552
1553 emad = spmc_shmem_obj_get_emad(req, i, ffa_version,
1554 &emad_size);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001555
1556 if ((uintptr_t) emad >= (uintptr_t)
1557 ((uint8_t *) req + total_length)) {
1558 WARN("Invalid emad access.\n");
1559 ret = FFA_ERROR_INVALID_PARAMETER;
1560 goto err_unlock_all;
1561 }
Marc Bonnici336630f2022-01-13 11:39:10 +00001562 }
1563
1564 /*
1565 * Validate all the endpoints match in the case of multiple
1566 * borrowers. We don't mandate that the order of the borrowers
1567 * must match in the descriptors therefore check to see if the
1568 * endpoints match in any order.
1569 */
1570 for (size_t i = 0; i < req->emad_count; i++) {
1571 bool found = false;
Marc Bonnicid1907f02022-04-19 17:42:53 +01001572 size_t emad_size;
1573 struct ffa_emad_v1_0 *emad;
1574 struct ffa_emad_v1_0 *other_emad;
1575
1576 emad = spmc_shmem_obj_get_emad(req, i, ffa_version,
1577 &emad_size);
Marc Bonnici336630f2022-01-13 11:39:10 +00001578
1579 for (size_t j = 0; j < obj->desc.emad_count; j++) {
Marc Bonnicid1907f02022-04-19 17:42:53 +01001580 other_emad = spmc_shmem_obj_get_emad(
1581 &obj->desc, j, MAKE_FFA_VERSION(1, 1),
1582 &emad_size);
1583
Marc Bonnicid1907f02022-04-19 17:42:53 +01001584 if (req->emad_count &&
1585 emad->mapd.endpoint_id ==
1586 other_emad->mapd.endpoint_id) {
Marc Bonnici336630f2022-01-13 11:39:10 +00001587 found = true;
1588 break;
1589 }
1590 }
1591
1592 if (!found) {
1593 WARN("%s: invalid receiver id (0x%x).\n",
Marc Bonnicid1907f02022-04-19 17:42:53 +01001594 __func__, emad->mapd.endpoint_id);
Marc Bonnici336630f2022-01-13 11:39:10 +00001595 ret = FFA_ERROR_INVALID_PARAMETER;
1596 goto err_unlock_all;
1597 }
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001598 }
1599
1600 mbox->state = MAILBOX_STATE_FULL;
1601
1602 if (req->emad_count != 0U) {
1603 obj->in_use++;
1604 }
1605
Marc Bonnicid1907f02022-04-19 17:42:53 +01001606 /*
1607 * If the caller is v1.0 convert the descriptor, otherwise copy
1608 * directly.
1609 */
1610 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1611 ret = spmc_populate_ffa_v1_0_descriptor(resp, obj, buf_size, 0,
1612 &copy_size,
1613 &out_desc_size);
1614 if (ret != 0U) {
1615 ERROR("%s: Failed to process descriptor.\n", __func__);
1616 goto err_unlock_all;
1617 }
1618 } else {
1619 copy_size = MIN(obj->desc_size, buf_size);
1620 out_desc_size = obj->desc_size;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001621
Marc Bonnicid1907f02022-04-19 17:42:53 +01001622 memcpy(resp, &obj->desc, copy_size);
1623 }
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001624
Marc Bonnici08f28ef2022-04-19 16:52:59 +01001625 /* Set the NS bit in the response if applicable. */
1626 spmc_ffa_mem_retrieve_set_ns_bit(resp, sp_ctx);
1627
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001628 spin_unlock(&spmc_shmem_obj_state.lock);
1629 spin_unlock(&mbox->lock);
1630
Marc Bonnicid1907f02022-04-19 17:42:53 +01001631 SMC_RET8(handle, FFA_MEM_RETRIEVE_RESP, out_desc_size,
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001632 copy_size, 0, 0, 0, 0, 0);
1633
1634err_unlock_all:
1635 spin_unlock(&spmc_shmem_obj_state.lock);
1636err_unlock_mailbox:
1637 spin_unlock(&mbox->lock);
1638 return spmc_ffa_error_return(handle, ret);
1639}
1640
1641/**
1642 * spmc_ffa_mem_frag_rx - FFA_MEM_FRAG_RX implementation.
1643 * @client: Client state.
1644 * @handle_low: Handle passed to &FFA_MEM_RETRIEVE_REQ. Bit[31:0].
1645 * @handle_high: Handle passed to &FFA_MEM_RETRIEVE_REQ. Bit[63:32].
1646 * @fragment_offset: Byte offset in descriptor to resume at.
1647 * @sender_id: Bit[31:16]: Endpoint id of sender if client is a
1648 * hypervisor. 0 otherwise.
1649 * @smc_handle: Handle passed to smc call. Used to return
1650 * FFA_MEM_FRAG_TX.
1651 *
1652 * Return: @smc_handle on success, error code on failure.
1653 */
1654long spmc_ffa_mem_frag_rx(uint32_t smc_fid,
1655 bool secure_origin,
1656 uint32_t handle_low,
1657 uint32_t handle_high,
1658 uint32_t fragment_offset,
1659 uint32_t sender_id,
1660 void *cookie,
1661 void *handle,
1662 uint64_t flags)
1663{
1664 int ret;
1665 void *src;
1666 size_t buf_size;
1667 size_t copy_size;
1668 size_t full_copy_size;
1669 uint32_t desc_sender_id;
1670 struct mailbox *mbox = spmc_get_mbox_desc(secure_origin);
1671 uint64_t mem_handle = handle_low | (((uint64_t)handle_high) << 32);
1672 struct spmc_shmem_obj *obj;
Marc Bonnicid1907f02022-04-19 17:42:53 +01001673 uint32_t ffa_version = get_partition_ffa_version(secure_origin);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001674
1675 if (!secure_origin) {
1676 WARN("%s: can only be called from swld.\n",
1677 __func__);
1678 return spmc_ffa_error_return(handle,
1679 FFA_ERROR_INVALID_PARAMETER);
1680 }
1681
1682 spin_lock(&spmc_shmem_obj_state.lock);
1683
1684 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, mem_handle);
1685 if (obj == NULL) {
1686 WARN("%s: invalid handle, 0x%lx, not a valid handle.\n",
1687 __func__, mem_handle);
1688 ret = FFA_ERROR_INVALID_PARAMETER;
1689 goto err_unlock_shmem;
1690 }
1691
1692 desc_sender_id = (uint32_t)obj->desc.sender_id << 16;
1693 if (sender_id != 0U && sender_id != desc_sender_id) {
1694 WARN("%s: invalid sender_id 0x%x != 0x%x\n", __func__,
1695 sender_id, desc_sender_id);
1696 ret = FFA_ERROR_INVALID_PARAMETER;
1697 goto err_unlock_shmem;
1698 }
1699
1700 if (fragment_offset >= obj->desc_size) {
1701 WARN("%s: invalid fragment_offset 0x%x >= 0x%zx\n",
1702 __func__, fragment_offset, obj->desc_size);
1703 ret = FFA_ERROR_INVALID_PARAMETER;
1704 goto err_unlock_shmem;
1705 }
1706
1707 spin_lock(&mbox->lock);
1708
1709 if (mbox->rxtx_page_count == 0U) {
1710 WARN("%s: buffer pair not registered.\n", __func__);
1711 ret = FFA_ERROR_INVALID_PARAMETER;
1712 goto err_unlock_all;
1713 }
1714
1715 if (mbox->state != MAILBOX_STATE_EMPTY) {
1716 WARN("%s: RX Buffer is full!\n", __func__);
1717 ret = FFA_ERROR_DENIED;
1718 goto err_unlock_all;
1719 }
1720
1721 buf_size = mbox->rxtx_page_count * FFA_PAGE_SIZE;
1722
1723 mbox->state = MAILBOX_STATE_FULL;
1724
Marc Bonnicid1907f02022-04-19 17:42:53 +01001725 /*
1726 * If the caller is v1.0 convert the descriptor, otherwise copy
1727 * directly.
1728 */
1729 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1730 size_t out_desc_size;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001731
Marc Bonnicid1907f02022-04-19 17:42:53 +01001732 ret = spmc_populate_ffa_v1_0_descriptor(mbox->rx_buffer, obj,
1733 buf_size,
1734 fragment_offset,
1735 &copy_size,
1736 &out_desc_size);
1737 if (ret != 0U) {
1738 ERROR("%s: Failed to process descriptor.\n", __func__);
1739 goto err_unlock_all;
1740 }
1741 } else {
1742 full_copy_size = obj->desc_size - fragment_offset;
1743 copy_size = MIN(full_copy_size, buf_size);
1744
1745 src = &obj->desc;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001746
Marc Bonnicid1907f02022-04-19 17:42:53 +01001747 memcpy(mbox->rx_buffer, src + fragment_offset, copy_size);
1748 }
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001749
1750 spin_unlock(&mbox->lock);
1751 spin_unlock(&spmc_shmem_obj_state.lock);
1752
1753 SMC_RET8(handle, FFA_MEM_FRAG_TX, handle_low, handle_high,
1754 copy_size, sender_id, 0, 0, 0);
1755
1756err_unlock_all:
1757 spin_unlock(&mbox->lock);
1758err_unlock_shmem:
1759 spin_unlock(&spmc_shmem_obj_state.lock);
1760 return spmc_ffa_error_return(handle, ret);
1761}
1762
1763/**
1764 * spmc_ffa_mem_relinquish - FFA_MEM_RELINQUISH implementation.
1765 * @client: Client state.
1766 *
1767 * Implements a subset of the FF-A FFA_MEM_RELINQUISH call.
1768 * Used by secure os release previously shared memory to non-secure os.
1769 *
1770 * The handle to release must be in the client's (secure os's) transmit buffer.
1771 *
1772 * Return: 0 on success, error code on failure.
1773 */
1774int spmc_ffa_mem_relinquish(uint32_t smc_fid,
1775 bool secure_origin,
1776 uint32_t handle_low,
1777 uint32_t handle_high,
1778 uint32_t fragment_offset,
1779 uint32_t sender_id,
1780 void *cookie,
1781 void *handle,
1782 uint64_t flags)
1783{
1784 int ret;
1785 struct mailbox *mbox = spmc_get_mbox_desc(secure_origin);
1786 struct spmc_shmem_obj *obj;
1787 const struct ffa_mem_relinquish_descriptor *req;
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001788 struct secure_partition_desc *sp_ctx = spmc_get_current_sp_ctx();
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001789
1790 if (!secure_origin) {
1791 WARN("%s: unsupported relinquish direction.\n", __func__);
1792 return spmc_ffa_error_return(handle,
1793 FFA_ERROR_INVALID_PARAMETER);
1794 }
1795
1796 spin_lock(&mbox->lock);
1797
1798 if (mbox->rxtx_page_count == 0U) {
1799 WARN("%s: buffer pair not registered.\n", __func__);
1800 ret = FFA_ERROR_INVALID_PARAMETER;
1801 goto err_unlock_mailbox;
1802 }
1803
1804 req = mbox->tx_buffer;
1805
1806 if (req->flags != 0U) {
1807 WARN("%s: unsupported flags 0x%x\n", __func__, req->flags);
1808 ret = FFA_ERROR_INVALID_PARAMETER;
1809 goto err_unlock_mailbox;
1810 }
1811
Marc Bonnici336630f2022-01-13 11:39:10 +00001812 if (req->endpoint_count == 0) {
1813 WARN("%s: endpoint count cannot be 0.\n", __func__);
1814 ret = FFA_ERROR_INVALID_PARAMETER;
1815 goto err_unlock_mailbox;
1816 }
1817
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001818 spin_lock(&spmc_shmem_obj_state.lock);
1819
1820 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, req->handle);
1821 if (obj == NULL) {
1822 ret = FFA_ERROR_INVALID_PARAMETER;
1823 goto err_unlock_all;
1824 }
1825
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001826 /*
1827 * Validate the endpoint ID was populated correctly. We don't currently
1828 * support proxy endpoints so the endpoint count should always be 1.
1829 */
1830 if (req->endpoint_count != 1U) {
1831 WARN("%s: unsupported endpoint count %u != 1\n", __func__,
1832 req->endpoint_count);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001833 ret = FFA_ERROR_INVALID_PARAMETER;
1834 goto err_unlock_all;
1835 }
Marc Bonnici336630f2022-01-13 11:39:10 +00001836
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001837 /* Validate provided endpoint ID matches the partition ID. */
1838 if (req->endpoint_array[0] != sp_ctx->sp_id) {
1839 WARN("%s: invalid endpoint ID %u != %u\n", __func__,
1840 req->endpoint_array[0], sp_ctx->sp_id);
1841 ret = FFA_ERROR_INVALID_PARAMETER;
1842 goto err_unlock_all;
1843 }
Marc Bonnici336630f2022-01-13 11:39:10 +00001844
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001845 /* Validate the caller is a valid participant. */
Shruti Gupta20ce06c2022-08-25 14:22:53 +01001846 if (!spmc_shmem_obj_validate_id(obj, sp_ctx->sp_id)) {
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001847 WARN("%s: Invalid endpoint ID (0x%x).\n",
1848 __func__, req->endpoint_array[0]);
1849 ret = FFA_ERROR_INVALID_PARAMETER;
1850 goto err_unlock_all;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001851 }
Marc Bonnici336630f2022-01-13 11:39:10 +00001852
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001853 if (obj->in_use == 0U) {
1854 ret = FFA_ERROR_INVALID_PARAMETER;
1855 goto err_unlock_all;
1856 }
1857 obj->in_use--;
1858
1859 spin_unlock(&spmc_shmem_obj_state.lock);
1860 spin_unlock(&mbox->lock);
1861
1862 SMC_RET1(handle, FFA_SUCCESS_SMC32);
1863
1864err_unlock_all:
1865 spin_unlock(&spmc_shmem_obj_state.lock);
1866err_unlock_mailbox:
1867 spin_unlock(&mbox->lock);
1868 return spmc_ffa_error_return(handle, ret);
1869}
1870
1871/**
1872 * spmc_ffa_mem_reclaim - FFA_MEM_RECLAIM implementation.
1873 * @client: Client state.
1874 * @handle_low: Unique handle of shared memory object to reclaim. Bit[31:0].
1875 * @handle_high: Unique handle of shared memory object to reclaim.
1876 * Bit[63:32].
1877 * @flags: Unsupported, ignored.
1878 *
1879 * Implements a subset of the FF-A FFA_MEM_RECLAIM call.
1880 * Used by non-secure os reclaim memory previously shared with secure os.
1881 *
1882 * Return: 0 on success, error code on failure.
1883 */
1884int spmc_ffa_mem_reclaim(uint32_t smc_fid,
1885 bool secure_origin,
1886 uint32_t handle_low,
1887 uint32_t handle_high,
1888 uint32_t mem_flags,
1889 uint64_t x4,
1890 void *cookie,
1891 void *handle,
1892 uint64_t flags)
1893{
1894 int ret;
1895 struct spmc_shmem_obj *obj;
1896 uint64_t mem_handle = handle_low | (((uint64_t)handle_high) << 32);
1897
1898 if (secure_origin) {
1899 WARN("%s: unsupported reclaim direction.\n", __func__);
1900 return spmc_ffa_error_return(handle,
1901 FFA_ERROR_INVALID_PARAMETER);
1902 }
1903
1904 if (mem_flags != 0U) {
1905 WARN("%s: unsupported flags 0x%x\n", __func__, mem_flags);
1906 return spmc_ffa_error_return(handle,
1907 FFA_ERROR_INVALID_PARAMETER);
1908 }
1909
1910 spin_lock(&spmc_shmem_obj_state.lock);
1911
1912 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, mem_handle);
1913 if (obj == NULL) {
1914 ret = FFA_ERROR_INVALID_PARAMETER;
1915 goto err_unlock;
1916 }
1917 if (obj->in_use != 0U) {
1918 ret = FFA_ERROR_DENIED;
1919 goto err_unlock;
1920 }
Marc Bonnici503320e2022-02-21 15:02:36 +00001921
Marc Bonnici82e28f12022-10-18 13:39:48 +01001922 if (obj->desc_filled != obj->desc_size) {
1923 WARN("%s: incomplete object desc filled %zu < size %zu\n",
1924 __func__, obj->desc_filled, obj->desc_size);
1925 ret = FFA_ERROR_INVALID_PARAMETER;
1926 goto err_unlock;
1927 }
1928
Marc Bonnici503320e2022-02-21 15:02:36 +00001929 /* Allow for platform specific operations to be performed. */
1930 ret = plat_spmc_shmem_reclaim(&obj->desc);
1931 if (ret != 0) {
1932 goto err_unlock;
1933 }
1934
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001935 spmc_shmem_obj_free(&spmc_shmem_obj_state, obj);
1936 spin_unlock(&spmc_shmem_obj_state.lock);
1937
1938 SMC_RET1(handle, FFA_SUCCESS_SMC32);
1939
1940err_unlock:
1941 spin_unlock(&spmc_shmem_obj_state.lock);
1942 return spmc_ffa_error_return(handle, ret);
1943}