blob: 7680a5b891441fbc6b0568e3a6514a03381ffee1 [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/**
Marc Bonnici9bdcb742022-06-06 14:37:57 +0100251 * spmc_shmem_obj_validate_id - Validate a partition ID is participating in
252 * a given memory transaction.
253 * @sp_id: Partition ID to validate.
Shruti Gupta20ce06c2022-08-25 14:22:53 +0100254 * @obj: The shared memory object containing the descriptor
255 * of the memory transaction.
Marc Bonnici9bdcb742022-06-06 14:37:57 +0100256 * Return: true if ID is valid, else false.
257 */
Shruti Gupta20ce06c2022-08-25 14:22:53 +0100258bool spmc_shmem_obj_validate_id(struct spmc_shmem_obj *obj, uint16_t sp_id)
Marc Bonnici9bdcb742022-06-06 14:37:57 +0100259{
260 bool found = false;
Shruti Gupta20ce06c2022-08-25 14:22:53 +0100261 struct ffa_mtd *desc = &obj->desc;
262 size_t desc_size = obj->desc_size;
Marc Bonnici9bdcb742022-06-06 14:37:57 +0100263
264 /* Validate the partition is a valid participant. */
265 for (unsigned int i = 0U; i < desc->emad_count; i++) {
266 size_t emad_size;
267 struct ffa_emad_v1_0 *emad;
268
269 emad = spmc_shmem_obj_get_emad(desc, i,
270 MAKE_FFA_VERSION(1, 1),
271 &emad_size);
Shruti Gupta20ce06c2022-08-25 14:22:53 +0100272 /*
273 * Validate the calculated emad address resides within the
274 * descriptor.
275 */
276 if ((emad == NULL) || (uintptr_t) emad >=
277 (uintptr_t)((uint8_t *) desc + desc_size)) {
278 VERBOSE("Invalid emad.\n");
279 break;
280 }
Marc Bonnici9bdcb742022-06-06 14:37:57 +0100281 if (sp_id == emad->mapd.endpoint_id) {
282 found = true;
283 break;
284 }
285 }
286 return found;
287}
288
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000289/*
290 * Compare two memory regions to determine if any range overlaps with another
291 * ongoing memory transaction.
292 */
293static bool
294overlapping_memory_regions(struct ffa_comp_mrd *region1,
295 struct ffa_comp_mrd *region2)
296{
297 uint64_t region1_start;
298 uint64_t region1_size;
299 uint64_t region1_end;
300 uint64_t region2_start;
301 uint64_t region2_size;
302 uint64_t region2_end;
303
304 assert(region1 != NULL);
305 assert(region2 != NULL);
306
307 if (region1 == region2) {
308 return true;
309 }
310
311 /*
312 * Check each memory region in the request against existing
313 * transactions.
314 */
315 for (size_t i = 0; i < region1->address_range_count; i++) {
316
317 region1_start = region1->address_range_array[i].address;
318 region1_size =
319 region1->address_range_array[i].page_count *
320 PAGE_SIZE_4KB;
321 region1_end = region1_start + region1_size;
322
323 for (size_t j = 0; j < region2->address_range_count; j++) {
324
325 region2_start = region2->address_range_array[j].address;
326 region2_size =
327 region2->address_range_array[j].page_count *
328 PAGE_SIZE_4KB;
329 region2_end = region2_start + region2_size;
330
Marc Bonnici79669bb2022-10-18 13:50:04 +0100331 /* Check if regions are not overlapping. */
332 if (!((region2_end <= region1_start) ||
333 (region1_end <= region2_start))) {
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000334 WARN("Overlapping mem regions 0x%lx-0x%lx & 0x%lx-0x%lx\n",
335 region1_start, region1_end,
336 region2_start, region2_end);
337 return true;
338 }
339 }
340 }
341 return false;
342}
343
Marc Bonnicid1907f02022-04-19 17:42:53 +0100344/*******************************************************************************
345 * FF-A v1.0 Memory Descriptor Conversion Helpers.
346 ******************************************************************************/
347/**
348 * spmc_shm_get_v1_1_descriptor_size - Calculate the required size for a v1.1
349 * converted descriptor.
350 * @orig: The original v1.0 memory transaction descriptor.
351 * @desc_size: The size of the original v1.0 memory transaction descriptor.
352 *
353 * Return: the size required to store the descriptor store in the v1.1 format.
354 */
Demi Marie Obenour81f0adc2023-01-12 14:28:32 -0500355static uint64_t
Marc Bonnicid1907f02022-04-19 17:42:53 +0100356spmc_shm_get_v1_1_descriptor_size(struct ffa_mtd_v1_0 *orig, size_t desc_size)
357{
Demi Marie Obenour81f0adc2023-01-12 14:28:32 -0500358 uint64_t size = 0;
Marc Bonnicid1907f02022-04-19 17:42:53 +0100359 struct ffa_comp_mrd *mrd;
360 struct ffa_emad_v1_0 *emad_array = orig->emad;
361
362 /* Get the size of the v1.1 descriptor. */
363 size += sizeof(struct ffa_mtd);
364
365 /* Add the size of the emad descriptors. */
366 size += orig->emad_count * sizeof(struct ffa_emad_v1_0);
367
368 /* Add the size of the composite mrds. */
369 size += sizeof(struct ffa_comp_mrd);
370
371 /* Add the size of the constituent mrds. */
372 mrd = (struct ffa_comp_mrd *) ((uint8_t *) orig +
373 emad_array[0].comp_mrd_offset);
374
Demi Marie Obenour81f0adc2023-01-12 14:28:32 -0500375 /* Add the size of the memory region descriptors. */
Marc Bonnicid1907f02022-04-19 17:42:53 +0100376 size += mrd->address_range_count * sizeof(struct ffa_cons_mrd);
377
378 return size;
379}
380
381/**
382 * spmc_shm_get_v1_0_descriptor_size - Calculate the required size for a v1.0
383 * converted descriptor.
384 * @orig: The original v1.1 memory transaction descriptor.
385 * @desc_size: The size of the original v1.1 memory transaction descriptor.
386 *
387 * Return: the size required to store the descriptor store in the v1.0 format.
388 */
389static size_t
390spmc_shm_get_v1_0_descriptor_size(struct ffa_mtd *orig, size_t desc_size)
391{
392 size_t size = 0;
393 struct ffa_comp_mrd *mrd;
394 struct ffa_emad_v1_0 *emad_array = (struct ffa_emad_v1_0 *)
395 ((uint8_t *) orig +
396 orig->emad_offset);
397
398 /* Get the size of the v1.0 descriptor. */
399 size += sizeof(struct ffa_mtd_v1_0);
400
401 /* Add the size of the v1.0 emad descriptors. */
402 size += orig->emad_count * sizeof(struct ffa_emad_v1_0);
403
404 /* Add the size of the composite mrds. */
405 size += sizeof(struct ffa_comp_mrd);
406
407 /* Add the size of the constituent mrds. */
408 mrd = (struct ffa_comp_mrd *) ((uint8_t *) orig +
409 emad_array[0].comp_mrd_offset);
410
411 /* Check the calculated address is within the memory descriptor. */
Marc Bonnicif744c992022-10-18 18:01:44 +0100412 if (((uintptr_t) mrd + sizeof(struct ffa_comp_mrd)) >
413 (uintptr_t)((uint8_t *) orig + desc_size)) {
Marc Bonnicid1907f02022-04-19 17:42:53 +0100414 return 0;
415 }
416 size += mrd->address_range_count * sizeof(struct ffa_cons_mrd);
417
418 return size;
419}
420
421/**
422 * spmc_shm_convert_shmem_obj_from_v1_0 - Converts a given v1.0 memory object.
423 * @out_obj: The shared memory object to populate the converted descriptor.
424 * @orig: The shared memory object containing the v1.0 descriptor.
425 *
426 * Return: true if the conversion is successful else false.
427 */
428static bool
429spmc_shm_convert_shmem_obj_from_v1_0(struct spmc_shmem_obj *out_obj,
430 struct spmc_shmem_obj *orig)
431{
432 struct ffa_mtd_v1_0 *mtd_orig = (struct ffa_mtd_v1_0 *) &orig->desc;
433 struct ffa_mtd *out = &out_obj->desc;
434 struct ffa_emad_v1_0 *emad_array_in;
435 struct ffa_emad_v1_0 *emad_array_out;
436 struct ffa_comp_mrd *mrd_in;
437 struct ffa_comp_mrd *mrd_out;
438
439 size_t mrd_in_offset;
440 size_t mrd_out_offset;
441 size_t mrd_size = 0;
442
443 /* Populate the new descriptor format from the v1.0 struct. */
444 out->sender_id = mtd_orig->sender_id;
445 out->memory_region_attributes = mtd_orig->memory_region_attributes;
446 out->flags = mtd_orig->flags;
447 out->handle = mtd_orig->handle;
448 out->tag = mtd_orig->tag;
449 out->emad_count = mtd_orig->emad_count;
450 out->emad_size = sizeof(struct ffa_emad_v1_0);
451
452 /*
453 * We will locate the emad descriptors directly after the ffa_mtd
454 * struct. This will be 8-byte aligned.
455 */
456 out->emad_offset = sizeof(struct ffa_mtd);
457
458 emad_array_in = mtd_orig->emad;
459 emad_array_out = (struct ffa_emad_v1_0 *)
460 ((uint8_t *) out + out->emad_offset);
461
462 /* Copy across the emad structs. */
463 for (unsigned int i = 0U; i < out->emad_count; i++) {
Shruti Gupta20ce06c2022-08-25 14:22:53 +0100464 /* Bound check for emad array. */
465 if (((uint8_t *)emad_array_in + sizeof(struct ffa_emad_v1_0)) >
466 ((uint8_t *) mtd_orig + orig->desc_size)) {
467 VERBOSE("%s: Invalid mtd structure.\n", __func__);
468 return false;
469 }
Marc Bonnicid1907f02022-04-19 17:42:53 +0100470 memcpy(&emad_array_out[i], &emad_array_in[i],
471 sizeof(struct ffa_emad_v1_0));
472 }
473
474 /* Place the mrd descriptors after the end of the emad descriptors.*/
475 mrd_in_offset = emad_array_in->comp_mrd_offset;
476 mrd_out_offset = out->emad_offset + (out->emad_size * out->emad_count);
477 mrd_out = (struct ffa_comp_mrd *) ((uint8_t *) out + mrd_out_offset);
478
479 /* Add the size of the composite memory region descriptor. */
480 mrd_size += sizeof(struct ffa_comp_mrd);
481
482 /* Find the mrd descriptor. */
483 mrd_in = (struct ffa_comp_mrd *) ((uint8_t *) mtd_orig + mrd_in_offset);
484
485 /* Add the size of the constituent memory region descriptors. */
486 mrd_size += mrd_in->address_range_count * sizeof(struct ffa_cons_mrd);
487
488 /*
489 * Update the offset in the emads by the delta between the input and
490 * output addresses.
491 */
492 for (unsigned int i = 0U; i < out->emad_count; i++) {
493 emad_array_out[i].comp_mrd_offset =
494 emad_array_in[i].comp_mrd_offset +
495 (mrd_out_offset - mrd_in_offset);
496 }
497
498 /* Verify that we stay within bound of the memory descriptors. */
499 if ((uintptr_t)((uint8_t *) mrd_in + mrd_size) >
500 (uintptr_t)((uint8_t *) mtd_orig + orig->desc_size) ||
501 ((uintptr_t)((uint8_t *) mrd_out + mrd_size) >
502 (uintptr_t)((uint8_t *) out + out_obj->desc_size))) {
503 ERROR("%s: Invalid mrd structure.\n", __func__);
504 return false;
505 }
506
507 /* Copy the mrd descriptors directly. */
508 memcpy(mrd_out, mrd_in, mrd_size);
509
510 return true;
511}
512
513/**
514 * spmc_shm_convert_mtd_to_v1_0 - Converts a given v1.1 memory object to
515 * v1.0 memory object.
516 * @out_obj: The shared memory object to populate the v1.0 descriptor.
517 * @orig: The shared memory object containing the v1.1 descriptor.
518 *
519 * Return: true if the conversion is successful else false.
520 */
521static bool
522spmc_shm_convert_mtd_to_v1_0(struct spmc_shmem_obj *out_obj,
523 struct spmc_shmem_obj *orig)
524{
525 struct ffa_mtd *mtd_orig = &orig->desc;
526 struct ffa_mtd_v1_0 *out = (struct ffa_mtd_v1_0 *) &out_obj->desc;
527 struct ffa_emad_v1_0 *emad_in;
528 struct ffa_emad_v1_0 *emad_array_in;
529 struct ffa_emad_v1_0 *emad_array_out;
530 struct ffa_comp_mrd *mrd_in;
531 struct ffa_comp_mrd *mrd_out;
532
533 size_t mrd_in_offset;
534 size_t mrd_out_offset;
535 size_t emad_out_array_size;
536 size_t mrd_size = 0;
Shruti Gupta20ce06c2022-08-25 14:22:53 +0100537 size_t orig_desc_size = orig->desc_size;
Marc Bonnicid1907f02022-04-19 17:42:53 +0100538
539 /* Populate the v1.0 descriptor format from the v1.1 struct. */
540 out->sender_id = mtd_orig->sender_id;
541 out->memory_region_attributes = mtd_orig->memory_region_attributes;
542 out->flags = mtd_orig->flags;
543 out->handle = mtd_orig->handle;
544 out->tag = mtd_orig->tag;
545 out->emad_count = mtd_orig->emad_count;
546
547 /* Determine the location of the emad array in both descriptors. */
548 emad_array_in = (struct ffa_emad_v1_0 *)
549 ((uint8_t *) mtd_orig + mtd_orig->emad_offset);
550 emad_array_out = out->emad;
551
552 /* Copy across the emad structs. */
553 emad_in = emad_array_in;
554 for (unsigned int i = 0U; i < out->emad_count; i++) {
Shruti Gupta20ce06c2022-08-25 14:22:53 +0100555 /* Bound check for emad array. */
556 if (((uint8_t *)emad_in + sizeof(struct ffa_emad_v1_0)) >
557 ((uint8_t *) mtd_orig + orig_desc_size)) {
558 VERBOSE("%s: Invalid mtd structure.\n", __func__);
559 return false;
560 }
Marc Bonnicid1907f02022-04-19 17:42:53 +0100561 memcpy(&emad_array_out[i], emad_in,
562 sizeof(struct ffa_emad_v1_0));
563
564 emad_in += mtd_orig->emad_size;
565 }
566
567 /* Place the mrd descriptors after the end of the emad descriptors. */
568 emad_out_array_size = sizeof(struct ffa_emad_v1_0) * out->emad_count;
569
570 mrd_out_offset = (uint8_t *) out->emad - (uint8_t *) out +
571 emad_out_array_size;
572
573 mrd_out = (struct ffa_comp_mrd *) ((uint8_t *) out + mrd_out_offset);
574
575 mrd_in_offset = mtd_orig->emad_offset +
576 (mtd_orig->emad_size * mtd_orig->emad_count);
577
578 /* Add the size of the composite memory region descriptor. */
579 mrd_size += sizeof(struct ffa_comp_mrd);
580
581 /* Find the mrd descriptor. */
582 mrd_in = (struct ffa_comp_mrd *) ((uint8_t *) mtd_orig + mrd_in_offset);
583
584 /* Add the size of the constituent memory region descriptors. */
585 mrd_size += mrd_in->address_range_count * sizeof(struct ffa_cons_mrd);
586
587 /*
588 * Update the offset in the emads by the delta between the input and
589 * output addresses.
590 */
591 emad_in = emad_array_in;
592
593 for (unsigned int i = 0U; i < out->emad_count; i++) {
594 emad_array_out[i].comp_mrd_offset = emad_in->comp_mrd_offset +
595 (mrd_out_offset -
596 mrd_in_offset);
597 emad_in += mtd_orig->emad_size;
598 }
599
600 /* Verify that we stay within bound of the memory descriptors. */
601 if ((uintptr_t)((uint8_t *) mrd_in + mrd_size) >
602 (uintptr_t)((uint8_t *) mtd_orig + orig->desc_size) ||
603 ((uintptr_t)((uint8_t *) mrd_out + mrd_size) >
604 (uintptr_t)((uint8_t *) out + out_obj->desc_size))) {
605 ERROR("%s: Invalid mrd structure.\n", __func__);
606 return false;
607 }
608
609 /* Copy the mrd descriptors directly. */
610 memcpy(mrd_out, mrd_in, mrd_size);
611
612 return true;
613}
614
615/**
616 * spmc_populate_ffa_v1_0_descriptor - Converts a given v1.1 memory object to
617 * the v1.0 format and populates the
618 * provided buffer.
619 * @dst: Buffer to populate v1.0 ffa_memory_region_descriptor.
620 * @orig_obj: Object containing v1.1 ffa_memory_region_descriptor.
621 * @buf_size: Size of the buffer to populate.
622 * @offset: The offset of the converted descriptor to copy.
623 * @copy_size: Will be populated with the number of bytes copied.
624 * @out_desc_size: Will be populated with the total size of the v1.0
625 * descriptor.
626 *
627 * Return: 0 if conversion and population succeeded.
628 * Note: This function invalidates the reference to @orig therefore
629 * `spmc_shmem_obj_lookup` must be called if further usage is required.
630 */
631static uint32_t
632spmc_populate_ffa_v1_0_descriptor(void *dst, struct spmc_shmem_obj *orig_obj,
633 size_t buf_size, size_t offset,
634 size_t *copy_size, size_t *v1_0_desc_size)
635{
636 struct spmc_shmem_obj *v1_0_obj;
637
638 /* Calculate the size that the v1.0 descriptor will require. */
639 *v1_0_desc_size = spmc_shm_get_v1_0_descriptor_size(
640 &orig_obj->desc, orig_obj->desc_size);
641
642 if (*v1_0_desc_size == 0) {
643 ERROR("%s: cannot determine size of descriptor.\n",
644 __func__);
645 return FFA_ERROR_INVALID_PARAMETER;
646 }
647
648 /* Get a new obj to store the v1.0 descriptor. */
649 v1_0_obj = spmc_shmem_obj_alloc(&spmc_shmem_obj_state,
650 *v1_0_desc_size);
651
652 if (!v1_0_obj) {
653 return FFA_ERROR_NO_MEMORY;
654 }
655
656 /* Perform the conversion from v1.1 to v1.0. */
657 if (!spmc_shm_convert_mtd_to_v1_0(v1_0_obj, orig_obj)) {
658 spmc_shmem_obj_free(&spmc_shmem_obj_state, v1_0_obj);
659 return FFA_ERROR_INVALID_PARAMETER;
660 }
661
662 *copy_size = MIN(v1_0_obj->desc_size - offset, buf_size);
663 memcpy(dst, (uint8_t *) &v1_0_obj->desc + offset, *copy_size);
664
665 /*
666 * We're finished with the v1.0 descriptor for now so free it.
667 * Note that this will invalidate any references to the v1.1
668 * descriptor.
669 */
670 spmc_shmem_obj_free(&spmc_shmem_obj_state, v1_0_obj);
671
672 return 0;
673}
674
Demi Marie Obenour4ed9df42022-12-30 19:30:58 -0500675static int
676spmc_validate_mtd_start(struct ffa_mtd *desc, uint32_t ffa_version,
677 size_t fragment_length, size_t total_length)
678{
679 unsigned long long emad_end;
680 unsigned long long emad_size;
681 unsigned long long emad_offset;
682 unsigned int min_desc_size;
683
684 /* Determine the appropriate minimum descriptor size. */
685 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
686 min_desc_size = sizeof(struct ffa_mtd_v1_0);
687 } else if (ffa_version == MAKE_FFA_VERSION(1, 1)) {
688 min_desc_size = sizeof(struct ffa_mtd);
689 } else {
690 return FFA_ERROR_INVALID_PARAMETER;
691 }
692 if (fragment_length < min_desc_size) {
693 WARN("%s: invalid length %zu < %u\n", __func__, fragment_length,
694 min_desc_size);
695 return FFA_ERROR_INVALID_PARAMETER;
696 }
697
698 if (desc->emad_count == 0U) {
699 WARN("%s: unsupported attribute desc count %u.\n",
700 __func__, desc->emad_count);
701 return FFA_ERROR_INVALID_PARAMETER;
702 }
703
704 /*
705 * If the caller is using FF-A v1.0 interpret the descriptor as a v1.0
706 * format, otherwise assume it is a v1.1 format.
707 */
708 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
709 emad_offset = emad_size = sizeof(struct ffa_emad_v1_0);
710 } else {
711 if (!is_aligned(desc->emad_offset, 16)) {
712 WARN("%s: Emad offset %" PRIx32 " is not 16-byte aligned.\n",
713 __func__, desc->emad_offset);
714 return FFA_ERROR_INVALID_PARAMETER;
715 }
716 if (desc->emad_offset < sizeof(struct ffa_mtd)) {
717 WARN("%s: Emad offset too small: 0x%" PRIx32 " < 0x%zx.\n",
718 __func__, desc->emad_offset,
719 sizeof(struct ffa_mtd));
720 return FFA_ERROR_INVALID_PARAMETER;
721 }
722 emad_offset = desc->emad_offset;
723 if (desc->emad_size < sizeof(struct ffa_emad_v1_0)) {
724 WARN("%s: Bad emad size (%" PRIu32 " < %zu).\n", __func__,
725 desc->emad_size, sizeof(struct ffa_emad_v1_0));
726 return FFA_ERROR_INVALID_PARAMETER;
727 }
728 if (!is_aligned(desc->emad_size, 16)) {
729 WARN("%s: Emad size 0x%" PRIx32 " is not 16-byte aligned.\n",
730 __func__, desc->emad_size);
731 return FFA_ERROR_INVALID_PARAMETER;
732 }
733 emad_size = desc->emad_size;
734 }
735
736 /*
737 * Overflow is impossible: the arithmetic happens in at least 64-bit
738 * precision, but all of the operands are bounded by UINT32_MAX, and
739 * ((2^32 - 1)^2 + (2^32 - 1) + (2^32 - 1)) = ((2^32 - 1) * (2^32 + 1))
740 * = (2^64 - 1).
741 */
742 CASSERT(sizeof(desc->emad_count == 4), assert_emad_count_max_too_large);
743 emad_end = (desc->emad_count * (unsigned long long)emad_size) +
744 (unsigned long long)sizeof(struct ffa_comp_mrd) +
745 (unsigned long long)emad_offset;
746
747 if (emad_end > total_length) {
748 WARN("%s: Composite memory region extends beyond descriptor: 0x%llx > 0x%zx\n",
749 __func__, emad_end, total_length);
750 return FFA_ERROR_INVALID_PARAMETER;
751 }
752
753 return 0;
754}
755
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100756/**
757 * spmc_shmem_check_obj - Check that counts in descriptor match overall size.
Marc Bonnicid1907f02022-04-19 17:42:53 +0100758 * @obj: Object containing ffa_memory_region_descriptor.
759 * @ffa_version: FF-A version of the provided descriptor.
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100760 *
Marc Bonnici336630f2022-01-13 11:39:10 +0000761 * Return: 0 if object is valid, -EINVAL if constituent_memory_region_descriptor
762 * offset or count is invalid.
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100763 */
Marc Bonnicid1907f02022-04-19 17:42:53 +0100764static int spmc_shmem_check_obj(struct spmc_shmem_obj *obj,
765 uint32_t ffa_version)
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100766{
Demi Marie Obenourb720be72023-01-12 13:33:02 -0500767 const struct ffa_emad_v1_0 *emad;
768 size_t emad_size;
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000769 uint32_t comp_mrd_offset = 0;
Demi Marie Obenourb720be72023-01-12 13:33:02 -0500770
Demi Marie Obenourf00e4d72023-01-12 13:25:23 -0500771 if (obj->desc_filled != obj->desc_size) {
772 ERROR("BUG: %s called on incomplete object (%zu != %zu)\n",
773 __func__, obj->desc_filled, obj->desc_size);
774 panic();
775 }
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000776
Demi Marie Obenourf00e4d72023-01-12 13:25:23 -0500777 if (spmc_validate_mtd_start(&obj->desc, ffa_version,
778 obj->desc_filled, obj->desc_size)) {
779 ERROR("BUG: %s called on object with corrupt memory region descriptor\n",
780 __func__);
781 panic();
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100782 }
783
Demi Marie Obenourb720be72023-01-12 13:33:02 -0500784 emad = spmc_shmem_obj_get_emad(&obj->desc, 0,
785 ffa_version, &emad_size);
786
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100787 for (size_t emad_num = 0; emad_num < obj->desc.emad_count; emad_num++) {
788 size_t size;
789 size_t count;
790 size_t expected_size;
Demi Marie Obenour00d36b22023-01-12 13:24:50 -0500791 uint64_t total_page_count;
Marc Bonnicid1907f02022-04-19 17:42:53 +0100792 size_t header_emad_size;
793 uint32_t offset;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100794 struct ffa_comp_mrd *comp;
Demi Marie Obenourb720be72023-01-12 13:33:02 -0500795 ffa_endpoint_id16_t ep_id;
Marc Bonnicid1907f02022-04-19 17:42:53 +0100796
797 /*
798 * Validate the calculated emad address resides within the
799 * descriptor.
800 */
Demi Marie Obenourb720be72023-01-12 13:33:02 -0500801 if ((uintptr_t) emad >
802 ((uintptr_t) &obj->desc + obj->desc_size - emad_size)) {
803 ERROR("BUG: Invalid emad access not detected earlier.\n");
804 panic();
Marc Bonnicid1907f02022-04-19 17:42:53 +0100805 }
806
Demi Marie Obenourb720be72023-01-12 13:33:02 -0500807 emad = (const struct ffa_emad_v1_0 *)((const uint8_t *)emad + emad_size);
Marc Bonnicid1907f02022-04-19 17:42:53 +0100808 offset = emad->comp_mrd_offset;
809
Demi Marie Obenour8711be32023-01-11 14:20:07 -0500810 /*
Demi Marie Obenourb720be72023-01-12 13:33:02 -0500811 * If a partition ID resides in the secure world validate that
812 * the partition ID is for a known partition. Ignore any
813 * partition ID belonging to the normal world as it is assumed
814 * the Hypervisor will have validated these.
815 */
816 ep_id = emad->mapd.endpoint_id;
817 if (ffa_is_secure_world_id(ep_id)) {
818 if (spmc_get_sp_ctx(ep_id) == NULL) {
819 WARN("%s: Invalid receiver id 0x%x\n",
820 __func__, ep_id);
821 return -EINVAL;
822 }
823 }
824
825 /*
Demi Marie Obenour8711be32023-01-11 14:20:07 -0500826 * The offset provided to the composite memory region descriptor
827 * should be consistent across endpoint descriptors. Store the
828 * first entry and compare against subsequent entries.
829 */
830 if (comp_mrd_offset == 0) {
831 comp_mrd_offset = offset;
832 } else {
833 if (comp_mrd_offset != offset) {
834 ERROR("%s: mismatching offsets provided, %u != %u\n",
835 __func__, offset, comp_mrd_offset);
836 return -EINVAL;
837 }
838 continue; /* Remainder only executed on first iteration. */
839 }
840
Demi Marie Obenour2bb87352023-01-11 14:25:24 -0500841 header_emad_size = (size_t)((uint8_t *)emad - (uint8_t *)&obj->desc) +
Marc Bonnicid1907f02022-04-19 17:42:53 +0100842 (obj->desc.emad_count * emad_size);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100843
844 if (offset < header_emad_size) {
845 WARN("%s: invalid object, offset %u < header + emad %zu\n",
846 __func__, offset, header_emad_size);
847 return -EINVAL;
848 }
849
850 size = obj->desc_size;
851
852 if (offset > size) {
853 WARN("%s: invalid object, offset %u > total size %zu\n",
854 __func__, offset, obj->desc_size);
855 return -EINVAL;
856 }
857 size -= offset;
858
859 if (size < sizeof(struct ffa_comp_mrd)) {
860 WARN("%s: invalid object, offset %u, total size %zu, no header space.\n",
861 __func__, offset, obj->desc_size);
862 return -EINVAL;
863 }
864 size -= sizeof(struct ffa_comp_mrd);
865
866 count = size / sizeof(struct ffa_cons_mrd);
867
Marc Bonnicid1907f02022-04-19 17:42:53 +0100868 comp = spmc_shmem_obj_get_comp_mrd(obj, ffa_version);
869
870 if (comp == NULL) {
871 WARN("%s: invalid comp_mrd offset\n", __func__);
872 return -EINVAL;
873 }
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100874
875 if (comp->address_range_count != count) {
876 WARN("%s: invalid object, desc count %u != %zu\n",
877 __func__, comp->address_range_count, count);
878 return -EINVAL;
879 }
880
881 expected_size = offset + sizeof(*comp) +
Demi Marie Obenour33e66662023-01-12 13:52:16 -0500882 count * sizeof(struct ffa_cons_mrd);
Marc Bonnicid1907f02022-04-19 17:42:53 +0100883
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100884 if (expected_size != obj->desc_size) {
885 WARN("%s: invalid object, computed size %zu != size %zu\n",
886 __func__, expected_size, obj->desc_size);
887 return -EINVAL;
888 }
889
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100890 total_page_count = 0;
891
892 for (size_t i = 0; i < count; i++) {
893 total_page_count +=
894 comp->address_range_array[i].page_count;
895 }
896 if (comp->total_page_count != total_page_count) {
Demi Marie Obenour00d36b22023-01-12 13:24:50 -0500897 WARN("%s: invalid object, desc total_page_count %u != %" PRIu64 "\n",
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100898 __func__, comp->total_page_count,
899 total_page_count);
900 return -EINVAL;
901 }
902 }
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000903 return 0;
904}
905
906/**
907 * spmc_shmem_check_state_obj - Check if the descriptor describes memory
908 * regions that are currently involved with an
909 * existing memory transactions. This implies that
910 * the memory is not in a valid state for lending.
911 * @obj: Object containing ffa_memory_region_descriptor.
912 *
913 * Return: 0 if object is valid, -EINVAL if invalid memory state.
914 */
Marc Bonnicid1907f02022-04-19 17:42:53 +0100915static int spmc_shmem_check_state_obj(struct spmc_shmem_obj *obj,
916 uint32_t ffa_version)
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000917{
918 size_t obj_offset = 0;
919 struct spmc_shmem_obj *inflight_obj;
920
921 struct ffa_comp_mrd *other_mrd;
Marc Bonnicid1907f02022-04-19 17:42:53 +0100922 struct ffa_comp_mrd *requested_mrd = spmc_shmem_obj_get_comp_mrd(obj,
923 ffa_version);
924
925 if (requested_mrd == NULL) {
926 return -EINVAL;
927 }
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100928
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000929 inflight_obj = spmc_shmem_obj_get_next(&spmc_shmem_obj_state,
930 &obj_offset);
931
932 while (inflight_obj != NULL) {
933 /*
934 * Don't compare the transaction to itself or to partially
935 * transmitted descriptors.
936 */
937 if ((obj->desc.handle != inflight_obj->desc.handle) &&
938 (obj->desc_size == obj->desc_filled)) {
Marc Bonnicid1907f02022-04-19 17:42:53 +0100939 other_mrd = spmc_shmem_obj_get_comp_mrd(inflight_obj,
Marc Bonnici344ca9d2022-05-20 14:38:55 +0100940 FFA_VERSION_COMPILED);
Marc Bonnicid1907f02022-04-19 17:42:53 +0100941 if (other_mrd == NULL) {
942 return -EINVAL;
943 }
Marc Bonnicic31ec9e2022-01-21 10:34:55 +0000944 if (overlapping_memory_regions(requested_mrd,
945 other_mrd)) {
946 return -EINVAL;
947 }
948 }
949
950 inflight_obj = spmc_shmem_obj_get_next(&spmc_shmem_obj_state,
951 &obj_offset);
952 }
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100953 return 0;
954}
955
956static long spmc_ffa_fill_desc(struct mailbox *mbox,
957 struct spmc_shmem_obj *obj,
958 uint32_t fragment_length,
959 ffa_mtd_flag32_t mtd_flag,
Marc Bonnicid1907f02022-04-19 17:42:53 +0100960 uint32_t ffa_version,
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100961 void *smc_handle)
962{
963 int ret;
Marc Bonnicid1907f02022-04-19 17:42:53 +0100964 size_t emad_size;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100965 uint32_t handle_low;
966 uint32_t handle_high;
Marc Bonnicid1907f02022-04-19 17:42:53 +0100967 struct ffa_emad_v1_0 *emad;
968 struct ffa_emad_v1_0 *other_emad;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100969
970 if (mbox->rxtx_page_count == 0U) {
971 WARN("%s: buffer pair not registered.\n", __func__);
Marc Bonnicid1907f02022-04-19 17:42:53 +0100972 ret = FFA_ERROR_INVALID_PARAMETER;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100973 goto err_arg;
974 }
975
Demi Marie Obenour4a3a6d32023-01-12 14:22:18 -0500976 CASSERT(sizeof(mbox->rxtx_page_count) == 4, assert_bogus_page_count);
977 if (fragment_length > (uint64_t)mbox->rxtx_page_count * PAGE_SIZE_4KB) {
978 WARN("%s: bad fragment size %u > %" PRIu64 " buffer size\n", __func__,
979 fragment_length, (uint64_t)mbox->rxtx_page_count * PAGE_SIZE_4KB);
Marc Bonnicid1907f02022-04-19 17:42:53 +0100980 ret = FFA_ERROR_INVALID_PARAMETER;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100981 goto err_arg;
982 }
983
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100984 if (fragment_length > obj->desc_size - obj->desc_filled) {
985 WARN("%s: bad fragment size %u > %zu remaining\n", __func__,
986 fragment_length, obj->desc_size - obj->desc_filled);
Marc Bonnicid1907f02022-04-19 17:42:53 +0100987 ret = FFA_ERROR_INVALID_PARAMETER;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100988 goto err_arg;
989 }
990
Marc Bonnicif0f45dc2022-10-18 13:57:16 +0100991 memcpy((uint8_t *)&obj->desc + obj->desc_filled,
992 (uint8_t *) mbox->tx_buffer, fragment_length);
993
Marc Bonnici9f23c8d2021-10-01 16:06:04 +0100994 /* Ensure that the sender ID resides in the normal world. */
995 if (ffa_is_secure_world_id(obj->desc.sender_id)) {
996 WARN("%s: Invalid sender ID 0x%x.\n",
997 __func__, obj->desc.sender_id);
998 ret = FFA_ERROR_DENIED;
999 goto err_arg;
1000 }
1001
Marc Bonnici08f28ef2022-04-19 16:52:59 +01001002 /* Ensure the NS bit is set to 0. */
1003 if ((obj->desc.memory_region_attributes & FFA_MEM_ATTR_NS_BIT) != 0U) {
1004 WARN("%s: NS mem attributes flags MBZ.\n", __func__);
1005 ret = FFA_ERROR_INVALID_PARAMETER;
1006 goto err_arg;
1007 }
1008
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001009 /*
1010 * We don't currently support any optional flags so ensure none are
1011 * requested.
1012 */
1013 if (obj->desc.flags != 0U && mtd_flag != 0U &&
1014 (obj->desc.flags != mtd_flag)) {
1015 WARN("%s: invalid memory transaction flags %u != %u\n",
1016 __func__, obj->desc.flags, mtd_flag);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001017 ret = FFA_ERROR_INVALID_PARAMETER;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001018 goto err_arg;
1019 }
1020
1021 if (obj->desc_filled == 0U) {
1022 /* First fragment, descriptor header has been copied */
Demi Marie Obenour4ed9df42022-12-30 19:30:58 -05001023 ret = spmc_validate_mtd_start(&obj->desc, ffa_version,
1024 fragment_length, obj->desc_size);
1025 if (ret != 0) {
1026 goto err_bad_desc;
1027 }
1028
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001029 obj->desc.handle = spmc_shmem_obj_state.next_handle++;
1030 obj->desc.flags |= mtd_flag;
1031 }
1032
1033 obj->desc_filled += fragment_length;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001034
1035 handle_low = (uint32_t)obj->desc.handle;
1036 handle_high = obj->desc.handle >> 32;
1037
1038 if (obj->desc_filled != obj->desc_size) {
1039 SMC_RET8(smc_handle, FFA_MEM_FRAG_RX, handle_low,
1040 handle_high, obj->desc_filled,
1041 (uint32_t)obj->desc.sender_id << 16, 0, 0, 0);
1042 }
1043
Marc Bonnici336630f2022-01-13 11:39:10 +00001044 /* The full descriptor has been received, perform any final checks. */
1045
Demi Marie Obenourcdd3e722023-01-11 14:16:37 -05001046 ret = spmc_shmem_check_obj(obj, ffa_version);
1047 if (ret != 0) {
1048 ret = FFA_ERROR_INVALID_PARAMETER;
1049 goto err_bad_desc;
1050 }
1051
Marc Bonnici336630f2022-01-13 11:39:10 +00001052 /* Ensure partition IDs are not duplicated. */
1053 for (size_t i = 0; i < obj->desc.emad_count; i++) {
Marc Bonnicid1907f02022-04-19 17:42:53 +01001054 emad = spmc_shmem_obj_get_emad(&obj->desc, i, ffa_version,
1055 &emad_size);
Demi Marie Obenour57bf10c2022-12-31 11:11:18 -05001056
Marc Bonnici336630f2022-01-13 11:39:10 +00001057 for (size_t j = i + 1; j < obj->desc.emad_count; j++) {
Marc Bonnicid1907f02022-04-19 17:42:53 +01001058 other_emad = spmc_shmem_obj_get_emad(&obj->desc, j,
1059 ffa_version,
1060 &emad_size);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001061
1062 if (emad->mapd.endpoint_id ==
1063 other_emad->mapd.endpoint_id) {
1064 WARN("%s: Duplicated endpoint id 0x%x\n",
1065 __func__, emad->mapd.endpoint_id);
1066 ret = FFA_ERROR_INVALID_PARAMETER;
1067 goto err_bad_desc;
1068 }
Marc Bonnici336630f2022-01-13 11:39:10 +00001069 }
1070 }
1071
Marc Bonnicid1907f02022-04-19 17:42:53 +01001072 ret = spmc_shmem_check_state_obj(obj, ffa_version);
Marc Bonnicic31ec9e2022-01-21 10:34:55 +00001073 if (ret) {
1074 ERROR("%s: invalid memory region descriptor.\n", __func__);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001075 ret = FFA_ERROR_INVALID_PARAMETER;
Marc Bonnicic31ec9e2022-01-21 10:34:55 +00001076 goto err_bad_desc;
1077 }
1078
Marc Bonnicid1907f02022-04-19 17:42:53 +01001079 /*
1080 * Everything checks out, if the sender was using FF-A v1.0, convert
1081 * the descriptor format to use the v1.1 structures.
1082 */
1083 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1084 struct spmc_shmem_obj *v1_1_obj;
1085 uint64_t mem_handle;
1086
1087 /* Calculate the size that the v1.1 descriptor will required. */
Demi Marie Obenour81f0adc2023-01-12 14:28:32 -05001088 uint64_t v1_1_desc_size =
Marc Bonnicid1907f02022-04-19 17:42:53 +01001089 spmc_shm_get_v1_1_descriptor_size((void *) &obj->desc,
vallau0146dbac22022-08-08 14:10:14 +02001090 obj->desc_size);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001091
Demi Marie Obenour81f0adc2023-01-12 14:28:32 -05001092 if (v1_1_desc_size > UINT32_MAX) {
1093 ret = FFA_ERROR_NO_MEMORY;
Marc Bonnicid1907f02022-04-19 17:42:53 +01001094 goto err_arg;
1095 }
1096
1097 /* Get a new obj to store the v1.1 descriptor. */
1098 v1_1_obj =
Demi Marie Obenour81f0adc2023-01-12 14:28:32 -05001099 spmc_shmem_obj_alloc(&spmc_shmem_obj_state, (size_t)v1_1_desc_size);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001100
vallau018f830992022-08-09 18:03:28 +02001101 if (!v1_1_obj) {
Marc Bonnicid1907f02022-04-19 17:42:53 +01001102 ret = FFA_ERROR_NO_MEMORY;
1103 goto err_arg;
1104 }
1105
1106 /* Perform the conversion from v1.0 to v1.1. */
Demi Marie Obenour81f0adc2023-01-12 14:28:32 -05001107 v1_1_obj->desc_size = (uint32_t)v1_1_desc_size;
1108 v1_1_obj->desc_filled = (uint32_t)v1_1_desc_size;
Marc Bonnicid1907f02022-04-19 17:42:53 +01001109 if (!spmc_shm_convert_shmem_obj_from_v1_0(v1_1_obj, obj)) {
1110 ERROR("%s: Could not convert mtd!\n", __func__);
1111 spmc_shmem_obj_free(&spmc_shmem_obj_state, v1_1_obj);
1112 goto err_arg;
1113 }
1114
1115 /*
1116 * We're finished with the v1.0 descriptor so free it
1117 * and continue our checks with the new v1.1 descriptor.
1118 */
1119 mem_handle = obj->desc.handle;
1120 spmc_shmem_obj_free(&spmc_shmem_obj_state, obj);
1121 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, mem_handle);
1122 if (obj == NULL) {
1123 ERROR("%s: Failed to find converted descriptor.\n",
1124 __func__);
1125 ret = FFA_ERROR_INVALID_PARAMETER;
1126 return spmc_ffa_error_return(smc_handle, ret);
1127 }
1128 }
1129
Marc Bonnici503320e2022-02-21 15:02:36 +00001130 /* Allow for platform specific operations to be performed. */
1131 ret = plat_spmc_shmem_begin(&obj->desc);
1132 if (ret != 0) {
1133 goto err_arg;
1134 }
1135
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001136 SMC_RET8(smc_handle, FFA_SUCCESS_SMC32, 0, handle_low, handle_high, 0,
1137 0, 0, 0);
1138
1139err_bad_desc:
1140err_arg:
1141 spmc_shmem_obj_free(&spmc_shmem_obj_state, obj);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001142 return spmc_ffa_error_return(smc_handle, ret);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001143}
1144
1145/**
1146 * spmc_ffa_mem_send - FFA_MEM_SHARE/LEND implementation.
1147 * @client: Client state.
1148 * @total_length: Total length of shared memory descriptor.
1149 * @fragment_length: Length of fragment of shared memory descriptor passed in
1150 * this call.
1151 * @address: Not supported, must be 0.
1152 * @page_count: Not supported, must be 0.
1153 * @smc_handle: Handle passed to smc call. Used to return
1154 * FFA_MEM_FRAG_RX or SMC_FC_FFA_SUCCESS.
1155 *
1156 * Implements a subset of the FF-A FFA_MEM_SHARE and FFA_MEM_LEND calls needed
1157 * to share or lend memory from non-secure os to secure os (with no stream
1158 * endpoints).
1159 *
1160 * Return: 0 on success, error code on failure.
1161 */
1162long spmc_ffa_mem_send(uint32_t smc_fid,
1163 bool secure_origin,
1164 uint64_t total_length,
1165 uint32_t fragment_length,
1166 uint64_t address,
1167 uint32_t page_count,
1168 void *cookie,
1169 void *handle,
1170 uint64_t flags)
1171
1172{
1173 long ret;
1174 struct spmc_shmem_obj *obj;
1175 struct mailbox *mbox = spmc_get_mbox_desc(secure_origin);
1176 ffa_mtd_flag32_t mtd_flag;
Marc Bonnicid1907f02022-04-19 17:42:53 +01001177 uint32_t ffa_version = get_partition_ffa_version(secure_origin);
Demi Marie Obenour1f9f8302022-12-30 19:14:18 -05001178 size_t min_desc_size;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001179
1180 if (address != 0U || page_count != 0U) {
1181 WARN("%s: custom memory region for message not supported.\n",
1182 __func__);
1183 return spmc_ffa_error_return(handle,
1184 FFA_ERROR_INVALID_PARAMETER);
1185 }
1186
1187 if (secure_origin) {
1188 WARN("%s: unsupported share direction.\n", __func__);
1189 return spmc_ffa_error_return(handle,
1190 FFA_ERROR_INVALID_PARAMETER);
1191 }
1192
Demi Marie Obenour1f9f8302022-12-30 19:14:18 -05001193 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1194 min_desc_size = sizeof(struct ffa_mtd_v1_0);
1195 } else if (ffa_version == MAKE_FFA_VERSION(1, 1)) {
1196 min_desc_size = sizeof(struct ffa_mtd);
1197 } else {
1198 WARN("%s: bad FF-A version.\n", __func__);
1199 return spmc_ffa_error_return(handle,
1200 FFA_ERROR_INVALID_PARAMETER);
1201 }
1202
1203 /* Check if the descriptor is too small for the FF-A version. */
1204 if (fragment_length < min_desc_size) {
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001205 WARN("%s: bad first fragment size %u < %zu\n",
Marc Bonnicid1907f02022-04-19 17:42:53 +01001206 __func__, fragment_length, sizeof(struct ffa_mtd_v1_0));
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001207 return spmc_ffa_error_return(handle,
1208 FFA_ERROR_INVALID_PARAMETER);
1209 }
1210
1211 if ((smc_fid & FUNCID_NUM_MASK) == FFA_FNUM_MEM_SHARE) {
1212 mtd_flag = FFA_MTD_FLAG_TYPE_SHARE_MEMORY;
1213 } else if ((smc_fid & FUNCID_NUM_MASK) == FFA_FNUM_MEM_LEND) {
1214 mtd_flag = FFA_MTD_FLAG_TYPE_LEND_MEMORY;
1215 } else {
1216 WARN("%s: invalid memory management operation.\n", __func__);
1217 return spmc_ffa_error_return(handle,
1218 FFA_ERROR_INVALID_PARAMETER);
1219 }
1220
1221 spin_lock(&spmc_shmem_obj_state.lock);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001222 obj = spmc_shmem_obj_alloc(&spmc_shmem_obj_state, total_length);
1223 if (obj == NULL) {
1224 ret = FFA_ERROR_NO_MEMORY;
1225 goto err_unlock;
1226 }
1227
1228 spin_lock(&mbox->lock);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001229 ret = spmc_ffa_fill_desc(mbox, obj, fragment_length, mtd_flag,
1230 ffa_version, handle);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001231 spin_unlock(&mbox->lock);
1232
1233 spin_unlock(&spmc_shmem_obj_state.lock);
1234 return ret;
1235
1236err_unlock:
1237 spin_unlock(&spmc_shmem_obj_state.lock);
1238 return spmc_ffa_error_return(handle, ret);
1239}
1240
1241/**
1242 * spmc_ffa_mem_frag_tx - FFA_MEM_FRAG_TX implementation.
1243 * @client: Client state.
1244 * @handle_low: Handle_low value returned from FFA_MEM_FRAG_RX.
1245 * @handle_high: Handle_high value returned from FFA_MEM_FRAG_RX.
1246 * @fragment_length: Length of fragments transmitted.
1247 * @sender_id: Vmid of sender in bits [31:16]
1248 * @smc_handle: Handle passed to smc call. Used to return
1249 * FFA_MEM_FRAG_RX or SMC_FC_FFA_SUCCESS.
1250 *
1251 * Return: @smc_handle on success, error code on failure.
1252 */
1253long spmc_ffa_mem_frag_tx(uint32_t smc_fid,
1254 bool secure_origin,
1255 uint64_t handle_low,
1256 uint64_t handle_high,
1257 uint32_t fragment_length,
1258 uint32_t sender_id,
1259 void *cookie,
1260 void *handle,
1261 uint64_t flags)
1262{
1263 long ret;
1264 uint32_t desc_sender_id;
Marc Bonnicid1907f02022-04-19 17:42:53 +01001265 uint32_t ffa_version = get_partition_ffa_version(secure_origin);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001266 struct mailbox *mbox = spmc_get_mbox_desc(secure_origin);
1267
1268 struct spmc_shmem_obj *obj;
1269 uint64_t mem_handle = handle_low | (((uint64_t)handle_high) << 32);
1270
1271 spin_lock(&spmc_shmem_obj_state.lock);
1272
1273 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, mem_handle);
1274 if (obj == NULL) {
1275 WARN("%s: invalid handle, 0x%lx, not a valid handle.\n",
1276 __func__, mem_handle);
1277 ret = FFA_ERROR_INVALID_PARAMETER;
1278 goto err_unlock;
1279 }
1280
1281 desc_sender_id = (uint32_t)obj->desc.sender_id << 16;
1282 if (sender_id != desc_sender_id) {
1283 WARN("%s: invalid sender_id 0x%x != 0x%x\n", __func__,
1284 sender_id, desc_sender_id);
1285 ret = FFA_ERROR_INVALID_PARAMETER;
1286 goto err_unlock;
1287 }
1288
1289 if (obj->desc_filled == obj->desc_size) {
1290 WARN("%s: object desc already filled, %zu\n", __func__,
1291 obj->desc_filled);
1292 ret = FFA_ERROR_INVALID_PARAMETER;
1293 goto err_unlock;
1294 }
1295
1296 spin_lock(&mbox->lock);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001297 ret = spmc_ffa_fill_desc(mbox, obj, fragment_length, 0, ffa_version,
1298 handle);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001299 spin_unlock(&mbox->lock);
1300
1301 spin_unlock(&spmc_shmem_obj_state.lock);
1302 return ret;
1303
1304err_unlock:
1305 spin_unlock(&spmc_shmem_obj_state.lock);
1306 return spmc_ffa_error_return(handle, ret);
1307}
1308
1309/**
Marc Bonnici08f28ef2022-04-19 16:52:59 +01001310 * spmc_ffa_mem_retrieve_set_ns_bit - Set the NS bit in the response descriptor
1311 * if the caller implements a version greater
1312 * than FF-A 1.0 or if they have requested
1313 * the functionality.
1314 * TODO: We are assuming that the caller is
1315 * an SP. To support retrieval from the
1316 * normal world this function will need to be
1317 * expanded accordingly.
1318 * @resp: Descriptor populated in callers RX buffer.
1319 * @sp_ctx: Context of the calling SP.
1320 */
1321void spmc_ffa_mem_retrieve_set_ns_bit(struct ffa_mtd *resp,
1322 struct secure_partition_desc *sp_ctx)
1323{
1324 if (sp_ctx->ffa_version > MAKE_FFA_VERSION(1, 0) ||
1325 sp_ctx->ns_bit_requested) {
1326 /*
1327 * Currently memory senders must reside in the normal
1328 * world, and we do not have the functionlaity to change
1329 * the state of memory dynamically. Therefore we can always set
1330 * the NS bit to 1.
1331 */
1332 resp->memory_region_attributes |= FFA_MEM_ATTR_NS_BIT;
1333 }
1334}
1335
1336/**
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001337 * spmc_ffa_mem_retrieve_req - FFA_MEM_RETRIEVE_REQ implementation.
1338 * @smc_fid: FID of SMC
1339 * @total_length: Total length of retrieve request descriptor if this is
1340 * the first call. Otherwise (unsupported) must be 0.
1341 * @fragment_length: Length of fragment of retrieve request descriptor passed
1342 * in this call. Only @fragment_length == @length is
1343 * supported by this implementation.
1344 * @address: Not supported, must be 0.
1345 * @page_count: Not supported, must be 0.
1346 * @smc_handle: Handle passed to smc call. Used to return
1347 * FFA_MEM_RETRIEVE_RESP.
1348 *
1349 * Implements a subset of the FF-A FFA_MEM_RETRIEVE_REQ call.
1350 * Used by secure os to retrieve memory already shared by non-secure os.
1351 * If the data does not fit in a single FFA_MEM_RETRIEVE_RESP message,
1352 * the client must call FFA_MEM_FRAG_RX until the full response has been
1353 * received.
1354 *
1355 * Return: @handle on success, error code on failure.
1356 */
1357long
1358spmc_ffa_mem_retrieve_req(uint32_t smc_fid,
1359 bool secure_origin,
1360 uint32_t total_length,
1361 uint32_t fragment_length,
1362 uint64_t address,
1363 uint32_t page_count,
1364 void *cookie,
1365 void *handle,
1366 uint64_t flags)
1367{
1368 int ret;
1369 size_t buf_size;
Marc Bonnicid1907f02022-04-19 17:42:53 +01001370 size_t copy_size = 0;
1371 size_t min_desc_size;
1372 size_t out_desc_size = 0;
1373
1374 /*
1375 * Currently we are only accessing fields that are the same in both the
1376 * v1.0 and v1.1 mtd struct therefore we can use a v1.1 struct directly
1377 * here. We only need validate against the appropriate struct size.
1378 */
1379 struct ffa_mtd *resp;
1380 const struct ffa_mtd *req;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001381 struct spmc_shmem_obj *obj = NULL;
1382 struct mailbox *mbox = spmc_get_mbox_desc(secure_origin);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001383 uint32_t ffa_version = get_partition_ffa_version(secure_origin);
Marc Bonnici08f28ef2022-04-19 16:52:59 +01001384 struct secure_partition_desc *sp_ctx = spmc_get_current_sp_ctx();
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001385
1386 if (!secure_origin) {
1387 WARN("%s: unsupported retrieve req direction.\n", __func__);
1388 return spmc_ffa_error_return(handle,
1389 FFA_ERROR_INVALID_PARAMETER);
1390 }
1391
1392 if (address != 0U || page_count != 0U) {
1393 WARN("%s: custom memory region not supported.\n", __func__);
1394 return spmc_ffa_error_return(handle,
1395 FFA_ERROR_INVALID_PARAMETER);
1396 }
1397
1398 spin_lock(&mbox->lock);
1399
1400 req = mbox->tx_buffer;
1401 resp = mbox->rx_buffer;
1402 buf_size = mbox->rxtx_page_count * FFA_PAGE_SIZE;
1403
1404 if (mbox->rxtx_page_count == 0U) {
1405 WARN("%s: buffer pair not registered.\n", __func__);
1406 ret = FFA_ERROR_INVALID_PARAMETER;
1407 goto err_unlock_mailbox;
1408 }
1409
1410 if (mbox->state != MAILBOX_STATE_EMPTY) {
1411 WARN("%s: RX Buffer is full! %d\n", __func__, mbox->state);
1412 ret = FFA_ERROR_DENIED;
1413 goto err_unlock_mailbox;
1414 }
1415
1416 if (fragment_length != total_length) {
1417 WARN("%s: fragmented retrieve request not supported.\n",
1418 __func__);
1419 ret = FFA_ERROR_INVALID_PARAMETER;
1420 goto err_unlock_mailbox;
1421 }
1422
Marc Bonnici336630f2022-01-13 11:39:10 +00001423 if (req->emad_count == 0U) {
1424 WARN("%s: unsupported attribute desc count %u.\n",
1425 __func__, obj->desc.emad_count);
vallau01460d3962022-08-09 17:06:53 +02001426 ret = FFA_ERROR_INVALID_PARAMETER;
1427 goto err_unlock_mailbox;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001428 }
1429
Marc Bonnicid1907f02022-04-19 17:42:53 +01001430 /* Determine the appropriate minimum descriptor size. */
1431 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1432 min_desc_size = sizeof(struct ffa_mtd_v1_0);
1433 } else {
1434 min_desc_size = sizeof(struct ffa_mtd);
1435 }
1436 if (total_length < min_desc_size) {
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001437 WARN("%s: invalid length %u < %zu\n", __func__, total_length,
Marc Bonnicid1907f02022-04-19 17:42:53 +01001438 min_desc_size);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001439 ret = FFA_ERROR_INVALID_PARAMETER;
1440 goto err_unlock_mailbox;
1441 }
1442
1443 spin_lock(&spmc_shmem_obj_state.lock);
1444
1445 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, req->handle);
1446 if (obj == NULL) {
1447 ret = FFA_ERROR_INVALID_PARAMETER;
1448 goto err_unlock_all;
1449 }
1450
1451 if (obj->desc_filled != obj->desc_size) {
1452 WARN("%s: incomplete object desc filled %zu < size %zu\n",
1453 __func__, obj->desc_filled, obj->desc_size);
1454 ret = FFA_ERROR_INVALID_PARAMETER;
1455 goto err_unlock_all;
1456 }
1457
1458 if (req->emad_count != 0U && req->sender_id != obj->desc.sender_id) {
1459 WARN("%s: wrong sender id 0x%x != 0x%x\n",
1460 __func__, req->sender_id, obj->desc.sender_id);
1461 ret = FFA_ERROR_INVALID_PARAMETER;
1462 goto err_unlock_all;
1463 }
1464
1465 if (req->emad_count != 0U && req->tag != obj->desc.tag) {
1466 WARN("%s: wrong tag 0x%lx != 0x%lx\n",
1467 __func__, req->tag, obj->desc.tag);
1468 ret = FFA_ERROR_INVALID_PARAMETER;
1469 goto err_unlock_all;
1470 }
1471
Marc Bonnici336630f2022-01-13 11:39:10 +00001472 if (req->emad_count != 0U && req->emad_count != obj->desc.emad_count) {
1473 WARN("%s: mistmatch of endpoint counts %u != %u\n",
1474 __func__, req->emad_count, obj->desc.emad_count);
1475 ret = FFA_ERROR_INVALID_PARAMETER;
1476 goto err_unlock_all;
1477 }
1478
Marc Bonnici08f28ef2022-04-19 16:52:59 +01001479 /* Ensure the NS bit is set to 0 in the request. */
1480 if ((req->memory_region_attributes & FFA_MEM_ATTR_NS_BIT) != 0U) {
1481 WARN("%s: NS mem attributes flags MBZ.\n", __func__);
1482 ret = FFA_ERROR_INVALID_PARAMETER;
1483 goto err_unlock_all;
1484 }
1485
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001486 if (req->flags != 0U) {
1487 if ((req->flags & FFA_MTD_FLAG_TYPE_MASK) !=
1488 (obj->desc.flags & FFA_MTD_FLAG_TYPE_MASK)) {
1489 /*
1490 * If the retrieve request specifies the memory
1491 * transaction ensure it matches what we expect.
1492 */
1493 WARN("%s: wrong mem transaction flags %x != %x\n",
1494 __func__, req->flags, obj->desc.flags);
1495 ret = FFA_ERROR_INVALID_PARAMETER;
1496 goto err_unlock_all;
1497 }
1498
1499 if (req->flags != FFA_MTD_FLAG_TYPE_SHARE_MEMORY &&
1500 req->flags != FFA_MTD_FLAG_TYPE_LEND_MEMORY) {
1501 /*
1502 * Current implementation does not support donate and
1503 * it supports no other flags.
1504 */
1505 WARN("%s: invalid flags 0x%x\n", __func__, req->flags);
1506 ret = FFA_ERROR_INVALID_PARAMETER;
1507 goto err_unlock_all;
1508 }
1509 }
1510
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001511 /* Validate the caller is a valid participant. */
Shruti Gupta20ce06c2022-08-25 14:22:53 +01001512 if (!spmc_shmem_obj_validate_id(obj, sp_ctx->sp_id)) {
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001513 WARN("%s: Invalid endpoint ID (0x%x).\n",
1514 __func__, sp_ctx->sp_id);
1515 ret = FFA_ERROR_INVALID_PARAMETER;
1516 goto err_unlock_all;
1517 }
1518
Marc Bonnicid1907f02022-04-19 17:42:53 +01001519 /* Validate that the provided emad offset and structure is valid.*/
1520 for (size_t i = 0; i < req->emad_count; i++) {
1521 size_t emad_size;
1522 struct ffa_emad_v1_0 *emad;
1523
1524 emad = spmc_shmem_obj_get_emad(req, i, ffa_version,
1525 &emad_size);
Marc Bonnicid1907f02022-04-19 17:42:53 +01001526
1527 if ((uintptr_t) emad >= (uintptr_t)
1528 ((uint8_t *) req + total_length)) {
1529 WARN("Invalid emad access.\n");
1530 ret = FFA_ERROR_INVALID_PARAMETER;
1531 goto err_unlock_all;
1532 }
Marc Bonnici336630f2022-01-13 11:39:10 +00001533 }
1534
1535 /*
1536 * Validate all the endpoints match in the case of multiple
1537 * borrowers. We don't mandate that the order of the borrowers
1538 * must match in the descriptors therefore check to see if the
1539 * endpoints match in any order.
1540 */
1541 for (size_t i = 0; i < req->emad_count; i++) {
1542 bool found = false;
Marc Bonnicid1907f02022-04-19 17:42:53 +01001543 size_t emad_size;
1544 struct ffa_emad_v1_0 *emad;
1545 struct ffa_emad_v1_0 *other_emad;
1546
1547 emad = spmc_shmem_obj_get_emad(req, i, ffa_version,
1548 &emad_size);
Marc Bonnici336630f2022-01-13 11:39:10 +00001549
1550 for (size_t j = 0; j < obj->desc.emad_count; j++) {
Marc Bonnicid1907f02022-04-19 17:42:53 +01001551 other_emad = spmc_shmem_obj_get_emad(
1552 &obj->desc, j, MAKE_FFA_VERSION(1, 1),
1553 &emad_size);
1554
Marc Bonnicid1907f02022-04-19 17:42:53 +01001555 if (req->emad_count &&
1556 emad->mapd.endpoint_id ==
1557 other_emad->mapd.endpoint_id) {
Marc Bonnici336630f2022-01-13 11:39:10 +00001558 found = true;
1559 break;
1560 }
1561 }
1562
1563 if (!found) {
1564 WARN("%s: invalid receiver id (0x%x).\n",
Marc Bonnicid1907f02022-04-19 17:42:53 +01001565 __func__, emad->mapd.endpoint_id);
Marc Bonnici336630f2022-01-13 11:39:10 +00001566 ret = FFA_ERROR_INVALID_PARAMETER;
1567 goto err_unlock_all;
1568 }
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001569 }
1570
1571 mbox->state = MAILBOX_STATE_FULL;
1572
1573 if (req->emad_count != 0U) {
1574 obj->in_use++;
1575 }
1576
Marc Bonnicid1907f02022-04-19 17:42:53 +01001577 /*
1578 * If the caller is v1.0 convert the descriptor, otherwise copy
1579 * directly.
1580 */
1581 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1582 ret = spmc_populate_ffa_v1_0_descriptor(resp, obj, buf_size, 0,
1583 &copy_size,
1584 &out_desc_size);
1585 if (ret != 0U) {
1586 ERROR("%s: Failed to process descriptor.\n", __func__);
1587 goto err_unlock_all;
1588 }
1589 } else {
1590 copy_size = MIN(obj->desc_size, buf_size);
1591 out_desc_size = obj->desc_size;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001592
Marc Bonnicid1907f02022-04-19 17:42:53 +01001593 memcpy(resp, &obj->desc, copy_size);
1594 }
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001595
Marc Bonnici08f28ef2022-04-19 16:52:59 +01001596 /* Set the NS bit in the response if applicable. */
1597 spmc_ffa_mem_retrieve_set_ns_bit(resp, sp_ctx);
1598
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001599 spin_unlock(&spmc_shmem_obj_state.lock);
1600 spin_unlock(&mbox->lock);
1601
Marc Bonnicid1907f02022-04-19 17:42:53 +01001602 SMC_RET8(handle, FFA_MEM_RETRIEVE_RESP, out_desc_size,
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001603 copy_size, 0, 0, 0, 0, 0);
1604
1605err_unlock_all:
1606 spin_unlock(&spmc_shmem_obj_state.lock);
1607err_unlock_mailbox:
1608 spin_unlock(&mbox->lock);
1609 return spmc_ffa_error_return(handle, ret);
1610}
1611
1612/**
1613 * spmc_ffa_mem_frag_rx - FFA_MEM_FRAG_RX implementation.
1614 * @client: Client state.
1615 * @handle_low: Handle passed to &FFA_MEM_RETRIEVE_REQ. Bit[31:0].
1616 * @handle_high: Handle passed to &FFA_MEM_RETRIEVE_REQ. Bit[63:32].
1617 * @fragment_offset: Byte offset in descriptor to resume at.
1618 * @sender_id: Bit[31:16]: Endpoint id of sender if client is a
1619 * hypervisor. 0 otherwise.
1620 * @smc_handle: Handle passed to smc call. Used to return
1621 * FFA_MEM_FRAG_TX.
1622 *
1623 * Return: @smc_handle on success, error code on failure.
1624 */
1625long spmc_ffa_mem_frag_rx(uint32_t smc_fid,
1626 bool secure_origin,
1627 uint32_t handle_low,
1628 uint32_t handle_high,
1629 uint32_t fragment_offset,
1630 uint32_t sender_id,
1631 void *cookie,
1632 void *handle,
1633 uint64_t flags)
1634{
1635 int ret;
1636 void *src;
1637 size_t buf_size;
1638 size_t copy_size;
1639 size_t full_copy_size;
1640 uint32_t desc_sender_id;
1641 struct mailbox *mbox = spmc_get_mbox_desc(secure_origin);
1642 uint64_t mem_handle = handle_low | (((uint64_t)handle_high) << 32);
1643 struct spmc_shmem_obj *obj;
Marc Bonnicid1907f02022-04-19 17:42:53 +01001644 uint32_t ffa_version = get_partition_ffa_version(secure_origin);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001645
1646 if (!secure_origin) {
1647 WARN("%s: can only be called from swld.\n",
1648 __func__);
1649 return spmc_ffa_error_return(handle,
1650 FFA_ERROR_INVALID_PARAMETER);
1651 }
1652
1653 spin_lock(&spmc_shmem_obj_state.lock);
1654
1655 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, mem_handle);
1656 if (obj == NULL) {
1657 WARN("%s: invalid handle, 0x%lx, not a valid handle.\n",
1658 __func__, mem_handle);
1659 ret = FFA_ERROR_INVALID_PARAMETER;
1660 goto err_unlock_shmem;
1661 }
1662
1663 desc_sender_id = (uint32_t)obj->desc.sender_id << 16;
1664 if (sender_id != 0U && sender_id != desc_sender_id) {
1665 WARN("%s: invalid sender_id 0x%x != 0x%x\n", __func__,
1666 sender_id, desc_sender_id);
1667 ret = FFA_ERROR_INVALID_PARAMETER;
1668 goto err_unlock_shmem;
1669 }
1670
1671 if (fragment_offset >= obj->desc_size) {
1672 WARN("%s: invalid fragment_offset 0x%x >= 0x%zx\n",
1673 __func__, fragment_offset, obj->desc_size);
1674 ret = FFA_ERROR_INVALID_PARAMETER;
1675 goto err_unlock_shmem;
1676 }
1677
1678 spin_lock(&mbox->lock);
1679
1680 if (mbox->rxtx_page_count == 0U) {
1681 WARN("%s: buffer pair not registered.\n", __func__);
1682 ret = FFA_ERROR_INVALID_PARAMETER;
1683 goto err_unlock_all;
1684 }
1685
1686 if (mbox->state != MAILBOX_STATE_EMPTY) {
1687 WARN("%s: RX Buffer is full!\n", __func__);
1688 ret = FFA_ERROR_DENIED;
1689 goto err_unlock_all;
1690 }
1691
1692 buf_size = mbox->rxtx_page_count * FFA_PAGE_SIZE;
1693
1694 mbox->state = MAILBOX_STATE_FULL;
1695
Marc Bonnicid1907f02022-04-19 17:42:53 +01001696 /*
1697 * If the caller is v1.0 convert the descriptor, otherwise copy
1698 * directly.
1699 */
1700 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1701 size_t out_desc_size;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001702
Marc Bonnicid1907f02022-04-19 17:42:53 +01001703 ret = spmc_populate_ffa_v1_0_descriptor(mbox->rx_buffer, obj,
1704 buf_size,
1705 fragment_offset,
1706 &copy_size,
1707 &out_desc_size);
1708 if (ret != 0U) {
1709 ERROR("%s: Failed to process descriptor.\n", __func__);
1710 goto err_unlock_all;
1711 }
1712 } else {
1713 full_copy_size = obj->desc_size - fragment_offset;
1714 copy_size = MIN(full_copy_size, buf_size);
1715
1716 src = &obj->desc;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001717
Marc Bonnicid1907f02022-04-19 17:42:53 +01001718 memcpy(mbox->rx_buffer, src + fragment_offset, copy_size);
1719 }
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001720
1721 spin_unlock(&mbox->lock);
1722 spin_unlock(&spmc_shmem_obj_state.lock);
1723
1724 SMC_RET8(handle, FFA_MEM_FRAG_TX, handle_low, handle_high,
1725 copy_size, sender_id, 0, 0, 0);
1726
1727err_unlock_all:
1728 spin_unlock(&mbox->lock);
1729err_unlock_shmem:
1730 spin_unlock(&spmc_shmem_obj_state.lock);
1731 return spmc_ffa_error_return(handle, ret);
1732}
1733
1734/**
1735 * spmc_ffa_mem_relinquish - FFA_MEM_RELINQUISH implementation.
1736 * @client: Client state.
1737 *
1738 * Implements a subset of the FF-A FFA_MEM_RELINQUISH call.
1739 * Used by secure os release previously shared memory to non-secure os.
1740 *
1741 * The handle to release must be in the client's (secure os's) transmit buffer.
1742 *
1743 * Return: 0 on success, error code on failure.
1744 */
1745int spmc_ffa_mem_relinquish(uint32_t smc_fid,
1746 bool secure_origin,
1747 uint32_t handle_low,
1748 uint32_t handle_high,
1749 uint32_t fragment_offset,
1750 uint32_t sender_id,
1751 void *cookie,
1752 void *handle,
1753 uint64_t flags)
1754{
1755 int ret;
1756 struct mailbox *mbox = spmc_get_mbox_desc(secure_origin);
1757 struct spmc_shmem_obj *obj;
1758 const struct ffa_mem_relinquish_descriptor *req;
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001759 struct secure_partition_desc *sp_ctx = spmc_get_current_sp_ctx();
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001760
1761 if (!secure_origin) {
1762 WARN("%s: unsupported relinquish direction.\n", __func__);
1763 return spmc_ffa_error_return(handle,
1764 FFA_ERROR_INVALID_PARAMETER);
1765 }
1766
1767 spin_lock(&mbox->lock);
1768
1769 if (mbox->rxtx_page_count == 0U) {
1770 WARN("%s: buffer pair not registered.\n", __func__);
1771 ret = FFA_ERROR_INVALID_PARAMETER;
1772 goto err_unlock_mailbox;
1773 }
1774
1775 req = mbox->tx_buffer;
1776
1777 if (req->flags != 0U) {
1778 WARN("%s: unsupported flags 0x%x\n", __func__, req->flags);
1779 ret = FFA_ERROR_INVALID_PARAMETER;
1780 goto err_unlock_mailbox;
1781 }
1782
Marc Bonnici336630f2022-01-13 11:39:10 +00001783 if (req->endpoint_count == 0) {
1784 WARN("%s: endpoint count cannot be 0.\n", __func__);
1785 ret = FFA_ERROR_INVALID_PARAMETER;
1786 goto err_unlock_mailbox;
1787 }
1788
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001789 spin_lock(&spmc_shmem_obj_state.lock);
1790
1791 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, req->handle);
1792 if (obj == NULL) {
1793 ret = FFA_ERROR_INVALID_PARAMETER;
1794 goto err_unlock_all;
1795 }
1796
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001797 /*
1798 * Validate the endpoint ID was populated correctly. We don't currently
1799 * support proxy endpoints so the endpoint count should always be 1.
1800 */
1801 if (req->endpoint_count != 1U) {
1802 WARN("%s: unsupported endpoint count %u != 1\n", __func__,
1803 req->endpoint_count);
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001804 ret = FFA_ERROR_INVALID_PARAMETER;
1805 goto err_unlock_all;
1806 }
Marc Bonnici336630f2022-01-13 11:39:10 +00001807
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001808 /* Validate provided endpoint ID matches the partition ID. */
1809 if (req->endpoint_array[0] != sp_ctx->sp_id) {
1810 WARN("%s: invalid endpoint ID %u != %u\n", __func__,
1811 req->endpoint_array[0], sp_ctx->sp_id);
1812 ret = FFA_ERROR_INVALID_PARAMETER;
1813 goto err_unlock_all;
1814 }
Marc Bonnici336630f2022-01-13 11:39:10 +00001815
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001816 /* Validate the caller is a valid participant. */
Shruti Gupta20ce06c2022-08-25 14:22:53 +01001817 if (!spmc_shmem_obj_validate_id(obj, sp_ctx->sp_id)) {
Marc Bonnici9bdcb742022-06-06 14:37:57 +01001818 WARN("%s: Invalid endpoint ID (0x%x).\n",
1819 __func__, req->endpoint_array[0]);
1820 ret = FFA_ERROR_INVALID_PARAMETER;
1821 goto err_unlock_all;
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001822 }
Marc Bonnici336630f2022-01-13 11:39:10 +00001823
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001824 if (obj->in_use == 0U) {
1825 ret = FFA_ERROR_INVALID_PARAMETER;
1826 goto err_unlock_all;
1827 }
1828 obj->in_use--;
1829
1830 spin_unlock(&spmc_shmem_obj_state.lock);
1831 spin_unlock(&mbox->lock);
1832
1833 SMC_RET1(handle, FFA_SUCCESS_SMC32);
1834
1835err_unlock_all:
1836 spin_unlock(&spmc_shmem_obj_state.lock);
1837err_unlock_mailbox:
1838 spin_unlock(&mbox->lock);
1839 return spmc_ffa_error_return(handle, ret);
1840}
1841
1842/**
1843 * spmc_ffa_mem_reclaim - FFA_MEM_RECLAIM implementation.
1844 * @client: Client state.
1845 * @handle_low: Unique handle of shared memory object to reclaim. Bit[31:0].
1846 * @handle_high: Unique handle of shared memory object to reclaim.
1847 * Bit[63:32].
1848 * @flags: Unsupported, ignored.
1849 *
1850 * Implements a subset of the FF-A FFA_MEM_RECLAIM call.
1851 * Used by non-secure os reclaim memory previously shared with secure os.
1852 *
1853 * Return: 0 on success, error code on failure.
1854 */
1855int spmc_ffa_mem_reclaim(uint32_t smc_fid,
1856 bool secure_origin,
1857 uint32_t handle_low,
1858 uint32_t handle_high,
1859 uint32_t mem_flags,
1860 uint64_t x4,
1861 void *cookie,
1862 void *handle,
1863 uint64_t flags)
1864{
1865 int ret;
1866 struct spmc_shmem_obj *obj;
1867 uint64_t mem_handle = handle_low | (((uint64_t)handle_high) << 32);
1868
1869 if (secure_origin) {
1870 WARN("%s: unsupported reclaim direction.\n", __func__);
1871 return spmc_ffa_error_return(handle,
1872 FFA_ERROR_INVALID_PARAMETER);
1873 }
1874
1875 if (mem_flags != 0U) {
1876 WARN("%s: unsupported flags 0x%x\n", __func__, mem_flags);
1877 return spmc_ffa_error_return(handle,
1878 FFA_ERROR_INVALID_PARAMETER);
1879 }
1880
1881 spin_lock(&spmc_shmem_obj_state.lock);
1882
1883 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, mem_handle);
1884 if (obj == NULL) {
1885 ret = FFA_ERROR_INVALID_PARAMETER;
1886 goto err_unlock;
1887 }
1888 if (obj->in_use != 0U) {
1889 ret = FFA_ERROR_DENIED;
1890 goto err_unlock;
1891 }
Marc Bonnici503320e2022-02-21 15:02:36 +00001892
Marc Bonnici82e28f12022-10-18 13:39:48 +01001893 if (obj->desc_filled != obj->desc_size) {
1894 WARN("%s: incomplete object desc filled %zu < size %zu\n",
1895 __func__, obj->desc_filled, obj->desc_size);
1896 ret = FFA_ERROR_INVALID_PARAMETER;
1897 goto err_unlock;
1898 }
1899
Marc Bonnici503320e2022-02-21 15:02:36 +00001900 /* Allow for platform specific operations to be performed. */
1901 ret = plat_spmc_shmem_reclaim(&obj->desc);
1902 if (ret != 0) {
1903 goto err_unlock;
1904 }
1905
Marc Bonnici9f23c8d2021-10-01 16:06:04 +01001906 spmc_shmem_obj_free(&spmc_shmem_obj_state, obj);
1907 spin_unlock(&spmc_shmem_obj_state.lock);
1908
1909 SMC_RET1(handle, FFA_SUCCESS_SMC32);
1910
1911err_unlock:
1912 spin_unlock(&spmc_shmem_obj_state.lock);
1913 return spmc_ffa_error_return(handle, ret);
1914}