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