summaryrefslogtreecommitdiffstats
path: root/bsps/include/bsp/irq-generic.h
blob: 0f9bd5765bec5638176a9bfa2a469abc38fdc56c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 * @file
 *
 * @ingroup bsp_interrupt
 *
 * @brief This header file provides interfaces of the generic interrupt
 *   controller support.
 */

/*
 * Copyright (C) 2016 Chris Johns <chrisj@rtems.org>
 *
 * Copyright (C) 2008, 2017 embedded brains GmbH (http://www.embedded-brains.de)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * The API is based on concepts of Pavel Pisa, Till Straumann and Eric Valette.
 */

#ifndef LIBBSP_SHARED_IRQ_GENERIC_H
#define LIBBSP_SHARED_IRQ_GENERIC_H

#include <stdbool.h>

#include <rtems/irq-extension.h>
#include <rtems/score/assert.h>

#ifdef RTEMS_SMP
  #include <rtems/score/atomic.h>
#endif

#include <bsp/irq.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#if !defined(BSP_INTERRUPT_VECTOR_COUNT)
  #error "BSP_INTERRUPT_VECTOR_COUNT shall be defined"
#endif

#if defined(BSP_INTERRUPT_USE_INDEX_TABLE) && !defined(BSP_INTERRUPT_HANDLER_TABLE_SIZE)
  #error "if you define BSP_INTERRUPT_USE_INDEX_TABLE, you have to define BSP_INTERRUPT_HANDLER_TABLE_SIZE etc. as well"
#endif

#ifndef BSP_INTERRUPT_HANDLER_TABLE_SIZE
  #define BSP_INTERRUPT_HANDLER_TABLE_SIZE BSP_INTERRUPT_VECTOR_COUNT
#endif

/* Internal macros for SMP support, do not use externally */
#ifdef RTEMS_SMP
  #define bsp_interrupt_disable(level) do { (void) level; } while (0)
  #define bsp_interrupt_enable(level) do { } while (0)
  #define bsp_interrupt_fence(order) _Atomic_Fence(order)
#else
  #define bsp_interrupt_disable(level) rtems_interrupt_disable(level)
  #define bsp_interrupt_enable(level) rtems_interrupt_enable(level)
  #define bsp_interrupt_fence(order) do { } while (0)
#endif

#define bsp_interrupt_assert(e) _Assert(e)

extern rtems_interrupt_entry bsp_interrupt_handler_table [];

#ifdef BSP_INTERRUPT_USE_INDEX_TABLE
  #if BSP_INTERRUPT_HANDLER_TABLE_SIZE < 0x100
    typedef uint8_t bsp_interrupt_handler_index_type;
  #elif BSP_INTERRUPT_HANDLER_TABLE_SIZE < 0x10000
    typedef uint16_t bsp_interrupt_handler_index_type;
  #else
    typedef uint32_t bsp_interrupt_handler_index_type;
  #endif
  extern bsp_interrupt_handler_index_type bsp_interrupt_handler_index_table [];
#endif

static inline rtems_vector_number bsp_interrupt_handler_index(
  rtems_vector_number vector
)
{
  #ifdef BSP_INTERRUPT_USE_INDEX_TABLE
    return bsp_interrupt_handler_index_table [vector];
  #else
    return vector;
  #endif
}

/**
 * @defgroup bsp_interrupt BSP Interrupt Support
 *
 * @ingroup RTEMSBSPsShared
 *
 * @brief Generic BSP Interrupt Support
 *
 * The BSP interrupt support manages a sequence of interrupt vector numbers
 * greater than or equal to zero and less than @ref BSP_INTERRUPT_VECTOR_COUNT
 * It provides methods to
 * @ref bsp_interrupt_handler_install() "install",
 * @ref bsp_interrupt_handler_remove() "remove" and
 * @ref bsp_interrupt_handler_dispatch() "dispatch" interrupt handlers for each
 * vector number.  It implements parts of the RTEMS interrupt manager.
 *
 * The entry points to a list of interrupt handlers are stored in a table
 * (= handler table).
 *
 * You have to configure the BSP interrupt support in the <bsp/irq.h> file
 * for each BSP.  For a minimum configuration you have to provide
 * @ref BSP_INTERRUPT_VECTOR_COUNT.
 *
 * For boards with small memory requirements you can define
 * @ref BSP_INTERRUPT_USE_INDEX_TABLE.  With an enabled index table the handler
 * table will be accessed via a small index table.  You can define the size of
 * the handler table with @ref BSP_INTERRUPT_HANDLER_TABLE_SIZE.
 *
 * You have to provide some special routines in your BSP (follow the links for
 * the details):
 * - bsp_interrupt_facility_initialize()
 * - bsp_interrupt_vector_enable()
 * - bsp_interrupt_vector_disable()
 * - bsp_interrupt_handler_default()
 *
 * The following now deprecated functions are provided for backward
 * compatibility:
 * - BSP_get_current_rtems_irq_handler()
 * - BSP_install_rtems_irq_handler()
 * - BSP_install_rtems_shared_irq_handler()
 * - BSP_remove_rtems_irq_handler()
 * - BSP_rtems_irq_mngt_set()
 * - BSP_rtems_irq_mngt_get()
 *
 * @{
 */

#ifdef BSP_INTERRUPT_CUSTOM_VALID_VECTOR
  bool bsp_interrupt_is_valid_vector(rtems_vector_number vector);
#else
  /**
   * @brief Returns true if the interrupt vector with number @a vector is
   * valid.
   */
  static inline bool bsp_interrupt_is_valid_vector(rtems_vector_number vector)
  {
    return vector < (rtems_vector_number) BSP_INTERRUPT_VECTOR_COUNT;
  }
#endif

/**
 * @brief Default interrupt handler.
 *
 * This routine will be called from bsp_interrupt_handler_dispatch() with the
 * current vector number @a vector when the handler list for this vector is
 * empty or the vector number is out of range.
 *
 * @note This function must cope with arbitrary vector numbers @a vector.
 */
void bsp_interrupt_handler_default(rtems_vector_number vector);

/**
 * @brief Initialize BSP interrupt support.
 *
 * You must call this function before you can install, remove and dispatch
 * interrupt handlers.  There is no protection against concurrent
 * initialization.  This function must be called at most once.  The BSP
 * specific bsp_interrupt_facility_initialize() function will be called after
 * all internals are initialized.  If the BSP specific initialization fails,
 * then this is a fatal error.  The fatal error source is
 * RTEMS_FATAL_SOURCE_BSP and the fatal error code is
 * BSP_FATAL_INTERRUPT_INITIALIZATION.
 */
void bsp_interrupt_initialize(void);

/**
 * @brief BSP specific initialization.
 *
 * This routine will be called form bsp_interrupt_initialize() and shall do the
 * following:
 * - Initialize the facilities that call bsp_interrupt_handler_dispatch().  For
 * example on PowerPC the external exception handler.
 * - Initialize the interrupt controller.  You shall set the interrupt
 * controller in a state such that interrupts are disabled for all vectors.
 * The vectors will be enabled with your bsp_interrupt_vector_enable() function
 * and disabled via your bsp_interrupt_vector_disable() function.  These
 * functions have to work afterwards.
 *
 * @return On success RTEMS_SUCCESSFUL shall be returned.
 */
rtems_status_code bsp_interrupt_facility_initialize(void);

/**
 * @brief Gets the attributes of the interrupt vector.
 *
 * @param vector is the interrupt vector number.  It shall be valid.
 *
 * @param[out] attributes is the pointer to an rtems_interrupt_attributes
 *   object.  When the function call is successful, the attributes of the
 *   interrupt vector will be stored in this object.  The pointer shall not be
 *   NULL.  The object shall be cleared to zero by the caller.
 *
 * @retval ::RTEMS_SUCCESSFUL The requested operation was successful.
 */
rtems_status_code bsp_interrupt_get_attributes(
  rtems_vector_number         vector,
  rtems_interrupt_attributes *attributes
);

/**
 * @brief Checks if the interrupt is enabled.
 *
 * The function checks if the interrupt associated with the interrupt vector
 * specified by ``vector`` was enabled for the processor executing the function
 * call at some time point during the call.
 *
 * @param vector is the interrupt vector number.  It shall be valid.
 *
 * @param[out] enabled is the pointer to a ``bool`` object.  It shall not be
 *   ``NULL``.  When the function call is successful, the enabled status of
 *   the interrupt associated with the interrupt vector specified by ``vector``
 *   will be stored in this object.  When the interrupt was enabled for the
 *   processor executing the function call at some time point during the call,
 *   the object will be set to true, otherwise to false.
 *
 * @retval ::RTEMS_SUCCESSFUL The requested operation was successful.
 */
rtems_status_code bsp_interrupt_vector_is_enabled(
  rtems_vector_number vector,
  bool               *enabled
);

/**
 * @brief Enables the interrupt vector.
 *
 * This function shall enable the vector at the corresponding facility (in most
 * cases the interrupt controller).  It will be called then the first handler
 * is installed for the vector in bsp_interrupt_handler_install() for example.
 *
 * @note The implementation should use
 * bsp_interrupt_assert( bsp_interrupt_is_valid_vector( vector ) ) to validate
 * the vector number in ::RTEMS_DEBUG configurations.
 *
 * @param vector is the interrupt vector number.
 *
 * @retval ::RTEMS_SUCCESSFUL The requested operation was successful.
 *
 * @retval ::RTEMS_UNSATISFIED The request to enable the interrupt vector has
 *   not been satisfied.
 */
rtems_status_code bsp_interrupt_vector_enable( rtems_vector_number vector );

/**
 * @brief Disables the interrupt vector.
 *
 * This function shall disable the vector at the corresponding facility (in
 * most cases the interrupt controller).  It will be called then the last
 * handler is removed for the vector in bsp_interrupt_handler_remove() for
 * example.
 *
 * @note The implementation should use
 * bsp_interrupt_assert( bsp_interrupt_is_valid_vector( vector ) ) to validate
 * the vector number in ::RTEMS_DEBUG configurations.
 *
 * @param vector is the interrupt vector number.
 *
 * @retval ::RTEMS_SUCCESSFUL The requested operation was successful.
 *
 * @retval ::RTEMS_UNSATISFIED The request to disable the interrupt vector has
 *   not been satisfied.
 */
rtems_status_code bsp_interrupt_vector_disable( rtems_vector_number vector );

/**
 * @brief Checks if the interrupt is pending.
 *
 * The function checks if the interrupt associated with the interrupt vector
 * specified by ``vector`` was pending for the processor executing the function
 * call at some time point during the call.
 *
 * @param vector is the interrupt vector number.  It shall be valid.
 *
 * @param[out] pending is the pointer to a ``bool`` object.  It shall not be
 *   ``NULL``.  When the function call is successful, the pending status of
 *   the interrupt associated with the interrupt vector specified by ``vector``
 *   will be stored in this object.  When the interrupt was pending for the
 *   processor executing the function call at some time point during the call,
 *   the object will be set to true, otherwise to false.
 *
 * @retval ::RTEMS_SUCCESSFUL The requested operation was successful.
 *
 * @retval ::RTEMS_UNSATISFIED The request to get the pending status has not
 *   been satisfied.
 */
rtems_status_code bsp_interrupt_is_pending(
  rtems_vector_number vector,
  bool               *pending
);

/**
 * @brief Causes the interrupt vector.
 *
 * @param vector is the number of the interrupt vector to cause.  It shall be
 *   valid.
 *
 * @retval ::RTEMS_SUCCESSFUL The requested operation was successful.
 *
 * @retval ::RTEMS_UNSATISFIED The request to cause the interrupt vector has
 *   not been satisfied.
 */
rtems_status_code bsp_interrupt_raise( rtems_vector_number vector );

#if defined(RTEMS_SMP)
/**
 * @brief Causes the interrupt vector on the processor.
 *
 * @param vector is the number of the interrupt vector to cause.  It shall be
 *   valid.
 *
 * @param cpu_index is the index of the target processor of the interrupt
 *   vector to cause.  It shall be valid.
 *
 * @retval ::RTEMS_SUCCESSFUL The requested operation was successful.
 *
 * @retval ::RTEMS_UNSATISFIED The request to cause the interrupt vector has
 *   not been satisfied.
 */
rtems_status_code bsp_interrupt_raise_on(
  rtems_vector_number vector,
  uint32_t            cpu_index
);
#endif

/**
 * @brief Clears the interrupt vector.
 *
 * @param vector is the number of the interrupt vector to clear.  It shall be
 *   valid.
 *
 * @retval ::RTEMS_SUCCESSFUL The requested operation was successful.
 *
 * @retval ::RTEMS_UNSATISFIED The request to cause the interrupt vector has
 *   not been satisfied.
 */
rtems_status_code bsp_interrupt_clear( rtems_vector_number vector );

/**
 * @brief Sequentially calls all interrupt handlers installed at the vector.
 *
 * This function does not validate the vector number.  If the vector number is
 * out of range, then the behaviour is undefined.
 *
 * You can call this function within every context which can be disabled via
 * rtems_interrupt_local_disable().
 *
 * @param vector is the vector number.
 */
static inline void bsp_interrupt_handler_dispatch_unchecked(
  rtems_vector_number vector
)
{
  const rtems_interrupt_entry *e;

  e = &bsp_interrupt_handler_table[ bsp_interrupt_handler_index( vector ) ];

  do {
    rtems_interrupt_handler handler;
    void *arg;

    arg = e->arg;
    bsp_interrupt_fence( ATOMIC_ORDER_ACQUIRE );
    handler = e->handler;
    ( *handler )( arg );

    e = e->next;
  } while ( e != NULL );
}

/**
 * @brief Sequentially calls all interrupt handlers installed at the vector.
 *
 * If the vector number is out of range or the handler list is empty
 * bsp_interrupt_handler_default() will be called with the vector number as
 * argument.
 *
 * You can call this function within every context which can be disabled via
 * rtems_interrupt_local_disable().
 *
 * @param vector is the vector number.
 */
static inline void bsp_interrupt_handler_dispatch( rtems_vector_number vector )
{
  if ( bsp_interrupt_is_valid_vector( vector ) ) {
    bsp_interrupt_handler_dispatch_unchecked( vector );
  } else {
    bsp_interrupt_handler_default( vector );
  }
}

/**
 * @brief Is interrupt handler empty.
 *
 * This routine returns true if the handler is empty and has not been
 * initialised else false is returned. The interrupt lock is not used
 * so this call can be used from within interrupts.
 *
 * @return If empty true shall be returned else false is returned.
 */
bool bsp_interrupt_handler_is_empty(rtems_vector_number vector);

/** @} */

/* For internal use only */
void bsp_interrupt_lock(void);

/* For internal use only */
void bsp_interrupt_unlock(void);

/**
 * @brief This table contains a bit map which indicates if an entry is unique
 *   or shared.
 *
 * If the bit associated with a vector is set, then the entry is unique,
 * otherwise it may be shared.  If the bit with index
 * #BSP_INTERRUPT_HANDLER_TABLE_SIZE is set, then the interrupt support is
 * initialized, otherwise it is not initialized.
 */
extern uint8_t bsp_interrupt_handler_unique_table[];

/**
 * @brief Checks if the handler entry associated with the hander index is
 *   unique.
 *
 * @param index is the handler index to check.
 *
 * @return Returns true, if handler entry associated with the hander index is
 *   unique, otherwise false.
 */
static inline bool bsp_interrupt_is_handler_unique( rtems_vector_number index )
{
  rtems_vector_number table_index;
  uint8_t             bit;

  table_index = index / 8;
  bit = (uint8_t) ( 1U << ( index % 8 ) );

  return ( bsp_interrupt_handler_unique_table[ table_index ] & bit ) != 0;
}

/**
 * @brief Checks if the interrupt support is initialized.
 *
 * @return Returns true, if the interrupt support is initialized, otherwise
 *   false.
 */
static inline bool bsp_interrupt_is_initialized( void )
{
  return bsp_interrupt_is_handler_unique( BSP_INTERRUPT_HANDLER_TABLE_SIZE );
}

/**
 * @brief This handler routine is used for empty entries.
 */
void bsp_interrupt_handler_empty( void *arg );

/**
 * @brief Checks if a handler entry is empty.
 */
static inline bool bsp_interrupt_is_empty_handler_entry(
  const rtems_interrupt_entry *entry
)
{
  return entry->handler == bsp_interrupt_handler_empty;
}

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* LIBBSP_SHARED_IRQ_GENERIC_H */