xref: /linux/kernel/irq/chip.c (revision 6a3335b43342b42dd6c69b4bbbde15d622cb49ca)
1 /*
2  * linux/kernel/irq/chip.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, for irq-chip
8  * based architectures.
9  *
10  * Detailed information is available in Documentation/DocBook/genericirq
11  */
12 
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>
18 
19 #include "internals.h"
20 
21 /**
22  *	dynamic_irq_init - initialize a dynamically allocated irq
23  *	@irq:	irq number to initialize
24  */
25 void dynamic_irq_init(unsigned int irq)
26 {
27 	struct irq_desc *desc;
28 	unsigned long flags;
29 
30 	desc = irq_to_desc(irq);
31 	if (!desc) {
32 		WARN(1, KERN_ERR "Trying to initialize invalid IRQ%d\n", irq);
33 		return;
34 	}
35 
36 	/* Ensure we don't have left over values from a previous use of this irq */
37 	spin_lock_irqsave(&desc->lock, flags);
38 	desc->status = IRQ_DISABLED;
39 	desc->chip = &no_irq_chip;
40 	desc->handle_irq = handle_bad_irq;
41 	desc->depth = 1;
42 	desc->msi_desc = NULL;
43 	desc->handler_data = NULL;
44 	desc->chip_data = NULL;
45 	desc->action = NULL;
46 	desc->irq_count = 0;
47 	desc->irqs_unhandled = 0;
48 #ifdef CONFIG_SMP
49 	cpumask_setall(&desc->affinity);
50 #endif
51 	spin_unlock_irqrestore(&desc->lock, flags);
52 }
53 
54 /**
55  *	dynamic_irq_cleanup - cleanup a dynamically allocated irq
56  *	@irq:	irq number to initialize
57  */
58 void dynamic_irq_cleanup(unsigned int irq)
59 {
60 	struct irq_desc *desc = irq_to_desc(irq);
61 	unsigned long flags;
62 
63 	if (!desc) {
64 		WARN(1, KERN_ERR "Trying to cleanup invalid IRQ%d\n", irq);
65 		return;
66 	}
67 
68 	spin_lock_irqsave(&desc->lock, flags);
69 	if (desc->action) {
70 		spin_unlock_irqrestore(&desc->lock, flags);
71 		WARN(1, KERN_ERR "Destroying IRQ%d without calling free_irq\n",
72 			irq);
73 		return;
74 	}
75 	desc->msi_desc = NULL;
76 	desc->handler_data = NULL;
77 	desc->chip_data = NULL;
78 	desc->handle_irq = handle_bad_irq;
79 	desc->chip = &no_irq_chip;
80 	desc->name = NULL;
81 	clear_kstat_irqs(desc);
82 	spin_unlock_irqrestore(&desc->lock, flags);
83 }
84 
85 
86 /**
87  *	set_irq_chip - set the irq chip for an irq
88  *	@irq:	irq number
89  *	@chip:	pointer to irq chip description structure
90  */
91 int set_irq_chip(unsigned int irq, struct irq_chip *chip)
92 {
93 	struct irq_desc *desc = irq_to_desc(irq);
94 	unsigned long flags;
95 
96 	if (!desc) {
97 		WARN(1, KERN_ERR "Trying to install chip for IRQ%d\n", irq);
98 		return -EINVAL;
99 	}
100 
101 	if (!chip)
102 		chip = &no_irq_chip;
103 
104 	spin_lock_irqsave(&desc->lock, flags);
105 	irq_chip_set_defaults(chip);
106 	desc->chip = chip;
107 	spin_unlock_irqrestore(&desc->lock, flags);
108 
109 	return 0;
110 }
111 EXPORT_SYMBOL(set_irq_chip);
112 
113 /**
114  *	set_irq_type - set the irq trigger type for an irq
115  *	@irq:	irq number
116  *	@type:	IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
117  */
118 int set_irq_type(unsigned int irq, unsigned int type)
119 {
120 	struct irq_desc *desc = irq_to_desc(irq);
121 	unsigned long flags;
122 	int ret = -ENXIO;
123 
124 	if (!desc) {
125 		printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
126 		return -ENODEV;
127 	}
128 
129 	type &= IRQ_TYPE_SENSE_MASK;
130 	if (type == IRQ_TYPE_NONE)
131 		return 0;
132 
133 	spin_lock_irqsave(&desc->lock, flags);
134 	ret = __irq_set_trigger(desc, irq, type);
135 	spin_unlock_irqrestore(&desc->lock, flags);
136 	return ret;
137 }
138 EXPORT_SYMBOL(set_irq_type);
139 
140 /**
141  *	set_irq_data - set irq type data for an irq
142  *	@irq:	Interrupt number
143  *	@data:	Pointer to interrupt specific data
144  *
145  *	Set the hardware irq controller data for an irq
146  */
147 int set_irq_data(unsigned int irq, void *data)
148 {
149 	struct irq_desc *desc = irq_to_desc(irq);
150 	unsigned long flags;
151 
152 	if (!desc) {
153 		printk(KERN_ERR
154 		       "Trying to install controller data for IRQ%d\n", irq);
155 		return -EINVAL;
156 	}
157 
158 	spin_lock_irqsave(&desc->lock, flags);
159 	desc->handler_data = data;
160 	spin_unlock_irqrestore(&desc->lock, flags);
161 	return 0;
162 }
163 EXPORT_SYMBOL(set_irq_data);
164 
165 /**
166  *	set_irq_data - set irq type data for an irq
167  *	@irq:	Interrupt number
168  *	@entry:	Pointer to MSI descriptor data
169  *
170  *	Set the hardware irq controller data for an irq
171  */
172 int set_irq_msi(unsigned int irq, struct msi_desc *entry)
173 {
174 	struct irq_desc *desc = irq_to_desc(irq);
175 	unsigned long flags;
176 
177 	if (!desc) {
178 		printk(KERN_ERR
179 		       "Trying to install msi data for IRQ%d\n", irq);
180 		return -EINVAL;
181 	}
182 
183 	spin_lock_irqsave(&desc->lock, flags);
184 	desc->msi_desc = entry;
185 	if (entry)
186 		entry->irq = irq;
187 	spin_unlock_irqrestore(&desc->lock, flags);
188 	return 0;
189 }
190 
191 /**
192  *	set_irq_chip_data - set irq chip data for an irq
193  *	@irq:	Interrupt number
194  *	@data:	Pointer to chip specific data
195  *
196  *	Set the hardware irq chip data for an irq
197  */
198 int set_irq_chip_data(unsigned int irq, void *data)
199 {
200 	struct irq_desc *desc = irq_to_desc(irq);
201 	unsigned long flags;
202 
203 	if (!desc) {
204 		printk(KERN_ERR
205 		       "Trying to install chip data for IRQ%d\n", irq);
206 		return -EINVAL;
207 	}
208 
209 	if (!desc->chip) {
210 		printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
211 		return -EINVAL;
212 	}
213 
214 	spin_lock_irqsave(&desc->lock, flags);
215 	desc->chip_data = data;
216 	spin_unlock_irqrestore(&desc->lock, flags);
217 
218 	return 0;
219 }
220 EXPORT_SYMBOL(set_irq_chip_data);
221 
222 /*
223  * default enable function
224  */
225 static void default_enable(unsigned int irq)
226 {
227 	struct irq_desc *desc = irq_to_desc(irq);
228 
229 	desc->chip->unmask(irq);
230 	desc->status &= ~IRQ_MASKED;
231 }
232 
233 /*
234  * default disable function
235  */
236 static void default_disable(unsigned int irq)
237 {
238 }
239 
240 /*
241  * default startup function
242  */
243 static unsigned int default_startup(unsigned int irq)
244 {
245 	struct irq_desc *desc = irq_to_desc(irq);
246 
247 	desc->chip->enable(irq);
248 	return 0;
249 }
250 
251 /*
252  * default shutdown function
253  */
254 static void default_shutdown(unsigned int irq)
255 {
256 	struct irq_desc *desc = irq_to_desc(irq);
257 
258 	desc->chip->mask(irq);
259 	desc->status |= IRQ_MASKED;
260 }
261 
262 /*
263  * Fixup enable/disable function pointers
264  */
265 void irq_chip_set_defaults(struct irq_chip *chip)
266 {
267 	if (!chip->enable)
268 		chip->enable = default_enable;
269 	if (!chip->disable)
270 		chip->disable = default_disable;
271 	if (!chip->startup)
272 		chip->startup = default_startup;
273 	/*
274 	 * We use chip->disable, when the user provided its own. When
275 	 * we have default_disable set for chip->disable, then we need
276 	 * to use default_shutdown, otherwise the irq line is not
277 	 * disabled on free_irq():
278 	 */
279 	if (!chip->shutdown)
280 		chip->shutdown = chip->disable != default_disable ?
281 			chip->disable : default_shutdown;
282 	if (!chip->name)
283 		chip->name = chip->typename;
284 	if (!chip->end)
285 		chip->end = dummy_irq_chip.end;
286 }
287 
288 static inline void mask_ack_irq(struct irq_desc *desc, int irq)
289 {
290 	if (desc->chip->mask_ack)
291 		desc->chip->mask_ack(irq);
292 	else {
293 		desc->chip->mask(irq);
294 		if (desc->chip->ack)
295 			desc->chip->ack(irq);
296 	}
297 }
298 
299 /**
300  *	handle_simple_irq - Simple and software-decoded IRQs.
301  *	@irq:	the interrupt number
302  *	@desc:	the interrupt description structure for this irq
303  *
304  *	Simple interrupts are either sent from a demultiplexing interrupt
305  *	handler or come from hardware, where no interrupt hardware control
306  *	is necessary.
307  *
308  *	Note: The caller is expected to handle the ack, clear, mask and
309  *	unmask issues if necessary.
310  */
311 void
312 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
313 {
314 	struct irqaction *action;
315 	irqreturn_t action_ret;
316 
317 	spin_lock(&desc->lock);
318 
319 	if (unlikely(desc->status & IRQ_INPROGRESS))
320 		goto out_unlock;
321 	desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
322 	kstat_incr_irqs_this_cpu(irq, desc);
323 
324 	action = desc->action;
325 	if (unlikely(!action || (desc->status & IRQ_DISABLED)))
326 		goto out_unlock;
327 
328 	desc->status |= IRQ_INPROGRESS;
329 	spin_unlock(&desc->lock);
330 
331 	action_ret = handle_IRQ_event(irq, action);
332 	if (!noirqdebug)
333 		note_interrupt(irq, desc, action_ret);
334 
335 	spin_lock(&desc->lock);
336 	desc->status &= ~IRQ_INPROGRESS;
337 out_unlock:
338 	spin_unlock(&desc->lock);
339 }
340 
341 /**
342  *	handle_level_irq - Level type irq handler
343  *	@irq:	the interrupt number
344  *	@desc:	the interrupt description structure for this irq
345  *
346  *	Level type interrupts are active as long as the hardware line has
347  *	the active level. This may require to mask the interrupt and unmask
348  *	it after the associated handler has acknowledged the device, so the
349  *	interrupt line is back to inactive.
350  */
351 void
352 handle_level_irq(unsigned int irq, struct irq_desc *desc)
353 {
354 	struct irqaction *action;
355 	irqreturn_t action_ret;
356 
357 	spin_lock(&desc->lock);
358 	mask_ack_irq(desc, irq);
359 	desc = irq_remap_to_desc(irq, desc);
360 
361 	if (unlikely(desc->status & IRQ_INPROGRESS))
362 		goto out_unlock;
363 	desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
364 	kstat_incr_irqs_this_cpu(irq, desc);
365 
366 	/*
367 	 * If its disabled or no action available
368 	 * keep it masked and get out of here
369 	 */
370 	action = desc->action;
371 	if (unlikely(!action || (desc->status & IRQ_DISABLED)))
372 		goto out_unlock;
373 
374 	desc->status |= IRQ_INPROGRESS;
375 	spin_unlock(&desc->lock);
376 
377 	action_ret = handle_IRQ_event(irq, action);
378 	if (!noirqdebug)
379 		note_interrupt(irq, desc, action_ret);
380 
381 	spin_lock(&desc->lock);
382 	desc->status &= ~IRQ_INPROGRESS;
383 	if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
384 		desc->chip->unmask(irq);
385 out_unlock:
386 	spin_unlock(&desc->lock);
387 }
388 EXPORT_SYMBOL_GPL(handle_level_irq);
389 
390 /**
391  *	handle_fasteoi_irq - irq handler for transparent controllers
392  *	@irq:	the interrupt number
393  *	@desc:	the interrupt description structure for this irq
394  *
395  *	Only a single callback will be issued to the chip: an ->eoi()
396  *	call when the interrupt has been serviced. This enables support
397  *	for modern forms of interrupt handlers, which handle the flow
398  *	details in hardware, transparently.
399  */
400 void
401 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
402 {
403 	struct irqaction *action;
404 	irqreturn_t action_ret;
405 
406 	spin_lock(&desc->lock);
407 
408 	if (unlikely(desc->status & IRQ_INPROGRESS))
409 		goto out;
410 
411 	desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
412 	kstat_incr_irqs_this_cpu(irq, desc);
413 
414 	/*
415 	 * If its disabled or no action available
416 	 * then mask it and get out of here:
417 	 */
418 	action = desc->action;
419 	if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
420 		desc->status |= IRQ_PENDING;
421 		if (desc->chip->mask)
422 			desc->chip->mask(irq);
423 		goto out;
424 	}
425 
426 	desc->status |= IRQ_INPROGRESS;
427 	desc->status &= ~IRQ_PENDING;
428 	spin_unlock(&desc->lock);
429 
430 	action_ret = handle_IRQ_event(irq, action);
431 	if (!noirqdebug)
432 		note_interrupt(irq, desc, action_ret);
433 
434 	spin_lock(&desc->lock);
435 	desc->status &= ~IRQ_INPROGRESS;
436 out:
437 	desc->chip->eoi(irq);
438 	desc = irq_remap_to_desc(irq, desc);
439 
440 	spin_unlock(&desc->lock);
441 }
442 
443 /**
444  *	handle_edge_irq - edge type IRQ handler
445  *	@irq:	the interrupt number
446  *	@desc:	the interrupt description structure for this irq
447  *
448  *	Interrupt occures on the falling and/or rising edge of a hardware
449  *	signal. The occurence is latched into the irq controller hardware
450  *	and must be acked in order to be reenabled. After the ack another
451  *	interrupt can happen on the same source even before the first one
452  *	is handled by the assosiacted event handler. If this happens it
453  *	might be necessary to disable (mask) the interrupt depending on the
454  *	controller hardware. This requires to reenable the interrupt inside
455  *	of the loop which handles the interrupts which have arrived while
456  *	the handler was running. If all pending interrupts are handled, the
457  *	loop is left.
458  */
459 void
460 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
461 {
462 	spin_lock(&desc->lock);
463 
464 	desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
465 
466 	/*
467 	 * If we're currently running this IRQ, or its disabled,
468 	 * we shouldn't process the IRQ. Mark it pending, handle
469 	 * the necessary masking and go out
470 	 */
471 	if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
472 		    !desc->action)) {
473 		desc->status |= (IRQ_PENDING | IRQ_MASKED);
474 		mask_ack_irq(desc, irq);
475 		desc = irq_remap_to_desc(irq, desc);
476 		goto out_unlock;
477 	}
478 	kstat_incr_irqs_this_cpu(irq, desc);
479 
480 	/* Start handling the irq */
481 	if (desc->chip->ack)
482 		desc->chip->ack(irq);
483 	desc = irq_remap_to_desc(irq, desc);
484 
485 	/* Mark the IRQ currently in progress.*/
486 	desc->status |= IRQ_INPROGRESS;
487 
488 	do {
489 		struct irqaction *action = desc->action;
490 		irqreturn_t action_ret;
491 
492 		if (unlikely(!action)) {
493 			desc->chip->mask(irq);
494 			goto out_unlock;
495 		}
496 
497 		/*
498 		 * When another irq arrived while we were handling
499 		 * one, we could have masked the irq.
500 		 * Renable it, if it was not disabled in meantime.
501 		 */
502 		if (unlikely((desc->status &
503 			       (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
504 			      (IRQ_PENDING | IRQ_MASKED))) {
505 			desc->chip->unmask(irq);
506 			desc->status &= ~IRQ_MASKED;
507 		}
508 
509 		desc->status &= ~IRQ_PENDING;
510 		spin_unlock(&desc->lock);
511 		action_ret = handle_IRQ_event(irq, action);
512 		if (!noirqdebug)
513 			note_interrupt(irq, desc, action_ret);
514 		spin_lock(&desc->lock);
515 
516 	} while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
517 
518 	desc->status &= ~IRQ_INPROGRESS;
519 out_unlock:
520 	spin_unlock(&desc->lock);
521 }
522 
523 /**
524  *	handle_percpu_IRQ - Per CPU local irq handler
525  *	@irq:	the interrupt number
526  *	@desc:	the interrupt description structure for this irq
527  *
528  *	Per CPU interrupts on SMP machines without locking requirements
529  */
530 void
531 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
532 {
533 	irqreturn_t action_ret;
534 
535 	kstat_incr_irqs_this_cpu(irq, desc);
536 
537 	if (desc->chip->ack)
538 		desc->chip->ack(irq);
539 
540 	action_ret = handle_IRQ_event(irq, desc->action);
541 	if (!noirqdebug)
542 		note_interrupt(irq, desc, action_ret);
543 
544 	if (desc->chip->eoi) {
545 		desc->chip->eoi(irq);
546 		desc = irq_remap_to_desc(irq, desc);
547 	}
548 }
549 
550 void
551 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
552 		  const char *name)
553 {
554 	struct irq_desc *desc = irq_to_desc(irq);
555 	unsigned long flags;
556 
557 	if (!desc) {
558 		printk(KERN_ERR
559 		       "Trying to install type control for IRQ%d\n", irq);
560 		return;
561 	}
562 
563 	if (!handle)
564 		handle = handle_bad_irq;
565 	else if (desc->chip == &no_irq_chip) {
566 		printk(KERN_WARNING "Trying to install %sinterrupt handler "
567 		       "for IRQ%d\n", is_chained ? "chained " : "", irq);
568 		/*
569 		 * Some ARM implementations install a handler for really dumb
570 		 * interrupt hardware without setting an irq_chip. This worked
571 		 * with the ARM no_irq_chip but the check in setup_irq would
572 		 * prevent us to setup the interrupt at all. Switch it to
573 		 * dummy_irq_chip for easy transition.
574 		 */
575 		desc->chip = &dummy_irq_chip;
576 	}
577 
578 	spin_lock_irqsave(&desc->lock, flags);
579 
580 	/* Uninstall? */
581 	if (handle == handle_bad_irq) {
582 		if (desc->chip != &no_irq_chip) {
583 			mask_ack_irq(desc, irq);
584 			desc = irq_remap_to_desc(irq, desc);
585 		}
586 		desc->status |= IRQ_DISABLED;
587 		desc->depth = 1;
588 	}
589 	desc->handle_irq = handle;
590 	desc->name = name;
591 
592 	if (handle != handle_bad_irq && is_chained) {
593 		desc->status &= ~IRQ_DISABLED;
594 		desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
595 		desc->depth = 0;
596 		desc->chip->startup(irq);
597 	}
598 	spin_unlock_irqrestore(&desc->lock, flags);
599 }
600 EXPORT_SYMBOL_GPL(__set_irq_handler);
601 
602 void
603 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
604 			 irq_flow_handler_t handle)
605 {
606 	set_irq_chip(irq, chip);
607 	__set_irq_handler(irq, handle, 0, NULL);
608 }
609 
610 void
611 set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
612 			      irq_flow_handler_t handle, const char *name)
613 {
614 	set_irq_chip(irq, chip);
615 	__set_irq_handler(irq, handle, 0, name);
616 }
617 
618 void __init set_irq_noprobe(unsigned int irq)
619 {
620 	struct irq_desc *desc = irq_to_desc(irq);
621 	unsigned long flags;
622 
623 	if (!desc) {
624 		printk(KERN_ERR "Trying to mark IRQ%d non-probeable\n", irq);
625 		return;
626 	}
627 
628 	spin_lock_irqsave(&desc->lock, flags);
629 	desc->status |= IRQ_NOPROBE;
630 	spin_unlock_irqrestore(&desc->lock, flags);
631 }
632 
633 void __init set_irq_probe(unsigned int irq)
634 {
635 	struct irq_desc *desc = irq_to_desc(irq);
636 	unsigned long flags;
637 
638 	if (!desc) {
639 		printk(KERN_ERR "Trying to mark IRQ%d probeable\n", irq);
640 		return;
641 	}
642 
643 	spin_lock_irqsave(&desc->lock, flags);
644 	desc->status &= ~IRQ_NOPROBE;
645 	spin_unlock_irqrestore(&desc->lock, flags);
646 }
647