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

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

Modify the GPT (Generic Page Table) API to support remote accesses,
in order to improve page faults and COW handling.

File size: 21.3 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    {
232        assert( false , __FUNCTION__ , "illegal TXT command\n" );
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
323                    assert( (owner_xp != XPTR_NULL) , __FUNCTION__, 
324                    "TXT owner process not found\n" );
[533]325
[570]326                    // get relevant infos on TXT owner process
327                    owner_cxy = GET_CXY( owner_xp );
328                    owner_ptr = GET_PTR( owner_xp );
329                    owner_pid = hal_remote_l32( XPTR( owner_cxy , &owner_ptr->pid ) );
[533]330
[570]331                    // block TXT owner process only if it is not the INIT process
332                    if( owner_pid != 1 )
333                    {
334                        // get parent process descriptor pointers
335                        parent_xp = hal_remote_l64( XPTR( owner_cxy , 
336                                                    &owner_ptr->parent_xp ) );
337                        parent_cxy = GET_CXY( parent_xp );
338                        parent_ptr = GET_PTR( parent_xp );
[533]339
[570]340                        // get pointers on the parent process main thread
341                        parent_main_ptr = hal_remote_lpt( XPTR(parent_cxy,
342                                                          &parent_ptr->th_tbl[0])); 
343                        parent_main_xp  = XPTR( parent_cxy , parent_main_ptr );
[533]344
[570]345                        // transfer TXT ownership
346                        process_txt_transfer_ownership( owner_xp );
[533]347
[570]348                        // block all threads in all clusters, but the main thread
349                        process_sigaction( owner_pid , BLOCK_ALL_THREADS );
[533]350
[570]351                        // block the main thread
352                        xptr_t main_xp = XPTR( owner_cxy , &owner_ptr->th_tbl[0] );
353                        thread_block( main_xp , THREAD_BLOCKED_GLOBAL );
[533]354
[570]355                        // atomically update owner process termination state
356                        hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
357                                              PROCESS_TERM_STOP );
[533]358
[570]359                        // take the children lock and unblock the parent process main thread
360                        thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT );
[533]361
[570]362                        return;
363                    }
[533]364                }
365
[570]366                // filter special character ^C  => kill TXT owner process
367                if( byte == 0x03 )
368                {
[533]369
370#if DEBUG_HAL_TXT_RX
371if( DEBUG_HAL_TXT_RX < rx_cycle )
372printk("\n[DBG] %s : read ^C character from TXT%d\n", __FUNCTION__, channel );
373#endif
[570]374                    // get pointer on TXT owner process in owner cluster
375                    owner_xp  = process_txt_get_owner( channel );
[533]376
[570]377                    // check process exist
378                    assert( (owner_xp != XPTR_NULL) , __FUNCTION__,
379                    "TXT owner process not found\n" );
[533]380
[570]381                    // get relevant infos on TXT owner process
382                    owner_cxy = GET_CXY( owner_xp );
383                    owner_ptr = GET_PTR( owner_xp );
384                    owner_pid = hal_remote_l32( XPTR( owner_cxy , &owner_ptr->pid ) );
[533]385
[570]386                    // kill TXT owner process only if it is not the INIT process
387                    if( owner_pid != 1 )
388                    {
389                        // get parent process descriptor pointers
390                        parent_xp  = hal_remote_l64( XPTR( owner_cxy,
391                                                     &owner_ptr->parent_xp ) );
392                        parent_cxy = GET_CXY( parent_xp );
393                        parent_ptr = GET_PTR( parent_xp );
[533]394
[570]395                        // get pointers on the parent process main thread
396                        parent_main_ptr = hal_remote_lpt( XPTR( parent_cxy,
397                                                          &parent_ptr->th_tbl[0])); 
398                        parent_main_xp  = XPTR( parent_cxy , parent_main_ptr );
[533]399
[570]400                        // remove process from TXT list
401                        process_txt_detach( owner_xp );
[533]402
[570]403                        // mark for delete all thread in all clusters, but the main
404                        process_sigaction( owner_pid , DELETE_ALL_THREADS );
[533]405               
[570]406                        // block main thread
407                        xptr_t main_xp = XPTR( owner_cxy , &owner_ptr->th_tbl[0] );
408                        thread_block( main_xp , THREAD_BLOCKED_GLOBAL );
[533]409
[570]410                        // atomically update owner process termination state
411                        hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
412                                              PROCESS_TERM_KILL );
[533]413
[570]414                        // take the children lock and unblock the parent process main thread
415                        thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT );
[533]416
[570]417                        return;
418                    }
[533]419                }
420
[570]421                // write byte in RX FIFO if not full / discard byte if full
422                if ( fifo->sts < MTY_FIFO_DEPTH )
423                {
[533]424
425#if DEBUG_HAL_TXT_RX
426if( DEBUG_HAL_TXT_RX < rx_cycle )
427printk("\n[DBG] %s : put character <%c> to TXT%d_RX fifo\n",
428__FUNCTION__, byte, channel );
429#endif
[570]430                    // store byte into FIFO
431                    fifo->data[fifo->ptw] = (char)byte; 
[533]432
[570]433                    // avoid race
434                    hal_fence();
[533]435
[570]436                    // update RX_FIFO state
437                    fifo->ptw = (fifo->ptw + 1) % MTY_FIFO_DEPTH;
438                    hal_atomic_add( &fifo->sts , 1 );
[533]439
[570]440                    // unblock TXT_RX server thread
441                    thread_unblock( XPTR( local_cxy , server ) , THREAD_BLOCKED_ISR );
[533]442
[570]443                    // send IPI to core running server thread
444                    dev_pic_send_ipi( local_cxy , server_lid );
445                }
446                else
447                {
448                    printk("\n[WARNING] %s : MTY_RX_FIFO[%d] full => discard char <%x>\n",
449                    __FUNCTION__, channel, (uint32_t)byte );
450                }
[533]451
[570]452                // update physical RX channel state
453                mty_rx_not_new = true;
454
455            }  // end get character value
456        }  // end if byte available in MTY_READ register
[533]457    }  // end RX
458
459    ///////////////////////  handle TX  /////////////////////////////
460    else
461    {
[570]462        // test physical TX channel state
463        if( mty_tx_not_new == false )      // send first byte (virtual channel index)
[533]464        {
[570]465            // scan the set of the TX_FIFO
466            for( n = 0 , found = false ; n < CONFIG_MAX_TXT_CHANNELS ; n++ )
[533]467            {
[570]468                // implement round-robin policy
469                channel = (n + mty_tx_last + 1) % CONFIG_MAX_TXT_CHANNELS;
470
471                // get pointer on TX_FIFO[channel]
472                fifo = &mty_tx_fifo[channel];
473
474                if( fifo->sts > 0 )
475                {
476                    found = true;
477                    break;
478                }
479            }
480
481            // get one byte from TX_FIFO if found and send channel index
482            if( found ) 
483            {
484                // get one byte from selected TX_FIFO
[533]485                byte = fifo->data[fifo->ptr];
486
487#if DEBUG_HAL_TXT_TX
488if( DEBUG_HAL_TXT_TX < tx_cycle )
489printk("\n[DBG] %s : get character <%c> from TXT%d_TX fifo\n",
490__FUNCTION__, byte, channel );
491#endif
492                // update TX_FIFO state
[570]493                fifo->ptr = (fifo->ptr + 1) % MTY_FIFO_DEPTH;
[533]494                hal_atomic_add( &fifo->sts , -1 );
495
[570]496                // update TX physical channel state
497                mty_tx_value   = (uint32_t)byte;
498                mty_tx_not_new = true;
499                mty_tx_last    = channel;
[533]500
[570]501                // write virtual channel index to TX_WRITE register
502                hal_remote_sb( write_xp , (uint8_t)channel );
[533]503
[570]504                // unblock TXT_TX server thread
505                thread_unblock( XPTR( local_cxy , server ) , THREAD_BLOCKED_ISR );
[533]506
[570]507                // send IPI to core running server thread
508                dev_pic_send_ipi( local_cxy , server_lid );
509            }
510        }
511        else                             // send second byte (character value)
512        {
513            // write registered character value to TX_WRITE register
514            hal_remote_sb( write_xp , (uint8_t)mty_tx_value );
[533]515
[570]516            // update TX physical channel state
517            mty_tx_not_new = false;
518        }  // end if MTY_WRITE register empty
[533]519    }  // end TX
520
521    hal_fence();
522
[570]523}  // end soclib_mty_isr()
[533]524
525/////////////////////////////////////////////////////////////
[570]526void __attribute__ ((noinline)) soclib_mty_aux( void * args )
[533]527{
[570]528    uint32_t   i;
529
530    xptr_t       dev_xp  = ((txt_sync_args_t *)args)->dev_xp;
531    const char * buffer  = ((txt_sync_args_t *)args)->buffer;
532    uint32_t     count   = ((txt_sync_args_t *)args)->count;
533    uint32_t     channel = ((txt_sync_args_t *)args)->channel;
[533]534   
[570]535    // get chdev cluster and local pointer
[533]536    cxy_t     dev_cxy = GET_CXY( dev_xp );
[570]537    chdev_t * dev_ptr = GET_PTR( dev_xp );
[533]538
[570]539    // get extended pointer on MTY channel base address
540    xptr_t mty_xp = (xptr_t)hal_remote_l64( XPTR( dev_cxy , &dev_ptr->base ) );
[533]541
[570]542    // get MTY channel segment cluster and local pointer
543    cxy_t      mty_cxy = GET_CXY( mty_xp );
544    uint32_t * mty_ptr = GET_PTR( mty_xp );
[533]545
[570]546    // get extended pointers on MTY_WRITE & MTY_STATUS registers
547    xptr_t write_xp  = XPTR( mty_cxy , mty_ptr + MTY_WRITE );
[533]548
[570]549    // loop on characters (two bytes per character)
[533]550    for( i = 0 ; i < count ; i++ )
551    {
[570]552        // write virtual channel index to MTY_WRITE register
553        hal_remote_sb( write_xp , (uint8_t)channel );
[539]554
[570]555        // write character value to MTY_WRITE register
556        hal_remote_sb( write_xp , buffer[i] );
[533]557    }
[570]558}  // end soclib_mty_aux()
[533]559
560
561
Note: See TracBrowser for help on using the repository browser.