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
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"
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
;
30 /* first time to use this irq_desc */
31 desc
= irq_to_desc_alloc(irq
);
33 WARN(1, KERN_ERR
"Trying to initialize invalid IRQ%d\n", irq
);
37 /* Ensure we don't have left over values from a previous use of this irq */
38 spin_lock_irqsave(&desc
->lock
, flags
);
39 desc
->status
= IRQ_DISABLED
;
40 desc
->chip
= &no_irq_chip
;
41 desc
->handle_irq
= handle_bad_irq
;
43 desc
->msi_desc
= NULL
;
44 desc
->handler_data
= NULL
;
45 desc
->chip_data
= NULL
;
48 desc
->irqs_unhandled
= 0;
50 cpus_setall(desc
->affinity
);
52 spin_unlock_irqrestore(&desc
->lock
, flags
);
56 * dynamic_irq_cleanup - cleanup a dynamically allocated irq
57 * @irq: irq number to initialize
59 void dynamic_irq_cleanup(unsigned int irq
)
61 struct irq_desc
*desc
;
64 desc
= irq_to_desc(irq
);
66 WARN(1, KERN_ERR
"Trying to cleanup invalid IRQ%d\n", irq
);
70 spin_lock_irqsave(&desc
->lock
, flags
);
72 spin_unlock_irqrestore(&desc
->lock
, flags
);
73 WARN(1, KERN_ERR
"Destroying IRQ%d without calling free_irq\n",
77 desc
->msi_desc
= NULL
;
78 desc
->handler_data
= NULL
;
79 desc
->chip_data
= NULL
;
80 desc
->handle_irq
= handle_bad_irq
;
81 desc
->chip
= &no_irq_chip
;
82 spin_unlock_irqrestore(&desc
->lock
, flags
);
87 * set_irq_chip - set the irq chip for an irq
89 * @chip: pointer to irq chip description structure
91 int set_irq_chip(unsigned int irq
, struct irq_chip
*chip
)
93 struct irq_desc
*desc
;
96 desc
= irq_to_desc(irq
);
98 WARN(1, KERN_ERR
"Trying to install chip for IRQ%d\n", irq
);
105 desc
= irq_to_desc(irq
);
106 spin_lock_irqsave(&desc
->lock
, flags
);
107 irq_chip_set_defaults(chip
);
109 spin_unlock_irqrestore(&desc
->lock
, flags
);
113 EXPORT_SYMBOL(set_irq_chip
);
116 * set_irq_type - set the irq trigger type for an irq
118 * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
120 int set_irq_type(unsigned int irq
, unsigned int type
)
122 struct irq_desc
*desc
;
126 desc
= irq_to_desc(irq
);
128 printk(KERN_ERR
"Trying to set irq type for IRQ%d\n", irq
);
132 if (type
== IRQ_TYPE_NONE
)
135 spin_lock_irqsave(&desc
->lock
, flags
);
136 ret
= __irq_set_trigger(desc
, irq
, flags
);
137 spin_unlock_irqrestore(&desc
->lock
, flags
);
140 EXPORT_SYMBOL(set_irq_type
);
143 * set_irq_data - set irq type data for an irq
144 * @irq: Interrupt number
145 * @data: Pointer to interrupt specific data
147 * Set the hardware irq controller data for an irq
149 int set_irq_data(unsigned int irq
, void *data
)
151 struct irq_desc
*desc
;
154 desc
= irq_to_desc(irq
);
157 "Trying to install controller data for IRQ%d\n", irq
);
161 spin_lock_irqsave(&desc
->lock
, flags
);
162 desc
->handler_data
= data
;
163 spin_unlock_irqrestore(&desc
->lock
, flags
);
166 EXPORT_SYMBOL(set_irq_data
);
169 * set_irq_data - set irq type data for an irq
170 * @irq: Interrupt number
171 * @entry: Pointer to MSI descriptor data
173 * Set the hardware irq controller data for an irq
175 int set_irq_msi(unsigned int irq
, struct msi_desc
*entry
)
177 struct irq_desc
*desc
;
180 desc
= irq_to_desc(irq
);
183 "Trying to install msi data for IRQ%d\n", irq
);
187 spin_lock_irqsave(&desc
->lock
, flags
);
188 desc
->msi_desc
= entry
;
191 spin_unlock_irqrestore(&desc
->lock
, flags
);
196 * set_irq_chip_data - set irq chip data for an irq
197 * @irq: Interrupt number
198 * @data: Pointer to chip specific data
200 * Set the hardware irq chip data for an irq
202 int set_irq_chip_data(unsigned int irq
, void *data
)
204 struct irq_desc
*desc
;
207 desc
= irq_to_desc(irq
);
210 "Trying to install chip data for IRQ%d\n", irq
);
215 printk(KERN_ERR
"BUG: bad set_irq_chip_data(IRQ#%d)\n", irq
);
219 spin_lock_irqsave(&desc
->lock
, flags
);
220 desc
->chip_data
= data
;
221 spin_unlock_irqrestore(&desc
->lock
, flags
);
225 EXPORT_SYMBOL(set_irq_chip_data
);
228 * default enable function
230 static void default_enable(unsigned int irq
)
232 struct irq_desc
*desc
;
234 desc
= irq_to_desc(irq
);
235 desc
->chip
->unmask(irq
);
236 desc
->status
&= ~IRQ_MASKED
;
240 * default disable function
242 static void default_disable(unsigned int irq
)
247 * default startup function
249 static unsigned int default_startup(unsigned int irq
)
251 struct irq_desc
*desc
;
253 desc
= irq_to_desc(irq
);
254 desc
->chip
->enable(irq
);
260 * default shutdown function
262 static void default_shutdown(unsigned int irq
)
264 struct irq_desc
*desc
;
266 desc
= irq_to_desc(irq
);
267 desc
->chip
->mask(irq
);
268 desc
->status
|= IRQ_MASKED
;
272 * Fixup enable/disable function pointers
274 void irq_chip_set_defaults(struct irq_chip
*chip
)
277 chip
->enable
= default_enable
;
279 chip
->disable
= default_disable
;
281 chip
->startup
= default_startup
;
283 * We use chip->disable, when the user provided its own. When
284 * we have default_disable set for chip->disable, then we need
285 * to use default_shutdown, otherwise the irq line is not
286 * disabled on free_irq():
289 chip
->shutdown
= chip
->disable
!= default_disable
?
290 chip
->disable
: default_shutdown
;
292 chip
->name
= chip
->typename
;
294 chip
->end
= dummy_irq_chip
.end
;
297 static inline void mask_ack_irq(struct irq_desc
*desc
, int irq
)
299 if (desc
->chip
->mask_ack
)
300 desc
->chip
->mask_ack(irq
);
302 desc
->chip
->mask(irq
);
303 desc
->chip
->ack(irq
);
308 * handle_simple_irq - Simple and software-decoded IRQs.
309 * @irq: the interrupt number
310 * @desc: the interrupt description structure for this irq
312 * Simple interrupts are either sent from a demultiplexing interrupt
313 * handler or come from hardware, where no interrupt hardware control
316 * Note: The caller is expected to handle the ack, clear, mask and
317 * unmask issues if necessary.
320 handle_simple_irq(unsigned int irq
, struct irq_desc
*desc
)
322 struct irqaction
*action
;
323 irqreturn_t action_ret
;
325 spin_lock(&desc
->lock
);
327 if (unlikely(desc
->status
& IRQ_INPROGRESS
))
329 desc
->status
&= ~(IRQ_REPLAY
| IRQ_WAITING
);
330 kstat_irqs_this_cpu(desc
)++;
332 action
= desc
->action
;
333 if (unlikely(!action
|| (desc
->status
& IRQ_DISABLED
)))
336 desc
->status
|= IRQ_INPROGRESS
;
337 spin_unlock(&desc
->lock
);
339 action_ret
= handle_IRQ_event(irq
, action
);
341 note_interrupt(irq
, desc
, action_ret
);
343 spin_lock(&desc
->lock
);
344 desc
->status
&= ~IRQ_INPROGRESS
;
346 spin_unlock(&desc
->lock
);
350 * handle_level_irq - Level type irq handler
351 * @irq: the interrupt number
352 * @desc: the interrupt description structure for this irq
354 * Level type interrupts are active as long as the hardware line has
355 * the active level. This may require to mask the interrupt and unmask
356 * it after the associated handler has acknowledged the device, so the
357 * interrupt line is back to inactive.
360 handle_level_irq(unsigned int irq
, struct irq_desc
*desc
)
362 struct irqaction
*action
;
363 irqreturn_t action_ret
;
365 spin_lock(&desc
->lock
);
366 mask_ack_irq(desc
, irq
);
368 if (unlikely(desc
->status
& IRQ_INPROGRESS
))
370 desc
->status
&= ~(IRQ_REPLAY
| IRQ_WAITING
);
371 kstat_irqs_this_cpu(desc
)++;
374 * If its disabled or no action available
375 * keep it masked and get out of here
377 action
= desc
->action
;
378 if (unlikely(!action
|| (desc
->status
& IRQ_DISABLED
)))
381 desc
->status
|= IRQ_INPROGRESS
;
382 spin_unlock(&desc
->lock
);
384 action_ret
= handle_IRQ_event(irq
, action
);
386 note_interrupt(irq
, desc
, action_ret
);
388 spin_lock(&desc
->lock
);
389 desc
->status
&= ~IRQ_INPROGRESS
;
390 if (!(desc
->status
& IRQ_DISABLED
) && desc
->chip
->unmask
)
391 desc
->chip
->unmask(irq
);
393 spin_unlock(&desc
->lock
);
397 * handle_fasteoi_irq - irq handler for transparent controllers
398 * @irq: the interrupt number
399 * @desc: the interrupt description structure for this irq
401 * Only a single callback will be issued to the chip: an ->eoi()
402 * call when the interrupt has been serviced. This enables support
403 * for modern forms of interrupt handlers, which handle the flow
404 * details in hardware, transparently.
407 handle_fasteoi_irq(unsigned int irq
, struct irq_desc
*desc
)
409 struct irqaction
*action
;
410 irqreturn_t action_ret
;
412 spin_lock(&desc
->lock
);
414 if (unlikely(desc
->status
& IRQ_INPROGRESS
))
417 desc
->status
&= ~(IRQ_REPLAY
| IRQ_WAITING
);
418 kstat_irqs_this_cpu(desc
)++;
421 * If its disabled or no action available
422 * then mask it and get out of here:
424 action
= desc
->action
;
425 if (unlikely(!action
|| (desc
->status
& IRQ_DISABLED
))) {
426 desc
->status
|= IRQ_PENDING
;
427 if (desc
->chip
->mask
)
428 desc
->chip
->mask(irq
);
432 desc
->status
|= IRQ_INPROGRESS
;
433 desc
->status
&= ~IRQ_PENDING
;
434 spin_unlock(&desc
->lock
);
436 action_ret
= handle_IRQ_event(irq
, action
);
438 note_interrupt(irq
, desc
, action_ret
);
440 spin_lock(&desc
->lock
);
441 desc
->status
&= ~IRQ_INPROGRESS
;
443 desc
->chip
->eoi(irq
);
445 spin_unlock(&desc
->lock
);
449 * handle_edge_irq - edge type IRQ handler
450 * @irq: the interrupt number
451 * @desc: the interrupt description structure for this irq
453 * Interrupt occures on the falling and/or rising edge of a hardware
454 * signal. The occurence is latched into the irq controller hardware
455 * and must be acked in order to be reenabled. After the ack another
456 * interrupt can happen on the same source even before the first one
457 * is handled by the assosiacted event handler. If this happens it
458 * might be necessary to disable (mask) the interrupt depending on the
459 * controller hardware. This requires to reenable the interrupt inside
460 * of the loop which handles the interrupts which have arrived while
461 * the handler was running. If all pending interrupts are handled, the
465 handle_edge_irq(unsigned int irq
, struct irq_desc
*desc
)
467 spin_lock(&desc
->lock
);
469 desc
->status
&= ~(IRQ_REPLAY
| IRQ_WAITING
);
472 * If we're currently running this IRQ, or its disabled,
473 * we shouldn't process the IRQ. Mark it pending, handle
474 * the necessary masking and go out
476 if (unlikely((desc
->status
& (IRQ_INPROGRESS
| IRQ_DISABLED
)) ||
478 desc
->status
|= (IRQ_PENDING
| IRQ_MASKED
);
479 mask_ack_irq(desc
, irq
);
483 kstat_irqs_this_cpu(desc
)++;
485 /* Start handling the irq */
486 desc
->chip
->ack(irq
);
488 /* Mark the IRQ currently in progress.*/
489 desc
->status
|= IRQ_INPROGRESS
;
492 struct irqaction
*action
= desc
->action
;
493 irqreturn_t action_ret
;
495 if (unlikely(!action
)) {
496 desc
->chip
->mask(irq
);
501 * When another irq arrived while we were handling
502 * one, we could have masked the irq.
503 * Renable it, if it was not disabled in meantime.
505 if (unlikely((desc
->status
&
506 (IRQ_PENDING
| IRQ_MASKED
| IRQ_DISABLED
)) ==
507 (IRQ_PENDING
| IRQ_MASKED
))) {
508 desc
->chip
->unmask(irq
);
509 desc
->status
&= ~IRQ_MASKED
;
512 desc
->status
&= ~IRQ_PENDING
;
513 spin_unlock(&desc
->lock
);
514 action_ret
= handle_IRQ_event(irq
, action
);
516 note_interrupt(irq
, desc
, action_ret
);
517 spin_lock(&desc
->lock
);
519 } while ((desc
->status
& (IRQ_PENDING
| IRQ_DISABLED
)) == IRQ_PENDING
);
521 desc
->status
&= ~IRQ_INPROGRESS
;
523 spin_unlock(&desc
->lock
);
527 * handle_percpu_IRQ - Per CPU local irq handler
528 * @irq: the interrupt number
529 * @desc: the interrupt description structure for this irq
531 * Per CPU interrupts on SMP machines without locking requirements
534 handle_percpu_irq(unsigned int irq
, struct irq_desc
*desc
)
536 irqreturn_t action_ret
;
538 kstat_irqs_this_cpu(desc
)++;
541 desc
->chip
->ack(irq
);
543 action_ret
= handle_IRQ_event(irq
, desc
->action
);
545 note_interrupt(irq
, desc
, action_ret
);
548 desc
->chip
->eoi(irq
);
552 __set_irq_handler(unsigned int irq
, irq_flow_handler_t handle
, int is_chained
,
555 struct irq_desc
*desc
;
558 desc
= irq_to_desc(irq
);
561 "Trying to install type control for IRQ%d\n", irq
);
566 handle
= handle_bad_irq
;
567 else if (desc
->chip
== &no_irq_chip
) {
568 printk(KERN_WARNING
"Trying to install %sinterrupt handler "
569 "for IRQ%d\n", is_chained
? "chained " : "", irq
);
571 * Some ARM implementations install a handler for really dumb
572 * interrupt hardware without setting an irq_chip. This worked
573 * with the ARM no_irq_chip but the check in setup_irq would
574 * prevent us to setup the interrupt at all. Switch it to
575 * dummy_irq_chip for easy transition.
577 desc
->chip
= &dummy_irq_chip
;
580 spin_lock_irqsave(&desc
->lock
, flags
);
583 if (handle
== handle_bad_irq
) {
584 if (desc
->chip
!= &no_irq_chip
)
585 mask_ack_irq(desc
, irq
);
586 desc
->status
|= IRQ_DISABLED
;
589 desc
->handle_irq
= handle
;
592 if (handle
!= handle_bad_irq
&& is_chained
) {
593 desc
->status
&= ~IRQ_DISABLED
;
594 desc
->status
|= IRQ_NOREQUEST
| IRQ_NOPROBE
;
596 desc
->chip
->startup(irq
);
598 spin_unlock_irqrestore(&desc
->lock
, flags
);
602 set_irq_chip_and_handler(unsigned int irq
, struct irq_chip
*chip
,
603 irq_flow_handler_t handle
)
605 set_irq_chip(irq
, chip
);
606 __set_irq_handler(irq
, handle
, 0, NULL
);
610 set_irq_chip_and_handler_name(unsigned int irq
, struct irq_chip
*chip
,
611 irq_flow_handler_t handle
, const char *name
)
613 set_irq_chip(irq
, chip
);
614 __set_irq_handler(irq
, handle
, 0, name
);
617 void __init
set_irq_noprobe(unsigned int irq
)
619 struct irq_desc
*desc
;
622 desc
= irq_to_desc(irq
);
624 printk(KERN_ERR
"Trying to mark IRQ%d non-probeable\n", irq
);
629 spin_lock_irqsave(&desc
->lock
, flags
);
630 desc
->status
|= IRQ_NOPROBE
;
631 spin_unlock_irqrestore(&desc
->lock
, flags
);
634 void __init
set_irq_probe(unsigned int irq
)
636 struct irq_desc
*desc
;
639 desc
= irq_to_desc(irq
);
641 printk(KERN_ERR
"Trying to mark IRQ%d probeable\n", irq
);
646 spin_lock_irqsave(&desc
->lock
, flags
);
647 desc
->status
&= ~IRQ_NOPROBE
;
648 spin_unlock_irqrestore(&desc
->lock
, flags
);