xref: /linux/arch/x86/events/intel/ds.c (revision 546b928da0427b0d6c663cbb992bd7bfa9ac7971)
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/msr.h>
14 #include <asm/timer.h>
15 
16 #include "../perf_event.h"
17 
18 /* Waste a full page so it can be mapped into the cpu_entry_area */
19 DEFINE_PER_CPU_PAGE_ALIGNED(struct debug_store, cpu_debug_store);
20 
21 /* The size of a BTS record in bytes: */
22 #define BTS_RECORD_SIZE		24
23 
24 #define PEBS_FIXUP_SIZE		PAGE_SIZE
25 
26 /*
27  * pebs_record_32 for p4 and core not supported
28 
29 struct pebs_record_32 {
30 	u32 flags, ip;
31 	u32 ax, bc, cx, dx;
32 	u32 si, di, bp, sp;
33 };
34 
35  */
36 
37 union omr_encoding {
38 	struct {
39 		u8 omr_source : 4;
40 		u8 omr_remote : 1;
41 		u8 omr_hitm : 1;
42 		u8 omr_snoop : 1;
43 		u8 omr_promoted : 1;
44 	};
45 	u8 omr_full;
46 };
47 
48 union intel_x86_pebs_dse {
49 	u64 val;
50 	struct {
51 		unsigned int ld_dse:4;
52 		unsigned int ld_stlb_miss:1;
53 		unsigned int ld_locked:1;
54 		unsigned int ld_data_blk:1;
55 		unsigned int ld_addr_blk:1;
56 		unsigned int ld_reserved:24;
57 	};
58 	struct {
59 		unsigned int st_l1d_hit:1;
60 		unsigned int st_reserved1:3;
61 		unsigned int st_stlb_miss:1;
62 		unsigned int st_locked:1;
63 		unsigned int st_reserved2:26;
64 	};
65 	struct {
66 		unsigned int st_lat_dse:4;
67 		unsigned int st_lat_stlb_miss:1;
68 		unsigned int st_lat_locked:1;
69 		unsigned int ld_reserved3:26;
70 	};
71 	struct {
72 		unsigned int mtl_dse:5;
73 		unsigned int mtl_locked:1;
74 		unsigned int mtl_stlb_miss:1;
75 		unsigned int mtl_fwd_blk:1;
76 		unsigned int ld_reserved4:24;
77 	};
78 	struct {
79 		unsigned int lnc_dse:8;
80 		unsigned int ld_reserved5:2;
81 		unsigned int lnc_stlb_miss:1;
82 		unsigned int lnc_locked:1;
83 		unsigned int lnc_data_blk:1;
84 		unsigned int lnc_addr_blk:1;
85 		unsigned int ld_reserved6:18;
86 	};
87 	struct {
88 		unsigned int pnc_dse: 8;
89 		unsigned int pnc_l2_miss:1;
90 		unsigned int pnc_stlb_clean_hit:1;
91 		unsigned int pnc_stlb_any_hit:1;
92 		unsigned int pnc_stlb_miss:1;
93 		unsigned int pnc_locked:1;
94 		unsigned int pnc_data_blk:1;
95 		unsigned int pnc_addr_blk:1;
96 		unsigned int pnc_fb_full:1;
97 		unsigned int ld_reserved8:16;
98 	};
99 	struct {
100 		unsigned int arw_dse:8;
101 		unsigned int arw_l2_miss:1;
102 		unsigned int arw_xq_promotion:1;
103 		unsigned int arw_reissue:1;
104 		unsigned int arw_stlb_miss:1;
105 		unsigned int arw_locked:1;
106 		unsigned int arw_data_blk:1;
107 		unsigned int arw_addr_blk:1;
108 		unsigned int arw_fb_full:1;
109 		unsigned int ld_reserved9:16;
110 	};
111 };
112 
113 
114 /*
115  * Map PEBS Load Latency Data Source encodings to generic
116  * memory data source information
117  */
118 #define P(a, b) PERF_MEM_S(a, b)
119 #define OP_LH (P(OP, LOAD) | P(LVL, HIT))
120 #define LEVEL(x) P(LVLNUM, x)
121 #define REM P(REMOTE, REMOTE)
122 #define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS))
123 
124 /* Version for Sandy Bridge and later */
125 static u64 pebs_data_source[PERF_PEBS_DATA_SOURCE_MAX] = {
126 	P(OP, LOAD) | P(LVL, MISS) | LEVEL(L3) | P(SNOOP, NA),/* 0x00:ukn L3 */
127 	OP_LH | P(LVL, L1)  | LEVEL(L1) | P(SNOOP, NONE),  /* 0x01: L1 local */
128 	OP_LH | P(LVL, LFB) | LEVEL(LFB) | P(SNOOP, NONE), /* 0x02: LFB hit */
129 	OP_LH | P(LVL, L2)  | LEVEL(L2) | P(SNOOP, NONE),  /* 0x03: L2 hit */
130 	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, NONE),  /* 0x04: L3 hit */
131 	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, MISS),  /* 0x05: L3 hit, snoop miss */
132 	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, HIT),   /* 0x06: L3 hit, snoop hit */
133 	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, HITM),  /* 0x07: L3 hit, snoop hitm */
134 	OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HIT),  /* 0x08: L3 miss snoop hit */
135 	OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HITM), /* 0x09: L3 miss snoop hitm*/
136 	OP_LH | P(LVL, LOC_RAM)  | LEVEL(RAM) | P(SNOOP, HIT),       /* 0x0a: L3 miss, shared */
137 	OP_LH | P(LVL, REM_RAM1) | REM | LEVEL(L3) | P(SNOOP, HIT),  /* 0x0b: L3 miss, shared */
138 	OP_LH | P(LVL, LOC_RAM)  | LEVEL(RAM) | SNOOP_NONE_MISS,     /* 0x0c: L3 miss, excl */
139 	OP_LH | P(LVL, REM_RAM1) | LEVEL(RAM) | REM | SNOOP_NONE_MISS, /* 0x0d: L3 miss, excl */
140 	OP_LH | P(LVL, IO)  | LEVEL(NA) | P(SNOOP, NONE), /* 0x0e: I/O */
141 	OP_LH | P(LVL, UNC) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0f: uncached */
142 };
143 
144 /* Patch up minor differences in the bits */
intel_pmu_pebs_data_source_nhm(void)145 void __init intel_pmu_pebs_data_source_nhm(void)
146 {
147 	pebs_data_source[0x05] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT);
148 	pebs_data_source[0x06] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
149 	pebs_data_source[0x07] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
150 }
151 
__intel_pmu_pebs_data_source_skl(bool pmem,u64 * data_source)152 static void __init __intel_pmu_pebs_data_source_skl(bool pmem, u64 *data_source)
153 {
154 	u64 pmem_or_l4 = pmem ? LEVEL(PMEM) : LEVEL(L4);
155 
156 	data_source[0x08] = OP_LH | pmem_or_l4 | P(SNOOP, HIT);
157 	data_source[0x09] = OP_LH | pmem_or_l4 | REM | P(SNOOP, HIT);
158 	data_source[0x0b] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, NONE);
159 	data_source[0x0c] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOPX, FWD);
160 	data_source[0x0d] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOP, HITM);
161 }
162 
intel_pmu_pebs_data_source_skl(bool pmem)163 void __init intel_pmu_pebs_data_source_skl(bool pmem)
164 {
165 	__intel_pmu_pebs_data_source_skl(pmem, pebs_data_source);
166 }
167 
__intel_pmu_pebs_data_source_grt(u64 * data_source)168 static void __init __intel_pmu_pebs_data_source_grt(u64 *data_source)
169 {
170 	data_source[0x05] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT);
171 	data_source[0x06] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
172 	data_source[0x08] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOPX, FWD);
173 }
174 
intel_pmu_pebs_data_source_grt(void)175 void __init intel_pmu_pebs_data_source_grt(void)
176 {
177 	__intel_pmu_pebs_data_source_grt(pebs_data_source);
178 }
179 
intel_pmu_pebs_data_source_adl(void)180 void __init intel_pmu_pebs_data_source_adl(void)
181 {
182 	u64 *data_source;
183 
184 	data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX].pebs_data_source;
185 	memcpy(data_source, pebs_data_source, sizeof(pebs_data_source));
186 	__intel_pmu_pebs_data_source_skl(false, data_source);
187 
188 	data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_ATOM_IDX].pebs_data_source;
189 	memcpy(data_source, pebs_data_source, sizeof(pebs_data_source));
190 	__intel_pmu_pebs_data_source_grt(data_source);
191 }
192 
__intel_pmu_pebs_data_source_cmt(u64 * data_source)193 static void __init __intel_pmu_pebs_data_source_cmt(u64 *data_source)
194 {
195 	data_source[0x07] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOPX, FWD);
196 	data_source[0x08] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
197 	data_source[0x0a] = OP_LH | P(LVL, LOC_RAM)  | LEVEL(RAM) | P(SNOOP, NONE);
198 	data_source[0x0b] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, NONE);
199 	data_source[0x0c] = OP_LH | LEVEL(RAM) | REM | P(SNOOPX, FWD);
200 	data_source[0x0d] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, HITM);
201 }
202 
intel_pmu_pebs_data_source_mtl(void)203 void __init intel_pmu_pebs_data_source_mtl(void)
204 {
205 	u64 *data_source;
206 
207 	data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX].pebs_data_source;
208 	memcpy(data_source, pebs_data_source, sizeof(pebs_data_source));
209 	__intel_pmu_pebs_data_source_skl(false, data_source);
210 
211 	data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_ATOM_IDX].pebs_data_source;
212 	memcpy(data_source, pebs_data_source, sizeof(pebs_data_source));
213 	__intel_pmu_pebs_data_source_cmt(data_source);
214 }
215 
intel_pmu_pebs_data_source_arl_h(void)216 void __init intel_pmu_pebs_data_source_arl_h(void)
217 {
218 	u64 *data_source;
219 
220 	intel_pmu_pebs_data_source_lnl();
221 
222 	data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_TINY_IDX].pebs_data_source;
223 	memcpy(data_source, pebs_data_source, sizeof(pebs_data_source));
224 	__intel_pmu_pebs_data_source_cmt(data_source);
225 }
226 
intel_pmu_pebs_data_source_cmt(void)227 void __init intel_pmu_pebs_data_source_cmt(void)
228 {
229 	__intel_pmu_pebs_data_source_cmt(pebs_data_source);
230 }
231 
232 /* Version for Lion Cove and later */
233 static u64 lnc_pebs_data_source[PERF_PEBS_DATA_SOURCE_MAX] = {
234 	P(OP, LOAD) | P(LVL, MISS) | LEVEL(L3) | P(SNOOP, NA),	/* 0x00: ukn L3 */
235 	OP_LH | P(LVL, L1)  | LEVEL(L1) | P(SNOOP, NONE),	/* 0x01: L1 hit */
236 	OP_LH | P(LVL, L1)  | LEVEL(L1) | P(SNOOP, NONE),	/* 0x02: L1 hit */
237 	OP_LH | P(LVL, LFB) | LEVEL(LFB) | P(SNOOP, NONE),	/* 0x03: LFB/L1 Miss Handling Buffer hit */
238 	0,							/* 0x04: Reserved */
239 	OP_LH | P(LVL, L2)  | LEVEL(L2) | P(SNOOP, NONE),	/* 0x05: L2 Hit */
240 	OP_LH | LEVEL(L2_MHB) | P(SNOOP, NONE),			/* 0x06: L2 Miss Handling Buffer Hit */
241 	0,							/* 0x07: Reserved */
242 	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, NONE),	/* 0x08: L3 Hit */
243 	0,							/* 0x09: Reserved */
244 	0,							/* 0x0a: Reserved */
245 	0,							/* 0x0b: Reserved */
246 	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOPX, FWD),	/* 0x0c: L3 Hit Snoop Fwd */
247 	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, HITM),	/* 0x0d: L3 Hit Snoop HitM */
248 	0,							/* 0x0e: Reserved */
249 	P(OP, LOAD) | P(LVL, MISS) | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, HITM),	/* 0x0f: L3 Miss Snoop HitM */
250 	OP_LH | LEVEL(MSC) | P(SNOOP, NONE),			/* 0x10: Memory-side Cache Hit */
251 	OP_LH | P(LVL, LOC_RAM)  | LEVEL(RAM) | P(SNOOP, NONE), /* 0x11: Local Memory Hit */
252 };
253 
intel_pmu_pebs_data_source_lnl(void)254 void __init intel_pmu_pebs_data_source_lnl(void)
255 {
256 	u64 *data_source;
257 
258 	data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX].pebs_data_source;
259 	memcpy(data_source, lnc_pebs_data_source, sizeof(lnc_pebs_data_source));
260 
261 	data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_ATOM_IDX].pebs_data_source;
262 	memcpy(data_source, pebs_data_source, sizeof(pebs_data_source));
263 	__intel_pmu_pebs_data_source_cmt(data_source);
264 }
265 
266 /* Version for Panthercove and later */
267 
268 /* L2 hit */
269 #define PNC_PEBS_DATA_SOURCE_MAX	16
270 static u64 pnc_pebs_l2_hit_data_source[PNC_PEBS_DATA_SOURCE_MAX] = {
271 	P(OP, LOAD) | P(LVL, NA) | LEVEL(NA) | P(SNOOP, NA),	/* 0x00: non-cache access */
272 	OP_LH               | LEVEL(L0) | P(SNOOP, NONE),	/* 0x01: L0 hit */
273 	OP_LH | P(LVL, L1)  | LEVEL(L1) | P(SNOOP, NONE),	/* 0x02: L1 hit */
274 	OP_LH | P(LVL, LFB) | LEVEL(LFB) | P(SNOOP, NONE),	/* 0x03: L1 Miss Handling Buffer hit */
275 	OP_LH | P(LVL, L2)  | LEVEL(L2) | P(SNOOP, NONE),	/* 0x04: L2 Hit Clean */
276 	0,							/* 0x05: Reserved */
277 	0,							/* 0x06: Reserved */
278 	OP_LH | P(LVL, L2)  | LEVEL(L2) | P(SNOOP, HIT),	/* 0x07: L2 Hit Snoop HIT */
279 	OP_LH | P(LVL, L2)  | LEVEL(L2) | P(SNOOP, HITM),	/* 0x08: L2 Hit Snoop Hit Modified */
280 	OP_LH | P(LVL, L2)  | LEVEL(L2) | P(SNOOP, MISS),	/* 0x09: Prefetch Promotion */
281 	OP_LH | P(LVL, L2)  | LEVEL(L2) | P(SNOOP, MISS),	/* 0x0a: Cross Core Prefetch Promotion */
282 	0,							/* 0x0b: Reserved */
283 	0,							/* 0x0c: Reserved */
284 	0,							/* 0x0d: Reserved */
285 	0,							/* 0x0e: Reserved */
286 	OP_LH | P(LVL, UNC) | LEVEL(NA) | P(SNOOP, NONE),	/* 0x0f: uncached */
287 };
288 
289 /* Version for Arctic Wolf and later */
290 
291 /* L2 hit */
292 #define ARW_PEBS_DATA_SOURCE_MAX	16
293 static u64 arw_pebs_l2_hit_data_source[ARW_PEBS_DATA_SOURCE_MAX] = {
294 	P(OP, LOAD) | P(LVL, NA) | LEVEL(NA) | P(SNOOP, NA),	/* 0x00: non-cache access */
295 	OP_LH | P(LVL, L1)  | LEVEL(L1) | P(SNOOP, NONE),	/* 0x01: L1 hit */
296 	OP_LH | P(LVL, LFB) | LEVEL(LFB) | P(SNOOP, NONE),	/* 0x02: WCB Hit */
297 	OP_LH | P(LVL, L2)  | LEVEL(L2) | P(SNOOP, NONE),	/* 0x03: L2 Hit Clean */
298 	OP_LH | P(LVL, L2)  | LEVEL(L2) | P(SNOOP, HIT),	/* 0x04: L2 Hit Snoop HIT */
299 	OP_LH | P(LVL, L2)  | LEVEL(L2) | P(SNOOP, HITM),	/* 0x05: L2 Hit Snoop Hit Modified */
300 	OP_LH | P(LVL, UNC) | LEVEL(NA) | P(SNOOP, NONE),	/* 0x06: uncached */
301 	0,							/* 0x07: Reserved */
302 	0,							/* 0x08: Reserved */
303 	0,							/* 0x09: Reserved */
304 	0,							/* 0x0a: Reserved */
305 	0,							/* 0x0b: Reserved */
306 	0,							/* 0x0c: Reserved */
307 	0,							/* 0x0d: Reserved */
308 	0,							/* 0x0e: Reserved */
309 	0,							/* 0x0f: Reserved */
310 };
311 
312 /* L2 miss */
313 #define OMR_DATA_SOURCE_MAX		16
314 static u64 omr_data_source[OMR_DATA_SOURCE_MAX] = {
315 	P(OP, LOAD) | P(LVL, NA) | LEVEL(NA) | P(SNOOP, NA),	/* 0x00: invalid */
316 	0,							/* 0x01: Reserved */
317 	OP_LH | P(LVL, L3) | LEVEL(L3) | P(REGION, L_SHARE),	/* 0x02: local CA shared cache */
318 	OP_LH | P(LVL, L3) | LEVEL(L3) | P(REGION, L_NON_SHARE),/* 0x03: local CA non-shared cache */
319 	OP_LH | P(LVL, L3) | LEVEL(L3) | P(REGION, O_IO),	/* 0x04: other CA IO agent */
320 	OP_LH | P(LVL, L3) | LEVEL(L3) | P(REGION, O_SHARE),	/* 0x05: other CA shared cache */
321 	OP_LH | P(LVL, L3) | LEVEL(L3) | P(REGION, O_NON_SHARE),/* 0x06: other CA non-shared cache */
322 	OP_LH | LEVEL(RAM) | P(REGION, MMIO),			/* 0x07: MMIO */
323 	OP_LH | LEVEL(RAM) | P(REGION, MEM0),			/* 0x08: Memory region 0 */
324 	OP_LH | LEVEL(RAM) | P(REGION, MEM1),			/* 0x09: Memory region 1 */
325 	OP_LH | LEVEL(RAM) | P(REGION, MEM2),			/* 0x0a: Memory region 2 */
326 	OP_LH | LEVEL(RAM) | P(REGION, MEM3),			/* 0x0b: Memory region 3 */
327 	OP_LH | LEVEL(RAM) | P(REGION, MEM4),			/* 0x0c: Memory region 4 */
328 	OP_LH | LEVEL(RAM) | P(REGION, MEM5),			/* 0x0d: Memory region 5 */
329 	OP_LH | LEVEL(RAM) | P(REGION, MEM6),			/* 0x0e: Memory region 6 */
330 	OP_LH | LEVEL(RAM) | P(REGION, MEM7),			/* 0x0f: Memory region 7 */
331 };
332 
parse_omr_data_source(u8 dse)333 static u64 parse_omr_data_source(u8 dse)
334 {
335 	union omr_encoding omr;
336 	u64 val = 0;
337 
338 	omr.omr_full = dse;
339 	val = omr_data_source[omr.omr_source];
340 	if (omr.omr_source > 0x1 && omr.omr_source < 0x7)
341 		val |= omr.omr_remote ? P(LVL, REM_CCE1) : 0;
342 	else if (omr.omr_source > 0x7)
343 		val |= omr.omr_remote ? P(LVL, REM_RAM1) : P(LVL, LOC_RAM);
344 
345 	if (omr.omr_remote)
346 		val |= REM;
347 
348 	if (omr.omr_source == 0x2) {
349 		u8 snoop = omr.omr_snoop | (omr.omr_promoted << 1);
350 
351 		if (omr.omr_hitm)
352 			val |= P(SNOOP, HITM);
353 		else if (snoop == 0x0)
354 			val |= P(SNOOP, NA);
355 		else if (snoop == 0x1)
356 			val |= P(SNOOP, MISS);
357 		else if (snoop == 0x2)
358 			val |= P(SNOOP, HIT);
359 		else if (snoop == 0x3)
360 			val |= P(SNOOP, NONE);
361 	} else if (omr.omr_source > 0x2 && omr.omr_source < 0x7) {
362 		val |= omr.omr_hitm ? P(SNOOP, HITM) : P(SNOOP, HIT);
363 		val |= omr.omr_snoop ? P(SNOOPX, FWD) : 0;
364 	} else {
365 		val |= P(SNOOP, NONE);
366 	}
367 
368 	return val;
369 }
370 
precise_store_data(u64 status)371 static u64 precise_store_data(u64 status)
372 {
373 	union intel_x86_pebs_dse dse;
374 	u64 val = P(OP, STORE) | P(SNOOP, NA) | P(LVL, L1) | P(TLB, L2);
375 
376 	dse.val = status;
377 
378 	/*
379 	 * bit 4: TLB access
380 	 * 1 = stored missed 2nd level TLB
381 	 *
382 	 * so it either hit the walker or the OS
383 	 * otherwise hit 2nd level TLB
384 	 */
385 	if (dse.st_stlb_miss)
386 		val |= P(TLB, MISS);
387 	else
388 		val |= P(TLB, HIT);
389 
390 	/*
391 	 * bit 0: hit L1 data cache
392 	 * if not set, then all we know is that
393 	 * it missed L1D
394 	 */
395 	if (dse.st_l1d_hit)
396 		val |= P(LVL, HIT);
397 	else
398 		val |= P(LVL, MISS);
399 
400 	/*
401 	 * bit 5: Locked prefix
402 	 */
403 	if (dse.st_locked)
404 		val |= P(LOCK, LOCKED);
405 
406 	return val;
407 }
408 
precise_datala_hsw(struct perf_event * event,u64 status)409 static u64 precise_datala_hsw(struct perf_event *event, u64 status)
410 {
411 	union perf_mem_data_src dse;
412 
413 	dse.val = PERF_MEM_NA;
414 
415 	if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW)
416 		dse.mem_op = PERF_MEM_OP_STORE;
417 	else if (event->hw.flags & PERF_X86_EVENT_PEBS_LD_HSW)
418 		dse.mem_op = PERF_MEM_OP_LOAD;
419 
420 	/*
421 	 * L1 info only valid for following events:
422 	 *
423 	 * MEM_UOPS_RETIRED.STLB_MISS_STORES
424 	 * MEM_UOPS_RETIRED.LOCK_STORES
425 	 * MEM_UOPS_RETIRED.SPLIT_STORES
426 	 * MEM_UOPS_RETIRED.ALL_STORES
427 	 */
428 	if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) {
429 		if (status & 1)
430 			dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_HIT;
431 		else
432 			dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_MISS;
433 	}
434 	return dse.val;
435 }
436 
pebs_set_tlb_lock(u64 * val,bool tlb,bool lock)437 static inline void pebs_set_tlb_lock(u64 *val, bool tlb, bool lock)
438 {
439 	/*
440 	 * TLB access
441 	 * 0 = did not miss 2nd level TLB
442 	 * 1 = missed 2nd level TLB
443 	 */
444 	if (tlb)
445 		*val |= P(TLB, MISS) | P(TLB, L2);
446 	else
447 		*val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2);
448 
449 	/* locked prefix */
450 	if (lock)
451 		*val |= P(LOCK, LOCKED);
452 }
453 
454 /* 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)455 static u64 __grt_latency_data(struct perf_event *event, u64 status,
456 			       u8 dse, bool tlb, bool lock, bool blk)
457 {
458 	u64 val;
459 
460 	WARN_ON_ONCE(is_hybrid() &&
461 		     hybrid_pmu(event->pmu)->pmu_type == hybrid_big);
462 
463 	dse &= PERF_PEBS_DATA_SOURCE_GRT_MASK;
464 	val = hybrid_var(event->pmu, pebs_data_source)[dse];
465 
466 	pebs_set_tlb_lock(&val, tlb, lock);
467 
468 	if (blk)
469 		val |= P(BLK, DATA);
470 	else
471 		val |= P(BLK, NA);
472 
473 	return val;
474 }
475 
grt_latency_data(struct perf_event * event,u64 status)476 u64 grt_latency_data(struct perf_event *event, u64 status)
477 {
478 	union intel_x86_pebs_dse dse;
479 
480 	dse.val = status;
481 
482 	return __grt_latency_data(event, status, dse.ld_dse,
483 				  dse.ld_locked, dse.ld_stlb_miss,
484 				  dse.ld_data_blk);
485 }
486 
487 /* Retrieve the latency data for e-core of MTL */
cmt_latency_data(struct perf_event * event,u64 status)488 u64 cmt_latency_data(struct perf_event *event, u64 status)
489 {
490 	union intel_x86_pebs_dse dse;
491 
492 	dse.val = status;
493 
494 	return __grt_latency_data(event, status, dse.mtl_dse,
495 				  dse.mtl_stlb_miss, dse.mtl_locked,
496 				  dse.mtl_fwd_blk);
497 }
498 
arw_latency_data(struct perf_event * event,u64 status)499 static u64 arw_latency_data(struct perf_event *event, u64 status)
500 {
501 	union intel_x86_pebs_dse dse;
502 	union perf_mem_data_src src;
503 	u64 val;
504 
505 	dse.val = status;
506 
507 	if (!dse.arw_l2_miss)
508 		val = arw_pebs_l2_hit_data_source[dse.arw_dse & 0xf];
509 	else
510 		val = parse_omr_data_source(dse.arw_dse);
511 
512 	if (!val)
513 		val = P(OP, LOAD) | LEVEL(NA) | P(SNOOP, NA);
514 
515 	if (dse.arw_stlb_miss)
516 		val |= P(TLB, MISS) | P(TLB, L2);
517 	else
518 		val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2);
519 
520 	if (dse.arw_locked)
521 		val |= P(LOCK, LOCKED);
522 
523 	if (dse.arw_data_blk)
524 		val |= P(BLK, DATA);
525 	if (dse.arw_addr_blk)
526 		val |= P(BLK, ADDR);
527 	if (!dse.arw_data_blk && !dse.arw_addr_blk)
528 		val |= P(BLK, NA);
529 
530 	src.val = val;
531 	if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW)
532 		src.mem_op = P(OP, STORE);
533 
534 	return src.val;
535 }
536 
lnc_latency_data(struct perf_event * event,u64 status)537 static u64 lnc_latency_data(struct perf_event *event, u64 status)
538 {
539 	union intel_x86_pebs_dse dse;
540 	union perf_mem_data_src src;
541 	u64 val;
542 
543 	dse.val = status;
544 
545 	/* LNC core latency data */
546 	val = hybrid_var(event->pmu, pebs_data_source)[status & PERF_PEBS_DATA_SOURCE_MASK];
547 	if (!val)
548 		val = P(OP, LOAD) | LEVEL(NA) | P(SNOOP, NA);
549 
550 	if (dse.lnc_stlb_miss)
551 		val |= P(TLB, MISS) | P(TLB, L2);
552 	else
553 		val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2);
554 
555 	if (dse.lnc_locked)
556 		val |= P(LOCK, LOCKED);
557 
558 	if (dse.lnc_data_blk)
559 		val |= P(BLK, DATA);
560 	if (dse.lnc_addr_blk)
561 		val |= P(BLK, ADDR);
562 	if (!dse.lnc_data_blk && !dse.lnc_addr_blk)
563 		val |= P(BLK, NA);
564 
565 	src.val = val;
566 	if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW)
567 		src.mem_op = P(OP, STORE);
568 
569 	return src.val;
570 }
571 
lnl_latency_data(struct perf_event * event,u64 status)572 u64 lnl_latency_data(struct perf_event *event, u64 status)
573 {
574 	struct x86_hybrid_pmu *pmu = hybrid_pmu(event->pmu);
575 
576 	if (pmu->pmu_type == hybrid_small)
577 		return cmt_latency_data(event, status);
578 
579 	return lnc_latency_data(event, status);
580 }
581 
arl_h_latency_data(struct perf_event * event,u64 status)582 u64 arl_h_latency_data(struct perf_event *event, u64 status)
583 {
584 	struct x86_hybrid_pmu *pmu = hybrid_pmu(event->pmu);
585 
586 	if (pmu->pmu_type == hybrid_tiny)
587 		return cmt_latency_data(event, status);
588 
589 	return lnl_latency_data(event, status);
590 }
591 
pnc_latency_data(struct perf_event * event,u64 status)592 u64 pnc_latency_data(struct perf_event *event, u64 status)
593 {
594 	union intel_x86_pebs_dse dse;
595 	union perf_mem_data_src src;
596 	u64 val;
597 
598 	dse.val = status;
599 
600 	if (!dse.pnc_l2_miss)
601 		val = pnc_pebs_l2_hit_data_source[dse.pnc_dse & 0xf];
602 	else
603 		val = parse_omr_data_source(dse.pnc_dse);
604 
605 	if (!val)
606 		val = P(OP, LOAD) | LEVEL(NA) | P(SNOOP, NA);
607 
608 	if (dse.pnc_stlb_miss)
609 		val |= P(TLB, MISS) | P(TLB, L2);
610 	else
611 		val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2);
612 
613 	if (dse.pnc_locked)
614 		val |= P(LOCK, LOCKED);
615 
616 	if (dse.pnc_data_blk)
617 		val |= P(BLK, DATA);
618 	if (dse.pnc_addr_blk)
619 		val |= P(BLK, ADDR);
620 	if (!dse.pnc_data_blk && !dse.pnc_addr_blk)
621 		val |= P(BLK, NA);
622 
623 	src.val = val;
624 	if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW)
625 		src.mem_op = P(OP, STORE);
626 
627 	return src.val;
628 }
629 
nvl_latency_data(struct perf_event * event,u64 status)630 u64 nvl_latency_data(struct perf_event *event, u64 status)
631 {
632 	struct x86_hybrid_pmu *pmu = hybrid_pmu(event->pmu);
633 
634 	if (pmu->pmu_type == hybrid_small)
635 		return arw_latency_data(event, status);
636 
637 	return pnc_latency_data(event, status);
638 }
639 
load_latency_data(struct perf_event * event,u64 status)640 static u64 load_latency_data(struct perf_event *event, u64 status)
641 {
642 	union intel_x86_pebs_dse dse;
643 	u64 val;
644 
645 	dse.val = status;
646 
647 	/*
648 	 * use the mapping table for bit 0-3
649 	 */
650 	val = hybrid_var(event->pmu, pebs_data_source)[dse.ld_dse];
651 
652 	/*
653 	 * Nehalem models do not support TLB, Lock infos
654 	 */
655 	if (x86_pmu.pebs_no_tlb) {
656 		val |= P(TLB, NA) | P(LOCK, NA);
657 		return val;
658 	}
659 
660 	pebs_set_tlb_lock(&val, dse.ld_stlb_miss, dse.ld_locked);
661 
662 	/*
663 	 * Ice Lake and earlier models do not support block infos.
664 	 */
665 	if (!x86_pmu.pebs_block) {
666 		val |= P(BLK, NA);
667 		return val;
668 	}
669 	/*
670 	 * bit 6: load was blocked since its data could not be forwarded
671 	 *        from a preceding store
672 	 */
673 	if (dse.ld_data_blk)
674 		val |= P(BLK, DATA);
675 
676 	/*
677 	 * bit 7: load was blocked due to potential address conflict with
678 	 *        a preceding store
679 	 */
680 	if (dse.ld_addr_blk)
681 		val |= P(BLK, ADDR);
682 
683 	if (!dse.ld_data_blk && !dse.ld_addr_blk)
684 		val |= P(BLK, NA);
685 
686 	return val;
687 }
688 
store_latency_data(struct perf_event * event,u64 status)689 static u64 store_latency_data(struct perf_event *event, u64 status)
690 {
691 	union intel_x86_pebs_dse dse;
692 	union perf_mem_data_src src;
693 	u64 val;
694 
695 	dse.val = status;
696 
697 	/*
698 	 * use the mapping table for bit 0-3
699 	 */
700 	val = hybrid_var(event->pmu, pebs_data_source)[dse.st_lat_dse];
701 
702 	pebs_set_tlb_lock(&val, dse.st_lat_stlb_miss, dse.st_lat_locked);
703 
704 	val |= P(BLK, NA);
705 
706 	/*
707 	 * the pebs_data_source table is only for loads
708 	 * so override the mem_op to say STORE instead
709 	 */
710 	src.val = val;
711 	src.mem_op = P(OP,STORE);
712 
713 	return src.val;
714 }
715 
716 struct pebs_record_core {
717 	u64 flags, ip;
718 	u64 ax, bx, cx, dx;
719 	u64 si, di, bp, sp;
720 	u64 r8,  r9,  r10, r11;
721 	u64 r12, r13, r14, r15;
722 };
723 
724 struct pebs_record_nhm {
725 	u64 flags, ip;
726 	u64 ax, bx, cx, dx;
727 	u64 si, di, bp, sp;
728 	u64 r8,  r9,  r10, r11;
729 	u64 r12, r13, r14, r15;
730 	u64 status, dla, dse, lat;
731 };
732 
733 /*
734  * Same as pebs_record_nhm, with two additional fields.
735  */
736 struct pebs_record_hsw {
737 	u64 flags, ip;
738 	u64 ax, bx, cx, dx;
739 	u64 si, di, bp, sp;
740 	u64 r8,  r9,  r10, r11;
741 	u64 r12, r13, r14, r15;
742 	u64 status, dla, dse, lat;
743 	u64 real_ip, tsx_tuning;
744 };
745 
746 union hsw_tsx_tuning {
747 	struct {
748 		u32 cycles_last_block     : 32,
749 		    hle_abort		  : 1,
750 		    rtm_abort		  : 1,
751 		    instruction_abort     : 1,
752 		    non_instruction_abort : 1,
753 		    retry		  : 1,
754 		    data_conflict	  : 1,
755 		    capacity_writes	  : 1,
756 		    capacity_reads	  : 1;
757 	};
758 	u64	    value;
759 };
760 
761 #define PEBS_HSW_TSX_FLAGS	0xff00000000ULL
762 
763 /* Same as HSW, plus TSC */
764 
765 struct pebs_record_skl {
766 	u64 flags, ip;
767 	u64 ax, bx, cx, dx;
768 	u64 si, di, bp, sp;
769 	u64 r8,  r9,  r10, r11;
770 	u64 r12, r13, r14, r15;
771 	u64 status, dla, dse, lat;
772 	u64 real_ip, tsx_tuning;
773 	u64 tsc;
774 };
775 
init_debug_store_on_cpu(int cpu)776 void init_debug_store_on_cpu(int cpu)
777 {
778 	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
779 
780 	if (!ds)
781 		return;
782 
783 	wrmsrq_on_cpu(cpu, MSR_IA32_DS_AREA, (u64)(unsigned long)ds);
784 }
785 
fini_debug_store_on_cpu(int cpu)786 void fini_debug_store_on_cpu(int cpu)
787 {
788 	if (!per_cpu(cpu_hw_events, cpu).ds)
789 		return;
790 
791 	wrmsrq_on_cpu(cpu, MSR_IA32_DS_AREA, 0);
792 }
793 
794 static DEFINE_PER_CPU(void *, insn_buffer);
795 
ds_update_cea(void * cea,void * addr,size_t size,pgprot_t prot)796 static void ds_update_cea(void *cea, void *addr, size_t size, pgprot_t prot)
797 {
798 	unsigned long start = (unsigned long)cea;
799 	phys_addr_t pa;
800 	size_t msz = 0;
801 
802 	pa = virt_to_phys(addr);
803 
804 	preempt_disable();
805 	for (; msz < size; msz += PAGE_SIZE, pa += PAGE_SIZE, cea += PAGE_SIZE)
806 		cea_set_pte(cea, pa, prot);
807 
808 	/*
809 	 * This is a cross-CPU update of the cpu_entry_area, we must shoot down
810 	 * all TLB entries for it.
811 	 */
812 	flush_tlb_kernel_range(start, start + size);
813 	preempt_enable();
814 }
815 
ds_clear_cea(void * cea,size_t size)816 static void ds_clear_cea(void *cea, size_t size)
817 {
818 	unsigned long start = (unsigned long)cea;
819 	size_t msz = 0;
820 
821 	preempt_disable();
822 	for (; msz < size; msz += PAGE_SIZE, cea += PAGE_SIZE)
823 		cea_set_pte(cea, 0, PAGE_NONE);
824 
825 	flush_tlb_kernel_range(start, start + size);
826 	preempt_enable();
827 }
828 
dsalloc_pages(size_t size,gfp_t flags,int cpu)829 static void *dsalloc_pages(size_t size, gfp_t flags, int cpu)
830 {
831 	unsigned int order = get_order(size);
832 	int node = cpu_to_node(cpu);
833 	struct page *page;
834 
835 	page = alloc_pages_node(node, flags | __GFP_ZERO, order);
836 	return page ? page_address(page) : NULL;
837 }
838 
dsfree_pages(const void * buffer,size_t size)839 static void dsfree_pages(const void *buffer, size_t size)
840 {
841 	if (buffer)
842 		free_pages((unsigned long)buffer, get_order(size));
843 }
844 
alloc_pebs_buffer(int cpu)845 static int alloc_pebs_buffer(int cpu)
846 {
847 	struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
848 	struct debug_store *ds = hwev->ds;
849 	size_t bsiz = x86_pmu.pebs_buffer_size;
850 	int max, node = cpu_to_node(cpu);
851 	void *buffer, *insn_buff, *cea;
852 
853 	if (!intel_pmu_has_pebs())
854 		return 0;
855 
856 	buffer = dsalloc_pages(bsiz, GFP_KERNEL, cpu);
857 	if (unlikely(!buffer))
858 		return -ENOMEM;
859 
860 	if (x86_pmu.arch_pebs) {
861 		hwev->pebs_vaddr = buffer;
862 		return 0;
863 	}
864 
865 	/*
866 	 * HSW+ already provides us the eventing ip; no need to allocate this
867 	 * buffer then.
868 	 */
869 	if (x86_pmu.intel_cap.pebs_format < 2) {
870 		insn_buff = kzalloc_node(PEBS_FIXUP_SIZE, GFP_KERNEL, node);
871 		if (!insn_buff) {
872 			dsfree_pages(buffer, bsiz);
873 			return -ENOMEM;
874 		}
875 		per_cpu(insn_buffer, cpu) = insn_buff;
876 	}
877 	hwev->pebs_vaddr = buffer;
878 	/* Update the cpu entry area mapping */
879 	cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer;
880 	ds->pebs_buffer_base = (unsigned long) cea;
881 	ds_update_cea(cea, buffer, bsiz, PAGE_KERNEL);
882 	ds->pebs_index = ds->pebs_buffer_base;
883 	max = x86_pmu.pebs_record_size * (bsiz / x86_pmu.pebs_record_size);
884 	ds->pebs_absolute_maximum = ds->pebs_buffer_base + max;
885 	return 0;
886 }
887 
release_pebs_buffer(int cpu)888 static void release_pebs_buffer(int cpu)
889 {
890 	struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
891 	void *cea;
892 
893 	if (!intel_pmu_has_pebs())
894 		return;
895 
896 	if (x86_pmu.ds_pebs) {
897 		kfree(per_cpu(insn_buffer, cpu));
898 		per_cpu(insn_buffer, cpu) = NULL;
899 
900 		/* Clear the fixmap */
901 		cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer;
902 		ds_clear_cea(cea, x86_pmu.pebs_buffer_size);
903 	}
904 
905 	dsfree_pages(hwev->pebs_vaddr, x86_pmu.pebs_buffer_size);
906 	hwev->pebs_vaddr = NULL;
907 }
908 
alloc_bts_buffer(int cpu)909 static int alloc_bts_buffer(int cpu)
910 {
911 	struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
912 	struct debug_store *ds = hwev->ds;
913 	void *buffer, *cea;
914 	int max;
915 
916 	if (!x86_pmu.bts)
917 		return 0;
918 
919 	buffer = dsalloc_pages(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, cpu);
920 	if (unlikely(!buffer)) {
921 		WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__);
922 		return -ENOMEM;
923 	}
924 	hwev->ds_bts_vaddr = buffer;
925 	/* Update the fixmap */
926 	cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer;
927 	ds->bts_buffer_base = (unsigned long) cea;
928 	ds_update_cea(cea, buffer, BTS_BUFFER_SIZE, PAGE_KERNEL);
929 	ds->bts_index = ds->bts_buffer_base;
930 	max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE;
931 	ds->bts_absolute_maximum = ds->bts_buffer_base +
932 					max * BTS_RECORD_SIZE;
933 	ds->bts_interrupt_threshold = ds->bts_absolute_maximum -
934 					(max / 16) * BTS_RECORD_SIZE;
935 	return 0;
936 }
937 
release_bts_buffer(int cpu)938 static void release_bts_buffer(int cpu)
939 {
940 	struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
941 	void *cea;
942 
943 	if (!x86_pmu.bts)
944 		return;
945 
946 	/* Clear the fixmap */
947 	cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer;
948 	ds_clear_cea(cea, BTS_BUFFER_SIZE);
949 	dsfree_pages(hwev->ds_bts_vaddr, BTS_BUFFER_SIZE);
950 	hwev->ds_bts_vaddr = NULL;
951 }
952 
alloc_ds_buffer(int cpu)953 static int alloc_ds_buffer(int cpu)
954 {
955 	struct debug_store *ds = &get_cpu_entry_area(cpu)->cpu_debug_store;
956 
957 	memset(ds, 0, sizeof(*ds));
958 	per_cpu(cpu_hw_events, cpu).ds = ds;
959 	return 0;
960 }
961 
release_ds_buffer(int cpu)962 static void release_ds_buffer(int cpu)
963 {
964 	per_cpu(cpu_hw_events, cpu).ds = NULL;
965 }
966 
release_ds_buffers(void)967 void release_ds_buffers(void)
968 {
969 	int cpu;
970 
971 	if (!x86_pmu.bts && !x86_pmu.ds_pebs)
972 		return;
973 
974 	for_each_possible_cpu(cpu)
975 		release_ds_buffer(cpu);
976 
977 	for_each_possible_cpu(cpu) {
978 		/*
979 		 * Again, ignore errors from offline CPUs, they will no longer
980 		 * observe cpu_hw_events.ds and not program the DS_AREA when
981 		 * they come up.
982 		 */
983 		fini_debug_store_on_cpu(cpu);
984 	}
985 
986 	for_each_possible_cpu(cpu) {
987 		if (x86_pmu.ds_pebs)
988 			release_pebs_buffer(cpu);
989 		release_bts_buffer(cpu);
990 	}
991 }
992 
reserve_ds_buffers(void)993 void reserve_ds_buffers(void)
994 {
995 	int bts_err = 0, pebs_err = 0;
996 	int cpu;
997 
998 	x86_pmu.bts_active = 0;
999 
1000 	if (x86_pmu.ds_pebs)
1001 		x86_pmu.pebs_active = 0;
1002 
1003 	if (!x86_pmu.bts && !x86_pmu.ds_pebs)
1004 		return;
1005 
1006 	if (!x86_pmu.bts)
1007 		bts_err = 1;
1008 
1009 	if (!x86_pmu.ds_pebs)
1010 		pebs_err = 1;
1011 
1012 	for_each_possible_cpu(cpu) {
1013 		if (alloc_ds_buffer(cpu)) {
1014 			bts_err = 1;
1015 			pebs_err = 1;
1016 		}
1017 
1018 		if (!bts_err && alloc_bts_buffer(cpu))
1019 			bts_err = 1;
1020 
1021 		if (x86_pmu.ds_pebs && !pebs_err &&
1022 		    alloc_pebs_buffer(cpu))
1023 			pebs_err = 1;
1024 
1025 		if (bts_err && pebs_err)
1026 			break;
1027 	}
1028 
1029 	if (bts_err) {
1030 		for_each_possible_cpu(cpu)
1031 			release_bts_buffer(cpu);
1032 	}
1033 
1034 	if (x86_pmu.ds_pebs && pebs_err) {
1035 		for_each_possible_cpu(cpu)
1036 			release_pebs_buffer(cpu);
1037 	}
1038 
1039 	if (bts_err && pebs_err) {
1040 		for_each_possible_cpu(cpu)
1041 			release_ds_buffer(cpu);
1042 	} else {
1043 		if (x86_pmu.bts && !bts_err)
1044 			x86_pmu.bts_active = 1;
1045 
1046 		if (x86_pmu.ds_pebs && !pebs_err)
1047 			x86_pmu.pebs_active = 1;
1048 
1049 		for_each_possible_cpu(cpu) {
1050 			/*
1051 			 * Ignores wrmsr_on_cpu() errors for offline CPUs they
1052 			 * will get this call through intel_pmu_cpu_starting().
1053 			 */
1054 			init_debug_store_on_cpu(cpu);
1055 		}
1056 	}
1057 }
1058 
alloc_arch_pebs_buf_on_cpu(int cpu)1059 inline int alloc_arch_pebs_buf_on_cpu(int cpu)
1060 {
1061 	if (!x86_pmu.arch_pebs)
1062 		return 0;
1063 
1064 	return alloc_pebs_buffer(cpu);
1065 }
1066 
release_arch_pebs_buf_on_cpu(int cpu)1067 inline void release_arch_pebs_buf_on_cpu(int cpu)
1068 {
1069 	if (!x86_pmu.arch_pebs)
1070 		return;
1071 
1072 	release_pebs_buffer(cpu);
1073 }
1074 
init_arch_pebs_on_cpu(int cpu)1075 void init_arch_pebs_on_cpu(int cpu)
1076 {
1077 	struct cpu_hw_events *cpuc = per_cpu_ptr(&cpu_hw_events, cpu);
1078 	u64 arch_pebs_base;
1079 
1080 	if (!x86_pmu.arch_pebs)
1081 		return;
1082 
1083 	if (!cpuc->pebs_vaddr) {
1084 		WARN(1, "Fail to allocate PEBS buffer on CPU %d\n", cpu);
1085 		x86_pmu.pebs_active = 0;
1086 		return;
1087 	}
1088 
1089 	/*
1090 	 * 4KB-aligned pointer of the output buffer
1091 	 * (alloc_pages_node() returns page aligned address)
1092 	 * Buffer Size = 4KB * 2^SIZE
1093 	 * contiguous physical buffer (alloc_pages_node() with order)
1094 	 */
1095 	arch_pebs_base = virt_to_phys(cpuc->pebs_vaddr) | PEBS_BUFFER_SHIFT;
1096 	wrmsrq_on_cpu(cpu, MSR_IA32_PEBS_BASE, arch_pebs_base);
1097 	x86_pmu.pebs_active = 1;
1098 }
1099 
fini_arch_pebs_on_cpu(int cpu)1100 inline void fini_arch_pebs_on_cpu(int cpu)
1101 {
1102 	if (!x86_pmu.arch_pebs)
1103 		return;
1104 
1105 	wrmsrq_on_cpu(cpu, MSR_IA32_PEBS_BASE, 0);
1106 }
1107 
1108 /*
1109  * BTS
1110  */
1111 
1112 struct event_constraint bts_constraint =
1113 	EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0);
1114 
intel_pmu_enable_bts(u64 config)1115 void intel_pmu_enable_bts(u64 config)
1116 {
1117 	unsigned long debugctlmsr;
1118 
1119 	debugctlmsr = get_debugctlmsr();
1120 
1121 	debugctlmsr |= DEBUGCTLMSR_TR;
1122 	debugctlmsr |= DEBUGCTLMSR_BTS;
1123 	if (config & ARCH_PERFMON_EVENTSEL_INT)
1124 		debugctlmsr |= DEBUGCTLMSR_BTINT;
1125 
1126 	if (!(config & ARCH_PERFMON_EVENTSEL_OS))
1127 		debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
1128 
1129 	if (!(config & ARCH_PERFMON_EVENTSEL_USR))
1130 		debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR;
1131 
1132 	update_debugctlmsr(debugctlmsr);
1133 }
1134 
intel_pmu_disable_bts(void)1135 void intel_pmu_disable_bts(void)
1136 {
1137 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1138 	unsigned long debugctlmsr;
1139 
1140 	if (!cpuc->ds)
1141 		return;
1142 
1143 	debugctlmsr = get_debugctlmsr();
1144 
1145 	debugctlmsr &=
1146 		~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT |
1147 		  DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR);
1148 
1149 	update_debugctlmsr(debugctlmsr);
1150 }
1151 
intel_pmu_drain_bts_buffer(void)1152 int intel_pmu_drain_bts_buffer(void)
1153 {
1154 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1155 	struct debug_store *ds = cpuc->ds;
1156 	struct bts_record {
1157 		u64	from;
1158 		u64	to;
1159 		u64	flags;
1160 	};
1161 	struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
1162 	struct bts_record *at, *base, *top;
1163 	struct perf_output_handle handle;
1164 	struct perf_event_header header;
1165 	struct perf_sample_data data;
1166 	unsigned long skip = 0;
1167 	struct pt_regs regs;
1168 
1169 	if (!event)
1170 		return 0;
1171 
1172 	if (!x86_pmu.bts_active)
1173 		return 0;
1174 
1175 	base = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
1176 	top  = (struct bts_record *)(unsigned long)ds->bts_index;
1177 
1178 	if (top <= base)
1179 		return 0;
1180 
1181 	memset(&regs, 0, sizeof(regs));
1182 
1183 	ds->bts_index = ds->bts_buffer_base;
1184 
1185 	perf_sample_data_init(&data, 0, event->hw.last_period);
1186 
1187 	/*
1188 	 * BTS leaks kernel addresses in branches across the cpl boundary,
1189 	 * such as traps or system calls, so unless the user is asking for
1190 	 * kernel tracing (and right now it's not possible), we'd need to
1191 	 * filter them out. But first we need to count how many of those we
1192 	 * have in the current batch. This is an extra O(n) pass, however,
1193 	 * it's much faster than the other one especially considering that
1194 	 * n <= 2560 (BTS_BUFFER_SIZE / BTS_RECORD_SIZE * 15/16; see the
1195 	 * alloc_bts_buffer()).
1196 	 */
1197 	for (at = base; at < top; at++) {
1198 		/*
1199 		 * Note that right now *this* BTS code only works if
1200 		 * attr::exclude_kernel is set, but let's keep this extra
1201 		 * check here in case that changes.
1202 		 */
1203 		if (event->attr.exclude_kernel &&
1204 		    (kernel_ip(at->from) || kernel_ip(at->to)))
1205 			skip++;
1206 	}
1207 
1208 	/*
1209 	 * Prepare a generic sample, i.e. fill in the invariant fields.
1210 	 * We will overwrite the from and to address before we output
1211 	 * the sample.
1212 	 */
1213 	rcu_read_lock();
1214 	perf_prepare_sample(&data, event, &regs);
1215 	perf_prepare_header(&header, &data, event, &regs);
1216 
1217 	if (perf_output_begin(&handle, &data, event,
1218 			      header.size * (top - base - skip)))
1219 		goto unlock;
1220 
1221 	for (at = base; at < top; at++) {
1222 		/* Filter out any records that contain kernel addresses. */
1223 		if (event->attr.exclude_kernel &&
1224 		    (kernel_ip(at->from) || kernel_ip(at->to)))
1225 			continue;
1226 
1227 		data.ip		= at->from;
1228 		data.addr	= at->to;
1229 
1230 		perf_output_sample(&handle, &header, &data, event);
1231 	}
1232 
1233 	perf_output_end(&handle);
1234 
1235 	/* There's new data available. */
1236 	event->hw.interrupts++;
1237 	event->pending_kill = POLL_IN;
1238 unlock:
1239 	rcu_read_unlock();
1240 	return 1;
1241 }
1242 
intel_pmu_drain_pebs_buffer(void)1243 void intel_pmu_drain_pebs_buffer(void)
1244 {
1245 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1246 	struct perf_sample_data data;
1247 
1248 	WARN_ON_ONCE(cpuc->enabled);
1249 
1250 	static_call(x86_pmu_drain_pebs)(NULL, &data);
1251 }
1252 
1253 /*
1254  * PEBS
1255  */
1256 struct event_constraint intel_core2_pebs_event_constraints[] = {
1257 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
1258 	INTEL_FLAGS_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
1259 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
1260 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
1261 	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1),    /* MEM_LOAD_RETIRED.* */
1262 	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
1263 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x01),
1264 	EVENT_CONSTRAINT_END
1265 };
1266 
1267 struct event_constraint intel_atom_pebs_event_constraints[] = {
1268 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
1269 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
1270 	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1),    /* MEM_LOAD_RETIRED.* */
1271 	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
1272 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x01),
1273 	/* Allow all events as PEBS with no flags */
1274 	INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
1275 	EVENT_CONSTRAINT_END
1276 };
1277 
1278 struct event_constraint intel_slm_pebs_event_constraints[] = {
1279 	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
1280 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x1),
1281 	/* Allow all events as PEBS with no flags */
1282 	INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
1283 	EVENT_CONSTRAINT_END
1284 };
1285 
1286 struct event_constraint intel_glm_pebs_event_constraints[] = {
1287 	/* Allow all events as PEBS with no flags */
1288 	INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
1289 	EVENT_CONSTRAINT_END
1290 };
1291 
1292 struct event_constraint intel_grt_pebs_event_constraints[] = {
1293 	/* Allow all events as PEBS with no flags */
1294 	INTEL_HYBRID_LAT_CONSTRAINT(0x5d0, 0x3),
1295 	INTEL_HYBRID_LAT_CONSTRAINT(0x6d0, 0x3f),
1296 	EVENT_CONSTRAINT_END
1297 };
1298 
1299 struct event_constraint intel_cmt_pebs_event_constraints[] = {
1300 	/* Allow all events as PEBS with no flags */
1301 	INTEL_HYBRID_LAT_CONSTRAINT(0x5d0, 0x3),
1302 	INTEL_HYBRID_LAT_CONSTRAINT(0x6d0, 0xff),
1303 	EVENT_CONSTRAINT_END
1304 };
1305 
1306 struct event_constraint intel_dkt_pebs_event_constraints[] = {
1307 	/* Allow all events as PEBS with no flags */
1308 	INTEL_HYBRID_LAT_CONSTRAINT(0x5d0, 0xff),
1309 	INTEL_HYBRID_LAT_CONSTRAINT(0x6d0, 0xff),
1310 	EVENT_CONSTRAINT_END
1311 };
1312 
1313 struct event_constraint intel_nehalem_pebs_event_constraints[] = {
1314 	INTEL_PLD_CONSTRAINT(0x100b, 0xf),      /* MEM_INST_RETIRED.* */
1315 	INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf),    /* MEM_UNCORE_RETIRED.* */
1316 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
1317 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf),    /* INST_RETIRED.ANY */
1318 	INTEL_EVENT_CONSTRAINT(0xc2, 0xf),    /* UOPS_RETIRED.* */
1319 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
1320 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
1321 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf),    /* SSEX_UOPS_RETIRED.* */
1322 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
1323 	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf),    /* MEM_LOAD_RETIRED.* */
1324 	INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf),    /* FP_ASSIST.* */
1325 	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
1326 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f),
1327 	EVENT_CONSTRAINT_END
1328 };
1329 
1330 struct event_constraint intel_westmere_pebs_event_constraints[] = {
1331 	INTEL_PLD_CONSTRAINT(0x100b, 0xf),      /* MEM_INST_RETIRED.* */
1332 	INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf),    /* MEM_UNCORE_RETIRED.* */
1333 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
1334 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf),    /* INSTR_RETIRED.* */
1335 	INTEL_EVENT_CONSTRAINT(0xc2, 0xf),    /* UOPS_RETIRED.* */
1336 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
1337 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc5, 0xf),    /* BR_MISP_RETIRED.* */
1338 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf),    /* SSEX_UOPS_RETIRED.* */
1339 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
1340 	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf),    /* MEM_LOAD_RETIRED.* */
1341 	INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf),    /* FP_ASSIST.* */
1342 	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
1343 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f),
1344 	EVENT_CONSTRAINT_END
1345 };
1346 
1347 struct event_constraint intel_snb_pebs_event_constraints[] = {
1348 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
1349 	INTEL_PLD_CONSTRAINT(0x01cd, 0x8),    /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
1350 	INTEL_PST_CONSTRAINT(0x02cd, 0x8),    /* MEM_TRANS_RETIRED.PRECISE_STORES */
1351 	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
1352 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf),
1353         INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf),    /* MEM_UOP_RETIRED.* */
1354         INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
1355         INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf),    /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
1356         INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf),    /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
1357 	/* Allow all events as PEBS with no flags */
1358 	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
1359 	EVENT_CONSTRAINT_END
1360 };
1361 
1362 struct event_constraint intel_ivb_pebs_event_constraints[] = {
1363         INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
1364         INTEL_PLD_CONSTRAINT(0x01cd, 0x8),    /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
1365 	INTEL_PST_CONSTRAINT(0x02cd, 0x8),    /* MEM_TRANS_RETIRED.PRECISE_STORES */
1366 	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
1367 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf),
1368 	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
1369 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2),
1370 	INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf),    /* MEM_UOP_RETIRED.* */
1371 	INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
1372 	INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf),    /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
1373 	INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf),    /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
1374 	/* Allow all events as PEBS with no flags */
1375 	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
1376         EVENT_CONSTRAINT_END
1377 };
1378 
1379 struct event_constraint intel_hsw_pebs_event_constraints[] = {
1380 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
1381 	INTEL_PLD_CONSTRAINT(0x01cd, 0xf),    /* MEM_TRANS_RETIRED.* */
1382 	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
1383 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf),
1384 	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
1385 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2),
1386 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
1387 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
1388 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
1389 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
1390 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
1391 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
1392 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
1393 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
1394 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
1395 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd2, 0xf),    /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
1396 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd3, 0xf),    /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
1397 	/* Allow all events as PEBS with no flags */
1398 	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
1399 	EVENT_CONSTRAINT_END
1400 };
1401 
1402 struct event_constraint intel_bdw_pebs_event_constraints[] = {
1403 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
1404 	INTEL_PLD_CONSTRAINT(0x01cd, 0xf),    /* MEM_TRANS_RETIRED.* */
1405 	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
1406 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf),
1407 	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
1408 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2),
1409 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
1410 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
1411 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
1412 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
1413 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
1414 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
1415 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
1416 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
1417 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
1418 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf),    /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
1419 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf),    /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
1420 	/* Allow all events as PEBS with no flags */
1421 	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
1422 	EVENT_CONSTRAINT_END
1423 };
1424 
1425 
1426 struct event_constraint intel_skl_pebs_event_constraints[] = {
1427 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x1c0, 0x2),	/* INST_RETIRED.PREC_DIST */
1428 	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
1429 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2),
1430 	/* INST_RETIRED.TOTAL_CYCLES_PS (inv=1, cmask=16) (cycles:p). */
1431 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f),
1432 	INTEL_PLD_CONSTRAINT(0x1cd, 0xf),		      /* MEM_TRANS_RETIRED.* */
1433 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */
1434 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */
1435 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */
1436 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x22d0, 0xf), /* MEM_INST_RETIRED.LOCK_STORES */
1437 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */
1438 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */
1439 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */
1440 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */
1441 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf),    /* MEM_LOAD_RETIRED.* */
1442 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf),    /* MEM_LOAD_L3_HIT_RETIRED.* */
1443 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf),    /* MEM_LOAD_L3_MISS_RETIRED.* */
1444 	/* Allow all events as PEBS with no flags */
1445 	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
1446 	EVENT_CONSTRAINT_END
1447 };
1448 
1449 struct event_constraint intel_icl_pebs_event_constraints[] = {
1450 	INTEL_PLD_CONSTRAINT(0x1cd, 0xff),			/* MEM_TRANS_RETIRED.LOAD_LATENCY */
1451 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf),	/* MEM_INST_RETIRED.STLB_MISS_LOADS */
1452 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf),	/* MEM_INST_RETIRED.STLB_MISS_STORES */
1453 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf),	/* MEM_INST_RETIRED.LOCK_LOADS */
1454 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf),	/* MEM_INST_RETIRED.SPLIT_LOADS */
1455 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf),	/* MEM_INST_RETIRED.SPLIT_STORES */
1456 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf),	/* MEM_INST_RETIRED.ALL_LOADS */
1457 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf),	/* MEM_INST_RETIRED.ALL_STORES */
1458 
1459 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD_RANGE(0xd1, 0xd4, 0xf), /* MEM_LOAD_*_RETIRED.* */
1460 
1461 	INTEL_FLAGS_EVENT_CONSTRAINT(0xd0, 0xf),		/* MEM_INST_RETIRED.* */
1462 
1463 	/*
1464 	 * Everything else is handled by PMU_FL_PEBS_ALL, because we
1465 	 * need the full constraints from the main table.
1466 	 */
1467 
1468 	EVENT_CONSTRAINT_END
1469 };
1470 
1471 struct event_constraint intel_glc_pebs_event_constraints[] = {
1472 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xfe),
1473 	INTEL_PLD_CONSTRAINT(0x1cd, 0xfe),
1474 	INTEL_PSD_CONSTRAINT(0x2cd, 0x1),
1475 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf),	/* MEM_INST_RETIRED.STLB_MISS_LOADS */
1476 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf),	/* MEM_INST_RETIRED.STLB_MISS_STORES */
1477 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf),	/* MEM_INST_RETIRED.LOCK_LOADS */
1478 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf),	/* MEM_INST_RETIRED.SPLIT_LOADS */
1479 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf),	/* MEM_INST_RETIRED.SPLIT_STORES */
1480 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf),	/* MEM_INST_RETIRED.ALL_LOADS */
1481 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf),	/* MEM_INST_RETIRED.ALL_STORES */
1482 
1483 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD_RANGE(0xd1, 0xd4, 0xf),
1484 
1485 	INTEL_FLAGS_EVENT_CONSTRAINT(0xd0, 0xf),
1486 
1487 	/*
1488 	 * Everything else is handled by PMU_FL_PEBS_ALL, because we
1489 	 * need the full constraints from the main table.
1490 	 */
1491 
1492 	EVENT_CONSTRAINT_END
1493 };
1494 
1495 struct event_constraint intel_lnc_pebs_event_constraints[] = {
1496 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x012a, 0x1),		/* OCR.* events */
1497 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x012b, 0x1),		/* OCR.* events */
1498 
1499 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x04a4, 0x1),		/* TOPDOWN.BAD_SPEC_SLOTS */
1500 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x08a4, 0x1),		/* TOPDOWN.BR_MISPREDICT_SLOTS */
1501 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x10a4, 0x8),		/* TOPDOWN.MEMORY_BOUND_SLOTS */
1502 
1503 	INTEL_HYBRID_LDLAT_CONSTRAINT(0x1cd, 0x3fc),
1504 	INTEL_HYBRID_STLAT_CONSTRAINT(0x2cd, 0x3),
1505 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf),	/* MEM_INST_RETIRED.STLB_MISS_LOADS */
1506 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf),	/* MEM_INST_RETIRED.STLB_MISS_STORES */
1507 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf),	/* MEM_INST_RETIRED.LOCK_LOADS */
1508 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf),	/* MEM_INST_RETIRED.SPLIT_LOADS */
1509 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf),	/* MEM_INST_RETIRED.SPLIT_STORES */
1510 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf),	/* MEM_INST_RETIRED.ALL_LOADS */
1511 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf),	/* MEM_INST_RETIRED.ALL_STORES */
1512 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x87d0, 0x3ff),		/* MEM_INST_RETIRED.ANY */
1513 
1514 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD_RANGE(0xd1, 0xd4, 0xf),
1515 
1516 	INTEL_FLAGS_EVENT_CONSTRAINT(0xd0, 0xf),
1517 
1518 	/*
1519 	 * Everything else is handled by PMU_FL_PEBS_ALL, because we
1520 	 * need the full constraints from the main table.
1521 	 */
1522 
1523 	EVENT_CONSTRAINT_END
1524 };
1525 
1526 struct event_constraint intel_pnc_pebs_event_constraints[] = {
1527 	INTEL_HYBRID_LDLAT_CONSTRAINT(0x1cd, 0xfc),
1528 	INTEL_HYBRID_STLAT_CONSTRAINT(0x2cd, 0x3),
1529 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf),	/* MEM_INST_RETIRED.STLB_MISS_LOADS */
1530 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf),	/* MEM_INST_RETIRED.STLB_MISS_STORES */
1531 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf),	/* MEM_INST_RETIRED.LOCK_LOADS */
1532 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf),	/* MEM_INST_RETIRED.SPLIT_LOADS */
1533 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf),	/* MEM_INST_RETIRED.SPLIT_STORES */
1534 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf),	/* MEM_INST_RETIRED.ALL_LOADS */
1535 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf),	/* MEM_INST_RETIRED.ALL_STORES */
1536 
1537 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD_RANGE(0xd1, 0xd4, 0xf),
1538 
1539 	INTEL_FLAGS_EVENT_CONSTRAINT(0xd0, 0xf),
1540 	INTEL_FLAGS_EVENT_CONSTRAINT(0xd6, 0xf),
1541 
1542 	/*
1543 	 * Everything else is handled by PMU_FL_PEBS_ALL, because we
1544 	 * need the full constraints from the main table.
1545 	 */
1546 
1547 	EVENT_CONSTRAINT_END
1548 };
1549 
intel_pebs_constraints(struct perf_event * event)1550 struct event_constraint *intel_pebs_constraints(struct perf_event *event)
1551 {
1552 	struct event_constraint *pebs_constraints = hybrid(event->pmu, pebs_constraints);
1553 	struct event_constraint *c;
1554 
1555 	if (!event->attr.precise_ip)
1556 		return NULL;
1557 
1558 	if (pebs_constraints) {
1559 		for_each_event_constraint(c, pebs_constraints) {
1560 			if (constraint_match(c, event->hw.config)) {
1561 				event->hw.flags |= c->flags;
1562 				return c;
1563 			}
1564 		}
1565 	}
1566 
1567 	/*
1568 	 * Extended PEBS support
1569 	 * Makes the PEBS code search the normal constraints.
1570 	 */
1571 	if (x86_pmu.flags & PMU_FL_PEBS_ALL)
1572 		return NULL;
1573 
1574 	return &emptyconstraint;
1575 }
1576 
1577 /*
1578  * We need the sched_task callback even for per-cpu events when we use
1579  * the large interrupt threshold, such that we can provide PID and TID
1580  * to PEBS samples.
1581  */
pebs_needs_sched_cb(struct cpu_hw_events * cpuc)1582 static inline bool pebs_needs_sched_cb(struct cpu_hw_events *cpuc)
1583 {
1584 	if (cpuc->n_pebs == cpuc->n_pebs_via_pt)
1585 		return false;
1586 
1587 	return cpuc->n_pebs && (cpuc->n_pebs == cpuc->n_large_pebs);
1588 }
1589 
intel_pmu_pebs_sched_task(struct perf_event_pmu_context * pmu_ctx,bool sched_in)1590 void intel_pmu_pebs_sched_task(struct perf_event_pmu_context *pmu_ctx, bool sched_in)
1591 {
1592 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1593 
1594 	if (!sched_in && pebs_needs_sched_cb(cpuc))
1595 		intel_pmu_drain_pebs_buffer();
1596 }
1597 
pebs_update_threshold(struct cpu_hw_events * cpuc)1598 static inline void pebs_update_threshold(struct cpu_hw_events *cpuc)
1599 {
1600 	struct debug_store *ds = cpuc->ds;
1601 	int max_pebs_events = intel_pmu_max_num_pebs(cpuc->pmu);
1602 	u64 threshold;
1603 	int reserved;
1604 
1605 	if (cpuc->n_pebs_via_pt)
1606 		return;
1607 
1608 	if (x86_pmu.flags & PMU_FL_PEBS_ALL)
1609 		reserved = max_pebs_events + x86_pmu_max_num_counters_fixed(cpuc->pmu);
1610 	else
1611 		reserved = max_pebs_events;
1612 
1613 	if (cpuc->n_pebs == cpuc->n_large_pebs) {
1614 		threshold = ds->pebs_absolute_maximum -
1615 			reserved * cpuc->pebs_record_size;
1616 	} else {
1617 		threshold = ds->pebs_buffer_base + cpuc->pebs_record_size;
1618 	}
1619 
1620 	ds->pebs_interrupt_threshold = threshold;
1621 }
1622 
1623 #define PEBS_DATACFG_CNTRS(x)						\
1624 	((x >> PEBS_DATACFG_CNTR_SHIFT) & PEBS_DATACFG_CNTR_MASK)
1625 
1626 #define PEBS_DATACFG_CNTR_BIT(x)					\
1627 	(((1ULL << x) & PEBS_DATACFG_CNTR_MASK) << PEBS_DATACFG_CNTR_SHIFT)
1628 
1629 #define PEBS_DATACFG_FIX(x)						\
1630 	((x >> PEBS_DATACFG_FIX_SHIFT) & PEBS_DATACFG_FIX_MASK)
1631 
1632 #define PEBS_DATACFG_FIX_BIT(x)						\
1633 	(((1ULL << (x)) & PEBS_DATACFG_FIX_MASK)			\
1634 	 << PEBS_DATACFG_FIX_SHIFT)
1635 
adaptive_pebs_record_size_update(void)1636 static void adaptive_pebs_record_size_update(void)
1637 {
1638 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1639 	u64 pebs_data_cfg = cpuc->pebs_data_cfg;
1640 	int sz = sizeof(struct pebs_basic);
1641 
1642 	if (pebs_data_cfg & PEBS_DATACFG_MEMINFO)
1643 		sz += sizeof(struct pebs_meminfo);
1644 	if (pebs_data_cfg & PEBS_DATACFG_GP)
1645 		sz += sizeof(struct pebs_gprs);
1646 	if (pebs_data_cfg & PEBS_DATACFG_XMMS)
1647 		sz += sizeof(struct pebs_xmm);
1648 	if (pebs_data_cfg & PEBS_DATACFG_LBRS)
1649 		sz += x86_pmu.lbr_nr * sizeof(struct lbr_entry);
1650 	if (pebs_data_cfg & (PEBS_DATACFG_METRICS | PEBS_DATACFG_CNTR)) {
1651 		sz += sizeof(struct pebs_cntr_header);
1652 
1653 		/* Metrics base and Metrics Data */
1654 		if (pebs_data_cfg & PEBS_DATACFG_METRICS)
1655 			sz += 2 * sizeof(u64);
1656 
1657 		if (pebs_data_cfg & PEBS_DATACFG_CNTR) {
1658 			sz += (hweight64(PEBS_DATACFG_CNTRS(pebs_data_cfg)) +
1659 			       hweight64(PEBS_DATACFG_FIX(pebs_data_cfg))) *
1660 			      sizeof(u64);
1661 		}
1662 	}
1663 
1664 	cpuc->pebs_record_size = sz;
1665 }
1666 
__intel_pmu_pebs_update_cfg(struct perf_event * event,int idx,u64 * pebs_data_cfg)1667 static void __intel_pmu_pebs_update_cfg(struct perf_event *event,
1668 					int idx, u64 *pebs_data_cfg)
1669 {
1670 	if (is_metric_event(event)) {
1671 		*pebs_data_cfg |= PEBS_DATACFG_METRICS;
1672 		return;
1673 	}
1674 
1675 	*pebs_data_cfg |= PEBS_DATACFG_CNTR;
1676 
1677 	if (idx >= INTEL_PMC_IDX_FIXED)
1678 		*pebs_data_cfg |= PEBS_DATACFG_FIX_BIT(idx - INTEL_PMC_IDX_FIXED);
1679 	else
1680 		*pebs_data_cfg |= PEBS_DATACFG_CNTR_BIT(idx);
1681 }
1682 
1683 
intel_pmu_pebs_late_setup(struct cpu_hw_events * cpuc)1684 void intel_pmu_pebs_late_setup(struct cpu_hw_events *cpuc)
1685 {
1686 	struct perf_event *event;
1687 	u64 pebs_data_cfg = 0;
1688 	int i;
1689 
1690 	for (i = 0; i < cpuc->n_events; i++) {
1691 		event = cpuc->event_list[i];
1692 		if (!is_pebs_counter_event_group(event))
1693 			continue;
1694 		__intel_pmu_pebs_update_cfg(event, cpuc->assign[i], &pebs_data_cfg);
1695 	}
1696 
1697 	if (pebs_data_cfg & ~cpuc->pebs_data_cfg)
1698 		cpuc->pebs_data_cfg |= pebs_data_cfg | PEBS_UPDATE_DS_SW;
1699 }
1700 
1701 #define PERF_PEBS_MEMINFO_TYPE	(PERF_SAMPLE_ADDR | PERF_SAMPLE_DATA_SRC |   \
1702 				PERF_SAMPLE_PHYS_ADDR |			     \
1703 				PERF_SAMPLE_WEIGHT_TYPE |		     \
1704 				PERF_SAMPLE_TRANSACTION |		     \
1705 				PERF_SAMPLE_DATA_PAGE_SIZE)
1706 
pebs_update_adaptive_cfg(struct perf_event * event)1707 static u64 pebs_update_adaptive_cfg(struct perf_event *event)
1708 {
1709 	struct perf_event_attr *attr = &event->attr;
1710 	u64 sample_type = attr->sample_type;
1711 	u64 pebs_data_cfg = 0;
1712 	bool gprs, tsx_weight;
1713 
1714 	if (!(sample_type & ~(PERF_SAMPLE_IP|PERF_SAMPLE_TIME)) &&
1715 	    attr->precise_ip > 1)
1716 		return pebs_data_cfg;
1717 
1718 	if (sample_type & PERF_PEBS_MEMINFO_TYPE)
1719 		pebs_data_cfg |= PEBS_DATACFG_MEMINFO;
1720 
1721 	/*
1722 	 * We need GPRs when:
1723 	 * + user requested them
1724 	 * + precise_ip < 2 for the non event IP
1725 	 * + For RTM TSX weight we need GPRs for the abort code.
1726 	 */
1727 	gprs = ((sample_type & PERF_SAMPLE_REGS_INTR) &&
1728 		(attr->sample_regs_intr & PEBS_GP_REGS)) ||
1729 	       ((sample_type & PERF_SAMPLE_REGS_USER) &&
1730 		(attr->sample_regs_user & PEBS_GP_REGS));
1731 
1732 	tsx_weight = (sample_type & PERF_SAMPLE_WEIGHT_TYPE) &&
1733 		     ((attr->config & INTEL_ARCH_EVENT_MASK) ==
1734 		      x86_pmu.rtm_abort_event);
1735 
1736 	if (gprs || (attr->precise_ip < 2) || tsx_weight)
1737 		pebs_data_cfg |= PEBS_DATACFG_GP;
1738 
1739 	if ((sample_type & PERF_SAMPLE_REGS_INTR) &&
1740 	    (attr->sample_regs_intr & PERF_REG_EXTENDED_MASK))
1741 		pebs_data_cfg |= PEBS_DATACFG_XMMS;
1742 
1743 	if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
1744 		/*
1745 		 * For now always log all LBRs. Could configure this
1746 		 * later.
1747 		 */
1748 		pebs_data_cfg |= PEBS_DATACFG_LBRS |
1749 			((x86_pmu.lbr_nr-1) << PEBS_DATACFG_LBR_SHIFT);
1750 	}
1751 
1752 	return pebs_data_cfg;
1753 }
1754 
1755 static void
pebs_update_state(bool needed_cb,struct cpu_hw_events * cpuc,struct perf_event * event,bool add)1756 pebs_update_state(bool needed_cb, struct cpu_hw_events *cpuc,
1757 		  struct perf_event *event, bool add)
1758 {
1759 	struct pmu *pmu = event->pmu;
1760 
1761 	/*
1762 	 * Make sure we get updated with the first PEBS event.
1763 	 * During removal, ->pebs_data_cfg is still valid for
1764 	 * the last PEBS event. Don't clear it.
1765 	 */
1766 	if ((cpuc->n_pebs == 1) && add)
1767 		cpuc->pebs_data_cfg = PEBS_UPDATE_DS_SW;
1768 
1769 	if (needed_cb != pebs_needs_sched_cb(cpuc)) {
1770 		if (!needed_cb)
1771 			perf_sched_cb_inc(pmu);
1772 		else
1773 			perf_sched_cb_dec(pmu);
1774 
1775 		cpuc->pebs_data_cfg |= PEBS_UPDATE_DS_SW;
1776 	}
1777 
1778 	/*
1779 	 * The PEBS record doesn't shrink on pmu::del(). Doing so would require
1780 	 * iterating all remaining PEBS events to reconstruct the config.
1781 	 */
1782 	if (x86_pmu.intel_cap.pebs_baseline && add) {
1783 		u64 pebs_data_cfg;
1784 
1785 		pebs_data_cfg = pebs_update_adaptive_cfg(event);
1786 		/*
1787 		 * Be sure to update the thresholds when we change the record.
1788 		 */
1789 		if (pebs_data_cfg & ~cpuc->pebs_data_cfg)
1790 			cpuc->pebs_data_cfg |= pebs_data_cfg | PEBS_UPDATE_DS_SW;
1791 	}
1792 }
1793 
intel_get_arch_pebs_data_config(struct perf_event * event)1794 u64 intel_get_arch_pebs_data_config(struct perf_event *event)
1795 {
1796 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1797 	u64 pebs_data_cfg = 0;
1798 	u64 cntr_mask;
1799 
1800 	if (WARN_ON(event->hw.idx < 0 || event->hw.idx >= X86_PMC_IDX_MAX))
1801 		return 0;
1802 
1803 	pebs_data_cfg |= pebs_update_adaptive_cfg(event);
1804 
1805 	cntr_mask = (PEBS_DATACFG_CNTR_MASK << PEBS_DATACFG_CNTR_SHIFT) |
1806 		    (PEBS_DATACFG_FIX_MASK << PEBS_DATACFG_FIX_SHIFT) |
1807 		    PEBS_DATACFG_CNTR | PEBS_DATACFG_METRICS;
1808 	pebs_data_cfg |= cpuc->pebs_data_cfg & cntr_mask;
1809 
1810 	return pebs_data_cfg;
1811 }
1812 
intel_pmu_pebs_add(struct perf_event * event)1813 void intel_pmu_pebs_add(struct perf_event *event)
1814 {
1815 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1816 	struct hw_perf_event *hwc = &event->hw;
1817 	bool needed_cb = pebs_needs_sched_cb(cpuc);
1818 
1819 	cpuc->n_pebs++;
1820 	if (hwc->flags & PERF_X86_EVENT_LARGE_PEBS)
1821 		cpuc->n_large_pebs++;
1822 	if (hwc->flags & PERF_X86_EVENT_PEBS_VIA_PT)
1823 		cpuc->n_pebs_via_pt++;
1824 
1825 	pebs_update_state(needed_cb, cpuc, event, true);
1826 }
1827 
intel_pmu_pebs_via_pt_disable(struct perf_event * event)1828 static void intel_pmu_pebs_via_pt_disable(struct perf_event *event)
1829 {
1830 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1831 
1832 	if (!is_pebs_pt(event))
1833 		return;
1834 
1835 	if (!(cpuc->pebs_enabled & ~PEBS_VIA_PT_MASK))
1836 		cpuc->pebs_enabled &= ~PEBS_VIA_PT_MASK;
1837 }
1838 
intel_pmu_pebs_via_pt_enable(struct perf_event * event)1839 static void intel_pmu_pebs_via_pt_enable(struct perf_event *event)
1840 {
1841 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1842 	struct hw_perf_event *hwc = &event->hw;
1843 	struct debug_store *ds = cpuc->ds;
1844 	u64 value = ds->pebs_event_reset[hwc->idx];
1845 	u32 base = MSR_RELOAD_PMC0;
1846 	unsigned int idx = hwc->idx;
1847 
1848 	if (!is_pebs_pt(event))
1849 		return;
1850 
1851 	if (!(event->hw.flags & PERF_X86_EVENT_LARGE_PEBS))
1852 		cpuc->pebs_enabled |= PEBS_PMI_AFTER_EACH_RECORD;
1853 
1854 	cpuc->pebs_enabled |= PEBS_OUTPUT_PT;
1855 
1856 	if (hwc->idx >= INTEL_PMC_IDX_FIXED) {
1857 		base = MSR_RELOAD_FIXED_CTR0;
1858 		idx = hwc->idx - INTEL_PMC_IDX_FIXED;
1859 		if (x86_pmu.intel_cap.pebs_format < 5)
1860 			value = ds->pebs_event_reset[MAX_PEBS_EVENTS_FMT4 + idx];
1861 		else
1862 			value = ds->pebs_event_reset[MAX_PEBS_EVENTS + idx];
1863 	}
1864 	wrmsrq(base + idx, value);
1865 }
1866 
intel_pmu_drain_large_pebs(struct cpu_hw_events * cpuc)1867 static inline void intel_pmu_drain_large_pebs(struct cpu_hw_events *cpuc)
1868 {
1869 	if (cpuc->n_pebs == cpuc->n_large_pebs &&
1870 	    cpuc->n_pebs != cpuc->n_pebs_via_pt) {
1871 		int enabled = __intel_pmu_quiesce();
1872 		intel_pmu_drain_pebs_buffer();
1873 		__intel_pmu_resume(enabled);
1874 	}
1875 }
1876 
__intel_pmu_pebs_enable(struct perf_event * event)1877 static void __intel_pmu_pebs_enable(struct perf_event *event)
1878 {
1879 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1880 	struct hw_perf_event *hwc = &event->hw;
1881 
1882 	hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;
1883 	cpuc->pebs_enabled |= 1ULL << hwc->idx;
1884 }
1885 
intel_pmu_pebs_enable(struct perf_event * event)1886 void intel_pmu_pebs_enable(struct perf_event *event)
1887 {
1888 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1889 	u64 pebs_data_cfg = cpuc->pebs_data_cfg & ~PEBS_UPDATE_DS_SW;
1890 	struct hw_perf_event *hwc = &event->hw;
1891 	struct debug_store *ds = cpuc->ds;
1892 	unsigned int idx = hwc->idx;
1893 
1894 	__intel_pmu_pebs_enable(event);
1895 
1896 	if ((event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT) && (x86_pmu.version < 5))
1897 		cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32);
1898 	else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
1899 		cpuc->pebs_enabled |= 1ULL << 63;
1900 
1901 	if (x86_pmu.intel_cap.pebs_baseline) {
1902 		hwc->config |= ICL_EVENTSEL_ADAPTIVE;
1903 		if (pebs_data_cfg != cpuc->active_pebs_data_cfg) {
1904 			/*
1905 			 * drain_pebs() assumes uniform record size;
1906 			 * hence we need to drain when changing said
1907 			 * size.
1908 			 */
1909 			intel_pmu_drain_pebs_buffer();
1910 			adaptive_pebs_record_size_update();
1911 			wrmsrq(MSR_PEBS_DATA_CFG, pebs_data_cfg);
1912 			cpuc->active_pebs_data_cfg = pebs_data_cfg;
1913 		}
1914 	}
1915 	if (cpuc->pebs_data_cfg & PEBS_UPDATE_DS_SW) {
1916 		cpuc->pebs_data_cfg = pebs_data_cfg;
1917 		pebs_update_threshold(cpuc);
1918 	}
1919 
1920 	if (idx >= INTEL_PMC_IDX_FIXED) {
1921 		if (x86_pmu.intel_cap.pebs_format < 5)
1922 			idx = MAX_PEBS_EVENTS_FMT4 + (idx - INTEL_PMC_IDX_FIXED);
1923 		else
1924 			idx = MAX_PEBS_EVENTS + (idx - INTEL_PMC_IDX_FIXED);
1925 	}
1926 
1927 	/*
1928 	 * Use auto-reload if possible to save a MSR write in the PMI.
1929 	 * This must be done in pmu::start(), because PERF_EVENT_IOC_PERIOD.
1930 	 */
1931 	if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
1932 		ds->pebs_event_reset[idx] =
1933 			(u64)(-hwc->sample_period) & x86_pmu.cntval_mask;
1934 	} else {
1935 		ds->pebs_event_reset[idx] = 0;
1936 	}
1937 
1938 	intel_pmu_pebs_via_pt_enable(event);
1939 }
1940 
intel_pmu_pebs_del(struct perf_event * event)1941 void intel_pmu_pebs_del(struct perf_event *event)
1942 {
1943 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1944 	struct hw_perf_event *hwc = &event->hw;
1945 	bool needed_cb = pebs_needs_sched_cb(cpuc);
1946 
1947 	cpuc->n_pebs--;
1948 	if (hwc->flags & PERF_X86_EVENT_LARGE_PEBS)
1949 		cpuc->n_large_pebs--;
1950 	if (hwc->flags & PERF_X86_EVENT_PEBS_VIA_PT)
1951 		cpuc->n_pebs_via_pt--;
1952 
1953 	pebs_update_state(needed_cb, cpuc, event, false);
1954 }
1955 
__intel_pmu_pebs_disable(struct perf_event * event)1956 static void __intel_pmu_pebs_disable(struct perf_event *event)
1957 {
1958 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1959 	struct hw_perf_event *hwc = &event->hw;
1960 
1961 	intel_pmu_drain_large_pebs(cpuc);
1962 	cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
1963 	hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
1964 }
1965 
intel_pmu_pebs_disable(struct perf_event * event)1966 void intel_pmu_pebs_disable(struct perf_event *event)
1967 {
1968 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1969 	struct hw_perf_event *hwc = &event->hw;
1970 
1971 	__intel_pmu_pebs_disable(event);
1972 
1973 	if ((event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT) &&
1974 	    (x86_pmu.version < 5))
1975 		cpuc->pebs_enabled &= ~(1ULL << (hwc->idx + 32));
1976 	else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
1977 		cpuc->pebs_enabled &= ~(1ULL << 63);
1978 
1979 	intel_pmu_pebs_via_pt_disable(event);
1980 
1981 	if (cpuc->enabled)
1982 		wrmsrq(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
1983 }
1984 
intel_pmu_pebs_enable_all(void)1985 void intel_pmu_pebs_enable_all(void)
1986 {
1987 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1988 
1989 	if (cpuc->pebs_enabled)
1990 		wrmsrq(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
1991 }
1992 
intel_pmu_pebs_disable_all(void)1993 void intel_pmu_pebs_disable_all(void)
1994 {
1995 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1996 
1997 	if (cpuc->pebs_enabled)
1998 		__intel_pmu_pebs_disable_all();
1999 }
2000 
intel_pmu_pebs_fixup_ip(struct pt_regs * regs)2001 static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
2002 {
2003 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2004 	unsigned long from = cpuc->lbr_entries[0].from;
2005 	unsigned long old_to, to = cpuc->lbr_entries[0].to;
2006 	unsigned long ip = regs->ip;
2007 	int is_64bit = 0;
2008 	void *kaddr;
2009 	int size;
2010 
2011 	/*
2012 	 * We don't need to fixup if the PEBS assist is fault like
2013 	 */
2014 	if (!x86_pmu.intel_cap.pebs_trap)
2015 		return 1;
2016 
2017 	/*
2018 	 * No LBR entry, no basic block, no rewinding
2019 	 */
2020 	if (!cpuc->lbr_stack.nr || !from || !to)
2021 		return 0;
2022 
2023 	/*
2024 	 * Basic blocks should never cross user/kernel boundaries
2025 	 */
2026 	if (kernel_ip(ip) != kernel_ip(to))
2027 		return 0;
2028 
2029 	/*
2030 	 * unsigned math, either ip is before the start (impossible) or
2031 	 * the basic block is larger than 1 page (sanity)
2032 	 */
2033 	if ((ip - to) > PEBS_FIXUP_SIZE)
2034 		return 0;
2035 
2036 	/*
2037 	 * We sampled a branch insn, rewind using the LBR stack
2038 	 */
2039 	if (ip == to) {
2040 		set_linear_ip(regs, from);
2041 		return 1;
2042 	}
2043 
2044 	size = ip - to;
2045 	if (!kernel_ip(ip)) {
2046 		int bytes;
2047 		u8 *buf = this_cpu_read(insn_buffer);
2048 
2049 		/* 'size' must fit our buffer, see above */
2050 		bytes = copy_from_user_nmi(buf, (void __user *)to, size);
2051 		if (bytes != 0)
2052 			return 0;
2053 
2054 		kaddr = buf;
2055 	} else {
2056 		kaddr = (void *)to;
2057 	}
2058 
2059 	do {
2060 		struct insn insn;
2061 
2062 		old_to = to;
2063 
2064 #ifdef CONFIG_X86_64
2065 		is_64bit = kernel_ip(to) || any_64bit_mode(regs);
2066 #endif
2067 		insn_init(&insn, kaddr, size, is_64bit);
2068 
2069 		/*
2070 		 * Make sure there was not a problem decoding the instruction.
2071 		 * This is doubly important because we have an infinite loop if
2072 		 * insn.length=0.
2073 		 */
2074 		if (insn_get_length(&insn))
2075 			break;
2076 
2077 		to += insn.length;
2078 		kaddr += insn.length;
2079 		size -= insn.length;
2080 	} while (to < ip);
2081 
2082 	if (to == ip) {
2083 		set_linear_ip(regs, old_to);
2084 		return 1;
2085 	}
2086 
2087 	/*
2088 	 * Even though we decoded the basic block, the instruction stream
2089 	 * never matched the given IP, either the TO or the IP got corrupted.
2090 	 */
2091 	return 0;
2092 }
2093 
intel_get_tsx_weight(u64 tsx_tuning)2094 static inline u64 intel_get_tsx_weight(u64 tsx_tuning)
2095 {
2096 	if (tsx_tuning) {
2097 		union hsw_tsx_tuning tsx = { .value = tsx_tuning };
2098 		return tsx.cycles_last_block;
2099 	}
2100 	return 0;
2101 }
2102 
intel_get_tsx_transaction(u64 tsx_tuning,u64 ax)2103 static inline u64 intel_get_tsx_transaction(u64 tsx_tuning, u64 ax)
2104 {
2105 	u64 txn = (tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32;
2106 
2107 	/* For RTM XABORTs also log the abort code from AX */
2108 	if ((txn & PERF_TXN_TRANSACTION) && (ax & 1))
2109 		txn |= ((ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
2110 	return txn;
2111 }
2112 
get_pebs_status(void * n)2113 static inline u64 get_pebs_status(void *n)
2114 {
2115 	if (x86_pmu.intel_cap.pebs_format < 4)
2116 		return ((struct pebs_record_nhm *)n)->status;
2117 	return ((struct pebs_basic *)n)->applicable_counters;
2118 }
2119 
2120 #define PERF_X86_EVENT_PEBS_HSW_PREC \
2121 		(PERF_X86_EVENT_PEBS_ST_HSW | \
2122 		 PERF_X86_EVENT_PEBS_LD_HSW | \
2123 		 PERF_X86_EVENT_PEBS_NA_HSW)
2124 
get_data_src(struct perf_event * event,u64 aux)2125 static u64 get_data_src(struct perf_event *event, u64 aux)
2126 {
2127 	u64 val = PERF_MEM_NA;
2128 	int fl = event->hw.flags;
2129 	bool fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC);
2130 
2131 	if (fl & PERF_X86_EVENT_PEBS_LDLAT)
2132 		val = load_latency_data(event, aux);
2133 	else if (fl & PERF_X86_EVENT_PEBS_STLAT)
2134 		val = store_latency_data(event, aux);
2135 	else if (fl & PERF_X86_EVENT_PEBS_LAT_HYBRID)
2136 		val = x86_pmu.pebs_latency_data(event, aux);
2137 	else if (fst && (fl & PERF_X86_EVENT_PEBS_HSW_PREC))
2138 		val = precise_datala_hsw(event, aux);
2139 	else if (fst)
2140 		val = precise_store_data(aux);
2141 	return val;
2142 }
2143 
setup_pebs_time(struct perf_event * event,struct perf_sample_data * data,u64 tsc)2144 static void setup_pebs_time(struct perf_event *event,
2145 			    struct perf_sample_data *data,
2146 			    u64 tsc)
2147 {
2148 	/* Converting to a user-defined clock is not supported yet. */
2149 	if (event->attr.use_clockid != 0)
2150 		return;
2151 
2152 	/*
2153 	 * Doesn't support the conversion when the TSC is unstable.
2154 	 * The TSC unstable case is a corner case and very unlikely to
2155 	 * happen. If it happens, the TSC in a PEBS record will be
2156 	 * dropped and fall back to perf_event_clock().
2157 	 */
2158 	if (!using_native_sched_clock() || !sched_clock_stable())
2159 		return;
2160 
2161 	data->time = native_sched_clock_from_tsc(tsc) + __sched_clock_offset;
2162 	data->sample_flags |= PERF_SAMPLE_TIME;
2163 }
2164 
2165 #define PERF_SAMPLE_ADDR_TYPE	(PERF_SAMPLE_ADDR |		\
2166 				 PERF_SAMPLE_PHYS_ADDR |	\
2167 				 PERF_SAMPLE_DATA_PAGE_SIZE)
2168 
setup_pebs_fixed_sample_data(struct perf_event * event,struct pt_regs * iregs,void * __pebs,struct perf_sample_data * data,struct pt_regs * regs)2169 static void setup_pebs_fixed_sample_data(struct perf_event *event,
2170 				   struct pt_regs *iregs, void *__pebs,
2171 				   struct perf_sample_data *data,
2172 				   struct pt_regs *regs)
2173 {
2174 	/*
2175 	 * We cast to the biggest pebs_record but are careful not to
2176 	 * unconditionally access the 'extra' entries.
2177 	 */
2178 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2179 	struct pebs_record_skl *pebs = __pebs;
2180 	u64 sample_type;
2181 	int fll;
2182 
2183 	if (pebs == NULL)
2184 		return;
2185 
2186 	sample_type = event->attr.sample_type;
2187 	fll = event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT;
2188 
2189 	perf_sample_data_init(data, 0, event->hw.last_period);
2190 
2191 	/*
2192 	 * Use latency for weight (only avail with PEBS-LL)
2193 	 */
2194 	if (fll && (sample_type & PERF_SAMPLE_WEIGHT_TYPE)) {
2195 		data->weight.full = pebs->lat;
2196 		data->sample_flags |= PERF_SAMPLE_WEIGHT_TYPE;
2197 	}
2198 
2199 	/*
2200 	 * data.data_src encodes the data source
2201 	 */
2202 	if (sample_type & PERF_SAMPLE_DATA_SRC) {
2203 		data->data_src.val = get_data_src(event, pebs->dse);
2204 		data->sample_flags |= PERF_SAMPLE_DATA_SRC;
2205 	}
2206 
2207 	/*
2208 	 * We must however always use iregs for the unwinder to stay sane; the
2209 	 * record BP,SP,IP can point into thin air when the record is from a
2210 	 * previous PMI context or an (I)RET happened between the record and
2211 	 * PMI.
2212 	 */
2213 	perf_sample_save_callchain(data, event, iregs);
2214 
2215 	/*
2216 	 * We use the interrupt regs as a base because the PEBS record does not
2217 	 * contain a full regs set, specifically it seems to lack segment
2218 	 * descriptors, which get used by things like user_mode().
2219 	 *
2220 	 * In the simple case fix up only the IP for PERF_SAMPLE_IP.
2221 	 */
2222 	*regs = *iregs;
2223 
2224 	/*
2225 	 * Initialize regs_>flags from PEBS,
2226 	 * Clear exact bit (which uses x86 EFLAGS Reserved bit 3),
2227 	 * i.e., do not rely on it being zero:
2228 	 */
2229 	regs->flags = pebs->flags & ~PERF_EFLAGS_EXACT;
2230 
2231 	if (sample_type & PERF_SAMPLE_REGS_INTR) {
2232 		regs->ax = pebs->ax;
2233 		regs->bx = pebs->bx;
2234 		regs->cx = pebs->cx;
2235 		regs->dx = pebs->dx;
2236 		regs->si = pebs->si;
2237 		regs->di = pebs->di;
2238 
2239 		regs->bp = pebs->bp;
2240 		regs->sp = pebs->sp;
2241 
2242 #ifndef CONFIG_X86_32
2243 		regs->r8 = pebs->r8;
2244 		regs->r9 = pebs->r9;
2245 		regs->r10 = pebs->r10;
2246 		regs->r11 = pebs->r11;
2247 		regs->r12 = pebs->r12;
2248 		regs->r13 = pebs->r13;
2249 		regs->r14 = pebs->r14;
2250 		regs->r15 = pebs->r15;
2251 #endif
2252 	}
2253 
2254 	if (event->attr.precise_ip > 1) {
2255 		/*
2256 		 * Haswell and later processors have an 'eventing IP'
2257 		 * (real IP) which fixes the off-by-1 skid in hardware.
2258 		 * Use it when precise_ip >= 2 :
2259 		 */
2260 		if (x86_pmu.intel_cap.pebs_format >= 2) {
2261 			set_linear_ip(regs, pebs->real_ip);
2262 			regs->flags |= PERF_EFLAGS_EXACT;
2263 		} else {
2264 			/* Otherwise, use PEBS off-by-1 IP: */
2265 			set_linear_ip(regs, pebs->ip);
2266 
2267 			/*
2268 			 * With precise_ip >= 2, try to fix up the off-by-1 IP
2269 			 * using the LBR. If successful, the fixup function
2270 			 * corrects regs->ip and calls set_linear_ip() on regs:
2271 			 */
2272 			if (intel_pmu_pebs_fixup_ip(regs))
2273 				regs->flags |= PERF_EFLAGS_EXACT;
2274 		}
2275 	} else {
2276 		/*
2277 		 * When precise_ip == 1, return the PEBS off-by-1 IP,
2278 		 * no fixup attempted:
2279 		 */
2280 		set_linear_ip(regs, pebs->ip);
2281 	}
2282 
2283 
2284 	if ((sample_type & PERF_SAMPLE_ADDR_TYPE) &&
2285 	    x86_pmu.intel_cap.pebs_format >= 1) {
2286 		data->addr = pebs->dla;
2287 		data->sample_flags |= PERF_SAMPLE_ADDR;
2288 	}
2289 
2290 	if (x86_pmu.intel_cap.pebs_format >= 2) {
2291 		/* Only set the TSX weight when no memory weight. */
2292 		if ((sample_type & PERF_SAMPLE_WEIGHT_TYPE) && !fll) {
2293 			data->weight.full = intel_get_tsx_weight(pebs->tsx_tuning);
2294 			data->sample_flags |= PERF_SAMPLE_WEIGHT_TYPE;
2295 		}
2296 		if (sample_type & PERF_SAMPLE_TRANSACTION) {
2297 			data->txn = intel_get_tsx_transaction(pebs->tsx_tuning,
2298 							      pebs->ax);
2299 			data->sample_flags |= PERF_SAMPLE_TRANSACTION;
2300 		}
2301 	}
2302 
2303 	/*
2304 	 * v3 supplies an accurate time stamp, so we use that
2305 	 * for the time stamp.
2306 	 *
2307 	 * We can only do this for the default trace clock.
2308 	 */
2309 	if (x86_pmu.intel_cap.pebs_format >= 3)
2310 		setup_pebs_time(event, data, pebs->tsc);
2311 
2312 	perf_sample_save_brstack(data, event, &cpuc->lbr_stack, NULL);
2313 }
2314 
adaptive_pebs_save_regs(struct pt_regs * regs,struct pebs_gprs * gprs)2315 static void adaptive_pebs_save_regs(struct pt_regs *regs,
2316 				    struct pebs_gprs *gprs)
2317 {
2318 	regs->ax = gprs->ax;
2319 	regs->bx = gprs->bx;
2320 	regs->cx = gprs->cx;
2321 	regs->dx = gprs->dx;
2322 	regs->si = gprs->si;
2323 	regs->di = gprs->di;
2324 	regs->bp = gprs->bp;
2325 	regs->sp = gprs->sp;
2326 #ifndef CONFIG_X86_32
2327 	regs->r8 = gprs->r8;
2328 	regs->r9 = gprs->r9;
2329 	regs->r10 = gprs->r10;
2330 	regs->r11 = gprs->r11;
2331 	regs->r12 = gprs->r12;
2332 	regs->r13 = gprs->r13;
2333 	regs->r14 = gprs->r14;
2334 	regs->r15 = gprs->r15;
2335 #endif
2336 }
2337 
intel_perf_event_update_pmc(struct perf_event * event,u64 pmc)2338 static void intel_perf_event_update_pmc(struct perf_event *event, u64 pmc)
2339 {
2340 	int shift = 64 - x86_pmu.cntval_bits;
2341 	struct hw_perf_event *hwc;
2342 	u64 delta, prev_pmc;
2343 
2344 	/*
2345 	 * A recorded counter may not have an assigned event in the
2346 	 * following cases. The value should be dropped.
2347 	 * - An event is deleted. There is still an active PEBS event.
2348 	 *   The PEBS record doesn't shrink on pmu::del().
2349 	 *   If the counter of the deleted event once occurred in a PEBS
2350 	 *   record, PEBS still records the counter until the counter is
2351 	 *   reassigned.
2352 	 * - An event is stopped for some reason, e.g., throttled.
2353 	 *   During this period, another event is added and takes the
2354 	 *   counter of the stopped event. The stopped event is assigned
2355 	 *   to another new and uninitialized counter, since the
2356 	 *   x86_pmu_start(RELOAD) is not invoked for a stopped event.
2357 	 *   The PEBS__DATA_CFG is updated regardless of the event state.
2358 	 *   The uninitialized counter can be recorded in a PEBS record.
2359 	 *   But the cpuc->events[uninitialized_counter] is always NULL,
2360 	 *   because the event is stopped. The uninitialized value is
2361 	 *   safely dropped.
2362 	 */
2363 	if (!event)
2364 		return;
2365 
2366 	hwc = &event->hw;
2367 	prev_pmc = local64_read(&hwc->prev_count);
2368 
2369 	/* Only update the count when the PMU is disabled */
2370 	WARN_ON(this_cpu_read(cpu_hw_events.enabled));
2371 	local64_set(&hwc->prev_count, pmc);
2372 
2373 	delta = (pmc << shift) - (prev_pmc << shift);
2374 	delta >>= shift;
2375 
2376 	local64_add(delta, &event->count);
2377 	local64_sub(delta, &hwc->period_left);
2378 }
2379 
__setup_pebs_counter_group(struct cpu_hw_events * cpuc,struct perf_event * event,struct pebs_cntr_header * cntr,void * next_record)2380 static inline void __setup_pebs_counter_group(struct cpu_hw_events *cpuc,
2381 					      struct perf_event *event,
2382 					      struct pebs_cntr_header *cntr,
2383 					      void *next_record)
2384 {
2385 	int bit;
2386 
2387 	for_each_set_bit(bit, (unsigned long *)&cntr->cntr, INTEL_PMC_MAX_GENERIC) {
2388 		intel_perf_event_update_pmc(cpuc->events[bit], *(u64 *)next_record);
2389 		next_record += sizeof(u64);
2390 	}
2391 
2392 	for_each_set_bit(bit, (unsigned long *)&cntr->fixed, INTEL_PMC_MAX_FIXED) {
2393 		/* The slots event will be handled with perf_metric later */
2394 		if ((cntr->metrics == INTEL_CNTR_METRICS) &&
2395 		    (bit + INTEL_PMC_IDX_FIXED == INTEL_PMC_IDX_FIXED_SLOTS)) {
2396 			next_record += sizeof(u64);
2397 			continue;
2398 		}
2399 		intel_perf_event_update_pmc(cpuc->events[bit + INTEL_PMC_IDX_FIXED],
2400 					    *(u64 *)next_record);
2401 		next_record += sizeof(u64);
2402 	}
2403 
2404 	/* HW will reload the value right after the overflow. */
2405 	if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)
2406 		local64_set(&event->hw.prev_count, (u64)-event->hw.sample_period);
2407 
2408 	if (cntr->metrics == INTEL_CNTR_METRICS) {
2409 		static_call(intel_pmu_update_topdown_event)
2410 			   (cpuc->events[INTEL_PMC_IDX_FIXED_SLOTS],
2411 			    (u64 *)next_record);
2412 		next_record += 2 * sizeof(u64);
2413 	}
2414 }
2415 
2416 #define PEBS_LATENCY_MASK			0xffff
2417 
__setup_perf_sample_data(struct perf_event * event,struct pt_regs * iregs,struct perf_sample_data * data)2418 static inline void __setup_perf_sample_data(struct perf_event *event,
2419 					    struct pt_regs *iregs,
2420 					    struct perf_sample_data *data)
2421 {
2422 	perf_sample_data_init(data, 0, event->hw.last_period);
2423 
2424 	/*
2425 	 * We must however always use iregs for the unwinder to stay sane; the
2426 	 * record BP,SP,IP can point into thin air when the record is from a
2427 	 * previous PMI context or an (I)RET happened between the record and
2428 	 * PMI.
2429 	 */
2430 	perf_sample_save_callchain(data, event, iregs);
2431 }
2432 
__setup_pebs_basic_group(struct perf_event * event,struct pt_regs * regs,struct perf_sample_data * data,u64 sample_type,u64 ip,u64 tsc,u16 retire)2433 static inline void __setup_pebs_basic_group(struct perf_event *event,
2434 					    struct pt_regs *regs,
2435 					    struct perf_sample_data *data,
2436 					    u64 sample_type, u64 ip,
2437 					    u64 tsc, u16 retire)
2438 {
2439 	/* The ip in basic is EventingIP */
2440 	set_linear_ip(regs, ip);
2441 	regs->flags |= PERF_EFLAGS_EXACT;
2442 	setup_pebs_time(event, data, tsc);
2443 
2444 	if (sample_type & PERF_SAMPLE_WEIGHT_STRUCT)
2445 		data->weight.var3_w = retire;
2446 }
2447 
__setup_pebs_gpr_group(struct perf_event * event,struct pt_regs * regs,struct pebs_gprs * gprs,u64 sample_type)2448 static inline void __setup_pebs_gpr_group(struct perf_event *event,
2449 					  struct pt_regs *regs,
2450 					  struct pebs_gprs *gprs,
2451 					  u64 sample_type)
2452 {
2453 	/*
2454 	 * Update flags with PEBS data. PERF_EFLAGS_EXACT must be set
2455 	 * in previous basic group handling.
2456 	 */
2457 	regs->flags = gprs->flags | PERF_EFLAGS_EXACT;
2458 
2459 	if (event->attr.precise_ip < 2) {
2460 		set_linear_ip(regs, gprs->ip);
2461 		regs->flags &= ~PERF_EFLAGS_EXACT;
2462 	} else if (regs->flags & X86_VM_MASK) {
2463 		regs->flags ^= (PERF_EFLAGS_VM | X86_VM_MASK);
2464 	}
2465 
2466 	if (sample_type & (PERF_SAMPLE_REGS_INTR | PERF_SAMPLE_REGS_USER))
2467 		adaptive_pebs_save_regs(regs, gprs);
2468 }
2469 
__setup_pebs_meminfo_group(struct perf_event * event,struct perf_sample_data * data,u64 sample_type,u64 latency,u16 instr_latency,u64 address,u64 aux,u64 tsx_tuning,u64 ax)2470 static inline void __setup_pebs_meminfo_group(struct perf_event *event,
2471 					      struct perf_sample_data *data,
2472 					      u64 sample_type, u64 latency,
2473 					      u16 instr_latency, u64 address,
2474 					      u64 aux, u64 tsx_tuning, u64 ax)
2475 {
2476 	if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) {
2477 		u64 tsx_latency = intel_get_tsx_weight(tsx_tuning);
2478 
2479 		data->weight.var2_w = instr_latency;
2480 
2481 		/*
2482 		 * Although meminfo::latency is defined as a u64,
2483 		 * only the lower 32 bits include the valid data
2484 		 * in practice on Ice Lake and earlier platforms.
2485 		 */
2486 		if (sample_type & PERF_SAMPLE_WEIGHT)
2487 			data->weight.full = latency ?: tsx_latency;
2488 		else
2489 			data->weight.var1_dw = (u32)latency ?: tsx_latency;
2490 
2491 		data->sample_flags |= PERF_SAMPLE_WEIGHT_TYPE;
2492 	}
2493 
2494 	if (sample_type & PERF_SAMPLE_DATA_SRC) {
2495 		data->data_src.val = get_data_src(event, aux);
2496 		data->sample_flags |= PERF_SAMPLE_DATA_SRC;
2497 	}
2498 
2499 	if (sample_type & PERF_SAMPLE_ADDR_TYPE) {
2500 		data->addr = address;
2501 		data->sample_flags |= PERF_SAMPLE_ADDR;
2502 	}
2503 
2504 	if (sample_type & PERF_SAMPLE_TRANSACTION) {
2505 		data->txn = intel_get_tsx_transaction(tsx_tuning, ax);
2506 		data->sample_flags |= PERF_SAMPLE_TRANSACTION;
2507 	}
2508 }
2509 
2510 /*
2511  * With adaptive PEBS the layout depends on what fields are configured.
2512  */
setup_pebs_adaptive_sample_data(struct perf_event * event,struct pt_regs * iregs,void * __pebs,struct perf_sample_data * data,struct pt_regs * regs)2513 static void setup_pebs_adaptive_sample_data(struct perf_event *event,
2514 					    struct pt_regs *iregs, void *__pebs,
2515 					    struct perf_sample_data *data,
2516 					    struct pt_regs *regs)
2517 {
2518 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2519 	u64 sample_type = event->attr.sample_type;
2520 	struct pebs_basic *basic = __pebs;
2521 	void *next_record = basic + 1;
2522 	struct pebs_meminfo *meminfo = NULL;
2523 	struct pebs_gprs *gprs = NULL;
2524 	struct x86_perf_regs *perf_regs;
2525 	u64 format_group;
2526 	u16 retire;
2527 
2528 	if (basic == NULL)
2529 		return;
2530 
2531 	perf_regs = container_of(regs, struct x86_perf_regs, regs);
2532 	perf_regs->xmm_regs = NULL;
2533 
2534 	format_group = basic->format_group;
2535 
2536 	__setup_perf_sample_data(event, iregs, data);
2537 
2538 	*regs = *iregs;
2539 
2540 	/* basic group */
2541 	retire = x86_pmu.flags & PMU_FL_RETIRE_LATENCY ?
2542 			basic->retire_latency : 0;
2543 	__setup_pebs_basic_group(event, regs, data, sample_type,
2544 				 basic->ip, basic->tsc, retire);
2545 
2546 	/*
2547 	 * The record for MEMINFO is in front of GP
2548 	 * But PERF_SAMPLE_TRANSACTION needs gprs->ax.
2549 	 * Save the pointer here but process later.
2550 	 */
2551 	if (format_group & PEBS_DATACFG_MEMINFO) {
2552 		meminfo = next_record;
2553 		next_record = meminfo + 1;
2554 	}
2555 
2556 	if (format_group & PEBS_DATACFG_GP) {
2557 		gprs = next_record;
2558 		next_record = gprs + 1;
2559 
2560 		__setup_pebs_gpr_group(event, regs, gprs, sample_type);
2561 	}
2562 
2563 	if (format_group & PEBS_DATACFG_MEMINFO) {
2564 		u64 latency = x86_pmu.flags & PMU_FL_INSTR_LATENCY ?
2565 				meminfo->cache_latency : meminfo->mem_latency;
2566 		u64 instr_latency = x86_pmu.flags & PMU_FL_INSTR_LATENCY ?
2567 				meminfo->instr_latency : 0;
2568 		u64 ax = gprs ? gprs->ax : 0;
2569 
2570 		__setup_pebs_meminfo_group(event, data, sample_type, latency,
2571 					   instr_latency, meminfo->address,
2572 					   meminfo->aux, meminfo->tsx_tuning,
2573 					   ax);
2574 	}
2575 
2576 	if (format_group & PEBS_DATACFG_XMMS) {
2577 		struct pebs_xmm *xmm = next_record;
2578 
2579 		next_record = xmm + 1;
2580 		perf_regs->xmm_regs = xmm->xmm;
2581 	}
2582 
2583 	if (format_group & PEBS_DATACFG_LBRS) {
2584 		struct lbr_entry *lbr = next_record;
2585 		int num_lbr = ((format_group >> PEBS_DATACFG_LBR_SHIFT)
2586 					& 0xff) + 1;
2587 		next_record = next_record + num_lbr * sizeof(struct lbr_entry);
2588 
2589 		if (has_branch_stack(event)) {
2590 			intel_pmu_store_pebs_lbrs(lbr);
2591 			intel_pmu_lbr_save_brstack(data, cpuc, event);
2592 		}
2593 	}
2594 
2595 	if (format_group & (PEBS_DATACFG_CNTR | PEBS_DATACFG_METRICS)) {
2596 		struct pebs_cntr_header *cntr = next_record;
2597 		unsigned int nr;
2598 
2599 		next_record += sizeof(struct pebs_cntr_header);
2600 		/*
2601 		 * The PEBS_DATA_CFG is a global register, which is the
2602 		 * superset configuration for all PEBS events.
2603 		 * For the PEBS record of non-sample-read group, ignore
2604 		 * the counter snapshot fields.
2605 		 */
2606 		if (is_pebs_counter_event_group(event)) {
2607 			__setup_pebs_counter_group(cpuc, event, cntr, next_record);
2608 			data->sample_flags |= PERF_SAMPLE_READ;
2609 		}
2610 
2611 		nr = hweight32(cntr->cntr) + hweight32(cntr->fixed);
2612 		if (cntr->metrics == INTEL_CNTR_METRICS)
2613 			nr += 2;
2614 		next_record += nr * sizeof(u64);
2615 	}
2616 
2617 	WARN_ONCE(next_record != __pebs + basic->format_size,
2618 			"PEBS record size %u, expected %llu, config %llx\n",
2619 			basic->format_size,
2620 			(u64)(next_record - __pebs),
2621 			format_group);
2622 }
2623 
arch_pebs_record_continued(struct arch_pebs_header * header)2624 static inline bool arch_pebs_record_continued(struct arch_pebs_header *header)
2625 {
2626 	/* Continue bit or null PEBS record indicates fragment follows. */
2627 	return header->cont || !(header->format & GENMASK_ULL(63, 16));
2628 }
2629 
setup_arch_pebs_sample_data(struct perf_event * event,struct pt_regs * iregs,void * __pebs,struct perf_sample_data * data,struct pt_regs * regs)2630 static void setup_arch_pebs_sample_data(struct perf_event *event,
2631 					struct pt_regs *iregs,
2632 					void *__pebs,
2633 					struct perf_sample_data *data,
2634 					struct pt_regs *regs)
2635 {
2636 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2637 	u64 sample_type = event->attr.sample_type;
2638 	struct arch_pebs_header *header = NULL;
2639 	struct arch_pebs_aux *meminfo = NULL;
2640 	struct arch_pebs_gprs *gprs = NULL;
2641 	struct x86_perf_regs *perf_regs;
2642 	void *next_record;
2643 	void *at = __pebs;
2644 
2645 	if (at == NULL)
2646 		return;
2647 
2648 	perf_regs = container_of(regs, struct x86_perf_regs, regs);
2649 	perf_regs->xmm_regs = NULL;
2650 
2651 	__setup_perf_sample_data(event, iregs, data);
2652 
2653 	*regs = *iregs;
2654 
2655 again:
2656 	header = at;
2657 	next_record = at + sizeof(struct arch_pebs_header);
2658 	if (header->basic) {
2659 		struct arch_pebs_basic *basic = next_record;
2660 		u16 retire = 0;
2661 
2662 		next_record = basic + 1;
2663 
2664 		if (sample_type & PERF_SAMPLE_WEIGHT_STRUCT)
2665 			retire = basic->valid ? basic->retire : 0;
2666 		__setup_pebs_basic_group(event, regs, data, sample_type,
2667 				 basic->ip, basic->tsc, retire);
2668 	}
2669 
2670 	/*
2671 	 * The record for MEMINFO is in front of GP
2672 	 * But PERF_SAMPLE_TRANSACTION needs gprs->ax.
2673 	 * Save the pointer here but process later.
2674 	 */
2675 	if (header->aux) {
2676 		meminfo = next_record;
2677 		next_record = meminfo + 1;
2678 	}
2679 
2680 	if (header->gpr) {
2681 		gprs = next_record;
2682 		next_record = gprs + 1;
2683 
2684 		__setup_pebs_gpr_group(event, regs,
2685 				       (struct pebs_gprs *)gprs,
2686 				       sample_type);
2687 	}
2688 
2689 	if (header->aux) {
2690 		u64 ax = gprs ? gprs->ax : 0;
2691 
2692 		__setup_pebs_meminfo_group(event, data, sample_type,
2693 					   meminfo->cache_latency,
2694 					   meminfo->instr_latency,
2695 					   meminfo->address, meminfo->aux,
2696 					   meminfo->tsx_tuning, ax);
2697 	}
2698 
2699 	if (header->xmm) {
2700 		struct pebs_xmm *xmm;
2701 
2702 		next_record += sizeof(struct arch_pebs_xer_header);
2703 
2704 		xmm = next_record;
2705 		perf_regs->xmm_regs = xmm->xmm;
2706 		next_record = xmm + 1;
2707 	}
2708 
2709 	if (header->lbr) {
2710 		struct arch_pebs_lbr_header *lbr_header = next_record;
2711 		struct lbr_entry *lbr;
2712 		int num_lbr;
2713 
2714 		next_record = lbr_header + 1;
2715 		lbr = next_record;
2716 
2717 		num_lbr = header->lbr == ARCH_PEBS_LBR_NUM_VAR ?
2718 				lbr_header->depth :
2719 				header->lbr * ARCH_PEBS_BASE_LBR_ENTRIES;
2720 		next_record += num_lbr * sizeof(struct lbr_entry);
2721 
2722 		if (has_branch_stack(event)) {
2723 			intel_pmu_store_pebs_lbrs(lbr);
2724 			intel_pmu_lbr_save_brstack(data, cpuc, event);
2725 		}
2726 	}
2727 
2728 	if (header->cntr) {
2729 		struct arch_pebs_cntr_header *cntr = next_record;
2730 		unsigned int nr;
2731 
2732 		next_record += sizeof(struct arch_pebs_cntr_header);
2733 
2734 		if (is_pebs_counter_event_group(event)) {
2735 			__setup_pebs_counter_group(cpuc, event,
2736 				(struct pebs_cntr_header *)cntr, next_record);
2737 			data->sample_flags |= PERF_SAMPLE_READ;
2738 		}
2739 
2740 		nr = hweight32(cntr->cntr) + hweight32(cntr->fixed);
2741 		if (cntr->metrics == INTEL_CNTR_METRICS)
2742 			nr += 2;
2743 		next_record += nr * sizeof(u64);
2744 	}
2745 
2746 	/* Parse followed fragments if there are. */
2747 	if (arch_pebs_record_continued(header)) {
2748 		at = at + header->size;
2749 		goto again;
2750 	}
2751 }
2752 
2753 static inline void *
get_next_pebs_record_by_bit(void * base,void * top,int bit)2754 get_next_pebs_record_by_bit(void *base, void *top, int bit)
2755 {
2756 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2757 	void *at;
2758 	u64 pebs_status;
2759 
2760 	/*
2761 	 * fmt0 does not have a status bitfield (does not use
2762 	 * perf_record_nhm format)
2763 	 */
2764 	if (x86_pmu.intel_cap.pebs_format < 1)
2765 		return base;
2766 
2767 	if (base == NULL)
2768 		return NULL;
2769 
2770 	for (at = base; at < top; at += cpuc->pebs_record_size) {
2771 		unsigned long status = get_pebs_status(at);
2772 
2773 		if (test_bit(bit, (unsigned long *)&status)) {
2774 			/* PEBS v3 has accurate status bits */
2775 			if (x86_pmu.intel_cap.pebs_format >= 3)
2776 				return at;
2777 
2778 			if (status == (1 << bit))
2779 				return at;
2780 
2781 			/* clear non-PEBS bit and re-check */
2782 			pebs_status = status & cpuc->pebs_enabled;
2783 			pebs_status &= PEBS_COUNTER_MASK;
2784 			if (pebs_status == (1 << bit))
2785 				return at;
2786 		}
2787 	}
2788 	return NULL;
2789 }
2790 
2791 /*
2792  * Special variant of intel_pmu_save_and_restart() for auto-reload.
2793  */
2794 static int
intel_pmu_save_and_restart_reload(struct perf_event * event,int count)2795 intel_pmu_save_and_restart_reload(struct perf_event *event, int count)
2796 {
2797 	struct hw_perf_event *hwc = &event->hw;
2798 	int shift = 64 - x86_pmu.cntval_bits;
2799 	u64 period = hwc->sample_period;
2800 	u64 prev_raw_count, new_raw_count;
2801 	s64 new, old;
2802 
2803 	WARN_ON(!period);
2804 
2805 	/*
2806 	 * drain_pebs() only happens when the PMU is disabled.
2807 	 */
2808 	WARN_ON(this_cpu_read(cpu_hw_events.enabled));
2809 
2810 	prev_raw_count = local64_read(&hwc->prev_count);
2811 	new_raw_count = rdpmc(hwc->event_base_rdpmc);
2812 	local64_set(&hwc->prev_count, new_raw_count);
2813 
2814 	/*
2815 	 * Since the counter increments a negative counter value and
2816 	 * overflows on the sign switch, giving the interval:
2817 	 *
2818 	 *   [-period, 0]
2819 	 *
2820 	 * the difference between two consecutive reads is:
2821 	 *
2822 	 *   A) value2 - value1;
2823 	 *      when no overflows have happened in between,
2824 	 *
2825 	 *   B) (0 - value1) + (value2 - (-period));
2826 	 *      when one overflow happened in between,
2827 	 *
2828 	 *   C) (0 - value1) + (n - 1) * (period) + (value2 - (-period));
2829 	 *      when @n overflows happened in between.
2830 	 *
2831 	 * Here A) is the obvious difference, B) is the extension to the
2832 	 * discrete interval, where the first term is to the top of the
2833 	 * interval and the second term is from the bottom of the next
2834 	 * interval and C) the extension to multiple intervals, where the
2835 	 * middle term is the whole intervals covered.
2836 	 *
2837 	 * An equivalent of C, by reduction, is:
2838 	 *
2839 	 *   value2 - value1 + n * period
2840 	 */
2841 	new = ((s64)(new_raw_count << shift) >> shift);
2842 	old = ((s64)(prev_raw_count << shift) >> shift);
2843 	local64_add(new - old + count * period, &event->count);
2844 
2845 	local64_set(&hwc->period_left, -new);
2846 
2847 	perf_event_update_userpage(event);
2848 
2849 	return 0;
2850 }
2851 
2852 typedef void (*setup_fn)(struct perf_event *, struct pt_regs *, void *,
2853 			 struct perf_sample_data *, struct pt_regs *);
2854 
2855 static struct pt_regs dummy_iregs;
2856 
2857 static __always_inline void
__intel_pmu_pebs_event(struct perf_event * event,struct pt_regs * iregs,struct pt_regs * regs,struct perf_sample_data * data,void * at,setup_fn setup_sample)2858 __intel_pmu_pebs_event(struct perf_event *event,
2859 		       struct pt_regs *iregs,
2860 		       struct pt_regs *regs,
2861 		       struct perf_sample_data *data,
2862 		       void *at,
2863 		       setup_fn setup_sample)
2864 {
2865 	setup_sample(event, iregs, at, data, regs);
2866 	perf_event_output(event, data, regs);
2867 }
2868 
2869 static __always_inline void
__intel_pmu_pebs_last_event(struct perf_event * event,struct pt_regs * iregs,struct pt_regs * regs,struct perf_sample_data * data,void * at,int count,setup_fn setup_sample)2870 __intel_pmu_pebs_last_event(struct perf_event *event,
2871 			    struct pt_regs *iregs,
2872 			    struct pt_regs *regs,
2873 			    struct perf_sample_data *data,
2874 			    void *at,
2875 			    int count,
2876 			    setup_fn setup_sample)
2877 {
2878 	struct hw_perf_event *hwc = &event->hw;
2879 
2880 	setup_sample(event, iregs, at, data, regs);
2881 	if (iregs == &dummy_iregs) {
2882 		/*
2883 		 * The PEBS records may be drained in the non-overflow context,
2884 		 * e.g., large PEBS + context switch. Perf should treat the
2885 		 * last record the same as other PEBS records, and doesn't
2886 		 * invoke the generic overflow handler.
2887 		 */
2888 		perf_event_output(event, data, regs);
2889 	} else {
2890 		/*
2891 		 * All but the last records are processed.
2892 		 * The last one is left to be able to call the overflow handler.
2893 		 */
2894 		perf_event_overflow(event, data, regs);
2895 	}
2896 
2897 	if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
2898 		if ((is_pebs_counter_event_group(event))) {
2899 			/*
2900 			 * The value of each sample has been updated when setup
2901 			 * the corresponding sample data.
2902 			 */
2903 			perf_event_update_userpage(event);
2904 		} else {
2905 			/*
2906 			 * Now, auto-reload is only enabled in fixed period mode.
2907 			 * The reload value is always hwc->sample_period.
2908 			 * May need to change it, if auto-reload is enabled in
2909 			 * freq mode later.
2910 			 */
2911 			intel_pmu_save_and_restart_reload(event, count);
2912 		}
2913 	} else {
2914 		/*
2915 		 * For a non-precise event, it's possible the
2916 		 * counters-snapshotting records a positive value for the
2917 		 * overflowed event. Then the HW auto-reload mechanism
2918 		 * reset the counter to 0 immediately, because the
2919 		 * pebs_event_reset is cleared if the PERF_X86_EVENT_AUTO_RELOAD
2920 		 * is not set. The counter backwards may be observed in a
2921 		 * PMI handler.
2922 		 *
2923 		 * Since the event value has been updated when processing the
2924 		 * counters-snapshotting record, only needs to set the new
2925 		 * period for the counter.
2926 		 */
2927 		if (is_pebs_counter_event_group(event))
2928 			static_call(x86_pmu_set_period)(event);
2929 		else
2930 			intel_pmu_save_and_restart(event);
2931 	}
2932 }
2933 
2934 static __always_inline void
__intel_pmu_pebs_events(struct perf_event * event,struct pt_regs * iregs,struct perf_sample_data * data,void * base,void * top,int bit,int count,setup_fn setup_sample)2935 __intel_pmu_pebs_events(struct perf_event *event,
2936 			struct pt_regs *iregs,
2937 			struct perf_sample_data *data,
2938 			void *base, void *top,
2939 			int bit, int count,
2940 			setup_fn setup_sample)
2941 {
2942 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2943 	struct x86_perf_regs perf_regs;
2944 	struct pt_regs *regs = &perf_regs.regs;
2945 	void *at = get_next_pebs_record_by_bit(base, top, bit);
2946 	int cnt = count;
2947 
2948 	if (!iregs)
2949 		iregs = &dummy_iregs;
2950 
2951 	while (cnt > 1) {
2952 		__intel_pmu_pebs_event(event, iregs, regs, data, at, setup_sample);
2953 		at += cpuc->pebs_record_size;
2954 		at = get_next_pebs_record_by_bit(at, top, bit);
2955 		cnt--;
2956 	}
2957 
2958 	__intel_pmu_pebs_last_event(event, iregs, regs, data, at, count, setup_sample);
2959 }
2960 
intel_pmu_drain_pebs_core(struct pt_regs * iregs,struct perf_sample_data * data)2961 static void intel_pmu_drain_pebs_core(struct pt_regs *iregs, struct perf_sample_data *data)
2962 {
2963 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2964 	struct debug_store *ds = cpuc->ds;
2965 	struct perf_event *event = cpuc->events[0]; /* PMC0 only */
2966 	struct pebs_record_core *at, *top;
2967 	int n;
2968 
2969 	if (!x86_pmu.pebs_active)
2970 		return;
2971 
2972 	at  = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base;
2973 	top = (struct pebs_record_core *)(unsigned long)ds->pebs_index;
2974 
2975 	/*
2976 	 * Whatever else happens, drain the thing
2977 	 */
2978 	ds->pebs_index = ds->pebs_buffer_base;
2979 
2980 	if (!test_bit(0, cpuc->active_mask))
2981 		return;
2982 
2983 	WARN_ON_ONCE(!event);
2984 
2985 	if (!event->attr.precise_ip)
2986 		return;
2987 
2988 	n = top - at;
2989 	if (n <= 0) {
2990 		if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)
2991 			intel_pmu_save_and_restart_reload(event, 0);
2992 		return;
2993 	}
2994 
2995 	__intel_pmu_pebs_events(event, iregs, data, at, top, 0, n,
2996 				setup_pebs_fixed_sample_data);
2997 }
2998 
intel_pmu_pebs_event_update_no_drain(struct cpu_hw_events * cpuc,u64 mask)2999 static void intel_pmu_pebs_event_update_no_drain(struct cpu_hw_events *cpuc, u64 mask)
3000 {
3001 	u64 pebs_enabled = cpuc->pebs_enabled & mask;
3002 	struct perf_event *event;
3003 	int bit;
3004 
3005 	/*
3006 	 * The drain_pebs() could be called twice in a short period
3007 	 * for auto-reload event in pmu::read(). There are no
3008 	 * overflows have happened in between.
3009 	 * It needs to call intel_pmu_save_and_restart_reload() to
3010 	 * update the event->count for this case.
3011 	 */
3012 	for_each_set_bit(bit, (unsigned long *)&pebs_enabled, X86_PMC_IDX_MAX) {
3013 		event = cpuc->events[bit];
3014 		if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)
3015 			intel_pmu_save_and_restart_reload(event, 0);
3016 	}
3017 }
3018 
intel_pmu_drain_pebs_nhm(struct pt_regs * iregs,struct perf_sample_data * data)3019 static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs, struct perf_sample_data *data)
3020 {
3021 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
3022 	struct debug_store *ds = cpuc->ds;
3023 	struct perf_event *event;
3024 	void *base, *at, *top;
3025 	short counts[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {};
3026 	short error[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {};
3027 	int max_pebs_events = intel_pmu_max_num_pebs(NULL);
3028 	int bit, i, size;
3029 	u64 mask;
3030 
3031 	if (!x86_pmu.pebs_active)
3032 		return;
3033 
3034 	base = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
3035 	top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
3036 
3037 	ds->pebs_index = ds->pebs_buffer_base;
3038 
3039 	mask = x86_pmu.pebs_events_mask;
3040 	size = max_pebs_events;
3041 	if (x86_pmu.flags & PMU_FL_PEBS_ALL) {
3042 		mask |= x86_pmu.fixed_cntr_mask64 << INTEL_PMC_IDX_FIXED;
3043 		size = INTEL_PMC_IDX_FIXED + x86_pmu_max_num_counters_fixed(NULL);
3044 	}
3045 
3046 	if (unlikely(base >= top)) {
3047 		intel_pmu_pebs_event_update_no_drain(cpuc, mask);
3048 		return;
3049 	}
3050 
3051 	for (at = base; at < top; at += x86_pmu.pebs_record_size) {
3052 		struct pebs_record_nhm *p = at;
3053 		u64 pebs_status;
3054 
3055 		pebs_status = p->status & cpuc->pebs_enabled;
3056 		pebs_status &= mask;
3057 
3058 		/* PEBS v3 has more accurate status bits */
3059 		if (x86_pmu.intel_cap.pebs_format >= 3) {
3060 			for_each_set_bit(bit, (unsigned long *)&pebs_status, size)
3061 				counts[bit]++;
3062 
3063 			continue;
3064 		}
3065 
3066 		/*
3067 		 * On some CPUs the PEBS status can be zero when PEBS is
3068 		 * racing with clearing of GLOBAL_STATUS.
3069 		 *
3070 		 * Normally we would drop that record, but in the
3071 		 * case when there is only a single active PEBS event
3072 		 * we can assume it's for that event.
3073 		 */
3074 		if (!pebs_status && cpuc->pebs_enabled &&
3075 			!(cpuc->pebs_enabled & (cpuc->pebs_enabled-1)))
3076 			pebs_status = p->status = cpuc->pebs_enabled;
3077 
3078 		bit = find_first_bit((unsigned long *)&pebs_status,
3079 				     max_pebs_events);
3080 
3081 		if (!(x86_pmu.pebs_events_mask & (1 << bit)))
3082 			continue;
3083 
3084 		/*
3085 		 * The PEBS hardware does not deal well with the situation
3086 		 * when events happen near to each other and multiple bits
3087 		 * are set. But it should happen rarely.
3088 		 *
3089 		 * If these events include one PEBS and multiple non-PEBS
3090 		 * events, it doesn't impact PEBS record. The record will
3091 		 * be handled normally. (slow path)
3092 		 *
3093 		 * If these events include two or more PEBS events, the
3094 		 * records for the events can be collapsed into a single
3095 		 * one, and it's not possible to reconstruct all events
3096 		 * that caused the PEBS record. It's called collision.
3097 		 * If collision happened, the record will be dropped.
3098 		 */
3099 		if (pebs_status != (1ULL << bit)) {
3100 			for_each_set_bit(i, (unsigned long *)&pebs_status, size)
3101 				error[i]++;
3102 			continue;
3103 		}
3104 
3105 		counts[bit]++;
3106 	}
3107 
3108 	for_each_set_bit(bit, (unsigned long *)&mask, size) {
3109 		if ((counts[bit] == 0) && (error[bit] == 0))
3110 			continue;
3111 
3112 		event = cpuc->events[bit];
3113 		if (WARN_ON_ONCE(!event))
3114 			continue;
3115 
3116 		if (WARN_ON_ONCE(!event->attr.precise_ip))
3117 			continue;
3118 
3119 		/* log dropped samples number */
3120 		if (error[bit]) {
3121 			perf_log_lost_samples(event, error[bit]);
3122 
3123 			if (iregs)
3124 				perf_event_account_interrupt(event);
3125 		}
3126 
3127 		if (counts[bit]) {
3128 			__intel_pmu_pebs_events(event, iregs, data, base,
3129 						top, bit, counts[bit],
3130 						setup_pebs_fixed_sample_data);
3131 		}
3132 	}
3133 }
3134 
3135 static __always_inline void
__intel_pmu_handle_pebs_record(struct pt_regs * iregs,struct pt_regs * regs,struct perf_sample_data * data,void * at,u64 pebs_status,short * counts,void ** last,setup_fn setup_sample)3136 __intel_pmu_handle_pebs_record(struct pt_regs *iregs,
3137 			       struct pt_regs *regs,
3138 			       struct perf_sample_data *data,
3139 			       void *at, u64 pebs_status,
3140 			       short *counts, void **last,
3141 			       setup_fn setup_sample)
3142 {
3143 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
3144 	struct perf_event *event;
3145 	int bit;
3146 
3147 	for_each_set_bit(bit, (unsigned long *)&pebs_status, X86_PMC_IDX_MAX) {
3148 		event = cpuc->events[bit];
3149 
3150 		if (WARN_ON_ONCE(!event) ||
3151 		    WARN_ON_ONCE(!event->attr.precise_ip))
3152 			continue;
3153 
3154 		if (counts[bit]++) {
3155 			__intel_pmu_pebs_event(event, iregs, regs, data,
3156 					       last[bit], setup_sample);
3157 		}
3158 
3159 		last[bit] = at;
3160 	}
3161 }
3162 
3163 static __always_inline void
__intel_pmu_handle_last_pebs_record(struct pt_regs * iregs,struct pt_regs * regs,struct perf_sample_data * data,u64 mask,short * counts,void ** last,setup_fn setup_sample)3164 __intel_pmu_handle_last_pebs_record(struct pt_regs *iregs,
3165 				    struct pt_regs *regs,
3166 				    struct perf_sample_data *data,
3167 				    u64 mask, short *counts, void **last,
3168 				    setup_fn setup_sample)
3169 {
3170 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
3171 	struct perf_event *event;
3172 	int bit;
3173 
3174 	for_each_set_bit(bit, (unsigned long *)&mask, X86_PMC_IDX_MAX) {
3175 		if (!counts[bit])
3176 			continue;
3177 
3178 		event = cpuc->events[bit];
3179 
3180 		__intel_pmu_pebs_last_event(event, iregs, regs, data, last[bit],
3181 					    counts[bit], setup_sample);
3182 	}
3183 
3184 }
3185 
intel_pmu_drain_pebs_icl(struct pt_regs * iregs,struct perf_sample_data * data)3186 static void intel_pmu_drain_pebs_icl(struct pt_regs *iregs, struct perf_sample_data *data)
3187 {
3188 	short counts[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {};
3189 	void *last[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS];
3190 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
3191 	struct debug_store *ds = cpuc->ds;
3192 	struct x86_perf_regs perf_regs;
3193 	struct pt_regs *regs = &perf_regs.regs;
3194 	struct pebs_basic *basic;
3195 	void *base, *at, *top;
3196 	u64 mask;
3197 
3198 	if (!x86_pmu.pebs_active)
3199 		return;
3200 
3201 	base = (struct pebs_basic *)(unsigned long)ds->pebs_buffer_base;
3202 	top = (struct pebs_basic *)(unsigned long)ds->pebs_index;
3203 
3204 	ds->pebs_index = ds->pebs_buffer_base;
3205 
3206 	mask = hybrid(cpuc->pmu, pebs_events_mask) |
3207 	       (hybrid(cpuc->pmu, fixed_cntr_mask64) << INTEL_PMC_IDX_FIXED);
3208 	mask &= cpuc->pebs_enabled;
3209 
3210 	if (unlikely(base >= top)) {
3211 		intel_pmu_pebs_event_update_no_drain(cpuc, mask);
3212 		return;
3213 	}
3214 
3215 	if (!iregs)
3216 		iregs = &dummy_iregs;
3217 
3218 	/* Process all but the last event for each counter. */
3219 	for (at = base; at < top; at += basic->format_size) {
3220 		u64 pebs_status;
3221 
3222 		basic = at;
3223 		if (basic->format_size != cpuc->pebs_record_size)
3224 			continue;
3225 
3226 		pebs_status = mask & basic->applicable_counters;
3227 		__intel_pmu_handle_pebs_record(iregs, regs, data, at,
3228 					       pebs_status, counts, last,
3229 					       setup_pebs_adaptive_sample_data);
3230 	}
3231 
3232 	__intel_pmu_handle_last_pebs_record(iregs, regs, data, mask, counts, last,
3233 					    setup_pebs_adaptive_sample_data);
3234 }
3235 
intel_pmu_drain_arch_pebs(struct pt_regs * iregs,struct perf_sample_data * data)3236 static void intel_pmu_drain_arch_pebs(struct pt_regs *iregs,
3237 				      struct perf_sample_data *data)
3238 {
3239 	short counts[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {};
3240 	void *last[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS];
3241 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
3242 	union arch_pebs_index index;
3243 	struct x86_perf_regs perf_regs;
3244 	struct pt_regs *regs = &perf_regs.regs;
3245 	void *base, *at, *top;
3246 	u64 mask;
3247 
3248 	rdmsrq(MSR_IA32_PEBS_INDEX, index.whole);
3249 
3250 	if (unlikely(!index.wr)) {
3251 		intel_pmu_pebs_event_update_no_drain(cpuc, X86_PMC_IDX_MAX);
3252 		return;
3253 	}
3254 
3255 	base = cpuc->pebs_vaddr;
3256 	top = cpuc->pebs_vaddr + (index.wr << ARCH_PEBS_INDEX_WR_SHIFT);
3257 
3258 	index.wr = 0;
3259 	index.full = 0;
3260 	index.en = 1;
3261 	if (cpuc->n_pebs == cpuc->n_large_pebs)
3262 		index.thresh = ARCH_PEBS_THRESH_MULTI;
3263 	else
3264 		index.thresh = ARCH_PEBS_THRESH_SINGLE;
3265 	wrmsrq(MSR_IA32_PEBS_INDEX, index.whole);
3266 
3267 	mask = hybrid(cpuc->pmu, arch_pebs_cap).counters & cpuc->pebs_enabled;
3268 
3269 	if (!iregs)
3270 		iregs = &dummy_iregs;
3271 
3272 	/* Process all but the last event for each counter. */
3273 	for (at = base; at < top;) {
3274 		struct arch_pebs_header *header;
3275 		struct arch_pebs_basic *basic;
3276 		u64 pebs_status;
3277 
3278 		header = at;
3279 
3280 		if (WARN_ON_ONCE(!header->size))
3281 			break;
3282 
3283 		/* 1st fragment or single record must have basic group */
3284 		if (!header->basic) {
3285 			at += header->size;
3286 			continue;
3287 		}
3288 
3289 		basic = at + sizeof(struct arch_pebs_header);
3290 		pebs_status = mask & basic->applicable_counters;
3291 		__intel_pmu_handle_pebs_record(iregs, regs, data, at,
3292 					       pebs_status, counts, last,
3293 					       setup_arch_pebs_sample_data);
3294 
3295 		/* Skip non-last fragments */
3296 		while (arch_pebs_record_continued(header)) {
3297 			if (!header->size)
3298 				break;
3299 			at += header->size;
3300 			header = at;
3301 		}
3302 
3303 		/* Skip last fragment or the single record */
3304 		at += header->size;
3305 	}
3306 
3307 	__intel_pmu_handle_last_pebs_record(iregs, regs, data, mask,
3308 					    counts, last,
3309 					    setup_arch_pebs_sample_data);
3310 }
3311 
intel_arch_pebs_init(void)3312 static void __init intel_arch_pebs_init(void)
3313 {
3314 	/*
3315 	 * Current hybrid platforms always both support arch-PEBS or not
3316 	 * on all kinds of cores. So directly set x86_pmu.arch_pebs flag
3317 	 * if boot cpu supports arch-PEBS.
3318 	 */
3319 	x86_pmu.arch_pebs = 1;
3320 	x86_pmu.pebs_buffer_size = PEBS_BUFFER_SIZE;
3321 	x86_pmu.drain_pebs = intel_pmu_drain_arch_pebs;
3322 	x86_pmu.pebs_capable = ~0ULL;
3323 	x86_pmu.flags |= PMU_FL_PEBS_ALL;
3324 
3325 	x86_pmu.pebs_enable = __intel_pmu_pebs_enable;
3326 	x86_pmu.pebs_disable = __intel_pmu_pebs_disable;
3327 }
3328 
3329 /*
3330  * PEBS probe and setup
3331  */
3332 
intel_ds_pebs_init(void)3333 static void __init intel_ds_pebs_init(void)
3334 {
3335 	/*
3336 	 * No support for 32bit formats
3337 	 */
3338 	if (!boot_cpu_has(X86_FEATURE_DTES64))
3339 		return;
3340 
3341 	x86_pmu.ds_pebs = boot_cpu_has(X86_FEATURE_PEBS);
3342 	x86_pmu.pebs_buffer_size = PEBS_BUFFER_SIZE;
3343 	if (x86_pmu.version <= 4)
3344 		x86_pmu.pebs_no_isolation = 1;
3345 
3346 	if (x86_pmu.ds_pebs) {
3347 		char pebs_type = x86_pmu.intel_cap.pebs_trap ?  '+' : '-';
3348 		char *pebs_qual = "";
3349 		int format = x86_pmu.intel_cap.pebs_format;
3350 
3351 		if (format < 4)
3352 			x86_pmu.intel_cap.pebs_baseline = 0;
3353 
3354 		x86_pmu.pebs_enable = intel_pmu_pebs_enable;
3355 		x86_pmu.pebs_disable = intel_pmu_pebs_disable;
3356 		x86_pmu.pebs_enable_all = intel_pmu_pebs_enable_all;
3357 		x86_pmu.pebs_disable_all = intel_pmu_pebs_disable_all;
3358 
3359 		switch (format) {
3360 		case 0:
3361 			pr_cont("PEBS fmt0%c, ", pebs_type);
3362 			x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
3363 			/*
3364 			 * Using >PAGE_SIZE buffers makes the WRMSR to
3365 			 * PERF_GLOBAL_CTRL in intel_pmu_enable_all()
3366 			 * mysteriously hang on Core2.
3367 			 *
3368 			 * As a workaround, we don't do this.
3369 			 */
3370 			x86_pmu.pebs_buffer_size = PAGE_SIZE;
3371 			x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
3372 			break;
3373 
3374 		case 1:
3375 			pr_cont("PEBS fmt1%c, ", pebs_type);
3376 			x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
3377 			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
3378 			break;
3379 
3380 		case 2:
3381 			pr_cont("PEBS fmt2%c, ", pebs_type);
3382 			x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw);
3383 			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
3384 			break;
3385 
3386 		case 3:
3387 			pr_cont("PEBS fmt3%c, ", pebs_type);
3388 			x86_pmu.pebs_record_size =
3389 						sizeof(struct pebs_record_skl);
3390 			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
3391 			x86_pmu.large_pebs_flags |= PERF_SAMPLE_TIME;
3392 			break;
3393 
3394 		case 6:
3395 			if (x86_pmu.intel_cap.pebs_baseline)
3396 				x86_pmu.large_pebs_flags |= PERF_SAMPLE_READ;
3397 			fallthrough;
3398 		case 5:
3399 			x86_pmu.pebs_ept = 1;
3400 			fallthrough;
3401 		case 4:
3402 			x86_pmu.drain_pebs = intel_pmu_drain_pebs_icl;
3403 			x86_pmu.pebs_record_size = sizeof(struct pebs_basic);
3404 			if (x86_pmu.intel_cap.pebs_baseline) {
3405 				x86_pmu.large_pebs_flags |=
3406 					PERF_SAMPLE_BRANCH_STACK |
3407 					PERF_SAMPLE_TIME;
3408 				x86_pmu.flags |= PMU_FL_PEBS_ALL;
3409 				x86_pmu.pebs_capable = ~0ULL;
3410 				pebs_qual = "-baseline";
3411 				x86_get_pmu(smp_processor_id())->capabilities |= PERF_PMU_CAP_EXTENDED_REGS;
3412 			} else {
3413 				/* Only basic record supported */
3414 				x86_pmu.large_pebs_flags &=
3415 					~(PERF_SAMPLE_ADDR |
3416 					  PERF_SAMPLE_TIME |
3417 					  PERF_SAMPLE_DATA_SRC |
3418 					  PERF_SAMPLE_TRANSACTION |
3419 					  PERF_SAMPLE_REGS_USER |
3420 					  PERF_SAMPLE_REGS_INTR);
3421 			}
3422 			pr_cont("PEBS fmt%d%c%s, ", format, pebs_type, pebs_qual);
3423 
3424 			/*
3425 			 * The PEBS-via-PT is not supported on hybrid platforms,
3426 			 * because not all CPUs of a hybrid machine support it.
3427 			 * The global x86_pmu.intel_cap, which only contains the
3428 			 * common capabilities, is used to check the availability
3429 			 * of the feature. The per-PMU pebs_output_pt_available
3430 			 * in a hybrid machine should be ignored.
3431 			 */
3432 			if (x86_pmu.intel_cap.pebs_output_pt_available) {
3433 				pr_cont("PEBS-via-PT, ");
3434 				x86_get_pmu(smp_processor_id())->capabilities |= PERF_PMU_CAP_AUX_OUTPUT;
3435 			}
3436 
3437 			break;
3438 
3439 		default:
3440 			pr_cont("no PEBS fmt%d%c, ", format, pebs_type);
3441 			x86_pmu.ds_pebs = 0;
3442 		}
3443 	}
3444 }
3445 
intel_pebs_init(void)3446 void __init intel_pebs_init(void)
3447 {
3448 	if (x86_pmu.intel_cap.pebs_format == 0xf)
3449 		intel_arch_pebs_init();
3450 	else
3451 		intel_ds_pebs_init();
3452 }
3453 
perf_restore_debug_store(void)3454 void perf_restore_debug_store(void)
3455 {
3456 	struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
3457 
3458 	if (!x86_pmu.bts && !x86_pmu.ds_pebs)
3459 		return;
3460 
3461 	wrmsrq(MSR_IA32_DS_AREA, (unsigned long)ds);
3462 }
3463