2 * arch/ppc/kernel/irq.c
4 * Derived from arch/i386/kernel/irq.c
5 * Copyright (C) 1992 Linus Torvalds
6 * Adapted from arch/i386 by Gary Thomas
7 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
8 * Updated and modified by Cort Dougan <cort@fsmlabs.com>
9 * Copyright (C) 1996-2001 Cort Dougan
10 * Adapted for Power Macintosh by Paul Mackerras
11 * Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
12 * Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
14 * This file contains the code used by various IRQ handling routines:
15 * asking for different IRQ's should be done through these routines
16 * instead of just grabbing them. Thus setups with different IRQ numbers
17 * shouldn't result in any weird surprises, and installing new handlers
20 * The MPC8xx has an interrupt mask in the SIU. If a bit is set, the
21 * interrupt is _enabled_. As expected, IRQ0 is bit 0 in the 32-bit
22 * mask register (of which only 16 are defined), hence the weird shifting
23 * and complement of the cached_irq_mask. I want to be able to stuff
24 * this right into the SIU SMASK register.
25 * Many of the prep/chrp functions are conditional compiled on CONFIG_8xx
26 * to reduce code space and undefined function references.
29 #include <linux/errno.h>
30 #include <linux/module.h>
31 #include <linux/threads.h>
32 #include <linux/kernel_stat.h>
33 #include <linux/signal.h>
34 #include <linux/sched.h>
35 #include <linux/ptrace.h>
36 #include <linux/ioport.h>
37 #include <linux/interrupt.h>
38 #include <linux/timex.h>
39 #include <linux/config.h>
40 #include <linux/init.h>
41 #include <linux/slab.h>
42 #include <linux/pci.h>
43 #include <linux/delay.h>
44 #include <linux/irq.h>
45 #include <linux/proc_fs.h>
46 #include <linux/random.h>
47 #include <linux/seq_file.h>
48 #include <linux/cpumask.h>
50 #include <asm/uaccess.h>
51 #include <asm/bitops.h>
52 #include <asm/system.h>
54 #include <asm/pgtable.h>
56 #include <asm/cache.h>
58 #include <asm/ptrace.h>
60 #define NR_MASK_WORDS ((NR_IRQS + 31) / 32)
62 extern atomic_t ipi_recv;
63 extern atomic_t ipi_sent;
64 void enable_irq(unsigned int irq_nr);
65 void disable_irq(unsigned int irq_nr);
67 static void register_irq_proc (unsigned int irq);
69 #define MAXCOUNT 10000000
71 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
73 .lock = SPIN_LOCK_UNLOCKED
77 int ppc_spurious_interrupts = 0;
78 struct irqaction *ppc_irq_action[NR_IRQS];
79 unsigned long ppc_cached_irq_mask[NR_MASK_WORDS];
80 unsigned long ppc_lost_interrupts[NR_MASK_WORDS];
81 atomic_t ppc_n_lost_interrupts;
83 /* nasty hack for shared irq's since we need to do kmalloc calls but
84 * can't very early in the boot when we need to do a request irq.
85 * this needs to be removed.
88 #define IRQ_KMALLOC_ENTRIES 8
89 static int cache_bitmask = 0;
90 static struct irqaction malloc_cache[IRQ_KMALLOC_ENTRIES];
91 extern int mem_init_done;
93 #if defined(CONFIG_TAU_INT)
94 extern int tau_interrupts(unsigned long cpu);
95 extern int tau_initialized;
98 void *irq_kmalloc(size_t size, int pri)
102 return kmalloc(size,pri);
103 for ( i = 0; i < IRQ_KMALLOC_ENTRIES ; i++ )
104 if ( ! ( cache_bitmask & (1<<i) ) )
106 cache_bitmask |= (1<<i);
107 return (void *)(&malloc_cache[i]);
112 void irq_kfree(void *ptr)
115 for ( i = 0 ; i < IRQ_KMALLOC_ENTRIES ; i++ )
116 if ( ptr == &malloc_cache[i] )
118 cache_bitmask &= ~(1<<i);
125 setup_irq(unsigned int irq, struct irqaction * new)
129 struct irqaction *old, **p;
130 irq_desc_t *desc = irq_desc + irq;
133 * Some drivers like serial.c use request_irq() heavily,
134 * so we have to be careful not to interfere with a
137 if (new->flags & SA_SAMPLE_RANDOM) {
139 * This function might sleep, we want to call it first,
140 * outside of the atomic block.
141 * Yes, this might clear the entropy pool if the wrong
142 * driver is attempted to be loaded, without actually
143 * installing a new handler, but is this really a problem,
144 * only the sysadmin is able to do this.
146 rand_initialize_irq(irq);
150 * The following block of code has to be executed atomically
152 spin_lock_irqsave(&desc->lock,flags);
154 if ((old = *p) != NULL) {
155 /* Can't share interrupts unless both agree to */
156 if (!(old->flags & new->flags & SA_SHIRQ)) {
157 spin_unlock_irqrestore(&desc->lock,flags);
161 /* add new interrupt at end of irq queue */
173 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING);
176 spin_unlock_irqrestore(&desc->lock,flags);
178 register_irq_proc(irq);
182 void free_irq(unsigned int irq, void* dev_id)
185 struct irqaction **p;
188 desc = irq_desc + irq;
189 spin_lock_irqsave(&desc->lock,flags);
192 struct irqaction * action = *p;
194 struct irqaction **pp = p;
196 if (action->dev_id != dev_id)
199 /* Found it - now remove it from the list of entries */
202 desc->status |= IRQ_DISABLED;
205 spin_unlock_irqrestore(&desc->lock,flags);
207 synchronize_irq(irq);
211 printk("Trying to free free IRQ%d\n",irq);
212 spin_unlock_irqrestore(&desc->lock,flags);
218 EXPORT_SYMBOL(free_irq);
220 int request_irq(unsigned int irq,
221 irqreturn_t (*handler)(int, void *, struct pt_regs *),
222 unsigned long irqflags, const char * devname, void *dev_id)
224 struct irqaction *action;
230 printk(KERN_ERR "request_irq called with NULL handler!\n");
235 action = (struct irqaction *)
236 irq_kmalloc(sizeof(struct irqaction), GFP_KERNEL);
238 printk(KERN_ERR "irq_kmalloc() failed for irq %d !\n", irq);
242 action->handler = handler;
243 action->flags = irqflags;
245 action->name = devname;
246 action->dev_id = dev_id;
249 retval = setup_irq(irq, action);
258 EXPORT_SYMBOL(request_irq);
261 * Generic enable/disable code: this just calls
262 * down into the PIC-specific version for the actual
263 * hardware disable after having gotten the irq
268 * disable_irq_nosync - disable an irq without waiting
269 * @irq: Interrupt to disable
271 * Disable the selected interrupt line. Disables of an interrupt
272 * stack. Unlike disable_irq(), this function does not ensure existing
273 * instances of the IRQ handler have completed before returning.
275 * This function may be called from IRQ context.
278 void disable_irq_nosync(unsigned int irq)
280 irq_desc_t *desc = irq_desc + irq;
283 spin_lock_irqsave(&desc->lock, flags);
284 if (!desc->depth++) {
285 if (!(desc->status & IRQ_PER_CPU))
286 desc->status |= IRQ_DISABLED;
289 spin_unlock_irqrestore(&desc->lock, flags);
293 * disable_irq - disable an irq and wait for completion
294 * @irq: Interrupt to disable
296 * Disable the selected interrupt line. Disables of an interrupt
297 * stack. That is for two disables you need two enables. This
298 * function waits for any pending IRQ handlers for this interrupt
299 * to complete before returning. If you use this function while
300 * holding a resource the IRQ handler may need you will deadlock.
302 * This function may be called - with care - from IRQ context.
305 void disable_irq(unsigned int irq)
307 irq_desc_t *desc = irq_desc + irq;
308 disable_irq_nosync(irq);
310 synchronize_irq(irq);
314 * enable_irq - enable interrupt handling on an irq
315 * @irq: Interrupt to enable
317 * Re-enables the processing of interrupts on this IRQ line
318 * providing no disable_irq calls are now in effect.
320 * This function may be called from IRQ context.
323 void enable_irq(unsigned int irq)
325 irq_desc_t *desc = irq_desc + irq;
328 spin_lock_irqsave(&desc->lock, flags);
329 switch (desc->depth) {
331 unsigned int status = desc->status & ~IRQ_DISABLED;
332 desc->status = status;
333 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
334 desc->status = status | IRQ_REPLAY;
335 hw_resend_irq(desc->handler,irq);
344 printk("enable_irq(%u) unbalanced\n", irq);
346 spin_unlock_irqrestore(&desc->lock, flags);
349 int show_interrupts(struct seq_file *p, void *v)
351 int i = *(loff_t *) v, j;
352 struct irqaction * action;
357 for (j=0; j<NR_CPUS; j++)
359 seq_printf(p, "CPU%d ", j);
364 spin_lock_irqsave(&irq_desc[i].lock, flags);
365 action = irq_desc[i].action;
366 if ( !action || !action->handler )
368 seq_printf(p, "%3d: ", i);
370 for (j = 0; j < NR_CPUS; j++)
372 seq_printf(p, "%10u ",
373 kstat_cpu(j).irqs[i]);
375 seq_printf(p, "%10u ", kstat_irqs(i));
376 #endif /* CONFIG_SMP */
377 if (irq_desc[i].handler)
378 seq_printf(p, " %s ", irq_desc[i].handler->typename);
380 seq_puts(p, " None ");
381 seq_printf(p, "%s", (irq_desc[i].status & IRQ_LEVEL) ? "Level " : "Edge ");
382 seq_printf(p, " %s", action->name);
383 for (action = action->next; action; action = action->next)
384 seq_printf(p, ", %s", action->name);
387 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
388 } else if (i == NR_IRQS) {
389 #ifdef CONFIG_TAU_INT
390 if (tau_initialized){
391 seq_puts(p, "TAU: ");
392 for (j = 0; j < NR_CPUS; j++)
394 seq_printf(p, "%10u ", tau_interrupts(j));
395 seq_puts(p, " PowerPC Thermal Assist (cpu temp)\n");
399 /* should this be per processor send/receive? */
400 seq_printf(p, "IPI (recv/sent): %10u/%u\n",
401 atomic_read(&ipi_recv), atomic_read(&ipi_sent));
403 seq_printf(p, "BAD: %10u\n", ppc_spurious_interrupts);
409 handle_irq_event(int irq, struct pt_regs *regs, struct irqaction *action)
413 if (!(action->flags & SA_INTERRUPT))
417 status |= action->flags;
418 action->handler(irq, action->dev_id, regs);
419 action = action->next;
421 if (status & SA_SAMPLE_RANDOM)
422 add_interrupt_randomness(irq);
427 * Eventually, this should take an array of interrupts and an array size
428 * so it can dispatch multiple interrupts.
430 void ppc_irq_dispatch_handler(struct pt_regs *regs, int irq)
433 struct irqaction *action;
434 irq_desc_t *desc = irq_desc + irq;
436 kstat_this_cpu.irqs[irq]++;
437 spin_lock(&desc->lock);
440 REPLAY is when Linux resends an IRQ that was dropped earlier
441 WAITING is used by probe to mark irqs that are being tested
443 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
444 if (!(status & IRQ_PER_CPU))
445 status |= IRQ_PENDING; /* we _want_ to handle it */
448 * If the IRQ is disabled for whatever reason, we cannot
449 * use the action we have.
452 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
453 action = desc->action;
454 if (!action || !action->handler) {
455 ppc_spurious_interrupts++;
456 printk(KERN_DEBUG "Unhandled interrupt %x, disabled\n", irq);
457 /* We can't call disable_irq here, it would deadlock */
459 desc->status |= IRQ_DISABLED;
461 /* This is a real interrupt, we have to eoi it,
465 status &= ~IRQ_PENDING; /* we commit to handling */
466 if (!(status & IRQ_PER_CPU))
467 status |= IRQ_INPROGRESS; /* we are handling it */
469 desc->status = status;
472 * If there is no IRQ handler or it was disabled, exit early.
473 Since we set PENDING, if another processor is handling
474 a different instance of this same irq, the other processor
475 will take care of it.
477 if (unlikely(!action))
482 * Edge triggered interrupts need to remember
484 * This applies to any hw interrupts that allow a second
485 * instance of the same irq to arrive while we are in do_IRQ
486 * or in the handler. But the code here only handles the _second_
487 * instance of the irq, not the third or fourth. So it is mostly
488 * useful for irq hardware that does not mask cleanly in an
492 spin_unlock(&desc->lock);
493 handle_irq_event(irq, regs, action);
494 spin_lock(&desc->lock);
496 if (likely(!(desc->status & IRQ_PENDING)))
498 desc->status &= ~IRQ_PENDING;
501 desc->status &= ~IRQ_INPROGRESS;
503 * The ->end() handler has to deal with interrupts which got
504 * disabled while the handler was running.
506 if (irq_desc[irq].handler) {
507 if (irq_desc[irq].handler->end)
508 irq_desc[irq].handler->end(irq);
509 else if (irq_desc[irq].handler->enable)
510 irq_desc[irq].handler->enable(irq);
512 spin_unlock(&desc->lock);
515 void do_IRQ(struct pt_regs *regs)
521 * Every platform is required to implement ppc_md.get_irq.
522 * This function will either return an irq number or -1 to
523 * indicate there are no more pending. But the first time
524 * through the loop this means there wasn't and IRQ pending.
525 * The value -2 is for buggy hardware and means that this IRQ
526 * has already been handled. -- Tom
528 while ((irq = ppc_md.get_irq(regs)) >= 0) {
529 ppc_irq_dispatch_handler(regs, irq);
532 if (irq != -2 && first)
533 /* That's not SMP safe ... but who cares ? */
534 ppc_spurious_interrupts++;
538 unsigned long probe_irq_on (void)
543 EXPORT_SYMBOL(probe_irq_on);
545 int probe_irq_off (unsigned long irqs)
550 EXPORT_SYMBOL(probe_irq_off);
552 unsigned int probe_irq_mask(unsigned long irqs)
558 void synchronize_irq(unsigned int irq)
560 while (irq_desc[irq].status & IRQ_INPROGRESS)
563 #endif /* CONFIG_SMP */
565 static struct proc_dir_entry *root_irq_dir;
566 static struct proc_dir_entry *irq_dir[NR_IRQS];
567 static struct proc_dir_entry *smp_affinity_entry[NR_IRQS];
569 #ifdef CONFIG_IRQ_ALL_CPUS
570 #define DEFAULT_CPU_AFFINITY CPU_MASK_ALL
572 #define DEFAULT_CPU_AFFINITY cpumask_of_cpu(0)
575 cpumask_t irq_affinity [NR_IRQS];
577 static int irq_affinity_read_proc (char *page, char **start, off_t off,
578 int count, int *eof, void *data)
580 int len = cpumask_scnprintf(page, count, irq_affinity[(long)data]);
583 len += sprintf(page + len, "\n");
587 static int irq_affinity_write_proc (struct file *file, const char __user *buffer,
588 unsigned long count, void *data)
590 int irq = (int) data, full_count = count, err;
591 cpumask_t new_value, tmp;
593 if (!irq_desc[irq].handler->set_affinity)
596 err = cpumask_parse(buffer, count, new_value);
599 * Do not allow disabling IRQs completely - it's a too easy
600 * way to make the system unusable accidentally :-) At least
601 * one online CPU still has to be targeted.
603 * We assume a 1-1 logical<->physical cpu mapping here. If
604 * we assume that the cpu indices in /proc/irq/../smp_affinity
605 * are actually logical cpu #'s then we have no problem.
606 * -- Cort <cort@fsmlabs.com>
608 cpus_and(tmp, new_value, cpu_online_map);
612 irq_affinity[irq] = new_value;
613 irq_desc[irq].handler->set_affinity(irq, new_value);
618 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
619 int count, int *eof, void *data)
621 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
624 len += sprintf(page + len, "\n");
628 static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
629 unsigned long count, void *data)
631 cpumask_t *mask = (cpumask_t *)data, full_count = count, err;
634 err = cpumask_parse(buffer, count, new_value);
642 #define MAX_NAMELEN 10
644 static void register_irq_proc (unsigned int irq)
646 struct proc_dir_entry *entry;
647 char name [MAX_NAMELEN];
649 if (!root_irq_dir || (irq_desc[irq].handler == NULL) || irq_dir[irq])
652 memset(name, 0, MAX_NAMELEN);
653 sprintf(name, "%d", irq);
655 /* create /proc/irq/1234 */
656 irq_dir[irq] = proc_mkdir(name, root_irq_dir);
658 /* create /proc/irq/1234/smp_affinity */
659 entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
662 entry->data = (void *)irq;
663 entry->read_proc = irq_affinity_read_proc;
664 entry->write_proc = irq_affinity_write_proc;
666 smp_affinity_entry[irq] = entry;
669 unsigned long prof_cpu_mask = -1;
671 void init_irq_proc (void)
673 struct proc_dir_entry *entry;
676 /* create /proc/irq */
677 root_irq_dir = proc_mkdir("irq", 0);
679 /* create /proc/irq/prof_cpu_mask */
680 entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
683 entry->data = (void *)&prof_cpu_mask;
684 entry->read_proc = prof_cpu_mask_read_proc;
685 entry->write_proc = prof_cpu_mask_write_proc;
688 * Create entries for all existing IRQs.
690 for (i = 0; i < NR_IRQS; i++) {
691 if (irq_desc[i].handler == NULL)
693 register_irq_proc(i);
697 irqreturn_t no_action(int irq, void *dev, struct pt_regs *regs)
702 void __init init_IRQ(void)
706 for (i = 0; i < NR_IRQS; ++i)
707 irq_affinity[i] = DEFAULT_CPU_AFFINITY;