| 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 files is part of the GIET-VM nano-kernel. |
|---|
| 8 | // It contains the kernel entry point for the last step of system initialisation. |
|---|
| 9 | // All procs in this phase have their MMU activated, and are running in parallel. |
|---|
| 10 | // Each processor perform the following actions: |
|---|
| 11 | // 1/ contributes to _schedulers_paddr[] initialisation |
|---|
| 12 | // 2/ contributes to _ptabs_paddr[] and _ptabs_vaddr arrays initialisation |
|---|
| 13 | // 3/ computes and set the ICU mask for its private ICU channel |
|---|
| 14 | // 4/ initialises its private TICK timer (if required) |
|---|
| 15 | // 5/ initialises the "idle" task context in its private scheduler |
|---|
| 16 | // 6/ initialises the SP, SR, PTPR, EPC registers |
|---|
| 17 | // 7/ jumps to the user code with an eret. |
|---|
| 18 | //////////////////////////////////////////////////////////////////////////////////// |
|---|
| 19 | |
|---|
| 20 | #include <common.h> |
|---|
| 21 | #include <irq_handler.h> |
|---|
| 22 | #include <ctx_handler.h> |
|---|
| 23 | #include <sys_handler.h> |
|---|
| 24 | #include <mapping_info.h> |
|---|
| 25 | #include <giet_config.h> |
|---|
| 26 | #include <mips32_registers.h> |
|---|
| 27 | #include <irq_handler.h> |
|---|
| 28 | #include <vm_handler.h> |
|---|
| 29 | #include <hwr_mapping.h> |
|---|
| 30 | #include <mwmr_channel.h> |
|---|
| 31 | #include <barrier.h> |
|---|
| 32 | #include <drivers.h> |
|---|
| 33 | |
|---|
| 34 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 35 | // Kernel Global variables |
|---|
| 36 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 37 | |
|---|
| 38 | __attribute__((section (".kdata"))) |
|---|
| 39 | unsigned int _ptabs_paddr[GIET_NB_VSPACE_MAX]; |
|---|
| 40 | |
|---|
| 41 | __attribute__((section (".kdata"))) |
|---|
| 42 | unsigned int _ptabs_vaddr[GIET_NB_VSPACE_MAX]; |
|---|
| 43 | |
|---|
| 44 | __attribute__((section (".kdata"))) |
|---|
| 45 | static_scheduler_t* _schedulers_paddr[NB_CLUSTERS*NB_PROCS_MAX]; |
|---|
| 46 | |
|---|
| 47 | __attribute__((section (".kdata"))) |
|---|
| 48 | unsigned int _idle_stack[NB_CLUSTERS*NB_PROCS_MAX*64]; |
|---|
| 49 | |
|---|
| 50 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 51 | // This function is the entry point for the last step of the boot sequence. |
|---|
| 52 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 53 | __attribute__((section (".kinit"))) void _kernel_init() |
|---|
| 54 | { |
|---|
| 55 | // compute cluster and local processor index |
|---|
| 56 | unsigned int proc_id = _procid(); |
|---|
| 57 | unsigned int cluster_id = proc_id / NB_PROCS_MAX; |
|---|
| 58 | unsigned int lpid = proc_id % NB_PROCS_MAX; |
|---|
| 59 | |
|---|
| 60 | // step 1 : Initialise scheduler physical addresses array |
|---|
| 61 | |
|---|
| 62 | // get scheduler physical address from register |
|---|
| 63 | static_scheduler_t* psched = (static_scheduler_t*)_get_sched(); |
|---|
| 64 | _schedulers_paddr[proc_id] = psched; |
|---|
| 65 | |
|---|
| 66 | #if GIET_DEBUG_INIT |
|---|
| 67 | _get_lock(&_tty_put_lock); |
|---|
| 68 | _puts("\n[GIET DEBUG] step 1 for processor "); |
|---|
| 69 | _putd( proc_id ); |
|---|
| 70 | _puts(" / scheduler pbase = "); |
|---|
| 71 | _putw( (unsigned int)psched ); |
|---|
| 72 | _puts("\n"); |
|---|
| 73 | _release_lock(&_tty_put_lock); |
|---|
| 74 | #endif |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | // step 2 : initialise page table addresse arrays |
|---|
| 78 | // each processor scans all tasks contexts in its |
|---|
| 79 | // private scheduler and get VSID, PTAB and PTPR values |
|---|
| 80 | |
|---|
| 81 | unsigned int ltid; |
|---|
| 82 | unsigned int tasks = _get_tasks_number(); |
|---|
| 83 | |
|---|
| 84 | for ( ltid = 0 ; ltid < tasks ; ltid++ ) |
|---|
| 85 | { |
|---|
| 86 | unsigned int vspace_id = _get_context_slot( ltid , CTX_VSID_ID ); |
|---|
| 87 | unsigned int ptab_vaddr = _get_context_slot( ltid , CTX_PTAB_ID ); |
|---|
| 88 | unsigned int ptab_paddr = _get_context_slot( ltid , CTX_PTPR_ID ) << 13; |
|---|
| 89 | |
|---|
| 90 | _ptabs_vaddr[vspace_id] = ptab_vaddr; |
|---|
| 91 | _ptabs_paddr[vspace_id] = ptab_paddr; |
|---|
| 92 | |
|---|
| 93 | #if GIET_DEBUG_INIT |
|---|
| 94 | _get_lock(&_tty_put_lock); |
|---|
| 95 | _puts("\n[GIET DEBUG] step 2 for processor "); |
|---|
| 96 | _putd( proc_id ); |
|---|
| 97 | _puts(" / vspace "); |
|---|
| 98 | _putd( vspace_id ); |
|---|
| 99 | _puts("\n- ptab vbase = "); |
|---|
| 100 | _putw( ptab_vaddr ); |
|---|
| 101 | _puts("\n- ptab pbase = "); |
|---|
| 102 | _putw( ptab_paddr ); |
|---|
| 103 | _puts("\n"); |
|---|
| 104 | _release_lock(&_tty_put_lock); |
|---|
| 105 | #endif |
|---|
| 106 | |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | // step 3 : compute and set ICU masks |
|---|
| 110 | // there is at most 32 interrupts per processor |
|---|
| 111 | // software interrupts are not supported yet |
|---|
| 112 | |
|---|
| 113 | unsigned int irq_id; |
|---|
| 114 | unsigned int hwi_mask = 0; |
|---|
| 115 | unsigned int pti_mask = 0; |
|---|
| 116 | |
|---|
| 117 | for ( irq_id = 0 ; irq_id < 32 ; irq_id++ ) |
|---|
| 118 | { |
|---|
| 119 | unsigned int entry = _get_interrupt_vector_entry(irq_id); |
|---|
| 120 | unsigned int isr = entry & 0x000000FF; |
|---|
| 121 | |
|---|
| 122 | if ( (isr == ISR_DMA) || (isr == ISR_IOC) || (isr == ISR_TTY) ) |
|---|
| 123 | { |
|---|
| 124 | hwi_mask = hwi_mask | 0x1<< irq_id; |
|---|
| 125 | } |
|---|
| 126 | else if ( (isr == ISR_SWITCH) || (isr == ISR_TIMER) ) |
|---|
| 127 | { |
|---|
| 128 | pti_mask = pti_mask | 0x1<< irq_id; |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | _icu_set_mask( cluster_id, lpid, hwi_mask, 0 ); // set HWI_MASK |
|---|
| 132 | _icu_set_mask( cluster_id, lpid, pti_mask, 1 ); // set PTI_MASK |
|---|
| 133 | |
|---|
| 134 | #if GIET_DEBUG_INIT |
|---|
| 135 | _get_lock(&_tty_put_lock); |
|---|
| 136 | _puts("\n[GIET DEBUG] step 3 for processor "); |
|---|
| 137 | _putd( proc_id ); |
|---|
| 138 | _puts("\n - ICU HWI_MASK = "); |
|---|
| 139 | _putw( hwi_mask ); |
|---|
| 140 | _puts("\n - ICU PTI_MASK = "); |
|---|
| 141 | _putw( pti_mask ); |
|---|
| 142 | _puts("\n"); |
|---|
| 143 | _release_lock(&_tty_put_lock); |
|---|
| 144 | #endif |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | // step 4 : start TICK timer if more than one task |
|---|
| 148 | if ( tasks > 1 ) |
|---|
| 149 | { |
|---|
| 150 | _timer_start( cluster_id, |
|---|
| 151 | proc_id, |
|---|
| 152 | GIET_TICK_VALUE ); |
|---|
| 153 | |
|---|
| 154 | #if GIET_DEBUG_INIT |
|---|
| 155 | _get_lock(&_tty_put_lock); |
|---|
| 156 | _puts("\n[GIET DEBUG] Step 4 for processor "); |
|---|
| 157 | _putd( proc_id ); |
|---|
| 158 | _puts(" / context switch activated\n"); |
|---|
| 159 | _release_lock(&_tty_put_lock); |
|---|
| 160 | #endif |
|---|
| 161 | |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | // step 5 : initialise the "idle" task context |
|---|
| 165 | // the SR initialisation value is 0xFF03 because |
|---|
| 166 | // the task _ctx_idle() executes in kernel mode... |
|---|
| 167 | // it uses the page table of vspace[0] |
|---|
| 168 | |
|---|
| 169 | _set_context_slot( IDLE_TASK_INDEX, CTX_RUN_ID, 1 ); |
|---|
| 170 | _set_context_slot( IDLE_TASK_INDEX, CTX_SR_ID, 0xFF03 ); |
|---|
| 171 | _set_context_slot( IDLE_TASK_INDEX, CTX_SP_ID, (unsigned int)&_idle_stack[proc_id] + 64 ); |
|---|
| 172 | _set_context_slot( IDLE_TASK_INDEX, CTX_RA_ID, (unsigned int)&_ctx_eret ); |
|---|
| 173 | _set_context_slot( IDLE_TASK_INDEX, CTX_EPC_ID, (unsigned int)&_ctx_idle ); |
|---|
| 174 | _set_context_slot( IDLE_TASK_INDEX, CTX_LTID_ID, IDLE_TASK_INDEX ); |
|---|
| 175 | _set_context_slot( IDLE_TASK_INDEX, CTX_PTPR_ID, _ptabs_paddr[0] >> 13 ); |
|---|
| 176 | |
|---|
| 177 | #if GIET_DEBUG_INIT |
|---|
| 178 | _get_lock(&_tty_put_lock); |
|---|
| 179 | _puts("\n[GIET DEBUG] Step 5 for processor "); |
|---|
| 180 | _putd( proc_id ); |
|---|
| 181 | _puts(" / idle task context set\n"); |
|---|
| 182 | _release_lock(&_tty_put_lock); |
|---|
| 183 | #endif |
|---|
| 184 | |
|---|
| 185 | // step 6 : each processor initialises SP, SR, PTPR, EPC, registers |
|---|
| 186 | // with the values corresponding to the first allocated task, |
|---|
| 187 | // and starts the "idle" task if there is no task allocated. |
|---|
| 188 | |
|---|
| 189 | unsigned int task_id; |
|---|
| 190 | |
|---|
| 191 | if ( tasks == 0 ) |
|---|
| 192 | { |
|---|
| 193 | task_id = IDLE_TASK_INDEX; |
|---|
| 194 | |
|---|
| 195 | _get_lock( &_tty_put_lock ); |
|---|
| 196 | _puts("\n [GIET WARNING] No task allocated to processor "); |
|---|
| 197 | _putw( proc_id ); |
|---|
| 198 | _puts(" => idle\n"); |
|---|
| 199 | _release_lock ( &_tty_put_lock ); |
|---|
| 200 | } |
|---|
| 201 | else |
|---|
| 202 | { |
|---|
| 203 | task_id = 0; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | unsigned int sp_value = _get_context_slot( task_id, CTX_SP_ID ); |
|---|
| 207 | unsigned int sr_value = _get_context_slot( task_id, CTX_SR_ID ); |
|---|
| 208 | unsigned int ptpr_value = _get_context_slot( task_id, CTX_PTPR_ID ); |
|---|
| 209 | unsigned int epc_value = _get_context_slot( task_id, CTX_EPC_ID ); |
|---|
| 210 | |
|---|
| 211 | #if GIET_DEBUG_INIT |
|---|
| 212 | _get_lock(&_tty_put_lock); |
|---|
| 213 | _puts("\n[GIET DEBUG] step 6 for processor "); |
|---|
| 214 | _putd( proc_id ); |
|---|
| 215 | _puts(" / registers initialised \n"); |
|---|
| 216 | _puts("- sp = "); |
|---|
| 217 | _putw( sp_value ); |
|---|
| 218 | _puts("\n"); |
|---|
| 219 | _puts("- sr = "); |
|---|
| 220 | _putw( sr_value ); |
|---|
| 221 | _puts("\n"); |
|---|
| 222 | _puts("- ptpr = "); |
|---|
| 223 | _putw( ptpr_value<<13 ); |
|---|
| 224 | _puts("\n"); |
|---|
| 225 | _puts("- epc = "); |
|---|
| 226 | _putw( epc_value ); |
|---|
| 227 | _puts("\n"); |
|---|
| 228 | _release_lock(&_tty_put_lock); |
|---|
| 229 | #endif |
|---|
| 230 | |
|---|
| 231 | // set registers and jump to user code |
|---|
| 232 | asm volatile ( "move $29, %0 \n" /* SP <= ctx[CTX_SP_ID] */ |
|---|
| 233 | "mtc0 %1, $12 \n" /* SR <= ctx[CTX_SR_ID] */ |
|---|
| 234 | "mtc2 %2, $0 \n" /* PTPR <= ctx[CTX_PTPR_ID] */ |
|---|
| 235 | "mtc0 %3, $14 \n" /* EPC <= ctx[CTX_EPC_ID] */ |
|---|
| 236 | "eret \n" /* jump to user code */ |
|---|
| 237 | "nop \n" |
|---|
| 238 | : |
|---|
| 239 | : "r"(sp_value), "r"(sr_value), "r"(ptpr_value), "r"(epc_value) ); |
|---|
| 240 | |
|---|
| 241 | } // end _kernel_init() |
|---|