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

Last change on this file since 722 was 716, checked in by alain, 9 years ago

Cosmetic.

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