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

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

Define the kernel_heap[X_SIZE][Y_SIZE] global variables,
and the TTY related global variables in boot.c.

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