VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.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
145         if (!(action->flags & SA_INTERRUPT))
146                 local_irq_enable();
147
148         do {
149                 status |= action->flags;
150                 action->handler(irq, action->dev_id, regs);
151                 action = action->next;
152         } while (action);
153         if (status & SA_SAMPLE_RANDOM)
154                 add_interrupt_randomness(irq);
155         local_irq_disable();
156
157         return status;
158 }
159
160 /*
161  * Generic enable/disable code: this just calls
162  * down into the PIC-specific version for the actual
163  * hardware disable after having gotten the irq
164  * controller lock. 
165  */
166  
167 /**
168  *      disable_irq_nosync - disable an irq without waiting
169  *      @irq: Interrupt to disable
170  *
171  *      Disable the selected interrupt line. Disables of an interrupt
172  *      stack. Unlike disable_irq(), this function does not ensure existing
173  *      instances of the IRQ handler have completed before returning.
174  *
175  *      This function may be called from IRQ context.
176  */
177  
178 void inline disable_irq_nosync(unsigned int irq)
179 {
180         irq_desc_t *desc = irq_desc + irq;
181         unsigned long flags;
182
183         spin_lock_irqsave(&desc->lock, flags);
184         if (!desc->depth++) {
185                 desc->status |= IRQ_DISABLED;
186                 desc->handler->disable(irq);
187         }
188         spin_unlock_irqrestore(&desc->lock, flags);
189 }
190
191 /**
192  *      disable_irq - disable an irq and wait for completion
193  *      @irq: Interrupt to disable
194  *
195  *      Disable the selected interrupt line. Disables of an interrupt
196  *      stack. That is for two disables you need two enables. This
197  *      function waits for any pending IRQ handlers for this interrupt
198  *      to complete before returning. If you use this function while
199  *      holding a resource the IRQ handler may need you will deadlock.
200  *
201  *      This function may be called - with care - from IRQ context.
202  */
203  
204 void disable_irq(unsigned int irq)
205 {
206         disable_irq_nosync(irq);
207         synchronize_irq(irq);
208 }
209
210 /**
211  *      enable_irq - enable interrupt handling on an irq
212  *      @irq: Interrupt to enable
213  *
214  *      Re-enables the processing of interrupts on this IRQ line
215  *      providing no disable_irq calls are now in effect.
216  *
217  *      This function may be called from IRQ context.
218  */
219  
220 void enable_irq(unsigned int irq)
221 {
222         irq_desc_t *desc = irq_desc + irq;
223         unsigned long flags;
224
225         spin_lock_irqsave(&desc->lock, flags);
226         switch (desc->depth) {
227         case 1: {
228                 unsigned int status = desc->status & ~IRQ_DISABLED;
229                 desc->status = status;
230                 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
231                         desc->status = status | IRQ_REPLAY;
232                         hw_resend_irq(desc->handler,irq);
233                 }
234                 desc->handler->enable(irq);
235                 /* fall-through */
236         }
237         default:
238                 desc->depth--;
239                 break;
240         case 0:
241                 printk("enable_irq(%u) unbalanced from %p\n", irq,
242                        __builtin_return_address(0));
243         }
244         spin_unlock_irqrestore(&desc->lock, flags);
245 }
246
247 /* Handle interrupt IRQ.  REGS are the registers at the time of ther
248    interrupt.  */
249 unsigned int handle_irq (int irq, struct pt_regs *regs)
250 {
251         /* 
252          * We ack quickly, we don't want the irq controller
253          * thinking we're snobs just because some other CPU has
254          * disabled global interrupts (we have already done the
255          * INT_ACK cycles, it's too late to try to pretend to the
256          * controller that we aren't taking the interrupt).
257          *
258          * 0 return value means that this irq is already being
259          * handled by some other CPU. (or is disabled)
260          */
261         int cpu = smp_processor_id();
262         irq_desc_t *desc = irq_desc + irq;
263         struct irqaction * action;
264         unsigned int status;
265
266         irq_enter();
267         kstat_cpu(cpu).irqs[irq]++;
268         spin_lock(&desc->lock);
269         desc->handler->ack(irq);
270         /*
271            REPLAY is when Linux resends an IRQ that was dropped earlier
272            WAITING is used by probe to mark irqs that are being tested
273            */
274         status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
275         status |= IRQ_PENDING; /* we _want_ to handle it */
276
277         /*
278          * If the IRQ is disabled for whatever reason, we cannot
279          * use the action we have.
280          */
281         action = NULL;
282         if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
283                 action = desc->action;
284                 status &= ~IRQ_PENDING; /* we commit to handling */
285                 status |= IRQ_INPROGRESS; /* we are handling it */
286         }
287         desc->status = status;
288
289         /*
290          * If there is no IRQ handler or it was disabled, exit early.
291            Since we set PENDING, if another processor is handling
292            a different instance of this same irq, the other processor
293            will take care of it.
294          */
295         if (unlikely(!action))
296                 goto out;
297
298         /*
299          * Edge triggered interrupts need to remember
300          * pending events.
301          * This applies to any hw interrupts that allow a second
302          * instance of the same irq to arrive while we are in handle_irq
303          * or in the handler. But the code here only handles the _second_
304          * instance of the irq, not the third or fourth. So it is mostly
305          * useful for irq hardware that does not mask cleanly in an
306          * SMP environment.
307          */
308         for (;;) {
309                 spin_unlock(&desc->lock);
310                 handle_IRQ_event(irq, regs, action);
311                 spin_lock(&desc->lock);
312                 
313                 if (likely(!(desc->status & IRQ_PENDING)))
314                         break;
315                 desc->status &= ~IRQ_PENDING;
316         }
317         desc->status &= ~IRQ_INPROGRESS;
318
319 out:
320         /*
321          * The ->end() handler has to deal with interrupts which got
322          * disabled while the handler was running.
323          */
324         desc->handler->end(irq);
325         spin_unlock(&desc->lock);
326
327         irq_exit();
328
329         return 1;
330 }
331
332 /**
333  *      request_irq - allocate an interrupt line
334  *      @irq: Interrupt line to allocate
335  *      @handler: Function to be called when the IRQ occurs
336  *      @irqflags: Interrupt type flags
337  *      @devname: An ascii name for the claiming device
338  *      @dev_id: A cookie passed back to the handler function
339  *
340  *      This call allocates interrupt resources and enables the
341  *      interrupt line and IRQ handling. From the point this
342  *      call is made your handler function may be invoked. Since
343  *      your handler function must clear any interrupt the board 
344  *      raises, you must take care both to initialise your hardware
345  *      and to set up the interrupt handler in the right order.
346  *
347  *      Dev_id must be globally unique. Normally the address of the
348  *      device data structure is used as the cookie. Since the handler
349  *      receives this value it makes sense to use it.
350  *
351  *      If your interrupt is shared you must pass a non NULL dev_id
352  *      as this is required when freeing the interrupt.
353  *
354  *      Flags:
355  *
356  *      SA_SHIRQ                Interrupt is shared
357  *
358  *      SA_INTERRUPT            Disable local interrupts while processing
359  *
360  *      SA_SAMPLE_RANDOM        The interrupt can be used for entropy
361  *
362  */
363  
364 int request_irq(unsigned int irq, 
365                 irqreturn_t (*handler)(int, void *, struct pt_regs *),
366                 unsigned long irqflags, 
367                 const char * devname,
368                 void *dev_id)
369 {
370         int retval;
371         struct irqaction * action;
372
373 #if 1
374         /*
375          * Sanity-check: shared interrupts should REALLY pass in
376          * a real dev-ID, otherwise we'll have trouble later trying
377          * to figure out which interrupt is which (messes up the
378          * interrupt freeing logic etc).
379          */
380         if (irqflags & SA_SHIRQ) {
381                 if (!dev_id)
382                         printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
383         }
384 #endif
385
386         if (irq >= NR_IRQS)
387                 return -EINVAL;
388         if (!handler)
389                 return -EINVAL;
390
391         action = (struct irqaction *)
392                         kmalloc(sizeof(struct irqaction), GFP_KERNEL);
393         if (!action)
394                 return -ENOMEM;
395
396         action->handler = handler;
397         action->flags = irqflags;
398         cpus_clear(action->mask);
399         action->name = devname;
400         action->next = NULL;
401         action->dev_id = dev_id;
402
403         retval = setup_irq(irq, action);
404         if (retval)
405                 kfree(action);
406         return retval;
407 }
408
409 EXPORT_SYMBOL(request_irq);
410
411 /**
412  *      free_irq - free an interrupt
413  *      @irq: Interrupt line to free
414  *      @dev_id: Device identity to free
415  *
416  *      Remove an interrupt handler. The handler is removed and if the
417  *      interrupt line is no longer in use by any driver it is disabled.
418  *      On a shared IRQ the caller must ensure the interrupt is disabled
419  *      on the card it drives before calling this function. The function
420  *      does not return until any executing interrupts for this IRQ
421  *      have completed.
422  *
423  *      This function may be called from interrupt context. 
424  *
425  *      Bugs: Attempting to free an irq in a handler for the same irq hangs
426  *            the machine.
427  */
428  
429 void free_irq(unsigned int irq, void *dev_id)
430 {
431         irq_desc_t *desc;
432         struct irqaction **p;
433         unsigned long flags;
434
435         if (irq >= NR_IRQS)
436                 return;
437
438         desc = irq_desc + irq;
439         spin_lock_irqsave(&desc->lock,flags);
440         p = &desc->action;
441         for (;;) {
442                 struct irqaction * action = *p;
443                 if (action) {
444                         struct irqaction **pp = p;
445                         p = &action->next;
446                         if (action->dev_id != dev_id)
447                                 continue;
448
449                         /* Found it - now remove it from the list of entries */
450                         *pp = action->next;
451                         if (!desc->action) {
452                                 desc->status |= IRQ_DISABLED;
453                                 desc->handler->shutdown(irq);
454                         }
455                         spin_unlock_irqrestore(&desc->lock,flags);
456
457                         synchronize_irq(irq);
458                         kfree(action);
459                         return;
460                 }
461                 printk("Trying to free free IRQ%d\n",irq);
462                 spin_unlock_irqrestore(&desc->lock,flags);
463                 return;
464         }
465 }
466
467 EXPORT_SYMBOL(free_irq);
468
469 /*
470  * IRQ autodetection code..
471  *
472  * This depends on the fact that any interrupt that
473  * comes in on to an unassigned handler will get stuck
474  * with "IRQ_WAITING" cleared and the interrupt
475  * disabled.
476  */
477
478 static DECLARE_MUTEX(probe_sem);
479
480 /**
481  *      probe_irq_on    - begin an interrupt autodetect
482  *
483  *      Commence probing for an interrupt. The interrupts are scanned
484  *      and a mask of potential interrupt lines is returned.
485  *
486  */
487  
488 unsigned long probe_irq_on(void)
489 {
490         unsigned int i;
491         irq_desc_t *desc;
492         unsigned long val;
493         unsigned long delay;
494
495         down(&probe_sem);
496         /* 
497          * something may have generated an irq long ago and we want to
498          * flush such a longstanding irq before considering it as spurious. 
499          */
500         for (i = NR_IRQS-1; i > 0; i--)  {
501                 desc = irq_desc + i;
502
503                 spin_lock_irq(&desc->lock);
504                 if (!irq_desc[i].action) 
505                         irq_desc[i].handler->startup(i);
506                 spin_unlock_irq(&desc->lock);
507         }
508
509         /* Wait for longstanding interrupts to trigger. */
510         for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
511                 /* about 20ms delay */ barrier();
512
513         /*
514          * enable any unassigned irqs
515          * (we must startup again here because if a longstanding irq
516          * happened in the previous stage, it may have masked itself)
517          */
518         for (i = NR_IRQS-1; i > 0; i--) {
519                 desc = irq_desc + i;
520
521                 spin_lock_irq(&desc->lock);
522                 if (!desc->action) {
523                         desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
524                         if (desc->handler->startup(i))
525                                 desc->status |= IRQ_PENDING;
526                 }
527                 spin_unlock_irq(&desc->lock);
528         }
529
530         /*
531          * Wait for spurious interrupts to trigger
532          */
533         for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
534                 /* about 100ms delay */ barrier();
535
536         /*
537          * Now filter out any obviously spurious interrupts
538          */
539         val = 0;
540         for (i = 0; i < NR_IRQS; i++) {
541                 irq_desc_t *desc = irq_desc + i;
542                 unsigned int status;
543
544                 spin_lock_irq(&desc->lock);
545                 status = desc->status;
546
547                 if (status & IRQ_AUTODETECT) {
548                         /* It triggered already - consider it spurious. */
549                         if (!(status & IRQ_WAITING)) {
550                                 desc->status = status & ~IRQ_AUTODETECT;
551                                 desc->handler->shutdown(i);
552                         } else
553                                 if (i < 32)
554                                         val |= 1 << i;
555                 }
556                 spin_unlock_irq(&desc->lock);
557         }
558
559         return val;
560 }
561
562 EXPORT_SYMBOL(probe_irq_on);
563
564 /*
565  * Return a mask of triggered interrupts (this
566  * can handle only legacy ISA interrupts).
567  */
568  
569 /**
570  *      probe_irq_mask - scan a bitmap of interrupt lines
571  *      @val:   mask of interrupts to consider
572  *
573  *      Scan the ISA bus interrupt lines and return a bitmap of
574  *      active interrupts. The interrupt probe logic state is then
575  *      returned to its previous value.
576  *
577  *      Note: we need to scan all the irq's even though we will
578  *      only return ISA irq numbers - just so that we reset them
579  *      all to a known state.
580  */
581 unsigned int probe_irq_mask(unsigned long val)
582 {
583         int i;
584         unsigned int mask;
585
586         mask = 0;
587         for (i = 0; i < NR_IRQS; i++) {
588                 irq_desc_t *desc = irq_desc + i;
589                 unsigned int status;
590
591                 spin_lock_irq(&desc->lock);
592                 status = desc->status;
593
594                 if (status & IRQ_AUTODETECT) {
595                         if (i < 16 && !(status & IRQ_WAITING))
596                                 mask |= 1 << i;
597
598                         desc->status = status & ~IRQ_AUTODETECT;
599                         desc->handler->shutdown(i);
600                 }
601                 spin_unlock_irq(&desc->lock);
602         }
603         up(&probe_sem);
604
605         return mask & val;
606 }
607
608 /*
609  * Return the one interrupt that triggered (this can
610  * handle any interrupt source).
611  */
612
613 /**
614  *      probe_irq_off   - end an interrupt autodetect
615  *      @val: mask of potential interrupts (unused)
616  *
617  *      Scans the unused interrupt lines and returns the line which
618  *      appears to have triggered the interrupt. If no interrupt was
619  *      found then zero is returned. If more than one interrupt is
620  *      found then minus the first candidate is returned to indicate
621  *      their is doubt.
622  *
623  *      The interrupt probe logic state is returned to its previous
624  *      value.
625  *
626  *      BUGS: When used in a module (which arguably shouldnt happen)
627  *      nothing prevents two IRQ probe callers from overlapping. The
628  *      results of this are non-optimal.
629  */
630  
631 int probe_irq_off(unsigned long val)
632 {
633         int i, irq_found, nr_irqs;
634
635         nr_irqs = 0;
636         irq_found = 0;
637         for (i = 0; i < NR_IRQS; i++) {
638                 irq_desc_t *desc = irq_desc + i;
639                 unsigned int status;
640
641                 spin_lock_irq(&desc->lock);
642                 status = desc->status;
643
644                 if (status & IRQ_AUTODETECT) {
645                         if (!(status & IRQ_WAITING)) {
646                                 if (!nr_irqs)
647                                         irq_found = i;
648                                 nr_irqs++;
649                         }
650                         desc->status = status & ~IRQ_AUTODETECT;
651                         desc->handler->shutdown(i);
652                 }
653                 spin_unlock_irq(&desc->lock);
654         }
655         up(&probe_sem);
656
657         if (nr_irqs > 1)
658                 irq_found = -irq_found;
659         return irq_found;
660 }
661
662 EXPORT_SYMBOL(probe_irq_off);
663
664 /* this was setup_x86_irq but it seems pretty generic */
665 int setup_irq(unsigned int irq, struct irqaction * new)
666 {
667         int shared = 0;
668         unsigned long flags;
669         struct irqaction *old, **p;
670         irq_desc_t *desc = irq_desc + irq;
671
672         /*
673          * Some drivers like serial.c use request_irq() heavily,
674          * so we have to be careful not to interfere with a
675          * running system.
676          */
677         if (new->flags & SA_SAMPLE_RANDOM) {
678                 /*
679                  * This function might sleep, we want to call it first,
680                  * outside of the atomic block.
681                  * Yes, this might clear the entropy pool if the wrong
682                  * driver is attempted to be loaded, without actually
683                  * installing a new handler, but is this really a problem,
684                  * only the sysadmin is able to do this.
685                  */
686                 rand_initialize_irq(irq);
687         }
688
689         /*
690          * The following block of code has to be executed atomically
691          */
692         spin_lock_irqsave(&desc->lock,flags);
693         p = &desc->action;
694         if ((old = *p) != NULL) {
695                 /* Can't share interrupts unless both agree to */
696                 if (!(old->flags & new->flags & SA_SHIRQ)) {
697                         spin_unlock_irqrestore(&desc->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->depth = 0;
713                 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING | IRQ_INPROGRESS);
714                 desc->handler->startup(irq);
715         }
716         spin_unlock_irqrestore(&desc->lock,flags);
717
718         /* register_irq_proc(irq); */
719         return 0;
720 }
721
722 /* Initialize irq handling for IRQs.
723    BASE_IRQ, BASE_IRQ+INTERVAL, ..., BASE_IRQ+NUM*INTERVAL
724    to IRQ_TYPE.  An IRQ_TYPE of 0 means to use a generic interrupt type.  */
725 void __init
726 init_irq_handlers (int base_irq, int num, int interval,
727                    struct hw_interrupt_type *irq_type)
728 {
729         while (num-- > 0) {
730                 irq_desc[base_irq].status  = IRQ_DISABLED;
731                 irq_desc[base_irq].action  = NULL;
732                 irq_desc[base_irq].depth   = 1;
733                 irq_desc[base_irq].handler = irq_type;
734                 base_irq += interval;
735         }
736 }
737
738 #if defined(CONFIG_PROC_FS) && defined(CONFIG_SYSCTL)
739 void init_irq_proc(void)
740 {
741 }
742 #endif /* CONFIG_PROC_FS && CONFIG_SYSCTL */