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 ret = __irq_set_trigger(desc, irq, type);
65 irq_put_desc_busunlock(desc, flags);
68 EXPORT_SYMBOL(irq_set_irq_type);
71 * irq_set_handler_data - set irq handler data for an irq
72 * @irq: Interrupt number
73 * @data: Pointer to interrupt specific data
75 * Set the hardware irq controller data for an irq
77 int irq_set_handler_data(unsigned int irq, void *data)
80 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
84 desc->irq_data.handler_data = data;
85 irq_put_desc_unlock(desc, flags);
88 EXPORT_SYMBOL(irq_set_handler_data);
91 * irq_set_msi_desc - set MSI descriptor data for an irq
92 * @irq: Interrupt number
93 * @entry: Pointer to MSI descriptor data
95 * Set the MSI descriptor entry for an irq
97 int irq_set_msi_desc(unsigned int irq, struct msi_desc *entry)
100 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
104 desc->irq_data.msi_desc = entry;
107 irq_put_desc_unlock(desc, flags);
112 * irq_set_chip_data - set irq chip data for an irq
113 * @irq: Interrupt number
114 * @data: Pointer to chip specific data
116 * Set the hardware irq chip data for an irq
118 int irq_set_chip_data(unsigned int irq, void *data)
121 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
125 desc->irq_data.chip_data = data;
126 irq_put_desc_unlock(desc, flags);
129 EXPORT_SYMBOL(irq_set_chip_data);
131 struct irq_data *irq_get_irq_data(unsigned int irq)
133 struct irq_desc *desc = irq_to_desc(irq);
135 return desc ? &desc->irq_data : NULL;
137 EXPORT_SYMBOL_GPL(irq_get_irq_data);
139 static void irq_state_clr_disabled(struct irq_desc *desc)
141 irqd_clear(&desc->irq_data, IRQD_IRQ_DISABLED);
144 static void irq_state_set_disabled(struct irq_desc *desc)
146 irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
149 static void irq_state_clr_masked(struct irq_desc *desc)
151 irqd_clear(&desc->irq_data, IRQD_IRQ_MASKED);
154 static void irq_state_set_masked(struct irq_desc *desc)
156 irqd_set(&desc->irq_data, IRQD_IRQ_MASKED);
159 int irq_startup(struct irq_desc *desc, bool resend)
163 irq_state_clr_disabled(desc);
166 if (desc->irq_data.chip->irq_startup) {
167 ret = desc->irq_data.chip->irq_startup(&desc->irq_data);
168 irq_state_clr_masked(desc);
173 check_irq_resend(desc, desc->irq_data.irq);
177 void irq_shutdown(struct irq_desc *desc)
179 irq_state_set_disabled(desc);
181 if (desc->irq_data.chip->irq_shutdown)
182 desc->irq_data.chip->irq_shutdown(&desc->irq_data);
183 else if (desc->irq_data.chip->irq_disable)
184 desc->irq_data.chip->irq_disable(&desc->irq_data);
186 desc->irq_data.chip->irq_mask(&desc->irq_data);
187 irq_state_set_masked(desc);
190 void irq_enable(struct irq_desc *desc)
192 irq_state_clr_disabled(desc);
193 if (desc->irq_data.chip->irq_enable)
194 desc->irq_data.chip->irq_enable(&desc->irq_data);
196 desc->irq_data.chip->irq_unmask(&desc->irq_data);
197 irq_state_clr_masked(desc);
200 void irq_disable(struct irq_desc *desc)
202 irq_state_set_disabled(desc);
203 if (desc->irq_data.chip->irq_disable) {
204 desc->irq_data.chip->irq_disable(&desc->irq_data);
205 irq_state_set_masked(desc);
209 void irq_percpu_enable(struct irq_desc *desc, unsigned int cpu)
211 if (desc->irq_data.chip->irq_enable)
212 desc->irq_data.chip->irq_enable(&desc->irq_data);
214 desc->irq_data.chip->irq_unmask(&desc->irq_data);
215 cpumask_set_cpu(cpu, desc->percpu_enabled);
218 void irq_percpu_disable(struct irq_desc *desc, unsigned int cpu)
220 if (desc->irq_data.chip->irq_disable)
221 desc->irq_data.chip->irq_disable(&desc->irq_data);
223 desc->irq_data.chip->irq_mask(&desc->irq_data);
224 cpumask_clear_cpu(cpu, desc->percpu_enabled);
227 static inline void mask_ack_irq(struct irq_desc *desc)
229 if (desc->irq_data.chip->irq_mask_ack)
230 desc->irq_data.chip->irq_mask_ack(&desc->irq_data);
232 desc->irq_data.chip->irq_mask(&desc->irq_data);
233 if (desc->irq_data.chip->irq_ack)
234 desc->irq_data.chip->irq_ack(&desc->irq_data);
236 irq_state_set_masked(desc);
239 void mask_irq(struct irq_desc *desc)
241 if (desc->irq_data.chip->irq_mask) {
242 desc->irq_data.chip->irq_mask(&desc->irq_data);
243 irq_state_set_masked(desc);
247 void unmask_irq(struct irq_desc *desc)
249 if (desc->irq_data.chip->irq_unmask) {
250 desc->irq_data.chip->irq_unmask(&desc->irq_data);
251 irq_state_clr_masked(desc);
256 * handle_nested_irq - Handle a nested irq from a irq thread
257 * @irq: the interrupt number
259 * Handle interrupts which are nested into a threaded interrupt
260 * handler. The handler function is called inside the calling
263 void handle_nested_irq(unsigned int irq)
265 struct irq_desc *desc = irq_to_desc(irq);
266 struct irqaction *action;
267 irqreturn_t action_ret;
271 raw_spin_lock_irq(&desc->lock);
273 kstat_incr_irqs_this_cpu(irq, desc);
275 action = desc->action;
276 if (unlikely(!action || irqd_irq_disabled(&desc->irq_data)))
279 irqd_set(&desc->irq_data, IRQD_IRQ_INPROGRESS);
280 raw_spin_unlock_irq(&desc->lock);
282 action_ret = action->thread_fn(action->irq, action->dev_id);
284 note_interrupt(irq, desc, action_ret);
286 raw_spin_lock_irq(&desc->lock);
287 irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
290 raw_spin_unlock_irq(&desc->lock);
292 EXPORT_SYMBOL_GPL(handle_nested_irq);
294 static bool irq_check_poll(struct irq_desc *desc)
296 if (!(desc->istate & IRQS_POLL_INPROGRESS))
298 return irq_wait_for_poll(desc);
302 * handle_simple_irq - Simple and software-decoded IRQs.
303 * @irq: the interrupt number
304 * @desc: the interrupt description structure for this irq
306 * Simple interrupts are either sent from a demultiplexing interrupt
307 * handler or come from hardware, where no interrupt hardware control
310 * Note: The caller is expected to handle the ack, clear, mask and
311 * unmask issues if necessary.
314 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
316 raw_spin_lock(&desc->lock);
318 if (unlikely(irqd_irq_inprogress(&desc->irq_data)))
319 if (!irq_check_poll(desc))
322 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
323 kstat_incr_irqs_this_cpu(irq, desc);
325 if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data)))
328 handle_irq_event(desc);
331 raw_spin_unlock(&desc->lock);
333 EXPORT_SYMBOL_GPL(handle_simple_irq);
336 * Called unconditionally from handle_level_irq() and only for oneshot
337 * interrupts from handle_fasteoi_irq()
339 static void cond_unmask_irq(struct irq_desc *desc)
342 * We need to unmask in the following cases:
343 * - Standard level irq (IRQF_ONESHOT is not set)
344 * - Oneshot irq which did not wake the thread (caused by a
345 * spurious interrupt or a primary handler handling it
348 if (!irqd_irq_disabled(&desc->irq_data) &&
349 irqd_irq_masked(&desc->irq_data) && !desc->threads_oneshot)
354 * handle_level_irq - Level type irq handler
355 * @irq: the interrupt number
356 * @desc: the interrupt description structure for this irq
358 * Level type interrupts are active as long as the hardware line has
359 * the active level. This may require to mask the interrupt and unmask
360 * it after the associated handler has acknowledged the device, so the
361 * interrupt line is back to inactive.
364 handle_level_irq(unsigned int irq, struct irq_desc *desc)
366 raw_spin_lock(&desc->lock);
369 if (unlikely(irqd_irq_inprogress(&desc->irq_data)))
370 if (!irq_check_poll(desc))
373 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
374 kstat_incr_irqs_this_cpu(irq, desc);
377 * If its disabled or no action available
378 * keep it masked and get out of here
380 if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data)))
383 handle_irq_event(desc);
385 cond_unmask_irq(desc);
388 raw_spin_unlock(&desc->lock);
390 EXPORT_SYMBOL_GPL(handle_level_irq);
392 #ifdef CONFIG_IRQ_PREFLOW_FASTEOI
393 static inline void preflow_handler(struct irq_desc *desc)
395 if (desc->preflow_handler)
396 desc->preflow_handler(&desc->irq_data);
399 static inline void preflow_handler(struct irq_desc *desc) { }
403 * handle_fasteoi_irq - irq handler for transparent controllers
404 * @irq: the interrupt number
405 * @desc: the interrupt description structure for this irq
407 * Only a single callback will be issued to the chip: an ->eoi()
408 * call when the interrupt has been serviced. This enables support
409 * for modern forms of interrupt handlers, which handle the flow
410 * details in hardware, transparently.
413 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
415 raw_spin_lock(&desc->lock);
417 if (unlikely(irqd_irq_inprogress(&desc->irq_data)))
418 if (!irq_check_poll(desc))
421 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
422 kstat_incr_irqs_this_cpu(irq, desc);
425 * If its disabled or no action available
426 * then mask it and get out of here:
428 if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data))) {
429 desc->istate |= IRQS_PENDING;
434 if (desc->istate & IRQS_ONESHOT)
437 preflow_handler(desc);
438 handle_irq_event(desc);
440 if (desc->istate & IRQS_ONESHOT)
441 cond_unmask_irq(desc);
444 desc->irq_data.chip->irq_eoi(&desc->irq_data);
446 raw_spin_unlock(&desc->lock);
449 if (!(desc->irq_data.chip->flags & IRQCHIP_EOI_IF_HANDLED))
455 * handle_edge_irq - edge type IRQ handler
456 * @irq: the interrupt number
457 * @desc: the interrupt description structure for this irq
459 * Interrupt occures on the falling and/or rising edge of a hardware
460 * signal. The occurrence is latched into the irq controller hardware
461 * and must be acked in order to be reenabled. After the ack another
462 * interrupt can happen on the same source even before the first one
463 * is handled by the associated event handler. If this happens it
464 * might be necessary to disable (mask) the interrupt depending on the
465 * controller hardware. This requires to reenable the interrupt inside
466 * of the loop which handles the interrupts which have arrived while
467 * the handler was running. If all pending interrupts are handled, the
471 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
473 raw_spin_lock(&desc->lock);
475 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
477 * If we're currently running this IRQ, or its disabled,
478 * we shouldn't process the IRQ. Mark it pending, handle
479 * the necessary masking and go out
481 if (unlikely(irqd_irq_disabled(&desc->irq_data) ||
482 irqd_irq_inprogress(&desc->irq_data) || !desc->action)) {
483 if (!irq_check_poll(desc)) {
484 desc->istate |= IRQS_PENDING;
489 kstat_incr_irqs_this_cpu(irq, desc);
491 /* Start handling the irq */
492 desc->irq_data.chip->irq_ack(&desc->irq_data);
495 if (unlikely(!desc->action)) {
501 * When another irq arrived while we were handling
502 * one, we could have masked the irq.
503 * Renable it, if it was not disabled in meantime.
505 if (unlikely(desc->istate & IRQS_PENDING)) {
506 if (!irqd_irq_disabled(&desc->irq_data) &&
507 irqd_irq_masked(&desc->irq_data))
511 handle_irq_event(desc);
513 } while ((desc->istate & IRQS_PENDING) &&
514 !irqd_irq_disabled(&desc->irq_data));
517 raw_spin_unlock(&desc->lock);
520 #ifdef CONFIG_IRQ_EDGE_EOI_HANDLER
522 * handle_edge_eoi_irq - edge eoi type IRQ handler
523 * @irq: the interrupt number
524 * @desc: the interrupt description structure for this irq
526 * Similar as the above handle_edge_irq, but using eoi and w/o the
529 void handle_edge_eoi_irq(unsigned int irq, struct irq_desc *desc)
531 struct irq_chip *chip = irq_desc_get_chip(desc);
533 raw_spin_lock(&desc->lock);
535 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
537 * If we're currently running this IRQ, or its disabled,
538 * we shouldn't process the IRQ. Mark it pending, handle
539 * the necessary masking and go out
541 if (unlikely(irqd_irq_disabled(&desc->irq_data) ||
542 irqd_irq_inprogress(&desc->irq_data) || !desc->action)) {
543 if (!irq_check_poll(desc)) {
544 desc->istate |= IRQS_PENDING;
548 kstat_incr_irqs_this_cpu(irq, desc);
551 if (unlikely(!desc->action))
554 handle_irq_event(desc);
556 } while ((desc->istate & IRQS_PENDING) &&
557 !irqd_irq_disabled(&desc->irq_data));
560 chip->irq_eoi(&desc->irq_data);
561 raw_spin_unlock(&desc->lock);
566 * handle_percpu_irq - Per CPU local irq handler
567 * @irq: the interrupt number
568 * @desc: the interrupt description structure for this irq
570 * Per CPU interrupts on SMP machines without locking requirements
573 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
575 struct irq_chip *chip = irq_desc_get_chip(desc);
577 kstat_incr_irqs_this_cpu(irq, desc);
580 chip->irq_ack(&desc->irq_data);
582 handle_irq_event_percpu(desc, desc->action);
585 chip->irq_eoi(&desc->irq_data);
589 * handle_percpu_devid_irq - Per CPU local irq handler with per cpu dev ids
590 * @irq: the interrupt number
591 * @desc: the interrupt description structure for this irq
593 * Per CPU interrupts on SMP machines without locking requirements. Same as
594 * handle_percpu_irq() above but with the following extras:
596 * action->percpu_dev_id is a pointer to percpu variables which
597 * contain the real device id for the cpu on which this handler is
600 void handle_percpu_devid_irq(unsigned int irq, struct irq_desc *desc)
602 struct irq_chip *chip = irq_desc_get_chip(desc);
603 struct irqaction *action = desc->action;
604 void *dev_id = __this_cpu_ptr(action->percpu_dev_id);
607 kstat_incr_irqs_this_cpu(irq, desc);
610 chip->irq_ack(&desc->irq_data);
612 trace_irq_handler_entry(irq, action);
613 res = action->handler(irq, dev_id);
614 trace_irq_handler_exit(irq, action, res);
617 chip->irq_eoi(&desc->irq_data);
621 __irq_set_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
625 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, 0);
631 handle = handle_bad_irq;
633 if (WARN_ON(desc->irq_data.chip == &no_irq_chip))
638 if (handle == handle_bad_irq) {
639 if (desc->irq_data.chip != &no_irq_chip)
641 irq_state_set_disabled(desc);
644 desc->handle_irq = handle;
647 if (handle != handle_bad_irq && is_chained) {
648 irq_settings_set_noprobe(desc);
649 irq_settings_set_norequest(desc);
650 irq_settings_set_nothread(desc);
651 irq_startup(desc, true);
654 irq_put_desc_busunlock(desc, flags);
656 EXPORT_SYMBOL_GPL(__irq_set_handler);
659 irq_set_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
660 irq_flow_handler_t handle, const char *name)
662 irq_set_chip(irq, chip);
663 __irq_set_handler(irq, handle, 0, name);
666 void irq_modify_status(unsigned int irq, unsigned long clr, unsigned long set)
669 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
673 irq_settings_clr_and_set(desc, clr, set);
675 irqd_clear(&desc->irq_data, IRQD_NO_BALANCING | IRQD_PER_CPU |
676 IRQD_TRIGGER_MASK | IRQD_LEVEL | IRQD_MOVE_PCNTXT);
677 if (irq_settings_has_no_balance_set(desc))
678 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
679 if (irq_settings_is_per_cpu(desc))
680 irqd_set(&desc->irq_data, IRQD_PER_CPU);
681 if (irq_settings_can_move_pcntxt(desc))
682 irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
683 if (irq_settings_is_level(desc))
684 irqd_set(&desc->irq_data, IRQD_LEVEL);
686 irqd_set(&desc->irq_data, irq_settings_get_trigger_mask(desc));
688 irq_put_desc_unlock(desc, flags);
690 EXPORT_SYMBOL_GPL(irq_modify_status);
693 * irq_cpu_online - Invoke all irq_cpu_online functions.
695 * Iterate through all irqs and invoke the chip.irq_cpu_online()
698 void irq_cpu_online(void)
700 struct irq_desc *desc;
701 struct irq_chip *chip;
705 for_each_active_irq(irq) {
706 desc = irq_to_desc(irq);
710 raw_spin_lock_irqsave(&desc->lock, flags);
712 chip = irq_data_get_irq_chip(&desc->irq_data);
713 if (chip && chip->irq_cpu_online &&
714 (!(chip->flags & IRQCHIP_ONOFFLINE_ENABLED) ||
715 !irqd_irq_disabled(&desc->irq_data)))
716 chip->irq_cpu_online(&desc->irq_data);
718 raw_spin_unlock_irqrestore(&desc->lock, flags);
723 * irq_cpu_offline - Invoke all irq_cpu_offline functions.
725 * Iterate through all irqs and invoke the chip.irq_cpu_offline()
728 void irq_cpu_offline(void)
730 struct irq_desc *desc;
731 struct irq_chip *chip;
735 for_each_active_irq(irq) {
736 desc = irq_to_desc(irq);
740 raw_spin_lock_irqsave(&desc->lock, flags);
742 chip = irq_data_get_irq_chip(&desc->irq_data);
743 if (chip && chip->irq_cpu_offline &&
744 (!(chip->flags & IRQCHIP_ONOFFLINE_ENABLED) ||
745 !irqd_irq_disabled(&desc->irq_data)))
746 chip->irq_cpu_offline(&desc->irq_data);
748 raw_spin_unlock_irqrestore(&desc->lock, flags);