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

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

Gather LETI-specific macros into hard_config.h

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