source: rtems/c/src/lib/libbsp/shared/include/irq-generic.h @ d9bd59da

5
Last change on this file since d9bd59da was d9bd59da, checked in by Sebastian Huber <sebastian.huber@…>, on 11/03/15 at 10:44:11

bsp/irq-server: Support shared interrupts

  • Property mode set to 100644
File size: 9.5 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup bsp_interrupt
5 *
6 * @brief Generic BSP interrupt support API.
7 */
8
9/*
10 * Based on concepts of Pavel Pisa, Till Straumann and Eric Valette.
11 *
12 * Copyright (c) 2008-2014 embedded brains GmbH.
13 *
14 *  embedded brains GmbH
15 *  Dornierstr. 4
16 *  82178 Puchheim
17 *  Germany
18 *  <rtems@embedded-brains.de>
19 *
20 * The license and distribution terms for this file may be
21 * found in the file LICENSE in this distribution or at
22 * http://www.rtems.org/license/LICENSE.
23 */
24
25#ifndef LIBBSP_SHARED_IRQ_GENERIC_H
26#define LIBBSP_SHARED_IRQ_GENERIC_H
27
28#include <stdbool.h>
29
30#include <rtems/irq-extension.h>
31
32#ifdef RTEMS_SMP
33  #include <rtems/score/atomic.h>
34#endif
35
36#include <bsp/irq.h>
37
38#ifdef __cplusplus
39extern "C" {
40#endif /* __cplusplus */
41
42#if !defined(BSP_INTERRUPT_VECTOR_MIN) || !defined(BSP_INTERRUPT_VECTOR_MAX) || (BSP_INTERRUPT_VECTOR_MAX + 1) < BSP_INTERRUPT_VECTOR_MIN
43  #error "invalid BSP_INTERRUPT_VECTOR_MIN or BSP_INTERRUPT_VECTOR_MAX"
44#endif
45
46#if defined(BSP_INTERRUPT_USE_INDEX_TABLE) && !defined(BSP_INTERRUPT_HANDLER_TABLE_SIZE)
47  #error "if you define BSP_INTERRUPT_USE_INDEX_TABLE, you have to define BSP_INTERRUPT_HANDLER_TABLE_SIZE etc. as well"
48#endif
49
50#if defined(BSP_INTERRUPT_NO_HEAP_USAGE) && !defined(BSP_INTERRUPT_USE_INDEX_TABLE)
51  #error "if you define BSP_INTERRUPT_NO_HEAP_USAGE, you have to define BSP_INTERRUPT_USE_INDEX_TABLE etc. as well"
52#endif
53
54#define BSP_INTERRUPT_VECTOR_NUMBER \
55  (BSP_INTERRUPT_VECTOR_MAX - BSP_INTERRUPT_VECTOR_MIN + 1)
56
57#ifndef BSP_INTERRUPT_HANDLER_TABLE_SIZE
58  #define BSP_INTERRUPT_HANDLER_TABLE_SIZE BSP_INTERRUPT_VECTOR_NUMBER
59#endif
60
61/* Internal macros for SMP support, do not use externally */
62#ifdef RTEMS_SMP
63  #define bsp_interrupt_disable(level) do { (void) level; } while (0)
64  #define bsp_interrupt_enable(level) do { } while (0)
65  #define bsp_interrupt_fence(order) _Atomic_Fence(order)
66#else
67  #define bsp_interrupt_disable(level) rtems_interrupt_disable(level)
68  #define bsp_interrupt_enable(level) rtems_interrupt_enable(level)
69  #define bsp_interrupt_fence(order) do { } while (0)
70#endif
71
72struct bsp_interrupt_handler_entry {
73  rtems_interrupt_handler handler;
74  void *arg;
75  const char *info;
76  struct bsp_interrupt_handler_entry *next;
77};
78
79typedef struct bsp_interrupt_handler_entry bsp_interrupt_handler_entry;
80
81extern bsp_interrupt_handler_entry bsp_interrupt_handler_table [];
82
83#ifdef BSP_INTERRUPT_USE_INDEX_TABLE
84  #if BSP_INTERRUPT_HANDLER_TABLE_SIZE < 0x100
85    typedef uint8_t bsp_interrupt_handler_index_type;
86  #elif BSP_INTERRUPT_HANDLER_TABLE_SIZE < 0x10000
87    typedef uint16_t bsp_interrupt_handler_index_type;
88  #else
89    typedef uint32_t bsp_interrupt_handler_index_type;
90  #endif
91  extern bsp_interrupt_handler_index_type bsp_interrupt_handler_index_table [];
92#endif
93
94static inline rtems_vector_number bsp_interrupt_handler_index(
95  rtems_vector_number vector
96)
97{
98  #ifdef BSP_INTERRUPT_USE_INDEX_TABLE
99    return bsp_interrupt_handler_index_table [vector - BSP_INTERRUPT_VECTOR_MIN];
100  #else
101    return vector - BSP_INTERRUPT_VECTOR_MIN;
102  #endif
103}
104
105/**
106 * @defgroup bsp_interrupt BSP Interrupt Support
107 *
108 * @ingroup bsp_shared
109 *
110 * @brief Generic BSP Interrupt Support
111 *
112 * The BSP interrupt support manages a sequence of interrupt vector numbers
113 * ranging from @ref BSP_INTERRUPT_VECTOR_MIN to @ref BSP_INTERRUPT_VECTOR_MAX
114 * including the end points.  It provides methods to
115 * @ref bsp_interrupt_handler_install() "install",
116 * @ref bsp_interrupt_handler_remove() "remove" and
117 * @ref bsp_interrupt_handler_dispatch() "dispatch" interrupt handlers for each
118 * vector number.  It implements parts of the RTEMS interrupt manager.
119 *
120 * The entry points to a list of interrupt handlers are stored in a table
121 * (= handler table).
122 *
123 * You have to configure the BSP interrupt support in the <bsp/irq.h> file
124 * for each BSP.  For a minimum configuration you have to provide
125 * @ref BSP_INTERRUPT_VECTOR_MIN and @ref BSP_INTERRUPT_VECTOR_MAX.
126 *
127 * For boards with small memory requirements you can define
128 * @ref BSP_INTERRUPT_USE_INDEX_TABLE.  With an enabled index table the handler
129 * table will be accessed via a small index table.  You can define the size of
130 * the handler table with @ref BSP_INTERRUPT_HANDLER_TABLE_SIZE.
131 *
132 * Normally new list entries are allocated from the heap.  You may define
133 * @ref BSP_INTERRUPT_NO_HEAP_USAGE, if you do not want to use the heap.  For
134 * this option you have to define @ref BSP_INTERRUPT_USE_INDEX_TABLE as well.
135 *
136 * You have to provide some special routines in your BSP (follow the links for
137 * the details):
138 * - bsp_interrupt_facility_initialize()
139 * - bsp_interrupt_vector_enable()
140 * - bsp_interrupt_vector_disable()
141 * - bsp_interrupt_handler_default()
142 *
143 * The following now deprecated functions are provided for backward
144 * compatibility:
145 * - BSP_get_current_rtems_irq_handler()
146 * - BSP_install_rtems_irq_handler()
147 * - BSP_install_rtems_shared_irq_handler()
148 * - BSP_remove_rtems_irq_handler()
149 * - BSP_rtems_irq_mngt_set()
150 * - BSP_rtems_irq_mngt_get()
151 *
152 * @{
153 */
154
155#ifdef BSP_INTERRUPT_CUSTOM_VALID_VECTOR
156  bool bsp_interrupt_is_valid_vector(rtems_vector_number vector);
157#else
158  /**
159   * @brief Returns true if the interrupt vector with number @a vector is
160   * valid.
161   */
162  static inline bool bsp_interrupt_is_valid_vector(rtems_vector_number vector)
163  {
164    return (rtems_vector_number) BSP_INTERRUPT_VECTOR_MIN <= vector
165      && vector <= (rtems_vector_number) BSP_INTERRUPT_VECTOR_MAX;
166  }
167#endif
168
169/**
170 * @brief Default interrupt handler.
171 *
172 * This routine will be called from bsp_interrupt_handler_dispatch() with the
173 * current vector number @a vector when the handler list for this vector is
174 * empty or the vector number is out of range.
175 *
176 * @note This function must cope with arbitrary vector numbers @a vector.
177 */
178void bsp_interrupt_handler_default(rtems_vector_number vector);
179
180/**
181 * @brief Initialize BSP interrupt support.
182 *
183 * You must call this function before you can install, remove and dispatch
184 * interrupt handlers.  There is no protection against concurrent
185 * initialization.  This function must be called at most once.  The BSP
186 * specific bsp_interrupt_facility_initialize() function will be called after
187 * all internals are initialized.  If the BSP specific initialization fails,
188 * then this is a fatal error.  The fatal error source is
189 * RTEMS_FATAL_SOURCE_BSP and the fatal error code is
190 * BSP_FATAL_INTERRUPT_INITIALIZATION.
191 */
192void bsp_interrupt_initialize(void);
193
194/**
195 * @brief BSP specific initialization.
196 *
197 * This routine will be called form bsp_interrupt_initialize() and shall do the
198 * following:
199 * - Initialize the facilities that call bsp_interrupt_handler_dispatch().  For
200 * example on PowerPC the external exception handler.
201 * - Initialize the interrupt controller.  You shall set the interrupt
202 * controller in a state such that interrupts are disabled for all vectors.
203 * The vectors will be enabled with your bsp_interrupt_vector_enable() function
204 * and disabled via your bsp_interrupt_vector_disable() function.  These
205 * functions have to work afterwards.
206 *
207 * @return On success RTEMS_SUCCESSFUL shall be returned.
208 */
209rtems_status_code bsp_interrupt_facility_initialize(void);
210
211/**
212 * @brief Enables the interrupt vector with number @a vector.
213 *
214 * This function shall enable the vector at the corresponding facility (in most
215 * cases the interrupt controller).  It will be called then the first handler
216 * is installed for the vector in bsp_interrupt_handler_install().  It is
217 * guaranteed that the vector number is within the BSP_INTERRUPT_VECTOR_MIN and
218 * BSP_INTERRUPT_VECTOR_MAX range.
219 *
220 * @note You must not install or remove an interrupt handler in this function.
221 * This may result in a deadlock.
222 *
223 * @return On success RTEMS_SUCCESSFUL shall be returned.
224 */
225rtems_status_code bsp_interrupt_vector_enable(rtems_vector_number vector);
226
227/**
228 * @brief Disables the interrupt vector with number @a vector.
229 *
230 * This function shall disable the vector at the corresponding facility (in
231 * most cases the interrupt controller).  It will be called then the last
232 * handler is removed for the vector in bsp_interrupt_handler_remove().  It is
233 * guaranteed that the vector number is within the BSP_INTERRUPT_VECTOR_MIN and
234 * BSP_INTERRUPT_VECTOR_MAX range.
235 *
236 * @note You must not install or remove an interrupt handler in this function.
237 * This may result in a deadlock.
238 *
239 * @return On success RTEMS_SUCCESSFUL shall be returned.
240 */
241rtems_status_code bsp_interrupt_vector_disable(rtems_vector_number vector);
242
243/**
244 * @brief Sequencially calls all interrupt handlers for the vector number @a
245 * vector.
246 *
247 * If the vector number is out of range or the handler list is empty
248 * bsp_interrupt_handler_default() will be called with argument @a vector.
249 *
250 * You can call this function within every context which can be disabled via
251 * rtems_interrupt_disable().
252 */
253static inline void bsp_interrupt_handler_dispatch(rtems_vector_number vector)
254{
255  if (bsp_interrupt_is_valid_vector(vector)) {
256    const bsp_interrupt_handler_entry *e =
257      &bsp_interrupt_handler_table [bsp_interrupt_handler_index(vector)];
258
259    do {
260      rtems_interrupt_handler handler;
261      void *arg;
262
263      arg = e->arg;
264      bsp_interrupt_fence(ATOMIC_ORDER_ACQUIRE);
265      handler = e->handler;
266      (*handler)(arg);
267
268      e = e->next;
269    } while (e != NULL);
270  } else {
271    bsp_interrupt_handler_default(vector);
272  }
273}
274
275/** @} */
276
277/* For internal use only */
278void bsp_interrupt_lock(void);
279
280/* For internal use only */
281void bsp_interrupt_unlock(void);
282
283#ifdef __cplusplus
284}
285#endif /* __cplusplus */
286
287#endif /* LIBBSP_SHARED_IRQ_GENERIC_H */
Note: See TracBrowser for help on using the repository browser.