perf_event_p4.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. * Netburst Perfomance Events (P4, old Xeon)
  3. *
  4. * Copyright (C) 2010 Parallels, Inc., Cyrill Gorcunov <gorcunov@openvz.org>
  5. * Copyright (C) 2010 Intel Corporation, Lin Ming <ming.m.lin@intel.com>
  6. *
  7. * For licencing details see kernel-base/COPYING
  8. */
  9. #ifdef CONFIG_CPU_SUP_INTEL
  10. #include <asm/perf_event_p4.h>
  11. /*
  12. * array indices: 0,1 - HT threads, used with HT enabled cpu
  13. */
  14. struct p4_event_template {
  15. u32 opcode; /* ESCR event + CCCR selector */
  16. u64 config; /* packed predefined bits */
  17. int dep; /* upstream dependency event index */
  18. unsigned int emask; /* ESCR EventMask */
  19. unsigned int escr_msr[2]; /* ESCR MSR for this event */
  20. unsigned int cntr[2]; /* counter index (offset) */
  21. };
  22. struct p4_pmu_res {
  23. /* maps hw_conf::idx into template for ESCR sake */
  24. struct p4_event_template *tpl[ARCH_P4_MAX_CCCR];
  25. };
  26. static DEFINE_PER_CPU(struct p4_pmu_res, p4_pmu_config);
  27. /*
  28. * WARN: CCCR1 doesn't have a working enable bit so try to not
  29. * use it if possible
  30. *
  31. * Also as only we start to support raw events we will need to
  32. * append _all_ P4_EVENT_PACK'ed events here
  33. */
  34. struct p4_event_template p4_templates[] = {
  35. [0] = {
  36. .opcode = P4_UOP_TYPE,
  37. .config = 0,
  38. .dep = -1,
  39. .emask =
  40. P4_EVENT_ATTR(P4_UOP_TYPE, TAGLOADS) |
  41. P4_EVENT_ATTR(P4_UOP_TYPE, TAGSTORES),
  42. .escr_msr = { MSR_P4_RAT_ESCR0, MSR_P4_RAT_ESCR1 },
  43. .cntr = { 16, 17 },
  44. },
  45. [1] = {
  46. .opcode = P4_GLOBAL_POWER_EVENTS,
  47. .config = 0,
  48. .dep = -1,
  49. .emask =
  50. P4_EVENT_ATTR(P4_GLOBAL_POWER_EVENTS, RUNNING),
  51. .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
  52. .cntr = { 0, 2 },
  53. },
  54. [2] = {
  55. .opcode = P4_INSTR_RETIRED,
  56. .config = 0,
  57. .dep = -1, /* needs front-end tagging */
  58. .emask =
  59. P4_EVENT_ATTR(P4_INSTR_RETIRED, NBOGUSNTAG) |
  60. P4_EVENT_ATTR(P4_INSTR_RETIRED, BOGUSNTAG),
  61. .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
  62. .cntr = { 12, 14 },
  63. },
  64. [3] = {
  65. .opcode = P4_BSQ_CACHE_REFERENCE,
  66. .config = 0,
  67. .dep = -1,
  68. .emask =
  69. P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_2ndL_HITS) |
  70. P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_2ndL_HITE) |
  71. P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_2ndL_HITM) |
  72. P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_3rdL_HITS) |
  73. P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_3rdL_HITE) |
  74. P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_3rdL_HITM),
  75. .escr_msr = { MSR_P4_BSU_ESCR0, MSR_P4_BSU_ESCR1 },
  76. .cntr = { 0, 2 },
  77. },
  78. [4] = {
  79. .opcode = P4_BSQ_CACHE_REFERENCE,
  80. .config = 0,
  81. .dep = -1,
  82. .emask =
  83. P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_2ndL_MISS) |
  84. P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_3rdL_MISS) |
  85. P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, WR_2ndL_MISS),
  86. .escr_msr = { MSR_P4_BSU_ESCR0, MSR_P4_BSU_ESCR1 },
  87. .cntr = { 0, 3 },
  88. },
  89. [5] = {
  90. .opcode = P4_RETIRED_BRANCH_TYPE,
  91. .config = 0,
  92. .dep = -1,
  93. .emask =
  94. P4_EVENT_ATTR(P4_RETIRED_BRANCH_TYPE, CONDITIONAL) |
  95. P4_EVENT_ATTR(P4_RETIRED_BRANCH_TYPE, CALL) |
  96. P4_EVENT_ATTR(P4_RETIRED_BRANCH_TYPE, RETURN) |
  97. P4_EVENT_ATTR(P4_RETIRED_BRANCH_TYPE, INDIRECT),
  98. .escr_msr = { MSR_P4_TBPU_ESCR0, MSR_P4_TBPU_ESCR1 },
  99. .cntr = { 4, 6 },
  100. },
  101. [6] = {
  102. .opcode = P4_MISPRED_BRANCH_RETIRED,
  103. .config = 0,
  104. .dep = -1,
  105. .emask =
  106. P4_EVENT_ATTR(P4_MISPRED_BRANCH_RETIRED, NBOGUS),
  107. .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
  108. .cntr = { 12, 14 },
  109. },
  110. [7] = {
  111. .opcode = P4_FSB_DATA_ACTIVITY,
  112. .config = p4_config_pack_cccr(P4_CCCR_EDGE | P4_CCCR_COMPARE),
  113. .dep = -1,
  114. .emask =
  115. P4_EVENT_ATTR(P4_FSB_DATA_ACTIVITY, DRDY_DRV) |
  116. P4_EVENT_ATTR(P4_FSB_DATA_ACTIVITY, DRDY_OWN),
  117. .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
  118. .cntr = { 0, 2 },
  119. },
  120. };
  121. static struct p4_event_template *p4_event_map[PERF_COUNT_HW_MAX] = {
  122. /* non-halted CPU clocks */
  123. [PERF_COUNT_HW_CPU_CYCLES] = &p4_templates[1],
  124. /* retired instructions: dep on tagging the FSB */
  125. [PERF_COUNT_HW_INSTRUCTIONS] = &p4_templates[2],
  126. /* cache hits */
  127. [PERF_COUNT_HW_CACHE_REFERENCES] = &p4_templates[3],
  128. /* cache misses */
  129. [PERF_COUNT_HW_CACHE_MISSES] = &p4_templates[4],
  130. /* branch instructions retired */
  131. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = &p4_templates[5],
  132. /* mispredicted branches retired */
  133. [PERF_COUNT_HW_BRANCH_MISSES] = &p4_templates[6],
  134. /* bus ready clocks (cpu is driving #DRDY_DRV\#DRDY_OWN): */
  135. [PERF_COUNT_HW_BUS_CYCLES] = &p4_templates[7],
  136. };
  137. static u64 p4_pmu_event_map(int hw_event)
  138. {
  139. struct p4_event_template *tpl;
  140. u64 config;
  141. if (hw_event > ARRAY_SIZE(p4_event_map)) {
  142. printk_once(KERN_ERR "PMU: Incorrect event index\n");
  143. return 0;
  144. }
  145. tpl = p4_event_map[hw_event];
  146. /*
  147. * fill config up according to
  148. * a predefined event template
  149. */
  150. config = tpl->config;
  151. config |= p4_config_pack_escr(P4_EVENT_UNPACK_EVENT(tpl->opcode) << P4_EVNTSEL_EVENT_SHIFT);
  152. config |= p4_config_pack_escr(tpl->emask << P4_EVNTSEL_EVENTMASK_SHIFT);
  153. config |= p4_config_pack_cccr(P4_EVENT_UNPACK_SELECTOR(tpl->opcode) << P4_CCCR_ESCR_SELECT_SHIFT);
  154. /* on HT machine we need a special bit */
  155. if (p4_ht_active() && p4_ht_thread(raw_smp_processor_id()))
  156. config = p4_set_ht_bit(config);
  157. return config;
  158. }
  159. /*
  160. * Note that we still have 5 events (from global events SDM list)
  161. * intersected in opcode+emask bits so we will need another
  162. * scheme there do distinguish templates.
  163. */
  164. static inline int p4_pmu_emask_match(unsigned int dst, unsigned int src)
  165. {
  166. return dst & src;
  167. }
  168. static struct p4_event_template *p4_pmu_template_lookup(u64 config)
  169. {
  170. u32 opcode = p4_config_unpack_opcode(config);
  171. unsigned int emask = p4_config_unpack_emask(config);
  172. unsigned int i;
  173. for (i = 0; i < ARRAY_SIZE(p4_templates); i++) {
  174. if (opcode == p4_templates[i].opcode &&
  175. p4_pmu_emask_match(emask, p4_templates[i].emask))
  176. return &p4_templates[i];
  177. }
  178. return NULL;
  179. }
  180. /*
  181. * We don't control raw events so it's up to the caller
  182. * to pass sane values (and we don't count the thread number
  183. * on HT machine but allow HT-compatible specifics to be
  184. * passed on)
  185. */
  186. static u64 p4_pmu_raw_event(u64 hw_event)
  187. {
  188. return hw_event &
  189. (p4_config_pack_escr(P4_EVNTSEL_MASK_HT) |
  190. p4_config_pack_cccr(P4_CCCR_MASK_HT));
  191. }
  192. static int p4_hw_config(struct perf_event_attr *attr, struct hw_perf_event *hwc)
  193. {
  194. int cpu = raw_smp_processor_id();
  195. /*
  196. * the reason we use cpu that early is that: if we get scheduled
  197. * first time on the same cpu -- we will not need swap thread
  198. * specific flags in config (and will save some cpu cycles)
  199. */
  200. /* CCCR by default */
  201. hwc->config = p4_config_pack_cccr(p4_default_cccr_conf(cpu));
  202. /* Count user and OS events unless not requested to */
  203. hwc->config |= p4_config_pack_escr(p4_default_escr_conf(cpu, attr->exclude_kernel,
  204. attr->exclude_user));
  205. return 0;
  206. }
  207. static inline void p4_pmu_clear_cccr_ovf(struct hw_perf_event *hwc)
  208. {
  209. unsigned long dummy;
  210. rdmsrl(hwc->config_base + hwc->idx, dummy);
  211. if (dummy & P4_CCCR_OVF) {
  212. (void)checking_wrmsrl(hwc->config_base + hwc->idx,
  213. ((u64)dummy) & ~P4_CCCR_OVF);
  214. }
  215. }
  216. static inline void p4_pmu_disable_event(struct perf_event *event)
  217. {
  218. struct hw_perf_event *hwc = &event->hw;
  219. /*
  220. * If event gets disabled while counter is in overflowed
  221. * state we need to clear P4_CCCR_OVF, otherwise interrupt get
  222. * asserted again and again
  223. */
  224. (void)checking_wrmsrl(hwc->config_base + hwc->idx,
  225. (u64)(p4_config_unpack_cccr(hwc->config)) &
  226. ~P4_CCCR_ENABLE & ~P4_CCCR_OVF);
  227. }
  228. static void p4_pmu_disable_all(void)
  229. {
  230. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  231. int idx;
  232. for (idx = 0; idx < x86_pmu.num_events; idx++) {
  233. struct perf_event *event = cpuc->events[idx];
  234. if (!test_bit(idx, cpuc->active_mask))
  235. continue;
  236. p4_pmu_disable_event(event);
  237. }
  238. }
  239. static void p4_pmu_enable_event(struct perf_event *event)
  240. {
  241. struct hw_perf_event *hwc = &event->hw;
  242. int thread = p4_ht_config_thread(hwc->config);
  243. u64 escr_conf = p4_config_unpack_escr(p4_clear_ht_bit(hwc->config));
  244. u64 escr_base;
  245. struct p4_event_template *tpl;
  246. struct p4_pmu_res *c;
  247. /*
  248. * some preparation work from per-cpu private fields
  249. * since we need to find out which ESCR to use
  250. */
  251. c = &__get_cpu_var(p4_pmu_config);
  252. tpl = c->tpl[hwc->idx];
  253. if (!tpl) {
  254. pr_crit("%s: Wrong index: %d\n", __func__, hwc->idx);
  255. return;
  256. }
  257. escr_base = (u64)tpl->escr_msr[thread];
  258. /*
  259. * - we dont support cascaded counters yet
  260. * - and counter 1 is broken (erratum)
  261. */
  262. WARN_ON_ONCE(p4_is_event_cascaded(hwc->config));
  263. WARN_ON_ONCE(hwc->idx == 1);
  264. (void)checking_wrmsrl(escr_base, escr_conf);
  265. (void)checking_wrmsrl(hwc->config_base + hwc->idx,
  266. (u64)(p4_config_unpack_cccr(hwc->config)) | P4_CCCR_ENABLE);
  267. }
  268. static void p4_pmu_enable_all(void)
  269. {
  270. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  271. int idx;
  272. for (idx = 0; idx < x86_pmu.num_events; idx++) {
  273. struct perf_event *event = cpuc->events[idx];
  274. if (!test_bit(idx, cpuc->active_mask))
  275. continue;
  276. p4_pmu_enable_event(event);
  277. }
  278. }
  279. static int p4_pmu_handle_irq(struct pt_regs *regs)
  280. {
  281. struct perf_sample_data data;
  282. struct cpu_hw_events *cpuc;
  283. struct perf_event *event;
  284. struct hw_perf_event *hwc;
  285. int idx, handled = 0;
  286. u64 val;
  287. data.addr = 0;
  288. data.raw = NULL;
  289. cpuc = &__get_cpu_var(cpu_hw_events);
  290. for (idx = 0; idx < x86_pmu.num_events; idx++) {
  291. if (!test_bit(idx, cpuc->active_mask))
  292. continue;
  293. event = cpuc->events[idx];
  294. hwc = &event->hw;
  295. WARN_ON_ONCE(hwc->idx != idx);
  296. /*
  297. * FIXME: Redundant call, actually not needed
  298. * but just to check if we're screwed
  299. */
  300. p4_pmu_clear_cccr_ovf(hwc);
  301. val = x86_perf_event_update(event);
  302. if (val & (1ULL << (x86_pmu.event_bits - 1)))
  303. continue;
  304. /*
  305. * event overflow
  306. */
  307. handled = 1;
  308. data.period = event->hw.last_period;
  309. if (!x86_perf_event_set_period(event))
  310. continue;
  311. if (perf_event_overflow(event, 1, &data, regs))
  312. p4_pmu_disable_event(event);
  313. }
  314. if (handled) {
  315. #ifdef CONFIG_X86_LOCAL_APIC
  316. /* p4 quirk: unmask it again */
  317. apic_write(APIC_LVTPC, apic_read(APIC_LVTPC) & ~APIC_LVT_MASKED);
  318. #endif
  319. inc_irq_stat(apic_perf_irqs);
  320. }
  321. return handled;
  322. }
  323. /*
  324. * swap thread specific fields according to a thread
  325. * we are going to run on
  326. */
  327. static void p4_pmu_swap_config_ts(struct hw_perf_event *hwc, int cpu)
  328. {
  329. u32 escr, cccr;
  330. /*
  331. * we either lucky and continue on same cpu or no HT support
  332. */
  333. if (!p4_should_swap_ts(hwc->config, cpu))
  334. return;
  335. /*
  336. * the event is migrated from an another logical
  337. * cpu, so we need to swap thread specific flags
  338. */
  339. escr = p4_config_unpack_escr(hwc->config);
  340. cccr = p4_config_unpack_cccr(hwc->config);
  341. if (p4_ht_thread(cpu)) {
  342. cccr &= ~P4_CCCR_OVF_PMI_T0;
  343. cccr |= P4_CCCR_OVF_PMI_T1;
  344. if (escr & P4_EVNTSEL_T0_OS) {
  345. escr &= ~P4_EVNTSEL_T0_OS;
  346. escr |= P4_EVNTSEL_T1_OS;
  347. }
  348. if (escr & P4_EVNTSEL_T0_USR) {
  349. escr &= ~P4_EVNTSEL_T0_USR;
  350. escr |= P4_EVNTSEL_T1_USR;
  351. }
  352. hwc->config = p4_config_pack_escr(escr);
  353. hwc->config |= p4_config_pack_cccr(cccr);
  354. hwc->config |= P4_CONFIG_HT;
  355. } else {
  356. cccr &= ~P4_CCCR_OVF_PMI_T1;
  357. cccr |= P4_CCCR_OVF_PMI_T0;
  358. if (escr & P4_EVNTSEL_T1_OS) {
  359. escr &= ~P4_EVNTSEL_T1_OS;
  360. escr |= P4_EVNTSEL_T0_OS;
  361. }
  362. if (escr & P4_EVNTSEL_T1_USR) {
  363. escr &= ~P4_EVNTSEL_T1_USR;
  364. escr |= P4_EVNTSEL_T0_USR;
  365. }
  366. hwc->config = p4_config_pack_escr(escr);
  367. hwc->config |= p4_config_pack_cccr(cccr);
  368. hwc->config &= ~P4_CONFIG_HT;
  369. }
  370. }
  371. /* ESCRs are not sequential in memory so we need a map */
  372. static unsigned int p4_escr_map[ARCH_P4_TOTAL_ESCR] = {
  373. MSR_P4_ALF_ESCR0, /* 0 */
  374. MSR_P4_ALF_ESCR1, /* 1 */
  375. MSR_P4_BPU_ESCR0, /* 2 */
  376. MSR_P4_BPU_ESCR1, /* 3 */
  377. MSR_P4_BSU_ESCR0, /* 4 */
  378. MSR_P4_BSU_ESCR1, /* 5 */
  379. MSR_P4_CRU_ESCR0, /* 6 */
  380. MSR_P4_CRU_ESCR1, /* 7 */
  381. MSR_P4_CRU_ESCR2, /* 8 */
  382. MSR_P4_CRU_ESCR3, /* 9 */
  383. MSR_P4_CRU_ESCR4, /* 10 */
  384. MSR_P4_CRU_ESCR5, /* 11 */
  385. MSR_P4_DAC_ESCR0, /* 12 */
  386. MSR_P4_DAC_ESCR1, /* 13 */
  387. MSR_P4_FIRM_ESCR0, /* 14 */
  388. MSR_P4_FIRM_ESCR1, /* 15 */
  389. MSR_P4_FLAME_ESCR0, /* 16 */
  390. MSR_P4_FLAME_ESCR1, /* 17 */
  391. MSR_P4_FSB_ESCR0, /* 18 */
  392. MSR_P4_FSB_ESCR1, /* 19 */
  393. MSR_P4_IQ_ESCR0, /* 20 */
  394. MSR_P4_IQ_ESCR1, /* 21 */
  395. MSR_P4_IS_ESCR0, /* 22 */
  396. MSR_P4_IS_ESCR1, /* 23 */
  397. MSR_P4_ITLB_ESCR0, /* 24 */
  398. MSR_P4_ITLB_ESCR1, /* 25 */
  399. MSR_P4_IX_ESCR0, /* 26 */
  400. MSR_P4_IX_ESCR1, /* 27 */
  401. MSR_P4_MOB_ESCR0, /* 28 */
  402. MSR_P4_MOB_ESCR1, /* 29 */
  403. MSR_P4_MS_ESCR0, /* 30 */
  404. MSR_P4_MS_ESCR1, /* 31 */
  405. MSR_P4_PMH_ESCR0, /* 32 */
  406. MSR_P4_PMH_ESCR1, /* 33 */
  407. MSR_P4_RAT_ESCR0, /* 34 */
  408. MSR_P4_RAT_ESCR1, /* 35 */
  409. MSR_P4_SAAT_ESCR0, /* 36 */
  410. MSR_P4_SAAT_ESCR1, /* 37 */
  411. MSR_P4_SSU_ESCR0, /* 38 */
  412. MSR_P4_SSU_ESCR1, /* 39 */
  413. MSR_P4_TBPU_ESCR0, /* 40 */
  414. MSR_P4_TBPU_ESCR1, /* 41 */
  415. MSR_P4_TC_ESCR0, /* 42 */
  416. MSR_P4_TC_ESCR1, /* 43 */
  417. MSR_P4_U2L_ESCR0, /* 44 */
  418. MSR_P4_U2L_ESCR1, /* 45 */
  419. };
  420. static int p4_get_escr_idx(unsigned int addr)
  421. {
  422. unsigned int i;
  423. for (i = 0; i < ARRAY_SIZE(p4_escr_map); i++) {
  424. if (addr == p4_escr_map[i])
  425. return i;
  426. }
  427. return -1;
  428. }
  429. static int p4_pmu_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
  430. {
  431. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  432. unsigned long escr_mask[BITS_TO_LONGS(ARCH_P4_TOTAL_ESCR)];
  433. struct hw_perf_event *hwc;
  434. struct p4_event_template *tpl;
  435. struct p4_pmu_res *c;
  436. int cpu = raw_smp_processor_id();
  437. int escr_idx, thread, i, num;
  438. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  439. bitmap_zero(escr_mask, ARCH_P4_TOTAL_ESCR);
  440. c = &__get_cpu_var(p4_pmu_config);
  441. /*
  442. * Firstly find out which resource events are going
  443. * to use, if ESCR+CCCR tuple is already borrowed
  444. * then get out of here
  445. */
  446. for (i = 0, num = n; i < n; i++, num--) {
  447. hwc = &cpuc->event_list[i]->hw;
  448. tpl = p4_pmu_template_lookup(hwc->config);
  449. if (!tpl)
  450. goto done;
  451. thread = p4_ht_thread(cpu);
  452. escr_idx = p4_get_escr_idx(tpl->escr_msr[thread]);
  453. if (escr_idx == -1)
  454. goto done;
  455. /* already allocated and remains on the same cpu */
  456. if (hwc->idx != -1 && !p4_should_swap_ts(hwc->config, cpu)) {
  457. if (assign)
  458. assign[i] = hwc->idx;
  459. /* upstream dependent event */
  460. if (unlikely(tpl->dep != -1))
  461. printk_once(KERN_WARNING "PMU: Dep events are "
  462. "not implemented yet\n");
  463. goto reserve;
  464. }
  465. /* it may be already borrowed */
  466. if (test_bit(tpl->cntr[thread], used_mask) ||
  467. test_bit(escr_idx, escr_mask))
  468. goto done;
  469. /*
  470. * ESCR+CCCR+COUNTERs are available to use lets swap
  471. * thread specific bits, push assigned bits
  472. * back and save template into per-cpu
  473. * area (which will allow us to find out the ESCR
  474. * to be used at moment of "enable event via real MSR")
  475. */
  476. p4_pmu_swap_config_ts(hwc, cpu);
  477. if (assign) {
  478. assign[i] = tpl->cntr[thread];
  479. c->tpl[assign[i]] = tpl;
  480. }
  481. reserve:
  482. set_bit(tpl->cntr[thread], used_mask);
  483. set_bit(escr_idx, escr_mask);
  484. }
  485. done:
  486. return num ? -ENOSPC : 0;
  487. }
  488. static __initconst struct x86_pmu p4_pmu = {
  489. .name = "Netburst P4/Xeon",
  490. .handle_irq = p4_pmu_handle_irq,
  491. .disable_all = p4_pmu_disable_all,
  492. .enable_all = p4_pmu_enable_all,
  493. .enable = p4_pmu_enable_event,
  494. .disable = p4_pmu_disable_event,
  495. .eventsel = MSR_P4_BPU_CCCR0,
  496. .perfctr = MSR_P4_BPU_PERFCTR0,
  497. .event_map = p4_pmu_event_map,
  498. .raw_event = p4_pmu_raw_event,
  499. .max_events = ARRAY_SIZE(p4_event_map),
  500. .get_event_constraints = x86_get_event_constraints,
  501. /*
  502. * IF HT disabled we may need to use all
  503. * ARCH_P4_MAX_CCCR counters simulaneously
  504. * though leave it restricted at moment assuming
  505. * HT is on
  506. */
  507. .num_events = ARCH_P4_MAX_CCCR,
  508. .apic = 1,
  509. .event_bits = 40,
  510. .event_mask = (1ULL << 40) - 1,
  511. .max_period = (1ULL << 39) - 1,
  512. .hw_config = p4_hw_config,
  513. .schedule_events = p4_pmu_schedule_events,
  514. };
  515. static __init int p4_pmu_init(void)
  516. {
  517. unsigned int low, high;
  518. /* If we get stripped -- indexig fails */
  519. BUILD_BUG_ON(ARCH_P4_MAX_CCCR > X86_PMC_MAX_GENERIC);
  520. rdmsr(MSR_IA32_MISC_ENABLE, low, high);
  521. if (!(low & (1 << 7))) {
  522. pr_cont("unsupported Netburst CPU model %d ",
  523. boot_cpu_data.x86_model);
  524. return -ENODEV;
  525. }
  526. pr_cont("Netburst events, ");
  527. x86_pmu = p4_pmu;
  528. return 0;
  529. }
  530. #endif /* CONFIG_CPU_SUP_INTEL */