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