source: mainline/kernel/generic/src/ddi/irq.c@ 24abb85d

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

Remove SYS_DEVICE_ASSIGN_DEVNO

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