upgrade to linux 2.6.10-1.12_FC2
[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 #include <linux/bitops.h>
47
48 #include <asm/atomic.h>
49 #include <asm/cpu.h>
50 #include <asm/io.h>
51 #include <asm/smp.h>
52 #include <asm/system.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 #ifdef CONFIG_X86_LOCAL_APIC
219                 seq_puts(p, "LOC: ");
220                 for (j = 0; j < NR_CPUS; j++)
221                         if (cpu_online(j))
222                                 seq_printf(p, "%10u ", irq_stat[j].apic_timer_irqs);
223                 seq_putc(p, '\n');
224 #endif
225                 seq_printf(p, "ERR: %10u\n", atomic_read(&irq_err_count));
226 #ifdef CONFIG_X86_IO_APIC
227 #ifdef APIC_MISMATCH_DEBUG
228                 seq_printf(p, "MIS: %10u\n", atomic_read(&irq_mis_count));
229 #endif
230 #endif
231         }
232         return 0;
233 }
234
235 #ifdef CONFIG_SMP
236 inline void synchronize_irq(unsigned int irq)
237 {
238         while (irq_descp(irq)->status & IRQ_INPROGRESS)
239                 cpu_relax();
240 }
241 EXPORT_SYMBOL(synchronize_irq);
242 #endif
243
244 /*
245  * This should really return information about whether
246  * we should do bottom half handling etc. Right now we
247  * end up _always_ checking the bottom half, which is a
248  * waste of time and is not what some drivers would
249  * prefer.
250  */
251 int handle_IRQ_event(unsigned int irq,
252                 struct pt_regs *regs, struct irqaction *action)
253 {
254         int status = 1; /* Force the "do bottom halves" bit */
255         int ret, retval = 0;
256
257         if (!(action->flags & SA_INTERRUPT))
258                 local_irq_enable();
259
260         do {
261                 ret = action->handler(irq, action->dev_id, regs);
262                 if (ret == IRQ_HANDLED)
263                         status |= action->flags;
264                 retval |= ret;
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         cpus_clear(action->mask);
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 __user *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         int redir = 0;
977
978         if (!desc->handler->set_affinity)
979                 return -EIO;
980
981         /*
982          * If string being written starts with a prefix of 'r' or 'R'
983          * and some limited number of spaces, set IA64_IRQ_REDIRECTED.
984          * If more than (R_PREFIX_LEN - 2) spaces are passed, they won't
985          * all be trimmed as part of prelen, the untrimmed spaces will
986          * cause the hex parsing to fail, and this write() syscall will
987          * fail with EINVAL.
988          */
989
990         if (!count)
991                 return -EINVAL;
992         rlen = min(sizeof(rbuf)-1, count);
993         if (copy_from_user(rbuf, buffer, rlen))
994                 return -EFAULT;
995         rbuf[rlen] = 0;
996         prelen = 0;
997         if (tolower(*rbuf) == 'r') {
998                 prelen = strspn(rbuf, "Rr ");
999                 redir++;
1000         }
1001
1002         err = cpumask_parse(buffer+prelen, count-prelen, new_value);
1003         if (err)
1004                 return err;
1005
1006         /*
1007          * Do not allow disabling IRQs completely - it's a too easy
1008          * way to make the system unusable accidentally :-) At least
1009          * one online CPU still has to be targeted.
1010          */
1011         cpus_and(tmp, new_value, cpu_online_map);
1012         if (cpus_empty(tmp))
1013                 return -EINVAL;
1014
1015         spin_lock_irqsave(&desc->lock, flags);
1016         pending_irq_cpumask[irq] = new_value;
1017         if (redir)
1018                 set_bit(irq, pending_irq_redir);
1019         else
1020                 clear_bit(irq, pending_irq_redir);
1021         spin_unlock_irqrestore(&desc->lock, flags);
1022
1023         return full_count;
1024 }
1025
1026 void move_irq(int irq)
1027 {
1028         /* note - we hold desc->lock */
1029         cpumask_t tmp;
1030         irq_desc_t *desc = irq_descp(irq);
1031         int redir = test_bit(irq, pending_irq_redir);
1032
1033         if (!cpus_empty(pending_irq_cpumask[irq])) {
1034                 cpus_and(tmp, pending_irq_cpumask[irq], cpu_online_map);
1035                 if (unlikely(!cpus_empty(tmp))) {
1036                         desc->handler->set_affinity(irq | (redir ? IA64_IRQ_REDIRECTED : 0),
1037                                                     pending_irq_cpumask[irq]);
1038                 }
1039                 cpus_clear(pending_irq_cpumask[irq]);
1040         }
1041 }
1042
1043
1044 #endif /* CONFIG_SMP */
1045
1046 #ifdef CONFIG_HOTPLUG_CPU
1047 unsigned int vectors_in_migration[NR_IRQS];
1048
1049 /*
1050  * Since cpu_online_map is already updated, we just need to check for
1051  * affinity that has zeros
1052  */
1053 static void migrate_irqs(void)
1054 {
1055         cpumask_t       mask;
1056         irq_desc_t *desc;
1057         int             irq, new_cpu;
1058
1059         for (irq=0; irq < NR_IRQS; irq++) {
1060                 desc = irq_descp(irq);
1061
1062                 /*
1063                  * No handling for now.
1064                  * TBD: Implement a disable function so we can now
1065                  * tell CPU not to respond to these local intr sources.
1066                  * such as ITV,CPEI,MCA etc.
1067                  */
1068                 if (desc->status == IRQ_PER_CPU)
1069                         continue;
1070
1071                 cpus_and(mask, irq_affinity[irq], cpu_online_map);
1072                 if (any_online_cpu(mask) == NR_CPUS) {
1073                         /*
1074                          * Save it for phase 2 processing
1075                          */
1076                         vectors_in_migration[irq] = irq;
1077
1078                         new_cpu = any_online_cpu(cpu_online_map);
1079                         mask = cpumask_of_cpu(new_cpu);
1080
1081                         /*
1082                          * Al three are essential, currently WARN_ON.. maybe panic?
1083                          */
1084                         if (desc->handler && desc->handler->disable &&
1085                                 desc->handler->enable && desc->handler->set_affinity) {
1086                                 desc->handler->disable(irq);
1087                                 desc->handler->set_affinity(irq, mask);
1088                                 desc->handler->enable(irq);
1089                         } else {
1090                                 WARN_ON((!(desc->handler) || !(desc->handler->disable) ||
1091                                                 !(desc->handler->enable) ||
1092                                                 !(desc->handler->set_affinity)));
1093                         }
1094                 }
1095         }
1096 }
1097
1098 void fixup_irqs(void)
1099 {
1100         unsigned int irq;
1101         extern void ia64_process_pending_intr(void);
1102
1103         ia64_set_itv(1<<16);
1104         /*
1105          * Phase 1: Locate irq's bound to this cpu and
1106          * relocate them for cpu removal.
1107          */
1108         migrate_irqs();
1109
1110         /*
1111          * Phase 2: Perform interrupt processing for all entries reported in
1112          * local APIC.
1113          */
1114         ia64_process_pending_intr();
1115
1116         /*
1117          * Phase 3: Now handle any interrupts not captured in local APIC.
1118          * This is to account for cases that device interrupted during the time the
1119          * rte was being disabled and re-programmed.
1120          */
1121         for (irq=0; irq < NR_IRQS; irq++) {
1122                 if (vectors_in_migration[irq]) {
1123                         vectors_in_migration[irq]=0;
1124                         do_IRQ(irq, NULL);
1125                 }
1126         }
1127
1128         /*
1129          * Now let processor die. We do irq disable and max_xtp() to
1130          * ensure there is no more interrupts routed to this processor.
1131          * But the local timer interrupt can have 1 pending which we
1132          * take care in timer_interrupt().
1133          */
1134         max_xtp();
1135         local_irq_disable();
1136 }
1137 #endif
1138
1139 #define MAX_NAMELEN 10
1140
1141 static void register_irq_proc (unsigned int irq)
1142 {
1143         char name [MAX_NAMELEN];
1144
1145         if (!root_irq_dir || (irq_descp(irq)->handler == &no_irq_type) || irq_dir[irq])
1146                 return;
1147
1148         memset(name, 0, MAX_NAMELEN);
1149         sprintf(name, "%d", irq);
1150
1151         /* create /proc/irq/1234 */
1152         irq_dir[irq] = proc_mkdir(name, root_irq_dir);
1153
1154 #ifdef CONFIG_SMP
1155         {
1156                 struct proc_dir_entry *entry;
1157
1158                 /* create /proc/irq/1234/smp_affinity */
1159                 entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
1160
1161                 if (entry) {
1162                         entry->nlink = 1;
1163                         entry->data = (void *)(long)irq;
1164                         entry->read_proc = irq_affinity_read_proc;
1165                         entry->write_proc = irq_affinity_write_proc;
1166                 }
1167
1168                 smp_affinity_entry[irq] = entry;
1169         }
1170 #endif
1171 }
1172
1173 void init_irq_proc (void)
1174 {
1175         int i;
1176
1177         /* create /proc/irq */
1178         root_irq_dir = proc_mkdir("irq", NULL);
1179
1180         /* create /proc/irq/prof_cpu_mask */
1181         create_prof_cpu_mask(root_irq_dir);
1182
1183         /*
1184          * Create entries for all existing IRQs.
1185          */
1186         for (i = 0; i < NR_IRQS; i++) {
1187                 if (irq_descp(i)->handler == &no_irq_type)
1188                         continue;
1189                 register_irq_proc(i);
1190         }
1191 }