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 * set_irq_chip - set the irq chip for an irq
24 * @chip: pointer to irq chip description structure
26 int set_irq_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(set_irq_chip);
49 * set_irq_type - set the irq trigger type for an irq
51 * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
53 int 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)
68 raw_spin_lock_irqsave(&desc->lock, flags);
69 ret = __irq_set_trigger(desc, irq, type);
70 raw_spin_unlock_irqrestore(&desc->lock, flags);
73 EXPORT_SYMBOL(set_irq_type);
76 * set_irq_data - set irq type data for an irq
77 * @irq: Interrupt number
78 * @data: Pointer to interrupt specific data
80 * Set the hardware irq controller data for an irq
82 int set_irq_data(unsigned int irq, void *data)
84 struct irq_desc *desc = irq_to_desc(irq);
89 "Trying to install controller data for IRQ%d\n", irq);
93 raw_spin_lock_irqsave(&desc->lock, flags);
94 desc->irq_data.handler_data = data;
95 raw_spin_unlock_irqrestore(&desc->lock, flags);
98 EXPORT_SYMBOL(set_irq_data);
101 * set_irq_msi - set MSI descriptor data for an irq
102 * @irq: Interrupt number
103 * @entry: Pointer to MSI descriptor data
105 * Set the MSI descriptor entry for an irq
107 int set_irq_msi(unsigned int irq, struct msi_desc *entry)
109 struct irq_desc *desc = irq_to_desc(irq);
114 "Trying to install msi data for IRQ%d\n", irq);
118 raw_spin_lock_irqsave(&desc->lock, flags);
119 desc->irq_data.msi_desc = entry;
122 raw_spin_unlock_irqrestore(&desc->lock, flags);
127 * set_irq_chip_data - set irq chip data for an irq
128 * @irq: Interrupt number
129 * @data: Pointer to chip specific data
131 * Set the hardware irq chip data for an irq
133 int set_irq_chip_data(unsigned int irq, void *data)
135 struct irq_desc *desc = irq_to_desc(irq);
140 "Trying to install chip data for IRQ%d\n", irq);
144 if (!desc->irq_data.chip) {
145 printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
149 raw_spin_lock_irqsave(&desc->lock, flags);
150 desc->irq_data.chip_data = data;
151 raw_spin_unlock_irqrestore(&desc->lock, flags);
155 EXPORT_SYMBOL(set_irq_chip_data);
157 struct irq_data *irq_get_irq_data(unsigned int irq)
159 struct irq_desc *desc = irq_to_desc(irq);
161 return desc ? &desc->irq_data : NULL;
163 EXPORT_SYMBOL_GPL(irq_get_irq_data);
166 * set_irq_nested_thread - Set/Reset the IRQ_NESTED_THREAD flag of an irq
168 * @irq: Interrupt number
169 * @nest: 0 to clear / 1 to set the IRQ_NESTED_THREAD flag
171 * The IRQ_NESTED_THREAD flag indicates that on
172 * request_threaded_irq() no separate interrupt thread should be
173 * created for the irq as the handler are called nested in the
174 * context of a demultiplexing interrupt handler thread.
176 void set_irq_nested_thread(unsigned int irq, int nest)
178 struct irq_desc *desc = irq_to_desc(irq);
184 raw_spin_lock_irqsave(&desc->lock, flags);
186 desc->status |= IRQ_NESTED_THREAD;
188 desc->status &= ~IRQ_NESTED_THREAD;
189 raw_spin_unlock_irqrestore(&desc->lock, flags);
191 EXPORT_SYMBOL_GPL(set_irq_nested_thread);
194 * default enable function
196 static void default_enable(struct irq_data *data)
198 struct irq_desc *desc = irq_data_to_desc(data);
200 desc->irq_data.chip->irq_unmask(&desc->irq_data);
201 desc->status &= ~IRQ_MASKED;
205 * default disable function
207 static void default_disable(struct irq_data *data)
212 * default startup function
214 static unsigned int default_startup(struct irq_data *data)
216 struct irq_desc *desc = irq_data_to_desc(data);
218 desc->irq_data.chip->irq_enable(data);
223 * default shutdown function
225 static void default_shutdown(struct irq_data *data)
227 struct irq_desc *desc = irq_data_to_desc(data);
229 desc->irq_data.chip->irq_mask(&desc->irq_data);
230 desc->status |= IRQ_MASKED;
233 #ifndef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED
234 /* Temporary migration helpers */
235 static void compat_irq_mask(struct irq_data *data)
237 data->chip->mask(data->irq);
240 static void compat_irq_unmask(struct irq_data *data)
242 data->chip->unmask(data->irq);
245 static void compat_irq_ack(struct irq_data *data)
247 data->chip->ack(data->irq);
250 static void compat_irq_mask_ack(struct irq_data *data)
252 data->chip->mask_ack(data->irq);
255 static void compat_irq_eoi(struct irq_data *data)
257 data->chip->eoi(data->irq);
260 static void compat_irq_enable(struct irq_data *data)
262 data->chip->enable(data->irq);
265 static void compat_irq_disable(struct irq_data *data)
267 data->chip->disable(data->irq);
270 static void compat_irq_shutdown(struct irq_data *data)
272 data->chip->shutdown(data->irq);
275 static unsigned int compat_irq_startup(struct irq_data *data)
277 return data->chip->startup(data->irq);
280 static int compat_irq_set_affinity(struct irq_data *data,
281 const struct cpumask *dest, bool force)
283 return data->chip->set_affinity(data->irq, dest);
286 static int compat_irq_set_type(struct irq_data *data, unsigned int type)
288 return data->chip->set_type(data->irq, type);
291 static int compat_irq_set_wake(struct irq_data *data, unsigned int on)
293 return data->chip->set_wake(data->irq, on);
296 static int compat_irq_retrigger(struct irq_data *data)
298 return data->chip->retrigger(data->irq);
301 static void compat_bus_lock(struct irq_data *data)
303 data->chip->bus_lock(data->irq);
306 static void compat_bus_sync_unlock(struct irq_data *data)
308 data->chip->bus_sync_unlock(data->irq);
313 * Fixup enable/disable function pointers
315 void irq_chip_set_defaults(struct irq_chip *chip)
317 #ifndef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED
319 * Compat fixup functions need to be before we set the
320 * defaults for enable/disable/startup/shutdown
323 chip->irq_enable = compat_irq_enable;
325 chip->irq_disable = compat_irq_disable;
327 chip->irq_shutdown = compat_irq_shutdown;
329 chip->irq_startup = compat_irq_startup;
334 if (!chip->irq_enable)
335 chip->irq_enable = default_enable;
336 if (!chip->irq_disable)
337 chip->irq_disable = default_disable;
338 if (!chip->irq_startup)
339 chip->irq_startup = default_startup;
341 * We use chip->irq_disable, when the user provided its own. When
342 * we have default_disable set for chip->irq_disable, then we need
343 * to use default_shutdown, otherwise the irq line is not
344 * disabled on free_irq():
346 if (!chip->irq_shutdown)
347 chip->irq_shutdown = chip->irq_disable != default_disable ?
348 chip->irq_disable : default_shutdown;
350 #ifndef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED
352 chip->end = dummy_irq_chip.end;
355 * Now fix up the remaining compat handlers
358 chip->irq_bus_lock = compat_bus_lock;
359 if (chip->bus_sync_unlock)
360 chip->irq_bus_sync_unlock = compat_bus_sync_unlock;
362 chip->irq_mask = compat_irq_mask;
364 chip->irq_unmask = compat_irq_unmask;
366 chip->irq_ack = compat_irq_ack;
368 chip->irq_mask_ack = compat_irq_mask_ack;
370 chip->irq_eoi = compat_irq_eoi;
371 if (chip->set_affinity)
372 chip->irq_set_affinity = compat_irq_set_affinity;
374 chip->irq_set_type = compat_irq_set_type;
376 chip->irq_set_wake = compat_irq_set_wake;
378 chip->irq_retrigger = compat_irq_retrigger;
382 static inline void mask_ack_irq(struct irq_desc *desc)
384 if (desc->irq_data.chip->irq_mask_ack)
385 desc->irq_data.chip->irq_mask_ack(&desc->irq_data);
387 desc->irq_data.chip->irq_mask(&desc->irq_data);
388 if (desc->irq_data.chip->irq_ack)
389 desc->irq_data.chip->irq_ack(&desc->irq_data);
391 desc->status |= IRQ_MASKED;
394 static inline void mask_irq(struct irq_desc *desc)
396 if (desc->irq_data.chip->irq_mask) {
397 desc->irq_data.chip->irq_mask(&desc->irq_data);
398 desc->status |= IRQ_MASKED;
402 static inline void unmask_irq(struct irq_desc *desc)
404 if (desc->irq_data.chip->irq_unmask) {
405 desc->irq_data.chip->irq_unmask(&desc->irq_data);
406 desc->status &= ~IRQ_MASKED;
411 * handle_nested_irq - Handle a nested irq from a irq thread
412 * @irq: the interrupt number
414 * Handle interrupts which are nested into a threaded interrupt
415 * handler. The handler function is called inside the calling
418 void handle_nested_irq(unsigned int irq)
420 struct irq_desc *desc = irq_to_desc(irq);
421 struct irqaction *action;
422 irqreturn_t action_ret;
426 raw_spin_lock_irq(&desc->lock);
428 kstat_incr_irqs_this_cpu(irq, desc);
430 action = desc->action;
431 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
434 desc->status |= IRQ_INPROGRESS;
435 raw_spin_unlock_irq(&desc->lock);
437 action_ret = action->thread_fn(action->irq, action->dev_id);
439 note_interrupt(irq, desc, action_ret);
441 raw_spin_lock_irq(&desc->lock);
442 desc->status &= ~IRQ_INPROGRESS;
445 raw_spin_unlock_irq(&desc->lock);
447 EXPORT_SYMBOL_GPL(handle_nested_irq);
450 * handle_simple_irq - Simple and software-decoded IRQs.
451 * @irq: the interrupt number
452 * @desc: the interrupt description structure for this irq
454 * Simple interrupts are either sent from a demultiplexing interrupt
455 * handler or come from hardware, where no interrupt hardware control
458 * Note: The caller is expected to handle the ack, clear, mask and
459 * unmask issues if necessary.
462 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
464 struct irqaction *action;
465 irqreturn_t action_ret;
467 raw_spin_lock(&desc->lock);
469 if (unlikely(desc->status & IRQ_INPROGRESS))
471 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
472 kstat_incr_irqs_this_cpu(irq, desc);
474 action = desc->action;
475 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
478 desc->status |= IRQ_INPROGRESS;
479 raw_spin_unlock(&desc->lock);
481 action_ret = handle_IRQ_event(irq, action);
483 note_interrupt(irq, desc, action_ret);
485 raw_spin_lock(&desc->lock);
486 desc->status &= ~IRQ_INPROGRESS;
488 raw_spin_unlock(&desc->lock);
492 * handle_level_irq - Level type irq handler
493 * @irq: the interrupt number
494 * @desc: the interrupt description structure for this irq
496 * Level type interrupts are active as long as the hardware line has
497 * the active level. This may require to mask the interrupt and unmask
498 * it after the associated handler has acknowledged the device, so the
499 * interrupt line is back to inactive.
502 handle_level_irq(unsigned int irq, struct irq_desc *desc)
504 struct irqaction *action;
505 irqreturn_t action_ret;
507 raw_spin_lock(&desc->lock);
510 if (unlikely(desc->status & IRQ_INPROGRESS))
512 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
513 kstat_incr_irqs_this_cpu(irq, desc);
516 * If its disabled or no action available
517 * keep it masked and get out of here
519 action = desc->action;
520 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
523 desc->status |= IRQ_INPROGRESS;
524 raw_spin_unlock(&desc->lock);
526 action_ret = handle_IRQ_event(irq, action);
528 note_interrupt(irq, desc, action_ret);
530 raw_spin_lock(&desc->lock);
531 desc->status &= ~IRQ_INPROGRESS;
533 if (!(desc->status & (IRQ_DISABLED | IRQ_ONESHOT)))
536 raw_spin_unlock(&desc->lock);
538 EXPORT_SYMBOL_GPL(handle_level_irq);
541 * handle_fasteoi_irq - irq handler for transparent controllers
542 * @irq: the interrupt number
543 * @desc: the interrupt description structure for this irq
545 * Only a single callback will be issued to the chip: an ->eoi()
546 * call when the interrupt has been serviced. This enables support
547 * for modern forms of interrupt handlers, which handle the flow
548 * details in hardware, transparently.
551 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
553 struct irqaction *action;
554 irqreturn_t action_ret;
556 raw_spin_lock(&desc->lock);
558 if (unlikely(desc->status & IRQ_INPROGRESS))
561 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
562 kstat_incr_irqs_this_cpu(irq, desc);
565 * If its disabled or no action available
566 * then mask it and get out of here:
568 action = desc->action;
569 if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
570 desc->status |= IRQ_PENDING;
575 desc->status |= IRQ_INPROGRESS;
576 desc->status &= ~IRQ_PENDING;
577 raw_spin_unlock(&desc->lock);
579 action_ret = handle_IRQ_event(irq, action);
581 note_interrupt(irq, desc, action_ret);
583 raw_spin_lock(&desc->lock);
584 desc->status &= ~IRQ_INPROGRESS;
586 desc->irq_data.chip->irq_eoi(&desc->irq_data);
588 raw_spin_unlock(&desc->lock);
592 * handle_edge_irq - edge type IRQ handler
593 * @irq: the interrupt number
594 * @desc: the interrupt description structure for this irq
596 * Interrupt occures on the falling and/or rising edge of a hardware
597 * signal. The occurence is latched into the irq controller hardware
598 * and must be acked in order to be reenabled. After the ack another
599 * interrupt can happen on the same source even before the first one
600 * is handled by the associated event handler. If this happens it
601 * might be necessary to disable (mask) the interrupt depending on the
602 * controller hardware. This requires to reenable the interrupt inside
603 * of the loop which handles the interrupts which have arrived while
604 * the handler was running. If all pending interrupts are handled, the
608 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
610 raw_spin_lock(&desc->lock);
612 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
615 * If we're currently running this IRQ, or its disabled,
616 * we shouldn't process the IRQ. Mark it pending, handle
617 * the necessary masking and go out
619 if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
621 desc->status |= (IRQ_PENDING | IRQ_MASKED);
625 kstat_incr_irqs_this_cpu(irq, desc);
627 /* Start handling the irq */
628 desc->irq_data.chip->irq_ack(&desc->irq_data);
630 /* Mark the IRQ currently in progress.*/
631 desc->status |= IRQ_INPROGRESS;
634 struct irqaction *action = desc->action;
635 irqreturn_t action_ret;
637 if (unlikely(!action)) {
643 * When another irq arrived while we were handling
644 * one, we could have masked the irq.
645 * Renable it, if it was not disabled in meantime.
647 if (unlikely((desc->status &
648 (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
649 (IRQ_PENDING | IRQ_MASKED))) {
653 desc->status &= ~IRQ_PENDING;
654 raw_spin_unlock(&desc->lock);
655 action_ret = handle_IRQ_event(irq, action);
657 note_interrupt(irq, desc, action_ret);
658 raw_spin_lock(&desc->lock);
660 } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
662 desc->status &= ~IRQ_INPROGRESS;
664 raw_spin_unlock(&desc->lock);
668 * handle_percpu_irq - Per CPU local irq handler
669 * @irq: the interrupt number
670 * @desc: the interrupt description structure for this irq
672 * Per CPU interrupts on SMP machines without locking requirements
675 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
677 irqreturn_t action_ret;
679 kstat_incr_irqs_this_cpu(irq, desc);
681 if (desc->irq_data.chip->irq_ack)
682 desc->irq_data.chip->irq_ack(&desc->irq_data);
684 action_ret = handle_IRQ_event(irq, desc->action);
686 note_interrupt(irq, desc, action_ret);
688 if (desc->irq_data.chip->irq_eoi)
689 desc->irq_data.chip->irq_eoi(&desc->irq_data);
693 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
696 struct irq_desc *desc = irq_to_desc(irq);
701 "Trying to install type control for IRQ%d\n", irq);
706 handle = handle_bad_irq;
707 else if (desc->irq_data.chip == &no_irq_chip) {
708 printk(KERN_WARNING "Trying to install %sinterrupt handler "
709 "for IRQ%d\n", is_chained ? "chained " : "", irq);
711 * Some ARM implementations install a handler for really dumb
712 * interrupt hardware without setting an irq_chip. This worked
713 * with the ARM no_irq_chip but the check in setup_irq would
714 * prevent us to setup the interrupt at all. Switch it to
715 * dummy_irq_chip for easy transition.
717 desc->irq_data.chip = &dummy_irq_chip;
721 raw_spin_lock_irqsave(&desc->lock, flags);
724 if (handle == handle_bad_irq) {
725 if (desc->irq_data.chip != &no_irq_chip)
727 desc->status |= IRQ_DISABLED;
730 desc->handle_irq = handle;
733 if (handle != handle_bad_irq && is_chained) {
734 desc->status &= ~IRQ_DISABLED;
735 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
737 desc->irq_data.chip->irq_startup(&desc->irq_data);
739 raw_spin_unlock_irqrestore(&desc->lock, flags);
740 chip_bus_sync_unlock(desc);
742 EXPORT_SYMBOL_GPL(__set_irq_handler);
745 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
746 irq_flow_handler_t handle)
748 set_irq_chip(irq, chip);
749 __set_irq_handler(irq, handle, 0, NULL);
753 set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
754 irq_flow_handler_t handle, const char *name)
756 set_irq_chip(irq, chip);
757 __set_irq_handler(irq, handle, 0, name);
760 void irq_modify_status(unsigned int irq, unsigned long clr, unsigned long set)
762 struct irq_desc *desc = irq_to_desc(irq);
769 set &= IRQF_MODIFY_MASK;
770 clr &= IRQF_MODIFY_MASK;
772 raw_spin_lock_irqsave(&desc->lock, flags);
773 desc->status &= ~clr;
775 raw_spin_unlock_irqrestore(&desc->lock, flags);