1 | /* |
---|
2 | * dev_txt.c - TXT (Text Terminal) generic device API implementation. |
---|
3 | * |
---|
4 | * Author Alain Greiner (2016,2017,2018,2019) |
---|
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_kernel_types.h> |
---|
25 | #include <hal_special.h> |
---|
26 | #include <hal_remote.h> |
---|
27 | #include <hal_drivers.h> |
---|
28 | #include <thread.h> |
---|
29 | #include <remote_busylock.h> |
---|
30 | #include <chdev.h> |
---|
31 | #include <rpc.h> |
---|
32 | #include <printk.h> |
---|
33 | #include <dev_txt.h> |
---|
34 | |
---|
35 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
36 | // Extern global variables |
---|
37 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
38 | |
---|
39 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
---|
40 | |
---|
41 | |
---|
42 | #if (DEBUG_SYS_READ & 1) |
---|
43 | extern uint32_t enter_txt_read; |
---|
44 | extern uint32_t exit_txt_read; |
---|
45 | #endif |
---|
46 | |
---|
47 | #if (DEBUG_SYS_WRITE & 1) |
---|
48 | extern uint32_t enter_txt_write; |
---|
49 | extern uint32_t exit_txt_write; |
---|
50 | #endif |
---|
51 | |
---|
52 | /////////////////////////////////////////////////// |
---|
53 | const char * dev_txt_type_str( dev_txt_cmd_t type ) |
---|
54 | { |
---|
55 | switch (type) |
---|
56 | { |
---|
57 | case (TXT_SYNC_WRITE): return "TXT_SYNC_WRITE"; |
---|
58 | case (TXT_READ): return "TXT_READ"; |
---|
59 | case (TXT_WRITE): return "TXT_WRITE"; |
---|
60 | default: return "undefined"; |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | ////////////////////////////////// |
---|
65 | void dev_txt_init( chdev_t * txt ) |
---|
66 | { |
---|
67 | // For all TXT channels other than the TXT0 (kernel terminal), |
---|
68 | // the PIC chdev must be initialized before the TXT chdev, because |
---|
69 | // the TXT chdev initialization requires the routing of an external IRQ |
---|
70 | |
---|
71 | xptr_t pic_xp = chdev_dir.pic; |
---|
72 | uint32_t channel = txt->channel; |
---|
73 | uint32_t impl = txt->impl; |
---|
74 | bool_t is_rx = txt->is_rx; |
---|
75 | |
---|
76 | assert( (pic_xp != XPTR_NULL) || (channel == 0) , |
---|
77 | "PIC not initialised before TXT" ); |
---|
78 | |
---|
79 | // set chdev name |
---|
80 | if( is_rx ) snprintf( txt->name , 16 , "txt%d_rx" , channel ); |
---|
81 | else snprintf( txt->name , 16 , "txt%d_tx" , channel ); |
---|
82 | |
---|
83 | // set TXT chdev extension |
---|
84 | txt->ext.txt.owner_xp = XPTR_NULL; |
---|
85 | xlist_root_init( XPTR( local_cxy, &txt->ext.txt.root ) ); |
---|
86 | remote_busylock_init( XPTR( local_cxy , &txt->ext.txt.lock ), LOCK_CHDEV_TXTLIST ); |
---|
87 | |
---|
88 | // call driver init function |
---|
89 | hal_drivers_txt_init(txt, impl); |
---|
90 | |
---|
91 | // no server thread and no IRQ routing for TXT0 |
---|
92 | // except for LETI where there MUST be IRQ routing |
---|
93 | // no server thread for IMPL_TXT_RS2 (multiplexed in software, not hardware) |
---|
94 | if( channel != 0 && impl != IMPL_TXT_RS2 ) |
---|
95 | { |
---|
96 | // select a core to execute the server thread |
---|
97 | lid_t lid = cluster_select_local_core(); |
---|
98 | |
---|
99 | // The unique IRQ from cluster 00's MTTY must be bound to a RX chdev |
---|
100 | // so that all IRQs are considered as RX IRQs and treated as such |
---|
101 | // Indeed, IRQs are raised by the MTTY only on user keystroke, so IRQs must |
---|
102 | // always be considered as RX IRQs. |
---|
103 | if ( ( impl != IMPL_TXT_MTY ) || ( channel == 1 && is_rx == 1 ) ) |
---|
104 | { |
---|
105 | // bind IRQ to the selected core |
---|
106 | dev_pic_bind_irq( lid , txt ); |
---|
107 | |
---|
108 | // enable IRQ |
---|
109 | dev_pic_enable_irq( lid , XPTR( local_cxy , txt ) ); |
---|
110 | } |
---|
111 | |
---|
112 | // create server thread |
---|
113 | thread_t * new_thread; |
---|
114 | error_t error; |
---|
115 | |
---|
116 | error = thread_kernel_create( &new_thread, |
---|
117 | THREAD_DEV, |
---|
118 | &chdev_server_func, |
---|
119 | txt, |
---|
120 | lid ); |
---|
121 | |
---|
122 | assert( (error == 0) , "cannot create server thread" ); |
---|
123 | |
---|
124 | // set "server" field in chdev descriptor |
---|
125 | txt->server = new_thread; |
---|
126 | |
---|
127 | // set "chdev" field in thread descriptor |
---|
128 | new_thread->chdev = txt; |
---|
129 | |
---|
130 | // unblock server thread |
---|
131 | thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL ); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | ////////////////////////////////////////////////////////////////////////////////// |
---|
136 | // This static function is called by dev_txt_read(), dev_txt_write() functions. |
---|
137 | ////////////////////////////////////i///////////////////////////////////////////// |
---|
138 | static error_t dev_txt_access( uint32_t type, |
---|
139 | uint32_t channel, |
---|
140 | char * buffer, |
---|
141 | uint32_t count ) |
---|
142 | { |
---|
143 | xptr_t dev_xp; |
---|
144 | thread_t * this = CURRENT_THREAD; |
---|
145 | |
---|
146 | // check channel argument |
---|
147 | assert( (channel < CONFIG_MAX_TXT_CHANNELS) , "illegal channel index" ); |
---|
148 | |
---|
149 | // get extended pointer on remote TXT chdev descriptor |
---|
150 | if( type == TXT_WRITE ) dev_xp = chdev_dir.txt_tx[channel]; |
---|
151 | else dev_xp = chdev_dir.txt_rx[channel]; |
---|
152 | |
---|
153 | assert( (dev_xp != XPTR_NULL) , "undefined TXT chdev descriptor" ); |
---|
154 | |
---|
155 | // register command in calling thread descriptor |
---|
156 | this->txt_cmd.dev_xp = dev_xp; |
---|
157 | this->txt_cmd.type = type; |
---|
158 | this->txt_cmd.buf_xp = XPTR( local_cxy , buffer ); |
---|
159 | this->txt_cmd.count = count; |
---|
160 | |
---|
161 | // register client thread in waiting queue, activate server thread |
---|
162 | // block client thread on THREAD_BLOCKED_IO and deschedule. |
---|
163 | // it is re-activated by the ISR signaling IO operation completion. |
---|
164 | chdev_register_command( dev_xp ); |
---|
165 | |
---|
166 | // return I/O operation status from calling thread descriptor |
---|
167 | return this->txt_cmd.error; |
---|
168 | } |
---|
169 | |
---|
170 | ///////////////////////////////////////// |
---|
171 | error_t dev_txt_write( uint32_t channel, |
---|
172 | char * buffer, |
---|
173 | uint32_t count ) |
---|
174 | { |
---|
175 | |
---|
176 | #if (DEBUG_SYS_WRITE & 1) |
---|
177 | enter_txt_write = hal_time_stamp(); |
---|
178 | #endif |
---|
179 | |
---|
180 | #if DEBUG_DEV_TXT_TX |
---|
181 | thread_t * this = CURRENT_THREAD; |
---|
182 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
183 | if( DEBUG_DEV_TXT_TX < cycle ) |
---|
184 | printk("\n[%s] thread[%x,%x] enters / cycle %d\n", |
---|
185 | __FUNCTION__, this->process->pid, this->trdid, cycle ); |
---|
186 | #endif |
---|
187 | |
---|
188 | // get extended pointer on TXT[0] chdev |
---|
189 | xptr_t dev_xp = chdev_dir.txt_tx[0]; |
---|
190 | |
---|
191 | assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , |
---|
192 | "undefined TXT0 chdev descriptor" ); |
---|
193 | |
---|
194 | // get TXTO chdev cluster and local pointer |
---|
195 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
---|
196 | chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); |
---|
197 | |
---|
198 | // If we use MTTYs (vci_multi_tty), we perform only sync writes |
---|
199 | // Otherwise, we use vci_tty_tsar so we can use async writes |
---|
200 | |
---|
201 | if( dev_ptr->impl == IMPL_TXT_MTY ) |
---|
202 | { |
---|
203 | // get driver command function |
---|
204 | dev_aux_t * aux = (dev_aux_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->aux ) ); |
---|
205 | |
---|
206 | // build arguments structure |
---|
207 | txt_sync_args_t args; |
---|
208 | args.dev_xp = dev_xp; |
---|
209 | args.buffer = buffer; |
---|
210 | args.count = count; |
---|
211 | args.channel = channel; |
---|
212 | |
---|
213 | // call driver function |
---|
214 | aux( &args ); |
---|
215 | |
---|
216 | return 0; |
---|
217 | } |
---|
218 | |
---|
219 | else |
---|
220 | { |
---|
221 | return dev_txt_access( TXT_WRITE , channel , buffer , count ); |
---|
222 | } |
---|
223 | |
---|
224 | #if DEBUG_DEV_TXT_TX |
---|
225 | cycle = (uint32_t)hal_get_cycles(); |
---|
226 | if( DEBUG_DEV_TXT_TX < cycle ) |
---|
227 | printk("\n[%s] thread[%x,%x] exit / cycle %d\n", |
---|
228 | __FUNCTION__, this->process->pid, this->trdid, cycle ); |
---|
229 | #endif |
---|
230 | |
---|
231 | #if (DEBUG_SYS_WRITE & 1) |
---|
232 | exit_txt_write = hal_time_stamp(); |
---|
233 | #endif |
---|
234 | |
---|
235 | } |
---|
236 | |
---|
237 | ///////////////////////////////////////// |
---|
238 | error_t dev_txt_read( uint32_t channel, |
---|
239 | char * buffer ) |
---|
240 | { |
---|
241 | |
---|
242 | #if (DEBUG_SYS_READ & 1) |
---|
243 | enter_txt_read = hal_time_stamp(); |
---|
244 | #endif |
---|
245 | |
---|
246 | #if DEBUG_DEV_TXT_RX |
---|
247 | thread_t * this = CURRENT_THREAD; |
---|
248 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
249 | if( DEBUG_DEV_TXT_RX < cycle ) |
---|
250 | printk("\n[%s] thread[%x,%x] enters / cycle %d\n", |
---|
251 | __FUNCTION__, this->process->pid, this->trdid, cycle ); |
---|
252 | #endif |
---|
253 | |
---|
254 | return dev_txt_access( TXT_READ , channel , buffer , 1 ); |
---|
255 | |
---|
256 | #if DEBUG_DEV_TXT_RX |
---|
257 | cycle = (uint32_t)hal_get_cycles(); |
---|
258 | if( DEBUG_DEV_TXT_RX < cycle ) |
---|
259 | printk("\n[%s] thread[%x,%x] exit / cycle %d\n", |
---|
260 | __FUNCTION__, this->process->pid, this->trdid, cycle ); |
---|
261 | #endif |
---|
262 | |
---|
263 | #if (DEBUG_SYS_READ & 1) |
---|
264 | exit_txt_read = hal_time_stamp(); |
---|
265 | #endif |
---|
266 | |
---|
267 | } |
---|
268 | |
---|
269 | //////////////////////////////////////////////// |
---|
270 | error_t dev_txt_sync_write( const char * buffer, |
---|
271 | uint32_t count ) |
---|
272 | { |
---|
273 | // get extended pointer on TXT[0] chdev |
---|
274 | xptr_t dev_xp = chdev_dir.txt_tx[0]; |
---|
275 | |
---|
276 | assert( (dev_xp != XPTR_NULL) , |
---|
277 | "undefined TXT0 chdev descriptor" ); |
---|
278 | |
---|
279 | // get TXTO chdev cluster and local pointer |
---|
280 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
---|
281 | chdev_t * dev_ptr = GET_PTR( dev_xp ); |
---|
282 | |
---|
283 | // get driver command function |
---|
284 | dev_aux_t * aux = (dev_aux_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->aux ) ); |
---|
285 | |
---|
286 | // build arguments structure |
---|
287 | txt_sync_args_t args; |
---|
288 | args.dev_xp = dev_xp; |
---|
289 | args.buffer = buffer; |
---|
290 | args.count = count; |
---|
291 | args.channel = 0; |
---|
292 | |
---|
293 | // call driver function |
---|
294 | aux( &args ); |
---|
295 | |
---|
296 | return 0; |
---|
297 | } |
---|
298 | |
---|