[5] | 1 | /* |
---|
| 2 | * chdev.c - channel device descriptor operations implementation. |
---|
| 3 | * |
---|
| 4 | * Authors 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 | |
---|
[14] | 24 | #include <kernel_config.h> |
---|
[5] | 25 | #include <hal_types.h> |
---|
| 26 | #include <hal_special.h> |
---|
[407] | 27 | #include <hal_irqmask.h> |
---|
[16] | 28 | #include <printk.h> |
---|
[5] | 29 | #include <boot_info.h> |
---|
| 30 | #include <xlist.h> |
---|
| 31 | #include <kmem.h> |
---|
[407] | 32 | #include <scheduler.h> |
---|
[5] | 33 | #include <thread.h> |
---|
| 34 | #include <rpc.h> |
---|
| 35 | #include <chdev.h> |
---|
[23] | 36 | #include <devfs.h> |
---|
[5] | 37 | |
---|
[317] | 38 | |
---|
| 39 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
---|
| 40 | |
---|
[407] | 41 | #if CONFIG_READ_DEBUG |
---|
| 42 | extern uint32_t enter_chdev_cmd; |
---|
| 43 | extern uint32_t exit_chdev_cmd; |
---|
| 44 | extern uint32_t enter_chdev_server; |
---|
| 45 | extern uint32_t exit_chdev_server; |
---|
| 46 | #endif |
---|
[317] | 47 | |
---|
[5] | 48 | //////////////////////////////////////////// |
---|
| 49 | char * chdev_func_str( uint32_t func_type ) |
---|
| 50 | { |
---|
| 51 | if ( func_type == DEV_FUNC_RAM ) return "RAM"; |
---|
| 52 | else if( func_type == DEV_FUNC_ROM ) return "ROM"; |
---|
| 53 | else if( func_type == DEV_FUNC_FBF ) return "FBF"; |
---|
| 54 | else if( func_type == DEV_FUNC_IOB ) return "IOB"; |
---|
| 55 | else if( func_type == DEV_FUNC_IOC ) return "IOC"; |
---|
| 56 | else if( func_type == DEV_FUNC_MMC ) return "MMC"; |
---|
| 57 | else if( func_type == DEV_FUNC_DMA ) return "DMA"; |
---|
| 58 | else if( func_type == DEV_FUNC_NIC ) return "NIC"; |
---|
| 59 | else if( func_type == DEV_FUNC_TIM ) return "TIM"; |
---|
| 60 | else if( func_type == DEV_FUNC_TXT ) return "TXT"; |
---|
| 61 | else if( func_type == DEV_FUNC_ICU ) return "ICU"; |
---|
| 62 | else if( func_type == DEV_FUNC_PIC ) return "PIC"; |
---|
[16] | 63 | else return "undefined"; |
---|
[5] | 64 | } |
---|
| 65 | |
---|
| 66 | ///////////////////////////////////////// |
---|
| 67 | chdev_t * chdev_create( uint32_t func, |
---|
| 68 | uint32_t impl, |
---|
| 69 | uint32_t channel, |
---|
| 70 | uint32_t is_rx, |
---|
| 71 | xptr_t base ) |
---|
| 72 | { |
---|
| 73 | chdev_t * chdev; |
---|
| 74 | kmem_req_t req; |
---|
| 75 | |
---|
| 76 | // allocate memory for chdev |
---|
| 77 | req.type = KMEM_DEVICE; |
---|
| 78 | req.flags = AF_ZERO; |
---|
| 79 | chdev = (chdev_t *)kmem_alloc( &req ); |
---|
| 80 | |
---|
| 81 | if( chdev == NULL ) return NULL; |
---|
| 82 | |
---|
| 83 | // initialize waiting threads queue and associated lock |
---|
| 84 | remote_spinlock_init( XPTR( local_cxy , &chdev->wait_lock ) ); |
---|
| 85 | xlist_root_init( XPTR( local_cxy , &chdev->wait_root ) ); |
---|
| 86 | |
---|
| 87 | // initialize attributes |
---|
| 88 | chdev->func = func; |
---|
| 89 | chdev->impl = impl; |
---|
| 90 | chdev->channel = channel; |
---|
| 91 | chdev->is_rx = is_rx; |
---|
| 92 | chdev->base = base; |
---|
| 93 | |
---|
| 94 | return chdev; |
---|
| 95 | |
---|
| 96 | } // end chdev_create() |
---|
| 97 | |
---|
| 98 | /////////////////////////////////// |
---|
| 99 | void chdev_print( chdev_t * chdev ) |
---|
| 100 | { |
---|
| 101 | printk("\n - func = %s" |
---|
| 102 | "\n - channel = %d" |
---|
| 103 | "\n - base = %l" |
---|
| 104 | "\n - cmd = %x" |
---|
| 105 | "\n - isr = %x" |
---|
| 106 | "\n - chdev = %x\n", |
---|
| 107 | chdev_func_str(chdev->func), |
---|
| 108 | chdev->channel, |
---|
| 109 | chdev->base, |
---|
| 110 | chdev->cmd, |
---|
| 111 | chdev->isr, |
---|
| 112 | chdev ); |
---|
| 113 | } |
---|
| 114 | |
---|
[407] | 115 | ////////////////////////////////////////////////// |
---|
| 116 | void chdev_register_command( xptr_t chdev_xp ) |
---|
[5] | 117 | { |
---|
[407] | 118 | thread_t * server_ptr; // local pointer on server thread associated to chdev |
---|
| 119 | core_t * core_ptr; // local pointer on core running the server thread |
---|
| 120 | uint32_t lid; // core running the server thread local index |
---|
| 121 | xptr_t lock_xp; // extended pointer on lock protecting the chdev queue |
---|
[408] | 122 | uint32_t different; // non zero if server thread core != client thread core |
---|
[407] | 123 | uint32_t save_sr; // for critical section |
---|
[5] | 124 | |
---|
[418] | 125 | #if CONFIG_READ_DEBUG |
---|
| 126 | enter_chdev_cmd = hal_time_stamp(); |
---|
| 127 | #endif |
---|
| 128 | |
---|
[407] | 129 | thread_t * this = CURRENT_THREAD; |
---|
| 130 | |
---|
| 131 | chdev_dmsg("\n[DBG] %s : core[%x,%d] (thread %s) enter / cycle %d\n", |
---|
| 132 | __FUNCTION__, local_cxy, this->core->lid, thread_type_str(this->type) , hal_time_stamp() ); |
---|
| 133 | |
---|
[5] | 134 | // get device descriptor cluster and local pointer |
---|
| 135 | cxy_t chdev_cxy = GET_CXY( chdev_xp ); |
---|
| 136 | chdev_t * chdev_ptr = (chdev_t *)GET_PTR( chdev_xp ); |
---|
| 137 | |
---|
| 138 | // build extended pointers on client thread xlist and device root |
---|
[407] | 139 | xptr_t list_xp = XPTR( local_cxy , &this->wait_list ); |
---|
| 140 | xptr_t root_xp = XPTR( chdev_cxy , &chdev_ptr->wait_root ); |
---|
[5] | 141 | |
---|
[407] | 142 | // get local pointer on server thread |
---|
| 143 | server_ptr = (thread_t *)hal_remote_lpt( XPTR( chdev_cxy , &chdev_ptr->server) ); |
---|
[5] | 144 | |
---|
[407] | 145 | chdev_dmsg("\n[DBG] %s : core[%x,%d] (thread %s) / server_cxy %x / server_ptr %x / server_type %\n", |
---|
| 146 | __FUNCTION__, local_cxy, this->core->lid, server_cxy, server_ptr, |
---|
| 147 | thread_type_str( hal_remote_lw( XPTR( server_cxy , &server_ptr->type) ) ) ); |
---|
| 148 | |
---|
| 149 | // build extended pointer on chdev lock protecting queue |
---|
| 150 | lock_xp = XPTR( chdev_cxy , &chdev_ptr->wait_lock ); |
---|
| 151 | |
---|
| 152 | // get local pointer on core running the server thread |
---|
| 153 | core_ptr = (core_t *)hal_remote_lpt( XPTR( chdev_cxy , &server_ptr->core ) ); |
---|
| 154 | |
---|
| 155 | // get core local index |
---|
| 156 | lid = hal_remote_lw( XPTR( chdev_cxy , &core_ptr->lid ) ); |
---|
| 157 | |
---|
[408] | 158 | // compute server core != thread core |
---|
| 159 | different = (lid != this->core->lid) || (local_cxy != chdev_cxy); |
---|
| 160 | |
---|
| 161 | // enter critical section to make atomic : |
---|
| 162 | // (1) client blocking |
---|
| 163 | // (2) client registration in server queue |
---|
| 164 | // (3) IPI to force server scheduling |
---|
| 165 | // (4) descheduling |
---|
| 166 | // ... in this order |
---|
[407] | 167 | hal_disable_irq( &save_sr ); |
---|
| 168 | |
---|
[408] | 169 | // block current thread |
---|
| 170 | thread_block( CURRENT_THREAD , THREAD_BLOCKED_IO ); |
---|
| 171 | |
---|
[5] | 172 | // register client thread in waiting queue |
---|
[407] | 173 | remote_spinlock_lock( lock_xp ); |
---|
| 174 | xlist_add_last( root_xp , list_xp ); |
---|
| 175 | remote_spinlock_unlock( lock_xp ); |
---|
[5] | 176 | |
---|
[408] | 177 | // send IPI to core running the server thread if required |
---|
| 178 | if( different ) dev_pic_send_ipi( chdev_cxy , lid ); |
---|
[407] | 179 | |
---|
| 180 | chdev_dmsg("\n[DBG] %s : core[%x,%d] (thread %s) deschedules / cycle %d\n", |
---|
| 181 | __FUNCTION__, local_cxy, this->core->lid, thread_type_str(this->type) , hal_time_stamp() ); |
---|
[5] | 182 | |
---|
[408] | 183 | // deschedule |
---|
| 184 | assert( thread_can_yield( this ) , __FUNCTION__ , "illegal sched_yield\n" ); |
---|
| 185 | sched_yield("blocked on I/O"); |
---|
[407] | 186 | |
---|
| 187 | chdev_dmsg("\n[DBG] %s : core[%x,%d] (thread %s) resumes / cycle %d\n", |
---|
| 188 | __FUNCTION__, local_cxy, this->core->lid, thread_type_str(this->type) , hal_time_stamp() ); |
---|
| 189 | |
---|
| 190 | // exit critical section |
---|
| 191 | hal_restore_irq( save_sr ); |
---|
| 192 | |
---|
[418] | 193 | #if CONFIG_READ_DEBUG |
---|
| 194 | exit_chdev_cmd = hal_time_stamp(); |
---|
| 195 | #endif |
---|
| 196 | |
---|
[5] | 197 | } // end chdev_register_command() |
---|
| 198 | |
---|
| 199 | /////////////////////////////////////////////// |
---|
| 200 | void chdev_sequencial_server( chdev_t * chdev ) |
---|
| 201 | { |
---|
| 202 | xptr_t client_xp; // extended pointer on waiting thread |
---|
| 203 | cxy_t client_cxy; // cluster of client thread |
---|
| 204 | thread_t * client_ptr; // local pointer on client thread |
---|
[407] | 205 | thread_t * server; // local pointer on server thread |
---|
[5] | 206 | xptr_t root_xp; // extended pointer on device waiting queue root |
---|
[407] | 207 | xptr_t lock_xp; // extended pointer on lock ptotecting chdev queue |
---|
[5] | 208 | |
---|
| 209 | server = CURRENT_THREAD; |
---|
| 210 | |
---|
[407] | 211 | chdev_dmsg("\n[DBG] %s : enter / server = %x / chdev = %x / cycle %d\n", |
---|
| 212 | __FUNCTION__ , server , chdev , hal_time_stamp() ); |
---|
| 213 | |
---|
[5] | 214 | root_xp = XPTR( local_cxy , &chdev->wait_root ); |
---|
[407] | 215 | lock_xp = XPTR( local_cxy , &chdev->wait_lock ); |
---|
[5] | 216 | |
---|
[407] | 217 | // This infinite loop is executed by the DEV thread |
---|
| 218 | // to handle commands registered in the chdev queue. |
---|
[5] | 219 | while( 1 ) |
---|
| 220 | { |
---|
[407] | 221 | // get the lock protecting the waiting queue |
---|
| 222 | remote_spinlock_lock( lock_xp ); |
---|
| 223 | |
---|
[5] | 224 | // check waiting queue state |
---|
[407] | 225 | if( xlist_is_empty( root_xp ) ) // waiting queue empty |
---|
[5] | 226 | { |
---|
| 227 | // release lock |
---|
[407] | 228 | remote_spinlock_unlock( lock_xp ); |
---|
[5] | 229 | |
---|
[407] | 230 | chdev_dmsg("\n[DBG] %s : thread %x deschedule /cycle %d\n", |
---|
| 231 | __FUNCTION__ , server , hal_time_stamp() ); |
---|
| 232 | |
---|
[408] | 233 | // deschedule |
---|
| 234 | sched_yield("I/O queue empty"); |
---|
[407] | 235 | |
---|
| 236 | chdev_dmsg("\n[DBG] %s : thread %x resume /cycle %d\n", |
---|
| 237 | __FUNCTION__ , server , hal_time_stamp() ); |
---|
| 238 | |
---|
[5] | 239 | } |
---|
[407] | 240 | else // waiting queue not empty |
---|
[5] | 241 | { |
---|
[407] | 242 | |
---|
| 243 | #if CONFIG_READ_DEBUG |
---|
| 244 | enter_chdev_server = hal_time_stamp(); |
---|
| 245 | #endif |
---|
[5] | 246 | // release lock |
---|
[407] | 247 | remote_spinlock_unlock( lock_xp ); |
---|
[5] | 248 | |
---|
[407] | 249 | // get extended pointer on first client thread |
---|
| 250 | client_xp = XLIST_FIRST_ELEMENT( root_xp , thread_t , wait_list ); |
---|
[5] | 251 | |
---|
[407] | 252 | // get client thread cluster, local pointer, and identifier |
---|
| 253 | client_cxy = GET_CXY( client_xp ); |
---|
| 254 | client_ptr = (thread_t *)GET_PTR( client_xp ); |
---|
| 255 | |
---|
| 256 | // call driver command function to execute I/O operation |
---|
| 257 | chdev->cmd( client_xp ); |
---|
[5] | 258 | |
---|
[407] | 259 | // remove the client thread from waiting queue |
---|
| 260 | remote_spinlock_lock( lock_xp ); |
---|
| 261 | xlist_unlink( XPTR( client_cxy , &client_ptr->wait_list ) ); |
---|
| 262 | remote_spinlock_unlock( lock_xp ); |
---|
[5] | 263 | |
---|
[418] | 264 | // unblock client thread |
---|
| 265 | thread_unblock( client_xp , THREAD_BLOCKED_IO ); |
---|
| 266 | |
---|
[407] | 267 | chdev_dmsg("\n[DBG] %s : thread %x complete operation for client %x / cycle %d\n", |
---|
| 268 | __FUNCTION__ , server , client_ptr , hal_time_stamp() ); |
---|
[5] | 269 | |
---|
[407] | 270 | #if CONFIG_READ_DEBUG |
---|
| 271 | exit_chdev_server = hal_time_stamp(); |
---|
| 272 | #endif |
---|
| 273 | |
---|
| 274 | } |
---|
[5] | 275 | } // end while |
---|
| 276 | } // end chdev_sequencial_server() |
---|
| 277 | |
---|
[317] | 278 | //////////////////////// |
---|
| 279 | void chdev_dir_display() |
---|
| 280 | { |
---|
| 281 | cxy_t iob_cxy = GET_CXY( chdev_dir.iob ); |
---|
| 282 | chdev_t * iob_ptr = (chdev_t *)GET_PTR( chdev_dir.iob ); |
---|
[407] | 283 | uint32_t iob_base = (uint32_t)hal_remote_lwd( XPTR( iob_cxy , &iob_ptr->base ) ); |
---|
[317] | 284 | |
---|
| 285 | cxy_t pic_cxy = GET_CXY( chdev_dir.pic ); |
---|
| 286 | chdev_t * pic_ptr = (chdev_t *)GET_PTR( chdev_dir.pic ); |
---|
[407] | 287 | uint32_t pic_base = (uint32_t)hal_remote_lwd( XPTR( pic_cxy , &pic_ptr->base ) ); |
---|
[317] | 288 | |
---|
[407] | 289 | cxy_t txt0_tx_cxy = GET_CXY( chdev_dir.txt_tx[0] ); |
---|
| 290 | chdev_t * txt0_tx_ptr = (chdev_t *)GET_PTR( chdev_dir.txt_tx[0] ); |
---|
| 291 | uint32_t txt0_tx_base = (uint32_t)hal_remote_lwd( XPTR( txt0_tx_cxy , &txt0_tx_ptr->base ) ); |
---|
[317] | 292 | |
---|
[407] | 293 | cxy_t txt0_rx_cxy = GET_CXY( chdev_dir.txt_rx[0] ); |
---|
| 294 | chdev_t * txt0_rx_ptr = (chdev_t *)GET_PTR( chdev_dir.txt_rx[0] ); |
---|
| 295 | uint32_t txt0_rx_base = (uint32_t)hal_remote_lwd( XPTR( txt0_rx_cxy , &txt0_rx_ptr->base ) ); |
---|
[317] | 296 | |
---|
[407] | 297 | cxy_t txt1_tx_cxy = GET_CXY( chdev_dir.txt_tx[1] ); |
---|
| 298 | chdev_t * txt1_tx_ptr = (chdev_t *)GET_PTR( chdev_dir.txt_tx[1] ); |
---|
| 299 | uint32_t txt1_tx_base = (uint32_t)hal_remote_lwd( XPTR( txt1_tx_cxy , &txt1_tx_ptr->base ) ); |
---|
[317] | 300 | |
---|
[407] | 301 | cxy_t txt1_rx_cxy = GET_CXY( chdev_dir.txt_rx[1] ); |
---|
| 302 | chdev_t * txt1_rx_ptr = (chdev_t *)GET_PTR( chdev_dir.txt_rx[1] ); |
---|
| 303 | uint32_t txt1_rx_base = (uint32_t)hal_remote_lwd( XPTR( txt1_rx_cxy , &txt1_rx_ptr->base ) ); |
---|
| 304 | |
---|
| 305 | cxy_t txt2_tx_cxy = GET_CXY( chdev_dir.txt_tx[2] ); |
---|
| 306 | chdev_t * txt2_tx_ptr = (chdev_t *)GET_PTR( chdev_dir.txt_tx[2] ); |
---|
| 307 | uint32_t txt2_tx_base = (uint32_t)hal_remote_lwd( XPTR( txt2_tx_cxy , &txt2_tx_ptr->base ) ); |
---|
| 308 | |
---|
| 309 | cxy_t txt2_rx_cxy = GET_CXY( chdev_dir.txt_rx[2] ); |
---|
| 310 | chdev_t * txt2_rx_ptr = (chdev_t *)GET_PTR( chdev_dir.txt_rx[2] ); |
---|
| 311 | uint32_t txt2_rx_base = (uint32_t)hal_remote_lwd( XPTR( txt2_rx_cxy , &txt2_rx_ptr->base ) ); |
---|
| 312 | |
---|
[317] | 313 | cxy_t ioc_cxy = GET_CXY( chdev_dir.ioc[0] ); |
---|
| 314 | chdev_t * ioc_ptr = (chdev_t *)GET_PTR( chdev_dir.ioc[0] ); |
---|
[407] | 315 | uint32_t ioc_base = (uint32_t)hal_remote_lwd( XPTR( ioc_cxy , &ioc_ptr->base ) ); |
---|
[317] | 316 | |
---|
| 317 | cxy_t fbf_cxy = GET_CXY( chdev_dir.fbf[0] ); |
---|
| 318 | chdev_t * fbf_ptr = (chdev_t *)GET_PTR( chdev_dir.fbf[0] ); |
---|
[407] | 319 | uint32_t fbf_base = (uint32_t)hal_remote_lwd( XPTR( fbf_cxy , &fbf_ptr->base ) ); |
---|
[317] | 320 | |
---|
[407] | 321 | cxy_t nic0_rx_cxy = GET_CXY( chdev_dir.nic_rx[0] ); |
---|
| 322 | chdev_t * nic0_rx_ptr = (chdev_t *)GET_PTR( chdev_dir.nic_rx[0] ); |
---|
| 323 | uint32_t nic0_rx_base = (uint32_t)hal_remote_lwd( XPTR( nic0_rx_cxy , &nic0_rx_ptr->base ) ); |
---|
[317] | 324 | |
---|
[407] | 325 | cxy_t nic0_tx_cxy = GET_CXY( chdev_dir.nic_tx[0] ); |
---|
| 326 | chdev_t * nic0_tx_ptr = (chdev_t *)GET_PTR( chdev_dir.nic_tx[0] ); |
---|
| 327 | uint32_t nic0_tx_base = (uint32_t)hal_remote_lwd( XPTR( nic0_tx_cxy , &nic0_tx_ptr->base ) ); |
---|
[317] | 328 | |
---|
| 329 | printk("\n***** external chdev directory in cluster %x\n" |
---|
[407] | 330 | " - iob : cxy = %X / ptr = %X / base = %X\n" |
---|
| 331 | " - pic : cxy = %X / ptr = %X / base = %X\n" |
---|
| 332 | " - ioc : cxy = %X / ptr = %X / base = %X\n" |
---|
| 333 | " - fbf : cxy = %X / ptr = %X / base = %X\n" |
---|
| 334 | " - txt_rx[0] : cxy = %X / ptr = %X / base = %X\n" |
---|
| 335 | " - txt_tx[0] : cxy = %X / ptr = %X / base = %X\n" |
---|
| 336 | " - txt_rx[1] : cxy = %X / ptr = %X / base = %X\n" |
---|
| 337 | " - txt_tx[1] : cxy = %X / ptr = %X / base = %X\n" |
---|
| 338 | " - txt_rx[2] : cxy = %X / ptr = %X / base = %X\n" |
---|
| 339 | " - txt_tx[2] : cxy = %X / ptr = %X / base = %X\n" |
---|
| 340 | " - nic_rx[0] : cxy = %X / ptr = %X / base = %X\n" |
---|
| 341 | " - nic_tx[0] : cxy = %X / ptr = %X / base = %X\n", |
---|
[317] | 342 | local_cxy, |
---|
[407] | 343 | iob_cxy , iob_ptr , iob_base , |
---|
| 344 | pic_cxy , pic_ptr , pic_base , |
---|
| 345 | ioc_cxy , ioc_ptr , ioc_base , |
---|
| 346 | fbf_cxy , fbf_ptr , fbf_base , |
---|
| 347 | txt0_rx_cxy , txt0_rx_ptr , txt0_rx_base , |
---|
| 348 | txt0_tx_cxy , txt0_tx_ptr , txt0_tx_base , |
---|
| 349 | txt1_rx_cxy , txt1_rx_ptr , txt1_rx_base , |
---|
| 350 | txt1_tx_cxy , txt1_tx_ptr , txt1_tx_base , |
---|
| 351 | txt2_rx_cxy , txt2_rx_ptr , txt2_rx_base , |
---|
| 352 | txt2_tx_cxy , txt2_tx_ptr , txt2_tx_base , |
---|
| 353 | nic0_rx_cxy , nic0_rx_ptr , nic0_rx_base , |
---|
| 354 | nic0_tx_cxy , nic0_tx_ptr , nic0_tx_base ); |
---|
[317] | 355 | |
---|
| 356 | } // end chdev_dir_display() |
---|
| 357 | |
---|