nmi.c 14 KB

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