summaryrefslogtreecommitdiffstats
path: root/arch/tile/kernel/irq.c
blob: 24cc6b2abc2c9cb97fa398aa74946b50f47296f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 * 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>

/*
 * The set of interrupts we enable for raw_local_irq_enable().
 * 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);

/* Define per-tile device interrupt state */
DEFINE_PER_CPU(HV_IntrState, dev_intr_state);

DEFINE_PER_CPU(irq_cpustat_t, irq_stat) ____cacheline_internodealigned_in_smp;
EXPORT_PER_CPU_SYMBOL(irq_stat);



/*
 * Interrupt dispatcher, invoked upon a hypervisor device interrupt downcall
 */
void tile_dev_intr(struct pt_regs *regs, int intnum)
{
	int irq;

	/*
	 * Get the device interrupt pending mask from where the hypervisor
	 * has tucked it away for us.
	 */
	unsigned long pending_dev_intr_mask = __insn_mfspr(SPR_SYSTEM_SAVE_1_3);


	/* Track time spent here in an interrupt context. */
	struct pt_regs *old_regs = set_irq_regs(regs);
	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))) {
			printk(KERN_EMERG "tile_dev_intr: "
			       "stack overflow: %ld\n",
			       sp - sizeof(struct thread_info));
			dump_stack();
		}
	}
#endif

	for (irq = 0; pending_dev_intr_mask; ++irq) {
		if (pending_dev_intr_mask & 0x1) {
			generic_handle_irq(irq);

			/* Count device irqs; IPIs are counted elsewhere. */
			if (irq > HV_MAX_IPI_INTERRUPT)
				__get_cpu_var(irq_stat).irq_dev_intr_count++;
		}
		pending_dev_intr_mask >>= 1;
	}

	/*
	 * Track time spent against the current process again and
	 * process any softirqs if they are waiting.
	 */
	irq_exit();
	set_irq_regs(old_regs);
}


/* Mask an interrupt. */
static void hv_dev_irq_mask(unsigned int irq)
{
	HV_IntrState *p_intr_state = &__get_cpu_var(dev_intr_state);
	hv_disable_intr(p_intr_state, 1 << irq);
}

/* Unmask an interrupt. */
static void hv_dev_irq_unmask(unsigned int irq)
{
	/* Re-enable the hypervisor to generate interrupts. */
	HV_IntrState *p_intr_state = &__get_cpu_var(dev_intr_state);
	hv_enable_intr(p_intr_state, 1 << irq);
}

/*
 * The HV doesn't latch incoming interrupts while an interrupt is
 * disabled, so we need to reenable interrupts before running the
 * handler.
 *
 * ISSUE: Enabling the interrupt this early avoids any race conditions
 * but introduces the possibility of nested interrupt stack overflow.
 * An imminent change to the HV IRQ model will fix this.
 */
static void hv_dev_irq_ack(unsigned int irq)
{
	hv_dev_irq_unmask(irq);
}

/*
 * Since ack() reenables interrupts, there's nothing to do at eoi().
 */
static void hv_dev_irq_eoi(unsigned int irq)
{
}

static struct irq_chip hv_dev_irq_chip = {
	.typename = "hv_dev_irq_chip",
	.ack = hv_dev_irq_ack,
	.mask = hv_dev_irq_mask,
	.unmask = hv_dev_irq_unmask,
	.eoi = hv_dev_irq_eoi,
};

static struct irqaction resched_action = {
	.handler = handle_reschedule_ipi,
	.name = "resched",
	.dev_id = handle_reschedule_ipi /* unique token */,
};

void __init init_IRQ(void)
{
	/* Bind IPI irqs. Does this belong somewhere else in init? */
	tile_irq_activate(IRQ_RESCHEDULE);
	BUG_ON(setup_irq(IRQ_RESCHEDULE, &resched_action));
}

void __cpuinit init_per_tile_IRQs(void)
{
	int rc;

	/* Set the pointer to the per-tile device interrupt state. */
	HV_IntrState *sv_ptr = &__get_cpu_var(dev_intr_state);
	rc = hv_dev_register_intr_state(sv_ptr);
	if (rc != HV_OK)
		panic("hv_dev_register_intr_state: error %d", rc);

}

void tile_irq_activate(unsigned int irq)
{
	/*
	 * Paravirtualized drivers can call up to the HV to find out
	 * which irq they're associated with.  The HV interface
	 * doesn't provide a generic call for discovering all valid
	 * IRQs, so drivers must call this method to initialize newly
	 * discovered IRQs.
	 *
	 * We could also just initialize all 32 IRQs at startup, but
	 * doing so would lead to a kernel fault if an unexpected
	 * interrupt fires and jumps to a NULL action.  By defering
	 * the set_irq_chip_and_handler() call, unexpected IRQs are
	 * handled properly by handle_bad_irq().
	 */
	hv_dev_irq_mask(irq);
	set_irq_chip_and_handler(irq, &hv_dev_irq_chip, handle_percpu_irq);
}

void ack_bad_irq(unsigned int irq)
{
	printk(KERN_ERR "unexpected IRQ trap at vector %02x\n", irq);
}

/*
 * Generic, controller-independent functions:
 */

int show_interrupts(struct seq_file *p, void *v)
{
	int i = *(loff_t *) v, j;
	struct irqaction *action;
	unsigned long flags;

	if (i == 0) {
		seq_printf(p, "           ");
		for (j = 0; j < NR_CPUS; j++)
			if (cpu_online(j))
				seq_printf(p, "CPU%-8d", j);
		seq_putc(p, '\n');
	}

	if (i < NR_IRQS) {
		raw_spin_lock_irqsave(&irq_desc[i].lock, flags);
		action = irq_desc[i].action;
		if (!action)
			goto skip;
		seq_printf(p, "%3d: ", i);
#ifndef CONFIG_SMP
		seq_printf(p, "%10u ", kstat_irqs(i));
#else
		for_each_online_cpu(j)
			seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
#endif
		seq_printf(p, " %14s", irq_desc[i].chip->typename);
		seq_printf(p, "  %s", action->name);

		for (action = action->next; action; action = action->next)
			seq_printf(p, ", %s", action->name);

		seq_putc(p, '\n');
skip:
		raw_spin_unlock_irqrestore(&irq_desc[i].lock, flags);
	}
	return 0;
}