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 printk(KERN_ERR
"Trying to initialize invalid IRQ%d\n", irq
);
36 /* Ensure we don't have left over values from a previous use of this irq */
37 desc
= irq_desc
+ 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 desc
->affinity
= CPU_MASK_ALL
;
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
;
65 printk(KERN_ERR
"Trying to cleanup invalid IRQ%d\n", irq
);
70 desc
= irq_desc
+ irq
;
71 spin_lock_irqsave(&desc
->lock
, flags
);
73 spin_unlock_irqrestore(&desc
->lock
, flags
);
74 printk(KERN_ERR
"Destroying IRQ%d without calling free_irq\n",
79 desc
->msi_desc
= NULL
;
80 desc
->handler_data
= NULL
;
81 desc
->chip_data
= NULL
;
82 desc
->handle_irq
= handle_bad_irq
;
83 desc
->chip
= &no_irq_chip
;
84 spin_unlock_irqrestore(&desc
->lock
, flags
);
89 * set_irq_chip - set the irq chip for an irq
91 * @chip: pointer to irq chip description structure
93 int set_irq_chip(unsigned int irq
, struct irq_chip
*chip
)
95 struct irq_desc
*desc
;
99 printk(KERN_ERR
"Trying to install chip for IRQ%d\n", irq
);
107 desc
= irq_desc
+ irq
;
108 spin_lock_irqsave(&desc
->lock
, flags
);
109 irq_chip_set_defaults(chip
);
111 spin_unlock_irqrestore(&desc
->lock
, flags
);
115 EXPORT_SYMBOL(set_irq_chip
);
118 * set_irq_type - set the irq type for an irq
120 * @type: interrupt type - see include/linux/interrupt.h
122 int set_irq_type(unsigned int irq
, unsigned int type
)
124 struct irq_desc
*desc
;
128 if (irq
>= NR_IRQS
) {
129 printk(KERN_ERR
"Trying to set irq type for IRQ%d\n", irq
);
133 desc
= irq_desc
+ irq
;
134 if (desc
->chip
->set_type
) {
135 spin_lock_irqsave(&desc
->lock
, flags
);
136 ret
= desc
->chip
->set_type(irq
, type
);
137 spin_unlock_irqrestore(&desc
->lock
, flags
);
141 EXPORT_SYMBOL(set_irq_type
);
144 * set_irq_data - set irq type data for an irq
145 * @irq: Interrupt number
146 * @data: Pointer to interrupt specific data
148 * Set the hardware irq controller data for an irq
150 int set_irq_data(unsigned int irq
, void *data
)
152 struct irq_desc
*desc
;
155 if (irq
>= NR_IRQS
) {
157 "Trying to install controller data for IRQ%d\n", irq
);
161 desc
= irq_desc
+ irq
;
162 spin_lock_irqsave(&desc
->lock
, flags
);
163 desc
->handler_data
= data
;
164 spin_unlock_irqrestore(&desc
->lock
, flags
);
167 EXPORT_SYMBOL(set_irq_data
);
170 * set_irq_data - set irq type data for an irq
171 * @irq: Interrupt number
172 * @entry: Pointer to MSI descriptor data
174 * Set the hardware irq controller data for an irq
176 int set_irq_msi(unsigned int irq
, struct msi_desc
*entry
)
178 struct irq_desc
*desc
;
181 if (irq
>= NR_IRQS
) {
183 "Trying to install msi data for IRQ%d\n", irq
);
186 desc
= irq_desc
+ 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
= irq_desc
+ irq
;
207 if (irq
>= NR_IRQS
|| !desc
->chip
) {
208 printk(KERN_ERR
"BUG: bad set_irq_chip_data(IRQ#%d)\n", irq
);
212 spin_lock_irqsave(&desc
->lock
, flags
);
213 desc
->chip_data
= data
;
214 spin_unlock_irqrestore(&desc
->lock
, flags
);
218 EXPORT_SYMBOL(set_irq_chip_data
);
221 * default enable function
223 static void default_enable(unsigned int irq
)
225 struct irq_desc
*desc
= irq_desc
+ irq
;
227 desc
->chip
->unmask(irq
);
228 desc
->status
&= ~IRQ_MASKED
;
232 * default disable function
234 static void default_disable(unsigned int irq
)
239 * default startup function
241 static unsigned int default_startup(unsigned int irq
)
243 irq_desc
[irq
].chip
->enable(irq
);
249 * default shutdown function
251 static void default_shutdown(unsigned int irq
)
253 struct irq_desc
*desc
= irq_desc
+ irq
;
255 desc
->chip
->mask(irq
);
256 desc
->status
|= IRQ_MASKED
;
260 * Fixup enable/disable function pointers
262 void irq_chip_set_defaults(struct irq_chip
*chip
)
265 chip
->enable
= default_enable
;
267 chip
->disable
= default_disable
;
269 chip
->startup
= default_startup
;
271 * We use chip->disable, when the user provided its own. When
272 * we have default_disable set for chip->disable, then we need
273 * to use default_shutdown, otherwise the irq line is not
274 * disabled on free_irq():
277 chip
->shutdown
= chip
->disable
!= default_disable
?
278 chip
->disable
: default_shutdown
;
280 chip
->name
= chip
->typename
;
282 chip
->end
= dummy_irq_chip
.end
;
285 static inline void mask_ack_irq(struct irq_desc
*desc
, int irq
)
287 if (desc
->chip
->mask_ack
)
288 desc
->chip
->mask_ack(irq
);
290 desc
->chip
->mask(irq
);
291 desc
->chip
->ack(irq
);
296 * handle_simple_irq - Simple and software-decoded IRQs.
297 * @irq: the interrupt number
298 * @desc: the interrupt description structure for this irq
300 * Simple interrupts are either sent from a demultiplexing interrupt
301 * handler or come from hardware, where no interrupt hardware control
304 * Note: The caller is expected to handle the ack, clear, mask and
305 * unmask issues if necessary.
308 handle_simple_irq(unsigned int irq
, struct irq_desc
*desc
)
310 struct irqaction
*action
;
311 irqreturn_t action_ret
;
312 const unsigned int cpu
= smp_processor_id();
314 spin_lock(&desc
->lock
);
316 if (unlikely(desc
->status
& IRQ_INPROGRESS
))
318 desc
->status
&= ~(IRQ_REPLAY
| IRQ_WAITING
);
319 kstat_cpu(cpu
).irqs
[irq
]++;
321 action
= desc
->action
;
322 if (unlikely(!action
|| (desc
->status
& IRQ_DISABLED
)))
325 desc
->status
|= IRQ_INPROGRESS
;
326 spin_unlock(&desc
->lock
);
328 action_ret
= handle_IRQ_event(irq
, action
);
330 note_interrupt(irq
, desc
, action_ret
);
332 spin_lock(&desc
->lock
);
333 desc
->status
&= ~IRQ_INPROGRESS
;
335 spin_unlock(&desc
->lock
);
339 * handle_level_irq - Level type irq handler
340 * @irq: the interrupt number
341 * @desc: the interrupt description structure for this irq
343 * Level type interrupts are active as long as the hardware line has
344 * the active level. This may require to mask the interrupt and unmask
345 * it after the associated handler has acknowledged the device, so the
346 * interrupt line is back to inactive.
349 handle_level_irq(unsigned int irq
, struct irq_desc
*desc
)
351 unsigned int cpu
= smp_processor_id();
352 struct irqaction
*action
;
353 irqreturn_t action_ret
;
355 spin_lock(&desc
->lock
);
356 mask_ack_irq(desc
, irq
);
358 if (unlikely(desc
->status
& IRQ_INPROGRESS
))
360 desc
->status
&= ~(IRQ_REPLAY
| IRQ_WAITING
);
361 kstat_cpu(cpu
).irqs
[irq
]++;
364 * If its disabled or no action available
365 * keep it masked and get out of here
367 action
= desc
->action
;
368 if (unlikely(!action
|| (desc
->status
& IRQ_DISABLED
)))
371 desc
->status
|= IRQ_INPROGRESS
;
372 spin_unlock(&desc
->lock
);
374 action_ret
= handle_IRQ_event(irq
, action
);
376 note_interrupt(irq
, desc
, action_ret
);
378 spin_lock(&desc
->lock
);
379 desc
->status
&= ~IRQ_INPROGRESS
;
380 if (!(desc
->status
& IRQ_DISABLED
) && desc
->chip
->unmask
)
381 desc
->chip
->unmask(irq
);
383 spin_unlock(&desc
->lock
);
387 * handle_fasteoi_irq - irq handler for transparent controllers
388 * @irq: the interrupt number
389 * @desc: the interrupt description structure for this irq
391 * Only a single callback will be issued to the chip: an ->eoi()
392 * call when the interrupt has been serviced. This enables support
393 * for modern forms of interrupt handlers, which handle the flow
394 * details in hardware, transparently.
397 handle_fasteoi_irq(unsigned int irq
, struct irq_desc
*desc
)
399 unsigned int cpu
= smp_processor_id();
400 struct irqaction
*action
;
401 irqreturn_t action_ret
;
403 spin_lock(&desc
->lock
);
405 if (unlikely(desc
->status
& IRQ_INPROGRESS
))
408 desc
->status
&= ~(IRQ_REPLAY
| IRQ_WAITING
);
409 kstat_cpu(cpu
).irqs
[irq
]++;
412 * If its disabled or no action available
413 * then mask it and get out of here:
415 action
= desc
->action
;
416 if (unlikely(!action
|| (desc
->status
& IRQ_DISABLED
))) {
417 desc
->status
|= IRQ_PENDING
;
418 if (desc
->chip
->mask
)
419 desc
->chip
->mask(irq
);
423 desc
->status
|= IRQ_INPROGRESS
;
424 desc
->status
&= ~IRQ_PENDING
;
425 spin_unlock(&desc
->lock
);
427 action_ret
= handle_IRQ_event(irq
, action
);
429 note_interrupt(irq
, desc
, action_ret
);
431 spin_lock(&desc
->lock
);
432 desc
->status
&= ~IRQ_INPROGRESS
;
434 desc
->chip
->eoi(irq
);
436 spin_unlock(&desc
->lock
);
440 * handle_edge_irq - edge type IRQ handler
441 * @irq: the interrupt number
442 * @desc: the interrupt description structure for this irq
444 * Interrupt occures on the falling and/or rising edge of a hardware
445 * signal. The occurence is latched into the irq controller hardware
446 * and must be acked in order to be reenabled. After the ack another
447 * interrupt can happen on the same source even before the first one
448 * is handled by the assosiacted event handler. If this happens it
449 * might be necessary to disable (mask) the interrupt depending on the
450 * controller hardware. This requires to reenable the interrupt inside
451 * of the loop which handles the interrupts which have arrived while
452 * the handler was running. If all pending interrupts are handled, the
456 handle_edge_irq(unsigned int irq
, struct irq_desc
*desc
)
458 const unsigned int cpu
= smp_processor_id();
460 spin_lock(&desc
->lock
);
462 desc
->status
&= ~(IRQ_REPLAY
| IRQ_WAITING
);
465 * If we're currently running this IRQ, or its disabled,
466 * we shouldn't process the IRQ. Mark it pending, handle
467 * the necessary masking and go out
469 if (unlikely((desc
->status
& (IRQ_INPROGRESS
| IRQ_DISABLED
)) ||
471 desc
->status
|= (IRQ_PENDING
| IRQ_MASKED
);
472 mask_ack_irq(desc
, irq
);
476 kstat_cpu(cpu
).irqs
[irq
]++;
478 /* Start handling the irq */
479 desc
->chip
->ack(irq
);
481 /* Mark the IRQ currently in progress.*/
482 desc
->status
|= IRQ_INPROGRESS
;
485 struct irqaction
*action
= desc
->action
;
486 irqreturn_t action_ret
;
488 if (unlikely(!action
)) {
489 desc
->chip
->mask(irq
);
494 * When another irq arrived while we were handling
495 * one, we could have masked the irq.
496 * Renable it, if it was not disabled in meantime.
498 if (unlikely((desc
->status
&
499 (IRQ_PENDING
| IRQ_MASKED
| IRQ_DISABLED
)) ==
500 (IRQ_PENDING
| IRQ_MASKED
))) {
501 desc
->chip
->unmask(irq
);
502 desc
->status
&= ~IRQ_MASKED
;
505 desc
->status
&= ~IRQ_PENDING
;
506 spin_unlock(&desc
->lock
);
507 action_ret
= handle_IRQ_event(irq
, action
);
509 note_interrupt(irq
, desc
, action_ret
);
510 spin_lock(&desc
->lock
);
512 } while ((desc
->status
& (IRQ_PENDING
| IRQ_DISABLED
)) == IRQ_PENDING
);
514 desc
->status
&= ~IRQ_INPROGRESS
;
516 spin_unlock(&desc
->lock
);
520 * handle_percpu_IRQ - Per CPU local irq handler
521 * @irq: the interrupt number
522 * @desc: the interrupt description structure for this irq
524 * Per CPU interrupts on SMP machines without locking requirements
527 handle_percpu_irq(unsigned int irq
, struct irq_desc
*desc
)
529 irqreturn_t action_ret
;
531 kstat_this_cpu
.irqs
[irq
]++;
534 desc
->chip
->ack(irq
);
536 action_ret
= handle_IRQ_event(irq
, desc
->action
);
538 note_interrupt(irq
, desc
, action_ret
);
541 desc
->chip
->eoi(irq
);
545 __set_irq_handler(unsigned int irq
, irq_flow_handler_t handle
, int is_chained
,
548 struct irq_desc
*desc
;
551 if (irq
>= NR_IRQS
) {
553 "Trying to install type control for IRQ%d\n", irq
);
557 desc
= irq_desc
+ irq
;
560 handle
= handle_bad_irq
;
561 else if (desc
->chip
== &no_irq_chip
) {
562 printk(KERN_WARNING
"Trying to install %sinterrupt handler "
563 "for IRQ%d\n", is_chained
? "chained " : "", irq
);
565 * Some ARM implementations install a handler for really dumb
566 * interrupt hardware without setting an irq_chip. This worked
567 * with the ARM no_irq_chip but the check in setup_irq would
568 * prevent us to setup the interrupt at all. Switch it to
569 * dummy_irq_chip for easy transition.
571 desc
->chip
= &dummy_irq_chip
;
574 spin_lock_irqsave(&desc
->lock
, flags
);
577 if (handle
== handle_bad_irq
) {
578 if (desc
->chip
!= &no_irq_chip
)
579 mask_ack_irq(desc
, irq
);
580 desc
->status
|= IRQ_DISABLED
;
583 desc
->handle_irq
= handle
;
586 if (handle
!= handle_bad_irq
&& is_chained
) {
587 desc
->status
&= ~IRQ_DISABLED
;
588 desc
->status
|= IRQ_NOREQUEST
| IRQ_NOPROBE
;
590 desc
->chip
->unmask(irq
);
592 spin_unlock_irqrestore(&desc
->lock
, flags
);
596 set_irq_chip_and_handler(unsigned int irq
, struct irq_chip
*chip
,
597 irq_flow_handler_t handle
)
599 set_irq_chip(irq
, chip
);
600 __set_irq_handler(irq
, handle
, 0, NULL
);
604 set_irq_chip_and_handler_name(unsigned int irq
, struct irq_chip
*chip
,
605 irq_flow_handler_t handle
, const char *name
)
607 set_irq_chip(irq
, chip
);
608 __set_irq_handler(irq
, handle
, 0, name
);
611 void __init
set_irq_noprobe(unsigned int irq
)
613 struct irq_desc
*desc
;
616 if (irq
>= NR_IRQS
) {
617 printk(KERN_ERR
"Trying to mark IRQ%d non-probeable\n", irq
);
622 desc
= irq_desc
+ irq
;
624 spin_lock_irqsave(&desc
->lock
, flags
);
625 desc
->status
|= IRQ_NOPROBE
;
626 spin_unlock_irqrestore(&desc
->lock
, flags
);
629 void __init
set_irq_probe(unsigned int irq
)
631 struct irq_desc
*desc
;
634 if (irq
>= NR_IRQS
) {
635 printk(KERN_ERR
"Trying to mark IRQ%d probeable\n", irq
);
640 desc
= irq_desc
+ irq
;
642 spin_lock_irqsave(&desc
->lock
, flags
);
643 desc
->status
&= ~IRQ_NOPROBE
;
644 spin_unlock_irqrestore(&desc
->lock
, flags
);