[IPV6]: Bring Type 0 routing header in-line with rfc3542.
[pandora-kernel.git] / arch / v850 / kernel / irq.c
1 /*
2  * arch/v850/kernel/irq.c -- High-level interrupt handling
3  *
4  *  Copyright (C) 2001,02,03,04  NEC Electronics Corporation
5  *  Copyright (C) 2001,02,03,04  Miles Bader <miles@gnu.org>
6  *  Copyright (C) 1994-2000  Ralf Baechle
7  *  Copyright (C) 1992  Linus Torvalds
8  *
9  * This file is subject to the terms and conditions of the GNU General
10  * Public License.  See the file COPYING in the main directory of this
11  * archive for more details.
12  *
13  * This file was was derived from the mips version, arch/mips/kernel/irq.c
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/irq.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/slab.h>
23 #include <linux/mm.h>
24 #include <linux/random.h>
25 #include <linux/seq_file.h>
26
27 #include <asm/system.h>
28
29 /*
30  * Controller mappings for all interrupt sources:
31  */
32 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
33         [0 ... NR_IRQS-1] = {
34                 .handler = &no_irq_type,
35                 .lock = SPIN_LOCK_UNLOCKED
36         }
37 };
38
39 /*
40  * Special irq handlers.
41  */
42
43 irqreturn_t no_action(int cpl, void *dev_id, struct pt_regs *regs)
44 {
45         return IRQ_NONE;
46 }
47
48 /*
49  * Generic no controller code
50  */
51
52 static void enable_none(unsigned int irq) { }
53 static unsigned int startup_none(unsigned int irq) { return 0; }
54 static void disable_none(unsigned int irq) { }
55 static void ack_none(unsigned int irq)
56 {
57         /*
58          * 'what should we do if we get a hw irq event on an illegal vector'.
59          * each architecture has to answer this themselves, it doesn't deserve
60          * a generic callback i think.
61          */
62         printk("received IRQ %d with unknown interrupt type\n", irq);
63 }
64
65 /* startup is the same as "enable", shutdown is same as "disable" */
66 #define shutdown_none   disable_none
67 #define end_none        enable_none
68
69 struct hw_interrupt_type no_irq_type = {
70         "none",
71         startup_none,
72         shutdown_none,
73         enable_none,
74         disable_none,
75         ack_none,
76         end_none
77 };
78
79 volatile unsigned long irq_err_count, spurious_count;
80
81 /*
82  * Generic, controller-independent functions:
83  */
84
85 int show_interrupts(struct seq_file *p, void *v)
86 {
87         int i = *(loff_t *) v;
88         struct irqaction * action;
89         unsigned long flags;
90
91         if (i == 0) {
92                 seq_puts(p, "           ");
93                 for (i=0; i < 1 /*smp_num_cpus*/; i++)
94                         seq_printf(p, "CPU%d       ", i);
95                 seq_putc(p, '\n');
96         }
97
98         if (i < NR_IRQS) {
99                 int j, count, num;
100                 const char *type_name = irq_desc[i].handler->typename;
101                 spin_lock_irqsave(&irq_desc[j].lock, flags);
102                 action = irq_desc[i].action;
103                 if (!action) 
104                         goto skip;
105
106                 count = 0;
107                 num = -1;
108                 for (j = 0; j < NR_IRQS; j++)
109                         if (irq_desc[j].handler->typename == type_name) {
110                                 if (i == j)
111                                         num = count;
112                                 count++;
113                         }
114
115                 seq_printf(p, "%3d: ",i);
116                 seq_printf(p, "%10u ", kstat_irqs(i));
117                 if (count > 1) {
118                         int prec = (num >= 100 ? 3 : num >= 10 ? 2 : 1);
119                         seq_printf(p, " %*s%d", 14 - prec, type_name, num);
120                 } else
121                         seq_printf(p, " %14s", type_name);
122                 
123                 seq_printf(p, "  %s", action->name);
124                 for (action=action->next; action; action = action->next)
125                         seq_printf(p, ", %s", action->name);
126                 seq_putc(p, '\n');
127 skip:
128                 spin_unlock_irqrestore(&irq_desc[j].lock, flags);
129         } else if (i == NR_IRQS)
130                 seq_printf(p, "ERR: %10lu\n", irq_err_count);
131         return 0;
132 }
133
134 /*
135  * This should really return information about whether
136  * we should do bottom half handling etc. Right now we
137  * end up _always_ checking the bottom half, which is a
138  * waste of time and is not what some drivers would
139  * prefer.
140  */
141 int handle_IRQ_event(unsigned int irq, struct pt_regs * regs, struct irqaction * action)
142 {
143         int status = 1; /* Force the "do bottom halves" bit */
144         int ret;
145
146         if (!(action->flags & SA_INTERRUPT))
147                 local_irq_enable();
148
149         do {
150                 ret = action->handler(irq, action->dev_id, regs);
151                 if (ret == IRQ_HANDLED)
152                         status |= action->flags;
153                 action = action->next;
154         } while (action);
155         if (status & SA_SAMPLE_RANDOM)
156                 add_interrupt_randomness(irq);
157         local_irq_disable();
158
159         return status;
160 }
161
162 /*
163  * Generic enable/disable code: this just calls
164  * down into the PIC-specific version for the actual
165  * hardware disable after having gotten the irq
166  * controller lock. 
167  */
168  
169 /**
170  *      disable_irq_nosync - disable an irq without waiting
171  *      @irq: Interrupt to disable
172  *
173  *      Disable the selected interrupt line. Disables of an interrupt
174  *      stack. Unlike disable_irq(), this function does not ensure existing
175  *      instances of the IRQ handler have completed before returning.
176  *
177  *      This function may be called from IRQ context.
178  */
179  
180 void inline disable_irq_nosync(unsigned int irq)
181 {
182         irq_desc_t *desc = irq_desc + irq;
183         unsigned long flags;
184
185         spin_lock_irqsave(&desc->lock, flags);
186         if (!desc->depth++) {
187                 desc->status |= IRQ_DISABLED;
188                 desc->handler->disable(irq);
189         }
190         spin_unlock_irqrestore(&desc->lock, flags);
191 }
192
193 /**
194  *      disable_irq - disable an irq and wait for completion
195  *      @irq: Interrupt to disable
196  *
197  *      Disable the selected interrupt line. Disables of an interrupt
198  *      stack. That is for two disables you need two enables. This
199  *      function waits for any pending IRQ handlers for this interrupt
200  *      to complete before returning. If you use this function while
201  *      holding a resource the IRQ handler may need you will deadlock.
202  *
203  *      This function may be called - with care - from IRQ context.
204  */
205  
206 void disable_irq(unsigned int irq)
207 {
208         disable_irq_nosync(irq);
209         synchronize_irq(irq);
210 }
211
212 /**
213  *      enable_irq - enable interrupt handling on an irq
214  *      @irq: Interrupt to enable
215  *
216  *      Re-enables the processing of interrupts on this IRQ line
217  *      providing no disable_irq calls are now in effect.
218  *
219  *      This function may be called from IRQ context.
220  */
221  
222 void enable_irq(unsigned int irq)
223 {
224         irq_desc_t *desc = irq_desc + irq;
225         unsigned long flags;
226
227         spin_lock_irqsave(&desc->lock, flags);
228         switch (desc->depth) {
229         case 1: {
230                 unsigned int status = desc->status & ~IRQ_DISABLED;
231                 desc->status = status;
232                 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
233                         desc->status = status | IRQ_REPLAY;
234                         hw_resend_irq(desc->handler,irq);
235                 }
236                 desc->handler->enable(irq);
237                 /* fall-through */
238         }
239         default:
240                 desc->depth--;
241                 break;
242         case 0:
243                 printk("enable_irq(%u) unbalanced from %p\n", irq,
244                        __builtin_return_address(0));
245         }
246         spin_unlock_irqrestore(&desc->lock, flags);
247 }
248
249 /* Handle interrupt IRQ.  REGS are the registers at the time of ther
250    interrupt.  */
251 unsigned int handle_irq (int irq, struct pt_regs *regs)
252 {
253         /* 
254          * We ack quickly, we don't want the irq controller
255          * thinking we're snobs just because some other CPU has
256          * disabled global interrupts (we have already done the
257          * INT_ACK cycles, it's too late to try to pretend to the
258          * controller that we aren't taking the interrupt).
259          *
260          * 0 return value means that this irq is already being
261          * handled by some other CPU. (or is disabled)
262          */
263         int cpu = smp_processor_id();
264         irq_desc_t *desc = irq_desc + irq;
265         struct irqaction * action;
266         unsigned int status;
267
268         irq_enter();
269         kstat_cpu(cpu).irqs[irq]++;
270         spin_lock(&desc->lock);
271         desc->handler->ack(irq);
272         /*
273            REPLAY is when Linux resends an IRQ that was dropped earlier
274            WAITING is used by probe to mark irqs that are being tested
275            */
276         status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
277         status |= IRQ_PENDING; /* we _want_ to handle it */
278
279         /*
280          * If the IRQ is disabled for whatever reason, we cannot
281          * use the action we have.
282          */
283         action = NULL;
284         if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
285                 action = desc->action;
286                 status &= ~IRQ_PENDING; /* we commit to handling */
287                 status |= IRQ_INPROGRESS; /* we are handling it */
288         }
289         desc->status = status;
290
291         /*
292          * If there is no IRQ handler or it was disabled, exit early.
293            Since we set PENDING, if another processor is handling
294            a different instance of this same irq, the other processor
295            will take care of it.
296          */
297         if (unlikely(!action))
298                 goto out;
299
300         /*
301          * Edge triggered interrupts need to remember
302          * pending events.
303          * This applies to any hw interrupts that allow a second
304          * instance of the same irq to arrive while we are in handle_irq
305          * or in the handler. But the code here only handles the _second_
306          * instance of the irq, not the third or fourth. So it is mostly
307          * useful for irq hardware that does not mask cleanly in an
308          * SMP environment.
309          */
310         for (;;) {
311                 spin_unlock(&desc->lock);
312                 handle_IRQ_event(irq, regs, action);
313                 spin_lock(&desc->lock);
314                 
315                 if (likely(!(desc->status & IRQ_PENDING)))
316                         break;
317                 desc->status &= ~IRQ_PENDING;
318         }
319         desc->status &= ~IRQ_INPROGRESS;
320
321 out:
322         /*
323          * The ->end() handler has to deal with interrupts which got
324          * disabled while the handler was running.
325          */
326         desc->handler->end(irq);
327         spin_unlock(&desc->lock);
328
329         irq_exit();
330
331         return 1;
332 }
333
334 /**
335  *      request_irq - allocate an interrupt line
336  *      @irq: Interrupt line to allocate
337  *      @handler: Function to be called when the IRQ occurs
338  *      @irqflags: Interrupt type flags
339  *      @devname: An ascii name for the claiming device
340  *      @dev_id: A cookie passed back to the handler function
341  *
342  *      This call allocates interrupt resources and enables the
343  *      interrupt line and IRQ handling. From the point this
344  *      call is made your handler function may be invoked. Since
345  *      your handler function must clear any interrupt the board 
346  *      raises, you must take care both to initialise your hardware
347  *      and to set up the interrupt handler in the right order.
348  *
349  *      Dev_id must be globally unique. Normally the address of the
350  *      device data structure is used as the cookie. Since the handler
351  *      receives this value it makes sense to use it.
352  *
353  *      If your interrupt is shared you must pass a non NULL dev_id
354  *      as this is required when freeing the interrupt.
355  *
356  *      Flags:
357  *
358  *      SA_SHIRQ                Interrupt is shared
359  *
360  *      SA_INTERRUPT            Disable local interrupts while processing
361  *
362  *      SA_SAMPLE_RANDOM        The interrupt can be used for entropy
363  *
364  */
365  
366 int request_irq(unsigned int irq, 
367                 irqreturn_t (*handler)(int, void *, struct pt_regs *),
368                 unsigned long irqflags, 
369                 const char * devname,
370                 void *dev_id)
371 {
372         int retval;
373         struct irqaction * action;
374
375 #if 1
376         /*
377          * Sanity-check: shared interrupts should REALLY pass in
378          * a real dev-ID, otherwise we'll have trouble later trying
379          * to figure out which interrupt is which (messes up the
380          * interrupt freeing logic etc).
381          */
382         if (irqflags & SA_SHIRQ) {
383                 if (!dev_id)
384                         printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
385         }
386 #endif
387
388         if (irq >= NR_IRQS)
389                 return -EINVAL;
390         if (!handler)
391                 return -EINVAL;
392
393         action = (struct irqaction *)
394                         kmalloc(sizeof(struct irqaction), GFP_KERNEL);
395         if (!action)
396                 return -ENOMEM;
397
398         action->handler = handler;
399         action->flags = irqflags;
400         cpus_clear(action->mask);
401         action->name = devname;
402         action->next = NULL;
403         action->dev_id = dev_id;
404
405         retval = setup_irq(irq, action);
406         if (retval)
407                 kfree(action);
408         return retval;
409 }
410
411 EXPORT_SYMBOL(request_irq);
412
413 /**
414  *      free_irq - free an interrupt
415  *      @irq: Interrupt line to free
416  *      @dev_id: Device identity to free
417  *
418  *      Remove an interrupt handler. The handler is removed and if the
419  *      interrupt line is no longer in use by any driver it is disabled.
420  *      On a shared IRQ the caller must ensure the interrupt is disabled
421  *      on the card it drives before calling this function. The function
422  *      does not return until any executing interrupts for this IRQ
423  *      have completed.
424  *
425  *      This function may be called from interrupt context. 
426  *
427  *      Bugs: Attempting to free an irq in a handler for the same irq hangs
428  *            the machine.
429  */
430  
431 void free_irq(unsigned int irq, void *dev_id)
432 {
433         irq_desc_t *desc;
434         struct irqaction **p;
435         unsigned long flags;
436
437         if (irq >= NR_IRQS)
438                 return;
439
440         desc = irq_desc + irq;
441         spin_lock_irqsave(&desc->lock,flags);
442         p = &desc->action;
443         for (;;) {
444                 struct irqaction * action = *p;
445                 if (action) {
446                         struct irqaction **pp = p;
447                         p = &action->next;
448                         if (action->dev_id != dev_id)
449                                 continue;
450
451                         /* Found it - now remove it from the list of entries */
452                         *pp = action->next;
453                         if (!desc->action) {
454                                 desc->status |= IRQ_DISABLED;
455                                 desc->handler->shutdown(irq);
456                         }
457                         spin_unlock_irqrestore(&desc->lock,flags);
458
459                         synchronize_irq(irq);
460                         kfree(action);
461                         return;
462                 }
463                 printk("Trying to free free IRQ%d\n",irq);
464                 spin_unlock_irqrestore(&desc->lock,flags);
465                 return;
466         }
467 }
468
469 EXPORT_SYMBOL(free_irq);
470
471 /*
472  * IRQ autodetection code..
473  *
474  * This depends on the fact that any interrupt that
475  * comes in on to an unassigned handler will get stuck
476  * with "IRQ_WAITING" cleared and the interrupt
477  * disabled.
478  */
479
480 static DECLARE_MUTEX(probe_sem);
481
482 /**
483  *      probe_irq_on    - begin an interrupt autodetect
484  *
485  *      Commence probing for an interrupt. The interrupts are scanned
486  *      and a mask of potential interrupt lines is returned.
487  *
488  */
489  
490 unsigned long probe_irq_on(void)
491 {
492         unsigned int i;
493         irq_desc_t *desc;
494         unsigned long val;
495         unsigned long delay;
496
497         down(&probe_sem);
498         /* 
499          * something may have generated an irq long ago and we want to
500          * flush such a longstanding irq before considering it as spurious. 
501          */
502         for (i = NR_IRQS-1; i > 0; i--)  {
503                 desc = irq_desc + i;
504
505                 spin_lock_irq(&desc->lock);
506                 if (!irq_desc[i].action) 
507                         irq_desc[i].handler->startup(i);
508                 spin_unlock_irq(&desc->lock);
509         }
510
511         /* Wait for longstanding interrupts to trigger. */
512         for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
513                 /* about 20ms delay */ barrier();
514
515         /*
516          * enable any unassigned irqs
517          * (we must startup again here because if a longstanding irq
518          * happened in the previous stage, it may have masked itself)
519          */
520         for (i = NR_IRQS-1; i > 0; i--) {
521                 desc = irq_desc + i;
522
523                 spin_lock_irq(&desc->lock);
524                 if (!desc->action) {
525                         desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
526                         if (desc->handler->startup(i))
527                                 desc->status |= IRQ_PENDING;
528                 }
529                 spin_unlock_irq(&desc->lock);
530         }
531
532         /*
533          * Wait for spurious interrupts to trigger
534          */
535         for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
536                 /* about 100ms delay */ barrier();
537
538         /*
539          * Now filter out any obviously spurious interrupts
540          */
541         val = 0;
542         for (i = 0; i < NR_IRQS; i++) {
543                 irq_desc_t *desc = irq_desc + i;
544                 unsigned int status;
545
546                 spin_lock_irq(&desc->lock);
547                 status = desc->status;
548
549                 if (status & IRQ_AUTODETECT) {
550                         /* It triggered already - consider it spurious. */
551                         if (!(status & IRQ_WAITING)) {
552                                 desc->status = status & ~IRQ_AUTODETECT;
553                                 desc->handler->shutdown(i);
554                         } else
555                                 if (i < 32)
556                                         val |= 1 << i;
557                 }
558                 spin_unlock_irq(&desc->lock);
559         }
560
561         return val;
562 }
563
564 EXPORT_SYMBOL(probe_irq_on);
565
566 /*
567  * Return a mask of triggered interrupts (this
568  * can handle only legacy ISA interrupts).
569  */
570  
571 /**
572  *      probe_irq_mask - scan a bitmap of interrupt lines
573  *      @val:   mask of interrupts to consider
574  *
575  *      Scan the ISA bus interrupt lines and return a bitmap of
576  *      active interrupts. The interrupt probe logic state is then
577  *      returned to its previous value.
578  *
579  *      Note: we need to scan all the irq's even though we will
580  *      only return ISA irq numbers - just so that we reset them
581  *      all to a known state.
582  */
583 unsigned int probe_irq_mask(unsigned long val)
584 {
585         int i;
586         unsigned int mask;
587
588         mask = 0;
589         for (i = 0; i < NR_IRQS; i++) {
590                 irq_desc_t *desc = irq_desc + i;
591                 unsigned int status;
592
593                 spin_lock_irq(&desc->lock);
594                 status = desc->status;
595
596                 if (status & IRQ_AUTODETECT) {
597                         if (i < 16 && !(status & IRQ_WAITING))
598                                 mask |= 1 << i;
599
600                         desc->status = status & ~IRQ_AUTODETECT;
601                         desc->handler->shutdown(i);
602                 }
603                 spin_unlock_irq(&desc->lock);
604         }
605         up(&probe_sem);
606
607         return mask & val;
608 }
609
610 /*
611  * Return the one interrupt that triggered (this can
612  * handle any interrupt source).
613  */
614
615 /**
616  *      probe_irq_off   - end an interrupt autodetect
617  *      @val: mask of potential interrupts (unused)
618  *
619  *      Scans the unused interrupt lines and returns the line which
620  *      appears to have triggered the interrupt. If no interrupt was
621  *      found then zero is returned. If more than one interrupt is
622  *      found then minus the first candidate is returned to indicate
623  *      their is doubt.
624  *
625  *      The interrupt probe logic state is returned to its previous
626  *      value.
627  *
628  *      BUGS: When used in a module (which arguably shouldnt happen)
629  *      nothing prevents two IRQ probe callers from overlapping. The
630  *      results of this are non-optimal.
631  */
632  
633 int probe_irq_off(unsigned long val)
634 {
635         int i, irq_found, nr_irqs;
636
637         nr_irqs = 0;
638         irq_found = 0;
639         for (i = 0; i < NR_IRQS; i++) {
640                 irq_desc_t *desc = irq_desc + i;
641                 unsigned int status;
642
643                 spin_lock_irq(&desc->lock);
644                 status = desc->status;
645
646                 if (status & IRQ_AUTODETECT) {
647                         if (!(status & IRQ_WAITING)) {
648                                 if (!nr_irqs)
649                                         irq_found = i;
650                                 nr_irqs++;
651                         }
652                         desc->status = status & ~IRQ_AUTODETECT;
653                         desc->handler->shutdown(i);
654                 }
655                 spin_unlock_irq(&desc->lock);
656         }
657         up(&probe_sem);
658
659         if (nr_irqs > 1)
660                 irq_found = -irq_found;
661         return irq_found;
662 }
663
664 EXPORT_SYMBOL(probe_irq_off);
665
666 /* this was setup_x86_irq but it seems pretty generic */
667 int setup_irq(unsigned int irq, struct irqaction * new)
668 {
669         int shared = 0;
670         unsigned long flags;
671         struct irqaction *old, **p;
672         irq_desc_t *desc = irq_desc + irq;
673
674         /*
675          * Some drivers like serial.c use request_irq() heavily,
676          * so we have to be careful not to interfere with a
677          * running system.
678          */
679         if (new->flags & SA_SAMPLE_RANDOM) {
680                 /*
681                  * This function might sleep, we want to call it first,
682                  * outside of the atomic block.
683                  * Yes, this might clear the entropy pool if the wrong
684                  * driver is attempted to be loaded, without actually
685                  * installing a new handler, but is this really a problem,
686                  * only the sysadmin is able to do this.
687                  */
688                 rand_initialize_irq(irq);
689         }
690
691         /*
692          * The following block of code has to be executed atomically
693          */
694         spin_lock_irqsave(&desc->lock,flags);
695         p = &desc->action;
696         if ((old = *p) != NULL) {
697                 /* Can't share interrupts unless both agree to */
698                 if (!(old->flags & new->flags & SA_SHIRQ)) {
699                         spin_unlock_irqrestore(&desc->lock,flags);
700                         return -EBUSY;
701                 }
702
703                 /* add new interrupt at end of irq queue */
704                 do {
705                         p = &old->next;
706                         old = *p;
707                 } while (old);
708                 shared = 1;
709         }
710
711         *p = new;
712
713         if (!shared) {
714                 desc->depth = 0;
715                 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING | IRQ_INPROGRESS);
716                 desc->handler->startup(irq);
717         }
718         spin_unlock_irqrestore(&desc->lock,flags);
719
720         /* register_irq_proc(irq); */
721         return 0;
722 }
723
724 /* Initialize irq handling for IRQs.
725    BASE_IRQ, BASE_IRQ+INTERVAL, ..., BASE_IRQ+NUM*INTERVAL
726    to IRQ_TYPE.  An IRQ_TYPE of 0 means to use a generic interrupt type.  */
727 void __init
728 init_irq_handlers (int base_irq, int num, int interval,
729                    struct hw_interrupt_type *irq_type)
730 {
731         while (num-- > 0) {
732                 irq_desc[base_irq].status  = IRQ_DISABLED;
733                 irq_desc[base_irq].action  = NULL;
734                 irq_desc[base_irq].depth   = 1;
735                 irq_desc[base_irq].handler = irq_type;
736                 base_irq += interval;
737         }
738 }
739
740 #if defined(CONFIG_PROC_FS) && defined(CONFIG_SYSCTL)
741 void init_irq_proc(void)
742 {
743 }
744 #endif /* CONFIG_PROC_FS && CONFIG_SYSCTL */