| [1] | 1 | /* | 
|---|
|  | 2 | * dev_mmc.c - MMC (Memory Cache 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 <hal_types.h> | 
|---|
|  | 25 | #include <hal_special.h> | 
|---|
| [257] | 26 | #include <hal_drivers.h> | 
|---|
| [1] | 27 | #include <printk.h> | 
|---|
| [3] | 28 | #include <chdev.h> | 
|---|
| [1] | 29 | #include <thread.h> | 
|---|
|  | 30 | #include <dev_mmc.h> | 
|---|
|  | 31 |  | 
|---|
|  | 32 | ///////////////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 33 | // Extern global variables | 
|---|
|  | 34 | ///////////////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 35 |  | 
|---|
| [3] | 36 | extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c | 
|---|
| [1] | 37 |  | 
|---|
| [238] | 38 | ////////////////////////////////// | 
|---|
|  | 39 | void dev_mmc_init( chdev_t * mmc ) | 
|---|
| [1] | 40 | { | 
|---|
|  | 41 | // get implementation from device descriptor | 
|---|
| [238] | 42 | uint32_t  impl = mmc->impl; | 
|---|
| [1] | 43 |  | 
|---|
| [238] | 44 | // set mmc name | 
|---|
|  | 45 | snprintf( mmc->name , 16 , "mmc_%x" , local_cxy ); | 
|---|
| [23] | 46 |  | 
|---|
| [257] | 47 | // call driver init function | 
|---|
|  | 48 | hal_drivers_mmc_init( mmc , impl ); | 
|---|
| [14] | 49 |  | 
|---|
| [188] | 50 | // bind IRQ to CP0 | 
|---|
| [238] | 51 | dev_pic_bind_irq( 0 , mmc ); | 
|---|
| [188] | 52 |  | 
|---|
|  | 53 | // enable IRQ | 
|---|
| [238] | 54 | dev_pic_enable_irq( 0 , XPTR( local_cxy , mmc ) ); | 
|---|
| [14] | 55 |  | 
|---|
| [1] | 56 | }  // end dev_mmc_init() | 
|---|
|  | 57 |  | 
|---|
|  | 58 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| [23] | 59 | // This static function is called by all MMC device functions. | 
|---|
|  | 60 | // It makes some checking, takes the lock granting exclusive | 
|---|
|  | 61 | // access to MMC peripheral, call the driver to execute the command | 
|---|
| [1] | 62 | // registered in the calling thread descriptor, and releases the lock. | 
|---|
|  | 63 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| [23] | 64 | static error_t dev_mmc_access( thread_t * this ) | 
|---|
| [1] | 65 | { | 
|---|
| [23] | 66 | // get extended pointer on MMC device descriptor | 
|---|
| [279] | 67 | xptr_t  dev_xp = this->mmc_cmd.dev_xp; | 
|---|
| [1] | 68 |  | 
|---|
| [23] | 69 | assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "target MMC device undefined" ); | 
|---|
| [1] | 70 |  | 
|---|
|  | 71 | // get MMC device cluster identifier & local pointer | 
|---|
| [3] | 72 | cxy_t     dev_cxy = GET_CXY( dev_xp ); | 
|---|
|  | 73 | chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); | 
|---|
| [1] | 74 |  | 
|---|
|  | 75 | // get driver command function pointer from MMC device descriptor | 
|---|
|  | 76 | dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->cmd ) ); | 
|---|
|  | 77 |  | 
|---|
|  | 78 | // get the MMC device remote spinlock | 
|---|
|  | 79 | remote_spinlock_lock( XPTR( dev_cxy , &dev_ptr->wait_lock ) ); | 
|---|
|  | 80 |  | 
|---|
|  | 81 | // call driver command | 
|---|
|  | 82 | cmd( XPTR( local_cxy , this ) ); | 
|---|
|  | 83 |  | 
|---|
|  | 84 | // release the MMC device remote spinlock | 
|---|
|  | 85 | remote_spinlock_unlock( XPTR( dev_cxy , &dev_ptr->wait_lock ) ); | 
|---|
|  | 86 |  | 
|---|
| [23] | 87 | // return operation status | 
|---|
| [279] | 88 | return this->mmc_cmd.error; | 
|---|
| [1] | 89 |  | 
|---|
|  | 90 | }  // end dev_mmc_access() | 
|---|
|  | 91 |  | 
|---|
| [23] | 92 | ///////////////////////////////////////// | 
|---|
|  | 93 | error_t dev_mmc_inval( xptr_t     buf_xp, | 
|---|
| [1] | 94 | uint32_t   buf_size ) | 
|---|
|  | 95 | { | 
|---|
| [23] | 96 | error_t error; | 
|---|
|  | 97 |  | 
|---|
| [1] | 98 | // get calling thread local pointer | 
|---|
|  | 99 | thread_t * this = CURRENT_THREAD; | 
|---|
|  | 100 |  | 
|---|
| [407] | 101 | mmc_dmsg("\n[DBG] %s enters for thread %x in process %x / buf_xp = %l\n", | 
|---|
| [23] | 102 | __FUNCTION__ , this->trdid , this->process->pid , buf_xp ); | 
|---|
| [1] | 103 |  | 
|---|
| [23] | 104 | // get buffer cluster and local pointer | 
|---|
|  | 105 | cxy_t  buf_cxy = GET_CXY( buf_xp ); | 
|---|
|  | 106 | void * buf_ptr = GET_PTR( buf_xp ); | 
|---|
|  | 107 |  | 
|---|
|  | 108 | assert( (((intptr_t)buf_ptr & (CONFIG_CACHE_LINE_SIZE -1)) == 0) , __FUNCTION__ , | 
|---|
| [3] | 109 | "buffer not aligned on cache line" ); | 
|---|
| [1] | 110 |  | 
|---|
| [23] | 111 | // get buffer physical address | 
|---|
|  | 112 | paddr_t  buf_paddr; | 
|---|
|  | 113 | error = vmm_v2p_translate( CONFIG_KERNEL_IDENTITY_MAP , buf_ptr , &buf_paddr ); | 
|---|
| [1] | 114 |  | 
|---|
| [23] | 115 | assert( (error == 0) , __FUNCTION__ , "cannot get buffer paddr" ); | 
|---|
|  | 116 |  | 
|---|
| [1] | 117 | // store command arguments in thread descriptor | 
|---|
| [279] | 118 | this->mmc_cmd.dev_xp    = chdev_dir.mmc[buf_cxy]; | 
|---|
|  | 119 | this->mmc_cmd.type      = MMC_CC_INVAL; | 
|---|
|  | 120 | this->mmc_cmd.buf_paddr = buf_paddr; | 
|---|
|  | 121 | this->mmc_cmd.buf_size  = buf_size; | 
|---|
| [1] | 122 |  | 
|---|
| [23] | 123 | // call MMC driver | 
|---|
|  | 124 | error = dev_mmc_access( this ); | 
|---|
| [1] | 125 |  | 
|---|
| [407] | 126 | mmc_dmsg("\n[DBG] %s completes for thread %x in process %x / error = %d\n", | 
|---|
| [23] | 127 | __FUNCTION__ , this->trdid , this->process->pid , error ); | 
|---|
| [1] | 128 |  | 
|---|
| [23] | 129 | return error; | 
|---|
|  | 130 | } | 
|---|
|  | 131 |  | 
|---|
|  | 132 | //////////////////////////////////////// | 
|---|
|  | 133 | error_t dev_mmc_sync( xptr_t     buf_xp, | 
|---|
| [1] | 134 | uint32_t   buf_size ) | 
|---|
|  | 135 | { | 
|---|
| [23] | 136 | error_t error; | 
|---|
|  | 137 |  | 
|---|
| [1] | 138 | // get calling thread local pointer | 
|---|
|  | 139 | thread_t * this = CURRENT_THREAD; | 
|---|
|  | 140 |  | 
|---|
| [407] | 141 | mmc_dmsg("\n[DBG] %s enters for thread %x in process %x / buf_xp = %l\n", | 
|---|
| [23] | 142 | __FUNCTION__ , this->trdid , this->process->pid , buf_xp ); | 
|---|
| [1] | 143 |  | 
|---|
| [23] | 144 | // get buffer cluster and local pointer | 
|---|
|  | 145 | cxy_t  buf_cxy = GET_CXY( buf_xp ); | 
|---|
|  | 146 | void * buf_ptr = GET_PTR( buf_xp ); | 
|---|
|  | 147 |  | 
|---|
|  | 148 | assert( (((intptr_t)buf_ptr & (CONFIG_CACHE_LINE_SIZE -1)) == 0) , __FUNCTION__ , | 
|---|
| [3] | 149 | "buffer not aligned on cache line" ); | 
|---|
| [1] | 150 |  | 
|---|
| [23] | 151 | // get  buffer physical address | 
|---|
|  | 152 | paddr_t  buf_paddr; | 
|---|
|  | 153 | error = vmm_v2p_translate( CONFIG_KERNEL_IDENTITY_MAP , buf_ptr , &buf_paddr ); | 
|---|
|  | 154 |  | 
|---|
|  | 155 | assert( (error == 0) , __FUNCTION__ , "cannot get buffer paddr" ); | 
|---|
|  | 156 |  | 
|---|
| [1] | 157 | // store command arguments in thread descriptor | 
|---|
| [279] | 158 | this->mmc_cmd.dev_xp    = chdev_dir.mmc[buf_cxy]; | 
|---|
|  | 159 | this->mmc_cmd.type      = MMC_CC_SYNC; | 
|---|
|  | 160 | this->mmc_cmd.buf_paddr = buf_paddr; | 
|---|
|  | 161 | this->mmc_cmd.buf_size  = buf_size; | 
|---|
| [1] | 162 |  | 
|---|
| [23] | 163 | // call MMC driver | 
|---|
|  | 164 | error = dev_mmc_access( this ); | 
|---|
| [1] | 165 |  | 
|---|
| [407] | 166 | mmc_dmsg("\n[DBG] %s completes for thread %x in process %x / error = %d\n", | 
|---|
| [23] | 167 | __FUNCTION__ , this->trdid , this->process->pid , error ); | 
|---|
| [1] | 168 |  | 
|---|
| [23] | 169 | return error; | 
|---|
|  | 170 | } | 
|---|
|  | 171 |  | 
|---|
| [1] | 172 | ///////////////////////////////////////// | 
|---|
|  | 173 | error_t dev_mmc_set_error( cxy_t     cxy, | 
|---|
|  | 174 | uint32_t  index, | 
|---|
|  | 175 | uint32_t  wdata ) | 
|---|
|  | 176 | { | 
|---|
|  | 177 | // get calling thread local pointer | 
|---|
|  | 178 | thread_t * this = CURRENT_THREAD; | 
|---|
|  | 179 |  | 
|---|
|  | 180 | // store command arguments in thread descriptor | 
|---|
| [279] | 181 | this->mmc_cmd.dev_xp    = chdev_dir.mmc[cxy]; | 
|---|
|  | 182 | this->mmc_cmd.type      = MMC_SET_ERROR; | 
|---|
|  | 183 | this->mmc_cmd.reg_index = index; | 
|---|
|  | 184 | this->mmc_cmd.reg_ptr   = &wdata; | 
|---|
| [1] | 185 |  | 
|---|
|  | 186 | // execute operation | 
|---|
| [23] | 187 | return dev_mmc_access( this ); | 
|---|
| [1] | 188 | } | 
|---|
|  | 189 |  | 
|---|
|  | 190 | ////////////////////////////////////////// | 
|---|
|  | 191 | error_t dev_mmc_get_error( cxy_t      cxy, | 
|---|
|  | 192 | uint32_t   index, | 
|---|
|  | 193 | uint32_t * rdata ) | 
|---|
|  | 194 | { | 
|---|
|  | 195 | // get calling thread local pointer | 
|---|
|  | 196 | thread_t * this = CURRENT_THREAD; | 
|---|
|  | 197 |  | 
|---|
|  | 198 | // store command arguments in thread descriptor | 
|---|
| [279] | 199 | this->mmc_cmd.dev_xp    = chdev_dir.mmc[cxy]; | 
|---|
|  | 200 | this->mmc_cmd.type      = MMC_GET_ERROR; | 
|---|
|  | 201 | this->mmc_cmd.reg_index = index; | 
|---|
|  | 202 | this->mmc_cmd.reg_ptr   = rdata; | 
|---|
| [1] | 203 |  | 
|---|
|  | 204 | // execute operation | 
|---|
| [23] | 205 | return dev_mmc_access( this ); | 
|---|
| [1] | 206 | } | 
|---|
|  | 207 |  | 
|---|
|  | 208 | //////////////////////////////////////////////////// | 
|---|
|  | 209 | error_t dev_mmc_get_instrumentation( cxy_t      cxy, | 
|---|
|  | 210 | uint32_t   index, | 
|---|
|  | 211 | uint32_t * rdata ) | 
|---|
|  | 212 | { | 
|---|
|  | 213 | // get calling thread local pointer | 
|---|
|  | 214 | thread_t * this = CURRENT_THREAD; | 
|---|
|  | 215 |  | 
|---|
|  | 216 | // store command arguments in thread descriptor | 
|---|
| [279] | 217 | this->mmc_cmd.dev_xp    = chdev_dir.mmc[cxy]; | 
|---|
|  | 218 | this->mmc_cmd.type      = MMC_GET_INSTRU; | 
|---|
|  | 219 | this->mmc_cmd.reg_index = index; | 
|---|
|  | 220 | this->mmc_cmd.reg_ptr   = rdata; | 
|---|
| [1] | 221 |  | 
|---|
|  | 222 | // execute operation | 
|---|
| [23] | 223 | return dev_mmc_access( this ); | 
|---|
| [1] | 224 | } | 
|---|
|  | 225 |  | 
|---|