source: trunk/hal/tsar_mips32/drivers/soclib_tty.c @ 436

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

1) improve the threads and process destruction mechanism.
2) introduce FIFOs in the soclib_tty driver.

File size: 17.6 KB
Line 
1/*
2 * soclib_tty.c - soclib tty driver implementation.
3 *
4 * Author  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 <dev_txt.h>
25#include <chdev.h>
26#include <soclib_tty.h>
27#include <remote_spinlock.h>
28#include <thread.h>
29#include <printk.h>
30#include <hal_special.h>
31
32#if (CONFIG_DEBUG_SYS_READ & 1)
33extern uint32_t  enter_tty_cmd_read;
34extern uint32_t  exit_tty_cmd_read;
35
36extern uint32_t  enter_tty_isr_read;
37extern uint32_t  exit_tty_isr_read;
38#endif
39
40#if (CONFIG_DEBUG_SYS_WRITE & 1)
41extern uint32_t  enter_tty_cmd_write;
42extern uint32_t  exit_tty_cmd_write;
43
44extern uint32_t  enter_tty_isr_write;
45extern uint32_t  exit_tty_isr_write;
46#endif
47
48////////////////////////////////////////////////////////////////////////////////////
49// These global variables implement the TTY_RX  FIFOs (one per channel)
50////////////////////////////////////////////////////////////////////////////////////
51
52__attribute__((section(".kdata")))
53tty_fifo_t  tty_rx_fifo[CONFIG_MAX_TXT_CHANNELS];
54
55__attribute__((section(".kdata")))
56tty_fifo_t  tty_tx_fifo[CONFIG_MAX_TXT_CHANNELS];
57
58///////////////////////////////////////
59void soclib_tty_init( chdev_t * chdev )
60{
61    xptr_t reg_xp;
62
63    // initialise function pointers in chdev
64    chdev->cmd = &soclib_tty_cmd;
65    chdev->isr = &soclib_tty_isr;
66    chdev->aux = &soclib_tty_aux;
67
68    // get TTY channel and extended pointer on TTY peripheral base address
69    xptr_t   tty_xp  = chdev->base;
70    uint32_t channel = chdev->channel;
71    bool_t   is_rx   = chdev->is_rx;
72
73    // get SOCLIB_TTY device cluster and local pointer
74    cxy_t      tty_cxy = GET_CXY( tty_xp );
75    uint32_t * tty_ptr = GET_PTR( tty_xp );
76
77    // set TTY_RX_IRQ_ENABLE
78    reg_xp = XPTR( tty_cxy , tty_ptr + (channel * TTY_SPAN) + TTY_RX_IRQ_ENABLE );
79    hal_remote_sw( reg_xp , 1 );
80
81    // reset TTY_TX_IRQ_ENABLE
82    reg_xp = XPTR( tty_cxy , tty_ptr + (channel * TTY_SPAN) + TTY_TX_IRQ_ENABLE );
83    hal_remote_sw( reg_xp , 0 );
84
85    // reset relevant FIFO
86    if( is_rx )
87    {
88        tty_rx_fifo[channel].sts = 0;
89        tty_rx_fifo[channel].ptr = 0;
90        tty_rx_fifo[channel].ptw = 0;
91    }
92    else
93    {
94        tty_tx_fifo[channel].sts = 0;
95        tty_tx_fifo[channel].ptr = 0;
96        tty_tx_fifo[channel].ptw = 0;
97    }
98}  // end soclib_tty_init()
99
100//////////////////////////////////////////////////////////////
101void __attribute__ ((noinline)) soclib_tty_cmd( xptr_t th_xp )
102{
103    tty_fifo_t * fifo;     // TTY_RX or TTY_TX FIFO
104    char         byte;     // byte value
105    uint32_t     done;     // number of bytes moved
106
107    // get client thread cluster and local pointer
108    cxy_t      th_cxy = GET_CXY( th_xp );
109    thread_t * th_ptr = GET_PTR( th_xp );
110
111    // get command arguments
112    uint32_t type     = hal_remote_lw ( XPTR( th_cxy , &th_ptr->txt_cmd.type   ) );
113    xptr_t   buf_xp   = hal_remote_lwd( XPTR( th_cxy , &th_ptr->txt_cmd.buf_xp ) );
114    uint32_t count    = hal_remote_lw ( XPTR( th_cxy , &th_ptr->txt_cmd.count  ) );
115    xptr_t   error_xp = XPTR( th_cxy , &th_ptr->txt_cmd.error );
116
117#if (CONFIG_DEBUG_SYS_READ & 1)
118if( type == TXT_READ) enter_tty_cmd_read = (uint32_t)hal_get_cycles();
119#endif
120
121#if (CONFIG_DEBUG_SYS_WRITE & 1)
122if( type == TXT_WRITE) enter_tty_cmd_write = (uint32_t)hal_get_cycles();
123#endif
124
125#if CONFIG_DEBUG_HAL_TXT_RX
126uint32_t cycle = (uint32_t)hal_get_cycles();
127if( (CONFIG_DEBUG_HAL_TXT_RX < cycle) && (type == TXT_READ) )
128printk("\n[DBG] %s : thread %x enter for RX / cycle %d\n",
129__FUNCTION__ , CURRENT_THREAD , cycle );
130#endif
131
132#if CONFIG_DEBUG_HAL_TXT_TX
133uint32_t cycle = (uint32_t)hal_get_cycles();
134if( (CONFIG_DEBUG_HAL_TXT_TX < cycle) && (type == TXT_WRITE) )
135printk("\n[DBG] %s : thread %x enter for TX / cycle %d\n",
136__FUNCTION__ , CURRENT_THREAD , cycle );
137#endif
138
139    // get TXT device pointers
140    xptr_t    dev_xp = (xptr_t)hal_remote_lwd( XPTR( th_cxy , &th_ptr->txt_cmd.dev_xp ) );
141    cxy_t     dev_cxy = GET_CXY( dev_xp );
142    chdev_t * dev_ptr = GET_PTR( dev_xp );
143
144    // get extended pointer on SOCLIB_TTY base segment
145    xptr_t tty_xp = (xptr_t)hal_remote_lwd( XPTR( dev_cxy , &dev_ptr->base ) );
146
147    // get SOCLIB_TTY base segment cluster and local pointer
148    cxy_t      tty_cxy = GET_CXY( tty_xp );
149    uint32_t * tty_ptr = GET_PTR( tty_xp );
150
151    // get TTY channel index and channel base address
152    uint32_t   channel = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->channel ) );
153    uint32_t * base    = tty_ptr + TTY_SPAN * channel;
154
155    ///////////////////////
156    if( type == TXT_WRITE )         // write bytes to TTY_TX FIFO
157    {
158        fifo = &tty_tx_fifo[channel];
159
160        done = 0;
161
162        while( done < count )
163        {
164            if( fifo->sts < TTY_FIFO_DEPTH )   // put one byte to FIFO if TX_FIFO not full
165            {
166                // get one byte from command buffer
167                byte = hal_remote_lb( buf_xp + done );
168
169                // write byte to FIFO
170                fifo->data[fifo->ptw] = byte;
171
172                // prevent race
173                hal_fence();
174
175                // update FIFO state
176                fifo->ptw = (fifo->ptw + 1) % TTY_FIFO_DEPTH;
177                hal_atomic_add( &fifo->sts , 1 );
178
179                // udate number of bytes moved
180                done++;
181
182                // enable TX_IRQ
183                hal_remote_sw( XPTR( tty_cxy , base + TTY_TX_IRQ_ENABLE ) , 1 );
184            }
185            else                                // block & deschedule if TX_FIFO full
186            {
187                // block on ISR
188                thread_block( XPTR( local_cxy , CURRENT_THREAD ) , THREAD_BLOCKED_ISR );
189
190                // deschedule
191                sched_yield( "TTY_TX_FIFO full" ); 
192            }
193        }
194
195        // set error status in command and return
196        hal_remote_sw( error_xp , 0 );
197    }
198    ///////////////////////////
199    else if( type == TXT_READ )       // read bytes from TTY_RX FIFO   
200    {
201        fifo = &tty_rx_fifo[channel];
202
203        done = 0;
204
205        while( done < count )
206        {
207            if( fifo->sts > 0 )               // get byte from FIFO if not empty
208            {
209                // get one byte from FIFO
210                char byte = fifo->data[fifo->ptr];
211
212                // update FIFO state
213                fifo->ptr = (fifo->ptr + 1) % TTY_FIFO_DEPTH;
214                hal_atomic_add( &fifo->sts , -1 );
215
216                // set byte to command buffer
217                hal_remote_sb( buf_xp + done , byte );
218
219                // udate number of bytes
220                done++;
221            }
222            else                             //  deschedule if FIFO empty
223            {
224                // block on ISR
225                thread_block( XPTR( local_cxy , CURRENT_THREAD ) , THREAD_BLOCKED_ISR );
226   
227                // deschedule
228                sched_yield( "TTY_TX_FIFO_RX empty" );
229            }
230        }  // end while
231
232        // set error status in command
233        hal_remote_sw( error_xp , 0 );
234    }
235    else
236    {
237        assert( false , __FUNCTION__ , "illegal TXT command\n" );
238    }
239
240#if CONFIG_DEBUG_HAL_TXT_RX
241cycle = (uint32_t)hal_get_cycles();
242if( (CONFIG_DEBUG_HAL_TXT_RX < cycle) && (type == TXT_READ) )
243printk("\n[DBG] %s : thread %x exit after RX / cycle %d\n",
244__FUNCTION__ , CURRENT_THREAD , cycle );
245#endif
246
247#if CONFIG_DEBUG_HAL_TXT_TX
248cycle = (uint32_t)hal_get_cycles();
249if( (CONFIG_DEBUG_HAL_TXT_TX < cycle) && (type == TXT_WRITE) )
250printk("\n[DBG] %s : thread %x exit after TX / cycle %d\n",
251__FUNCTION__ , CURRENT_THREAD , cycle );
252#endif
253
254#if (CONFIG_DEBUG_SYS_READ & 1)
255if( type == TXT_READ ) exit_tty_cmd_read = (uint32_t)hal_get_cycles();
256#endif
257
258#if (CONFIG_DEBUG_SYS_WRITE & 1)
259if( type == TXT_WRITE ) exit_tty_cmd_write = (uint32_t)hal_get_cycles();
260#endif
261
262}  // end soclib_tty_cmd()
263
264/////////////////////////////////////////////////////////////////
265void __attribute__ ((noinline)) soclib_tty_isr( chdev_t * chdev )
266{
267    thread_t   * server;       // pointer on TXT chdev server thread
268    lid_t        server_lid;   // local index of core running the server thread
269    uint32_t     channel;      // TXT chdev channel
270    bool_t       is_rx;        // TXT chdev direction
271    char         byte;         // byte value
272    xptr_t       owner_xp;     // extended pointer on foreground process in owner cluster
273    cxy_t        owner_cxy;   
274    process_t  * owner_ptr;
275    pid_t        owner_pid;
276    tty_fifo_t * fifo;         // pointer on TTY_TX or TTY_RX FIFO
277    cxy_t        tty_cxy;      // soclib_tty cluster
278    uint32_t *   tty_ptr;      // soclib_tty segment base address
279    uint32_t   * base;         // soclib_tty channel base address
280    xptr_t       status_xp;    // extended pointer on TTY_STATUS register
281    xptr_t       write_xp;     // extended pointer on TTY_WRITE register
282    xptr_t       read_xp;      // extended pointer on TTY_READ register
283
284    // get TXT chdev channel, direction and server thread
285    channel    = chdev->channel;
286    is_rx      = chdev->is_rx;
287    server     = chdev->server;
288    server_lid = server->core->lid;
289
290#if (CONFIG_DEBUG_SYS_READ & 1)
291if( is_rx ) enter_tty_isr_read = (uint32_t)hal_get_cycles();
292#endif
293
294#if (CONFIG_DEBUG_SYS_WRITE & 1)
295if( is_rx == 0 ) enter_tty_isr_write = (uint32_t)hal_get_cycles();
296#endif
297
298#if CONFIG_DEBUG_HAL_TXT_RX
299uint32_t cycle = (uint32_t)hal_get_cycles();
300if( (CONFIG_DEBUG_HAL_TXT_RX < cycle) && is_rx )
301printk("\n[DBG] %s : enter for RX / cycle %d\n", __FUNCTION__ , cycle );
302#endif
303
304#if CONFIG_DEBUG_HAL_TXT_TX
305uint32_t cycle = (uint32_t)hal_get_cycles();
306if( (CONFIG_DEBUG_HAL_TXT_TX < cycle) && (is_rx == 0) )
307printk("\n[DBG] %s : enter for TX / cycle %d\n", __FUNCTION__ , cycle );
308#endif
309
310    // get SOCLIB_TTY peripheral cluster and local pointer
311    tty_cxy = GET_CXY( chdev->base );
312    tty_ptr = GET_PTR( chdev->base );
313
314    // get channel base address
315    base    = tty_ptr + TTY_SPAN * channel;
316
317    // get extended pointer on TTY registers
318    status_xp = XPTR( tty_cxy , base + TTY_STATUS );
319    write_xp  = XPTR( tty_cxy , base + TTY_WRITE );
320    read_xp   = XPTR( tty_cxy , base + TTY_READ );
321
322    /////////////////////////// handle RX //////////////////////
323    if( is_rx )
324    {
325        fifo = &tty_rx_fifo[channel];
326
327        // try to move bytes until TTY_READ register empty
328        while( hal_remote_lw( status_xp ) & TTY_STATUS_RX_FULL )   
329        {
330            // get one byte from TTY_READ register & acknowledge RX_IRQ
331            byte = (char)hal_remote_lb( read_xp );
332
333            // filter special character ^Z
334            if( byte == 0x1A ) 
335            {
336                // get pointers on TXT owner process in owner cluster
337                owner_xp  = process_txt_get_owner( channel );
338               
339                // check process exist
340                assert( (owner_xp != XPTR_NULL) , __FUNCTION__, 
341                "TXT owner process not found\n" );
342
343                // get relevant infos on owner process
344                owner_cxy = GET_CXY( owner_xp );
345                owner_ptr = GET_PTR( owner_xp );
346                owner_pid = hal_remote_lw( XPTR( owner_cxy , &owner_ptr->pid ) );
347
348                // block owner process only if it is not a KSH
349                if( process_get_ppid( owner_xp ) > 1 )
350                {
351                    // send stop signal to owner process
352                    process_sigaction( owner_pid , BLOCK_ALL_THREADS );
353
354                    // atomically update owner process termination state
355                    hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
356                                          PROCESS_TERM_STOP );
357                    return;
358                }
359            }
360
361            // filter special character ^C
362            if( byte == 0x03 )
363            {
364                // get pointers on TXT owner process in owner cluster
365                owner_xp  = process_txt_get_owner( channel );
366
367                // check process exist
368                assert( (owner_xp != XPTR_NULL) , __FUNCTION__,
369                "TXT owner process not found\n" );
370
371                // get relevant infos on TXT owner process
372                owner_cxy = GET_CXY( owner_xp );
373                owner_ptr = GET_PTR( owner_xp );
374                owner_pid = hal_remote_lw( XPTR( owner_cxy , &owner_ptr->pid ) );
375
376                // kill TXT owner process only if it is not INIT
377                if( owner_pid != 1 )
378                {
379                    // remove process from TXT list
380                    process_txt_detach( owner_xp );
381
382                    // mark for delete all processes in all clusters, but the main
383                    process_sigaction( owner_pid , DELETE_ALL_THREADS );
384               
385                    // get pointer on target process main thread
386                    xptr_t main_xp = XPTR( owner_cxy , &owner_ptr->th_tbl[0] );
387
388                    // block main thread
389                    thread_block( main_xp , THREAD_BLOCKED_GLOBAL );
390
391                    // atomically update owner process termination state
392                    hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
393                                          PROCESS_TERM_KILL );
394                    return;
395                }
396            }
397
398            // write byte in TTY_RX FIFO if not full / discard byte if full
399            if ( fifo->sts < TTY_FIFO_DEPTH )
400            {
401                // store byte into FIFO
402                fifo->data[fifo->ptw] = (char)byte; 
403
404                // avoid race
405                hal_fence();
406
407                // update RX_FIFO state
408                fifo->ptw = (fifo->ptw + 1) % TTY_FIFO_DEPTH;
409                hal_atomic_add( &fifo->sts , 1 );
410
411                // unblock TXT_RX server thread
412                thread_unblock( XPTR( local_cxy , server ) , THREAD_BLOCKED_ISR );
413
414                // send IPI to core running server thread
415                dev_pic_send_ipi( local_cxy , server_lid );
416            }
417            else
418            {
419                printk("\n[WARNING] %s : TTY_RX_FIFO[%d] full => discard character <%x>\n",
420                __FUNCTION__, channel, (uint32_t)byte );
421            }
422        }  // end while TTY_READ register full
423
424    }  // end RX
425
426    ///////////////////////  handle TX  /////////////////////////////
427    else
428    {
429        fifo = &tty_tx_fifo[channel];
430
431        // try to move bytes until TX_FIFO empty
432        while( fifo->sts > 0 )
433        {
434            // write one byte to TTY_WRITE register if empty / exit while if full
435            if( (hal_remote_lw( status_xp ) & TTY_STATUS_TX_FULL) == 0 ) 
436            {
437                // get one byte from TX_FIFO
438                byte = fifo->data[fifo->ptr];
439
440                // update TX_FIFO state
441                fifo->ptr = (fifo->ptr + 1) % TTY_FIFO_DEPTH;
442                hal_atomic_add( &fifo->sts , -1 );
443
444                // write byte to TTY_WRITE register & acknowledge TX_IRQ
445                hal_remote_sb( write_xp , byte );
446            }
447        }
448
449        // disable TX_IRQ
450        hal_remote_sw( XPTR( tty_cxy , base + TTY_TX_IRQ_ENABLE ) , 0 );
451
452        // unblock TXT_TX server thread
453        thread_unblock( XPTR( local_cxy , server ) , THREAD_BLOCKED_ISR );
454
455        // send IPI to core running server thread
456        dev_pic_send_ipi( local_cxy , server_lid );
457    }  // end TX
458
459    hal_fence();
460
461#if CONFIG_DEBUG_HAL_TXT_RX
462cycle = (uint32_t)hal_get_cycles();
463if( (CONFIG_DEBUG_HAL_TXT_RX < cycle) && is_rx )
464printk("\n[DBG] %s : exit after RX / cycle %d\n", __FUNCTION__, cycle );
465#endif
466
467#if CONFIG_DEBUG_HAL_TXT_TX
468cycle = (uint32_t)hal_get_cycles();
469if( (CONFIG_DEBUG_HAL_TXT_TX < cycle) && (is_rx == 0) )
470printk("\n[DBG] %s : exit after TX / cycle %d\n", __FUNCTION__, cycle );
471#endif
472
473#if (CONFIG_DEBUG_SYS_READ & 1)
474if( is_rx ) exit_tty_isr_read = (uint32_t)hal_get_cycles();
475#endif
476
477#if (CONFIG_DEBUG_SYS_WRITE & 1)
478if( is_rx == 0 ) exit_tty_isr_write = (uint32_t)hal_get_cycles();
479#endif
480
481}  // end soclib_tty_isr()
482
483/////////////////////////////////////////////////////////////
484void __attribute__ ((noinline)) soclib_tty_aux( void * args )
485{
486    uint32_t   status;
487    bool_t     empty;
488    uint32_t   i;
489
490    xptr_t     dev_xp = ((txt_sync_args_t *)args)->dev_xp;
491    char     * buffer = ((txt_sync_args_t *)args)->buffer;
492    uint32_t   count  = ((txt_sync_args_t *)args)->count;
493   
494    // get TXT0 chdev cluster and local pointer
495    cxy_t     dev_cxy = GET_CXY( dev_xp );
496    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
497
498    // get extended pointer on TTY channel base address
499    xptr_t tty_xp = (xptr_t)hal_remote_lwd( XPTR( dev_cxy , &dev_ptr->base ) );
500
501    // get TTY channel segment cluster and local pointer
502    cxy_t      tty_cxy = GET_CXY( tty_xp );
503    uint32_t * tty_ptr = (uint32_t *)GET_PTR( tty_xp );
504
505    // get extended pointers on TTY_WRITE & TTY_STATUS registers
506    xptr_t write_xp  = XPTR( tty_cxy , tty_ptr + TTY_WRITE );
507    xptr_t status_xp = XPTR( tty_cxy , tty_ptr + TTY_STATUS );
508
509    // loop on characters (busy waiting policy)
510    for( i = 0 ; i < count ; i++ )
511    {
512        do
513        {
514            // get TTY_STATUS
515            status = hal_remote_lw( status_xp );
516            empty  = ( (status & TTY_STATUS_TX_FULL) == 0 );
517
518            // transfer one byte if TX buffer empty
519            if ( empty )  hal_remote_sb( write_xp , buffer[i] );
520        }
521        while ( empty == false );
522    }
523}  // end soclib_tty_aux()
524
525
526
Note: See TracBrowser for help on using the repository browser.