source: trunk/kernel/devices/dev_mmc.c @ 93

Last change on this file since 93 was 23, checked in by alain, 8 years ago

Introduce syscalls.

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