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

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

Cosmetic: simplify the map.bin file checking.

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