nmi.c 26 KB

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