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

Last change on this file since 29e7cc7 was 0db0df2, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 months ago

Hash table improvements

Implement hash_table_foreach macro, analogous to list_foreach.

Remove superfluous argument to hash_table_find_next().
(If the user needs to recheck the part of the list already
checked by hash_table_find(), they can just rerun that function.)

Add hash argument to hash_table_ops_t::key_equal.
The big change here is that users with big keys can store the hash
value alongside key in their entries, and for the low low cost of
sizeof(size_t) bytes eliminate a bunch of expensive key comparisons.

Also added a hash function for strings and arbitrary data.
Found this one by asking ChatGPT, because the latency of accesses
to my book collection is currently a couple of hours.

+ Some drive-by unused #include removal.

  • Property mode set to 100644
File size: 6.5 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
[174156fd]29/** @addtogroup kernel_generic_ddi
[0d107f31]30 * @{
31 */
32/**
33 * @file
[4d76cfc]34 * @brief IRQ dispatcher
[0d107f31]35 *
[4d76cfc]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.
[0d107f31]39 */
40
[7dcf22a]41#include <ddi/irq.h>
[82cbf8c6]42#include <adt/hash.h>
[0d107f31]43#include <adt/hash_table.h>
[cecb0789]44#include <mm/slab.h>
[d99c1d2]45#include <typedefs.h>
[0d107f31]46#include <synch/spinlock.h>
[691eb52]47#include <console/console.h>
[3a2f8aa]48#include <interrupt.h>
[b169619]49#include <memw.h>
[0d107f31]50#include <arch.h>
51
[82d515e9]52slab_cache_t *irq_cache = NULL;
[431c402]53
[4d76cfc]54/** Spinlock protecting the kernel IRQ hash table
[da1bafb]55 *
[0d107f31]56 * This lock must be taken only when interrupts are disabled.
[da1bafb]57 *
[0d107f31]58 */
[da1bafb]59IRQ_SPINLOCK_STATIC_INITIALIZE(irq_kernel_hash_table_lock);
60
[cecb0789]61/** The kernel IRQ hash table. */
62static hash_table_t irq_kernel_hash_table;
63
[4d76cfc]64/** Spinlock protecting the uspace IRQ hash table
[da1bafb]65 *
[cecb0789]66 * This lock must be taken only when interrupts are disabled.
[da1bafb]67 *
[cecb0789]68 */
[da1bafb]69IRQ_SPINLOCK_INITIALIZE(irq_uspace_hash_table_lock);
70
[4d76cfc]71/** The uspace IRQ hash table */
[cecb0789]72hash_table_t irq_uspace_hash_table;
[0d107f31]73
[82cbf8c6]74static size_t irq_ht_hash(const ht_link_t *);
[5e801dc]75static size_t irq_ht_key_hash(const void *);
[82cbf8c6]76static bool irq_ht_equal(const ht_link_t *, const ht_link_t *);
[0db0df2]77static bool irq_ht_key_equal(const void *, size_t, const ht_link_t *);
[0d107f31]78
[61eb2ce2]79static const hash_table_ops_t irq_ht_ops = {
[0d107f31]80 .hash = irq_ht_hash,
[82cbf8c6]81 .key_hash = irq_ht_key_hash,
82 .equal = irq_ht_equal,
83 .key_equal = irq_ht_key_equal
[0d107f31]84};
85
[4d76cfc]86/** Last valid INR */
[78ffb70]87inr_t last_inr = 0;
88
[4d76cfc]89/** Initialize IRQ subsystem
[0d107f31]90 *
[4d76cfc]91 * @param inrs Numbers of unique IRQ numbers or INRs.
92 * @param chains Number of buckets in the hash table.
[da1bafb]93 *
[0d107f31]94 */
[98000fb]95void irq_init(size_t inrs, size_t chains)
[0d107f31]96{
[78ffb70]97 last_inr = inrs - 1;
98
[82d515e9]99 irq_cache = slab_cache_create("irq_t", sizeof(irq_t), 0, NULL, NULL,
[431c402]100 FRAME_ATOMIC);
[82d515e9]101 assert(irq_cache);
[431c402]102
[82cbf8c6]103 hash_table_create(&irq_uspace_hash_table, chains, 0, &irq_ht_ops);
104 hash_table_create(&irq_kernel_hash_table, chains, 0, &irq_ht_ops);
[0d107f31]105}
106
[4d76cfc]107/** Initialize one IRQ structure
[0d107f31]108 *
[4d76cfc]109 * @param irq Pointer to the IRQ structure to be initialized.
[0d107f31]110 *
111 */
112void irq_initialize(irq_t *irq)
113{
[f542825]114 memsetb(irq, sizeof(irq_t), 0);
[da1bafb]115 irq_spinlock_initialize(&irq->lock, "irq.lock");
[0d107f31]116 irq->inr = -1;
[a35b458]117
[3a2f8aa]118 irq_initialize_arch(irq);
[0d107f31]119}
120
[4d76cfc]121/** Register IRQ for device
[0d107f31]122 *
[ee50130]123 * The irq structure must be filled with information about the interrupt source
124 * and with the claim() function pointer and handler() function pointer.
[0d107f31]125 *
[4d76cfc]126 * @param irq IRQ structure belonging to a device.
[da1bafb]127 *
[0d107f31]128 */
129void irq_register(irq_t *irq)
130{
[da1bafb]131 irq_spinlock_lock(&irq_kernel_hash_table_lock, true);
132 irq_spinlock_lock(&irq->lock, false);
[82cbf8c6]133 hash_table_insert(&irq_kernel_hash_table, &irq->link);
[da1bafb]134 irq_spinlock_unlock(&irq->lock, false);
135 irq_spinlock_unlock(&irq_kernel_hash_table_lock, true);
[0d107f31]136}
137
[82cbf8c6]138/** Search and lock an IRQ hash table */
139static irq_t *
140irq_dispatch_and_lock_table(hash_table_t *h, irq_spinlock_t *l, inr_t inr)
[691eb52]141{
[82cbf8c6]142 irq_spinlock_lock(l, false);
[0db0df2]143
144 hash_table_foreach(h, &inr, link, irq_t, irq) {
[82cbf8c6]145 irq_spinlock_lock(&irq->lock, false);
146 if (irq->claim(irq) == IRQ_ACCEPT) {
147 /* leave irq locked */
148 irq_spinlock_unlock(l, false);
149 return irq;
150 }
151 irq_spinlock_unlock(&irq->lock, false);
[0d107f31]152 }
[0db0df2]153
[82cbf8c6]154 irq_spinlock_unlock(l, false);
[a35b458]155
[691eb52]156 return NULL;
157}
[0d107f31]158
[4d76cfc]159/** Dispatch the IRQ
[691eb52]160 *
[ee50130]161 * We assume this function is only called from interrupt context (i.e. that
162 * interrupts are disabled prior to this call).
[691eb52]163 *
[ee50130]164 * This function attempts to lookup a fitting IRQ structure. In case of success,
165 * return with interrupts disabled and holding the respective structure.
[691eb52]166 *
[4d76cfc]167 * @param inr Interrupt number (aka inr or irq).
[691eb52]168 *
[ee50130]169 * @return IRQ structure of the respective device
170 * @return NULL if no IRQ structure found
[da1bafb]171 *
[691eb52]172 */
173irq_t *irq_dispatch_and_lock(inr_t inr)
174{
175 /*
[ee50130]176 * If the kernel console override is on, then try first the kernel
177 * handlers and eventually fall back to uspace handlers.
[691eb52]178 *
[ee50130]179 * In the usual case the uspace handlers have precedence.
[691eb52]180 */
[a35b458]181
[b366a6f4]182 if (console_override) {
[82cbf8c6]183 irq_t *irq = irq_dispatch_and_lock_table(&irq_kernel_hash_table,
184 &irq_kernel_hash_table_lock, inr);
[691eb52]185 if (irq)
186 return irq;
[a35b458]187
[82cbf8c6]188 return irq_dispatch_and_lock_table(&irq_uspace_hash_table,
189 &irq_uspace_hash_table_lock, inr);
[691eb52]190 }
[a35b458]191
[82cbf8c6]192 irq_t *irq = irq_dispatch_and_lock_table(&irq_uspace_hash_table,
193 &irq_uspace_hash_table_lock, inr);
[691eb52]194 if (irq)
195 return irq;
[a35b458]196
[82cbf8c6]197 return irq_dispatch_and_lock_table(&irq_kernel_hash_table,
198 &irq_kernel_hash_table_lock, inr);
[0d107f31]199}
200
[82cbf8c6]201/** Return the hash of the key stored in the item. */
202size_t irq_ht_hash(const ht_link_t *item)
[0d107f31]203{
[82cbf8c6]204 irq_t *irq = hash_table_get_inst(item, irq_t, link);
205 return hash_mix(irq->inr);
[0d107f31]206}
207
[82cbf8c6]208/** Return the hash of the key. */
[5e801dc]209size_t irq_ht_key_hash(const void *key)
[0d107f31]210{
[5e801dc]211 const inr_t *inr = key;
[82cbf8c6]212 return hash_mix(*inr);
[0d107f31]213}
214
[82cbf8c6]215/** Return true if the items have the same lookup key. */
216bool irq_ht_equal(const ht_link_t *item1, const ht_link_t *item2)
[2845930]217{
[82cbf8c6]218 irq_t *irq1 = hash_table_get_inst(item1, irq_t, link);
219 irq_t *irq2 = hash_table_get_inst(item2, irq_t, link);
220 return irq1->inr == irq2->inr;
221}
222
223/** Return true if the key is equal to the item's lookup key. */
[0db0df2]224bool irq_ht_key_equal(const void *key, size_t hash, const ht_link_t *item)
[82cbf8c6]225{
[5e801dc]226 const inr_t *inr = key;
[82cbf8c6]227 irq_t *irq = hash_table_get_inst(item, irq_t, link);
228 return irq->inr == *inr;
[2845930]229}
230
[0d107f31]231/** @}
232 */
Note: See TracBrowser for help on using the repository browser.