| 1 | /* |
|---|
| 2 | * dev_ioc.c - IOC (Block Device Controler) generic device API implementation. |
|---|
| 3 | * |
|---|
| 4 | * Author Alain Greiner (2016,2017,2018,2019) |
|---|
| 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-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 <kernel_config.h> |
|---|
| 25 | #include <hal_kernel_types.h> |
|---|
| 26 | #include <hal_gpt.h> |
|---|
| 27 | #include <hal_drivers.h> |
|---|
| 28 | #include <thread.h> |
|---|
| 29 | #include <printk.h> |
|---|
| 30 | #include <chdev.h> |
|---|
| 31 | #include <dev_ioc.h> |
|---|
| 32 | |
|---|
| 33 | ///////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 34 | // Extern global variables |
|---|
| 35 | ///////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 36 | |
|---|
| 37 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
|---|
| 38 | |
|---|
| 39 | //////////////////////////////////////// |
|---|
| 40 | char * dev_ioc_cmd_str( cmd_type_t cmd ) |
|---|
| 41 | { |
|---|
| 42 | if ( cmd == IOC_READ ) return "READ"; |
|---|
| 43 | else if( cmd == IOC_WRITE ) return "WRITE"; |
|---|
| 44 | else if( cmd == IOC_SYNC_READ ) return "SYNC_READ"; |
|---|
| 45 | else if( cmd == IOC_SYNC_WRITE ) return "SYNC_WRITE"; |
|---|
| 46 | else return "undefined"; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | ////////////////////////////////// |
|---|
| 50 | void dev_ioc_init( chdev_t * ioc ) |
|---|
| 51 | { |
|---|
| 52 | // the PIC chdev must be initialized before the IOC chdev, because |
|---|
| 53 | // the IOC chdev initialisation requires the routing of an external IRQ |
|---|
| 54 | xptr_t pic_xp = chdev_dir.pic; |
|---|
| 55 | |
|---|
| 56 | assert( (pic_xp != XPTR_NULL) , "PIC not initialised before IOC" ); |
|---|
| 57 | |
|---|
| 58 | // get implementation and channel from chdev descriptor |
|---|
| 59 | uint32_t impl = ioc->impl; |
|---|
| 60 | uint32_t channel = ioc->channel; |
|---|
| 61 | |
|---|
| 62 | // set chdev name |
|---|
| 63 | snprintf( ioc->name , 16 , "ioc%d" , channel ); |
|---|
| 64 | |
|---|
| 65 | // call driver init function |
|---|
| 66 | hal_drivers_ioc_init( ioc, impl ); |
|---|
| 67 | |
|---|
| 68 | // select a core to execute the IOC server thread |
|---|
| 69 | lid_t lid = cluster_select_local_core(); |
|---|
| 70 | |
|---|
| 71 | // bind the IOC IRQ to the selected core |
|---|
| 72 | dev_pic_bind_irq( lid , ioc ); |
|---|
| 73 | |
|---|
| 74 | // enable IRQ |
|---|
| 75 | dev_pic_enable_irq( lid , XPTR( local_cxy , ioc ) ); |
|---|
| 76 | |
|---|
| 77 | // create server thread |
|---|
| 78 | thread_t * new_thread; |
|---|
| 79 | error_t error; |
|---|
| 80 | |
|---|
| 81 | error = thread_kernel_create( &new_thread, |
|---|
| 82 | THREAD_DEV, |
|---|
| 83 | &chdev_server_func, |
|---|
| 84 | ioc, |
|---|
| 85 | lid ); |
|---|
| 86 | |
|---|
| 87 | assert( (error == 0) , "cannot create server thread" ); |
|---|
| 88 | |
|---|
| 89 | // set "server" field in chdev descriptor |
|---|
| 90 | ioc->server = new_thread; |
|---|
| 91 | |
|---|
| 92 | // set "chdev field in thread descriptor |
|---|
| 93 | new_thread->chdev = ioc; |
|---|
| 94 | |
|---|
| 95 | // unblock server thread |
|---|
| 96 | thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL ); |
|---|
| 97 | |
|---|
| 98 | } // end dev_ioc_init() |
|---|
| 99 | |
|---|
| 100 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 101 | // This static function is called by dev_ioc_read() & dev_ioc_write() functions. |
|---|
| 102 | // It builds and registers the command in the calling thread descriptor. |
|---|
| 103 | // Then, it registers the calling thead in IOC chdev waiting queue. |
|---|
| 104 | // Finally it blocks on the THREAD_BLOCKED_IO condition and deschedule. |
|---|
| 105 | ////////////////////////////////////i///////////////////////////////////////////// |
|---|
| 106 | static error_t dev_ioc_access( uint32_t cmd_type, |
|---|
| 107 | uint8_t * buffer, |
|---|
| 108 | uint32_t lba, |
|---|
| 109 | uint32_t count ) |
|---|
| 110 | { |
|---|
| 111 | thread_t * this = CURRENT_THREAD; // pointer on client thread |
|---|
| 112 | |
|---|
| 113 | #if ( DEV_IOC_RX || DEV_IOC_TX ) |
|---|
| 114 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 115 | #endif |
|---|
| 116 | |
|---|
| 117 | // software L2/L3 cache coherence for memory buffer |
|---|
| 118 | if( chdev_dir.iob ) |
|---|
| 119 | { |
|---|
| 120 | if (cmd_type == IOC_READ) dev_mmc_inval( XPTR( local_cxy , buffer ) , count<<9 ); |
|---|
| 121 | else dev_mmc_sync ( XPTR( local_cxy , buffer ) , count<<9 ); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | // get extended pointer on IOC chdev descriptor |
|---|
| 125 | xptr_t dev_xp = chdev_dir.ioc[0]; |
|---|
| 126 | |
|---|
| 127 | // check dev_xp |
|---|
| 128 | assert( (dev_xp != XPTR_NULL) , "undefined IOC chdev descriptor" ); |
|---|
| 129 | |
|---|
| 130 | // register command in calling thread descriptor |
|---|
| 131 | this->ioc_cmd.dev_xp = dev_xp; |
|---|
| 132 | this->ioc_cmd.type = cmd_type; |
|---|
| 133 | this->ioc_cmd.buf_xp = XPTR( local_cxy , buffer ); |
|---|
| 134 | this->ioc_cmd.lba = lba; |
|---|
| 135 | this->ioc_cmd.count = count; |
|---|
| 136 | |
|---|
| 137 | // register client thread in IOC chdev waiting queue, activate server thread, |
|---|
| 138 | // block client thread on THREAD_BLOCKED_IO and deschedule. |
|---|
| 139 | // it is re-activated by the ISR signaling IO operation completion. |
|---|
| 140 | chdev_register_command( dev_xp ); |
|---|
| 141 | |
|---|
| 142 | #if(DEV_IOC_RX & 1) |
|---|
| 143 | if( (DEV_IOC_RX < cycle) && (cmd_type != IOC_WRITE) ) |
|---|
| 144 | printk("\n[%s] thread[%x,%x] resumes for RX\n", |
|---|
| 145 | __FUNCTION__, this->process->pid , this->trdid ) |
|---|
| 146 | #endif |
|---|
| 147 | |
|---|
| 148 | #if(DEV_IOC_TX & 1) |
|---|
| 149 | if( (DEV_IOC_RX < cycle) && (cmd_type == IOC_WRITE) ) |
|---|
| 150 | printk("\n[%s] thread[%x,%x] resumes for TX\n", |
|---|
| 151 | __FUNCTION__, this->process->pid , this->trdid ) |
|---|
| 152 | #endif |
|---|
| 153 | |
|---|
| 154 | // return I/O operation status |
|---|
| 155 | return this->ioc_cmd.error; |
|---|
| 156 | |
|---|
| 157 | } // end dev_ioc_access() |
|---|
| 158 | |
|---|
| 159 | //////////////////////////////////////////// |
|---|
| 160 | error_t dev_ioc_read( uint8_t * buffer, |
|---|
| 161 | uint32_t lba, |
|---|
| 162 | uint32_t count ) |
|---|
| 163 | { |
|---|
| 164 | |
|---|
| 165 | #if DEBUG_DEV_IOC_RX |
|---|
| 166 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 167 | thread_t * this = CURRENT_THREAD; |
|---|
| 168 | if( DEBUG_DEV_IOC_RX < cycle ) |
|---|
| 169 | printk("\n[%s] thread[%x,%x] enters / lba %x / buffer %x / cycle %d\n", |
|---|
| 170 | __FUNCTION__ , this->process->pid, this->trdid, lba, buffer, cycle ); |
|---|
| 171 | #endif |
|---|
| 172 | |
|---|
| 173 | return dev_ioc_access( IOC_READ , buffer , lba , count ); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | //////////////////////////////////////////// |
|---|
| 177 | error_t dev_ioc_write( uint8_t * buffer, |
|---|
| 178 | uint32_t lba, |
|---|
| 179 | uint32_t count ) |
|---|
| 180 | { |
|---|
| 181 | |
|---|
| 182 | #if DEBUG_DEV_IOC_TX |
|---|
| 183 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 184 | thread_t * this = CURRENT_THREAD; |
|---|
| 185 | if( DEBUG_DEV_IOC_TX < cycle ) |
|---|
| 186 | printk("\n[%s] thread[%x,%x] enters / lba %x / buffer %x / cycle %d\n", |
|---|
| 187 | __FUNCTION__ , this->process->pid, this->trdid, lba, buffer, cycle ); |
|---|
| 188 | #endif |
|---|
| 189 | |
|---|
| 190 | return dev_ioc_access( IOC_WRITE , buffer , lba , count ); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 198 | // This static function is called by dev_ioc_sync_read() & dev_ioc_sync_write(). |
|---|
| 199 | // It builds and registers the command in the calling thread descriptor, and |
|---|
| 200 | // calls directly the blocking IOC driver command, that returns only when the |
|---|
| 201 | // IO operation is completed. |
|---|
| 202 | ////////////////////////////////////i///////////////////////////////////////////// |
|---|
| 203 | error_t dev_ioc_sync_access( uint32_t cmd_type, |
|---|
| 204 | xptr_t buffer_xp, |
|---|
| 205 | uint32_t lba, |
|---|
| 206 | uint32_t count ) |
|---|
| 207 | { |
|---|
| 208 | // get pointer on calling thread |
|---|
| 209 | thread_t * this = CURRENT_THREAD; |
|---|
| 210 | |
|---|
| 211 | // software L2/L3 cache coherence for memory buffer |
|---|
| 212 | if( chdev_dir.iob ) |
|---|
| 213 | { |
|---|
| 214 | if (cmd_type == IOC_SYNC_READ) dev_mmc_inval( buffer_xp , count<<9 ); |
|---|
| 215 | else dev_mmc_sync ( buffer_xp , count<<9 ); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | // get extended pointer on IOC[0] chdev |
|---|
| 219 | xptr_t ioc_xp = chdev_dir.ioc[0]; |
|---|
| 220 | |
|---|
| 221 | // check ioc_xp |
|---|
| 222 | assert( (ioc_xp != XPTR_NULL) , "undefined IOC chdev descriptor" ); |
|---|
| 223 | |
|---|
| 224 | // register command in calling thread descriptor |
|---|
| 225 | this->ioc_cmd.dev_xp = ioc_xp; |
|---|
| 226 | this->ioc_cmd.type = cmd_type; |
|---|
| 227 | this->ioc_cmd.buf_xp = buffer_xp; |
|---|
| 228 | this->ioc_cmd.lba = lba; |
|---|
| 229 | this->ioc_cmd.count = count; |
|---|
| 230 | |
|---|
| 231 | // get driver command function |
|---|
| 232 | cxy_t ioc_cxy = GET_CXY( ioc_xp ); |
|---|
| 233 | chdev_t * ioc_ptr = GET_PTR( ioc_xp ); |
|---|
| 234 | dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( ioc_cxy , &ioc_ptr->cmd ) ); |
|---|
| 235 | |
|---|
| 236 | // get core local index for the core handling the IOC IRQ |
|---|
| 237 | thread_t * server = (thread_t *)hal_remote_lpt( XPTR( ioc_cxy , &ioc_ptr->server ) ); |
|---|
| 238 | core_t * core = (core_t *)hal_remote_lpt( XPTR( ioc_cxy , &server->core ) ); |
|---|
| 239 | lid_t lid = (lid_t)hal_remote_l32( XPTR( ioc_cxy , &core->lid ) ); |
|---|
| 240 | |
|---|
| 241 | // mask the IRQ |
|---|
| 242 | dev_pic_disable_irq( lid , ioc_xp ); |
|---|
| 243 | |
|---|
| 244 | // call driver function |
|---|
| 245 | cmd( XPTR( local_cxy , this ) ); |
|---|
| 246 | |
|---|
| 247 | // unmask the IRQ |
|---|
| 248 | dev_pic_enable_irq( lid , ioc_xp ); |
|---|
| 249 | |
|---|
| 250 | // return I/O operation status from calling thread descriptor |
|---|
| 251 | return this->ioc_cmd.error; |
|---|
| 252 | |
|---|
| 253 | } // end dev_ioc_sync_access() |
|---|
| 254 | |
|---|
| 255 | //////////////////////////////////////////////// |
|---|
| 256 | error_t dev_ioc_sync_read( xptr_t buffer_xp, |
|---|
| 257 | uint32_t lba, |
|---|
| 258 | uint32_t count ) |
|---|
| 259 | { |
|---|
| 260 | |
|---|
| 261 | #if DEBUG_DEV_IOC_RX |
|---|
| 262 | thread_t * this = CURRENT_THREAD; |
|---|
| 263 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 264 | if( DEBUG_DEV_IOC_RX < cycle ) |
|---|
| 265 | printk("\n[%s] thread[%x,%x] : lba %x / buffer(%x,%x) / count %d / cycle %d\n", |
|---|
| 266 | __FUNCTION__ , this->process->pid, this->trdid, |
|---|
| 267 | lba, GET_CXY(buffer_xp), GET_PTR(buffer_xp), count, cycle ); |
|---|
| 268 | #endif |
|---|
| 269 | |
|---|
| 270 | return dev_ioc_sync_access( IOC_SYNC_READ , buffer_xp , lba , count ); |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | ///////////////////////////////////////////////// |
|---|
| 274 | error_t dev_ioc_sync_write( xptr_t buffer_xp, |
|---|
| 275 | uint32_t lba, |
|---|
| 276 | uint32_t count ) |
|---|
| 277 | { |
|---|
| 278 | |
|---|
| 279 | #if DEBUG_DEV_IOC_TX |
|---|
| 280 | thread_t * this = CURRENT_THREAD; |
|---|
| 281 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 282 | if( DEBUG_DEV_IOC_TX < cycle ) |
|---|
| 283 | printk("\n[%s] thread[%x,%x] : lba %x / buffer(%x,%x) / count %d / cycle %d\n", |
|---|
| 284 | __FUNCTION__ , this->process->pid, this->trdid, |
|---|
| 285 | lba, GET_CXY(buffer_xp), GET_PTR(buffer_xp), count, cycle ); |
|---|
| 286 | #endif |
|---|
| 287 | |
|---|
| 288 | return dev_ioc_sync_access( IOC_SYNC_WRITE , buffer_xp , lba , count ); |
|---|
| 289 | } |
|---|
| 290 | |
|---|