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