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
;
31 WARN(1, KERN_ERR
"Trying to initialize invalid IRQ%d\n", irq
);
35 /* Ensure we don't have left over values from a previous use of this irq */
36 desc
= irq_desc
+ irq
;
37 spin_lock_irqsave(&desc
->lock
, flags
);
38 desc
->status
= IRQ_DISABLED
;
39 desc
->chip
= &no_irq_chip
;
40 desc
->handle_irq
= handle_bad_irq
;
42 desc
->msi_desc
= NULL
;
43 desc
->handler_data
= NULL
;
44 desc
->chip_data
= NULL
;
47 desc
->irqs_unhandled
= 0;
49 cpus_setall(desc
->affinity
);
51 spin_unlock_irqrestore(&desc
->lock
, flags
);
55 * dynamic_irq_cleanup - cleanup a dynamically allocated irq
56 * @irq: irq number to initialize
58 void dynamic_irq_cleanup(unsigned int irq
)
60 struct irq_desc
*desc
;
64 WARN(1, KERN_ERR
"Trying to cleanup invalid IRQ%d\n", irq
);
68 desc
= irq_desc
+ irq
;
69 spin_lock_irqsave(&desc
->lock
, flags
);
71 spin_unlock_irqrestore(&desc
->lock
, flags
);
72 WARN(1, KERN_ERR
"Destroying IRQ%d without calling free_irq\n",
76 desc
->msi_desc
= NULL
;
77 desc
->handler_data
= NULL
;
78 desc
->chip_data
= NULL
;
79 desc
->handle_irq
= handle_bad_irq
;
80 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
;
97 WARN(1, KERN_ERR
"Trying to install chip for IRQ%d\n", irq
);
104 desc
= irq_desc
+ irq
;
105 spin_lock_irqsave(&desc
->lock
, flags
);
106 irq_chip_set_defaults(chip
);
108 spin_unlock_irqrestore(&desc
->lock
, flags
);
112 EXPORT_SYMBOL(set_irq_chip
);
115 * set_irq_type - set the irq type for an irq
117 * @type: interrupt type - see include/linux/interrupt.h
119 int set_irq_type(unsigned int irq
, unsigned int type
)
121 struct irq_desc
*desc
;
125 if (irq
>= NR_IRQS
) {
126 printk(KERN_ERR
"Trying to set irq type for IRQ%d\n", irq
);
130 desc
= irq_desc
+ irq
;
131 if (desc
->chip
->set_type
) {
132 spin_lock_irqsave(&desc
->lock
, flags
);
133 ret
= desc
->chip
->set_type(irq
, type
);
134 spin_unlock_irqrestore(&desc
->lock
, flags
);
138 EXPORT_SYMBOL(set_irq_type
);
141 * set_irq_data - set irq type data for an irq
142 * @irq: Interrupt number
143 * @data: Pointer to interrupt specific data
145 * Set the hardware irq controller data for an irq
147 int set_irq_data(unsigned int irq
, void *data
)
149 struct irq_desc
*desc
;
152 if (irq
>= NR_IRQS
) {
154 "Trying to install controller data for IRQ%d\n", irq
);
158 desc
= irq_desc
+ irq
;
159 spin_lock_irqsave(&desc
->lock
, flags
);
160 desc
->handler_data
= data
;
161 spin_unlock_irqrestore(&desc
->lock
, flags
);
164 EXPORT_SYMBOL(set_irq_data
);
167 * set_irq_data - set irq type data for an irq
168 * @irq: Interrupt number
169 * @entry: Pointer to MSI descriptor data
171 * Set the hardware irq controller data for an irq
173 int set_irq_msi(unsigned int irq
, struct msi_desc
*entry
)
175 struct irq_desc
*desc
;
178 if (irq
>= NR_IRQS
) {
180 "Trying to install msi data for IRQ%d\n", irq
);
183 desc
= irq_desc
+ irq
;
184 spin_lock_irqsave(&desc
->lock
, flags
);
185 desc
->msi_desc
= entry
;
188 spin_unlock_irqrestore(&desc
->lock
, flags
);
193 * set_irq_chip_data - set irq chip data for an irq
194 * @irq: Interrupt number
195 * @data: Pointer to chip specific data
197 * Set the hardware irq chip data for an irq
199 int set_irq_chip_data(unsigned int irq
, void *data
)
201 struct irq_desc
*desc
= irq_desc
+ irq
;
204 if (irq
>= NR_IRQS
|| !desc
->chip
) {
205 printk(KERN_ERR
"BUG: bad set_irq_chip_data(IRQ#%d)\n", irq
);
209 spin_lock_irqsave(&desc
->lock
, flags
);
210 desc
->chip_data
= data
;
211 spin_unlock_irqrestore(&desc
->lock
, flags
);
215 EXPORT_SYMBOL(set_irq_chip_data
);
218 * default enable function
220 static void default_enable(unsigned int irq
)
222 struct irq_desc
*desc
= irq_desc
+ irq
;
224 desc
->chip
->unmask(irq
);
225 desc
->status
&= ~IRQ_MASKED
;
229 * default disable function
231 static void default_disable(unsigned int irq
)
236 * default startup function
238 static unsigned int default_startup(unsigned int irq
)
240 irq_desc
[irq
].chip
->enable(irq
);
246 * default shutdown function
248 static void default_shutdown(unsigned int irq
)
250 struct irq_desc
*desc
= irq_desc
+ irq
;
252 desc
->chip
->mask(irq
);
253 desc
->status
|= IRQ_MASKED
;
257 * Fixup enable/disable function pointers
259 void irq_chip_set_defaults(struct irq_chip
*chip
)
262 chip
->enable
= default_enable
;
264 chip
->disable
= default_disable
;
266 chip
->startup
= default_startup
;
268 * We use chip->disable, when the user provided its own. When
269 * we have default_disable set for chip->disable, then we need
270 * to use default_shutdown, otherwise the irq line is not
271 * disabled on free_irq():
274 chip
->shutdown
= chip
->disable
!= default_disable
?
275 chip
->disable
: default_shutdown
;
277 chip
->name
= chip
->typename
;
279 chip
->end
= dummy_irq_chip
.end
;
282 static inline void mask_ack_irq(struct irq_desc
*desc
, int irq
)
284 if (desc
->chip
->mask_ack
)
285 desc
->chip
->mask_ack(irq
);
287 desc
->chip
->mask(irq
);
288 desc
->chip
->ack(irq
);
293 * handle_simple_irq - Simple and software-decoded IRQs.
294 * @irq: the interrupt number
295 * @desc: the interrupt description structure for this irq
297 * Simple interrupts are either sent from a demultiplexing interrupt
298 * handler or come from hardware, where no interrupt hardware control
301 * Note: The caller is expected to handle the ack, clear, mask and
302 * unmask issues if necessary.
305 handle_simple_irq(unsigned int irq
, struct irq_desc
*desc
)
307 struct irqaction
*action
;
308 irqreturn_t action_ret
;
309 const unsigned int cpu
= smp_processor_id();
311 spin_lock(&desc
->lock
);
313 if (unlikely(desc
->status
& IRQ_INPROGRESS
))
315 desc
->status
&= ~(IRQ_REPLAY
| IRQ_WAITING
);
316 kstat_cpu(cpu
).irqs
[irq
]++;
318 action
= desc
->action
;
319 if (unlikely(!action
|| (desc
->status
& IRQ_DISABLED
)))
322 desc
->status
|= IRQ_INPROGRESS
;
323 spin_unlock(&desc
->lock
);
325 action_ret
= handle_IRQ_event(irq
, action
);
327 note_interrupt(irq
, desc
, action_ret
);
329 spin_lock(&desc
->lock
);
330 desc
->status
&= ~IRQ_INPROGRESS
;
332 spin_unlock(&desc
->lock
);
336 * handle_level_irq - Level type irq handler
337 * @irq: the interrupt number
338 * @desc: the interrupt description structure for this irq
340 * Level type interrupts are active as long as the hardware line has
341 * the active level. This may require to mask the interrupt and unmask
342 * it after the associated handler has acknowledged the device, so the
343 * interrupt line is back to inactive.
346 handle_level_irq(unsigned int irq
, struct irq_desc
*desc
)
348 unsigned int cpu
= smp_processor_id();
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
))
357 desc
->status
&= ~(IRQ_REPLAY
| IRQ_WAITING
);
358 kstat_cpu(cpu
).irqs
[irq
]++;
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
)))
368 desc
->status
|= IRQ_INPROGRESS
;
369 spin_unlock(&desc
->lock
);
371 action_ret
= handle_IRQ_event(irq
, action
);
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
);
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.
394 handle_fasteoi_irq(unsigned int irq
, struct irq_desc
*desc
)
396 unsigned int cpu
= smp_processor_id();
397 struct irqaction
*action
;
398 irqreturn_t action_ret
;
400 spin_lock(&desc
->lock
);
402 if (unlikely(desc
->status
& IRQ_INPROGRESS
))
405 desc
->status
&= ~(IRQ_REPLAY
| IRQ_WAITING
);
406 kstat_cpu(cpu
).irqs
[irq
]++;
409 * If its disabled or no action available
410 * then mask it and get out of here:
412 action
= desc
->action
;
413 if (unlikely(!action
|| (desc
->status
& IRQ_DISABLED
))) {
414 desc
->status
|= IRQ_PENDING
;
415 if (desc
->chip
->mask
)
416 desc
->chip
->mask(irq
);
420 desc
->status
|= IRQ_INPROGRESS
;
421 desc
->status
&= ~IRQ_PENDING
;
422 spin_unlock(&desc
->lock
);
424 action_ret
= handle_IRQ_event(irq
, action
);
426 note_interrupt(irq
, desc
, action_ret
);
428 spin_lock(&desc
->lock
);
429 desc
->status
&= ~IRQ_INPROGRESS
;
431 desc
->chip
->eoi(irq
);
433 spin_unlock(&desc
->lock
);
437 * handle_edge_irq - edge type IRQ handler
438 * @irq: the interrupt number
439 * @desc: the interrupt description structure for this irq
441 * Interrupt occures on the falling and/or rising edge of a hardware
442 * signal. The occurence is latched into the irq controller hardware
443 * and must be acked in order to be reenabled. After the ack another
444 * interrupt can happen on the same source even before the first one
445 * is handled by the assosiacted event handler. If this happens it
446 * might be necessary to disable (mask) the interrupt depending on the
447 * controller hardware. This requires to reenable the interrupt inside
448 * of the loop which handles the interrupts which have arrived while
449 * the handler was running. If all pending interrupts are handled, the
453 handle_edge_irq(unsigned int irq
, struct irq_desc
*desc
)
455 const unsigned int cpu
= smp_processor_id();
457 spin_lock(&desc
->lock
);
459 desc
->status
&= ~(IRQ_REPLAY
| IRQ_WAITING
);
462 * If we're currently running this IRQ, or its disabled,
463 * we shouldn't process the IRQ. Mark it pending, handle
464 * the necessary masking and go out
466 if (unlikely((desc
->status
& (IRQ_INPROGRESS
| IRQ_DISABLED
)) ||
468 desc
->status
|= (IRQ_PENDING
| IRQ_MASKED
);
469 mask_ack_irq(desc
, irq
);
473 kstat_cpu(cpu
).irqs
[irq
]++;
475 /* Start handling the irq */
476 desc
->chip
->ack(irq
);
478 /* Mark the IRQ currently in progress.*/
479 desc
->status
|= IRQ_INPROGRESS
;
482 struct irqaction
*action
= desc
->action
;
483 irqreturn_t action_ret
;
485 if (unlikely(!action
)) {
486 desc
->chip
->mask(irq
);
491 * When another irq arrived while we were handling
492 * one, we could have masked the irq.
493 * Renable it, if it was not disabled in meantime.
495 if (unlikely((desc
->status
&
496 (IRQ_PENDING
| IRQ_MASKED
| IRQ_DISABLED
)) ==
497 (IRQ_PENDING
| IRQ_MASKED
))) {
498 desc
->chip
->unmask(irq
);
499 desc
->status
&= ~IRQ_MASKED
;
502 desc
->status
&= ~IRQ_PENDING
;
503 spin_unlock(&desc
->lock
);
504 action_ret
= handle_IRQ_event(irq
, action
);
506 note_interrupt(irq
, desc
, action_ret
);
507 spin_lock(&desc
->lock
);
509 } while ((desc
->status
& (IRQ_PENDING
| IRQ_DISABLED
)) == IRQ_PENDING
);
511 desc
->status
&= ~IRQ_INPROGRESS
;
513 spin_unlock(&desc
->lock
);
517 * handle_percpu_IRQ - Per CPU local irq handler
518 * @irq: the interrupt number
519 * @desc: the interrupt description structure for this irq
521 * Per CPU interrupts on SMP machines without locking requirements
524 handle_percpu_irq(unsigned int irq
, struct irq_desc
*desc
)
526 irqreturn_t action_ret
;
528 kstat_this_cpu
.irqs
[irq
]++;
531 desc
->chip
->ack(irq
);
533 action_ret
= handle_IRQ_event(irq
, desc
->action
);
535 note_interrupt(irq
, desc
, action_ret
);
538 desc
->chip
->eoi(irq
);
542 __set_irq_handler(unsigned int irq
, irq_flow_handler_t handle
, int is_chained
,
545 struct irq_desc
*desc
;
548 if (irq
>= NR_IRQS
) {
550 "Trying to install type control for IRQ%d\n", irq
);
554 desc
= irq_desc
+ irq
;
557 handle
= handle_bad_irq
;
558 else if (desc
->chip
== &no_irq_chip
) {
559 printk(KERN_WARNING
"Trying to install %sinterrupt handler "
560 "for IRQ%d\n", is_chained
? "chained " : "", irq
);
562 * Some ARM implementations install a handler for really dumb
563 * interrupt hardware without setting an irq_chip. This worked
564 * with the ARM no_irq_chip but the check in setup_irq would
565 * prevent us to setup the interrupt at all. Switch it to
566 * dummy_irq_chip for easy transition.
568 desc
->chip
= &dummy_irq_chip
;
571 spin_lock_irqsave(&desc
->lock
, flags
);
574 if (handle
== handle_bad_irq
) {
575 if (desc
->chip
!= &no_irq_chip
)
576 mask_ack_irq(desc
, irq
);
577 desc
->status
|= IRQ_DISABLED
;
580 desc
->handle_irq
= handle
;
583 if (handle
!= handle_bad_irq
&& is_chained
) {
584 desc
->status
&= ~IRQ_DISABLED
;
585 desc
->status
|= IRQ_NOREQUEST
| IRQ_NOPROBE
;
587 desc
->chip
->unmask(irq
);
589 spin_unlock_irqrestore(&desc
->lock
, flags
);
593 set_irq_chip_and_handler(unsigned int irq
, struct irq_chip
*chip
,
594 irq_flow_handler_t handle
)
596 set_irq_chip(irq
, chip
);
597 __set_irq_handler(irq
, handle
, 0, NULL
);
601 set_irq_chip_and_handler_name(unsigned int irq
, struct irq_chip
*chip
,
602 irq_flow_handler_t handle
, const char *name
)
604 set_irq_chip(irq
, chip
);
605 __set_irq_handler(irq
, handle
, 0, name
);
608 void __init
set_irq_noprobe(unsigned int irq
)
610 struct irq_desc
*desc
;
613 if (irq
>= NR_IRQS
) {
614 printk(KERN_ERR
"Trying to mark IRQ%d non-probeable\n", irq
);
619 desc
= irq_desc
+ irq
;
621 spin_lock_irqsave(&desc
->lock
, flags
);
622 desc
->status
|= IRQ_NOPROBE
;
623 spin_unlock_irqrestore(&desc
->lock
, flags
);
626 void __init
set_irq_probe(unsigned int irq
)
628 struct irq_desc
*desc
;
631 if (irq
>= NR_IRQS
) {
632 printk(KERN_ERR
"Trying to mark IRQ%d probeable\n", irq
);
637 desc
= irq_desc
+ irq
;
639 spin_lock_irqsave(&desc
->lock
, flags
);
640 desc
->status
&= ~IRQ_NOPROBE
;
641 spin_unlock_irqrestore(&desc
->lock
, flags
);