Subversion Repositories HelenOS

Rev

Rev 3675 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3675 Rev 4377
Line 37... Line 37...
37
 * devices and logic for dispatching interrupts to IRQ handlers
37
 * devices and logic for dispatching interrupts to IRQ handlers
38
 * defined by those devices.
38
 * defined by those devices.
39
 *
39
 *
40
 * This code is designed to support:
40
 * This code is designed to support:
41
 * - multiple devices sharing single IRQ
41
 * - multiple devices sharing single IRQ
42
 * - multiple IRQs per signle device
42
 * - multiple IRQs per single device
-
 
43
 * - multiple instances of the same device
43
 *
44
 *
44
 *
45
 *
45
 * Note about architectures.
46
 * Note about architectures.
46
 *
47
 *
47
 * Some architectures has the term IRQ well defined. Examples
48
 * Some architectures has the term IRQ well defined. Examples
Line 66... Line 67...
66
 * the claim() function instead of on devno.
67
 * the claim() function instead of on devno.
67
 */
68
 */
68
 
69
 
69
#include <ddi/irq.h>
70
#include <ddi/irq.h>
70
#include <adt/hash_table.h>
71
#include <adt/hash_table.h>
-
 
72
#include <mm/slab.h>
71
#include <arch/types.h>
73
#include <arch/types.h>
72
#include <synch/spinlock.h>
74
#include <synch/spinlock.h>
-
 
75
#include <console/console.h>
-
 
76
#include <memstr.h>
73
#include <arch.h>
77
#include <arch.h>
74
 
78
 
75
#define KEY_INR     0
79
#define KEY_INR     0
76
#define KEY_DEVNO   1
80
#define KEY_DEVNO   1
77
 
81
 
78
/**
82
/**
79
 * Spinlock protecting the hash table.
83
 * Spinlock protecting the kernel IRQ hash table.
80
 * This lock must be taken only when interrupts are disabled.
84
 * This lock must be taken only when interrupts are disabled.
81
 */
85
 */
82
SPINLOCK_INITIALIZE(irq_hash_table_lock);
86
SPINLOCK_INITIALIZE(irq_kernel_hash_table_lock);
-
 
87
/** The kernel IRQ hash table. */
83
static hash_table_t irq_hash_table;
88
static hash_table_t irq_kernel_hash_table;
-
 
89
 
-
 
90
/**
-
 
91
 * Spinlock protecting the uspace IRQ hash table.
-
 
92
 * This lock must be taken only when interrupts are disabled.
-
 
93
 */
-
 
94
SPINLOCK_INITIALIZE(irq_uspace_hash_table_lock);
-
 
95
/** The uspace IRQ hash table. */
-
 
96
hash_table_t irq_uspace_hash_table;
84
 
97
 
85
/**
98
/**
86
 * Hash table operations for cases when we know that
99
 * Hash table operations for cases when we know that
87
 * there will be collisions between different keys.
100
 * there will be collisions between different keys.
88
 */
101
 */
89
static index_t irq_ht_hash(unative_t *key);
102
static index_t irq_ht_hash(unative_t *key);
90
static bool irq_ht_compare(unative_t *key, count_t keys, link_t *item);
103
static bool irq_ht_compare(unative_t *key, count_t keys, link_t *item);
-
 
104
static void irq_ht_remove(link_t *item);
91
 
105
 
92
static hash_table_operations_t irq_ht_ops = {
106
static hash_table_operations_t irq_ht_ops = {
93
    .hash = irq_ht_hash,
107
    .hash = irq_ht_hash,
94
    .compare = irq_ht_compare,
108
    .compare = irq_ht_compare,
95
    .remove_callback = NULL     /* not used */
109
    .remove_callback = irq_ht_remove,
96
};
110
};
97
 
111
 
98
/**
112
/**
99
 * Hash table operations for cases when we know that
113
 * Hash table operations for cases when we know that
100
 * there will be no collisions between different keys.
114
 * there will be no collisions between different keys.
101
 * However, there might be still collisions among
115
 * However, there might be still collisions among
102
 * elements with single key (sharing of one IRQ).
116
 * elements with single key (sharing of one IRQ).
103
 */
117
 */
104
static index_t irq_lin_hash(unative_t *key);
118
static index_t irq_lin_hash(unative_t *key);
105
static bool irq_lin_compare(unative_t *key, count_t keys, link_t *item);
119
static bool irq_lin_compare(unative_t *key, count_t keys, link_t *item);
-
 
120
static void irq_lin_remove(link_t *item);
106
 
121
 
107
static hash_table_operations_t irq_lin_ops = {
122
static hash_table_operations_t irq_lin_ops = {
108
    .hash = irq_lin_hash,
123
    .hash = irq_lin_hash,
109
    .compare = irq_lin_compare,
124
    .compare = irq_lin_compare,
110
    .remove_callback = NULL     /* not used */
125
    .remove_callback = irq_lin_remove,
111
};
126
};
112
 
127
 
-
 
128
/** Number of buckets in either of the hash tables. */
-
 
129
static count_t buckets;
-
 
130
 
113
/** Initialize IRQ subsystem.
131
/** Initialize IRQ subsystem.
114
 *
132
 *
115
 * @param inrs Numbers of unique IRQ numbers or INRs.
133
 * @param inrs Numbers of unique IRQ numbers or INRs.
116
 * @param chains Number of chains in the hash table.
134
 * @param chains Number of chains in the hash table.
117
 */
135
 */
118
void irq_init(count_t inrs, count_t chains)
136
void irq_init(count_t inrs, count_t chains)
119
{
137
{
-
 
138
    buckets = chains;
120
    /*
139
    /*
121
     * Be smart about the choice of the hash table operations.
140
     * Be smart about the choice of the hash table operations.
122
     * In cases in which inrs equals the requested number of
141
     * In cases in which inrs equals the requested number of
123
     * chains (i.e. where there is no collision between
142
     * chains (i.e. where there is no collision between
124
     * different keys), we can use optimized set of operations.
143
     * different keys), we can use optimized set of operations.
125
     */
144
     */
126
    if (inrs == chains)
145
    if (inrs == chains) {
-
 
146
        hash_table_create(&irq_uspace_hash_table, chains, 2,
-
 
147
            &irq_lin_ops);
127
        hash_table_create(&irq_hash_table, chains, 2, &irq_lin_ops);
148
        hash_table_create(&irq_kernel_hash_table, chains, 2,
-
 
149
            &irq_lin_ops);
128
    else
150
    } else {
-
 
151
        hash_table_create(&irq_uspace_hash_table, chains, 2,
-
 
152
            &irq_ht_ops);
129
        hash_table_create(&irq_hash_table, chains, 2, &irq_ht_ops);
153
        hash_table_create(&irq_kernel_hash_table, chains, 2,
-
 
154
            &irq_ht_ops);
-
 
155
    }
130
}
156
}
131
 
157
 
132
/** Initialize one IRQ structure.
158
/** Initialize one IRQ structure.
133
 *
159
 *
134
 * @param irq Pointer to the IRQ structure to be initialized.
160
 * @param irq Pointer to the IRQ structure to be initialized.
135
 *
161
 *
136
 */
162
 */
137
void irq_initialize(irq_t *irq)
163
void irq_initialize(irq_t *irq)
138
{
164
{
-
 
165
    memsetb(irq, sizeof(irq_t), 0);
139
    link_initialize(&irq->link);
166
    link_initialize(&irq->link);
140
    spinlock_initialize(&irq->lock, "irq.lock");
167
    spinlock_initialize(&irq->lock, "irq.lock");
141
    irq->preack = false;
168
    link_initialize(&irq->notif_cfg.link);
142
    irq->inr = -1;
169
    irq->inr = -1;
143
    irq->devno = -1;
170
    irq->devno = -1;
144
    irq->trigger = (irq_trigger_t) 0;
-
 
145
    irq->claim = NULL;
-
 
146
    irq->handler = NULL;
-
 
147
    irq->arg = NULL;
-
 
148
    irq->cir = NULL;
-
 
149
    irq->cir_arg = NULL;
-
 
150
    irq->notif_cfg.notify = false;
-
 
151
    irq->notif_cfg.answerbox = NULL;
-
 
152
    irq->notif_cfg.code = NULL;
-
 
153
    irq->notif_cfg.method = 0;
-
 
154
    irq->notif_cfg.counter = 0;
-
 
155
    link_initialize(&irq->notif_cfg.link);
-
 
156
}
171
}
157
 
172
 
158
/** Register IRQ for device.
173
/** Register IRQ for device.
159
 *
174
 *
160
 * The irq structure must be filled with information
175
 * The irq structure must be filled with information
161
 * about the interrupt source and with the claim()
176
 * about the interrupt source and with the claim()
162
 * function pointer and irq_handler() function pointer.
177
 * function pointer and handler() function pointer.
163
 *
178
 *
164
 * @param irq IRQ structure belonging to a device.
179
 * @param irq       IRQ structure belonging to a device.
-
 
180
 * @return      True on success, false on failure.
165
 */
181
 */
166
void irq_register(irq_t *irq)
182
void irq_register(irq_t *irq)
167
{
183
{
168
    ipl_t ipl;
184
    ipl_t ipl;
169
    unative_t key[] = {
185
    unative_t key[] = {
170
        (unative_t) irq->inr,
186
        (unative_t) irq->inr,
171
        (unative_t) irq->devno
187
        (unative_t) irq->devno
172
    };
188
    };
173
   
189
   
174
    ipl = interrupts_disable();
190
    ipl = interrupts_disable();
175
    spinlock_lock(&irq_hash_table_lock);
191
    spinlock_lock(&irq_kernel_hash_table_lock);
-
 
192
    spinlock_lock(&irq->lock);
176
    hash_table_insert(&irq_hash_table, key, &irq->link);
193
    hash_table_insert(&irq_kernel_hash_table, key, &irq->link);
-
 
194
    spinlock_unlock(&irq->lock);   
177
    spinlock_unlock(&irq_hash_table_lock);
195
    spinlock_unlock(&irq_kernel_hash_table_lock);
178
    interrupts_restore(ipl);
196
    interrupts_restore(ipl);
179
}
197
}
180
 
198
 
181
/** Dispatch the IRQ.
199
/** Search and lock the uspace IRQ hash table.
182
 *
200
 *
183
 * We assume this function is only called from interrupt
-
 
184
 * context (i.e. that interrupts are disabled prior to
-
 
185
 * this call).
-
 
186
 *
-
 
187
 * This function attempts to lookup a fitting IRQ
-
 
188
 * structure. In case of success, return with interrupts
-
 
189
 * disabled and holding the respective structure.
-
 
190
 *
-
 
191
 * @param inr Interrupt number (aka inr or irq).
-
 
192
 *
-
 
193
 * @return IRQ structure of the respective device or NULL.
-
 
194
 */
201
 */
195
irq_t *irq_dispatch_and_lock(inr_t inr)
202
static irq_t *irq_dispatch_and_lock_uspace(inr_t inr)
196
{
203
{
197
    link_t *lnk;
204
    link_t *lnk;
198
    unative_t key[] = {
205
    unative_t key[] = {
199
        (unative_t) inr,
206
        (unative_t) inr,
200
        (unative_t) -1      /* search will use claim() instead of devno */
207
        (unative_t) -1    /* search will use claim() instead of devno */
201
    };
208
    };
202
   
209
   
203
    spinlock_lock(&irq_hash_table_lock);
210
    spinlock_lock(&irq_uspace_hash_table_lock);
204
 
-
 
205
    lnk = hash_table_find(&irq_hash_table, key);
211
    lnk = hash_table_find(&irq_uspace_hash_table, key);
206
    if (lnk) {
212
    if (lnk) {
207
        irq_t *irq;
213
        irq_t *irq;
208
       
214
       
209
        irq = hash_table_get_instance(lnk, irq_t, link);
215
        irq = hash_table_get_instance(lnk, irq_t, link);
210
 
-
 
211
        spinlock_unlock(&irq_hash_table_lock);
216
        spinlock_unlock(&irq_uspace_hash_table_lock);
212
        return irq;
217
        return irq;
213
    }
218
    }
214
   
-
 
215
    spinlock_unlock(&irq_hash_table_lock);
219
    spinlock_unlock(&irq_uspace_hash_table_lock);
216
 
220
   
217
    return NULL;   
221
    return NULL;
218
}
222
}
219
 
223
 
220
/** Find the IRQ structure corresponding to inr and devno.
-
 
221
 *
-
 
222
 * This functions attempts to lookup the IRQ structure
-
 
223
 * corresponding to its arguments. On success, this
-
 
224
 * function returns with interrups disabled, holding
-
 
225
 * the lock of the respective IRQ structure.
224
/** Search and lock the kernel IRQ hash table.
226
 *
225
 *
227
 * This function assumes interrupts are already disabled.
-
 
228
 *
-
 
229
 * @param inr INR being looked up.
-
 
230
 * @param devno Devno being looked up.
-
 
231
 *
-
 
232
 * @return Locked IRQ structure on success or NULL on failure.
-
 
233
 */
226
 */
234
irq_t *irq_find_and_lock(inr_t inr, devno_t devno)
227
static irq_t *irq_dispatch_and_lock_kernel(inr_t inr)
235
{
228
{
236
    link_t *lnk;
229
    link_t *lnk;
237
    unative_t keys[] = {
230
    unative_t key[] = {
238
        (unative_t) inr,
231
        (unative_t) inr,
239
        (unative_t) devno
232
        (unative_t) -1    /* search will use claim() instead of devno */
240
    };
233
    };
241
   
234
   
242
    spinlock_lock(&irq_hash_table_lock);
235
    spinlock_lock(&irq_kernel_hash_table_lock);
243
 
-
 
244
    lnk = hash_table_find(&irq_hash_table, keys);
236
    lnk = hash_table_find(&irq_kernel_hash_table, key);
245
    if (lnk) {
237
    if (lnk) {
246
        irq_t *irq;
238
        irq_t *irq;
247
       
239
       
248
        irq = hash_table_get_instance(lnk, irq_t, link);
240
        irq = hash_table_get_instance(lnk, irq_t, link);
249
 
-
 
250
        spinlock_unlock(&irq_hash_table_lock);
241
        spinlock_unlock(&irq_kernel_hash_table_lock);
251
        return irq;
242
        return irq;
252
    }
243
    }
253
   
-
 
254
    spinlock_unlock(&irq_hash_table_lock);
244
    spinlock_unlock(&irq_kernel_hash_table_lock);
255
 
245
   
256
    return NULL;   
246
    return NULL;
257
}
247
}
258
 
248
 
-
 
249
/** Dispatch the IRQ.
-
 
250
 *
-
 
251
 * We assume this function is only called from interrupt
-
 
252
 * context (i.e. that interrupts are disabled prior to
-
 
253
 * this call).
-
 
254
 *
-
 
255
 * This function attempts to lookup a fitting IRQ
-
 
256
 * structure. In case of success, return with interrupts
-
 
257
 * disabled and holding the respective structure.
-
 
258
 *
-
 
259
 * @param inr Interrupt number (aka inr or irq).
-
 
260
 *
-
 
261
 * @return IRQ structure of the respective device or NULL.
-
 
262
 */
-
 
263
irq_t *irq_dispatch_and_lock(inr_t inr)
-
 
264
{
-
 
265
    irq_t *irq;
-
 
266
   
-
 
267
    /*
-
 
268
     * If the kernel console is silenced,
-
 
269
     * then try first the uspace handlers,
-
 
270
     * eventually fall back to kernel handlers.
-
 
271
     *
-
 
272
     * If the kernel console is active,
-
 
273
     * then do it the other way around.
-
 
274
     */
-
 
275
    if (silent) {
-
 
276
        irq = irq_dispatch_and_lock_uspace(inr);
-
 
277
        if (irq)
-
 
278
            return irq;
-
 
279
        return irq_dispatch_and_lock_kernel(inr);
-
 
280
    }
-
 
281
   
-
 
282
    irq = irq_dispatch_and_lock_kernel(inr);
-
 
283
    if (irq)
-
 
284
        return irq;
-
 
285
    return irq_dispatch_and_lock_uspace(inr);
-
 
286
}
-
 
287
 
259
/** Compute hash index for the key.
288
/** Compute hash index for the key.
260
 *
289
 *
261
 * This function computes hash index into
290
 * This function computes hash index into
262
 * the IRQ hash table for which there
291
 * the IRQ hash table for which there
263
 * can be collisions between different
292
 * can be collisions between different
Line 270... Line 299...
270
 * @return Index into the hash table.
299
 * @return Index into the hash table.
271
 */
300
 */
272
index_t irq_ht_hash(unative_t key[])
301
index_t irq_ht_hash(unative_t key[])
273
{
302
{
274
    inr_t inr = (inr_t) key[KEY_INR];
303
    inr_t inr = (inr_t) key[KEY_INR];
275
    return inr % irq_hash_table.entries;
304
    return inr % buckets;
276
}
305
}
277
 
306
 
278
/** Compare hash table element with a key.
307
/** Compare hash table element with a key.
279
 *
308
 *
280
 * There are two things to note about this function.
309
 * There are two things to note about this function.
Line 304... Line 333...
304
    bool rv;
333
    bool rv;
305
   
334
   
306
    spinlock_lock(&irq->lock);
335
    spinlock_lock(&irq->lock);
307
    if (devno == -1) {
336
    if (devno == -1) {
308
        /* Invoked by irq_dispatch_and_lock(). */
337
        /* Invoked by irq_dispatch_and_lock(). */
-
 
338
        rv = ((irq->inr == inr) &&
309
        rv = ((irq->inr == inr) && (irq->claim() == IRQ_ACCEPT));
339
            (irq->claim(irq) == IRQ_ACCEPT));
310
    } else {
340
    } else {
311
        /* Invoked by irq_find_and_lock(). */
341
        /* Invoked by irq_find_and_lock(). */
312
        rv = ((irq->inr == inr) && (irq->devno == devno));
342
        rv = ((irq->inr == inr) && (irq->devno == devno));
313
    }
343
    }
314
   
344
   
Line 317... Line 347...
317
        spinlock_unlock(&irq->lock);
347
        spinlock_unlock(&irq->lock);
318
 
348
 
319
    return rv;
349
    return rv;
320
}
350
}
321
 
351
 
-
 
352
/** Unlock IRQ structure after hash_table_remove().
-
 
353
 *
-
 
354
 * @param lnk Link in the removed and locked IRQ structure.
-
 
355
 */
-
 
356
void irq_ht_remove(link_t *lnk)
-
 
357
{
-
 
358
    irq_t *irq __attribute__((unused))
-
 
359
        = hash_table_get_instance(lnk, irq_t, link);
-
 
360
    spinlock_unlock(&irq->lock);
-
 
361
}
-
 
362
 
322
/** Compute hash index for the key.
363
/** Compute hash index for the key.
323
 *
364
 *
324
 * This function computes hash index into
365
 * This function computes hash index into
325
 * the IRQ hash table for which there
366
 * the IRQ hash table for which there
326
 * are no collisions between different
367
 * are no collisions between different
Line 363... Line 404...
363
    bool rv;
404
    bool rv;
364
   
405
   
365
    spinlock_lock(&irq->lock);
406
    spinlock_lock(&irq->lock);
366
    if (devno == -1) {
407
    if (devno == -1) {
367
        /* Invoked by irq_dispatch_and_lock() */
408
        /* Invoked by irq_dispatch_and_lock() */
368
        rv = (irq->claim() == IRQ_ACCEPT);
409
        rv = (irq->claim(irq) == IRQ_ACCEPT);
369
    } else {
410
    } else {
370
        /* Invoked by irq_find_and_lock() */
411
        /* Invoked by irq_find_and_lock() */
371
        rv = (irq->devno == devno);
412
        rv = (irq->devno == devno);
372
    }
413
    }
373
   
414
   
Line 376... Line 417...
376
        spinlock_unlock(&irq->lock);
417
        spinlock_unlock(&irq->lock);
377
   
418
   
378
    return rv;
419
    return rv;
379
}
420
}
380
 
421
 
-
 
422
/** Unlock IRQ structure after hash_table_remove().
-
 
423
 *
-
 
424
 * @param lnk       Link in the removed and locked IRQ structure.
-
 
425
 */
-
 
426
void irq_lin_remove(link_t *lnk)
-
 
427
{
-
 
428
    irq_t *irq __attribute__((unused))
-
 
429
        = hash_table_get_instance(lnk, irq_t, link);
-
 
430
    spinlock_unlock(&irq->lock);
-
 
431
}
-
 
432
 
381
/** @}
433
/** @}
382
 */
434
 */