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/ptrace.h>
20 #include <linux/errno.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_lock.h>
29 #include <linux/init.h>
30 #include <linux/kernel_stat.h>
31 #include <linux/irq.h>
32 #include <linux/proc_fs.h>
33 #include <linux/seq_file.h>
34 #include <linux/module.h>
36 #include <asm/atomic.h>
39 #include <asm/system.h>
40 #include <asm/bitops.h>
41 #include <asm/uaccess.h>
42 #include <asm/pgalloc.h>
43 #include <asm/delay.h>
45 #include <asm/irc-regs.h>
46 #include <asm/irq-routing.h>
47 #include <asm/gdb-stub.h>
49 extern void __init fpga_init(void);
50 extern void __init route_mb93493_irqs(void);
52 static void register_irq_proc (unsigned int irq);
55 * Special irq handlers.
58 irqreturn_t no_action(int cpl, void *dev_id, struct pt_regs *regs) { return IRQ_HANDLED; }
60 atomic_t irq_err_count;
63 * Generic, controller-independent functions:
65 int show_interrupts(struct seq_file *p, void *v)
67 struct irqaction *action;
68 struct irq_group *group;
70 int level, grp, ix, i, j;
77 for_each_online_cpu(j)
78 seq_printf(p, "CPU%d ",j);
83 case 1 ... NR_IRQ_GROUPS * NR_IRQ_ACTIONS_PER_GROUP:
84 local_irq_save(flags);
86 grp = (i - 1) / NR_IRQ_ACTIONS_PER_GROUP;
87 group = irq_groups[grp];
91 ix = (i - 1) % NR_IRQ_ACTIONS_PER_GROUP;
92 action = group->actions[ix];
96 seq_printf(p, "%3d: ", i - 1);
99 seq_printf(p, "%10u ", kstat_irqs(i));
101 for_each_online_cpu(j)
102 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i - 1]);
105 level = group->sources[ix]->level - frv_irq_levels;
107 seq_printf(p, " %12s@%x", group->sources[ix]->muxname, level);
108 seq_printf(p, " %s", action->name);
110 for (action = action->next; action; action = action->next)
111 seq_printf(p, ", %s", action->name);
115 local_irq_restore(flags);
118 case NR_IRQ_GROUPS * NR_IRQ_ACTIONS_PER_GROUP + 1:
119 seq_printf(p, "ERR: %10u\n", atomic_read(&irq_err_count));
131 * Generic enable/disable code: this just calls
132 * down into the PIC-specific version for the actual
133 * hardware disable after having gotten the irq
138 * disable_irq_nosync - disable an irq without waiting
139 * @irq: Interrupt to disable
141 * Disable the selected interrupt line. Disables and Enables are
143 * Unlike disable_irq(), this function does not ensure existing
144 * instances of the IRQ handler have completed before returning.
146 * This function may be called from IRQ context.
149 void disable_irq_nosync(unsigned int irq)
151 struct irq_source *source;
152 struct irq_group *group;
153 struct irq_level *level;
155 int idx = irq & (NR_IRQ_ACTIONS_PER_GROUP - 1);
157 group = irq_groups[irq >> NR_IRQ_LOG2_ACTIONS_PER_GROUP];
161 source = group->sources[idx];
165 level = source->level;
167 spin_lock_irqsave(&level->lock, flags);
169 if (group->control) {
170 if (!group->disable_cnt[idx]++)
171 group->control(group, idx, 0);
172 } else if (!level->disable_count++) {
173 __set_MASK(level - frv_irq_levels);
176 spin_unlock_irqrestore(&level->lock, flags);
179 EXPORT_SYMBOL(disable_irq_nosync);
182 * disable_irq - disable an irq and wait for completion
183 * @irq: Interrupt to disable
185 * Disable the selected interrupt line. Enables and Disables are
187 * This function waits for any pending IRQ handlers for this interrupt
188 * to complete before returning. If you use this function while
189 * holding a resource the IRQ handler may need you will deadlock.
191 * This function may be called - with care - from IRQ context.
194 void disable_irq(unsigned int irq)
196 disable_irq_nosync(irq);
199 if (!local_irq_count(smp_processor_id())) {
202 } while (irq_desc[irq].status & IRQ_INPROGRESS);
207 EXPORT_SYMBOL(disable_irq);
210 * enable_irq - enable handling of an irq
211 * @irq: Interrupt to enable
213 * Undoes the effect of one call to disable_irq(). If this
214 * matches the last disable, processing of interrupts on this
215 * IRQ line is re-enabled.
217 * This function may be called from IRQ context.
220 void enable_irq(unsigned int irq)
222 struct irq_source *source;
223 struct irq_group *group;
224 struct irq_level *level;
226 int idx = irq & (NR_IRQ_ACTIONS_PER_GROUP - 1);
229 group = irq_groups[irq >> NR_IRQ_LOG2_ACTIONS_PER_GROUP];
233 source = group->sources[idx];
237 level = source->level;
239 spin_lock_irqsave(&level->lock, flags);
242 count = group->disable_cnt[idx];
244 count = level->disable_count;
248 if (group->control) {
249 if (group->actions[idx])
250 group->control(group, idx, 1);
253 __clr_MASK(level - frv_irq_levels);
262 printk("enable_irq(%u) unbalanced from %p\n", irq, __builtin_return_address(0));
266 group->disable_cnt[idx] = count;
268 level->disable_count = count;
270 spin_unlock_irqrestore(&level->lock, flags);
273 EXPORT_SYMBOL(enable_irq);
275 /*****************************************************************************/
277 * handles all normal device IRQ's
278 * - registers are referred to by the __frame variable (GR28)
279 * - IRQ distribution is complicated in this arch because of the many PICs, the
280 * way they work and the way they cascade
282 asmlinkage void do_IRQ(void)
284 struct irq_source *source;
289 level = (__frame->tbr >> 4) & 0xf;
290 cpu = smp_processor_id();
292 if ((unsigned long) __frame - (unsigned long) (current + 1) < 512)
299 kstat_this_cpu.irqs[level]++;
301 for (source = frv_irq_levels[level].sources; source; source = source->next)
302 source->doirq(source);
310 /*****************************************************************************/
312 * handles all NMIs when not co-opted by the debugger
313 * - registers are referred to by the __frame variable (GR28)
315 asmlinkage void do_NMI(void)
319 /*****************************************************************************/
321 * request_irq - allocate an interrupt line
322 * @irq: Interrupt line to allocate
323 * @handler: Function to be called when the IRQ occurs
324 * @irqflags: Interrupt type flags
325 * @devname: An ascii name for the claiming device
326 * @dev_id: A cookie passed back to the handler function
328 * This call allocates interrupt resources and enables the
329 * interrupt line and IRQ handling. From the point this
330 * call is made your handler function may be invoked. Since
331 * your handler function must clear any interrupt the board
332 * raises, you must take care both to initialise your hardware
333 * and to set up the interrupt handler in the right order.
335 * Dev_id must be globally unique. Normally the address of the
336 * device data structure is used as the cookie. Since the handler
337 * receives this value it makes sense to use it.
339 * If your interrupt is shared you must pass a non NULL dev_id
340 * as this is required when freeing the interrupt.
344 * IRQF_SHARED Interrupt is shared
346 * IRQF_DISABLED Disable local interrupts while processing
348 * IRQF_SAMPLE_RANDOM The interrupt can be used for entropy
352 int request_irq(unsigned int irq,
353 irqreturn_t (*handler)(int, void *, struct pt_regs *),
354 unsigned long irqflags,
355 const char * devname,
359 struct irqaction *action;
363 * Sanity-check: shared interrupts should REALLY pass in
364 * a real dev-ID, otherwise we'll have trouble later trying
365 * to figure out which interrupt is which (messes up the
366 * interrupt freeing logic etc).
368 if (irqflags & IRQF_SHARED) {
370 printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n",
371 devname, (&irq)[-1]);
375 if ((irq >> NR_IRQ_LOG2_ACTIONS_PER_GROUP) >= NR_IRQ_GROUPS)
380 action = (struct irqaction *) kmalloc(sizeof(struct irqaction), GFP_KERNEL);
384 action->handler = handler;
385 action->flags = irqflags;
386 action->mask = CPU_MASK_NONE;
387 action->name = devname;
389 action->dev_id = dev_id;
391 retval = setup_irq(irq, action);
397 EXPORT_SYMBOL(request_irq);
400 * free_irq - free an interrupt
401 * @irq: Interrupt line to free
402 * @dev_id: Device identity to free
404 * Remove an interrupt handler. The handler is removed and if the
405 * interrupt line is no longer in use by any driver it is disabled.
406 * On a shared IRQ the caller must ensure the interrupt is disabled
407 * on the card it drives before calling this function. The function
408 * does not return until any executing interrupts for this IRQ
411 * This function may be called from interrupt context.
413 * Bugs: Attempting to free an irq in a handler for the same irq hangs
417 void free_irq(unsigned int irq, void *dev_id)
419 struct irq_source *source;
420 struct irq_group *group;
421 struct irq_level *level;
422 struct irqaction **p, **pp;
425 if ((irq >> NR_IRQ_LOG2_ACTIONS_PER_GROUP) >= NR_IRQ_GROUPS)
428 group = irq_groups[irq >> NR_IRQ_LOG2_ACTIONS_PER_GROUP];
432 source = group->sources[irq & (NR_IRQ_ACTIONS_PER_GROUP - 1)];
436 level = source->level;
437 p = &group->actions[irq & (NR_IRQ_ACTIONS_PER_GROUP - 1)];
439 spin_lock_irqsave(&level->lock, flags);
441 for (pp = p; *pp; pp = &(*pp)->next) {
442 struct irqaction *action = *pp;
444 if (action->dev_id != dev_id)
447 /* found it - remove from the list of entries */
452 if (p == pp && group->control)
453 group->control(group, irq & (NR_IRQ_ACTIONS_PER_GROUP - 1), 0);
455 if (level->usage == 0)
456 __set_MASK(level - frv_irq_levels);
458 spin_unlock_irqrestore(&level->lock,flags);
461 /* Wait to make sure it's not being used on another CPU */
462 while (desc->status & IRQ_INPROGRESS)
470 EXPORT_SYMBOL(free_irq);
473 * IRQ autodetection code..
475 * This depends on the fact that any interrupt that comes in on to an
476 * unassigned IRQ will cause GxICR_DETECT to be set
479 static DECLARE_MUTEX(probe_sem);
482 * probe_irq_on - begin an interrupt autodetect
484 * Commence probing for an interrupt. The interrupts are scanned
485 * and a mask of potential interrupt lines is returned.
489 unsigned long probe_irq_on(void)
495 EXPORT_SYMBOL(probe_irq_on);
498 * Return a mask of triggered interrupts (this
499 * can handle only legacy ISA interrupts).
503 * probe_irq_mask - scan a bitmap of interrupt lines
504 * @val: mask of interrupts to consider
506 * Scan the ISA bus interrupt lines and return a bitmap of
507 * active interrupts. The interrupt probe logic state is then
508 * returned to its previous value.
510 * Note: we need to scan all the irq's even though we will
511 * only return ISA irq numbers - just so that we reset them
512 * all to a known state.
514 unsigned int probe_irq_mask(unsigned long xmask)
520 EXPORT_SYMBOL(probe_irq_mask);
523 * Return the one interrupt that triggered (this can
524 * handle any interrupt source).
528 * probe_irq_off - end an interrupt autodetect
529 * @xmask: mask of potential interrupts (unused)
531 * Scans the unused interrupt lines and returns the line which
532 * appears to have triggered the interrupt. If no interrupt was
533 * found then zero is returned. If more than one interrupt is
534 * found then minus the first candidate is returned to indicate
537 * The interrupt probe logic state is returned to its previous
540 * BUGS: When used in a module (which arguably shouldnt happen)
541 * nothing prevents two IRQ probe callers from overlapping. The
542 * results of this are non-optimal.
545 int probe_irq_off(unsigned long xmask)
551 EXPORT_SYMBOL(probe_irq_off);
553 /* this was setup_x86_irq but it seems pretty generic */
554 int setup_irq(unsigned int irq, struct irqaction *new)
556 struct irq_source *source;
557 struct irq_group *group;
558 struct irq_level *level;
559 struct irqaction **p, **pp;
562 group = irq_groups[irq >> NR_IRQ_LOG2_ACTIONS_PER_GROUP];
566 source = group->sources[irq & (NR_IRQ_ACTIONS_PER_GROUP - 1)];
570 level = source->level;
572 p = &group->actions[irq & (NR_IRQ_ACTIONS_PER_GROUP - 1)];
575 * Some drivers like serial.c use request_irq() heavily,
576 * so we have to be careful not to interfere with a
579 if (new->flags & IRQF_SAMPLE_RANDOM) {
581 * This function might sleep, we want to call it first,
582 * outside of the atomic block.
583 * Yes, this might clear the entropy pool if the wrong
584 * driver is attempted to be loaded, without actually
585 * installing a new handler, but is this really a problem,
586 * only the sysadmin is able to do this.
588 rand_initialize_irq(irq);
591 /* must juggle the interrupt processing stuff with interrupts disabled */
592 spin_lock_irqsave(&level->lock, flags);
594 /* can't share interrupts unless all parties agree to */
595 if (level->usage != 0 && !(level->flags & new->flags & IRQF_SHARED)) {
596 spin_unlock_irqrestore(&level->lock,flags);
600 /* add new interrupt at end of irq queue */
608 level->flags = new->flags;
610 /* turn the interrupts on */
611 if (level->usage == 1)
612 __clr_MASK(level - frv_irq_levels);
614 if (p == pp && group->control)
615 group->control(group, irq & (NR_IRQ_ACTIONS_PER_GROUP - 1), 1);
617 spin_unlock_irqrestore(&level->lock, flags);
618 register_irq_proc(irq);
622 static struct proc_dir_entry * root_irq_dir;
623 static struct proc_dir_entry * irq_dir [NR_IRQS];
627 static unsigned int parse_hex_value (const char __user *buffer,
628 unsigned long count, unsigned long *ret)
630 unsigned char hexnum [HEX_DIGITS];
636 if (count > HEX_DIGITS)
638 if (copy_from_user(hexnum, buffer, count))
642 * Parse the first 8 characters as a hex string, any non-hex char
643 * is end-of-string. '00e1', 'e1', '00E1', 'E1' are all the same.
647 for (i = 0; i < count; i++) {
648 unsigned int c = hexnum[i];
651 case '0' ... '9': c -= '0'; break;
652 case 'a' ... 'f': c -= 'a'-10; break;
653 case 'A' ... 'F': c -= 'A'-10; break;
657 value = (value << 4) | c;
665 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
666 int count, int *eof, void *data)
668 unsigned long *mask = (unsigned long *) data;
669 if (count < HEX_DIGITS+1)
671 return sprintf (page, "%08lx\n", *mask);
674 static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
675 unsigned long count, void *data)
677 unsigned long *mask = (unsigned long *) data, full_count = count, err;
678 unsigned long new_value;
681 err = parse_hex_value(buffer, count, &new_value);
689 #define MAX_NAMELEN 10
691 static void register_irq_proc (unsigned int irq)
693 char name [MAX_NAMELEN];
695 if (!root_irq_dir || irq_dir[irq])
698 memset(name, 0, MAX_NAMELEN);
699 sprintf(name, "%d", irq);
701 /* create /proc/irq/1234 */
702 irq_dir[irq] = proc_mkdir(name, root_irq_dir);
705 unsigned long prof_cpu_mask = -1;
707 void init_irq_proc (void)
709 struct proc_dir_entry *entry;
712 /* create /proc/irq */
713 root_irq_dir = proc_mkdir("irq", NULL);
715 /* create /proc/irq/prof_cpu_mask */
716 entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
721 entry->data = (void *)&prof_cpu_mask;
722 entry->read_proc = prof_cpu_mask_read_proc;
723 entry->write_proc = prof_cpu_mask_write_proc;
726 * Create entries for all existing IRQs.
728 for (i = 0; i < NR_IRQS; i++)
729 register_irq_proc(i);
732 /*****************************************************************************/
734 * initialise the interrupt system
736 void __init init_IRQ(void)
740 #ifdef CONFIG_FUJITSU_MB93493
741 route_mb93493_irqs();
743 } /* end init_IRQ() */