summaryrefslogtreecommitdiffstats
path: root/drivers/irqchip/irq-metag.c
blob: c16c186d97d35f4246fa2acbd17942789922d6d7 (plain) (blame)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * Meta internal (HWSTATMETA) interrupt code.
 *
 * Copyright (C) 2011-2012 Imagination Technologies Ltd.
 *
 * This code is based on the code in SoC/common/irq.c and SoC/comet/irq.c
 * The code base could be generalised/merged as a lot of the functionality is
 * similar. Until this is done, we try to keep the code simple here.
 */

#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/irqdomain.h>

#include <asm/irq.h>
#include <asm/hwthread.h>

#define PERF0VECINT		0x04820580
#define PERF1VECINT		0x04820588
#define PERF0TRIG_OFFSET	16
#define PERF1TRIG_OFFSET	17

/**
 * struct metag_internal_irq_priv - private meta internal interrupt data
 * @domain:		IRQ domain for all internal Meta IRQs (HWSTATMETA)
 * @unmasked:		Record of unmasked IRQs
 */
struct metag_internal_irq_priv {
	struct irq_domain	*domain;

	unsigned long		unmasked;
};

/* Private data for the one and only internal interrupt controller */
static struct metag_internal_irq_priv metag_internal_irq_priv;

static unsigned int metag_internal_irq_startup(struct irq_data *data);
static void metag_internal_irq_shutdown(struct irq_data *data);
static void metag_internal_irq_ack(struct irq_data *data);
static void metag_internal_irq_mask(struct irq_data *data);
static void metag_internal_irq_unmask(struct irq_data *data);
#ifdef CONFIG_SMP
static int metag_internal_irq_set_affinity(struct irq_data *data,
			const struct cpumask *cpumask, bool force);
#endif

static struct irq_chip internal_irq_edge_chip = {
	.name = "HWSTATMETA-IRQ",
	.irq_startup = metag_internal_irq_startup,
	.irq_shutdown = metag_internal_irq_shutdown,
	.irq_ack = metag_internal_irq_ack,
	.irq_mask = metag_internal_irq_mask,
	.irq_unmask = metag_internal_irq_unmask,
#ifdef CONFIG_SMP
	.irq_set_affinity = metag_internal_irq_set_affinity,
#endif
};

/*
 *	metag_hwvec_addr - get the address of *VECINT regs of irq
 *
 *	This function is a table of supported triggers on HWSTATMETA
 *	Could do with a structure, but better keep it simple. Changes
 *	in this code should be rare.
 */
static inline void __iomem *metag_hwvec_addr(irq_hw_number_t hw)
{
	void __iomem *addr;

	switch (hw) {
	case PERF0TRIG_OFFSET:
		addr = (void __iomem *)PERF0VECINT;
		break;
	case PERF1TRIG_OFFSET:
		addr = (void __iomem *)PERF1VECINT;
		break;
	default:
		addr = NULL;
		break;
	}
	return addr;
}

/*
 *	metag_internal_startup - setup an internal irq
 *	@irq:	the irq to startup
 *
 *	Multiplex interrupts for @irq onto TR1. Clear any pending
 *	interrupts.
 */
static unsigned int metag_internal_irq_startup(struct irq_data *data)
{
	/* Clear (toggle) the bit in HWSTATMETA for our interrupt. */
	metag_internal_irq_ack(data);

	/* Enable the interrupt by unmasking it */
	metag_internal_irq_unmask(data);

	return 0;
}

/*
 *	metag_internal_irq_shutdown - turn off the irq
 *	@irq:	the irq number to turn off
 *
 *	Mask @irq and clear any pending interrupts.
 *	Stop muxing @irq onto TR1.
 */
static void metag_internal_irq_shutdown(struct irq_data *data)
{
	/* Disable the IRQ at the core by masking it. */
	metag_internal_irq_mask(data);

	/* Clear (toggle) the bit in HWSTATMETA for our interrupt. */
	metag_internal_irq_ack(data);
}

/*
 *	metag_internal_irq_ack - acknowledge irq
 *	@irq:	the irq to ack
 */
static void metag_internal_irq_ack(struct irq_data *data)
{
	irq_hw_number_t hw = data->hwirq;
	unsigned int bit = 1 << hw;

	if (metag_in32(HWSTATMETA) & bit)
		metag_out32(bit, HWSTATMETA);
}

/**
 * metag_internal_irq_mask() - mask an internal irq by unvectoring
 * @data:	data for the internal irq to mask
 *
 * HWSTATMETA has no mask register. Instead the IRQ is unvectored from the core
 * and retriggered if necessary later.
 */
static void metag_internal_irq_mask(struct irq_data *data)
{
	struct metag_internal_irq_priv *priv = &metag_internal_irq_priv;
	irq_hw_number_t hw = data->hwirq;
	void __iomem *vec_addr = metag_hwvec_addr(hw);

	clear_bit(hw, &priv->unmasked);

	/* there is no interrupt mask, so unvector the interrupt */
	metag_out32(0, vec_addr);
}

/**
 * meta_intc_unmask_edge_irq_nomask() - unmask an edge irq by revectoring
 * @data:	data for the internal irq to unmask
 *
 * HWSTATMETA has no mask register. Instead the IRQ is revectored back to the
 * core and retriggered if necessary.
 */
static void metag_internal_irq_unmask(struct irq_data *data)
{
	struct metag_internal_irq_priv *priv = &metag_internal_irq_priv;
	irq_hw_number_t hw = data->hwirq;
	unsigned int bit = 1 << hw;
	void __iomem *vec_addr = metag_hwvec_addr(hw);
	unsigned int thread = hard_processor_id();

	set_bit(hw, &priv->unmasked);

	/* there is no interrupt mask, so revector the interrupt */
	metag_out32(TBI_TRIG_VEC(TBID_SIGNUM_TR1(thread)), vec_addr);

	/*
	 * Re-trigger interrupt
	 *
	 * Writing a 1 toggles, and a 0->1 transition triggers. We only
	 * retrigger if the status bit is already set, which means we
	 * need to clear it first. Retriggering is fundamentally racy
	 * because if the interrupt fires again after we clear it we
	 * could end up clearing it again and the interrupt handler
	 * thinking it hasn't fired. Therefore we need to keep trying to
	 * retrigger until the bit is set.
	 */
	if (metag_in32(HWSTATMETA) & bit) {
		metag_out32(bit, HWSTATMETA);
		while (!(metag_in32(HWSTATMETA) & bit))
			metag_out32(bit, HWSTATMETA);
	}
}

#ifdef CONFIG_SMP
/*
 *	metag_internal_irq_set_affinity - set the affinity for an interrupt
 */
static int metag_internal_irq_set_affinity(struct irq_data *data,
			const struct cpumask *cpumask, bool force)
{
	unsigned int cpu, thread;
	irq_hw_number_t hw = data->hwirq;
	/*
	 * Wire up this interrupt from *VECINT to the Meta core.
	 *
	 * Note that we can't wire up *VECINT to interrupt more than
	 * one cpu (the interrupt code doesn't support it), so we just
	 * pick the first cpu we find in 'cpumask'.
	 */
	cpu = cpumask_any_and(cpumask, cpu_online_mask);
	thread = cpu_2_hwthread_id[cpu];

	metag_out32(TBI_TRIG_VEC(TBID_SIGNUM_TR1(thread)),
		    metag_hwvec_addr(hw));

	return 0;
}
#endif

/*
 *	metag_internal_irq_demux - irq de-multiplexer
 *	@irq:	the interrupt number
 *	@desc:	the interrupt description structure for this irq
 *
 *	The cpu receives an interrupt on TR1 when an interrupt has
 *	occurred. It is this function's job to demux this irq and
 *	figure out exactly which trigger needs servicing.
 */
static void metag_internal_irq_demux(unsigned int irq, struct irq_desc *desc)
{
	struct metag_internal_irq_priv *priv = irq_desc_get_handler_data(desc);
	irq_hw_number_t hw;
	unsigned int irq_no;
	u32 status;

recalculate:
	status = metag_in32(HWSTATMETA) & priv->unmasked;

	for (hw = 0; status != 0; status >>= 1, ++hw) {
		if (status & 0x1) {
			/*
			 * Map the hardware IRQ number to a virtual Linux IRQ
			 * number.
			 */
			irq_no = irq_linear_revmap(priv->domain, hw);

			/*
			 * Only fire off interrupts that are
			 * registered to be handled by the kernel.
			 * Other interrupts are probably being
			 * handled by other Meta hardware threads.
			 */
			generic_handle_irq(irq_no);

			/*
			 * The handler may have re-enabled interrupts
			 * which could have caused a nested invocation
			 * of this code and make the copy of the
			 * status register we are using invalid.
			 */
			goto recalculate;
		}
	}
}

/**
 * internal_irq_map() - Map an internal meta IRQ to a virtual IRQ number.
 * @hw:		Number of the internal IRQ. Must be in range.
 *
 * Returns:	The virtual IRQ number of the Meta internal IRQ specified by
 *		@hw.
 */
int internal_irq_map(unsigned int hw)
{
	struct metag_internal_irq_priv *priv = &metag_internal_irq_priv;
	if (!priv->domain)
		return -ENODEV;
	return irq_create_mapping(priv->domain, hw);
}

/**
 *	metag_internal_irq_init_cpu - regsister with the Meta cpu
 *	@cpu:	the CPU to register on
 *
 *	Configure @cpu's TR1 irq so that we can demux irqs.
 */
static void metag_internal_irq_init_cpu(struct metag_internal_irq_priv *priv,
					int cpu)
{
	unsigned int thread = cpu_2_hwthread_id[cpu];
	unsigned int signum = TBID_SIGNUM_TR1(thread);
	int irq = tbisig_map(signum);

	/* Register the multiplexed IRQ handler */
	irq_set_handler_data(irq, priv);
	irq_set_chained_handler(irq, metag_internal_irq_demux);
	irq_set_irq_type(irq, IRQ_TYPE_LEVEL_LOW);
}

/**
 * metag_internal_intc_map() - map an internal irq
 * @d:		irq domain of internal trigger block
 * @irq:	virtual irq number
 * @hw:		hardware irq number within internal trigger block
 *
 * This sets up a virtual irq for a specified hardware interrupt. The irq chip
 * and handler is configured.
 */
static int metag_internal_intc_map(struct irq_domain *d, unsigned int irq,
				   irq_hw_number_t hw)
{
	/* only register interrupt if it is mapped */
	if (!metag_hwvec_addr(hw))
		return -EINVAL;

	irq_set_chip_and_handler(irq, &internal_irq_edge_chip,
				 handle_edge_irq);
	return 0;
}

static const struct irq_domain_ops metag_internal_intc_domain_ops = {
	.map	= metag_internal_intc_map,
};

/**
 *	metag_internal_irq_register - register internal IRQs
 *
 *	Register the irq chip and handler function for all internal IRQs
 */
int __init init_internal_IRQ(void)
{
	struct metag_internal_irq_priv *priv = &metag_internal_irq_priv;
	unsigned int cpu;

	/* Set up an IRQ domain */
	priv->domain = irq_domain_add_linear(NULL, 32,
					     &metag_internal_intc_domain_ops,
					     priv);
	if (unlikely(!priv->domain)) {
		pr_err("meta-internal-intc: cannot add IRQ domain\n");
		return -ENOMEM;
	}

	/* Setup TR1 for all cpus. */
	for_each_possible_cpu(cpu)
		metag_internal_irq_init_cpu(priv, cpu);

	return 0;
};