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