perfctr-watchdog.c 18 KB

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