perf_event_intel_ds.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. /*
  143. * Same as pebs_record_nhm, with two additional fields.
  144. */
  145. struct pebs_record_hsw {
  146. struct pebs_record_nhm nhm;
  147. /*
  148. * Real IP of the event. In the Intel documentation this
  149. * is called eventingrip.
  150. */
  151. u64 real_ip;
  152. /*
  153. * TSX tuning information field: abort cycles and abort flags.
  154. */
  155. u64 tsx_tuning;
  156. };
  157. void init_debug_store_on_cpu(int cpu)
  158. {
  159. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  160. if (!ds)
  161. return;
  162. wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA,
  163. (u32)((u64)(unsigned long)ds),
  164. (u32)((u64)(unsigned long)ds >> 32));
  165. }
  166. void fini_debug_store_on_cpu(int cpu)
  167. {
  168. if (!per_cpu(cpu_hw_events, cpu).ds)
  169. return;
  170. wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0);
  171. }
  172. static int alloc_pebs_buffer(int cpu)
  173. {
  174. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  175. int node = cpu_to_node(cpu);
  176. int max, thresh = 1; /* always use a single PEBS record */
  177. void *buffer;
  178. if (!x86_pmu.pebs)
  179. return 0;
  180. buffer = kmalloc_node(PEBS_BUFFER_SIZE, GFP_KERNEL | __GFP_ZERO, node);
  181. if (unlikely(!buffer))
  182. return -ENOMEM;
  183. max = PEBS_BUFFER_SIZE / x86_pmu.pebs_record_size;
  184. ds->pebs_buffer_base = (u64)(unsigned long)buffer;
  185. ds->pebs_index = ds->pebs_buffer_base;
  186. ds->pebs_absolute_maximum = ds->pebs_buffer_base +
  187. max * x86_pmu.pebs_record_size;
  188. ds->pebs_interrupt_threshold = ds->pebs_buffer_base +
  189. thresh * x86_pmu.pebs_record_size;
  190. return 0;
  191. }
  192. static void release_pebs_buffer(int cpu)
  193. {
  194. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  195. if (!ds || !x86_pmu.pebs)
  196. return;
  197. kfree((void *)(unsigned long)ds->pebs_buffer_base);
  198. ds->pebs_buffer_base = 0;
  199. }
  200. static int alloc_bts_buffer(int cpu)
  201. {
  202. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  203. int node = cpu_to_node(cpu);
  204. int max, thresh;
  205. void *buffer;
  206. if (!x86_pmu.bts)
  207. return 0;
  208. buffer = kmalloc_node(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_ZERO, node);
  209. if (unlikely(!buffer))
  210. return -ENOMEM;
  211. max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE;
  212. thresh = max / 16;
  213. ds->bts_buffer_base = (u64)(unsigned long)buffer;
  214. ds->bts_index = ds->bts_buffer_base;
  215. ds->bts_absolute_maximum = ds->bts_buffer_base +
  216. max * BTS_RECORD_SIZE;
  217. ds->bts_interrupt_threshold = ds->bts_absolute_maximum -
  218. thresh * BTS_RECORD_SIZE;
  219. return 0;
  220. }
  221. static void release_bts_buffer(int cpu)
  222. {
  223. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  224. if (!ds || !x86_pmu.bts)
  225. return;
  226. kfree((void *)(unsigned long)ds->bts_buffer_base);
  227. ds->bts_buffer_base = 0;
  228. }
  229. static int alloc_ds_buffer(int cpu)
  230. {
  231. int node = cpu_to_node(cpu);
  232. struct debug_store *ds;
  233. ds = kmalloc_node(sizeof(*ds), GFP_KERNEL | __GFP_ZERO, node);
  234. if (unlikely(!ds))
  235. return -ENOMEM;
  236. per_cpu(cpu_hw_events, cpu).ds = ds;
  237. return 0;
  238. }
  239. static void release_ds_buffer(int cpu)
  240. {
  241. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  242. if (!ds)
  243. return;
  244. per_cpu(cpu_hw_events, cpu).ds = NULL;
  245. kfree(ds);
  246. }
  247. void release_ds_buffers(void)
  248. {
  249. int cpu;
  250. if (!x86_pmu.bts && !x86_pmu.pebs)
  251. return;
  252. get_online_cpus();
  253. for_each_online_cpu(cpu)
  254. fini_debug_store_on_cpu(cpu);
  255. for_each_possible_cpu(cpu) {
  256. release_pebs_buffer(cpu);
  257. release_bts_buffer(cpu);
  258. release_ds_buffer(cpu);
  259. }
  260. put_online_cpus();
  261. }
  262. void reserve_ds_buffers(void)
  263. {
  264. int bts_err = 0, pebs_err = 0;
  265. int cpu;
  266. x86_pmu.bts_active = 0;
  267. x86_pmu.pebs_active = 0;
  268. if (!x86_pmu.bts && !x86_pmu.pebs)
  269. return;
  270. if (!x86_pmu.bts)
  271. bts_err = 1;
  272. if (!x86_pmu.pebs)
  273. pebs_err = 1;
  274. get_online_cpus();
  275. for_each_possible_cpu(cpu) {
  276. if (alloc_ds_buffer(cpu)) {
  277. bts_err = 1;
  278. pebs_err = 1;
  279. }
  280. if (!bts_err && alloc_bts_buffer(cpu))
  281. bts_err = 1;
  282. if (!pebs_err && alloc_pebs_buffer(cpu))
  283. pebs_err = 1;
  284. if (bts_err && pebs_err)
  285. break;
  286. }
  287. if (bts_err) {
  288. for_each_possible_cpu(cpu)
  289. release_bts_buffer(cpu);
  290. }
  291. if (pebs_err) {
  292. for_each_possible_cpu(cpu)
  293. release_pebs_buffer(cpu);
  294. }
  295. if (bts_err && pebs_err) {
  296. for_each_possible_cpu(cpu)
  297. release_ds_buffer(cpu);
  298. } else {
  299. if (x86_pmu.bts && !bts_err)
  300. x86_pmu.bts_active = 1;
  301. if (x86_pmu.pebs && !pebs_err)
  302. x86_pmu.pebs_active = 1;
  303. for_each_online_cpu(cpu)
  304. init_debug_store_on_cpu(cpu);
  305. }
  306. put_online_cpus();
  307. }
  308. /*
  309. * BTS
  310. */
  311. struct event_constraint bts_constraint =
  312. EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0);
  313. void intel_pmu_enable_bts(u64 config)
  314. {
  315. unsigned long debugctlmsr;
  316. debugctlmsr = get_debugctlmsr();
  317. debugctlmsr |= DEBUGCTLMSR_TR;
  318. debugctlmsr |= DEBUGCTLMSR_BTS;
  319. debugctlmsr |= DEBUGCTLMSR_BTINT;
  320. if (!(config & ARCH_PERFMON_EVENTSEL_OS))
  321. debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
  322. if (!(config & ARCH_PERFMON_EVENTSEL_USR))
  323. debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR;
  324. update_debugctlmsr(debugctlmsr);
  325. }
  326. void intel_pmu_disable_bts(void)
  327. {
  328. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  329. unsigned long debugctlmsr;
  330. if (!cpuc->ds)
  331. return;
  332. debugctlmsr = get_debugctlmsr();
  333. debugctlmsr &=
  334. ~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT |
  335. DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR);
  336. update_debugctlmsr(debugctlmsr);
  337. }
  338. int intel_pmu_drain_bts_buffer(void)
  339. {
  340. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  341. struct debug_store *ds = cpuc->ds;
  342. struct bts_record {
  343. u64 from;
  344. u64 to;
  345. u64 flags;
  346. };
  347. struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
  348. struct bts_record *at, *top;
  349. struct perf_output_handle handle;
  350. struct perf_event_header header;
  351. struct perf_sample_data data;
  352. struct pt_regs regs;
  353. if (!event)
  354. return 0;
  355. if (!x86_pmu.bts_active)
  356. return 0;
  357. at = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
  358. top = (struct bts_record *)(unsigned long)ds->bts_index;
  359. if (top <= at)
  360. return 0;
  361. memset(&regs, 0, sizeof(regs));
  362. ds->bts_index = ds->bts_buffer_base;
  363. perf_sample_data_init(&data, 0, event->hw.last_period);
  364. /*
  365. * Prepare a generic sample, i.e. fill in the invariant fields.
  366. * We will overwrite the from and to address before we output
  367. * the sample.
  368. */
  369. perf_prepare_sample(&header, &data, event, &regs);
  370. if (perf_output_begin(&handle, event, header.size * (top - at)))
  371. return 1;
  372. for (; at < top; at++) {
  373. data.ip = at->from;
  374. data.addr = at->to;
  375. perf_output_sample(&handle, &header, &data, event);
  376. }
  377. perf_output_end(&handle);
  378. /* There's new data available. */
  379. event->hw.interrupts++;
  380. event->pending_kill = POLL_IN;
  381. return 1;
  382. }
  383. /*
  384. * PEBS
  385. */
  386. struct event_constraint intel_core2_pebs_event_constraints[] = {
  387. INTEL_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
  388. INTEL_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
  389. INTEL_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
  390. INTEL_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
  391. INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
  392. EVENT_CONSTRAINT_END
  393. };
  394. struct event_constraint intel_atom_pebs_event_constraints[] = {
  395. INTEL_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
  396. INTEL_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
  397. INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
  398. EVENT_CONSTRAINT_END
  399. };
  400. struct event_constraint intel_nehalem_pebs_event_constraints[] = {
  401. INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
  402. INTEL_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
  403. INTEL_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
  404. INTEL_EVENT_CONSTRAINT(0xc0, 0xf), /* INST_RETIRED.ANY */
  405. INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
  406. INTEL_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  407. INTEL_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
  408. INTEL_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
  409. INTEL_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
  410. INTEL_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
  411. INTEL_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
  412. EVENT_CONSTRAINT_END
  413. };
  414. struct event_constraint intel_westmere_pebs_event_constraints[] = {
  415. INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
  416. INTEL_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
  417. INTEL_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
  418. INTEL_EVENT_CONSTRAINT(0xc0, 0xf), /* INSTR_RETIRED.* */
  419. INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
  420. INTEL_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  421. INTEL_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */
  422. INTEL_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
  423. INTEL_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
  424. INTEL_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
  425. INTEL_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
  426. EVENT_CONSTRAINT_END
  427. };
  428. struct event_constraint intel_snb_pebs_event_constraints[] = {
  429. INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
  430. INTEL_UEVENT_CONSTRAINT(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
  431. INTEL_UEVENT_CONSTRAINT(0x02c2, 0xf), /* UOPS_RETIRED.RETIRE_SLOTS */
  432. INTEL_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  433. INTEL_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */
  434. INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
  435. INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
  436. INTEL_EVENT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
  437. INTEL_EVENT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  438. INTEL_EVENT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
  439. INTEL_UEVENT_CONSTRAINT(0x02d4, 0xf), /* MEM_LOAD_UOPS_MISC_RETIRED.LLC_MISS */
  440. EVENT_CONSTRAINT_END
  441. };
  442. struct event_constraint intel_ivb_pebs_event_constraints[] = {
  443. INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
  444. INTEL_UEVENT_CONSTRAINT(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
  445. INTEL_UEVENT_CONSTRAINT(0x02c2, 0xf), /* UOPS_RETIRED.RETIRE_SLOTS */
  446. INTEL_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  447. INTEL_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */
  448. INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
  449. INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
  450. INTEL_EVENT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
  451. INTEL_EVENT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  452. INTEL_EVENT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
  453. INTEL_EVENT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
  454. EVENT_CONSTRAINT_END
  455. };
  456. struct event_constraint intel_hsw_pebs_event_constraints[] = {
  457. INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
  458. INTEL_UEVENT_CONSTRAINT(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
  459. INTEL_UEVENT_CONSTRAINT(0x02c2, 0xf), /* UOPS_RETIRED.RETIRE_SLOTS */
  460. INTEL_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  461. INTEL_UEVENT_CONSTRAINT(0x01c5, 0xf), /* BR_MISP_RETIRED.CONDITIONAL */
  462. INTEL_UEVENT_CONSTRAINT(0x04c5, 0xf), /* BR_MISP_RETIRED.ALL_BRANCHES */
  463. INTEL_UEVENT_CONSTRAINT(0x20c5, 0xf), /* BR_MISP_RETIRED.NEAR_TAKEN */
  464. INTEL_EVENT_CONSTRAINT(0xcd, 0x8), /* MEM_TRANS_RETIRED.* */
  465. /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
  466. INTEL_UEVENT_CONSTRAINT(0x11d0, 0xf),
  467. /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
  468. INTEL_UEVENT_CONSTRAINT(0x12d0, 0xf),
  469. INTEL_UEVENT_CONSTRAINT(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
  470. INTEL_UEVENT_CONSTRAINT(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
  471. /* MEM_UOPS_RETIRED.SPLIT_STORES */
  472. INTEL_UEVENT_CONSTRAINT(0x42d0, 0xf),
  473. INTEL_UEVENT_CONSTRAINT(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
  474. INTEL_UEVENT_CONSTRAINT(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
  475. INTEL_UEVENT_CONSTRAINT(0x01d1, 0xf), /* MEM_LOAD_UOPS_RETIRED.L1_HIT */
  476. INTEL_UEVENT_CONSTRAINT(0x02d1, 0xf), /* MEM_LOAD_UOPS_RETIRED.L2_HIT */
  477. INTEL_UEVENT_CONSTRAINT(0x04d1, 0xf), /* MEM_LOAD_UOPS_RETIRED.L3_HIT */
  478. /* MEM_LOAD_UOPS_RETIRED.HIT_LFB */
  479. INTEL_UEVENT_CONSTRAINT(0x40d1, 0xf),
  480. /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.XSNP_MISS */
  481. INTEL_UEVENT_CONSTRAINT(0x01d2, 0xf),
  482. /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.XSNP_HIT */
  483. INTEL_UEVENT_CONSTRAINT(0x02d2, 0xf),
  484. /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.LOCAL_DRAM */
  485. INTEL_UEVENT_CONSTRAINT(0x01d3, 0xf),
  486. INTEL_UEVENT_CONSTRAINT(0x04c8, 0xf), /* HLE_RETIRED.Abort */
  487. INTEL_UEVENT_CONSTRAINT(0x04c9, 0xf), /* RTM_RETIRED.Abort */
  488. EVENT_CONSTRAINT_END
  489. };
  490. struct event_constraint *intel_pebs_constraints(struct perf_event *event)
  491. {
  492. struct event_constraint *c;
  493. if (!event->attr.precise_ip)
  494. return NULL;
  495. if (x86_pmu.pebs_constraints) {
  496. for_each_event_constraint(c, x86_pmu.pebs_constraints) {
  497. if ((event->hw.config & c->cmask) == c->code) {
  498. event->hw.flags |= c->flags;
  499. return c;
  500. }
  501. }
  502. }
  503. return &emptyconstraint;
  504. }
  505. void intel_pmu_pebs_enable(struct perf_event *event)
  506. {
  507. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  508. struct hw_perf_event *hwc = &event->hw;
  509. hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;
  510. cpuc->pebs_enabled |= 1ULL << hwc->idx;
  511. if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
  512. cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32);
  513. else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
  514. cpuc->pebs_enabled |= 1ULL << 63;
  515. }
  516. void intel_pmu_pebs_disable(struct perf_event *event)
  517. {
  518. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  519. struct hw_perf_event *hwc = &event->hw;
  520. cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
  521. if (cpuc->enabled)
  522. wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
  523. hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
  524. }
  525. void intel_pmu_pebs_enable_all(void)
  526. {
  527. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  528. if (cpuc->pebs_enabled)
  529. wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
  530. }
  531. void intel_pmu_pebs_disable_all(void)
  532. {
  533. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  534. if (cpuc->pebs_enabled)
  535. wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
  536. }
  537. static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
  538. {
  539. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  540. unsigned long from = cpuc->lbr_entries[0].from;
  541. unsigned long old_to, to = cpuc->lbr_entries[0].to;
  542. unsigned long ip = regs->ip;
  543. int is_64bit = 0;
  544. /*
  545. * We don't need to fixup if the PEBS assist is fault like
  546. */
  547. if (!x86_pmu.intel_cap.pebs_trap)
  548. return 1;
  549. /*
  550. * No LBR entry, no basic block, no rewinding
  551. */
  552. if (!cpuc->lbr_stack.nr || !from || !to)
  553. return 0;
  554. /*
  555. * Basic blocks should never cross user/kernel boundaries
  556. */
  557. if (kernel_ip(ip) != kernel_ip(to))
  558. return 0;
  559. /*
  560. * unsigned math, either ip is before the start (impossible) or
  561. * the basic block is larger than 1 page (sanity)
  562. */
  563. if ((ip - to) > PAGE_SIZE)
  564. return 0;
  565. /*
  566. * We sampled a branch insn, rewind using the LBR stack
  567. */
  568. if (ip == to) {
  569. set_linear_ip(regs, from);
  570. return 1;
  571. }
  572. do {
  573. struct insn insn;
  574. u8 buf[MAX_INSN_SIZE];
  575. void *kaddr;
  576. old_to = to;
  577. if (!kernel_ip(ip)) {
  578. int bytes, size = MAX_INSN_SIZE;
  579. bytes = copy_from_user_nmi(buf, (void __user *)to, size);
  580. if (bytes != size)
  581. return 0;
  582. kaddr = buf;
  583. } else
  584. kaddr = (void *)to;
  585. #ifdef CONFIG_X86_64
  586. is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
  587. #endif
  588. insn_init(&insn, kaddr, is_64bit);
  589. insn_get_length(&insn);
  590. to += insn.length;
  591. } while (to < ip);
  592. if (to == ip) {
  593. set_linear_ip(regs, old_to);
  594. return 1;
  595. }
  596. /*
  597. * Even though we decoded the basic block, the instruction stream
  598. * never matched the given IP, either the TO or the IP got corrupted.
  599. */
  600. return 0;
  601. }
  602. static void __intel_pmu_pebs_event(struct perf_event *event,
  603. struct pt_regs *iregs, void *__pebs)
  604. {
  605. /*
  606. * We cast to pebs_record_nhm to get the load latency data
  607. * if extra_reg MSR_PEBS_LD_LAT_THRESHOLD used
  608. */
  609. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  610. struct pebs_record_nhm *pebs = __pebs;
  611. struct pebs_record_hsw *pebs_hsw = __pebs;
  612. struct perf_sample_data data;
  613. struct pt_regs regs;
  614. u64 sample_type;
  615. int fll, fst;
  616. if (!intel_pmu_save_and_restart(event))
  617. return;
  618. fll = event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT;
  619. fst = event->hw.flags & PERF_X86_EVENT_PEBS_ST;
  620. perf_sample_data_init(&data, 0, event->hw.last_period);
  621. data.period = event->hw.last_period;
  622. sample_type = event->attr.sample_type;
  623. /*
  624. * if PEBS-LL or PreciseStore
  625. */
  626. if (fll || fst) {
  627. if (sample_type & PERF_SAMPLE_ADDR)
  628. data.addr = pebs->dla;
  629. /*
  630. * Use latency for weight (only avail with PEBS-LL)
  631. */
  632. if (fll && (sample_type & PERF_SAMPLE_WEIGHT))
  633. data.weight = pebs->lat;
  634. /*
  635. * data.data_src encodes the data source
  636. */
  637. if (sample_type & PERF_SAMPLE_DATA_SRC) {
  638. if (fll)
  639. data.data_src.val = load_latency_data(pebs->dse);
  640. else
  641. data.data_src.val = precise_store_data(pebs->dse);
  642. }
  643. }
  644. /*
  645. * We use the interrupt regs as a base because the PEBS record
  646. * does not contain a full regs set, specifically it seems to
  647. * lack segment descriptors, which get used by things like
  648. * user_mode().
  649. *
  650. * In the simple case fix up only the IP and BP,SP regs, for
  651. * PERF_SAMPLE_IP and PERF_SAMPLE_CALLCHAIN to function properly.
  652. * A possible PERF_SAMPLE_REGS will have to transfer all regs.
  653. */
  654. regs = *iregs;
  655. regs.flags = pebs->flags;
  656. set_linear_ip(&regs, pebs->ip);
  657. regs.bp = pebs->bp;
  658. regs.sp = pebs->sp;
  659. if (event->attr.precise_ip > 1 && x86_pmu.intel_cap.pebs_format >= 2) {
  660. regs.ip = pebs_hsw->real_ip;
  661. regs.flags |= PERF_EFLAGS_EXACT;
  662. } else if (event->attr.precise_ip > 1 && intel_pmu_pebs_fixup_ip(&regs))
  663. regs.flags |= PERF_EFLAGS_EXACT;
  664. else
  665. regs.flags &= ~PERF_EFLAGS_EXACT;
  666. if (has_branch_stack(event))
  667. data.br_stack = &cpuc->lbr_stack;
  668. if (perf_event_overflow(event, &data, &regs))
  669. x86_pmu_stop(event, 0);
  670. }
  671. static void intel_pmu_drain_pebs_core(struct pt_regs *iregs)
  672. {
  673. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  674. struct debug_store *ds = cpuc->ds;
  675. struct perf_event *event = cpuc->events[0]; /* PMC0 only */
  676. struct pebs_record_core *at, *top;
  677. int n;
  678. if (!x86_pmu.pebs_active)
  679. return;
  680. at = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base;
  681. top = (struct pebs_record_core *)(unsigned long)ds->pebs_index;
  682. /*
  683. * Whatever else happens, drain the thing
  684. */
  685. ds->pebs_index = ds->pebs_buffer_base;
  686. if (!test_bit(0, cpuc->active_mask))
  687. return;
  688. WARN_ON_ONCE(!event);
  689. if (!event->attr.precise_ip)
  690. return;
  691. n = top - at;
  692. if (n <= 0)
  693. return;
  694. /*
  695. * Should not happen, we program the threshold at 1 and do not
  696. * set a reset value.
  697. */
  698. WARN_ONCE(n > 1, "bad leftover pebs %d\n", n);
  699. at += n - 1;
  700. __intel_pmu_pebs_event(event, iregs, at);
  701. }
  702. static void __intel_pmu_drain_pebs_nhm(struct pt_regs *iregs, void *at,
  703. void *top)
  704. {
  705. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  706. struct debug_store *ds = cpuc->ds;
  707. struct perf_event *event = NULL;
  708. u64 status = 0;
  709. int bit;
  710. ds->pebs_index = ds->pebs_buffer_base;
  711. for (; at < top; at += x86_pmu.pebs_record_size) {
  712. struct pebs_record_nhm *p = at;
  713. for_each_set_bit(bit, (unsigned long *)&p->status,
  714. x86_pmu.max_pebs_events) {
  715. event = cpuc->events[bit];
  716. if (!test_bit(bit, cpuc->active_mask))
  717. continue;
  718. WARN_ON_ONCE(!event);
  719. if (!event->attr.precise_ip)
  720. continue;
  721. if (__test_and_set_bit(bit, (unsigned long *)&status))
  722. continue;
  723. break;
  724. }
  725. if (!event || bit >= x86_pmu.max_pebs_events)
  726. continue;
  727. __intel_pmu_pebs_event(event, iregs, at);
  728. }
  729. }
  730. static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
  731. {
  732. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  733. struct debug_store *ds = cpuc->ds;
  734. struct pebs_record_nhm *at, *top;
  735. int n;
  736. if (!x86_pmu.pebs_active)
  737. return;
  738. at = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
  739. top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
  740. ds->pebs_index = ds->pebs_buffer_base;
  741. n = top - at;
  742. if (n <= 0)
  743. return;
  744. /*
  745. * Should not happen, we program the threshold at 1 and do not
  746. * set a reset value.
  747. */
  748. WARN_ONCE(n > x86_pmu.max_pebs_events,
  749. "Unexpected number of pebs records %d\n", n);
  750. return __intel_pmu_drain_pebs_nhm(iregs, at, top);
  751. }
  752. static void intel_pmu_drain_pebs_hsw(struct pt_regs *iregs)
  753. {
  754. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  755. struct debug_store *ds = cpuc->ds;
  756. struct pebs_record_hsw *at, *top;
  757. int n;
  758. if (!x86_pmu.pebs_active)
  759. return;
  760. at = (struct pebs_record_hsw *)(unsigned long)ds->pebs_buffer_base;
  761. top = (struct pebs_record_hsw *)(unsigned long)ds->pebs_index;
  762. n = top - at;
  763. if (n <= 0)
  764. return;
  765. /*
  766. * Should not happen, we program the threshold at 1 and do not
  767. * set a reset value.
  768. */
  769. WARN_ONCE(n > x86_pmu.max_pebs_events,
  770. "Unexpected number of pebs records %d\n", n);
  771. return __intel_pmu_drain_pebs_nhm(iregs, at, top);
  772. }
  773. /*
  774. * BTS, PEBS probe and setup
  775. */
  776. void intel_ds_init(void)
  777. {
  778. /*
  779. * No support for 32bit formats
  780. */
  781. if (!boot_cpu_has(X86_FEATURE_DTES64))
  782. return;
  783. x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS);
  784. x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS);
  785. if (x86_pmu.pebs) {
  786. char pebs_type = x86_pmu.intel_cap.pebs_trap ? '+' : '-';
  787. int format = x86_pmu.intel_cap.pebs_format;
  788. switch (format) {
  789. case 0:
  790. printk(KERN_CONT "PEBS fmt0%c, ", pebs_type);
  791. x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
  792. x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
  793. break;
  794. case 1:
  795. printk(KERN_CONT "PEBS fmt1%c, ", pebs_type);
  796. x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
  797. x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
  798. break;
  799. case 2:
  800. pr_cont("PEBS fmt2%c, ", pebs_type);
  801. x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw);
  802. x86_pmu.drain_pebs = intel_pmu_drain_pebs_hsw;
  803. break;
  804. default:
  805. printk(KERN_CONT "no PEBS fmt%d%c, ", format, pebs_type);
  806. x86_pmu.pebs = 0;
  807. }
  808. }
  809. }
  810. void perf_restore_debug_store(void)
  811. {
  812. struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
  813. if (!x86_pmu.bts && !x86_pmu.pebs)
  814. return;
  815. wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds);
  816. }