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

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

miscelaneous...

File size: 15.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 (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    xptr_t     server_xp;     // extended pointer on server thread
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
130    uint32_t   different;     // non zero if server thread core != client thread core
131    uint32_t   save_sr;       // for critical section
132
133#if (DEBUG_SYS_READ & 1)
134enter_chdev_cmd_read = (uint32_t)hal_get_cycles();
135#endif
136
137#if (DEBUG_SYS_WRITE & 1)
138enter_chdev_cmd_write = (uint32_t)hal_get_cycles();
139#endif
140
141    thread_t * this = CURRENT_THREAD;
142
143    // get chdev cluster and local pointer
144    cxy_t     chdev_cxy = GET_CXY( chdev_xp );
145    chdev_t * chdev_ptr = GET_PTR( chdev_xp );
146
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
157#if (DEBUG_CHDEV_CMD_RX || DEBUG_CHDEV_CMD_TX)
158bool_t is_rx = hal_remote_lw( XPTR( chdev_cxy , &chdev_ptr->is_rx ) );
159#endif
160   
161#if DEBUG_CHDEV_CMD_RX
162uint32_t rx_cycle = (uint32_t)hal_get_cycles();
163if( (is_rx) && (DEBUG_CHDEV_CMD_RX < rx_cycle) )
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
168#if DEBUG_CHDEV_CMD_TX
169uint32_t tx_cycle = (uint32_t)hal_get_cycles();
170if( (is_rx == 0) && (DEBUG_CHDEV_CMD_TX < tx_cycle) )
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
175    // build extended pointer on client thread xlist
176    xptr_t  list_xp    = XPTR( local_cxy , &this->wait_list );
177
178    // build extended pointer on chdev waiting queue root
179    xptr_t  root_xp    = XPTR( chdev_cxy , &chdev_ptr->wait_root );
180
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
185    lock_xp = XPTR( chdev_cxy , &chdev_ptr->wait_lock );
186
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
196
197    // enter critical section
198    hal_disable_irq( &save_sr );
199
200    // take the lock protecting chdev waiting queue
201    remote_spinlock_lock( lock_xp );
202
203    // block current thread
204    thread_block( XPTR( local_cxy , CURRENT_THREAD ) , THREAD_BLOCKED_IO );
205
206    // unblock server thread if required
207    if( hal_remote_lw( blocked_xp ) & THREAD_BLOCKED_IDLE )
208    thread_unblock( server_xp , THREAD_BLOCKED_IDLE );
209
210    // register client thread in waiting queue
211    xlist_add_last( root_xp , list_xp );
212
213    // send IPI to core running the server thread when server != client
214    different = (lid != this->core->lid) || (local_cxy != chdev_cxy);
215    if( different ) dev_pic_send_ipi( chdev_cxy , lid ); 
216   
217    // release lock
218    remote_spinlock_unlock( lock_xp );
219
220    // deschedule
221    assert( thread_can_yield( this ) , __FUNCTION__ , "illegal sched_yield\n" );
222    sched_yield("blocked on I/O");
223
224    // exit critical section
225    hal_restore_irq( save_sr );
226
227#if DEBUG_CHDEV_CMD_RX
228rx_cycle = (uint32_t)hal_get_cycles();
229if( (is_rx) && (DEBUG_CHDEV_CMD_RX < rx_cycle) )
230printk("\n[DBG] %s : client_thread %x (%s) exit for RX / cycle %d\n",
231__FUNCTION__, this, thread_type_str(this->type) , rx_cycle );
232#endif
233
234#if DEBUG_CHDEV_CMD_TX
235tx_cycle = (uint32_t)hal_get_cycles();
236if( (is_rx == 0) && (DEBUG_CHDEV_CMD_TX < tx_cycle) )
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
241#if (DEBUG_SYS_READ & 1)
242exit_chdev_cmd_read = (uint32_t)hal_get_cycles();
243#endif
244
245#if (DEBUG_SYS_WRITE & 1)
246exit_chdev_cmd_write = (uint32_t)hal_get_cycles();
247#endif
248
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
257    thread_t      * server;       // local pointer on server thread
258    xptr_t          root_xp;      // extended pointer on device waiting queue root
259    xptr_t          lock_xp;      // extended pointer on lock ptotecting chdev queue
260
261    server = CURRENT_THREAD;
262
263    // get root and lock on command queue
264    root_xp = XPTR( local_cxy , &chdev->wait_root );
265    lock_xp = XPTR( local_cxy , &chdev->wait_lock );
266
267        // This infinite loop is executed by the DEV thread
268    // to handle commands registered in the chdev queue.
269    while( 1 )
270    {
271        // get the lock protecting the waiting queue
272        remote_spinlock_lock( lock_xp );
273
274        // check waiting queue state
275        if( xlist_is_empty( root_xp ) ) // waiting queue empty
276        {
277            // release lock
278            remote_spinlock_unlock( lock_xp );
279
280            // block
281            thread_block( XPTR( local_cxy , server ) , THREAD_BLOCKED_IDLE ); 
282
283            // deschedule
284            assert( thread_can_yield( server ) , __FUNCTION__ , "illegal sched_yield\n" );
285            sched_yield("I/O queue empty");
286        } 
287        else                            // waiting queue not empty
288        {
289            // get extended pointer on first client thread
290            client_xp = XLIST_FIRST_ELEMENT( root_xp , thread_t , wait_list );
291
292            // get client thread cluster and local pointer
293            client_cxy = GET_CXY( client_xp );
294            client_ptr = GET_PTR( client_xp );
295
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
302#if DEBUG_CHDEV_SERVER_RX
303uint32_t rx_cycle = (uint32_t)hal_get_cycles();
304if( (chdev->is_rx) && (DEBUG_CHDEV_SERVER_RX < rx_cycle) )
305printk("\n[DBG] %s : server_thread %x start RX / client %x / cycle %d\n",
306__FUNCTION__ , server , client_ptr , rx_cycle );
307#endif
308
309#if DEBUG_CHDEV_SERVER_TX
310uint32_t tx_cycle = (uint32_t)hal_get_cycles();
311if( (chdev->is_rx == 0) && (DEBUG_CHDEV_SERVER_TX < tx_cycle) )
312printk("\n[DBG] %s : server_thread %x start TX / client %x / cycle %d\n",
313__FUNCTION__ , server , client_ptr , tx_cycle );
314#endif
315
316#if (DEBUG_SYS_READ & 1)
317enter_chdev_server_read = (uint32_t)hal_get_cycles();
318#endif
319
320#if (DEBUG_SYS_WRITE & 1)
321enter_chdev_server_write = (uint32_t)hal_get_cycles();
322#endif
323
324            // call driver command function to execute I/O operation
325            chdev->cmd( client_xp );
326       
327            // unblock client thread
328            thread_unblock( client_xp , THREAD_BLOCKED_IO );
329
330#if DEBUG_CHDEV_SERVER_RX
331rx_cycle = (uint32_t)hal_get_cycles();
332if( (chdev->is_rx) && (DEBUG_CHDEV_SERVER_RX < rx_cycle) )
333printk("\n[DBG] %s : server_thread %x completes RX / client %x / cycle %d\n",
334__FUNCTION__ , server , client_ptr , rx_cycle );
335#endif
336
337#if DEBUG_CHDEV_SERVER_TX
338tx_cycle = (uint32_t)hal_get_cycles();
339if( (chdev->is_rx == 0) && (DEBUG_CHDEV_SERVER_TX < tx_cycle) )
340printk("\n[DBG] %s : server_thread %x completes TX / client %x / cycle %d\n",
341__FUNCTION__ , server , client_ptr , tx_cycle );
342#endif
343
344#if (DEBUG_SYS_READ & 1)
345exit_chdev_server_read = (uint32_t)hal_get_cycles();
346#endif
347
348#if (DEBUG_SYS_WRITE & 1)
349exit_chdev_server_write = (uint32_t)hal_get_cycles();
350#endif
351
352        }
353    }  // end while
354}  // end chdev_sequencial_server()
355
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
365    assert( (file_xp != XPTR_NULL) , __FUNCTION__,
366    "file_xp == XPTR_NULL\n" );
367
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__ ,
378    "inode type %d is not INODE_TYPE_DEV\n", inode_type );
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
387////////////////////////
388void chdev_dir_display()
389{
390    uint32_t  i;
391    cxy_t     cxy;
392    chdev_t * ptr;
393    uint32_t  base;
394    reg_t     save_sr;
395
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 );
400
401    // get extended pointer on remote TXT0 chdev lock
402    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
403
404    // get TXT0 lock in busy waiting mode
405    remote_spinlock_lock_busy( lock_xp , &save_sr );
406
407    // header
408    nolock_printk("\n***** external chdevs directory *****\n");
409
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);
415
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);
421
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);
429
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    }
435
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    }
444
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    }
453
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);
461
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    }
467
468    // release lock
469    remote_spinlock_unlock_busy( lock_xp , save_sr );
470
471}  // end chdev_dir_display()
472
Note: See TracBrowser for help on using the repository browser.