Subversion Repositories HelenOS

Rev

Rev 4274 | 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>
3964 decky 75
#include <console/console.h>
3947 jermar 76
#include <memstr.h>
1919 jermar 77
#include <arch.h>
78
 
1922 jermar 79
#define KEY_INR		0
80
#define KEY_DEVNO	1
81
 
1919 jermar 82
/**
3947 jermar 83
 * Spinlock protecting the kernel IRQ hash table.
1919 jermar 84
 * This lock must be taken only when interrupts are disabled.
85
 */
3950 jermar 86
SPINLOCK_INITIALIZE(irq_kernel_hash_table_lock);
3947 jermar 87
/** The kernel IRQ hash table. */
88
static hash_table_t irq_kernel_hash_table;
1919 jermar 89
 
90
/**
3947 jermar 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;
97
 
98
/**
1919 jermar 99
 * Hash table operations for cases when we know that
100
 * there will be collisions between different keys.
101
 */
4490 decky 102
static size_t irq_ht_hash(unative_t *key);
103
static bool irq_ht_compare(unative_t *key, size_t keys, link_t *item);
4254 jermar 104
static void irq_ht_remove(link_t *item);
1919 jermar 105
 
106
static hash_table_operations_t irq_ht_ops = {
107
	.hash = irq_ht_hash,
108
	.compare = irq_ht_compare,
4254 jermar 109
	.remove_callback = irq_ht_remove,
1919 jermar 110
};
111
 
112
/**
113
 * Hash table operations for cases when we know that
114
 * there will be no collisions between different keys.
115
 * However, there might be still collisions among
116
 * elements with single key (sharing of one IRQ).
117
 */
4490 decky 118
static size_t irq_lin_hash(unative_t *key);
119
static bool irq_lin_compare(unative_t *key, size_t keys, link_t *item);
4254 jermar 120
static void irq_lin_remove(link_t *item);
1919 jermar 121
 
122
static hash_table_operations_t irq_lin_ops = {
123
	.hash = irq_lin_hash,
124
	.compare = irq_lin_compare,
4254 jermar 125
	.remove_callback = irq_lin_remove,
1919 jermar 126
};
127
 
3947 jermar 128
/** Number of buckets in either of the hash tables. */
4490 decky 129
static size_t buckets;
3947 jermar 130
 
1919 jermar 131
/** Initialize IRQ subsystem.
132
 *
133
 * @param inrs Numbers of unique IRQ numbers or INRs.
134
 * @param chains Number of chains in the hash table.
135
 */
4490 decky 136
void irq_init(size_t inrs, size_t chains)
1919 jermar 137
{
3947 jermar 138
	buckets = chains;
1919 jermar 139
	/*
140
	 * Be smart about the choice of the hash table operations.
141
	 * In cases in which inrs equals the requested number of
142
	 * chains (i.e. where there is no collision between
143
	 * different keys), we can use optimized set of operations.
144
	 */
3947 jermar 145
	if (inrs == chains) {
146
		hash_table_create(&irq_uspace_hash_table, chains, 2,
147
		    &irq_lin_ops);
148
		hash_table_create(&irq_kernel_hash_table, chains, 2,
149
		    &irq_lin_ops);
150
	} else {
151
		hash_table_create(&irq_uspace_hash_table, chains, 2,
152
		    &irq_ht_ops);
153
		hash_table_create(&irq_kernel_hash_table, chains, 2,
154
		    &irq_ht_ops);
155
	}
1919 jermar 156
}
157
 
158
/** Initialize one IRQ structure.
159
 *
160
 * @param irq Pointer to the IRQ structure to be initialized.
161
 *
162
 */
163
void irq_initialize(irq_t *irq)
164
{
3968 jermar 165
	memsetb(irq, sizeof(irq_t), 0);
1919 jermar 166
	link_initialize(&irq->link);
1921 jermar 167
	spinlock_initialize(&irq->lock, "irq.lock");
3947 jermar 168
	link_initialize(&irq->notif_cfg.link);
1919 jermar 169
	irq->inr = -1;
170
	irq->devno = -1;
171
}
172
 
173
/** Register IRQ for device.
174
 *
175
 * The irq structure must be filled with information
176
 * about the interrupt source and with the claim()
3947 jermar 177
 * function pointer and handler() function pointer.
1919 jermar 178
 *
3947 jermar 179
 * @param irq		IRQ structure belonging to a device.
180
 * @return		True on success, false on failure.
1919 jermar 181
 */
182
void irq_register(irq_t *irq)
183
{
184
	ipl_t ipl;
1922 jermar 185
	unative_t key[] = {
186
		(unative_t) irq->inr,
187
		(unative_t) irq->devno
188
	};
1919 jermar 189
 
190
	ipl = interrupts_disable();
3950 jermar 191
	spinlock_lock(&irq_kernel_hash_table_lock);
3947 jermar 192
	spinlock_lock(&irq->lock);
3950 jermar 193
	hash_table_insert(&irq_kernel_hash_table, key, &irq->link);
3947 jermar 194
	spinlock_unlock(&irq->lock);	
3950 jermar 195
	spinlock_unlock(&irq_kernel_hash_table_lock);
1919 jermar 196
	interrupts_restore(ipl);
197
}
198
 
3964 decky 199
/** Search and lock the uspace IRQ hash table.
1919 jermar 200
 *
201
 */
3964 decky 202
static irq_t *irq_dispatch_and_lock_uspace(inr_t inr)
1919 jermar 203
{
204
	link_t *lnk;
1922 jermar 205
	unative_t key[] = {
206
		(unative_t) inr,
3964 decky 207
		(unative_t) -1    /* search will use claim() instead of devno */
1922 jermar 208
	};
1919 jermar 209
 
3947 jermar 210
	spinlock_lock(&irq_uspace_hash_table_lock);
211
	lnk = hash_table_find(&irq_uspace_hash_table, key);
1919 jermar 212
	if (lnk) {
213
		irq_t *irq;
214
 
215
		irq = hash_table_get_instance(lnk, irq_t, link);
3947 jermar 216
		spinlock_unlock(&irq_uspace_hash_table_lock);
1919 jermar 217
		return irq;
218
	}
3947 jermar 219
	spinlock_unlock(&irq_uspace_hash_table_lock);
3964 decky 220
 
221
	return NULL;
222
}
1919 jermar 223
 
3964 decky 224
/** Search and lock the kernel IRQ hash table.
225
 *
226
 */
227
static irq_t *irq_dispatch_and_lock_kernel(inr_t inr)
228
{
229
	link_t *lnk;
230
	unative_t key[] = {
231
		(unative_t) inr,
232
		(unative_t) -1    /* search will use claim() instead of devno */
233
	};
234
 
3947 jermar 235
	spinlock_lock(&irq_kernel_hash_table_lock);
236
	lnk = hash_table_find(&irq_kernel_hash_table, key);
1922 jermar 237
	if (lnk) {
238
		irq_t *irq;
239
 
240
		irq = hash_table_get_instance(lnk, irq_t, link);
3947 jermar 241
		spinlock_unlock(&irq_kernel_hash_table_lock);
1922 jermar 242
		return irq;
243
	}
3947 jermar 244
	spinlock_unlock(&irq_kernel_hash_table_lock);
3964 decky 245
 
246
	return NULL;
247
}
1922 jermar 248
 
3964 decky 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);
1922 jermar 286
}
287
 
1919 jermar 288
/** Compute hash index for the key.
289
 *
290
 * This function computes hash index into
291
 * the IRQ hash table for which there
292
 * can be collisions between different
293
 * INRs.
294
 *
1922 jermar 295
 * The devno is not used to compute the hash.
1919 jermar 296
 *
1922 jermar 297
 * @param key The first of the keys is inr and the second is devno or -1.
298
 *
1919 jermar 299
 * @return Index into the hash table.
300
 */
4490 decky 301
size_t irq_ht_hash(unative_t key[])
1919 jermar 302
{
1922 jermar 303
	inr_t inr = (inr_t) key[KEY_INR];
3947 jermar 304
	return inr % buckets;
1919 jermar 305
}
306
 
307
/** Compare hash table element with a key.
308
 *
1922 jermar 309
 * There are two things to note about this function.
310
 * First, it is used for the more complex architecture setup
311
 * in which there are way too many interrupt numbers (i.e. inr's)
312
 * to arrange the hash table so that collisions occur only
313
 * among same inrs of different devnos. So the explicit check
314
 * for inr match must be done.
315
 * Second, if devno is -1, the second key (i.e. devno) is not
316
 * used for the match and the result of the claim() function
317
 * is used instead.
1919 jermar 318
 *
1922 jermar 319
 * This function assumes interrupts are already disabled.
320
 *
321
 * @param key Keys (i.e. inr and devno).
322
 * @param keys This is 2.
1919 jermar 323
 * @param item The item to compare the key with.
324
 *
325
 * @return True on match or false otherwise.
326
 */
4490 decky 327
bool irq_ht_compare(unative_t key[], size_t keys, link_t *item)
1919 jermar 328
{
329
	irq_t *irq = hash_table_get_instance(item, irq_t, link);
1922 jermar 330
	inr_t inr = (inr_t) key[KEY_INR];
331
	devno_t devno = (devno_t) key[KEY_DEVNO];
332
 
1921 jermar 333
	bool rv;
1919 jermar 334
 
1921 jermar 335
	spinlock_lock(&irq->lock);
1922 jermar 336
	if (devno == -1) {
2107 jermar 337
		/* Invoked by irq_dispatch_and_lock(). */
3906 jermar 338
		rv = ((irq->inr == inr) &&
3947 jermar 339
		    (irq->claim(irq) == IRQ_ACCEPT));
1922 jermar 340
	} else {
2107 jermar 341
		/* Invoked by irq_find_and_lock(). */
1922 jermar 342
		rv = ((irq->inr == inr) && (irq->devno == devno));
343
	}
344
 
345
	/* unlock only on non-match */
346
	if (!rv)
347
		spinlock_unlock(&irq->lock);
1921 jermar 348
 
349
	return rv;
1919 jermar 350
}
351
 
4254 jermar 352
/** Unlock IRQ structure after hash_table_remove().
353
 *
4274 decky 354
 * @param lnk Link in the removed and locked IRQ structure.
4254 jermar 355
 */
356
void irq_ht_remove(link_t *lnk)
357
{
4274 decky 358
	irq_t *irq __attribute__((unused))
359
	    = hash_table_get_instance(lnk, irq_t, link);
4254 jermar 360
	spinlock_unlock(&irq->lock);
361
}
362
 
1919 jermar 363
/** Compute hash index for the key.
364
 *
365
 * This function computes hash index into
366
 * the IRQ hash table for which there
367
 * are no collisions between different
368
 * INRs.
369
 *
1922 jermar 370
 * @param key The first of the keys is inr and the second is devno or -1.
1919 jermar 371
 *
372
 * @return Index into the hash table.
373
 */
4490 decky 374
size_t irq_lin_hash(unative_t key[])
1919 jermar 375
{
1922 jermar 376
	inr_t inr = (inr_t) key[KEY_INR];
377
	return inr;
1919 jermar 378
}
379
 
380
/** Compare hash table element with a key.
381
 *
1922 jermar 382
 * There are two things to note about this function.
383
 * First, it is used for the less complex architecture setup
384
 * in which there are not too many interrupt numbers (i.e. inr's)
385
 * to arrange the hash table so that collisions occur only
386
 * among same inrs of different devnos. So the explicit check
387
 * for inr match is not done.
388
 * Second, if devno is -1, the second key (i.e. devno) is not
389
 * used for the match and the result of the claim() function
390
 * is used instead.
1919 jermar 391
 *
1922 jermar 392
 * This function assumes interrupts are already disabled.
393
 *
394
 * @param key Keys (i.e. inr and devno).
395
 * @param keys This is 2.
1919 jermar 396
 * @param item The item to compare the key with.
397
 *
398
 * @return True on match or false otherwise.
399
 */
4490 decky 400
bool irq_lin_compare(unative_t key[], size_t keys, link_t *item)
1919 jermar 401
{
402
	irq_t *irq = list_get_instance(item, irq_t, link);
1922 jermar 403
	devno_t devno = (devno_t) key[KEY_DEVNO];
1921 jermar 404
	bool rv;
1919 jermar 405
 
1921 jermar 406
	spinlock_lock(&irq->lock);
1922 jermar 407
	if (devno == -1) {
2107 jermar 408
		/* Invoked by irq_dispatch_and_lock() */
3947 jermar 409
		rv = (irq->claim(irq) == IRQ_ACCEPT);
1922 jermar 410
	} else {
2107 jermar 411
		/* Invoked by irq_find_and_lock() */
1922 jermar 412
		rv = (irq->devno == devno);
413
	}
1921 jermar 414
 
1922 jermar 415
	/* unlock only on non-match */
416
	if (!rv)
417
		spinlock_unlock(&irq->lock);
418
 
1921 jermar 419
	return rv;
1919 jermar 420
}
421
 
4254 jermar 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
{
4274 decky 428
	irq_t *irq __attribute__((unused))
429
	    = hash_table_get_instance(lnk, irq_t, link);
4254 jermar 430
	spinlock_unlock(&irq->lock);
431
}
432
 
1919 jermar 433
/** @}
434
 */