blob: 8156e47da7ba4c5a31e9f9436b27a96cfb7da4c3 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Kevin Winchesterde0428a2011-08-30 20:41:05 -03002#include <linux/bitops.h>
3#include <linux/types.h>
4#include <linux/slab.h>
Peter Zijlstraca037702010-03-02 19:52:12 +01005
Hugh Dickinsc1961a42017-12-04 15:07:50 +01006#include <asm/cpu_entry_area.h>
Kevin Winchesterde0428a2011-08-30 20:41:05 -03007#include <asm/perf_event.h>
Peter Zijlstra42f3bdc2018-01-04 18:07:12 +01008#include <asm/tlbflush.h>
Stephane Eranian3e702ff2012-02-09 23:20:58 +01009#include <asm/insn.h>
Kevin Winchesterde0428a2011-08-30 20:41:05 -030010
Borislav Petkov27f6d222016-02-10 10:55:23 +010011#include "../perf_event.h"
Peter Zijlstraca037702010-03-02 19:52:12 +010012
Thomas Gleixner10043e02017-12-04 15:07:49 +010013/* 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
Peter Zijlstraca037702010-03-02 19:52:12 +010016/* The size of a BTS record in bytes: */
17#define BTS_RECORD_SIZE 24
18
Peter Zijlstra9536c8d2013-10-15 12:14:04 +020019#define PEBS_FIXUP_SIZE PAGE_SIZE
Peter Zijlstraca037702010-03-02 19:52:12 +010020
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
Stephane Eranianf20093e2013-01-24 16:10:32 +010032union 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))
Andi Kleen6ae5fa62017-08-16 15:21:54 -070056#define LEVEL(x) P(LVLNUM, x)
57#define REM P(REMOTE, REMOTE)
Stephane Eranianf20093e2013-01-24 16:10:32 +010058#define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS))
59
Andi Kleene17dc652016-03-01 14:25:24 -080060/* Version for Sandy Bridge and later */
61static u64 pebs_data_source[] = {
Andi Kleen6ae5fa62017-08-16 15:21:54 -070062 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 */
Stephane Eranianf20093e2013-01-24 16:10:32 +010078};
79
Andi Kleene17dc652016-03-01 14:25:24 -080080/* Patch up minor differences in the bits */
81void __init intel_pmu_pebs_data_source_nhm(void)
82{
Andi Kleen6ae5fa62017-08-16 15:21:54 -070083 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);
Andi Kleene17dc652016-03-01 14:25:24 -080097}
98
Stephane Eranian9ad64c02013-01-24 16:10:34 +010099static 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
Stephane Eranianc8aab2e2014-08-11 21:27:13 +0200137static u64 precise_datala_hsw(struct perf_event *event, u64 status)
Andi Kleenf9134f32013-06-17 17:36:52 -0700138{
139 union perf_mem_data_src dse;
140
Stephane Eranian770eee12014-08-11 21:27:12 +0200141 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;
Stephane Eranian722e76e2014-05-15 17:56:44 +0200147
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 */
Stephane Eranianc8aab2e2014-08-11 21:27:13 +0200156 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 }
Andi Kleenf9134f32013-06-17 17:36:52 -0700162 return dse.val;
163}
164
Stephane Eranianf20093e2013-01-24 16:10:32 +0100165static u64 load_latency_data(u64 status)
166{
167 union intel_x86_pebs_dse dse;
168 u64 val;
Stephane Eranianf20093e2013-01-24 16:10:32 +0100169
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 */
Andi Kleen95298352017-08-16 15:21:53 -0700180 if (x86_pmu.pebs_no_tlb) {
Stephane Eranianf20093e2013-01-24 16:10:32 +0100181 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
Peter Zijlstraca037702010-03-02 19:52:12 +0100203struct 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
Andi Kleen130768b2013-06-17 17:36:47 -0700220/*
221 * Same as pebs_record_nhm, with two additional fields.
222 */
223struct pebs_record_hsw {
Andi Kleen748e86a2013-09-05 20:37:39 -0700224 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;
Peter Zijlstrad2beea42013-09-12 13:00:47 +0200230 u64 real_ip, tsx_tuning;
Andi Kleen748e86a2013-09-05 20:37:39 -0700231};
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;
Andi Kleen130768b2013-06-17 17:36:47 -0700246};
247
Andi Kleena405bad2013-09-20 07:40:40 -0700248#define PEBS_HSW_TSX_FLAGS 0xff00000000ULL
249
Andi Kleen2f7ebf22015-05-10 12:22:40 -0700250/* 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
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300263void init_debug_store_on_cpu(int cpu)
Peter Zijlstraca037702010-03-02 19:52:12 +0100264{
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
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300275void fini_debug_store_on_cpu(int cpu)
Peter Zijlstraca037702010-03-02 19:52:12 +0100276{
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
Peter Zijlstra9536c8d2013-10-15 12:14:04 +0200283static DEFINE_PER_CPU(void *, insn_buffer);
284
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100285static void ds_update_cea(void *cea, void *addr, size_t size, pgprot_t prot)
286{
Peter Zijlstra42f3bdc2018-01-04 18:07:12 +0100287 unsigned long start = (unsigned long)cea;
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100288 phys_addr_t pa;
289 size_t msz = 0;
290
291 pa = virt_to_phys(addr);
Peter Zijlstra42f3bdc2018-01-04 18:07:12 +0100292
293 preempt_disable();
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100294 for (; msz < size; msz += PAGE_SIZE, pa += PAGE_SIZE, cea += PAGE_SIZE)
295 cea_set_pte(cea, pa, prot);
Peter Zijlstra42f3bdc2018-01-04 18:07:12 +0100296
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();
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100303}
304
305static void ds_clear_cea(void *cea, size_t size)
306{
Peter Zijlstra42f3bdc2018-01-04 18:07:12 +0100307 unsigned long start = (unsigned long)cea;
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100308 size_t msz = 0;
309
Peter Zijlstra42f3bdc2018-01-04 18:07:12 +0100310 preempt_disable();
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100311 for (; msz < size; msz += PAGE_SIZE, cea += PAGE_SIZE)
312 cea_set_pte(cea, 0, PAGE_NONE);
Peter Zijlstra42f3bdc2018-01-04 18:07:12 +0100313
314 flush_tlb_kernel_range(start, start + size);
315 preempt_enable();
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100316}
317
318static void *dsalloc_pages(size_t size, gfp_t flags, int cpu)
319{
320 unsigned int order = get_order(size);
321 int node = cpu_to_node(cpu);
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
Peter Zijlstra5ee25c82010-10-19 14:15:04 +0200334static int alloc_pebs_buffer(int cpu)
335{
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100336 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;
Peter Zijlstra5ee25c82010-10-19 14:15:04 +0200341
342 if (!x86_pmu.pebs)
343 return 0;
344
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100345 buffer = dsalloc_pages(bsiz, GFP_KERNEL, cpu);
Peter Zijlstra5ee25c82010-10-19 14:15:04 +0200346 if (unlikely(!buffer))
347 return -ENOMEM;
348
Peter Zijlstra9536c8d2013-10-15 12:14:04 +0200349 /*
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) {
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100356 dsfree_pages(buffer, bsiz);
Peter Zijlstra9536c8d2013-10-15 12:14:04 +0200357 return -ENOMEM;
358 }
359 per_cpu(insn_buffer, cpu) = ibuffer;
360 }
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100361 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);
Peter Zijlstra5ee25c82010-10-19 14:15:04 +0200366 ds->pebs_index = ds->pebs_buffer_base;
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100367 max = x86_pmu.pebs_record_size * (bsiz / x86_pmu.pebs_record_size);
368 ds->pebs_absolute_maximum = ds->pebs_buffer_base + max;
Peter Zijlstra5ee25c82010-10-19 14:15:04 +0200369 return 0;
370}
371
Peter Zijlstrab39f88a2010-10-19 14:08:29 +0200372static void release_pebs_buffer(int cpu)
373{
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100374 struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
375 struct debug_store *ds = hwev->ds;
376 void *cea;
Peter Zijlstrab39f88a2010-10-19 14:08:29 +0200377
378 if (!ds || !x86_pmu.pebs)
379 return;
380
Peter Zijlstra9536c8d2013-10-15 12:14:04 +0200381 kfree(per_cpu(insn_buffer, cpu));
382 per_cpu(insn_buffer, cpu) = NULL;
383
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100384 /* Clear the fixmap */
385 cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer;
386 ds_clear_cea(cea, x86_pmu.pebs_buffer_size);
Peter Zijlstrab39f88a2010-10-19 14:08:29 +0200387 ds->pebs_buffer_base = 0;
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100388 dsfree_pages(hwev->ds_pebs_vaddr, x86_pmu.pebs_buffer_size);
389 hwev->ds_pebs_vaddr = NULL;
Peter Zijlstrab39f88a2010-10-19 14:08:29 +0200390}
391
Peter Zijlstra5ee25c82010-10-19 14:15:04 +0200392static int alloc_bts_buffer(int cpu)
393{
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100394 struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
395 struct debug_store *ds = hwev->ds;
396 void *buffer, *cea;
397 int max;
Peter Zijlstra5ee25c82010-10-19 14:15:04 +0200398
399 if (!x86_pmu.bts)
400 return 0;
401
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100402 buffer = dsalloc_pages(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, cpu);
David Rientjes44851542014-06-30 16:04:08 -0700403 if (unlikely(!buffer)) {
404 WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__);
Peter Zijlstra5ee25c82010-10-19 14:15:04 +0200405 return -ENOMEM;
David Rientjes44851542014-06-30 16:04:08 -0700406 }
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100407 hwev->ds_bts_vaddr = buffer;
408 /* Update the fixmap */
409 cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer;
410 ds->bts_buffer_base = (unsigned long) cea;
411 ds_update_cea(cea, buffer, BTS_BUFFER_SIZE, PAGE_KERNEL);
Peter Zijlstra5ee25c82010-10-19 14:15:04 +0200412 ds->bts_index = ds->bts_buffer_base;
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100413 max = BTS_RECORD_SIZE * (BTS_BUFFER_SIZE / BTS_RECORD_SIZE);
414 ds->bts_absolute_maximum = ds->bts_buffer_base + max;
415 ds->bts_interrupt_threshold = ds->bts_absolute_maximum - (max / 16);
Peter Zijlstra5ee25c82010-10-19 14:15:04 +0200416 return 0;
417}
418
Peter Zijlstrab39f88a2010-10-19 14:08:29 +0200419static void release_bts_buffer(int cpu)
420{
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100421 struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
422 struct debug_store *ds = hwev->ds;
423 void *cea;
Peter Zijlstrab39f88a2010-10-19 14:08:29 +0200424
425 if (!ds || !x86_pmu.bts)
426 return;
427
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100428 /* Clear the fixmap */
429 cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer;
430 ds_clear_cea(cea, BTS_BUFFER_SIZE);
Peter Zijlstrab39f88a2010-10-19 14:08:29 +0200431 ds->bts_buffer_base = 0;
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100432 dsfree_pages(hwev->ds_bts_vaddr, BTS_BUFFER_SIZE);
433 hwev->ds_bts_vaddr = NULL;
Peter Zijlstrab39f88a2010-10-19 14:08:29 +0200434}
435
Peter Zijlstra65af94b2010-10-19 14:37:23 +0200436static int alloc_ds_buffer(int cpu)
437{
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100438 struct debug_store *ds = &get_cpu_entry_area(cpu)->cpu_debug_store;
Peter Zijlstra65af94b2010-10-19 14:37:23 +0200439
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100440 memset(ds, 0, sizeof(*ds));
Peter Zijlstra65af94b2010-10-19 14:37:23 +0200441 per_cpu(cpu_hw_events, cpu).ds = ds;
Peter Zijlstra65af94b2010-10-19 14:37:23 +0200442 return 0;
443}
444
445static void release_ds_buffer(int cpu)
446{
Peter Zijlstra65af94b2010-10-19 14:37:23 +0200447 per_cpu(cpu_hw_events, cpu).ds = NULL;
Peter Zijlstra65af94b2010-10-19 14:37:23 +0200448}
449
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300450void release_ds_buffers(void)
Peter Zijlstraca037702010-03-02 19:52:12 +0100451{
452 int cpu;
453
454 if (!x86_pmu.bts && !x86_pmu.pebs)
455 return;
456
457 get_online_cpus();
Peter Zijlstraca037702010-03-02 19:52:12 +0100458 for_each_online_cpu(cpu)
459 fini_debug_store_on_cpu(cpu);
460
461 for_each_possible_cpu(cpu) {
Peter Zijlstrab39f88a2010-10-19 14:08:29 +0200462 release_pebs_buffer(cpu);
463 release_bts_buffer(cpu);
Peter Zijlstra65af94b2010-10-19 14:37:23 +0200464 release_ds_buffer(cpu);
Peter Zijlstraca037702010-03-02 19:52:12 +0100465 }
Peter Zijlstraca037702010-03-02 19:52:12 +0100466 put_online_cpus();
467}
468
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300469void reserve_ds_buffers(void)
Peter Zijlstraca037702010-03-02 19:52:12 +0100470{
Peter Zijlstra6809b6e2010-10-19 14:22:50 +0200471 int bts_err = 0, pebs_err = 0;
472 int cpu;
473
474 x86_pmu.bts_active = 0;
475 x86_pmu.pebs_active = 0;
Peter Zijlstraca037702010-03-02 19:52:12 +0100476
477 if (!x86_pmu.bts && !x86_pmu.pebs)
Peter Zijlstraf80c9e32010-10-19 14:50:02 +0200478 return;
Peter Zijlstraca037702010-03-02 19:52:12 +0100479
Peter Zijlstra6809b6e2010-10-19 14:22:50 +0200480 if (!x86_pmu.bts)
481 bts_err = 1;
482
483 if (!x86_pmu.pebs)
484 pebs_err = 1;
485
Peter Zijlstraca037702010-03-02 19:52:12 +0100486 get_online_cpus();
487
488 for_each_possible_cpu(cpu) {
Peter Zijlstra6809b6e2010-10-19 14:22:50 +0200489 if (alloc_ds_buffer(cpu)) {
490 bts_err = 1;
491 pebs_err = 1;
492 }
Peter Zijlstraca037702010-03-02 19:52:12 +0100493
Peter Zijlstra6809b6e2010-10-19 14:22:50 +0200494 if (!bts_err && alloc_bts_buffer(cpu))
495 bts_err = 1;
Peter Zijlstraca037702010-03-02 19:52:12 +0100496
Peter Zijlstra6809b6e2010-10-19 14:22:50 +0200497 if (!pebs_err && alloc_pebs_buffer(cpu))
498 pebs_err = 1;
Peter Zijlstraca037702010-03-02 19:52:12 +0100499
Peter Zijlstra6809b6e2010-10-19 14:22:50 +0200500 if (bts_err && pebs_err)
501 break;
Peter Zijlstraca037702010-03-02 19:52:12 +0100502 }
503
Peter Zijlstra6809b6e2010-10-19 14:22:50 +0200504 if (bts_err) {
505 for_each_possible_cpu(cpu)
506 release_bts_buffer(cpu);
507 }
508
509 if (pebs_err) {
510 for_each_possible_cpu(cpu)
511 release_pebs_buffer(cpu);
512 }
513
514 if (bts_err && pebs_err) {
515 for_each_possible_cpu(cpu)
516 release_ds_buffer(cpu);
517 } else {
518 if (x86_pmu.bts && !bts_err)
519 x86_pmu.bts_active = 1;
520
521 if (x86_pmu.pebs && !pebs_err)
522 x86_pmu.pebs_active = 1;
523
Peter Zijlstraca037702010-03-02 19:52:12 +0100524 for_each_online_cpu(cpu)
525 init_debug_store_on_cpu(cpu);
526 }
527
528 put_online_cpus();
Peter Zijlstraca037702010-03-02 19:52:12 +0100529}
530
531/*
532 * BTS
533 */
534
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300535struct event_constraint bts_constraint =
Robert Richter15c7ad52012-06-20 20:46:33 +0200536 EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0);
Peter Zijlstraca037702010-03-02 19:52:12 +0100537
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300538void intel_pmu_enable_bts(u64 config)
Peter Zijlstraca037702010-03-02 19:52:12 +0100539{
540 unsigned long debugctlmsr;
541
542 debugctlmsr = get_debugctlmsr();
543
Peter Zijlstra7c5ecaf2010-03-25 14:51:49 +0100544 debugctlmsr |= DEBUGCTLMSR_TR;
545 debugctlmsr |= DEBUGCTLMSR_BTS;
Alexander Shishkin80623822015-01-30 12:40:35 +0200546 if (config & ARCH_PERFMON_EVENTSEL_INT)
547 debugctlmsr |= DEBUGCTLMSR_BTINT;
Peter Zijlstraca037702010-03-02 19:52:12 +0100548
549 if (!(config & ARCH_PERFMON_EVENTSEL_OS))
Peter Zijlstra7c5ecaf2010-03-25 14:51:49 +0100550 debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
Peter Zijlstraca037702010-03-02 19:52:12 +0100551
552 if (!(config & ARCH_PERFMON_EVENTSEL_USR))
Peter Zijlstra7c5ecaf2010-03-25 14:51:49 +0100553 debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR;
Peter Zijlstraca037702010-03-02 19:52:12 +0100554
555 update_debugctlmsr(debugctlmsr);
556}
557
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300558void intel_pmu_disable_bts(void)
Peter Zijlstraca037702010-03-02 19:52:12 +0100559{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500560 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstraca037702010-03-02 19:52:12 +0100561 unsigned long debugctlmsr;
562
563 if (!cpuc->ds)
564 return;
565
566 debugctlmsr = get_debugctlmsr();
567
568 debugctlmsr &=
Peter Zijlstra7c5ecaf2010-03-25 14:51:49 +0100569 ~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT |
570 DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR);
Peter Zijlstraca037702010-03-02 19:52:12 +0100571
572 update_debugctlmsr(debugctlmsr);
573}
574
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300575int intel_pmu_drain_bts_buffer(void)
Peter Zijlstraca037702010-03-02 19:52:12 +0100576{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500577 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstraca037702010-03-02 19:52:12 +0100578 struct debug_store *ds = cpuc->ds;
579 struct bts_record {
580 u64 from;
581 u64 to;
582 u64 flags;
583 };
Robert Richter15c7ad52012-06-20 20:46:33 +0200584 struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
Alexander Shishkina09d31f42015-08-31 17:09:27 +0300585 struct bts_record *at, *base, *top;
Peter Zijlstraca037702010-03-02 19:52:12 +0100586 struct perf_output_handle handle;
587 struct perf_event_header header;
588 struct perf_sample_data data;
Alexander Shishkina09d31f42015-08-31 17:09:27 +0300589 unsigned long skip = 0;
Peter Zijlstraca037702010-03-02 19:52:12 +0100590 struct pt_regs regs;
591
592 if (!event)
Stephane Eranianb0b20722010-09-10 13:28:01 +0200593 return 0;
Peter Zijlstraca037702010-03-02 19:52:12 +0100594
Peter Zijlstra6809b6e2010-10-19 14:22:50 +0200595 if (!x86_pmu.bts_active)
Stephane Eranianb0b20722010-09-10 13:28:01 +0200596 return 0;
Peter Zijlstraca037702010-03-02 19:52:12 +0100597
Alexander Shishkina09d31f42015-08-31 17:09:27 +0300598 base = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
599 top = (struct bts_record *)(unsigned long)ds->bts_index;
Peter Zijlstraca037702010-03-02 19:52:12 +0100600
Alexander Shishkina09d31f42015-08-31 17:09:27 +0300601 if (top <= base)
Stephane Eranianb0b20722010-09-10 13:28:01 +0200602 return 0;
Peter Zijlstraca037702010-03-02 19:52:12 +0100603
Stephane Eranian0e480262013-03-19 16:10:38 +0100604 memset(&regs, 0, sizeof(regs));
605
Peter Zijlstraca037702010-03-02 19:52:12 +0100606 ds->bts_index = ds->bts_buffer_base;
607
Robert Richterfd0d0002012-04-02 20:19:08 +0200608 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstraca037702010-03-02 19:52:12 +0100609
610 /*
Alexander Shishkina09d31f42015-08-31 17:09:27 +0300611 * BTS leaks kernel addresses in branches across the cpl boundary,
612 * such as traps or system calls, so unless the user is asking for
613 * kernel tracing (and right now it's not possible), we'd need to
614 * filter them out. But first we need to count how many of those we
615 * have in the current batch. This is an extra O(n) pass, however,
616 * it's much faster than the other one especially considering that
617 * n <= 2560 (BTS_BUFFER_SIZE / BTS_RECORD_SIZE * 15/16; see the
618 * alloc_bts_buffer()).
619 */
620 for (at = base; at < top; at++) {
621 /*
622 * Note that right now *this* BTS code only works if
623 * attr::exclude_kernel is set, but let's keep this extra
624 * check here in case that changes.
625 */
626 if (event->attr.exclude_kernel &&
627 (kernel_ip(at->from) || kernel_ip(at->to)))
628 skip++;
629 }
630
631 /*
Peter Zijlstraca037702010-03-02 19:52:12 +0100632 * Prepare a generic sample, i.e. fill in the invariant fields.
633 * We will overwrite the from and to address before we output
634 * the sample.
635 */
Peter Zijlstrae8d8a902016-03-18 17:31:27 +0100636 rcu_read_lock();
Peter Zijlstraca037702010-03-02 19:52:12 +0100637 perf_prepare_sample(&header, &data, event, &regs);
638
Alexander Shishkina09d31f42015-08-31 17:09:27 +0300639 if (perf_output_begin(&handle, event, header.size *
640 (top - base - skip)))
Peter Zijlstrae8d8a902016-03-18 17:31:27 +0100641 goto unlock;
Peter Zijlstraca037702010-03-02 19:52:12 +0100642
Alexander Shishkina09d31f42015-08-31 17:09:27 +0300643 for (at = base; at < top; at++) {
644 /* Filter out any records that contain kernel addresses. */
645 if (event->attr.exclude_kernel &&
646 (kernel_ip(at->from) || kernel_ip(at->to)))
647 continue;
648
Peter Zijlstraca037702010-03-02 19:52:12 +0100649 data.ip = at->from;
650 data.addr = at->to;
651
652 perf_output_sample(&handle, &header, &data, event);
653 }
654
655 perf_output_end(&handle);
656
657 /* There's new data available. */
658 event->hw.interrupts++;
659 event->pending_kill = POLL_IN;
Peter Zijlstrae8d8a902016-03-18 17:31:27 +0100660unlock:
661 rcu_read_unlock();
Stephane Eranianb0b20722010-09-10 13:28:01 +0200662 return 1;
Peter Zijlstraca037702010-03-02 19:52:12 +0100663}
664
Yan, Zheng9c964ef2015-05-06 15:33:51 -0400665static inline void intel_pmu_drain_pebs_buffer(void)
666{
667 struct pt_regs regs;
668
669 x86_pmu.drain_pebs(&regs);
670}
671
Peter Zijlstraca037702010-03-02 19:52:12 +0100672/*
673 * PEBS
674 */
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300675struct event_constraint intel_core2_pebs_event_constraints[] = {
Andi Kleenaf4bdcf2014-09-24 07:34:48 -0700676 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
677 INTEL_FLAGS_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
678 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
679 INTEL_FLAGS_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
680 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
Peter Zijlstra517e6342015-04-11 12:16:22 +0200681 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
682 INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01),
Peter Zijlstraca037702010-03-02 19:52:12 +0100683 EVENT_CONSTRAINT_END
684};
685
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300686struct event_constraint intel_atom_pebs_event_constraints[] = {
Andi Kleenaf4bdcf2014-09-24 07:34:48 -0700687 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
688 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
689 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
Peter Zijlstra517e6342015-04-11 12:16:22 +0200690 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
691 INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01),
Stephane Eranian673d1882015-12-03 21:03:10 +0100692 /* Allow all events as PEBS with no flags */
693 INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
Stephane Eranian17e31622011-03-02 17:05:01 +0200694 EVENT_CONSTRAINT_END
695};
696
Yan, Zheng1fa64182013-07-18 17:02:24 +0800697struct event_constraint intel_slm_pebs_event_constraints[] = {
Kan Liang33636732015-01-12 17:42:21 +0000698 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
699 INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x1),
Andi Kleen86a04462014-08-11 21:27:10 +0200700 /* Allow all events as PEBS with no flags */
701 INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
Yan, Zheng1fa64182013-07-18 17:02:24 +0800702 EVENT_CONSTRAINT_END
703};
704
Kan Liang8b92c3a2016-04-15 00:42:47 -0700705struct event_constraint intel_glm_pebs_event_constraints[] = {
706 /* Allow all events as PEBS with no flags */
707 INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
708 EVENT_CONSTRAINT_END
709};
710
Kan Liangdd0b06b2017-07-12 09:44:23 -0400711struct event_constraint intel_glp_pebs_event_constraints[] = {
712 /* Allow all events as PEBS with no flags */
713 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
714 EVENT_CONSTRAINT_END
715};
716
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300717struct event_constraint intel_nehalem_pebs_event_constraints[] = {
Stephane Eranianf20093e2013-01-24 16:10:32 +0100718 INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
Andi Kleenaf4bdcf2014-09-24 07:34:48 -0700719 INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
720 INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
721 INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INST_RETIRED.ANY */
Lin Ming7d5d02d2011-03-09 23:21:29 +0800722 INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
Andi Kleenaf4bdcf2014-09-24 07:34:48 -0700723 INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
724 INTEL_FLAGS_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
725 INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
726 INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
727 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
728 INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
Peter Zijlstra517e6342015-04-11 12:16:22 +0200729 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
730 INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
Stephane Eranian17e31622011-03-02 17:05:01 +0200731 EVENT_CONSTRAINT_END
732};
733
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300734struct event_constraint intel_westmere_pebs_event_constraints[] = {
Stephane Eranianf20093e2013-01-24 16:10:32 +0100735 INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
Andi Kleenaf4bdcf2014-09-24 07:34:48 -0700736 INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
737 INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
738 INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INSTR_RETIRED.* */
Lin Ming7d5d02d2011-03-09 23:21:29 +0800739 INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
Andi Kleenaf4bdcf2014-09-24 07:34:48 -0700740 INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
741 INTEL_FLAGS_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */
742 INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
743 INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
744 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
745 INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
Peter Zijlstra517e6342015-04-11 12:16:22 +0200746 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
747 INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
Peter Zijlstraca037702010-03-02 19:52:12 +0100748 EVENT_CONSTRAINT_END
749};
750
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300751struct event_constraint intel_snb_pebs_event_constraints[] = {
Andi Kleen0dbc9472014-09-24 07:34:47 -0700752 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
Stephane Eranianf20093e2013-01-24 16:10:32 +0100753 INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
Stephane Eranian9ad64c02013-01-24 16:10:34 +0100754 INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
Andi Kleen86a04462014-08-11 21:27:10 +0200755 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
756 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
Maria Dimakopouloub63b4b42014-11-17 20:07:00 +0100757 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
758 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
759 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
760 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
Andi Kleen86a04462014-08-11 21:27:10 +0200761 /* Allow all events as PEBS with no flags */
762 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
Lin Mingb06b3d42011-03-02 21:27:04 +0800763 EVENT_CONSTRAINT_END
764};
765
Stephane Eranian20a36e32012-09-11 01:07:01 +0200766struct event_constraint intel_ivb_pebs_event_constraints[] = {
Andi Kleen0dbc9472014-09-24 07:34:47 -0700767 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
Stephane Eranianf20093e2013-01-24 16:10:32 +0100768 INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
Stephane Eranian9ad64c02013-01-24 16:10:34 +0100769 INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
Andi Kleen86a04462014-08-11 21:27:10 +0200770 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
771 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
Andi Kleen72469762015-12-04 03:50:52 -0800772 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
773 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
Maria Dimakopouloub63b4b42014-11-17 20:07:00 +0100774 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
775 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
776 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
777 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
Andi Kleen86a04462014-08-11 21:27:10 +0200778 /* Allow all events as PEBS with no flags */
779 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
Stephane Eranian20a36e32012-09-11 01:07:01 +0200780 EVENT_CONSTRAINT_END
781};
782
Andi Kleen30443182013-06-17 17:36:49 -0700783struct event_constraint intel_hsw_pebs_event_constraints[] = {
Andi Kleen0dbc9472014-09-24 07:34:47 -0700784 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
Andi Kleen86a04462014-08-11 21:27:10 +0200785 INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */
786 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
787 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
Andi Kleen72469762015-12-04 03:50:52 -0800788 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
789 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
Andi Kleen86a04462014-08-11 21:27:10 +0200790 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
Maria Dimakopouloub63b4b42014-11-17 20:07:00 +0100791 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
792 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
793 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
794 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
795 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
796 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
797 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
798 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
799 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
800 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
Andi Kleen86a04462014-08-11 21:27:10 +0200801 /* Allow all events as PEBS with no flags */
802 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
Andi Kleen30443182013-06-17 17:36:49 -0700803 EVENT_CONSTRAINT_END
804};
805
Stephane Eranianb3e62462016-03-03 20:50:42 +0100806struct event_constraint intel_bdw_pebs_event_constraints[] = {
807 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
808 INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */
809 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
810 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
811 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
812 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
813 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
814 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
815 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
816 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
817 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
818 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
819 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
820 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
821 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
822 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
823 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
824 /* Allow all events as PEBS with no flags */
825 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
826 EVENT_CONSTRAINT_END
827};
828
829
Andi Kleen9a92e162015-05-10 12:22:44 -0700830struct event_constraint intel_skl_pebs_event_constraints[] = {
831 INTEL_FLAGS_UEVENT_CONSTRAINT(0x1c0, 0x2), /* INST_RETIRED.PREC_DIST */
Andi Kleen72469762015-12-04 03:50:52 -0800832 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
833 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
Andi Kleen442f5c72015-12-04 03:50:32 -0800834 /* INST_RETIRED.TOTAL_CYCLES_PS (inv=1, cmask=16) (cycles:p). */
835 INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
Andi Kleen9a92e162015-05-10 12:22:44 -0700836 INTEL_PLD_CONSTRAINT(0x1cd, 0xf), /* MEM_TRANS_RETIRED.* */
837 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */
838 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */
839 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */
840 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x22d0, 0xf), /* MEM_INST_RETIRED.LOCK_STORES */
841 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */
842 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */
843 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */
844 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */
845 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_RETIRED.* */
846 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_L3_HIT_RETIRED.* */
847 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_L3_MISS_RETIRED.* */
848 /* Allow all events as PEBS with no flags */
849 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
850 EVENT_CONSTRAINT_END
851};
852
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300853struct event_constraint *intel_pebs_constraints(struct perf_event *event)
Peter Zijlstraca037702010-03-02 19:52:12 +0100854{
855 struct event_constraint *c;
856
Peter Zijlstraab608342010-04-08 23:03:20 +0200857 if (!event->attr.precise_ip)
Peter Zijlstraca037702010-03-02 19:52:12 +0100858 return NULL;
859
860 if (x86_pmu.pebs_constraints) {
861 for_each_event_constraint(c, x86_pmu.pebs_constraints) {
Stephane Eranian9fac2cf2013-01-24 16:10:27 +0100862 if ((event->hw.config & c->cmask) == c->code) {
863 event->hw.flags |= c->flags;
Peter Zijlstraca037702010-03-02 19:52:12 +0100864 return c;
Stephane Eranian9fac2cf2013-01-24 16:10:27 +0100865 }
Peter Zijlstraca037702010-03-02 19:52:12 +0100866 }
867 }
868
869 return &emptyconstraint;
870}
871
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200872/*
873 * We need the sched_task callback even for per-cpu events when we use
874 * the large interrupt threshold, such that we can provide PID and TID
875 * to PEBS samples.
876 */
877static inline bool pebs_needs_sched_cb(struct cpu_hw_events *cpuc)
Yan, Zheng3569c0d2015-05-06 15:33:50 -0400878{
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200879 return cpuc->n_pebs && (cpuc->n_pebs == cpuc->n_large_pebs);
880}
881
Jiri Olsadf6c3db2017-07-19 09:52:47 +0200882void intel_pmu_pebs_sched_task(struct perf_event_context *ctx, bool sched_in)
883{
884 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
885
886 if (!sched_in && pebs_needs_sched_cb(cpuc))
887 intel_pmu_drain_pebs_buffer();
888}
889
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200890static inline void pebs_update_threshold(struct cpu_hw_events *cpuc)
891{
892 struct debug_store *ds = cpuc->ds;
893 u64 threshold;
894
895 if (cpuc->n_pebs == cpuc->n_large_pebs) {
896 threshold = ds->pebs_absolute_maximum -
897 x86_pmu.max_pebs_events * x86_pmu.pebs_record_size;
898 } else {
899 threshold = ds->pebs_buffer_base + x86_pmu.pebs_record_size;
900 }
901
902 ds->pebs_interrupt_threshold = threshold;
903}
904
905static void
906pebs_update_state(bool needed_cb, struct cpu_hw_events *cpuc, struct pmu *pmu)
907{
Jiri Olsab6a32f02016-08-18 11:09:52 +0200908 /*
909 * Make sure we get updated with the first PEBS
910 * event. It will trigger also during removal, but
911 * that does not hurt:
912 */
913 bool update = cpuc->n_pebs == 1;
914
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200915 if (needed_cb != pebs_needs_sched_cb(cpuc)) {
916 if (!needed_cb)
917 perf_sched_cb_inc(pmu);
918 else
919 perf_sched_cb_dec(pmu);
920
Jiri Olsab6a32f02016-08-18 11:09:52 +0200921 update = true;
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200922 }
Jiri Olsab6a32f02016-08-18 11:09:52 +0200923
924 if (update)
925 pebs_update_threshold(cpuc);
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200926}
927
Peter Zijlstra68f70822016-07-06 18:02:43 +0200928void intel_pmu_pebs_add(struct perf_event *event)
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200929{
930 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
931 struct hw_perf_event *hwc = &event->hw;
932 bool needed_cb = pebs_needs_sched_cb(cpuc);
933
934 cpuc->n_pebs++;
935 if (hwc->flags & PERF_X86_EVENT_FREERUNNING)
936 cpuc->n_large_pebs++;
937
938 pebs_update_state(needed_cb, cpuc, event->ctx->pmu);
Yan, Zheng3569c0d2015-05-06 15:33:50 -0400939}
940
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300941void intel_pmu_pebs_enable(struct perf_event *event)
Peter Zijlstraca037702010-03-02 19:52:12 +0100942{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500943 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstraef21f682010-03-03 13:12:23 +0100944 struct hw_perf_event *hwc = &event->hw;
Yan, Zheng851559e2015-05-06 15:33:47 -0400945 struct debug_store *ds = cpuc->ds;
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200946
Peter Zijlstraca037702010-03-02 19:52:12 +0100947 hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;
948
Peter Zijlstraad0e6cf2010-03-06 19:49:06 +0100949 cpuc->pebs_enabled |= 1ULL << hwc->idx;
Stephane Eranianf20093e2013-01-24 16:10:32 +0100950
951 if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
952 cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32);
Stephane Eranian9ad64c02013-01-24 16:10:34 +0100953 else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
954 cpuc->pebs_enabled |= 1ULL << 63;
Yan, Zheng851559e2015-05-06 15:33:47 -0400955
Yan, Zheng3569c0d2015-05-06 15:33:50 -0400956 /*
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200957 * Use auto-reload if possible to save a MSR write in the PMI.
958 * This must be done in pmu::start(), because PERF_EVENT_IOC_PERIOD.
Yan, Zheng3569c0d2015-05-06 15:33:50 -0400959 */
Yan, Zheng851559e2015-05-06 15:33:47 -0400960 if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
961 ds->pebs_event_reset[hwc->idx] =
962 (u64)(-hwc->sample_period) & x86_pmu.cntval_mask;
Jiri Olsadc853e22017-07-14 18:35:51 +0200963 } else {
964 ds->pebs_event_reset[hwc->idx] = 0;
Yan, Zheng851559e2015-05-06 15:33:47 -0400965 }
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200966}
Yan, Zheng3569c0d2015-05-06 15:33:50 -0400967
Peter Zijlstra68f70822016-07-06 18:02:43 +0200968void intel_pmu_pebs_del(struct perf_event *event)
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200969{
970 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
971 struct hw_perf_event *hwc = &event->hw;
972 bool needed_cb = pebs_needs_sched_cb(cpuc);
973
974 cpuc->n_pebs--;
975 if (hwc->flags & PERF_X86_EVENT_FREERUNNING)
976 cpuc->n_large_pebs--;
977
978 pebs_update_state(needed_cb, cpuc, event->ctx->pmu);
Peter Zijlstraca037702010-03-02 19:52:12 +0100979}
980
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300981void intel_pmu_pebs_disable(struct perf_event *event)
Peter Zijlstraca037702010-03-02 19:52:12 +0100982{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500983 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstraef21f682010-03-03 13:12:23 +0100984 struct hw_perf_event *hwc = &event->hw;
Liang, Kan2a853e12015-07-03 20:08:27 +0000985
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200986 if (cpuc->n_pebs == cpuc->n_large_pebs)
Liang, Kan2a853e12015-07-03 20:08:27 +0000987 intel_pmu_drain_pebs_buffer();
Peter Zijlstraca037702010-03-02 19:52:12 +0100988
Peter Zijlstraad0e6cf2010-03-06 19:49:06 +0100989 cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
Stephane Eranian983433b2013-06-21 16:20:41 +0200990
Peter Zijlstrab371b592015-05-21 10:57:13 +0200991 if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
Stephane Eranian983433b2013-06-21 16:20:41 +0200992 cpuc->pebs_enabled &= ~(1ULL << (hwc->idx + 32));
Peter Zijlstrab371b592015-05-21 10:57:13 +0200993 else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
Stephane Eranian983433b2013-06-21 16:20:41 +0200994 cpuc->pebs_enabled &= ~(1ULL << 63);
995
Peter Zijlstra4807e3d2010-03-06 13:47:07 +0100996 if (cpuc->enabled)
Peter Zijlstraad0e6cf2010-03-06 19:49:06 +0100997 wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
Peter Zijlstraca037702010-03-02 19:52:12 +0100998
999 hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
1000}
1001
Kevin Winchesterde0428a2011-08-30 20:41:05 -03001002void intel_pmu_pebs_enable_all(void)
Peter Zijlstraca037702010-03-02 19:52:12 +01001003{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001004 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstraca037702010-03-02 19:52:12 +01001005
1006 if (cpuc->pebs_enabled)
1007 wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
1008}
1009
Kevin Winchesterde0428a2011-08-30 20:41:05 -03001010void intel_pmu_pebs_disable_all(void)
Peter Zijlstraca037702010-03-02 19:52:12 +01001011{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001012 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstraca037702010-03-02 19:52:12 +01001013
1014 if (cpuc->pebs_enabled)
1015 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
1016}
1017
Peter Zijlstraef21f682010-03-03 13:12:23 +01001018static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
1019{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001020 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstraef21f682010-03-03 13:12:23 +01001021 unsigned long from = cpuc->lbr_entries[0].from;
1022 unsigned long old_to, to = cpuc->lbr_entries[0].to;
1023 unsigned long ip = regs->ip;
Peter Zijlstra57d1c0c2011-10-07 13:36:40 +02001024 int is_64bit = 0;
Peter Zijlstra9536c8d2013-10-15 12:14:04 +02001025 void *kaddr;
Dave Hansen6ba48ff2014-11-14 07:39:57 -08001026 int size;
Peter Zijlstraef21f682010-03-03 13:12:23 +01001027
Peter Zijlstra8db909a2010-03-03 17:07:40 +01001028 /*
1029 * We don't need to fixup if the PEBS assist is fault like
1030 */
1031 if (!x86_pmu.intel_cap.pebs_trap)
1032 return 1;
1033
Peter Zijlstraa562b182010-03-05 16:29:14 +01001034 /*
1035 * No LBR entry, no basic block, no rewinding
1036 */
Peter Zijlstraef21f682010-03-03 13:12:23 +01001037 if (!cpuc->lbr_stack.nr || !from || !to)
1038 return 0;
1039
Peter Zijlstraa562b182010-03-05 16:29:14 +01001040 /*
1041 * Basic blocks should never cross user/kernel boundaries
1042 */
1043 if (kernel_ip(ip) != kernel_ip(to))
1044 return 0;
1045
1046 /*
1047 * unsigned math, either ip is before the start (impossible) or
1048 * the basic block is larger than 1 page (sanity)
1049 */
Peter Zijlstra9536c8d2013-10-15 12:14:04 +02001050 if ((ip - to) > PEBS_FIXUP_SIZE)
Peter Zijlstraef21f682010-03-03 13:12:23 +01001051 return 0;
1052
1053 /*
1054 * We sampled a branch insn, rewind using the LBR stack
1055 */
1056 if (ip == to) {
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02001057 set_linear_ip(regs, from);
Peter Zijlstraef21f682010-03-03 13:12:23 +01001058 return 1;
1059 }
1060
Dave Hansen6ba48ff2014-11-14 07:39:57 -08001061 size = ip - to;
Peter Zijlstra9536c8d2013-10-15 12:14:04 +02001062 if (!kernel_ip(ip)) {
Dave Hansen6ba48ff2014-11-14 07:39:57 -08001063 int bytes;
Peter Zijlstra9536c8d2013-10-15 12:14:04 +02001064 u8 *buf = this_cpu_read(insn_buffer);
1065
Dave Hansen6ba48ff2014-11-14 07:39:57 -08001066 /* 'size' must fit our buffer, see above */
Peter Zijlstra9536c8d2013-10-15 12:14:04 +02001067 bytes = copy_from_user_nmi(buf, (void __user *)to, size);
Peter Zijlstra0a196842013-10-30 21:16:22 +01001068 if (bytes != 0)
Peter Zijlstra9536c8d2013-10-15 12:14:04 +02001069 return 0;
1070
1071 kaddr = buf;
1072 } else {
1073 kaddr = (void *)to;
1074 }
1075
Peter Zijlstraef21f682010-03-03 13:12:23 +01001076 do {
1077 struct insn insn;
Peter Zijlstraef21f682010-03-03 13:12:23 +01001078
1079 old_to = to;
Peter Zijlstraef21f682010-03-03 13:12:23 +01001080
Peter Zijlstra57d1c0c2011-10-07 13:36:40 +02001081#ifdef CONFIG_X86_64
1082 is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
1083#endif
Dave Hansen6ba48ff2014-11-14 07:39:57 -08001084 insn_init(&insn, kaddr, size, is_64bit);
Peter Zijlstraef21f682010-03-03 13:12:23 +01001085 insn_get_length(&insn);
Dave Hansen6ba48ff2014-11-14 07:39:57 -08001086 /*
1087 * Make sure there was not a problem decoding the
1088 * instruction and getting the length. This is
1089 * doubly important because we have an infinite
1090 * loop if insn.length=0.
1091 */
1092 if (!insn.length)
1093 break;
Peter Zijlstra9536c8d2013-10-15 12:14:04 +02001094
Peter Zijlstraef21f682010-03-03 13:12:23 +01001095 to += insn.length;
Peter Zijlstra9536c8d2013-10-15 12:14:04 +02001096 kaddr += insn.length;
Dave Hansen6ba48ff2014-11-14 07:39:57 -08001097 size -= insn.length;
Peter Zijlstraef21f682010-03-03 13:12:23 +01001098 } while (to < ip);
1099
1100 if (to == ip) {
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02001101 set_linear_ip(regs, old_to);
Peter Zijlstraef21f682010-03-03 13:12:23 +01001102 return 1;
1103 }
1104
Peter Zijlstraa562b182010-03-05 16:29:14 +01001105 /*
1106 * Even though we decoded the basic block, the instruction stream
1107 * never matched the given IP, either the TO or the IP got corrupted.
1108 */
Peter Zijlstraef21f682010-03-03 13:12:23 +01001109 return 0;
1110}
1111
Andi Kleen2f7ebf22015-05-10 12:22:40 -07001112static inline u64 intel_hsw_weight(struct pebs_record_skl *pebs)
Andi Kleen748e86a2013-09-05 20:37:39 -07001113{
1114 if (pebs->tsx_tuning) {
1115 union hsw_tsx_tuning tsx = { .value = pebs->tsx_tuning };
1116 return tsx.cycles_last_block;
1117 }
1118 return 0;
1119}
1120
Andi Kleen2f7ebf22015-05-10 12:22:40 -07001121static inline u64 intel_hsw_transaction(struct pebs_record_skl *pebs)
Andi Kleena405bad2013-09-20 07:40:40 -07001122{
1123 u64 txn = (pebs->tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32;
1124
1125 /* For RTM XABORTs also log the abort code from AX */
1126 if ((txn & PERF_TXN_TRANSACTION) && (pebs->ax & 1))
1127 txn |= ((pebs->ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
1128 return txn;
1129}
1130
Yan, Zheng43cf7632015-05-06 15:33:48 -04001131static void setup_pebs_sample_data(struct perf_event *event,
1132 struct pt_regs *iregs, void *__pebs,
1133 struct perf_sample_data *data,
1134 struct pt_regs *regs)
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001135{
Stephane Eranianc8aab2e2014-08-11 21:27:13 +02001136#define PERF_X86_EVENT_PEBS_HSW_PREC \
1137 (PERF_X86_EVENT_PEBS_ST_HSW | \
1138 PERF_X86_EVENT_PEBS_LD_HSW | \
1139 PERF_X86_EVENT_PEBS_NA_HSW)
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001140 /*
Peter Zijlstrad2beea42013-09-12 13:00:47 +02001141 * We cast to the biggest pebs_record but are careful not to
1142 * unconditionally access the 'extra' entries.
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001143 */
Christoph Lameter89cbc762014-08-17 12:30:40 -05001144 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Andi Kleen2f7ebf22015-05-10 12:22:40 -07001145 struct pebs_record_skl *pebs = __pebs;
Stephane Eranianf20093e2013-01-24 16:10:32 +01001146 u64 sample_type;
Stephane Eranianc8aab2e2014-08-11 21:27:13 +02001147 int fll, fst, dsrc;
1148 int fl = event->hw.flags;
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001149
Yan, Zheng21509082015-05-06 15:33:49 -04001150 if (pebs == NULL)
1151 return;
1152
Stephane Eranianc8aab2e2014-08-11 21:27:13 +02001153 sample_type = event->attr.sample_type;
1154 dsrc = sample_type & PERF_SAMPLE_DATA_SRC;
1155
1156 fll = fl & PERF_X86_EVENT_PEBS_LDLAT;
1157 fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC);
Stephane Eranianf20093e2013-01-24 16:10:32 +01001158
Yan, Zheng43cf7632015-05-06 15:33:48 -04001159 perf_sample_data_init(data, 0, event->hw.last_period);
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001160
Yan, Zheng43cf7632015-05-06 15:33:48 -04001161 data->period = event->hw.last_period;
Stephane Eranianf20093e2013-01-24 16:10:32 +01001162
1163 /*
Stephane Eranianc8aab2e2014-08-11 21:27:13 +02001164 * Use latency for weight (only avail with PEBS-LL)
Stephane Eranianf20093e2013-01-24 16:10:32 +01001165 */
Stephane Eranianc8aab2e2014-08-11 21:27:13 +02001166 if (fll && (sample_type & PERF_SAMPLE_WEIGHT))
Yan, Zheng43cf7632015-05-06 15:33:48 -04001167 data->weight = pebs->lat;
Stephane Eranianf20093e2013-01-24 16:10:32 +01001168
Stephane Eranianc8aab2e2014-08-11 21:27:13 +02001169 /*
1170 * data.data_src encodes the data source
1171 */
1172 if (dsrc) {
1173 u64 val = PERF_MEM_NA;
1174 if (fll)
1175 val = load_latency_data(pebs->dse);
1176 else if (fst && (fl & PERF_X86_EVENT_PEBS_HSW_PREC))
1177 val = precise_datala_hsw(event, pebs->dse);
1178 else if (fst)
1179 val = precise_store_data(pebs->dse);
Yan, Zheng43cf7632015-05-06 15:33:48 -04001180 data->data_src.val = val;
Stephane Eranianf20093e2013-01-24 16:10:32 +01001181 }
1182
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001183 /*
Peter Zijlstrab8000582016-11-17 18:17:31 +01001184 * We use the interrupt regs as a base because the PEBS record does not
1185 * contain a full regs set, specifically it seems to lack segment
1186 * descriptors, which get used by things like user_mode().
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001187 *
Peter Zijlstrab8000582016-11-17 18:17:31 +01001188 * In the simple case fix up only the IP for PERF_SAMPLE_IP.
1189 *
1190 * We must however always use BP,SP from iregs for the unwinder to stay
1191 * sane; the record BP,SP can point into thin air when the record is
1192 * from a previous PMI context or an (I)RET happend between the record
1193 * and PMI.
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001194 */
Yan, Zheng43cf7632015-05-06 15:33:48 -04001195 *regs = *iregs;
1196 regs->flags = pebs->flags;
1197 set_linear_ip(regs, pebs->ip);
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001198
Stephane Eranianaea48552014-09-24 13:48:38 +02001199 if (sample_type & PERF_SAMPLE_REGS_INTR) {
Yan, Zheng43cf7632015-05-06 15:33:48 -04001200 regs->ax = pebs->ax;
1201 regs->bx = pebs->bx;
1202 regs->cx = pebs->cx;
1203 regs->dx = pebs->dx;
1204 regs->si = pebs->si;
1205 regs->di = pebs->di;
Stephane Eranianaea48552014-09-24 13:48:38 +02001206
Peter Zijlstrab8000582016-11-17 18:17:31 +01001207 /*
1208 * Per the above; only set BP,SP if we don't need callchains.
1209 *
1210 * XXX: does this make sense?
1211 */
1212 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1213 regs->bp = pebs->bp;
1214 regs->sp = pebs->sp;
1215 }
1216
1217 /*
1218 * Preserve PERF_EFLAGS_VM from set_linear_ip().
1219 */
1220 regs->flags = pebs->flags | (regs->flags & PERF_EFLAGS_VM);
Stephane Eranianaea48552014-09-24 13:48:38 +02001221#ifndef CONFIG_X86_32
Yan, Zheng43cf7632015-05-06 15:33:48 -04001222 regs->r8 = pebs->r8;
1223 regs->r9 = pebs->r9;
1224 regs->r10 = pebs->r10;
1225 regs->r11 = pebs->r11;
1226 regs->r12 = pebs->r12;
1227 regs->r13 = pebs->r13;
1228 regs->r14 = pebs->r14;
1229 regs->r15 = pebs->r15;
Stephane Eranianaea48552014-09-24 13:48:38 +02001230#endif
1231 }
1232
Andi Kleen130768b2013-06-17 17:36:47 -07001233 if (event->attr.precise_ip > 1 && x86_pmu.intel_cap.pebs_format >= 2) {
Yan, Zheng43cf7632015-05-06 15:33:48 -04001234 regs->ip = pebs->real_ip;
1235 regs->flags |= PERF_EFLAGS_EXACT;
1236 } else if (event->attr.precise_ip > 1 && intel_pmu_pebs_fixup_ip(regs))
1237 regs->flags |= PERF_EFLAGS_EXACT;
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001238 else
Yan, Zheng43cf7632015-05-06 15:33:48 -04001239 regs->flags &= ~PERF_EFLAGS_EXACT;
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001240
Kan Liangfc7ce9c2017-08-28 20:52:49 -04001241 if ((sample_type & (PERF_SAMPLE_ADDR | PERF_SAMPLE_PHYS_ADDR)) &&
Peter Zijlstrad2beea42013-09-12 13:00:47 +02001242 x86_pmu.intel_cap.pebs_format >= 1)
Yan, Zheng43cf7632015-05-06 15:33:48 -04001243 data->addr = pebs->dla;
Andi Kleenf9134f32013-06-17 17:36:52 -07001244
Andi Kleena405bad2013-09-20 07:40:40 -07001245 if (x86_pmu.intel_cap.pebs_format >= 2) {
1246 /* Only set the TSX weight when no memory weight. */
Stephane Eranianc8aab2e2014-08-11 21:27:13 +02001247 if ((sample_type & PERF_SAMPLE_WEIGHT) && !fll)
Yan, Zheng43cf7632015-05-06 15:33:48 -04001248 data->weight = intel_hsw_weight(pebs);
Andi Kleena405bad2013-09-20 07:40:40 -07001249
Stephane Eranianc8aab2e2014-08-11 21:27:13 +02001250 if (sample_type & PERF_SAMPLE_TRANSACTION)
Yan, Zheng43cf7632015-05-06 15:33:48 -04001251 data->txn = intel_hsw_transaction(pebs);
Andi Kleena405bad2013-09-20 07:40:40 -07001252 }
Andi Kleen748e86a2013-09-05 20:37:39 -07001253
Andi Kleen2f7ebf22015-05-10 12:22:40 -07001254 /*
1255 * v3 supplies an accurate time stamp, so we use that
1256 * for the time stamp.
1257 *
1258 * We can only do this for the default trace clock.
1259 */
1260 if (x86_pmu.intel_cap.pebs_format >= 3 &&
1261 event->attr.use_clockid == 0)
1262 data->time = native_sched_clock_from_tsc(pebs->tsc);
1263
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001264 if (has_branch_stack(event))
Yan, Zheng43cf7632015-05-06 15:33:48 -04001265 data->br_stack = &cpuc->lbr_stack;
1266}
1267
Yan, Zheng21509082015-05-06 15:33:49 -04001268static inline void *
1269get_next_pebs_record_by_bit(void *base, void *top, int bit)
1270{
1271 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1272 void *at;
1273 u64 pebs_status;
1274
Stephane Eranian1424a092015-12-03 23:33:18 +01001275 /*
1276 * fmt0 does not have a status bitfield (does not use
1277 * perf_record_nhm format)
1278 */
1279 if (x86_pmu.intel_cap.pebs_format < 1)
1280 return base;
1281
Yan, Zheng21509082015-05-06 15:33:49 -04001282 if (base == NULL)
1283 return NULL;
1284
1285 for (at = base; at < top; at += x86_pmu.pebs_record_size) {
1286 struct pebs_record_nhm *p = at;
1287
1288 if (test_bit(bit, (unsigned long *)&p->status)) {
Peter Zijlstraa3d86542015-05-12 15:18:18 +02001289 /* PEBS v3 has accurate status bits */
1290 if (x86_pmu.intel_cap.pebs_format >= 3)
1291 return at;
Yan, Zheng21509082015-05-06 15:33:49 -04001292
1293 if (p->status == (1 << bit))
1294 return at;
1295
1296 /* clear non-PEBS bit and re-check */
1297 pebs_status = p->status & cpuc->pebs_enabled;
Kan Liangfd583ad2017-04-04 15:14:06 -04001298 pebs_status &= PEBS_COUNTER_MASK;
Yan, Zheng21509082015-05-06 15:33:49 -04001299 if (pebs_status == (1 << bit))
1300 return at;
1301 }
1302 }
1303 return NULL;
1304}
1305
Yan, Zheng43cf7632015-05-06 15:33:48 -04001306static void __intel_pmu_pebs_event(struct perf_event *event,
Yan, Zheng21509082015-05-06 15:33:49 -04001307 struct pt_regs *iregs,
1308 void *base, void *top,
1309 int bit, int count)
Yan, Zheng43cf7632015-05-06 15:33:48 -04001310{
1311 struct perf_sample_data data;
1312 struct pt_regs regs;
Yan, Zheng21509082015-05-06 15:33:49 -04001313 void *at = get_next_pebs_record_by_bit(base, top, bit);
Yan, Zheng43cf7632015-05-06 15:33:48 -04001314
Yan, Zheng21509082015-05-06 15:33:49 -04001315 if (!intel_pmu_save_and_restart(event) &&
1316 !(event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD))
Yan, Zheng43cf7632015-05-06 15:33:48 -04001317 return;
1318
Peter Zijlstraa3d86542015-05-12 15:18:18 +02001319 while (count > 1) {
1320 setup_pebs_sample_data(event, iregs, at, &data, &regs);
1321 perf_event_output(event, &data, &regs);
1322 at += x86_pmu.pebs_record_size;
1323 at = get_next_pebs_record_by_bit(at, top, bit);
1324 count--;
Yan, Zheng21509082015-05-06 15:33:49 -04001325 }
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001326
Yan, Zheng21509082015-05-06 15:33:49 -04001327 setup_pebs_sample_data(event, iregs, at, &data, &regs);
1328
1329 /*
1330 * All but the last records are processed.
1331 * The last one is left to be able to call the overflow handler.
1332 */
1333 if (perf_event_overflow(event, &data, &regs)) {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001334 x86_pmu_stop(event, 0);
Yan, Zheng21509082015-05-06 15:33:49 -04001335 return;
1336 }
1337
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001338}
1339
Peter Zijlstraca037702010-03-02 19:52:12 +01001340static void intel_pmu_drain_pebs_core(struct pt_regs *iregs)
1341{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001342 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstraca037702010-03-02 19:52:12 +01001343 struct debug_store *ds = cpuc->ds;
1344 struct perf_event *event = cpuc->events[0]; /* PMC0 only */
1345 struct pebs_record_core *at, *top;
Peter Zijlstraca037702010-03-02 19:52:12 +01001346 int n;
1347
Peter Zijlstra6809b6e2010-10-19 14:22:50 +02001348 if (!x86_pmu.pebs_active)
Peter Zijlstraca037702010-03-02 19:52:12 +01001349 return;
1350
Peter Zijlstraca037702010-03-02 19:52:12 +01001351 at = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base;
1352 top = (struct pebs_record_core *)(unsigned long)ds->pebs_index;
1353
Peter Zijlstrad80c7502010-03-09 11:41:02 +01001354 /*
1355 * Whatever else happens, drain the thing
1356 */
1357 ds->pebs_index = ds->pebs_buffer_base;
1358
1359 if (!test_bit(0, cpuc->active_mask))
Peter Zijlstra8f4aebd2010-03-06 13:26:11 +01001360 return;
Peter Zijlstraca037702010-03-02 19:52:12 +01001361
Peter Zijlstrad80c7502010-03-09 11:41:02 +01001362 WARN_ON_ONCE(!event);
1363
Peter Zijlstraab608342010-04-08 23:03:20 +02001364 if (!event->attr.precise_ip)
Peter Zijlstrad80c7502010-03-09 11:41:02 +01001365 return;
1366
Stephane Eranian1424a092015-12-03 23:33:18 +01001367 n = top - at;
Peter Zijlstrad80c7502010-03-09 11:41:02 +01001368 if (n <= 0)
1369 return;
Peter Zijlstraca037702010-03-02 19:52:12 +01001370
Yan, Zheng21509082015-05-06 15:33:49 -04001371 __intel_pmu_pebs_event(event, iregs, at, top, 0, n);
Peter Zijlstraca037702010-03-02 19:52:12 +01001372}
1373
Peter Zijlstrad2beea42013-09-12 13:00:47 +02001374static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
Peter Zijlstraca037702010-03-02 19:52:12 +01001375{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001376 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstraca037702010-03-02 19:52:12 +01001377 struct debug_store *ds = cpuc->ds;
Yan, Zheng21509082015-05-06 15:33:49 -04001378 struct perf_event *event;
1379 void *base, *at, *top;
Yan, Zheng21509082015-05-06 15:33:49 -04001380 short counts[MAX_PEBS_EVENTS] = {};
Kan Liangf38b0db2015-05-10 15:13:14 -04001381 short error[MAX_PEBS_EVENTS] = {};
Peter Zijlstraa3d86542015-05-12 15:18:18 +02001382 int bit, i;
Peter Zijlstrad2beea42013-09-12 13:00:47 +02001383
1384 if (!x86_pmu.pebs_active)
1385 return;
1386
Yan, Zheng21509082015-05-06 15:33:49 -04001387 base = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
Peter Zijlstrad2beea42013-09-12 13:00:47 +02001388 top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
Peter Zijlstraca037702010-03-02 19:52:12 +01001389
Peter Zijlstraca037702010-03-02 19:52:12 +01001390 ds->pebs_index = ds->pebs_buffer_base;
1391
Yan, Zheng21509082015-05-06 15:33:49 -04001392 if (unlikely(base >= top))
Peter Zijlstrad2beea42013-09-12 13:00:47 +02001393 return;
1394
Yan, Zheng21509082015-05-06 15:33:49 -04001395 for (at = base; at < top; at += x86_pmu.pebs_record_size) {
Andi Kleen130768b2013-06-17 17:36:47 -07001396 struct pebs_record_nhm *p = at;
Peter Zijlstra75f80852015-07-15 14:35:46 +02001397 u64 pebs_status;
Peter Zijlstraca037702010-03-02 19:52:12 +01001398
Peter Zijlstra8ef9b842016-09-07 14:42:55 +02001399 pebs_status = p->status & cpuc->pebs_enabled;
1400 pebs_status &= (1ULL << x86_pmu.max_pebs_events) - 1;
1401
1402 /* PEBS v3 has more accurate status bits */
Peter Zijlstraa3d86542015-05-12 15:18:18 +02001403 if (x86_pmu.intel_cap.pebs_format >= 3) {
Peter Zijlstra8ef9b842016-09-07 14:42:55 +02001404 for_each_set_bit(bit, (unsigned long *)&pebs_status,
1405 x86_pmu.max_pebs_events)
Peter Zijlstraa3d86542015-05-12 15:18:18 +02001406 counts[bit]++;
1407
1408 continue;
1409 }
1410
Andi Kleen01330d72015-12-03 13:22:20 -08001411 /*
1412 * On some CPUs the PEBS status can be zero when PEBS is
1413 * racing with clearing of GLOBAL_STATUS.
1414 *
1415 * Normally we would drop that record, but in the
1416 * case when there is only a single active PEBS event
1417 * we can assume it's for that event.
1418 */
1419 if (!pebs_status && cpuc->pebs_enabled &&
1420 !(cpuc->pebs_enabled & (cpuc->pebs_enabled-1)))
1421 pebs_status = cpuc->pebs_enabled;
1422
Peter Zijlstra75f80852015-07-15 14:35:46 +02001423 bit = find_first_bit((unsigned long *)&pebs_status,
Yan, Zheng21509082015-05-06 15:33:49 -04001424 x86_pmu.max_pebs_events);
Andi Kleen957ea1f2015-12-03 13:22:19 -08001425 if (bit >= x86_pmu.max_pebs_events)
Peter Zijlstraca037702010-03-02 19:52:12 +01001426 continue;
Peter Zijlstra75f80852015-07-15 14:35:46 +02001427
Yan, Zheng21509082015-05-06 15:33:49 -04001428 /*
1429 * The PEBS hardware does not deal well with the situation
1430 * when events happen near to each other and multiple bits
1431 * are set. But it should happen rarely.
1432 *
1433 * If these events include one PEBS and multiple non-PEBS
1434 * events, it doesn't impact PEBS record. The record will
1435 * be handled normally. (slow path)
1436 *
1437 * If these events include two or more PEBS events, the
1438 * records for the events can be collapsed into a single
1439 * one, and it's not possible to reconstruct all events
1440 * that caused the PEBS record. It's called collision.
1441 * If collision happened, the record will be dropped.
Yan, Zheng21509082015-05-06 15:33:49 -04001442 */
Peter Zijlstra75f80852015-07-15 14:35:46 +02001443 if (p->status != (1ULL << bit)) {
1444 for_each_set_bit(i, (unsigned long *)&pebs_status,
1445 x86_pmu.max_pebs_events)
1446 error[i]++;
1447 continue;
Yan, Zheng21509082015-05-06 15:33:49 -04001448 }
Peter Zijlstra75f80852015-07-15 14:35:46 +02001449
Yan, Zheng21509082015-05-06 15:33:49 -04001450 counts[bit]++;
1451 }
1452
1453 for (bit = 0; bit < x86_pmu.max_pebs_events; bit++) {
Kan Liangf38b0db2015-05-10 15:13:14 -04001454 if ((counts[bit] == 0) && (error[bit] == 0))
Yan, Zheng21509082015-05-06 15:33:49 -04001455 continue;
Peter Zijlstra75f80852015-07-15 14:35:46 +02001456
Yan, Zheng21509082015-05-06 15:33:49 -04001457 event = cpuc->events[bit];
Peter Zijlstra8ef9b842016-09-07 14:42:55 +02001458 if (WARN_ON_ONCE(!event))
1459 continue;
1460
1461 if (WARN_ON_ONCE(!event->attr.precise_ip))
1462 continue;
Yan, Zheng21509082015-05-06 15:33:49 -04001463
Kan Liangf38b0db2015-05-10 15:13:14 -04001464 /* log dropped samples number */
Jiri Olsa475113d2016-12-28 14:31:03 +01001465 if (error[bit]) {
Kan Liangf38b0db2015-05-10 15:13:14 -04001466 perf_log_lost_samples(event, error[bit]);
1467
Jiri Olsa475113d2016-12-28 14:31:03 +01001468 if (perf_event_account_interrupt(event))
1469 x86_pmu_stop(event, 0);
1470 }
1471
Kan Liangf38b0db2015-05-10 15:13:14 -04001472 if (counts[bit]) {
1473 __intel_pmu_pebs_event(event, iregs, base,
1474 top, bit, counts[bit]);
1475 }
Peter Zijlstraca037702010-03-02 19:52:12 +01001476 }
Peter Zijlstraca037702010-03-02 19:52:12 +01001477}
1478
1479/*
1480 * BTS, PEBS probe and setup
1481 */
1482
Mathias Krause066ce642014-08-26 18:49:45 +02001483void __init intel_ds_init(void)
Peter Zijlstraca037702010-03-02 19:52:12 +01001484{
1485 /*
1486 * No support for 32bit formats
1487 */
1488 if (!boot_cpu_has(X86_FEATURE_DTES64))
1489 return;
1490
1491 x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS);
1492 x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS);
Jiri Olsae72daf32016-03-01 20:03:52 +01001493 x86_pmu.pebs_buffer_size = PEBS_BUFFER_SIZE;
Peter Zijlstraca037702010-03-02 19:52:12 +01001494 if (x86_pmu.pebs) {
Peter Zijlstra8db909a2010-03-03 17:07:40 +01001495 char pebs_type = x86_pmu.intel_cap.pebs_trap ? '+' : '-';
1496 int format = x86_pmu.intel_cap.pebs_format;
Peter Zijlstraca037702010-03-02 19:52:12 +01001497
1498 switch (format) {
1499 case 0:
Chen Yucong1b74dde2016-02-02 11:45:02 +08001500 pr_cont("PEBS fmt0%c, ", pebs_type);
Peter Zijlstraca037702010-03-02 19:52:12 +01001501 x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
Jiri Olsae72daf32016-03-01 20:03:52 +01001502 /*
1503 * Using >PAGE_SIZE buffers makes the WRMSR to
1504 * PERF_GLOBAL_CTRL in intel_pmu_enable_all()
1505 * mysteriously hang on Core2.
1506 *
1507 * As a workaround, we don't do this.
1508 */
1509 x86_pmu.pebs_buffer_size = PAGE_SIZE;
Peter Zijlstraca037702010-03-02 19:52:12 +01001510 x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
Peter Zijlstraca037702010-03-02 19:52:12 +01001511 break;
1512
1513 case 1:
Chen Yucong1b74dde2016-02-02 11:45:02 +08001514 pr_cont("PEBS fmt1%c, ", pebs_type);
Peter Zijlstraca037702010-03-02 19:52:12 +01001515 x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
1516 x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
Peter Zijlstraca037702010-03-02 19:52:12 +01001517 break;
1518
Andi Kleen130768b2013-06-17 17:36:47 -07001519 case 2:
1520 pr_cont("PEBS fmt2%c, ", pebs_type);
1521 x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw);
Peter Zijlstrad2beea42013-09-12 13:00:47 +02001522 x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
Andi Kleen130768b2013-06-17 17:36:47 -07001523 break;
1524
Andi Kleen2f7ebf22015-05-10 12:22:40 -07001525 case 3:
1526 pr_cont("PEBS fmt3%c, ", pebs_type);
1527 x86_pmu.pebs_record_size =
1528 sizeof(struct pebs_record_skl);
1529 x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
Andi Kleena7b58d22015-05-27 21:13:14 -07001530 x86_pmu.free_running_flags |= PERF_SAMPLE_TIME;
Andi Kleen2f7ebf22015-05-10 12:22:40 -07001531 break;
1532
Peter Zijlstraca037702010-03-02 19:52:12 +01001533 default:
Chen Yucong1b74dde2016-02-02 11:45:02 +08001534 pr_cont("no PEBS fmt%d%c, ", format, pebs_type);
Peter Zijlstraca037702010-03-02 19:52:12 +01001535 x86_pmu.pebs = 0;
Peter Zijlstraca037702010-03-02 19:52:12 +01001536 }
1537 }
1538}
Stephane Eranian1d9d8632013-03-15 14:26:07 +01001539
1540void perf_restore_debug_store(void)
1541{
Linus Torvalds2a6e06b2013-03-17 15:44:43 -07001542 struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
1543
Stephane Eranian1d9d8632013-03-15 14:26:07 +01001544 if (!x86_pmu.bts && !x86_pmu.pebs)
1545 return;
1546
Linus Torvalds2a6e06b2013-03-17 15:44:43 -07001547 wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds);
Stephane Eranian1d9d8632013-03-15 14:26:07 +01001548}