Merge with Linux 2.4.0-test6-pre2.
[linux-2.6/linux-mips.git] / arch / i386 / kernel / irq.c
blobfcc0a72f5df7156b5b5c09372a301e961b77d026
1 /*
2 * linux/arch/i386/kernel/irq.c
4 * Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar
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.
14 * (mostly architecture independent, will move to kernel/irq.c in 2.5.)
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.
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/malloc.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>
36 #include <asm/io.h>
37 #include <asm/smp.h>
38 #include <asm/system.h>
39 #include <asm/bitops.h>
40 #include <asm/uaccess.h>
41 #include <asm/pgalloc.h>
42 #include <asm/delay.h>
43 #include <asm/desc.h>
44 #include <asm/irq.h>
49 * Linux has a controller-independent x86 interrupt architecture.
50 * every controller has a 'controller-template', that is used
51 * by the main code to do the right thing. Each driver-visible
52 * interrupt source is transparently wired to the apropriate
53 * controller. Thus drivers need not be aware of the
54 * interrupt-controller.
56 * Various interrupt controllers we handle: 8259 PIC, SMP IO-APIC,
57 * PIIX4's internal 8259 PIC and SGI's Visual Workstation Cobalt (IO-)APIC.
58 * (IO-APICs assumed to be messaging to Pentium local-APICs)
60 * the code is designed to be easily extended with new/different
61 * interrupt controllers, without having to do assembly magic.
64 irq_cpustat_t irq_stat [NR_CPUS];
67 * Controller mappings for all interrupt sources:
69 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned =
70 { [0 ... NR_IRQS-1] = { 0, &no_irq_type, NULL, 0, SPIN_LOCK_UNLOCKED}};
72 static void register_irq_proc (unsigned int irq);
75 * Special irq handlers.
78 void no_action(int cpl, void *dev_id, struct pt_regs *regs) { }
81 * Generic no controller code
84 static void enable_none(unsigned int irq) { }
85 static unsigned int startup_none(unsigned int irq) { return 0; }
86 static void disable_none(unsigned int irq) { }
87 static void ack_none(unsigned int irq)
90 * 'what should we do if we get a hw irq event on an illegal vector'.
91 * each architecture has to answer this themselves, it doesnt deserve
92 * a generic callback i think.
94 #if CONFIG_X86
95 printk("unexpected IRQ trap at vector %02x\n", irq);
96 #ifdef CONFIG_X86_LOCAL_APIC
98 * Currently unexpected vectors happen only on SMP and APIC.
99 * We _must_ ack these because every local APIC has only N
100 * irq slots per priority level, and a 'hanging, unacked' IRQ
101 * holds up an irq slot - in excessive cases (when multiple
102 * unexpected vectors occur) that might lock up the APIC
103 * completely.
105 ack_APIC_irq();
106 #endif
107 #endif
110 /* startup is the same as "enable", shutdown is same as "disable" */
111 #define shutdown_none disable_none
112 #define end_none enable_none
114 struct hw_interrupt_type no_irq_type = {
115 "none",
116 startup_none,
117 shutdown_none,
118 enable_none,
119 disable_none,
120 ack_none,
121 end_none
124 volatile unsigned long irq_err_count;
127 * Generic, controller-independent functions:
130 int get_irq_list(char *buf)
132 int i, j;
133 struct irqaction * action;
134 char *p = buf;
136 p += sprintf(p, " ");
137 for (j=0; j<smp_num_cpus; j++)
138 p += sprintf(p, "CPU%d ",j);
139 *p++ = '\n';
141 for (i = 0 ; i < NR_IRQS ; i++) {
142 action = irq_desc[i].action;
143 if (!action)
144 continue;
145 p += sprintf(p, "%3d: ",i);
146 #ifndef CONFIG_SMP
147 p += sprintf(p, "%10u ", kstat_irqs(i));
148 #else
149 for (j = 0; j < smp_num_cpus; j++)
150 p += sprintf(p, "%10u ",
151 kstat.irqs[cpu_logical_map(j)][i]);
152 #endif
153 p += sprintf(p, " %14s", irq_desc[i].handler->typename);
154 p += sprintf(p, " %s", action->name);
156 for (action=action->next; action; action = action->next)
157 p += sprintf(p, ", %s", action->name);
158 *p++ = '\n';
160 p += sprintf(p, "NMI: ");
161 for (j = 0; j < smp_num_cpus; j++)
162 p += sprintf(p, "%10u ",
163 nmi_counter(cpu_logical_map(j)));
164 p += sprintf(p, "\n");
165 #if CONFIG_SMP
166 p += sprintf(p, "LOC: ");
167 for (j = 0; j < smp_num_cpus; j++)
168 p += sprintf(p, "%10u ",
169 apic_timer_irqs[cpu_logical_map(j)]);
170 p += sprintf(p, "\n");
171 #endif
172 p += sprintf(p, "ERR: %10lu\n", irq_err_count);
173 return p - buf;
178 * Global interrupt locks for SMP. Allow interrupts to come in on any
179 * CPU, yet make cli/sti act globally to protect critical regions..
182 #ifdef CONFIG_SMP
183 unsigned char global_irq_holder = NO_PROC_ID;
184 unsigned volatile int global_irq_lock;
186 extern void show_stack(unsigned long* esp);
188 static void show(char * str)
190 int i;
191 int cpu = smp_processor_id();
193 printk("\n%s, CPU %d:\n", str, cpu);
194 printk("irq: %d [",irqs_running());
195 for(i=0;i < smp_num_cpus;i++)
196 printk(" %d",local_irq_count(i));
197 printk(" ]\nbh: %d [",spin_is_locked(&global_bh_lock) ? 1 : 0);
198 for(i=0;i < smp_num_cpus;i++)
199 printk(" %d",local_bh_count(i));
201 printk(" ]\nStack dumps:");
202 for(i = 0; i < smp_num_cpus; i++) {
203 unsigned long esp;
204 if (i == cpu)
205 continue;
206 printk("\nCPU %d:",i);
207 esp = init_tss[i].esp0;
208 if (!esp) {
209 /* tss->esp0 is set to NULL in cpu_init(),
210 * it's initialized when the cpu returns to user
211 * space. -- manfreds
213 printk(" <unknown> ");
214 continue;
216 esp &= ~(THREAD_SIZE-1);
217 esp += sizeof(struct task_struct);
218 show_stack((void*)esp);
220 printk("\nCPU %d:",cpu);
221 show_stack(NULL);
222 printk("\n");
225 #define MAXCOUNT 100000000
228 * I had a lockup scenario where a tight loop doing
229 * spin_unlock()/spin_lock() on CPU#1 was racing with
230 * spin_lock() on CPU#0. CPU#0 should have noticed spin_unlock(), but
231 * apparently the spin_unlock() information did not make it
232 * through to CPU#0 ... nasty, is this by design, do we have to limit
233 * 'memory update oscillation frequency' artificially like here?
235 * Such 'high frequency update' races can be avoided by careful design, but
236 * some of our major constructs like spinlocks use similar techniques,
237 * it would be nice to clarify this issue. Set this define to 0 if you
238 * want to check whether your system freezes. I suspect the delay done
239 * by SYNC_OTHER_CORES() is in correlation with 'snooping latency', but
240 * i thought that such things are guaranteed by design, since we use
241 * the 'LOCK' prefix.
243 #define SUSPECTED_CPU_OR_CHIPSET_BUG_WORKAROUND 0
245 #if SUSPECTED_CPU_OR_CHIPSET_BUG_WORKAROUND
246 # define SYNC_OTHER_CORES(x) udelay(x+1)
247 #else
249 * We have to allow irqs to arrive between __sti and __cli
251 # define SYNC_OTHER_CORES(x) __asm__ __volatile__ ("nop")
252 #endif
254 static inline void wait_on_irq(int cpu)
256 int count = MAXCOUNT;
258 for (;;) {
261 * Wait until all interrupts are gone. Wait
262 * for bottom half handlers unless we're
263 * already executing in one..
265 if (!irqs_running())
266 if (local_bh_count(cpu) || !spin_is_locked(&global_bh_lock))
267 break;
269 /* Duh, we have to loop. Release the lock to avoid deadlocks */
270 clear_bit(0,&global_irq_lock);
272 for (;;) {
273 if (!--count) {
274 show("wait_on_irq");
275 count = ~0;
277 __sti();
278 SYNC_OTHER_CORES(cpu);
279 __cli();
280 if (irqs_running())
281 continue;
282 if (global_irq_lock)
283 continue;
284 if (!local_bh_count(cpu) && spin_is_locked(&global_bh_lock))
285 continue;
286 if (!test_and_set_bit(0,&global_irq_lock))
287 break;
293 * This is called when we want to synchronize with
294 * interrupts. We may for example tell a device to
295 * stop sending interrupts: but to make sure there
296 * are no interrupts that are executing on another
297 * CPU we need to call this function.
299 void synchronize_irq(void)
301 if (irqs_running()) {
302 /* Stupid approach */
303 cli();
304 sti();
308 static inline void get_irqlock(int cpu)
310 if (test_and_set_bit(0,&global_irq_lock)) {
311 /* do we already hold the lock? */
312 if ((unsigned char) cpu == global_irq_holder)
313 return;
314 /* Uhhuh.. Somebody else got it. Wait.. */
315 do {
316 do {
317 } while (test_bit(0,&global_irq_lock));
318 } while (test_and_set_bit(0,&global_irq_lock));
321 * We also to make sure that nobody else is running
322 * in an interrupt context.
324 wait_on_irq(cpu);
327 * Ok, finally..
329 global_irq_holder = cpu;
332 #define EFLAGS_IF_SHIFT 9
335 * A global "cli()" while in an interrupt context
336 * turns into just a local cli(). Interrupts
337 * should use spinlocks for the (very unlikely)
338 * case that they ever want to protect against
339 * each other.
341 * If we already have local interrupts disabled,
342 * this will not turn a local disable into a
343 * global one (problems with spinlocks: this makes
344 * save_flags+cli+sti usable inside a spinlock).
346 void __global_cli(void)
348 unsigned int flags;
350 __save_flags(flags);
351 if (flags & (1 << EFLAGS_IF_SHIFT)) {
352 int cpu = smp_processor_id();
353 __cli();
354 if (!local_irq_count(cpu))
355 get_irqlock(cpu);
359 void __global_sti(void)
361 int cpu = smp_processor_id();
363 if (!local_irq_count(cpu))
364 release_irqlock(cpu);
365 __sti();
369 * SMP flags value to restore to:
370 * 0 - global cli
371 * 1 - global sti
372 * 2 - local cli
373 * 3 - local sti
375 unsigned long __global_save_flags(void)
377 int retval;
378 int local_enabled;
379 unsigned long flags;
380 int cpu = smp_processor_id();
382 __save_flags(flags);
383 local_enabled = (flags >> EFLAGS_IF_SHIFT) & 1;
384 /* default to local */
385 retval = 2 + local_enabled;
387 /* check for global flags if we're not in an interrupt */
388 if (!local_irq_count(cpu)) {
389 if (local_enabled)
390 retval = 1;
391 if (global_irq_holder == cpu)
392 retval = 0;
394 return retval;
397 void __global_restore_flags(unsigned long flags)
399 switch (flags) {
400 case 0:
401 __global_cli();
402 break;
403 case 1:
404 __global_sti();
405 break;
406 case 2:
407 __cli();
408 break;
409 case 3:
410 __sti();
411 break;
412 default:
413 printk("global_restore_flags: %08lx (%08lx)\n",
414 flags, (&flags)[-1]);
418 #endif
421 * This should really return information about whether
422 * we should do bottom half handling etc. Right now we
423 * end up _always_ checking the bottom half, which is a
424 * waste of time and is not what some drivers would
425 * prefer.
427 int handle_IRQ_event(unsigned int irq, struct pt_regs * regs, struct irqaction * action)
429 int status;
430 int cpu = smp_processor_id();
432 irq_enter(cpu, irq);
434 status = 1; /* Force the "do bottom halves" bit */
436 if (!(action->flags & SA_INTERRUPT))
437 __sti();
439 do {
440 status |= action->flags;
441 action->handler(irq, action->dev_id, regs);
442 action = action->next;
443 } while (action);
444 if (status & SA_SAMPLE_RANDOM)
445 add_interrupt_randomness(irq);
446 __cli();
448 irq_exit(cpu, irq);
450 return status;
454 * Generic enable/disable code: this just calls
455 * down into the PIC-specific version for the actual
456 * hardware disable after having gotten the irq
457 * controller lock.
461 * disable_irq_nosync - disable an irq without waiting
462 * @irq: Interrupt to disable
464 * Disable the selected interrupt line. Disables of an interrupt
465 * stack. Unlike disable_irq(), this function does not ensure existing
466 * instances of the IRQ handler have completed before returning.
468 * This function may be called from IRQ context.
471 void inline disable_irq_nosync(unsigned int irq)
473 irq_desc_t *desc = irq_desc + irq;
474 unsigned long flags;
476 spin_lock_irqsave(&desc->lock, flags);
477 if (!desc->depth++) {
478 desc->status |= IRQ_DISABLED;
479 desc->handler->disable(irq);
481 spin_unlock_irqrestore(&desc->lock, flags);
485 * disable_irq - disable an irq and wait for completion
486 * @irq: Interrupt to disable
488 * Disable the selected interrupt line. Disables of an interrupt
489 * stack. That is for two disables you need two enables. This
490 * function waits for any pending IRQ handlers for this interrupt
491 * to complete before returning. If you use this function while
492 * holding a resource the IRQ handler may need you will deadlock.
494 * This function may be called - with care - from IRQ context.
497 void disable_irq(unsigned int irq)
499 disable_irq_nosync(irq);
501 if (!local_irq_count(smp_processor_id())) {
502 do {
503 barrier();
504 } while (irq_desc[irq].status & IRQ_INPROGRESS);
509 * enable_irq - enable interrupt handling on an irq
510 * @irq: Interrupt to enable
512 * Re-enables the processing of interrupts on this IRQ line
513 * providing no disable_irq calls are now in effect.
515 * This function may be called from IRQ context.
518 void enable_irq(unsigned int irq)
520 irq_desc_t *desc = irq_desc + irq;
521 unsigned long flags;
523 spin_lock_irqsave(&desc->lock, flags);
524 switch (desc->depth) {
525 case 1: {
526 unsigned int status = desc->status & ~IRQ_DISABLED;
527 desc->status = status;
528 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
529 desc->status = status | IRQ_REPLAY;
530 hw_resend_irq(desc->handler,irq);
532 desc->handler->enable(irq);
533 /* fall-through */
535 default:
536 desc->depth--;
537 break;
538 case 0:
539 printk("enable_irq(%u) unbalanced from %p\n", irq,
540 __builtin_return_address(0));
542 spin_unlock_irqrestore(&desc->lock, flags);
546 * do_IRQ handles all normal device IRQ's (the special
547 * SMP cross-CPU interrupts have their own specific
548 * handlers).
550 asmlinkage unsigned int do_IRQ(struct pt_regs regs)
553 * We ack quickly, we don't want the irq controller
554 * thinking we're snobs just because some other CPU has
555 * disabled global interrupts (we have already done the
556 * INT_ACK cycles, it's too late to try to pretend to the
557 * controller that we aren't taking the interrupt).
559 * 0 return value means that this irq is already being
560 * handled by some other CPU. (or is disabled)
562 int irq = regs.orig_eax & 0xff; /* high bits used in ret_from_ code */
563 int cpu = smp_processor_id();
564 irq_desc_t *desc = irq_desc + irq;
565 struct irqaction * action;
566 unsigned int status;
568 kstat.irqs[cpu][irq]++;
569 spin_lock(&desc->lock);
570 desc->handler->ack(irq);
572 REPLAY is when Linux resends an IRQ that was dropped earlier
573 WAITING is used by probe to mark irqs that are being tested
575 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
576 status |= IRQ_PENDING; /* we _want_ to handle it */
579 * If the IRQ is disabled for whatever reason, we cannot
580 * use the action we have.
582 action = NULL;
583 if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
584 action = desc->action;
585 status &= ~IRQ_PENDING; /* we commit to handling */
586 status |= IRQ_INPROGRESS; /* we are handling it */
588 desc->status = status;
591 * If there is no IRQ handler or it was disabled, exit early.
592 Since we set PENDING, if another processor is handling
593 a different instance of this same irq, the other processor
594 will take care of it.
596 if (!action)
597 goto out;
600 * Edge triggered interrupts need to remember
601 * pending events.
602 * This applies to any hw interrupts that allow a second
603 * instance of the same irq to arrive while we are in do_IRQ
604 * or in the handler. But the code here only handles the _second_
605 * instance of the irq, not the third or fourth. So it is mostly
606 * useful for irq hardware that does not mask cleanly in an
607 * SMP environment.
609 for (;;) {
610 spin_unlock(&desc->lock);
611 handle_IRQ_event(irq, &regs, action);
612 spin_lock(&desc->lock);
614 if (!(desc->status & IRQ_PENDING))
615 break;
616 desc->status &= ~IRQ_PENDING;
618 desc->status &= ~IRQ_INPROGRESS;
619 out:
621 * The ->end() handler has to deal with interrupts which got
622 * disabled while the handler was running.
624 desc->handler->end(irq);
625 spin_unlock(&desc->lock);
627 if (softirq_state[cpu].active & softirq_state[cpu].mask)
628 do_softirq();
629 return 1;
633 * request_irq - allocate an interrupt line
634 * @irq: Interrupt line to allocate
635 * @handler: Function to be called when the IRQ occurs
636 * @irqflags: Interrupt type flags
637 * @devname: An ascii name for the claiming device
638 * @dev_id: A cookie passed back to the handler function
640 * This call allocates interrupt resources and enables the
641 * interrupt line and IRQ handling. From the point this
642 * call is made your handler function may be invoked. Since
643 * your handler function must clear any interrupt the board
644 * raises, you must take care both to initialise your hardware
645 * and to set up the interrupt handler in the right order.
647 * Dev_id must be globally unique. Normally the address of the
648 * device data structure is used as the cookie. Since the handler
649 * receives this value it makes sense to use it.
651 * If your interrupt is shared you must pass a non NULL dev_id
652 * as this is required when freeing the interrupt.
654 * Flags:
656 * SA_SHIRQ Interrupt is shared
658 * SA_INTERRUPT Disable local interrupts while processing
660 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
664 int request_irq(unsigned int irq,
665 void (*handler)(int, void *, struct pt_regs *),
666 unsigned long irqflags,
667 const char * devname,
668 void *dev_id)
670 int retval;
671 struct irqaction * action;
673 #if 1
675 * Sanity-check: shared interrupts should REALLY pass in
676 * a real dev-ID, otherwise we'll have trouble later trying
677 * to figure out which interrupt is which (messes up the
678 * interrupt freeing logic etc).
680 if (irqflags & SA_SHIRQ) {
681 if (!dev_id)
682 printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
684 #endif
686 if (irq >= NR_IRQS)
687 return -EINVAL;
688 if (!handler)
689 return -EINVAL;
691 action = (struct irqaction *)
692 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
693 if (!action)
694 return -ENOMEM;
696 action->handler = handler;
697 action->flags = irqflags;
698 action->mask = 0;
699 action->name = devname;
700 action->next = NULL;
701 action->dev_id = dev_id;
703 retval = setup_irq(irq, action);
704 if (retval)
705 kfree(action);
706 return retval;
710 * free_irq - free an interrupt
711 * @irq: Interrupt line to free
712 * @dev_id: Device identity to free
714 * Remove an interrupt handler. The handler is removed and if the
715 * interrupt line is no longer in use by any driver it is disabled.
716 * On a shared IRQ the caller must ensure the interrupt is disabled
717 * on the card it drives before calling this function. The function
718 * does not return until any executing interrupts for this IRQ
719 * have completed.
721 * This function may be called from interrupt context.
723 * Bugs: Attempting to free an irq in a handler for the same irq hangs
724 * the machine.
727 void free_irq(unsigned int irq, void *dev_id)
729 irq_desc_t *desc;
730 struct irqaction **p;
731 unsigned long flags;
733 if (irq >= NR_IRQS)
734 return;
736 desc = irq_desc + irq;
737 spin_lock_irqsave(&desc->lock,flags);
738 p = &desc->action;
739 for (;;) {
740 struct irqaction * action = *p;
741 if (action) {
742 struct irqaction **pp = p;
743 p = &action->next;
744 if (action->dev_id != dev_id)
745 continue;
747 /* Found it - now remove it from the list of entries */
748 *pp = action->next;
749 if (!desc->action) {
750 desc->status |= IRQ_DISABLED;
751 desc->handler->shutdown(irq);
753 spin_unlock_irqrestore(&desc->lock,flags);
755 #ifdef CONFIG_SMP
756 /* Wait to make sure it's not being used on another CPU */
757 while (desc->status & IRQ_INPROGRESS)
758 barrier();
759 #endif
760 kfree(action);
761 return;
763 printk("Trying to free free IRQ%d\n",irq);
764 spin_unlock_irqrestore(&desc->lock,flags);
765 return;
770 * IRQ autodetection code..
772 * This depends on the fact that any interrupt that
773 * comes in on to an unassigned handler will get stuck
774 * with "IRQ_WAITING" cleared and the interrupt
775 * disabled.
778 static DECLARE_MUTEX(probe_sem);
781 * probe_irq_on - begin an interrupt autodetect
783 * Commence probing for an interrupt. The interrupts are scanned
784 * and a mask of potential interrupt lines is returned.
788 unsigned long probe_irq_on(void)
790 unsigned int i;
791 irq_desc_t *desc;
792 unsigned long val;
793 unsigned long delay;
795 down(&probe_sem);
797 * something may have generated an irq long ago and we want to
798 * flush such a longstanding irq before considering it as spurious.
800 for (i = NR_IRQS-1; i > 0; i--) {
801 desc = irq_desc + i;
803 spin_lock_irq(&desc->lock);
804 if (!irq_desc[i].action)
805 irq_desc[i].handler->startup(i);
806 spin_unlock_irq(&desc->lock);
809 /* Wait for longstanding interrupts to trigger. */
810 for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
811 /* about 20ms delay */ synchronize_irq();
814 * enable any unassigned irqs
815 * (we must startup again here because if a longstanding irq
816 * happened in the previous stage, it may have masked itself)
818 for (i = NR_IRQS-1; i > 0; i--) {
819 desc = irq_desc + i;
821 spin_lock_irq(&desc->lock);
822 if (!desc->action) {
823 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
824 if (desc->handler->startup(i))
825 desc->status |= IRQ_PENDING;
827 spin_unlock_irq(&desc->lock);
831 * Wait for spurious interrupts to trigger
833 for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
834 /* about 100ms delay */ synchronize_irq();
837 * Now filter out any obviously spurious interrupts
839 val = 0;
840 for (i = 0; i < NR_IRQS; i++) {
841 irq_desc_t *desc = irq_desc + i;
842 unsigned int status;
844 spin_lock_irq(&desc->lock);
845 status = desc->status;
847 if (status & IRQ_AUTODETECT) {
848 /* It triggered already - consider it spurious. */
849 if (!(status & IRQ_WAITING)) {
850 desc->status = status & ~IRQ_AUTODETECT;
851 desc->handler->shutdown(i);
852 } else
853 if (i < 32)
854 val |= 1 << i;
856 spin_unlock_irq(&desc->lock);
859 return val;
863 * Return a mask of triggered interrupts (this
864 * can handle only legacy ISA interrupts).
868 * probe_irq_mask - scan a bitmap of interrupt lines
869 * @val: mask of interrupts to consider
871 * Scan the ISA bus interrupt lines and return a bitmap of
872 * active interrupts. The interrupt probe logic state is then
873 * returned to its previous value.
875 * Note: we need to scan all the irq's even though we will
876 * only return ISA irq numbers - just so that we reset them
877 * all to a known state.
879 unsigned int probe_irq_mask(unsigned long val)
881 int i;
882 unsigned int mask;
884 mask = 0;
885 for (i = 0; i < NR_IRQS; i++) {
886 irq_desc_t *desc = irq_desc + i;
887 unsigned int status;
889 spin_lock_irq(&desc->lock);
890 status = desc->status;
892 if (status & IRQ_AUTODETECT) {
893 if (i < 16 && !(status & IRQ_WAITING))
894 mask |= 1 << i;
896 desc->status = status & ~IRQ_AUTODETECT;
897 desc->handler->shutdown(i);
899 spin_unlock_irq(&desc->lock);
901 up(&probe_sem);
903 return mask & val;
907 * Return the one interrupt that triggered (this can
908 * handle any interrupt source).
912 * probe_irq_off - end an interrupt autodetect
913 * @val: mask of potential interrupts (unused)
915 * Scans the unused interrupt lines and returns the line which
916 * appears to have triggered the interrupt. If no interrupt was
917 * found then zero is returned. If more than one interrupt is
918 * found then minus the first candidate is returned to indicate
919 * their is doubt.
921 * The interrupt probe logic state is returned to its previous
922 * value.
924 * BUGS: When used in a module (which arguably shouldnt happen)
925 * nothing prevents two IRQ probe callers from overlapping. The
926 * results of this are non-optimal.
929 int probe_irq_off(unsigned long val)
931 int i, irq_found, nr_irqs;
933 nr_irqs = 0;
934 irq_found = 0;
935 for (i = 0; i < NR_IRQS; i++) {
936 irq_desc_t *desc = irq_desc + i;
937 unsigned int status;
939 spin_lock_irq(&desc->lock);
940 status = desc->status;
942 if (status & IRQ_AUTODETECT) {
943 if (!(status & IRQ_WAITING)) {
944 if (!nr_irqs)
945 irq_found = i;
946 nr_irqs++;
948 desc->status = status & ~IRQ_AUTODETECT;
949 desc->handler->shutdown(i);
951 spin_unlock_irq(&desc->lock);
953 up(&probe_sem);
955 if (nr_irqs > 1)
956 irq_found = -irq_found;
957 return irq_found;
960 /* this was setup_x86_irq but it seems pretty generic */
961 int setup_irq(unsigned int irq, struct irqaction * new)
963 int shared = 0;
964 unsigned long flags;
965 struct irqaction *old, **p;
966 irq_desc_t *desc = irq_desc + irq;
969 * Some drivers like serial.c use request_irq() heavily,
970 * so we have to be careful not to interfere with a
971 * running system.
973 if (new->flags & SA_SAMPLE_RANDOM) {
975 * This function might sleep, we want to call it first,
976 * outside of the atomic block.
977 * Yes, this might clear the entropy pool if the wrong
978 * driver is attempted to be loaded, without actually
979 * installing a new handler, but is this really a problem,
980 * only the sysadmin is able to do this.
982 rand_initialize_irq(irq);
986 * The following block of code has to be executed atomically
988 spin_lock_irqsave(&desc->lock,flags);
989 p = &desc->action;
990 if ((old = *p) != NULL) {
991 /* Can't share interrupts unless both agree to */
992 if (!(old->flags & new->flags & SA_SHIRQ)) {
993 spin_unlock_irqrestore(&desc->lock,flags);
994 return -EBUSY;
997 /* add new interrupt at end of irq queue */
998 do {
999 p = &old->next;
1000 old = *p;
1001 } while (old);
1002 shared = 1;
1005 *p = new;
1007 if (!shared) {
1008 desc->depth = 0;
1009 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING);
1010 desc->handler->startup(irq);
1012 spin_unlock_irqrestore(&desc->lock,flags);
1014 register_irq_proc(irq);
1015 return 0;
1018 static struct proc_dir_entry * root_irq_dir;
1019 static struct proc_dir_entry * irq_dir [NR_IRQS];
1020 static struct proc_dir_entry * smp_affinity_entry [NR_IRQS];
1022 static unsigned long irq_affinity [NR_IRQS] = { [0 ... NR_IRQS-1] = ~0UL };
1024 #define HEX_DIGITS 8
1026 static int irq_affinity_read_proc (char *page, char **start, off_t off,
1027 int count, int *eof, void *data)
1029 if (count < HEX_DIGITS+1)
1030 return -EINVAL;
1031 return sprintf (page, "%08lx\n", irq_affinity[(long)data]);
1034 static unsigned int parse_hex_value (const char *buffer,
1035 unsigned long count, unsigned long *ret)
1037 unsigned char hexnum [HEX_DIGITS];
1038 unsigned long value;
1039 int i;
1041 if (!count)
1042 return -EINVAL;
1043 if (count > HEX_DIGITS)
1044 count = HEX_DIGITS;
1045 if (copy_from_user(hexnum, buffer, count))
1046 return -EFAULT;
1049 * Parse the first 8 characters as a hex string, any non-hex char
1050 * is end-of-string. '00e1', 'e1', '00E1', 'E1' are all the same.
1052 value = 0;
1054 for (i = 0; i < count; i++) {
1055 unsigned int c = hexnum[i];
1057 switch (c) {
1058 case '0' ... '9': c -= '0'; break;
1059 case 'a' ... 'f': c -= 'a'-10; break;
1060 case 'A' ... 'F': c -= 'A'-10; break;
1061 default:
1062 goto out;
1064 value = (value << 4) | c;
1066 out:
1067 *ret = value;
1068 return 0;
1071 static int irq_affinity_write_proc (struct file *file, const char *buffer,
1072 unsigned long count, void *data)
1074 int irq = (long) data, full_count = count, err;
1075 unsigned long new_value;
1077 if (!irq_desc[irq].handler->set_affinity)
1078 return -EIO;
1080 err = parse_hex_value(buffer, count, &new_value);
1082 #if CONFIG_SMP
1084 * Do not allow disabling IRQs completely - it's a too easy
1085 * way to make the system unusable accidentally :-) At least
1086 * one online CPU still has to be targeted.
1088 if (!(new_value & cpu_online_map))
1089 return -EINVAL;
1090 #endif
1092 irq_affinity[irq] = new_value;
1093 irq_desc[irq].handler->set_affinity(irq, new_value);
1095 return full_count;
1098 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
1099 int count, int *eof, void *data)
1101 unsigned long *mask = (unsigned long *) data;
1102 if (count < HEX_DIGITS+1)
1103 return -EINVAL;
1104 return sprintf (page, "%08lx\n", *mask);
1107 static int prof_cpu_mask_write_proc (struct file *file, const char *buffer,
1108 unsigned long count, void *data)
1110 unsigned long *mask = (unsigned long *) data, full_count = count, err;
1111 unsigned long new_value;
1113 err = parse_hex_value(buffer, count, &new_value);
1114 if (err)
1115 return err;
1117 *mask = new_value;
1118 return full_count;
1121 #define MAX_NAMELEN 10
1123 static void register_irq_proc (unsigned int irq)
1125 struct proc_dir_entry *entry;
1126 char name [MAX_NAMELEN];
1128 if (!root_irq_dir || (irq_desc[irq].handler == &no_irq_type) ||
1129 irq_dir[irq])
1130 return;
1132 memset(name, 0, MAX_NAMELEN);
1133 sprintf(name, "%d", irq);
1135 /* create /proc/irq/1234 */
1136 irq_dir[irq] = proc_mkdir(name, root_irq_dir);
1138 /* create /proc/irq/1234/smp_affinity */
1139 entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
1141 entry->nlink = 1;
1142 entry->data = (void *)(long)irq;
1143 entry->read_proc = irq_affinity_read_proc;
1144 entry->write_proc = irq_affinity_write_proc;
1146 smp_affinity_entry[irq] = entry;
1149 unsigned long prof_cpu_mask = -1;
1151 void init_irq_proc (void)
1153 struct proc_dir_entry *entry;
1154 int i;
1156 /* create /proc/irq */
1157 root_irq_dir = proc_mkdir("irq", 0);
1159 /* create /proc/irq/prof_cpu_mask */
1160 entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
1162 entry->nlink = 1;
1163 entry->data = (void *)&prof_cpu_mask;
1164 entry->read_proc = prof_cpu_mask_read_proc;
1165 entry->write_proc = prof_cpu_mask_write_proc;
1168 * Create entries for all existing IRQs.
1170 for (i = 0; i < NR_IRQS; i++)
1171 register_irq_proc(i);