source: trunk/boot/tsar_mips32/boot.c @ 665

Last change on this file since 665 was 653, checked in by alain, 5 years ago

Remove the hard_config.h file include.

File size: 38.1 KB
RevLine 
[439]1/*
2 * boot.c - TSAR bootloader implementation.
3 *
[572]4 * Authors :   Vu Son  (2016)
[623]5 *             Alain Greiner (2016,2017,2018,2019)
[439]6 *
7 * Copyright (c) UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25/****************************************************************************
26 * This file contains the ALMOS-MKH. boot-loader for the TSAR architecture. *
[624]27 * that is a clusterised, shared memory, multi-processor architecture,      *
[439]28 * where each processor core is identified by a composite index [cxy,lid]   *
29 * with one physical memory bank per cluster.                               *
30 *                                                                          *
31 * The 'boot.elf' file (containing the boot-loader binary code) is stored   *
[653]32 * on disk (not in the FAT file system), and must be loaded into memory     *
33 * by the generic (OS undependant) preloader.                               *
[439]34 *                                                                          *
[623]35 * The main task of the boot-loader is to load in the first physical page   *
36 * of each cluster a copy of the kernel code (segments "kcode" and "kdata") *
37 * and to build - in each cluster - a cluster specific description of the   *
38 * hardware archtecture, stored in the "kdata" segment as the boot_info_t   *
[653]39 * structure. These "kernel.elf" and "arch_info.bin" files are supposed     *
40 * to be stored on disk in a FAT32 file system.                             *
[439]41 *                                                                          *
[623]42 * All cores contribute to the boot procedure, but all cores are not        *
43 * simultaneously active:                                                   *
44 * - in a first phase, only core[0][0] is running (core 0 in cluster 0).    *
45 * - in a second phase, only core[cxy][0] is running in each cluster.       *
46 * - in last phase, all core[cxy][lid] are running.                         *
[439]47 *                                                                          *
[623]48 * Finally, all cores jump to the kernel_init() function that makes the     *                 
49 * actual kernel initialisation.                                            *
[439]50 *                                                                          *
[653]51 * Implementation note:                                                     *
52 *                                                                          *
[623]53 * To allows each core to use the local copy of both the boot code and the  *
54 * kernel code, the boot-loader builds a minimal and temporary BPT (Boot    *
55 * Page Table) containing only two big pages: page[0] maps the kernel code, *
56 * and page 1 maps the boot code.                                           *
[439]57 ****************************************************************************/
58
59#include <elf-types.h>
[457]60#include <hal_kernel_types.h>
[439]61
62#include <kernel_config.h>
63#include <boot_config.h>
64
65#include <arch_info.h>
66#include <boot_info.h>
67
68#include <boot_utils.h>
69#include <boot_fat32.h>
70#include <boot_bdv_driver.h>
71#include <boot_hba_driver.h>
72#include <boot_tty_driver.h>
73
74/*****************************************************************************
75 *                                 Macros.                             
76 ****************************************************************************/
77
78#define PAGE_ROUND_DOWN(x)  ((x) & (~PPM_PAGE_SIZE -1))
79#define PAGE_ROUND_UP(x)    (((x) + PPM_PAGE_SIZE-1) &   \
80                            (~(PPM_PAGE_SIZE-1)))
81
82/*****************************************************************************
83 *                             Global variables.                           
84 ****************************************************************************/
85
[623]86// the Boot Page Table contains two PTE1, and should be aligned on 8 Kbytes
87
[624]88uint32_t                        boot_pt[2] __attribute__((aligned(0x2000)));
[623]89
[439]90// synchronization variables.
91
[623]92volatile boot_remote_spinlock_t tty0_lock;        // protect TTY0 access
93volatile boot_remote_barrier_t  global_barrier;   // synchronize CP0 cores
94volatile boot_remote_barrier_t  local_barrier;    // synchronize cores in one cluster
95uint32_t                        active_cores_nr;  // number of expected CP0s
[439]96 
97// kernel segments layout variables
98
99uint32_t                        seg_kcode_base;   // kcode segment base address
100uint32_t                        seg_kcode_size;   // kcode segment size (bytes)
101uint32_t                        seg_kdata_base;   // kdata segment base address
102uint32_t                        seg_kdata_size;   // kdata segment size (bytes)
103
[624]104uint32_t                        kernel_entry;     // kernel_init() function
[439]105
[623]106// Functions
[439]107
[524]108extern void boot_entry( void );    // boot_loader entry point
109extern void boot_loader( lid_t lid, cxy_t cxy );
[439]110
[572]111
112#if DEBUG_BOOT_INFO
[439]113/*********************************************************************************
[572]114 * This debug function returns the printable string for each device type.
[439]115 ********************************************************************************/
[572]116static char * device_type_str( enum device_types_e dev_type ) 
117{
118    switch ( dev_type ) 
119    {
[534]120        case DEV_TYPE_RAM_SCL: return "RAM_SCL";
121        case DEV_TYPE_ROM_SCL: return "ROM_SCL";
122        case DEV_TYPE_FBF_SCL: return "FBF_SCL";
123        case DEV_TYPE_IOB_TSR: return "IOB_TSR";
124        case DEV_TYPE_IOC_BDV: return "IOC_BDV";
125        case DEV_TYPE_IOC_HBA: return "IOC_HBA";
126        case DEV_TYPE_IOC_SDC: return "IOC_SDC";
127        case DEV_TYPE_IOC_SPI: return "IOC_SPI";
128        case DEV_TYPE_IOC_RDK: return "IOC_RDK";
129        case DEV_TYPE_MMC_TSR: return "MMC_TSR";
130        case DEV_TYPE_DMA_SCL: return "DMA_SCL";
131        case DEV_TYPE_NIC_CBF: return "NIC_CBF";
132        case DEV_TYPE_TIM_SCL: return "TIM_SCL";
133        case DEV_TYPE_TXT_TTY: return "TXT_TTY";
[557]134        case DEV_TYPE_TXT_MTY: return "TXT_MTY";
[534]135        case DEV_TYPE_ICU_XCU: return "ICU_XCU";
136        case DEV_TYPE_PIC_TSR: return "PIC_TSR";
137        default:               return "undefined";
138    }
[439]139}
[572]140#endif
[439]141
142/************************************************************************************
143 * This function loads the arch_info.bin file into the boot cluster memory.
144 ***********************************************************************************/
[474]145static void boot_archinfo_load( void )
[439]146{
[624]147    archinfo_header_t * header = (archinfo_header_t*)ARCHINFO_BASE; 
[439]148   
149    // Load file into memory
150    if (boot_fat32_load(ARCHINFO_PATHNAME, ARCHINFO_BASE, ARCHINFO_MAX_SIZE))
151    {
[624]152        boot_printf("\n[BOOT ERROR]in %s : <%s> file not found\n",
153        __FUNCTION__, ARCHINFO_PATHNAME);
[439]154        boot_exit();
155    }
156
157    if (header->signature != ARCHINFO_SIGNATURE)
158    {
159        boot_printf("\n[BOOT_ERROR]: boot_archinfo_load(): "
[572]160        "<%s> file signature should be %x\n",
161        ARCHINFO_PATHNAME, ARCHINFO_SIGNATURE);
[439]162        boot_exit();
163    }
164
165#if DEBUG_BOOT_INFO
166boot_printf("\n[BOOT INFO] in %s : file %s loaded at address = %x\n",
[572]167__FUNCTION__ , ARCHINFO_PATHNAME , ARCHINFO_BASE );
[439]168#endif
169
170} // boot_archinfo_load()
171
172/**************************************************************************************
173 * This function loads the 'kernel.elf' file into the boot cluster memory buffer,
[624]174 * analyzes it, and places the kcode and kdata segments at their final physical
175 * adresses (defined the .elf file), and set the kernel layout global variables.
[439]176 *************************************************************************************/
[474]177static void boot_kernel_load( void )
[439]178{
179    Elf32_Ehdr * elf_header;      // pointer on kernel.elf header. 
180    Elf32_Phdr * program_header;  // pointer on kernel.elf program header.
181    uint32_t     phdr_offset;     // program header offset in kernel.elf file.
182    uint32_t     segments_nb;     // number of segments in kernel.elf file.
183    uint32_t     seg_src_addr;    // segment address in kernel.elf file (source).
184    uint32_t     seg_paddr;       // segment local physical address of segment
185    uint32_t     seg_offset;      // segment offset in kernel.elf file
186    uint32_t     seg_filesz;      // segment size (bytes) in kernel.elf file
187    uint32_t     seg_memsz;       // segment size (bytes) in memory image.
188    bool_t       kcode_found;     // kcode segment found.
189    bool_t       kdata_found;     // kdata segment found.
190    uint32_t     seg_id;          // iterator for segments loop.
191
192#if DEBUG_BOOT_ELF
193boot_printf("\n[BOOT INFO] %s enters for file %s at cycle %d\n",
[572]194__FUNCTION__ , KERNEL_PATHNAME , boot_get_proctime() );
[439]195#endif
196
197    // Load kernel.elf file into memory buffer
198    if ( boot_fat32_load(KERNEL_PATHNAME, KERN_BASE, KERN_MAX_SIZE) )
199    {
200        boot_printf("\n[BOOT ERROR] in %s : <%s> file not found\n",
201                    KERNEL_PATHNAME);
202        boot_exit();
203    }
204
205    // get pointer to kernel.elf header 
206    elf_header = (Elf32_Ehdr*)KERN_BASE;
207
208    // check signature
209    if ((elf_header->e_ident[EI_MAG0] != ELFMAG0)   ||
210        (elf_header->e_ident[EI_MAG1] != ELFMAG1)   ||
211        (elf_header->e_ident[EI_MAG2] != ELFMAG2)   ||
212        (elf_header->e_ident[EI_MAG3] != ELFMAG3))
213    {
214        boot_printf("\n[BOOT_ERROR]: boot_kernel_load(): "
215                    "<%s> is not an ELF file\n",
216                    KERNEL_PATHNAME);
217        boot_exit();
218    }
219
220    // Get program header table offset and number of segments
221    phdr_offset     = elf_header->e_phoff;
222    segments_nb     = elf_header->e_phnum;
223
224    // Get program header table pointer
225    program_header  = (Elf32_Phdr*)(KERN_BASE + phdr_offset);
226
227    // loop on segments
228    kcode_found  = false;
229    kdata_found  = false;
230    for (seg_id = 0; seg_id < segments_nb; seg_id++) 
231    {
232        if (program_header[seg_id].p_type == PT_LOAD)   // Found one loadable segment
233        {
234            // Get segment attributes.
235            seg_paddr    = program_header[seg_id].p_paddr;   
236            seg_offset   = program_header[seg_id].p_offset;
237            seg_filesz   = program_header[seg_id].p_filesz;
238            seg_memsz    = program_header[seg_id].p_memsz;
239
240            // get segment base address in buffer
241            seg_src_addr = (uint32_t)KERN_BASE + seg_offset;
242
243            // Load segment to its final physical memory address
244            boot_memcpy( (void*)seg_paddr, 
245                         (void*)seg_src_addr, 
246                         seg_filesz );
247
248#if DEBUG_BOOT_ELF
249boot_printf("\n[BOOT INFO] in %s for file %s : found loadable segment\n"
[572]250"   base = %x / size = %x\n",
251__FUNCTION__ , KERNEL_PATHNAME , seg_paddr , seg_memsz );
[439]252#endif
253
254            // Fill remaining memory with zero if (filesz < memsz).
255            if( seg_memsz < seg_filesz )
256            {
257                boot_memset( (void*)(seg_paddr + seg_filesz), 0, seg_memsz - seg_filesz);
258            }
259
260            // Note: we suppose that the 'kernel.elf' file contains exactly
[624]261            // two loadable segments : kcode & kdata
[439]262
[624]263            if( program_header[seg_id].p_paddr == KCODE_BASE )     // kcode segment
[439]264            {
265                if( kcode_found )
266                {
267                    boot_printf("\n[BOOT_ERROR] in %s for file %s :\n"
[572]268                    "   two kcode segments found\n",
269                    __FUNCTION__ , KERNEL_PATHNAME );
[439]270                    boot_exit();
271                } 
272
273                kcode_found     = true;
274                seg_kcode_base = seg_paddr;
275                seg_kcode_size = seg_memsz;
276            }
277            else                                                    // kdata segment
278            {
279                if( kdata_found )
280                {
281                    boot_printf("\n[BOOT_ERROR] in %s for file %s :\n"
[572]282                    "   two kdata segments found\n",
283                    __FUNCTION__ , KERNEL_PATHNAME );
[439]284                    boot_exit();
285                } 
286
287                kdata_found     = true;
288                seg_kdata_base = seg_paddr;
289                seg_kdata_size = seg_memsz;
290            }
291        }
292    }
293
294    // check kcode & kdata segments found
295    if( kcode_found == false )
296    {
297        boot_printf("\n[BOOT_ERROR] in %s for file %s : seg_kcode not found\n",
[572]298        __FUNCTION__ , KERNEL_PATHNAME );
[439]299        boot_exit();
300    }
301    if( kdata_found == false )
302    {
303        boot_printf("\n[BOOT_ERROR] in %s for file %s : seg_kdata not found\n",
[572]304        __FUNCTION__ , KERNEL_PATHNAME );
[439]305        boot_exit();
306    }
307
308    // check segments sizes
309    if( (seg_kcode_size + seg_kdata_size) > KCODE_MAX_SIZE )
310    {
311        boot_printf("\n[BOOT_ERROR] in %s for file %s : seg_kcode + seg_kdata too large\n",
[572]312        __FUNCTION__ , KERNEL_PATHNAME );
[439]313    }
314
315    // set entry point
316    kernel_entry = (uint32_t)elf_header->e_entry;
317
318#if DEBUG_BOOT_ELF
319boot_printf("\n[BOOT INFO] %s completed for file %s at cycle %d\n",
[572]320__FUNCTION__ , KERNEL_PATHNAME , boot_get_proctime() );
[439]321#endif
322
323} // boot_kernel_load()
324
325/*************************************************************************************
326 * This function initializes the  boot_info_t structure for a given cluster.
327 * @ boot_info  : pointer to local boot_info_t structure 
328 * @ cxy        : cluster identifier                   
329 ************************************************************************************/
330static void boot_info_init( boot_info_t * boot_info,
331                            cxy_t         cxy )
332{
333    archinfo_header_t  * header;
334    archinfo_core_t    * core_base;     
335    archinfo_cluster_t * cluster_base; 
336    archinfo_device_t  * device_base;
337    archinfo_irq_t     * irq_base; 
338
339    archinfo_cluster_t * cluster; 
340    archinfo_cluster_t * my_cluster = NULL;   // target cluster
[572]341    archinfo_cluster_t * io_cluster = NULL;   // external peripherals cluster
[439]342
343    archinfo_core_t    * core;
344    uint32_t             core_id; 
345    archinfo_device_t  * device;
346    uint32_t             device_id;
347    archinfo_irq_t     * irq; 
348    uint32_t             irq_id;
349    uint32_t             end;
350    boot_device_t      * boot_dev; 
351
[624]352 #if DEBUG_BOOT_INFO
353boot_printf("\n[BOOT INFO] %s : enter at cycle %d\n",
354__FUNCTION__ , boot_get_proctime() );
355#endif
356
357   // get pointer on ARCHINFO header  and on the four arch_info arrays
[439]358    header       = (archinfo_header_t*)ARCHINFO_BASE;
359    core_base    = archinfo_get_core_base   (header);
360    cluster_base = archinfo_get_cluster_base(header);
361    device_base  = archinfo_get_device_base (header);
362    irq_base     = archinfo_get_irq_base    (header);
363
364    // Initialize global platform parameters
365    boot_info->x_size       = header->x_size;
366    boot_info->y_size       = header->y_size;
367    boot_info->x_width      = header->x_width;
368    boot_info->y_width      = header->y_width;
369    boot_info->paddr_width  = header->paddr_width;
370    boot_info->io_cxy       = header->io_cxy;
371
372    // Initialize kernel segments from global variables
373    boot_info->kcode_base  = seg_kcode_base;
374    boot_info->kcode_size  = seg_kcode_size;
375    boot_info->kdata_base  = seg_kdata_base;
376    boot_info->kdata_size  = seg_kdata_size;
377
[572]378    // loop on arch_info clusters to build cluster_info[][] array
379    // and get io_cluster and my_cluster pointers
[439]380    for (cluster =  cluster_base;
381         cluster < &cluster_base[header->x_size * header->y_size];
382         cluster++)
383    {
[557]384        int x = cluster->cxy >> Y_WIDTH;
385        int y = cluster->cxy & ((1 << Y_WIDTH) - 1);
[572]386        boot_info->cluster_info[x][y] = (uint8_t)cluster->cores;
[557]387
[572]388        if( cluster->cxy == cxy )            my_cluster = cluster;
389        if( cluster->cxy == header->io_cxy ) io_cluster = cluster;
[439]390    }
391
392    if( my_cluster == NULL ) 
393    {
394        boot_printf("\n[ERROR] in %s : cannot found cluster %x in arch_info\n",
[572]395        __FUNCTION__ , cxy );
[439]396        boot_exit();
397    }
398
399    if( io_cluster == NULL ) 
400    {
401        boot_printf("\n[ERROR] in %s : cannot found io_cluster %x in arch_info\n",
[572]402        __FUNCTION__ , cxy );
[439]403        boot_exit();
404    }
405
406    //////////////////////////////////////////////////////////
407    // initialize the boot_info array of external peripherals
408
409#if DEBUG_BOOT_INFO
410boot_printf("\n[BOOT INFO] %s : external peripherals at cycle %d\n",
[572]411__FUNCTION__ , boot_get_proctime() );
[439]412#endif
413
414    device_id = 0;
415    for (device = &device_base[io_cluster->device_offset];
416         device < &device_base[io_cluster->device_offset + io_cluster->devices];
417         device++ )
418    {
419        if( device_id >= CONFIG_MAX_EXT_DEV ) 
420        {
421            boot_printf("\n[ERROR] in %s : too much external devices in arch_info\n",
[572]422            __FUNCTION__ );
[439]423            boot_exit();
424        }
425       
426        // keep only external devices
427        if( (device->type != DEV_TYPE_RAM_SCL) &&
428            (device->type != DEV_TYPE_ICU_XCU) &&
429            (device->type != DEV_TYPE_MMC_TSR) &&
[550]430            (device->type != DEV_TYPE_DMA_SCL) &&
431            (device->type != DEV_TYPE_TXT_MTY) &&
432            (device->type != DEV_TYPE_IOC_SPI) ) 
[439]433        {
434            boot_dev = &boot_info->ext_dev[device_id];
435
436            boot_dev->type     = device->type;
437            boot_dev->base     = device->base;
438            boot_dev->channels = device->channels;
439            boot_dev->param0   = device->arg0;   
440            boot_dev->param1   = device->arg1;   
441            boot_dev->param2   = device->arg2;   
442            boot_dev->param3   = device->arg3;   
443            boot_dev->irqs     = device->irqs;   
444
445            device_id++;
446
447#if DEBUG_BOOT_INFO
448boot_printf("  - %s : base = %l / size = %l / channels = %d / irqs = %d\n",
[572]449device_type_str(device->type), device->base, device->size, device->channels, device->irqs );
[439]450#endif
451        }
452   
453        // handle IRQs for PIC
454        if (device->type == DEV_TYPE_PIC_TSR) 
455        {
456            for (irq_id = 0; irq_id < CONFIG_MAX_EXTERNAL_IRQS ; irq_id++)
457            {
458                boot_dev->irq[irq_id].valid  = 0;
459            }
460
461            for (irq = &irq_base[device->irq_offset];
462                 irq < &irq_base[device->irq_offset + device->irqs];
463                 irq++)
464            {
465                boot_dev->irq[irq->port].valid    = 1;
466                boot_dev->irq[irq->port].dev_type = irq->dev_type;
467                boot_dev->irq[irq->port].channel  = irq->channel;
468                boot_dev->irq[irq->port].is_rx    = irq->is_rx;
469
470#if DEBUG_BOOT_INFO
471boot_printf("    . irq_port = %d / source = %s / channel = %d / is_rx = %d\n",
[572]472irq->port , device_type_str( irq->dev_type ) , irq->channel , irq->is_rx );
[439]473#endif
474            }
475        }
476    }   // end loop on io_cluster peripherals
477
478    // initialize number of external peripherals
479    boot_info->ext_dev_nr = device_id;
480
481    // Initialize cluster specific resources
482    boot_info->cxy  = my_cluster->cxy;
483
484#if DEBUG_BOOT_INFO
485boot_printf("\n[BOOT INFO] %s : cores in cluster %x\n", __FUNCTION__ , cxy );
486#endif
487
488    ////////////////////////////////////////
489    // Initialize array of core descriptors
490    core_id = 0;
491    for (core = &core_base[my_cluster->core_offset];
492         core < &core_base[my_cluster->core_offset + my_cluster->cores];
493         core++ )
494    {
495        boot_info->core[core_id].gid = (gid_t)core->gid;
496        boot_info->core[core_id].lid = (lid_t)core->lid; 
497        boot_info->core[core_id].cxy = (cxy_t)core->cxy;
498
499#if DEBUG_BOOT_INFO
500boot_printf("  - core_gid = %x : cxy = %x / lid = %d\n", 
[572]501core->gid , core->cxy , core->lid );
[439]502#endif
503        core_id++;
504    }
505
506    // Initialize number of cores in my_cluster
507    boot_info->cores_nr = core_id;
508
509    //////////////////////////////////////////////////////////////////////
510    // initialise boot_info array of internal devices (RAM, ICU, MMC, DMA)
511
512#if DEBUG_BOOT_INFO
[572]513boot_printf("\n[BOOT INFO] %s : internal peripherals in cluster %x\n",
514__FUNCTION__ , cxy );
[439]515#endif
516
517    device_id = 0;
518    for (device = &device_base[my_cluster->device_offset];
519         device < &device_base[my_cluster->device_offset + my_cluster->devices];
520         device++ )
521    {
522        // keep only internal devices
523        if( (device->type == DEV_TYPE_RAM_SCL) ||
524            (device->type == DEV_TYPE_ICU_XCU) ||
525            (device->type == DEV_TYPE_MMC_TSR) ||
[534]526            (device->type == DEV_TYPE_DMA_SCL) ||
[550]527            (device->type == DEV_TYPE_TXT_MTY) ||
528            (device->type == DEV_TYPE_IOC_SPI) )
[439]529        {
530            if (device->type == DEV_TYPE_RAM_SCL)   // RAM
531            {
[550]532                // set number of physical memory pages
[439]533                boot_info->pages_nr   = device->size >> CONFIG_PPM_PAGE_SHIFT;
534
535#if DEBUG_BOOT_INFO
536boot_printf("  - RAM : %x pages\n", boot_info->pages_nr );
537#endif
538            }
[534]539            else                                    // ICU / MMC / DMA / MTY
[439]540            {
541                if( device_id >= CONFIG_MAX_INT_DEV ) 
542                {
543                    boot_printf("\n[ERROR] in %s : too much internal devices in cluster %x\n",
544                                __FUNCTION__ , cxy );
545                    boot_exit();
546                }
547       
548                boot_dev = &boot_info->int_dev[device_id];
549
550                boot_dev->type     = device->type;
551                boot_dev->base     = device->base;
552                boot_dev->channels = device->channels;
553                boot_dev->param0   = device->arg0;   
554                boot_dev->param1   = device->arg1;   
555                boot_dev->param2   = device->arg2;   
556                boot_dev->param3   = device->arg3;   
557                boot_dev->irqs     = device->irqs; 
558
559                device_id++;
560
561#if DEBUG_BOOT_INFO
562boot_printf("  - %s : base = %l / size = %l / channels = %d / irqs = %d\n",
[572]563device_type_str( device->type ) , device->base , device->size ,
564device->channels , device->irqs );   
[439]565#endif
566
567                // handle IRQs for ICU
568                if (device->type == DEV_TYPE_ICU_XCU) 
569                {
570                    for (irq_id = 0; irq_id < CONFIG_MAX_INTERNAL_IRQS ; irq_id++)
571                    {
572                        boot_dev->irq[irq_id].valid  = 0;
573                    }
574
575                    for (irq = &irq_base[device->irq_offset];
576                         irq < &irq_base[device->irq_offset + device->irqs] ; irq++)
577                    {
578                        boot_dev->irq[irq->port].valid    = 1;
579                        boot_dev->irq[irq->port].dev_type = irq->dev_type;
580                        boot_dev->irq[irq->port].channel  = irq->channel;
581                        boot_dev->irq[irq->port].is_rx    = irq->is_rx;
582
583#if DEBUG_BOOT_INFO
584boot_printf("    . irq_port = %d / source = %s / channel = %d / is_rx = %d\n",
[572]585irq->port , device_type_str( irq->dev_type ) , irq->channel , irq->is_rx );
[439]586#endif
587
588                    }
589                }
590            }
591        }
592    }  // end loop on local peripherals
593
594    // initialize number of internal peripherals
595    boot_info->int_dev_nr = device_id;
596
597   // Get the top address of the kernel segments
598    end = boot_info->kdata_base + boot_info->kdata_size;
599
600    // compute number of physical pages occupied by the kernel code
601    boot_info->pages_offset = ( (end & CONFIG_PPM_PAGE_MASK) == 0 ) ?
602                 (end >> CONFIG_PPM_PAGE_SHIFT) : (end >> CONFIG_PPM_PAGE_SHIFT) + 1;
603
[572]604    // no reserved zones for TSAR architecture
[439]605    boot_info->rsvd_nr = 0;
606
607    // set boot_info signature
608    boot_info->signature = BOOT_INFO_SIGNATURE;
609
610} // boot_info_init()
611
612/***********************************************************************************
613 * This function check the local boot_info_t structure for a given core.
614 * @ boot_info  : pointer to local 'boot_info_t' structure to be checked.
615 * @ lid        : core local identifier, index the core descriptor table.
616 **********************************************************************************/
617static void boot_check_core( boot_info_t * boot_info, 
618                             lid_t         lid)
619{
620    gid_t         gid;        // global hardware identifier of this core
621    boot_core_t * this;       // BOOT_INFO core descriptor of this core. 
622
623    // Get core hardware identifier
624    gid = (gid_t)boot_get_procid();
625
626    // get pointer on core descriptor
627    this = &boot_info->core[lid];
628
629    if ( (this->gid != gid) ||  (this->cxy != boot_info->cxy) )
630    {
631        boot_printf("\n[BOOT ERROR] in boot_check_core() :\n"
632                    " - boot_info cxy = %x\n"
633                    " - boot_info lid = %d\n"
634                    " - boot_info gid = %x\n"
635                    " - actual    gid = %x\n",
636                    this->cxy , this->lid , this->gid , gid );
637        boot_exit();
638    }
639
640} // boot_check_core()
641
642/*********************************************************************************
643 * This function is called by CP0 in cluster(0,0) to activate all other CP0s.
644 * It returns the number of CP0s actually activated.
645 ********************************************************************************/
[527]646static uint32_t boot_wake_all_cp0s( void )
[439]647{
648    archinfo_header_t*  header;         // Pointer on ARCHINFO header
649    archinfo_cluster_t* cluster_base;   // Pointer on ARCHINFO clusters base
650    archinfo_cluster_t* cluster;        // Iterator for loop on clusters
651    archinfo_device_t*  device_base;    // Pointer on ARCHINFO devices base
652    archinfo_device_t*  device;         // Iterator for loop on devices
653    uint32_t            cp0_nb = 0;     // CP0s counter
654
655    header       = (archinfo_header_t*)ARCHINFO_BASE;
656    cluster_base = archinfo_get_cluster_base(header);
657    device_base  = archinfo_get_device_base (header); 
658
659    // loop on all clusters
660    for (cluster = cluster_base;
661         cluster < &cluster_base[header->x_size * header->y_size];
662         cluster++)
663    {
664        // Skip boot cluster.
665        if (cluster->cxy == BOOT_CORE_CXY)
666            continue;
667           
668        // Skip clusters without core (thus without CP0).
669        if (cluster->cores == 0)
670            continue;
671
672        // Skip clusters without device (thus without XICU).
673        if (cluster->devices == 0)
674            continue;
675
676        // search XICU device associated to CP0, and send a WTI to activate it
677        for (device = &device_base[cluster->device_offset];
678             device < &device_base[cluster->device_offset + cluster->devices];
679             device++)
680        {
681            if (device->type == DEV_TYPE_ICU_XCU)
682            {
683
684#if DEBUG_BOOT_WAKUP
685boot_printf("\n[BOOT] core[%x,0] activated at cycle %d\n",
[572]686cluster->cxy , boot_get_proctime );
[439]687#endif
688
689                boot_remote_sw((xptr_t)device->base, (uint32_t)boot_entry);
690                cp0_nb++;
691            }
692        }
693    }
694    return cp0_nb;
695
696} // boot_wake_cp0()
697
698/*********************************************************************************
[623]699 * This function is called by all CP0s to activate the other CPi cores.
[439]700 * @ boot_info  : pointer to local 'boot_info_t' structure.
701 *********************************************************************************/
702static void boot_wake_local_cores(boot_info_t * boot_info)
703{
704    unsigned int     core_id;       
705
706    // get pointer on XCU device descriptor in boot_info
707    boot_device_t *  xcu = &boot_info->int_dev[0];
708 
709    // loop on cores
710    for (core_id = 1; core_id < boot_info->cores_nr; core_id++)
711    {
712
713#if DEBUG_BOOT_WAKUP
714boot_printf("\n[BOOT] core[%x,%d] activated at cycle %d\n",
[572]715boot_info->cxy , core_id , boot_get_proctime() );
[439]716#endif
717        // send an IPI
718        boot_remote_sw( (xptr_t)(xcu->base + (core_id << 2)) , (uint32_t)boot_entry ); 
719    }
720} // boot_wake_local_cores()
721
[623]722/*********************************************************************************
723 * This function is called by all core[cxy][0] to initialize the Boot Page Table:
724 * map two local big pages for the boot code and kernel code.
725 * @ cxy    : local cluster identifier.
726 *********************************************************************************/
727void boot_page_table_init( cxy_t  cxy )
728{
729    // set PTE1 in slot[0] for kernel code
730    uint32_t kernel_attr  = 0x8A800000;                   // flagss : V,C,X,G
731    uint32_t kernel_ppn1  = (cxy << 20) >> 9;             // big physical page index == 0
732    boot_pt[0]            = kernel_attr | kernel_ppn1;
[439]733
[623]734    // set PTE1 in slot[1] for boot code (no global flag)
735    uint32_t boot_attr    = 0x8A000000;                   // flags : V,C,X
736    uint32_t boot_ppn1    = ((cxy << 20) + 512) >> 9;     // big physical page index == 1
737    boot_pt[1]            = boot_attr | boot_ppn1;
738}
739
[439]740/*********************************************************************************
[623]741 * This function is called by all cores to activate the instruction MMU,
742 * and use the local copy of boot code.
743 *********************************************************************************/
744void boot_activate_ins_mmu( cxy_t cxy )
745{
746    // set mmu_ptpr register
747    uint32_t ptpr = ((uint32_t)boot_pt >> 13) | (cxy << 19);
748    asm volatile ( "mtc2   %0,   $0         \n" : : "r" (ptpr) );
749
750    // set ITLB bit in mmu_mode
751    asm volatile ( "mfc2   $26,  $1         \n"
752                   "ori    $26,  $26,  0x8  \n" 
753                   "mtc2   $26,  $1         \n" );
754}
755
756/*********************************************************************************
[439]757 * This main function of the boot-loader is called by the  boot_entry() 
758 * function, and executed by all cores.
759 * The arguments values are computed by the boot_entry code.
760 * @ lid    : core local identifier,
761 * @ cxy    : cluster identifier,
762 *********************************************************************************/
[524]763void boot_loader( lid_t lid,
764                  cxy_t cxy )
[439]765{
766    boot_info_t * boot_info;       // pointer on local boot_info_t structure
767
768    if (lid == 0) 
769    {
[623]770        /************************************i**********************
[653]771         * PHASE Sequencial : only core[0,0] executes it
[623]772         **********************************************************/
773        if (cxy == 0)
[439]774        {
775            // Initialize IOC driver
776            if      (USE_IOC_BDV) boot_bdv_init();
777            else if (USE_IOC_HBA) boot_hba_init();
778            // else if (USE_IOC_SDC) boot_sdc_init();
[572]779            // else if (USE_IOC_SPI) boot_spi_init();
[439]780            else if (!USE_IOC_RDK)
781            {
782                boot_printf("\n[BOOT ERROR] in %s : no IOC driver\n");
783                boot_exit();
784            }
785
786            // Initialize FAT32.
787            boot_fat32_init();
788
789            // Load the 'kernel.elf' file into memory from IOC, and set   
790            // the global variables defining the kernel layout     
791            boot_kernel_load();
792
793            boot_printf("\n[BOOT] core[%x,%d] loaded kernel at cycle %d\n",
[572]794            cxy , lid , boot_get_proctime() );
[439]795
796            // Load the arch_info.bin file into memory.
797            boot_archinfo_load();
798
[572]799            boot_printf("\n[BOOT] core[%x,%d] loaded arch_info at cycle %d\n",
800            cxy , lid , boot_get_proctime() );
801
[439]802            // Get local boot_info_t structure base address.
803            // It is the first structure in the .kdata segment.
804            boot_info = (boot_info_t *)seg_kdata_base;
805
806            // Initialize local boot_info_t structure.
807            boot_info_init( boot_info , cxy );
808
[572]809            boot_printf("\n[BOOT] core[%x,%d] initialised boot_info at cycle %d\n",
810            cxy , lid , boot_get_proctime() );
811
[439]812            // check boot_info signature
813            if (boot_info->signature != BOOT_INFO_SIGNATURE)
814            {
815                boot_printf("\n[BOOT ERROR] in %s reported by core[%x,%d]\n"
[572]816                "  illegal boot_info signature / should be %x\n",
817                __FUNCTION__ , cxy , lid , BOOT_INFO_SIGNATURE );
[439]818                boot_exit();
819            }
820
821            // Check core information.
822            boot_check_core(boot_info, lid);
823
[624]824            // identity maps two big pages for the boot and kernel code,
825            boot_page_table_init( 0 );
[439]826
[624]827            // activate the instruction MMU to use the local copy of boot code
828            boot_activate_ins_mmu( 0 );
[623]829
[653]830            // Activate other core[cxy,0] / set number of activated cores
[623]831            active_cores_nr = boot_wake_all_cp0s() + 1;
832
[653]833            // Wait until all clusters (i.e all core[cxy,0]) ready to enter kernel.
[439]834            boot_remote_barrier( XPTR( BOOT_CORE_CXY , &global_barrier ) ,
[623]835                                 active_cores_nr );
[439]836
837            // activate other local cores
838            boot_wake_local_cores( boot_info );
839
840            // Wait until all local cores in cluster ready
841            boot_remote_barrier( XPTR( cxy , &local_barrier ) , 
842                                 boot_info->cores_nr );
843        }
[623]844        /**************************************************************************
[653]845         * PHASE partially parallel : all core[cxy,0] with (cxy != 0) execute it
[623]846         **************************************************************************/
[439]847        else
848        {
[623]849            // at this point, the DATA extension registers point
[653]850            // on the local cluster cxy to use the local stack,
[623]851            // but all cores must access the code stored in cluster 0
852
[653]853            // Each core[cxy,0] copies the boot code (data and instructions)
[623]854            // from the cluster 0 to the local cluster.
[439]855            boot_remote_memcpy( XPTR( cxy           , BOOT_BASE ),
856                                XPTR( BOOT_CORE_CXY , BOOT_BASE ),
857                                BOOT_MAX_SIZE );
858
[623]859            // from now, it is safe to refer to the boot global variables
[439]860
[653]861#if DEBUG_BOOT_MULTI
862boot_printf("\n[BOOT] core[%x,%d] replicated boot code at cycle %d\n",
863cxy , lid , boot_get_proctime() );
864#endif
[624]865            // identity maps two big pages for the boot and kernel code,
866            boot_page_table_init( cxy );
[439]867
[624]868            // activate the instruction MMU to use the local copy of boot code
869            boot_activate_ins_mmu( cxy );
[623]870
[653]871            // Each core[cxy,0] copies the arch_info.bin into the local memory.
[439]872            boot_remote_memcpy(XPTR(cxy,           ARCHINFO_BASE),
873                               XPTR(BOOT_CORE_CXY, ARCHINFO_BASE),
874                               ARCHINFO_MAX_SIZE );
[653]875#if DEBUG_BOOT_MULTI
876boot_printf("\n[BOOT] core[%x,%d] replicated arch_info at cycle %d\n",
877cxy , lid , boot_get_proctime() );
878#endif
[624]879            // copy the kcode segment into local memory
[439]880            boot_remote_memcpy( XPTR( cxy           , seg_kcode_base ),
881                                XPTR( BOOT_CORE_CXY , seg_kcode_base ),
882                                seg_kcode_size );
883
[653]884            // Each core[cxy,0] copies the kdata segment into local memory
[439]885            boot_remote_memcpy( XPTR( cxy           , seg_kdata_base ),
886                                XPTR( BOOT_CORE_CXY , seg_kdata_base ),
887                                seg_kdata_size );
[653]888#if DEBUG_BOOT_MULTI
889boot_printf("\n[BOOT] core[%x,%d] replicated kernel code at cycle %d\n",
890cxy , lid , boot_get_proctime() );
891#endif
892            // Each core[cxy,0] get local boot_info_t structure base address.
[439]893            boot_info = (boot_info_t*)seg_kdata_base;
894
[653]895            // Each core[cxy,0] initializes local boot_info_t structure.
[439]896            boot_info_init( boot_info , cxy );
897
[653]898#if DEBUG_BOOT_MULTI
899boot_printf("\n[BOOT] core[%x,%d] initialised boot_info at cycle %d\n",
900cxy , lid , boot_get_proctime() );
901#endif
902            // Each core[cxy,0] checks core information.
[439]903            boot_check_core( boot_info , lid );
904
[653]905            // Each core[cxy,0] get number of active clusters from BOOT_CORE cluster
[623]906            uint32_t count = boot_remote_lw( XPTR( 0 , &active_cores_nr ) );
[439]907
[623]908            // Wait until all clusters (i.e all CP0s) ready
[439]909            boot_remote_barrier( XPTR( BOOT_CORE_CXY , &global_barrier ) , count );
910
911            // activate other local cores
912            boot_wake_local_cores( boot_info );
913
914            // Wait until all local cores in cluster ready
915            boot_remote_barrier( XPTR( cxy , &local_barrier ) , 
916                                 boot_info->cores_nr );
917        }
918    }
919    else
920    {
[623]921        /***********************************************************************
[653]922         * PHASE fully parallel : all cores[cxy,lid] with (lid! = 0) execute it
[623]923         **********************************************************************/
[439]924
[624]925        // activate the instruction MMU to use the local copy of the boot code
926        boot_activate_ins_mmu( cxy );
[439]927
928        // Get local boot_info_t structure base address.
929        boot_info = (boot_info_t *)seg_kdata_base;
930
931        // Check core information
932        boot_check_core(boot_info, lid);
933
934        // Wait until all local cores in cluster ready
935        boot_remote_barrier( XPTR( cxy , &local_barrier ) , boot_info->cores_nr );
936    }
937
[624]938    // All cores enter the kernel_init() function. The address is contained in
939    // the "kernel_entry" global variable, set by boot_kernel_load() function.
[439]940    // Each core initialise the following registers before jumping to kernel:
[623]941    // - gr_29    : stack pointer / kernel stack allocated in idle thread descriptor,
942    // - c0_sr    : status register / reset BEV bit
943    // - gr_04    : kernel_init() argument / pointer on boot_info structure
[439]944    // The array of idle-thread descriptors is allocated in the kdata segment,
[623]945    // just after the boot_info structure.
[624]946
947#if DEBUG_BOOT_MULTI
948boot_printf("\n[BOOT] core[%x,%d] jump to kernel_init = %x at cycle %d\n",
[637]949cxy, lid, kernel_entry, boot_get_proctime() );
[624]950#endif
951
[439]952    uint32_t base;
953    uint32_t offset = sizeof( boot_info_t );
954    uint32_t pmask  = CONFIG_PPM_PAGE_MASK;
955    uint32_t psize  = CONFIG_PPM_PAGE_SIZE;
956    if( offset & pmask ) base = seg_kdata_base + (offset & ~pmask) + psize;
957    else                 base = seg_kdata_base + offset;
[623]958    uint32_t sp = base + ((lid + 1) * CONFIG_THREAD_DESC_SIZE) - 16;
[439]959
960    asm volatile( "mfc0  $27,  $12           \n"
961                  "lui   $26,  0xFFBF        \n"
962                  "ori   $26,  $26,  0xFFFF  \n"
963                  "and   $27,  $27,  $26     \n"
964                  "mtc0  $27,  $12           \n"
965                  "move  $4,   %0            \n"
966                  "move  $29,  %1            \n"
[624]967                  "jr    %2                  \n"
[439]968                  :
969                  : "r"(boot_info) ,
970                    "r"(sp) ,
971                    "r"(kernel_entry) 
972                  : "$26" , "$27" , "$29" , "$4" );
973
974
975} // boot_loader()
Note: See TracBrowser for help on using the repository browser.