source: soft/giet_vm/giet_drivers/bdv_driver.c @ 496

Last change on this file since 496 was 496, checked in by alain, 9 years ago

1) Introduce access functions to MMC intrumentation registers.
2) Use _printf for error or debug messages.

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