source: soft/giet_vm/giet_kernel/kernel_init.c @ 259

Last change on this file since 259 was 258, checked in by alain, 11 years ago

This is a major release, including a deep restructuration of code.
The main evolutions are

  • use of the Tsar preloader to load the GIET boot-loader from disk
  • introduction of a FAT32 file system library,
  • use of this fat32 library by the boot-loader to load the map.bin data structure, and the various .elf files
  • reorganisation of drivers (one file per peripheral).
  • introduction of drivers for new peripherals: vci_chbuf_dma and vci_multi_ahci.
  • introduction of a new physical memory allocator in the boot code.

This release has been tested on the tsar_generic_iob architecture,
for the two following mappings: 4c_1p_iob_four.xml and 4c_1p_iob_sort.xml

  • Property svn:executable set to *
File size: 13.9 KB
Line 
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 file is part of the GIET-VM nano-kernel.
8//
9// This nano-kernel has been written for the MIPS32 processor.
10// The virtual adresses are on 32 bits and use the (unsigned int) type, but the
11// physicals addresses can have up to 40 bits, and use the  (unsigned long long) type.
12// It natively supports clusterised shared mmemory multi-processors architectures,
13// where each processor is identified by a composite index (cluster_id, local_id),
14// and where there is one physical memory bank per cluster.
15//
16// This file contains the _kernel_init() function, that performs the second
17// phase of system initialisation.  The three significant actions are:
18// 1) processor 0 makes peripherals and system FAT initialisation.
19// 2) processor 0 awake all other processors by an IPI.
20// 3) all processors running in parallel perform register initialisation,
21//    from their private scheduler, and jump to user code.
22////////////////////////////////////////////////////////////////////////////////////
23
24#include <giet_config.h>
25
26// kernel libraries
27#include <utils.h>
28#include <fat32.h>
29
30//for peripheral initialisation
31#include <dma_driver.h>
32#include <fbf_driver.h>
33#include <tty_driver.h>
34#include <icu_driver.h>
35#include <xcu_driver.h>
36#include <ioc_driver.h>
37#include <mmc_driver.h>
38#include <mwr_driver.h>
39#include <nic_driver.h>
40#include <tim_driver.h>
41
42#include <ctx_handler.h>
43#include <irq_handler.h>
44
45#include <mapping_info.h>
46#include <mips32_registers.h>
47
48///////////////////////////////////////////////////////////////////////////////////
49// array of pointers on the page tables (virtual addresses)
50///////////////////////////////////////////////////////////////////////////////////
51
52__attribute__((section (".kdata"))) 
53unsigned int _ptabs_vaddr[GIET_NB_VSPACE_MAX];    // virtual addresses
54
55__attribute__((section (".kdata")))       
56unsigned int _ptabs_ptprs[GIET_NB_VSPACE_MAX];    // physical addresses >> 13
57
58///////////////////////////////////////////////////////////////////////////////////
59// array of pointers on the schedulers (physical addresses)
60///////////////////////////////////////////////////////////////////////////////////
61
62__attribute__((section (".kdata"))) 
63static_scheduler_t* _schedulers[NB_CLUSTERS * NB_PROCS_MAX];   // virtual addresses
64
65////////////////////////////////////////////////////////////////////////////////////
66// staks for the "idle" tasks (256 bytes for each processor)
67////////////////////////////////////////////////////////////////////////////////////
68
69__attribute__((section (".kdata"))) 
70unsigned int _idle_stack[NB_CLUSTERS * NB_PROCS_MAX * 128]; 
71
72////////////////////////////////////////////////////////////////////////////////////
73// This function is the entry point in kernel for all processors.
74// It is executed in parallel by all procesors, and completes the system
75// initialisation that has been started by processor 0 in the boot_init() function.
76//
77// This kernel code makes the following assuptions, regarding the work bone
78// by the boot code:
79//
80// 1) The page tables associated to the various vspaces have been build
81//    in physical memory, and can be used by the kernel code.
82//
83// 2) All schedulers (this include all task contexts) have been initialised,
84//    Both the virtual and the physical base addresses of the page tables
85//    are available in the CTX_PTAB and CTX_PTPR slots.
86//
87// 3) The CP0_SCHED register of each processor contains a pointer on its
88//    private scheduler (virtual address).
89//
90// 4) The CP2_PTPR register of each processor contains a pointer on
91//    the vspace_0 page table (physical address>>13).
92//
93// 5) For all processors, the MMU is activated (CP2_MODE contains 0xF).
94//
95// This code must be loaded in .kinit section, in order to control seg_kinit_base,
96// as this address is used by the boot code to jump into kernel code.
97////////////////////////////////////////////////////////////////////////////////////
98// Each processor performs the following actions:
99// 1/ contribute to _schedulers_paddr[] array initialisation.
100// 2/ contribute to _ptabs_paddr[] and _ptabs_vaddr arrays initialisation
101// 3/ compute and set the ICU mask for its private ICU channel
102// 4/ initialise its private TICK timer (if tasks > 0)
103// 5/ initialise the "idle" task context in its private scheduler
104// 6/ initialise the SP, SR, PTPR, EPC registers
105// 7/ jump to the user code with an eret.
106////////////////////////////////////////////////////////////////////////////////////
107__attribute__((section (".kinit"))) void kernel_parallel_init() 
108{
109    unsigned int global_pid = _get_procid();
110
111#if 0
112// Debug feature : we can kill all processors but one
113if ( global_pid != 1 )
114{
115    _tty_get_lock( 0 );
116    _puts("\n[GIET] Processor ");
117    _putd( global_pid );
118    _puts(" suicide...\n");
119    _tty_release_lock( 0 );
120    _exit();
121}
122#endif
123
124    // Step 1 : each processor get its scheduler virtual address
125    //          and contribute to initialise the _schedulers[] array
126
127    unsigned int        cluster_id = global_pid / NB_PROCS_MAX;
128    unsigned int        proc_id    = global_pid % NB_PROCS_MAX;
129    static_scheduler_t* psched     = (static_scheduler_t*)_get_sched();
130    unsigned int        tasks      = psched->tasks;
131
132    _schedulers[global_pid] = psched;
133
134#if GIET_DEBUG_INIT
135_tty_get_lock( 0 );
136_puts("\n[GIET DEBUG] Parallel init : step 1 for processor ");
137_putd(global_pid);
138_puts("\n - scheduler vbase = ");
139_putx((unsigned int) psched);
140_puts("\n - tasks           = ");
141_putd(tasks);
142_puts("\n");
143_tty_release_lock( 0 );
144#endif
145
146    // step 2 : each processor that is allocated at least one task
147    //          completes its private scheduler initialisation, and
148    //          contribute to _ptabs_vaddr[] and _ptabs_ptprs[] arrays initialisation.
149    //          - set the CTX_RA slot vith the virtual address
150    //            of the _ctx_eret() function (for context switch).
151    //          - set the CTX_EPC slot that must contain the task
152    //            entry point, and contain only the address of the
153    //            memory location containing this entry point.
154
155    unsigned int ltid;
156
157    // loop on all allocated tasks
158    for (ltid = 0; ltid < tasks; ltid++) 
159    {
160        unsigned int vsid = _get_task_slot( global_pid, ltid , CTX_VSID_ID ); 
161        unsigned int ptab = _get_task_slot( global_pid, ltid , CTX_PTAB_ID ); 
162        unsigned int ptpr = _get_task_slot( global_pid, ltid , CTX_PTPR_ID ); 
163
164        _ptabs_vaddr[vsid] = ptab;
165        _ptabs_ptprs[vsid] = ptpr;
166
167        unsigned int ctx_ra = (unsigned int)(&_ctx_eret);
168        _set_task_slot( global_pid, ltid, CTX_RA_ID, ctx_ra );
169
170        unsigned int* ptr = (unsigned int*)_get_task_slot( global_pid, ltid, CTX_EPC_ID );
171        _set_task_slot( global_pid, ltid, CTX_EPC_ID, *ptr );
172
173#if GIET_DEBUG_INIT
174_tty_get_lock( 0 );
175_puts("\n[GIET DEBUG] Parallel init : step 2 for processor ");
176_putd( global_pid );
177_puts(" / task ");
178_putd( ltid );
179_puts("\n - ctx_vsid  = ");
180_putd( _get_task_slot( global_pid, ltid, CTX_VSID_ID ) );
181_puts("\n - ctx_ptpr  = ");
182_putx( _get_task_slot( global_pid, ltid, CTX_PTPR_ID ) );
183_puts("\n - ctx_ptab  = ");
184_putx( _get_task_slot( global_pid, ltid, CTX_PTAB_ID ) );
185_puts("\n - ctx_ltid  = ");
186_putd( _get_task_slot( global_pid, ltid, CTX_LTID_ID ) );
187_puts("\n - ctx_epc   = ");
188_putx( _get_task_slot( global_pid, ltid, CTX_EPC_ID ) );
189_puts("\n - ctx_ra    = ");
190_putx( _get_task_slot( global_pid, ltid, CTX_RA_ID ) );
191_puts("\n - ctx_gtid  = ");
192_putd( _get_task_slot( global_pid, ltid, CTX_GTID_ID ) );
193_puts("\n - ctx_tty   = ");
194_putd( _get_task_slot( global_pid, ltid, CTX_TTY_ID ) );
195_puts("\n");
196_tty_release_lock( 0 );
197#endif
198
199    }
200
201    // step 3 : compute and set ICU or XICU masks
202    //          there is at most 32 interrupts per processor
203
204    unsigned int isr_switch_channel = 0xFFFFFFFF;
205    unsigned int irq_id;            // IN_IRQ index
206    unsigned int hwi_mask = 0;
207    unsigned int swi_mask = 0;
208    unsigned int pti_mask = 0;
209
210    for (irq_id = 0; irq_id < 32; irq_id++) 
211    {
212        unsigned int entry = psched->interrupt_vector[irq_id];
213        unsigned int isr   = (entry & 0x000000FF);
214        unsigned int type  = (entry & 0x0000FF00) >> 8;
215        unsigned int valid = (entry & 0x80000000);
216
217        if      ((type == IRQ_TYPE_HWI) && valid ) hwi_mask = hwi_mask | (1<<irq_id);
218        else if ((type == IRQ_TYPE_SWI) && valid ) swi_mask = swi_mask | (1<<irq_id);
219        else if ((type == IRQ_TYPE_PTI) && valid ) pti_mask = pti_mask | (1<<irq_id);
220        else if ( valid )
221        {
222            _puts("\n[GIET ERROR] _kernel_parallel_start() : illegal IRQ type\n");
223            _puts(" irq_id = ");
224            _putx( irq_id );
225            _puts(" / entry = ");
226            _putx( entry );
227            _puts("\n");
228            _exit();
229        }
230        if (isr == ISR_SWITCH) isr_switch_channel = irq_id;
231    }
232
233#if GIET_DEBUG_INIT
234_tty_get_lock( 0 );
235_puts("\n[GIET DEBUG] Parallel init : step 3 for processor ");
236_putd(global_pid);
237_puts("\n - ICU HWI_MASK = ");
238_putx(hwi_mask);
239_puts("\n - ICU SWI_MASK = ");
240_putx(swi_mask);
241_puts("\n - ICU PTI_MASK = ");
242_putx(pti_mask);
243_puts("\n");
244_tty_release_lock( 0 );
245#endif
246
247    // GIET-VM consraint : only one IRQ type per irq_id
248    if ( hwi_mask & swi_mask & pti_mask )
249    {
250        _puts("[GIET ERROR] _kernel_parallel_start : conflicting IRQs\n");
251        _exit();
252    }
253
254#if USE_XICU
255    _xcu_set_mask(cluster_id, proc_id, hwi_mask, IRQ_TYPE_HWI); // set HWI_MASK
256    _xcu_set_mask(cluster_id, proc_id, swi_mask, IRQ_TYPE_SWI); // set SWI_MASK
257    _xcu_set_mask(cluster_id, proc_id, pti_mask, IRQ_TYPE_PTI); // set PTI_MASK
258#else
259    _icu_set_mask(cluster_id, proc_id, (hwi_mask | pti_mask | swi_mask) );   
260#endif
261
262    // step 4 : start TICK timer if at least one task
263    if (tasks > 0) 
264    {
265        // one ISR_SWITCH must be defined for each proc
266        if (isr_switch_channel == 0xFFFFFFFF) 
267        {
268            _tty_get_lock( 0 );
269            _puts("\n[GIET ERROR] ISR_SWITCH not found on proc ");
270            _putd(proc_id);
271            _puts("\n");
272            _tty_release_lock( 0 );
273            _exit();
274        }
275
276        // start system timer
277        unsigned int ko;
278#if USE_XICU
279        ko = _xcu_timer_start( cluster_id, isr_switch_channel, GIET_TICK_VALUE ); 
280#else
281        ko = _timer_start( cluster_id, isr_switch_channel, GIET_TICK_VALUE ); 
282#endif
283        if ( ko )
284        {
285            _tty_get_lock( 0 );
286            _puts("\n[GIET ERROR] ISR_SWITCH start error for processor ");
287            _putd(proc_id);
288            _puts("\n");
289            _tty_release_lock( 0 );
290            _exit();
291        }
292    } 
293
294#if GIET_DEBUG_INIT
295_tty_get_lock( 0 );
296_puts("\n[GIET DEBUG] Parallel init : step 4 for processor ");
297_putd(global_pid);
298if ( tasks > 1 ) _puts("\n  context switch activated\n");
299else             _puts("\n  context switch  not activated\n");
300_tty_release_lock( 0 );
301#endif
302
303    // step 5 : each processor updates the idle_task context:
304    //          (only CTX_SP, CTX_RA, CTX_EPC).
305    //          The stack size is 256 bytes, reserved in seg_kdata.
306    //          The PTPR register, the CTX_PTPR and CTX_PTAB slots
307    //          have been initialised in boot code.
308
309    unsigned int stack = (unsigned int)_idle_stack + ((global_pid + 1)<<9);
310
311    _set_task_slot( global_pid, IDLE_TASK_INDEX, CTX_SP_ID,  stack);
312    _set_task_slot( global_pid, IDLE_TASK_INDEX, CTX_RA_ID,  (unsigned int) &_ctx_eret);
313    _set_task_slot( global_pid, IDLE_TASK_INDEX, CTX_EPC_ID, (unsigned int) &_idle_task);
314
315#if GIET_DEBUG_INIT
316_tty_get_lock( 0 );
317_puts("\n[GIET DEBUG] Parallel init : step 5 for processor ");
318_putd(global_pid);
319_puts("\n  idle task context set\n");
320_tty_release_lock( 0 );
321#endif
322
323    // step 6 : each processor initialises SP, SR, PTPR, EPC, registers
324    //          with the values corresponding to the first allocated task,
325    //          or to the idle_task if there is no task allocated.
326
327    ltid = 0;
328
329    if (tasks == 0) 
330    {
331        ltid = IDLE_TASK_INDEX;
332
333        _tty_get_lock( 0 );
334        _puts("\n[GIET WARNING] No task allocated to processor ");
335        _putd(global_pid);
336        _puts(" => idle\n");
337        _tty_release_lock ( 0 );
338    }
339
340    unsigned int sp_value   = _get_task_slot(global_pid, ltid, CTX_SP_ID);
341    unsigned int sr_value   = _get_task_slot(global_pid, ltid, CTX_SR_ID);
342    unsigned int ptpr_value = _get_task_slot(global_pid, ltid, CTX_PTPR_ID);
343    unsigned int epc_value  = _get_task_slot(global_pid, ltid, CTX_EPC_ID);
344
345#if GIET_DEBUG_INIT
346_tty_get_lock( 0 );
347_puts("\n[GIET DEBUG] Parallel init : step 6 for processor ");
348_putd(global_pid);
349_puts("\n - sp   = ");
350_putx(sp_value);
351_puts("\n - sr   = ");
352_putx(sr_value);
353_puts("\n - ptpr = ");
354_putx(ptpr_value);
355_puts("\n - epc  = ");
356_putx(epc_value);
357_puts("\n");
358_tty_release_lock( 0 );
359#endif
360
361_tty_get_lock( 0 );
362_puts("\n[GIET] Processor ");
363_putd( global_pid );
364_puts(" completes kernel init at cycle ");
365_putd( _get_proctime() );
366_puts(" / task_entry_point = ");
367_putx( epc_value );
368_puts("\n");
369_tty_release_lock( 0 );
370
371    // Step 7 : set  registers and jump to user code
372    asm volatile (
373            "move    $29,       %0    \n"        /* SP <= ctx[CTX_SP_ID] */
374            "mtc0    %1,        $12   \n"        /* SR <= ctx[CTX_SR_ID] */
375            "mtc2    %2,        $0    \n"        /* PTPR <= ctx[CTX_PTPR_ID] */
376            "mtc0    %3,        $14   \n"        /* EPC <= ctx[CTX_EPC_ID] */
377            "eret                     \n"        /* jump to user code */
378            "nop                      \n"
379            :
380            : "r" (sp_value), "r" (sr_value), "r" (ptpr_value), "r" (epc_value));
381
382} // end kernel_parallel_init()
383
384
385// Local Variables:
386// tab-width: 4
387// c-basic-offset: 4
388// c-file-offsets:((innamespace . 0)(inline-open . 0))
389// indent-tabs-mode: nil
390// End:
391// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
392
Note: See TracBrowser for help on using the repository browser.