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