irq.c 7.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright 2010 Tilera Corporation. All Rights Reserved.
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation, version 2.
 *
 *   This program is distributed in the hope that it will be useful, but
 *   WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
 *   NON INFRINGEMENT.  See the GNU General Public License for
 *   more details.
 */

#include <linux/module.h>
#include <linux/seq_file.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/kernel_stat.h>
#include <linux/uaccess.h>
#include <hv/drv_pcie_rc_intf.h>
22 23
#include <arch/spr_def.h>
#include <asm/traps.h>
24
#include <linux/perf_event.h>
25 26 27

/* Bit-flag stored in irq_desc->chip_data to indicate HW-cleared irqs. */
#define IS_HW_CLEARED 1
28 29

/*
30
 * The set of interrupts we enable for arch_local_irq_enable().
31 32 33 34 35 36 37 38 39
 * This is initialized to have just a single interrupt that the kernel
 * doesn't actually use as a sentinel.  During kernel init,
 * interrupts are added as the kernel gets prepared to support them.
 * NOTE: we could probably initialize them all statically up front.
 */
DEFINE_PER_CPU(unsigned long long, interrupts_enabled_mask) =
  INITIAL_INTERRUPTS_ENABLED;
EXPORT_PER_CPU_SYMBOL(interrupts_enabled_mask);

40
/* Define per-tile device interrupt statistics state. */
41 42 43
DEFINE_PER_CPU(irq_cpustat_t, irq_stat) ____cacheline_internodealigned_in_smp;
EXPORT_PER_CPU_SYMBOL(irq_stat);

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/*
 * Define per-tile irq disable mask; the hardware/HV only has a single
 * mask that we use to implement both masking and disabling.
 */
static DEFINE_PER_CPU(unsigned long, irq_disable_mask)
	____cacheline_internodealigned_in_smp;

/*
 * Per-tile IRQ nesting depth.  Used to make sure we enable newly
 * enabled IRQs before exiting the outermost interrupt.
 */
static DEFINE_PER_CPU(int, irq_depth);

#if CHIP_HAS_IPI()
/* Use SPRs to manipulate device interrupts. */
59 60 61
#define mask_irqs(irq_mask) __insn_mtspr(SPR_IPI_MASK_SET_K, irq_mask)
#define unmask_irqs(irq_mask) __insn_mtspr(SPR_IPI_MASK_RESET_K, irq_mask)
#define clear_irqs(irq_mask) __insn_mtspr(SPR_IPI_EVENT_RESET_K, irq_mask)
62 63 64 65 66 67
#else
/* Use HV to manipulate device interrupts. */
#define mask_irqs(irq_mask) hv_disable_intr(irq_mask)
#define unmask_irqs(irq_mask) hv_enable_intr(irq_mask)
#define clear_irqs(irq_mask) hv_clear_intr(irq_mask)
#endif
68 69

/*
70
 * The interrupt handling path, implemented in terms of HV interrupt
C
Chris Metcalf 已提交
71
 * emulation on TILEPro, and IPI hardware on TILE-Gx.
C
Chris Metcalf 已提交
72
 * Entered with interrupts disabled.
73 74 75
 */
void tile_dev_intr(struct pt_regs *regs, int intnum)
{
76
	int depth = __this_cpu_inc_return(irq_depth);
77 78 79
	unsigned long original_irqs;
	unsigned long remaining_irqs;
	struct pt_regs *old_regs;
80

81
#if CHIP_HAS_IPI()
82
	/*
83 84 85 86
	 * Pending interrupts are listed in an SPR.  We might be
	 * nested, so be sure to only handle irqs that weren't already
	 * masked by a previous interrupt.  Then, mask out the ones
	 * we're going to handle.
87
	 */
88 89 90
	unsigned long masked = __insn_mfspr(SPR_IPI_MASK_K);
	original_irqs = __insn_mfspr(SPR_IPI_EVENT_K) & ~masked;
	__insn_mtspr(SPR_IPI_MASK_SET_K, original_irqs);
91 92 93 94 95 96
#else
	/*
	 * Hypervisor performs the equivalent of the Gx code above and
	 * then puts the pending interrupt mask into a system save reg
	 * for us to find.
	 */
97
	original_irqs = __insn_mfspr(SPR_SYSTEM_SAVE_K_3);
98 99
#endif
	remaining_irqs = original_irqs;
100 101

	/* Track time spent here in an interrupt context. */
102
	old_regs = set_irq_regs(regs);
103 104 105 106 107 108 109
	irq_enter();

#ifdef CONFIG_DEBUG_STACKOVERFLOW
	/* Debugging check for stack overflow: less than 1/8th stack free? */
	{
		long sp = stack_pointer - (long) current_thread_info();
		if (unlikely(sp < (sizeof(struct thread_info) + STACK_WARN))) {
110 111
			pr_emerg("%s: stack overflow: %ld\n",
				 __func__, sp - sizeof(struct thread_info));
112 113 114 115
			dump_stack();
		}
	}
#endif
116 117 118
	while (remaining_irqs) {
		unsigned long irq = __ffs(remaining_irqs);
		remaining_irqs &= ~(1UL << irq);
119

120 121
		/* Count device irqs; Linux IPIs are counted elsewhere. */
		if (irq != IRQ_RESCHEDULE)
122
			__this_cpu_inc(irq_stat.irq_dev_intr_count);
123

124
		generic_handle_irq(irq);
125 126
	}

127 128 129 130 131
	/*
	 * If we weren't nested, turn on all enabled interrupts,
	 * including any that were reenabled during interrupt
	 * handling.
	 */
132 133
	if (depth == 1)
		unmask_irqs(~__this_cpu_read(irq_disable_mask));
134

135
	__this_cpu_dec(irq_depth);
136

137 138 139 140 141 142 143 144 145
	/*
	 * Track time spent against the current process again and
	 * process any softirqs if they are waiting.
	 */
	irq_exit();
	set_irq_regs(old_regs);
}


146 147 148 149
/*
 * Remove an irq from the disabled mask.  If we're in an interrupt
 * context, defer enabling the HW interrupt until we leave.
 */
150
static void tile_irq_chip_enable(struct irq_data *d)
151
{
152
	get_cpu_var(irq_disable_mask) &= ~(1UL << d->irq);
153
	if (__this_cpu_read(irq_depth) == 0)
154
		unmask_irqs(1UL << d->irq);
155 156 157 158 159 160 161 162 163
	put_cpu_var(irq_disable_mask);
}

/*
 * Add an irq to the disabled mask.  We disable the HW interrupt
 * immediately so that there's no possibility of it firing.  If we're
 * in an interrupt context, the return path is careful to avoid
 * unmasking a newly disabled interrupt.
 */
164
static void tile_irq_chip_disable(struct irq_data *d)
165
{
166 167
	get_cpu_var(irq_disable_mask) |= (1UL << d->irq);
	mask_irqs(1UL << d->irq);
168 169 170
	put_cpu_var(irq_disable_mask);
}

171
/* Mask an interrupt. */
172
static void tile_irq_chip_mask(struct irq_data *d)
173
{
174
	mask_irqs(1UL << d->irq);
175 176 177
}

/* Unmask an interrupt. */
178
static void tile_irq_chip_unmask(struct irq_data *d)
179
{
180
	unmask_irqs(1UL << d->irq);
181 182 183
}

/*
184 185
 * Clear an interrupt before processing it so that any new assertions
 * will trigger another irq.
186
 */
187
static void tile_irq_chip_ack(struct irq_data *d)
188
{
189 190
	if ((unsigned long)irq_data_get_irq_chip_data(d) != IS_HW_CLEARED)
		clear_irqs(1UL << d->irq);
191 192 193
}

/*
194 195
 * For per-cpu interrupts, we need to avoid unmasking any interrupts
 * that we disabled via disable_percpu_irq().
196
 */
197
static void tile_irq_chip_eoi(struct irq_data *d)
198
{
199
	if (!(__this_cpu_read(irq_disable_mask) & (1UL << d->irq)))
200
		unmask_irqs(1UL << d->irq);
201 202
}

203
static struct irq_chip tile_irq_chip = {
204
	.name = "tile_irq_chip",
205 206
	.irq_enable = tile_irq_chip_enable,
	.irq_disable = tile_irq_chip_disable,
207 208 209 210
	.irq_ack = tile_irq_chip_ack,
	.irq_eoi = tile_irq_chip_eoi,
	.irq_mask = tile_irq_chip_mask,
	.irq_unmask = tile_irq_chip_unmask,
211 212 213 214
};

void __init init_IRQ(void)
{
215
	ipi_init();
216 217
}

218
void setup_irq_regs(void)
219
{
220 221 222
	/* Enable interrupt delivery. */
	unmask_irqs(~0UL);
#if CHIP_HAS_IPI()
223
	arch_local_irq_unmask(INT_IPI_K);
224
#endif
225 226
}

227
void tile_irq_activate(unsigned int irq, int tile_irq_type)
228 229
{
	/*
230
	 * We use handle_level_irq() by default because the pending
C
Chris Metcalf 已提交
231
	 * interrupt vector (whether modeled by the HV on
232 233 234 235 236 237 238
	 * TILEPro or implemented in hardware on TILE-Gx) has
	 * level-style semantics for each bit.  An interrupt fires
	 * whenever a bit is high, not just at edges.
	 */
	irq_flow_handler_t handle = handle_level_irq;
	if (tile_irq_type == TILE_IRQ_PERCPU)
		handle = handle_percpu_irq;
239
	irq_set_chip_and_handler(irq, &tile_irq_chip, handle);
240 241 242 243

	/*
	 * Flag interrupts that are hardware-cleared so that ack()
	 * won't clear them.
244
	 */
245
	if (tile_irq_type == TILE_IRQ_HW_CLEAR)
246
		irq_set_chip_data(irq, (void *)IS_HW_CLEARED);
247
}
248 249
EXPORT_SYMBOL(tile_irq_activate);

250 251 252

void ack_bad_irq(unsigned int irq)
{
253
	pr_err("unexpected IRQ trap at vector %02x\n", irq);
254 255
}

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
/*
 * /proc/interrupts printing:
 */
int arch_show_interrupts(struct seq_file *p, int prec)
{
#ifdef CONFIG_PERF_EVENTS
	int i;

	seq_printf(p, "%*s: ", prec, "PMI");

	for_each_online_cpu(i)
		seq_printf(p, "%10llu ", per_cpu(perf_irqs, i));
	seq_puts(p, "  perf_events\n");
#endif
	return 0;
}

273
#if CHIP_HAS_IPI()
T
Thomas Gleixner 已提交
274
int arch_setup_hwirq(unsigned int irq, int node)
275
{
T
Thomas Gleixner 已提交
276
	return irq >= NR_IRQS ? -EINVAL : 0;
277 278
}

T
Thomas Gleixner 已提交
279
void arch_teardown_hwirq(unsigned int irq) { }
280
#endif