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
);
42 EXPORT_SYMBOL(irq_set_chip
);
45 * irq_set_type - set the irq trigger type for an irq
47 * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
49 int irq_set_irq_type(unsigned int irq
, unsigned int type
)
52 struct irq_desc
*desc
= irq_get_desc_buslock(irq
, &flags
);
58 type
&= IRQ_TYPE_SENSE_MASK
;
59 if (type
!= IRQ_TYPE_NONE
)
60 ret
= __irq_set_trigger(desc
, irq
, type
);
61 irq_put_desc_busunlock(desc
, flags
);
64 EXPORT_SYMBOL(irq_set_irq_type
);
67 * irq_set_handler_data - set irq handler data for an irq
68 * @irq: Interrupt number
69 * @data: Pointer to interrupt specific data
71 * Set the hardware irq controller data for an irq
73 int irq_set_handler_data(unsigned int irq
, void *data
)
76 struct irq_desc
*desc
= irq_get_desc_lock(irq
, &flags
);
80 desc
->irq_data
.handler_data
= data
;
81 irq_put_desc_unlock(desc
, flags
);
84 EXPORT_SYMBOL(irq_set_handler_data
);
87 * irq_set_msi_desc - set MSI descriptor data for an irq
88 * @irq: Interrupt number
89 * @entry: Pointer to MSI descriptor data
91 * Set the MSI descriptor entry for an irq
93 int irq_set_msi_desc(unsigned int irq
, struct msi_desc
*entry
)
96 struct irq_desc
*desc
= irq_get_desc_lock(irq
, &flags
);
100 desc
->irq_data
.msi_desc
= entry
;
103 irq_put_desc_unlock(desc
, flags
);
108 * irq_set_chip_data - set irq chip data for an irq
109 * @irq: Interrupt number
110 * @data: Pointer to chip specific data
112 * Set the hardware irq chip data for an irq
114 int irq_set_chip_data(unsigned int irq
, void *data
)
117 struct irq_desc
*desc
= irq_get_desc_lock(irq
, &flags
);
121 desc
->irq_data
.chip_data
= data
;
122 irq_put_desc_unlock(desc
, flags
);
125 EXPORT_SYMBOL(irq_set_chip_data
);
127 struct irq_data
*irq_get_irq_data(unsigned int irq
)
129 struct irq_desc
*desc
= irq_to_desc(irq
);
131 return desc
? &desc
->irq_data
: NULL
;
133 EXPORT_SYMBOL_GPL(irq_get_irq_data
);
135 static void irq_state_clr_disabled(struct irq_desc
*desc
)
137 desc
->istate
&= ~IRQS_DISABLED
;
138 irq_compat_clr_disabled(desc
);
141 static void irq_state_set_disabled(struct irq_desc
*desc
)
143 desc
->istate
|= IRQS_DISABLED
;
144 irq_compat_set_disabled(desc
);
147 static void irq_state_clr_masked(struct irq_desc
*desc
)
149 desc
->istate
&= ~IRQS_MASKED
;
150 irq_compat_clr_masked(desc
);
153 static void irq_state_set_masked(struct irq_desc
*desc
)
155 desc
->istate
|= IRQS_MASKED
;
156 irq_compat_set_masked(desc
);
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 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 #ifndef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED
207 /* Temporary migration helpers */
208 static void compat_irq_mask(struct irq_data
*data
)
210 data
->chip
->mask(data
->irq
);
213 static void compat_irq_unmask(struct irq_data
*data
)
215 data
->chip
->unmask(data
->irq
);
218 static void compat_irq_ack(struct irq_data
*data
)
220 data
->chip
->ack(data
->irq
);
223 static void compat_irq_mask_ack(struct irq_data
*data
)
225 data
->chip
->mask_ack(data
->irq
);
228 static void compat_irq_eoi(struct irq_data
*data
)
230 data
->chip
->eoi(data
->irq
);
233 static void compat_irq_enable(struct irq_data
*data
)
235 data
->chip
->enable(data
->irq
);
238 static void compat_irq_disable(struct irq_data
*data
)
240 data
->chip
->disable(data
->irq
);
243 static void compat_irq_shutdown(struct irq_data
*data
)
245 data
->chip
->shutdown(data
->irq
);
248 static unsigned int compat_irq_startup(struct irq_data
*data
)
250 return data
->chip
->startup(data
->irq
);
253 static int compat_irq_set_affinity(struct irq_data
*data
,
254 const struct cpumask
*dest
, bool force
)
256 return data
->chip
->set_affinity(data
->irq
, dest
);
259 static int compat_irq_set_type(struct irq_data
*data
, unsigned int type
)
261 return data
->chip
->set_type(data
->irq
, type
);
264 static int compat_irq_set_wake(struct irq_data
*data
, unsigned int on
)
266 return data
->chip
->set_wake(data
->irq
, on
);
269 static int compat_irq_retrigger(struct irq_data
*data
)
271 return data
->chip
->retrigger(data
->irq
);
274 static void compat_bus_lock(struct irq_data
*data
)
276 data
->chip
->bus_lock(data
->irq
);
279 static void compat_bus_sync_unlock(struct irq_data
*data
)
281 data
->chip
->bus_sync_unlock(data
->irq
);
286 * Fixup enable/disable function pointers
288 void irq_chip_set_defaults(struct irq_chip
*chip
)
290 #ifndef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED
292 chip
->irq_enable
= compat_irq_enable
;
294 chip
->irq_disable
= compat_irq_disable
;
296 chip
->irq_shutdown
= compat_irq_shutdown
;
298 chip
->irq_startup
= compat_irq_startup
;
300 chip
->end
= dummy_irq_chip
.end
;
302 chip
->irq_bus_lock
= compat_bus_lock
;
303 if (chip
->bus_sync_unlock
)
304 chip
->irq_bus_sync_unlock
= compat_bus_sync_unlock
;
306 chip
->irq_mask
= compat_irq_mask
;
308 chip
->irq_unmask
= compat_irq_unmask
;
310 chip
->irq_ack
= compat_irq_ack
;
312 chip
->irq_mask_ack
= compat_irq_mask_ack
;
314 chip
->irq_eoi
= compat_irq_eoi
;
315 if (chip
->set_affinity
)
316 chip
->irq_set_affinity
= compat_irq_set_affinity
;
318 chip
->irq_set_type
= compat_irq_set_type
;
320 chip
->irq_set_wake
= compat_irq_set_wake
;
322 chip
->irq_retrigger
= compat_irq_retrigger
;
326 static inline void mask_ack_irq(struct irq_desc
*desc
)
328 if (desc
->irq_data
.chip
->irq_mask_ack
)
329 desc
->irq_data
.chip
->irq_mask_ack(&desc
->irq_data
);
331 desc
->irq_data
.chip
->irq_mask(&desc
->irq_data
);
332 if (desc
->irq_data
.chip
->irq_ack
)
333 desc
->irq_data
.chip
->irq_ack(&desc
->irq_data
);
335 irq_state_set_masked(desc
);
338 void mask_irq(struct irq_desc
*desc
)
340 if (desc
->irq_data
.chip
->irq_mask
) {
341 desc
->irq_data
.chip
->irq_mask(&desc
->irq_data
);
342 irq_state_set_masked(desc
);
346 void unmask_irq(struct irq_desc
*desc
)
348 if (desc
->irq_data
.chip
->irq_unmask
) {
349 desc
->irq_data
.chip
->irq_unmask(&desc
->irq_data
);
350 irq_state_clr_masked(desc
);
355 * handle_nested_irq - Handle a nested irq from a irq thread
356 * @irq: the interrupt number
358 * Handle interrupts which are nested into a threaded interrupt
359 * handler. The handler function is called inside the calling
362 void handle_nested_irq(unsigned int irq
)
364 struct irq_desc
*desc
= irq_to_desc(irq
);
365 struct irqaction
*action
;
366 irqreturn_t action_ret
;
370 raw_spin_lock_irq(&desc
->lock
);
372 kstat_incr_irqs_this_cpu(irq
, desc
);
374 action
= desc
->action
;
375 if (unlikely(!action
|| (desc
->istate
& IRQS_DISABLED
)))
378 irq_compat_set_progress(desc
);
379 desc
->istate
|= IRQS_INPROGRESS
;
380 raw_spin_unlock_irq(&desc
->lock
);
382 action_ret
= action
->thread_fn(action
->irq
, action
->dev_id
);
384 note_interrupt(irq
, desc
, action_ret
);
386 raw_spin_lock_irq(&desc
->lock
);
387 desc
->istate
&= ~IRQS_INPROGRESS
;
388 irq_compat_clr_progress(desc
);
391 raw_spin_unlock_irq(&desc
->lock
);
393 EXPORT_SYMBOL_GPL(handle_nested_irq
);
395 static bool irq_check_poll(struct irq_desc
*desc
)
397 if (!(desc
->istate
& IRQS_POLL_INPROGRESS
))
399 return irq_wait_for_poll(desc
);
403 * handle_simple_irq - Simple and software-decoded IRQs.
404 * @irq: the interrupt number
405 * @desc: the interrupt description structure for this irq
407 * Simple interrupts are either sent from a demultiplexing interrupt
408 * handler or come from hardware, where no interrupt hardware control
411 * Note: The caller is expected to handle the ack, clear, mask and
412 * unmask issues if necessary.
415 handle_simple_irq(unsigned int irq
, struct irq_desc
*desc
)
417 raw_spin_lock(&desc
->lock
);
419 if (unlikely(desc
->istate
& IRQS_INPROGRESS
))
420 if (!irq_check_poll(desc
))
423 desc
->istate
&= ~(IRQS_REPLAY
| IRQS_WAITING
);
424 kstat_incr_irqs_this_cpu(irq
, desc
);
426 if (unlikely(!desc
->action
|| (desc
->istate
& IRQS_DISABLED
)))
429 handle_irq_event(desc
);
432 raw_spin_unlock(&desc
->lock
);
436 * handle_level_irq - Level type irq handler
437 * @irq: the interrupt number
438 * @desc: the interrupt description structure for this irq
440 * Level type interrupts are active as long as the hardware line has
441 * the active level. This may require to mask the interrupt and unmask
442 * it after the associated handler has acknowledged the device, so the
443 * interrupt line is back to inactive.
446 handle_level_irq(unsigned int irq
, struct irq_desc
*desc
)
448 raw_spin_lock(&desc
->lock
);
451 if (unlikely(desc
->istate
& IRQS_INPROGRESS
))
452 if (!irq_check_poll(desc
))
455 desc
->istate
&= ~(IRQS_REPLAY
| IRQS_WAITING
);
456 kstat_incr_irqs_this_cpu(irq
, desc
);
459 * If its disabled or no action available
460 * keep it masked and get out of here
462 if (unlikely(!desc
->action
|| (desc
->istate
& IRQS_DISABLED
)))
465 handle_irq_event(desc
);
467 if (!(desc
->istate
& (IRQS_DISABLED
| IRQS_ONESHOT
)))
470 raw_spin_unlock(&desc
->lock
);
472 EXPORT_SYMBOL_GPL(handle_level_irq
);
474 #ifdef CONFIG_IRQ_PREFLOW_FASTEOI
475 static inline void preflow_handler(struct irq_desc
*desc
)
477 if (desc
->preflow_handler
)
478 desc
->preflow_handler(&desc
->irq_data
);
481 static inline void preflow_handler(struct irq_desc
*desc
) { }
485 * handle_fasteoi_irq - irq handler for transparent controllers
486 * @irq: the interrupt number
487 * @desc: the interrupt description structure for this irq
489 * Only a single callback will be issued to the chip: an ->eoi()
490 * call when the interrupt has been serviced. This enables support
491 * for modern forms of interrupt handlers, which handle the flow
492 * details in hardware, transparently.
495 handle_fasteoi_irq(unsigned int irq
, struct irq_desc
*desc
)
497 raw_spin_lock(&desc
->lock
);
499 if (unlikely(desc
->istate
& IRQS_INPROGRESS
))
500 if (!irq_check_poll(desc
))
503 desc
->istate
&= ~(IRQS_REPLAY
| IRQS_WAITING
);
504 kstat_incr_irqs_this_cpu(irq
, desc
);
507 * If its disabled or no action available
508 * then mask it and get out of here:
510 if (unlikely(!desc
->action
|| (desc
->istate
& IRQS_DISABLED
))) {
511 irq_compat_set_pending(desc
);
512 desc
->istate
|= IRQS_PENDING
;
517 if (desc
->istate
& IRQS_ONESHOT
)
520 preflow_handler(desc
);
521 handle_irq_event(desc
);
524 desc
->irq_data
.chip
->irq_eoi(&desc
->irq_data
);
526 raw_spin_unlock(&desc
->lock
);
529 if (!(desc
->irq_data
.chip
->flags
& IRQCHIP_EOI_IF_HANDLED
))
535 * handle_edge_irq - edge type IRQ handler
536 * @irq: the interrupt number
537 * @desc: the interrupt description structure for this irq
539 * Interrupt occures on the falling and/or rising edge of a hardware
540 * signal. The occurence is latched into the irq controller hardware
541 * and must be acked in order to be reenabled. After the ack another
542 * interrupt can happen on the same source even before the first one
543 * is handled by the associated event handler. If this happens it
544 * might be necessary to disable (mask) the interrupt depending on the
545 * controller hardware. This requires to reenable the interrupt inside
546 * of the loop which handles the interrupts which have arrived while
547 * the handler was running. If all pending interrupts are handled, the
551 handle_edge_irq(unsigned int irq
, struct irq_desc
*desc
)
553 raw_spin_lock(&desc
->lock
);
555 desc
->istate
&= ~(IRQS_REPLAY
| IRQS_WAITING
);
557 * If we're currently running this IRQ, or its disabled,
558 * we shouldn't process the IRQ. Mark it pending, handle
559 * the necessary masking and go out
561 if (unlikely((desc
->istate
& (IRQS_DISABLED
| IRQS_INPROGRESS
) ||
563 if (!irq_check_poll(desc
)) {
564 irq_compat_set_pending(desc
);
565 desc
->istate
|= IRQS_PENDING
;
570 kstat_incr_irqs_this_cpu(irq
, desc
);
572 /* Start handling the irq */
573 desc
->irq_data
.chip
->irq_ack(&desc
->irq_data
);
576 if (unlikely(!desc
->action
)) {
582 * When another irq arrived while we were handling
583 * one, we could have masked the irq.
584 * Renable it, if it was not disabled in meantime.
586 if (unlikely(desc
->istate
& IRQS_PENDING
)) {
587 if (!(desc
->istate
& IRQS_DISABLED
) &&
588 (desc
->istate
& IRQS_MASKED
))
592 handle_irq_event(desc
);
594 } while ((desc
->istate
& IRQS_PENDING
) &&
595 !(desc
->istate
& IRQS_DISABLED
));
598 raw_spin_unlock(&desc
->lock
);
602 * handle_percpu_irq - Per CPU local irq handler
603 * @irq: the interrupt number
604 * @desc: the interrupt description structure for this irq
606 * Per CPU interrupts on SMP machines without locking requirements
609 handle_percpu_irq(unsigned int irq
, struct irq_desc
*desc
)
611 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
613 kstat_incr_irqs_this_cpu(irq
, desc
);
616 chip
->irq_ack(&desc
->irq_data
);
618 handle_irq_event_percpu(desc
, desc
->action
);
621 chip
->irq_eoi(&desc
->irq_data
);
625 __irq_set_handler(unsigned int irq
, irq_flow_handler_t handle
, int is_chained
,
629 struct irq_desc
*desc
= irq_get_desc_buslock(irq
, &flags
);
635 handle
= handle_bad_irq
;
637 if (WARN_ON(desc
->irq_data
.chip
== &no_irq_chip
))
642 if (handle
== handle_bad_irq
) {
643 if (desc
->irq_data
.chip
!= &no_irq_chip
)
645 irq_compat_set_disabled(desc
);
646 desc
->istate
|= IRQS_DISABLED
;
649 desc
->handle_irq
= handle
;
652 if (handle
!= handle_bad_irq
&& is_chained
) {
653 irq_settings_set_noprobe(desc
);
654 irq_settings_set_norequest(desc
);
658 irq_put_desc_busunlock(desc
, flags
);
660 EXPORT_SYMBOL_GPL(__irq_set_handler
);
663 irq_set_chip_and_handler_name(unsigned int irq
, struct irq_chip
*chip
,
664 irq_flow_handler_t handle
, const char *name
)
666 irq_set_chip(irq
, chip
);
667 __irq_set_handler(irq
, handle
, 0, name
);
670 void irq_modify_status(unsigned int irq
, unsigned long clr
, unsigned long set
)
673 struct irq_desc
*desc
= irq_get_desc_lock(irq
, &flags
);
677 irq_settings_clr_and_set(desc
, clr
, set
);
679 irqd_clear(&desc
->irq_data
, IRQD_NO_BALANCING
| IRQD_PER_CPU
|
680 IRQD_TRIGGER_MASK
| IRQD_LEVEL
| IRQD_MOVE_PCNTXT
);
681 if (irq_settings_has_no_balance_set(desc
))
682 irqd_set(&desc
->irq_data
, IRQD_NO_BALANCING
);
683 if (irq_settings_is_per_cpu(desc
))
684 irqd_set(&desc
->irq_data
, IRQD_PER_CPU
);
685 if (irq_settings_can_move_pcntxt(desc
))
686 irqd_set(&desc
->irq_data
, IRQD_MOVE_PCNTXT
);
688 irqd_set(&desc
->irq_data
, irq_settings_get_trigger_mask(desc
));
690 irq_put_desc_unlock(desc
, flags
);