| 1 | /* |
|---|
| 2 | * dev_ioc.c - IOC (Block Device Controler) generic device API implementation. |
|---|
| 3 | * |
|---|
| 4 | * Author Alain Greiner (2016,2017,2018,2019) |
|---|
| 5 | * |
|---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
|---|
| 7 | * |
|---|
| 8 | * This file is part of ALMOS-MK |
|---|
| 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 <kernel_config.h> |
|---|
| 25 | #include <hal_kernel_types.h> |
|---|
| 26 | #include <hal_gpt.h> |
|---|
| 27 | #include <hal_drivers.h> |
|---|
| 28 | #include <thread.h> |
|---|
| 29 | #include <printk.h> |
|---|
| 30 | #include <chdev.h> |
|---|
| 31 | #include <dev_ioc.h> |
|---|
| 32 | |
|---|
| 33 | ///////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 34 | // Extern global variables |
|---|
| 35 | ///////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 36 | |
|---|
| 37 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
|---|
| 38 | |
|---|
| 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 | |
|---|
| 49 | ////////////////////////////////// |
|---|
| 50 | void dev_ioc_init( chdev_t * ioc ) |
|---|
| 51 | { |
|---|
| 52 | // get channel from chdev descriptor |
|---|
| 53 | uint32_t channel = ioc->channel; |
|---|
| 54 | |
|---|
| 55 | // set chdev name |
|---|
| 56 | snprintf( ioc->name , 16 , "ioc%d" , channel ); |
|---|
| 57 | |
|---|
| 58 | // call driver init function |
|---|
| 59 | hal_drivers_ioc_init( ioc ); |
|---|
| 60 | |
|---|
| 61 | // select a core to execute the IOC server thread |
|---|
| 62 | lid_t lid = cluster_select_local_core( local_cxy ); |
|---|
| 63 | |
|---|
| 64 | // bind the IOC IRQ to the selected core |
|---|
| 65 | dev_pic_bind_irq( lid , ioc ); |
|---|
| 66 | |
|---|
| 67 | // enable IRQ |
|---|
| 68 | dev_pic_enable_irq( lid , XPTR( local_cxy , ioc ) ); |
|---|
| 69 | |
|---|
| 70 | // create server thread |
|---|
| 71 | thread_t * new_thread; |
|---|
| 72 | error_t error; |
|---|
| 73 | |
|---|
| 74 | error = thread_kernel_create( &new_thread, |
|---|
| 75 | THREAD_DEV, |
|---|
| 76 | &chdev_server_func, |
|---|
| 77 | ioc, |
|---|
| 78 | lid ); |
|---|
| 79 | |
|---|
| 80 | assert( (error == 0) , "cannot create server thread" ); |
|---|
| 81 | |
|---|
| 82 | // set "server" field in chdev descriptor |
|---|
| 83 | ioc->server = new_thread; |
|---|
| 84 | |
|---|
| 85 | // set "chdev" field in thread descriptor |
|---|
| 86 | new_thread->chdev = ioc; |
|---|
| 87 | |
|---|
| 88 | // unblock server thread |
|---|
| 89 | thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL ); |
|---|
| 90 | |
|---|
| 91 | } // end dev_ioc_init() |
|---|
| 92 | |
|---|
| 93 | /////////////////////////////////////////////// |
|---|
| 94 | error_t dev_ioc_move_data( uint32_t cmd_type, |
|---|
| 95 | xptr_t buffer_xp, |
|---|
| 96 | uint32_t lba, |
|---|
| 97 | uint32_t count ) |
|---|
| 98 | { |
|---|
| 99 | thread_t * this = CURRENT_THREAD; // pointer on client thread |
|---|
| 100 | |
|---|
| 101 | #if ( DEBUG_DEV_IOC_RX || DEBUG_DEV_IOC_TX ) |
|---|
| 102 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 103 | #endif |
|---|
| 104 | |
|---|
| 105 | // software L2/L3 cache coherence for memory buffer |
|---|
| 106 | if( chdev_dir.iob ) |
|---|
| 107 | { |
|---|
| 108 | if( (cmd_type == IOC_SYNC_READ) || (cmd_type == IOC_READ) ) |
|---|
| 109 | { |
|---|
| 110 | dev_mmc_inval( buffer_xp , count<<9 ); |
|---|
| 111 | } |
|---|
| 112 | else // (cmd_type == IOC_SYNC_WRITE) or (cmd_type == IOC_WRITE) |
|---|
| 113 | { |
|---|
| 114 | dev_mmc_sync ( buffer_xp , count<<9 ); |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | // get extended pointer on IOC chdev descriptor |
|---|
| 119 | xptr_t ioc_xp = chdev_dir.ioc[0]; |
|---|
| 120 | |
|---|
| 121 | // check dev_xp |
|---|
| 122 | assert( (ioc_xp != XPTR_NULL) , "undefined IOC chdev descriptor" ); |
|---|
| 123 | |
|---|
| 124 | // register command in calling thread descriptor |
|---|
| 125 | this->ioc_cmd.dev_xp = ioc_xp; |
|---|
| 126 | this->ioc_cmd.type = cmd_type; |
|---|
| 127 | this->ioc_cmd.buf_xp = buffer_xp; |
|---|
| 128 | this->ioc_cmd.lba = lba; |
|---|
| 129 | this->ioc_cmd.count = count; |
|---|
| 130 | |
|---|
| 131 | // for a synchronous acces, the driver is directly called by the client thread |
|---|
| 132 | if( (cmd_type == IOC_SYNC_READ) || (cmd_type == IOC_SYNC_WRITE) ) |
|---|
| 133 | { |
|---|
| 134 | |
|---|
| 135 | #if DEBUG_DEV_IOC_RX |
|---|
| 136 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 137 | if( (DEBUG_DEV_IOC_RX < cycle) && (cmd_type == IOC_SYNC_READ) ) |
|---|
| 138 | printk("\n[%s] thread[%x,%x] enters for SYNC_READ / lba %x / buffer[%x,%x] / cycle %d\n", |
|---|
| 139 | __FUNCTION__ , this->process->pid, this->trdid, lba, |
|---|
| 140 | GET_CXY(buffer_xp), GET_PTR(buffer_xp), cycle ); |
|---|
| 141 | #endif |
|---|
| 142 | |
|---|
| 143 | #if DEBUG_DEV_IOC_TX |
|---|
| 144 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 145 | if( (DEBUG_DEV_IOC_TX < cycle) && (cmd_type == IOC_SYNC_WRITE) ) |
|---|
| 146 | printk("\n[%s] thread[%x,%x] enters for SYNC_WRITE / lba %x / buffer[%x,%x] / cycle %d\n", |
|---|
| 147 | __FUNCTION__ , this->process->pid, this->trdid, lba, |
|---|
| 148 | GET_CXY(buffer_xp), GET_PTR(buffer_xp), cycle ); |
|---|
| 149 | #endif |
|---|
| 150 | // get driver command function |
|---|
| 151 | cxy_t ioc_cxy = GET_CXY( ioc_xp ); |
|---|
| 152 | chdev_t * ioc_ptr = GET_PTR( ioc_xp ); |
|---|
| 153 | dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( ioc_cxy , &ioc_ptr->cmd ) ); |
|---|
| 154 | |
|---|
| 155 | // call driver function |
|---|
| 156 | cmd( XPTR( local_cxy , this ) ); |
|---|
| 157 | |
|---|
| 158 | #if DEBUG_DEV_IOC_RX |
|---|
| 159 | if( (DEBUG_DEV_IOC_RX < cycle) && (cmd_type == IOC_SYNC_READ) ) |
|---|
| 160 | printk("\n[%s] thread[%x,%x] resumes for IOC_SYNC_READ\n", |
|---|
| 161 | __FUNCTION__, this->process->pid , this->trdid ) |
|---|
| 162 | #endif |
|---|
| 163 | |
|---|
| 164 | #if DEBUG_DEV_IOC_TX |
|---|
| 165 | if( (DEBUG_DEV_IOC_RX < cycle) && (cmd_type == IOC_SYNC_WRITE) ) |
|---|
| 166 | printk("\n[%s] thread[%x,%x] resumes for IOC_SYNC_WRITE\n", |
|---|
| 167 | __FUNCTION__, this->process->pid , this->trdid ) |
|---|
| 168 | #endif |
|---|
| 169 | |
|---|
| 170 | } |
|---|
| 171 | // for an asynchronous access, the client thread registers in the chdev waiting queue, |
|---|
| 172 | // activates server thread, blocks on THREAD_BLOCKED_IO and deschedules. |
|---|
| 173 | // It is re-activated by the server thread after IO operation completion. |
|---|
| 174 | else // (cmd_type == IOC_READ) || (cmd_type == IOC_WRITE) |
|---|
| 175 | { |
|---|
| 176 | |
|---|
| 177 | #if DEBUG_DEV_IOC_RX |
|---|
| 178 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 179 | if( (DEBUG_DEV_IOC_RX < cycle) && (cmd_type == IOC_READ) ) |
|---|
| 180 | printk("\n[%s] thread[%x,%x] enters for READ / lba %x / buffer[%x,%x] / cycle %d\n", |
|---|
| 181 | __FUNCTION__ , this->process->pid, this->trdid, lba, |
|---|
| 182 | GET_CXY(buffer_xp), GET_PTR(buffer_xp), cycle ); |
|---|
| 183 | #endif |
|---|
| 184 | |
|---|
| 185 | #if DEBUG_DEV_IOC_TX |
|---|
| 186 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 187 | if( (DEBUG_DEV_IOC_TX < cycle) && (cmd_type == IOC_WRITE) ) |
|---|
| 188 | printk("\n[%s] thread[%x,%x] enters for WRITE / lba %x / buffer[%x,%x] / cycle %d\n", |
|---|
| 189 | __FUNCTION__ , this->process->pid, this->trdid, lba, |
|---|
| 190 | GET_CXY(buffer_xp), GET_PTR(buffer_xp), cycle ); |
|---|
| 191 | #endif |
|---|
| 192 | chdev_register_command( ioc_xp ); |
|---|
| 193 | |
|---|
| 194 | #if(DEBUG_DEV_IOC_RX ) |
|---|
| 195 | if( (DEBUG_DEV_IOC_RX < cycle) && (cmd_type == IOC_READ) ) |
|---|
| 196 | printk("\n[%s] thread[%x,%x] resumes for IOC_READ\n", |
|---|
| 197 | __FUNCTION__, this->process->pid , this->trdid ) |
|---|
| 198 | #endif |
|---|
| 199 | |
|---|
| 200 | #if(DEBUG_DEV_IOC_TX & 1) |
|---|
| 201 | if( (DEBUG_DEV_IOC_RX < cycle) && (cmd_type == IOC_WRITE) ) |
|---|
| 202 | printk("\n[%s] thread[%x,%x] resumes for IOC_WRITE\n", |
|---|
| 203 | __FUNCTION__, this->process->pid , this->trdid ) |
|---|
| 204 | #endif |
|---|
| 205 | |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | // return I/O operation status |
|---|
| 209 | return this->ioc_cmd.error; |
|---|
| 210 | |
|---|
| 211 | } // end dev_ioc_move_data() |
|---|
| 212 | |
|---|
| 213 | |
|---|