xref: /linux/arch/sh/kernel/irq.c (revision 662fa3d6099374c4615bf64d06895e3573b935b2)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/arch/sh/kernel/irq.c
4  *
5  *	Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar
6  *
7  *
8  * SuperH version:  Copyright (C) 1999  Niibe Yutaka
9  */
10 #include <linux/irq.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/kernel_stat.h>
14 #include <linux/seq_file.h>
15 #include <linux/ftrace.h>
16 #include <linux/delay.h>
17 #include <linux/ratelimit.h>
18 #include <asm/processor.h>
19 #include <asm/machvec.h>
20 #include <linux/uaccess.h>
21 #include <asm/thread_info.h>
22 #include <cpu/mmu_context.h>
23 #include <asm/softirq_stack.h>
24 
25 atomic_t irq_err_count;
26 
27 /*
28  * 'what should we do if we get a hw irq event on an illegal vector'.
29  * each architecture has to answer this themselves, it doesn't deserve
30  * a generic callback i think.
31  */
32 void ack_bad_irq(unsigned int irq)
33 {
34 	atomic_inc(&irq_err_count);
35 	printk("unexpected IRQ trap at vector %02x\n", irq);
36 }
37 
38 #if defined(CONFIG_PROC_FS)
39 /*
40  * /proc/interrupts printing for arch specific interrupts
41  */
42 int arch_show_interrupts(struct seq_file *p, int prec)
43 {
44 	int j;
45 
46 	seq_printf(p, "%*s: ", prec, "NMI");
47 	for_each_online_cpu(j)
48 		seq_printf(p, "%10u ", per_cpu(irq_stat.__nmi_count, j));
49 	seq_printf(p, "  Non-maskable interrupts\n");
50 
51 	seq_printf(p, "%*s: %10u\n", prec, "ERR", atomic_read(&irq_err_count));
52 
53 	return 0;
54 }
55 #endif
56 
57 #ifdef CONFIG_IRQSTACKS
58 /*
59  * per-CPU IRQ handling contexts (thread information and stack)
60  */
61 union irq_ctx {
62 	struct thread_info	tinfo;
63 	u32			stack[THREAD_SIZE/sizeof(u32)];
64 };
65 
66 static union irq_ctx *hardirq_ctx[NR_CPUS] __read_mostly;
67 static union irq_ctx *softirq_ctx[NR_CPUS] __read_mostly;
68 
69 static char softirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
70 static char hardirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
71 
72 static inline void handle_one_irq(unsigned int irq)
73 {
74 	union irq_ctx *curctx, *irqctx;
75 
76 	curctx = (union irq_ctx *)current_thread_info();
77 	irqctx = hardirq_ctx[smp_processor_id()];
78 
79 	/*
80 	 * this is where we switch to the IRQ stack. However, if we are
81 	 * already using the IRQ stack (because we interrupted a hardirq
82 	 * handler) we can't do that and just have to keep using the
83 	 * current stack (which is the irq stack already after all)
84 	 */
85 	if (curctx != irqctx) {
86 		u32 *isp;
87 
88 		isp = (u32 *)((char *)irqctx + sizeof(*irqctx));
89 		irqctx->tinfo.task = curctx->tinfo.task;
90 		irqctx->tinfo.previous_sp = current_stack_pointer;
91 
92 		/*
93 		 * Copy the softirq bits in preempt_count so that the
94 		 * softirq checks work in the hardirq context.
95 		 */
96 		irqctx->tinfo.preempt_count =
97 			(irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) |
98 			(curctx->tinfo.preempt_count & SOFTIRQ_MASK);
99 
100 		__asm__ __volatile__ (
101 			"mov	%0, r4		\n"
102 			"mov	r15, r8		\n"
103 			"jsr	@%1		\n"
104 			/* switch to the irq stack */
105 			" mov	%2, r15		\n"
106 			/* restore the stack (ring zero) */
107 			"mov	r8, r15		\n"
108 			: /* no outputs */
109 			: "r" (irq), "r" (generic_handle_irq), "r" (isp)
110 			: "memory", "r0", "r1", "r2", "r3", "r4",
111 			  "r5", "r6", "r7", "r8", "t", "pr"
112 		);
113 	} else
114 		generic_handle_irq(irq);
115 }
116 
117 /*
118  * allocate per-cpu stacks for hardirq and for softirq processing
119  */
120 void irq_ctx_init(int cpu)
121 {
122 	union irq_ctx *irqctx;
123 
124 	if (hardirq_ctx[cpu])
125 		return;
126 
127 	irqctx = (union irq_ctx *)&hardirq_stack[cpu * THREAD_SIZE];
128 	irqctx->tinfo.task		= NULL;
129 	irqctx->tinfo.cpu		= cpu;
130 	irqctx->tinfo.preempt_count	= HARDIRQ_OFFSET;
131 	irqctx->tinfo.addr_limit	= MAKE_MM_SEG(0);
132 
133 	hardirq_ctx[cpu] = irqctx;
134 
135 	irqctx = (union irq_ctx *)&softirq_stack[cpu * THREAD_SIZE];
136 	irqctx->tinfo.task		= NULL;
137 	irqctx->tinfo.cpu		= cpu;
138 	irqctx->tinfo.preempt_count	= 0;
139 	irqctx->tinfo.addr_limit	= MAKE_MM_SEG(0);
140 
141 	softirq_ctx[cpu] = irqctx;
142 
143 	printk("CPU %u irqstacks, hard=%p soft=%p\n",
144 		cpu, hardirq_ctx[cpu], softirq_ctx[cpu]);
145 }
146 
147 void irq_ctx_exit(int cpu)
148 {
149 	hardirq_ctx[cpu] = NULL;
150 }
151 
152 void do_softirq_own_stack(void)
153 {
154 	struct thread_info *curctx;
155 	union irq_ctx *irqctx;
156 	u32 *isp;
157 
158 	curctx = current_thread_info();
159 	irqctx = softirq_ctx[smp_processor_id()];
160 	irqctx->tinfo.task = curctx->task;
161 	irqctx->tinfo.previous_sp = current_stack_pointer;
162 
163 	/* build the stack frame on the softirq stack */
164 	isp = (u32 *)((char *)irqctx + sizeof(*irqctx));
165 
166 	__asm__ __volatile__ (
167 		"mov	r15, r9		\n"
168 		"jsr	@%0		\n"
169 		/* switch to the softirq stack */
170 		" mov	%1, r15		\n"
171 		/* restore the thread stack */
172 		"mov	r9, r15		\n"
173 		: /* no outputs */
174 		: "r" (__do_softirq), "r" (isp)
175 		: "memory", "r0", "r1", "r2", "r3", "r4",
176 		  "r5", "r6", "r7", "r8", "r9", "r15", "t", "pr"
177 	);
178 }
179 #else
180 static inline void handle_one_irq(unsigned int irq)
181 {
182 	generic_handle_irq(irq);
183 }
184 #endif
185 
186 asmlinkage __irq_entry int do_IRQ(unsigned int irq, struct pt_regs *regs)
187 {
188 	struct pt_regs *old_regs = set_irq_regs(regs);
189 
190 	irq_enter();
191 
192 	irq = irq_demux(irq_lookup(irq));
193 
194 	if (irq != NO_IRQ_IGNORE) {
195 		handle_one_irq(irq);
196 		irq_finish(irq);
197 	}
198 
199 	irq_exit();
200 
201 	set_irq_regs(old_regs);
202 
203 	return IRQ_HANDLED;
204 }
205 
206 void __init init_IRQ(void)
207 {
208 	plat_irq_setup();
209 
210 	/* Perform the machine specific initialisation */
211 	if (sh_mv.mv_init_irq)
212 		sh_mv.mv_init_irq();
213 
214 	intc_finalize();
215 
216 	irq_ctx_init(smp_processor_id());
217 }
218 
219 #ifdef CONFIG_HOTPLUG_CPU
220 /*
221  * The CPU has been marked offline.  Migrate IRQs off this CPU.  If
222  * the affinity settings do not allow other CPUs, force them onto any
223  * available CPU.
224  */
225 void migrate_irqs(void)
226 {
227 	unsigned int irq, cpu = smp_processor_id();
228 
229 	for_each_active_irq(irq) {
230 		struct irq_data *data = irq_get_irq_data(irq);
231 
232 		if (irq_data_get_node(data) == cpu) {
233 			struct cpumask *mask = irq_data_get_affinity_mask(data);
234 			unsigned int newcpu = cpumask_any_and(mask,
235 							      cpu_online_mask);
236 			if (newcpu >= nr_cpu_ids) {
237 				pr_info_ratelimited("IRQ%u no longer affine to CPU%u\n",
238 						    irq, cpu);
239 
240 				cpumask_setall(mask);
241 			}
242 			irq_set_affinity(irq, mask);
243 		}
244 	}
245 }
246 #endif
247