nmi.c 14 KB

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