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

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

Remove y_max in kernel init barriers

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