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

Last change on this file since 437 was 437, checked in by alain, 6 years ago

Fix various bugs

File size: 7.6 KB
Line 
1/*
2 * dev_mmc.c - MMC (Memory Cache Controler) generic device API implementation.
3 *
4 * Author  Alain Greiner    (2016,2017,2018)
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 <hal_drivers.h>
27#include <printk.h>
28#include <chdev.h>
29#include <thread.h>
30#include <dev_mmc.h>
31
32/////////////////////////////////////////////////////////////////////////////////////////
33// Extern global variables
34/////////////////////////////////////////////////////////////////////////////////////////
35
36extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c
37
38//////////////////////////////////
39void dev_mmc_init( chdev_t * mmc )
40{
41    // get implementation from device descriptor
42    uint32_t  impl = mmc->impl;
43
44    // set mmc name
45    snprintf( mmc->name , 16 , "mmc_%x" , local_cxy );
46
47    // call driver init function
48    hal_drivers_mmc_init( mmc , impl );
49
50    // bind IRQ to CP0
51    dev_pic_bind_irq( 0 , mmc );
52   
53    // enable IRQ
54    dev_pic_enable_irq( 0 , XPTR( local_cxy , mmc ) );
55
56}  // end dev_mmc_init()
57
58/////////////////////////////////////////////////////////////////////////////
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
62// registered in the calling thread descriptor, and releases the lock.
63/////////////////////////////////////////////////////////////////////////////
64static error_t dev_mmc_access( thread_t * this )
65{
66    // get extended pointer on MMC device descriptor
67    xptr_t  dev_xp = this->mmc_cmd.dev_xp;
68
69    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "target MMC device undefined" );
70
71    // get MMC device cluster identifier & local pointer
72    cxy_t     dev_cxy = GET_CXY( dev_xp );
73    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
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
87    // return operation status
88    return this->mmc_cmd.error; 
89
90}  // end dev_mmc_access()
91
92/////////////////////////////////////////
93error_t dev_mmc_inval( xptr_t     buf_xp,
94                       uint32_t   buf_size )
95{
96    error_t error;
97
98    // get calling thread local pointer
99    thread_t * this = CURRENT_THREAD;
100
101#if CONFIG_DEBUG_DEV_MMC
102uint32_t cycle = (uint32_t)hal_get_cycles();
103if( CONFIG_DEBUG_DEV_MMC < cycle )
104printk("\n[DBG] %s : thread %x enters / process %x / buf_xp = %l\n", 
105__FUNCTION__, this, this->process->pid , buf_xp );
106#endif
107
108    // get buffer cluster and local pointer
109    cxy_t  buf_cxy = GET_CXY( buf_xp );
110    void * buf_ptr = GET_PTR( buf_xp );
111   
112    assert( (((intptr_t)buf_ptr & (CONFIG_CACHE_LINE_SIZE -1)) == 0) , __FUNCTION__ ,
113             "buffer not aligned on cache line" );
114
115    // get buffer physical address
116    paddr_t  buf_paddr;
117    error = vmm_v2p_translate( CONFIG_KERNEL_IDENTITY_MAP , buf_ptr , &buf_paddr ); 
118
119    assert( (error == 0) , __FUNCTION__ , "cannot get buffer paddr" );
120
121    // store command arguments in thread descriptor
122    this->mmc_cmd.dev_xp    = chdev_dir.mmc[buf_cxy];
123    this->mmc_cmd.type      = MMC_CC_INVAL;
124    this->mmc_cmd.buf_paddr = buf_paddr;
125    this->mmc_cmd.buf_size  = buf_size;
126
127    // call MMC driver
128    error = dev_mmc_access( this );
129
130#if CONFIG_DEBUG_DEV_MMC
131cycle = (uint32_t)hal_get_cycles();
132if( CONFIG_DEBUG_DEV_MMC < cycle )
133printk("\n[DBG] %s : thread %x exit / process %x / buf_xp = %l\n", 
134__FUNCTION__, this, this->process->pid , buf_xp );
135#endif
136
137    return error;
138}
139
140////////////////////////////////////////
141error_t dev_mmc_sync( xptr_t     buf_xp,
142                      uint32_t   buf_size )
143{
144    error_t error;
145
146    // get calling thread local pointer
147    thread_t * this = CURRENT_THREAD;
148
149#if CONFIG_DEBUG_DEV_MMC
150uint32_t cycle = (uint32_t)hal_get_cycles();
151if( CONFIG_DEBUG_DEV_MMC < cycle )
152printk("\n[DBG] %s : thread %x enters / process %x / buf_xp = %l\n", 
153__FUNCTION__, this, this->process->pid , buf_xp );
154#endif
155
156    // get buffer cluster and local pointer
157    cxy_t  buf_cxy = GET_CXY( buf_xp );
158    void * buf_ptr = GET_PTR( buf_xp );
159   
160    assert( (((intptr_t)buf_ptr & (CONFIG_CACHE_LINE_SIZE -1)) == 0) , __FUNCTION__ ,
161             "buffer not aligned on cache line" );
162
163    // get  buffer physical address
164    paddr_t  buf_paddr;
165    error = vmm_v2p_translate( CONFIG_KERNEL_IDENTITY_MAP , buf_ptr , &buf_paddr ); 
166
167    assert( (error == 0) , __FUNCTION__ , "cannot get buffer paddr" );
168
169    // store command arguments in thread descriptor
170    this->mmc_cmd.dev_xp    = chdev_dir.mmc[buf_cxy];
171    this->mmc_cmd.type      = MMC_CC_SYNC;
172    this->mmc_cmd.buf_paddr = buf_paddr;
173    this->mmc_cmd.buf_size  = buf_size;
174
175    // call MMC driver
176    error = dev_mmc_access( this );
177
178#if CONFIG_DEBUG_DEV_MMC
179cycle = (uint32_t)hal_get_cycles();
180if( CONFIG_DEBUG_DEV_MMC < cycle )
181printk("\n[DBG] %s : thread %x exit / process %x / buf_xp = %l\n", 
182__FUNCTION__, this, this->process->pid , buf_xp );
183#endif
184
185    return error;
186}
187
188/////////////////////////////////////////
189error_t dev_mmc_set_error( cxy_t     cxy,
190                           uint32_t  index,
191                           uint32_t  wdata )
192{
193    // get calling thread local pointer
194    thread_t * this = CURRENT_THREAD;
195
196    // store command arguments in thread descriptor
197    this->mmc_cmd.dev_xp    = chdev_dir.mmc[cxy];
198    this->mmc_cmd.type      = MMC_SET_ERROR;
199    this->mmc_cmd.reg_index = index;
200    this->mmc_cmd.reg_ptr   = &wdata;
201
202    // execute operation
203    return dev_mmc_access( this ); 
204}
205                       
206//////////////////////////////////////////
207error_t dev_mmc_get_error( cxy_t      cxy,
208                           uint32_t   index,
209                           uint32_t * rdata )
210{
211    // get calling thread local pointer
212    thread_t * this = CURRENT_THREAD;
213
214    // store command arguments in thread descriptor
215    this->mmc_cmd.dev_xp    = chdev_dir.mmc[cxy];
216    this->mmc_cmd.type      = MMC_GET_ERROR;
217    this->mmc_cmd.reg_index = index;
218    this->mmc_cmd.reg_ptr   = rdata;
219
220    // execute operation
221    return dev_mmc_access( this ); 
222}
223                       
224////////////////////////////////////////////////////
225error_t dev_mmc_get_instrumentation( cxy_t      cxy,
226                                     uint32_t   index,
227                                     uint32_t * rdata )
228{
229    // get calling thread local pointer
230    thread_t * this = CURRENT_THREAD;
231
232    // store command arguments in thread descriptor
233    this->mmc_cmd.dev_xp    = chdev_dir.mmc[cxy];
234    this->mmc_cmd.type      = MMC_GET_INSTRU;
235    this->mmc_cmd.reg_index = index;
236    this->mmc_cmd.reg_ptr   = rdata;
237
238    // execute operation
239    return dev_mmc_access( this ); 
240}
241
Note: See TracBrowser for help on using the repository browser.