MAINTAINERS: Update qib and ipath entries from QLogic to Intel
[linux/fpc-iii.git] / kernel / irq / chip.c
blob6080f6bc8c33f452782c1d259bb14fb778b345c5
1 /*
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
8 * based architectures.
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"
23 /**
24 * irq_set_chip - set the irq chip for an irq
25 * @irq: irq number
26 * @chip: pointer to irq chip description structure
28 int irq_set_chip(unsigned int irq, struct irq_chip *chip)
30 unsigned long flags;
31 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
33 if (!desc)
34 return -EINVAL;
36 if (!chip)
37 chip = &no_irq_chip;
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.
46 irq_reserve_irq(irq);
47 return 0;
49 EXPORT_SYMBOL(irq_set_chip);
51 /**
52 * irq_set_type - set the irq trigger type for an irq
53 * @irq: irq number
54 * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
56 int irq_set_irq_type(unsigned int irq, unsigned int type)
58 unsigned long flags;
59 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
60 int ret = 0;
62 if (!desc)
63 return -EINVAL;
65 type &= IRQ_TYPE_SENSE_MASK;
66 ret = __irq_set_trigger(desc, irq, type);
67 irq_put_desc_busunlock(desc, flags);
68 return ret;
70 EXPORT_SYMBOL(irq_set_irq_type);
72 /**
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)
81 unsigned long flags;
82 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
84 if (!desc)
85 return -EINVAL;
86 desc->irq_data.handler_data = data;
87 irq_put_desc_unlock(desc, flags);
88 return 0;
90 EXPORT_SYMBOL(irq_set_handler_data);
92 /**
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)
101 unsigned long flags;
102 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
104 if (!desc)
105 return -EINVAL;
106 desc->irq_data.msi_desc = entry;
107 if (entry)
108 entry->irq = irq;
109 irq_put_desc_unlock(desc, flags);
110 return 0;
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)
122 unsigned long flags;
123 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
125 if (!desc)
126 return -EINVAL;
127 desc->irq_data.chip_data = data;
128 irq_put_desc_unlock(desc, flags);
129 return 0;
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)
163 int ret = 0;
165 irq_state_clr_disabled(desc);
166 desc->depth = 0;
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);
171 } else {
172 irq_enable(desc);
174 if (resend)
175 check_irq_resend(desc, desc->irq_data.irq);
176 return ret;
179 void irq_shutdown(struct irq_desc *desc)
181 irq_state_set_disabled(desc);
182 desc->depth = 1;
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);
187 else
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);
197 else
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);
215 else
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);
224 else
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);
233 else {
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
263 * threads context.
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;
271 might_sleep();
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 goto out_unlock;
281 irqd_set(&desc->irq_data, IRQD_IRQ_INPROGRESS);
282 raw_spin_unlock_irq(&desc->lock);
284 action_ret = action->thread_fn(action->irq, action->dev_id);
285 if (!noirqdebug)
286 note_interrupt(irq, desc, action_ret);
288 raw_spin_lock_irq(&desc->lock);
289 irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
291 out_unlock:
292 raw_spin_unlock_irq(&desc->lock);
294 EXPORT_SYMBOL_GPL(handle_nested_irq);
296 static bool irq_check_poll(struct irq_desc *desc)
298 if (!(desc->istate & IRQS_POLL_INPROGRESS))
299 return false;
300 return irq_wait_for_poll(desc);
304 * handle_simple_irq - Simple and software-decoded IRQs.
305 * @irq: the interrupt number
306 * @desc: the interrupt description structure for this irq
308 * Simple interrupts are either sent from a demultiplexing interrupt
309 * handler or come from hardware, where no interrupt hardware control
310 * is necessary.
312 * Note: The caller is expected to handle the ack, clear, mask and
313 * unmask issues if necessary.
315 void
316 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
318 raw_spin_lock(&desc->lock);
320 if (unlikely(irqd_irq_inprogress(&desc->irq_data)))
321 if (!irq_check_poll(desc))
322 goto out_unlock;
324 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
325 kstat_incr_irqs_this_cpu(irq, desc);
327 if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data)))
328 goto out_unlock;
330 handle_irq_event(desc);
332 out_unlock:
333 raw_spin_unlock(&desc->lock);
335 EXPORT_SYMBOL_GPL(handle_simple_irq);
338 * Called unconditionally from handle_level_irq() and only for oneshot
339 * interrupts from handle_fasteoi_irq()
341 static void cond_unmask_irq(struct irq_desc *desc)
344 * We need to unmask in the following cases:
345 * - Standard level irq (IRQF_ONESHOT is not set)
346 * - Oneshot irq which did not wake the thread (caused by a
347 * spurious interrupt or a primary handler handling it
348 * completely).
350 if (!irqd_irq_disabled(&desc->irq_data) &&
351 irqd_irq_masked(&desc->irq_data) && !desc->threads_oneshot)
352 unmask_irq(desc);
356 * handle_level_irq - Level type irq handler
357 * @irq: the interrupt number
358 * @desc: the interrupt description structure for this irq
360 * Level type interrupts are active as long as the hardware line has
361 * the active level. This may require to mask the interrupt and unmask
362 * it after the associated handler has acknowledged the device, so the
363 * interrupt line is back to inactive.
365 void
366 handle_level_irq(unsigned int irq, struct irq_desc *desc)
368 raw_spin_lock(&desc->lock);
369 mask_ack_irq(desc);
371 if (unlikely(irqd_irq_inprogress(&desc->irq_data)))
372 if (!irq_check_poll(desc))
373 goto out_unlock;
375 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
376 kstat_incr_irqs_this_cpu(irq, desc);
379 * If its disabled or no action available
380 * keep it masked and get out of here
382 if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data)))
383 goto out_unlock;
385 handle_irq_event(desc);
387 cond_unmask_irq(desc);
389 out_unlock:
390 raw_spin_unlock(&desc->lock);
392 EXPORT_SYMBOL_GPL(handle_level_irq);
394 #ifdef CONFIG_IRQ_PREFLOW_FASTEOI
395 static inline void preflow_handler(struct irq_desc *desc)
397 if (desc->preflow_handler)
398 desc->preflow_handler(&desc->irq_data);
400 #else
401 static inline void preflow_handler(struct irq_desc *desc) { }
402 #endif
405 * handle_fasteoi_irq - irq handler for transparent controllers
406 * @irq: the interrupt number
407 * @desc: the interrupt description structure for this irq
409 * Only a single callback will be issued to the chip: an ->eoi()
410 * call when the interrupt has been serviced. This enables support
411 * for modern forms of interrupt handlers, which handle the flow
412 * details in hardware, transparently.
414 void
415 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
417 raw_spin_lock(&desc->lock);
419 if (unlikely(irqd_irq_inprogress(&desc->irq_data)))
420 if (!irq_check_poll(desc))
421 goto out;
423 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
424 kstat_incr_irqs_this_cpu(irq, desc);
427 * If its disabled or no action available
428 * then mask it and get out of here:
430 if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data))) {
431 desc->istate |= IRQS_PENDING;
432 mask_irq(desc);
433 goto out;
436 if (desc->istate & IRQS_ONESHOT)
437 mask_irq(desc);
439 preflow_handler(desc);
440 handle_irq_event(desc);
442 if (desc->istate & IRQS_ONESHOT)
443 cond_unmask_irq(desc);
445 out_eoi:
446 desc->irq_data.chip->irq_eoi(&desc->irq_data);
447 out_unlock:
448 raw_spin_unlock(&desc->lock);
449 return;
450 out:
451 if (!(desc->irq_data.chip->flags & IRQCHIP_EOI_IF_HANDLED))
452 goto out_eoi;
453 goto out_unlock;
457 * handle_edge_irq - edge type IRQ handler
458 * @irq: the interrupt number
459 * @desc: the interrupt description structure for this irq
461 * Interrupt occures on the falling and/or rising edge of a hardware
462 * signal. The occurrence is latched into the irq controller hardware
463 * and must be acked in order to be reenabled. After the ack another
464 * interrupt can happen on the same source even before the first one
465 * is handled by the associated event handler. If this happens it
466 * might be necessary to disable (mask) the interrupt depending on the
467 * controller hardware. This requires to reenable the interrupt inside
468 * of the loop which handles the interrupts which have arrived while
469 * the handler was running. If all pending interrupts are handled, the
470 * loop is left.
472 void
473 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
475 raw_spin_lock(&desc->lock);
477 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
479 * If we're currently running this IRQ, or its disabled,
480 * we shouldn't process the IRQ. Mark it pending, handle
481 * the necessary masking and go out
483 if (unlikely(irqd_irq_disabled(&desc->irq_data) ||
484 irqd_irq_inprogress(&desc->irq_data) || !desc->action)) {
485 if (!irq_check_poll(desc)) {
486 desc->istate |= IRQS_PENDING;
487 mask_ack_irq(desc);
488 goto out_unlock;
491 kstat_incr_irqs_this_cpu(irq, desc);
493 /* Start handling the irq */
494 desc->irq_data.chip->irq_ack(&desc->irq_data);
496 do {
497 if (unlikely(!desc->action)) {
498 mask_irq(desc);
499 goto out_unlock;
503 * When another irq arrived while we were handling
504 * one, we could have masked the irq.
505 * Renable it, if it was not disabled in meantime.
507 if (unlikely(desc->istate & IRQS_PENDING)) {
508 if (!irqd_irq_disabled(&desc->irq_data) &&
509 irqd_irq_masked(&desc->irq_data))
510 unmask_irq(desc);
513 handle_irq_event(desc);
515 } while ((desc->istate & IRQS_PENDING) &&
516 !irqd_irq_disabled(&desc->irq_data));
518 out_unlock:
519 raw_spin_unlock(&desc->lock);
522 #ifdef CONFIG_IRQ_EDGE_EOI_HANDLER
524 * handle_edge_eoi_irq - edge eoi type IRQ handler
525 * @irq: the interrupt number
526 * @desc: the interrupt description structure for this irq
528 * Similar as the above handle_edge_irq, but using eoi and w/o the
529 * mask/unmask logic.
531 void handle_edge_eoi_irq(unsigned int irq, struct irq_desc *desc)
533 struct irq_chip *chip = irq_desc_get_chip(desc);
535 raw_spin_lock(&desc->lock);
537 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
539 * If we're currently running this IRQ, or its disabled,
540 * we shouldn't process the IRQ. Mark it pending, handle
541 * the necessary masking and go out
543 if (unlikely(irqd_irq_disabled(&desc->irq_data) ||
544 irqd_irq_inprogress(&desc->irq_data) || !desc->action)) {
545 if (!irq_check_poll(desc)) {
546 desc->istate |= IRQS_PENDING;
547 goto out_eoi;
550 kstat_incr_irqs_this_cpu(irq, desc);
552 do {
553 if (unlikely(!desc->action))
554 goto out_eoi;
556 handle_irq_event(desc);
558 } while ((desc->istate & IRQS_PENDING) &&
559 !irqd_irq_disabled(&desc->irq_data));
561 out_eoi:
562 chip->irq_eoi(&desc->irq_data);
563 raw_spin_unlock(&desc->lock);
565 #endif
568 * handle_percpu_irq - Per CPU local irq handler
569 * @irq: the interrupt number
570 * @desc: the interrupt description structure for this irq
572 * Per CPU interrupts on SMP machines without locking requirements
574 void
575 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
577 struct irq_chip *chip = irq_desc_get_chip(desc);
579 kstat_incr_irqs_this_cpu(irq, desc);
581 if (chip->irq_ack)
582 chip->irq_ack(&desc->irq_data);
584 handle_irq_event_percpu(desc, desc->action);
586 if (chip->irq_eoi)
587 chip->irq_eoi(&desc->irq_data);
591 * handle_percpu_devid_irq - Per CPU local irq handler with per cpu dev ids
592 * @irq: the interrupt number
593 * @desc: the interrupt description structure for this irq
595 * Per CPU interrupts on SMP machines without locking requirements. Same as
596 * handle_percpu_irq() above but with the following extras:
598 * action->percpu_dev_id is a pointer to percpu variables which
599 * contain the real device id for the cpu on which this handler is
600 * called
602 void handle_percpu_devid_irq(unsigned int irq, struct irq_desc *desc)
604 struct irq_chip *chip = irq_desc_get_chip(desc);
605 struct irqaction *action = desc->action;
606 void *dev_id = __this_cpu_ptr(action->percpu_dev_id);
607 irqreturn_t res;
609 kstat_incr_irqs_this_cpu(irq, desc);
611 if (chip->irq_ack)
612 chip->irq_ack(&desc->irq_data);
614 trace_irq_handler_entry(irq, action);
615 res = action->handler(irq, dev_id);
616 trace_irq_handler_exit(irq, action, res);
618 if (chip->irq_eoi)
619 chip->irq_eoi(&desc->irq_data);
622 void
623 __irq_set_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
624 const char *name)
626 unsigned long flags;
627 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, 0);
629 if (!desc)
630 return;
632 if (!handle) {
633 handle = handle_bad_irq;
634 } else {
635 if (WARN_ON(desc->irq_data.chip == &no_irq_chip))
636 goto out;
639 /* Uninstall? */
640 if (handle == handle_bad_irq) {
641 if (desc->irq_data.chip != &no_irq_chip)
642 mask_ack_irq(desc);
643 irq_state_set_disabled(desc);
644 desc->depth = 1;
646 desc->handle_irq = handle;
647 desc->name = name;
649 if (handle != handle_bad_irq && is_chained) {
650 irq_settings_set_noprobe(desc);
651 irq_settings_set_norequest(desc);
652 irq_settings_set_nothread(desc);
653 irq_startup(desc, true);
655 out:
656 irq_put_desc_busunlock(desc, flags);
658 EXPORT_SYMBOL_GPL(__irq_set_handler);
660 void
661 irq_set_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
662 irq_flow_handler_t handle, const char *name)
664 irq_set_chip(irq, chip);
665 __irq_set_handler(irq, handle, 0, name);
668 void irq_modify_status(unsigned int irq, unsigned long clr, unsigned long set)
670 unsigned long flags;
671 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
673 if (!desc)
674 return;
675 irq_settings_clr_and_set(desc, clr, set);
677 irqd_clear(&desc->irq_data, IRQD_NO_BALANCING | IRQD_PER_CPU |
678 IRQD_TRIGGER_MASK | IRQD_LEVEL | IRQD_MOVE_PCNTXT);
679 if (irq_settings_has_no_balance_set(desc))
680 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
681 if (irq_settings_is_per_cpu(desc))
682 irqd_set(&desc->irq_data, IRQD_PER_CPU);
683 if (irq_settings_can_move_pcntxt(desc))
684 irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
685 if (irq_settings_is_level(desc))
686 irqd_set(&desc->irq_data, IRQD_LEVEL);
688 irqd_set(&desc->irq_data, irq_settings_get_trigger_mask(desc));
690 irq_put_desc_unlock(desc, flags);
692 EXPORT_SYMBOL_GPL(irq_modify_status);
695 * irq_cpu_online - Invoke all irq_cpu_online functions.
697 * Iterate through all irqs and invoke the chip.irq_cpu_online()
698 * for each.
700 void irq_cpu_online(void)
702 struct irq_desc *desc;
703 struct irq_chip *chip;
704 unsigned long flags;
705 unsigned int irq;
707 for_each_active_irq(irq) {
708 desc = irq_to_desc(irq);
709 if (!desc)
710 continue;
712 raw_spin_lock_irqsave(&desc->lock, flags);
714 chip = irq_data_get_irq_chip(&desc->irq_data);
715 if (chip && chip->irq_cpu_online &&
716 (!(chip->flags & IRQCHIP_ONOFFLINE_ENABLED) ||
717 !irqd_irq_disabled(&desc->irq_data)))
718 chip->irq_cpu_online(&desc->irq_data);
720 raw_spin_unlock_irqrestore(&desc->lock, flags);
725 * irq_cpu_offline - Invoke all irq_cpu_offline functions.
727 * Iterate through all irqs and invoke the chip.irq_cpu_offline()
728 * for each.
730 void irq_cpu_offline(void)
732 struct irq_desc *desc;
733 struct irq_chip *chip;
734 unsigned long flags;
735 unsigned int irq;
737 for_each_active_irq(irq) {
738 desc = irq_to_desc(irq);
739 if (!desc)
740 continue;
742 raw_spin_lock_irqsave(&desc->lock, flags);
744 chip = irq_data_get_irq_chip(&desc->irq_data);
745 if (chip && chip->irq_cpu_offline &&
746 (!(chip->flags & IRQCHIP_ONOFFLINE_ENABLED) ||
747 !irqd_irq_disabled(&desc->irq_data)))
748 chip->irq_cpu_offline(&desc->irq_data);
750 raw_spin_unlock_irqrestore(&desc->lock, flags);