perfctr-watchdog.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /*
  2. * local apic based NMI watchdog for various CPUs.
  3. *
  4. * This file also handles reservation of performance counters for coordination
  5. * with other users (like oprofile).
  6. *
  7. * Note that these events normally don't tick when the CPU idles. This means
  8. * the frequency varies with CPU load.
  9. *
  10. * Original code for K7/P6 written by Keith Owens
  11. *
  12. */
  13. #include <linux/percpu.h>
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/bitops.h>
  17. #include <linux/smp.h>
  18. #include <linux/nmi.h>
  19. #include <asm/apic.h>
  20. #include <asm/intel_arch_perfmon.h>
  21. struct nmi_watchdog_ctlblk {
  22. unsigned int cccr_msr;
  23. unsigned int perfctr_msr; /* the MSR to reset in NMI handler */
  24. unsigned int evntsel_msr; /* the MSR to select the events to handle */
  25. };
  26. /* Interface defining a CPU specific perfctr watchdog */
  27. struct wd_ops {
  28. int (*reserve)(void);
  29. void (*unreserve)(void);
  30. int (*setup)(unsigned nmi_hz);
  31. void (*rearm)(struct nmi_watchdog_ctlblk *wd, unsigned nmi_hz);
  32. void (*stop)(void);
  33. unsigned perfctr;
  34. unsigned evntsel;
  35. u64 checkbit;
  36. };
  37. static const struct wd_ops *wd_ops;
  38. /*
  39. * this number is calculated from Intel's MSR_P4_CRU_ESCR5 register and it's
  40. * offset from MSR_P4_BSU_ESCR0.
  41. *
  42. * It will be the max for all platforms (for now)
  43. */
  44. #define NMI_MAX_COUNTER_BITS 66
  45. /*
  46. * perfctr_nmi_owner tracks the ownership of the perfctr registers:
  47. * evtsel_nmi_owner tracks the ownership of the event selection
  48. * - different performance counters/ event selection may be reserved for
  49. * different subsystems this reservation system just tries to coordinate
  50. * things a little
  51. */
  52. static DECLARE_BITMAP(perfctr_nmi_owner, NMI_MAX_COUNTER_BITS);
  53. static DECLARE_BITMAP(evntsel_nmi_owner, NMI_MAX_COUNTER_BITS);
  54. static DEFINE_PER_CPU(struct nmi_watchdog_ctlblk, nmi_watchdog_ctlblk);
  55. /* converts an msr to an appropriate reservation bit */
  56. static inline unsigned int nmi_perfctr_msr_to_bit(unsigned int msr)
  57. {
  58. /* returns the bit offset of the performance counter register */
  59. switch (boot_cpu_data.x86_vendor) {
  60. case X86_VENDOR_AMD:
  61. return (msr - MSR_K7_PERFCTR0);
  62. case X86_VENDOR_INTEL:
  63. if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  64. return (msr - MSR_ARCH_PERFMON_PERFCTR0);
  65. switch (boot_cpu_data.x86) {
  66. case 6:
  67. return (msr - MSR_P6_PERFCTR0);
  68. case 15:
  69. return (msr - MSR_P4_BPU_PERFCTR0);
  70. }
  71. }
  72. return 0;
  73. }
  74. /*
  75. * converts an msr to an appropriate reservation bit
  76. * returns the bit offset of the event selection register
  77. */
  78. static inline unsigned int nmi_evntsel_msr_to_bit(unsigned int msr)
  79. {
  80. /* returns the bit offset of the event selection register */
  81. switch (boot_cpu_data.x86_vendor) {
  82. case X86_VENDOR_AMD:
  83. return (msr - MSR_K7_EVNTSEL0);
  84. case X86_VENDOR_INTEL:
  85. if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  86. return (msr - MSR_ARCH_PERFMON_EVENTSEL0);
  87. switch (boot_cpu_data.x86) {
  88. case 6:
  89. return (msr - MSR_P6_EVNTSEL0);
  90. case 15:
  91. return (msr - MSR_P4_BSU_ESCR0);
  92. }
  93. }
  94. return 0;
  95. }
  96. /* checks for a bit availability (hack for oprofile) */
  97. int avail_to_resrv_perfctr_nmi_bit(unsigned int counter)
  98. {
  99. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  100. return (!test_bit(counter, perfctr_nmi_owner));
  101. }
  102. /* checks the an msr for availability */
  103. int avail_to_resrv_perfctr_nmi(unsigned int msr)
  104. {
  105. unsigned int counter;
  106. counter = nmi_perfctr_msr_to_bit(msr);
  107. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  108. return (!test_bit(counter, perfctr_nmi_owner));
  109. }
  110. EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi_bit);
  111. int reserve_perfctr_nmi(unsigned int msr)
  112. {
  113. unsigned int counter;
  114. counter = nmi_perfctr_msr_to_bit(msr);
  115. /* register not managed by the allocator? */
  116. if (counter > NMI_MAX_COUNTER_BITS)
  117. return 1;
  118. if (!test_and_set_bit(counter, perfctr_nmi_owner))
  119. return 1;
  120. return 0;
  121. }
  122. EXPORT_SYMBOL(reserve_perfctr_nmi);
  123. void release_perfctr_nmi(unsigned int msr)
  124. {
  125. unsigned int counter;
  126. counter = nmi_perfctr_msr_to_bit(msr);
  127. /* register not managed by the allocator? */
  128. if (counter > NMI_MAX_COUNTER_BITS)
  129. return;
  130. clear_bit(counter, perfctr_nmi_owner);
  131. }
  132. EXPORT_SYMBOL(release_perfctr_nmi);
  133. int reserve_evntsel_nmi(unsigned int msr)
  134. {
  135. unsigned int counter;
  136. counter = nmi_evntsel_msr_to_bit(msr);
  137. /* register not managed by the allocator? */
  138. if (counter > NMI_MAX_COUNTER_BITS)
  139. return 1;
  140. if (!test_and_set_bit(counter, evntsel_nmi_owner))
  141. return 1;
  142. return 0;
  143. }
  144. EXPORT_SYMBOL(reserve_evntsel_nmi);
  145. void release_evntsel_nmi(unsigned int msr)
  146. {
  147. unsigned int counter;
  148. counter = nmi_evntsel_msr_to_bit(msr);
  149. /* register not managed by the allocator? */
  150. if (counter > NMI_MAX_COUNTER_BITS)
  151. return;
  152. clear_bit(counter, evntsel_nmi_owner);
  153. }
  154. EXPORT_SYMBOL(release_evntsel_nmi);
  155. void disable_lapic_nmi_watchdog(void)
  156. {
  157. BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
  158. if (atomic_read(&nmi_active) <= 0)
  159. return;
  160. on_each_cpu(stop_apic_nmi_watchdog, NULL, 1);
  161. if (wd_ops)
  162. wd_ops->unreserve();
  163. BUG_ON(atomic_read(&nmi_active) != 0);
  164. }
  165. void enable_lapic_nmi_watchdog(void)
  166. {
  167. BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
  168. /* are we already enabled */
  169. if (atomic_read(&nmi_active) != 0)
  170. return;
  171. /* are we lapic aware */
  172. if (!wd_ops)
  173. return;
  174. if (!wd_ops->reserve()) {
  175. printk(KERN_ERR "NMI watchdog: cannot reserve perfctrs\n");
  176. return;
  177. }
  178. on_each_cpu(setup_apic_nmi_watchdog, NULL, 1);
  179. touch_nmi_watchdog();
  180. }
  181. /*
  182. * Activate the NMI watchdog via the local APIC.
  183. */
  184. static unsigned int adjust_for_32bit_ctr(unsigned int hz)
  185. {
  186. u64 counter_val;
  187. unsigned int retval = hz;
  188. /*
  189. * On Intel CPUs with P6/ARCH_PERFMON only 32 bits in the counter
  190. * are writable, with higher bits sign extending from bit 31.
  191. * So, we can only program the counter with 31 bit values and
  192. * 32nd bit should be 1, for 33.. to be 1.
  193. * Find the appropriate nmi_hz
  194. */
  195. counter_val = (u64)cpu_khz * 1000;
  196. do_div(counter_val, retval);
  197. if (counter_val > 0x7fffffffULL) {
  198. u64 count = (u64)cpu_khz * 1000;
  199. do_div(count, 0x7fffffffUL);
  200. retval = count + 1;
  201. }
  202. return retval;
  203. }
  204. static void write_watchdog_counter(unsigned int perfctr_msr,
  205. const char *descr, unsigned nmi_hz)
  206. {
  207. u64 count = (u64)cpu_khz * 1000;
  208. do_div(count, nmi_hz);
  209. if(descr)
  210. Dprintk("setting %s to -0x%08Lx\n", descr, count);
  211. wrmsrl(perfctr_msr, 0 - count);
  212. }
  213. static void write_watchdog_counter32(unsigned int perfctr_msr,
  214. const char *descr, unsigned nmi_hz)
  215. {
  216. u64 count = (u64)cpu_khz * 1000;
  217. do_div(count, nmi_hz);
  218. if(descr)
  219. Dprintk("setting %s to -0x%08Lx\n", descr, count);
  220. wrmsr(perfctr_msr, (u32)(-count), 0);
  221. }
  222. /*
  223. * AMD K7/K8/Family10h/Family11h support.
  224. * AMD keeps this interface nicely stable so there is not much variety
  225. */
  226. #define K7_EVNTSEL_ENABLE (1 << 22)
  227. #define K7_EVNTSEL_INT (1 << 20)
  228. #define K7_EVNTSEL_OS (1 << 17)
  229. #define K7_EVNTSEL_USR (1 << 16)
  230. #define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
  231. #define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
  232. static int setup_k7_watchdog(unsigned nmi_hz)
  233. {
  234. unsigned int perfctr_msr, evntsel_msr;
  235. unsigned int evntsel;
  236. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  237. perfctr_msr = wd_ops->perfctr;
  238. evntsel_msr = wd_ops->evntsel;
  239. wrmsrl(perfctr_msr, 0UL);
  240. evntsel = K7_EVNTSEL_INT
  241. | K7_EVNTSEL_OS
  242. | K7_EVNTSEL_USR
  243. | K7_NMI_EVENT;
  244. /* setup the timer */
  245. wrmsr(evntsel_msr, evntsel, 0);
  246. write_watchdog_counter(perfctr_msr, "K7_PERFCTR0",nmi_hz);
  247. apic_write(APIC_LVTPC, APIC_DM_NMI);
  248. evntsel |= K7_EVNTSEL_ENABLE;
  249. wrmsr(evntsel_msr, evntsel, 0);
  250. wd->perfctr_msr = perfctr_msr;
  251. wd->evntsel_msr = evntsel_msr;
  252. wd->cccr_msr = 0; /* unused */
  253. return 1;
  254. }
  255. static void single_msr_stop_watchdog(void)
  256. {
  257. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  258. wrmsr(wd->evntsel_msr, 0, 0);
  259. }
  260. static int single_msr_reserve(void)
  261. {
  262. if (!reserve_perfctr_nmi(wd_ops->perfctr))
  263. return 0;
  264. if (!reserve_evntsel_nmi(wd_ops->evntsel)) {
  265. release_perfctr_nmi(wd_ops->perfctr);
  266. return 0;
  267. }
  268. return 1;
  269. }
  270. static void single_msr_unreserve(void)
  271. {
  272. release_evntsel_nmi(wd_ops->evntsel);
  273. release_perfctr_nmi(wd_ops->perfctr);
  274. }
  275. static void single_msr_rearm(struct nmi_watchdog_ctlblk *wd, unsigned nmi_hz)
  276. {
  277. /* start the cycle over again */
  278. write_watchdog_counter(wd->perfctr_msr, NULL, nmi_hz);
  279. }
  280. static const struct wd_ops k7_wd_ops = {
  281. .reserve = single_msr_reserve,
  282. .unreserve = single_msr_unreserve,
  283. .setup = setup_k7_watchdog,
  284. .rearm = single_msr_rearm,
  285. .stop = single_msr_stop_watchdog,
  286. .perfctr = MSR_K7_PERFCTR0,
  287. .evntsel = MSR_K7_EVNTSEL0,
  288. .checkbit = 1ULL << 47,
  289. };
  290. /*
  291. * Intel Model 6 (PPro+,P2,P3,P-M,Core1)
  292. */
  293. #define P6_EVNTSEL0_ENABLE (1 << 22)
  294. #define P6_EVNTSEL_INT (1 << 20)
  295. #define P6_EVNTSEL_OS (1 << 17)
  296. #define P6_EVNTSEL_USR (1 << 16)
  297. #define P6_EVENT_CPU_CLOCKS_NOT_HALTED 0x79
  298. #define P6_NMI_EVENT P6_EVENT_CPU_CLOCKS_NOT_HALTED
  299. static int setup_p6_watchdog(unsigned nmi_hz)
  300. {
  301. unsigned int perfctr_msr, evntsel_msr;
  302. unsigned int evntsel;
  303. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  304. perfctr_msr = wd_ops->perfctr;
  305. evntsel_msr = wd_ops->evntsel;
  306. /* KVM doesn't implement this MSR */
  307. if (wrmsr_safe(perfctr_msr, 0, 0) < 0)
  308. return 0;
  309. evntsel = P6_EVNTSEL_INT
  310. | P6_EVNTSEL_OS
  311. | P6_EVNTSEL_USR
  312. | P6_NMI_EVENT;
  313. /* setup the timer */
  314. wrmsr(evntsel_msr, evntsel, 0);
  315. nmi_hz = adjust_for_32bit_ctr(nmi_hz);
  316. write_watchdog_counter32(perfctr_msr, "P6_PERFCTR0",nmi_hz);
  317. apic_write(APIC_LVTPC, APIC_DM_NMI);
  318. evntsel |= P6_EVNTSEL0_ENABLE;
  319. wrmsr(evntsel_msr, evntsel, 0);
  320. wd->perfctr_msr = perfctr_msr;
  321. wd->evntsel_msr = evntsel_msr;
  322. wd->cccr_msr = 0; /* unused */
  323. return 1;
  324. }
  325. static void p6_rearm(struct nmi_watchdog_ctlblk *wd, unsigned nmi_hz)
  326. {
  327. /*
  328. * P6 based Pentium M need to re-unmask
  329. * the apic vector but it doesn't hurt
  330. * other P6 variant.
  331. * ArchPerfom/Core Duo also needs this
  332. */
  333. apic_write(APIC_LVTPC, APIC_DM_NMI);
  334. /* P6/ARCH_PERFMON has 32 bit counter write */
  335. write_watchdog_counter32(wd->perfctr_msr, NULL,nmi_hz);
  336. }
  337. static const struct wd_ops p6_wd_ops = {
  338. .reserve = single_msr_reserve,
  339. .unreserve = single_msr_unreserve,
  340. .setup = setup_p6_watchdog,
  341. .rearm = p6_rearm,
  342. .stop = single_msr_stop_watchdog,
  343. .perfctr = MSR_P6_PERFCTR0,
  344. .evntsel = MSR_P6_EVNTSEL0,
  345. .checkbit = 1ULL << 39,
  346. };
  347. /*
  348. * Intel P4 performance counters.
  349. * By far the most complicated of all.
  350. */
  351. #define MSR_P4_MISC_ENABLE_PERF_AVAIL (1 << 7)
  352. #define P4_ESCR_EVENT_SELECT(N) ((N) << 25)
  353. #define P4_ESCR_OS (1 << 3)
  354. #define P4_ESCR_USR (1 << 2)
  355. #define P4_CCCR_OVF_PMI0 (1 << 26)
  356. #define P4_CCCR_OVF_PMI1 (1 << 27)
  357. #define P4_CCCR_THRESHOLD(N) ((N) << 20)
  358. #define P4_CCCR_COMPLEMENT (1 << 19)
  359. #define P4_CCCR_COMPARE (1 << 18)
  360. #define P4_CCCR_REQUIRED (3 << 16)
  361. #define P4_CCCR_ESCR_SELECT(N) ((N) << 13)
  362. #define P4_CCCR_ENABLE (1 << 12)
  363. #define P4_CCCR_OVF (1 << 31)
  364. /*
  365. * Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
  366. * CRU_ESCR0 (with any non-null event selector) through a complemented
  367. * max threshold. [IA32-Vol3, Section 14.9.9]
  368. */
  369. static int setup_p4_watchdog(unsigned nmi_hz)
  370. {
  371. unsigned int perfctr_msr, evntsel_msr, cccr_msr;
  372. unsigned int evntsel, cccr_val;
  373. unsigned int misc_enable, dummy;
  374. unsigned int ht_num;
  375. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  376. rdmsr(MSR_IA32_MISC_ENABLE, misc_enable, dummy);
  377. if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
  378. return 0;
  379. #ifdef CONFIG_SMP
  380. /* detect which hyperthread we are on */
  381. if (smp_num_siblings == 2) {
  382. unsigned int ebx, apicid;
  383. ebx = cpuid_ebx(1);
  384. apicid = (ebx >> 24) & 0xff;
  385. ht_num = apicid & 1;
  386. } else
  387. #endif
  388. ht_num = 0;
  389. /*
  390. * performance counters are shared resources
  391. * assign each hyperthread its own set
  392. * (re-use the ESCR0 register, seems safe
  393. * and keeps the cccr_val the same)
  394. */
  395. if (!ht_num) {
  396. /* logical cpu 0 */
  397. perfctr_msr = MSR_P4_IQ_PERFCTR0;
  398. evntsel_msr = MSR_P4_CRU_ESCR0;
  399. cccr_msr = MSR_P4_IQ_CCCR0;
  400. cccr_val = P4_CCCR_OVF_PMI0 | P4_CCCR_ESCR_SELECT(4);
  401. } else {
  402. /* logical cpu 1 */
  403. perfctr_msr = MSR_P4_IQ_PERFCTR1;
  404. evntsel_msr = MSR_P4_CRU_ESCR0;
  405. cccr_msr = MSR_P4_IQ_CCCR1;
  406. cccr_val = P4_CCCR_OVF_PMI1 | P4_CCCR_ESCR_SELECT(4);
  407. }
  408. evntsel = P4_ESCR_EVENT_SELECT(0x3F)
  409. | P4_ESCR_OS
  410. | P4_ESCR_USR;
  411. cccr_val |= P4_CCCR_THRESHOLD(15)
  412. | P4_CCCR_COMPLEMENT
  413. | P4_CCCR_COMPARE
  414. | P4_CCCR_REQUIRED;
  415. wrmsr(evntsel_msr, evntsel, 0);
  416. wrmsr(cccr_msr, cccr_val, 0);
  417. write_watchdog_counter(perfctr_msr, "P4_IQ_COUNTER0", nmi_hz);
  418. apic_write(APIC_LVTPC, APIC_DM_NMI);
  419. cccr_val |= P4_CCCR_ENABLE;
  420. wrmsr(cccr_msr, cccr_val, 0);
  421. wd->perfctr_msr = perfctr_msr;
  422. wd->evntsel_msr = evntsel_msr;
  423. wd->cccr_msr = cccr_msr;
  424. return 1;
  425. }
  426. static void stop_p4_watchdog(void)
  427. {
  428. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  429. wrmsr(wd->cccr_msr, 0, 0);
  430. wrmsr(wd->evntsel_msr, 0, 0);
  431. }
  432. static int p4_reserve(void)
  433. {
  434. if (!reserve_perfctr_nmi(MSR_P4_IQ_PERFCTR0))
  435. return 0;
  436. #ifdef CONFIG_SMP
  437. if (smp_num_siblings > 1 && !reserve_perfctr_nmi(MSR_P4_IQ_PERFCTR1))
  438. goto fail1;
  439. #endif
  440. if (!reserve_evntsel_nmi(MSR_P4_CRU_ESCR0))
  441. goto fail2;
  442. /* RED-PEN why is ESCR1 not reserved here? */
  443. return 1;
  444. fail2:
  445. #ifdef CONFIG_SMP
  446. if (smp_num_siblings > 1)
  447. release_perfctr_nmi(MSR_P4_IQ_PERFCTR1);
  448. fail1:
  449. #endif
  450. release_perfctr_nmi(MSR_P4_IQ_PERFCTR0);
  451. return 0;
  452. }
  453. static void p4_unreserve(void)
  454. {
  455. #ifdef CONFIG_SMP
  456. if (smp_num_siblings > 1)
  457. release_perfctr_nmi(MSR_P4_IQ_PERFCTR1);
  458. #endif
  459. release_evntsel_nmi(MSR_P4_CRU_ESCR0);
  460. release_perfctr_nmi(MSR_P4_IQ_PERFCTR0);
  461. }
  462. static void p4_rearm(struct nmi_watchdog_ctlblk *wd, unsigned nmi_hz)
  463. {
  464. unsigned dummy;
  465. /*
  466. * P4 quirks:
  467. * - An overflown perfctr will assert its interrupt
  468. * until the OVF flag in its CCCR is cleared.
  469. * - LVTPC is masked on interrupt and must be
  470. * unmasked by the LVTPC handler.
  471. */
  472. rdmsrl(wd->cccr_msr, dummy);
  473. dummy &= ~P4_CCCR_OVF;
  474. wrmsrl(wd->cccr_msr, dummy);
  475. apic_write(APIC_LVTPC, APIC_DM_NMI);
  476. /* start the cycle over again */
  477. write_watchdog_counter(wd->perfctr_msr, NULL, nmi_hz);
  478. }
  479. static const struct wd_ops p4_wd_ops = {
  480. .reserve = p4_reserve,
  481. .unreserve = p4_unreserve,
  482. .setup = setup_p4_watchdog,
  483. .rearm = p4_rearm,
  484. .stop = stop_p4_watchdog,
  485. /* RED-PEN this is wrong for the other sibling */
  486. .perfctr = MSR_P4_BPU_PERFCTR0,
  487. .evntsel = MSR_P4_BSU_ESCR0,
  488. .checkbit = 1ULL << 39,
  489. };
  490. /*
  491. * Watchdog using the Intel architected PerfMon.
  492. * Used for Core2 and hopefully all future Intel CPUs.
  493. */
  494. #define ARCH_PERFMON_NMI_EVENT_SEL ARCH_PERFMON_UNHALTED_CORE_CYCLES_SEL
  495. #define ARCH_PERFMON_NMI_EVENT_UMASK ARCH_PERFMON_UNHALTED_CORE_CYCLES_UMASK
  496. static struct wd_ops intel_arch_wd_ops;
  497. static int setup_intel_arch_watchdog(unsigned nmi_hz)
  498. {
  499. unsigned int ebx;
  500. union cpuid10_eax eax;
  501. unsigned int unused;
  502. unsigned int perfctr_msr, evntsel_msr;
  503. unsigned int evntsel;
  504. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  505. /*
  506. * Check whether the Architectural PerfMon supports
  507. * Unhalted Core Cycles Event or not.
  508. * NOTE: Corresponding bit = 0 in ebx indicates event present.
  509. */
  510. cpuid(10, &(eax.full), &ebx, &unused, &unused);
  511. if ((eax.split.mask_length < (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX+1)) ||
  512. (ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT))
  513. return 0;
  514. perfctr_msr = wd_ops->perfctr;
  515. evntsel_msr = wd_ops->evntsel;
  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. nmi_hz = adjust_for_32bit_ctr(nmi_hz);
  525. write_watchdog_counter32(perfctr_msr, "INTEL_ARCH_PERFCTR0", nmi_hz);
  526. apic_write(APIC_LVTPC, APIC_DM_NMI);
  527. evntsel |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  528. wrmsr(evntsel_msr, evntsel, 0);
  529. wd->perfctr_msr = perfctr_msr;
  530. wd->evntsel_msr = evntsel_msr;
  531. wd->cccr_msr = 0; /* unused */
  532. intel_arch_wd_ops.checkbit = 1ULL << (eax.split.bit_width - 1);
  533. return 1;
  534. }
  535. static struct wd_ops intel_arch_wd_ops __read_mostly = {
  536. .reserve = single_msr_reserve,
  537. .unreserve = single_msr_unreserve,
  538. .setup = setup_intel_arch_watchdog,
  539. .rearm = p6_rearm,
  540. .stop = single_msr_stop_watchdog,
  541. .perfctr = MSR_ARCH_PERFMON_PERFCTR1,
  542. .evntsel = MSR_ARCH_PERFMON_EVENTSEL1,
  543. };
  544. static void probe_nmi_watchdog(void)
  545. {
  546. switch (boot_cpu_data.x86_vendor) {
  547. case X86_VENDOR_AMD:
  548. if (boot_cpu_data.x86 != 6 && boot_cpu_data.x86 != 15 &&
  549. boot_cpu_data.x86 != 16)
  550. return;
  551. wd_ops = &k7_wd_ops;
  552. break;
  553. case X86_VENDOR_INTEL:
  554. /*
  555. * Work around Core Duo (Yonah) errata AE49 where perfctr1
  556. * doesn't have a working enable bit.
  557. */
  558. if (boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model == 14) {
  559. intel_arch_wd_ops.perfctr = MSR_ARCH_PERFMON_PERFCTR0;
  560. intel_arch_wd_ops.evntsel = MSR_ARCH_PERFMON_EVENTSEL0;
  561. }
  562. if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
  563. wd_ops = &intel_arch_wd_ops;
  564. break;
  565. }
  566. switch (boot_cpu_data.x86) {
  567. case 6:
  568. if (boot_cpu_data.x86_model > 13)
  569. return;
  570. wd_ops = &p6_wd_ops;
  571. break;
  572. case 15:
  573. wd_ops = &p4_wd_ops;
  574. break;
  575. default:
  576. return;
  577. }
  578. break;
  579. }
  580. }
  581. /* Interface to nmi.c */
  582. int lapic_watchdog_init(unsigned nmi_hz)
  583. {
  584. if (!wd_ops) {
  585. probe_nmi_watchdog();
  586. if (!wd_ops) {
  587. printk(KERN_INFO "NMI watchdog: CPU not supported\n");
  588. return -1;
  589. }
  590. if (!wd_ops->reserve()) {
  591. printk(KERN_ERR
  592. "NMI watchdog: cannot reserve perfctrs\n");
  593. return -1;
  594. }
  595. }
  596. if (!(wd_ops->setup(nmi_hz))) {
  597. printk(KERN_ERR "Cannot setup NMI watchdog on CPU %d\n",
  598. raw_smp_processor_id());
  599. return -1;
  600. }
  601. return 0;
  602. }
  603. void lapic_watchdog_stop(void)
  604. {
  605. if (wd_ops)
  606. wd_ops->stop();
  607. }
  608. unsigned lapic_adjust_nmi_hz(unsigned hz)
  609. {
  610. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  611. if (wd->perfctr_msr == MSR_P6_PERFCTR0 ||
  612. wd->perfctr_msr == MSR_ARCH_PERFMON_PERFCTR1)
  613. hz = adjust_for_32bit_ctr(hz);
  614. return hz;
  615. }
  616. int lapic_wd_event(unsigned nmi_hz)
  617. {
  618. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  619. u64 ctr;
  620. rdmsrl(wd->perfctr_msr, ctr);
  621. if (ctr & wd_ops->checkbit) /* perfctr still running? */
  622. return 0;
  623. wd_ops->rearm(wd, nmi_hz);
  624. return 1;
  625. }
  626. int lapic_watchdog_ok(void)
  627. {
  628. return wd_ops != NULL;
  629. }