source: soft/giet_vm/giet_boot/boot.c @ 742

Last change on this file since 742 was 742, checked in by alain, 8 years ago

Remove the seg_kernel_init vseg: All the kernel code is now packed
in one single seg_kernel_code vseg. The entry point for the _kernel_init()
function (from the boot code is now at vaddr = 0x80000000.
The goal is to use only one BPP per cluster for the replicated kernel code.

File size: 76.8 KB
Line 
1///////////////////////////////////////////////////////////////////////////////////
2// File     : boot.c
3// Date     : 01/11/2013
4// Author   : alain greiner
5// Copyright (c) UPMC-LIP6
6///////////////////////////////////////////////////////////////////////////////////
7// The boot.c file contains the bootloader for the GIET-VM static OS. 
8//
9// This code has been written for the MIPS32 processor.
10// The virtual adresses are on 32 bits and use the (unsigned int) type. The
11// physicals addresses can have up to 40 bits, and use type (unsigned long long).
12// It natively supports clusterised shared memory multi-processors architectures,
13// where each processor is identified by a composite index [x,y,p],
14// and where there is one physical memory bank per cluster.
15//
16// The boot.elf file is stored on disk and is loaded into memory by proc[0,0,0],
17// executing the generic preloader (stored in ROM). The boot-loader code itself
18// is executed in parallel by all proc[x,y,0], and performs the following tasks:
19// - load into memory various binary files, from a FAT32 file system.
20// - build the various page tables (one page table per vspace).
21// - initialize the shedulers (one scheduler per processor).
22//
23// 1) The binary files to be loaded are:
24//    - the "map.bin" file contains the hardware architecture description,
25//      the set of user applications that will be mapped on the architecture,
26//      and the mapping directives. The mapping includes the placement of threads
27//      on processors, and the placement of virtual segments on the physical
28//      segments. It is stored in the the seg_boot_mapping segment
29//      (at address SEG_BOOT_MAPPING_BASE defined in hard_config.h file).
30//    - the "kernel.elf" file contains the kernel binary code and data.
31//    - the various "application.elf" files.
32//
33// 2) The GIET-VM uses the paged virtual memory to provide two services:
34//    - classical memory protection, when several independant applications compiled
35//      in different virtual spaces are executing on the same hardware platform.
36//    - data placement in NUMA architectures, to control the placement
37//      of the software objects (vsegs) on the physical memory banks (psegs).
38//    The max number of vspaces (GIET_NB_VSPACE_MAX) is a configuration parameter.
39//    The page tables are statically build in the boot phase, and they do not
40//    change during execution.
41//    For each application, the page tables are replicated in all clusters.
42//    The GIET_VM uses both small pages (4 Kbytes), and big pages (2 Mbytes).
43//    Each page table (one page table per virtual space) is monolithic, and
44//    contains one PT1 (8 Kbytes) and a variable number of PT2s (4 Kbytes each).
45//    For each vspace, the max number of PT2s is defined by the size of the PTAB
46//    vseg in the mapping.
47//    The PT1 is indexed by the ix1 field (11 bits) of the VPN. An entry is 32 bits.
48//    A PT2 is indexed the ix2 field (9 bits) of the VPN. An entry is 64 bits.
49//    The first word contains the flags, the second word contains the PPN.
50//
51// 3) The Giet-VM implement one private scheduler per processor.
52//    For each application, the threads are statically allocated to processors
53//    and there is no thread migration during execution.
54//    Each sheduler occupies 8K bytes, and contains up to 14 thread contexts
55//    The thread context [13] is reserved for the "idle" thread that does nothing,
56//    and is launched by the scheduler when there is no other runable thread.
57///////////////////////////////////////////////////////////////////////////////////
58// Implementation Notes:
59//
60// 1) The cluster_id variable is a linear index in the mapping_info array.
61//    The cluster_xy variable is the tological index = x << Y_WIDTH + y
62//
63// 2) We set the _tty0_boot_mode variable to force the _printf() function to use
64//    the tty0_spin_lock for exclusive access to TTY0.
65///////////////////////////////////////////////////////////////////////////////////
66
67#include <giet_config.h>
68#include <hard_config.h>
69#include <mapping_info.h>
70#include <kernel_malloc.h>
71#include <memspace.h>
72#include <tty_driver.h>
73#include <xcu_driver.h>
74#include <bdv_driver.h>
75#include <hba_driver.h>
76#include <sdc_driver.h>
77#include <cma_driver.h>
78#include <nic_driver.h>
79#include <iob_driver.h>
80#include <pic_driver.h>
81#include <mwr_driver.h>
82#include <dma_driver.h>
83#include <mmc_driver.h>
84#include <ctx_handler.h>
85#include <irq_handler.h>
86#include <vmem.h>
87#include <pmem.h>
88#include <utils.h>
89#include <tty0.h>
90#include <kernel_locks.h>
91#include <kernel_barriers.h>
92#include <elf-types.h>
93#include <fat32.h>
94#include <mips32_registers.h>
95#include <stdarg.h>
96
97#if !defined(X_SIZE)
98# error: The X_SIZE value must be defined in the 'hard_config.h' file !
99#endif
100
101#if !defined(Y_SIZE)
102# error: The Y_SIZE value must be defined in the 'hard_config.h' file !
103#endif
104
105#if !defined(X_WIDTH)
106# error: The X_WIDTH value must be defined in the 'hard_config.h' file !
107#endif
108
109#if !defined(Y_WIDTH)
110# error: The Y_WIDTH value must be defined in the 'hard_config.h' file !
111#endif
112
113#if !defined(SEG_BOOT_MAPPING_BASE)
114# error: The SEG_BOOT_MAPPING_BASE value must be defined in the hard_config.h file !
115#endif
116
117#if !defined(NB_PROCS_MAX)
118# error: The NB_PROCS_MAX value must be defined in the 'hard_config.h' file !
119#endif
120
121#if !defined(GIET_NB_VSPACE_MAX)
122# error: The GIET_NB_VSPACE_MAX value must be defined in the 'giet_config.h' file !
123#endif
124
125#if !defined(GIET_ELF_BUFFER_SIZE)
126# error: The GIET_ELF_BUFFER_SIZE value must be defined in the giet_config.h file !
127#endif
128
129////////////////////////////////////////////////////////////////////////////
130//      Global variables for boot code
131////////////////////////////////////////////////////////////////////////////
132
133// Temporaty buffer used to load one complete .elf file 
134__attribute__((section(".kdata")))
135unsigned char  _boot_elf_buffer[GIET_ELF_BUFFER_SIZE] __attribute__((aligned(64)));
136
137// Physical memory allocators array (one per cluster)
138__attribute__((section(".kdata")))
139pmem_alloc_t  boot_pmem_alloc[X_SIZE][Y_SIZE];
140
141// Schedulers virtual base addresses array (one per processor)
142__attribute__((section(".kdata")))
143static_scheduler_t* _schedulers[X_SIZE][Y_SIZE][NB_PROCS_MAX];
144
145// Page tables virtual base addresses (one per vspace and per cluster)
146__attribute__((section(".kdata")))
147unsigned int        _ptabs_vaddr[GIET_NB_VSPACE_MAX][X_SIZE][Y_SIZE];
148
149// Page tables physical base addresses (one per vspace and per cluster)
150__attribute__((section(".kdata")))
151paddr_t             _ptabs_paddr[GIET_NB_VSPACE_MAX][X_SIZE][Y_SIZE];
152
153// Page tables pt2 allocators (one per vspace and per cluster)
154__attribute__((section(".kdata")))
155unsigned int        _ptabs_next_pt2[GIET_NB_VSPACE_MAX][X_SIZE][Y_SIZE];
156
157// Page tables max_pt2  (same value for all page tables)
158__attribute__((section(".kdata")))
159unsigned int        _ptabs_max_pt2;
160
161// boot code uses a spin lock to protect TTY0
162__attribute__((section(".kdata")))
163unsigned int        _tty0_boot_mode = 1;
164
165// boot code does not uses a lock to protect HBA command list
166__attribute__((section(".kdata")))
167unsigned int        _hba_boot_mode = 1;
168
169// required for concurrent PTAB building
170__attribute__((section(".kdata")))
171spin_lock_t         _ptabs_spin_lock[GIET_NB_VSPACE_MAX][X_SIZE][Y_SIZE];
172
173// barrier used by boot code for parallel execution
174__attribute__((section(".kdata")))
175simple_barrier_t    _barrier_all_clusters;
176
177//////////////////////////////////////////////////////////////////////////////
178//        Extern variables
179//////////////////////////////////////////////////////////////////////////////
180
181// this variable is allocated in the tty0.c file
182extern spin_lock_t  _tty0_spin_lock;
183
184// this variable is allocated in the mmc_driver.c
185extern unsigned int _mmc_boot_mode;
186
187// these variables are allocated in the bdv_driver.c file
188extern spin_lock_t  _bdv_lock __attribute__((aligned(64)));
189extern unsigned int _bdv_trdid;
190extern unsigned int _bdv_status;
191
192extern void boot_entry();
193
194//////////////////////////////////////////////////////////////////////////////
195// This function registers a new PTE1 in the page table defined
196// by the vspace_id argument, and the (x,y) coordinates.
197// It updates only the first level PT1.
198// As each vseg is mapped by a different processor, the PT1 entry cannot
199// be concurrently accessed, and we don't need to take any lock.
200//
201// Implementation note:
202// This function checks that the PT1 entry is not already mapped,
203// to enforce the rule: only one vseg in a given BPP.
204// The 4 vsegs used by the boot code being packed in one single BPP,
205// this verif is not done for all identity mapping vsegs.
206//////////////////////////////////////////////////////////////////////////////
207void boot_add_pte1( unsigned int vspace_id,
208                    unsigned int x,
209                    unsigned int y,
210                    unsigned int vpn,        // 20 bits right-justified
211                    unsigned int flags,      // 10 bits left-justified
212                    unsigned int ppn,        // 28 bits right-justified
213                    unsigned int ident )     // identity mapping if non zero
214{
215    unsigned int   pte1;     // PTE1 value
216    paddr_t        paddr;    // PTE1 physical address
217
218    // compute index in PT1
219    unsigned int    ix1 = vpn >> 9;         // 11 bits for ix1
220
221    // get PT1 physical base address
222    paddr_t  pt1_base = _ptabs_paddr[vspace_id][x][y];
223
224    if ( pt1_base == 0 )
225    {
226        _printf("\n[BOOT ERROR] in boot_add_pte1() : no PTAB in cluster[%d,%d]"
227                    " containing processors\n", x , y );
228        _exit();
229    }
230
231    // compute pte1 physical address
232    paddr = pt1_base + 4*ix1;
233
234    // check PTE1 not already mapped
235    if ( ident == 0 )
236    {
237        if ( _physical_read( paddr ) & PTE_V )
238        {
239            _printf("\n[BOOT ERROR] in boot_add_pte1() : vpn %x already mapped "
240                    "in PTAB[%d,%d] for vspace %d\n", vpn , x , y , vspace_id );
241            _exit();
242        }
243    }
244
245    // compute pte1 : 2 bits V T / 8 bits flags / 3 bits RSVD / 19 bits bppi
246    pte1 = PTE_V | (flags & 0x3FC00000) | ((ppn>>9) & 0x0007FFFF);
247
248    // write pte1 in PT1
249    _physical_write( paddr , pte1 );
250
251    asm volatile ("sync");
252
253}   // end boot_add_pte1()
254
255//////////////////////////////////////////////////////////////////////////////
256// This function registers a new PTE2 in the page table defined
257// by the vspace_id argument, and the (x,y) coordinates.
258// It updates both the first level PT1 and the second level PT2.
259// As the set of PT2s is implemented as a fixed size array (no dynamic
260// allocation), this function checks a possible overflow of the PT2 array.
261// As a given entry in PT1 can be shared by several vsegs, mapped by
262// different processors, we need to take the lock protecting PTAB[v][x][y].
263//////////////////////////////////////////////////////////////////////////////
264void boot_add_pte2( unsigned int vspace_id,
265                    unsigned int x,
266                    unsigned int y,
267                    unsigned int vpn,        // 20 bits right-justified
268                    unsigned int flags,      // 10 bits left-justified
269                    unsigned int ppn,        // 28 bits right-justified
270                    unsigned int ident )     // identity mapping if non zero
271{
272    unsigned int ix1;
273    unsigned int ix2;
274    paddr_t      pt2_pbase;     // PT2 physical base address
275    paddr_t      pte2_paddr;    // PTE2 physical address
276    unsigned int pt2_id;        // PT2 index
277    unsigned int ptd;           // PTD : entry in PT1
278
279    ix1 = vpn >> 9;             // 11 bits for ix1
280    ix2 = vpn & 0x1FF;          //  9 bits for ix2
281
282    // get page table physical base address
283    paddr_t      pt1_pbase = _ptabs_paddr[vspace_id][x][y];
284
285    if ( pt1_pbase == 0 )
286    {
287        _printf("\n[BOOT ERROR] in boot_add_pte2() : no PTAB for vspace %d "
288                "in cluster[%d,%d]\n", vspace_id , x , y );
289        _exit();
290    }
291
292    // get lock protecting PTAB[vspace_id][x][y]
293    _spin_lock_acquire( &_ptabs_spin_lock[vspace_id][x][y] );
294
295    // get ptd in PT1
296    ptd = _physical_read( pt1_pbase + 4 * ix1 );
297
298    if ((ptd & PTE_V) == 0)    // undefined PTD: compute PT2 base address,
299                               // and set a new PTD in PT1
300    {
301        // get a new pt2_id
302        pt2_id = _ptabs_next_pt2[vspace_id][x][y];
303        _ptabs_next_pt2[vspace_id][x][y] = pt2_id + 1;
304
305        // check overflow
306        if (pt2_id == _ptabs_max_pt2) 
307        {
308            _printf("\n[BOOT ERROR] in boot_add_pte2() : PTAB[%d,%d,%d]"
309                    " contains not enough PT2s\n", vspace_id, x, y );
310            _exit();
311        }
312
313        pt2_pbase = pt1_pbase + PT1_SIZE + PT2_SIZE * pt2_id;
314        ptd = PTE_V | PTE_T | (unsigned int) (pt2_pbase >> 12);
315
316        // set PTD into PT1
317        _physical_write( pt1_pbase + 4*ix1, ptd);
318    }
319    else                       // valid PTD: compute PT2 base address
320    {
321        pt2_pbase = ((paddr_t)(ptd & 0x0FFFFFFF)) << 12;
322    }
323
324    // set PTE in PT2 : flags & PPN in two 32 bits words
325    pte2_paddr  = pt2_pbase + 8 * ix2;
326    _physical_write(pte2_paddr     , (PTE_V | flags) );
327    _physical_write(pte2_paddr + 4 , ppn );
328
329    // release lock protecting PTAB[vspace_id][x][y]
330    _spin_lock_release( &_ptabs_spin_lock[vspace_id][x][y] );
331
332    asm volatile ("sync");
333
334}   // end boot_add_pte2()
335
336////////////////////////////////////////////////////////////////////////////////////
337// Align the value of paddr or vaddr to the required alignement,
338// defined by alignPow2 == L2(alignement).
339////////////////////////////////////////////////////////////////////////////////////
340paddr_t paddr_align_to( paddr_t paddr, unsigned int alignPow2 ) 
341{
342    paddr_t mask = (1 << alignPow2) - 1;
343    return ((paddr + mask) & ~mask);
344}
345
346unsigned int vaddr_align_to( unsigned int vaddr, unsigned int alignPow2 ) 
347{
348    unsigned int mask = (1 << alignPow2) - 1;
349    return ((vaddr + mask) & ~mask);
350}
351
352/////////////////////////////////////////////////////////////////////////////////////
353// This function map a vseg identified by the vseg pointer.
354//
355// A given vseg can be mapped in a Big Physical Pages (BPP: 2 Mbytes) or in a
356// Small Physical Pages (SPP: 4 Kbytes), depending on the "big" attribute of vseg.
357//
358// All boot vsegs are packed in a single BPP (2 Mbytes). For all other vsegs,
359// there is only one vseg in a given page (BPP or SPP), but a single vseg can
360// cover several contiguous physical pages.
361// Only the vsegs used by the boot code can be identity mapping.
362//
363// 1) First step: it computes various vseg attributes and checks
364//    alignment constraints.
365//
366// 2) Second step: it allocates the required number of contiguous physical pages,
367//    computes the physical base address (if the vseg is not identity mapping),
368//    register it in the vseg pbase field, and update the page table(s).
369//
370// 3) Third step (only for vseg that have the VSEG_TYPE_PTAB): for a given cluster,
371//    the M page tables associated to the M vspaces are packed in the same vseg.
372//    We divide this vseg in M sub-segments, and compute the vbase and pbase
373//    addresses for M page tables, and register these addresses in the _ptabs_paddr
374//    and _ptabs_vaddr arrays.
375/////////////////////////////////////////////////////////////////////////////////////
376void boot_vseg_map( mapping_vseg_t* vseg,
377                    unsigned int    vspace_id )
378{
379    mapping_header_t*   header  = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
380    mapping_cluster_t*  cluster = _get_cluster_base(header);
381    mapping_pseg_t*     pseg    = _get_pseg_base(header);
382
383    //////////// First step : compute vseg attributes
384
385    // compute destination cluster pointer & coordinates
386    pseg    = pseg + vseg->psegid;
387    cluster = cluster + pseg->clusterid;
388    unsigned int        x_dest     = cluster->x;
389    unsigned int        y_dest     = cluster->y;
390
391    // compute the "big" vseg attribute
392    unsigned int        big = vseg->big;
393
394    // all vsegs must be aligned on 4Kbytes
395    if ( vseg->vbase & 0x00000FFF ) 
396    {
397        _printf("\n[BOOT ERROR] vseg %s not aligned : vbase = %x\n", 
398                vseg->name, vseg->vbase );
399        _exit();
400    }
401
402    // compute the "is_ram" vseg attribute
403    unsigned int        is_ram;
404    if ( pseg->type == PSEG_TYPE_RAM )  is_ram = 1;
405    else                                is_ram = 0;
406
407    // compute the "is_ptab" attribute
408    unsigned int        is_ptab;
409    if ( vseg->type == VSEG_TYPE_PTAB ) is_ptab = 1;
410    else                                is_ptab = 0;
411
412    // compute actual vspace index
413    unsigned int vsid;
414    if ( vspace_id == 0xFFFFFFFF ) vsid = 0;
415    else                           vsid = vspace_id;
416
417    //////////// Second step : compute ppn and npages 
418    //////////// - if identity mapping :  ppn <= vpn
419    //////////// - if vseg is periph   :  ppn <= pseg.base >> 12
420    //////////// - if vseg is ram      :  ppn <= physical memory allocator
421
422    unsigned int ppn;          // first physical page index (28 bits = |x|y|bppi|sppi|)
423    unsigned int vpn;          // first virtual page index  (20 bits = |ix1|ix2|)
424    unsigned int vpn_max;      // last  virtual page index  (20 bits = |ix1|ix2|)
425
426    vpn     = vseg->vbase >> 12;
427    vpn_max = (vseg->vbase + vseg->length - 1) >> 12;
428
429    // compute npages
430    unsigned int npages;       // number of required (big or small) pages
431    if ( big == 0 ) npages  = vpn_max - vpn + 1;            // number of small pages
432    else            npages  = (vpn_max>>9) - (vpn>>9) + 1;  // number of big pages
433
434    // compute ppn
435    if ( vseg->ident )           // identity mapping : no memory allocation required
436    {
437        ppn = vpn;
438    }
439    else                         // not identity mapping
440    {
441        if ( is_ram )            // RAM : physical memory allocation required
442        {
443            // compute pointer on physical memory allocator in dest cluster
444            pmem_alloc_t*     palloc = &boot_pmem_alloc[x_dest][y_dest];
445
446            if ( big == 0 )      // allocate contiguous SPPs
447            {
448                ppn = _get_small_ppn( palloc, npages );
449            }
450            else                 // allocate contiguous BPPs
451            {
452                ppn = _get_big_ppn( palloc, npages ); 
453            }
454        }
455        else                    // PERI : no memory allocation required
456        {
457            ppn = pseg->base >> 12;
458        }
459    }
460
461    // update vseg.pbase field and register vseg mapped
462    vseg->pbase     = ((paddr_t)ppn) << 12;
463    vseg->mapped    = 1;
464
465    //////////// Third step : (only if the vseg is a page table)
466    //////////// - compute the physical & virtual base address for each vspace
467    ////////////   by dividing the vseg in several sub-segments.
468    //////////// - register it in _ptabs_vaddr & _ptabs_paddr arrays,
469    ////////////   and initialize next_pt2 allocators.
470    //////////// - reset all entries in first level page tables
471   
472    if ( is_ptab )
473    {
474        unsigned int   vs;        // vspace index
475        unsigned int   nspaces;   // number of vspaces
476        unsigned int   nsp;       // number of small pages for one PTAB
477        unsigned int   offset;    // address offset for current PTAB
478
479        nspaces = header->vspaces;
480        offset  = 0;
481
482        // each PTAB must be aligned on a 8 Kbytes boundary
483        nsp = ( vseg->length >> 12 ) / nspaces;
484        if ( (nsp & 0x1) == 0x1 ) nsp = nsp - 1;
485
486        // compute max_pt2
487        _ptabs_max_pt2 = ((nsp<<12) - PT1_SIZE) / PT2_SIZE;
488
489        for ( vs = 0 ; vs < nspaces ; vs++ )
490        {
491            _ptabs_vaddr   [vs][x_dest][y_dest] = (vpn + offset) << 12;
492            _ptabs_paddr   [vs][x_dest][y_dest] = ((paddr_t)(ppn + offset)) << 12;
493            _ptabs_next_pt2[vs][x_dest][y_dest] = 0;
494            offset += nsp;
495
496            // reset all entries in PT1 (8 Kbytes)
497            _physical_memset( _ptabs_paddr[vs][x_dest][y_dest], PT1_SIZE, 0 );
498        }
499    }
500
501    asm volatile ("sync");
502
503#if BOOT_DEBUG_PT
504if ( big )
505_printf("\n[BOOT] vseg %s : cluster[%d,%d] / "
506       "vbase = %x / length = %x / BIG    / npages = %d / pbase = %l\n",
507       vseg->name, x_dest, y_dest, vseg->vbase, vseg->length, npages, vseg-> pbase );
508else
509_printf("\n[BOOT] vseg %s : cluster[%d,%d] / "
510        "vbase = %x / length = %x / SMALL / npages = %d / pbase = %l\n",
511       vseg->name, x_dest, y_dest, vseg->vbase, vseg->length, npages, vseg-> pbase );
512#endif
513
514} // end boot_vseg_map()
515
516/////////////////////////////////////////////////////////////////////////////////////
517// For the vseg defined by the vseg pointer, this function register PTEs
518// in one or several page tables.
519// It is a global vseg (kernel vseg) if (vspace_id == 0xFFFFFFFF).
520// The number of involved PTABs depends on the "local" and "global" attributes:
521//  - PTEs are replicated in all vspaces for a global vseg.
522//  - PTEs are replicated in all clusters containing procs for a non local vseg.
523/////////////////////////////////////////////////////////////////////////////////////
524void boot_vseg_pte( mapping_vseg_t*  vseg,
525                    unsigned int     vspace_id )
526{
527    // compute the "global" vseg attribute and actual vspace index
528    unsigned int        global;
529    unsigned int        vsid;   
530    if ( vspace_id == 0xFFFFFFFF )
531    {
532        global = 1;
533        vsid   = 0;
534    }
535    else
536    {
537        global = 0;
538        vsid   = vspace_id;
539    }
540
541    // compute the "local" and "big" attributes
542    unsigned int        local  = vseg->local;
543    unsigned int        big    = vseg->big;
544
545    // compute vseg flags
546    // The three flags (Local, Remote and Dirty) are set to 1
547    // to avoid hardware update for these flags, because GIET_VM
548    // does use these flags.
549    unsigned int flags = 0;
550    if (vseg->mode & C_MODE_MASK) flags |= PTE_C;
551    if (vseg->mode & X_MODE_MASK) flags |= PTE_X;
552    if (vseg->mode & W_MODE_MASK) flags |= PTE_W;
553    if (vseg->mode & U_MODE_MASK) flags |= PTE_U;
554    if ( global )                 flags |= PTE_G;
555                                  flags |= PTE_L;
556                                  flags |= PTE_R;
557                                  flags |= PTE_D;
558
559    // compute VPN, PPN and number of pages (big or small)
560    unsigned int vpn     = vseg->vbase >> 12;
561    unsigned int vpn_max = (vseg->vbase + vseg->length - 1) >> 12;
562    unsigned int ppn     = (unsigned int)(vseg->pbase >> 12);
563    unsigned int npages;
564    if ( big == 0 ) npages  = vpn_max - vpn + 1;           
565    else            npages  = (vpn_max>>9) - (vpn>>9) + 1; 
566
567    // compute destination cluster coordinates, for local vsegs
568    mapping_header_t*   header       = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
569    mapping_cluster_t*  cluster      = _get_cluster_base(header);
570    mapping_pseg_t*     pseg         = _get_pseg_base(header);
571    mapping_pseg_t*     pseg_dest    = &pseg[vseg->psegid];
572    mapping_cluster_t*  cluster_dest = &cluster[pseg_dest->clusterid];
573    unsigned int        x_dest       = cluster_dest->x;
574    unsigned int        y_dest       = cluster_dest->y;
575
576    unsigned int p;           // iterator for physical page index
577    unsigned int x;           // iterator for cluster x coordinate 
578    unsigned int y;           // iterator for cluster y coordinate 
579    unsigned int v;           // iterator for vspace index
580
581    // loop on PTEs
582    for ( p = 0 ; p < npages ; p++ )
583    { 
584        if  ( (local != 0) && (global == 0) )         // one cluster  / one vspace
585        {
586            if ( big )   // big pages => PTE1s
587            {
588                boot_add_pte1( vsid,
589                               x_dest,
590                               y_dest,
591                               vpn + (p<<9),
592                               flags, 
593                               ppn + (p<<9),
594                               vseg->ident );
595            }
596            else         // small pages => PTE2s
597            {
598                boot_add_pte2( vsid,
599                               x_dest,
600                               y_dest,
601                               vpn + p,     
602                               flags, 
603                               ppn + p,
604                               vseg->ident );
605            }
606        }
607        else if ( (local == 0) && (global == 0) )     // all clusters / one vspace
608        {
609            for ( x = 0 ; x < X_SIZE ; x++ )
610            {
611                for ( y = 0 ; y < Y_SIZE ; y++ )
612                {
613                    if ( cluster[(x * Y_SIZE) + y].procs )
614                    {
615                        if ( big )   // big pages => PTE1s
616                        {
617                            boot_add_pte1( vsid,
618                                           x,
619                                           y,
620                                           vpn + (p<<9),
621                                           flags, 
622                                           ppn + (p<<9),
623                                           vseg->ident );
624                        }
625                        else         // small pages => PTE2s
626                        {
627                            boot_add_pte2( vsid,
628                                           x,
629                                           y,
630                                           vpn + p,
631                                           flags, 
632                                           ppn + p,
633                                           vseg->ident );
634                        }
635                    }
636                }
637            }
638        }
639        else if ( (local != 0) && (global != 0) )     // one cluster  / all vspaces
640        {
641            for ( v = 0 ; v < header->vspaces ; v++ )
642            {
643                if ( big )   // big pages => PTE1s
644                {
645                    boot_add_pte1( v,
646                                   x_dest,
647                                   y_dest,
648                                   vpn + (p<<9),
649                                   flags, 
650                                   ppn + (p<<9),
651                                   vseg->ident );
652                }
653                else         // small pages = PTE2s
654                { 
655                    boot_add_pte2( v,
656                                   x_dest,
657                                   y_dest,
658                                   vpn + p,
659                                   flags, 
660                                   ppn + p,
661                                   vseg->ident );
662                }
663            }
664        }
665        else if ( (local == 0) && (global != 0) )     // all clusters / all vspaces
666        {
667            for ( x = 0 ; x < X_SIZE ; x++ )
668            {
669                for ( y = 0 ; y < Y_SIZE ; y++ )
670                {
671                    if ( cluster[(x * Y_SIZE) + y].procs )
672                    {
673                        for ( v = 0 ; v < header->vspaces ; v++ )
674                        {
675                            if ( big )  // big pages => PTE1s
676                            {
677                                boot_add_pte1( v,
678                                               x,
679                                               y,
680                                               vpn + (p<<9),
681                                               flags, 
682                                               ppn + (p<<9),
683                                               vseg->ident );
684                            }
685                            else        // small pages -> PTE2s
686                            {
687                                boot_add_pte2( v,
688                                               x,
689                                               y,
690                                               vpn + p,
691                                               flags, 
692                                               ppn + p,
693                                               vseg->ident );
694                            }
695                        }
696                    }
697                }
698            }
699        }
700    }  // end for pages
701
702    asm volatile ("sync");
703
704}  // end boot_vseg_pte()
705
706
707///////////////////////////////////////////////////////////////////////////////
708// This function is executed by  processor[x][y][0] in each cluster
709// containing at least one processor.
710// It initialises all page table for all global or private vsegs
711// mapped in cluster[x][y], as specified in the mapping.
712// In each cluster all page tables for the different vspaces must be
713// packed in one vseg occupying one single BPP (Big Physical Page).
714//
715// For each vseg, the mapping is done in two steps:
716// 1) mapping : the boot_vseg_map() function allocates contiguous BPPs
717//    or SPPs (if the vseg is not associated to a peripheral), and register
718//    the physical base address in the vseg pbase field. It initialises the
719//    _ptabs_vaddr[] and _ptabs_paddr[] arrays if the vseg is a PTAB.
720//
721// 2) page table initialisation : the boot_vseg_pte() function initialise
722//    the PTEs (both PTE1 and PTE2) in one or several page tables:
723//    - PTEs are replicated in all vspaces for a global vseg.
724//    - PTEs are replicated in all clusters for a non local vseg.
725//
726// We must handle vsegs in the following order
727//   1) global vseg containing PTAB mapped in cluster[x][y],
728//   2) global vsegs occupying more than one BPP mapped in cluster[x][y],
729//   3) others global vsegs mapped in cluster[x][y],
730//   4) all private vsegs in all user spaces mapped in cluster[x][y].
731///////////////////////////////////////////////////////////////////////////////
732void boot_ptab_init( unsigned int cx,
733                     unsigned int cy ) 
734{
735    mapping_header_t*   header = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
736    mapping_vspace_t*   vspace = _get_vspace_base(header);
737    mapping_vseg_t*     vseg   = _get_vseg_base(header);
738    mapping_cluster_t*  cluster ;
739    mapping_pseg_t*     pseg    ;
740
741    unsigned int vspace_id;
742    unsigned int vseg_id;
743
744    unsigned int procid     = _get_procid();
745    unsigned int lpid       = procid & ((1<<P_WIDTH)-1);
746
747    if( lpid )
748    {
749        _printf("\n[BOOT ERROR] in boot_ptab_init() : "
750                "P[%d][%d][%d] should not execute it\n", cx, cy, lpid );
751        _exit();
752    } 
753
754    if ( header->vspaces == 0 )
755    {
756        _printf("\n[BOOT ERROR] in boot_ptab_init() : "
757                "mapping %s contains no vspace\n", header->name );
758        _exit();
759    }
760
761    ///////// Phase 1 : global vseg containing the PTAB (two barriers required)
762
763    // get PTAB global vseg in cluster(cx,cy)
764    unsigned int found = 0;
765    for (vseg_id = 0; vseg_id < header->globals; vseg_id++) 
766    {
767        pseg    = _get_pseg_base(header) + vseg[vseg_id].psegid;
768        cluster = _get_cluster_base(header) + pseg->clusterid;
769        if ( (vseg[vseg_id].type == VSEG_TYPE_PTAB) && 
770             (cluster->x == cx) && (cluster->y == cy) )
771        {
772            found = 1;
773            break;
774        }
775    }
776    if ( found == 0 )
777    {
778        _printf("\n[BOOT ERROR] in boot_ptab_init() : "
779                "cluster[%d][%d] contains no PTAB vseg\n", cx , cy );
780        _exit();
781    }
782
783    boot_vseg_map( &vseg[vseg_id], 0xFFFFFFFF );
784
785    //////////////////////////////////////////////
786    _simple_barrier_wait( &_barrier_all_clusters );
787    //////////////////////////////////////////////
788
789    boot_vseg_pte( &vseg[vseg_id], 0xFFFFFFFF );
790
791    //////////////////////////////////////////////
792    _simple_barrier_wait( &_barrier_all_clusters );
793    //////////////////////////////////////////////
794
795    ///////// Phase 2 : global vsegs occupying more than one BPP
796
797    for (vseg_id = 0; vseg_id < header->globals; vseg_id++) 
798    {
799        pseg    = _get_pseg_base(header) + vseg[vseg_id].psegid;
800        cluster = _get_cluster_base(header) + pseg->clusterid;
801        if ( (vseg[vseg_id].length > 0x200000) &&
802             (vseg[vseg_id].mapped == 0) &&
803             (cluster->x == cx) && (cluster->y == cy) )
804        {
805            boot_vseg_map( &vseg[vseg_id], 0xFFFFFFFF );
806            boot_vseg_pte( &vseg[vseg_id], 0xFFFFFFFF );
807        }
808    }
809
810    ///////// Phase 3 : all others global vsegs
811
812    for (vseg_id = 0; vseg_id < header->globals; vseg_id++) 
813    { 
814        pseg    = _get_pseg_base(header) + vseg[vseg_id].psegid;
815        cluster = _get_cluster_base(header) + pseg->clusterid;
816        if ( (vseg[vseg_id].mapped == 0) && 
817             (cluster->x == cx) && (cluster->y == cy) )
818        {
819            boot_vseg_map( &vseg[vseg_id], 0xFFFFFFFF );
820            boot_vseg_pte( &vseg[vseg_id], 0xFFFFFFFF );
821        }
822    }
823
824    ///////// Phase 4 : all private vsegs
825
826    for (vspace_id = 0; vspace_id < header->vspaces; vspace_id++) 
827    {
828        for (vseg_id = vspace[vspace_id].vseg_offset;
829             vseg_id < (vspace[vspace_id].vseg_offset + vspace[vspace_id].vsegs);
830             vseg_id++) 
831        {
832            pseg    = _get_pseg_base(header) + vseg[vseg_id].psegid;
833            cluster = _get_cluster_base(header) + pseg->clusterid;
834            if ( (cluster->x == cx) && (cluster->y == cy) )
835            {
836                boot_vseg_map( &vseg[vseg_id], vspace_id );
837                boot_vseg_pte( &vseg[vseg_id], vspace_id );
838            }
839        }
840    }
841
842    //////////////////////////////////////////////
843    _simple_barrier_wait( &_barrier_all_clusters );
844    //////////////////////////////////////////////
845
846} // end boot_ptab_init()
847
848////////////////////////////////////////////////////////////////////////////////
849// This function should be executed by P[0][0][0] only. It complete the
850// page table initialisation, taking care of all global vsegs that are
851// not mapped in a cluster containing a processor, and have not been
852// handled by the boot_ptab_init(x,y) function.
853// An example of such vsegs are the external peripherals in TSAR_LETI platform.
854////////////////////////////////////////////////////////////////////////////////
855void boot_ptab_extend()
856{
857
858    mapping_header_t*   header = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
859    mapping_vseg_t*     vseg   = _get_vseg_base(header);
860
861    unsigned int vseg_id;
862
863    for (vseg_id = 0; vseg_id < header->globals; vseg_id++) 
864    {
865        if ( vseg[vseg_id].mapped == 0 ) 
866        {
867            boot_vseg_map( &vseg[vseg_id], 0xFFFFFFFF );
868            boot_vseg_pte( &vseg[vseg_id], 0xFFFFFFFF );
869        }
870    }
871}  // end boot_ptab_extend()
872
873///////////////////////////////////////////////////////////////////////////////
874// This function returns in the vbase and length buffers the virtual base
875// address and the length of the  segment allocated to the schedulers array
876// in the cluster defined by the clusterid argument.
877///////////////////////////////////////////////////////////////////////////////
878void boot_get_sched_vaddr( unsigned int  cluster_id,
879                           unsigned int* vbase, 
880                           unsigned int* length )
881{
882    mapping_header_t* header = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
883    mapping_vseg_t*   vseg   = _get_vseg_base(header);
884    mapping_pseg_t*   pseg   = _get_pseg_base(header);
885
886    unsigned int vseg_id;
887    unsigned int found = 0;
888
889    for ( vseg_id = 0 ; (vseg_id < header->vsegs) && (found == 0) ; vseg_id++ )
890    {
891        if ( (vseg[vseg_id].type == VSEG_TYPE_SCHED) && 
892             (pseg[vseg[vseg_id].psegid].clusterid == cluster_id ) )
893        {
894            *vbase  = vseg[vseg_id].vbase;
895            *length = vseg[vseg_id].length;
896            found = 1;
897        }
898    }
899    if ( found == 0 )
900    {
901        mapping_cluster_t* cluster = _get_cluster_base(header);
902        _printf("\n[BOOT ERROR] No vseg of type SCHED in cluster [%d,%d]\n",
903                cluster[cluster_id].x, cluster[cluster_id].y );
904        _exit();
905    }
906} // end boot_get_sched_vaddr()
907
908#if BOOT_DEBUG_SCHED
909/////////////////////////////////////////////////////////////////////////////
910// This debug function should be executed by only one procesor.
911// It loops on all processors in all clusters to display
912// the HWI / PTI / WTI interrupt vectors for each processor.
913/////////////////////////////////////////////////////////////////////////////
914void boot_sched_irq_display()
915{
916    unsigned int         cx;
917    unsigned int         cy;
918    unsigned int         lpid;
919    unsigned int         slot;
920    unsigned int         entry;
921    unsigned int         type;
922    unsigned int         channel;
923
924    mapping_header_t*    header  = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
925    mapping_cluster_t*   cluster = _get_cluster_base(header);
926
927    static_scheduler_t*  psched; 
928
929    for ( cx = 0 ; cx < X_SIZE ; cx++ )
930    {
931        for ( cy = 0 ; cy < Y_SIZE ; cy++ )
932        {
933            unsigned int cluster_id = (cx * Y_SIZE) + cy;
934            unsigned int nprocs = cluster[cluster_id].procs;
935
936            for ( lpid = 0 ; lpid < nprocs ; lpid++ )
937            {
938                psched = _schedulers[cx][cy][lpid];
939       
940                _printf("\n[BOOT] interrupt vectors for proc[%d,%d,%d]\n",
941                        cx , cy , lpid );
942
943                for ( slot = 0 ; slot < 32 ; slot++ )
944                {
945                    entry   = psched->hwi_vector[slot];
946                    type    = entry & 0xFFFF;
947                    channel = entry >> 16;
948                    if ( type != ISR_DEFAULT )     
949                    _printf(" - HWI : index = %d / type = %s / channel = %d\n",
950                            slot , _isr_type_str[type] , channel );
951                }
952                for ( slot = 0 ; slot < 32 ; slot++ )
953                {
954                    entry   = psched->wti_vector[slot];
955                    type    = entry & 0xFFFF;
956                    channel = entry >> 16;
957                    if ( type != ISR_DEFAULT )     
958                    _printf(" - WTI : index = %d / type = %s / channel = %d\n",
959                            slot , _isr_type_str[type] , channel );
960                }
961                for ( slot = 0 ; slot < 32 ; slot++ )
962                {
963                    entry   = psched->pti_vector[slot];
964                    type    = entry & 0xFFFF;
965                    channel = entry >> 16;
966                    if ( type != ISR_DEFAULT )     
967                    _printf(" - PTI : index = %d / type = %s / channel = %d\n",
968                            slot , _isr_type_str[type] , channel );
969                }
970            }
971        }
972    } 
973}  // end boot_sched_irq_display()
974#endif
975
976
977////////////////////////////////////////////////////////////////////////////////////
978// This function is executed in parallel by all processors P[x][y][0].
979// P[x][y][0] initialises all schedulers in cluster[x][y]. The MMU must be activated.
980// It is split in two phases separated by a synchronisation barrier.
981// - In Step 1, it initialises the _schedulers[x][y][p] pointers array, the
982//              idle_thread context, the  HWI / PTI / WTI interrupt vectors,
983//              and the XCU HWI / PTI / WTI masks.
984// - In Step 2, it scan all threads in all vspaces to complete the threads contexts,
985//              initialisation as specified in the mapping_info data structure,
986//              and set the CP0_SCHED register.
987////////////////////////////////////////////////////////////////////////////////////
988void boot_scheduler_init( unsigned int x, 
989                          unsigned int y )
990{
991    mapping_header_t*    header  = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
992    mapping_cluster_t*   cluster = _get_cluster_base(header);
993    mapping_vspace_t*    vspace  = _get_vspace_base(header);
994    mapping_vseg_t*      vseg    = _get_vseg_base(header);
995    mapping_thread_t*    thread  = _get_thread_base(header);
996    mapping_periph_t*    periph  = _get_periph_base(header);
997    mapping_irq_t*       irq     = _get_irq_base(header);
998
999    unsigned int         periph_id; 
1000    unsigned int         irq_id;
1001    unsigned int         vspace_id;
1002    unsigned int         vseg_id;
1003    unsigned int         thread_id; 
1004
1005    unsigned int         sched_vbase;          // schedulers array vbase address
1006    unsigned int         sched_length;         // schedulers array length
1007    static_scheduler_t*  psched;               // pointer on processor scheduler
1008
1009    unsigned int cluster_id = (x * Y_SIZE) + y;
1010    unsigned int cluster_xy = (x << Y_WIDTH) + y; 
1011    unsigned int nprocs = cluster[cluster_id].procs;
1012    unsigned int lpid;                       
1013   
1014    if ( nprocs > 8 )
1015    {
1016        _printf("\n[BOOT ERROR] cluster[%d,%d] contains more than 8 procs\n", x, y );
1017        _exit();
1018    }
1019
1020    ////////////////////////////////////////////////////////////////////////////////
1021    // Step 1 : - initialize the schedulers[] array of pointers,
1022    //          - initialize the "threads" and "current variables.
1023    //          - initialise the idle_thread context.
1024    //          - initialize the HWI, PTI and WTI interrupt vectors.
1025    //          - initialize the XCU masks for HWI / WTI / PTI interrupts.
1026    //
1027    // The general policy for interrupts routing is the following:         
1028    //          - the local HWI are statically allocatedted to local processors.
1029    //          - the nprocs first PTI are allocated for TICK (one per processor).
1030    //          - we allocate 4 WTI per processor: the first one is for WAKUP,
1031    //            the 3 others WTI are used for external interrupts (from PIC),
1032    //            and are dynamically allocated by kernel on demand.
1033    ///////////////////////////////////////////////////////////////////////////////
1034
1035    // get scheduler array virtual base address in cluster[x,y]
1036    boot_get_sched_vaddr( cluster_id, &sched_vbase, &sched_length );
1037
1038    if ( sched_length < (nprocs<<13) ) // 8 Kbytes per scheduler
1039    {
1040        _printf("\n[BOOT ERROR] Sched segment too small in cluster[%d,%d]\n",
1041                x, y );
1042        _exit();
1043    }
1044
1045    // loop on local processors
1046    for ( lpid = 0 ; lpid < nprocs ; lpid++ )
1047    {
1048        // get scheduler pointer and initialise the schedulers pointers array
1049        psched = (static_scheduler_t*)(sched_vbase + (lpid<<13));
1050        _schedulers[x][y][lpid] = psched;
1051
1052        // initialise the "threads" and "current" variables default values
1053        psched->threads = 0;
1054        psched->current = IDLE_THREAD_INDEX;
1055
1056        // set default values for HWI / PTI / SWI vectors (valid bit = 0)
1057        unsigned int slot;
1058        for (slot = 0; slot < 32; slot++)
1059        {
1060            psched->hwi_vector[slot] = 0;
1061            psched->pti_vector[slot] = 0;
1062            psched->wti_vector[slot] = 0;
1063        }
1064
1065        // initializes the idle_thread context:
1066        // - the SR slot is 0xFF03 because this thread run in kernel mode.
1067        // - it uses the page table of vspace[0]
1068        // - it uses the kernel TTY0 terminal
1069        // - slots containing addresses (SP,RA,EPC) are initialised by kernel_init()
1070        // - It is always executable (NORUN == 0)
1071
1072        psched->context[IDLE_THREAD_INDEX].slot[CTX_CR_ID]    = 0;
1073        psched->context[IDLE_THREAD_INDEX].slot[CTX_SR_ID]    = 0xFF03;
1074        psched->context[IDLE_THREAD_INDEX].slot[CTX_PTPR_ID]  = _ptabs_paddr[0][x][y]>>13;
1075        psched->context[IDLE_THREAD_INDEX].slot[CTX_PTAB_ID]  = _ptabs_vaddr[0][x][y];
1076        psched->context[IDLE_THREAD_INDEX].slot[CTX_TTY_ID]   = 0;
1077        psched->context[IDLE_THREAD_INDEX].slot[CTX_LTID_ID]  = IDLE_THREAD_INDEX;
1078        psched->context[IDLE_THREAD_INDEX].slot[CTX_VSID_ID]  = 0;
1079        psched->context[IDLE_THREAD_INDEX].slot[CTX_NORUN_ID] = 0;
1080        psched->context[IDLE_THREAD_INDEX].slot[CTX_SIGS_ID]  = 0;
1081        psched->context[IDLE_THREAD_INDEX].slot[CTX_LOCKS_ID] = 0;
1082    }
1083
1084    // HWI / PTI / WTI masks (up to 8 local processors)
1085    unsigned int hwi_mask[8] = {0,0,0,0,0,0,0,0};
1086    unsigned int pti_mask[8] = {0,0,0,0,0,0,0,0};
1087    unsigned int wti_mask[8] = {0,0,0,0,0,0,0,0};
1088
1089    // scan local peripherals to get and check local XCU
1090    mapping_periph_t*  xcu = NULL;
1091    unsigned int       min = cluster[cluster_id].periph_offset ;
1092    unsigned int       max = min + cluster[cluster_id].periphs ;
1093
1094    for ( periph_id = min ; periph_id < max ; periph_id++ )
1095    {
1096        if( periph[periph_id].type == PERIPH_TYPE_XCU ) 
1097        {
1098            xcu = &periph[periph_id];
1099
1100            // check nb_hwi_in
1101            if ( xcu->arg0 < xcu->irqs )
1102            {
1103                _printf("\n[BOOT ERROR] Not enough HWI inputs for XCU[%d,%d]"
1104                        " : nb_hwi = %d / nb_irqs = %d\n",
1105                         x , y , xcu->arg0 , xcu->irqs );
1106                _exit();
1107            }
1108            // check nb_pti_in
1109            if ( xcu->arg2 < nprocs )
1110            {
1111                _printf("\n[BOOT ERROR] Not enough PTI inputs for XCU[%d,%d]\n",
1112                         x, y );
1113                _exit();
1114            }
1115            // check nb_wti_in
1116            if ( xcu->arg1 < (4 * nprocs) )
1117            {
1118                _printf("\n[BOOT ERROR] Not enough WTI inputs for XCU[%d,%d]\n",
1119                        x, y );
1120                _exit();
1121            }
1122            // check nb_irq_out
1123            if ( xcu->channels < (nprocs * header->irq_per_proc) )
1124            {
1125                _printf("\n[BOOT ERROR] Not enough outputs for XCU[%d,%d]\n",
1126                        x, y );
1127                _exit();
1128            }
1129        }
1130    } 
1131
1132    if ( xcu == NULL )
1133    {         
1134        _printf("\n[BOOT ERROR] missing XCU in cluster[%d,%d]\n", x , y );
1135        _exit();
1136    }
1137
1138    // HWI interrupt vector definition
1139    // scan HWI connected to local XCU
1140    // for round-robin allocation to local processors
1141    lpid = 0;
1142    for ( irq_id = xcu->irq_offset ;
1143          irq_id < xcu->irq_offset + xcu->irqs ;
1144          irq_id++ )
1145    {
1146        unsigned int type    = irq[irq_id].srctype;
1147        unsigned int srcid   = irq[irq_id].srcid;
1148        unsigned int isr     = irq[irq_id].isr & 0xFFFF;
1149        unsigned int channel = irq[irq_id].channel << 16;
1150
1151        if ( (type != IRQ_TYPE_HWI) || (srcid > 31) )
1152        {
1153            _printf("\n[BOOT ERROR] Bad IRQ in cluster[%d,%d]\n", x, y );
1154            _exit();
1155        }
1156
1157        // register entry in HWI interrupt vector
1158        _schedulers[x][y][lpid]->hwi_vector[srcid] = isr | channel;
1159
1160        // update XCU HWI mask for P[x,y,lpid]
1161        hwi_mask[lpid] = hwi_mask[lpid] | (1<<srcid);
1162
1163        lpid = (lpid + 1) % nprocs; 
1164    } // end for irqs
1165
1166    // PTI interrupt vector definition
1167    // one PTI for TICK per processor
1168    for ( lpid = 0 ; lpid < nprocs ; lpid++ )
1169    {
1170        // register entry in PTI interrupt vector
1171        _schedulers[x][y][lpid]->pti_vector[lpid] = ISR_TICK;
1172
1173        // update XCU PTI mask for P[x,y,lpid]
1174        pti_mask[lpid] = pti_mask[lpid] | (1<<lpid);
1175    }
1176
1177    // WTI interrupt vector definition
1178    // 4 WTI per processor, first for WAKUP
1179    for ( lpid = 0 ; lpid < nprocs ; lpid++ )
1180    {
1181        // register WAKUP ISR in WTI interrupt vector
1182        _schedulers[x][y][lpid]->wti_vector[lpid] = ISR_WAKUP;
1183
1184        // update XCU WTI mask for P[x,y,lpid] (4 entries per proc)
1185        wti_mask[lpid] = wti_mask[lpid] | (0x1<<(lpid                 ));
1186        wti_mask[lpid] = wti_mask[lpid] | (0x1<<(lpid + NB_PROCS_MAX  ));
1187        wti_mask[lpid] = wti_mask[lpid] | (0x1<<(lpid + 2*NB_PROCS_MAX));
1188        wti_mask[lpid] = wti_mask[lpid] | (0x1<<(lpid + 3*NB_PROCS_MAX));
1189    }
1190
1191    // set the XCU masks for HWI / WTI / PTI interrupts
1192    for ( lpid = 0 ; lpid < nprocs ; lpid++ )
1193    {
1194        unsigned int channel = lpid * IRQ_PER_PROCESSOR; 
1195
1196        _xcu_set_mask( cluster_xy, channel, hwi_mask[lpid], IRQ_TYPE_HWI ); 
1197        _xcu_set_mask( cluster_xy, channel, wti_mask[lpid], IRQ_TYPE_WTI );
1198        _xcu_set_mask( cluster_xy, channel, pti_mask[lpid], IRQ_TYPE_PTI );
1199    }
1200
1201    //////////////////////////////////////////////
1202    _simple_barrier_wait( &_barrier_all_clusters );
1203    //////////////////////////////////////////////
1204
1205#if BOOT_DEBUG_SCHED
1206if ( cluster_xy == 0 ) boot_sched_irq_display();
1207_simple_barrier_wait( &_barrier_all_clusters );
1208#endif
1209
1210    ///////////////////////////////////////////////////////////////////////////////
1211    // Step 2 : Initialise the threads context. The context of a thread placed
1212    //          on  processor P must be stored in the scheduler of P.
1213    //          For each vspace, this require two nested loops: loop on the threads,
1214    //          and loop on the local processors in cluster[x,y].
1215    //          We complete the scheduler when the required placement matches
1216    //          the local processor.
1217    ///////////////////////////////////////////////////////////////////////////////
1218
1219    for (vspace_id = 0; vspace_id < header->vspaces; vspace_id++) 
1220    {
1221        // We must set the PTPR depending on the vspace, because the start_vector
1222        // and the stack address are defined in virtual space.
1223        _set_mmu_ptpr( (unsigned int)(_ptabs_paddr[vspace_id][x][y] >> 13) );
1224
1225        // loop on the threads in vspace (thread_id is the global index in mapping)
1226        for (thread_id = vspace[vspace_id].thread_offset;
1227             thread_id < (vspace[vspace_id].thread_offset + vspace[vspace_id].threads);
1228             thread_id++) 
1229        {
1230            // get the required thread placement coordinates [x,y,p]
1231            unsigned int req_x      = cluster[thread[thread_id].clusterid].x;
1232            unsigned int req_y      = cluster[thread[thread_id].clusterid].y;
1233            unsigned int req_p      = thread[thread_id].proclocid;                 
1234
1235            // ctx_norun : two conditions to activate a thread
1236            // - The vspace.active flag is set in the mapping
1237            // - The thread.is_main flag is set in the mapping
1238            unsigned int ctx_norun = (unsigned int)(vspace[vspace_id].active == 0) |
1239                                     (unsigned int)(thread[thread_id].is_main == 0);
1240
1241            // ctx_ptpr : page table physical base address (shifted by 13 bit)
1242            unsigned int ctx_ptpr = (_ptabs_paddr[vspace_id][req_x][req_y] >> 13);
1243
1244            // ctx_ptab : page_table virtual base address
1245            unsigned int ctx_ptab = _ptabs_vaddr[vspace_id][req_x][req_y];
1246
1247            // ctx_entry : Get the virtual address of the memory location containing
1248            // the thread entry point : the start_vector is stored by GCC in the
1249            // seg_data segment, and we must wait the application.elf loading to get
1250            // the entry point value...
1251            vseg_id = vspace[vspace_id].start_vseg_id;     
1252            unsigned int ctx_entry = vseg[vseg_id].vbase + (thread[thread_id].startid)*4;
1253
1254            // ctx_sp :  Get the vseg containing the stack
1255            // allocate 16 slots (64 bytes) for possible arguments.
1256            vseg_id = thread[thread_id].stack_vseg_id;
1257            unsigned int ctx_sp = vseg[vseg_id].vbase + vseg[vseg_id].length - 64;
1258
1259            // loop on the local processors
1260            for ( lpid = 0 ; lpid < nprocs ; lpid++ )
1261            {
1262                if ( (x == req_x) && (y == req_y) && (req_p == lpid) )   // fit
1263                {
1264                    // pointer on selected scheduler
1265                    psched = _schedulers[x][y][lpid];
1266
1267                    // ltid : compute local thread index in scheduler
1268                    unsigned int ltid = psched->threads;
1269
1270                    // update the threads field in scheduler:
1271                    psched->threads   = ltid + 1;
1272
1273                    // ctx_trdid : compute pthread global identifier
1274                    unsigned int ctx_trdid = x << 24 | y<<16 | lpid<<8 | ltid;
1275
1276                    // initializes the thread context
1277                    psched->context[ltid].slot[CTX_CR_ID]     = 0;
1278                    psched->context[ltid].slot[CTX_SR_ID]     = GIET_SR_INIT_VALUE;
1279                    psched->context[ltid].slot[CTX_SP_ID]     = ctx_sp;
1280                    psched->context[ltid].slot[CTX_EPC_ID]    = ctx_entry;
1281                    psched->context[ltid].slot[CTX_ENTRY_ID]  = ctx_entry;
1282                    psched->context[ltid].slot[CTX_PTPR_ID]   = ctx_ptpr;
1283                    psched->context[ltid].slot[CTX_PTAB_ID]   = ctx_ptab;
1284                    psched->context[ltid].slot[CTX_LTID_ID]   = ltid;
1285                    psched->context[ltid].slot[CTX_TRDID_ID]  = ctx_trdid;
1286                    psched->context[ltid].slot[CTX_VSID_ID]   = vspace_id;
1287                    psched->context[ltid].slot[CTX_NORUN_ID]  = ctx_norun;
1288                    psched->context[ltid].slot[CTX_SIGS_ID]   = 0;
1289                    psched->context[ltid].slot[CTX_LOCKS_ID]  = 0;
1290
1291                    psched->context[ltid].slot[CTX_TTY_ID]    = 0xFFFFFFFF;
1292                    psched->context[ltid].slot[CTX_CMA_FB_ID] = 0xFFFFFFFF;
1293                    psched->context[ltid].slot[CTX_CMA_RX_ID] = 0xFFFFFFFF;
1294                    psched->context[ltid].slot[CTX_CMA_TX_ID] = 0xFFFFFFFF;
1295                    psched->context[ltid].slot[CTX_NIC_RX_ID] = 0xFFFFFFFF;
1296                    psched->context[ltid].slot[CTX_NIC_TX_ID] = 0xFFFFFFFF;
1297                    psched->context[ltid].slot[CTX_TIM_ID]    = 0xFFFFFFFF;
1298                    psched->context[ltid].slot[CTX_HBA_ID]    = 0xFFFFFFFF;
1299
1300                    // update thread ltid field in the mapping
1301                    thread[thread_id].ltid = ltid;
1302
1303#if BOOT_DEBUG_SCHED
1304_printf("\nThread %s in vspace %s allocated to P[%d,%d,%d]\n"
1305        " - ctx[LTID]  = %d\n"
1306        " - ctx[TRDID] = %d\n"
1307        " - ctx[SR]    = %x\n"
1308        " - ctx[SP]    = %x\n"
1309        " - ctx[ENTRY] = %x\n"
1310        " - ctx[PTPR]  = %x\n"
1311        " - ctx[PTAB]  = %x\n"
1312        " - ctx[VSID]  = %d\n"
1313        " - ctx[NORUN] = %x\n"
1314        " - ctx[SIG]   = %x\n",
1315        thread[thread_id].name,
1316        vspace[vspace_id].name,
1317        x, y, lpid,
1318        psched->context[ltid].slot[CTX_LTID_ID],
1319        psched->context[ltid].slot[CTX_TRDID_ID],
1320        psched->context[ltid].slot[CTX_SR_ID],
1321        psched->context[ltid].slot[CTX_SP_ID],
1322        psched->context[ltid].slot[CTX_ENTRY_ID],
1323        psched->context[ltid].slot[CTX_PTPR_ID],
1324        psched->context[ltid].slot[CTX_PTAB_ID],
1325        psched->context[ltid].slot[CTX_VSID_ID],
1326        psched->context[ltid].slot[CTX_NORUN_ID],
1327        psched->context[ltid].slot[CTX_SIGS_ID] );
1328#endif
1329                } // end if FIT
1330            } // end for loop on local procs
1331        } // end loop on threads
1332    } // end loop on vspaces
1333} // end boot_scheduler_init()
1334
1335
1336
1337//////////////////////////////////////////////////////////////////////////////////
1338// This function loads the map.bin file from block device.
1339//////////////////////////////////////////////////////////////////////////////////
1340void boot_mapping_init()
1341{
1342
1343#if BOOT_DEBUG_MAPPING
1344_printf("\n[BOOT DEBUG] boot_mapin_init() : enter\n");
1345#endif
1346
1347    // load map.bin file into buffer
1348    if ( _fat_load_no_cache( "map.bin",
1349                             SEG_BOOT_MAPPING_BASE,
1350                             SEG_BOOT_MAPPING_SIZE ) )
1351    {
1352        _printf("\n[BOOT ERROR] : map.bin file not found \n");
1353        _exit();
1354    }
1355
1356    // check mapping signature, number of clusters, number of vspaces 
1357    mapping_header_t * header = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
1358    if ( (header->signature != IN_MAPPING_SIGNATURE) ||
1359         (header->x_size    != X_SIZE)               || 
1360         (header->y_size    != Y_SIZE)               ||
1361         (header->vspaces   > GIET_NB_VSPACE_MAX)    )
1362    {
1363        _printf("\n[BOOT ERROR] Illegal mapping : signature = %x\n", header->signature );
1364        _exit();
1365    }
1366
1367#if BOOT_DEBUG_MAPPING
1368unsigned int  line;
1369unsigned int* pointer = (unsigned int*)SEG_BOOT_MAPPING_BASE;
1370_printf("\n[BOOT] First block of mapping\n");
1371for ( line = 0 ; line < 8 ; line++ )
1372{
1373    _printf(" | %X | %X | %X | %X | %X | %X | %X | %X |\n",
1374            *(pointer + 0),
1375            *(pointer + 1),
1376            *(pointer + 2),
1377            *(pointer + 3),
1378            *(pointer + 4),
1379            *(pointer + 5),
1380            *(pointer + 6),
1381            *(pointer + 7) );
1382
1383    pointer = pointer + 8;
1384}
1385#endif
1386
1387} // end boot_mapping_init()
1388
1389
1390///////////////////////////////////////////////////
1391void boot_dma_copy( unsigned int        cluster_xy,     
1392                    unsigned long long  dst_paddr,
1393                    unsigned long long  src_paddr, 
1394                    unsigned int        size )   
1395{
1396    // size must be multiple of 64 bytes
1397    if ( size & 0x3F ) size = (size & (~0x3F)) + 0x40;
1398
1399    unsigned int mode = MODE_DMA_NO_IRQ;
1400
1401    unsigned int src     = 0;
1402    unsigned int src_lsb = (unsigned int)src_paddr;
1403    unsigned int src_msb = (unsigned int)(src_paddr>>32);
1404   
1405    unsigned int dst     = 1;
1406    unsigned int dst_lsb = (unsigned int)dst_paddr;
1407    unsigned int dst_msb = (unsigned int)(dst_paddr>>32);
1408
1409    // initializes src channel
1410    _mwr_set_channel_register( cluster_xy , src , MWR_CHANNEL_MODE       , mode );
1411    _mwr_set_channel_register( cluster_xy , src , MWR_CHANNEL_SIZE       , size );
1412    _mwr_set_channel_register( cluster_xy , src , MWR_CHANNEL_BUFFER_LSB , src_lsb );
1413    _mwr_set_channel_register( cluster_xy , src , MWR_CHANNEL_BUFFER_MSB , src_msb );
1414    _mwr_set_channel_register( cluster_xy , src , MWR_CHANNEL_RUNNING    , 1 );
1415
1416    // initializes dst channel
1417    _mwr_set_channel_register( cluster_xy , dst , MWR_CHANNEL_MODE       , mode );
1418    _mwr_set_channel_register( cluster_xy , dst , MWR_CHANNEL_SIZE       , size );
1419    _mwr_set_channel_register( cluster_xy , dst , MWR_CHANNEL_BUFFER_LSB , dst_lsb );
1420    _mwr_set_channel_register( cluster_xy , dst , MWR_CHANNEL_BUFFER_MSB , dst_msb );
1421    _mwr_set_channel_register( cluster_xy , dst , MWR_CHANNEL_RUNNING    , 1 );
1422
1423    // start CPY coprocessor (write non-zero value into config register)
1424    _mwr_set_coproc_register( cluster_xy, 0 , 1 );
1425
1426    // poll dst channel status register to detect completion
1427    unsigned int status;
1428    do
1429    {
1430        status = _mwr_get_channel_register( cluster_xy , dst , MWR_CHANNEL_STATUS );
1431    } while ( status == MWR_CHANNEL_BUSY );
1432
1433    if ( status )
1434    {
1435        _printf("\n[BOOT ERROR] in boot_dma_copy()\n");
1436        _exit();
1437    } 
1438 
1439    // stop CPY coprocessor and DMA channels
1440    _mwr_set_channel_register( cluster_xy , src , MWR_CHANNEL_RUNNING    , 0 );
1441    _mwr_set_channel_register( cluster_xy , dst , MWR_CHANNEL_RUNNING    , 0 );
1442    _mwr_set_coproc_register ( cluster_xy , 0 , 0 );
1443
1444}  // end boot_dma_copy()
1445
1446//////////////////////////////////////////////////////////////////////////////////
1447// This function load all loadable segments contained in the .elf file identified
1448// by the "pathname" argument. Some loadable segments can be copied in several
1449// clusters: same virtual address but different physical addresses. 
1450// - It open the file.
1451// - It loads the complete file in the dedicated _boot_elf_buffer.
1452// - It copies each loadable segments  at the virtual address defined in
1453//   the .elf file, making several copies if the target vseg is not local.
1454// - It closes the file.
1455// This function is supposed to be executed by all processors[x,y,0].
1456//
1457// Note: We must use physical addresses to reach the destination buffers that
1458// can be located in remote clusters. We use either a _physical_memcpy(),
1459// or a _dma_physical_copy() if DMA is available.
1460//////////////////////////////////////////////////////////////////////////////////
1461void load_one_elf_file( unsigned int is_kernel,     // kernel file if non zero
1462                        char*        pathname,
1463                        unsigned int vspace_id )    // to scan the proper vspace
1464{
1465    mapping_header_t  * header  = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
1466    mapping_vspace_t  * vspace  = _get_vspace_base(header);
1467    mapping_vseg_t    * vseg    = _get_vseg_base(header);
1468
1469    unsigned int procid = _get_procid();
1470    unsigned int cxy    = procid >> P_WIDTH;
1471    unsigned int x      = cxy >> Y_WIDTH;
1472    unsigned int y      = cxy & ((1<<Y_WIDTH)-1);
1473    unsigned int p      = procid & ((1<<P_WIDTH)-1);
1474
1475#if BOOT_DEBUG_ELF
1476_printf("\n[DEBUG BOOT_ELF] load_one_elf_file() : P[%d,%d,%d] enters for %s\n",
1477        x , y , p , pathname );
1478#endif
1479
1480    Elf32_Ehdr* elf_header_ptr = NULL;  //  avoid a warning
1481
1482    // only P[0,0,0] load file
1483    if ( (cxy == 0) && (p == 0) )
1484    {
1485        if ( _fat_load_no_cache( pathname,
1486                                 (unsigned int)_boot_elf_buffer,
1487                                 GIET_ELF_BUFFER_SIZE ) )
1488        {
1489            _printf("\n[BOOT ERROR] in load_one_elf_file() : %s\n", pathname );
1490            _exit();
1491        }
1492
1493        // Check ELF Magic Number in ELF header
1494        Elf32_Ehdr* ptr = (Elf32_Ehdr*)_boot_elf_buffer;
1495
1496        if ( (ptr->e_ident[EI_MAG0] != ELFMAG0) ||
1497             (ptr->e_ident[EI_MAG1] != ELFMAG1) ||
1498             (ptr->e_ident[EI_MAG2] != ELFMAG2) ||
1499             (ptr->e_ident[EI_MAG3] != ELFMAG3) )
1500        {
1501            _printf("\n[BOOT ERROR] load_one_elf_file() : %s not ELF format\n",
1502                    pathname );
1503            _exit();
1504        }
1505
1506#if BOOT_DEBUG_ELF
1507_printf("\n[DEBUG BOOT_ELF] load_one_elf_file() : P[%d,%d,%d] load %s at cycle %d\n", 
1508        x , y , p , pathname , _get_proctime() );
1509#endif
1510
1511    } // end if P[0,0,0]
1512
1513    //////////////////////////////////////////////
1514    _simple_barrier_wait( &_barrier_all_clusters );
1515    //////////////////////////////////////////////
1516
1517    // Each processor P[x,y,0] copy replicated segments in cluster[x,y]
1518    elf_header_ptr = (Elf32_Ehdr*)_boot_elf_buffer;
1519
1520    // get program header table pointer
1521    unsigned int offset = elf_header_ptr->e_phoff;
1522    if( offset == 0 )
1523    {
1524        _printf("\n[BOOT ERROR] load_one_elf_file() : file %s "
1525                "does not contain loadable segment\n", pathname );
1526        _exit();
1527    }
1528
1529    Elf32_Phdr* elf_pht_ptr = (Elf32_Phdr*)(_boot_elf_buffer + offset);
1530
1531    // get number of segments
1532    unsigned int nsegments   = elf_header_ptr->e_phnum;
1533
1534    // First loop on loadable segments in the .elf file
1535    unsigned int seg_id;
1536    for (seg_id = 0 ; seg_id < nsegments ; seg_id++)
1537    {
1538        if(elf_pht_ptr[seg_id].p_type == PT_LOAD)
1539        {
1540            // Get segment attributes
1541            unsigned int seg_vaddr  = elf_pht_ptr[seg_id].p_vaddr;
1542            unsigned int seg_offset = elf_pht_ptr[seg_id].p_offset;
1543            unsigned int seg_filesz = elf_pht_ptr[seg_id].p_filesz;
1544            unsigned int seg_memsz  = elf_pht_ptr[seg_id].p_memsz;
1545
1546            if( seg_memsz != seg_filesz )
1547            {
1548                _printf("\n[BOOT ERROR] load_one_elf_file() : segment at vaddr = %x\n"
1549                        " in file %s has memsize = %x / filesize = %x \n"
1550                        " check that all global variables are in data segment\n", 
1551                        seg_vaddr, pathname , seg_memsz , seg_filesz );
1552                 _exit();
1553            }
1554
1555            unsigned int src_vaddr = (unsigned int)_boot_elf_buffer + seg_offset;
1556
1557            // search all vsegs matching the virtual address
1558            unsigned int vseg_first;
1559            unsigned int vseg_last;
1560            unsigned int vseg_id;
1561            unsigned int found = 0;
1562            if ( is_kernel )
1563            {
1564                vseg_first = 0;
1565                vseg_last  = header->globals;
1566            }
1567            else
1568            {
1569                vseg_first = vspace[vspace_id].vseg_offset;
1570                vseg_last  = vseg_first + vspace[vspace_id].vsegs;
1571            }
1572
1573            // Second loop on vsegs in the mapping
1574            for ( vseg_id = vseg_first ; vseg_id < vseg_last ; vseg_id++ )
1575            {
1576                if ( seg_vaddr == vseg[vseg_id].vbase )  // matching
1577                {
1578                    found = 1;
1579
1580                    // get destination buffer physical address, size, coordinates
1581                    paddr_t      seg_paddr  = vseg[vseg_id].pbase;
1582                    unsigned int seg_size   = vseg[vseg_id].length;
1583                    unsigned int cluster_xy = (unsigned int)(seg_paddr>>32);
1584                    unsigned int cx         = cluster_xy >> Y_WIDTH;
1585                    unsigned int cy         = cluster_xy & ((1<<Y_WIDTH)-1);
1586
1587                    // check vseg size
1588                    if ( seg_size < seg_filesz )
1589                    {
1590                        _printf("\n[BOOT ERROR] in load_one_elf_file() : vseg %s "
1591                                "is too small for segment %x\n"
1592                                "  file = %s / vseg_size = %x / seg_file_size = %x\n",
1593                                vseg[vseg_id].name , seg_vaddr , pathname,
1594                                seg_size , seg_filesz );
1595                        _exit();
1596                    }
1597
1598                    // P[x,y,0] copy the segment from boot buffer in cluster[0,0]
1599                    // to destination buffer in cluster[x,y], using DMA if available
1600                    if ( (cx == x) && (cy == y) )
1601                    {
1602                        if( USE_MWR_CPY )
1603                        {
1604                            boot_dma_copy( cluster_xy,  // DMA in cluster[x,y]       
1605                                           seg_paddr,
1606                                           (paddr_t)src_vaddr, 
1607                                           seg_filesz );   
1608#if BOOT_DEBUG_ELF
1609_printf("\n[DEBUG BOOT_ELF] load_one_elf_file() : DMA[%d,%d] copy segment %d :\n"
1610        "  vaddr = %x / size = %x / paddr = %l\n",
1611        x , y , seg_id , seg_vaddr , seg_memsz , seg_paddr );
1612#endif
1613                        }
1614                        else
1615                        {
1616                            _physical_memcpy( seg_paddr,            // dest paddr
1617                                              (paddr_t)src_vaddr,   // source paddr
1618                                              seg_filesz );         // size
1619#if BOOT_DEBUG_ELF
1620_printf("\n[DEBUG BOOT_ELF] load_one_elf_file() : P[%d,%d,%d] copy segment %d :\n"
1621        "  vaddr = %x / size = %x / paddr = %l\n",
1622        x , y , p , seg_id , seg_vaddr , seg_memsz , seg_paddr );
1623#endif
1624                        }
1625                    }
1626                }
1627            }  // end for vsegs
1628
1629            // check at least one matching vseg
1630            if ( found == 0 )
1631            {
1632                _printf("\n[BOOT ERROR] in load_one_elf_file() : vseg for loadable "
1633                        "segment %x in file %s not found "
1634                        "check consistency between the .py and .ld files\n",
1635                        seg_vaddr, pathname );
1636                _exit();
1637            }
1638        }
1639    }  // end for loadable segments
1640
1641    //////////////////////////////////////////////
1642    _simple_barrier_wait( &_barrier_all_clusters );
1643    //////////////////////////////////////////////
1644
1645    // only P[0,0,0] signals completion
1646    if ( (cxy == 0) && (p == 0) )
1647    {
1648        _printf("\n[BOOT] File %s loaded at cycle %d\n", 
1649                pathname , _get_proctime() );
1650    }
1651
1652} // end load_one_elf_file()
1653
1654
1655/////i////////////////////////////////////////////////////////////////////////////////
1656// This function uses the map.bin data structure to load the "kernel.elf" file
1657// as well as the various "application.elf" files into memory.
1658// - The "preloader.elf" file is not loaded, because it has been burned in the ROM.
1659// - The "boot.elf" file is not loaded, because it has been loaded by the preloader.
1660// This function scans all vsegs defined in the map.bin data structure to collect
1661// all .elf files pathnames, and calls the load_one_elf_file() for each .elf file.
1662// As the code can be replicated in several vsegs, the same code can be copied
1663// in one or several clusters by the load_one_elf_file() function.
1664//////////////////////////////////////////////////////////////////////////////////////
1665void boot_elf_load()
1666{
1667    mapping_header_t* header = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
1668    mapping_vspace_t* vspace = _get_vspace_base( header );
1669    mapping_vseg_t*   vseg   = _get_vseg_base( header );
1670
1671    unsigned int      vspace_id;
1672    unsigned int      vseg_id;
1673    unsigned int      found;
1674
1675    // Scan all global vsegs to find the pathname to the kernel.elf file
1676    found = 0;
1677    for( vseg_id = 0 ; vseg_id < header->globals ; vseg_id++ )
1678    {
1679        if(vseg[vseg_id].type == VSEG_TYPE_ELF) 
1680        {   
1681            found = 1;
1682            break;
1683        }
1684    }
1685
1686    // We need one kernel.elf file
1687    if (found == 0)
1688    {
1689        _printf("\n[BOOT ERROR] boot_elf_load() : kernel.elf file not found\n");
1690        _exit();
1691    }
1692
1693    // Load the kernel
1694    load_one_elf_file( 1,                           // kernel file
1695                       vseg[vseg_id].binpath,       // file pathname
1696                       0 );                         // vspace 0
1697
1698    // loop on the vspaces, scanning all vsegs in the vspace,
1699    // to find the pathname of the .elf file associated to the vspace.
1700    for( vspace_id = 0 ; vspace_id < header->vspaces ; vspace_id++ )
1701    {
1702        // loop on the private vsegs
1703        unsigned int found = 0;
1704        for (vseg_id = vspace[vspace_id].vseg_offset;
1705             vseg_id < (vspace[vspace_id].vseg_offset + vspace[vspace_id].vsegs);
1706             vseg_id++) 
1707        {
1708            if(vseg[vseg_id].type == VSEG_TYPE_ELF) 
1709            {   
1710                found = 1;
1711                break;
1712            }
1713        }
1714
1715        // We want one .elf file per vspace
1716        if (found == 0)
1717        {
1718            _printf("\n[BOOT ERROR] boot_elf_load() : "
1719                    ".elf file not found for vspace %s\n", vspace[vspace_id].name );
1720            _exit();
1721        }
1722
1723        load_one_elf_file( 0,                          // not a kernel file
1724                           vseg[vseg_id].binpath,      // file pathname
1725                           vspace_id );                // vspace index
1726
1727    }  // end for vspaces
1728
1729} // end boot_elf_load()
1730
1731
1732/////////////////////////////////////////////////////////////////////////////////
1733// This function is executed in parallel by all processors[x][y][0].
1734// It initialises the physical memory allocator in each cluster containing
1735// a RAM pseg.
1736/////////////////////////////////////////////////////////////////////////////////
1737void boot_pmem_init( unsigned int cx,
1738                     unsigned int cy ) 
1739{
1740    mapping_header_t*  header     = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
1741    mapping_cluster_t* cluster    = _get_cluster_base(header);
1742    mapping_pseg_t*    pseg       = _get_pseg_base(header);
1743
1744    unsigned int pseg_id;
1745    unsigned int procid     = _get_procid();
1746    unsigned int lpid       = procid & ((1<<P_WIDTH)-1);
1747
1748    if( lpid )
1749    {
1750        _printf("\n[BOOT ERROR] boot_pmem_init() : "
1751        "P[%d][%d][%d] should not execute it\n", cx, cy, lpid );
1752        _exit();
1753    }   
1754
1755    // scan the psegs in local cluster to find  pseg of type RAM
1756    unsigned int found      = 0;
1757    unsigned int cluster_id = cx * Y_SIZE + cy;
1758    unsigned int pseg_min   = cluster[cluster_id].pseg_offset;
1759    unsigned int pseg_max   = pseg_min + cluster[cluster_id].psegs;
1760    for ( pseg_id = pseg_min ; pseg_id < pseg_max ; pseg_id++ )
1761    {
1762        if ( pseg[pseg_id].type == PSEG_TYPE_RAM )
1763        {
1764            unsigned int base = (unsigned int)pseg[pseg_id].base;
1765            unsigned int size = (unsigned int)pseg[pseg_id].length;
1766            _pmem_alloc_init( cx, cy, base, size );
1767            found = 1;
1768
1769#if BOOT_DEBUG_PT
1770_printf("\n[BOOT] pmem allocator initialised in cluster[%d][%d]"
1771        " : base = %x / size = %x\n", cx , cy , base , size );
1772#endif
1773            break;
1774        }
1775    }
1776
1777    if ( found == 0 )
1778    {
1779        _printf("\n[BOOT ERROR] boot_pmem_init() : no RAM in cluster[%d][%d]\n",
1780                cx , cy );
1781        _exit();
1782    }   
1783} // end boot_pmem_init()
1784 
1785/////////////////////////////////////////////////////////////////////////
1786// This function is the entry point of the boot code for all processors.
1787/////////////////////////////////////////////////////////////////////////
1788void boot_init() 
1789{
1790
1791    unsigned int       gpid       = _get_procid();
1792    unsigned int       cx         = gpid >> (Y_WIDTH + P_WIDTH);
1793    unsigned int       cy         = (gpid >> P_WIDTH) & ((1<<Y_WIDTH)-1);
1794    unsigned int       lpid       = gpid & ((1 << P_WIDTH) -1);
1795
1796    //////////////////////////////////////////////////////////
1797    // Phase ONE : only P[0][0][0] execute it
1798    //////////////////////////////////////////////////////////
1799    if ( gpid == 0 )   
1800    {
1801        unsigned int cid;  // index for loop on clusters
1802
1803        // initialises the TTY0 spin lock
1804        _spin_lock_init( &_tty0_spin_lock );
1805
1806        _printf("\n[BOOT] P[0,0,0] starts at cycle %d\n", _get_proctime() );
1807
1808        // initialise the MMC locks array
1809        _mmc_boot_mode = 1;
1810        _mmc_init_locks();
1811
1812        // initialises the IOC peripheral
1813        if      ( USE_IOC_BDV != 0 ) _bdv_init();
1814        else if ( USE_IOC_HBA != 0 ) _hba_init();
1815        else if ( USE_IOC_SDC != 0 ) _sdc_init();
1816        else if ( USE_IOC_RDK == 0 )
1817        {
1818            _printf("\n[BOOT ERROR] boot_init() : no IOC peripheral\n");
1819            _exit();
1820        }
1821
1822        // initialises the FAT
1823        _fat_init( 0 );          // don't use Inode-Tree, Fat-Cache, etc.
1824
1825        _printf("\n[BOOT] FAT initialised at cycle %d\n", _get_proctime() );
1826
1827        // Load the map.bin file into memory
1828        boot_mapping_init();
1829
1830        mapping_header_t*  header     = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
1831        mapping_cluster_t* cluster    = _get_cluster_base(header);
1832
1833        _printf("\n[BOOT] Mapping %s loaded at cycle %d\n",
1834                header->name , _get_proctime() );
1835
1836        // initialises the barrier for all clusters containing processors
1837        unsigned int nclusters = 0;
1838        for ( cid = 0 ; cid < X_SIZE*Y_SIZE ; cid++ )
1839        {
1840            if ( cluster[cid].procs ) nclusters++ ;
1841        } 
1842
1843        _simple_barrier_init( &_barrier_all_clusters , nclusters );
1844
1845        // wake up all processors P[x][y][0]
1846        for ( cid = 1 ; cid < X_SIZE*Y_SIZE ; cid++ ) 
1847        {
1848            unsigned int x          = cluster[cid].x;
1849            unsigned int y          = cluster[cid].y;
1850            unsigned int cluster_xy = (x << Y_WIDTH) + y;
1851
1852            if ( cluster[cid].procs ) 
1853            {
1854                unsigned long long paddr = (((unsigned long long)cluster_xy)<<32) +
1855                                           SEG_XCU_BASE+XCU_REG( XCU_WTI_REG , 0 );
1856
1857                _physical_write( paddr , (unsigned int)boot_entry );
1858            }
1859        }
1860
1861        _printf("\n[BOOT] Processors P[x,y,0] start at cycle %d\n",
1862                _get_proctime() );
1863    }
1864
1865    /////////////////////////////////////////////////////////////////
1866    // Phase TWO : All processors P[x][y][0] execute it in parallel
1867    /////////////////////////////////////////////////////////////////
1868    if( lpid == 0 )
1869    {
1870        // Initializes physical memory allocator in cluster[cx][cy]
1871        boot_pmem_init( cx , cy );
1872
1873        // Build page table in cluster[cx][cy]
1874        boot_ptab_init( cx , cy );
1875
1876        //////////////////////////////////////////////
1877        _simple_barrier_wait( &_barrier_all_clusters );
1878        //////////////////////////////////////////////
1879
1880        // P[0][0][0] complete page tables with vsegs
1881        // mapped in clusters without processors
1882        if ( gpid == 0 )   
1883        {
1884            // complete page tables initialisation
1885            boot_ptab_extend();
1886
1887            _printf("\n[BOOT] Page tables"
1888                    " initialized at cycle %d\n", _get_proctime() );
1889        }
1890
1891        //////////////////////////////////////////////
1892        _simple_barrier_wait( &_barrier_all_clusters );
1893        //////////////////////////////////////////////
1894
1895        // All processors P[x,y,0] activate MMU (using local PTAB)
1896        _set_mmu_ptpr( (unsigned int)(_ptabs_paddr[0][cx][cy]>>13) );
1897        _set_mmu_mode( 0xF );
1898       
1899        // Each processor P[x,y,0] initialises all schedulers in cluster[x,y]
1900        boot_scheduler_init( cx , cy );
1901
1902        // Each processor P[x][y][0] initialises its CP0_SCHED register
1903        _set_sched( (unsigned int)_schedulers[cx][cy][0] );
1904
1905        //////////////////////////////////////////////
1906        _simple_barrier_wait( &_barrier_all_clusters );
1907        //////////////////////////////////////////////
1908
1909        if ( gpid == 0 ) 
1910        {
1911            _printf("\n[BOOT] Schedulers initialised at cycle %d\n", 
1912                    _get_proctime() );
1913        }
1914
1915        // All processor P[x,y,0] contributes to load .elf files into clusters.
1916        boot_elf_load();
1917
1918        //////////////////////////////////////////////
1919        _simple_barrier_wait( &_barrier_all_clusters );
1920        //////////////////////////////////////////////
1921       
1922        // Each processor P[x][y][0] wake up other processors in same cluster
1923        mapping_header_t*  header     = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
1924        mapping_cluster_t* cluster    = _get_cluster_base(header);
1925        unsigned int       cluster_xy = (cx << Y_WIDTH) + cy;
1926        unsigned int       cluster_id = (cx * Y_SIZE) + cy;
1927        unsigned int p;
1928        for ( p = 1 ; p < cluster[cluster_id].procs ; p++ )
1929        {
1930            _xcu_send_wti( cluster_xy , p , (unsigned int)boot_entry );
1931        }
1932
1933        // only P[0][0][0] makes display
1934        if ( gpid == 0 )
1935        {   
1936            _printf("\n[BOOT] All processors start at cycle %d\n",
1937                    _get_proctime() );
1938        }
1939    }
1940    // All other processors activate MMU (using local PTAB)
1941    if ( lpid != 0 )
1942    {
1943        _set_mmu_ptpr( (unsigned int)(_ptabs_paddr[0][cx][cy]>>13) );
1944        _set_mmu_mode( 0xF );
1945    }
1946
1947    // All processors set CP0_SCHED register
1948    _set_sched( (unsigned int)_schedulers[cx][cy][lpid] );
1949
1950    // All processors reset BEV bit in SR to use GIET_VM exception handler
1951    _set_sr( 0 );
1952
1953    // Each processor get kernel entry virtual address
1954    unsigned int kernel_entry = 0x80000000;
1955
1956#if BOOT_DEBUG_ELF
1957_printf("\n[DEBUG BOOT_ELF] P[%d,%d,%d] exit boot & jump to %x at cycle %d\n",
1958        cx, cy, lpid, kernel_entry , _get_proctime() );
1959#endif
1960
1961    // All processors jump to kernel_init
1962    asm volatile( "jr   %0" ::"r"(kernel_entry) );
1963
1964} // end boot_init()
1965
1966
1967// Local Variables:
1968// tab-width: 4
1969// c-basic-offset: 4
1970// c-file-offsets:((innamespace . 0)(inline-open . 0))
1971// indent-tabs-mode: nil
1972// End:
1973// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
1974
Note: See TracBrowser for help on using the repository browser.