[1] | 1 | /* |
---|
| 2 | * dev_txt.c - TXT (Text Terminal) generic device API implementation. |
---|
[49] | 3 | * |
---|
[626] | 4 | * Author Alain Greiner (2016,2017,2018,2019) |
---|
[1] | 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 | |
---|
[457] | 24 | #include <hal_kernel_types.h> |
---|
[1] | 25 | #include <hal_special.h> |
---|
| 26 | #include <hal_remote.h> |
---|
[77] | 27 | #include <hal_drivers.h> |
---|
[1] | 28 | #include <thread.h> |
---|
[565] | 29 | #include <remote_busylock.h> |
---|
[407] | 30 | #include <chdev.h> |
---|
[1] | 31 | #include <rpc.h> |
---|
| 32 | #include <printk.h> |
---|
| 33 | #include <dev_txt.h> |
---|
| 34 | |
---|
| 35 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 36 | // Extern global variables |
---|
| 37 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 38 | |
---|
[3] | 39 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
---|
[1] | 40 | |
---|
[565] | 41 | |
---|
[438] | 42 | #if (DEBUG_SYS_READ & 1) |
---|
[407] | 43 | extern uint32_t enter_txt_read; |
---|
| 44 | extern uint32_t exit_txt_read; |
---|
| 45 | #endif |
---|
| 46 | |
---|
[438] | 47 | #if (DEBUG_SYS_WRITE & 1) |
---|
[435] | 48 | extern uint32_t enter_txt_write; |
---|
| 49 | extern uint32_t exit_txt_write; |
---|
| 50 | #endif |
---|
| 51 | |
---|
[565] | 52 | /////////////////////////////////////////////////// |
---|
[527] | 53 | const char * dev_txt_type_str( dev_txt_cmd_t type ) |
---|
[435] | 54 | { |
---|
[619] | 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 | } |
---|
[435] | 62 | } |
---|
| 63 | |
---|
[188] | 64 | ////////////////////////////////// |
---|
| 65 | void dev_txt_init( chdev_t * txt ) |
---|
[1] | 66 | { |
---|
[188] | 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 |
---|
[1] | 70 | |
---|
[188] | 71 | xptr_t pic_xp = chdev_dir.pic; |
---|
| 72 | uint32_t channel = txt->channel; |
---|
| 73 | uint32_t impl = txt->impl; |
---|
[407] | 74 | bool_t is_rx = txt->is_rx; |
---|
[1] | 75 | |
---|
[674] | 76 | assert( __FUNCTION__, ((pic_xp != XPTR_NULL) || (channel == 0)) , |
---|
[663] | 77 | "PIC not initialised before TXT" ); |
---|
[188] | 78 | |
---|
[23] | 79 | // set chdev name |
---|
[674] | 80 | if( is_rx ) snprintk( txt->name , 16 , "txt%d_rx" , channel ); |
---|
| 81 | else snprintk( txt->name , 16 , "txt%d_tx" , channel ); |
---|
[23] | 82 | |
---|
[422] | 83 | // set TXT chdev extension |
---|
| 84 | txt->ext.txt.owner_xp = XPTR_NULL; |
---|
| 85 | xlist_root_init( XPTR( local_cxy, &txt->ext.txt.root ) ); |
---|
[565] | 86 | remote_busylock_init( XPTR( local_cxy , &txt->ext.txt.lock ), LOCK_CHDEV_TXTLIST ); |
---|
[422] | 87 | |
---|
[245] | 88 | // call driver init function |
---|
[647] | 89 | hal_drivers_txt_init( txt ); |
---|
[1] | 90 | |
---|
[422] | 91 | // no server thread and no IRQ routing for TXT0 |
---|
[540] | 92 | // except for LETI where there MUST be IRQ routing |
---|
[422] | 93 | // no server thread for IMPL_TXT_RS2 (multiplexed in software, not hardware) |
---|
[255] | 94 | if( channel != 0 && impl != IMPL_TXT_RS2 ) |
---|
[188] | 95 | { |
---|
[407] | 96 | // select a core to execute the server thread |
---|
[637] | 97 | lid_t lid = cluster_select_local_core( local_cxy ); |
---|
[3] | 98 | |
---|
[540] | 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 | { |
---|
[407] | 105 | // bind IRQ to the selected core |
---|
[188] | 106 | dev_pic_bind_irq( lid , txt ); |
---|
[3] | 107 | |
---|
[407] | 108 | // enable IRQ |
---|
[238] | 109 | dev_pic_enable_irq( lid , XPTR( local_cxy , txt ) ); |
---|
[540] | 110 | } |
---|
[3] | 111 | |
---|
[188] | 112 | // create server thread |
---|
| 113 | thread_t * new_thread; |
---|
| 114 | error_t error; |
---|
[3] | 115 | |
---|
[188] | 116 | error = thread_kernel_create( &new_thread, |
---|
| 117 | THREAD_DEV, |
---|
[619] | 118 | &chdev_server_func, |
---|
[188] | 119 | txt, |
---|
| 120 | lid ); |
---|
[3] | 121 | |
---|
[674] | 122 | assert( __FUNCTION__, (error == 0) , "cannot create server thread" ); |
---|
[1] | 123 | |
---|
[188] | 124 | // set "server" field in chdev descriptor |
---|
| 125 | txt->server = new_thread; |
---|
[1] | 126 | |
---|
[408] | 127 | // set "chdev" field in thread descriptor |
---|
| 128 | new_thread->chdev = txt; |
---|
| 129 | |
---|
| 130 | // unblock server thread |
---|
[188] | 131 | thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL ); |
---|
| 132 | } |
---|
[637] | 133 | } // end dev_txt_init() |
---|
[1] | 134 | |
---|
| 135 | ///////////////////////////////////////// |
---|
| 136 | error_t dev_txt_write( uint32_t channel, |
---|
| 137 | char * buffer, |
---|
| 138 | uint32_t count ) |
---|
| 139 | { |
---|
[637] | 140 | error_t error; |
---|
[436] | 141 | |
---|
[438] | 142 | #if (DEBUG_SYS_WRITE & 1) |
---|
[436] | 143 | enter_txt_write = hal_time_stamp(); |
---|
| 144 | #endif |
---|
| 145 | |
---|
[657] | 146 | thread_t * this = CURRENT_THREAD; |
---|
| 147 | |
---|
[438] | 148 | #if DEBUG_DEV_TXT_TX |
---|
[619] | 149 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 150 | if( DEBUG_DEV_TXT_TX < cycle ) |
---|
[637] | 151 | printk("\n[%s] thread[%x,%x] enters for <%s> / cycle %d\n", |
---|
| 152 | __FUNCTION__, this->process->pid, this->trdid, buffer, cycle ); |
---|
[436] | 153 | #endif |
---|
| 154 | |
---|
[657] | 155 | // check channel argument |
---|
[674] | 156 | assert( __FUNCTION__, (channel < CONFIG_MAX_TXT_CHANNELS) , "illegal channel index" ); |
---|
[637] | 157 | |
---|
| 158 | // get pointers on chdev |
---|
[657] | 159 | xptr_t dev_xp = chdev_dir.txt_tx[channel]; |
---|
[637] | 160 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
---|
| 161 | chdev_t * dev_ptr = GET_PTR( dev_xp ); |
---|
[539] | 162 | |
---|
[657] | 163 | // check dev_xp |
---|
[674] | 164 | assert( __FUNCTION__, (dev_xp != XPTR_NULL) , "undefined TXT chdev descriptor" ); |
---|
[657] | 165 | |
---|
| 166 | // If we use MTTY (vci_multi_tty), we do a synchronous write on TXT[0] |
---|
| 167 | // If we use TTY (vci_tty_tsar), we do a standard asynchronous write |
---|
| 168 | // TODO this is not very clean ... [AG] |
---|
| 169 | |
---|
| 170 | if( dev_ptr->impl == IMPL_TXT_MTY ) |
---|
[539] | 171 | { |
---|
| 172 | // get driver command function |
---|
| 173 | dev_aux_t * aux = (dev_aux_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->aux ) ); |
---|
| 174 | |
---|
| 175 | // build arguments structure |
---|
| 176 | txt_sync_args_t args; |
---|
| 177 | args.dev_xp = dev_xp; |
---|
| 178 | args.buffer = buffer; |
---|
| 179 | args.count = count; |
---|
| 180 | args.channel = channel; |
---|
| 181 | |
---|
[637] | 182 | // call directly the driver function |
---|
[539] | 183 | aux( &args ); |
---|
| 184 | |
---|
[637] | 185 | error = 0; |
---|
[539] | 186 | } |
---|
| 187 | else |
---|
| 188 | { |
---|
[657] | 189 | // register command in calling thread descriptor |
---|
| 190 | this->txt_cmd.dev_xp = dev_xp; |
---|
| 191 | this->txt_cmd.type = TXT_WRITE; |
---|
| 192 | this->txt_cmd.buf_xp = XPTR( local_cxy , buffer ); |
---|
| 193 | this->txt_cmd.count = count; |
---|
[637] | 194 | |
---|
[657] | 195 | // register client thread in waiting queue, activate server thread |
---|
| 196 | // block client thread on THREAD_BLOCKED_IO and deschedule. |
---|
| 197 | // it is re-activated by the ISR signaling IO operation completion. |
---|
| 198 | chdev_register_command( dev_xp ); |
---|
| 199 | |
---|
| 200 | // get I/O operation status from calling thread descriptor |
---|
| 201 | error = this->txt_cmd.error; |
---|
| 202 | |
---|
[637] | 203 | if( error ) |
---|
| 204 | { |
---|
| 205 | printk("\n[ERROR] in %s : cannot write string %s / cycle %d\n", |
---|
| 206 | __FUNCTION__, buffer, (uint32_t)hal_get_cycles() ); |
---|
| 207 | } |
---|
[539] | 208 | } |
---|
[436] | 209 | |
---|
[438] | 210 | #if DEBUG_DEV_TXT_TX |
---|
[436] | 211 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 212 | if( DEBUG_DEV_TXT_TX < cycle ) |
---|
[637] | 213 | printk("\n[%s] thread[%x,%x] exit / cycle %d\n", |
---|
[619] | 214 | __FUNCTION__, this->process->pid, this->trdid, cycle ); |
---|
[436] | 215 | #endif |
---|
| 216 | |
---|
[438] | 217 | #if (DEBUG_SYS_WRITE & 1) |
---|
[436] | 218 | exit_txt_write = hal_time_stamp(); |
---|
| 219 | #endif |
---|
| 220 | |
---|
[637] | 221 | return error; |
---|
[49] | 222 | |
---|
[637] | 223 | } // end dev_txt_write() |
---|
| 224 | |
---|
[1] | 225 | ///////////////////////////////////////// |
---|
| 226 | error_t dev_txt_read( uint32_t channel, |
---|
| 227 | char * buffer ) |
---|
| 228 | { |
---|
[637] | 229 | error_t error; |
---|
[436] | 230 | |
---|
[438] | 231 | #if (DEBUG_SYS_READ & 1) |
---|
[436] | 232 | enter_txt_read = hal_time_stamp(); |
---|
| 233 | #endif |
---|
| 234 | |
---|
[657] | 235 | thread_t * this = CURRENT_THREAD; |
---|
| 236 | |
---|
[438] | 237 | #if DEBUG_DEV_TXT_RX |
---|
[619] | 238 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 239 | if( DEBUG_DEV_TXT_RX < cycle ) |
---|
[619] | 240 | printk("\n[%s] thread[%x,%x] enters / cycle %d\n", |
---|
| 241 | __FUNCTION__, this->process->pid, this->trdid, cycle ); |
---|
[436] | 242 | #endif |
---|
| 243 | |
---|
[657] | 244 | // check channel argument |
---|
[674] | 245 | assert( __FUNCTION__, (channel < CONFIG_MAX_TXT_CHANNELS) , "illegal channel index" ); |
---|
[436] | 246 | |
---|
[657] | 247 | // get pointers on chdev |
---|
| 248 | xptr_t dev_xp = chdev_dir.txt_rx[channel]; |
---|
| 249 | |
---|
| 250 | // check dev_xp |
---|
[674] | 251 | assert( __FUNCTION__, (dev_xp != XPTR_NULL) , "undefined TXT chdev descriptor" ); |
---|
[657] | 252 | |
---|
| 253 | // register command in calling thread descriptor |
---|
| 254 | this->txt_cmd.dev_xp = dev_xp; |
---|
| 255 | this->txt_cmd.type = TXT_READ; |
---|
| 256 | this->txt_cmd.buf_xp = XPTR( local_cxy , buffer ); |
---|
| 257 | this->txt_cmd.count = 1; |
---|
| 258 | |
---|
| 259 | // register client thread in waiting queue, activate server thread |
---|
| 260 | // block client thread on THREAD_BLOCKED_IO and deschedule. |
---|
| 261 | // it is re-activated by the ISR signaling IO operation completion. |
---|
| 262 | chdev_register_command( dev_xp ); |
---|
| 263 | |
---|
| 264 | // get I/O operation status from calling thread descriptor |
---|
| 265 | error = this->txt_cmd.error; |
---|
| 266 | |
---|
[637] | 267 | if( error ) |
---|
| 268 | { |
---|
[657] | 269 | printk("\n[ERROR] in %s : cannot write string %s / cycle %d\n", |
---|
| 270 | __FUNCTION__, buffer, (uint32_t)hal_get_cycles() ); |
---|
[637] | 271 | } |
---|
| 272 | |
---|
[438] | 273 | #if DEBUG_DEV_TXT_RX |
---|
[436] | 274 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 275 | if( DEBUG_DEV_TXT_RX < cycle ) |
---|
[637] | 276 | printk("\n[%s] thread[%x,%x] get character <%c> / cycle %d\n", |
---|
| 277 | __FUNCTION__, this->process->pid, this->trdid, *buffer, cycle ); |
---|
[436] | 278 | #endif |
---|
| 279 | |
---|
[438] | 280 | #if (DEBUG_SYS_READ & 1) |
---|
[436] | 281 | exit_txt_read = hal_time_stamp(); |
---|
| 282 | #endif |
---|
| 283 | |
---|
[637] | 284 | return error; |
---|
[1] | 285 | |
---|
[637] | 286 | } // end dev_txt_read() |
---|
| 287 | |
---|
[565] | 288 | //////////////////////////////////////////////// |
---|
| 289 | error_t dev_txt_sync_write( const char * buffer, |
---|
| 290 | uint32_t count ) |
---|
[1] | 291 | { |
---|
[674] | 292 | // get extended pointers on TXT[0] chdev |
---|
| 293 | xptr_t dev_xp = chdev_dir.txt_tx[0]; |
---|
| 294 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
---|
[565] | 295 | chdev_t * dev_ptr = GET_PTR( dev_xp ); |
---|
[1] | 296 | |
---|
[674] | 297 | if( dev_xp != XPTR_NULL) |
---|
| 298 | { |
---|
| 299 | // get driver command function |
---|
| 300 | dev_aux_t * aux = (dev_aux_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->aux ) ); |
---|
[1] | 301 | |
---|
[674] | 302 | // build arguments structure |
---|
| 303 | txt_sync_args_t args; |
---|
| 304 | args.dev_xp = dev_xp; |
---|
| 305 | args.buffer = buffer; |
---|
| 306 | args.count = count; |
---|
| 307 | args.channel = 0; |
---|
[407] | 308 | |
---|
[674] | 309 | // call driver function |
---|
| 310 | aux( &args ); |
---|
[1] | 311 | |
---|
[674] | 312 | return 0; |
---|
| 313 | } |
---|
| 314 | else |
---|
| 315 | { |
---|
| 316 | return -1; |
---|
| 317 | } |
---|
| 318 | } // end dev_txt_sync_write() |
---|
[1] | 319 | |
---|