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