1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright 2018-2021 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 defined.
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  * Return the number of Physical Areas used by the buffer @buf.
109  * If @pabufs is not NULL, function fills it with the Physical Areas used
110  * to map the buffer @buf.
111  *
112  * @buf         Data buffer to analyze
113  * @pabufs[out] If not NULL, list the Physical Areas of the @buf
114  *
115  * Returns:
116  * Number of physical area used
117  * (-1) if error
118  */
119 int caam_mem_get_pa_area(struct caambuf *buf, struct caambuf **pabufs);
120 
121 /*
122  * Return if the buffer @buf is cacheable or not
123  *
124  * @buf  Buffer address
125  * @size Buffer size
126  */
127 bool caam_mem_is_cached_buf(void *buf, size_t size);
128 
129 /*
130  * Copy source data into the destination buffer removing non-significant
131  * first zeros (left zeros).
132  * If all source @src buffer is zero, left only one zero in the destination.
133  *
134  * @dst    [out] Destination buffer
135  * @src    Source to copy
136  */
137 void caam_mem_cpy_ltrim_buf(struct caambuf *dst, struct caambuf *src);
138 
139 #endif /* __CAAM_UTILS_MEM_H__ */
140