source: trunk/kernel/fs/devfs.c @ 598

Last change on this file since 598 was 598, checked in by alain, 5 years ago

1) Modify the devfs_user_move() function to support large user buffer transfer to/from TXT device.
2) Cosmetic: Improve debug.

File size: 18.6 KB
RevLine 
[23]1/*
2 * devfs.c - DEVFS File system API implementation.
3 *
4 * Author   Mohamed Lamine Karaoui (2014,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
[457]25#include <hal_kernel_types.h>
[23]26#include <hal_special.h>
[407]27#include <hal_uspace.h>
[23]28#include <printk.h>
29#include <chdev.h>
[433]30#include <thread.h>
[407]31#include <dev_txt.h>
[188]32#include <cluster.h>
[23]33#include <vfs.h>
[188]34#include <kmem.h>
[23]35#include <devfs.h>
36
[188]37/////////////////////////////////////////////////////////////////////////////////////////
38//     Extern variables
39/////////////////////////////////////////////////////////////////////////////////////////
[23]40
[188]41extern vfs_ctx_t            fs_context[];   // allocated in kernel_init.c
42extern chdev_directory_t    chdev_dir;      // allocated in kernel_init.c
[23]43
[438]44#if (DEBUG_SYS_READ & 1)
[435]45extern uint32_t  enter_devfs_read;
46extern uint32_t  exit_devfs_read;
[407]47#endif
48
[438]49#if (DEBUG_SYS_WRITE & 1)
[435]50extern uint32_t  enter_devfs_write;
51extern uint32_t  exit_devfs_write;
52#endif
53
[568]54/////////////////////////////////////
[484]55devfs_ctx_t * devfs_ctx_alloc( void )
[188]56{
57    kmem_req_t    req;
[23]58
[188]59        req.type    = KMEM_DEVFS_CTX;
60        req.size    = sizeof(devfs_ctx_t);
61    req.flags   = AF_KERNEL | AF_ZERO;
[23]62
[188]63        return (devfs_ctx_t *)kmem_alloc( &req );
64}
[23]65
[188]66/////////////////////////////////////////////
67void devfs_ctx_init( devfs_ctx_t * devfs_ctx,
[204]68                     xptr_t        devfs_dev_inode_xp,
[188]69                     xptr_t        devfs_external_inode_xp )
[23]70{
[204]71    devfs_ctx->dev_inode_xp      = devfs_dev_inode_xp;
[188]72    devfs_ctx->external_inode_xp = devfs_external_inode_xp;
[23]73
[188]74    fs_context[FS_TYPE_DEVFS].extend = devfs_ctx;
75}
[23]76
[188]77/////////////////////////////////////////////////
78void devfs_ctx_destroy( devfs_ctx_t * devfs_ctx )
79{
80    kmem_req_t    req;
[23]81
[188]82    req.type = KMEM_DEVFS_CTX;
83    req.ptr  = devfs_ctx;
84    kmem_free( &req );
85}
[23]86
[437]87/////////////////////////////////////////////////
[188]88void devfs_global_init( xptr_t   parent_inode_xp,
[204]89                        xptr_t * devfs_dev_inode_xp,
[188]90                        xptr_t * devfs_external_inode_xp )
[23]91{
92    error_t  error;
93
[457]94    // creates DEVFS "dev" inode in cluster 0
95    error = vfs_add_child_in_parent( 0,                // cxy
[188]96                                     INODE_TYPE_DIR,
97                                     FS_TYPE_DEVFS,
98                                     parent_inode_xp,
99                                     "dev",
100                                     NULL,
[204]101                                     devfs_dev_inode_xp ); 
[23]102
[492]103    assert( (error == 0) , "cannot create <dev>\n" );
[23]104
[568]105#if DEBUG_DEVFS_INIT
[598]106uint32_t   cycle = (uint32_t)hal_get_cycles();
107thread_t * this  = CURRENT_THREAD;
[438]108if( DEBUG_DEVFS_INIT < cycle )
[598]109printk("\n[%s] thread[%x,%x] created <dev> inode / cycle %d\n",
110__FUNCTION__, this->process->pid, this->trdid, cycle );
[437]111#endif
112
[457]113    // create DEVFS "external" inode in cluster 0
114    error = vfs_add_child_in_parent( 0,               // cxy
[188]115                                     INODE_TYPE_DIR,
116                                     FS_TYPE_DEVFS,
[204]117                                     *devfs_dev_inode_xp,
118                                     "external",
[188]119                                     NULL,
120                                     devfs_external_inode_xp ); 
[23]121
[492]122    assert( (error == 0) , "cannot create <external>\n" );
[279]123
[438]124#if DEBUG_DEVFS_INIT
[433]125cycle = (uint32_t)hal_get_cycles();
[438]126if( DEBUG_DEVFS_INIT < cycle )
[598]127printk("\n[%s] thread[%x,%x] created <external> inode / cycle %d\n",
128__FUNCTION__, this->process->pid, this->trdid, cycle );
[433]129#endif
130
[188]131} 
[23]132
[204]133///////////////////////////////////////////////////
134void devfs_local_init( xptr_t   devfs_dev_inode_xp,
135                       xptr_t   devfs_external_inode_xp,
136                       xptr_t * devfs_internal_inode_xp )
[23]137{
138    char          node_name[16];
[188]139    xptr_t        chdev_xp;
140    cxy_t         chdev_cxy;
[407]141    chdev_t     * chdev_ptr;
[188]142    xptr_t        inode_xp;
[23]143    uint32_t      channel;
144
[568]145    // create "internal" directory
[188]146    snprintf( node_name , 16 , "internal_%x" , local_cxy );
147    vfs_add_child_in_parent( local_cxy,
148                             INODE_TYPE_DIR,
149                             FS_TYPE_DEVFS,
[204]150                             devfs_dev_inode_xp,
[188]151                             node_name,
152                             NULL,
[204]153                             devfs_internal_inode_xp );
[568]154#if DEBUG_DEVFS_INIT
[598]155uint32_t   cycle = (uint32_t)hal_get_cycles();
156thread_t * this  = CURRENT_THREAD;
[568]157if( DEBUG_DEVFS_INIT < cycle )
[598]158printk("\n[%s] thread[%x,%x] created <%s> inode in cluster %x / cycle %d\n",
159__FUNCTION__, this->process->pid, this->trdid, node_name, local_cxy, cycle );
[568]160#endif
[23]161
162    // create MMC chdev inode
[407]163    chdev_xp  = chdev_dir.mmc[local_cxy];
[188]164    if( chdev_xp != XPTR_NULL)
165    {
[407]166        chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
[188]167        vfs_add_child_in_parent( local_cxy,
168                                 INODE_TYPE_DEV,
169                                 FS_TYPE_DEVFS,
[204]170                                 *devfs_internal_inode_xp,
[407]171                                 chdev_ptr->name,
[188]172                                 GET_PTR( chdev_xp ),
173                                 &inode_xp );
[568]174#if DEBUG_DEVFS_INIT
175cycle = (uint32_t)hal_get_cycles();
176if( DEBUG_DEVFS_INIT < cycle )
[598]177printk("\n[%s] thread[%x,%x] created <mmc> inode in cluster %x\n",
178__FUNCTION__, this->process->pid, this->trdid, local_cxy, cycle );
[568]179#endif
180
[188]181    }
[23]182
183    // create DMA chdev inodes (one DMA channel per core)
[188]184    for( channel = 0 ; channel < LOCAL_CLUSTER->cores_nr ; channel++ )
[23]185    {
[188]186        chdev_xp = chdev_dir.dma[channel];
187        if( chdev_xp != XPTR_NULL)
188        {
[407]189            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
[188]190            vfs_add_child_in_parent( local_cxy,
191                                     INODE_TYPE_DEV,
192                                     FS_TYPE_DEVFS,
[204]193                                     *devfs_internal_inode_xp,
[407]194                                     chdev_ptr->name,
[188]195                                     GET_PTR( chdev_xp ),
196                                     &inode_xp );
[568]197#if DEBUG_DEVFS_INIT
198cycle = (uint32_t)hal_get_cycles();
199if( DEBUG_DEVFS_INIT < cycle )
[598]200printk("\n[%s] thread [%x,%x] created <dma[%d]> inode in cluster %x\n",
201__FUNCTION__, this->process->pid, this->trdid, channel, local_cxy, cycle );
[568]202#endif
[188]203        }
[23]204    }
205
[188]206    // create an IOB inode in cluster containing IOB chdev
207    chdev_xp = chdev_dir.iob;
208    if( chdev_xp != XPTR_NULL )
[23]209    {
[188]210        chdev_cxy = GET_CXY( chdev_xp );
[407]211        chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
[188]212        if( chdev_cxy == local_cxy )
213        {
214            vfs_add_child_in_parent( local_cxy,
215                                     INODE_TYPE_DEV,
216                                     FS_TYPE_DEVFS,
217                                     devfs_external_inode_xp,
[407]218                                     chdev_ptr->name,
[188]219                                     GET_PTR( chdev_xp ),
220                                     &inode_xp );
[568]221#if DEBUG_DEVFS_INIT
222cycle = (uint32_t)hal_get_cycles();
223if( DEBUG_DEVFS_INIT < cycle )
[598]224printk("\n[%s] thread[%x,%x] created <iob> inode in cluster %x\n",
225__FUNCTION__, this->process->pid, this->trdid, local_cxy, cycle );
[568]226#endif
[188]227        }
228    }
[23]229       
[188]230    // create a PIC inode in cluster containing PIC chdev
231    chdev_xp = chdev_dir.pic;
232    if( chdev_xp != XPTR_NULL )
233    {
234        chdev_cxy = GET_CXY( chdev_xp );
[407]235        chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
[188]236        if( chdev_cxy == local_cxy )
[23]237        {
[188]238            vfs_add_child_in_parent( local_cxy,
239                                     INODE_TYPE_DEV,
240                                     FS_TYPE_DEVFS,
241                                     devfs_external_inode_xp,
[407]242                                     chdev_ptr->name,
[188]243                                     GET_PTR( chdev_xp ),
244                                     &inode_xp );
[568]245#if DEBUG_DEVFS_INIT
246cycle = (uint32_t)hal_get_cycles();
247if( DEBUG_DEVFS_INIT < cycle )
[598]248printk("\n[%s] thread[%x,%x] created <pic> inode in cluster %x\n",
249__FUNCTION__, this->process->pid, this->trdid, local_cxy, cycle );
[568]250#endif
[23]251        }
[188]252    }
[23]253
[407]254    // create a TXT_RX inode in each cluster containing a TXT_RX chdev
[188]255    for( channel = 0 ; channel < CONFIG_MAX_TXT_CHANNELS ; channel++ )
256    {
[407]257        chdev_xp = chdev_dir.txt_rx[channel];
[188]258        if( chdev_xp != XPTR_NULL )
[23]259        {
[188]260            chdev_cxy = GET_CXY( chdev_xp );
[407]261            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
[188]262            if( chdev_cxy == local_cxy )
263            {
264                vfs_add_child_in_parent( local_cxy,
265                                         INODE_TYPE_DEV,
266                                         FS_TYPE_DEVFS,
267                                         devfs_external_inode_xp,
[407]268                                         chdev_ptr->name,
[188]269                                         GET_PTR( chdev_xp ),
270                                         &inode_xp );
[568]271#if DEBUG_DEVFS_INIT
272cycle = (uint32_t)hal_get_cycles();
273if( DEBUG_DEVFS_INIT < cycle )
[598]274printk("\n[%s] thread[%x,%x] created <txt_rx[%d]> inode in cluster %x\n",
275__FUNCTION__, this->process->pid, this->trdid, channel, local_cxy, cycle );
[568]276#endif
[188]277            }
[23]278        }
[188]279    }
[23]280
[407]281    // create a TXT_TX inode in each cluster containing a TXT_TX chdev
282    for( channel = 0 ; channel < CONFIG_MAX_TXT_CHANNELS ; channel++ )
283    {
284        chdev_xp = chdev_dir.txt_tx[channel];
285        if( chdev_xp != XPTR_NULL )
286        {
287            chdev_cxy = GET_CXY( chdev_xp );
288            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
289            if( chdev_cxy == local_cxy )
290            {
291                vfs_add_child_in_parent( local_cxy,
292                                         INODE_TYPE_DEV,
293                                         FS_TYPE_DEVFS,
294                                         devfs_external_inode_xp,
295                                         chdev_ptr->name,
296                                         GET_PTR( chdev_xp ),
297                                         &inode_xp );
[568]298#if DEBUG_DEVFS_INIT
299cycle = (uint32_t)hal_get_cycles();
300if( DEBUG_DEVFS_INIT < cycle )
[598]301printk("\n[%s] thread[%x,%x] created <txt_tx[%d]> inode in cluster %x\n",
302__FUNCTION__, this->process->pid, this->trdid, channel, local_cxy, cycle );
[568]303#endif
[407]304            }
305        }
306    }
307
[188]308    // create an IOC inode in each cluster containing an IOC chdev
309    for( channel = 0 ; channel < CONFIG_MAX_IOC_CHANNELS ; channel++ )
310    {
311        chdev_xp = chdev_dir.ioc[channel];
312        if( chdev_xp != XPTR_NULL )
[23]313        {
[188]314            chdev_cxy = GET_CXY( chdev_xp );
[407]315            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
[188]316            if( chdev_cxy == local_cxy )
317            {
318                vfs_add_child_in_parent( local_cxy,
319                                         INODE_TYPE_DEV,
320                                         FS_TYPE_DEVFS,
321                                         devfs_external_inode_xp,
[407]322                                         chdev_ptr->name,
[188]323                                         GET_PTR( chdev_xp ),
324                                         &inode_xp );
[568]325#if DEBUG_DEVFS_INIT
326cycle = (uint32_t)hal_get_cycles();
327if( DEBUG_DEVFS_INIT < cycle )
[598]328printk("\n[%s] thread[%x,%x] created <ioc[%d]> inode in cluster %x\n",
329__FUNCTION__, this->process->pid, this->trdid, channel, local_cxy, cycle );
[568]330#endif
[188]331            }
[23]332        }
[188]333    }
[23]334
[188]335    // create a FBF inode in each cluster containing a FBF chdev
[568]336    for( channel = 0 ; channel < CONFIG_MAX_FBF_CHANNELS ; channel++ )
[188]337    {
338        chdev_xp = chdev_dir.fbf[channel];
339        if( chdev_xp != XPTR_NULL )
[23]340        {
[188]341            chdev_cxy = GET_CXY( chdev_xp );
[407]342            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
[188]343            if( chdev_cxy == local_cxy )
344            {
345                vfs_add_child_in_parent( local_cxy,
346                                         INODE_TYPE_DEV,
347                                         FS_TYPE_DEVFS,
348                                         devfs_external_inode_xp,
[407]349                                         chdev_ptr->name,
[188]350                                         GET_PTR( chdev_xp ),
351                                         &inode_xp );
[568]352#if DEBUG_DEVFS_INIT
353cycle = (uint32_t)hal_get_cycles();
354if( DEBUG_DEVFS_INIT < cycle )
[598]355printk("\n[%s] thread[%x,%x] created <fbf[%d]> inode in cluster %x\n",
356__FUNCTION__, this->process->pid, this->trdid, channel, local_cxy, cycle );
[568]357#endif
[188]358            }
[23]359        }
[188]360    }
[23]361
[188]362    // create a NIC_RX inode in each cluster containing a NIC_RX chdev
363    for( channel = 0 ; channel < CONFIG_MAX_NIC_CHANNELS ; channel++ )
364    {
365        chdev_xp = chdev_dir.nic_rx[channel];
366        if( chdev_xp != XPTR_NULL )
[23]367        {
[188]368            chdev_cxy = GET_CXY( chdev_xp );
[407]369            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
[188]370            if( chdev_cxy == local_cxy )
371            {
372                vfs_add_child_in_parent( local_cxy,
373                                         INODE_TYPE_DEV,
374                                         FS_TYPE_DEVFS,
375                                         devfs_external_inode_xp,
[407]376                                         chdev_ptr->name,
[188]377                                         GET_PTR( chdev_xp ),
378                                         &inode_xp );
[568]379#if DEBUG_DEVFS_INIT
380cycle = (uint32_t)hal_get_cycles();
381if( DEBUG_DEVFS_INIT < cycle )
[598]382printk("\n[%s] thread[%x,%x] created <nic_rx[%d]> inode in cluster %x\n",
383__FUNCTION__, this->process->pid, this->trdid, channel, local_cxy, cycle );
[568]384#endif
[188]385            }
[23]386        }
387    }
388
[188]389    // create a NIC_TX inode in each cluster containing a NIC_TX chdev
390    for( channel = 0 ; channel < CONFIG_MAX_NIC_CHANNELS ; channel++ )
[23]391    {
[188]392        chdev_xp = chdev_dir.nic_tx[channel];
393        if( chdev_xp != XPTR_NULL )
394        {
395            chdev_cxy = GET_CXY( chdev_xp );
[407]396            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
[188]397            if( chdev_cxy == local_cxy )
398            {
399                vfs_add_child_in_parent( local_cxy,
400                                         INODE_TYPE_DEV,
401                                         FS_TYPE_DEVFS,
402                                         devfs_external_inode_xp,
[407]403                                         chdev_ptr->name,
[188]404                                         GET_PTR( chdev_xp ),
405                                         &inode_xp );
[438]406#if DEBUG_DEVFS_INIT
[433]407cycle = (uint32_t)hal_get_cycles();
[438]408if( DEBUG_DEVFS_INIT < cycle )
[598]409printk("\n[%s] thread[%x,%x] created <nic_tx[%d]> inode in cluster %x\n",
410__FUNCTION__, this->process->pid, this->trdid, channel, local_cxy, cycle );
[433]411#endif
[568]412            }
413        }
414    }
[188]415}  // end devfs_local_init()
[23]416
[407]417//////////////////////////////////////////
418int devfs_user_move( bool_t     to_buffer,
419                     xptr_t     file_xp,
[598]420                     char     * u_buf,
[407]421                     uint32_t   size )
422{
[598]423    xptr_t           chdev_xp;
424    cxy_t            chdev_cxy;
425    chdev_t        * chdev_ptr;                    // associated chdev type
426    uint32_t         func;                         // chdev functionnal type
427    uint32_t         channel;                      // chdev channel index
428    uint32_t         burst;                        // number of bytes in a burst
429    uint32_t         todo;                         // number of bytes not yet moved
430    error_t          error;
431    uint32_t         i;
[407]432
[598]433    char             k_buf[CONFIG_TXT_KBUF_SIZE];  // local kernel buffer
[407]434
[598]435assert( ( file_xp != XPTR_NULL ) , "file_xp == XPTR_NULL" );
[407]436
[438]437#if (DEBUG_SYS_READ & 1)
[435]438enter_devfs_read = hal_time_stamp();
439#endif
440
[438]441#if (DEBUG_SYS_WRITE & 1)
[435]442enter_devfs_write = hal_time_stamp();
443#endif
444
[438]445#if DEBUG_DEVFS_MOVE
[598]446uint32_t   cycle = (uint32_t)hal_get_cycles();
447thread_t * this  = CURRENT_THREAD;
[438]448if( DEBUG_DEVFS_MOVE < cycle )
[598]449printk("\n[%s] thread[%x,%x] enter / to_mem %d / cycle %d\n",
450__FUNCTION__, this->process->pid, this->trdid, to_buffer, cycle );
[433]451#endif
[407]452
[430]453    // get extended pointer on chdev_xp
454    chdev_xp = chdev_from_file( file_xp );
[407]455
[430]456    // get cluster and local pointer on chdev
457    chdev_cxy  = GET_CXY( chdev_xp );
458    chdev_ptr  = (chdev_t *)GET_PTR( chdev_xp );
[407]459
460    // get chdev functionnal type and channel
[568]461    func    = hal_remote_l32( XPTR( chdev_cxy , &chdev_ptr->func ) );
462    channel = hal_remote_l32( XPTR( chdev_cxy , &chdev_ptr->channel ) );
[407]463
[598]464assert( ( func == DEV_FUNC_TXT ) , __FUNCTION__, "illegal device func_type");
465
466    // initialise number of bytes to move
467    todo = size;
468
469    /////////////// TXT read
470    if( to_buffer )
471    { 
472        while( todo )
473        {
474            // set burst size
475            if( todo > CONFIG_TXT_KBUF_SIZE ) burst = CONFIG_TXT_KBUF_SIZE;
476            else                              burst = todo;
477
478            // read burst bytes from TXT device to kernel buffer
479            for( i = 0 ; i < burst ; i++ )
[407]480            {
481                error = dev_txt_read( channel , &k_buf[i] );
482
[598]483                if( error )  return -1;
484            }
[407]485
[598]486            // move burst bytes from k_buf to u_buf                   
487            hal_strcpy_to_uspace( u_buf , k_buf , burst );
488
489            // update loop variables
490            todo  -= burst;
491            u_buf += burst;
492        }
493
[438]494#if DEBUG_DEVFS_MOVE
[433]495cycle = (uint32_t)hal_get_cycles();
[438]496if( DEBUG_DEVFS_MOVE < cycle )
[598]497printk("\n[%s] thread[%x,%x] exit / to_mem %d / cycle %d\n",
498__FUNCTION__, this->process->pid, this->trdid, to_buffer, cycle );
[433]499#endif
500
[438]501#if (DEBUG_SYS_READ & 1)
[435]502exit_devfs_read = hal_time_stamp();
[407]503#endif
504            return size;
[598]505    } 
506    ///////////// TXT write 
507    else               
508    {
509        while( todo )
[407]510        {
[598]511            // set burst size
512            if( todo > CONFIG_TXT_KBUF_SIZE ) burst = CONFIG_TXT_KBUF_SIZE;
513            else                              burst = todo;
[407]514
[598]515            // move burst bytes from u_buf to k_buf
516            hal_strcpy_from_uspace( k_buf , u_buf , burst );
[407]517
[598]518            // write burst bytes from kernel buffer to TXT device
519            error = dev_txt_write( channel , k_buf , burst );
520
521            if( error ) return -1;
522
523            // update loop variables
524            todo  -= burst;
525            u_buf += burst;
526        } 
527
[438]528#if DEBUG_DEVFS_MOVE
[433]529cycle = (uint32_t)hal_get_cycles();
[438]530if( DEBUG_DEVFS_MOVE < cycle )
[598]531printk("\n[%s] thread[%x,%x] exit / to_mem %d / cycle %d\n",
532__FUNCTION__, this->process->pid, this->trdid, to_buffer, cycle );
[433]533#endif
[407]534
[438]535#if (DEBUG_SYS_WRITE & 1)
[435]536exit_devfs_write = hal_time_stamp();
537#endif
[598]538            return size;
[407]539    }
540
541}  // end devfs_user_move()
542
543
Note: See TracBrowser for help on using the repository browser.