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