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