nmi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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/mm.h>
  15. #include <linux/delay.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/module.h>
  18. #include <linux/sysdev.h>
  19. #include <linux/nmi.h>
  20. #include <linux/sysctl.h>
  21. #include <linux/kprobes.h>
  22. #include <asm/smp.h>
  23. #include <asm/nmi.h>
  24. #include <asm/proto.h>
  25. #include <asm/kdebug.h>
  26. #include <asm/mce.h>
  27. #include <asm/intel_arch_perfmon.h>
  28. int unknown_nmi_panic;
  29. int nmi_watchdog_enabled;
  30. int panic_on_unrecovered_nmi;
  31. /* perfctr_nmi_owner tracks the ownership of the perfctr registers:
  32. * evtsel_nmi_owner tracks the ownership of the event selection
  33. * - different performance counters/ event selection may be reserved for
  34. * different subsystems this reservation system just tries to coordinate
  35. * things a little
  36. */
  37. static DEFINE_PER_CPU(unsigned, perfctr_nmi_owner);
  38. static DEFINE_PER_CPU(unsigned, evntsel_nmi_owner[2]);
  39. /* this number is calculated from Intel's MSR_P4_CRU_ESCR5 register and it's
  40. * offset from MSR_P4_BSU_ESCR0. It will be the max for all platforms (for now)
  41. */
  42. #define NMI_MAX_COUNTER_BITS 66
  43. /* nmi_active:
  44. * >0: the lapic NMI watchdog is active, but can be disabled
  45. * <0: the lapic NMI watchdog has not been set up, and cannot
  46. * be enabled
  47. * 0: the lapic NMI watchdog is disabled, but can be enabled
  48. */
  49. atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
  50. int panic_on_timeout;
  51. unsigned int nmi_watchdog = NMI_DEFAULT;
  52. static unsigned int nmi_hz = HZ;
  53. struct nmi_watchdog_ctlblk {
  54. int enabled;
  55. u64 check_bit;
  56. unsigned int cccr_msr;
  57. unsigned int perfctr_msr; /* the MSR to reset in NMI handler */
  58. unsigned int evntsel_msr; /* the MSR to select the events to handle */
  59. };
  60. static DEFINE_PER_CPU(struct nmi_watchdog_ctlblk, nmi_watchdog_ctlblk);
  61. /* local prototypes */
  62. static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu);
  63. /* converts an msr to an appropriate reservation bit */
  64. static inline unsigned int nmi_perfctr_msr_to_bit(unsigned int msr)
  65. {
  66. /* returns the bit offset of the performance counter register */
  67. switch (boot_cpu_data.x86_vendor) {
  68. case X86_VENDOR_AMD:
  69. return (msr - MSR_K7_PERFCTR0);
  70. case X86_VENDOR_INTEL:
  71. if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  72. return (msr - MSR_ARCH_PERFMON_PERFCTR0);
  73. else
  74. return (msr - MSR_P4_BPU_PERFCTR0);
  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. if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  87. return (msr - MSR_ARCH_PERFMON_EVENTSEL0);
  88. else
  89. return (msr - MSR_P4_BSU_ESCR0);
  90. }
  91. return 0;
  92. }
  93. /* checks for a bit availability (hack for oprofile) */
  94. int avail_to_resrv_perfctr_nmi_bit(unsigned int counter)
  95. {
  96. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  97. return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner)));
  98. }
  99. /* checks the an msr for availability */
  100. int avail_to_resrv_perfctr_nmi(unsigned int msr)
  101. {
  102. unsigned int counter;
  103. counter = nmi_perfctr_msr_to_bit(msr);
  104. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  105. return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner)));
  106. }
  107. int reserve_perfctr_nmi(unsigned int msr)
  108. {
  109. unsigned int counter;
  110. counter = nmi_perfctr_msr_to_bit(msr);
  111. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  112. if (!test_and_set_bit(counter, &__get_cpu_var(perfctr_nmi_owner)))
  113. return 1;
  114. return 0;
  115. }
  116. void release_perfctr_nmi(unsigned int msr)
  117. {
  118. unsigned int counter;
  119. counter = nmi_perfctr_msr_to_bit(msr);
  120. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  121. clear_bit(counter, &__get_cpu_var(perfctr_nmi_owner));
  122. }
  123. int reserve_evntsel_nmi(unsigned int msr)
  124. {
  125. unsigned int counter;
  126. counter = nmi_evntsel_msr_to_bit(msr);
  127. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  128. if (!test_and_set_bit(counter, &__get_cpu_var(evntsel_nmi_owner)))
  129. return 1;
  130. return 0;
  131. }
  132. void release_evntsel_nmi(unsigned int msr)
  133. {
  134. unsigned int counter;
  135. counter = nmi_evntsel_msr_to_bit(msr);
  136. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  137. clear_bit(counter, &__get_cpu_var(evntsel_nmi_owner));
  138. }
  139. static __cpuinit inline int nmi_known_cpu(void)
  140. {
  141. switch (boot_cpu_data.x86_vendor) {
  142. case X86_VENDOR_AMD:
  143. return boot_cpu_data.x86 == 15;
  144. case X86_VENDOR_INTEL:
  145. if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  146. return 1;
  147. else
  148. return (boot_cpu_data.x86 == 15);
  149. }
  150. return 0;
  151. }
  152. /* Run after command line and cpu_init init, but before all other checks */
  153. void nmi_watchdog_default(void)
  154. {
  155. if (nmi_watchdog != NMI_DEFAULT)
  156. return;
  157. if (nmi_known_cpu())
  158. nmi_watchdog = NMI_LOCAL_APIC;
  159. else
  160. nmi_watchdog = NMI_IO_APIC;
  161. }
  162. #ifdef CONFIG_SMP
  163. /* The performance counters used by NMI_LOCAL_APIC don't trigger when
  164. * the CPU is idle. To make sure the NMI watchdog really ticks on all
  165. * CPUs during the test make them busy.
  166. */
  167. static __init void nmi_cpu_busy(void *data)
  168. {
  169. volatile int *endflag = data;
  170. local_irq_enable_in_hardirq();
  171. /* Intentionally don't use cpu_relax here. This is
  172. to make sure that the performance counter really ticks,
  173. even if there is a simulator or similar that catches the
  174. pause instruction. On a real HT machine this is fine because
  175. all other CPUs are busy with "useless" delay loops and don't
  176. care if they get somewhat less cycles. */
  177. while (*endflag == 0)
  178. barrier();
  179. }
  180. #endif
  181. int __init check_nmi_watchdog (void)
  182. {
  183. volatile int endflag = 0;
  184. int *counts;
  185. int cpu;
  186. if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DEFAULT))
  187. return 0;
  188. if (!atomic_read(&nmi_active))
  189. return 0;
  190. counts = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
  191. if (!counts)
  192. return -1;
  193. printk(KERN_INFO "testing NMI watchdog ... ");
  194. #ifdef CONFIG_SMP
  195. if (nmi_watchdog == NMI_LOCAL_APIC)
  196. smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
  197. #endif
  198. for (cpu = 0; cpu < NR_CPUS; cpu++)
  199. counts[cpu] = cpu_pda(cpu)->__nmi_count;
  200. local_irq_enable();
  201. mdelay((10*1000)/nmi_hz); // wait 10 ticks
  202. for_each_online_cpu(cpu) {
  203. if (!per_cpu(nmi_watchdog_ctlblk, cpu).enabled)
  204. continue;
  205. if (cpu_pda(cpu)->__nmi_count - counts[cpu] <= 5) {
  206. printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n",
  207. cpu,
  208. counts[cpu],
  209. cpu_pda(cpu)->__nmi_count);
  210. per_cpu(nmi_watchdog_ctlblk, cpu).enabled = 0;
  211. atomic_dec(&nmi_active);
  212. }
  213. }
  214. if (!atomic_read(&nmi_active)) {
  215. kfree(counts);
  216. atomic_set(&nmi_active, -1);
  217. return -1;
  218. }
  219. endflag = 1;
  220. printk("OK.\n");
  221. /* now that we know it works we can reduce NMI frequency to
  222. something more reasonable; makes a difference in some configs */
  223. if (nmi_watchdog == NMI_LOCAL_APIC) {
  224. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  225. nmi_hz = 1;
  226. /*
  227. * On Intel CPUs with ARCH_PERFMON only 32 bits in the counter
  228. * are writable, with higher bits sign extending from bit 31.
  229. * So, we can only program the counter with 31 bit values and
  230. * 32nd bit should be 1, for 33.. to be 1.
  231. * Find the appropriate nmi_hz
  232. */
  233. if (wd->perfctr_msr == MSR_ARCH_PERFMON_PERFCTR0 &&
  234. ((u64)cpu_khz * 1000) > 0x7fffffffULL) {
  235. nmi_hz = ((u64)cpu_khz * 1000) / 0x7fffffffUL + 1;
  236. }
  237. }
  238. kfree(counts);
  239. return 0;
  240. }
  241. int __init setup_nmi_watchdog(char *str)
  242. {
  243. int nmi;
  244. if (!strncmp(str,"panic",5)) {
  245. panic_on_timeout = 1;
  246. str = strchr(str, ',');
  247. if (!str)
  248. return 1;
  249. ++str;
  250. }
  251. get_option(&str, &nmi);
  252. if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
  253. return 0;
  254. if ((nmi == NMI_LOCAL_APIC) && (nmi_known_cpu() == 0))
  255. return 0; /* no lapic support */
  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. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  654. u64 dummy;
  655. int rc=0;
  656. /* check for other users first */
  657. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
  658. == NOTIFY_STOP) {
  659. rc = 1;
  660. touched = 1;
  661. }
  662. sum = read_pda(apic_timer_irqs);
  663. if (__get_cpu_var(nmi_touch)) {
  664. __get_cpu_var(nmi_touch) = 0;
  665. touched = 1;
  666. }
  667. #ifdef CONFIG_X86_MCE
  668. /* Could check oops_in_progress here too, but it's safer
  669. not too */
  670. if (atomic_read(&mce_entry) > 0)
  671. touched = 1;
  672. #endif
  673. /* if the apic timer isn't firing, this cpu isn't doing much */
  674. if (!touched && __get_cpu_var(last_irq_sum) == sum) {
  675. /*
  676. * Ayiee, looks like this CPU is stuck ...
  677. * wait a few IRQs (5 seconds) before doing the oops ...
  678. */
  679. local_inc(&__get_cpu_var(alert_counter));
  680. if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
  681. die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs,
  682. panic_on_timeout);
  683. } else {
  684. __get_cpu_var(last_irq_sum) = sum;
  685. local_set(&__get_cpu_var(alert_counter), 0);
  686. }
  687. /* see if the nmi watchdog went off */
  688. if (wd->enabled) {
  689. if (nmi_watchdog == NMI_LOCAL_APIC) {
  690. rdmsrl(wd->perfctr_msr, dummy);
  691. if (dummy & wd->check_bit){
  692. /* this wasn't a watchdog timer interrupt */
  693. goto done;
  694. }
  695. /* only Intel uses the cccr msr */
  696. if (wd->cccr_msr != 0) {
  697. /*
  698. * P4 quirks:
  699. * - An overflown perfctr will assert its interrupt
  700. * until the OVF flag in its CCCR is cleared.
  701. * - LVTPC is masked on interrupt and must be
  702. * unmasked by the LVTPC handler.
  703. */
  704. rdmsrl(wd->cccr_msr, dummy);
  705. dummy &= ~P4_CCCR_OVF;
  706. wrmsrl(wd->cccr_msr, dummy);
  707. apic_write(APIC_LVTPC, APIC_DM_NMI);
  708. } else if (wd->perfctr_msr == MSR_ARCH_PERFMON_PERFCTR0) {
  709. /*
  710. * ArchPerfom/Core Duo needs to re-unmask
  711. * the apic vector
  712. */
  713. apic_write(APIC_LVTPC, APIC_DM_NMI);
  714. }
  715. /* start the cycle over again */
  716. wrmsrl(wd->perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
  717. rc = 1;
  718. } else if (nmi_watchdog == NMI_IO_APIC) {
  719. /* don't know how to accurately check for this.
  720. * just assume it was a watchdog timer interrupt
  721. * This matches the old behaviour.
  722. */
  723. rc = 1;
  724. } else
  725. printk(KERN_WARNING "Unknown enabled NMI hardware?!\n");
  726. }
  727. done:
  728. return rc;
  729. }
  730. asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code)
  731. {
  732. nmi_enter();
  733. add_pda(__nmi_count,1);
  734. default_do_nmi(regs);
  735. nmi_exit();
  736. }
  737. int do_nmi_callback(struct pt_regs * regs, int cpu)
  738. {
  739. #ifdef CONFIG_SYSCTL
  740. if (unknown_nmi_panic)
  741. return unknown_nmi_panic_callback(regs, cpu);
  742. #endif
  743. return 0;
  744. }
  745. #ifdef CONFIG_SYSCTL
  746. static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
  747. {
  748. unsigned char reason = get_nmi_reason();
  749. char buf[64];
  750. sprintf(buf, "NMI received for unknown reason %02x\n", reason);
  751. die_nmi(buf, regs, 1); /* Always panic here */
  752. return 0;
  753. }
  754. /*
  755. * proc handler for /proc/sys/kernel/nmi
  756. */
  757. int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
  758. void __user *buffer, size_t *length, loff_t *ppos)
  759. {
  760. int old_state;
  761. nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
  762. old_state = nmi_watchdog_enabled;
  763. proc_dointvec(table, write, file, buffer, length, ppos);
  764. if (!!old_state == !!nmi_watchdog_enabled)
  765. return 0;
  766. if (atomic_read(&nmi_active) < 0) {
  767. printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
  768. return -EIO;
  769. }
  770. /* if nmi_watchdog is not set yet, then set it */
  771. nmi_watchdog_default();
  772. if (nmi_watchdog == NMI_LOCAL_APIC) {
  773. if (nmi_watchdog_enabled)
  774. enable_lapic_nmi_watchdog();
  775. else
  776. disable_lapic_nmi_watchdog();
  777. } else {
  778. printk( KERN_WARNING
  779. "NMI watchdog doesn't know what hardware to touch\n");
  780. return -EIO;
  781. }
  782. return 0;
  783. }
  784. #endif
  785. EXPORT_SYMBOL(nmi_active);
  786. EXPORT_SYMBOL(nmi_watchdog);
  787. EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi);
  788. EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi_bit);
  789. EXPORT_SYMBOL(reserve_perfctr_nmi);
  790. EXPORT_SYMBOL(release_perfctr_nmi);
  791. EXPORT_SYMBOL(reserve_evntsel_nmi);
  792. EXPORT_SYMBOL(release_evntsel_nmi);
  793. EXPORT_SYMBOL(disable_timer_nmi_watchdog);
  794. EXPORT_SYMBOL(enable_timer_nmi_watchdog);
  795. EXPORT_SYMBOL(touch_nmi_watchdog);