nmi.c 28 KB

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