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