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