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

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

FIX rev520: Previous commit contained a superflous '}'

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    }  // end while to get a slot
195
196    // waiting policy depends on the command type
197
198    if( cmd_type == IOC_SYNC_READ )                // polling, busy waiting
199    {
200        uint32_t  pxis;
201        uint32_t  pxci;
202        uint32_t  error;
203        uint32_t  fault_id;
204        while(1)
205        {
206            pxis     = hal_remote_lw( XPTR( hba_cxy , hba_ptr + HBA_PXIS_REG ) );
207            pxci     = hal_remote_lw( XPTR( hba_cxy , hba_ptr + HBA_PXCI_REG ) );
208            error    = (pxis & 0x40000000) >> 30;
209            fault_id = (pxis & 0x1F000000) >> 24;
210
211            if( (pxci & (1<<cmd_id)) == 0 )  // completed
212            {
213                // release slot
214                hba_active_slots &= ~(1<<cmd_id);
215
216                // set operation status in client thread command
217                if( error && (fault_id == cmd_id) ) 
218                {
219                    hal_remote_sw( XPTR( th_cxy , &th_ptr->ioc_cmd.error ) , 1 );
220                }
221                else
222                {
223                    hal_remote_sw( XPTR( th_cxy , &th_ptr->ioc_cmd.error ) , 0 );
224                }
225
226                // exit while
227                break;
228            }   
229        }
230    }
231    else                                           // descheduling + IRQ
232    {
233        thread_block( XPTR( local_cxy , CURRENT_THREAD ) , THREAD_BLOCKED_ISR );
234        sched_yield( "blocked on ISR" );
235    }
236           
237} // end soclib_hba_cmd()
238
239
240/////////////////////////////////////////////////////////////////
241void __attribute__ ((noinline)) soclib_hba_isr( chdev_t * chdev )
242{
243    // get extended pointer on client thread
244    xptr_t root      = XPTR( local_cxy , &chdev->wait_root );
245    xptr_t client_xp = XLIST_FIRST_ELEMENT( root , thread_t , wait_list );
246
247    // get client thread cluster and local pointer
248    cxy_t      client_cxy = GET_CXY( client_xp );
249    thread_t * client_ptr = (thread_t *)GET_PTR( client_xp );
250
251    // get SOCLIB_HBA device cluster and local pointer
252    cxy_t      hba_cxy  = GET_CXY( chdev->base );
253    uint32_t * hba_ptr  = (uint32_t *)GET_PTR( chdev->base );
254
255    // get HBA_PXIS_REG and HBA_PXCI_REG current values
256    uint32_t current_pxis = hal_remote_lw( XPTR( hba_cxy , hba_ptr + HBA_PXIS_REG ) );
257    uint32_t current_pxci = hal_remote_lw( XPTR( hba_cxy , hba_ptr + HBA_PXCI_REG ) );
258
259    uint32_t  error    = (current_pxis & 0x40000000) >> 30;
260    uint32_t  fault_id = (current_pxis & 0x1F000000) >> 24;
261    uint32_t  iter;
262
263    // loop on active commands to signal one or several completed I/O operations
264    for( iter = 0 ; iter < 32 ; iter++ )
265    {
266        if ( ( (hba_active_slots & (1<<iter)) != 0 ) &&  // active command
267             ( (current_pxci     & (1<<iter)) == 0 ) )   // completed command
268        {
269            // release the slot
270            hba_active_slots &= ~(1<<iter);
271
272            // set operation status in client thread command
273            if( error && (iter == fault_id ) ) 
274            {
275                hal_remote_sw( XPTR( client_cxy , &client_ptr->ioc_cmd.error ) , 1 );
276            }
277            else
278            {
279                hal_remote_sw( XPTR( client_cxy , &client_ptr->ioc_cmd.error ) , 0 );
280            }
281
282            // unblock client thread
283            thread_unblock( client_xp , THREAD_BLOCKED_IO );
284        }
285    }
286
287    // reset HBA_PXIS_REG
288    hal_remote_sw( XPTR( hba_cxy , hba_ptr + HBA_PXIS_REG ) , 0 );
289
290} // end soclib_hba_isr()
291
292
293
Note: See TracBrowser for help on using the repository browser.