[PATCH] x86-64 architecture specific sync for 2.5.8
[linux-2.6/history.git] / arch / x86_64 / kernel / irq.c
blob623d09946c0f1bf7869063080f10e7ce22c756e6
1 /*
2 * linux/arch/x86_64/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/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 #include <linux/seq_file.h>
37 #include <asm/atomic.h>
38 #include <asm/io.h>
39 #include <asm/smp.h>
40 #include <asm/system.h>
41 #include <asm/bitops.h>
42 #include <asm/uaccess.h>
43 #include <asm/pgalloc.h>
44 #include <asm/delay.h>
45 #include <asm/desc.h>
46 #include <asm/irq.h>
51 * Linux has a controller-independent x86 interrupt architecture.
52 * every controller has a 'controller-template', that is used
53 * by the main code to do the right thing. Each driver-visible
54 * interrupt source is transparently wired to the apropriate
55 * controller. Thus drivers need not be aware of the
56 * interrupt-controller.
58 * Various interrupt controllers we handle: 8259 PIC, SMP IO-APIC,
59 * PIIX4's internal 8259 PIC and SGI's Visual Workstation Cobalt (IO-)APIC.
60 * (IO-APICs assumed to be messaging to Pentium local-APICs)
62 * the code is designed to be easily extended with new/different
63 * interrupt controllers, without having to do assembly magic.
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 atomic_t irq_err_count;
125 #ifdef CONFIG_X86_IO_APIC
126 #ifdef APIC_MISMATCH_DEBUG
127 atomic_t irq_mis_count;
128 #endif
129 #endif
132 * Generic, controller-independent functions:
135 int show_interrupts(struct seq_file *p, void *v)
137 int i, j;
138 struct irqaction * action;
140 seq_printf(p, " ");
141 for (j=0; j<smp_num_cpus; j++)
142 seq_printf(p, "CPU%d ",j);
143 seq_putc(p, '\n');
145 for (i = 0 ; i < NR_IRQS ; i++) {
146 action = irq_desc[i].action;
147 if (!action)
148 continue;
149 seq_printf(p, "%3d: ",i);
150 #ifndef CONFIG_SMP
151 seq_printf(p, "%10u ", kstat_irqs(i));
152 #else
153 for (j = 0; j < smp_num_cpus; j++)
154 seq_printf(p, "%10u ",
155 kstat.irqs[cpu_logical_map(j)][i]);
156 #endif
157 seq_printf(p, " %14s", irq_desc[i].handler->typename);
158 seq_printf(p, " %s", action->name);
160 for (action=action->next; action; action = action->next)
161 seq_printf(p, ", %s", action->name);
162 seq_putc(p, '\n');
164 seq_printf(p, "NMI: ");
165 for (j = 0; j < smp_num_cpus; j++)
166 seq_printf(p, "%10u ", nmi_count(cpu_logical_map(j)));
167 seq_putc(p, '\n');
168 #if CONFIG_X86_LOCAL_APIC
169 seq_printf(p, "LOC: ");
170 for (j = 0; j < smp_num_cpus; j++)
171 seq_printf(p, "%10u ", apic_timer_irqs[cpu_logical_map(j)]);
172 seq_putc(p, '\n');
173 #endif
174 seq_printf(p, "ERR: %10u\n", atomic_read(&irq_err_count));
175 #ifdef CONFIG_X86_IO_APIC
176 #ifdef APIC_MISMATCH_DEBUG
177 seq_printf(p, "MIS: %10u\n", atomic_read(&irq_mis_count));
178 #endif
179 #endif
180 return 0;
184 * Global interrupt locks for SMP. Allow interrupts to come in on any
185 * CPU, yet make cli/sti act globally to protect critical regions..
188 #ifdef CONFIG_SMP
189 unsigned char global_irq_holder = NO_PROC_ID;
190 unsigned volatile long global_irq_lock; /* pendantic: long for set_bit --RR */
192 extern void show_stack(unsigned long* esp);
195 /* XXX: this unfortunately doesn't support irqstacks currently, should check the other PDAs */
196 static void show(char * str)
198 int i;
199 int cpu = smp_processor_id();
201 printk("\n%s, CPU %d:\n", str, cpu);
202 printk("irq: %d [",irqs_running());
203 for(i=0;i < smp_num_cpus;i++)
204 printk(" %d",local_irq_count(i));
205 printk(" ]\nbh: %d [",spin_is_locked(&global_bh_lock) ? 1 : 0);
206 for(i=0;i < smp_num_cpus;i++)
207 printk(" %d",local_bh_count(i));
209 printk(" ]\nStack dumps:");
210 for(i = 0; i < smp_num_cpus; i++) {
211 unsigned long esp;
212 if (i == cpu)
213 continue;
214 printk("\nCPU %d:",i);
215 esp = init_tss[i].rsp0;
216 if (!esp) {
217 /* tss->esp0 is set to NULL in cpu_init(),
218 * it's initialized when the cpu returns to user
219 * space. -- manfreds
221 printk(" <unknown> ");
222 continue;
224 esp &= ~(THREAD_SIZE-1);
225 esp += sizeof(struct thread_info);
226 show_stack((void*)esp);
228 printk("\nCPU %d:",cpu);
229 show_stack(NULL);
230 printk("\n");
233 #define MAXCOUNT 100000000
236 * I had a lockup scenario where a tight loop doing
237 * spin_unlock()/spin_lock() on CPU#1 was racing with
238 * spin_lock() on CPU#0. CPU#0 should have noticed spin_unlock(), but
239 * apparently the spin_unlock() information did not make it
240 * through to CPU#0 ... nasty, is this by design, do we have to limit
241 * 'memory update oscillation frequency' artificially like here?
243 * Such 'high frequency update' races can be avoided by careful design, but
244 * some of our major constructs like spinlocks use similar techniques,
245 * it would be nice to clarify this issue. Set this define to 0 if you
246 * want to check whether your system freezes. I suspect the delay done
247 * by SYNC_OTHER_CORES() is in correlation with 'snooping latency', but
248 * i thought that such things are guaranteed by design, since we use
249 * the 'LOCK' prefix.
251 #define SUSPECTED_CPU_OR_CHIPSET_BUG_WORKAROUND 0
253 #if SUSPECTED_CPU_OR_CHIPSET_BUG_WORKAROUND
254 # define SYNC_OTHER_CORES(x) udelay(x+1)
255 #else
257 * We have to allow irqs to arrive between __sti and __cli
259 # define SYNC_OTHER_CORES(x) __asm__ __volatile__ ("nop")
260 #endif
262 static inline void wait_on_irq(int cpu)
264 int count = MAXCOUNT;
266 for (;;) {
269 * Wait until all interrupts are gone. Wait
270 * for bottom half handlers unless we're
271 * already executing in one..
273 if (!irqs_running())
274 if (local_bh_count(cpu) || !spin_is_locked(&global_bh_lock))
275 break;
277 /* Duh, we have to loop. Release the lock to avoid deadlocks */
278 clear_bit(0,&global_irq_lock);
280 for (;;) {
281 if (!--count) {
282 show("wait_on_irq");
283 count = ~0;
285 __sti();
286 SYNC_OTHER_CORES(cpu);
287 __cli();
288 if (irqs_running())
289 continue;
290 if (global_irq_lock)
291 continue;
292 if (!local_bh_count(cpu) && spin_is_locked(&global_bh_lock))
293 continue;
294 if (!test_and_set_bit(0,&global_irq_lock))
295 break;
301 * This is called when we want to synchronize with
302 * interrupts. We may for example tell a device to
303 * stop sending interrupts: but to make sure there
304 * are no interrupts that are executing on another
305 * CPU we need to call this function.
307 void synchronize_irq(void)
309 if (irqs_running()) {
310 /* Stupid approach */
311 cli();
312 sti();
316 static inline void get_irqlock(int cpu)
318 if (test_and_set_bit(0,&global_irq_lock)) {
319 /* do we already hold the lock? */
320 if ((unsigned char) cpu == global_irq_holder)
321 return;
322 /* Uhhuh.. Somebody else got it. Wait.. */
323 do {
324 do {
325 rep_nop();
326 } while (test_bit(0,&global_irq_lock));
327 } while (test_and_set_bit(0,&global_irq_lock));
330 * We also to make sure that nobody else is running
331 * in an interrupt context.
333 wait_on_irq(cpu);
336 * Ok, finally..
338 global_irq_holder = cpu;
341 #define EFLAGS_IF_SHIFT 9
344 * A global "cli()" while in an interrupt context
345 * turns into just a local cli(). Interrupts
346 * should use spinlocks for the (very unlikely)
347 * case that they ever want to protect against
348 * each other.
350 * If we already have local interrupts disabled,
351 * this will not turn a local disable into a
352 * global one (problems with spinlocks: this makes
353 * save_flags+cli+sti usable inside a spinlock).
355 void __global_cli(void)
357 unsigned long flags;
359 __save_flags(flags);
360 if (flags & (1 << EFLAGS_IF_SHIFT)) {
361 int cpu = smp_processor_id();
362 __cli();
363 if (!local_irq_count(cpu))
364 get_irqlock(cpu);
368 void __global_sti(void)
370 int cpu = smp_processor_id();
372 if (!local_irq_count(cpu))
373 release_irqlock(cpu);
374 __sti();
378 * SMP flags value to restore to:
379 * 0 - global cli
380 * 1 - global sti
381 * 2 - local cli
382 * 3 - local sti
384 unsigned long __global_save_flags(void)
386 int retval;
387 int local_enabled;
388 unsigned long flags;
389 int cpu = smp_processor_id();
391 __save_flags(flags);
392 local_enabled = (flags >> EFLAGS_IF_SHIFT) & 1;
393 /* default to local */
394 retval = 2 + local_enabled;
396 /* check for global flags if we're not in an interrupt */
397 if (!local_irq_count(cpu)) {
398 if (local_enabled)
399 retval = 1;
400 if (global_irq_holder == cpu)
401 retval = 0;
403 return retval;
406 void __global_restore_flags(unsigned long flags)
408 switch (flags) {
409 case 0:
410 __global_cli();
411 break;
412 case 1:
413 __global_sti();
414 break;
415 case 2:
416 __cli();
417 break;
418 case 3:
419 __sti();
420 break;
421 default:
422 printk("global_restore_flags: %08lx (%08lx)\n",
423 flags, (&flags)[-1]);
427 #endif
430 * This should really return information about whether
431 * we should do bottom half handling etc. Right now we
432 * end up _always_ checking the bottom half, which is a
433 * waste of time and is not what some drivers would
434 * prefer.
436 int handle_IRQ_event(unsigned int irq, struct pt_regs * regs, struct irqaction * action)
438 int status;
440 irq_enter(0, irq);
442 status = 1; /* Force the "do bottom halves" bit */
444 if (!(action->flags & SA_INTERRUPT))
445 __sti();
447 do {
448 status |= action->flags;
449 action->handler(irq, action->dev_id, regs);
450 action = action->next;
451 } while (action);
452 if (status & SA_SAMPLE_RANDOM)
453 add_interrupt_randomness(irq);
454 __cli();
456 irq_exit(0, irq);
458 return status;
462 * Generic enable/disable code: this just calls
463 * down into the PIC-specific version for the actual
464 * hardware disable after having gotten the irq
465 * controller lock.
469 * disable_irq_nosync - disable an irq without waiting
470 * @irq: Interrupt to disable
472 * Disable the selected interrupt line. Disables and Enables are
473 * nested.
474 * Unlike disable_irq(), this function does not ensure existing
475 * instances of the IRQ handler have completed before returning.
477 * This function may be called from IRQ context.
480 inline void disable_irq_nosync(unsigned int irq)
482 irq_desc_t *desc = irq_desc + irq;
483 unsigned long flags;
485 spin_lock_irqsave(&desc->lock, flags);
486 if (!desc->depth++) {
487 desc->status |= IRQ_DISABLED;
488 desc->handler->disable(irq);
490 spin_unlock_irqrestore(&desc->lock, flags);
494 * disable_irq - disable an irq and wait for completion
495 * @irq: Interrupt to disable
497 * Disable the selected interrupt line. Enables and Disables are
498 * nested.
499 * This function waits for any pending IRQ handlers for this interrupt
500 * to complete before returning. If you use this function while
501 * holding a resource the IRQ handler may need you will deadlock.
503 * This function may be called - with care - from IRQ context.
506 void disable_irq(unsigned int irq)
508 disable_irq_nosync(irq);
510 if (!local_irq_count(smp_processor_id())) {
511 do {
512 barrier();
513 cpu_relax();
514 } while (irq_desc[irq].status & IRQ_INPROGRESS);
519 * enable_irq - enable handling of an irq
520 * @irq: Interrupt to enable
522 * Undoes the effect of one call to disable_irq(). If this
523 * matches the last disable, processing of interrupts on this
524 * IRQ line is re-enabled.
526 * This function may be called from IRQ context.
529 void enable_irq(unsigned int irq)
531 irq_desc_t *desc = irq_desc + irq;
532 unsigned long flags;
534 spin_lock_irqsave(&desc->lock, flags);
535 switch (desc->depth) {
536 case 1: {
537 unsigned int status = desc->status & ~IRQ_DISABLED;
538 desc->status = status;
539 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
540 desc->status = status | IRQ_REPLAY;
541 hw_resend_irq(desc->handler,irq);
543 desc->handler->enable(irq);
544 /* fall-through */
546 default:
547 desc->depth--;
548 break;
549 case 0:
550 printk("enable_irq(%u) unbalanced from %p\n", irq,
551 __builtin_return_address(0));
553 spin_unlock_irqrestore(&desc->lock, flags);
557 * do_IRQ handles all normal device IRQ's (the special
558 * SMP cross-CPU interrupts have their own specific
559 * handlers).
561 asmlinkage unsigned int do_IRQ(struct pt_regs *regs)
564 * We ack quickly, we don't want the irq controller
565 * thinking we're snobs just because some other CPU has
566 * disabled global interrupts (we have already done the
567 * INT_ACK cycles, it's too late to try to pretend to the
568 * controller that we aren't taking the interrupt).
570 * 0 return value means that this irq is already being
571 * handled by some other CPU. (or is disabled)
573 unsigned irq = regs->orig_rax & 0xff; /* high bits used in ret_from_ code */
574 int cpu = smp_processor_id();
575 irq_desc_t *desc = irq_desc + irq;
576 struct irqaction * action;
577 unsigned int status;
579 if (irq > 256) BUG();
581 kstat.irqs[cpu][irq]++;
582 spin_lock(&desc->lock);
583 desc->handler->ack(irq);
585 REPLAY is when Linux resends an IRQ that was dropped earlier
586 WAITING is used by probe to mark irqs that are being tested
588 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
589 status |= IRQ_PENDING; /* we _want_ to handle it */
592 * If the IRQ is disabled for whatever reason, we cannot
593 * use the action we have.
595 action = NULL;
596 if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
597 action = desc->action;
598 status &= ~IRQ_PENDING; /* we commit to handling */
599 status |= IRQ_INPROGRESS; /* we are handling it */
601 desc->status = status;
604 * If there is no IRQ handler or it was disabled, exit early.
605 Since we set PENDING, if another processor is handling
606 a different instance of this same irq, the other processor
607 will take care of it.
609 if (!action)
610 goto out;
613 * Edge triggered interrupts need to remember
614 * pending events.
615 * This applies to any hw interrupts that allow a second
616 * instance of the same irq to arrive while we are in do_IRQ
617 * or in the handler. But the code here only handles the _second_
618 * instance of the irq, not the third or fourth. So it is mostly
619 * useful for irq hardware that does not mask cleanly in an
620 * SMP environment.
622 for (;;) {
623 spin_unlock(&desc->lock);
624 handle_IRQ_event(irq, regs, action);
625 spin_lock(&desc->lock);
627 if (!(desc->status & IRQ_PENDING))
628 break;
629 desc->status &= ~IRQ_PENDING;
631 desc->status &= ~IRQ_INPROGRESS;
632 out:
634 * The ->end() handler has to deal with interrupts which got
635 * disabled while the handler was running.
637 if (irq > 256) BUG();
638 desc->handler->end(irq);
639 spin_unlock(&desc->lock);
641 if (softirq_pending(cpu))
642 do_softirq();
643 return 1;
647 * request_irq - allocate an interrupt line
648 * @irq: Interrupt line to allocate
649 * @handler: Function to be called when the IRQ occurs
650 * @irqflags: Interrupt type flags
651 * @devname: An ascii name for the claiming device
652 * @dev_id: A cookie passed back to the handler function
654 * This call allocates interrupt resources and enables the
655 * interrupt line and IRQ handling. From the point this
656 * call is made your handler function may be invoked. Since
657 * your handler function must clear any interrupt the board
658 * raises, you must take care both to initialise your hardware
659 * and to set up the interrupt handler in the right order.
661 * Dev_id must be globally unique. Normally the address of the
662 * device data structure is used as the cookie. Since the handler
663 * receives this value it makes sense to use it.
665 * If your interrupt is shared you must pass a non NULL dev_id
666 * as this is required when freeing the interrupt.
668 * Flags:
670 * SA_SHIRQ Interrupt is shared
672 * SA_INTERRUPT Disable local interrupts while processing
674 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
678 int request_irq(unsigned int irq,
679 void (*handler)(int, void *, struct pt_regs *),
680 unsigned long irqflags,
681 const char * devname,
682 void *dev_id)
684 int retval;
685 struct irqaction * action;
687 #if 1
689 * Sanity-check: shared interrupts should REALLY pass in
690 * a real dev-ID, otherwise we'll have trouble later trying
691 * to figure out which interrupt is which (messes up the
692 * interrupt freeing logic etc).
694 if (irqflags & SA_SHIRQ) {
695 if (!dev_id)
696 printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
698 #endif
700 if (irq >= NR_IRQS)
701 return -EINVAL;
702 if (!handler)
703 return -EINVAL;
705 action = (struct irqaction *)
706 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
707 if (!action)
708 return -ENOMEM;
710 action->handler = handler;
711 action->flags = irqflags;
712 action->mask = 0;
713 action->name = devname;
714 action->next = NULL;
715 action->dev_id = dev_id;
717 retval = setup_irq(irq, action);
718 if (retval)
719 kfree(action);
720 return retval;
724 * free_irq - free an interrupt
725 * @irq: Interrupt line to free
726 * @dev_id: Device identity to free
728 * Remove an interrupt handler. The handler is removed and if the
729 * interrupt line is no longer in use by any driver it is disabled.
730 * On a shared IRQ the caller must ensure the interrupt is disabled
731 * on the card it drives before calling this function. The function
732 * does not return until any executing interrupts for this IRQ
733 * have completed.
735 * This function may be called from interrupt context.
737 * Bugs: Attempting to free an irq in a handler for the same irq hangs
738 * the machine.
741 void free_irq(unsigned int irq, void *dev_id)
743 irq_desc_t *desc;
744 struct irqaction **p;
745 unsigned long flags;
747 if (irq >= NR_IRQS)
748 return;
750 desc = irq_desc + irq;
751 spin_lock_irqsave(&desc->lock,flags);
752 p = &desc->action;
753 for (;;) {
754 struct irqaction * action = *p;
755 if (action) {
756 struct irqaction **pp = p;
757 p = &action->next;
758 if (action->dev_id != dev_id)
759 continue;
761 /* Found it - now remove it from the list of entries */
762 *pp = action->next;
763 if (!desc->action) {
764 desc->status |= IRQ_DISABLED;
765 desc->handler->shutdown(irq);
767 spin_unlock_irqrestore(&desc->lock,flags);
769 #ifdef CONFIG_SMP
770 /* Wait to make sure it's not being used on another CPU */
771 while (desc->status & IRQ_INPROGRESS) {
772 barrier();
773 cpu_relax();
775 #endif
776 kfree(action);
777 return;
779 printk("Trying to free free IRQ%d\n",irq);
780 spin_unlock_irqrestore(&desc->lock,flags);
781 return;
786 * IRQ autodetection code..
788 * This depends on the fact that any interrupt that
789 * comes in on to an unassigned handler will get stuck
790 * with "IRQ_WAITING" cleared and the interrupt
791 * disabled.
794 static DECLARE_MUTEX(probe_sem);
797 * probe_irq_on - begin an interrupt autodetect
799 * Commence probing for an interrupt. The interrupts are scanned
800 * and a mask of potential interrupt lines is returned.
804 unsigned long probe_irq_on(void)
806 unsigned int i;
807 irq_desc_t *desc;
808 unsigned long val;
809 unsigned long delay;
811 down(&probe_sem);
813 * something may have generated an irq long ago and we want to
814 * flush such a longstanding irq before considering it as spurious.
816 for (i = NR_IRQS-1; i > 0; i--) {
817 desc = irq_desc + i;
819 spin_lock_irq(&desc->lock);
820 if (!irq_desc[i].action)
821 irq_desc[i].handler->startup(i);
822 spin_unlock_irq(&desc->lock);
825 /* Wait for longstanding interrupts to trigger. */
826 for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
827 /* about 20ms delay */ synchronize_irq();
830 * enable any unassigned irqs
831 * (we must startup again here because if a longstanding irq
832 * happened in the previous stage, it may have masked itself)
834 for (i = NR_IRQS-1; i > 0; i--) {
835 desc = irq_desc + i;
837 spin_lock_irq(&desc->lock);
838 if (!desc->action) {
839 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
840 if (desc->handler->startup(i))
841 desc->status |= IRQ_PENDING;
843 spin_unlock_irq(&desc->lock);
847 * Wait for spurious interrupts to trigger
849 for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
850 /* about 100ms delay */ synchronize_irq();
853 * Now filter out any obviously spurious interrupts
855 val = 0;
856 for (i = 0; i < NR_IRQS; i++) {
857 irq_desc_t *desc = irq_desc + i;
858 unsigned int status;
860 spin_lock_irq(&desc->lock);
861 status = desc->status;
863 if (status & IRQ_AUTODETECT) {
864 /* It triggered already - consider it spurious. */
865 if (!(status & IRQ_WAITING)) {
866 desc->status = status & ~IRQ_AUTODETECT;
867 desc->handler->shutdown(i);
868 } else
869 if (i < 32)
870 val |= 1 << i;
872 spin_unlock_irq(&desc->lock);
875 return val;
879 * Return a mask of triggered interrupts (this
880 * can handle only legacy ISA interrupts).
884 * probe_irq_mask - scan a bitmap of interrupt lines
885 * @val: mask of interrupts to consider
887 * Scan the ISA bus interrupt lines and return a bitmap of
888 * active interrupts. The interrupt probe logic state is then
889 * returned to its previous value.
891 * Note: we need to scan all the irq's even though we will
892 * only return ISA irq numbers - just so that we reset them
893 * all to a known state.
895 unsigned int probe_irq_mask(unsigned long val)
897 int i;
898 unsigned int mask;
900 mask = 0;
901 for (i = 0; i < NR_IRQS; i++) {
902 irq_desc_t *desc = irq_desc + i;
903 unsigned int status;
905 spin_lock_irq(&desc->lock);
906 status = desc->status;
908 if (status & IRQ_AUTODETECT) {
909 if (i < 16 && !(status & IRQ_WAITING))
910 mask |= 1 << i;
912 desc->status = status & ~IRQ_AUTODETECT;
913 desc->handler->shutdown(i);
915 spin_unlock_irq(&desc->lock);
917 up(&probe_sem);
919 return mask & val;
923 * Return the one interrupt that triggered (this can
924 * handle any interrupt source).
928 * probe_irq_off - end an interrupt autodetect
929 * @val: mask of potential interrupts (unused)
931 * Scans the unused interrupt lines and returns the line which
932 * appears to have triggered the interrupt. If no interrupt was
933 * found then zero is returned. If more than one interrupt is
934 * found then minus the first candidate is returned to indicate
935 * their is doubt.
937 * The interrupt probe logic state is returned to its previous
938 * value.
940 * BUGS: When used in a module (which arguably shouldnt happen)
941 * nothing prevents two IRQ probe callers from overlapping. The
942 * results of this are non-optimal.
945 int probe_irq_off(unsigned long val)
947 int i, irq_found, nr_irqs;
949 nr_irqs = 0;
950 irq_found = 0;
951 for (i = 0; i < NR_IRQS; i++) {
952 irq_desc_t *desc = irq_desc + i;
953 unsigned int status;
955 spin_lock_irq(&desc->lock);
956 status = desc->status;
958 if (status & IRQ_AUTODETECT) {
959 if (!(status & IRQ_WAITING)) {
960 if (!nr_irqs)
961 irq_found = i;
962 nr_irqs++;
964 desc->status = status & ~IRQ_AUTODETECT;
965 desc->handler->shutdown(i);
967 spin_unlock_irq(&desc->lock);
969 up(&probe_sem);
971 if (nr_irqs > 1)
972 irq_found = -irq_found;
973 return irq_found;
976 /* this was setup_x86_irq but it seems pretty generic */
977 int setup_irq(unsigned int irq, struct irqaction * new)
979 int shared = 0;
980 unsigned long flags;
981 struct irqaction *old, **p;
982 irq_desc_t *desc = irq_desc + irq;
985 * Some drivers like serial.c use request_irq() heavily,
986 * so we have to be careful not to interfere with a
987 * running system.
989 if (new->flags & SA_SAMPLE_RANDOM) {
991 * This function might sleep, we want to call it first,
992 * outside of the atomic block.
993 * Yes, this might clear the entropy pool if the wrong
994 * driver is attempted to be loaded, without actually
995 * installing a new handler, but is this really a problem,
996 * only the sysadmin is able to do this.
998 rand_initialize_irq(irq);
1002 * The following block of code has to be executed atomically
1004 spin_lock_irqsave(&desc->lock,flags);
1005 p = &desc->action;
1006 if ((old = *p) != NULL) {
1007 /* Can't share interrupts unless both agree to */
1008 if (!(old->flags & new->flags & SA_SHIRQ)) {
1009 spin_unlock_irqrestore(&desc->lock,flags);
1010 return -EBUSY;
1013 /* add new interrupt at end of irq queue */
1014 do {
1015 p = &old->next;
1016 old = *p;
1017 } while (old);
1018 shared = 1;
1021 *p = new;
1023 if (!shared) {
1024 desc->depth = 0;
1025 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING);
1026 desc->handler->startup(irq);
1028 spin_unlock_irqrestore(&desc->lock,flags);
1030 register_irq_proc(irq);
1031 return 0;
1034 static struct proc_dir_entry * root_irq_dir;
1035 static struct proc_dir_entry * irq_dir [NR_IRQS];
1037 #define HEX_DIGITS 8
1039 static unsigned int parse_hex_value (const char *buffer,
1040 unsigned long count, unsigned long *ret)
1042 unsigned char hexnum [HEX_DIGITS];
1043 unsigned long value;
1044 int i;
1046 if (!count)
1047 return -EINVAL;
1048 if (count > HEX_DIGITS)
1049 count = HEX_DIGITS;
1050 if (copy_from_user(hexnum, buffer, count))
1051 return -EFAULT;
1054 * Parse the first 8 characters as a hex string, any non-hex char
1055 * is end-of-string. '00e1', 'e1', '00E1', 'E1' are all the same.
1057 value = 0;
1059 for (i = 0; i < count; i++) {
1060 unsigned int c = hexnum[i];
1062 switch (c) {
1063 case '0' ... '9': c -= '0'; break;
1064 case 'a' ... 'f': c -= 'a'-10; break;
1065 case 'A' ... 'F': c -= 'A'-10; break;
1066 default:
1067 goto out;
1069 value = (value << 4) | c;
1071 out:
1072 *ret = value;
1073 return 0;
1076 #if CONFIG_SMP
1078 static struct proc_dir_entry * smp_affinity_entry [NR_IRQS];
1080 static unsigned long irq_affinity [NR_IRQS] = { [0 ... NR_IRQS-1] = ~0UL };
1081 static int irq_affinity_read_proc (char *page, char **start, off_t off,
1082 int count, int *eof, void *data)
1084 if (count < HEX_DIGITS+1)
1085 return -EINVAL;
1086 return sprintf (page, "%08lx\n", irq_affinity[(long)data]);
1089 static int irq_affinity_write_proc (struct file *file, const char *buffer,
1090 unsigned long count, void *data)
1092 int irq = (long) data, full_count = count, err;
1093 unsigned long new_value;
1095 if (!irq_desc[irq].handler->set_affinity)
1096 return -EIO;
1098 err = parse_hex_value(buffer, count, &new_value);
1101 * Do not allow disabling IRQs completely - it's a too easy
1102 * way to make the system unusable accidentally :-) At least
1103 * one online CPU still has to be targeted.
1105 if (!(new_value & cpu_online_map))
1106 return -EINVAL;
1108 irq_affinity[irq] = new_value;
1109 irq_desc[irq].handler->set_affinity(irq, new_value);
1111 return full_count;
1114 #endif
1116 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
1117 int count, int *eof, void *data)
1119 unsigned long *mask = (unsigned long *) data;
1120 if (count < HEX_DIGITS+1)
1121 return -EINVAL;
1122 return sprintf (page, "%08lx\n", *mask);
1125 static int prof_cpu_mask_write_proc (struct file *file, const char *buffer,
1126 unsigned long count, void *data)
1128 unsigned long *mask = (unsigned long *) data, full_count = count, err;
1129 unsigned long new_value;
1131 err = parse_hex_value(buffer, count, &new_value);
1132 if (err)
1133 return err;
1135 *mask = new_value;
1136 return full_count;
1139 #define MAX_NAMELEN 10
1141 static void register_irq_proc (unsigned int irq)
1143 char name [MAX_NAMELEN];
1145 if (!root_irq_dir || (irq_desc[irq].handler == &no_irq_type) ||
1146 irq_dir[irq])
1147 return;
1149 memset(name, 0, MAX_NAMELEN);
1150 sprintf(name, "%d", irq);
1152 /* create /proc/irq/1234 */
1153 irq_dir[irq] = proc_mkdir(name, root_irq_dir);
1155 #if CONFIG_SMP
1157 struct proc_dir_entry *entry;
1159 /* create /proc/irq/1234/smp_affinity */
1160 entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
1162 if (entry) {
1163 entry->nlink = 1;
1164 entry->data = (void *)(long)irq;
1165 entry->read_proc = irq_affinity_read_proc;
1166 entry->write_proc = irq_affinity_write_proc;
1169 smp_affinity_entry[irq] = entry;
1171 #endif
1174 unsigned long prof_cpu_mask = -1;
1176 void init_irq_proc (void)
1178 struct proc_dir_entry *entry;
1179 int i;
1181 /* create /proc/irq */
1182 root_irq_dir = proc_mkdir("irq", 0);
1184 /* create /proc/irq/prof_cpu_mask */
1185 entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
1187 if (!entry)
1188 return;
1190 entry->nlink = 1;
1191 entry->data = (void *)&prof_cpu_mask;
1192 entry->read_proc = prof_cpu_mask_read_proc;
1193 entry->write_proc = prof_cpu_mask_write_proc;
1196 * Create entries for all existing IRQs.
1198 for (i = 0; i < NR_IRQS; i++)
1199 register_irq_proc(i);