1 | /* |
---|
2 | * remote_buf.c Remotely accessible, circular buffer implementation. |
---|
3 | * |
---|
4 | * Authors : Alain Greiner (2016,2017,2018,2019,2020) |
---|
5 | * |
---|
6 | * Copyright (c) UPMC Sorbonne Universites |
---|
7 | * |
---|
8 | * This file is part of ALMOS-MKH. |
---|
9 | * |
---|
10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
11 | * under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation; version 2.0 of the License. |
---|
13 | * |
---|
14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | * General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #include <hal_kernel_types.h> |
---|
25 | #include <hal_irqmask.h> |
---|
26 | #include <hal_remote.h> |
---|
27 | #include <hal_uspace.h> |
---|
28 | #include <bits.h> |
---|
29 | #include <memcpy.h> |
---|
30 | #include <kmem.h> |
---|
31 | #include <remote_buf.h> |
---|
32 | |
---|
33 | ///////////////////////////////////////////// |
---|
34 | remote_buf_t * remote_buf_alloc( cxy_t cxy ) |
---|
35 | { |
---|
36 | kmem_req_t req; |
---|
37 | |
---|
38 | req.type = KMEM_KCM; |
---|
39 | req.order = bits_log2( sizeof(remote_buf_t) ); |
---|
40 | req.flags = AF_ZERO; |
---|
41 | return kmem_remote_alloc( cxy , &req ); |
---|
42 | } |
---|
43 | |
---|
44 | ///////////////////////////////////////// |
---|
45 | error_t remote_buf_init( xptr_t buf_xp, |
---|
46 | uint32_t order ) |
---|
47 | { |
---|
48 | |
---|
49 | assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); |
---|
50 | assert( __FUNCTION__ , (order < 32) , "order cannot be larger than 31" ); |
---|
51 | |
---|
52 | kmem_req_t req; |
---|
53 | uint8_t * data; |
---|
54 | |
---|
55 | remote_buf_t * buf_ptr = GET_PTR( buf_xp ); |
---|
56 | cxy_t buf_cxy = GET_CXY( buf_xp ); |
---|
57 | |
---|
58 | // allocate the data buffer |
---|
59 | if( order >= CONFIG_PPM_PAGE_SHIFT ) // use KMEM_PPM |
---|
60 | { |
---|
61 | req.type = KMEM_PPM; |
---|
62 | req.order = order - CONFIG_PPM_PAGE_SHIFT; |
---|
63 | req.flags = AF_NONE; |
---|
64 | data = kmem_remote_alloc( buf_cxy , &req ); |
---|
65 | |
---|
66 | if( data == NULL ) return -1; |
---|
67 | } |
---|
68 | else // use KMEM_KCM |
---|
69 | { |
---|
70 | req.type = KMEM_KCM; |
---|
71 | req.order = order; |
---|
72 | req.flags = AF_NONE; |
---|
73 | data = kmem_remote_alloc( buf_cxy , &req ); |
---|
74 | |
---|
75 | if( data == NULL ) return -1; |
---|
76 | } |
---|
77 | |
---|
78 | // initialize buffer descriptor |
---|
79 | hal_remote_s32( XPTR( buf_cxy , &buf_ptr->order ) , order ); |
---|
80 | hal_remote_s32( XPTR( buf_cxy , &buf_ptr->wid ) , 0 ); |
---|
81 | hal_remote_s32( XPTR( buf_cxy , &buf_ptr->rid ) , 0 ); |
---|
82 | hal_remote_s32( XPTR( buf_cxy , &buf_ptr->sts ) , 0 ); |
---|
83 | hal_remote_spt( XPTR( buf_cxy , &buf_ptr->data ) , data ); |
---|
84 | |
---|
85 | return 0; |
---|
86 | |
---|
87 | } // end remote_buf_init() |
---|
88 | |
---|
89 | ////////////////////////////////////////////// |
---|
90 | void remote_buf_release_data( xptr_t buf_xp ) |
---|
91 | { |
---|
92 | kmem_req_t req; |
---|
93 | |
---|
94 | assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); |
---|
95 | |
---|
96 | remote_buf_t * buf_ptr = GET_PTR( buf_xp ); |
---|
97 | cxy_t buf_cxy = GET_CXY( buf_xp ); |
---|
98 | |
---|
99 | // gets data buffer local pointer and order |
---|
100 | uint32_t order = hal_remote_l32( XPTR( buf_cxy , &buf_ptr->order )); |
---|
101 | char * data_ptr = hal_remote_lpt( XPTR( buf_cxy , &buf_ptr->data )); |
---|
102 | |
---|
103 | // release memory allocated for data buffer if required |
---|
104 | if( data_ptr != NULL ) |
---|
105 | { |
---|
106 | if( order >= CONFIG_PPM_PAGE_SHIFT ) // use KMEM_PPM |
---|
107 | { |
---|
108 | req.type = KMEM_PPM; |
---|
109 | req.ptr = data_ptr; |
---|
110 | kmem_remote_free( buf_cxy , &req ); |
---|
111 | } |
---|
112 | else // use KMEM_KCM |
---|
113 | { |
---|
114 | req.type = KMEM_KCM; |
---|
115 | req.ptr = data_ptr; |
---|
116 | kmem_remote_free( buf_cxy , &req ); |
---|
117 | } |
---|
118 | } |
---|
119 | } // end remote_buf_release_data() |
---|
120 | |
---|
121 | ///////////////////////////////////////// |
---|
122 | void remote_buf_destroy( xptr_t buf_xp ) |
---|
123 | { |
---|
124 | |
---|
125 | assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); |
---|
126 | |
---|
127 | kmem_req_t req; |
---|
128 | |
---|
129 | remote_buf_t * buf_ptr = GET_PTR( buf_xp ); |
---|
130 | cxy_t buf_cxy = GET_CXY( buf_xp ); |
---|
131 | |
---|
132 | // release data buffer |
---|
133 | remote_buf_release_data( buf_xp ); |
---|
134 | |
---|
135 | // release remote_buf descriptor |
---|
136 | req.type = KMEM_KCM; |
---|
137 | req.ptr = buf_ptr; |
---|
138 | kmem_remote_free( buf_cxy , &req ); |
---|
139 | |
---|
140 | } // end remote_buf_destroy() |
---|
141 | |
---|
142 | ///////////////////////////////////////// |
---|
143 | void remote_buf_reset( xptr_t buf_xp ) |
---|
144 | { |
---|
145 | |
---|
146 | assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); |
---|
147 | |
---|
148 | remote_buf_t * buf_ptr = GET_PTR( buf_xp ); |
---|
149 | cxy_t buf_cxy = GET_CXY( buf_xp ); |
---|
150 | |
---|
151 | hal_remote_s32( XPTR( buf_cxy , &buf_ptr->wid ) , 0 ); |
---|
152 | hal_remote_s32( XPTR( buf_cxy , &buf_ptr->rid ) , 0 ); |
---|
153 | hal_remote_s32( XPTR( buf_cxy , &buf_ptr->sts ) , 0 ); |
---|
154 | } |
---|
155 | |
---|
156 | ///////////////////////////////////////////////// |
---|
157 | error_t remote_buf_get_to_user( xptr_t buf_xp, |
---|
158 | uint8_t * u_buf, |
---|
159 | uint32_t nbytes ) |
---|
160 | { |
---|
161 | |
---|
162 | assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); |
---|
163 | |
---|
164 | remote_buf_t * buf_ptr = GET_PTR( buf_xp ); |
---|
165 | cxy_t buf_cxy = GET_CXY( buf_xp ); |
---|
166 | |
---|
167 | // build relevant extended pointers |
---|
168 | xptr_t sts_xp = XPTR( buf_cxy , &buf_ptr->sts ); |
---|
169 | xptr_t rid_xp = XPTR( buf_cxy , &buf_ptr->rid ); |
---|
170 | xptr_t order_xp = XPTR( buf_cxy , &buf_ptr->order ); |
---|
171 | xptr_t data_xp = XPTR( buf_cxy , &buf_ptr->data ); |
---|
172 | |
---|
173 | // get relevant infos from remote buffer descriptor |
---|
174 | uint32_t sts = hal_remote_l32( sts_xp ); |
---|
175 | uint32_t rid = hal_remote_l32( rid_xp ); |
---|
176 | uint32_t order = hal_remote_l32( order_xp ); |
---|
177 | uint8_t * data = hal_remote_lpt( data_xp ); |
---|
178 | |
---|
179 | uint32_t size = 1 << order; |
---|
180 | uint32_t mask = size - 1; |
---|
181 | |
---|
182 | // check enough bytes in buffer |
---|
183 | if( nbytes > sts ) return -1; |
---|
184 | |
---|
185 | // move nbytes |
---|
186 | if( (rid + nbytes) <= size) // no wrap around => one move |
---|
187 | { |
---|
188 | hal_copy_to_uspace( u_buf, |
---|
189 | XPTR( buf_cxy , data + rid ), |
---|
190 | nbytes ); |
---|
191 | } |
---|
192 | else // wrap around => two moves |
---|
193 | { |
---|
194 | uint32_t bytes_1 = size - rid; |
---|
195 | uint32_t bytes_2 = nbytes - bytes_1; |
---|
196 | |
---|
197 | hal_copy_to_uspace( u_buf, |
---|
198 | XPTR( buf_cxy , data + rid ), |
---|
199 | bytes_1 ); |
---|
200 | |
---|
201 | hal_copy_to_uspace( u_buf + bytes_1, |
---|
202 | XPTR( buf_cxy , data ), |
---|
203 | bytes_2 ); |
---|
204 | } |
---|
205 | |
---|
206 | // update rid in buffer descriptor |
---|
207 | hal_remote_s32( rid_xp , (rid + nbytes) & mask ); |
---|
208 | |
---|
209 | // atomically update sts |
---|
210 | hal_remote_atomic_add( sts_xp , -nbytes ); |
---|
211 | |
---|
212 | return 0; |
---|
213 | |
---|
214 | } // end remote_buf_get_to_user() |
---|
215 | |
---|
216 | /////////////////////////////////////////////////// |
---|
217 | error_t remote_buf_get_to_kernel( xptr_t buf_xp, |
---|
218 | uint8_t * k_buf, |
---|
219 | uint32_t nbytes ) |
---|
220 | { |
---|
221 | |
---|
222 | assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); |
---|
223 | |
---|
224 | remote_buf_t * buf_ptr = GET_PTR( buf_xp ); |
---|
225 | cxy_t buf_cxy = GET_CXY( buf_xp ); |
---|
226 | |
---|
227 | // build relevant extended pointers |
---|
228 | xptr_t sts_xp = XPTR( buf_cxy , &buf_ptr->sts ); |
---|
229 | xptr_t rid_xp = XPTR( buf_cxy , &buf_ptr->rid ); |
---|
230 | xptr_t order_xp = XPTR( buf_cxy , &buf_ptr->order ); |
---|
231 | xptr_t data_xp = XPTR( buf_cxy , &buf_ptr->data ); |
---|
232 | |
---|
233 | // get relevant infos from remote buffer descriptor |
---|
234 | uint32_t sts = hal_remote_l32( sts_xp ); |
---|
235 | uint32_t rid = hal_remote_l32( rid_xp ); |
---|
236 | uint32_t order = hal_remote_l32( order_xp ); |
---|
237 | uint8_t * data = hal_remote_lpt( data_xp ); |
---|
238 | |
---|
239 | uint32_t size = 1 << order; |
---|
240 | uint32_t mask = size - 1; |
---|
241 | |
---|
242 | // check enough bytes in buffer |
---|
243 | if( nbytes > sts ) return -1; |
---|
244 | |
---|
245 | // move nbytes |
---|
246 | if( (rid + nbytes) <= size) // no wrap around => one move |
---|
247 | { |
---|
248 | hal_remote_memcpy( XPTR( local_cxy , k_buf ), |
---|
249 | XPTR( buf_cxy , data + rid ), |
---|
250 | nbytes ); |
---|
251 | } |
---|
252 | else // wrap around => two moves |
---|
253 | { |
---|
254 | uint32_t bytes_1 = size - rid; |
---|
255 | uint32_t bytes_2 = nbytes - bytes_1; |
---|
256 | |
---|
257 | hal_remote_memcpy( XPTR( local_cxy , k_buf ), |
---|
258 | XPTR( buf_cxy , data + rid ), |
---|
259 | bytes_1 ); |
---|
260 | |
---|
261 | hal_remote_memcpy( XPTR( local_cxy , k_buf + bytes_1 ), |
---|
262 | XPTR( buf_cxy , data ), |
---|
263 | bytes_2 ); |
---|
264 | } |
---|
265 | |
---|
266 | // update rid in buffer descriptor |
---|
267 | hal_remote_s32( rid_xp , (rid + nbytes) & mask ); |
---|
268 | |
---|
269 | // atomically update sts |
---|
270 | hal_remote_atomic_add( sts_xp , -nbytes ); |
---|
271 | |
---|
272 | return 0; |
---|
273 | |
---|
274 | } // end remote_buf_get_to_kernel() |
---|
275 | |
---|
276 | /////////////////////////////////////////////////// |
---|
277 | error_t remote_buf_put_from_user( xptr_t buf_xp, |
---|
278 | uint8_t * u_buf, |
---|
279 | uint32_t nbytes ) |
---|
280 | { |
---|
281 | |
---|
282 | assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); |
---|
283 | |
---|
284 | remote_buf_t * buf_ptr = GET_PTR( buf_xp ); |
---|
285 | cxy_t buf_cxy = GET_CXY( buf_xp ); |
---|
286 | |
---|
287 | // build relevant extended pointers |
---|
288 | xptr_t sts_xp = XPTR( buf_cxy , &buf_ptr->sts ); |
---|
289 | xptr_t wid_xp = XPTR( buf_cxy , &buf_ptr->wid ); |
---|
290 | xptr_t order_xp = XPTR( buf_cxy , &buf_ptr->order ); |
---|
291 | xptr_t data_xp = XPTR( buf_cxy , &buf_ptr->data ); |
---|
292 | |
---|
293 | // get relevant infos from remote buffer descriptor |
---|
294 | uint32_t sts = hal_remote_l32( sts_xp ); |
---|
295 | uint32_t wid = hal_remote_l32( wid_xp ); |
---|
296 | uint32_t order = hal_remote_l32( order_xp ); |
---|
297 | uint8_t * data = hal_remote_lpt( data_xp ); |
---|
298 | |
---|
299 | uint32_t size = 1 << order; |
---|
300 | uint32_t mask = size - 1; |
---|
301 | |
---|
302 | // check enough space in buffer |
---|
303 | if( nbytes > (size - sts) ) return -1; |
---|
304 | |
---|
305 | // move nbytes |
---|
306 | if( (wid + nbytes) <= size) // no wrap around => one move |
---|
307 | { |
---|
308 | hal_copy_from_uspace( XPTR( buf_cxy , data + wid ), |
---|
309 | u_buf, |
---|
310 | nbytes ); |
---|
311 | } |
---|
312 | else // wrap around => two moves |
---|
313 | { |
---|
314 | uint32_t bytes_1 = size - wid; |
---|
315 | uint32_t bytes_2 = nbytes - bytes_1; |
---|
316 | |
---|
317 | hal_copy_from_uspace( XPTR( buf_cxy , data + wid ), |
---|
318 | u_buf, |
---|
319 | bytes_1 ); |
---|
320 | |
---|
321 | hal_copy_from_uspace( XPTR( buf_cxy , data ), |
---|
322 | u_buf + bytes_1, |
---|
323 | bytes_2 ); |
---|
324 | } |
---|
325 | |
---|
326 | // update wid in buffer descriptor |
---|
327 | hal_remote_s32( wid_xp , (wid + nbytes) & mask ); |
---|
328 | |
---|
329 | // atomically update sts |
---|
330 | hal_remote_atomic_add( sts_xp , nbytes ); |
---|
331 | |
---|
332 | return 0; |
---|
333 | |
---|
334 | } // end remote_buf_put_from_user() |
---|
335 | |
---|
336 | ///////////////////////////////////////////////////// |
---|
337 | error_t remote_buf_put_from_kernel( xptr_t buf_xp, |
---|
338 | uint8_t * k_buf, |
---|
339 | uint32_t nbytes ) |
---|
340 | { |
---|
341 | |
---|
342 | assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); |
---|
343 | |
---|
344 | remote_buf_t * buf_ptr = GET_PTR( buf_xp ); |
---|
345 | cxy_t buf_cxy = GET_CXY( buf_xp ); |
---|
346 | |
---|
347 | // build relevant extended pointers |
---|
348 | xptr_t sts_xp = XPTR( buf_cxy , &buf_ptr->sts ); |
---|
349 | xptr_t wid_xp = XPTR( buf_cxy , &buf_ptr->wid ); |
---|
350 | xptr_t order_xp = XPTR( buf_cxy , &buf_ptr->order ); |
---|
351 | xptr_t data_xp = XPTR( buf_cxy , &buf_ptr->data ); |
---|
352 | |
---|
353 | // get relevant infos from remote buffer descriptor |
---|
354 | uint32_t sts = hal_remote_l32( sts_xp ); |
---|
355 | uint32_t wid = hal_remote_l32( wid_xp ); |
---|
356 | uint32_t order = hal_remote_l32( order_xp ); |
---|
357 | uint8_t * data = hal_remote_lpt( data_xp ); |
---|
358 | |
---|
359 | uint32_t size = 1 << order; |
---|
360 | uint32_t mask = size - 1; |
---|
361 | |
---|
362 | // check enough space in buffer |
---|
363 | if( nbytes > (size - sts) ) return -1; |
---|
364 | |
---|
365 | // move nbytes |
---|
366 | if( (wid + nbytes) <= size) // no wrap around => one move |
---|
367 | { |
---|
368 | hal_remote_memcpy( XPTR( buf_cxy , data + wid ), |
---|
369 | XPTR( local_cxy , k_buf ), |
---|
370 | nbytes ); |
---|
371 | } |
---|
372 | else // wrap around => two moves |
---|
373 | { |
---|
374 | uint32_t bytes_1 = size - wid; |
---|
375 | uint32_t bytes_2 = nbytes - bytes_1; |
---|
376 | |
---|
377 | hal_remote_memcpy( XPTR( buf_cxy , data + wid ), |
---|
378 | XPTR( local_cxy , k_buf ), |
---|
379 | bytes_1 ); |
---|
380 | |
---|
381 | hal_remote_memcpy( XPTR( buf_cxy , data ), |
---|
382 | XPTR( local_cxy , k_buf + bytes_1 ), |
---|
383 | bytes_2 ); |
---|
384 | } |
---|
385 | |
---|
386 | // update wid in buffer descriptor |
---|
387 | hal_remote_s32( wid_xp , (wid + nbytes) & mask ); |
---|
388 | |
---|
389 | // atomically update sts |
---|
390 | hal_remote_atomic_add( sts_xp , nbytes ); |
---|
391 | |
---|
392 | return 0; |
---|
393 | |
---|
394 | } // end remote_buf_put_from_kernel() |
---|
395 | |
---|
396 | //////////////////////////////////////////// |
---|
397 | uint32_t remote_buf_status( xptr_t buf_xp ) |
---|
398 | { |
---|
399 | remote_buf_t * buf_ptr = GET_PTR( buf_xp ); |
---|
400 | cxy_t buf_cxy = GET_CXY( buf_xp ); |
---|
401 | |
---|
402 | return hal_remote_l32( XPTR( buf_cxy , &buf_ptr->sts ) ); |
---|
403 | |
---|
404 | } // end remote_buf_status() |
---|
405 | |
---|
406 | |
---|