[657] | 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 | |
---|
[671] | 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 | |
---|
[666] | 44 | ///////////////////////////////////////// |
---|
| 45 | error_t remote_buf_init( xptr_t buf_xp, |
---|
[671] | 46 | uint32_t order ) |
---|
[657] | 47 | { |
---|
[671] | 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 | |
---|
[657] | 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 |
---|
[671] | 59 | if( order >= CONFIG_PPM_PAGE_SHIFT ) // use KMEM_PPM |
---|
[657] | 60 | { |
---|
| 61 | req.type = KMEM_PPM; |
---|
[671] | 62 | req.order = order - CONFIG_PPM_PAGE_SHIFT; |
---|
[657] | 63 | req.flags = AF_NONE; |
---|
| 64 | data = kmem_remote_alloc( buf_cxy , &req ); |
---|
[666] | 65 | |
---|
| 66 | if( data == NULL ) return -1; |
---|
[657] | 67 | } |
---|
[671] | 68 | else // use KMEM_KCM |
---|
[657] | 69 | { |
---|
| 70 | req.type = KMEM_KCM; |
---|
[671] | 71 | req.order = order; |
---|
[657] | 72 | req.flags = AF_NONE; |
---|
| 73 | data = kmem_remote_alloc( buf_cxy , &req ); |
---|
[666] | 74 | |
---|
| 75 | if( data == NULL ) return -1; |
---|
[657] | 76 | } |
---|
| 77 | |
---|
| 78 | // initialize buffer descriptor |
---|
[671] | 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 ); |
---|
[657] | 84 | |
---|
| 85 | return 0; |
---|
| 86 | |
---|
[666] | 87 | } // end remote_buf_init() |
---|
[657] | 88 | |
---|
[671] | 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 | |
---|
[657] | 121 | ///////////////////////////////////////// |
---|
| 122 | void remote_buf_destroy( xptr_t buf_xp ) |
---|
| 123 | { |
---|
[671] | 124 | |
---|
| 125 | assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); |
---|
| 126 | |
---|
[657] | 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 | |
---|
[671] | 132 | // release data buffer |
---|
| 133 | remote_buf_release_data( buf_xp ); |
---|
[666] | 134 | |
---|
[671] | 135 | // release remote_buf descriptor |
---|
| 136 | req.type = KMEM_KCM; |
---|
| 137 | req.ptr = buf_ptr; |
---|
| 138 | kmem_remote_free( buf_cxy , &req ); |
---|
[657] | 139 | |
---|
[671] | 140 | } // end remote_buf_destroy() |
---|
| 141 | |
---|
[657] | 142 | ///////////////////////////////////////// |
---|
| 143 | void remote_buf_reset( xptr_t buf_xp ) |
---|
| 144 | { |
---|
[671] | 145 | |
---|
| 146 | assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); |
---|
| 147 | |
---|
[657] | 148 | remote_buf_t * buf_ptr = GET_PTR( buf_xp ); |
---|
| 149 | cxy_t buf_cxy = GET_CXY( buf_xp ); |
---|
| 150 | |
---|
[671] | 151 | hal_remote_s32( XPTR( buf_cxy , &buf_ptr->wid ) , 0 ); |
---|
| 152 | hal_remote_s32( XPTR( buf_cxy , &buf_ptr->rid ) , 0 ); |
---|
[657] | 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 | { |
---|
[671] | 161 | |
---|
| 162 | assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); |
---|
| 163 | |
---|
[657] | 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 |
---|
[671] | 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 ); |
---|
[657] | 172 | |
---|
| 173 | // get relevant infos from remote buffer descriptor |
---|
| 174 | uint32_t sts = hal_remote_l32( sts_xp ); |
---|
[671] | 175 | uint32_t rid = hal_remote_l32( rid_xp ); |
---|
| 176 | uint32_t order = hal_remote_l32( order_xp ); |
---|
[657] | 177 | uint8_t * data = hal_remote_lpt( data_xp ); |
---|
| 178 | |
---|
[671] | 179 | uint32_t size = 1 << order; |
---|
| 180 | uint32_t mask = size - 1; |
---|
| 181 | |
---|
[657] | 182 | // check enough bytes in buffer |
---|
| 183 | if( nbytes > sts ) return -1; |
---|
| 184 | |
---|
| 185 | // move nbytes |
---|
[671] | 186 | if( (rid + nbytes) <= size) // no wrap around => one move |
---|
[657] | 187 | { |
---|
| 188 | hal_copy_to_uspace( u_buf, |
---|
[671] | 189 | XPTR( buf_cxy , data + rid ), |
---|
[657] | 190 | nbytes ); |
---|
| 191 | } |
---|
| 192 | else // wrap around => two moves |
---|
| 193 | { |
---|
[671] | 194 | uint32_t bytes_1 = size - rid; |
---|
[657] | 195 | uint32_t bytes_2 = nbytes - bytes_1; |
---|
| 196 | |
---|
| 197 | hal_copy_to_uspace( u_buf, |
---|
[671] | 198 | XPTR( buf_cxy , data + rid ), |
---|
[657] | 199 | bytes_1 ); |
---|
| 200 | |
---|
| 201 | hal_copy_to_uspace( u_buf + bytes_1, |
---|
| 202 | XPTR( buf_cxy , data ), |
---|
| 203 | bytes_2 ); |
---|
| 204 | } |
---|
| 205 | |
---|
[671] | 206 | // update rid in buffer descriptor |
---|
| 207 | hal_remote_s32( rid_xp , (rid + nbytes) & mask ); |
---|
[657] | 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 | { |
---|
[671] | 221 | |
---|
| 222 | assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); |
---|
| 223 | |
---|
[657] | 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 |
---|
[671] | 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 ); |
---|
[657] | 232 | |
---|
| 233 | // get relevant infos from remote buffer descriptor |
---|
| 234 | uint32_t sts = hal_remote_l32( sts_xp ); |
---|
[671] | 235 | uint32_t rid = hal_remote_l32( rid_xp ); |
---|
| 236 | uint32_t order = hal_remote_l32( order_xp ); |
---|
[657] | 237 | uint8_t * data = hal_remote_lpt( data_xp ); |
---|
| 238 | |
---|
[671] | 239 | uint32_t size = 1 << order; |
---|
| 240 | uint32_t mask = size - 1; |
---|
| 241 | |
---|
[657] | 242 | // check enough bytes in buffer |
---|
| 243 | if( nbytes > sts ) return -1; |
---|
| 244 | |
---|
| 245 | // move nbytes |
---|
[671] | 246 | if( (rid + nbytes) <= size) // no wrap around => one move |
---|
[657] | 247 | { |
---|
| 248 | hal_remote_memcpy( XPTR( local_cxy , k_buf ), |
---|
[671] | 249 | XPTR( buf_cxy , data + rid ), |
---|
[657] | 250 | nbytes ); |
---|
| 251 | } |
---|
| 252 | else // wrap around => two moves |
---|
| 253 | { |
---|
[671] | 254 | uint32_t bytes_1 = size - rid; |
---|
[657] | 255 | uint32_t bytes_2 = nbytes - bytes_1; |
---|
| 256 | |
---|
| 257 | hal_remote_memcpy( XPTR( local_cxy , k_buf ), |
---|
[671] | 258 | XPTR( buf_cxy , data + rid ), |
---|
[657] | 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 | |
---|
[671] | 266 | // update rid in buffer descriptor |
---|
| 267 | hal_remote_s32( rid_xp , (rid + nbytes) & mask ); |
---|
[657] | 268 | |
---|
| 269 | // atomically update sts |
---|
| 270 | hal_remote_atomic_add( sts_xp , -nbytes ); |
---|
| 271 | |
---|
| 272 | return 0; |
---|
| 273 | |
---|
[671] | 274 | } // end remote_buf_get_to_kernel() |
---|
[657] | 275 | |
---|
| 276 | /////////////////////////////////////////////////// |
---|
| 277 | error_t remote_buf_put_from_user( xptr_t buf_xp, |
---|
| 278 | uint8_t * u_buf, |
---|
| 279 | uint32_t nbytes ) |
---|
| 280 | { |
---|
[671] | 281 | |
---|
| 282 | assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); |
---|
| 283 | |
---|
[657] | 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 |
---|
[671] | 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 ); |
---|
[657] | 292 | |
---|
| 293 | // get relevant infos from remote buffer descriptor |
---|
| 294 | uint32_t sts = hal_remote_l32( sts_xp ); |
---|
[671] | 295 | uint32_t wid = hal_remote_l32( wid_xp ); |
---|
| 296 | uint32_t order = hal_remote_l32( order_xp ); |
---|
[657] | 297 | uint8_t * data = hal_remote_lpt( data_xp ); |
---|
| 298 | |
---|
[671] | 299 | uint32_t size = 1 << order; |
---|
| 300 | uint32_t mask = size - 1; |
---|
| 301 | |
---|
[657] | 302 | // check enough space in buffer |
---|
| 303 | if( nbytes > (size - sts) ) return -1; |
---|
| 304 | |
---|
| 305 | // move nbytes |
---|
[671] | 306 | if( (wid + nbytes) <= size) // no wrap around => one move |
---|
[657] | 307 | { |
---|
[671] | 308 | hal_copy_from_uspace( XPTR( buf_cxy , data + wid ), |
---|
[657] | 309 | u_buf, |
---|
| 310 | nbytes ); |
---|
| 311 | } |
---|
| 312 | else // wrap around => two moves |
---|
| 313 | { |
---|
[671] | 314 | uint32_t bytes_1 = size - wid; |
---|
[657] | 315 | uint32_t bytes_2 = nbytes - bytes_1; |
---|
| 316 | |
---|
[671] | 317 | hal_copy_from_uspace( XPTR( buf_cxy , data + wid ), |
---|
[657] | 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 | |
---|
[671] | 326 | // update wid in buffer descriptor |
---|
| 327 | hal_remote_s32( wid_xp , (wid + nbytes) & mask ); |
---|
[657] | 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 | { |
---|
[671] | 341 | |
---|
| 342 | assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); |
---|
| 343 | |
---|
[657] | 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 |
---|
[671] | 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 ); |
---|
[657] | 352 | |
---|
| 353 | // get relevant infos from remote buffer descriptor |
---|
| 354 | uint32_t sts = hal_remote_l32( sts_xp ); |
---|
[671] | 355 | uint32_t wid = hal_remote_l32( wid_xp ); |
---|
| 356 | uint32_t order = hal_remote_l32( order_xp ); |
---|
[657] | 357 | uint8_t * data = hal_remote_lpt( data_xp ); |
---|
| 358 | |
---|
[671] | 359 | uint32_t size = 1 << order; |
---|
| 360 | uint32_t mask = size - 1; |
---|
| 361 | |
---|
[657] | 362 | // check enough space in buffer |
---|
| 363 | if( nbytes > (size - sts) ) return -1; |
---|
| 364 | |
---|
| 365 | // move nbytes |
---|
[671] | 366 | if( (wid + nbytes) <= size) // no wrap around => one move |
---|
[657] | 367 | { |
---|
[671] | 368 | hal_remote_memcpy( XPTR( buf_cxy , data + wid ), |
---|
[657] | 369 | XPTR( local_cxy , k_buf ), |
---|
| 370 | nbytes ); |
---|
| 371 | } |
---|
| 372 | else // wrap around => two moves |
---|
| 373 | { |
---|
[671] | 374 | uint32_t bytes_1 = size - wid; |
---|
[657] | 375 | uint32_t bytes_2 = nbytes - bytes_1; |
---|
| 376 | |
---|
[671] | 377 | hal_remote_memcpy( XPTR( buf_cxy , data + wid ), |
---|
[657] | 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 | |
---|
[671] | 386 | // update wid in buffer descriptor |
---|
| 387 | hal_remote_s32( wid_xp , (wid + nbytes) & mask ); |
---|
[657] | 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 | |
---|