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

Last change on this file since 14 was 4, checked in by alain, 8 years ago

Introduce the chdev_t structure in place of device_t.

File size: 5.3 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 <thread.h>
28#include <spinlock.h>
29
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    // 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
46    chdev->ext.ioc.size  = block_size;
47    chdev->ext.ioc.count = block_count;
48
49} // end soclib_bdv_init()
50
51
52//////////////////////////////////////////////////////////////
53void __attribute__ ((noinline)) soclib_bdv_cmd( xptr_t th_xp )
54{
55    uint32_t   to_mem;       // command argument
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
66    to_mem =         hal_remote_lw ( XPTR( th_cxy , &th_ptr->command.ioc.to_mem ) );
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 ) );
71
72    // get IOC device cluster and local pointer
73    cxy_t      dev_cxy = GET_CXY( dev_xp );
74    chdev_t  * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
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
87    // get access type
88    uint32_t   type =  to_mem ? BDV_OP_READ : BDV_OP_WRITE; 
89
90    // set SOCLIB_BDV registers to start one I/O operation
91    hal_remote_sw( XPTR( bdv_cxy , bdv_ptr + BDV_IRQ_ENABLE_REG ) , 1       );
92    hal_remote_sw( XPTR( bdv_cxy , bdv_ptr + BDV_BUFFER_REG     ) , buf_lsb ); 
93    hal_remote_sw( XPTR( bdv_cxy , bdv_ptr + BDV_BUFFER_EXT_REG ) , buf_msb ); 
94    hal_remote_sw( XPTR( bdv_cxy , bdv_ptr + BDV_LBA_REG        ) , lba     );
95    hal_remote_sw( XPTR( bdv_cxy , bdv_ptr + BDV_COUNT_REG      ) , count   );
96    hal_remote_sw( XPTR( bdv_cxy , bdv_ptr + BDV_OP_REG         ) , type    );
97
98    // Block and deschedule server thread
99    thread_block( CURRENT_THREAD , THREAD_BLOCKED_DEV_ISR );
100    sched_yield();
101   
102} // end soclib_bdv_cmd()
103
104
105/////////////////////////////////////////////////////////////////
106void __attribute__ ((noinline)) soclib_bdv_isr( chdev_t * chdev )
107{
108    // get extended pointer on client thread
109    xptr_t root = XPTR( local_cxy , &chdev->wait_root );
110    xptr_t client_xp = XLIST_FIRST_ELEMENT( root , thread_t , wait_list );
111
112    // get extended pointer on server thread
113    xptr_t server_xp = XPTR( local_cxy , &chdev->server );
114
115    // get client thread cluster and local pointer
116    cxy_t      client_cxy = GET_CXY( client_xp );
117    thread_t * client_ptr = (thread_t *)GET_PTR( client_xp );
118
119    // get SOCLIB_BDV device cluster and local pointer
120    cxy_t      bdv_cxy  = GET_CXY( chdev->base );
121    uint32_t * bdv_ptr  = (uint32_t *)GET_PTR( chdev->base );
122
123    // get BDV status register and acknowledge IRQ
124        uint32_t status = hal_remote_lw( XPTR( bdv_cxy , bdv_ptr + BDV_STATUS_REG ) );   
125
126    // set operation status in command
127        if((status != BDV_READ_SUCCESS) && (status != BDV_WRITE_SUCCESS)) 
128    {
129        hal_remote_sw( XPTR( client_cxy , &client_ptr->command.ioc.error ) , 1 );
130    }
131        else
132    {
133        hal_remote_sw( XPTR( client_cxy , &client_ptr->command.ioc.error ) , 0 );
134    }
135
136    // unblock server thread
137    thread_unblock( server_xp , THREAD_BLOCKED_DEV_ISR );
138
139    // unblock client thread
140    thread_unblock( client_xp , THREAD_BLOCKED_IO );
141
142} // end soclib_bdv_isr()
143
144
145
Note: See TracBrowser for help on using the repository browser.