nmi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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/delay.h>
  17. #include <linux/bootmem.h>
  18. #include <linux/smp_lock.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/mc146818rtc.h>
  21. #include <linux/kernel_stat.h>
  22. #include <linux/module.h>
  23. #include <linux/sysdev.h>
  24. #include <linux/nmi.h>
  25. #include <linux/sysctl.h>
  26. #include <asm/smp.h>
  27. #include <asm/mtrr.h>
  28. #include <asm/mpspec.h>
  29. #include <asm/nmi.h>
  30. #include <asm/msr.h>
  31. #include <asm/proto.h>
  32. #include <asm/kdebug.h>
  33. #include <asm/local.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. static unsigned int nmi_perfctr_msr; /* the MSR to reset in NMI handler */
  58. static unsigned int nmi_p4_cccr_val;
  59. /* Note that these events don't tick when the CPU idles. This means
  60. the frequency varies with CPU load. */
  61. #define K7_EVNTSEL_ENABLE (1 << 22)
  62. #define K7_EVNTSEL_INT (1 << 20)
  63. #define K7_EVNTSEL_OS (1 << 17)
  64. #define K7_EVNTSEL_USR (1 << 16)
  65. #define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
  66. #define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
  67. #define MSR_P4_MISC_ENABLE 0x1A0
  68. #define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7)
  69. #define MSR_P4_MISC_ENABLE_PEBS_UNAVAIL (1<<12)
  70. #define MSR_P4_PERFCTR0 0x300
  71. #define MSR_P4_CCCR0 0x360
  72. #define P4_ESCR_EVENT_SELECT(N) ((N)<<25)
  73. #define P4_ESCR_OS (1<<3)
  74. #define P4_ESCR_USR (1<<2)
  75. #define P4_CCCR_OVF_PMI0 (1<<26)
  76. #define P4_CCCR_OVF_PMI1 (1<<27)
  77. #define P4_CCCR_THRESHOLD(N) ((N)<<20)
  78. #define P4_CCCR_COMPLEMENT (1<<19)
  79. #define P4_CCCR_COMPARE (1<<18)
  80. #define P4_CCCR_REQUIRED (3<<16)
  81. #define P4_CCCR_ESCR_SELECT(N) ((N)<<13)
  82. #define P4_CCCR_ENABLE (1<<12)
  83. /* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
  84. CRU_ESCR0 (with any non-null event selector) through a complemented
  85. max threshold. [IA32-Vol3, Section 14.9.9] */
  86. #define MSR_P4_IQ_COUNTER0 0x30C
  87. #define P4_NMI_CRU_ESCR0 (P4_ESCR_EVENT_SELECT(0x3F)|P4_ESCR_OS|P4_ESCR_USR)
  88. #define P4_NMI_IQ_CCCR0 \
  89. (P4_CCCR_OVF_PMI0|P4_CCCR_THRESHOLD(15)|P4_CCCR_COMPLEMENT| \
  90. P4_CCCR_COMPARE|P4_CCCR_REQUIRED|P4_CCCR_ESCR_SELECT(4)|P4_CCCR_ENABLE)
  91. static __cpuinit inline int nmi_known_cpu(void)
  92. {
  93. switch (boot_cpu_data.x86_vendor) {
  94. case X86_VENDOR_AMD:
  95. return boot_cpu_data.x86 == 15;
  96. case X86_VENDOR_INTEL:
  97. return boot_cpu_data.x86 == 15;
  98. }
  99. return 0;
  100. }
  101. /* Run after command line and cpu_init init, but before all other checks */
  102. void __cpuinit nmi_watchdog_default(void)
  103. {
  104. if (nmi_watchdog != NMI_DEFAULT)
  105. return;
  106. if (nmi_known_cpu())
  107. nmi_watchdog = NMI_LOCAL_APIC;
  108. else
  109. nmi_watchdog = NMI_IO_APIC;
  110. }
  111. #ifdef CONFIG_SMP
  112. /* The performance counters used by NMI_LOCAL_APIC don't trigger when
  113. * the CPU is idle. To make sure the NMI watchdog really ticks on all
  114. * CPUs during the test make them busy.
  115. */
  116. static __init void nmi_cpu_busy(void *data)
  117. {
  118. volatile int *endflag = data;
  119. local_irq_enable();
  120. /* Intentionally don't use cpu_relax here. This is
  121. to make sure that the performance counter really ticks,
  122. even if there is a simulator or similar that catches the
  123. pause instruction. On a real HT machine this is fine because
  124. all other CPUs are busy with "useless" delay loops and don't
  125. care if they get somewhat less cycles. */
  126. while (*endflag == 0)
  127. barrier();
  128. }
  129. #endif
  130. int __init check_nmi_watchdog (void)
  131. {
  132. volatile int endflag = 0;
  133. int *counts;
  134. int cpu;
  135. counts = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
  136. if (!counts)
  137. return -1;
  138. printk(KERN_INFO "testing NMI watchdog ... ");
  139. #ifdef CONFIG_SMP
  140. if (nmi_watchdog == NMI_LOCAL_APIC)
  141. smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
  142. #endif
  143. for (cpu = 0; cpu < NR_CPUS; cpu++)
  144. counts[cpu] = cpu_pda(cpu)->__nmi_count;
  145. local_irq_enable();
  146. mdelay((10*1000)/nmi_hz); // wait 10 ticks
  147. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  148. if (!cpu_online(cpu))
  149. continue;
  150. if (cpu_pda(cpu)->__nmi_count - counts[cpu] <= 5) {
  151. endflag = 1;
  152. printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n",
  153. cpu,
  154. counts[cpu],
  155. cpu_pda(cpu)->__nmi_count);
  156. nmi_active = 0;
  157. lapic_nmi_owner &= ~LAPIC_NMI_WATCHDOG;
  158. nmi_perfctr_msr = 0;
  159. kfree(counts);
  160. return -1;
  161. }
  162. }
  163. endflag = 1;
  164. printk("OK.\n");
  165. /* now that we know it works we can reduce NMI frequency to
  166. something more reasonable; makes a difference in some configs */
  167. if (nmi_watchdog == NMI_LOCAL_APIC)
  168. nmi_hz = 1;
  169. kfree(counts);
  170. return 0;
  171. }
  172. int __init setup_nmi_watchdog(char *str)
  173. {
  174. int nmi;
  175. if (!strncmp(str,"panic",5)) {
  176. panic_on_timeout = 1;
  177. str = strchr(str, ',');
  178. if (!str)
  179. return 1;
  180. ++str;
  181. }
  182. get_option(&str, &nmi);
  183. if (nmi >= NMI_INVALID)
  184. return 0;
  185. nmi_watchdog = nmi;
  186. return 1;
  187. }
  188. __setup("nmi_watchdog=", setup_nmi_watchdog);
  189. static void disable_lapic_nmi_watchdog(void)
  190. {
  191. if (nmi_active <= 0)
  192. return;
  193. switch (boot_cpu_data.x86_vendor) {
  194. case X86_VENDOR_AMD:
  195. wrmsr(MSR_K7_EVNTSEL0, 0, 0);
  196. break;
  197. case X86_VENDOR_INTEL:
  198. if (boot_cpu_data.x86 == 15) {
  199. wrmsr(MSR_P4_IQ_CCCR0, 0, 0);
  200. wrmsr(MSR_P4_CRU_ESCR0, 0, 0);
  201. }
  202. break;
  203. }
  204. nmi_active = -1;
  205. /* tell do_nmi() and others that we're not active any more */
  206. nmi_watchdog = 0;
  207. }
  208. static void enable_lapic_nmi_watchdog(void)
  209. {
  210. if (nmi_active < 0) {
  211. nmi_watchdog = NMI_LOCAL_APIC;
  212. setup_apic_nmi_watchdog();
  213. }
  214. }
  215. int reserve_lapic_nmi(void)
  216. {
  217. unsigned int old_owner;
  218. spin_lock(&lapic_nmi_owner_lock);
  219. old_owner = lapic_nmi_owner;
  220. lapic_nmi_owner |= LAPIC_NMI_RESERVED;
  221. spin_unlock(&lapic_nmi_owner_lock);
  222. if (old_owner & LAPIC_NMI_RESERVED)
  223. return -EBUSY;
  224. if (old_owner & LAPIC_NMI_WATCHDOG)
  225. disable_lapic_nmi_watchdog();
  226. return 0;
  227. }
  228. void release_lapic_nmi(void)
  229. {
  230. unsigned int new_owner;
  231. spin_lock(&lapic_nmi_owner_lock);
  232. new_owner = lapic_nmi_owner & ~LAPIC_NMI_RESERVED;
  233. lapic_nmi_owner = new_owner;
  234. spin_unlock(&lapic_nmi_owner_lock);
  235. if (new_owner & LAPIC_NMI_WATCHDOG)
  236. enable_lapic_nmi_watchdog();
  237. }
  238. void disable_timer_nmi_watchdog(void)
  239. {
  240. if ((nmi_watchdog != NMI_IO_APIC) || (nmi_active <= 0))
  241. return;
  242. disable_irq(0);
  243. unset_nmi_callback();
  244. nmi_active = -1;
  245. nmi_watchdog = NMI_NONE;
  246. }
  247. void enable_timer_nmi_watchdog(void)
  248. {
  249. if (nmi_active < 0) {
  250. nmi_watchdog = NMI_IO_APIC;
  251. touch_nmi_watchdog();
  252. nmi_active = 1;
  253. enable_irq(0);
  254. }
  255. }
  256. #ifdef CONFIG_PM
  257. static int nmi_pm_active; /* nmi_active before suspend */
  258. static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
  259. {
  260. nmi_pm_active = nmi_active;
  261. disable_lapic_nmi_watchdog();
  262. return 0;
  263. }
  264. static int lapic_nmi_resume(struct sys_device *dev)
  265. {
  266. if (nmi_pm_active > 0)
  267. enable_lapic_nmi_watchdog();
  268. return 0;
  269. }
  270. static struct sysdev_class nmi_sysclass = {
  271. set_kset_name("lapic_nmi"),
  272. .resume = lapic_nmi_resume,
  273. .suspend = lapic_nmi_suspend,
  274. };
  275. static struct sys_device device_lapic_nmi = {
  276. .id = 0,
  277. .cls = &nmi_sysclass,
  278. };
  279. static int __init init_lapic_nmi_sysfs(void)
  280. {
  281. int error;
  282. if (nmi_active == 0 || nmi_watchdog != NMI_LOCAL_APIC)
  283. return 0;
  284. error = sysdev_class_register(&nmi_sysclass);
  285. if (!error)
  286. error = sysdev_register(&device_lapic_nmi);
  287. return error;
  288. }
  289. /* must come after the local APIC's device_initcall() */
  290. late_initcall(init_lapic_nmi_sysfs);
  291. #endif /* CONFIG_PM */
  292. /*
  293. * Activate the NMI watchdog via the local APIC.
  294. * Original code written by Keith Owens.
  295. */
  296. static void clear_msr_range(unsigned int base, unsigned int n)
  297. {
  298. unsigned int i;
  299. for(i = 0; i < n; ++i)
  300. wrmsr(base+i, 0, 0);
  301. }
  302. static void setup_k7_watchdog(void)
  303. {
  304. int i;
  305. unsigned int evntsel;
  306. nmi_perfctr_msr = MSR_K7_PERFCTR0;
  307. for(i = 0; i < 4; ++i) {
  308. /* Simulator may not support it */
  309. if (checking_wrmsrl(MSR_K7_EVNTSEL0+i, 0UL)) {
  310. nmi_perfctr_msr = 0;
  311. return;
  312. }
  313. wrmsrl(MSR_K7_PERFCTR0+i, 0UL);
  314. }
  315. evntsel = K7_EVNTSEL_INT
  316. | K7_EVNTSEL_OS
  317. | K7_EVNTSEL_USR
  318. | K7_NMI_EVENT;
  319. wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
  320. wrmsrl(MSR_K7_PERFCTR0, -((u64)cpu_khz * 1000 / nmi_hz));
  321. apic_write(APIC_LVTPC, APIC_DM_NMI);
  322. evntsel |= K7_EVNTSEL_ENABLE;
  323. wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
  324. }
  325. static int setup_p4_watchdog(void)
  326. {
  327. unsigned int misc_enable, dummy;
  328. rdmsr(MSR_P4_MISC_ENABLE, misc_enable, dummy);
  329. if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
  330. return 0;
  331. nmi_perfctr_msr = MSR_P4_IQ_COUNTER0;
  332. nmi_p4_cccr_val = P4_NMI_IQ_CCCR0;
  333. #ifdef CONFIG_SMP
  334. if (smp_num_siblings == 2)
  335. nmi_p4_cccr_val |= P4_CCCR_OVF_PMI1;
  336. #endif
  337. if (!(misc_enable & MSR_P4_MISC_ENABLE_PEBS_UNAVAIL))
  338. clear_msr_range(0x3F1, 2);
  339. /* MSR 0x3F0 seems to have a default value of 0xFC00, but current
  340. docs doesn't fully define it, so leave it alone for now. */
  341. if (boot_cpu_data.x86_model >= 0x3) {
  342. /* MSR_P4_IQ_ESCR0/1 (0x3ba/0x3bb) removed */
  343. clear_msr_range(0x3A0, 26);
  344. clear_msr_range(0x3BC, 3);
  345. } else {
  346. clear_msr_range(0x3A0, 31);
  347. }
  348. clear_msr_range(0x3C0, 6);
  349. clear_msr_range(0x3C8, 6);
  350. clear_msr_range(0x3E0, 2);
  351. clear_msr_range(MSR_P4_CCCR0, 18);
  352. clear_msr_range(MSR_P4_PERFCTR0, 18);
  353. wrmsr(MSR_P4_CRU_ESCR0, P4_NMI_CRU_ESCR0, 0);
  354. wrmsr(MSR_P4_IQ_CCCR0, P4_NMI_IQ_CCCR0 & ~P4_CCCR_ENABLE, 0);
  355. Dprintk("setting P4_IQ_COUNTER0 to 0x%08lx\n", -(cpu_khz * 1000UL / nmi_hz));
  356. wrmsrl(MSR_P4_IQ_COUNTER0, -((u64)cpu_khz * 1000 / nmi_hz));
  357. apic_write(APIC_LVTPC, APIC_DM_NMI);
  358. wrmsr(MSR_P4_IQ_CCCR0, nmi_p4_cccr_val, 0);
  359. return 1;
  360. }
  361. void setup_apic_nmi_watchdog(void)
  362. {
  363. switch (boot_cpu_data.x86_vendor) {
  364. case X86_VENDOR_AMD:
  365. if (boot_cpu_data.x86 != 15)
  366. return;
  367. if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
  368. return;
  369. setup_k7_watchdog();
  370. break;
  371. case X86_VENDOR_INTEL:
  372. if (boot_cpu_data.x86 != 15)
  373. return;
  374. if (!setup_p4_watchdog())
  375. return;
  376. break;
  377. default:
  378. return;
  379. }
  380. lapic_nmi_owner = LAPIC_NMI_WATCHDOG;
  381. nmi_active = 1;
  382. }
  383. /*
  384. * the best way to detect whether a CPU has a 'hard lockup' problem
  385. * is to check it's local APIC timer IRQ counts. If they are not
  386. * changing then that CPU has some problem.
  387. *
  388. * as these watchdog NMI IRQs are generated on every CPU, we only
  389. * have to check the current processor.
  390. */
  391. static DEFINE_PER_CPU(unsigned, last_irq_sum);
  392. static DEFINE_PER_CPU(local_t, alert_counter);
  393. static DEFINE_PER_CPU(int, nmi_touch);
  394. void touch_nmi_watchdog (void)
  395. {
  396. int i;
  397. /*
  398. * Tell other CPUs to reset their alert counters. We cannot
  399. * do it ourselves because the alert count increase is not
  400. * atomic.
  401. */
  402. for (i = 0; i < NR_CPUS; i++)
  403. per_cpu(nmi_touch, i) = 1;
  404. touch_softlockup_watchdog();
  405. }
  406. void nmi_watchdog_tick (struct pt_regs * regs, unsigned reason)
  407. {
  408. int sum;
  409. int touched = 0;
  410. sum = read_pda(apic_timer_irqs);
  411. if (__get_cpu_var(nmi_touch)) {
  412. __get_cpu_var(nmi_touch) = 0;
  413. touched = 1;
  414. }
  415. if (!touched && __get_cpu_var(last_irq_sum) == sum) {
  416. /*
  417. * Ayiee, looks like this CPU is stuck ...
  418. * wait a few IRQs (5 seconds) before doing the oops ...
  419. */
  420. local_inc(&__get_cpu_var(alert_counter));
  421. if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz) {
  422. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
  423. == NOTIFY_STOP) {
  424. local_set(&__get_cpu_var(alert_counter), 0);
  425. return;
  426. }
  427. die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs);
  428. }
  429. } else {
  430. __get_cpu_var(last_irq_sum) = sum;
  431. local_set(&__get_cpu_var(alert_counter), 0);
  432. }
  433. if (nmi_perfctr_msr) {
  434. if (nmi_perfctr_msr == MSR_P4_IQ_COUNTER0) {
  435. /*
  436. * P4 quirks:
  437. * - An overflown perfctr will assert its interrupt
  438. * until the OVF flag in its CCCR is cleared.
  439. * - LVTPC is masked on interrupt and must be
  440. * unmasked by the LVTPC handler.
  441. */
  442. wrmsr(MSR_P4_IQ_CCCR0, nmi_p4_cccr_val, 0);
  443. apic_write(APIC_LVTPC, APIC_DM_NMI);
  444. }
  445. wrmsrl(nmi_perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
  446. }
  447. }
  448. static int dummy_nmi_callback(struct pt_regs * regs, int cpu)
  449. {
  450. return 0;
  451. }
  452. static nmi_callback_t nmi_callback = dummy_nmi_callback;
  453. asmlinkage void do_nmi(struct pt_regs * regs, long error_code)
  454. {
  455. int cpu = safe_smp_processor_id();
  456. nmi_enter();
  457. add_pda(__nmi_count,1);
  458. if (!rcu_dereference(nmi_callback)(regs, cpu))
  459. default_do_nmi(regs);
  460. nmi_exit();
  461. }
  462. void set_nmi_callback(nmi_callback_t callback)
  463. {
  464. rcu_assign_pointer(nmi_callback, callback);
  465. }
  466. void unset_nmi_callback(void)
  467. {
  468. nmi_callback = dummy_nmi_callback;
  469. }
  470. #ifdef CONFIG_SYSCTL
  471. static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
  472. {
  473. unsigned char reason = get_nmi_reason();
  474. char buf[64];
  475. if (!(reason & 0xc0)) {
  476. sprintf(buf, "NMI received for unknown reason %02x\n", reason);
  477. die_nmi(buf,regs);
  478. }
  479. return 0;
  480. }
  481. /*
  482. * proc handler for /proc/sys/kernel/unknown_nmi_panic
  483. */
  484. int proc_unknown_nmi_panic(struct ctl_table *table, int write, struct file *file,
  485. void __user *buffer, size_t *length, loff_t *ppos)
  486. {
  487. int old_state;
  488. old_state = unknown_nmi_panic;
  489. proc_dointvec(table, write, file, buffer, length, ppos);
  490. if (!!old_state == !!unknown_nmi_panic)
  491. return 0;
  492. if (unknown_nmi_panic) {
  493. if (reserve_lapic_nmi() < 0) {
  494. unknown_nmi_panic = 0;
  495. return -EBUSY;
  496. } else {
  497. set_nmi_callback(unknown_nmi_panic_callback);
  498. }
  499. } else {
  500. release_lapic_nmi();
  501. unset_nmi_callback();
  502. }
  503. return 0;
  504. }
  505. #endif
  506. EXPORT_SYMBOL(nmi_active);
  507. EXPORT_SYMBOL(nmi_watchdog);
  508. EXPORT_SYMBOL(reserve_lapic_nmi);
  509. EXPORT_SYMBOL(release_lapic_nmi);
  510. EXPORT_SYMBOL(disable_timer_nmi_watchdog);
  511. EXPORT_SYMBOL(enable_timer_nmi_watchdog);
  512. EXPORT_SYMBOL(touch_nmi_watchdog);