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

Last change on this file since 440 was 440, checked in by alain, 4 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
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 (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 (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 (DEBUG_SYS_READ & 1)
118if( type == TXT_READ) enter_tty_cmd_read = (uint32_t)hal_get_cycles();
119#endif
120
121#if (DEBUG_SYS_WRITE & 1)
122if( type == TXT_WRITE) enter_tty_cmd_write = (uint32_t)hal_get_cycles();
123#endif
124
125#if DEBUG_HAL_TXT_RX
126uint32_t cycle = (uint32_t)hal_get_cycles();
127if( (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 DEBUG_HAL_TXT_TX
133uint32_t cycle = (uint32_t)hal_get_cycles();
134if( (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 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 );
143
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 ) );
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
155    {
156        fifo = &tty_tx_fifo[channel];
157
158        done = 0;
159
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 );
166
167                // write byte to FIFO
168                fifo->data[fifo->ptw] = byte;
169
170                // prevent race
171                hal_fence();
172
173                // update FIFO state
174                fifo->ptw = (fifo->ptw + 1) % TTY_FIFO_DEPTH;
175                hal_atomic_add( &fifo->sts , 1 );
176
177                // udate number of bytes moved
178                done++;
179
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 );
195    }
196    ///////////////////////////
197    else if( type == TXT_READ )       // read bytes from TTY_RX FIFO   
198    {
199        fifo = &tty_rx_fifo[channel];
200
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 );
232    }
233    else
234    {
235        assert( false , __FUNCTION__ , "illegal TXT command\n" );
236    }
237
238#if DEBUG_HAL_TXT_RX
239cycle = (uint32_t)hal_get_cycles();
240if( (DEBUG_HAL_TXT_RX < cycle) && (type == TXT_READ) )
241printk("\n[DBG] %s : thread %x exit after RX / cycle %d\n",
242__FUNCTION__ , CURRENT_THREAD , cycle );
243#endif
244
245#if DEBUG_HAL_TXT_TX
246cycle = (uint32_t)hal_get_cycles();
247if( (DEBUG_HAL_TXT_TX < cycle) && (type == TXT_WRITE) )
248printk("\n[DBG] %s : thread %x exit after TX / cycle %d\n",
249__FUNCTION__ , CURRENT_THREAD , cycle );
250#endif
251
252#if (DEBUG_SYS_READ & 1)
253if( type == TXT_READ ) exit_tty_cmd_read = (uint32_t)hal_get_cycles();
254#endif
255
256#if (DEBUG_SYS_WRITE & 1)
257if( type == TXT_WRITE ) exit_tty_cmd_write = (uint32_t)hal_get_cycles();
258#endif
259
260}  // end soclib_tty_cmd()
261
262/////////////////////////////////////////////////////////////////
263void __attribute__ ((noinline)) soclib_tty_isr( chdev_t * chdev )
264{
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
281
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
288#if (DEBUG_SYS_READ & 1)
289if( is_rx ) enter_tty_isr_read = (uint32_t)hal_get_cycles();
290#endif
291
292#if (DEBUG_SYS_WRITE & 1)
293if( is_rx == 0 ) enter_tty_isr_write = (uint32_t)hal_get_cycles();
294#endif
295
296#if DEBUG_HAL_TXT_RX
297uint32_t cycle = (uint32_t)hal_get_cycles();
298if( (DEBUG_HAL_TXT_RX < cycle) && is_rx )
299printk("\n[DBG] %s : enter for RX / cycle %d\n", __FUNCTION__ , cycle );
300#endif
301
302#if DEBUG_HAL_TXT_TX
303uint32_t cycle = (uint32_t)hal_get_cycles();
304if( (DEBUG_HAL_TXT_TX < cycle) && (is_rx == 0) )
305printk("\n[DBG] %s : enter for TX / cycle %d\n", __FUNCTION__ , cycle );
306#endif
307
308    // get SOCLIB_TTY peripheral cluster and local pointer
309    tty_cxy = GET_CXY( chdev->base );
310    tty_ptr = GET_PTR( chdev->base );
311
312    // get channel base address
313    base    = tty_ptr + TTY_SPAN * channel;
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
320    /////////////////////////// handle RX //////////////////////
321    if( is_rx )
322    {
323        fifo = &tty_rx_fifo[channel];
324
325        // try to move bytes until TTY_READ register empty
326        while( hal_remote_lw( status_xp ) & TTY_STATUS_RX_FULL )   
327        {
328            // get one byte from TTY_READ register & acknowledge RX_IRQ
329            byte = (char)hal_remote_lb( read_xp );
330
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" );
340
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 ) );
345
346                // block owner process only if it is not INIT or KSH
347                if( process_get_ppid( owner_xp ) > 1 )
348                {
349                    // send stop signal to owner process
350                    process_sigaction( owner_pid , BLOCK_ALL_THREADS );
351
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            {
362                // get pointer on TXT owner process in owner cluster
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
380                    // mark for delete all thread in all clusters, but the main
381                    process_sigaction( owner_pid , DELETE_ALL_THREADS );
382               
383                    // get pointer on target process main thread
384                    xptr_t main_xp = XPTR( owner_cxy , &owner_ptr->th_tbl[0] );
385
386                    // block main thread
387                    thread_block( main_xp , THREAD_BLOCKED_GLOBAL );
388
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            }
395
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
426    {
427        fifo = &tty_tx_fifo[channel];
428
429        // try to move bytes until TX_FIFO empty
430        while( fifo->sts > 0 )
431        {
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 ) 
434            {
435                // get one byte from TX_FIFO
436                byte = fifo->data[fifo->ptr];
437
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
443                hal_remote_sb( write_xp , byte );
444            }
445        }
446
447        // disable TX_IRQ
448        hal_remote_sw( XPTR( tty_cxy , base + TTY_TX_IRQ_ENABLE ) , 0 );
449
450        // unblock TXT_TX server thread
451        thread_unblock( XPTR( local_cxy , server ) , THREAD_BLOCKED_ISR );
452
453        // send IPI to core running server thread
454        dev_pic_send_ipi( local_cxy , server_lid );
455    }  // end TX
456
457    hal_fence();
458
459#if DEBUG_HAL_TXT_RX
460cycle = (uint32_t)hal_get_cycles();
461if( (DEBUG_HAL_TXT_RX < cycle) && is_rx )
462printk("\n[DBG] %s : exit after RX / cycle %d\n", __FUNCTION__, cycle );
463#endif
464
465#if DEBUG_HAL_TXT_TX
466cycle = (uint32_t)hal_get_cycles();
467if( (DEBUG_HAL_TXT_TX < cycle) && (is_rx == 0) )
468printk("\n[DBG] %s : exit after TX / cycle %d\n", __FUNCTION__, cycle );
469#endif
470
471#if (DEBUG_SYS_READ & 1)
472if( is_rx ) exit_tty_isr_read = (uint32_t)hal_get_cycles();
473#endif
474
475#if (DEBUG_SYS_WRITE & 1)
476if( is_rx == 0 ) exit_tty_isr_write = (uint32_t)hal_get_cycles();
477#endif
478
479}  // end soclib_tty_isr()
480
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.