source: trunk/kernel/kern/chdev.c @ 440

Last change on this file since 440 was 440, checked in by alain, 6 years ago

1/ Fix a bug in the Multithreaded "sort" applicationr:
The pthread_create() arguments must be declared as global variables.
2/ The exit syscall can be called by any thread of a process..

File size: 15.7 KB
RevLine 
[5]1/*
2 * chdev.c - channel device descriptor operations implementation.
3 *
4 * Authors  Alain Greiner   (2016)
5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH.is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH.is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH.; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
[14]24#include <kernel_config.h>
[5]25#include <hal_types.h>
26#include <hal_special.h>
[407]27#include <hal_irqmask.h>
[16]28#include <printk.h>
[5]29#include <boot_info.h>
30#include <xlist.h>
31#include <kmem.h>
[407]32#include <scheduler.h>
[5]33#include <thread.h>
34#include <rpc.h>
35#include <chdev.h>
[23]36#include <devfs.h>
[5]37
[317]38
39extern chdev_directory_t    chdev_dir;   // allocated in kernel_init.c
40
[438]41#if (DEBUG_SYS_READ & 1)
[435]42extern uint32_t enter_chdev_cmd_read;
43extern uint32_t exit_chdev_cmd_read;
44extern uint32_t enter_chdev_server_read;
45extern uint32_t exit_chdev_server_read;
[407]46#endif
[317]47
[438]48#if (DEBUG_SYS_WRITE & 1)
[435]49extern uint32_t enter_chdev_cmd_write;
50extern uint32_t exit_chdev_cmd_write;
51extern uint32_t enter_chdev_server_write;
52extern uint32_t exit_chdev_server_write;
53#endif
54
[5]55////////////////////////////////////////////
56char * chdev_func_str( uint32_t func_type ) 
57{
58        if     ( func_type == DEV_FUNC_RAM ) return "RAM";
59        else if( func_type == DEV_FUNC_ROM ) return "ROM";
60        else if( func_type == DEV_FUNC_FBF ) return "FBF";
61        else if( func_type == DEV_FUNC_IOB ) return "IOB";
62        else if( func_type == DEV_FUNC_IOC ) return "IOC";
63        else if( func_type == DEV_FUNC_MMC ) return "MMC";
64        else if( func_type == DEV_FUNC_DMA ) return "DMA";
65        else if( func_type == DEV_FUNC_NIC ) return "NIC";
66        else if( func_type == DEV_FUNC_TIM ) return "TIM";
67        else if( func_type == DEV_FUNC_TXT ) return "TXT";
68        else if( func_type == DEV_FUNC_ICU ) return "ICU";
69        else if( func_type == DEV_FUNC_PIC ) return "PIC";
[16]70    else                                 return "undefined";
[5]71}
72
73/////////////////////////////////////////
74chdev_t * chdev_create( uint32_t    func,
75                        uint32_t    impl,
76                        uint32_t    channel,
77                        uint32_t    is_rx,
78                        xptr_t      base )
79{
80    chdev_t    * chdev;
81    kmem_req_t   req;
82
83    // allocate memory for chdev
84    req.type   = KMEM_DEVICE;
85    req.flags  = AF_ZERO;
86    chdev      = (chdev_t *)kmem_alloc( &req );
87
88    if( chdev == NULL ) return NULL;
89
90    // initialize waiting threads queue and associated lock
91    remote_spinlock_init( XPTR( local_cxy , &chdev->wait_lock ) );
92    xlist_root_init( XPTR( local_cxy , &chdev->wait_root ) );
93
94    // initialize attributes
95    chdev->func    =  func;
96    chdev->impl    =  impl;
97    chdev->channel =  channel;
98    chdev->is_rx   =  is_rx;
99    chdev->base    =  base; 
100
101    return chdev;
102
103}  // end chdev_create()
104
105///////////////////////////////////
106void chdev_print( chdev_t * chdev )
107{
108    printk("\n - func      = %s"
109           "\n - channel   = %d"
110           "\n - base      = %l"
111           "\n - cmd       = %x"
112           "\n - isr       = %x"
113           "\n - chdev     = %x\n",
114           chdev_func_str(chdev->func),
115           chdev->channel,
116           chdev->base,
117           chdev->cmd,
118           chdev->isr,
119           chdev );
120}
121
[407]122//////////////////////////////////////////////////
123void chdev_register_command( xptr_t     chdev_xp )
[5]124{
[407]125    thread_t * server_ptr;    // local pointer on server thread associated to chdev
[440]126    xptr_t     server_xp;     // extended pointer on server thread
[407]127    core_t   * core_ptr;      // local pointer on core running the server thread
128    uint32_t   lid;           // core running the server thread local index
129    xptr_t     lock_xp;       // extended pointer on lock protecting the chdev queue
[408]130    uint32_t   different;     // non zero if server thread core != client thread core
[407]131    uint32_t   save_sr;       // for critical section
[5]132
[438]133#if (DEBUG_SYS_READ & 1)
[435]134enter_chdev_cmd_read = (uint32_t)hal_get_cycles();
[418]135#endif
136
[438]137#if (DEBUG_SYS_WRITE & 1)
[435]138enter_chdev_cmd_write = (uint32_t)hal_get_cycles();
139#endif
140
[407]141    thread_t * this = CURRENT_THREAD;
142
[440]143    // get chdev cluster and local pointer
[5]144    cxy_t     chdev_cxy = GET_CXY( chdev_xp );
[440]145    chdev_t * chdev_ptr = GET_PTR( chdev_xp );
[5]146
[440]147    // get local and extended pointers on server thread
148    server_ptr = (thread_t *)hal_remote_lpt( XPTR( chdev_cxy , &chdev_ptr->server) );
149    server_xp  = XPTR( chdev_cxy , server_ptr );
150
151    // get local pointer on core running the server thread
152    core_ptr   = (core_t *)hal_remote_lpt( XPTR( chdev_cxy , &server_ptr->core ) );
153
154    // get server core local index
155    lid = hal_remote_lw( XPTR( chdev_cxy , &core_ptr->lid ) );
156
[438]157#if (DEBUG_CHDEV_CMD_RX || DEBUG_CHDEV_CMD_TX)
[437]158bool_t is_rx = hal_remote_lw( XPTR( chdev_cxy , &chdev_ptr->is_rx ) );
159#endif
160   
[438]161#if DEBUG_CHDEV_CMD_RX
[437]162uint32_t rx_cycle = (uint32_t)hal_get_cycles();
[438]163if( (is_rx) && (DEBUG_CHDEV_CMD_RX < rx_cycle) )
[437]164printk("\n[DBG] %s : client_thread %x (%s) enter for RX / cycle %d\n",
165__FUNCTION__, this, thread_type_str(this->type) , rx_cycle );
166#endif
167
[438]168#if DEBUG_CHDEV_CMD_TX
[437]169uint32_t tx_cycle = (uint32_t)hal_get_cycles();
[438]170if( (is_rx == 0) && (DEBUG_CHDEV_CMD_TX < tx_cycle) )
[437]171printk("\n[DBG] %s : client_thread %x (%s) enter for TX / cycle %d\n",
172__FUNCTION__, this, thread_type_str(this->type) , tx_cycle );
173#endif
174
[440]175    // build extended pointer on client thread xlist
176    xptr_t  list_xp    = XPTR( local_cxy , &this->wait_list );
[5]177
[440]178    // build extended pointer on chdev waiting queue root
179    xptr_t  root_xp    = XPTR( chdev_cxy , &chdev_ptr->wait_root );
[5]180
[440]181    // build extended pointer on server thread blocked state
182    xptr_t  blocked_xp = XPTR( chdev_cxy , &server_ptr->blocked );
183
184    // build extended pointer on lock protecting chdev waiting queue
[407]185    lock_xp = XPTR( chdev_cxy , &chdev_ptr->wait_lock );
186
[440]187    // critical section for the following sequence:
188    // (1) take the lock protecting waiting queue
189    // (2) block the client thread
190    // (3) unblock the server thread if required
191    // (4) register client thread in server queue
192    // (5) send IPI to force server scheduling
193    // (6) release the lock protecting waiting queue
194    // (7) deschedule
195    // ... in this order
[407]196
[440]197    // enter critical section
198    hal_disable_irq( &save_sr );
[407]199
[440]200    // take the lock
201    remote_spinlock_lock( lock_xp );
[408]202
203    // block current thread
[436]204    thread_block( XPTR( local_cxy , CURRENT_THREAD ) , THREAD_BLOCKED_IO );
[408]205
[440]206    if( hal_remote_lw( blocked_xp ) & THREAD_BLOCKED_IDLE )
207    thread_unblock( server_xp , THREAD_BLOCKED_IDLE );
208
[5]209    // register client thread in waiting queue
[407]210    xlist_add_last( root_xp , list_xp );
[5]211
[440]212    // send IPI to core running the server thread when server != client
213    different = (lid != this->core->lid) || (local_cxy != chdev_cxy);
[408]214    if( different ) dev_pic_send_ipi( chdev_cxy , lid ); 
[407]215   
[440]216    // release lock
217    remote_spinlock_unlock( lock_xp );
218
[408]219    // deschedule
220    assert( thread_can_yield( this ) , __FUNCTION__ , "illegal sched_yield\n" );
221    sched_yield("blocked on I/O");
[407]222
223    // exit critical section
224    hal_restore_irq( save_sr );
225
[438]226#if DEBUG_CHDEV_CMD_RX
[437]227rx_cycle = (uint32_t)hal_get_cycles();
[438]228if( (is_rx) && (DEBUG_CHDEV_CMD_RX < rx_cycle) )
[437]229printk("\n[DBG] %s : client_thread %x (%s) exit for RX / cycle %d\n",
230__FUNCTION__, this, thread_type_str(this->type) , rx_cycle );
[433]231#endif
232
[438]233#if DEBUG_CHDEV_CMD_TX
[437]234tx_cycle = (uint32_t)hal_get_cycles();
[438]235if( (is_rx == 0) && (DEBUG_CHDEV_CMD_TX < tx_cycle) )
[437]236printk("\n[DBG] %s : client_thread %x (%s) exit for TX / cycle %d\n",
237__FUNCTION__, this, thread_type_str(this->type) , tx_cycle );
238#endif
239
[438]240#if (DEBUG_SYS_READ & 1)
[435]241exit_chdev_cmd_read = (uint32_t)hal_get_cycles();
[418]242#endif
243
[438]244#if (DEBUG_SYS_WRITE & 1)
[435]245exit_chdev_cmd_write = (uint32_t)hal_get_cycles();
246#endif
247
[5]248}  // end chdev_register_command()
249
250///////////////////////////////////////////////
251void chdev_sequencial_server( chdev_t * chdev )
252{
253    xptr_t          client_xp;    // extended pointer on waiting thread
254    cxy_t           client_cxy;   // cluster of client thread
255    thread_t      * client_ptr;   // local pointer on client thread
[407]256    thread_t      * server;       // local pointer on server thread
[5]257    xptr_t          root_xp;      // extended pointer on device waiting queue root
[407]258    xptr_t          lock_xp;      // extended pointer on lock ptotecting chdev queue
[5]259
260    server = CURRENT_THREAD;
261
[437]262    // get root and lock on command queue
[5]263    root_xp = XPTR( local_cxy , &chdev->wait_root );
[407]264    lock_xp = XPTR( local_cxy , &chdev->wait_lock );
[5]265
[407]266        // This infinite loop is executed by the DEV thread
267    // to handle commands registered in the chdev queue.
[5]268    while( 1 )
269    {
[407]270        // get the lock protecting the waiting queue
271        remote_spinlock_lock( lock_xp );
272
[5]273        // check waiting queue state
[407]274        if( xlist_is_empty( root_xp ) ) // waiting queue empty
[5]275        {
276            // release lock
[407]277            remote_spinlock_unlock( lock_xp );
[5]278
[440]279            // block
280            thread_block( XPTR( local_cxy , server ) , THREAD_BLOCKED_IDLE ); 
281
[408]282            // deschedule
[440]283            assert( thread_can_yield( server ) , __FUNCTION__ , "illegal sched_yield\n" );
[408]284            sched_yield("I/O queue empty");
[5]285        } 
[407]286        else                            // waiting queue not empty
[5]287        {
[407]288            // get extended pointer on first client thread
289            client_xp = XLIST_FIRST_ELEMENT( root_xp , thread_t , wait_list );
[5]290
[440]291            // get client thread cluster and local pointer
[407]292            client_cxy = GET_CXY( client_xp );
[440]293            client_ptr = GET_PTR( client_xp );
[407]294
[440]295            // remove this first client thread from waiting queue
296            xlist_unlink( XPTR( client_cxy , &client_ptr->wait_list ) );
297
298            // release lock
299            remote_spinlock_unlock( lock_xp );
300
[438]301#if DEBUG_CHDEV_SERVER_RX
[437]302uint32_t rx_cycle = (uint32_t)hal_get_cycles();
[438]303if( (chdev->is_rx) && (DEBUG_CHDEV_SERVER_RX < rx_cycle) )
[437]304printk("\n[DBG] %s : server_thread %x start RX / client %x / cycle %d\n",
305__FUNCTION__ , server , client_ptr , rx_cycle );
306#endif
307
[438]308#if DEBUG_CHDEV_SERVER_TX
[437]309uint32_t tx_cycle = (uint32_t)hal_get_cycles();
[438]310if( (chdev->is_rx == 0) && (DEBUG_CHDEV_SERVER_TX < tx_cycle) )
[437]311printk("\n[DBG] %s : server_thread %x start TX / client %x / cycle %d\n",
312__FUNCTION__ , server , client_ptr , tx_cycle );
313#endif
314
[438]315#if (DEBUG_SYS_READ & 1)
[437]316enter_chdev_server_read = (uint32_t)hal_get_cycles();
317#endif
318
[438]319#if (DEBUG_SYS_WRITE & 1)
[437]320enter_chdev_server_write = (uint32_t)hal_get_cycles();
321#endif
322
[407]323            // call driver command function to execute I/O operation
324            chdev->cmd( client_xp );
[5]325       
[418]326            // unblock client thread
327            thread_unblock( client_xp , THREAD_BLOCKED_IO );
328
[438]329#if DEBUG_CHDEV_SERVER_RX
[437]330rx_cycle = (uint32_t)hal_get_cycles();
[438]331if( (chdev->is_rx) && (DEBUG_CHDEV_SERVER_RX < rx_cycle) )
[437]332printk("\n[DBG] %s : server_thread %x completes RX / client %x / cycle %d\n",
333__FUNCTION__ , server , client_ptr , rx_cycle );
[433]334#endif
[5]335
[438]336#if DEBUG_CHDEV_SERVER_TX
[437]337tx_cycle = (uint32_t)hal_get_cycles();
[438]338if( (chdev->is_rx == 0) && (DEBUG_CHDEV_SERVER_TX < tx_cycle) )
[437]339printk("\n[DBG] %s : server_thread %x completes TX / client %x / cycle %d\n",
340__FUNCTION__ , server , client_ptr , tx_cycle );
341#endif
342
[438]343#if (DEBUG_SYS_READ & 1)
[435]344exit_chdev_server_read = (uint32_t)hal_get_cycles();
[407]345#endif
346
[438]347#if (DEBUG_SYS_WRITE & 1)
[435]348exit_chdev_server_write = (uint32_t)hal_get_cycles();
349#endif
350
[407]351        }
[5]352    }  // end while
353}  // end chdev_sequencial_server()
354
[428]355////////////////////////////////////////
356xptr_t chdev_from_file( xptr_t file_xp )
357{
358    cxy_t         file_cxy;
359    vfs_file_t  * file_ptr;
360    uint32_t      inode_type;
361    vfs_inode_t * inode_ptr;
362    chdev_t     * chdev_ptr;
363
[440]364    assert( (file_xp != XPTR_NULL) , __FUNCTION__,
365    "file_xp == XPTR_NULL\n" );
366
[428]367    // get cluster and local pointer on remote file descriptor
368    // associated inode and chdev are stored in same cluster as the file desc.
369    file_cxy  = GET_CXY( file_xp );
370    file_ptr  = (vfs_file_t *)GET_PTR( file_xp );
371
372    // get inode type from file descriptor
373    inode_type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) );
374    inode_ptr  = (vfs_inode_t *)hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) );
375
376    assert( (inode_type == INODE_TYPE_DEV) , __FUNCTION__ ,
[440]377    "inode type %d is not INODE_TYPE_DEV\n", inode_type );
[428]378
379    // get chdev local pointer from inode extension
380    chdev_ptr = (chdev_t *)hal_remote_lpt( XPTR( file_cxy , &inode_ptr->extend ) );
381
382    return XPTR( file_cxy , chdev_ptr );
383
384}  // end chdev_from_file()
385
[317]386////////////////////////
387void chdev_dir_display()
388{
[428]389    uint32_t  i;
390    cxy_t     cxy;
391    chdev_t * ptr;
392    uint32_t  base;
393    reg_t     save_sr;
[317]394
[428]395    // get pointers on TXT0 chdev
396    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
397    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
398    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
[317]399
[428]400    // get extended pointer on remote TXT0 chdev lock
401    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
[317]402
[428]403    // get TXT0 lock in busy waiting mode
404    remote_spinlock_lock_busy( lock_xp , &save_sr );
[317]405
[428]406    // header
407    nolock_printk("\n***** external chdevs directory *****\n");
[317]408
[428]409    // IOB
410    cxy  = GET_CXY( chdev_dir.iob );
411    ptr  = GET_PTR( chdev_dir.iob );
412    base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
413    nolock_printk("  - iob       : cxy = %X / ptr = %X / base = %X\n", cxy, ptr, base);
[407]414
[428]415    // PIC
416    cxy  = GET_CXY( chdev_dir.pic );
417    ptr  = GET_PTR( chdev_dir.pic );
418    base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
419    nolock_printk("  - pic       : cxy = %X / ptr = %X / base = %X\n", cxy, ptr, base);
[407]420
[428]421    // TXT
422    for( i = 0 ; i < LOCAL_CLUSTER->nb_txt_channels ; i++ )
423    {
424        cxy = GET_CXY( chdev_dir.txt_rx[i] );
425        ptr = GET_PTR( chdev_dir.txt_rx[i] );
426        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
427        nolock_printk("  - txt_rx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
[407]428
[428]429        cxy = GET_CXY( chdev_dir.txt_tx[i] );
430        ptr = GET_PTR( chdev_dir.txt_tx[i] );
431        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
432        nolock_printk("  - txt_tx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
433    }
[317]434
[428]435    // IOC
436    for( i = 0 ; i < LOCAL_CLUSTER->nb_ioc_channels ; i++ )
437    {
438        cxy = GET_CXY( chdev_dir.ioc[i] );
439        ptr = GET_PTR( chdev_dir.ioc[i] );
440        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
441        nolock_printk("  - ioc[%d]    : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
442    }
[317]443
[428]444    // FBF
445    for( i = 0 ; i < LOCAL_CLUSTER->nb_fbf_channels ; i++ )
446    {
447        cxy  = GET_CXY( chdev_dir.fbf[i] );
448        ptr  = GET_PTR( chdev_dir.fbf[i] );
449        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
450        nolock_printk("  - fbf[%d]    : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
451    }
[317]452
[428]453    // NIC
454    for( i = 0 ; i < LOCAL_CLUSTER->nb_nic_channels ; i++ )
455    {
456        cxy = GET_CXY( chdev_dir.nic_rx[i] );
457        ptr = GET_PTR( chdev_dir.nic_rx[i] );
458        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
459        nolock_printk("  - nic_rx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
[317]460
[428]461        cxy = GET_CXY( chdev_dir.nic_tx[i] );
462        ptr = GET_PTR( chdev_dir.nic_tx[i] );
463        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
464        nolock_printk("  - nic_tx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
465    }
[317]466
[428]467    // release lock
468    remote_spinlock_unlock_busy( lock_xp , save_sr );
469
[317]470}  // end chdev_dir_display()
471
Note: See TracBrowser for help on using the repository browser.