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

Last change on this file since 202 was 188, checked in by alain, 7 years ago

Redefine the PIC device API.

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