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;
79 spin_unlock_irqrestore(&desc->lock, flags);
84 * set_irq_chip - set the irq chip for an irq
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);
94 WARN(1, KERN_ERR "Trying to install chip for IRQ%d\n", irq);
101 spin_lock_irqsave(&desc->lock, flags);
102 irq_chip_set_defaults(chip);
104 spin_unlock_irqrestore(&desc->lock, flags);
108 EXPORT_SYMBOL(set_irq_chip);
111 * set_irq_type - set the irq trigger type for an irq
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);
122 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
126 if (type == IRQ_TYPE_NONE)
129 spin_lock_irqsave(&desc->lock, flags);
130 ret = __irq_set_trigger(desc, irq, type);
131 spin_unlock_irqrestore(&desc->lock, flags);
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);
150 "Trying to install controller data for IRQ%d\n", irq);
154 spin_lock_irqsave(&desc->lock, flags);
155 desc->handler_data = data;
156 spin_unlock_irqrestore(&desc->lock, flags);
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);
175 "Trying to install msi data for IRQ%d\n", irq);
179 spin_lock_irqsave(&desc->lock, flags);
180 desc->msi_desc = entry;
183 spin_unlock_irqrestore(&desc->lock, flags);
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);
201 "Trying to install chip data for IRQ%d\n", irq);
206 printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
210 spin_lock_irqsave(&desc->lock, flags);
211 desc->chip_data = data;
212 spin_unlock_irqrestore(&desc->lock, flags);
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);
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)
264 chip->enable = default_enable;
266 chip->disable = default_disable;
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():
276 chip->shutdown = chip->disable != default_disable ?
277 chip->disable : default_shutdown;
279 chip->name = chip->typename;
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);
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
303 * Note: The caller is expected to handle the ack, clear, mask and
304 * unmask issues if necessary.
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))
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)))
323 desc->status |= IRQ_INPROGRESS;
324 spin_unlock(&desc->lock);
326 action_ret = handle_IRQ_event(irq, action);
328 note_interrupt(irq, desc, action_ret);
330 spin_lock(&desc->lock);
331 desc->status &= ~IRQ_INPROGRESS;
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.
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))
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)))
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 struct irqaction *action;
397 irqreturn_t action_ret;
399 spin_lock(&desc->lock);
401 if (unlikely(desc->status & IRQ_INPROGRESS))
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);
419 desc->status |= IRQ_INPROGRESS;
420 desc->status &= ~IRQ_PENDING;
421 spin_unlock(&desc->lock);
423 action_ret = handle_IRQ_event(irq, action);
425 note_interrupt(irq, desc, action_ret);
427 spin_lock(&desc->lock);
428 desc->status &= ~IRQ_INPROGRESS;
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
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)) ||
465 desc->status |= (IRQ_PENDING | IRQ_MASKED);
466 mask_ack_irq(desc, irq);
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;
478 struct irqaction *action = desc->action;
479 irqreturn_t action_ret;
481 if (unlikely(!action)) {
482 desc->chip->mask(irq);
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);
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;
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
520 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
522 irqreturn_t action_ret;
524 kstat_incr_irqs_this_cpu(irq, desc);
527 desc->chip->ack(irq);
529 action_ret = handle_IRQ_event(irq, desc->action);
531 note_interrupt(irq, desc, action_ret);
534 desc->chip->eoi(irq);
538 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
541 struct irq_desc *desc = irq_to_desc(irq);
546 "Trying to install type control for IRQ%d\n", irq);
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);
568 if (handle == handle_bad_irq) {
569 if (desc->chip != &no_irq_chip)
570 mask_ack_irq(desc, irq);
571 desc->status |= IRQ_DISABLED;
574 desc->handle_irq = handle;
577 if (handle != handle_bad_irq && is_chained) {
578 desc->status &= ~IRQ_DISABLED;
579 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
581 desc->chip->startup(irq);
583 spin_unlock_irqrestore(&desc->lock, flags);
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);
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);
608 printk(KERN_ERR "Trying to mark IRQ%d non-probeable\n", irq);
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);
623 printk(KERN_ERR "Trying to mark IRQ%d probeable\n", irq);
627 spin_lock_irqsave(&desc->lock, flags);
628 desc->status &= ~IRQ_NOPROBE;
629 spin_unlock_irqrestore(&desc->lock, flags);