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

Last change on this file since 626 was 626, checked in by alain, 3 years ago

This version has been tested on the sort multithreaded application
for TSAR_IOB architectures ranging from 1 to 8 clusters.
It fixes three bigs bugs:
1) the dev_ioc device API has been modified: the dev_ioc_sync_read()
and dev_ioc_sync_write() function use now extended pointers on the
kernel buffer to access a mapper stored in any cluster.
2) the hal_uspace API has been modified: the hal_copy_to_uspace()
and hal_copy_from_uspace() functions use now a (cxy,ptr) couple
to identify the target buffer (equivalent to an extended pointer.
3) an implementation bug has been fixed in the assembly code contained
in the hal_copy_to_uspace() and hal_copy_from_uspace() functions.

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