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_each_online_cpu(j
)
79 seq_printf(p
, "CPU%d ",j
);
84 case 1 ... NR_IRQ_GROUPS
* NR_IRQ_ACTIONS_PER_GROUP
:
85 local_irq_save(flags
);
87 grp
= (i
- 1) / NR_IRQ_ACTIONS_PER_GROUP
;
88 group
= irq_groups
[grp
];
92 ix
= (i
- 1) % NR_IRQ_ACTIONS_PER_GROUP
;
93 action
= group
->actions
[ix
];
97 seq_printf(p
, "%3d: ", i
- 1);
100 seq_printf(p
, "%10u ", kstat_irqs(i
));
102 for_each_online_cpu(j
)
103 seq_printf(p
, "%10u ", kstat_cpu(j
).irqs
[i
- 1]);
106 level
= group
->sources
[ix
]->level
- frv_irq_levels
;
108 seq_printf(p
, " %12s@%x", group
->sources
[ix
]->muxname
, level
);
109 seq_printf(p
, " %s", action
->name
);
111 for (action
= action
->next
; action
; action
= action
->next
)
112 seq_printf(p
, ", %s", action
->name
);
116 local_irq_restore(flags
);
119 case NR_IRQ_GROUPS
* NR_IRQ_ACTIONS_PER_GROUP
+ 1:
120 seq_printf(p
, "ERR: %10u\n", atomic_read(&irq_err_count
));
132 * Generic enable/disable code: this just calls
133 * down into the PIC-specific version for the actual
134 * hardware disable after having gotten the irq
139 * disable_irq_nosync - disable an irq without waiting
140 * @irq: Interrupt to disable
142 * Disable the selected interrupt line. Disables and Enables are
144 * Unlike disable_irq(), this function does not ensure existing
145 * instances of the IRQ handler have completed before returning.
147 * This function may be called from IRQ context.
150 void disable_irq_nosync(unsigned int irq
)
152 struct irq_source
*source
;
153 struct irq_group
*group
;
154 struct irq_level
*level
;
156 int idx
= irq
& (NR_IRQ_ACTIONS_PER_GROUP
- 1);
158 group
= irq_groups
[irq
>> NR_IRQ_LOG2_ACTIONS_PER_GROUP
];
162 source
= group
->sources
[idx
];
166 level
= source
->level
;
168 spin_lock_irqsave(&level
->lock
, flags
);
170 if (group
->control
) {
171 if (!group
->disable_cnt
[idx
]++)
172 group
->control(group
, idx
, 0);
173 } else if (!level
->disable_count
++) {
174 __set_MASK(level
- frv_irq_levels
);
177 spin_unlock_irqrestore(&level
->lock
, flags
);
180 EXPORT_SYMBOL(disable_irq_nosync
);
183 * disable_irq - disable an irq and wait for completion
184 * @irq: Interrupt to disable
186 * Disable the selected interrupt line. Enables and Disables are
188 * This function waits for any pending IRQ handlers for this interrupt
189 * to complete before returning. If you use this function while
190 * holding a resource the IRQ handler may need you will deadlock.
192 * This function may be called - with care - from IRQ context.
195 void disable_irq(unsigned int irq
)
197 disable_irq_nosync(irq
);
200 if (!local_irq_count(smp_processor_id())) {
203 } while (irq_desc
[irq
].status
& IRQ_INPROGRESS
);
208 EXPORT_SYMBOL(disable_irq
);
211 * enable_irq - enable handling of an irq
212 * @irq: Interrupt to enable
214 * Undoes the effect of one call to disable_irq(). If this
215 * matches the last disable, processing of interrupts on this
216 * IRQ line is re-enabled.
218 * This function may be called from IRQ context.
221 void enable_irq(unsigned int irq
)
223 struct irq_source
*source
;
224 struct irq_group
*group
;
225 struct irq_level
*level
;
227 int idx
= irq
& (NR_IRQ_ACTIONS_PER_GROUP
- 1);
230 group
= irq_groups
[irq
>> NR_IRQ_LOG2_ACTIONS_PER_GROUP
];
234 source
= group
->sources
[idx
];
238 level
= source
->level
;
240 spin_lock_irqsave(&level
->lock
, flags
);
243 count
= group
->disable_cnt
[idx
];
245 count
= level
->disable_count
;
249 if (group
->control
) {
250 if (group
->actions
[idx
])
251 group
->control(group
, idx
, 1);
254 __clr_MASK(level
- frv_irq_levels
);
263 printk("enable_irq(%u) unbalanced from %p\n", irq
, __builtin_return_address(0));
267 group
->disable_cnt
[idx
] = count
;
269 level
->disable_count
= count
;
271 spin_unlock_irqrestore(&level
->lock
, flags
);
274 EXPORT_SYMBOL(enable_irq
);
276 /*****************************************************************************/
278 * handles all normal device IRQ's
279 * - registers are referred to by the __frame variable (GR28)
280 * - IRQ distribution is complicated in this arch because of the many PICs, the
281 * way they work and the way they cascade
283 asmlinkage
void do_IRQ(void)
285 struct irq_source
*source
;
290 level
= (__frame
->tbr
>> 4) & 0xf;
291 cpu
= smp_processor_id();
293 if ((unsigned long) __frame
- (unsigned long) (current
+ 1) < 512)
300 kstat_this_cpu
.irqs
[level
]++;
302 for (source
= frv_irq_levels
[level
].sources
; source
; source
= source
->next
)
303 source
->doirq(source
);
311 /*****************************************************************************/
313 * handles all NMIs when not co-opted by the debugger
314 * - registers are referred to by the __frame variable (GR28)
316 asmlinkage
void do_NMI(void)
320 /*****************************************************************************/
322 * request_irq - allocate an interrupt line
323 * @irq: Interrupt line to allocate
324 * @handler: Function to be called when the IRQ occurs
325 * @irqflags: Interrupt type flags
326 * @devname: An ascii name for the claiming device
327 * @dev_id: A cookie passed back to the handler function
329 * This call allocates interrupt resources and enables the
330 * interrupt line and IRQ handling. From the point this
331 * call is made your handler function may be invoked. Since
332 * your handler function must clear any interrupt the board
333 * raises, you must take care both to initialise your hardware
334 * and to set up the interrupt handler in the right order.
336 * Dev_id must be globally unique. Normally the address of the
337 * device data structure is used as the cookie. Since the handler
338 * receives this value it makes sense to use it.
340 * If your interrupt is shared you must pass a non NULL dev_id
341 * as this is required when freeing the interrupt.
345 * SA_SHIRQ Interrupt is shared
347 * SA_INTERRUPT Disable local interrupts while processing
349 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
353 int request_irq(unsigned int irq
,
354 irqreturn_t (*handler
)(int, void *, struct pt_regs
*),
355 unsigned long irqflags
,
356 const char * devname
,
360 struct irqaction
*action
;
364 * Sanity-check: shared interrupts should REALLY pass in
365 * a real dev-ID, otherwise we'll have trouble later trying
366 * to figure out which interrupt is which (messes up the
367 * interrupt freeing logic etc).
369 if (irqflags
& SA_SHIRQ
) {
371 printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n",
372 devname
, (&irq
)[-1]);
376 if ((irq
>> NR_IRQ_LOG2_ACTIONS_PER_GROUP
) >= NR_IRQ_GROUPS
)
381 action
= (struct irqaction
*) kmalloc(sizeof(struct irqaction
), GFP_KERNEL
);
385 action
->handler
= handler
;
386 action
->flags
= irqflags
;
387 action
->mask
= CPU_MASK_NONE
;
388 action
->name
= devname
;
390 action
->dev_id
= dev_id
;
392 retval
= setup_irq(irq
, action
);
398 EXPORT_SYMBOL(request_irq
);
401 * free_irq - free an interrupt
402 * @irq: Interrupt line to free
403 * @dev_id: Device identity to free
405 * Remove an interrupt handler. The handler is removed and if the
406 * interrupt line is no longer in use by any driver it is disabled.
407 * On a shared IRQ the caller must ensure the interrupt is disabled
408 * on the card it drives before calling this function. The function
409 * does not return until any executing interrupts for this IRQ
412 * This function may be called from interrupt context.
414 * Bugs: Attempting to free an irq in a handler for the same irq hangs
418 void free_irq(unsigned int irq
, void *dev_id
)
420 struct irq_source
*source
;
421 struct irq_group
*group
;
422 struct irq_level
*level
;
423 struct irqaction
**p
, **pp
;
426 if ((irq
>> NR_IRQ_LOG2_ACTIONS_PER_GROUP
) >= NR_IRQ_GROUPS
)
429 group
= irq_groups
[irq
>> NR_IRQ_LOG2_ACTIONS_PER_GROUP
];
433 source
= group
->sources
[irq
& (NR_IRQ_ACTIONS_PER_GROUP
- 1)];
437 level
= source
->level
;
438 p
= &group
->actions
[irq
& (NR_IRQ_ACTIONS_PER_GROUP
- 1)];
440 spin_lock_irqsave(&level
->lock
, flags
);
442 for (pp
= p
; *pp
; pp
= &(*pp
)->next
) {
443 struct irqaction
*action
= *pp
;
445 if (action
->dev_id
!= dev_id
)
448 /* found it - remove from the list of entries */
453 if (p
== pp
&& group
->control
)
454 group
->control(group
, irq
& (NR_IRQ_ACTIONS_PER_GROUP
- 1), 0);
456 if (level
->usage
== 0)
457 __set_MASK(level
- frv_irq_levels
);
459 spin_unlock_irqrestore(&level
->lock
,flags
);
462 /* Wait to make sure it's not being used on another CPU */
463 while (desc
->status
& IRQ_INPROGRESS
)
471 EXPORT_SYMBOL(free_irq
);
474 * IRQ autodetection code..
476 * This depends on the fact that any interrupt that comes in on to an
477 * unassigned IRQ will cause GxICR_DETECT to be set
480 static DECLARE_MUTEX(probe_sem
);
483 * probe_irq_on - begin an interrupt autodetect
485 * Commence probing for an interrupt. The interrupts are scanned
486 * and a mask of potential interrupt lines is returned.
490 unsigned long probe_irq_on(void)
496 EXPORT_SYMBOL(probe_irq_on
);
499 * Return a mask of triggered interrupts (this
500 * can handle only legacy ISA interrupts).
504 * probe_irq_mask - scan a bitmap of interrupt lines
505 * @val: mask of interrupts to consider
507 * Scan the ISA bus interrupt lines and return a bitmap of
508 * active interrupts. The interrupt probe logic state is then
509 * returned to its previous value.
511 * Note: we need to scan all the irq's even though we will
512 * only return ISA irq numbers - just so that we reset them
513 * all to a known state.
515 unsigned int probe_irq_mask(unsigned long xmask
)
521 EXPORT_SYMBOL(probe_irq_mask
);
524 * Return the one interrupt that triggered (this can
525 * handle any interrupt source).
529 * probe_irq_off - end an interrupt autodetect
530 * @xmask: mask of potential interrupts (unused)
532 * Scans the unused interrupt lines and returns the line which
533 * appears to have triggered the interrupt. If no interrupt was
534 * found then zero is returned. If more than one interrupt is
535 * found then minus the first candidate is returned to indicate
538 * The interrupt probe logic state is returned to its previous
541 * BUGS: When used in a module (which arguably shouldnt happen)
542 * nothing prevents two IRQ probe callers from overlapping. The
543 * results of this are non-optimal.
546 int probe_irq_off(unsigned long xmask
)
552 EXPORT_SYMBOL(probe_irq_off
);
554 /* this was setup_x86_irq but it seems pretty generic */
555 int setup_irq(unsigned int irq
, struct irqaction
*new)
557 struct irq_source
*source
;
558 struct irq_group
*group
;
559 struct irq_level
*level
;
560 struct irqaction
**p
, **pp
;
563 group
= irq_groups
[irq
>> NR_IRQ_LOG2_ACTIONS_PER_GROUP
];
567 source
= group
->sources
[irq
& (NR_IRQ_ACTIONS_PER_GROUP
- 1)];
571 level
= source
->level
;
573 p
= &group
->actions
[irq
& (NR_IRQ_ACTIONS_PER_GROUP
- 1)];
576 * Some drivers like serial.c use request_irq() heavily,
577 * so we have to be careful not to interfere with a
580 if (new->flags
& SA_SAMPLE_RANDOM
) {
582 * This function might sleep, we want to call it first,
583 * outside of the atomic block.
584 * Yes, this might clear the entropy pool if the wrong
585 * driver is attempted to be loaded, without actually
586 * installing a new handler, but is this really a problem,
587 * only the sysadmin is able to do this.
589 rand_initialize_irq(irq
);
592 /* must juggle the interrupt processing stuff with interrupts disabled */
593 spin_lock_irqsave(&level
->lock
, flags
);
595 /* can't share interrupts unless all parties agree to */
596 if (level
->usage
!= 0 && !(level
->flags
& new->flags
& SA_SHIRQ
)) {
597 spin_unlock_irqrestore(&level
->lock
,flags
);
601 /* add new interrupt at end of irq queue */
609 level
->flags
= new->flags
;
611 /* turn the interrupts on */
612 if (level
->usage
== 1)
613 __clr_MASK(level
- frv_irq_levels
);
615 if (p
== pp
&& group
->control
)
616 group
->control(group
, irq
& (NR_IRQ_ACTIONS_PER_GROUP
- 1), 1);
618 spin_unlock_irqrestore(&level
->lock
, flags
);
619 register_irq_proc(irq
);
623 static struct proc_dir_entry
* root_irq_dir
;
624 static struct proc_dir_entry
* irq_dir
[NR_IRQS
];
628 static unsigned int parse_hex_value (const char *buffer
,
629 unsigned long count
, unsigned long *ret
)
631 unsigned char hexnum
[HEX_DIGITS
];
637 if (count
> HEX_DIGITS
)
639 if (copy_from_user(hexnum
, buffer
, count
))
643 * Parse the first 8 characters as a hex string, any non-hex char
644 * is end-of-string. '00e1', 'e1', '00E1', 'E1' are all the same.
648 for (i
= 0; i
< count
; i
++) {
649 unsigned int c
= hexnum
[i
];
652 case '0' ... '9': c
-= '0'; break;
653 case 'a' ... 'f': c
-= 'a'-10; break;
654 case 'A' ... 'F': c
-= 'A'-10; break;
658 value
= (value
<< 4) | c
;
666 static int prof_cpu_mask_read_proc (char *page
, char **start
, off_t off
,
667 int count
, int *eof
, void *data
)
669 unsigned long *mask
= (unsigned long *) data
;
670 if (count
< HEX_DIGITS
+1)
672 return sprintf (page
, "%08lx\n", *mask
);
675 static int prof_cpu_mask_write_proc (struct file
*file
, const char *buffer
,
676 unsigned long count
, void *data
)
678 unsigned long *mask
= (unsigned long *) data
, full_count
= count
, err
;
679 unsigned long new_value
;
682 err
= parse_hex_value(buffer
, count
, &new_value
);
690 #define MAX_NAMELEN 10
692 static void register_irq_proc (unsigned int irq
)
694 char name
[MAX_NAMELEN
];
696 if (!root_irq_dir
|| irq_dir
[irq
])
699 memset(name
, 0, MAX_NAMELEN
);
700 sprintf(name
, "%d", irq
);
702 /* create /proc/irq/1234 */
703 irq_dir
[irq
] = proc_mkdir(name
, root_irq_dir
);
706 unsigned long prof_cpu_mask
= -1;
708 void init_irq_proc (void)
710 struct proc_dir_entry
*entry
;
713 /* create /proc/irq */
714 root_irq_dir
= proc_mkdir("irq", 0);
716 /* create /proc/irq/prof_cpu_mask */
717 entry
= create_proc_entry("prof_cpu_mask", 0600, root_irq_dir
);
722 entry
->data
= (void *)&prof_cpu_mask
;
723 entry
->read_proc
= prof_cpu_mask_read_proc
;
724 entry
->write_proc
= prof_cpu_mask_write_proc
;
727 * Create entries for all existing IRQs.
729 for (i
= 0; i
< NR_IRQS
; i
++)
730 register_irq_proc(i
);
733 /*****************************************************************************/
735 * initialise the interrupt system
737 void __init
init_IRQ(void)
741 #ifdef CONFIG_FUJITSU_MB93493
742 route_mb93493_irqs();
744 } /* end init_IRQ() */