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