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 <kernel_utils.h> |
---|
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 | |
---|
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) |
---|
50 | # error: You must define IDLE_TASK_INDEX in the ctx_handler.h file |
---|
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 | |
---|
61 | /////////////////////////////////////////////////////////////////////////////////// |
---|
62 | // FAT internal representation for kernel code |
---|
63 | /////////////////////////////////////////////////////////////////////////////////// |
---|
64 | |
---|
65 | __attribute__((section (".kdata"))) |
---|
66 | fat32_fs_t fat __attribute__((aligned(512))); |
---|
67 | |
---|
68 | /////////////////////////////////////////////////////////////////////////////////// |
---|
69 | // array of pointers on the page tables (virtual addresses) |
---|
70 | /////////////////////////////////////////////////////////////////////////////////// |
---|
71 | |
---|
72 | __attribute__((section (".kdata"))) |
---|
73 | volatile unsigned int _ptabs_vaddr[GIET_NB_VSPACE_MAX]; // virtual addresses |
---|
74 | |
---|
75 | __attribute__((section (".kdata"))) |
---|
76 | volatile unsigned int _ptabs_ptprs[GIET_NB_VSPACE_MAX]; // physical addresses >> 13 |
---|
77 | |
---|
78 | /////////////////////////////////////////////////////////////////////////////////// |
---|
79 | // array of pointers on the schedulers (physical addresses) |
---|
80 | /////////////////////////////////////////////////////////////////////////////////// |
---|
81 | |
---|
82 | __attribute__((section (".kdata"))) |
---|
83 | volatile static_scheduler_t* _schedulers[X_SIZE][Y_SIZE][NB_PROCS_MAX]; |
---|
84 | |
---|
85 | //////////////////////////////////////////////////////////////////////////////////// |
---|
86 | // Synchonisation barrier before jumping to user code |
---|
87 | //////////////////////////////////////////////////////////////////////////////////// |
---|
88 | |
---|
89 | __attribute__((section (".kdata"))) |
---|
90 | volatile unsigned int kernel_init_barrier = 0; |
---|
91 | |
---|
92 | /////////////////////////////////////////////////////////////////////////////////// |
---|
93 | __attribute__((section (".kinit"))) void kernel_init() |
---|
94 | { |
---|
95 | // gpid : hardware processor index (fixed format: X_WIDTH|Y_WIDTH|P_WIDTH) |
---|
96 | // p : local processor id in a cluster ( p < NB_PROCS_MAX) |
---|
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); |
---|
102 | unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1); |
---|
103 | unsigned int p = gpid & ((1<<P_WIDTH)-1); |
---|
104 | unsigned int cpid = ((( x * Y_SIZE) + y) * NB_PROCS_MAX) + p; |
---|
105 | |
---|
106 | |
---|
107 | // This initialisation is done sequencially by each processor |
---|
108 | while( cpid != kernel_init_barrier ) asm volatile ( "nop" ); |
---|
109 | |
---|
110 | // Step 1 : each processor get its scheduler virtual address from CP0_SCHED register |
---|
111 | // and contributes to _schedulers[] array initialisation |
---|
112 | |
---|
113 | static_scheduler_t* psched = (static_scheduler_t*)_get_sched(); |
---|
114 | unsigned int tasks = psched->tasks; |
---|
115 | |
---|
116 | _schedulers[x][y][p] = psched; |
---|
117 | |
---|
118 | #if GIET_DEBUG_INIT |
---|
119 | _printf("\n[GIET DEBUG INIT] Processor[%d,%d,%d] starts kernel init\n" |
---|
120 | " - scheduler vbase = %x\n" |
---|
121 | " - tasks = %d\n", |
---|
122 | x, y, p, (unsigned int)psched, tasks ); |
---|
123 | #endif |
---|
124 | |
---|
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 |
---|
131 | // location containing this entry point. |
---|
132 | |
---|
133 | unsigned int ltid; |
---|
134 | |
---|
135 | for (ltid = 0; ltid < tasks; ltid++) |
---|
136 | { |
---|
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 ); |
---|
140 | |
---|
141 | // initialize PTABS arrays |
---|
142 | _ptabs_vaddr[vsid] = ptab; |
---|
143 | _ptabs_ptprs[vsid] = ptpr; |
---|
144 | |
---|
145 | #if GIET_DEBUG_INIT |
---|
146 | _printf("\n[GIET DEBUG INIT] Processor[%d,%d,%d] contributes to PTABS arrays\n" |
---|
147 | " - ptabs_vaddr[%d] = %x / ptpr_paddr[%d] = %l\n", |
---|
148 | x, y, p, |
---|
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 |
---|
157 | unsigned int ctx_ra = (unsigned int)(&_ctx_eret); |
---|
158 | _set_task_slot( x, y, p, ltid, CTX_RA_ID, ctx_ra ); |
---|
159 | |
---|
160 | // compute ctx_epc |
---|
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 ); |
---|
163 | |
---|
164 | #if GIET_DEBUG_INIT |
---|
165 | _printf("\n[GIET DEBUG INIT] Processor[%d,%d,%d] updates context for task %d\n" |
---|
166 | " - ctx_epc = %x\n" |
---|
167 | " - ctx_ra = %x\n", |
---|
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 ) ); |
---|
171 | #endif |
---|
172 | |
---|
173 | } // end for tasks |
---|
174 | |
---|
175 | // step 4 : compute and set XCU masks |
---|
176 | |
---|
177 | unsigned int isr_switch_index = 0xFFFFFFFF; |
---|
178 | unsigned int hwi_mask = 0; |
---|
179 | unsigned int pti_mask = 0; |
---|
180 | unsigned int wti_mask = 0; |
---|
181 | unsigned int irq_id; // IN_IRQ index |
---|
182 | unsigned int entry; // interrupt vector entry |
---|
183 | |
---|
184 | for (irq_id = 0; irq_id < 32; irq_id++) |
---|
185 | { |
---|
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; |
---|
189 | |
---|
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; |
---|
197 | } |
---|
198 | |
---|
199 | #if GIET_DEBUG_INIT |
---|
200 | _printf("\n[GIET DEBUG INIT] Processor[%d,%d,%d] sets XCU masks\n" |
---|
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 ); |
---|
205 | #endif |
---|
206 | |
---|
207 | unsigned int channel = p * IRQ_PER_PROCESSOR; |
---|
208 | |
---|
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 ); |
---|
212 | |
---|
213 | // step 5 : start TICK timer if at least one task |
---|
214 | if (tasks > 0) |
---|
215 | { |
---|
216 | // one ISR_TICK must be defined for each proc |
---|
217 | if (isr_switch_index == 0xFFFFFFFF) |
---|
218 | { |
---|
219 | _printf("\n[GIET ERROR] ISR_TICK not found for processor[%d,%d,%d]\n", |
---|
220 | x, y, p ); |
---|
221 | _exit(); |
---|
222 | } |
---|
223 | |
---|
224 | // start system timer |
---|
225 | _xcu_timer_start( cluster_xy, isr_switch_index, GIET_TICK_VALUE ); |
---|
226 | |
---|
227 | } |
---|
228 | |
---|
229 | #if GIET_DEBUG_INIT |
---|
230 | _printf("\n[GIET DEBUG INIT] Processor[%d,%d,%d] starts TICK timer\n", |
---|
231 | x, y, p ); |
---|
232 | #endif |
---|
233 | |
---|
234 | // step 6 : each processor updates the idle_task context: |
---|
235 | // (CTX_SP, CTX_RA, CTX_EPC). |
---|
236 | // The 4 Kbytes idle stack is implemented in the scheduler. |
---|
237 | // The PTPR register, the CTX_PTPR and CTX_PTAB slots |
---|
238 | // have been initialised in boot code. |
---|
239 | |
---|
240 | unsigned int pstack = ((unsigned int)psched) + 0x2000; |
---|
241 | |
---|
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); |
---|
245 | |
---|
246 | #if GIET_DEBUG_INIT |
---|
247 | _printf("\n[GIET DEBUG INIT] Processor[%d,%d,%d] initializes IDLE task\n" |
---|
248 | " - stack_base = %x\n" |
---|
249 | " - stack_size = 0x1000\n", |
---|
250 | x, y, p, pstack - 0x1000 ); |
---|
251 | #endif |
---|
252 | |
---|
253 | // step 7 : when all processors reach the synchronisation barrier, |
---|
254 | // each processor set registers SP, SR, PTPR, EPC, |
---|
255 | // with the values corresponding to the first allocated task, |
---|
256 | // or to the idle_task if there is no task allocated, |
---|
257 | // and jump to user code |
---|
258 | |
---|
259 | if (tasks == 0) |
---|
260 | { |
---|
261 | ltid = IDLE_TASK_INDEX; |
---|
262 | |
---|
263 | _printf("\n[GIET WARNING] No task allocated to processor[%d,%d,%d]\n", |
---|
264 | x, y, p ); |
---|
265 | } |
---|
266 | else |
---|
267 | { |
---|
268 | ltid = 0; |
---|
269 | } |
---|
270 | |
---|
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); |
---|
275 | |
---|
276 | #if GIET_DEBUG_INIT |
---|
277 | _printf("\n[GIET DEBUG INIT] Processor[%d,%d,%d] reach barrier at cycle %d\n", |
---|
278 | x, y, p, _get_proctime() ); |
---|
279 | #endif |
---|
280 | |
---|
281 | // increment barrier counter |
---|
282 | kernel_init_barrier++; |
---|
283 | |
---|
284 | // busy waiting until all processors synchronized |
---|
285 | while ( kernel_init_barrier != NB_TOTAL_PROCS ); |
---|
286 | |
---|
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", |
---|
293 | x, y, p, _get_proctime(), |
---|
294 | sp_value, sr_value, ptpr_value, epc_value ); |
---|
295 | #endif |
---|
296 | |
---|
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) |
---|
306 | : "$29", "memory" ); |
---|
307 | |
---|
308 | } // end kernel_init() |
---|
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 | |
---|