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)
161 irq_state_clr_disabled(desc);
164 if (desc->irq_data.chip->irq_startup) {
165 int ret = desc->irq_data.chip->irq_startup(&desc->irq_data);
166 irq_state_clr_masked(desc);
174 void irq_shutdown(struct irq_desc *desc)
176 irq_state_set_disabled(desc);
178 if (desc->irq_data.chip->irq_shutdown)
179 desc->irq_data.chip->irq_shutdown(&desc->irq_data);
180 else if (desc->irq_data.chip->irq_disable)
181 desc->irq_data.chip->irq_disable(&desc->irq_data);
183 desc->irq_data.chip->irq_mask(&desc->irq_data);
184 irq_state_set_masked(desc);
187 void irq_enable(struct irq_desc *desc)
189 irq_state_clr_disabled(desc);
190 if (desc->irq_data.chip->irq_enable)
191 desc->irq_data.chip->irq_enable(&desc->irq_data);
193 desc->irq_data.chip->irq_unmask(&desc->irq_data);
194 irq_state_clr_masked(desc);
197 void irq_disable(struct irq_desc *desc)
199 irq_state_set_disabled(desc);
200 if (desc->irq_data.chip->irq_disable) {
201 desc->irq_data.chip->irq_disable(&desc->irq_data);
202 irq_state_set_masked(desc);
206 void irq_percpu_enable(struct irq_desc *desc, unsigned int cpu)
208 if (desc->irq_data.chip->irq_enable)
209 desc->irq_data.chip->irq_enable(&desc->irq_data);
211 desc->irq_data.chip->irq_unmask(&desc->irq_data);
212 cpumask_set_cpu(cpu, desc->percpu_enabled);
215 void irq_percpu_disable(struct irq_desc *desc, unsigned int cpu)
217 if (desc->irq_data.chip->irq_disable)
218 desc->irq_data.chip->irq_disable(&desc->irq_data);
220 desc->irq_data.chip->irq_mask(&desc->irq_data);
221 cpumask_clear_cpu(cpu, desc->percpu_enabled);
224 static inline void mask_ack_irq(struct irq_desc *desc)
226 if (desc->irq_data.chip->irq_mask_ack)
227 desc->irq_data.chip->irq_mask_ack(&desc->irq_data);
229 desc->irq_data.chip->irq_mask(&desc->irq_data);
230 if (desc->irq_data.chip->irq_ack)
231 desc->irq_data.chip->irq_ack(&desc->irq_data);
233 irq_state_set_masked(desc);
236 void mask_irq(struct irq_desc *desc)
238 if (desc->irq_data.chip->irq_mask) {
239 desc->irq_data.chip->irq_mask(&desc->irq_data);
240 irq_state_set_masked(desc);
244 void unmask_irq(struct irq_desc *desc)
246 if (desc->irq_data.chip->irq_unmask) {
247 desc->irq_data.chip->irq_unmask(&desc->irq_data);
248 irq_state_clr_masked(desc);
253 * handle_nested_irq - Handle a nested irq from a irq thread
254 * @irq: the interrupt number
256 * Handle interrupts which are nested into a threaded interrupt
257 * handler. The handler function is called inside the calling
260 void handle_nested_irq(unsigned int irq)
262 struct irq_desc *desc = irq_to_desc(irq);
263 struct irqaction *action;
264 irqreturn_t action_ret;
268 raw_spin_lock_irq(&desc->lock);
270 kstat_incr_irqs_this_cpu(irq, desc);
272 action = desc->action;
273 if (unlikely(!action || irqd_irq_disabled(&desc->irq_data)))
276 irqd_set(&desc->irq_data, IRQD_IRQ_INPROGRESS);
277 raw_spin_unlock_irq(&desc->lock);
279 action_ret = action->thread_fn(action->irq, action->dev_id);
281 note_interrupt(irq, desc, action_ret);
283 raw_spin_lock_irq(&desc->lock);
284 irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
287 raw_spin_unlock_irq(&desc->lock);
289 EXPORT_SYMBOL_GPL(handle_nested_irq);
291 static bool irq_check_poll(struct irq_desc *desc)
293 if (!(desc->istate & IRQS_POLL_INPROGRESS))
295 return irq_wait_for_poll(desc);
299 * handle_simple_irq - Simple and software-decoded IRQs.
300 * @irq: the interrupt number
301 * @desc: the interrupt description structure for this irq
303 * Simple interrupts are either sent from a demultiplexing interrupt
304 * handler or come from hardware, where no interrupt hardware control
307 * Note: The caller is expected to handle the ack, clear, mask and
308 * unmask issues if necessary.
311 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
313 raw_spin_lock(&desc->lock);
315 if (unlikely(irqd_irq_inprogress(&desc->irq_data)))
316 if (!irq_check_poll(desc))
319 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
320 kstat_incr_irqs_this_cpu(irq, desc);
322 if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data)))
325 handle_irq_event(desc);
328 raw_spin_unlock(&desc->lock);
330 EXPORT_SYMBOL_GPL(handle_simple_irq);
333 * handle_level_irq - Level type irq handler
334 * @irq: the interrupt number
335 * @desc: the interrupt description structure for this irq
337 * Level type interrupts are active as long as the hardware line has
338 * the active level. This may require to mask the interrupt and unmask
339 * it after the associated handler has acknowledged the device, so the
340 * interrupt line is back to inactive.
343 handle_level_irq(unsigned int irq, struct irq_desc *desc)
345 raw_spin_lock(&desc->lock);
348 if (unlikely(irqd_irq_inprogress(&desc->irq_data)))
349 if (!irq_check_poll(desc))
352 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
353 kstat_incr_irqs_this_cpu(irq, desc);
356 * If its disabled or no action available
357 * keep it masked and get out of here
359 if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data)))
362 handle_irq_event(desc);
364 if (!irqd_irq_disabled(&desc->irq_data) && !(desc->istate & IRQS_ONESHOT))
367 raw_spin_unlock(&desc->lock);
369 EXPORT_SYMBOL_GPL(handle_level_irq);
371 #ifdef CONFIG_IRQ_PREFLOW_FASTEOI
372 static inline void preflow_handler(struct irq_desc *desc)
374 if (desc->preflow_handler)
375 desc->preflow_handler(&desc->irq_data);
378 static inline void preflow_handler(struct irq_desc *desc) { }
382 * handle_fasteoi_irq - irq handler for transparent controllers
383 * @irq: the interrupt number
384 * @desc: the interrupt description structure for this irq
386 * Only a single callback will be issued to the chip: an ->eoi()
387 * call when the interrupt has been serviced. This enables support
388 * for modern forms of interrupt handlers, which handle the flow
389 * details in hardware, transparently.
392 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
394 raw_spin_lock(&desc->lock);
396 if (unlikely(irqd_irq_inprogress(&desc->irq_data)))
397 if (!irq_check_poll(desc))
400 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
401 kstat_incr_irqs_this_cpu(irq, desc);
404 * If its disabled or no action available
405 * then mask it and get out of here:
407 if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data))) {
408 desc->istate |= IRQS_PENDING;
413 if (desc->istate & IRQS_ONESHOT)
416 preflow_handler(desc);
417 handle_irq_event(desc);
420 desc->irq_data.chip->irq_eoi(&desc->irq_data);
422 raw_spin_unlock(&desc->lock);
425 if (!(desc->irq_data.chip->flags & IRQCHIP_EOI_IF_HANDLED))
431 * handle_edge_irq - edge type IRQ handler
432 * @irq: the interrupt number
433 * @desc: the interrupt description structure for this irq
435 * Interrupt occures on the falling and/or rising edge of a hardware
436 * signal. The occurrence is latched into the irq controller hardware
437 * and must be acked in order to be reenabled. After the ack another
438 * interrupt can happen on the same source even before the first one
439 * is handled by the associated event handler. If this happens it
440 * might be necessary to disable (mask) the interrupt depending on the
441 * controller hardware. This requires to reenable the interrupt inside
442 * of the loop which handles the interrupts which have arrived while
443 * the handler was running. If all pending interrupts are handled, the
447 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
449 raw_spin_lock(&desc->lock);
451 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
453 * If we're currently running this IRQ, or its disabled,
454 * we shouldn't process the IRQ. Mark it pending, handle
455 * the necessary masking and go out
457 if (unlikely(irqd_irq_disabled(&desc->irq_data) ||
458 irqd_irq_inprogress(&desc->irq_data) || !desc->action)) {
459 if (!irq_check_poll(desc)) {
460 desc->istate |= IRQS_PENDING;
465 kstat_incr_irqs_this_cpu(irq, desc);
467 /* Start handling the irq */
468 desc->irq_data.chip->irq_ack(&desc->irq_data);
471 if (unlikely(!desc->action)) {
477 * When another irq arrived while we were handling
478 * one, we could have masked the irq.
479 * Renable it, if it was not disabled in meantime.
481 if (unlikely(desc->istate & IRQS_PENDING)) {
482 if (!irqd_irq_disabled(&desc->irq_data) &&
483 irqd_irq_masked(&desc->irq_data))
487 handle_irq_event(desc);
489 } while ((desc->istate & IRQS_PENDING) &&
490 !irqd_irq_disabled(&desc->irq_data));
493 raw_spin_unlock(&desc->lock);
496 #ifdef CONFIG_IRQ_EDGE_EOI_HANDLER
498 * handle_edge_eoi_irq - edge eoi type IRQ handler
499 * @irq: the interrupt number
500 * @desc: the interrupt description structure for this irq
502 * Similar as the above handle_edge_irq, but using eoi and w/o the
505 void handle_edge_eoi_irq(unsigned int irq, struct irq_desc *desc)
507 struct irq_chip *chip = irq_desc_get_chip(desc);
509 raw_spin_lock(&desc->lock);
511 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
513 * If we're currently running this IRQ, or its disabled,
514 * we shouldn't process the IRQ. Mark it pending, handle
515 * the necessary masking and go out
517 if (unlikely(irqd_irq_disabled(&desc->irq_data) ||
518 irqd_irq_inprogress(&desc->irq_data) || !desc->action)) {
519 if (!irq_check_poll(desc)) {
520 desc->istate |= IRQS_PENDING;
524 kstat_incr_irqs_this_cpu(irq, desc);
527 if (unlikely(!desc->action))
530 handle_irq_event(desc);
532 } while ((desc->istate & IRQS_PENDING) &&
533 !irqd_irq_disabled(&desc->irq_data));
536 chip->irq_eoi(&desc->irq_data);
537 raw_spin_unlock(&desc->lock);
542 * handle_percpu_irq - Per CPU local irq handler
543 * @irq: the interrupt number
544 * @desc: the interrupt description structure for this irq
546 * Per CPU interrupts on SMP machines without locking requirements
549 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
551 struct irq_chip *chip = irq_desc_get_chip(desc);
553 kstat_incr_irqs_this_cpu(irq, desc);
556 chip->irq_ack(&desc->irq_data);
558 handle_irq_event_percpu(desc, desc->action);
561 chip->irq_eoi(&desc->irq_data);
565 * handle_percpu_devid_irq - Per CPU local irq handler with per cpu dev ids
566 * @irq: the interrupt number
567 * @desc: the interrupt description structure for this irq
569 * Per CPU interrupts on SMP machines without locking requirements. Same as
570 * handle_percpu_irq() above but with the following extras:
572 * action->percpu_dev_id is a pointer to percpu variables which
573 * contain the real device id for the cpu on which this handler is
576 void handle_percpu_devid_irq(unsigned int irq, struct irq_desc *desc)
578 struct irq_chip *chip = irq_desc_get_chip(desc);
579 struct irqaction *action = desc->action;
580 void *dev_id = __this_cpu_ptr(action->percpu_dev_id);
583 kstat_incr_irqs_this_cpu(irq, desc);
586 chip->irq_ack(&desc->irq_data);
588 trace_irq_handler_entry(irq, action);
589 res = action->handler(irq, dev_id);
590 trace_irq_handler_exit(irq, action, res);
593 chip->irq_eoi(&desc->irq_data);
597 __irq_set_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
601 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, 0);
607 handle = handle_bad_irq;
609 if (WARN_ON(desc->irq_data.chip == &no_irq_chip))
614 if (handle == handle_bad_irq) {
615 if (desc->irq_data.chip != &no_irq_chip)
617 irq_state_set_disabled(desc);
620 desc->handle_irq = handle;
623 if (handle != handle_bad_irq && is_chained) {
624 irq_settings_set_noprobe(desc);
625 irq_settings_set_norequest(desc);
626 irq_settings_set_nothread(desc);
630 irq_put_desc_busunlock(desc, flags);
632 EXPORT_SYMBOL_GPL(__irq_set_handler);
635 irq_set_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
636 irq_flow_handler_t handle, const char *name)
638 irq_set_chip(irq, chip);
639 __irq_set_handler(irq, handle, 0, name);
642 void irq_modify_status(unsigned int irq, unsigned long clr, unsigned long set)
645 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
649 irq_settings_clr_and_set(desc, clr, set);
651 irqd_clear(&desc->irq_data, IRQD_NO_BALANCING | IRQD_PER_CPU |
652 IRQD_TRIGGER_MASK | IRQD_LEVEL | IRQD_MOVE_PCNTXT);
653 if (irq_settings_has_no_balance_set(desc))
654 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
655 if (irq_settings_is_per_cpu(desc))
656 irqd_set(&desc->irq_data, IRQD_PER_CPU);
657 if (irq_settings_can_move_pcntxt(desc))
658 irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
659 if (irq_settings_is_level(desc))
660 irqd_set(&desc->irq_data, IRQD_LEVEL);
662 irqd_set(&desc->irq_data, irq_settings_get_trigger_mask(desc));
664 irq_put_desc_unlock(desc, flags);
666 EXPORT_SYMBOL_GPL(irq_modify_status);
669 * irq_cpu_online - Invoke all irq_cpu_online functions.
671 * Iterate through all irqs and invoke the chip.irq_cpu_online()
674 void irq_cpu_online(void)
676 struct irq_desc *desc;
677 struct irq_chip *chip;
681 for_each_active_irq(irq) {
682 desc = irq_to_desc(irq);
686 raw_spin_lock_irqsave(&desc->lock, flags);
688 chip = irq_data_get_irq_chip(&desc->irq_data);
689 if (chip && chip->irq_cpu_online &&
690 (!(chip->flags & IRQCHIP_ONOFFLINE_ENABLED) ||
691 !irqd_irq_disabled(&desc->irq_data)))
692 chip->irq_cpu_online(&desc->irq_data);
694 raw_spin_unlock_irqrestore(&desc->lock, flags);
699 * irq_cpu_offline - Invoke all irq_cpu_offline functions.
701 * Iterate through all irqs and invoke the chip.irq_cpu_offline()
704 void irq_cpu_offline(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_offline &&
720 (!(chip->flags & IRQCHIP_ONOFFLINE_ENABLED) ||
721 !irqd_irq_disabled(&desc->irq_data)))
722 chip->irq_cpu_offline(&desc->irq_data);
724 raw_spin_unlock_irqrestore(&desc->lock, flags);