source: mainline/kernel/generic/src/ddi/irq.c@ 96b02eb9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 96b02eb9 was 96b02eb9, checked in by Martin Decky <martin@…>, 15 years ago

more unification of basic types

  • use sysarg_t and native_t (unsigned and signed variant) in both kernel and uspace
  • remove ipcarg_t in favour of sysarg_t

(no change in functionality)

  • Property mode set to 100644
File size: 12.2 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 single device
43 * - multiple instances of the same device
44 *
45 *
46 * Note about architectures.
47 *
48 * Some architectures has the term IRQ well defined. Examples
49 * of such architectures include amd64, ia32 and mips32. Some
50 * other architectures, such as sparc64, don't use the term
51 * at all. In those cases, we boldly step forward and define what
52 * an IRQ is.
53 *
54 * The implementation is generic enough and still allows the
55 * architectures to use the hardware layout effectively.
56 * For instance, on amd64 and ia32, where there is only 16
57 * IRQs, the irq_hash_table can be optimized to a one-dimensional
58 * array. Next, when it is known that the IRQ numbers (aka INR's)
59 * are unique, the claim functions can always return IRQ_ACCEPT.
60 *
61 *
62 * Note about the irq_hash_table.
63 *
64 * The hash table is configured to use two keys: inr and devno.
65 * However, the hash index is computed only from inr. Moreover,
66 * if devno is -1, the match is based on the return value of
67 * the claim() function instead of on devno.
68 */
69
70#include <ddi/irq.h>
71#include <adt/hash_table.h>
72#include <mm/slab.h>
73#include <typedefs.h>
74#include <synch/spinlock.h>
75#include <console/console.h>
76#include <interrupt.h>
77#include <memstr.h>
78#include <arch.h>
79
80#define KEY_INR 0
81#define KEY_DEVNO 1
82
83/** Spinlock protecting the kernel IRQ hash table.
84 *
85 * This lock must be taken only when interrupts are disabled.
86 *
87 */
88IRQ_SPINLOCK_STATIC_INITIALIZE(irq_kernel_hash_table_lock);
89
90/** The kernel IRQ hash table. */
91static hash_table_t irq_kernel_hash_table;
92
93/** Spinlock protecting the uspace IRQ hash table.
94 *
95 * This lock must be taken only when interrupts are disabled.
96 *
97 */
98IRQ_SPINLOCK_INITIALIZE(irq_uspace_hash_table_lock);
99
100/** The uspace IRQ hash table. */
101hash_table_t irq_uspace_hash_table;
102
103/**
104 * Hash table operations for cases when we know that
105 * there will be collisions between different keys.
106 *
107 */
108static size_t irq_ht_hash(sysarg_t *key);
109static bool irq_ht_compare(sysarg_t *key, size_t keys, link_t *item);
110static void irq_ht_remove(link_t *item);
111
112static hash_table_operations_t irq_ht_ops = {
113 .hash = irq_ht_hash,
114 .compare = irq_ht_compare,
115 .remove_callback = irq_ht_remove,
116};
117
118/**
119 * Hash table operations for cases when we know that
120 * there will be no collisions between different keys.
121 * However, there might be still collisions among
122 * elements with single key (sharing of one IRQ).
123 *
124 */
125static size_t irq_lin_hash(sysarg_t *key);
126static bool irq_lin_compare(sysarg_t *key, size_t keys, link_t *item);
127static void irq_lin_remove(link_t *item);
128
129static hash_table_operations_t irq_lin_ops = {
130 .hash = irq_lin_hash,
131 .compare = irq_lin_compare,
132 .remove_callback = irq_lin_remove,
133};
134
135/** Number of buckets in either of the hash tables. */
136static size_t buckets;
137
138/** Initialize IRQ subsystem.
139 *
140 * @param inrs Numbers of unique IRQ numbers or INRs.
141 * @param chains Number of chains in the hash table.
142 *
143 */
144void irq_init(size_t inrs, size_t chains)
145{
146 buckets = chains;
147 /*
148 * Be smart about the choice of the hash table operations.
149 * In cases in which inrs equals the requested number of
150 * chains (i.e. where there is no collision between
151 * different keys), we can use optimized set of operations.
152 */
153 if (inrs == chains) {
154 hash_table_create(&irq_uspace_hash_table, chains, 2,
155 &irq_lin_ops);
156 hash_table_create(&irq_kernel_hash_table, chains, 2,
157 &irq_lin_ops);
158 } else {
159 hash_table_create(&irq_uspace_hash_table, chains, 2,
160 &irq_ht_ops);
161 hash_table_create(&irq_kernel_hash_table, chains, 2,
162 &irq_ht_ops);
163 }
164}
165
166/** Initialize one IRQ structure.
167 *
168 * @param irq Pointer to the IRQ structure to be initialized.
169 *
170 */
171void irq_initialize(irq_t *irq)
172{
173 memsetb(irq, sizeof(irq_t), 0);
174 link_initialize(&irq->link);
175 irq_spinlock_initialize(&irq->lock, "irq.lock");
176 link_initialize(&irq->notif_cfg.link);
177 irq->inr = -1;
178 irq->devno = -1;
179
180 irq_initialize_arch(irq);
181}
182
183/** Register IRQ for device.
184 *
185 * The irq structure must be filled with information
186 * about the interrupt source and with the claim()
187 * function pointer and handler() function pointer.
188 *
189 * @param irq IRQ structure belonging to a device.
190 *
191 * @return True on success, false on failure.
192 *
193 */
194void irq_register(irq_t *irq)
195{
196 sysarg_t key[] = {
197 (sysarg_t) irq->inr,
198 (sysarg_t) irq->devno
199 };
200
201 irq_spinlock_lock(&irq_kernel_hash_table_lock, true);
202 irq_spinlock_lock(&irq->lock, false);
203 hash_table_insert(&irq_kernel_hash_table, key, &irq->link);
204 irq_spinlock_unlock(&irq->lock, false);
205 irq_spinlock_unlock(&irq_kernel_hash_table_lock, true);
206}
207
208/** Search and lock the uspace IRQ hash table.
209 *
210 */
211static irq_t *irq_dispatch_and_lock_uspace(inr_t inr)
212{
213 link_t *lnk;
214 sysarg_t key[] = {
215 (sysarg_t) inr,
216 (sysarg_t) -1 /* Search will use claim() instead of devno */
217 };
218
219 irq_spinlock_lock(&irq_uspace_hash_table_lock, false);
220 lnk = hash_table_find(&irq_uspace_hash_table, key);
221 if (lnk) {
222 irq_t *irq = hash_table_get_instance(lnk, irq_t, link);
223 irq_spinlock_unlock(&irq_uspace_hash_table_lock, false);
224 return irq;
225 }
226 irq_spinlock_unlock(&irq_uspace_hash_table_lock, false);
227
228 return NULL;
229}
230
231/** Search and lock the kernel IRQ hash table.
232 *
233 */
234static irq_t *irq_dispatch_and_lock_kernel(inr_t inr)
235{
236 link_t *lnk;
237 sysarg_t key[] = {
238 (sysarg_t) inr,
239 (sysarg_t) -1 /* Search will use claim() instead of devno */
240 };
241
242 irq_spinlock_lock(&irq_kernel_hash_table_lock, false);
243 lnk = hash_table_find(&irq_kernel_hash_table, key);
244 if (lnk) {
245 irq_t *irq = hash_table_get_instance(lnk, irq_t, link);
246 irq_spinlock_unlock(&irq_kernel_hash_table_lock, false);
247 return irq;
248 }
249 irq_spinlock_unlock(&irq_kernel_hash_table_lock, false);
250
251 return NULL;
252}
253
254/** Dispatch the IRQ.
255 *
256 * We assume this function is only called from interrupt
257 * context (i.e. that interrupts are disabled prior to
258 * this call).
259 *
260 * This function attempts to lookup a fitting IRQ
261 * structure. In case of success, return with interrupts
262 * disabled and holding the respective structure.
263 *
264 * @param inr Interrupt number (aka inr or irq).
265 *
266 * @return IRQ structure of the respective device or NULL.
267 *
268 */
269irq_t *irq_dispatch_and_lock(inr_t inr)
270{
271 /*
272 * If the kernel console is silenced,
273 * then try first the uspace handlers,
274 * eventually fall back to kernel handlers.
275 *
276 * If the kernel console is active,
277 * then do it the other way around.
278 */
279 if (silent) {
280 irq_t *irq = irq_dispatch_and_lock_uspace(inr);
281 if (irq)
282 return irq;
283
284 return irq_dispatch_and_lock_kernel(inr);
285 }
286
287 irq_t *irq = irq_dispatch_and_lock_kernel(inr);
288 if (irq)
289 return irq;
290
291 return irq_dispatch_and_lock_uspace(inr);
292}
293
294/** Compute hash index for the key.
295 *
296 * This function computes hash index into
297 * the IRQ hash table for which there
298 * can be collisions between different
299 * INRs.
300 *
301 * The devno is not used to compute the hash.
302 *
303 * @param key The first of the keys is inr and the second is devno or -1.
304 *
305 * @return Index into the hash table.
306 *
307 */
308size_t irq_ht_hash(sysarg_t key[])
309{
310 inr_t inr = (inr_t) key[KEY_INR];
311 return inr % buckets;
312}
313
314/** Compare hash table element with a key.
315 *
316 * There are two things to note about this function.
317 * First, it is used for the more complex architecture setup
318 * in which there are way too many interrupt numbers (i.e. inr's)
319 * to arrange the hash table so that collisions occur only
320 * among same inrs of different devnos. So the explicit check
321 * for inr match must be done.
322 * Second, if devno is -1, the second key (i.e. devno) is not
323 * used for the match and the result of the claim() function
324 * is used instead.
325 *
326 * This function assumes interrupts are already disabled.
327 *
328 * @param key Keys (i.e. inr and devno).
329 * @param keys This is 2.
330 * @param item The item to compare the key with.
331 *
332 * @return True on match or false otherwise.
333 *
334 */
335bool irq_ht_compare(sysarg_t key[], size_t keys, link_t *item)
336{
337 irq_t *irq = hash_table_get_instance(item, irq_t, link);
338 inr_t inr = (inr_t) key[KEY_INR];
339 devno_t devno = (devno_t) key[KEY_DEVNO];
340
341 bool rv;
342
343 irq_spinlock_lock(&irq->lock, false);
344 if (devno == -1) {
345 /* Invoked by irq_dispatch_and_lock(). */
346 rv = ((irq->inr == inr) &&
347 (irq->claim(irq) == IRQ_ACCEPT));
348 } else {
349 /* Invoked by irq_find_and_lock(). */
350 rv = ((irq->inr == inr) && (irq->devno == devno));
351 }
352
353 /* unlock only on non-match */
354 if (!rv)
355 irq_spinlock_unlock(&irq->lock, false);
356
357 return rv;
358}
359
360/** Unlock IRQ structure after hash_table_remove().
361 *
362 * @param lnk Link in the removed and locked IRQ structure.
363 */
364void irq_ht_remove(link_t *lnk)
365{
366 irq_t *irq __attribute__((unused))
367 = hash_table_get_instance(lnk, irq_t, link);
368 irq_spinlock_unlock(&irq->lock, false);
369}
370
371/** Compute hash index for the key.
372 *
373 * This function computes hash index into
374 * the IRQ hash table for which there
375 * are no collisions between different
376 * INRs.
377 *
378 * @param key The first of the keys is inr and the second is devno or -1.
379 *
380 * @return Index into the hash table.
381 *
382 */
383size_t irq_lin_hash(sysarg_t key[])
384{
385 inr_t inr = (inr_t) key[KEY_INR];
386 return inr;
387}
388
389/** Compare hash table element with a key.
390 *
391 * There are two things to note about this function.
392 * First, it is used for the less complex architecture setup
393 * in which there are not too many interrupt numbers (i.e. inr's)
394 * to arrange the hash table so that collisions occur only
395 * among same inrs of different devnos. So the explicit check
396 * for inr match is not done.
397 * Second, if devno is -1, the second key (i.e. devno) is not
398 * used for the match and the result of the claim() function
399 * is used instead.
400 *
401 * This function assumes interrupts are already disabled.
402 *
403 * @param key Keys (i.e. inr and devno).
404 * @param keys This is 2.
405 * @param item The item to compare the key with.
406 *
407 * @return True on match or false otherwise.
408 *
409 */
410bool irq_lin_compare(sysarg_t key[], size_t keys, link_t *item)
411{
412 irq_t *irq = list_get_instance(item, irq_t, link);
413 devno_t devno = (devno_t) key[KEY_DEVNO];
414 bool rv;
415
416 irq_spinlock_lock(&irq->lock, false);
417 if (devno == -1) {
418 /* Invoked by irq_dispatch_and_lock() */
419 rv = (irq->claim(irq) == IRQ_ACCEPT);
420 } else {
421 /* Invoked by irq_find_and_lock() */
422 rv = (irq->devno == devno);
423 }
424
425 /* unlock only on non-match */
426 if (!rv)
427 irq_spinlock_unlock(&irq->lock, false);
428
429 return rv;
430}
431
432/** Unlock IRQ structure after hash_table_remove().
433 *
434 * @param lnk Link in the removed and locked IRQ structure.
435 *
436 */
437void irq_lin_remove(link_t *lnk)
438{
439 irq_t *irq __attribute__((unused))
440 = hash_table_get_instance(lnk, irq_t, link);
441 irq_spinlock_unlock(&irq->lock, false);
442}
443
444/** @}
445 */
Note: See TracBrowser for help on using the repository browser.