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

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

miscelaneous...

File size: 15.8 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
[446]200    // take the lock protecting chdev waiting queue
[440]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
[446]206    // unblock server thread if required
[440]207    if( hal_remote_lw( blocked_xp ) & THREAD_BLOCKED_IDLE )
208    thread_unblock( server_xp , THREAD_BLOCKED_IDLE );
209
[5]210    // register client thread in waiting queue
[407]211    xlist_add_last( root_xp , list_xp );
[5]212
[440]213    // send IPI to core running the server thread when server != client
214    different = (lid != this->core->lid) || (local_cxy != chdev_cxy);
[408]215    if( different ) dev_pic_send_ipi( chdev_cxy , lid ); 
[407]216   
[440]217    // release lock
218    remote_spinlock_unlock( lock_xp );
219
[408]220    // deschedule
221    assert( thread_can_yield( this ) , __FUNCTION__ , "illegal sched_yield\n" );
222    sched_yield("blocked on I/O");
[407]223
224    // exit critical section
225    hal_restore_irq( save_sr );
226
[438]227#if DEBUG_CHDEV_CMD_RX
[437]228rx_cycle = (uint32_t)hal_get_cycles();
[438]229if( (is_rx) && (DEBUG_CHDEV_CMD_RX < rx_cycle) )
[437]230printk("\n[DBG] %s : client_thread %x (%s) exit for RX / cycle %d\n",
231__FUNCTION__, this, thread_type_str(this->type) , rx_cycle );
[433]232#endif
233
[438]234#if DEBUG_CHDEV_CMD_TX
[437]235tx_cycle = (uint32_t)hal_get_cycles();
[438]236if( (is_rx == 0) && (DEBUG_CHDEV_CMD_TX < tx_cycle) )
[437]237printk("\n[DBG] %s : client_thread %x (%s) exit for TX / cycle %d\n",
238__FUNCTION__, this, thread_type_str(this->type) , tx_cycle );
239#endif
240
[438]241#if (DEBUG_SYS_READ & 1)
[435]242exit_chdev_cmd_read = (uint32_t)hal_get_cycles();
[418]243#endif
244
[438]245#if (DEBUG_SYS_WRITE & 1)
[435]246exit_chdev_cmd_write = (uint32_t)hal_get_cycles();
247#endif
248
[5]249}  // end chdev_register_command()
250
251///////////////////////////////////////////////
252void chdev_sequencial_server( chdev_t * chdev )
253{
254    xptr_t          client_xp;    // extended pointer on waiting thread
255    cxy_t           client_cxy;   // cluster of client thread
256    thread_t      * client_ptr;   // local pointer on client thread
[407]257    thread_t      * server;       // local pointer on server thread
[5]258    xptr_t          root_xp;      // extended pointer on device waiting queue root
[407]259    xptr_t          lock_xp;      // extended pointer on lock ptotecting chdev queue
[5]260
261    server = CURRENT_THREAD;
262
[437]263    // get root and lock on command queue
[5]264    root_xp = XPTR( local_cxy , &chdev->wait_root );
[407]265    lock_xp = XPTR( local_cxy , &chdev->wait_lock );
[5]266
[407]267        // This infinite loop is executed by the DEV thread
268    // to handle commands registered in the chdev queue.
[5]269    while( 1 )
270    {
[407]271        // get the lock protecting the waiting queue
272        remote_spinlock_lock( lock_xp );
273
[5]274        // check waiting queue state
[407]275        if( xlist_is_empty( root_xp ) ) // waiting queue empty
[5]276        {
277            // release lock
[407]278            remote_spinlock_unlock( lock_xp );
[5]279
[440]280            // block
281            thread_block( XPTR( local_cxy , server ) , THREAD_BLOCKED_IDLE ); 
282
[408]283            // deschedule
[440]284            assert( thread_can_yield( server ) , __FUNCTION__ , "illegal sched_yield\n" );
[408]285            sched_yield("I/O queue empty");
[5]286        } 
[407]287        else                            // waiting queue not empty
[5]288        {
[407]289            // get extended pointer on first client thread
290            client_xp = XLIST_FIRST_ELEMENT( root_xp , thread_t , wait_list );
[5]291
[440]292            // get client thread cluster and local pointer
[407]293            client_cxy = GET_CXY( client_xp );
[440]294            client_ptr = GET_PTR( client_xp );
[407]295
[440]296            // remove this first client thread from waiting queue
297            xlist_unlink( XPTR( client_cxy , &client_ptr->wait_list ) );
298
299            // release lock
300            remote_spinlock_unlock( lock_xp );
301
[438]302#if DEBUG_CHDEV_SERVER_RX
[437]303uint32_t rx_cycle = (uint32_t)hal_get_cycles();
[438]304if( (chdev->is_rx) && (DEBUG_CHDEV_SERVER_RX < rx_cycle) )
[437]305printk("\n[DBG] %s : server_thread %x start RX / client %x / cycle %d\n",
306__FUNCTION__ , server , client_ptr , rx_cycle );
307#endif
308
[438]309#if DEBUG_CHDEV_SERVER_TX
[437]310uint32_t tx_cycle = (uint32_t)hal_get_cycles();
[438]311if( (chdev->is_rx == 0) && (DEBUG_CHDEV_SERVER_TX < tx_cycle) )
[437]312printk("\n[DBG] %s : server_thread %x start TX / client %x / cycle %d\n",
313__FUNCTION__ , server , client_ptr , tx_cycle );
314#endif
315
[438]316#if (DEBUG_SYS_READ & 1)
[437]317enter_chdev_server_read = (uint32_t)hal_get_cycles();
318#endif
319
[438]320#if (DEBUG_SYS_WRITE & 1)
[437]321enter_chdev_server_write = (uint32_t)hal_get_cycles();
322#endif
323
[407]324            // call driver command function to execute I/O operation
325            chdev->cmd( client_xp );
[5]326       
[418]327            // unblock client thread
328            thread_unblock( client_xp , THREAD_BLOCKED_IO );
329
[438]330#if DEBUG_CHDEV_SERVER_RX
[437]331rx_cycle = (uint32_t)hal_get_cycles();
[438]332if( (chdev->is_rx) && (DEBUG_CHDEV_SERVER_RX < rx_cycle) )
[437]333printk("\n[DBG] %s : server_thread %x completes RX / client %x / cycle %d\n",
334__FUNCTION__ , server , client_ptr , rx_cycle );
[433]335#endif
[5]336
[438]337#if DEBUG_CHDEV_SERVER_TX
[437]338tx_cycle = (uint32_t)hal_get_cycles();
[438]339if( (chdev->is_rx == 0) && (DEBUG_CHDEV_SERVER_TX < tx_cycle) )
[437]340printk("\n[DBG] %s : server_thread %x completes TX / client %x / cycle %d\n",
341__FUNCTION__ , server , client_ptr , tx_cycle );
342#endif
343
[438]344#if (DEBUG_SYS_READ & 1)
[435]345exit_chdev_server_read = (uint32_t)hal_get_cycles();
[407]346#endif
347
[438]348#if (DEBUG_SYS_WRITE & 1)
[435]349exit_chdev_server_write = (uint32_t)hal_get_cycles();
350#endif
351
[407]352        }
[5]353    }  // end while
354}  // end chdev_sequencial_server()
355
[428]356////////////////////////////////////////
357xptr_t chdev_from_file( xptr_t file_xp )
358{
359    cxy_t         file_cxy;
360    vfs_file_t  * file_ptr;
361    uint32_t      inode_type;
362    vfs_inode_t * inode_ptr;
363    chdev_t     * chdev_ptr;
364
[440]365    assert( (file_xp != XPTR_NULL) , __FUNCTION__,
366    "file_xp == XPTR_NULL\n" );
367
[428]368    // get cluster and local pointer on remote file descriptor
369    // associated inode and chdev are stored in same cluster as the file desc.
370    file_cxy  = GET_CXY( file_xp );
371    file_ptr  = (vfs_file_t *)GET_PTR( file_xp );
372
373    // get inode type from file descriptor
374    inode_type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) );
375    inode_ptr  = (vfs_inode_t *)hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) );
376
377    assert( (inode_type == INODE_TYPE_DEV) , __FUNCTION__ ,
[440]378    "inode type %d is not INODE_TYPE_DEV\n", inode_type );
[428]379
380    // get chdev local pointer from inode extension
381    chdev_ptr = (chdev_t *)hal_remote_lpt( XPTR( file_cxy , &inode_ptr->extend ) );
382
383    return XPTR( file_cxy , chdev_ptr );
384
385}  // end chdev_from_file()
386
[317]387////////////////////////
388void chdev_dir_display()
389{
[428]390    uint32_t  i;
391    cxy_t     cxy;
392    chdev_t * ptr;
393    uint32_t  base;
394    reg_t     save_sr;
[317]395
[428]396    // get pointers on TXT0 chdev
397    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
398    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
399    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
[317]400
[428]401    // get extended pointer on remote TXT0 chdev lock
402    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
[317]403
[428]404    // get TXT0 lock in busy waiting mode
405    remote_spinlock_lock_busy( lock_xp , &save_sr );
[317]406
[428]407    // header
408    nolock_printk("\n***** external chdevs directory *****\n");
[317]409
[428]410    // IOB
411    cxy  = GET_CXY( chdev_dir.iob );
412    ptr  = GET_PTR( chdev_dir.iob );
413    base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
414    nolock_printk("  - iob       : cxy = %X / ptr = %X / base = %X\n", cxy, ptr, base);
[407]415
[428]416    // PIC
417    cxy  = GET_CXY( chdev_dir.pic );
418    ptr  = GET_PTR( chdev_dir.pic );
419    base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
420    nolock_printk("  - pic       : cxy = %X / ptr = %X / base = %X\n", cxy, ptr, base);
[407]421
[428]422    // TXT
423    for( i = 0 ; i < LOCAL_CLUSTER->nb_txt_channels ; i++ )
424    {
425        cxy = GET_CXY( chdev_dir.txt_rx[i] );
426        ptr = GET_PTR( chdev_dir.txt_rx[i] );
427        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
428        nolock_printk("  - txt_rx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
[407]429
[428]430        cxy = GET_CXY( chdev_dir.txt_tx[i] );
431        ptr = GET_PTR( chdev_dir.txt_tx[i] );
432        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
433        nolock_printk("  - txt_tx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
434    }
[317]435
[428]436    // IOC
437    for( i = 0 ; i < LOCAL_CLUSTER->nb_ioc_channels ; i++ )
438    {
439        cxy = GET_CXY( chdev_dir.ioc[i] );
440        ptr = GET_PTR( chdev_dir.ioc[i] );
441        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
442        nolock_printk("  - ioc[%d]    : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
443    }
[317]444
[428]445    // FBF
446    for( i = 0 ; i < LOCAL_CLUSTER->nb_fbf_channels ; i++ )
447    {
448        cxy  = GET_CXY( chdev_dir.fbf[i] );
449        ptr  = GET_PTR( chdev_dir.fbf[i] );
450        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
451        nolock_printk("  - fbf[%d]    : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
452    }
[317]453
[428]454    // NIC
455    for( i = 0 ; i < LOCAL_CLUSTER->nb_nic_channels ; i++ )
456    {
457        cxy = GET_CXY( chdev_dir.nic_rx[i] );
458        ptr = GET_PTR( chdev_dir.nic_rx[i] );
459        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
460        nolock_printk("  - nic_rx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
[317]461
[428]462        cxy = GET_CXY( chdev_dir.nic_tx[i] );
463        ptr = GET_PTR( chdev_dir.nic_tx[i] );
464        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
465        nolock_printk("  - nic_tx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
466    }
[317]467
[428]468    // release lock
469    remote_spinlock_unlock_busy( lock_xp , save_sr );
470
[317]471}  // end chdev_dir_display()
472
Note: See TracBrowser for help on using the repository browser.