handle.c 5.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * linux/kernel/irq/handle.c
 *
I
Ingo Molnar 已提交
4 5
 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
 * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
L
Linus Torvalds 已提交
6 7
 *
 * This file contains the core interrupt handling code.
I
Ingo Molnar 已提交
8 9 10
 *
 * Detailed information is available in Documentation/DocBook/genericirq
 *
L
Linus Torvalds 已提交
11 12 13 14
 */

#include <linux/irq.h>
#include <linux/random.h>
15
#include <linux/sched.h>
L
Linus Torvalds 已提交
16 17
#include <linux/interrupt.h>
#include <linux/kernel_stat.h>
18

19
#include <trace/events/irq.h>
L
Linus Torvalds 已提交
20 21 22

#include "internals.h"

T
Thomas Gleixner 已提交
23 24
/**
 * handle_bad_irq - handle spurious and unhandled irqs
25 26 27 28
 * @irq:       the interrupt number
 * @desc:      description of the interrupt
 *
 * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
T
Thomas Gleixner 已提交
29
 */
T
Thomas Gleixner 已提交
30
void handle_bad_irq(unsigned int irq, struct irq_desc *desc)
T
Thomas Gleixner 已提交
31
{
32
	print_irq_desc(irq, desc);
T
Thomas Gleixner 已提交
33
	kstat_incr_irqs_this_cpu(irq, desc);
T
Thomas Gleixner 已提交
34 35 36
	ack_bad_irq(irq);
}

L
Linus Torvalds 已提交
37 38 39
/*
 * Special, empty irq handler:
 */
40
irqreturn_t no_action(int cpl, void *dev_id)
L
Linus Torvalds 已提交
41 42 43 44
{
	return IRQ_NONE;
}

45 46 47 48 49 50 51 52 53
static void warn_no_thread(unsigned int irq, struct irqaction *action)
{
	if (test_and_set_bit(IRQTF_WARNED, &action->thread_flags))
		return;

	printk(KERN_WARNING "IRQ %d device %s returned IRQ_WAKE_THREAD "
	       "but no thread function available.", irq, action->name);
}

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
static void irq_wake_thread(struct irq_desc *desc, struct irqaction *action)
{
	/*
	 * Wake up the handler thread for this action. In case the
	 * thread crashed and was killed we just pretend that we
	 * handled the interrupt. The hardirq handler has disabled the
	 * device interrupt, so no irq storm is lurking. If the
	 * RUNTHREAD bit is already set, nothing to do.
	 */
	if (test_bit(IRQTF_DIED, &action->thread_flags) ||
	    test_and_set_bit(IRQTF_RUNTHREAD, &action->thread_flags))
		return;

	/*
	 * It's safe to OR the mask lockless here. We have only two
	 * places which write to threads_oneshot: This code and the
	 * irq thread.
	 *
	 * This code is the hard irq context and can never run on two
	 * cpus in parallel. If it ever does we have more serious
	 * problems than this bitmask.
	 *
	 * The irq threads of this irq which clear their "running" bit
	 * in threads_oneshot are serialized via desc->lock against
	 * each other and they are serialized against this code by
	 * IRQS_INPROGRESS.
	 *
	 * Hard irq handler:
	 *
	 *	spin_lock(desc->lock);
	 *	desc->state |= IRQS_INPROGRESS;
	 *	spin_unlock(desc->lock);
	 *	set_bit(IRQTF_RUNTHREAD, &action->thread_flags);
	 *	desc->threads_oneshot |= mask;
	 *	spin_lock(desc->lock);
	 *	desc->state &= ~IRQS_INPROGRESS;
	 *	spin_unlock(desc->lock);
	 *
	 * irq thread:
	 *
	 * again:
	 *	spin_lock(desc->lock);
	 *	if (desc->state & IRQS_INPROGRESS) {
	 *		spin_unlock(desc->lock);
	 *		while(desc->state & IRQS_INPROGRESS)
	 *			cpu_relax();
	 *		goto again;
	 *	}
	 *	if (!test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
	 *		desc->threads_oneshot &= ~mask;
	 *	spin_unlock(desc->lock);
	 *
	 * So either the thread waits for us to clear IRQS_INPROGRESS
	 * or we are waiting in the flow handler for desc->lock to be
	 * released before we reach this point. The thread also checks
	 * IRQTF_RUNTHREAD under desc->lock. If set it leaves
	 * threads_oneshot untouched and runs the thread another time.
	 */
	desc->threads_oneshot |= action->thread_mask;
	wake_up_process(action->thread);
}

116 117
irqreturn_t
handle_irq_event_percpu(struct irq_desc *desc, struct irqaction *action)
L
Linus Torvalds 已提交
118
{
119
	irqreturn_t retval = IRQ_NONE;
120
	unsigned int random = 0, irq = desc->irq_data.irq;
L
Linus Torvalds 已提交
121 122

	do {
123 124
		irqreturn_t res;

125
		trace_irq_handler_entry(irq, action);
126 127
		res = action->handler(irq, action->dev_id);
		trace_irq_handler_exit(irq, action, res);
128

129 130
		if (WARN_ONCE(!irqs_disabled(),"irq %u handler %pF enabled interrupts\n",
			      irq, action->handler))
131 132
			local_irq_disable();

133
		switch (res) {
134
		case IRQ_WAKE_THREAD:
135 136 137 138
			/*
			 * Set result to handled so the spurious check
			 * does not trigger.
			 */
139
			res = IRQ_HANDLED;
140 141 142 143 144 145 146 147 148 149

			/*
			 * Catch drivers which return WAKE_THREAD but
			 * did not set up a thread function
			 */
			if (unlikely(!action->thread_fn)) {
				warn_no_thread(irq, action);
				break;
			}

150
			irq_wake_thread(desc, action);
151 152 153

			/* Fall through to add to randomness */
		case IRQ_HANDLED:
154
			random |= action->flags;
155 156 157 158 159 160
			break;

		default:
			break;
		}

161
		retval |= res;
L
Linus Torvalds 已提交
162 163 164
		action = action->next;
	} while (action);

165
	if (random & IRQF_SAMPLE_RANDOM)
L
Linus Torvalds 已提交
166 167
		add_interrupt_randomness(irq);

168
	if (!noirqdebug)
169
		note_interrupt(irq, desc, retval);
170
	return retval;
171 172 173 174 175 176 177
}

irqreturn_t handle_irq_event(struct irq_desc *desc)
{
	struct irqaction *action = desc->action;
	irqreturn_t ret;

178 179
	irq_compat_clr_pending(desc);
	desc->istate &= ~IRQS_PENDING;
180
	irq_compat_set_progress(desc);
181
	irqd_set(&desc->irq_data, IRQD_IRQ_INPROGRESS);
182 183 184 185 186
	raw_spin_unlock(&desc->lock);

	ret = handle_irq_event_percpu(desc, action);

	raw_spin_lock(&desc->lock);
187
	irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
188
	irq_compat_clr_progress(desc);
189 190 191 192 193 194 195 196 197 198 199 200
	return ret;
}

/**
 * handle_IRQ_event - irq action chain handler
 * @irq:	the interrupt number
 * @action:	the interrupt action chain for this irq
 *
 * Handles the action chain of an irq event
 */
irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
{
201
	return handle_irq_event_percpu(irq_to_desc(irq), action);
202
}