nmi.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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/delay.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/module.h>
  18. #include <linux/nmi.h>
  19. #include <linux/sysdev.h>
  20. #include <linux/sysctl.h>
  21. #include <linux/percpu.h>
  22. #include <linux/dmi.h>
  23. #include <linux/kprobes.h>
  24. #include <asm/smp.h>
  25. #include <asm/nmi.h>
  26. #include <asm/kdebug.h>
  27. #include <asm/intel_arch_perfmon.h>
  28. #include "mach_traps.h"
  29. int unknown_nmi_panic;
  30. int nmi_watchdog_enabled;
  31. /* perfctr_nmi_owner tracks the ownership of the perfctr registers:
  32. * evtsel_nmi_owner tracks the ownership of the event selection
  33. * - different performance counters/ event selection may be reserved for
  34. * different subsystems this reservation system just tries to coordinate
  35. * things a little
  36. */
  37. static DEFINE_PER_CPU(unsigned long, perfctr_nmi_owner);
  38. static DEFINE_PER_CPU(unsigned long, evntsel_nmi_owner[3]);
  39. /* this number is calculated from Intel's MSR_P4_CRU_ESCR5 register and it's
  40. * offset from MSR_P4_BSU_ESCR0. It will be the max for all platforms (for now)
  41. */
  42. #define NMI_MAX_COUNTER_BITS 66
  43. /* nmi_active:
  44. * >0: the lapic NMI watchdog is active, but can be disabled
  45. * <0: the lapic NMI watchdog has not been set up, and cannot
  46. * be enabled
  47. * 0: the lapic NMI watchdog is disabled, but can be enabled
  48. */
  49. atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
  50. unsigned int nmi_watchdog = NMI_DEFAULT;
  51. static unsigned int nmi_hz = HZ;
  52. struct nmi_watchdog_ctlblk {
  53. int enabled;
  54. u64 check_bit;
  55. unsigned int cccr_msr;
  56. unsigned int perfctr_msr; /* the MSR to reset in NMI handler */
  57. unsigned int evntsel_msr; /* the MSR to select the events to handle */
  58. };
  59. static DEFINE_PER_CPU(struct nmi_watchdog_ctlblk, nmi_watchdog_ctlblk);
  60. /* local prototypes */
  61. static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu);
  62. extern void show_registers(struct pt_regs *regs);
  63. extern int unknown_nmi_panic;
  64. /* converts an msr to an appropriate reservation bit */
  65. static inline unsigned int nmi_perfctr_msr_to_bit(unsigned int msr)
  66. {
  67. /* returns the bit offset of the performance counter register */
  68. switch (boot_cpu_data.x86_vendor) {
  69. case X86_VENDOR_AMD:
  70. return (msr - MSR_K7_PERFCTR0);
  71. case X86_VENDOR_INTEL:
  72. if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  73. return (msr - MSR_ARCH_PERFMON_PERFCTR0);
  74. switch (boot_cpu_data.x86) {
  75. case 6:
  76. return (msr - MSR_P6_PERFCTR0);
  77. case 15:
  78. return (msr - MSR_P4_BPU_PERFCTR0);
  79. }
  80. }
  81. return 0;
  82. }
  83. /* converts an msr to an appropriate reservation bit */
  84. static inline unsigned int nmi_evntsel_msr_to_bit(unsigned int msr)
  85. {
  86. /* returns the bit offset of the event selection register */
  87. switch (boot_cpu_data.x86_vendor) {
  88. case X86_VENDOR_AMD:
  89. return (msr - MSR_K7_EVNTSEL0);
  90. case X86_VENDOR_INTEL:
  91. if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  92. return (msr - MSR_ARCH_PERFMON_EVENTSEL0);
  93. switch (boot_cpu_data.x86) {
  94. case 6:
  95. return (msr - MSR_P6_EVNTSEL0);
  96. case 15:
  97. return (msr - MSR_P4_BSU_ESCR0);
  98. }
  99. }
  100. return 0;
  101. }
  102. /* checks for a bit availability (hack for oprofile) */
  103. int avail_to_resrv_perfctr_nmi_bit(unsigned int counter)
  104. {
  105. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  106. return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner)));
  107. }
  108. /* checks the an msr for availability */
  109. int avail_to_resrv_perfctr_nmi(unsigned int msr)
  110. {
  111. unsigned int counter;
  112. counter = nmi_perfctr_msr_to_bit(msr);
  113. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  114. return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner)));
  115. }
  116. int reserve_perfctr_nmi(unsigned int msr)
  117. {
  118. unsigned int counter;
  119. counter = nmi_perfctr_msr_to_bit(msr);
  120. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  121. if (!test_and_set_bit(counter, &__get_cpu_var(perfctr_nmi_owner)))
  122. return 1;
  123. return 0;
  124. }
  125. void release_perfctr_nmi(unsigned int msr)
  126. {
  127. unsigned int counter;
  128. counter = nmi_perfctr_msr_to_bit(msr);
  129. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  130. clear_bit(counter, &__get_cpu_var(perfctr_nmi_owner));
  131. }
  132. int reserve_evntsel_nmi(unsigned int msr)
  133. {
  134. unsigned int counter;
  135. counter = nmi_evntsel_msr_to_bit(msr);
  136. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  137. if (!test_and_set_bit(counter, &__get_cpu_var(evntsel_nmi_owner)[0]))
  138. return 1;
  139. return 0;
  140. }
  141. void release_evntsel_nmi(unsigned int msr)
  142. {
  143. unsigned int counter;
  144. counter = nmi_evntsel_msr_to_bit(msr);
  145. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  146. clear_bit(counter, &__get_cpu_var(evntsel_nmi_owner)[0]);
  147. }
  148. static __cpuinit inline int nmi_known_cpu(void)
  149. {
  150. switch (boot_cpu_data.x86_vendor) {
  151. case X86_VENDOR_AMD:
  152. return ((boot_cpu_data.x86 == 15) || (boot_cpu_data.x86 == 6));
  153. case X86_VENDOR_INTEL:
  154. if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  155. return 1;
  156. else
  157. return ((boot_cpu_data.x86 == 15) || (boot_cpu_data.x86 == 6));
  158. }
  159. return 0;
  160. }
  161. #ifdef CONFIG_SMP
  162. /* The performance counters used by NMI_LOCAL_APIC don't trigger when
  163. * the CPU is idle. To make sure the NMI watchdog really ticks on all
  164. * CPUs during the test make them busy.
  165. */
  166. static __init void nmi_cpu_busy(void *data)
  167. {
  168. volatile int *endflag = data;
  169. local_irq_enable_in_hardirq();
  170. /* Intentionally don't use cpu_relax here. This is
  171. to make sure that the performance counter really ticks,
  172. even if there is a simulator or similar that catches the
  173. pause instruction. On a real HT machine this is fine because
  174. all other CPUs are busy with "useless" delay loops and don't
  175. care if they get somewhat less cycles. */
  176. while (*endflag == 0)
  177. barrier();
  178. }
  179. #endif
  180. static int __init check_nmi_watchdog(void)
  181. {
  182. volatile int endflag = 0;
  183. unsigned int *prev_nmi_count;
  184. int cpu;
  185. /* Enable NMI watchdog for newer systems.
  186. Probably safe on most older systems too, but let's be careful.
  187. IBM ThinkPads use INT10 inside SMM and that allows early NMI inside SMM
  188. which hangs the system. Disable watchdog for all thinkpads */
  189. if (nmi_watchdog == NMI_DEFAULT && dmi_get_year(DMI_BIOS_DATE) >= 2004 &&
  190. !dmi_name_in_vendors("ThinkPad"))
  191. nmi_watchdog = NMI_LOCAL_APIC;
  192. if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DEFAULT))
  193. return 0;
  194. if (!atomic_read(&nmi_active))
  195. return 0;
  196. prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
  197. if (!prev_nmi_count)
  198. return -1;
  199. printk(KERN_INFO "Testing NMI watchdog ... ");
  200. if (nmi_watchdog == NMI_LOCAL_APIC)
  201. smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
  202. for_each_possible_cpu(cpu)
  203. prev_nmi_count[cpu] = per_cpu(irq_stat, cpu).__nmi_count;
  204. local_irq_enable();
  205. mdelay((10*1000)/nmi_hz); // wait 10 ticks
  206. for_each_possible_cpu(cpu) {
  207. #ifdef CONFIG_SMP
  208. /* Check cpu_callin_map here because that is set
  209. after the timer is started. */
  210. if (!cpu_isset(cpu, cpu_callin_map))
  211. continue;
  212. #endif
  213. if (!per_cpu(nmi_watchdog_ctlblk, cpu).enabled)
  214. continue;
  215. if (nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
  216. printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n",
  217. cpu,
  218. prev_nmi_count[cpu],
  219. nmi_count(cpu));
  220. per_cpu(nmi_watchdog_ctlblk, cpu).enabled = 0;
  221. atomic_dec(&nmi_active);
  222. }
  223. }
  224. if (!atomic_read(&nmi_active)) {
  225. kfree(prev_nmi_count);
  226. atomic_set(&nmi_active, -1);
  227. return -1;
  228. }
  229. endflag = 1;
  230. printk("OK.\n");
  231. /* now that we know it works we can reduce NMI frequency to
  232. something more reasonable; makes a difference in some configs */
  233. if (nmi_watchdog == NMI_LOCAL_APIC) {
  234. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  235. nmi_hz = 1;
  236. /*
  237. * On Intel CPUs with ARCH_PERFMON only 32 bits in the counter
  238. * are writable, with higher bits sign extending from bit 31.
  239. * So, we can only program the counter with 31 bit values and
  240. * 32nd bit should be 1, for 33.. to be 1.
  241. * Find the appropriate nmi_hz
  242. */
  243. if (wd->perfctr_msr == MSR_ARCH_PERFMON_PERFCTR0 &&
  244. ((u64)cpu_khz * 1000) > 0x7fffffffULL) {
  245. u64 count = (u64)cpu_khz * 1000;
  246. do_div(count, 0x7fffffffUL);
  247. nmi_hz = count + 1;
  248. }
  249. }
  250. kfree(prev_nmi_count);
  251. return 0;
  252. }
  253. /* This needs to happen later in boot so counters are working */
  254. late_initcall(check_nmi_watchdog);
  255. static int __init setup_nmi_watchdog(char *str)
  256. {
  257. int nmi;
  258. get_option(&str, &nmi);
  259. if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
  260. return 0;
  261. /*
  262. * If any other x86 CPU has a local APIC, then
  263. * please test the NMI stuff there and send me the
  264. * missing bits. Right now Intel P6/P4 and AMD K7 only.
  265. */
  266. if ((nmi == NMI_LOCAL_APIC) && (nmi_known_cpu() == 0))
  267. return 0; /* no lapic support */
  268. nmi_watchdog = nmi;
  269. return 1;
  270. }
  271. __setup("nmi_watchdog=", setup_nmi_watchdog);
  272. static void disable_lapic_nmi_watchdog(void)
  273. {
  274. BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
  275. if (atomic_read(&nmi_active) <= 0)
  276. return;
  277. on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
  278. BUG_ON(atomic_read(&nmi_active) != 0);
  279. }
  280. static void enable_lapic_nmi_watchdog(void)
  281. {
  282. BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
  283. /* are we already enabled */
  284. if (atomic_read(&nmi_active) != 0)
  285. return;
  286. /* are we lapic aware */
  287. if (nmi_known_cpu() <= 0)
  288. return;
  289. on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
  290. touch_nmi_watchdog();
  291. }
  292. void disable_timer_nmi_watchdog(void)
  293. {
  294. BUG_ON(nmi_watchdog != NMI_IO_APIC);
  295. if (atomic_read(&nmi_active) <= 0)
  296. return;
  297. disable_irq(0);
  298. on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
  299. BUG_ON(atomic_read(&nmi_active) != 0);
  300. }
  301. void enable_timer_nmi_watchdog(void)
  302. {
  303. BUG_ON(nmi_watchdog != NMI_IO_APIC);
  304. if (atomic_read(&nmi_active) == 0) {
  305. touch_nmi_watchdog();
  306. on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
  307. enable_irq(0);
  308. }
  309. }
  310. #ifdef CONFIG_PM
  311. static int nmi_pm_active; /* nmi_active before suspend */
  312. static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
  313. {
  314. /* only CPU0 goes here, other CPUs should be offline */
  315. nmi_pm_active = atomic_read(&nmi_active);
  316. stop_apic_nmi_watchdog(NULL);
  317. BUG_ON(atomic_read(&nmi_active) != 0);
  318. return 0;
  319. }
  320. static int lapic_nmi_resume(struct sys_device *dev)
  321. {
  322. /* only CPU0 goes here, other CPUs should be offline */
  323. if (nmi_pm_active > 0) {
  324. setup_apic_nmi_watchdog(NULL);
  325. touch_nmi_watchdog();
  326. }
  327. return 0;
  328. }
  329. static struct sysdev_class nmi_sysclass = {
  330. set_kset_name("lapic_nmi"),
  331. .resume = lapic_nmi_resume,
  332. .suspend = lapic_nmi_suspend,
  333. };
  334. static struct sys_device device_lapic_nmi = {
  335. .id = 0,
  336. .cls = &nmi_sysclass,
  337. };
  338. static int __init init_lapic_nmi_sysfs(void)
  339. {
  340. int error;
  341. /* should really be a BUG_ON but b/c this is an
  342. * init call, it just doesn't work. -dcz
  343. */
  344. if (nmi_watchdog != NMI_LOCAL_APIC)
  345. return 0;
  346. if ( atomic_read(&nmi_active) < 0 )
  347. return 0;
  348. error = sysdev_class_register(&nmi_sysclass);
  349. if (!error)
  350. error = sysdev_register(&device_lapic_nmi);
  351. return error;
  352. }
  353. /* must come after the local APIC's device_initcall() */
  354. late_initcall(init_lapic_nmi_sysfs);
  355. #endif /* CONFIG_PM */
  356. /*
  357. * Activate the NMI watchdog via the local APIC.
  358. * Original code written by Keith Owens.
  359. */
  360. static void write_watchdog_counter(unsigned int perfctr_msr, const char *descr)
  361. {
  362. u64 count = (u64)cpu_khz * 1000;
  363. do_div(count, nmi_hz);
  364. if(descr)
  365. Dprintk("setting %s to -0x%08Lx\n", descr, count);
  366. wrmsrl(perfctr_msr, 0 - count);
  367. }
  368. /* Note that these events don't tick when the CPU idles. This means
  369. the frequency varies with CPU load. */
  370. #define K7_EVNTSEL_ENABLE (1 << 22)
  371. #define K7_EVNTSEL_INT (1 << 20)
  372. #define K7_EVNTSEL_OS (1 << 17)
  373. #define K7_EVNTSEL_USR (1 << 16)
  374. #define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
  375. #define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
  376. static int setup_k7_watchdog(void)
  377. {
  378. unsigned int perfctr_msr, evntsel_msr;
  379. unsigned int evntsel;
  380. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  381. perfctr_msr = MSR_K7_PERFCTR0;
  382. evntsel_msr = MSR_K7_EVNTSEL0;
  383. if (!reserve_perfctr_nmi(perfctr_msr))
  384. goto fail;
  385. if (!reserve_evntsel_nmi(evntsel_msr))
  386. goto fail1;
  387. wrmsrl(perfctr_msr, 0UL);
  388. evntsel = K7_EVNTSEL_INT
  389. | K7_EVNTSEL_OS
  390. | K7_EVNTSEL_USR
  391. | K7_NMI_EVENT;
  392. /* setup the timer */
  393. wrmsr(evntsel_msr, evntsel, 0);
  394. write_watchdog_counter(perfctr_msr, "K7_PERFCTR0");
  395. apic_write(APIC_LVTPC, APIC_DM_NMI);
  396. evntsel |= K7_EVNTSEL_ENABLE;
  397. wrmsr(evntsel_msr, evntsel, 0);
  398. wd->perfctr_msr = perfctr_msr;
  399. wd->evntsel_msr = evntsel_msr;
  400. wd->cccr_msr = 0; //unused
  401. wd->check_bit = 1ULL<<63;
  402. return 1;
  403. fail1:
  404. release_perfctr_nmi(perfctr_msr);
  405. fail:
  406. return 0;
  407. }
  408. static void stop_k7_watchdog(void)
  409. {
  410. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  411. wrmsr(wd->evntsel_msr, 0, 0);
  412. release_evntsel_nmi(wd->evntsel_msr);
  413. release_perfctr_nmi(wd->perfctr_msr);
  414. }
  415. #define P6_EVNTSEL0_ENABLE (1 << 22)
  416. #define P6_EVNTSEL_INT (1 << 20)
  417. #define P6_EVNTSEL_OS (1 << 17)
  418. #define P6_EVNTSEL_USR (1 << 16)
  419. #define P6_EVENT_CPU_CLOCKS_NOT_HALTED 0x79
  420. #define P6_NMI_EVENT P6_EVENT_CPU_CLOCKS_NOT_HALTED
  421. static int setup_p6_watchdog(void)
  422. {
  423. unsigned int perfctr_msr, evntsel_msr;
  424. unsigned int evntsel;
  425. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  426. perfctr_msr = MSR_P6_PERFCTR0;
  427. evntsel_msr = MSR_P6_EVNTSEL0;
  428. if (!reserve_perfctr_nmi(perfctr_msr))
  429. goto fail;
  430. if (!reserve_evntsel_nmi(evntsel_msr))
  431. goto fail1;
  432. wrmsrl(perfctr_msr, 0UL);
  433. evntsel = P6_EVNTSEL_INT
  434. | P6_EVNTSEL_OS
  435. | P6_EVNTSEL_USR
  436. | P6_NMI_EVENT;
  437. /* setup the timer */
  438. wrmsr(evntsel_msr, evntsel, 0);
  439. write_watchdog_counter(perfctr_msr, "P6_PERFCTR0");
  440. apic_write(APIC_LVTPC, APIC_DM_NMI);
  441. evntsel |= P6_EVNTSEL0_ENABLE;
  442. wrmsr(evntsel_msr, evntsel, 0);
  443. wd->perfctr_msr = perfctr_msr;
  444. wd->evntsel_msr = evntsel_msr;
  445. wd->cccr_msr = 0; //unused
  446. wd->check_bit = 1ULL<<39;
  447. return 1;
  448. fail1:
  449. release_perfctr_nmi(perfctr_msr);
  450. fail:
  451. return 0;
  452. }
  453. static void stop_p6_watchdog(void)
  454. {
  455. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  456. wrmsr(wd->evntsel_msr, 0, 0);
  457. release_evntsel_nmi(wd->evntsel_msr);
  458. release_perfctr_nmi(wd->perfctr_msr);
  459. }
  460. /* Note that these events don't tick when the CPU idles. This means
  461. the frequency varies with CPU load. */
  462. #define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7)
  463. #define P4_ESCR_EVENT_SELECT(N) ((N)<<25)
  464. #define P4_ESCR_OS (1<<3)
  465. #define P4_ESCR_USR (1<<2)
  466. #define P4_CCCR_OVF_PMI0 (1<<26)
  467. #define P4_CCCR_OVF_PMI1 (1<<27)
  468. #define P4_CCCR_THRESHOLD(N) ((N)<<20)
  469. #define P4_CCCR_COMPLEMENT (1<<19)
  470. #define P4_CCCR_COMPARE (1<<18)
  471. #define P4_CCCR_REQUIRED (3<<16)
  472. #define P4_CCCR_ESCR_SELECT(N) ((N)<<13)
  473. #define P4_CCCR_ENABLE (1<<12)
  474. #define P4_CCCR_OVF (1<<31)
  475. /* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
  476. CRU_ESCR0 (with any non-null event selector) through a complemented
  477. max threshold. [IA32-Vol3, Section 14.9.9] */
  478. static int setup_p4_watchdog(void)
  479. {
  480. unsigned int perfctr_msr, evntsel_msr, cccr_msr;
  481. unsigned int evntsel, cccr_val;
  482. unsigned int misc_enable, dummy;
  483. unsigned int ht_num;
  484. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  485. rdmsr(MSR_IA32_MISC_ENABLE, misc_enable, dummy);
  486. if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
  487. return 0;
  488. #ifdef CONFIG_SMP
  489. /* detect which hyperthread we are on */
  490. if (smp_num_siblings == 2) {
  491. unsigned int ebx, apicid;
  492. ebx = cpuid_ebx(1);
  493. apicid = (ebx >> 24) & 0xff;
  494. ht_num = apicid & 1;
  495. } else
  496. #endif
  497. ht_num = 0;
  498. /* performance counters are shared resources
  499. * assign each hyperthread its own set
  500. * (re-use the ESCR0 register, seems safe
  501. * and keeps the cccr_val the same)
  502. */
  503. if (!ht_num) {
  504. /* logical cpu 0 */
  505. perfctr_msr = MSR_P4_IQ_PERFCTR0;
  506. evntsel_msr = MSR_P4_CRU_ESCR0;
  507. cccr_msr = MSR_P4_IQ_CCCR0;
  508. cccr_val = P4_CCCR_OVF_PMI0 | P4_CCCR_ESCR_SELECT(4);
  509. } else {
  510. /* logical cpu 1 */
  511. perfctr_msr = MSR_P4_IQ_PERFCTR1;
  512. evntsel_msr = MSR_P4_CRU_ESCR0;
  513. cccr_msr = MSR_P4_IQ_CCCR1;
  514. cccr_val = P4_CCCR_OVF_PMI1 | P4_CCCR_ESCR_SELECT(4);
  515. }
  516. if (!reserve_perfctr_nmi(perfctr_msr))
  517. goto fail;
  518. if (!reserve_evntsel_nmi(evntsel_msr))
  519. goto fail1;
  520. evntsel = P4_ESCR_EVENT_SELECT(0x3F)
  521. | P4_ESCR_OS
  522. | P4_ESCR_USR;
  523. cccr_val |= P4_CCCR_THRESHOLD(15)
  524. | P4_CCCR_COMPLEMENT
  525. | P4_CCCR_COMPARE
  526. | P4_CCCR_REQUIRED;
  527. wrmsr(evntsel_msr, evntsel, 0);
  528. wrmsr(cccr_msr, cccr_val, 0);
  529. write_watchdog_counter(perfctr_msr, "P4_IQ_COUNTER0");
  530. apic_write(APIC_LVTPC, APIC_DM_NMI);
  531. cccr_val |= P4_CCCR_ENABLE;
  532. wrmsr(cccr_msr, cccr_val, 0);
  533. wd->perfctr_msr = perfctr_msr;
  534. wd->evntsel_msr = evntsel_msr;
  535. wd->cccr_msr = cccr_msr;
  536. wd->check_bit = 1ULL<<39;
  537. return 1;
  538. fail1:
  539. release_perfctr_nmi(perfctr_msr);
  540. fail:
  541. return 0;
  542. }
  543. static void stop_p4_watchdog(void)
  544. {
  545. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  546. wrmsr(wd->cccr_msr, 0, 0);
  547. wrmsr(wd->evntsel_msr, 0, 0);
  548. release_evntsel_nmi(wd->evntsel_msr);
  549. release_perfctr_nmi(wd->perfctr_msr);
  550. }
  551. #define ARCH_PERFMON_NMI_EVENT_SEL ARCH_PERFMON_UNHALTED_CORE_CYCLES_SEL
  552. #define ARCH_PERFMON_NMI_EVENT_UMASK ARCH_PERFMON_UNHALTED_CORE_CYCLES_UMASK
  553. static int setup_intel_arch_watchdog(void)
  554. {
  555. unsigned int ebx;
  556. union cpuid10_eax eax;
  557. unsigned int unused;
  558. unsigned int perfctr_msr, evntsel_msr;
  559. unsigned int evntsel;
  560. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  561. /*
  562. * Check whether the Architectural PerfMon supports
  563. * Unhalted Core Cycles Event or not.
  564. * NOTE: Corresponding bit = 0 in ebx indicates event present.
  565. */
  566. cpuid(10, &(eax.full), &ebx, &unused, &unused);
  567. if ((eax.split.mask_length < (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX+1)) ||
  568. (ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT))
  569. goto fail;
  570. perfctr_msr = MSR_ARCH_PERFMON_PERFCTR0;
  571. evntsel_msr = MSR_ARCH_PERFMON_EVENTSEL0;
  572. if (!reserve_perfctr_nmi(perfctr_msr))
  573. goto fail;
  574. if (!reserve_evntsel_nmi(evntsel_msr))
  575. goto fail1;
  576. wrmsrl(perfctr_msr, 0UL);
  577. evntsel = ARCH_PERFMON_EVENTSEL_INT
  578. | ARCH_PERFMON_EVENTSEL_OS
  579. | ARCH_PERFMON_EVENTSEL_USR
  580. | ARCH_PERFMON_NMI_EVENT_SEL
  581. | ARCH_PERFMON_NMI_EVENT_UMASK;
  582. /* setup the timer */
  583. wrmsr(evntsel_msr, evntsel, 0);
  584. write_watchdog_counter(perfctr_msr, "INTEL_ARCH_PERFCTR0");
  585. apic_write(APIC_LVTPC, APIC_DM_NMI);
  586. evntsel |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  587. wrmsr(evntsel_msr, evntsel, 0);
  588. wd->perfctr_msr = perfctr_msr;
  589. wd->evntsel_msr = evntsel_msr;
  590. wd->cccr_msr = 0; //unused
  591. wd->check_bit = 1ULL << (eax.split.bit_width - 1);
  592. return 1;
  593. fail1:
  594. release_perfctr_nmi(perfctr_msr);
  595. fail:
  596. return 0;
  597. }
  598. static void stop_intel_arch_watchdog(void)
  599. {
  600. unsigned int ebx;
  601. union cpuid10_eax eax;
  602. unsigned int unused;
  603. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  604. /*
  605. * Check whether the Architectural PerfMon supports
  606. * Unhalted Core Cycles Event or not.
  607. * NOTE: Corresponding bit = 0 in ebx indicates event present.
  608. */
  609. cpuid(10, &(eax.full), &ebx, &unused, &unused);
  610. if ((eax.split.mask_length < (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX+1)) ||
  611. (ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT))
  612. return;
  613. wrmsr(wd->evntsel_msr, 0, 0);
  614. release_evntsel_nmi(wd->evntsel_msr);
  615. release_perfctr_nmi(wd->perfctr_msr);
  616. }
  617. void setup_apic_nmi_watchdog (void *unused)
  618. {
  619. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  620. /* only support LOCAL and IO APICs for now */
  621. if ((nmi_watchdog != NMI_LOCAL_APIC) &&
  622. (nmi_watchdog != NMI_IO_APIC))
  623. return;
  624. if (wd->enabled == 1)
  625. return;
  626. /* cheap hack to support suspend/resume */
  627. /* if cpu0 is not active neither should the other cpus */
  628. if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
  629. return;
  630. if (nmi_watchdog == NMI_LOCAL_APIC) {
  631. switch (boot_cpu_data.x86_vendor) {
  632. case X86_VENDOR_AMD:
  633. if (boot_cpu_data.x86 != 6 && boot_cpu_data.x86 != 15)
  634. return;
  635. if (!setup_k7_watchdog())
  636. return;
  637. break;
  638. case X86_VENDOR_INTEL:
  639. if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
  640. if (!setup_intel_arch_watchdog())
  641. return;
  642. break;
  643. }
  644. switch (boot_cpu_data.x86) {
  645. case 6:
  646. if (boot_cpu_data.x86_model > 0xd)
  647. return;
  648. if (!setup_p6_watchdog())
  649. return;
  650. break;
  651. case 15:
  652. if (boot_cpu_data.x86_model > 0x4)
  653. return;
  654. if (!setup_p4_watchdog())
  655. return;
  656. break;
  657. default:
  658. return;
  659. }
  660. break;
  661. default:
  662. return;
  663. }
  664. }
  665. wd->enabled = 1;
  666. atomic_inc(&nmi_active);
  667. }
  668. void stop_apic_nmi_watchdog(void *unused)
  669. {
  670. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  671. /* only support LOCAL and IO APICs for now */
  672. if ((nmi_watchdog != NMI_LOCAL_APIC) &&
  673. (nmi_watchdog != NMI_IO_APIC))
  674. return;
  675. if (wd->enabled == 0)
  676. return;
  677. if (nmi_watchdog == NMI_LOCAL_APIC) {
  678. switch (boot_cpu_data.x86_vendor) {
  679. case X86_VENDOR_AMD:
  680. stop_k7_watchdog();
  681. break;
  682. case X86_VENDOR_INTEL:
  683. if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
  684. stop_intel_arch_watchdog();
  685. break;
  686. }
  687. switch (boot_cpu_data.x86) {
  688. case 6:
  689. if (boot_cpu_data.x86_model > 0xd)
  690. break;
  691. stop_p6_watchdog();
  692. break;
  693. case 15:
  694. if (boot_cpu_data.x86_model > 0x4)
  695. break;
  696. stop_p4_watchdog();
  697. break;
  698. }
  699. break;
  700. default:
  701. return;
  702. }
  703. }
  704. wd->enabled = 0;
  705. atomic_dec(&nmi_active);
  706. }
  707. /*
  708. * the best way to detect whether a CPU has a 'hard lockup' problem
  709. * is to check it's local APIC timer IRQ counts. If they are not
  710. * changing then that CPU has some problem.
  711. *
  712. * as these watchdog NMI IRQs are generated on every CPU, we only
  713. * have to check the current processor.
  714. *
  715. * since NMIs don't listen to _any_ locks, we have to be extremely
  716. * careful not to rely on unsafe variables. The printk might lock
  717. * up though, so we have to break up any console locks first ...
  718. * [when there will be more tty-related locks, break them up
  719. * here too!]
  720. */
  721. static unsigned int
  722. last_irq_sums [NR_CPUS],
  723. alert_counter [NR_CPUS];
  724. void touch_nmi_watchdog (void)
  725. {
  726. int i;
  727. /*
  728. * Just reset the alert counters, (other CPUs might be
  729. * spinning on locks we hold):
  730. */
  731. for_each_possible_cpu(i)
  732. alert_counter[i] = 0;
  733. /*
  734. * Tickle the softlockup detector too:
  735. */
  736. touch_softlockup_watchdog();
  737. }
  738. EXPORT_SYMBOL(touch_nmi_watchdog);
  739. extern void die_nmi(struct pt_regs *, const char *msg);
  740. __kprobes int nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
  741. {
  742. /*
  743. * Since current_thread_info()-> is always on the stack, and we
  744. * always switch the stack NMI-atomically, it's safe to use
  745. * smp_processor_id().
  746. */
  747. unsigned int sum;
  748. int touched = 0;
  749. int cpu = smp_processor_id();
  750. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  751. u64 dummy;
  752. int rc=0;
  753. /* check for other users first */
  754. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
  755. == NOTIFY_STOP) {
  756. rc = 1;
  757. touched = 1;
  758. }
  759. sum = per_cpu(irq_stat, cpu).apic_timer_irqs;
  760. /* if the apic timer isn't firing, this cpu isn't doing much */
  761. if (!touched && last_irq_sums[cpu] == sum) {
  762. /*
  763. * Ayiee, looks like this CPU is stuck ...
  764. * wait a few IRQs (5 seconds) before doing the oops ...
  765. */
  766. alert_counter[cpu]++;
  767. if (alert_counter[cpu] == 5*nmi_hz)
  768. /*
  769. * die_nmi will return ONLY if NOTIFY_STOP happens..
  770. */
  771. die_nmi(regs, "BUG: NMI Watchdog detected LOCKUP");
  772. } else {
  773. last_irq_sums[cpu] = sum;
  774. alert_counter[cpu] = 0;
  775. }
  776. /* see if the nmi watchdog went off */
  777. if (wd->enabled) {
  778. if (nmi_watchdog == NMI_LOCAL_APIC) {
  779. rdmsrl(wd->perfctr_msr, dummy);
  780. if (dummy & wd->check_bit){
  781. /* this wasn't a watchdog timer interrupt */
  782. goto done;
  783. }
  784. /* only Intel P4 uses the cccr msr */
  785. if (wd->cccr_msr != 0) {
  786. /*
  787. * P4 quirks:
  788. * - An overflown perfctr will assert its interrupt
  789. * until the OVF flag in its CCCR is cleared.
  790. * - LVTPC is masked on interrupt and must be
  791. * unmasked by the LVTPC handler.
  792. */
  793. rdmsrl(wd->cccr_msr, dummy);
  794. dummy &= ~P4_CCCR_OVF;
  795. wrmsrl(wd->cccr_msr, dummy);
  796. apic_write(APIC_LVTPC, APIC_DM_NMI);
  797. }
  798. else if (wd->perfctr_msr == MSR_P6_PERFCTR0 ||
  799. wd->perfctr_msr == MSR_ARCH_PERFMON_PERFCTR0) {
  800. /* P6 based Pentium M need to re-unmask
  801. * the apic vector but it doesn't hurt
  802. * other P6 variant.
  803. * ArchPerfom/Core Duo also needs this */
  804. apic_write(APIC_LVTPC, APIC_DM_NMI);
  805. }
  806. /* start the cycle over again */
  807. write_watchdog_counter(wd->perfctr_msr, NULL);
  808. rc = 1;
  809. } else if (nmi_watchdog == NMI_IO_APIC) {
  810. /* don't know how to accurately check for this.
  811. * just assume it was a watchdog timer interrupt
  812. * This matches the old behaviour.
  813. */
  814. rc = 1;
  815. }
  816. }
  817. done:
  818. return rc;
  819. }
  820. int do_nmi_callback(struct pt_regs * regs, int cpu)
  821. {
  822. #ifdef CONFIG_SYSCTL
  823. if (unknown_nmi_panic)
  824. return unknown_nmi_panic_callback(regs, cpu);
  825. #endif
  826. return 0;
  827. }
  828. #ifdef CONFIG_SYSCTL
  829. static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
  830. {
  831. unsigned char reason = get_nmi_reason();
  832. char buf[64];
  833. sprintf(buf, "NMI received for unknown reason %02x\n", reason);
  834. die_nmi(regs, buf);
  835. return 0;
  836. }
  837. /*
  838. * proc handler for /proc/sys/kernel/nmi
  839. */
  840. int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
  841. void __user *buffer, size_t *length, loff_t *ppos)
  842. {
  843. int old_state;
  844. nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
  845. old_state = nmi_watchdog_enabled;
  846. proc_dointvec(table, write, file, buffer, length, ppos);
  847. if (!!old_state == !!nmi_watchdog_enabled)
  848. return 0;
  849. if (atomic_read(&nmi_active) < 0) {
  850. printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
  851. return -EIO;
  852. }
  853. if (nmi_watchdog == NMI_DEFAULT) {
  854. if (nmi_known_cpu() > 0)
  855. nmi_watchdog = NMI_LOCAL_APIC;
  856. else
  857. nmi_watchdog = NMI_IO_APIC;
  858. }
  859. if (nmi_watchdog == NMI_LOCAL_APIC) {
  860. if (nmi_watchdog_enabled)
  861. enable_lapic_nmi_watchdog();
  862. else
  863. disable_lapic_nmi_watchdog();
  864. } else {
  865. printk( KERN_WARNING
  866. "NMI watchdog doesn't know what hardware to touch\n");
  867. return -EIO;
  868. }
  869. return 0;
  870. }
  871. #endif
  872. EXPORT_SYMBOL(nmi_active);
  873. EXPORT_SYMBOL(nmi_watchdog);
  874. EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi);
  875. EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi_bit);
  876. EXPORT_SYMBOL(reserve_perfctr_nmi);
  877. EXPORT_SYMBOL(release_perfctr_nmi);
  878. EXPORT_SYMBOL(reserve_evntsel_nmi);
  879. EXPORT_SYMBOL(release_evntsel_nmi);
  880. EXPORT_SYMBOL(disable_timer_nmi_watchdog);
  881. EXPORT_SYMBOL(enable_timer_nmi_watchdog);