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

Last change on this file since 446 was 446, checked in by alain, 3 years ago

miscelaneous...

File size: 20.3 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 rx_cycle = (uint32_t)hal_get_cycles();
127if( (DEBUG_HAL_TXT_RX < rx_cycle) && (type == TXT_READ) )
128printk("\n[DBG] %s : thread %x enter for RX / cycle %d\n",
129__FUNCTION__ , CURRENT_THREAD , rx_cycle );
130#endif
131
132#if DEBUG_HAL_TXT_TX
133uint32_t tx_cycle = (uint32_t)hal_get_cycles();
134if( (DEBUG_HAL_TXT_TX < tx_cycle) && (type == TXT_WRITE) )
135printk("\n[DBG] %s : thread %x enter for TX / cycle %d\n",
136__FUNCTION__ , CURRENT_THREAD , tx_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_RX_FIFO 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
239rx_cycle = (uint32_t)hal_get_cycles();
240if( (DEBUG_HAL_TXT_RX < rx_cycle) && (type == TXT_READ) )
241printk("\n[DBG] %s : thread %x exit after RX / cycle %d\n",
242__FUNCTION__ , CURRENT_THREAD , rx_cycle );
243#endif
244
245#if DEBUG_HAL_TXT_TX
246tx_cycle = (uint32_t)hal_get_cycles();
247if( (DEBUG_HAL_TXT_TX < tx_cycle) && (type == TXT_WRITE) )
248printk("\n[DBG] %s : thread %x exit after TX / cycle %d\n",
249__FUNCTION__ , CURRENT_THREAD , tx_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 TXT owner process
271    cxy_t        owner_cxy;         // TXT owner process cluster
272    process_t  * owner_ptr;         // local pointer on TXT owner process
273    pid_t        owner_pid;         // TXT owner process identifier
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    xptr_t       parent_xp;         // extended pointer on parent process
282    cxy_t        parent_cxy;        // parent process cluster
283    process_t  * parent_ptr;        // local pointer on parent process
284    xptr_t       children_lock_xp;  // extended pointer on children processes lock
285    thread_t   * parent_main_ptr;   // extended pointer on parent process main thread
286    xptr_t       parent_main_xp;    // local pointer on parent process main thread
287
288    // get TXT chdev channel, direction and server thread
289    channel    = chdev->channel;
290    is_rx      = chdev->is_rx;
291    server     = chdev->server;
292    server_lid = server->core->lid;
293
294#if (DEBUG_SYS_READ & 1)
295if( is_rx ) enter_tty_isr_read = (uint32_t)hal_get_cycles();
296#endif
297
298#if (DEBUG_SYS_WRITE & 1)
299if( is_rx == 0 ) enter_tty_isr_write = (uint32_t)hal_get_cycles();
300#endif
301
302#if DEBUG_HAL_TXT_RX
303uint32_t rx_cycle = (uint32_t)hal_get_cycles();
304#endif
305
306#if DEBUG_HAL_TXT_TX
307uint32_t tx_cycle = (uint32_t)hal_get_cycles();
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  => block TXT owner process
334            if( byte == 0x1A ) 
335            {
336
337#if DEBUG_HAL_TXT_RX
338if( DEBUG_HAL_TXT_RX < rx_cycle )
339printk("\n[DBG] %s : read ^Z character from TXT%d\n", __FUNCTION__, channel );
340#endif
341                // get pointers on TXT owner process in owner cluster
342                owner_xp  = process_txt_get_owner( channel );
343               
344                // check process exist
345                assert( (owner_xp != XPTR_NULL) , __FUNCTION__, 
346                "TXT owner process not found\n" );
347
348                // get relevant infos on TXT owner process
349                owner_cxy = GET_CXY( owner_xp );
350                owner_ptr = GET_PTR( owner_xp );
351                owner_pid = hal_remote_lw( XPTR( owner_cxy , &owner_ptr->pid ) );
352
353                // block TXT owner process only if it is not the INIT process
354                if( owner_pid != 1 )
355                {
356                    // get parent process descriptor pointers
357                    parent_xp  = hal_remote_lwd( XPTR( owner_cxy , &owner_ptr->parent_xp ) );
358                    parent_cxy = GET_CXY( parent_xp );
359                    parent_ptr = GET_PTR( parent_xp );
360
361                    // get extended pointer on lock protecting children list in parent process
362                    children_lock_xp = XPTR( parent_cxy , &parent_ptr->children_lock ); 
363
364                    // get pointers on the parent process main thread
365                    parent_main_ptr = hal_remote_lpt(XPTR(parent_cxy,&parent_ptr->th_tbl[0])); 
366                    parent_main_xp  = XPTR( parent_cxy , parent_main_ptr );
367
368                    // transfer TXT ownership
369                    process_txt_transfer_ownership( owner_xp );
370
371                    // block all threads in all clusters, but the main thread
372                    process_sigaction( owner_pid , BLOCK_ALL_THREADS );
373
374                    // block the main thread
375                    xptr_t main_xp = XPTR( owner_cxy , &owner_ptr->th_tbl[0] );
376                    thread_block( main_xp , THREAD_BLOCKED_GLOBAL );
377
378                    // atomically update owner process termination state
379                    hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
380                                          PROCESS_TERM_STOP );
381
382                    // take the children lock and unblock the parent process main thread
383                    remote_spinlock_lock( children_lock_xp );
384                    thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT );
385                    remote_spinlock_unlock( children_lock_xp );
386
387                    return;
388                }
389            }
390
391            // filter special character ^C  => kill TXT owner process
392            if( byte == 0x03 )
393            {
394
395#if DEBUG_HAL_TXT_RX
396if( DEBUG_HAL_TXT_RX < rx_cycle )
397printk("\n[DBG] %s : read ^C character from TXT%d\n", __FUNCTION__, channel );
398#endif
399                // get pointer on TXT owner process in owner cluster
400                owner_xp  = process_txt_get_owner( channel );
401
402                // check process exist
403                assert( (owner_xp != XPTR_NULL) , __FUNCTION__,
404                "TXT owner process not found\n" );
405
406                // get relevant infos on TXT owner process
407                owner_cxy = GET_CXY( owner_xp );
408                owner_ptr = GET_PTR( owner_xp );
409                owner_pid = hal_remote_lw( XPTR( owner_cxy , &owner_ptr->pid ) );
410
411                // kill TXT owner process only if it is not the INIT process
412                if( owner_pid != 1 )
413                {
414                    // get parent process descriptor pointers
415                    parent_xp  = hal_remote_lwd( XPTR( owner_cxy , &owner_ptr->parent_xp ) );
416                    parent_cxy = GET_CXY( parent_xp );
417                    parent_ptr = GET_PTR( parent_xp );
418
419                    // get extended pointer on lock protecting children list in parent process
420                    children_lock_xp = XPTR( parent_cxy , &parent_ptr->children_lock ); 
421
422                    // get pointers on the parent process main thread
423                    parent_main_ptr = hal_remote_lpt(XPTR(parent_cxy,&parent_ptr->th_tbl[0])); 
424                    parent_main_xp  = XPTR( parent_cxy , parent_main_ptr );
425
426                    // remove process from TXT list
427                    process_txt_detach( owner_xp );
428
429                    // mark for delete all thread in all clusters, but the main
430                    process_sigaction( owner_pid , DELETE_ALL_THREADS );
431               
432                    // block main thread
433                    xptr_t main_xp = XPTR( owner_cxy , &owner_ptr->th_tbl[0] );
434                    thread_block( main_xp , THREAD_BLOCKED_GLOBAL );
435
436                    // atomically update owner process termination state
437                    hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
438                                          PROCESS_TERM_KILL );
439
440                    // take the children lock and unblock the parent process main thread
441                    remote_spinlock_lock( children_lock_xp );
442                    thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT );
443                    remote_spinlock_unlock( children_lock_xp );
444
445                    return;
446                }
447            }
448
449            // write byte in TTY_RX FIFO if not full / discard byte if full
450            if ( fifo->sts < TTY_FIFO_DEPTH )
451            {
452
453#if DEBUG_HAL_TXT_RX
454if( DEBUG_HAL_TXT_RX < rx_cycle )
455printk("\n[DBG] %s : read character %c from TXT%d\n", __FUNCTION__, byte, channel );
456#endif
457                // store byte into FIFO
458                fifo->data[fifo->ptw] = (char)byte; 
459
460                // avoid race
461                hal_fence();
462
463                // update RX_FIFO state
464                fifo->ptw = (fifo->ptw + 1) % TTY_FIFO_DEPTH;
465                hal_atomic_add( &fifo->sts , 1 );
466
467                // unblock TXT_RX server thread
468                thread_unblock( XPTR( local_cxy , server ) , THREAD_BLOCKED_ISR );
469
470                // send IPI to core running server thread
471                dev_pic_send_ipi( local_cxy , server_lid );
472            }
473            else
474            {
475                printk("\n[WARNING] %s : TTY_RX_FIFO[%d] full => discard character <%x>\n",
476                __FUNCTION__, channel, (uint32_t)byte );
477            }
478        }  // end while TTY_READ register full
479
480    }  // end RX
481
482    ///////////////////////  handle TX  /////////////////////////////
483    else
484    {
485        fifo = &tty_tx_fifo[channel];
486
487        // try to move bytes until TX_FIFO empty
488        while( fifo->sts > 0 )
489        {
490            // write one byte to TTY_WRITE register if empty / exit loop if full
491            if( (hal_remote_lw( status_xp ) & TTY_STATUS_TX_FULL) == 0 ) 
492            {
493                // get one byte from TX_FIFO
494                byte = fifo->data[fifo->ptr];
495
496#if DEBUG_HAL_TXT_TX
497if( DEBUG_HAL_TXT_TX < tx_cycle )
498printk("\n[DBG] %s : write character %c to TXT%d\n", __FUNCTION__, byte, channel );
499#endif
500                // update TX_FIFO state
501                fifo->ptr = (fifo->ptr + 1) % TTY_FIFO_DEPTH;
502                hal_atomic_add( &fifo->sts , -1 );
503
504                // write byte to TTY_WRITE register & acknowledge TX_IRQ
505                hal_remote_sb( write_xp , byte );
506            }
507        }
508
509        // disable TX_IRQ
510        hal_remote_sw( XPTR( tty_cxy , base + TTY_TX_IRQ_ENABLE ) , 0 );
511
512        // unblock TXT_TX server thread
513        thread_unblock( XPTR( local_cxy , server ) , THREAD_BLOCKED_ISR );
514
515        // send IPI to core running server thread
516        dev_pic_send_ipi( local_cxy , server_lid );
517
518    }  // end TX
519
520    hal_fence();
521
522#if (DEBUG_SYS_READ & 1)
523if( is_rx ) exit_tty_isr_read = (uint32_t)hal_get_cycles();
524#endif
525
526#if (DEBUG_SYS_WRITE & 1)
527if( is_rx == 0 ) exit_tty_isr_write = (uint32_t)hal_get_cycles();
528#endif
529
530}  // end soclib_tty_isr()
531
532/////////////////////////////////////////////////////////////
533void __attribute__ ((noinline)) soclib_tty_aux( void * args )
534{
535    uint32_t   status;
536    bool_t     empty;
537    uint32_t   i;
538
539    xptr_t     dev_xp = ((txt_sync_args_t *)args)->dev_xp;
540    char     * buffer = ((txt_sync_args_t *)args)->buffer;
541    uint32_t   count  = ((txt_sync_args_t *)args)->count;
542   
543    // get TXT0 chdev cluster and local pointer
544    cxy_t     dev_cxy = GET_CXY( dev_xp );
545    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
546
547    // get extended pointer on TTY channel base address
548    xptr_t tty_xp = (xptr_t)hal_remote_lwd( XPTR( dev_cxy , &dev_ptr->base ) );
549
550    // get TTY channel segment cluster and local pointer
551    cxy_t      tty_cxy = GET_CXY( tty_xp );
552    uint32_t * tty_ptr = (uint32_t *)GET_PTR( tty_xp );
553
554    // get extended pointers on TTY_WRITE & TTY_STATUS registers
555    xptr_t write_xp  = XPTR( tty_cxy , tty_ptr + TTY_WRITE );
556    xptr_t status_xp = XPTR( tty_cxy , tty_ptr + TTY_STATUS );
557
558    // loop on characters (busy waiting policy)
559    for( i = 0 ; i < count ; i++ )
560    {
561        do
562        {
563            // get TTY_STATUS
564            status = hal_remote_lw( status_xp );
565            empty  = ( (status & TTY_STATUS_TX_FULL) == 0 );
566
567            // transfer one byte if TX buffer empty
568            if ( empty )  hal_remote_sb( write_xp , buffer[i] );
569        }
570        while ( empty == false );
571    }
572}  // end soclib_tty_aux()
573
574
575
Note: See TracBrowser for help on using the repository browser.