nmi.c 23 KB

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