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

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

style

File size: 38.9 KB
Line 
1/*
2 * kernel_init.c - kernel parallel initialization
3 *
4 * Authors :  Mohamed Lamine Karaoui (2015)
5 *            Alain Greiner  (2016,2017)
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
25#include <kernel_config.h>
26#include <errno.h>
27#include <hal_types.h>
28#include <hal_special.h>
29#include <hal_context.h>
30#include <barrier.h>
31#include <remote_barrier.h>
32#include <core.h>
33#include <list.h>
34#include <xlist.h>
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>
43#include <chdev.h>
44#include <boot_info.h>
45#include <dqdt.h>
46#include <dev_icu.h>
47#include <dev_mmc.h>
48#include <dev_dma.h>
49#include <dev_iob.h>
50#include <dev_ioc.h>
51#include <dev_txt.h>
52#include <dev_pic.h>
53#include <printk.h>
54#include <vfs.h>
55#include <hal_drivers.h>
56#include <devfs.h>
57#include <mapper.h>
58#include <soclib_tty.h>
59
60#define KERNEL_INIT_SYNCHRO  0xA5A5B5B5
61
62///////////////////////////////////////////////////////////////////////////////////////////
63// All these global variables are replicated in all clusters.
64// They are initialised by the kernel_init() function.
65//
66// WARNING : The section names have been defined to control the base addresses of the
67// boot_info structure and the idle thread descriptors, through the kernel.ld script:
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.
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.
72///////////////////////////////////////////////////////////////////////////////////////////
73
74// This variable defines the local boot_info structure
75__attribute__((section(".kinfo")))
76boot_info_t          boot_info;
77
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
83// This variable defines the local cluster manager
84__attribute__((section(".kdata")))
85cluster_t            cluster_manager                         CONFIG_CACHE_LINE_ALIGNED;
86
87// This variables define the kernel process0 descriptor
88__attribute__((section(".kdata")))
89process_t            process_zero                            CONFIG_CACHE_LINE_ALIGNED;
90
91// This variable defines extended pointers on the distributed chdevs
92__attribute__((section(".kdata")))
93chdev_directory_t    chdev_dir                               CONFIG_CACHE_LINE_ALIGNED;
94
95// This variable contains the input IRQ indexes for the PIC device
96__attribute__((section(".kdata")))
97chdev_pic_input_t    chdev_pic_input                         CONFIG_CACHE_LINE_ALIGNED;
98
99// This variable contains the input IRQ indexes for the ICU device
100__attribute__((section(".kdata")))
101chdev_icu_input_t    chdev_icu_input                         CONFIG_CACHE_LINE_ALIGNED;
102
103// This variable defines the local cluster identifier
104__attribute__((section(".kdata")))
105cxy_t                local_cxy                               CONFIG_CACHE_LINE_ALIGNED;
106
107// This variable defines the TXT0 chdev descriptor
108__attribute__((section(".kdata")))
109chdev_t              txt0_chdev                              CONFIG_CACHE_LINE_ALIGNED;
110
111// This variable is used for CP0 cores synchronisation in kernel_init()
112__attribute__((section(".kdata")))
113remote_barrier_t     global_barrier                          CONFIG_CACHE_LINE_ALIGNED;
114
115// This variable is used for local cores synchronisation in kernel_init()
116__attribute__((section(".kdata")))
117barrier_t            local_barrier                           CONFIG_CACHE_LINE_ALIGNED;
118
119// This variable defines the array of supported File System contexts
120__attribute__((section(".kdata")))
121vfs_ctx_t            fs_context[FS_TYPES_NR]                 CONFIG_CACHE_LINE_ALIGNED;
122
123
124///////////////////////////////////////////////////////////////////////////////////////////
125// This function displays the ALMOS_MKH banner.
126///////////////////////////////////////////////////////////////////////////////////////////
127static void print_banner( uint32_t nclusters , uint32_t ncores )
128{
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"
139           "\n\n\t\t\t Version 0.0   :   %d clusters   /   %d cores per cluster\n\n", nclusters , ncores );
140}
141
142
143///////////////////////////////////////////////////////////////////////////////////////////
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
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.
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.
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
158    uint32_t        dev_nr;          // actual number of devices in this cluster
159    xptr_t          base;            // remote pointer on segment base
160    uint32_t        type;            // peripheral type
161    uint32_t        func;            // device functional index
162    uint32_t        impl;            // device implementation index
163    uint32_t        i;               // device index in dev_tbl
164    uint32_t        x;               // X cluster coordinate
165    uint32_t        y;               // Y cluster coordinate
166
167    // get number of peripherals and base of devices array from boot_info
168    dev_nr      = info->ext_dev_nr;
169    dev_tbl     = info->ext_dev;
170
171    // loop on external peripherals to find TXT device
172    for( i = 0 ; i < dev_nr ; i++ )
173    {
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
179        if (func == DEV_FUNC_TXT )
180        {
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;
187
188            // initialize lock
189            remote_spinlock_init( XPTR( local_cxy , &txt0_chdev.wait_lock ) );
190
191            // TODO use generic device initialisation
192            // hal_drivers_txt_init( &txt0_chdev );
193
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
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;
207                    hal_remote_swd( XPTR( cxy , &chdev_dir.txt[0] ) ,
208                                    XPTR( local_cxy , &txt0_chdev ) );
209                }
210            }
211
212            kinit_dmsg("\n[INFO] %s : core[%x][0] created TXT0 chdev"
213                       " / paddr = %l at cycle %d\n",
214                       __FUNCTION__ , local_cxy , chdev_func_str( func ),
215                       XPTR(local_cxy , &txt0_chdev) , hal_get_cycles() );
216        }
217    }
218}
219
220///////////////////////////////////////////////////////////////////////////////////////////
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.
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///////////////////////////////////////////////////////////////////////////////////////////
230static void internal_devices_init( boot_info_t * info )
231{
232    boot_device_t * dev;             // pointer on boot_info device (ICU/MMC/DMA)
233    uint32_t        x;               // X cluster coordinate
234    uint32_t        y;               // Y cluster coordinate
235    chdev_t       * chdev_ptr;       // local pointer on chdev descriptor
236    xptr_t          chdev_xp;        // extended pointer on chdev descriptor
237
238    ///////////  ICU   //////////
239
240    dev = &info->dev_icu;
241
242    assert( ((info->cores_nr == 0) || (dev->channels != 0)) , __FUNCTION__ ,
243            "ICU device must exist in cluster containing cores" );
244
245    assert( (dev->channels == 1) , __FUNCTION__ ,
246            "channels number must be 1 for ICU device" );
247
248    assert( (FUNC_FROM_TYPE( dev->type ) == DEV_FUNC_ICU ) , __FUNCTION__ ,
249            " inconsistent ICU  device type");
250
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 );
257
258    assert( (chdev_ptr != NULL) , __FUNCTION__ , "cannot allocate ICU chdev" );
259
260    // get extended pointer on chdev descriptor
261    chdev_xp = XPTR( local_cxy , chdev_ptr );
262
263    // make ICU specific initialisation
264    // TODO remove these three parameters
265    dev_icu_init( chdev_ptr , dev->param0 , dev->param1 , dev->param2 );
266
267    // initialize the ICU field in the chdev_dir[x][y] structures
268    // replicated in all clusters, and containing extended pointers
269    // on all remotely accessible devices
270    for( x = 0 ; x < info->x_size ; x++ )
271    {
272        for( y = 0 ; y < info->y_size ; y++ )
273        {
274            cxy_t  cxy = (x<<info->y_width) + y;
275            hal_remote_swd( XPTR( cxy , &chdev_dir.icu[local_cxy] ) , chdev_xp );
276        }
277    }
278
279    // initialize the entries of the local chdev_icu_input structure
280    // defining how internal peripherals are connected to ICU
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
294        {
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    }
300
301    kinit_dmsg("\n[INFO] %s : core[%x][0] created ICU chdev at cycle %d\n",
302               __FUNCTION__ , local_cxy , hal_get_cycles() );
303
304    /////////// MMC internal chdev ///////////
305
306    dev = &info->dev_mmc;
307
308    if( dev->channels != 0 )   // MMC device is defined
309    {
310        assert( (dev->channels == 1) , __FUNCTION__ ,
311            "channels number must be 1 for MMC device" );
312
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" );
324
325        // get extended pointer on chdev descriptor
326        chdev_xp = XPTR( local_cxy , chdev_ptr );
327
328        // make MMC specific initialisation
329        dev_mmc_init( chdev_ptr );
330
331        // initialize the MMC field in the chdev_dir[x][y] structures
332        // replicated in all clusters, and containing extended pointers
333        // on all remotely accessible devices
334        for( x = 0 ; x < info->x_size ; x++ )
335        {
336            for( y = 0 ; y < info->y_size ; y++ )
337            {
338                cxy_t  cxy = (x<<info->y_width) + y;
339                hal_remote_swd( XPTR( cxy , &chdev_dir.mmc[local_cxy] ) , chdev_xp );
340            }
341        }
342
343        kinit_dmsg("\n[INFO] %s : core[%x][0] created MMC chdev at cycle %d\n",
344                   __FUNCTION__ , local_cxy , hal_get_cycles() );
345    }
346
347    /////////// DMA internal chdevs //////////
348
349    dev = &info->dev_dma;
350
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");
355
356        // create one chdev per channel in local cluster
357        uint32_t channel;
358        for( channel = 0 ; channel < dev->channels ; channel++ )
359        {
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 );
365
366            assert( (chdev_ptr != NULL) , __FUNCTION__ , "cannot allocate DMA chdev" );
367
368            // get extended pointer on channel descriptor
369            chdev_xp = XPTR( local_cxy , chdev_ptr );
370
371            // make DMA specific initialisation
372            dev_dma_init( chdev_ptr );
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
378            kinit_dmsg("\n[INFO] %s : core[%x][0] created DMA[%d] chdev at cycle %d\n",
379                       __FUNCTION__ , local_cxy , channel , hal_get_cycles() );
380        }
381    }
382}  // end internal_devices_init()
383
384///////////////////////////////////////////////////////////////////////////////////////////
385// This static function allocates memory for the chdev descriptors associated
386// to the external (shared) peripherals contained in the local cluster. These external
387// devices (IOB, IOC, TXT, NIC, etc ) are distributed on all clusters.
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//
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:
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
400// (chdev_gid % (x_size*y_size)) create the corresponding chdev.
401//
402// TODO check that cluster IO contains a PIC [AG]
403// TODO make a default initialisation for the chdev_dir structure (XPTR_NULL )  [AG]
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
410    uint32_t        dev_nr;          // actual number of devices in this cluster
411    xptr_t          base;            // remote pointer on segment base
412    uint32_t        type;            // peripheral type
413    uint32_t        func;            // device functionnal index
414    uint32_t        impl;            // device implementation index
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
426    uint32_t        first_channel;   // used in loop on channels
427
428    chdev_t       * chdev;           // local pointer on one channel_device descriptor
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
433    dev_nr      = info->ext_dev_nr;
434    dev_tbl     = info->ext_dev;
435
436    // loop on external peripherals
437    for( i = 0 ; i < dev_nr ; i++ )
438    {
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
461        // check external device functional type
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) &&
467            (func != DEV_FUNC_FBF) )
468        {
469            assert( false , __FUNCTION__ , "undefined external peripheral type" );
470        }
471
472        // loops on channels
473        for( channel = first_channel ; channel < channels_nr ; channel++ )
474        {
475            // loop on directions
476            for( direction = 0 ; direction < directions_nr ; direction++ )
477            {
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;
483
484                // allocate and initialize a local chdev
485                // if local cluster matches target cluster
486                if( target_cxy == local_cxy )
487                {
488                    chdev = chdev_create( func,
489                                          impl,
490                                          channel,
491                                          direction,
492                                          base );
493
494                    assert( (chdev != NULL), __FUNCTION__ ,
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
514                    // all external (shared) devices are remotely accessible
515                    // initialize the replicated chdev_dir[x][y] structures
516                    // defining the extended pointers on chdev descriptors
517                    xptr_t * entry;
518
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];
525
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;
531                            hal_remote_swd( XPTR( cxy , entry ) , chdev_xp );
532                        }
533                    }
534
535                    kinit_dmsg("\n[INFO] %s : core[%x][0] create chdev %s[%d] at cycle %d\n",
536                               __FUNCTION__ , local_cxy , chdev_func_str( func ),
537                               channel , hal_get_cycles() );
538
539                }  // end if match
540
541                // increment chdev global index (matching or not)
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
549        // defining how external peripherals are connected to PIC
550        if( func == DEV_FUNC_PIC )
551        {
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++ )
560            {
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;
565
566                if( valid )  // only valid inputs are registered
567                {
568                    uint32_t * index;  // local pointer on one entry
569                    uint16_t dev_func = FUNC_FROM_TYPE( dev_type );
570
571                    if( dev_func == DEV_FUNC_TXT )
572                    {
573                        index = &chdev_pic_input.txt[channel];
574                    }
575                    else if( dev_func == DEV_FUNC_IOC )
576                    {
577                        index = &chdev_pic_input.ioc[channel];
578                    }
579                    else if( (dev_func == DEV_FUNC_NIC) && (is_rx == 0) )
580                    {
581                        index = &chdev_pic_input.nic_tx[channel];
582                    }
583                    else if( (dev_func == DEV_FUNC_NIC) && (is_rx != 0) )
584                    {
585                        index = &chdev_pic_input.nic_rx[channel];
586                    }
587                    else
588                    {
589                        assert( false , __FUNCTION__ , "illegal source device for PIC input" );
590                    }
591
592                    // set entry in local structure
593                    *index = id;
594                }
595            } // end loop on PIC inputs
596        } // end PIC
597    } // end loop on devices
598}  // end external_devices_init()
599
600
601///////////////////////////////////////////////////////////////////////////////////////////
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///////////////////////////////////////////////////////////////////////////////////////////
610static error_t get_core_identifiers( boot_info_t * info,
611                                     lid_t       * lid,
612                                     cxy_t       * cxy,
613                                     gid_t       * gid )
614{
615    uint32_t   i;
616    gid_t      global_id;
617
618    // get global identifier from hardware register
619    global_id = hal_get_gid();
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;
633}
634
635///////////////////////////////////////////////////////////////////////////////////////////
636// This function is the entry point for the kernel initialisation.
637// It is executed by all cores in all clusters, but only core[0], called CP0,
638// initializes the shared resources such as the cluster manager, or the local peripherals.
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.
641// Only CP0 in cluster 0 print the log messages.
642///////////////////////////////////////////////////////////////////////////////////////////
643// @ info    : pointer on the local boot-info structure.
644///////////////////////////////////////////////////////////////////////////////////////////
645void kernel_init( boot_info_t * info )
646{
647    lid_t        core_lid = -1;      // running core local index
648    cxy_t        core_cxy = -1;      // running core cluster identifier
649    gid_t        core_gid;           // running core hardware identifier
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
653    error_t      error;
654
655    // all cores get and check core identifiers
656    error = get_core_identifiers( info,
657                                  &core_lid,
658                                  &core_cxy,
659                                  &core_gid );
660
661    // CP0 initializes cluster identifier
662    if( core_lid == 0 ) local_cxy = info->cxy;
663
664    // each core gets a pointer on its private idle thread descriptor
665    thread = (thread_t *)( idle_threads + (core_lid * CONFIG_THREAD_DESC_SIZE) );
666
667    // each core registers this thread pointer in hardware register
668    hal_set_current_thread( thread );
669
670    list_root_init( &thread->locks_root );
671    xlist_root_init( XPTR( local_cxy, &thread->xlocks_root ) );
672
673    // CP0 in I/O cluster initializes TXT0 chdev descriptor
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
678    if( core_lid == 0 ) remote_barrier( XPTR( info->io_cxy , &global_barrier ),
679                                        (info->x_size * info->y_size) );
680    barrier_wait( &local_barrier , info->cores_nr );
681    /////////////////////////////////////////////////////////////////////////////////
682
683    if( (core_lid ==  0) && (local_cxy == info->io_cxy) )
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    }
688
689    // all cores check core identifiers
690    if( error )
691    {
692        nolock_printk("\n[PANIC] in %s : illegal core identifiers"
693               " gid = %x / cxy = %x / lid = %d\n",
694               __FUNCTION__ , core_lid , core_cxy , core_lid );
695        hal_core_sleep();
696    }
697
698    // CP0 initializes the local cluster manager (cores and memory allocators)
699    if( core_lid == 0 )
700    {
701        error = cluster_init( info );
702
703        if( error )
704        {
705            nolock_printk("\n[PANIC] in %s : cannot initialise cluster manager in cluster %x",
706                   __FUNCTION__ , local_cxy );
707            hal_core_sleep();
708        }
709    }
710
711    /////////////////////////////////////////////////////////////////////////////////
712    // global & local synchro, to protect access to cluster manager
713    if( core_lid == 0 ) remote_barrier( XPTR( info->io_cxy , &global_barrier ),
714                                        (info->x_size * info->y_size) );
715    barrier_wait( &local_barrier , info->cores_nr );
716    /////////////////////////////////////////////////////////////////////////////////
717
718    if( (core_lid ==  0) && (local_cxy == info->io_cxy) )
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    }
723
724    // all cores get pointer on local cluster manager and on core descriptor
725    cluster = &cluster_manager;
726    core    = &cluster->core_tbl[core_lid];
727
728    // CP0 initializes the process_zero descriptor
729    if( core_lid == 0 ) process_reference_init( &process_zero , 0 , XPTR_NULL );
730
731#ifdef __HAL_x86_64__
732        return; /* XXX temporary */
733#endif
734
735    // CP0 allocates and initializes the internal peripheral chdev descriptors.
736    // Each CP0[cxy] scan the set of its internal (private) peripherals,
737    // and allocates memory for the corresponding chdev descriptors.
738    if( core_lid == 0 ) internal_devices_init( info );
739
740    // CP0 allocates one WTI mailbox per core for Inter Processor Interrupt
741    // this must be done after ICU chdev initialisation, by CP0 only, and before
742    // external devices initialisation to enforce the rule :
743    // "The WTI index for the IPI routed to core[lid] is lid"
744    if( core_lid == 1 )
745    {
746        uint32_t  wti_id;
747        uint32_t  lid;
748        for( lid = 0 ; lid < LOCAL_CLUSTER->cores_nr ; lid++ )
749        {
750            wti_id = dev_icu_wti_alloc();
751
752            if( wti_id != lid )
753            {
754                nolock_printk("\n[PANIC] in %s : WTI index for IPI = %d / core_lid = %d",
755                              __FUNCTION__ , wti_id , lid );
756                hal_core_sleep();
757            }
758
759            dev_icu_enable_irq( lid , WTI_TYPE , wti_id , NULL );
760        }
761    }
762
763    // All CP0s contribute to initialise external peripheral chdev descriptors.
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
766    // on the (cxy) cluster according to the global index value.
767    if( core_lid == 0 ) external_devices_init( info );
768
769    /////////////////////////////////////////////////////////////////////////////////
770    // global &local synchro to protect access to peripherals
771    if( core_lid == 0 ) remote_barrier( XPTR( info->io_cxy , &global_barrier ),
772                                        (info->x_size * info->y_size) );
773    barrier_wait( &local_barrier , info->cores_nr );
774    /////////////////////////////////////////////////////////////////////////////////
775
776    if( (core_lid ==  0) && (local_cxy == info->io_cxy) )
777    {
778        kinit_dmsg("\n[INFO] %s : core[%x][%d] exit barrier 2 at cycle %d\n",
779                   __FUNCTION__ , core_cxy , core_lid , hal_get_cycles() );
780    }
781
782    error = thread_kernel_init( thread,
783                                THREAD_IDLE,
784                                &thread_idle_func,
785                                NULL,
786                                core_lid );
787    if( error )
788    {
789        nolock_printk("\n[PANIC] in %s : core[%x][%d] cannot initialize idle thread\n",
790                      __FUNCTION__ , local_cxy , core_lid );
791        hal_core_sleep();
792    }
793
794    // register idle thread in scheduler
795    core->scheduler.idle = thread;
796
797    // activate the idle thread
798    thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_GLOBAL );
799
800    if( (core_lid ==  0) && (local_cxy == info->io_cxy) )
801    {
802        kinit_dmsg("\n[INFO] %s : core[%x][%d] created idle thread %x at cycle %d\n",
803                   __FUNCTION__ , core_cxy , core_lid , thread , hal_get_cycles());
804    }
805
806    // CP0 in all clusters initializes cooperatively VFS and DEVFS
807    if( (core_lid == 0)  )
808    {
809        xptr_t  root_inode_xp;
810
811        // initialize root File System (must be FATFS in this implementation)
812        if( CONFIG_VFS_ROOT_IS_FATFS )
813        {
814            root_inode_xp = fatfs_init();
815        }
816        else
817        {
818            nolock_printk("\n[PANIC] in %s : root FS must be FATFS\n", __FUNCTION__ );
819            hal_core_sleep();
820        }
821
822        if( root_inode_xp == XPTR_NULL )
823        {
824            nolock_printk("\n[PANIC] in %s : core[%x][%d] cannot initialize file system\n",
825                   __FUNCTION__ , local_cxy , core_lid );
826            hal_core_sleep();
827        }
828
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
833        // mount the DEVFS File system
834        devfs_mount( root_inode_xp , "dev" );
835    }
836
837
838    // CP0 in I/O cluster creates the process_init and print banner
839    if( (core_lid ==  0) && (local_cxy == info->io_cxy) )
840    {
841        process_init_create();
842
843        print_banner( (info->x_size * info->y_size) , info->cores_nr );
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",
866                   sizeof( thread_t          ),
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   ));
886    }
887
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    /////////////////////////////////////////////////////////////////////////////////
894
895    if( (core_lid ==  0) && (local_cxy == info->io_cxy) )
896    {
897        kinit_dmsg("\n[INFO] %s : core[%x][%d] exit barrier 3 at cycle %d\n",
898                   __FUNCTION__ , core_cxy , core_lid , hal_get_cycles() );
899    }
900
901    // each core activates its private PTI IRQ
902    dev_icu_set_period( core_lid , CONFIG_SCHED_TICK_PERIOD );
903    dev_icu_enable_irq( core_lid , PTI_TYPE , core_lid , NULL );
904
905    // each core get its private IRQ masks values
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
911    thread_dmsg("\n[INFO] %s : core[%x][%d] complete kernel init at cycle %d\n"
912                "   hwi_mask = %x / wti_mask = %x / pti_mask = %x\n",
913                    __FUNCTION__ , local_cxy , core_lid , hal_get_cycles() ,
914                    hwi_mask , wti_mask , pti_mask );
915
916    // each core jump to idle thread
917    thread_idle_func();
918}
919
Note: See TracBrowser for help on using the repository browser.