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

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

Remove the linear IRQ hash table optimization

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