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