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
Line 
1/*
2 * dev_txt.c - TXT (Text Terminal) generic device API implementation.
3 *
4 * Author  Alain Greiner    (2016,2017,2018)
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
24#include <hal_kernel_types.h>
25#include <hal_special.h>
26#include <hal_remote.h>
27#include <hal_drivers.h>
28#include <thread.h>
29#include <remote_busylock.h>
30#include <chdev.h>
31#include <rpc.h>
32#include <printk.h>
33#include <dev_txt.h>
34
35/////////////////////////////////////////////////////////////////////////////////////////
36// Extern global variables
37/////////////////////////////////////////////////////////////////////////////////////////
38
39extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c
40
41
42#if (DEBUG_SYS_READ & 1)
43extern uint32_t enter_txt_read;
44extern uint32_t exit_txt_read;
45#endif
46
47#if (DEBUG_SYS_WRITE & 1)
48extern uint32_t enter_txt_write;
49extern uint32_t exit_txt_write;
50#endif
51
52///////////////////////////////////////////////////
53const char * dev_txt_type_str( dev_txt_cmd_t type )
54{
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  }
61}
62
63//////////////////////////////////
64void dev_txt_init( chdev_t * txt )
65{
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
69
70    xptr_t    pic_xp  = chdev_dir.pic;
71    uint32_t  channel = txt->channel;
72    uint32_t  impl    = txt->impl;
73    bool_t    is_rx   = txt->is_rx;
74
75    assert( (pic_xp != XPTR_NULL) || (channel == 0) ,
76            "PIC not initialised before TXT" );
77
78    // set chdev name
79    if( is_rx ) snprintf( txt->name , 16 , "txt%d_rx" , channel );
80    else        snprintf( txt->name , 16 , "txt%d_tx" , channel );
81
82    // set TXT chdev extension
83    txt->ext.txt.owner_xp = XPTR_NULL;
84    xlist_root_init( XPTR( local_cxy, &txt->ext.txt.root ) );
85    remote_busylock_init( XPTR( local_cxy , &txt->ext.txt.lock ), LOCK_CHDEV_TXTLIST );
86   
87    // call driver init function
88    hal_drivers_txt_init(txt, impl);
89
90    // no server thread and no IRQ routing for TXT0
91    // except for LETI where there MUST be IRQ routing
92    // no server thread for IMPL_TXT_RS2 (multiplexed in software, not hardware)
93    if( channel != 0 && impl != IMPL_TXT_RS2 )
94    {
95        // select a core to execute the server thread
96        lid_t lid = cluster_select_local_core();
97
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        {
104        // bind IRQ to the selected core
105        dev_pic_bind_irq( lid , txt );
106
107        // enable IRQ
108        dev_pic_enable_irq( lid , XPTR( local_cxy , txt ) );
109        }
110
111        // create server thread
112        thread_t * new_thread;
113        error_t    error;
114
115        error = thread_kernel_create( &new_thread,
116                                      THREAD_DEV,
117                                      &chdev_sequencial_server,
118                                      txt,
119                                      lid );
120
121        assert( (error == 0) , "cannot create server thread" );
122
123        // set "server" field in chdev descriptor
124        txt->server = new_thread;
125
126        // set "chdev" field in thread descriptor
127        new_thread->chdev = txt;
128
129        // unblock server thread
130        thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL );
131    }
132}
133
134//////////////////////////////////////////////////////////////////////////////////
135// This static function is called by dev_txt_read(), dev_txt_write() functions.
136////////////////////////////////////i/////////////////////////////////////////////
137static error_t dev_txt_access( uint32_t   type,
138                               uint32_t   channel,
139                               char     * buffer,
140                               uint32_t   count )
141{
142    xptr_t     dev_xp;
143    thread_t * this = CURRENT_THREAD;
144
145    // check channel argument
146    assert( (channel < CONFIG_MAX_TXT_CHANNELS) , "illegal channel index" );
147
148    // get extended pointer on remote TXT chdev descriptor
149    if( type == TXT_WRITE )  dev_xp = chdev_dir.txt_tx[channel];
150    else                     dev_xp = chdev_dir.txt_rx[channel];
151
152    assert( (dev_xp != XPTR_NULL) , "undefined TXT chdev descriptor" );
153
154    // register command in calling thread descriptor
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;
159
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.
163    chdev_register_command( dev_xp );
164
165    // return I/O operation status from calling thread descriptor
166    return this->txt_cmd.error;
167}
168
169/////////////////////////////////////////
170error_t dev_txt_write( uint32_t   channel,
171                       char     * buffer,
172                       uint32_t   count )
173{
174
175#if (DEBUG_SYS_WRITE & 1)
176enter_txt_write = hal_time_stamp();
177#endif
178
179#if DEBUG_DEV_TXT_TX
180uint32_t cycle = (uint32_t)hal_get_cycles();
181if( DEBUG_DEV_TXT_TX < cycle )
182printk("\n[DBG] %s : thread %x enters / cycle %d\n", __FUNCTION__, CURRENT_THREAD, cycle );
183#endif
184
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    {
217    return dev_txt_access( TXT_WRITE , channel , buffer , count );
218    }
219
220#if DEBUG_DEV_TXT_TX
221cycle = (uint32_t)hal_get_cycles();
222if( DEBUG_DEV_TXT_TX < cycle )
223printk("\n[DBG] %s : thread %x exit / cycle %d\n", __FUNCTION__, CURRENT_THREAD, cycle );
224#endif
225
226#if (DEBUG_SYS_WRITE & 1)
227exit_txt_write = hal_time_stamp();
228#endif
229
230}
231
232/////////////////////////////////////////
233error_t dev_txt_read( uint32_t   channel,
234                      char     * buffer )
235{
236
237#if (DEBUG_SYS_READ & 1)
238enter_txt_read = hal_time_stamp();
239#endif
240
241#if DEBUG_DEV_TXT_RX
242uint32_t cycle = (uint32_t)hal_get_cycles();
243if( DEBUG_DEV_TXT_RX < cycle )
244printk("\n[DBG] %s : thread %x enters / cycle %d\n", __FUNCTION__, CURRENT_THREAD, cycle );
245#endif
246
247    return dev_txt_access( TXT_READ , channel , buffer , 1 );
248
249#if DEBUG_DEV_TXT_RX
250cycle = (uint32_t)hal_get_cycles();
251if( DEBUG_DEV_TXT_RX < cycle )
252printk("\n[DBG] %s : thread %x exit / cycle %d\n", __FUNCTION__, CURRENT_THREAD, cycle );
253#endif
254
255#if (DEBUG_SYS_READ & 1)
256exit_txt_read = hal_time_stamp();
257#endif
258
259}
260
261////////////////////////////////////////////////
262error_t dev_txt_sync_write( const char * buffer,
263                            uint32_t     count )
264{
265    // get extended pointer on TXT[0] chdev
266    xptr_t  dev_xp = chdev_dir.txt_tx[0];
267
268    assert( (dev_xp != XPTR_NULL) ,
269    "undefined TXT0 chdev descriptor" );
270
271    // get TXTO chdev cluster and local pointer
272    cxy_t    dev_cxy  = GET_CXY( dev_xp );
273    chdev_t * dev_ptr = GET_PTR( dev_xp );
274
275    // get driver command function
276    dev_aux_t * aux = (dev_aux_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->aux ) );
277
278    // build arguments structure
279    txt_sync_args_t  args;
280    args.dev_xp = dev_xp;
281    args.buffer = buffer;
282    args.count  = count;
283    args.channel = 0;
284
285    // call driver function
286    aux( &args );
287
288    return 0;
289}
290
Note: See TracBrowser for help on using the repository browser.