nmi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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/config.h>
  16. #include <linux/delay.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/module.h>
  19. #include <linux/nmi.h>
  20. #include <linux/sysdev.h>
  21. #include <linux/sysctl.h>
  22. #include <linux/percpu.h>
  23. #include <asm/smp.h>
  24. #include <asm/nmi.h>
  25. #include <asm/kdebug.h>
  26. #include "mach_traps.h"
  27. /* perfctr_nmi_owner tracks the ownership of the perfctr registers:
  28. * evtsel_nmi_owner tracks the ownership of the event selection
  29. * - different performance counters/ event selection may be reserved for
  30. * different subsystems this reservation system just tries to coordinate
  31. * things a little
  32. */
  33. static DEFINE_PER_CPU(unsigned long, perfctr_nmi_owner);
  34. static DEFINE_PER_CPU(unsigned long, evntsel_nmi_owner[3]);
  35. /* this number is calculated from Intel's MSR_P4_CRU_ESCR5 register and it's
  36. * offset from MSR_P4_BSU_ESCR0. It will be the max for all platforms (for now)
  37. */
  38. #define NMI_MAX_COUNTER_BITS 66
  39. /* nmi_active:
  40. * >0: the lapic NMI watchdog is active, but can be disabled
  41. * <0: the lapic NMI watchdog has not been set up, and cannot
  42. * be enabled
  43. * 0: the lapic NMI watchdog is disabled, but can be enabled
  44. */
  45. atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
  46. unsigned int nmi_watchdog = NMI_DEFAULT;
  47. static unsigned int nmi_hz = HZ;
  48. struct nmi_watchdog_ctlblk {
  49. int enabled;
  50. u64 check_bit;
  51. unsigned int cccr_msr;
  52. unsigned int perfctr_msr; /* the MSR to reset in NMI handler */
  53. unsigned int evntsel_msr; /* the MSR to select the events to handle */
  54. };
  55. static DEFINE_PER_CPU(struct nmi_watchdog_ctlblk, nmi_watchdog_ctlblk);
  56. /* local prototypes */
  57. static void stop_apic_nmi_watchdog(void *unused);
  58. static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu);
  59. extern void show_registers(struct pt_regs *regs);
  60. extern int unknown_nmi_panic;
  61. /* converts an msr to an appropriate reservation bit */
  62. static inline unsigned int nmi_perfctr_msr_to_bit(unsigned int msr)
  63. {
  64. /* returns the bit offset of the performance counter register */
  65. switch (boot_cpu_data.x86_vendor) {
  66. case X86_VENDOR_AMD:
  67. return (msr - MSR_K7_PERFCTR0);
  68. case X86_VENDOR_INTEL:
  69. switch (boot_cpu_data.x86) {
  70. case 6:
  71. return (msr - MSR_P6_PERFCTR0);
  72. case 15:
  73. return (msr - MSR_P4_BPU_PERFCTR0);
  74. }
  75. }
  76. return 0;
  77. }
  78. /* converts an msr to an appropriate reservation bit */
  79. static inline unsigned int nmi_evntsel_msr_to_bit(unsigned int msr)
  80. {
  81. /* returns the bit offset of the event selection register */
  82. switch (boot_cpu_data.x86_vendor) {
  83. case X86_VENDOR_AMD:
  84. return (msr - MSR_K7_EVNTSEL0);
  85. case X86_VENDOR_INTEL:
  86. switch (boot_cpu_data.x86) {
  87. case 6:
  88. return (msr - MSR_P6_EVNTSEL0);
  89. case 15:
  90. return (msr - MSR_P4_BSU_ESCR0);
  91. }
  92. }
  93. return 0;
  94. }
  95. /* checks for a bit availability (hack for oprofile) */
  96. int avail_to_resrv_perfctr_nmi_bit(unsigned int counter)
  97. {
  98. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  99. return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner)));
  100. }
  101. /* checks the an msr for availability */
  102. int avail_to_resrv_perfctr_nmi(unsigned int msr)
  103. {
  104. unsigned int counter;
  105. counter = nmi_perfctr_msr_to_bit(msr);
  106. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  107. return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner)));
  108. }
  109. int reserve_perfctr_nmi(unsigned int msr)
  110. {
  111. unsigned int counter;
  112. counter = nmi_perfctr_msr_to_bit(msr);
  113. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  114. if (!test_and_set_bit(counter, &__get_cpu_var(perfctr_nmi_owner)))
  115. return 1;
  116. return 0;
  117. }
  118. void release_perfctr_nmi(unsigned int msr)
  119. {
  120. unsigned int counter;
  121. counter = nmi_perfctr_msr_to_bit(msr);
  122. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  123. clear_bit(counter, &__get_cpu_var(perfctr_nmi_owner));
  124. }
  125. int reserve_evntsel_nmi(unsigned int msr)
  126. {
  127. unsigned int counter;
  128. counter = nmi_evntsel_msr_to_bit(msr);
  129. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  130. if (!test_and_set_bit(counter, &__get_cpu_var(evntsel_nmi_owner)[0]))
  131. return 1;
  132. return 0;
  133. }
  134. void release_evntsel_nmi(unsigned int msr)
  135. {
  136. unsigned int counter;
  137. counter = nmi_evntsel_msr_to_bit(msr);
  138. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  139. clear_bit(counter, &__get_cpu_var(evntsel_nmi_owner)[0]);
  140. }
  141. static __cpuinit inline int nmi_known_cpu(void)
  142. {
  143. switch (boot_cpu_data.x86_vendor) {
  144. case X86_VENDOR_AMD:
  145. return ((boot_cpu_data.x86 == 15) || (boot_cpu_data.x86 == 6));
  146. case X86_VENDOR_INTEL:
  147. return ((boot_cpu_data.x86 == 15) || (boot_cpu_data.x86 == 6));
  148. }
  149. return 0;
  150. }
  151. #ifdef CONFIG_SMP
  152. /* The performance counters used by NMI_LOCAL_APIC don't trigger when
  153. * the CPU is idle. To make sure the NMI watchdog really ticks on all
  154. * CPUs during the test make them busy.
  155. */
  156. static __init void nmi_cpu_busy(void *data)
  157. {
  158. volatile int *endflag = data;
  159. local_irq_enable_in_hardirq();
  160. /* Intentionally don't use cpu_relax here. This is
  161. to make sure that the performance counter really ticks,
  162. even if there is a simulator or similar that catches the
  163. pause instruction. On a real HT machine this is fine because
  164. all other CPUs are busy with "useless" delay loops and don't
  165. care if they get somewhat less cycles. */
  166. while (*endflag == 0)
  167. barrier();
  168. }
  169. #endif
  170. static int __init check_nmi_watchdog(void)
  171. {
  172. volatile int endflag = 0;
  173. unsigned int *prev_nmi_count;
  174. int cpu;
  175. if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DEFAULT))
  176. return 0;
  177. if (!atomic_read(&nmi_active))
  178. return 0;
  179. prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
  180. if (!prev_nmi_count)
  181. return -1;
  182. printk(KERN_INFO "Testing NMI watchdog ... ");
  183. if (nmi_watchdog == NMI_LOCAL_APIC)
  184. smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
  185. for_each_possible_cpu(cpu)
  186. prev_nmi_count[cpu] = per_cpu(irq_stat, cpu).__nmi_count;
  187. local_irq_enable();
  188. mdelay((10*1000)/nmi_hz); // wait 10 ticks
  189. for_each_possible_cpu(cpu) {
  190. #ifdef CONFIG_SMP
  191. /* Check cpu_callin_map here because that is set
  192. after the timer is started. */
  193. if (!cpu_isset(cpu, cpu_callin_map))
  194. continue;
  195. #endif
  196. if (!per_cpu(nmi_watchdog_ctlblk, cpu).enabled)
  197. continue;
  198. if (nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
  199. printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n",
  200. cpu,
  201. prev_nmi_count[cpu],
  202. nmi_count(cpu));
  203. per_cpu(nmi_watchdog_ctlblk, cpu).enabled = 0;
  204. atomic_dec(&nmi_active);
  205. }
  206. }
  207. if (!atomic_read(&nmi_active)) {
  208. kfree(prev_nmi_count);
  209. atomic_set(&nmi_active, -1);
  210. return -1;
  211. }
  212. endflag = 1;
  213. printk("OK.\n");
  214. /* now that we know it works we can reduce NMI frequency to
  215. something more reasonable; makes a difference in some configs */
  216. if (nmi_watchdog == NMI_LOCAL_APIC)
  217. nmi_hz = 1;
  218. kfree(prev_nmi_count);
  219. return 0;
  220. }
  221. /* This needs to happen later in boot so counters are working */
  222. late_initcall(check_nmi_watchdog);
  223. static int __init setup_nmi_watchdog(char *str)
  224. {
  225. int nmi;
  226. get_option(&str, &nmi);
  227. if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
  228. return 0;
  229. /*
  230. * If any other x86 CPU has a local APIC, then
  231. * please test the NMI stuff there and send me the
  232. * missing bits. Right now Intel P6/P4 and AMD K7 only.
  233. */
  234. if ((nmi == NMI_LOCAL_APIC) && (nmi_known_cpu() == 0))
  235. return 0; /* no lapic support */
  236. nmi_watchdog = nmi;
  237. return 1;
  238. }
  239. __setup("nmi_watchdog=", setup_nmi_watchdog);
  240. static void disable_lapic_nmi_watchdog(void)
  241. {
  242. BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
  243. if (atomic_read(&nmi_active) <= 0)
  244. return;
  245. on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
  246. BUG_ON(atomic_read(&nmi_active) != 0);
  247. }
  248. static void enable_lapic_nmi_watchdog(void)
  249. {
  250. BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
  251. /* are we already enabled */
  252. if (atomic_read(&nmi_active) != 0)
  253. return;
  254. /* are we lapic aware */
  255. if (nmi_known_cpu() <= 0)
  256. return;
  257. on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
  258. touch_nmi_watchdog();
  259. }
  260. void disable_timer_nmi_watchdog(void)
  261. {
  262. BUG_ON(nmi_watchdog != NMI_IO_APIC);
  263. if (atomic_read(&nmi_active) <= 0)
  264. return;
  265. disable_irq(0);
  266. on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
  267. BUG_ON(atomic_read(&nmi_active) != 0);
  268. }
  269. void enable_timer_nmi_watchdog(void)
  270. {
  271. BUG_ON(nmi_watchdog != NMI_IO_APIC);
  272. if (atomic_read(&nmi_active) == 0) {
  273. touch_nmi_watchdog();
  274. on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
  275. enable_irq(0);
  276. }
  277. }
  278. #ifdef CONFIG_PM
  279. static int nmi_pm_active; /* nmi_active before suspend */
  280. static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
  281. {
  282. nmi_pm_active = atomic_read(&nmi_active);
  283. disable_lapic_nmi_watchdog();
  284. return 0;
  285. }
  286. static int lapic_nmi_resume(struct sys_device *dev)
  287. {
  288. if (nmi_pm_active > 0)
  289. enable_lapic_nmi_watchdog();
  290. return 0;
  291. }
  292. static struct sysdev_class nmi_sysclass = {
  293. set_kset_name("lapic_nmi"),
  294. .resume = lapic_nmi_resume,
  295. .suspend = lapic_nmi_suspend,
  296. };
  297. static struct sys_device device_lapic_nmi = {
  298. .id = 0,
  299. .cls = &nmi_sysclass,
  300. };
  301. static int __init init_lapic_nmi_sysfs(void)
  302. {
  303. int error;
  304. /* should really be a BUG_ON but b/c this is an
  305. * init call, it just doesn't work. -dcz
  306. */
  307. if (nmi_watchdog != NMI_LOCAL_APIC)
  308. return 0;
  309. if ( atomic_read(&nmi_active) < 0 )
  310. return 0;
  311. error = sysdev_class_register(&nmi_sysclass);
  312. if (!error)
  313. error = sysdev_register(&device_lapic_nmi);
  314. return error;
  315. }
  316. /* must come after the local APIC's device_initcall() */
  317. late_initcall(init_lapic_nmi_sysfs);
  318. #endif /* CONFIG_PM */
  319. /*
  320. * Activate the NMI watchdog via the local APIC.
  321. * Original code written by Keith Owens.
  322. */
  323. static void write_watchdog_counter(unsigned int perfctr_msr, const char *descr)
  324. {
  325. u64 count = (u64)cpu_khz * 1000;
  326. do_div(count, nmi_hz);
  327. if(descr)
  328. Dprintk("setting %s to -0x%08Lx\n", descr, count);
  329. wrmsrl(perfctr_msr, 0 - count);
  330. }
  331. /* Note that these events don't tick when the CPU idles. This means
  332. the frequency varies with CPU load. */
  333. #define K7_EVNTSEL_ENABLE (1 << 22)
  334. #define K7_EVNTSEL_INT (1 << 20)
  335. #define K7_EVNTSEL_OS (1 << 17)
  336. #define K7_EVNTSEL_USR (1 << 16)
  337. #define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
  338. #define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
  339. static int setup_k7_watchdog(void)
  340. {
  341. unsigned int perfctr_msr, evntsel_msr;
  342. unsigned int evntsel;
  343. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  344. perfctr_msr = MSR_K7_PERFCTR0;
  345. evntsel_msr = MSR_K7_EVNTSEL0;
  346. if (!reserve_perfctr_nmi(perfctr_msr))
  347. goto fail;
  348. if (!reserve_evntsel_nmi(evntsel_msr))
  349. goto fail1;
  350. wrmsrl(perfctr_msr, 0UL);
  351. evntsel = K7_EVNTSEL_INT
  352. | K7_EVNTSEL_OS
  353. | K7_EVNTSEL_USR
  354. | K7_NMI_EVENT;
  355. /* setup the timer */
  356. wrmsr(evntsel_msr, evntsel, 0);
  357. write_watchdog_counter(perfctr_msr, "K7_PERFCTR0");
  358. apic_write(APIC_LVTPC, APIC_DM_NMI);
  359. evntsel |= K7_EVNTSEL_ENABLE;
  360. wrmsr(evntsel_msr, evntsel, 0);
  361. wd->perfctr_msr = perfctr_msr;
  362. wd->evntsel_msr = evntsel_msr;
  363. wd->cccr_msr = 0; //unused
  364. wd->check_bit = 1ULL<<63;
  365. return 1;
  366. fail1:
  367. release_perfctr_nmi(perfctr_msr);
  368. fail:
  369. return 0;
  370. }
  371. static void stop_k7_watchdog(void)
  372. {
  373. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  374. wrmsr(wd->evntsel_msr, 0, 0);
  375. release_evntsel_nmi(wd->evntsel_msr);
  376. release_perfctr_nmi(wd->perfctr_msr);
  377. }
  378. #define P6_EVNTSEL0_ENABLE (1 << 22)
  379. #define P6_EVNTSEL_INT (1 << 20)
  380. #define P6_EVNTSEL_OS (1 << 17)
  381. #define P6_EVNTSEL_USR (1 << 16)
  382. #define P6_EVENT_CPU_CLOCKS_NOT_HALTED 0x79
  383. #define P6_NMI_EVENT P6_EVENT_CPU_CLOCKS_NOT_HALTED
  384. static int setup_p6_watchdog(void)
  385. {
  386. unsigned int perfctr_msr, evntsel_msr;
  387. unsigned int evntsel;
  388. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  389. perfctr_msr = MSR_P6_PERFCTR0;
  390. evntsel_msr = MSR_P6_EVNTSEL0;
  391. if (!reserve_perfctr_nmi(perfctr_msr))
  392. goto fail;
  393. if (!reserve_evntsel_nmi(evntsel_msr))
  394. goto fail1;
  395. wrmsrl(perfctr_msr, 0UL);
  396. evntsel = P6_EVNTSEL_INT
  397. | P6_EVNTSEL_OS
  398. | P6_EVNTSEL_USR
  399. | P6_NMI_EVENT;
  400. /* setup the timer */
  401. wrmsr(evntsel_msr, evntsel, 0);
  402. write_watchdog_counter(perfctr_msr, "P6_PERFCTR0");
  403. apic_write(APIC_LVTPC, APIC_DM_NMI);
  404. evntsel |= P6_EVNTSEL0_ENABLE;
  405. wrmsr(evntsel_msr, evntsel, 0);
  406. wd->perfctr_msr = perfctr_msr;
  407. wd->evntsel_msr = evntsel_msr;
  408. wd->cccr_msr = 0; //unused
  409. wd->check_bit = 1ULL<<39;
  410. return 1;
  411. fail1:
  412. release_perfctr_nmi(perfctr_msr);
  413. fail:
  414. return 0;
  415. }
  416. static void stop_p6_watchdog(void)
  417. {
  418. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  419. wrmsr(wd->evntsel_msr, 0, 0);
  420. release_evntsel_nmi(wd->evntsel_msr);
  421. release_perfctr_nmi(wd->perfctr_msr);
  422. }
  423. /* Note that these events don't tick when the CPU idles. This means
  424. the frequency varies with CPU load. */
  425. #define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7)
  426. #define P4_ESCR_EVENT_SELECT(N) ((N)<<25)
  427. #define P4_ESCR_OS (1<<3)
  428. #define P4_ESCR_USR (1<<2)
  429. #define P4_CCCR_OVF_PMI0 (1<<26)
  430. #define P4_CCCR_OVF_PMI1 (1<<27)
  431. #define P4_CCCR_THRESHOLD(N) ((N)<<20)
  432. #define P4_CCCR_COMPLEMENT (1<<19)
  433. #define P4_CCCR_COMPARE (1<<18)
  434. #define P4_CCCR_REQUIRED (3<<16)
  435. #define P4_CCCR_ESCR_SELECT(N) ((N)<<13)
  436. #define P4_CCCR_ENABLE (1<<12)
  437. #define P4_CCCR_OVF (1<<31)
  438. /* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
  439. CRU_ESCR0 (with any non-null event selector) through a complemented
  440. max threshold. [IA32-Vol3, Section 14.9.9] */
  441. static int setup_p4_watchdog(void)
  442. {
  443. unsigned int perfctr_msr, evntsel_msr, cccr_msr;
  444. unsigned int evntsel, cccr_val;
  445. unsigned int misc_enable, dummy;
  446. unsigned int ht_num;
  447. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  448. rdmsr(MSR_IA32_MISC_ENABLE, misc_enable, dummy);
  449. if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
  450. return 0;
  451. #ifdef CONFIG_SMP
  452. /* detect which hyperthread we are on */
  453. if (smp_num_siblings == 2) {
  454. unsigned int ebx, apicid;
  455. ebx = cpuid_ebx(1);
  456. apicid = (ebx >> 24) & 0xff;
  457. ht_num = apicid & 1;
  458. } else
  459. #endif
  460. ht_num = 0;
  461. /* performance counters are shared resources
  462. * assign each hyperthread its own set
  463. * (re-use the ESCR0 register, seems safe
  464. * and keeps the cccr_val the same)
  465. */
  466. if (!ht_num) {
  467. /* logical cpu 0 */
  468. perfctr_msr = MSR_P4_IQ_PERFCTR0;
  469. evntsel_msr = MSR_P4_CRU_ESCR0;
  470. cccr_msr = MSR_P4_IQ_CCCR0;
  471. cccr_val = P4_CCCR_OVF_PMI0 | P4_CCCR_ESCR_SELECT(4);
  472. } else {
  473. /* logical cpu 1 */
  474. perfctr_msr = MSR_P4_IQ_PERFCTR1;
  475. evntsel_msr = MSR_P4_CRU_ESCR0;
  476. cccr_msr = MSR_P4_IQ_CCCR1;
  477. cccr_val = P4_CCCR_OVF_PMI1 | P4_CCCR_ESCR_SELECT(4);
  478. }
  479. if (!reserve_perfctr_nmi(perfctr_msr))
  480. goto fail;
  481. if (!reserve_evntsel_nmi(evntsel_msr))
  482. goto fail1;
  483. evntsel = P4_ESCR_EVENT_SELECT(0x3F)
  484. | P4_ESCR_OS
  485. | P4_ESCR_USR;
  486. cccr_val |= P4_CCCR_THRESHOLD(15)
  487. | P4_CCCR_COMPLEMENT
  488. | P4_CCCR_COMPARE
  489. | P4_CCCR_REQUIRED;
  490. wrmsr(evntsel_msr, evntsel, 0);
  491. wrmsr(cccr_msr, cccr_val, 0);
  492. write_watchdog_counter(perfctr_msr, "P4_IQ_COUNTER0");
  493. apic_write(APIC_LVTPC, APIC_DM_NMI);
  494. cccr_val |= P4_CCCR_ENABLE;
  495. wrmsr(cccr_msr, cccr_val, 0);
  496. wd->perfctr_msr = perfctr_msr;
  497. wd->evntsel_msr = evntsel_msr;
  498. wd->cccr_msr = cccr_msr;
  499. wd->check_bit = 1ULL<<39;
  500. return 1;
  501. fail1:
  502. release_perfctr_nmi(perfctr_msr);
  503. fail:
  504. return 0;
  505. }
  506. static void stop_p4_watchdog(void)
  507. {
  508. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  509. wrmsr(wd->cccr_msr, 0, 0);
  510. wrmsr(wd->evntsel_msr, 0, 0);
  511. release_evntsel_nmi(wd->evntsel_msr);
  512. release_perfctr_nmi(wd->perfctr_msr);
  513. }
  514. void setup_apic_nmi_watchdog (void *unused)
  515. {
  516. /* only support LOCAL and IO APICs for now */
  517. if ((nmi_watchdog != NMI_LOCAL_APIC) &&
  518. (nmi_watchdog != NMI_IO_APIC))
  519. return;
  520. if (nmi_watchdog == NMI_LOCAL_APIC) {
  521. switch (boot_cpu_data.x86_vendor) {
  522. case X86_VENDOR_AMD:
  523. if (boot_cpu_data.x86 != 6 && boot_cpu_data.x86 != 15)
  524. return;
  525. if (!setup_k7_watchdog())
  526. return;
  527. break;
  528. case X86_VENDOR_INTEL:
  529. switch (boot_cpu_data.x86) {
  530. case 6:
  531. if (boot_cpu_data.x86_model > 0xd)
  532. return;
  533. if (!setup_p6_watchdog())
  534. return;
  535. break;
  536. case 15:
  537. if (boot_cpu_data.x86_model > 0x4)
  538. return;
  539. if (!setup_p4_watchdog())
  540. return;
  541. break;
  542. default:
  543. return;
  544. }
  545. break;
  546. default:
  547. return;
  548. }
  549. }
  550. __get_cpu_var(nmi_watchdog_ctlblk.enabled) = 1;
  551. atomic_inc(&nmi_active);
  552. }
  553. static void stop_apic_nmi_watchdog(void *unused)
  554. {
  555. /* only support LOCAL and IO APICs for now */
  556. if ((nmi_watchdog != NMI_LOCAL_APIC) &&
  557. (nmi_watchdog != NMI_IO_APIC))
  558. return;
  559. if (nmi_watchdog == NMI_LOCAL_APIC) {
  560. switch (boot_cpu_data.x86_vendor) {
  561. case X86_VENDOR_AMD:
  562. stop_k7_watchdog();
  563. break;
  564. case X86_VENDOR_INTEL:
  565. switch (boot_cpu_data.x86) {
  566. case 6:
  567. if (boot_cpu_data.x86_model > 0xd)
  568. break;
  569. stop_p6_watchdog();
  570. break;
  571. case 15:
  572. if (boot_cpu_data.x86_model > 0x4)
  573. break;
  574. stop_p4_watchdog();
  575. break;
  576. }
  577. break;
  578. default:
  579. return;
  580. }
  581. }
  582. __get_cpu_var(nmi_watchdog_ctlblk.enabled) = 0;
  583. atomic_dec(&nmi_active);
  584. }
  585. /*
  586. * the best way to detect whether a CPU has a 'hard lockup' problem
  587. * is to check it's local APIC timer IRQ counts. If they are not
  588. * changing then that CPU has some problem.
  589. *
  590. * as these watchdog NMI IRQs are generated on every CPU, we only
  591. * have to check the current processor.
  592. *
  593. * since NMIs don't listen to _any_ locks, we have to be extremely
  594. * careful not to rely on unsafe variables. The printk might lock
  595. * up though, so we have to break up any console locks first ...
  596. * [when there will be more tty-related locks, break them up
  597. * here too!]
  598. */
  599. static unsigned int
  600. last_irq_sums [NR_CPUS],
  601. alert_counter [NR_CPUS];
  602. void touch_nmi_watchdog (void)
  603. {
  604. int i;
  605. /*
  606. * Just reset the alert counters, (other CPUs might be
  607. * spinning on locks we hold):
  608. */
  609. for_each_possible_cpu(i)
  610. alert_counter[i] = 0;
  611. /*
  612. * Tickle the softlockup detector too:
  613. */
  614. touch_softlockup_watchdog();
  615. }
  616. EXPORT_SYMBOL(touch_nmi_watchdog);
  617. extern void die_nmi(struct pt_regs *, const char *msg);
  618. int nmi_watchdog_tick (struct pt_regs * regs, unsigned reason)
  619. {
  620. /*
  621. * Since current_thread_info()-> is always on the stack, and we
  622. * always switch the stack NMI-atomically, it's safe to use
  623. * smp_processor_id().
  624. */
  625. unsigned int sum;
  626. int touched = 0;
  627. int cpu = smp_processor_id();
  628. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  629. u64 dummy;
  630. int rc=0;
  631. /* check for other users first */
  632. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
  633. == NOTIFY_STOP) {
  634. rc = 1;
  635. touched = 1;
  636. }
  637. sum = per_cpu(irq_stat, cpu).apic_timer_irqs;
  638. /* if the apic timer isn't firing, this cpu isn't doing much */
  639. if (!touched && last_irq_sums[cpu] == sum) {
  640. /*
  641. * Ayiee, looks like this CPU is stuck ...
  642. * wait a few IRQs (5 seconds) before doing the oops ...
  643. */
  644. alert_counter[cpu]++;
  645. if (alert_counter[cpu] == 5*nmi_hz)
  646. /*
  647. * die_nmi will return ONLY if NOTIFY_STOP happens..
  648. */
  649. die_nmi(regs, "BUG: NMI Watchdog detected LOCKUP");
  650. } else {
  651. last_irq_sums[cpu] = sum;
  652. alert_counter[cpu] = 0;
  653. }
  654. /* see if the nmi watchdog went off */
  655. if (wd->enabled) {
  656. if (nmi_watchdog == NMI_LOCAL_APIC) {
  657. rdmsrl(wd->perfctr_msr, dummy);
  658. if (dummy & wd->check_bit){
  659. /* this wasn't a watchdog timer interrupt */
  660. goto done;
  661. }
  662. /* only Intel P4 uses the cccr msr */
  663. if (wd->cccr_msr != 0) {
  664. /*
  665. * P4 quirks:
  666. * - An overflown perfctr will assert its interrupt
  667. * until the OVF flag in its CCCR is cleared.
  668. * - LVTPC is masked on interrupt and must be
  669. * unmasked by the LVTPC handler.
  670. */
  671. rdmsrl(wd->cccr_msr, dummy);
  672. dummy &= ~P4_CCCR_OVF;
  673. wrmsrl(wd->cccr_msr, dummy);
  674. apic_write(APIC_LVTPC, APIC_DM_NMI);
  675. }
  676. else if (wd->perfctr_msr == MSR_P6_PERFCTR0) {
  677. /* Only P6 based Pentium M need to re-unmask
  678. * the apic vector but it doesn't hurt
  679. * other P6 variant */
  680. apic_write(APIC_LVTPC, APIC_DM_NMI);
  681. }
  682. /* start the cycle over again */
  683. write_watchdog_counter(wd->perfctr_msr, NULL);
  684. rc = 1;
  685. } else if (nmi_watchdog == NMI_IO_APIC) {
  686. /* don't know how to accurately check for this.
  687. * just assume it was a watchdog timer interrupt
  688. * This matches the old behaviour.
  689. */
  690. rc = 1;
  691. } else
  692. printk(KERN_WARNING "Unknown enabled NMI hardware?!\n");
  693. }
  694. done:
  695. return rc;
  696. }
  697. int do_nmi_callback(struct pt_regs * regs, int cpu)
  698. {
  699. #ifdef CONFIG_SYSCTL
  700. if (unknown_nmi_panic)
  701. return unknown_nmi_panic_callback(regs, cpu);
  702. #endif
  703. return 0;
  704. }
  705. #ifdef CONFIG_SYSCTL
  706. static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
  707. {
  708. unsigned char reason = get_nmi_reason();
  709. char buf[64];
  710. sprintf(buf, "NMI received for unknown reason %02x\n", reason);
  711. die_nmi(regs, buf);
  712. return 0;
  713. }
  714. #endif
  715. EXPORT_SYMBOL(nmi_active);
  716. EXPORT_SYMBOL(nmi_watchdog);
  717. EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi);
  718. EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi_bit);
  719. EXPORT_SYMBOL(reserve_perfctr_nmi);
  720. EXPORT_SYMBOL(release_perfctr_nmi);
  721. EXPORT_SYMBOL(reserve_evntsel_nmi);
  722. EXPORT_SYMBOL(release_evntsel_nmi);
  723. EXPORT_SYMBOL(disable_timer_nmi_watchdog);
  724. EXPORT_SYMBOL(enable_timer_nmi_watchdog);