- Andries Brouwer: final isofs pieces.
[davej-history.git] / arch / ia64 / kernel / irq.c
blobb3646e275c94a29f7e74a91599efc88db6000282
1 /*
2 * linux/arch/ia64/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/irq.h>
48 * Linux has a controller-independent x86 interrupt architecture.
49 * every controller has a 'controller-template', that is used
50 * by the main code to do the right thing. Each driver-visible
51 * interrupt source is transparently wired to the apropriate
52 * controller. Thus drivers need not be aware of the
53 * interrupt-controller.
55 * Various interrupt controllers we handle: 8259 PIC, SMP IO-APIC,
56 * PIIX4's internal 8259 PIC and SGI's Visual Workstation Cobalt (IO-)APIC.
57 * (IO-APICs assumed to be messaging to Pentium local-APICs)
59 * the code is designed to be easily extended with new/different
60 * interrupt controllers, without having to do assembly magic.
64 * Controller mappings for all interrupt sources:
66 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned =
67 { [0 ... NR_IRQS-1] = { IRQ_DISABLED, &no_irq_type, NULL, 0, SPIN_LOCK_UNLOCKED}};
69 static void register_irq_proc (unsigned int irq);
72 * Special irq handlers.
75 void no_action(int cpl, void *dev_id, struct pt_regs *regs) { }
78 * Generic no controller code
81 static void enable_none(unsigned int irq) { }
82 static unsigned int startup_none(unsigned int irq) { return 0; }
83 static void disable_none(unsigned int irq) { }
84 static void ack_none(unsigned int irq)
87 * 'what should we do if we get a hw irq event on an illegal vector'.
88 * each architecture has to answer this themselves, it doesnt deserve
89 * a generic callback i think.
91 #if CONFIG_X86
92 printk("unexpected IRQ trap at vector %02x\n", irq);
93 #ifdef CONFIG_X86_LOCAL_APIC
95 * Currently unexpected vectors happen only on SMP and APIC.
96 * We _must_ ack these because every local APIC has only N
97 * irq slots per priority level, and a 'hanging, unacked' IRQ
98 * holds up an irq slot - in excessive cases (when multiple
99 * unexpected vectors occur) that might lock up the APIC
100 * completely.
102 ack_APIC_irq();
103 #endif
104 #endif
105 #if CONFIG_IA64
106 printk("Unexpected irq vector 0x%x on CPU %u!\n", irq, smp_processor_id());
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_count(cpu_logical_map(j)));
164 p += sprintf(p, "\n");
165 #if defined(CONFIG_SMP) && defined(__i386__)
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 int global_irq_holder = NO_PROC_ID;
184 volatile unsigned long global_irq_lock; /* long for set_bit --RR */
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 #if defined(__ia64__)
204 * We can't unwind the stack of another CPU without access to
205 * the registers of that CPU. And sending an IPI when we're
206 * in a potentially wedged state doesn't sound like a smart
207 * idea.
209 #elif defined(__i386__)
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].esp0;
216 if(esp==NULL) {
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 task_struct);
226 show_stack((void*)esp);
228 #else
229 You lose...
230 #endif
231 printk("\nCPU %d:",cpu);
232 show_stack(NULL);
233 printk("\n");
236 #define MAXCOUNT 100000000
239 * I had a lockup scenario where a tight loop doing
240 * spin_unlock()/spin_lock() on CPU#1 was racing with
241 * spin_lock() on CPU#0. CPU#0 should have noticed spin_unlock(), but
242 * apparently the spin_unlock() information did not make it
243 * through to CPU#0 ... nasty, is this by design, do we have to limit
244 * 'memory update oscillation frequency' artificially like here?
246 * Such 'high frequency update' races can be avoided by careful design, but
247 * some of our major constructs like spinlocks use similar techniques,
248 * it would be nice to clarify this issue. Set this define to 0 if you
249 * want to check whether your system freezes. I suspect the delay done
250 * by SYNC_OTHER_CORES() is in correlation with 'snooping latency', but
251 * i thought that such things are guaranteed by design, since we use
252 * the 'LOCK' prefix.
254 #define SUSPECTED_CPU_OR_CHIPSET_BUG_WORKAROUND 0
256 #if SUSPECTED_CPU_OR_CHIPSET_BUG_WORKAROUND
257 # define SYNC_OTHER_CORES(x) udelay(x+1)
258 #else
260 * We have to allow irqs to arrive between __sti and __cli
262 # ifdef __ia64__
263 # define SYNC_OTHER_CORES(x) __asm__ __volatile__ ("nop 0")
264 # else
265 # define SYNC_OTHER_CORES(x) __asm__ __volatile__ ("nop")
266 # endif
267 #endif
269 static inline void wait_on_irq(int cpu)
271 int count = MAXCOUNT;
273 for (;;) {
276 * Wait until all interrupts are gone. Wait
277 * for bottom half handlers unless we're
278 * already executing in one..
280 if (!irqs_running())
281 if (local_bh_count(cpu) || !spin_is_locked(&global_bh_lock))
282 break;
284 /* Duh, we have to loop. Release the lock to avoid deadlocks */
285 clear_bit(0,&global_irq_lock);
287 for (;;) {
288 if (!--count) {
289 show("wait_on_irq");
290 count = ~0;
292 __sti();
293 SYNC_OTHER_CORES(cpu);
294 __cli();
295 if (irqs_running())
296 continue;
297 if (global_irq_lock)
298 continue;
299 if (!local_bh_count(cpu) && spin_is_locked(&global_bh_lock))
300 continue;
301 if (!test_and_set_bit(0,&global_irq_lock))
302 break;
308 * This is called when we want to synchronize with
309 * interrupts. We may for example tell a device to
310 * stop sending interrupts: but to make sure there
311 * are no interrupts that are executing on another
312 * CPU we need to call this function.
314 void synchronize_irq(void)
316 if (irqs_running()) {
317 /* Stupid approach */
318 cli();
319 sti();
323 static inline void get_irqlock(int cpu)
325 if (test_and_set_bit(0,&global_irq_lock)) {
326 /* do we already hold the lock? */
327 if (cpu == global_irq_holder)
328 return;
329 /* Uhhuh.. Somebody else got it. Wait.. */
330 do {
331 do {
332 } while (test_bit(0,&global_irq_lock));
333 } while (test_and_set_bit(0,&global_irq_lock));
336 * We also to make sure that nobody else is running
337 * in an interrupt context.
339 wait_on_irq(cpu);
342 * Ok, finally..
344 global_irq_holder = cpu;
347 #define EFLAGS_IF_SHIFT 9
350 * A global "cli()" while in an interrupt context
351 * turns into just a local cli(). Interrupts
352 * should use spinlocks for the (very unlikely)
353 * case that they ever want to protect against
354 * each other.
356 * If we already have local interrupts disabled,
357 * this will not turn a local disable into a
358 * global one (problems with spinlocks: this makes
359 * save_flags+cli+sti usable inside a spinlock).
361 void __global_cli(void)
363 unsigned int flags;
365 #ifdef __ia64__
366 __save_flags(flags);
367 if (flags & IA64_PSR_I) {
368 int cpu = smp_processor_id();
369 __cli();
370 if (!local_irq_count(cpu))
371 get_irqlock(cpu);
373 #else
374 __save_flags(flags);
375 if (flags & (1 << EFLAGS_IF_SHIFT)) {
376 int cpu = smp_processor_id();
377 __cli();
378 if (!local_irq_count(cpu))
379 get_irqlock(cpu);
381 #endif
384 void __global_sti(void)
386 int cpu = smp_processor_id();
388 if (!local_irq_count(cpu))
389 release_irqlock(cpu);
390 __sti();
394 * SMP flags value to restore to:
395 * 0 - global cli
396 * 1 - global sti
397 * 2 - local cli
398 * 3 - local sti
400 unsigned long __global_save_flags(void)
402 int retval;
403 int local_enabled;
404 unsigned long flags;
405 int cpu = smp_processor_id();
407 __save_flags(flags);
408 #ifdef __ia64__
409 local_enabled = (flags & IA64_PSR_I) != 0;
410 #else
411 local_enabled = (flags >> EFLAGS_IF_SHIFT) & 1;
412 #endif
413 /* default to local */
414 retval = 2 + local_enabled;
416 /* check for global flags if we're not in an interrupt */
417 if (!local_irq_count(cpu)) {
418 if (local_enabled)
419 retval = 1;
420 if (global_irq_holder == cpu)
421 retval = 0;
423 return retval;
426 void __global_restore_flags(unsigned long flags)
428 switch (flags) {
429 case 0:
430 __global_cli();
431 break;
432 case 1:
433 __global_sti();
434 break;
435 case 2:
436 __cli();
437 break;
438 case 3:
439 __sti();
440 break;
441 default:
442 printk("global_restore_flags: %08lx (%08lx)\n",
443 flags, (&flags)[-1]);
447 #endif
450 * This should really return information about whether
451 * we should do bottom half handling etc. Right now we
452 * end up _always_ checking the bottom half, which is a
453 * waste of time and is not what some drivers would
454 * prefer.
456 int handle_IRQ_event(unsigned int irq, struct pt_regs * regs, struct irqaction * action)
458 int status;
459 int cpu = smp_processor_id();
461 irq_enter(cpu, irq);
463 status = 1; /* Force the "do bottom halves" bit */
465 if (!(action->flags & SA_INTERRUPT))
466 __sti();
468 do {
469 status |= action->flags;
470 action->handler(irq, action->dev_id, regs);
471 action = action->next;
472 } while (action);
473 if (status & SA_SAMPLE_RANDOM)
474 add_interrupt_randomness(irq);
475 __cli();
477 irq_exit(cpu, irq);
479 return status;
483 * Generic enable/disable code: this just calls
484 * down into the PIC-specific version for the actual
485 * hardware disable after having gotten the irq
486 * controller lock.
488 void inline disable_irq_nosync(unsigned int irq)
490 irq_desc_t *desc = irq_desc + irq;
491 unsigned long flags;
493 spin_lock_irqsave(&desc->lock, flags);
494 if (!desc->depth++) {
495 desc->status |= IRQ_DISABLED;
496 desc->handler->disable(irq);
498 spin_unlock_irqrestore(&desc->lock, flags);
502 * Synchronous version of the above, making sure the IRQ is
503 * no longer running on any other IRQ..
505 void disable_irq(unsigned int irq)
507 disable_irq_nosync(irq);
509 #ifdef CONFIG_SMP
510 if (!local_irq_count(smp_processor_id())) {
511 do {
512 barrier();
513 } while (irq_desc[irq].status & IRQ_INPROGRESS);
515 #endif
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() unbalanced from %p\n", (void *) __builtin_return_address(0));
541 spin_unlock_irqrestore(&desc->lock, flags);
545 * do_IRQ handles all normal device IRQ's (the special
546 * SMP cross-CPU interrupts have their own specific
547 * handlers).
549 unsigned int do_IRQ(unsigned long irq, struct pt_regs *regs)
552 * We ack quickly, we don't want the irq controller
553 * thinking we're snobs just because some other CPU has
554 * disabled global interrupts (we have already done the
555 * INT_ACK cycles, it's too late to try to pretend to the
556 * controller that we aren't taking the interrupt).
558 * 0 return value means that this irq is already being
559 * handled by some other CPU. (or is disabled)
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 if (!(status & IRQ_PER_CPU))
585 status |= IRQ_INPROGRESS; /* we are handling it */
587 desc->status = status;
590 * If there is no IRQ handler or it was disabled, exit early.
591 * Since we set PENDING, if another processor is handling
592 * a different instance of this same irq, the other processor
593 * will take care of it.
595 if (!action)
596 goto out;
599 * Edge triggered interrupts need to remember
600 * pending events.
601 * This applies to any hw interrupts that allow a second
602 * instance of the same irq to arrive while we are in do_IRQ
603 * or in the handler. But the code here only handles the _second_
604 * instance of the irq, not the third or fourth. So it is mostly
605 * useful for irq hardware that does not mask cleanly in an
606 * SMP environment.
608 for (;;) {
609 spin_unlock(&desc->lock);
610 handle_IRQ_event(irq, regs, action);
611 spin_lock(&desc->lock);
613 if (!(desc->status & IRQ_PENDING))
614 break;
615 desc->status &= ~IRQ_PENDING;
617 desc->status &= ~IRQ_INPROGRESS;
618 out:
620 * The ->end() handler has to deal with interrupts which got
621 * disabled while the handler was running.
623 desc->handler->end(irq);
624 spin_unlock(&desc->lock);
626 return 1;
629 int request_irq(unsigned int irq,
630 void (*handler)(int, void *, struct pt_regs *),
631 unsigned long irqflags,
632 const char * devname,
633 void *dev_id)
635 int retval;
636 struct irqaction * action;
638 #if 1
640 * Sanity-check: shared interrupts should REALLY pass in
641 * a real dev-ID, otherwise we'll have trouble later trying
642 * to figure out which interrupt is which (messes up the
643 * interrupt freeing logic etc).
645 if (irqflags & SA_SHIRQ) {
646 if (!dev_id)
647 printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
649 #endif
651 if (irq >= NR_IRQS)
652 return -EINVAL;
653 if (!handler)
654 return -EINVAL;
656 action = (struct irqaction *)
657 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
658 if (!action)
659 return -ENOMEM;
661 action->handler = handler;
662 action->flags = irqflags;
663 action->mask = 0;
664 action->name = devname;
665 action->next = NULL;
666 action->dev_id = dev_id;
668 retval = setup_irq(irq, action);
669 if (retval)
670 kfree(action);
671 return retval;
674 void free_irq(unsigned int irq, void *dev_id)
676 irq_desc_t *desc;
677 struct irqaction **p;
678 unsigned long flags;
680 if (irq >= NR_IRQS)
681 return;
683 desc = irq_desc + irq;
684 spin_lock_irqsave(&desc->lock,flags);
685 p = &desc->action;
686 for (;;) {
687 struct irqaction * action = *p;
688 if (action) {
689 struct irqaction **pp = p;
690 p = &action->next;
691 if (action->dev_id != dev_id)
692 continue;
694 /* Found it - now remove it from the list of entries */
695 *pp = action->next;
696 if (!desc->action) {
697 desc->status |= IRQ_DISABLED;
698 desc->handler->shutdown(irq);
700 spin_unlock_irqrestore(&desc->lock,flags);
702 #ifdef CONFIG_SMP
703 /* Wait to make sure it's not being used on another CPU */
704 while (desc->status & IRQ_INPROGRESS)
705 barrier();
706 #endif
707 kfree(action);
708 return;
710 printk("Trying to free free IRQ%d\n",irq);
711 spin_unlock_irqrestore(&desc->lock,flags);
712 return;
717 * IRQ autodetection code..
719 * This depends on the fact that any interrupt that
720 * comes in on to an unassigned handler will get stuck
721 * with "IRQ_WAITING" cleared and the interrupt
722 * disabled.
724 unsigned long probe_irq_on(void)
726 unsigned int i;
727 irq_desc_t *desc;
728 unsigned long val;
729 unsigned long delay;
732 * something may have generated an irq long ago and we want to
733 * flush such a longstanding irq before considering it as spurious.
735 for (i = NR_IRQS-1; i > 0; i--) {
736 desc = irq_desc + i;
738 spin_lock_irq(&desc->lock);
739 if (!irq_desc[i].action)
740 irq_desc[i].handler->startup(i);
741 spin_unlock_irq(&desc->lock);
744 /* Wait for longstanding interrupts to trigger. */
745 for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
746 /* about 20ms delay */ synchronize_irq();
749 * enable any unassigned irqs
750 * (we must startup again here because if a longstanding irq
751 * happened in the previous stage, it may have masked itself)
753 for (i = NR_IRQS-1; i > 0; i--) {
754 desc = irq_desc + i;
756 spin_lock_irq(&desc->lock);
757 if (!desc->action) {
758 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
759 if (desc->handler->startup(i))
760 desc->status |= IRQ_PENDING;
762 spin_unlock_irq(&desc->lock);
766 * Wait for spurious interrupts to trigger
768 for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
769 /* about 100ms delay */ synchronize_irq();
772 * Now filter out any obviously spurious interrupts
774 val = 0;
775 for (i = 0; i < NR_IRQS; i++) {
776 irq_desc_t *desc = irq_desc + i;
777 unsigned int status;
779 spin_lock_irq(&desc->lock);
780 status = desc->status;
782 if (status & IRQ_AUTODETECT) {
783 /* It triggered already - consider it spurious. */
784 if (!(status & IRQ_WAITING)) {
785 desc->status = status & ~IRQ_AUTODETECT;
786 desc->handler->shutdown(i);
787 } else
788 if (i < 32)
789 val |= 1 << i;
791 spin_unlock_irq(&desc->lock);
794 return val;
798 * Return a mask of triggered interrupts (this
799 * can handle only legacy ISA interrupts).
801 unsigned int probe_irq_mask(unsigned long val)
803 int i;
804 unsigned int mask;
806 mask = 0;
807 for (i = 0; i < 16; i++) {
808 irq_desc_t *desc = irq_desc + i;
809 unsigned int status;
811 spin_lock_irq(&desc->lock);
812 status = desc->status;
814 if (status & IRQ_AUTODETECT) {
815 if (!(status & IRQ_WAITING))
816 mask |= 1 << i;
818 desc->status = status & ~IRQ_AUTODETECT;
819 desc->handler->shutdown(i);
821 spin_unlock_irq(&desc->lock);
824 return mask & val;
828 * Return the one interrupt that triggered (this can
829 * handle any interrupt source)
831 int probe_irq_off(unsigned long val)
833 int i, irq_found, nr_irqs;
835 nr_irqs = 0;
836 irq_found = 0;
837 for (i = 0; i < NR_IRQS; i++) {
838 irq_desc_t *desc = irq_desc + i;
839 unsigned int status;
841 spin_lock_irq(&desc->lock);
842 status = desc->status;
844 if (status & IRQ_AUTODETECT) {
845 if (!(status & IRQ_WAITING)) {
846 if (!nr_irqs)
847 irq_found = i;
848 nr_irqs++;
850 desc->status = status & ~IRQ_AUTODETECT;
851 desc->handler->shutdown(i);
853 spin_unlock_irq(&desc->lock);
856 if (nr_irqs > 1)
857 irq_found = -irq_found;
858 return irq_found;
861 /* this was setup_x86_irq but it seems pretty generic */
862 int setup_irq(unsigned int irq, struct irqaction * new)
864 int shared = 0;
865 unsigned long flags;
866 struct irqaction *old, **p;
867 irq_desc_t *desc = irq_desc + irq;
870 * Some drivers like serial.c use request_irq() heavily,
871 * so we have to be careful not to interfere with a
872 * running system.
874 if (new->flags & SA_SAMPLE_RANDOM) {
876 * This function might sleep, we want to call it first,
877 * outside of the atomic block.
878 * Yes, this might clear the entropy pool if the wrong
879 * driver is attempted to be loaded, without actually
880 * installing a new handler, but is this really a problem,
881 * only the sysadmin is able to do this.
883 rand_initialize_irq(irq);
887 * The following block of code has to be executed atomically
889 spin_lock_irqsave(&desc->lock,flags);
890 p = &desc->action;
891 if ((old = *p) != NULL) {
892 /* Can't share interrupts unless both agree to */
893 if (!(old->flags & new->flags & SA_SHIRQ)) {
894 spin_unlock_irqrestore(&desc->lock,flags);
895 return -EBUSY;
898 /* add new interrupt at end of irq queue */
899 do {
900 p = &old->next;
901 old = *p;
902 } while (old);
903 shared = 1;
906 *p = new;
908 if (!shared) {
909 desc->depth = 0;
910 desc->status &= ~IRQ_DISABLED;
911 desc->handler->startup(irq);
913 spin_unlock_irqrestore(&desc->lock,flags);
915 register_irq_proc(irq);
916 return 0;
919 static struct proc_dir_entry * root_irq_dir;
920 static struct proc_dir_entry * irq_dir [NR_IRQS];
921 static struct proc_dir_entry * smp_affinity_entry [NR_IRQS];
923 static unsigned long irq_affinity [NR_IRQS] = { [0 ... NR_IRQS-1] = ~0UL };
925 #define HEX_DIGITS 8
927 static int irq_affinity_read_proc (char *page, char **start, off_t off,
928 int count, int *eof, void *data)
930 if (count < HEX_DIGITS+1)
931 return -EINVAL;
932 return sprintf (page, "%08lx\n", irq_affinity[(long)data]);
935 static unsigned int parse_hex_value (const char *buffer,
936 unsigned long count, unsigned long *ret)
938 unsigned char hexnum [HEX_DIGITS];
939 unsigned long value;
940 int i;
942 if (!count)
943 return -EINVAL;
944 if (count > HEX_DIGITS)
945 count = HEX_DIGITS;
946 if (copy_from_user(hexnum, buffer, count))
947 return -EFAULT;
950 * Parse the first 8 characters as a hex string, any non-hex char
951 * is end-of-string. '00e1', 'e1', '00E1', 'E1' are all the same.
953 value = 0;
955 for (i = 0; i < count; i++) {
956 unsigned int c = hexnum[i];
958 switch (c) {
959 case '0' ... '9': c -= '0'; break;
960 case 'a' ... 'f': c -= 'a'-10; break;
961 case 'A' ... 'F': c -= 'A'-10; break;
962 default:
963 goto out;
965 value = (value << 4) | c;
967 out:
968 *ret = value;
969 return 0;
972 static int irq_affinity_write_proc (struct file *file, const char *buffer,
973 unsigned long count, void *data)
975 int irq = (long) data, full_count = count, err;
976 unsigned long new_value;
978 if (!irq_desc[irq].handler->set_affinity)
979 return -EIO;
981 err = parse_hex_value(buffer, count, &new_value);
983 #if CONFIG_SMP
985 * Do not allow disabling IRQs completely - it's a too easy
986 * way to make the system unusable accidentally :-) At least
987 * one online CPU still has to be targeted.
989 if (!(new_value & cpu_online_map))
990 return -EINVAL;
991 #endif
993 irq_affinity[irq] = new_value;
994 irq_desc[irq].handler->set_affinity(irq, new_value);
996 return full_count;
999 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
1000 int count, int *eof, void *data)
1002 unsigned long *mask = (unsigned long *) data;
1003 if (count < HEX_DIGITS+1)
1004 return -EINVAL;
1005 return sprintf (page, "%08lx\n", *mask);
1008 static int prof_cpu_mask_write_proc (struct file *file, const char *buffer,
1009 unsigned long count, void *data)
1011 unsigned long *mask = (unsigned long *) data, full_count = count, err;
1012 unsigned long new_value;
1014 err = parse_hex_value(buffer, count, &new_value);
1015 if (err)
1016 return err;
1018 *mask = new_value;
1019 return full_count;
1022 #define MAX_NAMELEN 10
1024 static void register_irq_proc (unsigned int irq)
1026 struct proc_dir_entry *entry;
1027 char name [MAX_NAMELEN];
1029 if (!root_irq_dir || (irq_desc[irq].handler == &no_irq_type))
1030 return;
1032 memset(name, 0, MAX_NAMELEN);
1033 sprintf(name, "%d", irq);
1035 /* create /proc/irq/1234 */
1036 irq_dir[irq] = proc_mkdir(name, root_irq_dir);
1038 /* create /proc/irq/1234/smp_affinity */
1039 entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
1041 entry->nlink = 1;
1042 entry->data = (void *)(long)irq;
1043 entry->read_proc = irq_affinity_read_proc;
1044 entry->write_proc = irq_affinity_write_proc;
1046 smp_affinity_entry[irq] = entry;
1049 unsigned long prof_cpu_mask = -1;
1051 void init_irq_proc (void)
1053 struct proc_dir_entry *entry;
1054 int i;
1056 /* create /proc/irq */
1057 root_irq_dir = proc_mkdir("irq", 0);
1059 /* create /proc/irq/prof_cpu_mask */
1060 entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
1062 entry->nlink = 1;
1063 entry->data = (void *)&prof_cpu_mask;
1064 entry->read_proc = prof_cpu_mask_read_proc;
1065 entry->write_proc = prof_cpu_mask_write_proc;
1068 * Create entries for all existing IRQs.
1070 for (i = 0; i < NR_IRQS; i++) {
1071 if (irq_desc[i].handler == &no_irq_type)
1072 continue;
1073 register_irq_proc(i);