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

Last change on this file since 79 was 68, checked in by alain, 5 years ago

Fix bug in kernel_init, and reduce size of remote_fifo.

File size: 7.6 KB
Line 
1/*
2 * dev_ioc.c - IOC (Block Device 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 <kernel_config.h>
25#include <hal_types.h>
26#include <hal_gpt.h>
27#include <soclib_bdv.h>
28#include <soclib_hba.h>
29//#include <soclib_sdc.h>
30//#include <soclib_spi.h>
31//#include <soclib_rdk.h>
32#include <thread.h>
33#include <printk.h>
34#include <chdev.h>
35#include <dev_ioc.h>
36
37/////////////////////////////////////////////////////////////////////////////////////////
38// Extern global variables
39/////////////////////////////////////////////////////////////////////////////////////////
40
41extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c
42
43extern chdev_pic_input_t  chdev_pic_input;   // allocated in kernel_init.c
44
45////////////////////////////////////
46void dev_ioc_init( chdev_t * chdev )
47{
48    // the local ICU chdev must be initialized before the IOC chdev, because
49    // the IOC chdev initialisation requires allocation of a WTI from local ICU
50    xptr_t  icu_xp  = chdev_dir.icu[local_cxy];
51    assert( (icu_xp != XPTR_NULL) , __FUNCTION__ , "ICU not initialised before IOC" );
52
53    // get implementation and channel from chdev descriptor
54    uint32_t  impl    = chdev->impl;
55    uint32_t  channel = chdev->channel;
56
57    // set chdev name
58    strcpy( chdev->name , "ioc" );
59
60    // set driver specific fields in chdev descriptor and call driver init function
61    if( impl == IMPL_IOC_BDV )
62    {
63        chdev->cmd = &soclib_bdv_cmd;
64        chdev->isr = &soclib_bdv_isr;
65        soclib_bdv_init( chdev );
66    }
67    else if( impl == IMPL_IOC_HBA )
68    {
69        chdev->cmd = &soclib_hba_cmd;
70        chdev->isr = &soclib_hba_isr;
71        soclib_hba_init( chdev );
72    }
73    else
74    {
75        assert( false , __FUNCTION__ , "undefined IOC device implementation" );
76    }
77
78    // get a WTI mailbox from local ICU
79    uint32_t wti_id = dev_icu_wti_alloc();
80
81    assert( (wti_id != -1) , __FUNCTION__ , "cannot allocate WTI mailbox" );
82
83    // select a core
84    lid_t lid = cluster_select_local_core();
85
86    // enable WTI IRQ and update WTI interrupt vector
87    dev_icu_enable_irq( lid , WTI_TYPE , wti_id , chdev );
88
89    // link IOC IRQ to WTI mailbox in PIC component
90    uint32_t irq_id = chdev_pic_input.ioc[channel];
91    dev_pic_bind_irq( irq_id , local_cxy , wti_id );
92
93    // create server thread
94    thread_t * new_thread;
95    error_t    error;
96
97    error = thread_kernel_create( &new_thread,
98                                  THREAD_DEV,
99                                  &chdev_sequencial_server,
100                                  chdev,
101                                  lid ); 
102    assert( (error == 0) , __FUNCTION__ , "cannot create server thread" );
103
104    // set "server" field in chdev descriptor
105    chdev->server = new_thread;
106   
107    // start server thread
108    thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL );
109 
110}  // end dev_ioc_init()
111
112//////////////////////////////////////////////////////////////////////////////////
113// This static function is called by dev_ioc_read() & dev_ioc_write() functions.
114// It builds and registers the command in the calling thread descriptor.
115// Then, it registers the calling thead in chdev waiting queue.
116// Finally it blocks on the THREAD_BLOCKED_DEV condition and deschedule.
117////////////////////////////////////i/////////////////////////////////////////////
118static error_t dev_ioc_access( uint32_t   cmd_type,
119                               uint8_t  * buffer,
120                               uint32_t   lba,
121                               uint32_t   count )
122{
123    thread_t * this = CURRENT_THREAD;              // pointer on client thread
124
125    ioc_dmsg("\n[INFO] in %s : thread %x in process %x"
126             " for lba = %x / buffer = %x / at cycle %d\n", 
127             __FUNCTION__ , this->trdid , this->process->pid , 
128             lba , (intptr_t)buffer , hal_time_stamp() );
129
130    // software L2/L3 cache coherence for memory buffer
131    if( chdev_dir.iob ) 
132    {
133        if ( cmd_type == IOC_READ ) dev_mmc_inval( XPTR( local_cxy , buffer ) , count<<9 );
134        else                        dev_mmc_sync ( XPTR( local_cxy , buffer ) , count<<9 );
135    }
136
137    // get extended pointer on IOC chdev descriptor
138    xptr_t  dev_xp = chdev_dir.ioc[0];
139
140    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined IOC chdev descriptor" );
141
142    // register command in calling thread descriptor
143    this->command.ioc.dev_xp    = dev_xp;
144    this->command.ioc.type      = cmd_type;
145    this->command.ioc.buf_xp    = XPTR( local_cxy , buffer );
146    this->command.ioc.lba       = lba;
147    this->command.ioc.count     = count;
148
149    // register client thread in IOC chdev waiting queue, activate server thread,
150    // block client thread on THREAD_BLOCKED_IO and deschedule.
151    // it is re-activated by the ISR signaling IO operation completion.
152    chdev_register_command( dev_xp , this );
153
154    ioc_dmsg("\n[INFO] in %s : thread %x in process %x"
155             " completes / error = %d / at cycle %d\n", 
156             __FUNCTION__ , this->trdid , this->process->pid ,
157             this->command.ioc.error , hal_time_stamp() );
158
159    // return I/O operation status
160    return this->command.ioc.error; 
161
162}  // end dev_ioc_access()
163
164////////////////////////////////////////////
165error_t dev_ioc_read( uint8_t      * buffer,
166                      uint32_t       lba,
167                      uint32_t       count )
168{
169    return dev_ioc_access( IOC_READ , buffer , lba , count ); 
170} 
171
172////////////////////////////////////////////
173error_t dev_ioc_write( uint8_t     * buffer,
174                       uint32_t      lba,
175                       uint32_t      count )
176{
177    return dev_ioc_access( IOC_WRITE , buffer , lba , count ); 
178}
179
180/////////////////////////////////////////////
181error_t dev_ioc_sync_read( uint8_t  * buffer,
182                           uint32_t   lba,
183                           uint32_t   count )
184{
185    // get pointer on calling thread
186    thread_t * this = CURRENT_THREAD;
187
188    // software L2/L3 cache coherence for memory buffer
189    if( chdev_dir.iob ) dev_mmc_inval( XPTR( local_cxy , buffer ) , count<<9 );
190
191    // get extended pointer on IOC[0] chdev
192    xptr_t  dev_xp = chdev_dir.ioc[0];
193
194    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined IOC chdev descriptor" );
195
196    // register command in calling thread descriptor
197    this->command.ioc.dev_xp    = dev_xp;
198    this->command.ioc.type      = IOC_SYNC_READ;
199    this->command.ioc.buf_xp    = XPTR( local_cxy , buffer );
200    this->command.ioc.lba       = lba;
201    this->command.ioc.count     = count;
202
203    // get driver command function
204    cxy_t       dev_cxy = GET_CXY( dev_xp );
205    chdev_t   * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
206    dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->cmd ) );
207
208    // call directly driver command
209    cmd( XPTR( local_cxy , this ) );
210
211    // return I/O operation status from calling thread descriptor
212    return this->command.ioc.error;
213}
214
Note: See TracBrowser for help on using the repository browser.