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

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

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

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