perf_event_intel_ds.c 29 KB

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