[258] | 1 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[289] | 2 | // File : ioc_driver.c |
---|
| 3 | // Date : 23/05/2013 |
---|
| 4 | // Author : alain greiner |
---|
| 5 | // Maintainer : cesar fuguet |
---|
[258] | 6 | // Copyright (c) UPMC-LIP6 |
---|
| 7 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[413] | 8 | // Implementation note: |
---|
[258] | 9 | // |
---|
[413] | 10 | // 1) In order to share the code, the two _ioc_read() and _ioc_write() functions |
---|
| 11 | // call the same _ioc_access() function. |
---|
[258] | 12 | // |
---|
[413] | 13 | // 2) The IOMMU is not supported yet, but the method is the following: |
---|
| 14 | // A fixed size 2 Mbytes vseg is allocated to the IOC peripheral, in the I/O |
---|
| 15 | // virtual space, and the user buffer is dynamically remapped to one single |
---|
| 16 | // big page in the IOMMU page table. |
---|
[258] | 17 | // The user buffer is unmapped by the _ioc_completed() function when |
---|
| 18 | // the transfer is completed. |
---|
| 19 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 20 | |
---|
| 21 | #include <giet_config.h> |
---|
| 22 | #include <ioc_driver.h> |
---|
[295] | 23 | #include <bdv_driver.h> |
---|
| 24 | #include <hba_driver.h> |
---|
| 25 | #include <sdc_driver.h> |
---|
| 26 | #include <rdk_driver.h> |
---|
[258] | 27 | #include <utils.h> |
---|
| 28 | #include <tty_driver.h> |
---|
| 29 | #include <iob_driver.h> |
---|
| 30 | #include <ctx_handler.h> |
---|
| 31 | #include <mmc_driver.h> |
---|
| 32 | #include <vmem.h> |
---|
| 33 | |
---|
[320] | 34 | #if !defined( SEG_IOC_BASE ) |
---|
| 35 | # error: You must define SEG_IOC_BASE in the hard_config.h file |
---|
| 36 | #endif |
---|
| 37 | |
---|
[258] | 38 | #if !defined( USE_IOB ) |
---|
| 39 | # error: You must define USE_IOB in the hard_config.h file |
---|
| 40 | #endif |
---|
| 41 | |
---|
| 42 | #if !defined(GIET_USE_IOMMU) |
---|
| 43 | # error: You must define GIET_USE_IOMMU in the giet_config.h file |
---|
| 44 | #endif |
---|
| 45 | |
---|
[295] | 46 | #if (USE_IOC_BDV + USE_IOC_SPI + USE_IOC_HBA + USE_IOC_RDK) != 1 |
---|
| 47 | # error: You must use only one IOC controller type (BDV or SPI or HBA or RDK) |
---|
[289] | 48 | #endif |
---|
| 49 | |
---|
[295] | 50 | #if USE_IOC_BDV |
---|
[289] | 51 | # include <bdv_driver.h> |
---|
| 52 | #endif |
---|
| 53 | |
---|
[295] | 54 | #if USE_IOC_SPI |
---|
[289] | 55 | # include <sdc_driver.h> |
---|
| 56 | #endif |
---|
| 57 | |
---|
[295] | 58 | #if USE_IOC_HBA |
---|
[289] | 59 | # include <hba_driver.h> |
---|
| 60 | #endif |
---|
| 61 | |
---|
[295] | 62 | #if USE_IOC_RDK |
---|
| 63 | # include <rdk_driver.h> |
---|
| 64 | #endif |
---|
[289] | 65 | |
---|
[295] | 66 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 67 | // IOC global variables |
---|
| 68 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 69 | |
---|
[258] | 70 | #define in_unckdata __attribute__((section (".unckdata"))) |
---|
| 71 | |
---|
| 72 | in_unckdata volatile unsigned int _ioc_iommu_ix1 = 0; |
---|
| 73 | in_unckdata volatile unsigned int _ioc_iommu_npages; |
---|
| 74 | |
---|
| 75 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 76 | // This function transfer data between a memory buffer and the block device. |
---|
| 77 | // The buffer lentgth is (count*block_size) bytes. |
---|
| 78 | // Arguments are: |
---|
| 79 | // - to_mem : from external storage to memory when non 0. |
---|
[295] | 80 | // - mode : BOOT_PA / BOOT_VA / KERNEL / USER |
---|
[258] | 81 | // - lba : first block index on the external storage. |
---|
| 82 | // - buf_vaddr : virtual base address of the memory buffer. |
---|
| 83 | // - count : number of blocks to be transfered. |
---|
| 84 | // Returns 0 if success, > 0 if error. |
---|
| 85 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 86 | static unsigned int _ioc_access( unsigned int to_mem, |
---|
[295] | 87 | unsigned int channel, |
---|
[258] | 88 | unsigned int mode, |
---|
| 89 | unsigned int lba, |
---|
| 90 | unsigned int buf_vaddr, |
---|
| 91 | unsigned int count) |
---|
| 92 | { |
---|
| 93 | |
---|
| 94 | #if GIET_DEBUG_IOC_DRIVER |
---|
[295] | 95 | unsigned int procid = _get_procid(); |
---|
[426] | 96 | unsigned int x = procid >> (Y_WIDTH + P_WIDTH); |
---|
| 97 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
| 98 | unsigned int p = procid & ((1<<P_WIDTH)-1); |
---|
[413] | 99 | _printf("\n[IOC DEBUG] _ioc_access() : P[%d,%d,%d] enters at cycle %d\n" |
---|
[295] | 100 | " - channel = %d\n" |
---|
| 101 | " - mode = %d\n" |
---|
[313] | 102 | " - vaddr = %x\n" |
---|
[295] | 103 | " - sectors = %d\n" |
---|
[313] | 104 | " - lba = %x\n", |
---|
[426] | 105 | x, y, p, _get_proctime(), channel, mode, buf_vaddr, count, lba ); |
---|
[258] | 106 | #endif |
---|
| 107 | |
---|
| 108 | unsigned int error; // return value |
---|
| 109 | unsigned int pt_vbase; // page table vbase address |
---|
[413] | 110 | unsigned int ppn; // user buffer first page PPN |
---|
| 111 | unsigned int flags; // user buffer protection flags |
---|
| 112 | paddr_t buf_paddr; // user buffer physical address (if no IOMMU), |
---|
[258] | 113 | |
---|
| 114 | // check buffer alignment |
---|
| 115 | if ((unsigned int) buf_vaddr & 0x3) |
---|
| 116 | { |
---|
[413] | 117 | _printf("\n[IOC ERROR] in _ioc_access() : buffer not word aligned\n"); |
---|
[295] | 118 | _exit(); |
---|
[258] | 119 | } |
---|
| 120 | |
---|
[295] | 121 | // check channel |
---|
| 122 | if ( (USE_IOC_HBA == 0) && (channel > 0) ) |
---|
| 123 | { |
---|
[413] | 124 | _printf("\n[IOC ERROR] in _ioc_access() : channel must be 0 when HBA not used\n"); |
---|
[295] | 125 | _exit(); |
---|
| 126 | } |
---|
| 127 | |
---|
[258] | 128 | unsigned int length = count << 9; // count * 512 bytes |
---|
| 129 | |
---|
[295] | 130 | // computing memory buffer physical address |
---|
| 131 | if ( (mode == IOC_BOOT_MODE) && ((_get_mmu_mode() & 0x4) == 0) ) // identity mapping |
---|
[258] | 132 | { |
---|
| 133 | buf_paddr = (paddr_t)buf_vaddr; |
---|
| 134 | } |
---|
[295] | 135 | else // V2P translation required |
---|
[258] | 136 | { |
---|
| 137 | // get page table virtual address |
---|
| 138 | pt_vbase = _get_context_slot(CTX_PTAB_ID); |
---|
| 139 | |
---|
[413] | 140 | // get user buffer first page ppn and flags |
---|
| 141 | unsigned int ko = _v2p_translate( (page_table_t*)pt_vbase, |
---|
| 142 | buf_vaddr >> 12, |
---|
| 143 | &ppn, |
---|
| 144 | &flags ); |
---|
| 145 | // check access rights |
---|
| 146 | if ( ko ) |
---|
[258] | 147 | { |
---|
[413] | 148 | _printf("\n[IOC ERROR] in _ioc_access() : buffer unmapped\n"); |
---|
| 149 | _exit(); |
---|
| 150 | } |
---|
[258] | 151 | |
---|
[413] | 152 | if ( (mode == IOC_USER_MODE) && ((flags & PTE_U) == 0) ) |
---|
| 153 | { |
---|
| 154 | _printf("\n[IOC ERROR] in _ioc_access() : buffer not user accessible\n"); |
---|
| 155 | _exit(); |
---|
| 156 | } |
---|
[258] | 157 | |
---|
[413] | 158 | if ( ((flags & PTE_W) == 0 ) && to_mem ) |
---|
| 159 | { |
---|
| 160 | _printf("\n[IOC ERROR] in _ioc_access() : buffer not writable\n"); |
---|
| 161 | _exit(); |
---|
| 162 | } |
---|
[258] | 163 | |
---|
[413] | 164 | buf_paddr = (((paddr_t)ppn) << 12) | (buf_vaddr & 0xFFF); |
---|
[258] | 165 | } |
---|
| 166 | |
---|
[413] | 167 | // cache coherence for both L1 & L2 caches |
---|
[258] | 168 | |
---|
| 169 | if ( to_mem ) // memory write : invalidate data caches |
---|
| 170 | { |
---|
[413] | 171 | // L1 cache (only if L1 cache coherence not guaranteed by hardware) |
---|
| 172 | if ( GIET_NO_HARD_CC ) _dcache_buf_invalidate( buf_vaddr, length ); |
---|
[258] | 173 | |
---|
[413] | 174 | // L2 cache (only if we use an IO-Bridge component in architecture)) |
---|
[297] | 175 | if ( USE_IOB ) _mmc_inval( buf_paddr, length ); |
---|
[258] | 176 | } |
---|
| 177 | else // memory read : update data caches |
---|
| 178 | { |
---|
[295] | 179 | // L1 cache : nothing to do for L1 write-through |
---|
[258] | 180 | |
---|
[413] | 181 | // L2 cache (only if we use an IO-Bridge component in architecture)) |
---|
[297] | 182 | if ( USE_IOB ) _mmc_sync( buf_paddr, length ); |
---|
[258] | 183 | } |
---|
| 184 | |
---|
[295] | 185 | // select the proper physical device |
---|
| 186 | |
---|
[413] | 187 | #if ( USE_IOC_BDV ) |
---|
[295] | 188 | if (to_mem) error = _bdv_read ( mode, lba, buf_paddr, count); |
---|
| 189 | else error = _bdv_write( mode, lba, buf_paddr, count); |
---|
| 190 | #elif ( USE_IOC_SPI ) |
---|
| 191 | if (to_mem) error = _sdc_read (mode, lba, buf_paddr, count); |
---|
| 192 | else error = _sdc_write(mode, lba, buf_paddr, count); |
---|
| 193 | #elif ( USE_IOC_HBA ) |
---|
| 194 | if (to_mem) error = _hba_read (channel, mode, lba, buf_paddr, count); |
---|
| 195 | else error = _hba_write(channel, mode, lba, buf_paddr, count); |
---|
| 196 | #elif ( USE_IOC_RDK ) |
---|
| 197 | if (to_mem) error = _rdk_read (lba, buf_vaddr, count); |
---|
| 198 | else error = _rdk_write(lba, buf_vaddr, count); |
---|
| 199 | #endif |
---|
| 200 | |
---|
[258] | 201 | return error; |
---|
| 202 | } // end _ioc_access() |
---|
| 203 | |
---|
[413] | 204 | ////////////////////////////////////////////// |
---|
[289] | 205 | unsigned int _ioc_init( unsigned int channel ) |
---|
[258] | 206 | { |
---|
[295] | 207 | |
---|
| 208 | #if ( USE_IOC_BDV ) |
---|
| 209 | |
---|
| 210 | return _bdv_init(); |
---|
| 211 | |
---|
| 212 | #elif ( USE_IOC_SPI ) |
---|
| 213 | |
---|
| 214 | return _sdc_init(); |
---|
| 215 | |
---|
| 216 | #elif ( USE_IOC_HBA ) |
---|
| 217 | |
---|
[289] | 218 | return _hba_init( channel ); |
---|
[295] | 219 | |
---|
| 220 | #elif ( USE_IOC_RDK ) |
---|
| 221 | |
---|
| 222 | return _rdk_init(); |
---|
| 223 | |
---|
[289] | 224 | #endif |
---|
[295] | 225 | |
---|
[258] | 226 | } |
---|
| 227 | |
---|
[413] | 228 | ////////////////////////////////////////////// |
---|
[295] | 229 | unsigned int _ioc_read( unsigned int channel, |
---|
| 230 | unsigned int mode, |
---|
[258] | 231 | unsigned int lba, |
---|
| 232 | void* buffer, |
---|
| 233 | unsigned int count) |
---|
| 234 | { |
---|
| 235 | return _ioc_access( 1, // read access |
---|
[295] | 236 | channel, |
---|
[258] | 237 | mode, |
---|
| 238 | lba, |
---|
| 239 | (unsigned int) buffer, |
---|
| 240 | count ); |
---|
| 241 | } |
---|
| 242 | |
---|
[413] | 243 | ////////////////////////////////////////////// |
---|
[295] | 244 | unsigned int _ioc_write( unsigned int channel, |
---|
| 245 | unsigned int mode, |
---|
[258] | 246 | unsigned int lba, |
---|
| 247 | const void* buffer, |
---|
| 248 | unsigned int count ) |
---|
| 249 | { |
---|
| 250 | return _ioc_access( 0, // write access |
---|
[295] | 251 | channel, |
---|
[258] | 252 | mode, |
---|
| 253 | lba, |
---|
| 254 | (unsigned int) buffer, |
---|
| 255 | count ); |
---|
| 256 | } |
---|
| 257 | |
---|
[413] | 258 | ///////////////////////////////////////////////////// |
---|
[295] | 259 | unsigned int _ioc_get_status( unsigned int channel ) |
---|
[258] | 260 | { |
---|
[295] | 261 | |
---|
| 262 | #if ( USE_IOC_BDV ) |
---|
| 263 | |
---|
| 264 | return _bdv_get_status( ); |
---|
| 265 | |
---|
| 266 | #elif ( USE_IOC_SPI ) |
---|
| 267 | |
---|
| 268 | return _sdc_get_status( ); |
---|
| 269 | |
---|
| 270 | #elif ( USE_IOC_HBA ) |
---|
| 271 | |
---|
| 272 | return _hba_get_status( channel ); |
---|
| 273 | |
---|
| 274 | #elif ( USE_IOC_RDK ) |
---|
| 275 | |
---|
| 276 | _printf("[GIET ERROR] _ioc_get_status() should not be called"); |
---|
| 277 | _printf(" when RAMDISK is used...\n"); |
---|
| 278 | _exit(); |
---|
| 279 | |
---|
| 280 | return 0; |
---|
| 281 | |
---|
[289] | 282 | #endif |
---|
[295] | 283 | |
---|
[258] | 284 | } |
---|
| 285 | |
---|
[413] | 286 | ////////////////////////////////// |
---|
[258] | 287 | unsigned int _ioc_get_block_size() |
---|
| 288 | { |
---|
[295] | 289 | |
---|
| 290 | #if ( USE_IOC_BDV ) |
---|
| 291 | |
---|
[289] | 292 | return _bdv_get_block_size(); |
---|
[295] | 293 | |
---|
| 294 | #elif ( USE_IOC_SPI ) |
---|
| 295 | |
---|
[289] | 296 | return _sdc_get_block_size(); |
---|
[295] | 297 | |
---|
| 298 | #elif ( USE_IOC_HBA ) |
---|
| 299 | |
---|
[289] | 300 | return _hba_get_block_size(); |
---|
[295] | 301 | |
---|
| 302 | #elif ( USE_IOC_RDK ) |
---|
| 303 | |
---|
| 304 | return 512; |
---|
| 305 | |
---|
[289] | 306 | #endif |
---|
[295] | 307 | |
---|
[258] | 308 | } |
---|
| 309 | |
---|
| 310 | |
---|
| 311 | // Local Variables: |
---|
| 312 | // tab-width: 4 |
---|
| 313 | // c-basic-offset: 4 |
---|
| 314 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 315 | // indent-tabs-mode: nil |
---|
| 316 | // End: |
---|
| 317 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 318 | |
---|