1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright 2025 NXP 4 */ 5 #include <io.h> 6 #include <kernel/tee_misc.h> 7 #include <mm/core_memprot.h> 8 #include <string.h> 9 #include <memutils.h> 10 11 static void imx_ele_buf_cache_op(enum utee_cache_operation op, 12 struct imx_ele_buf *ele_buf) 13 { 14 if (ele_buf && ele_buf->data) 15 cache_operation(op, ele_buf->data, ele_buf->size); 16 } 17 18 TEE_Result imx_ele_buf_alloc(struct imx_ele_buf *ele_buf, const uint8_t *buf, 19 size_t size) 20 { 21 if (!ele_buf || !size) 22 return TEE_ERROR_BAD_PARAMETERS; 23 24 ele_buf->data = alloc_cache_aligned(size); 25 if (!ele_buf->data) { 26 EMSG("buffer allocation failed"); 27 return TEE_ERROR_OUT_OF_MEMORY; 28 } 29 30 ele_buf->paddr = virt_to_phys(ele_buf->data); 31 if (!ele_buf->paddr) { 32 free(ele_buf->data); 33 return TEE_ERROR_OUT_OF_MEMORY; 34 } 35 36 reg_pair_from_64((uint64_t)ele_buf->paddr, &ele_buf->paddr_msb, 37 &ele_buf->paddr_lsb); 38 39 ele_buf->size = size; 40 41 if (buf) 42 memcpy(ele_buf->data, buf, size); 43 44 imx_ele_buf_cache_op(TEE_CACHEFLUSH, ele_buf); 45 46 return TEE_SUCCESS; 47 } 48 49 void imx_ele_buf_free(struct imx_ele_buf *ele_buf) 50 { 51 if (ele_buf) { 52 free(ele_buf->data); 53 ele_buf->data = NULL; 54 ele_buf->paddr = 0; 55 ele_buf->size = 0; 56 } 57 } 58 59 TEE_Result imx_ele_buf_copy(struct imx_ele_buf *ele_buf, uint8_t *buf, 60 size_t size) 61 { 62 if (!ele_buf || !buf || !size) 63 return TEE_ERROR_BAD_PARAMETERS; 64 65 if (size < ele_buf->size) 66 return TEE_ERROR_SHORT_BUFFER; 67 68 imx_ele_buf_cache_op(TEE_CACHEINVALIDATE, ele_buf); 69 memcpy(buf, ele_buf->data, ele_buf->size); 70 71 return TEE_SUCCESS; 72 } 73