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

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

Remove y_max in kernel init barriers

File size: 61.3 KB
RevLine 
[1]1/*
2 * kernel_init.c - kernel parallel initialization
[127]3 *
[23]4 * Authors :  Mohamed Lamine Karaoui (2015)
5 *            Alain Greiner  (2016,2017)
[1]6 *
7 * Copyright (c) Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
[14]25#include <kernel_config.h>
[556]26#include <hard_config.h> // for the USE_TXT_XXX macros
[1]27#include <errno.h>
[457]28#include <hal_kernel_types.h>
[1]29#include <hal_special.h>
30#include <hal_context.h>
[279]31#include <hal_irqmask.h>
[296]32#include <hal_ppm.h>
[14]33#include <barrier.h>
[1]34#include <remote_barrier.h>
[407]35#include <remote_fifo.h>
[1]36#include <core.h>
37#include <list.h>
[68]38#include <xlist.h>
[204]39#include <xhtab.h>
[1]40#include <thread.h>
41#include <scheduler.h>
42#include <kmem.h>
43#include <cluster.h>
44#include <string.h>
45#include <memcpy.h>
46#include <ppm.h>
47#include <page.h>
[5]48#include <chdev.h>
[1]49#include <boot_info.h>
50#include <dqdt.h>
51#include <dev_mmc.h>
[5]52#include <dev_dma.h>
53#include <dev_iob.h>
[1]54#include <dev_ioc.h>
[5]55#include <dev_txt.h>
[1]56#include <dev_pic.h>
57#include <printk.h>
58#include <vfs.h>
[23]59#include <devfs.h>
[68]60#include <mapper.h>
[559]61#include <cluster_info.h>
[1]62
63///////////////////////////////////////////////////////////////////////////////////////////
[279]64// All the following global variables are replicated in all clusters.
[1]65// They are initialised by the kernel_init() function.
[14]66//
[127]67// WARNING : The section names have been defined to control the base addresses of the
[14]68// boot_info structure and the idle thread descriptors, through the kernel.ld script:
[127]69// - the boot_info structure is built by the bootloader, and used by kernel_init.
70//   it must be the first object in the kdata segment.
[14]71// - the array of idle threads descriptors must be placed on the first page boundary after
72//   the boot_info structure in the kdata segment.
[1]73///////////////////////////////////////////////////////////////////////////////////////////
74
[5]75// This variable defines the local boot_info structure
76__attribute__((section(".kinfo")))
[14]77boot_info_t          boot_info;
[5]78
[14]79// This variable defines the "idle" threads descriptors array
80__attribute__((section(".kidle")))
[381]81char                 idle_threads[CONFIG_THREAD_DESC_SIZE *
[14]82                                   CONFIG_MAX_LOCAL_CORES]   CONFIG_PPM_PAGE_ALIGNED;
83
[127]84// This variable defines the local cluster manager
[5]85__attribute__((section(".kdata")))
[19]86cluster_t            cluster_manager                         CONFIG_CACHE_LINE_ALIGNED;
[1]87
[407]88// This variable defines the TXT0 kernel terminal (TX only)
[188]89__attribute__((section(".kdata")))
90chdev_t              txt0_chdev                              CONFIG_CACHE_LINE_ALIGNED;
91
[539]92// This variable defines the TXT0 lock for writing characters to MTY0
93__attribute__((section(".kdata")))
94spinlock_t           txt0_lock                               CONFIG_CACHE_LINE_ALIGNED;
95
[14]96// This variables define the kernel process0 descriptor
[5]97__attribute__((section(".kdata")))
[19]98process_t            process_zero                            CONFIG_CACHE_LINE_ALIGNED;
[1]99
[14]100// This variable defines extended pointers on the distributed chdevs
[5]101__attribute__((section(".kdata")))
[14]102chdev_directory_t    chdev_dir                               CONFIG_CACHE_LINE_ALIGNED;
[1]103
[188]104// This variable contains the input IRQ indexes for the IOPIC controller
[5]105__attribute__((section(".kdata")))
[246]106iopic_input_t        iopic_input                             CONFIG_CACHE_LINE_ALIGNED;
[1]107
[188]108// This variable contains the input IRQ indexes for the LAPIC controller
[5]109__attribute__((section(".kdata")))
[188]110lapic_input_t        lapic_input                             CONFIG_CACHE_LINE_ALIGNED;
[1]111
[14]112// This variable defines the local cluster identifier
[5]113__attribute__((section(".kdata")))
[14]114cxy_t                local_cxy                               CONFIG_CACHE_LINE_ALIGNED;
[5]115
[127]116// This variable is used for CP0 cores synchronisation in kernel_init()
[5]117__attribute__((section(".kdata")))
[14]118remote_barrier_t     global_barrier                          CONFIG_CACHE_LINE_ALIGNED;
[1]119
[127]120// This variable is used for local cores synchronisation in kernel_init()
[14]121__attribute__((section(".kdata")))
122barrier_t            local_barrier                           CONFIG_CACHE_LINE_ALIGNED;
123
[127]124// This variable defines the array of supported File System contexts
[50]125__attribute__((section(".kdata")))
126vfs_ctx_t            fs_context[FS_TYPES_NR]                 CONFIG_CACHE_LINE_ALIGNED;
127
[490]128// kernel_init is the entry point defined in hal/tsar_mips32/kernel.ld
129// It will be used by the bootloader.
130extern void kernel_init( boot_info_t * info );
[50]131
[435]132// these debug variables are used to analyse the sys_read() syscall timing
[408]133
[438]134#if DEBUG_SYS_READ
[407]135uint32_t   enter_sys_read;
136uint32_t   exit_sys_read;
137
[435]138uint32_t   enter_devfs_read;
139uint32_t   exit_devfs_read;
[407]140
141uint32_t   enter_txt_read;
142uint32_t   exit_txt_read;
143
[435]144uint32_t   enter_chdev_cmd_read;
145uint32_t   exit_chdev_cmd_read;
[407]146
[435]147uint32_t   enter_chdev_server_read;
148uint32_t   exit_chdev_server_read;
[407]149
[435]150uint32_t   enter_tty_cmd_read;
151uint32_t   exit_tty_cmd_read;
[407]152
[435]153uint32_t   enter_tty_isr_read;
154uint32_t   exit_tty_isr_read;
[407]155#endif
156
[435]157// these debug variables are used to analyse the sys_write() syscall timing
158
[438]159#if DEBUG_SYS_WRITE   
[435]160uint32_t   enter_sys_write;
161uint32_t   exit_sys_write;
162
163uint32_t   enter_devfs_write;
164uint32_t   exit_devfs_write;
165
166uint32_t   enter_txt_write;
167uint32_t   exit_txt_write;
168
169uint32_t   enter_chdev_cmd_write;
170uint32_t   exit_chdev_cmd_write;
171
172uint32_t   enter_chdev_server_write;
173uint32_t   exit_chdev_server_write;
174
175uint32_t   enter_tty_cmd_write;
176uint32_t   exit_tty_cmd_write;
177
178uint32_t   enter_tty_isr_write;
179uint32_t   exit_tty_isr_write;
180#endif
181
[1]182///////////////////////////////////////////////////////////////////////////////////////////
[5]183// This function displays the ALMOS_MKH banner.
[1]184///////////////////////////////////////////////////////////////////////////////////////////
[5]185static void print_banner( uint32_t nclusters , uint32_t ncores )
[127]186{
[5]187    printk("\n"
188           "                    _        __    __     _____     ______         __    __    _   __   _     _   \n"
189           "          /\\       | |      |  \\  /  |   / ___ \\   / _____|       |  \\  /  |  | | / /  | |   | |  \n"
190           "         /  \\      | |      |   \\/   |  | /   \\ | | /             |   \\/   |  | |/ /   | |   | |  \n"
191           "        / /\\ \\     | |      | |\\  /| |  | |   | | | |_____   ___  | |\\  /| |  |   /    | |___| |  \n"
192           "       / /__\\ \\    | |      | | \\/ | |  | |   | | \\_____  \\ |___| | | \\/ | |  |   \\    |  ___  |  \n"
193           "      / ______ \\   | |      | |    | |  | |   | |       | |       | |    | |  | |\\ \\   | |   | |  \n"
194           "     / /      \\ \\  | |____  | |    | |  | \\___/ |  _____/ |       | |    | |  | | \\ \\  | |   | |  \n"
195           "    /_/        \\_\\ |______| |_|    |_|   \\_____/  |______/        |_|    |_|  |_|  \\_\\ |_|   |_|  \n"
196           "\n\n\t\t Advanced Locality Management Operating System / Multi Kernel Hybrid\n"
[457]197           "\n\n\t\t %s / %d cluster(s) / %d core(s) per cluster\n\n",
198           CONFIG_ALMOS_VERSION , nclusters , ncores );
[5]199}
[1]200
201
[5]202///////////////////////////////////////////////////////////////////////////////////////////
[188]203// This function initializes the TXT0 chdev descriptor, that is the "kernel terminal",
204// shared by all kernel instances for debug messages.
205// It is a global variable (replicated in all clusters), because this terminal is used
206// before the kmem allocator initialisation, but only the instance in cluster containing
207// the calling core is registered in the "chdev_dir" directory.
[127]208// As this TXT0 chdev supports only the TXT_SYNC_WRITE command, we don't create
209// a server thread, we don't allocate a WTI, and we don't initialize the waiting queue.
[5]210///////////////////////////////////////////////////////////////////////////////////////////
211// @ info    : pointer on the local boot-info structure.
212///////////////////////////////////////////////////////////////////////////////////////////
213static void txt0_device_init( boot_info_t * info )
214{
215    boot_device_t * dev_tbl;         // pointer on array of devices in boot_info
[127]216    uint32_t        dev_nr;          // actual number of devices in this cluster
217    xptr_t          base;            // remote pointer on segment base
218    uint32_t        func;            // device functional index
[5]219    uint32_t        impl;            // device implementation index
[127]220    uint32_t        i;               // device index in dev_tbl
221    uint32_t        x;               // X cluster coordinate
222    uint32_t        y;               // Y cluster coordinate
[188]223    uint32_t        channels;        // number of channels
[1]224
[5]225    // get number of peripherals and base of devices array from boot_info
[127]226    dev_nr      = info->ext_dev_nr;
[5]227    dev_tbl     = info->ext_dev;
[1]228
[14]229    // loop on external peripherals to find TXT device
[127]230    for( i = 0 ; i < dev_nr ; i++ )
231    {
[5]232        base        = dev_tbl[i].base;
[188]233        func        = FUNC_FROM_TYPE( dev_tbl[i].type );
234        impl        = IMPL_FROM_TYPE( dev_tbl[i].type );
235        channels    = dev_tbl[i].channels;
[5]236
[127]237        if (func == DEV_FUNC_TXT )
[5]238        {
[492]239            assert( (channels > 0) , "number of TXT channels cannot be 0\n");
[5]240
[428]241            // initializes TXT_TX[0] chdev
[188]242            txt0_chdev.func    = func;
243            txt0_chdev.impl    = impl;
244            txt0_chdev.channel = 0;
245            txt0_chdev.base    = base;
246            txt0_chdev.is_rx   = false;
247
248            // initializes lock
[14]249            remote_spinlock_init( XPTR( local_cxy , &txt0_chdev.wait_lock ) );
[188]250           
251            // TXT specific initialisation:
252            // no server thread & no IRQ routing for channel 0
253            dev_txt_init( &txt0_chdev );                 
[14]254
[188]255            // register the TXT0 in all chdev_dir[x][y] structures
[5]256            for( x = 0 ; x < info->x_size ; x++ )
257            {
[559]258                for( y = 0 ; y < info->y_size; y++ ) // [FIXME]
[5]259                {
[559]260                    if (cluster_info_is_active(info->cluster_info[x][y])) {
261                        cxy_t  cxy = (x<<info->y_width) + y;
262                        hal_remote_swd( XPTR( cxy , &chdev_dir.txt_tx[0] ) ,
263                                        XPTR( local_cxy , &txt0_chdev ) );
264                    }
[5]265                }
266            }
267        }
[188]268        } // end loop on devices
269}  // end txt0_device_init()
[5]270
[1]271///////////////////////////////////////////////////////////////////////////////////////////
[535]272// This function is the same as txt0_device_init() but uses the internal multi_tty device
273// attached to cluster (0,0) instead of the external tty_tsar.
274// This function is used instead of txt0_device_init() only for TSAR LETI.
275///////////////////////////////////////////////////////////////////////////////////////////
276// @ info    : pointer on the local boot-info structure.
277///////////////////////////////////////////////////////////////////////////////////////////
278static void mtty0_device_init( boot_info_t * info)
279{
280    boot_device_t * dev_tbl;         // pointer on array of devices in boot_info
281    uint32_t        dev_nr;          // actual number of devices in this cluster
282    xptr_t          base;            // remote pointer on segment base
283    uint32_t        func;            // device functional index
284    uint32_t        impl;            // device implementation index
285    uint32_t        i;               // device index in dev_tbl
286    uint32_t        x;               // X cluster coordinate
287    uint32_t        y;               // Y cluster coordinate
288
289    dev_nr = info->int_dev_nr;
290    dev_tbl = info->int_dev;
291
[539]292    // Initialize spinlock for writing to MTY0
293    spinlock_init(&txt0_lock);
294   
[535]295    // Loop on internal peripherals of cluster (0,0) to find MTY0
296    for ( i = 0; i < dev_nr; i++ )
297    {
298        base = dev_tbl[i].base;
299        func = FUNC_FROM_TYPE( dev_tbl[i].type );
300        impl = IMPL_FROM_TYPE( dev_tbl[i].type );
301
302        if ( func == DEV_FUNC_TXT )
303        {
304            txt0_chdev.func     = func;
305            txt0_chdev.impl     = impl;
306            txt0_chdev.channel  = 0;
307            txt0_chdev.base     = base;
308            txt0_chdev.is_rx    = false;
309
310            // Initialize MTY0 chdev lock
311            remote_spinlock_init( XPTR( local_cxy, &txt0_chdev.wait_lock ) );
312
313            // MTY specific initialization
314            dev_txt_init( &txt0_chdev );
315
316            // register the MTY in all chdev_dir[x][y] structures
317            for( x = 0 ; x < info->x_size ; x++ )
318            {
[559]319                for( y = 0 ; y < info->y_size; y++ ) // [FIXME]
[535]320                {
[559]321                    if (cluster_info_is_active(info->cluster_info[x][y])) {
322                        cxy_t  cxy = (x<<info->y_width) + y;
323                        hal_remote_swd( XPTR( cxy , &chdev_dir.txt_tx[0] ) ,
324                                        XPTR( local_cxy , &txt0_chdev ) );
325                    }
[535]326                }
327            }
328        }
329    } // end loop on internal devices
330} // end mty0_device_init()
331
332///////////////////////////////////////////////////////////////////////////////////////////
[188]333// This function allocates memory and initializes the chdev descriptors for the internal
334// peripherals contained in the local cluster, other than the LAPIC, as specified by
335// the boot_info, including the linking with the driver for the specified implementation.
336// The relevant entries in all copies of the devices directory are initialised.
[1]337///////////////////////////////////////////////////////////////////////////////////////////
338// @ info    : pointer on the local boot-info structure.
339///////////////////////////////////////////////////////////////////////////////////////////
[5]340static void internal_devices_init( boot_info_t * info )
[1]341{
[188]342    boot_device_t * dev_tbl;         // pointer on array of internaldevices in boot_info
343        uint32_t        dev_nr;          // actual number of devices in this cluster
344        xptr_t          base;            // remote pointer on segment base
345    uint32_t        func;            // device functionnal index
346    uint32_t        impl;            // device implementation index
347        uint32_t        i;               // device index in dev_tbl
348        uint32_t        x;               // X cluster coordinate
349        uint32_t        y;               // Y cluster coordinate
350        uint32_t        channels;        // number of channels
351        uint32_t        channel;         // channel index
352        chdev_t       * chdev_ptr;       // local pointer on created chdev
[1]353
[188]354    // get number of internal peripherals and base from boot_info
355        dev_nr  = info->int_dev_nr;
356    dev_tbl = info->int_dev;
[1]357
[188]358    // loop on internal peripherals
359        for( i = 0 ; i < dev_nr ; i++ )
360        {
361        base        = dev_tbl[i].base;
362        channels    = dev_tbl[i].channels;
363        func        = FUNC_FROM_TYPE( dev_tbl[i].type );
364        impl        = IMPL_FROM_TYPE( dev_tbl[i].type );
[204]365 
[188]366        //////////////////////////
367        if( func == DEV_FUNC_MMC ) 
[5]368        {
[492]369            assert( (channels == 1) , "MMC device must be single channel\n" );
[1]370
[188]371            // create chdev in local cluster
372            chdev_ptr = chdev_create( func,
373                                      impl,
374                                      0,          // channel
375                                      false,      // direction
376                                      base );
[14]377
[492]378            assert( (chdev_ptr != NULL) ,
[188]379                    "cannot allocate memory for MMC chdev\n" );
380           
381            // make MMC specific initialisation
382            dev_mmc_init( chdev_ptr );
[1]383
[188]384            // set the MMC field in all chdev_dir[x][y] structures
385            for( x = 0 ; x < info->x_size ; x++ )
[1]386            {
[559]387                for( y = 0 ; y < info->y_size; y++ ) // [FIXME]
[188]388                {
[559]389                    if (cluster_info_is_active(info->cluster_info[x][y])) {
390                        cxy_t  cxy = (x<<info->y_width) + y;
391                        hal_remote_swd( XPTR( cxy , &chdev_dir.mmc[local_cxy] ), 
392                                        XPTR( local_cxy , chdev_ptr ) );
393                    }
[188]394                }
[1]395            }
[188]396
[438]397#if( DEBUG_KERNEL_INIT & 0x1 )
398if( hal_time_stamp() > DEBUG_KERNEL_INIT )
[407]399printk("\n[DBG] %s : created MMC in cluster %x / chdev = %x\n",
400__FUNCTION__ , local_cxy , chdev_ptr );
[389]401#endif
[14]402        }
[188]403        ///////////////////////////////
404        else if( func == DEV_FUNC_DMA )
[127]405        {
[188]406            // create one chdev per channel in local cluster
407            for( channel = 0 ; channel < channels ; channel++ )
408            {   
409                // create chdev[channel] in local cluster
410                chdev_ptr = chdev_create( func,
411                                          impl,
412                                          channel,
413                                          false,     // direction
414                                          base );
[5]415
[492]416                assert( (chdev_ptr != NULL) , "cannot allocate memory for DMA chdev" );
[536]417
[188]418                // make DMA specific initialisation
419                dev_dma_init( chdev_ptr );     
[127]420
[188]421                // initialize only the DMA[channel] field in the local chdev_dir[x][y]
422                // structure because the DMA device is not remotely accessible.
423                chdev_dir.dma[channel] = XPTR( local_cxy , chdev_ptr );
[5]424
[438]425#if( DEBUG_KERNEL_INIT & 0x1 )
426if( hal_time_stamp() > DEBUG_KERNEL_INIT )
[407]427printk("\n[DBG] %s : created DMA[%d] in cluster %x / chdev = %x\n",
[389]428__FUNCTION__ , channel , local_cxy , chdev_ptr );
429#endif
[188]430            }
[14]431        }
[536]432
[550]433        ///////////////////////////////
[556]434        else if ( func == DEV_FUNC_TXT && USE_TXT_MTY == 1 )
[536]435        {
436            assert(impl == IMPL_TXT_MTY,
437                "Internal TTYs should have MTY implementation\n");
438
439            for ( channel = 0; channel < channels; channel++ )
440            {
441                int rx;
442                for ( rx = 0; rx <= 1; rx++ )
443                {
444                    // skip MTY0_TX since it has already been initialized
445                    if ( channel == 0 && rx == 0 ) continue;
446
447                    // create chdev in local cluster
448                    chdev_ptr = chdev_create( func,
449                                              impl,
450                                              channel,
451                                              rx,
452                                              base );
453
454                    assert( (chdev_ptr != NULL) ,
455                        "cannot allocate memory for MTY chdev" );
456
457                    // make MTY specific initialization
458                    dev_txt_init( chdev_ptr );
459
460                    // set the MTY fields in all clusters
461                    xptr_t *chdev_entry;
462                    if ( rx == 1 ) {
463                        chdev_entry = &chdev_dir.txt_rx[channel];
464                    } else {
465                        chdev_entry = &chdev_dir.txt_tx[channel];
466                    }
[559]467                    for ( x = 0; x < info->x_size; x++ )
[536]468                    {
[559]469                        for ( y = 0; y < info->y_size; y++ )
[536]470                        {
[559]471                            if (cluster_info_is_active(info->cluster_info[x][y])) {
472                                cxy_t cxy = (x<<info->y_width) + y;
473                                hal_remote_swd( XPTR( cxy, chdev_entry ),
474                                                XPTR( local_cxy, chdev_ptr ) );
475                            }
[536]476                        }
477                    }
[550]478#if( DEBUG_KERNEL_INIT & 0x1 )
479if( hal_time_stamp() > DEBUG_KERNEL_INIT )
480printk("\n[DBG] %s : created MTY[%d] in cluster %x / chdev = %x\n",
481__FUNCTION__ , channel , local_cxy , chdev_ptr );
482#endif
[536]483                }
484            }
485        }
[550]486
487        ///////////////////////////////
488        else if ( func == DEV_FUNC_IOC )
489        {
490            assert(impl == IMPL_IOC_SPI, __FUNCTION__,
491                "Internal IOC should have SPI implementation\n");
492
493            for ( channel = 0; channel < channels; channel++ )
494            {
495                // create chdev in local cluster
496                chdev_ptr = chdev_create( func,
497                                          impl,
498                                          channel,
499                                          0,
500                                          base );
501
502                assert( (chdev_ptr != NULL) , __FUNCTION__ , 
503                    "cannot allocate memory for IOC chdev" );
504               
505                // make IOC specific initialization
506                dev_ioc_init( chdev_ptr );
507
508                // set the IOC fields in all clusters
509                xptr_t *chdev_entry = &chdev_dir.ioc[channel];
[559]510                for ( x = 0; x < info->x_size; x++ )
[550]511                {
[559]512                    for ( y = 0; y < info->y_size; y++ )
[550]513                    {
[559]514                        if (cluster_info_is_active(info->cluster_info[x][y])) {
515                            cxy_t cxy = (x<<info->y_width) + y;
516                            hal_remote_swd( XPTR( cxy, chdev_entry ),
517                                            XPTR( local_cxy, chdev_ptr ) );
518                        }
[550]519                    }
[127]520    }
[550]521#if( DEBUG_KERNEL_INIT & 0x1 )
522if( hal_time_stamp() > DEBUG_KERNEL_INIT )
523printk("\n[DBG] %s : created IOC[%d] in cluster %x / chdev = %x\n",
524__FUNCTION__ , channel , local_cxy , chdev_ptr );
525#endif
526            }
527        }
528
529    }
[5]530}  // end internal_devices_init()
531
532///////////////////////////////////////////////////////////////////////////////////////////
[188]533// This function allocates memory and initializes the chdev descriptors for the 
[408]534// external (shared) peripherals other than the IOPIC, as specified by the boot_info.
535// This includes the dynamic linking with the driver for the specified implementation.
[188]536// These chdev descriptors are distributed on all clusters, using a modulo on a global
[408]537// index, identically computed in all clusters.
538// This function is executed in all clusters by the CP0 core, that computes a global index
539// for all external chdevs. Each CP0 core creates only the chdevs that must be placed in
540// the local cluster, because the global index matches the local index.
[188]541// The relevant entries in all copies of the devices directory are initialised.
[5]542///////////////////////////////////////////////////////////////////////////////////////////
543// @ info    : pointer on the local boot-info structure.
544///////////////////////////////////////////////////////////////////////////////////////////
545static void external_devices_init( boot_info_t * info )
546{
[188]547    boot_device_t * dev_tbl;         // pointer on array of external devices in boot_info
548        uint32_t        dev_nr;          // actual number of external devices
549        xptr_t          base;            // remote pointer on segment base
[5]550    uint32_t        func;            // device functionnal index
551    uint32_t        impl;            // device implementation index
[188]552        uint32_t        i;               // device index in dev_tbl
553        uint32_t        x;               // X cluster coordinate
554        uint32_t        y;               // Y cluster coordinate
555        uint32_t        channels;        // number of channels
556        uint32_t        channel;         // channel index
557        uint32_t        directions;      // number of directions (1 or 2)
558        uint32_t        rx;              // direction index (0 or 1)
[127]559    chdev_t       * chdev;           // local pointer on one channel_device descriptor
[188]560    uint32_t        ext_chdev_gid;   // global index of external chdev
[5]561
562    // get number of peripherals and base of devices array from boot_info
[127]563    dev_nr      = info->ext_dev_nr;
[5]564    dev_tbl     = info->ext_dev;
565
[188]566    // initializes global index (PIC is already placed in cluster 0
567    ext_chdev_gid = 1;
568
[5]569    // loop on external peripherals
[127]570    for( i = 0 ; i < dev_nr ; i++ )
571    {
[188]572        base     = dev_tbl[i].base;
573        channels = dev_tbl[i].channels;
574        func     = FUNC_FROM_TYPE( dev_tbl[i].type );
575        impl     = IMPL_FROM_TYPE( dev_tbl[i].type );
[5]576
[407]577        // There is one chdev per direction for NIC and for TXT
578        if((func == DEV_FUNC_NIC) || (func == DEV_FUNC_TXT)) directions = 2;
579        else                                                 directions = 1;
[5]580
[407]581        // do nothing for ROM, that does not require a device descriptor.
[5]582        if( func == DEV_FUNC_ROM ) continue;
583
[188]584        // do nothing for PIC, that is already initialized
585        if( func == DEV_FUNC_PIC ) continue;
[5]586
[188]587        // check PIC device initialized
[492]588        assert( (chdev_dir.pic != XPTR_NULL ) ,
[188]589              "PIC device must be initialized before other devices\n" );
590
591        // check external device functionnal type
592        assert( ( (func == DEV_FUNC_IOB) ||
593                  (func == DEV_FUNC_IOC) ||
594                  (func == DEV_FUNC_TXT) ||
595                  (func == DEV_FUNC_NIC) ||
[492]596                  (func == DEV_FUNC_FBF) ) ,
[188]597                  "undefined external peripheral type\n" );
598
[127]599        // loops on channels
[428]600        for( channel = 0 ; channel < channels ; channel++ )
[127]601        {
[5]602            // loop on directions
[188]603            for( rx = 0 ; rx < directions ; rx++ )
[1]604            {
[428]605                // skip TXT_TX[0] chdev that has already been created & registered
[556]606                if( USE_TXT_MTY == 0 && (func == DEV_FUNC_TXT) && (channel == 0) && (rx == 0) )
[536]607                {
608                    continue;
609                }
[428]610
[536]611                // skip TXT chdevs because they are initialized in internal_devices_init()
[556]612                if ( USE_TXT_MTY == 1 && func == DEV_FUNC_TXT )
[536]613                {
614                    continue;
615                }
616
[550]617                if ( func == DEV_FUNC_IOC && impl == IMPL_IOC_SPI )
618                {
619                    continue;
620                }
621
[188]622                // compute target cluster for chdev[func,channel,direction]
[530]623                uint32_t offset     = ext_chdev_gid % ( info->x_size * (info->y_max) ); // [FIXME]
624                uint32_t cx         = offset / (info->y_max); // [FIXME]
625                uint32_t cy         = offset % (info->y_max); // [FIXME]
[5]626                uint32_t target_cxy = (cx<<info->y_width) + cy;
[1]627
[5]628                // allocate and initialize a local chdev
[407]629                // when local cluster matches target cluster
[5]630                if( target_cxy == local_cxy )
[1]631                {
[5]632                    chdev = chdev_create( func,
633                                          impl,
634                                          channel,
[188]635                                          rx,          // direction
[5]636                                          base );
637
[492]638                    assert( (chdev != NULL),
[5]639                            "cannot allocate external device" );
640
641                    // make device type specific initialisation
642                    if     ( func == DEV_FUNC_IOB ) dev_iob_init( chdev );
643                    else if( func == DEV_FUNC_IOC ) dev_ioc_init( chdev );
644                    else if( func == DEV_FUNC_TXT ) dev_txt_init( chdev );
645                    else if( func == DEV_FUNC_NIC ) dev_nic_init( chdev );
[188]646                    else if( func == DEV_FUNC_FBF ) dev_fbf_init( chdev );
[5]647
[127]648                    // all external (shared) devices are remotely accessible
[5]649                    // initialize the replicated chdev_dir[x][y] structures
[127]650                    // defining the extended pointers on chdev descriptors
651                    xptr_t * entry;
652
[188]653                    if(func==DEV_FUNC_IOB             ) entry  = &chdev_dir.iob;
654                    if(func==DEV_FUNC_IOC             ) entry  = &chdev_dir.ioc[channel];
655                    if(func==DEV_FUNC_FBF             ) entry  = &chdev_dir.fbf[channel];
[407]656                    if((func==DEV_FUNC_TXT) && (rx==0)) entry  = &chdev_dir.txt_tx[channel];
657                    if((func==DEV_FUNC_TXT) && (rx==1)) entry  = &chdev_dir.txt_rx[channel];
[188]658                    if((func==DEV_FUNC_NIC) && (rx==0)) entry  = &chdev_dir.nic_tx[channel];
659                    if((func==DEV_FUNC_NIC) && (rx==1)) entry  = &chdev_dir.nic_rx[channel];
[127]660
[1]661                    for( x = 0 ; x < info->x_size ; x++ )
662                    {
[559]663                        for ( y = 0; y < info->y_size; y++ )
[1]664                        {
[559]665                            if (cluster_info_is_active(info->cluster_info[x][y])) {
666                                cxy_t  cxy = (x<<info->y_width) + y;
667                                hal_remote_swd( XPTR( cxy , entry ),
668                                                XPTR( local_cxy , chdev ) );
669                            }
[5]670                        }
[1]671                    }
672
[438]673#if( DEBUG_KERNEL_INIT & 0x1 )
674if( hal_time_stamp() > DEBUG_KERNEL_INIT )
[407]675printk("\n[DBG] %s : create chdev %s / channel = %d / rx = %d / cluster %x / chdev = %x\n",
676__FUNCTION__ , chdev_func_str( func ), channel , rx , local_cxy , chdev );
[389]677#endif
[5]678                }  // end if match
679
[19]680                // increment chdev global index (matching or not)
[188]681                ext_chdev_gid++;
[5]682
683            } // end loop on directions
684        }  // end loop on channels
[188]685        } // end loop on devices
686}  // end external_devices_init()
[5]687
[188]688///////////////////////////////////////////////////////////////////////////////////////////
689// This function is called by CP0 in cluster 0 to allocate memory and initialize the PIC
[407]690// device, namely the informations attached to the external IOPIC controller, that
691// must be replicated in all clusters (struct iopic_input).
[188]692// This initialisation must be done before other devices initialisation because the IRQ
[407]693// routing infrastructure is required for both internal and external devices init.
[188]694///////////////////////////////////////////////////////////////////////////////////////////
695// @ info    : pointer on the local boot-info structure.
696///////////////////////////////////////////////////////////////////////////////////////////
697static void iopic_init( boot_info_t * info )
698{
699    boot_device_t * dev_tbl;         // pointer on boot_info external devices array
700        uint32_t        dev_nr;          // actual number of external devices
701        xptr_t          base;            // remote pointer on segment base
702    uint32_t        func;            // device functionnal index
703    uint32_t        impl;            // device implementation index
704        uint32_t        i;               // device index in dev_tbl
705    uint32_t        x;               // cluster X coordinate
706    uint32_t        y;               // cluster Y coordinate
707    bool_t          found;           // IOPIC found
708        chdev_t       * chdev;           // pointer on PIC chdev descriptor
709
710    // get number of external peripherals and base of array from boot_info
711        dev_nr      = info->ext_dev_nr;
712    dev_tbl     = info->ext_dev;
713
714    // loop on external peripherals to get the IOPIC 
715        for( i = 0 , found = false ; i < dev_nr ; i++ )
716        {
717        func = FUNC_FROM_TYPE( dev_tbl[i].type );
718
[127]719        if( func == DEV_FUNC_PIC )
[1]720        {
[188]721            base     = dev_tbl[i].base;
722            impl     = IMPL_FROM_TYPE( dev_tbl[i].type );
723            found    = true;
724            break;
725        }
726    }
[5]727
[492]728    assert( found , "PIC device not found\n" );
[1]729
[407]730    // allocate and initialize the PIC chdev in cluster 0
731    chdev = chdev_create( DEV_FUNC_PIC,
[188]732                          impl,
733                          0,      // channel
734                          0,      // direction,
735                          base );
[5]736
[492]737    assert( (chdev != NULL), "no memory for PIC chdev\n" );
[5]738
[188]739    // make PIC device type specific initialisation
740    dev_pic_init( chdev );
[1]741
[407]742    // register, in all clusters, the extended pointer
743    // on PIC chdev in "chdev_dir" array
[188]744    xptr_t * entry = &chdev_dir.pic;   
745               
746    for( x = 0 ; x < info->x_size ; x++ )
747    {
[559]748        for ( y = 0; y < info->y_size; y++ )
[188]749        {
[559]750            if (cluster_info_is_active(info->cluster_info[x][y])) {
751                cxy_t  cxy = (x<<info->y_width) + y;
752                hal_remote_swd( XPTR( cxy , entry ) , 
753                                XPTR( local_cxy , chdev ) );
754            }
[188]755        }
756    }
[1]757
[407]758    // initialize, in all clusters, the "iopic_input" structure
[188]759    // defining how external IRQs are connected to IOPIC
760
[407]761    // register default value for unused inputs
762    for( x = 0 ; x < info->x_size ; x++ )
763    {
[559]764        for ( y = 0; y < info->y_size; y++ )
[407]765        {
[559]766            if (cluster_info_is_active(info->cluster_info[x][y])) {
767                cxy_t  cxy = (x<<info->y_width) + y;
768                hal_remote_memset( XPTR( cxy , &iopic_input ) , 0xFF , sizeof(iopic_input_t) );
769            }
[407]770        }
771    }
772
773    // register input IRQ index for valid inputs
774    uint32_t   id;         // input IRQ index
775    uint8_t    valid;      // input IRQ is connected
776    uint32_t   type;       // source device type
777    uint8_t    channel;    // source device channel
778    uint8_t    is_rx;      // source device direction
779    uint32_t * ptr;        // local pointer on one field in iopic_input stucture
780
[188]781    for( id = 0 ; id < CONFIG_MAX_EXTERNAL_IRQS ; id++ )
782    {
783        valid   = dev_tbl[i].irq[id].valid;
784        type    = dev_tbl[i].irq[id].dev_type;
785        channel = dev_tbl[i].irq[id].channel;
786        is_rx   = dev_tbl[i].irq[id].is_rx;
[407]787        func    = FUNC_FROM_TYPE( type );
[188]788
[407]789        // get pointer on relevant field in iopic_input
790        if( valid )
[188]791        {
[407]792            if     ( func == DEV_FUNC_IOC )                 ptr = &iopic_input.ioc[channel]; 
793            else if((func == DEV_FUNC_TXT) && (is_rx == 0)) ptr = &iopic_input.txt_tx[channel];
794            else if((func == DEV_FUNC_TXT) && (is_rx != 0)) ptr = &iopic_input.txt_rx[channel];
[492]795            else if((func == DEV_FUNC_NIC) && (is_rx == 0)) ptr = &iopic_input.nic_tx[channel];
796            else if((func == DEV_FUNC_NIC) && (is_rx != 0)) ptr = &iopic_input.nic_rx[channel];
797            else if( func == DEV_FUNC_IOB )                 ptr = &iopic_input.iob;
798            else     assert( false , "illegal source device for IOPIC input" );
[188]799
[407]800            // set one entry in all "iopic_input" structures
801            for( x = 0 ; x < info->x_size ; x++ )
802            {
[559]803                for ( y = 0; y < info->y_size; y++ )
[407]804                {
[559]805                    if (cluster_info_is_active(info->cluster_info[x][y])) {
806                        cxy_t  cxy = (x<<info->y_width) + y;
807                        hal_remote_swd( XPTR( cxy , ptr ) , id ); 
808                    }
[407]809                }
810            }
[188]811        }
812    } 
813
[438]814#if( DEBUG_KERNEL_INIT & 0x1 )
815if( hal_time_stamp() > DEBUG_KERNEL_INIT )
[407]816{
817    printk("\n[DBG] %s created PIC chdev in cluster %x at cycle %d\n",
818    __FUNCTION__ , local_cxy , (uint32_t)hal_time_stamp() );
819    dev_pic_inputs_display();
820}
[389]821#endif
[188]822   
823}  // end iopic_init()
824
[1]825///////////////////////////////////////////////////////////////////////////////////////////
[188]826// This function is called by all CP0s in all cluster to complete the PIC device
827// initialisation, namely the informations attached to the LAPIC controller.
828// This initialisation must be done after the IOPIC initialisation, but before other
829// devices initialisation because the IRQ routing infrastructure is required for both
830// internal and external devices initialisation.
831///////////////////////////////////////////////////////////////////////////////////////////
832// @ info    : pointer on the local boot-info structure.
833///////////////////////////////////////////////////////////////////////////////////////////
834static void lapic_init( boot_info_t * info )
835{
836    boot_device_t * dev_tbl;      // pointer on boot_info internal devices array
837    uint32_t        dev_nr;       // number of internal devices
838    uint32_t        i;            // device index in dev_tbl
839        xptr_t          base;         // remote pointer on segment base
840    uint32_t        func;         // device functionnal type in boot_info
841    bool_t          found;        // LAPIC found
842
843    // get number of internal peripherals and base
844        dev_nr      = info->int_dev_nr;
845    dev_tbl     = info->int_dev;
846
847    // loop on internal peripherals to get the lapic device
848        for( i = 0 , found = false ; i < dev_nr ; i++ )
849        {
850        func = FUNC_FROM_TYPE( dev_tbl[i].type );
851
852        if( func == DEV_FUNC_ICU )
853        {
854            base     = dev_tbl[i].base;
855            found    = true;
856            break;
857        }
858    }
859
860    // if the LAPIC controller is not defined in the boot_info,
861    // we simply don't initialize the PIC extensions in the kernel,
862    // making the assumption that the LAPIC related informations
863    // are hidden in the hardware specific PIC driver.
864    if( found )
865    {
866        // initialise the PIC extensions for
867        // the core descriptor and core manager extensions
868        dev_pic_extend_init( (uint32_t *)GET_PTR( base ) );
869
870        // initialize the "lapic_input" structure
871        // defining how internal IRQs are connected to LAPIC
872        uint32_t        id;
873        uint8_t         valid;
874        uint8_t         channel;
875        uint32_t        func;
876
877        for( id = 0 ; id < CONFIG_MAX_INTERNAL_IRQS ; id++ )
878        {
879            valid    = dev_tbl[i].irq[id].valid;
880            func     = FUNC_FROM_TYPE( dev_tbl[i].irq[id].dev_type );
881            channel  = dev_tbl[i].irq[id].channel;
882
883            if( valid ) // only valid local IRQs are registered
884            {
885                if     ( func == DEV_FUNC_MMC ) lapic_input.mmc = id;
886                else if( func == DEV_FUNC_DMA ) lapic_input.dma[channel] = id;
[534]887                else if( func == DEV_FUNC_TXT ) lapic_input.mtty = id;
[550]888                else if( func == DEV_FUNC_IOC ) lapic_input.sdcard = id;
[492]889                else assert( false , "illegal source device for LAPIC input" );
[188]890            }
891        }
892    }
893}  // end lapic_init()
894
895///////////////////////////////////////////////////////////////////////////////////////////
[14]896// This static function returns the identifiers of the calling core.
897///////////////////////////////////////////////////////////////////////////////////////////
898// @ info    : pointer on boot_info structure.
899// @ lid     : [out] core local index in cluster.
900// @ cxy     : [out] cluster identifier.
901// @ lid     : [out] core global identifier (hardware).
902// @ return 0 if success / return EINVAL if not found.
903///////////////////////////////////////////////////////////////////////////////////////////
[23]904static error_t get_core_identifiers( boot_info_t * info,
905                                     lid_t       * lid,
[14]906                                     cxy_t       * cxy,
907                                     gid_t       * gid )
908{
[127]909    uint32_t   i;
[14]910    gid_t      global_id;
[19]911
[14]912    // get global identifier from hardware register
[127]913    global_id = hal_get_gid();
[14]914
915    // makes an associative search in boot_info to get (cxy,lid) from global_id
916    for( i = 0 ; i < info->cores_nr ; i++ )
917    {
918        if( global_id == info->core[i].gid )
919        {
920            *lid = info->core[i].lid;
921            *cxy = info->core[i].cxy;
922            *gid = global_id;
923            return 0;
924        }
925    }
926    return EINVAL;
[19]927}
[14]928
929///////////////////////////////////////////////////////////////////////////////////////////
[1]930// This function is the entry point for the kernel initialisation.
[19]931// It is executed by all cores in all clusters, but only core[0], called CP0,
[14]932// initializes the shared resources such as the cluster manager, or the local peripherals.
[19]933// To comply with the multi-kernels paradigm, it accesses only local cluster memory, using
934// only information contained in the local boot_info_t structure, set by the bootloader.
[103]935// Only CP0 in cluster 0 print the log messages.
[1]936///////////////////////////////////////////////////////////////////////////////////////////
937// @ info    : pointer on the local boot-info structure.
938///////////////////////////////////////////////////////////////////////////////////////////
939void kernel_init( boot_info_t * info )
940{
[204]941    lid_t        core_lid = -1;             // running core local index
942    cxy_t        core_cxy = -1;             // running core cluster identifier
943    gid_t        core_gid;                  // running core hardware identifier
944    cluster_t  * cluster;                   // pointer on local cluster manager
945    core_t     * core;                      // pointer on running core descriptor
946    thread_t   * thread;                    // pointer on idle thread descriptor
947
948    xptr_t       vfs_root_inode_xp;         // extended pointer on VFS root inode
949    xptr_t       devfs_dev_inode_xp;        // extended pointer on DEVFS dev inode   
950    xptr_t       devfs_external_inode_xp;   // extended pointer on DEVFS external inode       
951    xptr_t       devfs_internal_inode_xp;   // extended pointer on DEVFS internal inode       
952
[1]953    error_t      error;
[285]954    reg_t        status;                    // running core status register
[1]955
[188]956    /////////////////////////////////////////////////////////////////////////////////
957    // STEP 0 : Each core get its core identifier from boot_info, and makes
958    //          a partial initialisation of its private idle thread descriptor.
959    //          CP0 initializes the "local_cxy" global variable.
960    //          CP0 in cluster IO initializes the TXT0 chdev to print log messages.
961    /////////////////////////////////////////////////////////////////////////////////
962
[23]963    error = get_core_identifiers( info,
[14]964                                  &core_lid,
965                                  &core_cxy,
966                                  &core_gid );
[1]967
[127]968    // CP0 initializes cluster identifier
[14]969    if( core_lid == 0 ) local_cxy = info->cxy;
[1]970
[127]971    // each core gets a pointer on its private idle thread descriptor
972    thread = (thread_t *)( idle_threads + (core_lid * CONFIG_THREAD_DESC_SIZE) );
[68]973
[127]974    // each core registers this thread pointer in hardware register
[68]975    hal_set_current_thread( thread );
[71]976
[407]977    // each core register core descriptor pointer in idle thread descriptor
978    thread->core = &LOCAL_CLUSTER->core_tbl[core_lid];
979
[437]980    // each core initializes the idle thread lists of locks
[124]981    list_root_init( &thread->locks_root );
[188]982    xlist_root_init( XPTR( local_cxy , &thread->xlocks_root ) );
[437]983    thread->local_locks = 0;
984    thread->remote_locks = 0;
[124]985
[528]986    // CP0 in cluster 0 initializes TXT0 chdev descriptor
[535]987    if( core_cxy == 0 && core_lid == 0 ) // [MODIF]
988    {
[556]989        if( USE_TXT_MTY == 1 ) {
[535]990            mtty0_device_init( info );
991        } else {
992            txt0_device_init( info );
993        }
994    }
[14]995
996    /////////////////////////////////////////////////////////////////////////////////
[528]997    if( core_lid == 0 ) remote_barrier( XPTR( 0 , &global_barrier ), // [FIXME]
[560]998                                        cluster_info_nb_actives(info->cluster_info) );
[14]999    barrier_wait( &local_barrier , info->cores_nr );
[437]1000    /////////////////////////////////////////////////////////////////////////////////
[14]1001
[438]1002#if DEBUG_KERNEL_INIT
1003if( (core_lid ==  0) & (local_cxy == 0) ) 
[437]1004printk("\n[DBG] %s : exit barrier 0 : TXT0 initialized / cycle %d\n",
1005__FUNCTION__, (uint32_t)hal_get_cycles() );
1006#endif
[14]1007
[188]1008    /////////////////////////////////////////////////////////////////////////////
[407]1009    // STEP 1 : all cores check core identifier.
[188]1010    //          CP0 initializes the local cluster manager.
1011    //          This includes the memory allocators.
1012    /////////////////////////////////////////////////////////////////////////////
1013
1014    // all cores check identifiers
[14]1015    if( error )
[1]1016    {
[492]1017        assert( false ,
[428]1018        "illegal core identifiers gid = %x / cxy = %x / lid = %d",
1019        core_lid , core_cxy , core_lid );
[1]1020    }
1021
[188]1022    // CP0 initializes cluster manager
[14]1023    if( core_lid == 0 )
[1]1024    {
1025        error = cluster_init( info );
1026
[14]1027        if( error )
1028        {
[492]1029            assert( false ,
[428]1030            "cannot initialise cluster %x", local_cxy );
[14]1031        }
1032    }
[5]1033
[14]1034    /////////////////////////////////////////////////////////////////////////////////
[528]1035    if( core_lid == 0 ) remote_barrier( XPTR( 0 , &global_barrier ), // [FIXME]
[560]1036                                        cluster_info_nb_actives(info->cluster_info) );
[14]1037    barrier_wait( &local_barrier , info->cores_nr );
1038    /////////////////////////////////////////////////////////////////////////////////
[1]1039
[438]1040#if DEBUG_KERNEL_INIT
1041if( (core_lid ==  0) & (local_cxy == 0) ) 
[437]1042printk("\n[DBG] %s : exit barrier 1 : clusters initialised / cycle %d\n",
1043__FUNCTION__, (uint32_t)hal_get_cycles() );
1044#endif
[1]1045
[188]1046    /////////////////////////////////////////////////////////////////////////////////
[407]1047    // STEP 2 : CP0 initializes the process_zero descriptor.
[296]1048    //          CP0 in cluster 0 initializes the IOPIC device.
[188]1049    /////////////////////////////////////////////////////////////////////////////////
1050
1051    // all cores get pointer on local cluster manager & core descriptor
[14]1052    cluster = &cluster_manager;
[127]1053    core    = &cluster->core_tbl[core_lid];
[1]1054
[188]1055    // all CP0s initialize the process_zero descriptor
[428]1056    if( core_lid == 0 ) process_zero_create( &process_zero );
[5]1057
[188]1058    // CP0 in cluster 0 initializes the PIC chdev,
1059    if( (core_lid == 0) && (local_cxy == 0) ) iopic_init( info );
1060   
1061    ////////////////////////////////////////////////////////////////////////////////
[528]1062    if( core_lid == 0 ) remote_barrier( XPTR( 0 , &global_barrier ), // [FIXME]
[560]1063                                        cluster_info_nb_actives(info->cluster_info) );
[188]1064    barrier_wait( &local_barrier , info->cores_nr );
1065    ////////////////////////////////////////////////////////////////////////////////
[127]1066
[438]1067#if DEBUG_KERNEL_INIT
1068if( (core_lid ==  0) & (local_cxy == 0) ) 
[437]1069printk("\n[DBG] %s : exit barrier 2 : PIC initialised / cycle %d\n",
1070__FUNCTION__, (uint32_t)hal_get_cycles() );
1071#endif
[1]1072
[188]1073    ////////////////////////////////////////////////////////////////////////////////
[407]1074    // STEP 3 : CP0 initializes the distibuted LAPIC descriptor.
1075    //          CP0 initializes the internal chdev descriptors
1076    //          CP0 initialize the local external chdev descriptors
[188]1077    ////////////////////////////////////////////////////////////////////////////////
[5]1078
[279]1079    // all CP0s initialize their local LAPIC extension,
1080    if( core_lid == 0 ) lapic_init( info );
1081
[188]1082    // CP0 scan the internal (private) peripherals,
1083    // and allocates memory for the corresponding chdev descriptors.
1084    if( core_lid == 0 ) internal_devices_init( info );
1085       
[1]1086
[50]1087    // All CP0s contribute to initialise external peripheral chdev descriptors.
[14]1088    // Each CP0[cxy] scan the set of external (shared) peripherals (but the TXT0),
1089    // and allocates memory for the chdev descriptors that must be placed
[127]1090    // on the (cxy) cluster according to the global index value.
[188]1091
[14]1092    if( core_lid == 0 ) external_devices_init( info );
[1]1093
[14]1094    /////////////////////////////////////////////////////////////////////////////////
[528]1095    if( core_lid == 0 ) remote_barrier( XPTR( 0 , &global_barrier ), // [FIXME]
[560]1096                                        cluster_info_nb_actives(info->cluster_info) );
[14]1097    barrier_wait( &local_barrier , info->cores_nr );
1098    /////////////////////////////////////////////////////////////////////////////////
[5]1099
[438]1100#if DEBUG_KERNEL_INIT
1101if( (core_lid ==  0) & (local_cxy == 0) ) 
[437]1102printk("\n[DBG] %s : exit barrier 3 : all chdev initialised / cycle %d\n",
1103__FUNCTION__, (uint32_t)hal_get_cycles() );
1104#endif
[1]1105
[438]1106#if( DEBUG_KERNEL_INIT & 1 )
[443]1107if( (core_lid ==  0) & (local_cxy == 0) ) 
[437]1108chdev_dir_display();
1109#endif
1110   
[188]1111    /////////////////////////////////////////////////////////////////////////////////
[279]1112    // STEP 4 : All cores enable IPI (Inter Procesor Interrupt),
1113    //          Alh cores initialize IDLE thread.
[188]1114    //          Only CP0 in cluster 0 creates the VFS root inode.
1115    //          It access the boot device to initialize the file system context.
1116    /////////////////////////////////////////////////////////////////////////////////
1117
[279]1118    // All cores enable the shared IPI channel
1119    dev_pic_enable_ipi();
1120    hal_enable_irq( &status );
1121
[532]1122#if DEBUG_KERNEL_INIT
1123printk("\n[DBG] %s: IPI enabled for core %d cluster %d\n", __FUNCTION__,
1124  core_lid, local_cxy);
1125#endif
1126
[296]1127    // all cores initialize the idle thread descriptor
[457]1128    thread_idle_init( thread,
1129                      THREAD_IDLE,
1130                      &thread_idle_func,
1131                      NULL,
1132                      core_lid );
[1]1133
[296]1134    // all cores unblock idle thread, and register it in scheduler
1135    thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_GLOBAL );
[103]1136    core->scheduler.idle = thread;
[1]1137
[438]1138#if( DEBUG_KERNEL_INIT & 1 )
[407]1139sched_display( core_lid );
[389]1140#endif
[14]1141
[188]1142    // CPO in cluster 0 creates the VFS root
1143    if( (core_lid ==  0) && (local_cxy == 0 ) ) 
[14]1144    {
[188]1145        vfs_root_inode_xp = XPTR_NULL;
[23]1146
[188]1147        // File System must be FATFS in this implementation,
1148        // but other File System can be introduced here
[23]1149        if( CONFIG_VFS_ROOT_IS_FATFS )
1150        {
[389]1151            // 1. allocate memory for FATFS context in cluster 0
[188]1152            fatfs_ctx_t * fatfs_ctx = fatfs_ctx_alloc();
1153
[492]1154            assert( (fatfs_ctx != NULL) ,
[279]1155                    "cannot create FATFS context in cluster 0\n" );
[188]1156
1157            // 2. access boot device to initialize FATFS context
1158            fatfs_ctx_init( fatfs_ctx );
1159 
1160            // 3. get various informations from FATFS context
1161            uint32_t root_dir_cluster = fatfs_ctx->root_dir_cluster;
1162            uint32_t cluster_size     = fatfs_ctx->bytes_per_sector * 
1163                                        fatfs_ctx->sectors_per_cluster;
1164            uint32_t total_clusters   = fatfs_ctx->fat_sectors_count << 7;
1165 
1166            // 4. create VFS root inode in cluster 0
1167            error = vfs_inode_create( XPTR_NULL,                           // dentry_xp
1168                                      FS_TYPE_FATFS,                       // fs_type
1169                                      INODE_TYPE_DIR,                      // inode_type
1170                                      (void *)(intptr_t)root_dir_cluster,  // extend
1171                                      0,                                   // attr
1172                                      0,                                   // rights
1173                                      0,                                   // uid
1174                                      0,                                   // gid
1175                                      &vfs_root_inode_xp );                // return
1176
[492]1177            assert( (error == 0) ,
[279]1178                    "cannot create VFS root inode\n" );
[188]1179
1180            // 5. initialize VFS context for FAT in cluster 0
1181            vfs_ctx_init( FS_TYPE_FATFS,                 // file system type
1182                          0,                             // attributes
1183                              total_clusters,               
1184                              cluster_size,
1185                              vfs_root_inode_xp,             // VFS root
1186                          fatfs_ctx );                   // extend
[389]1187
1188            // 6. check initialisation
1189            vfs_ctx_t   * vfs_ctx = &fs_context[FS_TYPE_FATFS];
1190            assert( (((fatfs_ctx_t *)vfs_ctx->extend)->sectors_per_cluster == 8),
[492]1191             "illegal value for FATFS context in cluster %x\n", local_cxy );
[23]1192        }
1193        else
1194        {
[492]1195            assert( false ,
[428]1196            "root FS must be FATFS" );
[23]1197        }
1198
[389]1199        // register VFS root inode in process_zero descriptor of cluster 0
[188]1200        process_zero.vfs_root_xp = vfs_root_inode_xp;
1201        process_zero.vfs_cwd_xp  = vfs_root_inode_xp;
1202    }
1203
1204    /////////////////////////////////////////////////////////////////////////////////
[528]1205    if( core_lid == 0 ) remote_barrier( XPTR( 0 , &global_barrier ), // [FIXME]
[560]1206                                        cluster_info_nb_actives(info->cluster_info) );
[188]1207    barrier_wait( &local_barrier , info->cores_nr );
1208    /////////////////////////////////////////////////////////////////////////////////
1209
[438]1210#if DEBUG_KERNEL_INIT
1211if( (core_lid ==  0) & (local_cxy == 0) ) 
[437]1212printk("\n[DBG] %s : exit barrier 4 : VFS_root = %l in cluster 0 / cycle %d\n",
1213__FUNCTION__, vfs_root_inode_xp , (uint32_t)hal_get_cycles());
1214#endif
[188]1215
1216    /////////////////////////////////////////////////////////////////////////////////
1217    // STEP 5 : Other CP0s allocate memory for the selected FS context,
1218    //          and initialise both the local FS context and the local VFS context
1219    //          from values stored in cluster 0.
1220    //          They get the VFS root inode extended pointer from cluster 0.
1221    /////////////////////////////////////////////////////////////////////////////////
1222
1223    if( (core_lid ==  0) && (local_cxy != 0) ) 
1224    {
1225        // File System must be FATFS in this implementation,
1226        // but other File System can be introduced here
1227        if( CONFIG_VFS_ROOT_IS_FATFS )
[23]1228        {
[389]1229            // 1. allocate memory for local FATFS context
1230            fatfs_ctx_t * local_fatfs_ctx = fatfs_ctx_alloc();
[188]1231
[492]1232            assert( (local_fatfs_ctx != NULL) ,
[389]1233            "cannot create FATFS context in cluster %x\n", local_cxy );
[188]1234
[389]1235            // 2. get local pointer on VFS context for FATFS
[188]1236            vfs_ctx_t   * vfs_ctx = &fs_context[FS_TYPE_FATFS];
1237
[389]1238            // 3. get local pointer on FATFS context in cluster 0
1239            fatfs_ctx_t * remote_fatfs_ctx = hal_remote_lpt( XPTR( 0 , &vfs_ctx->extend ) );
1240
1241            // 4. copy FATFS context from cluster 0 to local cluster
1242            hal_remote_memcpy( XPTR( local_cxy , local_fatfs_ctx ), 
1243                               XPTR( 0 ,         remote_fatfs_ctx ), sizeof(fatfs_ctx_t) );
1244
1245            // 5. copy VFS context from cluster 0 to local cluster
[188]1246            hal_remote_memcpy( XPTR( local_cxy , vfs_ctx ), 
[389]1247                               XPTR( 0 ,         vfs_ctx ), sizeof(vfs_ctx_t) );
[188]1248
[389]1249            // 6. update extend field in local copy of VFS context
1250            vfs_ctx->extend = local_fatfs_ctx;
[188]1251
[389]1252            // 7. check initialisation
1253            assert( (((fatfs_ctx_t *)vfs_ctx->extend)->sectors_per_cluster == 8),
[492]1254            "illegal value for FATFS context in cluster %x\n", local_cxy );
[23]1255        }
1256
[188]1257        // get extended pointer on VFS root inode from cluster 0
[296]1258        vfs_root_inode_xp = hal_remote_lwd( XPTR( 0 , &process_zero.vfs_root_xp ) );
[101]1259
[188]1260        // update local process_zero descriptor
1261        process_zero.vfs_root_xp = vfs_root_inode_xp;
1262        process_zero.vfs_cwd_xp  = vfs_root_inode_xp;
[14]1263    }
1264
[188]1265    /////////////////////////////////////////////////////////////////////////////////
[528]1266    if( core_lid == 0 ) remote_barrier( XPTR( 0 , &global_barrier ), // [FIXME]
[560]1267                                        cluster_info_nb_actives(info->cluster_info) );
[188]1268    barrier_wait( &local_barrier , info->cores_nr );
[204]1269    /////////////////////////////////////////////////////////////////////////////////
[101]1270
[438]1271#if DEBUG_KERNEL_INIT
1272if( (core_lid ==  0) & (local_cxy == 0) ) 
[457]1273printk("\n[DBG] %s : exit barrier 5 : VFS_root = %l in cluster 0 / cycle %d\n",
1274__FUNCTION__, vfs_root_inode_xp , (uint32_t)hal_get_cycles());
[437]1275#endif
[188]1276
1277    /////////////////////////////////////////////////////////////////////////////////
1278    // STEP 6 : CP0 in cluster IO makes the global DEVFS tree initialisation:
[204]1279    //          It creates the DEVFS directory "dev", and the DEVFS "external"
1280    //          directory in cluster IO and mount these inodes into VFS.
[188]1281    /////////////////////////////////////////////////////////////////////////////////
1282
[528]1283    if( (core_lid ==  0) && (local_cxy == 0) )  // [FIXME]
[1]1284    {
[188]1285        // create "dev" and "external" directories.
1286        devfs_global_init( process_zero.vfs_root_xp,
[204]1287                           &devfs_dev_inode_xp,
[188]1288                           &devfs_external_inode_xp );
1289
1290        // creates the DEVFS context in cluster IO
1291        devfs_ctx_t * devfs_ctx = devfs_ctx_alloc();
1292
[492]1293        assert( (devfs_ctx != NULL) ,
[279]1294                "cannot create DEVFS context in cluster IO\n");
[188]1295
1296        // register DEVFS root and external directories
[204]1297        devfs_ctx_init( devfs_ctx, devfs_dev_inode_xp, devfs_external_inode_xp );
[188]1298    }   
1299
1300    /////////////////////////////////////////////////////////////////////////////////
[528]1301    if( core_lid == 0 ) remote_barrier( XPTR( 0 , &global_barrier ), // [FIXME]
[560]1302                                        cluster_info_nb_actives(info->cluster_info) );
[188]1303    barrier_wait( &local_barrier , info->cores_nr );
[204]1304    /////////////////////////////////////////////////////////////////////////////////
[188]1305
[438]1306#if DEBUG_KERNEL_INIT
1307if( (core_lid ==  0) & (local_cxy == 0) ) 
[457]1308printk("\n[DBG] %s : exit barrier 6 : dev_root = %l in cluster 0 / cycle %d\n",
1309__FUNCTION__, devfs_dev_inode_xp , (uint32_t)hal_get_cycles() );
[437]1310#endif
[188]1311
1312    /////////////////////////////////////////////////////////////////////////////////
1313    // STEP 7 : All CP0s complete in parallel the DEVFS tree initialization.
1314    //          Each CP0 get the "dev" and "external" extended pointers from
[204]1315    //          values stored in cluster IO.
[337]1316    //          Then each CP0 in cluster(i) creates the DEVFS "internal directory,
[204]1317    //          and creates the pseudo-files for all chdevs in cluster (i).
[188]1318    /////////////////////////////////////////////////////////////////////////////////
1319
1320    if( core_lid == 0 )
1321    {
[528]1322        // get extended pointer on "extend" field of VFS context for DEVFS in cluster IO
1323        xptr_t  extend_xp = XPTR( 0 , &fs_context[FS_TYPE_DEVFS].extend ); // [FIXME]
[188]1324
[457]1325        // get pointer on DEVFS context in cluster 0
[188]1326        devfs_ctx_t * devfs_ctx = hal_remote_lpt( extend_xp );
1327       
[457]1328        devfs_dev_inode_xp      = hal_remote_lwd( XPTR( 0 , &devfs_ctx->dev_inode_xp ) );
1329        devfs_external_inode_xp = hal_remote_lwd( XPTR( 0 , &devfs_ctx->external_inode_xp ) );
[188]1330
[204]1331        // populate DEVFS in all clusters
1332        devfs_local_init( devfs_dev_inode_xp,
1333                          devfs_external_inode_xp,
1334                          &devfs_internal_inode_xp );
[188]1335    }
1336
1337    /////////////////////////////////////////////////////////////////////////////////
[528]1338    if( core_lid == 0 ) remote_barrier( XPTR( 0 , &global_barrier ), // [FIXME]
[560]1339                                        cluster_info_nb_actives(info->cluster_info) );
[188]1340    barrier_wait( &local_barrier , info->cores_nr );
[204]1341    /////////////////////////////////////////////////////////////////////////////////
[188]1342
[438]1343#if DEBUG_KERNEL_INIT
1344if( (core_lid ==  0) & (local_cxy == 0) ) 
[437]1345printk("\n[DBG] %s : exit barrier 7 : dev_root = %l in cluster 0 / cycle %d\n",
1346__FUNCTION__, devfs_dev_inode_xp , (uint32_t)hal_get_cycles() );
1347#endif
[188]1348
1349    /////////////////////////////////////////////////////////////////////////////////
[428]1350    // STEP 8 : CP0 in cluster 0 creates the first user process (process_init)
[188]1351    /////////////////////////////////////////////////////////////////////////////////
1352
[457]1353    if( (core_lid == 0) && (local_cxy == 0) ) 
[188]1354    {
[428]1355
[438]1356#if( DEBUG_KERNEL_INIT & 1 )
[428]1357vfs_display( vfs_root_inode_xp );
1358#endif
1359
1360       process_init_create();
[188]1361    }
[101]1362
[188]1363    /////////////////////////////////////////////////////////////////////////////////
[528]1364    if( core_lid == 0 ) remote_barrier( XPTR( 0 , &global_barrier ), // [FIXME]
[560]1365                                        cluster_info_nb_actives(info->cluster_info) );
[188]1366    barrier_wait( &local_barrier , info->cores_nr );
[204]1367    /////////////////////////////////////////////////////////////////////////////////
[188]1368
[438]1369#if DEBUG_KERNEL_INIT
1370if( (core_lid ==  0) & (local_cxy == 0) ) 
[437]1371printk("\n[DBG] %s : exit barrier 8 : process init created / cycle %d\n", 
1372__FUNCTION__ , (uint32_t)hal_get_cycles() );
1373#endif
[188]1374
[443]1375#if (DEBUG_KERNEL_INIT & 1)
[556]1376if( (core_lid ==  0) /*& (local_cxy == 0)*/ ) 
[443]1377sched_display( 0 );
1378#endif
1379
[188]1380    /////////////////////////////////////////////////////////////////////////////////
1381    // STEP 9 : CP0 in cluster 0 print banner
1382    /////////////////////////////////////////////////////////////////////////////////
1383   
[528]1384    if( (core_lid ==  0) && (local_cxy == 0) ) // [FIXME]
[188]1385    {
[5]1386        print_banner( (info->x_size * info->y_size) , info->cores_nr );
[68]1387
[438]1388#if( DEBUG_KERNEL_INIT & 1 )
[437]1389printk("\n\n***** memory fooprint for main kernel objects\n\n"
[68]1390                   " - thread descriptor  : %d bytes\n"
1391                   " - process descriptor : %d bytes\n"
1392                   " - cluster manager    : %d bytes\n"
1393                   " - chdev descriptor   : %d bytes\n"
1394                   " - core descriptor    : %d bytes\n"
1395                   " - scheduler          : %d bytes\n"
1396                   " - rpc fifo           : %d bytes\n"
1397                   " - page descriptor    : %d bytes\n"
1398                   " - mapper root        : %d bytes\n"
1399                   " - ppm manager        : %d bytes\n"
1400                   " - kcm manager        : %d bytes\n"
1401                   " - khm manager        : %d bytes\n"
1402                   " - vmm manager        : %d bytes\n"
1403                   " - gpt root           : %d bytes\n"
1404                   " - list item          : %d bytes\n"
1405                   " - xlist item         : %d bytes\n"
1406                   " - spinlock           : %d bytes\n"
1407                   " - remote spinlock    : %d bytes\n"
1408                   " - rwlock             : %d bytes\n"
1409                   " - remote rwlock      : %d bytes\n",
[127]1410                   sizeof( thread_t          ),
[68]1411                   sizeof( process_t         ),
1412                   sizeof( cluster_t         ),
1413                   sizeof( chdev_t           ),
1414                   sizeof( core_t            ),
1415                   sizeof( scheduler_t       ),
[407]1416                   sizeof( remote_fifo_t     ),
[68]1417                   sizeof( page_t            ),
1418                   sizeof( mapper_t          ),
1419                   sizeof( ppm_t             ),
1420                   sizeof( kcm_t             ),
1421                   sizeof( khm_t             ),
1422                   sizeof( vmm_t             ),
1423                   sizeof( gpt_t             ),
1424                   sizeof( list_entry_t      ),
1425                   sizeof( xlist_entry_t     ),
1426                   sizeof( spinlock_t        ),
1427                   sizeof( remote_spinlock_t ),
1428                   sizeof( rwlock_t          ),
1429                   sizeof( remote_rwlock_t   ));
[406]1430#endif
1431
[1]1432    }
1433
[398]1434    // each core activates its private TICK IRQ
1435    dev_pic_enable_timer( CONFIG_SCHED_TICK_MS_PERIOD );
[14]1436
[440]1437#if DEBUG_KERNEL_INIT
1438printk("\n[DBG] %s : thread %x on core[%x,%d] jumps to thread_idle_func() / cycle %d\n",
1439__FUNCTION__ , CURRENT_THREAD , local_cxy , core_lid , (uint32_t)hal_get_cycles() );
1440#endif
1441
[407]1442    // each core jump to thread_idle_func
[50]1443    thread_idle_func();
[127]1444}
[14]1445
Note: See TracBrowser for help on using the repository browser.