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