source: trunk/softs/giet_tsar/stdio.c @ 623

Last change on this file since 623 was 622, checked in by alain, 10 years ago

Introducing a minimal GIET:

  • no virtual memory
  • no conyext switch
  • no system calls
File size: 37.7 KB
Line 
1////////////////////////////////////////////////////////////////////////////////////////
2// File : stdio.c
3// Written by Alain Greiner
4// Date : janvier 2014
5//
6// This file define varions functions that can be used by applications to access
7// peripherals, for the TSAR multi-processors multi_clusters architecture.
8// There is NO separation between application code and system code, as the
9// application are running in kernel mode without system calls.
10// This basic GIET does not support virtual memory, and does not support multi-tasking.
11//
12// The supported peripherals are:
13// - the SoClib multi_tty
14// - The SoCLib frame_buffer
15// - The SoCLib block_device
16//
17// The following parameters must be defined in the hard_config.h file.
18// - X_SIZE          : number of clusters in a row
19// - Y_SIZE          : number of clusters in a column
20// - X_WIDTH         : number of bits for X field in proc_id
21// - Y_WIDTH         : number of bits for Y field in proc_id
22// - NB_PROCS_MAX    : max number of processor per cluster
23// - NB_TTY_CHANNELS : max number of TTY channels
24//
25// The follobing base addresses must be defined in the ldscript
26// - seg_tty_base
27// - seg_fbf_base
28// - seg_ioc_base
29////////////////////////////////////////////////////////////////////////////////////////
30
31#include "stdio.h"
32
33#define NB_LOCKS      256
34#define NB_BARRIERS   16
35
36#define in_drivers __attribute__((section (".drivers")))
37#define in_unckdata __attribute__((section (".unckdata")))
38
39//////////////////////////////////////////////////////////////
40// various informations that must be defined in ldscript
41//////////////////////////////////////////////////////////////
42
43struct plouf;
44
45extern struct plouf seg_tty_base;
46extern struct plouf seg_fbf_base;
47extern struct plouf seg_ioc_base;
48extern struct plouf seg_mmc_base;
49
50////////////////////////////////////////////////////////////////////////////////////////
51//  Global uncachable variables for synchronization between drivers and ISRs
52////////////////////////////////////////////////////////////////////////////////////////
53
54in_unckdata int volatile    _ioc_lock    = 0;
55in_unckdata int volatile    _ioc_done    = 0;
56in_unckdata int volatile    _ioc_status;
57
58in_unckdata char volatile   _tty_get_buf[NB_TTY_CHANNELS];
59in_unckdata int volatile    _tty_get_full[NB_TTY_CHANNELS] = { [0 ... NB_TTY_CHANNELS-1] = 0 };
60
61////////////////////////////////////////////////////////////////////////////////////////
62//  Global uncachable variables for inter-task barriers
63////////////////////////////////////////////////////////////////////////////////////////
64
65in_unckdata int volatile    _barrier_value[NB_BARRIERS]  = { [0 ... NB_BARRIERS-1] = 0 };
66in_unckdata int volatile    _barrier_count[NB_BARRIERS]  = { [0 ... NB_BARRIERS-1] = 0 };
67in_unckdata int volatile    _barrier_lock[NB_BARRIERS]   = { [0 ... NB_BARRIERS-1] = 0 };
68
69////////////////////////////////////////////////////////////////////////////////////////
70//  Global uncachable variables for spin_locks using LL/C instructions
71////////////////////////////////////////////////////////////////////////////////////////
72
73in_unckdata int volatile    _spin_lock[NB_LOCKS] =    { [0 ... NB_LOCKS-1] = 0 };
74
75////////////////////////////////////////////////////////////////////////////////////////
76// Taken from MutekH.
77////////////////////////////////////////////////////////////////////////////////////////
78in_drivers void* _memcpy( void*        _dst, 
79                          const void*  _src, 
80                          unsigned int size )
81{
82    unsigned int *dst = _dst;
83    const unsigned int *src = _src;
84    if ( ! ((unsigned int)dst & 3) && ! ((unsigned int)src & 3) )
85    {
86        while (size > 3) 
87        {
88            *dst++ = *src++;
89            size -= 4;
90        }
91    }
92
93    unsigned char *cdst = (unsigned char*)dst;
94    unsigned char *csrc = (unsigned char*)src;
95
96    while (size--) 
97    {
98        *cdst++ = *csrc++;
99    }
100    return _dst;
101}
102
103////////////////////////////////////////////////////////////////////////////////////////
104// Access CP0 and returns processor ident
105// No more than 1024 processors...
106////////////////////////////////////////////////////////////////////////////////////////
107in_drivers unsigned int _procid()
108{
109    unsigned int ret;
110    asm volatile( "mfc0 %0, $15, 1": "=r"(ret) );
111    return (ret & 0x3FF);
112}
113////////////////////////////////////////////////////////////////////////////////////////
114// Access CP0 and returns processor time
115////////////////////////////////////////////////////////////////////////////////////////
116in_drivers unsigned int _proctime()
117{
118    unsigned int ret;
119    asm volatile( "mfc0 %0, $9": "=r"(ret) );
120    return ret;
121}
122////////////////////////////////////////////////////////////////////////////////////////
123// Returns the number of processsors controled by the GIET
124////////////////////////////////////////////////////////////////////////////////////////
125in_drivers unsigned int _procnumber()
126{
127    return (unsigned int)(NB_PROCS_MAX * X_SIZE * Y_SIZE);
128}
129////////////////////////////////////////////////////////////////////////////////////////
130// Access CP0 and mask IRQs
131////////////////////////////////////////////////////////////////////////////////////////
132in_drivers void _it_mask()
133{
134    int tmp;
135    asm volatile("mfc0  %0, $12"    : "=r" (tmp) );
136    asm volatile("ori   %0, %0, 1"  : "=r" (tmp) );
137    asm volatile("mtc0  %0, $12"    : "=r" (tmp) );
138}
139////////////////////////////////////////////////////////////////////////////////////////
140// Access CP0 and enable IRQs
141////////////////////////////////////////////////////////////////////////////////////////
142in_drivers void _it_enable()
143{
144    int tmp;
145    asm volatile("mfc0  %0, $12"    : "=r" (tmp) );
146    asm volatile("addi  %0, %0, -1" : "=r" (tmp) );
147    asm volatile("mtc0  %0, $12"    : "=r" (tmp) );
148}
149//////////////////////////////////////////////////////////////////////
150// Invalidate all cache lines corresponding to a memory buffer.
151// This is used by the block_device driver.
152/////////////////////////////////////////////////////////////////////////
153in_drivers void _dcache_buf_invalidate(const void * buffer, size_t size)
154{
155    size_t i;
156    size_t dcache_line_size;
157
158    // retrieve dcache line size from config register (bits 12:10)
159    asm volatile("mfc0 %0, $16, 1" : "=r" (dcache_line_size));
160
161    dcache_line_size = 2 << ((dcache_line_size>>10) & 0x7);
162
163    // iterate on lines to invalidate each one of them
164    for ( i=0; i<size; i+=dcache_line_size )
165        asm volatile(" cache %0, %1"
166                :
167                :"i" (0x11), "R" (*((char*)buffer+i)));
168}
169
170///////////////////////////////////////////////////////////////////////////////////////
171// Exit (suicide) after printing message on  a TTY terminal.
172///////////////////////////////////////////////////////////////////////////////////////
173in_drivers void _exit()
174{
175    unsigned int proc_id = _procid();
176    unsigned int l       = proc_id % NB_PROCS_MAX;
177    unsigned int x       = (proc_id / NB_PROCS_MAX) >> Y_WIDTH;
178    unsigned int y       = (proc_id / NB_PROCS_MAX) & ((1<<Y_WIDTH) - 1);
179
180    _tty_printf("\n\n!!!  Exit  Processor (%d,%d,%d)  !!!\n", x, y, l );
181
182    while(1) asm volatile("nop");   // infinite loop...
183}
184
185/////////////////////////////////////////////////////////////////////////
186// convert a 32 bits unsigned int to a string of 10 decimal characters.
187/////////////////////////////////////////////////////////////////////////
188in_drivers void _itoa_dec(unsigned val, char* buf)
189{
190    const char  DecTab[] = "0123456789";
191    unsigned int i;
192    for( i=0 ; i<10 ; i++ )
193    {
194        if( (val!=0) || (i==0) ) buf[9-i] = DecTab[val % 10];
195        else                     buf[9-i] = 0x20;
196        val /= 10;
197    }
198}
199//////////////////////////////////////////////////////////////////////////
200// convert a 32 bits unsigned int to a string of 8 hexadecimal characters.
201///////////////////////////////////////////////////////////////////////////
202in_drivers void _itoa_hex(unsigned int val, char* buf)
203{
204    const char  HexaTab[] = "0123456789ABCD";
205    unsigned int i;
206    for( i=0 ; i<8 ; i++ )
207    {
208        buf[7-i] = HexaTab[val % 16];
209        val /= 16;
210    }
211}
212
213
214///////////////////////////////////////////////////////////////////////////////////////
215// VCI MULTI_TTY
216///////////////////////////////////////////////////////////////////////////////////////
217//  The total number of TTY terminals is defined by NB_TTY_CHANNELS.
218//  1. If there is only one terminal, it is supposed to be shared, and used by
219//     all processors: a lock must be taken before display.
220//  2. If there is several terminals, and the number of processors is smaller
221//     than the number of terminals, there is one terminal per processor, but
222//     the TTY index is not equal to the proc_id, due to cluster indexing policy:
223//     - proc_id = cluster_xy * NB_PROCS_MAX + local_id (with cluster_xy = x << Y_WIDTH + y)
224//     - tty_id  = cluster_id * NB_PROCS_MAX + local_id (with cluster_id = x * Y_SIZE + y)
225//  3. If the computed tty_id is larger than NB_TTY_CHANNELS, an error is returned.
226///////////////////////////////////////////////////////////////////////////////////////
227// Write one or several characters directly from a fixed length user buffer
228// to the TTY_WRITE register of the TTY controler.
229// This is a non blocking call : it test the TTY_STATUS register.
230// If the TTY_STATUS_WRITE bit is set, the transfer stops and the function
231// returns  the number of characters that have been actually written.
232///////////////////////////////////////////////////////////////////////////////////////
233in_drivers int _tty_write( char*           buffer, 
234                           unsigned int    length, 
235                           unsigned int    channel )
236{
237    char*           tty_address;
238    unsigned int    base                = (unsigned int)&seg_tty_base;
239    unsigned int    nwritten    = 0;
240    int i;
241
242    tty_address = (char*)(base + channel*TTY_SPAN*4);
243
244    for ( i=0 ; i < length ; i++ )
245    {
246        if((tty_address[TTY_STATUS*4] & 0x2) == 0x2)  break;
247        else
248        {
249            tty_address[TTY_WRITE*4] = buffer[i]; // write character
250            nwritten++;
251        }
252    }
253
254    return nwritten;
255}
256///////////////////////////////////////////////////////////////////////////////////////
257// Fetch one character directly from the TTY_READ register of the TTY controler,
258// and writes this character to the user buffer.
259// This is a non blocking call : it returns 0 if the register is empty,
260// and returns 1 if the register is full.
261///////////////////////////////////////////////////////////////////////////////////////
262in_drivers int _tty_read( char*          buffer, 
263                          unsigned int   channel )
264{
265    char*           tty_address;
266    unsigned int    base                = (unsigned int)&seg_tty_base;
267
268    tty_address = (char*)(base + channel*TTY_SPAN*4);
269
270    if((tty_address[TTY_STATUS*4] & 0x1) == 0x1)
271    {
272        buffer[0] = tty_address[TTY_READ*4];
273        return 1;
274    }
275    else
276    {
277        return 0;
278    }
279}
280//////////////////////////////////////////////////////////////////////////////
281// This function displays a string on TTY0.
282// The string must be terminated by a NUL character.
283//////////////////////////////////////////////////////////////////////////////
284in_drivers void _tty_puts( char* string )
285{
286    int length = 0;
287    while (string[length] != 0) length++;
288    _tty_write( string, length, 0 );
289}
290
291///////////////////////////////////////////////////////////////////////////////
292// This function displays a 32 bits unsigned int as an hexa string on TTY0.
293///////////////////////////////////////////////////////////////////////////////
294in_drivers void _tty_putx(unsigned int val) 
295{
296    static const char HexaTab[] = "0123456789ABCDEF";
297    char buf[11];
298    unsigned int c;
299
300    buf[0] = '0';
301    buf[1] = 'x';
302    buf[10] = 0;
303
304    for (c = 0; c < 8; c++) 
305    { 
306        buf[9 - c] = HexaTab[val & 0xF];
307        val = val >> 4;
308    }
309    _tty_puts( buf );
310}
311
312///////////////////////////////////////////////////////////////////////////////
313// This function displays a 32 bits unsigned int as a decimal string on TTY0.
314///////////////////////////////////////////////////////////////////////////////
315in_drivers void _tty_putd( unsigned int val ) 
316{
317    static const char DecTab[] = "0123456789";
318    char buf[11];
319    unsigned int i;
320    unsigned int first;
321
322    buf[10] = 0;
323
324    for (i = 0; i < 10; i++) 
325    {
326        if ((val != 0) || (i == 0)) 
327        {
328            buf[9 - i] = DecTab[val % 10];
329            first = 9 - i;
330        }
331        else 
332        {
333            break;
334        }
335        val /= 10;
336    }
337    _tty_puts( &buf[first] );
338}
339
340//////////////////////////////////////////////////////////////////////////////
341// This function try to take the hardwired lock protecting exclusive access
342// to TTY terminal identified by the channel argument.
343// It returns only when the lock has been successfully taken.
344//////////////////////////////////////////////////////////////////////////////
345in_drivers void _tty_get_lock( unsigned int channel )
346{
347    unsigned int* tty_address = (unsigned int *) &seg_tty_base;
348    while ( tty_address[channel * TTY_SPAN + TTY_CONFIG] ) asm volatile("nop"); 
349}
350
351//////////////////////////////////////////////////////////////////////////////
352// This function releases the hardwired lock protecting exclusive access
353// to TTY terminal identified by the channel argument.
354//////////////////////////////////////////////////////////////////////////////
355in_drivers void _tty_release_lock( unsigned int channel )
356{
357    unsigned int* tty_address = (unsigned int *) &seg_tty_base;
358    tty_address[channel * TTY_SPAN + TTY_CONFIG] = 0;
359}
360
361//////////////////////////////////////////////////////////////////////////////
362// This function fetch a single ascii character from a terminal
363// implicitely defined by the processor ID.
364// It is a blocking function.
365//////////////////////////////////////////////////////////////////////////////
366in_drivers void _tty_getc( char* buf )
367{
368    unsigned int proc_id = _procid();
369    unsigned int channel;
370    unsigned int l;
371    unsigned int x;
372    unsigned int y;
373
374    // compute TTY terminal index
375    if ( NB_TTY_CHANNELS == 1 )
376    {
377        channel = 0;
378    }
379    else
380    {
381        l           = (proc_id % NB_PROCS_MAX);
382        x           = (proc_id / NB_PROCS_MAX) >> Y_WIDTH; 
383        y           = (proc_id / NB_PROCS_MAX) & ((1<<Y_WIDTH) - 1);
384        channel = (x * Y_SIZE + y) * NB_PROCS_MAX + l;
385        if (channel >= NB_TTY_CHANNELS )
386        {
387            _tty_get_lock( 0 );
388            _tty_puts( "ERROR in _tty_getc()\n" );
389            _tty_release_lock( 0 );
390            _exit();
391        }
392    }
393
394    while( _tty_read( buf, channel ) == 0 ) asm volatile("nop");
395}
396
397//////////////////////////////////////////////////////////////////////////////
398//  Fetch a string of decimal characters (most significant digit first)
399//  to build a 32 bits unsigned int.
400//  The terminal index is implicitely defined by the processor ID.
401//  This is a blocking function.
402//  The decimal characters are written in a 32 characters buffer
403//  until a <LF> or <CR> character is read.
404//  The <DEL> character is interpreted, and previous characters can be
405//  cancelled. All others characters are ignored.
406//  When the <LF> or <CR> character is received, the string is converted
407//  to an unsigned int value. If the number of decimal digit is too large
408//  for the 32 bits range, the zero value is returned.
409//////////////////////////////////////////////////////////////////////////////
410in_drivers void _tty_getw( unsigned int* word_buffer )
411{
412    char          buf[32];
413    char          byte;
414    char          cancel_string[3] = { 0x08, 0x20, 0x08 };
415    char          zero             = 0x30;
416    unsigned int  save = 0;
417    unsigned int  val = 0;
418    unsigned int  done = 0;
419    unsigned int  overflow = 0;
420    unsigned int  max = 0;
421    unsigned int  proc_id = _procid();
422    unsigned int  i;
423    unsigned int  channel;
424    unsigned int  l;
425    unsigned int  x;
426    unsigned int  y;
427
428    // compute TTY terminal index
429    if ( NB_TTY_CHANNELS == 1 )
430    {
431        channel = 0;
432    }
433    else
434    {
435        l           = (proc_id % NB_PROCS_MAX);
436        x           = (proc_id / NB_PROCS_MAX) >> Y_WIDTH; 
437        y           = (proc_id / NB_PROCS_MAX) & ((1<<Y_WIDTH) - 1);
438        channel = (x * Y_SIZE + y) * NB_PROCS_MAX + l;
439        if (channel >= NB_TTY_CHANNELS )   
440        {
441            _tty_get_lock( 0 );
442            _tty_puts( "ERROR in _tty_getw()\n" );
443            _tty_release_lock( 0 );
444            _exit();
445        }
446    }
447
448    while( done == 0 )
449    {
450        _tty_read( &byte, channel );
451
452        if (( byte > 0x2F) && (byte < 0x3A))  // decimal character
453        {
454            buf[max] = byte;
455            max++;
456            _tty_write( &byte, 1, channel );
457        }
458        else if ( (byte == 0x0A) || (byte == 0x0D) ) // LF or CR character
459        {
460            done = 1;
461        }
462        else if ( byte == 0x7F )        // DEL character
463        {
464            if (max > 0)
465            {
466                max--;          // cancel the character
467                _tty_write( cancel_string, 3, channel );
468            }
469        }
470    } // end while
471
472    // string conversion
473    for( i=0 ; i<max ; i++ )
474    {
475        val = val*10 + (buf[i] - 0x30);
476        if (val < save) overflow = 1;
477        save = val;
478    }
479    if (overflow == 0)
480    {
481        *word_buffer = val;     // return decimal value
482    }
483    else
484    {
485        for( i=0 ; i<max ; i++)     // cancel the string
486        {
487            _tty_write( cancel_string, 3, channel );
488        }
489        _tty_write( &zero, 1, channel );
490        *word_buffer = 0;       // return 0 value
491    }
492}
493
494//////////////////////////////////////////////////////////////////////////////
495//  This function is a simplified version of the mutek_printf() function.
496//  It takes the TTY lock on the selected channel for exclusive access.
497//  Only a limited number of formats are supported:
498//  - %d : signed decimal
499//  - %u : unsigned decimal
500//  - %x : hexadecimal
501//  - %c : char
502//  - %s : string
503//////////////////////////////////////////////////////////////////////////////
504in_drivers void _tty_printf( char *format, ...)
505{
506    va_list ap;
507    va_start( ap, format );
508
509    unsigned int channel;
510    unsigned int l;
511    unsigned int x;
512    unsigned int y;
513    unsigned int proc_id = _procid();
514
515    // compute TTY channel
516    if ( NB_TTY_CHANNELS == 1 )
517    {
518        channel = 0;
519    }
520    else
521    {
522        l           = (proc_id % NB_PROCS_MAX);
523        x           = (proc_id / NB_PROCS_MAX) >> Y_WIDTH; 
524        y           = (proc_id / NB_PROCS_MAX) & ((1<<Y_WIDTH) - 1);
525        channel = (x * Y_SIZE + y) * NB_PROCS_MAX + l;
526        if (channel >= NB_TTY_CHANNELS )
527        {
528            _tty_get_lock( 0 );
529            _tty_puts("ERROR in _tty_printf() for proc[" );
530            _tty_putd( x );
531            _tty_puts(",");
532            _tty_putd( y );
533            _tty_puts(",");
534            _tty_putd( l );
535            _tty_puts("] / TTY channel too large = ");
536            _tty_putd( channel );
537            _tty_puts("\n");
538            _tty_release_lock( 0 );
539            _exit();
540        }
541    }
542
543    // take the TTY lock
544    _tty_get_lock( channel );
545
546printf_text:
547
548    while (*format) 
549    {
550        unsigned int i;
551        for (i = 0; format[i] && format[i] != '%'; i++)
552            ;
553        if (i) 
554        {
555            _tty_write( format, i, channel );
556            format += i;
557        }
558        if (*format == '%') 
559        {
560            format++;
561            goto printf_arguments;
562        }
563    } // end while
564
565    va_end( ap );
566
567    // release lock
568    _tty_release_lock( 0 );
569
570    return;
571
572printf_arguments:
573
574    {
575        int                 val = va_arg(ap, long);
576        char                buf[20];
577        char*               pbuf;
578        unsigned int        len = 0;
579        static const char   HexaTab[] = "0123456789ABCDEF";
580        unsigned int        i;
581
582        switch (*format++) {
583            case ('c'):             // char conversion
584                len = 1;
585                buf[0] = val;
586                pbuf = buf;
587                break;
588            case ('d'):             // decimal signed integer
589                if (val < 0) 
590                {
591                    val = -val;
592                    _tty_write( "_" , 1, channel );
593                }
594            case ('u'):             // decimal unsigned integer
595                for( i=0 ; i<10 ; i++) 
596                {
597                    buf[9-i] = HexaTab[val % 10];
598                    if (!(val /= 10)) break;
599                }
600                len =  i+1;
601                pbuf = &buf[9-i];
602                break;
603            case ('x'):             // hexadecimal integer
604                _tty_write( "0x", 2, channel );
605                for( i=0 ; i<8 ; i++) 
606                {
607                    buf[7-i] = HexaTab[val % 16U];
608                    if (!(val /= 16U)) break;
609                }
610                len =  i+1;
611                pbuf = &buf[7-i];
612                break;
613            case ('s'):             // string
614                {
615                    char *str = (char*)val;
616                    while ( str[len] ) len++;
617                    pbuf = (char*)val;
618                }
619                break;
620            default:
621                goto printf_text;
622        } // end switch
623
624        _tty_write( pbuf, len, channel );
625        goto printf_text;
626    }
627} // end printf()
628
629//////////////////////////////////////////////////////////////////////////////////////
630//  These functions are the ISRs that must be executed when an IRQ is activated
631//  by the TTY: _tty_isr_X is associated to channel [X].
632//  It save the character in the communication buffer _tty_get_buf[X],
633//  and set the set/reset variable _tty_get_full[X].
634//  A character is lost if the buffer is full when the ISR is executed.
635//////////////////////////////////////////////////////////////////////////////////////
636in_drivers void _tty_isr_indexed(size_t index)
637{
638    char*   base = (char*)&seg_tty_base;
639    char*   tty_address = (char*)(base + index*TTY_SPAN*4);
640
641    _tty_get_buf[index]  = tty_address[TTY_READ*4];     // save character and reset IRQ
642    _tty_get_full[index] = 1;                       // signals character available
643}
644
645in_drivers void _tty_isr_00() { _tty_isr_indexed(0); }
646in_drivers void _tty_isr_01() { _tty_isr_indexed(1); }
647in_drivers void _tty_isr_02() { _tty_isr_indexed(2); }
648in_drivers void _tty_isr_03() { _tty_isr_indexed(3); }
649in_drivers void _tty_isr_04() { _tty_isr_indexed(4); }
650in_drivers void _tty_isr_05() { _tty_isr_indexed(5); }
651in_drivers void _tty_isr_06() { _tty_isr_indexed(6); }
652in_drivers void _tty_isr_07() { _tty_isr_indexed(7); }
653in_drivers void _tty_isr_08() { _tty_isr_indexed(8); }
654in_drivers void _tty_isr_09() { _tty_isr_indexed(9); }
655in_drivers void _tty_isr_10() { _tty_isr_indexed(10); }
656in_drivers void _tty_isr_11() { _tty_isr_indexed(11); }
657in_drivers void _tty_isr_12() { _tty_isr_indexed(12); }
658in_drivers void _tty_isr_13() { _tty_isr_indexed(13); }
659in_drivers void _tty_isr_14() { _tty_isr_indexed(14); }
660in_drivers void _tty_isr_15() { _tty_isr_indexed(15); }
661in_drivers void _tty_isr_16() { _tty_isr_indexed(16); }
662in_drivers void _tty_isr_17() { _tty_isr_indexed(17); }
663in_drivers void _tty_isr_18() { _tty_isr_indexed(18); }
664in_drivers void _tty_isr_19() { _tty_isr_indexed(19); }
665in_drivers void _tty_isr_20() { _tty_isr_indexed(20); }
666in_drivers void _tty_isr_21() { _tty_isr_indexed(21); }
667in_drivers void _tty_isr_22() { _tty_isr_indexed(22); }
668in_drivers void _tty_isr_23() { _tty_isr_indexed(23); }
669in_drivers void _tty_isr_24() { _tty_isr_indexed(24); }
670in_drivers void _tty_isr_25() { _tty_isr_indexed(25); }
671in_drivers void _tty_isr_26() { _tty_isr_indexed(26); }
672in_drivers void _tty_isr_27() { _tty_isr_indexed(27); }
673in_drivers void _tty_isr_28() { _tty_isr_indexed(28); }
674in_drivers void _tty_isr_29() { _tty_isr_indexed(29); }
675in_drivers void _tty_isr_30() { _tty_isr_indexed(30); }
676in_drivers void _tty_isr_31() { _tty_isr_indexed(31); }
677
678
679//////////////////////////////////////////////////////////////////////////////////////////
680//  I/O BLOCK_DEVICE
681// The three functions below use the three variables _ioc_lock _ioc_done,
682// and _ioc_status for synchronisation.
683// - As the IOC component can be used by several programs running in parallel,
684// the _ioc_lock variable guaranties exclusive access to the device.
685// The _ioc_read() and _ioc_write() functions use atomic LL/SC to get the lock.
686// and set _ioc_lock to a non zero value.
687// The _ioc_write() and _ioc_read() functions are blocking, polling the _ioc_lock
688// variable until the device is available.
689// - When the tranfer is completed, the ISR routine activated by the IOC IRQ
690// set the _ioc_done variable to a non-zero value. Possible address errors detected
691// by the IOC peripheral are reported by the ISR in the _ioc_status variable.
692// The _ioc_completed() function is polling the _ioc_done variable, waiting for
693// tranfer conpletion. When the completion is signaled, the _ioc_completed() function
694// reset the _ioc_done variable to zero, and releases the _ioc_lock variable.
695///////////////////////////////////////////////////////////////////////////////////////
696// This blocking function is used by the _ioc_read() and _ioc_write() functions
697// to get _ioc_lock using LL/SC.
698///////////////////////////////////////////////////////////////////////////////////////
699in_drivers void _ioc_get_lock()
700{
701    register unsigned int*      plock = (unsigned int*)&_ioc_lock;                     
702
703    asm volatile ("_ioc_llsc:                       \n"
704                  "ll   $2,    0(%0)                \n" // $2 <= _ioc_lock
705                  "bnez $2,    _ioc_llsc            \n" // retry  if busy
706                  "li   $3,    1                    \n" // prepare argument for sc 
707                  "sc   $3,    0(%0)                \n" // try to set _ioc_busy
708                  "beqz $3,    _ioc_llsc            \n" // retry if not atomic
709                  ::"r"(plock):"$2","$3");
710}
711//////////////////////////////////////////////////////////////////////////////////////
712// Transfer data from a memory buffer to the block_device.
713// - lba    : first block index on the disk
714// - buffer : base address of the memory buffer
715// - count  : number of blocks to be transfered
716// The source buffer must be in user address space.
717///////////////////////////////////////////////////////////////////////////////////////
718in_drivers void _ioc_write( size_t   lba, 
719                            void*    buffer, 
720                            size_t   count,
721                            size_t   ext )
722{
723    volatile unsigned int*      ioc_address = (unsigned int*)&seg_ioc_base;
724
725    // get the lock
726    _ioc_get_lock();
727
728    // block_device configuration
729    ioc_address[BLOCK_DEVICE_BUFFER]     = (unsigned int)buffer;
730    ioc_address[BLOCK_DEVICE_BUFFER_EXT] = ext;
731    ioc_address[BLOCK_DEVICE_COUNT]      = count;
732    ioc_address[BLOCK_DEVICE_LBA]        = lba;
733    ioc_address[BLOCK_DEVICE_IRQ_ENABLE] = 1;
734    ioc_address[BLOCK_DEVICE_OP]         = BLOCK_DEVICE_WRITE;
735}
736///////////////////////////////////////////////////////////////////////////////////////
737// Transfer data from a file on the block device to a memory buffer.
738// - lba    : first block index on the disk
739// - buffer : base address of the memory buffer
740// - count  : number of blocks to be transfered
741// The destination buffer must be in user address space.
742// All cache lines corresponding to the the target buffer must be invalidated
743// for cache coherence.
744///////////////////////////////////////////////////////////////////////////////////////
745in_drivers void _ioc_read( size_t   lba, 
746                           void*    buffer, 
747                           size_t   count,
748                           size_t   ext )
749{
750    volatile unsigned int*      ioc_address = (unsigned int*)&seg_ioc_base;
751
752    // get the lock
753    _ioc_get_lock();
754
755    // block_device configuration
756    ioc_address[BLOCK_DEVICE_BUFFER]     = (unsigned int)buffer;
757    ioc_address[BLOCK_DEVICE_BUFFER_EXT] = ext;
758    ioc_address[BLOCK_DEVICE_COUNT]      = count;
759    ioc_address[BLOCK_DEVICE_LBA]        = lba;
760    ioc_address[BLOCK_DEVICE_IRQ_ENABLE] = 1;
761    ioc_address[BLOCK_DEVICE_OP]         = BLOCK_DEVICE_READ;
762}
763///////////////////////////////////////////////////////////////////////////////////////
764// This blocking function cheks completion of an I/O transfer and reports errors.
765// It returns 0 if the transfer is successfully completed.
766// It returns -1 if an error has been reported.
767///////////////////////////////////////////////////////////////////////////////////////
768in_drivers void _ioc_completed()
769{
770    // waiting for completion
771    while (_ioc_done == 0)  asm volatile("nop"); 
772   
773    // reset synchronisation variables
774    _ioc_done = 0;
775    _ioc_lock = 0;
776
777    if( (_ioc_status != BLOCK_DEVICE_READ_SUCCESS) &&
778        (_ioc_status != BLOCK_DEVICE_WRITE_SUCCESS) )
779    {
780        _tty_get_lock( 0 );
781        _tty_puts( "ERROR in _ioc_completed()\n");
782        _tty_release_lock( 0 );
783        _exit();
784    }
785}
786//////////////////////////////////////////////////////////////////////////////////////
787//  This ISR must be executed when an IRQ is activated by IOC to signal completion.
788//  It acknowledge the IRQ using the ioc base address, save the status in _ioc_status,
789//  and set the _ioc_done variable to signal completion.
790//  This variable is defined in the drivers.c file.
791//////////////////////////////////////////////////////////////////////////////////////
792in_drivers void _ioc_isr()
793{
794    int* ioc_address = (int*)&seg_ioc_base;
795   
796    _ioc_status = ioc_address[BLOCK_DEVICE_STATUS];     // save status & reset IRQ
797    _ioc_done   = 1;                                                // signals completion
798}
799
800//////////////////////////////////////////////////////////////////////////////////////
801//  This ISR must be executed when an IRQ is activated by MEMC to signal
802//  an error detected by the TSAR memory cache after a write transaction.
803//  It displays an error message on the TTY terminal allocated to the processor
804//  executing the ISR.
805//////////////////////////////////////////////////////////////////////////////////////
806in_drivers void _mmc_isr()
807{
808    int*         mmc_address = (int*)&seg_mmc_base;
809    unsigned int cluster_xy  = _procid() / NB_PROCS_MAX;
810   
811    _tty_printf( "WRITE ERROR signaled by Memory Cache in cluster %x\n", cluster_xy );
812}
813
814//////////////////////////////////////////////////////////////////////////////////////
815//  FRAME_BUFFER
816// The _fb_sync_write & _fb_sync_read functions use a memcpy strategy to implement
817// the transfer between a data buffer and the frame buffer.
818// They are blocking until completion of the transfer.
819//////////////////////////////////////////////////////////////////////////////////////
820//  _fb_sync_write()
821// Transfer data from an user buffer to the frame_buffer device with a memcpy.
822// - offset     : offset (in bytes) in the frame buffer
823// - buffer : base address of the memory buffer
824// - length : number of bytes to be transfered
825//////////////////////////////////////////////////////////////////////////////////////
826in_drivers void _fb_sync_write( size_t  offset, 
827                                void*   buffer, 
828                                size_t  length,
829                                size_t  ext )
830{
831    volatile char*  fb = (char*)(void*)&seg_fbf_base + offset;
832    char*       ub = buffer;
833
834    _memcpy( (void*)fb, (void*)ub, length );
835}
836///////////////////////////////////////////////////////////////////////////////////////
837//  _fb_sync_read()
838// Transfer data from the frame_buffer device to an user buffer with a memcpy.
839// - offset     : offset (in bytes) in the frame buffer
840// - buffer : base address of the memory buffer
841// - length : number of bytes to be transfered
842//////////////////////////////////////////////////////////////////////////////////////
843in_drivers void  _fb_sync_read( size_t  offset, 
844                                void*   buffer, 
845                                size_t  length,
846                                size_t  ext )
847{
848    volatile char*  fb = (char*)(void*)&seg_fbf_base + offset;
849    char*       ub = buffer;
850
851    _memcpy( (void*)ub, (void*)fb, length );
852}
853
854///////////////////////////////////////////////////////////////////////////////////////
855// Release a software spin-lock
856///////////////////////////////////////////////////////////////////////////////////////
857in_drivers void _release_lock(size_t index)
858
859{
860    if( index >= NB_LOCKS ) 
861    {
862        _tty_get_lock( 0 );
863        _tty_puts( "ERROR in _release_lock()" );
864        _tty_release_lock( 0 );
865        _exit();
866    }
867   
868    _spin_lock[index] = 0;
869}
870///////////////////////////////////////////////////////////////////////////////////////
871// Try to take a software spin-lock.
872// This is a blocking call, as there is a busy-waiting loop,
873// until the lock is granted to the requester.
874// There is an internal delay of about 100 cycles between
875// two successive lock read, to avoid bus saturation.
876///////////////////////////////////////////////////////////////////////////////////////
877in_drivers void _get_lock(size_t index)
878{
879    if( index >= NB_LOCKS )
880    {
881        _tty_get_lock( 0 );
882        _tty_puts( "ERROR in _get_lock()" );
883        _tty_release_lock( 0 );
884        _exit();
885    }
886
887    register int   delay = ((_proctime() +_procid()) & 0xF) << 4;
888    register int * plock = (int *) &_spin_lock[index];                 
889
890    asm volatile ("_locks_llsc:                 \n"
891                  "ll   $2,    0(%0)            \n"     // $2 <= _locks_lock
892                  "bnez $2,    _locks_delay     \n"     // random delay if busy
893                  "li   $3,    1            \n"     // prepare argument for sc 
894                  "sc   $3,    0(%0)            \n"     // try to set _locks_busy
895                  "bnez $3,    _locks_ok    \n"     // exit if atomic
896                  "_locks_delay:            \n"
897                  "move $4,    %1           \n"     // $4 <= delay
898                  "_locks_loop:             \n"
899                  "addi $4,    $4,    -1    \n"     // $4 <= $4 - 1
900                  "beqz $4,    _locks_loop  \n"     // test end delay
901                  "j           _locks_llsc  \n"     // retry
902                  "_locks_ok:                   \n"
903                  ::"r"(plock),"r"(delay):"$2","$3","$4");
904}
905
906
907//////////////////////////////////////////////////////////////////////////////////////
908// This function makes a cooperative initialisation of the barrier:
909// - barrier_count[index] <= N
910// - barrier_lock[index]  <= 0
911// All tasks try to initialize the barrier, but the initialisation
912// is done by only one task, using LL/SC instructions.
913// This cooperative initialisation is questionnable,
914// because the barrier can ony be initialised once...
915//////////////////////////////////////////////////////////////////////////////////////
916in_drivers void _barrier_init(unsigned int index, unsigned int value)
917{
918
919    register int* pinit         = (int*)&_barrier_value[index];
920    register int* pcount        = (int*)&_barrier_count[index];
921    register int* plock         = (int*)&_barrier_lock[index];
922
923    if ( index >= NB_BARRIERS )
924    {
925        _tty_get_lock( 0 );
926        _tty_puts( "ERROR in _barrier_init()" );
927        _tty_release_lock( 0 );
928        _exit();
929    }
930
931    // parallel initialisation using atomic instructions LL/SC
932    asm volatile ("_barrier_init_test:                  \n"
933                  "ll   $2,     0(%0)                   \n"     // read barrier_value
934                  "bnez $2,     _barrier_init_done      \n"
935                  "move $3,     %3                              \n"
936                  "sc   $3,     0(%0)                   \n"     // try to write barrier_value
937                  "beqz $3,     _barrier_init_test      \n"
938                  "move $3,     %3                                  \n" 
939                  "sw   $3,     0(%1)                           \n"     // barrier_count <= barrier_value
940                  "move $3, $0                      \n" //
941                  "sw   $3,     0(%2)                           \n"     // barrier_lock <= 0
942                  "_barrier_init_done:                  \n"
943                  ::"r"(pinit),"r"(pcount),"r"(plock),"r"(value):"$2","$3");
944}
945//////////////////////////////////////////////////////////////////////////////////////
946// This blocking function uses a busy_wait technics (on the barrier_lock value),
947// because the GIET does not support dynamic scheduling/descheduling of tasks.
948// The barrier state is actually defined by two variables:
949// _barrier_count[index] define the number of particpants that are waiting
950// _barrier_lock[index] define the bool variable whose value is polled
951// The last participant change the value of _barrier_lock[index] to release the barrier...
952// There is at most 16 independant barriers, and an error is returned
953// if the barrier index is larger than 15.
954//////////////////////////////////////////////////////////////////////////////////////
955in_drivers void _barrier_wait(unsigned int index)
956{
957    register int*       pcount          = (int*)&_barrier_count[index];         
958    register int        count;
959
960    int                lock             = _barrier_lock[index];         
961
962    if ( index >= NB_BARRIERS )
963    {
964        _tty_get_lock( 0 );
965        _tty_puts( "ERROR in _barrier_wait()" );
966        _tty_release_lock( 0 );
967        _exit();
968    }
969   
970    // parallel decrement _barrier_count[index] using atomic instructions LL/SC
971    // input : pointer on _barrier_count[index]
972    // output : count = _barrier_count[index] (before decrementation)
973    asm volatile ("_barrier_decrement:                          \n"
974                  "ll   %0,     0(%1)                           \n"
975                  "addi $3,     %0,     -1                      \n"
976                  "sc   $3,     0(%1)                           \n"
977                  "beqz $3,     _barrier_decrement              \n"
978                  :"=&r"(count)
979                  :"r"(pcount)
980                  :"$2","$3");
981
982    // the last task re-initializes the barrier_ count variable
983    // and the barrier_lock variable, waking up all other waiting tasks
984
985    if ( count == 1 )    // last task
986    {
987        _barrier_count[index] = _barrier_value[index];
988        asm volatile( "sync" );
989        _barrier_lock[index]   = (lock == 0) ? 1 : 0;
990    }
991    else                // other tasks
992    {
993        while ( lock == _barrier_lock[index] )  asm volatile("nop");
994    }
995} 
996
997
998// Local Variables:
999// tab-width: 4;
1000// c-basic-offset: 4;
1001// c-file-offsets:((innamespace . 0)(inline-open . 0));
1002// indent-tabs-mode: nil;
1003// End:
1004//
1005// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
1006
Note: See TracBrowser for help on using the repository browser.