[1] | 1 | /* |
---|
| 2 | * dev_ioc.c - IOC (Block Device Controler) generic device API implementation. |
---|
[212] | 3 | * |
---|
[626] | 4 | * Author Alain Greiner (2016,2017,2018,2019) |
---|
[1] | 5 | * |
---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 7 | * |
---|
| 8 | * This file is part of ALMOS-MK |
---|
| 9 | * |
---|
[212] | 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
[1] | 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 | * |
---|
[212] | 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
[1] | 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 |
---|
[212] | 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
[1] | 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
[14] | 24 | #include <kernel_config.h> |
---|
[457] | 25 | #include <hal_kernel_types.h> |
---|
[1] | 26 | #include <hal_gpt.h> |
---|
[213] | 27 | #include <hal_drivers.h> |
---|
[1] | 28 | #include <thread.h> |
---|
| 29 | #include <printk.h> |
---|
[3] | 30 | #include <chdev.h> |
---|
[1] | 31 | #include <dev_ioc.h> |
---|
| 32 | |
---|
| 33 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 34 | // Extern global variables |
---|
| 35 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 36 | |
---|
[188] | 37 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
---|
[1] | 38 | |
---|
[626] | 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 | |
---|
[188] | 49 | ////////////////////////////////// |
---|
| 50 | void dev_ioc_init( chdev_t * ioc ) |
---|
[1] | 51 | { |
---|
[188] | 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; |
---|
[1] | 55 | |
---|
[492] | 56 | assert( (pic_xp != XPTR_NULL) , "PIC not initialised before IOC" ); |
---|
[188] | 57 | |
---|
[3] | 58 | // get implementation and channel from chdev descriptor |
---|
[188] | 59 | uint32_t impl = ioc->impl; |
---|
| 60 | uint32_t channel = ioc->channel; |
---|
[1] | 61 | |
---|
[23] | 62 | // set chdev name |
---|
[407] | 63 | snprintf( ioc->name , 16 , "ioc%d" , channel ); |
---|
[23] | 64 | |
---|
[211] | 65 | // call driver init function |
---|
[216] | 66 | hal_drivers_ioc_init( ioc, impl ); |
---|
[1] | 67 | |
---|
[188] | 68 | // select a core to execute the IOC server thread |
---|
[637] | 69 | lid_t lid = cluster_select_local_core( local_cxy ); |
---|
[3] | 70 | |
---|
[188] | 71 | // bind the IOC IRQ to the selected core |
---|
| 72 | dev_pic_bind_irq( lid , ioc ); |
---|
[3] | 73 | |
---|
[188] | 74 | // enable IRQ |
---|
[238] | 75 | dev_pic_enable_irq( lid , XPTR( local_cxy , ioc ) ); |
---|
[3] | 76 | |
---|
[1] | 77 | // create server thread |
---|
[3] | 78 | thread_t * new_thread; |
---|
[1] | 79 | error_t error; |
---|
| 80 | |
---|
[3] | 81 | error = thread_kernel_create( &new_thread, |
---|
| 82 | THREAD_DEV, |
---|
[619] | 83 | &chdev_server_func, |
---|
[188] | 84 | ioc, |
---|
[212] | 85 | lid ); |
---|
[188] | 86 | |
---|
[492] | 87 | assert( (error == 0) , "cannot create server thread" ); |
---|
[1] | 88 | |
---|
[408] | 89 | // set "server" field in chdev descriptor |
---|
[188] | 90 | ioc->server = new_thread; |
---|
[212] | 91 | |
---|
[408] | 92 | // set "chdev field in thread descriptor |
---|
| 93 | new_thread->chdev = ioc; |
---|
| 94 | |
---|
| 95 | // unblock server thread |
---|
[3] | 96 | thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL ); |
---|
[212] | 97 | |
---|
[1] | 98 | } // end dev_ioc_init() |
---|
| 99 | |
---|
| 100 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 101 | // This static function is called by dev_ioc_read() & dev_ioc_write() functions. |
---|
[23] | 102 | // It builds and registers the command in the calling thread descriptor. |
---|
[614] | 103 | // Then, it registers the calling thead in IOC chdev waiting queue. |
---|
[407] | 104 | // Finally it blocks on the THREAD_BLOCKED_IO condition and deschedule. |
---|
[1] | 105 | ////////////////////////////////////i///////////////////////////////////////////// |
---|
[23] | 106 | static error_t dev_ioc_access( uint32_t cmd_type, |
---|
| 107 | uint8_t * buffer, |
---|
| 108 | uint32_t lba, |
---|
| 109 | uint32_t count ) |
---|
[1] | 110 | { |
---|
[3] | 111 | thread_t * this = CURRENT_THREAD; // pointer on client thread |
---|
[1] | 112 | |
---|
[605] | 113 | #if ( DEV_IOC_RX || DEV_IOC_TX ) |
---|
| 114 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 115 | #endif |
---|
| 116 | |
---|
[212] | 117 | // software L2/L3 cache coherence for memory buffer |
---|
| 118 | if( chdev_dir.iob ) |
---|
[68] | 119 | { |
---|
[614] | 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 ); |
---|
[68] | 122 | } |
---|
[1] | 123 | |
---|
[3] | 124 | // get extended pointer on IOC chdev descriptor |
---|
| 125 | xptr_t dev_xp = chdev_dir.ioc[0]; |
---|
[1] | 126 | |
---|
[605] | 127 | // check dev_xp |
---|
| 128 | assert( (dev_xp != XPTR_NULL) , "undefined IOC chdev descriptor" ); |
---|
[1] | 129 | |
---|
[3] | 130 | // register command in calling thread descriptor |
---|
[279] | 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; |
---|
[1] | 136 | |
---|
[3] | 137 | // register client thread in IOC chdev waiting queue, activate server thread, |
---|
[1] | 138 | // block client thread on THREAD_BLOCKED_IO and deschedule. |
---|
| 139 | // it is re-activated by the ISR signaling IO operation completion. |
---|
[407] | 140 | chdev_register_command( dev_xp ); |
---|
[1] | 141 | |
---|
[605] | 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 | |
---|
[1] | 154 | // return I/O operation status |
---|
[279] | 155 | return this->ioc_cmd.error; |
---|
[1] | 156 | |
---|
| 157 | } // end dev_ioc_access() |
---|
| 158 | |
---|
| 159 | //////////////////////////////////////////// |
---|
[23] | 160 | error_t dev_ioc_read( uint8_t * buffer, |
---|
[1] | 161 | uint32_t lba, |
---|
| 162 | uint32_t count ) |
---|
| 163 | { |
---|
[437] | 164 | |
---|
[438] | 165 | #if DEBUG_DEV_IOC_RX |
---|
[605] | 166 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 167 | thread_t * this = CURRENT_THREAD; |
---|
[438] | 168 | if( DEBUG_DEV_IOC_RX < cycle ) |
---|
[605] | 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 ); |
---|
[437] | 171 | #endif |
---|
| 172 | |
---|
[212] | 173 | return dev_ioc_access( IOC_READ , buffer , lba , count ); |
---|
| 174 | } |
---|
[1] | 175 | |
---|
| 176 | //////////////////////////////////////////// |
---|
[23] | 177 | error_t dev_ioc_write( uint8_t * buffer, |
---|
| 178 | uint32_t lba, |
---|
| 179 | uint32_t count ) |
---|
[1] | 180 | { |
---|
[437] | 181 | |
---|
[438] | 182 | #if DEBUG_DEV_IOC_TX |
---|
[605] | 183 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 184 | thread_t * this = CURRENT_THREAD; |
---|
[438] | 185 | if( DEBUG_DEV_IOC_TX < cycle ) |
---|
[605] | 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 ); |
---|
[437] | 188 | #endif |
---|
| 189 | |
---|
[212] | 190 | return dev_ioc_access( IOC_WRITE , buffer , lba , count ); |
---|
[614] | 191 | } |
---|
[437] | 192 | |
---|
| 193 | |
---|
[23] | 194 | |
---|
[614] | 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, |
---|
[626] | 204 | xptr_t buffer_xp, |
---|
[614] | 205 | uint32_t lba, |
---|
| 206 | uint32_t count ) |
---|
[23] | 207 | { |
---|
[212] | 208 | // get pointer on calling thread |
---|
[23] | 209 | thread_t * this = CURRENT_THREAD; |
---|
| 210 | |
---|
[212] | 211 | // software L2/L3 cache coherence for memory buffer |
---|
[614] | 212 | if( chdev_dir.iob ) |
---|
| 213 | { |
---|
[626] | 214 | if (cmd_type == IOC_SYNC_READ) dev_mmc_inval( buffer_xp , count<<9 ); |
---|
| 215 | else dev_mmc_sync ( buffer_xp , count<<9 ); |
---|
[614] | 216 | } |
---|
[23] | 217 | |
---|
[212] | 218 | // get extended pointer on IOC[0] chdev |
---|
[279] | 219 | xptr_t ioc_xp = chdev_dir.ioc[0]; |
---|
[23] | 220 | |
---|
[614] | 221 | // check ioc_xp |
---|
| 222 | assert( (ioc_xp != XPTR_NULL) , "undefined IOC chdev descriptor" ); |
---|
[23] | 223 | |
---|
| 224 | // register command in calling thread descriptor |
---|
[279] | 225 | this->ioc_cmd.dev_xp = ioc_xp; |
---|
[614] | 226 | this->ioc_cmd.type = cmd_type; |
---|
[626] | 227 | this->ioc_cmd.buf_xp = buffer_xp; |
---|
[279] | 228 | this->ioc_cmd.lba = lba; |
---|
| 229 | this->ioc_cmd.count = count; |
---|
[23] | 230 | |
---|
[212] | 231 | // get driver command function |
---|
[279] | 232 | cxy_t ioc_cxy = GET_CXY( ioc_xp ); |
---|
[614] | 233 | chdev_t * ioc_ptr = GET_PTR( ioc_xp ); |
---|
[279] | 234 | dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( ioc_cxy , &ioc_ptr->cmd ) ); |
---|
[23] | 235 | |
---|
[279] | 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 ) ); |
---|
[565] | 239 | lid_t lid = (lid_t)hal_remote_l32( XPTR( ioc_cxy , &core->lid ) ); |
---|
[279] | 240 | |
---|
[207] | 241 | // mask the IRQ |
---|
[279] | 242 | dev_pic_disable_irq( lid , ioc_xp ); |
---|
[207] | 243 | |
---|
[279] | 244 | // call driver function |
---|
[23] | 245 | cmd( XPTR( local_cxy , this ) ); |
---|
| 246 | |
---|
[207] | 247 | // unmask the IRQ |
---|
[279] | 248 | dev_pic_enable_irq( lid , ioc_xp ); |
---|
[207] | 249 | |
---|
[614] | 250 | // return I/O operation status from calling thread descriptor |
---|
| 251 | return this->ioc_cmd.error; |
---|
| 252 | |
---|
[626] | 253 | } // end dev_ioc_sync_access() |
---|
[614] | 254 | |
---|
[626] | 255 | //////////////////////////////////////////////// |
---|
| 256 | error_t dev_ioc_sync_read( xptr_t buffer_xp, |
---|
[614] | 257 | uint32_t lba, |
---|
| 258 | uint32_t count ) |
---|
| 259 | { |
---|
| 260 | |
---|
[438] | 261 | #if DEBUG_DEV_IOC_RX |
---|
[614] | 262 | thread_t * this = CURRENT_THREAD; |
---|
| 263 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 264 | if( DEBUG_DEV_IOC_RX < cycle ) |
---|
[626] | 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 ); |
---|
[437] | 268 | #endif |
---|
[279] | 269 | |
---|
[626] | 270 | return dev_ioc_sync_access( IOC_SYNC_READ , buffer_xp , lba , count ); |
---|
[614] | 271 | } |
---|
[23] | 272 | |
---|
[626] | 273 | ///////////////////////////////////////////////// |
---|
| 274 | error_t dev_ioc_sync_write( xptr_t buffer_xp, |
---|
[614] | 275 | uint32_t lba, |
---|
| 276 | uint32_t count ) |
---|
| 277 | { |
---|
[279] | 278 | |
---|
[626] | 279 | #if DEBUG_DEV_IOC_TX |
---|
[614] | 280 | thread_t * this = CURRENT_THREAD; |
---|
| 281 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[626] | 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 ); |
---|
[614] | 286 | #endif |
---|
| 287 | |
---|
[626] | 288 | return dev_ioc_sync_access( IOC_SYNC_WRITE , buffer_xp , lba , count ); |
---|
[614] | 289 | } |
---|
| 290 | |
---|