nmi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. * linux/arch/i386/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. * Mikael Pettersson : Pentium 4 support for local APIC NMI watchdog.
  12. * Pavel Machek and
  13. * Mikael Pettersson : PM converted to driver model. Disable/enable API.
  14. */
  15. #include <linux/config.h>
  16. #include <linux/mm.h>
  17. #include <linux/irq.h>
  18. #include <linux/delay.h>
  19. #include <linux/bootmem.h>
  20. #include <linux/smp_lock.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/mc146818rtc.h>
  23. #include <linux/kernel_stat.h>
  24. #include <linux/module.h>
  25. #include <linux/nmi.h>
  26. #include <linux/sysdev.h>
  27. #include <linux/sysctl.h>
  28. #include <asm/smp.h>
  29. #include <asm/div64.h>
  30. #include <asm/nmi.h>
  31. #include "mach_traps.h"
  32. unsigned int nmi_watchdog = NMI_NONE;
  33. extern int unknown_nmi_panic;
  34. static unsigned int nmi_hz = HZ;
  35. static unsigned int nmi_perfctr_msr; /* the MSR to reset in NMI handler */
  36. static unsigned int nmi_p4_cccr_val;
  37. extern void show_registers(struct pt_regs *regs);
  38. /*
  39. * lapic_nmi_owner tracks the ownership of the lapic NMI hardware:
  40. * - it may be reserved by some other driver, or not
  41. * - when not reserved by some other driver, it may be used for
  42. * the NMI watchdog, or not
  43. *
  44. * This is maintained separately from nmi_active because the NMI
  45. * watchdog may also be driven from the I/O APIC timer.
  46. */
  47. static DEFINE_SPINLOCK(lapic_nmi_owner_lock);
  48. static unsigned int lapic_nmi_owner;
  49. #define LAPIC_NMI_WATCHDOG (1<<0)
  50. #define LAPIC_NMI_RESERVED (1<<1)
  51. /* nmi_active:
  52. * +1: the lapic NMI watchdog is active, but can be disabled
  53. * 0: the lapic NMI watchdog has not been set up, and cannot
  54. * be enabled
  55. * -1: the lapic NMI watchdog is disabled, but can be enabled
  56. */
  57. int nmi_active;
  58. #define K7_EVNTSEL_ENABLE (1 << 22)
  59. #define K7_EVNTSEL_INT (1 << 20)
  60. #define K7_EVNTSEL_OS (1 << 17)
  61. #define K7_EVNTSEL_USR (1 << 16)
  62. #define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
  63. #define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
  64. #define P6_EVNTSEL0_ENABLE (1 << 22)
  65. #define P6_EVNTSEL_INT (1 << 20)
  66. #define P6_EVNTSEL_OS (1 << 17)
  67. #define P6_EVNTSEL_USR (1 << 16)
  68. #define P6_EVENT_CPU_CLOCKS_NOT_HALTED 0x79
  69. #define P6_NMI_EVENT P6_EVENT_CPU_CLOCKS_NOT_HALTED
  70. #define MSR_P4_MISC_ENABLE 0x1A0
  71. #define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7)
  72. #define MSR_P4_MISC_ENABLE_PEBS_UNAVAIL (1<<12)
  73. #define MSR_P4_PERFCTR0 0x300
  74. #define MSR_P4_CCCR0 0x360
  75. #define P4_ESCR_EVENT_SELECT(N) ((N)<<25)
  76. #define P4_ESCR_OS (1<<3)
  77. #define P4_ESCR_USR (1<<2)
  78. #define P4_CCCR_OVF_PMI0 (1<<26)
  79. #define P4_CCCR_OVF_PMI1 (1<<27)
  80. #define P4_CCCR_THRESHOLD(N) ((N)<<20)
  81. #define P4_CCCR_COMPLEMENT (1<<19)
  82. #define P4_CCCR_COMPARE (1<<18)
  83. #define P4_CCCR_REQUIRED (3<<16)
  84. #define P4_CCCR_ESCR_SELECT(N) ((N)<<13)
  85. #define P4_CCCR_ENABLE (1<<12)
  86. /* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
  87. CRU_ESCR0 (with any non-null event selector) through a complemented
  88. max threshold. [IA32-Vol3, Section 14.9.9] */
  89. #define MSR_P4_IQ_COUNTER0 0x30C
  90. #define P4_NMI_CRU_ESCR0 (P4_ESCR_EVENT_SELECT(0x3F)|P4_ESCR_OS|P4_ESCR_USR)
  91. #define P4_NMI_IQ_CCCR0 \
  92. (P4_CCCR_OVF_PMI0|P4_CCCR_THRESHOLD(15)|P4_CCCR_COMPLEMENT| \
  93. P4_CCCR_COMPARE|P4_CCCR_REQUIRED|P4_CCCR_ESCR_SELECT(4)|P4_CCCR_ENABLE)
  94. static int __init check_nmi_watchdog(void)
  95. {
  96. unsigned int prev_nmi_count[NR_CPUS];
  97. int cpu;
  98. if (nmi_watchdog == NMI_NONE)
  99. return 0;
  100. printk(KERN_INFO "Testing NMI watchdog ... ");
  101. for (cpu = 0; cpu < NR_CPUS; cpu++)
  102. prev_nmi_count[cpu] = per_cpu(irq_stat, cpu).__nmi_count;
  103. local_irq_enable();
  104. mdelay((10*1000)/nmi_hz); // wait 10 ticks
  105. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  106. #ifdef CONFIG_SMP
  107. /* Check cpu_callin_map here because that is set
  108. after the timer is started. */
  109. if (!cpu_isset(cpu, cpu_callin_map))
  110. continue;
  111. #endif
  112. if (nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
  113. printk("CPU#%d: NMI appears to be stuck!\n", cpu);
  114. nmi_active = 0;
  115. lapic_nmi_owner &= ~LAPIC_NMI_WATCHDOG;
  116. return -1;
  117. }
  118. }
  119. printk("OK.\n");
  120. /* now that we know it works we can reduce NMI frequency to
  121. something more reasonable; makes a difference in some configs */
  122. if (nmi_watchdog == NMI_LOCAL_APIC)
  123. nmi_hz = 1;
  124. return 0;
  125. }
  126. /* This needs to happen later in boot so counters are working */
  127. late_initcall(check_nmi_watchdog);
  128. static int __init setup_nmi_watchdog(char *str)
  129. {
  130. int nmi;
  131. get_option(&str, &nmi);
  132. if (nmi >= NMI_INVALID)
  133. return 0;
  134. if (nmi == NMI_NONE)
  135. nmi_watchdog = nmi;
  136. /*
  137. * If any other x86 CPU has a local APIC, then
  138. * please test the NMI stuff there and send me the
  139. * missing bits. Right now Intel P6/P4 and AMD K7 only.
  140. */
  141. if ((nmi == NMI_LOCAL_APIC) &&
  142. (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
  143. (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15))
  144. nmi_watchdog = nmi;
  145. if ((nmi == NMI_LOCAL_APIC) &&
  146. (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) &&
  147. (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15))
  148. nmi_watchdog = nmi;
  149. /*
  150. * We can enable the IO-APIC watchdog
  151. * unconditionally.
  152. */
  153. if (nmi == NMI_IO_APIC) {
  154. nmi_active = 1;
  155. nmi_watchdog = nmi;
  156. }
  157. return 1;
  158. }
  159. __setup("nmi_watchdog=", setup_nmi_watchdog);
  160. static void disable_lapic_nmi_watchdog(void)
  161. {
  162. if (nmi_active <= 0)
  163. return;
  164. switch (boot_cpu_data.x86_vendor) {
  165. case X86_VENDOR_AMD:
  166. wrmsr(MSR_K7_EVNTSEL0, 0, 0);
  167. break;
  168. case X86_VENDOR_INTEL:
  169. switch (boot_cpu_data.x86) {
  170. case 6:
  171. if (boot_cpu_data.x86_model > 0xd)
  172. break;
  173. wrmsr(MSR_P6_EVNTSEL0, 0, 0);
  174. break;
  175. case 15:
  176. if (boot_cpu_data.x86_model > 0x4)
  177. break;
  178. wrmsr(MSR_P4_IQ_CCCR0, 0, 0);
  179. wrmsr(MSR_P4_CRU_ESCR0, 0, 0);
  180. break;
  181. }
  182. break;
  183. }
  184. nmi_active = -1;
  185. /* tell do_nmi() and others that we're not active any more */
  186. nmi_watchdog = 0;
  187. }
  188. static void enable_lapic_nmi_watchdog(void)
  189. {
  190. if (nmi_active < 0) {
  191. nmi_watchdog = NMI_LOCAL_APIC;
  192. setup_apic_nmi_watchdog();
  193. }
  194. }
  195. int reserve_lapic_nmi(void)
  196. {
  197. unsigned int old_owner;
  198. spin_lock(&lapic_nmi_owner_lock);
  199. old_owner = lapic_nmi_owner;
  200. lapic_nmi_owner |= LAPIC_NMI_RESERVED;
  201. spin_unlock(&lapic_nmi_owner_lock);
  202. if (old_owner & LAPIC_NMI_RESERVED)
  203. return -EBUSY;
  204. if (old_owner & LAPIC_NMI_WATCHDOG)
  205. disable_lapic_nmi_watchdog();
  206. return 0;
  207. }
  208. void release_lapic_nmi(void)
  209. {
  210. unsigned int new_owner;
  211. spin_lock(&lapic_nmi_owner_lock);
  212. new_owner = lapic_nmi_owner & ~LAPIC_NMI_RESERVED;
  213. lapic_nmi_owner = new_owner;
  214. spin_unlock(&lapic_nmi_owner_lock);
  215. if (new_owner & LAPIC_NMI_WATCHDOG)
  216. enable_lapic_nmi_watchdog();
  217. }
  218. void disable_timer_nmi_watchdog(void)
  219. {
  220. if ((nmi_watchdog != NMI_IO_APIC) || (nmi_active <= 0))
  221. return;
  222. unset_nmi_callback();
  223. nmi_active = -1;
  224. nmi_watchdog = NMI_NONE;
  225. }
  226. void enable_timer_nmi_watchdog(void)
  227. {
  228. if (nmi_active < 0) {
  229. nmi_watchdog = NMI_IO_APIC;
  230. touch_nmi_watchdog();
  231. nmi_active = 1;
  232. }
  233. }
  234. #ifdef CONFIG_PM
  235. static int nmi_pm_active; /* nmi_active before suspend */
  236. static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
  237. {
  238. nmi_pm_active = nmi_active;
  239. disable_lapic_nmi_watchdog();
  240. return 0;
  241. }
  242. static int lapic_nmi_resume(struct sys_device *dev)
  243. {
  244. if (nmi_pm_active > 0)
  245. enable_lapic_nmi_watchdog();
  246. return 0;
  247. }
  248. static struct sysdev_class nmi_sysclass = {
  249. set_kset_name("lapic_nmi"),
  250. .resume = lapic_nmi_resume,
  251. .suspend = lapic_nmi_suspend,
  252. };
  253. static struct sys_device device_lapic_nmi = {
  254. .id = 0,
  255. .cls = &nmi_sysclass,
  256. };
  257. static int __init init_lapic_nmi_sysfs(void)
  258. {
  259. int error;
  260. if (nmi_active == 0 || nmi_watchdog != NMI_LOCAL_APIC)
  261. return 0;
  262. error = sysdev_class_register(&nmi_sysclass);
  263. if (!error)
  264. error = sysdev_register(&device_lapic_nmi);
  265. return error;
  266. }
  267. /* must come after the local APIC's device_initcall() */
  268. late_initcall(init_lapic_nmi_sysfs);
  269. #endif /* CONFIG_PM */
  270. /*
  271. * Activate the NMI watchdog via the local APIC.
  272. * Original code written by Keith Owens.
  273. */
  274. static void clear_msr_range(unsigned int base, unsigned int n)
  275. {
  276. unsigned int i;
  277. for(i = 0; i < n; ++i)
  278. wrmsr(base+i, 0, 0);
  279. }
  280. static inline void write_watchdog_counter(const char *descr)
  281. {
  282. u64 count = (u64)cpu_khz * 1000;
  283. do_div(count, nmi_hz);
  284. if(descr)
  285. Dprintk("setting %s to -0x%08Lx\n", descr, count);
  286. wrmsrl(nmi_perfctr_msr, 0 - count);
  287. }
  288. static void setup_k7_watchdog(void)
  289. {
  290. unsigned int evntsel;
  291. nmi_perfctr_msr = MSR_K7_PERFCTR0;
  292. clear_msr_range(MSR_K7_EVNTSEL0, 4);
  293. clear_msr_range(MSR_K7_PERFCTR0, 4);
  294. evntsel = K7_EVNTSEL_INT
  295. | K7_EVNTSEL_OS
  296. | K7_EVNTSEL_USR
  297. | K7_NMI_EVENT;
  298. wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
  299. write_watchdog_counter("K7_PERFCTR0");
  300. apic_write(APIC_LVTPC, APIC_DM_NMI);
  301. evntsel |= K7_EVNTSEL_ENABLE;
  302. wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
  303. }
  304. static void setup_p6_watchdog(void)
  305. {
  306. unsigned int evntsel;
  307. nmi_perfctr_msr = MSR_P6_PERFCTR0;
  308. clear_msr_range(MSR_P6_EVNTSEL0, 2);
  309. clear_msr_range(MSR_P6_PERFCTR0, 2);
  310. evntsel = P6_EVNTSEL_INT
  311. | P6_EVNTSEL_OS
  312. | P6_EVNTSEL_USR
  313. | P6_NMI_EVENT;
  314. wrmsr(MSR_P6_EVNTSEL0, evntsel, 0);
  315. write_watchdog_counter("P6_PERFCTR0");
  316. apic_write(APIC_LVTPC, APIC_DM_NMI);
  317. evntsel |= P6_EVNTSEL0_ENABLE;
  318. wrmsr(MSR_P6_EVNTSEL0, evntsel, 0);
  319. }
  320. static int setup_p4_watchdog(void)
  321. {
  322. unsigned int misc_enable, dummy;
  323. rdmsr(MSR_P4_MISC_ENABLE, misc_enable, dummy);
  324. if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
  325. return 0;
  326. nmi_perfctr_msr = MSR_P4_IQ_COUNTER0;
  327. nmi_p4_cccr_val = P4_NMI_IQ_CCCR0;
  328. #ifdef CONFIG_SMP
  329. if (smp_num_siblings == 2)
  330. nmi_p4_cccr_val |= P4_CCCR_OVF_PMI1;
  331. #endif
  332. if (!(misc_enable & MSR_P4_MISC_ENABLE_PEBS_UNAVAIL))
  333. clear_msr_range(0x3F1, 2);
  334. /* MSR 0x3F0 seems to have a default value of 0xFC00, but current
  335. docs doesn't fully define it, so leave it alone for now. */
  336. if (boot_cpu_data.x86_model >= 0x3) {
  337. /* MSR_P4_IQ_ESCR0/1 (0x3ba/0x3bb) removed */
  338. clear_msr_range(0x3A0, 26);
  339. clear_msr_range(0x3BC, 3);
  340. } else {
  341. clear_msr_range(0x3A0, 31);
  342. }
  343. clear_msr_range(0x3C0, 6);
  344. clear_msr_range(0x3C8, 6);
  345. clear_msr_range(0x3E0, 2);
  346. clear_msr_range(MSR_P4_CCCR0, 18);
  347. clear_msr_range(MSR_P4_PERFCTR0, 18);
  348. wrmsr(MSR_P4_CRU_ESCR0, P4_NMI_CRU_ESCR0, 0);
  349. wrmsr(MSR_P4_IQ_CCCR0, P4_NMI_IQ_CCCR0 & ~P4_CCCR_ENABLE, 0);
  350. write_watchdog_counter("P4_IQ_COUNTER0");
  351. apic_write(APIC_LVTPC, APIC_DM_NMI);
  352. wrmsr(MSR_P4_IQ_CCCR0, nmi_p4_cccr_val, 0);
  353. return 1;
  354. }
  355. void setup_apic_nmi_watchdog (void)
  356. {
  357. switch (boot_cpu_data.x86_vendor) {
  358. case X86_VENDOR_AMD:
  359. if (boot_cpu_data.x86 != 6 && boot_cpu_data.x86 != 15)
  360. return;
  361. setup_k7_watchdog();
  362. break;
  363. case X86_VENDOR_INTEL:
  364. switch (boot_cpu_data.x86) {
  365. case 6:
  366. if (boot_cpu_data.x86_model > 0xd)
  367. return;
  368. setup_p6_watchdog();
  369. break;
  370. case 15:
  371. if (boot_cpu_data.x86_model > 0x4)
  372. return;
  373. if (!setup_p4_watchdog())
  374. return;
  375. break;
  376. default:
  377. return;
  378. }
  379. break;
  380. default:
  381. return;
  382. }
  383. lapic_nmi_owner = LAPIC_NMI_WATCHDOG;
  384. nmi_active = 1;
  385. }
  386. /*
  387. * the best way to detect whether a CPU has a 'hard lockup' problem
  388. * is to check it's local APIC timer IRQ counts. If they are not
  389. * changing then that CPU has some problem.
  390. *
  391. * as these watchdog NMI IRQs are generated on every CPU, we only
  392. * have to check the current processor.
  393. *
  394. * since NMIs don't listen to _any_ locks, we have to be extremely
  395. * careful not to rely on unsafe variables. The printk might lock
  396. * up though, so we have to break up any console locks first ...
  397. * [when there will be more tty-related locks, break them up
  398. * here too!]
  399. */
  400. static unsigned int
  401. last_irq_sums [NR_CPUS],
  402. alert_counter [NR_CPUS];
  403. void touch_nmi_watchdog (void)
  404. {
  405. int i;
  406. /*
  407. * Just reset the alert counters, (other CPUs might be
  408. * spinning on locks we hold):
  409. */
  410. for (i = 0; i < NR_CPUS; i++)
  411. alert_counter[i] = 0;
  412. /*
  413. * Tickle the softlockup detector too:
  414. */
  415. touch_softlockup_watchdog();
  416. }
  417. extern void die_nmi(struct pt_regs *, const char *msg);
  418. void nmi_watchdog_tick (struct pt_regs * regs)
  419. {
  420. /*
  421. * Since current_thread_info()-> is always on the stack, and we
  422. * always switch the stack NMI-atomically, it's safe to use
  423. * smp_processor_id().
  424. */
  425. int sum, cpu = smp_processor_id();
  426. sum = per_cpu(irq_stat, cpu).apic_timer_irqs;
  427. if (last_irq_sums[cpu] == sum) {
  428. /*
  429. * Ayiee, looks like this CPU is stuck ...
  430. * wait a few IRQs (5 seconds) before doing the oops ...
  431. */
  432. alert_counter[cpu]++;
  433. if (alert_counter[cpu] == 5*nmi_hz)
  434. /*
  435. * die_nmi will return ONLY if NOTIFY_STOP happens..
  436. */
  437. die_nmi(regs, "NMI Watchdog detected LOCKUP");
  438. last_irq_sums[cpu] = sum;
  439. alert_counter[cpu] = 0;
  440. }
  441. if (nmi_perfctr_msr) {
  442. if (nmi_perfctr_msr == MSR_P4_IQ_COUNTER0) {
  443. /*
  444. * P4 quirks:
  445. * - An overflown perfctr will assert its interrupt
  446. * until the OVF flag in its CCCR is cleared.
  447. * - LVTPC is masked on interrupt and must be
  448. * unmasked by the LVTPC handler.
  449. */
  450. wrmsr(MSR_P4_IQ_CCCR0, nmi_p4_cccr_val, 0);
  451. apic_write(APIC_LVTPC, APIC_DM_NMI);
  452. }
  453. else if (nmi_perfctr_msr == MSR_P6_PERFCTR0) {
  454. /* Only P6 based Pentium M need to re-unmask
  455. * the apic vector but it doesn't hurt
  456. * other P6 variant */
  457. apic_write(APIC_LVTPC, APIC_DM_NMI);
  458. }
  459. write_watchdog_counter(NULL);
  460. }
  461. }
  462. #ifdef CONFIG_SYSCTL
  463. static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
  464. {
  465. unsigned char reason = get_nmi_reason();
  466. char buf[64];
  467. if (!(reason & 0xc0)) {
  468. sprintf(buf, "NMI received for unknown reason %02x\n", reason);
  469. die_nmi(regs, buf);
  470. }
  471. return 0;
  472. }
  473. /*
  474. * proc handler for /proc/sys/kernel/unknown_nmi_panic
  475. */
  476. int proc_unknown_nmi_panic(ctl_table *table, int write, struct file *file,
  477. void __user *buffer, size_t *length, loff_t *ppos)
  478. {
  479. int old_state;
  480. old_state = unknown_nmi_panic;
  481. proc_dointvec(table, write, file, buffer, length, ppos);
  482. if (!!old_state == !!unknown_nmi_panic)
  483. return 0;
  484. if (unknown_nmi_panic) {
  485. if (reserve_lapic_nmi() < 0) {
  486. unknown_nmi_panic = 0;
  487. return -EBUSY;
  488. } else {
  489. set_nmi_callback(unknown_nmi_panic_callback);
  490. }
  491. } else {
  492. release_lapic_nmi();
  493. unset_nmi_callback();
  494. }
  495. return 0;
  496. }
  497. #endif
  498. EXPORT_SYMBOL(nmi_active);
  499. EXPORT_SYMBOL(nmi_watchdog);
  500. EXPORT_SYMBOL(reserve_lapic_nmi);
  501. EXPORT_SYMBOL(release_lapic_nmi);
  502. EXPORT_SYMBOL(disable_timer_nmi_watchdog);
  503. EXPORT_SYMBOL(enable_timer_nmi_watchdog);