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

Last change on this file since 604 was 570, checked in by alain, 6 years ago

Introduction of the soclib_mty driver for the TSAR-LETI architecture.

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