]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/kernel/cpu/perf_event.h
perf: Disable PERF_SAMPLE_BRANCH_* when not supported
[mirror_ubuntu-artful-kernel.git] / arch / x86 / kernel / cpu / perf_event.h
CommitLineData
de0428a7
KW
1/*
2 * Performance events x86 architecture header
3 *
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2009 Jaswinder Singh Rajput
7 * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
8 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
9 * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com>
10 * Copyright (C) 2009 Google, Inc., Stephane Eranian
11 *
12 * For licencing details see kernel-base/COPYING
13 */
14
15#include <linux/perf_event.h>
16
17/*
18 * | NHM/WSM | SNB |
19 * register -------------------------------
20 * | HT | no HT | HT | no HT |
21 *-----------------------------------------
22 * offcore | core | core | cpu | core |
23 * lbr_sel | core | core | cpu | core |
24 * ld_lat | cpu | core | cpu | core |
25 *-----------------------------------------
26 *
27 * Given that there is a small number of shared regs,
28 * we can pre-allocate their slot in the per-cpu
29 * per-core reg tables.
30 */
31enum extra_reg_type {
32 EXTRA_REG_NONE = -1, /* not used */
33
34 EXTRA_REG_RSP_0 = 0, /* offcore_response_0 */
35 EXTRA_REG_RSP_1 = 1, /* offcore_response_1 */
b36817e8 36 EXTRA_REG_LBR = 2, /* lbr_select */
de0428a7
KW
37
38 EXTRA_REG_MAX /* number of entries needed */
39};
40
41struct event_constraint {
42 union {
43 unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
44 u64 idxmsk64;
45 };
46 u64 code;
47 u64 cmask;
48 int weight;
bc1738f6 49 int overlap;
de0428a7
KW
50};
51
52struct amd_nb {
53 int nb_id; /* NorthBridge id */
54 int refcnt; /* reference count */
55 struct perf_event *owners[X86_PMC_IDX_MAX];
56 struct event_constraint event_constraints[X86_PMC_IDX_MAX];
57};
58
59/* The maximal number of PEBS events: */
60#define MAX_PEBS_EVENTS 4
61
62/*
63 * A debug store configuration.
64 *
65 * We only support architectures that use 64bit fields.
66 */
67struct debug_store {
68 u64 bts_buffer_base;
69 u64 bts_index;
70 u64 bts_absolute_maximum;
71 u64 bts_interrupt_threshold;
72 u64 pebs_buffer_base;
73 u64 pebs_index;
74 u64 pebs_absolute_maximum;
75 u64 pebs_interrupt_threshold;
76 u64 pebs_event_reset[MAX_PEBS_EVENTS];
77};
78
79/*
80 * Per register state.
81 */
82struct er_account {
83 raw_spinlock_t lock; /* per-core: protect structure */
84 u64 config; /* extra MSR config */
85 u64 reg; /* extra MSR number */
86 atomic_t ref; /* reference count */
87};
88
89/*
90 * Per core/cpu state
91 *
92 * Used to coordinate shared registers between HT threads or
93 * among events on a single PMU.
94 */
95struct intel_shared_regs {
96 struct er_account regs[EXTRA_REG_MAX];
97 int refcnt; /* per-core: #HT threads */
98 unsigned core_id; /* per-core: core id */
99};
100
101#define MAX_LBR_ENTRIES 16
102
103struct cpu_hw_events {
104 /*
105 * Generic x86 PMC bits
106 */
107 struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */
108 unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
109 unsigned long running[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
110 int enabled;
111
112 int n_events;
113 int n_added;
114 int n_txn;
115 int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */
116 u64 tags[X86_PMC_IDX_MAX];
117 struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */
118
119 unsigned int group_flag;
120
121 /*
122 * Intel DebugStore bits
123 */
124 struct debug_store *ds;
125 u64 pebs_enabled;
126
127 /*
128 * Intel LBR bits
129 */
130 int lbr_users;
131 void *lbr_context;
132 struct perf_branch_stack lbr_stack;
133 struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES];
b36817e8 134 struct er_account *lbr_sel;
3e702ff6 135 u64 br_sel;
de0428a7 136
144d31e6
GN
137 /*
138 * Intel host/guest exclude bits
139 */
140 u64 intel_ctrl_guest_mask;
141 u64 intel_ctrl_host_mask;
142 struct perf_guest_switch_msr guest_switch_msrs[X86_PMC_IDX_MAX];
143
de0428a7
KW
144 /*
145 * manage shared (per-core, per-cpu) registers
146 * used on Intel NHM/WSM/SNB
147 */
148 struct intel_shared_regs *shared_regs;
149
150 /*
151 * AMD specific bits
152 */
1018faa6
JR
153 struct amd_nb *amd_nb;
154 /* Inverted mask of bits to clear in the perf_ctr ctrl registers */
155 u64 perf_ctr_virt_mask;
de0428a7
KW
156
157 void *kfree_on_online;
158};
159
bc1738f6 160#define __EVENT_CONSTRAINT(c, n, m, w, o) {\
de0428a7
KW
161 { .idxmsk64 = (n) }, \
162 .code = (c), \
163 .cmask = (m), \
164 .weight = (w), \
bc1738f6 165 .overlap = (o), \
de0428a7
KW
166}
167
168#define EVENT_CONSTRAINT(c, n, m) \
bc1738f6
RR
169 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 0)
170
171/*
172 * The overlap flag marks event constraints with overlapping counter
173 * masks. This is the case if the counter mask of such an event is not
174 * a subset of any other counter mask of a constraint with an equal or
175 * higher weight, e.g.:
176 *
177 * c_overlaps = EVENT_CONSTRAINT_OVERLAP(0, 0x09, 0);
178 * c_another1 = EVENT_CONSTRAINT(0, 0x07, 0);
179 * c_another2 = EVENT_CONSTRAINT(0, 0x38, 0);
180 *
181 * The event scheduler may not select the correct counter in the first
182 * cycle because it needs to know which subsequent events will be
183 * scheduled. It may fail to schedule the events then. So we set the
184 * overlap flag for such constraints to give the scheduler a hint which
185 * events to select for counter rescheduling.
186 *
187 * Care must be taken as the rescheduling algorithm is O(n!) which
188 * will increase scheduling cycles for an over-commited system
189 * dramatically. The number of such EVENT_CONSTRAINT_OVERLAP() macros
190 * and its counter masks must be kept at a minimum.
191 */
192#define EVENT_CONSTRAINT_OVERLAP(c, n, m) \
193 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 1)
de0428a7
KW
194
195/*
196 * Constraint on the Event code.
197 */
198#define INTEL_EVENT_CONSTRAINT(c, n) \
199 EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT)
200
201/*
202 * Constraint on the Event code + UMask + fixed-mask
203 *
204 * filter mask to validate fixed counter events.
205 * the following filters disqualify for fixed counters:
206 * - inv
207 * - edge
208 * - cnt-mask
209 * The other filters are supported by fixed counters.
210 * The any-thread option is supported starting with v3.
211 */
212#define FIXED_EVENT_CONSTRAINT(c, n) \
213 EVENT_CONSTRAINT(c, (1ULL << (32+n)), X86_RAW_EVENT_MASK)
214
215/*
216 * Constraint on the Event code + UMask
217 */
218#define INTEL_UEVENT_CONSTRAINT(c, n) \
219 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
220
221#define EVENT_CONSTRAINT_END \
222 EVENT_CONSTRAINT(0, 0, 0)
223
224#define for_each_event_constraint(e, c) \
225 for ((e) = (c); (e)->weight; (e)++)
226
227/*
228 * Extra registers for specific events.
229 *
230 * Some events need large masks and require external MSRs.
231 * Those extra MSRs end up being shared for all events on
232 * a PMU and sometimes between PMU of sibling HT threads.
233 * In either case, the kernel needs to handle conflicting
234 * accesses to those extra, shared, regs. The data structure
235 * to manage those registers is stored in cpu_hw_event.
236 */
237struct extra_reg {
238 unsigned int event;
239 unsigned int msr;
240 u64 config_mask;
241 u64 valid_mask;
242 int idx; /* per_xxx->regs[] reg index */
243};
244
245#define EVENT_EXTRA_REG(e, ms, m, vm, i) { \
246 .event = (e), \
247 .msr = (ms), \
248 .config_mask = (m), \
249 .valid_mask = (vm), \
250 .idx = EXTRA_REG_##i \
251 }
252
253#define INTEL_EVENT_EXTRA_REG(event, msr, vm, idx) \
254 EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm, idx)
255
256#define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0, RSP_0)
257
258union perf_capabilities {
259 struct {
260 u64 lbr_format:6;
261 u64 pebs_trap:1;
262 u64 pebs_arch_reg:1;
263 u64 pebs_format:4;
264 u64 smm_freeze:1;
265 };
266 u64 capabilities;
267};
268
c1d6f42f
PZ
269struct x86_pmu_quirk {
270 struct x86_pmu_quirk *next;
271 void (*func)(void);
272};
273
de0428a7
KW
274/*
275 * struct x86_pmu - generic x86 pmu
276 */
277struct x86_pmu {
278 /*
279 * Generic x86 PMC bits
280 */
281 const char *name;
282 int version;
283 int (*handle_irq)(struct pt_regs *);
284 void (*disable_all)(void);
285 void (*enable_all)(int added);
286 void (*enable)(struct perf_event *);
287 void (*disable)(struct perf_event *);
288 int (*hw_config)(struct perf_event *event);
289 int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign);
290 unsigned eventsel;
291 unsigned perfctr;
292 u64 (*event_map)(int);
293 int max_events;
294 int num_counters;
295 int num_counters_fixed;
296 int cntval_bits;
297 u64 cntval_mask;
ffb871bc
GN
298 union {
299 unsigned long events_maskl;
300 unsigned long events_mask[BITS_TO_LONGS(ARCH_PERFMON_EVENTS_COUNT)];
301 };
302 int events_mask_len;
de0428a7
KW
303 int apic;
304 u64 max_period;
305 struct event_constraint *
306 (*get_event_constraints)(struct cpu_hw_events *cpuc,
307 struct perf_event *event);
308
309 void (*put_event_constraints)(struct cpu_hw_events *cpuc,
310 struct perf_event *event);
311 struct event_constraint *event_constraints;
c1d6f42f 312 struct x86_pmu_quirk *quirks;
de0428a7
KW
313 int perfctr_second_write;
314
0c9d42ed
PZ
315 /*
316 * sysfs attrs
317 */
318 int attr_rdpmc;
319
320 /*
321 * CPU Hotplug hooks
322 */
de0428a7
KW
323 int (*cpu_prepare)(int cpu);
324 void (*cpu_starting)(int cpu);
325 void (*cpu_dying)(int cpu);
326 void (*cpu_dead)(int cpu);
327
328 /*
329 * Intel Arch Perfmon v2+
330 */
331 u64 intel_ctrl;
332 union perf_capabilities intel_cap;
333
334 /*
335 * Intel DebugStore bits
336 */
337 int bts, pebs;
338 int bts_active, pebs_active;
339 int pebs_record_size;
340 void (*drain_pebs)(struct pt_regs *regs);
341 struct event_constraint *pebs_constraints;
342
343 /*
344 * Intel LBR
345 */
346 unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */
347 int lbr_nr; /* hardware stack size */
b36817e8
SE
348 u64 lbr_sel_mask; /* LBR_SELECT valid bits */
349 const int *lbr_sel_map; /* lbr_select mappings */
de0428a7
KW
350
351 /*
352 * Extra registers for events
353 */
354 struct extra_reg *extra_regs;
355 unsigned int er_flags;
144d31e6
GN
356
357 /*
358 * Intel host/guest support (KVM)
359 */
360 struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr);
de0428a7
KW
361};
362
c1d6f42f
PZ
363#define x86_add_quirk(func_) \
364do { \
365 static struct x86_pmu_quirk __quirk __initdata = { \
366 .func = func_, \
367 }; \
368 __quirk.next = x86_pmu.quirks; \
369 x86_pmu.quirks = &__quirk; \
370} while (0)
371
de0428a7
KW
372#define ERF_NO_HT_SHARING 1
373#define ERF_HAS_RSP_1 2
374
375extern struct x86_pmu x86_pmu __read_mostly;
376
377DECLARE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
378
379int x86_perf_event_set_period(struct perf_event *event);
380
381/*
382 * Generalized hw caching related hw_event table, filled
383 * in on a per model basis. A value of 0 means
384 * 'not supported', -1 means 'hw_event makes no sense on
385 * this CPU', any other value means the raw hw_event
386 * ID.
387 */
388
389#define C(x) PERF_COUNT_HW_CACHE_##x
390
391extern u64 __read_mostly hw_cache_event_ids
392 [PERF_COUNT_HW_CACHE_MAX]
393 [PERF_COUNT_HW_CACHE_OP_MAX]
394 [PERF_COUNT_HW_CACHE_RESULT_MAX];
395extern u64 __read_mostly hw_cache_extra_regs
396 [PERF_COUNT_HW_CACHE_MAX]
397 [PERF_COUNT_HW_CACHE_OP_MAX]
398 [PERF_COUNT_HW_CACHE_RESULT_MAX];
399
400u64 x86_perf_event_update(struct perf_event *event);
401
402static inline int x86_pmu_addr_offset(int index)
403{
404 int offset;
405
406 /* offset = X86_FEATURE_PERFCTR_CORE ? index << 1 : index */
407 alternative_io(ASM_NOP2,
408 "shll $1, %%eax",
409 X86_FEATURE_PERFCTR_CORE,
410 "=a" (offset),
411 "a" (index));
412
413 return offset;
414}
415
416static inline unsigned int x86_pmu_config_addr(int index)
417{
418 return x86_pmu.eventsel + x86_pmu_addr_offset(index);
419}
420
421static inline unsigned int x86_pmu_event_addr(int index)
422{
423 return x86_pmu.perfctr + x86_pmu_addr_offset(index);
424}
425
426int x86_setup_perfctr(struct perf_event *event);
427
428int x86_pmu_hw_config(struct perf_event *event);
429
430void x86_pmu_disable_all(void);
431
432static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc,
433 u64 enable_mask)
434{
1018faa6
JR
435 u64 disable_mask = __this_cpu_read(cpu_hw_events.perf_ctr_virt_mask);
436
de0428a7
KW
437 if (hwc->extra_reg.reg)
438 wrmsrl(hwc->extra_reg.reg, hwc->extra_reg.config);
1018faa6 439 wrmsrl(hwc->config_base, (hwc->config | enable_mask) & ~disable_mask);
de0428a7
KW
440}
441
442void x86_pmu_enable_all(int added);
443
444int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign);
445
446void x86_pmu_stop(struct perf_event *event, int flags);
447
448static inline void x86_pmu_disable_event(struct perf_event *event)
449{
450 struct hw_perf_event *hwc = &event->hw;
451
452 wrmsrl(hwc->config_base, hwc->config);
453}
454
455void x86_pmu_enable_event(struct perf_event *event);
456
457int x86_pmu_handle_irq(struct pt_regs *regs);
458
459extern struct event_constraint emptyconstraint;
460
461extern struct event_constraint unconstrained;
462
3e702ff6
SE
463static inline bool kernel_ip(unsigned long ip)
464{
465#ifdef CONFIG_X86_32
466 return ip > PAGE_OFFSET;
467#else
468 return (long)ip < 0;
469#endif
470}
471
de0428a7
KW
472#ifdef CONFIG_CPU_SUP_AMD
473
474int amd_pmu_init(void);
475
476#else /* CONFIG_CPU_SUP_AMD */
477
478static inline int amd_pmu_init(void)
479{
480 return 0;
481}
482
483#endif /* CONFIG_CPU_SUP_AMD */
484
485#ifdef CONFIG_CPU_SUP_INTEL
486
487int intel_pmu_save_and_restart(struct perf_event *event);
488
489struct event_constraint *
490x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event);
491
492struct intel_shared_regs *allocate_shared_regs(int cpu);
493
494int intel_pmu_init(void);
495
496void init_debug_store_on_cpu(int cpu);
497
498void fini_debug_store_on_cpu(int cpu);
499
500void release_ds_buffers(void);
501
502void reserve_ds_buffers(void);
503
504extern struct event_constraint bts_constraint;
505
506void intel_pmu_enable_bts(u64 config);
507
508void intel_pmu_disable_bts(void);
509
510int intel_pmu_drain_bts_buffer(void);
511
512extern struct event_constraint intel_core2_pebs_event_constraints[];
513
514extern struct event_constraint intel_atom_pebs_event_constraints[];
515
516extern struct event_constraint intel_nehalem_pebs_event_constraints[];
517
518extern struct event_constraint intel_westmere_pebs_event_constraints[];
519
520extern struct event_constraint intel_snb_pebs_event_constraints[];
521
522struct event_constraint *intel_pebs_constraints(struct perf_event *event);
523
524void intel_pmu_pebs_enable(struct perf_event *event);
525
526void intel_pmu_pebs_disable(struct perf_event *event);
527
528void intel_pmu_pebs_enable_all(void);
529
530void intel_pmu_pebs_disable_all(void);
531
532void intel_ds_init(void);
533
534void intel_pmu_lbr_reset(void);
535
536void intel_pmu_lbr_enable(struct perf_event *event);
537
538void intel_pmu_lbr_disable(struct perf_event *event);
539
540void intel_pmu_lbr_enable_all(void);
541
542void intel_pmu_lbr_disable_all(void);
543
544void intel_pmu_lbr_read(void);
545
546void intel_pmu_lbr_init_core(void);
547
548void intel_pmu_lbr_init_nhm(void);
549
550void intel_pmu_lbr_init_atom(void);
551
c5cc2cd9
SE
552void intel_pmu_lbr_init_snb(void);
553
60ce0fbd
SE
554int intel_pmu_setup_lbr_filter(struct perf_event *event);
555
de0428a7
KW
556int p4_pmu_init(void);
557
558int p6_pmu_init(void);
559
560#else /* CONFIG_CPU_SUP_INTEL */
561
562static inline void reserve_ds_buffers(void)
563{
564}
565
566static inline void release_ds_buffers(void)
567{
568}
569
570static inline int intel_pmu_init(void)
571{
572 return 0;
573}
574
575static inline struct intel_shared_regs *allocate_shared_regs(int cpu)
576{
577 return NULL;
578}
579
580#endif /* CONFIG_CPU_SUP_INTEL */