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

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

Remove y_max in all functions except dqdt_init()

File size: 61.8 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;
624                uint32_t cx;
625                uint32_t cy;
626                uint32_t target_cxy;
627                while (1) {
628                    offset     = ext_chdev_gid % ( info->x_size * (info->y_size) );
629                    cx         = offset / (info->y_size);
630                    cy         = offset % (info->y_size);
631                    target_cxy = (cx<<info->y_width) + cy;
632                    // ext_chdev_gid that results in empty target clusters are skipped
633                    if ( cluster_info_is_active( LOCAL_CLUSTER->cluster_info[cx][cy] ) == 0 ) {
634                        ext_chdev_gid++;
635                    } else { // The ext_chdev_gid resulted in a full target cluster
636                        break;
637                    }
638                }
639                // allocate and initialize a local chdev
640                // when local cluster matches target cluster
641                if( target_cxy == local_cxy )
642                {
643                    chdev = chdev_create( func,
644                                          impl,
645                                          channel,
646                                          rx,          // direction
647                                          base );
648
649                    assert( (chdev != NULL),
650                            "cannot allocate external device" );
651
652                    // make device type specific initialisation
653                    if     ( func == DEV_FUNC_IOB ) dev_iob_init( chdev );
654                    else if( func == DEV_FUNC_IOC ) dev_ioc_init( chdev );
655                    else if( func == DEV_FUNC_TXT ) dev_txt_init( chdev );
656                    else if( func == DEV_FUNC_NIC ) dev_nic_init( chdev );
657                    else if( func == DEV_FUNC_FBF ) dev_fbf_init( chdev );
658
659                    // all external (shared) devices are remotely accessible
660                    // initialize the replicated chdev_dir[x][y] structures
661                    // defining the extended pointers on chdev descriptors
662                    xptr_t * entry;
663
664                    if(func==DEV_FUNC_IOB             ) entry  = &chdev_dir.iob;
665                    if(func==DEV_FUNC_IOC             ) entry  = &chdev_dir.ioc[channel];
666                    if(func==DEV_FUNC_FBF             ) entry  = &chdev_dir.fbf[channel];
667                    if((func==DEV_FUNC_TXT) && (rx==0)) entry  = &chdev_dir.txt_tx[channel];
668                    if((func==DEV_FUNC_TXT) && (rx==1)) entry  = &chdev_dir.txt_rx[channel];
669                    if((func==DEV_FUNC_NIC) && (rx==0)) entry  = &chdev_dir.nic_tx[channel];
670                    if((func==DEV_FUNC_NIC) && (rx==1)) entry  = &chdev_dir.nic_rx[channel];
671
672                    for( x = 0 ; x < info->x_size ; x++ )
673                    {
674                        for ( y = 0; y < info->y_size; y++ )
675                        {
676                            if (cluster_info_is_active(info->cluster_info[x][y])) {
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
684#if( DEBUG_KERNEL_INIT & 0x1 )
685if( hal_time_stamp() > DEBUG_KERNEL_INIT )
686printk("\n[DBG] %s : create chdev %s / channel = %d / rx = %d / cluster %x / chdev = %x\n",
687__FUNCTION__ , chdev_func_str( func ), channel , rx , local_cxy , chdev );
688#endif
689                }  // end if match
690
691                // increment chdev global index (matching or not)
692                ext_chdev_gid++;
693
694            } // end loop on directions
695        }  // end loop on channels
696        } // end loop on devices
697}  // end external_devices_init()
698
699///////////////////////////////////////////////////////////////////////////////////////////
700// This function is called by CP0 in cluster 0 to allocate memory and initialize the PIC
701// device, namely the informations attached to the external IOPIC controller, that
702// must be replicated in all clusters (struct iopic_input).
703// This initialisation must be done before other devices initialisation because the IRQ
704// routing infrastructure is required for both internal and external devices init.
705///////////////////////////////////////////////////////////////////////////////////////////
706// @ info    : pointer on the local boot-info structure.
707///////////////////////////////////////////////////////////////////////////////////////////
708static void iopic_init( boot_info_t * info )
709{
710    boot_device_t * dev_tbl;         // pointer on boot_info external devices array
711        uint32_t        dev_nr;          // actual number of external devices
712        xptr_t          base;            // remote pointer on segment base
713    uint32_t        func;            // device functionnal index
714    uint32_t        impl;            // device implementation index
715        uint32_t        i;               // device index in dev_tbl
716    uint32_t        x;               // cluster X coordinate
717    uint32_t        y;               // cluster Y coordinate
718    bool_t          found;           // IOPIC found
719        chdev_t       * chdev;           // pointer on PIC chdev descriptor
720
721    // get number of external peripherals and base of array from boot_info
722        dev_nr      = info->ext_dev_nr;
723    dev_tbl     = info->ext_dev;
724
725    // loop on external peripherals to get the IOPIC 
726        for( i = 0 , found = false ; i < dev_nr ; i++ )
727        {
728        func = FUNC_FROM_TYPE( dev_tbl[i].type );
729
730        if( func == DEV_FUNC_PIC )
731        {
732            base     = dev_tbl[i].base;
733            impl     = IMPL_FROM_TYPE( dev_tbl[i].type );
734            found    = true;
735            break;
736        }
737    }
738
739    assert( found , "PIC device not found\n" );
740
741    // allocate and initialize the PIC chdev in cluster 0
742    chdev = chdev_create( DEV_FUNC_PIC,
743                          impl,
744                          0,      // channel
745                          0,      // direction,
746                          base );
747
748    assert( (chdev != NULL), "no memory for PIC chdev\n" );
749
750    // make PIC device type specific initialisation
751    dev_pic_init( chdev );
752
753    // register, in all clusters, the extended pointer
754    // on PIC chdev in "chdev_dir" array
755    xptr_t * entry = &chdev_dir.pic;   
756               
757    for( x = 0 ; x < info->x_size ; x++ )
758    {
759        for ( y = 0; y < info->y_size; y++ )
760        {
761            if (cluster_info_is_active(info->cluster_info[x][y])) {
762                cxy_t  cxy = (x<<info->y_width) + y;
763                hal_remote_swd( XPTR( cxy , entry ) , 
764                                XPTR( local_cxy , chdev ) );
765            }
766        }
767    }
768
769    // initialize, in all clusters, the "iopic_input" structure
770    // defining how external IRQs are connected to IOPIC
771
772    // register default value for unused inputs
773    for( x = 0 ; x < info->x_size ; x++ )
774    {
775        for ( y = 0; y < info->y_size; y++ )
776        {
777            if (cluster_info_is_active(info->cluster_info[x][y])) {
778                cxy_t  cxy = (x<<info->y_width) + y;
779                hal_remote_memset( XPTR( cxy , &iopic_input ) , 0xFF , sizeof(iopic_input_t) );
780            }
781        }
782    }
783
784    // register input IRQ index for valid inputs
785    uint32_t   id;         // input IRQ index
786    uint8_t    valid;      // input IRQ is connected
787    uint32_t   type;       // source device type
788    uint8_t    channel;    // source device channel
789    uint8_t    is_rx;      // source device direction
790    uint32_t * ptr;        // local pointer on one field in iopic_input stucture
791
792    for( id = 0 ; id < CONFIG_MAX_EXTERNAL_IRQS ; id++ )
793    {
794        valid   = dev_tbl[i].irq[id].valid;
795        type    = dev_tbl[i].irq[id].dev_type;
796        channel = dev_tbl[i].irq[id].channel;
797        is_rx   = dev_tbl[i].irq[id].is_rx;
798        func    = FUNC_FROM_TYPE( type );
799
800        // get pointer on relevant field in iopic_input
801        if( valid )
802        {
803            if     ( func == DEV_FUNC_IOC )                 ptr = &iopic_input.ioc[channel]; 
804            else if((func == DEV_FUNC_TXT) && (is_rx == 0)) ptr = &iopic_input.txt_tx[channel];
805            else if((func == DEV_FUNC_TXT) && (is_rx != 0)) ptr = &iopic_input.txt_rx[channel];
806            else if((func == DEV_FUNC_NIC) && (is_rx == 0)) ptr = &iopic_input.nic_tx[channel];
807            else if((func == DEV_FUNC_NIC) && (is_rx != 0)) ptr = &iopic_input.nic_rx[channel];
808            else if( func == DEV_FUNC_IOB )                 ptr = &iopic_input.iob;
809            else     assert( false , "illegal source device for IOPIC input" );
810
811            // set one entry in all "iopic_input" structures
812            for( x = 0 ; x < info->x_size ; x++ )
813            {
814                for ( y = 0; y < info->y_size; y++ )
815                {
816                    if (cluster_info_is_active(info->cluster_info[x][y])) {
817                        cxy_t  cxy = (x<<info->y_width) + y;
818                        hal_remote_swd( XPTR( cxy , ptr ) , id ); 
819                    }
820                }
821            }
822        }
823    } 
824
825#if( DEBUG_KERNEL_INIT & 0x1 )
826if( hal_time_stamp() > DEBUG_KERNEL_INIT )
827{
828    printk("\n[DBG] %s created PIC chdev in cluster %x at cycle %d\n",
829    __FUNCTION__ , local_cxy , (uint32_t)hal_time_stamp() );
830    dev_pic_inputs_display();
831}
832#endif
833   
834}  // end iopic_init()
835
836///////////////////////////////////////////////////////////////////////////////////////////
837// This function is called by all CP0s in all cluster to complete the PIC device
838// initialisation, namely the informations attached to the LAPIC controller.
839// This initialisation must be done after the IOPIC initialisation, but before other
840// devices initialisation because the IRQ routing infrastructure is required for both
841// internal and external devices initialisation.
842///////////////////////////////////////////////////////////////////////////////////////////
843// @ info    : pointer on the local boot-info structure.
844///////////////////////////////////////////////////////////////////////////////////////////
845static void lapic_init( boot_info_t * info )
846{
847    boot_device_t * dev_tbl;      // pointer on boot_info internal devices array
848    uint32_t        dev_nr;       // number of internal devices
849    uint32_t        i;            // device index in dev_tbl
850        xptr_t          base;         // remote pointer on segment base
851    uint32_t        func;         // device functionnal type in boot_info
852    bool_t          found;        // LAPIC found
853
854    // get number of internal peripherals and base
855        dev_nr      = info->int_dev_nr;
856    dev_tbl     = info->int_dev;
857
858    // loop on internal peripherals to get the lapic device
859        for( i = 0 , found = false ; i < dev_nr ; i++ )
860        {
861        func = FUNC_FROM_TYPE( dev_tbl[i].type );
862
863        if( func == DEV_FUNC_ICU )
864        {
865            base     = dev_tbl[i].base;
866            found    = true;
867            break;
868        }
869    }
870
871    // if the LAPIC controller is not defined in the boot_info,
872    // we simply don't initialize the PIC extensions in the kernel,
873    // making the assumption that the LAPIC related informations
874    // are hidden in the hardware specific PIC driver.
875    if( found )
876    {
877        // initialise the PIC extensions for
878        // the core descriptor and core manager extensions
879        dev_pic_extend_init( (uint32_t *)GET_PTR( base ) );
880
881        // initialize the "lapic_input" structure
882        // defining how internal IRQs are connected to LAPIC
883        uint32_t        id;
884        uint8_t         valid;
885        uint8_t         channel;
886        uint32_t        func;
887
888        for( id = 0 ; id < CONFIG_MAX_INTERNAL_IRQS ; id++ )
889        {
890            valid    = dev_tbl[i].irq[id].valid;
891            func     = FUNC_FROM_TYPE( dev_tbl[i].irq[id].dev_type );
892            channel  = dev_tbl[i].irq[id].channel;
893
894            if( valid ) // only valid local IRQs are registered
895            {
896                if     ( func == DEV_FUNC_MMC ) lapic_input.mmc = id;
897                else if( func == DEV_FUNC_DMA ) lapic_input.dma[channel] = id;
898                else if( func == DEV_FUNC_TXT ) lapic_input.mtty = id;
899                else if( func == DEV_FUNC_IOC ) lapic_input.sdcard = id;
900                else assert( false , "illegal source device for LAPIC input" );
901            }
902        }
903    }
904}  // end lapic_init()
905
906///////////////////////////////////////////////////////////////////////////////////////////
907// This static function returns the identifiers of the calling core.
908///////////////////////////////////////////////////////////////////////////////////////////
909// @ info    : pointer on boot_info structure.
910// @ lid     : [out] core local index in cluster.
911// @ cxy     : [out] cluster identifier.
912// @ lid     : [out] core global identifier (hardware).
913// @ return 0 if success / return EINVAL if not found.
914///////////////////////////////////////////////////////////////////////////////////////////
915static error_t get_core_identifiers( boot_info_t * info,
916                                     lid_t       * lid,
917                                     cxy_t       * cxy,
918                                     gid_t       * gid )
919{
920    uint32_t   i;
921    gid_t      global_id;
922
923    // get global identifier from hardware register
924    global_id = hal_get_gid();
925
926    // makes an associative search in boot_info to get (cxy,lid) from global_id
927    for( i = 0 ; i < info->cores_nr ; i++ )
928    {
929        if( global_id == info->core[i].gid )
930        {
931            *lid = info->core[i].lid;
932            *cxy = info->core[i].cxy;
933            *gid = global_id;
934            return 0;
935        }
936    }
937    return EINVAL;
938}
939
940///////////////////////////////////////////////////////////////////////////////////////////
941// This function is the entry point for the kernel initialisation.
942// It is executed by all cores in all clusters, but only core[0], called CP0,
943// initializes the shared resources such as the cluster manager, or the local peripherals.
944// To comply with the multi-kernels paradigm, it accesses only local cluster memory, using
945// only information contained in the local boot_info_t structure, set by the bootloader.
946// Only CP0 in cluster 0 print the log messages.
947///////////////////////////////////////////////////////////////////////////////////////////
948// @ info    : pointer on the local boot-info structure.
949///////////////////////////////////////////////////////////////////////////////////////////
950void kernel_init( boot_info_t * info )
951{
952    lid_t        core_lid = -1;             // running core local index
953    cxy_t        core_cxy = -1;             // running core cluster identifier
954    gid_t        core_gid;                  // running core hardware identifier
955    cluster_t  * cluster;                   // pointer on local cluster manager
956    core_t     * core;                      // pointer on running core descriptor
957    thread_t   * thread;                    // pointer on idle thread descriptor
958
959    xptr_t       vfs_root_inode_xp;         // extended pointer on VFS root inode
960    xptr_t       devfs_dev_inode_xp;        // extended pointer on DEVFS dev inode   
961    xptr_t       devfs_external_inode_xp;   // extended pointer on DEVFS external inode       
962    xptr_t       devfs_internal_inode_xp;   // extended pointer on DEVFS internal inode       
963
964    error_t      error;
965    reg_t        status;                    // running core status register
966
967    /////////////////////////////////////////////////////////////////////////////////
968    // STEP 0 : Each core get its core identifier from boot_info, and makes
969    //          a partial initialisation of its private idle thread descriptor.
970    //          CP0 initializes the "local_cxy" global variable.
971    //          CP0 in cluster IO initializes the TXT0 chdev to print log messages.
972    /////////////////////////////////////////////////////////////////////////////////
973
974    error = get_core_identifiers( info,
975                                  &core_lid,
976                                  &core_cxy,
977                                  &core_gid );
978
979    // CP0 initializes cluster identifier
980    if( core_lid == 0 ) local_cxy = info->cxy;
981
982    // each core gets a pointer on its private idle thread descriptor
983    thread = (thread_t *)( idle_threads + (core_lid * CONFIG_THREAD_DESC_SIZE) );
984
985    // each core registers this thread pointer in hardware register
986    hal_set_current_thread( thread );
987
988    // each core register core descriptor pointer in idle thread descriptor
989    thread->core = &LOCAL_CLUSTER->core_tbl[core_lid];
990
991    // each core initializes the idle thread lists of locks
992    list_root_init( &thread->locks_root );
993    xlist_root_init( XPTR( local_cxy , &thread->xlocks_root ) );
994    thread->local_locks = 0;
995    thread->remote_locks = 0;
996
997    // CP0 in cluster 0 initializes TXT0 chdev descriptor
998    if( core_cxy == 0 && core_lid == 0 ) // [MODIF]
999    {
1000        if( USE_TXT_MTY == 1 ) {
1001            mtty0_device_init( info );
1002        } else {
1003            txt0_device_init( info );
1004        }
1005    }
1006
1007    /////////////////////////////////////////////////////////////////////////////////
1008    if( core_lid == 0 ) remote_barrier( XPTR( 0 , &global_barrier ), // [FIXME]
1009                                        cluster_info_nb_actives(info->cluster_info) );
1010    barrier_wait( &local_barrier , info->cores_nr );
1011    /////////////////////////////////////////////////////////////////////////////////
1012
1013#if DEBUG_KERNEL_INIT
1014if( (core_lid ==  0) & (local_cxy == 0) ) 
1015printk("\n[DBG] %s : exit barrier 0 : TXT0 initialized / cycle %d\n",
1016__FUNCTION__, (uint32_t)hal_get_cycles() );
1017#endif
1018
1019    /////////////////////////////////////////////////////////////////////////////
1020    // STEP 1 : all cores check core identifier.
1021    //          CP0 initializes the local cluster manager.
1022    //          This includes the memory allocators.
1023    /////////////////////////////////////////////////////////////////////////////
1024
1025    // all cores check identifiers
1026    if( error )
1027    {
1028        assert( false ,
1029        "illegal core identifiers gid = %x / cxy = %x / lid = %d",
1030        core_lid , core_cxy , core_lid );
1031    }
1032
1033    // CP0 initializes cluster manager
1034    if( core_lid == 0 )
1035    {
1036        error = cluster_init( info );
1037
1038        if( error )
1039        {
1040            assert( false ,
1041            "cannot initialise cluster %x", local_cxy );
1042        }
1043    }
1044
1045    /////////////////////////////////////////////////////////////////////////////////
1046    if( core_lid == 0 ) remote_barrier( XPTR( 0 , &global_barrier ), // [FIXME]
1047                                        cluster_info_nb_actives(info->cluster_info) );
1048    barrier_wait( &local_barrier , info->cores_nr );
1049    /////////////////////////////////////////////////////////////////////////////////
1050
1051#if DEBUG_KERNEL_INIT
1052if( (core_lid ==  0) & (local_cxy == 0) ) 
1053printk("\n[DBG] %s : exit barrier 1 : clusters initialised / cycle %d\n",
1054__FUNCTION__, (uint32_t)hal_get_cycles() );
1055#endif
1056
1057    /////////////////////////////////////////////////////////////////////////////////
1058    // STEP 2 : CP0 initializes the process_zero descriptor.
1059    //          CP0 in cluster 0 initializes the IOPIC device.
1060    /////////////////////////////////////////////////////////////////////////////////
1061
1062    // all cores get pointer on local cluster manager & core descriptor
1063    cluster = &cluster_manager;
1064    core    = &cluster->core_tbl[core_lid];
1065
1066    // all CP0s initialize the process_zero descriptor
1067    if( core_lid == 0 ) process_zero_create( &process_zero );
1068
1069    // CP0 in cluster 0 initializes the PIC chdev,
1070    if( (core_lid == 0) && (local_cxy == 0) ) iopic_init( info );
1071   
1072    ////////////////////////////////////////////////////////////////////////////////
1073    if( core_lid == 0 ) remote_barrier( XPTR( 0 , &global_barrier ), // [FIXME]
1074                                        cluster_info_nb_actives(info->cluster_info) );
1075    barrier_wait( &local_barrier , info->cores_nr );
1076    ////////////////////////////////////////////////////////////////////////////////
1077
1078#if DEBUG_KERNEL_INIT
1079if( (core_lid ==  0) & (local_cxy == 0) ) 
1080printk("\n[DBG] %s : exit barrier 2 : PIC initialised / cycle %d\n",
1081__FUNCTION__, (uint32_t)hal_get_cycles() );
1082#endif
1083
1084    ////////////////////////////////////////////////////////////////////////////////
1085    // STEP 3 : CP0 initializes the distibuted LAPIC descriptor.
1086    //          CP0 initializes the internal chdev descriptors
1087    //          CP0 initialize the local external chdev descriptors
1088    ////////////////////////////////////////////////////////////////////////////////
1089
1090    // all CP0s initialize their local LAPIC extension,
1091    if( core_lid == 0 ) lapic_init( info );
1092
1093    // CP0 scan the internal (private) peripherals,
1094    // and allocates memory for the corresponding chdev descriptors.
1095    if( core_lid == 0 ) internal_devices_init( info );
1096       
1097
1098    // All CP0s contribute to initialise external peripheral chdev descriptors.
1099    // Each CP0[cxy] scan the set of external (shared) peripherals (but the TXT0),
1100    // and allocates memory for the chdev descriptors that must be placed
1101    // on the (cxy) cluster according to the global index value.
1102
1103    if( core_lid == 0 ) external_devices_init( info );
1104
1105    /////////////////////////////////////////////////////////////////////////////////
1106    if( core_lid == 0 ) remote_barrier( XPTR( 0 , &global_barrier ), // [FIXME]
1107                                        cluster_info_nb_actives(info->cluster_info) );
1108    barrier_wait( &local_barrier , info->cores_nr );
1109    /////////////////////////////////////////////////////////////////////////////////
1110
1111#if DEBUG_KERNEL_INIT
1112if( (core_lid ==  0) & (local_cxy == 0) ) 
1113printk("\n[DBG] %s : exit barrier 3 : all chdev initialised / cycle %d\n",
1114__FUNCTION__, (uint32_t)hal_get_cycles() );
1115#endif
1116
1117#if( DEBUG_KERNEL_INIT & 1 )
1118if( (core_lid ==  0) & (local_cxy == 0) ) 
1119chdev_dir_display();
1120#endif
1121   
1122    /////////////////////////////////////////////////////////////////////////////////
1123    // STEP 4 : All cores enable IPI (Inter Procesor Interrupt),
1124    //          Alh cores initialize IDLE thread.
1125    //          Only CP0 in cluster 0 creates the VFS root inode.
1126    //          It access the boot device to initialize the file system context.
1127    /////////////////////////////////////////////////////////////////////////////////
1128
1129    // All cores enable the shared IPI channel
1130    dev_pic_enable_ipi();
1131    hal_enable_irq( &status );
1132
1133#if DEBUG_KERNEL_INIT
1134printk("\n[DBG] %s: IPI enabled for core %d cluster %d\n", __FUNCTION__,
1135  core_lid, local_cxy);
1136#endif
1137
1138    // all cores initialize the idle thread descriptor
1139    thread_idle_init( thread,
1140                      THREAD_IDLE,
1141                      &thread_idle_func,
1142                      NULL,
1143                      core_lid );
1144
1145    // all cores unblock idle thread, and register it in scheduler
1146    thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_GLOBAL );
1147    core->scheduler.idle = thread;
1148
1149#if( DEBUG_KERNEL_INIT & 1 )
1150sched_display( core_lid );
1151#endif
1152
1153    // CPO in cluster 0 creates the VFS root
1154    if( (core_lid ==  0) && (local_cxy == 0 ) ) 
1155    {
1156        vfs_root_inode_xp = XPTR_NULL;
1157
1158        // File System must be FATFS in this implementation,
1159        // but other File System can be introduced here
1160        if( CONFIG_VFS_ROOT_IS_FATFS )
1161        {
1162            // 1. allocate memory for FATFS context in cluster 0
1163            fatfs_ctx_t * fatfs_ctx = fatfs_ctx_alloc();
1164
1165            assert( (fatfs_ctx != NULL) ,
1166                    "cannot create FATFS context in cluster 0\n" );
1167
1168            // 2. access boot device to initialize FATFS context
1169            fatfs_ctx_init( fatfs_ctx );
1170 
1171            // 3. get various informations from FATFS context
1172            uint32_t root_dir_cluster = fatfs_ctx->root_dir_cluster;
1173            uint32_t cluster_size     = fatfs_ctx->bytes_per_sector * 
1174                                        fatfs_ctx->sectors_per_cluster;
1175            uint32_t total_clusters   = fatfs_ctx->fat_sectors_count << 7;
1176 
1177            // 4. create VFS root inode in cluster 0
1178            error = vfs_inode_create( XPTR_NULL,                           // dentry_xp
1179                                      FS_TYPE_FATFS,                       // fs_type
1180                                      INODE_TYPE_DIR,                      // inode_type
1181                                      (void *)(intptr_t)root_dir_cluster,  // extend
1182                                      0,                                   // attr
1183                                      0,                                   // rights
1184                                      0,                                   // uid
1185                                      0,                                   // gid
1186                                      &vfs_root_inode_xp );                // return
1187
1188            assert( (error == 0) ,
1189                    "cannot create VFS root inode\n" );
1190
1191            // 5. initialize VFS context for FAT in cluster 0
1192            vfs_ctx_init( FS_TYPE_FATFS,                 // file system type
1193                          0,                             // attributes
1194                              total_clusters,               
1195                              cluster_size,
1196                              vfs_root_inode_xp,             // VFS root
1197                          fatfs_ctx );                   // extend
1198
1199            // 6. check initialisation
1200            vfs_ctx_t   * vfs_ctx = &fs_context[FS_TYPE_FATFS];
1201            assert( (((fatfs_ctx_t *)vfs_ctx->extend)->sectors_per_cluster == 8),
1202             "illegal value for FATFS context in cluster %x\n", local_cxy );
1203        }
1204        else
1205        {
1206            assert( false ,
1207            "root FS must be FATFS" );
1208        }
1209
1210        // register VFS root inode in process_zero descriptor of cluster 0
1211        process_zero.vfs_root_xp = vfs_root_inode_xp;
1212        process_zero.vfs_cwd_xp  = vfs_root_inode_xp;
1213    }
1214
1215    /////////////////////////////////////////////////////////////////////////////////
1216    if( core_lid == 0 ) remote_barrier( XPTR( 0 , &global_barrier ), // [FIXME]
1217                                        cluster_info_nb_actives(info->cluster_info) );
1218    barrier_wait( &local_barrier , info->cores_nr );
1219    /////////////////////////////////////////////////////////////////////////////////
1220
1221#if DEBUG_KERNEL_INIT
1222if( (core_lid ==  0) & (local_cxy == 0) ) 
1223printk("\n[DBG] %s : exit barrier 4 : VFS_root = %l in cluster 0 / cycle %d\n",
1224__FUNCTION__, vfs_root_inode_xp , (uint32_t)hal_get_cycles());
1225#endif
1226
1227    /////////////////////////////////////////////////////////////////////////////////
1228    // STEP 5 : Other CP0s allocate memory for the selected FS context,
1229    //          and initialise both the local FS context and the local VFS context
1230    //          from values stored in cluster 0.
1231    //          They get the VFS root inode extended pointer from cluster 0.
1232    /////////////////////////////////////////////////////////////////////////////////
1233
1234    if( (core_lid ==  0) && (local_cxy != 0) ) 
1235    {
1236        // File System must be FATFS in this implementation,
1237        // but other File System can be introduced here
1238        if( CONFIG_VFS_ROOT_IS_FATFS )
1239        {
1240            // 1. allocate memory for local FATFS context
1241            fatfs_ctx_t * local_fatfs_ctx = fatfs_ctx_alloc();
1242
1243            assert( (local_fatfs_ctx != NULL) ,
1244            "cannot create FATFS context in cluster %x\n", local_cxy );
1245
1246            // 2. get local pointer on VFS context for FATFS
1247            vfs_ctx_t   * vfs_ctx = &fs_context[FS_TYPE_FATFS];
1248
1249            // 3. get local pointer on FATFS context in cluster 0
1250            fatfs_ctx_t * remote_fatfs_ctx = hal_remote_lpt( XPTR( 0 , &vfs_ctx->extend ) );
1251
1252            // 4. copy FATFS context from cluster 0 to local cluster
1253            hal_remote_memcpy( XPTR( local_cxy , local_fatfs_ctx ), 
1254                               XPTR( 0 ,         remote_fatfs_ctx ), sizeof(fatfs_ctx_t) );
1255
1256            // 5. copy VFS context from cluster 0 to local cluster
1257            hal_remote_memcpy( XPTR( local_cxy , vfs_ctx ), 
1258                               XPTR( 0 ,         vfs_ctx ), sizeof(vfs_ctx_t) );
1259
1260            // 6. update extend field in local copy of VFS context
1261            vfs_ctx->extend = local_fatfs_ctx;
1262
1263            // 7. check initialisation
1264            assert( (((fatfs_ctx_t *)vfs_ctx->extend)->sectors_per_cluster == 8),
1265            "illegal value for FATFS context in cluster %x\n", local_cxy );
1266        }
1267
1268        // get extended pointer on VFS root inode from cluster 0
1269        vfs_root_inode_xp = hal_remote_lwd( XPTR( 0 , &process_zero.vfs_root_xp ) );
1270
1271        // update local process_zero descriptor
1272        process_zero.vfs_root_xp = vfs_root_inode_xp;
1273        process_zero.vfs_cwd_xp  = vfs_root_inode_xp;
1274    }
1275
1276    /////////////////////////////////////////////////////////////////////////////////
1277    if( core_lid == 0 ) remote_barrier( XPTR( 0 , &global_barrier ), // [FIXME]
1278                                        cluster_info_nb_actives(info->cluster_info) );
1279    barrier_wait( &local_barrier , info->cores_nr );
1280    /////////////////////////////////////////////////////////////////////////////////
1281
1282#if DEBUG_KERNEL_INIT
1283if( (core_lid ==  0) & (local_cxy == 0) ) 
1284printk("\n[DBG] %s : exit barrier 5 : VFS_root = %l in cluster 0 / cycle %d\n",
1285__FUNCTION__, vfs_root_inode_xp , (uint32_t)hal_get_cycles());
1286#endif
1287
1288    /////////////////////////////////////////////////////////////////////////////////
1289    // STEP 6 : CP0 in cluster IO makes the global DEVFS tree initialisation:
1290    //          It creates the DEVFS directory "dev", and the DEVFS "external"
1291    //          directory in cluster IO and mount these inodes into VFS.
1292    /////////////////////////////////////////////////////////////////////////////////
1293
1294    if( (core_lid ==  0) && (local_cxy == 0) )  // [FIXME]
1295    {
1296        // create "dev" and "external" directories.
1297        devfs_global_init( process_zero.vfs_root_xp,
1298                           &devfs_dev_inode_xp,
1299                           &devfs_external_inode_xp );
1300
1301        // creates the DEVFS context in cluster IO
1302        devfs_ctx_t * devfs_ctx = devfs_ctx_alloc();
1303
1304        assert( (devfs_ctx != NULL) ,
1305                "cannot create DEVFS context in cluster IO\n");
1306
1307        // register DEVFS root and external directories
1308        devfs_ctx_init( devfs_ctx, devfs_dev_inode_xp, devfs_external_inode_xp );
1309    }   
1310
1311    /////////////////////////////////////////////////////////////////////////////////
1312    if( core_lid == 0 ) remote_barrier( XPTR( 0 , &global_barrier ), // [FIXME]
1313                                        cluster_info_nb_actives(info->cluster_info) );
1314    barrier_wait( &local_barrier , info->cores_nr );
1315    /////////////////////////////////////////////////////////////////////////////////
1316
1317#if DEBUG_KERNEL_INIT
1318if( (core_lid ==  0) & (local_cxy == 0) ) 
1319printk("\n[DBG] %s : exit barrier 6 : dev_root = %l in cluster 0 / cycle %d\n",
1320__FUNCTION__, devfs_dev_inode_xp , (uint32_t)hal_get_cycles() );
1321#endif
1322
1323    /////////////////////////////////////////////////////////////////////////////////
1324    // STEP 7 : All CP0s complete in parallel the DEVFS tree initialization.
1325    //          Each CP0 get the "dev" and "external" extended pointers from
1326    //          values stored in cluster IO.
1327    //          Then each CP0 in cluster(i) creates the DEVFS "internal directory,
1328    //          and creates the pseudo-files for all chdevs in cluster (i).
1329    /////////////////////////////////////////////////////////////////////////////////
1330
1331    if( core_lid == 0 )
1332    {
1333        // get extended pointer on "extend" field of VFS context for DEVFS in cluster IO
1334        xptr_t  extend_xp = XPTR( 0 , &fs_context[FS_TYPE_DEVFS].extend ); // [FIXME]
1335
1336        // get pointer on DEVFS context in cluster 0
1337        devfs_ctx_t * devfs_ctx = hal_remote_lpt( extend_xp );
1338       
1339        devfs_dev_inode_xp      = hal_remote_lwd( XPTR( 0 , &devfs_ctx->dev_inode_xp ) );
1340        devfs_external_inode_xp = hal_remote_lwd( XPTR( 0 , &devfs_ctx->external_inode_xp ) );
1341
1342        // populate DEVFS in all clusters
1343        devfs_local_init( devfs_dev_inode_xp,
1344                          devfs_external_inode_xp,
1345                          &devfs_internal_inode_xp );
1346    }
1347
1348    /////////////////////////////////////////////////////////////////////////////////
1349    if( core_lid == 0 ) remote_barrier( XPTR( 0 , &global_barrier ), // [FIXME]
1350                                        cluster_info_nb_actives(info->cluster_info) );
1351    barrier_wait( &local_barrier , info->cores_nr );
1352    /////////////////////////////////////////////////////////////////////////////////
1353
1354#if DEBUG_KERNEL_INIT
1355if( (core_lid ==  0) & (local_cxy == 0) ) 
1356printk("\n[DBG] %s : exit barrier 7 : dev_root = %l in cluster 0 / cycle %d\n",
1357__FUNCTION__, devfs_dev_inode_xp , (uint32_t)hal_get_cycles() );
1358#endif
1359
1360    /////////////////////////////////////////////////////////////////////////////////
1361    // STEP 8 : CP0 in cluster 0 creates the first user process (process_init)
1362    /////////////////////////////////////////////////////////////////////////////////
1363
1364    if( (core_lid == 0) && (local_cxy == 0) ) 
1365    {
1366
1367#if( DEBUG_KERNEL_INIT & 1 )
1368vfs_display( vfs_root_inode_xp );
1369#endif
1370
1371       process_init_create();
1372    }
1373
1374    /////////////////////////////////////////////////////////////////////////////////
1375    if( core_lid == 0 ) remote_barrier( XPTR( 0 , &global_barrier ), // [FIXME]
1376                                        cluster_info_nb_actives(info->cluster_info) );
1377    barrier_wait( &local_barrier , info->cores_nr );
1378    /////////////////////////////////////////////////////////////////////////////////
1379
1380#if DEBUG_KERNEL_INIT
1381if( (core_lid ==  0) & (local_cxy == 0) ) 
1382printk("\n[DBG] %s : exit barrier 8 : process init created / cycle %d\n", 
1383__FUNCTION__ , (uint32_t)hal_get_cycles() );
1384#endif
1385
1386#if (DEBUG_KERNEL_INIT & 1)
1387if( (core_lid ==  0) /*& (local_cxy == 0)*/ ) 
1388sched_display( 0 );
1389#endif
1390
1391    /////////////////////////////////////////////////////////////////////////////////
1392    // STEP 9 : CP0 in cluster 0 print banner
1393    /////////////////////////////////////////////////////////////////////////////////
1394   
1395    if( (core_lid ==  0) && (local_cxy == 0) ) // [FIXME]
1396    {
1397        print_banner( (info->x_size * info->y_size) , info->cores_nr );
1398
1399#if( DEBUG_KERNEL_INIT & 1 )
1400printk("\n\n***** memory fooprint for main kernel objects\n\n"
1401                   " - thread descriptor  : %d bytes\n"
1402                   " - process descriptor : %d bytes\n"
1403                   " - cluster manager    : %d bytes\n"
1404                   " - chdev descriptor   : %d bytes\n"
1405                   " - core descriptor    : %d bytes\n"
1406                   " - scheduler          : %d bytes\n"
1407                   " - rpc fifo           : %d bytes\n"
1408                   " - page descriptor    : %d bytes\n"
1409                   " - mapper root        : %d bytes\n"
1410                   " - ppm manager        : %d bytes\n"
1411                   " - kcm manager        : %d bytes\n"
1412                   " - khm manager        : %d bytes\n"
1413                   " - vmm manager        : %d bytes\n"
1414                   " - gpt root           : %d bytes\n"
1415                   " - list item          : %d bytes\n"
1416                   " - xlist item         : %d bytes\n"
1417                   " - spinlock           : %d bytes\n"
1418                   " - remote spinlock    : %d bytes\n"
1419                   " - rwlock             : %d bytes\n"
1420                   " - remote rwlock      : %d bytes\n",
1421                   sizeof( thread_t          ),
1422                   sizeof( process_t         ),
1423                   sizeof( cluster_t         ),
1424                   sizeof( chdev_t           ),
1425                   sizeof( core_t            ),
1426                   sizeof( scheduler_t       ),
1427                   sizeof( remote_fifo_t     ),
1428                   sizeof( page_t            ),
1429                   sizeof( mapper_t          ),
1430                   sizeof( ppm_t             ),
1431                   sizeof( kcm_t             ),
1432                   sizeof( khm_t             ),
1433                   sizeof( vmm_t             ),
1434                   sizeof( gpt_t             ),
1435                   sizeof( list_entry_t      ),
1436                   sizeof( xlist_entry_t     ),
1437                   sizeof( spinlock_t        ),
1438                   sizeof( remote_spinlock_t ),
1439                   sizeof( rwlock_t          ),
1440                   sizeof( remote_rwlock_t   ));
1441#endif
1442
1443    }
1444
1445    // each core activates its private TICK IRQ
1446    dev_pic_enable_timer( CONFIG_SCHED_TICK_MS_PERIOD );
1447
1448#if DEBUG_KERNEL_INIT
1449printk("\n[DBG] %s : thread %x on core[%x,%d] jumps to thread_idle_func() / cycle %d\n",
1450__FUNCTION__ , CURRENT_THREAD , local_cxy , core_lid , (uint32_t)hal_get_cycles() );
1451#endif
1452
1453    // each core jump to thread_idle_func
1454    thread_idle_func();
1455}
1456
Note: See TracBrowser for help on using the repository browser.