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

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

Create a slab cache for allocating irq_t structures

  • Property mode set to 100644
File size: 7.8 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 respective device drivers
37 * and logic for dispatching interrupts to IRQ handlers defined by those
38 * drivers.
39 */
40
41#include <ddi/irq.h>
42#include <adt/hash_table.h>
43#include <mm/slab.h>
44#include <typedefs.h>
45#include <synch/spinlock.h>
46#include <console/console.h>
47#include <interrupt.h>
48#include <mem.h>
49#include <arch.h>
50
51slab_cache_t *irq_slab = NULL;
52
53/** Spinlock protecting the kernel IRQ hash table
54 *
55 * This lock must be taken only when interrupts are disabled.
56 *
57 */
58IRQ_SPINLOCK_STATIC_INITIALIZE(irq_kernel_hash_table_lock);
59
60/** The kernel IRQ hash table. */
61static hash_table_t irq_kernel_hash_table;
62
63/** Spinlock protecting the uspace IRQ hash table
64 *
65 * This lock must be taken only when interrupts are disabled.
66 *
67 */
68IRQ_SPINLOCK_INITIALIZE(irq_uspace_hash_table_lock);
69
70/** The uspace IRQ hash table */
71hash_table_t irq_uspace_hash_table;
72
73static size_t irq_ht_hash(sysarg_t *key);
74static bool irq_ht_compare(sysarg_t *key, size_t keys, link_t *item);
75static void irq_ht_remove(link_t *item);
76
77static hash_table_operations_t irq_ht_ops = {
78 .hash = irq_ht_hash,
79 .compare = irq_ht_compare,
80 .remove_callback = irq_ht_remove,
81};
82
83/** Number of buckets in either of the hash tables */
84static size_t buckets;
85
86/** Last valid INR */
87inr_t last_inr = 0;
88
89/** Initialize IRQ subsystem
90 *
91 * @param inrs Numbers of unique IRQ numbers or INRs.
92 * @param chains Number of buckets in the hash table.
93 *
94 */
95void irq_init(size_t inrs, size_t chains)
96{
97 buckets = chains;
98 last_inr = inrs - 1;
99
100 irq_slab = slab_cache_create("irq_t", sizeof(irq_t), 0, NULL, NULL,
101 FRAME_ATOMIC);
102 assert(irq_slab);
103
104 hash_table_create(&irq_uspace_hash_table, chains, 2, &irq_ht_ops);
105 hash_table_create(&irq_kernel_hash_table, chains, 2, &irq_ht_ops);
106}
107
108/** Initialize one IRQ structure
109 *
110 * @param irq Pointer to the IRQ structure to be initialized.
111 *
112 */
113void irq_initialize(irq_t *irq)
114{
115 memsetb(irq, sizeof(irq_t), 0);
116 link_initialize(&irq->link);
117 irq_spinlock_initialize(&irq->lock, "irq.lock");
118 irq->inr = -1;
119
120 irq_initialize_arch(irq);
121}
122
123/** Register IRQ for device
124 *
125 * The irq structure must be filled with information about the interrupt source
126 * and with the claim() function pointer and handler() function pointer.
127 *
128 * @param irq IRQ structure belonging to a device.
129 *
130 */
131void irq_register(irq_t *irq)
132{
133 sysarg_t key[] = {
134 [IRQ_HT_KEY_INR] = (sysarg_t) irq->inr,
135 [IRQ_HT_KEY_MODE] = (sysarg_t) IRQ_HT_MODE_NO_CLAIM
136 };
137
138 irq_spinlock_lock(&irq_kernel_hash_table_lock, true);
139 irq_spinlock_lock(&irq->lock, false);
140 hash_table_insert(&irq_kernel_hash_table, key, &irq->link);
141 irq_spinlock_unlock(&irq->lock, false);
142 irq_spinlock_unlock(&irq_kernel_hash_table_lock, true);
143}
144
145/** Search and lock the uspace IRQ hash table */
146static irq_t *irq_dispatch_and_lock_uspace(inr_t inr)
147{
148 link_t *lnk;
149 sysarg_t key[] = {
150 [IRQ_HT_KEY_INR] = (sysarg_t) inr,
151 [IRQ_HT_KEY_MODE] = (sysarg_t) IRQ_HT_MODE_CLAIM
152 };
153
154 irq_spinlock_lock(&irq_uspace_hash_table_lock, false);
155 lnk = hash_table_find(&irq_uspace_hash_table, key);
156 if (lnk) {
157 irq_t *irq = hash_table_get_instance(lnk, irq_t, link);
158 irq_spinlock_unlock(&irq_uspace_hash_table_lock, false);
159 return irq;
160 }
161 irq_spinlock_unlock(&irq_uspace_hash_table_lock, false);
162
163 return NULL;
164}
165
166/** Search and lock the kernel IRQ hash table */
167static irq_t *irq_dispatch_and_lock_kernel(inr_t inr)
168{
169 link_t *lnk;
170 sysarg_t key[] = {
171 [IRQ_HT_KEY_INR] = (sysarg_t) inr,
172 [IRQ_HT_KEY_MODE] = (sysarg_t) IRQ_HT_MODE_CLAIM
173 };
174
175 irq_spinlock_lock(&irq_kernel_hash_table_lock, false);
176 lnk = hash_table_find(&irq_kernel_hash_table, key);
177 if (lnk) {
178 irq_t *irq = hash_table_get_instance(lnk, irq_t, link);
179 irq_spinlock_unlock(&irq_kernel_hash_table_lock, false);
180 return irq;
181 }
182 irq_spinlock_unlock(&irq_kernel_hash_table_lock, false);
183
184 return NULL;
185}
186
187/** Dispatch the IRQ
188 *
189 * We assume this function is only called from interrupt context (i.e. that
190 * interrupts are disabled prior to this call).
191 *
192 * This function attempts to lookup a fitting IRQ structure. In case of success,
193 * return with interrupts disabled and holding the respective structure.
194 *
195 * @param inr Interrupt number (aka inr or irq).
196 *
197 * @return IRQ structure of the respective device
198 * @return NULL if no IRQ structure found
199 *
200 */
201irq_t *irq_dispatch_and_lock(inr_t inr)
202{
203 /*
204 * If the kernel console override is on, then try first the kernel
205 * handlers and eventually fall back to uspace handlers.
206 *
207 * In the usual case the uspace handlers have precedence.
208 */
209
210 if (console_override) {
211 irq_t *irq = irq_dispatch_and_lock_kernel(inr);
212 if (irq)
213 return irq;
214
215 return irq_dispatch_and_lock_uspace(inr);
216 }
217
218 irq_t *irq = irq_dispatch_and_lock_uspace(inr);
219 if (irq)
220 return irq;
221
222 return irq_dispatch_and_lock_kernel(inr);
223}
224
225/** Compute hash index for the key
226 *
227 * @param key The first of the keys is inr and the second is mode. Only inr is
228 * used to compute the hash.
229 *
230 * @return Index into the hash table.
231 *
232 */
233size_t irq_ht_hash(sysarg_t key[])
234{
235 inr_t inr = (inr_t) key[IRQ_HT_KEY_INR];
236 return inr % buckets;
237}
238
239/** Compare hash table element with a key
240 *
241 * If mode is IRQ_HT_MODE_CLAIM, the result of the claim() function is used for
242 * the match. Otherwise the key does not match.
243 *
244 * This function assumes interrupts are already disabled.
245 *
246 * @param key Keys (i.e. inr and mode).
247 * @param keys This is 2.
248 * @param item The item to compare the key with.
249 *
250 * @return True on match
251 * @return False on no match
252 *
253 */
254bool irq_ht_compare(sysarg_t key[], size_t keys, link_t *item)
255{
256 irq_t *irq = hash_table_get_instance(item, irq_t, link);
257 inr_t inr = (inr_t) key[IRQ_HT_KEY_INR];
258 irq_ht_mode_t mode = (irq_ht_mode_t) key[IRQ_HT_KEY_MODE];
259
260 bool rv;
261
262 irq_spinlock_lock(&irq->lock, false);
263 if (mode == IRQ_HT_MODE_CLAIM) {
264 /* Invoked by irq_dispatch_and_lock(). */
265 rv = ((irq->inr == inr) && (irq->claim(irq) == IRQ_ACCEPT));
266 } else {
267 /* Invoked by irq_find_and_lock(). */
268 rv = false;
269 }
270
271 /* unlock only on non-match */
272 if (!rv)
273 irq_spinlock_unlock(&irq->lock, false);
274
275 return rv;
276}
277
278/** Unlock IRQ structure after hash_table_remove()
279 *
280 * @param lnk Link in the removed and locked IRQ structure.
281 */
282void irq_ht_remove(link_t *lnk)
283{
284 irq_t *irq __attribute__((unused))
285 = hash_table_get_instance(lnk, irq_t, link);
286 irq_spinlock_unlock(&irq->lock, false);
287}
288
289/** @}
290 */
Note: See TracBrowser for help on using the repository browser.