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

Last change on this file since 201 was 200, checked in by karaoui, 12 years ago

+ removing some bugs from:

  • xml_parser.c: check that strcmp == 0 for coproc direction
  • mwmr_channel.c: mwmr->ptr to ptr.
  • utils.h double definition of memcpy fonction.

+ mapping_info.h : naming the enums
+ boot_init.c: changing the order of initialisation in order

to initialise the coproc with the mwmr channel values( depth, width...).
Note that the vobjs are now physically initialised.

+ mwmr_channel.h: changing the order argument definition to suit vci_mwmr_channel Soclib componnent.
+ hwr_mapping.h: adding the register description of the vci_mwmr_controller .

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    if(nitems == 0) return;
88
89    while(1)
90    {
91        // get the lock
92        mwmr_lock_acquire(&mwmr->lock);
93
94        // compute spaces and nwords
95        depth  = mwmr->depth;
96        width  = mwmr->width;
97        sts    = mwmr->sts;
98        ptw    = mwmr->ptw;
99        spaces = depth - sts;
100        nwords = width * nitems;
101
102        if( spaces >= nwords )  // write nwords, release lock and return
103        {
104            for ( x = 0 ; x < nwords ; x++ ) 
105            {
106                mwmr->data[ptw] = buffer[x];
107                if ( (ptw + 1) == depth ) ptw = 0;
108                else                      ptw = ptw + 1;
109            }
110            mwmr->ptw  = ptw;
111            mwmr->sts  = sts + nwords;
112            mwmr->lock = 0;
113            return;
114        }
115        else if ( spaces < width )      // release lock and retry after delay
116        {
117            mwmr->lock = 0;
118            for ( x = giet_rand()>>8 ; x > 0 ; x-- ) asm volatile ( "nop" );
119        }
120        else    // write as many items as possible, release lock and retry after delay
121        {
122            nwords = (spaces/width) * width;  // integer number of items
123            for ( x = 0 ; x < nwords ; x++ ) 
124            {
125                mwmr->data[ptw] = buffer[x];
126                if ( (ptw + 1) == depth ) ptw = 0;
127                else                      ptw = ptw + 1;
128            }
129            mwmr->sts  = sts + nwords;
130            mwmr->ptw  = ptw;
131            buffer     = buffer + nwords;
132            nitems     = nitems - (nwords/width);
133            mwmr->lock = 0;
134        }
135        // random delay before retry
136        for ( x = giet_rand()>>6 ; x > 0 ; x-- ) asm volatile ( "nop" );
137    }
138} 
139
140//////////////////////////////////////////////////////////////////////////////
141//      mwmr_read()
142// This blocking function returns only when the transfer is completed.
143// The nitems parameter is the number of items to be transfered.
144// The requested transfer is therefore (nitems * width) words.
145// It takes the lock for exclusive access before testing the channel state.
146// If there is not enough data in mwmr channel to read nitems,
147// it reads as many items as possible, releases the lock, and retry
148// after a random delay.
149//////////////////////////////////////////////////////////////////////////////
150void mwmr_read( mwmr_channel_t*         mwmr, 
151                unsigned int*           buffer,
152                unsigned int            nitems )
153{
154    unsigned int        x;
155    unsigned int        nwords;         // requested transfer length (in words)
156    unsigned int    depth;              // channel depth (in words)
157    unsigned int    width;              // channel width (in words)
158    unsigned int    sts;        // channel sts
159    unsigned int    ptr;        // channel ptw
160
161    if(nitems == 0) return;
162
163    while(1)
164    {
165        // get the lock
166        mwmr_lock_acquire( &mwmr->lock );
167
168        // compute nwords
169        depth  = mwmr->depth;
170        width  = mwmr->width;
171        sts    = mwmr->sts;
172        ptr    = mwmr->ptr;
173        nwords = width * nitems;
174
175        if( sts >= nwords )     // read nwords, release lock and return
176        {
177            for ( x = 0 ; x < nwords ; x++ ) 
178            {
179                buffer[x] = mwmr->data[ptr];
180                if ( (ptr + 1) == depth ) ptr = 0;
181                else                      ptr = ptr + 1;
182            }
183            mwmr->sts  = mwmr->sts - nwords;
184            mwmr->ptr  = ptr;
185            mwmr->lock = 0;
186            return;
187        }
188        else if ( sts < width ) // release lock and retry after delay
189        {
190            mwmr->lock = 0;
191            for ( x = giet_rand()>>8 ; x > 0 ; x-- ) asm volatile ( "nop" );
192        }
193        else    // read as many items as possible, release lock and retry after delay
194        {
195            nwords = (sts/width) * width;       // integer number of items
196            for ( x = 0 ; x < nwords ; x++ ) 
197            {
198                buffer[x] = mwmr->data[ptr];
199                if ( (ptr + 1) == depth ) ptr = 0;
200                else                      ptr = ptr + 1;
201            }
202            mwmr->sts  = sts - nwords;
203            mwmr->ptr  = ptr;
204            buffer     = buffer + nwords;
205            nitems     = nitems - (nwords/width);
206            mwmr->lock = 0;
207        }
208        // random delay before retry
209        for ( x = giet_rand()>>6 ; x > 0 ; x-- ) asm volatile ( "nop" );
210    }
211} 
212
Note: See TracBrowser for help on using the repository browser.