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