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@cs.nmt.edu)
9 * Copyright (C) 1996 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 program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
19 * This file contains the code used by various IRQ handling routines:
20 * asking for different IRQ's should be done through these routines
21 * instead of just grabbing them. Thus setups with different IRQ numbers
22 * shouldn't result in any weird surprises, and installing new handlers
26 #include <linux/errno.h>
27 #include <linux/module.h>
28 #include <linux/threads.h>
29 #include <linux/kernel_stat.h>
30 #include <linux/signal.h>
31 #include <linux/sched.h>
32 #include <linux/ioport.h>
33 #include <linux/interrupt.h>
34 #include <linux/timex.h>
35 #include <linux/config.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/pci.h>
39 #include <linux/delay.h>
40 #include <linux/irq.h>
41 #include <linux/proc_fs.h>
42 #include <linux/random.h>
43 #include <linux/kallsyms.h>
45 #include <asm/uaccess.h>
46 #include <asm/bitops.h>
47 #include <asm/system.h>
49 #include <asm/pgtable.h>
51 #include <asm/cache.h>
53 #include <asm/ptrace.h>
54 #include <asm/iSeries/LparData.h>
55 #include <asm/machdep.h>
59 extern void iSeries_smp_message_recv( struct pt_regs * );
62 static void register_irq_proc (unsigned int irq);
64 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
66 .lock = SPIN_LOCK_UNLOCKED
70 int __irq_offset_value;
71 int ppc_spurious_interrupts = 0;
72 unsigned long lpEvent_count = 0;
75 setup_irq(unsigned int irq, struct irqaction * new)
79 struct irqaction *old, **p;
80 irq_desc_t *desc = get_irq_desc(irq);
83 * Some drivers like serial.c use request_irq() heavily,
84 * so we have to be careful not to interfere with a
87 if (new->flags & SA_SAMPLE_RANDOM) {
89 * This function might sleep, we want to call it first,
90 * outside of the atomic block.
91 * Yes, this might clear the entropy pool if the wrong
92 * driver is attempted to be loaded, without actually
93 * installing a new handler, but is this really a problem,
94 * only the sysadmin is able to do this.
96 rand_initialize_irq(irq);
100 * The following block of code has to be executed atomically
102 spin_lock_irqsave(&desc->lock,flags);
104 if ((old = *p) != NULL) {
105 /* Can't share interrupts unless both agree to */
106 if (!(old->flags & new->flags & SA_SHIRQ)) {
107 spin_unlock_irqrestore(&desc->lock,flags);
111 /* add new interrupt at end of irq queue */
123 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING | IRQ_INPROGRESS);
124 if (desc->handler && desc->handler->startup)
125 desc->handler->startup(irq);
128 spin_unlock_irqrestore(&desc->lock,flags);
130 register_irq_proc(irq);
136 inline void synchronize_irq(unsigned int irq)
138 while (get_irq_desc(irq)->status & IRQ_INPROGRESS)
142 EXPORT_SYMBOL(synchronize_irq);
144 #endif /* CONFIG_SMP */
146 /* XXX Make this into free_irq() - Anton */
148 /* This could be promoted to a real free_irq() ... */
150 do_free_irq(int irq, void* dev_id)
152 irq_desc_t *desc = get_irq_desc(irq);
153 struct irqaction **p;
156 spin_lock_irqsave(&desc->lock,flags);
159 struct irqaction * action = *p;
161 struct irqaction **pp = p;
163 if (action->dev_id != dev_id)
166 /* Found it - now remove it from the list of entries */
169 desc->status |= IRQ_DISABLED;
172 spin_unlock_irqrestore(&desc->lock,flags);
174 /* Wait to make sure it's not being used on another CPU */
175 synchronize_irq(irq);
179 printk("Trying to free free IRQ%d\n",irq);
180 spin_unlock_irqrestore(&desc->lock,flags);
187 int request_irq(unsigned int irq,
188 irqreturn_t (*handler)(int, void *, struct pt_regs *),
189 unsigned long irqflags, const char * devname, void *dev_id)
191 struct irqaction *action;
197 /* We could implement really free_irq() instead of that... */
198 return do_free_irq(irq, dev_id);
200 action = (struct irqaction *)
201 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
203 printk(KERN_ERR "kmalloc() failed for irq %d !\n", irq);
207 action->handler = handler;
208 action->flags = irqflags;
210 action->name = devname;
211 action->dev_id = dev_id;
214 retval = setup_irq(irq, action);
221 EXPORT_SYMBOL(request_irq);
223 void free_irq(unsigned int irq, void *dev_id)
225 request_irq(irq, NULL, 0, NULL, dev_id);
228 EXPORT_SYMBOL(free_irq);
231 * Generic enable/disable code: this just calls
232 * down into the PIC-specific version for the actual
233 * hardware disable after having gotten the irq
238 * disable_irq_nosync - disable an irq without waiting
239 * @irq: Interrupt to disable
241 * Disable the selected interrupt line. Disables of an interrupt
242 * stack. Unlike disable_irq(), this function does not ensure existing
243 * instances of the IRQ handler have completed before returning.
245 * This function may be called from IRQ context.
248 inline void disable_irq_nosync(unsigned int irq)
250 irq_desc_t *desc = get_irq_desc(irq);
253 spin_lock_irqsave(&desc->lock, flags);
254 if (!desc->depth++) {
255 if (!(desc->status & IRQ_PER_CPU))
256 desc->status |= IRQ_DISABLED;
259 spin_unlock_irqrestore(&desc->lock, flags);
262 EXPORT_SYMBOL(disable_irq_nosync);
265 * disable_irq - disable an irq and wait for completion
266 * @irq: Interrupt to disable
268 * Disable the selected interrupt line. Disables of an interrupt
269 * stack. That is for two disables you need two enables. This
270 * function waits for any pending IRQ handlers for this interrupt
271 * to complete before returning. If you use this function while
272 * holding a resource the IRQ handler may need you will deadlock.
274 * This function may be called - with care - from IRQ context.
277 void disable_irq(unsigned int irq)
279 irq_desc_t *desc = get_irq_desc(irq);
280 disable_irq_nosync(irq);
282 synchronize_irq(irq);
285 EXPORT_SYMBOL(disable_irq);
288 * enable_irq - enable interrupt handling on an irq
289 * @irq: Interrupt to enable
291 * Re-enables the processing of interrupts on this IRQ line
292 * providing no disable_irq calls are now in effect.
294 * This function may be called from IRQ context.
297 void enable_irq(unsigned int irq)
299 irq_desc_t *desc = get_irq_desc(irq);
302 spin_lock_irqsave(&desc->lock, flags);
303 switch (desc->depth) {
305 unsigned int status = desc->status & ~IRQ_DISABLED;
306 desc->status = status;
307 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
308 desc->status = status | IRQ_REPLAY;
309 hw_resend_irq(desc->handler,irq);
318 printk("enable_irq(%u) unbalanced from %p\n", irq,
319 __builtin_return_address(0));
321 spin_unlock_irqrestore(&desc->lock, flags);
324 EXPORT_SYMBOL(enable_irq);
326 int show_interrupts(struct seq_file *p, void *v)
328 int i = *(loff_t *) v, j;
329 struct irqaction * action;
335 for (j=0; j<NR_CPUS; j++) {
337 seq_printf(p, "CPU%d ",j);
343 desc = get_irq_desc(i);
344 spin_lock_irqsave(&desc->lock, flags);
345 action = desc->action;
346 if (!action || !action->handler)
348 seq_printf(p, "%3d: ", i);
350 for (j = 0; j < NR_CPUS; j++) {
352 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
355 seq_printf(p, "%10u ", kstat_irqs(i));
356 #endif /* CONFIG_SMP */
358 seq_printf(p, " %s ", desc->handler->typename );
360 seq_printf(p, " None ");
361 seq_printf(p, "%s", (desc->status & IRQ_LEVEL) ? "Level " : "Edge ");
362 seq_printf(p, " %s",action->name);
363 for (action=action->next; action; action = action->next)
364 seq_printf(p, ", %s", action->name);
367 spin_unlock_irqrestore(&desc->lock, flags);
368 } else if (i == NR_IRQS)
369 seq_printf(p, "BAD: %10u\n", ppc_spurious_interrupts);
373 static inline int handle_irq_event(int irq, struct pt_regs *regs,
374 struct irqaction *action)
379 #ifndef CONFIG_PPC_ISERIES
380 if (!(action->flags & SA_INTERRUPT))
385 status |= action->flags;
386 retval |= action->handler(irq, action->dev_id, regs);
387 action = action->next;
389 if (status & SA_SAMPLE_RANDOM)
390 add_interrupt_randomness(irq);
391 #ifndef CONFIG_PPC_ISERIES
397 static void __report_bad_irq(int irq, irq_desc_t *desc, irqreturn_t action_ret)
399 struct irqaction *action;
401 if (action_ret != IRQ_HANDLED && action_ret != IRQ_NONE) {
402 printk(KERN_ERR "irq event %d: bogus return value %x\n",
405 printk(KERN_ERR "irq %d: nobody cared!\n", irq);
408 printk(KERN_ERR "handlers:\n");
409 action = desc->action;
411 printk(KERN_ERR "[<%p>]", action->handler);
412 print_symbol(" (%s)",
413 (unsigned long)action->handler);
415 action = action->next;
419 static void report_bad_irq(int irq, irq_desc_t *desc, irqreturn_t action_ret)
421 static int count = 100;
425 __report_bad_irq(irq, desc, action_ret);
429 static int noirqdebug;
431 static int __init noirqdebug_setup(char *str)
434 printk("IRQ lockup detection disabled\n");
438 __setup("noirqdebug", noirqdebug_setup);
441 * If 99,900 of the previous 100,000 interrupts have not been handled then
442 * assume that the IRQ is stuck in some manner. Drop a diagnostic and try to
445 * (The other 100-of-100,000 interrupts may have been a correctly-functioning
446 * device sharing an IRQ with the failing one)
448 * Called under desc->lock
450 static void note_interrupt(int irq, irq_desc_t *desc, irqreturn_t action_ret)
452 if (action_ret != IRQ_HANDLED) {
453 desc->irqs_unhandled++;
454 if (action_ret != IRQ_NONE)
455 report_bad_irq(irq, desc, action_ret);
459 if (desc->irq_count < 100000)
463 if (desc->irqs_unhandled > 99900) {
465 * The interrupt is stuck
467 __report_bad_irq(irq, desc, action_ret);
471 printk(KERN_EMERG "Disabling IRQ #%d\n", irq);
472 desc->status |= IRQ_DISABLED;
473 desc->handler->disable(irq);
475 desc->irqs_unhandled = 0;
479 * Eventually, this should take an array of interrupts and an array size
480 * so it can dispatch multiple interrupts.
482 void ppc_irq_dispatch_handler(struct pt_regs *regs, int irq)
485 struct irqaction *action;
486 int cpu = smp_processor_id();
487 irq_desc_t *desc = get_irq_desc(irq);
488 irqreturn_t action_ret;
490 kstat_cpu(cpu).irqs[irq]++;
492 if (desc->status & IRQ_PER_CPU) {
493 /* no locking required for CPU-local interrupts: */
495 action_ret = handle_irq_event(irq, regs, desc->action);
496 desc->handler->end(irq);
500 spin_lock(&desc->lock);
503 REPLAY is when Linux resends an IRQ that was dropped earlier
504 WAITING is used by probe to mark irqs that are being tested
506 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
507 status |= IRQ_PENDING; /* we _want_ to handle it */
510 * If the IRQ is disabled for whatever reason, we cannot
511 * use the action we have.
514 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
515 action = desc->action;
516 if (!action || !action->handler) {
517 ppc_spurious_interrupts++;
518 printk(KERN_DEBUG "Unhandled interrupt %x, disabled\n", irq);
519 /* We can't call disable_irq here, it would deadlock */
522 desc->status |= IRQ_DISABLED;
523 /* This is not a real spurrious interrupt, we
524 * have to eoi it, so we jump to out
529 status &= ~IRQ_PENDING; /* we commit to handling */
530 status |= IRQ_INPROGRESS; /* we are handling it */
532 desc->status = status;
535 * If there is no IRQ handler or it was disabled, exit early.
536 Since we set PENDING, if another processor is handling
537 a different instance of this same irq, the other processor
538 will take care of it.
540 if (unlikely(!action))
544 * Edge triggered interrupts need to remember
546 * This applies to any hw interrupts that allow a second
547 * instance of the same irq to arrive while we are in do_IRQ
548 * or in the handler. But the code here only handles the _second_
549 * instance of the irq, not the third or fourth. So it is mostly
550 * useful for irq hardware that does not mask cleanly in an
554 spin_unlock(&desc->lock);
555 action_ret = handle_irq_event(irq, regs, action);
556 spin_lock(&desc->lock);
558 note_interrupt(irq, desc, action_ret);
559 if (likely(!(desc->status & IRQ_PENDING)))
561 desc->status &= ~IRQ_PENDING;
564 desc->status &= ~IRQ_INPROGRESS;
566 * The ->end() handler has to deal with interrupts which got
567 * disabled while the handler was running.
570 if (desc->handler->end)
571 desc->handler->end(irq);
572 else if (desc->handler->enable)
573 desc->handler->enable(irq);
575 spin_unlock(&desc->lock);
578 #ifdef CONFIG_PPC_ISERIES
579 int do_IRQ(struct pt_regs *regs)
581 struct paca_struct *lpaca;
582 struct ItLpQueue *lpq;
586 #ifdef CONFIG_DEBUG_STACKOVERFLOW
587 /* Debugging check for stack overflow: is there less than 4KB free? */
591 sp = __get_SP() & (THREAD_SIZE-1);
593 if (unlikely(sp < (sizeof(struct thread_info) + 4096))) {
594 printk("do_IRQ: stack overflow: %ld\n",
595 sp - sizeof(struct thread_info));
603 if (lpaca->xLpPaca.xIntDword.xFields.xIpiCnt) {
604 lpaca->xLpPaca.xIntDword.xFields.xIpiCnt = 0;
605 iSeries_smp_message_recv(regs);
607 #endif /* CONFIG_SMP */
608 lpq = lpaca->lpQueuePtr;
609 if (lpq && ItLpQueue_isLpIntPending(lpq))
610 lpEvent_count += ItLpQueue_process(lpq, regs);
614 if (lpaca->xLpPaca.xIntDword.xFields.xDecrInt) {
615 lpaca->xLpPaca.xIntDword.xFields.xDecrInt = 0;
616 /* Signal a fake decrementer interrupt */
617 timer_interrupt(regs);
620 return 1; /* lets ret_from_int know we can do checks */
623 #else /* CONFIG_PPC_ISERIES */
625 int do_IRQ(struct pt_regs *regs)
632 * Every arch is required to implement ppc_md.get_irq.
633 * This function will either return an irq number or -1 to
634 * indicate there are no more pending. But the first time
635 * through the loop this means there wasn't an IRQ pending.
636 * The value -2 is for buggy hardware and means that this IRQ
637 * has already been handled. -- Tom
639 while ((irq = ppc_md.get_irq(regs)) >= 0) {
640 ppc_irq_dispatch_handler(regs, irq);
643 if (irq != -2 && first)
644 /* That's not SMP safe ... but who cares ? */
645 ppc_spurious_interrupts++;
649 return 1; /* lets ret_from_int know we can do checks */
651 #endif /* CONFIG_PPC_ISERIES */
653 unsigned long probe_irq_on (void)
658 EXPORT_SYMBOL(probe_irq_on);
660 int probe_irq_off (unsigned long irqs)
665 EXPORT_SYMBOL(probe_irq_off);
667 unsigned int probe_irq_mask(unsigned long irqs)
672 void __init init_IRQ(void)
684 static struct proc_dir_entry * root_irq_dir;
685 static struct proc_dir_entry * irq_dir [NR_IRQS];
686 static struct proc_dir_entry * smp_affinity_entry [NR_IRQS];
688 /* Protected by get_irq_desc(irq)->lock. */
689 #ifdef CONFIG_IRQ_ALL_CPUS
690 cpumask_t irq_affinity [NR_IRQS] = { [0 ... NR_IRQS-1] = CPU_MASK_ALL };
691 #else /* CONFIG_IRQ_ALL_CPUS */
692 cpumask_t irq_affinity [NR_IRQS] = { [0 ... NR_IRQS-1] = CPU_MASK_NONE };
693 #endif /* CONFIG_IRQ_ALL_CPUS */
695 static int irq_affinity_read_proc (char *page, char **start, off_t off,
696 int count, int *eof, void *data)
698 int len = cpumask_scnprintf(page, count, irq_affinity[(long)data]);
701 len += sprintf(page + len, "\n");
705 static int irq_affinity_write_proc (struct file *file, const char *buffer,
706 unsigned long count, void *data)
708 unsigned int irq = (long)data;
709 irq_desc_t *desc = get_irq_desc(irq);
711 cpumask_t new_value, tmp;
712 cpumask_t allcpus = CPU_MASK_ALL;
714 if (!desc->handler->set_affinity)
717 ret = cpumask_parse(buffer, count, new_value);
722 * We check for CPU_MASK_ALL in xics to send irqs to all cpus.
723 * In some cases CPU_MASK_ALL is smaller than the cpumask (eg
724 * NR_CPUS == 32 and cpumask is a long), so we mask it here to
727 cpus_and(new_value, new_value, allcpus);
730 * Grab lock here so cpu_online_map can't change, and also
731 * protect irq_affinity[].
733 spin_lock(&desc->lock);
736 * Do not allow disabling IRQs completely - it's a too easy
737 * way to make the system unusable accidentally :-) At least
738 * one online CPU still has to be targeted.
740 cpus_and(tmp, new_value, cpu_online_map);
741 if (cpus_empty(tmp)) {
746 irq_affinity[irq] = new_value;
747 desc->handler->set_affinity(irq, new_value);
751 spin_unlock(&desc->lock);
755 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
756 int count, int *eof, void *data)
758 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
761 len += sprintf(page + len, "\n");
765 static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
766 unsigned long count, void *data)
768 cpumask_t *mask = (cpumask_t *)data;
769 unsigned long full_count = count, err;
772 err = cpumask_parse(buffer, count, new_value);
778 #ifdef CONFIG_PPC_ISERIES
781 for (i=0; i<NR_CPUS; ++i) {
782 if ( paca[i].prof_buffer && (new_value & 1) )
783 paca[i].prof_enabled = 1;
785 paca[i].prof_enabled = 0;
794 #define MAX_NAMELEN 10
796 static void register_irq_proc (unsigned int irq)
798 struct proc_dir_entry *entry;
799 char name [MAX_NAMELEN];
801 if (!root_irq_dir || (irq_desc[irq].handler == NULL) || irq_dir[irq])
804 memset(name, 0, MAX_NAMELEN);
805 sprintf(name, "%d", irq);
807 /* create /proc/irq/1234 */
808 irq_dir[irq] = proc_mkdir(name, root_irq_dir);
810 /* create /proc/irq/1234/smp_affinity */
811 entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
815 entry->data = (void *)(long)irq;
816 entry->read_proc = irq_affinity_read_proc;
817 entry->write_proc = irq_affinity_write_proc;
820 smp_affinity_entry[irq] = entry;
823 unsigned long prof_cpu_mask = -1;
825 void init_irq_proc (void)
827 struct proc_dir_entry *entry;
830 /* create /proc/irq */
831 root_irq_dir = proc_mkdir("irq", 0);
833 /* create /proc/irq/prof_cpu_mask */
834 entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
840 entry->data = (void *)&prof_cpu_mask;
841 entry->read_proc = prof_cpu_mask_read_proc;
842 entry->write_proc = prof_cpu_mask_write_proc;
845 * Create entries for all existing IRQs.
848 if (get_irq_desc(i)->handler == NULL)
850 register_irq_proc(i);
854 irqreturn_t no_action(int irq, void *dev, struct pt_regs *regs)
859 #ifndef CONFIG_PPC_ISERIES
861 * Virtual IRQ mapping code, used on systems with XICS interrupt controllers.
864 #define UNDEFINED_IRQ 0xffffffff
865 unsigned int virt_irq_to_real_map[NR_IRQS];
868 * Don't use virtual irqs 0, 1, 2 for devices.
869 * The pcnet32 driver considers interrupt numbers < 2 to be invalid,
870 * and 2 is the XICS IPI interrupt.
871 * We limit virtual irqs to 17 less than NR_IRQS so that when we
872 * offset them by 16 (to reserve the first 16 for ISA interrupts)
873 * we don't end up with an interrupt number >= NR_IRQS.
875 #define MIN_VIRT_IRQ 3
876 #define MAX_VIRT_IRQ (NR_IRQS - NUM_ISA_INTERRUPTS - 1)
877 #define NR_VIRT_IRQS (MAX_VIRT_IRQ - MIN_VIRT_IRQ + 1)
883 for (i = 0; i < NR_IRQS; i++)
884 virt_irq_to_real_map[i] = UNDEFINED_IRQ;
887 /* Create a mapping for a real_irq if it doesn't already exist.
888 * Return the virtual irq as a convenience.
890 int virt_irq_create_mapping(unsigned int real_irq)
892 unsigned int virq, first_virq;
895 if (naca->interrupt_controller == IC_OPEN_PIC)
896 return real_irq; /* no mapping for openpic (for now) */
898 /* don't map interrupts < MIN_VIRT_IRQ */
899 if (real_irq < MIN_VIRT_IRQ) {
900 virt_irq_to_real_map[real_irq] = real_irq;
904 /* map to a number between MIN_VIRT_IRQ and MAX_VIRT_IRQ */
906 if (virq > MAX_VIRT_IRQ)
907 virq = (virq % NR_VIRT_IRQS) + MIN_VIRT_IRQ;
909 /* search for this number or a free slot */
911 while (virt_irq_to_real_map[virq] != UNDEFINED_IRQ) {
912 if (virt_irq_to_real_map[virq] == real_irq)
914 if (++virq > MAX_VIRT_IRQ)
916 if (virq == first_virq)
917 goto nospace; /* oops, no free slots */
920 virt_irq_to_real_map[virq] = real_irq;
925 printk(KERN_CRIT "Interrupt table is full\n");
926 printk(KERN_CRIT "Increase NR_IRQS (currently %d) "
927 "in your kernel sources and rebuild.\n", NR_IRQS);
934 * In most cases will get a hit on the very first slot checked in the
935 * virt_irq_to_real_map. Only when there are a large number of
936 * IRQs will this be expensive.
938 unsigned int real_irq_to_virt_slowpath(unsigned int real_irq)
941 unsigned int first_virq;
945 if (virq > MAX_VIRT_IRQ)
946 virq = (virq % NR_VIRT_IRQS) + MIN_VIRT_IRQ;
951 if (virt_irq_to_real_map[virq] == real_irq)
956 if (virq >= MAX_VIRT_IRQ)
959 } while (first_virq != virq);