| 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 kernel_generic_ddi
|
|---|
| 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.h>
|
|---|
| 43 | #include <adt/hash_table.h>
|
|---|
| 44 | #include <mm/slab.h>
|
|---|
| 45 | #include <typedefs.h>
|
|---|
| 46 | #include <synch/spinlock.h>
|
|---|
| 47 | #include <console/console.h>
|
|---|
| 48 | #include <interrupt.h>
|
|---|
| 49 | #include <memw.h>
|
|---|
| 50 | #include <arch.h>
|
|---|
| 51 |
|
|---|
| 52 | /** Spinlock protecting the kernel IRQ hash table
|
|---|
| 53 | *
|
|---|
| 54 | * This lock must be taken only when interrupts are disabled.
|
|---|
| 55 | *
|
|---|
| 56 | */
|
|---|
| 57 | IRQ_SPINLOCK_STATIC_INITIALIZE(irq_kernel_hash_table_lock);
|
|---|
| 58 |
|
|---|
| 59 | /** The kernel IRQ hash table. */
|
|---|
| 60 | static hash_table_t irq_kernel_hash_table;
|
|---|
| 61 |
|
|---|
| 62 | /** Spinlock protecting the uspace IRQ hash table
|
|---|
| 63 | *
|
|---|
| 64 | * This lock must be taken only when interrupts are disabled.
|
|---|
| 65 | *
|
|---|
| 66 | */
|
|---|
| 67 | IRQ_SPINLOCK_INITIALIZE(irq_uspace_hash_table_lock);
|
|---|
| 68 |
|
|---|
| 69 | /** The uspace IRQ hash table */
|
|---|
| 70 | hash_table_t irq_uspace_hash_table;
|
|---|
| 71 |
|
|---|
| 72 | static size_t irq_ht_hash(const ht_link_t *);
|
|---|
| 73 | static size_t irq_ht_key_hash(const void *);
|
|---|
| 74 | static bool irq_ht_equal(const ht_link_t *, const ht_link_t *);
|
|---|
| 75 | static bool irq_ht_key_equal(const void *, const ht_link_t *);
|
|---|
| 76 |
|
|---|
| 77 | static const hash_table_ops_t irq_ht_ops = {
|
|---|
| 78 | .hash = irq_ht_hash,
|
|---|
| 79 | .key_hash = irq_ht_key_hash,
|
|---|
| 80 | .equal = irq_ht_equal,
|
|---|
| 81 | .key_equal = irq_ht_key_equal
|
|---|
| 82 | };
|
|---|
| 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 | last_inr = inrs - 1;
|
|---|
| 96 |
|
|---|
| 97 | hash_table_create(&irq_uspace_hash_table, chains, 0, &irq_ht_ops);
|
|---|
| 98 | hash_table_create(&irq_kernel_hash_table, chains, 0, &irq_ht_ops);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /** Initialize one IRQ structure
|
|---|
| 102 | *
|
|---|
| 103 | * @param irq Pointer to the IRQ structure to be initialized.
|
|---|
| 104 | *
|
|---|
| 105 | */
|
|---|
| 106 | void irq_initialize(irq_t *irq)
|
|---|
| 107 | {
|
|---|
| 108 | memsetb(irq, sizeof(irq_t), 0);
|
|---|
| 109 | irq_spinlock_initialize(&irq->lock, "irq.lock");
|
|---|
| 110 | irq->inr = -1;
|
|---|
| 111 |
|
|---|
| 112 | irq_initialize_arch(irq);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /** Register IRQ for device
|
|---|
| 116 | *
|
|---|
| 117 | * The irq structure must be filled with information about the interrupt source
|
|---|
| 118 | * and with the claim() function pointer and handler() function pointer.
|
|---|
| 119 | *
|
|---|
| 120 | * @param irq IRQ structure belonging to a device.
|
|---|
| 121 | *
|
|---|
| 122 | */
|
|---|
| 123 | void irq_register(irq_t *irq)
|
|---|
| 124 | {
|
|---|
| 125 | irq_spinlock_lock(&irq_kernel_hash_table_lock, true);
|
|---|
| 126 | irq_spinlock_lock(&irq->lock, false);
|
|---|
| 127 | hash_table_insert(&irq_kernel_hash_table, &irq->link);
|
|---|
| 128 | irq_spinlock_unlock(&irq->lock, false);
|
|---|
| 129 | irq_spinlock_unlock(&irq_kernel_hash_table_lock, true);
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /** Search and lock an IRQ hash table */
|
|---|
| 133 | static irq_t *
|
|---|
| 134 | irq_dispatch_and_lock_table(hash_table_t *h, irq_spinlock_t *l, inr_t inr)
|
|---|
| 135 | {
|
|---|
| 136 | irq_spinlock_lock(l, false);
|
|---|
| 137 | ht_link_t *first = hash_table_find(h, &inr);
|
|---|
| 138 | for (ht_link_t *lnk = first; lnk;
|
|---|
| 139 | lnk = hash_table_find_next(h, first, lnk)) {
|
|---|
| 140 | irq_t *irq = hash_table_get_inst(lnk, irq_t, link);
|
|---|
| 141 | irq_spinlock_lock(&irq->lock, false);
|
|---|
| 142 | if (irq->claim(irq) == IRQ_ACCEPT) {
|
|---|
| 143 | /* leave irq locked */
|
|---|
| 144 | irq_spinlock_unlock(l, false);
|
|---|
| 145 | return irq;
|
|---|
| 146 | }
|
|---|
| 147 | irq_spinlock_unlock(&irq->lock, false);
|
|---|
| 148 | }
|
|---|
| 149 | irq_spinlock_unlock(l, false);
|
|---|
| 150 |
|
|---|
| 151 | return NULL;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /** Dispatch the IRQ
|
|---|
| 155 | *
|
|---|
| 156 | * We assume this function is only called from interrupt context (i.e. that
|
|---|
| 157 | * interrupts are disabled prior to this call).
|
|---|
| 158 | *
|
|---|
| 159 | * This function attempts to lookup a fitting IRQ structure. In case of success,
|
|---|
| 160 | * return with interrupts disabled and holding the respective structure.
|
|---|
| 161 | *
|
|---|
| 162 | * @param inr Interrupt number (aka inr or irq).
|
|---|
| 163 | *
|
|---|
| 164 | * @return IRQ structure of the respective device
|
|---|
| 165 | * @return NULL if no IRQ structure found
|
|---|
| 166 | *
|
|---|
| 167 | */
|
|---|
| 168 | irq_t *irq_dispatch_and_lock(inr_t inr)
|
|---|
| 169 | {
|
|---|
| 170 | /*
|
|---|
| 171 | * If the kernel console override is on, then try first the kernel
|
|---|
| 172 | * handlers and eventually fall back to uspace handlers.
|
|---|
| 173 | *
|
|---|
| 174 | * In the usual case the uspace handlers have precedence.
|
|---|
| 175 | */
|
|---|
| 176 |
|
|---|
| 177 | if (console_override) {
|
|---|
| 178 | irq_t *irq = irq_dispatch_and_lock_table(&irq_kernel_hash_table,
|
|---|
| 179 | &irq_kernel_hash_table_lock, inr);
|
|---|
| 180 | if (irq)
|
|---|
| 181 | return irq;
|
|---|
| 182 |
|
|---|
| 183 | return irq_dispatch_and_lock_table(&irq_uspace_hash_table,
|
|---|
| 184 | &irq_uspace_hash_table_lock, inr);
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | irq_t *irq = irq_dispatch_and_lock_table(&irq_uspace_hash_table,
|
|---|
| 188 | &irq_uspace_hash_table_lock, inr);
|
|---|
| 189 | if (irq)
|
|---|
| 190 | return irq;
|
|---|
| 191 |
|
|---|
| 192 | return irq_dispatch_and_lock_table(&irq_kernel_hash_table,
|
|---|
| 193 | &irq_kernel_hash_table_lock, inr);
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /** Return the hash of the key stored in the item. */
|
|---|
| 197 | size_t irq_ht_hash(const ht_link_t *item)
|
|---|
| 198 | {
|
|---|
| 199 | irq_t *irq = hash_table_get_inst(item, irq_t, link);
|
|---|
| 200 | return hash_mix(irq->inr);
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | /** Return the hash of the key. */
|
|---|
| 204 | size_t irq_ht_key_hash(const void *key)
|
|---|
| 205 | {
|
|---|
| 206 | const inr_t *inr = key;
|
|---|
| 207 | return hash_mix(*inr);
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | /** Return true if the items have the same lookup key. */
|
|---|
| 211 | bool irq_ht_equal(const ht_link_t *item1, const ht_link_t *item2)
|
|---|
| 212 | {
|
|---|
| 213 | irq_t *irq1 = hash_table_get_inst(item1, irq_t, link);
|
|---|
| 214 | irq_t *irq2 = hash_table_get_inst(item2, irq_t, link);
|
|---|
| 215 | return irq1->inr == irq2->inr;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | /** Return true if the key is equal to the item's lookup key. */
|
|---|
| 219 | bool irq_ht_key_equal(const void *key, const ht_link_t *item)
|
|---|
| 220 | {
|
|---|
| 221 | const inr_t *inr = key;
|
|---|
| 222 | irq_t *irq = hash_table_get_inst(item, irq_t, link);
|
|---|
| 223 | return irq->inr == *inr;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | /** @}
|
|---|
| 227 | */
|
|---|