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

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

Add SD card driver in kernel

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