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

Last change on this file since 433 was 433, checked in by cfuguet, 10 years ago

boot: two modifications in the GietVM bootloader

  1. Verifying that all vsegs sharing a big page have the same flags.
  1. Bugfixes in the _physical_memset() function
File size: 92.7 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                        // test if new vseg has the same mode bits than
575                        // the other vsegs in the same big page
576                        unsigned int pte1_mode = 0;
577                        if (pte1 & PTE_C) pte1_mode |= C_MODE_MASK;
578                        if (pte1 & PTE_X) pte1_mode |= X_MODE_MASK;
579                        if (pte1 & PTE_W) pte1_mode |= W_MODE_MASK;
580                        if (pte1 & PTE_U) pte1_mode |= U_MODE_MASK;
581                        if (vseg->mode != pte1_mode) {
582                            _puts("\n[BOOT ERROR] in boot_vseg_map() : vseg ");
583                            _puts( vseg->name );
584                            _puts(" has different flags (");
585                            _putx( vseg->mode );
586                            _puts(") than other vsegs sharing the same big page (");
587                            _putx( pte1_mode );
588                            _puts(")");
589                            _exit();
590                        }
591
592                        ppn = ((pte1 << 9) & 0x0FFFFE00);
593                    }
594                }
595                ppn = ppn | (vpn & 0x1FF);
596            }
597        }
598        else                    // PERI : no memory allocation required
599        {
600            ppn = pseg->base >> 12;
601        }
602    }
603
604    // update vseg.pbase field and update vsegs chaining
605    vseg->pbase     = ((paddr_t)ppn) << 12;
606    vseg->next_vseg = pseg->next_vseg;
607    pseg->next_vseg = (unsigned int)vseg;
608
609
610    //////////// Third step : (only if the vseg is a page table)
611    //////////// - compute the physical & virtual base address for each vspace
612    ////////////   by dividing the vseg in several sub-segments.
613    //////////// - register it in _ptabs_vaddr & _ptabs_paddr arrays,
614    ////////////   and initialize next_pt2 allocators.
615    //////////// - reset all entries in first level page tables
616   
617    if ( is_ptab )
618    {
619        unsigned int   vs;        // vspace index
620        unsigned int   nspaces;   // number of vspaces
621        unsigned int   nsp;       // number of small pages for one PTAB
622        unsigned int   offset;    // address offset for current PTAB
623
624        nspaces = header->vspaces;
625        offset  = 0;
626
627        // each PTAB must be aligned on a 8 Kbytes boundary
628        nsp = ( vseg->length >> 12 ) / nspaces;
629        if ( (nsp & 0x1) == 0x1 ) nsp = nsp - 1;
630
631        // compute max_pt2
632        _ptabs_max_pt2 = ((nsp<<12) - PT1_SIZE) / PT2_SIZE;
633
634        for ( vs = 0 ; vs < nspaces ; vs++ )
635        {
636            _ptabs_vaddr   [vs][x_dest][y_dest] = (vpn + offset) << 12;
637            _ptabs_paddr   [vs][x_dest][y_dest] = ((paddr_t)(ppn + offset)) << 12;
638            _ptabs_next_pt2[vs][x_dest][y_dest] = 0;
639            offset += nsp;
640
641            // reset all entries in PT1 (8 Kbytes)
642            _physical_memset( _ptabs_paddr[vs][x_dest][y_dest], PT1_SIZE, 0 );
643        }
644    }
645
646#if BOOT_DEBUG_PT
647_puts("[BOOT DEBUG] ");
648_puts( vseg->name );
649_puts(" in cluster[");
650_putd( x_dest );
651_puts(",");
652_putd( y_dest );
653_puts("] : vbase = ");
654_putx( vseg->vbase );
655_puts(" / length = ");
656_putx( vseg->length );
657if ( big ) _puts(" / BIG   / npages = ");
658else       _puts(" / SMALL / npages = ");
659_putd( npages );
660_puts(" / pbase = ");
661_putl( vseg->pbase );
662_puts("\n");
663#endif
664
665} // end boot_vseg_map()
666
667/////////////////////////////////////////////////////////////////////////////////////
668// For the vseg defined by the vseg pointer, this function register all PTEs
669// in one or several page tables.
670// It is a global vseg (system vseg) if (vspace_id == 0xFFFFFFFF).
671// The number of involved PTABs depends on the "local" and "global" attributes:
672//  - PTEs are replicated in all vspaces for a global vseg.
673//  - PTEs are replicated in all clusters for a non local vseg.
674/////////////////////////////////////////////////////////////////////////////////////
675void boot_vseg_pte( mapping_vseg_t*  vseg,
676                    unsigned int     vspace_id )
677{
678    // compute the "global" vseg attribute and actual vspace index
679    unsigned int        global;
680    unsigned int        vsid;   
681    if ( vspace_id == 0xFFFFFFFF )
682    {
683        global = 1;
684        vsid   = 0;
685    }
686    else
687    {
688        global = 0;
689        vsid   = vspace_id;
690    }
691
692    // compute the "local" and "big" attributes
693    unsigned int        local  = vseg->local;
694    unsigned int        big    = vseg->big;
695
696    // compute vseg flags
697    // The three flags (Local, Remote and Dirty) are set to 1 to reduce
698    // latency of TLB miss (L/R) and write (D): Avoid hardware update
699    // mechanism for these flags because GIET_VM does use these flags.
700    unsigned int flags = 0;
701    if (vseg->mode & C_MODE_MASK) flags |= PTE_C;
702    if (vseg->mode & X_MODE_MASK) flags |= PTE_X;
703    if (vseg->mode & W_MODE_MASK) flags |= PTE_W;
704    if (vseg->mode & U_MODE_MASK) flags |= PTE_U;
705    if ( global )                 flags |= PTE_G;
706                                  flags |= PTE_L;
707                                  flags |= PTE_R;
708                                  flags |= PTE_D;
709
710    // compute VPN, PPN and number of pages (big or small)
711    unsigned int vpn     = vseg->vbase >> 12;
712    unsigned int vpn_max = (vseg->vbase + vseg->length - 1) >> 12;
713    unsigned int ppn     = (unsigned int)(vseg->pbase >> 12);
714    unsigned int npages;
715    if ( big == 0 ) npages  = vpn_max - vpn + 1;           
716    else            npages  = (vpn_max>>9) - (vpn>>9) + 1; 
717
718    // compute destination cluster coordinates
719    unsigned int        x_dest;
720    unsigned int        y_dest;
721    mapping_header_t*   header  = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
722    mapping_cluster_t*  cluster = _get_cluster_base(header);
723    mapping_pseg_t*     pseg    = _get_pseg_base(header);
724    pseg     = pseg + vseg->psegid;
725    cluster  = cluster + pseg->clusterid;
726    x_dest   = cluster->x;
727    y_dest   = cluster->y;
728
729    unsigned int p;     // iterator for physical page index
730    unsigned int x;     // iterator for cluster x coordinate 
731    unsigned int y;     // iterator for cluster y coordinate 
732    unsigned int v;     // iterator for vspace index
733
734    // loop on PTEs
735    for ( p = 0 ; p < npages ; p++ )
736    { 
737        if  ( (local != 0) && (global == 0) )         // one cluster  / one vspace
738        {
739            if ( big )   // big pages => PTE1s
740            {
741                boot_add_pte1( vsid,
742                               x_dest,
743                               y_dest,
744                               vpn + (p<<9),
745                               flags, 
746                               ppn + (p<<9) );
747            }
748            else         // small pages => PTE2s
749            {
750                boot_add_pte2( vsid,
751                               x_dest,
752                               y_dest,
753                               vpn + p,     
754                               flags, 
755                               ppn + p );
756            }
757        }
758        else if ( (local == 0) && (global == 0) )     // all clusters / one vspace
759        {
760            for ( x = 0 ; x < X_SIZE ; x++ )
761            {
762                for ( y = 0 ; y < Y_SIZE ; y++ )
763                {
764                    if ( big )   // big pages => PTE1s
765                    {
766                        boot_add_pte1( vsid,
767                                       x,
768                                       y,
769                                       vpn + (p<<9),
770                                       flags, 
771                                       ppn + (p<<9) );
772                    }
773                    else         // small pages => PTE2s
774                    {
775                        boot_add_pte2( vsid,
776                                       x,
777                                       y,
778                                       vpn + p,
779                                       flags, 
780                                       ppn + p );
781                    }
782                }
783            }
784        }
785        else if ( (local != 0) && (global != 0) )     // one cluster  / all vspaces
786        {
787            for ( v = 0 ; v < header->vspaces ; v++ )
788            {
789                if ( big )   // big pages => PTE1s
790                {
791                    boot_add_pte1( v,
792                                   x_dest,
793                                   y_dest,
794                                   vpn + (p<<9),
795                                   flags, 
796                                   ppn + (p<<9) );
797                }
798                else         // small pages = PTE2s
799                { 
800                    boot_add_pte2( v,
801                                   x_dest,
802                                   y_dest,
803                                   vpn + p,
804                                   flags, 
805                                   ppn + p );
806                }
807            }
808        }
809        else if ( (local == 0) && (global != 0) )     // all clusters / all vspaces
810        {
811            for ( x = 0 ; x < X_SIZE ; x++ )
812            {
813                for ( y = 0 ; y < Y_SIZE ; y++ )
814                {
815                    for ( v = 0 ; v < header->vspaces ; v++ )
816                    {
817                        if ( big )  // big pages => PTE1s
818                        {
819                            boot_add_pte1( v,
820                                           x,
821                                           y,
822                                           vpn + (p<<9),
823                                           flags, 
824                                           ppn + (p<<9) );
825                        }
826                        else        // small pages -> PTE2s
827                        {
828                            boot_add_pte2( v,
829                                           x,
830                                           y,
831                                           vpn + p,
832                                           flags, 
833                                           ppn + p );
834                        }
835                    }
836                }
837            }
838        }
839    }  // end for pages
840}  // end boot_vseg_pte()
841
842///////////////////////////////////////////////////////////////////////////////
843// This function initialises the page tables for all vspaces defined
844// in the mapping_info data structure.
845// For each vspace, there is one page table per cluster.
846// In each cluster all page tables for the different vspaces must be
847// packed in one vseg occupying one single BPP (Big Physical Page).
848//
849// For each vseg, the mapping is done in two steps:
850//
851// A) mapping : the boot_vseg_map() function allocates contiguous BPPs
852//    or SPPs (if the vseg is not associated to a peripheral), and register
853//    the physical base address in the vseg pbase field. It initialises the
854//    _ptabs_vaddr and _ptabs_paddr arrays if the vseg is a PTAB.
855//
856// B) page table initialisation : the boot_vseg_pte() function initialise
857//    the PTEs (both PTE1 and PTE2) in one or several page tables:
858//    - PTEs are replicated in all vspaces for a global vseg.
859//    - PTEs are replicated in all clusters for a non local vseg.
860//
861// We must handle vsegs in the following order
862//   1) all global vsegs containing a page table,
863//   2) all global vsegs occupying more than one BPP,
864//   3) all others global vsegs
865//   4) all private vsegs in user space.
866///////////////////////////////////////////////////////////////////////////////
867void _ptabs_init() 
868{
869    mapping_header_t*   header = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
870    mapping_vspace_t*   vspace = _get_vspace_base(header);
871    mapping_vseg_t*     vseg   = _get_vseg_base(header);
872    mapping_vobj_t*     vobj   = _get_vobj_base(header);
873
874    unsigned int vspace_id;
875    unsigned int vseg_id;
876
877    if (header->vspaces == 0 )
878    {
879        _puts("\n[BOOT ERROR] in _ptabs_init() : mapping ");
880        _puts( header->name );
881        _puts(" contains no vspace\n");
882        _exit();
883    }
884
885    ///////// Phase 1 : global vsegs containing a PTAB (two loops required)
886
887#if BOOT_DEBUG_PT
888_puts("\n[BOOT DEBUG] map PTAB global vsegs\n");
889#endif
890
891    for (vseg_id = 0; vseg_id < header->globals; vseg_id++) 
892    {
893        unsigned int vobj_id = vseg[vseg_id].vobj_offset;
894        if ( (vobj[vobj_id].type == VOBJ_TYPE_PTAB) )
895        {
896            boot_vseg_map( &vseg[vseg_id], 0xFFFFFFFF );
897            vseg[vseg_id].mapped = 1;
898        }
899    }
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].type == VOBJ_TYPE_PTAB) )
905        {
906            boot_vseg_pte( &vseg[vseg_id], 0xFFFFFFFF );
907            vseg[vseg_id].mapped = 1;
908        }
909    }
910
911    ///////// Phase 2 : global vsegs occupying more than one BPP (one loop)
912
913#if BOOT_DEBUG_PT
914_puts("\n[BOOT DEBUG] map all multi-BPP global vsegs\n");
915#endif
916
917    for (vseg_id = 0; vseg_id < header->globals; vseg_id++) 
918    {
919        unsigned int vobj_id = vseg[vseg_id].vobj_offset;
920        if ( (vobj[vobj_id].length > 0x200000) &&
921             (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 3 : all others global vsegs (one loop)
930
931#if BOOT_DEBUG_PT
932_puts("\n[BOOT DEBUG] map all others global vsegs\n");
933#endif
934
935    for (vseg_id = 0; vseg_id < header->globals; vseg_id++) 
936    {
937        if ( vseg[vseg_id].mapped == 0 )
938        {
939            boot_vseg_map( &vseg[vseg_id], 0xFFFFFFFF );
940            vseg[vseg_id].mapped = 1;
941            boot_vseg_pte( &vseg[vseg_id], 0xFFFFFFFF );
942        }
943    }
944
945    ///////// Phase 4 : all private vsegs (two nested loops)
946
947    for (vspace_id = 0; vspace_id < header->vspaces; vspace_id++) 
948    {
949
950#if BOOT_DEBUG_PT
951_puts("\n[BOOT DEBUG] map private vsegs for vspace ");
952_puts( vspace[vspace_id].name );
953_puts("\n");
954#endif
955
956        for (vseg_id = vspace[vspace_id].vseg_offset;
957             vseg_id < (vspace[vspace_id].vseg_offset + vspace[vspace_id].vsegs);
958             vseg_id++) 
959        {
960            boot_vseg_map( &vseg[vseg_id], vspace_id );
961            vseg[vseg_id].mapped = 1;
962            boot_vseg_pte( &vseg[vseg_id], vspace_id );
963        }
964    }
965
966#if (BOOT_DEBUG_PT > 1)
967mapping_vseg_t*    curr;
968mapping_pseg_t*    pseg    = _get_pseg_base(header);
969mapping_cluster_t* cluster = _get_cluster_base(header);
970unsigned int       pseg_id;
971for( pseg_id = 0 ; pseg_id < header->psegs ; pseg_id++ )
972{
973    unsigned int cluster_id = pseg[pseg_id].clusterid;
974    _puts("\n[BOOT DEBUG] vsegs mapped on pseg ");
975    _puts( pseg[pseg_id].name );
976    _puts(" in cluster[");
977    _putd( cluster[cluster_id].x );
978    _puts(",");
979    _putd( cluster[cluster_id].y );
980    _puts("]\n");
981    for( curr = (mapping_vseg_t*)pseg[pseg_id].next_vseg ;
982         curr != 0 ;
983         curr = (mapping_vseg_t*)curr->next_vseg )
984    {
985        _puts(" - vseg ");
986        _puts( curr->name );
987        _puts(" : len = ");
988        _putx( curr->length );
989        _puts(" / vbase ");
990        _putx( curr->vbase );
991        _puts(" / pbase ");
992        _putl( curr->pbase );
993        _puts("\n");
994    } 
995}
996#endif
997
998} // end boot_ptabs_init()
999
1000///////////////////////////////////////////////////////////////////////////////
1001// This function initializes all private vobjs defined in the vspaces,
1002// such as mwmr channels, barriers and locks, because these vobjs
1003// are not known, and not initialized by the compiler.
1004// The MMU is supposed to be activated...
1005///////////////////////////////////////////////////////////////////////////////
1006void boot_vobjs_init() 
1007{
1008    mapping_header_t* header = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
1009    mapping_vspace_t* vspace = _get_vspace_base(header);
1010    mapping_vobj_t* vobj     = _get_vobj_base(header);
1011
1012    unsigned int vspace_id;
1013    unsigned int vobj_id;
1014
1015    // loop on the vspaces
1016    for (vspace_id = 0; vspace_id < header->vspaces; vspace_id++) 
1017    {
1018
1019#if BOOT_DEBUG_VOBJS
1020_puts("\n[BOOT DEBUG] ****** vobjs initialisation in vspace ");
1021_puts(vspace[vspace_id].name);
1022_puts(" ******\n");
1023#endif
1024
1025        _set_mmu_ptpr( (unsigned int)(_ptabs_paddr[vspace_id][0][0] >> 13) );
1026
1027        // loop on the vobjs
1028        for (vobj_id = vspace[vspace_id].vobj_offset;
1029             vobj_id < (vspace[vspace_id].vobj_offset + vspace[vspace_id].vobjs);
1030             vobj_id++) 
1031        {
1032            switch (vobj[vobj_id].type) 
1033            {
1034                case VOBJ_TYPE_MWMR:    // storage capacity is (vobj.length/4 - 5) words
1035                {
1036#if BOOT_DEBUG_VOBJS
1037_puts("MWMR    : ");
1038_puts(vobj[vobj_id].name);
1039_puts(" / vaddr = ");
1040_putx(vobj[vobj_id].vaddr);
1041_puts(" / paddr = ");
1042_putl(vobj[vobj_id].paddr);
1043_puts(" / length = ");
1044_putx(vobj[vobj_id].length);
1045_puts("\n");
1046#endif
1047                    mwmr_channel_t* mwmr = (mwmr_channel_t *) (vobj[vobj_id].vbase);
1048                    mwmr->ptw = 0;
1049                    mwmr->ptr = 0;
1050                    mwmr->sts = 0;
1051                    mwmr->width = vobj[vobj_id].init;
1052                    mwmr->depth = (vobj[vobj_id].length >> 2) - 6;
1053                    mwmr->lock = 0;
1054#if BOOT_DEBUG_VOBJS
1055_puts("          fifo depth = ");
1056_putd(mwmr->depth);
1057_puts(" / width = ");
1058_putd(mwmr->width);
1059_puts("\n");
1060#endif
1061                    break;
1062                }
1063                case VOBJ_TYPE_ELF:    // initialisation done by the loader
1064                {
1065#if BOOT_DEBUG_VOBJS
1066_puts("ELF     : ");
1067_puts(vobj[vobj_id].name);
1068_puts(" / vaddr = ");
1069_putx(vobj[vobj_id].vaddr);
1070_puts(" / paddr = ");
1071_putl(vobj[vobj_id].paddr);
1072_puts(" / length = ");
1073_putx(vobj[vobj_id].length);
1074_puts("\n");
1075#endif
1076                    break;
1077                }
1078                case VOBJ_TYPE_BLOB:    // initialisation done by the loader
1079                {
1080#if BOOT_DEBUG_VOBJS
1081_puts("BLOB    : ");
1082_puts(vobj[vobj_id].name);
1083_puts(" / vaddr = ");
1084_putx(vobj[vobj_id].vaddr);
1085_puts(" / paddr = ");
1086_putl(vobj[vobj_id].paddr);
1087_puts(" / length = ");
1088_putx(vobj[vobj_id].length);
1089_puts("\n");
1090#endif
1091                    break;
1092                }
1093                case VOBJ_TYPE_BARRIER:    // init is the number of participants
1094                {
1095#if BOOT_DEBUG_VOBJS
1096_puts("BARRIER : ");
1097_puts(vobj[vobj_id].name);
1098_puts(" / vaddr = ");
1099_putx(vobj[vobj_id].vaddr);
1100_puts(" / paddr = ");
1101_putl(vobj[vobj_id].paddr);
1102_puts(" / length = ");
1103_putx(vobj[vobj_id].length);
1104_puts("\n");
1105#endif
1106                    giet_barrier_t* barrier = (giet_barrier_t *) (vobj[vobj_id].vbase);
1107                    barrier->count  = vobj[vobj_id].init;
1108                    barrier->ntasks = vobj[vobj_id].init;
1109                    barrier->sense  = 0;
1110#if BOOT_DEBUG_VOBJS
1111_puts("          init_value = ");
1112_putd(barrier->init);
1113_puts("\n");
1114#endif
1115                    break;
1116                }
1117                case VOBJ_TYPE_LOCK:    // init value is "not taken"
1118                {
1119#if BOOT_DEBUG_VOBJS
1120_puts("LOCK    : ");
1121_puts(vobj[vobj_id].name);
1122_puts(" / vaddr = ");
1123_putx(vobj[vobj_id].vaddr);
1124_puts(" / paddr = ");
1125_putl(vobj[vobj_id].paddr);
1126_puts(" / length = ");
1127_putx(vobj[vobj_id].length);
1128_puts("\n");
1129#endif
1130                    unsigned int* lock = (unsigned int *) (vobj[vobj_id].vbase);
1131                    *lock = 0;
1132                    break;
1133                }
1134                case VOBJ_TYPE_BUFFER:    // nothing to initialise
1135                {
1136#if BOOT_DEBUG_VOBJS
1137_puts("BUFFER  : ");
1138_puts(vobj[vobj_id].name);
1139_puts(" / vaddr = ");
1140_putx(vobj[vobj_id].vaddr);
1141_puts(" / paddr = ");
1142_putl(vobj[vobj_id].paddr);
1143_puts(" / length = ");
1144_putx(vobj[vobj_id].length);
1145_puts("\n");
1146#endif
1147                    break;
1148                }
1149                case VOBJ_TYPE_MEMSPACE:
1150                {
1151#if BOOT_DEBUG_VOBJS
1152_puts("MEMSPACE  : ");
1153_puts(vobj[vobj_id].name);
1154_puts(" / vaddr = ");
1155_putx(vobj[vobj_id].vaddr);
1156_puts(" / paddr = ");
1157_putl(vobj[vobj_id].paddr);
1158_puts(" / length = ");
1159_putx(vobj[vobj_id].length);
1160_puts("\n");
1161#endif
1162                    giet_memspace_t* memspace = (giet_memspace_t *) vobj[vobj_id].vbase;
1163                    memspace->buffer = (void *) vobj[vobj_id].vbase + 8;
1164                    memspace->size = vobj[vobj_id].length - 8;
1165#if BOOT_DEBUG_VOBJS
1166_puts("          buffer vbase = ");
1167_putx((unsigned int)memspace->buffer);
1168_puts(" / size = ");
1169_putx(memspace->size);
1170_puts("\n");
1171#endif
1172                    break;
1173                }
1174                case VOBJ_TYPE_CONST:
1175                {
1176#if BOOT_DEBUG_VOBJS
1177_puts("CONST   : ");
1178_puts(vobj[vobj_id].name);
1179_puts(" / vaddr = ");
1180_putx(vobj[vobj_id].vaddr);
1181_puts(" / paddr = ");
1182_putl(vobj[vobj_id].paddr);
1183_puts(" / length = ");
1184_putx(vobj[vobj_id].length);
1185_puts(" / init = ");
1186_putx(vobj[vobj_id].init);
1187_puts("\n");
1188#endif
1189                    unsigned int* addr = (unsigned int *) vobj[vobj_id].vbase;
1190                    *addr = vobj[vobj_id].init;
1191
1192#if BOOT_DEBUG_VOBJS
1193_puts("          init = ");
1194_putx(*addr);
1195_puts("\n");
1196#endif
1197                    break;
1198                }
1199                default:
1200                {
1201                    _puts("\n[BOOT ERROR] in boot_vobjs_init() : Illegal vobj type ");
1202                    _putd( vobj[vobj_id].type );
1203                    _puts(" in vspace ");
1204                    _puts( vspace[vspace_id].name );
1205                    _puts("\n");
1206                    _exit();
1207                }
1208            }            // end switch type
1209        }          // end loop on vobjs
1210    }        // end loop on vspaces
1211} // end boot_vobjs_init()
1212
1213///////////////////////////////////////////////////////////////////////////////
1214// This function returns in the vbase and length buffers the virtual base
1215// address and the length of the  segment allocated to the schedulers array
1216// in the cluster defined by the clusterid argument.
1217///////////////////////////////////////////////////////////////////////////////
1218void boot_get_sched_vaddr( unsigned int  cluster_id,
1219                           unsigned int* vbase, 
1220                           unsigned int* length )
1221{
1222    mapping_header_t* header = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
1223    mapping_vobj_t*   vobj   = _get_vobj_base(header);
1224    mapping_vseg_t*   vseg   = _get_vseg_base(header);
1225    mapping_pseg_t*   pseg   = _get_pseg_base(header);
1226
1227    unsigned int vseg_id;
1228    unsigned int found = 0;
1229
1230    for ( vseg_id = 0 ; (vseg_id < header->vsegs) && (found == 0) ; vseg_id++ )
1231    {
1232        if ( (vobj[vseg[vseg_id].vobj_offset].type == VOBJ_TYPE_SCHED) && 
1233             (pseg[vseg[vseg_id].psegid].clusterid == cluster_id ) )
1234        {
1235            *vbase  = vseg[vseg_id].vbase;
1236            *length = vobj[vseg[vseg_id].vobj_offset].length;
1237            found = 1;
1238        }
1239    }
1240    if ( found == 0 )
1241    {
1242        mapping_cluster_t* cluster = _get_cluster_base(header);
1243        _puts("\n[BOOT ERROR] No vobj of type SCHED in cluster [");
1244        _putd( cluster[cluster_id].x );
1245        _puts(",");
1246        _putd( cluster[cluster_id].y );
1247        _puts("]\n");
1248        _exit();
1249    }
1250} // end boot_get_sched_vaddr()
1251
1252////////////////////////////////////////////////////////////////////////////////////
1253// This function initialises all processors schedulers.
1254// This is done by processor 0, and the MMU must be activated.
1255// - In Step 1, it initialises the _schedulers[x][y][l] pointers array, and scan
1256//              the processors for a first initialisation of the schedulers:
1257//              idle_task context, and HWI / SWI / PTI vectors.
1258// - In Step 2, it scan all tasks in all vspaces to complete the tasks contexts,
1259//              initialisation as specified in the mapping_info data structure.
1260////////////////////////////////////////////////////////////////////////////////////
1261void boot_schedulers_init() 
1262{
1263    mapping_header_t*  header  = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
1264    mapping_cluster_t* cluster = _get_cluster_base(header);
1265    mapping_vspace_t*  vspace  = _get_vspace_base(header);
1266    mapping_task_t*    task    = _get_task_base(header);
1267    mapping_vobj_t*    vobj    = _get_vobj_base(header);
1268    mapping_periph_t*  periph  = _get_periph_base(header);
1269    mapping_irq_t*     irq     = _get_irq_base(header);
1270
1271    unsigned int cluster_id;    // cluster index in mapping_info
1272    unsigned int periph_id;     // peripheral index in mapping_info
1273    unsigned int irq_id;        // irq index in mapping_info
1274    unsigned int vspace_id;     // vspace index in mapping_info
1275    unsigned int task_id;       // task index in mapping_info
1276    unsigned int vobj_id;       // vobj index in mapping_info
1277
1278    unsigned int lpid;          // local processor index (for several loops)
1279
1280    // TTY, NIC, CMA, HBA, user timer, and WTI channel allocators to user tasks:
1281    // - TTY[0] is reserved for the kernel
1282    // - In all clusters the first NB_PROCS_MAX timers
1283    //   are reserved for the kernel (context switch)
1284    unsigned int alloc_tty_channel = 1;              // global
1285    unsigned int alloc_nic_channel = 0;              // global
1286    unsigned int alloc_cma_channel = 0;              // global
1287    unsigned int alloc_hba_channel = 0;              // global
1288    unsigned int alloc_tim_channel[X_SIZE*Y_SIZE];   // one per cluster
1289
1290    // WTI allocators to processors
1291    // In all clusters, first NB_PROCS_MAX WTIs are for WAKUP
1292    unsigned int alloc_wti_channel[X_SIZE*Y_SIZE];   // one per cluster
1293
1294    // pointers on the XCU and PIC peripherals
1295    mapping_periph_t*  xcu = NULL;
1296    mapping_periph_t*  pic = NULL;
1297
1298    unsigned int          sched_vbase;  // schedulers array vbase address in a cluster
1299    unsigned int          sched_length; // schedulers array length
1300    static_scheduler_t*   psched;       // pointer on processor scheduler
1301
1302    /////////////////////////////////////////////////////////////////////////
1303    // Step 1 : loop on the clusters and on the processors
1304    //          to initialize the schedulers[] array of pointers,
1305    //          idle task context and interrupt vectors.
1306    // Implementation note:
1307    // We need to use both (proc_id) to scan the mapping info structure,
1308    // and (x,y,lpid) to access the schedulers array.
1309
1310    for (cluster_id = 0 ; cluster_id < X_SIZE*Y_SIZE ; cluster_id++) 
1311    {
1312        unsigned int x          = cluster[cluster_id].x;
1313        unsigned int y          = cluster[cluster_id].y;
1314
1315#if BOOT_DEBUG_SCHED
1316_puts("\n[BOOT DEBUG] Initialise schedulers in cluster[");
1317_putd( x );
1318_puts(",");
1319_putd( y );
1320_puts("]\n");
1321#endif
1322        alloc_tim_channel[cluster_id] = NB_PROCS_MAX;
1323        alloc_wti_channel[cluster_id] = NB_PROCS_MAX;
1324
1325        // checking processors number
1326        if ( cluster[cluster_id].procs > NB_PROCS_MAX )
1327        {
1328            _puts("\n[BOOT ERROR] Too much processors in cluster[");
1329            _putd( x );
1330            _puts(",");
1331            _putd( y );
1332            _puts("]\n");
1333            _exit();
1334        }
1335 
1336        // no schedulers initialisation if nprocs == 0
1337        if ( cluster[cluster_id].procs > 0 )
1338        {
1339            // get scheduler array virtual base address in cluster[cluster_id]
1340            boot_get_sched_vaddr( cluster_id, &sched_vbase, &sched_length );
1341
1342            if ( sched_length < (cluster[cluster_id].procs<<13) ) // 8 Kbytes per scheduler
1343            {
1344                _puts("\n[BOOT ERROR] Schedulers segment too small in cluster[");
1345                _putd( x );
1346                _puts(",");
1347                _putd( y );
1348                _puts("]\n");
1349                _exit();
1350            }
1351
1352            // scan peripherals to find the ICU/XCU and the PIC component
1353
1354            xcu = NULL; 
1355            for ( periph_id = cluster[cluster_id].periph_offset ;
1356                  periph_id < cluster[cluster_id].periph_offset + cluster[cluster_id].periphs;
1357                  periph_id++ )
1358            {
1359                if( (periph[periph_id].type == PERIPH_TYPE_XCU) || 
1360                    (periph[periph_id].type == PERIPH_TYPE_ICU) )
1361                {
1362                    xcu = &periph[periph_id];
1363
1364                    if ( xcu->arg < cluster[cluster_id].procs )
1365                    {
1366                        _puts("\n[BOOT ERROR] Not enough inputs for XCU[");
1367                        _putd( x );
1368                        _puts(",");
1369                        _putd( y );
1370                        _puts("]\n");
1371                        _exit();
1372                    }
1373                }
1374                if( periph[periph_id].type == PERIPH_TYPE_PIC )   
1375                {
1376                    pic = &periph[periph_id];
1377                }
1378            } 
1379            if ( xcu == NULL )
1380            {         
1381                _puts("\n[BOOT ERROR] No ICU / XCU component in cluster[");
1382                _putd( x );
1383                _puts(",");
1384                _putd( y );
1385                _puts("]\n");
1386                _exit();
1387            }
1388
1389            // loop on processors for schedulers default values
1390            // initialisation, including WTI and PTI vectors
1391            for ( lpid = 0 ; lpid < cluster[cluster_id].procs ; lpid++ )
1392            {
1393                // pointer on processor scheduler
1394                psched = (static_scheduler_t*)(sched_vbase + (lpid<<13));
1395
1396                // initialise the schedulers pointers array
1397                _schedulers[x][y][lpid] = psched;
1398
1399#if BOOT_DEBUG_SCHED
1400unsigned int   sched_vbase = (unsigned int)_schedulers[x][y][lpid];
1401unsigned int   sched_ppn;
1402unsigned int   sched_flags;
1403paddr_t        sched_pbase;
1404
1405page_table_t* ptab = (page_table_t*)(_ptabs_vaddr[0][x][y]);
1406_v2p_translate( ptab, sched_vbase>>12, &sched_ppn, &sched_flags );
1407sched_pbase = ((paddr_t)sched_ppn)<<12;
1408
1409_puts("\nProc[");
1410_putd( x );
1411_puts(",");
1412_putd( y );
1413_puts(",");
1414_putd( lpid );
1415_puts("] : scheduler vbase = ");
1416_putx( sched_vbase );
1417_puts(" : scheduler pbase = ");
1418_putl( sched_pbase );
1419_puts("\n");
1420#endif
1421                // initialise the "tasks" and "current" variables default values
1422                psched->tasks   = 0;
1423                psched->current = IDLE_TASK_INDEX;
1424
1425                // default values for HWI / PTI / SWI vectors (valid bit = 0)
1426                unsigned int slot;
1427                for (slot = 0; slot < 32; slot++)
1428                {
1429                    psched->hwi_vector[slot] = 0;
1430                    psched->pti_vector[slot] = 0;
1431                    psched->wti_vector[slot] = 0;
1432                }
1433
1434                // WTI[lpid] <= ISR_WAKUP / PTI[lpid] <= ISR_TICK
1435                psched->wti_vector[lpid] = ISR_WAKUP | 0x80000000;
1436                psched->pti_vector[lpid] = ISR_TICK  | 0x80000000;
1437
1438                // initializes the idle_task context in scheduler:
1439                // - the SR slot is 0xFF03 because this task run in kernel mode.
1440                // - it uses the page table of vspace[0]
1441                // - it uses the kernel TTY terminal
1442                // - slots containing addresses (SP, RA, EPC)
1443                //   must be initialised by kernel_init()
1444
1445                psched->context[IDLE_TASK_INDEX][CTX_CR_ID]   = 0;
1446                psched->context[IDLE_TASK_INDEX][CTX_SR_ID]   = 0xFF03;
1447                psched->context[IDLE_TASK_INDEX][CTX_PTPR_ID] = _ptabs_paddr[0][x][y]>>13;
1448                psched->context[IDLE_TASK_INDEX][CTX_PTAB_ID] = _ptabs_vaddr[0][x][y];
1449                psched->context[IDLE_TASK_INDEX][CTX_TTY_ID]  = 0;
1450                psched->context[IDLE_TASK_INDEX][CTX_LTID_ID] = IDLE_TASK_INDEX;
1451                psched->context[IDLE_TASK_INDEX][CTX_VSID_ID] = 0;
1452                psched->context[IDLE_TASK_INDEX][CTX_RUN_ID]  = 1;
1453
1454            }  // end for processors
1455
1456            // scan HWIs connected to local XCU
1457            // for round-robin allocation to processors
1458            lpid = 0;
1459            for ( irq_id = xcu->irq_offset ;
1460                  irq_id < xcu->irq_offset + xcu->irqs ;
1461                  irq_id++ )
1462            {
1463                unsigned int type    = irq[irq_id].srctype;
1464                unsigned int srcid   = irq[irq_id].srcid;
1465                unsigned int isr     = irq[irq_id].isr & 0xFFFF;
1466                unsigned int channel = irq[irq_id].channel << 16;
1467
1468                if ( (type != IRQ_TYPE_HWI) || (srcid > 31) )
1469                {
1470                    _puts("\n[BOOT ERROR] Bad IRQ in XCU of cluster[");
1471                    _putd( x );
1472                    _puts(",");
1473                    _putd( y );
1474                    _puts("]\n");
1475                    _exit();
1476                }
1477
1478                _schedulers[x][y][lpid]->hwi_vector[srcid] = isr | channel | 0x80000000;
1479                lpid = (lpid + 1) % cluster[cluster_id].procs; 
1480
1481            } // end for irqs
1482        } // end if nprocs > 0
1483    } // end for clusters
1484
1485    // If there is an external PIC component, we scan HWIs connected to PIC
1486    // for Round Robin allocation (as WTI) to processors.
1487    // We allocate one WTI per processor, starting from proc[0,0,0],
1488    // and we increment (cluster_id, lpid) as required.
1489    if ( pic != NULL )
1490    {   
1491        unsigned int cluster_id = 0;   // index in clusters array
1492        unsigned int lpid       = 0;   // processor local index
1493
1494        // scan IRQS defined in PIC
1495        for ( irq_id = pic->irq_offset ;
1496              irq_id < pic->irq_offset + pic->irqs ;
1497              irq_id++ )
1498        {
1499            // compute next values for (cluster_id,lpid)
1500            // if no more procesor available in current cluster
1501            unsigned int overflow = 0;
1502            while ( (lpid >= cluster[cluster_id].procs) ||
1503                    (alloc_wti_channel[cluster_id] >= xcu->arg) )
1504            {
1505                overflow++;
1506                cluster_id = (cluster_id + 1) % (X_SIZE*Y_SIZE);
1507                lpid       = 0;
1508
1509                // overflow detection
1510                if ( overflow > (X_SIZE*Y_SIZE*NB_PROCS_MAX*32) )
1511                {
1512                    _puts("\n[BOOT ERROR] Not enough processors for external IRQs\n");
1513                    _exit();
1514                }
1515            }
1516
1517            unsigned int type    = irq[irq_id].srctype;
1518            unsigned int srcid   = irq[irq_id].srcid;
1519            unsigned int isr     = irq[irq_id].isr & 0xFFFF;
1520            unsigned int channel = irq[irq_id].channel << 16;
1521
1522            if ( (type != IRQ_TYPE_HWI) || (srcid > 31) )
1523            {
1524                _puts("\n[BOOT ERROR] Bad IRQ in PIC component\n");
1525                _exit();
1526            }
1527
1528            // get scheduler[cluster_id] address
1529            unsigned int x          = cluster[cluster_id].x;
1530            unsigned int y          = cluster[cluster_id].y;
1531            unsigned int cluster_xy = (x<<Y_WIDTH) + y;
1532            psched                  = _schedulers[x][y][lpid];
1533
1534            // update WTI vector for scheduler[cluster_id][lpid]
1535            unsigned int index            = alloc_wti_channel[cluster_id];
1536            psched->wti_vector[index]     = isr | channel | 0x80000000;
1537            alloc_wti_channel[cluster_id] = index + 1;
1538            lpid                          = lpid + 1;
1539
1540            // update IRQ fields in mapping for PIC initialisation
1541            irq[irq_id].dest_id = index;
1542            irq[irq_id].dest_xy = cluster_xy;
1543
1544        }  // end for IRQs
1545    } // end if PIC
1546               
1547#if BOOT_DEBUG_SCHED
1548for ( cluster_id = 0 ; cluster_id < (X_SIZE*Y_SIZE) ; cluster_id++ )
1549{
1550    unsigned int x          = cluster[cluster_id].x;
1551    unsigned int y          = cluster[cluster_id].y;
1552    unsigned int slot;
1553    unsigned int entry;
1554    for ( lpid = 0 ; lpid < cluster[cluster_id].procs ; lpid++ )
1555    {
1556        psched = _schedulers[x][y][lpid];
1557       
1558        _puts("\n*** IRQS for proc[");
1559        _putd( x );
1560        _puts(",");
1561        _putd( y );
1562        _puts(",");
1563        _putd( lpid );
1564        _puts("]\n");
1565        for ( slot = 0 ; slot < 32 ; slot++ )
1566        {
1567            entry = psched->hwi_vector[slot];
1568            if ( entry & 0x80000000 ) 
1569            {
1570                _puts(" - HWI ");
1571                _putd( slot );
1572                _puts(" / isrtype = ");
1573                _putd( entry & 0xFFFF ); 
1574                _puts(" / channel = ");
1575                _putd( (entry >> 16) & 0x7FFF ); 
1576                _puts("\n");
1577            }
1578        }
1579        for ( slot = 0 ; slot < 32 ; slot++ )
1580        {
1581            entry = psched->wti_vector[slot];
1582            if ( entry & 0x80000000 ) 
1583            {
1584                _puts(" - WTI ");
1585                _putd( slot );
1586                _puts(" / isrtype = ");
1587                _putd( entry & 0xFFFF ); 
1588                _puts(" / channel = ");
1589                _putd( (entry >> 16) & 0x7FFF ); 
1590                _puts("\n");
1591            }
1592        }
1593        for ( slot = 0 ; slot < 32 ; slot++ )
1594        {
1595            entry = psched->pti_vector[slot];
1596            if ( entry & 0x80000000 ) 
1597            {
1598                _puts(" - PTI ");
1599                _putd( slot );
1600                _puts(" / isrtype = ");
1601                _putd( entry & 0xFFFF ); 
1602                _puts(" / channel = ");
1603                _putd( (entry >> 16) & 0x7FFF ); 
1604                _puts("\n");
1605            }
1606        }
1607    }
1608}
1609#endif
1610
1611    ///////////////////////////////////////////////////////////////////
1612    // Step 2 : loop on the vspaces and the tasks  to complete
1613    //          the schedulers and task contexts initialisation.
1614
1615    for (vspace_id = 0; vspace_id < header->vspaces; vspace_id++) 
1616    {
1617        // We must set the PTPR depending on the vspace, because the start_vector
1618        // and the stack address are defined in virtual space.
1619        _set_mmu_ptpr( (unsigned int)(_ptabs_paddr[vspace_id][0][0] >> 13) );
1620
1621        // loop on the tasks in vspace (task_id is the global index in mapping)
1622        for (task_id = vspace[vspace_id].task_offset;
1623             task_id < (vspace[vspace_id].task_offset + vspace[vspace_id].tasks);
1624             task_id++) 
1625        {
1626            // compute the cluster coordinates & local processor index
1627            unsigned int x          = cluster[task[task_id].clusterid].x;
1628            unsigned int y          = cluster[task[task_id].clusterid].y;
1629            unsigned int cluster_xy = (x<<Y_WIDTH) + y;
1630            unsigned int lpid       = task[task_id].proclocid;                 
1631
1632#if BOOT_DEBUG_SCHED
1633_puts("\n[BOOT DEBUG] Initialise context for task ");
1634_puts( task[task_id].name );
1635_puts(" in vspace ");
1636_puts( vspace[vspace_id].name );
1637_puts("\n");
1638#endif
1639            // compute gpid (global processor index) and scheduler base address
1640            unsigned int gpid = (cluster_xy << P_WIDTH) + lpid;
1641            psched            = _schedulers[x][y][lpid];
1642
1643            // ctx_sr : value required before an eret instruction
1644            unsigned int ctx_sr = 0x0000FF13;
1645
1646            // ctx_ptpr : page table physical base address (shifted by 13 bit)
1647            unsigned int ctx_ptpr = (unsigned int)(_ptabs_paddr[vspace_id][x][y] >> 13);
1648
1649            // ctx_ptab : page_table virtual base address
1650            unsigned int ctx_ptab = _ptabs_vaddr[vspace_id][x][y];
1651
1652            // ctx_tty : TTY terminal global index provided by the global allocator
1653            //           Each user terminal is a private ressource: the number of
1654            //           requested terminal cannot be larger than NB_TTY_CHANNELS.             
1655            unsigned int ctx_tty = 0xFFFFFFFF;
1656            if (task[task_id].use_tty) 
1657            {
1658                if (alloc_tty_channel >= NB_TTY_CHANNELS) 
1659                {
1660                    _puts("\n[BOOT ERROR] TTY channel index too large for task ");
1661                    _puts(task[task_id].name);
1662                    _puts(" in vspace ");
1663                    _puts(vspace[vspace_id].name);
1664                    _puts("\n");
1665                    _exit();
1666                }
1667                ctx_tty = alloc_tty_channel;
1668                alloc_tty_channel++;
1669             }
1670
1671            // ctx_nic : NIC channel global index provided by the global allocator
1672            //           Each channel is a private ressource: the number of
1673            //           requested channels cannot be larger than NB_NIC_CHANNELS.
1674            unsigned int ctx_nic = 0xFFFFFFFF;
1675            if (task[task_id].use_nic) 
1676            {
1677                if (alloc_nic_channel >= NB_NIC_CHANNELS) 
1678                {
1679                    _puts("\n[BOOT ERROR] NIC channel index too large for task ");
1680                    _puts(task[task_id].name);
1681                    _puts(" in vspace ");
1682                    _puts(vspace[vspace_id].name);
1683                    _puts("\n");
1684                    _exit();
1685                }
1686                ctx_nic = alloc_nic_channel;
1687                alloc_nic_channel++;
1688            }
1689
1690            // ctx_cma : CMA channel global index provided by the global allocator
1691            //           Each channel is a private ressource: the number of
1692            //           requested channels cannot be larger than NB_NIC_CHANNELS.
1693            unsigned int ctx_cma = 0xFFFFFFFF;
1694            if (task[task_id].use_cma) 
1695            {
1696                if (alloc_cma_channel >= NB_CMA_CHANNELS) 
1697                {
1698                    _puts("\n[BOOT ERROR] CMA channel index too large for task ");
1699                    _puts(task[task_id].name);
1700                    _puts(" in vspace ");
1701                    _puts(vspace[vspace_id].name);
1702                    _puts("\n");
1703                    _exit();
1704                }
1705                ctx_cma = alloc_cma_channel;
1706                alloc_cma_channel++;
1707            }
1708
1709            // ctx_hba : HBA channel global index provided by the global allocator
1710            //           Each channel is a private ressource: the number of
1711            //           requested channels cannot be larger than NB_NIC_CHANNELS.
1712            unsigned int ctx_hba = 0xFFFFFFFF;
1713            if (task[task_id].use_hba) 
1714            {
1715                if (alloc_hba_channel >= NB_IOC_CHANNELS) 
1716                {
1717                    _puts("\n[BOOT ERROR] IOC channel index too large for task ");
1718                    _puts(task[task_id].name);
1719                    _puts(" in vspace ");
1720                    _puts(vspace[vspace_id].name);
1721                    _puts("\n");
1722                    _exit();
1723                }
1724                ctx_hba = alloc_hba_channel;
1725                alloc_hba_channel++;
1726            }
1727            // ctx_tim : TIMER local channel index provided by the cluster allocator
1728            //           Each timer is a private ressource
1729            unsigned int ctx_tim = 0xFFFFFFFF;
1730            if (task[task_id].use_tim) 
1731            {
1732                unsigned int cluster_id = task[task_id].clusterid;
1733
1734                if ( alloc_tim_channel[cluster_id] >= NB_TIM_CHANNELS ) 
1735                {
1736                    _puts("\n[BOOT ERROR] local TIMER index too large for task ");
1737                    _puts(task[task_id].name);
1738                    _puts(" in vspace ");
1739                    _puts(vspace[vspace_id].name);
1740                    _puts("\n");
1741                    _exit();
1742                }
1743                ctx_tim =  alloc_tim_channel[cluster_id];
1744                alloc_tim_channel[cluster_id]++;
1745            }
1746            // ctx_epc : Get the virtual address of the memory location containing
1747            // the task entry point : the start_vector is stored by GCC in the seg_data
1748            // segment and we must wait the .elf loading to get the entry point value...
1749            vobj_id = vspace[vspace_id].start_vobj_id;     
1750            unsigned int ctx_epc = vobj[vobj_id].vbase + (task[task_id].startid)*4;
1751
1752            // ctx_sp :  Get the vobj containing the stack
1753            vobj_id = task[task_id].stack_vobj_id;
1754            unsigned int ctx_sp = vobj[vobj_id].vbase + vobj[vobj_id].length;
1755
1756            // get local task index in scheduler
1757            unsigned int ltid = psched->tasks;
1758
1759            // get vspace thread index
1760            unsigned int thread_id = task[task_id].trdid;
1761
1762            if (ltid >= IDLE_TASK_INDEX) 
1763            {
1764                _puts("\n[BOOT ERROR] in boot_schedulers_init() : ");
1765                _putd( ltid );
1766                _puts(" tasks allocated to processor ");
1767                _putd( gpid );
1768                _puts(" / max is ");
1769                _putd( IDLE_TASK_INDEX );
1770                _puts("\n");
1771                _exit();
1772            }
1773
1774            // update the "tasks" and "current" fields in scheduler:
1775            // the first task to execute is task 0 as soon as there is at least
1776            // one task allocated to processor.
1777            psched->tasks   = ltid + 1;
1778            psched->current = 0;
1779
1780            // initializes the task context in scheduler
1781            psched->context[ltid][CTX_CR_ID]    = 0;
1782            psched->context[ltid][CTX_SR_ID]    = ctx_sr;
1783            psched->context[ltid][CTX_SP_ID]    = ctx_sp;
1784            psched->context[ltid][CTX_EPC_ID]   = ctx_epc;
1785            psched->context[ltid][CTX_PTPR_ID]  = ctx_ptpr;
1786            psched->context[ltid][CTX_TTY_ID]   = ctx_tty;
1787            psched->context[ltid][CTX_CMA_ID]   = ctx_cma;
1788            psched->context[ltid][CTX_HBA_ID]   = ctx_hba;
1789            psched->context[ltid][CTX_NIC_ID]   = ctx_nic;
1790            psched->context[ltid][CTX_TIM_ID]   = ctx_tim;
1791            psched->context[ltid][CTX_PTAB_ID]  = ctx_ptab;
1792            psched->context[ltid][CTX_LTID_ID]  = ltid;
1793            psched->context[ltid][CTX_GTID_ID]  = task_id;
1794            psched->context[ltid][CTX_TRDID_ID] = thread_id;
1795            psched->context[ltid][CTX_VSID_ID]  = vspace_id;
1796            psched->context[ltid][CTX_RUN_ID]   = 1;
1797
1798#if BOOT_DEBUG_SCHED
1799_puts("\nTask ");
1800_putd( task_id );
1801_puts(" allocated to processor[");
1802_putd( x );
1803_puts(",");
1804_putd( y );
1805_puts(",");
1806_putd( lpid );
1807_puts("]\n  - ctx[LTID]   = ");
1808_putd( psched->context[ltid][CTX_LTID_ID] );
1809_puts("\n  - ctx[SR]     = ");
1810_putx( psched->context[ltid][CTX_SR_ID] );
1811_puts("\n  - ctx[SP]     = ");
1812_putx( psched->context[ltid][CTX_SP_ID] );
1813_puts("\n  - ctx[EPC]    = ");
1814_putx( psched->context[ltid][CTX_EPC_ID] );
1815_puts("\n  - ctx[PTPR]   = ");
1816_putx( psched->context[ltid][CTX_PTPR_ID] );
1817_puts("\n  - ctx[TTY]    = ");
1818_putx( psched->context[ltid][CTX_TTY_ID] );
1819_puts("\n  - ctx[NIC]    = ");
1820_putx( psched->context[ltid][CTX_NIC_ID] );
1821_puts("\n  - ctx[CMA]    = ");
1822_putx( psched->context[ltid][CTX_CMA_ID] );
1823_puts("\n  - ctx[IOC]    = ");
1824_putx( psched->context[ltid][CTX_HBA_ID] );
1825_puts("\n  - ctx[TIM]    = ");
1826_putx( psched->context[ltid][CTX_TIM_ID] );
1827_puts("\n  - ctx[PTAB]   = ");
1828_putx( psched->context[ltid][CTX_PTAB_ID] );
1829_puts("\n  - ctx[GTID]   = ");
1830_putx( psched->context[ltid][CTX_GTID_ID] );
1831_puts("\n  - ctx[VSID]   = ");
1832_putx( psched->context[ltid][CTX_VSID_ID] );
1833_puts("\n  - ctx[TRDID]  = ");
1834_putx( psched->context[ltid][CTX_TRDID_ID] );
1835_puts("\n");
1836#endif
1837
1838        } // end loop on tasks
1839    } // end loop on vspaces
1840} // end boot_schedulers_init()
1841
1842//////////////////////////////////////////////////////////////////////////////////
1843// This function loads the map.bin file from block device.
1844//////////////////////////////////////////////////////////////////////////////////
1845void boot_mapping_init()
1846{
1847    // desactivates IOC interrupt
1848    _ioc_init( 0 );
1849
1850    // open file "map.bin"
1851    int fd_id = _fat_open( IOC_BOOT_MODE,
1852                           "map.bin",
1853                           0 );         // no creation
1854    if ( fd_id == -1 )
1855    {
1856        _puts("\n[BOOT ERROR] : map.bin file not found \n");
1857        _exit();
1858    }
1859
1860#if BOOT_DEBUG_MAPPING
1861_puts("\n[BOOT] map.bin file successfully open at cycle ");
1862_putd(_get_proctime());
1863_puts("\n");
1864#endif
1865
1866    // get "map.bin" file size (from fat) and check it
1867    unsigned int size    = fat.fd[fd_id].file_size;
1868
1869    if ( size > SEG_BOOT_MAPPING_SIZE )
1870    {
1871        _puts("\n[BOOT ERROR] : allocated segment too small for map.bin file\n");
1872        _exit();
1873    }
1874
1875    // load "map.bin" file into buffer
1876    unsigned int nblocks = size >> 9;
1877    unsigned int offset  = size & 0x1FF;
1878    if ( offset ) nblocks++;
1879
1880    unsigned int ok = _fat_read( IOC_BOOT_MODE,
1881                                 fd_id, 
1882                                 (unsigned int*)SEG_BOOT_MAPPING_BASE, 
1883                                 nblocks,       
1884                                 0 );      // offset
1885    if ( ok == -1 )
1886    {
1887        _puts("\n[BOOT ERROR] : unable to load map.bin file \n");
1888        _exit();
1889    }
1890    _fat_close( fd_id );
1891   
1892    // close file "map.bin"
1893    boot_mapping_check();
1894
1895} // end boot_mapping_init()
1896
1897
1898/////////////////////////////////////////////////////////////////////////////////////
1899// This function load all loadable segments for one .elf file, identified
1900// by the "pathname" argument. Some loadable segments can be copied in several
1901// clusters: same virtual address but different physical addresses. 
1902// - It open the file.
1903// - It loads the complete file in the dedicated boot_elf_buffer.
1904// - It copies each loadable segments  at the virtual address defined in
1905//   the .elf file, making several copies if the target vseg is not local.
1906// - It closes the file.
1907// This function is supposed to be executed by processor[0,0,0].
1908// Note:
1909//   We must use physical addresses to reach the destination buffers that
1910//   can be located in remote clusters. We use either a _physical_memcpy(),
1911//   or a _dma_physical_copy() if DMA is available.
1912//////////////////////////////////////////////////////////////////////////////////////
1913void load_one_elf_file( unsigned int is_kernel,     // kernel file if non zero
1914                        char*        pathname,
1915                        unsigned int vspace_id )    // to scan the proper vspace
1916{
1917    mapping_header_t  * header  = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
1918    mapping_vspace_t  * vspace  = _get_vspace_base(header);
1919    mapping_vseg_t    * vseg    = _get_vseg_base(header);
1920    mapping_vobj_t    * vobj    = _get_vobj_base(header);
1921
1922    unsigned int seg_id;
1923
1924#if BOOT_DEBUG_ELF
1925_puts("\n[BOOT DEBUG] Start searching file ");
1926_puts( pathname );
1927_puts(" at cycle ");
1928_putd( _get_proctime() );
1929_puts("\n");
1930#endif
1931
1932    // open .elf file
1933    int fd_id = _fat_open( IOC_BOOT_MODE,
1934                           pathname,
1935                           0 );      // no creation
1936    if ( fd_id < 0 )
1937    {
1938        _puts("\n[BOOT ERROR] load_one_elf_file() : ");
1939        _puts( pathname );
1940        _puts(" not found\n");
1941        _exit();
1942    }
1943
1944    // check buffer size versus file size
1945    if ( fat.fd[fd_id].file_size > GIET_ELF_BUFFER_SIZE )
1946    {
1947        _puts("\n[BOOT ERROR] load_one_elf_file() : ");
1948        _puts( pathname );
1949        _puts(" exceeds the GIET_ELF_BUFFERSIZE defined in giet_config.h\n");
1950        _exit();
1951    }
1952
1953    // compute number of sectors
1954    unsigned int nbytes   = fat.fd[fd_id].file_size;
1955    unsigned int nsectors = nbytes>>9;
1956    if( nbytes & 0x1FF) nsectors++;
1957
1958    // load file in elf buffer
1959    if( _fat_read( IOC_BOOT_MODE, 
1960                   fd_id, 
1961                   boot_elf_buffer,
1962                   nsectors,
1963                   0 ) != nsectors )
1964    {
1965        _puts("\n[BOOT ERROR] load_one_elf_file() : unexpected EOF for file ");
1966        _puts( pathname );
1967        _puts("\n");   
1968        _exit();
1969    }
1970
1971    // Check ELF Magic Number in ELF header
1972    Elf32_Ehdr* elf_header_ptr = (Elf32_Ehdr*)boot_elf_buffer;
1973
1974    if ( (elf_header_ptr->e_ident[EI_MAG0] != ELFMAG0) ||
1975         (elf_header_ptr->e_ident[EI_MAG1] != ELFMAG1) ||
1976         (elf_header_ptr->e_ident[EI_MAG2] != ELFMAG2) ||
1977         (elf_header_ptr->e_ident[EI_MAG3] != ELFMAG3) )
1978    {
1979        _puts("\n[BOOT ERROR] load_elf() : file ");
1980        _puts( pathname );
1981        _puts(" does not use ELF format\n");   
1982        _exit();
1983    }
1984
1985    // get program header table pointer
1986    unsigned int pht_index = elf_header_ptr->e_phoff;
1987    if( pht_index == 0 )
1988    {
1989        _puts("\n[BOOT ERROR] load_one_elf_file() : file ");
1990        _puts( pathname );
1991        _puts(" does not contain loadable segment\n");   
1992        _exit();
1993    }
1994    Elf32_Phdr* elf_pht_ptr = (Elf32_Phdr*)(boot_elf_buffer + pht_index);
1995
1996    // get number of segments
1997    unsigned int nsegments   = elf_header_ptr->e_phnum;
1998
1999    _puts("\n[BOOT] File ");
2000    _puts( pathname );
2001    _puts(" loaded at cycle ");
2002    _putd( _get_proctime() );
2003    _puts("\n");
2004
2005    // Loop on loadable segments in the .elf file
2006    for (seg_id = 0 ; seg_id < nsegments ; seg_id++)
2007    {
2008        if(elf_pht_ptr[seg_id].p_type == PT_LOAD)
2009        {
2010            // Get segment attributes
2011            unsigned int seg_vaddr  = elf_pht_ptr[seg_id].p_vaddr;
2012            unsigned int seg_offset = elf_pht_ptr[seg_id].p_offset;
2013            unsigned int seg_filesz = elf_pht_ptr[seg_id].p_filesz;
2014            unsigned int seg_memsz  = elf_pht_ptr[seg_id].p_memsz;
2015
2016#if BOOT_DEBUG_ELF
2017_puts(" - segment ");
2018_putd( seg_id );
2019_puts(" / vaddr = ");
2020_putx( seg_vaddr );
2021_puts(" / file_size = ");
2022_putx( seg_filesz );
2023_puts("\n");
2024#endif
2025
2026            if( seg_memsz < seg_filesz )
2027            {
2028                _puts("\n[BOOT ERROR] load_one_elf_file() : segment at vaddr = ");
2029                _putx( seg_vaddr );
2030                _puts(" in file ");
2031                _puts( pathname );
2032                _puts(" has memsz < filesz \n");   
2033                _exit();
2034            }
2035
2036            // fill empty space with 0 as required
2037            if( seg_memsz > seg_filesz )
2038            {
2039                unsigned int i; 
2040                for( i = seg_filesz ; i < seg_memsz ; i++ ) boot_elf_buffer[i+seg_offset] = 0;
2041            } 
2042
2043            unsigned int src_vaddr = (unsigned int)boot_elf_buffer + seg_offset;
2044
2045            // search all vsegs matching the virtual address
2046            unsigned int vseg_first;
2047            unsigned int vseg_last;
2048            unsigned int vseg_id;
2049            unsigned int found = 0;
2050            if ( is_kernel )
2051            {
2052                vseg_first = 0;
2053                vseg_last  = header->globals;
2054            }
2055            else
2056            {
2057                vseg_first = vspace[vspace_id].vseg_offset;
2058                vseg_last  = vseg_first + vspace[vspace_id].vsegs;
2059            }
2060
2061            for ( vseg_id = vseg_first ; vseg_id < vseg_last ; vseg_id++ )
2062            {
2063                if ( seg_vaddr == vseg[vseg_id].vbase )  // matching
2064                {
2065                    found = 1;
2066
2067                    // get destination buffer physical address and size
2068                    paddr_t      seg_paddr  = vseg[vseg_id].pbase;
2069                    unsigned int vobj_id    = vseg[vseg_id].vobj_offset;
2070                    unsigned int seg_size   = vobj[vobj_id].length;
2071                   
2072#if BOOT_DEBUG_ELF
2073_puts("   loaded into vseg ");
2074_puts( vseg[vseg_id].name );
2075_puts(" at paddr = ");
2076_putl( seg_paddr );
2077_puts(" (buffer size = ");
2078_putx( seg_size );
2079_puts(")\n");
2080#endif
2081                    // check vseg size
2082                    if ( seg_size < seg_filesz )
2083                    {
2084                        _puts("\n[BOOT ERROR] in load_one_elf_file()\n");
2085                        _puts("vseg ");
2086                        _puts( vseg[vseg_id].name );
2087                        _puts(" is to small for loadable segment ");
2088                        _putx( seg_vaddr );
2089                        _puts(" in file ");
2090                        _puts( pathname );
2091                        _puts(" \n");   
2092                        _exit();
2093                    }
2094
2095                    // copy the segment from boot buffer to destination buffer
2096                    // using DMA channel[0,0,0] if it is available.
2097                    if( NB_DMA_CHANNELS > 0 )
2098                    {
2099                        _dma_physical_copy( 0,                  // DMA in cluster[0,0]
2100                                            0,                  // DMA channel 0
2101                                            (paddr_t)seg_paddr, // destination paddr
2102                                            (paddr_t)src_vaddr, // source paddr
2103                                            seg_filesz );       // size
2104                    }
2105                    else
2106                    {
2107                        _physical_memcpy( (paddr_t)seg_paddr,   // destination paddr
2108                                          (paddr_t)src_vaddr,   // source paddr
2109                                          seg_filesz );         // size
2110                    }
2111                }
2112            }  // end for vsegs in vspace
2113
2114            // check at least one matching vseg
2115            if ( found == 0 )
2116            {
2117                _puts("\n[BOOT ERROR] in load_one_elf_file()\n");
2118                _puts("vseg for loadable segment ");
2119                _putx( seg_vaddr );
2120                _puts(" in file ");
2121                _puts( pathname );
2122                _puts(" not found \n");   
2123                _exit();
2124            }
2125        }
2126    }  // end for loadable segments
2127
2128    // close .elf file
2129    _fat_close( fd_id );
2130
2131} // end load_one_elf_file()
2132
2133
2134/////i////////////////////////////////////////////////////////////////////////////////
2135// This function uses the map.bin data structure to load the "kernel.elf" file
2136// as well as the various "application.elf" files into memory.
2137// - The "preloader.elf" file is not loaded, because it has been burned in the ROM.
2138// - The "boot.elf" file is not loaded, because it has been loaded by the preloader.
2139// This function scans all vobjs defined in the map.bin data structure to collect
2140// all .elf files pathnames, and calls the load_one_elf_file() for each .elf file.
2141// As the code can be replicated in several vsegs, the same code can be copied
2142// in one or several clusters by the load_one_elf_file() function.
2143//////////////////////////////////////////////////////////////////////////////////////
2144void boot_elf_load()
2145{
2146    mapping_header_t* header = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
2147    mapping_vspace_t* vspace = _get_vspace_base( header );
2148    mapping_vobj_t*   vobj   = _get_vobj_base( header );
2149    unsigned int      vspace_id;
2150    unsigned int      vobj_id;
2151    unsigned int      found;
2152
2153    // Scan all vobjs corresponding to global vsegs,
2154    // to find the pathname to the kernel.elf file
2155    found = 0;
2156    for( vobj_id = 0 ; vobj_id < header->globals ; vobj_id++ )
2157    {
2158        if(vobj[vobj_id].type == VOBJ_TYPE_ELF) 
2159        {   
2160            found = 1;
2161            break;
2162        }
2163    }
2164
2165    // We need one kernel.elf file
2166    if (found == 0)
2167    {
2168        _puts("[BOOT ERROR] boot_elf_load() : kernel.elf file not found\n");
2169        _exit();
2170    }
2171
2172    // Load the kernel
2173    load_one_elf_file( 1,                           // kernel file
2174                       vobj[vobj_id].binpath,       // file pathname
2175                       0 );                         // vspace 0
2176
2177    // loop on the vspaces, scanning all vobjs in the vspace,
2178    // to find the pathname of the .elf file associated to the vspace.
2179    for( vspace_id = 0 ; vspace_id < header->vspaces ; vspace_id++ )
2180    {
2181        // loop on the vobjs in vspace (vobj_id is the global index)
2182        unsigned int found = 0;
2183        for (vobj_id = vspace[vspace_id].vobj_offset;
2184             vobj_id < (vspace[vspace_id].vobj_offset + vspace[vspace_id].vobjs);
2185             vobj_id++) 
2186        {
2187            if(vobj[vobj_id].type == VOBJ_TYPE_ELF) 
2188            {   
2189                found = 1;
2190                break;
2191            }
2192        }
2193
2194        // We want one .elf file per vspace
2195        if (found == 0)
2196        {
2197            _puts("[BOOT ERROR] boot_elf_load() : .elf file not found for vspace ");
2198            _puts( vspace[vspace_id].name );
2199            _puts("\n");
2200            _exit();
2201        }
2202
2203        load_one_elf_file( 0,                          // not a kernel file
2204                           vobj[vobj_id].binpath,      // file pathname
2205                           vspace_id );                // vspace index
2206
2207    }  // end for vspaces
2208
2209} // end boot_elf_load()
2210
2211////////////////////////////////////////////////////////////////////////////////
2212// This function intializes the periherals and coprocessors, as specified
2213// in the mapping_info file.
2214////////////////////////////////////////////////////////////////////////////////
2215void boot_peripherals_init() 
2216{
2217    mapping_header_t * header   = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
2218    mapping_cluster_t * cluster = _get_cluster_base(header);
2219    mapping_periph_t * periph   = _get_periph_base(header);
2220    mapping_vobj_t * vobj       = _get_vobj_base(header);
2221    mapping_coproc_t * coproc   = _get_coproc_base(header);
2222    mapping_cp_port_t * cp_port = _get_cp_port_base(header);
2223    mapping_irq_t * irq         = _get_irq_base(header);
2224
2225    unsigned int cluster_id;
2226    unsigned int periph_id;
2227    unsigned int coproc_id;
2228    unsigned int cp_port_id;
2229    unsigned int channel_id;
2230
2231    // loop on all physical clusters
2232    for (cluster_id = 0; cluster_id < X_SIZE*Y_SIZE; cluster_id++) 
2233    {
2234        // computes cluster coordinates
2235        unsigned int x          = cluster[cluster_id].x;
2236        unsigned int y          = cluster[cluster_id].y;
2237        unsigned int cluster_xy = (x<<Y_WIDTH) + y;
2238
2239#if BOOT_DEBUG_PERI
2240_puts("\n[BOOT DEBUG] Peripherals initialisation in cluster[");
2241_putd( x );
2242_puts(",");
2243_putd( y );
2244_puts("]\n");
2245#endif
2246
2247        // loop on peripherals
2248        for (periph_id = cluster[cluster_id].periph_offset;
2249             periph_id < cluster[cluster_id].periph_offset +
2250             cluster[cluster_id].periphs; periph_id++) 
2251        {
2252            unsigned int type       = periph[periph_id].type;
2253            unsigned int subtype    = periph[periph_id].subtype;
2254            unsigned int channels   = periph[periph_id].channels;
2255
2256            switch (type) 
2257            {
2258                case PERIPH_TYPE_IOC:    // vci_block_device component
2259                {
2260                    if ( subtype == PERIPH_SUBTYPE_BDV )
2261                    {
2262                        _bdv_lock.value = 0;
2263#if BOOT_DEBUG_PERI
2264_puts("- BDV : channels = ");
2265_putd(channels);
2266_puts("\n");
2267#endif
2268                    }
2269                    else if ( subtype == PERIPH_SUBTYPE_HBA )
2270                    {
2271                        // TODO
2272                    }
2273                    else if ( subtype == PERIPH_SUBTYPE_SPI )
2274                    {
2275                        // TODO
2276                    }
2277                    break;
2278                }
2279                case PERIPH_TYPE_CMA:    // vci_chbuf_dma component
2280                {
2281                    for (channel_id = 0; channel_id < channels; channel_id++) 
2282                    {
2283                        // TODO
2284                    }
2285#if BOOT_DEBUG_PERI
2286_puts("- CMA : channels = ");
2287_putd(channels);
2288_puts("\n");
2289#endif
2290                    break;
2291                }
2292                case PERIPH_TYPE_NIC:    // vci_multi_nic component
2293                {
2294                    for (channel_id = 0; channel_id < channels; channel_id++) 
2295                    {
2296                        // TODO
2297                    }
2298#if BOOT_DEBUG_PERI
2299_puts("- NIC : channels = ");
2300_putd(channels);
2301_puts("\n");
2302#endif
2303                    break;
2304                }
2305                case PERIPH_TYPE_TTY:    // vci_multi_tty component
2306                {
2307                    for (channel_id = 0; channel_id < channels; channel_id++) 
2308                    {
2309                        _tty_lock[channel_id].value = 0;
2310                        _tty_rx_full[channel_id]    = 0;
2311                    }
2312#if BOOT_DEBUG_PERI
2313_puts("- TTY : channels = ");
2314_putd(channels);
2315_puts("\n");
2316#endif
2317                    break;
2318                }
2319                case PERIPH_TYPE_IOB:    // vci_io_bridge component
2320                {
2321                    if (GIET_USE_IOMMU) 
2322                    {
2323                        // TODO
2324                        // get the iommu page table physical address
2325                        // set IOMMU page table address
2326                        // pseg_base[IOB_IOMMU_PTPR] = ptab_pbase;   
2327                        // activate IOMMU
2328                        // pseg_base[IOB_IOMMU_ACTIVE] = 1;       
2329                    }
2330                    break;
2331                }
2332                case PERIPH_TYPE_PIC:    // vci_iopic component
2333                {
2334#if BOOT_DEBUG_PERI
2335_puts("- PIC : channels = ");
2336_putd(channels);
2337_puts("\n");
2338#endif
2339                    // scan all IRQs defined in mapping for PIC component,
2340                    // and initialises addresses for WTI IRQs
2341                    for ( channel_id = periph[periph_id].irq_offset ;
2342                          channel_id < periph[periph_id].irq_offset + periph[periph_id].irqs ;
2343                          channel_id++ )
2344                    {
2345                        unsigned int hwi_id     = irq[channel_id].srcid;   // HWI index in PIC
2346                        unsigned int wti_id     = irq[channel_id].dest_id; // WTI index in XCU
2347                        unsigned int cluster_xy = irq[channel_id].dest_xy; // XCU coordinates
2348                        unsigned int vaddr;
2349
2350                        _xcu_get_wti_address( wti_id, &vaddr );
2351                        _pic_init( hwi_id, vaddr, cluster_xy ); 
2352
2353#if BOOT_DEBUG_PERI
2354unsigned int address = _pic_get_register( channel_id, IOPIC_ADDRESS );
2355unsigned int extend  = _pic_get_register( channel_id, IOPIC_EXTEND  );
2356_puts("    hwi_index = ");
2357_putd( hwi_id );
2358_puts(" / wti_index = ");
2359_putd( wti_id );
2360_puts(" / vaddr = ");
2361_putx( vaddr );
2362_puts(" in cluster[");
2363_putd( cluster_xy >> Y_WIDTH );
2364_puts(",");
2365_putd( cluster_xy & ((1<<Y_WIDTH)-1) );
2366_puts("] / checked_xcu_paddr = ");
2367_putl( (paddr_t)address + (((paddr_t)extend)<<32) );
2368_puts("\n");
2369#endif
2370                    }
2371                    break;
2372                }
2373            }  // end switch periph type
2374        }  // end for periphs
2375
2376#if BOOT_DEBUG_PERI
2377_puts("\n[BOOT DEBUG] Coprocessors initialisation in cluster[");
2378_putd( x );
2379_puts(",");
2380_putd( y );
2381_puts("]\n");
2382#endif
2383
2384        // loop on coprocessors
2385        for ( coproc_id = cluster[cluster_id].coproc_offset;
2386              coproc_id < cluster[cluster_id].coproc_offset +
2387              cluster[cluster_id].coprocs; coproc_id++ ) 
2388        {
2389
2390#if BOOT_DEBUG_PERI
2391_puts("- coprocessor name : ");
2392_puts(coproc[coproc_id].name);
2393_puts(" / nb ports = ");
2394_putd((unsigned int) coproc[coproc_id].ports);
2395_puts("\n");
2396#endif
2397            // loop on the coprocessor ports
2398            for ( cp_port_id = coproc[coproc_id].port_offset;
2399                  cp_port_id < coproc[coproc_id].port_offset + coproc[coproc_id].ports;
2400                  cp_port_id++ ) 
2401            {
2402                // get global index of associted vobj
2403                unsigned int vobj_id   = cp_port[cp_port_id].mwmr_vobj_id; 
2404
2405                // get MWMR channel base address
2406                page_table_t* ptab  = (page_table_t*)_ptabs_vaddr[0][x][y];
2407                unsigned int  vbase = vobj[vobj_id].vbase;
2408                unsigned int  ppn;
2409                unsigned int  flags;
2410                paddr_t       pbase;
2411
2412                _v2p_translate( ptab, 
2413                                vbase>>12 , 
2414                                &ppn, 
2415                                &flags );
2416
2417                pbase = ((paddr_t)ppn)<<12;
2418
2419                // initialise cp_port
2420                _mwr_hw_init( cluster_xy,
2421                              cp_port_id, 
2422                              cp_port[cp_port_id].direction, 
2423                              pbase );
2424#if BOOT_DEBUG_PERI
2425_puts("     port direction: ");
2426_putd( (unsigned int)cp_port[cp_port_id].direction );
2427_puts(" / mwmr_channel_pbase = ");
2428_putl( pbase );
2429_puts(" / name = ");
2430_puts(vobj[vobj_id].name);
2431_puts("\n"); 
2432#endif
2433            } // end for cp_ports
2434        } // end for coprocs
2435    } // end for clusters
2436} // end boot_peripherals_init()
2437
2438/////////////////////////////////////////////////////////////////////////
2439// This function initialises the physical memory allocators in each
2440// cluster containing a RAM pseg.
2441/////////////////////////////////////////////////////////////////////////
2442void boot_pmem_init() 
2443{
2444    mapping_header_t*  header     = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
2445    mapping_cluster_t* cluster    = _get_cluster_base(header);
2446    mapping_pseg_t*    pseg       = _get_pseg_base(header);
2447
2448    unsigned int cluster_id;
2449    unsigned int pseg_id;
2450
2451    // scan all clusters
2452    for ( cluster_id = 0 ; cluster_id < X_SIZE*Y_SIZE ; cluster_id++ ) 
2453    {
2454        // scan the psegs in cluster to find first pseg of type RAM
2455        unsigned int pseg_min = cluster[cluster_id].pseg_offset;
2456        unsigned int pseg_max = pseg_min + cluster[cluster_id].psegs;
2457        for ( pseg_id = pseg_min ; pseg_id < pseg_max ; pseg_id++ )
2458        {
2459            if ( pseg[pseg_id].type == PSEG_TYPE_RAM )
2460            {
2461                unsigned int x    = cluster[cluster_id].x;
2462                unsigned int y    = cluster[cluster_id].y;
2463                unsigned int base = (unsigned int)pseg[pseg_id].base;
2464                unsigned int size = (unsigned int)pseg[pseg_id].length;
2465                _pmem_alloc_init( x, y, base, size );
2466
2467#if BOOT_DEBUG_PT
2468_puts("\n[BOOT DEBUG] pmem allocator initialised in cluster[");
2469_putd( x );
2470_puts(",");
2471_putd( y );
2472_puts("] base = ");
2473_putx( base );
2474_puts(" / size = ");
2475_putx( size );
2476_puts("\n");
2477#endif
2478               break;
2479            }
2480        }
2481    }
2482} // end boot_pmem_init()
2483 
2484/////////////////////////////////////////////////////////////////////////
2485// This function is the entry point of the boot code for all processors.
2486// Most of this code is executed by Processor 0 only.
2487/////////////////////////////////////////////////////////////////////////
2488void boot_init() 
2489{
2490    mapping_header_t*  header     = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
2491    mapping_cluster_t* cluster    = _get_cluster_base(header);
2492    unsigned int       gpid       = _get_procid();
2493 
2494    if ( gpid == 0 )    // only Processor 0 does it
2495    {
2496        _puts("\n[BOOT] boot_init start at cycle ");
2497        _putd(_get_proctime());
2498        _puts("\n");
2499
2500        // Load the map.bin file into memory and check it
2501        boot_mapping_init();
2502
2503        _puts("\n[BOOT] Mapping \"");
2504        _puts( header->name );
2505        _puts("\" loaded at cycle ");
2506        _putd(_get_proctime());
2507        _puts("\n");
2508
2509        // Initializes the physical memory allocators
2510        boot_pmem_init();
2511
2512        _puts("\n[BOOT] Physical memory allocators initialised at cycle ");
2513        _putd(_get_proctime());
2514        _puts("\n");
2515
2516        // Build page tables
2517        _ptabs_init();
2518
2519        _puts("\n[BOOT] Page tables initialised at cycle ");
2520        _putd(_get_proctime());
2521        _puts("\n");
2522
2523        // Activate MMU for proc [0,0,0]
2524        _set_mmu_ptpr( (unsigned int)(_ptabs_paddr[0][0][0]>>13) );
2525        _set_mmu_mode( 0xF );
2526
2527        _puts("\n[BOOT] Processor[0,0,0] : MMU activation at cycle ");
2528        _putd(_get_proctime());
2529        _puts("\n");
2530
2531        // Initialise private vobjs in vspaces
2532        boot_vobjs_init();
2533
2534        _puts("\n[BOOT] Private vobjs initialised at cycle ");
2535        _putd(_get_proctime());
2536        _puts("\n");
2537
2538        // Initialise schedulers
2539        boot_schedulers_init();
2540
2541        _puts("\n[BOOT] Schedulers initialised at cycle ");
2542        _putd(_get_proctime());
2543        _puts("\n");
2544       
2545        // Set CP0_SCHED register for proc [0,0,0]
2546        _set_sched( (unsigned int)_schedulers[0][0][0] );
2547
2548        // Initialise non replicated peripherals
2549        boot_peripherals_init();
2550
2551        _puts("\n[BOOT] Non replicated peripherals initialised at cycle ");
2552        _putd(_get_proctime());
2553        _puts("\n");
2554
2555        // Loading all .elf files
2556        boot_elf_load();
2557
2558        // P0 starts all other processors
2559        unsigned int clusterid, p;
2560
2561        for ( clusterid = 0 ; clusterid < X_SIZE*Y_SIZE ; clusterid++ ) 
2562        {
2563            unsigned int nprocs     = cluster[clusterid].procs;
2564            unsigned int x          = cluster[clusterid].x;
2565            unsigned int y          = cluster[clusterid].y;
2566            unsigned int cluster_xy = (x<<Y_WIDTH) + y;
2567
2568            for ( p = 0 ; p < nprocs; p++ ) 
2569            {
2570                if ( (nprocs > 0) && ((clusterid != 0) || (p != 0)) )
2571                {
2572                    _xcu_send_wti( cluster_xy, p, (unsigned int)boot_entry );
2573                }
2574            }
2575        }
2576 
2577    }  // end monoprocessor boot
2578
2579    ///////////////////////////////////////////////////////////////////////////////
2580    //            Parallel execution starts actually here
2581    ///////////////////////////////////////////////////////////////////////////////
2582
2583    // all processor initialise the SCHED register
2584    // from the _schedulers[x][y][lpid array]
2585    unsigned int cluster_xy = gpid >> P_WIDTH;
2586    unsigned int lpid       = gpid & ((1<<P_WIDTH)-1);
2587    unsigned int x          = cluster_xy >> Y_WIDTH;
2588    unsigned int y          = cluster_xy & ((1<<Y_WIDTH)-1);
2589    _set_sched( (unsigned int)_schedulers[x][y][lpid] );
2590
2591    // all processors (but Proc[0,0,0]) activate MMU
2592    if ( gpid != 0 )
2593    {
2594        _set_mmu_ptpr( (unsigned int)(_ptabs_paddr[0][x][y]>>13) );
2595        _set_mmu_mode( 0xF );
2596    }
2597
2598    // all processors reset BEV bit in the status register to use
2599    // the GIET_VM exception handler instead of the PRELOADER exception handler
2600    _set_sr( 0 );
2601
2602    // all processors jump to kernel_init
2603    // using the address defined in the giet_vsegs.ld file
2604    unsigned int kernel_entry = (unsigned int)&kernel_init_vbase;
2605    asm volatile( "jr   %0" ::"r"(kernel_entry) );
2606
2607} // end boot_init()
2608
2609
2610// Local Variables:
2611// tab-width: 4
2612// c-basic-offset: 4
2613// c-file-offsets:((innamespace . 0)(inline-open . 0))
2614// indent-tabs-mode: nil
2615// End:
2616// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
2617
Note: See TracBrowser for help on using the repository browser.