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