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

Last change on this file since 601 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
Line 
1/*
2 * soclib_mty.c - soclib mty driver implementation.
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>
28#include <soclib_mty.h>
29#include <thread.h>
30#include <printk.h>
31#include <hal_special.h>
32
33extern   chdev_directory_t    chdev_dir;  // allocated in the kernel_init.c file.
34
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////////////////////////////////////////////////////////////////////////////////////
45
46__attribute__((section(".kdata")))
47mty_fifo_t  mty_rx_fifo[CONFIG_MAX_TXT_CHANNELS];
48
49__attribute__((section(".kdata")))
50mty_fifo_t  mty_tx_fifo[CONFIG_MAX_TXT_CHANNELS];
51
52////////////////////////////////////////////////////////////////////////////////////
53// These global variables define the physical channel RX and TX state,
54// as required by the virtual channels handling.
55////////////////////////////////////////////////////////////////////////////////////
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////////////////////////////////////////////////////////////////////////////////////
62
63__attribute__((section(".kdata")))
64bool_t      mty_rx_not_new;                // RX first byte if false
65
66__attribute__((section(".kdata")))
67uint32_t    mty_rx_channel;                // RX virtual channel index
68
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
78///////////////////////////////////////
79void soclib_mty_init( chdev_t * chdev )
80{
81    xptr_t reg_xp;
82
83    // initialise function pointers in chdev
84    chdev->cmd = &soclib_mty_cmd;
85    chdev->isr = &soclib_mty_isr;
86    chdev->aux = &soclib_mty_aux;
87
88    // get MTY channel and extended pointer on MTY peripheral base address
89    xptr_t   mty_xp  = chdev->base;
90    uint32_t channel = chdev->channel;
91    bool_t   is_rx   = chdev->is_rx;
92
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 );
96
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 );
100
101    // reset relevant FIFO
102    if( is_rx )
103    {
104        mty_rx_fifo[channel].sts = 0;
105        mty_rx_fifo[channel].ptr = 0;
106        mty_rx_fifo[channel].ptw = 0;
107    }
108    else
109    {
110        mty_tx_fifo[channel].sts = 0;
111        mty_tx_fifo[channel].ptr = 0;
112        mty_tx_fifo[channel].ptw = 0;
113    }
114}  // end soclib_mty_init()
115
116//////////////////////////////////////////////////////////////
117void __attribute__ ((noinline)) soclib_mty_cmd( xptr_t th_xp )
118{
119    mty_fifo_t * fifo;     // MTY_RX or MTY_TX FIFO
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
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  ) );
131    xptr_t   error_xp = XPTR( th_cxy , &th_ptr->txt_cmd.error );
132
133    // get TXT device cluster and pointers
134    xptr_t     dev_xp = (xptr_t)hal_remote_l64( XPTR( th_cxy , &th_ptr->txt_cmd.dev_xp ) );
135    cxy_t      dev_cxy = GET_CXY( dev_xp );
136    chdev_t  * dev_ptr = GET_PTR( dev_xp );
137
138    // get MTY channel index and channel base address
139    uint32_t   channel = hal_remote_l32( XPTR( dev_cxy , &dev_ptr->channel ) );
140
141    ///////////////////////
142    if( type == TXT_WRITE )         // write bytes to MTY_TX FIFO
143    {
144        fifo = &mty_tx_fifo[channel];
145
146        done = 0;
147
148        while( done < count )
149        {
150            if( fifo->sts < MTY_FIFO_DEPTH )   // put one byte to FIFO if TX_FIFO not full
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
168                fifo->ptw = (fifo->ptw + 1) % MTY_FIFO_DEPTH;
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
180                sched_yield( "MTY_TX_FIFO full" ); 
181            }
182        }
183
184        // set error status in command and return
185        hal_remote_s32( error_xp , 0 );
186    }
187    ///////////////////////////
188    else if( type == TXT_READ )       // read bytes from MTY_RX FIFO   
189    {
190        fifo = &mty_rx_fifo[channel];
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
208                fifo->ptr = (fifo->ptr + 1) % MTY_FIFO_DEPTH;
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
223                sched_yield( "MTY_RX_FIFO empty" );
224            }
225        }  // end while
226
227        // set error status in command
228        hal_remote_s32( error_xp , 0 );
229    }
230    else
231    {
232        assert( false , __FUNCTION__ , "illegal TXT command\n" );
233    }
234
235}  // end soclib_mty_cmd()
236
237/////////////////////////////////////////////////////////////////
238void __attribute__ ((noinline)) soclib_mty_isr( chdev_t * chdev )
239{
240    thread_t   * server;            // pointer on TXT chdev server thread
241    lid_t        server_lid;        // local index of core running the server thread
242    uint32_t     channel;           // TXT chdev channel (virtual channel index)
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
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
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
260    uint32_t     n;                 // index in loop
261    bool_t       found;
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
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;
275
276    // get SOCLIB_MTY peripheral cluster and local pointer
277    mty_cxy = GET_CXY( chdev->base );
278    mty_ptr = GET_PTR( chdev->base );
279
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 );
284
285    /////////////////////////// handle RX //////////////////////
286    if( is_rx )
287    {
288        // check one byte available in MTY_READ register
289        if( hal_remote_l32( status_xp ) & MTY_STATUS_RX_FULL )   
290        {
291            // get one byte from MTY_READ register & acknowledge RX_IRQ
292            byte = (char)hal_remote_lb( read_xp );
293
294            // test physical RX channel state
295            if( mty_rx_not_new == false )      // get first byte (virtual channel index)
296            {
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;
302            }
303            else                               // get second byte (character value)
304            {
305                // get virtual channel index registered
306                channel = mty_rx_channel;
307
308                // get destination TX_FIFO
309                fifo = &mty_rx_fifo[channel];
310
311                // filter special character ^Z  => block TXT owner process
312                if( byte == 0x1A ) 
313                {
314
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
319                    // get pointers on TXT owner process in owner cluster
320                    owner_xp  = process_txt_get_owner( channel );
321               
322                    // check process exist
323                    assert( (owner_xp != XPTR_NULL) , __FUNCTION__, 
324                    "TXT owner process not found\n" );
325
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 ) );
330
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 );
339
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 );
344
345                        // transfer TXT ownership
346                        process_txt_transfer_ownership( owner_xp );
347
348                        // block all threads in all clusters, but the main thread
349                        process_sigaction( owner_pid , BLOCK_ALL_THREADS );
350
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 );
354
355                        // atomically update owner process termination state
356                        hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
357                                              PROCESS_TERM_STOP );
358
359                        // take the children lock and unblock the parent process main thread
360                        thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT );
361
362                        return;
363                    }
364                }
365
366                // filter special character ^C  => kill TXT owner process
367                if( byte == 0x03 )
368                {
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
374                    // get pointer on TXT owner process in owner cluster
375                    owner_xp  = process_txt_get_owner( channel );
376
377                    // check process exist
378                    assert( (owner_xp != XPTR_NULL) , __FUNCTION__,
379                    "TXT owner process not found\n" );
380
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 ) );
385
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 );
394
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 );
399
400                        // remove process from TXT list
401                        process_txt_detach( owner_xp );
402
403                        // mark for delete all thread in all clusters, but the main
404                        process_sigaction( owner_pid , DELETE_ALL_THREADS );
405               
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 );
409
410                        // atomically update owner process termination state
411                        hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) ,
412                                              PROCESS_TERM_KILL );
413
414                        // take the children lock and unblock the parent process main thread
415                        thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT );
416
417                        return;
418                    }
419                }
420
421                // write byte in RX FIFO if not full / discard byte if full
422                if ( fifo->sts < MTY_FIFO_DEPTH )
423                {
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
430                    // store byte into FIFO
431                    fifo->data[fifo->ptw] = (char)byte; 
432
433                    // avoid race
434                    hal_fence();
435
436                    // update RX_FIFO state
437                    fifo->ptw = (fifo->ptw + 1) % MTY_FIFO_DEPTH;
438                    hal_atomic_add( &fifo->sts , 1 );
439
440                    // unblock TXT_RX server thread
441                    thread_unblock( XPTR( local_cxy , server ) , THREAD_BLOCKED_ISR );
442
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                }
451
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
457    }  // end RX
458
459    ///////////////////////  handle TX  /////////////////////////////
460    else
461    {
462        // test physical TX channel state
463        if( mty_tx_not_new == false )      // send first byte (virtual channel index)
464        {
465            // scan the set of the TX_FIFO
466            for( n = 0 , found = false ; n < CONFIG_MAX_TXT_CHANNELS ; n++ )
467            {
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
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
493                fifo->ptr = (fifo->ptr + 1) % MTY_FIFO_DEPTH;
494                hal_atomic_add( &fifo->sts , -1 );
495
496                // update TX physical channel state
497                mty_tx_value   = (uint32_t)byte;
498                mty_tx_not_new = true;
499                mty_tx_last    = channel;
500
501                // write virtual channel index to TX_WRITE register
502                hal_remote_sb( write_xp , (uint8_t)channel );
503
504                // unblock TXT_TX server thread
505                thread_unblock( XPTR( local_cxy , server ) , THREAD_BLOCKED_ISR );
506
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 );
515
516            // update TX physical channel state
517            mty_tx_not_new = false;
518        }  // end if MTY_WRITE register empty
519    }  // end TX
520
521    hal_fence();
522
523}  // end soclib_mty_isr()
524
525/////////////////////////////////////////////////////////////
526void __attribute__ ((noinline)) soclib_mty_aux( void * args )
527{
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;
534   
535    // get chdev cluster and local pointer
536    cxy_t     dev_cxy = GET_CXY( dev_xp );
537    chdev_t * dev_ptr = GET_PTR( dev_xp );
538
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 ) );
541
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 );
545
546    // get extended pointers on MTY_WRITE & MTY_STATUS registers
547    xptr_t write_xp  = XPTR( mty_cxy , mty_ptr + MTY_WRITE );
548
549    // loop on characters (two bytes per character)
550    for( i = 0 ; i < count ; i++ )
551    {
552        // write virtual channel index to MTY_WRITE register
553        hal_remote_sb( write_xp , (uint8_t)channel );
554
555        // write character value to MTY_WRITE register
556        hal_remote_sb( write_xp , buffer[i] );
557    }
558}  // end soclib_mty_aux()
559
560
561
Note: See TracBrowser for help on using the repository browser.