source: trunk/kernel/drivers/soclib/soclib_hba.c @ 1

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

First import

File size: 10.2 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 <device.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( xptr_t dev_xp )
58{
59    // get IOC device descriptor cluster and local pointer
60    cxy_t      dev_cxy  = GET_CXY( dev_xp );
61    device_t * dev_ptr  = (device_t *)GET_PTR( dev_xp );
62 
63    // get hardware device base address
64        xptr_t     hba_xp = (xptr_t)hal_remote_lwd( XPTR( dev_cxy , &dev_ptr->base ) );
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    hal_remote_sw( XPTR( dev_cxy , &dev_ptr->ext.ioc.size  ) , block_size );
76    hal_remote_sw( XPTR( dev_cxy , &dev_ptr->ext.ioc.count ) , block_count );
77
78        // reset SOCLIB_HBA driver global variables
79    hba_active_slots = 0;
80
81} // end soclib_hba_init()
82
83
84//////////////////////////////////////////////////////////////////
85void __attribute__ ((noinline)) soclib_hba_command( xptr_t th_xp )
86{
87
88    uint32_t           to_mem;       // to_mem : command argument
89    uint32_t           lba;          // lba    : command argument
90    uint32_t           count;        // count  : command argument
91    xptr_t             buf_xp;       // buffer : command argument
92    xptr_t             dev_xp;       // device : command argument
93    uint32_t           cmd_id;       // current slot index in command bit_vector
94    hba_cmd_desc_t   * cmd_desc;     // command descriptor pointer   
95    hba_cmd_table_t  * cmd_table;    // command table pointer
96    bool_t             found;
97    uint32_t           iter; 
98
99    // get client thread cluster and local pointer
100    cxy_t      th_cxy = GET_CXY( th_xp );
101    thread_t * th_ptr = (thread_t *)GET_PTR( th_xp );
102
103    // get command arguments and extended pointer on IOC device
104    to_mem    =         hal_remote_lw ( XPTR( th_cxy , &th_ptr->dev.ioc.to_mem ) );
105    lba       =         hal_remote_lw ( XPTR( th_cxy , &th_ptr->dev.ioc.lba    ) );
106    count     =         hal_remote_lw ( XPTR( th_cxy , &th_ptr->dev.ioc.count  ) );
107    buf_xp    = (xptr_t)hal_remote_lwd( XPTR( th_cxy , &th_ptr->dev.ioc.buf_xp ) );
108    dev_xp    = (xptr_t)hal_remote_lwd( XPTR( th_cxy , &th_ptr->dev.ioc.dev_xp ) );
109
110    // get IOC device cluster and local pointer
111    cxy_t      dev_cxy = GET_CXY( dev_xp );
112    device_t * dev_ptr = (device_t *)GET_PTR( dev_xp );
113
114    // get extended pointer on SOCLIB-HBA peripheral
115    xptr_t     hba_xp = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->base ) );
116
117    // get SOCLIB_HBA device cluster and local pointer
118    cxy_t      hba_cxy = GET_CXY( hba_xp );
119    uint32_t * hba_ptr = (uint32_t *)GET_PTR( hba_xp );
120
121    // try to register the I/O operation in a free slot
122    // returns if success, deschedule if no slot available
123    // we do not need a lock to access the slot allocator,
124    // because the driver is only called by the server thread.
125    while( 1 )
126    {
127        // try to find a free slot in the 32 slots command list
128        cmd_id = 0;
129        found  = false;
130        for ( iter = 0 ; iter < 32 ; iter++ )
131        {
132            if( (hba_active_slots & (1<<iter) ) == 0 )
133            {
134                found  = true;
135                cmd_id = iter;
136                hba_active_slots |= (1<<iter);
137                break;
138            }
139        }
140
141        if( found )  // register and starts the I/O operation
142        {
143            // compute pointers on command descriptor and command table   
144            cmd_desc  = &hba_cmd_list[cmd_id];
145            cmd_table = &hba_cmd_table[cmd_id];
146
147            // set  buffer descriptor in command table
148            cmd_table->buffer.dba  = (uint32_t)(buf_xp);   
149            cmd_table->buffer.dbau = (uint32_t)(buf_xp >> 32);
150            cmd_table->buffer.dbc  = count * 512;
151
152            // initialize command table header
153            cmd_table->header.lba0 = (char)lba;
154            cmd_table->header.lba1 = (char)(lba>>8);
155            cmd_table->header.lba2 = (char)(lba>>16);
156            cmd_table->header.lba3 = (char)(lba>>24);
157            cmd_table->header.lba4 = 0;
158            cmd_table->header.lba5 = 0;
159
160            // initialise command descriptor
161            cmd_desc->prdtl[0] = 1;
162            cmd_desc->prdtl[1] = 0;
163            if( to_mem ) cmd_desc->flag[0] = 0x00;
164            else         cmd_desc->flag[0] = 0x40;     
165
166#if USE_IOB // software L2/L3 cache coherence
167
168            // compute physical addresses
169            paddr_t   cmd_desc_paddr;    // command descriptor physical address
170            paddr_t   cmd_table_paddr;   // command table header physical address
171            bool_t    ident = CONFIG_KERNEL_IDENTITY;
172           
173            error = vmm_v2p_translate( ident , cmd_table , &cmd_table_paddr );
174            if( error )
175            {
176                printk("\n[PANIC] in %s : cannot get paddr fo cmd_table\n", __FUNCTION__ );
177                hal_core_sleep()
178            }
179
180            error = vmm_v2p_translate( ident , cmd_desc , &cmd_desc_paddr );
181            if( error )
182            {
183                printk("\n[PANIC] in %s : cannot get paddr fo cmd_desc\n", __FUNCTION__ );
184                hal_core_sleep()
185            }
186
187            // update L3 for command table
188            dev_mmc_sync( cmd_table_paddr , sizeof(hba_cmd_table_t) );
189
190            // update L3 for command descriptor
191            dev_mmc_sync( cmd_desc_paddr , sizeof(hba_cmd_desc_t) );
192
193#endif // end software L2/L3 cache coherence
194
195            // activates HBA interrupts
196            hal_remote_sw( XPTR( hba_cxy , hba_ptr + HBA_PXIE_REG ) , 0x1 );
197
198            // set hba_owner_thread[slot]
199            hba_owner_thread[cmd_id] = th_xp;
200
201            // set HBA_PXCI_REG to start transfer
202            hal_remote_sw( XPTR( hba_cxy , hba_ptr + HBA_PXCI_REG ) , 1<<cmd_id );
203
204            ioc_dmsg("INFO in %s : thread %x / buffer = %llx at cycle %d\n",
205            __FUNCTION__ , hal_remote_lw( XPTR( th_cxy , &th_ptr->trdid ) ) ,
206            buf_xp , hal_time_stamp() );
207
208            // exit the while
209            break;
210        }
211        else   // deschedule if no slot available in SOCLIB_HBA
212        {
213            sched_yield();
214        }
215    }  // end while
216           
217} // end soclib_hba_cmd()
218
219
220////////////////////////////////////////////////////////////////
221void __attribute__ ((noinline)) soclib_hba_isr( device_t * dev )
222{
223    // get extended pointer on client thread
224    xptr_t root      = XPTR( local_cxy , &dev->wait_root );
225    xptr_t client_xp = XLIST_FIRST_ELEMENT( root , thread_t , wait_list );
226
227    // get client thread cluster and local pointer
228    cxy_t      client_cxy = GET_CXY( client_xp );
229    thread_t * client_ptr = (thread_t *)GET_PTR( client_xp );
230
231    // get SOCLIB_HBA device cluster and local pointer
232    cxy_t      hba_cxy  = GET_CXY( dev->base );
233    uint32_t * hba_ptr  = (uint32_t *)GET_PTR( dev->base );
234
235    // get HBA_PXIS_REG and HBA_PXIS_REG current values
236    uint32_t current_pxis = hal_remote_lw( XPTR( hba_cxy , hba_ptr + HBA_PXIS_REG ) );
237    uint32_t current_pxci = hal_remote_lw( XPTR( hba_cxy , hba_ptr + HBA_PXCI_REG ) );
238
239    uint32_t  error    = (current_pxis & 0x40000000) >> 30;
240    uint32_t  fault_id = (current_pxis & 0x1F000000) >> 24;
241    uint32_t  iter;
242
243    // loop on active commands to signal one or several completed I/O operations
244    for( iter = 0 ; iter < 32 ; iter++ )
245    {
246        if ( ( (hba_active_slots & (1<<iter)) != 0 ) &&  // active command
247             ( (current_pxci     & (1<<iter)) == 0 ) )   // completed command
248        {
249            // release the slot
250            hba_active_slots &= ~(1<<iter);
251
252            // set operation status in client thread command
253            if( error && (iter == fault_id ) ) 
254            {
255                hal_remote_sw( XPTR( client_cxy , &client_ptr->dev.ioc.error ) , 1 );
256            }
257            else
258            {
259                hal_remote_sw( XPTR( client_cxy , &client_ptr->dev.ioc.error ) , 0 );
260            }
261
262            // unblock client thread
263            thread_unblock( client_xp , THREAD_BLOCKED_IO );
264 
265            ioc_dmsg("INFO in %s : thread %x at cycle %d\n",
266            __FUNCTION__ , hal_remote_lw( XPTR( client_cxy , &client_ptr->trdid ) ) ,
267            hal_time_stamp() );
268        }
269    }
270
271    // reset HBA_PXIS_REG
272    hal_remote_sw( XPTR( hba_cxy , hba_ptr + HBA_PXIS_REG ) , 0 );
273
274} // end soclib_hba_isr()
275
276
277
Note: See TracBrowser for help on using the repository browser.