| 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 | #include <spinlock.h> | 
|---|
| 31 |  | 
|---|
| 32 | /////////////////////////////////////// | 
|---|
| 33 | void soclib_bdv_init( chdev_t * chdev ) | 
|---|
| 34 | { | 
|---|
| 35 |     // get extended pointer on SOCLIB_BDV peripheral base address | 
|---|
| 36 |         xptr_t  bdv_xp = chdev->base; | 
|---|
| 37 |  | 
|---|
| 38 |     // set driver specific fields | 
|---|
| 39 |     chdev->cmd = &soclib_bdv_cmd; | 
|---|
| 40 |     chdev->isr = &soclib_bdv_isr; | 
|---|
| 41 |  | 
|---|
| 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   | 
|---|
| 47 |         uint32_t block_size = hal_remote_lw( XPTR( bdv_cxy , bdv_ptr + BDV_BLOCK_SIZE_REG ) ); | 
|---|
| 48 |         uint32_t block_count = hal_remote_lw( XPTR( bdv_cxy , bdv_ptr + BDV_SIZE_REG ) ); | 
|---|
| 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 | { | 
|---|
| 60 |     uint32_t   cmd_type;    // IOC_READ / IOC_WRITE / IOC_SYNC_READ | 
|---|
| 61 |     uint32_t   lba; | 
|---|
| 62 |     uint32_t   count; | 
|---|
| 63 |     xptr_t     buf_xp; | 
|---|
| 64 |     xptr_t     ioc_xp; | 
|---|
| 65 |  | 
|---|
| 66 |     // get client thread cluster and local pointer | 
|---|
| 67 |     cxy_t      th_cxy = GET_CXY( th_xp ); | 
|---|
| 68 |     thread_t * th_ptr = GET_PTR( th_xp ); | 
|---|
| 69 |  | 
|---|
| 70 |     // get command arguments and extended pointer on IOC device | 
|---|
| 71 |     cmd_type =         hal_remote_lw ( XPTR( th_cxy , &th_ptr->ioc_cmd.type   ) ); | 
|---|
| 72 |     lba      =         hal_remote_lw ( XPTR( th_cxy , &th_ptr->ioc_cmd.lba    ) ); | 
|---|
| 73 |     count    =         hal_remote_lw ( XPTR( th_cxy , &th_ptr->ioc_cmd.count  ) ); | 
|---|
| 74 |     buf_xp   = (xptr_t)hal_remote_lwd( XPTR( th_cxy , &th_ptr->ioc_cmd.buf_xp ) ); | 
|---|
| 75 |     ioc_xp   = (xptr_t)hal_remote_lwd( XPTR( th_cxy , &th_ptr->ioc_cmd.dev_xp ) ); | 
|---|
| 76 |  | 
|---|
| 77 | #if DEBUG_HAL_IOC_RX | 
|---|
| 78 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 79 | if( (DEBUG_HAL_IOC_RX < cycle) && (cmd_type != IOC_WRITE ) ) | 
|---|
| 80 | printk("\n[DBG] %s : thread %x enter for RX / cycle %d\n", | 
|---|
| 81 | __FUNCTION__ , CURRENT_THREAD , cycle ); | 
|---|
| 82 | #endif | 
|---|
| 83 |  | 
|---|
| 84 | #if DEBUG_HAL_IOC_TX | 
|---|
| 85 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 86 | if( (DEBUG_HAL_IOC_TX < cycle) && (cmd_type == IOC_WRITE) ) | 
|---|
| 87 | printk("\n[DBG] %s : thread %x enter for TX / cycle %d\n", | 
|---|
| 88 | __FUNCTION__ , CURRENT_THREAD , cycle ); | 
|---|
| 89 | #endif | 
|---|
| 90 |  | 
|---|
| 91 |     // get IOC device cluster and local pointer | 
|---|
| 92 |     cxy_t      ioc_cxy = GET_CXY( ioc_xp ); | 
|---|
| 93 |     chdev_t  * ioc_ptr = GET_PTR( ioc_xp ); | 
|---|
| 94 |  | 
|---|
| 95 |     // get cluster and pointers for SOCLIB-BDV peripheral segment base | 
|---|
| 96 |     xptr_t     seg_xp  = (xptr_t)hal_remote_lwd( XPTR( ioc_cxy , &ioc_ptr->base ) ); | 
|---|
| 97 |     cxy_t      seg_cxy = GET_CXY( seg_xp ); | 
|---|
| 98 |     uint32_t * seg_ptr = GET_PTR( seg_xp ); | 
|---|
| 99 |  | 
|---|
| 100 |     // split buffer address in two 32 bits words | 
|---|
| 101 |     uint32_t   buf_lsb = (uint32_t)(buf_xp); | 
|---|
| 102 |     uint32_t   buf_msb = (uint32_t)(buf_xp>>32); | 
|---|
| 103 |  | 
|---|
| 104 |     // set operation | 
|---|
| 105 |     uint32_t   op; | 
|---|
| 106 |     if( cmd_type == IOC_WRITE ) op = BDV_OP_WRITE;  | 
|---|
| 107 |     else                        op = BDV_OP_READ; | 
|---|
| 108 |  | 
|---|
| 109 |     // set SOCLIB_BDV registers to start one I/O operation | 
|---|
| 110 |     hal_remote_sw( XPTR( seg_cxy , seg_ptr + BDV_IRQ_ENABLE_REG ) , 1       ); | 
|---|
| 111 |     hal_remote_sw( XPTR( seg_cxy , seg_ptr + BDV_BUFFER_REG     ) , buf_lsb );  | 
|---|
| 112 |     hal_remote_sw( XPTR( seg_cxy , seg_ptr + BDV_BUFFER_EXT_REG ) , buf_msb );  | 
|---|
| 113 |     hal_remote_sw( XPTR( seg_cxy , seg_ptr + BDV_LBA_REG        ) , lba     ); | 
|---|
| 114 |     hal_remote_sw( XPTR( seg_cxy , seg_ptr + BDV_COUNT_REG      ) , count   ); | 
|---|
| 115 |     hal_remote_sw( XPTR( seg_cxy , seg_ptr + BDV_OP_REG         ) , op      ); | 
|---|
| 116 |  | 
|---|
| 117 |     // waiting policy  depends on the command type | 
|---|
| 118 |     // - for IOC_READ / IOC_WRITE commands, this function is called by the server thread | 
|---|
| 119 |     // - for IOC_SYNC_READ command, this function is directly called by the client thread | 
|---|
| 120 |  | 
|---|
| 121 |     if( cmd_type == IOC_SYNC_READ )                   // status polling policy | 
|---|
| 122 |     { | 
|---|
| 123 |         uint32_t status; | 
|---|
| 124 |         while (1) | 
|---|
| 125 |         { | 
|---|
| 126 |             status = hal_remote_lw( XPTR( seg_cxy , seg_ptr + BDV_STATUS_REG ) ); | 
|---|
| 127 |  | 
|---|
| 128 |             if( status == BDV_READ_SUCCESS ) // successfully completed | 
|---|
| 129 |             { | 
|---|
| 130 |                 hal_remote_sw( XPTR( th_cxy , &th_ptr->ioc_cmd.error ) , 0 ); | 
|---|
| 131 |                 break; | 
|---|
| 132 |             } | 
|---|
| 133 |             else if( status == BDV_BUSY )   // non completed | 
|---|
| 134 |             { | 
|---|
| 135 |                 continue; | 
|---|
| 136 |             } | 
|---|
| 137 |             else                            // error reported | 
|---|
| 138 |             { | 
|---|
| 139 |                 hal_remote_sw( XPTR( th_cxy , &th_ptr->ioc_cmd.error ) , 1 ); | 
|---|
| 140 |                 break; | 
|---|
| 141 |             } | 
|---|
| 142 |         } | 
|---|
| 143 |     } | 
|---|
| 144 |     else                                            // descheduling + IRQ policy | 
|---|
| 145 |     {  | 
|---|
| 146 |         thread_block( XPTR( local_cxy , CURRENT_THREAD ) , THREAD_BLOCKED_ISR ); | 
|---|
| 147 |         sched_yield("blocked on ISR"); | 
|---|
| 148 |  | 
|---|
| 149 |         // the IO operation status is reported in the command by the ISR | 
|---|
| 150 |     } | 
|---|
| 151 |      | 
|---|
| 152 | #if DEBUG_HAL_IOC_RX | 
|---|
| 153 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 154 | if( (DEBUG_HAL_IOC_RX < cycle) && (cmd_type != TXT_WRITE) ) | 
|---|
| 155 | printk("\n[DBG] %s : thread %x exit after RX / cycle %d\n", | 
|---|
| 156 | __FUNCTION__ , CURRENT_THREAD , cycle ); | 
|---|
| 157 | #endif | 
|---|
| 158 |  | 
|---|
| 159 | #if DEBUG_HAL_IOC_TX | 
|---|
| 160 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 161 | if( (DEBUG_HAL_IOC_TX < cycle) && (cmd_type == TXT_WRITE) ) | 
|---|
| 162 | printk("\n[DBG] %s : thread %x exit after TX / cycle %d\n", | 
|---|
| 163 | __FUNCTION__ , CURRENT_THREAD , cycle ); | 
|---|
| 164 | #endif | 
|---|
| 165 |  | 
|---|
| 166 | } // end soclib_bdv_cmd() | 
|---|
| 167 |  | 
|---|
| 168 |  | 
|---|
| 169 | ///////////////////////////////////////////////////////////////// | 
|---|
| 170 | void __attribute__ ((noinline)) soclib_bdv_isr( chdev_t * chdev ) | 
|---|
| 171 | { | 
|---|
| 172 |     error_t  error = 0; | 
|---|
| 173 |  | 
|---|
| 174 |     // get extended pointer on client thread | 
|---|
| 175 |     xptr_t root = XPTR( local_cxy , &chdev->wait_root ); | 
|---|
| 176 |     xptr_t client_xp = XLIST_FIRST_ELEMENT( root , thread_t , wait_list ); | 
|---|
| 177 |  | 
|---|
| 178 |     // get extended pointer on server thread | 
|---|
| 179 |     xptr_t server_xp = XPTR( local_cxy , &chdev->server ); | 
|---|
| 180 |  | 
|---|
| 181 |     // get client thread cluster and local pointer | 
|---|
| 182 |     cxy_t      client_cxy = GET_CXY( client_xp ); | 
|---|
| 183 |     thread_t * client_ptr = (thread_t *)GET_PTR( client_xp ); | 
|---|
| 184 |  | 
|---|
| 185 |     // get command type | 
|---|
| 186 |     uint32_t   cmd_type = hal_remote_lw( XPTR( client_cxy , &client_ptr->ioc_cmd.type ) ); | 
|---|
| 187 |      | 
|---|
| 188 |     // get SOCLIB_BDV device cluster and local pointer | 
|---|
| 189 |     cxy_t      bdv_cxy  = GET_CXY( chdev->base ); | 
|---|
| 190 |     uint32_t * bdv_ptr  = (uint32_t *)GET_PTR( chdev->base ); | 
|---|
| 191 |  | 
|---|
| 192 |     // get BDV status register and acknowledge IRQ | 
|---|
| 193 |         uint32_t status = hal_remote_lw( XPTR( bdv_cxy , bdv_ptr + BDV_STATUS_REG ) );    | 
|---|
| 194 |  | 
|---|
| 195 |     if( cmd_type == IOC_READ ) | 
|---|
| 196 |     { | 
|---|
| 197 |             error = (status != BDV_READ_SUCCESS); | 
|---|
| 198 |  | 
|---|
| 199 | #if DEBUG_HAL_IOC_RX | 
|---|
| 200 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 201 | if( DEBUG_HAL_IOC_RX < cycle ) | 
|---|
| 202 | printk("\n[DBG] %s : IOC_IRQ / RX transfer / client %x / server %x / cycle %d\n", | 
|---|
| 203 | __FUNCTION__, client_ptr , chdev->server , cycle ); | 
|---|
| 204 | #endif | 
|---|
| 205 |  | 
|---|
| 206 |     } | 
|---|
| 207 |         else if( cmd_type == IOC_WRITE ) | 
|---|
| 208 |     { | 
|---|
| 209 |             error = (status != BDV_WRITE_SUCCESS); | 
|---|
| 210 |  | 
|---|
| 211 | #if DEBUG_HAL_IOC_TX | 
|---|
| 212 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 213 | if( DEBUG_HAL_IOC_TX < cycle ) | 
|---|
| 214 | printk("\n[DBG] %s : IOC_IRQ / RX transfer / client %x / server %x / cycle %d\n", | 
|---|
| 215 | __FUNCTION__, client_ptr , chdev->server , cycle ); | 
|---|
| 216 | #endif | 
|---|
| 217 |  | 
|---|
| 218 |     } | 
|---|
| 219 |     else | 
|---|
| 220 |     { | 
|---|
| 221 |         assert( false , __FUNCTION__ , "IOC_SYNC_READ should not use IRQ" ); | 
|---|
| 222 |     }  | 
|---|
| 223 |  | 
|---|
| 224 |     // set operation status in command | 
|---|
| 225 |     hal_remote_sw( XPTR( client_cxy , &client_ptr->ioc_cmd.error ) , error ); | 
|---|
| 226 |  | 
|---|
| 227 |     // unblock server thread  | 
|---|
| 228 |     thread_unblock( server_xp , THREAD_BLOCKED_ISR ); | 
|---|
| 229 |  | 
|---|
| 230 | } // end soclib_bdv_isr() | 
|---|
| 231 |  | 
|---|
| 232 |  | 
|---|
| 233 |  | 
|---|