perf_event_intel_ds.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  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 << 4)
  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;
  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. return 0;
  239. }
  240. static void release_pebs_buffer(int cpu)
  241. {
  242. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  243. if (!ds || !x86_pmu.pebs)
  244. return;
  245. kfree(per_cpu(insn_buffer, cpu));
  246. per_cpu(insn_buffer, cpu) = NULL;
  247. kfree((void *)(unsigned long)ds->pebs_buffer_base);
  248. ds->pebs_buffer_base = 0;
  249. }
  250. static int alloc_bts_buffer(int cpu)
  251. {
  252. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  253. int node = cpu_to_node(cpu);
  254. int max, thresh;
  255. void *buffer;
  256. if (!x86_pmu.bts)
  257. return 0;
  258. buffer = kzalloc_node(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, node);
  259. if (unlikely(!buffer)) {
  260. WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__);
  261. return -ENOMEM;
  262. }
  263. max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE;
  264. thresh = max / 16;
  265. ds->bts_buffer_base = (u64)(unsigned long)buffer;
  266. ds->bts_index = ds->bts_buffer_base;
  267. ds->bts_absolute_maximum = ds->bts_buffer_base +
  268. max * BTS_RECORD_SIZE;
  269. ds->bts_interrupt_threshold = ds->bts_absolute_maximum -
  270. thresh * BTS_RECORD_SIZE;
  271. return 0;
  272. }
  273. static void release_bts_buffer(int cpu)
  274. {
  275. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  276. if (!ds || !x86_pmu.bts)
  277. return;
  278. kfree((void *)(unsigned long)ds->bts_buffer_base);
  279. ds->bts_buffer_base = 0;
  280. }
  281. static int alloc_ds_buffer(int cpu)
  282. {
  283. int node = cpu_to_node(cpu);
  284. struct debug_store *ds;
  285. ds = kzalloc_node(sizeof(*ds), GFP_KERNEL, node);
  286. if (unlikely(!ds))
  287. return -ENOMEM;
  288. per_cpu(cpu_hw_events, cpu).ds = ds;
  289. return 0;
  290. }
  291. static void release_ds_buffer(int cpu)
  292. {
  293. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  294. if (!ds)
  295. return;
  296. per_cpu(cpu_hw_events, cpu).ds = NULL;
  297. kfree(ds);
  298. }
  299. void release_ds_buffers(void)
  300. {
  301. int cpu;
  302. if (!x86_pmu.bts && !x86_pmu.pebs)
  303. return;
  304. get_online_cpus();
  305. for_each_online_cpu(cpu)
  306. fini_debug_store_on_cpu(cpu);
  307. for_each_possible_cpu(cpu) {
  308. release_pebs_buffer(cpu);
  309. release_bts_buffer(cpu);
  310. release_ds_buffer(cpu);
  311. }
  312. put_online_cpus();
  313. }
  314. void reserve_ds_buffers(void)
  315. {
  316. int bts_err = 0, pebs_err = 0;
  317. int cpu;
  318. x86_pmu.bts_active = 0;
  319. x86_pmu.pebs_active = 0;
  320. if (!x86_pmu.bts && !x86_pmu.pebs)
  321. return;
  322. if (!x86_pmu.bts)
  323. bts_err = 1;
  324. if (!x86_pmu.pebs)
  325. pebs_err = 1;
  326. get_online_cpus();
  327. for_each_possible_cpu(cpu) {
  328. if (alloc_ds_buffer(cpu)) {
  329. bts_err = 1;
  330. pebs_err = 1;
  331. }
  332. if (!bts_err && alloc_bts_buffer(cpu))
  333. bts_err = 1;
  334. if (!pebs_err && alloc_pebs_buffer(cpu))
  335. pebs_err = 1;
  336. if (bts_err && pebs_err)
  337. break;
  338. }
  339. if (bts_err) {
  340. for_each_possible_cpu(cpu)
  341. release_bts_buffer(cpu);
  342. }
  343. if (pebs_err) {
  344. for_each_possible_cpu(cpu)
  345. release_pebs_buffer(cpu);
  346. }
  347. if (bts_err && pebs_err) {
  348. for_each_possible_cpu(cpu)
  349. release_ds_buffer(cpu);
  350. } else {
  351. if (x86_pmu.bts && !bts_err)
  352. x86_pmu.bts_active = 1;
  353. if (x86_pmu.pebs && !pebs_err)
  354. x86_pmu.pebs_active = 1;
  355. for_each_online_cpu(cpu)
  356. init_debug_store_on_cpu(cpu);
  357. }
  358. put_online_cpus();
  359. }
  360. /*
  361. * BTS
  362. */
  363. struct event_constraint bts_constraint =
  364. EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0);
  365. void intel_pmu_enable_bts(u64 config)
  366. {
  367. unsigned long debugctlmsr;
  368. debugctlmsr = get_debugctlmsr();
  369. debugctlmsr |= DEBUGCTLMSR_TR;
  370. debugctlmsr |= DEBUGCTLMSR_BTS;
  371. if (config & ARCH_PERFMON_EVENTSEL_INT)
  372. debugctlmsr |= DEBUGCTLMSR_BTINT;
  373. if (!(config & ARCH_PERFMON_EVENTSEL_OS))
  374. debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
  375. if (!(config & ARCH_PERFMON_EVENTSEL_USR))
  376. debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR;
  377. update_debugctlmsr(debugctlmsr);
  378. }
  379. void intel_pmu_disable_bts(void)
  380. {
  381. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  382. unsigned long debugctlmsr;
  383. if (!cpuc->ds)
  384. return;
  385. debugctlmsr = get_debugctlmsr();
  386. debugctlmsr &=
  387. ~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT |
  388. DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR);
  389. update_debugctlmsr(debugctlmsr);
  390. }
  391. int intel_pmu_drain_bts_buffer(void)
  392. {
  393. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  394. struct debug_store *ds = cpuc->ds;
  395. struct bts_record {
  396. u64 from;
  397. u64 to;
  398. u64 flags;
  399. };
  400. struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
  401. struct bts_record *at, *top;
  402. struct perf_output_handle handle;
  403. struct perf_event_header header;
  404. struct perf_sample_data data;
  405. struct pt_regs regs;
  406. if (!event)
  407. return 0;
  408. if (!x86_pmu.bts_active)
  409. return 0;
  410. at = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
  411. top = (struct bts_record *)(unsigned long)ds->bts_index;
  412. if (top <= at)
  413. return 0;
  414. memset(&regs, 0, sizeof(regs));
  415. ds->bts_index = ds->bts_buffer_base;
  416. perf_sample_data_init(&data, 0, event->hw.last_period);
  417. /*
  418. * Prepare a generic sample, i.e. fill in the invariant fields.
  419. * We will overwrite the from and to address before we output
  420. * the sample.
  421. */
  422. perf_prepare_sample(&header, &data, event, &regs);
  423. if (perf_output_begin(&handle, event, header.size * (top - at)))
  424. return 1;
  425. for (; at < top; at++) {
  426. data.ip = at->from;
  427. data.addr = at->to;
  428. perf_output_sample(&handle, &header, &data, event);
  429. }
  430. perf_output_end(&handle);
  431. /* There's new data available. */
  432. event->hw.interrupts++;
  433. event->pending_kill = POLL_IN;
  434. return 1;
  435. }
  436. static inline void intel_pmu_drain_pebs_buffer(void)
  437. {
  438. struct pt_regs regs;
  439. x86_pmu.drain_pebs(&regs);
  440. }
  441. void intel_pmu_pebs_sched_task(struct perf_event_context *ctx, bool sched_in)
  442. {
  443. if (!sched_in)
  444. intel_pmu_drain_pebs_buffer();
  445. }
  446. /*
  447. * PEBS
  448. */
  449. struct event_constraint intel_core2_pebs_event_constraints[] = {
  450. INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
  451. INTEL_FLAGS_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
  452. INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
  453. INTEL_FLAGS_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
  454. INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
  455. /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
  456. INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01),
  457. EVENT_CONSTRAINT_END
  458. };
  459. struct event_constraint intel_atom_pebs_event_constraints[] = {
  460. INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
  461. INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
  462. INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
  463. /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
  464. INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01),
  465. EVENT_CONSTRAINT_END
  466. };
  467. struct event_constraint intel_slm_pebs_event_constraints[] = {
  468. /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
  469. INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x1),
  470. /* Allow all events as PEBS with no flags */
  471. INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
  472. EVENT_CONSTRAINT_END
  473. };
  474. struct event_constraint intel_nehalem_pebs_event_constraints[] = {
  475. INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
  476. INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
  477. INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
  478. INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INST_RETIRED.ANY */
  479. INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
  480. INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  481. INTEL_FLAGS_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
  482. INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
  483. INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
  484. INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
  485. INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
  486. /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
  487. INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
  488. EVENT_CONSTRAINT_END
  489. };
  490. struct event_constraint intel_westmere_pebs_event_constraints[] = {
  491. INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
  492. INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
  493. INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
  494. INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INSTR_RETIRED.* */
  495. INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
  496. INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  497. INTEL_FLAGS_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */
  498. INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
  499. INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
  500. INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
  501. INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
  502. /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
  503. INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
  504. EVENT_CONSTRAINT_END
  505. };
  506. struct event_constraint intel_snb_pebs_event_constraints[] = {
  507. INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
  508. INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
  509. INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
  510. /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
  511. INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
  512. INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
  513. INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  514. INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
  515. INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
  516. /* Allow all events as PEBS with no flags */
  517. INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
  518. EVENT_CONSTRAINT_END
  519. };
  520. struct event_constraint intel_ivb_pebs_event_constraints[] = {
  521. INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
  522. INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
  523. INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
  524. /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
  525. INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
  526. INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
  527. INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  528. INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
  529. INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
  530. /* Allow all events as PEBS with no flags */
  531. INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
  532. EVENT_CONSTRAINT_END
  533. };
  534. struct event_constraint intel_hsw_pebs_event_constraints[] = {
  535. INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
  536. INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */
  537. /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
  538. INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
  539. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
  540. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
  541. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
  542. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
  543. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
  544. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
  545. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
  546. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
  547. INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  548. INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
  549. INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
  550. /* Allow all events as PEBS with no flags */
  551. INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
  552. EVENT_CONSTRAINT_END
  553. };
  554. struct event_constraint *intel_pebs_constraints(struct perf_event *event)
  555. {
  556. struct event_constraint *c;
  557. if (!event->attr.precise_ip)
  558. return NULL;
  559. if (x86_pmu.pebs_constraints) {
  560. for_each_event_constraint(c, x86_pmu.pebs_constraints) {
  561. if ((event->hw.config & c->cmask) == c->code) {
  562. event->hw.flags |= c->flags;
  563. return c;
  564. }
  565. }
  566. }
  567. return &emptyconstraint;
  568. }
  569. static inline bool pebs_is_enabled(struct cpu_hw_events *cpuc)
  570. {
  571. return (cpuc->pebs_enabled & ((1ULL << MAX_PEBS_EVENTS) - 1));
  572. }
  573. void intel_pmu_pebs_enable(struct perf_event *event)
  574. {
  575. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  576. struct hw_perf_event *hwc = &event->hw;
  577. struct debug_store *ds = cpuc->ds;
  578. bool first_pebs;
  579. u64 threshold;
  580. hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;
  581. first_pebs = !pebs_is_enabled(cpuc);
  582. cpuc->pebs_enabled |= 1ULL << hwc->idx;
  583. if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
  584. cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32);
  585. else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
  586. cpuc->pebs_enabled |= 1ULL << 63;
  587. /*
  588. * When the event is constrained enough we can use a larger
  589. * threshold and run the event with less frequent PMI.
  590. */
  591. if (hwc->flags & PERF_X86_EVENT_FREERUNNING) {
  592. threshold = ds->pebs_absolute_maximum -
  593. x86_pmu.max_pebs_events * x86_pmu.pebs_record_size;
  594. if (first_pebs)
  595. perf_sched_cb_inc(event->ctx->pmu);
  596. } else {
  597. threshold = ds->pebs_buffer_base + x86_pmu.pebs_record_size;
  598. /*
  599. * If not all events can use larger buffer,
  600. * roll back to threshold = 1
  601. */
  602. if (!first_pebs &&
  603. (ds->pebs_interrupt_threshold > threshold))
  604. perf_sched_cb_dec(event->ctx->pmu);
  605. }
  606. /* Use auto-reload if possible to save a MSR write in the PMI */
  607. if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
  608. ds->pebs_event_reset[hwc->idx] =
  609. (u64)(-hwc->sample_period) & x86_pmu.cntval_mask;
  610. }
  611. if (first_pebs || ds->pebs_interrupt_threshold > threshold)
  612. ds->pebs_interrupt_threshold = threshold;
  613. }
  614. void intel_pmu_pebs_disable(struct perf_event *event)
  615. {
  616. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  617. struct hw_perf_event *hwc = &event->hw;
  618. struct debug_store *ds = cpuc->ds;
  619. cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
  620. if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
  621. cpuc->pebs_enabled &= ~(1ULL << (hwc->idx + 32));
  622. else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
  623. cpuc->pebs_enabled &= ~(1ULL << 63);
  624. if (ds->pebs_interrupt_threshold >
  625. ds->pebs_buffer_base + x86_pmu.pebs_record_size) {
  626. intel_pmu_drain_pebs_buffer();
  627. if (!pebs_is_enabled(cpuc))
  628. perf_sched_cb_dec(event->ctx->pmu);
  629. }
  630. if (cpuc->enabled)
  631. wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
  632. hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
  633. }
  634. void intel_pmu_pebs_enable_all(void)
  635. {
  636. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  637. if (cpuc->pebs_enabled)
  638. wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
  639. }
  640. void intel_pmu_pebs_disable_all(void)
  641. {
  642. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  643. if (cpuc->pebs_enabled)
  644. wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
  645. }
  646. static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
  647. {
  648. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  649. unsigned long from = cpuc->lbr_entries[0].from;
  650. unsigned long old_to, to = cpuc->lbr_entries[0].to;
  651. unsigned long ip = regs->ip;
  652. int is_64bit = 0;
  653. void *kaddr;
  654. int size;
  655. /*
  656. * We don't need to fixup if the PEBS assist is fault like
  657. */
  658. if (!x86_pmu.intel_cap.pebs_trap)
  659. return 1;
  660. /*
  661. * No LBR entry, no basic block, no rewinding
  662. */
  663. if (!cpuc->lbr_stack.nr || !from || !to)
  664. return 0;
  665. /*
  666. * Basic blocks should never cross user/kernel boundaries
  667. */
  668. if (kernel_ip(ip) != kernel_ip(to))
  669. return 0;
  670. /*
  671. * unsigned math, either ip is before the start (impossible) or
  672. * the basic block is larger than 1 page (sanity)
  673. */
  674. if ((ip - to) > PEBS_FIXUP_SIZE)
  675. return 0;
  676. /*
  677. * We sampled a branch insn, rewind using the LBR stack
  678. */
  679. if (ip == to) {
  680. set_linear_ip(regs, from);
  681. return 1;
  682. }
  683. size = ip - to;
  684. if (!kernel_ip(ip)) {
  685. int bytes;
  686. u8 *buf = this_cpu_read(insn_buffer);
  687. /* 'size' must fit our buffer, see above */
  688. bytes = copy_from_user_nmi(buf, (void __user *)to, size);
  689. if (bytes != 0)
  690. return 0;
  691. kaddr = buf;
  692. } else {
  693. kaddr = (void *)to;
  694. }
  695. do {
  696. struct insn insn;
  697. old_to = to;
  698. #ifdef CONFIG_X86_64
  699. is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
  700. #endif
  701. insn_init(&insn, kaddr, size, is_64bit);
  702. insn_get_length(&insn);
  703. /*
  704. * Make sure there was not a problem decoding the
  705. * instruction and getting the length. This is
  706. * doubly important because we have an infinite
  707. * loop if insn.length=0.
  708. */
  709. if (!insn.length)
  710. break;
  711. to += insn.length;
  712. kaddr += insn.length;
  713. size -= insn.length;
  714. } while (to < ip);
  715. if (to == ip) {
  716. set_linear_ip(regs, old_to);
  717. return 1;
  718. }
  719. /*
  720. * Even though we decoded the basic block, the instruction stream
  721. * never matched the given IP, either the TO or the IP got corrupted.
  722. */
  723. return 0;
  724. }
  725. static inline u64 intel_hsw_weight(struct pebs_record_hsw *pebs)
  726. {
  727. if (pebs->tsx_tuning) {
  728. union hsw_tsx_tuning tsx = { .value = pebs->tsx_tuning };
  729. return tsx.cycles_last_block;
  730. }
  731. return 0;
  732. }
  733. static inline u64 intel_hsw_transaction(struct pebs_record_hsw *pebs)
  734. {
  735. u64 txn = (pebs->tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32;
  736. /* For RTM XABORTs also log the abort code from AX */
  737. if ((txn & PERF_TXN_TRANSACTION) && (pebs->ax & 1))
  738. txn |= ((pebs->ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
  739. return txn;
  740. }
  741. static void setup_pebs_sample_data(struct perf_event *event,
  742. struct pt_regs *iregs, void *__pebs,
  743. struct perf_sample_data *data,
  744. struct pt_regs *regs)
  745. {
  746. #define PERF_X86_EVENT_PEBS_HSW_PREC \
  747. (PERF_X86_EVENT_PEBS_ST_HSW | \
  748. PERF_X86_EVENT_PEBS_LD_HSW | \
  749. PERF_X86_EVENT_PEBS_NA_HSW)
  750. /*
  751. * We cast to the biggest pebs_record but are careful not to
  752. * unconditionally access the 'extra' entries.
  753. */
  754. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  755. struct pebs_record_hsw *pebs = __pebs;
  756. u64 sample_type;
  757. int fll, fst, dsrc;
  758. int fl = event->hw.flags;
  759. if (pebs == NULL)
  760. return;
  761. sample_type = event->attr.sample_type;
  762. dsrc = sample_type & PERF_SAMPLE_DATA_SRC;
  763. fll = fl & PERF_X86_EVENT_PEBS_LDLAT;
  764. fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC);
  765. perf_sample_data_init(data, 0, event->hw.last_period);
  766. data->period = event->hw.last_period;
  767. /*
  768. * Use latency for weight (only avail with PEBS-LL)
  769. */
  770. if (fll && (sample_type & PERF_SAMPLE_WEIGHT))
  771. data->weight = pebs->lat;
  772. /*
  773. * data.data_src encodes the data source
  774. */
  775. if (dsrc) {
  776. u64 val = PERF_MEM_NA;
  777. if (fll)
  778. val = load_latency_data(pebs->dse);
  779. else if (fst && (fl & PERF_X86_EVENT_PEBS_HSW_PREC))
  780. val = precise_datala_hsw(event, pebs->dse);
  781. else if (fst)
  782. val = precise_store_data(pebs->dse);
  783. data->data_src.val = val;
  784. }
  785. /*
  786. * We use the interrupt regs as a base because the PEBS record
  787. * does not contain a full regs set, specifically it seems to
  788. * lack segment descriptors, which get used by things like
  789. * user_mode().
  790. *
  791. * In the simple case fix up only the IP and BP,SP regs, for
  792. * PERF_SAMPLE_IP and PERF_SAMPLE_CALLCHAIN to function properly.
  793. * A possible PERF_SAMPLE_REGS will have to transfer all regs.
  794. */
  795. *regs = *iregs;
  796. regs->flags = pebs->flags;
  797. set_linear_ip(regs, pebs->ip);
  798. regs->bp = pebs->bp;
  799. regs->sp = pebs->sp;
  800. if (sample_type & PERF_SAMPLE_REGS_INTR) {
  801. regs->ax = pebs->ax;
  802. regs->bx = pebs->bx;
  803. regs->cx = pebs->cx;
  804. regs->dx = pebs->dx;
  805. regs->si = pebs->si;
  806. regs->di = pebs->di;
  807. regs->bp = pebs->bp;
  808. regs->sp = pebs->sp;
  809. regs->flags = pebs->flags;
  810. #ifndef CONFIG_X86_32
  811. regs->r8 = pebs->r8;
  812. regs->r9 = pebs->r9;
  813. regs->r10 = pebs->r10;
  814. regs->r11 = pebs->r11;
  815. regs->r12 = pebs->r12;
  816. regs->r13 = pebs->r13;
  817. regs->r14 = pebs->r14;
  818. regs->r15 = pebs->r15;
  819. #endif
  820. }
  821. if (event->attr.precise_ip > 1 && x86_pmu.intel_cap.pebs_format >= 2) {
  822. regs->ip = pebs->real_ip;
  823. regs->flags |= PERF_EFLAGS_EXACT;
  824. } else if (event->attr.precise_ip > 1 && intel_pmu_pebs_fixup_ip(regs))
  825. regs->flags |= PERF_EFLAGS_EXACT;
  826. else
  827. regs->flags &= ~PERF_EFLAGS_EXACT;
  828. if ((sample_type & PERF_SAMPLE_ADDR) &&
  829. x86_pmu.intel_cap.pebs_format >= 1)
  830. data->addr = pebs->dla;
  831. if (x86_pmu.intel_cap.pebs_format >= 2) {
  832. /* Only set the TSX weight when no memory weight. */
  833. if ((sample_type & PERF_SAMPLE_WEIGHT) && !fll)
  834. data->weight = intel_hsw_weight(pebs);
  835. if (sample_type & PERF_SAMPLE_TRANSACTION)
  836. data->txn = intel_hsw_transaction(pebs);
  837. }
  838. if (has_branch_stack(event))
  839. data->br_stack = &cpuc->lbr_stack;
  840. }
  841. static inline void *
  842. get_next_pebs_record_by_bit(void *base, void *top, int bit)
  843. {
  844. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  845. void *at;
  846. u64 pebs_status;
  847. if (base == NULL)
  848. return NULL;
  849. for (at = base; at < top; at += x86_pmu.pebs_record_size) {
  850. struct pebs_record_nhm *p = at;
  851. if (test_bit(bit, (unsigned long *)&p->status)) {
  852. /* PEBS v3 has accurate status bits */
  853. if (x86_pmu.intel_cap.pebs_format >= 3)
  854. return at;
  855. if (p->status == (1 << bit))
  856. return at;
  857. /* clear non-PEBS bit and re-check */
  858. pebs_status = p->status & cpuc->pebs_enabled;
  859. pebs_status &= (1ULL << MAX_PEBS_EVENTS) - 1;
  860. if (pebs_status == (1 << bit))
  861. return at;
  862. }
  863. }
  864. return NULL;
  865. }
  866. static void __intel_pmu_pebs_event(struct perf_event *event,
  867. struct pt_regs *iregs,
  868. void *base, void *top,
  869. int bit, int count)
  870. {
  871. struct perf_sample_data data;
  872. struct pt_regs regs;
  873. void *at = get_next_pebs_record_by_bit(base, top, bit);
  874. if (!intel_pmu_save_and_restart(event) &&
  875. !(event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD))
  876. return;
  877. while (count > 1) {
  878. setup_pebs_sample_data(event, iregs, at, &data, &regs);
  879. perf_event_output(event, &data, &regs);
  880. at += x86_pmu.pebs_record_size;
  881. at = get_next_pebs_record_by_bit(at, top, bit);
  882. count--;
  883. }
  884. setup_pebs_sample_data(event, iregs, at, &data, &regs);
  885. /*
  886. * All but the last records are processed.
  887. * The last one is left to be able to call the overflow handler.
  888. */
  889. if (perf_event_overflow(event, &data, &regs)) {
  890. x86_pmu_stop(event, 0);
  891. return;
  892. }
  893. }
  894. static void intel_pmu_drain_pebs_core(struct pt_regs *iregs)
  895. {
  896. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  897. struct debug_store *ds = cpuc->ds;
  898. struct perf_event *event = cpuc->events[0]; /* PMC0 only */
  899. struct pebs_record_core *at, *top;
  900. int n;
  901. if (!x86_pmu.pebs_active)
  902. return;
  903. at = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base;
  904. top = (struct pebs_record_core *)(unsigned long)ds->pebs_index;
  905. /*
  906. * Whatever else happens, drain the thing
  907. */
  908. ds->pebs_index = ds->pebs_buffer_base;
  909. if (!test_bit(0, cpuc->active_mask))
  910. return;
  911. WARN_ON_ONCE(!event);
  912. if (!event->attr.precise_ip)
  913. return;
  914. n = (top - at) / x86_pmu.pebs_record_size;
  915. if (n <= 0)
  916. return;
  917. __intel_pmu_pebs_event(event, iregs, at, top, 0, n);
  918. }
  919. static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
  920. {
  921. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  922. struct debug_store *ds = cpuc->ds;
  923. struct perf_event *event;
  924. void *base, *at, *top;
  925. short counts[MAX_PEBS_EVENTS] = {};
  926. short error[MAX_PEBS_EVENTS] = {};
  927. int bit, i;
  928. if (!x86_pmu.pebs_active)
  929. return;
  930. base = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
  931. top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
  932. ds->pebs_index = ds->pebs_buffer_base;
  933. if (unlikely(base >= top))
  934. return;
  935. for (at = base; at < top; at += x86_pmu.pebs_record_size) {
  936. struct pebs_record_nhm *p = at;
  937. /* PEBS v3 has accurate status bits */
  938. if (x86_pmu.intel_cap.pebs_format >= 3) {
  939. for_each_set_bit(bit, (unsigned long *)&p->status,
  940. MAX_PEBS_EVENTS)
  941. counts[bit]++;
  942. continue;
  943. }
  944. bit = find_first_bit((unsigned long *)&p->status,
  945. x86_pmu.max_pebs_events);
  946. if (bit >= x86_pmu.max_pebs_events)
  947. continue;
  948. if (!test_bit(bit, cpuc->active_mask))
  949. continue;
  950. /*
  951. * The PEBS hardware does not deal well with the situation
  952. * when events happen near to each other and multiple bits
  953. * are set. But it should happen rarely.
  954. *
  955. * If these events include one PEBS and multiple non-PEBS
  956. * events, it doesn't impact PEBS record. The record will
  957. * be handled normally. (slow path)
  958. *
  959. * If these events include two or more PEBS events, the
  960. * records for the events can be collapsed into a single
  961. * one, and it's not possible to reconstruct all events
  962. * that caused the PEBS record. It's called collision.
  963. * If collision happened, the record will be dropped.
  964. *
  965. */
  966. if (p->status != (1 << bit)) {
  967. u64 pebs_status;
  968. /* slow path */
  969. pebs_status = p->status & cpuc->pebs_enabled;
  970. pebs_status &= (1ULL << MAX_PEBS_EVENTS) - 1;
  971. if (pebs_status != (1 << bit)) {
  972. for_each_set_bit(i, (unsigned long *)&pebs_status,
  973. MAX_PEBS_EVENTS)
  974. error[i]++;
  975. continue;
  976. }
  977. }
  978. counts[bit]++;
  979. }
  980. for (bit = 0; bit < x86_pmu.max_pebs_events; bit++) {
  981. if ((counts[bit] == 0) && (error[bit] == 0))
  982. continue;
  983. event = cpuc->events[bit];
  984. WARN_ON_ONCE(!event);
  985. WARN_ON_ONCE(!event->attr.precise_ip);
  986. /* log dropped samples number */
  987. if (error[bit])
  988. perf_log_lost_samples(event, error[bit]);
  989. if (counts[bit]) {
  990. __intel_pmu_pebs_event(event, iregs, base,
  991. top, bit, counts[bit]);
  992. }
  993. }
  994. }
  995. /*
  996. * BTS, PEBS probe and setup
  997. */
  998. void __init intel_ds_init(void)
  999. {
  1000. /*
  1001. * No support for 32bit formats
  1002. */
  1003. if (!boot_cpu_has(X86_FEATURE_DTES64))
  1004. return;
  1005. x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS);
  1006. x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS);
  1007. if (x86_pmu.pebs) {
  1008. char pebs_type = x86_pmu.intel_cap.pebs_trap ? '+' : '-';
  1009. int format = x86_pmu.intel_cap.pebs_format;
  1010. switch (format) {
  1011. case 0:
  1012. printk(KERN_CONT "PEBS fmt0%c, ", pebs_type);
  1013. x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
  1014. x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
  1015. break;
  1016. case 1:
  1017. printk(KERN_CONT "PEBS fmt1%c, ", pebs_type);
  1018. x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
  1019. x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
  1020. break;
  1021. case 2:
  1022. pr_cont("PEBS fmt2%c, ", pebs_type);
  1023. x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw);
  1024. x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
  1025. break;
  1026. default:
  1027. printk(KERN_CONT "no PEBS fmt%d%c, ", format, pebs_type);
  1028. x86_pmu.pebs = 0;
  1029. }
  1030. }
  1031. }
  1032. void perf_restore_debug_store(void)
  1033. {
  1034. struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
  1035. if (!x86_pmu.bts && !x86_pmu.pebs)
  1036. return;
  1037. wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds);
  1038. }