source: soft/giet_vm/libs/mwmr_channel.c @ 167

Last change on this file since 167 was 165, checked in by alain, 12 years ago

Introducing various modifications in kernel initialisation

File size: 8.4 KB
Line 
1//////////////////////////////////////////////////////////////////////////////////
2// File     : mwmr_channel_.c         
3// Date     : 01/04/2012
4// Author   : alain greiner
5// Copyright (c) UPMC-LIP6
6///////////////////////////////////////////////////////////////////////////////////
7// The mwmr_channel.c and mwmr_channel.h files are part of the GIET nano-kernel.
8// This  middleware implements a user level Multi-Writers / Multi-Readers
9// communication channel, that can be used by parallel multi-tasks applications
10// respecting the TCG (Tasks and Communications Graph) formalism.
11//
12// The mwmr_read() and mwmr_write() functions do not require a system call.
13// The channel itself must have been allocated in a non cacheable segment,
14// if the platform does not provide hardware cache coherence.
15//
16// ALL MWMR channels must be defined in the mapping_info data structure,
17// to be initialised by the GIET in the boot phase.
18// The vobj_get_vbase() system call (defined in stdio.c and stdio.h files)
19// can be used to get the virtual base address of the channel from it's name.
20//
21// An MWMR transaction transfer an integer number of items, and an item is
22// an integer number of unsigned int (32 bits words).
23// The max number of words that can be stored in a MWMR channel is defined by the
24// "depth" parameter, and the "width" parameter define the minimal number of
25// word contained in an atomic item. Therefore, the "depth" parameter must be
26// a multiple of the "width" parameter.
27//
28// Both the mwmr_read() and mwmr_write() functions are blocking functions.
29// A private lock provides exclusive access to the MWMR channel, that can have
30// a variable number of producers and a variable number of consumers.
31///////////////////////////////////////////////////////////////////////////////////
32
33#include <mwmr_channel.h>
34#include <stdio.h>
35
36//////////////////////////////////////////////////////////////////////////////
37//  mwmr_lock_aquire()
38// This blocking function returns only when the lock has been taken.
39// If the lock is already taken a fixed delay is introduced before retry.
40//////////////////////////////////////////////////////////////////////////////
41void mwmr_lock_acquire(unsigned int* lock_address)
42{
43    register unsigned int*      plock = lock_address;
44    register unsigned int       delay = 100;
45    asm volatile (
46            "mwmr_lock_try:                                     \n"
47            "ll   $2,    0(%0)                          \n" /* $2 <= lock current value */
48            "bnez $2,    mwmr_lock_delay        \n" /* retry after delay if lock busy */
49            "li   $3,    1                                      \n" /* $3 <= argument for sc */
50            "sc   $3,    0(%0)                          \n" /* try to get lock */
51            "bnez $3,    mwmr_lock_ok           \n" /* exit if atomic */
52            "mwmr_lock_delay:                           \n"
53            "move $4,    %1                 \n" /* $4 <= delay */
54            "mwmr_lock_loop:                \n"
55            "beqz $4,    mwmr_lock_loop         \n" /* test end delay */
56            "addi $4,    $4,  -1                        \n" /* $4 <= $4 - 1 */
57            "j           mwmr_lock_try          \n" /* retry ll */
58            "nop                                                        \n"
59            "mwmr_lock_ok:                                      \n"
60            :
61            :"r"(plock), "r"(delay)
62            :"$2", "$3", "$4");
63} 
64
65//////////////////////////////////////////////////////////////////////////////
66//      mwmr_write()
67// This blocking function returns only when the transfer is completed.
68// The nitems parameter is the number of items to be transfered.
69// The requested transfer is therefore (nitems * width) words.
70// It takes the lock for exclusive access before testing the channel state.
71// If there is not enough space in mwmr channel to write nitems,
72// it writes as many items as possible, releases the lock, and retry
73// after a random delay.
74//////////////////////////////////////////////////////////////////////////////
75void mwmr_write( mwmr_channel_t*        mwmr, 
76                 unsigned int*          buffer,
77                 unsigned int           nitems )
78{
79    unsigned int        x;
80    unsigned int        spaces;         // number of empty slots (in words)
81    unsigned int        nwords;         // requested transfer length (in words)
82    unsigned int    depth;              // channel depth (in words)
83    unsigned int    width;              // channel width (in words)
84    unsigned int    sts;        // channel sts
85    unsigned int    ptw;        // channel ptw
86
87    while(1)
88    {
89        // get the lock
90        mwmr_lock_acquire(&mwmr->lock);
91
92        // compute spaces and nwords
93        depth  = mwmr->depth;
94        width  = mwmr->width;
95        sts    = mwmr->sts;
96        ptw    = mwmr->ptw;
97        spaces = depth - sts;
98        nwords = width * nitems;
99
100        if( spaces >= nwords )  // write nwords, release lock and return
101        {
102            for ( x = 0 ; x < nwords ; x++ ) 
103            {
104                mwmr->data[ptw] = buffer[x];
105                if ( (ptw + 1) == depth ) ptw = 0;
106                else                      ptw = ptw + 1;
107            }
108            mwmr->ptw  = ptw;
109            mwmr->sts  = sts + nwords;
110            mwmr->lock = 0;
111            return;
112        }
113        else if ( spaces < width )      // release lock and retry after delay
114        {
115            mwmr->lock = 0;
116            for ( x = giet_rand()>>8 ; x > 0 ; x-- ) asm volatile ( "nop" );
117        }
118        else    // write as many items as possible, release lock and retry after delay
119        {
120            nwords = (spaces/width) * width;  // integer nmber of items
121            for ( x = 0 ; x < nwords ; x++ ) 
122            {
123                mwmr->data[ptw] = buffer[x];
124                if ( (ptw + 1) == depth ) ptw = 0;
125                else                      ptw = ptw + 1;
126            }
127            mwmr->sts  = sts + nwords;
128            mwmr->ptw  = ptw;
129            buffer     = buffer + nwords;
130            nitems     = nitems - (nwords/width);
131            mwmr->lock = 0;
132        }
133        // random delay before retry
134        for ( x = giet_rand()>>6 ; x > 0 ; x-- ) asm volatile ( "nop" );
135    }
136} 
137
138//////////////////////////////////////////////////////////////////////////////
139//      mwmr_read()
140// This blocking function returns only when the transfer is completed.
141// The nitems parameter is the number of items to be transfered.
142// The requested transfer is therefore (nitems * width) words.
143// It takes the lock for exclusive access before testing the channel state.
144// If there is not enough data in mwmr channel to read nitems,
145// it reads as many items as possible, releases the lock, and retry
146// after a random delay.
147//////////////////////////////////////////////////////////////////////////////
148void mwmr_read( mwmr_channel_t*         mwmr, 
149                unsigned int*           buffer,
150                unsigned int            nitems )
151{
152    unsigned int        x;
153    unsigned int        nwords;         // requested transfer length (in words)
154    unsigned int    depth;              // channel depth (in words)
155    unsigned int    width;              // channel width (in words)
156    unsigned int    sts;        // channel sts
157    unsigned int    ptr;        // channel ptw
158
159    while(1)
160    {
161        // get the lock
162        mwmr_lock_acquire( &mwmr->lock );
163
164        // compute nwords
165        depth  = mwmr->depth;
166        width  = mwmr->width;
167        sts    = mwmr->sts;
168        ptr    = mwmr->ptr;
169        nwords = width * nitems;
170
171        if( sts >= nwords )     // read nwords, release lock and return
172        {
173            for ( x = 0 ; x < nwords ; x++ ) 
174            {
175                buffer[x] = mwmr->data[ptr];
176                if ( (ptr + 1) == depth ) ptr = 0;
177                else                      ptr = ptr + 1;
178            }
179            mwmr->sts  = mwmr->sts - nwords;
180            mwmr->ptr  = ptr;
181            mwmr->lock = 0;
182            return;
183        }
184        else if ( sts < width ) // release lock and retry after delay
185        {
186            mwmr->lock = 0;
187            for ( x = giet_rand()>>8 ; x > 0 ; x-- ) asm volatile ( "nop" );
188        }
189        else    // read as many items as possible, release lock and retry after delay
190        {
191            nwords = (sts/width) * width;       // integer number of items
192            for ( x = 0 ; x < nwords ; x++ ) 
193            {
194                buffer[x] = mwmr->data[mwmr->ptr];
195                if ( (ptr + 1) == depth ) ptr = 0;
196                else                      ptr = ptr + 1;
197            }
198            mwmr->sts  = sts - nwords;
199            mwmr->ptr  = ptr;
200            buffer     = buffer + nwords;
201            nitems     = nitems - (nwords/width);
202            mwmr->lock = 0;
203        }
204        // random delay before retry
205        for ( x = giet_rand()>>6 ; x > 0 ; x-- ) asm volatile ( "nop" );
206    }
207} 
208
Note: See TracBrowser for help on using the repository browser.