genirq: cleanup the sparseirq modifications
[linux-2.6/mini2440.git] / kernel / irq / chip.c
blob4895fde4eb933c25fe50fbbfabc3fb0a6cd9c742
1 /*
2 * linux/kernel/irq/chip.c
4 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
5 * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
7 * This file contains the core interrupt handling code, for irq-chip
8 * based architectures.
10 * Detailed information is available in Documentation/DocBook/genericirq
13 #include <linux/irq.h>
14 #include <linux/msi.h>
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel_stat.h>
19 #include "internals.h"
21 /**
22 * dynamic_irq_init - initialize a dynamically allocated irq
23 * @irq: irq number to initialize
25 void dynamic_irq_init(unsigned int irq)
27 struct irq_desc *desc = irq_to_desc(irq);
28 unsigned long flags;
30 if (!desc) {
31 WARN(1, KERN_ERR "Trying to initialize invalid IRQ%d\n", irq);
32 return;
35 /* Ensure we don't have left over values from a previous use of this irq */
36 spin_lock_irqsave(&desc->lock, flags);
37 desc->status = IRQ_DISABLED;
38 desc->chip = &no_irq_chip;
39 desc->handle_irq = handle_bad_irq;
40 desc->depth = 1;
41 desc->msi_desc = NULL;
42 desc->handler_data = NULL;
43 desc->chip_data = NULL;
44 desc->action = NULL;
45 desc->irq_count = 0;
46 desc->irqs_unhandled = 0;
47 #ifdef CONFIG_SMP
48 cpus_setall(desc->affinity);
49 #endif
50 spin_unlock_irqrestore(&desc->lock, flags);
53 /**
54 * dynamic_irq_cleanup - cleanup a dynamically allocated irq
55 * @irq: irq number to initialize
57 void dynamic_irq_cleanup(unsigned int irq)
59 struct irq_desc *desc = irq_to_desc(irq);
60 unsigned long flags;
62 if (!desc) {
63 WARN(1, KERN_ERR "Trying to cleanup invalid IRQ%d\n", irq);
64 return;
67 spin_lock_irqsave(&desc->lock, flags);
68 if (desc->action) {
69 spin_unlock_irqrestore(&desc->lock, flags);
70 WARN(1, KERN_ERR "Destroying IRQ%d without calling free_irq\n",
71 irq);
72 return;
74 desc->msi_desc = NULL;
75 desc->handler_data = NULL;
76 desc->chip_data = NULL;
77 desc->handle_irq = handle_bad_irq;
78 desc->chip = &no_irq_chip;
79 spin_unlock_irqrestore(&desc->lock, flags);
83 /**
84 * set_irq_chip - set the irq chip for an irq
85 * @irq: irq number
86 * @chip: pointer to irq chip description structure
88 int set_irq_chip(unsigned int irq, struct irq_chip *chip)
90 struct irq_desc *desc = irq_to_desc(irq);
91 unsigned long flags;
93 if (!desc) {
94 WARN(1, KERN_ERR "Trying to install chip for IRQ%d\n", irq);
95 return -EINVAL;
98 if (!chip)
99 chip = &no_irq_chip;
101 spin_lock_irqsave(&desc->lock, flags);
102 irq_chip_set_defaults(chip);
103 desc->chip = chip;
104 spin_unlock_irqrestore(&desc->lock, flags);
106 return 0;
108 EXPORT_SYMBOL(set_irq_chip);
111 * set_irq_type - set the irq trigger type for an irq
112 * @irq: irq number
113 * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
115 int set_irq_type(unsigned int irq, unsigned int type)
117 struct irq_desc *desc = irq_to_desc(irq);
118 unsigned long flags;
119 int ret = -ENXIO;
121 if (!desc) {
122 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
123 return -ENODEV;
126 if (type == IRQ_TYPE_NONE)
127 return 0;
129 spin_lock_irqsave(&desc->lock, flags);
130 ret = __irq_set_trigger(desc, irq, flags);
131 spin_unlock_irqrestore(&desc->lock, flags);
132 return ret;
134 EXPORT_SYMBOL(set_irq_type);
137 * set_irq_data - set irq type data for an irq
138 * @irq: Interrupt number
139 * @data: Pointer to interrupt specific data
141 * Set the hardware irq controller data for an irq
143 int set_irq_data(unsigned int irq, void *data)
145 struct irq_desc *desc = irq_to_desc(irq);
146 unsigned long flags;
148 if (!desc) {
149 printk(KERN_ERR
150 "Trying to install controller data for IRQ%d\n", irq);
151 return -EINVAL;
154 spin_lock_irqsave(&desc->lock, flags);
155 desc->handler_data = data;
156 spin_unlock_irqrestore(&desc->lock, flags);
157 return 0;
159 EXPORT_SYMBOL(set_irq_data);
162 * set_irq_data - set irq type data for an irq
163 * @irq: Interrupt number
164 * @entry: Pointer to MSI descriptor data
166 * Set the hardware irq controller data for an irq
168 int set_irq_msi(unsigned int irq, struct msi_desc *entry)
170 struct irq_desc *desc = irq_to_desc(irq);
171 unsigned long flags;
173 if (!desc) {
174 printk(KERN_ERR
175 "Trying to install msi data for IRQ%d\n", irq);
176 return -EINVAL;
179 spin_lock_irqsave(&desc->lock, flags);
180 desc->msi_desc = entry;
181 if (entry)
182 entry->irq = irq;
183 spin_unlock_irqrestore(&desc->lock, flags);
184 return 0;
188 * set_irq_chip_data - set irq chip data for an irq
189 * @irq: Interrupt number
190 * @data: Pointer to chip specific data
192 * Set the hardware irq chip data for an irq
194 int set_irq_chip_data(unsigned int irq, void *data)
196 struct irq_desc *desc = irq_to_desc(irq);
197 unsigned long flags;
199 if (!desc) {
200 printk(KERN_ERR
201 "Trying to install chip data for IRQ%d\n", irq);
202 return -EINVAL;
205 if (!desc->chip) {
206 printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
207 return -EINVAL;
210 spin_lock_irqsave(&desc->lock, flags);
211 desc->chip_data = data;
212 spin_unlock_irqrestore(&desc->lock, flags);
214 return 0;
216 EXPORT_SYMBOL(set_irq_chip_data);
219 * default enable function
221 static void default_enable(unsigned int irq)
223 struct irq_desc *desc = irq_to_desc(irq);
225 desc->chip->unmask(irq);
226 desc->status &= ~IRQ_MASKED;
230 * default disable function
232 static void default_disable(unsigned int irq)
237 * default startup function
239 static unsigned int default_startup(unsigned int irq)
241 struct irq_desc *desc = irq_to_desc(irq);
243 desc->chip->enable(irq);
244 return 0;
248 * default shutdown function
250 static void default_shutdown(unsigned int irq)
252 struct irq_desc *desc = irq_to_desc(irq);
254 desc->chip->mask(irq);
255 desc->status |= IRQ_MASKED;
259 * Fixup enable/disable function pointers
261 void irq_chip_set_defaults(struct irq_chip *chip)
263 if (!chip->enable)
264 chip->enable = default_enable;
265 if (!chip->disable)
266 chip->disable = default_disable;
267 if (!chip->startup)
268 chip->startup = default_startup;
270 * We use chip->disable, when the user provided its own. When
271 * we have default_disable set for chip->disable, then we need
272 * to use default_shutdown, otherwise the irq line is not
273 * disabled on free_irq():
275 if (!chip->shutdown)
276 chip->shutdown = chip->disable != default_disable ?
277 chip->disable : default_shutdown;
278 if (!chip->name)
279 chip->name = chip->typename;
280 if (!chip->end)
281 chip->end = dummy_irq_chip.end;
284 static inline void mask_ack_irq(struct irq_desc *desc, int irq)
286 if (desc->chip->mask_ack)
287 desc->chip->mask_ack(irq);
288 else {
289 desc->chip->mask(irq);
290 desc->chip->ack(irq);
295 * handle_simple_irq - Simple and software-decoded IRQs.
296 * @irq: the interrupt number
297 * @desc: the interrupt description structure for this irq
299 * Simple interrupts are either sent from a demultiplexing interrupt
300 * handler or come from hardware, where no interrupt hardware control
301 * is necessary.
303 * Note: The caller is expected to handle the ack, clear, mask and
304 * unmask issues if necessary.
306 void
307 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
309 struct irqaction *action;
310 irqreturn_t action_ret;
312 spin_lock(&desc->lock);
314 if (unlikely(desc->status & IRQ_INPROGRESS))
315 goto out_unlock;
316 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
317 kstat_incr_irqs_this_cpu(irq, desc);
319 action = desc->action;
320 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
321 goto out_unlock;
323 desc->status |= IRQ_INPROGRESS;
324 spin_unlock(&desc->lock);
326 action_ret = handle_IRQ_event(irq, action);
327 if (!noirqdebug)
328 note_interrupt(irq, desc, action_ret);
330 spin_lock(&desc->lock);
331 desc->status &= ~IRQ_INPROGRESS;
332 out_unlock:
333 spin_unlock(&desc->lock);
337 * handle_level_irq - Level type irq handler
338 * @irq: the interrupt number
339 * @desc: the interrupt description structure for this irq
341 * Level type interrupts are active as long as the hardware line has
342 * the active level. This may require to mask the interrupt and unmask
343 * it after the associated handler has acknowledged the device, so the
344 * interrupt line is back to inactive.
346 void
347 handle_level_irq(unsigned int irq, struct irq_desc *desc)
349 struct irqaction *action;
350 irqreturn_t action_ret;
352 spin_lock(&desc->lock);
353 mask_ack_irq(desc, irq);
355 if (unlikely(desc->status & IRQ_INPROGRESS))
356 goto out_unlock;
357 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
358 kstat_incr_irqs_this_cpu(irq, desc);
361 * If its disabled or no action available
362 * keep it masked and get out of here
364 action = desc->action;
365 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
366 goto out_unlock;
368 desc->status |= IRQ_INPROGRESS;
369 spin_unlock(&desc->lock);
371 action_ret = handle_IRQ_event(irq, action);
372 if (!noirqdebug)
373 note_interrupt(irq, desc, action_ret);
375 spin_lock(&desc->lock);
376 desc->status &= ~IRQ_INPROGRESS;
377 if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
378 desc->chip->unmask(irq);
379 out_unlock:
380 spin_unlock(&desc->lock);
384 * handle_fasteoi_irq - irq handler for transparent controllers
385 * @irq: the interrupt number
386 * @desc: the interrupt description structure for this irq
388 * Only a single callback will be issued to the chip: an ->eoi()
389 * call when the interrupt has been serviced. This enables support
390 * for modern forms of interrupt handlers, which handle the flow
391 * details in hardware, transparently.
393 void
394 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
396 struct irqaction *action;
397 irqreturn_t action_ret;
399 spin_lock(&desc->lock);
401 if (unlikely(desc->status & IRQ_INPROGRESS))
402 goto out;
404 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
405 kstat_incr_irqs_this_cpu(irq, desc);
408 * If its disabled or no action available
409 * then mask it and get out of here:
411 action = desc->action;
412 if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
413 desc->status |= IRQ_PENDING;
414 if (desc->chip->mask)
415 desc->chip->mask(irq);
416 goto out;
419 desc->status |= IRQ_INPROGRESS;
420 desc->status &= ~IRQ_PENDING;
421 spin_unlock(&desc->lock);
423 action_ret = handle_IRQ_event(irq, action);
424 if (!noirqdebug)
425 note_interrupt(irq, desc, action_ret);
427 spin_lock(&desc->lock);
428 desc->status &= ~IRQ_INPROGRESS;
429 out:
430 desc->chip->eoi(irq);
432 spin_unlock(&desc->lock);
436 * handle_edge_irq - edge type IRQ handler
437 * @irq: the interrupt number
438 * @desc: the interrupt description structure for this irq
440 * Interrupt occures on the falling and/or rising edge of a hardware
441 * signal. The occurence is latched into the irq controller hardware
442 * and must be acked in order to be reenabled. After the ack another
443 * interrupt can happen on the same source even before the first one
444 * is handled by the assosiacted event handler. If this happens it
445 * might be necessary to disable (mask) the interrupt depending on the
446 * controller hardware. This requires to reenable the interrupt inside
447 * of the loop which handles the interrupts which have arrived while
448 * the handler was running. If all pending interrupts are handled, the
449 * loop is left.
451 void
452 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
454 spin_lock(&desc->lock);
456 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
459 * If we're currently running this IRQ, or its disabled,
460 * we shouldn't process the IRQ. Mark it pending, handle
461 * the necessary masking and go out
463 if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
464 !desc->action)) {
465 desc->status |= (IRQ_PENDING | IRQ_MASKED);
466 mask_ack_irq(desc, irq);
467 goto out_unlock;
469 kstat_incr_irqs_this_cpu(irq, desc);
471 /* Start handling the irq */
472 desc->chip->ack(irq);
474 /* Mark the IRQ currently in progress.*/
475 desc->status |= IRQ_INPROGRESS;
477 do {
478 struct irqaction *action = desc->action;
479 irqreturn_t action_ret;
481 if (unlikely(!action)) {
482 desc->chip->mask(irq);
483 goto out_unlock;
487 * When another irq arrived while we were handling
488 * one, we could have masked the irq.
489 * Renable it, if it was not disabled in meantime.
491 if (unlikely((desc->status &
492 (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
493 (IRQ_PENDING | IRQ_MASKED))) {
494 desc->chip->unmask(irq);
495 desc->status &= ~IRQ_MASKED;
498 desc->status &= ~IRQ_PENDING;
499 spin_unlock(&desc->lock);
500 action_ret = handle_IRQ_event(irq, action);
501 if (!noirqdebug)
502 note_interrupt(irq, desc, action_ret);
503 spin_lock(&desc->lock);
505 } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
507 desc->status &= ~IRQ_INPROGRESS;
508 out_unlock:
509 spin_unlock(&desc->lock);
513 * handle_percpu_IRQ - Per CPU local irq handler
514 * @irq: the interrupt number
515 * @desc: the interrupt description structure for this irq
517 * Per CPU interrupts on SMP machines without locking requirements
519 void
520 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
522 irqreturn_t action_ret;
524 kstat_incr_irqs_this_cpu(irq, desc);
526 if (desc->chip->ack)
527 desc->chip->ack(irq);
529 action_ret = handle_IRQ_event(irq, desc->action);
530 if (!noirqdebug)
531 note_interrupt(irq, desc, action_ret);
533 if (desc->chip->eoi)
534 desc->chip->eoi(irq);
537 void
538 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
539 const char *name)
541 struct irq_desc *desc = irq_to_desc(irq);
542 unsigned long flags;
544 if (!desc) {
545 printk(KERN_ERR
546 "Trying to install type control for IRQ%d\n", irq);
547 return;
550 if (!handle)
551 handle = handle_bad_irq;
552 else if (desc->chip == &no_irq_chip) {
553 printk(KERN_WARNING "Trying to install %sinterrupt handler "
554 "for IRQ%d\n", is_chained ? "chained " : "", irq);
556 * Some ARM implementations install a handler for really dumb
557 * interrupt hardware without setting an irq_chip. This worked
558 * with the ARM no_irq_chip but the check in setup_irq would
559 * prevent us to setup the interrupt at all. Switch it to
560 * dummy_irq_chip for easy transition.
562 desc->chip = &dummy_irq_chip;
565 spin_lock_irqsave(&desc->lock, flags);
567 /* Uninstall? */
568 if (handle == handle_bad_irq) {
569 if (desc->chip != &no_irq_chip)
570 mask_ack_irq(desc, irq);
571 desc->status |= IRQ_DISABLED;
572 desc->depth = 1;
574 desc->handle_irq = handle;
575 desc->name = name;
577 if (handle != handle_bad_irq && is_chained) {
578 desc->status &= ~IRQ_DISABLED;
579 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
580 desc->depth = 0;
581 desc->chip->startup(irq);
583 spin_unlock_irqrestore(&desc->lock, flags);
586 void
587 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
588 irq_flow_handler_t handle)
590 set_irq_chip(irq, chip);
591 __set_irq_handler(irq, handle, 0, NULL);
594 void
595 set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
596 irq_flow_handler_t handle, const char *name)
598 set_irq_chip(irq, chip);
599 __set_irq_handler(irq, handle, 0, name);
602 void __init set_irq_noprobe(unsigned int irq)
604 struct irq_desc *desc = irq_to_desc(irq);
605 unsigned long flags;
607 if (!desc) {
608 printk(KERN_ERR "Trying to mark IRQ%d non-probeable\n", irq);
609 return;
612 spin_lock_irqsave(&desc->lock, flags);
613 desc->status |= IRQ_NOPROBE;
614 spin_unlock_irqrestore(&desc->lock, flags);
617 void __init set_irq_probe(unsigned int irq)
619 struct irq_desc *desc = irq_to_desc(irq);
620 unsigned long flags;
622 if (!desc) {
623 printk(KERN_ERR "Trying to mark IRQ%d probeable\n", irq);
624 return;
627 spin_lock_irqsave(&desc->lock, flags);
628 desc->status &= ~IRQ_NOPROBE;
629 spin_unlock_irqrestore(&desc->lock, flags);