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

Last change on this file since 669 was 663, checked in by alain, 4 years ago

Introduce support for both TCP in the dev_nic.h & dev_nic.c files.

File size: 10.0 KB
RevLine 
[1]1/*
2 * dev_ioc.c - IOC (Block Device Controler) generic device API implementation.
[212]3 *
[657]4 * Author  Alain Greiner    (2016,2017,2018,2019,2020)
[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
[657]39////////////////////////////////////////////
40char * dev_ioc_cmd_str( ioc_cmd_type_t cmd )
[626]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
[663]80assert( (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
[657]93////////////////////////////////////////////////////////////////////////////////////
94// This static function executes an asynchronous SYNC_READ or SYNC_WRITE request.
95// thread in the IOC device waiting queue, activates the server thread, blocks on
96// the THREAD_BLOCKED_IO condition and deschedules.
97// The clent is re-activated by the server thread after IO operation completion.
98////////////////////////////////////////////////////////////////////////////////////
99static error_t dev_ioc_move( uint32_t   cmd_type,
100                             xptr_t     buffer_xp,
101                             uint32_t   lba,
102                             uint32_t   count )
103{
104    thread_t * this = CURRENT_THREAD;              // pointer on client thread
105
106    // get extended pointer on IOC chdev descriptor
107    xptr_t      ioc_xp  = chdev_dir.ioc[0];
108
109// check dev_xp
110assert( (ioc_xp != XPTR_NULL) , "undefined IOC chdev descriptor" );
111
112    // register command in client thread 
113    this->ioc_cmd.dev_xp    = ioc_xp;
114    this->ioc_cmd.type      = cmd_type;
115    this->ioc_cmd.buf_xp    = buffer_xp;
116    this->ioc_cmd.lba       = lba;
117    this->ioc_cmd.count     = count;
118
119    // register client thread in IOC queue, blocks and deschedules
120    chdev_register_command( ioc_xp );
121
122    // return I/O operation status
123    return this->ioc_cmd.error;
124
125}  // end dev_ioc_move()
126
127////////////////////////////////////////////////////////////////////////////////////
128// This static function executes a synchronous READ or WRITE request.
129// It register the command in the client thread descriptor, and calls directly
130// the driver cmd function.
131////////////////////////////////////////////////////////////////////////////////////
132error_t dev_ioc_sync_move( uint32_t   cmd_type,
[647]133                           xptr_t     buffer_xp,
134                           uint32_t   lba,
135                           uint32_t   count )
[1]136{
[3]137    thread_t * this = CURRENT_THREAD;              // pointer on client thread
[1]138
[3]139    // get extended pointer on IOC chdev descriptor
[647]140    xptr_t      ioc_xp  = chdev_dir.ioc[0];
[1]141
[605]142// check dev_xp
[647]143assert( (ioc_xp != XPTR_NULL) , "undefined IOC chdev descriptor" );
[1]144
[3]145    // register command in calling thread descriptor
[647]146    this->ioc_cmd.dev_xp    = ioc_xp;
[279]147    this->ioc_cmd.type      = cmd_type;
[647]148    this->ioc_cmd.buf_xp    = buffer_xp;
[279]149    this->ioc_cmd.lba       = lba;
150    this->ioc_cmd.count     = count;
[1]151
[657]152    // get driver command function
153    cxy_t       ioc_cxy = GET_CXY( ioc_xp );
154    chdev_t   * ioc_ptr = GET_PTR( ioc_xp );
155    dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( ioc_cxy , &ioc_ptr->cmd ) );
[1]156
[657]157    // call driver function whithout blocking & descheduling
158    cmd( XPTR( local_cxy , this ) );
159
160    // return I/O operation status
161    return this->ioc_cmd.error;
162
163}  // end dev_ioc_sync_move()
164
165///////////////////////////////////////////
166error_t dev_ioc_read( xptr_t     buffer_xp,
167                      uint32_t   lba,
168                      uint32_t   count )
169{
170
[647]171#if DEBUG_DEV_IOC_RX
[657]172thread_t * this  = CURRENT_THREAD;
[647]173uint32_t   cycle = (uint32_t)hal_get_cycles();
[657]174if( DEBUG_DEV_IOC_RX < cycle )
175printk("\n[%s] thread[%x,%x] enters IOC_READ / lba  %x / buffer[%x,%x] / cycle %d\n",
176__FUNCTION__, this->process->pid, this->trdid, lba,
[647]177GET_CXY(buffer_xp), GET_PTR(buffer_xp), cycle );
178#endif
179
[657]180    // software L2/L3 cache coherence for memory buffer
181    if( chdev_dir.iob ) dev_mmc_inval( buffer_xp , count<<9 );
182
183    // request an asynchronous transfer
184    error_t error = dev_ioc_move( IOC_READ,
185                                  buffer_xp,
186                                  lba,
187                                  count );
188#if(DEBUG_DEV_IOC_RX & 1)
189cycle = (uint32_t)hal_get_cycles();
190if( DEBUG_DEV_IOC_RX < cycle )
191printk("\n[%s] thread[%x,%x] exit IOC_READ / cycle %d\n",
192__FUNCTION__, this->process->pid , this->trdid , cycle );
193#endif
194
195    return error;
196
197}  // end dev_ioc_read()
198
199///////////////////////////////////////////
200error_t dev_ioc_write( xptr_t     buffer_xp,
201                       uint32_t   lba,
202                       uint32_t   count )
203{
204
[647]205#if DEBUG_DEV_IOC_TX
[657]206thread_t * this  = CURRENT_THREAD;
[647]207uint32_t   cycle = (uint32_t)hal_get_cycles();
[657]208if( DEBUG_DEV_IOC_TX < cycle )
209printk("\n[%s] thread[%x,%x] enters IOC_WRITE / lba  %x / buffer[%x,%x] / cycle %d\n",
210__FUNCTION__, this->process->pid, this->trdid, lba,
[647]211GET_CXY(buffer_xp), GET_PTR(buffer_xp), cycle );
212#endif
213
[657]214    // software L2/L3 cache coherence for memory buffer
215    if( chdev_dir.iob ) dev_mmc_sync ( buffer_xp , count<<9 );
[647]216
[657]217    // request a blocking, but asynchronous, transfer
218    error_t error = dev_ioc_move( IOC_WRITE,
219                          buffer_xp,
220                          lba,
221                          count );
222#if(DEBUG_DEV_IOC_TX & 1)
223cycle = (uint32_t)hal_get_cycles();
224if( DEBUG_DEV_IOC_TX < cycle )
225printk("\n[%s] thread[%x,%x] exit IOC_WRITE / cycle %d\n",
226__FUNCTION__, this->process->pid , this->trdid , cycle );
[605]227#endif
228
[657]229    return error;
[605]230
[657]231}  // end dev_ioc_write()
[1]232
[657]233
234///////////////////////////////////////////
235error_t dev_ioc_sync_read( xptr_t     buffer_xp,
236                           uint32_t   lba,
237                           uint32_t   count )
238{
239
[438]240#if DEBUG_DEV_IOC_RX
[657]241thread_t * this  = CURRENT_THREAD;
[605]242uint32_t   cycle = (uint32_t)hal_get_cycles();
[657]243if( DEBUG_DEV_IOC_RX < cycle )
244printk("\n[%s] thread[%x,%x] enters IOC_SYNC_READ / lba  %x / buffer[%x,%x] / cycle %d\n",
245__FUNCTION__, this->process->pid, this->trdid, lba,
[647]246GET_CXY(buffer_xp), GET_PTR(buffer_xp), cycle );
[437]247#endif
248
[657]249    // software L2/L3 cache coherence for memory buffer
250    if( chdev_dir.iob ) dev_mmc_inval( buffer_xp , count<<9 );
251
252    // request an asynchronous transfer
253    error_t error = dev_ioc_sync_move( IOC_SYNC_READ,
254                                       buffer_xp,
255                                       lba,
256                                       count );
257#if(DEBUG_DEV_IOC_RX & 1)
258cycle = (uint32_t)hal_get_cycles();
259if( DEBUG_DEV_IOC_RX < cycle )
260printk("\n[%s] thread[%x,%x] exit IOC_SYNC_READ / cycle %d\n",
261__FUNCTION__, this->process->pid , this->trdid , cycle );
262#endif
263
264    return error;
265
266}  // end dev_ioc_sync_read() 
267
268/////////////////////////////////////////////////
269error_t dev_ioc_sync_write( xptr_t     buffer_xp,
270                            uint32_t   lba,
271                            uint32_t   count )
272{
273
[438]274#if DEBUG_DEV_IOC_TX
[657]275thread_t * this  = CURRENT_THREAD;
[605]276uint32_t   cycle = (uint32_t)hal_get_cycles();
[657]277if( DEBUG_DEV_IOC_TX < cycle )
278printk("\n[%s] thread[%x,%x] enters IOC_SYNC_WRITE / lba  %x / buffer[%x,%x] / cycle %d\n",
279__FUNCTION__, this->process->pid, this->trdid, lba,
[647]280GET_CXY(buffer_xp), GET_PTR(buffer_xp), cycle );
[437]281#endif
282
[657]283    // software L2/L3 cache coherence for memory buffer
284    if( chdev_dir.iob ) dev_mmc_sync ( buffer_xp , count<<9 );
[437]285
[657]286    // request a blocking, but asynchronous, transfer
287    error_t error = dev_ioc_sync_move( IOC_SYNC_WRITE,
288                                       buffer_xp,
289                                       lba,
290                                       count );
[647]291#if(DEBUG_DEV_IOC_TX & 1)
[657]292cycle = (uint32_t)hal_get_cycles();
293if( DEBUG_DEV_IOC_TX < cycle )
294printk("\n[%s] thread[%x,%x] exit IOC_SYNC_WRITE / cycle %d\n",
295__FUNCTION__, this->process->pid , this->trdid , cycle );
[647]296#endif
[437]297
[657]298    return error;
[23]299
[657]300}  // end dev_ioc_sync_write()
[614]301
302
Note: See TracBrowser for help on using the repository browser.