xref: /openbmc/linux/arch/sh/kernel/irq.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a6a31139SPaul Mundt /*
31da177e4SLinus Torvalds  * linux/arch/sh/kernel/irq.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * SuperH version:  Copyright (C) 1999  Niibe Yutaka
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds #include <linux/irq.h>
11bf3a00f8SPaul Mundt #include <linux/interrupt.h>
12a6a31139SPaul Mundt #include <linux/module.h>
13bf3a00f8SPaul Mundt #include <linux/kernel_stat.h>
14bf3a00f8SPaul Mundt #include <linux/seq_file.h>
15ba93483fSPaul Mundt #include <linux/ftrace.h>
16763142d1SPaul Mundt #include <linux/delay.h>
179ab3a15dSPaul Mundt #include <linux/ratelimit.h>
18bf3a00f8SPaul Mundt #include <asm/processor.h>
19be782df5SPaul Mundt #include <asm/machvec.h>
207c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
21a6a31139SPaul Mundt #include <asm/thread_info.h>
22f15cbe6fSPaul Mundt #include <cpu/mmu_context.h>
23db1cc7aeSThomas Gleixner #include <asm/softirq_stack.h>
241da177e4SLinus Torvalds 
2535f3c518SPaul Mundt atomic_t irq_err_count;
2635f3c518SPaul Mundt 
271da177e4SLinus Torvalds /*
281da177e4SLinus Torvalds  * 'what should we do if we get a hw irq event on an illegal vector'.
291da177e4SLinus Torvalds  * each architecture has to answer this themselves, it doesn't deserve
301da177e4SLinus Torvalds  * a generic callback i think.
311da177e4SLinus Torvalds  */
ack_bad_irq(unsigned int irq)321da177e4SLinus Torvalds void ack_bad_irq(unsigned int irq)
331da177e4SLinus Torvalds {
34baf4326eSPaul Mundt 	atomic_inc(&irq_err_count);
351da177e4SLinus Torvalds 	printk("unexpected IRQ trap at vector %02x\n", irq);
361da177e4SLinus Torvalds }
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #if defined(CONFIG_PROC_FS)
39fa1d43abSPaul Mundt /*
403d44ae40SPaul Mundt  * /proc/interrupts printing for arch specific interrupts
41fa1d43abSPaul Mundt  */
arch_show_interrupts(struct seq_file * p,int prec)423d44ae40SPaul Mundt int arch_show_interrupts(struct seq_file *p, int prec)
43fa1d43abSPaul Mundt {
44731ba330SPaul Mundt 	int j;
45731ba330SPaul Mundt 
46731ba330SPaul Mundt 	seq_printf(p, "%*s: ", prec, "NMI");
47731ba330SPaul Mundt 	for_each_online_cpu(j)
4815b8d937SGeert Uytterhoeven 		seq_printf(p, "%10u ", per_cpu(irq_stat.__nmi_count, j));
49731ba330SPaul Mundt 	seq_printf(p, "  Non-maskable interrupts\n");
50731ba330SPaul Mundt 
51fa1d43abSPaul Mundt 	seq_printf(p, "%*s: %10u\n", prec, "ERR", atomic_read(&irq_err_count));
52731ba330SPaul Mundt 
53fa1d43abSPaul Mundt 	return 0;
54fa1d43abSPaul Mundt }
551da177e4SLinus Torvalds #endif
561da177e4SLinus Torvalds 
57110ed282SPaul Mundt #ifdef CONFIG_IRQSTACKS
58a6a31139SPaul Mundt /*
59a6a31139SPaul Mundt  * per-CPU IRQ handling contexts (thread information and stack)
60a6a31139SPaul Mundt  */
61a6a31139SPaul Mundt union irq_ctx {
62a6a31139SPaul Mundt 	struct thread_info	tinfo;
63a6a31139SPaul Mundt 	u32			stack[THREAD_SIZE/sizeof(u32)];
64a6a31139SPaul Mundt };
65a6a31139SPaul Mundt 
661dc41e58SPaul Mundt static union irq_ctx *hardirq_ctx[NR_CPUS] __read_mostly;
671dc41e58SPaul Mundt static union irq_ctx *softirq_ctx[NR_CPUS] __read_mostly;
68a6a31139SPaul Mundt 
69dc825b17SPaul Mundt static char softirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
70dc825b17SPaul Mundt static char hardirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
71dc825b17SPaul Mundt 
handle_one_irq(unsigned int irq)72dc825b17SPaul Mundt static inline void handle_one_irq(unsigned int irq)
731da177e4SLinus Torvalds {
74a6a31139SPaul Mundt 	union irq_ctx *curctx, *irqctx;
751da177e4SLinus Torvalds 
76a6a31139SPaul Mundt 	curctx = (union irq_ctx *)current_thread_info();
77a6a31139SPaul Mundt 	irqctx = hardirq_ctx[smp_processor_id()];
78a6a31139SPaul Mundt 
79a6a31139SPaul Mundt 	/*
80a6a31139SPaul Mundt 	 * this is where we switch to the IRQ stack. However, if we are
81a6a31139SPaul Mundt 	 * already using the IRQ stack (because we interrupted a hardirq
82a6a31139SPaul Mundt 	 * handler) we can't do that and just have to keep using the
83a6a31139SPaul Mundt 	 * current stack (which is the irq stack already after all)
84a6a31139SPaul Mundt 	 */
85a6a31139SPaul Mundt 	if (curctx != irqctx) {
86a6a31139SPaul Mundt 		u32 *isp;
87a6a31139SPaul Mundt 
88a6a31139SPaul Mundt 		isp = (u32 *)((char *)irqctx + sizeof(*irqctx));
89a6a31139SPaul Mundt 		irqctx->tinfo.task = curctx->tinfo.task;
90a6a31139SPaul Mundt 		irqctx->tinfo.previous_sp = current_stack_pointer;
91a6a31139SPaul Mundt 
921dc41e58SPaul Mundt 		/*
931dc41e58SPaul Mundt 		 * Copy the softirq bits in preempt_count so that the
941dc41e58SPaul Mundt 		 * softirq checks work in the hardirq context.
951dc41e58SPaul Mundt 		 */
961dc41e58SPaul Mundt 		irqctx->tinfo.preempt_count =
971dc41e58SPaul Mundt 			(irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) |
981dc41e58SPaul Mundt 			(curctx->tinfo.preempt_count & SOFTIRQ_MASK);
991dc41e58SPaul Mundt 
100a6a31139SPaul Mundt 		__asm__ __volatile__ (
101a6a31139SPaul Mundt 			"mov	%0, r4		\n"
1021dc41e58SPaul Mundt 			"mov	r15, r8		\n"
103baf4326eSPaul Mundt 			"jsr	@%1		\n"
10408a7e621SMasahiro Yamada 			/* switch to the irq stack */
105baf4326eSPaul Mundt 			" mov	%2, r15		\n"
106a6a31139SPaul Mundt 			/* restore the stack (ring zero) */
1071dc41e58SPaul Mundt 			"mov	r8, r15		\n"
108a6a31139SPaul Mundt 			: /* no outputs */
10935f3c518SPaul Mundt 			: "r" (irq), "r" (generic_handle_irq), "r" (isp)
110a6a31139SPaul Mundt 			: "memory", "r0", "r1", "r2", "r3", "r4",
111a6a31139SPaul Mundt 			  "r5", "r6", "r7", "r8", "t", "pr"
112a6a31139SPaul Mundt 		);
113a6a31139SPaul Mundt 	} else
11435f3c518SPaul Mundt 		generic_handle_irq(irq);
1151da177e4SLinus Torvalds }
116a6a31139SPaul Mundt 
117a6a31139SPaul Mundt /*
118a6a31139SPaul Mundt  * allocate per-cpu stacks for hardirq and for softirq processing
119a6a31139SPaul Mundt  */
irq_ctx_init(int cpu)120a6a31139SPaul Mundt void irq_ctx_init(int cpu)
121a6a31139SPaul Mundt {
122a6a31139SPaul Mundt 	union irq_ctx *irqctx;
123a6a31139SPaul Mundt 
124a6a31139SPaul Mundt 	if (hardirq_ctx[cpu])
125a6a31139SPaul Mundt 		return;
126a6a31139SPaul Mundt 
127a6a31139SPaul Mundt 	irqctx = (union irq_ctx *)&hardirq_stack[cpu * THREAD_SIZE];
128a6a31139SPaul Mundt 	irqctx->tinfo.task		= NULL;
129a6a31139SPaul Mundt 	irqctx->tinfo.cpu		= cpu;
130a6a31139SPaul Mundt 	irqctx->tinfo.preempt_count	= HARDIRQ_OFFSET;
131a6a31139SPaul Mundt 	irqctx->tinfo.addr_limit	= MAKE_MM_SEG(0);
132a6a31139SPaul Mundt 
133a6a31139SPaul Mundt 	hardirq_ctx[cpu] = irqctx;
134a6a31139SPaul Mundt 
135a6a31139SPaul Mundt 	irqctx = (union irq_ctx *)&softirq_stack[cpu * THREAD_SIZE];
136a6a31139SPaul Mundt 	irqctx->tinfo.task		= NULL;
137a6a31139SPaul Mundt 	irqctx->tinfo.cpu		= cpu;
1381dc41e58SPaul Mundt 	irqctx->tinfo.preempt_count	= 0;
139a6a31139SPaul Mundt 	irqctx->tinfo.addr_limit	= MAKE_MM_SEG(0);
140a6a31139SPaul Mundt 
141a6a31139SPaul Mundt 	softirq_ctx[cpu] = irqctx;
142a6a31139SPaul Mundt 
143a6a31139SPaul Mundt 	printk("CPU %u irqstacks, hard=%p soft=%p\n",
144a6a31139SPaul Mundt 		cpu, hardirq_ctx[cpu], softirq_ctx[cpu]);
145a6a31139SPaul Mundt }
146a6a31139SPaul Mundt 
irq_ctx_exit(int cpu)147a6a31139SPaul Mundt void irq_ctx_exit(int cpu)
148a6a31139SPaul Mundt {
149a6a31139SPaul Mundt 	hardirq_ctx[cpu] = NULL;
150a6a31139SPaul Mundt }
151a6a31139SPaul Mundt 
152*8cbb2b50SSebastian Andrzej Siewior #ifdef CONFIG_SOFTIRQ_ON_OWN_STACK
do_softirq_own_stack(void)1537d65f4a6SFrederic Weisbecker void do_softirq_own_stack(void)
154a6a31139SPaul Mundt {
155a6a31139SPaul Mundt 	struct thread_info *curctx;
156a6a31139SPaul Mundt 	union irq_ctx *irqctx;
157a6a31139SPaul Mundt 	u32 *isp;
158a6a31139SPaul Mundt 
159a6a31139SPaul Mundt 	curctx = current_thread_info();
160a6a31139SPaul Mundt 	irqctx = softirq_ctx[smp_processor_id()];
161a6a31139SPaul Mundt 	irqctx->tinfo.task = curctx->task;
162a6a31139SPaul Mundt 	irqctx->tinfo.previous_sp = current_stack_pointer;
163a6a31139SPaul Mundt 
164a6a31139SPaul Mundt 	/* build the stack frame on the softirq stack */
165a6a31139SPaul Mundt 	isp = (u32 *)((char *)irqctx + sizeof(*irqctx));
166a6a31139SPaul Mundt 
167a6a31139SPaul Mundt 	__asm__ __volatile__ (
168a6a31139SPaul Mundt 		"mov	r15, r9		\n"
169a6a31139SPaul Mundt 		"jsr	@%0		\n"
170a6a31139SPaul Mundt 		/* switch to the softirq stack */
171a6a31139SPaul Mundt 		" mov	%1, r15		\n"
172a6a31139SPaul Mundt 		/* restore the thread stack */
173a6a31139SPaul Mundt 		"mov	r9, r15		\n"
174a6a31139SPaul Mundt 		: /* no outputs */
175a6a31139SPaul Mundt 		: "r" (__do_softirq), "r" (isp)
176a6a31139SPaul Mundt 		: "memory", "r0", "r1", "r2", "r3", "r4",
177a6a31139SPaul Mundt 		  "r5", "r6", "r7", "r8", "r9", "r15", "t", "pr"
178a6a31139SPaul Mundt 	);
179a6a31139SPaul Mundt }
180f2c50921SSebastian Andrzej Siewior #endif
181dc825b17SPaul Mundt #else
handle_one_irq(unsigned int irq)182dc825b17SPaul Mundt static inline void handle_one_irq(unsigned int irq)
183dc825b17SPaul Mundt {
184dc825b17SPaul Mundt 	generic_handle_irq(irq);
185dc825b17SPaul Mundt }
186a6a31139SPaul Mundt #endif
187ea0f8feaSJamie Lenehan 
do_IRQ(unsigned int irq,struct pt_regs * regs)188dc825b17SPaul Mundt asmlinkage __irq_entry int do_IRQ(unsigned int irq, struct pt_regs *regs)
189dc825b17SPaul Mundt {
190dc825b17SPaul Mundt 	struct pt_regs *old_regs = set_irq_regs(regs);
191dc825b17SPaul Mundt 
192dc825b17SPaul Mundt 	irq_enter();
193dc825b17SPaul Mundt 
194dc825b17SPaul Mundt 	irq = irq_demux(irq_lookup(irq));
195dc825b17SPaul Mundt 
196dc825b17SPaul Mundt 	if (irq != NO_IRQ_IGNORE) {
197dc825b17SPaul Mundt 		handle_one_irq(irq);
198dc825b17SPaul Mundt 		irq_finish(irq);
199dc825b17SPaul Mundt 	}
200dc825b17SPaul Mundt 
201dc825b17SPaul Mundt 	irq_exit();
202dc825b17SPaul Mundt 
203dc825b17SPaul Mundt 	set_irq_regs(old_regs);
204dc825b17SPaul Mundt 
205dc825b17SPaul Mundt 	return IRQ_HANDLED;
206dc825b17SPaul Mundt }
207dc825b17SPaul Mundt 
init_IRQ(void)208ea0f8feaSJamie Lenehan void __init init_IRQ(void)
209ea0f8feaSJamie Lenehan {
21090015c89SMagnus Damm 	plat_irq_setup();
211ea0f8feaSJamie Lenehan 
212ea0f8feaSJamie Lenehan 	/* Perform the machine specific initialisation */
213ea0f8feaSJamie Lenehan 	if (sh_mv.mv_init_irq)
214ea0f8feaSJamie Lenehan 		sh_mv.mv_init_irq();
215ea0f8feaSJamie Lenehan 
216c1e30ad9SPaul Mundt 	intc_finalize();
217c1e30ad9SPaul Mundt 
218ea0f8feaSJamie Lenehan 	irq_ctx_init(smp_processor_id());
219ea0f8feaSJamie Lenehan }
220d8586ba6SPaul Mundt 
221763142d1SPaul Mundt #ifdef CONFIG_HOTPLUG_CPU
222763142d1SPaul Mundt /*
223763142d1SPaul Mundt  * The CPU has been marked offline.  Migrate IRQs off this CPU.  If
224763142d1SPaul Mundt  * the affinity settings do not allow other CPUs, force them onto any
225763142d1SPaul Mundt  * available CPU.
226763142d1SPaul Mundt  */
migrate_irqs(void)227763142d1SPaul Mundt void migrate_irqs(void)
228763142d1SPaul Mundt {
229763142d1SPaul Mundt 	unsigned int irq, cpu = smp_processor_id();
230763142d1SPaul Mundt 
231fb41a49dSPaul Mundt 	for_each_active_irq(irq) {
232fb41a49dSPaul Mundt 		struct irq_data *data = irq_get_irq_data(irq);
233fb41a49dSPaul Mundt 
234cde5c275SJiang Liu 		if (irq_data_get_node(data) == cpu) {
2354d0b8298SSamuel Holland 			const struct cpumask *mask = irq_data_get_affinity_mask(data);
2368b8149dfSThomas Gleixner 			unsigned int newcpu = cpumask_any_and(mask,
237763142d1SPaul Mundt 							      cpu_online_mask);
238763142d1SPaul Mundt 			if (newcpu >= nr_cpu_ids) {
2399ab3a15dSPaul Mundt 				pr_info_ratelimited("IRQ%u no longer affine to CPU%u\n",
240763142d1SPaul Mundt 						    irq, cpu);
241763142d1SPaul Mundt 
2424d0b8298SSamuel Holland 				irq_set_affinity(irq, cpu_all_mask);
2434d0b8298SSamuel Holland 			} else {
2448b8149dfSThomas Gleixner 				irq_set_affinity(irq, mask);
245763142d1SPaul Mundt 			}
246763142d1SPaul Mundt 		}
247763142d1SPaul Mundt 	}
2484d0b8298SSamuel Holland }
249763142d1SPaul Mundt #endif
250