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

Last change on this file since 710 was 709, checked in by alain, 9 years ago

Major release: Change the task model to implement the POSIX threads API.

  • The shell "exec" and "kill" commands can be used to activate/de-activate the applications.
  • The "pause", "resume", and "context" commands can be used to stop, restart, a single thtead or to display the thread context.

This version has been tested on the following multi-threaded applications,
that have been modified to use the POSIX threads:

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