perf_event_intel_ds.c 27 KB

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