| 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 |
|---|
| 29 | extern 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"))) |
|---|
| 37 | spin_lock_t _bdv_lock __attribute__((aligned(64))); |
|---|
| 38 | |
|---|
| 39 | // global index of the waiting task (only used in descheduling mode) |
|---|
| 40 | __attribute__((section(".kdata"))) |
|---|
| 41 | unsigned int _bdv_gtid; |
|---|
| 42 | |
|---|
| 43 | // BDV peripheral status (only used in descheduling mode) |
|---|
| 44 | __attribute__((section(".kdata"))) |
|---|
| 45 | unsigned int _bdv_status; |
|---|
| 46 | |
|---|
| 47 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 48 | // This low_level function returns the value contained in register (index). |
|---|
| 49 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 50 | unsigned 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 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 59 | void _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 | ///////////////////////////////////////////////////// |
|---|
| 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) |
|---|
| 76 | { |
|---|
| 77 | unsigned int procid = _get_procid(); |
|---|
| 78 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
|---|
| 79 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH) - 1); |
|---|
| 80 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
|---|
| 81 | |
|---|
| 82 | #if GIET_DEBUG_IOC |
|---|
| 83 | if ( _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 lock protecting BDV |
|---|
| 100 | _spin_lock_acquire( &_bdv_lock ); |
|---|
| 101 | |
|---|
| 102 | // set device registers |
|---|
| 103 | _bdv_set_register( BLOCK_DEVICE_BUFFER , (unsigned int)buf_paddr ); |
|---|
| 104 | _bdv_set_register( BLOCK_DEVICE_BUFFER_EXT, (unsigned int)(buf_paddr>>32) ); |
|---|
| 105 | _bdv_set_register( BLOCK_DEVICE_COUNT , count ); |
|---|
| 106 | _bdv_set_register( BLOCK_DEVICE_LBA , lba ); |
|---|
| 107 | |
|---|
| 108 | #if USE_IOB // software L2/L3 cache coherence |
|---|
| 109 | if ( to_mem ) _mmc_inval( buf_paddr, count<<9 ); |
|---|
| 110 | else _mmc_sync( buf_paddr, count<<9 ); |
|---|
| 111 | #endif // end software L2/L3 cache coherence |
|---|
| 112 | |
|---|
| 113 | ///////////////////////////////////////////////////////////////////// |
|---|
| 114 | // In synchronous mode, we launch transfer, |
|---|
| 115 | // and poll the BDV_STATUS register until completion. |
|---|
| 116 | ///////////////////////////////////////////////////////////////////// |
|---|
| 117 | if ( use_irq == 0 ) |
|---|
| 118 | { |
|---|
| 119 | // Launch transfert |
|---|
| 120 | if (to_mem == 0) _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_WRITE ); |
|---|
| 121 | else _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_READ ); |
|---|
| 122 | |
|---|
| 123 | #if GIET_DEBUG_IOC |
|---|
| 124 | if ( _get_proctime() > GIET_DEBUG_IOC ) |
|---|
| 125 | _printf("\n[BDV DEBUG] _bdv_access() : P[%d,%d,%d] launch transfer" |
|---|
| 126 | " in polling mode at cycle %d\n", |
|---|
| 127 | x , y , p , _get_proctime() ); |
|---|
| 128 | #endif |
|---|
| 129 | |
|---|
| 130 | do |
|---|
| 131 | { |
|---|
| 132 | status = _bdv_get_register( BLOCK_DEVICE_STATUS ); |
|---|
| 133 | |
|---|
| 134 | #if GIET_DEBUG_IOC |
|---|
| 135 | if ( _get_proctime() > GIET_DEBUG_IOC ) |
|---|
| 136 | _printf("\n[BDV DEBUG] _bdv_access() : P[%d,%d,%d] wait on BDV_STATUS ...\n", |
|---|
| 137 | x , y , p ); |
|---|
| 138 | #endif |
|---|
| 139 | } |
|---|
| 140 | while( (status != BLOCK_DEVICE_READ_SUCCESS) && |
|---|
| 141 | (status != BLOCK_DEVICE_READ_ERROR) && |
|---|
| 142 | (status != BLOCK_DEVICE_WRITE_SUCCESS) && |
|---|
| 143 | (status != BLOCK_DEVICE_WRITE_ERROR) ); // busy waiting |
|---|
| 144 | |
|---|
| 145 | // analyse status |
|---|
| 146 | error = ( (status == BLOCK_DEVICE_READ_ERROR) || |
|---|
| 147 | (status == BLOCK_DEVICE_WRITE_ERROR) ); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | ///////////////////////////////////////////////////////////////// |
|---|
| 151 | // in descheduling mode, we deschedule the task |
|---|
| 152 | // and use an interrupt to reschedule the task. |
|---|
| 153 | // We need a critical section, because we must reset the RUN bit |
|---|
| 154 | // before to launch the transfer, and we don't want to be |
|---|
| 155 | // descheduled between these two operations. |
|---|
| 156 | ///////////////////////////////////////////////////////////////// |
|---|
| 157 | else |
|---|
| 158 | { |
|---|
| 159 | unsigned int save_sr; |
|---|
| 160 | unsigned int ltid = _get_current_task_id(); |
|---|
| 161 | |
|---|
| 162 | // activates BDV interrupt |
|---|
| 163 | _bdv_set_register( BLOCK_DEVICE_IRQ_ENABLE, 1 ); |
|---|
| 164 | |
|---|
| 165 | // set _bdv_gtid |
|---|
| 166 | _bdv_gtid = (procid<<16) + ltid; |
|---|
| 167 | |
|---|
| 168 | // enters critical section |
|---|
| 169 | _it_disable( &save_sr ); |
|---|
| 170 | |
|---|
| 171 | // Set NORUN_MASK_IOC bit |
|---|
| 172 | static_scheduler_t* psched = (static_scheduler_t*)_schedulers[x][y][p]; |
|---|
| 173 | _atomic_or( &psched->context[ltid][CTX_NORUN_ID] , NORUN_MASK_IOC ); |
|---|
| 174 | |
|---|
| 175 | // launch transfer |
|---|
| 176 | if (to_mem == 0) _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_WRITE ); |
|---|
| 177 | else _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_READ ); |
|---|
| 178 | |
|---|
| 179 | #if GIET_DEBUG_IOC |
|---|
| 180 | if ( _get_proctime() > GIET_DEBUG_IOC ) |
|---|
| 181 | _printf("\n[BDV DEBUG] _bdv_access() : P[%d,%d,%d] launch transfer" |
|---|
| 182 | " in descheduling mode at cycle %d\n", |
|---|
| 183 | x , y , p , _get_proctime() ); |
|---|
| 184 | #endif |
|---|
| 185 | |
|---|
| 186 | // deschedule task |
|---|
| 187 | _ctx_switch(); |
|---|
| 188 | |
|---|
| 189 | #if GIET_DEBUG_IOC |
|---|
| 190 | if ( _get_proctime() > GIET_DEBUG_IOC ) |
|---|
| 191 | _printf("\n[BDV DEBUG] _bdv_access() : P[%d,%d,%d] resume execution at cycle %d\n", |
|---|
| 192 | x , y , p , _get_proctime() ); |
|---|
| 193 | #endif |
|---|
| 194 | |
|---|
| 195 | // restore SR |
|---|
| 196 | _it_restore( &save_sr ); |
|---|
| 197 | |
|---|
| 198 | // analyse status |
|---|
| 199 | error = ( (_bdv_status == BLOCK_DEVICE_READ_ERROR) || |
|---|
| 200 | (_bdv_status == BLOCK_DEVICE_WRITE_ERROR) ); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | // release lock |
|---|
| 204 | _spin_lock_release( &_bdv_lock ); |
|---|
| 205 | |
|---|
| 206 | #if GIET_DEBUG_IOC |
|---|
| 207 | if ( _get_proctime() > GIET_DEBUG_IOC ) |
|---|
| 208 | _printf("\n[BDV DEBUG] _bdv_access() : P[%d,%d,%d] exit at cycle %d\n", |
|---|
| 209 | x , y , p , _get_proctime() ); |
|---|
| 210 | #endif |
|---|
| 211 | |
|---|
| 212 | return error; |
|---|
| 213 | } // end _bdv_access() |
|---|
| 214 | |
|---|
| 215 | //////////////////////// |
|---|
| 216 | unsigned int _bdv_init() |
|---|
| 217 | { |
|---|
| 218 | if ( _bdv_get_register( BLOCK_DEVICE_BLOCK_SIZE ) != 512 ) |
|---|
| 219 | { |
|---|
| 220 | _puts("\n[GIET ERROR] in _bdv_init() : block size must be 512 bytes\n"); |
|---|
| 221 | return 1; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | _bdv_set_register( BLOCK_DEVICE_IRQ_ENABLE, 0 ); |
|---|
| 225 | return 0; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | ///////////////////////////////////// |
|---|
| 229 | void _bdv_isr( unsigned int irq_type, // HWI / WTI |
|---|
| 230 | unsigned int irq_id, // index returned by ICU |
|---|
| 231 | unsigned int channel ) // unused |
|---|
| 232 | { |
|---|
| 233 | // get BDV status and reset BDV_IRQ |
|---|
| 234 | unsigned int status = _bdv_get_register( BLOCK_DEVICE_STATUS ); |
|---|
| 235 | |
|---|
| 236 | // check status: does nothing if IDLE or BUSY |
|---|
| 237 | if ( (status == BLOCK_DEVICE_IDLE) || |
|---|
| 238 | (status == BLOCK_DEVICE_BUSY) ) return; |
|---|
| 239 | |
|---|
| 240 | // register status in global variable |
|---|
| 241 | _bdv_status = status; |
|---|
| 242 | |
|---|
| 243 | // identify task waiting on BDV |
|---|
| 244 | unsigned int procid = _bdv_gtid>>16; |
|---|
| 245 | unsigned int ltid = _bdv_gtid & 0xFFFF; |
|---|
| 246 | unsigned int cluster = procid >> P_WIDTH; |
|---|
| 247 | unsigned int x = cluster >> Y_WIDTH; |
|---|
| 248 | unsigned int y = cluster & ((1<<Y_WIDTH)-1); |
|---|
| 249 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
|---|
| 250 | |
|---|
| 251 | // Reset NORUN_MASK_IOC bit |
|---|
| 252 | static_scheduler_t* psched = (static_scheduler_t*)_schedulers[x][y][p]; |
|---|
| 253 | unsigned int* ptr = &psched->context[ltid][CTX_NORUN_ID]; |
|---|
| 254 | _atomic_and( ptr , ~NORUN_MASK_IOC ); |
|---|
| 255 | |
|---|
| 256 | // send a WAKUP WTI to processor running the sleeping task |
|---|
| 257 | _xcu_send_wti( cluster, |
|---|
| 258 | p, |
|---|
| 259 | 0 ); // don't force context switch |
|---|
| 260 | |
|---|
| 261 | #if GIET_DEBUG_IOC |
|---|
| 262 | unsigned int pid = _get_procid(); |
|---|
| 263 | unsigned int c_x = pid >> (Y_WIDTH + P_WIDTH); |
|---|
| 264 | unsigned int c_y = (pid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
|---|
| 265 | unsigned int c_p = pid & ((1<<P_WIDTH)-1); |
|---|
| 266 | if ( _get_proctime() > GIET_DEBUG_IOC ) |
|---|
| 267 | _printf("\n[BDV DEBUG] Processor[%d,%d,%d] enters _bdv_isr() at cycle %d\n" |
|---|
| 268 | " for task %d running on P[%d,%d,%d] / bdv_status = %x\n", |
|---|
| 269 | c_x , c_y , c_p , _get_proctime() , |
|---|
| 270 | ltid , x , y , p , status ); |
|---|
| 271 | #endif |
|---|
| 272 | |
|---|
| 273 | } // end bdv_isr() |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | // Local Variables: |
|---|
| 277 | // tab-width: 4 |
|---|
| 278 | // c-basic-offset: 4 |
|---|
| 279 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
|---|
| 280 | // indent-tabs-mode: nil |
|---|
| 281 | // End: |
|---|
| 282 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
|---|
| 283 | |
|---|