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)
29 struct irq_desc *desc = irq_get_desc_lock(irq, &flags);
37 irq_chip_set_defaults(chip);
38 desc->irq_data.chip = chip;
39 irq_put_desc_unlock(desc, flags);
41 * For !CONFIG_SPARSE_IRQ make the irq show up in
42 * allocated_irqs. For the CONFIG_SPARSE_IRQ case, it is
43 * already marked, and this call is harmless.
48 EXPORT_SYMBOL(irq_set_chip);
51 * irq_set_type - set the irq trigger type for an irq
53 * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
55 int irq_set_irq_type(unsigned int irq, unsigned int type)
58 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags);
64 type &= IRQ_TYPE_SENSE_MASK;
65 if (type != IRQ_TYPE_NONE)
66 ret = __irq_set_trigger(desc, irq, type);
67 irq_put_desc_busunlock(desc, flags);
70 EXPORT_SYMBOL(irq_set_irq_type);
73 * irq_set_handler_data - set irq handler data for an irq
74 * @irq: Interrupt number
75 * @data: Pointer to interrupt specific data
77 * Set the hardware irq controller data for an irq
79 int irq_set_handler_data(unsigned int irq, void *data)
82 struct irq_desc *desc = irq_get_desc_lock(irq, &flags);
86 desc->irq_data.handler_data = data;
87 irq_put_desc_unlock(desc, flags);
90 EXPORT_SYMBOL(irq_set_handler_data);
93 * irq_set_msi_desc - set MSI descriptor data for an irq
94 * @irq: Interrupt number
95 * @entry: Pointer to MSI descriptor data
97 * Set the MSI descriptor entry for an irq
99 int irq_set_msi_desc(unsigned int irq, struct msi_desc *entry)
102 struct irq_desc *desc = irq_get_desc_lock(irq, &flags);
106 desc->irq_data.msi_desc = entry;
109 irq_put_desc_unlock(desc, flags);
114 * irq_set_chip_data - set irq chip data for an irq
115 * @irq: Interrupt number
116 * @data: Pointer to chip specific data
118 * Set the hardware irq chip data for an irq
120 int irq_set_chip_data(unsigned int irq, void *data)
123 struct irq_desc *desc = irq_get_desc_lock(irq, &flags);
127 desc->irq_data.chip_data = data;
128 irq_put_desc_unlock(desc, flags);
131 EXPORT_SYMBOL(irq_set_chip_data);
133 struct irq_data *irq_get_irq_data(unsigned int irq)
135 struct irq_desc *desc = irq_to_desc(irq);
137 return desc ? &desc->irq_data : NULL;
139 EXPORT_SYMBOL_GPL(irq_get_irq_data);
141 static void irq_state_clr_disabled(struct irq_desc *desc)
143 irqd_clear(&desc->irq_data, IRQD_IRQ_DISABLED);
144 irq_compat_clr_disabled(desc);
147 static void irq_state_set_disabled(struct irq_desc *desc)
149 irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
150 irq_compat_set_disabled(desc);
153 static void irq_state_clr_masked(struct irq_desc *desc)
155 irqd_clear(&desc->irq_data, IRQD_IRQ_MASKED);
156 irq_compat_clr_masked(desc);
159 static void irq_state_set_masked(struct irq_desc *desc)
161 irqd_set(&desc->irq_data, IRQD_IRQ_MASKED);
162 irq_compat_set_masked(desc);
165 int irq_startup(struct irq_desc *desc)
167 irq_state_clr_disabled(desc);
170 if (desc->irq_data.chip->irq_startup) {
171 int ret = desc->irq_data.chip->irq_startup(&desc->irq_data);
172 irq_state_clr_masked(desc);
180 void irq_shutdown(struct irq_desc *desc)
182 irq_state_set_disabled(desc);
184 if (desc->irq_data.chip->irq_shutdown)
185 desc->irq_data.chip->irq_shutdown(&desc->irq_data);
186 if (desc->irq_data.chip->irq_disable)
187 desc->irq_data.chip->irq_disable(&desc->irq_data);
189 desc->irq_data.chip->irq_mask(&desc->irq_data);
190 irq_state_set_masked(desc);
193 void irq_enable(struct irq_desc *desc)
195 irq_state_clr_disabled(desc);
196 if (desc->irq_data.chip->irq_enable)
197 desc->irq_data.chip->irq_enable(&desc->irq_data);
199 desc->irq_data.chip->irq_unmask(&desc->irq_data);
200 irq_state_clr_masked(desc);
203 void irq_disable(struct irq_desc *desc)
205 irq_state_set_disabled(desc);
206 if (desc->irq_data.chip->irq_disable) {
207 desc->irq_data.chip->irq_disable(&desc->irq_data);
208 irq_state_set_masked(desc);
212 #ifndef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED
213 /* Temporary migration helpers */
214 static void compat_irq_mask(struct irq_data *data)
216 data->chip->mask(data->irq);
219 static void compat_irq_unmask(struct irq_data *data)
221 data->chip->unmask(data->irq);
224 static void compat_irq_ack(struct irq_data *data)
226 data->chip->ack(data->irq);
229 static void compat_irq_mask_ack(struct irq_data *data)
231 data->chip->mask_ack(data->irq);
234 static void compat_irq_eoi(struct irq_data *data)
236 data->chip->eoi(data->irq);
239 static void compat_irq_enable(struct irq_data *data)
241 data->chip->enable(data->irq);
244 static void compat_irq_disable(struct irq_data *data)
246 data->chip->disable(data->irq);
249 static void compat_irq_shutdown(struct irq_data *data)
251 data->chip->shutdown(data->irq);
254 static unsigned int compat_irq_startup(struct irq_data *data)
256 return data->chip->startup(data->irq);
259 static int compat_irq_set_affinity(struct irq_data *data,
260 const struct cpumask *dest, bool force)
262 return data->chip->set_affinity(data->irq, dest);
265 static int compat_irq_set_type(struct irq_data *data, unsigned int type)
267 return data->chip->set_type(data->irq, type);
270 static int compat_irq_set_wake(struct irq_data *data, unsigned int on)
272 return data->chip->set_wake(data->irq, on);
275 static int compat_irq_retrigger(struct irq_data *data)
277 return data->chip->retrigger(data->irq);
280 static void compat_bus_lock(struct irq_data *data)
282 data->chip->bus_lock(data->irq);
285 static void compat_bus_sync_unlock(struct irq_data *data)
287 data->chip->bus_sync_unlock(data->irq);
292 * Fixup enable/disable function pointers
294 void irq_chip_set_defaults(struct irq_chip *chip)
296 #ifndef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED
298 chip->irq_enable = compat_irq_enable;
300 chip->irq_disable = compat_irq_disable;
302 chip->irq_shutdown = compat_irq_shutdown;
304 chip->irq_startup = compat_irq_startup;
306 chip->end = dummy_irq_chip.end;
308 chip->irq_bus_lock = compat_bus_lock;
309 if (chip->bus_sync_unlock)
310 chip->irq_bus_sync_unlock = compat_bus_sync_unlock;
312 chip->irq_mask = compat_irq_mask;
314 chip->irq_unmask = compat_irq_unmask;
316 chip->irq_ack = compat_irq_ack;
318 chip->irq_mask_ack = compat_irq_mask_ack;
320 chip->irq_eoi = compat_irq_eoi;
321 if (chip->set_affinity)
322 chip->irq_set_affinity = compat_irq_set_affinity;
324 chip->irq_set_type = compat_irq_set_type;
326 chip->irq_set_wake = compat_irq_set_wake;
328 chip->irq_retrigger = compat_irq_retrigger;
332 static inline void mask_ack_irq(struct irq_desc *desc)
334 if (desc->irq_data.chip->irq_mask_ack)
335 desc->irq_data.chip->irq_mask_ack(&desc->irq_data);
337 desc->irq_data.chip->irq_mask(&desc->irq_data);
338 if (desc->irq_data.chip->irq_ack)
339 desc->irq_data.chip->irq_ack(&desc->irq_data);
341 irq_state_set_masked(desc);
344 void mask_irq(struct irq_desc *desc)
346 if (desc->irq_data.chip->irq_mask) {
347 desc->irq_data.chip->irq_mask(&desc->irq_data);
348 irq_state_set_masked(desc);
352 void unmask_irq(struct irq_desc *desc)
354 if (desc->irq_data.chip->irq_unmask) {
355 desc->irq_data.chip->irq_unmask(&desc->irq_data);
356 irq_state_clr_masked(desc);
361 * handle_nested_irq - Handle a nested irq from a irq thread
362 * @irq: the interrupt number
364 * Handle interrupts which are nested into a threaded interrupt
365 * handler. The handler function is called inside the calling
368 void handle_nested_irq(unsigned int irq)
370 struct irq_desc *desc = irq_to_desc(irq);
371 struct irqaction *action;
372 irqreturn_t action_ret;
376 raw_spin_lock_irq(&desc->lock);
378 kstat_incr_irqs_this_cpu(irq, desc);
380 action = desc->action;
381 if (unlikely(!action || irqd_irq_disabled(&desc->irq_data)))
384 irq_compat_set_progress(desc);
385 irqd_set(&desc->irq_data, IRQD_IRQ_INPROGRESS);
386 raw_spin_unlock_irq(&desc->lock);
388 action_ret = action->thread_fn(action->irq, action->dev_id);
390 note_interrupt(irq, desc, action_ret);
392 raw_spin_lock_irq(&desc->lock);
393 irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
394 irq_compat_clr_progress(desc);
397 raw_spin_unlock_irq(&desc->lock);
399 EXPORT_SYMBOL_GPL(handle_nested_irq);
401 static bool irq_check_poll(struct irq_desc *desc)
403 if (!(desc->istate & IRQS_POLL_INPROGRESS))
405 return irq_wait_for_poll(desc);
409 * handle_simple_irq - Simple and software-decoded IRQs.
410 * @irq: the interrupt number
411 * @desc: the interrupt description structure for this irq
413 * Simple interrupts are either sent from a demultiplexing interrupt
414 * handler or come from hardware, where no interrupt hardware control
417 * Note: The caller is expected to handle the ack, clear, mask and
418 * unmask issues if necessary.
421 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
423 raw_spin_lock(&desc->lock);
425 if (unlikely(irqd_irq_inprogress(&desc->irq_data)))
426 if (!irq_check_poll(desc))
429 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
430 kstat_incr_irqs_this_cpu(irq, desc);
432 if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data)))
435 handle_irq_event(desc);
438 raw_spin_unlock(&desc->lock);
442 * handle_level_irq - Level type irq handler
443 * @irq: the interrupt number
444 * @desc: the interrupt description structure for this irq
446 * Level type interrupts are active as long as the hardware line has
447 * the active level. This may require to mask the interrupt and unmask
448 * it after the associated handler has acknowledged the device, so the
449 * interrupt line is back to inactive.
452 handle_level_irq(unsigned int irq, struct irq_desc *desc)
454 raw_spin_lock(&desc->lock);
457 if (unlikely(irqd_irq_inprogress(&desc->irq_data)))
458 if (!irq_check_poll(desc))
461 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
462 kstat_incr_irqs_this_cpu(irq, desc);
465 * If its disabled or no action available
466 * keep it masked and get out of here
468 if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data)))
471 handle_irq_event(desc);
473 if (!irqd_irq_disabled(&desc->irq_data) && !(desc->istate & IRQS_ONESHOT))
476 raw_spin_unlock(&desc->lock);
478 EXPORT_SYMBOL_GPL(handle_level_irq);
480 #ifdef CONFIG_IRQ_PREFLOW_FASTEOI
481 static inline void preflow_handler(struct irq_desc *desc)
483 if (desc->preflow_handler)
484 desc->preflow_handler(&desc->irq_data);
487 static inline void preflow_handler(struct irq_desc *desc) { }
491 * handle_fasteoi_irq - irq handler for transparent controllers
492 * @irq: the interrupt number
493 * @desc: the interrupt description structure for this irq
495 * Only a single callback will be issued to the chip: an ->eoi()
496 * call when the interrupt has been serviced. This enables support
497 * for modern forms of interrupt handlers, which handle the flow
498 * details in hardware, transparently.
501 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
503 raw_spin_lock(&desc->lock);
505 if (unlikely(irqd_irq_inprogress(&desc->irq_data)))
506 if (!irq_check_poll(desc))
509 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
510 kstat_incr_irqs_this_cpu(irq, desc);
513 * If its disabled or no action available
514 * then mask it and get out of here:
516 if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data))) {
517 irq_compat_set_pending(desc);
518 desc->istate |= IRQS_PENDING;
523 if (desc->istate & IRQS_ONESHOT)
526 preflow_handler(desc);
527 handle_irq_event(desc);
530 desc->irq_data.chip->irq_eoi(&desc->irq_data);
532 raw_spin_unlock(&desc->lock);
535 if (!(desc->irq_data.chip->flags & IRQCHIP_EOI_IF_HANDLED))
541 * handle_edge_irq - edge type IRQ handler
542 * @irq: the interrupt number
543 * @desc: the interrupt description structure for this irq
545 * Interrupt occures on the falling and/or rising edge of a hardware
546 * signal. The occurence is latched into the irq controller hardware
547 * and must be acked in order to be reenabled. After the ack another
548 * interrupt can happen on the same source even before the first one
549 * is handled by the associated event handler. If this happens it
550 * might be necessary to disable (mask) the interrupt depending on the
551 * controller hardware. This requires to reenable the interrupt inside
552 * of the loop which handles the interrupts which have arrived while
553 * the handler was running. If all pending interrupts are handled, the
557 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
559 raw_spin_lock(&desc->lock);
561 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
563 * If we're currently running this IRQ, or its disabled,
564 * we shouldn't process the IRQ. Mark it pending, handle
565 * the necessary masking and go out
567 if (unlikely(irqd_irq_disabled(&desc->irq_data) ||
568 irqd_irq_inprogress(&desc->irq_data) || !desc->action)) {
569 if (!irq_check_poll(desc)) {
570 irq_compat_set_pending(desc);
571 desc->istate |= IRQS_PENDING;
576 kstat_incr_irqs_this_cpu(irq, desc);
578 /* Start handling the irq */
579 desc->irq_data.chip->irq_ack(&desc->irq_data);
582 if (unlikely(!desc->action)) {
588 * When another irq arrived while we were handling
589 * one, we could have masked the irq.
590 * Renable it, if it was not disabled in meantime.
592 if (unlikely(desc->istate & IRQS_PENDING)) {
593 if (!irqd_irq_disabled(&desc->irq_data) &&
594 irqd_irq_masked(&desc->irq_data))
598 handle_irq_event(desc);
600 } while ((desc->istate & IRQS_PENDING) &&
601 !irqd_irq_disabled(&desc->irq_data));
604 raw_spin_unlock(&desc->lock);
608 * handle_percpu_irq - Per CPU local irq handler
609 * @irq: the interrupt number
610 * @desc: the interrupt description structure for this irq
612 * Per CPU interrupts on SMP machines without locking requirements
615 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
617 struct irq_chip *chip = irq_desc_get_chip(desc);
619 kstat_incr_irqs_this_cpu(irq, desc);
622 chip->irq_ack(&desc->irq_data);
624 handle_irq_event_percpu(desc, desc->action);
627 chip->irq_eoi(&desc->irq_data);
631 __irq_set_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
635 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags);
641 handle = handle_bad_irq;
643 if (WARN_ON(desc->irq_data.chip == &no_irq_chip))
648 if (handle == handle_bad_irq) {
649 if (desc->irq_data.chip != &no_irq_chip)
651 irq_state_set_disabled(desc);
654 desc->handle_irq = handle;
657 if (handle != handle_bad_irq && is_chained) {
658 irq_settings_set_noprobe(desc);
659 irq_settings_set_norequest(desc);
663 irq_put_desc_busunlock(desc, flags);
665 EXPORT_SYMBOL_GPL(__irq_set_handler);
668 irq_set_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
669 irq_flow_handler_t handle, const char *name)
671 irq_set_chip(irq, chip);
672 __irq_set_handler(irq, handle, 0, name);
675 void irq_modify_status(unsigned int irq, unsigned long clr, unsigned long set)
678 struct irq_desc *desc = irq_get_desc_lock(irq, &flags);
682 irq_settings_clr_and_set(desc, clr, set);
684 irqd_clear(&desc->irq_data, IRQD_NO_BALANCING | IRQD_PER_CPU |
685 IRQD_TRIGGER_MASK | IRQD_LEVEL | IRQD_MOVE_PCNTXT);
686 if (irq_settings_has_no_balance_set(desc))
687 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
688 if (irq_settings_is_per_cpu(desc))
689 irqd_set(&desc->irq_data, IRQD_PER_CPU);
690 if (irq_settings_can_move_pcntxt(desc))
691 irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
693 irqd_set(&desc->irq_data, irq_settings_get_trigger_mask(desc));
695 irq_put_desc_unlock(desc, flags);
699 * irq_cpu_online - Invoke all irq_cpu_online functions.
701 * Iterate through all irqs and invoke the chip.irq_cpu_online()
704 void irq_cpu_online(void)
706 struct irq_desc *desc;
707 struct irq_chip *chip;
711 for_each_active_irq(irq) {
712 desc = irq_to_desc(irq);
716 raw_spin_lock_irqsave(&desc->lock, flags);
718 chip = irq_data_get_irq_chip(&desc->irq_data);
719 if (chip && chip->irq_cpu_online &&
720 (!(chip->flags & IRQCHIP_ONOFFLINE_ENABLED) ||
721 !irqd_irq_disabled(&desc->irq_data)))
722 chip->irq_cpu_online(&desc->irq_data);
724 raw_spin_unlock_irqrestore(&desc->lock, flags);
729 * irq_cpu_offline - Invoke all irq_cpu_offline functions.
731 * Iterate through all irqs and invoke the chip.irq_cpu_offline()
734 void irq_cpu_offline(void)
736 struct irq_desc *desc;
737 struct irq_chip *chip;
741 for_each_active_irq(irq) {
742 desc = irq_to_desc(irq);
746 raw_spin_lock_irqsave(&desc->lock, flags);
748 chip = irq_data_get_irq_chip(&desc->irq_data);
749 if (chip && chip->irq_cpu_offline &&
750 (!(chip->flags & IRQCHIP_ONOFFLINE_ENABLED) ||
751 !irqd_irq_disabled(&desc->irq_data)))
752 chip->irq_cpu_offline(&desc->irq_data);
754 raw_spin_unlock_irqrestore(&desc->lock, flags);