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