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