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

Last change on this file since 819 was 819, checked in by cfuguet, 8 years ago

Add a preprocessor flag which allows to inhibit the use of the
GLOBAL bit in page table entries.

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