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