1 | /////////////////////////////////////////////////////////////////////////////////// |
---|
2 | // File : bdv_driver.c |
---|
3 | // Date : 23/05/2013 |
---|
4 | // Author : alain greiner |
---|
5 | // Maintainer: cesar fuguet |
---|
6 | // Copyright (c) UPMC-LIP6 |
---|
7 | /////////////////////////////////////////////////////////////////////////////////// |
---|
8 | // Implementation notes: |
---|
9 | // 1. In order to share code, the two _bdv_read() and _bdv_write() functions |
---|
10 | // call the same _bdv_access() function. |
---|
11 | // 2. All accesses to BDV registers are done by the two |
---|
12 | // _bdv_set_register() and _bdv_get_register() low-level functions, |
---|
13 | // that are handling virtual / physical extended addressing. |
---|
14 | /////////////////////////////////////////////////////////////////////////////////// |
---|
15 | |
---|
16 | #include <giet_config.h> |
---|
17 | #include <hard_config.h> |
---|
18 | #include <bdv_driver.h> |
---|
19 | #include <xcu_driver.h> |
---|
20 | #include <ioc_driver.h> |
---|
21 | #include <utils.h> |
---|
22 | #include <tty0.h> |
---|
23 | #include <ctx_handler.h> |
---|
24 | |
---|
25 | /////////////////////////////////////////////////////////////////////////////// |
---|
26 | // BDV global variables |
---|
27 | /////////////////////////////////////////////////////////////////////////////// |
---|
28 | |
---|
29 | spin_lock_t _bdv_lock __attribute__((aligned(64))); |
---|
30 | unsigned int _bdv_status; |
---|
31 | unsigned int _bdv_gtid; |
---|
32 | |
---|
33 | /////////////////////////////////////////////////////////////////////////////// |
---|
34 | // This low_level function returns the value contained in register (index). |
---|
35 | /////////////////////////////////////////////////////////////////////////////// |
---|
36 | unsigned int _bdv_get_register( unsigned int index ) |
---|
37 | { |
---|
38 | unsigned int* vaddr = (unsigned int*)SEG_IOC_BASE + index; |
---|
39 | return _io_extended_read( vaddr ); |
---|
40 | } |
---|
41 | |
---|
42 | /////////////////////////////////////////////////////////////////////////////// |
---|
43 | // This low-level function set a new value in register (index). |
---|
44 | /////////////////////////////////////////////////////////////////////////////// |
---|
45 | void _bdv_set_register( unsigned int index, |
---|
46 | unsigned int value ) |
---|
47 | { |
---|
48 | unsigned int* vaddr = (unsigned int*)SEG_IOC_BASE + index; |
---|
49 | _io_extended_write( vaddr, value ); |
---|
50 | } |
---|
51 | |
---|
52 | /////////////////////////////////////////////////////////////////////////////// |
---|
53 | // This function transfer data between a memory buffer and the block device. |
---|
54 | // The buffer lentgth is (count*block_size) bytes. |
---|
55 | // Arguments are: |
---|
56 | // - to_mem : from external storage to memory when non 0. |
---|
57 | // - mode : BOOT / KERNEL / USER |
---|
58 | // - lba : first block index on the external storage. |
---|
59 | // - buf_paddr : physical base address of the memory buffer. |
---|
60 | // - count : number of blocks to be transfered. |
---|
61 | // Returns 0 if success, > 0 if error. |
---|
62 | /////////////////////////////////////////////////////////////////////////////// |
---|
63 | static unsigned int _bdv_access( unsigned int to_mem, |
---|
64 | unsigned int mode, |
---|
65 | unsigned int lba, |
---|
66 | unsigned long long buf_paddr, |
---|
67 | unsigned int count) |
---|
68 | { |
---|
69 | unsigned int procid = _get_procid(); |
---|
70 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
71 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH) - 1); |
---|
72 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
73 | |
---|
74 | #if GIET_DEBUG_IOC_DRIVER |
---|
75 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
76 | _putd( x ); |
---|
77 | _puts(","); |
---|
78 | _putd( y ); |
---|
79 | _puts(","); |
---|
80 | _putd( p ); |
---|
81 | _puts("] enters at cycle "); |
---|
82 | _putd( _get_proctime() ); |
---|
83 | _puts("\n - to_mem = "); |
---|
84 | _putd( to_mem ); |
---|
85 | _puts("\n - mode = "); |
---|
86 | _putd( mode ); |
---|
87 | _puts("\n - paddr = "); |
---|
88 | _putl( buf_paddr ); |
---|
89 | _puts("\n - sectors = "); |
---|
90 | _putd( count ); |
---|
91 | _puts("\n - lba = "); |
---|
92 | _putx( lba ); |
---|
93 | _puts("\n"); |
---|
94 | #endif |
---|
95 | |
---|
96 | unsigned int error = 0; |
---|
97 | |
---|
98 | // get the lock protecting BDV |
---|
99 | _lock_acquire( &_bdv_lock ); |
---|
100 | |
---|
101 | #if GIET_DEBUG_IOC_DRIVER |
---|
102 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
103 | _putd( x ); |
---|
104 | _puts(","); |
---|
105 | _putd( y ); |
---|
106 | _puts(","); |
---|
107 | _putd( p ); |
---|
108 | _puts("] get _bdv_lock at cycle "); |
---|
109 | _putd( _get_proctime() ); |
---|
110 | _puts("\n"); |
---|
111 | #endif |
---|
112 | |
---|
113 | // set device registers |
---|
114 | _bdv_set_register( BLOCK_DEVICE_BUFFER , (unsigned int)buf_paddr ); |
---|
115 | _bdv_set_register( BLOCK_DEVICE_BUFFER_EXT, (unsigned int)(buf_paddr>>32) ); |
---|
116 | _bdv_set_register( BLOCK_DEVICE_COUNT , count ); |
---|
117 | _bdv_set_register( BLOCK_DEVICE_LBA , lba ); |
---|
118 | |
---|
119 | // In BOOT mode, we launch transfer, and poll the BDV_STATUS |
---|
120 | // register because IRQs are masked. |
---|
121 | if ( mode == IOC_BOOT_MODE ) |
---|
122 | { |
---|
123 | // Launch transfert |
---|
124 | if (to_mem == 0) _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_WRITE ); |
---|
125 | else _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_READ ); |
---|
126 | |
---|
127 | #if GIET_DEBUG_IOC_DRIVER |
---|
128 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
129 | _putd( x ); |
---|
130 | _puts(","); |
---|
131 | _putd( y ); |
---|
132 | _puts(","); |
---|
133 | _putd( p ); |
---|
134 | _puts("] launch transfer in polling mode\n"); |
---|
135 | #endif |
---|
136 | unsigned int status; |
---|
137 | do |
---|
138 | { |
---|
139 | status = _bdv_get_register( BLOCK_DEVICE_STATUS ); |
---|
140 | |
---|
141 | #if GIET_DEBUG_IOC_DRIVER |
---|
142 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
143 | _putd( x ); |
---|
144 | _puts(","); |
---|
145 | _putd( y ); |
---|
146 | _puts(","); |
---|
147 | _putd( p ); |
---|
148 | _puts("] wait on BDV_STATUS register ...\n"); |
---|
149 | #endif |
---|
150 | } |
---|
151 | while( (status != BLOCK_DEVICE_READ_SUCCESS) && |
---|
152 | (status != BLOCK_DEVICE_READ_ERROR) && |
---|
153 | (status != BLOCK_DEVICE_WRITE_SUCCESS) && |
---|
154 | (status != BLOCK_DEVICE_WRITE_ERROR) ); // busy waiting |
---|
155 | |
---|
156 | // analyse status |
---|
157 | error = ( (status == BLOCK_DEVICE_READ_ERROR) || |
---|
158 | (status == BLOCK_DEVICE_WRITE_ERROR) ); |
---|
159 | |
---|
160 | // release lock |
---|
161 | _lock_release( &_bdv_lock ); |
---|
162 | } |
---|
163 | // in USER or KERNEL mode, we deschedule the task. |
---|
164 | // When the task is rescheduled, we check the _bdv_status variable, |
---|
165 | // and release the lock. |
---|
166 | // We need a critical section, because we must reset the RUN bit |
---|
167 | // before to launch the transfer, and we don't want to be descheduled |
---|
168 | // between these two operations. |
---|
169 | else |
---|
170 | { |
---|
171 | unsigned int save_sr; |
---|
172 | unsigned int ltid = _get_current_task_id(); |
---|
173 | |
---|
174 | // activates BDV interrupts |
---|
175 | _bdv_set_register( BLOCK_DEVICE_IRQ_ENABLE, 1 ); |
---|
176 | |
---|
177 | // set the _bdv_status variable |
---|
178 | _bdv_status = BLOCK_DEVICE_BUSY; |
---|
179 | |
---|
180 | // enters critical section |
---|
181 | _it_disable( &save_sr ); |
---|
182 | |
---|
183 | // set _bdv_gtid and reset runnable |
---|
184 | _bdv_gtid = (procid<<16) + ltid; |
---|
185 | _set_task_slot( x, y, p, ltid, CTX_RUN_ID, 0 ); |
---|
186 | |
---|
187 | // launch transfer |
---|
188 | if (to_mem == 0) _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_WRITE ); |
---|
189 | else _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_READ ); |
---|
190 | |
---|
191 | #if GIET_DEBUG_IOC_DRIVER |
---|
192 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
193 | _putd( x ); |
---|
194 | _puts(","); |
---|
195 | _putd( y ); |
---|
196 | _puts(","); |
---|
197 | _putd( p ); |
---|
198 | _puts("] launch transfer in nterrupt mode\n"); |
---|
199 | #endif |
---|
200 | |
---|
201 | // deschedule task |
---|
202 | _ctx_switch(); |
---|
203 | |
---|
204 | #if GIET_DEBUG_IOC_DRIVER |
---|
205 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
206 | _putd( x ); |
---|
207 | _puts(","); |
---|
208 | _putd( y ); |
---|
209 | _puts(","); |
---|
210 | _putd( p ); |
---|
211 | _puts("] resume execution after descheduling\n"); |
---|
212 | #endif |
---|
213 | // restore SR |
---|
214 | _it_restore( &save_sr ); |
---|
215 | |
---|
216 | // analyse status |
---|
217 | error = ( (_bdv_status == BLOCK_DEVICE_READ_ERROR) || |
---|
218 | (_bdv_status == BLOCK_DEVICE_WRITE_ERROR) ); |
---|
219 | |
---|
220 | // reset _bdv_status and release lock |
---|
221 | _bdv_status = BLOCK_DEVICE_IDLE; |
---|
222 | _lock_release( &_bdv_lock ); |
---|
223 | } |
---|
224 | |
---|
225 | #if GIET_DEBUG_IOC_DRIVER |
---|
226 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
227 | _putd( x ); |
---|
228 | _puts(","); |
---|
229 | _putd( y ); |
---|
230 | _puts(","); |
---|
231 | _putd( p ); |
---|
232 | _puts("] exit at cycle "); |
---|
233 | _putd( _get_proctime() ); |
---|
234 | _puts(" / error = "); |
---|
235 | _putd( error ); |
---|
236 | _puts("\n"); |
---|
237 | #endif |
---|
238 | |
---|
239 | return error; |
---|
240 | } // end _bdv_access() |
---|
241 | |
---|
242 | /////////////////////////////////////////////////////////////////////////////// |
---|
243 | // External functions |
---|
244 | /////////////////////////////////////////////////////////////////////////////// |
---|
245 | |
---|
246 | //////////////////////// |
---|
247 | unsigned int _bdv_init() |
---|
248 | { |
---|
249 | if ( _bdv_get_register( BLOCK_DEVICE_BLOCK_SIZE ) != 512 ) |
---|
250 | { |
---|
251 | _puts("\n[GIET ERROR] in _bdv_init() : block size must be 512 bytes\n"); |
---|
252 | return 1; |
---|
253 | } |
---|
254 | |
---|
255 | _bdv_set_register( BLOCK_DEVICE_IRQ_ENABLE, 0 ); |
---|
256 | return 0; |
---|
257 | } |
---|
258 | |
---|
259 | //////////////////////////////////////////////// |
---|
260 | unsigned int _bdv_read( unsigned int mode, |
---|
261 | unsigned int lba, |
---|
262 | unsigned long long buffer, |
---|
263 | unsigned int count) |
---|
264 | { |
---|
265 | return _bdv_access( 1, // read access |
---|
266 | mode, |
---|
267 | lba, |
---|
268 | buffer, |
---|
269 | count ); |
---|
270 | } |
---|
271 | |
---|
272 | ///////////////////////////////////////////////// |
---|
273 | unsigned int _bdv_write( unsigned int mode, |
---|
274 | unsigned int lba, |
---|
275 | unsigned long long buffer, |
---|
276 | unsigned int count ) |
---|
277 | { |
---|
278 | return _bdv_access( 0, // write access |
---|
279 | mode, |
---|
280 | lba, |
---|
281 | buffer, |
---|
282 | count ); |
---|
283 | } |
---|
284 | |
---|
285 | ////////////////////////////// |
---|
286 | unsigned int _bdv_get_status() |
---|
287 | { |
---|
288 | return _bdv_get_register( BLOCK_DEVICE_STATUS ); |
---|
289 | } |
---|
290 | |
---|
291 | ////////////////////////////////// |
---|
292 | unsigned int _bdv_get_block_size() |
---|
293 | { |
---|
294 | return _bdv_get_register( BLOCK_DEVICE_BLOCK_SIZE ); |
---|
295 | } |
---|
296 | |
---|
297 | ///////////////////////////////////// |
---|
298 | void _bdv_isr( unsigned int irq_type, // HWI / WTI |
---|
299 | unsigned int irq_id, // index returned by ICU |
---|
300 | unsigned int channel ) // unused |
---|
301 | { |
---|
302 | // get BDV status (and reset IRQ) |
---|
303 | unsigned int status = _bdv_get_register( BLOCK_DEVICE_STATUS ); |
---|
304 | |
---|
305 | // check status: does nothing if IDLE or BUSY |
---|
306 | if ( (status == BLOCK_DEVICE_IDLE) || |
---|
307 | (status == BLOCK_DEVICE_BUSY) ) return; |
---|
308 | |
---|
309 | // save status in kernel buffer _bdv_status |
---|
310 | _bdv_status = status; |
---|
311 | |
---|
312 | // identify task waiting on BDV |
---|
313 | unsigned int remote_procid = _bdv_gtid>>16; |
---|
314 | unsigned int ltid = _bdv_gtid & 0xFFFF; |
---|
315 | unsigned int remote_cluster = remote_procid >> P_WIDTH; |
---|
316 | unsigned int remote_x = remote_cluster >> Y_WIDTH; |
---|
317 | unsigned int remote_y = remote_cluster & ((1<<Y_WIDTH)-1); |
---|
318 | unsigned int remote_p = remote_procid & ((1<<P_WIDTH)-1); |
---|
319 | |
---|
320 | // re-activates sleeping task |
---|
321 | _set_task_slot( remote_x, |
---|
322 | remote_y, |
---|
323 | remote_p, |
---|
324 | ltid, |
---|
325 | CTX_RUN_ID, // CTX_RUN slot |
---|
326 | 1 ); // running |
---|
327 | |
---|
328 | // requires a context switch for remote processor running the waiting task |
---|
329 | _xcu_send_wti( remote_cluster, |
---|
330 | remote_p, |
---|
331 | 0 ); // don't force context switch if not idle |
---|
332 | |
---|
333 | #if GIET_DEBUG_IRQS // we don't take the TTY lock to avoid deadlock |
---|
334 | unsigned int procid = _get_procid(); |
---|
335 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
336 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
337 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
338 | |
---|
339 | _puts("\n[IRQS DEBUG] Processor["); |
---|
340 | _putd(x ); |
---|
341 | _puts(","); |
---|
342 | _putd(y ); |
---|
343 | _puts(","); |
---|
344 | _putd(p ); |
---|
345 | _puts("] enters _bdv_isr() at cycle "); |
---|
346 | _putd(_get_proctime() ); |
---|
347 | _puts("\n for task "); |
---|
348 | _putd(ltid ); |
---|
349 | _puts(" running on processor["); |
---|
350 | _putd(remote_x ); |
---|
351 | _puts(","); |
---|
352 | _putd(remore_y ); |
---|
353 | _puts(","); |
---|
354 | _putd(remote_p ); |
---|
355 | _puts(" / bdv status = "); |
---|
356 | _putx(_bdv_status ); |
---|
357 | _puts("\n"); |
---|
358 | #endif |
---|
359 | |
---|
360 | } |
---|
361 | |
---|
362 | |
---|
363 | // Local Variables: |
---|
364 | // tab-width: 4 |
---|
365 | // c-basic-offset: 4 |
---|
366 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
367 | // indent-tabs-mode: nil |
---|
368 | // End: |
---|
369 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
370 | |
---|