source: trunk/kernel/devices/dev_txt.c @ 539

Last change on this file since 539 was 539, checked in by nicolas.van.phan@…, 6 years ago

TTY MUX 4 : Multiplex TTY character sending

Now, when a thread wants to write to a tty,
when the dev_txt_write() or dev_txt_sync_write() are called,
they all call soclib_mtty_aux() with a channel number.
The soclib_mtty_aux() function will write the string char by char,
with each char preceded by the channel number, so that the receiving end
knows to which tty a character is addressed to.

N.B. dev_txt_write() makes *synchronous* writes for the moment
because unlike the vci_tty_tsar, the vci_multi_tty doesn't raise
interrupt except when a new char is received, so we can't use the
interrupt mechanism for writes.

N.B. Now, the TTY DEV threads all write to the same register (WRITE),
but when a thread sends a 2-byte (tty dest. nb. + char), the two must be
send consecutively, without another thread sending a byte in between.
Consequently, a lock has been added to guarantee this atomicity.

File size: 8.4 KB
RevLine 
[1]1/*
2 * dev_txt.c - TXT (Text Terminal) generic device API implementation.
[49]3 *
[1]4 * Author  Alain Greiner    (2016)
5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MK
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-kernel; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
[457]24#include <hal_kernel_types.h>
[1]25#include <hal_special.h>
26#include <hal_remote.h>
[77]27#include <hal_drivers.h>
[1]28#include <thread.h>
[407]29#include <chdev.h>
[1]30#include <rpc.h>
31#include <printk.h>
32#include <dev_txt.h>
33
34/////////////////////////////////////////////////////////////////////////////////////////
35// Extern global variables
36/////////////////////////////////////////////////////////////////////////////////////////
37
[3]38extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c
[1]39
[438]40#if (DEBUG_SYS_READ & 1)
[407]41extern uint32_t enter_txt_read;
42extern uint32_t exit_txt_read;
43#endif
44
[438]45#if (DEBUG_SYS_WRITE & 1)
[435]46extern uint32_t enter_txt_write;
47extern uint32_t exit_txt_write;
48#endif
49
50////////////////////////////////////////
[527]51const char * dev_txt_type_str( dev_txt_cmd_t type )
[435]52{
[527]53  switch (type) {
54    case (TXT_SYNC_WRITE): return "TXT_SYNC_WRITE";
55    case (TXT_READ):       return "TXT_READ";
56    case (TXT_WRITE):      return "TXT_WRITE";
57    default:               return "undefined";
58  }
[435]59}
60
[188]61//////////////////////////////////
62void dev_txt_init( chdev_t * txt )
[1]63{
[188]64    // For all TXT channels other than the TXT0 (kernel terminal),
65    // the PIC chdev must be initialized before the TXT chdev, because
66    // the TXT chdev initialization requires the routing of an external IRQ
[1]67
[188]68    xptr_t    pic_xp  = chdev_dir.pic;
69    uint32_t  channel = txt->channel;
70    uint32_t  impl    = txt->impl;
[407]71    bool_t    is_rx   = txt->is_rx;
[1]72
[492]73    assert( (pic_xp != XPTR_NULL) || (channel == 0) ,
[188]74            "PIC not initialised before TXT" );
75
[23]76    // set chdev name
[407]77    if( is_rx ) snprintf( txt->name , 16 , "txt%d_rx" , channel );
78    else        snprintf( txt->name , 16 , "txt%d_tx" , channel );
[23]79
[422]80    // set TXT chdev extension
81    txt->ext.txt.owner_xp = XPTR_NULL;
82    xlist_root_init( XPTR( local_cxy, &txt->ext.txt.root ) );
83    remote_spinlock_init( XPTR( local_cxy , &txt->ext.txt.lock ) );
84   
[245]85    // call driver init function
86    hal_drivers_txt_init(txt, impl);
[1]87
[422]88    // no server thread and no IRQ routing for TXT0
89    // no server thread for IMPL_TXT_RS2 (multiplexed in software, not hardware)
[255]90    if( channel != 0 && impl != IMPL_TXT_RS2 )
[188]91    {
[407]92        // select a core to execute the server thread
[188]93        lid_t lid = cluster_select_local_core();
[3]94
[407]95        // bind IRQ to the selected core
[188]96        dev_pic_bind_irq( lid , txt );
[3]97
[407]98        // enable IRQ
[238]99        dev_pic_enable_irq( lid , XPTR( local_cxy , txt ) );
[3]100
[188]101        // create server thread
102        thread_t * new_thread;
103        error_t    error;
[3]104
[188]105        error = thread_kernel_create( &new_thread,
106                                      THREAD_DEV,
107                                      &chdev_sequencial_server,
108                                      txt,
109                                      lid );
[3]110
[492]111        assert( (error == 0) , "cannot create server thread" );
[1]112
[188]113        // set "server" field in chdev descriptor
114        txt->server = new_thread;
[1]115
[408]116        // set "chdev" field in thread descriptor
117        new_thread->chdev = txt;
118
119        // unblock server thread
[188]120        thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL );
121    }
[49]122}
[1]123
124//////////////////////////////////////////////////////////////////////////////////
[3]125// This static function is called by dev_txt_read(), dev_txt_write() functions.
[1]126////////////////////////////////////i/////////////////////////////////////////////
127static error_t dev_txt_access( uint32_t   type,
128                               uint32_t   channel,
129                               char     * buffer,
130                               uint32_t   count )
131{
[407]132    xptr_t     dev_xp;
[1]133    thread_t * this = CURRENT_THREAD;
134
135    // check channel argument
[492]136    assert( (channel < CONFIG_MAX_TXT_CHANNELS) , "illegal channel index" );
[1]137
[3]138    // get extended pointer on remote TXT chdev descriptor
[407]139    if( type == TXT_WRITE )  dev_xp = chdev_dir.txt_tx[channel];
140    else                     dev_xp = chdev_dir.txt_rx[channel];
[1]141
[492]142    assert( (dev_xp != XPTR_NULL) , "undefined TXT chdev descriptor" );
[1]143
144    // register command in calling thread descriptor
[279]145    this->txt_cmd.dev_xp  = dev_xp;
146    this->txt_cmd.type    = type;
147    this->txt_cmd.buf_xp  = XPTR( local_cxy , buffer );
148    this->txt_cmd.count   = count;
[1]149
[3]150    // register client thread in waiting queue, activate server thread
151    // block client thread on THREAD_BLOCKED_IO and deschedule.
152    // it is re-activated by the ISR signaling IO operation completion.
[407]153    chdev_register_command( dev_xp );
[1]154
155    // return I/O operation status from calling thread descriptor
[279]156    return this->txt_cmd.error;
[49]157}
[1]158
159/////////////////////////////////////////
160error_t dev_txt_write( uint32_t   channel,
161                       char     * buffer,
162                       uint32_t   count )
163{
[436]164
[438]165#if (DEBUG_SYS_WRITE & 1)
[436]166enter_txt_write = hal_time_stamp();
167#endif
168
[438]169#if DEBUG_DEV_TXT_TX
[436]170uint32_t cycle = (uint32_t)hal_get_cycles();
[438]171if( DEBUG_DEV_TXT_TX < cycle )
[436]172printk("\n[DBG] %s : thread %x enters / cycle %d\n", __FUNCTION__, CURRENT_THREAD, cycle );
173#endif
174
[539]175    // get extended pointer on TXT[0] chdev
176    xptr_t dev_xp = chdev_dir.txt_tx[0];
177
178    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ ,
179    "undefined TXT0 chdev descriptor" );
180
181    // get TXTO chdev cluster and local pointer
182    cxy_t    dev_cxy  = GET_CXY( dev_xp );
183    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
184
185    // If we use MTTYs (vci_multi_tty), we perform only sync writes
186    if( dev_ptr->impl == IMPL_TXT_MTY )
187    {
188        // get driver command function
189        dev_aux_t * aux = (dev_aux_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->aux ) );
190
191        // build arguments structure
192        txt_sync_args_t  args;
193        args.dev_xp = dev_xp;
194        args.buffer = buffer;
195        args.count  = count;
196        args.channel = channel;
197
198        // call driver function
199        aux( &args );
200
201        return 0;
202    }
203
204    // Otherwise, we use vci_tty_tsar so we can use async writes
205    else
206    {
[435]207    return dev_txt_access( TXT_WRITE , channel , buffer , count );
[539]208    }
[436]209
[438]210#if DEBUG_DEV_TXT_TX
[436]211cycle = (uint32_t)hal_get_cycles();
[438]212if( DEBUG_DEV_TXT_TX < cycle )
[436]213printk("\n[DBG] %s : thread %x exit / cycle %d\n", __FUNCTION__, CURRENT_THREAD, cycle );
214#endif
215
[438]216#if (DEBUG_SYS_WRITE & 1)
[436]217exit_txt_write = hal_time_stamp();
218#endif
219
[1]220}
[49]221
[1]222/////////////////////////////////////////
223error_t dev_txt_read( uint32_t   channel,
224                      char     * buffer )
225{
[436]226
[438]227#if (DEBUG_SYS_READ & 1)
[436]228enter_txt_read = hal_time_stamp();
229#endif
230
[438]231#if DEBUG_DEV_TXT_RX
[436]232uint32_t cycle = (uint32_t)hal_get_cycles();
[438]233if( DEBUG_DEV_TXT_RX < cycle )
[436]234printk("\n[DBG] %s : thread %x enters / cycle %d\n", __FUNCTION__, CURRENT_THREAD, cycle );
235#endif
236
[435]237    return dev_txt_access( TXT_READ , channel , buffer , 1 );
[436]238
[438]239#if DEBUG_DEV_TXT_RX
[436]240cycle = (uint32_t)hal_get_cycles();
[438]241if( DEBUG_DEV_TXT_RX < cycle )
[436]242printk("\n[DBG] %s : thread %x exit / cycle %d\n", __FUNCTION__, CURRENT_THREAD, cycle );
243#endif
244
[438]245#if (DEBUG_SYS_READ & 1)
[436]246exit_txt_read = hal_time_stamp();
247#endif
248
[1]249}
250
[407]251//////////////////////////////////////////////
252error_t dev_txt_sync_write( char     * buffer,
[3]253                            uint32_t   count )
[1]254{
[49]255    // get extended pointer on TXT[0] chdev
[407]256    xptr_t  dev_xp = chdev_dir.txt_tx[0];
[1]257
[492]258    assert( (dev_xp != XPTR_NULL) ,
[407]259    "undefined TXT0 chdev descriptor" );
[23]260
[407]261    // get TXTO chdev cluster and local pointer
262    cxy_t    dev_cxy  = GET_CXY( dev_xp );
263    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
[1]264
[49]265    // get driver command function
[407]266    dev_aux_t * aux = (dev_aux_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->aux ) );
[1]267
[407]268    // build arguments structure
[435]269    txt_sync_args_t  args;
[407]270    args.dev_xp = dev_xp;
271    args.buffer = buffer;
272    args.count  = count;
[539]273    args.channel = 0;
[407]274
[279]275    // call driver function
[407]276    aux( &args );
[1]277
[407]278    return 0;
[49]279}
[1]280
Note: See TracBrowser for help on using the repository browser.