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