bba9939635b6d79796a8ebda74043e27c096ec8a
[linux-2.6-microblaze.git] / arch / x86 / events / intel / lbr.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/perf_event.h>
3 #include <linux/types.h>
4
5 #include <asm/perf_event.h>
6 #include <asm/msr.h>
7 #include <asm/insn.h>
8
9 #include "../perf_event.h"
10
11 static const enum {
12         LBR_EIP_FLAGS           = 1,
13         LBR_TSX                 = 2,
14 } lbr_desc[LBR_FORMAT_MAX_KNOWN + 1] = {
15         [LBR_FORMAT_EIP_FLAGS]  = LBR_EIP_FLAGS,
16         [LBR_FORMAT_EIP_FLAGS2] = LBR_EIP_FLAGS | LBR_TSX,
17 };
18
19 /*
20  * Intel LBR_SELECT bits
21  * Intel Vol3a, April 2011, Section 16.7 Table 16-10
22  *
23  * Hardware branch filter (not available on all CPUs)
24  */
25 #define LBR_KERNEL_BIT          0 /* do not capture at ring0 */
26 #define LBR_USER_BIT            1 /* do not capture at ring > 0 */
27 #define LBR_JCC_BIT             2 /* do not capture conditional branches */
28 #define LBR_REL_CALL_BIT        3 /* do not capture relative calls */
29 #define LBR_IND_CALL_BIT        4 /* do not capture indirect calls */
30 #define LBR_RETURN_BIT          5 /* do not capture near returns */
31 #define LBR_IND_JMP_BIT         6 /* do not capture indirect jumps */
32 #define LBR_REL_JMP_BIT         7 /* do not capture relative jumps */
33 #define LBR_FAR_BIT             8 /* do not capture far branches */
34 #define LBR_CALL_STACK_BIT      9 /* enable call stack */
35
36 /*
37  * Following bit only exists in Linux; we mask it out before writing it to
38  * the actual MSR. But it helps the constraint perf code to understand
39  * that this is a separate configuration.
40  */
41 #define LBR_NO_INFO_BIT        63 /* don't read LBR_INFO. */
42
43 #define LBR_KERNEL      (1 << LBR_KERNEL_BIT)
44 #define LBR_USER        (1 << LBR_USER_BIT)
45 #define LBR_JCC         (1 << LBR_JCC_BIT)
46 #define LBR_REL_CALL    (1 << LBR_REL_CALL_BIT)
47 #define LBR_IND_CALL    (1 << LBR_IND_CALL_BIT)
48 #define LBR_RETURN      (1 << LBR_RETURN_BIT)
49 #define LBR_REL_JMP     (1 << LBR_REL_JMP_BIT)
50 #define LBR_IND_JMP     (1 << LBR_IND_JMP_BIT)
51 #define LBR_FAR         (1 << LBR_FAR_BIT)
52 #define LBR_CALL_STACK  (1 << LBR_CALL_STACK_BIT)
53 #define LBR_NO_INFO     (1ULL << LBR_NO_INFO_BIT)
54
55 #define LBR_PLM (LBR_KERNEL | LBR_USER)
56
57 #define LBR_SEL_MASK    0x3ff   /* valid bits in LBR_SELECT */
58 #define LBR_NOT_SUPP    -1      /* LBR filter not supported */
59 #define LBR_IGN         0       /* ignored */
60
61 #define LBR_ANY          \
62         (LBR_JCC        |\
63          LBR_REL_CALL   |\
64          LBR_IND_CALL   |\
65          LBR_RETURN     |\
66          LBR_REL_JMP    |\
67          LBR_IND_JMP    |\
68          LBR_FAR)
69
70 #define LBR_FROM_FLAG_MISPRED   BIT_ULL(63)
71 #define LBR_FROM_FLAG_IN_TX     BIT_ULL(62)
72 #define LBR_FROM_FLAG_ABORT     BIT_ULL(61)
73
74 #define LBR_FROM_SIGNEXT_2MSB   (BIT_ULL(60) | BIT_ULL(59))
75
76 /*
77  * x86control flow change classification
78  * x86control flow changes include branches, interrupts, traps, faults
79  */
80 enum {
81         X86_BR_NONE             = 0,      /* unknown */
82
83         X86_BR_USER             = 1 << 0, /* branch target is user */
84         X86_BR_KERNEL           = 1 << 1, /* branch target is kernel */
85
86         X86_BR_CALL             = 1 << 2, /* call */
87         X86_BR_RET              = 1 << 3, /* return */
88         X86_BR_SYSCALL          = 1 << 4, /* syscall */
89         X86_BR_SYSRET           = 1 << 5, /* syscall return */
90         X86_BR_INT              = 1 << 6, /* sw interrupt */
91         X86_BR_IRET             = 1 << 7, /* return from interrupt */
92         X86_BR_JCC              = 1 << 8, /* conditional */
93         X86_BR_JMP              = 1 << 9, /* jump */
94         X86_BR_IRQ              = 1 << 10,/* hw interrupt or trap or fault */
95         X86_BR_IND_CALL         = 1 << 11,/* indirect calls */
96         X86_BR_ABORT            = 1 << 12,/* transaction abort */
97         X86_BR_IN_TX            = 1 << 13,/* in transaction */
98         X86_BR_NO_TX            = 1 << 14,/* not in transaction */
99         X86_BR_ZERO_CALL        = 1 << 15,/* zero length call */
100         X86_BR_CALL_STACK       = 1 << 16,/* call stack */
101         X86_BR_IND_JMP          = 1 << 17,/* indirect jump */
102
103         X86_BR_TYPE_SAVE        = 1 << 18,/* indicate to save branch type */
104
105 };
106
107 #define X86_BR_PLM (X86_BR_USER | X86_BR_KERNEL)
108 #define X86_BR_ANYTX (X86_BR_NO_TX | X86_BR_IN_TX)
109
110 #define X86_BR_ANY       \
111         (X86_BR_CALL    |\
112          X86_BR_RET     |\
113          X86_BR_SYSCALL |\
114          X86_BR_SYSRET  |\
115          X86_BR_INT     |\
116          X86_BR_IRET    |\
117          X86_BR_JCC     |\
118          X86_BR_JMP      |\
119          X86_BR_IRQ      |\
120          X86_BR_ABORT    |\
121          X86_BR_IND_CALL |\
122          X86_BR_IND_JMP  |\
123          X86_BR_ZERO_CALL)
124
125 #define X86_BR_ALL (X86_BR_PLM | X86_BR_ANY)
126
127 #define X86_BR_ANY_CALL          \
128         (X86_BR_CALL            |\
129          X86_BR_IND_CALL        |\
130          X86_BR_ZERO_CALL       |\
131          X86_BR_SYSCALL         |\
132          X86_BR_IRQ             |\
133          X86_BR_INT)
134
135 static void intel_pmu_lbr_filter(struct cpu_hw_events *cpuc);
136
137 /*
138  * We only support LBR implementations that have FREEZE_LBRS_ON_PMI
139  * otherwise it becomes near impossible to get a reliable stack.
140  */
141
142 static void __intel_pmu_lbr_enable(bool pmi)
143 {
144         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
145         u64 debugctl, lbr_select = 0, orig_debugctl;
146
147         /*
148          * No need to unfreeze manually, as v4 can do that as part
149          * of the GLOBAL_STATUS ack.
150          */
151         if (pmi && x86_pmu.version >= 4)
152                 return;
153
154         /*
155          * No need to reprogram LBR_SELECT in a PMI, as it
156          * did not change.
157          */
158         if (cpuc->lbr_sel)
159                 lbr_select = cpuc->lbr_sel->config & x86_pmu.lbr_sel_mask;
160         if (!pmi && cpuc->lbr_sel)
161                 wrmsrl(MSR_LBR_SELECT, lbr_select);
162
163         rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
164         orig_debugctl = debugctl;
165         debugctl |= DEBUGCTLMSR_LBR;
166         /*
167          * LBR callstack does not work well with FREEZE_LBRS_ON_PMI.
168          * If FREEZE_LBRS_ON_PMI is set, PMI near call/return instructions
169          * may cause superfluous increase/decrease of LBR_TOS.
170          */
171         if (!(lbr_select & LBR_CALL_STACK))
172                 debugctl |= DEBUGCTLMSR_FREEZE_LBRS_ON_PMI;
173         if (orig_debugctl != debugctl)
174                 wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
175 }
176
177 static void __intel_pmu_lbr_disable(void)
178 {
179         u64 debugctl;
180
181         rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
182         debugctl &= ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI);
183         wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
184 }
185
186 void intel_pmu_lbr_reset_32(void)
187 {
188         int i;
189
190         for (i = 0; i < x86_pmu.lbr_nr; i++)
191                 wrmsrl(x86_pmu.lbr_from + i, 0);
192 }
193
194 void intel_pmu_lbr_reset_64(void)
195 {
196         int i;
197
198         for (i = 0; i < x86_pmu.lbr_nr; i++) {
199                 wrmsrl(x86_pmu.lbr_from + i, 0);
200                 wrmsrl(x86_pmu.lbr_to   + i, 0);
201                 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
202                         wrmsrl(MSR_LBR_INFO_0 + i, 0);
203         }
204 }
205
206 void intel_pmu_lbr_reset(void)
207 {
208         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
209
210         if (!x86_pmu.lbr_nr)
211                 return;
212
213         x86_pmu.lbr_reset();
214
215         cpuc->last_task_ctx = NULL;
216         cpuc->last_log_id = 0;
217 }
218
219 /*
220  * TOS = most recently recorded branch
221  */
222 static inline u64 intel_pmu_lbr_tos(void)
223 {
224         u64 tos;
225
226         rdmsrl(x86_pmu.lbr_tos, tos);
227         return tos;
228 }
229
230 enum {
231         LBR_NONE,
232         LBR_VALID,
233 };
234
235 /*
236  * For formats with LBR_TSX flags (e.g. LBR_FORMAT_EIP_FLAGS2), bits 61:62 in
237  * MSR_LAST_BRANCH_FROM_x are the TSX flags when TSX is supported, but when
238  * TSX is not supported they have no consistent behavior:
239  *
240  *   - For wrmsr(), bits 61:62 are considered part of the sign extension.
241  *   - For HW updates (branch captures) bits 61:62 are always OFF and are not
242  *     part of the sign extension.
243  *
244  * Therefore, if:
245  *
246  *   1) LBR has TSX format
247  *   2) CPU has no TSX support enabled
248  *
249  * ... then any value passed to wrmsr() must be sign extended to 63 bits and any
250  * value from rdmsr() must be converted to have a 61 bits sign extension,
251  * ignoring the TSX flags.
252  */
253 static inline bool lbr_from_signext_quirk_needed(void)
254 {
255         int lbr_format = x86_pmu.intel_cap.lbr_format;
256         bool tsx_support = boot_cpu_has(X86_FEATURE_HLE) ||
257                            boot_cpu_has(X86_FEATURE_RTM);
258
259         return !tsx_support && (lbr_desc[lbr_format] & LBR_TSX);
260 }
261
262 static DEFINE_STATIC_KEY_FALSE(lbr_from_quirk_key);
263
264 /* If quirk is enabled, ensure sign extension is 63 bits: */
265 inline u64 lbr_from_signext_quirk_wr(u64 val)
266 {
267         if (static_branch_unlikely(&lbr_from_quirk_key)) {
268                 /*
269                  * Sign extend into bits 61:62 while preserving bit 63.
270                  *
271                  * Quirk is enabled when TSX is disabled. Therefore TSX bits
272                  * in val are always OFF and must be changed to be sign
273                  * extension bits. Since bits 59:60 are guaranteed to be
274                  * part of the sign extension bits, we can just copy them
275                  * to 61:62.
276                  */
277                 val |= (LBR_FROM_SIGNEXT_2MSB & val) << 2;
278         }
279         return val;
280 }
281
282 /*
283  * If quirk is needed, ensure sign extension is 61 bits:
284  */
285 static u64 lbr_from_signext_quirk_rd(u64 val)
286 {
287         if (static_branch_unlikely(&lbr_from_quirk_key)) {
288                 /*
289                  * Quirk is on when TSX is not enabled. Therefore TSX
290                  * flags must be read as OFF.
291                  */
292                 val &= ~(LBR_FROM_FLAG_IN_TX | LBR_FROM_FLAG_ABORT);
293         }
294         return val;
295 }
296
297 static inline void wrlbr_from(unsigned int idx, u64 val)
298 {
299         val = lbr_from_signext_quirk_wr(val);
300         wrmsrl(x86_pmu.lbr_from + idx, val);
301 }
302
303 static inline void wrlbr_to(unsigned int idx, u64 val)
304 {
305         wrmsrl(x86_pmu.lbr_to + idx, val);
306 }
307
308 static inline u64 rdlbr_from(unsigned int idx)
309 {
310         u64 val;
311
312         rdmsrl(x86_pmu.lbr_from + idx, val);
313
314         return lbr_from_signext_quirk_rd(val);
315 }
316
317 static inline u64 rdlbr_to(unsigned int idx)
318 {
319         u64 val;
320
321         rdmsrl(x86_pmu.lbr_to + idx, val);
322
323         return val;
324 }
325
326 void intel_pmu_lbr_restore(void *ctx)
327 {
328         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
329         struct x86_perf_task_context *task_ctx = ctx;
330         int i;
331         unsigned lbr_idx, mask;
332         u64 tos = task_ctx->tos;
333
334         mask = x86_pmu.lbr_nr - 1;
335         for (i = 0; i < task_ctx->valid_lbrs; i++) {
336                 lbr_idx = (tos - i) & mask;
337                 wrlbr_from(lbr_idx, task_ctx->lbr_from[i]);
338                 wrlbr_to  (lbr_idx, task_ctx->lbr_to[i]);
339
340                 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
341                         wrmsrl(MSR_LBR_INFO_0 + lbr_idx, task_ctx->lbr_info[i]);
342         }
343
344         for (; i < x86_pmu.lbr_nr; i++) {
345                 lbr_idx = (tos - i) & mask;
346                 wrlbr_from(lbr_idx, 0);
347                 wrlbr_to(lbr_idx, 0);
348                 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
349                         wrmsrl(MSR_LBR_INFO_0 + lbr_idx, 0);
350         }
351
352         wrmsrl(x86_pmu.lbr_tos, tos);
353
354         if (cpuc->lbr_select)
355                 wrmsrl(MSR_LBR_SELECT, task_ctx->lbr_sel);
356 }
357
358 static __always_inline bool
359 lbr_is_reset_in_cstate(struct x86_perf_task_context *task_ctx)
360 {
361         return !rdlbr_from(task_ctx->tos);
362 }
363
364 static void __intel_pmu_lbr_restore(struct x86_perf_task_context *task_ctx)
365 {
366         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
367
368         if (task_ctx->opt.lbr_callstack_users == 0 ||
369             task_ctx->opt.lbr_stack_state == LBR_NONE) {
370                 intel_pmu_lbr_reset();
371                 return;
372         }
373
374         /*
375          * Does not restore the LBR registers, if
376          * - No one else touched them, and
377          * - Was not cleared in Cstate
378          */
379         if ((task_ctx == cpuc->last_task_ctx) &&
380             (task_ctx->opt.log_id == cpuc->last_log_id) &&
381             !lbr_is_reset_in_cstate(task_ctx)) {
382                 task_ctx->opt.lbr_stack_state = LBR_NONE;
383                 return;
384         }
385
386         x86_pmu.lbr_restore(task_ctx);
387
388         task_ctx->opt.lbr_stack_state = LBR_NONE;
389 }
390
391 void intel_pmu_lbr_save(void *ctx)
392 {
393         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
394         struct x86_perf_task_context *task_ctx = ctx;
395         unsigned lbr_idx, mask;
396         u64 tos, from;
397         int i;
398
399         mask = x86_pmu.lbr_nr - 1;
400         tos = intel_pmu_lbr_tos();
401         for (i = 0; i < x86_pmu.lbr_nr; i++) {
402                 lbr_idx = (tos - i) & mask;
403                 from = rdlbr_from(lbr_idx);
404                 if (!from)
405                         break;
406                 task_ctx->lbr_from[i] = from;
407                 task_ctx->lbr_to[i]   = rdlbr_to(lbr_idx);
408                 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
409                         rdmsrl(MSR_LBR_INFO_0 + lbr_idx, task_ctx->lbr_info[i]);
410         }
411         task_ctx->valid_lbrs = i;
412         task_ctx->tos = tos;
413
414         if (cpuc->lbr_select)
415                 rdmsrl(MSR_LBR_SELECT, task_ctx->lbr_sel);
416 }
417
418 static void __intel_pmu_lbr_save(struct x86_perf_task_context *task_ctx)
419 {
420         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
421
422         if (task_ctx->opt.lbr_callstack_users == 0) {
423                 task_ctx->opt.lbr_stack_state = LBR_NONE;
424                 return;
425         }
426
427         x86_pmu.lbr_save(task_ctx);
428
429         task_ctx->opt.lbr_stack_state = LBR_VALID;
430
431         cpuc->last_task_ctx = task_ctx;
432         cpuc->last_log_id = ++task_ctx->opt.log_id;
433 }
434
435 void intel_pmu_lbr_swap_task_ctx(struct perf_event_context *prev,
436                                  struct perf_event_context *next)
437 {
438         struct x86_perf_task_context *prev_ctx_data, *next_ctx_data;
439
440         swap(prev->task_ctx_data, next->task_ctx_data);
441
442         /*
443          * Architecture specific synchronization makes sense in
444          * case both prev->task_ctx_data and next->task_ctx_data
445          * pointers are allocated.
446          */
447
448         prev_ctx_data = next->task_ctx_data;
449         next_ctx_data = prev->task_ctx_data;
450
451         if (!prev_ctx_data || !next_ctx_data)
452                 return;
453
454         swap(prev_ctx_data->opt.lbr_callstack_users,
455              next_ctx_data->opt.lbr_callstack_users);
456 }
457
458 void intel_pmu_lbr_sched_task(struct perf_event_context *ctx, bool sched_in)
459 {
460         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
461         struct x86_perf_task_context *task_ctx;
462
463         if (!cpuc->lbr_users)
464                 return;
465
466         /*
467          * If LBR callstack feature is enabled and the stack was saved when
468          * the task was scheduled out, restore the stack. Otherwise flush
469          * the LBR stack.
470          */
471         task_ctx = ctx ? ctx->task_ctx_data : NULL;
472         if (task_ctx) {
473                 if (sched_in)
474                         __intel_pmu_lbr_restore(task_ctx);
475                 else
476                         __intel_pmu_lbr_save(task_ctx);
477                 return;
478         }
479
480         /*
481          * Since a context switch can flip the address space and LBR entries
482          * are not tagged with an identifier, we need to wipe the LBR, even for
483          * per-cpu events. You simply cannot resolve the branches from the old
484          * address space.
485          */
486         if (sched_in)
487                 intel_pmu_lbr_reset();
488 }
489
490 static inline bool branch_user_callstack(unsigned br_sel)
491 {
492         return (br_sel & X86_BR_USER) && (br_sel & X86_BR_CALL_STACK);
493 }
494
495 void intel_pmu_lbr_add(struct perf_event *event)
496 {
497         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
498         struct x86_perf_task_context *task_ctx;
499
500         if (!x86_pmu.lbr_nr)
501                 return;
502
503         if (event->hw.flags & PERF_X86_EVENT_LBR_SELECT)
504                 cpuc->lbr_select = 1;
505
506         cpuc->br_sel = event->hw.branch_reg.reg;
507
508         if (branch_user_callstack(cpuc->br_sel) && event->ctx->task_ctx_data) {
509                 task_ctx = event->ctx->task_ctx_data;
510                 task_ctx->opt.lbr_callstack_users++;
511         }
512
513         /*
514          * Request pmu::sched_task() callback, which will fire inside the
515          * regular perf event scheduling, so that call will:
516          *
517          *  - restore or wipe; when LBR-callstack,
518          *  - wipe; otherwise,
519          *
520          * when this is from __perf_event_task_sched_in().
521          *
522          * However, if this is from perf_install_in_context(), no such callback
523          * will follow and we'll need to reset the LBR here if this is the
524          * first LBR event.
525          *
526          * The problem is, we cannot tell these cases apart... but we can
527          * exclude the biggest chunk of cases by looking at
528          * event->total_time_running. An event that has accrued runtime cannot
529          * be 'new'. Conversely, a new event can get installed through the
530          * context switch path for the first time.
531          */
532         if (x86_pmu.intel_cap.pebs_baseline && event->attr.precise_ip > 0)
533                 cpuc->lbr_pebs_users++;
534         perf_sched_cb_inc(event->ctx->pmu);
535         if (!cpuc->lbr_users++ && !event->total_time_running)
536                 intel_pmu_lbr_reset();
537 }
538
539 void intel_pmu_lbr_del(struct perf_event *event)
540 {
541         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
542         struct x86_perf_task_context *task_ctx;
543
544         if (!x86_pmu.lbr_nr)
545                 return;
546
547         if (branch_user_callstack(cpuc->br_sel) &&
548             event->ctx->task_ctx_data) {
549                 task_ctx = event->ctx->task_ctx_data;
550                 task_ctx->opt.lbr_callstack_users--;
551         }
552
553         if (event->hw.flags & PERF_X86_EVENT_LBR_SELECT)
554                 cpuc->lbr_select = 0;
555
556         if (x86_pmu.intel_cap.pebs_baseline && event->attr.precise_ip > 0)
557                 cpuc->lbr_pebs_users--;
558         cpuc->lbr_users--;
559         WARN_ON_ONCE(cpuc->lbr_users < 0);
560         WARN_ON_ONCE(cpuc->lbr_pebs_users < 0);
561         perf_sched_cb_dec(event->ctx->pmu);
562 }
563
564 static inline bool vlbr_exclude_host(void)
565 {
566         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
567
568         return test_bit(INTEL_PMC_IDX_FIXED_VLBR,
569                 (unsigned long *)&cpuc->intel_ctrl_guest_mask);
570 }
571
572 void intel_pmu_lbr_enable_all(bool pmi)
573 {
574         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
575
576         if (cpuc->lbr_users && !vlbr_exclude_host())
577                 __intel_pmu_lbr_enable(pmi);
578 }
579
580 void intel_pmu_lbr_disable_all(void)
581 {
582         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
583
584         if (cpuc->lbr_users && !vlbr_exclude_host())
585                 __intel_pmu_lbr_disable();
586 }
587
588 void intel_pmu_lbr_read_32(struct cpu_hw_events *cpuc)
589 {
590         unsigned long mask = x86_pmu.lbr_nr - 1;
591         u64 tos = intel_pmu_lbr_tos();
592         int i;
593
594         for (i = 0; i < x86_pmu.lbr_nr; i++) {
595                 unsigned long lbr_idx = (tos - i) & mask;
596                 union {
597                         struct {
598                                 u32 from;
599                                 u32 to;
600                         };
601                         u64     lbr;
602                 } msr_lastbranch;
603
604                 rdmsrl(x86_pmu.lbr_from + lbr_idx, msr_lastbranch.lbr);
605
606                 cpuc->lbr_entries[i].from       = msr_lastbranch.from;
607                 cpuc->lbr_entries[i].to         = msr_lastbranch.to;
608                 cpuc->lbr_entries[i].mispred    = 0;
609                 cpuc->lbr_entries[i].predicted  = 0;
610                 cpuc->lbr_entries[i].in_tx      = 0;
611                 cpuc->lbr_entries[i].abort      = 0;
612                 cpuc->lbr_entries[i].cycles     = 0;
613                 cpuc->lbr_entries[i].type       = 0;
614                 cpuc->lbr_entries[i].reserved   = 0;
615         }
616         cpuc->lbr_stack.nr = i;
617         cpuc->lbr_stack.hw_idx = tos;
618 }
619
620 /*
621  * Due to lack of segmentation in Linux the effective address (offset)
622  * is the same as the linear address, allowing us to merge the LIP and EIP
623  * LBR formats.
624  */
625 void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc)
626 {
627         bool need_info = false, call_stack = false;
628         unsigned long mask = x86_pmu.lbr_nr - 1;
629         int lbr_format = x86_pmu.intel_cap.lbr_format;
630         u64 tos = intel_pmu_lbr_tos();
631         int i;
632         int out = 0;
633         int num = x86_pmu.lbr_nr;
634
635         if (cpuc->lbr_sel) {
636                 need_info = !(cpuc->lbr_sel->config & LBR_NO_INFO);
637                 if (cpuc->lbr_sel->config & LBR_CALL_STACK)
638                         call_stack = true;
639         }
640
641         for (i = 0; i < num; i++) {
642                 unsigned long lbr_idx = (tos - i) & mask;
643                 u64 from, to, mis = 0, pred = 0, in_tx = 0, abort = 0;
644                 int skip = 0;
645                 u16 cycles = 0;
646                 int lbr_flags = lbr_desc[lbr_format];
647
648                 from = rdlbr_from(lbr_idx);
649                 to   = rdlbr_to(lbr_idx);
650
651                 /*
652                  * Read LBR call stack entries
653                  * until invalid entry (0s) is detected.
654                  */
655                 if (call_stack && !from)
656                         break;
657
658                 if (lbr_format == LBR_FORMAT_INFO && need_info) {
659                         u64 info;
660
661                         rdmsrl(MSR_LBR_INFO_0 + lbr_idx, info);
662                         mis = !!(info & LBR_INFO_MISPRED);
663                         pred = !mis;
664                         in_tx = !!(info & LBR_INFO_IN_TX);
665                         abort = !!(info & LBR_INFO_ABORT);
666                         cycles = (info & LBR_INFO_CYCLES);
667                 }
668
669                 if (lbr_format == LBR_FORMAT_TIME) {
670                         mis = !!(from & LBR_FROM_FLAG_MISPRED);
671                         pred = !mis;
672                         skip = 1;
673                         cycles = ((to >> 48) & LBR_INFO_CYCLES);
674
675                         to = (u64)((((s64)to) << 16) >> 16);
676                 }
677
678                 if (lbr_flags & LBR_EIP_FLAGS) {
679                         mis = !!(from & LBR_FROM_FLAG_MISPRED);
680                         pred = !mis;
681                         skip = 1;
682                 }
683                 if (lbr_flags & LBR_TSX) {
684                         in_tx = !!(from & LBR_FROM_FLAG_IN_TX);
685                         abort = !!(from & LBR_FROM_FLAG_ABORT);
686                         skip = 3;
687                 }
688                 from = (u64)((((s64)from) << skip) >> skip);
689
690                 /*
691                  * Some CPUs report duplicated abort records,
692                  * with the second entry not having an abort bit set.
693                  * Skip them here. This loop runs backwards,
694                  * so we need to undo the previous record.
695                  * If the abort just happened outside the window
696                  * the extra entry cannot be removed.
697                  */
698                 if (abort && x86_pmu.lbr_double_abort && out > 0)
699                         out--;
700
701                 cpuc->lbr_entries[out].from      = from;
702                 cpuc->lbr_entries[out].to        = to;
703                 cpuc->lbr_entries[out].mispred   = mis;
704                 cpuc->lbr_entries[out].predicted = pred;
705                 cpuc->lbr_entries[out].in_tx     = in_tx;
706                 cpuc->lbr_entries[out].abort     = abort;
707                 cpuc->lbr_entries[out].cycles    = cycles;
708                 cpuc->lbr_entries[out].type      = 0;
709                 cpuc->lbr_entries[out].reserved  = 0;
710                 out++;
711         }
712         cpuc->lbr_stack.nr = out;
713         cpuc->lbr_stack.hw_idx = tos;
714 }
715
716 void intel_pmu_lbr_read(void)
717 {
718         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
719
720         /*
721          * Don't read when all LBRs users are using adaptive PEBS.
722          *
723          * This could be smarter and actually check the event,
724          * but this simple approach seems to work for now.
725          */
726         if (!cpuc->lbr_users || vlbr_exclude_host() ||
727             cpuc->lbr_users == cpuc->lbr_pebs_users)
728                 return;
729
730         x86_pmu.lbr_read(cpuc);
731
732         intel_pmu_lbr_filter(cpuc);
733 }
734
735 /*
736  * SW filter is used:
737  * - in case there is no HW filter
738  * - in case the HW filter has errata or limitations
739  */
740 static int intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
741 {
742         u64 br_type = event->attr.branch_sample_type;
743         int mask = 0;
744
745         if (br_type & PERF_SAMPLE_BRANCH_USER)
746                 mask |= X86_BR_USER;
747
748         if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
749                 mask |= X86_BR_KERNEL;
750
751         /* we ignore BRANCH_HV here */
752
753         if (br_type & PERF_SAMPLE_BRANCH_ANY)
754                 mask |= X86_BR_ANY;
755
756         if (br_type & PERF_SAMPLE_BRANCH_ANY_CALL)
757                 mask |= X86_BR_ANY_CALL;
758
759         if (br_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
760                 mask |= X86_BR_RET | X86_BR_IRET | X86_BR_SYSRET;
761
762         if (br_type & PERF_SAMPLE_BRANCH_IND_CALL)
763                 mask |= X86_BR_IND_CALL;
764
765         if (br_type & PERF_SAMPLE_BRANCH_ABORT_TX)
766                 mask |= X86_BR_ABORT;
767
768         if (br_type & PERF_SAMPLE_BRANCH_IN_TX)
769                 mask |= X86_BR_IN_TX;
770
771         if (br_type & PERF_SAMPLE_BRANCH_NO_TX)
772                 mask |= X86_BR_NO_TX;
773
774         if (br_type & PERF_SAMPLE_BRANCH_COND)
775                 mask |= X86_BR_JCC;
776
777         if (br_type & PERF_SAMPLE_BRANCH_CALL_STACK) {
778                 if (!x86_pmu_has_lbr_callstack())
779                         return -EOPNOTSUPP;
780                 if (mask & ~(X86_BR_USER | X86_BR_KERNEL))
781                         return -EINVAL;
782                 mask |= X86_BR_CALL | X86_BR_IND_CALL | X86_BR_RET |
783                         X86_BR_CALL_STACK;
784         }
785
786         if (br_type & PERF_SAMPLE_BRANCH_IND_JUMP)
787                 mask |= X86_BR_IND_JMP;
788
789         if (br_type & PERF_SAMPLE_BRANCH_CALL)
790                 mask |= X86_BR_CALL | X86_BR_ZERO_CALL;
791
792         if (br_type & PERF_SAMPLE_BRANCH_TYPE_SAVE)
793                 mask |= X86_BR_TYPE_SAVE;
794
795         /*
796          * stash actual user request into reg, it may
797          * be used by fixup code for some CPU
798          */
799         event->hw.branch_reg.reg = mask;
800         return 0;
801 }
802
803 /*
804  * setup the HW LBR filter
805  * Used only when available, may not be enough to disambiguate
806  * all branches, may need the help of the SW filter
807  */
808 static int intel_pmu_setup_hw_lbr_filter(struct perf_event *event)
809 {
810         struct hw_perf_event_extra *reg;
811         u64 br_type = event->attr.branch_sample_type;
812         u64 mask = 0, v;
813         int i;
814
815         for (i = 0; i < PERF_SAMPLE_BRANCH_MAX_SHIFT; i++) {
816                 if (!(br_type & (1ULL << i)))
817                         continue;
818
819                 v = x86_pmu.lbr_sel_map[i];
820                 if (v == LBR_NOT_SUPP)
821                         return -EOPNOTSUPP;
822
823                 if (v != LBR_IGN)
824                         mask |= v;
825         }
826
827         reg = &event->hw.branch_reg;
828         reg->idx = EXTRA_REG_LBR;
829
830         /*
831          * The first 9 bits (LBR_SEL_MASK) in LBR_SELECT operate
832          * in suppress mode. So LBR_SELECT should be set to
833          * (~mask & LBR_SEL_MASK) | (mask & ~LBR_SEL_MASK)
834          * But the 10th bit LBR_CALL_STACK does not operate
835          * in suppress mode.
836          */
837         reg->config = mask ^ (x86_pmu.lbr_sel_mask & ~LBR_CALL_STACK);
838
839         if ((br_type & PERF_SAMPLE_BRANCH_NO_CYCLES) &&
840             (br_type & PERF_SAMPLE_BRANCH_NO_FLAGS) &&
841             (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO))
842                 reg->config |= LBR_NO_INFO;
843
844         return 0;
845 }
846
847 int intel_pmu_setup_lbr_filter(struct perf_event *event)
848 {
849         int ret = 0;
850
851         /*
852          * no LBR on this PMU
853          */
854         if (!x86_pmu.lbr_nr)
855                 return -EOPNOTSUPP;
856
857         /*
858          * setup SW LBR filter
859          */
860         ret = intel_pmu_setup_sw_lbr_filter(event);
861         if (ret)
862                 return ret;
863
864         /*
865          * setup HW LBR filter, if any
866          */
867         if (x86_pmu.lbr_sel_map)
868                 ret = intel_pmu_setup_hw_lbr_filter(event);
869
870         return ret;
871 }
872
873 /*
874  * return the type of control flow change at address "from"
875  * instruction is not necessarily a branch (in case of interrupt).
876  *
877  * The branch type returned also includes the priv level of the
878  * target of the control flow change (X86_BR_USER, X86_BR_KERNEL).
879  *
880  * If a branch type is unknown OR the instruction cannot be
881  * decoded (e.g., text page not present), then X86_BR_NONE is
882  * returned.
883  */
884 static int branch_type(unsigned long from, unsigned long to, int abort)
885 {
886         struct insn insn;
887         void *addr;
888         int bytes_read, bytes_left;
889         int ret = X86_BR_NONE;
890         int ext, to_plm, from_plm;
891         u8 buf[MAX_INSN_SIZE];
892         int is64 = 0;
893
894         to_plm = kernel_ip(to) ? X86_BR_KERNEL : X86_BR_USER;
895         from_plm = kernel_ip(from) ? X86_BR_KERNEL : X86_BR_USER;
896
897         /*
898          * maybe zero if lbr did not fill up after a reset by the time
899          * we get a PMU interrupt
900          */
901         if (from == 0 || to == 0)
902                 return X86_BR_NONE;
903
904         if (abort)
905                 return X86_BR_ABORT | to_plm;
906
907         if (from_plm == X86_BR_USER) {
908                 /*
909                  * can happen if measuring at the user level only
910                  * and we interrupt in a kernel thread, e.g., idle.
911                  */
912                 if (!current->mm)
913                         return X86_BR_NONE;
914
915                 /* may fail if text not present */
916                 bytes_left = copy_from_user_nmi(buf, (void __user *)from,
917                                                 MAX_INSN_SIZE);
918                 bytes_read = MAX_INSN_SIZE - bytes_left;
919                 if (!bytes_read)
920                         return X86_BR_NONE;
921
922                 addr = buf;
923         } else {
924                 /*
925                  * The LBR logs any address in the IP, even if the IP just
926                  * faulted. This means userspace can control the from address.
927                  * Ensure we don't blindy read any address by validating it is
928                  * a known text address.
929                  */
930                 if (kernel_text_address(from)) {
931                         addr = (void *)from;
932                         /*
933                          * Assume we can get the maximum possible size
934                          * when grabbing kernel data.  This is not
935                          * _strictly_ true since we could possibly be
936                          * executing up next to a memory hole, but
937                          * it is very unlikely to be a problem.
938                          */
939                         bytes_read = MAX_INSN_SIZE;
940                 } else {
941                         return X86_BR_NONE;
942                 }
943         }
944
945         /*
946          * decoder needs to know the ABI especially
947          * on 64-bit systems running 32-bit apps
948          */
949 #ifdef CONFIG_X86_64
950         is64 = kernel_ip((unsigned long)addr) || !test_thread_flag(TIF_IA32);
951 #endif
952         insn_init(&insn, addr, bytes_read, is64);
953         insn_get_opcode(&insn);
954         if (!insn.opcode.got)
955                 return X86_BR_ABORT;
956
957         switch (insn.opcode.bytes[0]) {
958         case 0xf:
959                 switch (insn.opcode.bytes[1]) {
960                 case 0x05: /* syscall */
961                 case 0x34: /* sysenter */
962                         ret = X86_BR_SYSCALL;
963                         break;
964                 case 0x07: /* sysret */
965                 case 0x35: /* sysexit */
966                         ret = X86_BR_SYSRET;
967                         break;
968                 case 0x80 ... 0x8f: /* conditional */
969                         ret = X86_BR_JCC;
970                         break;
971                 default:
972                         ret = X86_BR_NONE;
973                 }
974                 break;
975         case 0x70 ... 0x7f: /* conditional */
976                 ret = X86_BR_JCC;
977                 break;
978         case 0xc2: /* near ret */
979         case 0xc3: /* near ret */
980         case 0xca: /* far ret */
981         case 0xcb: /* far ret */
982                 ret = X86_BR_RET;
983                 break;
984         case 0xcf: /* iret */
985                 ret = X86_BR_IRET;
986                 break;
987         case 0xcc ... 0xce: /* int */
988                 ret = X86_BR_INT;
989                 break;
990         case 0xe8: /* call near rel */
991                 insn_get_immediate(&insn);
992                 if (insn.immediate1.value == 0) {
993                         /* zero length call */
994                         ret = X86_BR_ZERO_CALL;
995                         break;
996                 }
997                 /* fall through */
998         case 0x9a: /* call far absolute */
999                 ret = X86_BR_CALL;
1000                 break;
1001         case 0xe0 ... 0xe3: /* loop jmp */
1002                 ret = X86_BR_JCC;
1003                 break;
1004         case 0xe9 ... 0xeb: /* jmp */
1005                 ret = X86_BR_JMP;
1006                 break;
1007         case 0xff: /* call near absolute, call far absolute ind */
1008                 insn_get_modrm(&insn);
1009                 ext = (insn.modrm.bytes[0] >> 3) & 0x7;
1010                 switch (ext) {
1011                 case 2: /* near ind call */
1012                 case 3: /* far ind call */
1013                         ret = X86_BR_IND_CALL;
1014                         break;
1015                 case 4:
1016                 case 5:
1017                         ret = X86_BR_IND_JMP;
1018                         break;
1019                 }
1020                 break;
1021         default:
1022                 ret = X86_BR_NONE;
1023         }
1024         /*
1025          * interrupts, traps, faults (and thus ring transition) may
1026          * occur on any instructions. Thus, to classify them correctly,
1027          * we need to first look at the from and to priv levels. If they
1028          * are different and to is in the kernel, then it indicates
1029          * a ring transition. If the from instruction is not a ring
1030          * transition instr (syscall, systenter, int), then it means
1031          * it was a irq, trap or fault.
1032          *
1033          * we have no way of detecting kernel to kernel faults.
1034          */
1035         if (from_plm == X86_BR_USER && to_plm == X86_BR_KERNEL
1036             && ret != X86_BR_SYSCALL && ret != X86_BR_INT)
1037                 ret = X86_BR_IRQ;
1038
1039         /*
1040          * branch priv level determined by target as
1041          * is done by HW when LBR_SELECT is implemented
1042          */
1043         if (ret != X86_BR_NONE)
1044                 ret |= to_plm;
1045
1046         return ret;
1047 }
1048
1049 #define X86_BR_TYPE_MAP_MAX     16
1050
1051 static int branch_map[X86_BR_TYPE_MAP_MAX] = {
1052         PERF_BR_CALL,           /* X86_BR_CALL */
1053         PERF_BR_RET,            /* X86_BR_RET */
1054         PERF_BR_SYSCALL,        /* X86_BR_SYSCALL */
1055         PERF_BR_SYSRET,         /* X86_BR_SYSRET */
1056         PERF_BR_UNKNOWN,        /* X86_BR_INT */
1057         PERF_BR_UNKNOWN,        /* X86_BR_IRET */
1058         PERF_BR_COND,           /* X86_BR_JCC */
1059         PERF_BR_UNCOND,         /* X86_BR_JMP */
1060         PERF_BR_UNKNOWN,        /* X86_BR_IRQ */
1061         PERF_BR_IND_CALL,       /* X86_BR_IND_CALL */
1062         PERF_BR_UNKNOWN,        /* X86_BR_ABORT */
1063         PERF_BR_UNKNOWN,        /* X86_BR_IN_TX */
1064         PERF_BR_UNKNOWN,        /* X86_BR_NO_TX */
1065         PERF_BR_CALL,           /* X86_BR_ZERO_CALL */
1066         PERF_BR_UNKNOWN,        /* X86_BR_CALL_STACK */
1067         PERF_BR_IND,            /* X86_BR_IND_JMP */
1068 };
1069
1070 static int
1071 common_branch_type(int type)
1072 {
1073         int i;
1074
1075         type >>= 2; /* skip X86_BR_USER and X86_BR_KERNEL */
1076
1077         if (type) {
1078                 i = __ffs(type);
1079                 if (i < X86_BR_TYPE_MAP_MAX)
1080                         return branch_map[i];
1081         }
1082
1083         return PERF_BR_UNKNOWN;
1084 }
1085
1086 /*
1087  * implement actual branch filter based on user demand.
1088  * Hardware may not exactly satisfy that request, thus
1089  * we need to inspect opcodes. Mismatched branches are
1090  * discarded. Therefore, the number of branches returned
1091  * in PERF_SAMPLE_BRANCH_STACK sample may vary.
1092  */
1093 static void
1094 intel_pmu_lbr_filter(struct cpu_hw_events *cpuc)
1095 {
1096         u64 from, to;
1097         int br_sel = cpuc->br_sel;
1098         int i, j, type;
1099         bool compress = false;
1100
1101         /* if sampling all branches, then nothing to filter */
1102         if (((br_sel & X86_BR_ALL) == X86_BR_ALL) &&
1103             ((br_sel & X86_BR_TYPE_SAVE) != X86_BR_TYPE_SAVE))
1104                 return;
1105
1106         for (i = 0; i < cpuc->lbr_stack.nr; i++) {
1107
1108                 from = cpuc->lbr_entries[i].from;
1109                 to = cpuc->lbr_entries[i].to;
1110
1111                 type = branch_type(from, to, cpuc->lbr_entries[i].abort);
1112                 if (type != X86_BR_NONE && (br_sel & X86_BR_ANYTX)) {
1113                         if (cpuc->lbr_entries[i].in_tx)
1114                                 type |= X86_BR_IN_TX;
1115                         else
1116                                 type |= X86_BR_NO_TX;
1117                 }
1118
1119                 /* if type does not correspond, then discard */
1120                 if (type == X86_BR_NONE || (br_sel & type) != type) {
1121                         cpuc->lbr_entries[i].from = 0;
1122                         compress = true;
1123                 }
1124
1125                 if ((br_sel & X86_BR_TYPE_SAVE) == X86_BR_TYPE_SAVE)
1126                         cpuc->lbr_entries[i].type = common_branch_type(type);
1127         }
1128
1129         if (!compress)
1130                 return;
1131
1132         /* remove all entries with from=0 */
1133         for (i = 0; i < cpuc->lbr_stack.nr; ) {
1134                 if (!cpuc->lbr_entries[i].from) {
1135                         j = i;
1136                         while (++j < cpuc->lbr_stack.nr)
1137                                 cpuc->lbr_entries[j-1] = cpuc->lbr_entries[j];
1138                         cpuc->lbr_stack.nr--;
1139                         if (!cpuc->lbr_entries[i].from)
1140                                 continue;
1141                 }
1142                 i++;
1143         }
1144 }
1145
1146 void intel_pmu_store_pebs_lbrs(struct pebs_lbr *lbr)
1147 {
1148         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1149         int i;
1150
1151         cpuc->lbr_stack.nr = x86_pmu.lbr_nr;
1152
1153         /* Cannot get TOS for large PEBS */
1154         if (cpuc->n_pebs == cpuc->n_large_pebs)
1155                 cpuc->lbr_stack.hw_idx = -1ULL;
1156         else
1157                 cpuc->lbr_stack.hw_idx = intel_pmu_lbr_tos();
1158
1159         for (i = 0; i < x86_pmu.lbr_nr; i++) {
1160                 u64 info = lbr->lbr[i].info;
1161                 struct perf_branch_entry *e = &cpuc->lbr_entries[i];
1162
1163                 e->from         = lbr->lbr[i].from;
1164                 e->to           = lbr->lbr[i].to;
1165                 e->mispred      = !!(info & LBR_INFO_MISPRED);
1166                 e->predicted    = !(info & LBR_INFO_MISPRED);
1167                 e->in_tx        = !!(info & LBR_INFO_IN_TX);
1168                 e->abort        = !!(info & LBR_INFO_ABORT);
1169                 e->cycles       = info & LBR_INFO_CYCLES;
1170                 e->reserved     = 0;
1171         }
1172         intel_pmu_lbr_filter(cpuc);
1173 }
1174
1175 /*
1176  * Map interface branch filters onto LBR filters
1177  */
1178 static const int nhm_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
1179         [PERF_SAMPLE_BRANCH_ANY_SHIFT]          = LBR_ANY,
1180         [PERF_SAMPLE_BRANCH_USER_SHIFT]         = LBR_USER,
1181         [PERF_SAMPLE_BRANCH_KERNEL_SHIFT]       = LBR_KERNEL,
1182         [PERF_SAMPLE_BRANCH_HV_SHIFT]           = LBR_IGN,
1183         [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT]   = LBR_RETURN | LBR_REL_JMP
1184                                                 | LBR_IND_JMP | LBR_FAR,
1185         /*
1186          * NHM/WSM erratum: must include REL_JMP+IND_JMP to get CALL branches
1187          */
1188         [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] =
1189          LBR_REL_CALL | LBR_IND_CALL | LBR_REL_JMP | LBR_IND_JMP | LBR_FAR,
1190         /*
1191          * NHM/WSM erratum: must include IND_JMP to capture IND_CALL
1192          */
1193         [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL | LBR_IND_JMP,
1194         [PERF_SAMPLE_BRANCH_COND_SHIFT]     = LBR_JCC,
1195         [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
1196 };
1197
1198 static const int snb_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
1199         [PERF_SAMPLE_BRANCH_ANY_SHIFT]          = LBR_ANY,
1200         [PERF_SAMPLE_BRANCH_USER_SHIFT]         = LBR_USER,
1201         [PERF_SAMPLE_BRANCH_KERNEL_SHIFT]       = LBR_KERNEL,
1202         [PERF_SAMPLE_BRANCH_HV_SHIFT]           = LBR_IGN,
1203         [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT]   = LBR_RETURN | LBR_FAR,
1204         [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT]     = LBR_REL_CALL | LBR_IND_CALL
1205                                                 | LBR_FAR,
1206         [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT]     = LBR_IND_CALL,
1207         [PERF_SAMPLE_BRANCH_COND_SHIFT]         = LBR_JCC,
1208         [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT]     = LBR_IND_JMP,
1209         [PERF_SAMPLE_BRANCH_CALL_SHIFT]         = LBR_REL_CALL,
1210 };
1211
1212 static const int hsw_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
1213         [PERF_SAMPLE_BRANCH_ANY_SHIFT]          = LBR_ANY,
1214         [PERF_SAMPLE_BRANCH_USER_SHIFT]         = LBR_USER,
1215         [PERF_SAMPLE_BRANCH_KERNEL_SHIFT]       = LBR_KERNEL,
1216         [PERF_SAMPLE_BRANCH_HV_SHIFT]           = LBR_IGN,
1217         [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT]   = LBR_RETURN | LBR_FAR,
1218         [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT]     = LBR_REL_CALL | LBR_IND_CALL
1219                                                 | LBR_FAR,
1220         [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT]     = LBR_IND_CALL,
1221         [PERF_SAMPLE_BRANCH_COND_SHIFT]         = LBR_JCC,
1222         [PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT]   = LBR_REL_CALL | LBR_IND_CALL
1223                                                 | LBR_RETURN | LBR_CALL_STACK,
1224         [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT]     = LBR_IND_JMP,
1225         [PERF_SAMPLE_BRANCH_CALL_SHIFT]         = LBR_REL_CALL,
1226 };
1227
1228 /* core */
1229 void __init intel_pmu_lbr_init_core(void)
1230 {
1231         x86_pmu.lbr_nr     = 4;
1232         x86_pmu.lbr_tos    = MSR_LBR_TOS;
1233         x86_pmu.lbr_from   = MSR_LBR_CORE_FROM;
1234         x86_pmu.lbr_to     = MSR_LBR_CORE_TO;
1235
1236         /*
1237          * SW branch filter usage:
1238          * - compensate for lack of HW filter
1239          */
1240 }
1241
1242 /* nehalem/westmere */
1243 void __init intel_pmu_lbr_init_nhm(void)
1244 {
1245         x86_pmu.lbr_nr     = 16;
1246         x86_pmu.lbr_tos    = MSR_LBR_TOS;
1247         x86_pmu.lbr_from   = MSR_LBR_NHM_FROM;
1248         x86_pmu.lbr_to     = MSR_LBR_NHM_TO;
1249
1250         x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1251         x86_pmu.lbr_sel_map  = nhm_lbr_sel_map;
1252
1253         /*
1254          * SW branch filter usage:
1255          * - workaround LBR_SEL errata (see above)
1256          * - support syscall, sysret capture.
1257          *   That requires LBR_FAR but that means far
1258          *   jmp need to be filtered out
1259          */
1260 }
1261
1262 /* sandy bridge */
1263 void __init intel_pmu_lbr_init_snb(void)
1264 {
1265         x86_pmu.lbr_nr   = 16;
1266         x86_pmu.lbr_tos  = MSR_LBR_TOS;
1267         x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1268         x86_pmu.lbr_to   = MSR_LBR_NHM_TO;
1269
1270         x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1271         x86_pmu.lbr_sel_map  = snb_lbr_sel_map;
1272
1273         /*
1274          * SW branch filter usage:
1275          * - support syscall, sysret capture.
1276          *   That requires LBR_FAR but that means far
1277          *   jmp need to be filtered out
1278          */
1279 }
1280
1281 /* haswell */
1282 void intel_pmu_lbr_init_hsw(void)
1283 {
1284         x86_pmu.lbr_nr   = 16;
1285         x86_pmu.lbr_tos  = MSR_LBR_TOS;
1286         x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1287         x86_pmu.lbr_to   = MSR_LBR_NHM_TO;
1288
1289         x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1290         x86_pmu.lbr_sel_map  = hsw_lbr_sel_map;
1291
1292         if (lbr_from_signext_quirk_needed())
1293                 static_branch_enable(&lbr_from_quirk_key);
1294 }
1295
1296 /* skylake */
1297 __init void intel_pmu_lbr_init_skl(void)
1298 {
1299         x86_pmu.lbr_nr   = 32;
1300         x86_pmu.lbr_tos  = MSR_LBR_TOS;
1301         x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1302         x86_pmu.lbr_to   = MSR_LBR_NHM_TO;
1303
1304         x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1305         x86_pmu.lbr_sel_map  = hsw_lbr_sel_map;
1306
1307         /*
1308          * SW branch filter usage:
1309          * - support syscall, sysret capture.
1310          *   That requires LBR_FAR but that means far
1311          *   jmp need to be filtered out
1312          */
1313 }
1314
1315 /* atom */
1316 void __init intel_pmu_lbr_init_atom(void)
1317 {
1318         /*
1319          * only models starting at stepping 10 seems
1320          * to have an operational LBR which can freeze
1321          * on PMU interrupt
1322          */
1323         if (boot_cpu_data.x86_model == 28
1324             && boot_cpu_data.x86_stepping < 10) {
1325                 pr_cont("LBR disabled due to erratum");
1326                 return;
1327         }
1328
1329         x86_pmu.lbr_nr     = 8;
1330         x86_pmu.lbr_tos    = MSR_LBR_TOS;
1331         x86_pmu.lbr_from   = MSR_LBR_CORE_FROM;
1332         x86_pmu.lbr_to     = MSR_LBR_CORE_TO;
1333
1334         /*
1335          * SW branch filter usage:
1336          * - compensate for lack of HW filter
1337          */
1338 }
1339
1340 /* slm */
1341 void __init intel_pmu_lbr_init_slm(void)
1342 {
1343         x86_pmu.lbr_nr     = 8;
1344         x86_pmu.lbr_tos    = MSR_LBR_TOS;
1345         x86_pmu.lbr_from   = MSR_LBR_CORE_FROM;
1346         x86_pmu.lbr_to     = MSR_LBR_CORE_TO;
1347
1348         x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1349         x86_pmu.lbr_sel_map  = nhm_lbr_sel_map;
1350
1351         /*
1352          * SW branch filter usage:
1353          * - compensate for lack of HW filter
1354          */
1355         pr_cont("8-deep LBR, ");
1356 }
1357
1358 /* Knights Landing */
1359 void intel_pmu_lbr_init_knl(void)
1360 {
1361         x86_pmu.lbr_nr     = 8;
1362         x86_pmu.lbr_tos    = MSR_LBR_TOS;
1363         x86_pmu.lbr_from   = MSR_LBR_NHM_FROM;
1364         x86_pmu.lbr_to     = MSR_LBR_NHM_TO;
1365
1366         x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1367         x86_pmu.lbr_sel_map  = snb_lbr_sel_map;
1368
1369         /* Knights Landing does have MISPREDICT bit */
1370         if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_LIP)
1371                 x86_pmu.intel_cap.lbr_format = LBR_FORMAT_EIP_FLAGS;
1372 }
1373
1374 /**
1375  * x86_perf_get_lbr - get the LBR records information
1376  *
1377  * @lbr: the caller's memory to store the LBR records information
1378  *
1379  * Returns: 0 indicates the LBR info has been successfully obtained
1380  */
1381 int x86_perf_get_lbr(struct x86_pmu_lbr *lbr)
1382 {
1383         int lbr_fmt = x86_pmu.intel_cap.lbr_format;
1384
1385         lbr->nr = x86_pmu.lbr_nr;
1386         lbr->from = x86_pmu.lbr_from;
1387         lbr->to = x86_pmu.lbr_to;
1388         lbr->info = (lbr_fmt == LBR_FORMAT_INFO) ? MSR_LBR_INFO_0 : 0;
1389
1390         return 0;
1391 }
1392 EXPORT_SYMBOL_GPL(x86_perf_get_lbr);
1393
1394 struct event_constraint vlbr_constraint =
1395         __EVENT_CONSTRAINT(INTEL_FIXED_VLBR_EVENT, (1ULL << INTEL_PMC_IDX_FIXED_VLBR),
1396                           FIXED_EVENT_FLAGS, 1, 0, PERF_X86_EVENT_LBR_SELECT);