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

Last change on this file since 103 was 103, checked in by alain, 7 years ago

Introducing a nolock_printk() function used by kernel_init.

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 build by the bootloader, and used by kernel_init.
69//   it must be 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 sychronisation 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 sychronisation 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 register 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 functionnal 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        } // end loop on devices
219
220}  // end txt0_device_init()
221
222///////////////////////////////////////////////////////////////////////////////////////////
223// This static function allocates memory for the chdev (channel_device) descriptors
224// associated to the internal peripherals contained in the local cluster. These internal
225// devices (ICU, MMC, DMA) chdev descriptors are placed in the local cluster.
226// It initialises these device descriptors as specified by the boot_info_t structure,
227// including the dynamic linking with the driver for the specified implementation.
228// Finally, all copies of the devices directory are initialised.
229///////////////////////////////////////////////////////////////////////////////////////////
230// @ info    : pointer on the local boot-info structure.
231///////////////////////////////////////////////////////////////////////////////////////////
232static void internal_devices_init( boot_info_t * info )
233{
234    boot_device_t * dev;             // pointer on boot_info device (ICU/MMC/DMA)
235        uint32_t        x;               // X cluster coordinate
236        uint32_t        y;               // Y cluster coordinate
237        chdev_t       * chdev_ptr;       // local pointer on chdev descriptor
238    xptr_t          chdev_xp;        // extended pointer on chdev descriptor
239
240    ///////////  ICU   //////////
241
242    dev = &info->dev_icu;
243
244    assert( ((info->cores_nr == 0) || (dev->channels != 0)) , __FUNCTION__ ,
245            "ICU device must exist in cluster containing cores" );
246       
247    assert( (dev->channels == 1) , __FUNCTION__ ,
248            "channels number must be 1 for ICU device" );
249
250    assert( (FUNC_FROM_TYPE( dev->type ) == DEV_FUNC_ICU ) , __FUNCTION__ ,
251            " inconsistent ICU  device type");
252
253    // create one chdev in local cluster
254    chdev_ptr = chdev_create( FUNC_FROM_TYPE( dev->type ),
255                              IMPL_FROM_TYPE( dev->type ),
256                              0,                              // channel
257                              false,                          // TX
258                              dev->base );
259
260    assert( (chdev_ptr != NULL) , __FUNCTION__ , "cannot allocate ICU chdev" );
261           
262    // get extended pointer on chdev descriptor
263    chdev_xp = XPTR( local_cxy , chdev_ptr );
264
265    // make ICU specific initialisation
266    // TODO remove these three parameters
267    dev_icu_init( chdev_ptr , dev->param0 , dev->param1 , dev->param2 );
268
269    // initialize the ICU field in the chdev_dir[x][y] structures
270    // replicated in all clusters, and containing extended pointers
271    // on all remotely accessible devices
272    for( x = 0 ; x < info->x_size ; x++ )
273    {
274        for( y = 0 ; y < info->y_size ; y++ )
275        {
276            cxy_t  cxy = (x<<info->y_width) + y;
277            hal_remote_swd( XPTR( cxy , &chdev_dir.icu[local_cxy] ) , chdev_xp );
278        }
279    }
280
281    // initialize the entries of the local chdev_icu_input structure
282    // defining how internal peripherals are connected to ICU
283    uint32_t   id;
284    uint8_t    valid;
285    uint32_t   src_type;
286    uint8_t    src_ch;
287    uint32_t   src_func;
288    for( id = 0 ; id < CONFIG_MAX_HWIS_PER_ICU ; id++ )
289    {
290        valid    = dev->irq[id].valid;
291        src_type = dev->irq[id].dev_type;
292        src_ch   = dev->irq[id].channel;
293        src_func = FUNC_FROM_TYPE( src_type );
294
295        if( valid ) // only valid local IRQs are registered
296        {
297            if     ( src_func == DEV_FUNC_MMC ) chdev_icu_input.mmc = id;
298            else if( src_func == DEV_FUNC_DMA ) chdev_icu_input.dma[src_ch] = id;
299            else assert( false , __FUNCTION__ , "illegal source device for ICU input" );
300        }
301    }
302
303    kinit_dmsg("\n[INFO] %s : core[%x][0] created ICU chdev at cycle %d\n",
304               __FUNCTION__ , local_cxy , hal_get_cycles() );
305
306    /////////// MMC internal chdev /////////// 
307
308    dev = &info->dev_mmc;
309
310    if( dev->channels != 0 )   // MMC device is defined
311    {
312        assert( (dev->channels == 1) , __FUNCTION__ , 
313            "channels number must be 1 for MMC device" );
314
315        assert( (FUNC_FROM_TYPE( dev->type ) == DEV_FUNC_MMC ) , __FUNCTION__ ,
316            " inconsistent MMC device type");
317
318        // create one chdev in local cluster
319        chdev_ptr = chdev_create( FUNC_FROM_TYPE( dev->type ),
320                                  IMPL_FROM_TYPE( dev->type ),
321                                  0,                              // channel
322                                  false,                          // TX
323                                  dev->base );
324
325        assert( (chdev_ptr != NULL) , __FUNCTION__ , "cannot allocate MMC chdev" );
326           
327        // get extended pointer on chdev descriptor
328        chdev_xp = XPTR( local_cxy , chdev_ptr );
329
330        // make MMC specific initialisation
331        dev_mmc_init( chdev_ptr );     
332
333        // initialize the MMC field in the chdev_dir[x][y] structures
334        // replicated in all clusters, and containing extended pointers
335        // on all remotely accessible devices
336        for( x = 0 ; x < info->x_size ; x++ )
337        {
338            for( y = 0 ; y < info->y_size ; y++ )
339            {
340                cxy_t  cxy = (x<<info->y_width) + y;
341                hal_remote_swd( XPTR( cxy , &chdev_dir.mmc[local_cxy] ) , chdev_xp );
342            }
343        }
344
345        kinit_dmsg("\n[INFO] %s : core[%x][0] created MMC chdev at cycle %d\n",
346                   __FUNCTION__ , local_cxy , hal_get_cycles() );
347    }
348
349    /////////// DMA internal chdevs //////////
350
351    dev = &info->dev_dma;
352
353    if( dev->channels != 0 )   // DMA device is defined
354    {
355        assert( (FUNC_FROM_TYPE( dev->type ) == DEV_FUNC_DMA ) , __FUNCTION__ ,
356                " inconsistent DMA  device type");
357
358        // create one chdev per channel in local cluster
359        uint32_t channel;
360        for( channel = 0 ; channel < dev->channels ; channel++ )
361        { 
362            chdev_ptr = chdev_create( FUNC_FROM_TYPE( dev->type ),
363                                      IMPL_FROM_TYPE( dev->type ),
364                                      channel,                        // channel
365                                      false,                          // TX
366                                      dev->base );
367
368            assert( (chdev_ptr != NULL) , __FUNCTION__ , "cannot allocate DMA chdev" );
369           
370            // get extended pointer on channel descriptor
371            chdev_xp = XPTR( local_cxy , chdev_ptr );
372
373            // make DMA specific initialisation
374            dev_dma_init( chdev_ptr );     
375
376            // initialize only the DMA[channel] field in the local chdev_dir[x][y]
377            // structure because the DMA device is not remotely accessible.
378            chdev_dir.dma[channel] = chdev_xp;
379
380            kinit_dmsg("\n[INFO] %s : core[%x][0] created DMA[%d] chdev at cycle %d\n",
381                       __FUNCTION__ , local_cxy , channel , hal_get_cycles() );
382        }
383    } 
384}  // end internal_devices_init()
385
386///////////////////////////////////////////////////////////////////////////////////////////
387// This static function allocates memory for the chdev descriptors associated
388// to the external (shared) peripherals contained in the local cluster. These external
389// devices (IOB, IOC, TXT, NIC, etc ) are distributed on all clusters.
390// It initialises these device descriptors as specified by the boot_info_t structure,
391// including the dynamic linking with the driver for the specified implementation.
392// Finally, all copies of the devices directory are initialised.
393//
394// The number of channel_devices depends on the device functionnal type.
395// There is three nested loops to build the full set of external channel_devices:
396// - loop on external devices.
397// - loop on channels for multi-channels devices.
398// - loop on directions (RX/TX) for NIC device.
399// The set of channel_devices is indexed by the chdev_gid global index, that is used
400// to select the cluster containing a given chdev[func,channel,direction].
401// All clusters scan the full set of chdevs, but only the cluster matching
402// (chdev_gid % (x_size*y_size)) create the corresponding chdev.
403//
404// TODO check that cluster IO contains a PIC [AG]
405// TODO make a default initialisation for the chdev_dir structure (XPTR_NULL )  [AG]
406///////////////////////////////////////////////////////////////////////////////////////////
407// @ info    : pointer on the local boot-info structure.
408///////////////////////////////////////////////////////////////////////////////////////////
409static void external_devices_init( boot_info_t * info )
410{
411    boot_device_t * dev_tbl;         // pointer on array of devices in boot_info
412        uint32_t        dev_nr;          // actual number of devices in this cluster
413        xptr_t          base;            // remote pointer on segment base
414    uint32_t        type;            // peripheral type
415    uint32_t        func;            // device functionnal index
416    uint32_t        impl;            // device implementation index
417        uint32_t        i;               // device index in dev_tbl
418        uint32_t        x;               // X cluster coordinate
419        uint32_t        y;               // Y cluster coordinate
420        uint32_t        channels_nr;     // number of channels
421        uint32_t        channel;         // channel index
422        uint32_t        directions_nr;   // number of directions
423        uint32_t        direction;       // direction index
424        uint32_t        p0;              // device parameter 0
425        uint32_t        p1;              // device parameter 1
426        uint32_t        p2;              // device parameter 2
427        uint32_t        p3;              // device parameter 3
428    uint32_t        first_channel;   // used in loop on channels
429
430        chdev_t       * chdev;           // local pointer on one channel_device descriptor
431    xptr_t          chdev_xp;        // extended pointer on channel_device descriptor
432    uint32_t        chdev_gid = 0;   // global index of channel_device descriptor
433
434    // get number of peripherals and base of devices array from boot_info
435        dev_nr      = info->ext_dev_nr;
436    dev_tbl     = info->ext_dev;
437
438    // loop on external peripherals
439        for( i = 0 ; i < dev_nr ; i++ )
440        {
441        base        = dev_tbl[i].base;
442        type        = dev_tbl[i].type;
443        channels_nr = dev_tbl[i].channels;
444        p0          = dev_tbl[i].param0;
445        p1          = dev_tbl[i].param1;
446        p2          = dev_tbl[i].param2;
447        p3          = dev_tbl[i].param3;
448
449        func     = FUNC_FROM_TYPE( type );
450        impl     = IMPL_FROM_TYPE( type );
451
452        // There is one chdev per direction for NIC
453        if (func == DEV_FUNC_NIC) directions_nr = 2;
454        else                      directions_nr = 1;
455
456        // The TXT0 chdev has already been created
457        if (func == DEV_FUNC_TXT) first_channel = 1;
458        else                      first_channel = 0;
459
460        // do nothing for ROM, that does not require a device descriptor.
461        if( func == DEV_FUNC_ROM ) continue;
462
463        // check external device functionnal type
464        if( (func != DEV_FUNC_IOB) &&
465            (func != DEV_FUNC_PIC) &&
466            (func != DEV_FUNC_IOC) &&
467            (func != DEV_FUNC_TXT) &&
468            (func != DEV_FUNC_NIC) &&
469            (func != DEV_FUNC_FBF) ) 
470        {
471            assert( false , __FUNCTION__ , "undefined external peripheral type" );
472        }
473
474        // loops on channels
475        for( channel = first_channel ; channel < channels_nr ; channel++ )
476        { 
477            // loop on directions
478            for( direction = 0 ; direction < directions_nr ; direction++ )
479            {
480                // get target cluster for chdev[func,channel,direction]
481                uint32_t offset     = chdev_gid % ( info->x_size * info->y_size );
482                uint32_t cx         = offset / info->y_size;
483                uint32_t cy         = offset % info->y_size;
484                uint32_t target_cxy = (cx<<info->y_width) + cy;
485
486                // allocate and initialize a local chdev
487                // if local cluster matches target cluster
488                if( target_cxy == local_cxy )
489                {
490                    chdev = chdev_create( func,
491                                          impl,
492                                          channel,
493                                          direction,
494                                          base );
495
496                    assert( (chdev != NULL), __FUNCTION__ , 
497                            "cannot allocate external device" );
498
499                    // get extended pointer on chdev
500                    chdev_xp = XPTR( local_cxy , chdev );
501
502                    // make device type specific initialisation
503                    // the number of parameters depends on the device type
504                    // TODO : remove the parameters that  must be provided by the drivers
505                    if     ( func == DEV_FUNC_IOB ) dev_iob_init( chdev );
506                    else if( func == DEV_FUNC_IOC ) dev_ioc_init( chdev );
507                    else if( func == DEV_FUNC_TXT ) dev_txt_init( chdev );
508                    else if( func == DEV_FUNC_NIC ) dev_nic_init( chdev );
509                    else if( func == DEV_FUNC_PIC ) dev_pic_init( chdev , p0 );
510                    else if( func == DEV_FUNC_FBF ) dev_fbf_init( chdev , p0 , p1 );
511                    else
512                    {
513                        assert( false , __FUNCTION__ , "undefined device type" );
514                    }
515
516                    // all external (shared) devices are remotely accessible
517                    // initialize the replicated chdev_dir[x][y] structures
518                    // defining the extended pointers on chdev descriptors
519                    xptr_t * entry;   
520               
521                    if( func == DEV_FUNC_IOB ) entry  = &chdev_dir.iob;
522                    if( func == DEV_FUNC_PIC ) entry  = &chdev_dir.pic;
523                    if( func == DEV_FUNC_TXT ) entry  = &chdev_dir.txt[channel];
524                    if( func == DEV_FUNC_IOC ) entry  = &chdev_dir.ioc[channel];
525                    if( func == DEV_FUNC_FBF ) entry  = &chdev_dir.fbf[channel];
526                    if( func == DEV_FUNC_NIC ) entry  = &chdev_dir.nic_tx[channel];
527 
528                    for( x = 0 ; x < info->x_size ; x++ )
529                    {
530                        for( y = 0 ; y < info->y_size ; y++ )
531                        {
532                            cxy_t  cxy = (x<<info->y_width) + y;
533                            hal_remote_swd( XPTR( cxy , entry ) , chdev_xp );
534                        }
535                    }
536
537                            kinit_dmsg("\n[INFO] %s : core[%x][0] create chdev %s[%d] at cycle %d\n",
538                               __FUNCTION__ , local_cxy , chdev_func_str( func ),
539                               channel , hal_get_cycles() );
540
541                }  // end if match
542
543                // increment chdev global index (matching or not)
544                chdev_gid++;
545
546            } // end loop on directions
547
548        }  // end loop on channels
549
550        // initialize the entries of the local chdev_pic_input structure
551        // defining how external peripherals are connected to PIC
552        if( func == DEV_FUNC_PIC ) 
553        {
554            uint32_t   id;
555            uint8_t    valid;
556            uint32_t   dev_type;
557            uint8_t    channel;
558            uint8_t    is_rx;
559
560            // loop on PIC inputs
561            for( id = 0 ; id < CONFIG_MAX_IRQS_PER_PIC ; id++ )
562            {
563                valid     = dev_tbl[i].irq[id].valid;
564                dev_type  = dev_tbl[i].irq[id].dev_type;
565                channel   = dev_tbl[i].irq[id].channel;
566                is_rx     = dev_tbl[i].irq[id].is_rx;
567
568                if( valid )  // only valid inputs are registered
569                {
570                    uint32_t * index;  // local pointer on one entry
571                    uint16_t dev_func = FUNC_FROM_TYPE( dev_type );
572
573                    if( dev_func == DEV_FUNC_TXT )
574                    {
575                        index = &chdev_pic_input.txt[channel];
576                    }
577                    else if( dev_func == DEV_FUNC_IOC )
578                    {
579                        index = &chdev_pic_input.ioc[channel]; 
580                    }
581                    else if( (dev_func == DEV_FUNC_NIC) && (is_rx == 0) )
582                    {
583                        index = &chdev_pic_input.nic_tx[channel]; 
584                    }
585                    else if( (dev_func == DEV_FUNC_NIC) && (is_rx != 0) )
586                    {
587                        index = &chdev_pic_input.nic_rx[channel]; 
588                    }
589                    else
590                    {
591                        assert( false , __FUNCTION__ , "illegal source device for PIC input" );
592                    }                   
593
594                    // set entry in local structure
595                    *index = id; 
596                }
597            } // end loop on PIC inputs
598        } // end PIC
599        } // end loop on devices
600}  // end external_devices_init()
601
602
603///////////////////////////////////////////////////////////////////////////////////////////
604// This static function returns the identifiers of the calling core.
605///////////////////////////////////////////////////////////////////////////////////////////
606// @ info    : pointer on boot_info structure.
607// @ lid     : [out] core local index in cluster.
608// @ cxy     : [out] cluster identifier.
609// @ lid     : [out] core global identifier (hardware).
610// @ return 0 if success / return EINVAL if not found.
611///////////////////////////////////////////////////////////////////////////////////////////
612static error_t get_core_identifiers( boot_info_t * info,
613                                     lid_t       * lid,
614                                     cxy_t       * cxy,
615                                     gid_t       * gid )
616{
617        uint32_t   i;
618    gid_t      global_id;
619
620    // get global identifier from hardware register
621    global_id = hal_get_gid(); 
622
623    // makes an associative search in boot_info to get (cxy,lid) from global_id
624    for( i = 0 ; i < info->cores_nr ; i++ )
625    {
626        if( global_id == info->core[i].gid )
627        {
628            *lid = info->core[i].lid;
629            *cxy = info->core[i].cxy;
630            *gid = global_id;
631            return 0;
632        }
633    }
634    return EINVAL;
635}
636
637///////////////////////////////////////////////////////////////////////////////////////////
638// This function is the entry point for the kernel initialisation.
639// It is executed by all cores in all clusters, but only core[0], called CP0,
640// initializes the shared resources such as the cluster manager, or the local peripherals.
641// To comply with the multi-kernels paradigm, it accesses only local cluster memory, using
642// only information contained in the local boot_info_t structure, set by the bootloader.
643// Only CP0 in cluster 0 print the log messages.
644///////////////////////////////////////////////////////////////////////////////////////////
645// @ info    : pointer on the local boot-info structure.
646///////////////////////////////////////////////////////////////////////////////////////////
647void kernel_init( boot_info_t * info )
648{
649    lid_t        core_lid = -1;      // running core local index
650    cxy_t        core_cxy = -1;      // running core cluster identifier
651    gid_t        core_gid;           // running core hardware identifier
652    cluster_t  * cluster;            // pointer on local cluster manager
653    core_t     * core;               // pointer on running core descriptor
654    thread_t   * thread;             // pointer on idle thread descriptor
655    error_t      error;
656
657    // all cores get and check core identifiers
658    error = get_core_identifiers( info,
659                                  &core_lid,
660                                  &core_cxy,
661                                  &core_gid );
662
663    // CP0 initialise cluster identifier
664    if( core_lid == 0 ) local_cxy = info->cxy;
665
666    // each core get pointer on its private idle thread descriptor
667        thread = (thread_t *)( idle_threads + (core_lid * CONFIG_THREAD_DESC_SIZE) );
668
669    // each core registers this thread pointer in hardware register
670    hal_set_current_thread( thread );
671
672    // CP0 in I/O cluster initialises TXT0 chdev descriptor
673    if( (core_lid == 0) && (core_cxy == info->io_cxy) ) txt0_device_init( info );
674
675    /////////////////////////////////////////////////////////////////////////////////
676    // global & local synchro to protect access to TXT0 terminal
677    if( core_lid == 0 ) remote_barrier( XPTR( info->io_cxy , &global_barrier ), 
678                                        (info->x_size * info->y_size) );
679    barrier_wait( &local_barrier , info->cores_nr );
680    /////////////////////////////////////////////////////////////////////////////////
681
682    if( (core_lid ==  0) && (local_cxy == info->io_cxy) ) 
683    {
684        kinit_dmsg("\n[INFO] %s : core[%x][%d] exit barrier 0 at cycle %d\n",
685                   __FUNCTION__ , core_cxy , core_lid , hal_get_cycles() );
686    }
687
688    // all cores check core identifiers
689    if( error )
690    {
691        nolock_printk("\n[PANIC] in %s : illegal core identifiers"
692               " gid = %x / cxy = %x / lid = %d\n",
693               __FUNCTION__ , core_lid , core_cxy , core_lid );
694        hal_core_sleep();
695    }
696
697    // CP0 initializes the local cluster manager (cores and memory allocators)
698    if( core_lid == 0 )
699    {
700        error = cluster_init( info );
701
702        if( error )
703        {
704            nolock_printk("\n[PANIC] in %s : cannot initialise cluster manager in cluster %x",
705                   __FUNCTION__ , local_cxy );
706            hal_core_sleep();
707        }
708    }
709
710    /////////////////////////////////////////////////////////////////////////////////
711    // global & local synchro, to protect access to cluster manager
712    if( core_lid == 0 ) remote_barrier( XPTR( info->io_cxy , &global_barrier ), 
713                                        (info->x_size * info->y_size) );
714    barrier_wait( &local_barrier , info->cores_nr );
715    /////////////////////////////////////////////////////////////////////////////////
716
717    if( (core_lid ==  0) && (local_cxy == info->io_cxy) ) 
718    {
719        kinit_dmsg("\n[INFO] %s : core[%x][%d] exit barrier 1 at cycle %d\n",
720                   __FUNCTION__ , core_cxy , core_lid , hal_get_cycles() );
721    }
722
723    // all cores get pointer on local cluster manager and on core descriptor
724    cluster = &cluster_manager;
725        core    = &cluster->core_tbl[core_lid];
726
727    // CP0 initializes the process_zero descriptor
728    if( core_lid == 0 ) process_reference_init( &process_zero , 0 , XPTR_NULL );
729
730    // CP0 allocates and initialises the internal peripheral chdev descriptors.
731    // Each CP0[cxy] scan the set of its internal (private) peripherals,
732    // and allocate memory for the corresponding chdev descriptors.
733    if( core_lid == 0 ) internal_devices_init( info );
734       
735    // CP0 allocates one WTI mailbbox per core for Inter Processor Interrupt
736    // this must be done after ICU chdev initialisation, by CP0 only, and before
737    // external devices initialisation to enforce the rule :
738    // "The WTI index for the IPI routed to core[lid] is lid"
739    if( core_lid == 1 )
740    {
741        uint32_t  wti_id;
742        uint32_t  lid;
743        for( lid = 0 ; lid < LOCAL_CLUSTER->cores_nr ; lid++ )
744        {
745            wti_id = dev_icu_wti_alloc();
746
747            if( wti_id != lid )
748            {
749                nolock_printk("\n[PANIC] in %s : WTI index for IPI = %d / core_lid = %d",
750                              __FUNCTION__ , wti_id , lid );
751                hal_core_sleep();
752            }
753
754            dev_icu_enable_irq( lid , WTI_TYPE , wti_id , NULL );
755        }
756    }
757
758    // All CP0s contribute to initialise external peripheral chdev descriptors.
759    // Each CP0[cxy] scan the set of external (shared) peripherals (but the TXT0),
760    // and allocates memory for the chdev descriptors that must be placed
761    // on the (cxy) cluster according to the global index value. 
762    if( core_lid == 0 ) external_devices_init( info );
763
764    /////////////////////////////////////////////////////////////////////////////////
765    // global &local synchro to protect access to peripherals
766    if( core_lid == 0 ) remote_barrier( XPTR( info->io_cxy , &global_barrier ), 
767                                        (info->x_size * info->y_size) );
768    barrier_wait( &local_barrier , info->cores_nr );
769    /////////////////////////////////////////////////////////////////////////////////
770
771    if( (core_lid ==  0) && (local_cxy == info->io_cxy) ) 
772    {
773        kinit_dmsg("\n[INFO] %s : core[%x][%d] exit barrier 2 at cycle %d\n", 
774                   __FUNCTION__ , core_cxy , core_lid , hal_get_cycles() );
775    }
776
777    error = thread_kernel_init( thread,
778                                THREAD_IDLE, 
779                                &thread_idle_func, 
780                                NULL,
781                                core_lid );
782    if( error )
783    {
784        nolock_printk("\n[PANIC] in %s : core[%x][%d] cannot initialize idle thread\n",
785                      __FUNCTION__ , local_cxy , core_lid );
786        hal_core_sleep();
787    }
788
789    // register idle thread in scheduler
790    core->scheduler.idle = thread;
791
792    // activate the idle thread
793    thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_GLOBAL );
794
795    if( (core_lid ==  0) && (local_cxy == info->io_cxy) ) 
796    {
797        kinit_dmsg("\n[INFO] %s : core[%x][%d] created idle thread %x at cycle %d\n",
798                   __FUNCTION__ , core_cxy , core_lid , thread , hal_get_cycles());
799    }
800
801    // CP0 in all clusters initializes cooperatively VFS and DEVFS
802    if( (core_lid == 0)  )
803    {
804        xptr_t  root_inode_xp;
805
806        // initialize root File System (must be FATFS in this implementation)
807        if( CONFIG_VFS_ROOT_IS_FATFS )
808        {
809            root_inode_xp = fatfs_init();
810        }
811        else
812        {
813            nolock_printk("\n[PANIC] in %s : root FS must be FATFS\n", __FUNCTION__ );
814            hal_core_sleep();
815        }
816
817        if( root_inode_xp == XPTR_NULL )
818        {
819            nolock_printk("\n[PANIC] in %s : core[%x][%d] cannot initialize file system\n",
820                   __FUNCTION__ , local_cxy , core_lid );
821            hal_core_sleep();
822        }
823
824        // register VFS root inode in process_zero
825        process_zero.vfs_root_xp = root_inode_xp;
826        process_zero.vfs_cwd_xp  = root_inode_xp;
827
828        // mount the DEVFS File system
829            devfs_mount( root_inode_xp , "dev" );
830    }
831
832
833    // CP0 in I/O cluster creates the process_init and print banner
834    if( (core_lid ==  0) && (local_cxy == info->io_cxy) ) 
835    {
836        process_init_create();
837
838        print_banner( (info->x_size * info->y_size) , info->cores_nr );
839
840        kinit_dmsg("\n\n*** memory fooprint of main kernet objects ***\n"
841                   " - thread descriptor  : %d bytes\n"
842                   " - process descriptor : %d bytes\n"
843                   " - cluster manager    : %d bytes\n"
844                   " - chdev descriptor   : %d bytes\n"
845                   " - core descriptor    : %d bytes\n"
846                   " - scheduler          : %d bytes\n"
847                   " - rpc fifo           : %d bytes\n"
848                   " - page descriptor    : %d bytes\n"
849                   " - mapper root        : %d bytes\n"
850                   " - ppm manager        : %d bytes\n"
851                   " - kcm manager        : %d bytes\n"
852                   " - khm manager        : %d bytes\n"
853                   " - vmm manager        : %d bytes\n"
854                   " - gpt root           : %d bytes\n"
855                   " - list item          : %d bytes\n"
856                   " - xlist item         : %d bytes\n"
857                   " - spinlock           : %d bytes\n"
858                   " - remote spinlock    : %d bytes\n"
859                   " - rwlock             : %d bytes\n"
860                   " - remote rwlock      : %d bytes\n",
861                   sizeof( thread_t          ), 
862                   sizeof( process_t         ),
863                   sizeof( cluster_t         ),
864                   sizeof( chdev_t           ),
865                   sizeof( core_t            ),
866                   sizeof( scheduler_t       ),
867                   sizeof( rpc_fifo_t        ),
868                   sizeof( page_t            ),
869                   sizeof( mapper_t          ),
870                   sizeof( ppm_t             ),
871                   sizeof( kcm_t             ),
872                   sizeof( khm_t             ),
873                   sizeof( vmm_t             ),
874                   sizeof( gpt_t             ),
875                   sizeof( list_entry_t      ),
876                   sizeof( xlist_entry_t     ),
877                   sizeof( spinlock_t        ),
878                   sizeof( remote_spinlock_t ),
879                   sizeof( rwlock_t          ),
880                   sizeof( remote_rwlock_t   ));
881    }
882
883    /////////////////////////////////////////////////////////////////////////////////
884    // global syncho to protect access to File System
885    if( core_lid == 0 ) remote_barrier( XPTR( info->io_cxy , &global_barrier ),
886                                        (info->x_size * info->y_size) );
887    barrier_wait( &local_barrier , info->cores_nr );
888    /////////////////////////////////////////////////////////////////////////////////
889
890    if( (core_lid ==  0) && (local_cxy == info->io_cxy) ) 
891    {
892        kinit_dmsg("\n[INFO] %s : core[%x][%d] exit barrier 3 at cycle %d\n", 
893                   __FUNCTION__ , core_cxy , core_lid , hal_get_cycles() );
894    }
895
896    // each core activates its private PTI IRQ
897    dev_icu_set_period( core_lid , CONFIG_SCHED_TICK_PERIOD );
898    dev_icu_enable_irq( core_lid , PTI_TYPE , core_lid , NULL );
899
900    // each core get its private IRQ masks values
901    uint32_t hwi_mask;
902    uint32_t wti_mask;
903    uint32_t pti_mask;
904    dev_icu_get_masks( core_lid , &hwi_mask , &wti_mask , &pti_mask );
905
906    thread_dmsg("\n[INFO] %s : core[%x][%d] complete kernel init at cycle %d\n"
907                "   hwi_mask = %x / wti_mask = %x / pti_mask = %x\n",
908                    __FUNCTION__ , local_cxy , core_lid , hal_get_cycles() ,
909                    hwi_mask , wti_mask , pti_mask );
910
911    // each core jump to idle thread
912    thread_idle_func();
913
914} // end kernel_init()
915
Note: See TracBrowser for help on using the repository browser.