perf_event_intel_ds.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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. #define PEBS_FIXUP_SIZE PAGE_SIZE
  12. /*
  13. * pebs_record_32 for p4 and core not supported
  14. struct pebs_record_32 {
  15. u32 flags, ip;
  16. u32 ax, bc, cx, dx;
  17. u32 si, di, bp, sp;
  18. };
  19. */
  20. union intel_x86_pebs_dse {
  21. u64 val;
  22. struct {
  23. unsigned int ld_dse:4;
  24. unsigned int ld_stlb_miss:1;
  25. unsigned int ld_locked:1;
  26. unsigned int ld_reserved:26;
  27. };
  28. struct {
  29. unsigned int st_l1d_hit:1;
  30. unsigned int st_reserved1:3;
  31. unsigned int st_stlb_miss:1;
  32. unsigned int st_locked:1;
  33. unsigned int st_reserved2:26;
  34. };
  35. };
  36. /*
  37. * Map PEBS Load Latency Data Source encodings to generic
  38. * memory data source information
  39. */
  40. #define P(a, b) PERF_MEM_S(a, b)
  41. #define OP_LH (P(OP, LOAD) | P(LVL, HIT))
  42. #define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS))
  43. static const u64 pebs_data_source[] = {
  44. P(OP, LOAD) | P(LVL, MISS) | P(LVL, L3) | P(SNOOP, NA),/* 0x00:ukn L3 */
  45. OP_LH | P(LVL, L1) | P(SNOOP, NONE), /* 0x01: L1 local */
  46. OP_LH | P(LVL, LFB) | P(SNOOP, NONE), /* 0x02: LFB hit */
  47. OP_LH | P(LVL, L2) | P(SNOOP, NONE), /* 0x03: L2 hit */
  48. OP_LH | P(LVL, L3) | P(SNOOP, NONE), /* 0x04: L3 hit */
  49. OP_LH | P(LVL, L3) | P(SNOOP, MISS), /* 0x05: L3 hit, snoop miss */
  50. OP_LH | P(LVL, L3) | P(SNOOP, HIT), /* 0x06: L3 hit, snoop hit */
  51. OP_LH | P(LVL, L3) | P(SNOOP, HITM), /* 0x07: L3 hit, snoop hitm */
  52. OP_LH | P(LVL, REM_CCE1) | P(SNOOP, HIT), /* 0x08: L3 miss snoop hit */
  53. OP_LH | P(LVL, REM_CCE1) | P(SNOOP, HITM), /* 0x09: L3 miss snoop hitm*/
  54. OP_LH | P(LVL, LOC_RAM) | P(SNOOP, HIT), /* 0x0a: L3 miss, shared */
  55. OP_LH | P(LVL, REM_RAM1) | P(SNOOP, HIT), /* 0x0b: L3 miss, shared */
  56. OP_LH | P(LVL, LOC_RAM) | SNOOP_NONE_MISS,/* 0x0c: L3 miss, excl */
  57. OP_LH | P(LVL, REM_RAM1) | SNOOP_NONE_MISS,/* 0x0d: L3 miss, excl */
  58. OP_LH | P(LVL, IO) | P(SNOOP, NONE), /* 0x0e: I/O */
  59. OP_LH | P(LVL, UNC) | P(SNOOP, NONE), /* 0x0f: uncached */
  60. };
  61. static u64 precise_store_data(u64 status)
  62. {
  63. union intel_x86_pebs_dse dse;
  64. u64 val = P(OP, STORE) | P(SNOOP, NA) | P(LVL, L1) | P(TLB, L2);
  65. dse.val = status;
  66. /*
  67. * bit 4: TLB access
  68. * 1 = stored missed 2nd level TLB
  69. *
  70. * so it either hit the walker or the OS
  71. * otherwise hit 2nd level TLB
  72. */
  73. if (dse.st_stlb_miss)
  74. val |= P(TLB, MISS);
  75. else
  76. val |= P(TLB, HIT);
  77. /*
  78. * bit 0: hit L1 data cache
  79. * if not set, then all we know is that
  80. * it missed L1D
  81. */
  82. if (dse.st_l1d_hit)
  83. val |= P(LVL, HIT);
  84. else
  85. val |= P(LVL, MISS);
  86. /*
  87. * bit 5: Locked prefix
  88. */
  89. if (dse.st_locked)
  90. val |= P(LOCK, LOCKED);
  91. return val;
  92. }
  93. static u64 precise_datala_hsw(struct perf_event *event, u64 status)
  94. {
  95. union perf_mem_data_src dse;
  96. dse.val = PERF_MEM_NA;
  97. if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW)
  98. dse.mem_op = PERF_MEM_OP_STORE;
  99. else if (event->hw.flags & PERF_X86_EVENT_PEBS_LD_HSW)
  100. dse.mem_op = PERF_MEM_OP_LOAD;
  101. /*
  102. * L1 info only valid for following events:
  103. *
  104. * MEM_UOPS_RETIRED.STLB_MISS_STORES
  105. * MEM_UOPS_RETIRED.LOCK_STORES
  106. * MEM_UOPS_RETIRED.SPLIT_STORES
  107. * MEM_UOPS_RETIRED.ALL_STORES
  108. */
  109. if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) {
  110. if (status & 1)
  111. dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_HIT;
  112. else
  113. dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_MISS;
  114. }
  115. return dse.val;
  116. }
  117. static u64 load_latency_data(u64 status)
  118. {
  119. union intel_x86_pebs_dse dse;
  120. u64 val;
  121. int model = boot_cpu_data.x86_model;
  122. int fam = boot_cpu_data.x86;
  123. dse.val = status;
  124. /*
  125. * use the mapping table for bit 0-3
  126. */
  127. val = pebs_data_source[dse.ld_dse];
  128. /*
  129. * Nehalem models do not support TLB, Lock infos
  130. */
  131. if (fam == 0x6 && (model == 26 || model == 30
  132. || model == 31 || model == 46)) {
  133. val |= P(TLB, NA) | P(LOCK, NA);
  134. return val;
  135. }
  136. /*
  137. * bit 4: TLB access
  138. * 0 = did not miss 2nd level TLB
  139. * 1 = missed 2nd level TLB
  140. */
  141. if (dse.ld_stlb_miss)
  142. val |= P(TLB, MISS) | P(TLB, L2);
  143. else
  144. val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2);
  145. /*
  146. * bit 5: locked prefix
  147. */
  148. if (dse.ld_locked)
  149. val |= P(LOCK, LOCKED);
  150. return val;
  151. }
  152. struct pebs_record_core {
  153. u64 flags, ip;
  154. u64 ax, bx, cx, dx;
  155. u64 si, di, bp, sp;
  156. u64 r8, r9, r10, r11;
  157. u64 r12, r13, r14, r15;
  158. };
  159. struct pebs_record_nhm {
  160. u64 flags, ip;
  161. u64 ax, bx, cx, dx;
  162. u64 si, di, bp, sp;
  163. u64 r8, r9, r10, r11;
  164. u64 r12, r13, r14, r15;
  165. u64 status, dla, dse, lat;
  166. };
  167. /*
  168. * Same as pebs_record_nhm, with two additional fields.
  169. */
  170. struct pebs_record_hsw {
  171. u64 flags, ip;
  172. u64 ax, bx, cx, dx;
  173. u64 si, di, bp, sp;
  174. u64 r8, r9, r10, r11;
  175. u64 r12, r13, r14, r15;
  176. u64 status, dla, dse, lat;
  177. u64 real_ip, tsx_tuning;
  178. };
  179. union hsw_tsx_tuning {
  180. struct {
  181. u32 cycles_last_block : 32,
  182. hle_abort : 1,
  183. rtm_abort : 1,
  184. instruction_abort : 1,
  185. non_instruction_abort : 1,
  186. retry : 1,
  187. data_conflict : 1,
  188. capacity_writes : 1,
  189. capacity_reads : 1;
  190. };
  191. u64 value;
  192. };
  193. #define PEBS_HSW_TSX_FLAGS 0xff00000000ULL
  194. void init_debug_store_on_cpu(int cpu)
  195. {
  196. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  197. if (!ds)
  198. return;
  199. wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA,
  200. (u32)((u64)(unsigned long)ds),
  201. (u32)((u64)(unsigned long)ds >> 32));
  202. }
  203. void fini_debug_store_on_cpu(int cpu)
  204. {
  205. if (!per_cpu(cpu_hw_events, cpu).ds)
  206. return;
  207. wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0);
  208. }
  209. static DEFINE_PER_CPU(void *, insn_buffer);
  210. static int alloc_pebs_buffer(int cpu)
  211. {
  212. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  213. int node = cpu_to_node(cpu);
  214. int max, thresh = 1; /* always use a single PEBS record */
  215. void *buffer, *ibuffer;
  216. if (!x86_pmu.pebs)
  217. return 0;
  218. buffer = kzalloc_node(PEBS_BUFFER_SIZE, GFP_KERNEL, node);
  219. if (unlikely(!buffer))
  220. return -ENOMEM;
  221. /*
  222. * HSW+ already provides us the eventing ip; no need to allocate this
  223. * buffer then.
  224. */
  225. if (x86_pmu.intel_cap.pebs_format < 2) {
  226. ibuffer = kzalloc_node(PEBS_FIXUP_SIZE, GFP_KERNEL, node);
  227. if (!ibuffer) {
  228. kfree(buffer);
  229. return -ENOMEM;
  230. }
  231. per_cpu(insn_buffer, cpu) = ibuffer;
  232. }
  233. max = PEBS_BUFFER_SIZE / x86_pmu.pebs_record_size;
  234. ds->pebs_buffer_base = (u64)(unsigned long)buffer;
  235. ds->pebs_index = ds->pebs_buffer_base;
  236. ds->pebs_absolute_maximum = ds->pebs_buffer_base +
  237. max * x86_pmu.pebs_record_size;
  238. ds->pebs_interrupt_threshold = ds->pebs_buffer_base +
  239. thresh * x86_pmu.pebs_record_size;
  240. return 0;
  241. }
  242. static void release_pebs_buffer(int cpu)
  243. {
  244. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  245. if (!ds || !x86_pmu.pebs)
  246. return;
  247. kfree(per_cpu(insn_buffer, cpu));
  248. per_cpu(insn_buffer, cpu) = NULL;
  249. kfree((void *)(unsigned long)ds->pebs_buffer_base);
  250. ds->pebs_buffer_base = 0;
  251. }
  252. static int alloc_bts_buffer(int cpu)
  253. {
  254. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  255. int node = cpu_to_node(cpu);
  256. int max, thresh;
  257. void *buffer;
  258. if (!x86_pmu.bts)
  259. return 0;
  260. buffer = kzalloc_node(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, node);
  261. if (unlikely(!buffer)) {
  262. WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__);
  263. return -ENOMEM;
  264. }
  265. max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE;
  266. thresh = max / 16;
  267. ds->bts_buffer_base = (u64)(unsigned long)buffer;
  268. ds->bts_index = ds->bts_buffer_base;
  269. ds->bts_absolute_maximum = ds->bts_buffer_base +
  270. max * BTS_RECORD_SIZE;
  271. ds->bts_interrupt_threshold = ds->bts_absolute_maximum -
  272. thresh * BTS_RECORD_SIZE;
  273. return 0;
  274. }
  275. static void release_bts_buffer(int cpu)
  276. {
  277. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  278. if (!ds || !x86_pmu.bts)
  279. return;
  280. kfree((void *)(unsigned long)ds->bts_buffer_base);
  281. ds->bts_buffer_base = 0;
  282. }
  283. static int alloc_ds_buffer(int cpu)
  284. {
  285. int node = cpu_to_node(cpu);
  286. struct debug_store *ds;
  287. ds = kzalloc_node(sizeof(*ds), GFP_KERNEL, node);
  288. if (unlikely(!ds))
  289. return -ENOMEM;
  290. per_cpu(cpu_hw_events, cpu).ds = ds;
  291. return 0;
  292. }
  293. static void release_ds_buffer(int cpu)
  294. {
  295. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  296. if (!ds)
  297. return;
  298. per_cpu(cpu_hw_events, cpu).ds = NULL;
  299. kfree(ds);
  300. }
  301. void release_ds_buffers(void)
  302. {
  303. int cpu;
  304. if (!x86_pmu.bts && !x86_pmu.pebs)
  305. return;
  306. get_online_cpus();
  307. for_each_online_cpu(cpu)
  308. fini_debug_store_on_cpu(cpu);
  309. for_each_possible_cpu(cpu) {
  310. release_pebs_buffer(cpu);
  311. release_bts_buffer(cpu);
  312. release_ds_buffer(cpu);
  313. }
  314. put_online_cpus();
  315. }
  316. void reserve_ds_buffers(void)
  317. {
  318. int bts_err = 0, pebs_err = 0;
  319. int cpu;
  320. x86_pmu.bts_active = 0;
  321. x86_pmu.pebs_active = 0;
  322. if (!x86_pmu.bts && !x86_pmu.pebs)
  323. return;
  324. if (!x86_pmu.bts)
  325. bts_err = 1;
  326. if (!x86_pmu.pebs)
  327. pebs_err = 1;
  328. get_online_cpus();
  329. for_each_possible_cpu(cpu) {
  330. if (alloc_ds_buffer(cpu)) {
  331. bts_err = 1;
  332. pebs_err = 1;
  333. }
  334. if (!bts_err && alloc_bts_buffer(cpu))
  335. bts_err = 1;
  336. if (!pebs_err && alloc_pebs_buffer(cpu))
  337. pebs_err = 1;
  338. if (bts_err && pebs_err)
  339. break;
  340. }
  341. if (bts_err) {
  342. for_each_possible_cpu(cpu)
  343. release_bts_buffer(cpu);
  344. }
  345. if (pebs_err) {
  346. for_each_possible_cpu(cpu)
  347. release_pebs_buffer(cpu);
  348. }
  349. if (bts_err && pebs_err) {
  350. for_each_possible_cpu(cpu)
  351. release_ds_buffer(cpu);
  352. } else {
  353. if (x86_pmu.bts && !bts_err)
  354. x86_pmu.bts_active = 1;
  355. if (x86_pmu.pebs && !pebs_err)
  356. x86_pmu.pebs_active = 1;
  357. for_each_online_cpu(cpu)
  358. init_debug_store_on_cpu(cpu);
  359. }
  360. put_online_cpus();
  361. }
  362. /*
  363. * BTS
  364. */
  365. struct event_constraint bts_constraint =
  366. EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0);
  367. void intel_pmu_enable_bts(u64 config)
  368. {
  369. unsigned long debugctlmsr;
  370. debugctlmsr = get_debugctlmsr();
  371. debugctlmsr |= DEBUGCTLMSR_TR;
  372. debugctlmsr |= DEBUGCTLMSR_BTS;
  373. debugctlmsr |= DEBUGCTLMSR_BTINT;
  374. if (!(config & ARCH_PERFMON_EVENTSEL_OS))
  375. debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
  376. if (!(config & ARCH_PERFMON_EVENTSEL_USR))
  377. debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR;
  378. update_debugctlmsr(debugctlmsr);
  379. }
  380. void intel_pmu_disable_bts(void)
  381. {
  382. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  383. unsigned long debugctlmsr;
  384. if (!cpuc->ds)
  385. return;
  386. debugctlmsr = get_debugctlmsr();
  387. debugctlmsr &=
  388. ~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT |
  389. DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR);
  390. update_debugctlmsr(debugctlmsr);
  391. }
  392. int intel_pmu_drain_bts_buffer(void)
  393. {
  394. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  395. struct debug_store *ds = cpuc->ds;
  396. struct bts_record {
  397. u64 from;
  398. u64 to;
  399. u64 flags;
  400. };
  401. struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
  402. struct bts_record *at, *top;
  403. struct perf_output_handle handle;
  404. struct perf_event_header header;
  405. struct perf_sample_data data;
  406. struct pt_regs regs;
  407. if (!event)
  408. return 0;
  409. if (!x86_pmu.bts_active)
  410. return 0;
  411. at = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
  412. top = (struct bts_record *)(unsigned long)ds->bts_index;
  413. if (top <= at)
  414. return 0;
  415. memset(&regs, 0, sizeof(regs));
  416. ds->bts_index = ds->bts_buffer_base;
  417. perf_sample_data_init(&data, 0, event->hw.last_period);
  418. /*
  419. * Prepare a generic sample, i.e. fill in the invariant fields.
  420. * We will overwrite the from and to address before we output
  421. * the sample.
  422. */
  423. perf_prepare_sample(&header, &data, event, &regs);
  424. if (perf_output_begin(&handle, event, header.size * (top - at)))
  425. return 1;
  426. for (; at < top; at++) {
  427. data.ip = at->from;
  428. data.addr = at->to;
  429. perf_output_sample(&handle, &header, &data, event);
  430. }
  431. perf_output_end(&handle);
  432. /* There's new data available. */
  433. event->hw.interrupts++;
  434. event->pending_kill = POLL_IN;
  435. return 1;
  436. }
  437. /*
  438. * PEBS
  439. */
  440. struct event_constraint intel_core2_pebs_event_constraints[] = {
  441. INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
  442. INTEL_FLAGS_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
  443. INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
  444. INTEL_FLAGS_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
  445. INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
  446. EVENT_CONSTRAINT_END
  447. };
  448. struct event_constraint intel_atom_pebs_event_constraints[] = {
  449. INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
  450. INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
  451. INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
  452. EVENT_CONSTRAINT_END
  453. };
  454. struct event_constraint intel_slm_pebs_event_constraints[] = {
  455. /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
  456. INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x1),
  457. /* Allow all events as PEBS with no flags */
  458. INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
  459. EVENT_CONSTRAINT_END
  460. };
  461. struct event_constraint intel_nehalem_pebs_event_constraints[] = {
  462. INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
  463. INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
  464. INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
  465. INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INST_RETIRED.ANY */
  466. INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
  467. INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  468. INTEL_FLAGS_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
  469. INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
  470. INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
  471. INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
  472. INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
  473. EVENT_CONSTRAINT_END
  474. };
  475. struct event_constraint intel_westmere_pebs_event_constraints[] = {
  476. INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
  477. INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
  478. INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
  479. INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INSTR_RETIRED.* */
  480. INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
  481. INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  482. INTEL_FLAGS_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */
  483. INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
  484. INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
  485. INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
  486. INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
  487. EVENT_CONSTRAINT_END
  488. };
  489. struct event_constraint intel_snb_pebs_event_constraints[] = {
  490. INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
  491. INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
  492. INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
  493. /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
  494. INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
  495. /* Allow all events as PEBS with no flags */
  496. INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
  497. EVENT_CONSTRAINT_END
  498. };
  499. struct event_constraint intel_ivb_pebs_event_constraints[] = {
  500. INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
  501. INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
  502. INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
  503. /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
  504. INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
  505. /* Allow all events as PEBS with no flags */
  506. INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
  507. EVENT_CONSTRAINT_END
  508. };
  509. struct event_constraint intel_hsw_pebs_event_constraints[] = {
  510. INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
  511. INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */
  512. /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
  513. INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
  514. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
  515. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
  516. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
  517. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
  518. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
  519. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
  520. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
  521. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
  522. INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  523. INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
  524. INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
  525. /* Allow all events as PEBS with no flags */
  526. INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
  527. EVENT_CONSTRAINT_END
  528. };
  529. struct event_constraint *intel_pebs_constraints(struct perf_event *event)
  530. {
  531. struct event_constraint *c;
  532. if (!event->attr.precise_ip)
  533. return NULL;
  534. if (x86_pmu.pebs_constraints) {
  535. for_each_event_constraint(c, x86_pmu.pebs_constraints) {
  536. if ((event->hw.config & c->cmask) == c->code) {
  537. event->hw.flags |= c->flags;
  538. return c;
  539. }
  540. }
  541. }
  542. return &emptyconstraint;
  543. }
  544. void intel_pmu_pebs_enable(struct perf_event *event)
  545. {
  546. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  547. struct hw_perf_event *hwc = &event->hw;
  548. hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;
  549. cpuc->pebs_enabled |= 1ULL << hwc->idx;
  550. if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
  551. cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32);
  552. else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
  553. cpuc->pebs_enabled |= 1ULL << 63;
  554. }
  555. void intel_pmu_pebs_disable(struct perf_event *event)
  556. {
  557. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  558. struct hw_perf_event *hwc = &event->hw;
  559. cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
  560. if (event->hw.constraint->flags & PERF_X86_EVENT_PEBS_LDLAT)
  561. cpuc->pebs_enabled &= ~(1ULL << (hwc->idx + 32));
  562. else if (event->hw.constraint->flags & PERF_X86_EVENT_PEBS_ST)
  563. cpuc->pebs_enabled &= ~(1ULL << 63);
  564. if (cpuc->enabled)
  565. wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
  566. hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
  567. }
  568. void intel_pmu_pebs_enable_all(void)
  569. {
  570. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  571. if (cpuc->pebs_enabled)
  572. wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
  573. }
  574. void intel_pmu_pebs_disable_all(void)
  575. {
  576. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  577. if (cpuc->pebs_enabled)
  578. wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
  579. }
  580. static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
  581. {
  582. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  583. unsigned long from = cpuc->lbr_entries[0].from;
  584. unsigned long old_to, to = cpuc->lbr_entries[0].to;
  585. unsigned long ip = regs->ip;
  586. int is_64bit = 0;
  587. void *kaddr;
  588. int size;
  589. /*
  590. * We don't need to fixup if the PEBS assist is fault like
  591. */
  592. if (!x86_pmu.intel_cap.pebs_trap)
  593. return 1;
  594. /*
  595. * No LBR entry, no basic block, no rewinding
  596. */
  597. if (!cpuc->lbr_stack.nr || !from || !to)
  598. return 0;
  599. /*
  600. * Basic blocks should never cross user/kernel boundaries
  601. */
  602. if (kernel_ip(ip) != kernel_ip(to))
  603. return 0;
  604. /*
  605. * unsigned math, either ip is before the start (impossible) or
  606. * the basic block is larger than 1 page (sanity)
  607. */
  608. if ((ip - to) > PEBS_FIXUP_SIZE)
  609. return 0;
  610. /*
  611. * We sampled a branch insn, rewind using the LBR stack
  612. */
  613. if (ip == to) {
  614. set_linear_ip(regs, from);
  615. return 1;
  616. }
  617. size = ip - to;
  618. if (!kernel_ip(ip)) {
  619. int bytes;
  620. u8 *buf = this_cpu_read(insn_buffer);
  621. /* 'size' must fit our buffer, see above */
  622. bytes = copy_from_user_nmi(buf, (void __user *)to, size);
  623. if (bytes != 0)
  624. return 0;
  625. kaddr = buf;
  626. } else {
  627. kaddr = (void *)to;
  628. }
  629. do {
  630. struct insn insn;
  631. old_to = to;
  632. #ifdef CONFIG_X86_64
  633. is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
  634. #endif
  635. insn_init(&insn, kaddr, size, is_64bit);
  636. insn_get_length(&insn);
  637. /*
  638. * Make sure there was not a problem decoding the
  639. * instruction and getting the length. This is
  640. * doubly important because we have an infinite
  641. * loop if insn.length=0.
  642. */
  643. if (!insn.length)
  644. break;
  645. to += insn.length;
  646. kaddr += insn.length;
  647. size -= insn.length;
  648. } while (to < ip);
  649. if (to == ip) {
  650. set_linear_ip(regs, old_to);
  651. return 1;
  652. }
  653. /*
  654. * Even though we decoded the basic block, the instruction stream
  655. * never matched the given IP, either the TO or the IP got corrupted.
  656. */
  657. return 0;
  658. }
  659. static inline u64 intel_hsw_weight(struct pebs_record_hsw *pebs)
  660. {
  661. if (pebs->tsx_tuning) {
  662. union hsw_tsx_tuning tsx = { .value = pebs->tsx_tuning };
  663. return tsx.cycles_last_block;
  664. }
  665. return 0;
  666. }
  667. static inline u64 intel_hsw_transaction(struct pebs_record_hsw *pebs)
  668. {
  669. u64 txn = (pebs->tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32;
  670. /* For RTM XABORTs also log the abort code from AX */
  671. if ((txn & PERF_TXN_TRANSACTION) && (pebs->ax & 1))
  672. txn |= ((pebs->ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
  673. return txn;
  674. }
  675. static void __intel_pmu_pebs_event(struct perf_event *event,
  676. struct pt_regs *iregs, void *__pebs)
  677. {
  678. #define PERF_X86_EVENT_PEBS_HSW_PREC \
  679. (PERF_X86_EVENT_PEBS_ST_HSW | \
  680. PERF_X86_EVENT_PEBS_LD_HSW | \
  681. PERF_X86_EVENT_PEBS_NA_HSW)
  682. /*
  683. * We cast to the biggest pebs_record but are careful not to
  684. * unconditionally access the 'extra' entries.
  685. */
  686. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  687. struct pebs_record_hsw *pebs = __pebs;
  688. struct perf_sample_data data;
  689. struct pt_regs regs;
  690. u64 sample_type;
  691. int fll, fst, dsrc;
  692. int fl = event->hw.flags;
  693. if (!intel_pmu_save_and_restart(event))
  694. return;
  695. sample_type = event->attr.sample_type;
  696. dsrc = sample_type & PERF_SAMPLE_DATA_SRC;
  697. fll = fl & PERF_X86_EVENT_PEBS_LDLAT;
  698. fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC);
  699. perf_sample_data_init(&data, 0, event->hw.last_period);
  700. data.period = event->hw.last_period;
  701. /*
  702. * Use latency for weight (only avail with PEBS-LL)
  703. */
  704. if (fll && (sample_type & PERF_SAMPLE_WEIGHT))
  705. data.weight = pebs->lat;
  706. /*
  707. * data.data_src encodes the data source
  708. */
  709. if (dsrc) {
  710. u64 val = PERF_MEM_NA;
  711. if (fll)
  712. val = load_latency_data(pebs->dse);
  713. else if (fst && (fl & PERF_X86_EVENT_PEBS_HSW_PREC))
  714. val = precise_datala_hsw(event, pebs->dse);
  715. else if (fst)
  716. val = precise_store_data(pebs->dse);
  717. data.data_src.val = val;
  718. }
  719. /*
  720. * We use the interrupt regs as a base because the PEBS record
  721. * does not contain a full regs set, specifically it seems to
  722. * lack segment descriptors, which get used by things like
  723. * user_mode().
  724. *
  725. * In the simple case fix up only the IP and BP,SP regs, for
  726. * PERF_SAMPLE_IP and PERF_SAMPLE_CALLCHAIN to function properly.
  727. * A possible PERF_SAMPLE_REGS will have to transfer all regs.
  728. */
  729. regs = *iregs;
  730. regs.flags = pebs->flags;
  731. set_linear_ip(&regs, pebs->ip);
  732. regs.bp = pebs->bp;
  733. regs.sp = pebs->sp;
  734. if (sample_type & PERF_SAMPLE_REGS_INTR) {
  735. regs.ax = pebs->ax;
  736. regs.bx = pebs->bx;
  737. regs.cx = pebs->cx;
  738. regs.dx = pebs->dx;
  739. regs.si = pebs->si;
  740. regs.di = pebs->di;
  741. regs.bp = pebs->bp;
  742. regs.sp = pebs->sp;
  743. regs.flags = pebs->flags;
  744. #ifndef CONFIG_X86_32
  745. regs.r8 = pebs->r8;
  746. regs.r9 = pebs->r9;
  747. regs.r10 = pebs->r10;
  748. regs.r11 = pebs->r11;
  749. regs.r12 = pebs->r12;
  750. regs.r13 = pebs->r13;
  751. regs.r14 = pebs->r14;
  752. regs.r15 = pebs->r15;
  753. #endif
  754. }
  755. if (event->attr.precise_ip > 1 && x86_pmu.intel_cap.pebs_format >= 2) {
  756. regs.ip = pebs->real_ip;
  757. regs.flags |= PERF_EFLAGS_EXACT;
  758. } else if (event->attr.precise_ip > 1 && intel_pmu_pebs_fixup_ip(&regs))
  759. regs.flags |= PERF_EFLAGS_EXACT;
  760. else
  761. regs.flags &= ~PERF_EFLAGS_EXACT;
  762. if ((sample_type & PERF_SAMPLE_ADDR) &&
  763. x86_pmu.intel_cap.pebs_format >= 1)
  764. data.addr = pebs->dla;
  765. if (x86_pmu.intel_cap.pebs_format >= 2) {
  766. /* Only set the TSX weight when no memory weight. */
  767. if ((sample_type & PERF_SAMPLE_WEIGHT) && !fll)
  768. data.weight = intel_hsw_weight(pebs);
  769. if (sample_type & PERF_SAMPLE_TRANSACTION)
  770. data.txn = intel_hsw_transaction(pebs);
  771. }
  772. if (has_branch_stack(event))
  773. data.br_stack = &cpuc->lbr_stack;
  774. if (perf_event_overflow(event, &data, &regs))
  775. x86_pmu_stop(event, 0);
  776. }
  777. static void intel_pmu_drain_pebs_core(struct pt_regs *iregs)
  778. {
  779. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  780. struct debug_store *ds = cpuc->ds;
  781. struct perf_event *event = cpuc->events[0]; /* PMC0 only */
  782. struct pebs_record_core *at, *top;
  783. int n;
  784. if (!x86_pmu.pebs_active)
  785. return;
  786. at = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base;
  787. top = (struct pebs_record_core *)(unsigned long)ds->pebs_index;
  788. /*
  789. * Whatever else happens, drain the thing
  790. */
  791. ds->pebs_index = ds->pebs_buffer_base;
  792. if (!test_bit(0, cpuc->active_mask))
  793. return;
  794. WARN_ON_ONCE(!event);
  795. if (!event->attr.precise_ip)
  796. return;
  797. n = top - at;
  798. if (n <= 0)
  799. return;
  800. /*
  801. * Should not happen, we program the threshold at 1 and do not
  802. * set a reset value.
  803. */
  804. WARN_ONCE(n > 1, "bad leftover pebs %d\n", n);
  805. at += n - 1;
  806. __intel_pmu_pebs_event(event, iregs, at);
  807. }
  808. static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
  809. {
  810. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  811. struct debug_store *ds = cpuc->ds;
  812. struct perf_event *event = NULL;
  813. void *at, *top;
  814. u64 status = 0;
  815. int bit;
  816. if (!x86_pmu.pebs_active)
  817. return;
  818. at = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
  819. top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
  820. ds->pebs_index = ds->pebs_buffer_base;
  821. if (unlikely(at > top))
  822. return;
  823. /*
  824. * Should not happen, we program the threshold at 1 and do not
  825. * set a reset value.
  826. */
  827. WARN_ONCE(top - at > x86_pmu.max_pebs_events * x86_pmu.pebs_record_size,
  828. "Unexpected number of pebs records %ld\n",
  829. (long)(top - at) / x86_pmu.pebs_record_size);
  830. for (; at < top; at += x86_pmu.pebs_record_size) {
  831. struct pebs_record_nhm *p = at;
  832. for_each_set_bit(bit, (unsigned long *)&p->status,
  833. x86_pmu.max_pebs_events) {
  834. event = cpuc->events[bit];
  835. if (!test_bit(bit, cpuc->active_mask))
  836. continue;
  837. WARN_ON_ONCE(!event);
  838. if (!event->attr.precise_ip)
  839. continue;
  840. if (__test_and_set_bit(bit, (unsigned long *)&status))
  841. continue;
  842. break;
  843. }
  844. if (!event || bit >= x86_pmu.max_pebs_events)
  845. continue;
  846. __intel_pmu_pebs_event(event, iregs, at);
  847. }
  848. }
  849. /*
  850. * BTS, PEBS probe and setup
  851. */
  852. void __init intel_ds_init(void)
  853. {
  854. /*
  855. * No support for 32bit formats
  856. */
  857. if (!boot_cpu_has(X86_FEATURE_DTES64))
  858. return;
  859. x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS);
  860. x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS);
  861. if (x86_pmu.pebs) {
  862. char pebs_type = x86_pmu.intel_cap.pebs_trap ? '+' : '-';
  863. int format = x86_pmu.intel_cap.pebs_format;
  864. switch (format) {
  865. case 0:
  866. printk(KERN_CONT "PEBS fmt0%c, ", pebs_type);
  867. x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
  868. x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
  869. break;
  870. case 1:
  871. printk(KERN_CONT "PEBS fmt1%c, ", pebs_type);
  872. x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
  873. x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
  874. break;
  875. case 2:
  876. pr_cont("PEBS fmt2%c, ", pebs_type);
  877. x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw);
  878. x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
  879. break;
  880. default:
  881. printk(KERN_CONT "no PEBS fmt%d%c, ", format, pebs_type);
  882. x86_pmu.pebs = 0;
  883. }
  884. }
  885. }
  886. void perf_restore_debug_store(void)
  887. {
  888. struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
  889. if (!x86_pmu.bts && !x86_pmu.pebs)
  890. return;
  891. wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds);
  892. }