source: mainline/kernel/generic/src/ddi/irq.c@ 78ffb70

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

ipc_irq_register() and ipc_irq_unregister() should check for out-of-limit INRs
passed from userspace.

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