xref: /openbmc/linux/kernel/irq/handle.c (revision cb5bc83225a86ca53bbb889ed8439e4fd6cf44ac)
1 /*
2  * linux/kernel/irq/handle.c
3  *
4  * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
5  * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
6  *
7  * This file contains the core interrupt handling code.
8  *
9  * Detailed information is available in Documentation/DocBook/genericirq
10  *
11  */
12 
13 #include <linux/irq.h>
14 #include <linux/module.h>
15 #include <linux/random.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel_stat.h>
18 
19 #include "internals.h"
20 
21 #ifdef CONFIG_TRACE_IRQFLAGS
22 
23 /*
24  * lockdep: we want to handle all irq_desc locks as a single lock-class:
25  */
26 static struct lock_class_key irq_desc_lock_class;
27 #endif
28 
29 /**
30  * handle_bad_irq - handle spurious and unhandled irqs
31  * @irq:       the interrupt number
32  * @desc:      description of the interrupt
33  *
34  * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
35  */
36 void
37 handle_bad_irq(unsigned int irq, struct irq_desc *desc)
38 {
39 	print_irq_desc(irq, desc);
40 	kstat_irqs_this_cpu(desc)++;
41 	ack_bad_irq(irq);
42 }
43 
44 /*
45  * Linux has a controller-independent interrupt architecture.
46  * Every controller has a 'controller-template', that is used
47  * by the main code to do the right thing. Each driver-visible
48  * interrupt source is transparently wired to the appropriate
49  * controller. Thus drivers need not be aware of the
50  * interrupt-controller.
51  *
52  * The code is designed to be easily extended with new/different
53  * interrupt controllers, without having to do assembly magic or
54  * having to touch the generic code.
55  *
56  * Controller mappings for all interrupt sources:
57  */
58 int nr_irqs = NR_IRQS;
59 EXPORT_SYMBOL_GPL(nr_irqs);
60 
61 #ifdef CONFIG_HAVE_DYN_ARRAY
62 static struct irq_desc irq_desc_init = {
63 	.irq = -1U,
64 	.status = IRQ_DISABLED,
65 	.chip = &no_irq_chip,
66 	.handle_irq = handle_bad_irq,
67 	.depth = 1,
68 	.lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
69 #ifdef CONFIG_SMP
70 	.affinity = CPU_MASK_ALL
71 #endif
72 };
73 
74 
75 static void init_one_irq_desc(struct irq_desc *desc)
76 {
77 	memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
78 #ifdef CONFIG_TRACE_IRQFLAGS
79 	lockdep_set_class(&desc->lock, &irq_desc_lock_class);
80 #endif
81 }
82 
83 extern int after_bootmem;
84 extern void *__alloc_bootmem_nopanic(unsigned long size,
85 			     unsigned long align,
86 			     unsigned long goal);
87 
88 static void init_kstat_irqs(struct irq_desc *desc, int nr_desc, int nr)
89 {
90 	unsigned long bytes, total_bytes;
91 	char *ptr;
92 	int i;
93 	unsigned long phys;
94 
95 	/* Compute how many bytes we need per irq and allocate them */
96 	bytes = nr * sizeof(unsigned int);
97 	total_bytes = bytes * nr_desc;
98 	if (after_bootmem)
99 		ptr = kzalloc(total_bytes, GFP_ATOMIC);
100 	else
101 		ptr = __alloc_bootmem_nopanic(total_bytes, PAGE_SIZE, 0);
102 
103 	if (!ptr)
104 		panic(" can not allocate kstat_irqs\n");
105 
106 	phys = __pa(ptr);
107 	printk(KERN_DEBUG "kstat_irqs ==> [%#lx - %#lx]\n", phys, phys + total_bytes);
108 
109 	for (i = 0; i < nr_desc; i++) {
110 		desc[i].kstat_irqs = (unsigned int *)ptr;
111 		ptr += bytes;
112 	}
113 }
114 
115 static void __init init_work(void *data)
116 {
117 	struct dyn_array *da = data;
118 	int i;
119 	struct  irq_desc *desc;
120 
121 	desc = *da->name;
122 
123 	for (i = 0; i < *da->nr; i++) {
124 		init_one_irq_desc(&desc[i]);
125 #ifndef CONFIG_HAVE_SPARSE_IRQ
126 		desc[i].irq = i;
127 #endif
128 	}
129 
130 #ifdef CONFIG_HAVE_SPARSE_IRQ
131 	for (i = 1; i < *da->nr; i++)
132 		desc[i-1].next = &desc[i];
133 #endif
134 
135 	/* init kstat_irqs, nr_cpu_ids is ready already */
136 	init_kstat_irqs(desc, *da->nr, nr_cpu_ids);
137 }
138 
139 #ifdef CONFIG_HAVE_SPARSE_IRQ
140 static int nr_irq_desc = 32;
141 
142 static int __init parse_nr_irq_desc(char *arg)
143 {
144 	if (arg)
145 		nr_irq_desc = simple_strtoul(arg, NULL, 0);
146 	return 0;
147 }
148 
149 early_param("nr_irq_desc", parse_nr_irq_desc);
150 
151 struct irq_desc *sparse_irqs;
152 DEFINE_DYN_ARRAY(sparse_irqs, sizeof(struct irq_desc), nr_irq_desc, PAGE_SIZE, init_work);
153 
154 struct irq_desc *irq_to_desc(unsigned int irq)
155 {
156 	struct irq_desc *desc;
157 
158 	BUG_ON(irq == -1U);
159 
160 	desc = &sparse_irqs[0];
161 	while (desc) {
162 		if (desc->irq == irq)
163 			return desc;
164 
165 		if (desc->irq == -1U)
166 			return NULL;
167 
168 		desc = desc->next;
169 	}
170 	return NULL;
171 }
172 struct irq_desc *irq_to_desc_alloc(unsigned int irq)
173 {
174 	struct irq_desc *desc, *desc_pri;
175 	int i;
176 	int count = 0;
177 	unsigned long phys;
178 	unsigned long total_bytes;
179 
180 	BUG_ON(irq == -1U);
181 
182 	desc_pri = desc = &sparse_irqs[0];
183 	while (desc) {
184 		if (desc->irq == irq)
185 			return desc;
186 
187 		if (desc->irq == -1U) {
188 			desc->irq = irq;
189 			printk(KERN_DEBUG "found new irq_desc for irq %d\n", desc->irq);
190 			return desc;
191 		}
192 		desc_pri = desc;
193 		desc = desc->next;
194 		count++;
195 	}
196 
197 	/*
198 	 *  we run out of pre-allocate ones, allocate more
199 	 */
200 	printk(KERN_DEBUG "try to get more irq_desc %d\n", nr_irq_desc);
201 	{
202 		/* double check if some one mess up the list */
203 		struct irq_desc *desc;
204 		int count = 0;
205 
206 		desc = &sparse_irqs[0];
207 		while (desc) {
208 			printk(KERN_DEBUG "found irq_desc for irq %d\n", desc->irq);
209 			if (desc->next)
210 				printk(KERN_DEBUG "found irq_desc for irq %d and next will be irq %d\n", desc->irq, desc->next->irq);
211 			desc = desc->next;
212 			count++;
213 		}
214 		printk(KERN_DEBUG "all preallocted %d\n", count);
215 	}
216 
217 	total_bytes = sizeof(struct irq_desc) * nr_irq_desc;
218 	if (after_bootmem)
219 		desc = kzalloc(total_bytes, GFP_ATOMIC);
220 	else
221 		desc = __alloc_bootmem_nopanic(total_bytes, PAGE_SIZE, 0);
222 
223 	if (!desc)
224 		panic("please boot with nr_irq_desc= %d\n", count * 2);
225 
226 	phys = __pa(desc);
227 	printk(KERN_DEBUG "irq_desc ==> [%#lx - %#lx]\n", phys, phys + total_bytes);
228 
229 	for (i = 0; i < nr_irq_desc; i++)
230 		init_one_irq_desc(&desc[i]);
231 
232 	for (i = 1; i < nr_irq_desc; i++)
233 		desc[i-1].next = &desc[i];
234 
235 	/* init kstat_irqs, nr_cpu_ids is ready already */
236 	init_kstat_irqs(desc, nr_irq_desc, nr_cpu_ids);
237 
238 	desc->irq = irq;
239 	desc_pri->next = desc;
240 	printk(KERN_DEBUG "1 found new irq_desc for irq %d and pri will be irq %d\n", desc->irq, desc_pri->irq);
241 
242 	return desc;
243 }
244 #else
245 struct irq_desc *irq_desc;
246 DEFINE_DYN_ARRAY(irq_desc, sizeof(struct irq_desc), nr_irqs, PAGE_SIZE, init_work);
247 
248 #endif
249 
250 #else
251 
252 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
253 	[0 ... NR_IRQS-1] = {
254 		.status = IRQ_DISABLED,
255 		.chip = &no_irq_chip,
256 		.handle_irq = handle_bad_irq,
257 		.depth = 1,
258 		.lock = __SPIN_LOCK_UNLOCKED(sparse_irqs->lock),
259 #ifdef CONFIG_SMP
260 		.affinity = CPU_MASK_ALL
261 #endif
262 	}
263 };
264 
265 #endif
266 
267 #ifndef CONFIG_HAVE_SPARSE_IRQ
268 struct irq_desc *irq_to_desc(unsigned int irq)
269 {
270 	if (irq < nr_irqs)
271 		return &irq_desc[irq];
272 
273 	return NULL;
274 }
275 struct irq_desc *irq_to_desc_alloc(unsigned int irq)
276 {
277 	return irq_to_desc(irq);
278 }
279 #endif
280 
281 /*
282  * What should we do if we get a hw irq event on an illegal vector?
283  * Each architecture has to answer this themself.
284  */
285 static void ack_bad(unsigned int irq)
286 {
287 	struct irq_desc *desc;
288 
289 	desc = irq_to_desc(irq);
290 	print_irq_desc(irq, desc);
291 	ack_bad_irq(irq);
292 }
293 
294 /*
295  * NOP functions
296  */
297 static void noop(unsigned int irq)
298 {
299 }
300 
301 static unsigned int noop_ret(unsigned int irq)
302 {
303 	return 0;
304 }
305 
306 /*
307  * Generic no controller implementation
308  */
309 struct irq_chip no_irq_chip = {
310 	.name		= "none",
311 	.startup	= noop_ret,
312 	.shutdown	= noop,
313 	.enable		= noop,
314 	.disable	= noop,
315 	.ack		= ack_bad,
316 	.end		= noop,
317 };
318 
319 /*
320  * Generic dummy implementation which can be used for
321  * real dumb interrupt sources
322  */
323 struct irq_chip dummy_irq_chip = {
324 	.name		= "dummy",
325 	.startup	= noop_ret,
326 	.shutdown	= noop,
327 	.enable		= noop,
328 	.disable	= noop,
329 	.ack		= noop,
330 	.mask		= noop,
331 	.unmask		= noop,
332 	.end		= noop,
333 };
334 
335 /*
336  * Special, empty irq handler:
337  */
338 irqreturn_t no_action(int cpl, void *dev_id)
339 {
340 	return IRQ_NONE;
341 }
342 
343 /**
344  * handle_IRQ_event - irq action chain handler
345  * @irq:	the interrupt number
346  * @action:	the interrupt action chain for this irq
347  *
348  * Handles the action chain of an irq event
349  */
350 irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
351 {
352 	irqreturn_t ret, retval = IRQ_NONE;
353 	unsigned int status = 0;
354 
355 	if (!(action->flags & IRQF_DISABLED))
356 		local_irq_enable_in_hardirq();
357 
358 	do {
359 		ret = action->handler(irq, action->dev_id);
360 		if (ret == IRQ_HANDLED)
361 			status |= action->flags;
362 		retval |= ret;
363 		action = action->next;
364 	} while (action);
365 
366 	if (status & IRQF_SAMPLE_RANDOM)
367 		add_interrupt_randomness(irq);
368 	local_irq_disable();
369 
370 	return retval;
371 }
372 
373 #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
374 /**
375  * __do_IRQ - original all in one highlevel IRQ handler
376  * @irq:	the interrupt number
377  *
378  * __do_IRQ handles all normal device IRQ's (the special
379  * SMP cross-CPU interrupts have their own specific
380  * handlers).
381  *
382  * This is the original x86 implementation which is used for every
383  * interrupt type.
384  */
385 unsigned int __do_IRQ(unsigned int irq)
386 {
387 	struct irq_desc *desc = irq_to_desc(irq);
388 	struct irqaction *action;
389 	unsigned int status;
390 
391 	kstat_irqs_this_cpu(desc)++;
392 	if (CHECK_IRQ_PER_CPU(desc->status)) {
393 		irqreturn_t action_ret;
394 
395 		/*
396 		 * No locking required for CPU-local interrupts:
397 		 */
398 		if (desc->chip->ack)
399 			desc->chip->ack(irq);
400 		if (likely(!(desc->status & IRQ_DISABLED))) {
401 			action_ret = handle_IRQ_event(irq, desc->action);
402 			if (!noirqdebug)
403 				note_interrupt(irq, desc, action_ret);
404 		}
405 		desc->chip->end(irq);
406 		return 1;
407 	}
408 
409 	spin_lock(&desc->lock);
410 	if (desc->chip->ack)
411 		desc->chip->ack(irq);
412 	/*
413 	 * REPLAY is when Linux resends an IRQ that was dropped earlier
414 	 * WAITING is used by probe to mark irqs that are being tested
415 	 */
416 	status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
417 	status |= IRQ_PENDING; /* we _want_ to handle it */
418 
419 	/*
420 	 * If the IRQ is disabled for whatever reason, we cannot
421 	 * use the action we have.
422 	 */
423 	action = NULL;
424 	if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
425 		action = desc->action;
426 		status &= ~IRQ_PENDING; /* we commit to handling */
427 		status |= IRQ_INPROGRESS; /* we are handling it */
428 	}
429 	desc->status = status;
430 
431 	/*
432 	 * If there is no IRQ handler or it was disabled, exit early.
433 	 * Since we set PENDING, if another processor is handling
434 	 * a different instance of this same irq, the other processor
435 	 * will take care of it.
436 	 */
437 	if (unlikely(!action))
438 		goto out;
439 
440 	/*
441 	 * Edge triggered interrupts need to remember
442 	 * pending events.
443 	 * This applies to any hw interrupts that allow a second
444 	 * instance of the same irq to arrive while we are in do_IRQ
445 	 * or in the handler. But the code here only handles the _second_
446 	 * instance of the irq, not the third or fourth. So it is mostly
447 	 * useful for irq hardware that does not mask cleanly in an
448 	 * SMP environment.
449 	 */
450 	for (;;) {
451 		irqreturn_t action_ret;
452 
453 		spin_unlock(&desc->lock);
454 
455 		action_ret = handle_IRQ_event(irq, action);
456 		if (!noirqdebug)
457 			note_interrupt(irq, desc, action_ret);
458 
459 		spin_lock(&desc->lock);
460 		if (likely(!(desc->status & IRQ_PENDING)))
461 			break;
462 		desc->status &= ~IRQ_PENDING;
463 	}
464 	desc->status &= ~IRQ_INPROGRESS;
465 
466 out:
467 	/*
468 	 * The ->end() handler has to deal with interrupts which got
469 	 * disabled while the handler was running.
470 	 */
471 	desc->chip->end(irq);
472 	spin_unlock(&desc->lock);
473 
474 	return 1;
475 }
476 #endif
477 
478 
479 #ifdef CONFIG_TRACE_IRQFLAGS
480 void early_init_irq_lock_class(void)
481 {
482 #ifndef CONFIG_HAVE_DYN_ARRAY
483 	int i;
484 
485 	for (i = 0; i < nr_irqs; i++)
486 		lockdep_set_class(&irq_desc[i].lock, &irq_desc_lock_class);
487 #endif
488 }
489 #endif
490 
491 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
492 {
493 	struct irq_desc *desc = irq_to_desc(irq);
494 	return desc->kstat_irqs[cpu];
495 }
496 EXPORT_SYMBOL(kstat_irqs_cpu);
497 
498