| 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 | extern chdev_pic_input_t chdev_pic_input; // allocated in kernel_init.c |
|---|
| 44 | |
|---|
| 45 | //////////////////////////////////// |
|---|
| 46 | void dev_ioc_init( chdev_t * chdev ) |
|---|
| 47 | { |
|---|
| 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" ); |
|---|
| 52 | |
|---|
| 53 | // get implementation and channel from chdev descriptor |
|---|
| 54 | uint32_t impl = chdev->impl; |
|---|
| 55 | uint32_t channel = chdev->channel; |
|---|
| 56 | |
|---|
| 57 | // set driver specific fields in chdev descriptor and call driver init function |
|---|
| 58 | if( impl == IMPL_IOC_BDV ) |
|---|
| 59 | { |
|---|
| 60 | chdev->cmd = &soclib_bdv_cmd; |
|---|
| 61 | chdev->isr = &soclib_bdv_isr; |
|---|
| 62 | soclib_bdv_init( chdev ); |
|---|
| 63 | } |
|---|
| 64 | else if( impl == IMPL_IOC_HBA ) |
|---|
| 65 | { |
|---|
| 66 | chdev->cmd = &soclib_hba_command; |
|---|
| 67 | chdev->isr = &soclib_hba_isr; |
|---|
| 68 | soclib_hba_init( chdev ); |
|---|
| 69 | } |
|---|
| 70 | else |
|---|
| 71 | { |
|---|
| 72 | assert( false , __FUNCTION__ , "undefined IOC device implementation" ); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | // get a WTI mailbox from local ICU |
|---|
| 76 | uint32_t wti_id = dev_icu_wti_alloc(); |
|---|
| 77 | |
|---|
| 78 | assert( (wti_id != -1) , __FUNCTION__ , "cannot allocate WTI mailbox" ); |
|---|
| 79 | |
|---|
| 80 | // select a core |
|---|
| 81 | lid_t lid = cluster_select_local_core(); |
|---|
| 82 | |
|---|
| 83 | // enable WTI IRQ and update WTI interrupt vector |
|---|
| 84 | dev_icu_enable_irq( lid , WTI_TYPE , wti_id , chdev ); |
|---|
| 85 | |
|---|
| 86 | // link IOC IRQ to WTI mailbox in PIC component |
|---|
| 87 | uint32_t irq_id = chdev_pic_input.ioc[channel]; |
|---|
| 88 | dev_pic_bind_irq( irq_id , local_cxy , wti_id ); |
|---|
| 89 | |
|---|
| 90 | // create server thread |
|---|
| 91 | thread_t * new_thread; |
|---|
| 92 | error_t error; |
|---|
| 93 | |
|---|
| 94 | error = thread_kernel_create( &new_thread, |
|---|
| 95 | THREAD_DEV, |
|---|
| 96 | &chdev_sequencial_server, |
|---|
| 97 | chdev, |
|---|
| 98 | lid ); |
|---|
| 99 | assert( (error == 0) , __FUNCTION__ , "cannot create server thread" ); |
|---|
| 100 | |
|---|
| 101 | // set "server" field in chdev descriptor |
|---|
| 102 | chdev->server = new_thread; |
|---|
| 103 | |
|---|
| 104 | // start server thread |
|---|
| 105 | thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL ); |
|---|
| 106 | |
|---|
| 107 | } // end dev_ioc_init() |
|---|
| 108 | |
|---|
| 109 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 110 | // This static function is called by dev_ioc_read() & dev_ioc_write() functions. |
|---|
| 111 | // It builds and registers the command in the calling thread descriptor, after |
|---|
| 112 | // translation of buffer virtual address to physical address. |
|---|
| 113 | // Then, it registers the calling thead in chdev waiting queue. |
|---|
| 114 | // Finally it blocks on the THREAD_BLOCKED_DEV condition and deschedule. |
|---|
| 115 | ////////////////////////////////////i///////////////////////////////////////////// |
|---|
| 116 | static error_t dev_ioc_access( bool_t to_mem, |
|---|
| 117 | char * buffer, |
|---|
| 118 | uint32_t lba, |
|---|
| 119 | uint32_t count ) |
|---|
| 120 | { |
|---|
| 121 | thread_t * this = CURRENT_THREAD; // pointer on client thread |
|---|
| 122 | |
|---|
| 123 | error_t error; |
|---|
| 124 | paddr_t buf_paddr; |
|---|
| 125 | |
|---|
| 126 | // Get buffer physical address |
|---|
| 127 | error = vmm_v2p_translate( CONFIG_KERNEL_IDENTITY , buffer , &buf_paddr ); |
|---|
| 128 | |
|---|
| 129 | if( error ) return EINVAL; |
|---|
| 130 | |
|---|
| 131 | ioc_dmsg("\n[INFO] in %s : thread %x in process %x" |
|---|
| 132 | " for lba = %x / vaddr = %x / paddr = %l / at cycle %d\n", |
|---|
| 133 | __FUNCTION__ , this->trdid , this->process->pid , |
|---|
| 134 | lba , (uint32_t)buffer , buf_paddr , hal_time_stamp() ); |
|---|
| 135 | |
|---|
| 136 | #if USE_IOB // software L2/L3 cache coherence for memory buffer |
|---|
| 137 | |
|---|
| 138 | if ( to_mem ) dev_mmc_inval( buf_paddr, count<<9 ); |
|---|
| 139 | else dev_mmc_sync( buf_paddr, count<<9 ); |
|---|
| 140 | |
|---|
| 141 | #endif // end software L2/L3 cache coherence |
|---|
| 142 | |
|---|
| 143 | // get extended pointer on IOC chdev descriptor |
|---|
| 144 | xptr_t dev_xp = chdev_dir.ioc[0]; |
|---|
| 145 | |
|---|
| 146 | assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined IOC chdev descriptor" ); |
|---|
| 147 | |
|---|
| 148 | // register command in calling thread descriptor |
|---|
| 149 | this->command.ioc.dev_xp = dev_xp; |
|---|
| 150 | this->command.ioc.to_mem = to_mem; |
|---|
| 151 | this->command.ioc.buf_xp = XPTR( local_cxy , buffer ); |
|---|
| 152 | this->command.ioc.lba = lba; |
|---|
| 153 | this->command.ioc.count = count; |
|---|
| 154 | |
|---|
| 155 | // register client thread in IOC chdev waiting queue, activate server thread, |
|---|
| 156 | // block client thread on THREAD_BLOCKED_IO and deschedule. |
|---|
| 157 | // it is re-activated by the ISR signaling IO operation completion. |
|---|
| 158 | chdev_register_command( dev_xp , this ); |
|---|
| 159 | |
|---|
| 160 | ioc_dmsg("\n[INFO] in %s : thread %x in process %x" |
|---|
| 161 | " completes / error = %d / at cycle %d\n", |
|---|
| 162 | __FUNCTION__ , this->trdid , this->process->pid , |
|---|
| 163 | this->dev.ioc.error , hal_time_stamp() ); |
|---|
| 164 | |
|---|
| 165 | // return I/O operation status |
|---|
| 166 | return this->command.ioc.error; |
|---|
| 167 | |
|---|
| 168 | } // end dev_ioc_access() |
|---|
| 169 | |
|---|
| 170 | //////////////////////////////////////////// |
|---|
| 171 | error_t dev_ioc_read( char * buffer, |
|---|
| 172 | uint32_t lba, |
|---|
| 173 | uint32_t count ) |
|---|
| 174 | { |
|---|
| 175 | return dev_ioc_access( true , buffer , lba , count ); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | //////////////////////////////////////////// |
|---|
| 179 | error_t dev_ioc_write( char * buffer, |
|---|
| 180 | uint32_t lba, |
|---|
| 181 | uint32_t count ) |
|---|
| 182 | { |
|---|
| 183 | return dev_ioc_access( false , buffer , lba , count ); |
|---|
| 184 | } |
|---|