source: mainline/kernel/generic/src/ddi/irq.c@ 2b017ba

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2b017ba was 2b017ba, checked in by Jakub Jermar <jakub@…>, 19 years ago

Replace the old IRQ dispatcher and IPC notifier with new implementation.
Note that all architectures except for sparc64 are now broken
and don't even compile.

  • Property mode set to 100644
File size: 10.5 KB
Line 
1/*
2 * Copyright (C) 2006 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup genericddi
30 * @{
31 */
32/**
33 * @file
34 * @brief IRQ dispatcher.
35 *
36 * This file provides means of connecting IRQs with particular
37 * devices and logic for dispatching interrupts to IRQ handlers
38 * defined by those devices.
39 *
40 * This code is designed to support:
41 * - multiple devices sharing single IRQ
42 * - multiple IRQs per signle device
43 *
44 *
45 * Note about architectures.
46 *
47 * Some architectures has the term IRQ well defined. Examples
48 * of such architectures include amd64, ia32 and mips32. Some
49 * other architectures, such as sparc64, don't use the term
50 * at all. In those cases, we boldly step forward and define what
51 * an IRQ is.
52 *
53 * The implementation is generic enough and still allows the
54 * architectures to use the hardware layout effectively.
55 * For instance, on amd64 and ia32, where there is only 16
56 * IRQs, the irq_hash_table can be optimized to a one-dimensional
57 * array. Next, when it is known that the IRQ numbers (aka INR's)
58 * are unique, the claim functions can always return IRQ_ACCEPT.
59 *
60 *
61 * Note about the irq_hash_table.
62 *
63 * The hash table is configured to use two keys: inr and devno.
64 * However, the hash index is computed only from inr. Moreover,
65 * if devno is -1, the match is based on the return value of
66 * the claim() function instead of on devno.
67 */
68
69#include <ddi/irq.h>
70#include <adt/hash_table.h>
71#include <arch/types.h>
72#include <typedefs.h>
73#include <synch/spinlock.h>
74#include <arch.h>
75
76#define KEY_INR 0
77#define KEY_DEVNO 1
78
79/**
80 * Spinlock protecting the hash table.
81 * This lock must be taken only when interrupts are disabled.
82 */
83SPINLOCK_INITIALIZE(irq_hash_table_lock);
84static hash_table_t irq_hash_table;
85
86/**
87 * Hash table operations for cases when we know that
88 * there will be collisions between different keys.
89 */
90static index_t irq_ht_hash(unative_t *key);
91static bool irq_ht_compare(unative_t *key, count_t keys, link_t *item);
92
93static hash_table_operations_t irq_ht_ops = {
94 .hash = irq_ht_hash,
95 .compare = irq_ht_compare,
96 .remove_callback = NULL /* not used */
97};
98
99/**
100 * Hash table operations for cases when we know that
101 * there will be no collisions between different keys.
102 * However, there might be still collisions among
103 * elements with single key (sharing of one IRQ).
104 */
105static index_t irq_lin_hash(unative_t *key);
106static bool irq_lin_compare(unative_t *key, count_t keys, link_t *item);
107
108static hash_table_operations_t irq_lin_ops = {
109 .hash = irq_lin_hash,
110 .compare = irq_lin_compare,
111 .remove_callback = NULL /* not used */
112};
113
114/** Initialize IRQ subsystem.
115 *
116 * @param inrs Numbers of unique IRQ numbers or INRs.
117 * @param chains Number of chains in the hash table.
118 */
119void irq_init(count_t inrs, count_t chains)
120{
121 /*
122 * Be smart about the choice of the hash table operations.
123 * In cases in which inrs equals the requested number of
124 * chains (i.e. where there is no collision between
125 * different keys), we can use optimized set of operations.
126 */
127 if (inrs == chains)
128 hash_table_create(&irq_hash_table, chains, 2, &irq_lin_ops);
129 else
130 hash_table_create(&irq_hash_table, chains, 2, &irq_ht_ops);
131}
132
133/** Initialize one IRQ structure.
134 *
135 * @param irq Pointer to the IRQ structure to be initialized.
136 *
137 */
138void irq_initialize(irq_t *irq)
139{
140 link_initialize(&irq->link);
141 spinlock_initialize(&irq->lock, "irq.lock");
142 irq->inr = -1;
143 irq->devno = -1;
144 irq->trigger = 0;
145 irq->claim = NULL;
146 irq->handler = NULL;
147 irq->arg = NULL;
148 irq->notif_cfg.answerbox = NULL;
149 irq->notif_cfg.code = NULL;
150 irq->notif_cfg.method = 0;
151 irq->notif_cfg.counter = 0;
152}
153
154/** Register IRQ for device.
155 *
156 * The irq structure must be filled with information
157 * about the interrupt source and with the claim()
158 * function pointer and irq_handler() function pointer.
159 *
160 * @param irq IRQ structure belonging to a device.
161 */
162void irq_register(irq_t *irq)
163{
164 ipl_t ipl;
165 unative_t key[] = {
166 (unative_t) irq->inr,
167 (unative_t) irq->devno
168 };
169
170 ipl = interrupts_disable();
171 spinlock_lock(&irq_hash_table_lock);
172 hash_table_insert(&irq_hash_table, key, &irq->link);
173 spinlock_unlock(&irq_hash_table_lock);
174 interrupts_restore(ipl);
175}
176
177/** Dispatch the IRQ.
178 *
179 * We assume this function is only called from interrupt
180 * context (i.e. that interrupts are disabled prior to
181 * this call).
182 *
183 * This function attempts to lookup a fitting IRQ
184 * structure. In case of success, return with interrupts
185 * disabled and holding the respective structure.
186 *
187 * @param inr Interrupt number (aka inr or irq).
188 *
189 * @return IRQ structure of the respective device or NULL.
190 */
191irq_t *irq_dispatch_and_lock(inr_t inr)
192{
193 link_t *lnk;
194 unative_t key[] = {
195 (unative_t) inr,
196 (unative_t) -1 /* search will use claim() instead of devno */
197 };
198
199 spinlock_lock(&irq_hash_table_lock);
200
201 lnk = hash_table_find(&irq_hash_table, key);
202 if (lnk) {
203 irq_t *irq;
204
205 irq = hash_table_get_instance(lnk, irq_t, link);
206
207 spinlock_unlock(&irq_hash_table_lock);
208 return irq;
209 }
210
211 spinlock_unlock(&irq_hash_table_lock);
212
213 return NULL;
214}
215
216/** Find the IRQ structure corresponding to inr and devno.
217 *
218 * This functions attempts to lookup the IRQ structure
219 * corresponding to its arguments. On success, this
220 * function returns with interrups disabled, holding
221 * the lock of the respective IRQ structure.
222 *
223 * This function assumes interrupts are already disabled.
224 *
225 * @param inr INR being looked up.
226 * @param devno Devno being looked up.
227 *
228 * @return Locked IRQ structure on success or NULL on failure.
229 */
230irq_t *irq_find_and_lock(inr_t inr, devno_t devno)
231{
232 link_t *lnk;
233 unative_t keys[] = {
234 (unative_t) inr,
235 (unative_t) devno
236 };
237
238 spinlock_lock(&irq_hash_table_lock);
239
240 lnk = hash_table_find(&irq_hash_table, keys);
241 if (lnk) {
242 irq_t *irq;
243
244 irq = hash_table_get_instance(lnk, irq_t, link);
245
246 spinlock_unlock(&irq_hash_table_lock);
247 return irq;
248 }
249
250 spinlock_unlock(&irq_hash_table_lock);
251
252 return NULL;
253}
254
255/** Compute hash index for the key.
256 *
257 * This function computes hash index into
258 * the IRQ hash table for which there
259 * can be collisions between different
260 * INRs.
261 *
262 * The devno is not used to compute the hash.
263 *
264 * @param key The first of the keys is inr and the second is devno or -1.
265 *
266 * @return Index into the hash table.
267 */
268index_t irq_ht_hash(unative_t key[])
269{
270 inr_t inr = (inr_t) key[KEY_INR];
271 return inr % irq_hash_table.entries;
272}
273
274/** Compare hash table element with a key.
275 *
276 * There are two things to note about this function.
277 * First, it is used for the more complex architecture setup
278 * in which there are way too many interrupt numbers (i.e. inr's)
279 * to arrange the hash table so that collisions occur only
280 * among same inrs of different devnos. So the explicit check
281 * for inr match must be done.
282 * Second, if devno is -1, the second key (i.e. devno) is not
283 * used for the match and the result of the claim() function
284 * is used instead.
285 *
286 * This function assumes interrupts are already disabled.
287 *
288 * @param key Keys (i.e. inr and devno).
289 * @param keys This is 2.
290 * @param item The item to compare the key with.
291 *
292 * @return True on match or false otherwise.
293 */
294bool irq_ht_compare(unative_t key[], count_t keys, link_t *item)
295{
296 irq_t *irq = hash_table_get_instance(item, irq_t, link);
297 inr_t inr = (inr_t) key[KEY_INR];
298 devno_t devno = (devno_t) key[KEY_DEVNO];
299
300 bool rv;
301
302 spinlock_lock(&irq->lock);
303 if (devno == -1) {
304 /* Invoked by irq_dispatch(). */
305 rv = ((irq->inr == inr) && (irq->claim() == IRQ_ACCEPT));
306 } else {
307 /* Invoked by irq_find(). */
308 rv = ((irq->inr == inr) && (irq->devno == devno));
309 }
310
311 /* unlock only on non-match */
312 if (!rv)
313 spinlock_unlock(&irq->lock);
314
315 return rv;
316}
317
318/** Compute hash index for the key.
319 *
320 * This function computes hash index into
321 * the IRQ hash table for which there
322 * are no collisions between different
323 * INRs.
324 *
325 * @param key The first of the keys is inr and the second is devno or -1.
326 *
327 * @return Index into the hash table.
328 */
329index_t irq_lin_hash(unative_t key[])
330{
331 inr_t inr = (inr_t) key[KEY_INR];
332 return inr;
333}
334
335/** Compare hash table element with a key.
336 *
337 * There are two things to note about this function.
338 * First, it is used for the less complex architecture setup
339 * in which there are not too many interrupt numbers (i.e. inr's)
340 * to arrange the hash table so that collisions occur only
341 * among same inrs of different devnos. So the explicit check
342 * for inr match is not done.
343 * Second, if devno is -1, the second key (i.e. devno) is not
344 * used for the match and the result of the claim() function
345 * is used instead.
346 *
347 * This function assumes interrupts are already disabled.
348 *
349 * @param key Keys (i.e. inr and devno).
350 * @param keys This is 2.
351 * @param item The item to compare the key with.
352 *
353 * @return True on match or false otherwise.
354 */
355bool irq_lin_compare(unative_t key[], count_t keys, link_t *item)
356{
357 irq_t *irq = list_get_instance(item, irq_t, link);
358 devno_t devno = (devno_t) key[KEY_DEVNO];
359 bool rv;
360
361 spinlock_lock(&irq->lock);
362 if (devno == -1) {
363 /* Invoked by irq_dispatch() */
364 rv = (irq->claim() == IRQ_ACCEPT);
365 } else {
366 /* Invoked by irq_find() */
367 rv = (irq->devno == devno);
368 }
369
370 /* unlock only on non-match */
371 if (!rv)
372 spinlock_unlock(&irq->lock);
373
374 return rv;
375}
376
377/** @}
378 */
Note: See TracBrowser for help on using the repository browser.