Merge branch 'origin'
[pandora-kernel.git] / arch / frv / kernel / irq.c
1 /* irq.c: FRV IRQ handling
2  *
3  * Copyright (C) 2003, 2004 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 /*
13  * (mostly architecture independent, will move to kernel/irq.c in 2.5.)
14  *
15  * IRQs are in fact implemented a bit like signal handlers for the kernel.
16  * Naturally it's not a 1:1 relation, but there are similarities.
17  */
18
19 #include <linux/config.h>
20 #include <linux/ptrace.h>
21 #include <linux/errno.h>
22 #include <linux/signal.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_lock.h>
30 #include <linux/init.h>
31 #include <linux/kernel_stat.h>
32 #include <linux/irq.h>
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
35 #include <linux/module.h>
36
37 #include <asm/atomic.h>
38 #include <asm/io.h>
39 #include <asm/smp.h>
40 #include <asm/system.h>
41 #include <asm/bitops.h>
42 #include <asm/uaccess.h>
43 #include <asm/pgalloc.h>
44 #include <asm/delay.h>
45 #include <asm/irq.h>
46 #include <asm/irc-regs.h>
47 #include <asm/irq-routing.h>
48 #include <asm/gdb-stub.h>
49
50 extern void __init fpga_init(void);
51 extern void __init route_mb93493_irqs(void);
52
53 static void register_irq_proc (unsigned int irq);
54
55 /*
56  * Special irq handlers.
57  */
58
59 irqreturn_t no_action(int cpl, void *dev_id, struct pt_regs *regs) { return IRQ_HANDLED; }
60
61 atomic_t irq_err_count;
62
63 /*
64  * Generic, controller-independent functions:
65  */
66 int show_interrupts(struct seq_file *p, void *v)
67 {
68         struct irqaction *action;
69         struct irq_group *group;
70         unsigned long flags;
71         int level, grp, ix, i, j;
72
73         i = *(loff_t *) v;
74
75         switch (i) {
76         case 0:
77                 seq_printf(p, "           ");
78                 for (j = 0; j < NR_CPUS; j++)
79                         if (cpu_online(j))
80                                 seq_printf(p, "CPU%d       ",j);
81
82                 seq_putc(p, '\n');
83                 break;
84
85         case 1 ... NR_IRQ_GROUPS * NR_IRQ_ACTIONS_PER_GROUP:
86                 local_irq_save(flags);
87
88                 grp = (i - 1) / NR_IRQ_ACTIONS_PER_GROUP;
89                 group = irq_groups[grp];
90                 if (!group)
91                         goto skip;
92
93                 ix = (i - 1) % NR_IRQ_ACTIONS_PER_GROUP;
94                 action = group->actions[ix];
95                 if (!action)
96                         goto skip;
97
98                 seq_printf(p, "%3d: ", i - 1);
99
100 #ifndef CONFIG_SMP
101                 seq_printf(p, "%10u ", kstat_irqs(i));
102 #else
103                 for (j = 0; j < NR_CPUS; j++)
104                         if (cpu_online(j))
105                                 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i - 1]);
106 #endif
107
108                 level = group->sources[ix]->level - frv_irq_levels;
109
110                 seq_printf(p, " %12s@%x", group->sources[ix]->muxname, level);
111                 seq_printf(p, "  %s", action->name);
112
113                 for (action = action->next; action; action = action->next)
114                         seq_printf(p, ", %s", action->name);
115
116                 seq_putc(p, '\n');
117 skip:
118                 local_irq_restore(flags);
119                 break;
120
121         case NR_IRQ_GROUPS * NR_IRQ_ACTIONS_PER_GROUP + 1:
122                 seq_printf(p, "ERR: %10u\n", atomic_read(&irq_err_count));
123                 break;
124
125         default:
126                 break;
127         }
128
129         return 0;
130 }
131
132
133 /*
134  * Generic enable/disable code: this just calls
135  * down into the PIC-specific version for the actual
136  * hardware disable after having gotten the irq
137  * controller lock.
138  */
139
140 /**
141  *      disable_irq_nosync - disable an irq without waiting
142  *      @irq: Interrupt to disable
143  *
144  *      Disable the selected interrupt line.  Disables and Enables are
145  *      nested.
146  *      Unlike disable_irq(), this function does not ensure existing
147  *      instances of the IRQ handler have completed before returning.
148  *
149  *      This function may be called from IRQ context.
150  */
151
152 void disable_irq_nosync(unsigned int irq)
153 {
154         struct irq_source *source;
155         struct irq_group *group;
156         struct irq_level *level;
157         unsigned long flags;
158         int idx = irq & (NR_IRQ_ACTIONS_PER_GROUP - 1);
159
160         group = irq_groups[irq >> NR_IRQ_LOG2_ACTIONS_PER_GROUP];
161         if (!group)
162                 BUG();
163
164         source = group->sources[idx];
165         if (!source)
166                 BUG();
167
168         level = source->level;
169
170         spin_lock_irqsave(&level->lock, flags);
171
172         if (group->control) {
173                 if (!group->disable_cnt[idx]++)
174                         group->control(group, idx, 0);
175         } else if (!level->disable_count++) {
176                 __set_MASK(level - frv_irq_levels);
177         }
178
179         spin_unlock_irqrestore(&level->lock, flags);
180 }
181
182 EXPORT_SYMBOL(disable_irq_nosync);
183
184 /**
185  *      disable_irq - disable an irq and wait for completion
186  *      @irq: Interrupt to disable
187  *
188  *      Disable the selected interrupt line.  Enables and Disables are
189  *      nested.
190  *      This function waits for any pending IRQ handlers for this interrupt
191  *      to complete before returning. If you use this function while
192  *      holding a resource the IRQ handler may need you will deadlock.
193  *
194  *      This function may be called - with care - from IRQ context.
195  */
196
197 void disable_irq(unsigned int irq)
198 {
199         disable_irq_nosync(irq);
200
201 #ifdef CONFIG_SMP
202         if (!local_irq_count(smp_processor_id())) {
203                 do {
204                         barrier();
205                 } while (irq_desc[irq].status & IRQ_INPROGRESS);
206         }
207 #endif
208 }
209
210 EXPORT_SYMBOL(disable_irq);
211
212 /**
213  *      enable_irq - enable handling of an irq
214  *      @irq: Interrupt to enable
215  *
216  *      Undoes the effect of one call to disable_irq().  If this
217  *      matches the last disable, processing of interrupts on this
218  *      IRQ line is re-enabled.
219  *
220  *      This function may be called from IRQ context.
221  */
222
223 void enable_irq(unsigned int irq)
224 {
225         struct irq_source *source;
226         struct irq_group *group;
227         struct irq_level *level;
228         unsigned long flags;
229         int idx = irq & (NR_IRQ_ACTIONS_PER_GROUP - 1);
230         int count;
231
232         group = irq_groups[irq >> NR_IRQ_LOG2_ACTIONS_PER_GROUP];
233         if (!group)
234                 BUG();
235
236         source = group->sources[idx];
237         if (!source)
238                 BUG();
239
240         level = source->level;
241
242         spin_lock_irqsave(&level->lock, flags);
243
244         if (group->control)
245                 count = group->disable_cnt[idx];
246         else
247                 count = level->disable_count;
248
249         switch (count) {
250         case 1:
251                 if (group->control) {
252                         if (group->actions[idx])
253                                 group->control(group, idx, 1);
254                 } else {
255                         if (level->usage)
256                                 __clr_MASK(level - frv_irq_levels);
257                 }
258                 /* fall-through */
259
260         default:
261                 count--;
262                 break;
263
264         case 0:
265                 printk("enable_irq(%u) unbalanced from %p\n", irq, __builtin_return_address(0));
266         }
267
268         if (group->control)
269                 group->disable_cnt[idx] = count;
270         else
271                 level->disable_count = count;
272
273         spin_unlock_irqrestore(&level->lock, flags);
274 }
275
276 EXPORT_SYMBOL(enable_irq);
277
278 /*****************************************************************************/
279 /*
280  * handles all normal device IRQ's
281  * - registers are referred to by the __frame variable (GR28)
282  * - IRQ distribution is complicated in this arch because of the many PICs, the
283  *   way they work and the way they cascade
284  */
285 asmlinkage void do_IRQ(void)
286 {
287         struct irq_source *source;
288         int level, cpu;
289
290         irq_enter();
291
292         level = (__frame->tbr >> 4) & 0xf;
293         cpu = smp_processor_id();
294
295         if ((unsigned long) __frame - (unsigned long) (current + 1) < 512)
296                 BUG();
297
298         __set_MASK(level);
299         __clr_RC(level);
300         __clr_IRL();
301
302         kstat_this_cpu.irqs[level]++;
303
304         for (source = frv_irq_levels[level].sources; source; source = source->next)
305                 source->doirq(source);
306
307         __clr_MASK(level);
308
309         irq_exit();
310
311 } /* end do_IRQ() */
312
313 /*****************************************************************************/
314 /*
315  * handles all NMIs when not co-opted by the debugger
316  * - registers are referred to by the __frame variable (GR28)
317  */
318 asmlinkage void do_NMI(void)
319 {
320 } /* end do_NMI() */
321
322 /*****************************************************************************/
323 /**
324  *      request_irq - allocate an interrupt line
325  *      @irq: Interrupt line to allocate
326  *      @handler: Function to be called when the IRQ occurs
327  *      @irqflags: Interrupt type flags
328  *      @devname: An ascii name for the claiming device
329  *      @dev_id: A cookie passed back to the handler function
330  *
331  *      This call allocates interrupt resources and enables the
332  *      interrupt line and IRQ handling. From the point this
333  *      call is made your handler function may be invoked. Since
334  *      your handler function must clear any interrupt the board
335  *      raises, you must take care both to initialise your hardware
336  *      and to set up the interrupt handler in the right order.
337  *
338  *      Dev_id must be globally unique. Normally the address of the
339  *      device data structure is used as the cookie. Since the handler
340  *      receives this value it makes sense to use it.
341  *
342  *      If your interrupt is shared you must pass a non NULL dev_id
343  *      as this is required when freeing the interrupt.
344  *
345  *      Flags:
346  *
347  *      SA_SHIRQ                Interrupt is shared
348  *
349  *      SA_INTERRUPT            Disable local interrupts while processing
350  *
351  *      SA_SAMPLE_RANDOM        The interrupt can be used for entropy
352  *
353  */
354
355 int request_irq(unsigned int irq,
356                 irqreturn_t (*handler)(int, void *, struct pt_regs *),
357                 unsigned long irqflags,
358                 const char * devname,
359                 void *dev_id)
360 {
361         int retval;
362         struct irqaction *action;
363
364 #if 1
365         /*
366          * Sanity-check: shared interrupts should REALLY pass in
367          * a real dev-ID, otherwise we'll have trouble later trying
368          * to figure out which interrupt is which (messes up the
369          * interrupt freeing logic etc).
370          */
371         if (irqflags & SA_SHIRQ) {
372                 if (!dev_id)
373                         printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n",
374                                devname, (&irq)[-1]);
375         }
376 #endif
377
378         if ((irq >> NR_IRQ_LOG2_ACTIONS_PER_GROUP) >= NR_IRQ_GROUPS)
379                 return -EINVAL;
380         if (!handler)
381                 return -EINVAL;
382
383         action = (struct irqaction *) kmalloc(sizeof(struct irqaction), GFP_KERNEL);
384         if (!action)
385                 return -ENOMEM;
386
387         action->handler = handler;
388         action->flags = irqflags;
389         action->mask = CPU_MASK_NONE;
390         action->name = devname;
391         action->next = NULL;
392         action->dev_id = dev_id;
393
394         retval = setup_irq(irq, action);
395         if (retval)
396                 kfree(action);
397         return retval;
398 }
399
400 EXPORT_SYMBOL(request_irq);
401
402 /**
403  *      free_irq - free an interrupt
404  *      @irq: Interrupt line to free
405  *      @dev_id: Device identity to free
406  *
407  *      Remove an interrupt handler. The handler is removed and if the
408  *      interrupt line is no longer in use by any driver it is disabled.
409  *      On a shared IRQ the caller must ensure the interrupt is disabled
410  *      on the card it drives before calling this function. The function
411  *      does not return until any executing interrupts for this IRQ
412  *      have completed.
413  *
414  *      This function may be called from interrupt context.
415  *
416  *      Bugs: Attempting to free an irq in a handler for the same irq hangs
417  *            the machine.
418  */
419
420 void free_irq(unsigned int irq, void *dev_id)
421 {
422         struct irq_source *source;
423         struct irq_group *group;
424         struct irq_level *level;
425         struct irqaction **p, **pp;
426         unsigned long flags;
427
428         if ((irq >> NR_IRQ_LOG2_ACTIONS_PER_GROUP) >= NR_IRQ_GROUPS)
429                 return;
430
431         group = irq_groups[irq >> NR_IRQ_LOG2_ACTIONS_PER_GROUP];
432         if (!group)
433                 BUG();
434
435         source = group->sources[irq & (NR_IRQ_ACTIONS_PER_GROUP - 1)];
436         if (!source)
437                 BUG();
438
439         level = source->level;
440         p = &group->actions[irq & (NR_IRQ_ACTIONS_PER_GROUP - 1)];
441
442         spin_lock_irqsave(&level->lock, flags);
443
444         for (pp = p; *pp; pp = &(*pp)->next) {
445                 struct irqaction *action = *pp;
446
447                 if (action->dev_id != dev_id)
448                         continue;
449
450                 /* found it - remove from the list of entries */
451                 *pp = action->next;
452
453                 level->usage--;
454
455                 if (p == pp && group->control)
456                         group->control(group, irq & (NR_IRQ_ACTIONS_PER_GROUP - 1), 0);
457
458                 if (level->usage == 0)
459                         __set_MASK(level - frv_irq_levels);
460
461                 spin_unlock_irqrestore(&level->lock,flags);
462
463 #ifdef CONFIG_SMP
464                 /* Wait to make sure it's not being used on another CPU */
465                 while (desc->status & IRQ_INPROGRESS)
466                         barrier();
467 #endif
468                 kfree(action);
469                 return;
470         }
471 }
472
473 EXPORT_SYMBOL(free_irq);
474
475 /*
476  * IRQ autodetection code..
477  *
478  * This depends on the fact that any interrupt that comes in on to an
479  * unassigned IRQ will cause GxICR_DETECT to be set
480  */
481
482 static DECLARE_MUTEX(probe_sem);
483
484 /**
485  *      probe_irq_on    - begin an interrupt autodetect
486  *
487  *      Commence probing for an interrupt. The interrupts are scanned
488  *      and a mask of potential interrupt lines is returned.
489  *
490  */
491
492 unsigned long probe_irq_on(void)
493 {
494         down(&probe_sem);
495         return 0;
496 }
497
498 EXPORT_SYMBOL(probe_irq_on);
499
500 /*
501  * Return a mask of triggered interrupts (this
502  * can handle only legacy ISA interrupts).
503  */
504
505 /**
506  *      probe_irq_mask - scan a bitmap of interrupt lines
507  *      @val:   mask of interrupts to consider
508  *
509  *      Scan the ISA bus interrupt lines and return a bitmap of
510  *      active interrupts. The interrupt probe logic state is then
511  *      returned to its previous value.
512  *
513  *      Note: we need to scan all the irq's even though we will
514  *      only return ISA irq numbers - just so that we reset them
515  *      all to a known state.
516  */
517 unsigned int probe_irq_mask(unsigned long xmask)
518 {
519         up(&probe_sem);
520         return 0;
521 }
522
523 EXPORT_SYMBOL(probe_irq_mask);
524
525 /*
526  * Return the one interrupt that triggered (this can
527  * handle any interrupt source).
528  */
529
530 /**
531  *      probe_irq_off   - end an interrupt autodetect
532  *      @xmask: mask of potential interrupts (unused)
533  *
534  *      Scans the unused interrupt lines and returns the line which
535  *      appears to have triggered the interrupt. If no interrupt was
536  *      found then zero is returned. If more than one interrupt is
537  *      found then minus the first candidate is returned to indicate
538  *      their is doubt.
539  *
540  *      The interrupt probe logic state is returned to its previous
541  *      value.
542  *
543  *      BUGS: When used in a module (which arguably shouldnt happen)
544  *      nothing prevents two IRQ probe callers from overlapping. The
545  *      results of this are non-optimal.
546  */
547
548 int probe_irq_off(unsigned long xmask)
549 {
550         up(&probe_sem);
551         return -1;
552 }
553
554 EXPORT_SYMBOL(probe_irq_off);
555
556 /* this was setup_x86_irq but it seems pretty generic */
557 int setup_irq(unsigned int irq, struct irqaction *new)
558 {
559         struct irq_source *source;
560         struct irq_group *group;
561         struct irq_level *level;
562         struct irqaction **p, **pp;
563         unsigned long flags;
564
565         group = irq_groups[irq >> NR_IRQ_LOG2_ACTIONS_PER_GROUP];
566         if (!group)
567                 BUG();
568
569         source = group->sources[irq & (NR_IRQ_ACTIONS_PER_GROUP - 1)];
570         if (!source)
571                 BUG();
572
573         level = source->level;
574
575         p = &group->actions[irq & (NR_IRQ_ACTIONS_PER_GROUP - 1)];
576
577         /*
578          * Some drivers like serial.c use request_irq() heavily,
579          * so we have to be careful not to interfere with a
580          * running system.
581          */
582         if (new->flags & SA_SAMPLE_RANDOM) {
583                 /*
584                  * This function might sleep, we want to call it first,
585                  * outside of the atomic block.
586                  * Yes, this might clear the entropy pool if the wrong
587                  * driver is attempted to be loaded, without actually
588                  * installing a new handler, but is this really a problem,
589                  * only the sysadmin is able to do this.
590                  */
591                 rand_initialize_irq(irq);
592         }
593
594         /* must juggle the interrupt processing stuff with interrupts disabled */
595         spin_lock_irqsave(&level->lock, flags);
596
597         /* can't share interrupts unless all parties agree to */
598         if (level->usage != 0 && !(level->flags & new->flags & SA_SHIRQ)) {
599                 spin_unlock_irqrestore(&level->lock,flags);
600                 return -EBUSY;
601         }
602
603         /* add new interrupt at end of irq queue */
604         pp = p;
605         while (*pp)
606                 pp = &(*pp)->next;
607
608         *pp = new;
609
610         level->usage++;
611         level->flags = new->flags;
612
613         /* turn the interrupts on */
614         if (level->usage == 1)
615                 __clr_MASK(level - frv_irq_levels);
616
617         if (p == pp && group->control)
618                 group->control(group, irq & (NR_IRQ_ACTIONS_PER_GROUP - 1), 1);
619
620         spin_unlock_irqrestore(&level->lock, flags);
621         register_irq_proc(irq);
622         return 0;
623 }
624
625 static struct proc_dir_entry * root_irq_dir;
626 static struct proc_dir_entry * irq_dir [NR_IRQS];
627
628 #define HEX_DIGITS 8
629
630 static unsigned int parse_hex_value (const char *buffer,
631                                      unsigned long count, unsigned long *ret)
632 {
633         unsigned char hexnum [HEX_DIGITS];
634         unsigned long value;
635         int i;
636
637         if (!count)
638                 return -EINVAL;
639         if (count > HEX_DIGITS)
640                 count = HEX_DIGITS;
641         if (copy_from_user(hexnum, buffer, count))
642                 return -EFAULT;
643
644         /*
645          * Parse the first 8 characters as a hex string, any non-hex char
646          * is end-of-string. '00e1', 'e1', '00E1', 'E1' are all the same.
647          */
648         value = 0;
649
650         for (i = 0; i < count; i++) {
651                 unsigned int c = hexnum[i];
652
653                 switch (c) {
654                         case '0' ... '9': c -= '0'; break;
655                         case 'a' ... 'f': c -= 'a'-10; break;
656                         case 'A' ... 'F': c -= 'A'-10; break;
657                 default:
658                         goto out;
659                 }
660                 value = (value << 4) | c;
661         }
662 out:
663         *ret = value;
664         return 0;
665 }
666
667
668 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
669                         int count, int *eof, void *data)
670 {
671         unsigned long *mask = (unsigned long *) data;
672         if (count < HEX_DIGITS+1)
673                 return -EINVAL;
674         return sprintf (page, "%08lx\n", *mask);
675 }
676
677 static int prof_cpu_mask_write_proc (struct file *file, const char *buffer,
678                                         unsigned long count, void *data)
679 {
680         unsigned long *mask = (unsigned long *) data, full_count = count, err;
681         unsigned long new_value;
682
683         show_state();
684         err = parse_hex_value(buffer, count, &new_value);
685         if (err)
686                 return err;
687
688         *mask = new_value;
689         return full_count;
690 }
691
692 #define MAX_NAMELEN 10
693
694 static void register_irq_proc (unsigned int irq)
695 {
696         char name [MAX_NAMELEN];
697
698         if (!root_irq_dir || irq_dir[irq])
699                 return;
700
701         memset(name, 0, MAX_NAMELEN);
702         sprintf(name, "%d", irq);
703
704         /* create /proc/irq/1234 */
705         irq_dir[irq] = proc_mkdir(name, root_irq_dir);
706 }
707
708 unsigned long prof_cpu_mask = -1;
709
710 void init_irq_proc (void)
711 {
712         struct proc_dir_entry *entry;
713         int i;
714
715         /* create /proc/irq */
716         root_irq_dir = proc_mkdir("irq", 0);
717
718         /* create /proc/irq/prof_cpu_mask */
719         entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
720         if (!entry)
721             return;
722
723         entry->nlink = 1;
724         entry->data = (void *)&prof_cpu_mask;
725         entry->read_proc = prof_cpu_mask_read_proc;
726         entry->write_proc = prof_cpu_mask_write_proc;
727
728         /*
729          * Create entries for all existing IRQs.
730          */
731         for (i = 0; i < NR_IRQS; i++)
732                 register_irq_proc(i);
733 }
734
735 /*****************************************************************************/
736 /*
737  * initialise the interrupt system
738  */
739 void __init init_IRQ(void)
740 {
741         route_cpu_irqs();
742         fpga_init();
743 #ifdef CONFIG_FUJITSU_MB93493
744         route_mb93493_irqs();
745 #endif
746 } /* end init_IRQ() */