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