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

Last change on this file since 437 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
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 <tty_driver.h>
23#include <ctx_handler.h>
24
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
29///////////////////////////////////////////////////////////////////////////////
30// BDV global variables
31///////////////////////////////////////////////////////////////////////////////
32
33#define in_unckdata __attribute__((section (".unckdata")))
34#define in_kdata __attribute__((section (".kdata")))
35
36#if GIET_NO_HARD_CC
37in_unckdata giet_lock_t           _bdv_lock __attribute__((aligned(64)));
38in_unckdata volatile unsigned int _bdv_status;
39in_unckdata volatile unsigned int _bdv_gtid;
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
45
46///////////////////////////////////////////////////////////////////////////////
47// This low_level function returns the value contained in register (index).
48///////////////////////////////////////////////////////////////////////////////
49unsigned int _bdv_get_register( unsigned int index )
50{
51    unsigned int* vaddr = (unsigned int*)SEG_IOC_BASE + index;
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{
61    unsigned int* vaddr = (unsigned int*)SEG_IOC_BASE + index;
62    _io_extended_write( vaddr, value );
63}
64
65///////////////////////////////////////////////////////////////////////////////
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.
70// - mode       : BOOT / KERNEL / USER
71// - lba        : first block index on the external storage.
72// - buf_paddr  : physical base address of the memory buffer.
73// - count      : number of blocks to be transfered.
74// Returns 0 if success, > 0 if error.
75///////////////////////////////////////////////////////////////////////////////
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) 
81{
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);
86
87#if GIET_DEBUG_IOC_DRIVER
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");
107#endif
108
109    unsigned int       error = 0;
110
111    // get the lock protecting BDV
112    _get_lock(&_bdv_lock);
113
114#if GIET_DEBUG_IOC_DRIVER
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");
124#endif
125
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 );
131
132    // In BOOT mode, we launch transfer, and poll the BDV_STATUS
133    // register because IRQs are masked.
134    if ( mode == IOC_BOOT_MODE ) 
135    {
136        // Launch transfert
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 );
139
140#if GIET_DEBUG_IOC_DRIVER
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");
148#endif
149        unsigned int status;
150        do
151        {
152            status = _bdv_get_register( BLOCK_DEVICE_STATUS );
153
154#if GIET_DEBUG_IOC_DRIVER
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");
162#endif
163        }
164        while( (status != BLOCK_DEVICE_READ_SUCCESS)  &&
165               (status != BLOCK_DEVICE_READ_ERROR)    &&
166               (status != BLOCK_DEVICE_WRITE_SUCCESS) &&
167               (status != BLOCK_DEVICE_WRITE_ERROR)   );      // busy waiting
168
169        // analyse status
170        error = ( (status == BLOCK_DEVICE_READ_ERROR) ||
171                  (status == BLOCK_DEVICE_WRITE_ERROR) );
172
173        // release lock
174        _release_lock(&_bdv_lock);     
175    }
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
183    {
184        unsigned int save_sr;
185        unsigned int ltid = _get_current_task_id();
186
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 ); 
195
196        // set _bdv_gtid and reset runnable
197        _bdv_gtid = (procid<<16) + ltid;
198        _set_task_slot( x, y, p, ltid, CTX_RUN_ID, 0 ); 
199       
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  );
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("] launch transfer in nterrupt mode\n");
212#endif
213
214        // deschedule task
215        _ctx_switch();                     
216
217#if GIET_DEBUG_IOC_DRIVER
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");
225#endif
226        // restore SR
227        _it_restore( &save_sr );
228
229        // analyse status
230        error = ( (_bdv_status == BLOCK_DEVICE_READ_ERROR) ||
231                  (_bdv_status == BLOCK_DEVICE_WRITE_ERROR) );
232
233        // reset _bdv_status and release lock
234        _bdv_status = BLOCK_DEVICE_IDLE; 
235        _release_lock(&_bdv_lock);     
236    }
237
238#if GIET_DEBUG_IOC_DRIVER
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");
250#endif
251
252    return error;
253} // end _bdv_access()
254
255///////////////////////////////////////////////////////////////////////////////
256//      External functions
257///////////////////////////////////////////////////////////////////////////////
258
259////////////////////////
260unsigned int _bdv_init()
261{
262    if ( _bdv_get_register( BLOCK_DEVICE_BLOCK_SIZE ) != 512 )
263    {
264        _puts("\n[GIET ERROR] in _bdv_init() : block size must be 512 bytes\n");
265        return 1; 
266    }
267
268    _bdv_set_register( BLOCK_DEVICE_IRQ_ENABLE, 0 );
269    return 0;
270}
271
272////////////////////////////////////////////////
273unsigned int _bdv_read( unsigned int       mode, 
274                        unsigned int       lba, 
275                        unsigned long long buffer, 
276                        unsigned int       count) 
277{
278    return _bdv_access( 1,        // read access
279                        mode, 
280                        lba,
281                        buffer,
282                        count );
283}
284
285/////////////////////////////////////////////////
286unsigned int _bdv_write( unsigned int       mode, 
287                         unsigned int       lba, 
288                         unsigned long long buffer, 
289                         unsigned int       count ) 
290{
291    return _bdv_access( 0,        // write access
292                        mode, 
293                        lba,
294                        buffer,
295                        count );
296}
297
298//////////////////////////////
299unsigned int _bdv_get_status()
300{
301    return _bdv_get_register( BLOCK_DEVICE_STATUS );
302}
303
304//////////////////////////////////
305unsigned int _bdv_get_block_size()
306{
307    return _bdv_get_register( BLOCK_DEVICE_BLOCK_SIZE );
308}
309
310/////////////////////////////////////
311void _bdv_isr( unsigned int irq_type,   // HWI / WTI
312               unsigned int irq_id,     // index returned by ICU
313               unsigned int channel )   // unused
314{
315    // get BDV status (and reset IRQ)
316    unsigned int status =  _bdv_get_register( BLOCK_DEVICE_STATUS ); 
317
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; 
324
325    // identify task waiting on BDV
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);
332
333    // re-activates sleeping task
334    _set_task_slot( remote_x,
335                    remote_y,
336                    remote_p,
337                    ltid,       
338                    CTX_RUN_ID,  // CTX_RUN slot
339                    1 );         // running
340
341    // requires a context switch for remote processor running the waiting task
342    _xcu_send_wti( remote_cluster,   
343                   remote_p, 
344                   0 );          // don't force context switch if not idle
345
346#if GIET_DEBUG_IRQS  // we don't take the TTY lock to avoid deadlock
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
352_puts("\n[IRQS DEBUG] Processor[");
353_putd(x );
354_puts(",");
355_putd(y );
356_puts(",");
357_putd(p );
358_puts("] enters _bdv_isr() at cycle ");
359_putd(_get_proctime() );
360_puts("\n  for task ");
361_putd(ltid );
362_puts(" running on processor[");
363_putd(remote_x );
364_puts(",");
365_putd(remore_y );
366_puts(",");
367_putd(remote_p );
368_puts(" / bdv status = ");
369_putx(_bdv_status );
370_puts("\n");
371#endif
372
373}
374
375
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.