1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/perf_event.h>
3 #include <linux/types.h>
5 #include <asm/perf_event.h>
8 #include "../perf_event.h"
11 * Intel LBR_SELECT bits
12 * Intel Vol3a, April 2011, Section 16.7 Table 16-10
14 * Hardware branch filter (not available on all CPUs)
16 #define LBR_KERNEL_BIT 0 /* do not capture at ring0 */
17 #define LBR_USER_BIT 1 /* do not capture at ring > 0 */
18 #define LBR_JCC_BIT 2 /* do not capture conditional branches */
19 #define LBR_REL_CALL_BIT 3 /* do not capture relative calls */
20 #define LBR_IND_CALL_BIT 4 /* do not capture indirect calls */
21 #define LBR_RETURN_BIT 5 /* do not capture near returns */
22 #define LBR_IND_JMP_BIT 6 /* do not capture indirect jumps */
23 #define LBR_REL_JMP_BIT 7 /* do not capture relative jumps */
24 #define LBR_FAR_BIT 8 /* do not capture far branches */
25 #define LBR_CALL_STACK_BIT 9 /* enable call stack */
28 * Following bit only exists in Linux; we mask it out before writing it to
29 * the actual MSR. But it helps the constraint perf code to understand
30 * that this is a separate configuration.
32 #define LBR_NO_INFO_BIT 63 /* don't read LBR_INFO. */
34 #define LBR_KERNEL (1 << LBR_KERNEL_BIT)
35 #define LBR_USER (1 << LBR_USER_BIT)
36 #define LBR_JCC (1 << LBR_JCC_BIT)
37 #define LBR_REL_CALL (1 << LBR_REL_CALL_BIT)
38 #define LBR_IND_CALL (1 << LBR_IND_CALL_BIT)
39 #define LBR_RETURN (1 << LBR_RETURN_BIT)
40 #define LBR_REL_JMP (1 << LBR_REL_JMP_BIT)
41 #define LBR_IND_JMP (1 << LBR_IND_JMP_BIT)
42 #define LBR_FAR (1 << LBR_FAR_BIT)
43 #define LBR_CALL_STACK (1 << LBR_CALL_STACK_BIT)
44 #define LBR_NO_INFO (1ULL << LBR_NO_INFO_BIT)
46 #define LBR_PLM (LBR_KERNEL | LBR_USER)
48 #define LBR_SEL_MASK 0x3ff /* valid bits in LBR_SELECT */
49 #define LBR_NOT_SUPP -1 /* LBR filter not supported */
50 #define LBR_IGN 0 /* ignored */
61 #define LBR_FROM_FLAG_MISPRED BIT_ULL(63)
62 #define LBR_FROM_FLAG_IN_TX BIT_ULL(62)
63 #define LBR_FROM_FLAG_ABORT BIT_ULL(61)
65 #define LBR_FROM_SIGNEXT_2MSB (BIT_ULL(60) | BIT_ULL(59))
70 * Hardware branch filter for Arch LBR
72 #define ARCH_LBR_KERNEL_BIT 1 /* capture at ring0 */
73 #define ARCH_LBR_USER_BIT 2 /* capture at ring > 0 */
74 #define ARCH_LBR_CALL_STACK_BIT 3 /* enable call stack */
75 #define ARCH_LBR_JCC_BIT 16 /* capture conditional branches */
76 #define ARCH_LBR_REL_JMP_BIT 17 /* capture relative jumps */
77 #define ARCH_LBR_IND_JMP_BIT 18 /* capture indirect jumps */
78 #define ARCH_LBR_REL_CALL_BIT 19 /* capture relative calls */
79 #define ARCH_LBR_IND_CALL_BIT 20 /* capture indirect calls */
80 #define ARCH_LBR_RETURN_BIT 21 /* capture near returns */
81 #define ARCH_LBR_OTHER_BRANCH_BIT 22 /* capture other branches */
83 #define ARCH_LBR_KERNEL (1ULL << ARCH_LBR_KERNEL_BIT)
84 #define ARCH_LBR_USER (1ULL << ARCH_LBR_USER_BIT)
85 #define ARCH_LBR_CALL_STACK (1ULL << ARCH_LBR_CALL_STACK_BIT)
86 #define ARCH_LBR_JCC (1ULL << ARCH_LBR_JCC_BIT)
87 #define ARCH_LBR_REL_JMP (1ULL << ARCH_LBR_REL_JMP_BIT)
88 #define ARCH_LBR_IND_JMP (1ULL << ARCH_LBR_IND_JMP_BIT)
89 #define ARCH_LBR_REL_CALL (1ULL << ARCH_LBR_REL_CALL_BIT)
90 #define ARCH_LBR_IND_CALL (1ULL << ARCH_LBR_IND_CALL_BIT)
91 #define ARCH_LBR_RETURN (1ULL << ARCH_LBR_RETURN_BIT)
92 #define ARCH_LBR_OTHER_BRANCH (1ULL << ARCH_LBR_OTHER_BRANCH_BIT)
94 #define ARCH_LBR_ANY \
101 ARCH_LBR_OTHER_BRANCH)
103 #define ARCH_LBR_CTL_MASK 0x7f000e
105 static void intel_pmu_lbr_filter(struct cpu_hw_events *cpuc);
107 static __always_inline bool is_lbr_call_stack_bit_set(u64 config)
109 if (static_cpu_has(X86_FEATURE_ARCH_LBR))
110 return !!(config & ARCH_LBR_CALL_STACK);
112 return !!(config & LBR_CALL_STACK);
116 * We only support LBR implementations that have FREEZE_LBRS_ON_PMI
117 * otherwise it becomes near impossible to get a reliable stack.
120 static void __intel_pmu_lbr_enable(bool pmi)
122 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
123 u64 debugctl, lbr_select = 0, orig_debugctl;
126 * No need to unfreeze manually, as v4 can do that as part
127 * of the GLOBAL_STATUS ack.
129 if (pmi && x86_pmu.version >= 4)
133 * No need to reprogram LBR_SELECT in a PMI, as it
137 lbr_select = cpuc->lbr_sel->config & x86_pmu.lbr_sel_mask;
138 if (!static_cpu_has(X86_FEATURE_ARCH_LBR) && !pmi && cpuc->lbr_sel)
139 wrmsrl(MSR_LBR_SELECT, lbr_select);
141 rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
142 orig_debugctl = debugctl;
144 if (!static_cpu_has(X86_FEATURE_ARCH_LBR))
145 debugctl |= DEBUGCTLMSR_LBR;
147 * LBR callstack does not work well with FREEZE_LBRS_ON_PMI.
148 * If FREEZE_LBRS_ON_PMI is set, PMI near call/return instructions
149 * may cause superfluous increase/decrease of LBR_TOS.
151 if (is_lbr_call_stack_bit_set(lbr_select))
152 debugctl &= ~DEBUGCTLMSR_FREEZE_LBRS_ON_PMI;
154 debugctl |= DEBUGCTLMSR_FREEZE_LBRS_ON_PMI;
156 if (orig_debugctl != debugctl)
157 wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
159 if (static_cpu_has(X86_FEATURE_ARCH_LBR))
160 wrmsrl(MSR_ARCH_LBR_CTL, lbr_select | ARCH_LBR_CTL_LBREN);
163 void intel_pmu_lbr_reset_32(void)
167 for (i = 0; i < x86_pmu.lbr_nr; i++)
168 wrmsrl(x86_pmu.lbr_from + i, 0);
171 void intel_pmu_lbr_reset_64(void)
175 for (i = 0; i < x86_pmu.lbr_nr; i++) {
176 wrmsrl(x86_pmu.lbr_from + i, 0);
177 wrmsrl(x86_pmu.lbr_to + i, 0);
178 if (x86_pmu.lbr_has_info)
179 wrmsrl(x86_pmu.lbr_info + i, 0);
183 static void intel_pmu_arch_lbr_reset(void)
185 /* Write to ARCH_LBR_DEPTH MSR, all LBR entries are reset to 0 */
186 wrmsrl(MSR_ARCH_LBR_DEPTH, x86_pmu.lbr_nr);
189 void intel_pmu_lbr_reset(void)
191 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
198 cpuc->last_task_ctx = NULL;
199 cpuc->last_log_id = 0;
200 if (!static_cpu_has(X86_FEATURE_ARCH_LBR) && cpuc->lbr_select)
201 wrmsrl(MSR_LBR_SELECT, 0);
205 * TOS = most recently recorded branch
207 static inline u64 intel_pmu_lbr_tos(void)
211 rdmsrl(x86_pmu.lbr_tos, tos);
221 * For format LBR_FORMAT_EIP_FLAGS2, bits 61:62 in MSR_LAST_BRANCH_FROM_x
222 * are the TSX flags when TSX is supported, but when TSX is not supported
223 * they have no consistent behavior:
225 * - For wrmsr(), bits 61:62 are considered part of the sign extension.
226 * - For HW updates (branch captures) bits 61:62 are always OFF and are not
227 * part of the sign extension.
231 * 1) LBR format LBR_FORMAT_EIP_FLAGS2
232 * 2) CPU has no TSX support enabled
234 * ... then any value passed to wrmsr() must be sign extended to 63 bits and any
235 * value from rdmsr() must be converted to have a 61 bits sign extension,
236 * ignoring the TSX flags.
238 static inline bool lbr_from_signext_quirk_needed(void)
240 bool tsx_support = boot_cpu_has(X86_FEATURE_HLE) ||
241 boot_cpu_has(X86_FEATURE_RTM);
246 static DEFINE_STATIC_KEY_FALSE(lbr_from_quirk_key);
248 /* If quirk is enabled, ensure sign extension is 63 bits: */
249 inline u64 lbr_from_signext_quirk_wr(u64 val)
251 if (static_branch_unlikely(&lbr_from_quirk_key)) {
253 * Sign extend into bits 61:62 while preserving bit 63.
255 * Quirk is enabled when TSX is disabled. Therefore TSX bits
256 * in val are always OFF and must be changed to be sign
257 * extension bits. Since bits 59:60 are guaranteed to be
258 * part of the sign extension bits, we can just copy them
261 val |= (LBR_FROM_SIGNEXT_2MSB & val) << 2;
267 * If quirk is needed, ensure sign extension is 61 bits:
269 static u64 lbr_from_signext_quirk_rd(u64 val)
271 if (static_branch_unlikely(&lbr_from_quirk_key)) {
273 * Quirk is on when TSX is not enabled. Therefore TSX
274 * flags must be read as OFF.
276 val &= ~(LBR_FROM_FLAG_IN_TX | LBR_FROM_FLAG_ABORT);
281 static __always_inline void wrlbr_from(unsigned int idx, u64 val)
283 val = lbr_from_signext_quirk_wr(val);
284 wrmsrl(x86_pmu.lbr_from + idx, val);
287 static __always_inline void wrlbr_to(unsigned int idx, u64 val)
289 wrmsrl(x86_pmu.lbr_to + idx, val);
292 static __always_inline void wrlbr_info(unsigned int idx, u64 val)
294 wrmsrl(x86_pmu.lbr_info + idx, val);
297 static __always_inline u64 rdlbr_from(unsigned int idx, struct lbr_entry *lbr)
304 rdmsrl(x86_pmu.lbr_from + idx, val);
306 return lbr_from_signext_quirk_rd(val);
309 static __always_inline u64 rdlbr_to(unsigned int idx, struct lbr_entry *lbr)
316 rdmsrl(x86_pmu.lbr_to + idx, val);
321 static __always_inline u64 rdlbr_info(unsigned int idx, struct lbr_entry *lbr)
328 rdmsrl(x86_pmu.lbr_info + idx, val);
334 wrlbr_all(struct lbr_entry *lbr, unsigned int idx, bool need_info)
336 wrlbr_from(idx, lbr->from);
337 wrlbr_to(idx, lbr->to);
339 wrlbr_info(idx, lbr->info);
343 rdlbr_all(struct lbr_entry *lbr, unsigned int idx, bool need_info)
345 u64 from = rdlbr_from(idx, NULL);
347 /* Don't read invalid entry */
352 lbr->to = rdlbr_to(idx, NULL);
354 lbr->info = rdlbr_info(idx, NULL);
359 void intel_pmu_lbr_restore(void *ctx)
361 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
362 struct x86_perf_task_context *task_ctx = ctx;
363 bool need_info = x86_pmu.lbr_has_info;
364 u64 tos = task_ctx->tos;
365 unsigned lbr_idx, mask;
368 mask = x86_pmu.lbr_nr - 1;
369 for (i = 0; i < task_ctx->valid_lbrs; i++) {
370 lbr_idx = (tos - i) & mask;
371 wrlbr_all(&task_ctx->lbr[i], lbr_idx, need_info);
374 for (; i < x86_pmu.lbr_nr; i++) {
375 lbr_idx = (tos - i) & mask;
376 wrlbr_from(lbr_idx, 0);
377 wrlbr_to(lbr_idx, 0);
379 wrlbr_info(lbr_idx, 0);
382 wrmsrl(x86_pmu.lbr_tos, tos);
384 if (cpuc->lbr_select)
385 wrmsrl(MSR_LBR_SELECT, task_ctx->lbr_sel);
388 static void intel_pmu_arch_lbr_restore(void *ctx)
390 struct x86_perf_task_context_arch_lbr *task_ctx = ctx;
391 struct lbr_entry *entries = task_ctx->entries;
394 /* Fast reset the LBRs before restore if the call stack is not full. */
395 if (!entries[x86_pmu.lbr_nr - 1].from)
396 intel_pmu_arch_lbr_reset();
398 for (i = 0; i < x86_pmu.lbr_nr; i++) {
399 if (!entries[i].from)
401 wrlbr_all(&entries[i], i, true);
406 * Restore the Architecture LBR state from the xsave area in the perf
407 * context data for the task via the XRSTORS instruction.
409 static void intel_pmu_arch_lbr_xrstors(void *ctx)
411 struct x86_perf_task_context_arch_lbr_xsave *task_ctx = ctx;
413 xrstors(&task_ctx->xsave, XFEATURE_MASK_LBR);
416 static __always_inline bool lbr_is_reset_in_cstate(void *ctx)
418 if (static_cpu_has(X86_FEATURE_ARCH_LBR))
419 return x86_pmu.lbr_deep_c_reset && !rdlbr_from(0, NULL);
421 return !rdlbr_from(((struct x86_perf_task_context *)ctx)->tos, NULL);
424 static void __intel_pmu_lbr_restore(void *ctx)
426 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
428 if (task_context_opt(ctx)->lbr_callstack_users == 0 ||
429 task_context_opt(ctx)->lbr_stack_state == LBR_NONE) {
430 intel_pmu_lbr_reset();
435 * Does not restore the LBR registers, if
436 * - No one else touched them, and
437 * - Was not cleared in Cstate
439 if ((ctx == cpuc->last_task_ctx) &&
440 (task_context_opt(ctx)->log_id == cpuc->last_log_id) &&
441 !lbr_is_reset_in_cstate(ctx)) {
442 task_context_opt(ctx)->lbr_stack_state = LBR_NONE;
446 x86_pmu.lbr_restore(ctx);
448 task_context_opt(ctx)->lbr_stack_state = LBR_NONE;
451 void intel_pmu_lbr_save(void *ctx)
453 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
454 struct x86_perf_task_context *task_ctx = ctx;
455 bool need_info = x86_pmu.lbr_has_info;
456 unsigned lbr_idx, mask;
460 mask = x86_pmu.lbr_nr - 1;
461 tos = intel_pmu_lbr_tos();
462 for (i = 0; i < x86_pmu.lbr_nr; i++) {
463 lbr_idx = (tos - i) & mask;
464 if (!rdlbr_all(&task_ctx->lbr[i], lbr_idx, need_info))
467 task_ctx->valid_lbrs = i;
470 if (cpuc->lbr_select)
471 rdmsrl(MSR_LBR_SELECT, task_ctx->lbr_sel);
474 static void intel_pmu_arch_lbr_save(void *ctx)
476 struct x86_perf_task_context_arch_lbr *task_ctx = ctx;
477 struct lbr_entry *entries = task_ctx->entries;
480 for (i = 0; i < x86_pmu.lbr_nr; i++) {
481 if (!rdlbr_all(&entries[i], i, true))
485 /* LBR call stack is not full. Reset is required in restore. */
486 if (i < x86_pmu.lbr_nr)
487 entries[x86_pmu.lbr_nr - 1].from = 0;
491 * Save the Architecture LBR state to the xsave area in the perf
492 * context data for the task via the XSAVES instruction.
494 static void intel_pmu_arch_lbr_xsaves(void *ctx)
496 struct x86_perf_task_context_arch_lbr_xsave *task_ctx = ctx;
498 xsaves(&task_ctx->xsave, XFEATURE_MASK_LBR);
501 static void __intel_pmu_lbr_save(void *ctx)
503 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
505 if (task_context_opt(ctx)->lbr_callstack_users == 0) {
506 task_context_opt(ctx)->lbr_stack_state = LBR_NONE;
510 x86_pmu.lbr_save(ctx);
512 task_context_opt(ctx)->lbr_stack_state = LBR_VALID;
514 cpuc->last_task_ctx = ctx;
515 cpuc->last_log_id = ++task_context_opt(ctx)->log_id;
518 void intel_pmu_lbr_swap_task_ctx(struct perf_event_context *prev,
519 struct perf_event_context *next)
521 void *prev_ctx_data, *next_ctx_data;
523 swap(prev->task_ctx_data, next->task_ctx_data);
526 * Architecture specific synchronization makes sense in
527 * case both prev->task_ctx_data and next->task_ctx_data
528 * pointers are allocated.
531 prev_ctx_data = next->task_ctx_data;
532 next_ctx_data = prev->task_ctx_data;
534 if (!prev_ctx_data || !next_ctx_data)
537 swap(task_context_opt(prev_ctx_data)->lbr_callstack_users,
538 task_context_opt(next_ctx_data)->lbr_callstack_users);
541 void intel_pmu_lbr_sched_task(struct perf_event_context *ctx, bool sched_in)
543 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
546 if (!cpuc->lbr_users)
550 * If LBR callstack feature is enabled and the stack was saved when
551 * the task was scheduled out, restore the stack. Otherwise flush
554 task_ctx = ctx ? ctx->task_ctx_data : NULL;
557 __intel_pmu_lbr_restore(task_ctx);
559 __intel_pmu_lbr_save(task_ctx);
564 * Since a context switch can flip the address space and LBR entries
565 * are not tagged with an identifier, we need to wipe the LBR, even for
566 * per-cpu events. You simply cannot resolve the branches from the old
570 intel_pmu_lbr_reset();
573 static inline bool branch_user_callstack(unsigned br_sel)
575 return (br_sel & X86_BR_USER) && (br_sel & X86_BR_CALL_STACK);
578 void intel_pmu_lbr_add(struct perf_event *event)
580 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
585 if (event->hw.flags & PERF_X86_EVENT_LBR_SELECT)
586 cpuc->lbr_select = 1;
588 cpuc->br_sel = event->hw.branch_reg.reg;
590 if (branch_user_callstack(cpuc->br_sel) && event->ctx->task_ctx_data)
591 task_context_opt(event->ctx->task_ctx_data)->lbr_callstack_users++;
594 * Request pmu::sched_task() callback, which will fire inside the
595 * regular perf event scheduling, so that call will:
597 * - restore or wipe; when LBR-callstack,
600 * when this is from __perf_event_task_sched_in().
602 * However, if this is from perf_install_in_context(), no such callback
603 * will follow and we'll need to reset the LBR here if this is the
606 * The problem is, we cannot tell these cases apart... but we can
607 * exclude the biggest chunk of cases by looking at
608 * event->total_time_running. An event that has accrued runtime cannot
609 * be 'new'. Conversely, a new event can get installed through the
610 * context switch path for the first time.
612 if (x86_pmu.intel_cap.pebs_baseline && event->attr.precise_ip > 0)
613 cpuc->lbr_pebs_users++;
614 perf_sched_cb_inc(event->ctx->pmu);
615 if (!cpuc->lbr_users++ && !event->total_time_running)
616 intel_pmu_lbr_reset();
619 void release_lbr_buffers(void)
621 struct kmem_cache *kmem_cache;
622 struct cpu_hw_events *cpuc;
625 if (!static_cpu_has(X86_FEATURE_ARCH_LBR))
628 for_each_possible_cpu(cpu) {
629 cpuc = per_cpu_ptr(&cpu_hw_events, cpu);
630 kmem_cache = x86_get_pmu(cpu)->task_ctx_cache;
631 if (kmem_cache && cpuc->lbr_xsave) {
632 kmem_cache_free(kmem_cache, cpuc->lbr_xsave);
633 cpuc->lbr_xsave = NULL;
638 void reserve_lbr_buffers(void)
640 struct kmem_cache *kmem_cache;
641 struct cpu_hw_events *cpuc;
644 if (!static_cpu_has(X86_FEATURE_ARCH_LBR))
647 for_each_possible_cpu(cpu) {
648 cpuc = per_cpu_ptr(&cpu_hw_events, cpu);
649 kmem_cache = x86_get_pmu(cpu)->task_ctx_cache;
650 if (!kmem_cache || cpuc->lbr_xsave)
653 cpuc->lbr_xsave = kmem_cache_alloc_node(kmem_cache,
654 GFP_KERNEL | __GFP_ZERO,
659 void intel_pmu_lbr_del(struct perf_event *event)
661 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
666 if (branch_user_callstack(cpuc->br_sel) &&
667 event->ctx->task_ctx_data)
668 task_context_opt(event->ctx->task_ctx_data)->lbr_callstack_users--;
670 if (event->hw.flags & PERF_X86_EVENT_LBR_SELECT)
671 cpuc->lbr_select = 0;
673 if (x86_pmu.intel_cap.pebs_baseline && event->attr.precise_ip > 0)
674 cpuc->lbr_pebs_users--;
676 WARN_ON_ONCE(cpuc->lbr_users < 0);
677 WARN_ON_ONCE(cpuc->lbr_pebs_users < 0);
678 perf_sched_cb_dec(event->ctx->pmu);
681 static inline bool vlbr_exclude_host(void)
683 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
685 return test_bit(INTEL_PMC_IDX_FIXED_VLBR,
686 (unsigned long *)&cpuc->intel_ctrl_guest_mask);
689 void intel_pmu_lbr_enable_all(bool pmi)
691 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
693 if (cpuc->lbr_users && !vlbr_exclude_host())
694 __intel_pmu_lbr_enable(pmi);
697 void intel_pmu_lbr_disable_all(void)
699 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
701 if (cpuc->lbr_users && !vlbr_exclude_host()) {
702 if (static_cpu_has(X86_FEATURE_ARCH_LBR))
703 return __intel_pmu_arch_lbr_disable();
705 __intel_pmu_lbr_disable();
709 void intel_pmu_lbr_read_32(struct cpu_hw_events *cpuc)
711 unsigned long mask = x86_pmu.lbr_nr - 1;
712 struct perf_branch_entry *br = cpuc->lbr_entries;
713 u64 tos = intel_pmu_lbr_tos();
716 for (i = 0; i < x86_pmu.lbr_nr; i++) {
717 unsigned long lbr_idx = (tos - i) & mask;
726 rdmsrl(x86_pmu.lbr_from + lbr_idx, msr_lastbranch.lbr);
728 perf_clear_branch_entry_bitfields(br);
730 br->from = msr_lastbranch.from;
731 br->to = msr_lastbranch.to;
734 cpuc->lbr_stack.nr = i;
735 cpuc->lbr_stack.hw_idx = tos;
739 * Due to lack of segmentation in Linux the effective address (offset)
740 * is the same as the linear address, allowing us to merge the LIP and EIP
743 void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc)
745 bool need_info = false, call_stack = false;
746 unsigned long mask = x86_pmu.lbr_nr - 1;
747 struct perf_branch_entry *br = cpuc->lbr_entries;
748 u64 tos = intel_pmu_lbr_tos();
751 int num = x86_pmu.lbr_nr;
754 need_info = !(cpuc->lbr_sel->config & LBR_NO_INFO);
755 if (cpuc->lbr_sel->config & LBR_CALL_STACK)
759 for (i = 0; i < num; i++) {
760 unsigned long lbr_idx = (tos - i) & mask;
761 u64 from, to, mis = 0, pred = 0, in_tx = 0, abort = 0;
764 from = rdlbr_from(lbr_idx, NULL);
765 to = rdlbr_to(lbr_idx, NULL);
768 * Read LBR call stack entries
769 * until invalid entry (0s) is detected.
771 if (call_stack && !from)
774 if (x86_pmu.lbr_has_info) {
778 info = rdlbr_info(lbr_idx, NULL);
779 mis = !!(info & LBR_INFO_MISPRED);
781 cycles = (info & LBR_INFO_CYCLES);
782 if (x86_pmu.lbr_has_tsx) {
783 in_tx = !!(info & LBR_INFO_IN_TX);
784 abort = !!(info & LBR_INFO_ABORT);
790 if (x86_pmu.lbr_from_flags) {
791 mis = !!(from & LBR_FROM_FLAG_MISPRED);
795 if (x86_pmu.lbr_has_tsx) {
796 in_tx = !!(from & LBR_FROM_FLAG_IN_TX);
797 abort = !!(from & LBR_FROM_FLAG_ABORT);
800 from = (u64)((((s64)from) << skip) >> skip);
802 if (x86_pmu.lbr_to_cycles) {
803 cycles = ((to >> 48) & LBR_INFO_CYCLES);
804 to = (u64)((((s64)to) << 16) >> 16);
809 * Some CPUs report duplicated abort records,
810 * with the second entry not having an abort bit set.
811 * Skip them here. This loop runs backwards,
812 * so we need to undo the previous record.
813 * If the abort just happened outside the window
814 * the extra entry cannot be removed.
816 if (abort && x86_pmu.lbr_double_abort && out > 0)
819 perf_clear_branch_entry_bitfields(br+out);
822 br[out].mispred = mis;
823 br[out].predicted = pred;
824 br[out].in_tx = in_tx;
825 br[out].abort = abort;
826 br[out].cycles = cycles;
829 cpuc->lbr_stack.nr = out;
830 cpuc->lbr_stack.hw_idx = tos;
833 static DEFINE_STATIC_KEY_FALSE(x86_lbr_mispred);
834 static DEFINE_STATIC_KEY_FALSE(x86_lbr_cycles);
835 static DEFINE_STATIC_KEY_FALSE(x86_lbr_type);
837 static __always_inline int get_lbr_br_type(u64 info)
841 if (static_branch_likely(&x86_lbr_type))
842 type = (info & LBR_INFO_BR_TYPE) >> LBR_INFO_BR_TYPE_OFFSET;
847 static __always_inline bool get_lbr_mispred(u64 info)
851 if (static_branch_likely(&x86_lbr_mispred))
852 mispred = !!(info & LBR_INFO_MISPRED);
857 static __always_inline u16 get_lbr_cycles(u64 info)
859 u16 cycles = info & LBR_INFO_CYCLES;
861 if (static_cpu_has(X86_FEATURE_ARCH_LBR) &&
862 (!static_branch_likely(&x86_lbr_cycles) ||
863 !(info & LBR_INFO_CYC_CNT_VALID)))
869 static void intel_pmu_store_lbr(struct cpu_hw_events *cpuc,
870 struct lbr_entry *entries)
872 struct perf_branch_entry *e;
873 struct lbr_entry *lbr;
877 for (i = 0; i < x86_pmu.lbr_nr; i++) {
878 lbr = entries ? &entries[i] : NULL;
879 e = &cpuc->lbr_entries[i];
881 from = rdlbr_from(i, lbr);
883 * Read LBR entries until invalid entry (0s) is detected.
888 to = rdlbr_to(i, lbr);
889 info = rdlbr_info(i, lbr);
891 perf_clear_branch_entry_bitfields(e);
895 e->mispred = get_lbr_mispred(info);
896 e->predicted = !e->mispred;
897 e->in_tx = !!(info & LBR_INFO_IN_TX);
898 e->abort = !!(info & LBR_INFO_ABORT);
899 e->cycles = get_lbr_cycles(info);
900 e->type = get_lbr_br_type(info);
903 cpuc->lbr_stack.nr = i;
906 static void intel_pmu_arch_lbr_read(struct cpu_hw_events *cpuc)
908 intel_pmu_store_lbr(cpuc, NULL);
911 static void intel_pmu_arch_lbr_read_xsave(struct cpu_hw_events *cpuc)
913 struct x86_perf_task_context_arch_lbr_xsave *xsave = cpuc->lbr_xsave;
916 intel_pmu_store_lbr(cpuc, NULL);
919 xsaves(&xsave->xsave, XFEATURE_MASK_LBR);
921 intel_pmu_store_lbr(cpuc, xsave->lbr.entries);
924 void intel_pmu_lbr_read(void)
926 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
929 * Don't read when all LBRs users are using adaptive PEBS.
931 * This could be smarter and actually check the event,
932 * but this simple approach seems to work for now.
934 if (!cpuc->lbr_users || vlbr_exclude_host() ||
935 cpuc->lbr_users == cpuc->lbr_pebs_users)
938 x86_pmu.lbr_read(cpuc);
940 intel_pmu_lbr_filter(cpuc);
945 * - in case there is no HW filter
946 * - in case the HW filter has errata or limitations
948 static int intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
950 u64 br_type = event->attr.branch_sample_type;
953 if (br_type & PERF_SAMPLE_BRANCH_USER)
956 if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
957 mask |= X86_BR_KERNEL;
959 /* we ignore BRANCH_HV here */
961 if (br_type & PERF_SAMPLE_BRANCH_ANY)
964 if (br_type & PERF_SAMPLE_BRANCH_ANY_CALL)
965 mask |= X86_BR_ANY_CALL;
967 if (br_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
968 mask |= X86_BR_RET | X86_BR_IRET | X86_BR_SYSRET;
970 if (br_type & PERF_SAMPLE_BRANCH_IND_CALL)
971 mask |= X86_BR_IND_CALL;
973 if (br_type & PERF_SAMPLE_BRANCH_ABORT_TX)
974 mask |= X86_BR_ABORT;
976 if (br_type & PERF_SAMPLE_BRANCH_IN_TX)
977 mask |= X86_BR_IN_TX;
979 if (br_type & PERF_SAMPLE_BRANCH_NO_TX)
980 mask |= X86_BR_NO_TX;
982 if (br_type & PERF_SAMPLE_BRANCH_COND)
985 if (br_type & PERF_SAMPLE_BRANCH_CALL_STACK) {
986 if (!x86_pmu_has_lbr_callstack())
988 if (mask & ~(X86_BR_USER | X86_BR_KERNEL))
990 mask |= X86_BR_CALL | X86_BR_IND_CALL | X86_BR_RET |
994 if (br_type & PERF_SAMPLE_BRANCH_IND_JUMP)
995 mask |= X86_BR_IND_JMP;
997 if (br_type & PERF_SAMPLE_BRANCH_CALL)
998 mask |= X86_BR_CALL | X86_BR_ZERO_CALL;
1000 if (br_type & PERF_SAMPLE_BRANCH_TYPE_SAVE)
1001 mask |= X86_BR_TYPE_SAVE;
1004 * stash actual user request into reg, it may
1005 * be used by fixup code for some CPU
1007 event->hw.branch_reg.reg = mask;
1012 * setup the HW LBR filter
1013 * Used only when available, may not be enough to disambiguate
1014 * all branches, may need the help of the SW filter
1016 static int intel_pmu_setup_hw_lbr_filter(struct perf_event *event)
1018 struct hw_perf_event_extra *reg;
1019 u64 br_type = event->attr.branch_sample_type;
1023 for (i = 0; i < PERF_SAMPLE_BRANCH_MAX_SHIFT; i++) {
1024 if (!(br_type & (1ULL << i)))
1027 v = x86_pmu.lbr_sel_map[i];
1028 if (v == LBR_NOT_SUPP)
1035 reg = &event->hw.branch_reg;
1036 reg->idx = EXTRA_REG_LBR;
1038 if (static_cpu_has(X86_FEATURE_ARCH_LBR)) {
1042 * The Arch LBR HW can retrieve the common branch types
1043 * from the LBR_INFO. It doesn't require the high overhead
1045 * Enable the branch type by default for the Arch LBR.
1047 reg->reg |= X86_BR_TYPE_SAVE;
1052 * The first 9 bits (LBR_SEL_MASK) in LBR_SELECT operate
1053 * in suppress mode. So LBR_SELECT should be set to
1054 * (~mask & LBR_SEL_MASK) | (mask & ~LBR_SEL_MASK)
1055 * But the 10th bit LBR_CALL_STACK does not operate
1058 reg->config = mask ^ (x86_pmu.lbr_sel_mask & ~LBR_CALL_STACK);
1060 if ((br_type & PERF_SAMPLE_BRANCH_NO_CYCLES) &&
1061 (br_type & PERF_SAMPLE_BRANCH_NO_FLAGS) &&
1062 x86_pmu.lbr_has_info)
1063 reg->config |= LBR_NO_INFO;
1068 int intel_pmu_setup_lbr_filter(struct perf_event *event)
1073 * no LBR on this PMU
1075 if (!x86_pmu.lbr_nr)
1079 * setup SW LBR filter
1081 ret = intel_pmu_setup_sw_lbr_filter(event);
1086 * setup HW LBR filter, if any
1088 if (x86_pmu.lbr_sel_map)
1089 ret = intel_pmu_setup_hw_lbr_filter(event);
1095 ARCH_LBR_BR_TYPE_JCC = 0,
1096 ARCH_LBR_BR_TYPE_NEAR_IND_JMP = 1,
1097 ARCH_LBR_BR_TYPE_NEAR_REL_JMP = 2,
1098 ARCH_LBR_BR_TYPE_NEAR_IND_CALL = 3,
1099 ARCH_LBR_BR_TYPE_NEAR_REL_CALL = 4,
1100 ARCH_LBR_BR_TYPE_NEAR_RET = 5,
1101 ARCH_LBR_BR_TYPE_KNOWN_MAX = ARCH_LBR_BR_TYPE_NEAR_RET,
1103 ARCH_LBR_BR_TYPE_MAP_MAX = 16,
1106 static const int arch_lbr_br_type_map[ARCH_LBR_BR_TYPE_MAP_MAX] = {
1107 [ARCH_LBR_BR_TYPE_JCC] = X86_BR_JCC,
1108 [ARCH_LBR_BR_TYPE_NEAR_IND_JMP] = X86_BR_IND_JMP,
1109 [ARCH_LBR_BR_TYPE_NEAR_REL_JMP] = X86_BR_JMP,
1110 [ARCH_LBR_BR_TYPE_NEAR_IND_CALL] = X86_BR_IND_CALL,
1111 [ARCH_LBR_BR_TYPE_NEAR_REL_CALL] = X86_BR_CALL,
1112 [ARCH_LBR_BR_TYPE_NEAR_RET] = X86_BR_RET,
1116 * implement actual branch filter based on user demand.
1117 * Hardware may not exactly satisfy that request, thus
1118 * we need to inspect opcodes. Mismatched branches are
1119 * discarded. Therefore, the number of branches returned
1120 * in PERF_SAMPLE_BRANCH_STACK sample may vary.
1123 intel_pmu_lbr_filter(struct cpu_hw_events *cpuc)
1126 int br_sel = cpuc->br_sel;
1127 int i, j, type, to_plm;
1128 bool compress = false;
1130 /* if sampling all branches, then nothing to filter */
1131 if (((br_sel & X86_BR_ALL) == X86_BR_ALL) &&
1132 ((br_sel & X86_BR_TYPE_SAVE) != X86_BR_TYPE_SAVE))
1135 for (i = 0; i < cpuc->lbr_stack.nr; i++) {
1137 from = cpuc->lbr_entries[i].from;
1138 to = cpuc->lbr_entries[i].to;
1139 type = cpuc->lbr_entries[i].type;
1142 * Parse the branch type recorded in LBR_x_INFO MSR.
1143 * Doesn't support OTHER_BRANCH decoding for now.
1144 * OTHER_BRANCH branch type still rely on software decoding.
1146 if (static_cpu_has(X86_FEATURE_ARCH_LBR) &&
1147 type <= ARCH_LBR_BR_TYPE_KNOWN_MAX) {
1148 to_plm = kernel_ip(to) ? X86_BR_KERNEL : X86_BR_USER;
1149 type = arch_lbr_br_type_map[type] | to_plm;
1151 type = branch_type(from, to, cpuc->lbr_entries[i].abort);
1152 if (type != X86_BR_NONE && (br_sel & X86_BR_ANYTX)) {
1153 if (cpuc->lbr_entries[i].in_tx)
1154 type |= X86_BR_IN_TX;
1156 type |= X86_BR_NO_TX;
1159 /* if type does not correspond, then discard */
1160 if (type == X86_BR_NONE || (br_sel & type) != type) {
1161 cpuc->lbr_entries[i].from = 0;
1165 if ((br_sel & X86_BR_TYPE_SAVE) == X86_BR_TYPE_SAVE)
1166 cpuc->lbr_entries[i].type = common_branch_type(type);
1172 /* remove all entries with from=0 */
1173 for (i = 0; i < cpuc->lbr_stack.nr; ) {
1174 if (!cpuc->lbr_entries[i].from) {
1176 while (++j < cpuc->lbr_stack.nr)
1177 cpuc->lbr_entries[j-1] = cpuc->lbr_entries[j];
1178 cpuc->lbr_stack.nr--;
1179 if (!cpuc->lbr_entries[i].from)
1186 void intel_pmu_store_pebs_lbrs(struct lbr_entry *lbr)
1188 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1190 /* Cannot get TOS for large PEBS and Arch LBR */
1191 if (static_cpu_has(X86_FEATURE_ARCH_LBR) ||
1192 (cpuc->n_pebs == cpuc->n_large_pebs))
1193 cpuc->lbr_stack.hw_idx = -1ULL;
1195 cpuc->lbr_stack.hw_idx = intel_pmu_lbr_tos();
1197 intel_pmu_store_lbr(cpuc, lbr);
1198 intel_pmu_lbr_filter(cpuc);
1202 * Map interface branch filters onto LBR filters
1204 static const int nhm_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
1205 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
1206 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
1207 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
1208 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
1209 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_REL_JMP
1210 | LBR_IND_JMP | LBR_FAR,
1212 * NHM/WSM erratum: must include REL_JMP+IND_JMP to get CALL branches
1214 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] =
1215 LBR_REL_CALL | LBR_IND_CALL | LBR_REL_JMP | LBR_IND_JMP | LBR_FAR,
1217 * NHM/WSM erratum: must include IND_JMP to capture IND_CALL
1219 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL | LBR_IND_JMP,
1220 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
1221 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
1224 static const int snb_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
1225 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
1226 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
1227 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
1228 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
1229 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_FAR,
1230 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
1232 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL,
1233 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
1234 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
1235 [PERF_SAMPLE_BRANCH_CALL_SHIFT] = LBR_REL_CALL,
1238 static const int hsw_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
1239 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
1240 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
1241 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
1242 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
1243 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_FAR,
1244 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
1246 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL,
1247 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
1248 [PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
1249 | LBR_RETURN | LBR_CALL_STACK,
1250 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
1251 [PERF_SAMPLE_BRANCH_CALL_SHIFT] = LBR_REL_CALL,
1254 static int arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
1255 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = ARCH_LBR_ANY,
1256 [PERF_SAMPLE_BRANCH_USER_SHIFT] = ARCH_LBR_USER,
1257 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = ARCH_LBR_KERNEL,
1258 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
1259 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = ARCH_LBR_RETURN |
1260 ARCH_LBR_OTHER_BRANCH,
1261 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = ARCH_LBR_REL_CALL |
1263 ARCH_LBR_OTHER_BRANCH,
1264 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = ARCH_LBR_IND_CALL,
1265 [PERF_SAMPLE_BRANCH_COND_SHIFT] = ARCH_LBR_JCC,
1266 [PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] = ARCH_LBR_REL_CALL |
1269 ARCH_LBR_CALL_STACK,
1270 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = ARCH_LBR_IND_JMP,
1271 [PERF_SAMPLE_BRANCH_CALL_SHIFT] = ARCH_LBR_REL_CALL,
1275 void __init intel_pmu_lbr_init_core(void)
1278 x86_pmu.lbr_tos = MSR_LBR_TOS;
1279 x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
1280 x86_pmu.lbr_to = MSR_LBR_CORE_TO;
1283 * SW branch filter usage:
1284 * - compensate for lack of HW filter
1288 /* nehalem/westmere */
1289 void __init intel_pmu_lbr_init_nhm(void)
1291 x86_pmu.lbr_nr = 16;
1292 x86_pmu.lbr_tos = MSR_LBR_TOS;
1293 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1294 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1296 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1297 x86_pmu.lbr_sel_map = nhm_lbr_sel_map;
1300 * SW branch filter usage:
1301 * - workaround LBR_SEL errata (see above)
1302 * - support syscall, sysret capture.
1303 * That requires LBR_FAR but that means far
1304 * jmp need to be filtered out
1309 void __init intel_pmu_lbr_init_snb(void)
1311 x86_pmu.lbr_nr = 16;
1312 x86_pmu.lbr_tos = MSR_LBR_TOS;
1313 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1314 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1316 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1317 x86_pmu.lbr_sel_map = snb_lbr_sel_map;
1320 * SW branch filter usage:
1321 * - support syscall, sysret capture.
1322 * That requires LBR_FAR but that means far
1323 * jmp need to be filtered out
1327 static inline struct kmem_cache *
1328 create_lbr_kmem_cache(size_t size, size_t align)
1330 return kmem_cache_create("x86_lbr", size, align, 0, NULL);
1334 void intel_pmu_lbr_init_hsw(void)
1336 size_t size = sizeof(struct x86_perf_task_context);
1338 x86_pmu.lbr_nr = 16;
1339 x86_pmu.lbr_tos = MSR_LBR_TOS;
1340 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1341 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1343 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1344 x86_pmu.lbr_sel_map = hsw_lbr_sel_map;
1346 x86_get_pmu(smp_processor_id())->task_ctx_cache = create_lbr_kmem_cache(size, 0);
1350 __init void intel_pmu_lbr_init_skl(void)
1352 size_t size = sizeof(struct x86_perf_task_context);
1354 x86_pmu.lbr_nr = 32;
1355 x86_pmu.lbr_tos = MSR_LBR_TOS;
1356 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1357 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1358 x86_pmu.lbr_info = MSR_LBR_INFO_0;
1360 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1361 x86_pmu.lbr_sel_map = hsw_lbr_sel_map;
1363 x86_get_pmu(smp_processor_id())->task_ctx_cache = create_lbr_kmem_cache(size, 0);
1366 * SW branch filter usage:
1367 * - support syscall, sysret capture.
1368 * That requires LBR_FAR but that means far
1369 * jmp need to be filtered out
1374 void __init intel_pmu_lbr_init_atom(void)
1377 * only models starting at stepping 10 seems
1378 * to have an operational LBR which can freeze
1381 if (boot_cpu_data.x86_model == 28
1382 && boot_cpu_data.x86_stepping < 10) {
1383 pr_cont("LBR disabled due to erratum");
1388 x86_pmu.lbr_tos = MSR_LBR_TOS;
1389 x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
1390 x86_pmu.lbr_to = MSR_LBR_CORE_TO;
1393 * SW branch filter usage:
1394 * - compensate for lack of HW filter
1399 void __init intel_pmu_lbr_init_slm(void)
1402 x86_pmu.lbr_tos = MSR_LBR_TOS;
1403 x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
1404 x86_pmu.lbr_to = MSR_LBR_CORE_TO;
1406 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1407 x86_pmu.lbr_sel_map = nhm_lbr_sel_map;
1410 * SW branch filter usage:
1411 * - compensate for lack of HW filter
1413 pr_cont("8-deep LBR, ");
1416 /* Knights Landing */
1417 void intel_pmu_lbr_init_knl(void)
1420 x86_pmu.lbr_tos = MSR_LBR_TOS;
1421 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1422 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1424 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1425 x86_pmu.lbr_sel_map = snb_lbr_sel_map;
1427 /* Knights Landing does have MISPREDICT bit */
1428 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_LIP)
1429 x86_pmu.intel_cap.lbr_format = LBR_FORMAT_EIP_FLAGS;
1432 void intel_pmu_lbr_init(void)
1434 switch (x86_pmu.intel_cap.lbr_format) {
1435 case LBR_FORMAT_EIP_FLAGS2:
1436 x86_pmu.lbr_has_tsx = 1;
1437 x86_pmu.lbr_from_flags = 1;
1438 if (lbr_from_signext_quirk_needed())
1439 static_branch_enable(&lbr_from_quirk_key);
1442 case LBR_FORMAT_EIP_FLAGS:
1443 x86_pmu.lbr_from_flags = 1;
1446 case LBR_FORMAT_INFO:
1447 x86_pmu.lbr_has_tsx = 1;
1449 case LBR_FORMAT_INFO2:
1450 x86_pmu.lbr_has_info = 1;
1453 case LBR_FORMAT_TIME:
1454 x86_pmu.lbr_from_flags = 1;
1455 x86_pmu.lbr_to_cycles = 1;
1459 if (x86_pmu.lbr_has_info) {
1461 * Only used in combination with baseline pebs.
1463 static_branch_enable(&x86_lbr_mispred);
1464 static_branch_enable(&x86_lbr_cycles);
1469 * LBR state size is variable based on the max number of registers.
1470 * This calculates the expected state size, which should match
1471 * what the hardware enumerates for the size of XFEATURE_LBR.
1473 static inline unsigned int get_lbr_state_size(void)
1475 return sizeof(struct arch_lbr_state) +
1476 x86_pmu.lbr_nr * sizeof(struct lbr_entry);
1479 static bool is_arch_lbr_xsave_available(void)
1481 if (!boot_cpu_has(X86_FEATURE_XSAVES))
1485 * Check the LBR state with the corresponding software structure.
1486 * Disable LBR XSAVES support if the size doesn't match.
1488 if (xfeature_size(XFEATURE_LBR) == 0)
1491 if (WARN_ON(xfeature_size(XFEATURE_LBR) != get_lbr_state_size()))
1497 void __init intel_pmu_arch_lbr_init(void)
1499 struct pmu *pmu = x86_get_pmu(smp_processor_id());
1500 union cpuid28_eax eax;
1501 union cpuid28_ebx ebx;
1502 union cpuid28_ecx ecx;
1503 unsigned int unused_edx;
1504 bool arch_lbr_xsave;
1508 /* Arch LBR Capabilities */
1509 cpuid(28, &eax.full, &ebx.full, &ecx.full, &unused_edx);
1511 lbr_nr = fls(eax.split.lbr_depth_mask) * 8;
1513 goto clear_arch_lbr;
1515 /* Apply the max depth of Arch LBR */
1516 if (wrmsrl_safe(MSR_ARCH_LBR_DEPTH, lbr_nr))
1517 goto clear_arch_lbr;
1519 x86_pmu.lbr_depth_mask = eax.split.lbr_depth_mask;
1520 x86_pmu.lbr_deep_c_reset = eax.split.lbr_deep_c_reset;
1521 x86_pmu.lbr_lip = eax.split.lbr_lip;
1522 x86_pmu.lbr_cpl = ebx.split.lbr_cpl;
1523 x86_pmu.lbr_filter = ebx.split.lbr_filter;
1524 x86_pmu.lbr_call_stack = ebx.split.lbr_call_stack;
1525 x86_pmu.lbr_mispred = ecx.split.lbr_mispred;
1526 x86_pmu.lbr_timed_lbr = ecx.split.lbr_timed_lbr;
1527 x86_pmu.lbr_br_type = ecx.split.lbr_br_type;
1528 x86_pmu.lbr_nr = lbr_nr;
1530 if (x86_pmu.lbr_mispred)
1531 static_branch_enable(&x86_lbr_mispred);
1532 if (x86_pmu.lbr_timed_lbr)
1533 static_branch_enable(&x86_lbr_cycles);
1534 if (x86_pmu.lbr_br_type)
1535 static_branch_enable(&x86_lbr_type);
1537 arch_lbr_xsave = is_arch_lbr_xsave_available();
1538 if (arch_lbr_xsave) {
1539 size = sizeof(struct x86_perf_task_context_arch_lbr_xsave) +
1540 get_lbr_state_size();
1541 pmu->task_ctx_cache = create_lbr_kmem_cache(size,
1545 if (!pmu->task_ctx_cache) {
1546 arch_lbr_xsave = false;
1548 size = sizeof(struct x86_perf_task_context_arch_lbr) +
1549 lbr_nr * sizeof(struct lbr_entry);
1550 pmu->task_ctx_cache = create_lbr_kmem_cache(size, 0);
1553 x86_pmu.lbr_from = MSR_ARCH_LBR_FROM_0;
1554 x86_pmu.lbr_to = MSR_ARCH_LBR_TO_0;
1555 x86_pmu.lbr_info = MSR_ARCH_LBR_INFO_0;
1557 /* LBR callstack requires both CPL and Branch Filtering support */
1558 if (!x86_pmu.lbr_cpl ||
1559 !x86_pmu.lbr_filter ||
1560 !x86_pmu.lbr_call_stack)
1561 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] = LBR_NOT_SUPP;
1563 if (!x86_pmu.lbr_cpl) {
1564 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_NOT_SUPP;
1565 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_NOT_SUPP;
1566 } else if (!x86_pmu.lbr_filter) {
1567 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_NOT_SUPP;
1568 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_NOT_SUPP;
1569 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_NOT_SUPP;
1570 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_NOT_SUPP;
1571 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_NOT_SUPP;
1572 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_NOT_SUPP;
1573 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_CALL_SHIFT] = LBR_NOT_SUPP;
1576 x86_pmu.lbr_ctl_mask = ARCH_LBR_CTL_MASK;
1577 x86_pmu.lbr_ctl_map = arch_lbr_ctl_map;
1579 if (!x86_pmu.lbr_cpl && !x86_pmu.lbr_filter)
1580 x86_pmu.lbr_ctl_map = NULL;
1582 x86_pmu.lbr_reset = intel_pmu_arch_lbr_reset;
1583 if (arch_lbr_xsave) {
1584 x86_pmu.lbr_save = intel_pmu_arch_lbr_xsaves;
1585 x86_pmu.lbr_restore = intel_pmu_arch_lbr_xrstors;
1586 x86_pmu.lbr_read = intel_pmu_arch_lbr_read_xsave;
1589 x86_pmu.lbr_save = intel_pmu_arch_lbr_save;
1590 x86_pmu.lbr_restore = intel_pmu_arch_lbr_restore;
1591 x86_pmu.lbr_read = intel_pmu_arch_lbr_read;
1594 pr_cont("Architectural LBR, ");
1599 setup_clear_cpu_cap(X86_FEATURE_ARCH_LBR);
1603 * x86_perf_get_lbr - get the LBR records information
1605 * @lbr: the caller's memory to store the LBR records information
1607 void x86_perf_get_lbr(struct x86_pmu_lbr *lbr)
1609 int lbr_fmt = x86_pmu.intel_cap.lbr_format;
1611 lbr->nr = x86_pmu.lbr_nr;
1612 lbr->from = x86_pmu.lbr_from;
1613 lbr->to = x86_pmu.lbr_to;
1614 lbr->info = (lbr_fmt == LBR_FORMAT_INFO) ? x86_pmu.lbr_info : 0;
1616 EXPORT_SYMBOL_GPL(x86_perf_get_lbr);
1618 struct event_constraint vlbr_constraint =
1619 __EVENT_CONSTRAINT(INTEL_FIXED_VLBR_EVENT, (1ULL << INTEL_PMC_IDX_FIXED_VLBR),
1620 FIXED_EVENT_FLAGS, 1, 0, PERF_X86_EVENT_LBR_SELECT);