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 * irq_set_chip - set the irq chip for an irq
24 * @chip: pointer to irq chip description structure
26 int irq_set_chip(unsigned int irq, struct irq_chip *chip)
28 struct irq_desc *desc = irq_to_desc(irq);
32 WARN(1, KERN_ERR "Trying to install chip for IRQ%d\n", irq);
39 raw_spin_lock_irqsave(&desc->lock, flags);
40 irq_chip_set_defaults(chip);
41 desc->irq_data.chip = chip;
42 raw_spin_unlock_irqrestore(&desc->lock, flags);
46 EXPORT_SYMBOL(irq_set_chip);
49 * irq_set_type - set the irq trigger type for an irq
51 * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
53 int irq_set_irq_type(unsigned int irq, unsigned int type)
55 struct irq_desc *desc = irq_to_desc(irq);
60 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
64 type &= IRQ_TYPE_SENSE_MASK;
65 if (type == IRQ_TYPE_NONE)
69 raw_spin_lock_irqsave(&desc->lock, flags);
70 ret = __irq_set_trigger(desc, irq, type);
71 raw_spin_unlock_irqrestore(&desc->lock, flags);
72 chip_bus_sync_unlock(desc);
75 EXPORT_SYMBOL(irq_set_irq_type);
78 * irq_set_handler_data - set irq handler data for an irq
79 * @irq: Interrupt number
80 * @data: Pointer to interrupt specific data
82 * Set the hardware irq controller data for an irq
84 int irq_set_handler_data(unsigned int irq, void *data)
86 struct irq_desc *desc = irq_to_desc(irq);
91 "Trying to install controller data for IRQ%d\n", irq);
95 raw_spin_lock_irqsave(&desc->lock, flags);
96 desc->irq_data.handler_data = data;
97 raw_spin_unlock_irqrestore(&desc->lock, flags);
100 EXPORT_SYMBOL(irq_set_handler_data);
103 * irq_set_msi_desc - set MSI descriptor data for an irq
104 * @irq: Interrupt number
105 * @entry: Pointer to MSI descriptor data
107 * Set the MSI descriptor entry for an irq
109 int irq_set_msi_desc(unsigned int irq, struct msi_desc *entry)
111 struct irq_desc *desc = irq_to_desc(irq);
116 "Trying to install msi data for IRQ%d\n", irq);
120 raw_spin_lock_irqsave(&desc->lock, flags);
121 desc->irq_data.msi_desc = entry;
124 raw_spin_unlock_irqrestore(&desc->lock, flags);
129 * irq_set_chip_data - set irq chip data for an irq
130 * @irq: Interrupt number
131 * @data: Pointer to chip specific data
133 * Set the hardware irq chip data for an irq
135 int irq_set_chip_data(unsigned int irq, void *data)
137 struct irq_desc *desc = irq_to_desc(irq);
142 "Trying to install chip data for IRQ%d\n", irq);
146 if (!desc->irq_data.chip) {
147 printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
151 raw_spin_lock_irqsave(&desc->lock, flags);
152 desc->irq_data.chip_data = data;
153 raw_spin_unlock_irqrestore(&desc->lock, flags);
157 EXPORT_SYMBOL(irq_set_chip_data);
159 struct irq_data *irq_get_irq_data(unsigned int irq)
161 struct irq_desc *desc = irq_to_desc(irq);
163 return desc ? &desc->irq_data : NULL;
165 EXPORT_SYMBOL_GPL(irq_get_irq_data);
168 * set_irq_nested_thread - Set/Reset the IRQ_NESTED_THREAD flag of an irq
170 * @irq: Interrupt number
171 * @nest: 0 to clear / 1 to set the IRQ_NESTED_THREAD flag
173 * The IRQ_NESTED_THREAD flag indicates that on
174 * request_threaded_irq() no separate interrupt thread should be
175 * created for the irq as the handler are called nested in the
176 * context of a demultiplexing interrupt handler thread.
178 void set_irq_nested_thread(unsigned int irq, int nest)
180 struct irq_desc *desc = irq_to_desc(irq);
186 raw_spin_lock_irqsave(&desc->lock, flags);
188 desc->status |= IRQ_NESTED_THREAD;
190 desc->status &= ~IRQ_NESTED_THREAD;
191 raw_spin_unlock_irqrestore(&desc->lock, flags);
193 EXPORT_SYMBOL_GPL(set_irq_nested_thread);
195 int irq_startup(struct irq_desc *desc)
197 desc->status &= ~(IRQ_MASKED | IRQ_DISABLED);
200 if (desc->irq_data.chip->irq_startup)
201 return desc->irq_data.chip->irq_startup(&desc->irq_data);
207 void irq_shutdown(struct irq_desc *desc)
209 desc->status |= IRQ_MASKED | IRQ_DISABLED;
211 desc->irq_data.chip->irq_shutdown(&desc->irq_data);
214 void irq_enable(struct irq_desc *desc)
216 desc->irq_data.chip->irq_enable(&desc->irq_data);
219 void irq_disable(struct irq_desc *desc)
221 desc->irq_data.chip->irq_disable(&desc->irq_data);
225 * default enable function
227 static void default_enable(struct irq_data *data)
229 struct irq_desc *desc = irq_data_to_desc(data);
231 desc->irq_data.chip->irq_unmask(&desc->irq_data);
232 desc->status &= ~IRQ_MASKED;
236 * default disable function
238 static void default_disable(struct irq_data *data)
243 * default shutdown function
245 static void default_shutdown(struct irq_data *data)
247 struct irq_desc *desc = irq_data_to_desc(data);
249 desc->irq_data.chip->irq_mask(&desc->irq_data);
252 #ifndef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED
253 /* Temporary migration helpers */
254 static void compat_irq_mask(struct irq_data *data)
256 data->chip->mask(data->irq);
259 static void compat_irq_unmask(struct irq_data *data)
261 data->chip->unmask(data->irq);
264 static void compat_irq_ack(struct irq_data *data)
266 data->chip->ack(data->irq);
269 static void compat_irq_mask_ack(struct irq_data *data)
271 data->chip->mask_ack(data->irq);
274 static void compat_irq_eoi(struct irq_data *data)
276 data->chip->eoi(data->irq);
279 static void compat_irq_enable(struct irq_data *data)
281 data->chip->enable(data->irq);
284 static void compat_irq_disable(struct irq_data *data)
286 data->chip->disable(data->irq);
289 static void compat_irq_shutdown(struct irq_data *data)
291 data->chip->shutdown(data->irq);
294 static unsigned int compat_irq_startup(struct irq_data *data)
296 return data->chip->startup(data->irq);
299 static int compat_irq_set_affinity(struct irq_data *data,
300 const struct cpumask *dest, bool force)
302 return data->chip->set_affinity(data->irq, dest);
305 static int compat_irq_set_type(struct irq_data *data, unsigned int type)
307 return data->chip->set_type(data->irq, type);
310 static int compat_irq_set_wake(struct irq_data *data, unsigned int on)
312 return data->chip->set_wake(data->irq, on);
315 static int compat_irq_retrigger(struct irq_data *data)
317 return data->chip->retrigger(data->irq);
320 static void compat_bus_lock(struct irq_data *data)
322 data->chip->bus_lock(data->irq);
325 static void compat_bus_sync_unlock(struct irq_data *data)
327 data->chip->bus_sync_unlock(data->irq);
332 * Fixup enable/disable function pointers
334 void irq_chip_set_defaults(struct irq_chip *chip)
336 #ifndef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED
338 * Compat fixup functions need to be before we set the
339 * defaults for enable/disable/startup/shutdown
342 chip->irq_enable = compat_irq_enable;
344 chip->irq_disable = compat_irq_disable;
346 chip->irq_shutdown = compat_irq_shutdown;
348 chip->irq_startup = compat_irq_startup;
353 if (!chip->irq_enable)
354 chip->irq_enable = default_enable;
355 if (!chip->irq_disable)
356 chip->irq_disable = default_disable;
358 * We use chip->irq_disable, when the user provided its own. When
359 * we have default_disable set for chip->irq_disable, then we need
360 * to use default_shutdown, otherwise the irq line is not
361 * disabled on free_irq():
363 if (!chip->irq_shutdown)
364 chip->irq_shutdown = chip->irq_disable != default_disable ?
365 chip->irq_disable : default_shutdown;
367 #ifndef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED
369 chip->end = dummy_irq_chip.end;
372 * Now fix up the remaining compat handlers
375 chip->irq_bus_lock = compat_bus_lock;
376 if (chip->bus_sync_unlock)
377 chip->irq_bus_sync_unlock = compat_bus_sync_unlock;
379 chip->irq_mask = compat_irq_mask;
381 chip->irq_unmask = compat_irq_unmask;
383 chip->irq_ack = compat_irq_ack;
385 chip->irq_mask_ack = compat_irq_mask_ack;
387 chip->irq_eoi = compat_irq_eoi;
388 if (chip->set_affinity)
389 chip->irq_set_affinity = compat_irq_set_affinity;
391 chip->irq_set_type = compat_irq_set_type;
393 chip->irq_set_wake = compat_irq_set_wake;
395 chip->irq_retrigger = compat_irq_retrigger;
399 static inline void mask_ack_irq(struct irq_desc *desc)
401 if (desc->irq_data.chip->irq_mask_ack)
402 desc->irq_data.chip->irq_mask_ack(&desc->irq_data);
404 desc->irq_data.chip->irq_mask(&desc->irq_data);
405 if (desc->irq_data.chip->irq_ack)
406 desc->irq_data.chip->irq_ack(&desc->irq_data);
408 desc->status |= IRQ_MASKED;
411 static inline void mask_irq(struct irq_desc *desc)
413 if (desc->irq_data.chip->irq_mask) {
414 desc->irq_data.chip->irq_mask(&desc->irq_data);
415 desc->status |= IRQ_MASKED;
419 static inline void unmask_irq(struct irq_desc *desc)
421 if (desc->irq_data.chip->irq_unmask) {
422 desc->irq_data.chip->irq_unmask(&desc->irq_data);
423 desc->status &= ~IRQ_MASKED;
428 * handle_nested_irq - Handle a nested irq from a irq thread
429 * @irq: the interrupt number
431 * Handle interrupts which are nested into a threaded interrupt
432 * handler. The handler function is called inside the calling
435 void handle_nested_irq(unsigned int irq)
437 struct irq_desc *desc = irq_to_desc(irq);
438 struct irqaction *action;
439 irqreturn_t action_ret;
443 raw_spin_lock_irq(&desc->lock);
445 kstat_incr_irqs_this_cpu(irq, desc);
447 action = desc->action;
448 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
451 desc->status |= IRQ_INPROGRESS;
452 raw_spin_unlock_irq(&desc->lock);
454 action_ret = action->thread_fn(action->irq, action->dev_id);
456 note_interrupt(irq, desc, action_ret);
458 raw_spin_lock_irq(&desc->lock);
459 desc->status &= ~IRQ_INPROGRESS;
462 raw_spin_unlock_irq(&desc->lock);
464 EXPORT_SYMBOL_GPL(handle_nested_irq);
466 static bool irq_check_poll(struct irq_desc *desc)
468 if (!(desc->status & IRQ_POLL_INPROGRESS))
470 return irq_wait_for_poll(desc);
474 * handle_simple_irq - Simple and software-decoded IRQs.
475 * @irq: the interrupt number
476 * @desc: the interrupt description structure for this irq
478 * Simple interrupts are either sent from a demultiplexing interrupt
479 * handler or come from hardware, where no interrupt hardware control
482 * Note: The caller is expected to handle the ack, clear, mask and
483 * unmask issues if necessary.
486 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
488 struct irqaction *action;
489 irqreturn_t action_ret;
491 raw_spin_lock(&desc->lock);
493 if (unlikely(desc->status & IRQ_INPROGRESS))
494 if (!irq_check_poll(desc))
497 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
498 kstat_incr_irqs_this_cpu(irq, desc);
500 action = desc->action;
501 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
504 desc->status |= IRQ_INPROGRESS;
505 raw_spin_unlock(&desc->lock);
507 action_ret = handle_IRQ_event(irq, action);
509 note_interrupt(irq, desc, action_ret);
511 raw_spin_lock(&desc->lock);
512 desc->status &= ~IRQ_INPROGRESS;
514 raw_spin_unlock(&desc->lock);
518 * handle_level_irq - Level type irq handler
519 * @irq: the interrupt number
520 * @desc: the interrupt description structure for this irq
522 * Level type interrupts are active as long as the hardware line has
523 * the active level. This may require to mask the interrupt and unmask
524 * it after the associated handler has acknowledged the device, so the
525 * interrupt line is back to inactive.
528 handle_level_irq(unsigned int irq, struct irq_desc *desc)
530 struct irqaction *action;
531 irqreturn_t action_ret;
533 raw_spin_lock(&desc->lock);
536 if (unlikely(desc->status & IRQ_INPROGRESS))
537 if (!irq_check_poll(desc))
540 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
541 kstat_incr_irqs_this_cpu(irq, desc);
544 * If its disabled or no action available
545 * keep it masked and get out of here
547 action = desc->action;
548 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
551 desc->status |= IRQ_INPROGRESS;
552 raw_spin_unlock(&desc->lock);
554 action_ret = handle_IRQ_event(irq, action);
556 note_interrupt(irq, desc, action_ret);
558 raw_spin_lock(&desc->lock);
559 desc->status &= ~IRQ_INPROGRESS;
561 if (!(desc->status & (IRQ_DISABLED | IRQ_ONESHOT)))
564 raw_spin_unlock(&desc->lock);
566 EXPORT_SYMBOL_GPL(handle_level_irq);
569 * handle_fasteoi_irq - irq handler for transparent controllers
570 * @irq: the interrupt number
571 * @desc: the interrupt description structure for this irq
573 * Only a single callback will be issued to the chip: an ->eoi()
574 * call when the interrupt has been serviced. This enables support
575 * for modern forms of interrupt handlers, which handle the flow
576 * details in hardware, transparently.
579 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
581 struct irqaction *action;
582 irqreturn_t action_ret;
584 raw_spin_lock(&desc->lock);
586 if (unlikely(desc->status & IRQ_INPROGRESS))
587 if (!irq_check_poll(desc))
590 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
591 kstat_incr_irqs_this_cpu(irq, desc);
594 * If its disabled or no action available
595 * then mask it and get out of here:
597 action = desc->action;
598 if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
599 desc->status |= IRQ_PENDING;
604 desc->status |= IRQ_INPROGRESS;
605 desc->status &= ~IRQ_PENDING;
606 raw_spin_unlock(&desc->lock);
608 action_ret = handle_IRQ_event(irq, action);
610 note_interrupt(irq, desc, action_ret);
612 raw_spin_lock(&desc->lock);
613 desc->status &= ~IRQ_INPROGRESS;
615 desc->irq_data.chip->irq_eoi(&desc->irq_data);
617 raw_spin_unlock(&desc->lock);
621 * handle_edge_irq - edge type IRQ handler
622 * @irq: the interrupt number
623 * @desc: the interrupt description structure for this irq
625 * Interrupt occures on the falling and/or rising edge of a hardware
626 * signal. The occurence is latched into the irq controller hardware
627 * and must be acked in order to be reenabled. After the ack another
628 * interrupt can happen on the same source even before the first one
629 * is handled by the associated event handler. If this happens it
630 * might be necessary to disable (mask) the interrupt depending on the
631 * controller hardware. This requires to reenable the interrupt inside
632 * of the loop which handles the interrupts which have arrived while
633 * the handler was running. If all pending interrupts are handled, the
637 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
639 raw_spin_lock(&desc->lock);
641 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
644 * If we're currently running this IRQ, or its disabled,
645 * we shouldn't process the IRQ. Mark it pending, handle
646 * the necessary masking and go out
648 if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
650 if (!irq_check_poll(desc)) {
651 desc->status |= (IRQ_PENDING | IRQ_MASKED);
656 kstat_incr_irqs_this_cpu(irq, desc);
658 /* Start handling the irq */
659 desc->irq_data.chip->irq_ack(&desc->irq_data);
661 /* Mark the IRQ currently in progress.*/
662 desc->status |= IRQ_INPROGRESS;
665 struct irqaction *action = desc->action;
666 irqreturn_t action_ret;
668 if (unlikely(!action)) {
674 * When another irq arrived while we were handling
675 * one, we could have masked the irq.
676 * Renable it, if it was not disabled in meantime.
678 if (unlikely((desc->status &
679 (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
680 (IRQ_PENDING | IRQ_MASKED))) {
684 desc->status &= ~IRQ_PENDING;
685 raw_spin_unlock(&desc->lock);
686 action_ret = handle_IRQ_event(irq, action);
688 note_interrupt(irq, desc, action_ret);
689 raw_spin_lock(&desc->lock);
691 } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
693 desc->status &= ~IRQ_INPROGRESS;
695 raw_spin_unlock(&desc->lock);
699 * handle_percpu_irq - Per CPU local irq handler
700 * @irq: the interrupt number
701 * @desc: the interrupt description structure for this irq
703 * Per CPU interrupts on SMP machines without locking requirements
706 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
708 irqreturn_t action_ret;
710 kstat_incr_irqs_this_cpu(irq, desc);
712 if (desc->irq_data.chip->irq_ack)
713 desc->irq_data.chip->irq_ack(&desc->irq_data);
715 action_ret = handle_IRQ_event(irq, desc->action);
717 note_interrupt(irq, desc, action_ret);
719 if (desc->irq_data.chip->irq_eoi)
720 desc->irq_data.chip->irq_eoi(&desc->irq_data);
724 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
727 struct irq_desc *desc = irq_to_desc(irq);
732 "Trying to install type control for IRQ%d\n", irq);
737 handle = handle_bad_irq;
738 else if (desc->irq_data.chip == &no_irq_chip) {
739 printk(KERN_WARNING "Trying to install %sinterrupt handler "
740 "for IRQ%d\n", is_chained ? "chained " : "", irq);
742 * Some ARM implementations install a handler for really dumb
743 * interrupt hardware without setting an irq_chip. This worked
744 * with the ARM no_irq_chip but the check in setup_irq would
745 * prevent us to setup the interrupt at all. Switch it to
746 * dummy_irq_chip for easy transition.
748 desc->irq_data.chip = &dummy_irq_chip;
752 raw_spin_lock_irqsave(&desc->lock, flags);
755 if (handle == handle_bad_irq) {
756 if (desc->irq_data.chip != &no_irq_chip)
758 desc->status |= IRQ_DISABLED;
761 desc->handle_irq = handle;
764 if (handle != handle_bad_irq && is_chained) {
765 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
768 raw_spin_unlock_irqrestore(&desc->lock, flags);
769 chip_bus_sync_unlock(desc);
771 EXPORT_SYMBOL_GPL(__set_irq_handler);
774 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
775 irq_flow_handler_t handle)
777 set_irq_chip(irq, chip);
778 __set_irq_handler(irq, handle, 0, NULL);
782 set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
783 irq_flow_handler_t handle, const char *name)
785 set_irq_chip(irq, chip);
786 __set_irq_handler(irq, handle, 0, name);
789 void irq_modify_status(unsigned int irq, unsigned long clr, unsigned long set)
791 struct irq_desc *desc = irq_to_desc(irq);
798 set &= IRQF_MODIFY_MASK;
799 clr &= IRQF_MODIFY_MASK;
801 raw_spin_lock_irqsave(&desc->lock, flags);
802 desc->status &= ~clr;
804 raw_spin_unlock_irqrestore(&desc->lock, flags);