perfctr-watchdog.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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. /* initialize the wd struct before enabling */
  248. wd->perfctr_msr = perfctr_msr;
  249. wd->evntsel_msr = evntsel_msr;
  250. wd->cccr_msr = 0; /* unused */
  251. /* ok, everything is initialized, announce that we're set */
  252. cpu_nmi_set_wd_enabled();
  253. apic_write(APIC_LVTPC, APIC_DM_NMI);
  254. evntsel |= K7_EVNTSEL_ENABLE;
  255. wrmsr(evntsel_msr, evntsel, 0);
  256. return 1;
  257. }
  258. static void single_msr_stop_watchdog(void)
  259. {
  260. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  261. wrmsr(wd->evntsel_msr, 0, 0);
  262. }
  263. static int single_msr_reserve(void)
  264. {
  265. if (!reserve_perfctr_nmi(wd_ops->perfctr))
  266. return 0;
  267. if (!reserve_evntsel_nmi(wd_ops->evntsel)) {
  268. release_perfctr_nmi(wd_ops->perfctr);
  269. return 0;
  270. }
  271. return 1;
  272. }
  273. static void single_msr_unreserve(void)
  274. {
  275. release_evntsel_nmi(wd_ops->evntsel);
  276. release_perfctr_nmi(wd_ops->perfctr);
  277. }
  278. static void single_msr_rearm(struct nmi_watchdog_ctlblk *wd, unsigned nmi_hz)
  279. {
  280. /* start the cycle over again */
  281. write_watchdog_counter(wd->perfctr_msr, NULL, nmi_hz);
  282. }
  283. static const struct wd_ops k7_wd_ops = {
  284. .reserve = single_msr_reserve,
  285. .unreserve = single_msr_unreserve,
  286. .setup = setup_k7_watchdog,
  287. .rearm = single_msr_rearm,
  288. .stop = single_msr_stop_watchdog,
  289. .perfctr = MSR_K7_PERFCTR0,
  290. .evntsel = MSR_K7_EVNTSEL0,
  291. .checkbit = 1ULL << 47,
  292. };
  293. /*
  294. * Intel Model 6 (PPro+,P2,P3,P-M,Core1)
  295. */
  296. #define P6_EVNTSEL0_ENABLE (1 << 22)
  297. #define P6_EVNTSEL_INT (1 << 20)
  298. #define P6_EVNTSEL_OS (1 << 17)
  299. #define P6_EVNTSEL_USR (1 << 16)
  300. #define P6_EVENT_CPU_CLOCKS_NOT_HALTED 0x79
  301. #define P6_NMI_EVENT P6_EVENT_CPU_CLOCKS_NOT_HALTED
  302. static int setup_p6_watchdog(unsigned nmi_hz)
  303. {
  304. unsigned int perfctr_msr, evntsel_msr;
  305. unsigned int evntsel;
  306. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  307. perfctr_msr = wd_ops->perfctr;
  308. evntsel_msr = wd_ops->evntsel;
  309. /* KVM doesn't implement this MSR */
  310. if (wrmsr_safe(perfctr_msr, 0, 0) < 0)
  311. return 0;
  312. evntsel = P6_EVNTSEL_INT
  313. | P6_EVNTSEL_OS
  314. | P6_EVNTSEL_USR
  315. | P6_NMI_EVENT;
  316. /* setup the timer */
  317. wrmsr(evntsel_msr, evntsel, 0);
  318. nmi_hz = adjust_for_32bit_ctr(nmi_hz);
  319. write_watchdog_counter32(perfctr_msr, "P6_PERFCTR0",nmi_hz);
  320. /* initialize the wd struct before enabling */
  321. wd->perfctr_msr = perfctr_msr;
  322. wd->evntsel_msr = evntsel_msr;
  323. wd->cccr_msr = 0; /* unused */
  324. /* ok, everything is initialized, announce that we're set */
  325. cpu_nmi_set_wd_enabled();
  326. apic_write(APIC_LVTPC, APIC_DM_NMI);
  327. evntsel |= P6_EVNTSEL0_ENABLE;
  328. wrmsr(evntsel_msr, evntsel, 0);
  329. return 1;
  330. }
  331. static void p6_rearm(struct nmi_watchdog_ctlblk *wd, unsigned nmi_hz)
  332. {
  333. /*
  334. * P6 based Pentium M need to re-unmask
  335. * the apic vector but it doesn't hurt
  336. * other P6 variant.
  337. * ArchPerfom/Core Duo also needs this
  338. */
  339. apic_write(APIC_LVTPC, APIC_DM_NMI);
  340. /* P6/ARCH_PERFMON has 32 bit counter write */
  341. write_watchdog_counter32(wd->perfctr_msr, NULL,nmi_hz);
  342. }
  343. static const struct wd_ops p6_wd_ops = {
  344. .reserve = single_msr_reserve,
  345. .unreserve = single_msr_unreserve,
  346. .setup = setup_p6_watchdog,
  347. .rearm = p6_rearm,
  348. .stop = single_msr_stop_watchdog,
  349. .perfctr = MSR_P6_PERFCTR0,
  350. .evntsel = MSR_P6_EVNTSEL0,
  351. .checkbit = 1ULL << 39,
  352. };
  353. /*
  354. * Intel P4 performance counters.
  355. * By far the most complicated of all.
  356. */
  357. #define MSR_P4_MISC_ENABLE_PERF_AVAIL (1 << 7)
  358. #define P4_ESCR_EVENT_SELECT(N) ((N) << 25)
  359. #define P4_ESCR_OS (1 << 3)
  360. #define P4_ESCR_USR (1 << 2)
  361. #define P4_CCCR_OVF_PMI0 (1 << 26)
  362. #define P4_CCCR_OVF_PMI1 (1 << 27)
  363. #define P4_CCCR_THRESHOLD(N) ((N) << 20)
  364. #define P4_CCCR_COMPLEMENT (1 << 19)
  365. #define P4_CCCR_COMPARE (1 << 18)
  366. #define P4_CCCR_REQUIRED (3 << 16)
  367. #define P4_CCCR_ESCR_SELECT(N) ((N) << 13)
  368. #define P4_CCCR_ENABLE (1 << 12)
  369. #define P4_CCCR_OVF (1 << 31)
  370. #define P4_CONTROLS 18
  371. static unsigned int p4_controls[18] = {
  372. MSR_P4_BPU_CCCR0,
  373. MSR_P4_BPU_CCCR1,
  374. MSR_P4_BPU_CCCR2,
  375. MSR_P4_BPU_CCCR3,
  376. MSR_P4_MS_CCCR0,
  377. MSR_P4_MS_CCCR1,
  378. MSR_P4_MS_CCCR2,
  379. MSR_P4_MS_CCCR3,
  380. MSR_P4_FLAME_CCCR0,
  381. MSR_P4_FLAME_CCCR1,
  382. MSR_P4_FLAME_CCCR2,
  383. MSR_P4_FLAME_CCCR3,
  384. MSR_P4_IQ_CCCR0,
  385. MSR_P4_IQ_CCCR1,
  386. MSR_P4_IQ_CCCR2,
  387. MSR_P4_IQ_CCCR3,
  388. MSR_P4_IQ_CCCR4,
  389. MSR_P4_IQ_CCCR5,
  390. };
  391. /*
  392. * Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
  393. * CRU_ESCR0 (with any non-null event selector) through a complemented
  394. * max threshold. [IA32-Vol3, Section 14.9.9]
  395. */
  396. static int setup_p4_watchdog(unsigned nmi_hz)
  397. {
  398. unsigned int perfctr_msr, evntsel_msr, cccr_msr;
  399. unsigned int evntsel, cccr_val;
  400. unsigned int misc_enable, dummy;
  401. unsigned int ht_num;
  402. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  403. rdmsr(MSR_IA32_MISC_ENABLE, misc_enable, dummy);
  404. if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
  405. return 0;
  406. #ifdef CONFIG_SMP
  407. /* detect which hyperthread we are on */
  408. if (smp_num_siblings == 2) {
  409. unsigned int ebx, apicid;
  410. ebx = cpuid_ebx(1);
  411. apicid = (ebx >> 24) & 0xff;
  412. ht_num = apicid & 1;
  413. } else
  414. #endif
  415. ht_num = 0;
  416. /*
  417. * performance counters are shared resources
  418. * assign each hyperthread its own set
  419. * (re-use the ESCR0 register, seems safe
  420. * and keeps the cccr_val the same)
  421. */
  422. if (!ht_num) {
  423. /* logical cpu 0 */
  424. perfctr_msr = MSR_P4_IQ_PERFCTR0;
  425. evntsel_msr = MSR_P4_CRU_ESCR0;
  426. cccr_msr = MSR_P4_IQ_CCCR0;
  427. cccr_val = P4_CCCR_OVF_PMI0 | P4_CCCR_ESCR_SELECT(4);
  428. /*
  429. * If we're on the kdump kernel or other situation, we may
  430. * still have other performance counter registers set to
  431. * interrupt and they'll keep interrupting forever because
  432. * of the P4_CCCR_OVF quirk. So we need to ACK all the
  433. * pending interrupts and disable all the registers here,
  434. * before reenabling the NMI delivery. Refer to p4_rearm()
  435. * about the P4_CCCR_OVF quirk.
  436. */
  437. if (reset_devices) {
  438. unsigned int low, high;
  439. int i;
  440. for (i = 0; i < P4_CONTROLS; i++) {
  441. rdmsr(p4_controls[i], low, high);
  442. low &= ~(P4_CCCR_ENABLE | P4_CCCR_OVF);
  443. wrmsr(p4_controls[i], low, high);
  444. }
  445. }
  446. } else {
  447. /* logical cpu 1 */
  448. perfctr_msr = MSR_P4_IQ_PERFCTR1;
  449. evntsel_msr = MSR_P4_CRU_ESCR0;
  450. cccr_msr = MSR_P4_IQ_CCCR1;
  451. /* Pentium 4 D processors don't support P4_CCCR_OVF_PMI1 */
  452. if (boot_cpu_data.x86_model == 4 && boot_cpu_data.x86_mask == 4)
  453. cccr_val = P4_CCCR_OVF_PMI0;
  454. else
  455. cccr_val = P4_CCCR_OVF_PMI1;
  456. cccr_val |= P4_CCCR_ESCR_SELECT(4);
  457. }
  458. evntsel = P4_ESCR_EVENT_SELECT(0x3F)
  459. | P4_ESCR_OS
  460. | P4_ESCR_USR;
  461. cccr_val |= P4_CCCR_THRESHOLD(15)
  462. | P4_CCCR_COMPLEMENT
  463. | P4_CCCR_COMPARE
  464. | P4_CCCR_REQUIRED;
  465. wrmsr(evntsel_msr, evntsel, 0);
  466. wrmsr(cccr_msr, cccr_val, 0);
  467. write_watchdog_counter(perfctr_msr, "P4_IQ_COUNTER0", nmi_hz);
  468. wd->perfctr_msr = perfctr_msr;
  469. wd->evntsel_msr = evntsel_msr;
  470. wd->cccr_msr = cccr_msr;
  471. /* ok, everything is initialized, announce that we're set */
  472. cpu_nmi_set_wd_enabled();
  473. apic_write(APIC_LVTPC, APIC_DM_NMI);
  474. cccr_val |= P4_CCCR_ENABLE;
  475. wrmsr(cccr_msr, cccr_val, 0);
  476. return 1;
  477. }
  478. static void stop_p4_watchdog(void)
  479. {
  480. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  481. wrmsr(wd->cccr_msr, 0, 0);
  482. wrmsr(wd->evntsel_msr, 0, 0);
  483. }
  484. static int p4_reserve(void)
  485. {
  486. if (!reserve_perfctr_nmi(MSR_P4_IQ_PERFCTR0))
  487. return 0;
  488. #ifdef CONFIG_SMP
  489. if (smp_num_siblings > 1 && !reserve_perfctr_nmi(MSR_P4_IQ_PERFCTR1))
  490. goto fail1;
  491. #endif
  492. if (!reserve_evntsel_nmi(MSR_P4_CRU_ESCR0))
  493. goto fail2;
  494. /* RED-PEN why is ESCR1 not reserved here? */
  495. return 1;
  496. fail2:
  497. #ifdef CONFIG_SMP
  498. if (smp_num_siblings > 1)
  499. release_perfctr_nmi(MSR_P4_IQ_PERFCTR1);
  500. fail1:
  501. #endif
  502. release_perfctr_nmi(MSR_P4_IQ_PERFCTR0);
  503. return 0;
  504. }
  505. static void p4_unreserve(void)
  506. {
  507. #ifdef CONFIG_SMP
  508. if (smp_num_siblings > 1)
  509. release_perfctr_nmi(MSR_P4_IQ_PERFCTR1);
  510. #endif
  511. release_evntsel_nmi(MSR_P4_CRU_ESCR0);
  512. release_perfctr_nmi(MSR_P4_IQ_PERFCTR0);
  513. }
  514. static void p4_rearm(struct nmi_watchdog_ctlblk *wd, unsigned nmi_hz)
  515. {
  516. unsigned dummy;
  517. /*
  518. * P4 quirks:
  519. * - An overflown perfctr will assert its interrupt
  520. * until the OVF flag in its CCCR is cleared.
  521. * - LVTPC is masked on interrupt and must be
  522. * unmasked by the LVTPC handler.
  523. */
  524. rdmsrl(wd->cccr_msr, dummy);
  525. dummy &= ~P4_CCCR_OVF;
  526. wrmsrl(wd->cccr_msr, dummy);
  527. apic_write(APIC_LVTPC, APIC_DM_NMI);
  528. /* start the cycle over again */
  529. write_watchdog_counter(wd->perfctr_msr, NULL, nmi_hz);
  530. }
  531. static const struct wd_ops p4_wd_ops = {
  532. .reserve = p4_reserve,
  533. .unreserve = p4_unreserve,
  534. .setup = setup_p4_watchdog,
  535. .rearm = p4_rearm,
  536. .stop = stop_p4_watchdog,
  537. /* RED-PEN this is wrong for the other sibling */
  538. .perfctr = MSR_P4_BPU_PERFCTR0,
  539. .evntsel = MSR_P4_BSU_ESCR0,
  540. .checkbit = 1ULL << 39,
  541. };
  542. /*
  543. * Watchdog using the Intel architected PerfMon.
  544. * Used for Core2 and hopefully all future Intel CPUs.
  545. */
  546. #define ARCH_PERFMON_NMI_EVENT_SEL ARCH_PERFMON_UNHALTED_CORE_CYCLES_SEL
  547. #define ARCH_PERFMON_NMI_EVENT_UMASK ARCH_PERFMON_UNHALTED_CORE_CYCLES_UMASK
  548. static struct wd_ops intel_arch_wd_ops;
  549. static int setup_intel_arch_watchdog(unsigned nmi_hz)
  550. {
  551. unsigned int ebx;
  552. union cpuid10_eax eax;
  553. unsigned int unused;
  554. unsigned int perfctr_msr, evntsel_msr;
  555. unsigned int evntsel;
  556. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  557. /*
  558. * Check whether the Architectural PerfMon supports
  559. * Unhalted Core Cycles Event or not.
  560. * NOTE: Corresponding bit = 0 in ebx indicates event present.
  561. */
  562. cpuid(10, &(eax.full), &ebx, &unused, &unused);
  563. if ((eax.split.mask_length < (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX+1)) ||
  564. (ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT))
  565. return 0;
  566. perfctr_msr = wd_ops->perfctr;
  567. evntsel_msr = wd_ops->evntsel;
  568. wrmsrl(perfctr_msr, 0UL);
  569. evntsel = ARCH_PERFMON_EVENTSEL_INT
  570. | ARCH_PERFMON_EVENTSEL_OS
  571. | ARCH_PERFMON_EVENTSEL_USR
  572. | ARCH_PERFMON_NMI_EVENT_SEL
  573. | ARCH_PERFMON_NMI_EVENT_UMASK;
  574. /* setup the timer */
  575. wrmsr(evntsel_msr, evntsel, 0);
  576. nmi_hz = adjust_for_32bit_ctr(nmi_hz);
  577. write_watchdog_counter32(perfctr_msr, "INTEL_ARCH_PERFCTR0", nmi_hz);
  578. wd->perfctr_msr = perfctr_msr;
  579. wd->evntsel_msr = evntsel_msr;
  580. wd->cccr_msr = 0; /* unused */
  581. /* ok, everything is initialized, announce that we're set */
  582. cpu_nmi_set_wd_enabled();
  583. apic_write(APIC_LVTPC, APIC_DM_NMI);
  584. evntsel |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  585. wrmsr(evntsel_msr, evntsel, 0);
  586. intel_arch_wd_ops.checkbit = 1ULL << (eax.split.bit_width - 1);
  587. return 1;
  588. }
  589. static struct wd_ops intel_arch_wd_ops __read_mostly = {
  590. .reserve = single_msr_reserve,
  591. .unreserve = single_msr_unreserve,
  592. .setup = setup_intel_arch_watchdog,
  593. .rearm = p6_rearm,
  594. .stop = single_msr_stop_watchdog,
  595. .perfctr = MSR_ARCH_PERFMON_PERFCTR1,
  596. .evntsel = MSR_ARCH_PERFMON_EVENTSEL1,
  597. };
  598. static void probe_nmi_watchdog(void)
  599. {
  600. switch (boot_cpu_data.x86_vendor) {
  601. case X86_VENDOR_AMD:
  602. if (boot_cpu_data.x86 != 6 && boot_cpu_data.x86 != 15 &&
  603. boot_cpu_data.x86 != 16)
  604. return;
  605. wd_ops = &k7_wd_ops;
  606. break;
  607. case X86_VENDOR_INTEL:
  608. /*
  609. * Work around Core Duo (Yonah) errata AE49 where perfctr1
  610. * doesn't have a working enable bit.
  611. */
  612. if (boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model == 14) {
  613. intel_arch_wd_ops.perfctr = MSR_ARCH_PERFMON_PERFCTR0;
  614. intel_arch_wd_ops.evntsel = MSR_ARCH_PERFMON_EVENTSEL0;
  615. }
  616. if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
  617. wd_ops = &intel_arch_wd_ops;
  618. break;
  619. }
  620. switch (boot_cpu_data.x86) {
  621. case 6:
  622. if (boot_cpu_data.x86_model > 13)
  623. return;
  624. wd_ops = &p6_wd_ops;
  625. break;
  626. case 15:
  627. wd_ops = &p4_wd_ops;
  628. break;
  629. default:
  630. return;
  631. }
  632. break;
  633. }
  634. }
  635. /* Interface to nmi.c */
  636. int lapic_watchdog_init(unsigned nmi_hz)
  637. {
  638. if (!wd_ops) {
  639. probe_nmi_watchdog();
  640. if (!wd_ops) {
  641. printk(KERN_INFO "NMI watchdog: CPU not supported\n");
  642. return -1;
  643. }
  644. if (!wd_ops->reserve()) {
  645. printk(KERN_ERR
  646. "NMI watchdog: cannot reserve perfctrs\n");
  647. return -1;
  648. }
  649. }
  650. if (!(wd_ops->setup(nmi_hz))) {
  651. printk(KERN_ERR "Cannot setup NMI watchdog on CPU %d\n",
  652. raw_smp_processor_id());
  653. return -1;
  654. }
  655. return 0;
  656. }
  657. void lapic_watchdog_stop(void)
  658. {
  659. if (wd_ops)
  660. wd_ops->stop();
  661. }
  662. unsigned lapic_adjust_nmi_hz(unsigned hz)
  663. {
  664. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  665. if (wd->perfctr_msr == MSR_P6_PERFCTR0 ||
  666. wd->perfctr_msr == MSR_ARCH_PERFMON_PERFCTR1)
  667. hz = adjust_for_32bit_ctr(hz);
  668. return hz;
  669. }
  670. int lapic_wd_event(unsigned nmi_hz)
  671. {
  672. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  673. u64 ctr;
  674. rdmsrl(wd->perfctr_msr, ctr);
  675. if (ctr & wd_ops->checkbit) /* perfctr still running? */
  676. return 0;
  677. wd_ops->rearm(wd, nmi_hz);
  678. return 1;
  679. }
  680. int lapic_watchdog_ok(void)
  681. {
  682. return wd_ops != NULL;
  683. }