| 1 | /* |
|---|
| 2 | * dev_ioc.c - IOC (Block Device Controler) generic device API implementation. |
|---|
| 3 | * |
|---|
| 4 | * Author Alain Greiner (2016) |
|---|
| 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-kernel; 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_types.h> |
|---|
| 26 | #include <hal_gpt.h> |
|---|
| 27 | #include <soclib_bdv.h> |
|---|
| 28 | #include <soclib_hba.h> |
|---|
| 29 | //#include <soclib_sdc.h> |
|---|
| 30 | //#include <soclib_spi.h> |
|---|
| 31 | //#include <soclib_rdk.h> |
|---|
| 32 | #include <thread.h> |
|---|
| 33 | #include <printk.h> |
|---|
| 34 | #include <chdev.h> |
|---|
| 35 | #include <dev_ioc.h> |
|---|
| 36 | |
|---|
| 37 | ///////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 38 | // Extern global variables |
|---|
| 39 | ///////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 40 | |
|---|
| 41 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
|---|
| 42 | |
|---|
| 43 | ////////////////////////////////// |
|---|
| 44 | void dev_ioc_init( chdev_t * ioc ) |
|---|
| 45 | { |
|---|
| 46 | // the PIC chdev must be initialized before the IOC chdev, because |
|---|
| 47 | // the IOC chdev initialisation requires the routing of an external IRQ |
|---|
| 48 | xptr_t pic_xp = chdev_dir.pic; |
|---|
| 49 | |
|---|
| 50 | assert( (pic_xp != XPTR_NULL) , __FUNCTION__ , "PIC not initialised before IOC" ); |
|---|
| 51 | |
|---|
| 52 | // get implementation and channel from chdev descriptor |
|---|
| 53 | uint32_t impl = ioc->impl; |
|---|
| 54 | uint32_t channel = ioc->channel; |
|---|
| 55 | |
|---|
| 56 | // set chdev name |
|---|
| 57 | snprintf( ioc->name , 16 , "ioc_%d" , channel ); |
|---|
| 58 | |
|---|
| 59 | // set driver specific fields in chdev descriptor and call driver init function |
|---|
| 60 | if( impl == IMPL_IOC_BDV ) |
|---|
| 61 | { |
|---|
| 62 | ioc->cmd = &soclib_bdv_cmd; |
|---|
| 63 | ioc->isr = &soclib_bdv_isr; |
|---|
| 64 | soclib_bdv_init( ioc ); |
|---|
| 65 | } |
|---|
| 66 | else if( impl == IMPL_IOC_HBA ) |
|---|
| 67 | { |
|---|
| 68 | ioc->cmd = &soclib_hba_cmd; |
|---|
| 69 | ioc->isr = &soclib_hba_isr; |
|---|
| 70 | soclib_hba_init( ioc ); |
|---|
| 71 | } |
|---|
| 72 | else |
|---|
| 73 | { |
|---|
| 74 | assert( false , __FUNCTION__ , "undefined IOC device implementation" ); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | // select a core to execute the IOC server thread |
|---|
| 78 | lid_t lid = cluster_select_local_core(); |
|---|
| 79 | |
|---|
| 80 | // bind the IOC IRQ to the selected core |
|---|
| 81 | dev_pic_bind_irq( lid , ioc ); |
|---|
| 82 | |
|---|
| 83 | // enable IRQ |
|---|
| 84 | dev_pic_enable_irq( lid ,ioc ); |
|---|
| 85 | |
|---|
| 86 | // create server thread |
|---|
| 87 | thread_t * new_thread; |
|---|
| 88 | error_t error; |
|---|
| 89 | |
|---|
| 90 | error = thread_kernel_create( &new_thread, |
|---|
| 91 | THREAD_DEV, |
|---|
| 92 | &chdev_sequencial_server, |
|---|
| 93 | ioc, |
|---|
| 94 | lid ); |
|---|
| 95 | |
|---|
| 96 | assert( (error == 0) , __FUNCTION__ , "cannot create server thread" ); |
|---|
| 97 | |
|---|
| 98 | // set "server" field in ioc descriptor |
|---|
| 99 | ioc->server = new_thread; |
|---|
| 100 | |
|---|
| 101 | // start server thread |
|---|
| 102 | thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL ); |
|---|
| 103 | |
|---|
| 104 | } // end dev_ioc_init() |
|---|
| 105 | |
|---|
| 106 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 107 | // This static function is called by dev_ioc_read() & dev_ioc_write() functions. |
|---|
| 108 | // It builds and registers the command in the calling thread descriptor. |
|---|
| 109 | // Then, it registers the calling thead in chdev waiting queue. |
|---|
| 110 | // Finally it blocks on the THREAD_BLOCKED_DEV condition and deschedule. |
|---|
| 111 | ////////////////////////////////////i///////////////////////////////////////////// |
|---|
| 112 | static error_t dev_ioc_access( uint32_t cmd_type, |
|---|
| 113 | uint8_t * buffer, |
|---|
| 114 | uint32_t lba, |
|---|
| 115 | uint32_t count ) |
|---|
| 116 | { |
|---|
| 117 | thread_t * this = CURRENT_THREAD; // pointer on client thread |
|---|
| 118 | |
|---|
| 119 | ioc_dmsg("\n[INFO] in %s : thread %x in process %x" |
|---|
| 120 | " for lba = %x / buffer = %x / at cycle %d\n", |
|---|
| 121 | __FUNCTION__ , this->trdid , this->process->pid , |
|---|
| 122 | lba , (intptr_t)buffer , hal_get_cycles() ); |
|---|
| 123 | |
|---|
| 124 | // software L2/L3 cache coherence for memory buffer |
|---|
| 125 | if( chdev_dir.iob ) |
|---|
| 126 | { |
|---|
| 127 | if ( cmd_type == IOC_READ ) dev_mmc_inval( XPTR( local_cxy , buffer ) , count<<9 ); |
|---|
| 128 | else dev_mmc_sync ( XPTR( local_cxy , buffer ) , count<<9 ); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | // get extended pointer on IOC chdev descriptor |
|---|
| 132 | xptr_t dev_xp = chdev_dir.ioc[0]; |
|---|
| 133 | |
|---|
| 134 | assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined IOC chdev descriptor" ); |
|---|
| 135 | |
|---|
| 136 | // register command in calling thread descriptor |
|---|
| 137 | this->command.ioc.dev_xp = dev_xp; |
|---|
| 138 | this->command.ioc.type = cmd_type; |
|---|
| 139 | this->command.ioc.buf_xp = XPTR( local_cxy , buffer ); |
|---|
| 140 | this->command.ioc.lba = lba; |
|---|
| 141 | this->command.ioc.count = count; |
|---|
| 142 | |
|---|
| 143 | // register client thread in IOC chdev waiting queue, activate server thread, |
|---|
| 144 | // block client thread on THREAD_BLOCKED_IO and deschedule. |
|---|
| 145 | // it is re-activated by the ISR signaling IO operation completion. |
|---|
| 146 | chdev_register_command( dev_xp , this ); |
|---|
| 147 | |
|---|
| 148 | ioc_dmsg("\n[INFO] in %s : thread %x in process %x" |
|---|
| 149 | " completes / error = %d / at cycle %d\n", |
|---|
| 150 | __FUNCTION__ , this->trdid , this->process->pid , |
|---|
| 151 | this->command.ioc.error , hal_get_cycles() ); |
|---|
| 152 | |
|---|
| 153 | // return I/O operation status |
|---|
| 154 | return this->command.ioc.error; |
|---|
| 155 | |
|---|
| 156 | } // end dev_ioc_access() |
|---|
| 157 | |
|---|
| 158 | //////////////////////////////////////////// |
|---|
| 159 | error_t dev_ioc_read( uint8_t * buffer, |
|---|
| 160 | uint32_t lba, |
|---|
| 161 | uint32_t count ) |
|---|
| 162 | { |
|---|
| 163 | return dev_ioc_access( IOC_READ , buffer , lba , count ); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | //////////////////////////////////////////// |
|---|
| 167 | error_t dev_ioc_write( uint8_t * buffer, |
|---|
| 168 | uint32_t lba, |
|---|
| 169 | uint32_t count ) |
|---|
| 170 | { |
|---|
| 171 | return dev_ioc_access( IOC_WRITE , buffer , lba , count ); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | ///////////////////////////////////////////// |
|---|
| 175 | error_t dev_ioc_sync_read( uint8_t * buffer, |
|---|
| 176 | uint32_t lba, |
|---|
| 177 | uint32_t count ) |
|---|
| 178 | { |
|---|
| 179 | // get pointer on calling thread |
|---|
| 180 | thread_t * this = CURRENT_THREAD; |
|---|
| 181 | |
|---|
| 182 | // software L2/L3 cache coherence for memory buffer |
|---|
| 183 | if( chdev_dir.iob ) dev_mmc_inval( XPTR( local_cxy , buffer ) , count<<9 ); |
|---|
| 184 | |
|---|
| 185 | // get extended pointer on IOC[0] chdev |
|---|
| 186 | xptr_t dev_xp = chdev_dir.ioc[0]; |
|---|
| 187 | |
|---|
| 188 | assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined IOC chdev descriptor" ); |
|---|
| 189 | |
|---|
| 190 | // register command in calling thread descriptor |
|---|
| 191 | this->command.ioc.dev_xp = dev_xp; |
|---|
| 192 | this->command.ioc.type = IOC_SYNC_READ; |
|---|
| 193 | this->command.ioc.buf_xp = XPTR( local_cxy , buffer ); |
|---|
| 194 | this->command.ioc.lba = lba; |
|---|
| 195 | this->command.ioc.count = count; |
|---|
| 196 | |
|---|
| 197 | // get driver command function |
|---|
| 198 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
|---|
| 199 | chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); |
|---|
| 200 | dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->cmd ) ); |
|---|
| 201 | |
|---|
| 202 | // call directly driver command |
|---|
| 203 | cmd( XPTR( local_cxy , this ) ); |
|---|
| 204 | |
|---|
| 205 | // return I/O operation status from calling thread descriptor |
|---|
| 206 | return this->command.ioc.error; |
|---|
| 207 | } |
|---|
| 208 | |
|---|