| 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 <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 | ////////////////////////////////// |
|---|
| 40 | void dev_txt_init( chdev_t * txt ) |
|---|
| 41 | { |
|---|
| 42 | // For all TXT channels other than the TXT0 (kernel terminal), |
|---|
| 43 | // the PIC chdev must be initialized before the TXT chdev, because |
|---|
| 44 | // the TXT chdev initialization requires the routing of an external IRQ |
|---|
| 45 | |
|---|
| 46 | xptr_t pic_xp = chdev_dir.pic; |
|---|
| 47 | uint32_t channel = txt->channel; |
|---|
| 48 | uint32_t impl = txt->impl; |
|---|
| 49 | |
|---|
| 50 | assert( (pic_xp != XPTR_NULL) || (channel == 0) , __FUNCTION__ , |
|---|
| 51 | "PIC not initialised before TXT" ); |
|---|
| 52 | |
|---|
| 53 | // set chdev name |
|---|
| 54 | snprintf( txt->name , 16 , "txt_%d" , channel ); |
|---|
| 55 | |
|---|
| 56 | // call driver init function |
|---|
| 57 | hal_drivers_txt_init(txt, impl); |
|---|
| 58 | |
|---|
| 59 | // no server thread and no IRQ routing for TXT0 and IMPL_TXT_RS2 (multiplexed |
|---|
| 60 | // in software, not hardware) |
|---|
| 61 | if( channel != 0 && impl != IMPL_TXT_RS2 ) |
|---|
| 62 | { |
|---|
| 63 | // select a core to execute the TXT server thread |
|---|
| 64 | lid_t lid = cluster_select_local_core(); |
|---|
| 65 | |
|---|
| 66 | // bind TXT IRQ to the selected core |
|---|
| 67 | dev_pic_bind_irq( lid , txt ); |
|---|
| 68 | |
|---|
| 69 | // enable TXT IRQ |
|---|
| 70 | dev_pic_enable_irq( lid , XPTR( local_cxy , txt ) ); |
|---|
| 71 | |
|---|
| 72 | // create server thread |
|---|
| 73 | thread_t * new_thread; |
|---|
| 74 | error_t error; |
|---|
| 75 | |
|---|
| 76 | error = thread_kernel_create( &new_thread, |
|---|
| 77 | THREAD_DEV, |
|---|
| 78 | &chdev_sequencial_server, |
|---|
| 79 | txt, |
|---|
| 80 | lid ); |
|---|
| 81 | |
|---|
| 82 | assert( (error == 0) , __FUNCTION__ , "cannot create server thread" ); |
|---|
| 83 | |
|---|
| 84 | // set "server" field in chdev descriptor |
|---|
| 85 | txt->server = new_thread; |
|---|
| 86 | |
|---|
| 87 | // start server thread |
|---|
| 88 | thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL ); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 93 | // This static function is called by dev_txt_read(), dev_txt_write() functions. |
|---|
| 94 | ////////////////////////////////////i///////////////////////////////////////////// |
|---|
| 95 | static error_t dev_txt_access( uint32_t type, |
|---|
| 96 | uint32_t channel, |
|---|
| 97 | char * buffer, |
|---|
| 98 | uint32_t count ) |
|---|
| 99 | { |
|---|
| 100 | thread_t * this = CURRENT_THREAD; |
|---|
| 101 | |
|---|
| 102 | txt_dmsg("\n[INFO] in %s : thread %x in process %x enters\n", |
|---|
| 103 | __FUNCTION__ , this->trdid , this->process->pid ); |
|---|
| 104 | |
|---|
| 105 | // check channel argument |
|---|
| 106 | assert( (channel < CONFIG_MAX_TXT_CHANNELS) , __FUNCTION__ , "illegal channel index" ); |
|---|
| 107 | |
|---|
| 108 | // get extended pointer on remote TXT chdev descriptor |
|---|
| 109 | xptr_t dev_xp = chdev_dir.txt[channel]; |
|---|
| 110 | |
|---|
| 111 | assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined TXT chdev descriptor" ); |
|---|
| 112 | |
|---|
| 113 | // register command in calling thread descriptor |
|---|
| 114 | this->txt_cmd.dev_xp = dev_xp; |
|---|
| 115 | this->txt_cmd.type = type; |
|---|
| 116 | this->txt_cmd.buf_xp = XPTR( local_cxy , buffer ); |
|---|
| 117 | this->txt_cmd.count = count; |
|---|
| 118 | |
|---|
| 119 | // register client thread in waiting queue, activate server thread |
|---|
| 120 | // block client thread on THREAD_BLOCKED_IO and deschedule. |
|---|
| 121 | // it is re-activated by the ISR signaling IO operation completion. |
|---|
| 122 | chdev_register_command( dev_xp , this ); |
|---|
| 123 | |
|---|
| 124 | txt_dmsg("\n[INFO] in %s : thread %x in process %x completes / error = %d\n", |
|---|
| 125 | __FUNCTION__ , this->trdid , this->process->pid , this->txt_cmd.error ); |
|---|
| 126 | |
|---|
| 127 | // return I/O operation status from calling thread descriptor |
|---|
| 128 | return this->txt_cmd.error; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | ///////////////////////////////////////// |
|---|
| 132 | error_t dev_txt_write( uint32_t channel, |
|---|
| 133 | char * buffer, |
|---|
| 134 | uint32_t count ) |
|---|
| 135 | { |
|---|
| 136 | return dev_txt_access( TXT_WRITE , channel , buffer , count ); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | ///////////////////////////////////////// |
|---|
| 140 | error_t dev_txt_read( uint32_t channel, |
|---|
| 141 | char * buffer ) |
|---|
| 142 | { |
|---|
| 143 | return dev_txt_access( TXT_READ , channel , buffer , 1 ); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | /////////////////////////////////////////////// |
|---|
| 147 | error_t dev_txt_sync_write( uint32_t channel, |
|---|
| 148 | char * buffer, |
|---|
| 149 | uint32_t count ) |
|---|
| 150 | { |
|---|
| 151 | // get pointer on calling thread |
|---|
| 152 | thread_t * this = CURRENT_THREAD; |
|---|
| 153 | |
|---|
| 154 | // get extended pointer on TXT[0] chdev |
|---|
| 155 | xptr_t dev_xp = chdev_dir.txt[channel]; |
|---|
| 156 | |
|---|
| 157 | assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined TXT0 chdev descriptor" ); |
|---|
| 158 | |
|---|
| 159 | // register command in calling thread descriptor |
|---|
| 160 | this->txt_cmd.dev_xp = dev_xp; |
|---|
| 161 | this->txt_cmd.type = TXT_SYNC_WRITE; |
|---|
| 162 | this->txt_cmd.buf_xp = XPTR( local_cxy , buffer ); |
|---|
| 163 | this->txt_cmd.count = count; |
|---|
| 164 | |
|---|
| 165 | // get driver command function |
|---|
| 166 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
|---|
| 167 | chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); |
|---|
| 168 | dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->cmd ) ); |
|---|
| 169 | |
|---|
| 170 | // call driver function |
|---|
| 171 | cmd( XPTR( local_cxy , this ) ); |
|---|
| 172 | |
|---|
| 173 | // return I/O operation status from calling thread descriptor |
|---|
| 174 | return this->txt_cmd.error; |
|---|
| 175 | } |
|---|
| 176 | |
|---|