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
RevLine 
[283]1///////////////////////////////////////////////////////////////////////////////////
[284]2// File      : bdv_driver.c
3// Date      : 23/05/2013
4// Author    : alain greiner
5// Maintainer: cesar fuguet
[283]6// Copyright (c) UPMC-LIP6
7///////////////////////////////////////////////////////////////////////////////////
[295]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.
[283]14///////////////////////////////////////////////////////////////////////////////////
15
16#include <giet_config.h>
[320]17#include <hard_config.h>
[295]18#include <bdv_driver.h>
19#include <xcu_driver.h>
[283]20#include <ioc_driver.h>
21#include <utils.h>
[456]22#include <tty0.h>
[283]23#include <ctx_handler.h>
24
25///////////////////////////////////////////////////////////////////////////////
[295]26// BDV global variables
27///////////////////////////////////////////////////////////////////////////////
28
[496]29__attribute__((section(".kdata")))
[456]30spin_lock_t  _bdv_lock __attribute__((aligned(64)));
[496]31
32__attribute__((section(".kdata")))
[456]33unsigned int _bdv_status;
[496]34
35__attribute__((section(".kdata")))
[456]36unsigned int _bdv_gtid;
[295]37
38///////////////////////////////////////////////////////////////////////////////
39// This low_level function returns the value contained in register (index).
40///////////////////////////////////////////////////////////////////////////////
41unsigned int _bdv_get_register( unsigned int index )
42{
[320]43    unsigned int* vaddr = (unsigned int*)SEG_IOC_BASE + index;
[295]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{
[320]53    unsigned int* vaddr = (unsigned int*)SEG_IOC_BASE + index;
[295]54    _io_extended_write( vaddr, value );
55}
56
57///////////////////////////////////////////////////////////////////////////////
[283]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.
[289]62// - mode       : BOOT / KERNEL / USER
[283]63// - lba        : first block index on the external storage.
[289]64// - buf_paddr  : physical base address of the memory buffer.
[283]65// - count      : number of blocks to be transfered.
66// Returns 0 if success, > 0 if error.
67///////////////////////////////////////////////////////////////////////////////
[295]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) 
[283]73{
[426]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);
[283]78
[313]79#if GIET_DEBUG_IOC_DRIVER
[437]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");
[283]99#endif
100
[295]101    unsigned int       error = 0;
[283]102
[295]103    // get the lock protecting BDV
[469]104    _spin_lock_acquire( &_bdv_lock );
[283]105
[426]106#if GIET_DEBUG_IOC_DRIVER
[437]107_puts("\n[BDV DEBUG] _bdv_access() : P[");
108_putd( x );
109_puts(",");
110_putd( y );
111_puts(",");
112_putd( p );
[456]113_puts("] get _bdv_lock at cycle ");
114_putd( _get_proctime() );
[437]115_puts("\n");
[426]116#endif
117
[295]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 );
[283]123
[295]124    // In BOOT mode, we launch transfer, and poll the BDV_STATUS
125    // register because IRQs are masked.
126    if ( mode == IOC_BOOT_MODE ) 
[283]127    {
128        // Launch transfert
[295]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 );
[283]131
[333]132#if GIET_DEBUG_IOC_DRIVER
[437]133_puts("\n[BDV DEBUG] _bdv_access() : P[");
134_putd( x );
135_puts(",");
136_putd( y );
137_puts(",");
138_putd( p );
[496]139_puts("] launch transfer in polling mode at cycle ");
140_putd( _get_proctime() );
141_puts("\n");
[333]142#endif
[283]143        unsigned int status;
[289]144        do
[283]145        {
[295]146            status = _bdv_get_register( BLOCK_DEVICE_STATUS );
[283]147
[313]148#if GIET_DEBUG_IOC_DRIVER
[437]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");
[283]156#endif
[289]157        }
158        while( (status != BLOCK_DEVICE_READ_SUCCESS)  &&
159               (status != BLOCK_DEVICE_READ_ERROR)    &&
160               (status != BLOCK_DEVICE_WRITE_SUCCESS) &&
[295]161               (status != BLOCK_DEVICE_WRITE_ERROR)   );      // busy waiting
[283]162
163        // analyse status
164        error = ( (status == BLOCK_DEVICE_READ_ERROR) ||
165                  (status == BLOCK_DEVICE_WRITE_ERROR) );
166
167        // release lock
[469]168        _spin_lock_release( &_bdv_lock );     
[283]169    }
[295]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
[283]177    {
[295]178        unsigned int save_sr;
179        unsigned int ltid = _get_current_task_id();
[283]180
[295]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 ); 
[320]189
[295]190        // set _bdv_gtid and reset runnable
[426]191        _bdv_gtid = (procid<<16) + ltid;
192        _set_task_slot( x, y, p, ltid, CTX_RUN_ID, 0 ); 
[283]193       
[295]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  );
[283]197
[333]198#if GIET_DEBUG_IOC_DRIVER
[437]199_puts("\n[BDV DEBUG] _bdv_access() : P[");
200_putd( x );
201_puts(",");
202_putd( y );
203_puts(",");
204_putd( p );
[496]205_puts("] launch transfer in interrupt mode at cycle ");
206_putd( _get_proctime() );
207_puts("\n");
[333]208#endif
[437]209
[283]210        // deschedule task
211        _ctx_switch();                     
212
[426]213#if GIET_DEBUG_IOC_DRIVER
[437]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");
[426]221#endif
[295]222        // restore SR
223        _it_restore( &save_sr );
224
[283]225        // analyse status
[295]226        error = ( (_bdv_status == BLOCK_DEVICE_READ_ERROR) ||
227                  (_bdv_status == BLOCK_DEVICE_WRITE_ERROR) );
[283]228
[295]229        // reset _bdv_status and release lock
230        _bdv_status = BLOCK_DEVICE_IDLE; 
[469]231        _spin_lock_release( &_bdv_lock );     
[283]232    }
233
[313]234#if GIET_DEBUG_IOC_DRIVER
[437]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 = ");
[456]244_putd( error );
[437]245_puts("\n");
[283]246#endif
247
248    return error;
249} // end _bdv_access()
250
251///////////////////////////////////////////////////////////////////////////////
[437]252//      External functions
[283]253///////////////////////////////////////////////////////////////////////////////
[437]254
255////////////////////////
[295]256unsigned int _bdv_init()
[283]257{
[295]258    if ( _bdv_get_register( BLOCK_DEVICE_BLOCK_SIZE ) != 512 )
[283]259    {
[437]260        _puts("\n[GIET ERROR] in _bdv_init() : block size must be 512 bytes\n");
[283]261        return 1; 
262    }
263
[295]264    _bdv_set_register( BLOCK_DEVICE_IRQ_ENABLE, 0 );
[283]265    return 0;
266}
267
[437]268////////////////////////////////////////////////
[295]269unsigned int _bdv_read( unsigned int       mode, 
270                        unsigned int       lba, 
271                        unsigned long long buffer, 
272                        unsigned int       count) 
[283]273{
274    return _bdv_access( 1,        // read access
275                        mode, 
276                        lba,
277                        buffer,
278                        count );
279}
280
[437]281/////////////////////////////////////////////////
[295]282unsigned int _bdv_write( unsigned int       mode, 
283                         unsigned int       lba, 
284                         unsigned long long buffer, 
285                         unsigned int       count ) 
[283]286{
287    return _bdv_access( 0,        // write access
288                        mode, 
289                        lba,
290                        buffer,
291                        count );
292}
293
[437]294//////////////////////////////
[295]295unsigned int _bdv_get_status()
[283]296{
[295]297    return _bdv_get_register( BLOCK_DEVICE_STATUS );
[283]298}
299
[437]300//////////////////////////////////
[295]301unsigned int _bdv_get_block_size()
[283]302{
[295]303    return _bdv_get_register( BLOCK_DEVICE_BLOCK_SIZE );
[283]304}
305
[437]306/////////////////////////////////////
[295]307void _bdv_isr( unsigned int irq_type,   // HWI / WTI
308               unsigned int irq_id,     // index returned by ICU
309               unsigned int channel )   // unused
310{
[297]311    // get BDV status (and reset IRQ)
312    unsigned int status =  _bdv_get_register( BLOCK_DEVICE_STATUS ); 
[295]313
[297]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; 
[295]320
321    // identify task waiting on BDV
[426]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);
[295]328
[297]329    // re-activates sleeping task
[426]330    _set_task_slot( remote_x,
331                    remote_y,
332                    remote_p,
333                    ltid,       
[297]334                    CTX_RUN_ID,  // CTX_RUN slot
335                    1 );         // running
336
337    // requires a context switch for remote processor running the waiting task
[426]338    _xcu_send_wti( remote_cluster,   
339                   remote_p, 
[297]340                   0 );          // don't force context switch if not idle
341
[295]342#if GIET_DEBUG_IRQS  // we don't take the TTY lock to avoid deadlock
[426]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
[295]348_puts("\n[IRQS DEBUG] Processor[");
349_putd(x );
350_puts(",");
351_putd(y );
352_puts(",");
[426]353_putd(p );
[295]354_puts("] enters _bdv_isr() at cycle ");
355_putd(_get_proctime() );
356_puts("\n  for task ");
357_putd(ltid );
358_puts(" running on processor[");
[426]359_putd(remote_x );
[295]360_puts(",");
[496]361_putd(remote_y );
[295]362_puts(",");
[426]363_putd(remote_p );
[295]364_puts(" / bdv status = ");
365_putx(_bdv_status );
366_puts("\n");
367#endif
368
369}
370
371
[283]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.