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

Last change on this file since 41 was 23, checked in by alain, 7 years ago

Introduce syscalls.

File size: 7.9 KB
Line 
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>
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
38extern chdev_icu_input_t  chdev_icu_input;   // allocated in kernel_init.c
39
40////////////////////////////////////
41void dev_mmc_init( chdev_t * chdev )
42{
43    // get implementation from device descriptor
44    uint32_t  impl = chdev->impl;
45
46    // set chdev name
47    snprintf( chdev->name , 16 , "mmc_%x" , local_cxy );
48
49    // set driver specific fields in device descriptor and call driver init function
50    if( impl == IMPL_MMC_TSR )
51    {
52        chdev->cmd = &soclib_mmc_cmd;
53        chdev->isr = &soclib_mmc_isr;
54        soclib_mmc_init( chdev );
55    }
56    else
57    {
58        assert( false , __FUNCTION__ , "undefined MMC device implementation" );
59    }
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   
67}  // end dev_mmc_init()
68
69/////////////////////////////////////////////////////////////////////////////
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
73// registered in the calling thread descriptor, and releases the lock.
74/////////////////////////////////////////////////////////////////////////////
75static error_t dev_mmc_access( thread_t * this )
76{
77    // get extended pointer on MMC device descriptor
78    xptr_t  dev_xp = this->command.mmc.dev_xp;
79
80    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "target MMC device undefined" );
81
82    // get MMC device cluster identifier & local pointer
83    cxy_t     dev_cxy = GET_CXY( dev_xp );
84    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
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
98    // return operation status
99    return this->command.mmc.error; 
100
101}  // end dev_mmc_access()
102
103/////////////////////////////////////////
104error_t dev_mmc_inval( xptr_t     buf_xp,
105                       uint32_t   buf_size )
106{
107    error_t error;
108
109    // get calling thread local pointer
110    thread_t * this = CURRENT_THREAD;
111
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 );
114
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__ ,
120             "buffer not aligned on cache line" );
121
122    // get buffer physical address
123    paddr_t  buf_paddr;
124    error = vmm_v2p_translate( CONFIG_KERNEL_IDENTITY_MAP , buf_ptr , &buf_paddr ); 
125
126    assert( (error == 0) , __FUNCTION__ , "cannot get buffer paddr" );
127
128    // store command arguments in thread descriptor
129    this->command.mmc.dev_xp    = chdev_dir.mmc[buf_cxy];
130    this->command.mmc.type      = MMC_CC_INVAL;
131    this->command.mmc.buf_paddr = buf_paddr;
132    this->command.mmc.buf_size  = buf_size;
133
134    // call MMC driver
135    error = dev_mmc_access( this );
136
137    mmc_dmsg("\n[INFO] %s completes for thread %x in process %x / error = %d\n", 
138             __FUNCTION__ , this->trdid , this->process->pid , error );
139
140    return error;
141}
142
143////////////////////////////////////////
144error_t dev_mmc_sync( xptr_t     buf_xp,
145                      uint32_t   buf_size )
146{
147    error_t error;
148
149    // get calling thread local pointer
150    thread_t * this = CURRENT_THREAD;
151
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 );
154
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__ ,
160             "buffer not aligned on cache line" );
161
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
168    // store command arguments in thread descriptor
169    this->command.mmc.dev_xp    = chdev_dir.mmc[buf_cxy];
170    this->command.mmc.type      = MMC_CC_SYNC;
171    this->command.mmc.buf_paddr = buf_paddr;
172    this->command.mmc.buf_size  = buf_size;
173
174    // call MMC driver
175    error = dev_mmc_access( this );
176
177    mmc_dmsg("\n[INFO] %s completes for thread %x in process %x / error = %d\n", 
178                 __FUNCTION__ , this->trdid , this->process->pid , error );
179
180    return error;
181}
182
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
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;
196
197    // execute operation
198    return dev_mmc_access( this ); 
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
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;
214
215    // execute operation
216    return dev_mmc_access( this ); 
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
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;
232
233    // execute operation
234    return dev_mmc_access( this ); 
235}
236
Note: See TracBrowser for help on using the repository browser.