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

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

1) improve the threads and process destruction mechanism.
2) introduce FIFOs in the soclib_tty driver.

File size: 13.8 KB
Line 
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
24#include <kernel_config.h>
25#include <hal_types.h>
26#include <hal_special.h>
27#include <hal_irqmask.h>
28#include <printk.h>
29#include <boot_info.h>
30#include <xlist.h>
31#include <kmem.h>
32#include <scheduler.h>
33#include <thread.h>
34#include <rpc.h>
35#include <chdev.h>
36#include <devfs.h>
37
38
39extern chdev_directory_t    chdev_dir;   // allocated in kernel_init.c
40
41#if (CONFIG_DEBUG_SYS_READ & 1)
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;
46#endif
47
48#if (CONFIG_DEBUG_SYS_WRITE & 1)
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
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";
70    else                                 return "undefined";
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
122//////////////////////////////////////////////////
123void chdev_register_command( xptr_t     chdev_xp )
124{
125    thread_t * server_ptr;    // local pointer on server thread associated to chdev
126    core_t   * core_ptr;      // local pointer on core running the server thread
127    uint32_t   lid;           // core running the server thread local index
128    xptr_t     lock_xp;       // extended pointer on lock protecting the chdev queue
129    uint32_t   different;     // non zero if server thread core != client thread core
130    uint32_t   save_sr;       // for critical section
131
132#if (CONFIG_DEBUG_SYS_READ & 1)
133enter_chdev_cmd_read = (uint32_t)hal_get_cycles();
134#endif
135
136#if (CONFIG_DEBUG_SYS_WRITE & 1)
137enter_chdev_cmd_write = (uint32_t)hal_get_cycles();
138#endif
139
140    thread_t * this = CURRENT_THREAD;
141
142#if CONFIG_DEBUG_CHDEV_REGISTER_COMMAND
143uint32_t cycle = (uint32_t)hal_get_cycles();
144if( CONFIG_DEBUG_CHDEV_REGISTER_COMMAND < cycle )
145printk("\n[DBG] %s : client_thread %x (%s) enter / cycle %d\n",
146__FUNCTION__, this, thread_type_str(this->type) , cycle );
147#endif
148
149    // get device descriptor cluster and local pointer
150    cxy_t     chdev_cxy = GET_CXY( chdev_xp );
151    chdev_t * chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
152
153    // build extended pointers on client thread xlist and device root
154    xptr_t  list_xp = XPTR( local_cxy , &this->wait_list );
155    xptr_t  root_xp = XPTR( chdev_cxy , &chdev_ptr->wait_root );
156
157    // get local pointer on server thread
158    server_ptr = (thread_t *)hal_remote_lpt( XPTR( chdev_cxy , &chdev_ptr->server) );
159
160    // build extended pointer on chdev lock protecting queue
161    lock_xp = XPTR( chdev_cxy , &chdev_ptr->wait_lock );
162
163    // get local pointer on core running the server thread
164    core_ptr = (core_t *)hal_remote_lpt( XPTR( chdev_cxy , &server_ptr->core ) );
165
166    // get core local index
167    lid = hal_remote_lw( XPTR( chdev_cxy , &core_ptr->lid ) );
168
169    // compute server core != thread core
170    different = (lid != this->core->lid) || (local_cxy != chdev_cxy);
171
172    // enter critical section to make atomic :
173    // (1) client blocking
174    // (2) client registration in server queue
175    // (3) IPI to force server scheduling
176    // (4) descheduling
177    // ... in this order
178    hal_disable_irq( &save_sr );
179
180    // block current thread
181    thread_block( XPTR( local_cxy , CURRENT_THREAD ) , THREAD_BLOCKED_IO );
182
183    // register client thread in waiting queue
184    remote_spinlock_lock( lock_xp );
185    xlist_add_last( root_xp , list_xp );
186    remote_spinlock_unlock( lock_xp );
187
188    // send IPI to core running the server thread if required
189    if( different ) dev_pic_send_ipi( chdev_cxy , lid ); 
190   
191    // deschedule
192    assert( thread_can_yield( this ) , __FUNCTION__ , "illegal sched_yield\n" );
193    sched_yield("blocked on I/O");
194
195    // exit critical section
196    hal_restore_irq( save_sr );
197
198#if CONFIG_DEBUG_CHDEV_REGISTER_COMMAND
199cycle = (uint32_t)hal_get_cycles();
200if( CONFIG_DEBUG_CHDEV_REGISTER_COMMAND < cycle )
201printk("\n[DBG] %s : client_thread %x (%s) exit / cycle %d\n",
202__FUNCTION__, this, thread_type_str(this->type) , cycle );
203#endif
204
205#if (CONFIG_DEBUG_SYS_READ & 1)
206exit_chdev_cmd_read = (uint32_t)hal_get_cycles();
207#endif
208
209#if (CONFIG_DEBUG_SYS_WRITE & 1)
210exit_chdev_cmd_write = (uint32_t)hal_get_cycles();
211#endif
212
213}  // end chdev_register_command()
214
215///////////////////////////////////////////////
216void chdev_sequencial_server( chdev_t * chdev )
217{
218    xptr_t          client_xp;    // extended pointer on waiting thread
219    cxy_t           client_cxy;   // cluster of client thread
220    thread_t      * client_ptr;   // local pointer on client thread
221    thread_t      * server;       // local pointer on server thread
222    xptr_t          root_xp;      // extended pointer on device waiting queue root
223    xptr_t          lock_xp;      // extended pointer on lock ptotecting chdev queue
224
225    server = CURRENT_THREAD;
226
227#if CONFIG_DEBUG_CHDEV_SEQUENCIAL_SERVER
228uint32_t cycle = (uint32_t)hal_get_cycles();
229if( CONFIG_DEBUG_CHDEV_SEQUENCIAL_SERVER < cycle )
230printk("\n[DBG] %s : server_thread %x enter / chdev = %x / cycle %d\n",
231__FUNCTION__ , server , chdev , cycle );
232#endif
233
234    root_xp = XPTR( local_cxy , &chdev->wait_root );
235    lock_xp = XPTR( local_cxy , &chdev->wait_lock );
236
237        // This infinite loop is executed by the DEV thread
238    // to handle commands registered in the chdev queue.
239    while( 1 )
240    {
241        // get the lock protecting the waiting queue
242        remote_spinlock_lock( lock_xp );
243
244        // check waiting queue state
245        if( xlist_is_empty( root_xp ) ) // waiting queue empty
246        {
247            // release lock
248            remote_spinlock_unlock( lock_xp );
249
250            // deschedule
251            sched_yield("I/O queue empty");
252        } 
253        else                            // waiting queue not empty
254        {
255
256#if (CONFIG_DEBUG_SYS_READ & 1)
257enter_chdev_server_read = (uint32_t)hal_get_cycles();
258#endif
259
260#if (CONFIG_DEBUG_SYS_WRITE & 1)
261enter_chdev_server_write = (uint32_t)hal_get_cycles();
262#endif
263
264            // release lock
265            remote_spinlock_unlock( lock_xp );
266
267            // get extended pointer on first client thread
268            client_xp = XLIST_FIRST_ELEMENT( root_xp , thread_t , wait_list );
269
270            // get client thread cluster, local pointer, and identifier
271            client_cxy = GET_CXY( client_xp );
272            client_ptr = (thread_t *)GET_PTR( client_xp );
273
274            // call driver command function to execute I/O operation
275            chdev->cmd( client_xp );
276       
277            // remove the client thread from waiting queue
278            remote_spinlock_lock( lock_xp );
279            xlist_unlink( XPTR( client_cxy , &client_ptr->wait_list ) );
280            remote_spinlock_unlock( lock_xp );
281
282            // unblock client thread
283            thread_unblock( client_xp , THREAD_BLOCKED_IO );
284
285#if CONFIG_DEBUG_CHDEV_SEQUENCIAL_SERVER
286cycle = (uint32_t)hal_get_cycles();
287if( CONFIG_DEBUG_CHDEV_SEQUENCIAL_SERVER < cycle )
288printk("\n[DBG] %s : server_thread %x complete operation for client %x / cycle %d\n",
289__FUNCTION__ , server , client_ptr , cycle );
290#endif
291
292#if (CONFIG_DEBUG_SYS_READ & 1)
293exit_chdev_server_read = (uint32_t)hal_get_cycles();
294#endif
295
296#if (CONFIG_DEBUG_SYS_WRITE & 1)
297exit_chdev_server_write = (uint32_t)hal_get_cycles();
298#endif
299
300        }
301    }  // end while
302}  // end chdev_sequencial_server()
303
304////////////////////////////////////////
305xptr_t chdev_from_file( xptr_t file_xp )
306{
307    cxy_t         file_cxy;
308    vfs_file_t  * file_ptr;
309    uint32_t      inode_type;
310    vfs_inode_t * inode_ptr;
311    chdev_t     * chdev_ptr;
312
313    // get cluster and local pointer on remote file descriptor
314    // associated inode and chdev are stored in same cluster as the file desc.
315    file_cxy  = GET_CXY( file_xp );
316    file_ptr  = (vfs_file_t *)GET_PTR( file_xp );
317
318    // get inode type from file descriptor
319    inode_type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) );
320    inode_ptr  = (vfs_inode_t *)hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) );
321
322    assert( (inode_type == INODE_TYPE_DEV) , __FUNCTION__ ,
323    "inode type %d is not INODE_TYPE_DEV", inode_type );
324
325    // get chdev local pointer from inode extension
326    chdev_ptr = (chdev_t *)hal_remote_lpt( XPTR( file_cxy , &inode_ptr->extend ) );
327
328    return XPTR( file_cxy , chdev_ptr );
329
330}  // end chdev_from_file()
331
332////////////////////////
333void chdev_dir_display()
334{
335    uint32_t  i;
336    cxy_t     cxy;
337    chdev_t * ptr;
338    uint32_t  base;
339    reg_t     save_sr;
340
341    // get pointers on TXT0 chdev
342    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
343    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
344    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
345
346    // get extended pointer on remote TXT0 chdev lock
347    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
348
349    // get TXT0 lock in busy waiting mode
350    remote_spinlock_lock_busy( lock_xp , &save_sr );
351
352    // header
353    nolock_printk("\n***** external chdevs directory *****\n");
354
355    // IOB
356    cxy  = GET_CXY( chdev_dir.iob );
357    ptr  = GET_PTR( chdev_dir.iob );
358    base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
359    nolock_printk("  - iob       : cxy = %X / ptr = %X / base = %X\n", cxy, ptr, base);
360
361    // PIC
362    cxy  = GET_CXY( chdev_dir.pic );
363    ptr  = GET_PTR( chdev_dir.pic );
364    base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
365    nolock_printk("  - pic       : cxy = %X / ptr = %X / base = %X\n", cxy, ptr, base);
366
367    // TXT
368    for( i = 0 ; i < LOCAL_CLUSTER->nb_txt_channels ; i++ )
369    {
370        cxy = GET_CXY( chdev_dir.txt_rx[i] );
371        ptr = GET_PTR( chdev_dir.txt_rx[i] );
372        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
373        nolock_printk("  - txt_rx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
374
375        cxy = GET_CXY( chdev_dir.txt_tx[i] );
376        ptr = GET_PTR( chdev_dir.txt_tx[i] );
377        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
378        nolock_printk("  - txt_tx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
379    }
380
381    // IOC
382    for( i = 0 ; i < LOCAL_CLUSTER->nb_ioc_channels ; i++ )
383    {
384        cxy = GET_CXY( chdev_dir.ioc[i] );
385        ptr = GET_PTR( chdev_dir.ioc[i] );
386        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
387        nolock_printk("  - ioc[%d]    : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
388    }
389
390    // FBF
391    for( i = 0 ; i < LOCAL_CLUSTER->nb_fbf_channels ; i++ )
392    {
393        cxy  = GET_CXY( chdev_dir.fbf[i] );
394        ptr  = GET_PTR( chdev_dir.fbf[i] );
395        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
396        nolock_printk("  - fbf[%d]    : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
397    }
398
399    // NIC
400    for( i = 0 ; i < LOCAL_CLUSTER->nb_nic_channels ; i++ )
401    {
402        cxy = GET_CXY( chdev_dir.nic_rx[i] );
403        ptr = GET_PTR( chdev_dir.nic_rx[i] );
404        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
405        nolock_printk("  - nic_rx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
406
407        cxy = GET_CXY( chdev_dir.nic_tx[i] );
408        ptr = GET_PTR( chdev_dir.nic_tx[i] );
409        base = (uint32_t)hal_remote_lwd( XPTR( cxy , &ptr->base ) );
410        nolock_printk("  - nic_tx[%d] : cxy = %X / ptr = %X / base = %X\n", i, cxy, ptr, base);
411    }
412
413    // release lock
414    remote_spinlock_unlock_busy( lock_xp , save_sr );
415
416}  // end chdev_dir_display()
417
Note: See TracBrowser for help on using the repository browser.