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

Last change on this file since 592 was 592, checked in by alain, 9 years ago

Introduce support for the new POSIX-like FAT32.

  • Property svn:executable set to *
File size: 14.1 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// 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 <vmem.h>
14#include <tty0.h>
15#include <kernel_malloc.h>
16#include <kernel_locks.h>
17#include <kernel_barriers.h>
18#include <fat32.h>
19#include <xcu_driver.h>
20#include <nic_driver.h>
21#include <hba_driver.h>
22#include <sdc_driver.h>
23#include <bdv_driver.h>
24#include <mmc_driver.h>
25#include <ctx_handler.h>
26#include <irq_handler.h>
27#include <mapping_info.h>
28#include <mips32_registers.h>
29
30#if !defined(X_SIZE)
31# error: You must define X_SIZE in the hard_config.h file
32#endif
33
34#if !defined(Y_SIZE)
35# error: You must define Y_SIZE in the hard_config.h file
36#endif
37
38#if !defined(Y_WIDTH)
39# error: You must define Y_WIDTH in the hard_config.h file
40#endif
41
42#if !defined(Y_WIDTH)
43# error: You must define Y_WIDTH in the hard_config.h file
44#endif
45
46#if !defined(NB_PROCS_MAX)
47# error: You must define NB_PROCS_MAX in the hard_config.h file
48#endif
49
50#if !defined(NB_TOTAL_PROCS)
51# error: You must define NB_TOTAL_PROCS in the hard_config.h file
52#endif
53
54#if !defined(USE_XCU)
55# error: You must define USE_XCU in the hard_config.h file
56#endif
57
58#if !defined(USE_PIC)
59# error: You must define USE_PIC in the hard_config.h file
60#endif
61
62#if !defined(IDLE_TASK_INDEX)
63# error: You must define IDLE_TASK_INDEX in the ctx_handler.h file
64#endif
65
66#if !defined(GIET_TICK_VALUE)
67# error: You must define GIET_TICK_VALUE in the giet_config.h file
68#endif
69
70#if !defined(GIET_NB_VSPACE_MAX)
71# error: You must define GIET_NB_VSPACE_MAX in the giet_config.h file
72#endif
73
74#if !defined(NB_TTY_CHANNELS)
75# error: You must define NB_TTY_CHANNELS in the hard_config.h file
76#endif
77
78#if (NB_TTY_CHANNELS < 1)
79# error: NB_TTY_CHANNELS cannot be smaller than 1
80#endif
81
82#if !defined(GIET_ISR_TYPE_MAX)
83# error: You must define GIET_ISR_TYPE_MAX in the giet_config.h file
84#endif
85
86#if !defined(GIET_ISR_CHANNEL_MAX)
87# error: You must define GIET_ISR_CHANNEL_MAX in the giet_config.h file
88#endif
89
90
91////////////////////////////////////////////////////////////////////////////////
92//       Global variables
93////////////////////////////////////////////////////////////////////////////////
94
95// array of page tables virtual addresses
96__attribute__((section(".kdata")))
97volatile unsigned int _ptabs_vaddr[GIET_NB_VSPACE_MAX][X_SIZE][Y_SIZE]; 
98
99// array of page tables PTPR values (physical addresses >> 13)
100__attribute__((section(".kdata")))
101volatile unsigned int _ptabs_ptprs[GIET_NB_VSPACE_MAX][X_SIZE][Y_SIZE]; 
102
103// Array of pointers on the schedulers
104__attribute__((section(".kdata")))
105volatile static_scheduler_t* _schedulers[X_SIZE][Y_SIZE][NB_PROCS_MAX]; 
106
107// Synchonisation before entering parallel execution
108__attribute__((section(".kdata")))
109volatile unsigned int _kernel_init_done = 0;
110
111// Kernel uses sqt_lock to protect TTY0       
112__attribute__((section(".kdata")))
113unsigned int   _tty0_boot_mode = 0;
114
115// Kernel uses sqt_lock to protect command allocator in HBA       
116__attribute__((section(".kdata")))
117unsigned int   _hba_boot_mode = 0;
118
119// synchronisation barrier for parallel init by all processors     
120__attribute__((section(".kdata")))
121sqt_barrier_t  _all_procs_barrier  __attribute__((aligned(64)));
122
123////////////////////////////////////////////////////////////////////////////////
124//      Extern variables
125////////////////////////////////////////////////////////////////////////////////
126
127// this variable is defined in tty0.c file
128extern sqt_lock_t _tty0_sqt_lock;
129
130////////////////////////////////////////////////////////////////////////////////
131// This kernel_init() function completes the kernel initialisation in 6 steps:
132// Step 0 is done by processor[0,0,0]. Steps 1 to 4 are executed in parallel
133// by all processors.
134// - step 0 : P[0,0,0] Initialise various global variables.
135// - step 1 : Each processor initialises scheduler pointers array.
136// - step 2 : Each processor initialises PTAB pointers arrays.
137// - step 3 : Each processor initialise idle task and starts TICK timer.
138// - step 4 : Each processor set sp, sr, ptpr, epc registers values.
139////////////////////////////////////////////////////////////////////////////////
140__attribute__((section (".kinit"))) void kernel_init() 
141{
142    // gpid  : hardware processor index (fixed format: X_WIDTH|Y_WIDTH|P_WIDTH)
143    // x,y,p : proc coordinates ( x < X_SIZE / y < Y_SIZE / p < NB_PROCS_MAX )
144
145    unsigned int gpid       = _get_procid();
146    unsigned int cluster_xy = gpid >> P_WIDTH;
147    unsigned int x          = cluster_xy >> Y_WIDTH & ((1<<X_WIDTH)-1);
148    unsigned int y          = cluster_xy & ((1<<Y_WIDTH)-1);
149    unsigned int p          = gpid & ((1<<P_WIDTH)-1);
150    unsigned int unused;
151   
152    ////////////////////////////////////////////////////////////////////////////
153    // Step 0 : P[0,0,0] initialises global variables and peripherals
154   ////////////////////////////////////////////////////////////////////////////
155
156    if ( gpid == 0 )
157    {
158        //////  distributed kernel heap initialisation
159        _heap_init();
160       
161#if GIET_DEBUG_INIT
162_nolock_printf("\n[DEBUG KINIT] P[%d,%d,%d] completes kernel heap init\n", x, y, p );
163#endif
164        //////  distributed lock for TTY0
165        _sqt_lock_init( &_tty0_sqt_lock );
166
167#if GIET_DEBUG_INIT
168_nolock_printf("\n[DEBUG KINIT] P[%d,%d,%d] completes TTY0 lock init\n", x , y , p );
169#endif
170        //////  distributed kernel barrier between all processors
171        _sqt_barrier_init( &_all_procs_barrier );
172
173#if GIET_DEBUG_INIT
174_nolock_printf("\n[DEBUG KINIT] P[%d,%d,%d] completes barrier init\n", x , y , p );
175#endif
176
177        ////// _ext_irq_index[isr][channel] initialisation
178        if ( USE_PIC ) _ext_irq_init();
179
180#if GIET_DEBUG_INIT
181_nolock_printf("\n[DEBUG KINIT] P[%d,%d,%d] completes ext_irq init\n", x , y , p );
182#endif
183
184        //////  NIC peripheral initialization
185        if ( USE_NIC ) _nic_global_init( 1,      // broadcast accepted
186                                         1,      // bypass activated
187                                         0,      // tdm non activated
188                                         0 );    // tdm period
189#if GIET_DEBUG_INIT
190_nolock_printf("\n[DEBUG KINIT] P[%d,%d,%d] completes NIC init\n", x , y , p );
191#endif
192
193        //////  IOC peripheral initialisation
194        if ( USE_IOC_HBA )
195        {
196            _hba_init();
197            _ext_irq_alloc( ISR_HBA , 0 , &unused );
198
199#if GIET_DEBUG_INIT
200_nolock_printf("\n[DEBUG KINIT] P[%d,%d,%d] completes HBA init\n", x , y , p );
201#endif
202        }
203        if ( USE_IOC_SDC )
204        {
205            _sdc_init();
206            _ext_irq_alloc( ISR_SDC , 0 , &unused );
207
208#if GIET_DEBUG_INIT
209_nolock_printf("\n[DEBUG KINIT] P[%d,%d,%d] completes SDC init\n", x , y , p );
210#endif
211        }
212        if ( USE_IOC_BDV )
213        {
214            _bdv_init();
215            _ext_irq_alloc( ISR_BDV , 0 , &unused );
216
217#if GIET_DEBUG_INIT
218_nolock_printf("\n[DEBUG KINIT] P[%d,%d,%d] completes BDV init\n", x , y , p );
219#endif
220        }
221
222        //////  release other processors
223        _kernel_init_done = 1;
224    }
225    else 
226    {
227        while( _kernel_init_done == 0 )  asm volatile ( "nop" );
228    }
229
230    ///////////////////////////////////////////////////////////////////////////
231    // Step 1 : each processor get its scheduler vaddr from CP0_SCHED,
232    //          contributes to _schedulers[] array initialisation,
233    //          and wait completion of array initialisation.
234    ///////////////////////////////////////////////////////////////////////////
235
236    static_scheduler_t* psched     = (static_scheduler_t*)_get_sched();
237    unsigned int        tasks      = psched->tasks;
238
239    _schedulers[x][y][p] = psched;
240
241#if GIET_DEBUG_INIT
242_printf("\n[DEBUG KINIT] P[%d,%d,%d] initialises SCHED array\n"
243        " - scheduler vbase = %x\n"
244        " - tasks           = %d\n",
245        x, y, p, (unsigned int)psched, tasks );
246#endif
247
248    /////////////////////////////////////////
249    _sqt_barrier_wait( &_all_procs_barrier );   
250    /////////////////////////////////////////
251
252    ////////////////////////////////////////////////////////////////////////////
253    // step 2 : each processor that is allocated at least one task loops
254    //          on its allocated tasks:
255    //          - contributes to _ptabs_vaddr[][][] & _ptabs_ptprs[][][]
256    //            initialisation, from values stored in the tasks contexts.
257    //          - set CTX_RA slot  with the kernel _ctx_eret() virtual address.
258    //          - set CTX_EPC slot that must contain the task entry point,
259    //            and contain only at this point the virtual address of the
260    //            memory slot containing this entry point.
261    ////////////////////////////////////////////////////////////////////////////
262
263    unsigned int ltid;
264
265    for (ltid = 0; ltid < tasks; ltid++) 
266    {
267        unsigned int vsid = _get_task_slot( x, y, p, ltid , CTX_VSID_ID ); 
268        unsigned int ptab = _get_task_slot( x, y, p, ltid , CTX_PTAB_ID ); 
269        unsigned int ptpr = _get_task_slot( x, y, p, ltid , CTX_PTPR_ID ); 
270
271        // initialize PTABS arrays
272        _ptabs_vaddr[vsid][x][y] = ptab;
273        _ptabs_ptprs[vsid][x][y] = ptpr;
274
275        // set the ptpr to use the local page table
276        asm volatile( "mtc2    %0,   $0"
277                      : : "r" (ptpr) );
278
279        // compute ctx_ra
280        unsigned int ctx_ra = (unsigned int)(&_ctx_eret);
281        _set_task_slot( x, y, p, ltid, CTX_RA_ID, ctx_ra );
282
283        // compute ctx_epc
284        unsigned int* ptr = (unsigned int*)_get_task_slot(x,y,p,ltid,CTX_EPC_ID);
285        _set_task_slot(x,y,p,ltid,CTX_EPC_ID,*ptr);
286
287#if GIET_DEBUG_INIT
288_printf("\n[DEBUG KINIT] P[%d,%d,%d] initialises PTABS arrays"
289        " and context for task %d \n"
290        " - ptabs_vaddr[%d][%d][%d] = %x\n"
291        " - ptabs_paddr[%d][%d][%d] = %l\n"
292        " - ctx_epc              = %x\n"
293        " - ctx_ra               = %x\n",
294        x , y , p , ltid , 
295        vsid , x , y , ptab ,
296        vsid , x , y , ((unsigned long long)ptpr)<<13 ,
297        _get_task_slot( x, y, p, ltid, CTX_EPC_ID ),
298        _get_task_slot( x, y, p, ltid, CTX_RA_ID ) );
299#endif
300
301    }  // end for tasks
302
303    /////////////////////////////////////////
304    _sqt_barrier_wait( &_all_procs_barrier );   
305    /////////////////////////////////////////
306
307    ////////////////////////////////////////////////////////////////////////////
308    // step 3 : - Each processor complete idle task context initialisation,
309    //            (only the CTX_SP, CTX_RA, CTX_EPC slot, because the CTX_PTPR
310    //            and CTX_PTAB slots have been initialised in boot code)
311    //            The 4 Kbytes idle stack is implemented in the scheduler itself.
312    //          - Each processor starts TICK timer, as soon as at least one task
313    //            is allocated.
314    //          - P[0,0,0] initialises FAT (not done before, because it must
315    //            be done after the _ptabs_vaddr[v][x][y] array initialisation,
316    //            for V2P translation in _fat_ioc_access() function).
317    ////////////////////////////////////////////////////////////////////////////
318
319    unsigned int sp  = ((unsigned int)psched) + 0x2000;
320    unsigned int ra  = (unsigned int)(&_ctx_eret);
321    unsigned int epc = (unsigned int)(&_idle_task);
322
323    _set_task_slot( x , y , p , IDLE_TASK_INDEX , CTX_SP_ID  , sp  );
324    _set_task_slot( x , y , p , IDLE_TASK_INDEX , CTX_RA_ID  , ra  );
325    _set_task_slot( x , y , p , IDLE_TASK_INDEX , CTX_EPC_ID , epc );
326
327    if (tasks > 0) _xcu_timer_start( cluster_xy, p, GIET_TICK_VALUE ); 
328
329#if GIET_DEBUG_INIT
330_printf("\n[DEBUG KINIT] P[%d,%d,%d] initializes idle_task and starts TICK\n", 
331        x, y, p );
332#endif
333
334    if ( gpid == 0 )
335    {
336        _fat_init( 1 );   // kernel mode => Inode-Tree, Fat-Cache and File-Caches
337
338#if GIET_DEBUG_INIT
339_printf("\n[DEBUG KINIT] P[%d,%d,%d] completes kernel FAT init\n",
340        x, y, p );
341#endif
342
343    }
344
345    /////////////////////////////////////////
346    _sqt_barrier_wait( &_all_procs_barrier );   
347    /////////////////////////////////////////
348
349    ////////////////////////////////////////////////////////////////////////////
350    // step 4 : Each processor compute values for registers SP, SR, PTPR, EPC,
351    //          corresponding to the first allocated task (can be idle task)
352    //          and jumps to user code.
353    ////////////////////////////////////////////////////////////////////////////
354
355    if (tasks == 0) _printf("\n[GIET WARNING] No task allocated to P[%d,%d,%d]\n",
356                            x, y, p );
357
358    if (tasks == 0) ltid = IDLE_TASK_INDEX;
359    else            ltid = 0;
360
361    unsigned int sp_value   = _get_task_slot( x, y, p, ltid, CTX_SP_ID);
362    unsigned int sr_value   = _get_task_slot( x, y, p, ltid, CTX_SR_ID);
363    unsigned int ptpr_value = _get_task_slot( x, y, p, ltid, CTX_PTPR_ID);
364    unsigned int epc_value  = _get_task_slot( x, y, p, ltid, CTX_EPC_ID);
365
366#if GIET_DEBUG_INIT
367_printf("\n[DEBUG KINIT] P[%d,%d,%d] jumps to user code at cycle %d\n"
368        " ltid = %d / sp = %x / sr = %x / ptpr = %x / epc = %x\n",
369        x , y , p , _get_proctime() ,
370        ltid , sp_value , sr_value , ptpr_value , epc_value );
371#endif
372
373    // set registers and jump to user code
374    asm volatile ( "move  $29,  %0                  \n"   /* SP <= ctx[CTX_SP_ID] */
375                   "mtc0  %1,   $12                 \n"   /* SR <= ctx[CTX_SR_ID] */
376                   "mtc2  %2,   $0                  \n"   /* PTPR <= ctx[CTX_PTPR] */
377                   "mtc0  %3,   $14                 \n"   /* EPC <= ctx[CTX_EPC]  */
378                   "eret                            \n"   /* jump to user code  */
379                   "nop                             \n"
380                   : 
381                   : "r"(sp_value), "r"(sr_value), "r"(ptpr_value), "r"(epc_value)
382                   : "$29", "memory" );
383
384} // end kernel_init()
385
386
387// Local Variables:
388// tab-width: 4
389// c-basic-offset: 4
390// c-file-offsets:((innamespace . 0)(inline-open . 0))
391// indent-tabs-mode: nil
392// End:
393// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
394
Note: See TracBrowser for help on using the repository browser.