source: trunk/hal/tsar_mips32/drivers/soclib_bdv.c @ 419

Last change on this file since 419 was 408, checked in by alain, 7 years ago

Fix several bugs in the fork() syscall.

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