[75] | 1 | /* |
---|
| 2 | * soclib_bdv.c - soclib simple block device driver implementation. |
---|
| 3 | * |
---|
[437] | 4 | * Author Alain Greiner (2016,2017,2018) |
---|
[75] | 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 | |
---|
[451] | 24 | #include <soclib_bdv.h> |
---|
| 25 | #include <hal_kernel_types.h> |
---|
[75] | 26 | #include <chdev.h> |
---|
| 27 | #include <dev_ioc.h> |
---|
[265] | 28 | #include <printk.h> |
---|
[75] | 29 | #include <thread.h> |
---|
[622] | 30 | #include <hal_irqmask.h> |
---|
[75] | 31 | |
---|
| 32 | /////////////////////////////////////// |
---|
| 33 | void soclib_bdv_init( chdev_t * chdev ) |
---|
| 34 | { |
---|
[610] | 35 | // get extended pointer on SOCLIB_BDV peripheral base |
---|
[75] | 36 | xptr_t bdv_xp = chdev->base; |
---|
| 37 | |
---|
[211] | 38 | // set driver specific fields |
---|
| 39 | chdev->cmd = &soclib_bdv_cmd; |
---|
| 40 | chdev->isr = &soclib_bdv_isr; |
---|
| 41 | |
---|
[75] | 42 | // get hardware device cluster and local pointer |
---|
| 43 | cxy_t bdv_cxy = GET_CXY( bdv_xp ); |
---|
| 44 | uint32_t * bdv_ptr = (uint32_t *)GET_PTR( bdv_xp ); |
---|
| 45 | |
---|
| 46 | // get block_size and block_count |
---|
[570] | 47 | uint32_t block_size = hal_remote_l32( XPTR( bdv_cxy , bdv_ptr + BDV_BLOCK_SIZE_REG ) ); |
---|
| 48 | uint32_t block_count = hal_remote_l32( XPTR( bdv_cxy , bdv_ptr + BDV_SIZE_REG ) ); |
---|
[75] | 49 | |
---|
| 50 | // set IOC device descriptor extension |
---|
| 51 | chdev->ext.ioc.size = block_size; |
---|
| 52 | chdev->ext.ioc.count = block_count; |
---|
| 53 | |
---|
| 54 | } // end soclib_bdv_init() |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | ////////////////////////////////////////////////////////////// |
---|
| 58 | void __attribute__ ((noinline)) soclib_bdv_cmd( xptr_t th_xp ) |
---|
| 59 | { |
---|
[626] | 60 | uint32_t cmd_type; // IOC_READ / IOC_WRITE / IOC_SYNC_READ / IOC_SYNC_WRITE |
---|
[279] | 61 | uint32_t lba; |
---|
| 62 | uint32_t count; |
---|
| 63 | xptr_t buf_xp; |
---|
| 64 | xptr_t ioc_xp; |
---|
[610] | 65 | uint32_t status; // I/0 operation status (from BDV) |
---|
| 66 | reg_t save_sr; // for critical section |
---|
| 67 | uint32_t op; // BDV_OP_READ / BDV_OP_WRITE |
---|
[75] | 68 | |
---|
| 69 | // get client thread cluster and local pointer |
---|
| 70 | cxy_t th_cxy = GET_CXY( th_xp ); |
---|
[440] | 71 | thread_t * th_ptr = GET_PTR( th_xp ); |
---|
[75] | 72 | |
---|
[610] | 73 | #if (DEBUG_HAL_IOC_RX || DEBUG_HAL_IOC_TX) |
---|
| 74 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 75 | thread_t * this = CURRENT_THREAD; |
---|
| 76 | process_t * process = hal_remote_lpt( XPTR( th_cxy , &th_ptr->process ) ); |
---|
| 77 | pid_t client_pid = hal_remote_l32( XPTR( th_cxy , &process->pid ) ); |
---|
| 78 | trdid_t client_trdid = hal_remote_l32( XPTR( th_cxy , &th_ptr->trdid ) ); |
---|
| 79 | #endif |
---|
| 80 | |
---|
[75] | 81 | // get command arguments and extended pointer on IOC device |
---|
[610] | 82 | cmd_type = hal_remote_l32( XPTR( th_cxy , &th_ptr->ioc_cmd.type ) ); |
---|
| 83 | lba = hal_remote_l32( XPTR( th_cxy , &th_ptr->ioc_cmd.lba ) ); |
---|
| 84 | count = hal_remote_l32( XPTR( th_cxy , &th_ptr->ioc_cmd.count ) ); |
---|
[570] | 85 | buf_xp = (xptr_t)hal_remote_l64( XPTR( th_cxy , &th_ptr->ioc_cmd.buf_xp ) ); |
---|
| 86 | ioc_xp = (xptr_t)hal_remote_l64( XPTR( th_cxy , &th_ptr->ioc_cmd.dev_xp ) ); |
---|
[75] | 87 | |
---|
[626] | 88 | // decode command |
---|
| 89 | if ( (cmd_type == IOC_READ) || (cmd_type == IOC_SYNC_READ) ) op = BDV_OP_READ; |
---|
| 90 | else if( (cmd_type == IOC_WRITE) || (cmd_type == IOC_SYNC_WRITE) ) op = BDV_OP_WRITE; |
---|
| 91 | else assert( false , "illegal command" ); |
---|
[437] | 92 | |
---|
[75] | 93 | // get IOC device cluster and local pointer |
---|
[279] | 94 | cxy_t ioc_cxy = GET_CXY( ioc_xp ); |
---|
[440] | 95 | chdev_t * ioc_ptr = GET_PTR( ioc_xp ); |
---|
[75] | 96 | |
---|
[440] | 97 | // get cluster and pointers for SOCLIB-BDV peripheral segment base |
---|
[570] | 98 | xptr_t seg_xp = (xptr_t)hal_remote_l64( XPTR( ioc_cxy , &ioc_ptr->base ) ); |
---|
[440] | 99 | cxy_t seg_cxy = GET_CXY( seg_xp ); |
---|
| 100 | uint32_t * seg_ptr = GET_PTR( seg_xp ); |
---|
[75] | 101 | |
---|
| 102 | // split buffer address in two 32 bits words |
---|
| 103 | uint32_t buf_lsb = (uint32_t)(buf_xp); |
---|
| 104 | uint32_t buf_msb = (uint32_t)(buf_xp>>32); |
---|
| 105 | |
---|
[626] | 106 | #if DEBUG_HAL_IOC_RX |
---|
| 107 | if( DEBUG_HAL_IOC_RX < cycle ) |
---|
| 108 | printk("\n[%s] thread[%x,%x] enters / client[%x,%x] / cmd %d / lba %x / buf(%x,%x) / cycle %d\n", |
---|
| 109 | __FUNCTION__ , this->process->pid, this->trdid, client_pid, client_trdid, |
---|
| 110 | cmd_type, lba, buf_msb, buf_lsb, cycle ); |
---|
| 111 | #endif |
---|
| 112 | |
---|
| 113 | #if DEBUG_HAL_IOC_TX |
---|
| 114 | if( DEBUG_HAL_IOC_TX < cycle ) |
---|
| 115 | printk("\n[%s] thread[%x,%x] enters / client[%x,%x] / cmd %d / lba %x / buf(%x,%x) / cycle %d\n", |
---|
| 116 | __FUNCTION__ , this->process->pid, this->trdid, client_pid, client_trdid, |
---|
| 117 | cmd_type, lba, buf_msb, buf_lsb, cycle ); |
---|
| 118 | #endif |
---|
| 119 | |
---|
[610] | 120 | // select operation |
---|
[615] | 121 | if( (cmd_type == IOC_READ) || (cmd_type == IOC_SYNC_READ) ) op = BDV_OP_READ; |
---|
| 122 | else op = BDV_OP_WRITE; |
---|
[75] | 123 | |
---|
[610] | 124 | // set SOCLIB_BDV registers to configure the I/O operation |
---|
[570] | 125 | hal_remote_s32( XPTR( seg_cxy , seg_ptr + BDV_IRQ_ENABLE_REG ) , 1 ); |
---|
| 126 | hal_remote_s32( XPTR( seg_cxy , seg_ptr + BDV_BUFFER_REG ) , buf_lsb ); |
---|
| 127 | hal_remote_s32( XPTR( seg_cxy , seg_ptr + BDV_BUFFER_EXT_REG ) , buf_msb ); |
---|
| 128 | hal_remote_s32( XPTR( seg_cxy , seg_ptr + BDV_LBA_REG ) , lba ); |
---|
| 129 | hal_remote_s32( XPTR( seg_cxy , seg_ptr + BDV_COUNT_REG ) , count ); |
---|
[75] | 130 | |
---|
| 131 | // waiting policy depends on the command type |
---|
[407] | 132 | // - for IOC_READ / IOC_WRITE commands, this function is called by the server thread |
---|
[610] | 133 | // that blocks and deschedules after launching the I/O transfer. |
---|
| 134 | // The I/O operation status is reported in the command by the ISR. |
---|
[615] | 135 | // - for IOC_SYNC_READ / IOC_SYNC_WRITE command, this function is called by the client |
---|
| 136 | // thread that polls the BDV status register until I/O transfer completion. |
---|
[75] | 137 | |
---|
[615] | 138 | if( (cmd_type == IOC_SYNC_READ) || (cmd_type == IOC_SYNC_WRITE) ) // polling policy |
---|
[75] | 139 | { |
---|
[610] | 140 | // launch I/O operation on BDV device |
---|
| 141 | hal_remote_s32( XPTR( seg_cxy , seg_ptr + BDV_OP_REG ) , op ); |
---|
| 142 | |
---|
| 143 | // wait completion |
---|
[75] | 144 | while (1) |
---|
| 145 | { |
---|
[570] | 146 | status = hal_remote_l32( XPTR( seg_cxy , seg_ptr + BDV_STATUS_REG ) ); |
---|
[75] | 147 | |
---|
[615] | 148 | if( (status == BDV_READ_SUCCESS) || |
---|
| 149 | (status == BDV_WRITE_SUCCESS) ) // successfully completed |
---|
[75] | 150 | { |
---|
[570] | 151 | hal_remote_s32( XPTR( th_cxy , &th_ptr->ioc_cmd.error ) , 0 ); |
---|
[626] | 152 | |
---|
| 153 | #if DEBUG_HAL_IOC_RX |
---|
| 154 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 155 | if( (DEBUG_HAL_IOC_RX < cycle) && (cmd_type == IOC_SYNC_READ) ) |
---|
| 156 | printk("\n[%s] thread[%x,%x] exit after SYNC_READ for client thread[%x,%x] / cycle %d\n", |
---|
| 157 | __FUNCTION__, this->process->pid, this->trdid, client_pid, client_trdid, cycle ); |
---|
| 158 | #endif |
---|
| 159 | |
---|
| 160 | #if DEBUG_HAL_IOC_TX |
---|
| 161 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 162 | if( (DEBUG_HAL_IOC_TX < cycle) && (cmd_type == IOC_SYNC_WRITE) ) |
---|
| 163 | printk("\n[%s] thread[%x,%x] exit after SYNC_WRITE for client thread[%x,%x] / cycle %d\n", |
---|
| 164 | __FUNCTION__, this->process->pid, this->trdid, client_pid, client_trdid, cycle ); |
---|
| 165 | #endif |
---|
[75] | 166 | break; |
---|
| 167 | } |
---|
[626] | 168 | else if( status == BDV_BUSY ) // non completed |
---|
[75] | 169 | { |
---|
| 170 | continue; |
---|
| 171 | } |
---|
[626] | 172 | else // error reported |
---|
[75] | 173 | { |
---|
[570] | 174 | hal_remote_s32( XPTR( th_cxy , &th_ptr->ioc_cmd.error ) , 1 ); |
---|
[75] | 175 | break; |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | } |
---|
[615] | 179 | else // descheduling + IRQ policy |
---|
[75] | 180 | { |
---|
[610] | 181 | // enter critical section to atomically |
---|
| 182 | // lauch I/O operation and deschedule |
---|
| 183 | hal_disable_irq( &save_sr ); |
---|
| 184 | |
---|
| 185 | // launch I/O operation on BDV device |
---|
| 186 | hal_remote_s32( XPTR( seg_cxy , seg_ptr + BDV_OP_REG ) , op ); |
---|
| 187 | |
---|
| 188 | // server thread blocks on ISR |
---|
[436] | 189 | thread_block( XPTR( local_cxy , CURRENT_THREAD ) , THREAD_BLOCKED_ISR ); |
---|
[610] | 190 | |
---|
| 191 | #if DEBUG_HAL_IOC_RX |
---|
| 192 | if( (DEBUG_HAL_IOC_RX < cycle) && (cmd_type != IOC_WRITE ) ) |
---|
[626] | 193 | printk("\n[%s] thread[%x,%x] blocks & deschedules after lauching READ transfer\n", |
---|
[610] | 194 | __FUNCTION__ , this->process->pid, this->trdid ); |
---|
| 195 | #endif |
---|
| 196 | |
---|
| 197 | #if DEBUG_HAL_IOC_TX |
---|
| 198 | if( (DEBUG_HAL_IOC_TX < cycle) && (cmd_type == IOC_WRITE) ) |
---|
[626] | 199 | printk("\n[%s] thread[%x,%x] blocks & deschedules after lauching WRITE transfer\n", |
---|
[610] | 200 | __FUNCTION__ , this->process->pid, this->trdid ); |
---|
| 201 | #endif |
---|
| 202 | // server thread deschedules |
---|
[408] | 203 | sched_yield("blocked on ISR"); |
---|
[407] | 204 | |
---|
[610] | 205 | // exit critical section |
---|
| 206 | hal_restore_irq( save_sr ); |
---|
[75] | 207 | |
---|
[438] | 208 | #if DEBUG_HAL_IOC_RX |
---|
[437] | 209 | cycle = (uint32_t)hal_get_cycles(); |
---|
[610] | 210 | if( (DEBUG_HAL_IOC_RX < cycle) && (cmd_type != IOC_WRITE) ) |
---|
[626] | 211 | printk("\n[%s] thread[%x,%x] exit after READ for client thread[%x,%x] / cycle %d\n", |
---|
[610] | 212 | __FUNCTION__, this->process->pid, this->trdid, client_pid, client_trdid, cycle ); |
---|
[437] | 213 | #endif |
---|
| 214 | |
---|
[438] | 215 | #if DEBUG_HAL_IOC_TX |
---|
[437] | 216 | cycle = (uint32_t)hal_get_cycles(); |
---|
[610] | 217 | if( (DEBUG_HAL_IOC_TX < cycle) && (cmd_type == IOC_WRITE) ) |
---|
[626] | 218 | printk("\n[%s] thread[%x,%x] exit after WRITE for client thread[%x,%x] / cycle %d\n", |
---|
[610] | 219 | __FUNCTION__, this->process->pid, this->trdid, client_pid, client_trdid, cycle ); |
---|
[437] | 220 | #endif |
---|
| 221 | |
---|
[626] | 222 | } |
---|
| 223 | |
---|
[75] | 224 | } // end soclib_bdv_cmd() |
---|
| 225 | |
---|
| 226 | |
---|
| 227 | ///////////////////////////////////////////////////////////////// |
---|
| 228 | void __attribute__ ((noinline)) soclib_bdv_isr( chdev_t * chdev ) |
---|
| 229 | { |
---|
[437] | 230 | error_t error = 0; |
---|
| 231 | |
---|
[610] | 232 | // get extended pointer on server thread |
---|
| 233 | xptr_t server_xp = XPTR( local_cxy , chdev->server ); |
---|
| 234 | |
---|
[75] | 235 | // get extended pointer on client thread |
---|
[610] | 236 | xptr_t root = XPTR( local_cxy , &chdev->wait_root ); |
---|
[570] | 237 | xptr_t client_xp = XLIST_FIRST( root , thread_t , wait_list ); |
---|
[75] | 238 | |
---|
| 239 | // get client thread cluster and local pointer |
---|
| 240 | cxy_t client_cxy = GET_CXY( client_xp ); |
---|
[610] | 241 | thread_t * client_ptr = GET_PTR( client_xp ); |
---|
[75] | 242 | |
---|
[437] | 243 | // get command type |
---|
[570] | 244 | uint32_t cmd_type = hal_remote_l32( XPTR( client_cxy , &client_ptr->ioc_cmd.type ) ); |
---|
[437] | 245 | |
---|
[610] | 246 | #if (DEBUG_HAL_IOC_RX || DEBUG_HAL_IOC_TX) |
---|
| 247 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 248 | process_t * process = hal_remote_lpt( XPTR( client_cxy , &client_ptr->process ) ); |
---|
| 249 | pid_t client_pid = hal_remote_l32( XPTR( client_cxy , &process->pid ) ); |
---|
| 250 | trdid_t client_trdid = hal_remote_l32( XPTR( client_cxy , &client_ptr->trdid ) ); |
---|
| 251 | thread_t * server = GET_PTR( server_xp ); |
---|
| 252 | pid_t server_pid = server->process->pid; |
---|
| 253 | trdid_t server_trdid = server->trdid; |
---|
| 254 | #endif |
---|
| 255 | |
---|
[75] | 256 | // get SOCLIB_BDV device cluster and local pointer |
---|
| 257 | cxy_t bdv_cxy = GET_CXY( chdev->base ); |
---|
[610] | 258 | uint32_t * bdv_ptr = GET_PTR( chdev->base ); |
---|
[75] | 259 | |
---|
| 260 | // get BDV status register and acknowledge IRQ |
---|
[570] | 261 | uint32_t status = hal_remote_l32( XPTR( bdv_cxy , bdv_ptr + BDV_STATUS_REG ) ); |
---|
[75] | 262 | |
---|
[437] | 263 | if( cmd_type == IOC_READ ) |
---|
[75] | 264 | { |
---|
[437] | 265 | error = (status != BDV_READ_SUCCESS); |
---|
| 266 | |
---|
[438] | 267 | #if DEBUG_HAL_IOC_RX |
---|
| 268 | if( DEBUG_HAL_IOC_RX < cycle ) |
---|
[610] | 269 | printk("\n[%s] RX transfer completed for client[%x,%x] / server[%x,%x] / cycle %d\n", |
---|
| 270 | __FUNCTION__, client_pid, client_trdid, server_pid, server_trdid, cycle ); |
---|
[437] | 271 | #endif |
---|
| 272 | |
---|
[75] | 273 | } |
---|
[437] | 274 | else if( cmd_type == IOC_WRITE ) |
---|
[75] | 275 | { |
---|
[437] | 276 | error = (status != BDV_WRITE_SUCCESS); |
---|
| 277 | |
---|
[438] | 278 | #if DEBUG_HAL_IOC_TX |
---|
| 279 | if( DEBUG_HAL_IOC_TX < cycle ) |
---|
[610] | 280 | printk("\n[%s] TX transfer completed for client[%x,%x] / server[%x,%x] / cycle %d\n", |
---|
| 281 | __FUNCTION__, client_pid, client_trdid, server_pid, server_trdid, cycle ); |
---|
[437] | 282 | #endif |
---|
| 283 | |
---|
[75] | 284 | } |
---|
[437] | 285 | else |
---|
| 286 | { |
---|
[626] | 287 | assert( false , "illegal command %d", cmd_type ); |
---|
[437] | 288 | } |
---|
[75] | 289 | |
---|
[437] | 290 | // set operation status in command |
---|
[570] | 291 | hal_remote_s32( XPTR( client_cxy , &client_ptr->ioc_cmd.error ) , error ); |
---|
[437] | 292 | |
---|
[75] | 293 | // unblock server thread |
---|
[436] | 294 | thread_unblock( server_xp , THREAD_BLOCKED_ISR ); |
---|
[75] | 295 | |
---|
| 296 | } // end soclib_bdv_isr() |
---|
| 297 | |
---|
| 298 | |
---|
| 299 | |
---|