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

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

Introducing an explicit FAT initialisation in boot.c.

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