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

Last change on this file since 452 was 437, checked in by alain, 10 years ago

Introducing dynamic allocation of peripheral channel(TTY, NIC, TIM, CMA)
Removint the ICU driver : ICU component not supported anymore.
Removing the FBF driver.

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