[283] | 1 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[284] | 2 | // File : bdv_driver.c |
---|
| 3 | // Date : 23/05/2013 |
---|
| 4 | // Author : alain greiner |
---|
| 5 | // Maintainer: cesar fuguet |
---|
[283] | 6 | // Copyright (c) UPMC-LIP6 |
---|
| 7 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[295] | 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. |
---|
[283] | 14 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 15 | |
---|
| 16 | #include <giet_config.h> |
---|
[320] | 17 | #include <hard_config.h> |
---|
[295] | 18 | #include <bdv_driver.h> |
---|
| 19 | #include <xcu_driver.h> |
---|
[283] | 20 | #include <ioc_driver.h> |
---|
| 21 | #include <utils.h> |
---|
[456] | 22 | #include <tty0.h> |
---|
[283] | 23 | #include <ctx_handler.h> |
---|
| 24 | |
---|
| 25 | /////////////////////////////////////////////////////////////////////////////// |
---|
[295] | 26 | // BDV global variables |
---|
| 27 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 28 | |
---|
[496] | 29 | __attribute__((section(".kdata"))) |
---|
[456] | 30 | spin_lock_t _bdv_lock __attribute__((aligned(64))); |
---|
[496] | 31 | |
---|
| 32 | __attribute__((section(".kdata"))) |
---|
[456] | 33 | unsigned int _bdv_status; |
---|
[496] | 34 | |
---|
| 35 | __attribute__((section(".kdata"))) |
---|
[456] | 36 | unsigned int _bdv_gtid; |
---|
[295] | 37 | |
---|
| 38 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 39 | // This low_level function returns the value contained in register (index). |
---|
| 40 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 41 | unsigned int _bdv_get_register( unsigned int index ) |
---|
| 42 | { |
---|
[320] | 43 | unsigned int* vaddr = (unsigned int*)SEG_IOC_BASE + index; |
---|
[295] | 44 | return _io_extended_read( vaddr ); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 48 | // This low-level function set a new value in register (index). |
---|
| 49 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 50 | void _bdv_set_register( unsigned int index, |
---|
| 51 | unsigned int value ) |
---|
| 52 | { |
---|
[320] | 53 | unsigned int* vaddr = (unsigned int*)SEG_IOC_BASE + index; |
---|
[295] | 54 | _io_extended_write( vaddr, value ); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | /////////////////////////////////////////////////////////////////////////////// |
---|
[283] | 58 | // This function transfer data between a memory buffer and the block device. |
---|
| 59 | // The buffer lentgth is (count*block_size) bytes. |
---|
| 60 | // Arguments are: |
---|
| 61 | // - to_mem : from external storage to memory when non 0. |
---|
[289] | 62 | // - mode : BOOT / KERNEL / USER |
---|
[283] | 63 | // - lba : first block index on the external storage. |
---|
[289] | 64 | // - buf_paddr : physical base address of the memory buffer. |
---|
[283] | 65 | // - count : number of blocks to be transfered. |
---|
| 66 | // Returns 0 if success, > 0 if error. |
---|
| 67 | /////////////////////////////////////////////////////////////////////////////// |
---|
[295] | 68 | static unsigned int _bdv_access( unsigned int to_mem, |
---|
| 69 | unsigned int mode, |
---|
| 70 | unsigned int lba, |
---|
| 71 | unsigned long long buf_paddr, |
---|
| 72 | unsigned int count) |
---|
[283] | 73 | { |
---|
[426] | 74 | unsigned int procid = _get_procid(); |
---|
| 75 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
| 76 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH) - 1); |
---|
| 77 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
[283] | 78 | |
---|
[313] | 79 | #if GIET_DEBUG_IOC_DRIVER |
---|
[437] | 80 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
| 81 | _putd( x ); |
---|
| 82 | _puts(","); |
---|
| 83 | _putd( y ); |
---|
| 84 | _puts(","); |
---|
| 85 | _putd( p ); |
---|
| 86 | _puts("] enters at cycle "); |
---|
| 87 | _putd( _get_proctime() ); |
---|
| 88 | _puts("\n - to_mem = "); |
---|
| 89 | _putd( to_mem ); |
---|
| 90 | _puts("\n - mode = "); |
---|
| 91 | _putd( mode ); |
---|
| 92 | _puts("\n - paddr = "); |
---|
| 93 | _putl( buf_paddr ); |
---|
| 94 | _puts("\n - sectors = "); |
---|
| 95 | _putd( count ); |
---|
| 96 | _puts("\n - lba = "); |
---|
| 97 | _putx( lba ); |
---|
| 98 | _puts("\n"); |
---|
[283] | 99 | #endif |
---|
| 100 | |
---|
[295] | 101 | unsigned int error = 0; |
---|
[283] | 102 | |
---|
[295] | 103 | // get the lock protecting BDV |
---|
[469] | 104 | _spin_lock_acquire( &_bdv_lock ); |
---|
[283] | 105 | |
---|
[426] | 106 | #if GIET_DEBUG_IOC_DRIVER |
---|
[437] | 107 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
| 108 | _putd( x ); |
---|
| 109 | _puts(","); |
---|
| 110 | _putd( y ); |
---|
| 111 | _puts(","); |
---|
| 112 | _putd( p ); |
---|
[456] | 113 | _puts("] get _bdv_lock at cycle "); |
---|
| 114 | _putd( _get_proctime() ); |
---|
[437] | 115 | _puts("\n"); |
---|
[426] | 116 | #endif |
---|
| 117 | |
---|
[295] | 118 | // set device registers |
---|
| 119 | _bdv_set_register( BLOCK_DEVICE_BUFFER , (unsigned int)buf_paddr ); |
---|
| 120 | _bdv_set_register( BLOCK_DEVICE_BUFFER_EXT, (unsigned int)(buf_paddr>>32) ); |
---|
| 121 | _bdv_set_register( BLOCK_DEVICE_COUNT , count ); |
---|
| 122 | _bdv_set_register( BLOCK_DEVICE_LBA , lba ); |
---|
[283] | 123 | |
---|
[295] | 124 | // In BOOT mode, we launch transfer, and poll the BDV_STATUS |
---|
| 125 | // register because IRQs are masked. |
---|
| 126 | if ( mode == IOC_BOOT_MODE ) |
---|
[283] | 127 | { |
---|
| 128 | // Launch transfert |
---|
[295] | 129 | if (to_mem == 0) _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_WRITE ); |
---|
| 130 | else _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_READ ); |
---|
[283] | 131 | |
---|
[333] | 132 | #if GIET_DEBUG_IOC_DRIVER |
---|
[437] | 133 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
| 134 | _putd( x ); |
---|
| 135 | _puts(","); |
---|
| 136 | _putd( y ); |
---|
| 137 | _puts(","); |
---|
| 138 | _putd( p ); |
---|
[496] | 139 | _puts("] launch transfer in polling mode at cycle "); |
---|
| 140 | _putd( _get_proctime() ); |
---|
| 141 | _puts("\n"); |
---|
[333] | 142 | #endif |
---|
[283] | 143 | unsigned int status; |
---|
[289] | 144 | do |
---|
[283] | 145 | { |
---|
[295] | 146 | status = _bdv_get_register( BLOCK_DEVICE_STATUS ); |
---|
[283] | 147 | |
---|
[313] | 148 | #if GIET_DEBUG_IOC_DRIVER |
---|
[437] | 149 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
| 150 | _putd( x ); |
---|
| 151 | _puts(","); |
---|
| 152 | _putd( y ); |
---|
| 153 | _puts(","); |
---|
| 154 | _putd( p ); |
---|
| 155 | _puts("] wait on BDV_STATUS register ...\n"); |
---|
[283] | 156 | #endif |
---|
[289] | 157 | } |
---|
| 158 | while( (status != BLOCK_DEVICE_READ_SUCCESS) && |
---|
| 159 | (status != BLOCK_DEVICE_READ_ERROR) && |
---|
| 160 | (status != BLOCK_DEVICE_WRITE_SUCCESS) && |
---|
[295] | 161 | (status != BLOCK_DEVICE_WRITE_ERROR) ); // busy waiting |
---|
[283] | 162 | |
---|
| 163 | // analyse status |
---|
| 164 | error = ( (status == BLOCK_DEVICE_READ_ERROR) || |
---|
| 165 | (status == BLOCK_DEVICE_WRITE_ERROR) ); |
---|
| 166 | |
---|
| 167 | // release lock |
---|
[469] | 168 | _spin_lock_release( &_bdv_lock ); |
---|
[283] | 169 | } |
---|
[295] | 170 | // in USER or KERNEL mode, we deschedule the task. |
---|
| 171 | // When the task is rescheduled, we check the _bdv_status variable, |
---|
| 172 | // and release the lock. |
---|
| 173 | // We need a critical section, because we must reset the RUN bit |
---|
| 174 | // before to launch the transfer, and we don't want to be descheduled |
---|
| 175 | // between these two operations. |
---|
| 176 | else |
---|
[283] | 177 | { |
---|
[295] | 178 | unsigned int save_sr; |
---|
| 179 | unsigned int ltid = _get_current_task_id(); |
---|
[283] | 180 | |
---|
[295] | 181 | // activates BDV interrupts |
---|
| 182 | _bdv_set_register( BLOCK_DEVICE_IRQ_ENABLE, 1 ); |
---|
| 183 | |
---|
| 184 | // set the _bdv_status variable |
---|
| 185 | _bdv_status = BLOCK_DEVICE_BUSY; |
---|
| 186 | |
---|
| 187 | // enters critical section |
---|
| 188 | _it_disable( &save_sr ); |
---|
[320] | 189 | |
---|
[295] | 190 | // set _bdv_gtid and reset runnable |
---|
[426] | 191 | _bdv_gtid = (procid<<16) + ltid; |
---|
| 192 | _set_task_slot( x, y, p, ltid, CTX_RUN_ID, 0 ); |
---|
[283] | 193 | |
---|
[295] | 194 | // launch transfer |
---|
| 195 | if (to_mem == 0) _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_WRITE ); |
---|
| 196 | else _bdv_set_register( BLOCK_DEVICE_OP, BLOCK_DEVICE_READ ); |
---|
[283] | 197 | |
---|
[333] | 198 | #if GIET_DEBUG_IOC_DRIVER |
---|
[437] | 199 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
| 200 | _putd( x ); |
---|
| 201 | _puts(","); |
---|
| 202 | _putd( y ); |
---|
| 203 | _puts(","); |
---|
| 204 | _putd( p ); |
---|
[496] | 205 | _puts("] launch transfer in interrupt mode at cycle "); |
---|
| 206 | _putd( _get_proctime() ); |
---|
| 207 | _puts("\n"); |
---|
[333] | 208 | #endif |
---|
[437] | 209 | |
---|
[283] | 210 | // deschedule task |
---|
| 211 | _ctx_switch(); |
---|
| 212 | |
---|
[426] | 213 | #if GIET_DEBUG_IOC_DRIVER |
---|
[437] | 214 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
| 215 | _putd( x ); |
---|
| 216 | _puts(","); |
---|
| 217 | _putd( y ); |
---|
| 218 | _puts(","); |
---|
| 219 | _putd( p ); |
---|
| 220 | _puts("] resume execution after descheduling\n"); |
---|
[426] | 221 | #endif |
---|
[295] | 222 | // restore SR |
---|
| 223 | _it_restore( &save_sr ); |
---|
| 224 | |
---|
[283] | 225 | // analyse status |
---|
[295] | 226 | error = ( (_bdv_status == BLOCK_DEVICE_READ_ERROR) || |
---|
| 227 | (_bdv_status == BLOCK_DEVICE_WRITE_ERROR) ); |
---|
[283] | 228 | |
---|
[295] | 229 | // reset _bdv_status and release lock |
---|
| 230 | _bdv_status = BLOCK_DEVICE_IDLE; |
---|
[469] | 231 | _spin_lock_release( &_bdv_lock ); |
---|
[283] | 232 | } |
---|
| 233 | |
---|
[313] | 234 | #if GIET_DEBUG_IOC_DRIVER |
---|
[437] | 235 | _puts("\n[BDV DEBUG] _bdv_access() : P["); |
---|
| 236 | _putd( x ); |
---|
| 237 | _puts(","); |
---|
| 238 | _putd( y ); |
---|
| 239 | _puts(","); |
---|
| 240 | _putd( p ); |
---|
| 241 | _puts("] exit at cycle "); |
---|
| 242 | _putd( _get_proctime() ); |
---|
| 243 | _puts(" / error = "); |
---|
[456] | 244 | _putd( error ); |
---|
[437] | 245 | _puts("\n"); |
---|
[283] | 246 | #endif |
---|
| 247 | |
---|
| 248 | return error; |
---|
| 249 | } // end _bdv_access() |
---|
| 250 | |
---|
| 251 | /////////////////////////////////////////////////////////////////////////////// |
---|
[437] | 252 | // External functions |
---|
[283] | 253 | /////////////////////////////////////////////////////////////////////////////// |
---|
[437] | 254 | |
---|
| 255 | //////////////////////// |
---|
[295] | 256 | unsigned int _bdv_init() |
---|
[283] | 257 | { |
---|
[295] | 258 | if ( _bdv_get_register( BLOCK_DEVICE_BLOCK_SIZE ) != 512 ) |
---|
[283] | 259 | { |
---|
[437] | 260 | _puts("\n[GIET ERROR] in _bdv_init() : block size must be 512 bytes\n"); |
---|
[283] | 261 | return 1; |
---|
| 262 | } |
---|
| 263 | |
---|
[295] | 264 | _bdv_set_register( BLOCK_DEVICE_IRQ_ENABLE, 0 ); |
---|
[283] | 265 | return 0; |
---|
| 266 | } |
---|
| 267 | |
---|
[437] | 268 | //////////////////////////////////////////////// |
---|
[295] | 269 | unsigned int _bdv_read( unsigned int mode, |
---|
| 270 | unsigned int lba, |
---|
| 271 | unsigned long long buffer, |
---|
| 272 | unsigned int count) |
---|
[283] | 273 | { |
---|
| 274 | return _bdv_access( 1, // read access |
---|
| 275 | mode, |
---|
| 276 | lba, |
---|
| 277 | buffer, |
---|
| 278 | count ); |
---|
| 279 | } |
---|
| 280 | |
---|
[437] | 281 | ///////////////////////////////////////////////// |
---|
[295] | 282 | unsigned int _bdv_write( unsigned int mode, |
---|
| 283 | unsigned int lba, |
---|
| 284 | unsigned long long buffer, |
---|
| 285 | unsigned int count ) |
---|
[283] | 286 | { |
---|
| 287 | return _bdv_access( 0, // write access |
---|
| 288 | mode, |
---|
| 289 | lba, |
---|
| 290 | buffer, |
---|
| 291 | count ); |
---|
| 292 | } |
---|
| 293 | |
---|
[437] | 294 | ////////////////////////////// |
---|
[295] | 295 | unsigned int _bdv_get_status() |
---|
[283] | 296 | { |
---|
[295] | 297 | return _bdv_get_register( BLOCK_DEVICE_STATUS ); |
---|
[283] | 298 | } |
---|
| 299 | |
---|
[437] | 300 | ////////////////////////////////// |
---|
[295] | 301 | unsigned int _bdv_get_block_size() |
---|
[283] | 302 | { |
---|
[295] | 303 | return _bdv_get_register( BLOCK_DEVICE_BLOCK_SIZE ); |
---|
[283] | 304 | } |
---|
| 305 | |
---|
[437] | 306 | ///////////////////////////////////// |
---|
[295] | 307 | void _bdv_isr( unsigned int irq_type, // HWI / WTI |
---|
| 308 | unsigned int irq_id, // index returned by ICU |
---|
| 309 | unsigned int channel ) // unused |
---|
| 310 | { |
---|
[297] | 311 | // get BDV status (and reset IRQ) |
---|
| 312 | unsigned int status = _bdv_get_register( BLOCK_DEVICE_STATUS ); |
---|
[295] | 313 | |
---|
[297] | 314 | // check status: does nothing if IDLE or BUSY |
---|
| 315 | if ( (status == BLOCK_DEVICE_IDLE) || |
---|
| 316 | (status == BLOCK_DEVICE_BUSY) ) return; |
---|
| 317 | |
---|
| 318 | // save status in kernel buffer _bdv_status |
---|
| 319 | _bdv_status = status; |
---|
[295] | 320 | |
---|
| 321 | // identify task waiting on BDV |
---|
[426] | 322 | unsigned int remote_procid = _bdv_gtid>>16; |
---|
| 323 | unsigned int ltid = _bdv_gtid & 0xFFFF; |
---|
| 324 | unsigned int remote_cluster = remote_procid >> P_WIDTH; |
---|
| 325 | unsigned int remote_x = remote_cluster >> Y_WIDTH; |
---|
| 326 | unsigned int remote_y = remote_cluster & ((1<<Y_WIDTH)-1); |
---|
| 327 | unsigned int remote_p = remote_procid & ((1<<P_WIDTH)-1); |
---|
[295] | 328 | |
---|
[297] | 329 | // re-activates sleeping task |
---|
[426] | 330 | _set_task_slot( remote_x, |
---|
| 331 | remote_y, |
---|
| 332 | remote_p, |
---|
| 333 | ltid, |
---|
[297] | 334 | CTX_RUN_ID, // CTX_RUN slot |
---|
| 335 | 1 ); // running |
---|
| 336 | |
---|
| 337 | // requires a context switch for remote processor running the waiting task |
---|
[426] | 338 | _xcu_send_wti( remote_cluster, |
---|
| 339 | remote_p, |
---|
[297] | 340 | 0 ); // don't force context switch if not idle |
---|
| 341 | |
---|
[295] | 342 | #if GIET_DEBUG_IRQS // we don't take the TTY lock to avoid deadlock |
---|
[426] | 343 | unsigned int procid = _get_procid(); |
---|
| 344 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
| 345 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
| 346 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
| 347 | |
---|
[295] | 348 | _puts("\n[IRQS DEBUG] Processor["); |
---|
| 349 | _putd(x ); |
---|
| 350 | _puts(","); |
---|
| 351 | _putd(y ); |
---|
| 352 | _puts(","); |
---|
[426] | 353 | _putd(p ); |
---|
[295] | 354 | _puts("] enters _bdv_isr() at cycle "); |
---|
| 355 | _putd(_get_proctime() ); |
---|
| 356 | _puts("\n for task "); |
---|
| 357 | _putd(ltid ); |
---|
| 358 | _puts(" running on processor["); |
---|
[426] | 359 | _putd(remote_x ); |
---|
[295] | 360 | _puts(","); |
---|
[496] | 361 | _putd(remote_y ); |
---|
[295] | 362 | _puts(","); |
---|
[426] | 363 | _putd(remote_p ); |
---|
[295] | 364 | _puts(" / bdv status = "); |
---|
| 365 | _putx(_bdv_status ); |
---|
| 366 | _puts("\n"); |
---|
| 367 | #endif |
---|
| 368 | |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | |
---|
[283] | 372 | // Local Variables: |
---|
| 373 | // tab-width: 4 |
---|
| 374 | // c-basic-offset: 4 |
---|
| 375 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 376 | // indent-tabs-mode: nil |
---|
| 377 | // End: |
---|
| 378 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 379 | |
---|