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

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

...miscelaneous...

File size: 7.0 KB
RevLine 
[1]1/*
2 * dev_ioc.c - IOC (Block Device Controler) generic device API implementation.
[212]3 *
[626]4 * Author  Alain Greiner    (2016,2017,2018,2019)
[1]5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MK
9 *
[212]10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
[1]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 *
[212]14 * ALMOS-MKH is distributed in the hope that it will be useful, but
[1]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
[212]20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
[1]21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
[14]24#include <kernel_config.h>
[457]25#include <hal_kernel_types.h>
[1]26#include <hal_gpt.h>
[213]27#include <hal_drivers.h>
[1]28#include <thread.h>
29#include <printk.h>
[3]30#include <chdev.h>
[1]31#include <dev_ioc.h>
32
33/////////////////////////////////////////////////////////////////////////////////////////
34// Extern global variables
35/////////////////////////////////////////////////////////////////////////////////////////
36
[188]37extern chdev_directory_t  chdev_dir;     // allocated in kernel_init.c
[1]38
[626]39////////////////////////////////////////
40char * dev_ioc_cmd_str( cmd_type_t cmd )
41{
42    if     ( cmd == IOC_READ       )  return "READ";
43    else if( cmd == IOC_WRITE      )  return "WRITE";
44    else if( cmd == IOC_SYNC_READ  )  return "SYNC_READ";
45    else if( cmd == IOC_SYNC_WRITE )  return "SYNC_WRITE";
46    else                              return "undefined";
47}
48
[188]49//////////////////////////////////
50void dev_ioc_init( chdev_t * ioc )
[1]51{
[647]52    // get channel from chdev descriptor
[188]53    uint32_t  channel = ioc->channel;
[1]54
[23]55    // set chdev name
[407]56    snprintf( ioc->name , 16 , "ioc%d" , channel );
[23]57
[211]58    // call driver init function
[647]59    hal_drivers_ioc_init( ioc );
[1]60
[188]61    // select a core to execute the IOC server thread
[637]62    lid_t lid = cluster_select_local_core( local_cxy );
[3]63
[188]64    // bind the IOC IRQ to the selected core
65    dev_pic_bind_irq( lid , ioc );
[3]66
[188]67    // enable IRQ
[238]68    dev_pic_enable_irq( lid , XPTR( local_cxy , ioc ) );
[3]69
[1]70    // create server thread
[3]71    thread_t * new_thread;
[1]72    error_t    error;
73
[3]74    error = thread_kernel_create( &new_thread,
75                                  THREAD_DEV,
[619]76                                  &chdev_server_func,
[188]77                                  ioc,
[212]78                                  lid );
[188]79
[492]80    assert( (error == 0) , "cannot create server thread" );
[1]81
[408]82    // set "server" field in chdev descriptor
[188]83    ioc->server = new_thread;
[212]84
[647]85    // set "chdev" field in thread descriptor
[408]86    new_thread->chdev = ioc;
87
88    // unblock server thread
[3]89    thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL );
[212]90
[1]91}  // end dev_ioc_init()
92
[647]93///////////////////////////////////////////////
94error_t dev_ioc_move_data( uint32_t   cmd_type,
95                           xptr_t     buffer_xp,
96                           uint32_t   lba,
97                           uint32_t   count )
[1]98{
[3]99    thread_t * this = CURRENT_THREAD;              // pointer on client thread
[1]100
[647]101#if ( DEBUG_DEV_IOC_RX  || DEBUG_DEV_IOC_TX )
[605]102uint32_t   cycle = (uint32_t)hal_get_cycles();
103#endif
104
[212]105    // software L2/L3 cache coherence for memory buffer
106    if( chdev_dir.iob )
[68]107    {
[647]108        if( (cmd_type == IOC_SYNC_READ) || (cmd_type == IOC_READ) )
109        {
110            dev_mmc_inval( buffer_xp , count<<9 );
111        }
112        else  // (cmd_type == IOC_SYNC_WRITE) or (cmd_type == IOC_WRITE)
113        {
114            dev_mmc_sync ( buffer_xp , count<<9 );
115        }
[68]116    }
[1]117
[3]118    // get extended pointer on IOC chdev descriptor
[647]119    xptr_t      ioc_xp  = chdev_dir.ioc[0];
[1]120
[605]121// check dev_xp
[647]122assert( (ioc_xp != XPTR_NULL) , "undefined IOC chdev descriptor" );
[1]123
[3]124    // register command in calling thread descriptor
[647]125    this->ioc_cmd.dev_xp    = ioc_xp;
[279]126    this->ioc_cmd.type      = cmd_type;
[647]127    this->ioc_cmd.buf_xp    = buffer_xp;
[279]128    this->ioc_cmd.lba       = lba;
129    this->ioc_cmd.count     = count;
[1]130
[647]131    // for a synchronous acces, the driver is directly called by the client thread
132    if( (cmd_type == IOC_SYNC_READ) || (cmd_type == IOC_SYNC_WRITE) )
133    {
[1]134
[647]135#if DEBUG_DEV_IOC_RX
136uint32_t   cycle = (uint32_t)hal_get_cycles();
137if( (DEBUG_DEV_IOC_RX < cycle) && (cmd_type == IOC_SYNC_READ) )
138printk("\n[%s] thread[%x,%x] enters for SYNC_READ / lba  %x / buffer[%x,%x] / cycle %d\n",
139__FUNCTION__ , this->process->pid, this->trdid, lba,
140GET_CXY(buffer_xp), GET_PTR(buffer_xp), cycle );
141#endif
142
143#if DEBUG_DEV_IOC_TX
144uint32_t   cycle = (uint32_t)hal_get_cycles();
145if( (DEBUG_DEV_IOC_TX < cycle) && (cmd_type == IOC_SYNC_WRITE) )
146printk("\n[%s] thread[%x,%x] enters for SYNC_WRITE / lba  %x / buffer[%x,%x] / cycle %d\n",
147__FUNCTION__ , this->process->pid, this->trdid, lba,
148GET_CXY(buffer_xp), GET_PTR(buffer_xp), cycle );
149#endif
150        // get driver command function
151        cxy_t       ioc_cxy = GET_CXY( ioc_xp );
152        chdev_t   * ioc_ptr = GET_PTR( ioc_xp );
153        dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( ioc_cxy , &ioc_ptr->cmd ) );
154
155        // call driver function
156        cmd( XPTR( local_cxy , this ) );
157
158#if DEBUG_DEV_IOC_RX
159if( (DEBUG_DEV_IOC_RX < cycle) && (cmd_type == IOC_SYNC_READ) )
160printk("\n[%s] thread[%x,%x] resumes for IOC_SYNC_READ\n",
[605]161__FUNCTION__, this->process->pid , this->trdid )
162#endif
163
[647]164#if DEBUG_DEV_IOC_TX
165if( (DEBUG_DEV_IOC_RX < cycle) && (cmd_type == IOC_SYNC_WRITE) )
166printk("\n[%s] thread[%x,%x] resumes for IOC_SYNC_WRITE\n",
[605]167__FUNCTION__, this->process->pid , this->trdid )
168#endif
169
[647]170    }
171    // for an asynchronous access, the client thread registers in the chdev waiting queue,
172    // activates server thread, blocks on THREAD_BLOCKED_IO and deschedules.
173    // It is re-activated by the server thread after IO operation completion.
174    else  // (cmd_type == IOC_READ) || (cmd_type == IOC_WRITE)
175    {
[1]176
[438]177#if DEBUG_DEV_IOC_RX
[605]178uint32_t   cycle = (uint32_t)hal_get_cycles();
[647]179if( (DEBUG_DEV_IOC_RX < cycle) && (cmd_type == IOC_READ) )
180printk("\n[%s] thread[%x,%x] enters for READ / lba  %x / buffer[%x,%x] / cycle %d\n",
181__FUNCTION__ , this->process->pid, this->trdid, lba,
182GET_CXY(buffer_xp), GET_PTR(buffer_xp), cycle );
[437]183#endif
184
[438]185#if DEBUG_DEV_IOC_TX
[605]186uint32_t   cycle = (uint32_t)hal_get_cycles();
[647]187if( (DEBUG_DEV_IOC_TX < cycle) && (cmd_type == IOC_WRITE) )
188printk("\n[%s] thread[%x,%x] enters for WRITE / lba  %x / buffer[%x,%x] / cycle %d\n",
189__FUNCTION__ , this->process->pid, this->trdid, lba,
190GET_CXY(buffer_xp), GET_PTR(buffer_xp), cycle );
[437]191#endif
[647]192        chdev_register_command( ioc_xp );
[437]193
[647]194#if(DEBUG_DEV_IOC_RX )
195if( (DEBUG_DEV_IOC_RX < cycle) && (cmd_type == IOC_READ) )
196printk("\n[%s] thread[%x,%x] resumes for IOC_READ\n",
197__FUNCTION__, this->process->pid , this->trdid )
198#endif
[437]199
[647]200#if(DEBUG_DEV_IOC_TX & 1)
201if( (DEBUG_DEV_IOC_RX < cycle) && (cmd_type == IOC_WRITE) )
202printk("\n[%s] thread[%x,%x] resumes for IOC_WRITE\n",
203__FUNCTION__, this->process->pid , this->trdid )
204#endif
[437]205
[614]206    }
[23]207
[647]208    // return I/O operation status
[614]209    return this->ioc_cmd.error;
210
[647]211}  // end dev_ioc_move_data()
[614]212
213
Note: See TracBrowser for help on using the repository browser.