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

Last change on this file since 401 was 296, checked in by alain, 7 years ago

Several modifs in the generic scheduler and in the hal_context to
fix the context switch mechanism.

File size: 5.6 KB
Line 
1/*
2 * remote_fifo.c  Implement a lock-less FIFO, multiple-remote-writers / single-local-reader
3 *
4 * Authors : Mohamed Lamine Karaoui (2015)
5 *           Alain Greiner          (2016,2017)
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    {
45        fifo->valid[slot] = 0;
46    }
47}
48
49//////////////////////////////////////////////
50error_t remote_fifo_put_item( xptr_t     fifo, 
51                              uint64_t   item,
52                              bool_t   * first )
53{
54    uint32_t        wr_id;
55    uint32_t        rd_id;
56    uint32_t        ptw;
57    uint32_t        watchdog;
58    reg_t           save_sr;
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       
71    // get write slot index and atomic increment
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( NULL );
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
114        hal_remote_swd( XPTR( cxy , &ptr->data[ptw] ), item );
115
116        hal_fence();
117
118    // set the slot valid flag
119        hal_remote_sw( XPTR( cxy , &ptr->valid[ptw] ) , 1 );
120        hal_fence();
121
122    // return the first RPC flag
123    *first = ( wr_id == rd_id );
124   
125    // restore scheduling
126        hal_restore_irq( save_sr );
127
128    return 0;
129 
130} // end remote_fifo_put_item()
131
132//////////////////////////////////////////////////
133error_t local_fifo_get_item( remote_fifo_t * fifo,
134                             uint64_t      * item )
135{
136    // get fifo state
137        uint32_t rd_id     = fifo->rd_id;
138        uint32_t wr_id     = fifo->wr_id;
139
140        // return if fifo empty
141        if( rd_id == wr_id ) return EAGAIN;
142       
143    // compute actual read slot pointer
144        uint32_t ptr = rd_id % CONFIG_REMOTE_FIFO_SLOTS;
145       
146        // wait slot filled by the writer
147        while( fifo->valid[ptr] == 0 ) {}
148       
149    // copy item from FIFO to local buffer
150    *item = fifo->data[ptr];
151
152    // reset valid slot flag
153    fifo->valid[ptr] = 0;
154
155    // increment the read index
156        fifo->rd_id += 1;
157
158        return 0;
159} // end local_fifo_get_item()
160
161/////////////////////////////////////////
162bool_t remote_fifo_is_full( xptr_t fifo )
163{
164    uint32_t nslots;
165
166    // get remote cluster identifier and pointer on FIFO
167    cxy_t           cxy = (cxy_t)GET_CXY( fifo );
168    remote_fifo_t * ptr = (remote_fifo_t *)GET_PTR( fifo );
169   
170    // get read and write pointers
171        uint32_t wr_id = hal_remote_lw( XPTR( cxy , &ptr->wr_id ) );
172        uint32_t rd_id = hal_remote_lw( XPTR( cxy , &ptr->rd_id ) );
173
174    // compute number of full slots
175    if( wr_id >= rd_id ) nslots = wr_id - rd_id;
176    else                 nslots = (0xFFFFFFFF - rd_id) + wr_id;
177
178    return ( nslots >= CONFIG_REMOTE_FIFO_SLOTS );
179}
180
181//////////////////////////////////////////////////
182bool_t local_fifo_is_empty( remote_fifo_t * fifo )
183{
184    return ( fifo->wr_id == fifo->rd_id );
185}
186
187/////////////////////////////////////////
188uint32_t remote_fifo_items( xptr_t fifo )
189{
190    uint32_t nslots;
191
192    // get remote cluster identifier and pointer on FIFO
193    cxy_t           cxy = (cxy_t)GET_CXY( fifo );
194    remote_fifo_t * ptr = (remote_fifo_t *)GET_PTR( fifo );
195   
196    // get read and write pointers
197        uint32_t wr_id = hal_remote_lw( XPTR( cxy , &ptr->wr_id ) );
198        uint32_t rd_id = hal_remote_lw( XPTR( cxy , &ptr->rd_id ) );
199
200    // compute number of full slots
201    if( wr_id >= rd_id ) nslots = wr_id - rd_id;
202    else                 nslots = (0xFFFFFFFF - rd_id) + wr_id;
203
204    return nslots;
205}
206
Note: See TracBrowser for help on using the repository browser.