source: trunk/kernel/drivers/soclib/soclib_bdv.c @ 51

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

Introduce syscalls.

File size: 6.2 KB
RevLine 
[1]1/*
2 * soclib_bdv.c - soclib simple block device driver implementation.
3 *
4 * Author     Alain Greiner (2016)
5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
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
[4]24#include <chdev.h>
[1]25#include <dev_ioc.h>
26#include <soclib_bdv.h>
27#include <thread.h>
28#include <spinlock.h>
29
30
[4]31///////////////////////////////////////
32void soclib_bdv_init( chdev_t * chdev )
[1]33{
34    // get extended pointer on SOCLIB_BDV peripheral base address
[4]35        xptr_t  bdv_xp = chdev->base;
[1]36
37    // get hardware device cluster and local pointer
38    cxy_t      bdv_cxy  = GET_CXY( bdv_xp );
39    uint32_t * bdv_ptr  = (uint32_t *)GET_PTR( bdv_xp );
40
41    // get block_size and block_count 
42        uint32_t block_size = hal_remote_lw( XPTR( bdv_cxy , bdv_ptr + BDV_BLOCK_SIZE_REG ) );
43        uint32_t block_count = hal_remote_lw( XPTR( bdv_cxy , bdv_ptr + BDV_SIZE_REG ) );
44
45    // set IOC device descriptor extension
[4]46    chdev->ext.ioc.size  = block_size;
47    chdev->ext.ioc.count = block_count;
[1]48
49} // end soclib_bdv_init()
50
51
[4]52//////////////////////////////////////////////////////////////
53void __attribute__ ((noinline)) soclib_bdv_cmd( xptr_t th_xp )
[1]54{
[23]55    uint32_t   cmd_type;     // IOC_READ / IOC_WRITE / IOC_SYNC_READ
[1]56    uint32_t   lba;          // command argument
57    uint32_t   count;        // command argument
58    xptr_t     buf_xp;       // command argument
59    xptr_t     dev_xp;       // command argument
60
61    // get client thread cluster and local pointer
62    cxy_t      th_cxy = GET_CXY( th_xp );
63    thread_t * th_ptr = (thread_t *)GET_PTR( th_xp );
64
65    // get command arguments and extended pointer on IOC device
[23]66    cmd_type =         hal_remote_lw ( XPTR( th_cxy , &th_ptr->command.ioc.type   ) );
67    lba      =         hal_remote_lw ( XPTR( th_cxy , &th_ptr->command.ioc.lba    ) );
68    count    =         hal_remote_lw ( XPTR( th_cxy , &th_ptr->command.ioc.count  ) );
69    buf_xp   = (xptr_t)hal_remote_lwd( XPTR( th_cxy , &th_ptr->command.ioc.buf_xp ) );
70    dev_xp   = (xptr_t)hal_remote_lwd( XPTR( th_cxy , &th_ptr->command.ioc.dev_xp ) );
[1]71
72    // get IOC device cluster and local pointer
73    cxy_t      dev_cxy = GET_CXY( dev_xp );
[4]74    chdev_t  * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
[1]75
76    // get extended pointer on SOCLIB-BDV peripheral
77    xptr_t     bdv_xp = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->base ) );
78
79    // get SOCLIB_BDV device cluster and local pointer
80    cxy_t      bdv_cxy = GET_CXY( bdv_xp );
81    uint32_t * bdv_ptr = (uint32_t *)GET_PTR( bdv_xp );
82
83    // split buffer address in two 32 bits words
84    uint32_t   buf_lsb = (uint32_t)(buf_xp);
85    uint32_t   buf_msb = (uint32_t)(buf_xp>>32);
86
[23]87    // set operation
88    uint32_t   op;
89    if( cmd_type == IOC_WRITE ) op = BDV_OP_WRITE; 
90    else                        op = BDV_OP_READ;
[1]91
92    // set SOCLIB_BDV registers to start one I/O operation
93    hal_remote_sw( XPTR( bdv_cxy , bdv_ptr + BDV_IRQ_ENABLE_REG ) , 1       );
94    hal_remote_sw( XPTR( bdv_cxy , bdv_ptr + BDV_BUFFER_REG     ) , buf_lsb ); 
95    hal_remote_sw( XPTR( bdv_cxy , bdv_ptr + BDV_BUFFER_EXT_REG ) , buf_msb ); 
96    hal_remote_sw( XPTR( bdv_cxy , bdv_ptr + BDV_LBA_REG        ) , lba     );
97    hal_remote_sw( XPTR( bdv_cxy , bdv_ptr + BDV_COUNT_REG      ) , count   );
[23]98    hal_remote_sw( XPTR( bdv_cxy , bdv_ptr + BDV_OP_REG         ) , op      );
[1]99
[23]100    // waiting policy  depends on the command type
101
102    if( cmd_type == IOC_SYNC_READ )                  // polling policy
103    {
104        uint32_t status;
105        while (1)
106        {
107            status = hal_remote_lw( XPTR( bdv_cxy , bdv_ptr + BDV_STATUS_REG ) );
108
109            if( status == BDV_READ_SUCCESS ) // successfully completed
110            {
111                hal_remote_sw( XPTR( th_cxy , &th_ptr->command.ioc.error ) , 0 );
112                break;
113            }
114            else if( status == BDV_BUSY )   // non completed
115            {
116                continue;
117            }
118            else                            // error reported
119            {
120                hal_remote_sw( XPTR( th_cxy , &th_ptr->command.ioc.error ) , 1 );
121                break;
122            }
123        }
124    }
125    else                                            // descheduling + IRQ policy
126    { 
127        thread_block( CURRENT_THREAD , THREAD_BLOCKED_DEV_ISR );
128        sched_yield();
129    }
[1]130   
131} // end soclib_bdv_cmd()
132
133
[4]134/////////////////////////////////////////////////////////////////
135void __attribute__ ((noinline)) soclib_bdv_isr( chdev_t * chdev )
[1]136{
137    // get extended pointer on client thread
[4]138    xptr_t root = XPTR( local_cxy , &chdev->wait_root );
[1]139    xptr_t client_xp = XLIST_FIRST_ELEMENT( root , thread_t , wait_list );
140
141    // get extended pointer on server thread
[4]142    xptr_t server_xp = XPTR( local_cxy , &chdev->server );
[1]143
144    // get client thread cluster and local pointer
145    cxy_t      client_cxy = GET_CXY( client_xp );
146    thread_t * client_ptr = (thread_t *)GET_PTR( client_xp );
147
148    // get SOCLIB_BDV device cluster and local pointer
[4]149    cxy_t      bdv_cxy  = GET_CXY( chdev->base );
150    uint32_t * bdv_ptr  = (uint32_t *)GET_PTR( chdev->base );
[1]151
152    // get BDV status register and acknowledge IRQ
153        uint32_t status = hal_remote_lw( XPTR( bdv_cxy , bdv_ptr + BDV_STATUS_REG ) );   
154
155    // set operation status in command
156        if((status != BDV_READ_SUCCESS) && (status != BDV_WRITE_SUCCESS)) 
157    {
[4]158        hal_remote_sw( XPTR( client_cxy , &client_ptr->command.ioc.error ) , 1 );
[1]159    }
160        else
161    {
[4]162        hal_remote_sw( XPTR( client_cxy , &client_ptr->command.ioc.error ) , 0 );
[1]163    }
164
165    // unblock server thread
166    thread_unblock( server_xp , THREAD_BLOCKED_DEV_ISR );
167
168    // unblock client thread
169    thread_unblock( client_xp , THREAD_BLOCKED_IO );
170
171} // end soclib_bdv_isr()
172
173
174
Note: See TracBrowser for help on using the repository browser.