source: trunk/kernel/devices/dev_ioc.c @ 605

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

Improve the FAT32 file system to support cat, rm, cp commands.

File size: 8.6 KB
Line 
1/*
2 * dev_ioc.c - IOC (Block Device 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-MKH; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <kernel_config.h>
25#include <hal_kernel_types.h>
26#include <hal_gpt.h>
27#include <hal_drivers.h>
28#include <thread.h>
29#include <printk.h>
30#include <chdev.h>
31#include <dev_ioc.h>
32
33/////////////////////////////////////////////////////////////////////////////////////////
34// Extern global variables
35/////////////////////////////////////////////////////////////////////////////////////////
36
37extern chdev_directory_t  chdev_dir;     // allocated in kernel_init.c
38
39//////////////////////////////////
40void dev_ioc_init( chdev_t * ioc )
41{
42    // the PIC chdev must be initialized before the IOC chdev, because
43    // the IOC chdev initialisation requires the routing of an external IRQ
44    xptr_t  pic_xp  = chdev_dir.pic;
45
46    assert( (pic_xp != XPTR_NULL) , "PIC not initialised before IOC" );
47
48    // get implementation and channel from chdev descriptor
49    uint32_t  impl    = ioc->impl;
50    uint32_t  channel = ioc->channel;
51
52    // set chdev name
53    snprintf( ioc->name , 16 , "ioc%d" , channel );
54
55    // call driver init function
56    hal_drivers_ioc_init( ioc, impl );
57
58    // select a core to execute the IOC server thread
59    lid_t lid = cluster_select_local_core();
60
61    // bind the IOC IRQ to the selected core
62    dev_pic_bind_irq( lid , ioc );
63
64    // enable IRQ
65    dev_pic_enable_irq( lid , XPTR( local_cxy , ioc ) );
66
67    // create server thread
68    thread_t * new_thread;
69    error_t    error;
70
71    error = thread_kernel_create( &new_thread,
72                                  THREAD_DEV,
73                                  &chdev_sequencial_server,
74                                  ioc,
75                                  lid );
76
77    assert( (error == 0) , "cannot create server thread" );
78
79    // set "server" field in chdev descriptor
80    ioc->server = new_thread;
81
82    // set "chdev field in thread descriptor
83    new_thread->chdev = ioc;
84
85    // unblock server thread
86    thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL );
87
88}  // end dev_ioc_init()
89
90//////////////////////////////////////////////////////////////////////////////////
91// This static function is called by dev_ioc_read() & dev_ioc_write() functions.
92// It builds and registers the command in the calling thread descriptor.
93// Then, it registers the calling thead in IOCchdev waiting queue.
94// Finally it blocks on the THREAD_BLOCKED_IO condition and deschedule.
95////////////////////////////////////i/////////////////////////////////////////////
96static error_t dev_ioc_access( uint32_t   cmd_type,
97                               uint8_t  * buffer,
98                               uint32_t   lba,
99                               uint32_t   count )
100{
101    thread_t * this = CURRENT_THREAD;              // pointer on client thread
102
103#if ( DEV_IOC_RX  || DEV_IOC_TX )
104uint32_t   cycle = (uint32_t)hal_get_cycles();
105#endif
106
107    // software L2/L3 cache coherence for memory buffer
108    if( chdev_dir.iob )
109    {
110        if ( cmd_type == IOC_READ ) dev_mmc_inval( XPTR( local_cxy , buffer ) , count<<9 );
111        else                        dev_mmc_sync ( XPTR( local_cxy , buffer ) , count<<9 );
112    }
113
114    // get extended pointer on IOC chdev descriptor
115    xptr_t  dev_xp = chdev_dir.ioc[0];
116
117// check dev_xp
118assert( (dev_xp != XPTR_NULL) , "undefined IOC chdev descriptor" );
119
120    // register command in calling thread descriptor
121    this->ioc_cmd.dev_xp    = dev_xp;
122    this->ioc_cmd.type      = cmd_type;
123    this->ioc_cmd.buf_xp    = XPTR( local_cxy , buffer );
124    this->ioc_cmd.lba       = lba;
125    this->ioc_cmd.count     = count;
126
127    // register client thread in IOC chdev waiting queue, activate server thread,
128    // block client thread on THREAD_BLOCKED_IO and deschedule.
129    // it is re-activated by the ISR signaling IO operation completion.
130    chdev_register_command( dev_xp );
131
132#if(DEV_IOC_RX & 1)
133if( (DEV_IOC_RX < cycle) && (cmd_type != IOC_WRITE) )
134printk("\n[%s] thread[%x,%x] resumes for RX\n",
135__FUNCTION__, this->process->pid , this->trdid )
136#endif
137
138#if(DEV_IOC_TX & 1)
139if( (DEV_IOC_RX < cycle) && (cmd_type == IOC_WRITE) )
140printk("\n[%s] thread[%x,%x] resumes for TX\n",
141__FUNCTION__, this->process->pid , this->trdid )
142#endif
143
144    // return I/O operation status
145    return this->ioc_cmd.error;
146
147}  // end dev_ioc_access()
148
149////////////////////////////////////////////
150error_t dev_ioc_read( uint8_t      * buffer,
151                      uint32_t       lba,
152                      uint32_t       count )
153{
154
155#if DEBUG_DEV_IOC_RX
156uint32_t   cycle = (uint32_t)hal_get_cycles();
157thread_t * this  = CURRENT_THREAD;
158if( DEBUG_DEV_IOC_RX < cycle )
159printk("\n[%s] thread[%x,%x] enters / lba  %x / buffer %x / cycle %d\n",
160__FUNCTION__ , this->process->pid, this->trdid, lba, buffer, cycle );
161#endif
162
163    return dev_ioc_access( IOC_READ , buffer , lba , count );
164
165#if DEBUG_DEV_IOC_RX
166cycle = (uint32_t)hal_get_cycles();
167if( DEBUG_DEV_IOC_RX < cycle )
168printk("\n[%s] thread[%x,%x] exit / lba  %x / buffer %x / cycle %d\n",
169__FUNCTION__ , this->process->pid, this->trdid, lba, buffer, cycle );
170#endif
171
172}
173
174////////////////////////////////////////////
175error_t dev_ioc_write( uint8_t     * buffer,
176                       uint32_t      lba,
177                       uint32_t      count )
178{
179
180#if DEBUG_DEV_IOC_TX
181uint32_t   cycle = (uint32_t)hal_get_cycles();
182thread_t * this  = CURRENT_THREAD;
183if( DEBUG_DEV_IOC_TX < cycle )
184printk("\n[%s] thread[%x,%x] enters / lba  %x / buffer %x / cycle %d\n",
185__FUNCTION__ , this->process->pid, this->trdid, lba, buffer, cycle );
186#endif
187
188    return dev_ioc_access( IOC_WRITE , buffer , lba , count );
189
190#if DEBUG_DEV_IOC_TX
191cycle = (uint32_t)hal_get_cycles();
192if( DEBUG_DEV_IOC_TX < cycle )
193printk("\n[%s] thread[%x,%x] exit / lba  %x / buffer %x / cycle %d\n",
194__FUNCTION__ , this->process->pid, this->trdid, lba, buffer, cycle );
195#endif
196
197}
198
199/////////////////////////////////////////////
200error_t dev_ioc_sync_read( uint8_t  * buffer,
201                           uint32_t   lba,
202                           uint32_t   count )
203{
204    // get pointer on calling thread
205    thread_t * this = CURRENT_THREAD;
206
207#if DEBUG_DEV_IOC_RX
208uint32_t cycle = (uint32_t)hal_get_cycles();
209if( DEBUG_DEV_IOC_RX < cycle )
210printk("\n[%s] thread[%x,%x] enters / lba  %x / buffer %x / cycle %d\n",
211__FUNCTION__ , this->process->pid, this->trdid, lba, buffer, cycle );
212#endif
213
214    // software L2/L3 cache coherence for memory buffer
215    if( chdev_dir.iob ) dev_mmc_inval( XPTR( local_cxy , buffer ) , count<<9 );
216
217    // get extended pointer on IOC[0] chdev
218    xptr_t  ioc_xp = chdev_dir.ioc[0];
219
220    assert( (ioc_xp != XPTR_NULL) , "undefined IOC chdev descriptor" );
221
222    // register command in calling thread descriptor
223    this->ioc_cmd.dev_xp    = ioc_xp;
224    this->ioc_cmd.type      = IOC_SYNC_READ;
225    this->ioc_cmd.buf_xp    = XPTR( local_cxy , buffer );
226    this->ioc_cmd.lba       = lba;
227    this->ioc_cmd.count     = count;
228
229    // get driver command function
230    cxy_t       ioc_cxy = GET_CXY( ioc_xp );
231    chdev_t   * ioc_ptr = (chdev_t *)GET_PTR( ioc_xp );
232    dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( ioc_cxy , &ioc_ptr->cmd ) );
233
234    // get core local index for the core handling the IOC IRQ
235    thread_t * server = (thread_t *)hal_remote_lpt( XPTR( ioc_cxy , &ioc_ptr->server ) );
236    core_t   * core   = (core_t *)hal_remote_lpt( XPTR( ioc_cxy , &server->core ) );
237    lid_t      lid    = (lid_t)hal_remote_l32( XPTR( ioc_cxy , &core->lid ) );
238
239    // mask the IRQ
240    dev_pic_disable_irq( lid , ioc_xp );
241
242    // call driver function
243    cmd( XPTR( local_cxy , this ) );
244
245    // unmask the IRQ
246    dev_pic_enable_irq( lid , ioc_xp );
247
248#if DEBUG_DEV_IOC_RX
249cycle = (uint32_t)hal_get_cycles();
250if( DEBUG_DEV_IOC_RX < cycle )
251printk("\n[%s] thread[%x,%x] exit / lba  %x / buffer %x / cycle %d\n",
252__FUNCTION__ , this->process->pid, this->trdid, lba, buffer, cycle );
253#endif
254
255    // return I/O operation status from calling thread descriptor
256    return this->ioc_cmd.error;
257
258}  // end ioc_sync_read()
259
Note: See TracBrowser for help on using the repository browser.