Subversion Repositories HelenOS

Rev

Rev 3906 | Rev 3950 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1919 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2006 Jakub Jermar
1919 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
 
1920 jermar 29
/** @addtogroup genericddi
1919 jermar 30
 * @{
31
 */
32
/**
33
 * @file
1922 jermar 34
 * @brief   IRQ dispatcher.
1919 jermar 35
 *
36
 * This file provides means of connecting IRQs with particular
37
 * devices and logic for dispatching interrupts to IRQ handlers
38
 * defined by those devices.
39
 *
40
 * This code is designed to support:
41
 * - multiple devices sharing single IRQ
3906 jermar 42
 * - multiple IRQs per single device
43
 * - multiple instances of the same device
1919 jermar 44
 *
45
 *
46
 * Note about architectures.
47
 *
48
 * Some architectures has the term IRQ well defined. Examples
49
 * of such architectures include amd64, ia32 and mips32. Some
50
 * other architectures, such as sparc64, don't use the term
51
 * at all. In those cases, we boldly step forward and define what
52
 * an IRQ is.
53
 *
54
 * The implementation is generic enough and still allows the
55
 * architectures to use the hardware layout effectively.
56
 * For instance, on amd64 and ia32, where there is only 16
57
 * IRQs, the irq_hash_table can be optimized to a one-dimensional
58
 * array. Next, when it is known that the IRQ numbers (aka INR's)
59
 * are unique, the claim functions can always return IRQ_ACCEPT.
1922 jermar 60
 *
61
 *
62
 * Note about the irq_hash_table.
63
 *
64
 * The hash table is configured to use two keys: inr and devno.
65
 * However, the hash index is computed only from inr. Moreover,
66
 * if devno is -1, the match is based on the return value of
67
 * the claim() function instead of on devno.
1919 jermar 68
 */
69
 
1920 jermar 70
#include <ddi/irq.h>
1919 jermar 71
#include <adt/hash_table.h>
3947 jermar 72
#include <mm/slab.h>
1919 jermar 73
#include <arch/types.h>
74
#include <synch/spinlock.h>
3947 jermar 75
#include <memstr.h>
1919 jermar 76
#include <arch.h>
77
 
1922 jermar 78
#define KEY_INR     0
79
#define KEY_DEVNO   1
80
 
1919 jermar 81
/**
3947 jermar 82
 * Spinlock protecting the kernel IRQ hash table.
1919 jermar 83
 * This lock must be taken only when interrupts are disabled.
84
 */
3947 jermar 85
static SPINLOCK_INITIALIZE(irq_kernel_hash_table_lock);
86
/** The kernel IRQ hash table. */
87
static hash_table_t irq_kernel_hash_table;
1919 jermar 88
 
89
/**
3947 jermar 90
 * Spinlock protecting the uspace IRQ hash table.
91
 * This lock must be taken only when interrupts are disabled.
92
 */
93
SPINLOCK_INITIALIZE(irq_uspace_hash_table_lock);
94
/** The uspace IRQ hash table. */
95
hash_table_t irq_uspace_hash_table;
96
 
97
/**
1919 jermar 98
 * Hash table operations for cases when we know that
99
 * there will be collisions between different keys.
100
 */
101
static index_t irq_ht_hash(unative_t *key);
102
static bool irq_ht_compare(unative_t *key, count_t keys, link_t *item);
103
 
104
static hash_table_operations_t irq_ht_ops = {
105
    .hash = irq_ht_hash,
106
    .compare = irq_ht_compare,
107
    .remove_callback = NULL     /* not used */
108
};
109
 
110
/**
111
 * Hash table operations for cases when we know that
112
 * there will be no collisions between different keys.
113
 * However, there might be still collisions among
114
 * elements with single key (sharing of one IRQ).
115
 */
116
static index_t irq_lin_hash(unative_t *key);
117
static bool irq_lin_compare(unative_t *key, count_t keys, link_t *item);
118
 
119
static hash_table_operations_t irq_lin_ops = {
120
    .hash = irq_lin_hash,
121
    .compare = irq_lin_compare,
122
    .remove_callback = NULL     /* not used */
123
};
124
 
3947 jermar 125
/** Number of buckets in either of the hash tables. */
126
static count_t buckets;
127
 
1919 jermar 128
/** Initialize IRQ subsystem.
129
 *
130
 * @param inrs Numbers of unique IRQ numbers or INRs.
131
 * @param chains Number of chains in the hash table.
132
 */
133
void irq_init(count_t inrs, count_t chains)
134
{
3947 jermar 135
    buckets = chains;
1919 jermar 136
    /*
137
     * Be smart about the choice of the hash table operations.
138
     * In cases in which inrs equals the requested number of
139
     * chains (i.e. where there is no collision between
140
     * different keys), we can use optimized set of operations.
141
     */
3947 jermar 142
    if (inrs == chains) {
143
        hash_table_create(&irq_uspace_hash_table, chains, 2,
144
            &irq_lin_ops);
145
        hash_table_create(&irq_kernel_hash_table, chains, 2,
146
            &irq_lin_ops);
147
    } else {
148
        hash_table_create(&irq_uspace_hash_table, chains, 2,
149
            &irq_ht_ops);
150
        hash_table_create(&irq_kernel_hash_table, chains, 2,
151
            &irq_ht_ops);
152
    }
1919 jermar 153
}
154
 
155
/** Initialize one IRQ structure.
156
 *
157
 * @param irq Pointer to the IRQ structure to be initialized.
158
 *
159
 */
160
void irq_initialize(irq_t *irq)
161
{
3947 jermar 162
    memsetb(irq, 0, sizeof(irq_t));
1919 jermar 163
    link_initialize(&irq->link);
1921 jermar 164
    spinlock_initialize(&irq->lock, "irq.lock");
3947 jermar 165
    link_initialize(&irq->notif_cfg.link);
1919 jermar 166
    irq->inr = -1;
167
    irq->devno = -1;
168
}
169
 
170
/** Register IRQ for device.
171
 *
172
 * The irq structure must be filled with information
173
 * about the interrupt source and with the claim()
3947 jermar 174
 * function pointer and handler() function pointer.
1919 jermar 175
 *
3947 jermar 176
 * @param irq       IRQ structure belonging to a device.
177
 * @return      True on success, false on failure.
1919 jermar 178
 */
179
void irq_register(irq_t *irq)
180
{
3947 jermar 181
    spinlock_t *lock = &irq_kernel_hash_table_lock;
182
    hash_table_t *table = &irq_kernel_hash_table;
1919 jermar 183
    ipl_t ipl;
1922 jermar 184
    unative_t key[] = {
185
        (unative_t) irq->inr,
186
        (unative_t) irq->devno
187
    };
1919 jermar 188
 
189
    ipl = interrupts_disable();
3947 jermar 190
    spinlock_lock(lock);
191
    spinlock_lock(&irq->lock);
192
    hash_table_insert(table, key, &irq->link);
193
    spinlock_unlock(&irq->lock);   
194
    spinlock_unlock(lock);
1919 jermar 195
    interrupts_restore(ipl);
196
}
197
 
198
/** Dispatch the IRQ.
199
 *
1922 jermar 200
 * We assume this function is only called from interrupt
201
 * context (i.e. that interrupts are disabled prior to
202
 * this call).
203
 *
204
 * This function attempts to lookup a fitting IRQ
205
 * structure. In case of success, return with interrupts
206
 * disabled and holding the respective structure.
207
 *
1919 jermar 208
 * @param inr Interrupt number (aka inr or irq).
209
 *
210
 * @return IRQ structure of the respective device or NULL.
211
 */
1922 jermar 212
irq_t *irq_dispatch_and_lock(inr_t inr)
1919 jermar 213
{
214
    link_t *lnk;
1922 jermar 215
    unative_t key[] = {
216
        (unative_t) inr,
217
        (unative_t) -1      /* search will use claim() instead of devno */
218
    };
1919 jermar 219
 
3947 jermar 220
    /*
221
     * Try uspace handlers first.
222
     */
223
    spinlock_lock(&irq_uspace_hash_table_lock);
224
    lnk = hash_table_find(&irq_uspace_hash_table, key);
1919 jermar 225
    if (lnk) {
226
        irq_t *irq;
227
 
228
        irq = hash_table_get_instance(lnk, irq_t, link);
3947 jermar 229
        spinlock_unlock(&irq_uspace_hash_table_lock);
1919 jermar 230
        return irq;
231
    }
3947 jermar 232
    spinlock_unlock(&irq_uspace_hash_table_lock);
1919 jermar 233
 
3947 jermar 234
    /*
235
     * Fallback to kernel handlers.
236
     */
237
    spinlock_lock(&irq_kernel_hash_table_lock);
238
    lnk = hash_table_find(&irq_kernel_hash_table, key);
1922 jermar 239
    if (lnk) {
240
        irq_t *irq;
241
 
242
        irq = hash_table_get_instance(lnk, irq_t, link);
3947 jermar 243
        spinlock_unlock(&irq_kernel_hash_table_lock);
1922 jermar 244
        return irq;
245
    }
3947 jermar 246
    spinlock_unlock(&irq_kernel_hash_table_lock);
1922 jermar 247
 
248
    return NULL;   
249
}
250
 
1919 jermar 251
/** Compute hash index for the key.
252
 *
253
 * This function computes hash index into
254
 * the IRQ hash table for which there
255
 * can be collisions between different
256
 * INRs.
257
 *
1922 jermar 258
 * The devno is not used to compute the hash.
1919 jermar 259
 *
1922 jermar 260
 * @param key The first of the keys is inr and the second is devno or -1.
261
 *
1919 jermar 262
 * @return Index into the hash table.
263
 */
1922 jermar 264
index_t irq_ht_hash(unative_t key[])
1919 jermar 265
{
1922 jermar 266
    inr_t inr = (inr_t) key[KEY_INR];
3947 jermar 267
    return inr % buckets;
1919 jermar 268
}
269
 
270
/** Compare hash table element with a key.
271
 *
1922 jermar 272
 * There are two things to note about this function.
273
 * First, it is used for the more complex architecture setup
274
 * in which there are way too many interrupt numbers (i.e. inr's)
275
 * to arrange the hash table so that collisions occur only
276
 * among same inrs of different devnos. So the explicit check
277
 * for inr match must be done.
278
 * Second, if devno is -1, the second key (i.e. devno) is not
279
 * used for the match and the result of the claim() function
280
 * is used instead.
1919 jermar 281
 *
1922 jermar 282
 * This function assumes interrupts are already disabled.
283
 *
284
 * @param key Keys (i.e. inr and devno).
285
 * @param keys This is 2.
1919 jermar 286
 * @param item The item to compare the key with.
287
 *
288
 * @return True on match or false otherwise.
289
 */
1922 jermar 290
bool irq_ht_compare(unative_t key[], count_t keys, link_t *item)
1919 jermar 291
{
292
    irq_t *irq = hash_table_get_instance(item, irq_t, link);
1922 jermar 293
    inr_t inr = (inr_t) key[KEY_INR];
294
    devno_t devno = (devno_t) key[KEY_DEVNO];
295
 
1921 jermar 296
    bool rv;
1919 jermar 297
 
1921 jermar 298
    spinlock_lock(&irq->lock);
1922 jermar 299
    if (devno == -1) {
2107 jermar 300
        /* Invoked by irq_dispatch_and_lock(). */
3906 jermar 301
        rv = ((irq->inr == inr) &&
3947 jermar 302
            (irq->claim(irq) == IRQ_ACCEPT));
1922 jermar 303
    } else {
2107 jermar 304
        /* Invoked by irq_find_and_lock(). */
1922 jermar 305
        rv = ((irq->inr == inr) && (irq->devno == devno));
306
    }
307
 
308
    /* unlock only on non-match */
309
    if (!rv)
310
        spinlock_unlock(&irq->lock);
1921 jermar 311
 
312
    return rv;
1919 jermar 313
}
314
 
315
/** Compute hash index for the key.
316
 *
317
 * This function computes hash index into
318
 * the IRQ hash table for which there
319
 * are no collisions between different
320
 * INRs.
321
 *
1922 jermar 322
 * @param key The first of the keys is inr and the second is devno or -1.
1919 jermar 323
 *
324
 * @return Index into the hash table.
325
 */
1922 jermar 326
index_t irq_lin_hash(unative_t key[])
1919 jermar 327
{
1922 jermar 328
    inr_t inr = (inr_t) key[KEY_INR];
329
    return inr;
1919 jermar 330
}
331
 
332
/** Compare hash table element with a key.
333
 *
1922 jermar 334
 * There are two things to note about this function.
335
 * First, it is used for the less complex architecture setup
336
 * in which there are not too many interrupt numbers (i.e. inr's)
337
 * to arrange the hash table so that collisions occur only
338
 * among same inrs of different devnos. So the explicit check
339
 * for inr match is not done.
340
 * Second, if devno is -1, the second key (i.e. devno) is not
341
 * used for the match and the result of the claim() function
342
 * is used instead.
1919 jermar 343
 *
1922 jermar 344
 * This function assumes interrupts are already disabled.
345
 *
346
 * @param key Keys (i.e. inr and devno).
347
 * @param keys This is 2.
1919 jermar 348
 * @param item The item to compare the key with.
349
 *
350
 * @return True on match or false otherwise.
351
 */
1922 jermar 352
bool irq_lin_compare(unative_t key[], count_t keys, link_t *item)
1919 jermar 353
{
354
    irq_t *irq = list_get_instance(item, irq_t, link);
1922 jermar 355
    devno_t devno = (devno_t) key[KEY_DEVNO];
1921 jermar 356
    bool rv;
1919 jermar 357
 
1921 jermar 358
    spinlock_lock(&irq->lock);
1922 jermar 359
    if (devno == -1) {
2107 jermar 360
        /* Invoked by irq_dispatch_and_lock() */
3947 jermar 361
        rv = (irq->claim(irq) == IRQ_ACCEPT);
1922 jermar 362
    } else {
2107 jermar 363
        /* Invoked by irq_find_and_lock() */
1922 jermar 364
        rv = (irq->devno == devno);
365
    }
1921 jermar 366
 
1922 jermar 367
    /* unlock only on non-match */
368
    if (!rv)
369
        spinlock_unlock(&irq->lock);
370
 
1921 jermar 371
    return rv;
1919 jermar 372
}
373
 
374
/** @}
375
 */