xref: /openbmc/linux/arch/powerpc/kernel/irq_64.c (revision 3b73c45e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Derived from arch/i386/kernel/irq.c
4  *    Copyright (C) 1992 Linus Torvalds
5  *  Adapted from arch/i386 by Gary Thomas
6  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
7  *  Updated and modified by Cort Dougan <cort@fsmlabs.com>
8  *    Copyright (C) 1996-2001 Cort Dougan
9  *  Adapted for Power Macintosh by Paul Mackerras
10  *    Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
11  *
12  * This file contains the code used by various IRQ handling routines:
13  * asking for different IRQ's should be done through these routines
14  * instead of just grabbing them. Thus setups with different IRQ numbers
15  * shouldn't result in any weird surprises, and installing new handlers
16  * should be easier.
17  */
18 
19 #undef DEBUG
20 
21 #include <linux/export.h>
22 #include <linux/threads.h>
23 #include <linux/kernel_stat.h>
24 #include <linux/signal.h>
25 #include <linux/sched.h>
26 #include <linux/ptrace.h>
27 #include <linux/ioport.h>
28 #include <linux/interrupt.h>
29 #include <linux/timex.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/delay.h>
33 #include <linux/irq.h>
34 #include <linux/seq_file.h>
35 #include <linux/cpumask.h>
36 #include <linux/profile.h>
37 #include <linux/bitops.h>
38 #include <linux/list.h>
39 #include <linux/radix-tree.h>
40 #include <linux/mutex.h>
41 #include <linux/pci.h>
42 #include <linux/debugfs.h>
43 #include <linux/of.h>
44 #include <linux/of_irq.h>
45 #include <linux/vmalloc.h>
46 #include <linux/pgtable.h>
47 #include <linux/static_call.h>
48 
49 #include <linux/uaccess.h>
50 #include <asm/interrupt.h>
51 #include <asm/io.h>
52 #include <asm/irq.h>
53 #include <asm/cache.h>
54 #include <asm/ptrace.h>
55 #include <asm/machdep.h>
56 #include <asm/udbg.h>
57 #include <asm/smp.h>
58 #include <asm/hw_irq.h>
59 #include <asm/softirq_stack.h>
60 #include <asm/ppc_asm.h>
61 
62 #include <asm/paca.h>
63 #include <asm/firmware.h>
64 #include <asm/lv1call.h>
65 #include <asm/dbell.h>
66 #include <asm/trace.h>
67 #include <asm/cpu_has_feature.h>
68 
69 int distribute_irqs = 1;
70 
71 void replay_soft_interrupts(void)
72 {
73 	struct pt_regs regs;
74 
75 	/*
76 	 * Be careful here, calling these interrupt handlers can cause
77 	 * softirqs to be raised, which they may run when calling irq_exit,
78 	 * which will cause local_irq_enable() to be run, which can then
79 	 * recurse into this function. Don't keep any state across
80 	 * interrupt handler calls which may change underneath us.
81 	 *
82 	 * We use local_paca rather than get_paca() to avoid all the
83 	 * debug_smp_processor_id() business in this low level function.
84 	 */
85 
86 	ppc_save_regs(&regs);
87 	regs.softe = IRQS_ENABLED;
88 	regs.msr |= MSR_EE;
89 
90 again:
91 	if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG))
92 		WARN_ON_ONCE(mfmsr() & MSR_EE);
93 
94 	/*
95 	 * Force the delivery of pending soft-disabled interrupts on PS3.
96 	 * Any HV call will have this side effect.
97 	 */
98 	if (firmware_has_feature(FW_FEATURE_PS3_LV1)) {
99 		u64 tmp, tmp2;
100 		lv1_get_version_info(&tmp, &tmp2);
101 	}
102 
103 	/*
104 	 * Check if an hypervisor Maintenance interrupt happened.
105 	 * This is a higher priority interrupt than the others, so
106 	 * replay it first.
107 	 */
108 	if (IS_ENABLED(CONFIG_PPC_BOOK3S) && (local_paca->irq_happened & PACA_IRQ_HMI)) {
109 		local_paca->irq_happened &= ~PACA_IRQ_HMI;
110 		regs.trap = INTERRUPT_HMI;
111 		handle_hmi_exception(&regs);
112 		if (!(local_paca->irq_happened & PACA_IRQ_HARD_DIS))
113 			hard_irq_disable();
114 	}
115 
116 	if (local_paca->irq_happened & PACA_IRQ_DEC) {
117 		local_paca->irq_happened &= ~PACA_IRQ_DEC;
118 		regs.trap = INTERRUPT_DECREMENTER;
119 		timer_interrupt(&regs);
120 		if (!(local_paca->irq_happened & PACA_IRQ_HARD_DIS))
121 			hard_irq_disable();
122 	}
123 
124 	if (local_paca->irq_happened & PACA_IRQ_EE) {
125 		local_paca->irq_happened &= ~PACA_IRQ_EE;
126 		regs.trap = INTERRUPT_EXTERNAL;
127 		do_IRQ(&regs);
128 		if (!(local_paca->irq_happened & PACA_IRQ_HARD_DIS))
129 			hard_irq_disable();
130 	}
131 
132 	if (IS_ENABLED(CONFIG_PPC_DOORBELL) && (local_paca->irq_happened & PACA_IRQ_DBELL)) {
133 		local_paca->irq_happened &= ~PACA_IRQ_DBELL;
134 		regs.trap = INTERRUPT_DOORBELL;
135 		doorbell_exception(&regs);
136 		if (!(local_paca->irq_happened & PACA_IRQ_HARD_DIS))
137 			hard_irq_disable();
138 	}
139 
140 	/* Book3E does not support soft-masking PMI interrupts */
141 	if (IS_ENABLED(CONFIG_PPC_BOOK3S) && (local_paca->irq_happened & PACA_IRQ_PMI)) {
142 		local_paca->irq_happened &= ~PACA_IRQ_PMI;
143 		regs.trap = INTERRUPT_PERFMON;
144 		performance_monitor_exception(&regs);
145 		if (!(local_paca->irq_happened & PACA_IRQ_HARD_DIS))
146 			hard_irq_disable();
147 	}
148 
149 	if (local_paca->irq_happened & ~PACA_IRQ_HARD_DIS) {
150 		/*
151 		 * We are responding to the next interrupt, so interrupt-off
152 		 * latencies should be reset here.
153 		 */
154 		trace_hardirqs_on();
155 		trace_hardirqs_off();
156 		goto again;
157 	}
158 }
159 
160 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_PPC_KUAP)
161 static inline void replay_soft_interrupts_irqrestore(void)
162 {
163 	unsigned long kuap_state = get_kuap();
164 
165 	/*
166 	 * Check if anything calls local_irq_enable/restore() when KUAP is
167 	 * disabled (user access enabled). We handle that case here by saving
168 	 * and re-locking AMR but we shouldn't get here in the first place,
169 	 * hence the warning.
170 	 */
171 	kuap_assert_locked();
172 
173 	if (kuap_state != AMR_KUAP_BLOCKED)
174 		set_kuap(AMR_KUAP_BLOCKED);
175 
176 	replay_soft_interrupts();
177 
178 	if (kuap_state != AMR_KUAP_BLOCKED)
179 		set_kuap(kuap_state);
180 }
181 #else
182 #define replay_soft_interrupts_irqrestore() replay_soft_interrupts()
183 #endif
184 
185 notrace void arch_local_irq_restore(unsigned long mask)
186 {
187 	unsigned char irq_happened;
188 
189 	/* Write the new soft-enabled value if it is a disable */
190 	if (mask) {
191 		irq_soft_mask_set(mask);
192 		return;
193 	}
194 
195 	if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG))
196 		WARN_ON_ONCE(in_nmi() || in_hardirq());
197 
198 	/*
199 	 * After the stb, interrupts are unmasked and there are no interrupts
200 	 * pending replay. The restart sequence makes this atomic with
201 	 * respect to soft-masked interrupts. If this was just a simple code
202 	 * sequence, a soft-masked interrupt could become pending right after
203 	 * the comparison and before the stb.
204 	 *
205 	 * This allows interrupts to be unmasked without hard disabling, and
206 	 * also without new hard interrupts coming in ahead of pending ones.
207 	 */
208 	asm_volatile_goto(
209 "1:					\n"
210 "		lbz	9,%0(13)	\n"
211 "		cmpwi	9,0		\n"
212 "		bne	%l[happened]	\n"
213 "		stb	9,%1(13)	\n"
214 "2:					\n"
215 		RESTART_TABLE(1b, 2b, 1b)
216 	: : "i" (offsetof(struct paca_struct, irq_happened)),
217 	    "i" (offsetof(struct paca_struct, irq_soft_mask))
218 	: "cr0", "r9"
219 	: happened);
220 
221 	if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG))
222 		WARN_ON_ONCE(!(mfmsr() & MSR_EE));
223 
224 	return;
225 
226 happened:
227 	irq_happened = READ_ONCE(local_paca->irq_happened);
228 	if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG))
229 		WARN_ON_ONCE(!irq_happened);
230 
231 	if (irq_happened == PACA_IRQ_HARD_DIS) {
232 		if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG))
233 			WARN_ON_ONCE(mfmsr() & MSR_EE);
234 		irq_soft_mask_set(IRQS_ENABLED);
235 		local_paca->irq_happened = 0;
236 		__hard_irq_enable();
237 		return;
238 	}
239 
240 	/* Have interrupts to replay, need to hard disable first */
241 	if (!(irq_happened & PACA_IRQ_HARD_DIS)) {
242 		if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG)) {
243 			if (!(mfmsr() & MSR_EE)) {
244 				/*
245 				 * An interrupt could have come in and cleared
246 				 * MSR[EE] and set IRQ_HARD_DIS, so check
247 				 * IRQ_HARD_DIS again and warn if it is still
248 				 * clear.
249 				 */
250 				irq_happened = READ_ONCE(local_paca->irq_happened);
251 				WARN_ON_ONCE(!(irq_happened & PACA_IRQ_HARD_DIS));
252 			}
253 		}
254 		__hard_irq_disable();
255 		local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
256 	} else {
257 		if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG)) {
258 			if (WARN_ON_ONCE(mfmsr() & MSR_EE))
259 				__hard_irq_disable();
260 		}
261 	}
262 
263 	/*
264 	 * Disable preempt here, so that the below preempt_enable will
265 	 * perform resched if required (a replayed interrupt may set
266 	 * need_resched).
267 	 */
268 	preempt_disable();
269 	irq_soft_mask_set(IRQS_ALL_DISABLED);
270 	trace_hardirqs_off();
271 
272 	replay_soft_interrupts_irqrestore();
273 	local_paca->irq_happened = 0;
274 
275 	trace_hardirqs_on();
276 	irq_soft_mask_set(IRQS_ENABLED);
277 	__hard_irq_enable();
278 	preempt_enable();
279 }
280 EXPORT_SYMBOL(arch_local_irq_restore);
281 
282 /*
283  * This is a helper to use when about to go into idle low-power
284  * when the latter has the side effect of re-enabling interrupts
285  * (such as calling H_CEDE under pHyp).
286  *
287  * You call this function with interrupts soft-disabled (this is
288  * already the case when ppc_md.power_save is called). The function
289  * will return whether to enter power save or just return.
290  *
291  * In the former case, it will have notified lockdep of interrupts
292  * being re-enabled and generally sanitized the lazy irq state,
293  * and in the latter case it will leave with interrupts hard
294  * disabled and marked as such, so the local_irq_enable() call
295  * in arch_cpu_idle() will properly re-enable everything.
296  */
297 bool prep_irq_for_idle(void)
298 {
299 	/*
300 	 * First we need to hard disable to ensure no interrupt
301 	 * occurs before we effectively enter the low power state
302 	 */
303 	__hard_irq_disable();
304 	local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
305 
306 	/*
307 	 * If anything happened while we were soft-disabled,
308 	 * we return now and do not enter the low power state.
309 	 */
310 	if (lazy_irq_pending())
311 		return false;
312 
313 	/* Tell lockdep we are about to re-enable */
314 	trace_hardirqs_on();
315 
316 	/*
317 	 * Mark interrupts as soft-enabled and clear the
318 	 * PACA_IRQ_HARD_DIS from the pending mask since we
319 	 * are about to hard enable as well as a side effect
320 	 * of entering the low power state.
321 	 */
322 	local_paca->irq_happened &= ~PACA_IRQ_HARD_DIS;
323 	irq_soft_mask_set(IRQS_ENABLED);
324 
325 	/* Tell the caller to enter the low power state */
326 	return true;
327 }
328 
329 #ifdef CONFIG_PPC_BOOK3S
330 /*
331  * This is for idle sequences that return with IRQs off, but the
332  * idle state itself wakes on interrupt. Tell the irq tracer that
333  * IRQs are enabled for the duration of idle so it does not get long
334  * off times. Must be paired with fini_irq_for_idle_irqsoff.
335  */
336 bool prep_irq_for_idle_irqsoff(void)
337 {
338 	WARN_ON(!irqs_disabled());
339 
340 	/*
341 	 * First we need to hard disable to ensure no interrupt
342 	 * occurs before we effectively enter the low power state
343 	 */
344 	__hard_irq_disable();
345 	local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
346 
347 	/*
348 	 * If anything happened while we were soft-disabled,
349 	 * we return now and do not enter the low power state.
350 	 */
351 	if (lazy_irq_pending())
352 		return false;
353 
354 	/* Tell lockdep we are about to re-enable */
355 	trace_hardirqs_on();
356 
357 	return true;
358 }
359 
360 /*
361  * Take the SRR1 wakeup reason, index into this table to find the
362  * appropriate irq_happened bit.
363  *
364  * Sytem reset exceptions taken in idle state also come through here,
365  * but they are NMI interrupts so do not need to wait for IRQs to be
366  * restored, and should be taken as early as practical. These are marked
367  * with 0xff in the table. The Power ISA specifies 0100b as the system
368  * reset interrupt reason.
369  */
370 #define IRQ_SYSTEM_RESET	0xff
371 
372 static const u8 srr1_to_lazyirq[0x10] = {
373 	0, 0, 0,
374 	PACA_IRQ_DBELL,
375 	IRQ_SYSTEM_RESET,
376 	PACA_IRQ_DBELL,
377 	PACA_IRQ_DEC,
378 	0,
379 	PACA_IRQ_EE,
380 	PACA_IRQ_EE,
381 	PACA_IRQ_HMI,
382 	0, 0, 0, 0, 0 };
383 
384 void replay_system_reset(void)
385 {
386 	struct pt_regs regs;
387 
388 	ppc_save_regs(&regs);
389 	regs.trap = 0x100;
390 	get_paca()->in_nmi = 1;
391 	system_reset_exception(&regs);
392 	get_paca()->in_nmi = 0;
393 }
394 EXPORT_SYMBOL_GPL(replay_system_reset);
395 
396 void irq_set_pending_from_srr1(unsigned long srr1)
397 {
398 	unsigned int idx = (srr1 & SRR1_WAKEMASK_P8) >> 18;
399 	u8 reason = srr1_to_lazyirq[idx];
400 
401 	/*
402 	 * Take the system reset now, which is immediately after registers
403 	 * are restored from idle. It's an NMI, so interrupts need not be
404 	 * re-enabled before it is taken.
405 	 */
406 	if (unlikely(reason == IRQ_SYSTEM_RESET)) {
407 		replay_system_reset();
408 		return;
409 	}
410 
411 	if (reason == PACA_IRQ_DBELL) {
412 		/*
413 		 * When doorbell triggers a system reset wakeup, the message
414 		 * is not cleared, so if the doorbell interrupt is replayed
415 		 * and the IPI handled, the doorbell interrupt would still
416 		 * fire when EE is enabled.
417 		 *
418 		 * To avoid taking the superfluous doorbell interrupt,
419 		 * execute a msgclr here before the interrupt is replayed.
420 		 */
421 		ppc_msgclr(PPC_DBELL_MSGTYPE);
422 	}
423 
424 	/*
425 	 * The 0 index (SRR1[42:45]=b0000) must always evaluate to 0,
426 	 * so this can be called unconditionally with the SRR1 wake
427 	 * reason as returned by the idle code, which uses 0 to mean no
428 	 * interrupt.
429 	 *
430 	 * If a future CPU was to designate this as an interrupt reason,
431 	 * then a new index for no interrupt must be assigned.
432 	 */
433 	local_paca->irq_happened |= reason;
434 }
435 #endif /* CONFIG_PPC_BOOK3S */
436 
437 /*
438  * Force a replay of the external interrupt handler on this CPU.
439  */
440 void force_external_irq_replay(void)
441 {
442 	/*
443 	 * This must only be called with interrupts soft-disabled,
444 	 * the replay will happen when re-enabling.
445 	 */
446 	WARN_ON(!arch_irqs_disabled());
447 
448 	/*
449 	 * Interrupts must always be hard disabled before irq_happened is
450 	 * modified (to prevent lost update in case of interrupt between
451 	 * load and store).
452 	 */
453 	__hard_irq_disable();
454 	local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
455 
456 	/* Indicate in the PACA that we have an interrupt to replay */
457 	local_paca->irq_happened |= PACA_IRQ_EE;
458 }
459 
460 static int __init setup_noirqdistrib(char *str)
461 {
462 	distribute_irqs = 0;
463 	return 1;
464 }
465 
466 __setup("noirqdistrib", setup_noirqdistrib);
467