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

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

blip

File size: 13.6 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
[407]41#if CONFIG_READ_DEBUG
42extern uint32_t enter_chdev_cmd;
43extern uint32_t exit_chdev_cmd;
44extern uint32_t enter_chdev_server;
45extern uint32_t exit_chdev_server;
46#endif
[317]47
[5]48////////////////////////////////////////////
49char * chdev_func_str( uint32_t func_type ) 
50{
51        if     ( func_type == DEV_FUNC_RAM ) return "RAM";
52        else if( func_type == DEV_FUNC_ROM ) return "ROM";
53        else if( func_type == DEV_FUNC_FBF ) return "FBF";
54        else if( func_type == DEV_FUNC_IOB ) return "IOB";
55        else if( func_type == DEV_FUNC_IOC ) return "IOC";
56        else if( func_type == DEV_FUNC_MMC ) return "MMC";
57        else if( func_type == DEV_FUNC_DMA ) return "DMA";
58        else if( func_type == DEV_FUNC_NIC ) return "NIC";
59        else if( func_type == DEV_FUNC_TIM ) return "TIM";
60        else if( func_type == DEV_FUNC_TXT ) return "TXT";
61        else if( func_type == DEV_FUNC_ICU ) return "ICU";
62        else if( func_type == DEV_FUNC_PIC ) return "PIC";
[16]63    else                                 return "undefined";
[5]64}
65
66/////////////////////////////////////////
67chdev_t * chdev_create( uint32_t    func,
68                        uint32_t    impl,
69                        uint32_t    channel,
70                        uint32_t    is_rx,
71                        xptr_t      base )
72{
73    chdev_t    * chdev;
74    kmem_req_t   req;
75
76    // allocate memory for chdev
77    req.type   = KMEM_DEVICE;
78    req.flags  = AF_ZERO;
79    chdev      = (chdev_t *)kmem_alloc( &req );
80
81    if( chdev == NULL ) return NULL;
82
83    // initialize waiting threads queue and associated lock
84    remote_spinlock_init( XPTR( local_cxy , &chdev->wait_lock ) );
85    xlist_root_init( XPTR( local_cxy , &chdev->wait_root ) );
86
87    // initialize attributes
88    chdev->func    =  func;
89    chdev->impl    =  impl;
90    chdev->channel =  channel;
91    chdev->is_rx   =  is_rx;
92    chdev->base    =  base; 
93
94    return chdev;
95
96}  // end chdev_create()
97
98///////////////////////////////////
99void chdev_print( chdev_t * chdev )
100{
101    printk("\n - func      = %s"
102           "\n - channel   = %d"
103           "\n - base      = %l"
104           "\n - cmd       = %x"
105           "\n - isr       = %x"
106           "\n - chdev     = %x\n",
107           chdev_func_str(chdev->func),
108           chdev->channel,
109           chdev->base,
110           chdev->cmd,
111           chdev->isr,
112           chdev );
113}
114
[407]115//////////////////////////////////////////////////
116void chdev_register_command( xptr_t     chdev_xp )
[5]117{
[407]118    thread_t * server_ptr;    // local pointer on server thread associated to chdev
119    core_t   * core_ptr;      // local pointer on core running the server thread
120    uint32_t   lid;           // core running the server thread local index
121    xptr_t     lock_xp;       // extended pointer on lock protecting the chdev queue
[408]122    uint32_t   different;     // non zero if server thread core != client thread core
[407]123    uint32_t   save_sr;       // for critical section
[5]124
[418]125#if CONFIG_READ_DEBUG
126enter_chdev_cmd = hal_time_stamp();
127#endif
128
[407]129    thread_t * this = CURRENT_THREAD;
130
[433]131#if CONFIG_DEBUG_CHDEV_REGISTER_COMMAND
132uint32_t cycle = (uint32_t)hal_get_cycles();
133if( CONFIG_DEBUG_CHDEV_REGISTER_COMMAND < cycle )
134printk("\n[DBG] %s : client_thread %x (%s) enter / cycle %d\n",
135__FUNCTION__, this, thread_type_str(this->type) , cycle );
136#endif
[407]137
[5]138    // get device descriptor cluster and local pointer
139    cxy_t     chdev_cxy = GET_CXY( chdev_xp );
140    chdev_t * chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
141
142    // build extended pointers on client thread xlist and device root
[407]143    xptr_t  list_xp = XPTR( local_cxy , &this->wait_list );
144    xptr_t  root_xp = XPTR( chdev_cxy , &chdev_ptr->wait_root );
[5]145
[407]146    // get local pointer on server thread
147    server_ptr = (thread_t *)hal_remote_lpt( XPTR( chdev_cxy , &chdev_ptr->server) );
[5]148
[407]149    // build extended pointer on chdev lock protecting queue
150    lock_xp = XPTR( chdev_cxy , &chdev_ptr->wait_lock );
151
152    // get local pointer on core running the server thread
153    core_ptr = (core_t *)hal_remote_lpt( XPTR( chdev_cxy , &server_ptr->core ) );
154
155    // get core local index
156    lid = hal_remote_lw( XPTR( chdev_cxy , &core_ptr->lid ) );
157
[408]158    // compute server core != thread core
159    different = (lid != this->core->lid) || (local_cxy != chdev_cxy);
160
161    // enter critical section to make atomic :
162    // (1) client blocking
163    // (2) client registration in server queue
164    // (3) IPI to force server scheduling
165    // (4) descheduling
166    // ... in this order
[407]167    hal_disable_irq( &save_sr );
168
[408]169    // block current thread
170    thread_block( CURRENT_THREAD , THREAD_BLOCKED_IO );
171
[5]172    // register client thread in waiting queue
[407]173    remote_spinlock_lock( lock_xp );
174    xlist_add_last( root_xp , list_xp );
175    remote_spinlock_unlock( lock_xp );
[5]176
[408]177    // send IPI to core running the server thread if required
178    if( different ) dev_pic_send_ipi( chdev_cxy , lid ); 
[407]179   
[433]180#if CONFIG_DEBUG_CHDEV_REGISTER_COMMAND
181cycle = (uint32_t)hal_get_cycles();
182if( CONFIG_DEBUG_CHDEV_REGISTER_COMMAND < cycle )
183printk("\n[DBG] %s : client_thread %x (%s) exit / cycle %d\n",
184__FUNCTION__, this, thread_type_str(this->type) , cycle );
185#endif
[5]186
[408]187    // deschedule
188    assert( thread_can_yield( this ) , __FUNCTION__ , "illegal sched_yield\n" );
189    sched_yield("blocked on I/O");
[407]190
191    // exit critical section
192    hal_restore_irq( save_sr );
193
[433]194#if CONFIG_DEBUG_CHDEV_REGISTER_COMMAND
195cycle = (uint32_t)hal_get_cycles();
196if( CONFIG_DEBUG_CHDEV_REGISTER_COMMAND < cycle )
197printk("\n[DBG] %s : client_thread %x (%s) resumes / cycle %d\n",
198__FUNCTION__, this, thread_type_str(this->type) , cycle );
199#endif
200
[418]201#if CONFIG_READ_DEBUG
202exit_chdev_cmd = hal_time_stamp();
203#endif
204
[5]205}  // end chdev_register_command()
206
207///////////////////////////////////////////////
208void chdev_sequencial_server( chdev_t * chdev )
209{
210    xptr_t          client_xp;    // extended pointer on waiting thread
211    cxy_t           client_cxy;   // cluster of client thread
212    thread_t      * client_ptr;   // local pointer on client thread
[407]213    thread_t      * server;       // local pointer on server thread
[5]214    xptr_t          root_xp;      // extended pointer on device waiting queue root
[407]215    xptr_t          lock_xp;      // extended pointer on lock ptotecting chdev queue
[5]216
217    server = CURRENT_THREAD;
218
[433]219#if CONFIG_DEBUG_CHDEV_SEQUENCIAL_SERVER
220uint32_t cycle = (uint32_t)hal_get_cycles();
221if( CONFIG_DEBUG_CHDEV_SEQUENCIAL_SERVER < cycle )
222printk("\n[DBG] %s : server_thread %x enter / chdev = %x / cycle %d\n",
223__FUNCTION__ , server , chdev , cycle );
224#endif
[407]225
[5]226    root_xp = XPTR( local_cxy , &chdev->wait_root );
[407]227    lock_xp = XPTR( local_cxy , &chdev->wait_lock );
[5]228
[407]229        // This infinite loop is executed by the DEV thread
230    // to handle commands registered in the chdev queue.
[5]231    while( 1 )
232    {
[407]233        // get the lock protecting the waiting queue
234        remote_spinlock_lock( lock_xp );
235
[5]236        // check waiting queue state
[407]237        if( xlist_is_empty( root_xp ) ) // waiting queue empty
[5]238        {
239            // release lock
[407]240            remote_spinlock_unlock( lock_xp );
[5]241
[407]242chdev_dmsg("\n[DBG] %s : thread %x deschedule /cycle %d\n",
243__FUNCTION__ , server , hal_time_stamp() );
244
[408]245            // deschedule
246            sched_yield("I/O queue empty");
[407]247
248chdev_dmsg("\n[DBG] %s : thread %x resume /cycle %d\n",
249__FUNCTION__ , server , hal_time_stamp() );
250
[5]251        } 
[407]252        else                            // waiting queue not empty
[5]253        {
[407]254
255#if CONFIG_READ_DEBUG
256enter_chdev_server = hal_time_stamp();
257#endif
[5]258            // release lock
[407]259            remote_spinlock_unlock( lock_xp );
[5]260
[407]261            // get extended pointer on first client thread
262            client_xp = XLIST_FIRST_ELEMENT( root_xp , thread_t , wait_list );
[5]263
[407]264            // get client thread cluster, local pointer, and identifier
265            client_cxy = GET_CXY( client_xp );
266            client_ptr = (thread_t *)GET_PTR( client_xp );
267
268            // call driver command function to execute I/O operation
269            chdev->cmd( client_xp );
[5]270       
[407]271            // remove the client thread from waiting queue
272            remote_spinlock_lock( lock_xp );
273            xlist_unlink( XPTR( client_cxy , &client_ptr->wait_list ) );
274            remote_spinlock_unlock( lock_xp );
[5]275
[418]276            // unblock client thread
277            thread_unblock( client_xp , THREAD_BLOCKED_IO );
278
[433]279#if CONFIG_DEBUG_CHDEV_SEQUENCIAL_SERVER
280cycle = (uint32_t)hal_get_cycles();
281if( CONFIG_DEBUG_CHDEV_SEQUENCIAL_SERVER < cycle )
282printk("\n[DBG] %s : server_thread %x complete operation for client %x / cycle %d\n",
283__FUNCTION__ , server , client_ptr , cycle );
284#endif
[5]285
[407]286#if CONFIG_READ_DEBUG
287exit_chdev_server = hal_time_stamp();
288#endif
289
290        }
[5]291    }  // end while
292}  // end chdev_sequencial_server()
293
[428]294////////////////////////////////////////
295xptr_t chdev_from_file( xptr_t file_xp )
296{
297    cxy_t         file_cxy;
298    vfs_file_t  * file_ptr;
299    uint32_t      inode_type;
300    vfs_inode_t * inode_ptr;
301    chdev_t     * chdev_ptr;
302
303    // get cluster and local pointer on remote file descriptor
304    // associated inode and chdev are stored in same cluster as the file desc.
305    file_cxy  = GET_CXY( file_xp );
306    file_ptr  = (vfs_file_t *)GET_PTR( file_xp );
307
308    // get inode type from file descriptor
309    inode_type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) );
310    inode_ptr  = (vfs_inode_t *)hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) );
311
312    assert( (inode_type == INODE_TYPE_DEV) , __FUNCTION__ ,
313    "inode type %d is not INODE_TYPE_DEV", inode_type );
314
315    // get chdev local pointer from inode extension
316    chdev_ptr = (chdev_t *)hal_remote_lpt( XPTR( file_cxy , &inode_ptr->extend ) );
317
318    return XPTR( file_cxy , chdev_ptr );
319
320}  // end chdev_from_file()
321
[317]322////////////////////////
323void chdev_dir_display()
324{
[428]325    uint32_t  i;
326    cxy_t     cxy;
327    chdev_t * ptr;
328    uint32_t  base;
329    reg_t     save_sr;
[317]330
[428]331    // get pointers on TXT0 chdev
332    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
333    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
334    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
[317]335
[428]336    // get extended pointer on remote TXT0 chdev lock
337    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
[317]338
[428]339    // get TXT0 lock in busy waiting mode
340    remote_spinlock_lock_busy( lock_xp , &save_sr );
[317]341
[428]342    // header
343    nolock_printk("\n***** external chdevs directory *****\n");
[317]344
[428]345    // IOB
346    cxy  = GET_CXY( chdev_dir.iob );
347    ptr  = GET_PTR( chdev_dir.iob );
348    base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
349    nolock_printk("  - iob       : cxy = %X / ptr = %X / base = %X\n", cxy, ptr, base);
[407]350
[428]351    // PIC
352    cxy  = GET_CXY( chdev_dir.pic );
353    ptr  = GET_PTR( chdev_dir.pic );
354    base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
355    nolock_printk("  - pic       : cxy = %X / ptr = %X / base = %X\n", cxy, ptr, base);
[407]356
[428]357    // TXT
358    for( i = 0 ; i < LOCAL_CLUSTER->nb_txt_channels ; i++ )
359    {
360        cxy = GET_CXY( chdev_dir.txt_rx[i] );
361        ptr = GET_PTR( chdev_dir.txt_rx[i] );
362        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
363        nolock_printk("  - txt_rx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
[407]364
[428]365        cxy = GET_CXY( chdev_dir.txt_tx[i] );
366        ptr = GET_PTR( chdev_dir.txt_tx[i] );
367        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
368        nolock_printk("  - txt_tx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
369    }
[317]370
[428]371    // IOC
372    for( i = 0 ; i < LOCAL_CLUSTER->nb_ioc_channels ; i++ )
373    {
374        cxy = GET_CXY( chdev_dir.ioc[i] );
375        ptr = GET_PTR( chdev_dir.ioc[i] );
376        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
377        nolock_printk("  - ioc[%d]    : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
378    }
[317]379
[428]380    // FBF
381    for( i = 0 ; i < LOCAL_CLUSTER->nb_fbf_channels ; i++ )
382    {
383        cxy  = GET_CXY( chdev_dir.fbf[i] );
384        ptr  = GET_PTR( chdev_dir.fbf[i] );
385        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
386        nolock_printk("  - fbf[%d]    : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
387    }
[317]388
[428]389    // NIC
390    for( i = 0 ; i < LOCAL_CLUSTER->nb_nic_channels ; i++ )
391    {
392        cxy = GET_CXY( chdev_dir.nic_rx[i] );
393        ptr = GET_PTR( chdev_dir.nic_rx[i] );
394        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
395        nolock_printk("  - nic_rx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
[317]396
[428]397        cxy = GET_CXY( chdev_dir.nic_tx[i] );
398        ptr = GET_PTR( chdev_dir.nic_tx[i] );
399        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
400        nolock_printk("  - nic_tx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
401    }
[317]402
[428]403    // release lock
404    remote_spinlock_unlock_busy( lock_xp , save_sr );
405
[317]406}  // end chdev_dir_display()
407
Note: See TracBrowser for help on using the repository browser.