Subversion Repositories HelenOS

Rev

Rev 3022 | Rev 4296 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3022 Rev 4055
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
 */
Line 108... Line 121...
108
    .hash = irq_lin_hash,
121
    .hash = irq_lin_hash,
109
    .compare = irq_lin_compare,
122
    .compare = irq_lin_compare,
110
    .remove_callback = NULL     /* not used */
123
    .remove_callback = NULL     /* not used */
111
};
124
};
112
 
125
 
-
 
126
/** Number of buckets in either of the hash tables. */
-
 
127
static count_t buckets;
-
 
128
 
113
/** Initialize IRQ subsystem.
129
/** Initialize IRQ subsystem.
114
 *
130
 *
115
 * @param inrs Numbers of unique IRQ numbers or INRs.
131
 * @param inrs Numbers of unique IRQ numbers or INRs.
116
 * @param chains Number of chains in the hash table.
132
 * @param chains Number of chains in the hash table.
117
 */
133
 */
118
void irq_init(count_t inrs, count_t chains)
134
void irq_init(count_t inrs, count_t chains)
119
{
135
{
-
 
136
    buckets = chains;
120
    /*
137
    /*
121
     * Be smart about the choice of the hash table operations.
138
     * Be smart about the choice of the hash table operations.
122
     * In cases in which inrs equals the requested number of
139
     * In cases in which inrs equals the requested number of
123
     * chains (i.e. where there is no collision between
140
     * chains (i.e. where there is no collision between
124
     * different keys), we can use optimized set of operations.
141
     * different keys), we can use optimized set of operations.
125
     */
142
     */
126
    if (inrs == chains)
143
    if (inrs == chains) {
-
 
144
        hash_table_create(&irq_uspace_hash_table, chains, 2,
-
 
145
            &irq_lin_ops);
127
        hash_table_create(&irq_hash_table, chains, 2, &irq_lin_ops);
146
        hash_table_create(&irq_kernel_hash_table, chains, 2,
-
 
147
            &irq_lin_ops);
128
    else
148
    } else {
-
 
149
        hash_table_create(&irq_uspace_hash_table, chains, 2,
-
 
150
            &irq_ht_ops);
129
        hash_table_create(&irq_hash_table, chains, 2, &irq_ht_ops);
151
        hash_table_create(&irq_kernel_hash_table, chains, 2,
-
 
152
            &irq_ht_ops);
-
 
153
    }
130
}
154
}
131
 
155
 
132
/** Initialize one IRQ structure.
156
/** Initialize one IRQ structure.
133
 *
157
 *
134
 * @param irq Pointer to the IRQ structure to be initialized.
158
 * @param irq Pointer to the IRQ structure to be initialized.
135
 *
159
 *
136
 */
160
 */
137
void irq_initialize(irq_t *irq)
161
void irq_initialize(irq_t *irq)
138
{
162
{
-
 
163
    memsetb(irq, sizeof(irq_t), 0);
139
    link_initialize(&irq->link);
164
    link_initialize(&irq->link);
140
    spinlock_initialize(&irq->lock, "irq.lock");
165
    spinlock_initialize(&irq->lock, "irq.lock");
141
    irq->preack = false;
166
    link_initialize(&irq->notif_cfg.link);
142
    irq->inr = -1;
167
    irq->inr = -1;
143
    irq->devno = -1;
168
    irq->devno = -1;
144
    irq->trigger = (irq_trigger_t) 0;
-
 
145
    irq->claim = NULL;
-
 
146
    irq->handler = NULL;
-
 
147
    irq->arg = NULL;
-
 
148
    irq->notif_cfg.notify = false;
-
 
149
    irq->notif_cfg.answerbox = NULL;
-
 
150
    irq->notif_cfg.code = NULL;
-
 
151
    irq->notif_cfg.method = 0;
-
 
152
    irq->notif_cfg.counter = 0;
-
 
153
    link_initialize(&irq->notif_cfg.link);
-
 
154
}
169
}
155
 
170
 
156
/** Register IRQ for device.
171
/** Register IRQ for device.
157
 *
172
 *
158
 * The irq structure must be filled with information
173
 * The irq structure must be filled with information
159
 * about the interrupt source and with the claim()
174
 * about the interrupt source and with the claim()
160
 * function pointer and irq_handler() function pointer.
175
 * function pointer and handler() function pointer.
161
 *
176
 *
162
 * @param irq IRQ structure belonging to a device.
177
 * @param irq       IRQ structure belonging to a device.
-
 
178
 * @return      True on success, false on failure.
163
 */
179
 */
164
void irq_register(irq_t *irq)
180
void irq_register(irq_t *irq)
165
{
181
{
166
    ipl_t ipl;
182
    ipl_t ipl;
167
    unative_t key[] = {
183
    unative_t key[] = {
168
        (unative_t) irq->inr,
184
        (unative_t) irq->inr,
169
        (unative_t) irq->devno
185
        (unative_t) irq->devno
170
    };
186
    };
171
   
187
   
172
    ipl = interrupts_disable();
188
    ipl = interrupts_disable();
173
    spinlock_lock(&irq_hash_table_lock);
189
    spinlock_lock(&irq_kernel_hash_table_lock);
-
 
190
    spinlock_lock(&irq->lock);
174
    hash_table_insert(&irq_hash_table, key, &irq->link);
191
    hash_table_insert(&irq_kernel_hash_table, key, &irq->link);
-
 
192
    spinlock_unlock(&irq->lock);   
175
    spinlock_unlock(&irq_hash_table_lock);
193
    spinlock_unlock(&irq_kernel_hash_table_lock);
176
    interrupts_restore(ipl);
194
    interrupts_restore(ipl);
177
}
195
}
178
 
196
 
179
/** Dispatch the IRQ.
-
 
180
 *
-
 
181
 * We assume this function is only called from interrupt
-
 
182
 * context (i.e. that interrupts are disabled prior to
-
 
183
 * this call).
-
 
184
 *
-
 
185
 * This function attempts to lookup a fitting IRQ
-
 
186
 * structure. In case of success, return with interrupts
-
 
187
 * disabled and holding the respective structure.
197
/** Search and lock the uspace IRQ hash table.
188
 *
-
 
189
 * @param inr Interrupt number (aka inr or irq).
-
 
190
 *
198
 *
191
 * @return IRQ structure of the respective device or NULL.
-
 
192
 */
199
 */
193
irq_t *irq_dispatch_and_lock(inr_t inr)
200
static irq_t *irq_dispatch_and_lock_uspace(inr_t inr)
194
{
201
{
195
    link_t *lnk;
202
    link_t *lnk;
196
    unative_t key[] = {
203
    unative_t key[] = {
197
        (unative_t) inr,
204
        (unative_t) inr,
198
        (unative_t) -1      /* search will use claim() instead of devno */
205
        (unative_t) -1    /* search will use claim() instead of devno */
199
    };
206
    };
200
   
207
   
201
    spinlock_lock(&irq_hash_table_lock);
208
    spinlock_lock(&irq_uspace_hash_table_lock);
202
 
-
 
203
    lnk = hash_table_find(&irq_hash_table, key);
209
    lnk = hash_table_find(&irq_uspace_hash_table, key);
204
    if (lnk) {
210
    if (lnk) {
205
        irq_t *irq;
211
        irq_t *irq;
206
       
212
       
207
        irq = hash_table_get_instance(lnk, irq_t, link);
213
        irq = hash_table_get_instance(lnk, irq_t, link);
208
 
-
 
209
        spinlock_unlock(&irq_hash_table_lock);
214
        spinlock_unlock(&irq_uspace_hash_table_lock);
210
        return irq;
215
        return irq;
211
    }
216
    }
-
 
217
    spinlock_unlock(&irq_uspace_hash_table_lock);
212
   
218
   
213
    spinlock_unlock(&irq_hash_table_lock);
-
 
214
 
-
 
215
    return NULL;   
219
    return NULL;
216
}
220
}
217
 
221
 
218
/** Find the IRQ structure corresponding to inr and devno.
-
 
219
 *
-
 
220
 * This functions attempts to lookup the IRQ structure
-
 
221
 * corresponding to its arguments. On success, this
-
 
222
 * function returns with interrups disabled, holding
-
 
223
 * the lock of the respective IRQ structure.
222
/** Search and lock the kernel IRQ hash table.
224
 *
-
 
225
 * This function assumes interrupts are already disabled.
-
 
226
 *
-
 
227
 * @param inr INR being looked up.
-
 
228
 * @param devno Devno being looked up.
-
 
229
 *
223
 *
230
 * @return Locked IRQ structure on success or NULL on failure.
-
 
231
 */
224
 */
232
irq_t *irq_find_and_lock(inr_t inr, devno_t devno)
225
static irq_t *irq_dispatch_and_lock_kernel(inr_t inr)
233
{
226
{
234
    link_t *lnk;
227
    link_t *lnk;
235
    unative_t keys[] = {
228
    unative_t key[] = {
236
        (unative_t) inr,
229
        (unative_t) inr,
237
        (unative_t) devno
230
        (unative_t) -1    /* search will use claim() instead of devno */
238
    };
231
    };
239
   
232
   
240
    spinlock_lock(&irq_hash_table_lock);
233
    spinlock_lock(&irq_kernel_hash_table_lock);
241
 
-
 
242
    lnk = hash_table_find(&irq_hash_table, keys);
234
    lnk = hash_table_find(&irq_kernel_hash_table, key);
243
    if (lnk) {
235
    if (lnk) {
244
        irq_t *irq;
236
        irq_t *irq;
245
       
237
       
246
        irq = hash_table_get_instance(lnk, irq_t, link);
238
        irq = hash_table_get_instance(lnk, irq_t, link);
247
 
-
 
248
        spinlock_unlock(&irq_hash_table_lock);
239
        spinlock_unlock(&irq_kernel_hash_table_lock);
249
        return irq;
240
        return irq;
250
    }
241
    }
-
 
242
    spinlock_unlock(&irq_kernel_hash_table_lock);
251
   
243
   
252
    spinlock_unlock(&irq_hash_table_lock);
244
    return NULL;
-
 
245
}
253
 
246
 
-
 
247
/** Dispatch the IRQ.
-
 
248
 *
-
 
249
 * We assume this function is only called from interrupt
-
 
250
 * context (i.e. that interrupts are disabled prior to
-
 
251
 * this call).
-
 
252
 *
-
 
253
 * This function attempts to lookup a fitting IRQ
-
 
254
 * structure. In case of success, return with interrupts
-
 
255
 * disabled and holding the respective structure.
-
 
256
 *
-
 
257
 * @param inr Interrupt number (aka inr or irq).
-
 
258
 *
-
 
259
 * @return IRQ structure of the respective device or NULL.
-
 
260
 */
-
 
261
irq_t *irq_dispatch_and_lock(inr_t inr)
-
 
262
{
-
 
263
    irq_t *irq;
-
 
264
   
-
 
265
    /*
-
 
266
     * If the kernel console is silenced,
-
 
267
     * then try first the uspace handlers,
-
 
268
     * eventually fall back to kernel handlers.
-
 
269
     *
-
 
270
     * If the kernel console is active,
-
 
271
     * then do it the other way around.
-
 
272
     */
-
 
273
    if (silent) {
-
 
274
        irq = irq_dispatch_and_lock_uspace(inr);
-
 
275
        if (irq)
-
 
276
            return irq;
-
 
277
        return irq_dispatch_and_lock_kernel(inr);
-
 
278
    }
-
 
279
   
-
 
280
    irq = irq_dispatch_and_lock_kernel(inr);
-
 
281
    if (irq)
254
    return NULL;   
282
        return irq;
-
 
283
    return irq_dispatch_and_lock_uspace(inr);
255
}
284
}
256
 
285
 
257
/** Compute hash index for the key.
286
/** Compute hash index for the key.
258
 *
287
 *
259
 * This function computes hash index into
288
 * This function computes hash index into
Line 268... Line 297...
268
 * @return Index into the hash table.
297
 * @return Index into the hash table.
269
 */
298
 */
270
index_t irq_ht_hash(unative_t key[])
299
index_t irq_ht_hash(unative_t key[])
271
{
300
{
272
    inr_t inr = (inr_t) key[KEY_INR];
301
    inr_t inr = (inr_t) key[KEY_INR];
273
    return inr % irq_hash_table.entries;
302
    return inr % buckets;
274
}
303
}
275
 
304
 
276
/** Compare hash table element with a key.
305
/** Compare hash table element with a key.
277
 *
306
 *
278
 * There are two things to note about this function.
307
 * There are two things to note about this function.
Line 302... Line 331...
302
    bool rv;
331
    bool rv;
303
   
332
   
304
    spinlock_lock(&irq->lock);
333
    spinlock_lock(&irq->lock);
305
    if (devno == -1) {
334
    if (devno == -1) {
306
        /* Invoked by irq_dispatch_and_lock(). */
335
        /* Invoked by irq_dispatch_and_lock(). */
-
 
336
        rv = ((irq->inr == inr) &&
307
        rv = ((irq->inr == inr) && (irq->claim() == IRQ_ACCEPT));
337
            (irq->claim(irq) == IRQ_ACCEPT));
308
    } else {
338
    } else {
309
        /* Invoked by irq_find_and_lock(). */
339
        /* Invoked by irq_find_and_lock(). */
310
        rv = ((irq->inr == inr) && (irq->devno == devno));
340
        rv = ((irq->inr == inr) && (irq->devno == devno));
311
    }
341
    }
312
   
342
   
Line 361... Line 391...
361
    bool rv;
391
    bool rv;
362
   
392
   
363
    spinlock_lock(&irq->lock);
393
    spinlock_lock(&irq->lock);
364
    if (devno == -1) {
394
    if (devno == -1) {
365
        /* Invoked by irq_dispatch_and_lock() */
395
        /* Invoked by irq_dispatch_and_lock() */
366
        rv = (irq->claim() == IRQ_ACCEPT);
396
        rv = (irq->claim(irq) == IRQ_ACCEPT);
367
    } else {
397
    } else {
368
        /* Invoked by irq_find_and_lock() */
398
        /* Invoked by irq_find_and_lock() */
369
        rv = (irq->devno == devno);
399
        rv = (irq->devno == devno);
370
    }
400
    }
371
   
401