source: trunk/hal/tsar_mips32/drivers/soclib_mty.c

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

Mainly cosmetic.

File size: 21.2 KB
RevLine 
[533]1/*
[570]2 * soclib_mty.c - soclib mty driver implementation.
[533]3 *
4 * Author  Alain Greiner (2016,2017,2018)
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
25#include <hal_kernel_types.h>
26#include <dev_txt.h>
27#include <chdev.h>
[570]28#include <soclib_mty.h>
[533]29#include <thread.h>
30#include <printk.h>
31#include <hal_special.h>
32
[570]33extern   chdev_directory_t    chdev_dir;  // allocated in the kernel_init.c file.
[533]34
[570]35////////////////////////////////////////////////////////////////////////////////////
36// These global variables implement the MTY_RX  FIFOs (one per channel)
37////////////////////////////////////////////////////////////////////////////////////
38// Implementation note:
39// We allocate - in each cluster - two arrays of FIFOs containing as many entries
40// as the total number of TXT channels, but all entries are not used in all
41// clusters: for a given cluster K, a given entry corresponding to a given channel
42// and a given direction is only used if the associated chdev is in cluster K.
43// With this policy, the driver can ignore the actual placement of chdevs.
44////////////////////////////////////////////////////////////////////////////////////
[533]45
[570]46__attribute__((section(".kdata")))
47mty_fifo_t  mty_rx_fifo[CONFIG_MAX_TXT_CHANNELS];
[533]48
[570]49__attribute__((section(".kdata")))
50mty_fifo_t  mty_tx_fifo[CONFIG_MAX_TXT_CHANNELS];
[533]51
52////////////////////////////////////////////////////////////////////////////////////
[570]53// These global variables define the physical channel RX and TX state,
54// as required by the virtual channels handling.
[533]55////////////////////////////////////////////////////////////////////////////////////
[570]56// Implementation note:
57// These state variables are required, because the ISR is called twice for each
58// character on the physical channel (for index and value), and does not the same
59// actions for index and value. They are also used to implement the round-robin
60// policy between TX_FIFOs.
61////////////////////////////////////////////////////////////////////////////////////
[533]62
63__attribute__((section(".kdata")))
[570]64bool_t      mty_rx_not_new;                // RX first byte if false
[533]65
66__attribute__((section(".kdata")))
[570]67uint32_t    mty_rx_channel;                // RX virtual channel index
[533]68
[570]69__attribute__((section(".kdata")))
70bool_t      mty_tx_not_new;                // TX first byte if false
71
72__attribute__((section(".kdata")))
73uint32_t    mty_tx_value;                  // TX character ascii value
74
75__attribute__((section(".kdata")))
76uint32_t    mty_tx_last;                   // TX last selected virtual channel
77
[533]78///////////////////////////////////////
[570]79void soclib_mty_init( chdev_t * chdev )
[533]80{
81    xptr_t reg_xp;
82
83    // initialise function pointers in chdev
[570]84    chdev->cmd = &soclib_mty_cmd;
85    chdev->isr = &soclib_mty_isr;
86    chdev->aux = &soclib_mty_aux;
[533]87
[570]88    // get MTY channel and extended pointer on MTY peripheral base address
89    xptr_t   mty_xp  = chdev->base;
[533]90    uint32_t channel = chdev->channel;
91    bool_t   is_rx   = chdev->is_rx;
92
[570]93    // get SOCLIB_MTY device cluster and local pointer
94    cxy_t      mty_cxy = GET_CXY( mty_xp );
95    uint32_t * mty_ptr = GET_PTR( mty_xp );
[533]96
[570]97    // enable interruptions for RX
98    reg_xp = XPTR( mty_cxy , mty_ptr + MTY_CONFIG );
99    hal_remote_s32( reg_xp , MTY_CONFIG_RX_ENABLE );
[533]100
101    // reset relevant FIFO
102    if( is_rx )
103    {
[570]104        mty_rx_fifo[channel].sts = 0;
105        mty_rx_fifo[channel].ptr = 0;
106        mty_rx_fifo[channel].ptw = 0;
[533]107    }
108    else
109    {
[570]110        mty_tx_fifo[channel].sts = 0;
111        mty_tx_fifo[channel].ptr = 0;
112        mty_tx_fifo[channel].ptw = 0;
[533]113    }
[570]114}  // end soclib_mty_init()
[533]115
116//////////////////////////////////////////////////////////////
[570]117void __attribute__ ((noinline)) soclib_mty_cmd( xptr_t th_xp )
[533]118{
[570]119    mty_fifo_t * fifo;     // MTY_RX or MTY_TX FIFO
[533]120    char         byte;     // byte value
121    uint32_t     done;     // number of bytes moved
122
123    // get client thread cluster and local pointer
124    cxy_t      th_cxy = GET_CXY( th_xp );
125    thread_t * th_ptr = GET_PTR( th_xp );
126
127    // get command arguments
[570]128    uint32_t type     = hal_remote_l32 ( XPTR( th_cxy , &th_ptr->txt_cmd.type   ) );
129    xptr_t   buf_xp   = hal_remote_l64( XPTR( th_cxy , &th_ptr->txt_cmd.buf_xp ) );
130    uint32_t count    = hal_remote_l32 ( XPTR( th_cxy , &th_ptr->txt_cmd.count  ) );
[533]131    xptr_t   error_xp = XPTR( th_cxy , &th_ptr->txt_cmd.error );
132
133    // get TXT device cluster and pointers
[570]134    xptr_t     dev_xp = (xptr_t)hal_remote_l64( XPTR( th_cxy , &th_ptr->txt_cmd.dev_xp ) );
[533]135    cxy_t      dev_cxy = GET_CXY( dev_xp );
136    chdev_t  * dev_ptr = GET_PTR( dev_xp );
137
[570]138    // get MTY channel index and channel base address
139    uint32_t   channel = hal_remote_l32( XPTR( dev_cxy , &dev_ptr->channel ) );
[533]140
141    ///////////////////////
[570]142    if( type == TXT_WRITE )         // write bytes to MTY_TX FIFO
[533]143    {
[570]144        fifo = &mty_tx_fifo[channel];
[533]145
146        done = 0;
147
148        while( done < count )
149        {
[570]150            if( fifo->sts < MTY_FIFO_DEPTH )   // put one byte to FIFO if TX_FIFO not full
[533]151            {
152                // get one byte from command buffer
153                byte = hal_remote_lb( buf_xp + done );
154
155#if DEBUG_HAL_TXT_TX
156uint32_t tx_cycle = (uint32_t)hal_get_cycles();
157if( DEBUG_HAL_TXT_TX < tx_cycle )
158printk("\n[DBG] %s : thread %x put character <%c> to TXT%d_TX fifo / cycle %d\n",
159__FUNCTION__, CURRENT_THREAD, byte, channel, tx_cycle );
160#endif
161                // write byte to FIFO
162                fifo->data[fifo->ptw] = byte;
163
164                // prevent race
165                hal_fence();
166
167                // update FIFO state
[570]168                fifo->ptw = (fifo->ptw + 1) % MTY_FIFO_DEPTH;
[533]169                hal_atomic_add( &fifo->sts , 1 );
170
171                // udate number of bytes moved
172                done++;
173            }
174            else                                // block & deschedule if TX_FIFO full
175            {
176                // block on ISR
177                thread_block( XPTR( local_cxy , CURRENT_THREAD ) , THREAD_BLOCKED_ISR );
178
179                // deschedule
[570]180                sched_yield( "MTY_TX_FIFO full" ); 
[533]181            }
182        }
183
184        // set error status in command and return
[570]185        hal_remote_s32( error_xp , 0 );
[533]186    }
187    ///////////////////////////
[570]188    else if( type == TXT_READ )       // read bytes from MTY_RX FIFO   
[533]189    {
[570]190        fifo = &mty_rx_fifo[channel];
[533]191
192        done = 0;
193
194        while( done < count )
195        {
196            if( fifo->sts > 0 )               // get byte from FIFO if not empty
197            {
198                // get one byte from FIFO
199                char byte = fifo->data[fifo->ptr];
200
201#if DEBUG_HAL_TXT_RX
202uint32_t rx_cycle = (uint32_t)hal_get_cycles();
203if( DEBUG_HAL_TXT_RX < rx_cycle )
204printk("\n[DBG] %s : thread %x get character <%c> from TXT%d_RX fifo / cycle %d\n",
205__FUNCTION__, CURRENT_THREAD, byte, channel, rx_cycle );
206#endif
207                // update FIFO state
[570]208                fifo->ptr = (fifo->ptr + 1) % MTY_FIFO_DEPTH;
[533]209                hal_atomic_add( &fifo->sts , -1 );
210
211                // set byte to command buffer
212                hal_remote_sb( buf_xp + done , byte );
213
214                // udate number of bytes
215                done++;
216            }
217            else                             //  deschedule if FIFO empty
218            {
219                // block on ISR
220                thread_block( XPTR( local_cxy , CURRENT_THREAD ) , THREAD_BLOCKED_ISR );
221   
222                // deschedule
[570]223                sched_yield( "MTY_RX_FIFO empty" );
[533]224            }
225        }  // end while
226
227        // set error status in command
[570]228        hal_remote_s32( error_xp , 0 );
[533]229    }
230    else
231    {
[679]232        assert( __FUNCTION__, false , "illegal TXT command\n" );
[533]233    }
234
[570]235}  // end soclib_mty_cmd()
[533]236
237/////////////////////////////////////////////////////////////////
[570]238void __attribute__ ((noinline)) soclib_mty_isr( chdev_t * chdev )
[533]239{
240    thread_t   * server;            // pointer on TXT chdev server thread
241    lid_t        server_lid;        // local index of core running the server thread
[570]242    uint32_t     channel;           // TXT chdev channel (virtual channel index)
[533]243    bool_t       is_rx;             // TXT chdev direction
244    char         byte;              // byte value
245    xptr_t       owner_xp;          // extended pointer on TXT owner process
246    cxy_t        owner_cxy;         // TXT owner process cluster
247    process_t  * owner_ptr;         // local pointer on TXT owner process
248    pid_t        owner_pid;         // TXT owner process identifier
[570]249    mty_fifo_t * fifo;              // pointer on MTY_TX or MTY_RX FIFO
250    cxy_t        mty_cxy;           // soclib_mty cluster
251    uint32_t   * mty_ptr;           // soclib_mty segment base address
252    xptr_t       status_xp;         // extended pointer on MTY_STATUS register
253    xptr_t       write_xp;          // extended pointer on MTY_WRITE register
254    xptr_t       read_xp;           // extended pointer on MTY_READ register
[533]255    xptr_t       parent_xp;         // extended pointer on parent process
256    cxy_t        parent_cxy;        // parent process cluster
257    process_t  * parent_ptr;        // local pointer on parent process
258    thread_t   * parent_main_ptr;   // extended pointer on parent process main thread
259    xptr_t       parent_main_xp;    // local pointer on parent process main thread
[570]260    uint32_t     n;                 // index in loop
261    bool_t       found;
[533]262
263#if DEBUG_HAL_TXT_RX
264uint32_t rx_cycle = (uint32_t)hal_get_cycles();
265#endif
266
267#if DEBUG_HAL_TXT_TX
268uint32_t tx_cycle = (uint32_t)hal_get_cycles();
269#endif
270
[570]271    // get TXT chdev channel, direction and server thread
272    is_rx      = chdev->is_rx;
273    server     = chdev->server;
274    server_lid = server->core->lid;
[533]275
[570]276    // get SOCLIB_MTY peripheral cluster and local pointer
277    mty_cxy = GET_CXY( chdev->base );
278    mty_ptr = GET_PTR( chdev->base );
[533]279
[570]280    // get extended pointer on MTY registers
281    status_xp = XPTR( mty_cxy , mty_ptr + MTY_STATUS );
282    write_xp  = XPTR( mty_cxy , mty_ptr + MTY_WRITE );
283    read_xp   = XPTR( mty_cxy , mty_ptr + MTY_READ );
[533]284
285    /////////////////////////// handle RX //////////////////////
286    if( is_rx )
287    {
[570]288        // check one byte available in MTY_READ register
289        if( hal_remote_l32( status_xp ) & MTY_STATUS_RX_FULL )   
[533]290        {
[570]291            // get one byte from MTY_READ register & acknowledge RX_IRQ
[533]292            byte = (char)hal_remote_lb( read_xp );
293
[570]294            // test physical RX channel state
295            if( mty_rx_not_new == false )      // get first byte (virtual channel index)
[540]296            {
[570]297                // register virtual channel index
298                mty_rx_channel = (uint32_t)byte;
299
300                // update physical RX channel state
301                mty_rx_not_new = true;
[540]302            }
[570]303            else                               // get second byte (character value)
[540]304            {
[570]305                // get virtual channel index registered
306                channel = mty_rx_channel;
[540]307
[570]308                // get destination TX_FIFO
309                fifo = &mty_rx_fifo[channel];
[540]310
[570]311                // filter special character ^Z  => block TXT owner process
312                if( byte == 0x1A ) 
313                {
[540]314
[533]315#if DEBUG_HAL_TXT_RX
316if( DEBUG_HAL_TXT_RX < rx_cycle )
317printk("\n[DBG] %s : read ^Z character from TXT%d\n", __FUNCTION__, channel );
318#endif
[570]319                    // get pointers on TXT owner process in owner cluster
320                    owner_xp  = process_txt_get_owner( channel );
[533]321               
[570]322                    // check process exist
[679]323                    assert( __FUNCTION__, (owner_xp != XPTR_NULL) , "TXT owner process not found\n" );
[533]324
[570]325                    // get relevant infos on TXT owner process
326                    owner_cxy = GET_CXY( owner_xp );
327                    owner_ptr = GET_PTR( owner_xp );
328                    owner_pid = hal_remote_l32( XPTR( owner_cxy , &owner_ptr->pid ) );
[533]329
[570]330                    // block TXT owner process only if it is not the INIT process
331                    if( owner_pid != 1 )
332                    {
333                        // get parent process descriptor pointers
334                        parent_xp = hal_remote_l64( XPTR( owner_cxy , 
335                                                    &owner_ptr->parent_xp ) );
336                        parent_cxy = GET_CXY( parent_xp );
337                        parent_ptr = GET_PTR( parent_xp );
[533]338
[570]339                        // get pointers on the parent process main thread
340                        parent_main_ptr = hal_remote_lpt( XPTR(parent_cxy,
341                                                          &parent_ptr->th_tbl[0])); 
342                        parent_main_xp  = XPTR( parent_cxy , parent_main_ptr );
[533]343
[570]344                        // transfer TXT ownership
345                        process_txt_transfer_ownership( owner_xp );
[533]346
[570]347                        // block all threads in all clusters, but the main thread
348                        process_sigaction( owner_pid , BLOCK_ALL_THREADS );
[533]349
[570]350                        // block the main thread
351                        xptr_t main_xp = XPTR( owner_cxy , &owner_ptr->th_tbl[0] );
352                        thread_block( main_xp , THREAD_BLOCKED_GLOBAL );
[533]353
[570]354                        // atomically update owner process termination state
355                        hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
356                                              PROCESS_TERM_STOP );
[533]357
[570]358                        // take the children lock and unblock the parent process main thread
359                        thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT );
[533]360
[570]361                        return;
362                    }
[533]363                }
364
[570]365                // filter special character ^C  => kill TXT owner process
366                if( byte == 0x03 )
367                {
[533]368
369#if DEBUG_HAL_TXT_RX
370if( DEBUG_HAL_TXT_RX < rx_cycle )
371printk("\n[DBG] %s : read ^C character from TXT%d\n", __FUNCTION__, channel );
372#endif
[570]373                    // get pointer on TXT owner process in owner cluster
374                    owner_xp  = process_txt_get_owner( channel );
[533]375
[570]376                    // check process exist
[679]377                    assert( __FUNCTION__, (owner_xp != XPTR_NULL) , "TXT owner process not found\n" );
[533]378
[570]379                    // get relevant infos on TXT owner process
380                    owner_cxy = GET_CXY( owner_xp );
381                    owner_ptr = GET_PTR( owner_xp );
382                    owner_pid = hal_remote_l32( XPTR( owner_cxy , &owner_ptr->pid ) );
[533]383
[570]384                    // kill TXT owner process only if it is not the INIT process
385                    if( owner_pid != 1 )
386                    {
387                        // get parent process descriptor pointers
388                        parent_xp  = hal_remote_l64( XPTR( owner_cxy,
389                                                     &owner_ptr->parent_xp ) );
390                        parent_cxy = GET_CXY( parent_xp );
391                        parent_ptr = GET_PTR( parent_xp );
[533]392
[570]393                        // get pointers on the parent process main thread
394                        parent_main_ptr = hal_remote_lpt( XPTR( parent_cxy,
395                                                          &parent_ptr->th_tbl[0])); 
396                        parent_main_xp  = XPTR( parent_cxy , parent_main_ptr );
[533]397
[570]398                        // remove process from TXT list
399                        process_txt_detach( owner_xp );
[533]400
[570]401                        // mark for delete all thread in all clusters, but the main
402                        process_sigaction( owner_pid , DELETE_ALL_THREADS );
[533]403               
[570]404                        // block main thread
405                        xptr_t main_xp = XPTR( owner_cxy , &owner_ptr->th_tbl[0] );
406                        thread_block( main_xp , THREAD_BLOCKED_GLOBAL );
[533]407
[570]408                        // atomically update owner process termination state
409                        hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
410                                              PROCESS_TERM_KILL );
[533]411
[570]412                        // take the children lock and unblock the parent process main thread
413                        thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT );
[533]414
[570]415                        return;
416                    }
[533]417                }
418
[570]419                // write byte in RX FIFO if not full / discard byte if full
420                if ( fifo->sts < MTY_FIFO_DEPTH )
421                {
[533]422
423#if DEBUG_HAL_TXT_RX
424if( DEBUG_HAL_TXT_RX < rx_cycle )
425printk("\n[DBG] %s : put character <%c> to TXT%d_RX fifo\n",
426__FUNCTION__, byte, channel );
427#endif
[570]428                    // store byte into FIFO
429                    fifo->data[fifo->ptw] = (char)byte; 
[533]430
[570]431                    // avoid race
432                    hal_fence();
[533]433
[570]434                    // update RX_FIFO state
435                    fifo->ptw = (fifo->ptw + 1) % MTY_FIFO_DEPTH;
436                    hal_atomic_add( &fifo->sts , 1 );
[533]437
[570]438                    // unblock TXT_RX server thread
439                    thread_unblock( XPTR( local_cxy , server ) , THREAD_BLOCKED_ISR );
[533]440
[570]441                    // send IPI to core running server thread
442                    dev_pic_send_ipi( local_cxy , server_lid );
443                }
444                else
445                {
446                    printk("\n[WARNING] %s : MTY_RX_FIFO[%d] full => discard char <%x>\n",
447                    __FUNCTION__, channel, (uint32_t)byte );
448                }
[533]449
[570]450                // update physical RX channel state
451                mty_rx_not_new = true;
452
453            }  // end get character value
454        }  // end if byte available in MTY_READ register
[533]455    }  // end RX
456
457    ///////////////////////  handle TX  /////////////////////////////
458    else
459    {
[570]460        // test physical TX channel state
461        if( mty_tx_not_new == false )      // send first byte (virtual channel index)
[533]462        {
[570]463            // scan the set of the TX_FIFO
464            for( n = 0 , found = false ; n < CONFIG_MAX_TXT_CHANNELS ; n++ )
[533]465            {
[570]466                // implement round-robin policy
467                channel = (n + mty_tx_last + 1) % CONFIG_MAX_TXT_CHANNELS;
468
469                // get pointer on TX_FIFO[channel]
470                fifo = &mty_tx_fifo[channel];
471
472                if( fifo->sts > 0 )
473                {
474                    found = true;
475                    break;
476                }
477            }
478
479            // get one byte from TX_FIFO if found and send channel index
480            if( found ) 
481            {
482                // get one byte from selected TX_FIFO
[533]483                byte = fifo->data[fifo->ptr];
484
485#if DEBUG_HAL_TXT_TX
486if( DEBUG_HAL_TXT_TX < tx_cycle )
487printk("\n[DBG] %s : get character <%c> from TXT%d_TX fifo\n",
488__FUNCTION__, byte, channel );
489#endif
490                // update TX_FIFO state
[570]491                fifo->ptr = (fifo->ptr + 1) % MTY_FIFO_DEPTH;
[533]492                hal_atomic_add( &fifo->sts , -1 );
493
[570]494                // update TX physical channel state
495                mty_tx_value   = (uint32_t)byte;
496                mty_tx_not_new = true;
497                mty_tx_last    = channel;
[533]498
[570]499                // write virtual channel index to TX_WRITE register
500                hal_remote_sb( write_xp , (uint8_t)channel );
[533]501
[570]502                // unblock TXT_TX server thread
503                thread_unblock( XPTR( local_cxy , server ) , THREAD_BLOCKED_ISR );
[533]504
[570]505                // send IPI to core running server thread
506                dev_pic_send_ipi( local_cxy , server_lid );
507            }
508        }
509        else                             // send second byte (character value)
510        {
511            // write registered character value to TX_WRITE register
512            hal_remote_sb( write_xp , (uint8_t)mty_tx_value );
[533]513
[570]514            // update TX physical channel state
515            mty_tx_not_new = false;
516        }  // end if MTY_WRITE register empty
[533]517    }  // end TX
518
519    hal_fence();
520
[570]521}  // end soclib_mty_isr()
[533]522
523/////////////////////////////////////////////////////////////
[570]524void __attribute__ ((noinline)) soclib_mty_aux( void * args )
[533]525{
[570]526    uint32_t   i;
527
528    xptr_t       dev_xp  = ((txt_sync_args_t *)args)->dev_xp;
529    const char * buffer  = ((txt_sync_args_t *)args)->buffer;
530    uint32_t     count   = ((txt_sync_args_t *)args)->count;
531    uint32_t     channel = ((txt_sync_args_t *)args)->channel;
[533]532   
[570]533    // get chdev cluster and local pointer
[533]534    cxy_t     dev_cxy = GET_CXY( dev_xp );
[570]535    chdev_t * dev_ptr = GET_PTR( dev_xp );
[533]536
[570]537    // get extended pointer on MTY channel base address
538    xptr_t mty_xp = (xptr_t)hal_remote_l64( XPTR( dev_cxy , &dev_ptr->base ) );
[533]539
[570]540    // get MTY channel segment cluster and local pointer
541    cxy_t      mty_cxy = GET_CXY( mty_xp );
542    uint32_t * mty_ptr = GET_PTR( mty_xp );
[533]543
[570]544    // get extended pointers on MTY_WRITE & MTY_STATUS registers
545    xptr_t write_xp  = XPTR( mty_cxy , mty_ptr + MTY_WRITE );
[533]546
[570]547    // loop on characters (two bytes per character)
[533]548    for( i = 0 ; i < count ; i++ )
549    {
[570]550        // write virtual channel index to MTY_WRITE register
551        hal_remote_sb( write_xp , (uint8_t)channel );
[539]552
[570]553        // write character value to MTY_WRITE register
554        hal_remote_sb( write_xp , buffer[i] );
[533]555    }
[570]556}  // end soclib_mty_aux()
[533]557
558
559
Note: See TracBrowser for help on using the repository browser.