nmi.c 26 KB

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