- Peter Anvin: more P4 configuration parsing
[davej-history.git] / arch / i386 / kernel / irq.c
blob92ddd01f5deaa271e6e179c096ac4fd67a4e0437
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.
65 * Controller mappings for all interrupt sources:
67 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned =
68 { [0 ... NR_IRQS-1] = { 0, &no_irq_type, NULL, 0, SPIN_LOCK_UNLOCKED}};
70 static void register_irq_proc (unsigned int irq);
73 * Special irq handlers.
76 void no_action(int cpl, void *dev_id, struct pt_regs *regs) { }
79 * Generic no controller code
82 static void enable_none(unsigned int irq) { }
83 static unsigned int startup_none(unsigned int irq) { return 0; }
84 static void disable_none(unsigned int irq) { }
85 static void ack_none(unsigned int irq)
88 * 'what should we do if we get a hw irq event on an illegal vector'.
89 * each architecture has to answer this themselves, it doesnt deserve
90 * a generic callback i think.
92 #if CONFIG_X86
93 printk("unexpected IRQ trap at vector %02x\n", irq);
94 #ifdef CONFIG_X86_LOCAL_APIC
96 * Currently unexpected vectors happen only on SMP and APIC.
97 * We _must_ ack these because every local APIC has only N
98 * irq slots per priority level, and a 'hanging, unacked' IRQ
99 * holds up an irq slot - in excessive cases (when multiple
100 * unexpected vectors occur) that might lock up the APIC
101 * completely.
103 ack_APIC_irq();
104 #endif
105 #endif
108 /* startup is the same as "enable", shutdown is same as "disable" */
109 #define shutdown_none disable_none
110 #define end_none enable_none
112 struct hw_interrupt_type no_irq_type = {
113 "none",
114 startup_none,
115 shutdown_none,
116 enable_none,
117 disable_none,
118 ack_none,
119 end_none
122 volatile unsigned long irq_err_count;
125 * Generic, controller-independent functions:
128 int get_irq_list(char *buf)
130 int i, j;
131 struct irqaction * action;
132 char *p = buf;
134 p += sprintf(p, " ");
135 for (j=0; j<smp_num_cpus; j++)
136 p += sprintf(p, "CPU%d ",j);
137 *p++ = '\n';
139 for (i = 0 ; i < NR_IRQS ; i++) {
140 action = irq_desc[i].action;
141 if (!action)
142 continue;
143 p += sprintf(p, "%3d: ",i);
144 #ifndef CONFIG_SMP
145 p += sprintf(p, "%10u ", kstat_irqs(i));
146 #else
147 for (j = 0; j < smp_num_cpus; j++)
148 p += sprintf(p, "%10u ",
149 kstat.irqs[cpu_logical_map(j)][i]);
150 #endif
151 p += sprintf(p, " %14s", irq_desc[i].handler->typename);
152 p += sprintf(p, " %s", action->name);
154 for (action=action->next; action; action = action->next)
155 p += sprintf(p, ", %s", action->name);
156 *p++ = '\n';
158 p += sprintf(p, "NMI: ");
159 for (j = 0; j < smp_num_cpus; j++)
160 p += sprintf(p, "%10u ",
161 nmi_count(cpu_logical_map(j)));
162 p += sprintf(p, "\n");
163 #if CONFIG_SMP
164 p += sprintf(p, "LOC: ");
165 for (j = 0; j < smp_num_cpus; j++)
166 p += sprintf(p, "%10u ",
167 apic_timer_irqs[cpu_logical_map(j)]);
168 p += sprintf(p, "\n");
169 #endif
170 p += sprintf(p, "ERR: %10lu\n", irq_err_count);
171 return p - buf;
176 * Global interrupt locks for SMP. Allow interrupts to come in on any
177 * CPU, yet make cli/sti act globally to protect critical regions..
180 #ifdef CONFIG_SMP
181 unsigned char global_irq_holder = NO_PROC_ID;
182 unsigned volatile int global_irq_lock;
184 extern void show_stack(unsigned long* esp);
186 static void show(char * str)
188 int i;
189 int cpu = smp_processor_id();
191 printk("\n%s, CPU %d:\n", str, cpu);
192 printk("irq: %d [",irqs_running());
193 for(i=0;i < smp_num_cpus;i++)
194 printk(" %d",local_irq_count(i));
195 printk(" ]\nbh: %d [",spin_is_locked(&global_bh_lock) ? 1 : 0);
196 for(i=0;i < smp_num_cpus;i++)
197 printk(" %d",local_bh_count(i));
199 printk(" ]\nStack dumps:");
200 for(i = 0; i < smp_num_cpus; i++) {
201 unsigned long esp;
202 if (i == cpu)
203 continue;
204 printk("\nCPU %d:",i);
205 esp = init_tss[i].esp0;
206 if (!esp) {
207 /* tss->esp0 is set to NULL in cpu_init(),
208 * it's initialized when the cpu returns to user
209 * space. -- manfreds
211 printk(" <unknown> ");
212 continue;
214 esp &= ~(THREAD_SIZE-1);
215 esp += sizeof(struct task_struct);
216 show_stack((void*)esp);
218 printk("\nCPU %d:",cpu);
219 show_stack(NULL);
220 printk("\n");
223 #define MAXCOUNT 100000000
226 * I had a lockup scenario where a tight loop doing
227 * spin_unlock()/spin_lock() on CPU#1 was racing with
228 * spin_lock() on CPU#0. CPU#0 should have noticed spin_unlock(), but
229 * apparently the spin_unlock() information did not make it
230 * through to CPU#0 ... nasty, is this by design, do we have to limit
231 * 'memory update oscillation frequency' artificially like here?
233 * Such 'high frequency update' races can be avoided by careful design, but
234 * some of our major constructs like spinlocks use similar techniques,
235 * it would be nice to clarify this issue. Set this define to 0 if you
236 * want to check whether your system freezes. I suspect the delay done
237 * by SYNC_OTHER_CORES() is in correlation with 'snooping latency', but
238 * i thought that such things are guaranteed by design, since we use
239 * the 'LOCK' prefix.
241 #define SUSPECTED_CPU_OR_CHIPSET_BUG_WORKAROUND 0
243 #if SUSPECTED_CPU_OR_CHIPSET_BUG_WORKAROUND
244 # define SYNC_OTHER_CORES(x) udelay(x+1)
245 #else
247 * We have to allow irqs to arrive between __sti and __cli
249 # define SYNC_OTHER_CORES(x) __asm__ __volatile__ ("nop")
250 #endif
252 static inline void wait_on_irq(int cpu)
254 int count = MAXCOUNT;
256 for (;;) {
259 * Wait until all interrupts are gone. Wait
260 * for bottom half handlers unless we're
261 * already executing in one..
263 if (!irqs_running())
264 if (local_bh_count(cpu) || !spin_is_locked(&global_bh_lock))
265 break;
267 /* Duh, we have to loop. Release the lock to avoid deadlocks */
268 clear_bit(0,&global_irq_lock);
270 for (;;) {
271 if (!--count) {
272 show("wait_on_irq");
273 count = ~0;
275 __sti();
276 SYNC_OTHER_CORES(cpu);
277 __cli();
278 if (irqs_running())
279 continue;
280 if (global_irq_lock)
281 continue;
282 if (!local_bh_count(cpu) && spin_is_locked(&global_bh_lock))
283 continue;
284 if (!test_and_set_bit(0,&global_irq_lock))
285 break;
291 * This is called when we want to synchronize with
292 * interrupts. We may for example tell a device to
293 * stop sending interrupts: but to make sure there
294 * are no interrupts that are executing on another
295 * CPU we need to call this function.
297 void synchronize_irq(void)
299 if (irqs_running()) {
300 /* Stupid approach */
301 cli();
302 sti();
306 static inline void get_irqlock(int cpu)
308 if (test_and_set_bit(0,&global_irq_lock)) {
309 /* do we already hold the lock? */
310 if ((unsigned char) cpu == global_irq_holder)
311 return;
312 /* Uhhuh.. Somebody else got it. Wait.. */
313 do {
314 do {
315 } while (test_bit(0,&global_irq_lock));
316 } while (test_and_set_bit(0,&global_irq_lock));
319 * We also to make sure that nobody else is running
320 * in an interrupt context.
322 wait_on_irq(cpu);
325 * Ok, finally..
327 global_irq_holder = cpu;
330 #define EFLAGS_IF_SHIFT 9
333 * A global "cli()" while in an interrupt context
334 * turns into just a local cli(). Interrupts
335 * should use spinlocks for the (very unlikely)
336 * case that they ever want to protect against
337 * each other.
339 * If we already have local interrupts disabled,
340 * this will not turn a local disable into a
341 * global one (problems with spinlocks: this makes
342 * save_flags+cli+sti usable inside a spinlock).
344 void __global_cli(void)
346 unsigned int flags;
348 __save_flags(flags);
349 if (flags & (1 << EFLAGS_IF_SHIFT)) {
350 int cpu = smp_processor_id();
351 __cli();
352 if (!local_irq_count(cpu))
353 get_irqlock(cpu);
357 void __global_sti(void)
359 int cpu = smp_processor_id();
361 if (!local_irq_count(cpu))
362 release_irqlock(cpu);
363 __sti();
367 * SMP flags value to restore to:
368 * 0 - global cli
369 * 1 - global sti
370 * 2 - local cli
371 * 3 - local sti
373 unsigned long __global_save_flags(void)
375 int retval;
376 int local_enabled;
377 unsigned long flags;
378 int cpu = smp_processor_id();
380 __save_flags(flags);
381 local_enabled = (flags >> EFLAGS_IF_SHIFT) & 1;
382 /* default to local */
383 retval = 2 + local_enabled;
385 /* check for global flags if we're not in an interrupt */
386 if (!local_irq_count(cpu)) {
387 if (local_enabled)
388 retval = 1;
389 if (global_irq_holder == cpu)
390 retval = 0;
392 return retval;
395 void __global_restore_flags(unsigned long flags)
397 switch (flags) {
398 case 0:
399 __global_cli();
400 break;
401 case 1:
402 __global_sti();
403 break;
404 case 2:
405 __cli();
406 break;
407 case 3:
408 __sti();
409 break;
410 default:
411 printk("global_restore_flags: %08lx (%08lx)\n",
412 flags, (&flags)[-1]);
416 #endif
419 * This should really return information about whether
420 * we should do bottom half handling etc. Right now we
421 * end up _always_ checking the bottom half, which is a
422 * waste of time and is not what some drivers would
423 * prefer.
425 int handle_IRQ_event(unsigned int irq, struct pt_regs * regs, struct irqaction * action)
427 int status;
428 int cpu = smp_processor_id();
430 irq_enter(cpu, irq);
432 status = 1; /* Force the "do bottom halves" bit */
434 if (!(action->flags & SA_INTERRUPT))
435 __sti();
437 do {
438 status |= action->flags;
439 action->handler(irq, action->dev_id, regs);
440 action = action->next;
441 } while (action);
442 if (status & SA_SAMPLE_RANDOM)
443 add_interrupt_randomness(irq);
444 __cli();
446 irq_exit(cpu, irq);
448 return status;
452 * Generic enable/disable code: this just calls
453 * down into the PIC-specific version for the actual
454 * hardware disable after having gotten the irq
455 * controller lock.
459 * disable_irq_nosync - disable an irq without waiting
460 * @irq: Interrupt to disable
462 * Disable the selected interrupt line. Disables of an interrupt
463 * stack. Unlike disable_irq(), this function does not ensure existing
464 * instances of the IRQ handler have completed before returning.
466 * This function may be called from IRQ context.
469 void inline disable_irq_nosync(unsigned int irq)
471 irq_desc_t *desc = irq_desc + irq;
472 unsigned long flags;
474 spin_lock_irqsave(&desc->lock, flags);
475 if (!desc->depth++) {
476 desc->status |= IRQ_DISABLED;
477 desc->handler->disable(irq);
479 spin_unlock_irqrestore(&desc->lock, flags);
483 * disable_irq - disable an irq and wait for completion
484 * @irq: Interrupt to disable
486 * Disable the selected interrupt line. Disables of an interrupt
487 * stack. That is for two disables you need two enables. This
488 * function waits for any pending IRQ handlers for this interrupt
489 * to complete before returning. If you use this function while
490 * holding a resource the IRQ handler may need you will deadlock.
492 * This function may be called - with care - from IRQ context.
495 void disable_irq(unsigned int irq)
497 disable_irq_nosync(irq);
499 if (!local_irq_count(smp_processor_id())) {
500 do {
501 barrier();
502 } while (irq_desc[irq].status & IRQ_INPROGRESS);
507 * enable_irq - enable interrupt handling on an irq
508 * @irq: Interrupt to enable
510 * Re-enables the processing of interrupts on this IRQ line
511 * providing no disable_irq calls are now in effect.
513 * This function may be called from IRQ context.
516 void enable_irq(unsigned int irq)
518 irq_desc_t *desc = irq_desc + irq;
519 unsigned long flags;
521 spin_lock_irqsave(&desc->lock, flags);
522 switch (desc->depth) {
523 case 1: {
524 unsigned int status = desc->status & ~IRQ_DISABLED;
525 desc->status = status;
526 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
527 desc->status = status | IRQ_REPLAY;
528 hw_resend_irq(desc->handler,irq);
530 desc->handler->enable(irq);
531 /* fall-through */
533 default:
534 desc->depth--;
535 break;
536 case 0:
537 printk("enable_irq(%u) unbalanced from %p\n", irq,
538 __builtin_return_address(0));
540 spin_unlock_irqrestore(&desc->lock, flags);
544 * do_IRQ handles all normal device IRQ's (the special
545 * SMP cross-CPU interrupts have their own specific
546 * handlers).
548 asmlinkage unsigned int do_IRQ(struct pt_regs regs)
551 * We ack quickly, we don't want the irq controller
552 * thinking we're snobs just because some other CPU has
553 * disabled global interrupts (we have already done the
554 * INT_ACK cycles, it's too late to try to pretend to the
555 * controller that we aren't taking the interrupt).
557 * 0 return value means that this irq is already being
558 * handled by some other CPU. (or is disabled)
560 int irq = regs.orig_eax & 0xff; /* high bits used in ret_from_ code */
561 int cpu = smp_processor_id();
562 irq_desc_t *desc = irq_desc + irq;
563 struct irqaction * action;
564 unsigned int status;
566 kstat.irqs[cpu][irq]++;
567 spin_lock(&desc->lock);
568 desc->handler->ack(irq);
570 REPLAY is when Linux resends an IRQ that was dropped earlier
571 WAITING is used by probe to mark irqs that are being tested
573 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
574 status |= IRQ_PENDING; /* we _want_ to handle it */
577 * If the IRQ is disabled for whatever reason, we cannot
578 * use the action we have.
580 action = NULL;
581 if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
582 action = desc->action;
583 status &= ~IRQ_PENDING; /* we commit to handling */
584 status |= IRQ_INPROGRESS; /* we are handling it */
586 desc->status = status;
589 * If there is no IRQ handler or it was disabled, exit early.
590 Since we set PENDING, if another processor is handling
591 a different instance of this same irq, the other processor
592 will take care of it.
594 if (!action)
595 goto out;
598 * Edge triggered interrupts need to remember
599 * pending events.
600 * This applies to any hw interrupts that allow a second
601 * instance of the same irq to arrive while we are in do_IRQ
602 * or in the handler. But the code here only handles the _second_
603 * instance of the irq, not the third or fourth. So it is mostly
604 * useful for irq hardware that does not mask cleanly in an
605 * SMP environment.
607 for (;;) {
608 spin_unlock(&desc->lock);
609 handle_IRQ_event(irq, &regs, action);
610 spin_lock(&desc->lock);
612 if (!(desc->status & IRQ_PENDING))
613 break;
614 desc->status &= ~IRQ_PENDING;
616 desc->status &= ~IRQ_INPROGRESS;
617 out:
619 * The ->end() handler has to deal with interrupts which got
620 * disabled while the handler was running.
622 desc->handler->end(irq);
623 spin_unlock(&desc->lock);
625 if (softirq_active(cpu) & softirq_mask(cpu))
626 do_softirq();
627 return 1;
631 * request_irq - allocate an interrupt line
632 * @irq: Interrupt line to allocate
633 * @handler: Function to be called when the IRQ occurs
634 * @irqflags: Interrupt type flags
635 * @devname: An ascii name for the claiming device
636 * @dev_id: A cookie passed back to the handler function
638 * This call allocates interrupt resources and enables the
639 * interrupt line and IRQ handling. From the point this
640 * call is made your handler function may be invoked. Since
641 * your handler function must clear any interrupt the board
642 * raises, you must take care both to initialise your hardware
643 * and to set up the interrupt handler in the right order.
645 * Dev_id must be globally unique. Normally the address of the
646 * device data structure is used as the cookie. Since the handler
647 * receives this value it makes sense to use it.
649 * If your interrupt is shared you must pass a non NULL dev_id
650 * as this is required when freeing the interrupt.
652 * Flags:
654 * SA_SHIRQ Interrupt is shared
656 * SA_INTERRUPT Disable local interrupts while processing
658 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
662 int request_irq(unsigned int irq,
663 void (*handler)(int, void *, struct pt_regs *),
664 unsigned long irqflags,
665 const char * devname,
666 void *dev_id)
668 int retval;
669 struct irqaction * action;
671 #if 1
673 * Sanity-check: shared interrupts should REALLY pass in
674 * a real dev-ID, otherwise we'll have trouble later trying
675 * to figure out which interrupt is which (messes up the
676 * interrupt freeing logic etc).
678 if (irqflags & SA_SHIRQ) {
679 if (!dev_id)
680 printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
682 #endif
684 if (irq >= NR_IRQS)
685 return -EINVAL;
686 if (!handler)
687 return -EINVAL;
689 action = (struct irqaction *)
690 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
691 if (!action)
692 return -ENOMEM;
694 action->handler = handler;
695 action->flags = irqflags;
696 action->mask = 0;
697 action->name = devname;
698 action->next = NULL;
699 action->dev_id = dev_id;
701 retval = setup_irq(irq, action);
702 if (retval)
703 kfree(action);
704 return retval;
708 * free_irq - free an interrupt
709 * @irq: Interrupt line to free
710 * @dev_id: Device identity to free
712 * Remove an interrupt handler. The handler is removed and if the
713 * interrupt line is no longer in use by any driver it is disabled.
714 * On a shared IRQ the caller must ensure the interrupt is disabled
715 * on the card it drives before calling this function. The function
716 * does not return until any executing interrupts for this IRQ
717 * have completed.
719 * This function may be called from interrupt context.
721 * Bugs: Attempting to free an irq in a handler for the same irq hangs
722 * the machine.
725 void free_irq(unsigned int irq, void *dev_id)
727 irq_desc_t *desc;
728 struct irqaction **p;
729 unsigned long flags;
731 if (irq >= NR_IRQS)
732 return;
734 desc = irq_desc + irq;
735 spin_lock_irqsave(&desc->lock,flags);
736 p = &desc->action;
737 for (;;) {
738 struct irqaction * action = *p;
739 if (action) {
740 struct irqaction **pp = p;
741 p = &action->next;
742 if (action->dev_id != dev_id)
743 continue;
745 /* Found it - now remove it from the list of entries */
746 *pp = action->next;
747 if (!desc->action) {
748 desc->status |= IRQ_DISABLED;
749 desc->handler->shutdown(irq);
751 spin_unlock_irqrestore(&desc->lock,flags);
753 #ifdef CONFIG_SMP
754 /* Wait to make sure it's not being used on another CPU */
755 while (desc->status & IRQ_INPROGRESS)
756 barrier();
757 #endif
758 kfree(action);
759 return;
761 printk("Trying to free free IRQ%d\n",irq);
762 spin_unlock_irqrestore(&desc->lock,flags);
763 return;
768 * IRQ autodetection code..
770 * This depends on the fact that any interrupt that
771 * comes in on to an unassigned handler will get stuck
772 * with "IRQ_WAITING" cleared and the interrupt
773 * disabled.
776 static DECLARE_MUTEX(probe_sem);
779 * probe_irq_on - begin an interrupt autodetect
781 * Commence probing for an interrupt. The interrupts are scanned
782 * and a mask of potential interrupt lines is returned.
786 unsigned long probe_irq_on(void)
788 unsigned int i;
789 irq_desc_t *desc;
790 unsigned long val;
791 unsigned long delay;
793 down(&probe_sem);
795 * something may have generated an irq long ago and we want to
796 * flush such a longstanding irq before considering it as spurious.
798 for (i = NR_IRQS-1; i > 0; i--) {
799 desc = irq_desc + i;
801 spin_lock_irq(&desc->lock);
802 if (!irq_desc[i].action)
803 irq_desc[i].handler->startup(i);
804 spin_unlock_irq(&desc->lock);
807 /* Wait for longstanding interrupts to trigger. */
808 for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
809 /* about 20ms delay */ synchronize_irq();
812 * enable any unassigned irqs
813 * (we must startup again here because if a longstanding irq
814 * happened in the previous stage, it may have masked itself)
816 for (i = NR_IRQS-1; i > 0; i--) {
817 desc = irq_desc + i;
819 spin_lock_irq(&desc->lock);
820 if (!desc->action) {
821 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
822 if (desc->handler->startup(i))
823 desc->status |= IRQ_PENDING;
825 spin_unlock_irq(&desc->lock);
829 * Wait for spurious interrupts to trigger
831 for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
832 /* about 100ms delay */ synchronize_irq();
835 * Now filter out any obviously spurious interrupts
837 val = 0;
838 for (i = 0; i < NR_IRQS; i++) {
839 irq_desc_t *desc = irq_desc + i;
840 unsigned int status;
842 spin_lock_irq(&desc->lock);
843 status = desc->status;
845 if (status & IRQ_AUTODETECT) {
846 /* It triggered already - consider it spurious. */
847 if (!(status & IRQ_WAITING)) {
848 desc->status = status & ~IRQ_AUTODETECT;
849 desc->handler->shutdown(i);
850 } else
851 if (i < 32)
852 val |= 1 << i;
854 spin_unlock_irq(&desc->lock);
857 return val;
861 * Return a mask of triggered interrupts (this
862 * can handle only legacy ISA interrupts).
866 * probe_irq_mask - scan a bitmap of interrupt lines
867 * @val: mask of interrupts to consider
869 * Scan the ISA bus interrupt lines and return a bitmap of
870 * active interrupts. The interrupt probe logic state is then
871 * returned to its previous value.
873 * Note: we need to scan all the irq's even though we will
874 * only return ISA irq numbers - just so that we reset them
875 * all to a known state.
877 unsigned int probe_irq_mask(unsigned long val)
879 int i;
880 unsigned int mask;
882 mask = 0;
883 for (i = 0; i < NR_IRQS; i++) {
884 irq_desc_t *desc = irq_desc + i;
885 unsigned int status;
887 spin_lock_irq(&desc->lock);
888 status = desc->status;
890 if (status & IRQ_AUTODETECT) {
891 if (i < 16 && !(status & IRQ_WAITING))
892 mask |= 1 << i;
894 desc->status = status & ~IRQ_AUTODETECT;
895 desc->handler->shutdown(i);
897 spin_unlock_irq(&desc->lock);
899 up(&probe_sem);
901 return mask & val;
905 * Return the one interrupt that triggered (this can
906 * handle any interrupt source).
910 * probe_irq_off - end an interrupt autodetect
911 * @val: mask of potential interrupts (unused)
913 * Scans the unused interrupt lines and returns the line which
914 * appears to have triggered the interrupt. If no interrupt was
915 * found then zero is returned. If more than one interrupt is
916 * found then minus the first candidate is returned to indicate
917 * their is doubt.
919 * The interrupt probe logic state is returned to its previous
920 * value.
922 * BUGS: When used in a module (which arguably shouldnt happen)
923 * nothing prevents two IRQ probe callers from overlapping. The
924 * results of this are non-optimal.
927 int probe_irq_off(unsigned long val)
929 int i, irq_found, nr_irqs;
931 nr_irqs = 0;
932 irq_found = 0;
933 for (i = 0; i < NR_IRQS; i++) {
934 irq_desc_t *desc = irq_desc + i;
935 unsigned int status;
937 spin_lock_irq(&desc->lock);
938 status = desc->status;
940 if (status & IRQ_AUTODETECT) {
941 if (!(status & IRQ_WAITING)) {
942 if (!nr_irqs)
943 irq_found = i;
944 nr_irqs++;
946 desc->status = status & ~IRQ_AUTODETECT;
947 desc->handler->shutdown(i);
949 spin_unlock_irq(&desc->lock);
951 up(&probe_sem);
953 if (nr_irqs > 1)
954 irq_found = -irq_found;
955 return irq_found;
958 /* this was setup_x86_irq but it seems pretty generic */
959 int setup_irq(unsigned int irq, struct irqaction * new)
961 int shared = 0;
962 unsigned long flags;
963 struct irqaction *old, **p;
964 irq_desc_t *desc = irq_desc + irq;
967 * Some drivers like serial.c use request_irq() heavily,
968 * so we have to be careful not to interfere with a
969 * running system.
971 if (new->flags & SA_SAMPLE_RANDOM) {
973 * This function might sleep, we want to call it first,
974 * outside of the atomic block.
975 * Yes, this might clear the entropy pool if the wrong
976 * driver is attempted to be loaded, without actually
977 * installing a new handler, but is this really a problem,
978 * only the sysadmin is able to do this.
980 rand_initialize_irq(irq);
984 * The following block of code has to be executed atomically
986 spin_lock_irqsave(&desc->lock,flags);
987 p = &desc->action;
988 if ((old = *p) != NULL) {
989 /* Can't share interrupts unless both agree to */
990 if (!(old->flags & new->flags & SA_SHIRQ)) {
991 spin_unlock_irqrestore(&desc->lock,flags);
992 return -EBUSY;
995 /* add new interrupt at end of irq queue */
996 do {
997 p = &old->next;
998 old = *p;
999 } while (old);
1000 shared = 1;
1003 *p = new;
1005 if (!shared) {
1006 desc->depth = 0;
1007 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING);
1008 desc->handler->startup(irq);
1010 spin_unlock_irqrestore(&desc->lock,flags);
1012 register_irq_proc(irq);
1013 return 0;
1016 static struct proc_dir_entry * root_irq_dir;
1017 static struct proc_dir_entry * irq_dir [NR_IRQS];
1018 static struct proc_dir_entry * smp_affinity_entry [NR_IRQS];
1020 static unsigned long irq_affinity [NR_IRQS] = { [0 ... NR_IRQS-1] = ~0UL };
1022 #define HEX_DIGITS 8
1024 static int irq_affinity_read_proc (char *page, char **start, off_t off,
1025 int count, int *eof, void *data)
1027 if (count < HEX_DIGITS+1)
1028 return -EINVAL;
1029 return sprintf (page, "%08lx\n", irq_affinity[(long)data]);
1032 static unsigned int parse_hex_value (const char *buffer,
1033 unsigned long count, unsigned long *ret)
1035 unsigned char hexnum [HEX_DIGITS];
1036 unsigned long value;
1037 int i;
1039 if (!count)
1040 return -EINVAL;
1041 if (count > HEX_DIGITS)
1042 count = HEX_DIGITS;
1043 if (copy_from_user(hexnum, buffer, count))
1044 return -EFAULT;
1047 * Parse the first 8 characters as a hex string, any non-hex char
1048 * is end-of-string. '00e1', 'e1', '00E1', 'E1' are all the same.
1050 value = 0;
1052 for (i = 0; i < count; i++) {
1053 unsigned int c = hexnum[i];
1055 switch (c) {
1056 case '0' ... '9': c -= '0'; break;
1057 case 'a' ... 'f': c -= 'a'-10; break;
1058 case 'A' ... 'F': c -= 'A'-10; break;
1059 default:
1060 goto out;
1062 value = (value << 4) | c;
1064 out:
1065 *ret = value;
1066 return 0;
1069 static int irq_affinity_write_proc (struct file *file, const char *buffer,
1070 unsigned long count, void *data)
1072 int irq = (long) data, full_count = count, err;
1073 unsigned long new_value;
1075 if (!irq_desc[irq].handler->set_affinity)
1076 return -EIO;
1078 err = parse_hex_value(buffer, count, &new_value);
1080 #if CONFIG_SMP
1082 * Do not allow disabling IRQs completely - it's a too easy
1083 * way to make the system unusable accidentally :-) At least
1084 * one online CPU still has to be targeted.
1086 if (!(new_value & cpu_online_map))
1087 return -EINVAL;
1088 #endif
1090 irq_affinity[irq] = new_value;
1091 irq_desc[irq].handler->set_affinity(irq, new_value);
1093 return full_count;
1096 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
1097 int count, int *eof, void *data)
1099 unsigned long *mask = (unsigned long *) data;
1100 if (count < HEX_DIGITS+1)
1101 return -EINVAL;
1102 return sprintf (page, "%08lx\n", *mask);
1105 static int prof_cpu_mask_write_proc (struct file *file, const char *buffer,
1106 unsigned long count, void *data)
1108 unsigned long *mask = (unsigned long *) data, full_count = count, err;
1109 unsigned long new_value;
1111 err = parse_hex_value(buffer, count, &new_value);
1112 if (err)
1113 return err;
1115 *mask = new_value;
1116 return full_count;
1119 #define MAX_NAMELEN 10
1121 static void register_irq_proc (unsigned int irq)
1123 struct proc_dir_entry *entry;
1124 char name [MAX_NAMELEN];
1126 if (!root_irq_dir || (irq_desc[irq].handler == &no_irq_type) ||
1127 irq_dir[irq])
1128 return;
1130 memset(name, 0, MAX_NAMELEN);
1131 sprintf(name, "%d", irq);
1133 /* create /proc/irq/1234 */
1134 irq_dir[irq] = proc_mkdir(name, root_irq_dir);
1136 /* create /proc/irq/1234/smp_affinity */
1137 entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
1139 entry->nlink = 1;
1140 entry->data = (void *)(long)irq;
1141 entry->read_proc = irq_affinity_read_proc;
1142 entry->write_proc = irq_affinity_write_proc;
1144 smp_affinity_entry[irq] = entry;
1147 unsigned long prof_cpu_mask = -1;
1149 void init_irq_proc (void)
1151 struct proc_dir_entry *entry;
1152 int i;
1154 /* create /proc/irq */
1155 root_irq_dir = proc_mkdir("irq", 0);
1157 /* create /proc/irq/prof_cpu_mask */
1158 entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
1160 entry->nlink = 1;
1161 entry->data = (void *)&prof_cpu_mask;
1162 entry->read_proc = prof_cpu_mask_read_proc;
1163 entry->write_proc = prof_cpu_mask_write_proc;
1166 * Create entries for all existing IRQs.
1168 for (i = 0; i < NR_IRQS; i++)
1169 register_irq_proc(i);