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

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

Major release: Change the task model to implement the POSIX threads API.

  • The shell "exec" and "kill" commands can be used to activate/de-activate the applications.
  • The "pause", "resume", and "context" commands can be used to stop, restart, a single thtead or to display the thread context.

This version has been tested on the following multi-threaded applications,
that have been modified to use the POSIX threads:

  • classif
  • convol
  • transpose
  • gameoflife
  • raycast
File size: 10.1 KB
Line 
1///////////////////////////////////////////////////////////////////////////////////
2// File      : bdv_driver.c
3// Date      : 23/05/2013
4// Author    : alain greiner cesar fuguet
5// Copyright (c) UPMC-LIP6
6///////////////////////////////////////////////////////////////////////////////////
7// Implementation notes:
8// All accesses to BDV registers are done by the two
9// _bdv_set_register() and _bdv_get_register() low-level functions,
10// that are handling virtual / physical extended addressing.
11///////////////////////////////////////////////////////////////////////////////////
12
13#include <giet_config.h>
14#include <hard_config.h>
15#include <bdv_driver.h>
16#include <xcu_driver.h>
17#include <mmc_driver.h>
18#include <kernel_locks.h>
19#include <utils.h>
20#include <tty0.h>
21#include <ctx_handler.h>
22#include <irq_handler.h>
23
24///////////////////////////////////////////////////////////////////////////////
25//      Extern variables
26///////////////////////////////////////////////////////////////////////////////
27
28// allocated in the boot.c or kernel_init.c files
29extern static_scheduler_t* _schedulers[X_SIZE][Y_SIZE][NB_PROCS_MAX]; 
30 
31///////////////////////////////////////////////////////////////////////////////
32//      Global variables
33///////////////////////////////////////////////////////////////////////////////
34
35// lock protecting single channel BDV peripheral
36__attribute__((section(".kdata")))
37spin_lock_t  _bdv_lock __attribute__((aligned(64)));
38
39// global index of the waiting thread (only used in descheduling mode)
40__attribute__((section(".kdata")))
41unsigned int _bdv_trdid;
42
43// BDV peripheral status (only used in descheduling mode)
44__attribute__((section(".kdata")))
45unsigned int _bdv_status;
46
47///////////////////////////////////////////////////////////////////////////////
48// This low_level function returns the value contained in register (index).
49///////////////////////////////////////////////////////////////////////////////
50unsigned int _bdv_get_register( unsigned int index )
51{
52    unsigned int* vaddr = (unsigned int*)SEG_IOC_BASE + index;
53    return _io_extended_read( vaddr );
54}
55
56///////////////////////////////////////////////////////////////////////////////
57// This low-level function set a new value in register (index).
58///////////////////////////////////////////////////////////////////////////////
59void _bdv_set_register( unsigned int index,
60                        unsigned int value ) 
61{
62    unsigned int* vaddr = (unsigned int*)SEG_IOC_BASE + index;
63    _io_extended_write( vaddr, value );
64}
65
66///////////////////////////////////////////////////////////////////////////////
67//      Extern functions
68///////////////////////////////////////////////////////////////////////////////
69
70/////////////////////////////////////////////////////
71unsigned int _bdv_access( unsigned int       use_irq,
72                          unsigned int       to_mem,
73                          unsigned int       lba,
74                          unsigned long long buf_paddr,
75                          unsigned int       count) 
76{
77
78#if GIET_DEBUG_IOC
79unsigned int procid  = _get_procid();
80unsigned int x       = procid >> (Y_WIDTH + P_WIDTH);
81unsigned int y       = (procid >> P_WIDTH) & ((1<<Y_WIDTH) - 1);
82unsigned int p       = procid & ((1<<P_WIDTH)-1);
83if ( _get_proctime() > GIET_DEBUG_IOC )
84_printf("\n[BDV DEBUG] P[%d,%d,%d] enters _bdv_access at cycle %d\n"
85        "  use_irq = %d / to_mem = %d / lba = %x / paddr = %l / count = %d\n",
86        x , y , p , _get_proctime() , use_irq , to_mem , lba , buf_paddr, count );
87#endif
88
89    // check buffer alignment
90    if( buf_paddr & 0x3F )
91    {
92        _printf("\n[BDV ERROR] in _bdv_access() : buffer not cache ligne aligned\n");
93        return -1;
94    }
95
96    unsigned int error;
97    unsigned int status;
98
99    // get the BDV lock and register it in task context
100    static_scheduler_t*  psched = _get_sched();
101    unsigned int         ltid   = _get_thread_ltid();
102    _spin_lock_acquire( &_bdv_lock );
103    _atomic_or( &psched->context[ltid].slot[CTX_LOCKS_ID] , LOCKS_MASK_BDV ); 
104
105    // set device registers
106    _bdv_set_register( BLOCK_DEVICE_BUFFER    , (unsigned int)buf_paddr );
107    _bdv_set_register( BLOCK_DEVICE_BUFFER_EXT, (unsigned int)(buf_paddr>>32) );
108    _bdv_set_register( BLOCK_DEVICE_COUNT     , count );
109    _bdv_set_register( BLOCK_DEVICE_LBA       , lba );
110
111#if USE_IOB    // software L2/L3 cache coherence
112    if ( to_mem )  _mmc_inval( buf_paddr, count<<9 );
113    else           _mmc_sync( buf_paddr, count<<9 );
114#endif     // end software L2/L3 cache coherence
115
116    /////////////////////////////////////////////////////////////////////
117    // In synchronous mode, we launch transfer,
118    // and poll the BDV_STATUS register until completion.
119    /////////////////////////////////////////////////////////////////////
120    if ( use_irq == 0 ) 
121    {
122        // Launch transfert
123        if (to_mem == 0) _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_WRITE );
124        else             _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_READ );
125
126#if GIET_DEBUG_IOC
127if ( _get_proctime() > GIET_DEBUG_IOC )
128_printf("\n[BDV DEBUG] _bdv_access() : P[%d,%d,%d] launch transfer"
129        " in polling mode at cycle %d\n",
130        x , y , p , _get_proctime() );
131#endif
132
133        do
134        {
135            status = _bdv_get_register( BLOCK_DEVICE_STATUS );
136
137#if GIET_DEBUG_IOC
138if ( _get_proctime() > GIET_DEBUG_IOC )
139_printf("\n[BDV DEBUG] _bdv_access() : P[%d,%d,%d] wait on BDV_STATUS ...\n",
140        x , y , p );
141#endif
142        }
143        while( (status != BLOCK_DEVICE_READ_SUCCESS)  &&
144               (status != BLOCK_DEVICE_READ_ERROR)    &&
145               (status != BLOCK_DEVICE_WRITE_SUCCESS) &&
146               (status != BLOCK_DEVICE_WRITE_ERROR)   );      // busy waiting
147
148        // analyse status
149        error = ( (status == BLOCK_DEVICE_READ_ERROR) ||
150                  (status == BLOCK_DEVICE_WRITE_ERROR) );
151    }
152
153    /////////////////////////////////////////////////////////////////
154    // in descheduling mode, we deschedule the thread
155    // and use an interrupt to reschedule the thread.
156    // We need a critical section, because we must reset the RUN bit
157        // before to launch the transfer, and we don't want to be
158    // descheduled between these two operations.
159    /////////////////////////////////////////////////////////////////
160    else
161    {
162        unsigned int save_sr;
163
164        // activates BDV interrupt
165        _bdv_set_register( BLOCK_DEVICE_IRQ_ENABLE, 1 );
166
167        // set _bdv_trdid
168        _bdv_trdid = _get_thread_trdid();
169
170        // enters critical section
171        _it_disable( &save_sr ); 
172
173        // Set NORUN_MASK_IOC bit
174        static_scheduler_t* psched  = (static_scheduler_t*)_get_sched();
175        unsigned int ltid = psched->current;
176        _atomic_or( &psched->context[ltid].slot[CTX_NORUN_ID] , NORUN_MASK_IOC );
177       
178        // launch transfer
179        if (to_mem == 0) _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_WRITE );
180        else             _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_READ  );
181
182#if GIET_DEBUG_IOC
183if ( _get_proctime() > GIET_DEBUG_IOC )
184_printf("\n[BDV DEBUG] _bdv_access() : P[%d,%d,%d] launch transfer"
185        " in descheduling mode at cycle %d\n",
186        x , y , p , _get_proctime() );
187#endif
188
189        // deschedule thread
190        _ctx_switch();                     
191
192#if GIET_DEBUG_IOC
193if ( _get_proctime() > GIET_DEBUG_IOC )
194_printf("\n[BDV DEBUG] _bdv_access() : P[%d,%d,%d] resume execution at cycle %d\n",
195        x , y , p , _get_proctime() );
196#endif
197
198        // restore SR
199        _it_restore( &save_sr );
200
201        // analyse status
202        error = ( (_bdv_status == BLOCK_DEVICE_READ_ERROR) ||
203                  (_bdv_status == BLOCK_DEVICE_WRITE_ERROR) );
204    }
205
206    // release BDV lock and clear task context
207    _spin_lock_release( &_bdv_lock );     
208    _atomic_and( &psched->context[ltid].slot[CTX_LOCKS_ID] , ~LOCKS_MASK_BDV ); 
209
210#if GIET_DEBUG_IOC
211if ( _get_proctime() > GIET_DEBUG_IOC )
212_printf("\n[BDV DEBUG] _bdv_access() : P[%d,%d,%d] exit at cycle %d\n",
213        x , y , p , _get_proctime() );
214#endif
215
216    return error;
217} // end _bdv_access()
218
219////////////////////////
220unsigned int _bdv_init()
221{
222    if ( _bdv_get_register( BLOCK_DEVICE_BLOCK_SIZE ) != 512 )
223    {
224        _puts("\n[GIET ERROR] in _bdv_init() : block size must be 512 bytes\n");
225        return 1; 
226    }
227
228    _bdv_set_register( BLOCK_DEVICE_IRQ_ENABLE, 0 );
229    return 0;
230}
231
232/////////////////////////////////////
233void _bdv_isr( unsigned int irq_type,   // HWI / WTI
234               unsigned int irq_id,     // index returned by ICU
235               unsigned int channel )   // unused
236{
237    // get BDV status and reset BDV_IRQ
238    unsigned int status =  _bdv_get_register( BLOCK_DEVICE_STATUS ); 
239
240    // check status: does nothing if IDLE or BUSY
241    if ( (status == BLOCK_DEVICE_IDLE) ||
242         (status == BLOCK_DEVICE_BUSY) )   return;
243 
244    // register status in global variable
245    _bdv_status = status;
246
247    // identify thread waiting on BDV
248    unsigned int x       = (_bdv_trdid >> 24) & 0xFF;
249    unsigned int y       = (_bdv_trdid >> 16) & 0xFF;
250    unsigned int p       = (_bdv_trdid >>  8) & 0xFF;
251    unsigned int ltid    = (_bdv_trdid      ) & 0xFF;
252
253    // Reset NORUN_MASK_IOC bit
254    static_scheduler_t* psched  = (static_scheduler_t*)_schedulers[x][y][p];
255    unsigned int*       ptr     = &psched->context[ltid].slot[CTX_NORUN_ID];
256    _atomic_and( ptr , ~NORUN_MASK_IOC );
257
258    // send a WAKUP WTI to processor running the sleeping thread
259    _xcu_send_wti( (x<<Y_WIDTH) + y,
260                   p, 
261                   0 );          // don't force context switch
262
263#if GIET_DEBUG_IOC 
264unsigned int pid  = _get_procid();
265unsigned int c_x  = pid >> (Y_WIDTH + P_WIDTH);
266unsigned int c_y  = (pid >> P_WIDTH) & ((1<<Y_WIDTH)-1);
267unsigned int c_p  = pid & ((1<<P_WIDTH)-1);
268if ( _get_proctime() > GIET_DEBUG_IOC )
269_printf("\n[BDV DEBUG] Processor[%d,%d,%d] enters _bdv_isr() at cycle %d\n"
270        "  for thread %d running on P[%d,%d,%d] / bdv_status = %x\n",
271        c_x , c_y , c_p , _get_proctime() ,
272        ltid , x , y , p , status );
273#endif
274
275} // end bdv_isr()
276
277
278// Local Variables:
279// tab-width: 4
280// c-basic-offset: 4
281// c-file-offsets:((innamespace . 0)(inline-open . 0))
282// indent-tabs-mode: nil
283// End:
284// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
285
Note: See TracBrowser for help on using the repository browser.