nmi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * linux/arch/x86_64/nmi.c
  3. *
  4. * NMI watchdog support on APIC systems
  5. *
  6. * Started by Ingo Molnar <mingo@redhat.com>
  7. *
  8. * Fixes:
  9. * Mikael Pettersson : AMD K7 support for local APIC NMI watchdog.
  10. * Mikael Pettersson : Power Management for local APIC NMI watchdog.
  11. * Pavel Machek and
  12. * Mikael Pettersson : PM converted to driver model. Disable/enable API.
  13. */
  14. #include <linux/config.h>
  15. #include <linux/mm.h>
  16. #include <linux/irq.h>
  17. #include <linux/delay.h>
  18. #include <linux/bootmem.h>
  19. #include <linux/smp_lock.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/mc146818rtc.h>
  22. #include <linux/kernel_stat.h>
  23. #include <linux/module.h>
  24. #include <linux/sysdev.h>
  25. #include <linux/nmi.h>
  26. #include <linux/sysctl.h>
  27. #include <asm/smp.h>
  28. #include <asm/mtrr.h>
  29. #include <asm/mpspec.h>
  30. #include <asm/nmi.h>
  31. #include <asm/msr.h>
  32. #include <asm/proto.h>
  33. #include <asm/kdebug.h>
  34. /*
  35. * lapic_nmi_owner tracks the ownership of the lapic NMI hardware:
  36. * - it may be reserved by some other driver, or not
  37. * - when not reserved by some other driver, it may be used for
  38. * the NMI watchdog, or not
  39. *
  40. * This is maintained separately from nmi_active because the NMI
  41. * watchdog may also be driven from the I/O APIC timer.
  42. */
  43. static DEFINE_SPINLOCK(lapic_nmi_owner_lock);
  44. static unsigned int lapic_nmi_owner;
  45. #define LAPIC_NMI_WATCHDOG (1<<0)
  46. #define LAPIC_NMI_RESERVED (1<<1)
  47. /* nmi_active:
  48. * +1: the lapic NMI watchdog is active, but can be disabled
  49. * 0: the lapic NMI watchdog has not been set up, and cannot
  50. * be enabled
  51. * -1: the lapic NMI watchdog is disabled, but can be enabled
  52. */
  53. int nmi_active; /* oprofile uses this */
  54. int panic_on_timeout;
  55. unsigned int nmi_watchdog = NMI_DEFAULT;
  56. static unsigned int nmi_hz = HZ;
  57. unsigned int nmi_perfctr_msr; /* the MSR to reset in NMI handler */
  58. /* Note that these events don't tick when the CPU idles. This means
  59. the frequency varies with CPU load. */
  60. #define K7_EVNTSEL_ENABLE (1 << 22)
  61. #define K7_EVNTSEL_INT (1 << 20)
  62. #define K7_EVNTSEL_OS (1 << 17)
  63. #define K7_EVNTSEL_USR (1 << 16)
  64. #define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
  65. #define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
  66. #define P6_EVNTSEL0_ENABLE (1 << 22)
  67. #define P6_EVNTSEL_INT (1 << 20)
  68. #define P6_EVNTSEL_OS (1 << 17)
  69. #define P6_EVNTSEL_USR (1 << 16)
  70. #define P6_EVENT_CPU_CLOCKS_NOT_HALTED 0x79
  71. #define P6_NMI_EVENT P6_EVENT_CPU_CLOCKS_NOT_HALTED
  72. /* Run after command line and cpu_init init, but before all other checks */
  73. void __init nmi_watchdog_default(void)
  74. {
  75. if (nmi_watchdog != NMI_DEFAULT)
  76. return;
  77. /* For some reason the IO APIC watchdog doesn't work on the AMD
  78. 8111 chipset. For now switch to local APIC mode using
  79. perfctr0 there. On Intel CPUs we don't have code to handle
  80. the perfctr and the IO-APIC seems to work, so use that. */
  81. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
  82. nmi_watchdog = NMI_LOCAL_APIC;
  83. printk(KERN_INFO
  84. "Using local APIC NMI watchdog using perfctr0\n");
  85. } else {
  86. printk(KERN_INFO "Using IO APIC NMI watchdog\n");
  87. nmi_watchdog = NMI_IO_APIC;
  88. }
  89. }
  90. /* Why is there no CPUID flag for this? */
  91. static __init int cpu_has_lapic(void)
  92. {
  93. switch (boot_cpu_data.x86_vendor) {
  94. case X86_VENDOR_INTEL:
  95. case X86_VENDOR_AMD:
  96. return boot_cpu_data.x86 >= 6;
  97. /* .... add more cpus here or find a different way to figure this out. */
  98. default:
  99. return 0;
  100. }
  101. }
  102. int __init check_nmi_watchdog (void)
  103. {
  104. int counts[NR_CPUS];
  105. int cpu;
  106. if (nmi_watchdog == NMI_LOCAL_APIC && !cpu_has_lapic()) {
  107. nmi_watchdog = NMI_NONE;
  108. return -1;
  109. }
  110. printk(KERN_INFO "testing NMI watchdog ... ");
  111. for (cpu = 0; cpu < NR_CPUS; cpu++)
  112. counts[cpu] = cpu_pda[cpu].__nmi_count;
  113. local_irq_enable();
  114. mdelay((10*1000)/nmi_hz); // wait 10 ticks
  115. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  116. #ifdef CONFIG_SMP
  117. /* Check cpu_callin_map here because that is set
  118. after the timer is started. */
  119. if (!cpu_isset(cpu, cpu_callin_map))
  120. continue;
  121. #endif
  122. if (cpu_pda[cpu].__nmi_count - counts[cpu] <= 5) {
  123. printk("CPU#%d: NMI appears to be stuck (%d)!\n",
  124. cpu,
  125. cpu_pda[cpu].__nmi_count);
  126. nmi_active = 0;
  127. lapic_nmi_owner &= ~LAPIC_NMI_WATCHDOG;
  128. return -1;
  129. }
  130. }
  131. printk("OK.\n");
  132. /* now that we know it works we can reduce NMI frequency to
  133. something more reasonable; makes a difference in some configs */
  134. if (nmi_watchdog == NMI_LOCAL_APIC)
  135. nmi_hz = 1;
  136. return 0;
  137. }
  138. int __init setup_nmi_watchdog(char *str)
  139. {
  140. int nmi;
  141. if (!strncmp(str,"panic",5)) {
  142. panic_on_timeout = 1;
  143. str = strchr(str, ',');
  144. if (!str)
  145. return 1;
  146. ++str;
  147. }
  148. get_option(&str, &nmi);
  149. if (nmi >= NMI_INVALID)
  150. return 0;
  151. nmi_watchdog = nmi;
  152. return 1;
  153. }
  154. __setup("nmi_watchdog=", setup_nmi_watchdog);
  155. static void disable_lapic_nmi_watchdog(void)
  156. {
  157. if (nmi_active <= 0)
  158. return;
  159. switch (boot_cpu_data.x86_vendor) {
  160. case X86_VENDOR_AMD:
  161. wrmsr(MSR_K7_EVNTSEL0, 0, 0);
  162. break;
  163. case X86_VENDOR_INTEL:
  164. wrmsr(MSR_IA32_EVNTSEL0, 0, 0);
  165. break;
  166. }
  167. nmi_active = -1;
  168. /* tell do_nmi() and others that we're not active any more */
  169. nmi_watchdog = 0;
  170. }
  171. static void enable_lapic_nmi_watchdog(void)
  172. {
  173. if (nmi_active < 0) {
  174. nmi_watchdog = NMI_LOCAL_APIC;
  175. setup_apic_nmi_watchdog();
  176. }
  177. }
  178. int reserve_lapic_nmi(void)
  179. {
  180. unsigned int old_owner;
  181. spin_lock(&lapic_nmi_owner_lock);
  182. old_owner = lapic_nmi_owner;
  183. lapic_nmi_owner |= LAPIC_NMI_RESERVED;
  184. spin_unlock(&lapic_nmi_owner_lock);
  185. if (old_owner & LAPIC_NMI_RESERVED)
  186. return -EBUSY;
  187. if (old_owner & LAPIC_NMI_WATCHDOG)
  188. disable_lapic_nmi_watchdog();
  189. return 0;
  190. }
  191. void release_lapic_nmi(void)
  192. {
  193. unsigned int new_owner;
  194. spin_lock(&lapic_nmi_owner_lock);
  195. new_owner = lapic_nmi_owner & ~LAPIC_NMI_RESERVED;
  196. lapic_nmi_owner = new_owner;
  197. spin_unlock(&lapic_nmi_owner_lock);
  198. if (new_owner & LAPIC_NMI_WATCHDOG)
  199. enable_lapic_nmi_watchdog();
  200. }
  201. void disable_timer_nmi_watchdog(void)
  202. {
  203. if ((nmi_watchdog != NMI_IO_APIC) || (nmi_active <= 0))
  204. return;
  205. disable_irq(0);
  206. unset_nmi_callback();
  207. nmi_active = -1;
  208. nmi_watchdog = NMI_NONE;
  209. }
  210. void enable_timer_nmi_watchdog(void)
  211. {
  212. if (nmi_active < 0) {
  213. nmi_watchdog = NMI_IO_APIC;
  214. touch_nmi_watchdog();
  215. nmi_active = 1;
  216. enable_irq(0);
  217. }
  218. }
  219. #ifdef CONFIG_PM
  220. static int nmi_pm_active; /* nmi_active before suspend */
  221. static int lapic_nmi_suspend(struct sys_device *dev, u32 state)
  222. {
  223. nmi_pm_active = nmi_active;
  224. disable_lapic_nmi_watchdog();
  225. return 0;
  226. }
  227. static int lapic_nmi_resume(struct sys_device *dev)
  228. {
  229. if (nmi_pm_active > 0)
  230. enable_lapic_nmi_watchdog();
  231. return 0;
  232. }
  233. static struct sysdev_class nmi_sysclass = {
  234. set_kset_name("lapic_nmi"),
  235. .resume = lapic_nmi_resume,
  236. .suspend = lapic_nmi_suspend,
  237. };
  238. static struct sys_device device_lapic_nmi = {
  239. .id = 0,
  240. .cls = &nmi_sysclass,
  241. };
  242. static int __init init_lapic_nmi_sysfs(void)
  243. {
  244. int error;
  245. if (nmi_active == 0 || nmi_watchdog != NMI_LOCAL_APIC)
  246. return 0;
  247. error = sysdev_class_register(&nmi_sysclass);
  248. if (!error)
  249. error = sysdev_register(&device_lapic_nmi);
  250. return error;
  251. }
  252. /* must come after the local APIC's device_initcall() */
  253. late_initcall(init_lapic_nmi_sysfs);
  254. #endif /* CONFIG_PM */
  255. /*
  256. * Activate the NMI watchdog via the local APIC.
  257. * Original code written by Keith Owens.
  258. */
  259. static void setup_k7_watchdog(void)
  260. {
  261. int i;
  262. unsigned int evntsel;
  263. /* No check, so can start with slow frequency */
  264. nmi_hz = 1;
  265. /* XXX should check these in EFER */
  266. nmi_perfctr_msr = MSR_K7_PERFCTR0;
  267. for(i = 0; i < 4; ++i) {
  268. /* Simulator may not support it */
  269. if (checking_wrmsrl(MSR_K7_EVNTSEL0+i, 0UL))
  270. return;
  271. wrmsrl(MSR_K7_PERFCTR0+i, 0UL);
  272. }
  273. evntsel = K7_EVNTSEL_INT
  274. | K7_EVNTSEL_OS
  275. | K7_EVNTSEL_USR
  276. | K7_NMI_EVENT;
  277. wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
  278. wrmsrl(MSR_K7_PERFCTR0, -((u64)cpu_khz*1000) / nmi_hz);
  279. apic_write(APIC_LVTPC, APIC_DM_NMI);
  280. evntsel |= K7_EVNTSEL_ENABLE;
  281. wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
  282. }
  283. void setup_apic_nmi_watchdog(void)
  284. {
  285. switch (boot_cpu_data.x86_vendor) {
  286. case X86_VENDOR_AMD:
  287. if (boot_cpu_data.x86 != 15)
  288. return;
  289. if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
  290. return;
  291. setup_k7_watchdog();
  292. break;
  293. default:
  294. return;
  295. }
  296. lapic_nmi_owner = LAPIC_NMI_WATCHDOG;
  297. nmi_active = 1;
  298. }
  299. /*
  300. * the best way to detect whether a CPU has a 'hard lockup' problem
  301. * is to check it's local APIC timer IRQ counts. If they are not
  302. * changing then that CPU has some problem.
  303. *
  304. * as these watchdog NMI IRQs are generated on every CPU, we only
  305. * have to check the current processor.
  306. *
  307. * since NMIs don't listen to _any_ locks, we have to be extremely
  308. * careful not to rely on unsafe variables. The printk might lock
  309. * up though, so we have to break up any console locks first ...
  310. * [when there will be more tty-related locks, break them up
  311. * here too!]
  312. */
  313. static unsigned int
  314. last_irq_sums [NR_CPUS],
  315. alert_counter [NR_CPUS];
  316. void touch_nmi_watchdog (void)
  317. {
  318. int i;
  319. /*
  320. * Just reset the alert counters, (other CPUs might be
  321. * spinning on locks we hold):
  322. */
  323. for (i = 0; i < NR_CPUS; i++)
  324. alert_counter[i] = 0;
  325. }
  326. void nmi_watchdog_tick (struct pt_regs * regs, unsigned reason)
  327. {
  328. int sum, cpu;
  329. cpu = safe_smp_processor_id();
  330. sum = read_pda(apic_timer_irqs);
  331. if (last_irq_sums[cpu] == sum) {
  332. /*
  333. * Ayiee, looks like this CPU is stuck ...
  334. * wait a few IRQs (5 seconds) before doing the oops ...
  335. */
  336. alert_counter[cpu]++;
  337. if (alert_counter[cpu] == 5*nmi_hz) {
  338. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
  339. == NOTIFY_STOP) {
  340. alert_counter[cpu] = 0;
  341. return;
  342. }
  343. die_nmi("NMI Watchdog detected LOCKUP on CPU%d", regs);
  344. }
  345. } else {
  346. last_irq_sums[cpu] = sum;
  347. alert_counter[cpu] = 0;
  348. }
  349. if (nmi_perfctr_msr)
  350. wrmsr(nmi_perfctr_msr, -(cpu_khz/nmi_hz*1000), -1);
  351. }
  352. static int dummy_nmi_callback(struct pt_regs * regs, int cpu)
  353. {
  354. return 0;
  355. }
  356. static nmi_callback_t nmi_callback = dummy_nmi_callback;
  357. asmlinkage void do_nmi(struct pt_regs * regs, long error_code)
  358. {
  359. int cpu = safe_smp_processor_id();
  360. nmi_enter();
  361. add_pda(__nmi_count,1);
  362. if (!nmi_callback(regs, cpu))
  363. default_do_nmi(regs);
  364. nmi_exit();
  365. }
  366. void set_nmi_callback(nmi_callback_t callback)
  367. {
  368. nmi_callback = callback;
  369. }
  370. void unset_nmi_callback(void)
  371. {
  372. nmi_callback = dummy_nmi_callback;
  373. }
  374. #ifdef CONFIG_SYSCTL
  375. static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
  376. {
  377. unsigned char reason = get_nmi_reason();
  378. char buf[64];
  379. if (!(reason & 0xc0)) {
  380. sprintf(buf, "NMI received for unknown reason %02x\n", reason);
  381. die_nmi(buf,regs);
  382. }
  383. return 0;
  384. }
  385. /*
  386. * proc handler for /proc/sys/kernel/unknown_nmi_panic
  387. */
  388. int proc_unknown_nmi_panic(struct ctl_table *table, int write, struct file *file,
  389. void __user *buffer, size_t *length, loff_t *ppos)
  390. {
  391. int old_state;
  392. old_state = unknown_nmi_panic;
  393. proc_dointvec(table, write, file, buffer, length, ppos);
  394. if (!!old_state == !!unknown_nmi_panic)
  395. return 0;
  396. if (unknown_nmi_panic) {
  397. if (reserve_lapic_nmi() < 0) {
  398. unknown_nmi_panic = 0;
  399. return -EBUSY;
  400. } else {
  401. set_nmi_callback(unknown_nmi_panic_callback);
  402. }
  403. } else {
  404. release_lapic_nmi();
  405. unset_nmi_callback();
  406. }
  407. return 0;
  408. }
  409. #endif
  410. EXPORT_SYMBOL(nmi_active);
  411. EXPORT_SYMBOL(nmi_watchdog);
  412. EXPORT_SYMBOL(reserve_lapic_nmi);
  413. EXPORT_SYMBOL(release_lapic_nmi);
  414. EXPORT_SYMBOL(disable_timer_nmi_watchdog);
  415. EXPORT_SYMBOL(enable_timer_nmi_watchdog);
  416. EXPORT_SYMBOL(touch_nmi_watchdog);