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

Last change on this file since 576 was 570, checked in by alain, 6 years ago

Introduction of the soclib_mty driver for the TSAR-LETI architecture.

File size: 7.9 KB
Line 
1/*
2 * soclib_bdv.c - soclib simple block device driver implementation.
3 *
4 * Author     Alain Greiner (2016,2017,2018)
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 <soclib_bdv.h>
25#include <hal_kernel_types.h>
26#include <chdev.h>
27#include <dev_ioc.h>
28#include <printk.h>
29#include <thread.h>
30
31///////////////////////////////////////
32void soclib_bdv_init( chdev_t * chdev )
33{
34    // get extended pointer on SOCLIB_BDV peripheral base address
35        xptr_t  bdv_xp = chdev->base;
36
37    // set driver specific fields
38    chdev->cmd = &soclib_bdv_cmd;
39    chdev->isr = &soclib_bdv_isr;
40
41    // get hardware device cluster and local pointer
42    cxy_t      bdv_cxy  = GET_CXY( bdv_xp );
43    uint32_t * bdv_ptr  = (uint32_t *)GET_PTR( bdv_xp );
44
45    // get block_size and block_count 
46        uint32_t block_size = hal_remote_l32( XPTR( bdv_cxy , bdv_ptr + BDV_BLOCK_SIZE_REG ) );
47        uint32_t block_count = hal_remote_l32( XPTR( bdv_cxy , bdv_ptr + BDV_SIZE_REG ) );
48
49    // set IOC device descriptor extension
50    chdev->ext.ioc.size  = block_size;
51    chdev->ext.ioc.count = block_count;
52
53} // end soclib_bdv_init()
54
55
56//////////////////////////////////////////////////////////////
57void __attribute__ ((noinline)) soclib_bdv_cmd( xptr_t th_xp )
58{
59    uint32_t   cmd_type;    // IOC_READ / IOC_WRITE / IOC_SYNC_READ
60    uint32_t   lba;
61    uint32_t   count;
62    xptr_t     buf_xp;
63    xptr_t     ioc_xp;
64
65    // get client thread cluster and local pointer
66    cxy_t      th_cxy = GET_CXY( th_xp );
67    thread_t * th_ptr = GET_PTR( th_xp );
68
69    // get command arguments and extended pointer on IOC device
70    cmd_type =         hal_remote_l32 ( XPTR( th_cxy , &th_ptr->ioc_cmd.type   ) );
71    lba      =         hal_remote_l32 ( XPTR( th_cxy , &th_ptr->ioc_cmd.lba    ) );
72    count    =         hal_remote_l32 ( XPTR( th_cxy , &th_ptr->ioc_cmd.count  ) );
73    buf_xp   = (xptr_t)hal_remote_l64( XPTR( th_cxy , &th_ptr->ioc_cmd.buf_xp ) );
74    ioc_xp   = (xptr_t)hal_remote_l64( XPTR( th_cxy , &th_ptr->ioc_cmd.dev_xp ) );
75
76#if DEBUG_HAL_IOC_RX
77uint32_t cycle = (uint32_t)hal_get_cycles();
78if( (DEBUG_HAL_IOC_RX < cycle) && (cmd_type != IOC_WRITE ) )
79printk("\n[DBG] %s : thread %x enter for RX / cycle %d\n",
80__FUNCTION__ , CURRENT_THREAD , cycle );
81#endif
82
83#if DEBUG_HAL_IOC_TX
84uint32_t cycle = (uint32_t)hal_get_cycles();
85if( (DEBUG_HAL_IOC_TX < cycle) && (cmd_type == IOC_WRITE) )
86printk("\n[DBG] %s : thread %x enter for TX / cycle %d\n",
87__FUNCTION__ , CURRENT_THREAD , cycle );
88#endif
89
90    // get IOC device cluster and local pointer
91    cxy_t      ioc_cxy = GET_CXY( ioc_xp );
92    chdev_t  * ioc_ptr = GET_PTR( ioc_xp );
93
94    // get cluster and pointers for SOCLIB-BDV peripheral segment base
95    xptr_t     seg_xp  = (xptr_t)hal_remote_l64( XPTR( ioc_cxy , &ioc_ptr->base ) );
96    cxy_t      seg_cxy = GET_CXY( seg_xp );
97    uint32_t * seg_ptr = GET_PTR( seg_xp );
98
99    // split buffer address in two 32 bits words
100    uint32_t   buf_lsb = (uint32_t)(buf_xp);
101    uint32_t   buf_msb = (uint32_t)(buf_xp>>32);
102
103    // set operation
104    uint32_t   op;
105    if( cmd_type == IOC_WRITE ) op = BDV_OP_WRITE; 
106    else                        op = BDV_OP_READ;
107
108    // set SOCLIB_BDV registers to start one I/O operation
109    hal_remote_s32( XPTR( seg_cxy , seg_ptr + BDV_IRQ_ENABLE_REG ) , 1       );
110    hal_remote_s32( XPTR( seg_cxy , seg_ptr + BDV_BUFFER_REG     ) , buf_lsb ); 
111    hal_remote_s32( XPTR( seg_cxy , seg_ptr + BDV_BUFFER_EXT_REG ) , buf_msb ); 
112    hal_remote_s32( XPTR( seg_cxy , seg_ptr + BDV_LBA_REG        ) , lba     );
113    hal_remote_s32( XPTR( seg_cxy , seg_ptr + BDV_COUNT_REG      ) , count   );
114    hal_remote_s32( XPTR( seg_cxy , seg_ptr + BDV_OP_REG         ) , op      );
115
116    // waiting policy  depends on the command type
117    // - for IOC_READ / IOC_WRITE commands, this function is called by the server thread
118    // - for IOC_SYNC_READ command, this function is directly called by the client thread
119
120    if( cmd_type == IOC_SYNC_READ )                   // status polling policy
121    {
122        uint32_t status;
123        while (1)
124        {
125            status = hal_remote_l32( XPTR( seg_cxy , seg_ptr + BDV_STATUS_REG ) );
126
127            if( status == BDV_READ_SUCCESS ) // successfully completed
128            {
129                hal_remote_s32( XPTR( th_cxy , &th_ptr->ioc_cmd.error ) , 0 );
130                break;
131            }
132            else if( status == BDV_BUSY )   // non completed
133            {
134                continue;
135            }
136            else                            // error reported
137            {
138                hal_remote_s32( XPTR( th_cxy , &th_ptr->ioc_cmd.error ) , 1 );
139                break;
140            }
141        }
142    }
143    else                                            // descheduling + IRQ policy
144    { 
145        thread_block( XPTR( local_cxy , CURRENT_THREAD ) , THREAD_BLOCKED_ISR );
146        sched_yield("blocked on ISR");
147
148        // the IO operation status is reported in the command by the ISR
149    }
150   
151#if DEBUG_HAL_IOC_RX
152cycle = (uint32_t)hal_get_cycles();
153if( (DEBUG_HAL_IOC_RX < cycle) && (cmd_type != TXT_WRITE) )
154printk("\n[DBG] %s : thread %x exit after RX / cycle %d\n",
155__FUNCTION__ , CURRENT_THREAD , cycle );
156#endif
157
158#if DEBUG_HAL_IOC_TX
159cycle = (uint32_t)hal_get_cycles();
160if( (DEBUG_HAL_IOC_TX < cycle) && (cmd_type == TXT_WRITE) )
161printk("\n[DBG] %s : thread %x exit after TX / cycle %d\n",
162__FUNCTION__ , CURRENT_THREAD , cycle );
163#endif
164
165} // end soclib_bdv_cmd()
166
167
168/////////////////////////////////////////////////////////////////
169void __attribute__ ((noinline)) soclib_bdv_isr( chdev_t * chdev )
170{
171    error_t  error = 0;
172
173    // get extended pointer on client thread
174    xptr_t root = XPTR( local_cxy , &chdev->wait_root );
175    xptr_t client_xp = XLIST_FIRST( root , thread_t , wait_list );
176
177    // get extended pointer on server thread
178    xptr_t server_xp = XPTR( local_cxy , &chdev->server );
179
180    // get client thread cluster and local pointer
181    cxy_t      client_cxy = GET_CXY( client_xp );
182    thread_t * client_ptr = (thread_t *)GET_PTR( client_xp );
183
184    // get command type
185    uint32_t   cmd_type = hal_remote_l32( XPTR( client_cxy , &client_ptr->ioc_cmd.type ) );
186   
187    // get SOCLIB_BDV device cluster and local pointer
188    cxy_t      bdv_cxy  = GET_CXY( chdev->base );
189    uint32_t * bdv_ptr  = (uint32_t *)GET_PTR( chdev->base );
190
191    // get BDV status register and acknowledge IRQ
192        uint32_t status = hal_remote_l32( XPTR( bdv_cxy , bdv_ptr + BDV_STATUS_REG ) );   
193
194    if( cmd_type == IOC_READ )
195    {
196            error = (status != BDV_READ_SUCCESS);
197
198#if DEBUG_HAL_IOC_RX
199uint32_t cycle = (uint32_t)hal_get_cycles();
200if( DEBUG_HAL_IOC_RX < cycle )
201printk("\n[DBG] %s : IOC_IRQ / RX transfer / client %x / server %x / cycle %d\n",
202__FUNCTION__, client_ptr , chdev->server , cycle );
203#endif
204
205    }
206        else if( cmd_type == IOC_WRITE )
207    {
208            error = (status != BDV_WRITE_SUCCESS);
209
210#if DEBUG_HAL_IOC_TX
211uint32_t cycle = (uint32_t)hal_get_cycles();
212if( DEBUG_HAL_IOC_TX < cycle )
213printk("\n[DBG] %s : IOC_IRQ / RX transfer / client %x / server %x / cycle %d\n",
214__FUNCTION__, client_ptr , chdev->server , cycle );
215#endif
216
217    }
218    else
219    {
220        assert( false , "IOC_SYNC_READ should not use IRQ" );
221    } 
222
223    // set operation status in command
224    hal_remote_s32( XPTR( client_cxy , &client_ptr->ioc_cmd.error ) , error );
225
226    // unblock server thread
227    thread_unblock( server_xp , THREAD_BLOCKED_ISR );
228
229} // end soclib_bdv_isr()
230
231
232
Note: See TracBrowser for help on using the repository browser.