[75] | 1 | /* |
---|
| 2 | * soclib_tty.c - soclib tty driver implementation. |
---|
| 3 | * |
---|
| 4 | * Author Alain Greiner (2016) |
---|
| 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 | #include <dev_txt.h> |
---|
| 25 | #include <chdev.h> |
---|
| 26 | #include <soclib_tty.h> |
---|
| 27 | #include <remote_spinlock.h> |
---|
| 28 | #include <thread.h> |
---|
[407] | 29 | #include <printk.h> |
---|
[75] | 30 | #include <hal_special.h> |
---|
| 31 | |
---|
[438] | 32 | #if (DEBUG_SYS_READ & 1) |
---|
[435] | 33 | extern uint32_t enter_tty_cmd_read; |
---|
| 34 | extern uint32_t exit_tty_cmd_read; |
---|
[407] | 35 | |
---|
[435] | 36 | extern uint32_t enter_tty_isr_read; |
---|
| 37 | extern uint32_t exit_tty_isr_read; |
---|
[407] | 38 | #endif |
---|
| 39 | |
---|
[438] | 40 | #if (DEBUG_SYS_WRITE & 1) |
---|
[435] | 41 | extern uint32_t enter_tty_cmd_write; |
---|
| 42 | extern uint32_t exit_tty_cmd_write; |
---|
| 43 | |
---|
| 44 | extern uint32_t enter_tty_isr_write; |
---|
| 45 | extern uint32_t exit_tty_isr_write; |
---|
| 46 | #endif |
---|
| 47 | |
---|
[436] | 48 | //////////////////////////////////////////////////////////////////////////////////// |
---|
| 49 | // These global variables implement the TTY_RX FIFOs (one per channel) |
---|
| 50 | //////////////////////////////////////////////////////////////////////////////////// |
---|
| 51 | |
---|
| 52 | __attribute__((section(".kdata"))) |
---|
| 53 | tty_fifo_t tty_rx_fifo[CONFIG_MAX_TXT_CHANNELS]; |
---|
| 54 | |
---|
| 55 | __attribute__((section(".kdata"))) |
---|
| 56 | tty_fifo_t tty_tx_fifo[CONFIG_MAX_TXT_CHANNELS]; |
---|
| 57 | |
---|
[75] | 58 | /////////////////////////////////////// |
---|
| 59 | void soclib_tty_init( chdev_t * chdev ) |
---|
| 60 | { |
---|
[407] | 61 | xptr_t reg_xp; |
---|
| 62 | |
---|
[436] | 63 | // initialise function pointers in chdev |
---|
[77] | 64 | chdev->cmd = &soclib_tty_cmd; |
---|
| 65 | chdev->isr = &soclib_tty_isr; |
---|
[407] | 66 | chdev->aux = &soclib_tty_aux; |
---|
[77] | 67 | |
---|
[407] | 68 | // get TTY channel and extended pointer on TTY peripheral base address |
---|
| 69 | xptr_t tty_xp = chdev->base; |
---|
| 70 | uint32_t channel = chdev->channel; |
---|
[436] | 71 | bool_t is_rx = chdev->is_rx; |
---|
[75] | 72 | |
---|
| 73 | // get SOCLIB_TTY device cluster and local pointer |
---|
| 74 | cxy_t tty_cxy = GET_CXY( tty_xp ); |
---|
[435] | 75 | uint32_t * tty_ptr = GET_PTR( tty_xp ); |
---|
[75] | 76 | |
---|
[435] | 77 | // set TTY_RX_IRQ_ENABLE |
---|
[407] | 78 | reg_xp = XPTR( tty_cxy , tty_ptr + (channel * TTY_SPAN) + TTY_RX_IRQ_ENABLE ); |
---|
[435] | 79 | hal_remote_sw( reg_xp , 1 ); |
---|
[407] | 80 | |
---|
| 81 | // reset TTY_TX_IRQ_ENABLE |
---|
| 82 | reg_xp = XPTR( tty_cxy , tty_ptr + (channel * TTY_SPAN) + TTY_TX_IRQ_ENABLE ); |
---|
| 83 | hal_remote_sw( reg_xp , 0 ); |
---|
[75] | 84 | |
---|
[436] | 85 | // reset relevant FIFO |
---|
| 86 | if( is_rx ) |
---|
| 87 | { |
---|
| 88 | tty_rx_fifo[channel].sts = 0; |
---|
| 89 | tty_rx_fifo[channel].ptr = 0; |
---|
| 90 | tty_rx_fifo[channel].ptw = 0; |
---|
| 91 | } |
---|
| 92 | else |
---|
| 93 | { |
---|
| 94 | tty_tx_fifo[channel].sts = 0; |
---|
| 95 | tty_tx_fifo[channel].ptr = 0; |
---|
| 96 | tty_tx_fifo[channel].ptw = 0; |
---|
| 97 | } |
---|
| 98 | } // end soclib_tty_init() |
---|
| 99 | |
---|
[75] | 100 | ////////////////////////////////////////////////////////////// |
---|
| 101 | void __attribute__ ((noinline)) soclib_tty_cmd( xptr_t th_xp ) |
---|
| 102 | { |
---|
[436] | 103 | tty_fifo_t * fifo; // TTY_RX or TTY_TX FIFO |
---|
| 104 | char byte; // byte value |
---|
| 105 | uint32_t done; // number of bytes moved |
---|
[407] | 106 | |
---|
[436] | 107 | // get client thread cluster and local pointer |
---|
| 108 | cxy_t th_cxy = GET_CXY( th_xp ); |
---|
| 109 | thread_t * th_ptr = GET_PTR( th_xp ); |
---|
| 110 | |
---|
| 111 | // get command arguments |
---|
| 112 | uint32_t type = hal_remote_lw ( XPTR( th_cxy , &th_ptr->txt_cmd.type ) ); |
---|
| 113 | xptr_t buf_xp = hal_remote_lwd( XPTR( th_cxy , &th_ptr->txt_cmd.buf_xp ) ); |
---|
| 114 | uint32_t count = hal_remote_lw ( XPTR( th_cxy , &th_ptr->txt_cmd.count ) ); |
---|
| 115 | xptr_t error_xp = XPTR( th_cxy , &th_ptr->txt_cmd.error ); |
---|
| 116 | |
---|
[438] | 117 | #if (DEBUG_SYS_READ & 1) |
---|
[435] | 118 | if( type == TXT_READ) enter_tty_cmd_read = (uint32_t)hal_get_cycles(); |
---|
[407] | 119 | #endif |
---|
| 120 | |
---|
[438] | 121 | #if (DEBUG_SYS_WRITE & 1) |
---|
[435] | 122 | if( type == TXT_WRITE) enter_tty_cmd_write = (uint32_t)hal_get_cycles(); |
---|
[432] | 123 | #endif |
---|
[407] | 124 | |
---|
[438] | 125 | #if DEBUG_HAL_TXT_RX |
---|
[446] | 126 | uint32_t rx_cycle = (uint32_t)hal_get_cycles(); |
---|
| 127 | if( (DEBUG_HAL_TXT_RX < rx_cycle) && (type == TXT_READ) ) |
---|
[436] | 128 | printk("\n[DBG] %s : thread %x enter for RX / cycle %d\n", |
---|
[446] | 129 | __FUNCTION__ , CURRENT_THREAD , rx_cycle ); |
---|
[436] | 130 | #endif |
---|
[75] | 131 | |
---|
[438] | 132 | #if DEBUG_HAL_TXT_TX |
---|
[446] | 133 | uint32_t tx_cycle = (uint32_t)hal_get_cycles(); |
---|
| 134 | if( (DEBUG_HAL_TXT_TX < tx_cycle) && (type == TXT_WRITE) ) |
---|
[436] | 135 | printk("\n[DBG] %s : thread %x enter for TX / cycle %d\n", |
---|
[446] | 136 | __FUNCTION__ , CURRENT_THREAD , tx_cycle ); |
---|
[435] | 137 | #endif |
---|
[407] | 138 | |
---|
[440] | 139 | // get TXT device cluster and pointers |
---|
| 140 | xptr_t dev_xp = (xptr_t)hal_remote_lwd( XPTR( th_cxy , &th_ptr->txt_cmd.dev_xp ) ); |
---|
| 141 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
---|
| 142 | chdev_t * dev_ptr = GET_PTR( dev_xp ); |
---|
[436] | 143 | |
---|
[440] | 144 | // get cluster and pointers for SOCLIB_TTY peripheral base segment |
---|
| 145 | xptr_t tty_xp = (xptr_t)hal_remote_lwd( XPTR( dev_cxy , &dev_ptr->base ) ); |
---|
[436] | 146 | cxy_t tty_cxy = GET_CXY( tty_xp ); |
---|
| 147 | uint32_t * tty_ptr = GET_PTR( tty_xp ); |
---|
| 148 | |
---|
| 149 | // get TTY channel index and channel base address |
---|
| 150 | uint32_t channel = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->channel ) ); |
---|
| 151 | uint32_t * base = tty_ptr + TTY_SPAN * channel; |
---|
| 152 | |
---|
| 153 | /////////////////////// |
---|
| 154 | if( type == TXT_WRITE ) // write bytes to TTY_TX FIFO |
---|
[435] | 155 | { |
---|
[436] | 156 | fifo = &tty_tx_fifo[channel]; |
---|
[75] | 157 | |
---|
[436] | 158 | done = 0; |
---|
[75] | 159 | |
---|
[436] | 160 | while( done < count ) |
---|
| 161 | { |
---|
| 162 | if( fifo->sts < TTY_FIFO_DEPTH ) // put one byte to FIFO if TX_FIFO not full |
---|
| 163 | { |
---|
| 164 | // get one byte from command buffer |
---|
| 165 | byte = hal_remote_lb( buf_xp + done ); |
---|
[75] | 166 | |
---|
[436] | 167 | // write byte to FIFO |
---|
| 168 | fifo->data[fifo->ptw] = byte; |
---|
[75] | 169 | |
---|
[436] | 170 | // prevent race |
---|
| 171 | hal_fence(); |
---|
[75] | 172 | |
---|
[436] | 173 | // update FIFO state |
---|
| 174 | fifo->ptw = (fifo->ptw + 1) % TTY_FIFO_DEPTH; |
---|
| 175 | hal_atomic_add( &fifo->sts , 1 ); |
---|
[75] | 176 | |
---|
[436] | 177 | // udate number of bytes moved |
---|
| 178 | done++; |
---|
[75] | 179 | |
---|
[436] | 180 | // enable TX_IRQ |
---|
| 181 | hal_remote_sw( XPTR( tty_cxy , base + TTY_TX_IRQ_ENABLE ) , 1 ); |
---|
| 182 | } |
---|
| 183 | else // block & deschedule if TX_FIFO full |
---|
| 184 | { |
---|
| 185 | // block on ISR |
---|
| 186 | thread_block( XPTR( local_cxy , CURRENT_THREAD ) , THREAD_BLOCKED_ISR ); |
---|
| 187 | |
---|
| 188 | // deschedule |
---|
| 189 | sched_yield( "TTY_TX_FIFO full" ); |
---|
| 190 | } |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | // set error status in command and return |
---|
| 194 | hal_remote_sw( error_xp , 0 ); |
---|
[435] | 195 | } |
---|
[436] | 196 | /////////////////////////// |
---|
| 197 | else if( type == TXT_READ ) // read bytes from TTY_RX FIFO |
---|
[435] | 198 | { |
---|
[436] | 199 | fifo = &tty_rx_fifo[channel]; |
---|
[75] | 200 | |
---|
[436] | 201 | done = 0; |
---|
| 202 | |
---|
| 203 | while( done < count ) |
---|
| 204 | { |
---|
| 205 | if( fifo->sts > 0 ) // get byte from FIFO if not empty |
---|
| 206 | { |
---|
| 207 | // get one byte from FIFO |
---|
| 208 | char byte = fifo->data[fifo->ptr]; |
---|
| 209 | |
---|
| 210 | // update FIFO state |
---|
| 211 | fifo->ptr = (fifo->ptr + 1) % TTY_FIFO_DEPTH; |
---|
| 212 | hal_atomic_add( &fifo->sts , -1 ); |
---|
| 213 | |
---|
| 214 | // set byte to command buffer |
---|
| 215 | hal_remote_sb( buf_xp + done , byte ); |
---|
| 216 | |
---|
| 217 | // udate number of bytes |
---|
| 218 | done++; |
---|
| 219 | } |
---|
| 220 | else // deschedule if FIFO empty |
---|
| 221 | { |
---|
| 222 | // block on ISR |
---|
| 223 | thread_block( XPTR( local_cxy , CURRENT_THREAD ) , THREAD_BLOCKED_ISR ); |
---|
| 224 | |
---|
| 225 | // deschedule |
---|
[446] | 226 | sched_yield( "TTY_RX_FIFO empty" ); |
---|
[436] | 227 | } |
---|
| 228 | } // end while |
---|
| 229 | |
---|
| 230 | // set error status in command |
---|
| 231 | hal_remote_sw( error_xp , 0 ); |
---|
[435] | 232 | } |
---|
| 233 | else |
---|
| 234 | { |
---|
| 235 | assert( false , __FUNCTION__ , "illegal TXT command\n" ); |
---|
| 236 | } |
---|
| 237 | |
---|
[438] | 238 | #if DEBUG_HAL_TXT_RX |
---|
[446] | 239 | rx_cycle = (uint32_t)hal_get_cycles(); |
---|
| 240 | if( (DEBUG_HAL_TXT_RX < rx_cycle) && (type == TXT_READ) ) |
---|
[436] | 241 | printk("\n[DBG] %s : thread %x exit after RX / cycle %d\n", |
---|
[446] | 242 | __FUNCTION__ , CURRENT_THREAD , rx_cycle ); |
---|
[432] | 243 | #endif |
---|
[407] | 244 | |
---|
[438] | 245 | #if DEBUG_HAL_TXT_TX |
---|
[446] | 246 | tx_cycle = (uint32_t)hal_get_cycles(); |
---|
| 247 | if( (DEBUG_HAL_TXT_TX < tx_cycle) && (type == TXT_WRITE) ) |
---|
[436] | 248 | printk("\n[DBG] %s : thread %x exit after TX / cycle %d\n", |
---|
[446] | 249 | __FUNCTION__ , CURRENT_THREAD , tx_cycle ); |
---|
[436] | 250 | #endif |
---|
| 251 | |
---|
[438] | 252 | #if (DEBUG_SYS_READ & 1) |
---|
[435] | 253 | if( type == TXT_READ ) exit_tty_cmd_read = (uint32_t)hal_get_cycles(); |
---|
[407] | 254 | #endif |
---|
| 255 | |
---|
[438] | 256 | #if (DEBUG_SYS_WRITE & 1) |
---|
[435] | 257 | if( type == TXT_WRITE ) exit_tty_cmd_write = (uint32_t)hal_get_cycles(); |
---|
| 258 | #endif |
---|
| 259 | |
---|
[407] | 260 | } // end soclib_tty_cmd() |
---|
| 261 | |
---|
[75] | 262 | ///////////////////////////////////////////////////////////////// |
---|
| 263 | void __attribute__ ((noinline)) soclib_tty_isr( chdev_t * chdev ) |
---|
| 264 | { |
---|
[446] | 265 | thread_t * server; // pointer on TXT chdev server thread |
---|
| 266 | lid_t server_lid; // local index of core running the server thread |
---|
| 267 | uint32_t channel; // TXT chdev channel |
---|
| 268 | bool_t is_rx; // TXT chdev direction |
---|
| 269 | char byte; // byte value |
---|
| 270 | xptr_t owner_xp; // extended pointer on TXT owner process |
---|
| 271 | cxy_t owner_cxy; // TXT owner process cluster |
---|
| 272 | process_t * owner_ptr; // local pointer on TXT owner process |
---|
| 273 | pid_t owner_pid; // TXT owner process identifier |
---|
| 274 | tty_fifo_t * fifo; // pointer on TTY_TX or TTY_RX FIFO |
---|
| 275 | cxy_t tty_cxy; // soclib_tty cluster |
---|
| 276 | uint32_t * tty_ptr; // soclib_tty segment base address |
---|
| 277 | uint32_t * base; // soclib_tty channel base address |
---|
| 278 | xptr_t status_xp; // extended pointer on TTY_STATUS register |
---|
| 279 | xptr_t write_xp; // extended pointer on TTY_WRITE register |
---|
| 280 | xptr_t read_xp; // extended pointer on TTY_READ register |
---|
| 281 | xptr_t parent_xp; // extended pointer on parent process |
---|
| 282 | cxy_t parent_cxy; // parent process cluster |
---|
| 283 | process_t * parent_ptr; // local pointer on parent process |
---|
| 284 | xptr_t children_lock_xp; // extended pointer on children processes lock |
---|
| 285 | thread_t * parent_main_ptr; // extended pointer on parent process main thread |
---|
| 286 | xptr_t parent_main_xp; // local pointer on parent process main thread |
---|
[75] | 287 | |
---|
[436] | 288 | // get TXT chdev channel, direction and server thread |
---|
| 289 | channel = chdev->channel; |
---|
| 290 | is_rx = chdev->is_rx; |
---|
| 291 | server = chdev->server; |
---|
| 292 | server_lid = server->core->lid; |
---|
| 293 | |
---|
[438] | 294 | #if (DEBUG_SYS_READ & 1) |
---|
[436] | 295 | if( is_rx ) enter_tty_isr_read = (uint32_t)hal_get_cycles(); |
---|
[407] | 296 | #endif |
---|
| 297 | |
---|
[438] | 298 | #if (DEBUG_SYS_WRITE & 1) |
---|
[436] | 299 | if( is_rx == 0 ) enter_tty_isr_write = (uint32_t)hal_get_cycles(); |
---|
[435] | 300 | #endif |
---|
| 301 | |
---|
[438] | 302 | #if DEBUG_HAL_TXT_RX |
---|
[446] | 303 | uint32_t rx_cycle = (uint32_t)hal_get_cycles(); |
---|
[432] | 304 | #endif |
---|
| 305 | |
---|
[438] | 306 | #if DEBUG_HAL_TXT_TX |
---|
[446] | 307 | uint32_t tx_cycle = (uint32_t)hal_get_cycles(); |
---|
[436] | 308 | #endif |
---|
[75] | 309 | |
---|
[424] | 310 | // get SOCLIB_TTY peripheral cluster and local pointer |
---|
[436] | 311 | tty_cxy = GET_CXY( chdev->base ); |
---|
| 312 | tty_ptr = GET_PTR( chdev->base ); |
---|
[424] | 313 | |
---|
| 314 | // get channel base address |
---|
[436] | 315 | base = tty_ptr + TTY_SPAN * channel; |
---|
[424] | 316 | |
---|
| 317 | // get extended pointer on TTY registers |
---|
| 318 | status_xp = XPTR( tty_cxy , base + TTY_STATUS ); |
---|
| 319 | write_xp = XPTR( tty_cxy , base + TTY_WRITE ); |
---|
| 320 | read_xp = XPTR( tty_cxy , base + TTY_READ ); |
---|
| 321 | |
---|
[436] | 322 | /////////////////////////// handle RX ////////////////////// |
---|
| 323 | if( is_rx ) |
---|
[424] | 324 | { |
---|
[436] | 325 | fifo = &tty_rx_fifo[channel]; |
---|
[424] | 326 | |
---|
[436] | 327 | // try to move bytes until TTY_READ register empty |
---|
| 328 | while( hal_remote_lw( status_xp ) & TTY_STATUS_RX_FULL ) |
---|
[435] | 329 | { |
---|
[436] | 330 | // get one byte from TTY_READ register & acknowledge RX_IRQ |
---|
| 331 | byte = (char)hal_remote_lb( read_xp ); |
---|
[424] | 332 | |
---|
[446] | 333 | // filter special character ^Z => block TXT owner process |
---|
[436] | 334 | if( byte == 0x1A ) |
---|
| 335 | { |
---|
[446] | 336 | |
---|
| 337 | #if DEBUG_HAL_TXT_RX |
---|
| 338 | if( DEBUG_HAL_TXT_RX < rx_cycle ) |
---|
| 339 | printk("\n[DBG] %s : read ^Z character from TXT%d\n", __FUNCTION__, channel ); |
---|
| 340 | #endif |
---|
[436] | 341 | // get pointers on TXT owner process in owner cluster |
---|
| 342 | owner_xp = process_txt_get_owner( channel ); |
---|
| 343 | |
---|
| 344 | // check process exist |
---|
| 345 | assert( (owner_xp != XPTR_NULL) , __FUNCTION__, |
---|
| 346 | "TXT owner process not found\n" ); |
---|
[435] | 347 | |
---|
[446] | 348 | // get relevant infos on TXT owner process |
---|
[436] | 349 | owner_cxy = GET_CXY( owner_xp ); |
---|
| 350 | owner_ptr = GET_PTR( owner_xp ); |
---|
| 351 | owner_pid = hal_remote_lw( XPTR( owner_cxy , &owner_ptr->pid ) ); |
---|
[424] | 352 | |
---|
[446] | 353 | // block TXT owner process only if it is not the INIT process |
---|
| 354 | if( owner_pid != 1 ) |
---|
[436] | 355 | { |
---|
[446] | 356 | // get parent process descriptor pointers |
---|
| 357 | parent_xp = hal_remote_lwd( XPTR( owner_cxy , &owner_ptr->parent_xp ) ); |
---|
| 358 | parent_cxy = GET_CXY( parent_xp ); |
---|
| 359 | parent_ptr = GET_PTR( parent_xp ); |
---|
| 360 | |
---|
| 361 | // get extended pointer on lock protecting children list in parent process |
---|
| 362 | children_lock_xp = XPTR( parent_cxy , &parent_ptr->children_lock ); |
---|
| 363 | |
---|
| 364 | // get pointers on the parent process main thread |
---|
| 365 | parent_main_ptr = hal_remote_lpt(XPTR(parent_cxy,&parent_ptr->th_tbl[0])); |
---|
| 366 | parent_main_xp = XPTR( parent_cxy , parent_main_ptr ); |
---|
| 367 | |
---|
| 368 | // transfer TXT ownership |
---|
| 369 | process_txt_transfer_ownership( owner_xp ); |
---|
| 370 | |
---|
| 371 | // block all threads in all clusters, but the main thread |
---|
[436] | 372 | process_sigaction( owner_pid , BLOCK_ALL_THREADS ); |
---|
[424] | 373 | |
---|
[446] | 374 | // block the main thread |
---|
| 375 | xptr_t main_xp = XPTR( owner_cxy , &owner_ptr->th_tbl[0] ); |
---|
| 376 | thread_block( main_xp , THREAD_BLOCKED_GLOBAL ); |
---|
| 377 | |
---|
[436] | 378 | // atomically update owner process termination state |
---|
| 379 | hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) , |
---|
| 380 | PROCESS_TERM_STOP ); |
---|
[446] | 381 | |
---|
| 382 | // take the children lock and unblock the parent process main thread |
---|
| 383 | remote_spinlock_lock( children_lock_xp ); |
---|
| 384 | thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT ); |
---|
| 385 | remote_spinlock_unlock( children_lock_xp ); |
---|
| 386 | |
---|
[436] | 387 | return; |
---|
| 388 | } |
---|
| 389 | } |
---|
| 390 | |
---|
[446] | 391 | // filter special character ^C => kill TXT owner process |
---|
[436] | 392 | if( byte == 0x03 ) |
---|
| 393 | { |
---|
[446] | 394 | |
---|
| 395 | #if DEBUG_HAL_TXT_RX |
---|
| 396 | if( DEBUG_HAL_TXT_RX < rx_cycle ) |
---|
| 397 | printk("\n[DBG] %s : read ^C character from TXT%d\n", __FUNCTION__, channel ); |
---|
| 398 | #endif |
---|
[440] | 399 | // get pointer on TXT owner process in owner cluster |
---|
[436] | 400 | owner_xp = process_txt_get_owner( channel ); |
---|
| 401 | |
---|
| 402 | // check process exist |
---|
| 403 | assert( (owner_xp != XPTR_NULL) , __FUNCTION__, |
---|
| 404 | "TXT owner process not found\n" ); |
---|
| 405 | |
---|
| 406 | // get relevant infos on TXT owner process |
---|
| 407 | owner_cxy = GET_CXY( owner_xp ); |
---|
| 408 | owner_ptr = GET_PTR( owner_xp ); |
---|
| 409 | owner_pid = hal_remote_lw( XPTR( owner_cxy , &owner_ptr->pid ) ); |
---|
| 410 | |
---|
[446] | 411 | // kill TXT owner process only if it is not the INIT process |
---|
[436] | 412 | if( owner_pid != 1 ) |
---|
| 413 | { |
---|
[446] | 414 | // get parent process descriptor pointers |
---|
| 415 | parent_xp = hal_remote_lwd( XPTR( owner_cxy , &owner_ptr->parent_xp ) ); |
---|
| 416 | parent_cxy = GET_CXY( parent_xp ); |
---|
| 417 | parent_ptr = GET_PTR( parent_xp ); |
---|
| 418 | |
---|
| 419 | // get extended pointer on lock protecting children list in parent process |
---|
| 420 | children_lock_xp = XPTR( parent_cxy , &parent_ptr->children_lock ); |
---|
| 421 | |
---|
| 422 | // get pointers on the parent process main thread |
---|
| 423 | parent_main_ptr = hal_remote_lpt(XPTR(parent_cxy,&parent_ptr->th_tbl[0])); |
---|
| 424 | parent_main_xp = XPTR( parent_cxy , parent_main_ptr ); |
---|
| 425 | |
---|
[436] | 426 | // remove process from TXT list |
---|
| 427 | process_txt_detach( owner_xp ); |
---|
| 428 | |
---|
[440] | 429 | // mark for delete all thread in all clusters, but the main |
---|
[436] | 430 | process_sigaction( owner_pid , DELETE_ALL_THREADS ); |
---|
[435] | 431 | |
---|
[446] | 432 | // block main thread |
---|
[436] | 433 | xptr_t main_xp = XPTR( owner_cxy , &owner_ptr->th_tbl[0] ); |
---|
| 434 | thread_block( main_xp , THREAD_BLOCKED_GLOBAL ); |
---|
[435] | 435 | |
---|
[436] | 436 | // atomically update owner process termination state |
---|
| 437 | hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) , |
---|
| 438 | PROCESS_TERM_KILL ); |
---|
[446] | 439 | |
---|
| 440 | // take the children lock and unblock the parent process main thread |
---|
| 441 | remote_spinlock_lock( children_lock_xp ); |
---|
| 442 | thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT ); |
---|
| 443 | remote_spinlock_unlock( children_lock_xp ); |
---|
| 444 | |
---|
[436] | 445 | return; |
---|
| 446 | } |
---|
| 447 | } |
---|
[424] | 448 | |
---|
[436] | 449 | // write byte in TTY_RX FIFO if not full / discard byte if full |
---|
| 450 | if ( fifo->sts < TTY_FIFO_DEPTH ) |
---|
| 451 | { |
---|
[446] | 452 | |
---|
| 453 | #if DEBUG_HAL_TXT_RX |
---|
| 454 | if( DEBUG_HAL_TXT_RX < rx_cycle ) |
---|
| 455 | printk("\n[DBG] %s : read character %c from TXT%d\n", __FUNCTION__, byte, channel ); |
---|
| 456 | #endif |
---|
[436] | 457 | // store byte into FIFO |
---|
| 458 | fifo->data[fifo->ptw] = (char)byte; |
---|
| 459 | |
---|
| 460 | // avoid race |
---|
| 461 | hal_fence(); |
---|
| 462 | |
---|
| 463 | // update RX_FIFO state |
---|
| 464 | fifo->ptw = (fifo->ptw + 1) % TTY_FIFO_DEPTH; |
---|
| 465 | hal_atomic_add( &fifo->sts , 1 ); |
---|
| 466 | |
---|
| 467 | // unblock TXT_RX server thread |
---|
| 468 | thread_unblock( XPTR( local_cxy , server ) , THREAD_BLOCKED_ISR ); |
---|
| 469 | |
---|
| 470 | // send IPI to core running server thread |
---|
| 471 | dev_pic_send_ipi( local_cxy , server_lid ); |
---|
| 472 | } |
---|
| 473 | else |
---|
| 474 | { |
---|
| 475 | printk("\n[WARNING] %s : TTY_RX_FIFO[%d] full => discard character <%x>\n", |
---|
| 476 | __FUNCTION__, channel, (uint32_t)byte ); |
---|
| 477 | } |
---|
| 478 | } // end while TTY_READ register full |
---|
| 479 | |
---|
| 480 | } // end RX |
---|
| 481 | |
---|
| 482 | /////////////////////// handle TX ///////////////////////////// |
---|
| 483 | else |
---|
[424] | 484 | { |
---|
[436] | 485 | fifo = &tty_tx_fifo[channel]; |
---|
| 486 | |
---|
| 487 | // try to move bytes until TX_FIFO empty |
---|
| 488 | while( fifo->sts > 0 ) |
---|
[424] | 489 | { |
---|
[446] | 490 | // write one byte to TTY_WRITE register if empty / exit loop if full |
---|
[436] | 491 | if( (hal_remote_lw( status_xp ) & TTY_STATUS_TX_FULL) == 0 ) |
---|
[424] | 492 | { |
---|
[436] | 493 | // get one byte from TX_FIFO |
---|
| 494 | byte = fifo->data[fifo->ptr]; |
---|
[424] | 495 | |
---|
[446] | 496 | #if DEBUG_HAL_TXT_TX |
---|
| 497 | if( DEBUG_HAL_TXT_TX < tx_cycle ) |
---|
| 498 | printk("\n[DBG] %s : write character %c to TXT%d\n", __FUNCTION__, byte, channel ); |
---|
| 499 | #endif |
---|
[436] | 500 | // update TX_FIFO state |
---|
| 501 | fifo->ptr = (fifo->ptr + 1) % TTY_FIFO_DEPTH; |
---|
| 502 | hal_atomic_add( &fifo->sts , -1 ); |
---|
| 503 | |
---|
| 504 | // write byte to TTY_WRITE register & acknowledge TX_IRQ |
---|
[424] | 505 | hal_remote_sb( write_xp , byte ); |
---|
| 506 | } |
---|
| 507 | } |
---|
| 508 | |
---|
[435] | 509 | // disable TX_IRQ |
---|
| 510 | hal_remote_sw( XPTR( tty_cxy , base + TTY_TX_IRQ_ENABLE ) , 0 ); |
---|
[424] | 511 | |
---|
[436] | 512 | // unblock TXT_TX server thread |
---|
| 513 | thread_unblock( XPTR( local_cxy , server ) , THREAD_BLOCKED_ISR ); |
---|
[424] | 514 | |
---|
[436] | 515 | // send IPI to core running server thread |
---|
| 516 | dev_pic_send_ipi( local_cxy , server_lid ); |
---|
[446] | 517 | |
---|
[436] | 518 | } // end TX |
---|
| 519 | |
---|
[435] | 520 | hal_fence(); |
---|
[424] | 521 | |
---|
[438] | 522 | #if (DEBUG_SYS_READ & 1) |
---|
[436] | 523 | if( is_rx ) exit_tty_isr_read = (uint32_t)hal_get_cycles(); |
---|
[424] | 524 | #endif |
---|
| 525 | |
---|
[438] | 526 | #if (DEBUG_SYS_WRITE & 1) |
---|
[436] | 527 | if( is_rx == 0 ) exit_tty_isr_write = (uint32_t)hal_get_cycles(); |
---|
[435] | 528 | #endif |
---|
| 529 | |
---|
[424] | 530 | } // end soclib_tty_isr() |
---|
| 531 | |
---|
[436] | 532 | ///////////////////////////////////////////////////////////// |
---|
| 533 | void __attribute__ ((noinline)) soclib_tty_aux( void * args ) |
---|
| 534 | { |
---|
| 535 | uint32_t status; |
---|
| 536 | bool_t empty; |
---|
| 537 | uint32_t i; |
---|
| 538 | |
---|
| 539 | xptr_t dev_xp = ((txt_sync_args_t *)args)->dev_xp; |
---|
| 540 | char * buffer = ((txt_sync_args_t *)args)->buffer; |
---|
| 541 | uint32_t count = ((txt_sync_args_t *)args)->count; |
---|
| 542 | |
---|
| 543 | // get TXT0 chdev cluster and local pointer |
---|
| 544 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
---|
| 545 | chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); |
---|
| 546 | |
---|
| 547 | // get extended pointer on TTY channel base address |
---|
| 548 | xptr_t tty_xp = (xptr_t)hal_remote_lwd( XPTR( dev_cxy , &dev_ptr->base ) ); |
---|
| 549 | |
---|
| 550 | // get TTY channel segment cluster and local pointer |
---|
| 551 | cxy_t tty_cxy = GET_CXY( tty_xp ); |
---|
| 552 | uint32_t * tty_ptr = (uint32_t *)GET_PTR( tty_xp ); |
---|
| 553 | |
---|
| 554 | // get extended pointers on TTY_WRITE & TTY_STATUS registers |
---|
| 555 | xptr_t write_xp = XPTR( tty_cxy , tty_ptr + TTY_WRITE ); |
---|
| 556 | xptr_t status_xp = XPTR( tty_cxy , tty_ptr + TTY_STATUS ); |
---|
| 557 | |
---|
| 558 | // loop on characters (busy waiting policy) |
---|
| 559 | for( i = 0 ; i < count ; i++ ) |
---|
| 560 | { |
---|
| 561 | do |
---|
| 562 | { |
---|
| 563 | // get TTY_STATUS |
---|
| 564 | status = hal_remote_lw( status_xp ); |
---|
| 565 | empty = ( (status & TTY_STATUS_TX_FULL) == 0 ); |
---|
| 566 | |
---|
| 567 | // transfer one byte if TX buffer empty |
---|
| 568 | if ( empty ) hal_remote_sb( write_xp , buffer[i] ); |
---|
| 569 | } |
---|
| 570 | while ( empty == false ); |
---|
| 571 | } |
---|
| 572 | } // end soclib_tty_aux() |
---|
| 573 | |
---|
| 574 | |
---|
| 575 | |
---|