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

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

The boot code use now the _v2p_add_pte1() and _v2p_add_pte2() functions,
defined in the common/vmem.c directory, because thes functions are also
used by the kernel to implement the _sys_fat_mmap() function.

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