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