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 <trace/events/irq.h>
21 #include "internals.h"
24 * irq_set_chip - set the irq chip for an irq
26 * @chip: pointer to irq chip description structure
28 int irq_set_chip(unsigned int irq
, struct irq_chip
*chip
)
31 struct irq_desc
*desc
= irq_get_desc_lock(irq
, &flags
, 0);
39 desc
->irq_data
.chip
= chip
;
40 irq_put_desc_unlock(desc
, flags
);
42 * For !CONFIG_SPARSE_IRQ make the irq show up in
43 * allocated_irqs. For the CONFIG_SPARSE_IRQ case, it is
44 * already marked, and this call is harmless.
49 EXPORT_SYMBOL(irq_set_chip
);
52 * irq_set_type - set the irq trigger type for an irq
54 * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
56 int irq_set_irq_type(unsigned int irq
, unsigned int type
)
59 struct irq_desc
*desc
= irq_get_desc_buslock(irq
, &flags
, IRQ_GET_DESC_CHECK_GLOBAL
);
65 type
&= IRQ_TYPE_SENSE_MASK
;
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
, 0);
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
, IRQ_GET_DESC_CHECK_GLOBAL
);
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
, 0);
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
);
146 static void irq_state_set_disabled(struct irq_desc
*desc
)
148 irqd_set(&desc
->irq_data
, IRQD_IRQ_DISABLED
);
151 static void irq_state_clr_masked(struct irq_desc
*desc
)
153 irqd_clear(&desc
->irq_data
, IRQD_IRQ_MASKED
);
156 static void irq_state_set_masked(struct irq_desc
*desc
)
158 irqd_set(&desc
->irq_data
, IRQD_IRQ_MASKED
);
161 int irq_startup(struct irq_desc
*desc
, bool resend
)
165 irq_state_clr_disabled(desc
);
168 if (desc
->irq_data
.chip
->irq_startup
) {
169 ret
= desc
->irq_data
.chip
->irq_startup(&desc
->irq_data
);
170 irq_state_clr_masked(desc
);
175 check_irq_resend(desc
, desc
->irq_data
.irq
);
179 void irq_shutdown(struct irq_desc
*desc
)
181 irq_state_set_disabled(desc
);
183 if (desc
->irq_data
.chip
->irq_shutdown
)
184 desc
->irq_data
.chip
->irq_shutdown(&desc
->irq_data
);
185 else if (desc
->irq_data
.chip
->irq_disable
)
186 desc
->irq_data
.chip
->irq_disable(&desc
->irq_data
);
188 desc
->irq_data
.chip
->irq_mask(&desc
->irq_data
);
189 irq_state_set_masked(desc
);
192 void irq_enable(struct irq_desc
*desc
)
194 irq_state_clr_disabled(desc
);
195 if (desc
->irq_data
.chip
->irq_enable
)
196 desc
->irq_data
.chip
->irq_enable(&desc
->irq_data
);
198 desc
->irq_data
.chip
->irq_unmask(&desc
->irq_data
);
199 irq_state_clr_masked(desc
);
202 void irq_disable(struct irq_desc
*desc
)
204 irq_state_set_disabled(desc
);
205 if (desc
->irq_data
.chip
->irq_disable
) {
206 desc
->irq_data
.chip
->irq_disable(&desc
->irq_data
);
207 irq_state_set_masked(desc
);
211 void irq_percpu_enable(struct irq_desc
*desc
, unsigned int cpu
)
213 if (desc
->irq_data
.chip
->irq_enable
)
214 desc
->irq_data
.chip
->irq_enable(&desc
->irq_data
);
216 desc
->irq_data
.chip
->irq_unmask(&desc
->irq_data
);
217 cpumask_set_cpu(cpu
, desc
->percpu_enabled
);
220 void irq_percpu_disable(struct irq_desc
*desc
, unsigned int cpu
)
222 if (desc
->irq_data
.chip
->irq_disable
)
223 desc
->irq_data
.chip
->irq_disable(&desc
->irq_data
);
225 desc
->irq_data
.chip
->irq_mask(&desc
->irq_data
);
226 cpumask_clear_cpu(cpu
, desc
->percpu_enabled
);
229 static inline void mask_ack_irq(struct irq_desc
*desc
)
231 if (desc
->irq_data
.chip
->irq_mask_ack
)
232 desc
->irq_data
.chip
->irq_mask_ack(&desc
->irq_data
);
234 desc
->irq_data
.chip
->irq_mask(&desc
->irq_data
);
235 if (desc
->irq_data
.chip
->irq_ack
)
236 desc
->irq_data
.chip
->irq_ack(&desc
->irq_data
);
238 irq_state_set_masked(desc
);
241 void mask_irq(struct irq_desc
*desc
)
243 if (desc
->irq_data
.chip
->irq_mask
) {
244 desc
->irq_data
.chip
->irq_mask(&desc
->irq_data
);
245 irq_state_set_masked(desc
);
249 void unmask_irq(struct irq_desc
*desc
)
251 if (desc
->irq_data
.chip
->irq_unmask
) {
252 desc
->irq_data
.chip
->irq_unmask(&desc
->irq_data
);
253 irq_state_clr_masked(desc
);
258 * handle_nested_irq - Handle a nested irq from a irq thread
259 * @irq: the interrupt number
261 * Handle interrupts which are nested into a threaded interrupt
262 * handler. The handler function is called inside the calling
265 void handle_nested_irq(unsigned int irq
)
267 struct irq_desc
*desc
= irq_to_desc(irq
);
268 struct irqaction
*action
;
269 irqreturn_t action_ret
;
273 raw_spin_lock_irq(&desc
->lock
);
275 kstat_incr_irqs_this_cpu(irq
, desc
);
277 action
= desc
->action
;
278 if (unlikely(!action
|| irqd_irq_disabled(&desc
->irq_data
))) {
279 desc
->istate
|= IRQS_PENDING
;
283 irqd_set(&desc
->irq_data
, IRQD_IRQ_INPROGRESS
);
284 raw_spin_unlock_irq(&desc
->lock
);
286 action_ret
= action
->thread_fn(action
->irq
, action
->dev_id
);
288 note_interrupt(irq
, desc
, action_ret
);
290 raw_spin_lock_irq(&desc
->lock
);
291 irqd_clear(&desc
->irq_data
, IRQD_IRQ_INPROGRESS
);
294 raw_spin_unlock_irq(&desc
->lock
);
296 EXPORT_SYMBOL_GPL(handle_nested_irq
);
298 static bool irq_check_poll(struct irq_desc
*desc
)
300 if (!(desc
->istate
& IRQS_POLL_INPROGRESS
))
302 return irq_wait_for_poll(desc
);
306 * handle_simple_irq - Simple and software-decoded IRQs.
307 * @irq: the interrupt number
308 * @desc: the interrupt description structure for this irq
310 * Simple interrupts are either sent from a demultiplexing interrupt
311 * handler or come from hardware, where no interrupt hardware control
314 * Note: The caller is expected to handle the ack, clear, mask and
315 * unmask issues if necessary.
318 handle_simple_irq(unsigned int irq
, struct irq_desc
*desc
)
320 raw_spin_lock(&desc
->lock
);
322 if (unlikely(irqd_irq_inprogress(&desc
->irq_data
)))
323 if (!irq_check_poll(desc
))
326 desc
->istate
&= ~(IRQS_REPLAY
| IRQS_WAITING
);
327 kstat_incr_irqs_this_cpu(irq
, desc
);
329 if (unlikely(!desc
->action
|| irqd_irq_disabled(&desc
->irq_data
))) {
330 desc
->istate
|= IRQS_PENDING
;
334 handle_irq_event(desc
);
337 raw_spin_unlock(&desc
->lock
);
339 EXPORT_SYMBOL_GPL(handle_simple_irq
);
342 * Called unconditionally from handle_level_irq() and only for oneshot
343 * interrupts from handle_fasteoi_irq()
345 static void cond_unmask_irq(struct irq_desc
*desc
)
348 * We need to unmask in the following cases:
349 * - Standard level irq (IRQF_ONESHOT is not set)
350 * - Oneshot irq which did not wake the thread (caused by a
351 * spurious interrupt or a primary handler handling it
354 if (!irqd_irq_disabled(&desc
->irq_data
) &&
355 irqd_irq_masked(&desc
->irq_data
) && !desc
->threads_oneshot
)
360 * handle_level_irq - Level type irq handler
361 * @irq: the interrupt number
362 * @desc: the interrupt description structure for this irq
364 * Level type interrupts are active as long as the hardware line has
365 * the active level. This may require to mask the interrupt and unmask
366 * it after the associated handler has acknowledged the device, so the
367 * interrupt line is back to inactive.
370 handle_level_irq(unsigned int irq
, struct irq_desc
*desc
)
372 raw_spin_lock(&desc
->lock
);
375 if (unlikely(irqd_irq_inprogress(&desc
->irq_data
)))
376 if (!irq_check_poll(desc
))
379 desc
->istate
&= ~(IRQS_REPLAY
| IRQS_WAITING
);
380 kstat_incr_irqs_this_cpu(irq
, desc
);
383 * If its disabled or no action available
384 * keep it masked and get out of here
386 if (unlikely(!desc
->action
|| irqd_irq_disabled(&desc
->irq_data
))) {
387 desc
->istate
|= IRQS_PENDING
;
391 handle_irq_event(desc
);
393 cond_unmask_irq(desc
);
396 raw_spin_unlock(&desc
->lock
);
398 EXPORT_SYMBOL_GPL(handle_level_irq
);
400 #ifdef CONFIG_IRQ_PREFLOW_FASTEOI
401 static inline void preflow_handler(struct irq_desc
*desc
)
403 if (desc
->preflow_handler
)
404 desc
->preflow_handler(&desc
->irq_data
);
407 static inline void preflow_handler(struct irq_desc
*desc
) { }
411 * handle_fasteoi_irq - irq handler for transparent controllers
412 * @irq: the interrupt number
413 * @desc: the interrupt description structure for this irq
415 * Only a single callback will be issued to the chip: an ->eoi()
416 * call when the interrupt has been serviced. This enables support
417 * for modern forms of interrupt handlers, which handle the flow
418 * details in hardware, transparently.
421 handle_fasteoi_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
);
433 * If its disabled or no action available
434 * then mask it and get out of here:
436 if (unlikely(!desc
->action
|| irqd_irq_disabled(&desc
->irq_data
))) {
437 desc
->istate
|= IRQS_PENDING
;
442 if (desc
->istate
& IRQS_ONESHOT
)
445 preflow_handler(desc
);
446 handle_irq_event(desc
);
448 if (desc
->istate
& IRQS_ONESHOT
)
449 cond_unmask_irq(desc
);
452 desc
->irq_data
.chip
->irq_eoi(&desc
->irq_data
);
454 raw_spin_unlock(&desc
->lock
);
457 if (!(desc
->irq_data
.chip
->flags
& IRQCHIP_EOI_IF_HANDLED
))
463 * handle_edge_irq - edge type IRQ handler
464 * @irq: the interrupt number
465 * @desc: the interrupt description structure for this irq
467 * Interrupt occures on the falling and/or rising edge of a hardware
468 * signal. The occurrence is latched into the irq controller hardware
469 * and must be acked in order to be reenabled. After the ack another
470 * interrupt can happen on the same source even before the first one
471 * is handled by the associated event handler. If this happens it
472 * might be necessary to disable (mask) the interrupt depending on the
473 * controller hardware. This requires to reenable the interrupt inside
474 * of the loop which handles the interrupts which have arrived while
475 * the handler was running. If all pending interrupts are handled, the
479 handle_edge_irq(unsigned int irq
, struct irq_desc
*desc
)
481 raw_spin_lock(&desc
->lock
);
483 desc
->istate
&= ~(IRQS_REPLAY
| IRQS_WAITING
);
485 * If we're currently running this IRQ, or its disabled,
486 * we shouldn't process the IRQ. Mark it pending, handle
487 * the necessary masking and go out
489 if (unlikely(irqd_irq_disabled(&desc
->irq_data
) ||
490 irqd_irq_inprogress(&desc
->irq_data
) || !desc
->action
)) {
491 if (!irq_check_poll(desc
)) {
492 desc
->istate
|= IRQS_PENDING
;
497 kstat_incr_irqs_this_cpu(irq
, desc
);
499 /* Start handling the irq */
500 desc
->irq_data
.chip
->irq_ack(&desc
->irq_data
);
503 if (unlikely(!desc
->action
)) {
509 * When another irq arrived while we were handling
510 * one, we could have masked the irq.
511 * Renable it, if it was not disabled in meantime.
513 if (unlikely(desc
->istate
& IRQS_PENDING
)) {
514 if (!irqd_irq_disabled(&desc
->irq_data
) &&
515 irqd_irq_masked(&desc
->irq_data
))
519 handle_irq_event(desc
);
521 } while ((desc
->istate
& IRQS_PENDING
) &&
522 !irqd_irq_disabled(&desc
->irq_data
));
525 raw_spin_unlock(&desc
->lock
);
527 EXPORT_SYMBOL(handle_edge_irq
);
529 #ifdef CONFIG_IRQ_EDGE_EOI_HANDLER
531 * handle_edge_eoi_irq - edge eoi type IRQ handler
532 * @irq: the interrupt number
533 * @desc: the interrupt description structure for this irq
535 * Similar as the above handle_edge_irq, but using eoi and w/o the
538 void handle_edge_eoi_irq(unsigned int irq
, struct irq_desc
*desc
)
540 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
542 raw_spin_lock(&desc
->lock
);
544 desc
->istate
&= ~(IRQS_REPLAY
| IRQS_WAITING
);
546 * If we're currently running this IRQ, or its disabled,
547 * we shouldn't process the IRQ. Mark it pending, handle
548 * the necessary masking and go out
550 if (unlikely(irqd_irq_disabled(&desc
->irq_data
) ||
551 irqd_irq_inprogress(&desc
->irq_data
) || !desc
->action
)) {
552 if (!irq_check_poll(desc
)) {
553 desc
->istate
|= IRQS_PENDING
;
557 kstat_incr_irqs_this_cpu(irq
, desc
);
560 if (unlikely(!desc
->action
))
563 handle_irq_event(desc
);
565 } while ((desc
->istate
& IRQS_PENDING
) &&
566 !irqd_irq_disabled(&desc
->irq_data
));
569 chip
->irq_eoi(&desc
->irq_data
);
570 raw_spin_unlock(&desc
->lock
);
575 * handle_percpu_irq - Per CPU local irq handler
576 * @irq: the interrupt number
577 * @desc: the interrupt description structure for this irq
579 * Per CPU interrupts on SMP machines without locking requirements
582 handle_percpu_irq(unsigned int irq
, struct irq_desc
*desc
)
584 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
586 kstat_incr_irqs_this_cpu(irq
, desc
);
589 chip
->irq_ack(&desc
->irq_data
);
591 handle_irq_event_percpu(desc
, desc
->action
);
594 chip
->irq_eoi(&desc
->irq_data
);
598 * handle_percpu_devid_irq - Per CPU local irq handler with per cpu dev ids
599 * @irq: the interrupt number
600 * @desc: the interrupt description structure for this irq
602 * Per CPU interrupts on SMP machines without locking requirements. Same as
603 * handle_percpu_irq() above but with the following extras:
605 * action->percpu_dev_id is a pointer to percpu variables which
606 * contain the real device id for the cpu on which this handler is
609 void handle_percpu_devid_irq(unsigned int irq
, struct irq_desc
*desc
)
611 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
612 struct irqaction
*action
= desc
->action
;
613 void *dev_id
= __this_cpu_ptr(action
->percpu_dev_id
);
616 kstat_incr_irqs_this_cpu(irq
, desc
);
619 chip
->irq_ack(&desc
->irq_data
);
621 trace_irq_handler_entry(irq
, action
);
622 res
= action
->handler(irq
, dev_id
);
623 trace_irq_handler_exit(irq
, action
, res
);
626 chip
->irq_eoi(&desc
->irq_data
);
630 __irq_set_handler(unsigned int irq
, irq_flow_handler_t handle
, int is_chained
,
634 struct irq_desc
*desc
= irq_get_desc_buslock(irq
, &flags
, 0);
640 handle
= handle_bad_irq
;
642 if (WARN_ON(desc
->irq_data
.chip
== &no_irq_chip
))
647 if (handle
== handle_bad_irq
) {
648 if (desc
->irq_data
.chip
!= &no_irq_chip
)
650 irq_state_set_disabled(desc
);
653 desc
->handle_irq
= handle
;
656 if (handle
!= handle_bad_irq
&& is_chained
) {
657 irq_settings_set_noprobe(desc
);
658 irq_settings_set_norequest(desc
);
659 irq_settings_set_nothread(desc
);
660 irq_startup(desc
, true);
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
);
674 EXPORT_SYMBOL_GPL(irq_set_chip_and_handler_name
);
676 void irq_modify_status(unsigned int irq
, unsigned long clr
, unsigned long set
)
679 struct irq_desc
*desc
= irq_get_desc_lock(irq
, &flags
, 0);
683 irq_settings_clr_and_set(desc
, clr
, set
);
685 irqd_clear(&desc
->irq_data
, IRQD_NO_BALANCING
| IRQD_PER_CPU
|
686 IRQD_TRIGGER_MASK
| IRQD_LEVEL
| IRQD_MOVE_PCNTXT
);
687 if (irq_settings_has_no_balance_set(desc
))
688 irqd_set(&desc
->irq_data
, IRQD_NO_BALANCING
);
689 if (irq_settings_is_per_cpu(desc
))
690 irqd_set(&desc
->irq_data
, IRQD_PER_CPU
);
691 if (irq_settings_can_move_pcntxt(desc
))
692 irqd_set(&desc
->irq_data
, IRQD_MOVE_PCNTXT
);
693 if (irq_settings_is_level(desc
))
694 irqd_set(&desc
->irq_data
, IRQD_LEVEL
);
696 irqd_set(&desc
->irq_data
, irq_settings_get_trigger_mask(desc
));
698 irq_put_desc_unlock(desc
, flags
);
700 EXPORT_SYMBOL_GPL(irq_modify_status
);
703 * irq_cpu_online - Invoke all irq_cpu_online functions.
705 * Iterate through all irqs and invoke the chip.irq_cpu_online()
708 void irq_cpu_online(void)
710 struct irq_desc
*desc
;
711 struct irq_chip
*chip
;
715 for_each_active_irq(irq
) {
716 desc
= irq_to_desc(irq
);
720 raw_spin_lock_irqsave(&desc
->lock
, flags
);
722 chip
= irq_data_get_irq_chip(&desc
->irq_data
);
723 if (chip
&& chip
->irq_cpu_online
&&
724 (!(chip
->flags
& IRQCHIP_ONOFFLINE_ENABLED
) ||
725 !irqd_irq_disabled(&desc
->irq_data
)))
726 chip
->irq_cpu_online(&desc
->irq_data
);
728 raw_spin_unlock_irqrestore(&desc
->lock
, flags
);
733 * irq_cpu_offline - Invoke all irq_cpu_offline functions.
735 * Iterate through all irqs and invoke the chip.irq_cpu_offline()
738 void irq_cpu_offline(void)
740 struct irq_desc
*desc
;
741 struct irq_chip
*chip
;
745 for_each_active_irq(irq
) {
746 desc
= irq_to_desc(irq
);
750 raw_spin_lock_irqsave(&desc
->lock
, flags
);
752 chip
= irq_data_get_irq_chip(&desc
->irq_data
);
753 if (chip
&& chip
->irq_cpu_offline
&&
754 (!(chip
->flags
& IRQCHIP_ONOFFLINE_ENABLED
) ||
755 !irqd_irq_disabled(&desc
->irq_data
)))
756 chip
->irq_cpu_offline(&desc
->irq_data
);
758 raw_spin_unlock_irqrestore(&desc
->lock
, flags
);