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

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

1/ Fix a bug in the Multithreaded "sort" applicationr:
The pthread_create() arguments must be declared as global variables.
2/ The exit syscall can be called by any thread of a process..

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