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

Last change on this file since 459 was 440, checked in by alain, 6 years ago

1/ Fix a bug in the Multithreaded "sort" applicationr:
The pthread_create() arguments must be declared as global variables.
2/ The exit syscall can be called by any thread of a process..

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