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

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

TTY MUX 1 : Init tty chdevs in internal_devices_init()

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