xref: /optee_os/core/kernel/transfer_list.c (revision 86dbb179ca94ad5512680d4a7691447c47f3bdb1)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2023, Linaro Limited
4  */
5 
6 /*******************************************************************************
7  * Transfer list library compliant with the Firmware Handoff specification at:
8  * https://github.com/FirmwareHandoff/firmware_handoff
9  ******************************************************************************/
10 
11 #include <kernel/cache_helpers.h>
12 #include <kernel/panic.h>
13 #include <kernel/transfer_list.h>
14 #include <mm/core_memprot.h>
15 #include <mm/core_mmu.h>
16 #include <string.h>
17 #include <util.h>
18 
19 /*******************************************************************************
20  * Adapt a physical address to match the maximum transfer entry data alignment
21  * required by an existing transfer list.
22  * Compliant with 2.4.6 of Firmware Handoff specification (v0.9).
23  * @pa: Physical address for adapting.
24  * @tl: Pointer to the existing transfer list.
25  * Return the adapted physical address.
26  ******************************************************************************/
27 static paddr_t get_align_base_addr(paddr_t pa,
28 				   struct transfer_list_header *tl)
29 {
30 	paddr_t align_mask = TL_ALIGNMENT_FROM_ORDER(tl->alignment) - 1;
31 	paddr_t align_off = (paddr_t)tl & align_mask;
32 	paddr_t new_addr = (pa & ~align_mask) + align_off;
33 
34 	if (new_addr < pa)
35 		new_addr += TL_ALIGNMENT_FROM_ORDER(tl->alignment);
36 
37 	return new_addr;
38 }
39 
40 static void unmap_list(struct transfer_list_header *tl, size_t sz)
41 {
42 	if (core_mmu_remove_mapping(MEM_AREA_TRANSFER_LIST, tl, sz))
43 		panic("Failed to remove transfer list mapping");
44 }
45 
46 struct transfer_list_header *transfer_list_map(paddr_t pa)
47 {
48 	struct transfer_list_header *tl = NULL;
49 	size_t sz = SMALL_PAGE_SIZE;
50 	size_t old_sz = 0;
51 
52 	while (true) {
53 		tl = core_mmu_add_mapping(MEM_AREA_TRANSFER_LIST, pa, sz);
54 		if (!tl) {
55 			EMSG("Failed to map TL with PA %#"PRIxPA", size %#zx",
56 			     pa, sz);
57 			return NULL;
58 		}
59 		old_sz = sz;
60 
61 		if (transfer_list_check_header(tl) == TL_OPS_NONE) {
62 			unmap_list(tl, sz);
63 			return NULL;
64 		}
65 
66 		if (tl->max_size <= sz)
67 			return tl;
68 
69 		sz = ROUNDUP(tl->max_size, SMALL_PAGE_SIZE);
70 		unmap_list(tl, old_sz);
71 	}
72 }
73 
74 void transfer_list_unmap_sync(struct transfer_list_header *tl)
75 {
76 	size_t sz = tl->max_size;
77 
78 	transfer_list_update_checksum(tl);
79 	dcache_cleaninv_range(tl, sz);
80 	unmap_list(tl, sz);
81 }
82 
83 void transfer_list_unmap_nosync(struct transfer_list_header *tl)
84 {
85 	unmap_list(tl, tl->max_size);
86 }
87 
88 void transfer_list_dump(struct transfer_list_header *tl)
89 {
90 	struct transfer_list_entry *tl_e = NULL;
91 	int i __maybe_unused = 0;
92 
93 	if (!tl)
94 		return;
95 
96 	DMSG("Dump transfer list:");
97 	DMSG("signature  %#"PRIx32, tl->signature);
98 	DMSG("checksum   %#"PRIx8, tl->checksum);
99 	DMSG("version    %#"PRIx8, tl->version);
100 	DMSG("hdr_size   %#"PRIx8, tl->hdr_size);
101 	DMSG("alignment  %#"PRIx8, tl->alignment);
102 	DMSG("size       %#"PRIx32, tl->size);
103 	DMSG("max_size   %#"PRIx32, tl->max_size);
104 	while (true) {
105 		tl_e = transfer_list_next(tl, tl_e);
106 		if (!tl_e)
107 			break;
108 
109 		DMSG("Entry %d:", i++);
110 		DMSG("tag_id     %#"PRIx16, tl_e->tag_id);
111 		DMSG("hdr_size   %#"PRIx8, tl_e->hdr_size);
112 		DMSG("data_size  %#"PRIx32, tl_e->data_size);
113 		DMSG("data_addr  %#"PRIxVA,
114 		     (vaddr_t)transfer_list_entry_data(tl_e));
115 	}
116 }
117 
118 /*******************************************************************************
119  * Creating a transfer list in a specified reserved memory region.
120  * Compliant with 2.4.5 of Firmware Handoff specification (v0.9).
121  * @pa: Physical address for residing the new transfer list.
122  * @max_size: Maximum size of the new transfer list.
123  * Return pointer to the created transfer list or NULL on error.
124  ******************************************************************************/
125 struct transfer_list_header *transfer_list_init(paddr_t pa, size_t max_size)
126 {
127 	struct transfer_list_header *tl = NULL;
128 	int align = TL_ALIGNMENT_FROM_ORDER(TRANSFER_LIST_INIT_MAX_ALIGN);
129 
130 	if (!pa || !max_size)
131 		return NULL;
132 
133 	if (!IS_ALIGNED(pa, align) || !IS_ALIGNED(max_size, align) ||
134 	    max_size < sizeof(*tl))
135 		return NULL;
136 
137 	tl = core_mmu_add_mapping(MEM_AREA_TRANSFER_LIST, pa, max_size);
138 	if (!tl)
139 		return NULL;
140 
141 	memset(tl, 0, max_size);
142 	tl->signature = TRANSFER_LIST_SIGNATURE;
143 	tl->version = TRANSFER_LIST_VERSION;
144 	tl->hdr_size = sizeof(*tl);
145 	tl->alignment = TRANSFER_LIST_INIT_MAX_ALIGN; /* initial max align */
146 	tl->size = sizeof(*tl); /* initial size is the size of header */
147 	tl->max_size = max_size;
148 
149 	transfer_list_update_checksum(tl);
150 
151 	return tl;
152 }
153 
154 /*******************************************************************************
155  * Relocating a transfer list to a specified reserved memory region.
156  * Compliant with 2.4.6 of Firmware Handoff specification (v0.9).
157  * @tl: Pointer to the transfer list for relocating.
158  * @pa: Physical address for relocating the transfer list.
159  * @max_size: Maximum size of the transfer list after relocating
160  * Return pointer to the relocated transfer list or NULL on error.
161  ******************************************************************************/
162 struct transfer_list_header *
163 transfer_list_relocate(struct transfer_list_header *tl, paddr_t pa,
164 		       size_t max_size)
165 {
166 	paddr_t new_addr = 0;
167 	struct transfer_list_header *new_tl = NULL;
168 	size_t new_max_size = 0;
169 
170 	if (!tl || !pa || !max_size)
171 		return NULL;
172 
173 	new_addr = get_align_base_addr(pa, tl);
174 	new_max_size = max_size - (new_addr - pa);
175 
176 	/* The new space is not sufficient for the TL */
177 	if (tl->size > new_max_size)
178 		return NULL;
179 
180 	new_tl = core_mmu_add_mapping(MEM_AREA_TRANSFER_LIST, new_addr,
181 				      new_max_size);
182 	if (!new_tl)
183 		return NULL;
184 
185 	memmove(new_tl, tl, tl->size);
186 	new_tl->max_size = new_max_size;
187 
188 	transfer_list_update_checksum(new_tl);
189 	transfer_list_unmap_nosync(tl);
190 
191 	return new_tl;
192 }
193 
194 /*******************************************************************************
195  * Verifying the header of a transfer list.
196  * Compliant with 2.4.1 of Firmware Handoff specification (v0.9).
197  * @tl: Pointer to the transfer list.
198  * Return transfer list operation status code.
199  ******************************************************************************/
200 int transfer_list_check_header(const struct transfer_list_header *tl)
201 {
202 	if (!tl)
203 		return TL_OPS_NONE;
204 
205 	if (tl->signature != TRANSFER_LIST_SIGNATURE) {
206 		EMSG("Bad transfer list signature %#"PRIx32, tl->signature);
207 		return TL_OPS_NONE;
208 	}
209 
210 	if (!tl->max_size) {
211 		EMSG("Bad transfer list max size %#"PRIx32, tl->max_size);
212 		return TL_OPS_NONE;
213 	}
214 
215 	if (tl->size > tl->max_size) {
216 		EMSG("Bad transfer list size %#"PRIx32, tl->size);
217 		return TL_OPS_NONE;
218 	}
219 
220 	if (tl->hdr_size != sizeof(struct transfer_list_header)) {
221 		EMSG("Bad transfer list header size %#"PRIx8, tl->hdr_size);
222 		return TL_OPS_NONE;
223 	}
224 
225 	if (!transfer_list_verify_checksum(tl)) {
226 		EMSG("Bad transfer list checksum %#"PRIx8, tl->checksum);
227 		return TL_OPS_NONE;
228 	}
229 
230 	if (tl->version == 0) {
231 		EMSG("Transfer list version is invalid");
232 		return TL_OPS_NONE;
233 	} else if (tl->version == TRANSFER_LIST_VERSION) {
234 		DMSG("Transfer list version is valid for all operations");
235 		return TL_OPS_ALL;
236 	} else if (tl->version > TRANSFER_LIST_VERSION) {
237 		DMSG("Transfer list version is valid for read-only");
238 		return TL_OPS_RO;
239 	}
240 
241 	DMSG("Old transfer list version is detected");
242 	return TL_OPS_CUS;
243 }
244 
245 /*******************************************************************************
246  * Enumerate the next transfer entry.
247  * @tl: Pointer to the transfer list.
248  * @cur: Pointer to the current transfer entry where we want to search for the
249  *       next one.
250  * Return pointer to the next transfer entry or NULL on error or if @cur is the
251  * last entry.
252  ******************************************************************************/
253 struct transfer_list_entry *transfer_list_next(struct transfer_list_header *tl,
254 					       struct transfer_list_entry *cur)
255 {
256 	struct transfer_list_entry *tl_e = NULL;
257 	vaddr_t tl_ev = 0;
258 	vaddr_t va = 0;
259 	vaddr_t ev = 0;
260 	size_t sz = 0;
261 
262 	if (!tl)
263 		return NULL;
264 
265 	tl_ev = (vaddr_t)tl + tl->size;
266 
267 	if (cur) {
268 		va = (vaddr_t)cur;
269 		/* check if the total size overflow */
270 		if (ADD_OVERFLOW(cur->hdr_size, cur->data_size, &sz))
271 			return NULL;
272 		/* roundup to the next entry */
273 		if (ADD_OVERFLOW(va, sz, &va) ||
274 		    ROUNDUP_OVERFLOW(va, TRANSFER_LIST_GRANULE, &va))
275 			return NULL;
276 	} else {
277 		va = (vaddr_t)tl + tl->hdr_size;
278 	}
279 
280 	tl_e = (struct transfer_list_entry *)va;
281 
282 	if (va + sizeof(*tl_e) > tl_ev || tl_e->hdr_size < sizeof(*tl_e) ||
283 	    ADD_OVERFLOW(tl_e->hdr_size, tl_e->data_size, &sz) ||
284 	    ADD_OVERFLOW(va, sz, &ev) || ev > tl_ev)
285 		return NULL;
286 
287 	return tl_e;
288 }
289 
290 /*******************************************************************************
291  * Calculate the byte sum (modulo 256) of a transfer list.
292  * @tl: Pointer to the transfer list.
293  * Return byte sum of the transfer list.
294  ******************************************************************************/
295 static uint8_t calc_byte_sum(const struct transfer_list_header *tl)
296 {
297 	uint8_t *b = (uint8_t *)tl;
298 	uint8_t cs = 0;
299 	size_t n = 0;
300 
301 	if (!tl)
302 		return 0;
303 
304 	for (n = 0; n < tl->size; n++)
305 		cs += b[n];
306 
307 	return cs;
308 }
309 
310 /*******************************************************************************
311  * Update the checksum of a transfer list.
312  * @tl: Pointer to the transfer list.
313  * Return updated checksum of the transfer list.
314  ******************************************************************************/
315 void transfer_list_update_checksum(struct transfer_list_header *tl)
316 {
317 	uint8_t cs = 0;
318 
319 	if (!tl)
320 		return;
321 
322 	cs = calc_byte_sum(tl);
323 	cs -= tl->checksum;
324 	cs = 256 - cs;
325 	tl->checksum = cs;
326 	assert(transfer_list_verify_checksum(tl));
327 }
328 
329 /*******************************************************************************
330  * Verify the checksum of a transfer list.
331  * @tl: Pointer to the transfer list.
332  * Return true if verified or false if not.
333  ******************************************************************************/
334 bool transfer_list_verify_checksum(const struct transfer_list_header *tl)
335 {
336 	return !calc_byte_sum(tl);
337 }
338 
339 /*******************************************************************************
340  * Update the data size of a transfer entry.
341  * @tl: Pointer to the transfer list.
342  * @tl_e: Pointer to the transfer entry.
343  * @new_data_size: New data size of the transfer entry.
344  * Return true on success or false on error.
345  ******************************************************************************/
346 bool transfer_list_set_data_size(struct transfer_list_header *tl,
347 				 struct transfer_list_entry *tl_e,
348 				 uint32_t new_data_size)
349 {
350 	vaddr_t tl_old_ev = 0;
351 	vaddr_t new_ev = 0;
352 	vaddr_t old_ev = 0;
353 	vaddr_t r_new_ev = 0;
354 	struct transfer_list_entry *dummy_te = NULL;
355 	size_t gap = 0;
356 	size_t mov_dis = 0;
357 	size_t sz = 0;
358 
359 	if (!tl || !tl_e)
360 		return false;
361 
362 	tl_old_ev = (vaddr_t)tl + tl->size;
363 
364 	/*
365 	 * Calculate the old and new end of transfer entry
366 	 * both must be roundup to align with TRANSFER_LIST_GRANULE
367 	 */
368 	if (ADD_OVERFLOW(tl_e->hdr_size, tl_e->data_size, &sz) ||
369 	    ADD_OVERFLOW((vaddr_t)tl_e, sz, &old_ev) ||
370 	    ROUNDUP_OVERFLOW(old_ev, TRANSFER_LIST_GRANULE, &old_ev))
371 		return false;
372 
373 	if (ADD_OVERFLOW(tl_e->hdr_size, new_data_size, &sz) ||
374 	    ADD_OVERFLOW((vaddr_t)tl_e, sz, &new_ev) ||
375 	    ROUNDUP_OVERFLOW(new_ev, TRANSFER_LIST_GRANULE, &new_ev))
376 		return false;
377 
378 	if (new_ev > old_ev) {
379 		/*
380 		 * Move distance should be rounded up to match the entry data
381 		 * alignment.
382 		 * Ensure that the increased size doesn't exceed the max size
383 		 * of TL
384 		 */
385 		mov_dis = new_ev - old_ev;
386 		if (ROUNDUP_OVERFLOW(mov_dis,
387 				     TL_ALIGNMENT_FROM_ORDER(tl->alignment),
388 				     &mov_dis) ||
389 		    tl->size + mov_dis > tl->max_size) {
390 			return false;
391 		}
392 		r_new_ev = old_ev + mov_dis;
393 		tl->size += mov_dis;
394 	} else {
395 		/*
396 		 * Move distance should be rounded down to match the entry data
397 		 * alignment.
398 		 */
399 		mov_dis = ROUNDDOWN(old_ev - new_ev,
400 				    TL_ALIGNMENT_FROM_ORDER(tl->alignment));
401 		r_new_ev = old_ev - mov_dis;
402 		tl->size -= mov_dis;
403 	}
404 	/* Move all following entries to fit in the expanded or shrunk space */
405 	memmove((void *)r_new_ev, (void *)old_ev, tl_old_ev - old_ev);
406 
407 	/*
408 	 * Fill the gap due to round up/down with a void entry if the size of
409 	 * the gap is more than an entry header.
410 	 */
411 	gap = r_new_ev - new_ev;
412 	if (gap >= sizeof(*dummy_te)) {
413 		/* Create a dummy transfer entry to fill up the gap */
414 		dummy_te = (struct transfer_list_entry *)new_ev;
415 		dummy_te->tag_id = TL_TAG_EMPTY;
416 		dummy_te->reserved0 = 0;
417 		dummy_te->hdr_size = sizeof(*dummy_te);
418 		dummy_te->data_size = gap - sizeof(*dummy_te);
419 	}
420 
421 	tl_e->data_size = new_data_size;
422 
423 	transfer_list_update_checksum(tl);
424 	return true;
425 }
426 
427 /*******************************************************************************
428  * Remove a specified transfer entry from a transfer list.
429  * @tl: Pointer to the transfer list.
430  * @tl_e: Pointer to the transfer entry.
431  * Return true on success or false on error.
432  ******************************************************************************/
433 bool transfer_list_rem(struct transfer_list_header *tl,
434 		       struct transfer_list_entry *tl_e)
435 {
436 	if (!tl || !tl_e || (vaddr_t)tl_e > (vaddr_t)tl + tl->size)
437 		return false;
438 
439 	tl_e->tag_id = TL_TAG_EMPTY;
440 	tl_e->reserved0 = 0;
441 	transfer_list_update_checksum(tl);
442 	return true;
443 }
444 
445 /*******************************************************************************
446  * Add a new transfer entry into a transfer list.
447  * Compliant with 2.4.3 of Firmware Handoff specification (v0.9).
448  * @tl: Pointer to the transfer list.
449  * @tag_id: Tag ID for the new transfer entry.
450  * @data_size: Data size of the new transfer entry.
451  * @data: Pointer to the data for the new transfer entry.
452  *        NULL to skip data copying.
453  * Return pointer to the added transfer entry or NULL on error.
454  ******************************************************************************/
455 struct transfer_list_entry *transfer_list_add(struct transfer_list_header *tl,
456 					      uint16_t tag_id,
457 					      uint32_t data_size,
458 					      const void *data)
459 {
460 	vaddr_t max_tl_ev = 0;
461 	vaddr_t tl_ev = 0;
462 	vaddr_t ev = 0;
463 	struct transfer_list_entry *tl_e = NULL;
464 	size_t sz = 0;
465 
466 	if (!tl)
467 		return NULL;
468 
469 	max_tl_ev = (vaddr_t)tl + tl->max_size;
470 	tl_ev = (vaddr_t)tl + tl->size;
471 	ev = tl_ev;
472 
473 	/*
474 	 * Skip the step 1 (optional step).
475 	 * New transfer entry will be added into the tail
476 	 */
477 	if (ADD_OVERFLOW(sizeof(*tl_e), data_size, &sz) ||
478 	    ADD_OVERFLOW(ev, sz, &ev) ||
479 	    ROUNDUP_OVERFLOW(ev, TRANSFER_LIST_GRANULE, &ev) ||
480 	    ev > max_tl_ev) {
481 		return NULL;
482 	}
483 
484 	tl_e = (struct transfer_list_entry *)tl_ev;
485 	*tl_e = (struct transfer_list_entry){
486 		.tag_id = tag_id,
487 		.hdr_size = sizeof(*tl_e),
488 		.data_size = data_size,
489 	};
490 
491 	tl->size += ev - tl_ev;
492 
493 	if (data)
494 		memmove(tl_e + tl_e->hdr_size, data, data_size);
495 
496 	transfer_list_update_checksum(tl);
497 
498 	return tl_e;
499 }
500 
501 /*******************************************************************************
502  * Add a new transfer entry into a transfer list with specified new data
503  * alignment requirement.
504  * Compliant with 2.4.4 of Firmware Handoff specification (v0.9).
505  * @tl: Pointer to the transfer list.
506  * @tag_id: Tag ID for the new transfer entry.
507  * @data_size: Data size of the new transfer entry.
508  * @data: Pointer to the data for the new transfer entry.
509  * @alignment: New data alignment specified as a power of two.
510  * Return pointer to the added transfer entry or NULL on error.
511  ******************************************************************************/
512 struct transfer_list_entry *
513 transfer_list_add_with_align(struct transfer_list_header *tl, uint16_t tag_id,
514 			     uint32_t data_size, const void *data,
515 			     uint8_t alignment)
516 {
517 	struct transfer_list_entry *tl_e = NULL;
518 	vaddr_t tl_ev = 0;
519 	vaddr_t ev = 0;
520 	vaddr_t new_tl_ev = 0;
521 	size_t dummy_te_data_sz = 0;
522 
523 	if (!tl)
524 		return NULL;
525 
526 	tl_ev = (vaddr_t)tl + tl->size;
527 	ev = tl_ev + sizeof(struct transfer_list_entry);
528 
529 	if (!IS_ALIGNED(ev, TL_ALIGNMENT_FROM_ORDER(alignment))) {
530 		/*
531 		 * Transfer entry data address is not aligned to the new
532 		 * alignment. Fill the gap with an empty transfer entry as a
533 		 * placeholder before adding the desired transfer entry
534 		 */
535 		new_tl_ev = ROUNDUP(ev, TL_ALIGNMENT_FROM_ORDER(alignment)) -
536 			    sizeof(struct transfer_list_entry);
537 		assert(new_tl_ev - tl_ev > sizeof(struct transfer_list_entry));
538 		dummy_te_data_sz = new_tl_ev - tl_ev -
539 				   sizeof(struct transfer_list_entry);
540 		if (!transfer_list_add(tl, TL_TAG_EMPTY, dummy_te_data_sz,
541 				       NULL)) {
542 			return NULL;
543 		}
544 	}
545 
546 	tl_e = transfer_list_add(tl, tag_id, data_size, data);
547 
548 	if (alignment > tl->alignment) {
549 		tl->alignment = alignment;
550 		transfer_list_update_checksum(tl);
551 	}
552 
553 	return tl_e;
554 }
555 
556 /*******************************************************************************
557  * Search for an existing transfer entry with the specified tag id from a
558  * transfer list.
559  * @tl: Pointer to the transfer list.
560  * @tag_id: Tag ID to match a transfer entry.
561  * Return pointer to the found transfer entry or NULL if not found.
562  ******************************************************************************/
563 struct transfer_list_entry *transfer_list_find(struct transfer_list_header *tl,
564 					       uint16_t tag_id)
565 {
566 	struct transfer_list_entry *tl_e = NULL;
567 
568 	do {
569 		tl_e = transfer_list_next(tl, tl_e);
570 	} while (tl_e && tl_e->tag_id != tag_id);
571 
572 	return tl_e;
573 }
574 
575 /*******************************************************************************
576  * Retrieve the data pointer of a specified transfer entry.
577  * @tl_e: Pointer to the transfer entry.
578  * Return pointer to the transfer entry data or NULL on error.
579  ******************************************************************************/
580 void *transfer_list_entry_data(struct transfer_list_entry *tl_e)
581 {
582 	if (!tl_e)
583 		return NULL;
584 
585 	return (uint8_t *)tl_e + tl_e->hdr_size;
586 }
587