perf_event_intel_ds.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. #include <linux/bitops.h>
  2. #include <linux/types.h>
  3. #include <linux/slab.h>
  4. #include <asm/perf_event.h>
  5. #include <asm/insn.h>
  6. #include "perf_event.h"
  7. /* The size of a BTS record in bytes: */
  8. #define BTS_RECORD_SIZE 24
  9. #define BTS_BUFFER_SIZE (PAGE_SIZE << 4)
  10. #define PEBS_BUFFER_SIZE PAGE_SIZE
  11. /*
  12. * pebs_record_32 for p4 and core not supported
  13. struct pebs_record_32 {
  14. u32 flags, ip;
  15. u32 ax, bc, cx, dx;
  16. u32 si, di, bp, sp;
  17. };
  18. */
  19. union intel_x86_pebs_dse {
  20. u64 val;
  21. struct {
  22. unsigned int ld_dse:4;
  23. unsigned int ld_stlb_miss:1;
  24. unsigned int ld_locked:1;
  25. unsigned int ld_reserved:26;
  26. };
  27. struct {
  28. unsigned int st_l1d_hit:1;
  29. unsigned int st_reserved1:3;
  30. unsigned int st_stlb_miss:1;
  31. unsigned int st_locked:1;
  32. unsigned int st_reserved2:26;
  33. };
  34. };
  35. /*
  36. * Map PEBS Load Latency Data Source encodings to generic
  37. * memory data source information
  38. */
  39. #define P(a, b) PERF_MEM_S(a, b)
  40. #define OP_LH (P(OP, LOAD) | P(LVL, HIT))
  41. #define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS))
  42. static const u64 pebs_data_source[] = {
  43. P(OP, LOAD) | P(LVL, MISS) | P(LVL, L3) | P(SNOOP, NA),/* 0x00:ukn L3 */
  44. OP_LH | P(LVL, L1) | P(SNOOP, NONE), /* 0x01: L1 local */
  45. OP_LH | P(LVL, LFB) | P(SNOOP, NONE), /* 0x02: LFB hit */
  46. OP_LH | P(LVL, L2) | P(SNOOP, NONE), /* 0x03: L2 hit */
  47. OP_LH | P(LVL, L3) | P(SNOOP, NONE), /* 0x04: L3 hit */
  48. OP_LH | P(LVL, L3) | P(SNOOP, MISS), /* 0x05: L3 hit, snoop miss */
  49. OP_LH | P(LVL, L3) | P(SNOOP, HIT), /* 0x06: L3 hit, snoop hit */
  50. OP_LH | P(LVL, L3) | P(SNOOP, HITM), /* 0x07: L3 hit, snoop hitm */
  51. OP_LH | P(LVL, REM_CCE1) | P(SNOOP, HIT), /* 0x08: L3 miss snoop hit */
  52. OP_LH | P(LVL, REM_CCE1) | P(SNOOP, HITM), /* 0x09: L3 miss snoop hitm*/
  53. OP_LH | P(LVL, LOC_RAM) | P(SNOOP, HIT), /* 0x0a: L3 miss, shared */
  54. OP_LH | P(LVL, REM_RAM1) | P(SNOOP, HIT), /* 0x0b: L3 miss, shared */
  55. OP_LH | P(LVL, LOC_RAM) | SNOOP_NONE_MISS,/* 0x0c: L3 miss, excl */
  56. OP_LH | P(LVL, REM_RAM1) | SNOOP_NONE_MISS,/* 0x0d: L3 miss, excl */
  57. OP_LH | P(LVL, IO) | P(SNOOP, NONE), /* 0x0e: I/O */
  58. OP_LH | P(LVL, UNC) | P(SNOOP, NONE), /* 0x0f: uncached */
  59. };
  60. static u64 precise_store_data(u64 status)
  61. {
  62. union intel_x86_pebs_dse dse;
  63. u64 val = P(OP, STORE) | P(SNOOP, NA) | P(LVL, L1) | P(TLB, L2);
  64. dse.val = status;
  65. /*
  66. * bit 4: TLB access
  67. * 1 = stored missed 2nd level TLB
  68. *
  69. * so it either hit the walker or the OS
  70. * otherwise hit 2nd level TLB
  71. */
  72. if (dse.st_stlb_miss)
  73. val |= P(TLB, MISS);
  74. else
  75. val |= P(TLB, HIT);
  76. /*
  77. * bit 0: hit L1 data cache
  78. * if not set, then all we know is that
  79. * it missed L1D
  80. */
  81. if (dse.st_l1d_hit)
  82. val |= P(LVL, HIT);
  83. else
  84. val |= P(LVL, MISS);
  85. /*
  86. * bit 5: Locked prefix
  87. */
  88. if (dse.st_locked)
  89. val |= P(LOCK, LOCKED);
  90. return val;
  91. }
  92. static u64 load_latency_data(u64 status)
  93. {
  94. union intel_x86_pebs_dse dse;
  95. u64 val;
  96. int model = boot_cpu_data.x86_model;
  97. int fam = boot_cpu_data.x86;
  98. dse.val = status;
  99. /*
  100. * use the mapping table for bit 0-3
  101. */
  102. val = pebs_data_source[dse.ld_dse];
  103. /*
  104. * Nehalem models do not support TLB, Lock infos
  105. */
  106. if (fam == 0x6 && (model == 26 || model == 30
  107. || model == 31 || model == 46)) {
  108. val |= P(TLB, NA) | P(LOCK, NA);
  109. return val;
  110. }
  111. /*
  112. * bit 4: TLB access
  113. * 0 = did not miss 2nd level TLB
  114. * 1 = missed 2nd level TLB
  115. */
  116. if (dse.ld_stlb_miss)
  117. val |= P(TLB, MISS) | P(TLB, L2);
  118. else
  119. val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2);
  120. /*
  121. * bit 5: locked prefix
  122. */
  123. if (dse.ld_locked)
  124. val |= P(LOCK, LOCKED);
  125. return val;
  126. }
  127. struct pebs_record_core {
  128. u64 flags, ip;
  129. u64 ax, bx, cx, dx;
  130. u64 si, di, bp, sp;
  131. u64 r8, r9, r10, r11;
  132. u64 r12, r13, r14, r15;
  133. };
  134. struct pebs_record_nhm {
  135. u64 flags, ip;
  136. u64 ax, bx, cx, dx;
  137. u64 si, di, bp, sp;
  138. u64 r8, r9, r10, r11;
  139. u64 r12, r13, r14, r15;
  140. u64 status, dla, dse, lat;
  141. };
  142. void init_debug_store_on_cpu(int cpu)
  143. {
  144. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  145. if (!ds)
  146. return;
  147. wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA,
  148. (u32)((u64)(unsigned long)ds),
  149. (u32)((u64)(unsigned long)ds >> 32));
  150. }
  151. void fini_debug_store_on_cpu(int cpu)
  152. {
  153. if (!per_cpu(cpu_hw_events, cpu).ds)
  154. return;
  155. wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0);
  156. }
  157. static int alloc_pebs_buffer(int cpu)
  158. {
  159. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  160. int node = cpu_to_node(cpu);
  161. int max, thresh = 1; /* always use a single PEBS record */
  162. void *buffer;
  163. if (!x86_pmu.pebs)
  164. return 0;
  165. buffer = kmalloc_node(PEBS_BUFFER_SIZE, GFP_KERNEL | __GFP_ZERO, node);
  166. if (unlikely(!buffer))
  167. return -ENOMEM;
  168. max = PEBS_BUFFER_SIZE / x86_pmu.pebs_record_size;
  169. ds->pebs_buffer_base = (u64)(unsigned long)buffer;
  170. ds->pebs_index = ds->pebs_buffer_base;
  171. ds->pebs_absolute_maximum = ds->pebs_buffer_base +
  172. max * x86_pmu.pebs_record_size;
  173. ds->pebs_interrupt_threshold = ds->pebs_buffer_base +
  174. thresh * x86_pmu.pebs_record_size;
  175. return 0;
  176. }
  177. static void release_pebs_buffer(int cpu)
  178. {
  179. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  180. if (!ds || !x86_pmu.pebs)
  181. return;
  182. kfree((void *)(unsigned long)ds->pebs_buffer_base);
  183. ds->pebs_buffer_base = 0;
  184. }
  185. static int alloc_bts_buffer(int cpu)
  186. {
  187. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  188. int node = cpu_to_node(cpu);
  189. int max, thresh;
  190. void *buffer;
  191. if (!x86_pmu.bts)
  192. return 0;
  193. buffer = kmalloc_node(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_ZERO, node);
  194. if (unlikely(!buffer))
  195. return -ENOMEM;
  196. max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE;
  197. thresh = max / 16;
  198. ds->bts_buffer_base = (u64)(unsigned long)buffer;
  199. ds->bts_index = ds->bts_buffer_base;
  200. ds->bts_absolute_maximum = ds->bts_buffer_base +
  201. max * BTS_RECORD_SIZE;
  202. ds->bts_interrupt_threshold = ds->bts_absolute_maximum -
  203. thresh * BTS_RECORD_SIZE;
  204. return 0;
  205. }
  206. static void release_bts_buffer(int cpu)
  207. {
  208. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  209. if (!ds || !x86_pmu.bts)
  210. return;
  211. kfree((void *)(unsigned long)ds->bts_buffer_base);
  212. ds->bts_buffer_base = 0;
  213. }
  214. static int alloc_ds_buffer(int cpu)
  215. {
  216. int node = cpu_to_node(cpu);
  217. struct debug_store *ds;
  218. ds = kmalloc_node(sizeof(*ds), GFP_KERNEL | __GFP_ZERO, node);
  219. if (unlikely(!ds))
  220. return -ENOMEM;
  221. per_cpu(cpu_hw_events, cpu).ds = ds;
  222. return 0;
  223. }
  224. static void release_ds_buffer(int cpu)
  225. {
  226. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  227. if (!ds)
  228. return;
  229. per_cpu(cpu_hw_events, cpu).ds = NULL;
  230. kfree(ds);
  231. }
  232. void release_ds_buffers(void)
  233. {
  234. int cpu;
  235. if (!x86_pmu.bts && !x86_pmu.pebs)
  236. return;
  237. get_online_cpus();
  238. for_each_online_cpu(cpu)
  239. fini_debug_store_on_cpu(cpu);
  240. for_each_possible_cpu(cpu) {
  241. release_pebs_buffer(cpu);
  242. release_bts_buffer(cpu);
  243. release_ds_buffer(cpu);
  244. }
  245. put_online_cpus();
  246. }
  247. void reserve_ds_buffers(void)
  248. {
  249. int bts_err = 0, pebs_err = 0;
  250. int cpu;
  251. x86_pmu.bts_active = 0;
  252. x86_pmu.pebs_active = 0;
  253. if (!x86_pmu.bts && !x86_pmu.pebs)
  254. return;
  255. if (!x86_pmu.bts)
  256. bts_err = 1;
  257. if (!x86_pmu.pebs)
  258. pebs_err = 1;
  259. get_online_cpus();
  260. for_each_possible_cpu(cpu) {
  261. if (alloc_ds_buffer(cpu)) {
  262. bts_err = 1;
  263. pebs_err = 1;
  264. }
  265. if (!bts_err && alloc_bts_buffer(cpu))
  266. bts_err = 1;
  267. if (!pebs_err && alloc_pebs_buffer(cpu))
  268. pebs_err = 1;
  269. if (bts_err && pebs_err)
  270. break;
  271. }
  272. if (bts_err) {
  273. for_each_possible_cpu(cpu)
  274. release_bts_buffer(cpu);
  275. }
  276. if (pebs_err) {
  277. for_each_possible_cpu(cpu)
  278. release_pebs_buffer(cpu);
  279. }
  280. if (bts_err && pebs_err) {
  281. for_each_possible_cpu(cpu)
  282. release_ds_buffer(cpu);
  283. } else {
  284. if (x86_pmu.bts && !bts_err)
  285. x86_pmu.bts_active = 1;
  286. if (x86_pmu.pebs && !pebs_err)
  287. x86_pmu.pebs_active = 1;
  288. for_each_online_cpu(cpu)
  289. init_debug_store_on_cpu(cpu);
  290. }
  291. put_online_cpus();
  292. }
  293. /*
  294. * BTS
  295. */
  296. struct event_constraint bts_constraint =
  297. EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0);
  298. void intel_pmu_enable_bts(u64 config)
  299. {
  300. unsigned long debugctlmsr;
  301. debugctlmsr = get_debugctlmsr();
  302. debugctlmsr |= DEBUGCTLMSR_TR;
  303. debugctlmsr |= DEBUGCTLMSR_BTS;
  304. debugctlmsr |= DEBUGCTLMSR_BTINT;
  305. if (!(config & ARCH_PERFMON_EVENTSEL_OS))
  306. debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
  307. if (!(config & ARCH_PERFMON_EVENTSEL_USR))
  308. debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR;
  309. update_debugctlmsr(debugctlmsr);
  310. }
  311. void intel_pmu_disable_bts(void)
  312. {
  313. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  314. unsigned long debugctlmsr;
  315. if (!cpuc->ds)
  316. return;
  317. debugctlmsr = get_debugctlmsr();
  318. debugctlmsr &=
  319. ~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT |
  320. DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR);
  321. update_debugctlmsr(debugctlmsr);
  322. }
  323. int intel_pmu_drain_bts_buffer(void)
  324. {
  325. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  326. struct debug_store *ds = cpuc->ds;
  327. struct bts_record {
  328. u64 from;
  329. u64 to;
  330. u64 flags;
  331. };
  332. struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
  333. struct bts_record *at, *top;
  334. struct perf_output_handle handle;
  335. struct perf_event_header header;
  336. struct perf_sample_data data;
  337. struct pt_regs regs;
  338. if (!event)
  339. return 0;
  340. if (!x86_pmu.bts_active)
  341. return 0;
  342. at = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
  343. top = (struct bts_record *)(unsigned long)ds->bts_index;
  344. if (top <= at)
  345. return 0;
  346. memset(&regs, 0, sizeof(regs));
  347. ds->bts_index = ds->bts_buffer_base;
  348. perf_sample_data_init(&data, 0, event->hw.last_period);
  349. /*
  350. * Prepare a generic sample, i.e. fill in the invariant fields.
  351. * We will overwrite the from and to address before we output
  352. * the sample.
  353. */
  354. perf_prepare_sample(&header, &data, event, &regs);
  355. if (perf_output_begin(&handle, event, header.size * (top - at)))
  356. return 1;
  357. for (; at < top; at++) {
  358. data.ip = at->from;
  359. data.addr = at->to;
  360. perf_output_sample(&handle, &header, &data, event);
  361. }
  362. perf_output_end(&handle);
  363. /* There's new data available. */
  364. event->hw.interrupts++;
  365. event->pending_kill = POLL_IN;
  366. return 1;
  367. }
  368. /*
  369. * PEBS
  370. */
  371. struct event_constraint intel_core2_pebs_event_constraints[] = {
  372. INTEL_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
  373. INTEL_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
  374. INTEL_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
  375. INTEL_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
  376. INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
  377. EVENT_CONSTRAINT_END
  378. };
  379. struct event_constraint intel_atom_pebs_event_constraints[] = {
  380. INTEL_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
  381. INTEL_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
  382. INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
  383. EVENT_CONSTRAINT_END
  384. };
  385. struct event_constraint intel_nehalem_pebs_event_constraints[] = {
  386. INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
  387. INTEL_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
  388. INTEL_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
  389. INTEL_EVENT_CONSTRAINT(0xc0, 0xf), /* INST_RETIRED.ANY */
  390. INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
  391. INTEL_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  392. INTEL_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
  393. INTEL_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
  394. INTEL_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
  395. INTEL_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
  396. INTEL_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
  397. EVENT_CONSTRAINT_END
  398. };
  399. struct event_constraint intel_westmere_pebs_event_constraints[] = {
  400. INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
  401. INTEL_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
  402. INTEL_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
  403. INTEL_EVENT_CONSTRAINT(0xc0, 0xf), /* INSTR_RETIRED.* */
  404. INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
  405. INTEL_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  406. INTEL_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */
  407. INTEL_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
  408. INTEL_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
  409. INTEL_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
  410. INTEL_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
  411. EVENT_CONSTRAINT_END
  412. };
  413. struct event_constraint intel_snb_pebs_event_constraints[] = {
  414. INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
  415. INTEL_UEVENT_CONSTRAINT(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
  416. INTEL_UEVENT_CONSTRAINT(0x02c2, 0xf), /* UOPS_RETIRED.RETIRE_SLOTS */
  417. INTEL_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  418. INTEL_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */
  419. INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
  420. INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
  421. INTEL_EVENT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
  422. INTEL_EVENT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  423. INTEL_EVENT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
  424. INTEL_UEVENT_CONSTRAINT(0x02d4, 0xf), /* MEM_LOAD_UOPS_MISC_RETIRED.LLC_MISS */
  425. EVENT_CONSTRAINT_END
  426. };
  427. struct event_constraint intel_ivb_pebs_event_constraints[] = {
  428. INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
  429. INTEL_UEVENT_CONSTRAINT(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
  430. INTEL_UEVENT_CONSTRAINT(0x02c2, 0xf), /* UOPS_RETIRED.RETIRE_SLOTS */
  431. INTEL_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  432. INTEL_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */
  433. INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
  434. INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
  435. INTEL_EVENT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
  436. INTEL_EVENT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  437. INTEL_EVENT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
  438. INTEL_EVENT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
  439. EVENT_CONSTRAINT_END
  440. };
  441. struct event_constraint *intel_pebs_constraints(struct perf_event *event)
  442. {
  443. struct event_constraint *c;
  444. if (!event->attr.precise_ip)
  445. return NULL;
  446. if (x86_pmu.pebs_constraints) {
  447. for_each_event_constraint(c, x86_pmu.pebs_constraints) {
  448. if ((event->hw.config & c->cmask) == c->code) {
  449. event->hw.flags |= c->flags;
  450. return c;
  451. }
  452. }
  453. }
  454. return &emptyconstraint;
  455. }
  456. void intel_pmu_pebs_enable(struct perf_event *event)
  457. {
  458. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  459. struct hw_perf_event *hwc = &event->hw;
  460. hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;
  461. cpuc->pebs_enabled |= 1ULL << hwc->idx;
  462. if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
  463. cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32);
  464. else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
  465. cpuc->pebs_enabled |= 1ULL << 63;
  466. }
  467. void intel_pmu_pebs_disable(struct perf_event *event)
  468. {
  469. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  470. struct hw_perf_event *hwc = &event->hw;
  471. cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
  472. if (cpuc->enabled)
  473. wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
  474. hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
  475. }
  476. void intel_pmu_pebs_enable_all(void)
  477. {
  478. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  479. if (cpuc->pebs_enabled)
  480. wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
  481. }
  482. void intel_pmu_pebs_disable_all(void)
  483. {
  484. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  485. if (cpuc->pebs_enabled)
  486. wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
  487. }
  488. static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
  489. {
  490. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  491. unsigned long from = cpuc->lbr_entries[0].from;
  492. unsigned long old_to, to = cpuc->lbr_entries[0].to;
  493. unsigned long ip = regs->ip;
  494. int is_64bit = 0;
  495. /*
  496. * We don't need to fixup if the PEBS assist is fault like
  497. */
  498. if (!x86_pmu.intel_cap.pebs_trap)
  499. return 1;
  500. /*
  501. * No LBR entry, no basic block, no rewinding
  502. */
  503. if (!cpuc->lbr_stack.nr || !from || !to)
  504. return 0;
  505. /*
  506. * Basic blocks should never cross user/kernel boundaries
  507. */
  508. if (kernel_ip(ip) != kernel_ip(to))
  509. return 0;
  510. /*
  511. * unsigned math, either ip is before the start (impossible) or
  512. * the basic block is larger than 1 page (sanity)
  513. */
  514. if ((ip - to) > PAGE_SIZE)
  515. return 0;
  516. /*
  517. * We sampled a branch insn, rewind using the LBR stack
  518. */
  519. if (ip == to) {
  520. set_linear_ip(regs, from);
  521. return 1;
  522. }
  523. do {
  524. struct insn insn;
  525. u8 buf[MAX_INSN_SIZE];
  526. void *kaddr;
  527. old_to = to;
  528. if (!kernel_ip(ip)) {
  529. int bytes, size = MAX_INSN_SIZE;
  530. bytes = copy_from_user_nmi(buf, (void __user *)to, size);
  531. if (bytes != size)
  532. return 0;
  533. kaddr = buf;
  534. } else
  535. kaddr = (void *)to;
  536. #ifdef CONFIG_X86_64
  537. is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
  538. #endif
  539. insn_init(&insn, kaddr, is_64bit);
  540. insn_get_length(&insn);
  541. to += insn.length;
  542. } while (to < ip);
  543. if (to == ip) {
  544. set_linear_ip(regs, old_to);
  545. return 1;
  546. }
  547. /*
  548. * Even though we decoded the basic block, the instruction stream
  549. * never matched the given IP, either the TO or the IP got corrupted.
  550. */
  551. return 0;
  552. }
  553. static void __intel_pmu_pebs_event(struct perf_event *event,
  554. struct pt_regs *iregs, void *__pebs)
  555. {
  556. /*
  557. * We cast to pebs_record_nhm to get the load latency data
  558. * if extra_reg MSR_PEBS_LD_LAT_THRESHOLD used
  559. */
  560. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  561. struct pebs_record_nhm *pebs = __pebs;
  562. struct perf_sample_data data;
  563. struct pt_regs regs;
  564. u64 sample_type;
  565. int fll, fst;
  566. if (!intel_pmu_save_and_restart(event))
  567. return;
  568. fll = event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT;
  569. fst = event->hw.flags & PERF_X86_EVENT_PEBS_ST;
  570. perf_sample_data_init(&data, 0, event->hw.last_period);
  571. data.period = event->hw.last_period;
  572. sample_type = event->attr.sample_type;
  573. /*
  574. * if PEBS-LL or PreciseStore
  575. */
  576. if (fll || fst) {
  577. if (sample_type & PERF_SAMPLE_ADDR)
  578. data.addr = pebs->dla;
  579. /*
  580. * Use latency for weight (only avail with PEBS-LL)
  581. */
  582. if (fll && (sample_type & PERF_SAMPLE_WEIGHT))
  583. data.weight = pebs->lat;
  584. /*
  585. * data.data_src encodes the data source
  586. */
  587. if (sample_type & PERF_SAMPLE_DATA_SRC) {
  588. if (fll)
  589. data.data_src.val = load_latency_data(pebs->dse);
  590. else
  591. data.data_src.val = precise_store_data(pebs->dse);
  592. }
  593. }
  594. /*
  595. * We use the interrupt regs as a base because the PEBS record
  596. * does not contain a full regs set, specifically it seems to
  597. * lack segment descriptors, which get used by things like
  598. * user_mode().
  599. *
  600. * In the simple case fix up only the IP and BP,SP regs, for
  601. * PERF_SAMPLE_IP and PERF_SAMPLE_CALLCHAIN to function properly.
  602. * A possible PERF_SAMPLE_REGS will have to transfer all regs.
  603. */
  604. regs = *iregs;
  605. regs.flags = pebs->flags;
  606. set_linear_ip(&regs, pebs->ip);
  607. regs.bp = pebs->bp;
  608. regs.sp = pebs->sp;
  609. if (event->attr.precise_ip > 1 && intel_pmu_pebs_fixup_ip(&regs))
  610. regs.flags |= PERF_EFLAGS_EXACT;
  611. else
  612. regs.flags &= ~PERF_EFLAGS_EXACT;
  613. if (has_branch_stack(event))
  614. data.br_stack = &cpuc->lbr_stack;
  615. if (perf_event_overflow(event, &data, &regs))
  616. x86_pmu_stop(event, 0);
  617. }
  618. static void intel_pmu_drain_pebs_core(struct pt_regs *iregs)
  619. {
  620. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  621. struct debug_store *ds = cpuc->ds;
  622. struct perf_event *event = cpuc->events[0]; /* PMC0 only */
  623. struct pebs_record_core *at, *top;
  624. int n;
  625. if (!x86_pmu.pebs_active)
  626. return;
  627. at = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base;
  628. top = (struct pebs_record_core *)(unsigned long)ds->pebs_index;
  629. /*
  630. * Whatever else happens, drain the thing
  631. */
  632. ds->pebs_index = ds->pebs_buffer_base;
  633. if (!test_bit(0, cpuc->active_mask))
  634. return;
  635. WARN_ON_ONCE(!event);
  636. if (!event->attr.precise_ip)
  637. return;
  638. n = top - at;
  639. if (n <= 0)
  640. return;
  641. /*
  642. * Should not happen, we program the threshold at 1 and do not
  643. * set a reset value.
  644. */
  645. WARN_ONCE(n > 1, "bad leftover pebs %d\n", n);
  646. at += n - 1;
  647. __intel_pmu_pebs_event(event, iregs, at);
  648. }
  649. static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
  650. {
  651. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  652. struct debug_store *ds = cpuc->ds;
  653. struct pebs_record_nhm *at, *top;
  654. struct perf_event *event = NULL;
  655. u64 status = 0;
  656. int bit, n;
  657. if (!x86_pmu.pebs_active)
  658. return;
  659. at = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
  660. top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
  661. ds->pebs_index = ds->pebs_buffer_base;
  662. n = top - at;
  663. if (n <= 0)
  664. return;
  665. /*
  666. * Should not happen, we program the threshold at 1 and do not
  667. * set a reset value.
  668. */
  669. WARN_ONCE(n > x86_pmu.max_pebs_events, "Unexpected number of pebs records %d\n", n);
  670. for ( ; at < top; at++) {
  671. for_each_set_bit(bit, (unsigned long *)&at->status, x86_pmu.max_pebs_events) {
  672. event = cpuc->events[bit];
  673. if (!test_bit(bit, cpuc->active_mask))
  674. continue;
  675. WARN_ON_ONCE(!event);
  676. if (!event->attr.precise_ip)
  677. continue;
  678. if (__test_and_set_bit(bit, (unsigned long *)&status))
  679. continue;
  680. break;
  681. }
  682. if (!event || bit >= x86_pmu.max_pebs_events)
  683. continue;
  684. __intel_pmu_pebs_event(event, iregs, at);
  685. }
  686. }
  687. /*
  688. * BTS, PEBS probe and setup
  689. */
  690. void intel_ds_init(void)
  691. {
  692. /*
  693. * No support for 32bit formats
  694. */
  695. if (!boot_cpu_has(X86_FEATURE_DTES64))
  696. return;
  697. x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS);
  698. x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS);
  699. if (x86_pmu.pebs) {
  700. char pebs_type = x86_pmu.intel_cap.pebs_trap ? '+' : '-';
  701. int format = x86_pmu.intel_cap.pebs_format;
  702. switch (format) {
  703. case 0:
  704. printk(KERN_CONT "PEBS fmt0%c, ", pebs_type);
  705. x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
  706. x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
  707. break;
  708. case 1:
  709. printk(KERN_CONT "PEBS fmt1%c, ", pebs_type);
  710. x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
  711. x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
  712. break;
  713. default:
  714. printk(KERN_CONT "no PEBS fmt%d%c, ", format, pebs_type);
  715. x86_pmu.pebs = 0;
  716. }
  717. }
  718. }
  719. void perf_restore_debug_store(void)
  720. {
  721. struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
  722. if (!x86_pmu.bts && !x86_pmu.pebs)
  723. return;
  724. wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds);
  725. }