2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
15 #include <linux/module.h>
16 #include <linux/seq_file.h>
17 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/kernel_stat.h>
20 #include <linux/uaccess.h>
21 #include <hv/drv_pcie_rc_intf.h>
24 * The set of interrupts we enable for raw_local_irq_enable().
25 * This is initialized to have just a single interrupt that the kernel
26 * doesn't actually use as a sentinel. During kernel init,
27 * interrupts are added as the kernel gets prepared to support them.
28 * NOTE: we could probably initialize them all statically up front.
30 DEFINE_PER_CPU(unsigned long long, interrupts_enabled_mask) =
31 INITIAL_INTERRUPTS_ENABLED;
32 EXPORT_PER_CPU_SYMBOL(interrupts_enabled_mask);
34 /* Define per-tile device interrupt state */
35 DEFINE_PER_CPU(HV_IntrState, dev_intr_state);
37 DEFINE_PER_CPU(irq_cpustat_t, irq_stat) ____cacheline_internodealigned_in_smp;
38 EXPORT_PER_CPU_SYMBOL(irq_stat);
43 * Interrupt dispatcher, invoked upon a hypervisor device interrupt downcall
45 void tile_dev_intr(struct pt_regs *regs, int intnum)
50 * Get the device interrupt pending mask from where the hypervisor
51 * has tucked it away for us.
53 unsigned long pending_dev_intr_mask = __insn_mfspr(SPR_SYSTEM_SAVE_1_3);
56 /* Track time spent here in an interrupt context. */
57 struct pt_regs *old_regs = set_irq_regs(regs);
60 #ifdef CONFIG_DEBUG_STACKOVERFLOW
61 /* Debugging check for stack overflow: less than 1/8th stack free? */
63 long sp = stack_pointer - (long) current_thread_info();
64 if (unlikely(sp < (sizeof(struct thread_info) + STACK_WARN))) {
65 printk(KERN_EMERG "tile_dev_intr: "
66 "stack overflow: %ld\n",
67 sp - sizeof(struct thread_info));
73 for (irq = 0; pending_dev_intr_mask; ++irq) {
74 if (pending_dev_intr_mask & 0x1) {
75 generic_handle_irq(irq);
77 /* Count device irqs; IPIs are counted elsewhere. */
78 if (irq > HV_MAX_IPI_INTERRUPT)
79 __get_cpu_var(irq_stat).irq_dev_intr_count++;
81 pending_dev_intr_mask >>= 1;
85 * Track time spent against the current process again and
86 * process any softirqs if they are waiting.
89 set_irq_regs(old_regs);
93 /* Mask an interrupt. */
94 static void hv_dev_irq_mask(unsigned int irq)
96 HV_IntrState *p_intr_state = &__get_cpu_var(dev_intr_state);
97 hv_disable_intr(p_intr_state, 1 << irq);
100 /* Unmask an interrupt. */
101 static void hv_dev_irq_unmask(unsigned int irq)
103 /* Re-enable the hypervisor to generate interrupts. */
104 HV_IntrState *p_intr_state = &__get_cpu_var(dev_intr_state);
105 hv_enable_intr(p_intr_state, 1 << irq);
109 * The HV doesn't latch incoming interrupts while an interrupt is
110 * disabled, so we need to reenable interrupts before running the
113 * ISSUE: Enabling the interrupt this early avoids any race conditions
114 * but introduces the possibility of nested interrupt stack overflow.
115 * An imminent change to the HV IRQ model will fix this.
117 static void hv_dev_irq_ack(unsigned int irq)
119 hv_dev_irq_unmask(irq);
123 * Since ack() reenables interrupts, there's nothing to do at eoi().
125 static void hv_dev_irq_eoi(unsigned int irq)
129 static struct irq_chip hv_dev_irq_chip = {
130 .typename = "hv_dev_irq_chip",
131 .ack = hv_dev_irq_ack,
132 .mask = hv_dev_irq_mask,
133 .unmask = hv_dev_irq_unmask,
134 .eoi = hv_dev_irq_eoi,
137 static struct irqaction resched_action = {
138 .handler = handle_reschedule_ipi,
140 .dev_id = handle_reschedule_ipi /* unique token */,
143 void __init init_IRQ(void)
145 /* Bind IPI irqs. Does this belong somewhere else in init? */
146 tile_irq_activate(IRQ_RESCHEDULE);
147 BUG_ON(setup_irq(IRQ_RESCHEDULE, &resched_action));
150 void __cpuinit init_per_tile_IRQs(void)
154 /* Set the pointer to the per-tile device interrupt state. */
155 HV_IntrState *sv_ptr = &__get_cpu_var(dev_intr_state);
156 rc = hv_dev_register_intr_state(sv_ptr);
158 panic("hv_dev_register_intr_state: error %d", rc);
162 void tile_irq_activate(unsigned int irq)
165 * Paravirtualized drivers can call up to the HV to find out
166 * which irq they're associated with. The HV interface
167 * doesn't provide a generic call for discovering all valid
168 * IRQs, so drivers must call this method to initialize newly
171 * We could also just initialize all 32 IRQs at startup, but
172 * doing so would lead to a kernel fault if an unexpected
173 * interrupt fires and jumps to a NULL action. By defering
174 * the set_irq_chip_and_handler() call, unexpected IRQs are
175 * handled properly by handle_bad_irq().
177 hv_dev_irq_mask(irq);
178 set_irq_chip_and_handler(irq, &hv_dev_irq_chip, handle_percpu_irq);
181 void ack_bad_irq(unsigned int irq)
183 printk(KERN_ERR "unexpected IRQ trap at vector %02x\n", irq);
187 * Generic, controller-independent functions:
190 int show_interrupts(struct seq_file *p, void *v)
192 int i = *(loff_t *) v, j;
193 struct irqaction *action;
198 for (j = 0; j < NR_CPUS; j++)
200 seq_printf(p, "CPU%-8d", j);
205 raw_spin_lock_irqsave(&irq_desc[i].lock, flags);
206 action = irq_desc[i].action;
209 seq_printf(p, "%3d: ", i);
211 seq_printf(p, "%10u ", kstat_irqs(i));
213 for_each_online_cpu(j)
214 seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
216 seq_printf(p, " %14s", irq_desc[i].chip->typename);
217 seq_printf(p, " %s", action->name);
219 for (action = action->next; action; action = action->next)
220 seq_printf(p, ", %s", action->name);
224 raw_spin_unlock_irqrestore(&irq_desc[i].lock, flags);