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 <tty0.h> |
---|
14 | #include <fat32.h> |
---|
15 | #include <xcu_driver.h> |
---|
16 | #include <ioc_driver.h> |
---|
17 | #include <ctx_handler.h> |
---|
18 | #include <irq_handler.h> |
---|
19 | #include <mapping_info.h> |
---|
20 | #include <mips32_registers.h> |
---|
21 | |
---|
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) |
---|
51 | # error: You must define IDLE_TASK_INDEX in the ctx_handler.h file |
---|
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 | |
---|
62 | /////////////////////////////////////////////////////////////////////////////////// |
---|
63 | // FAT internal representation for kernel code |
---|
64 | /////////////////////////////////////////////////////////////////////////////////// |
---|
65 | |
---|
66 | __attribute__((section (".kdata"))) |
---|
67 | fat32_fs_t fat __attribute__((aligned(512))); |
---|
68 | |
---|
69 | /////////////////////////////////////////////////////////////////////////////////// |
---|
70 | // array of pointers on the page tables (virtual addresses) |
---|
71 | /////////////////////////////////////////////////////////////////////////////////// |
---|
72 | |
---|
73 | __attribute__((section (".kdata"))) |
---|
74 | volatile unsigned int _ptabs_vaddr[GIET_NB_VSPACE_MAX]; // virtual addresses |
---|
75 | |
---|
76 | __attribute__((section (".kdata"))) |
---|
77 | volatile unsigned int _ptabs_ptprs[GIET_NB_VSPACE_MAX]; // physical addresses >> 13 |
---|
78 | |
---|
79 | /////////////////////////////////////////////////////////////////////////////////// |
---|
80 | // array of pointers on the schedulers (physical addresses) |
---|
81 | /////////////////////////////////////////////////////////////////////////////////// |
---|
82 | |
---|
83 | __attribute__((section (".kdata"))) |
---|
84 | volatile static_scheduler_t* _schedulers[X_SIZE][Y_SIZE][NB_PROCS_MAX]; |
---|
85 | |
---|
86 | //////////////////////////////////////////////////////////////////////////////////// |
---|
87 | // Synchonisation barrier before jumping to user code |
---|
88 | //////////////////////////////////////////////////////////////////////////////////// |
---|
89 | |
---|
90 | __attribute__((section (".kdata"))) |
---|
91 | volatile unsigned int kernel_init_barrier = 0; |
---|
92 | |
---|
93 | /////////////////////////////////////////////////////////////////////////////////// |
---|
94 | __attribute__((section (".kinit"))) void kernel_init() |
---|
95 | { |
---|
96 | // gpid : hardware processor index (fixed format: X_WIDTH|Y_WIDTH|P_WIDTH) |
---|
97 | // p : local processor id in a cluster ( p < NB_PROCS_MAX) |
---|
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); |
---|
103 | unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1); |
---|
104 | unsigned int p = gpid & ((1<<P_WIDTH)-1); |
---|
105 | unsigned int cpid = ((( x * Y_SIZE) + y) * NB_PROCS_MAX) + p; |
---|
106 | |
---|
107 | |
---|
108 | // This initialisation is done sequencially by each processor |
---|
109 | while( cpid != kernel_init_barrier ) asm volatile ( "nop" ); |
---|
110 | |
---|
111 | // Step 0 : Processor[0,0,0] initialises the kernel FAT |
---|
112 | if ( gpid == 0 ) _fat_init( IOC_BOOT_MODE ); |
---|
113 | |
---|
114 | // Step 1 : each processor get its scheduler virtual address from CP0_SCHED register |
---|
115 | // and contributes to _schedulers[] array initialisation |
---|
116 | |
---|
117 | static_scheduler_t* psched = (static_scheduler_t*)_get_sched(); |
---|
118 | unsigned int tasks = psched->tasks; |
---|
119 | |
---|
120 | _schedulers[x][y][p] = psched; |
---|
121 | |
---|
122 | #if GIET_DEBUG_INIT |
---|
123 | _printf("\n[GIET DEBUG INIT] Processor[%d,%d,%d] starts kernel init\n" |
---|
124 | " - scheduler vbase = %x\n" |
---|
125 | " - tasks = %d\n", |
---|
126 | x, y, p, (unsigned int)psched, tasks ); |
---|
127 | #endif |
---|
128 | |
---|
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 |
---|
135 | // location containing this entry point. |
---|
136 | |
---|
137 | unsigned int ltid; |
---|
138 | |
---|
139 | for (ltid = 0; ltid < tasks; ltid++) |
---|
140 | { |
---|
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 ); |
---|
144 | |
---|
145 | // initialize PTABS arrays |
---|
146 | _ptabs_vaddr[vsid] = ptab; |
---|
147 | _ptabs_ptprs[vsid] = ptpr; |
---|
148 | |
---|
149 | #if GIET_DEBUG_INIT |
---|
150 | _printf("\n[GIET DEBUG INIT] Processor[%d,%d,%d] contributes to PTABS arrays\n" |
---|
151 | " - ptabs_vaddr[%d] = %x / ptpr_paddr[%d] = %l\n", |
---|
152 | x, y, p, |
---|
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 |
---|
161 | unsigned int ctx_ra = (unsigned int)(&_ctx_eret); |
---|
162 | _set_task_slot( x, y, p, ltid, CTX_RA_ID, ctx_ra ); |
---|
163 | |
---|
164 | // compute ctx_epc |
---|
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 ); |
---|
167 | |
---|
168 | #if GIET_DEBUG_INIT |
---|
169 | _printf("\n[GIET DEBUG INIT] Processor[%d,%d,%d] updates context for task %d\n" |
---|
170 | " - ctx_epc = %x\n" |
---|
171 | " - ctx_ra = %x\n", |
---|
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 ) ); |
---|
175 | #endif |
---|
176 | |
---|
177 | } // end for tasks |
---|
178 | |
---|
179 | // step 3 : compute and set XCU masks |
---|
180 | |
---|
181 | unsigned int isr_switch_index = 0xFFFFFFFF; |
---|
182 | unsigned int hwi_mask = 0; |
---|
183 | unsigned int pti_mask = 0; |
---|
184 | unsigned int wti_mask = 0; |
---|
185 | unsigned int irq_id; // IN_IRQ index |
---|
186 | unsigned int entry; // interrupt vector entry |
---|
187 | |
---|
188 | for (irq_id = 0; irq_id < 32; irq_id++) |
---|
189 | { |
---|
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; |
---|
193 | |
---|
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; |
---|
201 | } |
---|
202 | |
---|
203 | #if GIET_DEBUG_INIT |
---|
204 | _printf("\n[GIET DEBUG INIT] Processor[%d,%d,%d] sets XCU masks\n" |
---|
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 ); |
---|
209 | #endif |
---|
210 | |
---|
211 | unsigned int channel = p * IRQ_PER_PROCESSOR; |
---|
212 | |
---|
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 ); |
---|
216 | |
---|
217 | // step 4 : start TICK timer if at least one task |
---|
218 | if (tasks > 0) |
---|
219 | { |
---|
220 | // one ISR_TICK must be defined for each proc |
---|
221 | if (isr_switch_index == 0xFFFFFFFF) |
---|
222 | { |
---|
223 | _printf("\n[GIET ERROR] ISR_TICK not found for processor[%d,%d,%d]\n", |
---|
224 | x, y, p ); |
---|
225 | _exit(); |
---|
226 | } |
---|
227 | |
---|
228 | // start system timer |
---|
229 | _xcu_timer_start( cluster_xy, isr_switch_index, GIET_TICK_VALUE ); |
---|
230 | |
---|
231 | } |
---|
232 | |
---|
233 | #if GIET_DEBUG_INIT |
---|
234 | _printf("\n[GIET DEBUG INIT] Processor[%d,%d,%d] starts TICK timer\n", |
---|
235 | x, y, p ); |
---|
236 | #endif |
---|
237 | |
---|
238 | // step 5 : each processor updates the idle_task context: |
---|
239 | // (CTX_SP, CTX_RA, CTX_EPC). |
---|
240 | // The 4 Kbytes idle stack is implemented in the scheduler. |
---|
241 | // The PTPR register, the CTX_PTPR and CTX_PTAB slots |
---|
242 | // have been initialised in boot code. |
---|
243 | |
---|
244 | unsigned int pstack = ((unsigned int)psched) + 0x2000; |
---|
245 | |
---|
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); |
---|
249 | |
---|
250 | #if GIET_DEBUG_INIT |
---|
251 | _printf("\n[GIET DEBUG INIT] Processor[%d,%d,%d] initializes IDLE task\n" |
---|
252 | " - stack_base = %x\n" |
---|
253 | " - stack_size = 0x1000\n", |
---|
254 | x, y, p, pstack - 0x1000 ); |
---|
255 | #endif |
---|
256 | |
---|
257 | // step 6 : when all processors reach the synchronisation barrier, |
---|
258 | // each processor set registers SP, SR, PTPR, EPC, |
---|
259 | // with the values corresponding to the first allocated task, |
---|
260 | // or to the idle_task if there is no task allocated, |
---|
261 | // and jump to user code |
---|
262 | |
---|
263 | if (tasks == 0) |
---|
264 | { |
---|
265 | ltid = IDLE_TASK_INDEX; |
---|
266 | |
---|
267 | _printf("\n[GIET WARNING] No task allocated to processor[%d,%d,%d]\n", |
---|
268 | x, y, p ); |
---|
269 | } |
---|
270 | else |
---|
271 | { |
---|
272 | ltid = 0; |
---|
273 | } |
---|
274 | |
---|
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); |
---|
279 | |
---|
280 | #if GIET_DEBUG_INIT |
---|
281 | _printf("\n[GIET DEBUG INIT] Processor[%d,%d,%d] reach barrier at cycle %d\n", |
---|
282 | x, y, p, _get_proctime() ); |
---|
283 | #endif |
---|
284 | |
---|
285 | // increment barrier counter |
---|
286 | kernel_init_barrier++; |
---|
287 | |
---|
288 | // busy waiting until all processors synchronized |
---|
289 | while ( kernel_init_barrier != NB_TOTAL_PROCS ); |
---|
290 | |
---|
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", |
---|
297 | x, y, p, _get_proctime(), |
---|
298 | sp_value, sr_value, ptpr_value, epc_value ); |
---|
299 | #endif |
---|
300 | |
---|
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) |
---|
310 | : "$29", "memory" ); |
---|
311 | |
---|
312 | } // end kernel_init() |
---|
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 | |
---|