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