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

Last change on this file since 582 was 582, checked in by alain, 6 years ago

New DQDT implementation supporting missing clusters
thanks to the cluster_info[x][y] array.

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