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

Last change on this file since 602 was 590, checked in by alain, 9 years ago

Introduce support for the new POSIX-like FAT32 library.

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