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

Last change on this file since 395 was 296, checked in by alain, 7 years ago

Several modifs in the generic scheduler and in the hal_context to
fix the context switch mechanism.

File size: 10.7 KB
Line 
1/*
2 * soclib_hba.c - soclib AHCI 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 <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 = (thread_t *)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 = (chdev_t *)GET_PTR( dev_xp );
118
119    // get extended pointer on SOCLIB-HBA peripheral
120    xptr_t     hba_xp = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->base ) );
121
122    // get SOCLIB_HBA device cluster and local pointer
123    cxy_t      hba_cxy = GET_CXY( hba_xp );
124    uint32_t * hba_ptr = (uint32_t *)GET_PTR( hba_xp );
125
126    // try to register the I/O operation in a free slot
127    // returns if success, deschedule if no slot available
128    // we do not need a lock to access the slot allocator,
129    // because the driver is only called by the server thread.
130    while( 1 )
131    {
132        // try to find a free slot in the 32 slots command list
133        cmd_id = 0;
134        found  = false;
135        for ( iter = 0 ; iter < 32 ; iter++ )
136        {
137            if( (hba_active_slots & (1<<iter) ) == 0 )
138            {
139                found  = true;
140                cmd_id = iter;
141                hba_active_slots |= (1<<iter);
142                break;
143            }
144        }
145
146        if( found )  // slot available in SOCLIB_HBA
147        {
148            // compute pointers on command descriptor and command table   
149            cmd_desc  = &hba_cmd_list[cmd_id];
150            cmd_table = &hba_cmd_table[cmd_id];
151
152            // set  buffer descriptor in command table
153            cmd_table->buffer.dba  = (uint32_t)(buf_xp);   
154            cmd_table->buffer.dbau = (uint32_t)(buf_xp >> 32);
155            cmd_table->buffer.dbc  = count * 512;
156
157            // initialize command table header
158            cmd_table->header.lba0 = (char)lba;
159            cmd_table->header.lba1 = (char)(lba>>8);
160            cmd_table->header.lba2 = (char)(lba>>16);
161            cmd_table->header.lba3 = (char)(lba>>24);
162            cmd_table->header.lba4 = 0;
163            cmd_table->header.lba5 = 0;
164
165            // initialise command descriptor
166            cmd_desc->prdtl[0] = 1;
167            cmd_desc->prdtl[1] = 0;
168            if( cmd_type == IOC_WRITE ) cmd_desc->flag[0] = 0x40;
169            else                        cmd_desc->flag[0] = 0x00;     
170
171#if USE_IOB // software L2/L3 cache coherence
172
173            dev_mmc_sync( cmd_table , sizeof(hba_cmd_table_t) );
174            dev_mmc_sync( cmd_desc , sizeof(hba_cmd_desc_t) );
175
176#endif // end software L2/L3 cache coherence
177
178            // set hba_owner_thread[slot]
179            hba_owner_thread[cmd_id] = th_xp;
180
181            // register slot in bit_vector
182            hba_active_slots |= 1<<cmd_id;
183 
184            // set HBA_PXCI_REG to start transfer
185            hal_remote_sw( XPTR( hba_cxy , hba_ptr + HBA_PXCI_REG ) , 1<<cmd_id );
186
187            // exit the while
188            break;
189        }
190        else   // no slot available in SOCLIB_HBA
191        {
192            if( cmd_type == IOC_SYNC_READ )     // fatal if synchronous access
193            {
194                printk("\n[PANIC] in %s : no slot available for a SYNC_READ\n", __FUNCTION__ );
195                hal_core_sleep();
196            }
197            else                                // retry if asynchronous access.
198            {
199                sched_yield( NULL );
200            }
201        }
202    }  // end while to get a slot
203
204    // waiting policy depends on the command type
205
206    if( cmd_type == IOC_SYNC_READ )                // polling, busy waiting
207    {
208        uint32_t  pxis;
209        uint32_t  pxci;
210        uint32_t  error;
211        uint32_t  fault_id;
212        while(1)
213        {
214            pxis     = hal_remote_lw( XPTR( hba_cxy , hba_ptr + HBA_PXIS_REG ) );
215            pxci     = hal_remote_lw( XPTR( hba_cxy , hba_ptr + HBA_PXCI_REG ) );
216            error    = (pxis & 0x40000000) >> 30;
217            fault_id = (pxis & 0x1F000000) >> 24;
218
219            if( (pxci & (1<<cmd_id)) == 0 )  // completed
220            {
221                // release slot
222                hba_active_slots &= ~(1<<cmd_id);
223
224                // set operation status in client thread command
225                if( error && (fault_id == cmd_id) ) 
226                {
227                    hal_remote_sw( XPTR( th_cxy , &th_ptr->ioc_cmd.error ) , 1 );
228                }
229                else
230                {
231                    hal_remote_sw( XPTR( th_cxy , &th_ptr->ioc_cmd.error ) , 0 );
232                }
233
234                // exit while
235                break;
236            }   
237        }
238    }
239    else                                           // descheduling + IRQ
240    {
241        thread_block( CURRENT_THREAD , THREAD_BLOCKED_DEV_ISR );
242        sched_yield( NULL );
243    }
244           
245} // end soclib_hba_cmd()
246
247
248/////////////////////////////////////////////////////////////////
249void __attribute__ ((noinline)) soclib_hba_isr( chdev_t * chdev )
250{
251    // get extended pointer on client thread
252    xptr_t root      = XPTR( local_cxy , &chdev->wait_root );
253    xptr_t client_xp = XLIST_FIRST_ELEMENT( root , thread_t , wait_list );
254
255    // get client thread cluster and local pointer
256    cxy_t      client_cxy = GET_CXY( client_xp );
257    thread_t * client_ptr = (thread_t *)GET_PTR( client_xp );
258
259    // get SOCLIB_HBA device cluster and local pointer
260    cxy_t      hba_cxy  = GET_CXY( chdev->base );
261    uint32_t * hba_ptr  = (uint32_t *)GET_PTR( chdev->base );
262
263    // get HBA_PXIS_REG and HBA_PXCI_REG current values
264    uint32_t current_pxis = hal_remote_lw( XPTR( hba_cxy , hba_ptr + HBA_PXIS_REG ) );
265    uint32_t current_pxci = hal_remote_lw( XPTR( hba_cxy , hba_ptr + HBA_PXCI_REG ) );
266
267    uint32_t  error    = (current_pxis & 0x40000000) >> 30;
268    uint32_t  fault_id = (current_pxis & 0x1F000000) >> 24;
269    uint32_t  iter;
270
271    // loop on active commands to signal one or several completed I/O operations
272    for( iter = 0 ; iter < 32 ; iter++ )
273    {
274        if ( ( (hba_active_slots & (1<<iter)) != 0 ) &&  // active command
275             ( (current_pxci     & (1<<iter)) == 0 ) )   // completed command
276        {
277            // release the slot
278            hba_active_slots &= ~(1<<iter);
279
280            // set operation status in client thread command
281            if( error && (iter == fault_id ) ) 
282            {
283                hal_remote_sw( XPTR( client_cxy , &client_ptr->ioc_cmd.error ) , 1 );
284            }
285            else
286            {
287                hal_remote_sw( XPTR( client_cxy , &client_ptr->ioc_cmd.error ) , 0 );
288            }
289
290            // unblock client thread
291            thread_unblock( client_xp , THREAD_BLOCKED_IO );
292 
293            ioc_dmsg("INFO in %s : thread %x at cycle %d\n",
294            __FUNCTION__ , hal_remote_lw( XPTR( client_cxy , &client_ptr->trdid ) ) ,
295            hal_get_cycles() );
296        }
297    }
298
299    // reset HBA_PXIS_REG
300    hal_remote_sw( XPTR( hba_cxy , hba_ptr + HBA_PXIS_REG ) , 0 );
301
302} // end soclib_hba_isr()
303
304
305
Note: See TracBrowser for help on using the repository browser.