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