1 /* $Id: irq.c,v 1.114 2002/01/11 08:45:38 davem Exp $
2 * irq.c: UltraSparc IRQ handling/init/registry.
4 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
6 * Copyright (C) 1998 Jakub Jelinek (jj@ultra.linux.cz)
9 #include <linux/module.h>
10 #include <linux/sched.h>
11 #include <linux/ptrace.h>
12 #include <linux/errno.h>
13 #include <linux/kernel_stat.h>
14 #include <linux/signal.h>
16 #include <linux/interrupt.h>
17 #include <linux/slab.h>
18 #include <linux/random.h>
19 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include <linux/proc_fs.h>
22 #include <linux/seq_file.h>
23 #include <linux/bootmem.h>
24 #include <linux/irq.h>
25 #include <linux/msi.h>
27 #include <asm/ptrace.h>
28 #include <asm/processor.h>
29 #include <asm/atomic.h>
30 #include <asm/system.h>
34 #include <asm/iommu.h>
36 #include <asm/oplib.h>
38 #include <asm/timer.h>
40 #include <asm/starfire.h>
41 #include <asm/uaccess.h>
42 #include <asm/cache.h>
43 #include <asm/cpudata.h>
44 #include <asm/auxio.h>
47 /* UPA nodes send interrupt packet to UltraSparc with first data reg
48 * value low 5 (7 on Starfire) bits holding the IRQ identifier being
49 * delivered. We must translate this into a non-vector IRQ so we can
50 * set the softint on this cpu.
52 * To make processing these packets efficient and race free we use
53 * an array of irq buckets below. The interrupt vector handler in
54 * entry.S feeds incoming packets into per-cpu pil-indexed lists.
55 * The IVEC handler does not need to act atomically, the PIL dispatch
56 * code uses CAS to get an atomic snapshot of the list and clear it
59 * If you make changes to ino_bucket, please update hand coded assembler
60 * of the vectored interrupt trap handler(s) in entry.S and sun4v_ivec.S
63 /* Next handler in per-CPU IRQ worklist. We know that
64 * bucket pointers have the high 32-bits clear, so to
65 * save space we only store the bits we need.
67 /*0x00*/unsigned int irq_chain;
69 /* Virtual interrupt number assigned to this INO. */
70 /*0x04*/unsigned int virt_irq;
73 #define NUM_IVECS (IMAP_INR + 1)
74 struct ino_bucket ivector_table[NUM_IVECS] __attribute__ ((aligned (SMP_CACHE_BYTES)));
76 #define __irq_ino(irq) \
77 (((struct ino_bucket *)(unsigned long)(irq)) - &ivector_table[0])
78 #define __bucket(irq) ((struct ino_bucket *)(unsigned long)(irq))
79 #define __irq(bucket) ((unsigned int)(unsigned long)(bucket))
81 /* This has to be in the main kernel image, it cannot be
82 * turned into per-cpu data. The reason is that the main
83 * kernel image is locked into the TLB and this structure
84 * is accessed from the vectored interrupt trap handler. If
85 * access to this structure takes a TLB miss it could cause
86 * the 5-level sparc v9 trap stack to overflow.
88 #define irq_work(__cpu) &(trap_block[(__cpu)].irq_worklist)
90 static unsigned int virt_to_real_irq_table[NR_IRQS];
92 static unsigned char virt_irq_alloc(unsigned int real_irq)
96 BUILD_BUG_ON(NR_IRQS >= 256);
98 for (ent = 1; ent < NR_IRQS; ent++) {
99 if (!virt_to_real_irq_table[ent])
102 if (ent >= NR_IRQS) {
103 printk(KERN_ERR "IRQ: Out of virtual IRQs.\n");
107 virt_to_real_irq_table[ent] = real_irq;
112 #ifdef CONFIG_PCI_MSI
113 static void virt_irq_free(unsigned int virt_irq)
115 unsigned int real_irq;
117 if (virt_irq >= NR_IRQS)
120 real_irq = virt_to_real_irq_table[virt_irq];
121 virt_to_real_irq_table[virt_irq] = 0;
123 __bucket(real_irq)->virt_irq = 0;
127 static unsigned int virt_to_real_irq(unsigned char virt_irq)
129 return virt_to_real_irq_table[virt_irq];
133 * /proc/interrupts printing:
136 int show_interrupts(struct seq_file *p, void *v)
138 int i = *(loff_t *) v, j;
139 struct irqaction * action;
144 for_each_online_cpu(j)
145 seq_printf(p, "CPU%d ",j);
150 spin_lock_irqsave(&irq_desc[i].lock, flags);
151 action = irq_desc[i].action;
154 seq_printf(p, "%3d: ",i);
156 seq_printf(p, "%10u ", kstat_irqs(i));
158 for_each_online_cpu(j)
159 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
161 seq_printf(p, " %9s", irq_desc[i].chip->typename);
162 seq_printf(p, " %s", action->name);
164 for (action=action->next; action; action = action->next)
165 seq_printf(p, ", %s", action->name);
169 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
174 extern unsigned long real_hard_smp_processor_id(void);
176 static unsigned int sun4u_compute_tid(unsigned long imap, unsigned long cpuid)
180 if (this_is_starfire) {
181 tid = starfire_translate(imap, cpuid);
182 tid <<= IMAP_TID_SHIFT;
185 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
188 __asm__ ("rdpr %%ver, %0" : "=r" (ver));
189 if ((ver >> 32UL) == __JALAPENO_ID ||
190 (ver >> 32UL) == __SERRANO_ID) {
191 tid = cpuid << IMAP_TID_SHIFT;
192 tid &= IMAP_TID_JBUS;
194 unsigned int a = cpuid & 0x1f;
195 unsigned int n = (cpuid >> 5) & 0x1f;
197 tid = ((a << IMAP_AID_SHIFT) |
198 (n << IMAP_NID_SHIFT));
199 tid &= (IMAP_AID_SAFARI |
203 tid = cpuid << IMAP_TID_SHIFT;
211 struct irq_handler_data {
215 void (*pre_handler)(unsigned int, void *, void *);
216 void *pre_handler_arg1;
217 void *pre_handler_arg2;
220 static inline struct ino_bucket *virt_irq_to_bucket(unsigned int virt_irq)
222 unsigned int real_irq = virt_to_real_irq(virt_irq);
223 struct ino_bucket *bucket = NULL;
225 if (likely(real_irq))
226 bucket = __bucket(real_irq);
232 static int irq_choose_cpu(unsigned int virt_irq)
234 cpumask_t mask = irq_desc[virt_irq].affinity;
237 if (cpus_equal(mask, CPU_MASK_ALL)) {
238 static int irq_rover;
239 static DEFINE_SPINLOCK(irq_rover_lock);
242 /* Round-robin distribution... */
244 spin_lock_irqsave(&irq_rover_lock, flags);
246 while (!cpu_online(irq_rover)) {
247 if (++irq_rover >= NR_CPUS)
252 if (++irq_rover >= NR_CPUS)
254 } while (!cpu_online(irq_rover));
256 spin_unlock_irqrestore(&irq_rover_lock, flags);
260 cpus_and(tmp, cpu_online_map, mask);
265 cpuid = first_cpu(tmp);
271 static int irq_choose_cpu(unsigned int virt_irq)
273 return real_hard_smp_processor_id();
277 static void sun4u_irq_enable(unsigned int virt_irq)
279 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
282 unsigned long cpuid, imap;
285 cpuid = irq_choose_cpu(virt_irq);
288 tid = sun4u_compute_tid(imap, cpuid);
290 upa_writel(tid | IMAP_VALID, imap);
294 static void sun4u_irq_disable(unsigned int virt_irq)
296 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
299 unsigned long imap = data->imap;
300 u32 tmp = upa_readl(imap);
303 upa_writel(tmp, imap);
307 static void sun4u_irq_end(unsigned int virt_irq)
309 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
312 upa_writel(ICLR_IDLE, data->iclr);
315 static void sun4v_irq_enable(unsigned int virt_irq)
317 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
318 unsigned int ino = bucket - &ivector_table[0];
320 if (likely(bucket)) {
324 cpuid = irq_choose_cpu(virt_irq);
326 err = sun4v_intr_settarget(ino, cpuid);
328 printk("sun4v_intr_settarget(%x,%lu): err(%d)\n",
330 err = sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
332 printk("sun4v_intr_setenabled(%x): err(%d)\n",
337 static void sun4v_irq_disable(unsigned int virt_irq)
339 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
340 unsigned int ino = bucket - &ivector_table[0];
342 if (likely(bucket)) {
345 err = sun4v_intr_setenabled(ino, HV_INTR_DISABLED);
347 printk("sun4v_intr_setenabled(%x): "
348 "err(%d)\n", ino, err);
352 #ifdef CONFIG_PCI_MSI
353 static void sun4v_msi_enable(unsigned int virt_irq)
355 sun4v_irq_enable(virt_irq);
356 unmask_msi_irq(virt_irq);
359 static void sun4v_msi_disable(unsigned int virt_irq)
361 mask_msi_irq(virt_irq);
362 sun4v_irq_disable(virt_irq);
366 static void sun4v_irq_end(unsigned int virt_irq)
368 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
369 unsigned int ino = bucket - &ivector_table[0];
371 if (likely(bucket)) {
374 err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
376 printk("sun4v_intr_setstate(%x): "
377 "err(%d)\n", ino, err);
381 static void run_pre_handler(unsigned int virt_irq)
383 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
384 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
386 if (likely(data->pre_handler)) {
387 data->pre_handler(__irq_ino(__irq(bucket)),
388 data->pre_handler_arg1,
389 data->pre_handler_arg2);
393 static struct irq_chip sun4u_irq = {
395 .enable = sun4u_irq_enable,
396 .disable = sun4u_irq_disable,
397 .end = sun4u_irq_end,
400 static struct irq_chip sun4u_irq_ack = {
401 .typename = "sun4u+ack",
402 .enable = sun4u_irq_enable,
403 .disable = sun4u_irq_disable,
404 .ack = run_pre_handler,
405 .end = sun4u_irq_end,
408 static struct irq_chip sun4v_irq = {
410 .enable = sun4v_irq_enable,
411 .disable = sun4v_irq_disable,
412 .end = sun4v_irq_end,
415 static struct irq_chip sun4v_irq_ack = {
416 .typename = "sun4v+ack",
417 .enable = sun4v_irq_enable,
418 .disable = sun4v_irq_disable,
419 .ack = run_pre_handler,
420 .end = sun4v_irq_end,
423 #ifdef CONFIG_PCI_MSI
424 static struct irq_chip sun4v_msi = {
425 .typename = "sun4v+msi",
426 .mask = mask_msi_irq,
427 .unmask = unmask_msi_irq,
428 .enable = sun4v_msi_enable,
429 .disable = sun4v_msi_disable,
430 .ack = run_pre_handler,
431 .end = sun4v_irq_end,
435 void irq_install_pre_handler(int virt_irq,
436 void (*func)(unsigned int, void *, void *),
437 void *arg1, void *arg2)
439 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
440 struct irq_chip *chip;
442 data->pre_handler = func;
443 data->pre_handler_arg1 = arg1;
444 data->pre_handler_arg2 = arg2;
446 chip = get_irq_chip(virt_irq);
447 if (chip == &sun4u_irq_ack ||
448 chip == &sun4v_irq_ack
449 #ifdef CONFIG_PCI_MSI
450 || chip == &sun4v_msi
455 chip = (chip == &sun4u_irq ?
456 &sun4u_irq_ack : &sun4v_irq_ack);
457 set_irq_chip(virt_irq, chip);
460 unsigned int build_irq(int inofixup, unsigned long iclr, unsigned long imap)
462 struct ino_bucket *bucket;
463 struct irq_handler_data *data;
466 BUG_ON(tlb_type == hypervisor);
468 ino = (upa_readl(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
469 bucket = &ivector_table[ino];
470 if (!bucket->virt_irq) {
471 bucket->virt_irq = virt_irq_alloc(__irq(bucket));
472 set_irq_chip(bucket->virt_irq, &sun4u_irq);
475 data = get_irq_chip_data(bucket->virt_irq);
479 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
480 if (unlikely(!data)) {
481 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
484 set_irq_chip_data(bucket->virt_irq, data);
490 return bucket->virt_irq;
493 unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino)
495 struct ino_bucket *bucket;
496 struct irq_handler_data *data;
497 unsigned long sysino;
499 BUG_ON(tlb_type != hypervisor);
501 sysino = sun4v_devino_to_sysino(devhandle, devino);
502 bucket = &ivector_table[sysino];
503 if (!bucket->virt_irq) {
504 bucket->virt_irq = virt_irq_alloc(__irq(bucket));
505 set_irq_chip(bucket->virt_irq, &sun4v_irq);
508 data = get_irq_chip_data(bucket->virt_irq);
512 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
513 if (unlikely(!data)) {
514 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
517 set_irq_chip_data(bucket->virt_irq, data);
519 /* Catch accidental accesses to these things. IMAP/ICLR handling
520 * is done by hypervisor calls on sun4v platforms, not by direct
527 return bucket->virt_irq;
530 #ifdef CONFIG_PCI_MSI
531 unsigned int sun4v_build_msi(u32 devhandle, unsigned int *virt_irq_p,
532 unsigned int msi_start, unsigned int msi_end)
534 struct ino_bucket *bucket;
535 struct irq_handler_data *data;
536 unsigned long sysino;
539 BUG_ON(tlb_type != hypervisor);
541 /* Find a free devino in the given range. */
542 for (devino = msi_start; devino < msi_end; devino++) {
543 sysino = sun4v_devino_to_sysino(devhandle, devino);
544 bucket = &ivector_table[sysino];
545 if (!bucket->virt_irq)
548 if (devino >= msi_end)
551 sysino = sun4v_devino_to_sysino(devhandle, devino);
552 bucket = &ivector_table[sysino];
553 bucket->virt_irq = virt_irq_alloc(__irq(bucket));
554 *virt_irq_p = bucket->virt_irq;
555 set_irq_chip(bucket->virt_irq, &sun4v_msi);
557 data = get_irq_chip_data(bucket->virt_irq);
561 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
562 if (unlikely(!data)) {
563 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
566 set_irq_chip_data(bucket->virt_irq, data);
574 void sun4v_destroy_msi(unsigned int virt_irq)
576 virt_irq_free(virt_irq);
580 void ack_bad_irq(unsigned int virt_irq)
582 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
583 unsigned int ino = 0xdeadbeef;
586 ino = bucket - &ivector_table[0];
588 printk(KERN_CRIT "Unexpected IRQ from ino[%x] virt_irq[%u]\n",
593 extern irqreturn_t timer_interrupt(int, void *);
595 void timer_irq(int irq, struct pt_regs *regs)
597 unsigned long clr_mask = 1 << irq;
598 unsigned long tick_mask = tick_ops->softint_mask;
599 struct pt_regs *old_regs;
601 if (get_softint() & tick_mask) {
603 clr_mask = tick_mask;
605 clear_softint(clr_mask);
607 old_regs = set_irq_regs(regs);
610 kstat_this_cpu.irqs[0]++;
611 timer_interrupt(irq, NULL);
614 set_irq_regs(old_regs);
618 void handler_irq(int irq, struct pt_regs *regs)
620 struct ino_bucket *bucket;
621 struct pt_regs *old_regs;
623 clear_softint(1 << irq);
625 old_regs = set_irq_regs(regs);
629 bucket = __bucket(xchg32(irq_work(smp_processor_id()), 0));
631 struct ino_bucket *next = __bucket(bucket->irq_chain);
633 bucket->irq_chain = 0;
634 __do_IRQ(bucket->virt_irq);
640 set_irq_regs(old_regs);
650 static struct sun5_timer *prom_timers;
651 static u64 prom_limit0, prom_limit1;
653 static void map_prom_timers(void)
655 struct device_node *dp;
658 /* PROM timer node hangs out in the top level of device siblings... */
659 dp = of_find_node_by_path("/");
662 if (!strcmp(dp->name, "counter-timer"))
667 /* Assume if node is not present, PROM uses different tick mechanism
668 * which we should not care about.
671 prom_timers = (struct sun5_timer *) 0;
675 /* If PROM is really using this, it must be mapped by him. */
676 addr = of_get_property(dp, "address", NULL);
678 prom_printf("PROM does not have timer mapped, trying to continue.\n");
679 prom_timers = (struct sun5_timer *) 0;
682 prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
685 static void kill_prom_timer(void)
690 /* Save them away for later. */
691 prom_limit0 = prom_timers->limit0;
692 prom_limit1 = prom_timers->limit1;
694 /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
695 * We turn both off here just to be paranoid.
697 prom_timers->limit0 = 0;
698 prom_timers->limit1 = 0;
700 /* Wheee, eat the interrupt packet too... */
701 __asm__ __volatile__(
703 " ldxa [%%g0] %0, %%g1\n"
704 " ldxa [%%g2] %1, %%g1\n"
705 " stxa %%g0, [%%g0] %0\n"
708 : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
712 void init_irqwork_curcpu(void)
714 int cpu = hard_smp_processor_id();
716 trap_block[cpu].irq_worklist = 0;
719 static void __cpuinit register_one_mondo(unsigned long paddr, unsigned long type)
721 unsigned long num_entries = 128;
722 unsigned long status;
724 status = sun4v_cpu_qconf(type, paddr, num_entries);
725 if (status != HV_EOK) {
726 prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
727 "err %lu\n", type, paddr, num_entries, status);
732 static void __cpuinit sun4v_register_mondo_queues(int this_cpu)
734 struct trap_per_cpu *tb = &trap_block[this_cpu];
736 register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO);
737 register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO);
738 register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR);
739 register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR);
742 static void __cpuinit alloc_one_mondo(unsigned long *pa_ptr, int use_bootmem)
747 page = alloc_bootmem_low_pages(PAGE_SIZE);
749 page = (void *) get_zeroed_page(GFP_ATOMIC);
752 prom_printf("SUN4V: Error, cannot allocate mondo queue.\n");
756 *pa_ptr = __pa(page);
759 static void __cpuinit alloc_one_kbuf(unsigned long *pa_ptr, int use_bootmem)
764 page = alloc_bootmem_low_pages(PAGE_SIZE);
766 page = (void *) get_zeroed_page(GFP_ATOMIC);
769 prom_printf("SUN4V: Error, cannot allocate kbuf page.\n");
773 *pa_ptr = __pa(page);
776 static void __cpuinit init_cpu_send_mondo_info(struct trap_per_cpu *tb, int use_bootmem)
781 BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
784 page = alloc_bootmem_low_pages(PAGE_SIZE);
786 page = (void *) get_zeroed_page(GFP_ATOMIC);
789 prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
793 tb->cpu_mondo_block_pa = __pa(page);
794 tb->cpu_list_pa = __pa(page + 64);
798 /* Allocate and register the mondo and error queues for this cpu. */
799 void __cpuinit sun4v_init_mondo_queues(int use_bootmem, int cpu, int alloc, int load)
801 struct trap_per_cpu *tb = &trap_block[cpu];
804 alloc_one_mondo(&tb->cpu_mondo_pa, use_bootmem);
805 alloc_one_mondo(&tb->dev_mondo_pa, use_bootmem);
806 alloc_one_mondo(&tb->resum_mondo_pa, use_bootmem);
807 alloc_one_kbuf(&tb->resum_kernel_buf_pa, use_bootmem);
808 alloc_one_mondo(&tb->nonresum_mondo_pa, use_bootmem);
809 alloc_one_kbuf(&tb->nonresum_kernel_buf_pa, use_bootmem);
811 init_cpu_send_mondo_info(tb, use_bootmem);
815 if (cpu != hard_smp_processor_id()) {
816 prom_printf("SUN4V: init mondo on cpu %d not %d\n",
817 cpu, hard_smp_processor_id());
820 sun4v_register_mondo_queues(cpu);
824 static struct irqaction timer_irq_action = {
828 /* Only invoked on boot processor. */
829 void __init init_IRQ(void)
833 memset(&ivector_table[0], 0, sizeof(ivector_table));
835 if (tlb_type == hypervisor)
836 sun4v_init_mondo_queues(1, hard_smp_processor_id(), 1, 1);
838 /* We need to clear any IRQ's pending in the soft interrupt
839 * registers, a spurious one could be left around from the
840 * PROM timer which we just disabled.
842 clear_softint(get_softint());
844 /* Now that ivector table is initialized, it is safe
845 * to receive IRQ vector traps. We will normally take
846 * one or two right now, in case some device PROM used
847 * to boot us wants to speak to us. We just ignore them.
849 __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
850 "or %%g1, %0, %%g1\n\t"
851 "wrpr %%g1, 0x0, %%pstate"
856 irq_desc[0].action = &timer_irq_action;