File: /usr/src/linux/arch/i386/kernel/irq.c

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