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, 0);
37 desc->irq_data.chip = chip;
38 irq_put_desc_unlock(desc, flags);
40 * For !CONFIG_SPARSE_IRQ make the irq show up in
41 * allocated_irqs. For the CONFIG_SPARSE_IRQ case, it is
42 * already marked, and this call is harmless.
47 EXPORT_SYMBOL(irq_set_chip);
50 * irq_set_type - set the irq trigger type for an irq
52 * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
54 int irq_set_irq_type(unsigned int irq, unsigned int type)
57 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
63 type &= IRQ_TYPE_SENSE_MASK;
64 if (type != IRQ_TYPE_NONE)
65 ret = __irq_set_trigger(desc, irq, type);
66 irq_put_desc_busunlock(desc, flags);
69 EXPORT_SYMBOL(irq_set_irq_type);
72 * irq_set_handler_data - set irq handler data for an irq
73 * @irq: Interrupt number
74 * @data: Pointer to interrupt specific data
76 * Set the hardware irq controller data for an irq
78 int irq_set_handler_data(unsigned int irq, void *data)
81 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
85 desc->irq_data.handler_data = data;
86 irq_put_desc_unlock(desc, flags);
89 EXPORT_SYMBOL(irq_set_handler_data);
92 * irq_set_msi_desc - set MSI descriptor data for an irq
93 * @irq: Interrupt number
94 * @entry: Pointer to MSI descriptor data
96 * Set the MSI descriptor entry for an irq
98 int irq_set_msi_desc(unsigned int irq, struct msi_desc *entry)
101 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
105 desc->irq_data.msi_desc = entry;
108 irq_put_desc_unlock(desc, flags);
113 * irq_set_chip_data - set irq chip data for an irq
114 * @irq: Interrupt number
115 * @data: Pointer to chip specific data
117 * Set the hardware irq chip data for an irq
119 int irq_set_chip_data(unsigned int irq, void *data)
122 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
126 desc->irq_data.chip_data = data;
127 irq_put_desc_unlock(desc, flags);
130 EXPORT_SYMBOL(irq_set_chip_data);
132 struct irq_data *irq_get_irq_data(unsigned int irq)
134 struct irq_desc *desc = irq_to_desc(irq);
136 return desc ? &desc->irq_data : NULL;
138 EXPORT_SYMBOL_GPL(irq_get_irq_data);
140 static void irq_state_clr_disabled(struct irq_desc *desc)
142 irqd_clear(&desc->irq_data, IRQD_IRQ_DISABLED);
145 static void irq_state_set_disabled(struct irq_desc *desc)
147 irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
150 static void irq_state_clr_masked(struct irq_desc *desc)
152 irqd_clear(&desc->irq_data, IRQD_IRQ_MASKED);
155 static void irq_state_set_masked(struct irq_desc *desc)
157 irqd_set(&desc->irq_data, IRQD_IRQ_MASKED);
160 int irq_startup(struct irq_desc *desc, bool resend)
164 irq_state_clr_disabled(desc);
167 if (desc->irq_data.chip->irq_startup) {
168 ret = desc->irq_data.chip->irq_startup(&desc->irq_data);
169 irq_state_clr_masked(desc);
174 check_irq_resend(desc, desc->irq_data.irq);
178 void irq_shutdown(struct irq_desc *desc)
180 irq_state_set_disabled(desc);
182 if (desc->irq_data.chip->irq_shutdown)
183 desc->irq_data.chip->irq_shutdown(&desc->irq_data);
184 else if (desc->irq_data.chip->irq_disable)
185 desc->irq_data.chip->irq_disable(&desc->irq_data);
187 desc->irq_data.chip->irq_mask(&desc->irq_data);
188 irq_state_set_masked(desc);
191 void irq_enable(struct irq_desc *desc)
193 irq_state_clr_disabled(desc);
194 if (desc->irq_data.chip->irq_enable)
195 desc->irq_data.chip->irq_enable(&desc->irq_data);
197 desc->irq_data.chip->irq_unmask(&desc->irq_data);
198 irq_state_clr_masked(desc);
201 void irq_disable(struct irq_desc *desc)
203 irq_state_set_disabled(desc);
204 if (desc->irq_data.chip->irq_disable) {
205 desc->irq_data.chip->irq_disable(&desc->irq_data);
206 irq_state_set_masked(desc);
210 void irq_percpu_enable(struct irq_desc *desc, unsigned int cpu)
212 if (desc->irq_data.chip->irq_enable)
213 desc->irq_data.chip->irq_enable(&desc->irq_data);
215 desc->irq_data.chip->irq_unmask(&desc->irq_data);
216 cpumask_set_cpu(cpu, desc->percpu_enabled);
219 void irq_percpu_disable(struct irq_desc *desc, unsigned int cpu)
221 if (desc->irq_data.chip->irq_disable)
222 desc->irq_data.chip->irq_disable(&desc->irq_data);
224 desc->irq_data.chip->irq_mask(&desc->irq_data);
225 cpumask_clear_cpu(cpu, desc->percpu_enabled);
228 static inline void mask_ack_irq(struct irq_desc *desc)
230 if (desc->irq_data.chip->irq_mask_ack)
231 desc->irq_data.chip->irq_mask_ack(&desc->irq_data);
233 desc->irq_data.chip->irq_mask(&desc->irq_data);
234 if (desc->irq_data.chip->irq_ack)
235 desc->irq_data.chip->irq_ack(&desc->irq_data);
237 irq_state_set_masked(desc);
240 void mask_irq(struct irq_desc *desc)
242 if (desc->irq_data.chip->irq_mask) {
243 desc->irq_data.chip->irq_mask(&desc->irq_data);
244 irq_state_set_masked(desc);
248 void unmask_irq(struct irq_desc *desc)
250 if (desc->irq_data.chip->irq_unmask) {
251 desc->irq_data.chip->irq_unmask(&desc->irq_data);
252 irq_state_clr_masked(desc);
257 * handle_nested_irq - Handle a nested irq from a irq thread
258 * @irq: the interrupt number
260 * Handle interrupts which are nested into a threaded interrupt
261 * handler. The handler function is called inside the calling
264 void handle_nested_irq(unsigned int irq)
266 struct irq_desc *desc = irq_to_desc(irq);
267 struct irqaction *action;
268 irqreturn_t action_ret;
272 raw_spin_lock_irq(&desc->lock);
274 kstat_incr_irqs_this_cpu(irq, desc);
276 action = desc->action;
277 if (unlikely(!action || irqd_irq_disabled(&desc->irq_data)))
280 irqd_set(&desc->irq_data, IRQD_IRQ_INPROGRESS);
281 raw_spin_unlock_irq(&desc->lock);
283 action_ret = action->thread_fn(action->irq, action->dev_id);
285 note_interrupt(irq, desc, action_ret);
287 raw_spin_lock_irq(&desc->lock);
288 irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
291 raw_spin_unlock_irq(&desc->lock);
293 EXPORT_SYMBOL_GPL(handle_nested_irq);
295 static bool irq_check_poll(struct irq_desc *desc)
297 if (!(desc->istate & IRQS_POLL_INPROGRESS))
299 return irq_wait_for_poll(desc);
303 * handle_simple_irq - Simple and software-decoded IRQs.
304 * @irq: the interrupt number
305 * @desc: the interrupt description structure for this irq
307 * Simple interrupts are either sent from a demultiplexing interrupt
308 * handler or come from hardware, where no interrupt hardware control
311 * Note: The caller is expected to handle the ack, clear, mask and
312 * unmask issues if necessary.
315 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
317 raw_spin_lock(&desc->lock);
319 if (unlikely(irqd_irq_inprogress(&desc->irq_data)))
320 if (!irq_check_poll(desc))
323 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
324 kstat_incr_irqs_this_cpu(irq, desc);
326 if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data)))
329 handle_irq_event(desc);
332 raw_spin_unlock(&desc->lock);
334 EXPORT_SYMBOL_GPL(handle_simple_irq);
337 * Called unconditionally from handle_level_irq() and only for oneshot
338 * interrupts from handle_fasteoi_irq()
340 static void cond_unmask_irq(struct irq_desc *desc)
343 * We need to unmask in the following cases:
344 * - Standard level irq (IRQF_ONESHOT is not set)
345 * - Oneshot irq which did not wake the thread (caused by a
346 * spurious interrupt or a primary handler handling it
349 if (!irqd_irq_disabled(&desc->irq_data) &&
350 irqd_irq_masked(&desc->irq_data) && !desc->threads_oneshot)
355 * handle_level_irq - Level type irq handler
356 * @irq: the interrupt number
357 * @desc: the interrupt description structure for this irq
359 * Level type interrupts are active as long as the hardware line has
360 * the active level. This may require to mask the interrupt and unmask
361 * it after the associated handler has acknowledged the device, so the
362 * interrupt line is back to inactive.
365 handle_level_irq(unsigned int irq, struct irq_desc *desc)
367 raw_spin_lock(&desc->lock);
370 if (unlikely(irqd_irq_inprogress(&desc->irq_data)))
371 if (!irq_check_poll(desc))
374 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
375 kstat_incr_irqs_this_cpu(irq, desc);
378 * If its disabled or no action available
379 * keep it masked and get out of here
381 if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data)))
384 handle_irq_event(desc);
386 cond_unmask_irq(desc);
389 raw_spin_unlock(&desc->lock);
391 EXPORT_SYMBOL_GPL(handle_level_irq);
393 #ifdef CONFIG_IRQ_PREFLOW_FASTEOI
394 static inline void preflow_handler(struct irq_desc *desc)
396 if (desc->preflow_handler)
397 desc->preflow_handler(&desc->irq_data);
400 static inline void preflow_handler(struct irq_desc *desc) { }
404 * handle_fasteoi_irq - irq handler for transparent controllers
405 * @irq: the interrupt number
406 * @desc: the interrupt description structure for this irq
408 * Only a single callback will be issued to the chip: an ->eoi()
409 * call when the interrupt has been serviced. This enables support
410 * for modern forms of interrupt handlers, which handle the flow
411 * details in hardware, transparently.
414 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
416 raw_spin_lock(&desc->lock);
418 if (unlikely(irqd_irq_inprogress(&desc->irq_data)))
419 if (!irq_check_poll(desc))
422 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
423 kstat_incr_irqs_this_cpu(irq, desc);
426 * If its disabled or no action available
427 * then mask it and get out of here:
429 if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data))) {
430 desc->istate |= IRQS_PENDING;
435 if (desc->istate & IRQS_ONESHOT)
438 preflow_handler(desc);
439 handle_irq_event(desc);
441 if (desc->istate & IRQS_ONESHOT)
442 cond_unmask_irq(desc);
445 desc->irq_data.chip->irq_eoi(&desc->irq_data);
447 raw_spin_unlock(&desc->lock);
450 if (!(desc->irq_data.chip->flags & IRQCHIP_EOI_IF_HANDLED))
456 * handle_edge_irq - edge type IRQ handler
457 * @irq: the interrupt number
458 * @desc: the interrupt description structure for this irq
460 * Interrupt occures on the falling and/or rising edge of a hardware
461 * signal. The occurrence is latched into the irq controller hardware
462 * and must be acked in order to be reenabled. After the ack another
463 * interrupt can happen on the same source even before the first one
464 * is handled by the associated event handler. If this happens it
465 * might be necessary to disable (mask) the interrupt depending on the
466 * controller hardware. This requires to reenable the interrupt inside
467 * of the loop which handles the interrupts which have arrived while
468 * the handler was running. If all pending interrupts are handled, the
472 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
474 raw_spin_lock(&desc->lock);
476 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
478 * If we're currently running this IRQ, or its disabled,
479 * we shouldn't process the IRQ. Mark it pending, handle
480 * the necessary masking and go out
482 if (unlikely(irqd_irq_disabled(&desc->irq_data) ||
483 irqd_irq_inprogress(&desc->irq_data) || !desc->action)) {
484 if (!irq_check_poll(desc)) {
485 desc->istate |= IRQS_PENDING;
490 kstat_incr_irqs_this_cpu(irq, desc);
492 /* Start handling the irq */
493 desc->irq_data.chip->irq_ack(&desc->irq_data);
496 if (unlikely(!desc->action)) {
502 * When another irq arrived while we were handling
503 * one, we could have masked the irq.
504 * Renable it, if it was not disabled in meantime.
506 if (unlikely(desc->istate & IRQS_PENDING)) {
507 if (!irqd_irq_disabled(&desc->irq_data) &&
508 irqd_irq_masked(&desc->irq_data))
512 handle_irq_event(desc);
514 } while ((desc->istate & IRQS_PENDING) &&
515 !irqd_irq_disabled(&desc->irq_data));
518 raw_spin_unlock(&desc->lock);
521 #ifdef CONFIG_IRQ_EDGE_EOI_HANDLER
523 * handle_edge_eoi_irq - edge eoi type IRQ handler
524 * @irq: the interrupt number
525 * @desc: the interrupt description structure for this irq
527 * Similar as the above handle_edge_irq, but using eoi and w/o the
530 void handle_edge_eoi_irq(unsigned int irq, struct irq_desc *desc)
532 struct irq_chip *chip = irq_desc_get_chip(desc);
534 raw_spin_lock(&desc->lock);
536 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
538 * If we're currently running this IRQ, or its disabled,
539 * we shouldn't process the IRQ. Mark it pending, handle
540 * the necessary masking and go out
542 if (unlikely(irqd_irq_disabled(&desc->irq_data) ||
543 irqd_irq_inprogress(&desc->irq_data) || !desc->action)) {
544 if (!irq_check_poll(desc)) {
545 desc->istate |= IRQS_PENDING;
549 kstat_incr_irqs_this_cpu(irq, desc);
552 if (unlikely(!desc->action))
555 handle_irq_event(desc);
557 } while ((desc->istate & IRQS_PENDING) &&
558 !irqd_irq_disabled(&desc->irq_data));
561 chip->irq_eoi(&desc->irq_data);
562 raw_spin_unlock(&desc->lock);
567 * handle_percpu_irq - Per CPU local irq handler
568 * @irq: the interrupt number
569 * @desc: the interrupt description structure for this irq
571 * Per CPU interrupts on SMP machines without locking requirements
574 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
576 struct irq_chip *chip = irq_desc_get_chip(desc);
578 kstat_incr_irqs_this_cpu(irq, desc);
581 chip->irq_ack(&desc->irq_data);
583 handle_irq_event_percpu(desc, desc->action);
586 chip->irq_eoi(&desc->irq_data);
590 * handle_percpu_devid_irq - Per CPU local irq handler with per cpu dev ids
591 * @irq: the interrupt number
592 * @desc: the interrupt description structure for this irq
594 * Per CPU interrupts on SMP machines without locking requirements. Same as
595 * handle_percpu_irq() above but with the following extras:
597 * action->percpu_dev_id is a pointer to percpu variables which
598 * contain the real device id for the cpu on which this handler is
601 void handle_percpu_devid_irq(unsigned int irq, struct irq_desc *desc)
603 struct irq_chip *chip = irq_desc_get_chip(desc);
604 struct irqaction *action = desc->action;
605 void *dev_id = __this_cpu_ptr(action->percpu_dev_id);
608 kstat_incr_irqs_this_cpu(irq, desc);
611 chip->irq_ack(&desc->irq_data);
613 trace_irq_handler_entry(irq, action);
614 res = action->handler(irq, dev_id);
615 trace_irq_handler_exit(irq, action, res);
618 chip->irq_eoi(&desc->irq_data);
622 __irq_set_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
626 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, 0);
632 handle = handle_bad_irq;
634 if (WARN_ON(desc->irq_data.chip == &no_irq_chip))
639 if (handle == handle_bad_irq) {
640 if (desc->irq_data.chip != &no_irq_chip)
642 irq_state_set_disabled(desc);
645 desc->handle_irq = handle;
648 if (handle != handle_bad_irq && is_chained) {
649 irq_settings_set_noprobe(desc);
650 irq_settings_set_norequest(desc);
651 irq_settings_set_nothread(desc);
652 irq_startup(desc, true);
655 irq_put_desc_busunlock(desc, flags);
657 EXPORT_SYMBOL_GPL(__irq_set_handler);
660 irq_set_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
661 irq_flow_handler_t handle, const char *name)
663 irq_set_chip(irq, chip);
664 __irq_set_handler(irq, handle, 0, name);
667 void irq_modify_status(unsigned int irq, unsigned long clr, unsigned long set)
670 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
674 irq_settings_clr_and_set(desc, clr, set);
676 irqd_clear(&desc->irq_data, IRQD_NO_BALANCING | IRQD_PER_CPU |
677 IRQD_TRIGGER_MASK | IRQD_LEVEL | IRQD_MOVE_PCNTXT);
678 if (irq_settings_has_no_balance_set(desc))
679 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
680 if (irq_settings_is_per_cpu(desc))
681 irqd_set(&desc->irq_data, IRQD_PER_CPU);
682 if (irq_settings_can_move_pcntxt(desc))
683 irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
684 if (irq_settings_is_level(desc))
685 irqd_set(&desc->irq_data, IRQD_LEVEL);
687 irqd_set(&desc->irq_data, irq_settings_get_trigger_mask(desc));
689 irq_put_desc_unlock(desc, flags);
691 EXPORT_SYMBOL_GPL(irq_modify_status);
694 * irq_cpu_online - Invoke all irq_cpu_online functions.
696 * Iterate through all irqs and invoke the chip.irq_cpu_online()
699 void irq_cpu_online(void)
701 struct irq_desc *desc;
702 struct irq_chip *chip;
706 for_each_active_irq(irq) {
707 desc = irq_to_desc(irq);
711 raw_spin_lock_irqsave(&desc->lock, flags);
713 chip = irq_data_get_irq_chip(&desc->irq_data);
714 if (chip && chip->irq_cpu_online &&
715 (!(chip->flags & IRQCHIP_ONOFFLINE_ENABLED) ||
716 !irqd_irq_disabled(&desc->irq_data)))
717 chip->irq_cpu_online(&desc->irq_data);
719 raw_spin_unlock_irqrestore(&desc->lock, flags);
724 * irq_cpu_offline - Invoke all irq_cpu_offline functions.
726 * Iterate through all irqs and invoke the chip.irq_cpu_offline()
729 void irq_cpu_offline(void)
731 struct irq_desc *desc;
732 struct irq_chip *chip;
736 for_each_active_irq(irq) {
737 desc = irq_to_desc(irq);
741 raw_spin_lock_irqsave(&desc->lock, flags);
743 chip = irq_data_get_irq_chip(&desc->irq_data);
744 if (chip && chip->irq_cpu_offline &&
745 (!(chip->flags & IRQCHIP_ONOFFLINE_ENABLED) ||
746 !irqd_irq_disabled(&desc->irq_data)))
747 chip->irq_cpu_offline(&desc->irq_data);
749 raw_spin_unlock_irqrestore(&desc->lock, flags);