nmi.c 24 KB

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