]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - arch/x86/events/intel/ds.c
perf/x86: Support constraint ranges
[mirror_ubuntu-focal-kernel.git] / arch / x86 / events / intel / ds.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
de0428a7
KW
2#include <linux/bitops.h>
3#include <linux/types.h>
4#include <linux/slab.h>
ca037701 5
c1961a46 6#include <asm/cpu_entry_area.h>
de0428a7 7#include <asm/perf_event.h>
42f3bdc5 8#include <asm/tlbflush.h>
3e702ff6 9#include <asm/insn.h>
de0428a7 10
27f6d22b 11#include "../perf_event.h"
ca037701 12
10043e02
TG
13/* Waste a full page so it can be mapped into the cpu_entry_area */
14DEFINE_PER_CPU_PAGE_ALIGNED(struct debug_store, cpu_debug_store);
15
ca037701
PZ
16/* The size of a BTS record in bytes: */
17#define BTS_RECORD_SIZE 24
18
9536c8d2 19#define PEBS_FIXUP_SIZE PAGE_SIZE
ca037701
PZ
20
21/*
22 * pebs_record_32 for p4 and core not supported
23
24struct pebs_record_32 {
25 u32 flags, ip;
26 u32 ax, bc, cx, dx;
27 u32 si, di, bp, sp;
28};
29
30 */
31
f20093ee
SE
32union intel_x86_pebs_dse {
33 u64 val;
34 struct {
35 unsigned int ld_dse:4;
36 unsigned int ld_stlb_miss:1;
37 unsigned int ld_locked:1;
38 unsigned int ld_reserved:26;
39 };
40 struct {
41 unsigned int st_l1d_hit:1;
42 unsigned int st_reserved1:3;
43 unsigned int st_stlb_miss:1;
44 unsigned int st_locked:1;
45 unsigned int st_reserved2:26;
46 };
47};
48
49
50/*
51 * Map PEBS Load Latency Data Source encodings to generic
52 * memory data source information
53 */
54#define P(a, b) PERF_MEM_S(a, b)
55#define OP_LH (P(OP, LOAD) | P(LVL, HIT))
6ae5fa61
AK
56#define LEVEL(x) P(LVLNUM, x)
57#define REM P(REMOTE, REMOTE)
f20093ee
SE
58#define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS))
59
e17dc653
AK
60/* Version for Sandy Bridge and later */
61static u64 pebs_data_source[] = {
6ae5fa61
AK
62 P(OP, LOAD) | P(LVL, MISS) | LEVEL(L3) | P(SNOOP, NA),/* 0x00:ukn L3 */
63 OP_LH | P(LVL, L1) | LEVEL(L1) | P(SNOOP, NONE), /* 0x01: L1 local */
64 OP_LH | P(LVL, LFB) | LEVEL(LFB) | P(SNOOP, NONE), /* 0x02: LFB hit */
65 OP_LH | P(LVL, L2) | LEVEL(L2) | P(SNOOP, NONE), /* 0x03: L2 hit */
66 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, NONE), /* 0x04: L3 hit */
67 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, MISS), /* 0x05: L3 hit, snoop miss */
68 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT), /* 0x06: L3 hit, snoop hit */
69 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM), /* 0x07: L3 hit, snoop hitm */
70 OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HIT), /* 0x08: L3 miss snoop hit */
71 OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HITM), /* 0x09: L3 miss snoop hitm*/
72 OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | P(SNOOP, HIT), /* 0x0a: L3 miss, shared */
73 OP_LH | P(LVL, REM_RAM1) | REM | LEVEL(L3) | P(SNOOP, HIT), /* 0x0b: L3 miss, shared */
74 OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | SNOOP_NONE_MISS, /* 0x0c: L3 miss, excl */
75 OP_LH | P(LVL, REM_RAM1) | LEVEL(RAM) | REM | SNOOP_NONE_MISS, /* 0x0d: L3 miss, excl */
76 OP_LH | P(LVL, IO) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0e: I/O */
77 OP_LH | P(LVL, UNC) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0f: uncached */
f20093ee
SE
78};
79
e17dc653
AK
80/* Patch up minor differences in the bits */
81void __init intel_pmu_pebs_data_source_nhm(void)
82{
6ae5fa61
AK
83 pebs_data_source[0x05] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT);
84 pebs_data_source[0x06] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
85 pebs_data_source[0x07] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
86}
87
88void __init intel_pmu_pebs_data_source_skl(bool pmem)
89{
90 u64 pmem_or_l4 = pmem ? LEVEL(PMEM) : LEVEL(L4);
91
92 pebs_data_source[0x08] = OP_LH | pmem_or_l4 | P(SNOOP, HIT);
93 pebs_data_source[0x09] = OP_LH | pmem_or_l4 | REM | P(SNOOP, HIT);
94 pebs_data_source[0x0b] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, NONE);
95 pebs_data_source[0x0c] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOPX, FWD);
96 pebs_data_source[0x0d] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOP, HITM);
e17dc653
AK
97}
98
9ad64c0f
SE
99static u64 precise_store_data(u64 status)
100{
101 union intel_x86_pebs_dse dse;
102 u64 val = P(OP, STORE) | P(SNOOP, NA) | P(LVL, L1) | P(TLB, L2);
103
104 dse.val = status;
105
106 /*
107 * bit 4: TLB access
108 * 1 = stored missed 2nd level TLB
109 *
110 * so it either hit the walker or the OS
111 * otherwise hit 2nd level TLB
112 */
113 if (dse.st_stlb_miss)
114 val |= P(TLB, MISS);
115 else
116 val |= P(TLB, HIT);
117
118 /*
119 * bit 0: hit L1 data cache
120 * if not set, then all we know is that
121 * it missed L1D
122 */
123 if (dse.st_l1d_hit)
124 val |= P(LVL, HIT);
125 else
126 val |= P(LVL, MISS);
127
128 /*
129 * bit 5: Locked prefix
130 */
131 if (dse.st_locked)
132 val |= P(LOCK, LOCKED);
133
134 return val;
135}
136
c8aab2e0 137static u64 precise_datala_hsw(struct perf_event *event, u64 status)
f9134f36
AK
138{
139 union perf_mem_data_src dse;
140
770eee1f
SE
141 dse.val = PERF_MEM_NA;
142
143 if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW)
144 dse.mem_op = PERF_MEM_OP_STORE;
145 else if (event->hw.flags & PERF_X86_EVENT_PEBS_LD_HSW)
146 dse.mem_op = PERF_MEM_OP_LOAD;
722e76e6
SE
147
148 /*
149 * L1 info only valid for following events:
150 *
151 * MEM_UOPS_RETIRED.STLB_MISS_STORES
152 * MEM_UOPS_RETIRED.LOCK_STORES
153 * MEM_UOPS_RETIRED.SPLIT_STORES
154 * MEM_UOPS_RETIRED.ALL_STORES
155 */
c8aab2e0
SE
156 if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) {
157 if (status & 1)
158 dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_HIT;
159 else
160 dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_MISS;
161 }
f9134f36
AK
162 return dse.val;
163}
164
f20093ee
SE
165static u64 load_latency_data(u64 status)
166{
167 union intel_x86_pebs_dse dse;
168 u64 val;
f20093ee
SE
169
170 dse.val = status;
171
172 /*
173 * use the mapping table for bit 0-3
174 */
175 val = pebs_data_source[dse.ld_dse];
176
177 /*
178 * Nehalem models do not support TLB, Lock infos
179 */
95298355 180 if (x86_pmu.pebs_no_tlb) {
f20093ee
SE
181 val |= P(TLB, NA) | P(LOCK, NA);
182 return val;
183 }
184 /*
185 * bit 4: TLB access
186 * 0 = did not miss 2nd level TLB
187 * 1 = missed 2nd level TLB
188 */
189 if (dse.ld_stlb_miss)
190 val |= P(TLB, MISS) | P(TLB, L2);
191 else
192 val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2);
193
194 /*
195 * bit 5: locked prefix
196 */
197 if (dse.ld_locked)
198 val |= P(LOCK, LOCKED);
199
200 return val;
201}
202
ca037701
PZ
203struct pebs_record_core {
204 u64 flags, ip;
205 u64 ax, bx, cx, dx;
206 u64 si, di, bp, sp;
207 u64 r8, r9, r10, r11;
208 u64 r12, r13, r14, r15;
209};
210
211struct pebs_record_nhm {
212 u64 flags, ip;
213 u64 ax, bx, cx, dx;
214 u64 si, di, bp, sp;
215 u64 r8, r9, r10, r11;
216 u64 r12, r13, r14, r15;
217 u64 status, dla, dse, lat;
218};
219
130768b8
AK
220/*
221 * Same as pebs_record_nhm, with two additional fields.
222 */
223struct pebs_record_hsw {
748e86aa
AK
224 u64 flags, ip;
225 u64 ax, bx, cx, dx;
226 u64 si, di, bp, sp;
227 u64 r8, r9, r10, r11;
228 u64 r12, r13, r14, r15;
229 u64 status, dla, dse, lat;
d2beea4a 230 u64 real_ip, tsx_tuning;
748e86aa
AK
231};
232
233union hsw_tsx_tuning {
234 struct {
235 u32 cycles_last_block : 32,
236 hle_abort : 1,
237 rtm_abort : 1,
238 instruction_abort : 1,
239 non_instruction_abort : 1,
240 retry : 1,
241 data_conflict : 1,
242 capacity_writes : 1,
243 capacity_reads : 1;
244 };
245 u64 value;
130768b8
AK
246};
247
a405bad5
AK
248#define PEBS_HSW_TSX_FLAGS 0xff00000000ULL
249
2f7ebf2e
AK
250/* Same as HSW, plus TSC */
251
252struct pebs_record_skl {
253 u64 flags, ip;
254 u64 ax, bx, cx, dx;
255 u64 si, di, bp, sp;
256 u64 r8, r9, r10, r11;
257 u64 r12, r13, r14, r15;
258 u64 status, dla, dse, lat;
259 u64 real_ip, tsx_tuning;
260 u64 tsc;
261};
262
de0428a7 263void init_debug_store_on_cpu(int cpu)
ca037701
PZ
264{
265 struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
266
267 if (!ds)
268 return;
269
270 wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA,
271 (u32)((u64)(unsigned long)ds),
272 (u32)((u64)(unsigned long)ds >> 32));
273}
274
de0428a7 275void fini_debug_store_on_cpu(int cpu)
ca037701
PZ
276{
277 if (!per_cpu(cpu_hw_events, cpu).ds)
278 return;
279
280 wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0);
281}
282
9536c8d2
PZ
283static DEFINE_PER_CPU(void *, insn_buffer);
284
c1961a46 285static void ds_update_cea(void *cea, void *addr, size_t size, pgprot_t prot)
5ee25c87 286{
42f3bdc5 287 unsigned long start = (unsigned long)cea;
c1961a46
HD
288 phys_addr_t pa;
289 size_t msz = 0;
290
291 pa = virt_to_phys(addr);
42f3bdc5
PZ
292
293 preempt_disable();
c1961a46
HD
294 for (; msz < size; msz += PAGE_SIZE, pa += PAGE_SIZE, cea += PAGE_SIZE)
295 cea_set_pte(cea, pa, prot);
42f3bdc5
PZ
296
297 /*
298 * This is a cross-CPU update of the cpu_entry_area, we must shoot down
299 * all TLB entries for it.
300 */
301 flush_tlb_kernel_range(start, start + size);
302 preempt_enable();
c1961a46
HD
303}
304
305static void ds_clear_cea(void *cea, size_t size)
306{
42f3bdc5 307 unsigned long start = (unsigned long)cea;
c1961a46
HD
308 size_t msz = 0;
309
42f3bdc5 310 preempt_disable();
c1961a46
HD
311 for (; msz < size; msz += PAGE_SIZE, cea += PAGE_SIZE)
312 cea_set_pte(cea, 0, PAGE_NONE);
42f3bdc5
PZ
313
314 flush_tlb_kernel_range(start, start + size);
315 preempt_enable();
c1961a46
HD
316}
317
318static void *dsalloc_pages(size_t size, gfp_t flags, int cpu)
319{
320 unsigned int order = get_order(size);
96681fc3 321 int node = cpu_to_node(cpu);
c1961a46
HD
322 struct page *page;
323
324 page = __alloc_pages_node(node, flags | __GFP_ZERO, order);
325 return page ? page_address(page) : NULL;
326}
327
328static void dsfree_pages(const void *buffer, size_t size)
329{
330 if (buffer)
331 free_pages((unsigned long)buffer, get_order(size));
332}
333
334static int alloc_pebs_buffer(int cpu)
335{
336 struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
337 struct debug_store *ds = hwev->ds;
338 size_t bsiz = x86_pmu.pebs_buffer_size;
339 int max, node = cpu_to_node(cpu);
340 void *buffer, *ibuffer, *cea;
5ee25c87
PZ
341
342 if (!x86_pmu.pebs)
343 return 0;
344
c1961a46 345 buffer = dsalloc_pages(bsiz, GFP_KERNEL, cpu);
5ee25c87
PZ
346 if (unlikely(!buffer))
347 return -ENOMEM;
348
9536c8d2
PZ
349 /*
350 * HSW+ already provides us the eventing ip; no need to allocate this
351 * buffer then.
352 */
353 if (x86_pmu.intel_cap.pebs_format < 2) {
354 ibuffer = kzalloc_node(PEBS_FIXUP_SIZE, GFP_KERNEL, node);
355 if (!ibuffer) {
c1961a46 356 dsfree_pages(buffer, bsiz);
9536c8d2
PZ
357 return -ENOMEM;
358 }
359 per_cpu(insn_buffer, cpu) = ibuffer;
360 }
c1961a46
HD
361 hwev->ds_pebs_vaddr = buffer;
362 /* Update the cpu entry area mapping */
363 cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer;
364 ds->pebs_buffer_base = (unsigned long) cea;
365 ds_update_cea(cea, buffer, bsiz, PAGE_KERNEL);
5ee25c87 366 ds->pebs_index = ds->pebs_buffer_base;
c1961a46
HD
367 max = x86_pmu.pebs_record_size * (bsiz / x86_pmu.pebs_record_size);
368 ds->pebs_absolute_maximum = ds->pebs_buffer_base + max;
5ee25c87
PZ
369 return 0;
370}
371
b39f88ac
PZ
372static void release_pebs_buffer(int cpu)
373{
c1961a46 374 struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
c1961a46 375 void *cea;
b39f88ac 376
efe951d3 377 if (!x86_pmu.pebs)
b39f88ac
PZ
378 return;
379
9536c8d2
PZ
380 kfree(per_cpu(insn_buffer, cpu));
381 per_cpu(insn_buffer, cpu) = NULL;
382
c1961a46
HD
383 /* Clear the fixmap */
384 cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer;
385 ds_clear_cea(cea, x86_pmu.pebs_buffer_size);
c1961a46
HD
386 dsfree_pages(hwev->ds_pebs_vaddr, x86_pmu.pebs_buffer_size);
387 hwev->ds_pebs_vaddr = NULL;
b39f88ac
PZ
388}
389
5ee25c87
PZ
390static int alloc_bts_buffer(int cpu)
391{
c1961a46
HD
392 struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
393 struct debug_store *ds = hwev->ds;
394 void *buffer, *cea;
395 int max;
5ee25c87
PZ
396
397 if (!x86_pmu.bts)
398 return 0;
399
c1961a46 400 buffer = dsalloc_pages(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, cpu);
44851541
DR
401 if (unlikely(!buffer)) {
402 WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__);
5ee25c87 403 return -ENOMEM;
44851541 404 }
c1961a46
HD
405 hwev->ds_bts_vaddr = buffer;
406 /* Update the fixmap */
407 cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer;
408 ds->bts_buffer_base = (unsigned long) cea;
409 ds_update_cea(cea, buffer, BTS_BUFFER_SIZE, PAGE_KERNEL);
5ee25c87 410 ds->bts_index = ds->bts_buffer_base;
2c991e40
HD
411 max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE;
412 ds->bts_absolute_maximum = ds->bts_buffer_base +
413 max * BTS_RECORD_SIZE;
414 ds->bts_interrupt_threshold = ds->bts_absolute_maximum -
415 (max / 16) * BTS_RECORD_SIZE;
5ee25c87
PZ
416 return 0;
417}
418
b39f88ac
PZ
419static void release_bts_buffer(int cpu)
420{
c1961a46 421 struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
c1961a46 422 void *cea;
b39f88ac 423
efe951d3 424 if (!x86_pmu.bts)
b39f88ac
PZ
425 return;
426
c1961a46
HD
427 /* Clear the fixmap */
428 cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer;
429 ds_clear_cea(cea, BTS_BUFFER_SIZE);
c1961a46
HD
430 dsfree_pages(hwev->ds_bts_vaddr, BTS_BUFFER_SIZE);
431 hwev->ds_bts_vaddr = NULL;
b39f88ac
PZ
432}
433
65af94ba
PZ
434static int alloc_ds_buffer(int cpu)
435{
c1961a46 436 struct debug_store *ds = &get_cpu_entry_area(cpu)->cpu_debug_store;
65af94ba 437
c1961a46 438 memset(ds, 0, sizeof(*ds));
65af94ba 439 per_cpu(cpu_hw_events, cpu).ds = ds;
65af94ba
PZ
440 return 0;
441}
442
443static void release_ds_buffer(int cpu)
444{
65af94ba 445 per_cpu(cpu_hw_events, cpu).ds = NULL;
65af94ba
PZ
446}
447
de0428a7 448void release_ds_buffers(void)
ca037701
PZ
449{
450 int cpu;
451
452 if (!x86_pmu.bts && !x86_pmu.pebs)
453 return;
454
efe951d3
PZ
455 for_each_possible_cpu(cpu)
456 release_ds_buffer(cpu);
457
458 for_each_possible_cpu(cpu) {
459 /*
460 * Again, ignore errors from offline CPUs, they will no longer
461 * observe cpu_hw_events.ds and not program the DS_AREA when
462 * they come up.
463 */
ca037701 464 fini_debug_store_on_cpu(cpu);
efe951d3 465 }
ca037701
PZ
466
467 for_each_possible_cpu(cpu) {
b39f88ac
PZ
468 release_pebs_buffer(cpu);
469 release_bts_buffer(cpu);
ca037701 470 }
ca037701
PZ
471}
472
de0428a7 473void reserve_ds_buffers(void)
ca037701 474{
6809b6ea
PZ
475 int bts_err = 0, pebs_err = 0;
476 int cpu;
477
478 x86_pmu.bts_active = 0;
479 x86_pmu.pebs_active = 0;
ca037701
PZ
480
481 if (!x86_pmu.bts && !x86_pmu.pebs)
f80c9e30 482 return;
ca037701 483
6809b6ea
PZ
484 if (!x86_pmu.bts)
485 bts_err = 1;
486
487 if (!x86_pmu.pebs)
488 pebs_err = 1;
489
ca037701 490 for_each_possible_cpu(cpu) {
6809b6ea
PZ
491 if (alloc_ds_buffer(cpu)) {
492 bts_err = 1;
493 pebs_err = 1;
494 }
ca037701 495
6809b6ea
PZ
496 if (!bts_err && alloc_bts_buffer(cpu))
497 bts_err = 1;
498
499 if (!pebs_err && alloc_pebs_buffer(cpu))
500 pebs_err = 1;
5ee25c87 501
6809b6ea 502 if (bts_err && pebs_err)
5ee25c87 503 break;
6809b6ea
PZ
504 }
505
506 if (bts_err) {
507 for_each_possible_cpu(cpu)
508 release_bts_buffer(cpu);
509 }
ca037701 510
6809b6ea
PZ
511 if (pebs_err) {
512 for_each_possible_cpu(cpu)
513 release_pebs_buffer(cpu);
ca037701
PZ
514 }
515
6809b6ea
PZ
516 if (bts_err && pebs_err) {
517 for_each_possible_cpu(cpu)
518 release_ds_buffer(cpu);
519 } else {
520 if (x86_pmu.bts && !bts_err)
521 x86_pmu.bts_active = 1;
522
523 if (x86_pmu.pebs && !pebs_err)
524 x86_pmu.pebs_active = 1;
525
efe951d3
PZ
526 for_each_possible_cpu(cpu) {
527 /*
528 * Ignores wrmsr_on_cpu() errors for offline CPUs they
529 * will get this call through intel_pmu_cpu_starting().
530 */
ca037701 531 init_debug_store_on_cpu(cpu);
efe951d3 532 }
ca037701 533 }
ca037701
PZ
534}
535
536/*
537 * BTS
538 */
539
de0428a7 540struct event_constraint bts_constraint =
15c7ad51 541 EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0);
ca037701 542
de0428a7 543void intel_pmu_enable_bts(u64 config)
ca037701
PZ
544{
545 unsigned long debugctlmsr;
546
547 debugctlmsr = get_debugctlmsr();
548
7c5ecaf7
PZ
549 debugctlmsr |= DEBUGCTLMSR_TR;
550 debugctlmsr |= DEBUGCTLMSR_BTS;
8062382c
AS
551 if (config & ARCH_PERFMON_EVENTSEL_INT)
552 debugctlmsr |= DEBUGCTLMSR_BTINT;
ca037701
PZ
553
554 if (!(config & ARCH_PERFMON_EVENTSEL_OS))
7c5ecaf7 555 debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
ca037701
PZ
556
557 if (!(config & ARCH_PERFMON_EVENTSEL_USR))
7c5ecaf7 558 debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR;
ca037701
PZ
559
560 update_debugctlmsr(debugctlmsr);
561}
562
de0428a7 563void intel_pmu_disable_bts(void)
ca037701 564{
89cbc767 565 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
ca037701
PZ
566 unsigned long debugctlmsr;
567
568 if (!cpuc->ds)
569 return;
570
571 debugctlmsr = get_debugctlmsr();
572
573 debugctlmsr &=
7c5ecaf7
PZ
574 ~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT |
575 DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR);
ca037701
PZ
576
577 update_debugctlmsr(debugctlmsr);
578}
579
de0428a7 580int intel_pmu_drain_bts_buffer(void)
ca037701 581{
89cbc767 582 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
ca037701
PZ
583 struct debug_store *ds = cpuc->ds;
584 struct bts_record {
585 u64 from;
586 u64 to;
587 u64 flags;
588 };
15c7ad51 589 struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
a09d31f4 590 struct bts_record *at, *base, *top;
ca037701
PZ
591 struct perf_output_handle handle;
592 struct perf_event_header header;
593 struct perf_sample_data data;
a09d31f4 594 unsigned long skip = 0;
ca037701
PZ
595 struct pt_regs regs;
596
597 if (!event)
b0b2072d 598 return 0;
ca037701 599
6809b6ea 600 if (!x86_pmu.bts_active)
b0b2072d 601 return 0;
ca037701 602
a09d31f4
AS
603 base = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
604 top = (struct bts_record *)(unsigned long)ds->bts_index;
ca037701 605
a09d31f4 606 if (top <= base)
b0b2072d 607 return 0;
ca037701 608
0e48026a
SE
609 memset(&regs, 0, sizeof(regs));
610
ca037701
PZ
611 ds->bts_index = ds->bts_buffer_base;
612
fd0d000b 613 perf_sample_data_init(&data, 0, event->hw.last_period);
ca037701 614
a09d31f4
AS
615 /*
616 * BTS leaks kernel addresses in branches across the cpl boundary,
617 * such as traps or system calls, so unless the user is asking for
618 * kernel tracing (and right now it's not possible), we'd need to
619 * filter them out. But first we need to count how many of those we
620 * have in the current batch. This is an extra O(n) pass, however,
621 * it's much faster than the other one especially considering that
622 * n <= 2560 (BTS_BUFFER_SIZE / BTS_RECORD_SIZE * 15/16; see the
623 * alloc_bts_buffer()).
624 */
625 for (at = base; at < top; at++) {
626 /*
627 * Note that right now *this* BTS code only works if
628 * attr::exclude_kernel is set, but let's keep this extra
629 * check here in case that changes.
630 */
631 if (event->attr.exclude_kernel &&
632 (kernel_ip(at->from) || kernel_ip(at->to)))
633 skip++;
634 }
635
ca037701
PZ
636 /*
637 * Prepare a generic sample, i.e. fill in the invariant fields.
638 * We will overwrite the from and to address before we output
639 * the sample.
640 */
e8d8a90f 641 rcu_read_lock();
ca037701
PZ
642 perf_prepare_sample(&header, &data, event, &regs);
643
a09d31f4
AS
644 if (perf_output_begin(&handle, event, header.size *
645 (top - base - skip)))
e8d8a90f 646 goto unlock;
ca037701 647
a09d31f4
AS
648 for (at = base; at < top; at++) {
649 /* Filter out any records that contain kernel addresses. */
650 if (event->attr.exclude_kernel &&
651 (kernel_ip(at->from) || kernel_ip(at->to)))
652 continue;
653
ca037701
PZ
654 data.ip = at->from;
655 data.addr = at->to;
656
657 perf_output_sample(&handle, &header, &data, event);
658 }
659
660 perf_output_end(&handle);
661
662 /* There's new data available. */
663 event->hw.interrupts++;
664 event->pending_kill = POLL_IN;
e8d8a90f
PZ
665unlock:
666 rcu_read_unlock();
b0b2072d 667 return 1;
ca037701
PZ
668}
669
9c964efa
YZ
670static inline void intel_pmu_drain_pebs_buffer(void)
671{
672 struct pt_regs regs;
673
674 x86_pmu.drain_pebs(&regs);
675}
676
ca037701
PZ
677/*
678 * PEBS
679 */
de0428a7 680struct event_constraint intel_core2_pebs_event_constraints[] = {
af4bdcf6
AK
681 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
682 INTEL_FLAGS_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
683 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
684 INTEL_FLAGS_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
685 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
517e6341
PZ
686 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
687 INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01),
ca037701
PZ
688 EVENT_CONSTRAINT_END
689};
690
de0428a7 691struct event_constraint intel_atom_pebs_event_constraints[] = {
af4bdcf6
AK
692 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
693 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
694 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
517e6341
PZ
695 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
696 INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01),
673d188b
SE
697 /* Allow all events as PEBS with no flags */
698 INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
17e31629
SE
699 EVENT_CONSTRAINT_END
700};
701
1fa64180 702struct event_constraint intel_slm_pebs_event_constraints[] = {
33636732
KL
703 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
704 INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x1),
86a04461
AK
705 /* Allow all events as PEBS with no flags */
706 INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
1fa64180
YZ
707 EVENT_CONSTRAINT_END
708};
709
8b92c3a7
KL
710struct event_constraint intel_glm_pebs_event_constraints[] = {
711 /* Allow all events as PEBS with no flags */
712 INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
713 EVENT_CONSTRAINT_END
714};
715
de0428a7 716struct event_constraint intel_nehalem_pebs_event_constraints[] = {
f20093ee 717 INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
af4bdcf6
AK
718 INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
719 INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
720 INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INST_RETIRED.ANY */
7d5d02da 721 INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
af4bdcf6
AK
722 INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
723 INTEL_FLAGS_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
724 INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
725 INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
726 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
727 INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
517e6341
PZ
728 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
729 INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
17e31629
SE
730 EVENT_CONSTRAINT_END
731};
732
de0428a7 733struct event_constraint intel_westmere_pebs_event_constraints[] = {
f20093ee 734 INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
af4bdcf6
AK
735 INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
736 INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
737 INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INSTR_RETIRED.* */
7d5d02da 738 INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
af4bdcf6
AK
739 INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
740 INTEL_FLAGS_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */
741 INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
742 INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
743 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
744 INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
517e6341
PZ
745 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
746 INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
ca037701
PZ
747 EVENT_CONSTRAINT_END
748};
749
de0428a7 750struct event_constraint intel_snb_pebs_event_constraints[] = {
0dbc9479 751 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
f20093ee 752 INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
9ad64c0f 753 INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
86a04461
AK
754 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
755 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
b63b4b45
MD
756 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
757 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
758 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
759 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
86a04461
AK
760 /* Allow all events as PEBS with no flags */
761 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
b06b3d49
LM
762 EVENT_CONSTRAINT_END
763};
764
20a36e39 765struct event_constraint intel_ivb_pebs_event_constraints[] = {
0dbc9479 766 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
f20093ee 767 INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
9ad64c0f 768 INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
86a04461
AK
769 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
770 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
72469764
AK
771 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
772 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
b63b4b45
MD
773 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
774 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
775 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
776 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
86a04461
AK
777 /* Allow all events as PEBS with no flags */
778 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
20a36e39
SE
779 EVENT_CONSTRAINT_END
780};
781
3044318f 782struct event_constraint intel_hsw_pebs_event_constraints[] = {
0dbc9479 783 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
86a04461
AK
784 INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */
785 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
786 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
72469764
AK
787 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
788 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
86a04461 789 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
b63b4b45
MD
790 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
791 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
792 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
793 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
794 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
795 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
796 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
797 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
798 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
799 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
9a92e16f
AK
800 /* Allow all events as PEBS with no flags */
801 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
802 EVENT_CONSTRAINT_END
803};
804
b3e62463
SE
805struct event_constraint intel_bdw_pebs_event_constraints[] = {
806 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
807 INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */
808 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
809 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
810 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
811 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
812 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
813 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
814 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
815 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
816 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
817 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
818 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
819 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
820 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
821 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
822 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
823 /* Allow all events as PEBS with no flags */
824 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
825 EVENT_CONSTRAINT_END
826};
827
828
9a92e16f
AK
829struct event_constraint intel_skl_pebs_event_constraints[] = {
830 INTEL_FLAGS_UEVENT_CONSTRAINT(0x1c0, 0x2), /* INST_RETIRED.PREC_DIST */
72469764
AK
831 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
832 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
442f5c74
AK
833 /* INST_RETIRED.TOTAL_CYCLES_PS (inv=1, cmask=16) (cycles:p). */
834 INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
9a92e16f
AK
835 INTEL_PLD_CONSTRAINT(0x1cd, 0xf), /* MEM_TRANS_RETIRED.* */
836 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */
837 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */
838 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */
839 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x22d0, 0xf), /* MEM_INST_RETIRED.LOCK_STORES */
840 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */
841 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */
842 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */
843 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */
844 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_RETIRED.* */
845 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_L3_HIT_RETIRED.* */
846 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_L3_MISS_RETIRED.* */
86a04461
AK
847 /* Allow all events as PEBS with no flags */
848 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
3044318f
AK
849 EVENT_CONSTRAINT_END
850};
851
de0428a7 852struct event_constraint *intel_pebs_constraints(struct perf_event *event)
ca037701
PZ
853{
854 struct event_constraint *c;
855
ab608344 856 if (!event->attr.precise_ip)
ca037701
PZ
857 return NULL;
858
859 if (x86_pmu.pebs_constraints) {
860 for_each_event_constraint(c, x86_pmu.pebs_constraints) {
63b79f6e 861 if (constraint_match(c, event->hw.config)) {
9fac2cf3 862 event->hw.flags |= c->flags;
ca037701 863 return c;
9fac2cf3 864 }
ca037701
PZ
865 }
866 }
867
31962340
KL
868 /*
869 * Extended PEBS support
870 * Makes the PEBS code search the normal constraints.
871 */
872 if (x86_pmu.flags & PMU_FL_PEBS_ALL)
873 return NULL;
874
ca037701
PZ
875 return &emptyconstraint;
876}
877
09e61b4f
PZ
878/*
879 * We need the sched_task callback even for per-cpu events when we use
880 * the large interrupt threshold, such that we can provide PID and TID
881 * to PEBS samples.
882 */
883static inline bool pebs_needs_sched_cb(struct cpu_hw_events *cpuc)
884{
885 return cpuc->n_pebs && (cpuc->n_pebs == cpuc->n_large_pebs);
886}
887
df6c3db8
JO
888void intel_pmu_pebs_sched_task(struct perf_event_context *ctx, bool sched_in)
889{
890 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
891
892 if (!sched_in && pebs_needs_sched_cb(cpuc))
893 intel_pmu_drain_pebs_buffer();
894}
895
09e61b4f
PZ
896static inline void pebs_update_threshold(struct cpu_hw_events *cpuc)
897{
898 struct debug_store *ds = cpuc->ds;
899 u64 threshold;
ec71a398
KL
900 int reserved;
901
902 if (x86_pmu.flags & PMU_FL_PEBS_ALL)
903 reserved = x86_pmu.max_pebs_events + x86_pmu.num_counters_fixed;
904 else
905 reserved = x86_pmu.max_pebs_events;
09e61b4f
PZ
906
907 if (cpuc->n_pebs == cpuc->n_large_pebs) {
908 threshold = ds->pebs_absolute_maximum -
c22497f5 909 reserved * cpuc->pebs_record_size;
09e61b4f 910 } else {
c22497f5 911 threshold = ds->pebs_buffer_base + cpuc->pebs_record_size;
09e61b4f
PZ
912 }
913
914 ds->pebs_interrupt_threshold = threshold;
915}
916
c22497f5
KL
917static void adaptive_pebs_record_size_update(void)
918{
919 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
920 u64 pebs_data_cfg = cpuc->pebs_data_cfg;
921 int sz = sizeof(struct pebs_basic);
922
923 if (pebs_data_cfg & PEBS_DATACFG_MEMINFO)
924 sz += sizeof(struct pebs_meminfo);
925 if (pebs_data_cfg & PEBS_DATACFG_GP)
926 sz += sizeof(struct pebs_gprs);
927 if (pebs_data_cfg & PEBS_DATACFG_XMMS)
928 sz += sizeof(struct pebs_xmm);
929 if (pebs_data_cfg & PEBS_DATACFG_LBRS)
930 sz += x86_pmu.lbr_nr * sizeof(struct pebs_lbr_entry);
931
932 cpuc->pebs_record_size = sz;
933}
934
935#define PERF_PEBS_MEMINFO_TYPE (PERF_SAMPLE_ADDR | PERF_SAMPLE_DATA_SRC | \
936 PERF_SAMPLE_PHYS_ADDR | PERF_SAMPLE_WEIGHT | \
937 PERF_SAMPLE_TRANSACTION)
938
939static u64 pebs_update_adaptive_cfg(struct perf_event *event)
940{
941 struct perf_event_attr *attr = &event->attr;
942 u64 sample_type = attr->sample_type;
943 u64 pebs_data_cfg = 0;
944 bool gprs, tsx_weight;
945
946 if (!(sample_type & ~(PERF_SAMPLE_IP|PERF_SAMPLE_TIME)) &&
947 attr->precise_ip > 1)
948 return pebs_data_cfg;
949
950 if (sample_type & PERF_PEBS_MEMINFO_TYPE)
951 pebs_data_cfg |= PEBS_DATACFG_MEMINFO;
952
953 /*
954 * We need GPRs when:
955 * + user requested them
956 * + precise_ip < 2 for the non event IP
957 * + For RTM TSX weight we need GPRs for the abort code.
958 */
959 gprs = (sample_type & PERF_SAMPLE_REGS_INTR) &&
960 (attr->sample_regs_intr & PEBS_GP_REGS);
961
962 tsx_weight = (sample_type & PERF_SAMPLE_WEIGHT) &&
963 ((attr->config & INTEL_ARCH_EVENT_MASK) ==
964 x86_pmu.rtm_abort_event);
965
966 if (gprs || (attr->precise_ip < 2) || tsx_weight)
967 pebs_data_cfg |= PEBS_DATACFG_GP;
968
969 if ((sample_type & PERF_SAMPLE_REGS_INTR) &&
970 (attr->sample_regs_intr & PEBS_XMM_REGS))
971 pebs_data_cfg |= PEBS_DATACFG_XMMS;
972
973 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
974 /*
975 * For now always log all LBRs. Could configure this
976 * later.
977 */
978 pebs_data_cfg |= PEBS_DATACFG_LBRS |
979 ((x86_pmu.lbr_nr-1) << PEBS_DATACFG_LBR_SHIFT);
980 }
981
982 return pebs_data_cfg;
983}
984
09e61b4f 985static void
c22497f5
KL
986pebs_update_state(bool needed_cb, struct cpu_hw_events *cpuc,
987 struct perf_event *event, bool add)
09e61b4f 988{
c22497f5 989 struct pmu *pmu = event->ctx->pmu;
b6a32f02
JO
990 /*
991 * Make sure we get updated with the first PEBS
992 * event. It will trigger also during removal, but
993 * that does not hurt:
994 */
995 bool update = cpuc->n_pebs == 1;
996
09e61b4f
PZ
997 if (needed_cb != pebs_needs_sched_cb(cpuc)) {
998 if (!needed_cb)
999 perf_sched_cb_inc(pmu);
1000 else
1001 perf_sched_cb_dec(pmu);
1002
b6a32f02 1003 update = true;
09e61b4f 1004 }
b6a32f02 1005
c22497f5
KL
1006 /*
1007 * The PEBS record doesn't shrink on pmu::del(). Doing so would require
1008 * iterating all remaining PEBS events to reconstruct the config.
1009 */
1010 if (x86_pmu.intel_cap.pebs_baseline && add) {
1011 u64 pebs_data_cfg;
1012
1013 /* Clear pebs_data_cfg and pebs_record_size for first PEBS. */
1014 if (cpuc->n_pebs == 1) {
1015 cpuc->pebs_data_cfg = 0;
1016 cpuc->pebs_record_size = sizeof(struct pebs_basic);
1017 }
1018
1019 pebs_data_cfg = pebs_update_adaptive_cfg(event);
1020
1021 /* Update pebs_record_size if new event requires more data. */
1022 if (pebs_data_cfg & ~cpuc->pebs_data_cfg) {
1023 cpuc->pebs_data_cfg |= pebs_data_cfg;
1024 adaptive_pebs_record_size_update();
1025 update = true;
1026 }
1027 }
1028
b6a32f02
JO
1029 if (update)
1030 pebs_update_threshold(cpuc);
09e61b4f
PZ
1031}
1032
68f7082f 1033void intel_pmu_pebs_add(struct perf_event *event)
3569c0d7 1034{
09e61b4f
PZ
1035 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1036 struct hw_perf_event *hwc = &event->hw;
1037 bool needed_cb = pebs_needs_sched_cb(cpuc);
1038
1039 cpuc->n_pebs++;
174afc3e 1040 if (hwc->flags & PERF_X86_EVENT_LARGE_PEBS)
09e61b4f
PZ
1041 cpuc->n_large_pebs++;
1042
c22497f5 1043 pebs_update_state(needed_cb, cpuc, event, true);
3569c0d7
YZ
1044}
1045
de0428a7 1046void intel_pmu_pebs_enable(struct perf_event *event)
ca037701 1047{
89cbc767 1048 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
ef21f683 1049 struct hw_perf_event *hwc = &event->hw;
851559e3 1050 struct debug_store *ds = cpuc->ds;
09e61b4f 1051
ca037701
PZ
1052 hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;
1053
ad0e6cfe 1054 cpuc->pebs_enabled |= 1ULL << hwc->idx;
f20093ee
SE
1055
1056 if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
1057 cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32);
9ad64c0f
SE
1058 else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
1059 cpuc->pebs_enabled |= 1ULL << 63;
851559e3 1060
c22497f5
KL
1061 if (x86_pmu.intel_cap.pebs_baseline) {
1062 hwc->config |= ICL_EVENTSEL_ADAPTIVE;
1063 if (cpuc->pebs_data_cfg != cpuc->active_pebs_data_cfg) {
1064 wrmsrl(MSR_PEBS_DATA_CFG, cpuc->pebs_data_cfg);
1065 cpuc->active_pebs_data_cfg = cpuc->pebs_data_cfg;
1066 }
1067 }
1068
3569c0d7 1069 /*
09e61b4f
PZ
1070 * Use auto-reload if possible to save a MSR write in the PMI.
1071 * This must be done in pmu::start(), because PERF_EVENT_IOC_PERIOD.
3569c0d7 1072 */
851559e3 1073 if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
ec71a398
KL
1074 unsigned int idx = hwc->idx;
1075
1076 if (idx >= INTEL_PMC_IDX_FIXED)
1077 idx = MAX_PEBS_EVENTS + (idx - INTEL_PMC_IDX_FIXED);
1078 ds->pebs_event_reset[idx] =
851559e3 1079 (u64)(-hwc->sample_period) & x86_pmu.cntval_mask;
dc853e26
JO
1080 } else {
1081 ds->pebs_event_reset[hwc->idx] = 0;
851559e3 1082 }
09e61b4f
PZ
1083}
1084
68f7082f 1085void intel_pmu_pebs_del(struct perf_event *event)
09e61b4f
PZ
1086{
1087 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1088 struct hw_perf_event *hwc = &event->hw;
1089 bool needed_cb = pebs_needs_sched_cb(cpuc);
1090
1091 cpuc->n_pebs--;
174afc3e 1092 if (hwc->flags & PERF_X86_EVENT_LARGE_PEBS)
09e61b4f 1093 cpuc->n_large_pebs--;
3569c0d7 1094
c22497f5 1095 pebs_update_state(needed_cb, cpuc, event, false);
ca037701
PZ
1096}
1097
de0428a7 1098void intel_pmu_pebs_disable(struct perf_event *event)
ca037701 1099{
89cbc767 1100 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
ef21f683 1101 struct hw_perf_event *hwc = &event->hw;
2a853e11 1102
09e61b4f 1103 if (cpuc->n_pebs == cpuc->n_large_pebs)
2a853e11 1104 intel_pmu_drain_pebs_buffer();
ca037701 1105
ad0e6cfe 1106 cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
983433b5 1107
b371b594 1108 if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
983433b5 1109 cpuc->pebs_enabled &= ~(1ULL << (hwc->idx + 32));
b371b594 1110 else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
983433b5
SE
1111 cpuc->pebs_enabled &= ~(1ULL << 63);
1112
4807e3d5 1113 if (cpuc->enabled)
ad0e6cfe 1114 wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
ca037701
PZ
1115
1116 hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
1117}
1118
de0428a7 1119void intel_pmu_pebs_enable_all(void)
ca037701 1120{
89cbc767 1121 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
ca037701
PZ
1122
1123 if (cpuc->pebs_enabled)
1124 wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
1125}
1126
de0428a7 1127void intel_pmu_pebs_disable_all(void)
ca037701 1128{
89cbc767 1129 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
ca037701
PZ
1130
1131 if (cpuc->pebs_enabled)
1132 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
1133}
1134
ef21f683
PZ
1135static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
1136{
89cbc767 1137 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
ef21f683
PZ
1138 unsigned long from = cpuc->lbr_entries[0].from;
1139 unsigned long old_to, to = cpuc->lbr_entries[0].to;
1140 unsigned long ip = regs->ip;
57d1c0c0 1141 int is_64bit = 0;
9536c8d2 1142 void *kaddr;
6ba48ff4 1143 int size;
ef21f683 1144
8db909a7
PZ
1145 /*
1146 * We don't need to fixup if the PEBS assist is fault like
1147 */
1148 if (!x86_pmu.intel_cap.pebs_trap)
1149 return 1;
1150
a562b187
PZ
1151 /*
1152 * No LBR entry, no basic block, no rewinding
1153 */
ef21f683
PZ
1154 if (!cpuc->lbr_stack.nr || !from || !to)
1155 return 0;
1156
a562b187
PZ
1157 /*
1158 * Basic blocks should never cross user/kernel boundaries
1159 */
1160 if (kernel_ip(ip) != kernel_ip(to))
1161 return 0;
1162
1163 /*
1164 * unsigned math, either ip is before the start (impossible) or
1165 * the basic block is larger than 1 page (sanity)
1166 */
9536c8d2 1167 if ((ip - to) > PEBS_FIXUP_SIZE)
ef21f683
PZ
1168 return 0;
1169
1170 /*
1171 * We sampled a branch insn, rewind using the LBR stack
1172 */
1173 if (ip == to) {
d07bdfd3 1174 set_linear_ip(regs, from);
ef21f683
PZ
1175 return 1;
1176 }
1177
6ba48ff4 1178 size = ip - to;
9536c8d2 1179 if (!kernel_ip(ip)) {
6ba48ff4 1180 int bytes;
9536c8d2
PZ
1181 u8 *buf = this_cpu_read(insn_buffer);
1182
6ba48ff4 1183 /* 'size' must fit our buffer, see above */
9536c8d2 1184 bytes = copy_from_user_nmi(buf, (void __user *)to, size);
0a196848 1185 if (bytes != 0)
9536c8d2
PZ
1186 return 0;
1187
1188 kaddr = buf;
1189 } else {
1190 kaddr = (void *)to;
1191 }
1192
ef21f683
PZ
1193 do {
1194 struct insn insn;
ef21f683
PZ
1195
1196 old_to = to;
ef21f683 1197
57d1c0c0
PZ
1198#ifdef CONFIG_X86_64
1199 is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
1200#endif
6ba48ff4 1201 insn_init(&insn, kaddr, size, is_64bit);
ef21f683 1202 insn_get_length(&insn);
6ba48ff4
DH
1203 /*
1204 * Make sure there was not a problem decoding the
1205 * instruction and getting the length. This is
1206 * doubly important because we have an infinite
1207 * loop if insn.length=0.
1208 */
1209 if (!insn.length)
1210 break;
9536c8d2 1211
ef21f683 1212 to += insn.length;
9536c8d2 1213 kaddr += insn.length;
6ba48ff4 1214 size -= insn.length;
ef21f683
PZ
1215 } while (to < ip);
1216
1217 if (to == ip) {
d07bdfd3 1218 set_linear_ip(regs, old_to);
ef21f683
PZ
1219 return 1;
1220 }
1221
a562b187
PZ
1222 /*
1223 * Even though we decoded the basic block, the instruction stream
1224 * never matched the given IP, either the TO or the IP got corrupted.
1225 */
ef21f683
PZ
1226 return 0;
1227}
1228
48f38aa4 1229static inline u64 intel_get_tsx_weight(u64 tsx_tuning)
748e86aa 1230{
48f38aa4
AK
1231 if (tsx_tuning) {
1232 union hsw_tsx_tuning tsx = { .value = tsx_tuning };
748e86aa
AK
1233 return tsx.cycles_last_block;
1234 }
1235 return 0;
1236}
1237
48f38aa4 1238static inline u64 intel_get_tsx_transaction(u64 tsx_tuning, u64 ax)
a405bad5 1239{
48f38aa4 1240 u64 txn = (tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32;
a405bad5
AK
1241
1242 /* For RTM XABORTs also log the abort code from AX */
48f38aa4
AK
1243 if ((txn & PERF_TXN_TRANSACTION) && (ax & 1))
1244 txn |= ((ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
a405bad5
AK
1245 return txn;
1246}
1247
c22497f5
KL
1248static inline u64 get_pebs_status(void *n)
1249{
1250 if (x86_pmu.intel_cap.pebs_format < 4)
1251 return ((struct pebs_record_nhm *)n)->status;
1252 return ((struct pebs_basic *)n)->applicable_counters;
1253}
1254
48f38aa4
AK
1255#define PERF_X86_EVENT_PEBS_HSW_PREC \
1256 (PERF_X86_EVENT_PEBS_ST_HSW | \
1257 PERF_X86_EVENT_PEBS_LD_HSW | \
1258 PERF_X86_EVENT_PEBS_NA_HSW)
1259
1260static u64 get_data_src(struct perf_event *event, u64 aux)
1261{
1262 u64 val = PERF_MEM_NA;
1263 int fl = event->hw.flags;
1264 bool fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC);
1265
1266 if (fl & PERF_X86_EVENT_PEBS_LDLAT)
1267 val = load_latency_data(aux);
1268 else if (fst && (fl & PERF_X86_EVENT_PEBS_HSW_PREC))
1269 val = precise_datala_hsw(event, aux);
1270 else if (fst)
1271 val = precise_store_data(aux);
1272 return val;
1273}
1274
c22497f5 1275static void setup_pebs_fixed_sample_data(struct perf_event *event,
43cf7631
YZ
1276 struct pt_regs *iregs, void *__pebs,
1277 struct perf_sample_data *data,
1278 struct pt_regs *regs)
2b0b5c6f
PZ
1279{
1280 /*
d2beea4a
PZ
1281 * We cast to the biggest pebs_record but are careful not to
1282 * unconditionally access the 'extra' entries.
2b0b5c6f 1283 */
89cbc767 1284 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2f7ebf2e 1285 struct pebs_record_skl *pebs = __pebs;
f20093ee 1286 u64 sample_type;
48f38aa4 1287 int fll;
2b0b5c6f 1288
21509084
YZ
1289 if (pebs == NULL)
1290 return;
1291
c8aab2e0 1292 sample_type = event->attr.sample_type;
48f38aa4 1293 fll = event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT;
f20093ee 1294
43cf7631 1295 perf_sample_data_init(data, 0, event->hw.last_period);
2b0b5c6f 1296
43cf7631 1297 data->period = event->hw.last_period;
f20093ee
SE
1298
1299 /*
c8aab2e0 1300 * Use latency for weight (only avail with PEBS-LL)
f20093ee 1301 */
c8aab2e0 1302 if (fll && (sample_type & PERF_SAMPLE_WEIGHT))
43cf7631 1303 data->weight = pebs->lat;
c8aab2e0
SE
1304
1305 /*
1306 * data.data_src encodes the data source
1307 */
48f38aa4
AK
1308 if (sample_type & PERF_SAMPLE_DATA_SRC)
1309 data->data_src.val = get_data_src(event, pebs->dse);
f20093ee 1310
6cbc304f
PZ
1311 /*
1312 * We must however always use iregs for the unwinder to stay sane; the
1313 * record BP,SP,IP can point into thin air when the record is from a
a97673a1 1314 * previous PMI context or an (I)RET happened between the record and
6cbc304f
PZ
1315 * PMI.
1316 */
1317 if (sample_type & PERF_SAMPLE_CALLCHAIN)
1318 data->callchain = perf_callchain(event, iregs);
1319
2b0b5c6f 1320 /*
b8000586
PZ
1321 * We use the interrupt regs as a base because the PEBS record does not
1322 * contain a full regs set, specifically it seems to lack segment
1323 * descriptors, which get used by things like user_mode().
2b0b5c6f 1324 *
b8000586 1325 * In the simple case fix up only the IP for PERF_SAMPLE_IP.
2b0b5c6f 1326 */
43cf7631 1327 *regs = *iregs;
d1e7e602
SE
1328
1329 /*
1330 * Initialize regs_>flags from PEBS,
1331 * Clear exact bit (which uses x86 EFLAGS Reserved bit 3),
1332 * i.e., do not rely on it being zero:
1333 */
1334 regs->flags = pebs->flags & ~PERF_EFLAGS_EXACT;
2b0b5c6f 1335
aea48559 1336 if (sample_type & PERF_SAMPLE_REGS_INTR) {
43cf7631
YZ
1337 regs->ax = pebs->ax;
1338 regs->bx = pebs->bx;
1339 regs->cx = pebs->cx;
1340 regs->dx = pebs->dx;
1341 regs->si = pebs->si;
1342 regs->di = pebs->di;
43cf7631 1343
6cbc304f
PZ
1344 regs->bp = pebs->bp;
1345 regs->sp = pebs->sp;
b8000586 1346
aea48559 1347#ifndef CONFIG_X86_32
43cf7631
YZ
1348 regs->r8 = pebs->r8;
1349 regs->r9 = pebs->r9;
1350 regs->r10 = pebs->r10;
1351 regs->r11 = pebs->r11;
1352 regs->r12 = pebs->r12;
1353 regs->r13 = pebs->r13;
1354 regs->r14 = pebs->r14;
1355 regs->r15 = pebs->r15;
aea48559
SE
1356#endif
1357 }
1358
71eb9ee9 1359 if (event->attr.precise_ip > 1) {
d1e7e602
SE
1360 /*
1361 * Haswell and later processors have an 'eventing IP'
1362 * (real IP) which fixes the off-by-1 skid in hardware.
1363 * Use it when precise_ip >= 2 :
1364 */
71eb9ee9
SE
1365 if (x86_pmu.intel_cap.pebs_format >= 2) {
1366 set_linear_ip(regs, pebs->real_ip);
1367 regs->flags |= PERF_EFLAGS_EXACT;
1368 } else {
d1e7e602 1369 /* Otherwise, use PEBS off-by-1 IP: */
71eb9ee9
SE
1370 set_linear_ip(regs, pebs->ip);
1371
d1e7e602
SE
1372 /*
1373 * With precise_ip >= 2, try to fix up the off-by-1 IP
1374 * using the LBR. If successful, the fixup function
1375 * corrects regs->ip and calls set_linear_ip() on regs:
1376 */
71eb9ee9
SE
1377 if (intel_pmu_pebs_fixup_ip(regs))
1378 regs->flags |= PERF_EFLAGS_EXACT;
1379 }
d1e7e602
SE
1380 } else {
1381 /*
1382 * When precise_ip == 1, return the PEBS off-by-1 IP,
1383 * no fixup attempted:
1384 */
71eb9ee9 1385 set_linear_ip(regs, pebs->ip);
d1e7e602 1386 }
71eb9ee9 1387
2b0b5c6f 1388
fc7ce9c7 1389 if ((sample_type & (PERF_SAMPLE_ADDR | PERF_SAMPLE_PHYS_ADDR)) &&
d2beea4a 1390 x86_pmu.intel_cap.pebs_format >= 1)
43cf7631 1391 data->addr = pebs->dla;
f9134f36 1392
a405bad5
AK
1393 if (x86_pmu.intel_cap.pebs_format >= 2) {
1394 /* Only set the TSX weight when no memory weight. */
c8aab2e0 1395 if ((sample_type & PERF_SAMPLE_WEIGHT) && !fll)
48f38aa4 1396 data->weight = intel_get_tsx_weight(pebs->tsx_tuning);
a405bad5 1397
c8aab2e0 1398 if (sample_type & PERF_SAMPLE_TRANSACTION)
48f38aa4
AK
1399 data->txn = intel_get_tsx_transaction(pebs->tsx_tuning,
1400 pebs->ax);
a405bad5 1401 }
748e86aa 1402
2f7ebf2e
AK
1403 /*
1404 * v3 supplies an accurate time stamp, so we use that
1405 * for the time stamp.
1406 *
1407 * We can only do this for the default trace clock.
1408 */
1409 if (x86_pmu.intel_cap.pebs_format >= 3 &&
1410 event->attr.use_clockid == 0)
1411 data->time = native_sched_clock_from_tsc(pebs->tsc);
1412
60ce0fbd 1413 if (has_branch_stack(event))
43cf7631
YZ
1414 data->br_stack = &cpuc->lbr_stack;
1415}
1416
c22497f5
KL
1417static void adaptive_pebs_save_regs(struct pt_regs *regs,
1418 struct pebs_gprs *gprs)
1419{
1420 regs->ax = gprs->ax;
1421 regs->bx = gprs->bx;
1422 regs->cx = gprs->cx;
1423 regs->dx = gprs->dx;
1424 regs->si = gprs->si;
1425 regs->di = gprs->di;
1426 regs->bp = gprs->bp;
1427 regs->sp = gprs->sp;
1428#ifndef CONFIG_X86_32
1429 regs->r8 = gprs->r8;
1430 regs->r9 = gprs->r9;
1431 regs->r10 = gprs->r10;
1432 regs->r11 = gprs->r11;
1433 regs->r12 = gprs->r12;
1434 regs->r13 = gprs->r13;
1435 regs->r14 = gprs->r14;
1436 regs->r15 = gprs->r15;
1437#endif
1438}
1439
1440/*
1441 * With adaptive PEBS the layout depends on what fields are configured.
1442 */
1443
1444static void setup_pebs_adaptive_sample_data(struct perf_event *event,
1445 struct pt_regs *iregs, void *__pebs,
1446 struct perf_sample_data *data,
1447 struct pt_regs *regs)
1448{
1449 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1450 struct pebs_basic *basic = __pebs;
1451 void *next_record = basic + 1;
1452 u64 sample_type;
1453 u64 format_size;
1454 struct pebs_meminfo *meminfo = NULL;
1455 struct pebs_gprs *gprs = NULL;
1456 struct x86_perf_regs *perf_regs;
1457
1458 if (basic == NULL)
1459 return;
1460
1461 perf_regs = container_of(regs, struct x86_perf_regs, regs);
1462 perf_regs->xmm_regs = NULL;
1463
1464 sample_type = event->attr.sample_type;
1465 format_size = basic->format_size;
1466 perf_sample_data_init(data, 0, event->hw.last_period);
1467 data->period = event->hw.last_period;
1468
1469 if (event->attr.use_clockid == 0)
1470 data->time = native_sched_clock_from_tsc(basic->tsc);
1471
1472 /*
1473 * We must however always use iregs for the unwinder to stay sane; the
1474 * record BP,SP,IP can point into thin air when the record is from a
1475 * previous PMI context or an (I)RET happened between the record and
1476 * PMI.
1477 */
1478 if (sample_type & PERF_SAMPLE_CALLCHAIN)
1479 data->callchain = perf_callchain(event, iregs);
1480
1481 *regs = *iregs;
1482 /* The ip in basic is EventingIP */
1483 set_linear_ip(regs, basic->ip);
1484 regs->flags = PERF_EFLAGS_EXACT;
1485
1486 /*
1487 * The record for MEMINFO is in front of GP
1488 * But PERF_SAMPLE_TRANSACTION needs gprs->ax.
1489 * Save the pointer here but process later.
1490 */
1491 if (format_size & PEBS_DATACFG_MEMINFO) {
1492 meminfo = next_record;
1493 next_record = meminfo + 1;
1494 }
1495
1496 if (format_size & PEBS_DATACFG_GP) {
1497 gprs = next_record;
1498 next_record = gprs + 1;
1499
1500 if (event->attr.precise_ip < 2) {
1501 set_linear_ip(regs, gprs->ip);
1502 regs->flags &= ~PERF_EFLAGS_EXACT;
1503 }
1504
1505 if (sample_type & PERF_SAMPLE_REGS_INTR)
1506 adaptive_pebs_save_regs(regs, gprs);
1507 }
1508
1509 if (format_size & PEBS_DATACFG_MEMINFO) {
1510 if (sample_type & PERF_SAMPLE_WEIGHT)
1511 data->weight = meminfo->latency ?:
1512 intel_get_tsx_weight(meminfo->tsx_tuning);
1513
1514 if (sample_type & PERF_SAMPLE_DATA_SRC)
1515 data->data_src.val = get_data_src(event, meminfo->aux);
1516
1517 if (sample_type & (PERF_SAMPLE_ADDR | PERF_SAMPLE_PHYS_ADDR))
1518 data->addr = meminfo->address;
1519
1520 if (sample_type & PERF_SAMPLE_TRANSACTION)
1521 data->txn = intel_get_tsx_transaction(meminfo->tsx_tuning,
1522 gprs ? gprs->ax : 0);
1523 }
1524
1525 if (format_size & PEBS_DATACFG_XMMS) {
1526 struct pebs_xmm *xmm = next_record;
1527
1528 next_record = xmm + 1;
1529 perf_regs->xmm_regs = xmm->xmm;
1530 }
1531
1532 if (format_size & PEBS_DATACFG_LBRS) {
1533 struct pebs_lbr *lbr = next_record;
1534 int num_lbr = ((format_size >> PEBS_DATACFG_LBR_SHIFT)
1535 & 0xff) + 1;
1536 next_record = next_record + num_lbr*sizeof(struct pebs_lbr_entry);
1537
1538 if (has_branch_stack(event)) {
1539 intel_pmu_store_pebs_lbrs(lbr);
1540 data->br_stack = &cpuc->lbr_stack;
1541 }
1542 }
1543
1544 WARN_ONCE(next_record != __pebs + (format_size >> 48),
1545 "PEBS record size %llu, expected %llu, config %llx\n",
1546 format_size >> 48,
1547 (u64)(next_record - __pebs),
1548 basic->format_size);
1549}
1550
21509084
YZ
1551static inline void *
1552get_next_pebs_record_by_bit(void *base, void *top, int bit)
1553{
1554 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1555 void *at;
1556 u64 pebs_status;
1557
1424a09a
SE
1558 /*
1559 * fmt0 does not have a status bitfield (does not use
1560 * perf_record_nhm format)
1561 */
1562 if (x86_pmu.intel_cap.pebs_format < 1)
1563 return base;
1564
21509084
YZ
1565 if (base == NULL)
1566 return NULL;
1567
c22497f5
KL
1568 for (at = base; at < top; at += cpuc->pebs_record_size) {
1569 unsigned long status = get_pebs_status(at);
21509084 1570
c22497f5 1571 if (test_bit(bit, (unsigned long *)&status)) {
a3d86542
PZ
1572 /* PEBS v3 has accurate status bits */
1573 if (x86_pmu.intel_cap.pebs_format >= 3)
1574 return at;
21509084 1575
c22497f5 1576 if (status == (1 << bit))
21509084
YZ
1577 return at;
1578
1579 /* clear non-PEBS bit and re-check */
c22497f5 1580 pebs_status = status & cpuc->pebs_enabled;
fd583ad1 1581 pebs_status &= PEBS_COUNTER_MASK;
21509084
YZ
1582 if (pebs_status == (1 << bit))
1583 return at;
1584 }
1585 }
1586 return NULL;
1587}
1588
5bee2cc6
KL
1589void intel_pmu_auto_reload_read(struct perf_event *event)
1590{
1591 WARN_ON(!(event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD));
1592
1593 perf_pmu_disable(event->pmu);
1594 intel_pmu_drain_pebs_buffer();
1595 perf_pmu_enable(event->pmu);
1596}
1597
d31fc13f
KL
1598/*
1599 * Special variant of intel_pmu_save_and_restart() for auto-reload.
1600 */
1601static int
1602intel_pmu_save_and_restart_reload(struct perf_event *event, int count)
1603{
1604 struct hw_perf_event *hwc = &event->hw;
1605 int shift = 64 - x86_pmu.cntval_bits;
1606 u64 period = hwc->sample_period;
1607 u64 prev_raw_count, new_raw_count;
1608 s64 new, old;
1609
1610 WARN_ON(!period);
1611
1612 /*
1613 * drain_pebs() only happens when the PMU is disabled.
1614 */
1615 WARN_ON(this_cpu_read(cpu_hw_events.enabled));
1616
1617 prev_raw_count = local64_read(&hwc->prev_count);
1618 rdpmcl(hwc->event_base_rdpmc, new_raw_count);
1619 local64_set(&hwc->prev_count, new_raw_count);
1620
1621 /*
1622 * Since the counter increments a negative counter value and
1623 * overflows on the sign switch, giving the interval:
1624 *
1625 * [-period, 0]
1626 *
1627 * the difference between two consequtive reads is:
1628 *
1629 * A) value2 - value1;
1630 * when no overflows have happened in between,
1631 *
1632 * B) (0 - value1) + (value2 - (-period));
1633 * when one overflow happened in between,
1634 *
1635 * C) (0 - value1) + (n - 1) * (period) + (value2 - (-period));
1636 * when @n overflows happened in between.
1637 *
1638 * Here A) is the obvious difference, B) is the extension to the
1639 * discrete interval, where the first term is to the top of the
1640 * interval and the second term is from the bottom of the next
1641 * interval and C) the extension to multiple intervals, where the
1642 * middle term is the whole intervals covered.
1643 *
1644 * An equivalent of C, by reduction, is:
1645 *
1646 * value2 - value1 + n * period
1647 */
1648 new = ((s64)(new_raw_count << shift) >> shift);
1649 old = ((s64)(prev_raw_count << shift) >> shift);
1650 local64_add(new - old + count * period, &event->count);
1651
1652 perf_event_update_userpage(event);
1653
1654 return 0;
1655}
1656
43cf7631 1657static void __intel_pmu_pebs_event(struct perf_event *event,
21509084
YZ
1658 struct pt_regs *iregs,
1659 void *base, void *top,
c22497f5
KL
1660 int bit, int count,
1661 void (*setup_sample)(struct perf_event *,
1662 struct pt_regs *,
1663 void *,
1664 struct perf_sample_data *,
1665 struct pt_regs *))
43cf7631 1666{
c22497f5 1667 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
d31fc13f 1668 struct hw_perf_event *hwc = &event->hw;
43cf7631 1669 struct perf_sample_data data;
c22497f5
KL
1670 struct x86_perf_regs perf_regs;
1671 struct pt_regs *regs = &perf_regs.regs;
21509084 1672 void *at = get_next_pebs_record_by_bit(base, top, bit);
43cf7631 1673
d31fc13f
KL
1674 if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
1675 /*
1676 * Now, auto-reload is only enabled in fixed period mode.
1677 * The reload value is always hwc->sample_period.
1678 * May need to change it, if auto-reload is enabled in
1679 * freq mode later.
1680 */
1681 intel_pmu_save_and_restart_reload(event, count);
1682 } else if (!intel_pmu_save_and_restart(event))
43cf7631
YZ
1683 return;
1684
a3d86542 1685 while (count > 1) {
c22497f5
KL
1686 setup_sample(event, iregs, at, &data, regs);
1687 perf_event_output(event, &data, regs);
1688 at += cpuc->pebs_record_size;
a3d86542
PZ
1689 at = get_next_pebs_record_by_bit(at, top, bit);
1690 count--;
21509084
YZ
1691 }
1692
c22497f5 1693 setup_sample(event, iregs, at, &data, regs);
60ce0fbd 1694
21509084
YZ
1695 /*
1696 * All but the last records are processed.
1697 * The last one is left to be able to call the overflow handler.
1698 */
c22497f5 1699 if (perf_event_overflow(event, &data, regs)) {
a4eaf7f1 1700 x86_pmu_stop(event, 0);
21509084
YZ
1701 return;
1702 }
1703
2b0b5c6f
PZ
1704}
1705
ca037701
PZ
1706static void intel_pmu_drain_pebs_core(struct pt_regs *iregs)
1707{
89cbc767 1708 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
ca037701
PZ
1709 struct debug_store *ds = cpuc->ds;
1710 struct perf_event *event = cpuc->events[0]; /* PMC0 only */
1711 struct pebs_record_core *at, *top;
ca037701
PZ
1712 int n;
1713
6809b6ea 1714 if (!x86_pmu.pebs_active)
ca037701
PZ
1715 return;
1716
ca037701
PZ
1717 at = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base;
1718 top = (struct pebs_record_core *)(unsigned long)ds->pebs_index;
1719
d80c7502
PZ
1720 /*
1721 * Whatever else happens, drain the thing
1722 */
1723 ds->pebs_index = ds->pebs_buffer_base;
1724
1725 if (!test_bit(0, cpuc->active_mask))
8f4aebd2 1726 return;
ca037701 1727
d80c7502
PZ
1728 WARN_ON_ONCE(!event);
1729
ab608344 1730 if (!event->attr.precise_ip)
d80c7502
PZ
1731 return;
1732
1424a09a 1733 n = top - at;
d31fc13f
KL
1734 if (n <= 0) {
1735 if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)
1736 intel_pmu_save_and_restart_reload(event, 0);
d80c7502 1737 return;
d31fc13f 1738 }
ca037701 1739
c22497f5
KL
1740 __intel_pmu_pebs_event(event, iregs, at, top, 0, n,
1741 setup_pebs_fixed_sample_data);
ca037701
PZ
1742}
1743
477f00f9
KL
1744static void intel_pmu_pebs_event_update_no_drain(struct cpu_hw_events *cpuc, int size)
1745{
1746 struct perf_event *event;
1747 int bit;
1748
1749 /*
1750 * The drain_pebs() could be called twice in a short period
1751 * for auto-reload event in pmu::read(). There are no
1752 * overflows have happened in between.
1753 * It needs to call intel_pmu_save_and_restart_reload() to
1754 * update the event->count for this case.
1755 */
1756 for_each_set_bit(bit, (unsigned long *)&cpuc->pebs_enabled, size) {
1757 event = cpuc->events[bit];
1758 if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)
1759 intel_pmu_save_and_restart_reload(event, 0);
1760 }
1761}
1762
d2beea4a 1763static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
ca037701 1764{
89cbc767 1765 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
ca037701 1766 struct debug_store *ds = cpuc->ds;
21509084
YZ
1767 struct perf_event *event;
1768 void *base, *at, *top;
ec71a398
KL
1769 short counts[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {};
1770 short error[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {};
1771 int bit, i, size;
1772 u64 mask;
d2beea4a
PZ
1773
1774 if (!x86_pmu.pebs_active)
1775 return;
1776
21509084 1777 base = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
d2beea4a 1778 top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
ca037701 1779
ca037701
PZ
1780 ds->pebs_index = ds->pebs_buffer_base;
1781
ec71a398
KL
1782 mask = (1ULL << x86_pmu.max_pebs_events) - 1;
1783 size = x86_pmu.max_pebs_events;
1784 if (x86_pmu.flags & PMU_FL_PEBS_ALL) {
1785 mask |= ((1ULL << x86_pmu.num_counters_fixed) - 1) << INTEL_PMC_IDX_FIXED;
1786 size = INTEL_PMC_IDX_FIXED + x86_pmu.num_counters_fixed;
1787 }
1788
d31fc13f 1789 if (unlikely(base >= top)) {
477f00f9 1790 intel_pmu_pebs_event_update_no_drain(cpuc, size);
d2beea4a 1791 return;
d31fc13f 1792 }
d2beea4a 1793
21509084 1794 for (at = base; at < top; at += x86_pmu.pebs_record_size) {
130768b8 1795 struct pebs_record_nhm *p = at;
75f80859 1796 u64 pebs_status;
ca037701 1797
8ef9b845 1798 pebs_status = p->status & cpuc->pebs_enabled;
ec71a398 1799 pebs_status &= mask;
8ef9b845
PZ
1800
1801 /* PEBS v3 has more accurate status bits */
a3d86542 1802 if (x86_pmu.intel_cap.pebs_format >= 3) {
c22497f5 1803 for_each_set_bit(bit, (unsigned long *)&pebs_status, size)
a3d86542
PZ
1804 counts[bit]++;
1805
1806 continue;
1807 }
1808
01330d72
AK
1809 /*
1810 * On some CPUs the PEBS status can be zero when PEBS is
1811 * racing with clearing of GLOBAL_STATUS.
1812 *
1813 * Normally we would drop that record, but in the
1814 * case when there is only a single active PEBS event
1815 * we can assume it's for that event.
1816 */
1817 if (!pebs_status && cpuc->pebs_enabled &&
1818 !(cpuc->pebs_enabled & (cpuc->pebs_enabled-1)))
1819 pebs_status = cpuc->pebs_enabled;
1820
75f80859 1821 bit = find_first_bit((unsigned long *)&pebs_status,
21509084 1822 x86_pmu.max_pebs_events);
957ea1fd 1823 if (bit >= x86_pmu.max_pebs_events)
21509084 1824 continue;
75f80859 1825
21509084
YZ
1826 /*
1827 * The PEBS hardware does not deal well with the situation
1828 * when events happen near to each other and multiple bits
1829 * are set. But it should happen rarely.
1830 *
1831 * If these events include one PEBS and multiple non-PEBS
1832 * events, it doesn't impact PEBS record. The record will
1833 * be handled normally. (slow path)
1834 *
1835 * If these events include two or more PEBS events, the
1836 * records for the events can be collapsed into a single
1837 * one, and it's not possible to reconstruct all events
1838 * that caused the PEBS record. It's called collision.
1839 * If collision happened, the record will be dropped.
21509084 1840 */
75f80859 1841 if (p->status != (1ULL << bit)) {
c22497f5 1842 for_each_set_bit(i, (unsigned long *)&pebs_status, size)
75f80859
PZ
1843 error[i]++;
1844 continue;
ca037701 1845 }
75f80859 1846
21509084
YZ
1847 counts[bit]++;
1848 }
ca037701 1849
c22497f5 1850 for_each_set_bit(bit, (unsigned long *)&mask, size) {
f38b0dbb 1851 if ((counts[bit] == 0) && (error[bit] == 0))
ca037701 1852 continue;
75f80859 1853
21509084 1854 event = cpuc->events[bit];
8ef9b845
PZ
1855 if (WARN_ON_ONCE(!event))
1856 continue;
1857
1858 if (WARN_ON_ONCE(!event->attr.precise_ip))
1859 continue;
ca037701 1860
f38b0dbb 1861 /* log dropped samples number */
475113d9 1862 if (error[bit]) {
f38b0dbb
KL
1863 perf_log_lost_samples(event, error[bit]);
1864
475113d9
JO
1865 if (perf_event_account_interrupt(event))
1866 x86_pmu_stop(event, 0);
1867 }
1868
f38b0dbb
KL
1869 if (counts[bit]) {
1870 __intel_pmu_pebs_event(event, iregs, base,
c22497f5
KL
1871 top, bit, counts[bit],
1872 setup_pebs_fixed_sample_data);
f38b0dbb 1873 }
ca037701 1874 }
ca037701
PZ
1875}
1876
c22497f5
KL
1877static void intel_pmu_drain_pebs_icl(struct pt_regs *iregs)
1878{
1879 short counts[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {};
1880 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1881 struct debug_store *ds = cpuc->ds;
1882 struct perf_event *event;
1883 void *base, *at, *top;
1884 int bit, size;
1885 u64 mask;
1886
1887 if (!x86_pmu.pebs_active)
1888 return;
1889
1890 base = (struct pebs_basic *)(unsigned long)ds->pebs_buffer_base;
1891 top = (struct pebs_basic *)(unsigned long)ds->pebs_index;
1892
1893 ds->pebs_index = ds->pebs_buffer_base;
1894
1895 mask = ((1ULL << x86_pmu.max_pebs_events) - 1) |
1896 (((1ULL << x86_pmu.num_counters_fixed) - 1) << INTEL_PMC_IDX_FIXED);
1897 size = INTEL_PMC_IDX_FIXED + x86_pmu.num_counters_fixed;
1898
1899 if (unlikely(base >= top)) {
1900 intel_pmu_pebs_event_update_no_drain(cpuc, size);
1901 return;
1902 }
1903
1904 for (at = base; at < top; at += cpuc->pebs_record_size) {
1905 u64 pebs_status;
1906
1907 pebs_status = get_pebs_status(at) & cpuc->pebs_enabled;
1908 pebs_status &= mask;
1909
1910 for_each_set_bit(bit, (unsigned long *)&pebs_status, size)
1911 counts[bit]++;
1912 }
1913
1914 for_each_set_bit(bit, (unsigned long *)&mask, size) {
1915 if (counts[bit] == 0)
1916 continue;
1917
1918 event = cpuc->events[bit];
1919 if (WARN_ON_ONCE(!event))
1920 continue;
1921
1922 if (WARN_ON_ONCE(!event->attr.precise_ip))
1923 continue;
1924
1925 __intel_pmu_pebs_event(event, iregs, base,
1926 top, bit, counts[bit],
1927 setup_pebs_adaptive_sample_data);
1928 }
1929}
1930
ca037701
PZ
1931/*
1932 * BTS, PEBS probe and setup
1933 */
1934
066ce64c 1935void __init intel_ds_init(void)
ca037701
PZ
1936{
1937 /*
1938 * No support for 32bit formats
1939 */
1940 if (!boot_cpu_has(X86_FEATURE_DTES64))
1941 return;
1942
1943 x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS);
1944 x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS);
e72daf3f 1945 x86_pmu.pebs_buffer_size = PEBS_BUFFER_SIZE;
878068ea 1946 if (x86_pmu.version <= 4) {
9b545c04 1947 x86_pmu.pebs_no_isolation = 1;
878068ea
KL
1948 x86_pmu.pebs_no_xmm_regs = 1;
1949 }
ca037701 1950 if (x86_pmu.pebs) {
8db909a7 1951 char pebs_type = x86_pmu.intel_cap.pebs_trap ? '+' : '-';
c22497f5 1952 char *pebs_qual = "";
8db909a7 1953 int format = x86_pmu.intel_cap.pebs_format;
ca037701 1954
c22497f5
KL
1955 if (format < 4)
1956 x86_pmu.intel_cap.pebs_baseline = 0;
1957
ca037701
PZ
1958 switch (format) {
1959 case 0:
1b74dde7 1960 pr_cont("PEBS fmt0%c, ", pebs_type);
ca037701 1961 x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
e72daf3f
JO
1962 /*
1963 * Using >PAGE_SIZE buffers makes the WRMSR to
1964 * PERF_GLOBAL_CTRL in intel_pmu_enable_all()
1965 * mysteriously hang on Core2.
1966 *
1967 * As a workaround, we don't do this.
1968 */
1969 x86_pmu.pebs_buffer_size = PAGE_SIZE;
ca037701 1970 x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
ca037701
PZ
1971 break;
1972
1973 case 1:
1b74dde7 1974 pr_cont("PEBS fmt1%c, ", pebs_type);
ca037701
PZ
1975 x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
1976 x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
ca037701
PZ
1977 break;
1978
130768b8
AK
1979 case 2:
1980 pr_cont("PEBS fmt2%c, ", pebs_type);
1981 x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw);
d2beea4a 1982 x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
130768b8
AK
1983 break;
1984
2f7ebf2e
AK
1985 case 3:
1986 pr_cont("PEBS fmt3%c, ", pebs_type);
1987 x86_pmu.pebs_record_size =
1988 sizeof(struct pebs_record_skl);
1989 x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
174afc3e 1990 x86_pmu.large_pebs_flags |= PERF_SAMPLE_TIME;
2f7ebf2e
AK
1991 break;
1992
c22497f5
KL
1993 case 4:
1994 x86_pmu.drain_pebs = intel_pmu_drain_pebs_icl;
1995 x86_pmu.pebs_record_size = sizeof(struct pebs_basic);
1996 if (x86_pmu.intel_cap.pebs_baseline) {
1997 x86_pmu.large_pebs_flags |=
1998 PERF_SAMPLE_BRANCH_STACK |
1999 PERF_SAMPLE_TIME;
2000 x86_pmu.flags |= PMU_FL_PEBS_ALL;
2001 pebs_qual = "-baseline";
2002 } else {
2003 /* Only basic record supported */
2004 x86_pmu.pebs_no_xmm_regs = 1;
2005 x86_pmu.large_pebs_flags &=
2006 ~(PERF_SAMPLE_ADDR |
2007 PERF_SAMPLE_TIME |
2008 PERF_SAMPLE_DATA_SRC |
2009 PERF_SAMPLE_TRANSACTION |
2010 PERF_SAMPLE_REGS_USER |
2011 PERF_SAMPLE_REGS_INTR);
2012 }
2013 pr_cont("PEBS fmt4%c%s, ", pebs_type, pebs_qual);
2014 break;
2015
ca037701 2016 default:
1b74dde7 2017 pr_cont("no PEBS fmt%d%c, ", format, pebs_type);
ca037701 2018 x86_pmu.pebs = 0;
ca037701
PZ
2019 }
2020 }
2021}
1d9d8639
SE
2022
2023void perf_restore_debug_store(void)
2024{
2a6e06b2
LT
2025 struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
2026
1d9d8639
SE
2027 if (!x86_pmu.bts && !x86_pmu.pebs)
2028 return;
2029
2a6e06b2 2030 wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds);
1d9d8639 2031}