Pull acpi_bus_register_driver into release branch
[linux-2.6] / arch / arm / kernel / irq.c
1 /*
2  *  linux/arch/arm/kernel/irq.c
3  *
4  *  Copyright (C) 1992 Linus Torvalds
5  *  Modifications for ARM processor Copyright (C) 1995-2000 Russell King.
6  *
7  *  Support for Dynamic Tick Timer Copyright (C) 2004-2005 Nokia Corporation.
8  *  Dynamic Tick Timer written by Tony Lindgren <tony@atomide.com> and
9  *  Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  *
15  *  This file contains the code used by various IRQ handling routines:
16  *  asking for different IRQ's should be done through these routines
17  *  instead of just grabbing them. Thus setups with different IRQ numbers
18  *  shouldn't result in any weird surprises, and installing new handlers
19  *  should be easier.
20  *
21  *  IRQ's are in fact implemented a bit like signal handlers for the kernel.
22  *  Naturally it's not a 1:1 relation, but there are similarities.
23  */
24 #include <linux/config.h>
25 #include <linux/kernel_stat.h>
26 #include <linux/module.h>
27 #include <linux/signal.h>
28 #include <linux/ioport.h>
29 #include <linux/interrupt.h>
30 #include <linux/ptrace.h>
31 #include <linux/slab.h>
32 #include <linux/random.h>
33 #include <linux/smp.h>
34 #include <linux/init.h>
35 #include <linux/seq_file.h>
36 #include <linux/errno.h>
37 #include <linux/list.h>
38 #include <linux/kallsyms.h>
39 #include <linux/proc_fs.h>
40
41 #include <asm/irq.h>
42 #include <asm/system.h>
43 #include <asm/mach/irq.h>
44 #include <asm/mach/time.h>
45
46 /*
47  * Maximum IRQ count.  Currently, this is arbitary.  However, it should
48  * not be set too low to prevent false triggering.  Conversely, if it
49  * is set too high, then you could miss a stuck IRQ.
50  *
51  * Maybe we ought to set a timer and re-enable the IRQ at a later time?
52  */
53 #define MAX_IRQ_CNT     100000
54
55 static int noirqdebug;
56 static volatile unsigned long irq_err_count;
57 static DEFINE_SPINLOCK(irq_controller_lock);
58 static LIST_HEAD(irq_pending);
59
60 struct irqdesc irq_desc[NR_IRQS];
61 void (*init_arch_irq)(void) __initdata = NULL;
62
63 /*
64  * No architecture-specific irq_finish function defined in arm/arch/irqs.h.
65  */
66 #ifndef irq_finish
67 #define irq_finish(irq) do { } while (0)
68 #endif
69
70 /*
71  * Dummy mask/unmask handler
72  */
73 void dummy_mask_unmask_irq(unsigned int irq)
74 {
75 }
76
77 irqreturn_t no_action(int irq, void *dev_id, struct pt_regs *regs)
78 {
79         return IRQ_NONE;
80 }
81
82 void do_bad_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
83 {
84         irq_err_count += 1;
85         printk(KERN_ERR "IRQ: spurious interrupt %d\n", irq);
86 }
87
88 static struct irqchip bad_chip = {
89         .ack    = dummy_mask_unmask_irq,
90         .mask   = dummy_mask_unmask_irq,
91         .unmask = dummy_mask_unmask_irq,
92 };
93
94 static struct irqdesc bad_irq_desc = {
95         .chip           = &bad_chip,
96         .handle         = do_bad_IRQ,
97         .pend           = LIST_HEAD_INIT(bad_irq_desc.pend),
98         .disable_depth  = 1,
99 };
100
101 #ifdef CONFIG_SMP
102 void synchronize_irq(unsigned int irq)
103 {
104         struct irqdesc *desc = irq_desc + irq;
105
106         while (desc->running)
107                 barrier();
108 }
109 EXPORT_SYMBOL(synchronize_irq);
110
111 #define smp_set_running(desc)   do { desc->running = 1; } while (0)
112 #define smp_clear_running(desc) do { desc->running = 0; } while (0)
113 #else
114 #define smp_set_running(desc)   do { } while (0)
115 #define smp_clear_running(desc) do { } while (0)
116 #endif
117
118 /**
119  *      disable_irq_nosync - disable an irq without waiting
120  *      @irq: Interrupt to disable
121  *
122  *      Disable the selected interrupt line.  Enables and disables
123  *      are nested.  We do this lazily.
124  *
125  *      This function may be called from IRQ context.
126  */
127 void disable_irq_nosync(unsigned int irq)
128 {
129         struct irqdesc *desc = irq_desc + irq;
130         unsigned long flags;
131
132         spin_lock_irqsave(&irq_controller_lock, flags);
133         desc->disable_depth++;
134         list_del_init(&desc->pend);
135         spin_unlock_irqrestore(&irq_controller_lock, flags);
136 }
137 EXPORT_SYMBOL(disable_irq_nosync);
138
139 /**
140  *      disable_irq - disable an irq and wait for completion
141  *      @irq: Interrupt to disable
142  *
143  *      Disable the selected interrupt line.  Enables and disables
144  *      are nested.  This functions waits for any pending IRQ
145  *      handlers for this interrupt to complete before returning.
146  *      If you use this function while holding a resource the IRQ
147  *      handler may need you will deadlock.
148  *
149  *      This function may be called - with care - from IRQ context.
150  */
151 void disable_irq(unsigned int irq)
152 {
153         struct irqdesc *desc = irq_desc + irq;
154
155         disable_irq_nosync(irq);
156         if (desc->action)
157                 synchronize_irq(irq);
158 }
159 EXPORT_SYMBOL(disable_irq);
160
161 /**
162  *      enable_irq - enable interrupt handling on an irq
163  *      @irq: Interrupt to enable
164  *
165  *      Re-enables the processing of interrupts on this IRQ line.
166  *      Note that this may call the interrupt handler, so you may
167  *      get unexpected results if you hold IRQs disabled.
168  *
169  *      This function may be called from IRQ context.
170  */
171 void enable_irq(unsigned int irq)
172 {
173         struct irqdesc *desc = irq_desc + irq;
174         unsigned long flags;
175
176         spin_lock_irqsave(&irq_controller_lock, flags);
177         if (unlikely(!desc->disable_depth)) {
178                 printk("enable_irq(%u) unbalanced from %p\n", irq,
179                         __builtin_return_address(0));
180         } else if (!--desc->disable_depth) {
181                 desc->probing = 0;
182                 desc->chip->unmask(irq);
183
184                 /*
185                  * If the interrupt is waiting to be processed,
186                  * try to re-run it.  We can't directly run it
187                  * from here since the caller might be in an
188                  * interrupt-protected region.
189                  */
190                 if (desc->pending && list_empty(&desc->pend)) {
191                         desc->pending = 0;
192                         if (!desc->chip->retrigger ||
193                             desc->chip->retrigger(irq))
194                                 list_add(&desc->pend, &irq_pending);
195                 }
196         }
197         spin_unlock_irqrestore(&irq_controller_lock, flags);
198 }
199 EXPORT_SYMBOL(enable_irq);
200
201 /*
202  * Enable wake on selected irq
203  */
204 void enable_irq_wake(unsigned int irq)
205 {
206         struct irqdesc *desc = irq_desc + irq;
207         unsigned long flags;
208
209         spin_lock_irqsave(&irq_controller_lock, flags);
210         if (desc->chip->set_wake)
211                 desc->chip->set_wake(irq, 1);
212         spin_unlock_irqrestore(&irq_controller_lock, flags);
213 }
214 EXPORT_SYMBOL(enable_irq_wake);
215
216 void disable_irq_wake(unsigned int irq)
217 {
218         struct irqdesc *desc = irq_desc + irq;
219         unsigned long flags;
220
221         spin_lock_irqsave(&irq_controller_lock, flags);
222         if (desc->chip->set_wake)
223                 desc->chip->set_wake(irq, 0);
224         spin_unlock_irqrestore(&irq_controller_lock, flags);
225 }
226 EXPORT_SYMBOL(disable_irq_wake);
227
228 int show_interrupts(struct seq_file *p, void *v)
229 {
230         int i = *(loff_t *) v, cpu;
231         struct irqaction * action;
232         unsigned long flags;
233
234         if (i == 0) {
235                 char cpuname[12];
236
237                 seq_printf(p, "    ");
238                 for_each_present_cpu(cpu) {
239                         sprintf(cpuname, "CPU%d", cpu);
240                         seq_printf(p, " %10s", cpuname);
241                 }
242                 seq_putc(p, '\n');
243         }
244
245         if (i < NR_IRQS) {
246                 spin_lock_irqsave(&irq_controller_lock, flags);
247                 action = irq_desc[i].action;
248                 if (!action)
249                         goto unlock;
250
251                 seq_printf(p, "%3d: ", i);
252                 for_each_present_cpu(cpu)
253                         seq_printf(p, "%10u ", kstat_cpu(cpu).irqs[i]);
254                 seq_printf(p, "  %s", action->name);
255                 for (action = action->next; action; action = action->next)
256                         seq_printf(p, ", %s", action->name);
257
258                 seq_putc(p, '\n');
259 unlock:
260                 spin_unlock_irqrestore(&irq_controller_lock, flags);
261         } else if (i == NR_IRQS) {
262 #ifdef CONFIG_ARCH_ACORN
263                 show_fiq_list(p, v);
264 #endif
265 #ifdef CONFIG_SMP
266                 show_ipi_list(p);
267                 show_local_irqs(p);
268 #endif
269                 seq_printf(p, "Err: %10lu\n", irq_err_count);
270         }
271         return 0;
272 }
273
274 /*
275  * IRQ lock detection.
276  *
277  * Hopefully, this should get us out of a few locked situations.
278  * However, it may take a while for this to happen, since we need
279  * a large number if IRQs to appear in the same jiffie with the
280  * same instruction pointer (or within 2 instructions).
281  */
282 static int check_irq_lock(struct irqdesc *desc, int irq, struct pt_regs *regs)
283 {
284         unsigned long instr_ptr = instruction_pointer(regs);
285
286         if (desc->lck_jif == jiffies &&
287             desc->lck_pc >= instr_ptr && desc->lck_pc < instr_ptr + 8) {
288                 desc->lck_cnt += 1;
289
290                 if (desc->lck_cnt > MAX_IRQ_CNT) {
291                         printk(KERN_ERR "IRQ LOCK: IRQ%d is locking the system, disabled\n", irq);
292                         return 1;
293                 }
294         } else {
295                 desc->lck_cnt = 0;
296                 desc->lck_pc  = instruction_pointer(regs);
297                 desc->lck_jif = jiffies;
298         }
299         return 0;
300 }
301
302 static void
303 report_bad_irq(unsigned int irq, struct pt_regs *regs, struct irqdesc *desc, int ret)
304 {
305         static int count = 100;
306         struct irqaction *action;
307
308         if (noirqdebug)
309                 return;
310
311         if (ret != IRQ_HANDLED && ret != IRQ_NONE) {
312                 if (!count)
313                         return;
314                 count--;
315                 printk("irq%u: bogus retval mask %x\n", irq, ret);
316         } else {
317                 desc->irqs_unhandled++;
318                 if (desc->irqs_unhandled <= 99900)
319                         return;
320                 desc->irqs_unhandled = 0;
321                 printk("irq%u: nobody cared\n", irq);
322         }
323         show_regs(regs);
324         dump_stack();
325         printk(KERN_ERR "handlers:");
326         action = desc->action;
327         do {
328                 printk("\n" KERN_ERR "[<%p>]", action->handler);
329                 print_symbol(" (%s)", (unsigned long)action->handler);
330                 action = action->next;
331         } while (action);
332         printk("\n");
333 }
334
335 static int
336 __do_irq(unsigned int irq, struct irqaction *action, struct pt_regs *regs)
337 {
338         unsigned int status;
339         int ret, retval = 0;
340
341         spin_unlock(&irq_controller_lock);
342
343 #ifdef CONFIG_NO_IDLE_HZ
344         if (!(action->flags & SA_TIMER) && system_timer->dyn_tick != NULL) {
345                 write_seqlock(&xtime_lock);
346                 if (system_timer->dyn_tick->state & DYN_TICK_ENABLED)
347                         system_timer->dyn_tick->handler(irq, 0, regs);
348                 write_sequnlock(&xtime_lock);
349         }
350 #endif
351
352         if (!(action->flags & SA_INTERRUPT))
353                 local_irq_enable();
354
355         status = 0;
356         do {
357                 ret = action->handler(irq, action->dev_id, regs);
358                 if (ret == IRQ_HANDLED)
359                         status |= action->flags;
360                 retval |= ret;
361                 action = action->next;
362         } while (action);
363
364         if (status & SA_SAMPLE_RANDOM)
365                 add_interrupt_randomness(irq);
366
367         spin_lock_irq(&irq_controller_lock);
368
369         return retval;
370 }
371
372 /*
373  * This is for software-decoded IRQs.  The caller is expected to
374  * handle the ack, clear, mask and unmask issues.
375  */
376 void
377 do_simple_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
378 {
379         struct irqaction *action;
380         const unsigned int cpu = smp_processor_id();
381
382         desc->triggered = 1;
383
384         kstat_cpu(cpu).irqs[irq]++;
385
386         smp_set_running(desc);
387
388         action = desc->action;
389         if (action) {
390                 int ret = __do_irq(irq, action, regs);
391                 if (ret != IRQ_HANDLED)
392                         report_bad_irq(irq, regs, desc, ret);
393         }
394
395         smp_clear_running(desc);
396 }
397
398 /*
399  * Most edge-triggered IRQ implementations seem to take a broken
400  * approach to this.  Hence the complexity.
401  */
402 void
403 do_edge_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
404 {
405         const unsigned int cpu = smp_processor_id();
406
407         desc->triggered = 1;
408
409         /*
410          * If we're currently running this IRQ, or its disabled,
411          * we shouldn't process the IRQ.  Instead, turn on the
412          * hardware masks.
413          */
414         if (unlikely(desc->running || desc->disable_depth))
415                 goto running;
416
417         /*
418          * Acknowledge and clear the IRQ, but don't mask it.
419          */
420         desc->chip->ack(irq);
421
422         /*
423          * Mark the IRQ currently in progress.
424          */
425         desc->running = 1;
426
427         kstat_cpu(cpu).irqs[irq]++;
428
429         do {
430                 struct irqaction *action;
431
432                 action = desc->action;
433                 if (!action)
434                         break;
435
436                 if (desc->pending && !desc->disable_depth) {
437                         desc->pending = 0;
438                         desc->chip->unmask(irq);
439                 }
440
441                 __do_irq(irq, action, regs);
442         } while (desc->pending && !desc->disable_depth);
443
444         desc->running = 0;
445
446         /*
447          * If we were disabled or freed, shut down the handler.
448          */
449         if (likely(desc->action && !check_irq_lock(desc, irq, regs)))
450                 return;
451
452  running:
453         /*
454          * We got another IRQ while this one was masked or
455          * currently running.  Delay it.
456          */
457         desc->pending = 1;
458         desc->chip->mask(irq);
459         desc->chip->ack(irq);
460 }
461
462 /*
463  * Level-based IRQ handler.  Nice and simple.
464  */
465 void
466 do_level_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
467 {
468         struct irqaction *action;
469         const unsigned int cpu = smp_processor_id();
470
471         desc->triggered = 1;
472
473         /*
474          * Acknowledge, clear _AND_ disable the interrupt.
475          */
476         desc->chip->ack(irq);
477
478         if (likely(!desc->disable_depth)) {
479                 kstat_cpu(cpu).irqs[irq]++;
480
481                 smp_set_running(desc);
482
483                 /*
484                  * Return with this interrupt masked if no action
485                  */
486                 action = desc->action;
487                 if (action) {
488                         int ret = __do_irq(irq, desc->action, regs);
489
490                         if (ret != IRQ_HANDLED)
491                                 report_bad_irq(irq, regs, desc, ret);
492
493                         if (likely(!desc->disable_depth &&
494                                    !check_irq_lock(desc, irq, regs)))
495                                 desc->chip->unmask(irq);
496                 }
497
498                 smp_clear_running(desc);
499         }
500 }
501
502 static void do_pending_irqs(struct pt_regs *regs)
503 {
504         struct list_head head, *l, *n;
505
506         do {
507                 struct irqdesc *desc;
508
509                 /*
510                  * First, take the pending interrupts off the list.
511                  * The act of calling the handlers may add some IRQs
512                  * back onto the list.
513                  */
514                 head = irq_pending;
515                 INIT_LIST_HEAD(&irq_pending);
516                 head.next->prev = &head;
517                 head.prev->next = &head;
518
519                 /*
520                  * Now run each entry.  We must delete it from our
521                  * list before calling the handler.
522                  */
523                 list_for_each_safe(l, n, &head) {
524                         desc = list_entry(l, struct irqdesc, pend);
525                         list_del_init(&desc->pend);
526                         desc_handle_irq(desc - irq_desc, desc, regs);
527                 }
528
529                 /*
530                  * The list must be empty.
531                  */
532                 BUG_ON(!list_empty(&head));
533         } while (!list_empty(&irq_pending));
534 }
535
536 /*
537  * do_IRQ handles all hardware IRQ's.  Decoded IRQs should not
538  * come via this function.  Instead, they should provide their
539  * own 'handler'
540  */
541 asmlinkage void asm_do_IRQ(unsigned int irq, struct pt_regs *regs)
542 {
543         struct irqdesc *desc = irq_desc + irq;
544
545         /*
546          * Some hardware gives randomly wrong interrupts.  Rather
547          * than crashing, do something sensible.
548          */
549         if (irq >= NR_IRQS)
550                 desc = &bad_irq_desc;
551
552         irq_enter();
553         spin_lock(&irq_controller_lock);
554         desc_handle_irq(irq, desc, regs);
555
556         /*
557          * Now re-run any pending interrupts.
558          */
559         if (!list_empty(&irq_pending))
560                 do_pending_irqs(regs);
561
562         irq_finish(irq);
563
564         spin_unlock(&irq_controller_lock);
565         irq_exit();
566 }
567
568 void __set_irq_handler(unsigned int irq, irq_handler_t handle, int is_chained)
569 {
570         struct irqdesc *desc;
571         unsigned long flags;
572
573         if (irq >= NR_IRQS) {
574                 printk(KERN_ERR "Trying to install handler for IRQ%d\n", irq);
575                 return;
576         }
577
578         if (handle == NULL)
579                 handle = do_bad_IRQ;
580
581         desc = irq_desc + irq;
582
583         if (is_chained && desc->chip == &bad_chip)
584                 printk(KERN_WARNING "Trying to install chained handler for IRQ%d\n", irq);
585
586         spin_lock_irqsave(&irq_controller_lock, flags);
587         if (handle == do_bad_IRQ) {
588                 desc->chip->mask(irq);
589                 desc->chip->ack(irq);
590                 desc->disable_depth = 1;
591         }
592         desc->handle = handle;
593         if (handle != do_bad_IRQ && is_chained) {
594                 desc->valid = 0;
595                 desc->probe_ok = 0;
596                 desc->disable_depth = 0;
597                 desc->chip->unmask(irq);
598         }
599         spin_unlock_irqrestore(&irq_controller_lock, flags);
600 }
601
602 void set_irq_chip(unsigned int irq, struct irqchip *chip)
603 {
604         struct irqdesc *desc;
605         unsigned long flags;
606
607         if (irq >= NR_IRQS) {
608                 printk(KERN_ERR "Trying to install chip for IRQ%d\n", irq);
609                 return;
610         }
611
612         if (chip == NULL)
613                 chip = &bad_chip;
614
615         desc = irq_desc + irq;
616         spin_lock_irqsave(&irq_controller_lock, flags);
617         desc->chip = chip;
618         spin_unlock_irqrestore(&irq_controller_lock, flags);
619 }
620
621 int set_irq_type(unsigned int irq, unsigned int type)
622 {
623         struct irqdesc *desc;
624         unsigned long flags;
625         int ret = -ENXIO;
626
627         if (irq >= NR_IRQS) {
628                 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
629                 return -ENODEV;
630         }
631
632         desc = irq_desc + irq;
633         if (desc->chip->set_type) {
634                 spin_lock_irqsave(&irq_controller_lock, flags);
635                 ret = desc->chip->set_type(irq, type);
636                 spin_unlock_irqrestore(&irq_controller_lock, flags);
637         }
638
639         return ret;
640 }
641 EXPORT_SYMBOL(set_irq_type);
642
643 void set_irq_flags(unsigned int irq, unsigned int iflags)
644 {
645         struct irqdesc *desc;
646         unsigned long flags;
647
648         if (irq >= NR_IRQS) {
649                 printk(KERN_ERR "Trying to set irq flags for IRQ%d\n", irq);
650                 return;
651         }
652
653         desc = irq_desc + irq;
654         spin_lock_irqsave(&irq_controller_lock, flags);
655         desc->valid = (iflags & IRQF_VALID) != 0;
656         desc->probe_ok = (iflags & IRQF_PROBE) != 0;
657         desc->noautoenable = (iflags & IRQF_NOAUTOEN) != 0;
658         spin_unlock_irqrestore(&irq_controller_lock, flags);
659 }
660
661 int setup_irq(unsigned int irq, struct irqaction *new)
662 {
663         int shared = 0;
664         struct irqaction *old, **p;
665         unsigned long flags;
666         struct irqdesc *desc;
667
668         /*
669          * Some drivers like serial.c use request_irq() heavily,
670          * so we have to be careful not to interfere with a
671          * running system.
672          */
673         if (new->flags & SA_SAMPLE_RANDOM) {
674                 /*
675                  * This function might sleep, we want to call it first,
676                  * outside of the atomic block.
677                  * Yes, this might clear the entropy pool if the wrong
678                  * driver is attempted to be loaded, without actually
679                  * installing a new handler, but is this really a problem,
680                  * only the sysadmin is able to do this.
681                  */
682                 rand_initialize_irq(irq);
683         }
684
685         /*
686          * The following block of code has to be executed atomically
687          */
688         desc = irq_desc + irq;
689         spin_lock_irqsave(&irq_controller_lock, flags);
690         p = &desc->action;
691         if ((old = *p) != NULL) {
692                 /*
693                  * Can't share interrupts unless both agree to and are
694                  * the same type.
695                  */
696                 if (!(old->flags & new->flags & SA_SHIRQ) ||
697                     (~old->flags & new->flags) & SA_TRIGGER_MASK) {
698                         spin_unlock_irqrestore(&irq_controller_lock, flags);
699                         return -EBUSY;
700                 }
701
702                 /* add new interrupt at end of irq queue */
703                 do {
704                         p = &old->next;
705                         old = *p;
706                 } while (old);
707                 shared = 1;
708         }
709
710         *p = new;
711
712         if (!shared) {
713                 desc->probing = 0;
714                 desc->running = 0;
715                 desc->pending = 0;
716                 desc->disable_depth = 1;
717
718                 if (new->flags & SA_TRIGGER_MASK &&
719                     desc->chip->set_type) {
720                         unsigned int type = new->flags & SA_TRIGGER_MASK;
721                         desc->chip->set_type(irq, type);
722                 }
723
724                 if (!desc->noautoenable) {
725                         desc->disable_depth = 0;
726                         desc->chip->unmask(irq);
727                 }
728         }
729
730         spin_unlock_irqrestore(&irq_controller_lock, flags);
731         return 0;
732 }
733
734 /**
735  *      request_irq - allocate an interrupt line
736  *      @irq: Interrupt line to allocate
737  *      @handler: Function to be called when the IRQ occurs
738  *      @irqflags: Interrupt type flags
739  *      @devname: An ascii name for the claiming device
740  *      @dev_id: A cookie passed back to the handler function
741  *
742  *      This call allocates interrupt resources and enables the
743  *      interrupt line and IRQ handling. From the point this
744  *      call is made your handler function may be invoked. Since
745  *      your handler function must clear any interrupt the board
746  *      raises, you must take care both to initialise your hardware
747  *      and to set up the interrupt handler in the right order.
748  *
749  *      Dev_id must be globally unique. Normally the address of the
750  *      device data structure is used as the cookie. Since the handler
751  *      receives this value it makes sense to use it.
752  *
753  *      If your interrupt is shared you must pass a non NULL dev_id
754  *      as this is required when freeing the interrupt.
755  *
756  *      Flags:
757  *
758  *      SA_SHIRQ                Interrupt is shared
759  *
760  *      SA_INTERRUPT            Disable local interrupts while processing
761  *
762  *      SA_SAMPLE_RANDOM        The interrupt can be used for entropy
763  *
764  */
765 int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *),
766                  unsigned long irq_flags, const char * devname, void *dev_id)
767 {
768         unsigned long retval;
769         struct irqaction *action;
770
771         if (irq >= NR_IRQS || !irq_desc[irq].valid || !handler ||
772             (irq_flags & SA_SHIRQ && !dev_id))
773                 return -EINVAL;
774
775         action = (struct irqaction *)kmalloc(sizeof(struct irqaction), GFP_KERNEL);
776         if (!action)
777                 return -ENOMEM;
778
779         action->handler = handler;
780         action->flags = irq_flags;
781         cpus_clear(action->mask);
782         action->name = devname;
783         action->next = NULL;
784         action->dev_id = dev_id;
785
786         retval = setup_irq(irq, action);
787
788         if (retval)
789                 kfree(action);
790         return retval;
791 }
792
793 EXPORT_SYMBOL(request_irq);
794
795 /**
796  *      free_irq - free an interrupt
797  *      @irq: Interrupt line to free
798  *      @dev_id: Device identity to free
799  *
800  *      Remove an interrupt handler. The handler is removed and if the
801  *      interrupt line is no longer in use by any driver it is disabled.
802  *      On a shared IRQ the caller must ensure the interrupt is disabled
803  *      on the card it drives before calling this function.
804  *
805  *      This function must not be called from interrupt context.
806  */
807 void free_irq(unsigned int irq, void *dev_id)
808 {
809         struct irqaction * action, **p;
810         unsigned long flags;
811
812         if (irq >= NR_IRQS || !irq_desc[irq].valid) {
813                 printk(KERN_ERR "Trying to free IRQ%d\n",irq);
814                 dump_stack();
815                 return;
816         }
817
818         spin_lock_irqsave(&irq_controller_lock, flags);
819         for (p = &irq_desc[irq].action; (action = *p) != NULL; p = &action->next) {
820                 if (action->dev_id != dev_id)
821                         continue;
822
823                 /* Found it - now free it */
824                 *p = action->next;
825                 break;
826         }
827         spin_unlock_irqrestore(&irq_controller_lock, flags);
828
829         if (!action) {
830                 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
831                 dump_stack();
832         } else {
833                 synchronize_irq(irq);
834                 kfree(action);
835         }
836 }
837
838 EXPORT_SYMBOL(free_irq);
839
840 static DECLARE_MUTEX(probe_sem);
841
842 /* Start the interrupt probing.  Unlike other architectures,
843  * we don't return a mask of interrupts from probe_irq_on,
844  * but return the number of interrupts enabled for the probe.
845  * The interrupts which have been enabled for probing is
846  * instead recorded in the irq_desc structure.
847  */
848 unsigned long probe_irq_on(void)
849 {
850         unsigned int i, irqs = 0;
851         unsigned long delay;
852
853         down(&probe_sem);
854
855         /*
856          * first snaffle up any unassigned but
857          * probe-able interrupts
858          */
859         spin_lock_irq(&irq_controller_lock);
860         for (i = 0; i < NR_IRQS; i++) {
861                 if (!irq_desc[i].probe_ok || irq_desc[i].action)
862                         continue;
863
864                 irq_desc[i].probing = 1;
865                 irq_desc[i].triggered = 0;
866                 if (irq_desc[i].chip->set_type)
867                         irq_desc[i].chip->set_type(i, IRQT_PROBE);
868                 irq_desc[i].chip->unmask(i);
869                 irqs += 1;
870         }
871         spin_unlock_irq(&irq_controller_lock);
872
873         /*
874          * wait for spurious interrupts to mask themselves out again
875          */
876         for (delay = jiffies + HZ/10; time_before(jiffies, delay); )
877                 /* min 100ms delay */;
878
879         /*
880          * now filter out any obviously spurious interrupts
881          */
882         spin_lock_irq(&irq_controller_lock);
883         for (i = 0; i < NR_IRQS; i++) {
884                 if (irq_desc[i].probing && irq_desc[i].triggered) {
885                         irq_desc[i].probing = 0;
886                         irqs -= 1;
887                 }
888         }
889         spin_unlock_irq(&irq_controller_lock);
890
891         return irqs;
892 }
893
894 EXPORT_SYMBOL(probe_irq_on);
895
896 unsigned int probe_irq_mask(unsigned long irqs)
897 {
898         unsigned int mask = 0, i;
899
900         spin_lock_irq(&irq_controller_lock);
901         for (i = 0; i < 16 && i < NR_IRQS; i++)
902                 if (irq_desc[i].probing && irq_desc[i].triggered)
903                         mask |= 1 << i;
904         spin_unlock_irq(&irq_controller_lock);
905
906         up(&probe_sem);
907
908         return mask;
909 }
910 EXPORT_SYMBOL(probe_irq_mask);
911
912 /*
913  * Possible return values:
914  *  >= 0 - interrupt number
915  *    -1 - no interrupt/many interrupts
916  */
917 int probe_irq_off(unsigned long irqs)
918 {
919         unsigned int i;
920         int irq_found = NO_IRQ;
921
922         /*
923          * look at the interrupts, and find exactly one
924          * that we were probing has been triggered
925          */
926         spin_lock_irq(&irq_controller_lock);
927         for (i = 0; i < NR_IRQS; i++) {
928                 if (irq_desc[i].probing &&
929                     irq_desc[i].triggered) {
930                         if (irq_found != NO_IRQ) {
931                                 irq_found = NO_IRQ;
932                                 goto out;
933                         }
934                         irq_found = i;
935                 }
936         }
937
938         if (irq_found == -1)
939                 irq_found = NO_IRQ;
940 out:
941         spin_unlock_irq(&irq_controller_lock);
942
943         up(&probe_sem);
944
945         return irq_found;
946 }
947
948 EXPORT_SYMBOL(probe_irq_off);
949
950 #ifdef CONFIG_SMP
951 static void route_irq(struct irqdesc *desc, unsigned int irq, unsigned int cpu)
952 {
953         pr_debug("IRQ%u: moving from cpu%u to cpu%u\n", irq, desc->cpu, cpu);
954
955         spin_lock_irq(&irq_controller_lock);
956         desc->cpu = cpu;
957         desc->chip->set_cpu(desc, irq, cpu);
958         spin_unlock_irq(&irq_controller_lock);
959 }
960
961 #ifdef CONFIG_PROC_FS
962 static int
963 irq_affinity_read_proc(char *page, char **start, off_t off, int count,
964                        int *eof, void *data)
965 {
966         struct irqdesc *desc = irq_desc + ((int)data);
967         int len = cpumask_scnprintf(page, count, desc->affinity);
968
969         if (count - len < 2)
970                 return -EINVAL;
971         page[len++] = '\n';
972         page[len] = '\0';
973
974         return len;
975 }
976
977 static int
978 irq_affinity_write_proc(struct file *file, const char __user *buffer,
979                         unsigned long count, void *data)
980 {
981         unsigned int irq = (unsigned int)data;
982         struct irqdesc *desc = irq_desc + irq;
983         cpumask_t affinity, tmp;
984         int ret = -EIO;
985
986         if (!desc->chip->set_cpu)
987                 goto out;
988
989         ret = cpumask_parse(buffer, count, affinity);
990         if (ret)
991                 goto out;
992
993         cpus_and(tmp, affinity, cpu_online_map);
994         if (cpus_empty(tmp)) {
995                 ret = -EINVAL;
996                 goto out;
997         }
998
999         desc->affinity = affinity;
1000         route_irq(desc, irq, first_cpu(tmp));
1001         ret = count;
1002
1003  out:
1004         return ret;
1005 }
1006 #endif
1007 #endif
1008
1009 void __init init_irq_proc(void)
1010 {
1011 #if defined(CONFIG_SMP) && defined(CONFIG_PROC_FS)
1012         struct proc_dir_entry *dir;
1013         int irq;
1014
1015         dir = proc_mkdir("irq", NULL);
1016         if (!dir)
1017                 return;
1018
1019         for (irq = 0; irq < NR_IRQS; irq++) {
1020                 struct proc_dir_entry *entry;
1021                 struct irqdesc *desc;
1022                 char name[16];
1023
1024                 desc = irq_desc + irq;
1025                 memset(name, 0, sizeof(name));
1026                 snprintf(name, sizeof(name) - 1, "%u", irq);
1027
1028                 desc->procdir = proc_mkdir(name, dir);
1029                 if (!desc->procdir)
1030                         continue;
1031
1032                 entry = create_proc_entry("smp_affinity", 0600, desc->procdir);
1033                 if (entry) {
1034                         entry->nlink = 1;
1035                         entry->data = (void *)irq;
1036                         entry->read_proc = irq_affinity_read_proc;
1037                         entry->write_proc = irq_affinity_write_proc;
1038                 }
1039         }
1040 #endif
1041 }
1042
1043 void __init init_IRQ(void)
1044 {
1045         struct irqdesc *desc;
1046         int irq;
1047
1048 #ifdef CONFIG_SMP
1049         bad_irq_desc.affinity = CPU_MASK_ALL;
1050         bad_irq_desc.cpu = smp_processor_id();
1051 #endif
1052
1053         for (irq = 0, desc = irq_desc; irq < NR_IRQS; irq++, desc++) {
1054                 *desc = bad_irq_desc;
1055                 INIT_LIST_HEAD(&desc->pend);
1056         }
1057
1058         init_arch_irq();
1059 }
1060
1061 static int __init noirqdebug_setup(char *str)
1062 {
1063         noirqdebug = 1;
1064         return 1;
1065 }
1066
1067 __setup("noirqdebug", noirqdebug_setup);
1068
1069 #ifdef CONFIG_HOTPLUG_CPU
1070 /*
1071  * The CPU has been marked offline.  Migrate IRQs off this CPU.  If
1072  * the affinity settings do not allow other CPUs, force them onto any
1073  * available CPU.
1074  */
1075 void migrate_irqs(void)
1076 {
1077         unsigned int i, cpu = smp_processor_id();
1078
1079         for (i = 0; i < NR_IRQS; i++) {
1080                 struct irqdesc *desc = irq_desc + i;
1081
1082                 if (desc->cpu == cpu) {
1083                         unsigned int newcpu = any_online_cpu(desc->affinity);
1084
1085                         if (newcpu == NR_CPUS) {
1086                                 if (printk_ratelimit())
1087                                         printk(KERN_INFO "IRQ%u no longer affine to CPU%u\n",
1088                                                i, cpu);
1089
1090                                 cpus_setall(desc->affinity);
1091                                 newcpu = any_online_cpu(desc->affinity);
1092                         }
1093
1094                         route_irq(desc, i, newcpu);
1095                 }
1096         }
1097 }
1098 #endif /* CONFIG_HOTPLUG_CPU */