Subversion Repositories HelenOS

Rev

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

Rev 3906 Rev 3947
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 <memstr.h>
74
#include <arch.h>
76
#include <arch.h>
75
 
77
 
76
#define KEY_INR     0
78
#define KEY_INR     0
77
#define KEY_DEVNO   1
79
#define KEY_DEVNO   1
78
 
80
 
79
/**
81
/**
80
 * Spinlock protecting the hash table.
82
 * Spinlock protecting the kernel IRQ hash table.
81
 * This lock must be taken only when interrupts are disabled.
83
 * This lock must be taken only when interrupts are disabled.
82
 */
84
 */
83
SPINLOCK_INITIALIZE(irq_hash_table_lock);
85
static SPINLOCK_INITIALIZE(irq_kernel_hash_table_lock);
-
 
86
/** The kernel IRQ hash table. */
84
static hash_table_t irq_hash_table;
87
static hash_table_t irq_kernel_hash_table;
-
 
88
 
-
 
89
/**
-
 
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;
85
 
96
 
86
/**
97
/**
87
 * Hash table operations for cases when we know that
98
 * Hash table operations for cases when we know that
88
 * there will be collisions between different keys.
99
 * there will be collisions between different keys.
89
 */
100
 */
Line 109... Line 120...
109
    .hash = irq_lin_hash,
120
    .hash = irq_lin_hash,
110
    .compare = irq_lin_compare,
121
    .compare = irq_lin_compare,
111
    .remove_callback = NULL     /* not used */
122
    .remove_callback = NULL     /* not used */
112
};
123
};
113
 
124
 
-
 
125
/** Number of buckets in either of the hash tables. */
-
 
126
static count_t buckets;
-
 
127
 
114
/** Initialize IRQ subsystem.
128
/** Initialize IRQ subsystem.
115
 *
129
 *
116
 * @param inrs Numbers of unique IRQ numbers or INRs.
130
 * @param inrs Numbers of unique IRQ numbers or INRs.
117
 * @param chains Number of chains in the hash table.
131
 * @param chains Number of chains in the hash table.
118
 */
132
 */
119
void irq_init(count_t inrs, count_t chains)
133
void irq_init(count_t inrs, count_t chains)
120
{
134
{
-
 
135
    buckets = chains;
121
    /*
136
    /*
122
     * Be smart about the choice of the hash table operations.
137
     * Be smart about the choice of the hash table operations.
123
     * In cases in which inrs equals the requested number of
138
     * In cases in which inrs equals the requested number of
124
     * chains (i.e. where there is no collision between
139
     * chains (i.e. where there is no collision between
125
     * different keys), we can use optimized set of operations.
140
     * different keys), we can use optimized set of operations.
126
     */
141
     */
127
    if (inrs == chains)
142
    if (inrs == chains) {
-
 
143
        hash_table_create(&irq_uspace_hash_table, chains, 2,
-
 
144
            &irq_lin_ops);
128
        hash_table_create(&irq_hash_table, chains, 2, &irq_lin_ops);
145
        hash_table_create(&irq_kernel_hash_table, chains, 2,
-
 
146
            &irq_lin_ops);
129
    else
147
    } else {
-
 
148
        hash_table_create(&irq_uspace_hash_table, chains, 2,
-
 
149
            &irq_ht_ops);
130
        hash_table_create(&irq_hash_table, chains, 2, &irq_ht_ops);
150
        hash_table_create(&irq_kernel_hash_table, chains, 2,
-
 
151
            &irq_ht_ops);
-
 
152
    }
131
}
153
}
132
 
154
 
133
/** Initialize one IRQ structure.
155
/** Initialize one IRQ structure.
134
 *
156
 *
135
 * @param irq Pointer to the IRQ structure to be initialized.
157
 * @param irq Pointer to the IRQ structure to be initialized.
136
 *
158
 *
137
 */
159
 */
138
void irq_initialize(irq_t *irq)
160
void irq_initialize(irq_t *irq)
139
{
161
{
-
 
162
    memsetb(irq, 0, sizeof(irq_t));
140
    link_initialize(&irq->link);
163
    link_initialize(&irq->link);
141
    spinlock_initialize(&irq->lock, "irq.lock");
164
    spinlock_initialize(&irq->lock, "irq.lock");
142
    irq->preack = false;
165
    link_initialize(&irq->notif_cfg.link);
143
    irq->inr = -1;
166
    irq->inr = -1;
144
    irq->devno = -1;
167
    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
}
168
}
158
 
169
 
159
/** Register IRQ for device.
170
/** Register IRQ for device.
160
 *
171
 *
161
 * The irq structure must be filled with information
172
 * The irq structure must be filled with information
162
 * about the interrupt source and with the claim()
173
 * about the interrupt source and with the claim()
163
 * function pointer and irq_handler() function pointer.
174
 * function pointer and handler() function pointer.
164
 *
175
 *
165
 * @param irq IRQ structure belonging to a device.
176
 * @param irq       IRQ structure belonging to a device.
-
 
177
 * @return      True on success, false on failure.
166
 */
178
 */
167
void irq_register(irq_t *irq)
179
void irq_register(irq_t *irq)
168
{
180
{
-
 
181
    spinlock_t *lock = &irq_kernel_hash_table_lock;
-
 
182
    hash_table_t *table = &irq_kernel_hash_table;
169
    ipl_t ipl;
183
    ipl_t ipl;
170
    unative_t key[] = {
184
    unative_t key[] = {
171
        (unative_t) irq->inr,
185
        (unative_t) irq->inr,
172
        (unative_t) irq->devno
186
        (unative_t) irq->devno
173
    };
187
    };
174
   
188
   
175
    ipl = interrupts_disable();
189
    ipl = interrupts_disable();
-
 
190
    spinlock_lock(lock);
176
    spinlock_lock(&irq_hash_table_lock);
191
    spinlock_lock(&irq->lock);
177
    hash_table_insert(&irq_hash_table, key, &irq->link);
192
    hash_table_insert(table, key, &irq->link);
178
    spinlock_unlock(&irq_hash_table_lock);
193
    spinlock_unlock(&irq->lock);   
-
 
194
    spinlock_unlock(lock);
179
    interrupts_restore(ipl);
195
    interrupts_restore(ipl);
180
}
196
}
181
 
197
 
182
/** Dispatch the IRQ.
198
/** Dispatch the IRQ.
183
 *
199
 *
Line 199... Line 215...
199
    unative_t key[] = {
215
    unative_t key[] = {
200
        (unative_t) inr,
216
        (unative_t) inr,
201
        (unative_t) -1      /* search will use claim() instead of devno */
217
        (unative_t) -1      /* search will use claim() instead of devno */
202
    };
218
    };
203
   
219
   
-
 
220
    /*
-
 
221
     * Try uspace handlers first.
-
 
222
     */
204
    spinlock_lock(&irq_hash_table_lock);
223
    spinlock_lock(&irq_uspace_hash_table_lock);
205
 
-
 
206
    lnk = hash_table_find(&irq_hash_table, key);
224
    lnk = hash_table_find(&irq_uspace_hash_table, key);
207
    if (lnk) {
225
    if (lnk) {
208
        irq_t *irq;
226
        irq_t *irq;
209
       
227
       
210
        irq = hash_table_get_instance(lnk, irq_t, link);
228
        irq = hash_table_get_instance(lnk, irq_t, link);
211
 
-
 
212
        spinlock_unlock(&irq_hash_table_lock);
229
        spinlock_unlock(&irq_uspace_hash_table_lock);
213
        return irq;
230
        return irq;
214
    }
231
    }
215
   
-
 
216
    spinlock_unlock(&irq_hash_table_lock);
232
    spinlock_unlock(&irq_uspace_hash_table_lock);
217
 
-
 
218
    return NULL;   
-
 
219
}
-
 
220
 
233
 
221
/** Find the IRQ structure corresponding to inr and devno.
-
 
222
 *
234
    /*
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.
-
 
227
 *
-
 
228
 * This function assumes interrupts are already disabled.
-
 
229
 *
-
 
230
 * @param inr INR being looked up.
235
     * Fallback to kernel handlers.
231
 * @param devno Devno being looked up.
-
 
232
 *
-
 
233
 * @return Locked IRQ structure on success or NULL on failure.
-
 
234
 */
236
     */
235
irq_t *irq_find_and_lock(inr_t inr, devno_t devno)
-
 
236
{
-
 
237
    link_t *lnk;
-
 
238
    unative_t keys[] = {
-
 
239
        (unative_t) inr,
-
 
240
        (unative_t) devno
-
 
241
    };
-
 
242
   
-
 
243
    spinlock_lock(&irq_hash_table_lock);
237
    spinlock_lock(&irq_kernel_hash_table_lock);
244
 
-
 
245
    lnk = hash_table_find(&irq_hash_table, keys);
238
    lnk = hash_table_find(&irq_kernel_hash_table, key);
246
    if (lnk) {
239
    if (lnk) {
247
        irq_t *irq;
240
        irq_t *irq;
248
       
241
       
249
        irq = hash_table_get_instance(lnk, irq_t, link);
242
        irq = hash_table_get_instance(lnk, irq_t, link);
250
 
-
 
251
        spinlock_unlock(&irq_hash_table_lock);
243
        spinlock_unlock(&irq_kernel_hash_table_lock);
252
        return irq;
244
        return irq;
253
    }
245
    }
254
   
-
 
255
    spinlock_unlock(&irq_hash_table_lock);
246
    spinlock_unlock(&irq_kernel_hash_table_lock);
256
 
247
 
257
    return NULL;   
248
    return NULL;   
258
}
249
}
259
 
250
 
260
/** Compute hash index for the key.
251
/** Compute hash index for the key.
Line 271... Line 262...
271
 * @return Index into the hash table.
262
 * @return Index into the hash table.
272
 */
263
 */
273
index_t irq_ht_hash(unative_t key[])
264
index_t irq_ht_hash(unative_t key[])
274
{
265
{
275
    inr_t inr = (inr_t) key[KEY_INR];
266
    inr_t inr = (inr_t) key[KEY_INR];
276
    return inr % irq_hash_table.entries;
267
    return inr % buckets;
277
}
268
}
278
 
269
 
279
/** Compare hash table element with a key.
270
/** Compare hash table element with a key.
280
 *
271
 *
281
 * There are two things to note about this function.
272
 * There are two things to note about this function.
Line 306... Line 297...
306
   
297
   
307
    spinlock_lock(&irq->lock);
298
    spinlock_lock(&irq->lock);
308
    if (devno == -1) {
299
    if (devno == -1) {
309
        /* Invoked by irq_dispatch_and_lock(). */
300
        /* Invoked by irq_dispatch_and_lock(). */
310
        rv = ((irq->inr == inr) &&
301
        rv = ((irq->inr == inr) &&
311
            (irq->claim(irq->instance) == IRQ_ACCEPT));
302
            (irq->claim(irq) == IRQ_ACCEPT));
312
    } else {
303
    } else {
313
        /* Invoked by irq_find_and_lock(). */
304
        /* Invoked by irq_find_and_lock(). */
314
        rv = ((irq->inr == inr) && (irq->devno == devno));
305
        rv = ((irq->inr == inr) && (irq->devno == devno));
315
    }
306
    }
316
   
307
   
Line 365... Line 356...
365
    bool rv;
356
    bool rv;
366
   
357
   
367
    spinlock_lock(&irq->lock);
358
    spinlock_lock(&irq->lock);
368
    if (devno == -1) {
359
    if (devno == -1) {
369
        /* Invoked by irq_dispatch_and_lock() */
360
        /* Invoked by irq_dispatch_and_lock() */
370
        rv = (irq->claim(irq->instance) == IRQ_ACCEPT);
361
        rv = (irq->claim(irq) == IRQ_ACCEPT);
371
    } else {
362
    } else {
372
        /* Invoked by irq_find_and_lock() */
363
        /* Invoked by irq_find_and_lock() */
373
        rv = (irq->devno == devno);
364
        rv = (irq->devno == devno);
374
    }
365
    }
375
   
366