1/*
2 *  linux/arch/arm/kernel/irq.c
3 *
4 *  Copyright (C) 1992 Linus Torvalds
5 *  Modifications for ARM processor Copyright (C) 1995-2000 Russell King.
6 *  'Borrowed' for ARM26 and (C) 2003 Ian Molton.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 *  This file contains the code used by various IRQ handling routines:
13 *  asking for different IRQ's should be done through these routines
14 *  instead of just grabbing them. Thus setups with different IRQ numbers
15 *  shouldn't result in any weird surprises, and installing new handlers
16 *  should be easier.
17 *
18 *  IRQ's are in fact implemented a bit like signal handlers for the kernel.
19 *  Naturally it's not a 1:1 relation, but there are similarities.
20 */
21#include <linux/module.h>
22#include <linux/ptrace.h>
23#include <linux/kernel_stat.h>
24#include <linux/signal.h>
25#include <linux/sched.h>
26#include <linux/ioport.h>
27#include <linux/interrupt.h>
28#include <linux/slab.h>
29#include <linux/random.h>
30#include <linux/smp.h>
31#include <linux/init.h>
32#include <linux/seq_file.h>
33#include <linux/errno.h>
34
35#include <asm/irq.h>
36#include <asm/system.h>
37#include <asm/irqchip.h>
38
39void __init arc_init_irq(void);
40
41#define MAX_IRQ_CNT	100000
42
43static volatile unsigned long irq_err_count;
44static DEFINE_SPINLOCK(irq_controller_lock);
45
46struct irqdesc irq_desc[NR_IRQS];
47
48/*
49 * Dummy mask/unmask handler
50 */
51void dummy_mask_unmask_irq(unsigned int irq)
52{
53}
54
55void do_bad_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
56{
57	irq_err_count += 1;
58	printk(KERN_ERR "IRQ: spurious interrupt %d\n", irq);
59}
60
61static struct irqchip bad_chip = {
62	.ack	= dummy_mask_unmask_irq,
63	.mask	= dummy_mask_unmask_irq,
64	.unmask = dummy_mask_unmask_irq,
65};
66
67static struct irqdesc bad_irq_desc = {
68	.chip	= &bad_chip,
69	.handle = do_bad_IRQ,
70	.depth	= 1,
71};
72
73/**
74 *	disable_irq - disable an irq and wait for completion
75 *	@irq: Interrupt to disable
76 *
77 *	Disable the selected interrupt line.  We do this lazily.
78 *
79 *	This function may be called from IRQ context.
80 */
81void disable_irq(unsigned int irq)
82{
83	struct irqdesc *desc = irq_desc + irq;
84	unsigned long flags;
85	spin_lock_irqsave(&irq_controller_lock, flags);
86	if (!desc->depth++)
87		desc->enabled = 0;
88	spin_unlock_irqrestore(&irq_controller_lock, flags);
89}
90
91/**
92 *	enable_irq - enable interrupt handling on an irq
93 *	@irq: Interrupt to enable
94 *
95 *	Re-enables the processing of interrupts on this IRQ line.
96 *	Note that this may call the interrupt handler, so you may
97 *	get unexpected results if you hold IRQs disabled.
98 *
99 *	This function may be called from IRQ context.
100 */
101void enable_irq(unsigned int irq)
102{
103	struct irqdesc *desc = irq_desc + irq;
104	unsigned long flags;
105	int pending = 0;
106
107	spin_lock_irqsave(&irq_controller_lock, flags);
108	if (unlikely(!desc->depth)) {
109		printk("enable_irq(%u) unbalanced from %p\n", irq,
110			__builtin_return_address(0));
111	} else if (!--desc->depth) {
112		desc->probing = 0;
113		desc->enabled = 1;
114		desc->chip->unmask(irq);
115		pending = desc->pending;
116		desc->pending = 0;
117		/*
118		 * If the interrupt was waiting to be processed,
119		 * retrigger it.
120		 */
121		if (pending)
122			desc->chip->rerun(irq);
123	}
124	spin_unlock_irqrestore(&irq_controller_lock, flags);
125}
126
127int show_interrupts(struct seq_file *p, void *v)
128{
129	int i = *(loff_t *) v;
130	struct irqaction * action;
131
132	if (i < NR_IRQS) {
133	    	action = irq_desc[i].action;
134		if (!action)
135			goto out;
136		seq_printf(p, "%3d: %10u ", i, kstat_irqs(i));
137		seq_printf(p, "  %s", action->name);
138		for (action = action->next; action; action = action->next) {
139			seq_printf(p, ", %s", action->name);
140		}
141		seq_putc(p, '\n');
142	} else if (i == NR_IRQS) {
143		show_fiq_list(p, v);
144		seq_printf(p, "Err: %10lu\n", irq_err_count);
145	}
146out:
147	return 0;
148}
149
150/*
151 * IRQ lock detection.
152 *
153 * Hopefully, this should get us out of a few locked situations.
154 * However, it may take a while for this to happen, since we need
155 * a large number if IRQs to appear in the same jiffie with the
156 * same instruction pointer (or within 2 instructions).
157 */
158static int check_irq_lock(struct irqdesc *desc, int irq, struct pt_regs *regs)
159{
160	unsigned long instr_ptr = instruction_pointer(regs);
161
162	if (desc->lck_jif == jiffies &&
163	    desc->lck_pc >= instr_ptr && desc->lck_pc < instr_ptr + 8) {
164		desc->lck_cnt += 1;
165
166		if (desc->lck_cnt > MAX_IRQ_CNT) {
167			printk(KERN_ERR "IRQ LOCK: IRQ%d is locking the system, disabled\n", irq);
168			return 1;
169		}
170	} else {
171		desc->lck_cnt = 0;
172		desc->lck_pc  = instruction_pointer(regs);
173		desc->lck_jif = jiffies;
174	}
175	return 0;
176}
177
178static void
179__do_irq(unsigned int irq, struct irqaction *action, struct pt_regs *regs)
180{
181	unsigned int status;
182	int ret;
183
184	spin_unlock(&irq_controller_lock);
185	if (!(action->flags & IRQF_DISABLED))
186		local_irq_enable();
187
188	status = 0;
189	do {
190		ret = action->handler(irq, action->dev_id, regs);
191		if (ret == IRQ_HANDLED)
192			status |= action->flags;
193		action = action->next;
194	} while (action);
195
196	if (status & IRQF_SAMPLE_RANDOM)
197		add_interrupt_randomness(irq);
198
199	spin_lock_irq(&irq_controller_lock);
200}
201
202/*
203 * This is for software-decoded IRQs.  The caller is expected to
204 * handle the ack, clear, mask and unmask issues.
205 */
206void
207do_simple_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
208{
209	struct irqaction *action;
210	const int cpu = smp_processor_id();
211
212	desc->triggered = 1;
213
214	kstat_cpu(cpu).irqs[irq]++;
215
216	action = desc->action;
217	if (action)
218		__do_irq(irq, desc->action, regs);
219}
220
221/*
222 * Most edge-triggered IRQ implementations seem to take a broken
223 * approach to this.  Hence the complexity.
224 */
225void
226do_edge_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
227{
228	const int cpu = smp_processor_id();
229
230	desc->triggered = 1;
231
232	/*
233	 * If we're currently running this IRQ, or its disabled,
234	 * we shouldn't process the IRQ.  Instead, turn on the
235	 * hardware masks.
236	 */
237	if (unlikely(desc->running || !desc->enabled))
238		goto running;
239
240	/*
241	 * Acknowledge and clear the IRQ, but don't mask it.
242	 */
243	desc->chip->ack(irq);
244
245	/*
246	 * Mark the IRQ currently in progress.
247	 */
248	desc->running = 1;
249
250	kstat_cpu(cpu).irqs[irq]++;
251
252	do {
253		struct irqaction *action;
254
255		action = desc->action;
256		if (!action)
257			break;
258
259		if (desc->pending && desc->enabled) {
260			desc->pending = 0;
261			desc->chip->unmask(irq);
262		}
263
264		__do_irq(irq, action, regs);
265	} while (desc->pending);
266
267	desc->running = 0;
268
269	/*
270	 * If we were disabled or freed, shut down the handler.
271	 */
272	if (likely(desc->action && !check_irq_lock(desc, irq, regs)))
273		return;
274
275 running:
276	/*
277	 * We got another IRQ while this one was masked or
278	 * currently running.  Delay it.
279	 */
280	desc->pending = 1;
281	desc->chip->mask(irq);
282	desc->chip->ack(irq);
283}
284
285/*
286 * Level-based IRQ handler.  Nice and simple.
287 */
288void
289do_level_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
290{
291	struct irqaction *action;
292	const int cpu = smp_processor_id();
293
294	desc->triggered = 1;
295
296	/*
297	 * Acknowledge, clear _AND_ disable the interrupt.
298	 */
299	desc->chip->ack(irq);
300
301	if (likely(desc->enabled)) {
302		kstat_cpu(cpu).irqs[irq]++;
303
304		/*
305		 * Return with this interrupt masked if no action
306		 */
307		action = desc->action;
308		if (action) {
309			__do_irq(irq, desc->action, regs);
310
311			if (likely(desc->enabled &&
312				   !check_irq_lock(desc, irq, regs)))
313				desc->chip->unmask(irq);
314		}
315	}
316}
317
318/*
319 * do_IRQ handles all hardware IRQ's.  Decoded IRQs should not
320 * come via this function.  Instead, they should provide their
321 * own 'handler'
322 */
323asmlinkage void asm_do_IRQ(int irq, struct pt_regs *regs)
324{
325	struct irqdesc *desc = irq_desc + irq;
326
327	/*
328	 * Some hardware gives randomly wrong interrupts.  Rather
329	 * than crashing, do something sensible.
330	 */
331	if (irq >= NR_IRQS)
332		desc = &bad_irq_desc;
333
334	irq_enter();
335	spin_lock(&irq_controller_lock);
336	desc->handle(irq, desc, regs);
337	spin_unlock(&irq_controller_lock);
338	irq_exit();
339}
340
341void __set_irq_handler(unsigned int irq, irq_handler_t handle, int is_chained)
342{
343	struct irqdesc *desc;
344	unsigned long flags;
345
346	if (irq >= NR_IRQS) {
347		printk(KERN_ERR "Trying to install handler for IRQ%d\n", irq);
348		return;
349	}
350
351	if (handle == NULL)
352		handle = do_bad_IRQ;
353
354	desc = irq_desc + irq;
355
356	if (is_chained && desc->chip == &bad_chip)
357		printk(KERN_WARNING "Trying to install chained handler for IRQ%d\n", irq);
358
359	spin_lock_irqsave(&irq_controller_lock, flags);
360	if (handle == do_bad_IRQ) {
361		desc->chip->mask(irq);
362		desc->chip->ack(irq);
363		desc->depth = 1;
364		desc->enabled = 0;
365	}
366	desc->handle = handle;
367	if (handle != do_bad_IRQ && is_chained) {
368		desc->valid = 0;
369		desc->probe_ok = 0;
370		desc->depth = 0;
371		desc->chip->unmask(irq);
372	}
373	spin_unlock_irqrestore(&irq_controller_lock, flags);
374}
375
376void set_irq_chip(unsigned int irq, struct irqchip *chip)
377{
378	struct irqdesc *desc;
379	unsigned long flags;
380
381	if (irq >= NR_IRQS) {
382		printk(KERN_ERR "Trying to install chip for IRQ%d\n", irq);
383		return;
384	}
385
386	if (chip == NULL)
387		chip = &bad_chip;
388
389	desc = irq_desc + irq;
390	spin_lock_irqsave(&irq_controller_lock, flags);
391	desc->chip = chip;
392	spin_unlock_irqrestore(&irq_controller_lock, flags);
393}
394
395int set_irq_type(unsigned int irq, unsigned int type)
396{
397	struct irqdesc *desc;
398	unsigned long flags;
399	int ret = -ENXIO;
400
401	if (irq >= NR_IRQS) {
402		printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
403		return -ENODEV;
404	}
405
406	desc = irq_desc + irq;
407	if (desc->chip->type) {
408		spin_lock_irqsave(&irq_controller_lock, flags);
409		ret = desc->chip->type(irq, type);
410		spin_unlock_irqrestore(&irq_controller_lock, flags);
411	}
412
413	return ret;
414}
415
416void set_irq_flags(unsigned int irq, unsigned int iflags)
417{
418	struct irqdesc *desc;
419	unsigned long flags;
420
421	if (irq >= NR_IRQS) {
422		printk(KERN_ERR "Trying to set irq flags for IRQ%d\n", irq);
423		return;
424	}
425
426	desc = irq_desc + irq;
427	spin_lock_irqsave(&irq_controller_lock, flags);
428	desc->valid = (iflags & IRQF_VALID) != 0;
429	desc->probe_ok = (iflags & IRQF_PROBE) != 0;
430	desc->noautoenable = (iflags & IRQF_NOAUTOEN) != 0;
431	spin_unlock_irqrestore(&irq_controller_lock, flags);
432}
433
434int setup_irq(unsigned int irq, struct irqaction *new)
435{
436	int shared = 0;
437	struct irqaction *old, **p;
438	unsigned long flags;
439	struct irqdesc *desc;
440
441	/*
442	 * Some drivers like serial.c use request_irq() heavily,
443	 * so we have to be careful not to interfere with a
444	 * running system.
445	 */
446	if (new->flags & IRQF_SAMPLE_RANDOM) {
447		/*
448		 * This function might sleep, we want to call it first,
449		 * outside of the atomic block.
450		 * Yes, this might clear the entropy pool if the wrong
451		 * driver is attempted to be loaded, without actually
452		 * installing a new handler, but is this really a problem,
453		 * only the sysadmin is able to do this.
454		 */
455	        rand_initialize_irq(irq);
456	}
457
458	/*
459	 * The following block of code has to be executed atomically
460	 */
461	desc = irq_desc + irq;
462	spin_lock_irqsave(&irq_controller_lock, flags);
463	p = &desc->action;
464	if ((old = *p) != NULL) {
465		/* Can't share interrupts unless both agree to */
466		if (!(old->flags & new->flags & IRQF_SHARED)) {
467			spin_unlock_irqrestore(&irq_controller_lock, flags);
468			return -EBUSY;
469		}
470
471		/* add new interrupt at end of irq queue */
472		do {
473			p = &old->next;
474			old = *p;
475		} while (old);
476		shared = 1;
477	}
478
479	*p = new;
480
481	if (!shared) {
482 		desc->probing = 0;
483		desc->running = 0;
484		desc->pending = 0;
485		desc->depth = 1;
486		if (!desc->noautoenable) {
487			desc->depth = 0;
488			desc->enabled = 1;
489			desc->chip->unmask(irq);
490		}
491	}
492
493	spin_unlock_irqrestore(&irq_controller_lock, flags);
494	return 0;
495}
496
497/**
498 *	request_irq - allocate an interrupt line
499 *	@irq: Interrupt line to allocate
500 *	@handler: Function to be called when the IRQ occurs
501 *	@irqflags: Interrupt type flags
502 *	@devname: An ascii name for the claiming device
503 *	@dev_id: A cookie passed back to the handler function
504 *
505 *	This call allocates interrupt resources and enables the
506 *	interrupt line and IRQ handling. From the point this
507 *	call is made your handler function may be invoked. Since
508 *	your handler function must clear any interrupt the board
509 *	raises, you must take care both to initialise your hardware
510 *	and to set up the interrupt handler in the right order.
511 *
512 *	Dev_id must be globally unique. Normally the address of the
513 *	device data structure is used as the cookie. Since the handler
514 *	receives this value it makes sense to use it.
515 *
516 *	If your interrupt is shared you must pass a non NULL dev_id
517 *	as this is required when freeing the interrupt.
518 *
519 *	Flags:
520 *
521 *	IRQF_SHARED		Interrupt is shared
522 *
523 *	IRQF_DISABLED	Disable local interrupts while processing
524 *
525 *	IRQF_SAMPLE_RANDOM	The interrupt can be used for entropy
526 *
527 */
528
529int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *),
530		 unsigned long irq_flags, const char * devname, void *dev_id)
531{
532	unsigned long retval;
533	struct irqaction *action;
534
535	if (irq >= NR_IRQS || !irq_desc[irq].valid || !handler ||
536	    (irq_flags & IRQF_SHARED && !dev_id))
537		return -EINVAL;
538
539	action = kmalloc(sizeof(struct irqaction), GFP_KERNEL);
540	if (!action)
541		return -ENOMEM;
542
543	action->handler = handler;
544	action->flags = irq_flags;
545	cpus_clear(action->mask);
546	action->name = devname;
547	action->next = NULL;
548	action->dev_id = dev_id;
549
550	retval = setup_irq(irq, action);
551
552	if (retval)
553		kfree(action);
554	return retval;
555}
556
557EXPORT_SYMBOL(request_irq);
558
559/**
560 *	free_irq - free an interrupt
561 *	@irq: Interrupt line to free
562 *	@dev_id: Device identity to free
563 *
564 *	Remove an interrupt handler. The handler is removed and if the
565 *	interrupt line is no longer in use by any driver it is disabled.
566 *	On a shared IRQ the caller must ensure the interrupt is disabled
567 *	on the card it drives before calling this function.
568 *
569 *	This function may be called from interrupt context.
570 */
571void free_irq(unsigned int irq, void *dev_id)
572{
573	struct irqaction * action, **p;
574	unsigned long flags;
575
576	if (irq >= NR_IRQS || !irq_desc[irq].valid) {
577		printk(KERN_ERR "Trying to free IRQ%d\n",irq);
578#ifdef CONFIG_DEBUG_ERRORS
579		__backtrace();
580#endif
581		return;
582	}
583
584	spin_lock_irqsave(&irq_controller_lock, flags);
585	for (p = &irq_desc[irq].action; (action = *p) != NULL; p = &action->next) {
586		if (action->dev_id != dev_id)
587			continue;
588
589	    	/* Found it - now free it */
590		*p = action->next;
591		kfree(action);
592		goto out;
593	}
594	printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
595#ifdef CONFIG_DEBUG_ERRORS
596	__backtrace();
597#endif
598out:
599	spin_unlock_irqrestore(&irq_controller_lock, flags);
600}
601
602EXPORT_SYMBOL(free_irq);
603
604/* Start the interrupt probing.  Unlike other architectures,
605 * we don't return a mask of interrupts from probe_irq_on,
606 * but return the number of interrupts enabled for the probe.
607 * The interrupts which have been enabled for probing is
608 * instead recorded in the irq_desc structure.
609 */
610unsigned long probe_irq_on(void)
611{
612	unsigned int i, irqs = 0;
613	unsigned long delay;
614
615	/*
616	 * first snaffle up any unassigned but
617	 * probe-able interrupts
618	 */
619	spin_lock_irq(&irq_controller_lock);
620	for (i = 0; i < NR_IRQS; i++) {
621		if (!irq_desc[i].probe_ok || irq_desc[i].action)
622			continue;
623
624		irq_desc[i].probing = 1;
625		irq_desc[i].triggered = 0;
626		if (irq_desc[i].chip->type)
627			irq_desc[i].chip->type(i, IRQT_PROBE);
628		irq_desc[i].chip->unmask(i);
629		irqs += 1;
630	}
631	spin_unlock_irq(&irq_controller_lock);
632
633	/*
634	 * wait for spurious interrupts to mask themselves out again
635	 */
636	for (delay = jiffies + HZ/10; time_before(jiffies, delay); )
637		/* min 100ms delay */;
638
639	/*
640	 * now filter out any obviously spurious interrupts
641	 */
642	spin_lock_irq(&irq_controller_lock);
643	for (i = 0; i < NR_IRQS; i++) {
644		if (irq_desc[i].probing && irq_desc[i].triggered) {
645			irq_desc[i].probing = 0;
646			irqs -= 1;
647		}
648	}
649	spin_unlock_irq(&irq_controller_lock);
650
651	return irqs;
652}
653
654EXPORT_SYMBOL(probe_irq_on);
655
656/*
657 * Possible return values:
658 *  >= 0 - interrupt number
659 *    -1 - no interrupt/many interrupts
660 */
661int probe_irq_off(unsigned long irqs)
662{
663	unsigned int i;
664	int irq_found = NO_IRQ;
665
666	/*
667	 * look at the interrupts, and find exactly one
668	 * that we were probing has been triggered
669	 */
670	spin_lock_irq(&irq_controller_lock);
671	for (i = 0; i < NR_IRQS; i++) {
672		if (irq_desc[i].probing &&
673		    irq_desc[i].triggered) {
674			if (irq_found != NO_IRQ) {
675				irq_found = NO_IRQ;
676				goto out;
677			}
678			irq_found = i;
679		}
680	}
681
682	if (irq_found == -1)
683		irq_found = NO_IRQ;
684out:
685	spin_unlock_irq(&irq_controller_lock);
686
687	return irq_found;
688}
689
690EXPORT_SYMBOL(probe_irq_off);
691
692void __init init_irq_proc(void)
693{
694}
695
696void __init init_IRQ(void)
697{
698	struct irqdesc *desc;
699	extern void init_dma(void);
700	int irq;
701
702	for (irq = 0, desc = irq_desc; irq < NR_IRQS; irq++, desc++)
703		*desc = bad_irq_desc;
704
705	arc_init_irq();
706	init_dma();
707}
708