1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright 2018-2021, 2024 NXP 4 * 5 * Brief Memory management utilities. 6 * Primitive to allocate, free memory. 7 */ 8 9 #ifndef __CAAM_UTILS_MEM_H__ 10 #define __CAAM_UTILS_MEM_H__ 11 12 #include <caam_common.h> 13 14 /* 15 * Allocate normal memory. 16 * 17 * @size size in bytes of the memory to allocate 18 */ 19 void *caam_alloc(size_t size); 20 21 /* 22 * Allocate normal memory and initialize it to 0's. 23 * 24 * @size size in bytes of the memory to allocate 25 */ 26 void *caam_calloc(size_t size); 27 28 /* 29 * Allocate memory aligned with a cache line and initialize it to 0's. 30 * 31 * @size size in bytes of the memory to allocate 32 */ 33 void *caam_calloc_align(size_t size); 34 35 /* 36 * Free allocated memory 37 * 38 * @ptr reference to the object to free 39 */ 40 void caam_free(void *ptr); 41 42 /* 43 * Allocate Job descriptor and initialize it to 0's. 44 * 45 * @nbentries Number of descriptor entries 46 */ 47 uint32_t *caam_calloc_desc(uint8_t nbentries); 48 49 /* 50 * Free descriptor 51 * 52 * @ptr Reference to the descriptor to free 53 */ 54 void caam_free_desc(uint32_t **ptr); 55 56 /* 57 * Allocate internal driver buffer and initialize it with 0s. 58 * 59 * @buf [out] buffer allocated 60 * @size size in bytes of the memory to allocate 61 */ 62 enum caam_status caam_calloc_buf(struct caambuf *buf, size_t size); 63 64 /* 65 * Allocate internal driver buffer. 66 * 67 * @buf [out] buffer allocated 68 * @size size in bytes of the memory to allocate 69 */ 70 enum caam_status caam_alloc_buf(struct caambuf *buf, size_t size); 71 72 /* 73 * Allocate internal driver buffer aligned with a cache line and initialize 74 * if with 0's. 75 * 76 * @buf [out] buffer allocated 77 * @size size in bytes of the memory to allocate 78 */ 79 enum caam_status caam_calloc_align_buf(struct caambuf *buf, size_t size); 80 81 /* 82 * Allocate internal driver buffer aligned with a cache line. 83 * 84 * @buf [out] buffer allocated 85 * @size size in bytes of the memory to allocate 86 */ 87 enum caam_status caam_alloc_align_buf(struct caambuf *buf, size_t size); 88 89 /* 90 * Free internal driver buffer allocated memory 91 * 92 * @buf Driver buffer to free 93 */ 94 void caam_free_buf(struct caambuf *buf); 95 96 /* 97 * Copy source data into the block buffer. Allocate block buffer if 98 * it's not already allocated. 99 * 100 * @block [in/out] Block buffer information. Return buffer filled. 101 * @src Source to copy 102 * @offset Source offset to start 103 */ 104 enum caam_status caam_cpy_block_src(struct caamblock *block, 105 struct caambuf *src, size_t offset); 106 107 /* 108 * Copy data into the buffer. Allocate buffer if it's not already allocated. 109 * 110 * @dst [out] Destination data to allocate and fill 111 * @src_data Source to copy 112 * @src_length Length to copy 113 */ 114 enum caam_status caam_cpy_buf(struct caambuf *dst, uint8_t *src_data, 115 size_t src_length); 116 117 /* 118 * Return the number of Physical Areas used by the buffer @buf. 119 * If @pabufs is not NULL, function fills it with the Physical Areas used 120 * to map the buffer @buf. 121 * 122 * @buf Data buffer to analyze 123 * @pabufs[out] If not NULL, list the Physical Areas of the @buf 124 * 125 * Returns: 126 * Number of physical area used 127 * (-1) if error 128 */ 129 int caam_mem_get_pa_area(struct caambuf *buf, struct caambuf **pabufs); 130 131 /* 132 * Return if the buffer @buf is cacheable or not 133 * 134 * @buf Buffer address 135 * @size Buffer size 136 */ 137 bool caam_mem_is_cached_buf(void *buf, size_t size); 138 139 /* 140 * Copy source data into the destination buffer removing non-significant 141 * first zeros (left zeros). 142 * If all source @src buffer is zero, left only one zero in the destination. 143 * 144 * @dst [out] Destination buffer 145 * @src Source to copy 146 */ 147 void caam_mem_cpy_ltrim_buf(struct caambuf *dst, struct caambuf *src); 148 149 #endif /* __CAAM_UTILS_MEM_H__ */ 150