1 /*
2  * Copyright (C) 2015 Mellanox Technologies Ltd.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19 
20 FILE_LICENCE ( GPL2_OR_LATER );
21 
22 #include <stddef.h>
23 #include "../../include/private/mlx_memory_priv.h"
24 #include "../../include/public/mlx_memory.h"
25 
26 mlx_status
mlx_memory_alloc(IN mlx_utils * utils,IN mlx_size size,OUT mlx_void ** ptr)27 mlx_memory_alloc(
28 				IN mlx_utils *utils,
29 				IN mlx_size size,
30 				OUT mlx_void **ptr
31 				)
32 {
33 	mlx_status status = MLX_SUCCESS;
34 	*ptr = NULL;
35 	if ( utils == NULL || size == 0 || *ptr != NULL ){
36 		status = MLX_INVALID_PARAMETER;
37 		goto bad_param;
38 	}
39 	status = mlx_memory_alloc_priv(utils, size, ptr);
40 bad_param:
41 	return status;
42 }
43 
44 mlx_status
mlx_memory_zalloc(IN mlx_utils * utils,IN mlx_size size,OUT mlx_void ** ptr)45 mlx_memory_zalloc(
46 				IN mlx_utils *utils,
47 				IN mlx_size size,
48 				OUT mlx_void **ptr
49 				)
50 {
51 	mlx_status status = MLX_SUCCESS;
52 	*ptr = NULL;
53 	if ( utils == NULL || size == 0 || *ptr != NULL ){
54 		status = MLX_INVALID_PARAMETER;
55 		goto bad_param;
56 	}
57 	status = mlx_memory_zalloc_priv(utils, size, ptr);
58 bad_param:
59 	return status;
60 }
61 
62 mlx_status
mlx_memory_free(IN mlx_utils * utils,IN mlx_void ** ptr)63 mlx_memory_free(
64 				IN mlx_utils *utils,
65 				IN mlx_void **ptr
66 				)
67 {
68 	mlx_status status = MLX_SUCCESS;
69 	if ( utils == NULL ||  ptr == NULL || *ptr == NULL ){
70 		status = MLX_INVALID_PARAMETER;
71 		goto bad_param;
72 	}
73 	status = mlx_memory_free_priv(utils, *ptr);
74 	*ptr = NULL;
75 bad_param:
76 	return status;
77 }
78 mlx_status
mlx_memory_alloc_dma(IN mlx_utils * utils,IN mlx_size size,IN mlx_size align,OUT mlx_void ** ptr)79 mlx_memory_alloc_dma(
80 					IN mlx_utils *utils,
81 					IN mlx_size size ,
82 					IN mlx_size align,
83 					OUT mlx_void **ptr
84 					)
85 {
86 	mlx_status status = MLX_SUCCESS;
87 	*ptr = NULL;
88 	if ( utils == NULL || size == 0 || *ptr != NULL ){
89 		status = MLX_INVALID_PARAMETER;
90 		goto bad_param;
91 	}
92 	status = mlx_memory_alloc_dma_priv(utils, size, align, ptr);
93 bad_param:
94 	return status;
95 }
96 
97 mlx_status
mlx_memory_free_dma(IN mlx_utils * utils,IN mlx_size size,IN mlx_void ** ptr)98 mlx_memory_free_dma(
99 					IN mlx_utils *utils,
100 					IN mlx_size size ,
101 					IN mlx_void **ptr
102 					)
103 {
104 	mlx_status status = MLX_SUCCESS;
105 	if ( utils == NULL || size == 0 || ptr == NULL || *ptr == NULL ){
106 		status = MLX_INVALID_PARAMETER;
107 		goto bad_param;
108 	}
109 	status = mlx_memory_free_dma_priv(utils, size, *ptr);
110 	*ptr = NULL;
111 bad_param:
112 	return status;
113 }
114 
115 mlx_status
mlx_memory_map_dma(IN mlx_utils * utils,IN mlx_void * addr,IN mlx_size number_of_bytes,OUT mlx_physical_address * phys_addr,OUT mlx_void ** mapping)116 mlx_memory_map_dma(
117 					IN mlx_utils *utils,
118 					IN mlx_void *addr ,
119 					IN mlx_size number_of_bytes,
120 					OUT mlx_physical_address *phys_addr,
121 					OUT mlx_void **mapping
122 					)
123 {
124 	mlx_status status = MLX_SUCCESS;
125 	if ( utils == NULL || phys_addr == NULL ){
126 		status = MLX_INVALID_PARAMETER;
127 		goto bad_param;
128 	}
129 	status = mlx_memory_map_dma_priv(utils, addr, number_of_bytes, phys_addr, mapping);
130 bad_param:
131 	return status;
132 }
133 
134 mlx_status
mlx_memory_ummap_dma(IN mlx_utils * utils,IN mlx_void * mapping)135 mlx_memory_ummap_dma(
136 					IN mlx_utils *utils,
137 					IN mlx_void *mapping
138 					)
139 {
140 	mlx_status status = MLX_SUCCESS;
141 	if ( utils == NULL){
142 		status = MLX_INVALID_PARAMETER;
143 		goto bad_param;
144 	}
145 	status = mlx_memory_ummap_dma_priv(utils, mapping);
146 bad_param:
147 	return status;
148 }
149 
150 mlx_status
mlx_memory_cmp(IN mlx_utils * utils,IN mlx_void * first_block,IN mlx_void * second_block,IN mlx_size size,OUT mlx_uint32 * out)151 mlx_memory_cmp(
152 			IN mlx_utils *utils,
153 			IN mlx_void *first_block,
154 			IN mlx_void *second_block,
155 			IN mlx_size size,
156 			OUT mlx_uint32 *out
157 			)
158 {
159 	mlx_status status = MLX_SUCCESS;
160 	if ( utils == NULL || first_block == NULL || second_block == NULL ||
161 			out == NULL){
162 		status = MLX_INVALID_PARAMETER;
163 		goto bad_param;
164 	}
165 	status = mlx_memory_cmp_priv(utils, first_block, second_block, size, out);
166 bad_param:
167 	return status;
168 }
169 
170 mlx_status
mlx_memory_set(IN mlx_utils * utils,IN mlx_void * block,IN mlx_int32 value,IN mlx_size size)171 mlx_memory_set(
172 					IN mlx_utils *utils,
173 					IN mlx_void *block,
174 					IN mlx_int32 value,
175 					IN mlx_size size
176 					)
177 {
178 	mlx_status status = MLX_SUCCESS;
179 	if ( utils == NULL || block == NULL){
180 		status = MLX_INVALID_PARAMETER;
181 		goto bad_param;
182 	}
183 	status = mlx_memory_set_priv(utils, block, value, size);
184 bad_param:
185 	return status;
186 }
187 
188 mlx_status
mlx_memory_cpy(IN mlx_utils * utils,OUT mlx_void * destination_buffer,IN mlx_void * source_buffer,IN mlx_size length)189 mlx_memory_cpy(
190 			IN mlx_utils *utils,
191 			OUT mlx_void *destination_buffer,
192 			IN mlx_void *source_buffer,
193 			IN mlx_size length
194 			)
195 {
196 	mlx_status status = MLX_SUCCESS;
197 	if ( utils == NULL || destination_buffer == NULL || source_buffer == NULL){
198 		status = MLX_INVALID_PARAMETER;
199 		goto bad_param;
200 	}
201 	status = mlx_memory_cpy_priv(utils, destination_buffer, source_buffer, length);
202 bad_param:
203 	return status;
204 }
205 
206 mlx_status
mlx_memory_cpu_to_be32(IN mlx_utils * utils,IN mlx_uint32 source,IN mlx_uint32 * destination)207 mlx_memory_cpu_to_be32(
208 			IN mlx_utils *utils,
209 			IN mlx_uint32 source,
210 			IN mlx_uint32 *destination
211 			)
212 {
213 	mlx_status status = MLX_SUCCESS;
214 	if ( utils == NULL || destination == NULL ){
215 		status = MLX_INVALID_PARAMETER;
216 		goto bad_param;
217 	}
218 	status = mlx_memory_cpu_to_be32_priv(utils, source, destination);
219 bad_param:
220 	return status;
221 }
222 
223 mlx_status
mlx_memory_be32_to_cpu(IN mlx_utils * utils,IN mlx_uint32 source,IN mlx_uint32 * destination)224 mlx_memory_be32_to_cpu(
225 			IN mlx_utils *utils,
226 			IN mlx_uint32 source,
227 			IN mlx_uint32 *destination
228 			)
229 {
230 	mlx_status status = MLX_SUCCESS;
231 	if ( utils == NULL || destination == NULL ){
232 		status = MLX_INVALID_PARAMETER;
233 		goto bad_param;
234 	}
235 	status = mlx_memory_be32_to_cpu_priv(utils, source, destination);
236 bad_param:
237 	return status;
238 }
239