1/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License.  See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Code to handle x86 style IRQs plus some generic interrupt stuff.
7 *
8 * Copyright (C) 1992 Linus Torvalds
9 * Copyright (C) 1994 - 2000 Ralf Baechle
10 */
11#include <linux/config.h>
12#include <linux/kernel.h>
13#include <linux/delay.h>
14#include <linux/init.h>
15#include <linux/interrupt.h>
16#include <linux/kernel_stat.h>
17#include <linux/module.h>
18#include <linux/proc_fs.h>
19#include <linux/slab.h>
20#include <linux/mm.h>
21#include <linux/random.h>
22#include <linux/sched.h>
23
24#include <asm/atomic.h>
25#include <asm/system.h>
26#include <asm/uaccess.h>
27
28/*
29 * Controller mappings for all interrupt sources:
30 */
31irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned =
32	{ [0 ... NR_IRQS-1] = { 0, &no_irq_type, NULL, 0, SPIN_LOCK_UNLOCKED}};
33
34static void register_irq_proc (unsigned int irq);
35
36/*
37 * Special irq handlers.
38 */
39
40void no_action(int cpl, void *dev_id, struct pt_regs *regs) { }
41
42/*
43 * Generic no controller code
44 */
45
46static void enable_none(unsigned int irq) { }
47static unsigned int startup_none(unsigned int irq) { return 0; }
48static void disable_none(unsigned int irq) { }
49static void ack_none(unsigned int irq)
50{
51	/*
52	 * 'what should we do if we get a hw irq event on an illegal vector'.
53	 * each architecture has to answer this themselves, it doesn't deserve
54	 * a generic callback i think.
55	 */
56	printk("unexpected interrupt %d\n", irq);
57}
58
59/* startup is the same as "enable", shutdown is same as "disable" */
60#define shutdown_none	disable_none
61#define end_none	enable_none
62
63struct hw_interrupt_type no_irq_type = {
64	"none",
65	startup_none,
66	shutdown_none,
67	enable_none,
68	disable_none,
69	ack_none,
70	end_none
71};
72
73atomic_t irq_err_count;
74
75/*
76 * Generic, controller-independent functions:
77 */
78
79int get_irq_list(char *buf)
80{
81	int i, j;
82	struct irqaction * action;
83	char *p = buf;
84
85	p += sprintf(p, "           ");
86	for (j=0; j<smp_num_cpus; j++)
87		p += sprintf(p, "CPU%d       ",j);
88	*p++ = '\n';
89
90	for (i = 0 ; i < NR_IRQS ; i++) {
91		action = irq_desc[i].action;
92		if (!action)
93			continue;
94		p += sprintf(p, "%3d: ",i);
95#ifndef CONFIG_SMP
96		p += sprintf(p, "%10u ", kstat_irqs(i));
97#else
98		for (j = 0; j < smp_num_cpus; j++)
99			p += sprintf(p, "%10u ",
100				kstat.irqs[cpu_logical_map(j)][i]);
101#endif
102		p += sprintf(p, " %14s", irq_desc[i].handler->typename);
103		p += sprintf(p, "  %s", action->name);
104
105		for (action=action->next; action; action = action->next)
106			p += sprintf(p, ", %s", action->name);
107		*p++ = '\n';
108	}
109	p += sprintf(p, "\n");
110	p += sprintf(p, "ERR: %10u\n", atomic_read(&irq_err_count));
111	return p - buf;
112}
113
114#ifdef CONFIG_SMP
115int global_irq_holder = NO_PROC_ID;
116spinlock_t global_irq_lock = SPIN_LOCK_UNLOCKED;
117
118/*
119 * Most of this code is take from the mips64 tree (ip27-irq.c).  It's virtually
120 * identical to the i386 implentation in arh/i386/irq.c, with translations for
121 * the interrupt enable bit
122 */
123
124#define MAXCOUNT 		100000000
125#define SYNC_OTHER_CORES(x)	udelay(x+1)
126
127static inline void wait_on_irq(int cpu)
128{
129	int count = MAXCOUNT;
130
131	for (;;) {
132
133		/*
134		 * Wait until all interrupts are gone. Wait
135		 * for bottom half handlers unless we're
136		 * already executing in one..
137		 */
138		if (!irqs_running())
139			if (local_bh_count(cpu) || !spin_is_locked(&global_bh_lock))
140				break;
141
142		/* Duh, we have to loop. Release the lock to avoid deadlocks */
143		spin_unlock(&global_irq_lock);
144
145		for (;;) {
146			if (!--count) {
147				printk("Count spun out.  Huh?\n");
148				count = ~0;
149			}
150			__sti();
151			SYNC_OTHER_CORES(cpu);
152			__cli();
153			if (irqs_running())
154				continue;
155			if (spin_is_locked(&global_irq_lock))
156				continue;
157			if (!local_bh_count(cpu) && spin_is_locked(&global_bh_lock))
158				continue;
159			if (spin_trylock(&global_irq_lock))
160				break;
161		}
162	}
163}
164
165/*
166 * This is called when we want to synchronize with
167 * interrupts. We may for example tell a device to
168 * stop sending interrupts: but to make sure there
169 * are no interrupts that are executing on another
170 * CPU we need to call this function.
171 */
172void synchronize_irq(void)
173{
174	if (irqs_running()) {
175		/* Stupid approach */
176		cli();
177		sti();
178	}
179}
180
181static inline void get_irqlock(int cpu)
182{
183	if (!spin_trylock(&global_irq_lock)) {
184		/* do we already hold the lock? */
185		if ((unsigned char) cpu == global_irq_holder)
186			return;
187		/* Uhhuh.. Somebody else got it. Wait.. */
188		spin_lock(&global_irq_lock);
189	}
190	/*
191	 * We also to make sure that nobody else is running
192	 * in an interrupt context.
193	 */
194	wait_on_irq(cpu);
195
196	/*
197	 * Ok, finally..
198	 */
199	global_irq_holder = cpu;
200}
201
202/*
203 * A global "cli()" while in an interrupt context turns into just a local
204 * cli(). Interrupts should use spinlocks for the (very unlikely) case that
205 * they ever want to protect against each other.
206 *
207 * If we already have local interrupts disabled, this will not turn a local
208 * disable into a global one (problems with spinlocks: this makes
209 * save_flags+cli+sti usable inside a spinlock).
210 */
211
212void __global_cli(void)
213{
214	unsigned int flags;
215
216	__save_flags(flags);
217	if (flags & ST0_IE) {
218		int cpu = smp_processor_id();
219		__cli();
220		if (!local_irq_count(cpu))
221			get_irqlock(cpu);
222	}
223}
224
225void __global_sti(void)
226{
227	int cpu = smp_processor_id();
228
229	if (!local_irq_count(cpu))
230		release_irqlock(cpu);
231	__sti();
232}
233
234/*
235 * SMP flags value to restore to:
236 * 0 - global cli
237 * 1 - global sti
238 * 2 - local cli
239 * 3 - local sti
240 */
241unsigned long __global_save_flags(void)
242{
243	int retval;
244	int local_enabled;
245	unsigned long flags;
246	int cpu = smp_processor_id();
247
248	__save_flags(flags);
249	local_enabled = (flags & ST0_IE);
250	/* default to local */
251	retval = 2 + local_enabled;
252
253	/* check for global flags if we're not in an interrupt */
254	if (!local_irq_count(cpu)) {
255		if (local_enabled)
256			retval = 1;
257		if (global_irq_holder == cpu)
258			retval = 0;
259	}
260
261	return retval;
262}
263
264void __global_restore_flags(unsigned long flags)
265{
266	switch (flags) {
267		case 0:
268			__global_cli();
269			break;
270		case 1:
271			__global_sti();
272			break;
273		case 2:
274			__cli();
275			break;
276		case 3:
277			__sti();
278			break;
279		default:
280			printk("global_restore_flags: %08lx\n", flags);
281	}
282}
283#endif /* CONFIG_SMP */
284
285/*
286 * This should really return information about whether
287 * we should do bottom half handling etc. Right now we
288 * end up _always_ checking the bottom half, which is a
289 * waste of time and is not what some drivers would
290 * prefer.
291 */
292int handle_IRQ_event(unsigned int irq, struct pt_regs * regs, struct irqaction * action)
293{
294	int status;
295	int cpu = smp_processor_id();
296
297	irq_enter(cpu, irq);
298
299	status = 1;	/* Force the "do bottom halves" bit */
300
301	if (!(action->flags & SA_INTERRUPT))
302		__sti();
303
304	do {
305		status |= action->flags;
306		action->handler(irq, action->dev_id, regs);
307		action = action->next;
308	} while (action);
309	if (status & SA_SAMPLE_RANDOM)
310		add_interrupt_randomness(irq);
311	__cli();
312
313	irq_exit(cpu, irq);
314
315	return status;
316}
317
318/*
319 * Generic enable/disable code: this just calls
320 * down into the PIC-specific version for the actual
321 * hardware disable after having gotten the irq
322 * controller lock.
323 */
324
325/**
326 *	disable_irq_nosync - disable an irq without waiting
327 *	@irq: Interrupt to disable
328 *
329 *	Disable the selected interrupt line. Disables of an interrupt
330 *	stack. Unlike disable_irq(), this function does not ensure existing
331 *	instances of the IRQ handler have completed before returning.
332 *
333 *	This function may be called from IRQ context.
334 */
335
336void inline disable_irq_nosync(unsigned int irq)
337{
338	irq_desc_t *desc = irq_desc + irq;
339	unsigned long flags;
340
341	spin_lock_irqsave(&desc->lock, flags);
342	if (!desc->depth++) {
343		desc->status |= IRQ_DISABLED;
344		desc->handler->disable(irq);
345	}
346	spin_unlock_irqrestore(&desc->lock, flags);
347}
348
349/**
350 *	disable_irq - disable an irq and wait for completion
351 *	@irq: Interrupt to disable
352 *
353 *	Disable the selected interrupt line. Disables of an interrupt
354 *	stack. That is for two disables you need two enables. This
355 *	function waits for any pending IRQ handlers for this interrupt
356 *	to complete before returning. If you use this function while
357 *	holding a resource the IRQ handler may need you will deadlock.
358 *
359 *	This function may be called - with care - from IRQ context.
360 */
361
362void disable_irq(unsigned int irq)
363{
364	disable_irq_nosync(irq);
365
366	if (!local_irq_count(smp_processor_id())) {
367		do {
368			barrier();
369		} while (irq_desc[irq].status & IRQ_INPROGRESS);
370	}
371}
372
373/**
374 *	enable_irq - enable interrupt handling on an irq
375 *	@irq: Interrupt to enable
376 *
377 *	Re-enables the processing of interrupts on this IRQ line
378 *	providing no disable_irq calls are now in effect.
379 *
380 *	This function may be called from IRQ context.
381 */
382
383void enable_irq(unsigned int irq)
384{
385	irq_desc_t *desc = irq_desc + irq;
386	unsigned long flags;
387
388	spin_lock_irqsave(&desc->lock, flags);
389	switch (desc->depth) {
390	case 1: {
391		unsigned int status = desc->status & ~IRQ_DISABLED;
392		desc->status = status;
393		if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
394			desc->status = status | IRQ_REPLAY;
395			hw_resend_irq(desc->handler,irq);
396		}
397		desc->handler->enable(irq);
398		/* fall-through */
399	}
400	default:
401		desc->depth--;
402		break;
403	case 0:
404		printk("enable_irq(%u) unbalanced from %p\n", irq,
405		       __builtin_return_address(0));
406	}
407	spin_unlock_irqrestore(&desc->lock, flags);
408}
409
410/*
411 * do_IRQ handles all normal device IRQ's (the special
412 * SMP cross-CPU interrupts have their own specific
413 * handlers).
414 */
415asmlinkage unsigned int do_IRQ(int irq, struct pt_regs *regs)
416{
417	/*
418	 * We ack quickly, we don't want the irq controller
419	 * thinking we're snobs just because some other CPU has
420	 * disabled global interrupts (we have already done the
421	 * INT_ACK cycles, it's too late to try to pretend to the
422	 * controller that we aren't taking the interrupt).
423	 *
424	 * 0 return value means that this irq is already being
425	 * handled by some other CPU. (or is disabled)
426	 */
427	int cpu = smp_processor_id();
428	irq_desc_t *desc = irq_desc + irq;
429	struct irqaction * action;
430	unsigned int status;
431
432	kstat.irqs[cpu][irq]++;
433	spin_lock(&desc->lock);
434	desc->handler->ack(irq);
435	/*
436	   REPLAY is when Linux resends an IRQ that was dropped earlier
437	   WAITING is used by probe to mark irqs that are being tested
438	   */
439	status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
440	status |= IRQ_PENDING; /* we _want_ to handle it */
441
442	/*
443	 * If the IRQ is disabled for whatever reason, we cannot
444	 * use the action we have.
445	 */
446	action = NULL;
447	if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
448		action = desc->action;
449		status &= ~IRQ_PENDING; /* we commit to handling */
450		status |= IRQ_INPROGRESS; /* we are handling it */
451	}
452	desc->status = status;
453
454	/*
455	 * If there is no IRQ handler or it was disabled, exit early.
456	   Since we set PENDING, if another processor is handling
457	   a different instance of this same irq, the other processor
458	   will take care of it.
459	 */
460	if (!action)
461		goto out;
462
463	/*
464	 * Edge triggered interrupts need to remember
465	 * pending events.
466	 * This applies to any hw interrupts that allow a second
467	 * instance of the same irq to arrive while we are in do_IRQ
468	 * or in the handler. But the code here only handles the _second_
469	 * instance of the irq, not the third or fourth. So it is mostly
470	 * useful for irq hardware that does not mask cleanly in an
471	 * SMP environment.
472	 */
473	for (;;) {
474		spin_unlock(&desc->lock);
475		handle_IRQ_event(irq, regs, action);
476		spin_lock(&desc->lock);
477
478		if (!(desc->status & IRQ_PENDING))
479			break;
480		desc->status &= ~IRQ_PENDING;
481	}
482	desc->status &= ~IRQ_INPROGRESS;
483out:
484	/*
485	 * The ->end() handler has to deal with interrupts which got
486	 * disabled while the handler was running.
487	 */
488	desc->handler->end(irq);
489	spin_unlock(&desc->lock);
490
491	if (softirq_pending(cpu))
492		do_softirq();
493	return 1;
494}
495
496/**
497 *	request_irq - allocate an interrupt line
498 *	@irq: Interrupt line to allocate
499 *	@handler: Function to be called when the IRQ occurs
500 *	@irqflags: Interrupt type flags
501 *	@devname: An ascii name for the claiming device
502 *	@dev_id: A cookie passed back to the handler function
503 *
504 *	This call allocates interrupt resources and enables the
505 *	interrupt line and IRQ handling. From the point this
506 *	call is made your handler function may be invoked. Since
507 *	your handler function must clear any interrupt the board
508 *	raises, you must take care both to initialise your hardware
509 *	and to set up the interrupt handler in the right order.
510 *
511 *	Dev_id must be globally unique. Normally the address of the
512 *	device data structure is used as the cookie. Since the handler
513 *	receives this value it makes sense to use it.
514 *
515 *	If your interrupt is shared you must pass a non NULL dev_id
516 *	as this is required when freeing the interrupt.
517 *
518 *	Flags:
519 *
520 *	SA_SHIRQ		Interrupt is shared
521 *
522 *	SA_INTERRUPT		Disable local interrupts while processing
523 *
524 *	SA_SAMPLE_RANDOM	The interrupt can be used for entropy
525 *
526 */
527
528int request_irq(unsigned int irq,
529		void (*handler)(int, void *, struct pt_regs *),
530		unsigned long irqflags,
531		const char * devname,
532		void *dev_id)
533{
534	int retval;
535	struct irqaction * action;
536
537	/*
538	 * Sanity-check: shared interrupts should REALLY pass in
539	 * a real dev-ID, otherwise we'll have trouble later trying
540	 * to figure out which interrupt is which (messes up the
541	 * interrupt freeing logic etc).
542	 */
543	if (irqflags & SA_SHIRQ) {
544		if (!dev_id)
545			printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
546	}
547
548	if (irq >= NR_IRQS)
549		return -EINVAL;
550	if (!handler)
551		return -EINVAL;
552
553	action = (struct irqaction *)
554			kmalloc(sizeof(struct irqaction), GFP_KERNEL);
555	if (!action)
556		return -ENOMEM;
557
558	action->handler = handler;
559	action->flags = irqflags;
560	action->mask = 0;
561	action->name = devname;
562	action->next = NULL;
563	action->dev_id = dev_id;
564
565	retval = setup_irq(irq, action);
566	if (retval)
567		kfree(action);
568	return retval;
569}
570
571/**
572 *	free_irq - free an interrupt
573 *	@irq: Interrupt line to free
574 *	@dev_id: Device identity to free
575 *
576 *	Remove an interrupt handler. The handler is removed and if the
577 *	interrupt line is no longer in use by any driver it is disabled.
578 *	On a shared IRQ the caller must ensure the interrupt is disabled
579 *	on the card it drives before calling this function. The function
580 *	does not return until any executing interrupts for this IRQ
581 *	have completed.
582 *
583 *	This function may be called from interrupt context.
584 *
585 *	Bugs: Attempting to free an irq in a handler for the same irq hangs
586 *	      the machine.
587 */
588
589void free_irq(unsigned int irq, void *dev_id)
590{
591	irq_desc_t *desc;
592	struct irqaction **p;
593	unsigned long flags;
594
595	if (irq >= NR_IRQS)
596		return;
597
598	desc = irq_desc + irq;
599	spin_lock_irqsave(&desc->lock,flags);
600	p = &desc->action;
601	for (;;) {
602		struct irqaction * action = *p;
603		if (action) {
604			struct irqaction **pp = p;
605			p = &action->next;
606			if (action->dev_id != dev_id)
607				continue;
608
609			/* Found it - now remove it from the list of entries */
610			*pp = action->next;
611			if (!desc->action) {
612				desc->status |= IRQ_DISABLED;
613				desc->handler->shutdown(irq);
614			}
615			spin_unlock_irqrestore(&desc->lock,flags);
616
617#ifdef CONFIG_SMP
618			/* Wait to make sure it's not being used on another CPU */
619			while (desc->status & IRQ_INPROGRESS)
620				barrier();
621#endif
622			kfree(action);
623			return;
624		}
625		printk("Trying to free free IRQ%d\n",irq);
626		spin_unlock_irqrestore(&desc->lock,flags);
627		return;
628	}
629}
630
631/*
632 * IRQ autodetection code..
633 *
634 * This depends on the fact that any interrupt that
635 * comes in on to an unassigned handler will get stuck
636 * with "IRQ_WAITING" cleared and the interrupt
637 * disabled.
638 */
639
640static DECLARE_MUTEX(probe_sem);
641
642/**
643 *	probe_irq_on	- begin an interrupt autodetect
644 *
645 *	Commence probing for an interrupt. The interrupts are scanned
646 *	and a mask of potential interrupt lines is returned.
647 *
648 */
649
650unsigned long probe_irq_on(void)
651{
652	unsigned int i;
653	irq_desc_t *desc;
654	unsigned long val;
655	unsigned long delay;
656
657	down(&probe_sem);
658	/*
659	 * something may have generated an irq long ago and we want to
660	 * flush such a longstanding irq before considering it as spurious.
661	 */
662	for (i = NR_IRQS-1; i > 0; i--)  {
663		desc = irq_desc + i;
664
665		spin_lock_irq(&desc->lock);
666		if (!irq_desc[i].action)
667			irq_desc[i].handler->startup(i);
668		spin_unlock_irq(&desc->lock);
669	}
670
671	/* Wait for longstanding interrupts to trigger. */
672	for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
673		/* about 20ms delay */ synchronize_irq();
674
675	/*
676	 * enable any unassigned irqs
677	 * (we must startup again here because if a longstanding irq
678	 * happened in the previous stage, it may have masked itself)
679	 */
680	for (i = NR_IRQS-1; i > 0; i--) {
681		desc = irq_desc + i;
682
683		spin_lock_irq(&desc->lock);
684		if (!desc->action) {
685			desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
686			if (desc->handler->startup(i))
687				desc->status |= IRQ_PENDING;
688		}
689		spin_unlock_irq(&desc->lock);
690	}
691
692	/*
693	 * Wait for spurious interrupts to trigger
694	 */
695	for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
696		/* about 100ms delay */ synchronize_irq();
697
698	/*
699	 * Now filter out any obviously spurious interrupts
700	 */
701	val = 0;
702	for (i = 0; i < NR_IRQS; i++) {
703		irq_desc_t *desc = irq_desc + i;
704		unsigned int status;
705
706		spin_lock_irq(&desc->lock);
707		status = desc->status;
708
709		if (status & IRQ_AUTODETECT) {
710			/* It triggered already - consider it spurious. */
711			if (!(status & IRQ_WAITING)) {
712				desc->status = status & ~IRQ_AUTODETECT;
713				desc->handler->shutdown(i);
714			} else
715				if (i < 32)
716					val |= 1 << i;
717		}
718		spin_unlock_irq(&desc->lock);
719	}
720
721	return val;
722}
723
724/*
725 * Return a mask of triggered interrupts (this
726 * can handle only legacy ISA interrupts).
727 */
728
729/**
730 *	probe_irq_mask - scan a bitmap of interrupt lines
731 *	@val:	mask of interrupts to consider
732 *
733 *	Scan the ISA bus interrupt lines and return a bitmap of
734 *	active interrupts. The interrupt probe logic state is then
735 *	returned to its previous value.
736 *
737 *	Note: we need to scan all the irq's even though we will
738 *	only return ISA irq numbers - just so that we reset them
739 *	all to a known state.
740 */
741unsigned int probe_irq_mask(unsigned long val)
742{
743	int i;
744	unsigned int mask;
745
746	mask = 0;
747	for (i = 0; i < NR_IRQS; i++) {
748		irq_desc_t *desc = irq_desc + i;
749		unsigned int status;
750
751		spin_lock_irq(&desc->lock);
752		status = desc->status;
753
754		if (status & IRQ_AUTODETECT) {
755			if (i < 16 && !(status & IRQ_WAITING))
756				mask |= 1 << i;
757
758			desc->status = status & ~IRQ_AUTODETECT;
759			desc->handler->shutdown(i);
760		}
761		spin_unlock_irq(&desc->lock);
762	}
763	up(&probe_sem);
764
765	return mask & val;
766}
767
768/*
769 * Return the one interrupt that triggered (this can
770 * handle any interrupt source).
771 */
772
773/**
774 *	probe_irq_off	- end an interrupt autodetect
775 *	@val: mask of potential interrupts (unused)
776 *
777 *	Scans the unused interrupt lines and returns the line which
778 *	appears to have triggered the interrupt. If no interrupt was
779 *	found then zero is returned. If more than one interrupt is
780 *	found then minus the first candidate is returned to indicate
781 *	their is doubt.
782 *
783 *	The interrupt probe logic state is returned to its previous
784 *	value.
785 *
786 *	BUGS: When used in a module (which arguably shouldnt happen)
787 *	nothing prevents two IRQ probe callers from overlapping. The
788 *	results of this are non-optimal.
789 */
790
791int probe_irq_off(unsigned long val)
792{
793	int i, irq_found, nr_irqs;
794
795	nr_irqs = 0;
796	irq_found = 0;
797	for (i = 0; i < NR_IRQS; i++) {
798		irq_desc_t *desc = irq_desc + i;
799		unsigned int status;
800
801		spin_lock_irq(&desc->lock);
802		status = desc->status;
803
804		if (status & IRQ_AUTODETECT) {
805			if (!(status & IRQ_WAITING)) {
806				if (!nr_irqs)
807					irq_found = i;
808				nr_irqs++;
809			}
810			desc->status = status & ~IRQ_AUTODETECT;
811			desc->handler->shutdown(i);
812		}
813		spin_unlock_irq(&desc->lock);
814	}
815	up(&probe_sem);
816
817	if (nr_irqs > 1)
818		irq_found = -irq_found;
819	return irq_found;
820}
821
822/* this was setup_x86_irq but it seems pretty generic */
823int setup_irq(unsigned int irq, struct irqaction * new)
824{
825	int shared = 0;
826	unsigned long flags;
827	struct irqaction *old, **p;
828	irq_desc_t *desc = irq_desc + irq;
829
830	/*
831	 * Some drivers like serial.c use request_irq() heavily,
832	 * so we have to be careful not to interfere with a
833	 * running system.
834	 */
835	if (new->flags & SA_SAMPLE_RANDOM) {
836		/*
837		 * This function might sleep, we want to call it first,
838		 * outside of the atomic block.
839		 * Yes, this might clear the entropy pool if the wrong
840		 * driver is attempted to be loaded, without actually
841		 * installing a new handler, but is this really a problem,
842		 * only the sysadmin is able to do this.
843		 */
844		rand_initialize_irq(irq);
845	}
846
847	/*
848	 * The following block of code has to be executed atomically
849	 */
850	spin_lock_irqsave(&desc->lock,flags);
851	p = &desc->action;
852	if ((old = *p) != NULL) {
853		/* Can't share interrupts unless both agree to */
854		if (!(old->flags & new->flags & SA_SHIRQ)) {
855			spin_unlock_irqrestore(&desc->lock,flags);
856			return -EBUSY;
857		}
858
859		/* add new interrupt at end of irq queue */
860		do {
861			p = &old->next;
862			old = *p;
863		} while (old);
864		shared = 1;
865	}
866
867	*p = new;
868
869	if (!shared) {
870		desc->depth = 0;
871		desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING);
872		desc->handler->startup(irq);
873	}
874	spin_unlock_irqrestore(&desc->lock,flags);
875
876	register_irq_proc(irq);
877	return 0;
878}
879
880void __init init_generic_irq(void)
881{
882	int i;
883
884	for (i = 0; i < NR_IRQS; i++) {
885		irq_desc[i].status  = IRQ_DISABLED;
886		irq_desc[i].action  = NULL;
887		irq_desc[i].depth   = 1;
888		irq_desc[i].handler = &no_irq_type;
889	}
890}
891
892EXPORT_SYMBOL(disable_irq_nosync);
893EXPORT_SYMBOL(disable_irq);
894EXPORT_SYMBOL(enable_irq);
895EXPORT_SYMBOL(probe_irq_mask);
896
897static struct proc_dir_entry * root_irq_dir;
898static struct proc_dir_entry * irq_dir [NR_IRQS];
899
900#define HEX_DIGITS 8
901
902static unsigned int parse_hex_value (const char *buffer,
903		unsigned long count, unsigned long *ret)
904{
905	unsigned char hexnum [HEX_DIGITS];
906	unsigned long value;
907	int i;
908
909	if (!count)
910		return -EINVAL;
911	if (count > HEX_DIGITS)
912		count = HEX_DIGITS;
913	if (copy_from_user(hexnum, buffer, count))
914		return -EFAULT;
915
916	/*
917	 * Parse the first 8 characters as a hex string, any non-hex char
918	 * is end-of-string. '00e1', 'e1', '00E1', 'E1' are all the same.
919	 */
920	value = 0;
921
922	for (i = 0; i < count; i++) {
923		unsigned int c = hexnum[i];
924
925		switch (c) {
926			case '0' ... '9': c -= '0'; break;
927			case 'a' ... 'f': c -= 'a'-10; break;
928			case 'A' ... 'F': c -= 'A'-10; break;
929		default:
930			goto out;
931		}
932		value = (value << 4) | c;
933	}
934out:
935	*ret = value;
936	return 0;
937}
938
939#if CONFIG_SMP
940
941static struct proc_dir_entry * smp_affinity_entry [NR_IRQS];
942
943static unsigned long irq_affinity [NR_IRQS] = { [0 ... NR_IRQS-1] = ~0UL };
944static int irq_affinity_read_proc (char *page, char **start, off_t off,
945			int count, int *eof, void *data)
946{
947	if (count < HEX_DIGITS+1)
948		return -EINVAL;
949	return sprintf (page, "%08lx\n", irq_affinity[(long)data]);
950}
951
952static int irq_affinity_write_proc (struct file *file, const char *buffer,
953					unsigned long count, void *data)
954{
955	int irq = (long) data, full_count = count, err;
956	unsigned long new_value;
957
958	if (!irq_desc[irq].handler->set_affinity)
959		return -EIO;
960
961	err = parse_hex_value(buffer, count, &new_value);
962
963	/*
964	 * Do not allow disabling IRQs completely - it's a too easy
965	 * way to make the system unusable accidentally :-) At least
966	 * one online CPU still has to be targeted.
967	 */
968	if (!(new_value & cpu_online_map))
969		return -EINVAL;
970
971	irq_affinity[irq] = new_value;
972	irq_desc[irq].handler->set_affinity(irq, new_value);
973
974	return full_count;
975}
976
977#endif
978
979static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
980			int count, int *eof, void *data)
981{
982	unsigned long *mask = (unsigned long *) data;
983	if (count < HEX_DIGITS+1)
984		return -EINVAL;
985	return sprintf (page, "%08lx\n", *mask);
986}
987
988static int prof_cpu_mask_write_proc (struct file *file, const char *buffer,
989					unsigned long count, void *data)
990{
991	unsigned long *mask = (unsigned long *) data, full_count = count, err;
992	unsigned long new_value;
993
994	err = parse_hex_value(buffer, count, &new_value);
995	if (err)
996		return err;
997
998	*mask = new_value;
999	return full_count;
1000}
1001
1002#define MAX_NAMELEN 10
1003
1004static void register_irq_proc (unsigned int irq)
1005{
1006	char name [MAX_NAMELEN];
1007
1008	if (!root_irq_dir || (irq_desc[irq].handler == &no_irq_type) ||
1009			irq_dir[irq])
1010		return;
1011
1012	memset(name, 0, MAX_NAMELEN);
1013	sprintf(name, "%d", irq);
1014
1015	/* create /proc/irq/1234 */
1016	irq_dir[irq] = proc_mkdir(name, root_irq_dir);
1017
1018#if CONFIG_SMP
1019	{
1020		struct proc_dir_entry *entry;
1021
1022		/* create /proc/irq/1234/smp_affinity */
1023		entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
1024
1025		if (entry) {
1026			entry->nlink = 1;
1027			entry->data = (void *)(long)irq;
1028			entry->read_proc = irq_affinity_read_proc;
1029			entry->write_proc = irq_affinity_write_proc;
1030		}
1031
1032		smp_affinity_entry[irq] = entry;
1033	}
1034#endif
1035}
1036
1037unsigned long prof_cpu_mask = -1;
1038
1039void init_irq_proc (void)
1040{
1041	struct proc_dir_entry *entry;
1042	int i;
1043
1044	/* create /proc/irq */
1045	root_irq_dir = proc_mkdir("irq", 0);
1046
1047	/* create /proc/irq/prof_cpu_mask */
1048	entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
1049
1050	if (!entry)
1051	    return;
1052
1053	entry->nlink = 1;
1054	entry->data = (void *)&prof_cpu_mask;
1055	entry->read_proc = prof_cpu_mask_read_proc;
1056	entry->write_proc = prof_cpu_mask_write_proc;
1057
1058	/*
1059	 * Create entries for all existing IRQs.
1060	 */
1061	for (i = 0; i < NR_IRQS; i++)
1062		register_irq_proc(i);
1063}
1064