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 = 0, /* needs front-end tagging */
  58. .emask =
  59. P4_EVENT_ATTR(P4_INSTR_RETIRED, NBOGUSNTAG) |
  60. P4_EVENT_ATTR(P4_INSTR_RETIRED, NBOGUSTAG) |
  61. P4_EVENT_ATTR(P4_INSTR_RETIRED, BOGUSNTAG) |
  62. P4_EVENT_ATTR(P4_INSTR_RETIRED, BOGUSTAG),
  63. .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
  64. .cntr = { 12, 14 },
  65. },
  66. [3] = {
  67. .opcode = P4_BSQ_CACHE_REFERENCE,
  68. .config = 0,
  69. .dep = -1,
  70. .emask =
  71. P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_2ndL_HITS) |
  72. P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_2ndL_HITE) |
  73. P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_2ndL_HITM) |
  74. P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_3rdL_HITS) |
  75. P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_3rdL_HITE) |
  76. P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_3rdL_HITM),
  77. .escr_msr = { MSR_P4_BSU_ESCR0, MSR_P4_BSU_ESCR1 },
  78. .cntr = { 0, 2 },
  79. },
  80. [4] = {
  81. .opcode = P4_BSQ_CACHE_REFERENCE,
  82. .config = 0,
  83. .dep = -1,
  84. .emask =
  85. P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_2ndL_MISS) |
  86. P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_3rdL_MISS) |
  87. P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, WR_2ndL_MISS),
  88. .escr_msr = { MSR_P4_BSU_ESCR0, MSR_P4_BSU_ESCR1 },
  89. .cntr = { 0, 3 },
  90. },
  91. [5] = {
  92. .opcode = P4_RETIRED_BRANCH_TYPE,
  93. .config = 0,
  94. .dep = -1,
  95. .emask =
  96. P4_EVENT_ATTR(P4_RETIRED_BRANCH_TYPE, CONDITIONAL) |
  97. P4_EVENT_ATTR(P4_RETIRED_BRANCH_TYPE, CALL) |
  98. P4_EVENT_ATTR(P4_RETIRED_BRANCH_TYPE, RETURN) |
  99. P4_EVENT_ATTR(P4_RETIRED_BRANCH_TYPE, INDIRECT),
  100. .escr_msr = { MSR_P4_TBPU_ESCR0, MSR_P4_TBPU_ESCR1 },
  101. .cntr = { 4, 6 },
  102. },
  103. [6] = {
  104. .opcode = P4_MISPRED_BRANCH_RETIRED,
  105. .config = 0,
  106. .dep = -1,
  107. .emask =
  108. P4_EVENT_ATTR(P4_MISPRED_BRANCH_RETIRED, NBOGUS),
  109. .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
  110. .cntr = { 12, 14 },
  111. },
  112. [7] = {
  113. .opcode = P4_FSB_DATA_ACTIVITY,
  114. .config = p4_config_pack_cccr(P4_CCCR_EDGE | P4_CCCR_COMPARE),
  115. .dep = -1,
  116. .emask =
  117. P4_EVENT_ATTR(P4_FSB_DATA_ACTIVITY, DRDY_DRV) |
  118. P4_EVENT_ATTR(P4_FSB_DATA_ACTIVITY, DRDY_OWN),
  119. .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
  120. .cntr = { 0, 2 },
  121. },
  122. };
  123. static struct p4_event_template *p4_event_map[PERF_COUNT_HW_MAX] = {
  124. /* non-halted CPU clocks */
  125. [PERF_COUNT_HW_CPU_CYCLES] = &p4_templates[1],
  126. /* retired instructions: dep on tagging the FSB */
  127. [PERF_COUNT_HW_INSTRUCTIONS] = &p4_templates[2],
  128. /* cache hits */
  129. [PERF_COUNT_HW_CACHE_REFERENCES] = &p4_templates[3],
  130. /* cache misses */
  131. [PERF_COUNT_HW_CACHE_MISSES] = &p4_templates[4],
  132. /* branch instructions retired */
  133. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = &p4_templates[5],
  134. /* mispredicted branches retired */
  135. [PERF_COUNT_HW_BRANCH_MISSES] = &p4_templates[6],
  136. /* bus ready clocks (cpu is driving #DRDY_DRV\#DRDY_OWN): */
  137. [PERF_COUNT_HW_BUS_CYCLES] = &p4_templates[7],
  138. };
  139. static u64 p4_pmu_event_map(int hw_event)
  140. {
  141. struct p4_event_template *tpl;
  142. u64 config;
  143. if (hw_event > ARRAY_SIZE(p4_event_map)) {
  144. printk_once(KERN_ERR "PMU: Incorrect event index\n");
  145. return 0;
  146. }
  147. tpl = p4_event_map[hw_event];
  148. /*
  149. * fill config up according to
  150. * a predefined event template
  151. */
  152. config = tpl->config;
  153. config |= p4_config_pack_escr(P4_EVENT_UNPACK_EVENT(tpl->opcode) << P4_EVNTSEL_EVENT_SHIFT);
  154. config |= p4_config_pack_escr(tpl->emask << P4_EVNTSEL_EVENTMASK_SHIFT);
  155. config |= p4_config_pack_cccr(P4_EVENT_UNPACK_SELECTOR(tpl->opcode) << P4_CCCR_ESCR_SELECT_SHIFT);
  156. /* on HT machine we need a special bit */
  157. if (p4_ht_active() && p4_ht_thread(raw_smp_processor_id()))
  158. config = p4_set_ht_bit(config);
  159. return config;
  160. }
  161. /*
  162. * Note that we still have 5 events (from global events SDM list)
  163. * intersected in opcode+emask bits so we will need another
  164. * scheme there do distinguish templates.
  165. */
  166. static inline int p4_pmu_emask_match(unsigned int dst, unsigned int src)
  167. {
  168. return dst & src;
  169. }
  170. static struct p4_event_template *p4_pmu_template_lookup(u64 config)
  171. {
  172. u32 opcode = p4_config_unpack_opcode(config);
  173. unsigned int emask = p4_config_unpack_emask(config);
  174. unsigned int i;
  175. for (i = 0; i < ARRAY_SIZE(p4_templates); i++) {
  176. if (opcode == p4_templates[i].opcode &&
  177. p4_pmu_emask_match(emask, p4_templates[i].emask))
  178. return &p4_templates[i];
  179. }
  180. return NULL;
  181. }
  182. /*
  183. * We don't control raw events so it's up to the caller
  184. * to pass sane values (and we don't count the thread number
  185. * on HT machine but allow HT-compatible specifics to be
  186. * passed on)
  187. */
  188. static u64 p4_pmu_raw_event(u64 hw_event)
  189. {
  190. return hw_event &
  191. (p4_config_pack_escr(P4_EVNTSEL_MASK_HT) |
  192. p4_config_pack_cccr(P4_CCCR_MASK_HT));
  193. }
  194. static int p4_hw_config(struct perf_event_attr *attr, struct hw_perf_event *hwc)
  195. {
  196. int cpu = raw_smp_processor_id();
  197. /*
  198. * the reason we use cpu that early is that: if we get scheduled
  199. * first time on the same cpu -- we will not need swap thread
  200. * specific flags in config (and will save some cpu cycles)
  201. */
  202. /* CCCR by default */
  203. hwc->config = p4_config_pack_cccr(p4_default_cccr_conf(cpu));
  204. /* Count user and OS events unless not requested to */
  205. hwc->config |= p4_config_pack_escr(p4_default_escr_conf(cpu, attr->exclude_kernel,
  206. attr->exclude_user));
  207. return 0;
  208. }
  209. static inline void p4_pmu_clear_cccr_ovf(struct hw_perf_event *hwc)
  210. {
  211. unsigned long dummy;
  212. rdmsrl(hwc->config_base + hwc->idx, dummy);
  213. if (dummy & P4_CCCR_OVF) {
  214. (void)checking_wrmsrl(hwc->config_base + hwc->idx,
  215. ((u64)dummy) & ~P4_CCCR_OVF);
  216. }
  217. }
  218. static inline void p4_pmu_disable_event(struct perf_event *event)
  219. {
  220. struct hw_perf_event *hwc = &event->hw;
  221. /*
  222. * If event gets disabled while counter is in overflowed
  223. * state we need to clear P4_CCCR_OVF, otherwise interrupt get
  224. * asserted again and again
  225. */
  226. (void)checking_wrmsrl(hwc->config_base + hwc->idx,
  227. (u64)(p4_config_unpack_cccr(hwc->config)) &
  228. ~P4_CCCR_ENABLE & ~P4_CCCR_OVF);
  229. }
  230. static void p4_pmu_disable_all(void)
  231. {
  232. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  233. int idx;
  234. for (idx = 0; idx < x86_pmu.num_events; idx++) {
  235. struct perf_event *event = cpuc->events[idx];
  236. if (!test_bit(idx, cpuc->active_mask))
  237. continue;
  238. p4_pmu_disable_event(event);
  239. }
  240. }
  241. static void p4_pmu_enable_event(struct perf_event *event)
  242. {
  243. struct hw_perf_event *hwc = &event->hw;
  244. int thread = p4_ht_config_thread(hwc->config);
  245. u64 escr_conf = p4_config_unpack_escr(p4_clear_ht_bit(hwc->config));
  246. u64 escr_base;
  247. struct p4_event_template *tpl;
  248. struct p4_pmu_res *c;
  249. /*
  250. * some preparation work from per-cpu private fields
  251. * since we need to find out which ESCR to use
  252. */
  253. c = &__get_cpu_var(p4_pmu_config);
  254. tpl = c->tpl[hwc->idx];
  255. if (!tpl) {
  256. pr_crit("%s: Wrong index: %d\n", __func__, hwc->idx);
  257. return;
  258. }
  259. escr_base = (u64)tpl->escr_msr[thread];
  260. /*
  261. * - we dont support cascaded counters yet
  262. * - and counter 1 is broken (erratum)
  263. */
  264. WARN_ON_ONCE(p4_is_event_cascaded(hwc->config));
  265. WARN_ON_ONCE(hwc->idx == 1);
  266. (void)checking_wrmsrl(escr_base, escr_conf);
  267. (void)checking_wrmsrl(hwc->config_base + hwc->idx,
  268. (u64)(p4_config_unpack_cccr(hwc->config)) | P4_CCCR_ENABLE);
  269. }
  270. static void p4_pmu_enable_all(void)
  271. {
  272. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  273. int idx;
  274. for (idx = 0; idx < x86_pmu.num_events; idx++) {
  275. struct perf_event *event = cpuc->events[idx];
  276. if (!test_bit(idx, cpuc->active_mask))
  277. continue;
  278. p4_pmu_enable_event(event);
  279. }
  280. }
  281. static int p4_pmu_handle_irq(struct pt_regs *regs)
  282. {
  283. struct perf_sample_data data;
  284. struct cpu_hw_events *cpuc;
  285. struct perf_event *event;
  286. struct hw_perf_event *hwc;
  287. int idx, handled = 0;
  288. u64 val;
  289. data.addr = 0;
  290. data.raw = NULL;
  291. cpuc = &__get_cpu_var(cpu_hw_events);
  292. for (idx = 0; idx < x86_pmu.num_events; idx++) {
  293. if (!test_bit(idx, cpuc->active_mask))
  294. continue;
  295. event = cpuc->events[idx];
  296. hwc = &event->hw;
  297. WARN_ON_ONCE(hwc->idx != idx);
  298. /*
  299. * FIXME: Redundant call, actually not needed
  300. * but just to check if we're screwed
  301. */
  302. p4_pmu_clear_cccr_ovf(hwc);
  303. val = x86_perf_event_update(event);
  304. if (val & (1ULL << (x86_pmu.event_bits - 1)))
  305. continue;
  306. /*
  307. * event overflow
  308. */
  309. handled = 1;
  310. data.period = event->hw.last_period;
  311. if (!x86_perf_event_set_period(event))
  312. continue;
  313. if (perf_event_overflow(event, 1, &data, regs))
  314. p4_pmu_disable_event(event);
  315. }
  316. if (handled) {
  317. /* p4 quirk: unmask it again */
  318. apic_write(APIC_LVTPC, apic_read(APIC_LVTPC) & ~APIC_LVT_MASKED);
  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 */