source: trunk/kernel/libk/remote_fifo.c @ 487

Last change on this file since 487 was 457, checked in by alain, 6 years ago

This version modifies the exec syscall and fixes a large number of small bugs.
The version number has been updated (0.1)

File size: 5.5 KB
RevLine 
[1]1/*
[68]2 * remote_fifo.c  Implement a lock-less FIFO, multiple-remote-writers / single-local-reader
[1]3 *
4 * Authors : Mohamed Lamine Karaoui (2015)
[68]5 *           Alain Greiner          (2016,2017)
[1]6 *
7 * Copyright (c) UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
[457]25#include <hal_kernel_types.h>
[1]26#include <hal_irqmask.h>
27#include <hal_remote.h>
28#include <hal_special.h>
29#include <bits.h>
30#include <memcpy.h>
31#include <kmem.h>
32#include <thread.h>
33#include <cluster.h>
34#include <remote_fifo.h>
35
36////////////////////////////////////////////
37void local_fifo_init( remote_fifo_t * fifo )
38{
39    uint32_t  slot;
40
[407]41    fifo->owner     = 0;
[1]42        fifo->wr_id     = 0;
43        fifo->rd_id     = 0;
44    for( slot = 0 ; slot < CONFIG_REMOTE_FIFO_SLOTS ; slot++ )
45    {
[68]46        fifo->valid[slot] = 0;
[1]47    }
48}
49
[407]50/////////////////////////////////////////////////
51error_t remote_fifo_put_item( xptr_t     fifo_xp, 
52                              uint64_t   item )
[1]53{
54    uint32_t        wr_id;
55    uint32_t        rd_id;
56    uint32_t        ptw;
57    uint32_t        watchdog;
58    uint32_t        nslots;
59
60    // get remote cluster identifier and pointer on FIFO
[407]61    cxy_t           fifo_cxy = (cxy_t)GET_CXY( fifo_xp );
62    remote_fifo_t * fifo_ptr = (remote_fifo_t *)GET_PTR( fifo_xp );
[1]63
64    // initialise watchdog for contention detection
65    watchdog = 0;
66
[407]67    // get write slot index with atomic increment
68        wr_id = hal_remote_atomic_add( XPTR( fifo_cxy , &fifo_ptr->wr_id ) , 1 );
[1]69
[407]70    // wait until allocated slot is empty in remote FIFO
71    // max retry = CONFIG_REMOTE_FIFO_MAX_ITERATIONS 
72    // return error if watchdog is reached
[1]73    while( 1 )
74    {
75        // return error if contention detected by watchdog
[407]76        if( watchdog > CONFIG_REMOTE_FIFO_MAX_ITERATIONS )  return EBUSY;
[1]77
78        // read remote rd_id value
[407]79        rd_id = hal_remote_lw( XPTR( fifo_cxy , &fifo_ptr->rd_id ) );
[1]80
81        // compute number of full slots
82        if( wr_id >= rd_id ) nslots = wr_id - rd_id;
83        else                 nslots = (0xFFFFFFFF - rd_id) + wr_id;
84
[407]85        // exit waiting loop as soon as fifo not full
[1]86        if ( nslots < CONFIG_REMOTE_FIFO_SLOTS )  break;
87       
[407]88        // retry later if fifo full:
89        // - deschedule without blocking if possible
90        // - wait ~1000 cycles otherwise
[408]91        if( thread_can_yield() ) sched_yield( "wait RPC fifo" );
[407]92        else                     hal_fixed_delay( 1000 );
[1]93
94        // increment watchdog
95        watchdog++;
96    }
97
98    // compute actual write slot pointer
99    ptw = wr_id % CONFIG_REMOTE_FIFO_SLOTS;
100
101    // copy item to fifo
[407]102        hal_remote_swd( XPTR( fifo_cxy , &fifo_ptr->data[ptw] ), item );
[124]103        hal_fence();
[1]104
105    // set the slot valid flag
[407]106        hal_remote_sw( XPTR( fifo_cxy , &fifo_ptr->valid[ptw] ) , 1 );
[124]107        hal_fence();
[1]108
109    return 0;
110 
111} // end remote_fifo_put_item()
112
113//////////////////////////////////////////////////
114error_t local_fifo_get_item( remote_fifo_t * fifo,
[68]115                             uint64_t      * item )
[1]116{
117    // get fifo state
118        uint32_t rd_id     = fifo->rd_id;
119        uint32_t wr_id     = fifo->wr_id;
120
121        // return if fifo empty
122        if( rd_id == wr_id ) return EAGAIN;
123       
124    // compute actual read slot pointer
125        uint32_t ptr = rd_id % CONFIG_REMOTE_FIFO_SLOTS;
126       
127        // wait slot filled by the writer
128        while( fifo->valid[ptr] == 0 ) {}
129       
130    // copy item from FIFO to local buffer
[68]131    *item = fifo->data[ptr];
[1]132
133    // reset valid slot flag
134    fifo->valid[ptr] = 0;
135
136    // increment the read index
137        fifo->rd_id += 1;
138
139        return 0;
140} // end local_fifo_get_item()
141
142/////////////////////////////////////////
143bool_t remote_fifo_is_full( xptr_t fifo )
144{
145    uint32_t nslots;
146
147    // get remote cluster identifier and pointer on FIFO
148    cxy_t           cxy = (cxy_t)GET_CXY( fifo );
149    remote_fifo_t * ptr = (remote_fifo_t *)GET_PTR( fifo );
150   
151    // get read and write pointers
152        uint32_t wr_id = hal_remote_lw( XPTR( cxy , &ptr->wr_id ) );
153        uint32_t rd_id = hal_remote_lw( XPTR( cxy , &ptr->rd_id ) );
154
155    // compute number of full slots
156    if( wr_id >= rd_id ) nslots = wr_id - rd_id;
157    else                 nslots = (0xFFFFFFFF - rd_id) + wr_id;
158
159    return ( nslots >= CONFIG_REMOTE_FIFO_SLOTS );
160}
161
162//////////////////////////////////////////////////
163bool_t local_fifo_is_empty( remote_fifo_t * fifo )
164{
165    return ( fifo->wr_id == fifo->rd_id );
166}
167
168/////////////////////////////////////////
169uint32_t remote_fifo_items( xptr_t fifo )
170{
171    uint32_t nslots;
172
173    // get remote cluster identifier and pointer on FIFO
174    cxy_t           cxy = (cxy_t)GET_CXY( fifo );
175    remote_fifo_t * ptr = (remote_fifo_t *)GET_PTR( fifo );
176   
177    // get read and write pointers
178        uint32_t wr_id = hal_remote_lw( XPTR( cxy , &ptr->wr_id ) );
179        uint32_t rd_id = hal_remote_lw( XPTR( cxy , &ptr->rd_id ) );
180
181    // compute number of full slots
182    if( wr_id >= rd_id ) nslots = wr_id - rd_id;
183    else                 nslots = (0xFFFFFFFF - rd_id) + wr_id;
184
185    return nslots;
186}
187
Note: See TracBrowser for help on using the repository browser.