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

Last change on this file since 581 was 579, checked in by bellefin, 9 years ago

Improve hba driver : adding the _hba_boot_mode variable to indicate which mode is used

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