source: trunk/kernel/devices/dev_icu.c @ 9

Last change on this file since 9 was 3, checked in by alain, 5 years ago

Introduce dev_fbf, dev dma, dev_iob

File size: 12.4 KB
Line 
1/*
2 * dev_icu.c - ICU (Interrupt Controler Unit) generic device API implementation.
3 *
4 * Authors   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 <hal_types.h>
25#include <hal_special.h>
26#include <chdev.h>
27#include <thread.h>
28#include <cluster.h>
29#include <printk.h>
30#include <memcpy.h>
31#include <spinlock.h>
32#include <soclib_xcu.h>
33#include <dev_icu.h>
34
35/////////////////////////////////////////////////////////////////////////////////////////
36// Extern global variables
37/////////////////////////////////////////////////////////////////////////////////////////
38
39extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c
40
41extern chdev_pic_input_t  chdev_pic_input;   // allocated in kernel_init.c
42
43/////////////////////////////////
44void dev_icu_init( chdev_t * icu,
45                   uint32_t  hwi_nr,
46                   uint32_t  wti_nr,
47                   uint32_t  pti_nr )
48{
49    // set ICU chdev extension fields
50    icu->ext.icu.hwi_nr     = hwi_nr;
51    icu->ext.icu.wti_nr     = wti_nr;
52    icu->ext.icu.pti_nr     = pti_nr;
53    icu->ext.icu.wti_bitmap = 0;
54    spinlock_init( &icu->ext.icu.wti_lock );
55   
56    // get implementation
57    uint32_t impl = icu->impl;
58
59    // call the relevant driver init function
60    if( impl == IMPL_ICU_XCU )
61    {
62        uint32_t  lid;
63        for( lid = 0 ; lid < LOCAL_CLUSTER->cores_nr ; lid++ )
64        {
65            soclib_xcu_init( icu , lid );
66        }
67    }
68    else
69    {
70        assert( false , __FUNCTION__ , "undefined ICU implementation" );
71    }
72} // end dev_icu_init()
73
74/////////////////////////////////////////////////////////////////////////////////////
75// This static function check the irq_type / irq_index arguments.
76// It is called by the dev_icu_enable_irq() & dev_icu_disable_irq() functions.
77/////////////////////////////////////////////////////////////////////////////////////
78static inline void dev_icu_check_irq( chdev_t  * icu,
79                                      uint32_t   irq_type,
80                                      uint32_t   irq_index )
81{
82    if( irq_type == HWI_TYPE ) 
83    {
84        assert( (irq_index < icu->ext.icu.hwi_nr) , __FUNCTION__ , "illegal HWI" );
85    }
86    if( irq_type == WTI_TYPE )
87    {
88        assert( (irq_index < icu->ext.icu.wti_nr) , __FUNCTION__ , "illegal WTI" );
89    }
90    if( irq_type == PTI_TYPE )
91    {
92        assert( (irq_index < icu->ext.icu.pti_nr) , __FUNCTION__ , "illegal PTI" );
93    }
94}  // end dev_icu_check_irq()
95
96////////////////////////////////////////
97void dev_icu_enable_irq( lid_t      lid,
98                         uint32_t   irq_type,
99                         uint32_t   irq_index,
100                         chdev_t  * src_chdev )
101{
102    // get local pointer on local ICU chdev
103    xptr_t    icu_xp = chdev_dir.icu[local_cxy];
104    chdev_t * icu    = (chdev_t *)GET_PTR( icu_xp );
105
106    // check IRQ type and index
107    dev_icu_check_irq( icu , irq_type , irq_index );
108
109    // (1) call implementation specific ICU driver to enable IRQ
110    if( icu->impl == IMPL_ICU_XCU ) 
111    {
112        soclib_xcu_enable_irq( icu , 1<<irq_index , irq_type , lid );
113    }
114
115    // (2) get selected core local pointer, and register
116    // source chdev pointer in relevant interrupt vector
117    core_t * core = &LOCAL_CLUSTER->core_tbl[lid];
118        core_set_irq_vector_entry( core , irq_type , irq_index , src_chdev );
119
120    // (3) register IRQ type and index in source chdev descriptor
121    src_chdev->irq_type = irq_type;
122    src_chdev->irq_id   = irq_index;
123
124}  // end dev_icu_enable_irq()
125
126/////////////////////////////////////////
127void dev_icu_disable_irq( lid_t      lid,
128                          uint32_t   irq_type,
129                          uint32_t   irq_index )
130{
131    // get local pointer on local ICU chdev
132    xptr_t    icu_xp = chdev_dir.icu[local_cxy];
133    chdev_t * icu    = (chdev_t *)GET_PTR( icu_xp );
134
135    // check IRQ type and index
136    dev_icu_check_irq( icu , irq_type , irq_index );
137
138    // (1) call the implementation specific ICU driver to disable IRQ
139    if( icu->impl == IMPL_ICU_XCU ) 
140    {
141        soclib_xcu_disable_irq( icu , 1<<irq_index , irq_type , lid );
142    }
143
144    // (2) get selected remote core local pointer, and remove
145    // the source chdev xptr from relevant interrupt vector
146
147    core_t * core = &LOCAL_CLUSTER->core_tbl[lid];
148        core_set_irq_vector_entry( core , irq_type , irq_index , NULL );
149
150} // end dev_icu_disable_irq()
151
152//////////////////////////////////////////////
153void dev_icu_set_period( uint32_t   pti_index,
154                         uint32_t   period )
155{
156    // get local pointer on local ICU chdev
157    xptr_t    icu_xp = chdev_dir.icu[local_cxy];
158    chdev_t * icu    = (chdev_t *)GET_PTR( icu_xp );
159
160    // check PTI index
161    assert( (pti_index < icu->ext.icu.pti_nr) , __FUNCTION__ , "illegal PTI index" ); 
162
163    // call the implementation specific driver ICU to set period
164    if( icu->impl == IMPL_ICU_XCU ) 
165    {
166        soclib_xcu_set_period( icu , pti_index , period );
167    }
168}  // end dev_icu_set_period()
169
170////////////////////////////////////////////
171void dev_icu_ack_timer( uint32_t pti_index )
172{
173    // get local pointer on local ICU chdev
174    xptr_t    icu_xp = chdev_dir.icu[local_cxy];
175    chdev_t * icu    = (chdev_t *)GET_PTR( icu_xp );
176
177    // check PTI index
178    assert( (pti_index < icu->ext.icu.pti_nr) , __FUNCTION__ , "illegal PTI index" ); 
179
180    // call the implementation specific driver ICU to acknowledge PTI IRQ
181    if( icu->impl == IMPL_ICU_XCU ) 
182    {
183        soclib_xcu_ack_timer( icu , pti_index );
184    }
185}  // end dev_icu_ack_timer()
186
187////////////////////////////////////
188void dev_icu_send_ipi( cxy_t    cxy,
189                       lid_t    lid )
190{
191    // check arguments
192    cluster_t * cluster  = LOCAL_CLUSTER;
193    uint32_t    y_width  = cluster->y_width;
194    uint32_t    x_size   = cluster->x_size;
195    uint32_t    y_size   = cluster->y_size;
196    uint32_t    cores_nr = cluster->cores_nr;
197    uint32_t    x = cxy >> y_width;
198    uint32_t    y = cxy & ((1<<y_width)-1);
199
200    assert( ((x < x_size) && (y < y_size)) , __FUNCTION__ , "illegal cluster identifier" );
201
202    assert( (lid < cores_nr) , __FUNCTION__ , "illegal core local index" );
203
204    // get extended pointer on target ICU chdev
205    xptr_t icu_xp = chdev_dir.icu[cxy];
206
207     // get target ICU cluster and local pointer
208    cxy_t     icu_cxy = GET_CXY( icu_xp );
209    chdev_t * icu_ptr = (chdev_t *)GET_PTR( icu_xp );
210
211    // get implementation from remote ICU chdev
212    uint32_t impl = hal_remote_lw( XPTR( icu_cxy , &icu_ptr->impl ) );   
213
214    // call the implementation specific ICU driver to send IPI
215    if( impl == IMPL_ICU_XCU ) 
216    {
217        soclib_xcu_send_ipi( icu_xp , lid );
218    }
219}  // end dev_icu_send_ipi()
220
221//////////////////////////
222void dev_icu_irq_handler()
223{
224    uint32_t   hwi_status;   // HWI index + 1  / no pending HWI if 0
225    uint32_t   wti_status;   // WTI index + 1  / no pending WTI if 0
226    uint32_t   pti_status;   // PTI index + 1  / no pending PTI if 0
227    chdev_t  * src_chdev;    // pointer on source chdev descriptor
228    uint32_t   index;        // IRQ index
229
230    core_t   * core = CURRENT_CORE;
231
232    // get local pointer on local ICU chdev
233    xptr_t    icu_xp = chdev_dir.icu[local_cxy];
234    chdev_t * icu    = (chdev_t *)GET_PTR( icu_xp );
235
236    // call the implementation specific ICU driver 
237    // to return highest priority pending IRQ of each type
238    if( icu->impl == IMPL_ICU_XCU )
239    {
240        soclib_xcu_get_status( icu , core->lid , &hwi_status , &wti_status , &pti_status );
241    }
242
243    // analyse ICU status and handle up to 3 pending IRQ (one WTI, one HWI, one PTI)
244
245    if( wti_status )          // pending WTI
246        {
247        index = wti_status - 1;
248
249        if( index < LOCAL_CLUSTER->cores_nr )   // it is an IPI
250        {
251            assert( (index == core->lid) , __FUNCTION__ , "illegal IPI index" );
252
253            // TODO acknowledge WTI [AG]
254
255            // TODO force scheduling [AG]
256        }
257        else                                    // it is an external device
258        {
259            // get pointer on IRQ source chdev
260                    src_chdev = core->wti_vector[index];
261
262                    if( src_chdev == NULL )        // strange, but not fatal => disable IRQ
263                    {
264                printk("\n[WARNING] in %s : no handler for WTI %d on core %d in cluster %x\n",
265                       __FUNCTION__ , index , core->lid , local_cxy );
266                    core->spurious_irqs ++;
267                dev_icu_disable_irq( core->lid , WTI_TYPE , index ); 
268            }
269            else                                 // call relevant ISR
270            {
271                        icu_dmsg("\n[INFO] %s received WTI : index = %d for cpu %d in cluster %d\n",
272                         __FUNCTION__ , index , core->lid , local_cxy );
273
274                // call ISR
275                    src_chdev->isr( src_chdev );
276            }
277        }
278        }
279
280        if( hwi_status )      // pending HWI
281        {
282        index = hwi_status - 1;
283
284        // get pointer on IRQ source chdev
285                src_chdev = core->hwi_vector[index];
286
287                if( src_chdev == NULL )        // strange, but not fatal => disable IRQ
288                {
289            printk("\n[WARNING] in %s : no handler for HWI %d on core %d in cluster %x\n",
290                   __FUNCTION__ , index , core->lid , local_cxy );
291                core->spurious_irqs ++;
292            dev_icu_disable_irq( core->lid , HWI_TYPE , index ); 
293                }
294        else                    // call relevant ISR
295        {
296                    icu_dmsg("\n[INFO] %s received HWI : index = %d for cpu %d in cluster %d\n",
297                     __FUNCTION__ , index , core->lid , local_cxy );
298
299            // call ISR
300                    src_chdev->isr( src_chdev );
301        }
302        }
303
304    if( pti_status )      // pending PTI
305        {
306        index = pti_status - 1;
307
308                icu_dmsg("\n[INFO] %s received PTI : index = %d for cpu %d in cluster %d\n",
309                 __FUNCTION__ , index , core->lid , local_cxy );
310
311        // acknowledge PTI
312        dev_icu_ack_timer( index );
313
314        // TODO execute all actions related to TICK event
315        core_clock( core );
316        }
317}  // end dev_icu_irq_handler()
318
319////////////////////////////
320uint32_t dev_icu_wti_alloc()
321{
322    // get local pointer on local ICU chdev
323    xptr_t    icu_xp = chdev_dir.icu[local_cxy];
324    chdev_t * icu    = (chdev_t *)GET_PTR( icu_xp );
325
326    // get bitmap pointer, lock pointer, and size
327    uint32_t   * bitmap = &icu->ext.icu.wti_bitmap;
328    spinlock_t * lock   = &icu->ext.icu.wti_lock;
329    uint32_t     size   =  icu->ext.icu.wti_nr;
330
331    // get lock protecting WTI allocator
332    spinlock_lock( lock );
333
334    // get first free mailbox index
335    uint32_t index = (uint32_t)bitmap_ffc( bitmap , size );
336 
337    // set bitmap entry if found
338    if( index < size ) bitmap_set( bitmap , index );
339
340    // release lock
341    spinlock_unlock( lock );
342
343    return index;
344}  // end dev_icu_wti_alloc()
345
346//////////////////////////////////////////
347void dev_icu_wti_release( uint32_t index )
348{
349    // get pointer on local ICU chdev descriptor
350    xptr_t    icu_xp  = chdev_dir.icu[local_cxy];
351    chdev_t * icu_ptr = (chdev_t *)GET_PTR( icu_xp );
352
353    // get bitmap pointer, lock, and size
354    bitmap_t   * bitmap = &icu_ptr->ext.icu.wti_bitmap;
355    spinlock_t * lock   = &icu_ptr->ext.icu.wti_lock;
356    uint32_t     size   =  icu_ptr->ext.icu.wti_nr;
357
358    // check index
359    assert( (index < size) , __FUNCTION__ , "illegal WTI index" );
360
361    // get lock protecting WTI allocator
362    spinlock_lock( lock );
363
364    // clear bitmap entry
365    bitmap_clear( bitmap , index );
366
367    // release lock
368    spinlock_unlock( lock );
369
370}  // end dev_icu_wti_release()
371
372//////////////////////////////////////////////
373uint32_t * dev_icu_wti_ptr( uint32_t  wti_id )
374{
375    uint32_t *  wti_ptr = NULL;
376
377    // get pointer on local ICU chdev descriptor
378    xptr_t    icu_xp  = chdev_dir.icu[local_cxy];
379    chdev_t * icu     = (chdev_t *)GET_PTR( icu_xp );
380
381    // call implementation specific ICU driver
382    if( icu->impl == IMPL_ICU_XCU )
383    {
384        wti_ptr = soclib_xcu_wti_ptr( icu , wti_id );   
385    }
386
387    return wti_ptr;
388}   // end dev_icu_wti_xptr()
389
390
Note: See TracBrowser for help on using the repository browser.