+- add patches.fixes/linux-post-2.6.3-20040220
[linux-flexiantxendom0-3.2.10.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         irq_enter();
459         cpu = smp_processor_id(); /* for CONFIG_PREEMPT, this must come after irq_enter()! */
460
461         kstat_cpu(cpu).irqs[irq]++;
462
463         if (desc->status & IRQ_PER_CPU) {
464                 /* no locking required for CPU-local interrupts: */
465                 desc->handler->ack(irq);
466                 action_ret = handle_IRQ_event(irq, regs, desc->action);
467                 desc->handler->end(irq);
468         } else {
469                 spin_lock(&desc->lock);
470                 desc->handler->ack(irq);
471                 /*
472                  * REPLAY is when Linux resends an IRQ that was dropped earlier
473                  * WAITING is used by probe to mark irqs that are being tested
474                  */
475                 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
476                 status |= IRQ_PENDING; /* we _want_ to handle it */
477
478                 /*
479                  * If the IRQ is disabled for whatever reason, we cannot
480                  * use the action we have.
481                  */
482                 action = NULL;
483                 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
484                         action = desc->action;
485                         status &= ~IRQ_PENDING; /* we commit to handling */
486                         status |= IRQ_INPROGRESS; /* we are handling it */
487                 }
488                 desc->status = status;
489
490                 /*
491                  * If there is no IRQ handler or it was disabled, exit early.
492                  * Since we set PENDING, if another processor is handling
493                  * a different instance of this same irq, the other processor
494                  * will take care of it.
495                  */
496                 if (unlikely(!action))
497                         goto out;
498
499                 /*
500                  * Edge triggered interrupts need to remember
501                  * pending events.
502                  * This applies to any hw interrupts that allow a second
503                  * instance of the same irq to arrive while we are in do_IRQ
504                  * or in the handler. But the code here only handles the _second_
505                  * instance of the irq, not the third or fourth. So it is mostly
506                  * useful for irq hardware that does not mask cleanly in an
507                  * SMP environment.
508                  */
509                 for (;;) {
510                         spin_unlock(&desc->lock);
511                         action_ret = handle_IRQ_event(irq, regs, action);
512                         spin_lock(&desc->lock);
513                         if (!noirqdebug)
514                                 note_interrupt(irq, desc, action_ret);
515                         if (!(desc->status & IRQ_PENDING))
516                                 break;
517                         desc->status &= ~IRQ_PENDING;
518                 }
519                 desc->status &= ~IRQ_INPROGRESS;
520           out:
521                 /*
522                  * The ->end() handler has to deal with interrupts which got
523                  * disabled while the handler was running.
524                  */
525                 desc->handler->end(irq);
526                 spin_unlock(&desc->lock);
527         }
528         irq_exit();
529         return 1;
530 }
531
532 /**
533  *      request_irq - allocate an interrupt line
534  *      @irq: Interrupt line to allocate
535  *      @handler: Function to be called when the IRQ occurs
536  *      @irqflags: Interrupt type flags
537  *      @devname: An ascii name for the claiming device
538  *      @dev_id: A cookie passed back to the handler function
539  *
540  *      This call allocates interrupt resources and enables the
541  *      interrupt line and IRQ handling. From the point this
542  *      call is made your handler function may be invoked. Since
543  *      your handler function must clear any interrupt the board 
544  *      raises, you must take care both to initialise your hardware
545  *      and to set up the interrupt handler in the right order.
546  *
547  *      Dev_id must be globally unique. Normally the address of the
548  *      device data structure is used as the cookie. Since the handler
549  *      receives this value it makes sense to use it.
550  *
551  *      If your interrupt is shared you must pass a non NULL dev_id
552  *      as this is required when freeing the interrupt.
553  *
554  *      Flags:
555  *
556  *      SA_SHIRQ                Interrupt is shared
557  *
558  *      SA_INTERRUPT            Disable local interrupts while processing
559  *
560  *      SA_SAMPLE_RANDOM        The interrupt can be used for entropy
561  *
562  */
563
564 int request_irq(unsigned int irq,
565                 irqreturn_t (*handler)(int, void *, struct pt_regs *),
566                 unsigned long irqflags,
567                 const char * devname,
568                 void *dev_id)
569 {
570         int retval;
571         struct irqaction * action;
572
573 #if 1
574         /*
575          * Sanity-check: shared interrupts should REALLY pass in
576          * a real dev-ID, otherwise we'll have trouble later trying
577          * to figure out which interrupt is which (messes up the
578          * interrupt freeing logic etc).
579          */
580         if (irqflags & SA_SHIRQ) {
581                 if (!dev_id)
582                         printk(KERN_ERR "Bad boy: %s called us without a dev_id!\n", devname);
583         }
584 #endif
585
586         if (irq >= NR_IRQS)
587                 return -EINVAL;
588         if (!handler)
589                 return -EINVAL;
590
591         action = (struct irqaction *)
592                         kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
593         if (!action)
594                 return -ENOMEM;
595
596         action->handler = handler;
597         action->flags = irqflags;
598         action->mask = 0;
599         action->name = devname;
600         action->next = NULL;
601         action->dev_id = dev_id;
602
603         retval = setup_irq(irq, action);
604         if (retval)
605                 kfree(action);
606         return retval;
607 }
608
609 EXPORT_SYMBOL(request_irq);
610
611 /**
612  *      free_irq - free an interrupt
613  *      @irq: Interrupt line to free
614  *      @dev_id: Device identity to free
615  *
616  *      Remove an interrupt handler. The handler is removed and if the
617  *      interrupt line is no longer in use by any driver it is disabled.
618  *      On a shared IRQ the caller must ensure the interrupt is disabled
619  *      on the card it drives before calling this function. The function
620  *      does not return until any executing interrupts for this IRQ
621  *      have completed.
622  *
623  *      This function must not be called from interrupt context.
624  */
625
626 void free_irq(unsigned int irq, void *dev_id)
627 {
628         irq_desc_t *desc;
629         struct irqaction **p;
630         unsigned long flags;
631
632         if (irq >= NR_IRQS)
633                 return;
634
635         desc = irq_descp(irq);
636         spin_lock_irqsave(&desc->lock,flags);
637         p = &desc->action;
638         for (;;) {
639                 struct irqaction * action = *p;
640                 if (action) {
641                         struct irqaction **pp = p;
642                         p = &action->next;
643                         if (action->dev_id != dev_id)
644                                 continue;
645
646                         /* Found it - now remove it from the list of entries */
647                         *pp = action->next;
648                         if (!desc->action) {
649                                 desc->status |= IRQ_DISABLED;
650                                 desc->handler->shutdown(irq);
651                         }
652                         spin_unlock_irqrestore(&desc->lock,flags);
653
654                         /* Wait to make sure it's not being used on another CPU */
655                         synchronize_irq(irq);
656                         kfree(action);
657                         return;
658                 }
659                 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
660                 spin_unlock_irqrestore(&desc->lock,flags);
661                 return;
662         }
663 }
664
665 EXPORT_SYMBOL(free_irq);
666
667 /*
668  * IRQ autodetection code..
669  *
670  * This depends on the fact that any interrupt that
671  * comes in on to an unassigned handler will get stuck
672  * with "IRQ_WAITING" cleared and the interrupt
673  * disabled.
674  */
675
676 static DECLARE_MUTEX(probe_sem);
677
678 /**
679  *      probe_irq_on    - begin an interrupt autodetect
680  *
681  *      Commence probing for an interrupt. The interrupts are scanned
682  *      and a mask of potential interrupt lines is returned.
683  *
684  */
685
686 unsigned long probe_irq_on(void)
687 {
688         unsigned int i;
689         irq_desc_t *desc;
690         unsigned long val;
691         unsigned long delay;
692
693         down(&probe_sem);
694         /*
695          * something may have generated an irq long ago and we want to
696          * flush such a longstanding irq before considering it as spurious.
697          */
698         for (i = NR_IRQS-1; i > 0; i--)  {
699                 desc = irq_descp(i);
700
701                 spin_lock_irq(&desc->lock);
702                 if (!desc->action)
703                         desc->handler->startup(i);
704                 spin_unlock_irq(&desc->lock);
705         }
706
707         /* Wait for longstanding interrupts to trigger. */
708         for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
709                 /* about 20ms delay */ barrier();
710
711         /*
712          * enable any unassigned irqs
713          * (we must startup again here because if a longstanding irq
714          * happened in the previous stage, it may have masked itself)
715          */
716         for (i = NR_IRQS-1; i > 0; i--) {
717                 desc = irq_descp(i);
718
719                 spin_lock_irq(&desc->lock);
720                 if (!desc->action) {
721                         desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
722                         if (desc->handler->startup(i))
723                                 desc->status |= IRQ_PENDING;
724                 }
725                 spin_unlock_irq(&desc->lock);
726         }
727
728         /*
729          * Wait for spurious interrupts to trigger
730          */
731         for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
732                 /* about 100ms delay */ barrier();
733
734         /*
735          * Now filter out any obviously spurious interrupts
736          */
737         val = 0;
738         for (i = 0; i < NR_IRQS; i++) {
739                 irq_desc_t *desc = irq_descp(i);
740                 unsigned int status;
741
742                 spin_lock_irq(&desc->lock);
743                 status = desc->status;
744
745                 if (status & IRQ_AUTODETECT) {
746                         /* It triggered already - consider it spurious. */
747                         if (!(status & IRQ_WAITING)) {
748                                 desc->status = status & ~IRQ_AUTODETECT;
749                                 desc->handler->shutdown(i);
750                         } else
751                                 if (i < 32)
752                                         val |= 1 << i;
753                 }
754                 spin_unlock_irq(&desc->lock);
755         }
756
757         return val;
758 }
759
760 EXPORT_SYMBOL(probe_irq_on);
761
762 /**
763  *      probe_irq_mask - scan a bitmap of interrupt lines
764  *      @val:   mask of interrupts to consider
765  *
766  *      Scan the ISA bus interrupt lines and return a bitmap of
767  *      active interrupts. The interrupt probe logic state is then
768  *      returned to its previous value.
769  *
770  *      Note: we need to scan all the irq's even though we will
771  *      only return ISA irq numbers - just so that we reset them
772  *      all to a known state.
773  */
774 unsigned int probe_irq_mask(unsigned long val)
775 {
776         int i;
777         unsigned int mask;
778
779         mask = 0;
780         for (i = 0; i < 16; i++) {
781                 irq_desc_t *desc = irq_descp(i);
782                 unsigned int status;
783
784                 spin_lock_irq(&desc->lock);
785                 status = desc->status;
786
787                 if (status & IRQ_AUTODETECT) {
788                         if (!(status & IRQ_WAITING))
789                                 mask |= 1 << i;
790
791                         desc->status = status & ~IRQ_AUTODETECT;
792                         desc->handler->shutdown(i);
793                 }
794                 spin_unlock_irq(&desc->lock);
795         }
796         up(&probe_sem);
797
798         return mask & val;
799 }
800 EXPORT_SYMBOL(probe_irq_mask);
801
802 /**
803  *      probe_irq_off   - end an interrupt autodetect
804  *      @val: mask of potential interrupts (unused)
805  *
806  *      Scans the unused interrupt lines and returns the line which
807  *      appears to have triggered the interrupt. If no interrupt was
808  *      found then zero is returned. If more than one interrupt is
809  *      found then minus the first candidate is returned to indicate
810  *      their is doubt.
811  *
812  *      The interrupt probe logic state is returned to its previous
813  *      value.
814  *
815  *      BUGS: When used in a module (which arguably shouldn't happen)
816  *      nothing prevents two IRQ probe callers from overlapping. The
817  *      results of this are non-optimal.
818  */
819
820 int probe_irq_off(unsigned long val)
821 {
822         int i, irq_found, nr_irqs;
823
824         nr_irqs = 0;
825         irq_found = 0;
826         for (i = 0; i < NR_IRQS; i++) {
827                 irq_desc_t *desc = irq_descp(i);
828                 unsigned int status;
829
830                 spin_lock_irq(&desc->lock);
831                 status = desc->status;
832
833                 if (status & IRQ_AUTODETECT) {
834                         if (!(status & IRQ_WAITING)) {
835                                 if (!nr_irqs)
836                                         irq_found = i;
837                                 nr_irqs++;
838                         }
839                         desc->status = status & ~IRQ_AUTODETECT;
840                         desc->handler->shutdown(i);
841                 }
842                 spin_unlock_irq(&desc->lock);
843         }
844         up(&probe_sem);
845
846         if (nr_irqs > 1)
847                 irq_found = -irq_found;
848         return irq_found;
849 }
850
851 EXPORT_SYMBOL(probe_irq_off);
852
853 int setup_irq(unsigned int irq, struct irqaction * new)
854 {
855         int shared = 0;
856         unsigned long flags;
857         struct irqaction *old, **p;
858         irq_desc_t *desc = irq_descp(irq);
859
860         if (desc->handler == &no_irq_type)
861                 return -ENOSYS;
862         /*
863          * Some drivers like serial.c use request_irq() heavily,
864          * so we have to be careful not to interfere with a
865          * running system.
866          */
867         if (new->flags & SA_SAMPLE_RANDOM) {
868                 /*
869                  * This function might sleep, we want to call it first,
870                  * outside of the atomic block.
871                  * Yes, this might clear the entropy pool if the wrong
872                  * driver is attempted to be loaded, without actually
873                  * installing a new handler, but is this really a problem,
874                  * only the sysadmin is able to do this.
875                  */
876                 rand_initialize_irq(irq);
877         }
878
879         if (new->flags & SA_PERCPU_IRQ) {
880                 desc->status |= IRQ_PER_CPU;
881                 desc->handler = &irq_type_ia64_lsapic;
882         }
883
884         /*
885          * The following block of code has to be executed atomically
886          */
887         spin_lock_irqsave(&desc->lock,flags);
888         p = &desc->action;
889         if ((old = *p) != NULL) {
890                 /* Can't share interrupts unless both agree to */
891                 if (!(old->flags & new->flags & SA_SHIRQ)) {
892                         spin_unlock_irqrestore(&desc->lock,flags);
893                         return -EBUSY;
894                 }
895
896                 /* add new interrupt at end of irq queue */
897                 do {
898                         p = &old->next;
899                         old = *p;
900                 } while (old);
901                 shared = 1;
902         }
903
904         *p = new;
905
906         if (!shared) {
907                 desc->depth = 0;
908                 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING | IRQ_INPROGRESS);
909                 desc->handler->startup(irq);
910         }
911         spin_unlock_irqrestore(&desc->lock,flags);
912
913         register_irq_proc(irq);
914         return 0;
915 }
916
917 static struct proc_dir_entry * root_irq_dir;
918 static struct proc_dir_entry * irq_dir [NR_IRQS];
919
920 #ifdef CONFIG_SMP
921
922 static struct proc_dir_entry * smp_affinity_entry [NR_IRQS];
923
924 static cpumask_t irq_affinity [NR_IRQS] = { [0 ... NR_IRQS-1] = CPU_MASK_ALL };
925
926 static char irq_redir [NR_IRQS]; // = { [0 ... NR_IRQS-1] = 1 };
927
928 void set_irq_affinity_info (unsigned int irq, int hwid, int redir)
929 {
930         cpumask_t mask = CPU_MASK_NONE;
931
932         cpu_set(cpu_logical_id(hwid), mask);
933
934         if (irq < NR_IRQS) {
935                 irq_affinity[irq] = mask;
936                 irq_redir[irq] = (char) (redir & 0xff);
937         }
938 }
939
940 static int irq_affinity_read_proc (char *page, char **start, off_t off,
941                         int count, int *eof, void *data)
942 {
943         int len = cpumask_scnprintf(page, count, irq_affinity[(long)data]);
944         if (count - len < 2)
945                 return -EINVAL;
946         len += sprintf(page + len, "\n");
947         return len;
948 }
949
950 static int irq_affinity_write_proc (struct file *file, const char *buffer,
951                                     unsigned long count, void *data)
952 {
953         unsigned int irq = (unsigned long) data;
954         int full_count = count, err;
955         cpumask_t new_value, tmp;
956 #       define R_PREFIX_LEN 16
957         char rbuf[R_PREFIX_LEN];
958         int rlen;
959         int prelen;
960         irq_desc_t *desc = irq_descp(irq);
961
962         if (!desc->handler->set_affinity)
963                 return -EIO;
964
965         /*
966          * If string being written starts with a prefix of 'r' or 'R'
967          * and some limited number of spaces, set IA64_IRQ_REDIRECTED.
968          * If more than (R_PREFIX_LEN - 2) spaces are passed, they won't
969          * all be trimmed as part of prelen, the untrimmed spaces will
970          * cause the hex parsing to fail, and this write() syscall will
971          * fail with EINVAL.
972          */
973
974         if (!count)
975                 return -EINVAL;
976         rlen = min(sizeof(rbuf)-1, count);
977         if (copy_from_user(rbuf, buffer, rlen))
978                 return -EFAULT;
979         rbuf[rlen] = 0;
980         prelen = 0;
981         if (tolower(*rbuf) == 'r') {
982                 prelen = strspn(rbuf, "Rr ");
983                 irq |= IA64_IRQ_REDIRECTED;
984         }
985
986         err = cpumask_parse(buffer+prelen, count-prelen, new_value);
987         if (err)
988                 return err;
989
990         /*
991          * Do not allow disabling IRQs completely - it's a too easy
992          * way to make the system unusable accidentally :-) At least
993          * one online CPU still has to be targeted.
994          */
995         cpus_and(tmp, new_value, cpu_online_map);
996         if (cpus_empty(tmp))
997                 return -EINVAL;
998
999         desc->handler->set_affinity(irq, new_value);
1000         return full_count;
1001 }
1002
1003 #endif /* CONFIG_SMP */
1004
1005 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
1006                         int count, int *eof, void *data)
1007 {
1008         int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
1009         if (count - len < 2)
1010                 return -EINVAL;
1011         len += sprintf(page + len, "\n");
1012         return len;
1013 }
1014
1015 static int prof_cpu_mask_write_proc (struct file *file, const char *buffer,
1016                                         unsigned long count, void *data)
1017 {
1018         cpumask_t *mask = (cpumask_t *)data;
1019         unsigned long full_count = count, err;
1020         cpumask_t new_value;
1021
1022         err = cpumask_parse(buffer, count, new_value);
1023         if (err)
1024                 return err;
1025
1026         *mask = new_value;
1027         return full_count;
1028 }
1029
1030 #define MAX_NAMELEN 10
1031
1032 static void register_irq_proc (unsigned int irq)
1033 {
1034         char name [MAX_NAMELEN];
1035
1036         if (!root_irq_dir || (irq_descp(irq)->handler == &no_irq_type) || irq_dir[irq])
1037                 return;
1038
1039         memset(name, 0, MAX_NAMELEN);
1040         sprintf(name, "%d", irq);
1041
1042         /* create /proc/irq/1234 */
1043         irq_dir[irq] = proc_mkdir(name, root_irq_dir);
1044
1045 #ifdef CONFIG_SMP
1046         {
1047                 struct proc_dir_entry *entry;
1048
1049                 /* create /proc/irq/1234/smp_affinity */
1050                 entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
1051
1052                 if (entry) {
1053                         entry->nlink = 1;
1054                         entry->data = (void *)(long)irq;
1055                         entry->read_proc = irq_affinity_read_proc;
1056                         entry->write_proc = irq_affinity_write_proc;
1057                 }
1058
1059                 smp_affinity_entry[irq] = entry;
1060         }
1061 #endif
1062 }
1063
1064 cpumask_t prof_cpu_mask = CPU_MASK_ALL;
1065
1066 void init_irq_proc (void)
1067 {
1068         struct proc_dir_entry *entry;
1069         int i;
1070
1071         /* create /proc/irq */
1072         root_irq_dir = proc_mkdir("irq", 0);
1073
1074         /* create /proc/irq/prof_cpu_mask */
1075         entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
1076
1077         if (!entry)
1078                 return;
1079
1080         entry->nlink = 1;
1081         entry->data = (void *)&prof_cpu_mask;
1082         entry->read_proc = prof_cpu_mask_read_proc;
1083         entry->write_proc = prof_cpu_mask_write_proc;
1084
1085         /*
1086          * Create entries for all existing IRQs.
1087          */
1088         for (i = 0; i < NR_IRQS; i++) {
1089                 if (irq_descp(i)->handler == &no_irq_type)
1090                         continue;
1091                 register_irq_proc(i);
1092         }
1093 }