• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/bitops.h>
3 #include <linux/types.h>
4 #include <linux/slab.h>
5 #include <linux/sched/clock.h>
6 
7 #include <asm/cpu_entry_area.h>
8 #include <asm/debugreg.h>
9 #include <asm/perf_event.h>
10 #include <asm/tlbflush.h>
11 #include <asm/insn.h>
12 #include <asm/io.h>
13 #include <asm/timer.h>
14 
15 #include "../perf_event.h"
16 
17 /* Waste a full page so it can be mapped into the cpu_entry_area */
18 DEFINE_PER_CPU_PAGE_ALIGNED(struct debug_store, cpu_debug_store);
19 
20 /* The size of a BTS record in bytes: */
21 #define BTS_RECORD_SIZE		24
22 
23 #define PEBS_FIXUP_SIZE		PAGE_SIZE
24 
25 /*
26  * pebs_record_32 for p4 and core not supported
27 
28 struct pebs_record_32 {
29 	u32 flags, ip;
30 	u32 ax, bc, cx, dx;
31 	u32 si, di, bp, sp;
32 };
33 
34  */
35 
36 union intel_x86_pebs_dse {
37 	u64 val;
38 	struct {
39 		unsigned int ld_dse:4;
40 		unsigned int ld_stlb_miss:1;
41 		unsigned int ld_locked:1;
42 		unsigned int ld_data_blk:1;
43 		unsigned int ld_addr_blk:1;
44 		unsigned int ld_reserved:24;
45 	};
46 	struct {
47 		unsigned int st_l1d_hit:1;
48 		unsigned int st_reserved1:3;
49 		unsigned int st_stlb_miss:1;
50 		unsigned int st_locked:1;
51 		unsigned int st_reserved2:26;
52 	};
53 	struct {
54 		unsigned int st_lat_dse:4;
55 		unsigned int st_lat_stlb_miss:1;
56 		unsigned int st_lat_locked:1;
57 		unsigned int ld_reserved3:26;
58 	};
59 	struct {
60 		unsigned int mtl_dse:5;
61 		unsigned int mtl_locked:1;
62 		unsigned int mtl_stlb_miss:1;
63 		unsigned int mtl_fwd_blk:1;
64 		unsigned int ld_reserved4:24;
65 	};
66 	struct {
67 		unsigned int lnc_dse:8;
68 		unsigned int ld_reserved5:2;
69 		unsigned int lnc_stlb_miss:1;
70 		unsigned int lnc_locked:1;
71 		unsigned int lnc_data_blk:1;
72 		unsigned int lnc_addr_blk:1;
73 		unsigned int ld_reserved6:18;
74 	};
75 };
76 
77 
78 /*
79  * Map PEBS Load Latency Data Source encodings to generic
80  * memory data source information
81  */
82 #define P(a, b) PERF_MEM_S(a, b)
83 #define OP_LH (P(OP, LOAD) | P(LVL, HIT))
84 #define LEVEL(x) P(LVLNUM, x)
85 #define REM P(REMOTE, REMOTE)
86 #define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS))
87 
88 /* Version for Sandy Bridge and later */
89 static u64 pebs_data_source[PERF_PEBS_DATA_SOURCE_MAX] = {
90 	P(OP, LOAD) | P(LVL, MISS) | LEVEL(L3) | P(SNOOP, NA),/* 0x00:ukn L3 */
91 	OP_LH | P(LVL, L1)  | LEVEL(L1) | P(SNOOP, NONE),  /* 0x01: L1 local */
92 	OP_LH | P(LVL, LFB) | LEVEL(LFB) | P(SNOOP, NONE), /* 0x02: LFB hit */
93 	OP_LH | P(LVL, L2)  | LEVEL(L2) | P(SNOOP, NONE),  /* 0x03: L2 hit */
94 	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, NONE),  /* 0x04: L3 hit */
95 	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, MISS),  /* 0x05: L3 hit, snoop miss */
96 	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, HIT),   /* 0x06: L3 hit, snoop hit */
97 	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, HITM),  /* 0x07: L3 hit, snoop hitm */
98 	OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HIT),  /* 0x08: L3 miss snoop hit */
99 	OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HITM), /* 0x09: L3 miss snoop hitm*/
100 	OP_LH | P(LVL, LOC_RAM)  | LEVEL(RAM) | P(SNOOP, HIT),       /* 0x0a: L3 miss, shared */
101 	OP_LH | P(LVL, REM_RAM1) | REM | LEVEL(L3) | P(SNOOP, HIT),  /* 0x0b: L3 miss, shared */
102 	OP_LH | P(LVL, LOC_RAM)  | LEVEL(RAM) | SNOOP_NONE_MISS,     /* 0x0c: L3 miss, excl */
103 	OP_LH | P(LVL, REM_RAM1) | LEVEL(RAM) | REM | SNOOP_NONE_MISS, /* 0x0d: L3 miss, excl */
104 	OP_LH | P(LVL, IO)  | LEVEL(NA) | P(SNOOP, NONE), /* 0x0e: I/O */
105 	OP_LH | P(LVL, UNC) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0f: uncached */
106 };
107 
108 /* Patch up minor differences in the bits */
intel_pmu_pebs_data_source_nhm(void)109 void __init intel_pmu_pebs_data_source_nhm(void)
110 {
111 	pebs_data_source[0x05] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT);
112 	pebs_data_source[0x06] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
113 	pebs_data_source[0x07] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
114 }
115 
__intel_pmu_pebs_data_source_skl(bool pmem,u64 * data_source)116 static void __init __intel_pmu_pebs_data_source_skl(bool pmem, u64 *data_source)
117 {
118 	u64 pmem_or_l4 = pmem ? LEVEL(PMEM) : LEVEL(L4);
119 
120 	data_source[0x08] = OP_LH | pmem_or_l4 | P(SNOOP, HIT);
121 	data_source[0x09] = OP_LH | pmem_or_l4 | REM | P(SNOOP, HIT);
122 	data_source[0x0b] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, NONE);
123 	data_source[0x0c] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOPX, FWD);
124 	data_source[0x0d] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOP, HITM);
125 }
126 
intel_pmu_pebs_data_source_skl(bool pmem)127 void __init intel_pmu_pebs_data_source_skl(bool pmem)
128 {
129 	__intel_pmu_pebs_data_source_skl(pmem, pebs_data_source);
130 }
131 
__intel_pmu_pebs_data_source_grt(u64 * data_source)132 static void __init __intel_pmu_pebs_data_source_grt(u64 *data_source)
133 {
134 	data_source[0x05] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT);
135 	data_source[0x06] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
136 	data_source[0x08] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOPX, FWD);
137 }
138 
intel_pmu_pebs_data_source_grt(void)139 void __init intel_pmu_pebs_data_source_grt(void)
140 {
141 	__intel_pmu_pebs_data_source_grt(pebs_data_source);
142 }
143 
intel_pmu_pebs_data_source_adl(void)144 void __init intel_pmu_pebs_data_source_adl(void)
145 {
146 	u64 *data_source;
147 
148 	data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX].pebs_data_source;
149 	memcpy(data_source, pebs_data_source, sizeof(pebs_data_source));
150 	__intel_pmu_pebs_data_source_skl(false, data_source);
151 
152 	data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_ATOM_IDX].pebs_data_source;
153 	memcpy(data_source, pebs_data_source, sizeof(pebs_data_source));
154 	__intel_pmu_pebs_data_source_grt(data_source);
155 }
156 
__intel_pmu_pebs_data_source_cmt(u64 * data_source)157 static void __init __intel_pmu_pebs_data_source_cmt(u64 *data_source)
158 {
159 	data_source[0x07] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOPX, FWD);
160 	data_source[0x08] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
161 	data_source[0x0a] = OP_LH | P(LVL, LOC_RAM)  | LEVEL(RAM) | P(SNOOP, NONE);
162 	data_source[0x0b] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, NONE);
163 	data_source[0x0c] = OP_LH | LEVEL(RAM) | REM | P(SNOOPX, FWD);
164 	data_source[0x0d] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, HITM);
165 }
166 
intel_pmu_pebs_data_source_mtl(void)167 void __init intel_pmu_pebs_data_source_mtl(void)
168 {
169 	u64 *data_source;
170 
171 	data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX].pebs_data_source;
172 	memcpy(data_source, pebs_data_source, sizeof(pebs_data_source));
173 	__intel_pmu_pebs_data_source_skl(false, data_source);
174 
175 	data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_ATOM_IDX].pebs_data_source;
176 	memcpy(data_source, pebs_data_source, sizeof(pebs_data_source));
177 	__intel_pmu_pebs_data_source_cmt(data_source);
178 }
179 
intel_pmu_pebs_data_source_cmt(void)180 void __init intel_pmu_pebs_data_source_cmt(void)
181 {
182 	__intel_pmu_pebs_data_source_cmt(pebs_data_source);
183 }
184 
185 /* Version for Lion Cove and later */
186 static u64 lnc_pebs_data_source[PERF_PEBS_DATA_SOURCE_MAX] = {
187 	P(OP, LOAD) | P(LVL, MISS) | LEVEL(L3) | P(SNOOP, NA),	/* 0x00: ukn L3 */
188 	OP_LH | P(LVL, L1)  | LEVEL(L1) | P(SNOOP, NONE),	/* 0x01: L1 hit */
189 	OP_LH | P(LVL, L1)  | LEVEL(L1) | P(SNOOP, NONE),	/* 0x02: L1 hit */
190 	OP_LH | P(LVL, LFB) | LEVEL(LFB) | P(SNOOP, NONE),	/* 0x03: LFB/L1 Miss Handling Buffer hit */
191 	0,							/* 0x04: Reserved */
192 	OP_LH | P(LVL, L2)  | LEVEL(L2) | P(SNOOP, NONE),	/* 0x05: L2 Hit */
193 	OP_LH | LEVEL(L2_MHB) | P(SNOOP, NONE),			/* 0x06: L2 Miss Handling Buffer Hit */
194 	0,							/* 0x07: Reserved */
195 	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, NONE),	/* 0x08: L3 Hit */
196 	0,							/* 0x09: Reserved */
197 	0,							/* 0x0a: Reserved */
198 	0,							/* 0x0b: Reserved */
199 	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOPX, FWD),	/* 0x0c: L3 Hit Snoop Fwd */
200 	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, HITM),	/* 0x0d: L3 Hit Snoop HitM */
201 	0,							/* 0x0e: Reserved */
202 	P(OP, LOAD) | P(LVL, MISS) | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, HITM),	/* 0x0f: L3 Miss Snoop HitM */
203 	OP_LH | LEVEL(MSC) | P(SNOOP, NONE),			/* 0x10: Memory-side Cache Hit */
204 	OP_LH | P(LVL, LOC_RAM)  | LEVEL(RAM) | P(SNOOP, NONE), /* 0x11: Local Memory Hit */
205 };
206 
intel_pmu_pebs_data_source_lnl(void)207 void __init intel_pmu_pebs_data_source_lnl(void)
208 {
209 	u64 *data_source;
210 
211 	data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX].pebs_data_source;
212 	memcpy(data_source, lnc_pebs_data_source, sizeof(lnc_pebs_data_source));
213 
214 	data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_ATOM_IDX].pebs_data_source;
215 	memcpy(data_source, pebs_data_source, sizeof(pebs_data_source));
216 	__intel_pmu_pebs_data_source_cmt(data_source);
217 }
218 
precise_store_data(u64 status)219 static u64 precise_store_data(u64 status)
220 {
221 	union intel_x86_pebs_dse dse;
222 	u64 val = P(OP, STORE) | P(SNOOP, NA) | P(LVL, L1) | P(TLB, L2);
223 
224 	dse.val = status;
225 
226 	/*
227 	 * bit 4: TLB access
228 	 * 1 = stored missed 2nd level TLB
229 	 *
230 	 * so it either hit the walker or the OS
231 	 * otherwise hit 2nd level TLB
232 	 */
233 	if (dse.st_stlb_miss)
234 		val |= P(TLB, MISS);
235 	else
236 		val |= P(TLB, HIT);
237 
238 	/*
239 	 * bit 0: hit L1 data cache
240 	 * if not set, then all we know is that
241 	 * it missed L1D
242 	 */
243 	if (dse.st_l1d_hit)
244 		val |= P(LVL, HIT);
245 	else
246 		val |= P(LVL, MISS);
247 
248 	/*
249 	 * bit 5: Locked prefix
250 	 */
251 	if (dse.st_locked)
252 		val |= P(LOCK, LOCKED);
253 
254 	return val;
255 }
256 
precise_datala_hsw(struct perf_event * event,u64 status)257 static u64 precise_datala_hsw(struct perf_event *event, u64 status)
258 {
259 	union perf_mem_data_src dse;
260 
261 	dse.val = PERF_MEM_NA;
262 
263 	if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW)
264 		dse.mem_op = PERF_MEM_OP_STORE;
265 	else if (event->hw.flags & PERF_X86_EVENT_PEBS_LD_HSW)
266 		dse.mem_op = PERF_MEM_OP_LOAD;
267 
268 	/*
269 	 * L1 info only valid for following events:
270 	 *
271 	 * MEM_UOPS_RETIRED.STLB_MISS_STORES
272 	 * MEM_UOPS_RETIRED.LOCK_STORES
273 	 * MEM_UOPS_RETIRED.SPLIT_STORES
274 	 * MEM_UOPS_RETIRED.ALL_STORES
275 	 */
276 	if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) {
277 		if (status & 1)
278 			dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_HIT;
279 		else
280 			dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_MISS;
281 	}
282 	return dse.val;
283 }
284 
pebs_set_tlb_lock(u64 * val,bool tlb,bool lock)285 static inline void pebs_set_tlb_lock(u64 *val, bool tlb, bool lock)
286 {
287 	/*
288 	 * TLB access
289 	 * 0 = did not miss 2nd level TLB
290 	 * 1 = missed 2nd level TLB
291 	 */
292 	if (tlb)
293 		*val |= P(TLB, MISS) | P(TLB, L2);
294 	else
295 		*val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2);
296 
297 	/* locked prefix */
298 	if (lock)
299 		*val |= P(LOCK, LOCKED);
300 }
301 
302 /* Retrieve the latency data for e-core of ADL */
__grt_latency_data(struct perf_event * event,u64 status,u8 dse,bool tlb,bool lock,bool blk)303 static u64 __grt_latency_data(struct perf_event *event, u64 status,
304 			       u8 dse, bool tlb, bool lock, bool blk)
305 {
306 	u64 val;
307 
308 	WARN_ON_ONCE(hybrid_pmu(event->pmu)->pmu_type == hybrid_big);
309 
310 	dse &= PERF_PEBS_DATA_SOURCE_GRT_MASK;
311 	val = hybrid_var(event->pmu, pebs_data_source)[dse];
312 
313 	pebs_set_tlb_lock(&val, tlb, lock);
314 
315 	if (blk)
316 		val |= P(BLK, DATA);
317 	else
318 		val |= P(BLK, NA);
319 
320 	return val;
321 }
322 
grt_latency_data(struct perf_event * event,u64 status)323 u64 grt_latency_data(struct perf_event *event, u64 status)
324 {
325 	union intel_x86_pebs_dse dse;
326 
327 	dse.val = status;
328 
329 	return __grt_latency_data(event, status, dse.ld_dse,
330 				  dse.ld_locked, dse.ld_stlb_miss,
331 				  dse.ld_data_blk);
332 }
333 
334 /* Retrieve the latency data for e-core of MTL */
cmt_latency_data(struct perf_event * event,u64 status)335 u64 cmt_latency_data(struct perf_event *event, u64 status)
336 {
337 	union intel_x86_pebs_dse dse;
338 
339 	dse.val = status;
340 
341 	return __grt_latency_data(event, status, dse.mtl_dse,
342 				  dse.mtl_stlb_miss, dse.mtl_locked,
343 				  dse.mtl_fwd_blk);
344 }
345 
lnc_latency_data(struct perf_event * event,u64 status)346 static u64 lnc_latency_data(struct perf_event *event, u64 status)
347 {
348 	union intel_x86_pebs_dse dse;
349 	union perf_mem_data_src src;
350 	u64 val;
351 
352 	dse.val = status;
353 
354 	/* LNC core latency data */
355 	val = hybrid_var(event->pmu, pebs_data_source)[status & PERF_PEBS_DATA_SOURCE_MASK];
356 	if (!val)
357 		val = P(OP, LOAD) | LEVEL(NA) | P(SNOOP, NA);
358 
359 	if (dse.lnc_stlb_miss)
360 		val |= P(TLB, MISS) | P(TLB, L2);
361 	else
362 		val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2);
363 
364 	if (dse.lnc_locked)
365 		val |= P(LOCK, LOCKED);
366 
367 	if (dse.lnc_data_blk)
368 		val |= P(BLK, DATA);
369 	if (dse.lnc_addr_blk)
370 		val |= P(BLK, ADDR);
371 	if (!dse.lnc_data_blk && !dse.lnc_addr_blk)
372 		val |= P(BLK, NA);
373 
374 	src.val = val;
375 	if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW)
376 		src.mem_op = P(OP, STORE);
377 
378 	return src.val;
379 }
380 
lnl_latency_data(struct perf_event * event,u64 status)381 u64 lnl_latency_data(struct perf_event *event, u64 status)
382 {
383 	struct x86_hybrid_pmu *pmu = hybrid_pmu(event->pmu);
384 
385 	if (pmu->pmu_type == hybrid_small)
386 		return cmt_latency_data(event, status);
387 
388 	return lnc_latency_data(event, status);
389 }
390 
load_latency_data(struct perf_event * event,u64 status)391 static u64 load_latency_data(struct perf_event *event, u64 status)
392 {
393 	union intel_x86_pebs_dse dse;
394 	u64 val;
395 
396 	dse.val = status;
397 
398 	/*
399 	 * use the mapping table for bit 0-3
400 	 */
401 	val = hybrid_var(event->pmu, pebs_data_source)[dse.ld_dse];
402 
403 	/*
404 	 * Nehalem models do not support TLB, Lock infos
405 	 */
406 	if (x86_pmu.pebs_no_tlb) {
407 		val |= P(TLB, NA) | P(LOCK, NA);
408 		return val;
409 	}
410 
411 	pebs_set_tlb_lock(&val, dse.ld_stlb_miss, dse.ld_locked);
412 
413 	/*
414 	 * Ice Lake and earlier models do not support block infos.
415 	 */
416 	if (!x86_pmu.pebs_block) {
417 		val |= P(BLK, NA);
418 		return val;
419 	}
420 	/*
421 	 * bit 6: load was blocked since its data could not be forwarded
422 	 *        from a preceding store
423 	 */
424 	if (dse.ld_data_blk)
425 		val |= P(BLK, DATA);
426 
427 	/*
428 	 * bit 7: load was blocked due to potential address conflict with
429 	 *        a preceding store
430 	 */
431 	if (dse.ld_addr_blk)
432 		val |= P(BLK, ADDR);
433 
434 	if (!dse.ld_data_blk && !dse.ld_addr_blk)
435 		val |= P(BLK, NA);
436 
437 	return val;
438 }
439 
store_latency_data(struct perf_event * event,u64 status)440 static u64 store_latency_data(struct perf_event *event, u64 status)
441 {
442 	union intel_x86_pebs_dse dse;
443 	union perf_mem_data_src src;
444 	u64 val;
445 
446 	dse.val = status;
447 
448 	/*
449 	 * use the mapping table for bit 0-3
450 	 */
451 	val = hybrid_var(event->pmu, pebs_data_source)[dse.st_lat_dse];
452 
453 	pebs_set_tlb_lock(&val, dse.st_lat_stlb_miss, dse.st_lat_locked);
454 
455 	val |= P(BLK, NA);
456 
457 	/*
458 	 * the pebs_data_source table is only for loads
459 	 * so override the mem_op to say STORE instead
460 	 */
461 	src.val = val;
462 	src.mem_op = P(OP,STORE);
463 
464 	return src.val;
465 }
466 
467 struct pebs_record_core {
468 	u64 flags, ip;
469 	u64 ax, bx, cx, dx;
470 	u64 si, di, bp, sp;
471 	u64 r8,  r9,  r10, r11;
472 	u64 r12, r13, r14, r15;
473 };
474 
475 struct pebs_record_nhm {
476 	u64 flags, ip;
477 	u64 ax, bx, cx, dx;
478 	u64 si, di, bp, sp;
479 	u64 r8,  r9,  r10, r11;
480 	u64 r12, r13, r14, r15;
481 	u64 status, dla, dse, lat;
482 };
483 
484 /*
485  * Same as pebs_record_nhm, with two additional fields.
486  */
487 struct pebs_record_hsw {
488 	u64 flags, ip;
489 	u64 ax, bx, cx, dx;
490 	u64 si, di, bp, sp;
491 	u64 r8,  r9,  r10, r11;
492 	u64 r12, r13, r14, r15;
493 	u64 status, dla, dse, lat;
494 	u64 real_ip, tsx_tuning;
495 };
496 
497 union hsw_tsx_tuning {
498 	struct {
499 		u32 cycles_last_block     : 32,
500 		    hle_abort		  : 1,
501 		    rtm_abort		  : 1,
502 		    instruction_abort     : 1,
503 		    non_instruction_abort : 1,
504 		    retry		  : 1,
505 		    data_conflict	  : 1,
506 		    capacity_writes	  : 1,
507 		    capacity_reads	  : 1;
508 	};
509 	u64	    value;
510 };
511 
512 #define PEBS_HSW_TSX_FLAGS	0xff00000000ULL
513 
514 /* Same as HSW, plus TSC */
515 
516 struct pebs_record_skl {
517 	u64 flags, ip;
518 	u64 ax, bx, cx, dx;
519 	u64 si, di, bp, sp;
520 	u64 r8,  r9,  r10, r11;
521 	u64 r12, r13, r14, r15;
522 	u64 status, dla, dse, lat;
523 	u64 real_ip, tsx_tuning;
524 	u64 tsc;
525 };
526 
init_debug_store_on_cpu(int cpu)527 void init_debug_store_on_cpu(int cpu)
528 {
529 	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
530 
531 	if (!ds)
532 		return;
533 
534 	wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA,
535 		     (u32)((u64)(unsigned long)ds),
536 		     (u32)((u64)(unsigned long)ds >> 32));
537 }
538 
fini_debug_store_on_cpu(int cpu)539 void fini_debug_store_on_cpu(int cpu)
540 {
541 	if (!per_cpu(cpu_hw_events, cpu).ds)
542 		return;
543 
544 	wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0);
545 }
546 
547 static DEFINE_PER_CPU(void *, insn_buffer);
548 
ds_update_cea(void * cea,void * addr,size_t size,pgprot_t prot)549 static void ds_update_cea(void *cea, void *addr, size_t size, pgprot_t prot)
550 {
551 	unsigned long start = (unsigned long)cea;
552 	phys_addr_t pa;
553 	size_t msz = 0;
554 
555 	pa = virt_to_phys(addr);
556 
557 	preempt_disable();
558 	for (; msz < size; msz += PAGE_SIZE, pa += PAGE_SIZE, cea += PAGE_SIZE)
559 		cea_set_pte(cea, pa, prot);
560 
561 	/*
562 	 * This is a cross-CPU update of the cpu_entry_area, we must shoot down
563 	 * all TLB entries for it.
564 	 */
565 	flush_tlb_kernel_range(start, start + size);
566 	preempt_enable();
567 }
568 
ds_clear_cea(void * cea,size_t size)569 static void ds_clear_cea(void *cea, size_t size)
570 {
571 	unsigned long start = (unsigned long)cea;
572 	size_t msz = 0;
573 
574 	preempt_disable();
575 	for (; msz < size; msz += PAGE_SIZE, cea += PAGE_SIZE)
576 		cea_set_pte(cea, 0, PAGE_NONE);
577 
578 	flush_tlb_kernel_range(start, start + size);
579 	preempt_enable();
580 }
581 
dsalloc_pages(size_t size,gfp_t flags,int cpu)582 static void *dsalloc_pages(size_t size, gfp_t flags, int cpu)
583 {
584 	unsigned int order = get_order(size);
585 	int node = cpu_to_node(cpu);
586 	struct page *page;
587 
588 	page = __alloc_pages_node(node, flags | __GFP_ZERO, order);
589 	return page ? page_address(page) : NULL;
590 }
591 
dsfree_pages(const void * buffer,size_t size)592 static void dsfree_pages(const void *buffer, size_t size)
593 {
594 	if (buffer)
595 		free_pages((unsigned long)buffer, get_order(size));
596 }
597 
alloc_pebs_buffer(int cpu)598 static int alloc_pebs_buffer(int cpu)
599 {
600 	struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
601 	struct debug_store *ds = hwev->ds;
602 	size_t bsiz = x86_pmu.pebs_buffer_size;
603 	int max, node = cpu_to_node(cpu);
604 	void *buffer, *insn_buff, *cea;
605 
606 	if (!x86_pmu.pebs)
607 		return 0;
608 
609 	buffer = dsalloc_pages(bsiz, GFP_KERNEL, cpu);
610 	if (unlikely(!buffer))
611 		return -ENOMEM;
612 
613 	/*
614 	 * HSW+ already provides us the eventing ip; no need to allocate this
615 	 * buffer then.
616 	 */
617 	if (x86_pmu.intel_cap.pebs_format < 2) {
618 		insn_buff = kzalloc_node(PEBS_FIXUP_SIZE, GFP_KERNEL, node);
619 		if (!insn_buff) {
620 			dsfree_pages(buffer, bsiz);
621 			return -ENOMEM;
622 		}
623 		per_cpu(insn_buffer, cpu) = insn_buff;
624 	}
625 	hwev->ds_pebs_vaddr = buffer;
626 	/* Update the cpu entry area mapping */
627 	cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer;
628 	ds->pebs_buffer_base = (unsigned long) cea;
629 	ds_update_cea(cea, buffer, bsiz, PAGE_KERNEL);
630 	ds->pebs_index = ds->pebs_buffer_base;
631 	max = x86_pmu.pebs_record_size * (bsiz / x86_pmu.pebs_record_size);
632 	ds->pebs_absolute_maximum = ds->pebs_buffer_base + max;
633 	return 0;
634 }
635 
release_pebs_buffer(int cpu)636 static void release_pebs_buffer(int cpu)
637 {
638 	struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
639 	void *cea;
640 
641 	if (!x86_pmu.pebs)
642 		return;
643 
644 	kfree(per_cpu(insn_buffer, cpu));
645 	per_cpu(insn_buffer, cpu) = NULL;
646 
647 	/* Clear the fixmap */
648 	cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer;
649 	ds_clear_cea(cea, x86_pmu.pebs_buffer_size);
650 	dsfree_pages(hwev->ds_pebs_vaddr, x86_pmu.pebs_buffer_size);
651 	hwev->ds_pebs_vaddr = NULL;
652 }
653 
alloc_bts_buffer(int cpu)654 static int alloc_bts_buffer(int cpu)
655 {
656 	struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
657 	struct debug_store *ds = hwev->ds;
658 	void *buffer, *cea;
659 	int max;
660 
661 	if (!x86_pmu.bts)
662 		return 0;
663 
664 	buffer = dsalloc_pages(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, cpu);
665 	if (unlikely(!buffer)) {
666 		WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__);
667 		return -ENOMEM;
668 	}
669 	hwev->ds_bts_vaddr = buffer;
670 	/* Update the fixmap */
671 	cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer;
672 	ds->bts_buffer_base = (unsigned long) cea;
673 	ds_update_cea(cea, buffer, BTS_BUFFER_SIZE, PAGE_KERNEL);
674 	ds->bts_index = ds->bts_buffer_base;
675 	max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE;
676 	ds->bts_absolute_maximum = ds->bts_buffer_base +
677 					max * BTS_RECORD_SIZE;
678 	ds->bts_interrupt_threshold = ds->bts_absolute_maximum -
679 					(max / 16) * BTS_RECORD_SIZE;
680 	return 0;
681 }
682 
release_bts_buffer(int cpu)683 static void release_bts_buffer(int cpu)
684 {
685 	struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
686 	void *cea;
687 
688 	if (!x86_pmu.bts)
689 		return;
690 
691 	/* Clear the fixmap */
692 	cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer;
693 	ds_clear_cea(cea, BTS_BUFFER_SIZE);
694 	dsfree_pages(hwev->ds_bts_vaddr, BTS_BUFFER_SIZE);
695 	hwev->ds_bts_vaddr = NULL;
696 }
697 
alloc_ds_buffer(int cpu)698 static int alloc_ds_buffer(int cpu)
699 {
700 	struct debug_store *ds = &get_cpu_entry_area(cpu)->cpu_debug_store;
701 
702 	memset(ds, 0, sizeof(*ds));
703 	per_cpu(cpu_hw_events, cpu).ds = ds;
704 	return 0;
705 }
706 
release_ds_buffer(int cpu)707 static void release_ds_buffer(int cpu)
708 {
709 	per_cpu(cpu_hw_events, cpu).ds = NULL;
710 }
711 
release_ds_buffers(void)712 void release_ds_buffers(void)
713 {
714 	int cpu;
715 
716 	if (!x86_pmu.bts && !x86_pmu.pebs)
717 		return;
718 
719 	for_each_possible_cpu(cpu)
720 		release_ds_buffer(cpu);
721 
722 	for_each_possible_cpu(cpu) {
723 		/*
724 		 * Again, ignore errors from offline CPUs, they will no longer
725 		 * observe cpu_hw_events.ds and not program the DS_AREA when
726 		 * they come up.
727 		 */
728 		fini_debug_store_on_cpu(cpu);
729 	}
730 
731 	for_each_possible_cpu(cpu) {
732 		release_pebs_buffer(cpu);
733 		release_bts_buffer(cpu);
734 	}
735 }
736 
reserve_ds_buffers(void)737 void reserve_ds_buffers(void)
738 {
739 	int bts_err = 0, pebs_err = 0;
740 	int cpu;
741 
742 	x86_pmu.bts_active = 0;
743 	x86_pmu.pebs_active = 0;
744 
745 	if (!x86_pmu.bts && !x86_pmu.pebs)
746 		return;
747 
748 	if (!x86_pmu.bts)
749 		bts_err = 1;
750 
751 	if (!x86_pmu.pebs)
752 		pebs_err = 1;
753 
754 	for_each_possible_cpu(cpu) {
755 		if (alloc_ds_buffer(cpu)) {
756 			bts_err = 1;
757 			pebs_err = 1;
758 		}
759 
760 		if (!bts_err && alloc_bts_buffer(cpu))
761 			bts_err = 1;
762 
763 		if (!pebs_err && alloc_pebs_buffer(cpu))
764 			pebs_err = 1;
765 
766 		if (bts_err && pebs_err)
767 			break;
768 	}
769 
770 	if (bts_err) {
771 		for_each_possible_cpu(cpu)
772 			release_bts_buffer(cpu);
773 	}
774 
775 	if (pebs_err) {
776 		for_each_possible_cpu(cpu)
777 			release_pebs_buffer(cpu);
778 	}
779 
780 	if (bts_err && pebs_err) {
781 		for_each_possible_cpu(cpu)
782 			release_ds_buffer(cpu);
783 	} else {
784 		if (x86_pmu.bts && !bts_err)
785 			x86_pmu.bts_active = 1;
786 
787 		if (x86_pmu.pebs && !pebs_err)
788 			x86_pmu.pebs_active = 1;
789 
790 		for_each_possible_cpu(cpu) {
791 			/*
792 			 * Ignores wrmsr_on_cpu() errors for offline CPUs they
793 			 * will get this call through intel_pmu_cpu_starting().
794 			 */
795 			init_debug_store_on_cpu(cpu);
796 		}
797 	}
798 }
799 
800 /*
801  * BTS
802  */
803 
804 struct event_constraint bts_constraint =
805 	EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0);
806 
intel_pmu_enable_bts(u64 config)807 void intel_pmu_enable_bts(u64 config)
808 {
809 	unsigned long debugctlmsr;
810 
811 	debugctlmsr = get_debugctlmsr();
812 
813 	debugctlmsr |= DEBUGCTLMSR_TR;
814 	debugctlmsr |= DEBUGCTLMSR_BTS;
815 	if (config & ARCH_PERFMON_EVENTSEL_INT)
816 		debugctlmsr |= DEBUGCTLMSR_BTINT;
817 
818 	if (!(config & ARCH_PERFMON_EVENTSEL_OS))
819 		debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
820 
821 	if (!(config & ARCH_PERFMON_EVENTSEL_USR))
822 		debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR;
823 
824 	update_debugctlmsr(debugctlmsr);
825 }
826 
intel_pmu_disable_bts(void)827 void intel_pmu_disable_bts(void)
828 {
829 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
830 	unsigned long debugctlmsr;
831 
832 	if (!cpuc->ds)
833 		return;
834 
835 	debugctlmsr = get_debugctlmsr();
836 
837 	debugctlmsr &=
838 		~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT |
839 		  DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR);
840 
841 	update_debugctlmsr(debugctlmsr);
842 }
843 
intel_pmu_drain_bts_buffer(void)844 int intel_pmu_drain_bts_buffer(void)
845 {
846 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
847 	struct debug_store *ds = cpuc->ds;
848 	struct bts_record {
849 		u64	from;
850 		u64	to;
851 		u64	flags;
852 	};
853 	struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
854 	struct bts_record *at, *base, *top;
855 	struct perf_output_handle handle;
856 	struct perf_event_header header;
857 	struct perf_sample_data data;
858 	unsigned long skip = 0;
859 	struct pt_regs regs;
860 
861 	if (!event)
862 		return 0;
863 
864 	if (!x86_pmu.bts_active)
865 		return 0;
866 
867 	base = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
868 	top  = (struct bts_record *)(unsigned long)ds->bts_index;
869 
870 	if (top <= base)
871 		return 0;
872 
873 	memset(&regs, 0, sizeof(regs));
874 
875 	ds->bts_index = ds->bts_buffer_base;
876 
877 	perf_sample_data_init(&data, 0, event->hw.last_period);
878 
879 	/*
880 	 * BTS leaks kernel addresses in branches across the cpl boundary,
881 	 * such as traps or system calls, so unless the user is asking for
882 	 * kernel tracing (and right now it's not possible), we'd need to
883 	 * filter them out. But first we need to count how many of those we
884 	 * have in the current batch. This is an extra O(n) pass, however,
885 	 * it's much faster than the other one especially considering that
886 	 * n <= 2560 (BTS_BUFFER_SIZE / BTS_RECORD_SIZE * 15/16; see the
887 	 * alloc_bts_buffer()).
888 	 */
889 	for (at = base; at < top; at++) {
890 		/*
891 		 * Note that right now *this* BTS code only works if
892 		 * attr::exclude_kernel is set, but let's keep this extra
893 		 * check here in case that changes.
894 		 */
895 		if (event->attr.exclude_kernel &&
896 		    (kernel_ip(at->from) || kernel_ip(at->to)))
897 			skip++;
898 	}
899 
900 	/*
901 	 * Prepare a generic sample, i.e. fill in the invariant fields.
902 	 * We will overwrite the from and to address before we output
903 	 * the sample.
904 	 */
905 	rcu_read_lock();
906 	perf_prepare_sample(&data, event, &regs);
907 	perf_prepare_header(&header, &data, event, &regs);
908 
909 	if (perf_output_begin(&handle, &data, event,
910 			      header.size * (top - base - skip)))
911 		goto unlock;
912 
913 	for (at = base; at < top; at++) {
914 		/* Filter out any records that contain kernel addresses. */
915 		if (event->attr.exclude_kernel &&
916 		    (kernel_ip(at->from) || kernel_ip(at->to)))
917 			continue;
918 
919 		data.ip		= at->from;
920 		data.addr	= at->to;
921 
922 		perf_output_sample(&handle, &header, &data, event);
923 	}
924 
925 	perf_output_end(&handle);
926 
927 	/* There's new data available. */
928 	event->hw.interrupts++;
929 	event->pending_kill = POLL_IN;
930 unlock:
931 	rcu_read_unlock();
932 	return 1;
933 }
934 
intel_pmu_drain_pebs_buffer(void)935 void intel_pmu_drain_pebs_buffer(void)
936 {
937 	struct perf_sample_data data;
938 
939 	static_call(x86_pmu_drain_pebs)(NULL, &data);
940 }
941 
942 /*
943  * PEBS
944  */
945 struct event_constraint intel_core2_pebs_event_constraints[] = {
946 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
947 	INTEL_FLAGS_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
948 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
949 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
950 	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1),    /* MEM_LOAD_RETIRED.* */
951 	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
952 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x01),
953 	EVENT_CONSTRAINT_END
954 };
955 
956 struct event_constraint intel_atom_pebs_event_constraints[] = {
957 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
958 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
959 	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1),    /* MEM_LOAD_RETIRED.* */
960 	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
961 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x01),
962 	/* Allow all events as PEBS with no flags */
963 	INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
964 	EVENT_CONSTRAINT_END
965 };
966 
967 struct event_constraint intel_slm_pebs_event_constraints[] = {
968 	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
969 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x1),
970 	/* Allow all events as PEBS with no flags */
971 	INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
972 	EVENT_CONSTRAINT_END
973 };
974 
975 struct event_constraint intel_glm_pebs_event_constraints[] = {
976 	/* Allow all events as PEBS with no flags */
977 	INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
978 	EVENT_CONSTRAINT_END
979 };
980 
981 struct event_constraint intel_grt_pebs_event_constraints[] = {
982 	/* Allow all events as PEBS with no flags */
983 	INTEL_HYBRID_LAT_CONSTRAINT(0x5d0, 0x3),
984 	INTEL_HYBRID_LAT_CONSTRAINT(0x6d0, 0xf),
985 	EVENT_CONSTRAINT_END
986 };
987 
988 struct event_constraint intel_nehalem_pebs_event_constraints[] = {
989 	INTEL_PLD_CONSTRAINT(0x100b, 0xf),      /* MEM_INST_RETIRED.* */
990 	INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf),    /* MEM_UNCORE_RETIRED.* */
991 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
992 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf),    /* INST_RETIRED.ANY */
993 	INTEL_EVENT_CONSTRAINT(0xc2, 0xf),    /* UOPS_RETIRED.* */
994 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
995 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
996 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf),    /* SSEX_UOPS_RETIRED.* */
997 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
998 	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf),    /* MEM_LOAD_RETIRED.* */
999 	INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf),    /* FP_ASSIST.* */
1000 	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
1001 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f),
1002 	EVENT_CONSTRAINT_END
1003 };
1004 
1005 struct event_constraint intel_westmere_pebs_event_constraints[] = {
1006 	INTEL_PLD_CONSTRAINT(0x100b, 0xf),      /* MEM_INST_RETIRED.* */
1007 	INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf),    /* MEM_UNCORE_RETIRED.* */
1008 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
1009 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf),    /* INSTR_RETIRED.* */
1010 	INTEL_EVENT_CONSTRAINT(0xc2, 0xf),    /* UOPS_RETIRED.* */
1011 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
1012 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc5, 0xf),    /* BR_MISP_RETIRED.* */
1013 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf),    /* SSEX_UOPS_RETIRED.* */
1014 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
1015 	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf),    /* MEM_LOAD_RETIRED.* */
1016 	INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf),    /* FP_ASSIST.* */
1017 	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
1018 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f),
1019 	EVENT_CONSTRAINT_END
1020 };
1021 
1022 struct event_constraint intel_snb_pebs_event_constraints[] = {
1023 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
1024 	INTEL_PLD_CONSTRAINT(0x01cd, 0x8),    /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
1025 	INTEL_PST_CONSTRAINT(0x02cd, 0x8),    /* MEM_TRANS_RETIRED.PRECISE_STORES */
1026 	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
1027 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf),
1028         INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf),    /* MEM_UOP_RETIRED.* */
1029         INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
1030         INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf),    /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
1031         INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf),    /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
1032 	/* Allow all events as PEBS with no flags */
1033 	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
1034 	EVENT_CONSTRAINT_END
1035 };
1036 
1037 struct event_constraint intel_ivb_pebs_event_constraints[] = {
1038         INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
1039         INTEL_PLD_CONSTRAINT(0x01cd, 0x8),    /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
1040 	INTEL_PST_CONSTRAINT(0x02cd, 0x8),    /* MEM_TRANS_RETIRED.PRECISE_STORES */
1041 	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
1042 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf),
1043 	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
1044 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2),
1045 	INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf),    /* MEM_UOP_RETIRED.* */
1046 	INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
1047 	INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf),    /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
1048 	INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf),    /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
1049 	/* Allow all events as PEBS with no flags */
1050 	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
1051         EVENT_CONSTRAINT_END
1052 };
1053 
1054 struct event_constraint intel_hsw_pebs_event_constraints[] = {
1055 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
1056 	INTEL_PLD_CONSTRAINT(0x01cd, 0xf),    /* MEM_TRANS_RETIRED.* */
1057 	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
1058 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf),
1059 	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
1060 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2),
1061 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
1062 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
1063 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
1064 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
1065 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
1066 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
1067 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
1068 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
1069 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
1070 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd2, 0xf),    /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
1071 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd3, 0xf),    /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
1072 	/* Allow all events as PEBS with no flags */
1073 	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
1074 	EVENT_CONSTRAINT_END
1075 };
1076 
1077 struct event_constraint intel_bdw_pebs_event_constraints[] = {
1078 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
1079 	INTEL_PLD_CONSTRAINT(0x01cd, 0xf),    /* MEM_TRANS_RETIRED.* */
1080 	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
1081 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf),
1082 	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
1083 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2),
1084 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
1085 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
1086 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
1087 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
1088 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
1089 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
1090 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
1091 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
1092 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
1093 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf),    /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
1094 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf),    /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
1095 	/* Allow all events as PEBS with no flags */
1096 	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
1097 	EVENT_CONSTRAINT_END
1098 };
1099 
1100 
1101 struct event_constraint intel_skl_pebs_event_constraints[] = {
1102 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x1c0, 0x2),	/* INST_RETIRED.PREC_DIST */
1103 	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
1104 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2),
1105 	/* INST_RETIRED.TOTAL_CYCLES_PS (inv=1, cmask=16) (cycles:p). */
1106 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f),
1107 	INTEL_PLD_CONSTRAINT(0x1cd, 0xf),		      /* MEM_TRANS_RETIRED.* */
1108 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */
1109 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */
1110 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */
1111 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x22d0, 0xf), /* MEM_INST_RETIRED.LOCK_STORES */
1112 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */
1113 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */
1114 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */
1115 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */
1116 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf),    /* MEM_LOAD_RETIRED.* */
1117 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf),    /* MEM_LOAD_L3_HIT_RETIRED.* */
1118 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf),    /* MEM_LOAD_L3_MISS_RETIRED.* */
1119 	/* Allow all events as PEBS with no flags */
1120 	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
1121 	EVENT_CONSTRAINT_END
1122 };
1123 
1124 struct event_constraint intel_icl_pebs_event_constraints[] = {
1125 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x100000000ULL),	/* old INST_RETIRED.PREC_DIST */
1126 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x0100, 0x100000000ULL),	/* INST_RETIRED.PREC_DIST */
1127 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x0400, 0x800000000ULL),	/* SLOTS */
1128 
1129 	INTEL_PLD_CONSTRAINT(0x1cd, 0xff),			/* MEM_TRANS_RETIRED.LOAD_LATENCY */
1130 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf),	/* MEM_INST_RETIRED.STLB_MISS_LOADS */
1131 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf),	/* MEM_INST_RETIRED.STLB_MISS_STORES */
1132 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf),	/* MEM_INST_RETIRED.LOCK_LOADS */
1133 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf),	/* MEM_INST_RETIRED.SPLIT_LOADS */
1134 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf),	/* MEM_INST_RETIRED.SPLIT_STORES */
1135 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf),	/* MEM_INST_RETIRED.ALL_LOADS */
1136 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf),	/* MEM_INST_RETIRED.ALL_STORES */
1137 
1138 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD_RANGE(0xd1, 0xd4, 0xf), /* MEM_LOAD_*_RETIRED.* */
1139 
1140 	INTEL_FLAGS_EVENT_CONSTRAINT(0xd0, 0xf),		/* MEM_INST_RETIRED.* */
1141 
1142 	/*
1143 	 * Everything else is handled by PMU_FL_PEBS_ALL, because we
1144 	 * need the full constraints from the main table.
1145 	 */
1146 
1147 	EVENT_CONSTRAINT_END
1148 };
1149 
1150 struct event_constraint intel_glc_pebs_event_constraints[] = {
1151 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x100, 0x100000000ULL),	/* INST_RETIRED.PREC_DIST */
1152 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x0400, 0x800000000ULL),
1153 
1154 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xfe),
1155 	INTEL_PLD_CONSTRAINT(0x1cd, 0xfe),
1156 	INTEL_PSD_CONSTRAINT(0x2cd, 0x1),
1157 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf),	/* MEM_INST_RETIRED.STLB_MISS_LOADS */
1158 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf),	/* MEM_INST_RETIRED.STLB_MISS_STORES */
1159 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf),	/* MEM_INST_RETIRED.LOCK_LOADS */
1160 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf),	/* MEM_INST_RETIRED.SPLIT_LOADS */
1161 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf),	/* MEM_INST_RETIRED.SPLIT_STORES */
1162 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf),	/* MEM_INST_RETIRED.ALL_LOADS */
1163 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf),	/* MEM_INST_RETIRED.ALL_STORES */
1164 
1165 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD_RANGE(0xd1, 0xd4, 0xf),
1166 
1167 	INTEL_FLAGS_EVENT_CONSTRAINT(0xd0, 0xf),
1168 
1169 	/*
1170 	 * Everything else is handled by PMU_FL_PEBS_ALL, because we
1171 	 * need the full constraints from the main table.
1172 	 */
1173 
1174 	EVENT_CONSTRAINT_END
1175 };
1176 
1177 struct event_constraint intel_lnc_pebs_event_constraints[] = {
1178 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x100, 0x100000000ULL),	/* INST_RETIRED.PREC_DIST */
1179 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x0400, 0x800000000ULL),
1180 
1181 	INTEL_HYBRID_LDLAT_CONSTRAINT(0x1cd, 0x3fc),
1182 	INTEL_HYBRID_STLAT_CONSTRAINT(0x2cd, 0x3),
1183 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf),	/* MEM_INST_RETIRED.STLB_MISS_LOADS */
1184 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf),	/* MEM_INST_RETIRED.STLB_MISS_STORES */
1185 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf),	/* MEM_INST_RETIRED.LOCK_LOADS */
1186 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf),	/* MEM_INST_RETIRED.SPLIT_LOADS */
1187 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf),	/* MEM_INST_RETIRED.SPLIT_STORES */
1188 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf),	/* MEM_INST_RETIRED.ALL_LOADS */
1189 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf),	/* MEM_INST_RETIRED.ALL_STORES */
1190 
1191 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD_RANGE(0xd1, 0xd4, 0xf),
1192 
1193 	INTEL_FLAGS_EVENT_CONSTRAINT(0xd0, 0xf),
1194 
1195 	/*
1196 	 * Everything else is handled by PMU_FL_PEBS_ALL, because we
1197 	 * need the full constraints from the main table.
1198 	 */
1199 
1200 	EVENT_CONSTRAINT_END
1201 };
1202 
intel_pebs_constraints(struct perf_event * event)1203 struct event_constraint *intel_pebs_constraints(struct perf_event *event)
1204 {
1205 	struct event_constraint *pebs_constraints = hybrid(event->pmu, pebs_constraints);
1206 	struct event_constraint *c;
1207 
1208 	if (!event->attr.precise_ip)
1209 		return NULL;
1210 
1211 	if (pebs_constraints) {
1212 		for_each_event_constraint(c, pebs_constraints) {
1213 			if (constraint_match(c, event->hw.config)) {
1214 				event->hw.flags |= c->flags;
1215 				return c;
1216 			}
1217 		}
1218 	}
1219 
1220 	/*
1221 	 * Extended PEBS support
1222 	 * Makes the PEBS code search the normal constraints.
1223 	 */
1224 	if (x86_pmu.flags & PMU_FL_PEBS_ALL)
1225 		return NULL;
1226 
1227 	return &emptyconstraint;
1228 }
1229 
1230 /*
1231  * We need the sched_task callback even for per-cpu events when we use
1232  * the large interrupt threshold, such that we can provide PID and TID
1233  * to PEBS samples.
1234  */
pebs_needs_sched_cb(struct cpu_hw_events * cpuc)1235 static inline bool pebs_needs_sched_cb(struct cpu_hw_events *cpuc)
1236 {
1237 	if (cpuc->n_pebs == cpuc->n_pebs_via_pt)
1238 		return false;
1239 
1240 	return cpuc->n_pebs && (cpuc->n_pebs == cpuc->n_large_pebs);
1241 }
1242 
intel_pmu_pebs_sched_task(struct perf_event_pmu_context * pmu_ctx,bool sched_in)1243 void intel_pmu_pebs_sched_task(struct perf_event_pmu_context *pmu_ctx, bool sched_in)
1244 {
1245 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1246 
1247 	if (!sched_in && pebs_needs_sched_cb(cpuc))
1248 		intel_pmu_drain_pebs_buffer();
1249 }
1250 
pebs_update_threshold(struct cpu_hw_events * cpuc)1251 static inline void pebs_update_threshold(struct cpu_hw_events *cpuc)
1252 {
1253 	struct debug_store *ds = cpuc->ds;
1254 	int max_pebs_events = intel_pmu_max_num_pebs(cpuc->pmu);
1255 	u64 threshold;
1256 	int reserved;
1257 
1258 	if (cpuc->n_pebs_via_pt)
1259 		return;
1260 
1261 	if (x86_pmu.flags & PMU_FL_PEBS_ALL)
1262 		reserved = max_pebs_events + x86_pmu_max_num_counters_fixed(cpuc->pmu);
1263 	else
1264 		reserved = max_pebs_events;
1265 
1266 	if (cpuc->n_pebs == cpuc->n_large_pebs) {
1267 		threshold = ds->pebs_absolute_maximum -
1268 			reserved * cpuc->pebs_record_size;
1269 	} else {
1270 		threshold = ds->pebs_buffer_base + cpuc->pebs_record_size;
1271 	}
1272 
1273 	ds->pebs_interrupt_threshold = threshold;
1274 }
1275 
adaptive_pebs_record_size_update(void)1276 static void adaptive_pebs_record_size_update(void)
1277 {
1278 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1279 	u64 pebs_data_cfg = cpuc->pebs_data_cfg;
1280 	int sz = sizeof(struct pebs_basic);
1281 
1282 	if (pebs_data_cfg & PEBS_DATACFG_MEMINFO)
1283 		sz += sizeof(struct pebs_meminfo);
1284 	if (pebs_data_cfg & PEBS_DATACFG_GP)
1285 		sz += sizeof(struct pebs_gprs);
1286 	if (pebs_data_cfg & PEBS_DATACFG_XMMS)
1287 		sz += sizeof(struct pebs_xmm);
1288 	if (pebs_data_cfg & PEBS_DATACFG_LBRS)
1289 		sz += x86_pmu.lbr_nr * sizeof(struct lbr_entry);
1290 
1291 	cpuc->pebs_record_size = sz;
1292 }
1293 
1294 #define PERF_PEBS_MEMINFO_TYPE	(PERF_SAMPLE_ADDR | PERF_SAMPLE_DATA_SRC |   \
1295 				PERF_SAMPLE_PHYS_ADDR |			     \
1296 				PERF_SAMPLE_WEIGHT_TYPE |		     \
1297 				PERF_SAMPLE_TRANSACTION |		     \
1298 				PERF_SAMPLE_DATA_PAGE_SIZE)
1299 
pebs_update_adaptive_cfg(struct perf_event * event)1300 static u64 pebs_update_adaptive_cfg(struct perf_event *event)
1301 {
1302 	struct perf_event_attr *attr = &event->attr;
1303 	u64 sample_type = attr->sample_type;
1304 	u64 pebs_data_cfg = 0;
1305 	bool gprs, tsx_weight;
1306 
1307 	if (!(sample_type & ~(PERF_SAMPLE_IP|PERF_SAMPLE_TIME)) &&
1308 	    attr->precise_ip > 1)
1309 		return pebs_data_cfg;
1310 
1311 	if (sample_type & PERF_PEBS_MEMINFO_TYPE)
1312 		pebs_data_cfg |= PEBS_DATACFG_MEMINFO;
1313 
1314 	/*
1315 	 * We need GPRs when:
1316 	 * + user requested them
1317 	 * + precise_ip < 2 for the non event IP
1318 	 * + For RTM TSX weight we need GPRs for the abort code.
1319 	 */
1320 	gprs = ((sample_type & PERF_SAMPLE_REGS_INTR) &&
1321 		(attr->sample_regs_intr & PEBS_GP_REGS)) ||
1322 	       ((sample_type & PERF_SAMPLE_REGS_USER) &&
1323 		(attr->sample_regs_user & PEBS_GP_REGS));
1324 
1325 	tsx_weight = (sample_type & PERF_SAMPLE_WEIGHT_TYPE) &&
1326 		     ((attr->config & INTEL_ARCH_EVENT_MASK) ==
1327 		      x86_pmu.rtm_abort_event);
1328 
1329 	if (gprs || (attr->precise_ip < 2) || tsx_weight)
1330 		pebs_data_cfg |= PEBS_DATACFG_GP;
1331 
1332 	if ((sample_type & PERF_SAMPLE_REGS_INTR) &&
1333 	    (attr->sample_regs_intr & PERF_REG_EXTENDED_MASK))
1334 		pebs_data_cfg |= PEBS_DATACFG_XMMS;
1335 
1336 	if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
1337 		/*
1338 		 * For now always log all LBRs. Could configure this
1339 		 * later.
1340 		 */
1341 		pebs_data_cfg |= PEBS_DATACFG_LBRS |
1342 			((x86_pmu.lbr_nr-1) << PEBS_DATACFG_LBR_SHIFT);
1343 	}
1344 
1345 	return pebs_data_cfg;
1346 }
1347 
1348 static void
pebs_update_state(bool needed_cb,struct cpu_hw_events * cpuc,struct perf_event * event,bool add)1349 pebs_update_state(bool needed_cb, struct cpu_hw_events *cpuc,
1350 		  struct perf_event *event, bool add)
1351 {
1352 	struct pmu *pmu = event->pmu;
1353 
1354 	/*
1355 	 * Make sure we get updated with the first PEBS event.
1356 	 * During removal, ->pebs_data_cfg is still valid for
1357 	 * the last PEBS event. Don't clear it.
1358 	 */
1359 	if ((cpuc->n_pebs == 1) && add)
1360 		cpuc->pebs_data_cfg = PEBS_UPDATE_DS_SW;
1361 
1362 	if (needed_cb != pebs_needs_sched_cb(cpuc)) {
1363 		if (!needed_cb)
1364 			perf_sched_cb_inc(pmu);
1365 		else
1366 			perf_sched_cb_dec(pmu);
1367 
1368 		cpuc->pebs_data_cfg |= PEBS_UPDATE_DS_SW;
1369 	}
1370 
1371 	/*
1372 	 * The PEBS record doesn't shrink on pmu::del(). Doing so would require
1373 	 * iterating all remaining PEBS events to reconstruct the config.
1374 	 */
1375 	if (x86_pmu.intel_cap.pebs_baseline && add) {
1376 		u64 pebs_data_cfg;
1377 
1378 		pebs_data_cfg = pebs_update_adaptive_cfg(event);
1379 		/*
1380 		 * Be sure to update the thresholds when we change the record.
1381 		 */
1382 		if (pebs_data_cfg & ~cpuc->pebs_data_cfg)
1383 			cpuc->pebs_data_cfg |= pebs_data_cfg | PEBS_UPDATE_DS_SW;
1384 	}
1385 }
1386 
intel_pmu_pebs_add(struct perf_event * event)1387 void intel_pmu_pebs_add(struct perf_event *event)
1388 {
1389 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1390 	struct hw_perf_event *hwc = &event->hw;
1391 	bool needed_cb = pebs_needs_sched_cb(cpuc);
1392 
1393 	cpuc->n_pebs++;
1394 	if (hwc->flags & PERF_X86_EVENT_LARGE_PEBS)
1395 		cpuc->n_large_pebs++;
1396 	if (hwc->flags & PERF_X86_EVENT_PEBS_VIA_PT)
1397 		cpuc->n_pebs_via_pt++;
1398 
1399 	pebs_update_state(needed_cb, cpuc, event, true);
1400 }
1401 
intel_pmu_pebs_via_pt_disable(struct perf_event * event)1402 static void intel_pmu_pebs_via_pt_disable(struct perf_event *event)
1403 {
1404 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1405 
1406 	if (!is_pebs_pt(event))
1407 		return;
1408 
1409 	if (!(cpuc->pebs_enabled & ~PEBS_VIA_PT_MASK))
1410 		cpuc->pebs_enabled &= ~PEBS_VIA_PT_MASK;
1411 }
1412 
intel_pmu_pebs_via_pt_enable(struct perf_event * event)1413 static void intel_pmu_pebs_via_pt_enable(struct perf_event *event)
1414 {
1415 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1416 	struct hw_perf_event *hwc = &event->hw;
1417 	struct debug_store *ds = cpuc->ds;
1418 	u64 value = ds->pebs_event_reset[hwc->idx];
1419 	u32 base = MSR_RELOAD_PMC0;
1420 	unsigned int idx = hwc->idx;
1421 
1422 	if (!is_pebs_pt(event))
1423 		return;
1424 
1425 	if (!(event->hw.flags & PERF_X86_EVENT_LARGE_PEBS))
1426 		cpuc->pebs_enabled |= PEBS_PMI_AFTER_EACH_RECORD;
1427 
1428 	cpuc->pebs_enabled |= PEBS_OUTPUT_PT;
1429 
1430 	if (hwc->idx >= INTEL_PMC_IDX_FIXED) {
1431 		base = MSR_RELOAD_FIXED_CTR0;
1432 		idx = hwc->idx - INTEL_PMC_IDX_FIXED;
1433 		if (x86_pmu.intel_cap.pebs_format < 5)
1434 			value = ds->pebs_event_reset[MAX_PEBS_EVENTS_FMT4 + idx];
1435 		else
1436 			value = ds->pebs_event_reset[MAX_PEBS_EVENTS + idx];
1437 	}
1438 	wrmsrl(base + idx, value);
1439 }
1440 
intel_pmu_drain_large_pebs(struct cpu_hw_events * cpuc)1441 static inline void intel_pmu_drain_large_pebs(struct cpu_hw_events *cpuc)
1442 {
1443 	if (cpuc->n_pebs == cpuc->n_large_pebs &&
1444 	    cpuc->n_pebs != cpuc->n_pebs_via_pt)
1445 		intel_pmu_drain_pebs_buffer();
1446 }
1447 
intel_pmu_pebs_enable(struct perf_event * event)1448 void intel_pmu_pebs_enable(struct perf_event *event)
1449 {
1450 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1451 	u64 pebs_data_cfg = cpuc->pebs_data_cfg & ~PEBS_UPDATE_DS_SW;
1452 	struct hw_perf_event *hwc = &event->hw;
1453 	struct debug_store *ds = cpuc->ds;
1454 	unsigned int idx = hwc->idx;
1455 
1456 	hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;
1457 
1458 	cpuc->pebs_enabled |= 1ULL << hwc->idx;
1459 
1460 	if ((event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT) && (x86_pmu.version < 5))
1461 		cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32);
1462 	else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
1463 		cpuc->pebs_enabled |= 1ULL << 63;
1464 
1465 	if (x86_pmu.intel_cap.pebs_baseline) {
1466 		hwc->config |= ICL_EVENTSEL_ADAPTIVE;
1467 		if (pebs_data_cfg != cpuc->active_pebs_data_cfg) {
1468 			/*
1469 			 * drain_pebs() assumes uniform record size;
1470 			 * hence we need to drain when changing said
1471 			 * size.
1472 			 */
1473 			intel_pmu_drain_pebs_buffer();
1474 			adaptive_pebs_record_size_update();
1475 			wrmsrl(MSR_PEBS_DATA_CFG, pebs_data_cfg);
1476 			cpuc->active_pebs_data_cfg = pebs_data_cfg;
1477 		}
1478 	}
1479 	if (cpuc->pebs_data_cfg & PEBS_UPDATE_DS_SW) {
1480 		cpuc->pebs_data_cfg = pebs_data_cfg;
1481 		pebs_update_threshold(cpuc);
1482 	}
1483 
1484 	if (idx >= INTEL_PMC_IDX_FIXED) {
1485 		if (x86_pmu.intel_cap.pebs_format < 5)
1486 			idx = MAX_PEBS_EVENTS_FMT4 + (idx - INTEL_PMC_IDX_FIXED);
1487 		else
1488 			idx = MAX_PEBS_EVENTS + (idx - INTEL_PMC_IDX_FIXED);
1489 	}
1490 
1491 	/*
1492 	 * Use auto-reload if possible to save a MSR write in the PMI.
1493 	 * This must be done in pmu::start(), because PERF_EVENT_IOC_PERIOD.
1494 	 */
1495 	if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
1496 		ds->pebs_event_reset[idx] =
1497 			(u64)(-hwc->sample_period) & x86_pmu.cntval_mask;
1498 	} else {
1499 		ds->pebs_event_reset[idx] = 0;
1500 	}
1501 
1502 	intel_pmu_pebs_via_pt_enable(event);
1503 }
1504 
intel_pmu_pebs_del(struct perf_event * event)1505 void intel_pmu_pebs_del(struct perf_event *event)
1506 {
1507 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1508 	struct hw_perf_event *hwc = &event->hw;
1509 	bool needed_cb = pebs_needs_sched_cb(cpuc);
1510 
1511 	cpuc->n_pebs--;
1512 	if (hwc->flags & PERF_X86_EVENT_LARGE_PEBS)
1513 		cpuc->n_large_pebs--;
1514 	if (hwc->flags & PERF_X86_EVENT_PEBS_VIA_PT)
1515 		cpuc->n_pebs_via_pt--;
1516 
1517 	pebs_update_state(needed_cb, cpuc, event, false);
1518 }
1519 
intel_pmu_pebs_disable(struct perf_event * event)1520 void intel_pmu_pebs_disable(struct perf_event *event)
1521 {
1522 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1523 	struct hw_perf_event *hwc = &event->hw;
1524 
1525 	intel_pmu_drain_large_pebs(cpuc);
1526 
1527 	cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
1528 
1529 	if ((event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT) &&
1530 	    (x86_pmu.version < 5))
1531 		cpuc->pebs_enabled &= ~(1ULL << (hwc->idx + 32));
1532 	else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
1533 		cpuc->pebs_enabled &= ~(1ULL << 63);
1534 
1535 	intel_pmu_pebs_via_pt_disable(event);
1536 
1537 	if (cpuc->enabled)
1538 		wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
1539 
1540 	hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
1541 }
1542 
intel_pmu_pebs_enable_all(void)1543 void intel_pmu_pebs_enable_all(void)
1544 {
1545 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1546 
1547 	if (cpuc->pebs_enabled)
1548 		wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
1549 }
1550 
intel_pmu_pebs_disable_all(void)1551 void intel_pmu_pebs_disable_all(void)
1552 {
1553 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1554 
1555 	if (cpuc->pebs_enabled)
1556 		__intel_pmu_pebs_disable_all();
1557 }
1558 
intel_pmu_pebs_fixup_ip(struct pt_regs * regs)1559 static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
1560 {
1561 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1562 	unsigned long from = cpuc->lbr_entries[0].from;
1563 	unsigned long old_to, to = cpuc->lbr_entries[0].to;
1564 	unsigned long ip = regs->ip;
1565 	int is_64bit = 0;
1566 	void *kaddr;
1567 	int size;
1568 
1569 	/*
1570 	 * We don't need to fixup if the PEBS assist is fault like
1571 	 */
1572 	if (!x86_pmu.intel_cap.pebs_trap)
1573 		return 1;
1574 
1575 	/*
1576 	 * No LBR entry, no basic block, no rewinding
1577 	 */
1578 	if (!cpuc->lbr_stack.nr || !from || !to)
1579 		return 0;
1580 
1581 	/*
1582 	 * Basic blocks should never cross user/kernel boundaries
1583 	 */
1584 	if (kernel_ip(ip) != kernel_ip(to))
1585 		return 0;
1586 
1587 	/*
1588 	 * unsigned math, either ip is before the start (impossible) or
1589 	 * the basic block is larger than 1 page (sanity)
1590 	 */
1591 	if ((ip - to) > PEBS_FIXUP_SIZE)
1592 		return 0;
1593 
1594 	/*
1595 	 * We sampled a branch insn, rewind using the LBR stack
1596 	 */
1597 	if (ip == to) {
1598 		set_linear_ip(regs, from);
1599 		return 1;
1600 	}
1601 
1602 	size = ip - to;
1603 	if (!kernel_ip(ip)) {
1604 		int bytes;
1605 		u8 *buf = this_cpu_read(insn_buffer);
1606 
1607 		/* 'size' must fit our buffer, see above */
1608 		bytes = copy_from_user_nmi(buf, (void __user *)to, size);
1609 		if (bytes != 0)
1610 			return 0;
1611 
1612 		kaddr = buf;
1613 	} else {
1614 		kaddr = (void *)to;
1615 	}
1616 
1617 	do {
1618 		struct insn insn;
1619 
1620 		old_to = to;
1621 
1622 #ifdef CONFIG_X86_64
1623 		is_64bit = kernel_ip(to) || any_64bit_mode(regs);
1624 #endif
1625 		insn_init(&insn, kaddr, size, is_64bit);
1626 
1627 		/*
1628 		 * Make sure there was not a problem decoding the instruction.
1629 		 * This is doubly important because we have an infinite loop if
1630 		 * insn.length=0.
1631 		 */
1632 		if (insn_get_length(&insn))
1633 			break;
1634 
1635 		to += insn.length;
1636 		kaddr += insn.length;
1637 		size -= insn.length;
1638 	} while (to < ip);
1639 
1640 	if (to == ip) {
1641 		set_linear_ip(regs, old_to);
1642 		return 1;
1643 	}
1644 
1645 	/*
1646 	 * Even though we decoded the basic block, the instruction stream
1647 	 * never matched the given IP, either the TO or the IP got corrupted.
1648 	 */
1649 	return 0;
1650 }
1651 
intel_get_tsx_weight(u64 tsx_tuning)1652 static inline u64 intel_get_tsx_weight(u64 tsx_tuning)
1653 {
1654 	if (tsx_tuning) {
1655 		union hsw_tsx_tuning tsx = { .value = tsx_tuning };
1656 		return tsx.cycles_last_block;
1657 	}
1658 	return 0;
1659 }
1660 
intel_get_tsx_transaction(u64 tsx_tuning,u64 ax)1661 static inline u64 intel_get_tsx_transaction(u64 tsx_tuning, u64 ax)
1662 {
1663 	u64 txn = (tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32;
1664 
1665 	/* For RTM XABORTs also log the abort code from AX */
1666 	if ((txn & PERF_TXN_TRANSACTION) && (ax & 1))
1667 		txn |= ((ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
1668 	return txn;
1669 }
1670 
get_pebs_status(void * n)1671 static inline u64 get_pebs_status(void *n)
1672 {
1673 	if (x86_pmu.intel_cap.pebs_format < 4)
1674 		return ((struct pebs_record_nhm *)n)->status;
1675 	return ((struct pebs_basic *)n)->applicable_counters;
1676 }
1677 
1678 #define PERF_X86_EVENT_PEBS_HSW_PREC \
1679 		(PERF_X86_EVENT_PEBS_ST_HSW | \
1680 		 PERF_X86_EVENT_PEBS_LD_HSW | \
1681 		 PERF_X86_EVENT_PEBS_NA_HSW)
1682 
get_data_src(struct perf_event * event,u64 aux)1683 static u64 get_data_src(struct perf_event *event, u64 aux)
1684 {
1685 	u64 val = PERF_MEM_NA;
1686 	int fl = event->hw.flags;
1687 	bool fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC);
1688 
1689 	if (fl & PERF_X86_EVENT_PEBS_LDLAT)
1690 		val = load_latency_data(event, aux);
1691 	else if (fl & PERF_X86_EVENT_PEBS_STLAT)
1692 		val = store_latency_data(event, aux);
1693 	else if (fl & PERF_X86_EVENT_PEBS_LAT_HYBRID)
1694 		val = x86_pmu.pebs_latency_data(event, aux);
1695 	else if (fst && (fl & PERF_X86_EVENT_PEBS_HSW_PREC))
1696 		val = precise_datala_hsw(event, aux);
1697 	else if (fst)
1698 		val = precise_store_data(aux);
1699 	return val;
1700 }
1701 
setup_pebs_time(struct perf_event * event,struct perf_sample_data * data,u64 tsc)1702 static void setup_pebs_time(struct perf_event *event,
1703 			    struct perf_sample_data *data,
1704 			    u64 tsc)
1705 {
1706 	/* Converting to a user-defined clock is not supported yet. */
1707 	if (event->attr.use_clockid != 0)
1708 		return;
1709 
1710 	/*
1711 	 * Doesn't support the conversion when the TSC is unstable.
1712 	 * The TSC unstable case is a corner case and very unlikely to
1713 	 * happen. If it happens, the TSC in a PEBS record will be
1714 	 * dropped and fall back to perf_event_clock().
1715 	 */
1716 	if (!using_native_sched_clock() || !sched_clock_stable())
1717 		return;
1718 
1719 	data->time = native_sched_clock_from_tsc(tsc) + __sched_clock_offset;
1720 	data->sample_flags |= PERF_SAMPLE_TIME;
1721 }
1722 
1723 #define PERF_SAMPLE_ADDR_TYPE	(PERF_SAMPLE_ADDR |		\
1724 				 PERF_SAMPLE_PHYS_ADDR |	\
1725 				 PERF_SAMPLE_DATA_PAGE_SIZE)
1726 
setup_pebs_fixed_sample_data(struct perf_event * event,struct pt_regs * iregs,void * __pebs,struct perf_sample_data * data,struct pt_regs * regs)1727 static void setup_pebs_fixed_sample_data(struct perf_event *event,
1728 				   struct pt_regs *iregs, void *__pebs,
1729 				   struct perf_sample_data *data,
1730 				   struct pt_regs *regs)
1731 {
1732 	/*
1733 	 * We cast to the biggest pebs_record but are careful not to
1734 	 * unconditionally access the 'extra' entries.
1735 	 */
1736 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1737 	struct pebs_record_skl *pebs = __pebs;
1738 	u64 sample_type;
1739 	int fll;
1740 
1741 	if (pebs == NULL)
1742 		return;
1743 
1744 	sample_type = event->attr.sample_type;
1745 	fll = event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT;
1746 
1747 	perf_sample_data_init(data, 0, event->hw.last_period);
1748 
1749 	data->period = event->hw.last_period;
1750 
1751 	/*
1752 	 * Use latency for weight (only avail with PEBS-LL)
1753 	 */
1754 	if (fll && (sample_type & PERF_SAMPLE_WEIGHT_TYPE)) {
1755 		data->weight.full = pebs->lat;
1756 		data->sample_flags |= PERF_SAMPLE_WEIGHT_TYPE;
1757 	}
1758 
1759 	/*
1760 	 * data.data_src encodes the data source
1761 	 */
1762 	if (sample_type & PERF_SAMPLE_DATA_SRC) {
1763 		data->data_src.val = get_data_src(event, pebs->dse);
1764 		data->sample_flags |= PERF_SAMPLE_DATA_SRC;
1765 	}
1766 
1767 	/*
1768 	 * We must however always use iregs for the unwinder to stay sane; the
1769 	 * record BP,SP,IP can point into thin air when the record is from a
1770 	 * previous PMI context or an (I)RET happened between the record and
1771 	 * PMI.
1772 	 */
1773 	if (sample_type & PERF_SAMPLE_CALLCHAIN)
1774 		perf_sample_save_callchain(data, event, iregs);
1775 
1776 	/*
1777 	 * We use the interrupt regs as a base because the PEBS record does not
1778 	 * contain a full regs set, specifically it seems to lack segment
1779 	 * descriptors, which get used by things like user_mode().
1780 	 *
1781 	 * In the simple case fix up only the IP for PERF_SAMPLE_IP.
1782 	 */
1783 	*regs = *iregs;
1784 
1785 	/*
1786 	 * Initialize regs_>flags from PEBS,
1787 	 * Clear exact bit (which uses x86 EFLAGS Reserved bit 3),
1788 	 * i.e., do not rely on it being zero:
1789 	 */
1790 	regs->flags = pebs->flags & ~PERF_EFLAGS_EXACT;
1791 
1792 	if (sample_type & PERF_SAMPLE_REGS_INTR) {
1793 		regs->ax = pebs->ax;
1794 		regs->bx = pebs->bx;
1795 		regs->cx = pebs->cx;
1796 		regs->dx = pebs->dx;
1797 		regs->si = pebs->si;
1798 		regs->di = pebs->di;
1799 
1800 		regs->bp = pebs->bp;
1801 		regs->sp = pebs->sp;
1802 
1803 #ifndef CONFIG_X86_32
1804 		regs->r8 = pebs->r8;
1805 		regs->r9 = pebs->r9;
1806 		regs->r10 = pebs->r10;
1807 		regs->r11 = pebs->r11;
1808 		regs->r12 = pebs->r12;
1809 		regs->r13 = pebs->r13;
1810 		regs->r14 = pebs->r14;
1811 		regs->r15 = pebs->r15;
1812 #endif
1813 	}
1814 
1815 	if (event->attr.precise_ip > 1) {
1816 		/*
1817 		 * Haswell and later processors have an 'eventing IP'
1818 		 * (real IP) which fixes the off-by-1 skid in hardware.
1819 		 * Use it when precise_ip >= 2 :
1820 		 */
1821 		if (x86_pmu.intel_cap.pebs_format >= 2) {
1822 			set_linear_ip(regs, pebs->real_ip);
1823 			regs->flags |= PERF_EFLAGS_EXACT;
1824 		} else {
1825 			/* Otherwise, use PEBS off-by-1 IP: */
1826 			set_linear_ip(regs, pebs->ip);
1827 
1828 			/*
1829 			 * With precise_ip >= 2, try to fix up the off-by-1 IP
1830 			 * using the LBR. If successful, the fixup function
1831 			 * corrects regs->ip and calls set_linear_ip() on regs:
1832 			 */
1833 			if (intel_pmu_pebs_fixup_ip(regs))
1834 				regs->flags |= PERF_EFLAGS_EXACT;
1835 		}
1836 	} else {
1837 		/*
1838 		 * When precise_ip == 1, return the PEBS off-by-1 IP,
1839 		 * no fixup attempted:
1840 		 */
1841 		set_linear_ip(regs, pebs->ip);
1842 	}
1843 
1844 
1845 	if ((sample_type & PERF_SAMPLE_ADDR_TYPE) &&
1846 	    x86_pmu.intel_cap.pebs_format >= 1) {
1847 		data->addr = pebs->dla;
1848 		data->sample_flags |= PERF_SAMPLE_ADDR;
1849 	}
1850 
1851 	if (x86_pmu.intel_cap.pebs_format >= 2) {
1852 		/* Only set the TSX weight when no memory weight. */
1853 		if ((sample_type & PERF_SAMPLE_WEIGHT_TYPE) && !fll) {
1854 			data->weight.full = intel_get_tsx_weight(pebs->tsx_tuning);
1855 			data->sample_flags |= PERF_SAMPLE_WEIGHT_TYPE;
1856 		}
1857 		if (sample_type & PERF_SAMPLE_TRANSACTION) {
1858 			data->txn = intel_get_tsx_transaction(pebs->tsx_tuning,
1859 							      pebs->ax);
1860 			data->sample_flags |= PERF_SAMPLE_TRANSACTION;
1861 		}
1862 	}
1863 
1864 	/*
1865 	 * v3 supplies an accurate time stamp, so we use that
1866 	 * for the time stamp.
1867 	 *
1868 	 * We can only do this for the default trace clock.
1869 	 */
1870 	if (x86_pmu.intel_cap.pebs_format >= 3)
1871 		setup_pebs_time(event, data, pebs->tsc);
1872 
1873 	if (has_branch_stack(event))
1874 		perf_sample_save_brstack(data, event, &cpuc->lbr_stack, NULL);
1875 }
1876 
adaptive_pebs_save_regs(struct pt_regs * regs,struct pebs_gprs * gprs)1877 static void adaptive_pebs_save_regs(struct pt_regs *regs,
1878 				    struct pebs_gprs *gprs)
1879 {
1880 	regs->ax = gprs->ax;
1881 	regs->bx = gprs->bx;
1882 	regs->cx = gprs->cx;
1883 	regs->dx = gprs->dx;
1884 	regs->si = gprs->si;
1885 	regs->di = gprs->di;
1886 	regs->bp = gprs->bp;
1887 	regs->sp = gprs->sp;
1888 #ifndef CONFIG_X86_32
1889 	regs->r8 = gprs->r8;
1890 	regs->r9 = gprs->r9;
1891 	regs->r10 = gprs->r10;
1892 	regs->r11 = gprs->r11;
1893 	regs->r12 = gprs->r12;
1894 	regs->r13 = gprs->r13;
1895 	regs->r14 = gprs->r14;
1896 	regs->r15 = gprs->r15;
1897 #endif
1898 }
1899 
1900 #define PEBS_LATENCY_MASK			0xffff
1901 #define PEBS_CACHE_LATENCY_OFFSET		32
1902 #define PEBS_RETIRE_LATENCY_OFFSET		32
1903 
1904 /*
1905  * With adaptive PEBS the layout depends on what fields are configured.
1906  */
1907 
setup_pebs_adaptive_sample_data(struct perf_event * event,struct pt_regs * iregs,void * __pebs,struct perf_sample_data * data,struct pt_regs * regs)1908 static void setup_pebs_adaptive_sample_data(struct perf_event *event,
1909 					    struct pt_regs *iregs, void *__pebs,
1910 					    struct perf_sample_data *data,
1911 					    struct pt_regs *regs)
1912 {
1913 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1914 	struct pebs_basic *basic = __pebs;
1915 	void *next_record = basic + 1;
1916 	u64 sample_type;
1917 	u64 format_size;
1918 	struct pebs_meminfo *meminfo = NULL;
1919 	struct pebs_gprs *gprs = NULL;
1920 	struct x86_perf_regs *perf_regs;
1921 
1922 	if (basic == NULL)
1923 		return;
1924 
1925 	perf_regs = container_of(regs, struct x86_perf_regs, regs);
1926 	perf_regs->xmm_regs = NULL;
1927 
1928 	sample_type = event->attr.sample_type;
1929 	format_size = basic->format_size;
1930 	perf_sample_data_init(data, 0, event->hw.last_period);
1931 	data->period = event->hw.last_period;
1932 
1933 	setup_pebs_time(event, data, basic->tsc);
1934 
1935 	/*
1936 	 * We must however always use iregs for the unwinder to stay sane; the
1937 	 * record BP,SP,IP can point into thin air when the record is from a
1938 	 * previous PMI context or an (I)RET happened between the record and
1939 	 * PMI.
1940 	 */
1941 	if (sample_type & PERF_SAMPLE_CALLCHAIN)
1942 		perf_sample_save_callchain(data, event, iregs);
1943 
1944 	*regs = *iregs;
1945 	/* The ip in basic is EventingIP */
1946 	set_linear_ip(regs, basic->ip);
1947 	regs->flags = PERF_EFLAGS_EXACT;
1948 
1949 	if (sample_type & PERF_SAMPLE_WEIGHT_STRUCT) {
1950 		if (x86_pmu.flags & PMU_FL_RETIRE_LATENCY)
1951 			data->weight.var3_w = format_size >> PEBS_RETIRE_LATENCY_OFFSET & PEBS_LATENCY_MASK;
1952 		else
1953 			data->weight.var3_w = 0;
1954 	}
1955 
1956 	/*
1957 	 * The record for MEMINFO is in front of GP
1958 	 * But PERF_SAMPLE_TRANSACTION needs gprs->ax.
1959 	 * Save the pointer here but process later.
1960 	 */
1961 	if (format_size & PEBS_DATACFG_MEMINFO) {
1962 		meminfo = next_record;
1963 		next_record = meminfo + 1;
1964 	}
1965 
1966 	if (format_size & PEBS_DATACFG_GP) {
1967 		gprs = next_record;
1968 		next_record = gprs + 1;
1969 
1970 		if (event->attr.precise_ip < 2) {
1971 			set_linear_ip(regs, gprs->ip);
1972 			regs->flags &= ~PERF_EFLAGS_EXACT;
1973 		}
1974 
1975 		if (sample_type & (PERF_SAMPLE_REGS_INTR | PERF_SAMPLE_REGS_USER))
1976 			adaptive_pebs_save_regs(regs, gprs);
1977 	}
1978 
1979 	if (format_size & PEBS_DATACFG_MEMINFO) {
1980 		if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) {
1981 			u64 weight = meminfo->latency;
1982 
1983 			if (x86_pmu.flags & PMU_FL_INSTR_LATENCY) {
1984 				data->weight.var2_w = weight & PEBS_LATENCY_MASK;
1985 				weight >>= PEBS_CACHE_LATENCY_OFFSET;
1986 			}
1987 
1988 			/*
1989 			 * Although meminfo::latency is defined as a u64,
1990 			 * only the lower 32 bits include the valid data
1991 			 * in practice on Ice Lake and earlier platforms.
1992 			 */
1993 			if (sample_type & PERF_SAMPLE_WEIGHT) {
1994 				data->weight.full = weight ?:
1995 					intel_get_tsx_weight(meminfo->tsx_tuning);
1996 			} else {
1997 				data->weight.var1_dw = (u32)(weight & PEBS_LATENCY_MASK) ?:
1998 					intel_get_tsx_weight(meminfo->tsx_tuning);
1999 			}
2000 			data->sample_flags |= PERF_SAMPLE_WEIGHT_TYPE;
2001 		}
2002 
2003 		if (sample_type & PERF_SAMPLE_DATA_SRC) {
2004 			data->data_src.val = get_data_src(event, meminfo->aux);
2005 			data->sample_flags |= PERF_SAMPLE_DATA_SRC;
2006 		}
2007 
2008 		if (sample_type & PERF_SAMPLE_ADDR_TYPE) {
2009 			data->addr = meminfo->address;
2010 			data->sample_flags |= PERF_SAMPLE_ADDR;
2011 		}
2012 
2013 		if (sample_type & PERF_SAMPLE_TRANSACTION) {
2014 			data->txn = intel_get_tsx_transaction(meminfo->tsx_tuning,
2015 							  gprs ? gprs->ax : 0);
2016 			data->sample_flags |= PERF_SAMPLE_TRANSACTION;
2017 		}
2018 	}
2019 
2020 	if (format_size & PEBS_DATACFG_XMMS) {
2021 		struct pebs_xmm *xmm = next_record;
2022 
2023 		next_record = xmm + 1;
2024 		perf_regs->xmm_regs = xmm->xmm;
2025 	}
2026 
2027 	if (format_size & PEBS_DATACFG_LBRS) {
2028 		struct lbr_entry *lbr = next_record;
2029 		int num_lbr = ((format_size >> PEBS_DATACFG_LBR_SHIFT)
2030 					& 0xff) + 1;
2031 		next_record = next_record + num_lbr * sizeof(struct lbr_entry);
2032 
2033 		if (has_branch_stack(event)) {
2034 			intel_pmu_store_pebs_lbrs(lbr);
2035 			intel_pmu_lbr_save_brstack(data, cpuc, event);
2036 		}
2037 	}
2038 
2039 	WARN_ONCE(next_record != __pebs + (format_size >> 48),
2040 			"PEBS record size %llu, expected %llu, config %llx\n",
2041 			format_size >> 48,
2042 			(u64)(next_record - __pebs),
2043 			basic->format_size);
2044 }
2045 
2046 static inline void *
get_next_pebs_record_by_bit(void * base,void * top,int bit)2047 get_next_pebs_record_by_bit(void *base, void *top, int bit)
2048 {
2049 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2050 	void *at;
2051 	u64 pebs_status;
2052 
2053 	/*
2054 	 * fmt0 does not have a status bitfield (does not use
2055 	 * perf_record_nhm format)
2056 	 */
2057 	if (x86_pmu.intel_cap.pebs_format < 1)
2058 		return base;
2059 
2060 	if (base == NULL)
2061 		return NULL;
2062 
2063 	for (at = base; at < top; at += cpuc->pebs_record_size) {
2064 		unsigned long status = get_pebs_status(at);
2065 
2066 		if (test_bit(bit, (unsigned long *)&status)) {
2067 			/* PEBS v3 has accurate status bits */
2068 			if (x86_pmu.intel_cap.pebs_format >= 3)
2069 				return at;
2070 
2071 			if (status == (1 << bit))
2072 				return at;
2073 
2074 			/* clear non-PEBS bit and re-check */
2075 			pebs_status = status & cpuc->pebs_enabled;
2076 			pebs_status &= PEBS_COUNTER_MASK;
2077 			if (pebs_status == (1 << bit))
2078 				return at;
2079 		}
2080 	}
2081 	return NULL;
2082 }
2083 
2084 /*
2085  * Special variant of intel_pmu_save_and_restart() for auto-reload.
2086  */
2087 static int
intel_pmu_save_and_restart_reload(struct perf_event * event,int count)2088 intel_pmu_save_and_restart_reload(struct perf_event *event, int count)
2089 {
2090 	struct hw_perf_event *hwc = &event->hw;
2091 	int shift = 64 - x86_pmu.cntval_bits;
2092 	u64 period = hwc->sample_period;
2093 	u64 prev_raw_count, new_raw_count;
2094 	s64 new, old;
2095 
2096 	WARN_ON(!period);
2097 
2098 	/*
2099 	 * drain_pebs() only happens when the PMU is disabled.
2100 	 */
2101 	WARN_ON(this_cpu_read(cpu_hw_events.enabled));
2102 
2103 	prev_raw_count = local64_read(&hwc->prev_count);
2104 	rdpmcl(hwc->event_base_rdpmc, new_raw_count);
2105 	local64_set(&hwc->prev_count, new_raw_count);
2106 
2107 	/*
2108 	 * Since the counter increments a negative counter value and
2109 	 * overflows on the sign switch, giving the interval:
2110 	 *
2111 	 *   [-period, 0]
2112 	 *
2113 	 * the difference between two consecutive reads is:
2114 	 *
2115 	 *   A) value2 - value1;
2116 	 *      when no overflows have happened in between,
2117 	 *
2118 	 *   B) (0 - value1) + (value2 - (-period));
2119 	 *      when one overflow happened in between,
2120 	 *
2121 	 *   C) (0 - value1) + (n - 1) * (period) + (value2 - (-period));
2122 	 *      when @n overflows happened in between.
2123 	 *
2124 	 * Here A) is the obvious difference, B) is the extension to the
2125 	 * discrete interval, where the first term is to the top of the
2126 	 * interval and the second term is from the bottom of the next
2127 	 * interval and C) the extension to multiple intervals, where the
2128 	 * middle term is the whole intervals covered.
2129 	 *
2130 	 * An equivalent of C, by reduction, is:
2131 	 *
2132 	 *   value2 - value1 + n * period
2133 	 */
2134 	new = ((s64)(new_raw_count << shift) >> shift);
2135 	old = ((s64)(prev_raw_count << shift) >> shift);
2136 	local64_add(new - old + count * period, &event->count);
2137 
2138 	local64_set(&hwc->period_left, -new);
2139 
2140 	perf_event_update_userpage(event);
2141 
2142 	return 0;
2143 }
2144 
2145 static __always_inline void
__intel_pmu_pebs_event(struct perf_event * event,struct pt_regs * iregs,struct perf_sample_data * data,void * base,void * top,int bit,int count,void (* setup_sample)(struct perf_event *,struct pt_regs *,void *,struct perf_sample_data *,struct pt_regs *))2146 __intel_pmu_pebs_event(struct perf_event *event,
2147 		       struct pt_regs *iregs,
2148 		       struct perf_sample_data *data,
2149 		       void *base, void *top,
2150 		       int bit, int count,
2151 		       void (*setup_sample)(struct perf_event *,
2152 					    struct pt_regs *,
2153 					    void *,
2154 					    struct perf_sample_data *,
2155 					    struct pt_regs *))
2156 {
2157 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2158 	struct hw_perf_event *hwc = &event->hw;
2159 	struct x86_perf_regs perf_regs;
2160 	struct pt_regs *regs = &perf_regs.regs;
2161 	void *at = get_next_pebs_record_by_bit(base, top, bit);
2162 	static struct pt_regs dummy_iregs;
2163 
2164 	if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
2165 		/*
2166 		 * Now, auto-reload is only enabled in fixed period mode.
2167 		 * The reload value is always hwc->sample_period.
2168 		 * May need to change it, if auto-reload is enabled in
2169 		 * freq mode later.
2170 		 */
2171 		intel_pmu_save_and_restart_reload(event, count);
2172 	} else if (!intel_pmu_save_and_restart(event))
2173 		return;
2174 
2175 	if (!iregs)
2176 		iregs = &dummy_iregs;
2177 
2178 	while (count > 1) {
2179 		setup_sample(event, iregs, at, data, regs);
2180 		perf_event_output(event, data, regs);
2181 		at += cpuc->pebs_record_size;
2182 		at = get_next_pebs_record_by_bit(at, top, bit);
2183 		count--;
2184 	}
2185 
2186 	setup_sample(event, iregs, at, data, regs);
2187 	if (iregs == &dummy_iregs) {
2188 		/*
2189 		 * The PEBS records may be drained in the non-overflow context,
2190 		 * e.g., large PEBS + context switch. Perf should treat the
2191 		 * last record the same as other PEBS records, and doesn't
2192 		 * invoke the generic overflow handler.
2193 		 */
2194 		perf_event_output(event, data, regs);
2195 	} else {
2196 		/*
2197 		 * All but the last records are processed.
2198 		 * The last one is left to be able to call the overflow handler.
2199 		 */
2200 		if (perf_event_overflow(event, data, regs))
2201 			x86_pmu_stop(event, 0);
2202 	}
2203 }
2204 
intel_pmu_drain_pebs_core(struct pt_regs * iregs,struct perf_sample_data * data)2205 static void intel_pmu_drain_pebs_core(struct pt_regs *iregs, struct perf_sample_data *data)
2206 {
2207 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2208 	struct debug_store *ds = cpuc->ds;
2209 	struct perf_event *event = cpuc->events[0]; /* PMC0 only */
2210 	struct pebs_record_core *at, *top;
2211 	int n;
2212 
2213 	if (!x86_pmu.pebs_active)
2214 		return;
2215 
2216 	at  = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base;
2217 	top = (struct pebs_record_core *)(unsigned long)ds->pebs_index;
2218 
2219 	/*
2220 	 * Whatever else happens, drain the thing
2221 	 */
2222 	ds->pebs_index = ds->pebs_buffer_base;
2223 
2224 	if (!test_bit(0, cpuc->active_mask))
2225 		return;
2226 
2227 	WARN_ON_ONCE(!event);
2228 
2229 	if (!event->attr.precise_ip)
2230 		return;
2231 
2232 	n = top - at;
2233 	if (n <= 0) {
2234 		if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)
2235 			intel_pmu_save_and_restart_reload(event, 0);
2236 		return;
2237 	}
2238 
2239 	__intel_pmu_pebs_event(event, iregs, data, at, top, 0, n,
2240 			       setup_pebs_fixed_sample_data);
2241 }
2242 
intel_pmu_pebs_event_update_no_drain(struct cpu_hw_events * cpuc,u64 mask)2243 static void intel_pmu_pebs_event_update_no_drain(struct cpu_hw_events *cpuc, u64 mask)
2244 {
2245 	u64 pebs_enabled = cpuc->pebs_enabled & mask;
2246 	struct perf_event *event;
2247 	int bit;
2248 
2249 	/*
2250 	 * The drain_pebs() could be called twice in a short period
2251 	 * for auto-reload event in pmu::read(). There are no
2252 	 * overflows have happened in between.
2253 	 * It needs to call intel_pmu_save_and_restart_reload() to
2254 	 * update the event->count for this case.
2255 	 */
2256 	for_each_set_bit(bit, (unsigned long *)&pebs_enabled, X86_PMC_IDX_MAX) {
2257 		event = cpuc->events[bit];
2258 		if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)
2259 			intel_pmu_save_and_restart_reload(event, 0);
2260 	}
2261 }
2262 
intel_pmu_drain_pebs_nhm(struct pt_regs * iregs,struct perf_sample_data * data)2263 static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs, struct perf_sample_data *data)
2264 {
2265 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2266 	struct debug_store *ds = cpuc->ds;
2267 	struct perf_event *event;
2268 	void *base, *at, *top;
2269 	short counts[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {};
2270 	short error[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {};
2271 	int max_pebs_events = intel_pmu_max_num_pebs(NULL);
2272 	int bit, i, size;
2273 	u64 mask;
2274 
2275 	if (!x86_pmu.pebs_active)
2276 		return;
2277 
2278 	base = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
2279 	top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
2280 
2281 	ds->pebs_index = ds->pebs_buffer_base;
2282 
2283 	mask = x86_pmu.pebs_events_mask;
2284 	size = max_pebs_events;
2285 	if (x86_pmu.flags & PMU_FL_PEBS_ALL) {
2286 		mask |= x86_pmu.fixed_cntr_mask64 << INTEL_PMC_IDX_FIXED;
2287 		size = INTEL_PMC_IDX_FIXED + x86_pmu_max_num_counters_fixed(NULL);
2288 	}
2289 
2290 	if (unlikely(base >= top)) {
2291 		intel_pmu_pebs_event_update_no_drain(cpuc, mask);
2292 		return;
2293 	}
2294 
2295 	for (at = base; at < top; at += x86_pmu.pebs_record_size) {
2296 		struct pebs_record_nhm *p = at;
2297 		u64 pebs_status;
2298 
2299 		pebs_status = p->status & cpuc->pebs_enabled;
2300 		pebs_status &= mask;
2301 
2302 		/* PEBS v3 has more accurate status bits */
2303 		if (x86_pmu.intel_cap.pebs_format >= 3) {
2304 			for_each_set_bit(bit, (unsigned long *)&pebs_status, size)
2305 				counts[bit]++;
2306 
2307 			continue;
2308 		}
2309 
2310 		/*
2311 		 * On some CPUs the PEBS status can be zero when PEBS is
2312 		 * racing with clearing of GLOBAL_STATUS.
2313 		 *
2314 		 * Normally we would drop that record, but in the
2315 		 * case when there is only a single active PEBS event
2316 		 * we can assume it's for that event.
2317 		 */
2318 		if (!pebs_status && cpuc->pebs_enabled &&
2319 			!(cpuc->pebs_enabled & (cpuc->pebs_enabled-1)))
2320 			pebs_status = p->status = cpuc->pebs_enabled;
2321 
2322 		bit = find_first_bit((unsigned long *)&pebs_status,
2323 				     max_pebs_events);
2324 
2325 		if (!(x86_pmu.pebs_events_mask & (1 << bit)))
2326 			continue;
2327 
2328 		/*
2329 		 * The PEBS hardware does not deal well with the situation
2330 		 * when events happen near to each other and multiple bits
2331 		 * are set. But it should happen rarely.
2332 		 *
2333 		 * If these events include one PEBS and multiple non-PEBS
2334 		 * events, it doesn't impact PEBS record. The record will
2335 		 * be handled normally. (slow path)
2336 		 *
2337 		 * If these events include two or more PEBS events, the
2338 		 * records for the events can be collapsed into a single
2339 		 * one, and it's not possible to reconstruct all events
2340 		 * that caused the PEBS record. It's called collision.
2341 		 * If collision happened, the record will be dropped.
2342 		 */
2343 		if (pebs_status != (1ULL << bit)) {
2344 			for_each_set_bit(i, (unsigned long *)&pebs_status, size)
2345 				error[i]++;
2346 			continue;
2347 		}
2348 
2349 		counts[bit]++;
2350 	}
2351 
2352 	for_each_set_bit(bit, (unsigned long *)&mask, size) {
2353 		if ((counts[bit] == 0) && (error[bit] == 0))
2354 			continue;
2355 
2356 		event = cpuc->events[bit];
2357 		if (WARN_ON_ONCE(!event))
2358 			continue;
2359 
2360 		if (WARN_ON_ONCE(!event->attr.precise_ip))
2361 			continue;
2362 
2363 		/* log dropped samples number */
2364 		if (error[bit]) {
2365 			perf_log_lost_samples(event, error[bit]);
2366 
2367 			if (iregs && perf_event_account_interrupt(event))
2368 				x86_pmu_stop(event, 0);
2369 		}
2370 
2371 		if (counts[bit]) {
2372 			__intel_pmu_pebs_event(event, iregs, data, base,
2373 					       top, bit, counts[bit],
2374 					       setup_pebs_fixed_sample_data);
2375 		}
2376 	}
2377 }
2378 
intel_pmu_drain_pebs_icl(struct pt_regs * iregs,struct perf_sample_data * data)2379 static void intel_pmu_drain_pebs_icl(struct pt_regs *iregs, struct perf_sample_data *data)
2380 {
2381 	short counts[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {};
2382 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2383 	struct debug_store *ds = cpuc->ds;
2384 	struct perf_event *event;
2385 	void *base, *at, *top;
2386 	int bit;
2387 	u64 mask;
2388 
2389 	if (!x86_pmu.pebs_active)
2390 		return;
2391 
2392 	base = (struct pebs_basic *)(unsigned long)ds->pebs_buffer_base;
2393 	top = (struct pebs_basic *)(unsigned long)ds->pebs_index;
2394 
2395 	ds->pebs_index = ds->pebs_buffer_base;
2396 
2397 	mask = hybrid(cpuc->pmu, pebs_events_mask) |
2398 	       (hybrid(cpuc->pmu, fixed_cntr_mask64) << INTEL_PMC_IDX_FIXED);
2399 
2400 	if (unlikely(base >= top)) {
2401 		intel_pmu_pebs_event_update_no_drain(cpuc, mask);
2402 		return;
2403 	}
2404 
2405 	for (at = base; at < top; at += cpuc->pebs_record_size) {
2406 		u64 pebs_status;
2407 
2408 		pebs_status = get_pebs_status(at) & cpuc->pebs_enabled;
2409 		pebs_status &= mask;
2410 
2411 		for_each_set_bit(bit, (unsigned long *)&pebs_status, X86_PMC_IDX_MAX)
2412 			counts[bit]++;
2413 	}
2414 
2415 	for_each_set_bit(bit, (unsigned long *)&mask, X86_PMC_IDX_MAX) {
2416 		if (counts[bit] == 0)
2417 			continue;
2418 
2419 		event = cpuc->events[bit];
2420 		if (WARN_ON_ONCE(!event))
2421 			continue;
2422 
2423 		if (WARN_ON_ONCE(!event->attr.precise_ip))
2424 			continue;
2425 
2426 		__intel_pmu_pebs_event(event, iregs, data, base,
2427 				       top, bit, counts[bit],
2428 				       setup_pebs_adaptive_sample_data);
2429 	}
2430 }
2431 
2432 /*
2433  * BTS, PEBS probe and setup
2434  */
2435 
intel_ds_init(void)2436 void __init intel_ds_init(void)
2437 {
2438 	/*
2439 	 * No support for 32bit formats
2440 	 */
2441 	if (!boot_cpu_has(X86_FEATURE_DTES64))
2442 		return;
2443 
2444 	x86_pmu.bts  = boot_cpu_has(X86_FEATURE_BTS);
2445 	x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS);
2446 	x86_pmu.pebs_buffer_size = PEBS_BUFFER_SIZE;
2447 	if (x86_pmu.version <= 4)
2448 		x86_pmu.pebs_no_isolation = 1;
2449 
2450 	if (x86_pmu.pebs) {
2451 		char pebs_type = x86_pmu.intel_cap.pebs_trap ?  '+' : '-';
2452 		char *pebs_qual = "";
2453 		int format = x86_pmu.intel_cap.pebs_format;
2454 
2455 		if (format < 4)
2456 			x86_pmu.intel_cap.pebs_baseline = 0;
2457 
2458 		switch (format) {
2459 		case 0:
2460 			pr_cont("PEBS fmt0%c, ", pebs_type);
2461 			x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
2462 			/*
2463 			 * Using >PAGE_SIZE buffers makes the WRMSR to
2464 			 * PERF_GLOBAL_CTRL in intel_pmu_enable_all()
2465 			 * mysteriously hang on Core2.
2466 			 *
2467 			 * As a workaround, we don't do this.
2468 			 */
2469 			x86_pmu.pebs_buffer_size = PAGE_SIZE;
2470 			x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
2471 			break;
2472 
2473 		case 1:
2474 			pr_cont("PEBS fmt1%c, ", pebs_type);
2475 			x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
2476 			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
2477 			break;
2478 
2479 		case 2:
2480 			pr_cont("PEBS fmt2%c, ", pebs_type);
2481 			x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw);
2482 			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
2483 			break;
2484 
2485 		case 3:
2486 			pr_cont("PEBS fmt3%c, ", pebs_type);
2487 			x86_pmu.pebs_record_size =
2488 						sizeof(struct pebs_record_skl);
2489 			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
2490 			x86_pmu.large_pebs_flags |= PERF_SAMPLE_TIME;
2491 			break;
2492 
2493 		case 6:
2494 		case 5:
2495 			x86_pmu.pebs_ept = 1;
2496 			fallthrough;
2497 		case 4:
2498 			x86_pmu.drain_pebs = intel_pmu_drain_pebs_icl;
2499 			x86_pmu.pebs_record_size = sizeof(struct pebs_basic);
2500 			if (x86_pmu.intel_cap.pebs_baseline) {
2501 				x86_pmu.large_pebs_flags |=
2502 					PERF_SAMPLE_BRANCH_STACK |
2503 					PERF_SAMPLE_TIME;
2504 				x86_pmu.flags |= PMU_FL_PEBS_ALL;
2505 				x86_pmu.pebs_capable = ~0ULL;
2506 				pebs_qual = "-baseline";
2507 				x86_get_pmu(smp_processor_id())->capabilities |= PERF_PMU_CAP_EXTENDED_REGS;
2508 			} else {
2509 				/* Only basic record supported */
2510 				x86_pmu.large_pebs_flags &=
2511 					~(PERF_SAMPLE_ADDR |
2512 					  PERF_SAMPLE_TIME |
2513 					  PERF_SAMPLE_DATA_SRC |
2514 					  PERF_SAMPLE_TRANSACTION |
2515 					  PERF_SAMPLE_REGS_USER |
2516 					  PERF_SAMPLE_REGS_INTR);
2517 			}
2518 			pr_cont("PEBS fmt4%c%s, ", pebs_type, pebs_qual);
2519 
2520 			/*
2521 			 * The PEBS-via-PT is not supported on hybrid platforms,
2522 			 * because not all CPUs of a hybrid machine support it.
2523 			 * The global x86_pmu.intel_cap, which only contains the
2524 			 * common capabilities, is used to check the availability
2525 			 * of the feature. The per-PMU pebs_output_pt_available
2526 			 * in a hybrid machine should be ignored.
2527 			 */
2528 			if (x86_pmu.intel_cap.pebs_output_pt_available) {
2529 				pr_cont("PEBS-via-PT, ");
2530 				x86_get_pmu(smp_processor_id())->capabilities |= PERF_PMU_CAP_AUX_OUTPUT;
2531 			}
2532 
2533 			break;
2534 
2535 		default:
2536 			pr_cont("no PEBS fmt%d%c, ", format, pebs_type);
2537 			x86_pmu.pebs = 0;
2538 		}
2539 	}
2540 }
2541 
perf_restore_debug_store(void)2542 void perf_restore_debug_store(void)
2543 {
2544 	struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
2545 
2546 	if (!x86_pmu.bts && !x86_pmu.pebs)
2547 		return;
2548 
2549 	wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds);
2550 }
2551