| 1 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | // File : drivers.c |
|---|
| 3 | // Date : 01/04/2012 |
|---|
| 4 | // Author : alain greiner |
|---|
| 5 | // Copyright (c) UPMC-LIP6 |
|---|
| 6 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 7 | // The drivers.c and drivers.h files are part ot the GIET-VM nano kernel. |
|---|
| 8 | // They contains the drivers for the peripherals available in the SoCLib library: |
|---|
| 9 | // - vci_multi_tty |
|---|
| 10 | // - vci_multi_timer |
|---|
| 11 | // - vci_multi_dma |
|---|
| 12 | // - vci_multi_icu |
|---|
| 13 | // - vci_xicu & vci_multi_icu |
|---|
| 14 | // - vci_gcd |
|---|
| 15 | // - vci_frame_buffer |
|---|
| 16 | // - vci_block_device |
|---|
| 17 | // |
|---|
| 18 | // The following global parameters must be defined in the giet_config.h file: |
|---|
| 19 | // - CLUSTER_SIZE |
|---|
| 20 | // - NB_CLUSTERS |
|---|
| 21 | // - NB_PROCS_MAX |
|---|
| 22 | // - NB_TIMERS_MAX |
|---|
| 23 | // - NB_DMAS_MAX |
|---|
| 24 | // - NB_TTYS |
|---|
| 25 | // |
|---|
| 26 | // The following virtual base addresses must be defined in the giet.ld file: |
|---|
| 27 | // - seg_icu_base |
|---|
| 28 | // - seg_tim_base |
|---|
| 29 | // - seg_tty_base |
|---|
| 30 | // - seg_gcd_base |
|---|
| 31 | // - seg_dma_base |
|---|
| 32 | // - seg_fbf_base |
|---|
| 33 | // - seg_ioc_base |
|---|
| 34 | // As some peripherals can be replicated in the clusters (ICU, TIMER, DMA) |
|---|
| 35 | // These addresses must be completed by an offset depending on the cluster index |
|---|
| 36 | // full_base_address = seg_***_base + cluster_id * CLUSTER_SIZE |
|---|
| 37 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 38 | |
|---|
| 39 | #include <vm_handler.h> |
|---|
| 40 | #include <sys_handler.h> |
|---|
| 41 | #include <giet_config.h> |
|---|
| 42 | #include <drivers.h> |
|---|
| 43 | #include <common.h> |
|---|
| 44 | #include <hwr_mapping.h> |
|---|
| 45 | #include <mips32_registers.h> |
|---|
| 46 | #include <ctx_handler.h> |
|---|
| 47 | |
|---|
| 48 | #if !defined(NB_CLUSTERS) |
|---|
| 49 | # error: You must define NB_CLUSTERS in the configs file |
|---|
| 50 | #endif |
|---|
| 51 | |
|---|
| 52 | #if !defined(NB_PROCS_MAX) |
|---|
| 53 | # error: You must define NB_PROCS_MAX in the configs file |
|---|
| 54 | #endif |
|---|
| 55 | |
|---|
| 56 | #if (NB_PROCS_MAX > 8) |
|---|
| 57 | # error: NB_PROCS_MAX cannot be larger than 8! |
|---|
| 58 | #endif |
|---|
| 59 | |
|---|
| 60 | #if !defined(CLUSTER_SIZE) |
|---|
| 61 | # error: You must define CLUSTER_SIZE in the configs file |
|---|
| 62 | #endif |
|---|
| 63 | |
|---|
| 64 | #if !defined(NB_TTYS) |
|---|
| 65 | # error: You must define NB_TTYS in the configs file |
|---|
| 66 | #endif |
|---|
| 67 | |
|---|
| 68 | #if (NB_TTYS < 1) |
|---|
| 69 | # error: NB_TTYS cannot be smaller than 1! |
|---|
| 70 | #endif |
|---|
| 71 | |
|---|
| 72 | #if !defined(NB_DMAS_MAX) |
|---|
| 73 | #define NB_DMAS_MAX 0 |
|---|
| 74 | #endif |
|---|
| 75 | |
|---|
| 76 | #if !defined(NB_TIMERS_MAX) |
|---|
| 77 | #define NB_TIMERS_MAX 0 |
|---|
| 78 | #endif |
|---|
| 79 | |
|---|
| 80 | #if ( (NB_TIMERS_MAX + NB_PROCS_MAX) > 32 ) |
|---|
| 81 | # error: NB_TIMERS_MAX + NB_PROCS_MAX cannot be larger than 32 |
|---|
| 82 | #endif |
|---|
| 83 | |
|---|
| 84 | #if !defined(NB_IOCS) |
|---|
| 85 | # error: You must define NB_IOCS in the configs file |
|---|
| 86 | #endif |
|---|
| 87 | |
|---|
| 88 | #if ( NB_IOCS > 1 ) |
|---|
| 89 | # error: NB_IOCS cannot be larger than 1 |
|---|
| 90 | #endif |
|---|
| 91 | |
|---|
| 92 | #if !defined( USE_XICU ) |
|---|
| 93 | # error: You must define USE_XICU in the configs file |
|---|
| 94 | #endif |
|---|
| 95 | |
|---|
| 96 | #if !defined( IOMMU_ACTIVE ) |
|---|
| 97 | # error: You must define IOMMU_ACTIVE in the configs file |
|---|
| 98 | #endif |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | #define in_unckdata __attribute__((section (".unckdata"))) |
|---|
| 102 | |
|---|
| 103 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 104 | // Timers driver |
|---|
| 105 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 106 | // The timers can be implemented in a vci_timer component or in a vci_xicu |
|---|
| 107 | // component (depending on the USE_XICU parameter). |
|---|
| 108 | // There is one timer (or xicu) component per cluster. |
|---|
| 109 | // There is two types of timers: |
|---|
| 110 | // - "system" timers : one per processor, used for context switch. |
|---|
| 111 | // local_id in [0, NB_PROCS_MAX-1], |
|---|
| 112 | // - "user" timers : requested by the task in the mapping_info data structure. |
|---|
| 113 | // local_id in [NB_PROC_MAX, NB_PROCS_MAX + NB_TIMERS_MAX - 1] |
|---|
| 114 | // For each user timer, the timer_id is stored in the context of the task. |
|---|
| 115 | // The global index is cluster_id * (NB_PROCS_MAX+NB_TIMERS_MAX) + local_id |
|---|
| 116 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 117 | |
|---|
| 118 | // User Timer signaling variables |
|---|
| 119 | |
|---|
| 120 | #if (NB_TIMERS_MAX > 0) |
|---|
| 121 | in_unckdata volatile unsigned char _user_timer_event[NB_CLUSTERS*NB_TIMERS_MAX] |
|---|
| 122 | = { [0 ... ((NB_CLUSTERS*NB_TIMERS_MAX)-1)] = 0 }; |
|---|
| 123 | #endif |
|---|
| 124 | |
|---|
| 125 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 126 | // _timer_start() |
|---|
| 127 | // This function activates a timer in the vci_timer (or vci_xicu) component |
|---|
| 128 | // by writing in the proper register the period value. |
|---|
| 129 | // It can be used by both the kernel to initialise a "system" timer, |
|---|
| 130 | // or by a task (through a system call) to configure an "user" timer. |
|---|
| 131 | // Returns 0 if success, > 0 if error. |
|---|
| 132 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 133 | unsigned int _timer_start( unsigned int cluster_id, |
|---|
| 134 | unsigned int local_id, |
|---|
| 135 | unsigned int period ) |
|---|
| 136 | { |
|---|
| 137 | // parameters checking |
|---|
| 138 | if ( cluster_id >= NB_CLUSTERS) return 1; |
|---|
| 139 | if ( local_id >= NB_TIMERS_MAX + NB_PROCS_MAX ) return 1; |
|---|
| 140 | |
|---|
| 141 | #if USE_XICU |
|---|
| 142 | unsigned int* timer_address = (unsigned int*)((char*)&seg_icu_base + |
|---|
| 143 | (cluster_id * CLUSTER_SIZE) ); |
|---|
| 144 | |
|---|
| 145 | timer_address[XICU_REG(XICU_PTI_PER, local_id)] = period; |
|---|
| 146 | #else |
|---|
| 147 | unsigned int* timer_address = (unsigned int*)((char*)&seg_tim_base + |
|---|
| 148 | (cluster_id * CLUSTER_SIZE) ); |
|---|
| 149 | |
|---|
| 150 | timer_address[local_id * TIMER_SPAN + TIMER_PERIOD] = period; |
|---|
| 151 | timer_address[local_id * TIMER_SPAN + TIMER_MODE] = 0x3; |
|---|
| 152 | #endif |
|---|
| 153 | |
|---|
| 154 | return 0; |
|---|
| 155 | } |
|---|
| 156 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 157 | // _timer_stop() |
|---|
| 158 | // This function desactivates a timer in the vci_timer (or vci_xicu) component |
|---|
| 159 | // by writing in the proper register. |
|---|
| 160 | // Returns 0 if success, > 0 if error. |
|---|
| 161 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 162 | unsigned int _timer_stop( unsigned int cluster_id, |
|---|
| 163 | unsigned int local_id ) |
|---|
| 164 | { |
|---|
| 165 | // parameters checking |
|---|
| 166 | if ( cluster_id >= NB_CLUSTERS) return 1; |
|---|
| 167 | if ( local_id >= NB_TIMERS_MAX + NB_PROCS_MAX ) return 1; |
|---|
| 168 | |
|---|
| 169 | #if USE_XICU |
|---|
| 170 | unsigned int* timer_address = (unsigned int*)((char*)&seg_icu_base + |
|---|
| 171 | (cluster_id * CLUSTER_SIZE) ); |
|---|
| 172 | |
|---|
| 173 | timer_address[XICU_REG(XICU_PTI_PER, local_id)] = 0; |
|---|
| 174 | #else |
|---|
| 175 | unsigned int* timer_address = (unsigned int*)((char*)&seg_tim_base + |
|---|
| 176 | (cluster_id * CLUSTER_SIZE) ); |
|---|
| 177 | |
|---|
| 178 | timer_address[local_id * TIMER_SPAN + TIMER_MODE] = 0; |
|---|
| 179 | #endif |
|---|
| 180 | |
|---|
| 181 | return 0; |
|---|
| 182 | } |
|---|
| 183 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 184 | // _timer_reset_irq() |
|---|
| 185 | // This function acknowlegge a timer interrupt in the vci_timer (or vci_xicu) |
|---|
| 186 | // component by reading/writing in the proper register. |
|---|
| 187 | // It can be used by both the isr_switch() for a "system" timer, |
|---|
| 188 | // or by the _isr_timer() for an "user" timer. |
|---|
| 189 | // Returns 0 if success, > 0 if error. |
|---|
| 190 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 191 | unsigned int _timer_reset_irq( unsigned int cluster_id, |
|---|
| 192 | unsigned int local_id ) |
|---|
| 193 | { |
|---|
| 194 | // parameters checking |
|---|
| 195 | if ( cluster_id >= NB_CLUSTERS) return 1; |
|---|
| 196 | if ( local_id >= NB_TIMERS_MAX + NB_PROCS_MAX ) return 1; |
|---|
| 197 | |
|---|
| 198 | #if USE_XICU |
|---|
| 199 | unsigned int* timer_address = (unsigned int*)((char*)&seg_icu_base + |
|---|
| 200 | (cluster_id * (unsigned)CLUSTER_SIZE) ); |
|---|
| 201 | |
|---|
| 202 | unsigned int bloup = timer_address[XICU_REG(XICU_PTI_ACK, local_id)]; |
|---|
| 203 | bloup++; // to avoid a warning |
|---|
| 204 | #else |
|---|
| 205 | unsigned int* timer_address = (unsigned int*)((char*)&seg_tim_base + |
|---|
| 206 | (cluster_id * CLUSTER_SIZE) ); |
|---|
| 207 | |
|---|
| 208 | timer_address[local_id * TIMER_SPAN + TIMER_RESETIRQ] = 0; |
|---|
| 209 | #endif |
|---|
| 210 | |
|---|
| 211 | return 0; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | ///////////////////////////////////////////////////////////////////////////////// |
|---|
| 215 | // VciMultiTty driver |
|---|
| 216 | ///////////////////////////////////////////////////////////////////////////////// |
|---|
| 217 | // There is only one multi_tty controler in the architecture. |
|---|
| 218 | // The total number of TTYs is defined by the configuration parameter NB_TTYS. |
|---|
| 219 | // The "system" terminal is TTY[0]. |
|---|
| 220 | // The "user" TTYs are allocated to applications by the GIET in the boot phase, |
|---|
| 221 | // as defined in the mapping_info data structure. The corresponding tty_id must |
|---|
| 222 | // be stored in the context of the task by the boot code. |
|---|
| 223 | // The TTY address is : seg_tty_base + tty_id*TTY_SPAN |
|---|
| 224 | ///////////////////////////////////////////////////////////////////////////////// |
|---|
| 225 | |
|---|
| 226 | // TTY variables |
|---|
| 227 | in_unckdata volatile unsigned char _tty_get_buf[NB_TTYS]; |
|---|
| 228 | in_unckdata volatile unsigned char _tty_get_full[NB_TTYS] = { [0 ... NB_TTYS-1] = 0 }; |
|---|
| 229 | in_unckdata unsigned int _tty_put_lock = 0; // protect kernel TTY[0] |
|---|
| 230 | |
|---|
| 231 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 232 | // _tty_error() |
|---|
| 233 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 234 | void _tty_error( unsigned int task_id ) |
|---|
| 235 | { |
|---|
| 236 | unsigned int proc_id = _procid(); |
|---|
| 237 | |
|---|
| 238 | _get_lock(&_tty_put_lock); |
|---|
| 239 | _puts("\n[GIET ERROR] TTY index too large for task "); |
|---|
| 240 | _putd( task_id ); |
|---|
| 241 | _puts(" on processor "); |
|---|
| 242 | _putd( proc_id ); |
|---|
| 243 | _puts("\n"); |
|---|
| 244 | _release_lock(&_tty_put_lock); |
|---|
| 245 | } |
|---|
| 246 | ///////////////////////////////////////////////////////////////////////////////// |
|---|
| 247 | // _tty_write() |
|---|
| 248 | // Write one or several characters directly from a fixed-length user buffer to |
|---|
| 249 | // the TTY_WRITE register of the TTY controler. |
|---|
| 250 | // It doesn't use the TTY_PUT_IRQ interrupt and the associated kernel buffer. |
|---|
| 251 | // This is a non blocking call: it tests the TTY_STATUS register, and stops |
|---|
| 252 | // the transfer as soon as the TTY_STATUS[WRITE] bit is set. |
|---|
| 253 | // The function returns the number of characters that have been written. |
|---|
| 254 | ///////////////////////////////////////////////////////////////////////////////// |
|---|
| 255 | unsigned int _tty_write( const char *buffer, |
|---|
| 256 | unsigned int length) |
|---|
| 257 | { |
|---|
| 258 | unsigned int nwritten; |
|---|
| 259 | |
|---|
| 260 | unsigned int task_id = _get_current_task_id(); |
|---|
| 261 | unsigned int tty_id = _get_context_slot(task_id, CTX_TTY_ID); |
|---|
| 262 | |
|---|
| 263 | if ( tty_id >= NB_TTYS ) |
|---|
| 264 | { |
|---|
| 265 | _tty_error( task_id ); |
|---|
| 266 | return 0; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | unsigned int* tty_address = (unsigned int*) &seg_tty_base; |
|---|
| 270 | |
|---|
| 271 | for (nwritten = 0; nwritten < length; nwritten++) |
|---|
| 272 | { |
|---|
| 273 | // check tty's status |
|---|
| 274 | if ((tty_address[tty_id*TTY_SPAN + TTY_STATUS] & 0x2) == 0x2) |
|---|
| 275 | break; |
|---|
| 276 | else |
|---|
| 277 | // write character |
|---|
| 278 | tty_address[tty_id*TTY_SPAN + TTY_WRITE] = (unsigned int)buffer[nwritten]; |
|---|
| 279 | } |
|---|
| 280 | return nwritten; |
|---|
| 281 | } |
|---|
| 282 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 283 | // _tty_read() |
|---|
| 284 | // This non-blocking function uses the TTY_GET_IRQ[tty_id] interrupt and |
|---|
| 285 | // the associated kernel buffer, that has been written by the ISR. |
|---|
| 286 | // It get the TTY terminal index from the context of the current task. |
|---|
| 287 | // It fetches one single character from the _tty_get_buf[tty_id] kernel |
|---|
| 288 | // buffer, writes this character to the user buffer, and resets the |
|---|
| 289 | // _tty_get_full[tty_id] buffer. |
|---|
| 290 | // The length argument is not used. |
|---|
| 291 | // Returns 0 if the kernel buffer is empty, 1 if the buffer is full. |
|---|
| 292 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 293 | unsigned int _tty_read( char *buffer, |
|---|
| 294 | unsigned int length) |
|---|
| 295 | { |
|---|
| 296 | unsigned int task_id = _get_current_task_id(); |
|---|
| 297 | unsigned int tty_id = _get_context_slot(task_id, CTX_TTY_ID); |
|---|
| 298 | |
|---|
| 299 | if ( tty_id >= NB_TTYS ) |
|---|
| 300 | { |
|---|
| 301 | _tty_error( task_id ); |
|---|
| 302 | return 0; |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | if (_tty_get_full[tty_id] == 0) |
|---|
| 306 | { |
|---|
| 307 | return 0; |
|---|
| 308 | } |
|---|
| 309 | else |
|---|
| 310 | { |
|---|
| 311 | *buffer = _tty_get_buf[tty_id]; |
|---|
| 312 | _tty_get_full[tty_id] = 0; |
|---|
| 313 | return 1; |
|---|
| 314 | } |
|---|
| 315 | } |
|---|
| 316 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 317 | // _tty_get_char() |
|---|
| 318 | // This function is used by the _isr_tty to read a character in the TTY |
|---|
| 319 | // terminal defined by the tty_id argument. The character is stored |
|---|
| 320 | // in requested buffer, and the IRQ is acknowledged. |
|---|
| 321 | // Returns 0 if success, 1 if tty_id too large. |
|---|
| 322 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 323 | unsigned int _tty_get_char( unsigned int tty_id, |
|---|
| 324 | unsigned char* buffer ) |
|---|
| 325 | { |
|---|
| 326 | // checking argument |
|---|
| 327 | if ( tty_id >= NB_TTYS ) return 1; |
|---|
| 328 | |
|---|
| 329 | // compute terminal base address |
|---|
| 330 | unsigned int *tty_address = (unsigned int*) &seg_tty_base; |
|---|
| 331 | |
|---|
| 332 | *buffer = (unsigned char)tty_address[tty_id*TTY_SPAN + TTY_READ]; |
|---|
| 333 | return 0; |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 337 | // VciMultiIcu and VciXicu drivers |
|---|
| 338 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 339 | // There is one vci_multi_icu (or vci_xicu) component per cluster, |
|---|
| 340 | // and the number of independant ICUs is equal to NB_PROCS_MAX, |
|---|
| 341 | // because there is one private interrupr controler per processor. |
|---|
| 342 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 343 | |
|---|
| 344 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 345 | // _icu_set_mask() |
|---|
| 346 | // This function can be used with both the vci_xicu & vci_multi_icu components. |
|---|
| 347 | // It set the mask register for the ICU channel identified by the cluster index |
|---|
| 348 | // and the processor index: all '1' bits are set / all '0' bits are not modified. |
|---|
| 349 | // Returns 0 if success, > 0 if error. |
|---|
| 350 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 351 | unsigned int _icu_set_mask( unsigned int cluster_id, |
|---|
| 352 | unsigned int proc_id, |
|---|
| 353 | unsigned int value, |
|---|
| 354 | unsigned int is_timer ) |
|---|
| 355 | { |
|---|
| 356 | // parameters checking |
|---|
| 357 | if ( cluster_id >= NB_CLUSTERS) return 1; |
|---|
| 358 | if ( proc_id >= NB_PROCS_MAX ) return 1; |
|---|
| 359 | |
|---|
| 360 | unsigned int* icu_address = (unsigned int*)( (char*)&seg_icu_base + |
|---|
| 361 | (cluster_id * (unsigned)CLUSTER_SIZE) ); |
|---|
| 362 | #if USE_XICU |
|---|
| 363 | if ( is_timer ) icu_address[XICU_REG(XICU_MSK_PTI_ENABLE, proc_id)] = value; |
|---|
| 364 | else icu_address[XICU_REG(XICU_MSK_HWI_ENABLE, proc_id)] = value; |
|---|
| 365 | #else |
|---|
| 366 | icu_address[proc_id * ICU_SPAN + ICU_MASK_SET] = value; |
|---|
| 367 | #endif |
|---|
| 368 | |
|---|
| 369 | return 0; |
|---|
| 370 | } |
|---|
| 371 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 372 | // _icu_get_index() |
|---|
| 373 | // This function can be used with both the vci_xicu & vci_multi_icu components. |
|---|
| 374 | // It returns the index of the highest priority (smaller index) active HWI. |
|---|
| 375 | // The ICU channel is identified by the cluster index and the processor index. |
|---|
| 376 | // Returns 0 if success, > 0 if error. |
|---|
| 377 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 378 | unsigned int _icu_get_index( unsigned int cluster_id, |
|---|
| 379 | unsigned int proc_id, |
|---|
| 380 | unsigned int* buffer ) |
|---|
| 381 | { |
|---|
| 382 | // parameters checking |
|---|
| 383 | if ( cluster_id >= NB_CLUSTERS) return 1; |
|---|
| 384 | if ( proc_id >= NB_PROCS_MAX ) return 1; |
|---|
| 385 | |
|---|
| 386 | unsigned int* icu_address = (unsigned int*)( (char*)&seg_icu_base + |
|---|
| 387 | (cluster_id * (unsigned)CLUSTER_SIZE) ); |
|---|
| 388 | #if USE_XICU |
|---|
| 389 | unsigned int prio = icu_address[XICU_REG(XICU_PRIO, proc_id)]; |
|---|
| 390 | unsigned int pti_ok = (prio & 0x00000001); |
|---|
| 391 | unsigned int hwi_ok = (prio & 0x00000002); |
|---|
| 392 | unsigned int swi_ok = (prio & 0x00000004); |
|---|
| 393 | unsigned int pti_id = (prio & 0x00001F00) >> 8; |
|---|
| 394 | unsigned int hwi_id = (prio & 0x001F0000) >> 16; |
|---|
| 395 | unsigned int swi_id = (prio & 0x1F000000) >> 24; |
|---|
| 396 | if (pti_ok) *buffer = pti_id; |
|---|
| 397 | else if (hwi_ok) *buffer = hwi_id; |
|---|
| 398 | else if (swi_ok) *buffer = swi_id; |
|---|
| 399 | else *buffer = 32; |
|---|
| 400 | #else |
|---|
| 401 | *buffer = icu_address[proc_id * ICU_SPAN + ICU_IT_VECTOR]; |
|---|
| 402 | #endif |
|---|
| 403 | |
|---|
| 404 | return 0; |
|---|
| 405 | } |
|---|
| 406 | |
|---|
| 407 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 408 | // VciGcd driver |
|---|
| 409 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 410 | // The Greater Dommon Divider is a -very- simple hardware coprocessor |
|---|
| 411 | // performing the computation of the GCD of two 32 bits integers. |
|---|
| 412 | // It has no DMA capability. |
|---|
| 413 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 414 | |
|---|
| 415 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 416 | // _gcd_write() |
|---|
| 417 | // Write a 32-bit word in a memory mapped register of the GCD coprocessor. |
|---|
| 418 | // Returns 0 if success, > 0 if error. |
|---|
| 419 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 420 | unsigned int _gcd_write( unsigned int register_index, |
|---|
| 421 | unsigned int value) |
|---|
| 422 | { |
|---|
| 423 | // parameters checking |
|---|
| 424 | if (register_index >= GCD_END) |
|---|
| 425 | return 1; |
|---|
| 426 | |
|---|
| 427 | unsigned int* gcd_address = (unsigned int*) &seg_gcd_base; |
|---|
| 428 | |
|---|
| 429 | gcd_address[register_index] = value; // write word |
|---|
| 430 | return 0; |
|---|
| 431 | } |
|---|
| 432 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 433 | // _gcd_read() |
|---|
| 434 | // Read a 32-bit word in a memory mapped register of the GCD coprocessor. |
|---|
| 435 | // Returns 0 if success, > 0 if error. |
|---|
| 436 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 437 | unsigned int _gcd_read( unsigned int register_index, |
|---|
| 438 | unsigned int *buffer) |
|---|
| 439 | { |
|---|
| 440 | // parameters checking |
|---|
| 441 | if (register_index >= GCD_END) |
|---|
| 442 | return 1; |
|---|
| 443 | |
|---|
| 444 | unsigned int* gcd_address = (unsigned int*) &seg_gcd_base; |
|---|
| 445 | |
|---|
| 446 | *buffer = gcd_address[register_index]; // read word |
|---|
| 447 | return 0; |
|---|
| 448 | } |
|---|
| 449 | |
|---|
| 450 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 451 | // VciBlockDevice driver |
|---|
| 452 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 453 | // The VciBlockDevice is a single channel external storage contrÃŽler. |
|---|
| 454 | // |
|---|
| 455 | // The IOMMU can be activated or not: |
|---|
| 456 | // |
|---|
| 457 | // 1) When the IOMMU is used, a fixed size 2Mbytes vseg is allocated to |
|---|
| 458 | // the IOC peripheral, in the I/O virtual space, and the user buffer is |
|---|
| 459 | // dynamically remapped in the IOMMU page table. The corresponding entry |
|---|
| 460 | // in the IOMMU PT1 is defined by the kernel _ioc_iommu_ix1 variable. |
|---|
| 461 | // The number of pages to be unmapped is stored in the _ioc_npages variable. |
|---|
| 462 | // The number of PT2 entries is dynamically computed and stored in the |
|---|
| 463 | // kernel _ioc_iommu_npages variable. It cannot be larger than 512. |
|---|
| 464 | // The user buffer is unmapped by the _ioc_completed() function when |
|---|
| 465 | // the transfer is completed. |
|---|
| 466 | // |
|---|
| 467 | // 2/ If the IOMMU is not used, we check that the user buffer is mapped to a |
|---|
| 468 | // contiguous physical buffer (this is generally true because the user space |
|---|
| 469 | // page tables are statically constructed to use contiguous physical memory). |
|---|
| 470 | // |
|---|
| 471 | // Finally, the memory buffer must fulfill the following conditions: |
|---|
| 472 | // - The user buffer must be word aligned, |
|---|
| 473 | // - The user buffer must be mapped in user address space, |
|---|
| 474 | // - The user buffer must be writable in case of (to_mem) access, |
|---|
| 475 | // - The total number of physical pages occupied by the user buffer cannot |
|---|
| 476 | // be larger than 512 pages if the IOMMU is activated, |
|---|
| 477 | // - All physical pages occupied by the user buffer must be contiguous |
|---|
| 478 | // if the IOMMU is not activated. |
|---|
| 479 | // An error code is returned if these conditions are not verified. |
|---|
| 480 | // |
|---|
| 481 | // As the IOC component can be used by several programs running in parallel, |
|---|
| 482 | // the _ioc_lock variable guaranties exclusive access to the device. The |
|---|
| 483 | // _ioc_read() and _ioc_write() functions use atomic LL/SC to get the lock. |
|---|
| 484 | // and set _ioc_lock to a non zero value. The _ioc_write() and _ioc_read() |
|---|
| 485 | // functions are blocking, polling the _ioc_lock variable until the device is |
|---|
| 486 | // available. |
|---|
| 487 | // When the tranfer is completed, the ISR routine activated by the IOC IRQ |
|---|
| 488 | // set the _ioc_done variable to a non-zero value. Possible address errors |
|---|
| 489 | // detected by the IOC peripheral are reported by the ISR in the _ioc_status |
|---|
| 490 | // variable. |
|---|
| 491 | // The _ioc_completed() function is polling the _ioc_done variable, waiting for |
|---|
| 492 | // transfer completion. When the completion is signaled, the _ioc_completed() |
|---|
| 493 | // function reset the _ioc_done variable to zero, and releases the _ioc_lock |
|---|
| 494 | // variable. |
|---|
| 495 | // |
|---|
| 496 | // In a multi-processing environment, this polling policy should be replaced by |
|---|
| 497 | // a descheduling policy for the requesting process. |
|---|
| 498 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 499 | |
|---|
| 500 | // IOC global variables |
|---|
| 501 | in_unckdata volatile unsigned int _ioc_status = 0; |
|---|
| 502 | in_unckdata volatile unsigned int _ioc_done = 0; |
|---|
| 503 | in_unckdata unsigned int _ioc_lock = 0; |
|---|
| 504 | in_unckdata unsigned int _ioc_iommu_ix1 = 0; |
|---|
| 505 | in_unckdata unsigned int _ioc_iommu_npages; |
|---|
| 506 | |
|---|
| 507 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 508 | // _ioc_access() |
|---|
| 509 | // This function transfer data between a memory buffer and the block device. |
|---|
| 510 | // The buffer lentgth is (count*block_size) bytes. |
|---|
| 511 | // Arguments are: |
|---|
| 512 | // - to_mem : from external storage to memory when non 0 |
|---|
| 513 | // - lba : first block index on the external storage. |
|---|
| 514 | // - user_vaddr : virtual base address of the memory buffer. |
|---|
| 515 | // - count : number of blocks to be transfered. |
|---|
| 516 | // Returns 0 if success, > 0 if error. |
|---|
| 517 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 518 | unsigned int _ioc_access( unsigned int to_mem, |
|---|
| 519 | unsigned int lba, |
|---|
| 520 | unsigned int user_vaddr, |
|---|
| 521 | unsigned int count ) |
|---|
| 522 | { |
|---|
| 523 | unsigned int user_vpn_min; // first virtuel page index in user space |
|---|
| 524 | unsigned int user_vpn_max; // last virtual page index in user space |
|---|
| 525 | unsigned int vpn; // current virtual page index in user space |
|---|
| 526 | unsigned int ppn; // physical page number |
|---|
| 527 | unsigned int flags; // page protection flags |
|---|
| 528 | unsigned int ix2; // page index in IOMMU PT1 page table |
|---|
| 529 | unsigned int addr; // buffer address for IOC peripheral |
|---|
| 530 | unsigned int ppn_first; // first physical page number for user buffer |
|---|
| 531 | |
|---|
| 532 | // check buffer alignment |
|---|
| 533 | if ( (unsigned int)user_vaddr & 0x3 ) return 1; |
|---|
| 534 | |
|---|
| 535 | unsigned int* ioc_address = (unsigned int*) &seg_ioc_base ; |
|---|
| 536 | |
|---|
| 537 | unsigned int block_size = ioc_address[BLOCK_DEVICE_BLOCK_SIZE]; |
|---|
| 538 | unsigned int length = count*block_size; |
|---|
| 539 | |
|---|
| 540 | // get user space page table virtual address |
|---|
| 541 | unsigned int task_id = _get_current_task_id(); |
|---|
| 542 | unsigned int user_pt_vbase = _get_context_slot( task_id, CTX_PTAB_ID ); |
|---|
| 543 | |
|---|
| 544 | user_vpn_min = user_vaddr >> 12; |
|---|
| 545 | user_vpn_max = (user_vaddr + length - 1) >> 12; |
|---|
| 546 | ix2 = 0; |
|---|
| 547 | |
|---|
| 548 | // loop on all virtual pages covering the user buffer |
|---|
| 549 | for ( vpn = user_vpn_min ; vpn <= user_vpn_max ; vpn++ ) |
|---|
| 550 | { |
|---|
| 551 | // get ppn and flags for each vpn |
|---|
| 552 | unsigned int ko = _v2p_translate( (page_table_t*)user_pt_vbase, |
|---|
| 553 | vpn, |
|---|
| 554 | &ppn, |
|---|
| 555 | &flags ); |
|---|
| 556 | |
|---|
| 557 | // check access rights |
|---|
| 558 | if ( ko ) return 2; // unmapped |
|---|
| 559 | if ( (flags & PTE_U) == 0 ) return 3; // not in user space |
|---|
| 560 | if ( ( (flags & PTE_W) == 0 ) && to_mem ) return 4; // not writable |
|---|
| 561 | |
|---|
| 562 | // save first ppn value |
|---|
| 563 | if ( ix2 == 0 ) ppn_first = ppn; |
|---|
| 564 | |
|---|
| 565 | if ( IOMMU_ACTIVE ) // the user buffer must be remapped in the I/0 space |
|---|
| 566 | { |
|---|
| 567 | // check buffer length < 2 Mbytes |
|---|
| 568 | if ( ix2 > 511 ) return 2; |
|---|
| 569 | |
|---|
| 570 | // map the physical page in IOMMU page table |
|---|
| 571 | _iommu_add_pte2( _ioc_iommu_ix1, // PT1 index |
|---|
| 572 | ix2, // PT2 index |
|---|
| 573 | ppn, // Physical page number |
|---|
| 574 | flags ); // Protection flags |
|---|
| 575 | } |
|---|
| 576 | else // no IOMMU : check that physical pages are contiguous |
|---|
| 577 | { |
|---|
| 578 | if ( (ppn - ppn_first) != ix2 ) return 5; // split physical buffer |
|---|
| 579 | } |
|---|
| 580 | |
|---|
| 581 | // increment page index |
|---|
| 582 | ix2++; |
|---|
| 583 | } // end for vpn |
|---|
| 584 | |
|---|
| 585 | // register the number of pages to be unmapped |
|---|
| 586 | _ioc_iommu_npages = (user_vpn_max - user_vpn_min) + 1; |
|---|
| 587 | |
|---|
| 588 | // invalidate data cache in case of memory write |
|---|
| 589 | if ( to_mem ) _dcache_buf_invalidate( (void*)user_vaddr, length ); |
|---|
| 590 | |
|---|
| 591 | // compute buffer base address for IOC depending on IOMMU activation |
|---|
| 592 | if ( IOMMU_ACTIVE ) addr = (_ioc_iommu_ix1) << 21 | (user_vaddr & 0xFFF); |
|---|
| 593 | else addr = (ppn_first << 12) | (user_vaddr & 0xFFF); |
|---|
| 594 | |
|---|
| 595 | // get the lock on ioc device |
|---|
| 596 | _get_lock( &_ioc_lock ); |
|---|
| 597 | |
|---|
| 598 | // peripheral configuration |
|---|
| 599 | ioc_address[BLOCK_DEVICE_BUFFER] = addr; |
|---|
| 600 | ioc_address[BLOCK_DEVICE_COUNT] = count; |
|---|
| 601 | ioc_address[BLOCK_DEVICE_LBA] = lba; |
|---|
| 602 | if ( to_mem == 0 ) ioc_address[BLOCK_DEVICE_OP] = BLOCK_DEVICE_WRITE; |
|---|
| 603 | else ioc_address[BLOCK_DEVICE_OP] = BLOCK_DEVICE_READ; |
|---|
| 604 | |
|---|
| 605 | return 0; |
|---|
| 606 | } |
|---|
| 607 | ///////////////////////////////////////////////////////////////////////////////// |
|---|
| 608 | // _ioc_completed() |
|---|
| 609 | // |
|---|
| 610 | // This function checks completion of an I/O transfer and reports errors. |
|---|
| 611 | // As it is a blocking call, the processor is stalled. |
|---|
| 612 | // If the virtual memory is activated, the pages mapped in the I/O virtual |
|---|
| 613 | // space are unmapped, and the IOB TLB is cleared. |
|---|
| 614 | // Returns 0 if success, > 0 if error. |
|---|
| 615 | ///////////////////////////////////////////////////////////////////////////////// |
|---|
| 616 | unsigned int _ioc_completed() |
|---|
| 617 | { |
|---|
| 618 | unsigned int ret; |
|---|
| 619 | unsigned int ix2; |
|---|
| 620 | |
|---|
| 621 | // busy waiting |
|---|
| 622 | while (_ioc_done == 0) |
|---|
| 623 | asm volatile("nop"); |
|---|
| 624 | |
|---|
| 625 | // unmap the buffer from IOMMU page table if IOMMU is activated |
|---|
| 626 | if ( IOMMU_ACTIVE ) |
|---|
| 627 | { |
|---|
| 628 | unsigned int* iob_address = (unsigned int*) &seg_iob_base; |
|---|
| 629 | |
|---|
| 630 | for ( ix2 = 0 ; ix2 < _ioc_iommu_npages ; ix2++ ) |
|---|
| 631 | { |
|---|
| 632 | // unmap the page in IOMMU page table |
|---|
| 633 | _iommu_inval_pte2( _ioc_iommu_ix1, // PT1 index |
|---|
| 634 | ix2 ); // PT2 index |
|---|
| 635 | |
|---|
| 636 | // clear IOMMU TLB |
|---|
| 637 | iob_address[IOB_INVAL_PTE] = (_ioc_iommu_ix1 << 21) | (ix2 << 12); |
|---|
| 638 | } |
|---|
| 639 | } |
|---|
| 640 | |
|---|
| 641 | // test IOC status |
|---|
| 642 | if ((_ioc_status != BLOCK_DEVICE_READ_SUCCESS) |
|---|
| 643 | && (_ioc_status != BLOCK_DEVICE_WRITE_SUCCESS)) ret = 1; // error |
|---|
| 644 | else ret = 0; // success |
|---|
| 645 | |
|---|
| 646 | // reset synchronization variables |
|---|
| 647 | _ioc_lock =0; |
|---|
| 648 | _ioc_done =0; |
|---|
| 649 | |
|---|
| 650 | return ret; |
|---|
| 651 | } |
|---|
| 652 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 653 | // _ioc_read() |
|---|
| 654 | // Transfer data from the block device to a memory buffer in user space. |
|---|
| 655 | // - lba : first block index on the block device |
|---|
| 656 | // - buffer : base address of the memory buffer (must be word aligned) |
|---|
| 657 | // - count : number of blocks to be transfered. |
|---|
| 658 | // Returns 0 if success, > 0 if error. |
|---|
| 659 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 660 | unsigned int _ioc_read( unsigned int lba, |
|---|
| 661 | void* buffer, |
|---|
| 662 | unsigned int count ) |
|---|
| 663 | { |
|---|
| 664 | return _ioc_access( 1, // read access |
|---|
| 665 | lba, |
|---|
| 666 | (unsigned int)buffer, |
|---|
| 667 | count ); |
|---|
| 668 | } |
|---|
| 669 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 670 | // _ioc_write() |
|---|
| 671 | // Transfer data from a memory buffer in user space to the block device. |
|---|
| 672 | // - lba : first block index on the block device |
|---|
| 673 | // - buffer : base address of the memory buffer (must be word aligned) |
|---|
| 674 | // - count : number of blocks to be transfered. |
|---|
| 675 | // Returns 0 if success, > 0 if error. |
|---|
| 676 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 677 | unsigned int _ioc_write( unsigned int lba, |
|---|
| 678 | const void* buffer, |
|---|
| 679 | unsigned int count ) |
|---|
| 680 | { |
|---|
| 681 | return _ioc_access( 0, // write access |
|---|
| 682 | lba, |
|---|
| 683 | (unsigned int)buffer, |
|---|
| 684 | count ); |
|---|
| 685 | } |
|---|
| 686 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 687 | // _ioc_get_status() |
|---|
| 688 | // This function returns the transfert status, and acknowledge the IRQ. |
|---|
| 689 | // Returns 0 if success, > 0 if error. |
|---|
| 690 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 691 | unsigned int _ioc_get_status(unsigned int* status) |
|---|
| 692 | { |
|---|
| 693 | // get IOC base address |
|---|
| 694 | unsigned int* ioc_address = (unsigned int*) &seg_ioc_base; |
|---|
| 695 | |
|---|
| 696 | *status = ioc_address[BLOCK_DEVICE_STATUS]; // read status & reset IRQ |
|---|
| 697 | return 0; |
|---|
| 698 | } |
|---|
| 699 | |
|---|
| 700 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 701 | // VciMultiDma driver |
|---|
| 702 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 703 | // The DMA controllers are physically distributed in the clusters. |
|---|
| 704 | // There is (NB_CLUSTERS * NB_DMAS_MAX) channels, indexed by a global index: |
|---|
| 705 | // dma_id = cluster_id * NB_DMA_MAX + loc_id |
|---|
| 706 | // |
|---|
| 707 | // As a DMA channel can be used by several tasks, each DMA channel is protected |
|---|
| 708 | // by a specific lock: _dma_lock[dma_id] |
|---|
| 709 | // The signalisation between the OS and the DMA uses the _dma_done[dma_id] |
|---|
| 710 | // synchronisation variables (set by the ISR, and reset by the OS). |
|---|
| 711 | // The transfer status is copied by the ISR in the _dma_status[dma_id] variables. |
|---|
| 712 | // |
|---|
| 713 | // These DMA channels can be used by the FB driver, or by the NIC driver. |
|---|
| 714 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 715 | |
|---|
| 716 | //+1: for the case where the NB_DMAS_MAX == 0 |
|---|
| 717 | #if NB_DMAS_MAX > 0 |
|---|
| 718 | in_unckdata unsigned int _dma_lock[NB_DMAS_MAX * NB_CLUSTERS] |
|---|
| 719 | = { [0 ... ((NB_DMAS_MAX) * NB_CLUSTERS)-1] = 0 }; |
|---|
| 720 | |
|---|
| 721 | in_unckdata volatile unsigned int _dma_done[NB_DMAS_MAX * NB_CLUSTERS] |
|---|
| 722 | = { [0 ... (NB_DMAS_MAX * NB_CLUSTERS)-1] = 0 }; |
|---|
| 723 | |
|---|
| 724 | in_unckdata volatile unsigned int _dma_status[NB_DMAS_MAX * NB_CLUSTERS]; |
|---|
| 725 | |
|---|
| 726 | in_unckdata unsigned int _dma_iommu_ix1 = 1; |
|---|
| 727 | |
|---|
| 728 | in_unckdata unsigned int _dma_iommu_npages[NB_DMAS_MAX * NB_CLUSTERS]; |
|---|
| 729 | #endif |
|---|
| 730 | |
|---|
| 731 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 732 | // _dma_reset_irq() |
|---|
| 733 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 734 | unsigned int _dma_reset_irq( unsigned int cluster_id, |
|---|
| 735 | unsigned int channel_id ) |
|---|
| 736 | { |
|---|
| 737 | #if NB_DMAS_MAX > 0 |
|---|
| 738 | // parameters checking |
|---|
| 739 | if ( cluster_id >= NB_CLUSTERS ) return 1; |
|---|
| 740 | if ( channel_id >= NB_DMAS_MAX ) return 1; |
|---|
| 741 | |
|---|
| 742 | // compute DMA base address |
|---|
| 743 | unsigned int* dma_address = (unsigned int*)( (char*)&seg_dma_base + |
|---|
| 744 | (cluster_id * (unsigned)CLUSTER_SIZE) ); |
|---|
| 745 | |
|---|
| 746 | dma_address[channel_id*DMA_SPAN + DMA_RESET] = 0; |
|---|
| 747 | return 0; |
|---|
| 748 | #else |
|---|
| 749 | return -1; |
|---|
| 750 | #endif |
|---|
| 751 | } |
|---|
| 752 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 753 | // _dma_get_status() |
|---|
| 754 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 755 | unsigned int _dma_get_status( unsigned int cluster_id, |
|---|
| 756 | unsigned int channel_id, |
|---|
| 757 | unsigned int* status ) |
|---|
| 758 | { |
|---|
| 759 | #if NB_DMAS_MAX > 0 |
|---|
| 760 | // parameters checking |
|---|
| 761 | if ( cluster_id >= NB_CLUSTERS ) return 1; |
|---|
| 762 | if ( channel_id >= NB_DMAS_MAX ) return 1; |
|---|
| 763 | |
|---|
| 764 | // compute DMA base address |
|---|
| 765 | unsigned int* dma_address = (unsigned int*)( (char*)&seg_dma_base + |
|---|
| 766 | (cluster_id * (unsigned)CLUSTER_SIZE) ); |
|---|
| 767 | |
|---|
| 768 | *status = dma_address[channel_id*DMA_SPAN + DMA_LEN]; |
|---|
| 769 | return 0; |
|---|
| 770 | #else |
|---|
| 771 | return -1; |
|---|
| 772 | #endif |
|---|
| 773 | } |
|---|
| 774 | |
|---|
| 775 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 776 | // VciFrameBuffer driver |
|---|
| 777 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 778 | // The vci_frame_buffer device can be accessed directly by software with memcpy(), |
|---|
| 779 | // or it can be accessed through a multi-channels DMA component: |
|---|
| 780 | // |
|---|
| 781 | // The '_fb_sync_write' and '_fb_sync_read' functions use a memcpy strategy to |
|---|
| 782 | // implement the transfer between a data buffer (user space) and the frame |
|---|
| 783 | // buffer (kernel space). They are blocking until completion of the transfer. |
|---|
| 784 | // |
|---|
| 785 | // The '_fb_write()', '_fb_read()' and '_fb_completed()' functions use the DMA |
|---|
| 786 | // controlers (distributed in the clusters) to transfer data |
|---|
| 787 | // between the user buffer and the frame buffer. A DMA channel is |
|---|
| 788 | // allocated to each task requesting it in the mapping_info data structure. |
|---|
| 789 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 790 | |
|---|
| 791 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 792 | // _fb_sync_write() |
|---|
| 793 | // Transfer data from an memory buffer to the frame_buffer device using |
|---|
| 794 | // a memcpy. The source memory buffer must be in user address space. |
|---|
| 795 | // - offset : offset (in bytes) in the frame buffer. |
|---|
| 796 | // - buffer : base address of the memory buffer. |
|---|
| 797 | // - length : number of bytes to be transfered. |
|---|
| 798 | // Returns 0 if success, > 0 if error. |
|---|
| 799 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 800 | unsigned int _fb_sync_write( unsigned int offset, |
|---|
| 801 | const void* buffer, |
|---|
| 802 | unsigned int length ) |
|---|
| 803 | { |
|---|
| 804 | |
|---|
| 805 | // buffer must be mapped in user space |
|---|
| 806 | if ( ((unsigned int)buffer + length ) >= 0x80000000 ) |
|---|
| 807 | { |
|---|
| 808 | return 1; |
|---|
| 809 | } |
|---|
| 810 | else |
|---|
| 811 | { |
|---|
| 812 | unsigned char *fb_address = (unsigned char*)&seg_fbf_base + offset; |
|---|
| 813 | memcpy((void*)fb_address, (void*)buffer, length); |
|---|
| 814 | return 0; |
|---|
| 815 | } |
|---|
| 816 | } |
|---|
| 817 | |
|---|
| 818 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 819 | // _fb_sync_read() |
|---|
| 820 | // Transfer data from the frame_buffer device to a memory buffer using |
|---|
| 821 | // a memcpy. The destination memory buffer must be in user address space. |
|---|
| 822 | // - offset : offset (in bytes) in the frame buffer. |
|---|
| 823 | // - buffer : base address of the memory buffer. |
|---|
| 824 | // - length : number of bytes to be transfered. |
|---|
| 825 | // Returns 0 if success, > 0 if error. |
|---|
| 826 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 827 | unsigned int _fb_sync_read( unsigned int offset, |
|---|
| 828 | const void* buffer, |
|---|
| 829 | unsigned int length ) |
|---|
| 830 | { |
|---|
| 831 | // buffer must be mapped in user space |
|---|
| 832 | if ( ((unsigned int)buffer + length ) >= 0x80000000 ) |
|---|
| 833 | { |
|---|
| 834 | return 1; |
|---|
| 835 | } |
|---|
| 836 | else |
|---|
| 837 | { |
|---|
| 838 | unsigned char *fb_address = (unsigned char*)&seg_fbf_base + offset; |
|---|
| 839 | memcpy((void*)buffer, (void*)fb_address, length); |
|---|
| 840 | return 0; |
|---|
| 841 | } |
|---|
| 842 | } |
|---|
| 843 | |
|---|
| 844 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 845 | // _fb_dma_access() |
|---|
| 846 | // Transfer data between a user buffer and the frame_buffer using DMA. |
|---|
| 847 | // - to_user : from frame buffer to user buffer when true. |
|---|
| 848 | // - offset : offset (in bytes) in the frame buffer. |
|---|
| 849 | // - user_vaddr : virtual base address of the memory buffer. |
|---|
| 850 | // - length : number of bytes to be transfered. |
|---|
| 851 | // The user buffer must be mapped in user address space and word-aligned. |
|---|
| 852 | // The user buffer length must be multiple of 4 bytes. |
|---|
| 853 | // Me must compute the physical base addresses for both the frame buffer |
|---|
| 854 | // and the user buffer before programming the DMA transfer. |
|---|
| 855 | // The GIET being fully static, we don't need to split the transfer in 4 Kbytes |
|---|
| 856 | // pages, because the user buffer is contiguous in physical space. |
|---|
| 857 | // Returns 0 if success, > 0 if error. |
|---|
| 858 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 859 | unsigned int _fb_dma_access( unsigned int to_user, |
|---|
| 860 | unsigned int offset, |
|---|
| 861 | unsigned int user_vaddr, |
|---|
| 862 | unsigned int length ) |
|---|
| 863 | { |
|---|
| 864 | #if NB_DMAS_MAX > 0 |
|---|
| 865 | unsigned int ko; // unsuccessfull V2P translation |
|---|
| 866 | unsigned int flags; // protection flags |
|---|
| 867 | unsigned int ppn; // physical page number |
|---|
| 868 | unsigned int user_pbase; // user buffer pbase address |
|---|
| 869 | unsigned int fb_pbase; // frame buffer pbase address |
|---|
| 870 | |
|---|
| 871 | // get DMA channel and compute DMA vbase address |
|---|
| 872 | unsigned int task_id = _get_current_task_id(); |
|---|
| 873 | unsigned int dma_id = _get_context_slot( task_id, CTX_FBDMA_ID ); |
|---|
| 874 | unsigned int cluster_id = dma_id / NB_DMAS_MAX; |
|---|
| 875 | unsigned int loc_id = dma_id % NB_DMAS_MAX; |
|---|
| 876 | |
|---|
| 877 | unsigned int* dma_base = (unsigned int*)( (char*)&seg_dma_base + |
|---|
| 878 | (cluster_id * (unsigned)CLUSTER_SIZE) ); |
|---|
| 879 | |
|---|
| 880 | // check user buffer address and length alignment |
|---|
| 881 | if ( (user_vaddr & 0x3) || (length & 0x3) ) |
|---|
| 882 | { |
|---|
| 883 | _get_lock(&_tty_put_lock); |
|---|
| 884 | _puts("\n[GIET ERROR] in _fbdma_access() : user buffer not word aligned\n"); |
|---|
| 885 | _release_lock(&_tty_put_lock); |
|---|
| 886 | return 1; |
|---|
| 887 | } |
|---|
| 888 | |
|---|
| 889 | // get user space page table virtual address |
|---|
| 890 | unsigned int user_ptab = _get_context_slot( task_id, CTX_PTAB_ID ); |
|---|
| 891 | |
|---|
| 892 | // compute frame buffer pbase address |
|---|
| 893 | unsigned int fb_vaddr = (unsigned int)&seg_fbf_base + offset; |
|---|
| 894 | |
|---|
| 895 | ko = _v2p_translate( (page_table_t*)user_ptab, |
|---|
| 896 | (fb_vaddr >> 12), |
|---|
| 897 | &ppn, |
|---|
| 898 | &flags ); |
|---|
| 899 | fb_pbase = (ppn << 12) | (fb_vaddr & 0x00000FFF); |
|---|
| 900 | |
|---|
| 901 | if ( ko ) |
|---|
| 902 | { |
|---|
| 903 | _get_lock(&_tty_put_lock); |
|---|
| 904 | _puts("\n[GIET ERROR] in _fbdma_access() : frame buffer unmapped\n"); |
|---|
| 905 | _release_lock(&_tty_put_lock); |
|---|
| 906 | return 2; |
|---|
| 907 | } |
|---|
| 908 | |
|---|
| 909 | // Compute user buffer pbase address |
|---|
| 910 | ko = _v2p_translate( (page_table_t*)user_ptab, |
|---|
| 911 | (user_vaddr >> 12), |
|---|
| 912 | &ppn, |
|---|
| 913 | &flags ); |
|---|
| 914 | user_pbase = (ppn << 12) | (user_vaddr & 0x00000FFF); |
|---|
| 915 | |
|---|
| 916 | if ( ko ) |
|---|
| 917 | { |
|---|
| 918 | _get_lock(&_tty_put_lock); |
|---|
| 919 | _puts("\n[GIET ERROR] in _fbdma_access() : user buffer unmapped\n"); |
|---|
| 920 | _release_lock(&_tty_put_lock); |
|---|
| 921 | return 3; |
|---|
| 922 | } |
|---|
| 923 | if ( (flags & PTE_U) == 0 ) |
|---|
| 924 | { |
|---|
| 925 | _get_lock(&_tty_put_lock); |
|---|
| 926 | _puts("[GIET ERROR] in _fbdma_access() : user buffer not in user space\n"); |
|---|
| 927 | _release_lock(&_tty_put_lock); |
|---|
| 928 | return 4; |
|---|
| 929 | } |
|---|
| 930 | if ( ( (flags & PTE_W) == 0 ) && to_user ) |
|---|
| 931 | { |
|---|
| 932 | _get_lock(&_tty_put_lock); |
|---|
| 933 | _puts("\n[GIET ERROR] in _fbdma_access() : user buffer not writable\n"); |
|---|
| 934 | _release_lock(&_tty_put_lock); |
|---|
| 935 | return 5; |
|---|
| 936 | } |
|---|
| 937 | |
|---|
| 938 | |
|---|
| 939 | |
|---|
| 940 | /* |
|---|
| 941 | // loop on all virtual pages covering the user buffer |
|---|
| 942 | unsigned int user_vpn_min = user_vaddr >> 12; |
|---|
| 943 | unsigned int user_vpn_max = (user_vaddr + length - 1) >> 12; |
|---|
| 944 | unsigned int ix2 = 0; |
|---|
| 945 | unsigned int ix1 = _dma_iommu_ix1 + dma_id; |
|---|
| 946 | |
|---|
| 947 | for ( vpn = user_vpn_min ; vpn <= user_vpn_max ; vpn++ ) |
|---|
| 948 | { |
|---|
| 949 | // get ppn and flags for each vpn |
|---|
| 950 | unsigned int ko = _v2p_translate( (page_table_t*)user_pt_vbase, |
|---|
| 951 | vpn, |
|---|
| 952 | &ppn, |
|---|
| 953 | &flags ); |
|---|
| 954 | |
|---|
| 955 | // check access rights |
|---|
| 956 | if ( ko ) return 3; // unmapped |
|---|
| 957 | if ( (flags & PTE_U) == 0 ) return 4; // not in user space |
|---|
| 958 | if ( ( (flags & PTE_W) == 0 ) && to_user ) return 5; // not writable |
|---|
| 959 | |
|---|
| 960 | // save first ppn value |
|---|
| 961 | if ( ix2 == 0 ) ppn_first = ppn; |
|---|
| 962 | |
|---|
| 963 | if ( IOMMU_ACTIVE ) // the user buffer must be remapped in the I/0 space |
|---|
| 964 | { |
|---|
| 965 | // check buffer length < 2 Mbytes |
|---|
| 966 | if ( ix2 > 511 ) return 2; |
|---|
| 967 | |
|---|
| 968 | // map the physical page in IOMMU page table |
|---|
| 969 | _iommu_add_pte2( ix1, // PT1 index |
|---|
| 970 | ix2, // PT2 index |
|---|
| 971 | ppn, // physical page number |
|---|
| 972 | flags ); // protection flags |
|---|
| 973 | } |
|---|
| 974 | else // no IOMMU : check that physical pages are contiguous |
|---|
| 975 | { |
|---|
| 976 | if ( (ppn - ppn_first) != ix2 ) return 6; // split physical buffer |
|---|
| 977 | } |
|---|
| 978 | |
|---|
| 979 | // increment page index |
|---|
| 980 | ix2++; |
|---|
| 981 | } // end for vpn |
|---|
| 982 | |
|---|
| 983 | // register the number of pages to be unmapped if iommu activated |
|---|
| 984 | _dma_iommu_npages[dma_id] = (user_vpn_max - user_vpn_min) + 1; |
|---|
| 985 | |
|---|
| 986 | */ |
|---|
| 987 | |
|---|
| 988 | // invalidate data cache in case of memory write |
|---|
| 989 | if ( to_user ) _dcache_buf_invalidate( (void*)user_vaddr, length ); |
|---|
| 990 | |
|---|
| 991 | // get the lock |
|---|
| 992 | _get_lock( &_dma_lock[dma_id] ); |
|---|
| 993 | |
|---|
| 994 | // DMA configuration |
|---|
| 995 | if ( to_user ) |
|---|
| 996 | { |
|---|
| 997 | dma_base[loc_id*DMA_SPAN + DMA_SRC] = (unsigned int)fb_pbase; |
|---|
| 998 | dma_base[loc_id*DMA_SPAN + DMA_DST] = (unsigned int)user_pbase; |
|---|
| 999 | } |
|---|
| 1000 | else |
|---|
| 1001 | { |
|---|
| 1002 | dma_base[loc_id*DMA_SPAN + DMA_SRC] = (unsigned int)user_pbase; |
|---|
| 1003 | dma_base[loc_id*DMA_SPAN + DMA_DST] = (unsigned int)fb_pbase; |
|---|
| 1004 | } |
|---|
| 1005 | dma_base[loc_id*DMA_SPAN + DMA_LEN] = (unsigned int)length; |
|---|
| 1006 | |
|---|
| 1007 | return 0; |
|---|
| 1008 | |
|---|
| 1009 | #else //NB_DMAS_MAX == 0 |
|---|
| 1010 | return -1; |
|---|
| 1011 | #endif |
|---|
| 1012 | } |
|---|
| 1013 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 1014 | // _fb_write() |
|---|
| 1015 | // Transfer data from a memory buffer to the frame_buffer device using DMA. |
|---|
| 1016 | // - offset : offset (in bytes) in the frame buffer. |
|---|
| 1017 | // - buffer : base address of the memory buffer. |
|---|
| 1018 | // - length : number of bytes to be transfered. |
|---|
| 1019 | // Returns 0 if success, > 0 if error. |
|---|
| 1020 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 1021 | unsigned int _fb_write( unsigned int offset, |
|---|
| 1022 | const void* buffer, |
|---|
| 1023 | unsigned int length ) |
|---|
| 1024 | { |
|---|
| 1025 | return _fb_dma_access( 0, // write to frame buffer |
|---|
| 1026 | offset, |
|---|
| 1027 | (unsigned int)buffer, |
|---|
| 1028 | length ); |
|---|
| 1029 | } |
|---|
| 1030 | |
|---|
| 1031 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 1032 | // _fb_read() |
|---|
| 1033 | // Transfer data from the frame_buffer device to a memory buffer using DMA. |
|---|
| 1034 | // - offset : offset (in bytes) in the frame buffer. |
|---|
| 1035 | // - buffer : base address of the memory buffer. |
|---|
| 1036 | // - length : number of bytes to be transfered. |
|---|
| 1037 | // Returns 0 if success, > 0 if error. |
|---|
| 1038 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 1039 | unsigned int _fb_read( unsigned int offset, |
|---|
| 1040 | const void* buffer, |
|---|
| 1041 | unsigned int length ) |
|---|
| 1042 | { |
|---|
| 1043 | return _fb_dma_access( 1, // read from frame buffer |
|---|
| 1044 | offset, |
|---|
| 1045 | (unsigned int)buffer, |
|---|
| 1046 | length ); |
|---|
| 1047 | } |
|---|
| 1048 | |
|---|
| 1049 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 1050 | // _fb_completed() |
|---|
| 1051 | // This function checks completion of a DMA transfer to or fom the frame buffer. |
|---|
| 1052 | // As it is a blocking call, the processor is busy waiting. |
|---|
| 1053 | // Returns 0 if success, > 0 if error |
|---|
| 1054 | // (1 == read error / 2 == DMA idle error / 3 == write error) |
|---|
| 1055 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 1056 | unsigned int _fb_completed() |
|---|
| 1057 | { |
|---|
| 1058 | #if NB_DMAS_MAX > 0 |
|---|
| 1059 | unsigned int task_id = _get_current_task_id(); |
|---|
| 1060 | unsigned int dma_id = _get_context_slot( task_id, CTX_FBDMA_ID ); |
|---|
| 1061 | |
|---|
| 1062 | // busy waiting with a pseudo random delay between bus access |
|---|
| 1063 | while (_dma_done[dma_id] == 0) |
|---|
| 1064 | { |
|---|
| 1065 | unsigned int i; |
|---|
| 1066 | unsigned int delay = ( _proctime() ^ _procid()<<4 ) & 0xFF; |
|---|
| 1067 | for (i = 0; i < delay; i++) |
|---|
| 1068 | asm volatile("nop"); |
|---|
| 1069 | } |
|---|
| 1070 | |
|---|
| 1071 | // unmap the buffer from IOMMU page table if IOMMU is activated |
|---|
| 1072 | if ( IOMMU_ACTIVE ) |
|---|
| 1073 | { |
|---|
| 1074 | unsigned int* iob_address = (unsigned int*) &seg_iob_base; |
|---|
| 1075 | |
|---|
| 1076 | unsigned int ix1 = _dma_iommu_ix1 + dma_id; |
|---|
| 1077 | unsigned int ix2; |
|---|
| 1078 | |
|---|
| 1079 | for ( ix2 = 0 ; ix2 < _dma_iommu_npages[dma_id] ; ix2++ ) |
|---|
| 1080 | { |
|---|
| 1081 | // unmap the page in IOMMU page table |
|---|
| 1082 | _iommu_inval_pte2( ix1, // PT1 index |
|---|
| 1083 | ix2 ); // PT2 index |
|---|
| 1084 | |
|---|
| 1085 | // clear IOMMU TLB |
|---|
| 1086 | iob_address[IOB_INVAL_PTE] = (ix1 << 21) | (ix2 << 12); |
|---|
| 1087 | } |
|---|
| 1088 | } |
|---|
| 1089 | |
|---|
| 1090 | // reset synchronization variables |
|---|
| 1091 | _dma_lock[dma_id] = 0; |
|---|
| 1092 | _dma_done[dma_id] = 0; |
|---|
| 1093 | |
|---|
| 1094 | return _dma_status[dma_id]; |
|---|
| 1095 | |
|---|
| 1096 | #else //NB_DMAS_MAX == 0 |
|---|
| 1097 | |
|---|
| 1098 | return -1; |
|---|
| 1099 | |
|---|
| 1100 | #endif |
|---|
| 1101 | } |
|---|
| 1102 | |
|---|