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
= irq_to_desc(irq
);
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 spin_lock_irqsave(&desc
->lock
, flags
);
37 desc
->status
= IRQ_DISABLED
;
38 desc
->chip
= &no_irq_chip
;
39 desc
->handle_irq
= handle_bad_irq
;
41 desc
->msi_desc
= NULL
;
42 desc
->handler_data
= NULL
;
43 desc
->chip_data
= NULL
;
46 desc
->irqs_unhandled
= 0;
48 cpus_setall(desc
->affinity
);
50 spin_unlock_irqrestore(&desc
->lock
, flags
);
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
);
63 WARN(1, KERN_ERR
"Trying to cleanup invalid IRQ%d\n", irq
);
67 spin_lock_irqsave(&desc
->lock
, flags
);
69 spin_unlock_irqrestore(&desc
->lock
, flags
);
70 WARN(1, KERN_ERR
"Destroying IRQ%d without calling free_irq\n",
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
;
80 spin_unlock_irqrestore(&desc
->lock
, flags
);
85 * set_irq_chip - set the irq chip for an irq
87 * @chip: pointer to irq chip description structure
89 int set_irq_chip(unsigned int irq
, struct irq_chip
*chip
)
91 struct irq_desc
*desc
= irq_to_desc(irq
);
95 WARN(1, KERN_ERR
"Trying to install chip for IRQ%d\n", irq
);
102 spin_lock_irqsave(&desc
->lock
, flags
);
103 irq_chip_set_defaults(chip
);
105 spin_unlock_irqrestore(&desc
->lock
, flags
);
109 EXPORT_SYMBOL(set_irq_chip
);
112 * set_irq_type - set the irq trigger type for an irq
114 * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
116 int set_irq_type(unsigned int irq
, unsigned int type
)
118 struct irq_desc
*desc
= irq_to_desc(irq
);
123 printk(KERN_ERR
"Trying to set irq type for IRQ%d\n", irq
);
127 if (type
== IRQ_TYPE_NONE
)
130 spin_lock_irqsave(&desc
->lock
, flags
);
131 ret
= __irq_set_trigger(desc
, irq
, type
);
132 spin_unlock_irqrestore(&desc
->lock
, flags
);
135 EXPORT_SYMBOL(set_irq_type
);
138 * set_irq_data - set irq type data for an irq
139 * @irq: Interrupt number
140 * @data: Pointer to interrupt specific data
142 * Set the hardware irq controller data for an irq
144 int set_irq_data(unsigned int irq
, void *data
)
146 struct irq_desc
*desc
= irq_to_desc(irq
);
151 "Trying to install controller data for IRQ%d\n", irq
);
155 spin_lock_irqsave(&desc
->lock
, flags
);
156 desc
->handler_data
= data
;
157 spin_unlock_irqrestore(&desc
->lock
, flags
);
160 EXPORT_SYMBOL(set_irq_data
);
163 * set_irq_data - set irq type data for an irq
164 * @irq: Interrupt number
165 * @entry: Pointer to MSI descriptor data
167 * Set the hardware irq controller data for an irq
169 int set_irq_msi(unsigned int irq
, struct msi_desc
*entry
)
171 struct irq_desc
*desc
= irq_to_desc(irq
);
176 "Trying to install msi data for IRQ%d\n", irq
);
180 spin_lock_irqsave(&desc
->lock
, flags
);
181 desc
->msi_desc
= entry
;
184 spin_unlock_irqrestore(&desc
->lock
, flags
);
189 * set_irq_chip_data - set irq chip data for an irq
190 * @irq: Interrupt number
191 * @data: Pointer to chip specific data
193 * Set the hardware irq chip data for an irq
195 int set_irq_chip_data(unsigned int irq
, void *data
)
197 struct irq_desc
*desc
= irq_to_desc(irq
);
202 "Trying to install chip data for IRQ%d\n", irq
);
207 printk(KERN_ERR
"BUG: bad set_irq_chip_data(IRQ#%d)\n", irq
);
211 spin_lock_irqsave(&desc
->lock
, flags
);
212 desc
->chip_data
= data
;
213 spin_unlock_irqrestore(&desc
->lock
, flags
);
217 EXPORT_SYMBOL(set_irq_chip_data
);
220 * default enable function
222 static void default_enable(unsigned int irq
)
224 struct irq_desc
*desc
= irq_to_desc(irq
);
226 desc
->chip
->unmask(irq
);
227 desc
->status
&= ~IRQ_MASKED
;
231 * default disable function
233 static void default_disable(unsigned int irq
)
238 * default startup function
240 static unsigned int default_startup(unsigned int irq
)
242 struct irq_desc
*desc
= irq_to_desc(irq
);
244 desc
->chip
->enable(irq
);
249 * default shutdown function
251 static void default_shutdown(unsigned int irq
)
253 struct irq_desc
*desc
= irq_to_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
;
313 spin_lock(&desc
->lock
);
315 if (unlikely(desc
->status
& IRQ_INPROGRESS
))
317 desc
->status
&= ~(IRQ_REPLAY
| IRQ_WAITING
);
318 kstat_incr_irqs_this_cpu(irq
, desc
);
320 action
= desc
->action
;
321 if (unlikely(!action
|| (desc
->status
& IRQ_DISABLED
)))
324 desc
->status
|= IRQ_INPROGRESS
;
325 spin_unlock(&desc
->lock
);
327 action_ret
= handle_IRQ_event(irq
, action
);
329 note_interrupt(irq
, desc
, action_ret
);
331 spin_lock(&desc
->lock
);
332 desc
->status
&= ~IRQ_INPROGRESS
;
334 spin_unlock(&desc
->lock
);
338 * handle_level_irq - Level type irq handler
339 * @irq: the interrupt number
340 * @desc: the interrupt description structure for this irq
342 * Level type interrupts are active as long as the hardware line has
343 * the active level. This may require to mask the interrupt and unmask
344 * it after the associated handler has acknowledged the device, so the
345 * interrupt line is back to inactive.
348 handle_level_irq(unsigned int irq
, struct irq_desc
*desc
)
350 struct irqaction
*action
;
351 irqreturn_t action_ret
;
353 spin_lock(&desc
->lock
);
354 mask_ack_irq(desc
, irq
);
356 if (unlikely(desc
->status
& IRQ_INPROGRESS
))
358 desc
->status
&= ~(IRQ_REPLAY
| IRQ_WAITING
);
359 kstat_incr_irqs_this_cpu(irq
, desc
);
362 * If its disabled or no action available
363 * keep it masked and get out of here
365 action
= desc
->action
;
366 if (unlikely(!action
|| (desc
->status
& IRQ_DISABLED
)))
369 desc
->status
|= IRQ_INPROGRESS
;
370 spin_unlock(&desc
->lock
);
372 action_ret
= handle_IRQ_event(irq
, action
);
374 note_interrupt(irq
, desc
, action_ret
);
376 spin_lock(&desc
->lock
);
377 desc
->status
&= ~IRQ_INPROGRESS
;
378 if (!(desc
->status
& IRQ_DISABLED
) && desc
->chip
->unmask
)
379 desc
->chip
->unmask(irq
);
381 spin_unlock(&desc
->lock
);
385 * handle_fasteoi_irq - irq handler for transparent controllers
386 * @irq: the interrupt number
387 * @desc: the interrupt description structure for this irq
389 * Only a single callback will be issued to the chip: an ->eoi()
390 * call when the interrupt has been serviced. This enables support
391 * for modern forms of interrupt handlers, which handle the flow
392 * details in hardware, transparently.
395 handle_fasteoi_irq(unsigned int irq
, struct irq_desc
*desc
)
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_incr_irqs_this_cpu(irq
, desc
);
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 spin_lock(&desc
->lock
);
457 desc
->status
&= ~(IRQ_REPLAY
| IRQ_WAITING
);
460 * If we're currently running this IRQ, or its disabled,
461 * we shouldn't process the IRQ. Mark it pending, handle
462 * the necessary masking and go out
464 if (unlikely((desc
->status
& (IRQ_INPROGRESS
| IRQ_DISABLED
)) ||
466 desc
->status
|= (IRQ_PENDING
| IRQ_MASKED
);
467 mask_ack_irq(desc
, irq
);
470 kstat_incr_irqs_this_cpu(irq
, desc
);
472 /* Start handling the irq */
473 desc
->chip
->ack(irq
);
475 /* Mark the IRQ currently in progress.*/
476 desc
->status
|= IRQ_INPROGRESS
;
479 struct irqaction
*action
= desc
->action
;
480 irqreturn_t action_ret
;
482 if (unlikely(!action
)) {
483 desc
->chip
->mask(irq
);
488 * When another irq arrived while we were handling
489 * one, we could have masked the irq.
490 * Renable it, if it was not disabled in meantime.
492 if (unlikely((desc
->status
&
493 (IRQ_PENDING
| IRQ_MASKED
| IRQ_DISABLED
)) ==
494 (IRQ_PENDING
| IRQ_MASKED
))) {
495 desc
->chip
->unmask(irq
);
496 desc
->status
&= ~IRQ_MASKED
;
499 desc
->status
&= ~IRQ_PENDING
;
500 spin_unlock(&desc
->lock
);
501 action_ret
= handle_IRQ_event(irq
, action
);
503 note_interrupt(irq
, desc
, action_ret
);
504 spin_lock(&desc
->lock
);
506 } while ((desc
->status
& (IRQ_PENDING
| IRQ_DISABLED
)) == IRQ_PENDING
);
508 desc
->status
&= ~IRQ_INPROGRESS
;
510 spin_unlock(&desc
->lock
);
514 * handle_percpu_IRQ - Per CPU local irq handler
515 * @irq: the interrupt number
516 * @desc: the interrupt description structure for this irq
518 * Per CPU interrupts on SMP machines without locking requirements
521 handle_percpu_irq(unsigned int irq
, struct irq_desc
*desc
)
523 irqreturn_t action_ret
;
525 kstat_incr_irqs_this_cpu(irq
, desc
);
528 desc
->chip
->ack(irq
);
530 action_ret
= handle_IRQ_event(irq
, desc
->action
);
532 note_interrupt(irq
, desc
, action_ret
);
535 desc
->chip
->eoi(irq
);
539 __set_irq_handler(unsigned int irq
, irq_flow_handler_t handle
, int is_chained
,
542 struct irq_desc
*desc
= irq_to_desc(irq
);
547 "Trying to install type control for IRQ%d\n", irq
);
552 handle
= handle_bad_irq
;
553 else if (desc
->chip
== &no_irq_chip
) {
554 printk(KERN_WARNING
"Trying to install %sinterrupt handler "
555 "for IRQ%d\n", is_chained
? "chained " : "", irq
);
557 * Some ARM implementations install a handler for really dumb
558 * interrupt hardware without setting an irq_chip. This worked
559 * with the ARM no_irq_chip but the check in setup_irq would
560 * prevent us to setup the interrupt at all. Switch it to
561 * dummy_irq_chip for easy transition.
563 desc
->chip
= &dummy_irq_chip
;
566 spin_lock_irqsave(&desc
->lock
, flags
);
569 if (handle
== handle_bad_irq
) {
570 if (desc
->chip
!= &no_irq_chip
)
571 mask_ack_irq(desc
, irq
);
572 desc
->status
|= IRQ_DISABLED
;
575 desc
->handle_irq
= handle
;
578 if (handle
!= handle_bad_irq
&& is_chained
) {
579 desc
->status
&= ~IRQ_DISABLED
;
580 desc
->status
|= IRQ_NOREQUEST
| IRQ_NOPROBE
;
582 desc
->chip
->startup(irq
);
584 spin_unlock_irqrestore(&desc
->lock
, flags
);
588 set_irq_chip_and_handler(unsigned int irq
, struct irq_chip
*chip
,
589 irq_flow_handler_t handle
)
591 set_irq_chip(irq
, chip
);
592 __set_irq_handler(irq
, handle
, 0, NULL
);
596 set_irq_chip_and_handler_name(unsigned int irq
, struct irq_chip
*chip
,
597 irq_flow_handler_t handle
, const char *name
)
599 set_irq_chip(irq
, chip
);
600 __set_irq_handler(irq
, handle
, 0, name
);
603 void __init
set_irq_noprobe(unsigned int irq
)
605 struct irq_desc
*desc
= irq_to_desc(irq
);
609 printk(KERN_ERR
"Trying to mark IRQ%d non-probeable\n", irq
);
613 spin_lock_irqsave(&desc
->lock
, flags
);
614 desc
->status
|= IRQ_NOPROBE
;
615 spin_unlock_irqrestore(&desc
->lock
, flags
);
618 void __init
set_irq_probe(unsigned int irq
)
620 struct irq_desc
*desc
= irq_to_desc(irq
);
624 printk(KERN_ERR
"Trying to mark IRQ%d probeable\n", irq
);
628 spin_lock_irqsave(&desc
->lock
, flags
);
629 desc
->status
&= ~IRQ_NOPROBE
;
630 spin_unlock_irqrestore(&desc
->lock
, flags
);