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

Last change on this file since 267 was 124, checked in by max@…, 7 years ago

rename hal_wbflush->hal_fence

File size: 5.6 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
25#include <hal_types.h>
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
41        fifo->wr_id     = 0;
42        fifo->rd_id     = 0;
43    for( slot = 0 ; slot < CONFIG_REMOTE_FIFO_SLOTS ; slot++ )
44    {
[68]45        fifo->valid[slot] = 0;
[1]46    }
47}
48
[68]49//////////////////////////////////////////////
50error_t remote_fifo_put_item( xptr_t     fifo, 
51                              uint64_t * item,
52                              bool_t   * first )
[1]53{
54    uint32_t        wr_id;
55    uint32_t        rd_id;
56    uint32_t        ptw;
57    uint32_t        watchdog;
[60]58    reg_t           save_sr;
[1]59    uint32_t        nslots;
60
61    // get remote cluster identifier and pointer on FIFO
62    cxy_t           cxy = (cxy_t)GET_CXY( fifo );
63    remote_fifo_t * ptr = (remote_fifo_t *)GET_PTR( fifo );
64
65    // initialise watchdog for contention detection
66    watchdog = 0;
67
68    // no descheduling during put access
69        hal_disable_irq( &save_sr );
70       
[68]71    // get write slot index and atomic increment
[1]72        wr_id = hal_remote_atomic_add( XPTR( cxy , &ptr->wr_id ) , 1 );
73
74    // check fifo state
75    while( 1 )
76    {
77        // return error if contention detected by watchdog
78        if( watchdog > CONFIG_REMOTE_FIFO_MAX_ITERATIONS ) 
79        {
80            // restore scheduling
81                hal_restore_irq( save_sr );
82
83            // return error
84            return EBUSY;
85        }
86
87        // read remote rd_id value
88        rd_id = hal_remote_lw( XPTR( cxy , &ptr->rd_id ) );
89
90        // compute number of full slots
91        if( wr_id >= rd_id ) nslots = wr_id - rd_id;
92        else                 nslots = (0xFFFFFFFF - rd_id) + wr_id;
93
94        // exit if fifo not full
95        if ( nslots < CONFIG_REMOTE_FIFO_SLOTS )  break;
96       
97        // restore interrupts
98            hal_restore_irq( save_sr );
99
100        // deschedule without blocking
101        if( thread_can_yield() ) sched_yield();
102
103        // disable interrupts
104            hal_disable_irq( &save_sr );
105
106        // increment watchdog
107        watchdog++;
108    }
109
110    // compute actual write slot pointer
111    ptw = wr_id % CONFIG_REMOTE_FIFO_SLOTS;
112
113    // copy item to fifo
[68]114        hal_remote_swd( XPTR( cxy , &ptr->data[ptw] ), *item );
[124]115        hal_fence();
[1]116
117    // set the slot valid flag
118        hal_remote_sw( XPTR( cxy , &ptr->valid[ptw] ) , 1 );
[124]119        hal_fence();
[1]120
121    // return the first RPC flag
122    *first = ( wr_id == rd_id );
123   
124    // restore scheduling
125        hal_restore_irq( save_sr );
126
127    return 0;
128 
129} // end remote_fifo_put_item()
130
131//////////////////////////////////////////////////
132error_t local_fifo_get_item( remote_fifo_t * fifo,
[68]133                             uint64_t      * item )
[1]134{
135    // get fifo state
136        uint32_t rd_id     = fifo->rd_id;
137        uint32_t wr_id     = fifo->wr_id;
138
139        // return if fifo empty
140        if( rd_id == wr_id ) return EAGAIN;
141       
142    // compute actual read slot pointer
143        uint32_t ptr = rd_id % CONFIG_REMOTE_FIFO_SLOTS;
144       
145        // wait slot filled by the writer
146        while( fifo->valid[ptr] == 0 ) {}
147       
148    // copy item from FIFO to local buffer
[68]149    *item = fifo->data[ptr];
[1]150
151    // reset valid slot flag
152    fifo->valid[ptr] = 0;
153
154    // increment the read index
155        fifo->rd_id += 1;
156
157        return 0;
158} // end local_fifo_get_item()
159
160/////////////////////////////////////////
161bool_t remote_fifo_is_full( xptr_t fifo )
162{
163    uint32_t nslots;
164
165    // get remote cluster identifier and pointer on FIFO
166    cxy_t           cxy = (cxy_t)GET_CXY( fifo );
167    remote_fifo_t * ptr = (remote_fifo_t *)GET_PTR( fifo );
168   
169    // get read and write pointers
170        uint32_t wr_id = hal_remote_lw( XPTR( cxy , &ptr->wr_id ) );
171        uint32_t rd_id = hal_remote_lw( XPTR( cxy , &ptr->rd_id ) );
172
173    // compute number of full slots
174    if( wr_id >= rd_id ) nslots = wr_id - rd_id;
175    else                 nslots = (0xFFFFFFFF - rd_id) + wr_id;
176
177    return ( nslots >= CONFIG_REMOTE_FIFO_SLOTS );
178}
179
180//////////////////////////////////////////////////
181bool_t local_fifo_is_empty( remote_fifo_t * fifo )
182{
183    return ( fifo->wr_id == fifo->rd_id );
184}
185
186/////////////////////////////////////////
187uint32_t remote_fifo_items( xptr_t fifo )
188{
189    uint32_t nslots;
190
191    // get remote cluster identifier and pointer on FIFO
192    cxy_t           cxy = (cxy_t)GET_CXY( fifo );
193    remote_fifo_t * ptr = (remote_fifo_t *)GET_PTR( fifo );
194   
195    // get read and write pointers
196        uint32_t wr_id = hal_remote_lw( XPTR( cxy , &ptr->wr_id ) );
197        uint32_t rd_id = hal_remote_lw( XPTR( cxy , &ptr->rd_id ) );
198
199    // compute number of full slots
200    if( wr_id >= rd_id ) nslots = wr_id - rd_id;
201    else                 nslots = (0xFFFFFFFF - rd_id) + wr_id;
202
203    return nslots;
204}
205
Note: See TracBrowser for help on using the repository browser.