blob: 6522f0279cb89ba73fc8f74b47f5da37bc932669 [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
Kevin Winchesterde0428a2011-08-30 20:41:05 -03006#include <asm/perf_event.h>
Stephane Eranian3e702ff2012-02-09 23:20:58 +01007#include <asm/insn.h>
Kevin Winchesterde0428a2011-08-30 20:41:05 -03008
Borislav Petkov27f6d222016-02-10 10:55:23 +01009#include "../perf_event.h"
Peter Zijlstraca037702010-03-02 19:52:12 +010010
Thomas Gleixner10043e02017-12-04 15:07:49 +010011/* Waste a full page so it can be mapped into the cpu_entry_area */
12DEFINE_PER_CPU_PAGE_ALIGNED(struct debug_store, cpu_debug_store);
13
Peter Zijlstraca037702010-03-02 19:52:12 +010014/* The size of a BTS record in bytes: */
15#define BTS_RECORD_SIZE 24
16
Peter Zijlstra9536c8d2013-10-15 12:14:04 +020017#define PEBS_FIXUP_SIZE PAGE_SIZE
Peter Zijlstraca037702010-03-02 19:52:12 +010018
19/*
20 * pebs_record_32 for p4 and core not supported
21
22struct pebs_record_32 {
23 u32 flags, ip;
24 u32 ax, bc, cx, dx;
25 u32 si, di, bp, sp;
26};
27
28 */
29
Stephane Eranianf20093e2013-01-24 16:10:32 +010030union intel_x86_pebs_dse {
31 u64 val;
32 struct {
33 unsigned int ld_dse:4;
34 unsigned int ld_stlb_miss:1;
35 unsigned int ld_locked:1;
36 unsigned int ld_reserved:26;
37 };
38 struct {
39 unsigned int st_l1d_hit:1;
40 unsigned int st_reserved1:3;
41 unsigned int st_stlb_miss:1;
42 unsigned int st_locked:1;
43 unsigned int st_reserved2:26;
44 };
45};
46
47
48/*
49 * Map PEBS Load Latency Data Source encodings to generic
50 * memory data source information
51 */
52#define P(a, b) PERF_MEM_S(a, b)
53#define OP_LH (P(OP, LOAD) | P(LVL, HIT))
Andi Kleen6ae5fa62017-08-16 15:21:54 -070054#define LEVEL(x) P(LVLNUM, x)
55#define REM P(REMOTE, REMOTE)
Stephane Eranianf20093e2013-01-24 16:10:32 +010056#define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS))
57
Andi Kleene17dc652016-03-01 14:25:24 -080058/* Version for Sandy Bridge and later */
59static u64 pebs_data_source[] = {
Andi Kleen6ae5fa62017-08-16 15:21:54 -070060 P(OP, LOAD) | P(LVL, MISS) | LEVEL(L3) | P(SNOOP, NA),/* 0x00:ukn L3 */
61 OP_LH | P(LVL, L1) | LEVEL(L1) | P(SNOOP, NONE), /* 0x01: L1 local */
62 OP_LH | P(LVL, LFB) | LEVEL(LFB) | P(SNOOP, NONE), /* 0x02: LFB hit */
63 OP_LH | P(LVL, L2) | LEVEL(L2) | P(SNOOP, NONE), /* 0x03: L2 hit */
64 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, NONE), /* 0x04: L3 hit */
65 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, MISS), /* 0x05: L3 hit, snoop miss */
66 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT), /* 0x06: L3 hit, snoop hit */
67 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM), /* 0x07: L3 hit, snoop hitm */
68 OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HIT), /* 0x08: L3 miss snoop hit */
69 OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HITM), /* 0x09: L3 miss snoop hitm*/
70 OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | P(SNOOP, HIT), /* 0x0a: L3 miss, shared */
71 OP_LH | P(LVL, REM_RAM1) | REM | LEVEL(L3) | P(SNOOP, HIT), /* 0x0b: L3 miss, shared */
72 OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | SNOOP_NONE_MISS, /* 0x0c: L3 miss, excl */
73 OP_LH | P(LVL, REM_RAM1) | LEVEL(RAM) | REM | SNOOP_NONE_MISS, /* 0x0d: L3 miss, excl */
74 OP_LH | P(LVL, IO) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0e: I/O */
75 OP_LH | P(LVL, UNC) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0f: uncached */
Stephane Eranianf20093e2013-01-24 16:10:32 +010076};
77
Andi Kleene17dc652016-03-01 14:25:24 -080078/* Patch up minor differences in the bits */
79void __init intel_pmu_pebs_data_source_nhm(void)
80{
Andi Kleen6ae5fa62017-08-16 15:21:54 -070081 pebs_data_source[0x05] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT);
82 pebs_data_source[0x06] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
83 pebs_data_source[0x07] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
84}
85
86void __init intel_pmu_pebs_data_source_skl(bool pmem)
87{
88 u64 pmem_or_l4 = pmem ? LEVEL(PMEM) : LEVEL(L4);
89
90 pebs_data_source[0x08] = OP_LH | pmem_or_l4 | P(SNOOP, HIT);
91 pebs_data_source[0x09] = OP_LH | pmem_or_l4 | REM | P(SNOOP, HIT);
92 pebs_data_source[0x0b] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, NONE);
93 pebs_data_source[0x0c] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOPX, FWD);
94 pebs_data_source[0x0d] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOP, HITM);
Andi Kleene17dc652016-03-01 14:25:24 -080095}
96
Stephane Eranian9ad64c02013-01-24 16:10:34 +010097static u64 precise_store_data(u64 status)
98{
99 union intel_x86_pebs_dse dse;
100 u64 val = P(OP, STORE) | P(SNOOP, NA) | P(LVL, L1) | P(TLB, L2);
101
102 dse.val = status;
103
104 /*
105 * bit 4: TLB access
106 * 1 = stored missed 2nd level TLB
107 *
108 * so it either hit the walker or the OS
109 * otherwise hit 2nd level TLB
110 */
111 if (dse.st_stlb_miss)
112 val |= P(TLB, MISS);
113 else
114 val |= P(TLB, HIT);
115
116 /*
117 * bit 0: hit L1 data cache
118 * if not set, then all we know is that
119 * it missed L1D
120 */
121 if (dse.st_l1d_hit)
122 val |= P(LVL, HIT);
123 else
124 val |= P(LVL, MISS);
125
126 /*
127 * bit 5: Locked prefix
128 */
129 if (dse.st_locked)
130 val |= P(LOCK, LOCKED);
131
132 return val;
133}
134
Stephane Eranianc8aab2e2014-08-11 21:27:13 +0200135static u64 precise_datala_hsw(struct perf_event *event, u64 status)
Andi Kleenf9134f32013-06-17 17:36:52 -0700136{
137 union perf_mem_data_src dse;
138
Stephane Eranian770eee12014-08-11 21:27:12 +0200139 dse.val = PERF_MEM_NA;
140
141 if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW)
142 dse.mem_op = PERF_MEM_OP_STORE;
143 else if (event->hw.flags & PERF_X86_EVENT_PEBS_LD_HSW)
144 dse.mem_op = PERF_MEM_OP_LOAD;
Stephane Eranian722e76e2014-05-15 17:56:44 +0200145
146 /*
147 * L1 info only valid for following events:
148 *
149 * MEM_UOPS_RETIRED.STLB_MISS_STORES
150 * MEM_UOPS_RETIRED.LOCK_STORES
151 * MEM_UOPS_RETIRED.SPLIT_STORES
152 * MEM_UOPS_RETIRED.ALL_STORES
153 */
Stephane Eranianc8aab2e2014-08-11 21:27:13 +0200154 if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) {
155 if (status & 1)
156 dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_HIT;
157 else
158 dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_MISS;
159 }
Andi Kleenf9134f32013-06-17 17:36:52 -0700160 return dse.val;
161}
162
Stephane Eranianf20093e2013-01-24 16:10:32 +0100163static u64 load_latency_data(u64 status)
164{
165 union intel_x86_pebs_dse dse;
166 u64 val;
Stephane Eranianf20093e2013-01-24 16:10:32 +0100167
168 dse.val = status;
169
170 /*
171 * use the mapping table for bit 0-3
172 */
173 val = pebs_data_source[dse.ld_dse];
174
175 /*
176 * Nehalem models do not support TLB, Lock infos
177 */
Andi Kleen95298352017-08-16 15:21:53 -0700178 if (x86_pmu.pebs_no_tlb) {
Stephane Eranianf20093e2013-01-24 16:10:32 +0100179 val |= P(TLB, NA) | P(LOCK, NA);
180 return val;
181 }
182 /*
183 * bit 4: TLB access
184 * 0 = did not miss 2nd level TLB
185 * 1 = missed 2nd level TLB
186 */
187 if (dse.ld_stlb_miss)
188 val |= P(TLB, MISS) | P(TLB, L2);
189 else
190 val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2);
191
192 /*
193 * bit 5: locked prefix
194 */
195 if (dse.ld_locked)
196 val |= P(LOCK, LOCKED);
197
198 return val;
199}
200
Peter Zijlstraca037702010-03-02 19:52:12 +0100201struct pebs_record_core {
202 u64 flags, ip;
203 u64 ax, bx, cx, dx;
204 u64 si, di, bp, sp;
205 u64 r8, r9, r10, r11;
206 u64 r12, r13, r14, r15;
207};
208
209struct pebs_record_nhm {
210 u64 flags, ip;
211 u64 ax, bx, cx, dx;
212 u64 si, di, bp, sp;
213 u64 r8, r9, r10, r11;
214 u64 r12, r13, r14, r15;
215 u64 status, dla, dse, lat;
216};
217
Andi Kleen130768b2013-06-17 17:36:47 -0700218/*
219 * Same as pebs_record_nhm, with two additional fields.
220 */
221struct pebs_record_hsw {
Andi Kleen748e86a2013-09-05 20:37:39 -0700222 u64 flags, ip;
223 u64 ax, bx, cx, dx;
224 u64 si, di, bp, sp;
225 u64 r8, r9, r10, r11;
226 u64 r12, r13, r14, r15;
227 u64 status, dla, dse, lat;
Peter Zijlstrad2beea42013-09-12 13:00:47 +0200228 u64 real_ip, tsx_tuning;
Andi Kleen748e86a2013-09-05 20:37:39 -0700229};
230
231union hsw_tsx_tuning {
232 struct {
233 u32 cycles_last_block : 32,
234 hle_abort : 1,
235 rtm_abort : 1,
236 instruction_abort : 1,
237 non_instruction_abort : 1,
238 retry : 1,
239 data_conflict : 1,
240 capacity_writes : 1,
241 capacity_reads : 1;
242 };
243 u64 value;
Andi Kleen130768b2013-06-17 17:36:47 -0700244};
245
Andi Kleena405bad2013-09-20 07:40:40 -0700246#define PEBS_HSW_TSX_FLAGS 0xff00000000ULL
247
Andi Kleen2f7ebf22015-05-10 12:22:40 -0700248/* Same as HSW, plus TSC */
249
250struct pebs_record_skl {
251 u64 flags, ip;
252 u64 ax, bx, cx, dx;
253 u64 si, di, bp, sp;
254 u64 r8, r9, r10, r11;
255 u64 r12, r13, r14, r15;
256 u64 status, dla, dse, lat;
257 u64 real_ip, tsx_tuning;
258 u64 tsc;
259};
260
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300261void init_debug_store_on_cpu(int cpu)
Peter Zijlstraca037702010-03-02 19:52:12 +0100262{
263 struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
264
265 if (!ds)
266 return;
267
268 wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA,
269 (u32)((u64)(unsigned long)ds),
270 (u32)((u64)(unsigned long)ds >> 32));
271}
272
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300273void fini_debug_store_on_cpu(int cpu)
Peter Zijlstraca037702010-03-02 19:52:12 +0100274{
275 if (!per_cpu(cpu_hw_events, cpu).ds)
276 return;
277
278 wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0);
279}
280
Peter Zijlstra9536c8d2013-10-15 12:14:04 +0200281static DEFINE_PER_CPU(void *, insn_buffer);
282
Peter Zijlstra5ee25c82010-10-19 14:15:04 +0200283static int alloc_pebs_buffer(int cpu)
284{
285 struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
Peter Zijlstra96681fc2010-10-19 14:55:33 +0200286 int node = cpu_to_node(cpu);
Yan, Zheng3569c0d2015-05-06 15:33:50 -0400287 int max;
Peter Zijlstra9536c8d2013-10-15 12:14:04 +0200288 void *buffer, *ibuffer;
Peter Zijlstra5ee25c82010-10-19 14:15:04 +0200289
290 if (!x86_pmu.pebs)
291 return 0;
292
Jiri Olsae72daf32016-03-01 20:03:52 +0100293 buffer = kzalloc_node(x86_pmu.pebs_buffer_size, GFP_KERNEL, node);
Peter Zijlstra5ee25c82010-10-19 14:15:04 +0200294 if (unlikely(!buffer))
295 return -ENOMEM;
296
Peter Zijlstra9536c8d2013-10-15 12:14:04 +0200297 /*
298 * HSW+ already provides us the eventing ip; no need to allocate this
299 * buffer then.
300 */
301 if (x86_pmu.intel_cap.pebs_format < 2) {
302 ibuffer = kzalloc_node(PEBS_FIXUP_SIZE, GFP_KERNEL, node);
303 if (!ibuffer) {
304 kfree(buffer);
305 return -ENOMEM;
306 }
307 per_cpu(insn_buffer, cpu) = ibuffer;
308 }
309
Jiri Olsae72daf32016-03-01 20:03:52 +0100310 max = x86_pmu.pebs_buffer_size / x86_pmu.pebs_record_size;
Peter Zijlstra5ee25c82010-10-19 14:15:04 +0200311
312 ds->pebs_buffer_base = (u64)(unsigned long)buffer;
313 ds->pebs_index = ds->pebs_buffer_base;
314 ds->pebs_absolute_maximum = ds->pebs_buffer_base +
315 max * x86_pmu.pebs_record_size;
316
Peter Zijlstra5ee25c82010-10-19 14:15:04 +0200317 return 0;
318}
319
Peter Zijlstrab39f88a2010-10-19 14:08:29 +0200320static void release_pebs_buffer(int cpu)
321{
322 struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
323
324 if (!ds || !x86_pmu.pebs)
325 return;
326
Peter Zijlstra9536c8d2013-10-15 12:14:04 +0200327 kfree(per_cpu(insn_buffer, cpu));
328 per_cpu(insn_buffer, cpu) = NULL;
329
Peter Zijlstrab39f88a2010-10-19 14:08:29 +0200330 kfree((void *)(unsigned long)ds->pebs_buffer_base);
331 ds->pebs_buffer_base = 0;
332}
333
Peter Zijlstra5ee25c82010-10-19 14:15:04 +0200334static int alloc_bts_buffer(int cpu)
335{
336 struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
Peter Zijlstra96681fc2010-10-19 14:55:33 +0200337 int node = cpu_to_node(cpu);
Peter Zijlstra5ee25c82010-10-19 14:15:04 +0200338 int max, thresh;
339 void *buffer;
340
341 if (!x86_pmu.bts)
342 return 0;
343
David Rientjes44851542014-06-30 16:04:08 -0700344 buffer = kzalloc_node(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, node);
345 if (unlikely(!buffer)) {
346 WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__);
Peter Zijlstra5ee25c82010-10-19 14:15:04 +0200347 return -ENOMEM;
David Rientjes44851542014-06-30 16:04:08 -0700348 }
Peter Zijlstra5ee25c82010-10-19 14:15:04 +0200349
350 max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE;
351 thresh = max / 16;
352
353 ds->bts_buffer_base = (u64)(unsigned long)buffer;
354 ds->bts_index = ds->bts_buffer_base;
355 ds->bts_absolute_maximum = ds->bts_buffer_base +
356 max * BTS_RECORD_SIZE;
357 ds->bts_interrupt_threshold = ds->bts_absolute_maximum -
358 thresh * BTS_RECORD_SIZE;
359
360 return 0;
361}
362
Peter Zijlstrab39f88a2010-10-19 14:08:29 +0200363static void release_bts_buffer(int cpu)
364{
365 struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
366
367 if (!ds || !x86_pmu.bts)
368 return;
369
370 kfree((void *)(unsigned long)ds->bts_buffer_base);
371 ds->bts_buffer_base = 0;
372}
373
Peter Zijlstra65af94b2010-10-19 14:37:23 +0200374static int alloc_ds_buffer(int cpu)
375{
Peter Zijlstra96681fc2010-10-19 14:55:33 +0200376 int node = cpu_to_node(cpu);
Peter Zijlstra65af94b2010-10-19 14:37:23 +0200377 struct debug_store *ds;
378
Joe Perches7bfb7e62013-08-29 13:59:17 -0700379 ds = kzalloc_node(sizeof(*ds), GFP_KERNEL, node);
Peter Zijlstra65af94b2010-10-19 14:37:23 +0200380 if (unlikely(!ds))
381 return -ENOMEM;
382
383 per_cpu(cpu_hw_events, cpu).ds = ds;
384
385 return 0;
386}
387
388static void release_ds_buffer(int cpu)
389{
390 struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
391
392 if (!ds)
393 return;
394
395 per_cpu(cpu_hw_events, cpu).ds = NULL;
396 kfree(ds);
397}
398
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300399void release_ds_buffers(void)
Peter Zijlstraca037702010-03-02 19:52:12 +0100400{
401 int cpu;
402
403 if (!x86_pmu.bts && !x86_pmu.pebs)
404 return;
405
406 get_online_cpus();
Peter Zijlstraca037702010-03-02 19:52:12 +0100407 for_each_online_cpu(cpu)
408 fini_debug_store_on_cpu(cpu);
409
410 for_each_possible_cpu(cpu) {
Peter Zijlstrab39f88a2010-10-19 14:08:29 +0200411 release_pebs_buffer(cpu);
412 release_bts_buffer(cpu);
Peter Zijlstra65af94b2010-10-19 14:37:23 +0200413 release_ds_buffer(cpu);
Peter Zijlstraca037702010-03-02 19:52:12 +0100414 }
Peter Zijlstraca037702010-03-02 19:52:12 +0100415 put_online_cpus();
416}
417
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300418void reserve_ds_buffers(void)
Peter Zijlstraca037702010-03-02 19:52:12 +0100419{
Peter Zijlstra6809b6e2010-10-19 14:22:50 +0200420 int bts_err = 0, pebs_err = 0;
421 int cpu;
422
423 x86_pmu.bts_active = 0;
424 x86_pmu.pebs_active = 0;
Peter Zijlstraca037702010-03-02 19:52:12 +0100425
426 if (!x86_pmu.bts && !x86_pmu.pebs)
Peter Zijlstraf80c9e32010-10-19 14:50:02 +0200427 return;
Peter Zijlstraca037702010-03-02 19:52:12 +0100428
Peter Zijlstra6809b6e2010-10-19 14:22:50 +0200429 if (!x86_pmu.bts)
430 bts_err = 1;
431
432 if (!x86_pmu.pebs)
433 pebs_err = 1;
434
Peter Zijlstraca037702010-03-02 19:52:12 +0100435 get_online_cpus();
436
437 for_each_possible_cpu(cpu) {
Peter Zijlstra6809b6e2010-10-19 14:22:50 +0200438 if (alloc_ds_buffer(cpu)) {
439 bts_err = 1;
440 pebs_err = 1;
441 }
Peter Zijlstraca037702010-03-02 19:52:12 +0100442
Peter Zijlstra6809b6e2010-10-19 14:22:50 +0200443 if (!bts_err && alloc_bts_buffer(cpu))
444 bts_err = 1;
Peter Zijlstraca037702010-03-02 19:52:12 +0100445
Peter Zijlstra6809b6e2010-10-19 14:22:50 +0200446 if (!pebs_err && alloc_pebs_buffer(cpu))
447 pebs_err = 1;
Peter Zijlstraca037702010-03-02 19:52:12 +0100448
Peter Zijlstra6809b6e2010-10-19 14:22:50 +0200449 if (bts_err && pebs_err)
450 break;
Peter Zijlstraca037702010-03-02 19:52:12 +0100451 }
452
Peter Zijlstra6809b6e2010-10-19 14:22:50 +0200453 if (bts_err) {
454 for_each_possible_cpu(cpu)
455 release_bts_buffer(cpu);
456 }
457
458 if (pebs_err) {
459 for_each_possible_cpu(cpu)
460 release_pebs_buffer(cpu);
461 }
462
463 if (bts_err && pebs_err) {
464 for_each_possible_cpu(cpu)
465 release_ds_buffer(cpu);
466 } else {
467 if (x86_pmu.bts && !bts_err)
468 x86_pmu.bts_active = 1;
469
470 if (x86_pmu.pebs && !pebs_err)
471 x86_pmu.pebs_active = 1;
472
Peter Zijlstraca037702010-03-02 19:52:12 +0100473 for_each_online_cpu(cpu)
474 init_debug_store_on_cpu(cpu);
475 }
476
477 put_online_cpus();
Peter Zijlstraca037702010-03-02 19:52:12 +0100478}
479
480/*
481 * BTS
482 */
483
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300484struct event_constraint bts_constraint =
Robert Richter15c7ad52012-06-20 20:46:33 +0200485 EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0);
Peter Zijlstraca037702010-03-02 19:52:12 +0100486
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300487void intel_pmu_enable_bts(u64 config)
Peter Zijlstraca037702010-03-02 19:52:12 +0100488{
489 unsigned long debugctlmsr;
490
491 debugctlmsr = get_debugctlmsr();
492
Peter Zijlstra7c5ecaf2010-03-25 14:51:49 +0100493 debugctlmsr |= DEBUGCTLMSR_TR;
494 debugctlmsr |= DEBUGCTLMSR_BTS;
Alexander Shishkin80623822015-01-30 12:40:35 +0200495 if (config & ARCH_PERFMON_EVENTSEL_INT)
496 debugctlmsr |= DEBUGCTLMSR_BTINT;
Peter Zijlstraca037702010-03-02 19:52:12 +0100497
498 if (!(config & ARCH_PERFMON_EVENTSEL_OS))
Peter Zijlstra7c5ecaf2010-03-25 14:51:49 +0100499 debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
Peter Zijlstraca037702010-03-02 19:52:12 +0100500
501 if (!(config & ARCH_PERFMON_EVENTSEL_USR))
Peter Zijlstra7c5ecaf2010-03-25 14:51:49 +0100502 debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR;
Peter Zijlstraca037702010-03-02 19:52:12 +0100503
504 update_debugctlmsr(debugctlmsr);
505}
506
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300507void intel_pmu_disable_bts(void)
Peter Zijlstraca037702010-03-02 19:52:12 +0100508{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500509 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstraca037702010-03-02 19:52:12 +0100510 unsigned long debugctlmsr;
511
512 if (!cpuc->ds)
513 return;
514
515 debugctlmsr = get_debugctlmsr();
516
517 debugctlmsr &=
Peter Zijlstra7c5ecaf2010-03-25 14:51:49 +0100518 ~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT |
519 DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR);
Peter Zijlstraca037702010-03-02 19:52:12 +0100520
521 update_debugctlmsr(debugctlmsr);
522}
523
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300524int intel_pmu_drain_bts_buffer(void)
Peter Zijlstraca037702010-03-02 19:52:12 +0100525{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500526 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstraca037702010-03-02 19:52:12 +0100527 struct debug_store *ds = cpuc->ds;
528 struct bts_record {
529 u64 from;
530 u64 to;
531 u64 flags;
532 };
Robert Richter15c7ad52012-06-20 20:46:33 +0200533 struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
Alexander Shishkina09d31f42015-08-31 17:09:27 +0300534 struct bts_record *at, *base, *top;
Peter Zijlstraca037702010-03-02 19:52:12 +0100535 struct perf_output_handle handle;
536 struct perf_event_header header;
537 struct perf_sample_data data;
Alexander Shishkina09d31f42015-08-31 17:09:27 +0300538 unsigned long skip = 0;
Peter Zijlstraca037702010-03-02 19:52:12 +0100539 struct pt_regs regs;
540
541 if (!event)
Stephane Eranianb0b20722010-09-10 13:28:01 +0200542 return 0;
Peter Zijlstraca037702010-03-02 19:52:12 +0100543
Peter Zijlstra6809b6e2010-10-19 14:22:50 +0200544 if (!x86_pmu.bts_active)
Stephane Eranianb0b20722010-09-10 13:28:01 +0200545 return 0;
Peter Zijlstraca037702010-03-02 19:52:12 +0100546
Alexander Shishkina09d31f42015-08-31 17:09:27 +0300547 base = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
548 top = (struct bts_record *)(unsigned long)ds->bts_index;
Peter Zijlstraca037702010-03-02 19:52:12 +0100549
Alexander Shishkina09d31f42015-08-31 17:09:27 +0300550 if (top <= base)
Stephane Eranianb0b20722010-09-10 13:28:01 +0200551 return 0;
Peter Zijlstraca037702010-03-02 19:52:12 +0100552
Stephane Eranian0e480262013-03-19 16:10:38 +0100553 memset(&regs, 0, sizeof(regs));
554
Peter Zijlstraca037702010-03-02 19:52:12 +0100555 ds->bts_index = ds->bts_buffer_base;
556
Robert Richterfd0d0002012-04-02 20:19:08 +0200557 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstraca037702010-03-02 19:52:12 +0100558
559 /*
Alexander Shishkina09d31f42015-08-31 17:09:27 +0300560 * BTS leaks kernel addresses in branches across the cpl boundary,
561 * such as traps or system calls, so unless the user is asking for
562 * kernel tracing (and right now it's not possible), we'd need to
563 * filter them out. But first we need to count how many of those we
564 * have in the current batch. This is an extra O(n) pass, however,
565 * it's much faster than the other one especially considering that
566 * n <= 2560 (BTS_BUFFER_SIZE / BTS_RECORD_SIZE * 15/16; see the
567 * alloc_bts_buffer()).
568 */
569 for (at = base; at < top; at++) {
570 /*
571 * Note that right now *this* BTS code only works if
572 * attr::exclude_kernel is set, but let's keep this extra
573 * check here in case that changes.
574 */
575 if (event->attr.exclude_kernel &&
576 (kernel_ip(at->from) || kernel_ip(at->to)))
577 skip++;
578 }
579
580 /*
Peter Zijlstraca037702010-03-02 19:52:12 +0100581 * Prepare a generic sample, i.e. fill in the invariant fields.
582 * We will overwrite the from and to address before we output
583 * the sample.
584 */
Peter Zijlstrae8d8a902016-03-18 17:31:27 +0100585 rcu_read_lock();
Peter Zijlstraca037702010-03-02 19:52:12 +0100586 perf_prepare_sample(&header, &data, event, &regs);
587
Alexander Shishkina09d31f42015-08-31 17:09:27 +0300588 if (perf_output_begin(&handle, event, header.size *
589 (top - base - skip)))
Peter Zijlstrae8d8a902016-03-18 17:31:27 +0100590 goto unlock;
Peter Zijlstraca037702010-03-02 19:52:12 +0100591
Alexander Shishkina09d31f42015-08-31 17:09:27 +0300592 for (at = base; at < top; at++) {
593 /* Filter out any records that contain kernel addresses. */
594 if (event->attr.exclude_kernel &&
595 (kernel_ip(at->from) || kernel_ip(at->to)))
596 continue;
597
Peter Zijlstraca037702010-03-02 19:52:12 +0100598 data.ip = at->from;
599 data.addr = at->to;
600
601 perf_output_sample(&handle, &header, &data, event);
602 }
603
604 perf_output_end(&handle);
605
606 /* There's new data available. */
607 event->hw.interrupts++;
608 event->pending_kill = POLL_IN;
Peter Zijlstrae8d8a902016-03-18 17:31:27 +0100609unlock:
610 rcu_read_unlock();
Stephane Eranianb0b20722010-09-10 13:28:01 +0200611 return 1;
Peter Zijlstraca037702010-03-02 19:52:12 +0100612}
613
Yan, Zheng9c964ef2015-05-06 15:33:51 -0400614static inline void intel_pmu_drain_pebs_buffer(void)
615{
616 struct pt_regs regs;
617
618 x86_pmu.drain_pebs(&regs);
619}
620
Peter Zijlstraca037702010-03-02 19:52:12 +0100621/*
622 * PEBS
623 */
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300624struct event_constraint intel_core2_pebs_event_constraints[] = {
Andi Kleenaf4bdcf2014-09-24 07:34:48 -0700625 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
626 INTEL_FLAGS_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
627 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
628 INTEL_FLAGS_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
629 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
Peter Zijlstra517e6342015-04-11 12:16:22 +0200630 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
631 INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01),
Peter Zijlstraca037702010-03-02 19:52:12 +0100632 EVENT_CONSTRAINT_END
633};
634
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300635struct event_constraint intel_atom_pebs_event_constraints[] = {
Andi Kleenaf4bdcf2014-09-24 07:34:48 -0700636 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
637 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
638 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
Peter Zijlstra517e6342015-04-11 12:16:22 +0200639 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
640 INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01),
Stephane Eranian673d1882015-12-03 21:03:10 +0100641 /* Allow all events as PEBS with no flags */
642 INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
Stephane Eranian17e31622011-03-02 17:05:01 +0200643 EVENT_CONSTRAINT_END
644};
645
Yan, Zheng1fa64182013-07-18 17:02:24 +0800646struct event_constraint intel_slm_pebs_event_constraints[] = {
Kan Liang33636732015-01-12 17:42:21 +0000647 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
648 INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x1),
Andi Kleen86a04462014-08-11 21:27:10 +0200649 /* Allow all events as PEBS with no flags */
650 INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
Yan, Zheng1fa64182013-07-18 17:02:24 +0800651 EVENT_CONSTRAINT_END
652};
653
Kan Liang8b92c3a2016-04-15 00:42:47 -0700654struct event_constraint intel_glm_pebs_event_constraints[] = {
655 /* Allow all events as PEBS with no flags */
656 INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
657 EVENT_CONSTRAINT_END
658};
659
Kan Liangdd0b06b2017-07-12 09:44:23 -0400660struct event_constraint intel_glp_pebs_event_constraints[] = {
661 /* Allow all events as PEBS with no flags */
662 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
663 EVENT_CONSTRAINT_END
664};
665
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300666struct event_constraint intel_nehalem_pebs_event_constraints[] = {
Stephane Eranianf20093e2013-01-24 16:10:32 +0100667 INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
Andi Kleenaf4bdcf2014-09-24 07:34:48 -0700668 INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
669 INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
670 INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INST_RETIRED.ANY */
Lin Ming7d5d02d2011-03-09 23:21:29 +0800671 INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
Andi Kleenaf4bdcf2014-09-24 07:34:48 -0700672 INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
673 INTEL_FLAGS_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
674 INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
675 INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
676 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
677 INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
Peter Zijlstra517e6342015-04-11 12:16:22 +0200678 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
679 INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
Stephane Eranian17e31622011-03-02 17:05:01 +0200680 EVENT_CONSTRAINT_END
681};
682
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300683struct event_constraint intel_westmere_pebs_event_constraints[] = {
Stephane Eranianf20093e2013-01-24 16:10:32 +0100684 INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
Andi Kleenaf4bdcf2014-09-24 07:34:48 -0700685 INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
686 INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
687 INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INSTR_RETIRED.* */
Lin Ming7d5d02d2011-03-09 23:21:29 +0800688 INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
Andi Kleenaf4bdcf2014-09-24 07:34:48 -0700689 INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
690 INTEL_FLAGS_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */
691 INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
692 INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
693 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
694 INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
Peter Zijlstra517e6342015-04-11 12:16:22 +0200695 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
696 INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
Peter Zijlstraca037702010-03-02 19:52:12 +0100697 EVENT_CONSTRAINT_END
698};
699
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300700struct event_constraint intel_snb_pebs_event_constraints[] = {
Andi Kleen0dbc9472014-09-24 07:34:47 -0700701 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
Stephane Eranianf20093e2013-01-24 16:10:32 +0100702 INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
Stephane Eranian9ad64c02013-01-24 16:10:34 +0100703 INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
Andi Kleen86a04462014-08-11 21:27:10 +0200704 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
705 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
Maria Dimakopouloub63b4b42014-11-17 20:07:00 +0100706 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
707 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
708 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
709 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
Andi Kleen86a04462014-08-11 21:27:10 +0200710 /* Allow all events as PEBS with no flags */
711 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
Lin Mingb06b3d42011-03-02 21:27:04 +0800712 EVENT_CONSTRAINT_END
713};
714
Stephane Eranian20a36e32012-09-11 01:07:01 +0200715struct event_constraint intel_ivb_pebs_event_constraints[] = {
Andi Kleen0dbc9472014-09-24 07:34:47 -0700716 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
Stephane Eranianf20093e2013-01-24 16:10:32 +0100717 INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
Stephane Eranian9ad64c02013-01-24 16:10:34 +0100718 INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
Andi Kleen86a04462014-08-11 21:27:10 +0200719 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
720 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
Andi Kleen72469762015-12-04 03:50:52 -0800721 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
722 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
Maria Dimakopouloub63b4b42014-11-17 20:07:00 +0100723 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
724 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
725 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
726 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
Andi Kleen86a04462014-08-11 21:27:10 +0200727 /* Allow all events as PEBS with no flags */
728 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
Stephane Eranian20a36e32012-09-11 01:07:01 +0200729 EVENT_CONSTRAINT_END
730};
731
Andi Kleen30443182013-06-17 17:36:49 -0700732struct event_constraint intel_hsw_pebs_event_constraints[] = {
Andi Kleen0dbc9472014-09-24 07:34:47 -0700733 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
Andi Kleen86a04462014-08-11 21:27:10 +0200734 INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */
735 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
736 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
Andi Kleen72469762015-12-04 03:50:52 -0800737 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
738 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
Andi Kleen86a04462014-08-11 21:27:10 +0200739 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
Maria Dimakopouloub63b4b42014-11-17 20:07:00 +0100740 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
741 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
742 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
743 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
744 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
745 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
746 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
747 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
748 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
749 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
Andi Kleen86a04462014-08-11 21:27:10 +0200750 /* Allow all events as PEBS with no flags */
751 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
Andi Kleen30443182013-06-17 17:36:49 -0700752 EVENT_CONSTRAINT_END
753};
754
Stephane Eranianb3e62462016-03-03 20:50:42 +0100755struct event_constraint intel_bdw_pebs_event_constraints[] = {
756 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
757 INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */
758 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
759 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
760 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
761 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
762 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
763 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
764 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
765 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
766 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
767 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
768 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
769 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
770 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
771 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
772 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
773 /* Allow all events as PEBS with no flags */
774 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
775 EVENT_CONSTRAINT_END
776};
777
778
Andi Kleen9a92e162015-05-10 12:22:44 -0700779struct event_constraint intel_skl_pebs_event_constraints[] = {
780 INTEL_FLAGS_UEVENT_CONSTRAINT(0x1c0, 0x2), /* INST_RETIRED.PREC_DIST */
Andi Kleen72469762015-12-04 03:50:52 -0800781 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
782 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
Andi Kleen442f5c72015-12-04 03:50:32 -0800783 /* INST_RETIRED.TOTAL_CYCLES_PS (inv=1, cmask=16) (cycles:p). */
784 INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
Andi Kleen9a92e162015-05-10 12:22:44 -0700785 INTEL_PLD_CONSTRAINT(0x1cd, 0xf), /* MEM_TRANS_RETIRED.* */
786 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */
787 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */
788 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */
789 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x22d0, 0xf), /* MEM_INST_RETIRED.LOCK_STORES */
790 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */
791 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */
792 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */
793 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */
794 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_RETIRED.* */
795 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_L3_HIT_RETIRED.* */
796 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_L3_MISS_RETIRED.* */
797 /* Allow all events as PEBS with no flags */
798 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
799 EVENT_CONSTRAINT_END
800};
801
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300802struct event_constraint *intel_pebs_constraints(struct perf_event *event)
Peter Zijlstraca037702010-03-02 19:52:12 +0100803{
804 struct event_constraint *c;
805
Peter Zijlstraab608342010-04-08 23:03:20 +0200806 if (!event->attr.precise_ip)
Peter Zijlstraca037702010-03-02 19:52:12 +0100807 return NULL;
808
809 if (x86_pmu.pebs_constraints) {
810 for_each_event_constraint(c, x86_pmu.pebs_constraints) {
Stephane Eranian9fac2cf2013-01-24 16:10:27 +0100811 if ((event->hw.config & c->cmask) == c->code) {
812 event->hw.flags |= c->flags;
Peter Zijlstraca037702010-03-02 19:52:12 +0100813 return c;
Stephane Eranian9fac2cf2013-01-24 16:10:27 +0100814 }
Peter Zijlstraca037702010-03-02 19:52:12 +0100815 }
816 }
817
818 return &emptyconstraint;
819}
820
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200821/*
822 * We need the sched_task callback even for per-cpu events when we use
823 * the large interrupt threshold, such that we can provide PID and TID
824 * to PEBS samples.
825 */
826static inline bool pebs_needs_sched_cb(struct cpu_hw_events *cpuc)
Yan, Zheng3569c0d2015-05-06 15:33:50 -0400827{
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200828 return cpuc->n_pebs && (cpuc->n_pebs == cpuc->n_large_pebs);
829}
830
Jiri Olsadf6c3db2017-07-19 09:52:47 +0200831void intel_pmu_pebs_sched_task(struct perf_event_context *ctx, bool sched_in)
832{
833 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
834
835 if (!sched_in && pebs_needs_sched_cb(cpuc))
836 intel_pmu_drain_pebs_buffer();
837}
838
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200839static inline void pebs_update_threshold(struct cpu_hw_events *cpuc)
840{
841 struct debug_store *ds = cpuc->ds;
842 u64 threshold;
843
844 if (cpuc->n_pebs == cpuc->n_large_pebs) {
845 threshold = ds->pebs_absolute_maximum -
846 x86_pmu.max_pebs_events * x86_pmu.pebs_record_size;
847 } else {
848 threshold = ds->pebs_buffer_base + x86_pmu.pebs_record_size;
849 }
850
851 ds->pebs_interrupt_threshold = threshold;
852}
853
854static void
855pebs_update_state(bool needed_cb, struct cpu_hw_events *cpuc, struct pmu *pmu)
856{
Jiri Olsab6a32f02016-08-18 11:09:52 +0200857 /*
858 * Make sure we get updated with the first PEBS
859 * event. It will trigger also during removal, but
860 * that does not hurt:
861 */
862 bool update = cpuc->n_pebs == 1;
863
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200864 if (needed_cb != pebs_needs_sched_cb(cpuc)) {
865 if (!needed_cb)
866 perf_sched_cb_inc(pmu);
867 else
868 perf_sched_cb_dec(pmu);
869
Jiri Olsab6a32f02016-08-18 11:09:52 +0200870 update = true;
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200871 }
Jiri Olsab6a32f02016-08-18 11:09:52 +0200872
873 if (update)
874 pebs_update_threshold(cpuc);
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200875}
876
Peter Zijlstra68f70822016-07-06 18:02:43 +0200877void intel_pmu_pebs_add(struct perf_event *event)
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200878{
879 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
880 struct hw_perf_event *hwc = &event->hw;
881 bool needed_cb = pebs_needs_sched_cb(cpuc);
882
883 cpuc->n_pebs++;
884 if (hwc->flags & PERF_X86_EVENT_FREERUNNING)
885 cpuc->n_large_pebs++;
886
887 pebs_update_state(needed_cb, cpuc, event->ctx->pmu);
Yan, Zheng3569c0d2015-05-06 15:33:50 -0400888}
889
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300890void intel_pmu_pebs_enable(struct perf_event *event)
Peter Zijlstraca037702010-03-02 19:52:12 +0100891{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500892 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstraef21f682010-03-03 13:12:23 +0100893 struct hw_perf_event *hwc = &event->hw;
Yan, Zheng851559e2015-05-06 15:33:47 -0400894 struct debug_store *ds = cpuc->ds;
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200895
Peter Zijlstraca037702010-03-02 19:52:12 +0100896 hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;
897
Peter Zijlstraad0e6cf2010-03-06 19:49:06 +0100898 cpuc->pebs_enabled |= 1ULL << hwc->idx;
Stephane Eranianf20093e2013-01-24 16:10:32 +0100899
900 if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
901 cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32);
Stephane Eranian9ad64c02013-01-24 16:10:34 +0100902 else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
903 cpuc->pebs_enabled |= 1ULL << 63;
Yan, Zheng851559e2015-05-06 15:33:47 -0400904
Yan, Zheng3569c0d2015-05-06 15:33:50 -0400905 /*
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200906 * Use auto-reload if possible to save a MSR write in the PMI.
907 * This must be done in pmu::start(), because PERF_EVENT_IOC_PERIOD.
Yan, Zheng3569c0d2015-05-06 15:33:50 -0400908 */
Yan, Zheng851559e2015-05-06 15:33:47 -0400909 if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
910 ds->pebs_event_reset[hwc->idx] =
911 (u64)(-hwc->sample_period) & x86_pmu.cntval_mask;
Jiri Olsadc853e22017-07-14 18:35:51 +0200912 } else {
913 ds->pebs_event_reset[hwc->idx] = 0;
Yan, Zheng851559e2015-05-06 15:33:47 -0400914 }
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200915}
Yan, Zheng3569c0d2015-05-06 15:33:50 -0400916
Peter Zijlstra68f70822016-07-06 18:02:43 +0200917void intel_pmu_pebs_del(struct perf_event *event)
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200918{
919 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
920 struct hw_perf_event *hwc = &event->hw;
921 bool needed_cb = pebs_needs_sched_cb(cpuc);
922
923 cpuc->n_pebs--;
924 if (hwc->flags & PERF_X86_EVENT_FREERUNNING)
925 cpuc->n_large_pebs--;
926
927 pebs_update_state(needed_cb, cpuc, event->ctx->pmu);
Peter Zijlstraca037702010-03-02 19:52:12 +0100928}
929
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300930void intel_pmu_pebs_disable(struct perf_event *event)
Peter Zijlstraca037702010-03-02 19:52:12 +0100931{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500932 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstraef21f682010-03-03 13:12:23 +0100933 struct hw_perf_event *hwc = &event->hw;
Liang, Kan2a853e12015-07-03 20:08:27 +0000934
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200935 if (cpuc->n_pebs == cpuc->n_large_pebs)
Liang, Kan2a853e12015-07-03 20:08:27 +0000936 intel_pmu_drain_pebs_buffer();
Peter Zijlstraca037702010-03-02 19:52:12 +0100937
Peter Zijlstraad0e6cf2010-03-06 19:49:06 +0100938 cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
Stephane Eranian983433b2013-06-21 16:20:41 +0200939
Peter Zijlstrab371b592015-05-21 10:57:13 +0200940 if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
Stephane Eranian983433b2013-06-21 16:20:41 +0200941 cpuc->pebs_enabled &= ~(1ULL << (hwc->idx + 32));
Peter Zijlstrab371b592015-05-21 10:57:13 +0200942 else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
Stephane Eranian983433b2013-06-21 16:20:41 +0200943 cpuc->pebs_enabled &= ~(1ULL << 63);
944
Peter Zijlstra4807e3d2010-03-06 13:47:07 +0100945 if (cpuc->enabled)
Peter Zijlstraad0e6cf2010-03-06 19:49:06 +0100946 wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
Peter Zijlstraca037702010-03-02 19:52:12 +0100947
948 hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
949}
950
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300951void intel_pmu_pebs_enable_all(void)
Peter Zijlstraca037702010-03-02 19:52:12 +0100952{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500953 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstraca037702010-03-02 19:52:12 +0100954
955 if (cpuc->pebs_enabled)
956 wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
957}
958
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300959void intel_pmu_pebs_disable_all(void)
Peter Zijlstraca037702010-03-02 19:52:12 +0100960{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500961 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstraca037702010-03-02 19:52:12 +0100962
963 if (cpuc->pebs_enabled)
964 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
965}
966
Peter Zijlstraef21f682010-03-03 13:12:23 +0100967static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
968{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500969 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstraef21f682010-03-03 13:12:23 +0100970 unsigned long from = cpuc->lbr_entries[0].from;
971 unsigned long old_to, to = cpuc->lbr_entries[0].to;
972 unsigned long ip = regs->ip;
Peter Zijlstra57d1c0c2011-10-07 13:36:40 +0200973 int is_64bit = 0;
Peter Zijlstra9536c8d2013-10-15 12:14:04 +0200974 void *kaddr;
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800975 int size;
Peter Zijlstraef21f682010-03-03 13:12:23 +0100976
Peter Zijlstra8db909a2010-03-03 17:07:40 +0100977 /*
978 * We don't need to fixup if the PEBS assist is fault like
979 */
980 if (!x86_pmu.intel_cap.pebs_trap)
981 return 1;
982
Peter Zijlstraa562b182010-03-05 16:29:14 +0100983 /*
984 * No LBR entry, no basic block, no rewinding
985 */
Peter Zijlstraef21f682010-03-03 13:12:23 +0100986 if (!cpuc->lbr_stack.nr || !from || !to)
987 return 0;
988
Peter Zijlstraa562b182010-03-05 16:29:14 +0100989 /*
990 * Basic blocks should never cross user/kernel boundaries
991 */
992 if (kernel_ip(ip) != kernel_ip(to))
993 return 0;
994
995 /*
996 * unsigned math, either ip is before the start (impossible) or
997 * the basic block is larger than 1 page (sanity)
998 */
Peter Zijlstra9536c8d2013-10-15 12:14:04 +0200999 if ((ip - to) > PEBS_FIXUP_SIZE)
Peter Zijlstraef21f682010-03-03 13:12:23 +01001000 return 0;
1001
1002 /*
1003 * We sampled a branch insn, rewind using the LBR stack
1004 */
1005 if (ip == to) {
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02001006 set_linear_ip(regs, from);
Peter Zijlstraef21f682010-03-03 13:12:23 +01001007 return 1;
1008 }
1009
Dave Hansen6ba48ff2014-11-14 07:39:57 -08001010 size = ip - to;
Peter Zijlstra9536c8d2013-10-15 12:14:04 +02001011 if (!kernel_ip(ip)) {
Dave Hansen6ba48ff2014-11-14 07:39:57 -08001012 int bytes;
Peter Zijlstra9536c8d2013-10-15 12:14:04 +02001013 u8 *buf = this_cpu_read(insn_buffer);
1014
Dave Hansen6ba48ff2014-11-14 07:39:57 -08001015 /* 'size' must fit our buffer, see above */
Peter Zijlstra9536c8d2013-10-15 12:14:04 +02001016 bytes = copy_from_user_nmi(buf, (void __user *)to, size);
Peter Zijlstra0a196842013-10-30 21:16:22 +01001017 if (bytes != 0)
Peter Zijlstra9536c8d2013-10-15 12:14:04 +02001018 return 0;
1019
1020 kaddr = buf;
1021 } else {
1022 kaddr = (void *)to;
1023 }
1024
Peter Zijlstraef21f682010-03-03 13:12:23 +01001025 do {
1026 struct insn insn;
Peter Zijlstraef21f682010-03-03 13:12:23 +01001027
1028 old_to = to;
Peter Zijlstraef21f682010-03-03 13:12:23 +01001029
Peter Zijlstra57d1c0c2011-10-07 13:36:40 +02001030#ifdef CONFIG_X86_64
1031 is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
1032#endif
Dave Hansen6ba48ff2014-11-14 07:39:57 -08001033 insn_init(&insn, kaddr, size, is_64bit);
Peter Zijlstraef21f682010-03-03 13:12:23 +01001034 insn_get_length(&insn);
Dave Hansen6ba48ff2014-11-14 07:39:57 -08001035 /*
1036 * Make sure there was not a problem decoding the
1037 * instruction and getting the length. This is
1038 * doubly important because we have an infinite
1039 * loop if insn.length=0.
1040 */
1041 if (!insn.length)
1042 break;
Peter Zijlstra9536c8d2013-10-15 12:14:04 +02001043
Peter Zijlstraef21f682010-03-03 13:12:23 +01001044 to += insn.length;
Peter Zijlstra9536c8d2013-10-15 12:14:04 +02001045 kaddr += insn.length;
Dave Hansen6ba48ff2014-11-14 07:39:57 -08001046 size -= insn.length;
Peter Zijlstraef21f682010-03-03 13:12:23 +01001047 } while (to < ip);
1048
1049 if (to == ip) {
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02001050 set_linear_ip(regs, old_to);
Peter Zijlstraef21f682010-03-03 13:12:23 +01001051 return 1;
1052 }
1053
Peter Zijlstraa562b182010-03-05 16:29:14 +01001054 /*
1055 * Even though we decoded the basic block, the instruction stream
1056 * never matched the given IP, either the TO or the IP got corrupted.
1057 */
Peter Zijlstraef21f682010-03-03 13:12:23 +01001058 return 0;
1059}
1060
Andi Kleen2f7ebf22015-05-10 12:22:40 -07001061static inline u64 intel_hsw_weight(struct pebs_record_skl *pebs)
Andi Kleen748e86a2013-09-05 20:37:39 -07001062{
1063 if (pebs->tsx_tuning) {
1064 union hsw_tsx_tuning tsx = { .value = pebs->tsx_tuning };
1065 return tsx.cycles_last_block;
1066 }
1067 return 0;
1068}
1069
Andi Kleen2f7ebf22015-05-10 12:22:40 -07001070static inline u64 intel_hsw_transaction(struct pebs_record_skl *pebs)
Andi Kleena405bad2013-09-20 07:40:40 -07001071{
1072 u64 txn = (pebs->tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32;
1073
1074 /* For RTM XABORTs also log the abort code from AX */
1075 if ((txn & PERF_TXN_TRANSACTION) && (pebs->ax & 1))
1076 txn |= ((pebs->ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
1077 return txn;
1078}
1079
Yan, Zheng43cf7632015-05-06 15:33:48 -04001080static void setup_pebs_sample_data(struct perf_event *event,
1081 struct pt_regs *iregs, void *__pebs,
1082 struct perf_sample_data *data,
1083 struct pt_regs *regs)
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001084{
Stephane Eranianc8aab2e2014-08-11 21:27:13 +02001085#define PERF_X86_EVENT_PEBS_HSW_PREC \
1086 (PERF_X86_EVENT_PEBS_ST_HSW | \
1087 PERF_X86_EVENT_PEBS_LD_HSW | \
1088 PERF_X86_EVENT_PEBS_NA_HSW)
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001089 /*
Peter Zijlstrad2beea42013-09-12 13:00:47 +02001090 * We cast to the biggest pebs_record but are careful not to
1091 * unconditionally access the 'extra' entries.
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001092 */
Christoph Lameter89cbc762014-08-17 12:30:40 -05001093 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Andi Kleen2f7ebf22015-05-10 12:22:40 -07001094 struct pebs_record_skl *pebs = __pebs;
Stephane Eranianf20093e2013-01-24 16:10:32 +01001095 u64 sample_type;
Stephane Eranianc8aab2e2014-08-11 21:27:13 +02001096 int fll, fst, dsrc;
1097 int fl = event->hw.flags;
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001098
Yan, Zheng21509082015-05-06 15:33:49 -04001099 if (pebs == NULL)
1100 return;
1101
Stephane Eranianc8aab2e2014-08-11 21:27:13 +02001102 sample_type = event->attr.sample_type;
1103 dsrc = sample_type & PERF_SAMPLE_DATA_SRC;
1104
1105 fll = fl & PERF_X86_EVENT_PEBS_LDLAT;
1106 fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC);
Stephane Eranianf20093e2013-01-24 16:10:32 +01001107
Yan, Zheng43cf7632015-05-06 15:33:48 -04001108 perf_sample_data_init(data, 0, event->hw.last_period);
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001109
Yan, Zheng43cf7632015-05-06 15:33:48 -04001110 data->period = event->hw.last_period;
Stephane Eranianf20093e2013-01-24 16:10:32 +01001111
1112 /*
Stephane Eranianc8aab2e2014-08-11 21:27:13 +02001113 * Use latency for weight (only avail with PEBS-LL)
Stephane Eranianf20093e2013-01-24 16:10:32 +01001114 */
Stephane Eranianc8aab2e2014-08-11 21:27:13 +02001115 if (fll && (sample_type & PERF_SAMPLE_WEIGHT))
Yan, Zheng43cf7632015-05-06 15:33:48 -04001116 data->weight = pebs->lat;
Stephane Eranianf20093e2013-01-24 16:10:32 +01001117
Stephane Eranianc8aab2e2014-08-11 21:27:13 +02001118 /*
1119 * data.data_src encodes the data source
1120 */
1121 if (dsrc) {
1122 u64 val = PERF_MEM_NA;
1123 if (fll)
1124 val = load_latency_data(pebs->dse);
1125 else if (fst && (fl & PERF_X86_EVENT_PEBS_HSW_PREC))
1126 val = precise_datala_hsw(event, pebs->dse);
1127 else if (fst)
1128 val = precise_store_data(pebs->dse);
Yan, Zheng43cf7632015-05-06 15:33:48 -04001129 data->data_src.val = val;
Stephane Eranianf20093e2013-01-24 16:10:32 +01001130 }
1131
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001132 /*
Peter Zijlstrab8000582016-11-17 18:17:31 +01001133 * We use the interrupt regs as a base because the PEBS record does not
1134 * contain a full regs set, specifically it seems to lack segment
1135 * descriptors, which get used by things like user_mode().
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001136 *
Peter Zijlstrab8000582016-11-17 18:17:31 +01001137 * In the simple case fix up only the IP for PERF_SAMPLE_IP.
1138 *
1139 * We must however always use BP,SP from iregs for the unwinder to stay
1140 * sane; the record BP,SP can point into thin air when the record is
1141 * from a previous PMI context or an (I)RET happend between the record
1142 * and PMI.
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001143 */
Yan, Zheng43cf7632015-05-06 15:33:48 -04001144 *regs = *iregs;
1145 regs->flags = pebs->flags;
1146 set_linear_ip(regs, pebs->ip);
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001147
Stephane Eranianaea48552014-09-24 13:48:38 +02001148 if (sample_type & PERF_SAMPLE_REGS_INTR) {
Yan, Zheng43cf7632015-05-06 15:33:48 -04001149 regs->ax = pebs->ax;
1150 regs->bx = pebs->bx;
1151 regs->cx = pebs->cx;
1152 regs->dx = pebs->dx;
1153 regs->si = pebs->si;
1154 regs->di = pebs->di;
Stephane Eranianaea48552014-09-24 13:48:38 +02001155
Peter Zijlstrab8000582016-11-17 18:17:31 +01001156 /*
1157 * Per the above; only set BP,SP if we don't need callchains.
1158 *
1159 * XXX: does this make sense?
1160 */
1161 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1162 regs->bp = pebs->bp;
1163 regs->sp = pebs->sp;
1164 }
1165
1166 /*
1167 * Preserve PERF_EFLAGS_VM from set_linear_ip().
1168 */
1169 regs->flags = pebs->flags | (regs->flags & PERF_EFLAGS_VM);
Stephane Eranianaea48552014-09-24 13:48:38 +02001170#ifndef CONFIG_X86_32
Yan, Zheng43cf7632015-05-06 15:33:48 -04001171 regs->r8 = pebs->r8;
1172 regs->r9 = pebs->r9;
1173 regs->r10 = pebs->r10;
1174 regs->r11 = pebs->r11;
1175 regs->r12 = pebs->r12;
1176 regs->r13 = pebs->r13;
1177 regs->r14 = pebs->r14;
1178 regs->r15 = pebs->r15;
Stephane Eranianaea48552014-09-24 13:48:38 +02001179#endif
1180 }
1181
Andi Kleen130768b2013-06-17 17:36:47 -07001182 if (event->attr.precise_ip > 1 && x86_pmu.intel_cap.pebs_format >= 2) {
Yan, Zheng43cf7632015-05-06 15:33:48 -04001183 regs->ip = pebs->real_ip;
1184 regs->flags |= PERF_EFLAGS_EXACT;
1185 } else if (event->attr.precise_ip > 1 && intel_pmu_pebs_fixup_ip(regs))
1186 regs->flags |= PERF_EFLAGS_EXACT;
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001187 else
Yan, Zheng43cf7632015-05-06 15:33:48 -04001188 regs->flags &= ~PERF_EFLAGS_EXACT;
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001189
Kan Liangfc7ce9c2017-08-28 20:52:49 -04001190 if ((sample_type & (PERF_SAMPLE_ADDR | PERF_SAMPLE_PHYS_ADDR)) &&
Peter Zijlstrad2beea42013-09-12 13:00:47 +02001191 x86_pmu.intel_cap.pebs_format >= 1)
Yan, Zheng43cf7632015-05-06 15:33:48 -04001192 data->addr = pebs->dla;
Andi Kleenf9134f32013-06-17 17:36:52 -07001193
Andi Kleena405bad2013-09-20 07:40:40 -07001194 if (x86_pmu.intel_cap.pebs_format >= 2) {
1195 /* Only set the TSX weight when no memory weight. */
Stephane Eranianc8aab2e2014-08-11 21:27:13 +02001196 if ((sample_type & PERF_SAMPLE_WEIGHT) && !fll)
Yan, Zheng43cf7632015-05-06 15:33:48 -04001197 data->weight = intel_hsw_weight(pebs);
Andi Kleena405bad2013-09-20 07:40:40 -07001198
Stephane Eranianc8aab2e2014-08-11 21:27:13 +02001199 if (sample_type & PERF_SAMPLE_TRANSACTION)
Yan, Zheng43cf7632015-05-06 15:33:48 -04001200 data->txn = intel_hsw_transaction(pebs);
Andi Kleena405bad2013-09-20 07:40:40 -07001201 }
Andi Kleen748e86a2013-09-05 20:37:39 -07001202
Andi Kleen2f7ebf22015-05-10 12:22:40 -07001203 /*
1204 * v3 supplies an accurate time stamp, so we use that
1205 * for the time stamp.
1206 *
1207 * We can only do this for the default trace clock.
1208 */
1209 if (x86_pmu.intel_cap.pebs_format >= 3 &&
1210 event->attr.use_clockid == 0)
1211 data->time = native_sched_clock_from_tsc(pebs->tsc);
1212
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001213 if (has_branch_stack(event))
Yan, Zheng43cf7632015-05-06 15:33:48 -04001214 data->br_stack = &cpuc->lbr_stack;
1215}
1216
Yan, Zheng21509082015-05-06 15:33:49 -04001217static inline void *
1218get_next_pebs_record_by_bit(void *base, void *top, int bit)
1219{
1220 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1221 void *at;
1222 u64 pebs_status;
1223
Stephane Eranian1424a092015-12-03 23:33:18 +01001224 /*
1225 * fmt0 does not have a status bitfield (does not use
1226 * perf_record_nhm format)
1227 */
1228 if (x86_pmu.intel_cap.pebs_format < 1)
1229 return base;
1230
Yan, Zheng21509082015-05-06 15:33:49 -04001231 if (base == NULL)
1232 return NULL;
1233
1234 for (at = base; at < top; at += x86_pmu.pebs_record_size) {
1235 struct pebs_record_nhm *p = at;
1236
1237 if (test_bit(bit, (unsigned long *)&p->status)) {
Peter Zijlstraa3d86542015-05-12 15:18:18 +02001238 /* PEBS v3 has accurate status bits */
1239 if (x86_pmu.intel_cap.pebs_format >= 3)
1240 return at;
Yan, Zheng21509082015-05-06 15:33:49 -04001241
1242 if (p->status == (1 << bit))
1243 return at;
1244
1245 /* clear non-PEBS bit and re-check */
1246 pebs_status = p->status & cpuc->pebs_enabled;
Kan Liangfd583ad2017-04-04 15:14:06 -04001247 pebs_status &= PEBS_COUNTER_MASK;
Yan, Zheng21509082015-05-06 15:33:49 -04001248 if (pebs_status == (1 << bit))
1249 return at;
1250 }
1251 }
1252 return NULL;
1253}
1254
Yan, Zheng43cf7632015-05-06 15:33:48 -04001255static void __intel_pmu_pebs_event(struct perf_event *event,
Yan, Zheng21509082015-05-06 15:33:49 -04001256 struct pt_regs *iregs,
1257 void *base, void *top,
1258 int bit, int count)
Yan, Zheng43cf7632015-05-06 15:33:48 -04001259{
1260 struct perf_sample_data data;
1261 struct pt_regs regs;
Yan, Zheng21509082015-05-06 15:33:49 -04001262 void *at = get_next_pebs_record_by_bit(base, top, bit);
Yan, Zheng43cf7632015-05-06 15:33:48 -04001263
Yan, Zheng21509082015-05-06 15:33:49 -04001264 if (!intel_pmu_save_and_restart(event) &&
1265 !(event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD))
Yan, Zheng43cf7632015-05-06 15:33:48 -04001266 return;
1267
Peter Zijlstraa3d86542015-05-12 15:18:18 +02001268 while (count > 1) {
1269 setup_pebs_sample_data(event, iregs, at, &data, &regs);
1270 perf_event_output(event, &data, &regs);
1271 at += x86_pmu.pebs_record_size;
1272 at = get_next_pebs_record_by_bit(at, top, bit);
1273 count--;
Yan, Zheng21509082015-05-06 15:33:49 -04001274 }
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001275
Yan, Zheng21509082015-05-06 15:33:49 -04001276 setup_pebs_sample_data(event, iregs, at, &data, &regs);
1277
1278 /*
1279 * All but the last records are processed.
1280 * The last one is left to be able to call the overflow handler.
1281 */
1282 if (perf_event_overflow(event, &data, &regs)) {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001283 x86_pmu_stop(event, 0);
Yan, Zheng21509082015-05-06 15:33:49 -04001284 return;
1285 }
1286
Peter Zijlstra2b0b5c62010-04-08 23:03:20 +02001287}
1288
Peter Zijlstraca037702010-03-02 19:52:12 +01001289static void intel_pmu_drain_pebs_core(struct pt_regs *iregs)
1290{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001291 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstraca037702010-03-02 19:52:12 +01001292 struct debug_store *ds = cpuc->ds;
1293 struct perf_event *event = cpuc->events[0]; /* PMC0 only */
1294 struct pebs_record_core *at, *top;
Peter Zijlstraca037702010-03-02 19:52:12 +01001295 int n;
1296
Peter Zijlstra6809b6e2010-10-19 14:22:50 +02001297 if (!x86_pmu.pebs_active)
Peter Zijlstraca037702010-03-02 19:52:12 +01001298 return;
1299
Peter Zijlstraca037702010-03-02 19:52:12 +01001300 at = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base;
1301 top = (struct pebs_record_core *)(unsigned long)ds->pebs_index;
1302
Peter Zijlstrad80c7502010-03-09 11:41:02 +01001303 /*
1304 * Whatever else happens, drain the thing
1305 */
1306 ds->pebs_index = ds->pebs_buffer_base;
1307
1308 if (!test_bit(0, cpuc->active_mask))
Peter Zijlstra8f4aebd2010-03-06 13:26:11 +01001309 return;
Peter Zijlstraca037702010-03-02 19:52:12 +01001310
Peter Zijlstrad80c7502010-03-09 11:41:02 +01001311 WARN_ON_ONCE(!event);
1312
Peter Zijlstraab608342010-04-08 23:03:20 +02001313 if (!event->attr.precise_ip)
Peter Zijlstrad80c7502010-03-09 11:41:02 +01001314 return;
1315
Stephane Eranian1424a092015-12-03 23:33:18 +01001316 n = top - at;
Peter Zijlstrad80c7502010-03-09 11:41:02 +01001317 if (n <= 0)
1318 return;
Peter Zijlstraca037702010-03-02 19:52:12 +01001319
Yan, Zheng21509082015-05-06 15:33:49 -04001320 __intel_pmu_pebs_event(event, iregs, at, top, 0, n);
Peter Zijlstraca037702010-03-02 19:52:12 +01001321}
1322
Peter Zijlstrad2beea42013-09-12 13:00:47 +02001323static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
Peter Zijlstraca037702010-03-02 19:52:12 +01001324{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001325 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstraca037702010-03-02 19:52:12 +01001326 struct debug_store *ds = cpuc->ds;
Yan, Zheng21509082015-05-06 15:33:49 -04001327 struct perf_event *event;
1328 void *base, *at, *top;
Yan, Zheng21509082015-05-06 15:33:49 -04001329 short counts[MAX_PEBS_EVENTS] = {};
Kan Liangf38b0db2015-05-10 15:13:14 -04001330 short error[MAX_PEBS_EVENTS] = {};
Peter Zijlstraa3d86542015-05-12 15:18:18 +02001331 int bit, i;
Peter Zijlstrad2beea42013-09-12 13:00:47 +02001332
1333 if (!x86_pmu.pebs_active)
1334 return;
1335
Yan, Zheng21509082015-05-06 15:33:49 -04001336 base = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
Peter Zijlstrad2beea42013-09-12 13:00:47 +02001337 top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
Peter Zijlstraca037702010-03-02 19:52:12 +01001338
Peter Zijlstraca037702010-03-02 19:52:12 +01001339 ds->pebs_index = ds->pebs_buffer_base;
1340
Yan, Zheng21509082015-05-06 15:33:49 -04001341 if (unlikely(base >= top))
Peter Zijlstrad2beea42013-09-12 13:00:47 +02001342 return;
1343
Yan, Zheng21509082015-05-06 15:33:49 -04001344 for (at = base; at < top; at += x86_pmu.pebs_record_size) {
Andi Kleen130768b2013-06-17 17:36:47 -07001345 struct pebs_record_nhm *p = at;
Peter Zijlstra75f80852015-07-15 14:35:46 +02001346 u64 pebs_status;
Peter Zijlstraca037702010-03-02 19:52:12 +01001347
Peter Zijlstra8ef9b842016-09-07 14:42:55 +02001348 pebs_status = p->status & cpuc->pebs_enabled;
1349 pebs_status &= (1ULL << x86_pmu.max_pebs_events) - 1;
1350
1351 /* PEBS v3 has more accurate status bits */
Peter Zijlstraa3d86542015-05-12 15:18:18 +02001352 if (x86_pmu.intel_cap.pebs_format >= 3) {
Peter Zijlstra8ef9b842016-09-07 14:42:55 +02001353 for_each_set_bit(bit, (unsigned long *)&pebs_status,
1354 x86_pmu.max_pebs_events)
Peter Zijlstraa3d86542015-05-12 15:18:18 +02001355 counts[bit]++;
1356
1357 continue;
1358 }
1359
Andi Kleen01330d72015-12-03 13:22:20 -08001360 /*
1361 * On some CPUs the PEBS status can be zero when PEBS is
1362 * racing with clearing of GLOBAL_STATUS.
1363 *
1364 * Normally we would drop that record, but in the
1365 * case when there is only a single active PEBS event
1366 * we can assume it's for that event.
1367 */
1368 if (!pebs_status && cpuc->pebs_enabled &&
1369 !(cpuc->pebs_enabled & (cpuc->pebs_enabled-1)))
1370 pebs_status = cpuc->pebs_enabled;
1371
Peter Zijlstra75f80852015-07-15 14:35:46 +02001372 bit = find_first_bit((unsigned long *)&pebs_status,
Yan, Zheng21509082015-05-06 15:33:49 -04001373 x86_pmu.max_pebs_events);
Andi Kleen957ea1f2015-12-03 13:22:19 -08001374 if (bit >= x86_pmu.max_pebs_events)
Peter Zijlstraca037702010-03-02 19:52:12 +01001375 continue;
Peter Zijlstra75f80852015-07-15 14:35:46 +02001376
Yan, Zheng21509082015-05-06 15:33:49 -04001377 /*
1378 * The PEBS hardware does not deal well with the situation
1379 * when events happen near to each other and multiple bits
1380 * are set. But it should happen rarely.
1381 *
1382 * If these events include one PEBS and multiple non-PEBS
1383 * events, it doesn't impact PEBS record. The record will
1384 * be handled normally. (slow path)
1385 *
1386 * If these events include two or more PEBS events, the
1387 * records for the events can be collapsed into a single
1388 * one, and it's not possible to reconstruct all events
1389 * that caused the PEBS record. It's called collision.
1390 * If collision happened, the record will be dropped.
Yan, Zheng21509082015-05-06 15:33:49 -04001391 */
Peter Zijlstra75f80852015-07-15 14:35:46 +02001392 if (p->status != (1ULL << bit)) {
1393 for_each_set_bit(i, (unsigned long *)&pebs_status,
1394 x86_pmu.max_pebs_events)
1395 error[i]++;
1396 continue;
Yan, Zheng21509082015-05-06 15:33:49 -04001397 }
Peter Zijlstra75f80852015-07-15 14:35:46 +02001398
Yan, Zheng21509082015-05-06 15:33:49 -04001399 counts[bit]++;
1400 }
1401
1402 for (bit = 0; bit < x86_pmu.max_pebs_events; bit++) {
Kan Liangf38b0db2015-05-10 15:13:14 -04001403 if ((counts[bit] == 0) && (error[bit] == 0))
Yan, Zheng21509082015-05-06 15:33:49 -04001404 continue;
Peter Zijlstra75f80852015-07-15 14:35:46 +02001405
Yan, Zheng21509082015-05-06 15:33:49 -04001406 event = cpuc->events[bit];
Peter Zijlstra8ef9b842016-09-07 14:42:55 +02001407 if (WARN_ON_ONCE(!event))
1408 continue;
1409
1410 if (WARN_ON_ONCE(!event->attr.precise_ip))
1411 continue;
Yan, Zheng21509082015-05-06 15:33:49 -04001412
Kan Liangf38b0db2015-05-10 15:13:14 -04001413 /* log dropped samples number */
Jiri Olsa475113d2016-12-28 14:31:03 +01001414 if (error[bit]) {
Kan Liangf38b0db2015-05-10 15:13:14 -04001415 perf_log_lost_samples(event, error[bit]);
1416
Jiri Olsa475113d2016-12-28 14:31:03 +01001417 if (perf_event_account_interrupt(event))
1418 x86_pmu_stop(event, 0);
1419 }
1420
Kan Liangf38b0db2015-05-10 15:13:14 -04001421 if (counts[bit]) {
1422 __intel_pmu_pebs_event(event, iregs, base,
1423 top, bit, counts[bit]);
1424 }
Peter Zijlstraca037702010-03-02 19:52:12 +01001425 }
Peter Zijlstraca037702010-03-02 19:52:12 +01001426}
1427
1428/*
1429 * BTS, PEBS probe and setup
1430 */
1431
Mathias Krause066ce642014-08-26 18:49:45 +02001432void __init intel_ds_init(void)
Peter Zijlstraca037702010-03-02 19:52:12 +01001433{
1434 /*
1435 * No support for 32bit formats
1436 */
1437 if (!boot_cpu_has(X86_FEATURE_DTES64))
1438 return;
1439
1440 x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS);
1441 x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS);
Jiri Olsae72daf32016-03-01 20:03:52 +01001442 x86_pmu.pebs_buffer_size = PEBS_BUFFER_SIZE;
Peter Zijlstraca037702010-03-02 19:52:12 +01001443 if (x86_pmu.pebs) {
Peter Zijlstra8db909a2010-03-03 17:07:40 +01001444 char pebs_type = x86_pmu.intel_cap.pebs_trap ? '+' : '-';
1445 int format = x86_pmu.intel_cap.pebs_format;
Peter Zijlstraca037702010-03-02 19:52:12 +01001446
1447 switch (format) {
1448 case 0:
Chen Yucong1b74dde2016-02-02 11:45:02 +08001449 pr_cont("PEBS fmt0%c, ", pebs_type);
Peter Zijlstraca037702010-03-02 19:52:12 +01001450 x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
Jiri Olsae72daf32016-03-01 20:03:52 +01001451 /*
1452 * Using >PAGE_SIZE buffers makes the WRMSR to
1453 * PERF_GLOBAL_CTRL in intel_pmu_enable_all()
1454 * mysteriously hang on Core2.
1455 *
1456 * As a workaround, we don't do this.
1457 */
1458 x86_pmu.pebs_buffer_size = PAGE_SIZE;
Peter Zijlstraca037702010-03-02 19:52:12 +01001459 x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
Peter Zijlstraca037702010-03-02 19:52:12 +01001460 break;
1461
1462 case 1:
Chen Yucong1b74dde2016-02-02 11:45:02 +08001463 pr_cont("PEBS fmt1%c, ", pebs_type);
Peter Zijlstraca037702010-03-02 19:52:12 +01001464 x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
1465 x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
Peter Zijlstraca037702010-03-02 19:52:12 +01001466 break;
1467
Andi Kleen130768b2013-06-17 17:36:47 -07001468 case 2:
1469 pr_cont("PEBS fmt2%c, ", pebs_type);
1470 x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw);
Peter Zijlstrad2beea42013-09-12 13:00:47 +02001471 x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
Andi Kleen130768b2013-06-17 17:36:47 -07001472 break;
1473
Andi Kleen2f7ebf22015-05-10 12:22:40 -07001474 case 3:
1475 pr_cont("PEBS fmt3%c, ", pebs_type);
1476 x86_pmu.pebs_record_size =
1477 sizeof(struct pebs_record_skl);
1478 x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
Andi Kleena7b58d22015-05-27 21:13:14 -07001479 x86_pmu.free_running_flags |= PERF_SAMPLE_TIME;
Andi Kleen2f7ebf22015-05-10 12:22:40 -07001480 break;
1481
Peter Zijlstraca037702010-03-02 19:52:12 +01001482 default:
Chen Yucong1b74dde2016-02-02 11:45:02 +08001483 pr_cont("no PEBS fmt%d%c, ", format, pebs_type);
Peter Zijlstraca037702010-03-02 19:52:12 +01001484 x86_pmu.pebs = 0;
Peter Zijlstraca037702010-03-02 19:52:12 +01001485 }
1486 }
1487}
Stephane Eranian1d9d8632013-03-15 14:26:07 +01001488
1489void perf_restore_debug_store(void)
1490{
Linus Torvalds2a6e06b2013-03-17 15:44:43 -07001491 struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
1492
Stephane Eranian1d9d8632013-03-15 14:26:07 +01001493 if (!x86_pmu.bts && !x86_pmu.pebs)
1494 return;
1495
Linus Torvalds2a6e06b2013-03-17 15:44:43 -07001496 wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds);
Stephane Eranian1d9d8632013-03-15 14:26:07 +01001497}