nmi.c 25 KB

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