source: trunk/kernel/kern/kernel_init.c @ 132

Last change on this file since 132 was 127, checked in by max@…, 7 years ago

style

File size: 38.9 KB
RevLine 
[1]1/*
2 * kernel_init.c - kernel parallel initialization
[127]3 *
[23]4 * Authors :  Mohamed Lamine Karaoui (2015)
5 *            Alain Greiner  (2016,2017)
[1]6 *
7 * Copyright (c) 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
[14]25#include <kernel_config.h>
[1]26#include <errno.h>
27#include <hal_types.h>
28#include <hal_special.h>
29#include <hal_context.h>
[14]30#include <barrier.h>
[1]31#include <remote_barrier.h>
32#include <core.h>
33#include <list.h>
[68]34#include <xlist.h>
[1]35#include <thread.h>
36#include <scheduler.h>
37#include <kmem.h>
38#include <cluster.h>
39#include <string.h>
40#include <memcpy.h>
41#include <ppm.h>
42#include <page.h>
[5]43#include <chdev.h>
[1]44#include <boot_info.h>
45#include <dqdt.h>
46#include <dev_icu.h>
47#include <dev_mmc.h>
[5]48#include <dev_dma.h>
49#include <dev_iob.h>
[1]50#include <dev_ioc.h>
[5]51#include <dev_txt.h>
[1]52#include <dev_pic.h>
53#include <printk.h>
54#include <vfs.h>
[77]55#include <hal_drivers.h>
[23]56#include <devfs.h>
[68]57#include <mapper.h>
[101]58#include <soclib_tty.h>
[1]59
[5]60#define KERNEL_INIT_SYNCHRO  0xA5A5B5B5
[1]61
62///////////////////////////////////////////////////////////////////////////////////////////
63// All these global variables are replicated in all clusters.
64// They are initialised by the kernel_init() function.
[14]65//
[127]66// WARNING : The section names have been defined to control the base addresses of the
[14]67// boot_info structure and the idle thread descriptors, through the kernel.ld script:
[127]68// - the boot_info structure is built by the bootloader, and used by kernel_init.
69//   it must be the first object in the kdata segment.
[14]70// - the array of idle threads descriptors must be placed on the first page boundary after
71//   the boot_info structure in the kdata segment.
[1]72///////////////////////////////////////////////////////////////////////////////////////////
73
[5]74// This variable defines the local boot_info structure
75__attribute__((section(".kinfo")))
[14]76boot_info_t          boot_info;
[5]77
[14]78// This variable defines the "idle" threads descriptors array
79__attribute__((section(".kidle")))
80char                  idle_threads[CONFIG_THREAD_DESC_SIZE *
81                                   CONFIG_MAX_LOCAL_CORES]   CONFIG_PPM_PAGE_ALIGNED;
82
[127]83// This variable defines the local cluster manager
[5]84__attribute__((section(".kdata")))
[19]85cluster_t            cluster_manager                         CONFIG_CACHE_LINE_ALIGNED;
[1]86
[14]87// This variables define the kernel process0 descriptor
[5]88__attribute__((section(".kdata")))
[19]89process_t            process_zero                            CONFIG_CACHE_LINE_ALIGNED;
[1]90
[14]91// This variable defines extended pointers on the distributed chdevs
[5]92__attribute__((section(".kdata")))
[14]93chdev_directory_t    chdev_dir                               CONFIG_CACHE_LINE_ALIGNED;
[1]94
[5]95// This variable contains the input IRQ indexes for the PIC device
96__attribute__((section(".kdata")))
[14]97chdev_pic_input_t    chdev_pic_input                         CONFIG_CACHE_LINE_ALIGNED;
[1]98
[5]99// This variable contains the input IRQ indexes for the ICU device
100__attribute__((section(".kdata")))
[14]101chdev_icu_input_t    chdev_icu_input                         CONFIG_CACHE_LINE_ALIGNED;
[1]102
[14]103// This variable defines the local cluster identifier
[5]104__attribute__((section(".kdata")))
[14]105cxy_t                local_cxy                               CONFIG_CACHE_LINE_ALIGNED;
[5]106
[127]107// This variable defines the TXT0 chdev descriptor
[5]108__attribute__((section(".kdata")))
[14]109chdev_t              txt0_chdev                              CONFIG_CACHE_LINE_ALIGNED;
[1]110
[127]111// This variable is used for CP0 cores synchronisation in kernel_init()
[5]112__attribute__((section(".kdata")))
[14]113remote_barrier_t     global_barrier                          CONFIG_CACHE_LINE_ALIGNED;
[1]114
[127]115// This variable is used for local cores synchronisation in kernel_init()
[14]116__attribute__((section(".kdata")))
117barrier_t            local_barrier                           CONFIG_CACHE_LINE_ALIGNED;
118
[127]119// This variable defines the array of supported File System contexts
[50]120__attribute__((section(".kdata")))
121vfs_ctx_t            fs_context[FS_TYPES_NR]                 CONFIG_CACHE_LINE_ALIGNED;
122
123
[1]124///////////////////////////////////////////////////////////////////////////////////////////
[5]125// This function displays the ALMOS_MKH banner.
[1]126///////////////////////////////////////////////////////////////////////////////////////////
[5]127static void print_banner( uint32_t nclusters , uint32_t ncores )
[127]128{
[5]129    printk("\n"
130           "                    _        __    __     _____     ______         __    __    _   __   _     _   \n"
131           "          /\\       | |      |  \\  /  |   / ___ \\   / _____|       |  \\  /  |  | | / /  | |   | |  \n"
132           "         /  \\      | |      |   \\/   |  | /   \\ | | /             |   \\/   |  | |/ /   | |   | |  \n"
133           "        / /\\ \\     | |      | |\\  /| |  | |   | | | |_____   ___  | |\\  /| |  |   /    | |___| |  \n"
134           "       / /__\\ \\    | |      | | \\/ | |  | |   | | \\_____  \\ |___| | | \\/ | |  |   \\    |  ___  |  \n"
135           "      / ______ \\   | |      | |    | |  | |   | |       | |       | |    | |  | |\\ \\   | |   | |  \n"
136           "     / /      \\ \\  | |____  | |    | |  | \\___/ |  _____/ |       | |    | |  | | \\ \\  | |   | |  \n"
137           "    /_/        \\_\\ |______| |_|    |_|   \\_____/  |______/        |_|    |_|  |_|  \\_\\ |_|   |_|  \n"
138           "\n\n\t\t Advanced Locality Management Operating System / Multi Kernel Hybrid\n"
[127]139           "\n\n\t\t\t Version 0.0   :   %d clusters   /   %d cores per cluster\n\n", nclusters , ncores );
[5]140}
[1]141
142
[5]143///////////////////////////////////////////////////////////////////////////////////////////
[127]144// This static function initializes the TXT0 chdev descriptor, associated to the "kernel
145// terminal", and shared by all kernel instances for debug messages. It also registers it
[14]146// in the chdev directory, containing extended pointers on all chdevs.
147// The global variable txt0_chdev is replicated in all clusters, but only the chdev
148// allocated in I/O cluster is used by ALMOS-MKH.
149// Therefore, this function must be called by a thread running in the I/O cluster.
[127]150// As this TXT0 chdev supports only the TXT_SYNC_WRITE command, we don't create
151// a server thread, we don't allocate a WTI, and we don't initialize the waiting queue.
[5]152///////////////////////////////////////////////////////////////////////////////////////////
153// @ info    : pointer on the local boot-info structure.
154///////////////////////////////////////////////////////////////////////////////////////////
155static void txt0_device_init( boot_info_t * info )
156{
157    boot_device_t * dev_tbl;         // pointer on array of devices in boot_info
[127]158    uint32_t        dev_nr;          // actual number of devices in this cluster
159    xptr_t          base;            // remote pointer on segment base
[5]160    uint32_t        type;            // peripheral type
[127]161    uint32_t        func;            // device functional index
[5]162    uint32_t        impl;            // device implementation index
[127]163    uint32_t        i;               // device index in dev_tbl
164    uint32_t        x;               // X cluster coordinate
165    uint32_t        y;               // Y cluster coordinate
[1]166
[5]167    // get number of peripherals and base of devices array from boot_info
[127]168    dev_nr      = info->ext_dev_nr;
[5]169    dev_tbl     = info->ext_dev;
[1]170
[14]171    // loop on external peripherals to find TXT device
[127]172    for( i = 0 ; i < dev_nr ; i++ )
173    {
[5]174        base        = dev_tbl[i].base;
175        type        = dev_tbl[i].type;
176        func        = FUNC_FROM_TYPE( type );
177        impl        = IMPL_FROM_TYPE( type );
178
[127]179        if (func == DEV_FUNC_TXT )
[5]180        {
[14]181            // initialize basic fields
182            txt0_chdev.func     = func;
183            txt0_chdev.impl     = impl;
184            txt0_chdev.channel  = 0;
185            txt0_chdev.is_rx    = 0;
186            txt0_chdev.base     = base;
[5]187
[14]188            // initialize lock
189            remote_spinlock_init( XPTR( local_cxy , &txt0_chdev.wait_lock ) );
190
[101]191            // TODO use generic device initialisation
192            // hal_drivers_txt_init( &txt0_chdev );
[5]193
[101]194            if( impl == IMPL_TXT_TTY )
195            {
196                txt0_chdev.cmd = &soclib_tty_cmd;
197                txt0_chdev.isr = &soclib_tty_isr;
198                soclib_tty_init( &txt0_chdev );
199            }
200
[5]201            // initialize the replicated chdev_dir[x][y] structures
202            for( x = 0 ; x < info->x_size ; x++ )
203            {
204                for( y = 0 ; y < info->y_size ; y++ )
205                {
206                    cxy_t  cxy = (x<<info->y_width) + y;
[19]207                    hal_remote_swd( XPTR( cxy , &chdev_dir.txt[0] ) ,
[14]208                                    XPTR( local_cxy , &txt0_chdev ) );
[5]209                }
210            }
211
[127]212            kinit_dmsg("\n[INFO] %s : core[%x][0] created TXT0 chdev"
[14]213                       " / paddr = %l at cycle %d\n",
[19]214                       __FUNCTION__ , local_cxy , chdev_func_str( func ),
[101]215                       XPTR(local_cxy , &txt0_chdev) , hal_get_cycles() );
[5]216        }
[127]217    }
218}
[5]219
[1]220///////////////////////////////////////////////////////////////////////////////////////////
[5]221// This static function allocates memory for the chdev (channel_device) descriptors
222// associated to the internal peripherals contained in the local cluster. These internal
223// devices (ICU, MMC, DMA) chdev descriptors are placed in the local cluster.
[1]224// It initialises these device descriptors as specified by the boot_info_t structure,
225// including the dynamic linking with the driver for the specified implementation.
226// Finally, all copies of the devices directory are initialised.
227///////////////////////////////////////////////////////////////////////////////////////////
228// @ info    : pointer on the local boot-info structure.
229///////////////////////////////////////////////////////////////////////////////////////////
[5]230static void internal_devices_init( boot_info_t * info )
[1]231{
[14]232    boot_device_t * dev;             // pointer on boot_info device (ICU/MMC/DMA)
[127]233    uint32_t        x;               // X cluster coordinate
234    uint32_t        y;               // Y cluster coordinate
235    chdev_t       * chdev_ptr;       // local pointer on chdev descriptor
[14]236    xptr_t          chdev_xp;        // extended pointer on chdev descriptor
[1]237
[14]238    ///////////  ICU   //////////
[1]239
[14]240    dev = &info->dev_icu;
[5]241
[14]242    assert( ((info->cores_nr == 0) || (dev->channels != 0)) , __FUNCTION__ ,
243            "ICU device must exist in cluster containing cores" );
[127]244
[14]245    assert( (dev->channels == 1) , __FUNCTION__ ,
246            "channels number must be 1 for ICU device" );
[1]247
[14]248    assert( (FUNC_FROM_TYPE( dev->type ) == DEV_FUNC_ICU ) , __FUNCTION__ ,
249            " inconsistent ICU  device type");
[1]250
[14]251    // create one chdev in local cluster
252    chdev_ptr = chdev_create( FUNC_FROM_TYPE( dev->type ),
253                              IMPL_FROM_TYPE( dev->type ),
254                              0,                              // channel
255                              false,                          // TX
256                              dev->base );
[1]257
[14]258    assert( (chdev_ptr != NULL) , __FUNCTION__ , "cannot allocate ICU chdev" );
[127]259
260    // get extended pointer on chdev descriptor
[14]261    chdev_xp = XPTR( local_cxy , chdev_ptr );
262
[127]263    // make ICU specific initialisation
[14]264    // TODO remove these three parameters
[19]265    dev_icu_init( chdev_ptr , dev->param0 , dev->param1 , dev->param2 );
[14]266
267    // initialize the ICU field in the chdev_dir[x][y] structures
[127]268    // replicated in all clusters, and containing extended pointers
269    // on all remotely accessible devices
[14]270    for( x = 0 ; x < info->x_size ; x++ )
271    {
272        for( y = 0 ; y < info->y_size ; y++ )
[5]273        {
[14]274            cxy_t  cxy = (x<<info->y_width) + y;
275            hal_remote_swd( XPTR( cxy , &chdev_dir.icu[local_cxy] ) , chdev_xp );
[5]276        }
[14]277    }
[1]278
[14]279    // initialize the entries of the local chdev_icu_input structure
[127]280    // defining how internal peripherals are connected to ICU
[14]281    uint32_t   id;
282    uint8_t    valid;
283    uint32_t   src_type;
284    uint8_t    src_ch;
285    uint32_t   src_func;
286    for( id = 0 ; id < CONFIG_MAX_HWIS_PER_ICU ; id++ )
287    {
288        valid    = dev->irq[id].valid;
289        src_type = dev->irq[id].dev_type;
290        src_ch   = dev->irq[id].channel;
291        src_func = FUNC_FROM_TYPE( src_type );
292
293        if( valid ) // only valid local IRQs are registered
[5]294        {
[14]295            if     ( src_func == DEV_FUNC_MMC ) chdev_icu_input.mmc = id;
296            else if( src_func == DEV_FUNC_DMA ) chdev_icu_input.dma[src_ch] = id;
297            else assert( false , __FUNCTION__ , "illegal source device for ICU input" );
298        }
299    }
[1]300
[50]301    kinit_dmsg("\n[INFO] %s : core[%x][0] created ICU chdev at cycle %d\n",
[101]302               __FUNCTION__ , local_cxy , hal_get_cycles() );
[1]303
[127]304    /////////// MMC internal chdev ///////////
[1]305
[14]306    dev = &info->dev_mmc;
[1]307
[14]308    if( dev->channels != 0 )   // MMC device is defined
309    {
[127]310        assert( (dev->channels == 1) , __FUNCTION__ ,
[14]311            "channels number must be 1 for MMC device" );
[1]312
[14]313        assert( (FUNC_FROM_TYPE( dev->type ) == DEV_FUNC_MMC ) , __FUNCTION__ ,
314            " inconsistent MMC device type");
315
316        // create one chdev in local cluster
317        chdev_ptr = chdev_create( FUNC_FROM_TYPE( dev->type ),
318                                  IMPL_FROM_TYPE( dev->type ),
319                                  0,                              // channel
320                                  false,                          // TX
321                                  dev->base );
322
323        assert( (chdev_ptr != NULL) , __FUNCTION__ , "cannot allocate MMC chdev" );
[127]324
325        // get extended pointer on chdev descriptor
[14]326        chdev_xp = XPTR( local_cxy , chdev_ptr );
327
328        // make MMC specific initialisation
[127]329        dev_mmc_init( chdev_ptr );
[14]330
331        // initialize the MMC field in the chdev_dir[x][y] structures
[127]332        // replicated in all clusters, and containing extended pointers
333        // on all remotely accessible devices
[14]334        for( x = 0 ; x < info->x_size ; x++ )
335        {
336            for( y = 0 ; y < info->y_size ; y++ )
[1]337            {
[14]338                cxy_t  cxy = (x<<info->y_width) + y;
339                hal_remote_swd( XPTR( cxy , &chdev_dir.mmc[local_cxy] ) , chdev_xp );
[1]340            }
[14]341        }
[1]342
[50]343        kinit_dmsg("\n[INFO] %s : core[%x][0] created MMC chdev at cycle %d\n",
[101]344                   __FUNCTION__ , local_cxy , hal_get_cycles() );
[14]345    }
[5]346
[14]347    /////////// DMA internal chdevs //////////
[1]348
[14]349    dev = &info->dev_dma;
[1]350
[14]351    if( dev->channels != 0 )   // DMA device is defined
352    {
353        assert( (FUNC_FROM_TYPE( dev->type ) == DEV_FUNC_DMA ) , __FUNCTION__ ,
354                " inconsistent DMA  device type");
[5]355
[14]356        // create one chdev per channel in local cluster
357        uint32_t channel;
358        for( channel = 0 ; channel < dev->channels ; channel++ )
[127]359        {
[14]360            chdev_ptr = chdev_create( FUNC_FROM_TYPE( dev->type ),
361                                      IMPL_FROM_TYPE( dev->type ),
362                                      channel,                        // channel
363                                      false,                          // TX
364                                      dev->base );
[5]365
[14]366            assert( (chdev_ptr != NULL) , __FUNCTION__ , "cannot allocate DMA chdev" );
[127]367
368            // get extended pointer on channel descriptor
[14]369            chdev_xp = XPTR( local_cxy , chdev_ptr );
[5]370
[14]371            // make DMA specific initialisation
[127]372            dev_dma_init( chdev_ptr );
[14]373
374            // initialize only the DMA[channel] field in the local chdev_dir[x][y]
375            // structure because the DMA device is not remotely accessible.
376            chdev_dir.dma[channel] = chdev_xp;
377
[50]378            kinit_dmsg("\n[INFO] %s : core[%x][0] created DMA[%d] chdev at cycle %d\n",
[101]379                       __FUNCTION__ , local_cxy , channel , hal_get_cycles() );
[14]380        }
[127]381    }
[5]382}  // end internal_devices_init()
383
384///////////////////////////////////////////////////////////////////////////////////////////
[127]385// This static function allocates memory for the chdev descriptors associated
[5]386// to the external (shared) peripherals contained in the local cluster. These external
[127]387// devices (IOB, IOC, TXT, NIC, etc ) are distributed on all clusters.
[5]388// It initialises these device descriptors as specified by the boot_info_t structure,
389// including the dynamic linking with the driver for the specified implementation.
390// Finally, all copies of the devices directory are initialised.
391//
[127]392// The number of channel_devices depends on the device functional type.
393// There are three nested loops to build the full set of external channel_devices:
[5]394// - loop on external devices.
395// - loop on channels for multi-channels devices.
396// - loop on directions (RX/TX) for NIC device.
397// The set of channel_devices is indexed by the chdev_gid global index, that is used
398// to select the cluster containing a given chdev[func,channel,direction].
399// All clusters scan the full set of chdevs, but only the cluster matching
[127]400// (chdev_gid % (x_size*y_size)) create the corresponding chdev.
[5]401//
402// TODO check that cluster IO contains a PIC [AG]
[50]403// TODO make a default initialisation for the chdev_dir structure (XPTR_NULL )  [AG]
[5]404///////////////////////////////////////////////////////////////////////////////////////////
405// @ info    : pointer on the local boot-info structure.
406///////////////////////////////////////////////////////////////////////////////////////////
407static void external_devices_init( boot_info_t * info )
408{
409    boot_device_t * dev_tbl;         // pointer on array of devices in boot_info
[127]410    uint32_t        dev_nr;          // actual number of devices in this cluster
411    xptr_t          base;            // remote pointer on segment base
[5]412    uint32_t        type;            // peripheral type
413    uint32_t        func;            // device functionnal index
414    uint32_t        impl;            // device implementation index
[127]415    uint32_t        i;               // device index in dev_tbl
416    uint32_t        x;               // X cluster coordinate
417    uint32_t        y;               // Y cluster coordinate
418    uint32_t        channels_nr;     // number of channels
419    uint32_t        channel;         // channel index
420    uint32_t        directions_nr;   // number of directions
421    uint32_t        direction;       // direction index
422    uint32_t        p0;              // device parameter 0
423    uint32_t        p1;              // device parameter 1
424    uint32_t        p2;              // device parameter 2
425    uint32_t        p3;              // device parameter 3
[5]426    uint32_t        first_channel;   // used in loop on channels
427
[127]428    chdev_t       * chdev;           // local pointer on one channel_device descriptor
[5]429    xptr_t          chdev_xp;        // extended pointer on channel_device descriptor
430    uint32_t        chdev_gid = 0;   // global index of channel_device descriptor
431
432    // get number of peripherals and base of devices array from boot_info
[127]433    dev_nr      = info->ext_dev_nr;
[5]434    dev_tbl     = info->ext_dev;
435
436    // loop on external peripherals
[127]437    for( i = 0 ; i < dev_nr ; i++ )
438    {
[5]439        base        = dev_tbl[i].base;
440        type        = dev_tbl[i].type;
441        channels_nr = dev_tbl[i].channels;
442        p0          = dev_tbl[i].param0;
443        p1          = dev_tbl[i].param1;
444        p2          = dev_tbl[i].param2;
445        p3          = dev_tbl[i].param3;
446
447        func     = FUNC_FROM_TYPE( type );
448        impl     = IMPL_FROM_TYPE( type );
449
450        // There is one chdev per direction for NIC
451        if (func == DEV_FUNC_NIC) directions_nr = 2;
452        else                      directions_nr = 1;
453
454        // The TXT0 chdev has already been created
455        if (func == DEV_FUNC_TXT) first_channel = 1;
456        else                      first_channel = 0;
457
458        // do nothing for ROM, that does not require a device descriptor.
459        if( func == DEV_FUNC_ROM ) continue;
460
[127]461        // check external device functional type
[5]462        if( (func != DEV_FUNC_IOB) &&
463            (func != DEV_FUNC_PIC) &&
464            (func != DEV_FUNC_IOC) &&
465            (func != DEV_FUNC_TXT) &&
466            (func != DEV_FUNC_NIC) &&
[127]467            (func != DEV_FUNC_FBF) )
[1]468        {
[5]469            assert( false , __FUNCTION__ , "undefined external peripheral type" );
470        }
471
[127]472        // loops on channels
[5]473        for( channel = first_channel ; channel < channels_nr ; channel++ )
[127]474        {
[5]475            // loop on directions
476            for( direction = 0 ; direction < directions_nr ; direction++ )
[1]477            {
[5]478                // get target cluster for chdev[func,channel,direction]
479                uint32_t offset     = chdev_gid % ( info->x_size * info->y_size );
480                uint32_t cx         = offset / info->y_size;
481                uint32_t cy         = offset % info->y_size;
482                uint32_t target_cxy = (cx<<info->y_width) + cy;
[1]483
[5]484                // allocate and initialize a local chdev
485                // if local cluster matches target cluster
486                if( target_cxy == local_cxy )
[1]487                {
[5]488                    chdev = chdev_create( func,
489                                          impl,
490                                          channel,
491                                          direction,
492                                          base );
493
[127]494                    assert( (chdev != NULL), __FUNCTION__ ,
[5]495                            "cannot allocate external device" );
496
497                    // get extended pointer on chdev
498                    chdev_xp = XPTR( local_cxy , chdev );
499
500                    // make device type specific initialisation
501                    // the number of parameters depends on the device type
502                    // TODO : remove the parameters that  must be provided by the drivers
503                    if     ( func == DEV_FUNC_IOB ) dev_iob_init( chdev );
504                    else if( func == DEV_FUNC_IOC ) dev_ioc_init( chdev );
505                    else if( func == DEV_FUNC_TXT ) dev_txt_init( chdev );
506                    else if( func == DEV_FUNC_NIC ) dev_nic_init( chdev );
507                    else if( func == DEV_FUNC_PIC ) dev_pic_init( chdev , p0 );
508                    else if( func == DEV_FUNC_FBF ) dev_fbf_init( chdev , p0 , p1 );
509                    else
510                    {
511                        assert( false , __FUNCTION__ , "undefined device type" );
512                    }
513
[127]514                    // all external (shared) devices are remotely accessible
[5]515                    // initialize the replicated chdev_dir[x][y] structures
[127]516                    // defining the extended pointers on chdev descriptors
517                    xptr_t * entry;
518
[5]519                    if( func == DEV_FUNC_IOB ) entry  = &chdev_dir.iob;
520                    if( func == DEV_FUNC_PIC ) entry  = &chdev_dir.pic;
521                    if( func == DEV_FUNC_TXT ) entry  = &chdev_dir.txt[channel];
522                    if( func == DEV_FUNC_IOC ) entry  = &chdev_dir.ioc[channel];
523                    if( func == DEV_FUNC_FBF ) entry  = &chdev_dir.fbf[channel];
524                    if( func == DEV_FUNC_NIC ) entry  = &chdev_dir.nic_tx[channel];
[127]525
[1]526                    for( x = 0 ; x < info->x_size ; x++ )
527                    {
528                        for( y = 0 ; y < info->y_size ; y++ )
529                        {
530                            cxy_t  cxy = (x<<info->y_width) + y;
[5]531                            hal_remote_swd( XPTR( cxy , entry ) , chdev_xp );
532                        }
[1]533                    }
534
[127]535                    kinit_dmsg("\n[INFO] %s : core[%x][0] create chdev %s[%d] at cycle %d\n",
[5]536                               __FUNCTION__ , local_cxy , chdev_func_str( func ),
[101]537                               channel , hal_get_cycles() );
[5]538
539                }  // end if match
540
[19]541                // increment chdev global index (matching or not)
[5]542                chdev_gid++;
543
544            } // end loop on directions
545
546        }  // end loop on channels
547
548        // initialize the entries of the local chdev_pic_input structure
[127]549        // defining how external peripherals are connected to PIC
550        if( func == DEV_FUNC_PIC )
[1]551        {
[5]552            uint32_t   id;
553            uint8_t    valid;
554            uint32_t   dev_type;
555            uint8_t    channel;
556            uint8_t    is_rx;
557
558            // loop on PIC inputs
559            for( id = 0 ; id < CONFIG_MAX_IRQS_PER_PIC ; id++ )
[1]560            {
[5]561                valid     = dev_tbl[i].irq[id].valid;
562                dev_type  = dev_tbl[i].irq[id].dev_type;
563                channel   = dev_tbl[i].irq[id].channel;
564                is_rx     = dev_tbl[i].irq[id].is_rx;
[1]565
[5]566                if( valid )  // only valid inputs are registered
[1]567                {
[5]568                    uint32_t * index;  // local pointer on one entry
[1]569                    uint16_t dev_func = FUNC_FROM_TYPE( dev_type );
[5]570
571                    if( dev_func == DEV_FUNC_TXT )
[1]572                    {
[5]573                        index = &chdev_pic_input.txt[channel];
[1]574                    }
[5]575                    else if( dev_func == DEV_FUNC_IOC )
576                    {
[127]577                        index = &chdev_pic_input.ioc[channel];
[5]578                    }
579                    else if( (dev_func == DEV_FUNC_NIC) && (is_rx == 0) )
580                    {
[127]581                        index = &chdev_pic_input.nic_tx[channel];
[5]582                    }
583                    else if( (dev_func == DEV_FUNC_NIC) && (is_rx != 0) )
584                    {
[127]585                        index = &chdev_pic_input.nic_rx[channel];
[5]586                    }
587                    else
588                    {
589                        assert( false , __FUNCTION__ , "illegal source device for PIC input" );
[127]590                    }
[5]591
592                    // set entry in local structure
[127]593                    *index = id;
[1]594                }
[5]595            } // end loop on PIC inputs
596        } // end PIC
[127]597    } // end loop on devices
[5]598}  // end external_devices_init()
[1]599
600
601///////////////////////////////////////////////////////////////////////////////////////////
[14]602// This static function returns the identifiers of the calling core.
603///////////////////////////////////////////////////////////////////////////////////////////
604// @ info    : pointer on boot_info structure.
605// @ lid     : [out] core local index in cluster.
606// @ cxy     : [out] cluster identifier.
607// @ lid     : [out] core global identifier (hardware).
608// @ return 0 if success / return EINVAL if not found.
609///////////////////////////////////////////////////////////////////////////////////////////
[23]610static error_t get_core_identifiers( boot_info_t * info,
611                                     lid_t       * lid,
[14]612                                     cxy_t       * cxy,
613                                     gid_t       * gid )
614{
[127]615    uint32_t   i;
[14]616    gid_t      global_id;
[19]617
[14]618    // get global identifier from hardware register
[127]619    global_id = hal_get_gid();
[14]620
621    // makes an associative search in boot_info to get (cxy,lid) from global_id
622    for( i = 0 ; i < info->cores_nr ; i++ )
623    {
624        if( global_id == info->core[i].gid )
625        {
626            *lid = info->core[i].lid;
627            *cxy = info->core[i].cxy;
628            *gid = global_id;
629            return 0;
630        }
631    }
632    return EINVAL;
[19]633}
[14]634
635///////////////////////////////////////////////////////////////////////////////////////////
[1]636// This function is the entry point for the kernel initialisation.
[19]637// It is executed by all cores in all clusters, but only core[0], called CP0,
[14]638// initializes the shared resources such as the cluster manager, or the local peripherals.
[19]639// To comply with the multi-kernels paradigm, it accesses only local cluster memory, using
640// only information contained in the local boot_info_t structure, set by the bootloader.
[103]641// Only CP0 in cluster 0 print the log messages.
[1]642///////////////////////////////////////////////////////////////////////////////////////////
643// @ info    : pointer on the local boot-info structure.
644///////////////////////////////////////////////////////////////////////////////////////////
645void kernel_init( boot_info_t * info )
646{
[23]647    lid_t        core_lid = -1;      // running core local index
[14]648    cxy_t        core_cxy = -1;      // running core cluster identifier
[127]649    gid_t        core_gid;           // running core hardware identifier
[14]650    cluster_t  * cluster;            // pointer on local cluster manager
651    core_t     * core;               // pointer on running core descriptor
652    thread_t   * thread;             // pointer on idle thread descriptor
[1]653    error_t      error;
654
[103]655    // all cores get and check core identifiers
[23]656    error = get_core_identifiers( info,
[14]657                                  &core_lid,
658                                  &core_cxy,
659                                  &core_gid );
[1]660
[127]661    // CP0 initializes cluster identifier
[14]662    if( core_lid == 0 ) local_cxy = info->cxy;
[1]663
[127]664    // each core gets a pointer on its private idle thread descriptor
665    thread = (thread_t *)( idle_threads + (core_lid * CONFIG_THREAD_DESC_SIZE) );
[68]666
[127]667    // each core registers this thread pointer in hardware register
[68]668    hal_set_current_thread( thread );
[71]669
[124]670    list_root_init( &thread->locks_root );
671    xlist_root_init( XPTR( local_cxy, &thread->xlocks_root ) );
672
[127]673    // CP0 in I/O cluster initializes TXT0 chdev descriptor
[14]674    if( (core_lid == 0) && (core_cxy == info->io_cxy) ) txt0_device_init( info );
675
676    /////////////////////////////////////////////////////////////////////////////////
677    // global & local synchro to protect access to TXT0 terminal
[127]678    if( core_lid == 0 ) remote_barrier( XPTR( info->io_cxy , &global_barrier ),
[14]679                                        (info->x_size * info->y_size) );
680    barrier_wait( &local_barrier , info->cores_nr );
681    /////////////////////////////////////////////////////////////////////////////////
682
[127]683    if( (core_lid ==  0) && (local_cxy == info->io_cxy) )
[103]684    {
685        kinit_dmsg("\n[INFO] %s : core[%x][%d] exit barrier 0 at cycle %d\n",
686                   __FUNCTION__ , core_cxy , core_lid , hal_get_cycles() );
687    }
[14]688
689    // all cores check core identifiers
690    if( error )
[1]691    {
[103]692        nolock_printk("\n[PANIC] in %s : illegal core identifiers"
[14]693               " gid = %x / cxy = %x / lid = %d\n",
694               __FUNCTION__ , core_lid , core_cxy , core_lid );
695        hal_core_sleep();
[1]696    }
697
[19]698    // CP0 initializes the local cluster manager (cores and memory allocators)
[14]699    if( core_lid == 0 )
[1]700    {
701        error = cluster_init( info );
702
[14]703        if( error )
704        {
[103]705            nolock_printk("\n[PANIC] in %s : cannot initialise cluster manager in cluster %x",
[14]706                   __FUNCTION__ , local_cxy );
707            hal_core_sleep();
708        }
709    }
[5]710
[14]711    /////////////////////////////////////////////////////////////////////////////////
712    // global & local synchro, to protect access to cluster manager
[127]713    if( core_lid == 0 ) remote_barrier( XPTR( info->io_cxy , &global_barrier ),
[14]714                                        (info->x_size * info->y_size) );
715    barrier_wait( &local_barrier , info->cores_nr );
716    /////////////////////////////////////////////////////////////////////////////////
[1]717
[127]718    if( (core_lid ==  0) && (local_cxy == info->io_cxy) )
[103]719    {
720        kinit_dmsg("\n[INFO] %s : core[%x][%d] exit barrier 1 at cycle %d\n",
721                   __FUNCTION__ , core_cxy , core_lid , hal_get_cycles() );
722    }
[1]723
[14]724    // all cores get pointer on local cluster manager and on core descriptor
725    cluster = &cluster_manager;
[127]726    core    = &cluster->core_tbl[core_lid];
[1]727
[19]728    // CP0 initializes the process_zero descriptor
[101]729    if( core_lid == 0 ) process_reference_init( &process_zero , 0 , XPTR_NULL );
[5]730
[124]731#ifdef __HAL_x86_64__
732        return; /* XXX temporary */
733#endif
734
[127]735    // CP0 allocates and initializes the internal peripheral chdev descriptors.
[14]736    // Each CP0[cxy] scan the set of its internal (private) peripherals,
[127]737    // and allocates memory for the corresponding chdev descriptors.
[14]738    if( core_lid == 0 ) internal_devices_init( info );
[127]739
740    // CP0 allocates one WTI mailbox per core for Inter Processor Interrupt
[14]741    // this must be done after ICU chdev initialisation, by CP0 only, and before
[50]742    // external devices initialisation to enforce the rule :
743    // "The WTI index for the IPI routed to core[lid] is lid"
[101]744    if( core_lid == 1 )
[1]745    {
[14]746        uint32_t  wti_id;
747        uint32_t  lid;
748        for( lid = 0 ; lid < LOCAL_CLUSTER->cores_nr ; lid++ )
[1]749        {
[14]750            wti_id = dev_icu_wti_alloc();
[1]751
[14]752            if( wti_id != lid )
753            {
[103]754                nolock_printk("\n[PANIC] in %s : WTI index for IPI = %d / core_lid = %d",
755                              __FUNCTION__ , wti_id , lid );
[14]756                hal_core_sleep();
757            }
[5]758
[14]759            dev_icu_enable_irq( lid , WTI_TYPE , wti_id , NULL );
760        }
[1]761    }
762
[50]763    // All CP0s contribute to initialise external peripheral chdev descriptors.
[14]764    // Each CP0[cxy] scan the set of external (shared) peripherals (but the TXT0),
765    // and allocates memory for the chdev descriptors that must be placed
[127]766    // on the (cxy) cluster according to the global index value.
[14]767    if( core_lid == 0 ) external_devices_init( info );
[1]768
[14]769    /////////////////////////////////////////////////////////////////////////////////
770    // global &local synchro to protect access to peripherals
[127]771    if( core_lid == 0 ) remote_barrier( XPTR( info->io_cxy , &global_barrier ),
[14]772                                        (info->x_size * info->y_size) );
773    barrier_wait( &local_barrier , info->cores_nr );
774    /////////////////////////////////////////////////////////////////////////////////
[5]775
[127]776    if( (core_lid ==  0) && (local_cxy == info->io_cxy) )
[103]777    {
[127]778        kinit_dmsg("\n[INFO] %s : core[%x][%d] exit barrier 2 at cycle %d\n",
[103]779                   __FUNCTION__ , core_cxy , core_lid , hal_get_cycles() );
780    }
[1]781
[14]782    error = thread_kernel_init( thread,
[127]783                                THREAD_IDLE,
784                                &thread_idle_func,
[14]785                                NULL,
786                                core_lid );
787    if( error )
[1]788    {
[103]789        nolock_printk("\n[PANIC] in %s : core[%x][%d] cannot initialize idle thread\n",
790                      __FUNCTION__ , local_cxy , core_lid );
[14]791        hal_core_sleep();
[1]792    }
793
[103]794    // register idle thread in scheduler
795    core->scheduler.idle = thread;
[1]796
[103]797    // activate the idle thread
798    thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_GLOBAL );
799
[127]800    if( (core_lid ==  0) && (local_cxy == info->io_cxy) )
[103]801    {
[14]802        kinit_dmsg("\n[INFO] %s : core[%x][%d] created idle thread %x at cycle %d\n",
[101]803                   __FUNCTION__ , core_cxy , core_lid , thread , hal_get_cycles());
[14]804    }
805
[127]806    // CP0 in all clusters initializes cooperatively VFS and DEVFS
[23]807    if( (core_lid == 0)  )
[14]808    {
[23]809        xptr_t  root_inode_xp;
810
[50]811        // initialize root File System (must be FATFS in this implementation)
[23]812        if( CONFIG_VFS_ROOT_IS_FATFS )
813        {
814            root_inode_xp = fatfs_init();
815        }
816        else
817        {
[103]818            nolock_printk("\n[PANIC] in %s : root FS must be FATFS\n", __FUNCTION__ );
[23]819            hal_core_sleep();
820        }
821
822        if( root_inode_xp == XPTR_NULL )
823        {
[103]824            nolock_printk("\n[PANIC] in %s : core[%x][%d] cannot initialize file system\n",
[23]825                   __FUNCTION__ , local_cxy , core_lid );
826            hal_core_sleep();
827        }
828
[101]829        // register VFS root inode in process_zero
830        process_zero.vfs_root_xp = root_inode_xp;
831        process_zero.vfs_cwd_xp  = root_inode_xp;
832
[23]833        // mount the DEVFS File system
[127]834        devfs_mount( root_inode_xp , "dev" );
[14]835    }
836
[101]837
838    // CP0 in I/O cluster creates the process_init and print banner
[127]839    if( (core_lid ==  0) && (local_cxy == info->io_cxy) )
[1]840    {
[101]841        process_init_create();
842
[5]843        print_banner( (info->x_size * info->y_size) , info->cores_nr );
[68]844
845        kinit_dmsg("\n\n*** memory fooprint of main kernet objects ***\n"
846                   " - thread descriptor  : %d bytes\n"
847                   " - process descriptor : %d bytes\n"
848                   " - cluster manager    : %d bytes\n"
849                   " - chdev descriptor   : %d bytes\n"
850                   " - core descriptor    : %d bytes\n"
851                   " - scheduler          : %d bytes\n"
852                   " - rpc fifo           : %d bytes\n"
853                   " - page descriptor    : %d bytes\n"
854                   " - mapper root        : %d bytes\n"
855                   " - ppm manager        : %d bytes\n"
856                   " - kcm manager        : %d bytes\n"
857                   " - khm manager        : %d bytes\n"
858                   " - vmm manager        : %d bytes\n"
859                   " - gpt root           : %d bytes\n"
860                   " - list item          : %d bytes\n"
861                   " - xlist item         : %d bytes\n"
862                   " - spinlock           : %d bytes\n"
863                   " - remote spinlock    : %d bytes\n"
864                   " - rwlock             : %d bytes\n"
865                   " - remote rwlock      : %d bytes\n",
[127]866                   sizeof( thread_t          ),
[68]867                   sizeof( process_t         ),
868                   sizeof( cluster_t         ),
869                   sizeof( chdev_t           ),
870                   sizeof( core_t            ),
871                   sizeof( scheduler_t       ),
872                   sizeof( rpc_fifo_t        ),
873                   sizeof( page_t            ),
874                   sizeof( mapper_t          ),
875                   sizeof( ppm_t             ),
876                   sizeof( kcm_t             ),
877                   sizeof( khm_t             ),
878                   sizeof( vmm_t             ),
879                   sizeof( gpt_t             ),
880                   sizeof( list_entry_t      ),
881                   sizeof( xlist_entry_t     ),
882                   sizeof( spinlock_t        ),
883                   sizeof( remote_spinlock_t ),
884                   sizeof( rwlock_t          ),
885                   sizeof( remote_rwlock_t   ));
[1]886    }
887
[14]888    /////////////////////////////////////////////////////////////////////////////////
889    // global syncho to protect access to File System
890    if( core_lid == 0 ) remote_barrier( XPTR( info->io_cxy , &global_barrier ),
891                                        (info->x_size * info->y_size) );
892    barrier_wait( &local_barrier , info->cores_nr );
893    /////////////////////////////////////////////////////////////////////////////////
[1]894
[127]895    if( (core_lid ==  0) && (local_cxy == info->io_cxy) )
[103]896    {
[127]897        kinit_dmsg("\n[INFO] %s : core[%x][%d] exit barrier 3 at cycle %d\n",
[103]898                   __FUNCTION__ , core_cxy , core_lid , hal_get_cycles() );
899    }
[14]900
[127]901    // each core activates its private PTI IRQ
[14]902    dev_icu_set_period( core_lid , CONFIG_SCHED_TICK_PERIOD );
903    dev_icu_enable_irq( core_lid , PTI_TYPE , core_lid , NULL );
904
[101]905    // each core get its private IRQ masks values
[14]906    uint32_t hwi_mask;
907    uint32_t wti_mask;
908    uint32_t pti_mask;
909    dev_icu_get_masks( core_lid , &hwi_mask , &wti_mask , &pti_mask );
910
[103]911    thread_dmsg("\n[INFO] %s : core[%x][%d] complete kernel init at cycle %d\n"
[14]912                "   hwi_mask = %x / wti_mask = %x / pti_mask = %x\n",
[101]913                    __FUNCTION__ , local_cxy , core_lid , hal_get_cycles() ,
[14]914                    hwi_mask , wti_mask , pti_mask );
915
916    // each core jump to idle thread
[50]917    thread_idle_func();
[127]918}
[14]919
Note: See TracBrowser for help on using the repository browser.