| 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 <soclib_tty.h> |
|---|
| 28 | #include <thread.h> |
|---|
| 29 | #include <rpc.h> |
|---|
| 30 | #include <printk.h> |
|---|
| 31 | #include <dev_txt.h> |
|---|
| 32 | |
|---|
| 33 | ///////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 34 | // Extern global variables |
|---|
| 35 | ///////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 36 | |
|---|
| 37 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
|---|
| 38 | |
|---|
| 39 | extern chdev_pic_input_t chdev_pic_input; // allocated in kernel_init.c |
|---|
| 40 | |
|---|
| 41 | //////////////////////////////////// |
|---|
| 42 | void dev_txt_init( chdev_t * chdev ) |
|---|
| 43 | { |
|---|
| 44 | // the local ICU chdev must be initialized before the TXT chdev, because |
|---|
| 45 | // the TXT chdev initialisation requires allocation of a WTI from local ICU |
|---|
| 46 | xptr_t icu_xp = chdev_dir.icu[local_cxy]; |
|---|
| 47 | assert( (icu_xp != XPTR_NULL) , __FUNCTION__ , "ICU not initialised before TXT" ); |
|---|
| 48 | |
|---|
| 49 | // get implementation and channel index |
|---|
| 50 | uint32_t impl = chdev->impl; |
|---|
| 51 | uint32_t channel = chdev->channel; |
|---|
| 52 | |
|---|
| 53 | // set chdev name |
|---|
| 54 | snprintf( chdev->name , 16 , "txt_%d" , chdev->channel ); |
|---|
| 55 | |
|---|
| 56 | // set fields "cmd", "isr", and call driver init function |
|---|
| 57 | if( impl == IMPL_TXT_TTY ) |
|---|
| 58 | { |
|---|
| 59 | chdev->cmd = &soclib_tty_cmd; |
|---|
| 60 | chdev->isr = &soclib_tty_isr; |
|---|
| 61 | soclib_tty_init( chdev ); |
|---|
| 62 | } |
|---|
| 63 | else |
|---|
| 64 | { |
|---|
| 65 | assert( false , __FUNCTION__ , "undefined TXT device implementation" ); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | // get a WTI mailbox from local ICU |
|---|
| 69 | uint32_t wti_id = dev_icu_wti_alloc(); |
|---|
| 70 | |
|---|
| 71 | assert( (wti_id != -1) , __FUNCTION__ , "cannot allocate WTI mailbox" ); |
|---|
| 72 | |
|---|
| 73 | // select a core |
|---|
| 74 | lid_t lid = cluster_select_local_core(); |
|---|
| 75 | |
|---|
| 76 | // enable WTI IRQ and update WTI interrupt vector |
|---|
| 77 | dev_icu_enable_irq( lid , WTI_TYPE , wti_id , chdev ); |
|---|
| 78 | |
|---|
| 79 | // link IOC IRQ to WTI mailbox in PIC component |
|---|
| 80 | uint32_t irq_id = chdev_pic_input.txt[channel]; |
|---|
| 81 | dev_pic_bind_irq( irq_id , local_cxy , wti_id ); |
|---|
| 82 | |
|---|
| 83 | // create server thread |
|---|
| 84 | thread_t * new_thread; |
|---|
| 85 | error_t error; |
|---|
| 86 | |
|---|
| 87 | error = thread_kernel_create( &new_thread, |
|---|
| 88 | THREAD_DEV, |
|---|
| 89 | &chdev_sequencial_server, |
|---|
| 90 | chdev, |
|---|
| 91 | lid ); |
|---|
| 92 | assert( (error == 0) , __FUNCTION__ , "cannot create server thread" ); |
|---|
| 93 | |
|---|
| 94 | // set "server" field in chdev descriptor |
|---|
| 95 | chdev->server = new_thread; |
|---|
| 96 | |
|---|
| 97 | // start server thread |
|---|
| 98 | thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL ); |
|---|
| 99 | |
|---|
| 100 | } // end dev_txt_init() |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 104 | // This static function is called by dev_txt_read(), dev_txt_write() functions. |
|---|
| 105 | ////////////////////////////////////i///////////////////////////////////////////// |
|---|
| 106 | static error_t dev_txt_access( uint32_t type, |
|---|
| 107 | uint32_t channel, |
|---|
| 108 | char * buffer, |
|---|
| 109 | uint32_t count ) |
|---|
| 110 | { |
|---|
| 111 | thread_t * this = CURRENT_THREAD; |
|---|
| 112 | |
|---|
| 113 | txt_dmsg("\n[INFO] in %s : thread %x in process %x enters\n", |
|---|
| 114 | __FUNCTION__ , this->trdid , this->process->pid ); |
|---|
| 115 | |
|---|
| 116 | // check channel argument |
|---|
| 117 | assert( (channel < CONFIG_MAX_TXT_CHANNELS) , __FUNCTION__ , "illegal channel index" ); |
|---|
| 118 | |
|---|
| 119 | // get extended pointer on remote TXT chdev descriptor |
|---|
| 120 | xptr_t dev_xp = chdev_dir.txt[channel]; |
|---|
| 121 | |
|---|
| 122 | assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined TXT chdev descriptor" ); |
|---|
| 123 | |
|---|
| 124 | // register command in calling thread descriptor |
|---|
| 125 | this->command.txt.dev_xp = dev_xp; |
|---|
| 126 | this->command.txt.type = type; |
|---|
| 127 | this->command.txt.buf_xp = XPTR( local_cxy , buffer ); |
|---|
| 128 | this->command.txt.count = count; |
|---|
| 129 | |
|---|
| 130 | // register client thread in waiting queue, activate server thread |
|---|
| 131 | // block client thread on THREAD_BLOCKED_IO and deschedule. |
|---|
| 132 | // it is re-activated by the ISR signaling IO operation completion. |
|---|
| 133 | chdev_register_command( dev_xp , this ); |
|---|
| 134 | |
|---|
| 135 | txt_dmsg("\n[INFO] in %s : thread %x in process %x completes / error = %d\n", |
|---|
| 136 | __FUNCTION__ , this->trdid , this->process->pid , this->command.txt.error ); |
|---|
| 137 | |
|---|
| 138 | // return I/O operation status from calling thread descriptor |
|---|
| 139 | return this->command.txt.error; |
|---|
| 140 | |
|---|
| 141 | } // end dev_txt_access() |
|---|
| 142 | |
|---|
| 143 | ///////////////////////////////////////// |
|---|
| 144 | error_t dev_txt_write( uint32_t channel, |
|---|
| 145 | char * buffer, |
|---|
| 146 | uint32_t count ) |
|---|
| 147 | { |
|---|
| 148 | return dev_txt_access( TXT_WRITE , channel , buffer , count ); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | ///////////////////////////////////////// |
|---|
| 152 | error_t dev_txt_read( uint32_t channel, |
|---|
| 153 | char * buffer ) |
|---|
| 154 | { |
|---|
| 155 | return dev_txt_access( TXT_READ , channel , buffer , 1 ); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | /////////////////////////////////////////////// |
|---|
| 159 | error_t dev_txt_sync_write( uint32_t channel, |
|---|
| 160 | char * buffer, |
|---|
| 161 | uint32_t count ) |
|---|
| 162 | { |
|---|
| 163 | // get pointer on calling thread |
|---|
| 164 | thread_t * this = CURRENT_THREAD; |
|---|
| 165 | |
|---|
| 166 | // get extended pointer on TXT[0] chdev |
|---|
| 167 | xptr_t dev_xp = chdev_dir.txt[channel]; |
|---|
| 168 | |
|---|
| 169 | assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined TXT0 chdev descriptor" ); |
|---|
| 170 | |
|---|
| 171 | // register command in calling thread |
|---|
| 172 | this->command.txt.dev_xp = dev_xp; |
|---|
| 173 | this->command.txt.type = TXT_SYNC_WRITE; |
|---|
| 174 | this->command.txt.buf_xp = XPTR( local_cxy , buffer ); |
|---|
| 175 | this->command.txt.count = count; |
|---|
| 176 | |
|---|
| 177 | // get driver command function |
|---|
| 178 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
|---|
| 179 | chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); |
|---|
| 180 | dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->cmd ) ); |
|---|
| 181 | |
|---|
| 182 | // call directly driver command |
|---|
| 183 | cmd( XPTR( local_cxy , this ) ); |
|---|
| 184 | |
|---|
| 185 | // return I/O operation status from calling thread descriptor |
|---|
| 186 | return this->command.txt.error; |
|---|
| 187 | |
|---|
| 188 | } // end dev_txt_sync_write() |
|---|
| 189 | |
|---|