perfctr-watchdog.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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. pr_debug("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. pr_debug("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. #define P4_CONTROLS 18
  365. static unsigned int p4_controls[18] = {
  366. MSR_P4_BPU_CCCR0,
  367. MSR_P4_BPU_CCCR1,
  368. MSR_P4_BPU_CCCR2,
  369. MSR_P4_BPU_CCCR3,
  370. MSR_P4_MS_CCCR0,
  371. MSR_P4_MS_CCCR1,
  372. MSR_P4_MS_CCCR2,
  373. MSR_P4_MS_CCCR3,
  374. MSR_P4_FLAME_CCCR0,
  375. MSR_P4_FLAME_CCCR1,
  376. MSR_P4_FLAME_CCCR2,
  377. MSR_P4_FLAME_CCCR3,
  378. MSR_P4_IQ_CCCR0,
  379. MSR_P4_IQ_CCCR1,
  380. MSR_P4_IQ_CCCR2,
  381. MSR_P4_IQ_CCCR3,
  382. MSR_P4_IQ_CCCR4,
  383. MSR_P4_IQ_CCCR5,
  384. };
  385. /*
  386. * Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
  387. * CRU_ESCR0 (with any non-null event selector) through a complemented
  388. * max threshold. [IA32-Vol3, Section 14.9.9]
  389. */
  390. static int setup_p4_watchdog(unsigned nmi_hz)
  391. {
  392. unsigned int perfctr_msr, evntsel_msr, cccr_msr;
  393. unsigned int evntsel, cccr_val;
  394. unsigned int misc_enable, dummy;
  395. unsigned int ht_num;
  396. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  397. rdmsr(MSR_IA32_MISC_ENABLE, misc_enable, dummy);
  398. if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
  399. return 0;
  400. #ifdef CONFIG_SMP
  401. /* detect which hyperthread we are on */
  402. if (smp_num_siblings == 2) {
  403. unsigned int ebx, apicid;
  404. ebx = cpuid_ebx(1);
  405. apicid = (ebx >> 24) & 0xff;
  406. ht_num = apicid & 1;
  407. } else
  408. #endif
  409. ht_num = 0;
  410. /*
  411. * performance counters are shared resources
  412. * assign each hyperthread its own set
  413. * (re-use the ESCR0 register, seems safe
  414. * and keeps the cccr_val the same)
  415. */
  416. if (!ht_num) {
  417. /* logical cpu 0 */
  418. perfctr_msr = MSR_P4_IQ_PERFCTR0;
  419. evntsel_msr = MSR_P4_CRU_ESCR0;
  420. cccr_msr = MSR_P4_IQ_CCCR0;
  421. cccr_val = P4_CCCR_OVF_PMI0 | P4_CCCR_ESCR_SELECT(4);
  422. /*
  423. * If we're on the kdump kernel or other situation, we may
  424. * still have other performance counter registers set to
  425. * interrupt and they'll keep interrupting forever because
  426. * of the P4_CCCR_OVF quirk. So we need to ACK all the
  427. * pending interrupts and disable all the registers here,
  428. * before reenabling the NMI delivery. Refer to p4_rearm()
  429. * about the P4_CCCR_OVF quirk.
  430. */
  431. if (reset_devices) {
  432. unsigned int low, high;
  433. int i;
  434. for (i = 0; i < P4_CONTROLS; i++) {
  435. rdmsr(p4_controls[i], low, high);
  436. low &= ~(P4_CCCR_ENABLE | P4_CCCR_OVF);
  437. wrmsr(p4_controls[i], low, high);
  438. }
  439. }
  440. } else {
  441. /* logical cpu 1 */
  442. perfctr_msr = MSR_P4_IQ_PERFCTR1;
  443. evntsel_msr = MSR_P4_CRU_ESCR0;
  444. cccr_msr = MSR_P4_IQ_CCCR1;
  445. /* Pentium 4 D processors don't support P4_CCCR_OVF_PMI1 */
  446. if (boot_cpu_data.x86_model == 4 && boot_cpu_data.x86_mask == 4)
  447. cccr_val = P4_CCCR_OVF_PMI0;
  448. else
  449. cccr_val = P4_CCCR_OVF_PMI1;
  450. cccr_val |= P4_CCCR_ESCR_SELECT(4);
  451. }
  452. evntsel = P4_ESCR_EVENT_SELECT(0x3F)
  453. | P4_ESCR_OS
  454. | P4_ESCR_USR;
  455. cccr_val |= P4_CCCR_THRESHOLD(15)
  456. | P4_CCCR_COMPLEMENT
  457. | P4_CCCR_COMPARE
  458. | P4_CCCR_REQUIRED;
  459. wrmsr(evntsel_msr, evntsel, 0);
  460. wrmsr(cccr_msr, cccr_val, 0);
  461. write_watchdog_counter(perfctr_msr, "P4_IQ_COUNTER0", nmi_hz);
  462. apic_write(APIC_LVTPC, APIC_DM_NMI);
  463. cccr_val |= P4_CCCR_ENABLE;
  464. wrmsr(cccr_msr, cccr_val, 0);
  465. wd->perfctr_msr = perfctr_msr;
  466. wd->evntsel_msr = evntsel_msr;
  467. wd->cccr_msr = cccr_msr;
  468. return 1;
  469. }
  470. static void stop_p4_watchdog(void)
  471. {
  472. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  473. wrmsr(wd->cccr_msr, 0, 0);
  474. wrmsr(wd->evntsel_msr, 0, 0);
  475. }
  476. static int p4_reserve(void)
  477. {
  478. if (!reserve_perfctr_nmi(MSR_P4_IQ_PERFCTR0))
  479. return 0;
  480. #ifdef CONFIG_SMP
  481. if (smp_num_siblings > 1 && !reserve_perfctr_nmi(MSR_P4_IQ_PERFCTR1))
  482. goto fail1;
  483. #endif
  484. if (!reserve_evntsel_nmi(MSR_P4_CRU_ESCR0))
  485. goto fail2;
  486. /* RED-PEN why is ESCR1 not reserved here? */
  487. return 1;
  488. fail2:
  489. #ifdef CONFIG_SMP
  490. if (smp_num_siblings > 1)
  491. release_perfctr_nmi(MSR_P4_IQ_PERFCTR1);
  492. fail1:
  493. #endif
  494. release_perfctr_nmi(MSR_P4_IQ_PERFCTR0);
  495. return 0;
  496. }
  497. static void p4_unreserve(void)
  498. {
  499. #ifdef CONFIG_SMP
  500. if (smp_num_siblings > 1)
  501. release_perfctr_nmi(MSR_P4_IQ_PERFCTR1);
  502. #endif
  503. release_evntsel_nmi(MSR_P4_CRU_ESCR0);
  504. release_perfctr_nmi(MSR_P4_IQ_PERFCTR0);
  505. }
  506. static void p4_rearm(struct nmi_watchdog_ctlblk *wd, unsigned nmi_hz)
  507. {
  508. unsigned dummy;
  509. /*
  510. * P4 quirks:
  511. * - An overflown perfctr will assert its interrupt
  512. * until the OVF flag in its CCCR is cleared.
  513. * - LVTPC is masked on interrupt and must be
  514. * unmasked by the LVTPC handler.
  515. */
  516. rdmsrl(wd->cccr_msr, dummy);
  517. dummy &= ~P4_CCCR_OVF;
  518. wrmsrl(wd->cccr_msr, dummy);
  519. apic_write(APIC_LVTPC, APIC_DM_NMI);
  520. /* start the cycle over again */
  521. write_watchdog_counter(wd->perfctr_msr, NULL, nmi_hz);
  522. }
  523. static const struct wd_ops p4_wd_ops = {
  524. .reserve = p4_reserve,
  525. .unreserve = p4_unreserve,
  526. .setup = setup_p4_watchdog,
  527. .rearm = p4_rearm,
  528. .stop = stop_p4_watchdog,
  529. /* RED-PEN this is wrong for the other sibling */
  530. .perfctr = MSR_P4_BPU_PERFCTR0,
  531. .evntsel = MSR_P4_BSU_ESCR0,
  532. .checkbit = 1ULL << 39,
  533. };
  534. /*
  535. * Watchdog using the Intel architected PerfMon.
  536. * Used for Core2 and hopefully all future Intel CPUs.
  537. */
  538. #define ARCH_PERFMON_NMI_EVENT_SEL ARCH_PERFMON_UNHALTED_CORE_CYCLES_SEL
  539. #define ARCH_PERFMON_NMI_EVENT_UMASK ARCH_PERFMON_UNHALTED_CORE_CYCLES_UMASK
  540. static struct wd_ops intel_arch_wd_ops;
  541. static int setup_intel_arch_watchdog(unsigned nmi_hz)
  542. {
  543. unsigned int ebx;
  544. union cpuid10_eax eax;
  545. unsigned int unused;
  546. unsigned int perfctr_msr, evntsel_msr;
  547. unsigned int evntsel;
  548. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  549. /*
  550. * Check whether the Architectural PerfMon supports
  551. * Unhalted Core Cycles Event or not.
  552. * NOTE: Corresponding bit = 0 in ebx indicates event present.
  553. */
  554. cpuid(10, &(eax.full), &ebx, &unused, &unused);
  555. if ((eax.split.mask_length < (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX+1)) ||
  556. (ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT))
  557. return 0;
  558. perfctr_msr = wd_ops->perfctr;
  559. evntsel_msr = wd_ops->evntsel;
  560. wrmsrl(perfctr_msr, 0UL);
  561. evntsel = ARCH_PERFMON_EVENTSEL_INT
  562. | ARCH_PERFMON_EVENTSEL_OS
  563. | ARCH_PERFMON_EVENTSEL_USR
  564. | ARCH_PERFMON_NMI_EVENT_SEL
  565. | ARCH_PERFMON_NMI_EVENT_UMASK;
  566. /* setup the timer */
  567. wrmsr(evntsel_msr, evntsel, 0);
  568. nmi_hz = adjust_for_32bit_ctr(nmi_hz);
  569. write_watchdog_counter32(perfctr_msr, "INTEL_ARCH_PERFCTR0", nmi_hz);
  570. apic_write(APIC_LVTPC, APIC_DM_NMI);
  571. evntsel |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  572. wrmsr(evntsel_msr, evntsel, 0);
  573. wd->perfctr_msr = perfctr_msr;
  574. wd->evntsel_msr = evntsel_msr;
  575. wd->cccr_msr = 0; /* unused */
  576. intel_arch_wd_ops.checkbit = 1ULL << (eax.split.bit_width - 1);
  577. return 1;
  578. }
  579. static struct wd_ops intel_arch_wd_ops __read_mostly = {
  580. .reserve = single_msr_reserve,
  581. .unreserve = single_msr_unreserve,
  582. .setup = setup_intel_arch_watchdog,
  583. .rearm = p6_rearm,
  584. .stop = single_msr_stop_watchdog,
  585. .perfctr = MSR_ARCH_PERFMON_PERFCTR1,
  586. .evntsel = MSR_ARCH_PERFMON_EVENTSEL1,
  587. };
  588. static void probe_nmi_watchdog(void)
  589. {
  590. switch (boot_cpu_data.x86_vendor) {
  591. case X86_VENDOR_AMD:
  592. if (boot_cpu_data.x86 != 6 && boot_cpu_data.x86 != 15 &&
  593. boot_cpu_data.x86 != 16)
  594. return;
  595. wd_ops = &k7_wd_ops;
  596. break;
  597. case X86_VENDOR_INTEL:
  598. /*
  599. * Work around Core Duo (Yonah) errata AE49 where perfctr1
  600. * doesn't have a working enable bit.
  601. */
  602. if (boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model == 14) {
  603. intel_arch_wd_ops.perfctr = MSR_ARCH_PERFMON_PERFCTR0;
  604. intel_arch_wd_ops.evntsel = MSR_ARCH_PERFMON_EVENTSEL0;
  605. }
  606. if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
  607. wd_ops = &intel_arch_wd_ops;
  608. break;
  609. }
  610. switch (boot_cpu_data.x86) {
  611. case 6:
  612. if (boot_cpu_data.x86_model > 13)
  613. return;
  614. wd_ops = &p6_wd_ops;
  615. break;
  616. case 15:
  617. wd_ops = &p4_wd_ops;
  618. break;
  619. default:
  620. return;
  621. }
  622. break;
  623. }
  624. }
  625. /* Interface to nmi.c */
  626. int lapic_watchdog_init(unsigned nmi_hz)
  627. {
  628. if (!wd_ops) {
  629. probe_nmi_watchdog();
  630. if (!wd_ops) {
  631. printk(KERN_INFO "NMI watchdog: CPU not supported\n");
  632. return -1;
  633. }
  634. if (!wd_ops->reserve()) {
  635. printk(KERN_ERR
  636. "NMI watchdog: cannot reserve perfctrs\n");
  637. return -1;
  638. }
  639. }
  640. if (!(wd_ops->setup(nmi_hz))) {
  641. printk(KERN_ERR "Cannot setup NMI watchdog on CPU %d\n",
  642. raw_smp_processor_id());
  643. return -1;
  644. }
  645. return 0;
  646. }
  647. void lapic_watchdog_stop(void)
  648. {
  649. if (wd_ops)
  650. wd_ops->stop();
  651. }
  652. unsigned lapic_adjust_nmi_hz(unsigned hz)
  653. {
  654. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  655. if (wd->perfctr_msr == MSR_P6_PERFCTR0 ||
  656. wd->perfctr_msr == MSR_ARCH_PERFMON_PERFCTR1)
  657. hz = adjust_for_32bit_ctr(hz);
  658. return hz;
  659. }
  660. int lapic_wd_event(unsigned nmi_hz)
  661. {
  662. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  663. u64 ctr;
  664. rdmsrl(wd->perfctr_msr, ctr);
  665. if (ctr & wd_ops->checkbit) /* perfctr still running? */
  666. return 0;
  667. wd_ops->rearm(wd, nmi_hz);
  668. return 1;
  669. }
  670. int lapic_watchdog_ok(void)
  671. {
  672. return wd_ops != NULL;
  673. }