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

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

Fix bug in kernel_init, and reduce size of remote_fifo.

File size: 38.1 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 <soclib_tty.h>
56#include <devfs.h>
57#include <mapper.h>
58
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            // Complete TXT specific initialisation
192            if( impl == IMPL_TXT_TTY )
193            {
194                txt0_chdev.cmd = &soclib_tty_cmd;
195                txt0_chdev.isr = &soclib_tty_isr;
196                soclib_tty_init( &txt0_chdev );
197            }
198
199            // initialize the replicated chdev_dir[x][y] structures
200            for( x = 0 ; x < info->x_size ; x++ )
201            {
202                for( y = 0 ; y < info->y_size ; y++ )
203                {
204                    cxy_t  cxy = (x<<info->y_width) + y;
205                    hal_remote_swd( XPTR( cxy , &chdev_dir.txt[0] ) ,
206                                    XPTR( local_cxy , &txt0_chdev ) );
207                }
208            }
209
210                    kinit_dmsg("\n[INFO] %s : core[%x][0] created TXT0 chdev"
211                       " / paddr = %l at cycle %d\n",
212                       __FUNCTION__ , local_cxy , chdev_func_str( func ),
213                       XPTR(local_cxy , &txt0_chdev) , hal_time_stamp() );
214        }
215
216        } // end loop on devices
217
218}  // end txt0_device_init()
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_time_stamp() );
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_time_stamp() );
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_time_stamp() );
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 functionnal type.
393// There is 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 functionnal 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_time_stamp() );
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///////////////////////////////////////////////////////////////////////////////////////////
642// @ info    : pointer on the local boot-info structure.
643///////////////////////////////////////////////////////////////////////////////////////////
644void kernel_init( boot_info_t * info )
645{
646    lid_t        core_lid = -1;      // running core local index
647    cxy_t        core_cxy = -1;      // running core cluster identifier
648    gid_t        core_gid;           // running core hardware identifier
649    cluster_t  * cluster;            // pointer on local cluster manager
650    core_t     * core;               // pointer on running core descriptor
651    thread_t   * thread;             // pointer on idle thread descriptor
652    error_t      error;
653
654    // all cores get core identifiers
655    error = get_core_identifiers( info,
656                                  &core_lid,
657                                  &core_cxy,
658                                  &core_gid );
659
660    // CP0 initialise cluster identifier
661    if( core_lid == 0 ) local_cxy = info->cxy;
662
663    // each core get pointer on its private idle thread descriptor
664        thread = (thread_t *)( idle_threads + (core_lid * CONFIG_THREAD_DESC_SIZE) );
665
666    // each core register this thread pointer in hardware register
667    hal_set_current_thread( thread );
668   
669    // CP0 in I/O cluster initialises TXT0 chdev descriptor
670    if( (core_lid == 0) && (core_cxy == info->io_cxy) ) txt0_device_init( info );
671
672    /////////////////////////////////////////////////////////////////////////////////
673    // global & local synchro to protect access to TXT0 terminal
674    if( core_lid == 0 ) remote_barrier( XPTR( info->io_cxy , &global_barrier ), 
675                                        (info->x_size * info->y_size) );
676    barrier_wait( &local_barrier , info->cores_nr );
677    /////////////////////////////////////////////////////////////////////////////////
678
679    kinit_dmsg("\n[INFO] %s : core[%x][%d] exit barrier 0 at cycle %d\n",
680               __FUNCTION__ , core_cxy , core_lid , hal_time_stamp() );
681
682    // all cores check core identifiers
683    if( error )
684    {
685        printk("\n[PANIC] in %s : illegal core identifiers"
686               " gid = %x / cxy = %x / lid = %d\n",
687               __FUNCTION__ , core_lid , core_cxy , core_lid );
688        hal_core_sleep();
689    }
690
691    // CP0 initializes the local cluster manager (cores and memory allocators)
692    if( core_lid == 0 )
693    {
694        error = cluster_init( info );
695
696        if( error )
697        {
698            printk("\n[PANIC] in %s : cannot initialise cluster manager in cluster %x",
699                   __FUNCTION__ , local_cxy );
700            hal_core_sleep();
701        }
702    }
703
704    /////////////////////////////////////////////////////////////////////////////////
705    // global & local synchro, to protect access to cluster manager
706    if( core_lid == 0 ) remote_barrier( XPTR( info->io_cxy , &global_barrier ), 
707                                        (info->x_size * info->y_size) );
708    barrier_wait( &local_barrier , info->cores_nr );
709    /////////////////////////////////////////////////////////////////////////////////
710
711    kinit_dmsg("\n[INFO] %s : core[%x][%d] exit barrier 1 at cycle %d\n",
712               __FUNCTION__ , core_cxy , core_lid , hal_time_stamp() );
713
714    // all cores get pointer on local cluster manager and on core descriptor
715    cluster = &cluster_manager;
716        core    = &cluster->core_tbl[core_lid];
717
718    // CP0 initializes the process_zero descriptor
719    if( core_lid == 0 ) process_zero_init( info );
720
721    // CP0 allocates and initialises the internal peripheral chdev descriptors.
722    // Each CP0[cxy] scan the set of its internal (private) peripherals,
723    // and allocate memory for the corresponding chdev descriptors.
724    if( core_lid == 0 ) internal_devices_init( info );
725       
726    // CP0 allocates one WTI mailbbox per core for Inter Processor Interrupt
727    // this must be done after ICU chdev initialisation, by CP0 only, and before
728    // external devices initialisation to enforce the rule :
729    // "The WTI index for the IPI routed to core[lid] is lid"
730    if( core_lid == 0 )
731    {
732        uint32_t  wti_id;
733        uint32_t  lid;
734        for( lid = 0 ; lid < LOCAL_CLUSTER->cores_nr ; lid++ )
735        {
736            wti_id = dev_icu_wti_alloc();
737
738            if( wti_id != lid )
739            {
740                printk("\n[PANIC] in %s : WTI index for IPI = %d / core_lid = %d",
741                       __FUNCTION__ , wti_id , lid );
742                hal_core_sleep();
743            }
744
745            dev_icu_enable_irq( lid , WTI_TYPE , wti_id , NULL );
746        }
747    }
748
749    // All CP0s contribute to initialise external peripheral chdev descriptors.
750    // Each CP0[cxy] scan the set of external (shared) peripherals (but the TXT0),
751    // and allocates memory for the chdev descriptors that must be placed
752    // on the (cxy) cluster according to the global index value. 
753    if( core_lid == 0 ) external_devices_init( info );
754
755    /////////////////////////////////////////////////////////////////////////////////
756    // global &local synchro to protect access to peripherals
757    if( core_lid == 0 ) remote_barrier( XPTR( info->io_cxy , &global_barrier ), 
758                                        (info->x_size * info->y_size) );
759    barrier_wait( &local_barrier , info->cores_nr );
760    /////////////////////////////////////////////////////////////////////////////////
761
762    kinit_dmsg("\n[INFO] %s : core[%x][%d] exit barrier 2 at cycle %d\n", 
763               __FUNCTION__ , core_cxy , core_lid , hal_time_stamp() );
764
765
766    error = thread_kernel_init( thread,
767                                THREAD_IDLE, 
768                                &thread_idle_func, 
769                                NULL,
770                                core_lid );
771    if( error )
772    {
773        printk("\n[PANIC] in %s : core[%x][%d] cannot initialize idle thread\n",
774               __FUNCTION__ , local_cxy , core_lid );
775        hal_core_sleep();
776    }
777    else
778    {
779        // register idle thread in scheduler
780        core->scheduler.idle = thread;
781
782        // activate the idle thread
783        thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_GLOBAL );
784
785        kinit_dmsg("\n[INFO] %s : core[%x][%d] created idle thread %x at cycle %d\n",
786                   __FUNCTION__ , core_cxy , core_lid , thread , hal_time_stamp());
787    }
788
789    // CP0 in all clusters initializes cooperatively VFS and DEVFS
790    if( (core_lid == 0)  )
791    {
792        xptr_t  root_inode_xp;
793
794        // initialize root File System (must be FATFS in this implementation)
795        if( CONFIG_VFS_ROOT_IS_FATFS )
796        {
797            root_inode_xp = fatfs_init();
798        }
799        else
800        {
801            printk("\n[PANIC] in %s : root FS must be FATFS\n", __FUNCTION__ );
802            hal_core_sleep();
803        }
804
805        if( root_inode_xp == XPTR_NULL )
806        {
807            printk("\n[PANIC] in %s : core[%x][%d] cannot initialize file system\n",
808                   __FUNCTION__ , local_cxy , core_lid );
809            hal_core_sleep();
810        }
811
812        // mount the DEVFS File system
813            devfs_mount( root_inode_xp , "dev" );
814    }
815
816    // CP0 in I/O cluster print banner
817    if( (core_lid ==  0) && (local_cxy == info->io_cxy) ) 
818    {
819        print_banner( (info->x_size * info->y_size) , info->cores_nr );
820
821        kinit_dmsg("\n\n*** memory fooprint of main kernet objects ***\n"
822                   " - thread descriptor  : %d bytes\n"
823                   " - process descriptor : %d bytes\n"
824                   " - cluster manager    : %d bytes\n"
825                   " - chdev descriptor   : %d bytes\n"
826                   " - core descriptor    : %d bytes\n"
827                   " - scheduler          : %d bytes\n"
828                   " - rpc fifo           : %d bytes\n"
829                   " - page descriptor    : %d bytes\n"
830                   " - mapper root        : %d bytes\n"
831                   " - ppm manager        : %d bytes\n"
832                   " - kcm manager        : %d bytes\n"
833                   " - khm manager        : %d bytes\n"
834                   " - vmm manager        : %d bytes\n"
835                   " - gpt root           : %d bytes\n"
836                   " - list item          : %d bytes\n"
837                   " - xlist item         : %d bytes\n"
838                   " - spinlock           : %d bytes\n"
839                   " - remote spinlock    : %d bytes\n"
840                   " - rwlock             : %d bytes\n"
841                   " - remote rwlock      : %d bytes\n",
842                   sizeof( thread_t          ), 
843                   sizeof( process_t         ),
844                   sizeof( cluster_t         ),
845                   sizeof( chdev_t           ),
846                   sizeof( core_t            ),
847                   sizeof( scheduler_t       ),
848                   sizeof( rpc_fifo_t        ),
849                   sizeof( page_t            ),
850                   sizeof( mapper_t          ),
851                   sizeof( ppm_t             ),
852                   sizeof( kcm_t             ),
853                   sizeof( khm_t             ),
854                   sizeof( vmm_t             ),
855                   sizeof( gpt_t             ),
856                   sizeof( list_entry_t      ),
857                   sizeof( xlist_entry_t     ),
858                   sizeof( spinlock_t        ),
859                   sizeof( remote_spinlock_t ),
860                   sizeof( rwlock_t          ),
861                   sizeof( remote_rwlock_t   ));
862    }
863
864    /////////////////////////////////////////////////////////////////////////////////
865    // global syncho to protect access to File System
866    if( core_lid == 0 ) remote_barrier( XPTR( info->io_cxy , &global_barrier ),
867                                        (info->x_size * info->y_size) );
868    barrier_wait( &local_barrier , info->cores_nr );
869    /////////////////////////////////////////////////////////////////////////////////
870
871    kinit_dmsg("\n[INFO] %s : core[%x][%d] exit barrier 3 at cycle %d\n", 
872               __FUNCTION__ , core_cxy , core_lid , hal_time_stamp() );
873
874    // each core activates its private PTI IRQ
875    dev_icu_set_period( core_lid , CONFIG_SCHED_TICK_PERIOD );
876    dev_icu_enable_irq( core_lid , PTI_TYPE , core_lid , NULL );
877
878    // each core get its private IRQ masks values and
879    uint32_t hwi_mask;
880    uint32_t wti_mask;
881    uint32_t pti_mask;
882    dev_icu_get_masks( core_lid , &hwi_mask , &wti_mask , &pti_mask );
883
884    thread_dmsg("\n[INFO] %s : core[%x][%d] activates scheduler at cycle %d\n"
885                "   hwi_mask = %x / wti_mask = %x / pti_mask = %x\n",
886                    __FUNCTION__ , local_cxy , core_lid , hal_time_stamp() ,
887                    hwi_mask , wti_mask , pti_mask );
888
889    // each core jump to idle thread
890    thread_idle_func();
891
892} // end kernel_init()
893
Note: See TracBrowser for help on using the repository browser.