nmi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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/delay.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/module.h>
  19. #include <linux/nmi.h>
  20. #include <linux/sysdev.h>
  21. #include <linux/sysctl.h>
  22. #include <linux/percpu.h>
  23. #include <asm/smp.h>
  24. #include <asm/nmi.h>
  25. #include "mach_traps.h"
  26. unsigned int nmi_watchdog = NMI_NONE;
  27. extern int unknown_nmi_panic;
  28. static unsigned int nmi_hz = HZ;
  29. static unsigned int nmi_perfctr_msr; /* the MSR to reset in NMI handler */
  30. static unsigned int nmi_p4_cccr_val;
  31. extern void show_registers(struct pt_regs *regs);
  32. /* perfctr_nmi_owner tracks the ownership of the perfctr registers:
  33. * evtsel_nmi_owner tracks the ownership of the event selection
  34. * - different performance counters/ event selection may be reserved for
  35. * different subsystems this reservation system just tries to coordinate
  36. * things a little
  37. */
  38. static DEFINE_PER_CPU(unsigned long, perfctr_nmi_owner);
  39. static DEFINE_PER_CPU(unsigned long, evntsel_nmi_owner[3]);
  40. /* this number is calculated from Intel's MSR_P4_CRU_ESCR5 register and it's
  41. * offset from MSR_P4_BSU_ESCR0. It will be the max for all platforms (for now)
  42. */
  43. #define NMI_MAX_COUNTER_BITS 66
  44. /*
  45. * lapic_nmi_owner tracks the ownership of the lapic NMI hardware:
  46. * - it may be reserved by some other driver, or not
  47. * - when not reserved by some other driver, it may be used for
  48. * the NMI watchdog, or not
  49. *
  50. * This is maintained separately from nmi_active because the NMI
  51. * watchdog may also be driven from the I/O APIC timer.
  52. */
  53. static DEFINE_SPINLOCK(lapic_nmi_owner_lock);
  54. static unsigned int lapic_nmi_owner;
  55. #define LAPIC_NMI_WATCHDOG (1<<0)
  56. #define LAPIC_NMI_RESERVED (1<<1)
  57. /* nmi_active:
  58. * +1: the lapic NMI watchdog is active, but can be disabled
  59. * 0: the lapic NMI watchdog has not been set up, and cannot
  60. * be enabled
  61. * -1: the lapic NMI watchdog is disabled, but can be enabled
  62. */
  63. int nmi_active;
  64. #define K7_EVNTSEL_ENABLE (1 << 22)
  65. #define K7_EVNTSEL_INT (1 << 20)
  66. #define K7_EVNTSEL_OS (1 << 17)
  67. #define K7_EVNTSEL_USR (1 << 16)
  68. #define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
  69. #define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
  70. #define P6_EVNTSEL0_ENABLE (1 << 22)
  71. #define P6_EVNTSEL_INT (1 << 20)
  72. #define P6_EVNTSEL_OS (1 << 17)
  73. #define P6_EVNTSEL_USR (1 << 16)
  74. #define P6_EVENT_CPU_CLOCKS_NOT_HALTED 0x79
  75. #define P6_NMI_EVENT P6_EVENT_CPU_CLOCKS_NOT_HALTED
  76. #define MSR_P4_MISC_ENABLE 0x1A0
  77. #define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7)
  78. #define MSR_P4_MISC_ENABLE_PEBS_UNAVAIL (1<<12)
  79. #define MSR_P4_PERFCTR0 0x300
  80. #define MSR_P4_CCCR0 0x360
  81. #define P4_ESCR_EVENT_SELECT(N) ((N)<<25)
  82. #define P4_ESCR_OS (1<<3)
  83. #define P4_ESCR_USR (1<<2)
  84. #define P4_CCCR_OVF_PMI0 (1<<26)
  85. #define P4_CCCR_OVF_PMI1 (1<<27)
  86. #define P4_CCCR_THRESHOLD(N) ((N)<<20)
  87. #define P4_CCCR_COMPLEMENT (1<<19)
  88. #define P4_CCCR_COMPARE (1<<18)
  89. #define P4_CCCR_REQUIRED (3<<16)
  90. #define P4_CCCR_ESCR_SELECT(N) ((N)<<13)
  91. #define P4_CCCR_ENABLE (1<<12)
  92. /* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
  93. CRU_ESCR0 (with any non-null event selector) through a complemented
  94. max threshold. [IA32-Vol3, Section 14.9.9] */
  95. #define MSR_P4_IQ_COUNTER0 0x30C
  96. #define P4_NMI_CRU_ESCR0 (P4_ESCR_EVENT_SELECT(0x3F)|P4_ESCR_OS|P4_ESCR_USR)
  97. #define P4_NMI_IQ_CCCR0 \
  98. (P4_CCCR_OVF_PMI0|P4_CCCR_THRESHOLD(15)|P4_CCCR_COMPLEMENT| \
  99. P4_CCCR_COMPARE|P4_CCCR_REQUIRED|P4_CCCR_ESCR_SELECT(4)|P4_CCCR_ENABLE)
  100. /* converts an msr to an appropriate reservation bit */
  101. static inline unsigned int nmi_perfctr_msr_to_bit(unsigned int msr)
  102. {
  103. /* returns the bit offset of the performance counter register */
  104. switch (boot_cpu_data.x86_vendor) {
  105. case X86_VENDOR_AMD:
  106. return (msr - MSR_K7_PERFCTR0);
  107. case X86_VENDOR_INTEL:
  108. switch (boot_cpu_data.x86) {
  109. case 6:
  110. return (msr - MSR_P6_PERFCTR0);
  111. case 15:
  112. return (msr - MSR_P4_BPU_PERFCTR0);
  113. }
  114. }
  115. return 0;
  116. }
  117. /* converts an msr to an appropriate reservation bit */
  118. static inline unsigned int nmi_evntsel_msr_to_bit(unsigned int msr)
  119. {
  120. /* returns the bit offset of the event selection register */
  121. switch (boot_cpu_data.x86_vendor) {
  122. case X86_VENDOR_AMD:
  123. return (msr - MSR_K7_EVNTSEL0);
  124. case X86_VENDOR_INTEL:
  125. switch (boot_cpu_data.x86) {
  126. case 6:
  127. return (msr - MSR_P6_EVNTSEL0);
  128. case 15:
  129. return (msr - MSR_P4_BSU_ESCR0);
  130. }
  131. }
  132. return 0;
  133. }
  134. /* checks for a bit availability (hack for oprofile) */
  135. int avail_to_resrv_perfctr_nmi_bit(unsigned int counter)
  136. {
  137. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  138. return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner)));
  139. }
  140. /* checks the an msr for availability */
  141. int avail_to_resrv_perfctr_nmi(unsigned int msr)
  142. {
  143. unsigned int counter;
  144. counter = nmi_perfctr_msr_to_bit(msr);
  145. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  146. return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner)));
  147. }
  148. int reserve_perfctr_nmi(unsigned int msr)
  149. {
  150. unsigned int counter;
  151. counter = nmi_perfctr_msr_to_bit(msr);
  152. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  153. if (!test_and_set_bit(counter, &__get_cpu_var(perfctr_nmi_owner)))
  154. return 1;
  155. return 0;
  156. }
  157. void release_perfctr_nmi(unsigned int msr)
  158. {
  159. unsigned int counter;
  160. counter = nmi_perfctr_msr_to_bit(msr);
  161. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  162. clear_bit(counter, &__get_cpu_var(perfctr_nmi_owner));
  163. }
  164. int reserve_evntsel_nmi(unsigned int msr)
  165. {
  166. unsigned int counter;
  167. counter = nmi_evntsel_msr_to_bit(msr);
  168. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  169. if (!test_and_set_bit(counter, &__get_cpu_var(evntsel_nmi_owner)[0]))
  170. return 1;
  171. return 0;
  172. }
  173. void release_evntsel_nmi(unsigned int msr)
  174. {
  175. unsigned int counter;
  176. counter = nmi_evntsel_msr_to_bit(msr);
  177. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  178. clear_bit(counter, &__get_cpu_var(evntsel_nmi_owner)[0]);
  179. }
  180. #ifdef CONFIG_SMP
  181. /* The performance counters used by NMI_LOCAL_APIC don't trigger when
  182. * the CPU is idle. To make sure the NMI watchdog really ticks on all
  183. * CPUs during the test make them busy.
  184. */
  185. static __init void nmi_cpu_busy(void *data)
  186. {
  187. volatile int *endflag = data;
  188. local_irq_enable_in_hardirq();
  189. /* Intentionally don't use cpu_relax here. This is
  190. to make sure that the performance counter really ticks,
  191. even if there is a simulator or similar that catches the
  192. pause instruction. On a real HT machine this is fine because
  193. all other CPUs are busy with "useless" delay loops and don't
  194. care if they get somewhat less cycles. */
  195. while (*endflag == 0)
  196. barrier();
  197. }
  198. #endif
  199. static int __init check_nmi_watchdog(void)
  200. {
  201. volatile int endflag = 0;
  202. unsigned int *prev_nmi_count;
  203. int cpu;
  204. if (nmi_watchdog == NMI_NONE)
  205. return 0;
  206. prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
  207. if (!prev_nmi_count)
  208. return -1;
  209. printk(KERN_INFO "Testing NMI watchdog ... ");
  210. if (nmi_watchdog == NMI_LOCAL_APIC)
  211. smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
  212. for_each_possible_cpu(cpu)
  213. prev_nmi_count[cpu] = per_cpu(irq_stat, cpu).__nmi_count;
  214. local_irq_enable();
  215. mdelay((10*1000)/nmi_hz); // wait 10 ticks
  216. for_each_possible_cpu(cpu) {
  217. #ifdef CONFIG_SMP
  218. /* Check cpu_callin_map here because that is set
  219. after the timer is started. */
  220. if (!cpu_isset(cpu, cpu_callin_map))
  221. continue;
  222. #endif
  223. if (nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
  224. endflag = 1;
  225. printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n",
  226. cpu,
  227. prev_nmi_count[cpu],
  228. nmi_count(cpu));
  229. nmi_active = 0;
  230. lapic_nmi_owner &= ~LAPIC_NMI_WATCHDOG;
  231. kfree(prev_nmi_count);
  232. return -1;
  233. }
  234. }
  235. endflag = 1;
  236. printk("OK.\n");
  237. /* now that we know it works we can reduce NMI frequency to
  238. something more reasonable; makes a difference in some configs */
  239. if (nmi_watchdog == NMI_LOCAL_APIC)
  240. nmi_hz = 1;
  241. kfree(prev_nmi_count);
  242. return 0;
  243. }
  244. /* This needs to happen later in boot so counters are working */
  245. late_initcall(check_nmi_watchdog);
  246. static int __init setup_nmi_watchdog(char *str)
  247. {
  248. int nmi;
  249. get_option(&str, &nmi);
  250. if (nmi >= NMI_INVALID)
  251. return 0;
  252. if (nmi == NMI_NONE)
  253. nmi_watchdog = nmi;
  254. /*
  255. * If any other x86 CPU has a local APIC, then
  256. * please test the NMI stuff there and send me the
  257. * missing bits. Right now Intel P6/P4 and AMD K7 only.
  258. */
  259. if ((nmi == NMI_LOCAL_APIC) &&
  260. (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
  261. (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15))
  262. nmi_watchdog = nmi;
  263. if ((nmi == NMI_LOCAL_APIC) &&
  264. (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) &&
  265. (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15))
  266. nmi_watchdog = nmi;
  267. /*
  268. * We can enable the IO-APIC watchdog
  269. * unconditionally.
  270. */
  271. if (nmi == NMI_IO_APIC) {
  272. nmi_active = 1;
  273. nmi_watchdog = nmi;
  274. }
  275. return 1;
  276. }
  277. __setup("nmi_watchdog=", setup_nmi_watchdog);
  278. static void disable_lapic_nmi_watchdog(void)
  279. {
  280. if (nmi_active <= 0)
  281. return;
  282. switch (boot_cpu_data.x86_vendor) {
  283. case X86_VENDOR_AMD:
  284. wrmsr(MSR_K7_EVNTSEL0, 0, 0);
  285. break;
  286. case X86_VENDOR_INTEL:
  287. switch (boot_cpu_data.x86) {
  288. case 6:
  289. if (boot_cpu_data.x86_model > 0xd)
  290. break;
  291. wrmsr(MSR_P6_EVNTSEL0, 0, 0);
  292. break;
  293. case 15:
  294. if (boot_cpu_data.x86_model > 0x4)
  295. break;
  296. wrmsr(MSR_P4_IQ_CCCR0, 0, 0);
  297. wrmsr(MSR_P4_CRU_ESCR0, 0, 0);
  298. break;
  299. }
  300. break;
  301. }
  302. nmi_active = -1;
  303. /* tell do_nmi() and others that we're not active any more */
  304. nmi_watchdog = 0;
  305. }
  306. static void enable_lapic_nmi_watchdog(void)
  307. {
  308. if (nmi_active < 0) {
  309. nmi_watchdog = NMI_LOCAL_APIC;
  310. setup_apic_nmi_watchdog();
  311. }
  312. }
  313. int reserve_lapic_nmi(void)
  314. {
  315. unsigned int old_owner;
  316. spin_lock(&lapic_nmi_owner_lock);
  317. old_owner = lapic_nmi_owner;
  318. lapic_nmi_owner |= LAPIC_NMI_RESERVED;
  319. spin_unlock(&lapic_nmi_owner_lock);
  320. if (old_owner & LAPIC_NMI_RESERVED)
  321. return -EBUSY;
  322. if (old_owner & LAPIC_NMI_WATCHDOG)
  323. disable_lapic_nmi_watchdog();
  324. return 0;
  325. }
  326. void release_lapic_nmi(void)
  327. {
  328. unsigned int new_owner;
  329. spin_lock(&lapic_nmi_owner_lock);
  330. new_owner = lapic_nmi_owner & ~LAPIC_NMI_RESERVED;
  331. lapic_nmi_owner = new_owner;
  332. spin_unlock(&lapic_nmi_owner_lock);
  333. if (new_owner & LAPIC_NMI_WATCHDOG)
  334. enable_lapic_nmi_watchdog();
  335. }
  336. void disable_timer_nmi_watchdog(void)
  337. {
  338. if ((nmi_watchdog != NMI_IO_APIC) || (nmi_active <= 0))
  339. return;
  340. unset_nmi_callback();
  341. nmi_active = -1;
  342. nmi_watchdog = NMI_NONE;
  343. }
  344. void enable_timer_nmi_watchdog(void)
  345. {
  346. if (nmi_active < 0) {
  347. nmi_watchdog = NMI_IO_APIC;
  348. touch_nmi_watchdog();
  349. nmi_active = 1;
  350. }
  351. }
  352. #ifdef CONFIG_PM
  353. static int nmi_pm_active; /* nmi_active before suspend */
  354. static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
  355. {
  356. nmi_pm_active = nmi_active;
  357. disable_lapic_nmi_watchdog();
  358. return 0;
  359. }
  360. static int lapic_nmi_resume(struct sys_device *dev)
  361. {
  362. if (nmi_pm_active > 0)
  363. enable_lapic_nmi_watchdog();
  364. return 0;
  365. }
  366. static struct sysdev_class nmi_sysclass = {
  367. set_kset_name("lapic_nmi"),
  368. .resume = lapic_nmi_resume,
  369. .suspend = lapic_nmi_suspend,
  370. };
  371. static struct sys_device device_lapic_nmi = {
  372. .id = 0,
  373. .cls = &nmi_sysclass,
  374. };
  375. static int __init init_lapic_nmi_sysfs(void)
  376. {
  377. int error;
  378. if (nmi_active == 0 || nmi_watchdog != NMI_LOCAL_APIC)
  379. return 0;
  380. error = sysdev_class_register(&nmi_sysclass);
  381. if (!error)
  382. error = sysdev_register(&device_lapic_nmi);
  383. return error;
  384. }
  385. /* must come after the local APIC's device_initcall() */
  386. late_initcall(init_lapic_nmi_sysfs);
  387. #endif /* CONFIG_PM */
  388. /*
  389. * Activate the NMI watchdog via the local APIC.
  390. * Original code written by Keith Owens.
  391. */
  392. static void write_watchdog_counter(const char *descr)
  393. {
  394. u64 count = (u64)cpu_khz * 1000;
  395. do_div(count, nmi_hz);
  396. if(descr)
  397. Dprintk("setting %s to -0x%08Lx\n", descr, count);
  398. wrmsrl(nmi_perfctr_msr, 0 - count);
  399. }
  400. static int setup_k7_watchdog(void)
  401. {
  402. unsigned int evntsel;
  403. nmi_perfctr_msr = MSR_K7_PERFCTR0;
  404. if (!reserve_perfctr_nmi(nmi_perfctr_msr))
  405. goto fail;
  406. if (!reserve_evntsel_nmi(MSR_K7_EVNTSEL0))
  407. goto fail1;
  408. wrmsrl(MSR_K7_PERFCTR0, 0UL);
  409. evntsel = K7_EVNTSEL_INT
  410. | K7_EVNTSEL_OS
  411. | K7_EVNTSEL_USR
  412. | K7_NMI_EVENT;
  413. wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
  414. write_watchdog_counter("K7_PERFCTR0");
  415. apic_write(APIC_LVTPC, APIC_DM_NMI);
  416. evntsel |= K7_EVNTSEL_ENABLE;
  417. wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
  418. return 1;
  419. fail1:
  420. release_perfctr_nmi(nmi_perfctr_msr);
  421. fail:
  422. return 0;
  423. }
  424. static int setup_p6_watchdog(void)
  425. {
  426. unsigned int evntsel;
  427. nmi_perfctr_msr = MSR_P6_PERFCTR0;
  428. if (!reserve_perfctr_nmi(nmi_perfctr_msr))
  429. goto fail;
  430. if (!reserve_evntsel_nmi(MSR_P6_EVNTSEL0))
  431. goto fail1;
  432. evntsel = P6_EVNTSEL_INT
  433. | P6_EVNTSEL_OS
  434. | P6_EVNTSEL_USR
  435. | P6_NMI_EVENT;
  436. wrmsr(MSR_P6_EVNTSEL0, evntsel, 0);
  437. write_watchdog_counter("P6_PERFCTR0");
  438. apic_write(APIC_LVTPC, APIC_DM_NMI);
  439. evntsel |= P6_EVNTSEL0_ENABLE;
  440. wrmsr(MSR_P6_EVNTSEL0, evntsel, 0);
  441. return 1;
  442. fail1:
  443. release_perfctr_nmi(nmi_perfctr_msr);
  444. fail:
  445. return 0;
  446. }
  447. static int setup_p4_watchdog(void)
  448. {
  449. unsigned int misc_enable, dummy;
  450. rdmsr(MSR_P4_MISC_ENABLE, misc_enable, dummy);
  451. if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
  452. return 0;
  453. nmi_perfctr_msr = MSR_P4_IQ_COUNTER0;
  454. nmi_p4_cccr_val = P4_NMI_IQ_CCCR0;
  455. #ifdef CONFIG_SMP
  456. if (smp_num_siblings == 2)
  457. nmi_p4_cccr_val |= P4_CCCR_OVF_PMI1;
  458. #endif
  459. if (!reserve_perfctr_nmi(nmi_perfctr_msr))
  460. goto fail;
  461. if (!reserve_evntsel_nmi(MSR_P4_CRU_ESCR0))
  462. goto fail1;
  463. wrmsr(MSR_P4_CRU_ESCR0, P4_NMI_CRU_ESCR0, 0);
  464. wrmsr(MSR_P4_IQ_CCCR0, P4_NMI_IQ_CCCR0 & ~P4_CCCR_ENABLE, 0);
  465. write_watchdog_counter("P4_IQ_COUNTER0");
  466. apic_write(APIC_LVTPC, APIC_DM_NMI);
  467. wrmsr(MSR_P4_IQ_CCCR0, nmi_p4_cccr_val, 0);
  468. return 1;
  469. fail1:
  470. release_perfctr_nmi(nmi_perfctr_msr);
  471. fail:
  472. return 0;
  473. }
  474. void setup_apic_nmi_watchdog (void)
  475. {
  476. switch (boot_cpu_data.x86_vendor) {
  477. case X86_VENDOR_AMD:
  478. if (boot_cpu_data.x86 != 6 && boot_cpu_data.x86 != 15)
  479. return;
  480. if (!setup_k7_watchdog())
  481. return;
  482. break;
  483. case X86_VENDOR_INTEL:
  484. switch (boot_cpu_data.x86) {
  485. case 6:
  486. if (boot_cpu_data.x86_model > 0xd)
  487. return;
  488. if(!setup_p6_watchdog())
  489. return;
  490. break;
  491. case 15:
  492. if (boot_cpu_data.x86_model > 0x4)
  493. return;
  494. if (!setup_p4_watchdog())
  495. return;
  496. break;
  497. default:
  498. return;
  499. }
  500. break;
  501. default:
  502. return;
  503. }
  504. lapic_nmi_owner = LAPIC_NMI_WATCHDOG;
  505. nmi_active = 1;
  506. }
  507. /*
  508. * the best way to detect whether a CPU has a 'hard lockup' problem
  509. * is to check it's local APIC timer IRQ counts. If they are not
  510. * changing then that CPU has some problem.
  511. *
  512. * as these watchdog NMI IRQs are generated on every CPU, we only
  513. * have to check the current processor.
  514. *
  515. * since NMIs don't listen to _any_ locks, we have to be extremely
  516. * careful not to rely on unsafe variables. The printk might lock
  517. * up though, so we have to break up any console locks first ...
  518. * [when there will be more tty-related locks, break them up
  519. * here too!]
  520. */
  521. static unsigned int
  522. last_irq_sums [NR_CPUS],
  523. alert_counter [NR_CPUS];
  524. void touch_nmi_watchdog (void)
  525. {
  526. int i;
  527. /*
  528. * Just reset the alert counters, (other CPUs might be
  529. * spinning on locks we hold):
  530. */
  531. for_each_possible_cpu(i)
  532. alert_counter[i] = 0;
  533. /*
  534. * Tickle the softlockup detector too:
  535. */
  536. touch_softlockup_watchdog();
  537. }
  538. EXPORT_SYMBOL(touch_nmi_watchdog);
  539. extern void die_nmi(struct pt_regs *, const char *msg);
  540. void nmi_watchdog_tick (struct pt_regs * regs)
  541. {
  542. /*
  543. * Since current_thread_info()-> is always on the stack, and we
  544. * always switch the stack NMI-atomically, it's safe to use
  545. * smp_processor_id().
  546. */
  547. unsigned int sum;
  548. int cpu = smp_processor_id();
  549. sum = per_cpu(irq_stat, cpu).apic_timer_irqs;
  550. if (last_irq_sums[cpu] == sum) {
  551. /*
  552. * Ayiee, looks like this CPU is stuck ...
  553. * wait a few IRQs (5 seconds) before doing the oops ...
  554. */
  555. alert_counter[cpu]++;
  556. if (alert_counter[cpu] == 5*nmi_hz)
  557. /*
  558. * die_nmi will return ONLY if NOTIFY_STOP happens..
  559. */
  560. die_nmi(regs, "BUG: NMI Watchdog detected LOCKUP");
  561. } else {
  562. last_irq_sums[cpu] = sum;
  563. alert_counter[cpu] = 0;
  564. }
  565. if (nmi_perfctr_msr) {
  566. if (nmi_perfctr_msr == MSR_P4_IQ_COUNTER0) {
  567. /*
  568. * P4 quirks:
  569. * - An overflown perfctr will assert its interrupt
  570. * until the OVF flag in its CCCR is cleared.
  571. * - LVTPC is masked on interrupt and must be
  572. * unmasked by the LVTPC handler.
  573. */
  574. wrmsr(MSR_P4_IQ_CCCR0, nmi_p4_cccr_val, 0);
  575. apic_write(APIC_LVTPC, APIC_DM_NMI);
  576. }
  577. else if (nmi_perfctr_msr == MSR_P6_PERFCTR0) {
  578. /* Only P6 based Pentium M need to re-unmask
  579. * the apic vector but it doesn't hurt
  580. * other P6 variant */
  581. apic_write(APIC_LVTPC, APIC_DM_NMI);
  582. }
  583. write_watchdog_counter(NULL);
  584. }
  585. }
  586. #ifdef CONFIG_SYSCTL
  587. static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
  588. {
  589. unsigned char reason = get_nmi_reason();
  590. char buf[64];
  591. if (!(reason & 0xc0)) {
  592. sprintf(buf, "NMI received for unknown reason %02x\n", reason);
  593. die_nmi(regs, buf);
  594. }
  595. return 0;
  596. }
  597. /*
  598. * proc handler for /proc/sys/kernel/unknown_nmi_panic
  599. */
  600. int proc_unknown_nmi_panic(ctl_table *table, int write, struct file *file,
  601. void __user *buffer, size_t *length, loff_t *ppos)
  602. {
  603. int old_state;
  604. old_state = unknown_nmi_panic;
  605. proc_dointvec(table, write, file, buffer, length, ppos);
  606. if (!!old_state == !!unknown_nmi_panic)
  607. return 0;
  608. if (unknown_nmi_panic) {
  609. if (reserve_lapic_nmi() < 0) {
  610. unknown_nmi_panic = 0;
  611. return -EBUSY;
  612. } else {
  613. set_nmi_callback(unknown_nmi_panic_callback);
  614. }
  615. } else {
  616. release_lapic_nmi();
  617. unset_nmi_callback();
  618. }
  619. return 0;
  620. }
  621. #endif
  622. EXPORT_SYMBOL(nmi_active);
  623. EXPORT_SYMBOL(nmi_watchdog);
  624. EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi);
  625. EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi_bit);
  626. EXPORT_SYMBOL(reserve_perfctr_nmi);
  627. EXPORT_SYMBOL(release_perfctr_nmi);
  628. EXPORT_SYMBOL(reserve_evntsel_nmi);
  629. EXPORT_SYMBOL(release_evntsel_nmi);
  630. EXPORT_SYMBOL(reserve_lapic_nmi);
  631. EXPORT_SYMBOL(release_lapic_nmi);
  632. EXPORT_SYMBOL(disable_timer_nmi_watchdog);
  633. EXPORT_SYMBOL(enable_timer_nmi_watchdog);