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

Last change on this file since 427 was 427, checked in by alain, 10 years ago

Introducing fixed format (X_WIDTH / Y_WIDTH / P_WIDTH) for processor index.

File size: 91.8 KB
Line 
1/////////////////////////////////////////////////////////////////////////////////////////
2// File     : boot.c
3// Date     : 01/11/2013
4// Author   : alain greiner
5// Copyright (c) UPMC-LIP6
6//////////////////////////////////////////////////////////////////////////////////////////
7// The boot.c file is part of the GIET-VM nano-kernel.
8//
9// This nano-kernel has been written for the MIPS32 processor.
10// The virtual adresses are on 32 bits and use the (unsigned int) type. The
11// physicals addresses can have up to 40 bits, and use the  (unsigned long long) type.
12// It natively supports clusterised shared memory multi-processors architectures,
13// where each processor is identified by a composite index (cluster_xy, local_id),
14// and where there is one physical memory bank per cluster.
15//
16// This code, executed in the boot phase by proc[0,0,0], performs the following tasks:
17// - load into memory various binary files, from a FAT32 file system,
18// - build the various page tables (one page table per vspace)
19// - initialize the shedulers (one scheduler per processor)
20//
21// 1) The binary files to be loaded are:
22//    - the "map.bin" file contains the hardware architecture description and the
23//      mapping directives. It must be stored in the the seg_boot_mapping segment
24//      (at address SEG_BOOT_MAPPING_BASE defined in hard_config.h file).
25//    - the "sys.elf" file contains the kernel binary code and data.
26//    - the various "application.elf" files.
27//
28// 2) The map.bin file contains the binary representation of the map.xml file defining:
29//    - the hardware architecture: number of clusters, number or processors,
30//      size of the memory segments, and peripherals in each cluster.
31//    - The structure of the various multi-threaded software applications:
32//      number of tasks, communication channels.
33//    - The mapping: grouping of virtual objects (vobj) in the virtual segments (vseg),
34//      placement of virtual segments (vseg) in the physical segments (pseg), placement
35//      of software tasks on the processors,
36//
37// 3) The GIET-VM uses the paged virtual memory to provides two services:
38//    - classical memory protection, when several independant applications compiled
39//      in different virtual spaces are executing on the same hardware platform.
40//    - data placement in NUMA architectures, to control the placement
41//      of the software objects (vsegs) on the physical memory banks (psegs).
42//
43//    The max number of vspaces (GIET_NB_VSPACE_MAX) is a configuration parameter.
44//    The page table are statically build in the boot phase, and they do not
45//    change during execution.
46//    The GIET_VM uses both small pages (4 Kbytes), and big pages (2 Mbytes).
47//
48//    Each page table (one page table per virtual space) is monolithic, and contains
49//    one PT1 (8 Kbytes) and a variable number of PT2s (4 Kbytes each). For each vspace,
50//    the numberof PT2s is defined by the size of the PTAB vobj in the mapping.
51//    The PT1 is indexed by the ix1 field (11 bits) of the VPN. Each entry is 32 bits.
52//    A PT2 is indexed the ix2 field (9 bits) of the VPN. Each entry is a double word.
53//    The first word contains the flags, the second word contains the PPN.
54//
55//    The page tables can be distributed in all clusters.
56///////////////////////////////////////////////////////////////////////////////////////
57// Implementation Notes:
58//
59// 1) The cluster_id variable is a linear index in the mapping_info array of clusters.
60//    We use the cluster_xy variable for the tological index = x << Y_WIDTH + y
61//
62///////////////////////////////////////////////////////////////////////////////////////
63
64#include <giet_config.h>
65#include <mwmr_channel.h>
66#include <barrier.h>
67#include <memspace.h>
68#include <tty_driver.h>
69#include <xcu_driver.h>
70#include <bdv_driver.h>
71#include <dma_driver.h>
72#include <cma_driver.h>
73#include <nic_driver.h>
74#include <ioc_driver.h>
75#include <iob_driver.h>
76#include <pic_driver.h>
77#include <mwr_driver.h>
78#include <ctx_handler.h>
79#include <irq_handler.h>
80#include <vmem.h>
81#include <pmem.h>
82#include <utils.h>
83#include <elf-types.h>
84
85// for boot FAT initialisation
86#include <fat32.h>
87
88#include <mips32_registers.h>
89#include <stdarg.h>
90
91#if !defined(X_SIZE)
92# error: The X_SIZE value must be defined in the 'hard_config.h' file !
93#endif
94
95#if !defined(Y_SIZE)
96# error: The Y_SIZE value must be defined in the 'hard_config.h' file !
97#endif
98
99#if !defined(X_WIDTH)
100# error: The X_WIDTH value must be defined in the 'hard_config.h' file !
101#endif
102
103#if !defined(Y_WIDTH)
104# error: The Y_WIDTH value must be defined in the 'hard_config.h' file !
105#endif
106
107#if !defined(SEG_BOOT_MAPPING_BASE)
108# error: The SEG_BOOT_MAPPING_BASE value must be defined in the hard_config.h file !
109#endif
110
111#if !defined(NB_PROCS_MAX)
112# error: The NB_PROCS_MAX value must be defined in the 'hard_config.h' file !
113#endif
114
115#if !defined(GIET_NB_VSPACE_MAX)
116# error: The GIET_NB_VSPACE_MAX value must be defined in the 'giet_config.h' file !
117#endif
118
119#if !defined(GIET_ELF_BUFFER_SIZE)
120# error: The GIET_ELF_BUFFER_SIZE value must be defined in the giet_config.h file !
121#endif
122
123////////////////////////////////////////////////////////////////////////////
124//      Global variables for boot code
125////////////////////////////////////////////////////////////////////////////
126
127extern void boot_entry();
128
129// FAT internal representation for boot code 
130__attribute__((section (".bootdata"))) 
131fat32_fs_t             fat   __attribute__((aligned(512)));
132
133// Temporaty buffer used to load one complete .elf file 
134__attribute__((section (".bootdata"))) 
135char                   boot_elf_buffer[GIET_ELF_BUFFER_SIZE] __attribute__((aligned(512)));
136
137// Physical memory allocators array (one per cluster)
138__attribute__((section (".bootdata"))) 
139pmem_alloc_t           boot_pmem_alloc[X_SIZE][Y_SIZE];
140
141// Schedulers virtual base addresses array (one per processor)
142__attribute__((section (".bootdata"))) 
143static_scheduler_t*    _schedulers[X_SIZE][Y_SIZE][NB_PROCS_MAX];
144
145// Page tables virtual base addresses array (one per vspace)
146__attribute__((section (".bootdata"))) 
147unsigned int           _ptabs_vaddr[GIET_NB_VSPACE_MAX][X_SIZE][Y_SIZE];
148
149// Page tables physical base addresses (one per vspace and per cluster)
150__attribute__((section (".bootdata"))) 
151paddr_t                _ptabs_paddr[GIET_NB_VSPACE_MAX][X_SIZE][Y_SIZE];
152
153// Page tables pt2 allocators (one per vspace and per cluster)
154__attribute__((section (".bootdata"))) 
155unsigned int           _ptabs_next_pt2[GIET_NB_VSPACE_MAX][X_SIZE][Y_SIZE];
156
157// Page tables max_pt2  (same value for all page tables)
158__attribute__((section (".bootdata"))) 
159unsigned int           _ptabs_max_pt2;
160
161/////////////////////////////////////////////////////////////////////
162// This function checks consistence beween the  mapping_info data
163// structure (soft), and the giet_config file (hard).
164/////////////////////////////////////////////////////////////////////
165void boot_mapping_check() 
166{
167    mapping_header_t * header = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
168
169    // checking mapping availability
170    if (header->signature != IN_MAPPING_SIGNATURE) 
171    {
172        _puts("\n[BOOT ERROR] Illegal mapping signature: ");
173        _putx(header->signature);
174        _puts("\n");
175        _exit();
176    }
177
178    // checking number of clusters
179    if ( (header->x_size  != X_SIZE)  || 
180         (header->y_size  != Y_SIZE)  ||
181         (header->x_width != X_WIDTH) ||
182         (header->y_width != Y_WIDTH) )
183    {
184        _puts("\n[BOOT ERROR] Incoherent X_SIZE or Y_SIZE ");
185        _puts("\n             - In hard_config:  X_SIZE = ");
186        _putd( X_SIZE );
187        _puts(" / Y_SIZE = ");
188        _putd( Y_SIZE );
189        _puts(" / X_WIDTH = ");
190        _putd( X_WIDTH );
191        _puts(" / Y_WIDTH = ");
192        _putd( Y_WIDTH );
193        _puts("\n             - In mapping_info: x_size = ");
194        _putd( header->x_size );
195        _puts(" / y_size = ");
196        _putd( header->y_size );
197        _puts(" / x_width = ");
198        _putd( header->x_width );
199        _puts(" / y_width = ");
200        _putd( header->y_width );
201        _puts("\n");
202        _exit();
203    }
204    // checking number of virtual spaces
205    if (header->vspaces > GIET_NB_VSPACE_MAX) 
206    {
207        _puts("\n[BOOT ERROR] : number of vspaces > GIET_NB_VSPACE_MAX\n");
208        _puts("\n");
209        _exit();
210    }
211
212#if BOOT_DEBUG_MAPPING
213_puts("\n - x_size    = ");
214_putd( header->x_size );
215_puts("\n - y_size    = ");
216_putd( header->y_size );
217_puts("\n - procs     = ");
218_putd( header->procs );
219_puts("\n - periphs   = ");
220_putd( header->periphs );
221_puts("\n - vspaces   = ");
222_putd( header->vspaces );
223_puts("\n - tasks     = ");
224_putd( header->tasks );
225_puts("\n");
226_puts("\n - size of header  = ");
227_putd( MAPPING_HEADER_SIZE );
228_puts("\n - size of cluster = ");
229_putd( MAPPING_CLUSTER_SIZE );
230_puts("\n - size of pseg    = ");
231_putd( MAPPING_PSEG_SIZE );
232_puts("\n - size of proc    = ");
233_putd( MAPPING_PROC_SIZE );
234_puts("\n - size of vspace  = ");
235_putd( MAPPING_VSPACE_SIZE );
236_puts("\n - size of vseg    = ");
237_putd( MAPPING_VSEG_SIZE );
238_puts("\n - size of vobj    = ");
239_putd( MAPPING_VOBJ_SIZE );
240_puts("\n - size of task    = ");
241_putd( MAPPING_TASK_SIZE );
242_puts("\n");
243
244unsigned int cluster_id;
245mapping_cluster_t * cluster = _get_cluster_base(header);
246for( cluster_id = 0; cluster_id < X_SIZE*Y_SIZE ; cluster_id++) 
247{
248    _puts("\n - cluster[");
249    _putd( cluster[cluster_id].x );
250    _puts(",");
251    _putd( cluster[cluster_id].y );
252    _puts("]\n   procs   = ");
253    _putd( cluster[cluster_id].procs );
254    _puts("\n   psegs   = ");
255    _putd( cluster[cluster_id].psegs );
256    _puts("\n   periphs = ");
257    _putd( cluster[cluster_id].periphs );
258    _puts("\n");
259}
260#endif
261
262} // end boot_mapping_check()
263
264//////////////////////////////////////////////////////////////////////////////
265// This function registers a new PTE1 in the page table defined
266// by the vspace_id argument, and the (x,y) coordinates.
267// It updates only the first level PT1.
268//////////////////////////////////////////////////////////////////////////////
269void boot_add_pte1( unsigned int vspace_id,
270                    unsigned int x,
271                    unsigned int y,
272                    unsigned int vpn,        // 20 bits right-justified
273                    unsigned int flags,      // 10 bits left-justified
274                    unsigned int ppn )       // 28 bits right-justified
275{
276
277#if (BOOT_DEBUG_PT > 1)
278_puts(" - PTE1 in PTAB[");
279_putd( vspace_id );
280_puts(",");
281_putd( x );
282_puts(",");
283_putd( y );
284_puts("] : vpn = ");
285_putx( vpn );
286#endif
287
288    // compute index in PT1
289    unsigned int    ix1 = vpn >> 9;         // 11 bits for ix1
290
291    // get page table physical base address
292    paddr_t         pt1_pbase = _ptabs_paddr[vspace_id][x][y];
293
294    // check pt1_base
295    if ( pt1_pbase == 0 )
296    {
297        _puts("\n[BOOT ERROR] in boot_add_pte1() : illegal pbase address for PTAB[");
298        _putd( vspace_id );
299        _puts(",");
300        _putd( x );
301        _puts(",");
302        _putd( y );
303        _puts("]\n");
304        _exit();
305    }
306
307    // compute pte1 : 2 bits V T / 8 bits flags / 3 bits RSVD / 19 bits bppi
308    unsigned int    pte1 = PTE_V |
309                           (flags & 0x3FC00000) |
310                           ((ppn>>9) & 0x0007FFFF);
311
312    // write pte1 in PT1
313    _physical_write( pt1_pbase + 4*ix1, pte1 );
314
315#if (BOOT_DEBUG_PT > 1)
316_puts(" / ppn = ");
317_putx( ppn );
318_puts(" / flags = ");
319_putx( flags );
320_puts("\n");
321#endif
322
323}   // end boot_add_pte1()
324
325//////////////////////////////////////////////////////////////////////////////
326// This function registers a new PTE2 in the page table defined
327// by the vspace_id argument, and the (x,y) coordinates.
328// It updates both the first level PT1 and the second level PT2.
329// As the set of PT2s is implemented as a fixed size array (no dynamic
330// allocation), this function checks a possible overflow of the PT2 array.
331//////////////////////////////////////////////////////////////////////////////
332void boot_add_pte2( unsigned int vspace_id,
333                    unsigned int x,
334                    unsigned int y,
335                    unsigned int vpn,        // 20 bits right-justified
336                    unsigned int flags,      // 10 bits left-justified
337                    unsigned int ppn )       // 28 bits right-justified
338{
339
340#if (BOOT_DEBUG_PT > 1)
341_puts(" - PTE2 in PTAB[");
342_putd( vspace_id );
343_puts(",");
344_putd( x );
345_puts(",");
346_putd( y );
347_puts("] : vpn = ");
348_putx( vpn );
349#endif
350
351    unsigned int ix1;
352    unsigned int ix2;
353    paddr_t      pt2_pbase;     // PT2 physical base address
354    paddr_t      pte2_paddr;    // PTE2 physical address
355    unsigned int pt2_id;        // PT2 index
356    unsigned int ptd;           // PTD : entry in PT1
357
358    ix1 = vpn >> 9;             // 11 bits for ix1
359    ix2 = vpn & 0x1FF;          //  9 bits for ix2
360
361    // get page table physical base address and size
362    paddr_t      pt1_pbase = _ptabs_paddr[vspace_id][x][y];
363
364    // check pt1_base
365    if ( pt1_pbase == 0 )
366    {
367        _puts("\n[BOOT ERROR] in boot_add_pte2() : PTAB[");
368        _putd( vspace_id );
369        _puts(",");
370        _putd( x );
371        _puts(",");
372        _putd( y );
373        _puts("] undefined\n");
374        _exit();
375    }
376
377    // get ptd in PT1
378    ptd = _physical_read(pt1_pbase + 4 * ix1);
379
380    if ((ptd & PTE_V) == 0)    // undefined PTD: compute PT2 base address,
381                               // and set a new PTD in PT1
382    {
383        pt2_id = _ptabs_next_pt2[vspace_id][x][y];
384        if (pt2_id == _ptabs_max_pt2) 
385        {
386            _puts("\n[BOOT ERROR] in boot_add_pte2() : PTAB[");
387            _putd( vspace_id );
388            _puts(",");
389            _putd( x );
390            _puts(",");
391            _putd( y );
392            _puts("] contains not enough PT2s\n");
393            _exit();
394        }
395
396        pt2_pbase = pt1_pbase + PT1_SIZE + PT2_SIZE * pt2_id;
397        ptd = PTE_V | PTE_T | (unsigned int) (pt2_pbase >> 12);
398        _physical_write( pt1_pbase + 4*ix1, ptd);
399        _ptabs_next_pt2[vspace_id][x][y] = pt2_id + 1;
400    }
401    else                       // valid PTD: compute PT2 base address
402    {
403        pt2_pbase = ((paddr_t)(ptd & 0x0FFFFFFF)) << 12;
404    }
405
406    // set PTE in PT2 : flags & PPN in two 32 bits words
407    pte2_paddr  = pt2_pbase + 8 * ix2;
408    _physical_write(pte2_paddr     , (PTE_V |flags) );
409    _physical_write(pte2_paddr + 4 , ppn);
410
411#if (BOOT_DEBUG_PT > 1)
412_puts(" / ppn = ");
413_putx( ppn );
414_puts(" / flags = ");
415_putx( flags );
416_puts("\n");
417#endif
418
419}   // end boot_add_pte2()
420
421////////////////////////////////////////////////////////////////////////////////////
422// Align the value of paddr or vaddr to the required alignement,
423// defined by alignPow2 == L2(alignement).
424////////////////////////////////////////////////////////////////////////////////////
425paddr_t paddr_align_to(paddr_t paddr, unsigned int alignPow2) 
426{
427    paddr_t mask = (1 << alignPow2) - 1;
428    return ((paddr + mask) & ~mask);
429}
430
431unsigned int vaddr_align_to(unsigned int vaddr, unsigned int alignPow2) 
432{
433    unsigned int mask = (1 << alignPow2) - 1;
434    return ((vaddr + mask) & ~mask);
435}
436
437/////////////////////////////////////////////////////////////////////////////////////
438// This function map a vseg identified by the vseg pointer.
439//
440// A given vseg can be mapped in Big Physical Pages (BPP: 2 Mbytes) or in a
441// Small Physical Pages (SPP: 4 Kbytes), depending on the "big" attribute of vseg,
442// with the following rules:
443// - SPP : There is only one vseg in a small physical page, but a single vseg
444//   can cover several contiguous small physical pages.
445// - BPP : It can exist several vsegs in a single big physical page, and a single
446//   vseg can cover several contiguous big physical pages.
447//
448// 1) First step: it computes the vseg length, and register it in vseg->length field.
449//    It computes - for each vobj - the actual vbase address, taking into
450//    account the alignment constraints and register it in vobj->vbase field.
451//
452// 2) Second step: it allocates the required number of physical pages,
453//    computes the physical base address (if the vseg is not identity mapping),
454//    and register it in the vseg pbase field.
455//    Only the 4 vsegs used by the boot code and the peripheral vsegs
456//    can be identity mapping: The first big physical page in cluster[0,0]
457//    is reserved for the 4 boot vsegs.
458//
459// 3) Third step (only for vseg that have the VOBJ_TYPE_PTAB): all page tables
460//    associated to the various vspaces must be packed in the same vseg.
461//    We divide the vseg in M sub-segments, and compute the vbase and pbase
462//    addresses for each page table, and register it in the _ptabs_paddr
463//    and _ptabs_vaddr arrays.
464// 
465/////////////////////////////////////////////////////////////////////////////////////
466void boot_vseg_map( mapping_vseg_t* vseg,
467                    unsigned int    vspace_id )
468{
469    mapping_header_t*   header  = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
470    mapping_vobj_t*     vobj    = _get_vobj_base(header);
471    mapping_cluster_t*  cluster = _get_cluster_base(header);
472    mapping_pseg_t*     pseg    = _get_pseg_base(header);
473
474    // compute destination cluster pointer & coordinates
475    pseg    = pseg + vseg->psegid;
476    cluster = cluster + pseg->clusterid;
477    unsigned int        x_dest     = cluster->x;
478    unsigned int        y_dest     = cluster->y;
479
480    // compute the first vobj global index
481    unsigned int        vobj_id = vseg->vobj_offset;
482   
483    // compute the "big" vseg attribute
484    unsigned int        big = vseg->big;
485
486    // compute the "is_ram" vseg attribute
487    unsigned int        is_ram;
488    if ( pseg->type == PSEG_TYPE_RAM )  is_ram = 1;
489    else                                is_ram = 0;
490
491    // compute the "is_ptab" attribute
492    unsigned int        is_ptab;
493    if ( vobj[vobj_id].type == VOBJ_TYPE_PTAB ) is_ptab = 1;
494    else                                        is_ptab = 0;
495
496    // compute actual vspace index
497    unsigned int vsid;
498    if ( vspace_id == 0xFFFFFFFF ) vsid = 0;
499    else                           vsid = vspace_id;
500
501    //////////// First step : compute vseg length and vobj(s) vbase
502
503    unsigned int vobj_vbase = vseg->vbase;   // min vbase for first vobj
504
505    for ( vobj_id = vseg->vobj_offset ;
506          vobj_id < (vseg->vobj_offset + vseg->vobjs) ; 
507          vobj_id++ ) 
508    {
509        // compute and register vobj vbase
510        vobj[vobj_id].vbase = vaddr_align_to( vobj_vbase, vobj[vobj_id].align );
511   
512        // compute min vbase for next vobj
513        vobj_vbase = vobj[vobj_id].vbase + vobj[vobj_id].length;
514    }
515
516    // compute and register vseg length (multiple of 4 Kbytes)
517    vseg->length = vaddr_align_to( vobj_vbase - vseg->vbase, 12 );
518   
519    //////////// Second step : compute ppn and npages 
520    //////////// - if identity mapping :  ppn <= vpn
521    //////////// - if vseg is periph   :  ppn <= pseg.base >> 12
522    //////////// - if vseg is ram      :  ppn <= physical memory allocator
523
524    unsigned int ppn;          // first physical page index ( 28 bits = |x|y|bppi|sppi| )
525    unsigned int vpn;          // first virtual page index  ( 20 bits = |ix1|ix2| )
526    unsigned int vpn_max;      // last  virtual page index  ( 20 bits = |ix1|ix2| )
527
528    vpn     = vseg->vbase >> 12;
529    vpn_max = (vseg->vbase + vseg->length - 1) >> 12;
530
531    // compute npages
532    unsigned int npages;       // number of required (big or small) pages
533    if ( big == 0 ) npages  = vpn_max - vpn + 1;            // number of small pages
534    else            npages  = (vpn_max>>9) - (vpn>>9) + 1;  // number of big pages
535
536    // compute ppn
537    if ( vseg->ident )           // identity mapping
538    {
539        ppn = vpn;
540    }
541    else                         // not identity mapping
542    {
543        if ( is_ram )            // RAM : physical memory allocation required
544        {
545            // compute pointer on physical memory allocator in dest cluster
546            pmem_alloc_t*     palloc = &boot_pmem_alloc[x_dest][y_dest];
547
548            if ( big == 0 )             // SPP : small physical pages
549            {
550                // allocate contiguous small physical pages
551                ppn = _get_small_ppn( palloc, npages );
552            }
553            else                            // BPP : big physical pages
554            {
555 
556                // one big page can be shared by several vsegs
557                // we must chek if BPP already allocated
558                if ( is_ptab )   // It cannot be mapped
559                {
560                    ppn = _get_big_ppn( palloc, npages ); 
561                }
562                else             // It can be mapped
563                {
564                    unsigned int ix1   = vpn >> 9;   // 11 bits
565                    paddr_t      paddr = _ptabs_paddr[vsid][x_dest][y_dest] + (ix1<<2);
566                    unsigned int pte1  = _physical_read( paddr );
567                    if ( (pte1 & PTE_V) == 0 )     // BPP not allocated yet
568                    {
569                        // allocate contiguous big physical pages
570                        ppn = _get_big_ppn( palloc, npages ); 
571                    }
572                    else                           // BPP already allocated
573                    {
574                        ppn = ((pte1 << 9) & 0x0FFFFE00);
575                    }
576                }
577                ppn = ppn | (vpn & 0x1FF);
578            }
579        }
580        else                    // PERI : no memory allocation required
581        {
582            ppn = pseg->base >> 12;
583        }
584    }
585
586    // update vseg.pbase field and update vsegs chaining
587    vseg->pbase     = ((paddr_t)ppn) << 12;
588    vseg->next_vseg = pseg->next_vseg;
589    pseg->next_vseg = (unsigned int)vseg;
590
591
592    //////////// Third step : (only if the vseg is a page table)
593    //////////// - compute the physical & virtual base address for each vspace
594    ////////////   by dividing the vseg in several sub-segments.
595    //////////// - register it in _ptabs_vaddr & _ptabs_paddr arrays,
596    ////////////   and initialize next_pt2 allocators.
597    //////////// - reset all entries in first level page tables
598   
599    if ( is_ptab )
600    {
601        unsigned int   vs;        // vspace index
602        unsigned int   nspaces;   // number of vspaces
603        unsigned int   nsp;       // number of small pages for one PTAB
604        unsigned int   offset;    // address offset for current PTAB
605
606        nspaces = header->vspaces;
607        offset  = 0;
608
609        // each PTAB must be aligned on a 8 Kbytes boundary
610        nsp = ( vseg->length >> 12 ) / nspaces;
611        if ( (nsp & 0x1) == 0x1 ) nsp = nsp - 1;
612
613        // compute max_pt2
614        _ptabs_max_pt2 = ((nsp<<12) - PT1_SIZE) / PT2_SIZE;
615       
616        for ( vs = 0 ; vs < nspaces ; vs++ )
617        {
618            _ptabs_vaddr   [vs][x_dest][y_dest] = (vpn + offset) << 12; 
619            _ptabs_paddr   [vs][x_dest][y_dest] = ((paddr_t)(ppn + offset)) << 12;
620            _ptabs_next_pt2[vs][x_dest][y_dest] = 0;
621            offset += nsp;
622/*
623            // reset all entries in PT1 (8 Kbytes)
624            _physical_memset( _ptabs_paddr[vs][x_dest][y_dest], PT1_SIZE, 0 );
625           
626*/
627        }
628    }
629
630#if BOOT_DEBUG_PT
631_puts("[BOOT DEBUG] ");
632_puts( vseg->name ); 
633_puts(" in cluster[");
634_putd( x_dest );
635_puts(",");
636_putd( y_dest );
637_puts("] : vbase = ");
638_putx( vseg->vbase );
639_puts(" / length = ");
640_putx( vseg->length );
641if ( big ) _puts(" / BIG   / npages = ");
642else       _puts(" / SMALL / npages = ");
643_putd( npages );
644_puts(" / pbase = ");
645_putl( vseg->pbase );
646_puts("\n");
647#endif
648
649} // end boot_vseg_map()
650
651/////////////////////////////////////////////////////////////////////////////////////
652// For the vseg defined by the vseg pointer, this function register all PTEs
653// in one or several page tables.
654// It is a global vseg (system vseg) if (vspace_id == 0xFFFFFFFF).
655// The number of involved PTABs depends on the "local" and "global" attributes:
656//  - PTEs are replicated in all vspaces for a global vseg.
657//  - PTEs are replicated in all clusters for a non local vseg.
658/////////////////////////////////////////////////////////////////////////////////////
659void boot_vseg_pte( mapping_vseg_t*  vseg,
660                    unsigned int     vspace_id )
661{
662    // compute the "global" vseg attribute and actual vspace index
663    unsigned int        global;
664    unsigned int        vsid;   
665    if ( vspace_id == 0xFFFFFFFF )
666    {
667        global = 1;
668        vsid   = 0;
669    }
670    else
671    {
672        global = 0;
673        vsid   = vspace_id;
674    }
675
676    // compute the "local" and "big" attributes
677    unsigned int        local  = vseg->local;
678    unsigned int        big    = vseg->big;
679
680    // compute vseg flags
681    // The three flags (Local, Remote and Dirty) are set to 1 to reduce
682    // latency of TLB miss (L/R) and write (D): Avoid hardware update
683    // mechanism for these flags because GIET_VM does use these flags.
684    unsigned int flags = 0;
685    if (vseg->mode & C_MODE_MASK) flags |= PTE_C;
686    if (vseg->mode & X_MODE_MASK) flags |= PTE_X;
687    if (vseg->mode & W_MODE_MASK) flags |= PTE_W;
688    if (vseg->mode & U_MODE_MASK) flags |= PTE_U;
689    if ( global )                 flags |= PTE_G;
690                                  flags |= PTE_L;
691                                  flags |= PTE_R;
692                                  flags |= PTE_D;
693
694    // compute VPN, PPN and number of pages (big or small)
695    unsigned int vpn     = vseg->vbase >> 12;
696    unsigned int vpn_max = (vseg->vbase + vseg->length - 1) >> 12;
697    unsigned int ppn     = (unsigned int)(vseg->pbase >> 12);
698    unsigned int npages;
699    if ( big == 0 ) npages  = vpn_max - vpn + 1;           
700    else            npages  = (vpn_max>>9) - (vpn>>9) + 1; 
701
702    // compute destination cluster coordinates
703    unsigned int        x_dest;
704    unsigned int        y_dest;
705    mapping_header_t*   header  = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
706    mapping_cluster_t*  cluster = _get_cluster_base(header);
707    mapping_pseg_t*     pseg    = _get_pseg_base(header);
708    pseg     = pseg + vseg->psegid;
709    cluster  = cluster + pseg->clusterid;
710    x_dest   = cluster->x;
711    y_dest   = cluster->y;
712
713    unsigned int p;     // iterator for physical page index
714    unsigned int x;     // iterator for cluster x coordinate 
715    unsigned int y;     // iterator for cluster y coordinate 
716    unsigned int v;     // iterator for vspace index
717
718    // loop on PTEs
719    for ( p = 0 ; p < npages ; p++ )
720    { 
721        if  ( (local != 0) && (global == 0) )         // one cluster  / one vspace
722        {
723            if ( big )   // big pages => PTE1s
724            {
725                boot_add_pte1( vsid,
726                               x_dest,
727                               y_dest,
728                               vpn + (p<<9),
729                               flags, 
730                               ppn + (p<<9) );
731            }
732            else         // small pages => PTE2s
733            {
734                boot_add_pte2( vsid,
735                               x_dest,
736                               y_dest,
737                               vpn + p,     
738                               flags, 
739                               ppn + p );
740            }
741        }
742        else if ( (local == 0) && (global == 0) )     // all clusters / one vspace
743        {
744            for ( x = 0 ; x < X_SIZE ; x++ )
745            {
746                for ( y = 0 ; y < Y_SIZE ; y++ )
747                {
748                    if ( big )   // big pages => PTE1s
749                    {
750                        boot_add_pte1( vsid,
751                                       x,
752                                       y,
753                                       vpn + (p<<9),
754                                       flags, 
755                                       ppn + (p<<9) );
756                    }
757                    else         // small pages => PTE2s
758                    {
759                        boot_add_pte2( vsid,
760                                       x,
761                                       y,
762                                       vpn + p,
763                                       flags, 
764                                       ppn + p );
765                    }
766                }
767            }
768        }
769        else if ( (local != 0) && (global != 0) )     // one cluster  / all vspaces
770        {
771            for ( v = 0 ; v < header->vspaces ; v++ )
772            {
773                if ( big )   // big pages => PTE1s
774                {
775                    boot_add_pte1( v,
776                                   x_dest,
777                                   y_dest,
778                                   vpn + (p<<9),
779                                   flags, 
780                                   ppn + (p<<9) );
781                }
782                else         // small pages = PTE2s
783                { 
784                    boot_add_pte2( v,
785                                   x_dest,
786                                   y_dest,
787                                   vpn + p,
788                                   flags, 
789                                   ppn + p );
790                }
791            }
792        }
793        else if ( (local == 0) && (global != 0) )     // all clusters / all vspaces
794        {
795            for ( x = 0 ; x < X_SIZE ; x++ )
796            {
797                for ( y = 0 ; y < Y_SIZE ; y++ )
798                {
799                    for ( v = 0 ; v < header->vspaces ; v++ )
800                    {
801                        if ( big )  // big pages => PTE1s
802                        {
803                            boot_add_pte1( v,
804                                           x,
805                                           y,
806                                           vpn + (p<<9),
807                                           flags, 
808                                           ppn + (p<<9) );
809                        }
810                        else        // small pages -> PTE2s
811                        {
812                            boot_add_pte2( v,
813                                           x,
814                                           y,
815                                           vpn + p,
816                                           flags, 
817                                           ppn + p );
818                        }
819                    }
820                }
821            }
822        }
823    }  // end for pages
824}  // end boot_vseg_pte()
825
826///////////////////////////////////////////////////////////////////////////////
827// This function initialises the page tables for all vspaces defined
828// in the mapping_info data structure.
829// For each vspace, there is one page table per cluster.
830// In each cluster all page tables for the different vspaces must be
831// packed in one vseg occupying one single BPP (Big Physical Page).
832//
833// For each vseg, the mapping is done in two steps:
834//
835// A) mapping : the boot_vseg_map() function allocates contiguous BPPs
836//    or SPPs (if the vseg is not associated to a peripheral), and register
837//    the physical base address in the vseg pbase field. It initialises the
838//    _ptabs_vaddr and _ptabs_paddr arrays if the vseg is a PTAB.
839//
840// B) page table initialisation : the boot_vseg_pte() function initialise
841//    the PTEs (both PTE1 and PTE2) in one or several page tables:
842//    - PTEs are replicated in all vspaces for a global vseg.
843//    - PTEs are replicated in all clusters for a non local vseg.
844//
845// We must handle vsegs in the following order
846//   1) all global vsegs containing a page table,
847//   2) all global vsegs occupying more than one BPP,
848//   3) all others global vsegs
849//   4) all private vsegs in user space.
850///////////////////////////////////////////////////////////////////////////////
851void _ptabs_init() 
852{
853    mapping_header_t*   header = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
854    mapping_vspace_t*   vspace = _get_vspace_base(header);
855    mapping_vseg_t*     vseg   = _get_vseg_base(header);
856    mapping_vobj_t*     vobj   = _get_vobj_base(header);
857
858    unsigned int vspace_id;
859    unsigned int vseg_id;
860
861    if (header->vspaces == 0 )
862    {
863        _puts("\n[BOOT ERROR] in _ptabs_init() : mapping ");
864        _puts( header->name );
865        _puts(" contains no vspace\n");
866        _exit();
867    }
868
869    ///////// Phase 1 : global vsegs containing a PTAB (two loops required)
870
871#if BOOT_DEBUG_PT
872_puts("\n[BOOT DEBUG] map PTAB global vsegs\n");
873#endif
874
875    for (vseg_id = 0; vseg_id < header->globals; vseg_id++) 
876    {
877        unsigned int vobj_id = vseg[vseg_id].vobj_offset;
878        if ( (vobj[vobj_id].type == VOBJ_TYPE_PTAB) )
879        {
880            boot_vseg_map( &vseg[vseg_id], 0xFFFFFFFF );
881            vseg[vseg_id].mapped = 1;
882        }
883    }
884
885    for (vseg_id = 0; vseg_id < header->globals; vseg_id++) 
886    {
887        unsigned int vobj_id = vseg[vseg_id].vobj_offset;
888        if ( (vobj[vobj_id].type == VOBJ_TYPE_PTAB) )
889        {
890            boot_vseg_pte( &vseg[vseg_id], 0xFFFFFFFF );
891            vseg[vseg_id].mapped = 1;
892        }
893    }
894
895    ///////// Phase 2 : global vsegs occupying more than one BPP (one loop)
896
897#if BOOT_DEBUG_PT
898_puts("\n[BOOT DEBUG] map all multi-BPP global vsegs\n");
899#endif
900
901    for (vseg_id = 0; vseg_id < header->globals; vseg_id++) 
902    {
903        unsigned int vobj_id = vseg[vseg_id].vobj_offset;
904        if ( (vobj[vobj_id].length > 0x200000) &&
905             (vseg[vseg_id].mapped == 0) )
906        {
907            boot_vseg_map( &vseg[vseg_id], 0xFFFFFFFF );
908            vseg[vseg_id].mapped = 1;
909            boot_vseg_pte( &vseg[vseg_id], 0xFFFFFFFF );
910        }
911    }
912
913    ///////// Phase 3 : all others global vsegs (one loop)
914
915#if BOOT_DEBUG_PT
916_puts("\n[BOOT DEBUG] map all others global vsegs\n");
917#endif
918
919    for (vseg_id = 0; vseg_id < header->globals; vseg_id++) 
920    {
921        if ( vseg[vseg_id].mapped == 0 )
922        {
923            boot_vseg_map( &vseg[vseg_id], 0xFFFFFFFF );
924            vseg[vseg_id].mapped = 1;
925            boot_vseg_pte( &vseg[vseg_id], 0xFFFFFFFF );
926        }
927    }
928
929    ///////// Phase 4 : all private vsegs (two nested loops)
930
931    for (vspace_id = 0; vspace_id < header->vspaces; vspace_id++) 
932    {
933
934#if BOOT_DEBUG_PT
935_puts("\n[BOOT DEBUG] map private vsegs for vspace ");
936_puts( vspace[vspace_id].name );
937_puts("\n");
938#endif
939
940        for (vseg_id = vspace[vspace_id].vseg_offset;
941             vseg_id < (vspace[vspace_id].vseg_offset + vspace[vspace_id].vsegs);
942             vseg_id++) 
943        {
944            boot_vseg_map( &vseg[vseg_id], vspace_id );
945            vseg[vseg_id].mapped = 1;
946            boot_vseg_pte( &vseg[vseg_id], vspace_id );
947        }
948    }
949
950#if (BOOT_DEBUG_PT > 1)
951mapping_vseg_t*    curr;
952mapping_pseg_t*    pseg    = _get_pseg_base(header);
953mapping_cluster_t* cluster = _get_cluster_base(header);
954unsigned int       pseg_id;
955for( pseg_id = 0 ; pseg_id < header->psegs ; pseg_id++ )
956{
957    unsigned int cluster_id = pseg[pseg_id].clusterid;
958    _puts("\n[BOOT DEBUG] vsegs mapped on pseg ");
959    _puts( pseg[pseg_id].name );
960    _puts(" in cluster[");
961    _putd( cluster[cluster_id].x );
962    _puts(",");
963    _putd( cluster[cluster_id].y );
964    _puts("]\n");
965    for( curr = (mapping_vseg_t*)pseg[pseg_id].next_vseg ;
966         curr != 0 ;
967         curr = (mapping_vseg_t*)curr->next_vseg )
968    {
969        _puts(" - vseg ");
970        _puts( curr->name );
971        _puts(" : len = ");
972        _putx( curr->length );
973        _puts(" / vbase ");
974        _putx( curr->vbase );
975        _puts(" / pbase ");
976        _putl( curr->pbase );
977        _puts("\n");
978    } 
979}
980#endif
981
982} // end boot_ptabs_init()
983
984///////////////////////////////////////////////////////////////////////////////
985// This function initializes all private vobjs defined in the vspaces,
986// such as mwmr channels, barriers and locks, because these vobjs
987// are not known, and not initialized by the compiler.
988// The MMU is supposed to be activated...
989///////////////////////////////////////////////////////////////////////////////
990void boot_vobjs_init() 
991{
992    mapping_header_t* header = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
993    mapping_vspace_t* vspace = _get_vspace_base(header);
994    mapping_vobj_t* vobj     = _get_vobj_base(header);
995
996    unsigned int vspace_id;
997    unsigned int vobj_id;
998
999    // loop on the vspaces
1000    for (vspace_id = 0; vspace_id < header->vspaces; vspace_id++) 
1001    {
1002
1003#if BOOT_DEBUG_VOBJS
1004_puts("\n[BOOT DEBUG] ****** vobjs initialisation in vspace ");
1005_puts(vspace[vspace_id].name);
1006_puts(" ******\n");
1007#endif
1008
1009        _set_mmu_ptpr( (unsigned int)(_ptabs_paddr[vspace_id][0][0] >> 13) );
1010
1011        // loop on the vobjs
1012        for (vobj_id = vspace[vspace_id].vobj_offset;
1013             vobj_id < (vspace[vspace_id].vobj_offset + vspace[vspace_id].vobjs);
1014             vobj_id++) 
1015        {
1016            switch (vobj[vobj_id].type) 
1017            {
1018                case VOBJ_TYPE_MWMR:    // storage capacity is (vobj.length/4 - 5) words
1019                {
1020#if BOOT_DEBUG_VOBJS
1021_puts("MWMR    : ");
1022_puts(vobj[vobj_id].name);
1023_puts(" / vaddr = ");
1024_putx(vobj[vobj_id].vaddr);
1025_puts(" / paddr = ");
1026_putl(vobj[vobj_id].paddr);
1027_puts(" / length = ");
1028_putx(vobj[vobj_id].length);
1029_puts("\n");
1030#endif
1031                    mwmr_channel_t* mwmr = (mwmr_channel_t *) (vobj[vobj_id].vbase);
1032                    mwmr->ptw = 0;
1033                    mwmr->ptr = 0;
1034                    mwmr->sts = 0;
1035                    mwmr->width = vobj[vobj_id].init;
1036                    mwmr->depth = (vobj[vobj_id].length >> 2) - 6;
1037                    mwmr->lock = 0;
1038#if BOOT_DEBUG_VOBJS
1039_puts("          fifo depth = ");
1040_putd(mwmr->depth);
1041_puts(" / width = ");
1042_putd(mwmr->width);
1043_puts("\n");
1044#endif
1045                    break;
1046                }
1047                case VOBJ_TYPE_ELF:    // initialisation done by the loader
1048                {
1049#if BOOT_DEBUG_VOBJS
1050_puts("ELF     : ");
1051_puts(vobj[vobj_id].name);
1052_puts(" / vaddr = ");
1053_putx(vobj[vobj_id].vaddr);
1054_puts(" / paddr = ");
1055_putl(vobj[vobj_id].paddr);
1056_puts(" / length = ");
1057_putx(vobj[vobj_id].length);
1058_puts("\n");
1059#endif
1060                    break;
1061                }
1062                case VOBJ_TYPE_BLOB:    // initialisation done by the loader
1063                {
1064#if BOOT_DEBUG_VOBJS
1065_puts("BLOB    : ");
1066_puts(vobj[vobj_id].name);
1067_puts(" / vaddr = ");
1068_putx(vobj[vobj_id].vaddr);
1069_puts(" / paddr = ");
1070_putl(vobj[vobj_id].paddr);
1071_puts(" / length = ");
1072_putx(vobj[vobj_id].length);
1073_puts("\n");
1074#endif
1075                    break;
1076                }
1077                case VOBJ_TYPE_BARRIER:    // init is the number of participants
1078                {
1079#if BOOT_DEBUG_VOBJS
1080_puts("BARRIER : ");
1081_puts(vobj[vobj_id].name);
1082_puts(" / vaddr = ");
1083_putx(vobj[vobj_id].vaddr);
1084_puts(" / paddr = ");
1085_putl(vobj[vobj_id].paddr);
1086_puts(" / length = ");
1087_putx(vobj[vobj_id].length);
1088_puts("\n");
1089#endif
1090                    giet_barrier_t* barrier = (giet_barrier_t *) (vobj[vobj_id].vbase);
1091                    barrier->count  = vobj[vobj_id].init;
1092                    barrier->ntasks = vobj[vobj_id].init;
1093                    barrier->sense  = 0;
1094#if BOOT_DEBUG_VOBJS
1095_puts("          init_value = ");
1096_putd(barrier->init);
1097_puts("\n");
1098#endif
1099                    break;
1100                }
1101                case VOBJ_TYPE_LOCK:    // init value is "not taken"
1102                {
1103#if BOOT_DEBUG_VOBJS
1104_puts("LOCK    : ");
1105_puts(vobj[vobj_id].name);
1106_puts(" / vaddr = ");
1107_putx(vobj[vobj_id].vaddr);
1108_puts(" / paddr = ");
1109_putl(vobj[vobj_id].paddr);
1110_puts(" / length = ");
1111_putx(vobj[vobj_id].length);
1112_puts("\n");
1113#endif
1114                    unsigned int* lock = (unsigned int *) (vobj[vobj_id].vbase);
1115                    *lock = 0;
1116                    break;
1117                }
1118                case VOBJ_TYPE_BUFFER:    // nothing to initialise
1119                {
1120#if BOOT_DEBUG_VOBJS
1121_puts("BUFFER  : ");
1122_puts(vobj[vobj_id].name);
1123_puts(" / vaddr = ");
1124_putx(vobj[vobj_id].vaddr);
1125_puts(" / paddr = ");
1126_putl(vobj[vobj_id].paddr);
1127_puts(" / length = ");
1128_putx(vobj[vobj_id].length);
1129_puts("\n");
1130#endif
1131                    break;
1132                }
1133                case VOBJ_TYPE_MEMSPACE:
1134                {
1135#if BOOT_DEBUG_VOBJS
1136_puts("MEMSPACE  : ");
1137_puts(vobj[vobj_id].name);
1138_puts(" / vaddr = ");
1139_putx(vobj[vobj_id].vaddr);
1140_puts(" / paddr = ");
1141_putl(vobj[vobj_id].paddr);
1142_puts(" / length = ");
1143_putx(vobj[vobj_id].length);
1144_puts("\n");
1145#endif
1146                    giet_memspace_t* memspace = (giet_memspace_t *) vobj[vobj_id].vbase;
1147                    memspace->buffer = (void *) vobj[vobj_id].vbase + 8;
1148                    memspace->size = vobj[vobj_id].length - 8;
1149#if BOOT_DEBUG_VOBJS
1150_puts("          buffer vbase = ");
1151_putx((unsigned int)memspace->buffer);
1152_puts(" / size = ");
1153_putx(memspace->size);
1154_puts("\n");
1155#endif
1156                    break;
1157                }
1158                case VOBJ_TYPE_CONST:
1159                {
1160#if BOOT_DEBUG_VOBJS
1161_puts("CONST   : ");
1162_puts(vobj[vobj_id].name);
1163_puts(" / vaddr = ");
1164_putx(vobj[vobj_id].vaddr);
1165_puts(" / paddr = ");
1166_putl(vobj[vobj_id].paddr);
1167_puts(" / length = ");
1168_putx(vobj[vobj_id].length);
1169_puts(" / init = ");
1170_putx(vobj[vobj_id].init);
1171_puts("\n");
1172#endif
1173                    unsigned int* addr = (unsigned int *) vobj[vobj_id].vbase;
1174                    *addr = vobj[vobj_id].init;
1175
1176#if BOOT_DEBUG_VOBJS
1177_puts("          init = ");
1178_putx(*addr);
1179_puts("\n");
1180#endif
1181                    break;
1182                }
1183                default:
1184                {
1185                    _puts("\n[BOOT ERROR] in boot_vobjs_init() : Illegal vobj type ");
1186                    _putd( vobj[vobj_id].type );
1187                    _puts(" in vspace ");
1188                    _puts( vspace[vspace_id].name );
1189                    _puts("\n");
1190                    _exit();
1191                }
1192            }            // end switch type
1193        }          // end loop on vobjs
1194    }        // end loop on vspaces
1195} // end boot_vobjs_init()
1196
1197///////////////////////////////////////////////////////////////////////////////
1198// This function returns in the vbase and length buffers the virtual base
1199// address and the length of the  segment allocated to the schedulers array
1200// in the cluster defined by the clusterid argument.
1201///////////////////////////////////////////////////////////////////////////////
1202void boot_get_sched_vaddr( unsigned int  cluster_id,
1203                           unsigned int* vbase, 
1204                           unsigned int* length )
1205{
1206    mapping_header_t* header = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
1207    mapping_vobj_t*   vobj   = _get_vobj_base(header);
1208    mapping_vseg_t*   vseg   = _get_vseg_base(header);
1209    mapping_pseg_t*   pseg   = _get_pseg_base(header);
1210
1211    unsigned int vseg_id;
1212    unsigned int found = 0;
1213
1214    for ( vseg_id = 0 ; (vseg_id < header->vsegs) && (found == 0) ; vseg_id++ )
1215    {
1216        if ( (vobj[vseg[vseg_id].vobj_offset].type == VOBJ_TYPE_SCHED) && 
1217             (pseg[vseg[vseg_id].psegid].clusterid == cluster_id ) )
1218        {
1219            *vbase  = vseg[vseg_id].vbase;
1220            *length = vobj[vseg[vseg_id].vobj_offset].length;
1221            found = 1;
1222        }
1223    }
1224    if ( found == 0 )
1225    {
1226        mapping_cluster_t* cluster = _get_cluster_base(header);
1227        _puts("\n[BOOT ERROR] No vobj of type SCHED in cluster [");
1228        _putd( cluster[cluster_id].x );
1229        _puts(",");
1230        _putd( cluster[cluster_id].y );
1231        _puts("]\n");
1232        _exit();
1233    }
1234} // end boot_get_sched_vaddr()
1235
1236////////////////////////////////////////////////////////////////////////////////////
1237// This function initialises all processors schedulers.
1238// This is done by processor 0, and the MMU must be activated.
1239// - In Step 1, it initialises the _schedulers[x][y][l] pointers array, and scan
1240//              the processors for a first initialisation of the schedulers:
1241//              idle_task context, and HWI / SWI / PTI vectors.
1242// - In Step 2, it scan all tasks in all vspaces to complete the tasks contexts,
1243//              initialisation as specified in the mapping_info data structure.
1244////////////////////////////////////////////////////////////////////////////////////
1245void boot_schedulers_init() 
1246{
1247    mapping_header_t*  header  = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
1248    mapping_cluster_t* cluster = _get_cluster_base(header);
1249    mapping_vspace_t*  vspace  = _get_vspace_base(header);
1250    mapping_task_t*    task    = _get_task_base(header);
1251    mapping_vobj_t*    vobj    = _get_vobj_base(header);
1252    mapping_periph_t*  periph  = _get_periph_base(header);
1253    mapping_irq_t*     irq     = _get_irq_base(header);
1254
1255    unsigned int cluster_id;    // cluster index in mapping_info
1256    unsigned int periph_id;     // peripheral index in mapping_info
1257    unsigned int irq_id;        // irq index in mapping_info
1258    unsigned int vspace_id;     // vspace index in mapping_info
1259    unsigned int task_id;       // task index in mapping_info
1260    unsigned int vobj_id;       // vobj index in mapping_info
1261
1262    unsigned int lpid;          // local processor index (for several loops)
1263
1264    // TTY, NIC, CMA, HBA, user timer, and WTI channel allocators to user tasks:
1265    // - TTY[0] is reserved for the kernel
1266    // - In all clusters the first NB_PROCS_MAX timers
1267    //   are reserved for the kernel (context switch)
1268    unsigned int alloc_tty_channel = 1;              // global
1269    unsigned int alloc_nic_channel = 0;              // global
1270    unsigned int alloc_cma_channel = 0;              // global
1271    unsigned int alloc_hba_channel = 0;              // global
1272    unsigned int alloc_tim_channel[X_SIZE*Y_SIZE];   // one per cluster
1273
1274    // WTI allocators to processors
1275    // In all clusters, first NB_PROCS_MAX WTIs are for WAKUP
1276    unsigned int alloc_wti_channel[X_SIZE*Y_SIZE];   // one per cluster
1277
1278    // pointers on the XCU and PIC peripherals
1279    mapping_periph_t*  xcu = NULL;
1280    mapping_periph_t*  pic = NULL;
1281
1282    unsigned int          sched_vbase;  // schedulers array vbase address in a cluster
1283    unsigned int          sched_length; // schedulers array length
1284    static_scheduler_t*   psched;       // pointer on processor scheduler
1285
1286    /////////////////////////////////////////////////////////////////////////
1287    // Step 1 : loop on the clusters and on the processors
1288    //          to initialize the schedulers[] array of pointers,
1289    //          idle task context and interrupt vectors.
1290    // Implementation note:
1291    // We need to use both (proc_id) to scan the mapping info structure,
1292    // and (x,y,lpid) to access the schedulers array.
1293
1294    for (cluster_id = 0 ; cluster_id < X_SIZE*Y_SIZE ; cluster_id++) 
1295    {
1296        unsigned int x          = cluster[cluster_id].x;
1297        unsigned int y          = cluster[cluster_id].y;
1298
1299#if BOOT_DEBUG_SCHED
1300_puts("\n[BOOT DEBUG] Initialise schedulers in cluster[");
1301_putd( x );
1302_puts(",");
1303_putd( y );
1304_puts("]\n");
1305#endif
1306        alloc_tim_channel[cluster_id] = NB_PROCS_MAX;
1307        alloc_wti_channel[cluster_id] = NB_PROCS_MAX;
1308
1309        // checking processors number
1310        if ( cluster[cluster_id].procs > NB_PROCS_MAX )
1311        {
1312            _puts("\n[BOOT ERROR] Too much processors in cluster[");
1313            _putd( x );
1314            _puts(",");
1315            _putd( y );
1316            _puts("]\n");
1317            _exit();
1318        }
1319 
1320        // no schedulers initialisation if nprocs == 0
1321        if ( cluster[cluster_id].procs > 0 )
1322        {
1323            // get scheduler array virtual base address in cluster[cluster_id]
1324            boot_get_sched_vaddr( cluster_id, &sched_vbase, &sched_length );
1325
1326            if ( sched_length < (cluster[cluster_id].procs<<13) ) // 8 Kbytes per scheduler
1327            {
1328                _puts("\n[BOOT ERROR] Schedulers segment too small in cluster[");
1329                _putd( x );
1330                _puts(",");
1331                _putd( y );
1332                _puts("]\n");
1333                _exit();
1334            }
1335
1336            // scan peripherals to find the ICU/XCU and the PIC component
1337
1338            xcu = NULL; 
1339            for ( periph_id = cluster[cluster_id].periph_offset ;
1340                  periph_id < cluster[cluster_id].periph_offset + cluster[cluster_id].periphs;
1341                  periph_id++ )
1342            {
1343                if( (periph[periph_id].type == PERIPH_TYPE_XCU) || 
1344                    (periph[periph_id].type == PERIPH_TYPE_ICU) )
1345                {
1346                    xcu = &periph[periph_id];
1347
1348                    if ( xcu->arg < cluster[cluster_id].procs )
1349                    {
1350                        _puts("\n[BOOT ERROR] Not enough inputs for XCU[");
1351                        _putd( x );
1352                        _puts(",");
1353                        _putd( y );
1354                        _puts("]\n");
1355                        _exit();
1356                    }
1357                }
1358                if( periph[periph_id].type == PERIPH_TYPE_PIC )   
1359                {
1360                    pic = &periph[periph_id];
1361                }
1362            } 
1363            if ( xcu == NULL )
1364            {         
1365                _puts("\n[BOOT ERROR] No ICU / XCU component in cluster[");
1366                _putd( x );
1367                _puts(",");
1368                _putd( y );
1369                _puts("]\n");
1370                _exit();
1371            }
1372
1373            // loop on processors for schedulers default values
1374            // initialisation, including WTI and PTI vectors
1375            for ( lpid = 0 ; lpid < cluster[cluster_id].procs ; lpid++ )
1376            {
1377                // pointer on processor scheduler
1378                psched = (static_scheduler_t*)(sched_vbase + (lpid<<13));
1379
1380                // initialise the schedulers pointers array
1381                _schedulers[x][y][lpid] = psched;
1382
1383#if BOOT_DEBUG_SCHED
1384unsigned int   sched_vbase = (unsigned int)_schedulers[x][y][lpid];
1385unsigned int   sched_ppn;
1386unsigned int   sched_flags;
1387paddr_t        sched_pbase;
1388
1389page_table_t* ptab = (page_table_t*)(_ptabs_vaddr[0][x][y]);
1390_v2p_translate( ptab, sched_vbase>>12, &sched_ppn, &sched_flags );
1391sched_pbase = ((paddr_t)sched_ppn)<<12;
1392
1393_puts("\nProc[");
1394_putd( x );
1395_puts(",");
1396_putd( y );
1397_puts(",");
1398_putd( lpid );
1399_puts("] : scheduler vbase = ");
1400_putx( sched_vbase );
1401_puts(" : scheduler pbase = ");
1402_putl( sched_pbase );
1403_puts("\n");
1404#endif
1405                // initialise the "tasks" and "current" variables default values
1406                psched->tasks   = 0;
1407                psched->current = IDLE_TASK_INDEX;
1408
1409                // default values for HWI / PTI / SWI vectors (valid bit = 0)
1410                unsigned int slot;
1411                for (slot = 0; slot < 32; slot++)
1412                {
1413                    psched->hwi_vector[slot] = 0;
1414                    psched->pti_vector[slot] = 0;
1415                    psched->wti_vector[slot] = 0;
1416                }
1417
1418                // WTI[lpid] <= ISR_WAKUP / PTI[lpid] <= ISR_TICK
1419                psched->wti_vector[lpid] = ISR_WAKUP | 0x80000000;
1420                psched->pti_vector[lpid] = ISR_TICK  | 0x80000000;
1421
1422                // initializes the idle_task context in scheduler:
1423                // - the SR slot is 0xFF03 because this task run in kernel mode.
1424                // - it uses the page table of vspace[0]
1425                // - it uses the kernel TTY terminal
1426                // - slots containing addresses (SP, RA, EPC)
1427                //   must be initialised by kernel_init()
1428
1429                psched->context[IDLE_TASK_INDEX][CTX_CR_ID]   = 0;
1430                psched->context[IDLE_TASK_INDEX][CTX_SR_ID]   = 0xFF03;
1431                psched->context[IDLE_TASK_INDEX][CTX_PTPR_ID] = _ptabs_paddr[0][x][y]>>13;
1432                psched->context[IDLE_TASK_INDEX][CTX_PTAB_ID] = _ptabs_vaddr[0][x][y];
1433                psched->context[IDLE_TASK_INDEX][CTX_TTY_ID]  = 0;
1434                psched->context[IDLE_TASK_INDEX][CTX_LTID_ID] = IDLE_TASK_INDEX;
1435                psched->context[IDLE_TASK_INDEX][CTX_VSID_ID] = 0;
1436                psched->context[IDLE_TASK_INDEX][CTX_RUN_ID]  = 1;
1437
1438            }  // end for processors
1439
1440            // scan HWIs connected to local XCU
1441            // for round-robin allocation to processors
1442            lpid = 0;
1443            for ( irq_id = xcu->irq_offset ;
1444                  irq_id < xcu->irq_offset + xcu->irqs ;
1445                  irq_id++ )
1446            {
1447                unsigned int type    = irq[irq_id].srctype;
1448                unsigned int srcid   = irq[irq_id].srcid;
1449                unsigned int isr     = irq[irq_id].isr & 0xFFFF;
1450                unsigned int channel = irq[irq_id].channel << 16;
1451
1452                if ( (type != IRQ_TYPE_HWI) || (srcid > 31) )
1453                {
1454                    _puts("\n[BOOT ERROR] Bad IRQ in XCU of cluster[");
1455                    _putd( x );
1456                    _puts(",");
1457                    _putd( y );
1458                    _puts("]\n");
1459                    _exit();
1460                }
1461
1462                _schedulers[x][y][lpid]->hwi_vector[srcid] = isr | channel | 0x80000000;
1463                lpid = (lpid + 1) % cluster[cluster_id].procs; 
1464
1465            } // end for irqs
1466        } // end if nprocs > 0
1467    } // end for clusters
1468
1469    // If there is an external PIC component, we scan HWIs connected to PIC
1470    // for Round Robin allocation (as WTI) to processors.
1471    // We allocate one WTI per processor, starting from proc[0,0,0],
1472    // and we increment (cluster_id, lpid) as required.
1473    if ( pic != NULL )
1474    {   
1475        unsigned int cluster_id = 0;   // index in clusters array
1476        unsigned int lpid       = 0;   // processor local index
1477
1478        // scan IRQS defined in PIC
1479        for ( irq_id = pic->irq_offset ;
1480              irq_id < pic->irq_offset + pic->irqs ;
1481              irq_id++ )
1482        {
1483            // compute next values for (cluster_id,lpid)
1484            // if no more procesor available in current cluster
1485            unsigned int overflow = 0;
1486            while ( (lpid >= cluster[cluster_id].procs) ||
1487                    (alloc_wti_channel[cluster_id] >= xcu->arg) )
1488            {
1489                overflow++;
1490                cluster_id = (cluster_id + 1) % (X_SIZE*Y_SIZE);
1491                lpid       = 0;
1492
1493                // overflow detection
1494                if ( overflow > (X_SIZE*Y_SIZE*NB_PROCS_MAX*32) )
1495                {
1496                    _puts("\n[BOOT ERROR] Not enough processors for external IRQs\n");
1497                    _exit();
1498                }
1499            }
1500
1501            unsigned int type    = irq[irq_id].srctype;
1502            unsigned int srcid   = irq[irq_id].srcid;
1503            unsigned int isr     = irq[irq_id].isr & 0xFFFF;
1504            unsigned int channel = irq[irq_id].channel << 16;
1505
1506            if ( (type != IRQ_TYPE_HWI) || (srcid > 31) )
1507            {
1508                _puts("\n[BOOT ERROR] Bad IRQ in PIC component\n");
1509                _exit();
1510            }
1511
1512            // get scheduler[cluster_id] address
1513            unsigned int x          = cluster[cluster_id].x;
1514            unsigned int y          = cluster[cluster_id].y;
1515            unsigned int cluster_xy = (x<<Y_WIDTH) + y;
1516            psched                  = _schedulers[x][y][lpid];
1517
1518            // update WTI vector for scheduler[cluster_id][lpid]
1519            unsigned int index            = alloc_wti_channel[cluster_id];
1520            psched->wti_vector[index]     = isr | channel | 0x80000000;
1521            alloc_wti_channel[cluster_id] = index + 1;
1522            lpid                          = lpid + 1;
1523
1524            // update IRQ fields in mapping for PIC initialisation
1525            irq[irq_id].dest_id = index;
1526            irq[irq_id].dest_xy = cluster_xy;
1527
1528        }  // end for IRQs
1529    } // end if PIC
1530               
1531#if BOOT_DEBUG_SCHED
1532for ( cluster_id = 0 ; cluster_id < (X_SIZE*Y_SIZE) ; cluster_id++ )
1533{
1534    unsigned int x          = cluster[cluster_id].x;
1535    unsigned int y          = cluster[cluster_id].y;
1536    unsigned int slot;
1537    unsigned int entry;
1538    for ( lpid = 0 ; lpid < cluster[cluster_id].procs ; lpid++ )
1539    {
1540        psched = _schedulers[x][y][lpid];
1541       
1542        _puts("\n*** IRQS for proc[");
1543        _putd( x );
1544        _puts(",");
1545        _putd( y );
1546        _puts(",");
1547        _putd( lpid );
1548        _puts("]\n");
1549        for ( slot = 0 ; slot < 32 ; slot++ )
1550        {
1551            entry = psched->hwi_vector[slot];
1552            if ( entry & 0x80000000 ) 
1553            {
1554                _puts(" - HWI ");
1555                _putd( slot );
1556                _puts(" / isrtype = ");
1557                _putd( entry & 0xFFFF ); 
1558                _puts(" / channel = ");
1559                _putd( (entry >> 16) & 0x7FFF ); 
1560                _puts("\n");
1561            }
1562        }
1563        for ( slot = 0 ; slot < 32 ; slot++ )
1564        {
1565            entry = psched->wti_vector[slot];
1566            if ( entry & 0x80000000 ) 
1567            {
1568                _puts(" - WTI ");
1569                _putd( slot );
1570                _puts(" / isrtype = ");
1571                _putd( entry & 0xFFFF ); 
1572                _puts(" / channel = ");
1573                _putd( (entry >> 16) & 0x7FFF ); 
1574                _puts("\n");
1575            }
1576        }
1577        for ( slot = 0 ; slot < 32 ; slot++ )
1578        {
1579            entry = psched->pti_vector[slot];
1580            if ( entry & 0x80000000 ) 
1581            {
1582                _puts(" - PTI ");
1583                _putd( slot );
1584                _puts(" / isrtype = ");
1585                _putd( entry & 0xFFFF ); 
1586                _puts(" / channel = ");
1587                _putd( (entry >> 16) & 0x7FFF ); 
1588                _puts("\n");
1589            }
1590        }
1591    }
1592}
1593#endif
1594
1595    ///////////////////////////////////////////////////////////////////
1596    // Step 2 : loop on the vspaces and the tasks  to complete
1597    //          the schedulers and task contexts initialisation.
1598
1599    for (vspace_id = 0; vspace_id < header->vspaces; vspace_id++) 
1600    {
1601        // We must set the PTPR depending on the vspace, because the start_vector
1602        // and the stack address are defined in virtual space.
1603        _set_mmu_ptpr( (unsigned int)(_ptabs_paddr[vspace_id][0][0] >> 13) );
1604
1605        // loop on the tasks in vspace (task_id is the global index in mapping)
1606        for (task_id = vspace[vspace_id].task_offset;
1607             task_id < (vspace[vspace_id].task_offset + vspace[vspace_id].tasks);
1608             task_id++) 
1609        {
1610            // compute the cluster coordinates & local processor index
1611            unsigned int x          = cluster[task[task_id].clusterid].x;
1612            unsigned int y          = cluster[task[task_id].clusterid].y;
1613            unsigned int cluster_xy = (x<<Y_WIDTH) + y;
1614            unsigned int lpid       = task[task_id].proclocid;                 
1615
1616#if BOOT_DEBUG_SCHED
1617_puts("\n[BOOT DEBUG] Initialise context for task ");
1618_puts( task[task_id].name );
1619_puts(" in vspace ");
1620_puts( vspace[vspace_id].name );
1621_puts("\n");
1622#endif
1623            // compute gpid (global processor index) and scheduler base address
1624            unsigned int gpid = (cluster_xy << P_WIDTH) + lpid;
1625            psched            = _schedulers[x][y][lpid];
1626
1627            // ctx_sr : value required before an eret instruction
1628            unsigned int ctx_sr = 0x0000FF13;
1629
1630            // ctx_ptpr : page table physical base address (shifted by 13 bit)
1631            unsigned int ctx_ptpr = (unsigned int)(_ptabs_paddr[vspace_id][x][y] >> 13);
1632
1633            // ctx_ptab : page_table virtual base address
1634            unsigned int ctx_ptab = _ptabs_vaddr[vspace_id][x][y];
1635
1636            // ctx_tty : TTY terminal global index provided by the global allocator
1637            //           Each user terminal is a private ressource: the number of
1638            //           requested terminal cannot be larger than NB_TTY_CHANNELS.             
1639            unsigned int ctx_tty = 0xFFFFFFFF;
1640            if (task[task_id].use_tty) 
1641            {
1642                if (alloc_tty_channel >= NB_TTY_CHANNELS) 
1643                {
1644                    _puts("\n[BOOT ERROR] TTY channel index too large for task ");
1645                    _puts(task[task_id].name);
1646                    _puts(" in vspace ");
1647                    _puts(vspace[vspace_id].name);
1648                    _puts("\n");
1649                    _exit();
1650                }
1651                ctx_tty = alloc_tty_channel;
1652                alloc_tty_channel++;
1653             }
1654
1655            // ctx_nic : NIC channel global index provided by the global allocator
1656            //           Each channel is a private ressource: the number of
1657            //           requested channels cannot be larger than NB_NIC_CHANNELS.
1658            unsigned int ctx_nic = 0xFFFFFFFF;
1659            if (task[task_id].use_nic) 
1660            {
1661                if (alloc_nic_channel >= NB_NIC_CHANNELS) 
1662                {
1663                    _puts("\n[BOOT ERROR] NIC channel index too large for task ");
1664                    _puts(task[task_id].name);
1665                    _puts(" in vspace ");
1666                    _puts(vspace[vspace_id].name);
1667                    _puts("\n");
1668                    _exit();
1669                }
1670                ctx_nic = alloc_nic_channel;
1671                alloc_nic_channel++;
1672            }
1673
1674            // ctx_cma : CMA channel global index provided by the global allocator
1675            //           Each channel is a private ressource: the number of
1676            //           requested channels cannot be larger than NB_NIC_CHANNELS.
1677            unsigned int ctx_cma = 0xFFFFFFFF;
1678            if (task[task_id].use_cma) 
1679            {
1680                if (alloc_cma_channel >= NB_CMA_CHANNELS) 
1681                {
1682                    _puts("\n[BOOT ERROR] CMA channel index too large for task ");
1683                    _puts(task[task_id].name);
1684                    _puts(" in vspace ");
1685                    _puts(vspace[vspace_id].name);
1686                    _puts("\n");
1687                    _exit();
1688                }
1689                ctx_cma = alloc_cma_channel;
1690                alloc_cma_channel++;
1691            }
1692
1693            // ctx_hba : HBA channel global index provided by the global allocator
1694            //           Each channel is a private ressource: the number of
1695            //           requested channels cannot be larger than NB_NIC_CHANNELS.
1696            unsigned int ctx_hba = 0xFFFFFFFF;
1697            if (task[task_id].use_hba) 
1698            {
1699                if (alloc_hba_channel >= NB_IOC_CHANNELS) 
1700                {
1701                    _puts("\n[BOOT ERROR] IOC channel index too large for task ");
1702                    _puts(task[task_id].name);
1703                    _puts(" in vspace ");
1704                    _puts(vspace[vspace_id].name);
1705                    _puts("\n");
1706                    _exit();
1707                }
1708                ctx_hba = alloc_hba_channel;
1709                alloc_hba_channel++;
1710            }
1711            // ctx_tim : TIMER local channel index provided by the cluster allocator
1712            //           Each timer is a private ressource
1713            unsigned int ctx_tim = 0xFFFFFFFF;
1714            if (task[task_id].use_tim) 
1715            {
1716                unsigned int cluster_id = task[task_id].clusterid;
1717
1718                if ( alloc_tim_channel[cluster_id] >= NB_TIM_CHANNELS ) 
1719                {
1720                    _puts("\n[BOOT ERROR] local TIMER index too large for task ");
1721                    _puts(task[task_id].name);
1722                    _puts(" in vspace ");
1723                    _puts(vspace[vspace_id].name);
1724                    _puts("\n");
1725                    _exit();
1726                }
1727                ctx_tim =  alloc_tim_channel[cluster_id];
1728                alloc_tim_channel[cluster_id]++;
1729            }
1730            // ctx_epc : Get the virtual address of the memory location containing
1731            // the task entry point : the start_vector is stored by GCC in the seg_data
1732            // segment and we must wait the .elf loading to get the entry point value...
1733            vobj_id = vspace[vspace_id].start_vobj_id;     
1734            unsigned int ctx_epc = vobj[vobj_id].vbase + (task[task_id].startid)*4;
1735
1736            // ctx_sp :  Get the vobj containing the stack
1737            vobj_id = task[task_id].stack_vobj_id;
1738            unsigned int ctx_sp = vobj[vobj_id].vbase + vobj[vobj_id].length;
1739
1740            // get local task index in scheduler
1741            unsigned int ltid = psched->tasks;
1742
1743            // get vspace thread index
1744            unsigned int thread_id = task[task_id].trdid;
1745
1746            if (ltid >= IDLE_TASK_INDEX) 
1747            {
1748                _puts("\n[BOOT ERROR] in boot_schedulers_init() : ");
1749                _putd( ltid );
1750                _puts(" tasks allocated to processor ");
1751                _putd( gpid );
1752                _puts(" / max is ");
1753                _putd( IDLE_TASK_INDEX );
1754                _puts("\n");
1755                _exit();
1756            }
1757
1758            // update the "tasks" and "current" fields in scheduler:
1759            // the first task to execute is task 0 as soon as there is at least
1760            // one task allocated to processor.
1761            psched->tasks   = ltid + 1;
1762            psched->current = 0;
1763
1764            // initializes the task context in scheduler
1765            psched->context[ltid][CTX_CR_ID]    = 0;
1766            psched->context[ltid][CTX_SR_ID]    = ctx_sr;
1767            psched->context[ltid][CTX_SP_ID]    = ctx_sp;
1768            psched->context[ltid][CTX_EPC_ID]   = ctx_epc;
1769            psched->context[ltid][CTX_PTPR_ID]  = ctx_ptpr;
1770            psched->context[ltid][CTX_TTY_ID]   = ctx_tty;
1771            psched->context[ltid][CTX_CMA_ID]   = ctx_cma;
1772            psched->context[ltid][CTX_HBA_ID]   = ctx_hba;
1773            psched->context[ltid][CTX_NIC_ID]   = ctx_nic;
1774            psched->context[ltid][CTX_TIM_ID]   = ctx_tim;
1775            psched->context[ltid][CTX_PTAB_ID]  = ctx_ptab;
1776            psched->context[ltid][CTX_LTID_ID]  = ltid;
1777            psched->context[ltid][CTX_GTID_ID]  = task_id;
1778            psched->context[ltid][CTX_TRDID_ID] = thread_id;
1779            psched->context[ltid][CTX_VSID_ID]  = vspace_id;
1780            psched->context[ltid][CTX_RUN_ID]   = 1;
1781
1782#if BOOT_DEBUG_SCHED
1783_puts("\nTask ");
1784_putd( task_id );
1785_puts(" allocated to processor[");
1786_putd( x );
1787_puts(",");
1788_putd( y );
1789_puts(",");
1790_putd( lpid );
1791_puts("]\n  - ctx[LTID]   = ");
1792_putd( psched->context[ltid][CTX_LTID_ID] );
1793_puts("\n  - ctx[SR]     = ");
1794_putx( psched->context[ltid][CTX_SR_ID] );
1795_puts("\n  - ctx[SP]     = ");
1796_putx( psched->context[ltid][CTX_SP_ID] );
1797_puts("\n  - ctx[EPC]    = ");
1798_putx( psched->context[ltid][CTX_EPC_ID] );
1799_puts("\n  - ctx[PTPR]   = ");
1800_putx( psched->context[ltid][CTX_PTPR_ID] );
1801_puts("\n  - ctx[TTY]    = ");
1802_putx( psched->context[ltid][CTX_TTY_ID] );
1803_puts("\n  - ctx[NIC]    = ");
1804_putx( psched->context[ltid][CTX_NIC_ID] );
1805_puts("\n  - ctx[CMA]    = ");
1806_putx( psched->context[ltid][CTX_CMA_ID] );
1807_puts("\n  - ctx[IOC]    = ");
1808_putx( psched->context[ltid][CTX_HBA_ID] );
1809_puts("\n  - ctx[TIM]    = ");
1810_putx( psched->context[ltid][CTX_TIM_ID] );
1811_puts("\n  - ctx[PTAB]   = ");
1812_putx( psched->context[ltid][CTX_PTAB_ID] );
1813_puts("\n  - ctx[GTID]   = ");
1814_putx( psched->context[ltid][CTX_GTID_ID] );
1815_puts("\n  - ctx[VSID]   = ");
1816_putx( psched->context[ltid][CTX_VSID_ID] );
1817_puts("\n  - ctx[TRDID]  = ");
1818_putx( psched->context[ltid][CTX_TRDID_ID] );
1819_puts("\n");
1820#endif
1821
1822        } // end loop on tasks
1823    } // end loop on vspaces
1824} // end boot_schedulers_init()
1825
1826//////////////////////////////////////////////////////////////////////////////////
1827// This function loads the map.bin file from block device.
1828//////////////////////////////////////////////////////////////////////////////////
1829void boot_mapping_init()
1830{
1831    // desactivates IOC interrupt
1832    _ioc_init( 0 );
1833
1834    // open file "map.bin"
1835    int fd_id = _fat_open( IOC_BOOT_MODE,
1836                           "map.bin",
1837                           0 );         // no creation
1838    if ( fd_id == -1 )
1839    {
1840        _puts("\n[BOOT ERROR] : map.bin file not found \n");
1841        _exit();
1842    }
1843
1844#if BOOT_DEBUG_MAPPING
1845_puts("\n[BOOT] map.bin file successfully open at cycle ");
1846_putd(_get_proctime());
1847_puts("\n");
1848#endif
1849
1850    // get "map.bin" file size (from fat) and check it
1851    unsigned int size    = fat.fd[fd_id].file_size;
1852
1853    if ( size > SEG_BOOT_MAPPING_SIZE )
1854    {
1855        _puts("\n[BOOT ERROR] : allocated segment too small for map.bin file\n");
1856        _exit();
1857    }
1858
1859    // load "map.bin" file into buffer
1860    unsigned int nblocks = size >> 9;
1861    unsigned int offset  = size & 0x1FF;
1862    if ( offset ) nblocks++;
1863
1864    unsigned int ok = _fat_read( IOC_BOOT_MODE,
1865                                 fd_id, 
1866                                 (unsigned int*)SEG_BOOT_MAPPING_BASE, 
1867                                 nblocks,       
1868                                 0 );      // offset
1869    if ( ok == -1 )
1870    {
1871        _puts("\n[BOOT ERROR] : unable to load map.bin file \n");
1872        _exit();
1873    }
1874    _fat_close( fd_id );
1875   
1876    // close file "map.bin"
1877    boot_mapping_check();
1878
1879} // end boot_mapping_init()
1880
1881
1882/////////////////////////////////////////////////////////////////////////////////////
1883// This function load all loadable segments for one .elf file, identified
1884// by the "pathname" argument. Some loadable segments can be copied in several
1885// clusters: same virtual address but different physical addresses. 
1886// - It open the file.
1887// - It loads the complete file in the dedicated boot_elf_buffer.
1888// - It copies each loadable segments  at the virtual address defined in
1889//   the .elf file, making several copies if the target vseg is not local.
1890// - It closes the file.
1891// This function is supposed to be executed by processor[0,0,0].
1892// Note:
1893//   We must use physical addresses to reach the destination buffers that
1894//   can be located in remote clusters. We use either a _physical_memcpy(),
1895//   or a _dma_physical_copy() if DMA is available.
1896//////////////////////////////////////////////////////////////////////////////////////
1897void load_one_elf_file( unsigned int is_kernel,     // kernel file if non zero
1898                        char*        pathname,
1899                        unsigned int vspace_id )    // to scan the proper vspace
1900{
1901    mapping_header_t  * header  = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
1902    mapping_vspace_t  * vspace  = _get_vspace_base(header);
1903    mapping_vseg_t    * vseg    = _get_vseg_base(header);
1904    mapping_vobj_t    * vobj    = _get_vobj_base(header);
1905
1906    unsigned int seg_id;
1907
1908#if BOOT_DEBUG_ELF
1909_puts("\n[BOOT DEBUG] Start searching file ");
1910_puts( pathname );
1911_puts(" at cycle ");
1912_putd( _get_proctime() );
1913_puts("\n");
1914#endif
1915
1916    // open .elf file
1917    int fd_id = _fat_open( IOC_BOOT_MODE,
1918                           pathname,
1919                           0 );      // no creation
1920    if ( fd_id < 0 )
1921    {
1922        _puts("\n[BOOT ERROR] load_one_elf_file() : ");
1923        _puts( pathname );
1924        _puts(" not found\n");
1925        _exit();
1926    }
1927
1928    // check buffer size versus file size
1929    if ( fat.fd[fd_id].file_size > GIET_ELF_BUFFER_SIZE )
1930    {
1931        _puts("\n[BOOT ERROR] load_one_elf_file() : ");
1932        _puts( pathname );
1933        _puts(" exceeds the GIET_ELF_BUFFERSIZE defined in giet_config.h\n");
1934        _exit();
1935    }
1936
1937    // compute number of sectors
1938    unsigned int nbytes   = fat.fd[fd_id].file_size;
1939    unsigned int nsectors = nbytes>>9;
1940    if( nbytes & 0x1FF) nsectors++;
1941
1942    // load file in elf buffer
1943    if( _fat_read( IOC_BOOT_MODE, 
1944                   fd_id, 
1945                   boot_elf_buffer,
1946                   nsectors,
1947                   0 ) != nsectors )
1948    {
1949        _puts("\n[BOOT ERROR] load_one_elf_file() : unexpected EOF for file ");
1950        _puts( pathname );
1951        _puts("\n");   
1952        _exit();
1953    }
1954
1955    // Check ELF Magic Number in ELF header
1956    Elf32_Ehdr* elf_header_ptr = (Elf32_Ehdr*)boot_elf_buffer;
1957
1958    if ( (elf_header_ptr->e_ident[EI_MAG0] != ELFMAG0) ||
1959         (elf_header_ptr->e_ident[EI_MAG1] != ELFMAG1) ||
1960         (elf_header_ptr->e_ident[EI_MAG2] != ELFMAG2) ||
1961         (elf_header_ptr->e_ident[EI_MAG3] != ELFMAG3) )
1962    {
1963        _puts("\n[BOOT ERROR] load_elf() : file ");
1964        _puts( pathname );
1965        _puts(" does not use ELF format\n");   
1966        _exit();
1967    }
1968
1969    // get program header table pointer
1970    unsigned int pht_index = elf_header_ptr->e_phoff;
1971    if( pht_index == 0 )
1972    {
1973        _puts("\n[BOOT ERROR] load_one_elf_file() : file ");
1974        _puts( pathname );
1975        _puts(" does not contain loadable segment\n");   
1976        _exit();
1977    }
1978    Elf32_Phdr* elf_pht_ptr = (Elf32_Phdr*)(boot_elf_buffer + pht_index);
1979
1980    // get number of segments
1981    unsigned int nsegments   = elf_header_ptr->e_phnum;
1982
1983    _puts("\n[BOOT] File ");
1984    _puts( pathname );
1985    _puts(" loaded at cycle ");
1986    _putd( _get_proctime() );
1987    _puts("\n");
1988
1989    // Loop on loadable segments in the .elf file
1990    for (seg_id = 0 ; seg_id < nsegments ; seg_id++)
1991    {
1992        if(elf_pht_ptr[seg_id].p_type == PT_LOAD)
1993        {
1994            // Get segment attributes
1995            unsigned int seg_vaddr  = elf_pht_ptr[seg_id].p_vaddr;
1996            unsigned int seg_offset = elf_pht_ptr[seg_id].p_offset;
1997            unsigned int seg_filesz = elf_pht_ptr[seg_id].p_filesz;
1998            unsigned int seg_memsz  = elf_pht_ptr[seg_id].p_memsz;
1999
2000#if BOOT_DEBUG_ELF
2001_puts(" - segment ");
2002_putd( seg_id );
2003_puts(" / vaddr = ");
2004_putx( seg_vaddr );
2005_puts(" / file_size = ");
2006_putx( seg_filesz );
2007_puts("\n");
2008#endif
2009
2010            if( seg_memsz < seg_filesz )
2011            {
2012                _puts("\n[BOOT ERROR] load_one_elf_file() : segment at vaddr = ");
2013                _putx( seg_vaddr );
2014                _puts(" in file ");
2015                _puts( pathname );
2016                _puts(" has memsz < filesz \n");   
2017                _exit();
2018            }
2019
2020            // fill empty space with 0 as required
2021            if( seg_memsz > seg_filesz )
2022            {
2023                unsigned int i; 
2024                for( i = seg_filesz ; i < seg_memsz ; i++ ) boot_elf_buffer[i+seg_offset] = 0;
2025            } 
2026
2027            unsigned int src_vaddr = (unsigned int)boot_elf_buffer + seg_offset;
2028
2029            // search all vsegs matching the virtual address
2030            unsigned int vseg_first;
2031            unsigned int vseg_last;
2032            unsigned int vseg_id;
2033            unsigned int found = 0;
2034            if ( is_kernel )
2035            {
2036                vseg_first = 0;
2037                vseg_last  = header->globals;
2038            }
2039            else
2040            {
2041                vseg_first = vspace[vspace_id].vseg_offset;
2042                vseg_last  = vseg_first + vspace[vspace_id].vsegs;
2043            }
2044
2045            for ( vseg_id = vseg_first ; vseg_id < vseg_last ; vseg_id++ )
2046            {
2047                if ( seg_vaddr == vseg[vseg_id].vbase )  // matching
2048                {
2049                    found = 1;
2050
2051                    // get destination buffer physical address and size
2052                    paddr_t      seg_paddr  = vseg[vseg_id].pbase;
2053                    unsigned int vobj_id    = vseg[vseg_id].vobj_offset;
2054                    unsigned int seg_size   = vobj[vobj_id].length;
2055                   
2056#if BOOT_DEBUG_ELF
2057_puts("   loaded into vseg ");
2058_puts( vseg[vseg_id].name );
2059_puts(" at paddr = ");
2060_putl( seg_paddr );
2061_puts(" (buffer size = ");
2062_putx( seg_size );
2063_puts(")\n");
2064#endif
2065                    // check vseg size
2066                    if ( seg_size < seg_filesz )
2067                    {
2068                        _puts("\n[BOOT ERROR] in load_one_elf_file()\n");
2069                        _puts("vseg ");
2070                        _puts( vseg[vseg_id].name );
2071                        _puts(" is to small for loadable segment ");
2072                        _putx( seg_vaddr );
2073                        _puts(" in file ");
2074                        _puts( pathname );
2075                        _puts(" \n");   
2076                        _exit();
2077                    }
2078
2079                    // copy the segment from boot buffer to destination buffer
2080                    // using DMA channel[0,0,0] if it is available.
2081                    if( NB_DMA_CHANNELS > 0 )
2082                    {
2083                        _dma_physical_copy( 0,                  // DMA in cluster[0,0]
2084                                            0,                  // DMA channel 0
2085                                            (paddr_t)seg_paddr, // destination paddr
2086                                            (paddr_t)src_vaddr, // source paddr
2087                                            seg_filesz );       // size
2088                    }
2089                    else
2090                    {
2091                        _physical_memcpy( (paddr_t)seg_paddr,   // destination paddr
2092                                          (paddr_t)src_vaddr,   // source paddr
2093                                          seg_filesz );         // size
2094                    }
2095                }
2096            }  // end for vsegs in vspace
2097
2098            // check at least one matching vseg
2099            if ( found == 0 )
2100            {
2101                _puts("\n[BOOT ERROR] in load_one_elf_file()\n");
2102                _puts("vseg for loadable segment ");
2103                _putx( seg_vaddr );
2104                _puts(" in file ");
2105                _puts( pathname );
2106                _puts(" not found \n");   
2107                _exit();
2108            }
2109        }
2110    }  // end for loadable segments
2111
2112    // close .elf file
2113    _fat_close( fd_id );
2114
2115} // end load_one_elf_file()
2116
2117
2118/////i////////////////////////////////////////////////////////////////////////////////
2119// This function uses the map.bin data structure to load the "kernel.elf" file
2120// as well as the various "application.elf" files into memory.
2121// - The "preloader.elf" file is not loaded, because it has been burned in the ROM.
2122// - The "boot.elf" file is not loaded, because it has been loaded by the preloader.
2123// This function scans all vobjs defined in the map.bin data structure to collect
2124// all .elf files pathnames, and calls the load_one_elf_file() for each .elf file.
2125// As the code can be replicated in several vsegs, the same code can be copied
2126// in one or several clusters by the load_one_elf_file() function.
2127//////////////////////////////////////////////////////////////////////////////////////
2128void boot_elf_load()
2129{
2130    mapping_header_t* header = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
2131    mapping_vspace_t* vspace = _get_vspace_base( header );
2132    mapping_vobj_t*   vobj   = _get_vobj_base( header );
2133    unsigned int      vspace_id;
2134    unsigned int      vobj_id;
2135    unsigned int      found;
2136
2137    // Scan all vobjs corresponding to global vsegs,
2138    // to find the pathname to the kernel.elf file
2139    found = 0;
2140    for( vobj_id = 0 ; vobj_id < header->globals ; vobj_id++ )
2141    {
2142        if(vobj[vobj_id].type == VOBJ_TYPE_ELF) 
2143        {   
2144            found = 1;
2145            break;
2146        }
2147    }
2148
2149    // We need one kernel.elf file
2150    if (found == 0)
2151    {
2152        _puts("[BOOT ERROR] boot_elf_load() : kernel.elf file not found\n");
2153        _exit();
2154    }
2155
2156    // Load the kernel
2157    load_one_elf_file( 1,                           // kernel file
2158                       vobj[vobj_id].binpath,       // file pathname
2159                       0 );                         // vspace 0
2160
2161    // loop on the vspaces, scanning all vobjs in the vspace,
2162    // to find the pathname of the .elf file associated to the vspace.
2163    for( vspace_id = 0 ; vspace_id < header->vspaces ; vspace_id++ )
2164    {
2165        // loop on the vobjs in vspace (vobj_id is the global index)
2166        unsigned int found = 0;
2167        for (vobj_id = vspace[vspace_id].vobj_offset;
2168             vobj_id < (vspace[vspace_id].vobj_offset + vspace[vspace_id].vobjs);
2169             vobj_id++) 
2170        {
2171            if(vobj[vobj_id].type == VOBJ_TYPE_ELF) 
2172            {   
2173                found = 1;
2174                break;
2175            }
2176        }
2177
2178        // We want one .elf file per vspace
2179        if (found == 0)
2180        {
2181            _puts("[BOOT ERROR] boot_elf_load() : .elf file not found for vspace ");
2182            _puts( vspace[vspace_id].name );
2183            _puts("\n");
2184            _exit();
2185        }
2186
2187        load_one_elf_file( 0,                          // not a kernel file
2188                           vobj[vobj_id].binpath,      // file pathname
2189                           vspace_id );                // vspace index
2190
2191    }  // end for vspaces
2192
2193} // end boot_elf_load()
2194
2195////////////////////////////////////////////////////////////////////////////////
2196// This function intializes the periherals and coprocessors, as specified
2197// in the mapping_info file.
2198////////////////////////////////////////////////////////////////////////////////
2199void boot_peripherals_init() 
2200{
2201    mapping_header_t * header   = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
2202    mapping_cluster_t * cluster = _get_cluster_base(header);
2203    mapping_periph_t * periph   = _get_periph_base(header);
2204    mapping_vobj_t * vobj       = _get_vobj_base(header);
2205    mapping_coproc_t * coproc   = _get_coproc_base(header);
2206    mapping_cp_port_t * cp_port = _get_cp_port_base(header);
2207    mapping_irq_t * irq         = _get_irq_base(header);
2208
2209    unsigned int cluster_id;
2210    unsigned int periph_id;
2211    unsigned int coproc_id;
2212    unsigned int cp_port_id;
2213    unsigned int channel_id;
2214
2215    // loop on all physical clusters
2216    for (cluster_id = 0; cluster_id < X_SIZE*Y_SIZE; cluster_id++) 
2217    {
2218        // computes cluster coordinates
2219        unsigned int x          = cluster[cluster_id].x;
2220        unsigned int y          = cluster[cluster_id].y;
2221        unsigned int cluster_xy = (x<<Y_WIDTH) + y;
2222
2223#if BOOT_DEBUG_PERI
2224_puts("\n[BOOT DEBUG] Peripherals initialisation in cluster[");
2225_putd( x );
2226_puts(",");
2227_putd( y );
2228_puts("]\n");
2229#endif
2230
2231        // loop on peripherals
2232        for (periph_id = cluster[cluster_id].periph_offset;
2233             periph_id < cluster[cluster_id].periph_offset +
2234             cluster[cluster_id].periphs; periph_id++) 
2235        {
2236            unsigned int type       = periph[periph_id].type;
2237            unsigned int subtype    = periph[periph_id].subtype;
2238            unsigned int channels   = periph[periph_id].channels;
2239
2240            switch (type) 
2241            {
2242                case PERIPH_TYPE_IOC:    // vci_block_device component
2243                {
2244                    if ( subtype == PERIPH_SUBTYPE_BDV )
2245                    {
2246                        _bdv_lock.value = 0;
2247#if BOOT_DEBUG_PERI
2248_puts("- BDV : channels = ");
2249_putd(channels);
2250_puts("\n");
2251#endif
2252                    }
2253                    else if ( subtype == PERIPH_SUBTYPE_HBA )
2254                    {
2255                        // TODO
2256                    }
2257                    else if ( subtype == PERIPH_SUBTYPE_SPI )
2258                    {
2259                        // TODO
2260                    }
2261                    break;
2262                }
2263                case PERIPH_TYPE_CMA:    // vci_chbuf_dma component
2264                {
2265                    for (channel_id = 0; channel_id < channels; channel_id++) 
2266                    {
2267                        // TODO
2268                    }
2269#if BOOT_DEBUG_PERI
2270_puts("- CMA : channels = ");
2271_putd(channels);
2272_puts("\n");
2273#endif
2274                    break;
2275                }
2276                case PERIPH_TYPE_NIC:    // vci_multi_nic component
2277                {
2278                    for (channel_id = 0; channel_id < channels; channel_id++) 
2279                    {
2280                        // TODO
2281                    }
2282#if BOOT_DEBUG_PERI
2283_puts("- NIC : channels = ");
2284_putd(channels);
2285_puts("\n");
2286#endif
2287                    break;
2288                }
2289                case PERIPH_TYPE_TTY:    // vci_multi_tty component
2290                {
2291                    for (channel_id = 0; channel_id < channels; channel_id++) 
2292                    {
2293                        _tty_lock[channel_id].value = 0;
2294                        _tty_rx_full[channel_id]    = 0;
2295                    }
2296#if BOOT_DEBUG_PERI
2297_puts("- TTY : channels = ");
2298_putd(channels);
2299_puts("\n");
2300#endif
2301                    break;
2302                }
2303                case PERIPH_TYPE_IOB:    // vci_io_bridge component
2304                {
2305                    if (GIET_USE_IOMMU) 
2306                    {
2307                        // TODO
2308                        // get the iommu page table physical address
2309                        // set IOMMU page table address
2310                        // pseg_base[IOB_IOMMU_PTPR] = ptab_pbase;   
2311                        // activate IOMMU
2312                        // pseg_base[IOB_IOMMU_ACTIVE] = 1;       
2313                    }
2314                    break;
2315                }
2316                case PERIPH_TYPE_PIC:    // vci_iopic component
2317                {
2318#if BOOT_DEBUG_PERI
2319_puts("- PIC : channels = ");
2320_putd(channels);
2321_puts("\n");
2322#endif
2323                    // scan all IRQs defined in mapping for PIC component,
2324                    // and initialises addresses for WTI IRQs
2325                    for ( channel_id = periph[periph_id].irq_offset ;
2326                          channel_id < periph[periph_id].irq_offset + periph[periph_id].irqs ;
2327                          channel_id++ )
2328                    {
2329                        unsigned int hwi_id     = irq[channel_id].srcid;   // HWI index in PIC
2330                        unsigned int wti_id     = irq[channel_id].dest_id; // WTI index in XCU
2331                        unsigned int cluster_xy = irq[channel_id].dest_xy; // XCU coordinates
2332                        unsigned int vaddr;
2333
2334                        _xcu_get_wti_address( wti_id, &vaddr );
2335                        _pic_init( hwi_id, vaddr, cluster_xy ); 
2336
2337#if BOOT_DEBUG_PERI
2338unsigned int address = _pic_get_register( channel_id, IOPIC_ADDRESS );
2339unsigned int extend  = _pic_get_register( channel_id, IOPIC_EXTEND  );
2340_puts("    hwi_index = ");
2341_putd( hwi_id );
2342_puts(" / wti_index = ");
2343_putd( wti_id );
2344_puts(" / vaddr = ");
2345_putx( vaddr );
2346_puts(" in cluster[");
2347_putd( cluster_xy >> Y_WIDTH );
2348_puts(",");
2349_putd( cluster_xy & ((1<<Y_WIDTH)-1) );
2350_puts("] / checked_xcu_paddr = ");
2351_putl( (paddr_t)address + (((paddr_t)extend)<<32) );
2352_puts("\n");
2353#endif
2354                    }
2355                    break;
2356                }
2357            }  // end switch periph type
2358        }  // end for periphs
2359
2360#if BOOT_DEBUG_PERI
2361_puts("\n[BOOT DEBUG] Coprocessors initialisation in cluster[");
2362_putd( x );
2363_puts(",");
2364_putd( y );
2365_puts("]\n");
2366#endif
2367
2368        // loop on coprocessors
2369        for ( coproc_id = cluster[cluster_id].coproc_offset;
2370              coproc_id < cluster[cluster_id].coproc_offset +
2371              cluster[cluster_id].coprocs; coproc_id++ ) 
2372        {
2373
2374#if BOOT_DEBUG_PERI
2375_puts("- coprocessor name : ");
2376_puts(coproc[coproc_id].name);
2377_puts(" / nb ports = ");
2378_putd((unsigned int) coproc[coproc_id].ports);
2379_puts("\n");
2380#endif
2381            // loop on the coprocessor ports
2382            for ( cp_port_id = coproc[coproc_id].port_offset;
2383                  cp_port_id < coproc[coproc_id].port_offset + coproc[coproc_id].ports;
2384                  cp_port_id++ ) 
2385            {
2386                // get global index of associted vobj
2387                unsigned int vobj_id   = cp_port[cp_port_id].mwmr_vobj_id; 
2388
2389                // get MWMR channel base address
2390                page_table_t* ptab  = (page_table_t*)_ptabs_vaddr[0][x][y];
2391                unsigned int  vbase = vobj[vobj_id].vbase;
2392                unsigned int  ppn;
2393                unsigned int  flags;
2394                paddr_t       pbase;
2395
2396                _v2p_translate( ptab, 
2397                                vbase>>12 , 
2398                                &ppn, 
2399                                &flags );
2400
2401                pbase = ((paddr_t)ppn)<<12;
2402
2403                // initialise cp_port
2404                _mwr_hw_init( cluster_xy,
2405                              cp_port_id, 
2406                              cp_port[cp_port_id].direction, 
2407                              pbase );
2408#if BOOT_DEBUG_PERI
2409_puts("     port direction: ");
2410_putd( (unsigned int)cp_port[cp_port_id].direction );
2411_puts(" / mwmr_channel_pbase = ");
2412_putl( pbase );
2413_puts(" / name = ");
2414_puts(vobj[vobj_id].name);
2415_puts("\n"); 
2416#endif
2417            } // end for cp_ports
2418        } // end for coprocs
2419    } // end for clusters
2420} // end boot_peripherals_init()
2421
2422/////////////////////////////////////////////////////////////////////////
2423// This function initialises the physical memory allocators in each
2424// cluster containing a RAM pseg.
2425/////////////////////////////////////////////////////////////////////////
2426void boot_pmem_init() 
2427{
2428    mapping_header_t*  header     = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
2429    mapping_cluster_t* cluster    = _get_cluster_base(header);
2430    mapping_pseg_t*    pseg       = _get_pseg_base(header);
2431
2432    unsigned int cluster_id;
2433    unsigned int pseg_id;
2434
2435    // scan all clusters
2436    for ( cluster_id = 0 ; cluster_id < X_SIZE*Y_SIZE ; cluster_id++ ) 
2437    {
2438        // scan the psegs in cluster to find first pseg of type RAM
2439        unsigned int pseg_min = cluster[cluster_id].pseg_offset;
2440        unsigned int pseg_max = pseg_min + cluster[cluster_id].psegs;
2441        for ( pseg_id = pseg_min ; pseg_id < pseg_max ; pseg_id++ )
2442        {
2443            if ( pseg[pseg_id].type == PSEG_TYPE_RAM )
2444            {
2445                unsigned int x    = cluster[cluster_id].x;
2446                unsigned int y    = cluster[cluster_id].y;
2447                unsigned int base = (unsigned int)pseg[pseg_id].base;
2448                unsigned int size = (unsigned int)pseg[pseg_id].length;
2449                _pmem_alloc_init( x, y, base, size );
2450
2451#if BOOT_DEBUG_PT
2452_puts("\n[BOOT DEBUG] pmem allocator initialised in cluster[");
2453_putd( x );
2454_puts(",");
2455_putd( y );
2456_puts("] base = ");
2457_putx( base );
2458_puts(" / size = ");
2459_putx( size );
2460_puts("\n");
2461#endif
2462               break;
2463            }
2464        }
2465    }
2466} // end boot_pmem_init()
2467 
2468/////////////////////////////////////////////////////////////////////////
2469// This function is the entry point of the boot code for all processors.
2470// Most of this code is executed by Processor 0 only.
2471/////////////////////////////////////////////////////////////////////////
2472void boot_init() 
2473{
2474    mapping_header_t*  header     = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
2475    mapping_cluster_t* cluster    = _get_cluster_base(header);
2476    unsigned int       gpid       = _get_procid();
2477 
2478    if ( gpid == 0 )    // only Processor 0 does it
2479    {
2480        _puts("\n[BOOT] boot_init start at cycle ");
2481        _putd(_get_proctime());
2482        _puts("\n");
2483
2484        // Load the map.bin file into memory and check it
2485        boot_mapping_init();
2486
2487        _puts("\n[BOOT] Mapping \"");
2488        _puts( header->name );
2489        _puts("\" loaded at cycle ");
2490        _putd(_get_proctime());
2491        _puts("\n");
2492
2493        // Initializes the physical memory allocators
2494        boot_pmem_init();
2495
2496        _puts("\n[BOOT] Physical memory allocators initialised at cycle ");
2497        _putd(_get_proctime());
2498        _puts("\n");
2499
2500        // Build page tables
2501        _ptabs_init();
2502
2503        _puts("\n[BOOT] Page tables initialised at cycle ");
2504        _putd(_get_proctime());
2505        _puts("\n");
2506
2507        // Activate MMU for proc [0,0,0]
2508        _set_mmu_ptpr( (unsigned int)(_ptabs_paddr[0][0][0]>>13) );
2509        _set_mmu_mode( 0xF );
2510
2511        _puts("\n[BOOT] Processor[0,0,0] : MMU activation at cycle ");
2512        _putd(_get_proctime());
2513        _puts("\n");
2514
2515        // Initialise private vobjs in vspaces
2516        boot_vobjs_init();
2517
2518        _puts("\n[BOOT] Private vobjs initialised at cycle ");
2519        _putd(_get_proctime());
2520        _puts("\n");
2521
2522        // Initialise schedulers
2523        boot_schedulers_init();
2524
2525        _puts("\n[BOOT] Schedulers initialised at cycle ");
2526        _putd(_get_proctime());
2527        _puts("\n");
2528       
2529        // Set CP0_SCHED register for proc [0,0,0]
2530        _set_sched( (unsigned int)_schedulers[0][0][0] );
2531
2532        // Initialise non replicated peripherals
2533        boot_peripherals_init();
2534
2535        _puts("\n[BOOT] Non replicated peripherals initialised at cycle ");
2536        _putd(_get_proctime());
2537        _puts("\n");
2538
2539        // Loading all .elf files
2540        boot_elf_load();
2541
2542        // P0 starts all other processors
2543        unsigned int clusterid, p;
2544
2545        for ( clusterid = 0 ; clusterid < X_SIZE*Y_SIZE ; clusterid++ ) 
2546        {
2547            unsigned int nprocs     = cluster[clusterid].procs;
2548            unsigned int x          = cluster[clusterid].x;
2549            unsigned int y          = cluster[clusterid].y;
2550            unsigned int cluster_xy = (x<<Y_WIDTH) + y;
2551
2552            for ( p = 0 ; p < nprocs; p++ ) 
2553            {
2554                if ( (nprocs > 0) && ((clusterid != 0) || (p != 0)) )
2555                {
2556                    _xcu_send_wti( cluster_xy, p, (unsigned int)boot_entry );
2557                }
2558            }
2559        }
2560 
2561    }  // end monoprocessor boot
2562
2563    ///////////////////////////////////////////////////////////////////////////////
2564    //            Parallel execution starts actually here
2565    ///////////////////////////////////////////////////////////////////////////////
2566
2567    // all processor initialise the SCHED register
2568    // from the _schedulers[x][y][lpid array]
2569    unsigned int cluster_xy = gpid >> P_WIDTH;
2570    unsigned int lpid       = gpid & ((1<<P_WIDTH)-1);
2571    unsigned int x          = cluster_xy >> Y_WIDTH;
2572    unsigned int y          = cluster_xy & ((1<<Y_WIDTH)-1);
2573    _set_sched( (unsigned int)_schedulers[x][y][lpid] );
2574
2575    // all processors (but Proc[0,0,0]) activate MMU
2576    if ( gpid != 0 )
2577    {
2578        _set_mmu_ptpr( (unsigned int)(_ptabs_paddr[0][x][y]>>13) );
2579        _set_mmu_mode( 0xF );
2580    }
2581
2582    // all processors reset BEV bit in the status register to use
2583    // the GIET_VM exception handler instead of the PRELOADER exception handler
2584    _set_sr( 0 );
2585
2586    // all processors jump to kernel_init
2587    // using the address defined in the giet_vsegs.ld file
2588    unsigned int kernel_entry = (unsigned int)&kernel_init_vbase;
2589    asm volatile( "jr   %0" ::"r"(kernel_entry) );
2590
2591} // end boot_init()
2592
2593
2594// Local Variables:
2595// tab-width: 4
2596// c-basic-offset: 4
2597// c-file-offsets:((innamespace . 0)(inline-open . 0))
2598// indent-tabs-mode: nil
2599// End:
2600// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
2601
Note: See TracBrowser for help on using the repository browser.