1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/perf_event.h>
3 #include <linux/types.h>
4 
5 #include <asm/cpu_device_id.h>
6 #include <asm/perf_event.h>
7 #include <asm/msr.h>
8 
9 #include "../perf_event.h"
10 
11 /*
12  * Intel LBR_SELECT bits
13  * Intel Vol3a, April 2011, Section 16.7 Table 16-10
14  *
15  * Hardware branch filter (not available on all CPUs)
16  */
17 #define LBR_KERNEL_BIT		0 /* do not capture at ring0 */
18 #define LBR_USER_BIT		1 /* do not capture at ring > 0 */
19 #define LBR_JCC_BIT		2 /* do not capture conditional branches */
20 #define LBR_REL_CALL_BIT	3 /* do not capture relative calls */
21 #define LBR_IND_CALL_BIT	4 /* do not capture indirect calls */
22 #define LBR_RETURN_BIT		5 /* do not capture near returns */
23 #define LBR_IND_JMP_BIT		6 /* do not capture indirect jumps */
24 #define LBR_REL_JMP_BIT		7 /* do not capture relative jumps */
25 #define LBR_FAR_BIT		8 /* do not capture far branches */
26 #define LBR_CALL_STACK_BIT	9 /* enable call stack */
27 
28 /*
29  * Following bit only exists in Linux; we mask it out before writing it to
30  * the actual MSR. But it helps the constraint perf code to understand
31  * that this is a separate configuration.
32  */
33 #define LBR_NO_INFO_BIT	       63 /* don't read LBR_INFO. */
34 
35 #define LBR_KERNEL	(1 << LBR_KERNEL_BIT)
36 #define LBR_USER	(1 << LBR_USER_BIT)
37 #define LBR_JCC		(1 << LBR_JCC_BIT)
38 #define LBR_REL_CALL	(1 << LBR_REL_CALL_BIT)
39 #define LBR_IND_CALL	(1 << LBR_IND_CALL_BIT)
40 #define LBR_RETURN	(1 << LBR_RETURN_BIT)
41 #define LBR_REL_JMP	(1 << LBR_REL_JMP_BIT)
42 #define LBR_IND_JMP	(1 << LBR_IND_JMP_BIT)
43 #define LBR_FAR		(1 << LBR_FAR_BIT)
44 #define LBR_CALL_STACK	(1 << LBR_CALL_STACK_BIT)
45 #define LBR_NO_INFO	(1ULL << LBR_NO_INFO_BIT)
46 
47 #define LBR_PLM (LBR_KERNEL | LBR_USER)
48 
49 #define LBR_SEL_MASK	0x3ff	/* valid bits in LBR_SELECT */
50 #define LBR_NOT_SUPP	-1	/* LBR filter not supported */
51 #define LBR_IGN		0	/* ignored */
52 
53 #define LBR_ANY		 \
54 	(LBR_JCC	|\
55 	 LBR_REL_CALL	|\
56 	 LBR_IND_CALL	|\
57 	 LBR_RETURN	|\
58 	 LBR_REL_JMP	|\
59 	 LBR_IND_JMP	|\
60 	 LBR_FAR)
61 
62 #define LBR_FROM_FLAG_MISPRED	BIT_ULL(63)
63 #define LBR_FROM_FLAG_IN_TX	BIT_ULL(62)
64 #define LBR_FROM_FLAG_ABORT	BIT_ULL(61)
65 
66 #define LBR_FROM_SIGNEXT_2MSB	(BIT_ULL(60) | BIT_ULL(59))
67 
68 /*
69  * Intel LBR_CTL bits
70  *
71  * Hardware branch filter for Arch LBR
72  */
73 #define ARCH_LBR_KERNEL_BIT		1  /* capture at ring0 */
74 #define ARCH_LBR_USER_BIT		2  /* capture at ring > 0 */
75 #define ARCH_LBR_CALL_STACK_BIT		3  /* enable call stack */
76 #define ARCH_LBR_JCC_BIT		16 /* capture conditional branches */
77 #define ARCH_LBR_REL_JMP_BIT		17 /* capture relative jumps */
78 #define ARCH_LBR_IND_JMP_BIT		18 /* capture indirect jumps */
79 #define ARCH_LBR_REL_CALL_BIT		19 /* capture relative calls */
80 #define ARCH_LBR_IND_CALL_BIT		20 /* capture indirect calls */
81 #define ARCH_LBR_RETURN_BIT		21 /* capture near returns */
82 #define ARCH_LBR_OTHER_BRANCH_BIT	22 /* capture other branches */
83 
84 #define ARCH_LBR_KERNEL			(1ULL << ARCH_LBR_KERNEL_BIT)
85 #define ARCH_LBR_USER			(1ULL << ARCH_LBR_USER_BIT)
86 #define ARCH_LBR_CALL_STACK		(1ULL << ARCH_LBR_CALL_STACK_BIT)
87 #define ARCH_LBR_JCC			(1ULL << ARCH_LBR_JCC_BIT)
88 #define ARCH_LBR_REL_JMP		(1ULL << ARCH_LBR_REL_JMP_BIT)
89 #define ARCH_LBR_IND_JMP		(1ULL << ARCH_LBR_IND_JMP_BIT)
90 #define ARCH_LBR_REL_CALL		(1ULL << ARCH_LBR_REL_CALL_BIT)
91 #define ARCH_LBR_IND_CALL		(1ULL << ARCH_LBR_IND_CALL_BIT)
92 #define ARCH_LBR_RETURN			(1ULL << ARCH_LBR_RETURN_BIT)
93 #define ARCH_LBR_OTHER_BRANCH		(1ULL << ARCH_LBR_OTHER_BRANCH_BIT)
94 
95 #define ARCH_LBR_ANY			 \
96 	(ARCH_LBR_JCC			|\
97 	 ARCH_LBR_REL_JMP		|\
98 	 ARCH_LBR_IND_JMP		|\
99 	 ARCH_LBR_REL_CALL		|\
100 	 ARCH_LBR_IND_CALL		|\
101 	 ARCH_LBR_RETURN		|\
102 	 ARCH_LBR_OTHER_BRANCH)
103 
104 #define ARCH_LBR_CTL_MASK			0x7f000e
105 
106 static void intel_pmu_lbr_filter(struct cpu_hw_events *cpuc);
107 
is_lbr_call_stack_bit_set(u64 config)108 static __always_inline bool is_lbr_call_stack_bit_set(u64 config)
109 {
110 	if (static_cpu_has(X86_FEATURE_ARCH_LBR))
111 		return !!(config & ARCH_LBR_CALL_STACK);
112 
113 	return !!(config & LBR_CALL_STACK);
114 }
115 
116 /*
117  * We only support LBR implementations that have FREEZE_LBRS_ON_PMI
118  * otherwise it becomes near impossible to get a reliable stack.
119  */
120 
__intel_pmu_lbr_enable(bool pmi)121 static void __intel_pmu_lbr_enable(bool pmi)
122 {
123 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
124 	u64 debugctl, lbr_select = 0, orig_debugctl;
125 
126 	/*
127 	 * No need to unfreeze manually, as v4 can do that as part
128 	 * of the GLOBAL_STATUS ack.
129 	 */
130 	if (pmi && x86_pmu.version >= 4)
131 		return;
132 
133 	/*
134 	 * No need to reprogram LBR_SELECT in a PMI, as it
135 	 * did not change.
136 	 */
137 	if (cpuc->lbr_sel)
138 		lbr_select = cpuc->lbr_sel->config & x86_pmu.lbr_sel_mask;
139 	if (!static_cpu_has(X86_FEATURE_ARCH_LBR) && !pmi && cpuc->lbr_sel)
140 		wrmsrl(MSR_LBR_SELECT, lbr_select);
141 
142 	rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
143 	orig_debugctl = debugctl;
144 
145 	if (!static_cpu_has(X86_FEATURE_ARCH_LBR))
146 		debugctl |= DEBUGCTLMSR_LBR;
147 	/*
148 	 * LBR callstack does not work well with FREEZE_LBRS_ON_PMI.
149 	 * If FREEZE_LBRS_ON_PMI is set, PMI near call/return instructions
150 	 * may cause superfluous increase/decrease of LBR_TOS.
151 	 */
152 	if (is_lbr_call_stack_bit_set(lbr_select))
153 		debugctl &= ~DEBUGCTLMSR_FREEZE_LBRS_ON_PMI;
154 	else
155 		debugctl |= DEBUGCTLMSR_FREEZE_LBRS_ON_PMI;
156 
157 	if (orig_debugctl != debugctl)
158 		wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
159 
160 	if (static_cpu_has(X86_FEATURE_ARCH_LBR))
161 		wrmsrl(MSR_ARCH_LBR_CTL, lbr_select | ARCH_LBR_CTL_LBREN);
162 }
163 
intel_pmu_lbr_reset_32(void)164 void intel_pmu_lbr_reset_32(void)
165 {
166 	int i;
167 
168 	for (i = 0; i < x86_pmu.lbr_nr; i++)
169 		wrmsrl(x86_pmu.lbr_from + i, 0);
170 }
171 
intel_pmu_lbr_reset_64(void)172 void intel_pmu_lbr_reset_64(void)
173 {
174 	int i;
175 
176 	for (i = 0; i < x86_pmu.lbr_nr; i++) {
177 		wrmsrl(x86_pmu.lbr_from + i, 0);
178 		wrmsrl(x86_pmu.lbr_to   + i, 0);
179 		if (x86_pmu.lbr_has_info)
180 			wrmsrl(x86_pmu.lbr_info + i, 0);
181 	}
182 }
183 
intel_pmu_arch_lbr_reset(void)184 static void intel_pmu_arch_lbr_reset(void)
185 {
186 	/* Write to ARCH_LBR_DEPTH MSR, all LBR entries are reset to 0 */
187 	wrmsrl(MSR_ARCH_LBR_DEPTH, x86_pmu.lbr_nr);
188 }
189 
intel_pmu_lbr_reset(void)190 void intel_pmu_lbr_reset(void)
191 {
192 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
193 
194 	if (!x86_pmu.lbr_nr)
195 		return;
196 
197 	x86_pmu.lbr_reset();
198 
199 	cpuc->last_task_ctx = NULL;
200 	cpuc->last_log_id = 0;
201 	if (!static_cpu_has(X86_FEATURE_ARCH_LBR) && cpuc->lbr_select)
202 		wrmsrl(MSR_LBR_SELECT, 0);
203 }
204 
205 /*
206  * TOS = most recently recorded branch
207  */
intel_pmu_lbr_tos(void)208 static inline u64 intel_pmu_lbr_tos(void)
209 {
210 	u64 tos;
211 
212 	rdmsrl(x86_pmu.lbr_tos, tos);
213 	return tos;
214 }
215 
216 enum {
217 	LBR_NONE,
218 	LBR_VALID,
219 };
220 
221 /*
222  * For format LBR_FORMAT_EIP_FLAGS2, bits 61:62 in MSR_LAST_BRANCH_FROM_x
223  * are the TSX flags when TSX is supported, but when TSX is not supported
224  * they have no consistent behavior:
225  *
226  *   - For wrmsr(), bits 61:62 are considered part of the sign extension.
227  *   - For HW updates (branch captures) bits 61:62 are always OFF and are not
228  *     part of the sign extension.
229  *
230  * Therefore, if:
231  *
232  *   1) LBR format LBR_FORMAT_EIP_FLAGS2
233  *   2) CPU has no TSX support enabled
234  *
235  * ... then any value passed to wrmsr() must be sign extended to 63 bits and any
236  * value from rdmsr() must be converted to have a 61 bits sign extension,
237  * ignoring the TSX flags.
238  */
lbr_from_signext_quirk_needed(void)239 static inline bool lbr_from_signext_quirk_needed(void)
240 {
241 	bool tsx_support = boot_cpu_has(X86_FEATURE_HLE) ||
242 			   boot_cpu_has(X86_FEATURE_RTM);
243 
244 	return !tsx_support;
245 }
246 
247 static DEFINE_STATIC_KEY_FALSE(lbr_from_quirk_key);
248 
249 /* If quirk is enabled, ensure sign extension is 63 bits: */
lbr_from_signext_quirk_wr(u64 val)250 inline u64 lbr_from_signext_quirk_wr(u64 val)
251 {
252 	if (static_branch_unlikely(&lbr_from_quirk_key)) {
253 		/*
254 		 * Sign extend into bits 61:62 while preserving bit 63.
255 		 *
256 		 * Quirk is enabled when TSX is disabled. Therefore TSX bits
257 		 * in val are always OFF and must be changed to be sign
258 		 * extension bits. Since bits 59:60 are guaranteed to be
259 		 * part of the sign extension bits, we can just copy them
260 		 * to 61:62.
261 		 */
262 		val |= (LBR_FROM_SIGNEXT_2MSB & val) << 2;
263 	}
264 	return val;
265 }
266 
267 /*
268  * If quirk is needed, ensure sign extension is 61 bits:
269  */
lbr_from_signext_quirk_rd(u64 val)270 static u64 lbr_from_signext_quirk_rd(u64 val)
271 {
272 	if (static_branch_unlikely(&lbr_from_quirk_key)) {
273 		/*
274 		 * Quirk is on when TSX is not enabled. Therefore TSX
275 		 * flags must be read as OFF.
276 		 */
277 		val &= ~(LBR_FROM_FLAG_IN_TX | LBR_FROM_FLAG_ABORT);
278 	}
279 	return val;
280 }
281 
wrlbr_from(unsigned int idx,u64 val)282 static __always_inline void wrlbr_from(unsigned int idx, u64 val)
283 {
284 	val = lbr_from_signext_quirk_wr(val);
285 	wrmsrl(x86_pmu.lbr_from + idx, val);
286 }
287 
wrlbr_to(unsigned int idx,u64 val)288 static __always_inline void wrlbr_to(unsigned int idx, u64 val)
289 {
290 	wrmsrl(x86_pmu.lbr_to + idx, val);
291 }
292 
wrlbr_info(unsigned int idx,u64 val)293 static __always_inline void wrlbr_info(unsigned int idx, u64 val)
294 {
295 	wrmsrl(x86_pmu.lbr_info + idx, val);
296 }
297 
rdlbr_from(unsigned int idx,struct lbr_entry * lbr)298 static __always_inline u64 rdlbr_from(unsigned int idx, struct lbr_entry *lbr)
299 {
300 	u64 val;
301 
302 	if (lbr)
303 		return lbr->from;
304 
305 	rdmsrl(x86_pmu.lbr_from + idx, val);
306 
307 	return lbr_from_signext_quirk_rd(val);
308 }
309 
rdlbr_to(unsigned int idx,struct lbr_entry * lbr)310 static __always_inline u64 rdlbr_to(unsigned int idx, struct lbr_entry *lbr)
311 {
312 	u64 val;
313 
314 	if (lbr)
315 		return lbr->to;
316 
317 	rdmsrl(x86_pmu.lbr_to + idx, val);
318 
319 	return val;
320 }
321 
rdlbr_info(unsigned int idx,struct lbr_entry * lbr)322 static __always_inline u64 rdlbr_info(unsigned int idx, struct lbr_entry *lbr)
323 {
324 	u64 val;
325 
326 	if (lbr)
327 		return lbr->info;
328 
329 	rdmsrl(x86_pmu.lbr_info + idx, val);
330 
331 	return val;
332 }
333 
334 static inline void
wrlbr_all(struct lbr_entry * lbr,unsigned int idx,bool need_info)335 wrlbr_all(struct lbr_entry *lbr, unsigned int idx, bool need_info)
336 {
337 	wrlbr_from(idx, lbr->from);
338 	wrlbr_to(idx, lbr->to);
339 	if (need_info)
340 		wrlbr_info(idx, lbr->info);
341 }
342 
343 static inline bool
rdlbr_all(struct lbr_entry * lbr,unsigned int idx,bool need_info)344 rdlbr_all(struct lbr_entry *lbr, unsigned int idx, bool need_info)
345 {
346 	u64 from = rdlbr_from(idx, NULL);
347 
348 	/* Don't read invalid entry */
349 	if (!from)
350 		return false;
351 
352 	lbr->from = from;
353 	lbr->to = rdlbr_to(idx, NULL);
354 	if (need_info)
355 		lbr->info = rdlbr_info(idx, NULL);
356 
357 	return true;
358 }
359 
intel_pmu_lbr_restore(void * ctx)360 void intel_pmu_lbr_restore(void *ctx)
361 {
362 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
363 	struct x86_perf_task_context *task_ctx = ctx;
364 	bool need_info = x86_pmu.lbr_has_info;
365 	u64 tos = task_ctx->tos;
366 	unsigned lbr_idx, mask;
367 	int i;
368 
369 	mask = x86_pmu.lbr_nr - 1;
370 	for (i = 0; i < task_ctx->valid_lbrs; i++) {
371 		lbr_idx = (tos - i) & mask;
372 		wrlbr_all(&task_ctx->lbr[i], lbr_idx, need_info);
373 	}
374 
375 	for (; i < x86_pmu.lbr_nr; i++) {
376 		lbr_idx = (tos - i) & mask;
377 		wrlbr_from(lbr_idx, 0);
378 		wrlbr_to(lbr_idx, 0);
379 		if (need_info)
380 			wrlbr_info(lbr_idx, 0);
381 	}
382 
383 	wrmsrl(x86_pmu.lbr_tos, tos);
384 
385 	if (cpuc->lbr_select)
386 		wrmsrl(MSR_LBR_SELECT, task_ctx->lbr_sel);
387 }
388 
intel_pmu_arch_lbr_restore(void * ctx)389 static void intel_pmu_arch_lbr_restore(void *ctx)
390 {
391 	struct x86_perf_task_context_arch_lbr *task_ctx = ctx;
392 	struct lbr_entry *entries = task_ctx->entries;
393 	int i;
394 
395 	/* Fast reset the LBRs before restore if the call stack is not full. */
396 	if (!entries[x86_pmu.lbr_nr - 1].from)
397 		intel_pmu_arch_lbr_reset();
398 
399 	for (i = 0; i < x86_pmu.lbr_nr; i++) {
400 		if (!entries[i].from)
401 			break;
402 		wrlbr_all(&entries[i], i, true);
403 	}
404 }
405 
406 /*
407  * Restore the Architecture LBR state from the xsave area in the perf
408  * context data for the task via the XRSTORS instruction.
409  */
intel_pmu_arch_lbr_xrstors(void * ctx)410 static void intel_pmu_arch_lbr_xrstors(void *ctx)
411 {
412 	struct x86_perf_task_context_arch_lbr_xsave *task_ctx = ctx;
413 
414 	xrstors(&task_ctx->xsave, XFEATURE_MASK_LBR);
415 }
416 
lbr_is_reset_in_cstate(void * ctx)417 static __always_inline bool lbr_is_reset_in_cstate(void *ctx)
418 {
419 	if (static_cpu_has(X86_FEATURE_ARCH_LBR))
420 		return x86_pmu.lbr_deep_c_reset && !rdlbr_from(0, NULL);
421 
422 	return !rdlbr_from(((struct x86_perf_task_context *)ctx)->tos, NULL);
423 }
424 
has_lbr_callstack_users(void * ctx)425 static inline bool has_lbr_callstack_users(void *ctx)
426 {
427 	return task_context_opt(ctx)->lbr_callstack_users ||
428 	       x86_pmu.lbr_callstack_users;
429 }
430 
__intel_pmu_lbr_restore(void * ctx)431 static void __intel_pmu_lbr_restore(void *ctx)
432 {
433 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
434 
435 	if (!has_lbr_callstack_users(ctx) ||
436 	    task_context_opt(ctx)->lbr_stack_state == LBR_NONE) {
437 		intel_pmu_lbr_reset();
438 		return;
439 	}
440 
441 	/*
442 	 * Does not restore the LBR registers, if
443 	 * - No one else touched them, and
444 	 * - Was not cleared in Cstate
445 	 */
446 	if ((ctx == cpuc->last_task_ctx) &&
447 	    (task_context_opt(ctx)->log_id == cpuc->last_log_id) &&
448 	    !lbr_is_reset_in_cstate(ctx)) {
449 		task_context_opt(ctx)->lbr_stack_state = LBR_NONE;
450 		return;
451 	}
452 
453 	x86_pmu.lbr_restore(ctx);
454 
455 	task_context_opt(ctx)->lbr_stack_state = LBR_NONE;
456 }
457 
intel_pmu_lbr_save(void * ctx)458 void intel_pmu_lbr_save(void *ctx)
459 {
460 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
461 	struct x86_perf_task_context *task_ctx = ctx;
462 	bool need_info = x86_pmu.lbr_has_info;
463 	unsigned lbr_idx, mask;
464 	u64 tos;
465 	int i;
466 
467 	mask = x86_pmu.lbr_nr - 1;
468 	tos = intel_pmu_lbr_tos();
469 	for (i = 0; i < x86_pmu.lbr_nr; i++) {
470 		lbr_idx = (tos - i) & mask;
471 		if (!rdlbr_all(&task_ctx->lbr[i], lbr_idx, need_info))
472 			break;
473 	}
474 	task_ctx->valid_lbrs = i;
475 	task_ctx->tos = tos;
476 
477 	if (cpuc->lbr_select)
478 		rdmsrl(MSR_LBR_SELECT, task_ctx->lbr_sel);
479 }
480 
intel_pmu_arch_lbr_save(void * ctx)481 static void intel_pmu_arch_lbr_save(void *ctx)
482 {
483 	struct x86_perf_task_context_arch_lbr *task_ctx = ctx;
484 	struct lbr_entry *entries = task_ctx->entries;
485 	int i;
486 
487 	for (i = 0; i < x86_pmu.lbr_nr; i++) {
488 		if (!rdlbr_all(&entries[i], i, true))
489 			break;
490 	}
491 
492 	/* LBR call stack is not full. Reset is required in restore. */
493 	if (i < x86_pmu.lbr_nr)
494 		entries[x86_pmu.lbr_nr - 1].from = 0;
495 }
496 
497 /*
498  * Save the Architecture LBR state to the xsave area in the perf
499  * context data for the task via the XSAVES instruction.
500  */
intel_pmu_arch_lbr_xsaves(void * ctx)501 static void intel_pmu_arch_lbr_xsaves(void *ctx)
502 {
503 	struct x86_perf_task_context_arch_lbr_xsave *task_ctx = ctx;
504 
505 	xsaves(&task_ctx->xsave, XFEATURE_MASK_LBR);
506 }
507 
__intel_pmu_lbr_save(void * ctx)508 static void __intel_pmu_lbr_save(void *ctx)
509 {
510 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
511 
512 	if (!has_lbr_callstack_users(ctx)) {
513 		task_context_opt(ctx)->lbr_stack_state = LBR_NONE;
514 		return;
515 	}
516 
517 	x86_pmu.lbr_save(ctx);
518 
519 	task_context_opt(ctx)->lbr_stack_state = LBR_VALID;
520 
521 	cpuc->last_task_ctx = ctx;
522 	cpuc->last_log_id = ++task_context_opt(ctx)->log_id;
523 }
524 
intel_pmu_lbr_swap_task_ctx(struct perf_event_pmu_context * prev_epc,struct perf_event_pmu_context * next_epc)525 void intel_pmu_lbr_swap_task_ctx(struct perf_event_pmu_context *prev_epc,
526 				 struct perf_event_pmu_context *next_epc)
527 {
528 	void *prev_ctx_data, *next_ctx_data;
529 
530 	swap(prev_epc->task_ctx_data, next_epc->task_ctx_data);
531 
532 	/*
533 	 * Architecture specific synchronization makes sense in case
534 	 * both prev_epc->task_ctx_data and next_epc->task_ctx_data
535 	 * pointers are allocated.
536 	 */
537 
538 	prev_ctx_data = next_epc->task_ctx_data;
539 	next_ctx_data = prev_epc->task_ctx_data;
540 
541 	if (!prev_ctx_data || !next_ctx_data)
542 		return;
543 
544 	swap(task_context_opt(prev_ctx_data)->lbr_callstack_users,
545 	     task_context_opt(next_ctx_data)->lbr_callstack_users);
546 }
547 
intel_pmu_lbr_sched_task(struct perf_event_pmu_context * pmu_ctx,struct task_struct * task,bool sched_in)548 void intel_pmu_lbr_sched_task(struct perf_event_pmu_context *pmu_ctx,
549 			      struct task_struct *task, bool sched_in)
550 {
551 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
552 	struct perf_ctx_data *ctx_data;
553 	void *task_ctx;
554 
555 	if (!cpuc->lbr_users)
556 		return;
557 
558 	/*
559 	 * If LBR callstack feature is enabled and the stack was saved when
560 	 * the task was scheduled out, restore the stack. Otherwise flush
561 	 * the LBR stack.
562 	 */
563 	rcu_read_lock();
564 	ctx_data = rcu_dereference(task->perf_ctx_data);
565 	task_ctx = ctx_data ? ctx_data->data : NULL;
566 	if (task_ctx) {
567 		if (sched_in)
568 			__intel_pmu_lbr_restore(task_ctx);
569 		else
570 			__intel_pmu_lbr_save(task_ctx);
571 		rcu_read_unlock();
572 		return;
573 	}
574 	rcu_read_unlock();
575 
576 	/*
577 	 * Since a context switch can flip the address space and LBR entries
578 	 * are not tagged with an identifier, we need to wipe the LBR, even for
579 	 * per-cpu events. You simply cannot resolve the branches from the old
580 	 * address space.
581 	 */
582 	if (sched_in)
583 		intel_pmu_lbr_reset();
584 }
585 
branch_user_callstack(unsigned br_sel)586 static inline bool branch_user_callstack(unsigned br_sel)
587 {
588 	return (br_sel & X86_BR_USER) && (br_sel & X86_BR_CALL_STACK);
589 }
590 
intel_pmu_lbr_add(struct perf_event * event)591 void intel_pmu_lbr_add(struct perf_event *event)
592 {
593 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
594 
595 	if (!x86_pmu.lbr_nr)
596 		return;
597 
598 	if (event->hw.flags & PERF_X86_EVENT_LBR_SELECT)
599 		cpuc->lbr_select = 1;
600 
601 	cpuc->br_sel = event->hw.branch_reg.reg;
602 
603 	if (branch_user_callstack(cpuc->br_sel)) {
604 		if (event->attach_state & PERF_ATTACH_TASK) {
605 			struct task_struct *task = event->hw.target;
606 			struct perf_ctx_data *ctx_data;
607 
608 			rcu_read_lock();
609 			ctx_data = rcu_dereference(task->perf_ctx_data);
610 			if (ctx_data)
611 				task_context_opt(ctx_data->data)->lbr_callstack_users++;
612 			rcu_read_unlock();
613 		} else
614 			x86_pmu.lbr_callstack_users++;
615 	}
616 	/*
617 	 * Request pmu::sched_task() callback, which will fire inside the
618 	 * regular perf event scheduling, so that call will:
619 	 *
620 	 *  - restore or wipe; when LBR-callstack,
621 	 *  - wipe; otherwise,
622 	 *
623 	 * when this is from __perf_event_task_sched_in().
624 	 *
625 	 * However, if this is from perf_install_in_context(), no such callback
626 	 * will follow and we'll need to reset the LBR here if this is the
627 	 * first LBR event.
628 	 *
629 	 * The problem is, we cannot tell these cases apart... but we can
630 	 * exclude the biggest chunk of cases by looking at
631 	 * event->total_time_running. An event that has accrued runtime cannot
632 	 * be 'new'. Conversely, a new event can get installed through the
633 	 * context switch path for the first time.
634 	 */
635 	if (x86_pmu.intel_cap.pebs_baseline && event->attr.precise_ip > 0)
636 		cpuc->lbr_pebs_users++;
637 	perf_sched_cb_inc(event->pmu);
638 	if (!cpuc->lbr_users++ && !event->total_time_running)
639 		intel_pmu_lbr_reset();
640 }
641 
release_lbr_buffers(void)642 void release_lbr_buffers(void)
643 {
644 	struct kmem_cache *kmem_cache;
645 	struct cpu_hw_events *cpuc;
646 	int cpu;
647 
648 	if (!static_cpu_has(X86_FEATURE_ARCH_LBR))
649 		return;
650 
651 	for_each_possible_cpu(cpu) {
652 		cpuc = per_cpu_ptr(&cpu_hw_events, cpu);
653 		kmem_cache = x86_get_pmu(cpu)->task_ctx_cache;
654 		if (kmem_cache && cpuc->lbr_xsave) {
655 			kmem_cache_free(kmem_cache, cpuc->lbr_xsave);
656 			cpuc->lbr_xsave = NULL;
657 		}
658 	}
659 }
660 
reserve_lbr_buffers(void)661 void reserve_lbr_buffers(void)
662 {
663 	struct kmem_cache *kmem_cache;
664 	struct cpu_hw_events *cpuc;
665 	int cpu;
666 
667 	if (!static_cpu_has(X86_FEATURE_ARCH_LBR))
668 		return;
669 
670 	for_each_possible_cpu(cpu) {
671 		cpuc = per_cpu_ptr(&cpu_hw_events, cpu);
672 		kmem_cache = x86_get_pmu(cpu)->task_ctx_cache;
673 		if (!kmem_cache || cpuc->lbr_xsave)
674 			continue;
675 
676 		cpuc->lbr_xsave = kmem_cache_alloc_node(kmem_cache,
677 							GFP_KERNEL | __GFP_ZERO,
678 							cpu_to_node(cpu));
679 	}
680 }
681 
intel_pmu_lbr_del(struct perf_event * event)682 void intel_pmu_lbr_del(struct perf_event *event)
683 {
684 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
685 
686 	if (!x86_pmu.lbr_nr)
687 		return;
688 
689 	if (branch_user_callstack(cpuc->br_sel)) {
690 		if (event->attach_state & PERF_ATTACH_TASK) {
691 			struct task_struct *task = event->hw.target;
692 			struct perf_ctx_data *ctx_data;
693 
694 			rcu_read_lock();
695 			ctx_data = rcu_dereference(task->perf_ctx_data);
696 			if (ctx_data)
697 				task_context_opt(ctx_data->data)->lbr_callstack_users--;
698 			rcu_read_unlock();
699 		} else
700 			x86_pmu.lbr_callstack_users--;
701 	}
702 
703 	if (event->hw.flags & PERF_X86_EVENT_LBR_SELECT)
704 		cpuc->lbr_select = 0;
705 
706 	if (x86_pmu.intel_cap.pebs_baseline && event->attr.precise_ip > 0)
707 		cpuc->lbr_pebs_users--;
708 	cpuc->lbr_users--;
709 	WARN_ON_ONCE(cpuc->lbr_users < 0);
710 	WARN_ON_ONCE(cpuc->lbr_pebs_users < 0);
711 	perf_sched_cb_dec(event->pmu);
712 
713 	/*
714 	 * The logged occurrences information is only valid for the
715 	 * current LBR group. If another LBR group is scheduled in
716 	 * later, the information from the stale LBRs will be wrongly
717 	 * interpreted. Reset the LBRs here.
718 	 *
719 	 * Only clear once for a branch counter group with the leader
720 	 * event. Because
721 	 * - Cannot simply reset the LBRs with the !cpuc->lbr_users.
722 	 *   Because it's possible that the last LBR user is not in a
723 	 *   branch counter group, e.g., a branch_counters group +
724 	 *   several normal LBR events.
725 	 * - The LBR reset can be done with any one of the events in a
726 	 *   branch counter group, since they are always scheduled together.
727 	 *   It's easy to force the leader event an LBR event.
728 	 */
729 	if (is_branch_counters_group(event) && event == event->group_leader)
730 		intel_pmu_lbr_reset();
731 }
732 
vlbr_exclude_host(void)733 static inline bool vlbr_exclude_host(void)
734 {
735 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
736 
737 	return test_bit(INTEL_PMC_IDX_FIXED_VLBR,
738 		(unsigned long *)&cpuc->intel_ctrl_guest_mask);
739 }
740 
intel_pmu_lbr_enable_all(bool pmi)741 void intel_pmu_lbr_enable_all(bool pmi)
742 {
743 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
744 
745 	if (cpuc->lbr_users && !vlbr_exclude_host())
746 		__intel_pmu_lbr_enable(pmi);
747 }
748 
intel_pmu_lbr_disable_all(void)749 void intel_pmu_lbr_disable_all(void)
750 {
751 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
752 
753 	if (cpuc->lbr_users && !vlbr_exclude_host()) {
754 		if (static_cpu_has(X86_FEATURE_ARCH_LBR))
755 			return __intel_pmu_arch_lbr_disable();
756 
757 		__intel_pmu_lbr_disable();
758 	}
759 }
760 
intel_pmu_lbr_read_32(struct cpu_hw_events * cpuc)761 void intel_pmu_lbr_read_32(struct cpu_hw_events *cpuc)
762 {
763 	unsigned long mask = x86_pmu.lbr_nr - 1;
764 	struct perf_branch_entry *br = cpuc->lbr_entries;
765 	u64 tos = intel_pmu_lbr_tos();
766 	int i;
767 
768 	for (i = 0; i < x86_pmu.lbr_nr; i++) {
769 		unsigned long lbr_idx = (tos - i) & mask;
770 		union {
771 			struct {
772 				u32 from;
773 				u32 to;
774 			};
775 			u64     lbr;
776 		} msr_lastbranch;
777 
778 		rdmsrl(x86_pmu.lbr_from + lbr_idx, msr_lastbranch.lbr);
779 
780 		perf_clear_branch_entry_bitfields(br);
781 
782 		br->from	= msr_lastbranch.from;
783 		br->to		= msr_lastbranch.to;
784 		br++;
785 	}
786 	cpuc->lbr_stack.nr = i;
787 	cpuc->lbr_stack.hw_idx = tos;
788 }
789 
790 /*
791  * Due to lack of segmentation in Linux the effective address (offset)
792  * is the same as the linear address, allowing us to merge the LIP and EIP
793  * LBR formats.
794  */
intel_pmu_lbr_read_64(struct cpu_hw_events * cpuc)795 void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc)
796 {
797 	bool need_info = false, call_stack = false;
798 	unsigned long mask = x86_pmu.lbr_nr - 1;
799 	struct perf_branch_entry *br = cpuc->lbr_entries;
800 	u64 tos = intel_pmu_lbr_tos();
801 	int i;
802 	int out = 0;
803 	int num = x86_pmu.lbr_nr;
804 
805 	if (cpuc->lbr_sel) {
806 		need_info = !(cpuc->lbr_sel->config & LBR_NO_INFO);
807 		if (cpuc->lbr_sel->config & LBR_CALL_STACK)
808 			call_stack = true;
809 	}
810 
811 	for (i = 0; i < num; i++) {
812 		unsigned long lbr_idx = (tos - i) & mask;
813 		u64 from, to, mis = 0, pred = 0, in_tx = 0, abort = 0;
814 		u16 cycles = 0;
815 
816 		from = rdlbr_from(lbr_idx, NULL);
817 		to   = rdlbr_to(lbr_idx, NULL);
818 
819 		/*
820 		 * Read LBR call stack entries
821 		 * until invalid entry (0s) is detected.
822 		 */
823 		if (call_stack && !from)
824 			break;
825 
826 		if (x86_pmu.lbr_has_info) {
827 			if (need_info) {
828 				u64 info;
829 
830 				info = rdlbr_info(lbr_idx, NULL);
831 				mis = !!(info & LBR_INFO_MISPRED);
832 				pred = !mis;
833 				cycles = (info & LBR_INFO_CYCLES);
834 				if (x86_pmu.lbr_has_tsx) {
835 					in_tx = !!(info & LBR_INFO_IN_TX);
836 					abort = !!(info & LBR_INFO_ABORT);
837 				}
838 			}
839 		} else {
840 			int skip = 0;
841 
842 			if (x86_pmu.lbr_from_flags) {
843 				mis = !!(from & LBR_FROM_FLAG_MISPRED);
844 				pred = !mis;
845 				skip = 1;
846 			}
847 			if (x86_pmu.lbr_has_tsx) {
848 				in_tx = !!(from & LBR_FROM_FLAG_IN_TX);
849 				abort = !!(from & LBR_FROM_FLAG_ABORT);
850 				skip = 3;
851 			}
852 			from = (u64)((((s64)from) << skip) >> skip);
853 
854 			if (x86_pmu.lbr_to_cycles) {
855 				cycles = ((to >> 48) & LBR_INFO_CYCLES);
856 				to = (u64)((((s64)to) << 16) >> 16);
857 			}
858 		}
859 
860 		/*
861 		 * Some CPUs report duplicated abort records,
862 		 * with the second entry not having an abort bit set.
863 		 * Skip them here. This loop runs backwards,
864 		 * so we need to undo the previous record.
865 		 * If the abort just happened outside the window
866 		 * the extra entry cannot be removed.
867 		 */
868 		if (abort && x86_pmu.lbr_double_abort && out > 0)
869 			out--;
870 
871 		perf_clear_branch_entry_bitfields(br+out);
872 		br[out].from	 = from;
873 		br[out].to	 = to;
874 		br[out].mispred	 = mis;
875 		br[out].predicted = pred;
876 		br[out].in_tx	 = in_tx;
877 		br[out].abort	 = abort;
878 		br[out].cycles	 = cycles;
879 		out++;
880 	}
881 	cpuc->lbr_stack.nr = out;
882 	cpuc->lbr_stack.hw_idx = tos;
883 }
884 
885 static DEFINE_STATIC_KEY_FALSE(x86_lbr_mispred);
886 static DEFINE_STATIC_KEY_FALSE(x86_lbr_cycles);
887 static DEFINE_STATIC_KEY_FALSE(x86_lbr_type);
888 
get_lbr_br_type(u64 info)889 static __always_inline int get_lbr_br_type(u64 info)
890 {
891 	int type = 0;
892 
893 	if (static_branch_likely(&x86_lbr_type))
894 		type = (info & LBR_INFO_BR_TYPE) >> LBR_INFO_BR_TYPE_OFFSET;
895 
896 	return type;
897 }
898 
get_lbr_mispred(u64 info)899 static __always_inline bool get_lbr_mispred(u64 info)
900 {
901 	bool mispred = 0;
902 
903 	if (static_branch_likely(&x86_lbr_mispred))
904 		mispred = !!(info & LBR_INFO_MISPRED);
905 
906 	return mispred;
907 }
908 
get_lbr_cycles(u64 info)909 static __always_inline u16 get_lbr_cycles(u64 info)
910 {
911 	u16 cycles = info & LBR_INFO_CYCLES;
912 
913 	if (static_cpu_has(X86_FEATURE_ARCH_LBR) &&
914 	    (!static_branch_likely(&x86_lbr_cycles) ||
915 	     !(info & LBR_INFO_CYC_CNT_VALID)))
916 		cycles = 0;
917 
918 	return cycles;
919 }
920 
921 static_assert((64 - PERF_BRANCH_ENTRY_INFO_BITS_MAX) > LBR_INFO_BR_CNTR_NUM * LBR_INFO_BR_CNTR_BITS);
922 
intel_pmu_store_lbr(struct cpu_hw_events * cpuc,struct lbr_entry * entries)923 static void intel_pmu_store_lbr(struct cpu_hw_events *cpuc,
924 				struct lbr_entry *entries)
925 {
926 	struct perf_branch_entry *e;
927 	struct lbr_entry *lbr;
928 	u64 from, to, info;
929 	int i;
930 
931 	for (i = 0; i < x86_pmu.lbr_nr; i++) {
932 		lbr = entries ? &entries[i] : NULL;
933 		e = &cpuc->lbr_entries[i];
934 
935 		from = rdlbr_from(i, lbr);
936 		/*
937 		 * Read LBR entries until invalid entry (0s) is detected.
938 		 */
939 		if (!from)
940 			break;
941 
942 		to = rdlbr_to(i, lbr);
943 		info = rdlbr_info(i, lbr);
944 
945 		perf_clear_branch_entry_bitfields(e);
946 
947 		e->from		= from;
948 		e->to		= to;
949 		e->mispred	= get_lbr_mispred(info);
950 		e->predicted	= !e->mispred;
951 		e->in_tx	= !!(info & LBR_INFO_IN_TX);
952 		e->abort	= !!(info & LBR_INFO_ABORT);
953 		e->cycles	= get_lbr_cycles(info);
954 		e->type		= get_lbr_br_type(info);
955 
956 		/*
957 		 * Leverage the reserved field of cpuc->lbr_entries[i] to
958 		 * temporarily store the branch counters information.
959 		 * The later code will decide what content can be disclosed
960 		 * to the perf tool. Pleae see intel_pmu_lbr_counters_reorder().
961 		 */
962 		e->reserved	= (info >> LBR_INFO_BR_CNTR_OFFSET) & LBR_INFO_BR_CNTR_FULL_MASK;
963 	}
964 
965 	cpuc->lbr_stack.nr = i;
966 }
967 
968 /*
969  * The enabled order may be different from the counter order.
970  * Update the lbr_counters with the enabled order.
971  */
intel_pmu_lbr_counters_reorder(struct cpu_hw_events * cpuc,struct perf_event * event)972 static void intel_pmu_lbr_counters_reorder(struct cpu_hw_events *cpuc,
973 					   struct perf_event *event)
974 {
975 	int i, j, pos = 0, order[X86_PMC_IDX_MAX];
976 	struct perf_event *leader, *sibling;
977 	u64 src, dst, cnt;
978 
979 	leader = event->group_leader;
980 	if (branch_sample_counters(leader))
981 		order[pos++] = leader->hw.idx;
982 
983 	for_each_sibling_event(sibling, leader) {
984 		if (!branch_sample_counters(sibling))
985 			continue;
986 		order[pos++] = sibling->hw.idx;
987 	}
988 
989 	WARN_ON_ONCE(!pos);
990 
991 	for (i = 0; i < cpuc->lbr_stack.nr; i++) {
992 		src = cpuc->lbr_entries[i].reserved;
993 		dst = 0;
994 		for (j = 0; j < pos; j++) {
995 			cnt = (src >> (order[j] * LBR_INFO_BR_CNTR_BITS)) & LBR_INFO_BR_CNTR_MASK;
996 			dst |= cnt << j * LBR_INFO_BR_CNTR_BITS;
997 		}
998 		cpuc->lbr_counters[i] = dst;
999 		cpuc->lbr_entries[i].reserved = 0;
1000 	}
1001 }
1002 
intel_pmu_lbr_save_brstack(struct perf_sample_data * data,struct cpu_hw_events * cpuc,struct perf_event * event)1003 void intel_pmu_lbr_save_brstack(struct perf_sample_data *data,
1004 				struct cpu_hw_events *cpuc,
1005 				struct perf_event *event)
1006 {
1007 	if (is_branch_counters_group(event)) {
1008 		intel_pmu_lbr_counters_reorder(cpuc, event);
1009 		perf_sample_save_brstack(data, event, &cpuc->lbr_stack, cpuc->lbr_counters);
1010 		return;
1011 	}
1012 
1013 	perf_sample_save_brstack(data, event, &cpuc->lbr_stack, NULL);
1014 }
1015 
intel_pmu_arch_lbr_read(struct cpu_hw_events * cpuc)1016 static void intel_pmu_arch_lbr_read(struct cpu_hw_events *cpuc)
1017 {
1018 	intel_pmu_store_lbr(cpuc, NULL);
1019 }
1020 
intel_pmu_arch_lbr_read_xsave(struct cpu_hw_events * cpuc)1021 static void intel_pmu_arch_lbr_read_xsave(struct cpu_hw_events *cpuc)
1022 {
1023 	struct x86_perf_task_context_arch_lbr_xsave *xsave = cpuc->lbr_xsave;
1024 
1025 	if (!xsave) {
1026 		intel_pmu_store_lbr(cpuc, NULL);
1027 		return;
1028 	}
1029 	xsaves(&xsave->xsave, XFEATURE_MASK_LBR);
1030 
1031 	intel_pmu_store_lbr(cpuc, xsave->lbr.entries);
1032 }
1033 
intel_pmu_lbr_read(void)1034 void intel_pmu_lbr_read(void)
1035 {
1036 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1037 
1038 	/*
1039 	 * Don't read when all LBRs users are using adaptive PEBS.
1040 	 *
1041 	 * This could be smarter and actually check the event,
1042 	 * but this simple approach seems to work for now.
1043 	 */
1044 	if (!cpuc->lbr_users || vlbr_exclude_host() ||
1045 	    cpuc->lbr_users == cpuc->lbr_pebs_users)
1046 		return;
1047 
1048 	x86_pmu.lbr_read(cpuc);
1049 
1050 	intel_pmu_lbr_filter(cpuc);
1051 }
1052 
1053 /*
1054  * SW filter is used:
1055  * - in case there is no HW filter
1056  * - in case the HW filter has errata or limitations
1057  */
intel_pmu_setup_sw_lbr_filter(struct perf_event * event)1058 static int intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
1059 {
1060 	u64 br_type = event->attr.branch_sample_type;
1061 	int mask = 0;
1062 
1063 	if (br_type & PERF_SAMPLE_BRANCH_USER)
1064 		mask |= X86_BR_USER;
1065 
1066 	if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
1067 		mask |= X86_BR_KERNEL;
1068 
1069 	/* we ignore BRANCH_HV here */
1070 
1071 	if (br_type & PERF_SAMPLE_BRANCH_ANY)
1072 		mask |= X86_BR_ANY;
1073 
1074 	if (br_type & PERF_SAMPLE_BRANCH_ANY_CALL)
1075 		mask |= X86_BR_ANY_CALL;
1076 
1077 	if (br_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
1078 		mask |= X86_BR_RET | X86_BR_IRET | X86_BR_SYSRET;
1079 
1080 	if (br_type & PERF_SAMPLE_BRANCH_IND_CALL)
1081 		mask |= X86_BR_IND_CALL;
1082 
1083 	if (br_type & PERF_SAMPLE_BRANCH_ABORT_TX)
1084 		mask |= X86_BR_ABORT;
1085 
1086 	if (br_type & PERF_SAMPLE_BRANCH_IN_TX)
1087 		mask |= X86_BR_IN_TX;
1088 
1089 	if (br_type & PERF_SAMPLE_BRANCH_NO_TX)
1090 		mask |= X86_BR_NO_TX;
1091 
1092 	if (br_type & PERF_SAMPLE_BRANCH_COND)
1093 		mask |= X86_BR_JCC;
1094 
1095 	if (br_type & PERF_SAMPLE_BRANCH_CALL_STACK) {
1096 		if (!x86_pmu_has_lbr_callstack())
1097 			return -EOPNOTSUPP;
1098 		if (mask & ~(X86_BR_USER | X86_BR_KERNEL))
1099 			return -EINVAL;
1100 		mask |= X86_BR_CALL | X86_BR_IND_CALL | X86_BR_RET |
1101 			X86_BR_CALL_STACK;
1102 	}
1103 
1104 	if (br_type & PERF_SAMPLE_BRANCH_IND_JUMP)
1105 		mask |= X86_BR_IND_JMP;
1106 
1107 	if (br_type & PERF_SAMPLE_BRANCH_CALL)
1108 		mask |= X86_BR_CALL | X86_BR_ZERO_CALL;
1109 
1110 	if (br_type & PERF_SAMPLE_BRANCH_TYPE_SAVE)
1111 		mask |= X86_BR_TYPE_SAVE;
1112 
1113 	/*
1114 	 * stash actual user request into reg, it may
1115 	 * be used by fixup code for some CPU
1116 	 */
1117 	event->hw.branch_reg.reg = mask;
1118 	return 0;
1119 }
1120 
1121 /*
1122  * setup the HW LBR filter
1123  * Used only when available, may not be enough to disambiguate
1124  * all branches, may need the help of the SW filter
1125  */
intel_pmu_setup_hw_lbr_filter(struct perf_event * event)1126 static int intel_pmu_setup_hw_lbr_filter(struct perf_event *event)
1127 {
1128 	struct hw_perf_event_extra *reg;
1129 	u64 br_type = event->attr.branch_sample_type;
1130 	u64 mask = 0, v;
1131 	int i;
1132 
1133 	for (i = 0; i < PERF_SAMPLE_BRANCH_MAX_SHIFT; i++) {
1134 		if (!(br_type & (1ULL << i)))
1135 			continue;
1136 
1137 		v = x86_pmu.lbr_sel_map[i];
1138 		if (v == LBR_NOT_SUPP)
1139 			return -EOPNOTSUPP;
1140 
1141 		if (v != LBR_IGN)
1142 			mask |= v;
1143 	}
1144 
1145 	reg = &event->hw.branch_reg;
1146 	reg->idx = EXTRA_REG_LBR;
1147 
1148 	if (static_cpu_has(X86_FEATURE_ARCH_LBR)) {
1149 		reg->config = mask;
1150 
1151 		/*
1152 		 * The Arch LBR HW can retrieve the common branch types
1153 		 * from the LBR_INFO. It doesn't require the high overhead
1154 		 * SW disassemble.
1155 		 * Enable the branch type by default for the Arch LBR.
1156 		 */
1157 		reg->reg |= X86_BR_TYPE_SAVE;
1158 		return 0;
1159 	}
1160 
1161 	/*
1162 	 * The first 9 bits (LBR_SEL_MASK) in LBR_SELECT operate
1163 	 * in suppress mode. So LBR_SELECT should be set to
1164 	 * (~mask & LBR_SEL_MASK) | (mask & ~LBR_SEL_MASK)
1165 	 * But the 10th bit LBR_CALL_STACK does not operate
1166 	 * in suppress mode.
1167 	 */
1168 	reg->config = mask ^ (x86_pmu.lbr_sel_mask & ~LBR_CALL_STACK);
1169 
1170 	if ((br_type & PERF_SAMPLE_BRANCH_NO_CYCLES) &&
1171 	    (br_type & PERF_SAMPLE_BRANCH_NO_FLAGS) &&
1172 	    x86_pmu.lbr_has_info)
1173 		reg->config |= LBR_NO_INFO;
1174 
1175 	return 0;
1176 }
1177 
intel_pmu_setup_lbr_filter(struct perf_event * event)1178 int intel_pmu_setup_lbr_filter(struct perf_event *event)
1179 {
1180 	int ret = 0;
1181 
1182 	/*
1183 	 * no LBR on this PMU
1184 	 */
1185 	if (!x86_pmu.lbr_nr)
1186 		return -EOPNOTSUPP;
1187 
1188 	/*
1189 	 * setup SW LBR filter
1190 	 */
1191 	ret = intel_pmu_setup_sw_lbr_filter(event);
1192 	if (ret)
1193 		return ret;
1194 
1195 	/*
1196 	 * setup HW LBR filter, if any
1197 	 */
1198 	if (x86_pmu.lbr_sel_map)
1199 		ret = intel_pmu_setup_hw_lbr_filter(event);
1200 
1201 	return ret;
1202 }
1203 
1204 enum {
1205 	ARCH_LBR_BR_TYPE_JCC			= 0,
1206 	ARCH_LBR_BR_TYPE_NEAR_IND_JMP		= 1,
1207 	ARCH_LBR_BR_TYPE_NEAR_REL_JMP		= 2,
1208 	ARCH_LBR_BR_TYPE_NEAR_IND_CALL		= 3,
1209 	ARCH_LBR_BR_TYPE_NEAR_REL_CALL		= 4,
1210 	ARCH_LBR_BR_TYPE_NEAR_RET		= 5,
1211 	ARCH_LBR_BR_TYPE_KNOWN_MAX		= ARCH_LBR_BR_TYPE_NEAR_RET,
1212 
1213 	ARCH_LBR_BR_TYPE_MAP_MAX		= 16,
1214 };
1215 
1216 static const int arch_lbr_br_type_map[ARCH_LBR_BR_TYPE_MAP_MAX] = {
1217 	[ARCH_LBR_BR_TYPE_JCC]			= X86_BR_JCC,
1218 	[ARCH_LBR_BR_TYPE_NEAR_IND_JMP]		= X86_BR_IND_JMP,
1219 	[ARCH_LBR_BR_TYPE_NEAR_REL_JMP]		= X86_BR_JMP,
1220 	[ARCH_LBR_BR_TYPE_NEAR_IND_CALL]	= X86_BR_IND_CALL,
1221 	[ARCH_LBR_BR_TYPE_NEAR_REL_CALL]	= X86_BR_CALL,
1222 	[ARCH_LBR_BR_TYPE_NEAR_RET]		= X86_BR_RET,
1223 };
1224 
1225 /*
1226  * implement actual branch filter based on user demand.
1227  * Hardware may not exactly satisfy that request, thus
1228  * we need to inspect opcodes. Mismatched branches are
1229  * discarded. Therefore, the number of branches returned
1230  * in PERF_SAMPLE_BRANCH_STACK sample may vary.
1231  */
1232 static void
intel_pmu_lbr_filter(struct cpu_hw_events * cpuc)1233 intel_pmu_lbr_filter(struct cpu_hw_events *cpuc)
1234 {
1235 	u64 from, to;
1236 	int br_sel = cpuc->br_sel;
1237 	int i, j, type, to_plm;
1238 	bool compress = false;
1239 
1240 	/* if sampling all branches, then nothing to filter */
1241 	if (((br_sel & X86_BR_ALL) == X86_BR_ALL) &&
1242 	    ((br_sel & X86_BR_TYPE_SAVE) != X86_BR_TYPE_SAVE))
1243 		return;
1244 
1245 	for (i = 0; i < cpuc->lbr_stack.nr; i++) {
1246 
1247 		from = cpuc->lbr_entries[i].from;
1248 		to = cpuc->lbr_entries[i].to;
1249 		type = cpuc->lbr_entries[i].type;
1250 
1251 		/*
1252 		 * Parse the branch type recorded in LBR_x_INFO MSR.
1253 		 * Doesn't support OTHER_BRANCH decoding for now.
1254 		 * OTHER_BRANCH branch type still rely on software decoding.
1255 		 */
1256 		if (static_cpu_has(X86_FEATURE_ARCH_LBR) &&
1257 		    type <= ARCH_LBR_BR_TYPE_KNOWN_MAX) {
1258 			to_plm = kernel_ip(to) ? X86_BR_KERNEL : X86_BR_USER;
1259 			type = arch_lbr_br_type_map[type] | to_plm;
1260 		} else
1261 			type = branch_type(from, to, cpuc->lbr_entries[i].abort);
1262 		if (type != X86_BR_NONE && (br_sel & X86_BR_ANYTX)) {
1263 			if (cpuc->lbr_entries[i].in_tx)
1264 				type |= X86_BR_IN_TX;
1265 			else
1266 				type |= X86_BR_NO_TX;
1267 		}
1268 
1269 		/* if type does not correspond, then discard */
1270 		if (type == X86_BR_NONE || (br_sel & type) != type) {
1271 			cpuc->lbr_entries[i].from = 0;
1272 			compress = true;
1273 		}
1274 
1275 		if ((br_sel & X86_BR_TYPE_SAVE) == X86_BR_TYPE_SAVE)
1276 			cpuc->lbr_entries[i].type = common_branch_type(type);
1277 	}
1278 
1279 	if (!compress)
1280 		return;
1281 
1282 	/* remove all entries with from=0 */
1283 	for (i = 0; i < cpuc->lbr_stack.nr; ) {
1284 		if (!cpuc->lbr_entries[i].from) {
1285 			j = i;
1286 			while (++j < cpuc->lbr_stack.nr) {
1287 				cpuc->lbr_entries[j-1] = cpuc->lbr_entries[j];
1288 				cpuc->lbr_counters[j-1] = cpuc->lbr_counters[j];
1289 			}
1290 			cpuc->lbr_stack.nr--;
1291 			if (!cpuc->lbr_entries[i].from)
1292 				continue;
1293 		}
1294 		i++;
1295 	}
1296 }
1297 
intel_pmu_store_pebs_lbrs(struct lbr_entry * lbr)1298 void intel_pmu_store_pebs_lbrs(struct lbr_entry *lbr)
1299 {
1300 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1301 
1302 	/* Cannot get TOS for large PEBS and Arch LBR */
1303 	if (static_cpu_has(X86_FEATURE_ARCH_LBR) ||
1304 	    (cpuc->n_pebs == cpuc->n_large_pebs))
1305 		cpuc->lbr_stack.hw_idx = -1ULL;
1306 	else
1307 		cpuc->lbr_stack.hw_idx = intel_pmu_lbr_tos();
1308 
1309 	intel_pmu_store_lbr(cpuc, lbr);
1310 	intel_pmu_lbr_filter(cpuc);
1311 }
1312 
1313 /*
1314  * Map interface branch filters onto LBR filters
1315  */
1316 static const int nhm_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
1317 	[PERF_SAMPLE_BRANCH_ANY_SHIFT]		= LBR_ANY,
1318 	[PERF_SAMPLE_BRANCH_USER_SHIFT]		= LBR_USER,
1319 	[PERF_SAMPLE_BRANCH_KERNEL_SHIFT]	= LBR_KERNEL,
1320 	[PERF_SAMPLE_BRANCH_HV_SHIFT]		= LBR_IGN,
1321 	[PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT]	= LBR_RETURN | LBR_REL_JMP
1322 						| LBR_IND_JMP | LBR_FAR,
1323 	/*
1324 	 * NHM/WSM erratum: must include REL_JMP+IND_JMP to get CALL branches
1325 	 */
1326 	[PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] =
1327 	 LBR_REL_CALL | LBR_IND_CALL | LBR_REL_JMP | LBR_IND_JMP | LBR_FAR,
1328 	/*
1329 	 * NHM/WSM erratum: must include IND_JMP to capture IND_CALL
1330 	 */
1331 	[PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL | LBR_IND_JMP,
1332 	[PERF_SAMPLE_BRANCH_COND_SHIFT]     = LBR_JCC,
1333 	[PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
1334 };
1335 
1336 static const int snb_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
1337 	[PERF_SAMPLE_BRANCH_ANY_SHIFT]		= LBR_ANY,
1338 	[PERF_SAMPLE_BRANCH_USER_SHIFT]		= LBR_USER,
1339 	[PERF_SAMPLE_BRANCH_KERNEL_SHIFT]	= LBR_KERNEL,
1340 	[PERF_SAMPLE_BRANCH_HV_SHIFT]		= LBR_IGN,
1341 	[PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT]	= LBR_RETURN | LBR_FAR,
1342 	[PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT]	= LBR_REL_CALL | LBR_IND_CALL
1343 						| LBR_FAR,
1344 	[PERF_SAMPLE_BRANCH_IND_CALL_SHIFT]	= LBR_IND_CALL,
1345 	[PERF_SAMPLE_BRANCH_COND_SHIFT]		= LBR_JCC,
1346 	[PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT]	= LBR_IND_JMP,
1347 	[PERF_SAMPLE_BRANCH_CALL_SHIFT]		= LBR_REL_CALL,
1348 };
1349 
1350 static const int hsw_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
1351 	[PERF_SAMPLE_BRANCH_ANY_SHIFT]		= LBR_ANY,
1352 	[PERF_SAMPLE_BRANCH_USER_SHIFT]		= LBR_USER,
1353 	[PERF_SAMPLE_BRANCH_KERNEL_SHIFT]	= LBR_KERNEL,
1354 	[PERF_SAMPLE_BRANCH_HV_SHIFT]		= LBR_IGN,
1355 	[PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT]	= LBR_RETURN | LBR_FAR,
1356 	[PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT]	= LBR_REL_CALL | LBR_IND_CALL
1357 						| LBR_FAR,
1358 	[PERF_SAMPLE_BRANCH_IND_CALL_SHIFT]	= LBR_IND_CALL,
1359 	[PERF_SAMPLE_BRANCH_COND_SHIFT]		= LBR_JCC,
1360 	[PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT]	= LBR_REL_CALL | LBR_IND_CALL
1361 						| LBR_RETURN | LBR_CALL_STACK,
1362 	[PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT]	= LBR_IND_JMP,
1363 	[PERF_SAMPLE_BRANCH_CALL_SHIFT]		= LBR_REL_CALL,
1364 };
1365 
1366 static int arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
1367 	[PERF_SAMPLE_BRANCH_ANY_SHIFT]		= ARCH_LBR_ANY,
1368 	[PERF_SAMPLE_BRANCH_USER_SHIFT]		= ARCH_LBR_USER,
1369 	[PERF_SAMPLE_BRANCH_KERNEL_SHIFT]	= ARCH_LBR_KERNEL,
1370 	[PERF_SAMPLE_BRANCH_HV_SHIFT]		= LBR_IGN,
1371 	[PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT]	= ARCH_LBR_RETURN |
1372 						  ARCH_LBR_OTHER_BRANCH,
1373 	[PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT]     = ARCH_LBR_REL_CALL |
1374 						  ARCH_LBR_IND_CALL |
1375 						  ARCH_LBR_OTHER_BRANCH,
1376 	[PERF_SAMPLE_BRANCH_IND_CALL_SHIFT]     = ARCH_LBR_IND_CALL,
1377 	[PERF_SAMPLE_BRANCH_COND_SHIFT]         = ARCH_LBR_JCC,
1378 	[PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT]   = ARCH_LBR_REL_CALL |
1379 						  ARCH_LBR_IND_CALL |
1380 						  ARCH_LBR_RETURN |
1381 						  ARCH_LBR_CALL_STACK,
1382 	[PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT]	= ARCH_LBR_IND_JMP,
1383 	[PERF_SAMPLE_BRANCH_CALL_SHIFT]		= ARCH_LBR_REL_CALL,
1384 };
1385 
1386 /* core */
intel_pmu_lbr_init_core(void)1387 void __init intel_pmu_lbr_init_core(void)
1388 {
1389 	x86_pmu.lbr_nr     = 4;
1390 	x86_pmu.lbr_tos    = MSR_LBR_TOS;
1391 	x86_pmu.lbr_from   = MSR_LBR_CORE_FROM;
1392 	x86_pmu.lbr_to     = MSR_LBR_CORE_TO;
1393 
1394 	/*
1395 	 * SW branch filter usage:
1396 	 * - compensate for lack of HW filter
1397 	 */
1398 }
1399 
1400 /* nehalem/westmere */
intel_pmu_lbr_init_nhm(void)1401 void __init intel_pmu_lbr_init_nhm(void)
1402 {
1403 	x86_pmu.lbr_nr     = 16;
1404 	x86_pmu.lbr_tos    = MSR_LBR_TOS;
1405 	x86_pmu.lbr_from   = MSR_LBR_NHM_FROM;
1406 	x86_pmu.lbr_to     = MSR_LBR_NHM_TO;
1407 
1408 	x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1409 	x86_pmu.lbr_sel_map  = nhm_lbr_sel_map;
1410 
1411 	/*
1412 	 * SW branch filter usage:
1413 	 * - workaround LBR_SEL errata (see above)
1414 	 * - support syscall, sysret capture.
1415 	 *   That requires LBR_FAR but that means far
1416 	 *   jmp need to be filtered out
1417 	 */
1418 }
1419 
1420 /* sandy bridge */
intel_pmu_lbr_init_snb(void)1421 void __init intel_pmu_lbr_init_snb(void)
1422 {
1423 	x86_pmu.lbr_nr	 = 16;
1424 	x86_pmu.lbr_tos	 = MSR_LBR_TOS;
1425 	x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1426 	x86_pmu.lbr_to   = MSR_LBR_NHM_TO;
1427 
1428 	x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1429 	x86_pmu.lbr_sel_map  = snb_lbr_sel_map;
1430 
1431 	/*
1432 	 * SW branch filter usage:
1433 	 * - support syscall, sysret capture.
1434 	 *   That requires LBR_FAR but that means far
1435 	 *   jmp need to be filtered out
1436 	 */
1437 }
1438 
1439 static inline struct kmem_cache *
create_lbr_kmem_cache(size_t size,size_t align)1440 create_lbr_kmem_cache(size_t size, size_t align)
1441 {
1442 	return kmem_cache_create("x86_lbr", size, align, 0, NULL);
1443 }
1444 
1445 /* haswell */
intel_pmu_lbr_init_hsw(void)1446 void intel_pmu_lbr_init_hsw(void)
1447 {
1448 	size_t size = sizeof(struct x86_perf_task_context);
1449 
1450 	x86_pmu.lbr_nr	 = 16;
1451 	x86_pmu.lbr_tos	 = MSR_LBR_TOS;
1452 	x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1453 	x86_pmu.lbr_to   = MSR_LBR_NHM_TO;
1454 
1455 	x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1456 	x86_pmu.lbr_sel_map  = hsw_lbr_sel_map;
1457 
1458 	x86_get_pmu(smp_processor_id())->task_ctx_cache = create_lbr_kmem_cache(size, 0);
1459 }
1460 
1461 /* skylake */
intel_pmu_lbr_init_skl(void)1462 __init void intel_pmu_lbr_init_skl(void)
1463 {
1464 	size_t size = sizeof(struct x86_perf_task_context);
1465 
1466 	x86_pmu.lbr_nr	 = 32;
1467 	x86_pmu.lbr_tos	 = MSR_LBR_TOS;
1468 	x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1469 	x86_pmu.lbr_to   = MSR_LBR_NHM_TO;
1470 	x86_pmu.lbr_info = MSR_LBR_INFO_0;
1471 
1472 	x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1473 	x86_pmu.lbr_sel_map  = hsw_lbr_sel_map;
1474 
1475 	x86_get_pmu(smp_processor_id())->task_ctx_cache = create_lbr_kmem_cache(size, 0);
1476 
1477 	/*
1478 	 * SW branch filter usage:
1479 	 * - support syscall, sysret capture.
1480 	 *   That requires LBR_FAR but that means far
1481 	 *   jmp need to be filtered out
1482 	 */
1483 }
1484 
1485 /* atom */
intel_pmu_lbr_init_atom(void)1486 void __init intel_pmu_lbr_init_atom(void)
1487 {
1488 	/*
1489 	 * only models starting at stepping 10 seems
1490 	 * to have an operational LBR which can freeze
1491 	 * on PMU interrupt
1492 	 */
1493 	if (boot_cpu_data.x86_vfm == INTEL_ATOM_BONNELL
1494 	    && boot_cpu_data.x86_stepping < 10) {
1495 		pr_cont("LBR disabled due to erratum");
1496 		return;
1497 	}
1498 
1499 	x86_pmu.lbr_nr	   = 8;
1500 	x86_pmu.lbr_tos    = MSR_LBR_TOS;
1501 	x86_pmu.lbr_from   = MSR_LBR_CORE_FROM;
1502 	x86_pmu.lbr_to     = MSR_LBR_CORE_TO;
1503 
1504 	/*
1505 	 * SW branch filter usage:
1506 	 * - compensate for lack of HW filter
1507 	 */
1508 }
1509 
1510 /* slm */
intel_pmu_lbr_init_slm(void)1511 void __init intel_pmu_lbr_init_slm(void)
1512 {
1513 	x86_pmu.lbr_nr	   = 8;
1514 	x86_pmu.lbr_tos    = MSR_LBR_TOS;
1515 	x86_pmu.lbr_from   = MSR_LBR_CORE_FROM;
1516 	x86_pmu.lbr_to     = MSR_LBR_CORE_TO;
1517 
1518 	x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1519 	x86_pmu.lbr_sel_map  = nhm_lbr_sel_map;
1520 
1521 	/*
1522 	 * SW branch filter usage:
1523 	 * - compensate for lack of HW filter
1524 	 */
1525 	pr_cont("8-deep LBR, ");
1526 }
1527 
1528 /* Knights Landing */
intel_pmu_lbr_init_knl(void)1529 void intel_pmu_lbr_init_knl(void)
1530 {
1531 	x86_pmu.lbr_nr	   = 8;
1532 	x86_pmu.lbr_tos    = MSR_LBR_TOS;
1533 	x86_pmu.lbr_from   = MSR_LBR_NHM_FROM;
1534 	x86_pmu.lbr_to     = MSR_LBR_NHM_TO;
1535 
1536 	x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1537 	x86_pmu.lbr_sel_map  = snb_lbr_sel_map;
1538 
1539 	/* Knights Landing does have MISPREDICT bit */
1540 	if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_LIP)
1541 		x86_pmu.intel_cap.lbr_format = LBR_FORMAT_EIP_FLAGS;
1542 }
1543 
intel_pmu_lbr_init(void)1544 void intel_pmu_lbr_init(void)
1545 {
1546 	switch (x86_pmu.intel_cap.lbr_format) {
1547 	case LBR_FORMAT_EIP_FLAGS2:
1548 		x86_pmu.lbr_has_tsx = 1;
1549 		x86_pmu.lbr_from_flags = 1;
1550 		if (lbr_from_signext_quirk_needed())
1551 			static_branch_enable(&lbr_from_quirk_key);
1552 		break;
1553 
1554 	case LBR_FORMAT_EIP_FLAGS:
1555 		x86_pmu.lbr_from_flags = 1;
1556 		break;
1557 
1558 	case LBR_FORMAT_INFO:
1559 		x86_pmu.lbr_has_tsx = 1;
1560 		fallthrough;
1561 	case LBR_FORMAT_INFO2:
1562 		x86_pmu.lbr_has_info = 1;
1563 		break;
1564 
1565 	case LBR_FORMAT_TIME:
1566 		x86_pmu.lbr_from_flags = 1;
1567 		x86_pmu.lbr_to_cycles = 1;
1568 		break;
1569 	}
1570 
1571 	if (x86_pmu.lbr_has_info) {
1572 		/*
1573 		 * Only used in combination with baseline pebs.
1574 		 */
1575 		static_branch_enable(&x86_lbr_mispred);
1576 		static_branch_enable(&x86_lbr_cycles);
1577 	}
1578 }
1579 
1580 /*
1581  * LBR state size is variable based on the max number of registers.
1582  * This calculates the expected state size, which should match
1583  * what the hardware enumerates for the size of XFEATURE_LBR.
1584  */
get_lbr_state_size(void)1585 static inline unsigned int get_lbr_state_size(void)
1586 {
1587 	return sizeof(struct arch_lbr_state) +
1588 	       x86_pmu.lbr_nr * sizeof(struct lbr_entry);
1589 }
1590 
is_arch_lbr_xsave_available(void)1591 static bool is_arch_lbr_xsave_available(void)
1592 {
1593 	if (!boot_cpu_has(X86_FEATURE_XSAVES))
1594 		return false;
1595 
1596 	/*
1597 	 * Check the LBR state with the corresponding software structure.
1598 	 * Disable LBR XSAVES support if the size doesn't match.
1599 	 */
1600 	if (xfeature_size(XFEATURE_LBR) == 0)
1601 		return false;
1602 
1603 	if (WARN_ON(xfeature_size(XFEATURE_LBR) != get_lbr_state_size()))
1604 		return false;
1605 
1606 	return true;
1607 }
1608 
intel_pmu_arch_lbr_init(void)1609 void __init intel_pmu_arch_lbr_init(void)
1610 {
1611 	struct pmu *pmu = x86_get_pmu(smp_processor_id());
1612 	union cpuid28_eax eax;
1613 	union cpuid28_ebx ebx;
1614 	union cpuid28_ecx ecx;
1615 	unsigned int unused_edx;
1616 	bool arch_lbr_xsave;
1617 	size_t size;
1618 	u64 lbr_nr;
1619 
1620 	/* Arch LBR Capabilities */
1621 	cpuid(28, &eax.full, &ebx.full, &ecx.full, &unused_edx);
1622 
1623 	lbr_nr = fls(eax.split.lbr_depth_mask) * 8;
1624 	if (!lbr_nr)
1625 		goto clear_arch_lbr;
1626 
1627 	/* Apply the max depth of Arch LBR */
1628 	if (wrmsrl_safe(MSR_ARCH_LBR_DEPTH, lbr_nr))
1629 		goto clear_arch_lbr;
1630 
1631 	x86_pmu.lbr_depth_mask = eax.split.lbr_depth_mask;
1632 	x86_pmu.lbr_deep_c_reset = eax.split.lbr_deep_c_reset;
1633 	x86_pmu.lbr_lip = eax.split.lbr_lip;
1634 	x86_pmu.lbr_cpl = ebx.split.lbr_cpl;
1635 	x86_pmu.lbr_filter = ebx.split.lbr_filter;
1636 	x86_pmu.lbr_call_stack = ebx.split.lbr_call_stack;
1637 	x86_pmu.lbr_mispred = ecx.split.lbr_mispred;
1638 	x86_pmu.lbr_timed_lbr = ecx.split.lbr_timed_lbr;
1639 	x86_pmu.lbr_br_type = ecx.split.lbr_br_type;
1640 	x86_pmu.lbr_counters = ecx.split.lbr_counters;
1641 	x86_pmu.lbr_nr = lbr_nr;
1642 
1643 	if (!!x86_pmu.lbr_counters)
1644 		x86_pmu.flags |= PMU_FL_BR_CNTR;
1645 
1646 	if (x86_pmu.lbr_mispred)
1647 		static_branch_enable(&x86_lbr_mispred);
1648 	if (x86_pmu.lbr_timed_lbr)
1649 		static_branch_enable(&x86_lbr_cycles);
1650 	if (x86_pmu.lbr_br_type)
1651 		static_branch_enable(&x86_lbr_type);
1652 
1653 	arch_lbr_xsave = is_arch_lbr_xsave_available();
1654 	if (arch_lbr_xsave) {
1655 		size = sizeof(struct x86_perf_task_context_arch_lbr_xsave) +
1656 		       get_lbr_state_size();
1657 		pmu->task_ctx_cache = create_lbr_kmem_cache(size,
1658 							    XSAVE_ALIGNMENT);
1659 	}
1660 
1661 	if (!pmu->task_ctx_cache) {
1662 		arch_lbr_xsave = false;
1663 
1664 		size = sizeof(struct x86_perf_task_context_arch_lbr) +
1665 		       lbr_nr * sizeof(struct lbr_entry);
1666 		pmu->task_ctx_cache = create_lbr_kmem_cache(size, 0);
1667 	}
1668 
1669 	x86_pmu.lbr_from = MSR_ARCH_LBR_FROM_0;
1670 	x86_pmu.lbr_to = MSR_ARCH_LBR_TO_0;
1671 	x86_pmu.lbr_info = MSR_ARCH_LBR_INFO_0;
1672 
1673 	/* LBR callstack requires both CPL and Branch Filtering support */
1674 	if (!x86_pmu.lbr_cpl ||
1675 	    !x86_pmu.lbr_filter ||
1676 	    !x86_pmu.lbr_call_stack)
1677 		arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] = LBR_NOT_SUPP;
1678 
1679 	if (!x86_pmu.lbr_cpl) {
1680 		arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_NOT_SUPP;
1681 		arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_NOT_SUPP;
1682 	} else if (!x86_pmu.lbr_filter) {
1683 		arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_NOT_SUPP;
1684 		arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_NOT_SUPP;
1685 		arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_NOT_SUPP;
1686 		arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_NOT_SUPP;
1687 		arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_NOT_SUPP;
1688 		arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_NOT_SUPP;
1689 		arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_CALL_SHIFT] = LBR_NOT_SUPP;
1690 	}
1691 
1692 	x86_pmu.lbr_ctl_mask = ARCH_LBR_CTL_MASK;
1693 	x86_pmu.lbr_ctl_map  = arch_lbr_ctl_map;
1694 
1695 	if (!x86_pmu.lbr_cpl && !x86_pmu.lbr_filter)
1696 		x86_pmu.lbr_ctl_map = NULL;
1697 
1698 	x86_pmu.lbr_reset = intel_pmu_arch_lbr_reset;
1699 	if (arch_lbr_xsave) {
1700 		x86_pmu.lbr_save = intel_pmu_arch_lbr_xsaves;
1701 		x86_pmu.lbr_restore = intel_pmu_arch_lbr_xrstors;
1702 		x86_pmu.lbr_read = intel_pmu_arch_lbr_read_xsave;
1703 		pr_cont("XSAVE ");
1704 	} else {
1705 		x86_pmu.lbr_save = intel_pmu_arch_lbr_save;
1706 		x86_pmu.lbr_restore = intel_pmu_arch_lbr_restore;
1707 		x86_pmu.lbr_read = intel_pmu_arch_lbr_read;
1708 	}
1709 
1710 	pr_cont("Architectural LBR, ");
1711 
1712 	return;
1713 
1714 clear_arch_lbr:
1715 	setup_clear_cpu_cap(X86_FEATURE_ARCH_LBR);
1716 }
1717 
1718 /**
1719  * x86_perf_get_lbr - get the LBR records information
1720  *
1721  * @lbr: the caller's memory to store the LBR records information
1722  */
x86_perf_get_lbr(struct x86_pmu_lbr * lbr)1723 void x86_perf_get_lbr(struct x86_pmu_lbr *lbr)
1724 {
1725 	lbr->nr = x86_pmu.lbr_nr;
1726 	lbr->from = x86_pmu.lbr_from;
1727 	lbr->to = x86_pmu.lbr_to;
1728 	lbr->info = x86_pmu.lbr_info;
1729 	lbr->has_callstack = x86_pmu_has_lbr_callstack();
1730 }
1731 EXPORT_SYMBOL_GPL(x86_perf_get_lbr);
1732 
1733 struct event_constraint vlbr_constraint =
1734 	__EVENT_CONSTRAINT(INTEL_FIXED_VLBR_EVENT, (1ULL << INTEL_PMC_IDX_FIXED_VLBR),
1735 			  FIXED_EVENT_FLAGS, 1, 0, PERF_X86_EVENT_LBR_SELECT);
1736