nmi.c 19 KB

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