| 1 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | // File : kernel_init.c |
|---|
| 3 | // Date : 26/05/2012 |
|---|
| 4 | // Authors : alain greiner & mohamed karaoui |
|---|
| 5 | // Copyright (c) UPMC-LIP6 |
|---|
| 6 | //////////////////////////////////////////////////////////////////////////////////// |
|---|
| 7 | // This kernel_init.c file is part of the GIET-VM nano-kernel. |
|---|
| 8 | //////////////////////////////////////////////////////////////////////////////////// |
|---|
| 9 | |
|---|
| 10 | #include <giet_config.h> |
|---|
| 11 | #include <hard_config.h> |
|---|
| 12 | #include <utils.h> |
|---|
| 13 | #include <tty0.h> |
|---|
| 14 | #include <kernel_malloc.h> |
|---|
| 15 | #include <fat32.h> |
|---|
| 16 | #include <xcu_driver.h> |
|---|
| 17 | #include <ioc_driver.h> |
|---|
| 18 | #include <ctx_handler.h> |
|---|
| 19 | #include <irq_handler.h> |
|---|
| 20 | #include <mapping_info.h> |
|---|
| 21 | #include <mips32_registers.h> |
|---|
| 22 | |
|---|
| 23 | #if !defined(X_SIZE) |
|---|
| 24 | # error: You must define X_SIZE in the hard_config.h file |
|---|
| 25 | #endif |
|---|
| 26 | |
|---|
| 27 | #if !defined(Y_SIZE) |
|---|
| 28 | # error: You must define Y_SIZE in the hard_config.h file |
|---|
| 29 | #endif |
|---|
| 30 | |
|---|
| 31 | #if !defined(Y_WIDTH) |
|---|
| 32 | # error: You must define Y_WIDTH in the hard_config.h file |
|---|
| 33 | #endif |
|---|
| 34 | |
|---|
| 35 | #if !defined(Y_WIDTH) |
|---|
| 36 | # error: You must define Y_WIDTH in the hard_config.h file |
|---|
| 37 | #endif |
|---|
| 38 | |
|---|
| 39 | #if !defined(NB_PROCS_MAX) |
|---|
| 40 | # error: You must define NB_PROCS_MAX in the hard_config.h file |
|---|
| 41 | #endif |
|---|
| 42 | |
|---|
| 43 | #if !defined(NB_TOTAL_PROCS) |
|---|
| 44 | # error: You must define NB_TOTAL_PROCS in the hard_config.h file |
|---|
| 45 | #endif |
|---|
| 46 | |
|---|
| 47 | #if !defined(USE_XCU) |
|---|
| 48 | # error: You must define USE_XCU in the hard_config.h file |
|---|
| 49 | #endif |
|---|
| 50 | |
|---|
| 51 | #if !defined(IDLE_TASK_INDEX) |
|---|
| 52 | # error: You must define IDLE_TASK_INDEX in the ctx_handler.h file |
|---|
| 53 | #endif |
|---|
| 54 | |
|---|
| 55 | #if !defined(GIET_TICK_VALUE) |
|---|
| 56 | # error: You must define GIET_TICK_VALUE in the giet_config.h file |
|---|
| 57 | #endif |
|---|
| 58 | |
|---|
| 59 | #if !defined(GIET_NB_VSPACE_MAX) |
|---|
| 60 | # error: You must define GIET_NB_VSPACE_MAX in the giet_config.h file |
|---|
| 61 | #endif |
|---|
| 62 | |
|---|
| 63 | #if !defined(NB_TTY_CHANNELS) |
|---|
| 64 | # error: You must define NB_TTY_CHANNELS in the hard_config.h file |
|---|
| 65 | #endif |
|---|
| 66 | |
|---|
| 67 | #if (NB_TTY_CHANNELS < 1) |
|---|
| 68 | # error: NB_TTY_CHANNELS cannot be smaller than 1 |
|---|
| 69 | #endif |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 74 | // Ditributed kernel heap descriptors array (for dynamic memory allocation) |
|---|
| 75 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 76 | |
|---|
| 77 | kernel_heap_t kernel_heap[X_SIZE][Y_SIZE]; |
|---|
| 78 | |
|---|
| 79 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 80 | // FAT internal representation for kernel code |
|---|
| 81 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 82 | |
|---|
| 83 | fat32_fs_t fat __attribute__((aligned(512))); |
|---|
| 84 | |
|---|
| 85 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 86 | // array of pointers on the page tables (virtual addresses) |
|---|
| 87 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 88 | |
|---|
| 89 | volatile unsigned int _ptabs_vaddr[GIET_NB_VSPACE_MAX]; // virtual addresses |
|---|
| 90 | volatile unsigned int _ptabs_ptprs[GIET_NB_VSPACE_MAX]; // physical addresses >> 13 |
|---|
| 91 | |
|---|
| 92 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 93 | // Array of pointers on the schedulers (physical addresses) |
|---|
| 94 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 95 | |
|---|
| 96 | volatile static_scheduler_t* _schedulers[X_SIZE][Y_SIZE][NB_PROCS_MAX]; |
|---|
| 97 | |
|---|
| 98 | //////////////////////////////////////////////////////////////////////////////////// |
|---|
| 99 | // Synchonisation barrier before jumping to user code |
|---|
| 100 | //////////////////////////////////////////////////////////////////////////////////// |
|---|
| 101 | |
|---|
| 102 | volatile unsigned int kernel_init_barrier = 0; |
|---|
| 103 | |
|---|
| 104 | //////////////////////////////////////////////////////////////////////////////////// |
|---|
| 105 | // Global variables for TTY/kernel communications |
|---|
| 106 | //////////////////////////////////////////////////////////////////////////////////// |
|---|
| 107 | |
|---|
| 108 | sbt_lock_t _tty_tx_lock[NB_TTY_CHANNELS] __attribute__((aligned(64))); |
|---|
| 109 | |
|---|
| 110 | unsigned int _tty_rx_buf[NB_TTY_CHANNELS]; |
|---|
| 111 | unsigned int _tty_rx_full[NB_TTY_CHANNELS]; |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 118 | // This kernel_init() function completes the kernel initialisation in 7 steps: |
|---|
| 119 | // All processors execute this code, but this is done sequencially. |
|---|
| 120 | // - step 0 : Initialise fat, heap descriptors, and tty locks |
|---|
| 121 | // - step 1 : Initialise scheduler pointers array |
|---|
| 122 | // - step 2 : Initialise PTAB pointers arrays |
|---|
| 123 | // - step 3 : Initialise private XCU masks |
|---|
| 124 | // - step 4 : |
|---|
| 125 | // - step 5 : |
|---|
| 126 | // - step 6 : |
|---|
| 127 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 128 | __attribute__((section (".kinit"))) void kernel_init() |
|---|
| 129 | { |
|---|
| 130 | // gpid : hardware processor index (fixed format: X_WIDTH|Y_WIDTH|P_WIDTH) |
|---|
| 131 | // p : local processor id in a cluster ( p < NB_PROCS_MAX) |
|---|
| 132 | // cpid : continuous processor index = (((x * Y_SIZE + y) * NB_PROCS_MAX) + p |
|---|
| 133 | |
|---|
| 134 | unsigned int gpid = _get_procid(); |
|---|
| 135 | unsigned int cluster_xy = gpid >> P_WIDTH; |
|---|
| 136 | unsigned int x = cluster_xy >> Y_WIDTH & ((1<<X_WIDTH)-1); |
|---|
| 137 | unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1); |
|---|
| 138 | unsigned int p = gpid & ((1<<P_WIDTH)-1); |
|---|
| 139 | unsigned int cpid = ((( x * Y_SIZE) + y) * NB_PROCS_MAX) + p; |
|---|
| 140 | |
|---|
| 141 | // This initialisation is done sequencially by each processor |
|---|
| 142 | while( cpid != kernel_init_barrier ) asm volatile ( "nop" ); |
|---|
| 143 | |
|---|
| 144 | // Step 0 : P[0,0,0] initialises the kernel FAT |
|---|
| 145 | // and the distributed kernel_heap descriptors array. |
|---|
| 146 | if ( gpid == 0 ) |
|---|
| 147 | { |
|---|
| 148 | _heap_init(); |
|---|
| 149 | |
|---|
| 150 | #if GIET_DEBUG_INIT |
|---|
| 151 | _nolock_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] completes kernel HEAP init\n", x, y, p ); |
|---|
| 152 | #endif |
|---|
| 153 | unsigned int channel; |
|---|
| 154 | for ( channel = 0 ; channel < NB_TTY_CHANNELS ; channel++ ) |
|---|
| 155 | { |
|---|
| 156 | _sbt_lock_init( &_tty_tx_lock[channel] ); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | #if GIET_DEBUG_INIT |
|---|
| 160 | _nolock_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] completes TTY locks init\n", x, y, p ); |
|---|
| 161 | #endif |
|---|
| 162 | |
|---|
| 163 | _fat_init( IOC_BOOT_MODE ); |
|---|
| 164 | |
|---|
| 165 | #if GIET_DEBUG_INIT |
|---|
| 166 | _nolock_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] completes kernel FAT init\n", x, y, p ); |
|---|
| 167 | #endif |
|---|
| 168 | |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | // Step 1 : each processor get its scheduler virtual address from CP0_SCHED register |
|---|
| 172 | // and contributes to _schedulers[] array initialisation |
|---|
| 173 | |
|---|
| 174 | static_scheduler_t* psched = (static_scheduler_t*)_get_sched(); |
|---|
| 175 | unsigned int tasks = psched->tasks; |
|---|
| 176 | |
|---|
| 177 | _schedulers[x][y][p] = psched; |
|---|
| 178 | |
|---|
| 179 | #if GIET_DEBUG_INIT |
|---|
| 180 | _nolock_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] initialises SCHED array\n" |
|---|
| 181 | " - scheduler vbase = %x\n" |
|---|
| 182 | " - tasks = %d\n", |
|---|
| 183 | x, y, p, (unsigned int)psched, tasks ); |
|---|
| 184 | #endif |
|---|
| 185 | |
|---|
| 186 | // step 2 : each processor that is allocated at least one task loops |
|---|
| 187 | // on all allocated tasks: |
|---|
| 188 | // - contributes to _ptabs_vaddr[] & _ptabs_ptprs[] initialisation. |
|---|
| 189 | // - set CTX_RA slot with the kernel _ctx_eret() virtual address. |
|---|
| 190 | // - set CTX_EPC slot that must contain the task entry point, |
|---|
| 191 | // and contain only at this point the virtual address of the memory |
|---|
| 192 | // location containing this entry point. |
|---|
| 193 | |
|---|
| 194 | unsigned int ltid; |
|---|
| 195 | |
|---|
| 196 | for (ltid = 0; ltid < tasks; ltid++) |
|---|
| 197 | { |
|---|
| 198 | unsigned int vsid = _get_task_slot( x, y, p, ltid , CTX_VSID_ID ); |
|---|
| 199 | unsigned int ptab = _get_task_slot( x, y, p, ltid , CTX_PTAB_ID ); |
|---|
| 200 | unsigned int ptpr = _get_task_slot( x, y, p, ltid , CTX_PTPR_ID ); |
|---|
| 201 | |
|---|
| 202 | // initialize PTABS arrays |
|---|
| 203 | _ptabs_vaddr[vsid] = ptab; |
|---|
| 204 | _ptabs_ptprs[vsid] = ptpr; |
|---|
| 205 | |
|---|
| 206 | #if GIET_DEBUG_INIT |
|---|
| 207 | _nolock_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] initialises PTABS arrays\n" |
|---|
| 208 | " - ptabs_vaddr[%d] = %x / ptpr_paddr[%d] = %l\n", |
|---|
| 209 | x, y, p, |
|---|
| 210 | vsid, ptab, vsid, ((unsigned long long)ptpr)<<13 ); |
|---|
| 211 | #endif |
|---|
| 212 | |
|---|
| 213 | // set the ptpr to use the task page table |
|---|
| 214 | asm volatile( "mtc2 %0, $0 \n" |
|---|
| 215 | : : "r" (ptpr) ); |
|---|
| 216 | |
|---|
| 217 | // compute ctx_ra |
|---|
| 218 | unsigned int ctx_ra = (unsigned int)(&_ctx_eret); |
|---|
| 219 | _set_task_slot( x, y, p, ltid, CTX_RA_ID, ctx_ra ); |
|---|
| 220 | |
|---|
| 221 | // compute ctx_epc |
|---|
| 222 | unsigned int* ptr = (unsigned int*)_get_task_slot( x, y, p, ltid, CTX_EPC_ID ); |
|---|
| 223 | _set_task_slot( x, y, p, ltid, CTX_EPC_ID, *ptr ); |
|---|
| 224 | |
|---|
| 225 | #if GIET_DEBUG_INIT |
|---|
| 226 | _nolock_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] updates context for task %d\n" |
|---|
| 227 | " - ctx_epc = %x\n" |
|---|
| 228 | " - ctx_ra = %x\n", |
|---|
| 229 | x, y, p, ltid, |
|---|
| 230 | _get_task_slot( x, y, p, ltid, CTX_EPC_ID ), |
|---|
| 231 | _get_task_slot( x, y, p, ltid, CTX_RA_ID ) ); |
|---|
| 232 | #endif |
|---|
| 233 | |
|---|
| 234 | } // end for tasks |
|---|
| 235 | |
|---|
| 236 | // step 3 : compute and set XCU masks |
|---|
| 237 | |
|---|
| 238 | unsigned int isr_switch_index = 0xFFFFFFFF; |
|---|
| 239 | unsigned int hwi_mask = 0; |
|---|
| 240 | unsigned int pti_mask = 0; |
|---|
| 241 | unsigned int wti_mask = 0; |
|---|
| 242 | unsigned int irq_id; // IN_IRQ index |
|---|
| 243 | unsigned int entry; // interrupt vector entry |
|---|
| 244 | |
|---|
| 245 | for (irq_id = 0; irq_id < 32; irq_id++) |
|---|
| 246 | { |
|---|
| 247 | entry = psched->hwi_vector[irq_id]; |
|---|
| 248 | if ( entry & 0x80000000 ) hwi_mask = hwi_mask | (1<<irq_id); |
|---|
| 249 | if ( (entry & 0x0000FFFF) == ISR_TICK ) isr_switch_index = irq_id; |
|---|
| 250 | |
|---|
| 251 | entry = psched->pti_vector[irq_id]; |
|---|
| 252 | if ( entry & 0x80000000 ) pti_mask = pti_mask | (1<<irq_id); |
|---|
| 253 | if ( (entry & 0x0000FFFF) == ISR_TICK ) isr_switch_index = irq_id; |
|---|
| 254 | |
|---|
| 255 | entry = psched->wti_vector[irq_id]; |
|---|
| 256 | if ( entry & 0x80000000 ) wti_mask = wti_mask | (1<<irq_id); |
|---|
| 257 | if ( (entry & 0x0000FFFF) == ISR_TICK ) isr_switch_index = irq_id; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | #if GIET_DEBUG_INIT |
|---|
| 261 | _nolock_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] sets XCU masks\n" |
|---|
| 262 | " - XCU HWI_MASK = %x\n" |
|---|
| 263 | " - XCU WTI_MASK = %x\n" |
|---|
| 264 | " - XCU PTI_MASK = %x\n", |
|---|
| 265 | x, y, p, hwi_mask, wti_mask, pti_mask ); |
|---|
| 266 | #endif |
|---|
| 267 | |
|---|
| 268 | unsigned int channel = p * IRQ_PER_PROCESSOR; |
|---|
| 269 | |
|---|
| 270 | _xcu_set_mask( cluster_xy, channel, hwi_mask, IRQ_TYPE_HWI ); |
|---|
| 271 | _xcu_set_mask( cluster_xy, channel, wti_mask, IRQ_TYPE_WTI ); |
|---|
| 272 | _xcu_set_mask( cluster_xy, channel, pti_mask, IRQ_TYPE_PTI ); |
|---|
| 273 | |
|---|
| 274 | // step 4 : start TICK timer if at least one task |
|---|
| 275 | if (tasks > 0) |
|---|
| 276 | { |
|---|
| 277 | // one ISR_TICK must be defined for each proc |
|---|
| 278 | if (isr_switch_index == 0xFFFFFFFF) |
|---|
| 279 | { |
|---|
| 280 | _nolock_printf("\n[GIET ERROR] ISR_TICK not found for processor[%d,%d,%d]\n", |
|---|
| 281 | x, y, p ); |
|---|
| 282 | _exit(); |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | // start system timer |
|---|
| 286 | _xcu_timer_start( cluster_xy, isr_switch_index, GIET_TICK_VALUE ); |
|---|
| 287 | |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | #if GIET_DEBUG_INIT |
|---|
| 291 | _nolock_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] starts TICK timer\n", |
|---|
| 292 | x, y, p ); |
|---|
| 293 | #endif |
|---|
| 294 | |
|---|
| 295 | // step 5 : each processor updates the idle_task context: |
|---|
| 296 | // (CTX_SP, CTX_RA, CTX_EPC). |
|---|
| 297 | // The 4 Kbytes idle stack is implemented in the scheduler. |
|---|
| 298 | // The PTPR register, the CTX_PTPR and CTX_PTAB slots |
|---|
| 299 | // have been initialised in boot code. |
|---|
| 300 | |
|---|
| 301 | unsigned int pstack = ((unsigned int)psched) + 0x2000; |
|---|
| 302 | |
|---|
| 303 | _set_task_slot( x, y, p, IDLE_TASK_INDEX, CTX_SP_ID, pstack); |
|---|
| 304 | _set_task_slot( x, y, p, IDLE_TASK_INDEX, CTX_RA_ID, (unsigned int) &_ctx_eret); |
|---|
| 305 | _set_task_slot( x, y, p, IDLE_TASK_INDEX, CTX_EPC_ID, (unsigned int) &_idle_task); |
|---|
| 306 | |
|---|
| 307 | #if GIET_DEBUG_INIT |
|---|
| 308 | _nolock_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] initializes IDLE task\n" |
|---|
| 309 | " - stack_base = %x\n" |
|---|
| 310 | " - stack_size = 0x1000\n", |
|---|
| 311 | x, y, p, pstack - 0x1000 ); |
|---|
| 312 | #endif |
|---|
| 313 | |
|---|
| 314 | // step 6 : when all processors reach the synchronisation barrier, |
|---|
| 315 | // each processor set registers SP, SR, PTPR, EPC, |
|---|
| 316 | // with the values corresponding to the first allocated task, |
|---|
| 317 | // or to the idle_task if there is no task allocated, |
|---|
| 318 | // and jump to user code |
|---|
| 319 | |
|---|
| 320 | if (tasks == 0) |
|---|
| 321 | { |
|---|
| 322 | ltid = IDLE_TASK_INDEX; |
|---|
| 323 | |
|---|
| 324 | _nolock_printf("\n[GIET WARNING] No task allocated to processor[%d,%d,%d]\n", |
|---|
| 325 | x, y, p ); |
|---|
| 326 | } |
|---|
| 327 | else |
|---|
| 328 | { |
|---|
| 329 | ltid = 0; |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | unsigned int sp_value = _get_task_slot( x, y, p, ltid, CTX_SP_ID); |
|---|
| 333 | unsigned int sr_value = _get_task_slot( x, y, p, ltid, CTX_SR_ID); |
|---|
| 334 | unsigned int ptpr_value = _get_task_slot( x, y, p, ltid, CTX_PTPR_ID); |
|---|
| 335 | unsigned int epc_value = _get_task_slot( x, y, p, ltid, CTX_EPC_ID); |
|---|
| 336 | |
|---|
| 337 | #if GIET_DEBUG_INIT |
|---|
| 338 | _nolock_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] reach barrier at cycle %d\n", |
|---|
| 339 | x, y, p, _get_proctime() ); |
|---|
| 340 | #endif |
|---|
| 341 | |
|---|
| 342 | // increment barrier counter |
|---|
| 343 | kernel_init_barrier++; |
|---|
| 344 | |
|---|
| 345 | // busy waiting until all processors synchronized |
|---|
| 346 | while ( kernel_init_barrier != NB_TOTAL_PROCS ); |
|---|
| 347 | |
|---|
| 348 | #if GIET_DEBUG_INIT |
|---|
| 349 | _printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] initializes registers at cycle %d\n" |
|---|
| 350 | " - sp = %x\n" |
|---|
| 351 | " - sr = %x\n" |
|---|
| 352 | " - ptpr = %x\n" |
|---|
| 353 | " - epc = %x\n", |
|---|
| 354 | x, y, p, _get_proctime(), |
|---|
| 355 | sp_value, sr_value, ptpr_value, epc_value ); |
|---|
| 356 | #endif |
|---|
| 357 | |
|---|
| 358 | // set registers and jump to user code |
|---|
| 359 | asm volatile ( "move $29, %0 \n" /* SP <= ctx[CTX_SP_ID] */ |
|---|
| 360 | "mtc0 %1, $12 \n" /* SR <= ctx[CTX_SR_ID] */ |
|---|
| 361 | "mtc2 %2, $0 \n" /* PTPR <= ctx[CTX_PTPR] */ |
|---|
| 362 | "mtc0 %3, $14 \n" /* EPC <= ctx[CTX_EPC] */ |
|---|
| 363 | "eret \n" /* jump to user code */ |
|---|
| 364 | "nop \n" |
|---|
| 365 | : |
|---|
| 366 | : "r"(sp_value), "r"(sr_value), "r"(ptpr_value), "r"(epc_value) |
|---|
| 367 | : "$29", "memory" ); |
|---|
| 368 | |
|---|
| 369 | } // end kernel_init() |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | // Local Variables: |
|---|
| 373 | // tab-width: 4 |
|---|
| 374 | // c-basic-offset: 4 |
|---|
| 375 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
|---|
| 376 | // indent-tabs-mode: nil |
|---|
| 377 | // End: |
|---|
| 378 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
|---|
| 379 | |
|---|