nmi.c 24 KB

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