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

Last change on this file since 601 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
Line 
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
25#include <hal_kernel_types.h>
26#include <hal_special.h>
27#include <hal_uspace.h>
28#include <printk.h>
29#include <chdev.h>
30#include <thread.h>
31#include <dev_txt.h>
32#include <cluster.h>
33#include <vfs.h>
34#include <kmem.h>
35#include <devfs.h>
36
37/////////////////////////////////////////////////////////////////////////////////////////
38//     Extern variables
39/////////////////////////////////////////////////////////////////////////////////////////
40
41extern vfs_ctx_t            fs_context[];   // allocated in kernel_init.c
42extern chdev_directory_t    chdev_dir;      // allocated in kernel_init.c
43
44#if (DEBUG_SYS_READ & 1)
45extern uint32_t  enter_devfs_read;
46extern uint32_t  exit_devfs_read;
47#endif
48
49#if (DEBUG_SYS_WRITE & 1)
50extern uint32_t  enter_devfs_write;
51extern uint32_t  exit_devfs_write;
52#endif
53
54/////////////////////////////////////
55devfs_ctx_t * devfs_ctx_alloc( void )
56{
57    kmem_req_t    req;
58
59        req.type    = KMEM_DEVFS_CTX;
60        req.size    = sizeof(devfs_ctx_t);
61    req.flags   = AF_KERNEL | AF_ZERO;
62
63        return (devfs_ctx_t *)kmem_alloc( &req );
64}
65
66/////////////////////////////////////////////
67void devfs_ctx_init( devfs_ctx_t * devfs_ctx,
68                     xptr_t        devfs_dev_inode_xp,
69                     xptr_t        devfs_external_inode_xp )
70{
71    devfs_ctx->dev_inode_xp      = devfs_dev_inode_xp;
72    devfs_ctx->external_inode_xp = devfs_external_inode_xp;
73
74    fs_context[FS_TYPE_DEVFS].extend = devfs_ctx;
75}
76
77/////////////////////////////////////////////////
78void devfs_ctx_destroy( devfs_ctx_t * devfs_ctx )
79{
80    kmem_req_t    req;
81
82    req.type = KMEM_DEVFS_CTX;
83    req.ptr  = devfs_ctx;
84    kmem_free( &req );
85}
86
87/////////////////////////////////////////////////
88void devfs_global_init( xptr_t   parent_inode_xp,
89                        xptr_t * devfs_dev_inode_xp,
90                        xptr_t * devfs_external_inode_xp )
91{
92    error_t  error;
93
94    // creates DEVFS "dev" inode in cluster 0
95    error = vfs_add_child_in_parent( 0,                // cxy
96                                     INODE_TYPE_DIR,
97                                     FS_TYPE_DEVFS,
98                                     parent_inode_xp,
99                                     "dev",
100                                     NULL,
101                                     devfs_dev_inode_xp ); 
102
103    assert( (error == 0) , "cannot create <dev>\n" );
104
105#if DEBUG_DEVFS_INIT
106uint32_t   cycle = (uint32_t)hal_get_cycles();
107thread_t * this  = CURRENT_THREAD;
108if( DEBUG_DEVFS_INIT < cycle )
109printk("\n[%s] thread[%x,%x] created <dev> inode / cycle %d\n",
110__FUNCTION__, this->process->pid, this->trdid, cycle );
111#endif
112
113    // create DEVFS "external" inode in cluster 0
114    error = vfs_add_child_in_parent( 0,               // cxy
115                                     INODE_TYPE_DIR,
116                                     FS_TYPE_DEVFS,
117                                     *devfs_dev_inode_xp,
118                                     "external",
119                                     NULL,
120                                     devfs_external_inode_xp ); 
121
122    assert( (error == 0) , "cannot create <external>\n" );
123
124#if DEBUG_DEVFS_INIT
125cycle = (uint32_t)hal_get_cycles();
126if( DEBUG_DEVFS_INIT < cycle )
127printk("\n[%s] thread[%x,%x] created <external> inode / cycle %d\n",
128__FUNCTION__, this->process->pid, this->trdid, cycle );
129#endif
130
131} 
132
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 )
137{
138    char          node_name[16];
139    xptr_t        chdev_xp;
140    cxy_t         chdev_cxy;
141    chdev_t     * chdev_ptr;
142    xptr_t        inode_xp;
143    uint32_t      channel;
144
145    // create "internal" directory
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,
150                             devfs_dev_inode_xp,
151                             node_name,
152                             NULL,
153                             devfs_internal_inode_xp );
154#if DEBUG_DEVFS_INIT
155uint32_t   cycle = (uint32_t)hal_get_cycles();
156thread_t * this  = CURRENT_THREAD;
157if( DEBUG_DEVFS_INIT < cycle )
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 );
160#endif
161
162    // create MMC chdev inode
163    chdev_xp  = chdev_dir.mmc[local_cxy];
164    if( chdev_xp != XPTR_NULL)
165    {
166        chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
167        vfs_add_child_in_parent( local_cxy,
168                                 INODE_TYPE_DEV,
169                                 FS_TYPE_DEVFS,
170                                 *devfs_internal_inode_xp,
171                                 chdev_ptr->name,
172                                 GET_PTR( chdev_xp ),
173                                 &inode_xp );
174#if DEBUG_DEVFS_INIT
175cycle = (uint32_t)hal_get_cycles();
176if( DEBUG_DEVFS_INIT < cycle )
177printk("\n[%s] thread[%x,%x] created <mmc> inode in cluster %x\n",
178__FUNCTION__, this->process->pid, this->trdid, local_cxy, cycle );
179#endif
180
181    }
182
183    // create DMA chdev inodes (one DMA channel per core)
184    for( channel = 0 ; channel < LOCAL_CLUSTER->cores_nr ; channel++ )
185    {
186        chdev_xp = chdev_dir.dma[channel];
187        if( chdev_xp != XPTR_NULL)
188        {
189            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
190            vfs_add_child_in_parent( local_cxy,
191                                     INODE_TYPE_DEV,
192                                     FS_TYPE_DEVFS,
193                                     *devfs_internal_inode_xp,
194                                     chdev_ptr->name,
195                                     GET_PTR( chdev_xp ),
196                                     &inode_xp );
197#if DEBUG_DEVFS_INIT
198cycle = (uint32_t)hal_get_cycles();
199if( DEBUG_DEVFS_INIT < cycle )
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 );
202#endif
203        }
204    }
205
206    // create an IOB inode in cluster containing IOB chdev
207    chdev_xp = chdev_dir.iob;
208    if( chdev_xp != XPTR_NULL )
209    {
210        chdev_cxy = GET_CXY( chdev_xp );
211        chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
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,
218                                     chdev_ptr->name,
219                                     GET_PTR( chdev_xp ),
220                                     &inode_xp );
221#if DEBUG_DEVFS_INIT
222cycle = (uint32_t)hal_get_cycles();
223if( DEBUG_DEVFS_INIT < cycle )
224printk("\n[%s] thread[%x,%x] created <iob> inode in cluster %x\n",
225__FUNCTION__, this->process->pid, this->trdid, local_cxy, cycle );
226#endif
227        }
228    }
229       
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 );
235        chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
236        if( chdev_cxy == local_cxy )
237        {
238            vfs_add_child_in_parent( local_cxy,
239                                     INODE_TYPE_DEV,
240                                     FS_TYPE_DEVFS,
241                                     devfs_external_inode_xp,
242                                     chdev_ptr->name,
243                                     GET_PTR( chdev_xp ),
244                                     &inode_xp );
245#if DEBUG_DEVFS_INIT
246cycle = (uint32_t)hal_get_cycles();
247if( DEBUG_DEVFS_INIT < cycle )
248printk("\n[%s] thread[%x,%x] created <pic> inode in cluster %x\n",
249__FUNCTION__, this->process->pid, this->trdid, local_cxy, cycle );
250#endif
251        }
252    }
253
254    // create a TXT_RX inode in each cluster containing a TXT_RX chdev
255    for( channel = 0 ; channel < CONFIG_MAX_TXT_CHANNELS ; channel++ )
256    {
257        chdev_xp = chdev_dir.txt_rx[channel];
258        if( chdev_xp != XPTR_NULL )
259        {
260            chdev_cxy = GET_CXY( chdev_xp );
261            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
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,
268                                         chdev_ptr->name,
269                                         GET_PTR( chdev_xp ),
270                                         &inode_xp );
271#if DEBUG_DEVFS_INIT
272cycle = (uint32_t)hal_get_cycles();
273if( DEBUG_DEVFS_INIT < cycle )
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 );
276#endif
277            }
278        }
279    }
280
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 );
298#if DEBUG_DEVFS_INIT
299cycle = (uint32_t)hal_get_cycles();
300if( DEBUG_DEVFS_INIT < cycle )
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 );
303#endif
304            }
305        }
306    }
307
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 )
313        {
314            chdev_cxy = GET_CXY( chdev_xp );
315            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
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,
322                                         chdev_ptr->name,
323                                         GET_PTR( chdev_xp ),
324                                         &inode_xp );
325#if DEBUG_DEVFS_INIT
326cycle = (uint32_t)hal_get_cycles();
327if( DEBUG_DEVFS_INIT < cycle )
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 );
330#endif
331            }
332        }
333    }
334
335    // create a FBF inode in each cluster containing a FBF chdev
336    for( channel = 0 ; channel < CONFIG_MAX_FBF_CHANNELS ; channel++ )
337    {
338        chdev_xp = chdev_dir.fbf[channel];
339        if( chdev_xp != XPTR_NULL )
340        {
341            chdev_cxy = GET_CXY( chdev_xp );
342            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
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,
349                                         chdev_ptr->name,
350                                         GET_PTR( chdev_xp ),
351                                         &inode_xp );
352#if DEBUG_DEVFS_INIT
353cycle = (uint32_t)hal_get_cycles();
354if( DEBUG_DEVFS_INIT < cycle )
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 );
357#endif
358            }
359        }
360    }
361
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 )
367        {
368            chdev_cxy = GET_CXY( chdev_xp );
369            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
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,
376                                         chdev_ptr->name,
377                                         GET_PTR( chdev_xp ),
378                                         &inode_xp );
379#if DEBUG_DEVFS_INIT
380cycle = (uint32_t)hal_get_cycles();
381if( DEBUG_DEVFS_INIT < cycle )
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 );
384#endif
385            }
386        }
387    }
388
389    // create a NIC_TX inode in each cluster containing a NIC_TX chdev
390    for( channel = 0 ; channel < CONFIG_MAX_NIC_CHANNELS ; channel++ )
391    {
392        chdev_xp = chdev_dir.nic_tx[channel];
393        if( chdev_xp != XPTR_NULL )
394        {
395            chdev_cxy = GET_CXY( chdev_xp );
396            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
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,
403                                         chdev_ptr->name,
404                                         GET_PTR( chdev_xp ),
405                                         &inode_xp );
406#if DEBUG_DEVFS_INIT
407cycle = (uint32_t)hal_get_cycles();
408if( DEBUG_DEVFS_INIT < cycle )
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 );
411#endif
412            }
413        }
414    }
415}  // end devfs_local_init()
416
417//////////////////////////////////////////
418int devfs_user_move( bool_t     to_buffer,
419                     xptr_t     file_xp,
420                     char     * u_buf,
421                     uint32_t   size )
422{
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;
432
433    char             k_buf[CONFIG_TXT_KBUF_SIZE];  // local kernel buffer
434
435assert( ( file_xp != XPTR_NULL ) , "file_xp == XPTR_NULL" );
436
437#if (DEBUG_SYS_READ & 1)
438enter_devfs_read = hal_time_stamp();
439#endif
440
441#if (DEBUG_SYS_WRITE & 1)
442enter_devfs_write = hal_time_stamp();
443#endif
444
445#if DEBUG_DEVFS_MOVE
446uint32_t   cycle = (uint32_t)hal_get_cycles();
447thread_t * this  = CURRENT_THREAD;
448if( DEBUG_DEVFS_MOVE < cycle )
449printk("\n[%s] thread[%x,%x] enter / to_mem %d / cycle %d\n",
450__FUNCTION__, this->process->pid, this->trdid, to_buffer, cycle );
451#endif
452
453    // get extended pointer on chdev_xp
454    chdev_xp = chdev_from_file( file_xp );
455
456    // get cluster and local pointer on chdev
457    chdev_cxy  = GET_CXY( chdev_xp );
458    chdev_ptr  = (chdev_t *)GET_PTR( chdev_xp );
459
460    // get chdev functionnal type and channel
461    func    = hal_remote_l32( XPTR( chdev_cxy , &chdev_ptr->func ) );
462    channel = hal_remote_l32( XPTR( chdev_cxy , &chdev_ptr->channel ) );
463
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++ )
480            {
481                error = dev_txt_read( channel , &k_buf[i] );
482
483                if( error )  return -1;
484            }
485
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
494#if DEBUG_DEVFS_MOVE
495cycle = (uint32_t)hal_get_cycles();
496if( DEBUG_DEVFS_MOVE < cycle )
497printk("\n[%s] thread[%x,%x] exit / to_mem %d / cycle %d\n",
498__FUNCTION__, this->process->pid, this->trdid, to_buffer, cycle );
499#endif
500
501#if (DEBUG_SYS_READ & 1)
502exit_devfs_read = hal_time_stamp();
503#endif
504            return size;
505    } 
506    ///////////// TXT write 
507    else               
508    {
509        while( todo )
510        {
511            // set burst size
512            if( todo > CONFIG_TXT_KBUF_SIZE ) burst = CONFIG_TXT_KBUF_SIZE;
513            else                              burst = todo;
514
515            // move burst bytes from u_buf to k_buf
516            hal_strcpy_from_uspace( k_buf , u_buf , burst );
517
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
528#if DEBUG_DEVFS_MOVE
529cycle = (uint32_t)hal_get_cycles();
530if( DEBUG_DEVFS_MOVE < cycle )
531printk("\n[%s] thread[%x,%x] exit / to_mem %d / cycle %d\n",
532__FUNCTION__, this->process->pid, this->trdid, to_buffer, cycle );
533#endif
534
535#if (DEBUG_SYS_WRITE & 1)
536exit_devfs_write = hal_time_stamp();
537#endif
538            return size;
539    }
540
541}  // end devfs_user_move()
542
543
Note: See TracBrowser for help on using the repository browser.