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 <chdev.h> |
---|
30 | #include <rpc.h> |
---|
31 | #include <printk.h> |
---|
32 | #include <dev_txt.h> |
---|
33 | |
---|
34 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
35 | // Extern global variables |
---|
36 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
37 | |
---|
38 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
---|
39 | |
---|
40 | #if (DEBUG_SYS_READ & 1) |
---|
41 | extern uint32_t enter_txt_read; |
---|
42 | extern uint32_t exit_txt_read; |
---|
43 | #endif |
---|
44 | |
---|
45 | #if (DEBUG_SYS_WRITE & 1) |
---|
46 | extern uint32_t enter_txt_write; |
---|
47 | extern uint32_t exit_txt_write; |
---|
48 | #endif |
---|
49 | |
---|
50 | //////////////////////////////////////// |
---|
51 | char * dev_txt_type_str( uint32_t type ) |
---|
52 | { |
---|
53 | if ( type == TXT_SYNC_WRITE ) return "TXT_SYNC_WRITE"; |
---|
54 | else if( type == TXT_READ ) return "TXT_READ"; |
---|
55 | else if( type == TXT_WRITE ) return "TXT_WRITE"; |
---|
56 | else return "undefined"; |
---|
57 | } |
---|
58 | |
---|
59 | ////////////////////////////////// |
---|
60 | void dev_txt_init( chdev_t * txt ) |
---|
61 | { |
---|
62 | // For all TXT channels other than the TXT0 (kernel terminal), |
---|
63 | // the PIC chdev must be initialized before the TXT chdev, because |
---|
64 | // the TXT chdev initialization requires the routing of an external IRQ |
---|
65 | |
---|
66 | xptr_t pic_xp = chdev_dir.pic; |
---|
67 | uint32_t channel = txt->channel; |
---|
68 | uint32_t impl = txt->impl; |
---|
69 | bool_t is_rx = txt->is_rx; |
---|
70 | |
---|
71 | assert( (pic_xp != XPTR_NULL) || (channel == 0) , __FUNCTION__ , |
---|
72 | "PIC not initialised before TXT" ); |
---|
73 | |
---|
74 | // set chdev name |
---|
75 | if( is_rx ) snprintf( txt->name , 16 , "txt%d_rx" , channel ); |
---|
76 | else snprintf( txt->name , 16 , "txt%d_tx" , channel ); |
---|
77 | |
---|
78 | // set TXT chdev extension |
---|
79 | txt->ext.txt.owner_xp = XPTR_NULL; |
---|
80 | xlist_root_init( XPTR( local_cxy, &txt->ext.txt.root ) ); |
---|
81 | remote_spinlock_init( XPTR( local_cxy , &txt->ext.txt.lock ) ); |
---|
82 | |
---|
83 | // call driver init function |
---|
84 | hal_drivers_txt_init(txt, impl); |
---|
85 | |
---|
86 | // no server thread and no IRQ routing for TXT0 |
---|
87 | // no server thread for IMPL_TXT_RS2 (multiplexed in software, not hardware) |
---|
88 | if( channel != 0 && impl != IMPL_TXT_RS2 ) |
---|
89 | { |
---|
90 | // select a core to execute the server thread |
---|
91 | lid_t lid = cluster_select_local_core(); |
---|
92 | |
---|
93 | // bind IRQ to the selected core |
---|
94 | dev_pic_bind_irq( lid , txt ); |
---|
95 | |
---|
96 | // enable IRQ |
---|
97 | dev_pic_enable_irq( lid , XPTR( local_cxy , txt ) ); |
---|
98 | |
---|
99 | // create server thread |
---|
100 | thread_t * new_thread; |
---|
101 | error_t error; |
---|
102 | |
---|
103 | error = thread_kernel_create( &new_thread, |
---|
104 | THREAD_DEV, |
---|
105 | &chdev_sequencial_server, |
---|
106 | txt, |
---|
107 | lid ); |
---|
108 | |
---|
109 | assert( (error == 0) , __FUNCTION__ , "cannot create server thread" ); |
---|
110 | |
---|
111 | // set "server" field in chdev descriptor |
---|
112 | txt->server = new_thread; |
---|
113 | |
---|
114 | // set "chdev" field in thread descriptor |
---|
115 | new_thread->chdev = txt; |
---|
116 | |
---|
117 | // unblock server thread |
---|
118 | thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL ); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | ////////////////////////////////////////////////////////////////////////////////// |
---|
123 | // This static function is called by dev_txt_read(), dev_txt_write() functions. |
---|
124 | ////////////////////////////////////i///////////////////////////////////////////// |
---|
125 | static error_t dev_txt_access( uint32_t type, |
---|
126 | uint32_t channel, |
---|
127 | char * buffer, |
---|
128 | uint32_t count ) |
---|
129 | { |
---|
130 | xptr_t dev_xp; |
---|
131 | thread_t * this = CURRENT_THREAD; |
---|
132 | |
---|
133 | // check channel argument |
---|
134 | assert( (channel < CONFIG_MAX_TXT_CHANNELS) , __FUNCTION__ , "illegal channel index" ); |
---|
135 | |
---|
136 | // get extended pointer on remote TXT chdev descriptor |
---|
137 | if( type == TXT_WRITE ) dev_xp = chdev_dir.txt_tx[channel]; |
---|
138 | else dev_xp = chdev_dir.txt_rx[channel]; |
---|
139 | |
---|
140 | assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined TXT chdev descriptor" ); |
---|
141 | |
---|
142 | // register command in calling thread descriptor |
---|
143 | this->txt_cmd.dev_xp = dev_xp; |
---|
144 | this->txt_cmd.type = type; |
---|
145 | this->txt_cmd.buf_xp = XPTR( local_cxy , buffer ); |
---|
146 | this->txt_cmd.count = count; |
---|
147 | |
---|
148 | // register client thread in waiting queue, activate server thread |
---|
149 | // block client thread on THREAD_BLOCKED_IO and deschedule. |
---|
150 | // it is re-activated by the ISR signaling IO operation completion. |
---|
151 | chdev_register_command( dev_xp ); |
---|
152 | |
---|
153 | // return I/O operation status from calling thread descriptor |
---|
154 | return this->txt_cmd.error; |
---|
155 | } |
---|
156 | |
---|
157 | ///////////////////////////////////////// |
---|
158 | error_t dev_txt_write( uint32_t channel, |
---|
159 | char * buffer, |
---|
160 | uint32_t count ) |
---|
161 | { |
---|
162 | |
---|
163 | #if (DEBUG_SYS_WRITE & 1) |
---|
164 | enter_txt_write = hal_time_stamp(); |
---|
165 | #endif |
---|
166 | |
---|
167 | #if DEBUG_DEV_TXT_TX |
---|
168 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
169 | if( DEBUG_DEV_TXT_TX < cycle ) |
---|
170 | printk("\n[DBG] %s : thread %x enters / cycle %d\n", __FUNCTION__, CURRENT_THREAD, cycle ); |
---|
171 | #endif |
---|
172 | |
---|
173 | return dev_txt_access( TXT_WRITE , channel , buffer , count ); |
---|
174 | |
---|
175 | #if DEBUG_DEV_TXT_TX |
---|
176 | cycle = (uint32_t)hal_get_cycles(); |
---|
177 | if( DEBUG_DEV_TXT_TX < cycle ) |
---|
178 | printk("\n[DBG] %s : thread %x exit / cycle %d\n", __FUNCTION__, CURRENT_THREAD, cycle ); |
---|
179 | #endif |
---|
180 | |
---|
181 | #if (DEBUG_SYS_WRITE & 1) |
---|
182 | exit_txt_write = hal_time_stamp(); |
---|
183 | #endif |
---|
184 | |
---|
185 | } |
---|
186 | |
---|
187 | ///////////////////////////////////////// |
---|
188 | error_t dev_txt_read( uint32_t channel, |
---|
189 | char * buffer ) |
---|
190 | { |
---|
191 | |
---|
192 | #if (DEBUG_SYS_READ & 1) |
---|
193 | enter_txt_read = hal_time_stamp(); |
---|
194 | #endif |
---|
195 | |
---|
196 | #if DEBUG_DEV_TXT_RX |
---|
197 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
198 | if( DEBUG_DEV_TXT_RX < cycle ) |
---|
199 | printk("\n[DBG] %s : thread %x enters / cycle %d\n", __FUNCTION__, CURRENT_THREAD, cycle ); |
---|
200 | #endif |
---|
201 | |
---|
202 | return dev_txt_access( TXT_READ , channel , buffer , 1 ); |
---|
203 | |
---|
204 | #if DEBUG_DEV_TXT_RX |
---|
205 | cycle = (uint32_t)hal_get_cycles(); |
---|
206 | if( DEBUG_DEV_TXT_RX < cycle ) |
---|
207 | printk("\n[DBG] %s : thread %x exit / cycle %d\n", __FUNCTION__, CURRENT_THREAD, cycle ); |
---|
208 | #endif |
---|
209 | |
---|
210 | #if (DEBUG_SYS_READ & 1) |
---|
211 | exit_txt_read = hal_time_stamp(); |
---|
212 | #endif |
---|
213 | |
---|
214 | } |
---|
215 | |
---|
216 | ////////////////////////////////////////////// |
---|
217 | error_t dev_txt_sync_write( char * buffer, |
---|
218 | uint32_t count ) |
---|
219 | { |
---|
220 | // get extended pointer on TXT[0] chdev |
---|
221 | xptr_t dev_xp = chdev_dir.txt_tx[0]; |
---|
222 | |
---|
223 | assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , |
---|
224 | "undefined TXT0 chdev descriptor" ); |
---|
225 | |
---|
226 | // get TXTO chdev cluster and local pointer |
---|
227 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
---|
228 | chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); |
---|
229 | |
---|
230 | // get driver command function |
---|
231 | dev_aux_t * aux = (dev_aux_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->aux ) ); |
---|
232 | |
---|
233 | // build arguments structure |
---|
234 | txt_sync_args_t args; |
---|
235 | args.dev_xp = dev_xp; |
---|
236 | args.buffer = buffer; |
---|
237 | args.count = count; |
---|
238 | |
---|
239 | // call driver function |
---|
240 | aux( &args ); |
---|
241 | |
---|
242 | return 0; |
---|
243 | } |
---|
244 | |
---|