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