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

Last change on this file since 528 was 527, checked in by viala@…, 6 years ago

Rewrite if-then-else return function into switch case.

For safety reason and performance:

1) Safety: GCC complain with a warning if you forgot an enum variant.
2) code-gen just outperform naive if-then-else.

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