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

Last change on this file since 432 was 426, checked in by alain, 10 years ago

Introducing fixed format (X_WIDTH / Y_WIDTH / P_WIDTH ) for processor index.

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