source: trunk/hal/tsar_mips32/drivers/soclib_hba.c @ 520

Last change on this file since 520 was 520, checked in by viala@…, 6 years ago

[soclib_hba] Use assert instead of printk + Hal_core_sleep for panic.

File size: 10.3 KB
Line 
1/*
2 * soclib_hba.c - soclib AHCI 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 <printk.h>
25#include <hal_special.h>
26#include <chdev.h>
27#include <dev_ioc.h>
28#include <soclib_hba.h>
29#include <spinlock.h>
30#include <thread.h>
31
32//////////////////////////////////////////////////////////////////////////////////
33//   SOCLIB_HBA specific global variables
34//////////////////////////////////////////////////////////////////////////////////
35       
36// command list : up to 32 commands
37__attribute__((section(".kdata")))
38hba_cmd_desc_t     hba_cmd_list[32] __attribute__((aligned(0x40)));   
39
40// command tables array : one command table per entry in command list
41__attribute__((section(".kdata")))
42hba_cmd_table_t    hba_cmd_table[32] __attribute__((aligned(0x40))); 
43
44// extended pointer on the owner thread, for each slot
45__attribute__((section(".kdata")))
46xptr_t             hba_owner_thread[32];
47
48// bit vector of active slots
49__attribute__((section(".kdata")))
50uint32_t           hba_active_slots; 
51
52// spinlock protecting the command slot allocator
53__attribute__((section(".kdata")))
54spinlock_t         hba_lock;
55
56///////////////////////////////////////
57void soclib_hba_init( chdev_t * chdev )
58{
59    // get hardware device base address
60        xptr_t  hba_xp = chdev->base;
61
62    // set driver specific fields
63    chdev->cmd = &soclib_hba_cmd;
64    chdev->isr = &soclib_hba_isr;
65
66    // get hardware device cluster and local pointer
67    cxy_t      hba_cxy  = GET_CXY( hba_xp );
68    uint32_t * hba_ptr  = (uint32_t *)GET_PTR( hba_xp );
69
70    // get block_size and block_count 
71        uint32_t block_size  = hal_remote_lw( XPTR( hba_cxy , hba_ptr + HBA_BLOCK_SIZE_REG ) );
72        uint32_t block_count = hal_remote_lw( XPTR( hba_cxy , hba_ptr + HBA_BLOCK_COUNT_REG ) );
73
74    // set device descriptor extension
75    chdev->ext.ioc.size  = block_size;
76    chdev->ext.ioc.count = block_count;
77
78    // activate HBA interrupts
79    hal_remote_sw( XPTR( hba_cxy , hba_ptr + HBA_PXIE_REG ) , 0x1 );
80
81        // reset SOCLIB_HBA driver global variable
82    hba_active_slots = 0;
83
84} // end soclib_hba_init()
85
86
87//////////////////////////////////////////////////////////////
88void __attribute__ ((noinline)) soclib_hba_cmd( xptr_t th_xp )
89{
90
91    uint32_t           cmd_type;     // IOC_READ / IOC_WRITE / IOC_SYNC_READ
92    uint32_t           lba;          // lba    : command argument
93    uint32_t           count;        // count  : command argument
94    xptr_t             buf_xp;       // buffer : command argument
95    xptr_t             dev_xp;       // device : command argument
96
97    uint32_t           cmd_id;       // current slot index in command bit_vector
98    hba_cmd_desc_t   * cmd_desc;     // command descriptor pointer   
99    hba_cmd_table_t  * cmd_table;    // command table pointer
100
101    bool_t             found;
102    uint32_t           iter; 
103
104    // get client thread cluster and local pointer
105    cxy_t      th_cxy = GET_CXY( th_xp );
106    thread_t * th_ptr = GET_PTR( th_xp );
107
108    // get command arguments and extended pointer on IOC device
109    cmd_type  =         hal_remote_lw ( XPTR( th_cxy , &th_ptr->ioc_cmd.type   ) );
110    lba       =         hal_remote_lw ( XPTR( th_cxy , &th_ptr->ioc_cmd.lba    ) );
111    count     =         hal_remote_lw ( XPTR( th_cxy , &th_ptr->ioc_cmd.count  ) );
112    buf_xp    = (xptr_t)hal_remote_lwd( XPTR( th_cxy , &th_ptr->ioc_cmd.buf_xp ) );
113    dev_xp    = (xptr_t)hal_remote_lwd( XPTR( th_cxy , &th_ptr->ioc_cmd.dev_xp ) );
114
115    // get IOC device cluster and local pointer
116    cxy_t     dev_cxy = GET_CXY( dev_xp );
117    chdev_t * dev_ptr = GET_PTR( dev_xp );
118
119    // get cluster and pointers for SOCLIB-HBA peripheral segment base
120    xptr_t     hba_xp  = (xptr_t)hal_remote_lwd( XPTR( dev_cxy , &dev_ptr->base ) );
121    cxy_t      hba_cxy = GET_CXY( hba_xp );
122    uint32_t * hba_ptr = GET_PTR( hba_xp );
123
124    // try to register the I/O operation in a free slot
125    // returns if success, deschedule if no slot available
126    // we do not need a lock to access the slot allocator,
127    // because the driver is only called by the server thread.
128    while( 1 )
129    {
130        // try to find a free slot in the 32 slots command list
131        cmd_id = 0;
132        found  = false;
133        for ( iter = 0 ; iter < 32 ; iter++ )
134        {
135            if( (hba_active_slots & (1<<iter) ) == 0 )
136            {
137                found  = true;
138                cmd_id = iter;
139                hba_active_slots |= (1<<iter);
140                break;
141            }
142        }
143
144        if( found )  // slot available in SOCLIB_HBA
145        {
146            // compute pointers on command descriptor and command table   
147            cmd_desc  = &hba_cmd_list[cmd_id];
148            cmd_table = &hba_cmd_table[cmd_id];
149
150            // set  buffer descriptor in command table
151            cmd_table->buffer.dba  = (uint32_t)(buf_xp);   
152            cmd_table->buffer.dbau = (uint32_t)(buf_xp >> 32);
153            cmd_table->buffer.dbc  = count * 512;
154
155            // initialize command table header
156            cmd_table->header.lba0 = (char)lba;
157            cmd_table->header.lba1 = (char)(lba>>8);
158            cmd_table->header.lba2 = (char)(lba>>16);
159            cmd_table->header.lba3 = (char)(lba>>24);
160            cmd_table->header.lba4 = 0;
161            cmd_table->header.lba5 = 0;
162
163            // initialise command descriptor
164            cmd_desc->prdtl[0] = 1;
165            cmd_desc->prdtl[1] = 0;
166            if( cmd_type == IOC_WRITE ) cmd_desc->flag[0] = 0x40;
167            else                        cmd_desc->flag[0] = 0x00;     
168
169#if USE_IOB // software L2/L3 cache coherence
170
171            dev_mmc_sync( cmd_table , sizeof(hba_cmd_table_t) );
172            dev_mmc_sync( cmd_desc , sizeof(hba_cmd_desc_t) );
173
174#endif // end software L2/L3 cache coherence
175
176            // set hba_owner_thread[slot]
177            hba_owner_thread[cmd_id] = th_xp;
178
179            // register slot in bit_vector
180            hba_active_slots |= 1<<cmd_id;
181 
182            // set HBA_PXCI_REG to start transfer
183            hal_remote_sw( XPTR( hba_cxy , hba_ptr + HBA_PXCI_REG ) , 1<<cmd_id );
184
185            // exit the while
186            break;
187        }
188        else   // no slot available in SOCLIB_HBA
189        {
190            // fatal if synchronous access
191            assert( cmd_type == IOC_SYNC_READ, "no slot available for a IOC_SYNC_READ\n" );
192            sched_yield( "blocked on ISR" );
193        }
194        }
195    }  // end while to get a slot
196
197    // waiting policy depends on the command type
198
199    if( cmd_type == IOC_SYNC_READ )                // polling, busy waiting
200    {
201        uint32_t  pxis;
202        uint32_t  pxci;
203        uint32_t  error;
204        uint32_t  fault_id;
205        while(1)
206        {
207            pxis     = hal_remote_lw( XPTR( hba_cxy , hba_ptr + HBA_PXIS_REG ) );
208            pxci     = hal_remote_lw( XPTR( hba_cxy , hba_ptr + HBA_PXCI_REG ) );
209            error    = (pxis & 0x40000000) >> 30;
210            fault_id = (pxis & 0x1F000000) >> 24;
211
212            if( (pxci & (1<<cmd_id)) == 0 )  // completed
213            {
214                // release slot
215                hba_active_slots &= ~(1<<cmd_id);
216
217                // set operation status in client thread command
218                if( error && (fault_id == cmd_id) ) 
219                {
220                    hal_remote_sw( XPTR( th_cxy , &th_ptr->ioc_cmd.error ) , 1 );
221                }
222                else
223                {
224                    hal_remote_sw( XPTR( th_cxy , &th_ptr->ioc_cmd.error ) , 0 );
225                }
226
227                // exit while
228                break;
229            }   
230        }
231    }
232    else                                           // descheduling + IRQ
233    {
234        thread_block( XPTR( local_cxy , CURRENT_THREAD ) , THREAD_BLOCKED_ISR );
235        sched_yield( "blocked on ISR" );
236    }
237           
238} // end soclib_hba_cmd()
239
240
241/////////////////////////////////////////////////////////////////
242void __attribute__ ((noinline)) soclib_hba_isr( chdev_t * chdev )
243{
244    // get extended pointer on client thread
245    xptr_t root      = XPTR( local_cxy , &chdev->wait_root );
246    xptr_t client_xp = XLIST_FIRST_ELEMENT( root , thread_t , wait_list );
247
248    // get client thread cluster and local pointer
249    cxy_t      client_cxy = GET_CXY( client_xp );
250    thread_t * client_ptr = (thread_t *)GET_PTR( client_xp );
251
252    // get SOCLIB_HBA device cluster and local pointer
253    cxy_t      hba_cxy  = GET_CXY( chdev->base );
254    uint32_t * hba_ptr  = (uint32_t *)GET_PTR( chdev->base );
255
256    // get HBA_PXIS_REG and HBA_PXCI_REG current values
257    uint32_t current_pxis = hal_remote_lw( XPTR( hba_cxy , hba_ptr + HBA_PXIS_REG ) );
258    uint32_t current_pxci = hal_remote_lw( XPTR( hba_cxy , hba_ptr + HBA_PXCI_REG ) );
259
260    uint32_t  error    = (current_pxis & 0x40000000) >> 30;
261    uint32_t  fault_id = (current_pxis & 0x1F000000) >> 24;
262    uint32_t  iter;
263
264    // loop on active commands to signal one or several completed I/O operations
265    for( iter = 0 ; iter < 32 ; iter++ )
266    {
267        if ( ( (hba_active_slots & (1<<iter)) != 0 ) &&  // active command
268             ( (current_pxci     & (1<<iter)) == 0 ) )   // completed command
269        {
270            // release the slot
271            hba_active_slots &= ~(1<<iter);
272
273            // set operation status in client thread command
274            if( error && (iter == fault_id ) ) 
275            {
276                hal_remote_sw( XPTR( client_cxy , &client_ptr->ioc_cmd.error ) , 1 );
277            }
278            else
279            {
280                hal_remote_sw( XPTR( client_cxy , &client_ptr->ioc_cmd.error ) , 0 );
281            }
282
283            // unblock client thread
284            thread_unblock( client_xp , THREAD_BLOCKED_IO );
285        }
286    }
287
288    // reset HBA_PXIS_REG
289    hal_remote_sw( XPTR( hba_cxy , hba_ptr + HBA_PXIS_REG ) , 0 );
290
291} // end soclib_hba_isr()
292
293
294
Note: See TracBrowser for help on using the repository browser.