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