blob: 5dc60f6acfd18390b25b3502f194e22ffd5c8b72 [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;
Demi Marie Obenourf00e4d72023-01-12 13:25:23 -0500791 if (obj->desc_filled != obj->desc_size) {
792 ERROR("BUG: %s called on incomplete object (%zu != %zu)\n",
793 __func__, obj->desc_filled, obj->desc_size);
794 panic();
795 }
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000796
Demi Marie Obenourf00e4d72023-01-12 13:25:23 -0500797 if (spmc_validate_mtd_start(&obj->desc, ffa_version,
798 obj->desc_filled, obj->desc_size)) {
799 ERROR("BUG: %s called on object with corrupt memory region descriptor\n",
800 __func__);
801 panic();
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100802 }
803
804 for (size_t emad_num = 0; emad_num < obj->desc.emad_count; emad_num++) {
805 size_t size;
806 size_t count;
807 size_t expected_size;
Demi Marie Obenour00d36b22023-01-12 13:24:50 -0500808 uint64_t total_page_count;
Marc Bonnicid1907f02022-04-19 17:42:53 +0100809 size_t emad_size;
Marc Bonnicid1907f02022-04-19 17:42:53 +0100810 size_t header_emad_size;
811 uint32_t offset;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100812 struct ffa_comp_mrd *comp;
Marc Bonnicid1907f02022-04-19 17:42:53 +0100813 struct ffa_emad_v1_0 *emad;
814
815 emad = spmc_shmem_obj_get_emad(&obj->desc, emad_num,
816 ffa_version, &emad_size);
Marc Bonnicid1907f02022-04-19 17:42:53 +0100817
818 /*
819 * Validate the calculated emad address resides within the
820 * descriptor.
821 */
822 if ((uintptr_t) emad >=
823 (uintptr_t)((uint8_t *) &obj->desc + obj->desc_size)) {
824 WARN("Invalid emad access.\n");
825 return -EINVAL;
826 }
827
828 offset = emad->comp_mrd_offset;
829
Demi Marie Obenour8711be32023-01-11 14:20:07 -0500830 /*
831 * The offset provided to the composite memory region descriptor
832 * should be consistent across endpoint descriptors. Store the
833 * first entry and compare against subsequent entries.
834 */
835 if (comp_mrd_offset == 0) {
836 comp_mrd_offset = offset;
837 } else {
838 if (comp_mrd_offset != offset) {
839 ERROR("%s: mismatching offsets provided, %u != %u\n",
840 __func__, offset, comp_mrd_offset);
841 return -EINVAL;
842 }
843 continue; /* Remainder only executed on first iteration. */
844 }
845
Demi Marie Obenour2bb87352023-01-11 14:25:24 -0500846 header_emad_size = (size_t)((uint8_t *)emad - (uint8_t *)&obj->desc) +
Marc Bonnicid1907f02022-04-19 17:42:53 +0100847 (obj->desc.emad_count * emad_size);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100848
849 if (offset < header_emad_size) {
850 WARN("%s: invalid object, offset %u < header + emad %zu\n",
851 __func__, offset, header_emad_size);
852 return -EINVAL;
853 }
854
855 size = obj->desc_size;
856
857 if (offset > size) {
858 WARN("%s: invalid object, offset %u > total size %zu\n",
859 __func__, offset, obj->desc_size);
860 return -EINVAL;
861 }
862 size -= offset;
863
864 if (size < sizeof(struct ffa_comp_mrd)) {
865 WARN("%s: invalid object, offset %u, total size %zu, no header space.\n",
866 __func__, offset, obj->desc_size);
867 return -EINVAL;
868 }
869 size -= sizeof(struct ffa_comp_mrd);
870
871 count = size / sizeof(struct ffa_cons_mrd);
872
Marc Bonnicid1907f02022-04-19 17:42:53 +0100873 comp = spmc_shmem_obj_get_comp_mrd(obj, ffa_version);
874
875 if (comp == NULL) {
876 WARN("%s: invalid comp_mrd offset\n", __func__);
877 return -EINVAL;
878 }
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100879
880 if (comp->address_range_count != count) {
881 WARN("%s: invalid object, desc count %u != %zu\n",
882 __func__, comp->address_range_count, count);
883 return -EINVAL;
884 }
885
886 expected_size = offset + sizeof(*comp) +
Marc Bonnicid1907f02022-04-19 17:42:53 +0100887 spmc_shmem_obj_ffa_constituent_size(obj,
888 ffa_version);
889
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100890 if (expected_size != obj->desc_size) {
891 WARN("%s: invalid object, computed size %zu != size %zu\n",
892 __func__, expected_size, obj->desc_size);
893 return -EINVAL;
894 }
895
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100896 total_page_count = 0;
897
898 for (size_t i = 0; i < count; i++) {
899 total_page_count +=
900 comp->address_range_array[i].page_count;
901 }
902 if (comp->total_page_count != total_page_count) {
Demi Marie Obenour00d36b22023-01-12 13:24:50 -0500903 WARN("%s: invalid object, desc total_page_count %u != %" PRIu64 "\n",
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100904 __func__, comp->total_page_count,
905 total_page_count);
906 return -EINVAL;
907 }
908 }
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000909 return 0;
910}
911
912/**
913 * spmc_shmem_check_state_obj - Check if the descriptor describes memory
914 * regions that are currently involved with an
915 * existing memory transactions. This implies that
916 * the memory is not in a valid state for lending.
917 * @obj: Object containing ffa_memory_region_descriptor.
918 *
919 * Return: 0 if object is valid, -EINVAL if invalid memory state.
920 */
Marc Bonnicid1907f02022-04-19 17:42:53 +0100921static int spmc_shmem_check_state_obj(struct spmc_shmem_obj *obj,
922 uint32_t ffa_version)
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000923{
924 size_t obj_offset = 0;
925 struct spmc_shmem_obj *inflight_obj;
926
927 struct ffa_comp_mrd *other_mrd;
Marc Bonnicid1907f02022-04-19 17:42:53 +0100928 struct ffa_comp_mrd *requested_mrd = spmc_shmem_obj_get_comp_mrd(obj,
929 ffa_version);
930
931 if (requested_mrd == NULL) {
932 return -EINVAL;
933 }
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100934
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000935 inflight_obj = spmc_shmem_obj_get_next(&spmc_shmem_obj_state,
936 &obj_offset);
937
938 while (inflight_obj != NULL) {
939 /*
940 * Don't compare the transaction to itself or to partially
941 * transmitted descriptors.
942 */
943 if ((obj->desc.handle != inflight_obj->desc.handle) &&
944 (obj->desc_size == obj->desc_filled)) {
Marc Bonnicid1907f02022-04-19 17:42:53 +0100945 other_mrd = spmc_shmem_obj_get_comp_mrd(inflight_obj,
Marc Bonnici344ca9d2022-05-20 14:38:55 +0100946 FFA_VERSION_COMPILED);
Marc Bonnicid1907f02022-04-19 17:42:53 +0100947 if (other_mrd == NULL) {
948 return -EINVAL;
949 }
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000950 if (overlapping_memory_regions(requested_mrd,
951 other_mrd)) {
952 return -EINVAL;
953 }
954 }
955
956 inflight_obj = spmc_shmem_obj_get_next(&spmc_shmem_obj_state,
957 &obj_offset);
958 }
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100959 return 0;
960}
961
962static long spmc_ffa_fill_desc(struct mailbox *mbox,
963 struct spmc_shmem_obj *obj,
964 uint32_t fragment_length,
965 ffa_mtd_flag32_t mtd_flag,
Marc Bonnicid1907f02022-04-19 17:42:53 +0100966 uint32_t ffa_version,
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100967 void *smc_handle)
968{
969 int ret;
Marc Bonnicid1907f02022-04-19 17:42:53 +0100970 size_t emad_size;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100971 uint32_t handle_low;
972 uint32_t handle_high;
Marc Bonnicid1907f02022-04-19 17:42:53 +0100973 struct ffa_emad_v1_0 *emad;
974 struct ffa_emad_v1_0 *other_emad;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100975
976 if (mbox->rxtx_page_count == 0U) {
977 WARN("%s: buffer pair not registered.\n", __func__);
Marc Bonnicid1907f02022-04-19 17:42:53 +0100978 ret = FFA_ERROR_INVALID_PARAMETER;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100979 goto err_arg;
980 }
981
982 if (fragment_length > mbox->rxtx_page_count * PAGE_SIZE_4KB) {
983 WARN("%s: bad fragment size %u > %u buffer size\n", __func__,
984 fragment_length, mbox->rxtx_page_count * PAGE_SIZE_4KB);
Marc Bonnicid1907f02022-04-19 17:42:53 +0100985 ret = FFA_ERROR_INVALID_PARAMETER;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100986 goto err_arg;
987 }
988
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100989 if (fragment_length > obj->desc_size - obj->desc_filled) {
990 WARN("%s: bad fragment size %u > %zu remaining\n", __func__,
991 fragment_length, obj->desc_size - obj->desc_filled);
Marc Bonnicid1907f02022-04-19 17:42:53 +0100992 ret = FFA_ERROR_INVALID_PARAMETER;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100993 goto err_arg;
994 }
995
Marc Bonnicif0f45dc2022-10-18 13:57:16 +0100996 memcpy((uint8_t *)&obj->desc + obj->desc_filled,
997 (uint8_t *) mbox->tx_buffer, fragment_length);
998
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100999 /* Ensure that the sender ID resides in the normal world. */
1000 if (ffa_is_secure_world_id(obj->desc.sender_id)) {
1001 WARN("%s: Invalid sender ID 0x%x.\n",
1002 __func__, obj->desc.sender_id);
1003 ret = FFA_ERROR_DENIED;
1004 goto err_arg;
1005 }
1006
Marc Bonnici08f28ef2022-04-19 16:52:59 +01001007 /* Ensure the NS bit is set to 0. */
1008 if ((obj->desc.memory_region_attributes & FFA_MEM_ATTR_NS_BIT) != 0U) {
1009 WARN("%s: NS mem attributes flags MBZ.\n", __func__);
1010 ret = FFA_ERROR_INVALID_PARAMETER;
1011 goto err_arg;
1012 }
1013
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001014 /*
1015 * We don't currently support any optional flags so ensure none are
1016 * requested.
1017 */
1018 if (obj->desc.flags != 0U && mtd_flag != 0U &&
1019 (obj->desc.flags != mtd_flag)) {
1020 WARN("%s: invalid memory transaction flags %u != %u\n",
1021 __func__, obj->desc.flags, mtd_flag);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001022 ret = FFA_ERROR_INVALID_PARAMETER;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001023 goto err_arg;
1024 }
1025
1026 if (obj->desc_filled == 0U) {
1027 /* First fragment, descriptor header has been copied */
Demi Marie Obenour4ed9df42022-12-30 19:30:58 -05001028 ret = spmc_validate_mtd_start(&obj->desc, ffa_version,
1029 fragment_length, obj->desc_size);
1030 if (ret != 0) {
1031 goto err_bad_desc;
1032 }
1033
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001034 obj->desc.handle = spmc_shmem_obj_state.next_handle++;
1035 obj->desc.flags |= mtd_flag;
1036 }
1037
1038 obj->desc_filled += fragment_length;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001039
1040 handle_low = (uint32_t)obj->desc.handle;
1041 handle_high = obj->desc.handle >> 32;
1042
1043 if (obj->desc_filled != obj->desc_size) {
1044 SMC_RET8(smc_handle, FFA_MEM_FRAG_RX, handle_low,
1045 handle_high, obj->desc_filled,
1046 (uint32_t)obj->desc.sender_id << 16, 0, 0, 0);
1047 }
1048
Marc Bonnici336630f2022-01-13 11:39:10 +00001049 /* The full descriptor has been received, perform any final checks. */
1050
Demi Marie Obenourcdd3e722023-01-11 14:16:37 -05001051 ret = spmc_shmem_check_obj(obj, ffa_version);
1052 if (ret != 0) {
1053 ret = FFA_ERROR_INVALID_PARAMETER;
1054 goto err_bad_desc;
1055 }
1056
Marc Bonnici336630f2022-01-13 11:39:10 +00001057 /*
1058 * If a partition ID resides in the secure world validate that the
1059 * partition ID is for a known partition. Ignore any partition ID
1060 * belonging to the normal world as it is assumed the Hypervisor will
1061 * have validated these.
1062 */
1063 for (size_t i = 0; i < obj->desc.emad_count; i++) {
Marc Bonnicid1907f02022-04-19 17:42:53 +01001064 emad = spmc_shmem_obj_get_emad(&obj->desc, i, ffa_version,
1065 &emad_size);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001066
1067 ffa_endpoint_id16_t ep_id = emad->mapd.endpoint_id;
Marc Bonnici336630f2022-01-13 11:39:10 +00001068
1069 if (ffa_is_secure_world_id(ep_id)) {
1070 if (spmc_get_sp_ctx(ep_id) == NULL) {
1071 WARN("%s: Invalid receiver id 0x%x\n",
1072 __func__, ep_id);
1073 ret = FFA_ERROR_INVALID_PARAMETER;
1074 goto err_bad_desc;
1075 }
1076 }
1077 }
1078
1079 /* Ensure partition IDs are not duplicated. */
1080 for (size_t i = 0; i < obj->desc.emad_count; i++) {
Marc Bonnicid1907f02022-04-19 17:42:53 +01001081 emad = spmc_shmem_obj_get_emad(&obj->desc, i, ffa_version,
1082 &emad_size);
Demi Marie Obenour57bf10c2022-12-31 11:11:18 -05001083
Marc Bonnici336630f2022-01-13 11:39:10 +00001084 for (size_t j = i + 1; j < obj->desc.emad_count; j++) {
Marc Bonnicid1907f02022-04-19 17:42:53 +01001085 other_emad = spmc_shmem_obj_get_emad(&obj->desc, j,
1086 ffa_version,
1087 &emad_size);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001088
1089 if (emad->mapd.endpoint_id ==
1090 other_emad->mapd.endpoint_id) {
1091 WARN("%s: Duplicated endpoint id 0x%x\n",
1092 __func__, emad->mapd.endpoint_id);
1093 ret = FFA_ERROR_INVALID_PARAMETER;
1094 goto err_bad_desc;
1095 }
Marc Bonnici336630f2022-01-13 11:39:10 +00001096 }
1097 }
1098
Marc Bonnicid1907f02022-04-19 17:42:53 +01001099 ret = spmc_shmem_check_state_obj(obj, ffa_version);
Marc Bonnicic31ec9e2022-01-21 10:34:55 +00001100 if (ret) {
1101 ERROR("%s: invalid memory region descriptor.\n", __func__);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001102 ret = FFA_ERROR_INVALID_PARAMETER;
Marc Bonnicic31ec9e2022-01-21 10:34:55 +00001103 goto err_bad_desc;
1104 }
1105
Marc Bonnicid1907f02022-04-19 17:42:53 +01001106 /*
1107 * Everything checks out, if the sender was using FF-A v1.0, convert
1108 * the descriptor format to use the v1.1 structures.
1109 */
1110 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1111 struct spmc_shmem_obj *v1_1_obj;
1112 uint64_t mem_handle;
1113
1114 /* Calculate the size that the v1.1 descriptor will required. */
1115 size_t v1_1_desc_size =
1116 spmc_shm_get_v1_1_descriptor_size((void *) &obj->desc,
vallau0146dbac22022-08-08 14:10:14 +02001117 obj->desc_size);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001118
1119 if (v1_1_desc_size == 0U) {
1120 ERROR("%s: cannot determine size of descriptor.\n",
1121 __func__);
1122 goto err_arg;
1123 }
1124
1125 /* Get a new obj to store the v1.1 descriptor. */
1126 v1_1_obj =
1127 spmc_shmem_obj_alloc(&spmc_shmem_obj_state, v1_1_desc_size);
1128
vallau018f830992022-08-09 18:03:28 +02001129 if (!v1_1_obj) {
Marc Bonnicid1907f02022-04-19 17:42:53 +01001130 ret = FFA_ERROR_NO_MEMORY;
1131 goto err_arg;
1132 }
1133
1134 /* Perform the conversion from v1.0 to v1.1. */
1135 v1_1_obj->desc_size = v1_1_desc_size;
1136 v1_1_obj->desc_filled = v1_1_desc_size;
1137 if (!spmc_shm_convert_shmem_obj_from_v1_0(v1_1_obj, obj)) {
1138 ERROR("%s: Could not convert mtd!\n", __func__);
1139 spmc_shmem_obj_free(&spmc_shmem_obj_state, v1_1_obj);
1140 goto err_arg;
1141 }
1142
1143 /*
1144 * We're finished with the v1.0 descriptor so free it
1145 * and continue our checks with the new v1.1 descriptor.
1146 */
1147 mem_handle = obj->desc.handle;
1148 spmc_shmem_obj_free(&spmc_shmem_obj_state, obj);
1149 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, mem_handle);
1150 if (obj == NULL) {
1151 ERROR("%s: Failed to find converted descriptor.\n",
1152 __func__);
1153 ret = FFA_ERROR_INVALID_PARAMETER;
1154 return spmc_ffa_error_return(smc_handle, ret);
1155 }
1156 }
1157
Marc Bonnici503320e2022-02-21 15:02:36 +00001158 /* Allow for platform specific operations to be performed. */
1159 ret = plat_spmc_shmem_begin(&obj->desc);
1160 if (ret != 0) {
1161 goto err_arg;
1162 }
1163
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001164 SMC_RET8(smc_handle, FFA_SUCCESS_SMC32, 0, handle_low, handle_high, 0,
1165 0, 0, 0);
1166
1167err_bad_desc:
1168err_arg:
1169 spmc_shmem_obj_free(&spmc_shmem_obj_state, obj);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001170 return spmc_ffa_error_return(smc_handle, ret);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001171}
1172
1173/**
1174 * spmc_ffa_mem_send - FFA_MEM_SHARE/LEND implementation.
1175 * @client: Client state.
1176 * @total_length: Total length of shared memory descriptor.
1177 * @fragment_length: Length of fragment of shared memory descriptor passed in
1178 * this call.
1179 * @address: Not supported, must be 0.
1180 * @page_count: Not supported, must be 0.
1181 * @smc_handle: Handle passed to smc call. Used to return
1182 * FFA_MEM_FRAG_RX or SMC_FC_FFA_SUCCESS.
1183 *
1184 * Implements a subset of the FF-A FFA_MEM_SHARE and FFA_MEM_LEND calls needed
1185 * to share or lend memory from non-secure os to secure os (with no stream
1186 * endpoints).
1187 *
1188 * Return: 0 on success, error code on failure.
1189 */
1190long spmc_ffa_mem_send(uint32_t smc_fid,
1191 bool secure_origin,
1192 uint64_t total_length,
1193 uint32_t fragment_length,
1194 uint64_t address,
1195 uint32_t page_count,
1196 void *cookie,
1197 void *handle,
1198 uint64_t flags)
1199
1200{
1201 long ret;
1202 struct spmc_shmem_obj *obj;
1203 struct mailbox *mbox = spmc_get_mbox_desc(secure_origin);
1204 ffa_mtd_flag32_t mtd_flag;
Marc Bonnicid1907f02022-04-19 17:42:53 +01001205 uint32_t ffa_version = get_partition_ffa_version(secure_origin);
Demi Marie Obenour1f9f8302022-12-30 19:14:18 -05001206 size_t min_desc_size;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001207
1208 if (address != 0U || page_count != 0U) {
1209 WARN("%s: custom memory region for message not supported.\n",
1210 __func__);
1211 return spmc_ffa_error_return(handle,
1212 FFA_ERROR_INVALID_PARAMETER);
1213 }
1214
1215 if (secure_origin) {
1216 WARN("%s: unsupported share direction.\n", __func__);
1217 return spmc_ffa_error_return(handle,
1218 FFA_ERROR_INVALID_PARAMETER);
1219 }
1220
Demi Marie Obenour1f9f8302022-12-30 19:14:18 -05001221 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1222 min_desc_size = sizeof(struct ffa_mtd_v1_0);
1223 } else if (ffa_version == MAKE_FFA_VERSION(1, 1)) {
1224 min_desc_size = sizeof(struct ffa_mtd);
1225 } else {
1226 WARN("%s: bad FF-A version.\n", __func__);
1227 return spmc_ffa_error_return(handle,
1228 FFA_ERROR_INVALID_PARAMETER);
1229 }
1230
1231 /* Check if the descriptor is too small for the FF-A version. */
1232 if (fragment_length < min_desc_size) {
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001233 WARN("%s: bad first fragment size %u < %zu\n",
Marc Bonnicid1907f02022-04-19 17:42:53 +01001234 __func__, fragment_length, sizeof(struct ffa_mtd_v1_0));
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001235 return spmc_ffa_error_return(handle,
1236 FFA_ERROR_INVALID_PARAMETER);
1237 }
1238
1239 if ((smc_fid & FUNCID_NUM_MASK) == FFA_FNUM_MEM_SHARE) {
1240 mtd_flag = FFA_MTD_FLAG_TYPE_SHARE_MEMORY;
1241 } else if ((smc_fid & FUNCID_NUM_MASK) == FFA_FNUM_MEM_LEND) {
1242 mtd_flag = FFA_MTD_FLAG_TYPE_LEND_MEMORY;
1243 } else {
1244 WARN("%s: invalid memory management operation.\n", __func__);
1245 return spmc_ffa_error_return(handle,
1246 FFA_ERROR_INVALID_PARAMETER);
1247 }
1248
1249 spin_lock(&spmc_shmem_obj_state.lock);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001250 obj = spmc_shmem_obj_alloc(&spmc_shmem_obj_state, total_length);
1251 if (obj == NULL) {
1252 ret = FFA_ERROR_NO_MEMORY;
1253 goto err_unlock;
1254 }
1255
1256 spin_lock(&mbox->lock);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001257 ret = spmc_ffa_fill_desc(mbox, obj, fragment_length, mtd_flag,
1258 ffa_version, handle);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001259 spin_unlock(&mbox->lock);
1260
1261 spin_unlock(&spmc_shmem_obj_state.lock);
1262 return ret;
1263
1264err_unlock:
1265 spin_unlock(&spmc_shmem_obj_state.lock);
1266 return spmc_ffa_error_return(handle, ret);
1267}
1268
1269/**
1270 * spmc_ffa_mem_frag_tx - FFA_MEM_FRAG_TX implementation.
1271 * @client: Client state.
1272 * @handle_low: Handle_low value returned from FFA_MEM_FRAG_RX.
1273 * @handle_high: Handle_high value returned from FFA_MEM_FRAG_RX.
1274 * @fragment_length: Length of fragments transmitted.
1275 * @sender_id: Vmid of sender in bits [31:16]
1276 * @smc_handle: Handle passed to smc call. Used to return
1277 * FFA_MEM_FRAG_RX or SMC_FC_FFA_SUCCESS.
1278 *
1279 * Return: @smc_handle on success, error code on failure.
1280 */
1281long spmc_ffa_mem_frag_tx(uint32_t smc_fid,
1282 bool secure_origin,
1283 uint64_t handle_low,
1284 uint64_t handle_high,
1285 uint32_t fragment_length,
1286 uint32_t sender_id,
1287 void *cookie,
1288 void *handle,
1289 uint64_t flags)
1290{
1291 long ret;
1292 uint32_t desc_sender_id;
Marc Bonnicid1907f02022-04-19 17:42:53 +01001293 uint32_t ffa_version = get_partition_ffa_version(secure_origin);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001294 struct mailbox *mbox = spmc_get_mbox_desc(secure_origin);
1295
1296 struct spmc_shmem_obj *obj;
1297 uint64_t mem_handle = handle_low | (((uint64_t)handle_high) << 32);
1298
1299 spin_lock(&spmc_shmem_obj_state.lock);
1300
1301 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, mem_handle);
1302 if (obj == NULL) {
1303 WARN("%s: invalid handle, 0x%lx, not a valid handle.\n",
1304 __func__, mem_handle);
1305 ret = FFA_ERROR_INVALID_PARAMETER;
1306 goto err_unlock;
1307 }
1308
1309 desc_sender_id = (uint32_t)obj->desc.sender_id << 16;
1310 if (sender_id != desc_sender_id) {
1311 WARN("%s: invalid sender_id 0x%x != 0x%x\n", __func__,
1312 sender_id, desc_sender_id);
1313 ret = FFA_ERROR_INVALID_PARAMETER;
1314 goto err_unlock;
1315 }
1316
1317 if (obj->desc_filled == obj->desc_size) {
1318 WARN("%s: object desc already filled, %zu\n", __func__,
1319 obj->desc_filled);
1320 ret = FFA_ERROR_INVALID_PARAMETER;
1321 goto err_unlock;
1322 }
1323
1324 spin_lock(&mbox->lock);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001325 ret = spmc_ffa_fill_desc(mbox, obj, fragment_length, 0, ffa_version,
1326 handle);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001327 spin_unlock(&mbox->lock);
1328
1329 spin_unlock(&spmc_shmem_obj_state.lock);
1330 return ret;
1331
1332err_unlock:
1333 spin_unlock(&spmc_shmem_obj_state.lock);
1334 return spmc_ffa_error_return(handle, ret);
1335}
1336
1337/**
Marc Bonnici08f28ef2022-04-19 16:52:59 +01001338 * spmc_ffa_mem_retrieve_set_ns_bit - Set the NS bit in the response descriptor
1339 * if the caller implements a version greater
1340 * than FF-A 1.0 or if they have requested
1341 * the functionality.
1342 * TODO: We are assuming that the caller is
1343 * an SP. To support retrieval from the
1344 * normal world this function will need to be
1345 * expanded accordingly.
1346 * @resp: Descriptor populated in callers RX buffer.
1347 * @sp_ctx: Context of the calling SP.
1348 */
1349void spmc_ffa_mem_retrieve_set_ns_bit(struct ffa_mtd *resp,
1350 struct secure_partition_desc *sp_ctx)
1351{
1352 if (sp_ctx->ffa_version > MAKE_FFA_VERSION(1, 0) ||
1353 sp_ctx->ns_bit_requested) {
1354 /*
1355 * Currently memory senders must reside in the normal
1356 * world, and we do not have the functionlaity to change
1357 * the state of memory dynamically. Therefore we can always set
1358 * the NS bit to 1.
1359 */
1360 resp->memory_region_attributes |= FFA_MEM_ATTR_NS_BIT;
1361 }
1362}
1363
1364/**
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001365 * spmc_ffa_mem_retrieve_req - FFA_MEM_RETRIEVE_REQ implementation.
1366 * @smc_fid: FID of SMC
1367 * @total_length: Total length of retrieve request descriptor if this is
1368 * the first call. Otherwise (unsupported) must be 0.
1369 * @fragment_length: Length of fragment of retrieve request descriptor passed
1370 * in this call. Only @fragment_length == @length is
1371 * supported by this implementation.
1372 * @address: Not supported, must be 0.
1373 * @page_count: Not supported, must be 0.
1374 * @smc_handle: Handle passed to smc call. Used to return
1375 * FFA_MEM_RETRIEVE_RESP.
1376 *
1377 * Implements a subset of the FF-A FFA_MEM_RETRIEVE_REQ call.
1378 * Used by secure os to retrieve memory already shared by non-secure os.
1379 * If the data does not fit in a single FFA_MEM_RETRIEVE_RESP message,
1380 * the client must call FFA_MEM_FRAG_RX until the full response has been
1381 * received.
1382 *
1383 * Return: @handle on success, error code on failure.
1384 */
1385long
1386spmc_ffa_mem_retrieve_req(uint32_t smc_fid,
1387 bool secure_origin,
1388 uint32_t total_length,
1389 uint32_t fragment_length,
1390 uint64_t address,
1391 uint32_t page_count,
1392 void *cookie,
1393 void *handle,
1394 uint64_t flags)
1395{
1396 int ret;
1397 size_t buf_size;
Marc Bonnicid1907f02022-04-19 17:42:53 +01001398 size_t copy_size = 0;
1399 size_t min_desc_size;
1400 size_t out_desc_size = 0;
1401
1402 /*
1403 * Currently we are only accessing fields that are the same in both the
1404 * v1.0 and v1.1 mtd struct therefore we can use a v1.1 struct directly
1405 * here. We only need validate against the appropriate struct size.
1406 */
1407 struct ffa_mtd *resp;
1408 const struct ffa_mtd *req;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001409 struct spmc_shmem_obj *obj = NULL;
1410 struct mailbox *mbox = spmc_get_mbox_desc(secure_origin);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001411 uint32_t ffa_version = get_partition_ffa_version(secure_origin);
Marc Bonnici08f28ef2022-04-19 16:52:59 +01001412 struct secure_partition_desc *sp_ctx = spmc_get_current_sp_ctx();
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001413
1414 if (!secure_origin) {
1415 WARN("%s: unsupported retrieve req direction.\n", __func__);
1416 return spmc_ffa_error_return(handle,
1417 FFA_ERROR_INVALID_PARAMETER);
1418 }
1419
1420 if (address != 0U || page_count != 0U) {
1421 WARN("%s: custom memory region not supported.\n", __func__);
1422 return spmc_ffa_error_return(handle,
1423 FFA_ERROR_INVALID_PARAMETER);
1424 }
1425
1426 spin_lock(&mbox->lock);
1427
1428 req = mbox->tx_buffer;
1429 resp = mbox->rx_buffer;
1430 buf_size = mbox->rxtx_page_count * FFA_PAGE_SIZE;
1431
1432 if (mbox->rxtx_page_count == 0U) {
1433 WARN("%s: buffer pair not registered.\n", __func__);
1434 ret = FFA_ERROR_INVALID_PARAMETER;
1435 goto err_unlock_mailbox;
1436 }
1437
1438 if (mbox->state != MAILBOX_STATE_EMPTY) {
1439 WARN("%s: RX Buffer is full! %d\n", __func__, mbox->state);
1440 ret = FFA_ERROR_DENIED;
1441 goto err_unlock_mailbox;
1442 }
1443
1444 if (fragment_length != total_length) {
1445 WARN("%s: fragmented retrieve request not supported.\n",
1446 __func__);
1447 ret = FFA_ERROR_INVALID_PARAMETER;
1448 goto err_unlock_mailbox;
1449 }
1450
Marc Bonnici336630f2022-01-13 11:39:10 +00001451 if (req->emad_count == 0U) {
1452 WARN("%s: unsupported attribute desc count %u.\n",
1453 __func__, obj->desc.emad_count);
vallau01460d3962022-08-09 17:06:53 +02001454 ret = FFA_ERROR_INVALID_PARAMETER;
1455 goto err_unlock_mailbox;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001456 }
1457
Marc Bonnicid1907f02022-04-19 17:42:53 +01001458 /* Determine the appropriate minimum descriptor size. */
1459 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1460 min_desc_size = sizeof(struct ffa_mtd_v1_0);
1461 } else {
1462 min_desc_size = sizeof(struct ffa_mtd);
1463 }
1464 if (total_length < min_desc_size) {
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001465 WARN("%s: invalid length %u < %zu\n", __func__, total_length,
Marc Bonnicid1907f02022-04-19 17:42:53 +01001466 min_desc_size);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001467 ret = FFA_ERROR_INVALID_PARAMETER;
1468 goto err_unlock_mailbox;
1469 }
1470
1471 spin_lock(&spmc_shmem_obj_state.lock);
1472
1473 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, req->handle);
1474 if (obj == NULL) {
1475 ret = FFA_ERROR_INVALID_PARAMETER;
1476 goto err_unlock_all;
1477 }
1478
1479 if (obj->desc_filled != obj->desc_size) {
1480 WARN("%s: incomplete object desc filled %zu < size %zu\n",
1481 __func__, obj->desc_filled, obj->desc_size);
1482 ret = FFA_ERROR_INVALID_PARAMETER;
1483 goto err_unlock_all;
1484 }
1485
1486 if (req->emad_count != 0U && req->sender_id != obj->desc.sender_id) {
1487 WARN("%s: wrong sender id 0x%x != 0x%x\n",
1488 __func__, req->sender_id, obj->desc.sender_id);
1489 ret = FFA_ERROR_INVALID_PARAMETER;
1490 goto err_unlock_all;
1491 }
1492
1493 if (req->emad_count != 0U && req->tag != obj->desc.tag) {
1494 WARN("%s: wrong tag 0x%lx != 0x%lx\n",
1495 __func__, req->tag, obj->desc.tag);
1496 ret = FFA_ERROR_INVALID_PARAMETER;
1497 goto err_unlock_all;
1498 }
1499
Marc Bonnici336630f2022-01-13 11:39:10 +00001500 if (req->emad_count != 0U && req->emad_count != obj->desc.emad_count) {
1501 WARN("%s: mistmatch of endpoint counts %u != %u\n",
1502 __func__, req->emad_count, obj->desc.emad_count);
1503 ret = FFA_ERROR_INVALID_PARAMETER;
1504 goto err_unlock_all;
1505 }
1506
Marc Bonnici08f28ef2022-04-19 16:52:59 +01001507 /* Ensure the NS bit is set to 0 in the request. */
1508 if ((req->memory_region_attributes & FFA_MEM_ATTR_NS_BIT) != 0U) {
1509 WARN("%s: NS mem attributes flags MBZ.\n", __func__);
1510 ret = FFA_ERROR_INVALID_PARAMETER;
1511 goto err_unlock_all;
1512 }
1513
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001514 if (req->flags != 0U) {
1515 if ((req->flags & FFA_MTD_FLAG_TYPE_MASK) !=
1516 (obj->desc.flags & FFA_MTD_FLAG_TYPE_MASK)) {
1517 /*
1518 * If the retrieve request specifies the memory
1519 * transaction ensure it matches what we expect.
1520 */
1521 WARN("%s: wrong mem transaction flags %x != %x\n",
1522 __func__, req->flags, obj->desc.flags);
1523 ret = FFA_ERROR_INVALID_PARAMETER;
1524 goto err_unlock_all;
1525 }
1526
1527 if (req->flags != FFA_MTD_FLAG_TYPE_SHARE_MEMORY &&
1528 req->flags != FFA_MTD_FLAG_TYPE_LEND_MEMORY) {
1529 /*
1530 * Current implementation does not support donate and
1531 * it supports no other flags.
1532 */
1533 WARN("%s: invalid flags 0x%x\n", __func__, req->flags);
1534 ret = FFA_ERROR_INVALID_PARAMETER;
1535 goto err_unlock_all;
1536 }
1537 }
1538
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001539 /* Validate the caller is a valid participant. */
Shruti Gupta20ce06c2022-08-25 14:22:53 +01001540 if (!spmc_shmem_obj_validate_id(obj, sp_ctx->sp_id)) {
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001541 WARN("%s: Invalid endpoint ID (0x%x).\n",
1542 __func__, sp_ctx->sp_id);
1543 ret = FFA_ERROR_INVALID_PARAMETER;
1544 goto err_unlock_all;
1545 }
1546
Marc Bonnicid1907f02022-04-19 17:42:53 +01001547 /* Validate that the provided emad offset and structure is valid.*/
1548 for (size_t i = 0; i < req->emad_count; i++) {
1549 size_t emad_size;
1550 struct ffa_emad_v1_0 *emad;
1551
1552 emad = spmc_shmem_obj_get_emad(req, i, ffa_version,
1553 &emad_size);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001554
1555 if ((uintptr_t) emad >= (uintptr_t)
1556 ((uint8_t *) req + total_length)) {
1557 WARN("Invalid emad access.\n");
1558 ret = FFA_ERROR_INVALID_PARAMETER;
1559 goto err_unlock_all;
1560 }
Marc Bonnici336630f2022-01-13 11:39:10 +00001561 }
1562
1563 /*
1564 * Validate all the endpoints match in the case of multiple
1565 * borrowers. We don't mandate that the order of the borrowers
1566 * must match in the descriptors therefore check to see if the
1567 * endpoints match in any order.
1568 */
1569 for (size_t i = 0; i < req->emad_count; i++) {
1570 bool found = false;
Marc Bonnicid1907f02022-04-19 17:42:53 +01001571 size_t emad_size;
1572 struct ffa_emad_v1_0 *emad;
1573 struct ffa_emad_v1_0 *other_emad;
1574
1575 emad = spmc_shmem_obj_get_emad(req, i, ffa_version,
1576 &emad_size);
Marc Bonnici336630f2022-01-13 11:39:10 +00001577
1578 for (size_t j = 0; j < obj->desc.emad_count; j++) {
Marc Bonnicid1907f02022-04-19 17:42:53 +01001579 other_emad = spmc_shmem_obj_get_emad(
1580 &obj->desc, j, MAKE_FFA_VERSION(1, 1),
1581 &emad_size);
1582
Marc Bonnicid1907f02022-04-19 17:42:53 +01001583 if (req->emad_count &&
1584 emad->mapd.endpoint_id ==
1585 other_emad->mapd.endpoint_id) {
Marc Bonnici336630f2022-01-13 11:39:10 +00001586 found = true;
1587 break;
1588 }
1589 }
1590
1591 if (!found) {
1592 WARN("%s: invalid receiver id (0x%x).\n",
Marc Bonnicid1907f02022-04-19 17:42:53 +01001593 __func__, emad->mapd.endpoint_id);
Marc Bonnici336630f2022-01-13 11:39:10 +00001594 ret = FFA_ERROR_INVALID_PARAMETER;
1595 goto err_unlock_all;
1596 }
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001597 }
1598
1599 mbox->state = MAILBOX_STATE_FULL;
1600
1601 if (req->emad_count != 0U) {
1602 obj->in_use++;
1603 }
1604
Marc Bonnicid1907f02022-04-19 17:42:53 +01001605 /*
1606 * If the caller is v1.0 convert the descriptor, otherwise copy
1607 * directly.
1608 */
1609 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1610 ret = spmc_populate_ffa_v1_0_descriptor(resp, obj, buf_size, 0,
1611 &copy_size,
1612 &out_desc_size);
1613 if (ret != 0U) {
1614 ERROR("%s: Failed to process descriptor.\n", __func__);
1615 goto err_unlock_all;
1616 }
1617 } else {
1618 copy_size = MIN(obj->desc_size, buf_size);
1619 out_desc_size = obj->desc_size;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001620
Marc Bonnicid1907f02022-04-19 17:42:53 +01001621 memcpy(resp, &obj->desc, copy_size);
1622 }
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001623
Marc Bonnici08f28ef2022-04-19 16:52:59 +01001624 /* Set the NS bit in the response if applicable. */
1625 spmc_ffa_mem_retrieve_set_ns_bit(resp, sp_ctx);
1626
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001627 spin_unlock(&spmc_shmem_obj_state.lock);
1628 spin_unlock(&mbox->lock);
1629
Marc Bonnicid1907f02022-04-19 17:42:53 +01001630 SMC_RET8(handle, FFA_MEM_RETRIEVE_RESP, out_desc_size,
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001631 copy_size, 0, 0, 0, 0, 0);
1632
1633err_unlock_all:
1634 spin_unlock(&spmc_shmem_obj_state.lock);
1635err_unlock_mailbox:
1636 spin_unlock(&mbox->lock);
1637 return spmc_ffa_error_return(handle, ret);
1638}
1639
1640/**
1641 * spmc_ffa_mem_frag_rx - FFA_MEM_FRAG_RX implementation.
1642 * @client: Client state.
1643 * @handle_low: Handle passed to &FFA_MEM_RETRIEVE_REQ. Bit[31:0].
1644 * @handle_high: Handle passed to &FFA_MEM_RETRIEVE_REQ. Bit[63:32].
1645 * @fragment_offset: Byte offset in descriptor to resume at.
1646 * @sender_id: Bit[31:16]: Endpoint id of sender if client is a
1647 * hypervisor. 0 otherwise.
1648 * @smc_handle: Handle passed to smc call. Used to return
1649 * FFA_MEM_FRAG_TX.
1650 *
1651 * Return: @smc_handle on success, error code on failure.
1652 */
1653long spmc_ffa_mem_frag_rx(uint32_t smc_fid,
1654 bool secure_origin,
1655 uint32_t handle_low,
1656 uint32_t handle_high,
1657 uint32_t fragment_offset,
1658 uint32_t sender_id,
1659 void *cookie,
1660 void *handle,
1661 uint64_t flags)
1662{
1663 int ret;
1664 void *src;
1665 size_t buf_size;
1666 size_t copy_size;
1667 size_t full_copy_size;
1668 uint32_t desc_sender_id;
1669 struct mailbox *mbox = spmc_get_mbox_desc(secure_origin);
1670 uint64_t mem_handle = handle_low | (((uint64_t)handle_high) << 32);
1671 struct spmc_shmem_obj *obj;
Marc Bonnicid1907f02022-04-19 17:42:53 +01001672 uint32_t ffa_version = get_partition_ffa_version(secure_origin);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001673
1674 if (!secure_origin) {
1675 WARN("%s: can only be called from swld.\n",
1676 __func__);
1677 return spmc_ffa_error_return(handle,
1678 FFA_ERROR_INVALID_PARAMETER);
1679 }
1680
1681 spin_lock(&spmc_shmem_obj_state.lock);
1682
1683 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, mem_handle);
1684 if (obj == NULL) {
1685 WARN("%s: invalid handle, 0x%lx, not a valid handle.\n",
1686 __func__, mem_handle);
1687 ret = FFA_ERROR_INVALID_PARAMETER;
1688 goto err_unlock_shmem;
1689 }
1690
1691 desc_sender_id = (uint32_t)obj->desc.sender_id << 16;
1692 if (sender_id != 0U && sender_id != desc_sender_id) {
1693 WARN("%s: invalid sender_id 0x%x != 0x%x\n", __func__,
1694 sender_id, desc_sender_id);
1695 ret = FFA_ERROR_INVALID_PARAMETER;
1696 goto err_unlock_shmem;
1697 }
1698
1699 if (fragment_offset >= obj->desc_size) {
1700 WARN("%s: invalid fragment_offset 0x%x >= 0x%zx\n",
1701 __func__, fragment_offset, obj->desc_size);
1702 ret = FFA_ERROR_INVALID_PARAMETER;
1703 goto err_unlock_shmem;
1704 }
1705
1706 spin_lock(&mbox->lock);
1707
1708 if (mbox->rxtx_page_count == 0U) {
1709 WARN("%s: buffer pair not registered.\n", __func__);
1710 ret = FFA_ERROR_INVALID_PARAMETER;
1711 goto err_unlock_all;
1712 }
1713
1714 if (mbox->state != MAILBOX_STATE_EMPTY) {
1715 WARN("%s: RX Buffer is full!\n", __func__);
1716 ret = FFA_ERROR_DENIED;
1717 goto err_unlock_all;
1718 }
1719
1720 buf_size = mbox->rxtx_page_count * FFA_PAGE_SIZE;
1721
1722 mbox->state = MAILBOX_STATE_FULL;
1723
Marc Bonnicid1907f02022-04-19 17:42:53 +01001724 /*
1725 * If the caller is v1.0 convert the descriptor, otherwise copy
1726 * directly.
1727 */
1728 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1729 size_t out_desc_size;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001730
Marc Bonnicid1907f02022-04-19 17:42:53 +01001731 ret = spmc_populate_ffa_v1_0_descriptor(mbox->rx_buffer, obj,
1732 buf_size,
1733 fragment_offset,
1734 &copy_size,
1735 &out_desc_size);
1736 if (ret != 0U) {
1737 ERROR("%s: Failed to process descriptor.\n", __func__);
1738 goto err_unlock_all;
1739 }
1740 } else {
1741 full_copy_size = obj->desc_size - fragment_offset;
1742 copy_size = MIN(full_copy_size, buf_size);
1743
1744 src = &obj->desc;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001745
Marc Bonnicid1907f02022-04-19 17:42:53 +01001746 memcpy(mbox->rx_buffer, src + fragment_offset, copy_size);
1747 }
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001748
1749 spin_unlock(&mbox->lock);
1750 spin_unlock(&spmc_shmem_obj_state.lock);
1751
1752 SMC_RET8(handle, FFA_MEM_FRAG_TX, handle_low, handle_high,
1753 copy_size, sender_id, 0, 0, 0);
1754
1755err_unlock_all:
1756 spin_unlock(&mbox->lock);
1757err_unlock_shmem:
1758 spin_unlock(&spmc_shmem_obj_state.lock);
1759 return spmc_ffa_error_return(handle, ret);
1760}
1761
1762/**
1763 * spmc_ffa_mem_relinquish - FFA_MEM_RELINQUISH implementation.
1764 * @client: Client state.
1765 *
1766 * Implements a subset of the FF-A FFA_MEM_RELINQUISH call.
1767 * Used by secure os release previously shared memory to non-secure os.
1768 *
1769 * The handle to release must be in the client's (secure os's) transmit buffer.
1770 *
1771 * Return: 0 on success, error code on failure.
1772 */
1773int spmc_ffa_mem_relinquish(uint32_t smc_fid,
1774 bool secure_origin,
1775 uint32_t handle_low,
1776 uint32_t handle_high,
1777 uint32_t fragment_offset,
1778 uint32_t sender_id,
1779 void *cookie,
1780 void *handle,
1781 uint64_t flags)
1782{
1783 int ret;
1784 struct mailbox *mbox = spmc_get_mbox_desc(secure_origin);
1785 struct spmc_shmem_obj *obj;
1786 const struct ffa_mem_relinquish_descriptor *req;
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001787 struct secure_partition_desc *sp_ctx = spmc_get_current_sp_ctx();
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001788
1789 if (!secure_origin) {
1790 WARN("%s: unsupported relinquish direction.\n", __func__);
1791 return spmc_ffa_error_return(handle,
1792 FFA_ERROR_INVALID_PARAMETER);
1793 }
1794
1795 spin_lock(&mbox->lock);
1796
1797 if (mbox->rxtx_page_count == 0U) {
1798 WARN("%s: buffer pair not registered.\n", __func__);
1799 ret = FFA_ERROR_INVALID_PARAMETER;
1800 goto err_unlock_mailbox;
1801 }
1802
1803 req = mbox->tx_buffer;
1804
1805 if (req->flags != 0U) {
1806 WARN("%s: unsupported flags 0x%x\n", __func__, req->flags);
1807 ret = FFA_ERROR_INVALID_PARAMETER;
1808 goto err_unlock_mailbox;
1809 }
1810
Marc Bonnici336630f2022-01-13 11:39:10 +00001811 if (req->endpoint_count == 0) {
1812 WARN("%s: endpoint count cannot be 0.\n", __func__);
1813 ret = FFA_ERROR_INVALID_PARAMETER;
1814 goto err_unlock_mailbox;
1815 }
1816
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001817 spin_lock(&spmc_shmem_obj_state.lock);
1818
1819 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, req->handle);
1820 if (obj == NULL) {
1821 ret = FFA_ERROR_INVALID_PARAMETER;
1822 goto err_unlock_all;
1823 }
1824
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001825 /*
1826 * Validate the endpoint ID was populated correctly. We don't currently
1827 * support proxy endpoints so the endpoint count should always be 1.
1828 */
1829 if (req->endpoint_count != 1U) {
1830 WARN("%s: unsupported endpoint count %u != 1\n", __func__,
1831 req->endpoint_count);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001832 ret = FFA_ERROR_INVALID_PARAMETER;
1833 goto err_unlock_all;
1834 }
Marc Bonnici336630f2022-01-13 11:39:10 +00001835
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001836 /* Validate provided endpoint ID matches the partition ID. */
1837 if (req->endpoint_array[0] != sp_ctx->sp_id) {
1838 WARN("%s: invalid endpoint ID %u != %u\n", __func__,
1839 req->endpoint_array[0], sp_ctx->sp_id);
1840 ret = FFA_ERROR_INVALID_PARAMETER;
1841 goto err_unlock_all;
1842 }
Marc Bonnici336630f2022-01-13 11:39:10 +00001843
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001844 /* Validate the caller is a valid participant. */
Shruti Gupta20ce06c2022-08-25 14:22:53 +01001845 if (!spmc_shmem_obj_validate_id(obj, sp_ctx->sp_id)) {
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001846 WARN("%s: Invalid endpoint ID (0x%x).\n",
1847 __func__, req->endpoint_array[0]);
1848 ret = FFA_ERROR_INVALID_PARAMETER;
1849 goto err_unlock_all;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001850 }
Marc Bonnici336630f2022-01-13 11:39:10 +00001851
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001852 if (obj->in_use == 0U) {
1853 ret = FFA_ERROR_INVALID_PARAMETER;
1854 goto err_unlock_all;
1855 }
1856 obj->in_use--;
1857
1858 spin_unlock(&spmc_shmem_obj_state.lock);
1859 spin_unlock(&mbox->lock);
1860
1861 SMC_RET1(handle, FFA_SUCCESS_SMC32);
1862
1863err_unlock_all:
1864 spin_unlock(&spmc_shmem_obj_state.lock);
1865err_unlock_mailbox:
1866 spin_unlock(&mbox->lock);
1867 return spmc_ffa_error_return(handle, ret);
1868}
1869
1870/**
1871 * spmc_ffa_mem_reclaim - FFA_MEM_RECLAIM implementation.
1872 * @client: Client state.
1873 * @handle_low: Unique handle of shared memory object to reclaim. Bit[31:0].
1874 * @handle_high: Unique handle of shared memory object to reclaim.
1875 * Bit[63:32].
1876 * @flags: Unsupported, ignored.
1877 *
1878 * Implements a subset of the FF-A FFA_MEM_RECLAIM call.
1879 * Used by non-secure os reclaim memory previously shared with secure os.
1880 *
1881 * Return: 0 on success, error code on failure.
1882 */
1883int spmc_ffa_mem_reclaim(uint32_t smc_fid,
1884 bool secure_origin,
1885 uint32_t handle_low,
1886 uint32_t handle_high,
1887 uint32_t mem_flags,
1888 uint64_t x4,
1889 void *cookie,
1890 void *handle,
1891 uint64_t flags)
1892{
1893 int ret;
1894 struct spmc_shmem_obj *obj;
1895 uint64_t mem_handle = handle_low | (((uint64_t)handle_high) << 32);
1896
1897 if (secure_origin) {
1898 WARN("%s: unsupported reclaim direction.\n", __func__);
1899 return spmc_ffa_error_return(handle,
1900 FFA_ERROR_INVALID_PARAMETER);
1901 }
1902
1903 if (mem_flags != 0U) {
1904 WARN("%s: unsupported flags 0x%x\n", __func__, mem_flags);
1905 return spmc_ffa_error_return(handle,
1906 FFA_ERROR_INVALID_PARAMETER);
1907 }
1908
1909 spin_lock(&spmc_shmem_obj_state.lock);
1910
1911 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, mem_handle);
1912 if (obj == NULL) {
1913 ret = FFA_ERROR_INVALID_PARAMETER;
1914 goto err_unlock;
1915 }
1916 if (obj->in_use != 0U) {
1917 ret = FFA_ERROR_DENIED;
1918 goto err_unlock;
1919 }
Marc Bonnici503320e2022-02-21 15:02:36 +00001920
Marc Bonnici82e28f12022-10-18 13:39:48 +01001921 if (obj->desc_filled != obj->desc_size) {
1922 WARN("%s: incomplete object desc filled %zu < size %zu\n",
1923 __func__, obj->desc_filled, obj->desc_size);
1924 ret = FFA_ERROR_INVALID_PARAMETER;
1925 goto err_unlock;
1926 }
1927
Marc Bonnici503320e2022-02-21 15:02:36 +00001928 /* Allow for platform specific operations to be performed. */
1929 ret = plat_spmc_shmem_reclaim(&obj->desc);
1930 if (ret != 0) {
1931 goto err_unlock;
1932 }
1933
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001934 spmc_shmem_obj_free(&spmc_shmem_obj_state, obj);
1935 spin_unlock(&spmc_shmem_obj_state.lock);
1936
1937 SMC_RET1(handle, FFA_SUCCESS_SMC32);
1938
1939err_unlock:
1940 spin_unlock(&spmc_shmem_obj_state.lock);
1941 return spmc_ffa_error_return(handle, ret);
1942}