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

Last change on this file since 554 was 552, checked in by nicolas.van.phan@…, 6 years ago

Stop fetching instructions from local cluster

At the beginning of the bootloader, all cores in all clusters fetch
their instruction from the memory bank of *cluster 00*.
After replication of the code in all cluster, the cores would
fetch their instruction in their own cluster.
However, to do this, they have to write to coprocessor 2 register 25,
which doesn't exist on the real LETI machine.
As a consequence, this feature has been removed, bringing a contention
point since all cluster now fetch instructions from cluster 00.
This issue will have to be solved in the future.

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