Subversion Repositories HelenOS

Rev

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

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