source: trunk/kernel/devices/dev_txt.c @ 411

Last change on this file since 411 was 408, checked in by alain, 5 years ago

Fix several bugs in the fork() syscall.

File size: 6.4 KB
Line 
1/*
2 * dev_txt.c - TXT (Text Terminal) generic device API implementation.
3 *
4 * Author  Alain Greiner    (2016)
5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MK
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-kernel; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <hal_types.h>
25#include <hal_special.h>
26#include <hal_remote.h>
27#include <hal_drivers.h>
28#include <thread.h>
29#include <chdev.h>
30#include <rpc.h>
31#include <printk.h>
32#include <dev_txt.h>
33
34/////////////////////////////////////////////////////////////////////////////////////////
35// Extern global variables
36/////////////////////////////////////////////////////////////////////////////////////////
37
38extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c
39
40#if CONFIG_READ_DEBUG
41extern uint32_t enter_txt_read;
42extern uint32_t exit_txt_read;
43#endif
44
45//////////////////////////////////
46void dev_txt_init( chdev_t * txt )
47{
48    // For all TXT channels other than the TXT0 (kernel terminal),
49    // the PIC chdev must be initialized before the TXT chdev, because
50    // the TXT chdev initialization requires the routing of an external IRQ
51
52    xptr_t    pic_xp  = chdev_dir.pic;
53    uint32_t  channel = txt->channel;
54    uint32_t  impl    = txt->impl;
55    bool_t    is_rx   = txt->is_rx;
56
57    assert( (pic_xp != XPTR_NULL) || (channel == 0) , __FUNCTION__ , 
58            "PIC not initialised before TXT" );
59
60    // set chdev name
61    if( is_rx ) snprintf( txt->name , 16 , "txt%d_rx" , channel );
62    else        snprintf( txt->name , 16 , "txt%d_tx" , channel );
63
64    // call driver init function
65    hal_drivers_txt_init(txt, impl);
66
67    // no server thread and no IRQ routing for TXT0 and IMPL_TXT_RS2 (multiplexed
68    // in software, not hardware)
69    if( channel != 0 && impl != IMPL_TXT_RS2 )
70    {
71        // select a core to execute the server thread
72        lid_t lid = cluster_select_local_core();
73
74        // bind IRQ to the selected core
75        dev_pic_bind_irq( lid , txt );
76
77        // enable IRQ
78        dev_pic_enable_irq( lid , XPTR( local_cxy , txt ) );
79
80        // create server thread
81        thread_t * new_thread;
82        error_t    error;
83
84        error = thread_kernel_create( &new_thread,
85                                      THREAD_DEV,
86                                      &chdev_sequencial_server,
87                                      txt,
88                                      lid );
89
90        assert( (error == 0) , __FUNCTION__ , "cannot create server thread" );
91
92        // set "server" field in chdev descriptor
93        txt->server = new_thread;
94
95        // set "chdev" field in thread descriptor
96        new_thread->chdev = txt;
97
98        // unblock server thread
99        thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL );
100    }
101}
102
103//////////////////////////////////////////////////////////////////////////////////
104// This static function is called by dev_txt_read(), dev_txt_write() functions.
105////////////////////////////////////i/////////////////////////////////////////////
106static error_t dev_txt_access( uint32_t   type,
107                               uint32_t   channel,
108                               char     * buffer,
109                               uint32_t   count )
110{
111    xptr_t     dev_xp;
112    thread_t * this = CURRENT_THREAD;
113
114txt_dmsg("\n[DBG] %s : core[%x,%d] (thread %s) enters / cycle %d\n",
115__FUNCTION__, local_cxy, this->core->lid, thread_type_str(this->type), hal_time_stamp() );
116
117    // check channel argument
118    assert( (channel < CONFIG_MAX_TXT_CHANNELS) , __FUNCTION__ , "illegal channel index" );
119
120    // get extended pointer on remote TXT chdev descriptor
121    if( type == TXT_WRITE )  dev_xp = chdev_dir.txt_tx[channel];
122    else                     dev_xp = chdev_dir.txt_rx[channel];
123
124    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined TXT chdev descriptor" );
125
126    // register command in calling thread descriptor
127    this->txt_cmd.dev_xp  = dev_xp;
128    this->txt_cmd.type    = type;
129    this->txt_cmd.buf_xp  = XPTR( local_cxy , buffer );
130    this->txt_cmd.count   = count;
131
132    // register client thread in waiting queue, activate server thread
133    // block client thread on THREAD_BLOCKED_IO and deschedule.
134    // it is re-activated by the ISR signaling IO operation completion.
135    chdev_register_command( dev_xp );
136
137txt_dmsg("\n[DBG] %s : core[%x,%d] (thread %s) exit / cycle %d\n",
138__FUNCTION__, local_cxy, this->core->lid, thread_type_str(this->type), hal_time_stamp() );
139
140    // return I/O operation status from calling thread descriptor
141    return this->txt_cmd.error;
142}
143
144/////////////////////////////////////////
145error_t dev_txt_write( uint32_t   channel,
146                       char     * buffer,
147                       uint32_t   count )
148{
149    error_t error = dev_txt_access( TXT_WRITE , channel , buffer , count );
150    return error;
151}
152
153/////////////////////////////////////////
154error_t dev_txt_read( uint32_t   channel,
155                      char     * buffer )
156{
157
158#if CONFIG_READ_DEBUG
159enter_txt_read = hal_time_stamp();
160#endif
161
162    error_t error = dev_txt_access( TXT_READ , channel , buffer , 1 );
163
164#if CONFIG_READ_DEBUG
165exit_txt_read = hal_time_stamp();
166#endif
167
168    return error;
169
170}
171
172//////////////////////////////////////////////
173error_t dev_txt_sync_write( char     * buffer,
174                            uint32_t   count )
175{
176    // get extended pointer on TXT[0] chdev
177    xptr_t  dev_xp = chdev_dir.txt_tx[0];
178
179    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ ,
180    "undefined TXT0 chdev descriptor" );
181
182    // get TXTO chdev cluster and local pointer
183    cxy_t    dev_cxy  = GET_CXY( dev_xp );
184    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
185
186    // get driver command function
187    dev_aux_t * aux = (dev_aux_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->aux ) );
188
189    // build arguments structure
190    txt_aux_t  args;
191    args.dev_xp = dev_xp;
192    args.buffer = buffer;
193    args.count  = count;
194
195    // call driver function
196    aux( &args );
197
198    return 0;
199}
200
Note: See TracBrowser for help on using the repository browser.