1 /* irq.c: FRV IRQ handling
3 * Copyright (C) 2003, 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
13 * (mostly architecture independent, will move to kernel/irq.c in 2.5.)
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.
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>
37 #include <asm/atomic.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>
46 #include <asm/irc-regs.h>
47 #include <asm/irq-routing.h>
48 #include <asm/gdb-stub.h>
50 extern void __init fpga_init(void);
51 extern void __init route_mb93493_irqs(void);
53 static void register_irq_proc (unsigned int irq);
56 * Special irq handlers.
59 irqreturn_t no_action(int cpl, void *dev_id, struct pt_regs *regs) { return IRQ_HANDLED; }
61 atomic_t irq_err_count;
64 * Generic, controller-independent functions:
66 int show_interrupts(struct seq_file *p, void *v)
68 struct irqaction *action;
69 struct irq_group *group;
71 int level, grp, ix, i, j;
78 for (j = 0; j < NR_CPUS; j++)
80 seq_printf(p, "CPU%d ",j);
85 case 1 ... NR_IRQ_GROUPS * NR_IRQ_ACTIONS_PER_GROUP:
86 local_irq_save(flags);
88 grp = (i - 1) / NR_IRQ_ACTIONS_PER_GROUP;
89 group = irq_groups[grp];
93 ix = (i - 1) % NR_IRQ_ACTIONS_PER_GROUP;
94 action = group->actions[ix];
98 seq_printf(p, "%3d: ", i - 1);
101 seq_printf(p, "%10u ", kstat_irqs(i));
103 for (j = 0; j < NR_CPUS; j++)
105 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i - 1]);
108 level = group->sources[ix]->level - frv_irq_levels;
110 seq_printf(p, " %12s@%x", group->sources[ix]->muxname, level);
111 seq_printf(p, " %s", action->name);
113 for (action = action->next; action; action = action->next)
114 seq_printf(p, ", %s", action->name);
118 local_irq_restore(flags);
121 case NR_IRQ_GROUPS * NR_IRQ_ACTIONS_PER_GROUP + 1:
122 seq_printf(p, "ERR: %10u\n", atomic_read(&irq_err_count));
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
141 * disable_irq_nosync - disable an irq without waiting
142 * @irq: Interrupt to disable
144 * Disable the selected interrupt line. Disables and Enables are
146 * Unlike disable_irq(), this function does not ensure existing
147 * instances of the IRQ handler have completed before returning.
149 * This function may be called from IRQ context.
152 void disable_irq_nosync(unsigned int irq)
154 struct irq_source *source;
155 struct irq_group *group;
156 struct irq_level *level;
158 int idx = irq & (NR_IRQ_ACTIONS_PER_GROUP - 1);
160 group = irq_groups[irq >> NR_IRQ_LOG2_ACTIONS_PER_GROUP];
164 source = group->sources[idx];
168 level = source->level;
170 spin_lock_irqsave(&level->lock, flags);
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);
179 spin_unlock_irqrestore(&level->lock, flags);
182 EXPORT_SYMBOL(disable_irq_nosync);
185 * disable_irq - disable an irq and wait for completion
186 * @irq: Interrupt to disable
188 * Disable the selected interrupt line. Enables and Disables are
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.
194 * This function may be called - with care - from IRQ context.
197 void disable_irq(unsigned int irq)
199 disable_irq_nosync(irq);
202 if (!local_irq_count(smp_processor_id())) {
205 } while (irq_desc[irq].status & IRQ_INPROGRESS);
210 EXPORT_SYMBOL(disable_irq);
213 * enable_irq - enable handling of an irq
214 * @irq: Interrupt to enable
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.
220 * This function may be called from IRQ context.
223 void enable_irq(unsigned int irq)
225 struct irq_source *source;
226 struct irq_group *group;
227 struct irq_level *level;
229 int idx = irq & (NR_IRQ_ACTIONS_PER_GROUP - 1);
232 group = irq_groups[irq >> NR_IRQ_LOG2_ACTIONS_PER_GROUP];
236 source = group->sources[idx];
240 level = source->level;
242 spin_lock_irqsave(&level->lock, flags);
245 count = group->disable_cnt[idx];
247 count = level->disable_count;
251 if (group->control) {
252 if (group->actions[idx])
253 group->control(group, idx, 1);
256 __clr_MASK(level - frv_irq_levels);
265 printk("enable_irq(%u) unbalanced from %p\n", irq, __builtin_return_address(0));
269 group->disable_cnt[idx] = count;
271 level->disable_count = count;
273 spin_unlock_irqrestore(&level->lock, flags);
276 EXPORT_SYMBOL(enable_irq);
278 /*****************************************************************************/
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
285 asmlinkage void do_IRQ(void)
287 struct irq_source *source;
290 level = (__frame->tbr >> 4) & 0xf;
291 cpu = smp_processor_id();
296 *(volatile u32 *) 0xe1200004 = ~((irqcount++ << 8) | level);
297 *(volatile u16 *) 0xffc00100 = (u16) ~0x9999;
302 if ((unsigned long) __frame - (unsigned long) (current + 1) < 512)
309 kstat_this_cpu.irqs[level]++;
313 for (source = frv_irq_levels[level].sources; source; source = source->next)
314 source->doirq(source);
320 /* only process softirqs if we didn't interrupt another interrupt handler */
321 if ((__frame->psr & PSR_PIL) == PSR_PIL_0)
322 if (local_softirq_pending())
325 #ifdef CONFIG_PREEMPT
327 while (--current->preempt_count == 0) {
328 if (!(__frame->psr & PSR_S) ||
329 current->need_resched == 0 ||
332 current->preempt_count++;
341 *(volatile u16 *) 0xffc00100 = (u16) ~0x6666;
348 /*****************************************************************************/
350 * handles all NMIs when not co-opted by the debugger
351 * - registers are referred to by the __frame variable (GR28)
353 asmlinkage void do_NMI(void)
357 /*****************************************************************************/
359 * request_irq - allocate an interrupt line
360 * @irq: Interrupt line to allocate
361 * @handler: Function to be called when the IRQ occurs
362 * @irqflags: Interrupt type flags
363 * @devname: An ascii name for the claiming device
364 * @dev_id: A cookie passed back to the handler function
366 * This call allocates interrupt resources and enables the
367 * interrupt line and IRQ handling. From the point this
368 * call is made your handler function may be invoked. Since
369 * your handler function must clear any interrupt the board
370 * raises, you must take care both to initialise your hardware
371 * and to set up the interrupt handler in the right order.
373 * Dev_id must be globally unique. Normally the address of the
374 * device data structure is used as the cookie. Since the handler
375 * receives this value it makes sense to use it.
377 * If your interrupt is shared you must pass a non NULL dev_id
378 * as this is required when freeing the interrupt.
382 * SA_SHIRQ Interrupt is shared
384 * SA_INTERRUPT Disable local interrupts while processing
386 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
390 int request_irq(unsigned int irq,
391 irqreturn_t (*handler)(int, void *, struct pt_regs *),
392 unsigned long irqflags,
393 const char * devname,
397 struct irqaction *action;
401 * Sanity-check: shared interrupts should REALLY pass in
402 * a real dev-ID, otherwise we'll have trouble later trying
403 * to figure out which interrupt is which (messes up the
404 * interrupt freeing logic etc).
406 if (irqflags & SA_SHIRQ) {
408 printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n",
409 devname, (&irq)[-1]);
413 if ((irq >> NR_IRQ_LOG2_ACTIONS_PER_GROUP) >= NR_IRQ_GROUPS)
418 action = (struct irqaction *) kmalloc(sizeof(struct irqaction), GFP_KERNEL);
422 action->handler = handler;
423 action->flags = irqflags;
424 action->mask = CPU_MASK_NONE;
425 action->name = devname;
427 action->dev_id = dev_id;
429 retval = setup_irq(irq, action);
435 EXPORT_SYMBOL(request_irq);
438 * free_irq - free an interrupt
439 * @irq: Interrupt line to free
440 * @dev_id: Device identity to free
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
449 * This function may be called from interrupt context.
451 * Bugs: Attempting to free an irq in a handler for the same irq hangs
455 void free_irq(unsigned int irq, void *dev_id)
457 struct irq_source *source;
458 struct irq_group *group;
459 struct irq_level *level;
460 struct irqaction **p, **pp;
463 if ((irq >> NR_IRQ_LOG2_ACTIONS_PER_GROUP) >= NR_IRQ_GROUPS)
466 group = irq_groups[irq >> NR_IRQ_LOG2_ACTIONS_PER_GROUP];
470 source = group->sources[irq & (NR_IRQ_ACTIONS_PER_GROUP - 1)];
474 level = source->level;
475 p = &group->actions[irq & (NR_IRQ_ACTIONS_PER_GROUP - 1)];
477 spin_lock_irqsave(&level->lock, flags);
479 for (pp = p; *pp; pp = &(*pp)->next) {
480 struct irqaction *action = *pp;
482 if (action->dev_id != dev_id)
485 /* found it - remove from the list of entries */
490 if (p == pp && group->control)
491 group->control(group, irq & (NR_IRQ_ACTIONS_PER_GROUP - 1), 0);
493 if (level->usage == 0)
494 __set_MASK(level - frv_irq_levels);
496 spin_unlock_irqrestore(&level->lock,flags);
499 /* Wait to make sure it's not being used on another CPU */
500 while (desc->status & IRQ_INPROGRESS)
508 EXPORT_SYMBOL(free_irq);
511 * IRQ autodetection code..
513 * This depends on the fact that any interrupt that comes in on to an
514 * unassigned IRQ will cause GxICR_DETECT to be set
517 static DECLARE_MUTEX(probe_sem);
520 * probe_irq_on - begin an interrupt autodetect
522 * Commence probing for an interrupt. The interrupts are scanned
523 * and a mask of potential interrupt lines is returned.
527 unsigned long probe_irq_on(void)
533 EXPORT_SYMBOL(probe_irq_on);
536 * Return a mask of triggered interrupts (this
537 * can handle only legacy ISA interrupts).
541 * probe_irq_mask - scan a bitmap of interrupt lines
542 * @val: mask of interrupts to consider
544 * Scan the ISA bus interrupt lines and return a bitmap of
545 * active interrupts. The interrupt probe logic state is then
546 * returned to its previous value.
548 * Note: we need to scan all the irq's even though we will
549 * only return ISA irq numbers - just so that we reset them
550 * all to a known state.
552 unsigned int probe_irq_mask(unsigned long xmask)
558 EXPORT_SYMBOL(probe_irq_mask);
561 * Return the one interrupt that triggered (this can
562 * handle any interrupt source).
566 * probe_irq_off - end an interrupt autodetect
567 * @xmask: mask of potential interrupts (unused)
569 * Scans the unused interrupt lines and returns the line which
570 * appears to have triggered the interrupt. If no interrupt was
571 * found then zero is returned. If more than one interrupt is
572 * found then minus the first candidate is returned to indicate
575 * The interrupt probe logic state is returned to its previous
578 * BUGS: When used in a module (which arguably shouldnt happen)
579 * nothing prevents two IRQ probe callers from overlapping. The
580 * results of this are non-optimal.
583 int probe_irq_off(unsigned long xmask)
589 EXPORT_SYMBOL(probe_irq_off);
591 /* this was setup_x86_irq but it seems pretty generic */
592 int setup_irq(unsigned int irq, struct irqaction *new)
594 struct irq_source *source;
595 struct irq_group *group;
596 struct irq_level *level;
597 struct irqaction **p, **pp;
600 group = irq_groups[irq >> NR_IRQ_LOG2_ACTIONS_PER_GROUP];
604 source = group->sources[irq & (NR_IRQ_ACTIONS_PER_GROUP - 1)];
608 level = source->level;
610 p = &group->actions[irq & (NR_IRQ_ACTIONS_PER_GROUP - 1)];
613 * Some drivers like serial.c use request_irq() heavily,
614 * so we have to be careful not to interfere with a
617 if (new->flags & SA_SAMPLE_RANDOM) {
619 * This function might sleep, we want to call it first,
620 * outside of the atomic block.
621 * Yes, this might clear the entropy pool if the wrong
622 * driver is attempted to be loaded, without actually
623 * installing a new handler, but is this really a problem,
624 * only the sysadmin is able to do this.
626 rand_initialize_irq(irq);
629 /* must juggle the interrupt processing stuff with interrupts disabled */
630 spin_lock_irqsave(&level->lock, flags);
632 /* can't share interrupts unless all parties agree to */
633 if (level->usage != 0 && !(level->flags & new->flags & SA_SHIRQ)) {
634 spin_unlock_irqrestore(&level->lock,flags);
638 /* add new interrupt at end of irq queue */
646 level->flags = new->flags;
648 /* turn the interrupts on */
649 if (level->usage == 1)
650 __clr_MASK(level - frv_irq_levels);
652 if (p == pp && group->control)
653 group->control(group, irq & (NR_IRQ_ACTIONS_PER_GROUP - 1), 1);
655 spin_unlock_irqrestore(&level->lock, flags);
656 register_irq_proc(irq);
660 static struct proc_dir_entry * root_irq_dir;
661 static struct proc_dir_entry * irq_dir [NR_IRQS];
665 static unsigned int parse_hex_value (const char *buffer,
666 unsigned long count, unsigned long *ret)
668 unsigned char hexnum [HEX_DIGITS];
674 if (count > HEX_DIGITS)
676 if (copy_from_user(hexnum, buffer, count))
680 * Parse the first 8 characters as a hex string, any non-hex char
681 * is end-of-string. '00e1', 'e1', '00E1', 'E1' are all the same.
685 for (i = 0; i < count; i++) {
686 unsigned int c = hexnum[i];
689 case '0' ... '9': c -= '0'; break;
690 case 'a' ... 'f': c -= 'a'-10; break;
691 case 'A' ... 'F': c -= 'A'-10; break;
695 value = (value << 4) | c;
703 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
704 int count, int *eof, void *data)
706 unsigned long *mask = (unsigned long *) data;
707 if (count < HEX_DIGITS+1)
709 return sprintf (page, "%08lx\n", *mask);
712 static int prof_cpu_mask_write_proc (struct file *file, const char *buffer,
713 unsigned long count, void *data)
715 unsigned long *mask = (unsigned long *) data, full_count = count, err;
716 unsigned long new_value;
719 err = parse_hex_value(buffer, count, &new_value);
727 #define MAX_NAMELEN 10
729 static void register_irq_proc (unsigned int irq)
731 char name [MAX_NAMELEN];
733 if (!root_irq_dir || irq_dir[irq])
736 memset(name, 0, MAX_NAMELEN);
737 sprintf(name, "%d", irq);
739 /* create /proc/irq/1234 */
740 irq_dir[irq] = proc_mkdir(name, root_irq_dir);
743 unsigned long prof_cpu_mask = -1;
745 void init_irq_proc (void)
747 struct proc_dir_entry *entry;
750 /* create /proc/irq */
751 root_irq_dir = proc_mkdir("irq", 0);
753 /* create /proc/irq/prof_cpu_mask */
754 entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
759 entry->data = (void *)&prof_cpu_mask;
760 entry->read_proc = prof_cpu_mask_read_proc;
761 entry->write_proc = prof_cpu_mask_write_proc;
764 * Create entries for all existing IRQs.
766 for (i = 0; i < NR_IRQS; i++)
767 register_irq_proc(i);
770 /*****************************************************************************/
772 * initialise the interrupt system
774 void __init init_IRQ(void)
778 #ifdef CONFIG_FUJITSU_MB93493
779 route_mb93493_irqs();
781 } /* end init_IRQ() */