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