Ignore:
Timestamp:
Nov 19, 2020, 11:47:00 PM (3 years ago)
Author:
alain
Message:

Cosmetic.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/libk/remote_buf.c

    r666 r671  
    3131#include <remote_buf.h>
    3232
     33/////////////////////////////////////////////
     34remote_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
    3344/////////////////////////////////////////
    3445error_t remote_buf_init( xptr_t   buf_xp,
    35                          uint32_t size )
    36 {
     46                         uint32_t order )
     47{
     48
     49assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" );
     50assert( __FUNCTION__ , (order < 32) , "order cannot be larger than 31" );
     51
    3752    kmem_req_t     req;
    3853    uint8_t      * data;
     
    4257
    4358    // allocate the data buffer
    44     if( size == 0 )
    45     {
    46         data = NULL;
    47     }
    48     else if( size >= CONFIG_PPM_PAGE_SIZE )
     59    if( order >= CONFIG_PPM_PAGE_SHIFT )  // use KMEM_PPM
    4960    {
    5061        req.type  = KMEM_PPM;
    51         req.order = bits_log2( size >> CONFIG_PPM_PAGE_SHIFT );
     62        req.order = order - CONFIG_PPM_PAGE_SHIFT;
    5263        req.flags = AF_NONE;
    5364        data = kmem_remote_alloc( buf_cxy , &req );
     
    5566        if( data == NULL )  return -1;
    5667    }
    57     else
     68    else                                     // use KMEM_KCM
    5869    {
    5970        req.type  = KMEM_KCM;
    60         req.order = bits_log2( size );
     71        req.order = order;
    6172        req.flags = AF_NONE;
    6273        data = kmem_remote_alloc( buf_cxy , &req );
     
    6677
    6778    // initialize buffer descriptor
    68     hal_remote_s32( XPTR( buf_cxy , &buf_ptr->size ) , size );
    69     hal_remote_s32( XPTR( buf_cxy , &buf_ptr->ptw  ) , 0 );
    70     hal_remote_s32( XPTR( buf_cxy , &buf_ptr->ptr  ) , 0 );
    71     hal_remote_s32( XPTR( buf_cxy , &buf_ptr->sts  ) , 0 );
    72     hal_remote_spt( XPTR( buf_cxy , &buf_ptr->data ) , data );
     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 );
    7384
    7485    return 0;
    7586
    7687}  // end remote_buf_init()
     88
     89//////////////////////////////////////////////
     90void remote_buf_release_data( xptr_t  buf_xp )
     91{
     92    kmem_req_t     req;
     93
     94assert( __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()
    77120
    78121/////////////////////////////////////////
    79122void remote_buf_destroy( xptr_t  buf_xp )
    80123{
     124
     125assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" );
     126
    81127    kmem_req_t   req;
    82128
     
    84130    cxy_t          buf_cxy = GET_CXY( buf_xp );
    85131
    86     uint32_t size = hal_remote_l32( XPTR( buf_cxy , &buf_ptr->size ));
    87 
    88     // release memory allocated to data buffer if required
    89     if( size == 0 )
    90     {
    91         return;
    92     }
    93     else if( size >= CONFIG_PPM_PAGE_SIZE )
    94     {
    95         req.type  = KMEM_PPM;
    96         req.ptr   = hal_remote_lpt( XPTR( buf_cxy , &buf_ptr->data ) );
    97         kmem_remote_free( buf_cxy , &req );
    98     }
    99     else
    100     {
    101         req.type  = KMEM_KCM;
    102         req.ptr   = hal_remote_lpt( XPTR( buf_cxy , &buf_ptr->data ) );
    103         kmem_remote_free( buf_cxy , &req );
    104     }
    105 }
     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()
    106141
    107142/////////////////////////////////////////
    108143void remote_buf_reset( xptr_t    buf_xp )
    109144{
    110     remote_buf_t * buf_ptr = GET_PTR( buf_xp );
    111     cxy_t          buf_cxy = GET_CXY( buf_xp );
    112 
    113     hal_remote_s32( XPTR( buf_cxy , &buf_ptr->ptw ) , 0 );
    114     hal_remote_s32( XPTR( buf_cxy , &buf_ptr->ptr ) , 0 );
     145
     146assert( __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 );
    115153    hal_remote_s32( XPTR( buf_cxy , &buf_ptr->sts ) , 0 );
    116154}
     
    121159                                uint32_t  nbytes )
    122160{
     161
     162assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" );
     163
    123164    remote_buf_t * buf_ptr = GET_PTR( buf_xp );
    124165    cxy_t          buf_cxy = GET_CXY( buf_xp );
    125166
    126167    // build relevant extended pointers
    127     xptr_t sts_xp  = XPTR( buf_cxy , &buf_ptr->sts );
    128     xptr_t ptr_xp  = XPTR( buf_cxy , &buf_ptr->ptr );
    129     xptr_t size_xp = XPTR( buf_cxy , &buf_ptr->size );
    130     xptr_t data_xp = XPTR( buf_cxy , &buf_ptr->data );
     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 );
    131172
    132173    // get relevant infos from remote buffer descriptor
    133174    uint32_t  sts  = hal_remote_l32( sts_xp );
    134     uint32_t  ptr  = hal_remote_l32( ptr_xp );
    135     uint32_t  size = hal_remote_l32( size_xp );
     175    uint32_t  rid  = hal_remote_l32( rid_xp );
     176    uint32_t  order = hal_remote_l32( order_xp );
    136177    uint8_t * data = hal_remote_lpt( data_xp );
     178
     179    uint32_t  size = 1 << order;
     180    uint32_t  mask = size - 1;
    137181
    138182    // check enough bytes in buffer
     
    140184
    141185    // move nbytes
    142     if( (ptr + nbytes) <= size)  // no wrap around => one move
     186    if( (rid + nbytes) <= size)  // no wrap around => one move
    143187    {
    144188        hal_copy_to_uspace( u_buf,
    145                             XPTR( buf_cxy , data + ptr ),
     189                            XPTR( buf_cxy , data + rid ),
    146190                            nbytes );
    147191    }
    148192    else                         // wrap around => two moves
    149193    {
    150         uint32_t bytes_1 = size - ptr;
     194        uint32_t bytes_1 = size - rid;
    151195        uint32_t bytes_2 = nbytes - bytes_1;
    152196     
    153197        hal_copy_to_uspace( u_buf,
    154                             XPTR( buf_cxy , data + ptr ),
     198                            XPTR( buf_cxy , data + rid ),
    155199                            bytes_1 );
    156200
     
    160204    }
    161205
    162     // update ptr in buffer descriptor
    163     hal_remote_s32( ptr_xp , (ptr + nbytes) % size );
     206    // update rid in buffer descriptor
     207    hal_remote_s32( rid_xp , (rid + nbytes) & mask );
    164208
    165209    // atomically update sts
     
    175219                                  uint32_t  nbytes )
    176220{
     221
     222assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" );
     223
    177224    remote_buf_t * buf_ptr = GET_PTR( buf_xp );
    178225    cxy_t          buf_cxy = GET_CXY( buf_xp );
    179226
    180227    // build relevant extended pointers
    181     xptr_t sts_xp  = XPTR( buf_cxy , &buf_ptr->sts );
    182     xptr_t ptr_xp  = XPTR( buf_cxy , &buf_ptr->ptr );
    183     xptr_t size_xp = XPTR( buf_cxy , &buf_ptr->size );
    184     xptr_t data_xp = XPTR( buf_cxy , &buf_ptr->data );
     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 );
    185232
    186233    // get relevant infos from remote buffer descriptor
    187234    uint32_t  sts  = hal_remote_l32( sts_xp );
    188     uint32_t  ptr  = hal_remote_l32( ptr_xp );
    189     uint32_t  size = hal_remote_l32( size_xp );
     235    uint32_t  rid  = hal_remote_l32( rid_xp );
     236    uint32_t  order = hal_remote_l32( order_xp );
    190237    uint8_t * data = hal_remote_lpt( data_xp );
     238
     239    uint32_t  size = 1 << order;
     240    uint32_t  mask = size - 1;
    191241
    192242    // check enough bytes in buffer
     
    194244
    195245    // move nbytes
    196     if( (ptr + nbytes) <= size)  // no wrap around => one move
     246    if( (rid + nbytes) <= size)  // no wrap around => one move
    197247    {
    198248        hal_remote_memcpy( XPTR( local_cxy , k_buf ),
    199                            XPTR( buf_cxy   , data + ptr ),
     249                           XPTR( buf_cxy   , data + rid ),
    200250                           nbytes );
    201251    }
    202252    else                         // wrap around => two moves
    203253    {
    204         uint32_t bytes_1 = size - ptr;
     254        uint32_t bytes_1 = size - rid;
    205255        uint32_t bytes_2 = nbytes - bytes_1;
    206256     
    207257        hal_remote_memcpy( XPTR( local_cxy , k_buf ),
    208                            XPTR( buf_cxy   , data + ptr ),
     258                           XPTR( buf_cxy   , data + rid ),
    209259                           bytes_1 );
    210260
     
    214264    }
    215265
    216     // update ptr in buffer descriptor
    217     hal_remote_s32( ptr_xp , (ptr + nbytes) % size );
     266    // update rid in buffer descriptor
     267    hal_remote_s32( rid_xp , (rid + nbytes) & mask );
    218268
    219269    // atomically update sts
     
    222272    return 0;
    223273
    224 }  // end remote_buf_get_to_user()
     274}  // end remote_buf_get_to_kernel()
    225275
    226276///////////////////////////////////////////////////
     
    229279                                  uint32_t  nbytes )
    230280{
     281
     282assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" );
     283
    231284    remote_buf_t * buf_ptr = GET_PTR( buf_xp );
    232285    cxy_t          buf_cxy = GET_CXY( buf_xp );
    233286
    234287    // build relevant extended pointers
    235     xptr_t sts_xp  = XPTR( buf_cxy , &buf_ptr->sts );
    236     xptr_t ptw_xp  = XPTR( buf_cxy , &buf_ptr->ptw );
    237     xptr_t size_xp = XPTR( buf_cxy , &buf_ptr->size );
    238     xptr_t data_xp = XPTR( buf_cxy , &buf_ptr->data );
     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 );
    239292
    240293    // get relevant infos from remote buffer descriptor
    241294    uint32_t  sts  = hal_remote_l32( sts_xp );
    242     uint32_t  ptw  = hal_remote_l32( ptw_xp );
    243     uint32_t  size = hal_remote_l32( size_xp );
     295    uint32_t  wid  = hal_remote_l32( wid_xp );
     296    uint32_t  order = hal_remote_l32( order_xp );
    244297    uint8_t * data = hal_remote_lpt( data_xp );
     298
     299    uint32_t  size = 1 << order;
     300    uint32_t  mask = size - 1;
    245301
    246302    // check enough space in buffer
     
    248304
    249305    // move nbytes
    250     if( (ptw + nbytes) <= size)  // no wrap around => one move
    251     {
    252         hal_copy_from_uspace( XPTR( buf_cxy , data + ptw ),
     306    if( (wid + nbytes) <= size)  // no wrap around => one move
     307    {
     308        hal_copy_from_uspace( XPTR( buf_cxy , data + wid ),
    253309                              u_buf,
    254310                              nbytes );
     
    256312    else                         // wrap around => two moves
    257313    {
    258         uint32_t bytes_1 = size - ptw;
     314        uint32_t bytes_1 = size - wid;
    259315        uint32_t bytes_2 = nbytes - bytes_1;
    260316     
    261         hal_copy_from_uspace( XPTR( buf_cxy , data + ptw ),
     317        hal_copy_from_uspace( XPTR( buf_cxy , data + wid ),
    262318                              u_buf,
    263319                              bytes_1 );
     
    268324    }
    269325
    270     // update ptw in buffer descriptor
    271     hal_remote_s32( ptw_xp , (ptw + nbytes) % size );
     326    // update wid in buffer descriptor
     327    hal_remote_s32( wid_xp , (wid + nbytes) & mask );
    272328
    273329    // atomically update sts
     
    283339                                    uint32_t  nbytes )
    284340{
     341
     342assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" );
     343
    285344    remote_buf_t * buf_ptr = GET_PTR( buf_xp );
    286345    cxy_t          buf_cxy = GET_CXY( buf_xp );
    287346
    288347    // build relevant extended pointers
    289     xptr_t sts_xp  = XPTR( buf_cxy , &buf_ptr->sts );
    290     xptr_t ptw_xp  = XPTR( buf_cxy , &buf_ptr->ptw );
    291     xptr_t size_xp = XPTR( buf_cxy , &buf_ptr->size );
    292     xptr_t data_xp = XPTR( buf_cxy , &buf_ptr->data );
     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 );
    293352
    294353    // get relevant infos from remote buffer descriptor
    295354    uint32_t  sts  = hal_remote_l32( sts_xp );
    296     uint32_t  ptw  = hal_remote_l32( ptw_xp );
    297     uint32_t  size = hal_remote_l32( size_xp );
     355    uint32_t  wid  = hal_remote_l32( wid_xp );
     356    uint32_t  order = hal_remote_l32( order_xp );
    298357    uint8_t * data = hal_remote_lpt( data_xp );
     358
     359    uint32_t  size = 1 << order;
     360    uint32_t  mask = size - 1;
    299361
    300362    // check enough space in buffer
     
    302364
    303365    // move nbytes
    304     if( (ptw + nbytes) <= size)  // no wrap around => one move
    305     {
    306         hal_remote_memcpy( XPTR( buf_cxy   , data + ptw ),
     366    if( (wid + nbytes) <= size)  // no wrap around => one move
     367    {
     368        hal_remote_memcpy( XPTR( buf_cxy   , data + wid ),
    307369                           XPTR( local_cxy , k_buf ),
    308370                           nbytes );
     
    310372    else                         // wrap around => two moves
    311373    {
    312         uint32_t bytes_1 = size - ptw;
     374        uint32_t bytes_1 = size - wid;
    313375        uint32_t bytes_2 = nbytes - bytes_1;
    314376     
    315         hal_remote_memcpy( XPTR( buf_cxy   , data + ptw ),
     377        hal_remote_memcpy( XPTR( buf_cxy   , data + wid ),
    316378                           XPTR( local_cxy , k_buf ),
    317379                           bytes_1 );
     
    322384    }
    323385
    324     // update ptw in buffer descriptor
    325     hal_remote_s32( ptw_xp , (ptw + nbytes) % size );
     386    // update wid in buffer descriptor
     387    hal_remote_s32( wid_xp , (wid + nbytes) & mask );
    326388
    327389    // atomically update sts
Note: See TracChangeset for help on using the changeset viewer.