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

Last change on this file since 51 was 50, checked in by alain, 7 years ago

bloup

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