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