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

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

Complete restructuration of kernel locks.

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