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

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

Fix a bug in scheduler related to RPC blocking.

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