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