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
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 devices and logic
37 * for dispatching interrupts to IRQ handlers defined by those devices.
38 *
39 * This code is designed to support:
40 * - multiple devices sharing single IRQ
41 * - multiple IRQs per single device
42 * - multiple instances of the same device
43 *
44 *
45 * Note about architectures.
46 *
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.
51 *
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.
57 *
58 *
59 * Note about the irq_hash_table.
60 *
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.
65 */
66
67#include <ddi/irq.h>
68#include <adt/hash_table.h>
69#include <mm/slab.h>
70#include <typedefs.h>
71#include <synch/spinlock.h>
72#include <console/console.h>
73#include <interrupt.h>
74#include <mem.h>
75#include <arch.h>
76
77/** Spinlock protecting the kernel IRQ hash table.
78 *
79 * This lock must be taken only when interrupts are disabled.
80 *
81 */
82IRQ_SPINLOCK_STATIC_INITIALIZE(irq_kernel_hash_table_lock);
83
84/** The kernel IRQ hash table. */
85static hash_table_t irq_kernel_hash_table;
86
87/** Spinlock protecting the uspace IRQ hash table.
88 *
89 * This lock must be taken only when interrupts are disabled.
90 *
91 */
92IRQ_SPINLOCK_INITIALIZE(irq_uspace_hash_table_lock);
93
94/** The uspace IRQ hash table. */
95hash_table_t irq_uspace_hash_table;
96
97/**
98 * Hash table operations for cases when we know that there will be collisions
99 * between different keys.
100 */
101static size_t irq_ht_hash(sysarg_t *key);
102static bool irq_ht_compare(sysarg_t *key, size_t keys, link_t *item);
103static void irq_ht_remove(link_t *item);
104
105static hash_table_operations_t irq_ht_ops = {
106 .hash = irq_ht_hash,
107 .compare = irq_ht_compare,
108 .remove_callback = irq_ht_remove,
109};
110
111/**
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
114 * elements with single key (sharing of one IRQ).
115 */
116static size_t irq_lin_hash(sysarg_t *key);
117static bool irq_lin_compare(sysarg_t *key, size_t keys, link_t *item);
118static void irq_lin_remove(link_t *item);
119
120static hash_table_operations_t irq_lin_ops = {
121 .hash = irq_lin_hash,
122 .compare = irq_lin_compare,
123 .remove_callback = irq_lin_remove,
124};
125
126/** Number of buckets in either of the hash tables. */
127static size_t buckets;
128
129/** Last valid INR. */
130inr_t last_inr = 0;
131
132/** Initialize IRQ subsystem.
133 *
134 * @param inrs Numbers of unique IRQ numbers or INRs.
135 * @param chains Number of chains in the hash table.
136 *
137 */
138void irq_init(size_t inrs, size_t chains)
139{
140 buckets = chains;
141 last_inr = inrs - 1;
142
143 /*
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.
148 */
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 }
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{
169 memsetb(irq, sizeof(irq_t), 0);
170 link_initialize(&irq->link);
171 irq_spinlock_initialize(&irq->lock, "irq.lock");
172 link_initialize(&irq->notif_cfg.link);
173 irq->inr = -1;
174
175 irq_initialize_arch(irq);
176}
177
178/** Register IRQ for device.
179 *
180 * The irq structure must be filled with information about the interrupt source
181 * and with the claim() function pointer and handler() function pointer.
182 *
183 * @param irq IRQ structure belonging to a device.
184 *
185 */
186void irq_register(irq_t *irq)
187{
188 sysarg_t key[] = {
189 [IRQ_HT_KEY_INR] = (sysarg_t) irq->inr,
190 [IRQ_HT_KEY_MODE] = (sysarg_t) IRQ_HT_MODE_NO_CLAIM
191 };
192
193 irq_spinlock_lock(&irq_kernel_hash_table_lock, true);
194 irq_spinlock_lock(&irq->lock, false);
195 hash_table_insert(&irq_kernel_hash_table, key, &irq->link);
196 irq_spinlock_unlock(&irq->lock, false);
197 irq_spinlock_unlock(&irq_kernel_hash_table_lock, true);
198}
199
200/** Search and lock the uspace IRQ hash table.
201 *
202 */
203static irq_t *irq_dispatch_and_lock_uspace(inr_t inr)
204{
205 link_t *lnk;
206 sysarg_t key[] = {
207 [IRQ_HT_KEY_INR] = (sysarg_t) inr,
208 [IRQ_HT_KEY_MODE] = (sysarg_t) IRQ_HT_MODE_CLAIM
209 };
210
211 irq_spinlock_lock(&irq_uspace_hash_table_lock, false);
212 lnk = hash_table_find(&irq_uspace_hash_table, key);
213 if (lnk) {
214 irq_t *irq = hash_table_get_instance(lnk, irq_t, link);
215 irq_spinlock_unlock(&irq_uspace_hash_table_lock, false);
216 return irq;
217 }
218 irq_spinlock_unlock(&irq_uspace_hash_table_lock, false);
219
220 return NULL;
221}
222
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;
229 sysarg_t key[] = {
230 [IRQ_HT_KEY_INR] = (sysarg_t) inr,
231 [IRQ_HT_KEY_MODE] = (sysarg_t) IRQ_HT_MODE_CLAIM
232 };
233
234 irq_spinlock_lock(&irq_kernel_hash_table_lock, false);
235 lnk = hash_table_find(&irq_kernel_hash_table, key);
236 if (lnk) {
237 irq_t *irq = hash_table_get_instance(lnk, irq_t, link);
238 irq_spinlock_unlock(&irq_kernel_hash_table_lock, false);
239 return irq;
240 }
241 irq_spinlock_unlock(&irq_kernel_hash_table_lock, false);
242
243 return NULL;
244}
245
246/** Dispatch the IRQ.
247 *
248 * We assume this function is only called from interrupt context (i.e. that
249 * interrupts are disabled prior to this call).
250 *
251 * This function attempts to lookup a fitting IRQ structure. In case of success,
252 * return with interrupts disabled and holding the respective structure.
253 *
254 * @param inr Interrupt number (aka inr or irq).
255 *
256 * @return IRQ structure of the respective device
257 * @return NULL if no IRQ structure found
258 *
259 */
260irq_t *irq_dispatch_and_lock(inr_t inr)
261{
262 /*
263 * If the kernel console override is on, then try first the kernel
264 * handlers and eventually fall back to uspace handlers.
265 *
266 * In the usual case the uspace handlers have precedence.
267 */
268
269 if (console_override) {
270 irq_t *irq = irq_dispatch_and_lock_kernel(inr);
271 if (irq)
272 return irq;
273
274 return irq_dispatch_and_lock_uspace(inr);
275 }
276
277 irq_t *irq = irq_dispatch_and_lock_uspace(inr);
278 if (irq)
279 return irq;
280
281 return irq_dispatch_and_lock_kernel(inr);
282}
283
284/** Compute hash index for the key.
285 *
286 * This function computes hash index into the IRQ hash table for which there can
287 * be collisions between different INRs.
288 *
289 * The mode is not used to compute the hash.
290 *
291 * @param key The first of the keys is inr and the second is mode.
292 *
293 * @return Index into the hash table.
294 *
295 */
296size_t irq_ht_hash(sysarg_t key[])
297{
298 inr_t inr = (inr_t) key[IRQ_HT_KEY_INR];
299 return inr % buckets;
300}
301
302/** Compare hash table element with a key.
303 *
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
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.
310 *
311 * This function assumes interrupts are already disabled.
312 *
313 * @param key Keys (i.e. inr and mode).
314 * @param keys This is 2.
315 * @param item The item to compare the key with.
316 *
317 * @return true on match
318 * @return false on no match
319 *
320 */
321bool irq_ht_compare(sysarg_t key[], size_t keys, link_t *item)
322{
323 irq_t *irq = hash_table_get_instance(item, irq_t, link);
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];
326
327 bool rv;
328
329 irq_spinlock_lock(&irq->lock, false);
330 if (mode == IRQ_HT_MODE_CLAIM) {
331 /* Invoked by irq_dispatch_and_lock(). */
332 rv = ((irq->inr == inr) && (irq->claim(irq) == IRQ_ACCEPT));
333 } else {
334 /* Invoked by irq_find_and_lock(). */
335 rv = false;
336 }
337
338 /* unlock only on non-match */
339 if (!rv)
340 irq_spinlock_unlock(&irq->lock, false);
341
342 return rv;
343}
344
345/** Unlock IRQ structure after hash_table_remove().
346 *
347 * @param lnk Link in the removed and locked IRQ structure.
348 */
349void irq_ht_remove(link_t *lnk)
350{
351 irq_t *irq __attribute__((unused))
352 = hash_table_get_instance(lnk, irq_t, link);
353 irq_spinlock_unlock(&irq->lock, false);
354}
355
356/** Compute hash index for the key.
357 *
358 * This function computes hash index into the IRQ hash table for which there are
359 * no collisions between different INRs.
360 *
361 * @param key The first of the keys is inr and the second is mode.
362 *
363 * @return Index into the hash table.
364 *
365 */
366size_t irq_lin_hash(sysarg_t key[])
367{
368 inr_t inr = (inr_t) key[IRQ_HT_KEY_INR];
369 return inr;
370}
371
372/** Compare hash table element with a key.
373 *
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.
380 *
381 * This function assumes interrupts are already disabled.
382 *
383 * @param key Keys (i.e. inr and mode).
384 * @param keys This is 2.
385 * @param item The item to compare the key with.
386 *
387 * @return true on match
388 * @return false on no match
389 *
390 */
391bool irq_lin_compare(sysarg_t key[], size_t keys, link_t *item)
392{
393 irq_t *irq = list_get_instance(item, irq_t, link);
394 irq_ht_mode_t mode = (irq_ht_mode_t) key[IRQ_HT_KEY_MODE];
395 bool rv;
396
397 irq_spinlock_lock(&irq->lock, false);
398 if (mode == IRQ_HT_MODE_CLAIM) {
399 /* Invoked by irq_dispatch_and_lock() */
400 rv = (irq->claim(irq) == IRQ_ACCEPT);
401 } else {
402 /* Invoked by irq_find_and_lock() */
403 rv = false;
404 }
405
406 /* unlock only on non-match */
407 if (!rv)
408 irq_spinlock_unlock(&irq->lock, false);
409
410 return rv;
411}
412
413/** Unlock IRQ structure after hash_table_remove().
414 *
415 * @param lnk Link in the removed and locked IRQ structure.
416 *
417 */
418void irq_lin_remove(link_t *lnk)
419{
420 irq_t *irq __attribute__((unused))
421 = hash_table_get_instance(lnk, irq_t, link);
422 irq_spinlock_unlock(&irq->lock, false);
423}
424
425/** @}
426 */
Note: See TracBrowser for help on using the repository browser.