nmi.c 29 KB

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