1/*
2 * Linux/Meta general interrupt handling code
3 *
4 */
5
6#include <linux/kernel.h>
7#include <linux/interrupt.h>
8#include <linux/init.h>
9#include <linux/irqchip/metag-ext.h>
10#include <linux/irqchip/metag.h>
11#include <linux/irqdomain.h>
12#include <linux/ratelimit.h>
13
14#include <asm/core_reg.h>
15#include <asm/mach/arch.h>
16#include <asm/uaccess.h>
17
18#ifdef CONFIG_4KSTACKS
19union irq_ctx {
20	struct thread_info      tinfo;
21	u32                     stack[THREAD_SIZE/sizeof(u32)];
22};
23
24static union irq_ctx *hardirq_ctx[NR_CPUS] __read_mostly;
25static union irq_ctx *softirq_ctx[NR_CPUS] __read_mostly;
26#endif
27
28static struct irq_domain *root_domain;
29
30static unsigned int startup_meta_irq(struct irq_data *data)
31{
32	tbi_startup_interrupt(data->hwirq);
33	return 0;
34}
35
36static void shutdown_meta_irq(struct irq_data *data)
37{
38	tbi_shutdown_interrupt(data->hwirq);
39}
40
41void do_IRQ(int irq, struct pt_regs *regs)
42{
43	struct pt_regs *old_regs = set_irq_regs(regs);
44#ifdef CONFIG_4KSTACKS
45	struct irq_desc *desc;
46	union irq_ctx *curctx, *irqctx;
47	u32 *isp;
48#endif
49
50	irq_enter();
51
52	irq = irq_linear_revmap(root_domain, irq);
53
54#ifdef CONFIG_DEBUG_STACKOVERFLOW
55	/* Debugging check for stack overflow: is there less than 1KB free? */
56	{
57		unsigned long sp;
58
59		sp = __core_reg_get(A0StP);
60		sp &= THREAD_SIZE - 1;
61
62		if (unlikely(sp > (THREAD_SIZE - 1024)))
63			pr_err("Stack overflow in do_IRQ: %ld\n", sp);
64	}
65#endif
66
67
68#ifdef CONFIG_4KSTACKS
69	curctx = (union irq_ctx *) current_thread_info();
70	irqctx = hardirq_ctx[smp_processor_id()];
71
72	/*
73	 * this is where we switch to the IRQ stack. However, if we are
74	 * already using the IRQ stack (because we interrupted a hardirq
75	 * handler) we can't do that and just have to keep using the
76	 * current stack (which is the irq stack already after all)
77	 */
78	if (curctx != irqctx) {
79		/* build the stack frame on the IRQ stack */
80		isp = (u32 *) ((char *)irqctx + sizeof(struct thread_info));
81		irqctx->tinfo.task = curctx->tinfo.task;
82
83		/*
84		 * Copy the softirq bits in preempt_count so that the
85		 * softirq checks work in the hardirq context.
86		 */
87		irqctx->tinfo.preempt_count =
88			(irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) |
89			(curctx->tinfo.preempt_count & SOFTIRQ_MASK);
90
91		desc = irq_to_desc(irq);
92
93		asm volatile (
94			"MOV   D0.5,%0\n"
95			"MOV   D1Ar1,%1\n"
96			"MOV   D1RtP,%2\n"
97			"SWAP  A0StP,D0.5\n"
98			"SWAP  PC,D1RtP\n"
99			"MOV   A0StP,D0.5\n"
100			:
101			: "r" (isp), "r" (desc), "r" (desc->handle_irq)
102			: "memory", "cc", "D1Ar1", "D0Ar2", "D1Ar3", "D0Ar4",
103			  "D1Ar5", "D0Ar6", "D0Re0", "D1Re0", "D0.4", "D1RtP",
104			  "D0.5"
105			);
106	} else
107#endif
108		generic_handle_irq(irq);
109
110	irq_exit();
111
112	set_irq_regs(old_regs);
113}
114
115#ifdef CONFIG_4KSTACKS
116
117static char softirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
118
119static char hardirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
120
121/*
122 * allocate per-cpu stacks for hardirq and for softirq processing
123 */
124void irq_ctx_init(int cpu)
125{
126	union irq_ctx *irqctx;
127
128	if (hardirq_ctx[cpu])
129		return;
130
131	irqctx = (union irq_ctx *) &hardirq_stack[cpu * THREAD_SIZE];
132	irqctx->tinfo.task              = NULL;
133	irqctx->tinfo.cpu               = cpu;
134	irqctx->tinfo.preempt_count     = HARDIRQ_OFFSET;
135	irqctx->tinfo.addr_limit        = MAKE_MM_SEG(0);
136
137	hardirq_ctx[cpu] = irqctx;
138
139	irqctx = (union irq_ctx *) &softirq_stack[cpu * THREAD_SIZE];
140	irqctx->tinfo.task              = NULL;
141	irqctx->tinfo.cpu               = cpu;
142	irqctx->tinfo.preempt_count     = 0;
143	irqctx->tinfo.addr_limit        = MAKE_MM_SEG(0);
144
145	softirq_ctx[cpu] = irqctx;
146
147	pr_info("CPU %u irqstacks, hard=%p soft=%p\n",
148		cpu, hardirq_ctx[cpu], softirq_ctx[cpu]);
149}
150
151void irq_ctx_exit(int cpu)
152{
153	hardirq_ctx[smp_processor_id()] = NULL;
154}
155
156extern asmlinkage void __do_softirq(void);
157
158void do_softirq_own_stack(void)
159{
160	struct thread_info *curctx;
161	union irq_ctx *irqctx;
162	u32 *isp;
163
164	curctx = current_thread_info();
165	irqctx = softirq_ctx[smp_processor_id()];
166	irqctx->tinfo.task = curctx->task;
167
168	/* build the stack frame on the softirq stack */
169	isp = (u32 *) ((char *)irqctx + sizeof(struct thread_info));
170
171	asm volatile (
172		"MOV   D0.5,%0\n"
173		"SWAP  A0StP,D0.5\n"
174		"CALLR D1RtP,___do_softirq\n"
175		"MOV   A0StP,D0.5\n"
176		:
177		: "r" (isp)
178		: "memory", "cc", "D1Ar1", "D0Ar2", "D1Ar3", "D0Ar4",
179		  "D1Ar5", "D0Ar6", "D0Re0", "D1Re0", "D0.4", "D1RtP",
180		  "D0.5"
181		);
182}
183#endif
184
185static struct irq_chip meta_irq_type = {
186	.name = "META-IRQ",
187	.irq_startup = startup_meta_irq,
188	.irq_shutdown = shutdown_meta_irq,
189};
190
191/**
192 * tbisig_map() - Map a TBI signal number to a virtual IRQ number.
193 * @hw:		Number of the TBI signal. Must be in range.
194 *
195 * Returns:	The virtual IRQ number of the TBI signal number IRQ specified by
196 *		@hw.
197 */
198int tbisig_map(unsigned int hw)
199{
200	return irq_create_mapping(root_domain, hw);
201}
202
203/**
204 * metag_tbisig_map() - map a tbi signal to a Linux virtual IRQ number
205 * @d:		root irq domain
206 * @irq:	virtual irq number
207 * @hw:		hardware irq number (TBI signal number)
208 *
209 * This sets up a virtual irq for a specified TBI signal number.
210 */
211static int metag_tbisig_map(struct irq_domain *d, unsigned int irq,
212			    irq_hw_number_t hw)
213{
214#ifdef CONFIG_SMP
215	irq_set_chip_and_handler(irq, &meta_irq_type, handle_percpu_irq);
216#else
217	irq_set_chip_and_handler(irq, &meta_irq_type, handle_simple_irq);
218#endif
219	return 0;
220}
221
222static const struct irq_domain_ops metag_tbisig_domain_ops = {
223	.map = metag_tbisig_map,
224};
225
226/*
227 * void init_IRQ(void)
228 *
229 * Parameters:	None
230 *
231 * Returns:	Nothing
232 *
233 * This function should be called during kernel startup to initialize
234 * the IRQ handling routines.
235 */
236void __init init_IRQ(void)
237{
238	root_domain = irq_domain_add_linear(NULL, 32,
239					    &metag_tbisig_domain_ops, NULL);
240	if (unlikely(!root_domain))
241		panic("init_IRQ: cannot add root IRQ domain");
242
243	irq_ctx_init(smp_processor_id());
244
245	init_internal_IRQ();
246	init_external_IRQ();
247
248	if (machine_desc->init_irq)
249		machine_desc->init_irq();
250}
251
252int __init arch_probe_nr_irqs(void)
253{
254	if (machine_desc->nr_irqs)
255		nr_irqs = machine_desc->nr_irqs;
256	return 0;
257}
258
259#ifdef CONFIG_HOTPLUG_CPU
260/*
261 * The CPU has been marked offline.  Migrate IRQs off this CPU.  If
262 * the affinity settings do not allow other CPUs, force them onto any
263 * available CPU.
264 */
265void migrate_irqs(void)
266{
267	unsigned int i, cpu = smp_processor_id();
268
269	for_each_active_irq(i) {
270		struct irq_data *data = irq_get_irq_data(i);
271		struct cpumask *mask;
272		unsigned int newcpu;
273
274		if (irqd_is_per_cpu(data))
275			continue;
276
277		mask = irq_data_get_affinity_mask(data);
278		if (!cpumask_test_cpu(cpu, mask))
279			continue;
280
281		newcpu = cpumask_any_and(mask, cpu_online_mask);
282
283		if (newcpu >= nr_cpu_ids) {
284			pr_info_ratelimited("IRQ%u no longer affine to CPU%u\n",
285					    i, cpu);
286
287			cpumask_setall(mask);
288		}
289		irq_set_affinity(i, mask);
290	}
291}
292#endif /* CONFIG_HOTPLUG_CPU */
293