1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _ASM_POWERPC_INTERRUPT_H
3 #define _ASM_POWERPC_INTERRUPT_H
5 #include <linux/context_tracking.h>
6 #include <linux/hardirq.h>
7 #include <asm/cputime.h>
8 #include <asm/ftrace.h>
9 #include <asm/kprobes.h>
10 #include <asm/runlatch.h>
12 static inline void nap_adjust_return(struct pt_regs *regs)
14 #ifdef CONFIG_PPC_970_NAP
15 if (unlikely(test_thread_local_flags(_TLF_NAPPING))) {
16 /* Can avoid a test-and-clear because NMIs do not call this */
17 clear_thread_local_flags(_TLF_NAPPING);
18 regs->nip = (unsigned long)power4_idle_nap_return;
23 struct interrupt_state {
26 static inline void booke_restore_dbcr0(void)
28 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
29 unsigned long dbcr0 = current->thread.debug.dbcr0;
31 if (IS_ENABLED(CONFIG_PPC32) && unlikely(dbcr0 & DBCR0_IDM)) {
33 mtspr(SPRN_DBCR0, global_dbcr0[smp_processor_id()]);
38 static inline void interrupt_enter_prepare(struct pt_regs *regs, struct interrupt_state *state)
41 if (!arch_irq_disabled_regs(regs))
44 if (user_mode(regs)) {
46 account_cpu_user_entry();
48 kuap_save_and_lock(regs);
53 if (irq_soft_mask_set_return(IRQS_ALL_DISABLED) == IRQS_ENABLED)
55 local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
57 if (user_mode(regs)) {
58 CT_WARN_ON(ct_state() != CONTEXT_USER);
61 account_cpu_user_entry();
62 account_stolen_time();
65 * CT_WARN_ON comes here via program_check_exception,
68 if (TRAP(regs) != 0x700)
69 CT_WARN_ON(ct_state() != CONTEXT_KERNEL);
73 booke_restore_dbcr0();
77 * Care should be taken to note that interrupt_exit_prepare and
78 * interrupt_async_exit_prepare do not necessarily return immediately to
79 * regs context (e.g., if regs is usermode, we don't necessarily return to
80 * user mode). Other interrupts might be taken between here and return,
81 * context switch / preemption may occur in the exit path after this, or a
82 * signal may be delivered, etc.
84 * The real interrupt exit code is platform specific, e.g.,
85 * interrupt_exit_user_prepare / interrupt_exit_kernel_prepare for 64s.
87 * However interrupt_nmi_exit_prepare does return directly to regs, because
88 * NMIs do not do "exit work" or replay soft-masked interrupts.
90 static inline void interrupt_exit_prepare(struct pt_regs *regs, struct interrupt_state *state)
96 static inline void interrupt_async_enter_prepare(struct pt_regs *regs, struct interrupt_state *state)
98 #ifdef CONFIG_PPC_BOOK3S_64
99 if (cpu_has_feature(CPU_FTR_CTRL) &&
100 !test_thread_local_flags(_TLF_RUNLATCH))
101 __ppc64_runlatch_on();
104 interrupt_enter_prepare(regs, state);
108 static inline void interrupt_async_exit_prepare(struct pt_regs *regs, struct interrupt_state *state)
111 * Adjust at exit so the main handler sees the true NIA. This must
112 * come before irq_exit() because irq_exit can enable interrupts, and
113 * if another interrupt is taken before nap_adjust_return has run
114 * here, then that interrupt would return directly to idle nap return.
116 nap_adjust_return(regs);
119 interrupt_exit_prepare(regs, state);
122 struct interrupt_nmi_state {
130 static inline bool nmi_disables_ftrace(struct pt_regs *regs)
132 /* Allow DEC and PMI to be traced when they are soft-NMI */
133 if (IS_ENABLED(CONFIG_PPC_BOOK3S_64)) {
134 if (TRAP(regs) == 0x900)
136 if (TRAP(regs) == 0xf00)
139 if (IS_ENABLED(CONFIG_PPC_BOOK3E)) {
140 if (TRAP(regs) == 0x260)
147 static inline void interrupt_nmi_enter_prepare(struct pt_regs *regs, struct interrupt_nmi_state *state)
150 state->irq_soft_mask = local_paca->irq_soft_mask;
151 state->irq_happened = local_paca->irq_happened;
154 * Set IRQS_ALL_DISABLED unconditionally so irqs_disabled() does
155 * the right thing, and set IRQ_HARD_DIS. We do not want to reconcile
156 * because that goes through irq tracing which we don't want in NMI.
158 local_paca->irq_soft_mask = IRQS_ALL_DISABLED;
159 local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
161 /* Don't do any per-CPU operations until interrupt state is fixed */
163 if (nmi_disables_ftrace(regs)) {
164 state->ftrace_enabled = this_cpu_get_ftrace_enabled();
165 this_cpu_set_ftrace_enabled(0);
170 * Do not use nmi_enter() for pseries hash guest taking a real-mode
171 * NMI because not everything it touches is within the RMA limit.
173 if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64) ||
174 !firmware_has_feature(FW_FEATURE_LPAR) ||
175 radix_enabled() || (mfmsr() & MSR_DR))
179 static inline void interrupt_nmi_exit_prepare(struct pt_regs *regs, struct interrupt_nmi_state *state)
181 if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64) ||
182 !firmware_has_feature(FW_FEATURE_LPAR) ||
183 radix_enabled() || (mfmsr() & MSR_DR))
187 * nmi does not call nap_adjust_return because nmi should not create
188 * new work to do (must use irq_work for that).
192 if (nmi_disables_ftrace(regs))
193 this_cpu_set_ftrace_enabled(state->ftrace_enabled);
195 /* Check we didn't change the pending interrupt mask. */
196 WARN_ON_ONCE((state->irq_happened | PACA_IRQ_HARD_DIS) != local_paca->irq_happened);
197 local_paca->irq_happened = state->irq_happened;
198 local_paca->irq_soft_mask = state->irq_soft_mask;
203 * Don't use noinstr here like x86, but rather add NOKPROBE_SYMBOL to each
204 * function definition. The reason for this is the noinstr section is placed
205 * after the main text section, i.e., very far away from the interrupt entry
206 * asm. That creates problems with fitting linker stubs when building large
209 #define interrupt_handler __visible noinline notrace __no_kcsan __no_sanitize_address
212 * DECLARE_INTERRUPT_HANDLER_RAW - Declare raw interrupt handler function
213 * @func: Function name of the entry point
214 * @returns: Returns a value back to asm caller
216 #define DECLARE_INTERRUPT_HANDLER_RAW(func) \
217 __visible long func(struct pt_regs *regs)
220 * DEFINE_INTERRUPT_HANDLER_RAW - Define raw interrupt handler function
221 * @func: Function name of the entry point
222 * @returns: Returns a value back to asm caller
224 * @func is called from ASM entry code.
226 * This is a plain function which does no tracing, reconciling, etc.
227 * The macro is written so it acts as function definition. Append the
228 * body with a pair of curly brackets.
230 * raw interrupt handlers must not enable or disable interrupts, or
231 * schedule, tracing and instrumentation (ftrace, lockdep, etc) would
232 * not be advisable either, although may be possible in a pinch, the
233 * trace will look odd at least.
235 * A raw handler may call one of the other interrupt handler functions
236 * to be converted into that interrupt context without these restrictions.
238 * On PPC64, _RAW handlers may return with fast_interrupt_return.
240 * Specific handlers may have additional restrictions.
242 #define DEFINE_INTERRUPT_HANDLER_RAW(func) \
243 static __always_inline long ____##func(struct pt_regs *regs); \
245 interrupt_handler long func(struct pt_regs *regs) \
249 ret = ____##func (regs); \
253 NOKPROBE_SYMBOL(func); \
255 static __always_inline long ____##func(struct pt_regs *regs)
258 * DECLARE_INTERRUPT_HANDLER - Declare synchronous interrupt handler function
259 * @func: Function name of the entry point
261 #define DECLARE_INTERRUPT_HANDLER(func) \
262 __visible void func(struct pt_regs *regs)
265 * DEFINE_INTERRUPT_HANDLER - Define synchronous interrupt handler function
266 * @func: Function name of the entry point
268 * @func is called from ASM entry code.
270 * The macro is written so it acts as function definition. Append the
271 * body with a pair of curly brackets.
273 #define DEFINE_INTERRUPT_HANDLER(func) \
274 static __always_inline void ____##func(struct pt_regs *regs); \
276 interrupt_handler void func(struct pt_regs *regs) \
278 struct interrupt_state state; \
280 interrupt_enter_prepare(regs, &state); \
284 interrupt_exit_prepare(regs, &state); \
286 NOKPROBE_SYMBOL(func); \
288 static __always_inline void ____##func(struct pt_regs *regs)
291 * DECLARE_INTERRUPT_HANDLER_RET - Declare synchronous interrupt handler function
292 * @func: Function name of the entry point
293 * @returns: Returns a value back to asm caller
295 #define DECLARE_INTERRUPT_HANDLER_RET(func) \
296 __visible long func(struct pt_regs *regs)
299 * DEFINE_INTERRUPT_HANDLER_RET - Define synchronous interrupt handler function
300 * @func: Function name of the entry point
301 * @returns: Returns a value back to asm caller
303 * @func is called from ASM entry code.
305 * The macro is written so it acts as function definition. Append the
306 * body with a pair of curly brackets.
308 #define DEFINE_INTERRUPT_HANDLER_RET(func) \
309 static __always_inline long ____##func(struct pt_regs *regs); \
311 interrupt_handler long func(struct pt_regs *regs) \
313 struct interrupt_state state; \
316 interrupt_enter_prepare(regs, &state); \
318 ret = ____##func (regs); \
320 interrupt_exit_prepare(regs, &state); \
324 NOKPROBE_SYMBOL(func); \
326 static __always_inline long ____##func(struct pt_regs *regs)
329 * DECLARE_INTERRUPT_HANDLER_ASYNC - Declare asynchronous interrupt handler function
330 * @func: Function name of the entry point
332 #define DECLARE_INTERRUPT_HANDLER_ASYNC(func) \
333 __visible void func(struct pt_regs *regs)
336 * DEFINE_INTERRUPT_HANDLER_ASYNC - Define asynchronous interrupt handler function
337 * @func: Function name of the entry point
339 * @func is called from ASM entry code.
341 * The macro is written so it acts as function definition. Append the
342 * body with a pair of curly brackets.
344 #define DEFINE_INTERRUPT_HANDLER_ASYNC(func) \
345 static __always_inline void ____##func(struct pt_regs *regs); \
347 interrupt_handler void func(struct pt_regs *regs) \
349 struct interrupt_state state; \
351 interrupt_async_enter_prepare(regs, &state); \
355 interrupt_async_exit_prepare(regs, &state); \
357 NOKPROBE_SYMBOL(func); \
359 static __always_inline void ____##func(struct pt_regs *regs)
362 * DECLARE_INTERRUPT_HANDLER_NMI - Declare NMI interrupt handler function
363 * @func: Function name of the entry point
364 * @returns: Returns a value back to asm caller
366 #define DECLARE_INTERRUPT_HANDLER_NMI(func) \
367 __visible long func(struct pt_regs *regs)
370 * DEFINE_INTERRUPT_HANDLER_NMI - Define NMI interrupt handler function
371 * @func: Function name of the entry point
372 * @returns: Returns a value back to asm caller
374 * @func is called from ASM entry code.
376 * The macro is written so it acts as function definition. Append the
377 * body with a pair of curly brackets.
379 #define DEFINE_INTERRUPT_HANDLER_NMI(func) \
380 static __always_inline long ____##func(struct pt_regs *regs); \
382 interrupt_handler long func(struct pt_regs *regs) \
384 struct interrupt_nmi_state state; \
387 interrupt_nmi_enter_prepare(regs, &state); \
389 ret = ____##func (regs); \
391 interrupt_nmi_exit_prepare(regs, &state); \
395 NOKPROBE_SYMBOL(func); \
397 static __always_inline long ____##func(struct pt_regs *regs)
400 /* Interrupt handlers */
402 DECLARE_INTERRUPT_HANDLER_NMI(system_reset_exception);
403 #ifdef CONFIG_PPC_BOOK3S_64
404 DECLARE_INTERRUPT_HANDLER_ASYNC(machine_check_exception);
406 DECLARE_INTERRUPT_HANDLER_NMI(machine_check_exception);
408 DECLARE_INTERRUPT_HANDLER(SMIException);
409 DECLARE_INTERRUPT_HANDLER(handle_hmi_exception);
410 DECLARE_INTERRUPT_HANDLER(unknown_exception);
411 DECLARE_INTERRUPT_HANDLER_ASYNC(unknown_async_exception);
412 DECLARE_INTERRUPT_HANDLER_NMI(unknown_nmi_exception);
413 DECLARE_INTERRUPT_HANDLER(instruction_breakpoint_exception);
414 DECLARE_INTERRUPT_HANDLER(RunModeException);
415 DECLARE_INTERRUPT_HANDLER(single_step_exception);
416 DECLARE_INTERRUPT_HANDLER(program_check_exception);
417 DECLARE_INTERRUPT_HANDLER(emulation_assist_interrupt);
418 DECLARE_INTERRUPT_HANDLER(alignment_exception);
419 DECLARE_INTERRUPT_HANDLER(StackOverflow);
420 DECLARE_INTERRUPT_HANDLER(stack_overflow_exception);
421 DECLARE_INTERRUPT_HANDLER(kernel_fp_unavailable_exception);
422 DECLARE_INTERRUPT_HANDLER(altivec_unavailable_exception);
423 DECLARE_INTERRUPT_HANDLER(vsx_unavailable_exception);
424 DECLARE_INTERRUPT_HANDLER(facility_unavailable_exception);
425 DECLARE_INTERRUPT_HANDLER(fp_unavailable_tm);
426 DECLARE_INTERRUPT_HANDLER(altivec_unavailable_tm);
427 DECLARE_INTERRUPT_HANDLER(vsx_unavailable_tm);
428 DECLARE_INTERRUPT_HANDLER_NMI(performance_monitor_exception_nmi);
429 DECLARE_INTERRUPT_HANDLER_ASYNC(performance_monitor_exception_async);
430 DECLARE_INTERRUPT_HANDLER_RAW(performance_monitor_exception);
431 DECLARE_INTERRUPT_HANDLER(DebugException);
432 DECLARE_INTERRUPT_HANDLER(altivec_assist_exception);
433 DECLARE_INTERRUPT_HANDLER(CacheLockingException);
434 DECLARE_INTERRUPT_HANDLER(SPEFloatingPointException);
435 DECLARE_INTERRUPT_HANDLER(SPEFloatingPointRoundException);
436 DECLARE_INTERRUPT_HANDLER_NMI(WatchdogException);
437 DECLARE_INTERRUPT_HANDLER(kernel_bad_stack);
440 DECLARE_INTERRUPT_HANDLER_RAW(do_slb_fault);
441 DECLARE_INTERRUPT_HANDLER(do_bad_slb_fault);
444 DECLARE_INTERRUPT_HANDLER_RAW(do_hash_fault);
447 DECLARE_INTERRUPT_HANDLER_RET(do_page_fault);
448 DECLARE_INTERRUPT_HANDLER(do_bad_page_fault_segv);
451 DECLARE_INTERRUPT_HANDLER(do_break);
454 DECLARE_INTERRUPT_HANDLER_ASYNC(timer_interrupt);
457 DECLARE_INTERRUPT_HANDLER_NMI(machine_check_early);
458 DECLARE_INTERRUPT_HANDLER_NMI(hmi_exception_realmode);
460 DECLARE_INTERRUPT_HANDLER_ASYNC(TAUException);
462 void __noreturn unrecoverable_exception(struct pt_regs *regs);
464 void replay_system_reset(void);
465 void replay_soft_interrupts(void);
467 static inline void interrupt_cond_local_irq_enable(struct pt_regs *regs)
469 if (!arch_irq_disabled_regs(regs))
473 #endif /* _ASM_POWERPC_INTERRUPT_H */