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>
22 #include <arch/spr_def.h>
23 #include <asm/traps.h>
25 /* Bit-flag stored in irq_desc->chip_data to indicate HW-cleared irqs. */
26 #define IS_HW_CLEARED 1
29 * The set of interrupts we enable for arch_local_irq_enable().
30 * This is initialized to have just a single interrupt that the kernel
31 * doesn't actually use as a sentinel. During kernel init,
32 * interrupts are added as the kernel gets prepared to support them.
33 * NOTE: we could probably initialize them all statically up front.
35 DEFINE_PER_CPU(unsigned long long, interrupts_enabled_mask
) =
36 INITIAL_INTERRUPTS_ENABLED
;
37 EXPORT_PER_CPU_SYMBOL(interrupts_enabled_mask
);
39 /* Define per-tile device interrupt statistics state. */
40 DEFINE_PER_CPU(irq_cpustat_t
, irq_stat
) ____cacheline_internodealigned_in_smp
;
41 EXPORT_PER_CPU_SYMBOL(irq_stat
);
44 * Define per-tile irq disable mask; the hardware/HV only has a single
45 * mask that we use to implement both masking and disabling.
47 static DEFINE_PER_CPU(unsigned long, irq_disable_mask
)
48 ____cacheline_internodealigned_in_smp
;
51 * Per-tile IRQ nesting depth. Used to make sure we enable newly
52 * enabled IRQs before exiting the outermost interrupt.
54 static DEFINE_PER_CPU(int, irq_depth
);
56 /* State for allocating IRQs on Gx. */
58 static unsigned long available_irqs
= ((1UL << NR_IRQS
) - 1) &
59 (~(1UL << IRQ_RESCHEDULE
));
60 static DEFINE_SPINLOCK(available_irqs_lock
);
64 /* Use SPRs to manipulate device interrupts. */
65 #define mask_irqs(irq_mask) __insn_mtspr(SPR_IPI_MASK_SET_K, irq_mask)
66 #define unmask_irqs(irq_mask) __insn_mtspr(SPR_IPI_MASK_RESET_K, irq_mask)
67 #define clear_irqs(irq_mask) __insn_mtspr(SPR_IPI_EVENT_RESET_K, irq_mask)
69 /* Use HV to manipulate device interrupts. */
70 #define mask_irqs(irq_mask) hv_disable_intr(irq_mask)
71 #define unmask_irqs(irq_mask) hv_enable_intr(irq_mask)
72 #define clear_irqs(irq_mask) hv_clear_intr(irq_mask)
76 * The interrupt handling path, implemented in terms of HV interrupt
77 * emulation on TILEPro, and IPI hardware on TILE-Gx.
78 * Entered with interrupts disabled.
80 void tile_dev_intr(struct pt_regs
*regs
, int intnum
)
82 int depth
= __get_cpu_var(irq_depth
)++;
83 unsigned long original_irqs
;
84 unsigned long remaining_irqs
;
85 struct pt_regs
*old_regs
;
89 * Pending interrupts are listed in an SPR. We might be
90 * nested, so be sure to only handle irqs that weren't already
91 * masked by a previous interrupt. Then, mask out the ones
92 * we're going to handle.
94 unsigned long masked
= __insn_mfspr(SPR_IPI_MASK_K
);
95 original_irqs
= __insn_mfspr(SPR_IPI_EVENT_K
) & ~masked
;
96 __insn_mtspr(SPR_IPI_MASK_SET_K
, original_irqs
);
99 * Hypervisor performs the equivalent of the Gx code above and
100 * then puts the pending interrupt mask into a system save reg
103 original_irqs
= __insn_mfspr(SPR_SYSTEM_SAVE_K_3
);
105 remaining_irqs
= original_irqs
;
107 /* Track time spent here in an interrupt context. */
108 old_regs
= set_irq_regs(regs
);
111 #ifdef CONFIG_DEBUG_STACKOVERFLOW
112 /* Debugging check for stack overflow: less than 1/8th stack free? */
114 long sp
= stack_pointer
- (long) current_thread_info();
115 if (unlikely(sp
< (sizeof(struct thread_info
) + STACK_WARN
))) {
116 pr_emerg("tile_dev_intr: "
117 "stack overflow: %ld\n",
118 sp
- sizeof(struct thread_info
));
123 while (remaining_irqs
) {
124 unsigned long irq
= __ffs(remaining_irqs
);
125 remaining_irqs
&= ~(1UL << irq
);
127 /* Count device irqs; Linux IPIs are counted elsewhere. */
128 if (irq
!= IRQ_RESCHEDULE
)
129 __get_cpu_var(irq_stat
).irq_dev_intr_count
++;
131 generic_handle_irq(irq
);
135 * If we weren't nested, turn on all enabled interrupts,
136 * including any that were reenabled during interrupt
140 unmask_irqs(~__get_cpu_var(irq_disable_mask
));
142 __get_cpu_var(irq_depth
)--;
145 * Track time spent against the current process again and
146 * process any softirqs if they are waiting.
149 set_irq_regs(old_regs
);
154 * Remove an irq from the disabled mask. If we're in an interrupt
155 * context, defer enabling the HW interrupt until we leave.
157 static void tile_irq_chip_enable(struct irq_data
*d
)
159 get_cpu_var(irq_disable_mask
) &= ~(1UL << d
->irq
);
160 if (__get_cpu_var(irq_depth
) == 0)
161 unmask_irqs(1UL << d
->irq
);
162 put_cpu_var(irq_disable_mask
);
166 * Add an irq to the disabled mask. We disable the HW interrupt
167 * immediately so that there's no possibility of it firing. If we're
168 * in an interrupt context, the return path is careful to avoid
169 * unmasking a newly disabled interrupt.
171 static void tile_irq_chip_disable(struct irq_data
*d
)
173 get_cpu_var(irq_disable_mask
) |= (1UL << d
->irq
);
174 mask_irqs(1UL << d
->irq
);
175 put_cpu_var(irq_disable_mask
);
178 /* Mask an interrupt. */
179 static void tile_irq_chip_mask(struct irq_data
*d
)
181 mask_irqs(1UL << d
->irq
);
184 /* Unmask an interrupt. */
185 static void tile_irq_chip_unmask(struct irq_data
*d
)
187 unmask_irqs(1UL << d
->irq
);
191 * Clear an interrupt before processing it so that any new assertions
192 * will trigger another irq.
194 static void tile_irq_chip_ack(struct irq_data
*d
)
196 if ((unsigned long)irq_data_get_irq_chip_data(d
) != IS_HW_CLEARED
)
197 clear_irqs(1UL << d
->irq
);
201 * For per-cpu interrupts, we need to avoid unmasking any interrupts
202 * that we disabled via disable_percpu_irq().
204 static void tile_irq_chip_eoi(struct irq_data
*d
)
206 if (!(__get_cpu_var(irq_disable_mask
) & (1UL << d
->irq
)))
207 unmask_irqs(1UL << d
->irq
);
210 static struct irq_chip tile_irq_chip
= {
211 .name
= "tile_irq_chip",
212 .irq_enable
= tile_irq_chip_enable
,
213 .irq_disable
= tile_irq_chip_disable
,
214 .irq_ack
= tile_irq_chip_ack
,
215 .irq_eoi
= tile_irq_chip_eoi
,
216 .irq_mask
= tile_irq_chip_mask
,
217 .irq_unmask
= tile_irq_chip_unmask
,
220 void __init
init_IRQ(void)
225 void setup_irq_regs(void)
227 /* Enable interrupt delivery. */
230 arch_local_irq_unmask(INT_IPI_K
);
234 void tile_irq_activate(unsigned int irq
, int tile_irq_type
)
237 * We use handle_level_irq() by default because the pending
238 * interrupt vector (whether modeled by the HV on
239 * TILEPro or implemented in hardware on TILE-Gx) has
240 * level-style semantics for each bit. An interrupt fires
241 * whenever a bit is high, not just at edges.
243 irq_flow_handler_t handle
= handle_level_irq
;
244 if (tile_irq_type
== TILE_IRQ_PERCPU
)
245 handle
= handle_percpu_irq
;
246 irq_set_chip_and_handler(irq
, &tile_irq_chip
, handle
);
249 * Flag interrupts that are hardware-cleared so that ack()
252 if (tile_irq_type
== TILE_IRQ_HW_CLEAR
)
253 irq_set_chip_data(irq
, (void *)IS_HW_CLEARED
);
255 EXPORT_SYMBOL(tile_irq_activate
);
258 void ack_bad_irq(unsigned int irq
)
260 pr_err("unexpected IRQ trap at vector %02x\n", irq
);
264 * Generic, controller-independent functions:
273 spin_lock_irqsave(&available_irqs_lock
, flags
);
274 if (available_irqs
== 0)
277 result
= __ffs(available_irqs
);
278 available_irqs
&= ~(1UL << result
);
279 dynamic_irq_init(result
);
281 spin_unlock_irqrestore(&available_irqs_lock
, flags
);
285 EXPORT_SYMBOL(create_irq
);
287 void destroy_irq(unsigned int irq
)
291 spin_lock_irqsave(&available_irqs_lock
, flags
);
292 available_irqs
|= (1UL << irq
);
293 dynamic_irq_cleanup(irq
);
294 spin_unlock_irqrestore(&available_irqs_lock
, flags
);
296 EXPORT_SYMBOL(destroy_irq
);