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

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

Improve cstyle

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