1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * linux/include/linux/nmi.h
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun #ifndef LINUX_NMI_H
6*4882a593Smuzhiyun #define LINUX_NMI_H
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/sched.h>
9*4882a593Smuzhiyun #include <asm/irq.h>
10*4882a593Smuzhiyun #if defined(CONFIG_HAVE_NMI_WATCHDOG)
11*4882a593Smuzhiyun #include <asm/nmi.h>
12*4882a593Smuzhiyun #endif
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #ifdef CONFIG_LOCKUP_DETECTOR
15*4882a593Smuzhiyun void lockup_detector_init(void);
16*4882a593Smuzhiyun void lockup_detector_soft_poweroff(void);
17*4882a593Smuzhiyun void lockup_detector_cleanup(void);
18*4882a593Smuzhiyun bool is_hardlockup(void);
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun extern int watchdog_user_enabled;
21*4882a593Smuzhiyun extern int nmi_watchdog_user_enabled;
22*4882a593Smuzhiyun extern int soft_watchdog_user_enabled;
23*4882a593Smuzhiyun extern int watchdog_thresh;
24*4882a593Smuzhiyun extern unsigned long watchdog_enabled;
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun extern struct cpumask watchdog_cpumask;
27*4882a593Smuzhiyun extern unsigned long *watchdog_cpumask_bits;
28*4882a593Smuzhiyun #ifdef CONFIG_SMP
29*4882a593Smuzhiyun extern int sysctl_softlockup_all_cpu_backtrace;
30*4882a593Smuzhiyun extern int sysctl_hardlockup_all_cpu_backtrace;
31*4882a593Smuzhiyun #else
32*4882a593Smuzhiyun #define sysctl_softlockup_all_cpu_backtrace 0
33*4882a593Smuzhiyun #define sysctl_hardlockup_all_cpu_backtrace 0
34*4882a593Smuzhiyun #endif /* !CONFIG_SMP */
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #else /* CONFIG_LOCKUP_DETECTOR */
lockup_detector_init(void)37*4882a593Smuzhiyun static inline void lockup_detector_init(void) { }
lockup_detector_soft_poweroff(void)38*4882a593Smuzhiyun static inline void lockup_detector_soft_poweroff(void) { }
lockup_detector_cleanup(void)39*4882a593Smuzhiyun static inline void lockup_detector_cleanup(void) { }
40*4882a593Smuzhiyun #endif /* !CONFIG_LOCKUP_DETECTOR */
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun #ifdef CONFIG_SOFTLOCKUP_DETECTOR
43*4882a593Smuzhiyun extern void touch_softlockup_watchdog_sched(void);
44*4882a593Smuzhiyun extern void touch_softlockup_watchdog(void);
45*4882a593Smuzhiyun extern void touch_softlockup_watchdog_sync(void);
46*4882a593Smuzhiyun extern void touch_all_softlockup_watchdogs(void);
47*4882a593Smuzhiyun extern unsigned int softlockup_panic;
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun extern int lockup_detector_online_cpu(unsigned int cpu);
50*4882a593Smuzhiyun extern int lockup_detector_offline_cpu(unsigned int cpu);
51*4882a593Smuzhiyun #else /* CONFIG_SOFTLOCKUP_DETECTOR */
touch_softlockup_watchdog_sched(void)52*4882a593Smuzhiyun static inline void touch_softlockup_watchdog_sched(void) { }
touch_softlockup_watchdog(void)53*4882a593Smuzhiyun static inline void touch_softlockup_watchdog(void) { }
touch_softlockup_watchdog_sync(void)54*4882a593Smuzhiyun static inline void touch_softlockup_watchdog_sync(void) { }
touch_all_softlockup_watchdogs(void)55*4882a593Smuzhiyun static inline void touch_all_softlockup_watchdogs(void) { }
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun #define lockup_detector_online_cpu NULL
58*4882a593Smuzhiyun #define lockup_detector_offline_cpu NULL
59*4882a593Smuzhiyun #endif /* CONFIG_SOFTLOCKUP_DETECTOR */
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun #ifdef CONFIG_DETECT_HUNG_TASK
62*4882a593Smuzhiyun void reset_hung_task_detector(void);
63*4882a593Smuzhiyun #else
reset_hung_task_detector(void)64*4882a593Smuzhiyun static inline void reset_hung_task_detector(void) { }
65*4882a593Smuzhiyun #endif
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun /*
68*4882a593Smuzhiyun * The run state of the lockup detectors is controlled by the content of the
69*4882a593Smuzhiyun * 'watchdog_enabled' variable. Each lockup detector has its dedicated bit -
70*4882a593Smuzhiyun * bit 0 for the hard lockup detector and bit 1 for the soft lockup detector.
71*4882a593Smuzhiyun *
72*4882a593Smuzhiyun * 'watchdog_user_enabled', 'nmi_watchdog_user_enabled' and
73*4882a593Smuzhiyun * 'soft_watchdog_user_enabled' are variables that are only used as an
74*4882a593Smuzhiyun * 'interface' between the parameters in /proc/sys/kernel and the internal
75*4882a593Smuzhiyun * state bits in 'watchdog_enabled'. The 'watchdog_thresh' variable is
76*4882a593Smuzhiyun * handled differently because its value is not boolean, and the lockup
77*4882a593Smuzhiyun * detectors are 'suspended' while 'watchdog_thresh' is equal zero.
78*4882a593Smuzhiyun */
79*4882a593Smuzhiyun #define NMI_WATCHDOG_ENABLED_BIT 0
80*4882a593Smuzhiyun #define SOFT_WATCHDOG_ENABLED_BIT 1
81*4882a593Smuzhiyun #define NMI_WATCHDOG_ENABLED (1 << NMI_WATCHDOG_ENABLED_BIT)
82*4882a593Smuzhiyun #define SOFT_WATCHDOG_ENABLED (1 << SOFT_WATCHDOG_ENABLED_BIT)
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun #if defined(CONFIG_HARDLOCKUP_DETECTOR)
85*4882a593Smuzhiyun extern void hardlockup_detector_disable(void);
86*4882a593Smuzhiyun extern unsigned int hardlockup_panic;
87*4882a593Smuzhiyun #else
hardlockup_detector_disable(void)88*4882a593Smuzhiyun static inline void hardlockup_detector_disable(void) {}
89*4882a593Smuzhiyun #endif
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun #if defined(CONFIG_HAVE_NMI_WATCHDOG) || defined(CONFIG_HARDLOCKUP_DETECTOR)
92*4882a593Smuzhiyun # define NMI_WATCHDOG_SYSCTL_PERM 0644
93*4882a593Smuzhiyun #else
94*4882a593Smuzhiyun # define NMI_WATCHDOG_SYSCTL_PERM 0444
95*4882a593Smuzhiyun #endif
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun #if defined(CONFIG_HARDLOCKUP_DETECTOR_PERF)
98*4882a593Smuzhiyun extern void arch_touch_nmi_watchdog(void);
99*4882a593Smuzhiyun extern void hardlockup_detector_perf_stop(void);
100*4882a593Smuzhiyun extern void hardlockup_detector_perf_restart(void);
101*4882a593Smuzhiyun extern void hardlockup_detector_perf_disable(void);
102*4882a593Smuzhiyun extern void hardlockup_detector_perf_enable(void);
103*4882a593Smuzhiyun extern void hardlockup_detector_perf_cleanup(void);
104*4882a593Smuzhiyun extern int hardlockup_detector_perf_init(void);
105*4882a593Smuzhiyun #else
hardlockup_detector_perf_stop(void)106*4882a593Smuzhiyun static inline void hardlockup_detector_perf_stop(void) { }
hardlockup_detector_perf_restart(void)107*4882a593Smuzhiyun static inline void hardlockup_detector_perf_restart(void) { }
hardlockup_detector_perf_disable(void)108*4882a593Smuzhiyun static inline void hardlockup_detector_perf_disable(void) { }
hardlockup_detector_perf_enable(void)109*4882a593Smuzhiyun static inline void hardlockup_detector_perf_enable(void) { }
hardlockup_detector_perf_cleanup(void)110*4882a593Smuzhiyun static inline void hardlockup_detector_perf_cleanup(void) { }
111*4882a593Smuzhiyun # if !defined(CONFIG_HAVE_NMI_WATCHDOG)
hardlockup_detector_perf_init(void)112*4882a593Smuzhiyun static inline int hardlockup_detector_perf_init(void) { return -ENODEV; }
arch_touch_nmi_watchdog(void)113*4882a593Smuzhiyun static inline void arch_touch_nmi_watchdog(void) {}
114*4882a593Smuzhiyun # else
hardlockup_detector_perf_init(void)115*4882a593Smuzhiyun static inline int hardlockup_detector_perf_init(void) { return 0; }
116*4882a593Smuzhiyun # endif
117*4882a593Smuzhiyun #endif
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun void watchdog_nmi_stop(void);
120*4882a593Smuzhiyun void watchdog_nmi_start(void);
121*4882a593Smuzhiyun int watchdog_nmi_probe(void);
122*4882a593Smuzhiyun int watchdog_nmi_enable(unsigned int cpu);
123*4882a593Smuzhiyun void watchdog_nmi_disable(unsigned int cpu);
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun void lockup_detector_reconfigure(void);
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /**
128*4882a593Smuzhiyun * touch_nmi_watchdog - restart NMI watchdog timeout.
129*4882a593Smuzhiyun *
130*4882a593Smuzhiyun * If the architecture supports the NMI watchdog, touch_nmi_watchdog()
131*4882a593Smuzhiyun * may be used to reset the timeout - for code which intentionally
132*4882a593Smuzhiyun * disables interrupts for a long time. This call is stateless.
133*4882a593Smuzhiyun */
touch_nmi_watchdog(void)134*4882a593Smuzhiyun static inline void touch_nmi_watchdog(void)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun arch_touch_nmi_watchdog();
137*4882a593Smuzhiyun touch_softlockup_watchdog();
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun /*
141*4882a593Smuzhiyun * Create trigger_all_cpu_backtrace() out of the arch-provided
142*4882a593Smuzhiyun * base function. Return whether such support was available,
143*4882a593Smuzhiyun * to allow calling code to fall back to some other mechanism:
144*4882a593Smuzhiyun */
145*4882a593Smuzhiyun #ifdef arch_trigger_cpumask_backtrace
trigger_all_cpu_backtrace(void)146*4882a593Smuzhiyun static inline bool trigger_all_cpu_backtrace(void)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun arch_trigger_cpumask_backtrace(cpu_online_mask, false);
149*4882a593Smuzhiyun return true;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun
trigger_allbutself_cpu_backtrace(void)152*4882a593Smuzhiyun static inline bool trigger_allbutself_cpu_backtrace(void)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun arch_trigger_cpumask_backtrace(cpu_online_mask, true);
155*4882a593Smuzhiyun return true;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
trigger_cpumask_backtrace(struct cpumask * mask)158*4882a593Smuzhiyun static inline bool trigger_cpumask_backtrace(struct cpumask *mask)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun arch_trigger_cpumask_backtrace(mask, false);
161*4882a593Smuzhiyun return true;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
trigger_single_cpu_backtrace(int cpu)164*4882a593Smuzhiyun static inline bool trigger_single_cpu_backtrace(int cpu)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun arch_trigger_cpumask_backtrace(cpumask_of(cpu), false);
167*4882a593Smuzhiyun return true;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun /* generic implementation */
171*4882a593Smuzhiyun void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,
172*4882a593Smuzhiyun bool exclude_self,
173*4882a593Smuzhiyun void (*raise)(cpumask_t *mask));
174*4882a593Smuzhiyun bool nmi_cpu_backtrace(struct pt_regs *regs);
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun #else
trigger_all_cpu_backtrace(void)177*4882a593Smuzhiyun static inline bool trigger_all_cpu_backtrace(void)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun return false;
180*4882a593Smuzhiyun }
trigger_allbutself_cpu_backtrace(void)181*4882a593Smuzhiyun static inline bool trigger_allbutself_cpu_backtrace(void)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun return false;
184*4882a593Smuzhiyun }
trigger_cpumask_backtrace(struct cpumask * mask)185*4882a593Smuzhiyun static inline bool trigger_cpumask_backtrace(struct cpumask *mask)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun return false;
188*4882a593Smuzhiyun }
trigger_single_cpu_backtrace(int cpu)189*4882a593Smuzhiyun static inline bool trigger_single_cpu_backtrace(int cpu)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun return false;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun #endif
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun #ifdef CONFIG_HARDLOCKUP_DETECTOR_PERF
196*4882a593Smuzhiyun u64 hw_nmi_get_sample_period(int watchdog_thresh);
197*4882a593Smuzhiyun #endif
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun #if defined(CONFIG_HARDLOCKUP_CHECK_TIMESTAMP) && \
200*4882a593Smuzhiyun defined(CONFIG_HARDLOCKUP_DETECTOR)
201*4882a593Smuzhiyun void watchdog_update_hrtimer_threshold(u64 period);
202*4882a593Smuzhiyun #else
watchdog_update_hrtimer_threshold(u64 period)203*4882a593Smuzhiyun static inline void watchdog_update_hrtimer_threshold(u64 period) { }
204*4882a593Smuzhiyun #endif
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun struct ctl_table;
207*4882a593Smuzhiyun int proc_watchdog(struct ctl_table *, int, void *, size_t *, loff_t *);
208*4882a593Smuzhiyun int proc_nmi_watchdog(struct ctl_table *, int , void *, size_t *, loff_t *);
209*4882a593Smuzhiyun int proc_soft_watchdog(struct ctl_table *, int , void *, size_t *, loff_t *);
210*4882a593Smuzhiyun int proc_watchdog_thresh(struct ctl_table *, int , void *, size_t *, loff_t *);
211*4882a593Smuzhiyun int proc_watchdog_cpumask(struct ctl_table *, int, void *, size_t *, loff_t *);
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun #ifdef CONFIG_HAVE_ACPI_APEI_NMI
214*4882a593Smuzhiyun #include <asm/nmi.h>
215*4882a593Smuzhiyun #endif
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun #endif
218