blob: 2ba71ecc244f92b09114a2ead90f7758bf982447 [file] [log] [blame]
Kevin Winchesterde0428a2011-08-30 20:41:05 -03001/*
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
Peter Zijlstra1c2ac3f2012-05-14 15:25:34 +020017#if 0
18#undef wrmsrl
19#define wrmsrl(msr, val) \
20do { \
21 unsigned int _msr = (msr); \
22 u64 _val = (val); \
23 trace_printk("wrmsrl(%x, %Lx)\n", (unsigned int)(_msr), \
24 (unsigned long long)(_val)); \
25 native_write_msr((_msr), (u32)(_val), (u32)(_val >> 32)); \
26} while (0)
27#endif
28
Kevin Winchesterde0428a2011-08-30 20:41:05 -030029/*
30 * | NHM/WSM | SNB |
31 * register -------------------------------
32 * | HT | no HT | HT | no HT |
33 *-----------------------------------------
34 * offcore | core | core | cpu | core |
35 * lbr_sel | core | core | cpu | core |
36 * ld_lat | cpu | core | cpu | core |
37 *-----------------------------------------
38 *
39 * Given that there is a small number of shared regs,
40 * we can pre-allocate their slot in the per-cpu
41 * per-core reg tables.
42 */
43enum extra_reg_type {
44 EXTRA_REG_NONE = -1, /* not used */
45
46 EXTRA_REG_RSP_0 = 0, /* offcore_response_0 */
47 EXTRA_REG_RSP_1 = 1, /* offcore_response_1 */
Stephane Eranianb36817e2012-02-09 23:20:53 +010048 EXTRA_REG_LBR = 2, /* lbr_select */
Stephane Eranianf20093e2013-01-24 16:10:32 +010049 EXTRA_REG_LDLAT = 3, /* ld_lat_threshold */
Kevin Winchesterde0428a2011-08-30 20:41:05 -030050
51 EXTRA_REG_MAX /* number of entries needed */
52};
53
54struct event_constraint {
55 union {
56 unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
57 u64 idxmsk64;
58 };
59 u64 code;
60 u64 cmask;
61 int weight;
Robert Richterbc1738f2011-11-18 12:35:22 +010062 int overlap;
Stephane Eranian9fac2cf2013-01-24 16:10:27 +010063 int flags;
Kevin Winchesterde0428a2011-08-30 20:41:05 -030064};
Stephane Eranianf20093e2013-01-24 16:10:32 +010065/*
Stephane Eranian2f7f73a2013-06-20 18:42:54 +020066 * struct hw_perf_event.flags flags
Stephane Eranianf20093e2013-01-24 16:10:32 +010067 */
68#define PERF_X86_EVENT_PEBS_LDLAT 0x1 /* ld+ldlat data address sampling */
Stephane Eranian9ad64c02013-01-24 16:10:34 +010069#define PERF_X86_EVENT_PEBS_ST 0x2 /* st data address sampling */
Andi Kleen86a04462014-08-11 21:27:10 +020070#define PERF_X86_EVENT_PEBS_ST_HSW 0x4 /* haswell style datala, store */
Stephane Eranian2f7f73a2013-06-20 18:42:54 +020071#define PERF_X86_EVENT_COMMITTED 0x8 /* event passed commit_txn */
Andi Kleen86a04462014-08-11 21:27:10 +020072#define PERF_X86_EVENT_PEBS_LD_HSW 0x10 /* haswell style datala, load */
73#define PERF_X86_EVENT_PEBS_NA_HSW 0x20 /* haswell style datala, unknown */
Maria Dimakopoulou6f6539c2014-11-17 20:06:57 +010074#define PERF_X86_EVENT_EXCL 0x40 /* HT exclusivity on counter */
Maria Dimakopouloue9791212014-11-17 20:06:58 +010075#define PERF_X86_EVENT_DYNAMIC 0x80 /* dynamic alloc'd constraint */
Andy Lutomirski7911d3f2014-10-24 15:58:12 -070076#define PERF_X86_EVENT_RDPMC_ALLOWED 0x40 /* grant rdpmc permission */
77
Kevin Winchesterde0428a2011-08-30 20:41:05 -030078
79struct amd_nb {
80 int nb_id; /* NorthBridge id */
81 int refcnt; /* reference count */
82 struct perf_event *owners[X86_PMC_IDX_MAX];
83 struct event_constraint event_constraints[X86_PMC_IDX_MAX];
84};
85
86/* The maximal number of PEBS events: */
Andi Kleen70ab7002012-06-05 17:56:48 -070087#define MAX_PEBS_EVENTS 8
Kevin Winchesterde0428a2011-08-30 20:41:05 -030088
89/*
90 * A debug store configuration.
91 *
92 * We only support architectures that use 64bit fields.
93 */
94struct debug_store {
95 u64 bts_buffer_base;
96 u64 bts_index;
97 u64 bts_absolute_maximum;
98 u64 bts_interrupt_threshold;
99 u64 pebs_buffer_base;
100 u64 pebs_index;
101 u64 pebs_absolute_maximum;
102 u64 pebs_interrupt_threshold;
103 u64 pebs_event_reset[MAX_PEBS_EVENTS];
104};
105
106/*
107 * Per register state.
108 */
109struct er_account {
110 raw_spinlock_t lock; /* per-core: protect structure */
111 u64 config; /* extra MSR config */
112 u64 reg; /* extra MSR number */
113 atomic_t ref; /* reference count */
114};
115
116/*
117 * Per core/cpu state
118 *
119 * Used to coordinate shared registers between HT threads or
120 * among events on a single PMU.
121 */
122struct intel_shared_regs {
123 struct er_account regs[EXTRA_REG_MAX];
124 int refcnt; /* per-core: #HT threads */
125 unsigned core_id; /* per-core: core id */
126};
127
Maria Dimakopoulou6f6539c2014-11-17 20:06:57 +0100128enum intel_excl_state_type {
129 INTEL_EXCL_UNUSED = 0, /* counter is unused */
130 INTEL_EXCL_SHARED = 1, /* counter can be used by both threads */
131 INTEL_EXCL_EXCLUSIVE = 2, /* counter can be used by one thread only */
132};
133
134struct intel_excl_states {
135 enum intel_excl_state_type init_state[X86_PMC_IDX_MAX];
136 enum intel_excl_state_type state[X86_PMC_IDX_MAX];
Maria Dimakopouloue9791212014-11-17 20:06:58 +0100137 bool sched_started; /* true if scheduling has started */
Maria Dimakopoulou6f6539c2014-11-17 20:06:57 +0100138};
139
140struct intel_excl_cntrs {
141 raw_spinlock_t lock;
142
143 struct intel_excl_states states[2];
144
145 int refcnt; /* per-core: #HT threads */
146 unsigned core_id; /* per-core: core id */
147};
148
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300149#define MAX_LBR_ENTRIES 16
150
Stephane Eranian90413462014-11-17 20:06:54 +0100151enum {
152 X86_PERF_KFREE_SHARED = 0,
153 X86_PERF_KFREE_EXCL = 1,
154 X86_PERF_KFREE_MAX
155};
156
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300157struct cpu_hw_events {
158 /*
159 * Generic x86 PMC bits
160 */
161 struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */
162 unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
163 unsigned long running[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
164 int enabled;
165
Peter Zijlstrac347a2f2014-02-24 12:26:21 +0100166 int n_events; /* the # of events in the below arrays */
167 int n_added; /* the # last events in the below arrays;
168 they've never been enabled yet */
169 int n_txn; /* the # last events in the below arrays;
170 added in the current transaction */
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300171 int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */
172 u64 tags[X86_PMC_IDX_MAX];
173 struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */
174
175 unsigned int group_flag;
Peter Zijlstra5a4252942012-06-05 15:30:31 +0200176 int is_fake;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300177
178 /*
179 * Intel DebugStore bits
180 */
181 struct debug_store *ds;
182 u64 pebs_enabled;
183
184 /*
185 * Intel LBR bits
186 */
187 int lbr_users;
188 void *lbr_context;
189 struct perf_branch_stack lbr_stack;
190 struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES];
Stephane Eranianb36817e2012-02-09 23:20:53 +0100191 struct er_account *lbr_sel;
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100192 u64 br_sel;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300193
194 /*
Gleb Natapov144d31e2011-10-05 14:01:21 +0200195 * Intel host/guest exclude bits
196 */
197 u64 intel_ctrl_guest_mask;
198 u64 intel_ctrl_host_mask;
199 struct perf_guest_switch_msr guest_switch_msrs[X86_PMC_IDX_MAX];
200
201 /*
Peter Zijlstra2b9e3442013-09-12 12:53:44 +0200202 * Intel checkpoint mask
203 */
204 u64 intel_cp_status;
205
206 /*
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300207 * manage shared (per-core, per-cpu) registers
208 * used on Intel NHM/WSM/SNB
209 */
210 struct intel_shared_regs *shared_regs;
Maria Dimakopoulou6f6539c2014-11-17 20:06:57 +0100211 /*
212 * manage exclusive counter access between hyperthread
213 */
214 struct event_constraint *constraint_list; /* in enable order */
215 struct intel_excl_cntrs *excl_cntrs;
216 int excl_thread_id; /* 0 or 1 */
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300217
218 /*
219 * AMD specific bits
220 */
Joerg Roedel1018faa2012-02-29 14:57:32 +0100221 struct amd_nb *amd_nb;
222 /* Inverted mask of bits to clear in the perf_ctr ctrl registers */
223 u64 perf_ctr_virt_mask;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300224
Stephane Eranian90413462014-11-17 20:06:54 +0100225 void *kfree_on_online[X86_PERF_KFREE_MAX];
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300226};
227
Stephane Eranian9fac2cf2013-01-24 16:10:27 +0100228#define __EVENT_CONSTRAINT(c, n, m, w, o, f) {\
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300229 { .idxmsk64 = (n) }, \
230 .code = (c), \
231 .cmask = (m), \
232 .weight = (w), \
Robert Richterbc1738f2011-11-18 12:35:22 +0100233 .overlap = (o), \
Stephane Eranian9fac2cf2013-01-24 16:10:27 +0100234 .flags = f, \
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300235}
236
237#define EVENT_CONSTRAINT(c, n, m) \
Stephane Eranian9fac2cf2013-01-24 16:10:27 +0100238 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 0, 0)
Robert Richterbc1738f2011-11-18 12:35:22 +0100239
Maria Dimakopoulou6f6539c2014-11-17 20:06:57 +0100240#define INTEL_EXCLEVT_CONSTRAINT(c, n) \
241 __EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT, HWEIGHT(n),\
242 0, PERF_X86_EVENT_EXCL)
243
Robert Richterbc1738f2011-11-18 12:35:22 +0100244/*
245 * The overlap flag marks event constraints with overlapping counter
246 * masks. This is the case if the counter mask of such an event is not
247 * a subset of any other counter mask of a constraint with an equal or
248 * higher weight, e.g.:
249 *
250 * c_overlaps = EVENT_CONSTRAINT_OVERLAP(0, 0x09, 0);
251 * c_another1 = EVENT_CONSTRAINT(0, 0x07, 0);
252 * c_another2 = EVENT_CONSTRAINT(0, 0x38, 0);
253 *
254 * The event scheduler may not select the correct counter in the first
255 * cycle because it needs to know which subsequent events will be
256 * scheduled. It may fail to schedule the events then. So we set the
257 * overlap flag for such constraints to give the scheduler a hint which
258 * events to select for counter rescheduling.
259 *
260 * Care must be taken as the rescheduling algorithm is O(n!) which
261 * will increase scheduling cycles for an over-commited system
262 * dramatically. The number of such EVENT_CONSTRAINT_OVERLAP() macros
263 * and its counter masks must be kept at a minimum.
264 */
265#define EVENT_CONSTRAINT_OVERLAP(c, n, m) \
Stephane Eranian9fac2cf2013-01-24 16:10:27 +0100266 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 1, 0)
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300267
268/*
269 * Constraint on the Event code.
270 */
271#define INTEL_EVENT_CONSTRAINT(c, n) \
272 EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT)
273
274/*
275 * Constraint on the Event code + UMask + fixed-mask
276 *
277 * filter mask to validate fixed counter events.
278 * the following filters disqualify for fixed counters:
279 * - inv
280 * - edge
281 * - cnt-mask
Andi Kleen3a632cb2013-06-17 17:36:48 -0700282 * - in_tx
283 * - in_tx_checkpointed
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300284 * The other filters are supported by fixed counters.
285 * The any-thread option is supported starting with v3.
286 */
Andi Kleen3a632cb2013-06-17 17:36:48 -0700287#define FIXED_EVENT_FLAGS (X86_RAW_EVENT_MASK|HSW_IN_TX|HSW_IN_TX_CHECKPOINTED)
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300288#define FIXED_EVENT_CONSTRAINT(c, n) \
Andi Kleen3a632cb2013-06-17 17:36:48 -0700289 EVENT_CONSTRAINT(c, (1ULL << (32+n)), FIXED_EVENT_FLAGS)
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300290
291/*
292 * Constraint on the Event code + UMask
293 */
294#define INTEL_UEVENT_CONSTRAINT(c, n) \
295 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
296
Andi Kleen7550ddf2014-09-24 07:34:46 -0700297/* Like UEVENT_CONSTRAINT, but match flags too */
298#define INTEL_FLAGS_UEVENT_CONSTRAINT(c, n) \
299 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS)
300
Maria Dimakopouloue9791212014-11-17 20:06:58 +0100301#define INTEL_EXCLUEVT_CONSTRAINT(c, n) \
302 __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK, \
303 HWEIGHT(n), 0, PERF_X86_EVENT_EXCL)
304
Stephane Eranianf20093e2013-01-24 16:10:32 +0100305#define INTEL_PLD_CONSTRAINT(c, n) \
Andi Kleen86a04462014-08-11 21:27:10 +0200306 __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
Stephane Eranianf20093e2013-01-24 16:10:32 +0100307 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LDLAT)
308
Stephane Eranian9ad64c02013-01-24 16:10:34 +0100309#define INTEL_PST_CONSTRAINT(c, n) \
Andi Kleen86a04462014-08-11 21:27:10 +0200310 __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
Stephane Eranian9ad64c02013-01-24 16:10:34 +0100311 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST)
312
Andi Kleen86a04462014-08-11 21:27:10 +0200313/* Event constraint, but match on all event flags too. */
314#define INTEL_FLAGS_EVENT_CONSTRAINT(c, n) \
315 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS)
316
317/* Check only flags, but allow all event/umask */
318#define INTEL_ALL_EVENT_CONSTRAINT(code, n) \
319 EVENT_CONSTRAINT(code, n, X86_ALL_EVENT_FLAGS)
320
321/* Check flags and event code, and set the HSW store flag */
322#define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_ST(code, n) \
323 __EVENT_CONSTRAINT(code, n, \
324 ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
Andi Kleenf9134f32013-06-17 17:36:52 -0700325 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST_HSW)
326
Andi Kleen86a04462014-08-11 21:27:10 +0200327/* Check flags and event code, and set the HSW load flag */
328#define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(code, n) \
Maria Dimakopouloub63b4b42014-11-17 20:07:00 +0100329 __EVENT_CONSTRAINT(code, n, \
Andi Kleen86a04462014-08-11 21:27:10 +0200330 ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
331 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW)
332
Maria Dimakopouloub63b4b42014-11-17 20:07:00 +0100333#define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(code, n) \
334 __EVENT_CONSTRAINT(code, n, \
335 ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
336 HWEIGHT(n), 0, \
337 PERF_X86_EVENT_PEBS_LD_HSW|PERF_X86_EVENT_EXCL)
338
Andi Kleen86a04462014-08-11 21:27:10 +0200339/* Check flags and event code/umask, and set the HSW store flag */
340#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(code, n) \
341 __EVENT_CONSTRAINT(code, n, \
342 INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
343 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST_HSW)
344
Maria Dimakopouloub63b4b42014-11-17 20:07:00 +0100345#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(code, n) \
346 __EVENT_CONSTRAINT(code, n, \
347 INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
348 HWEIGHT(n), 0, \
349 PERF_X86_EVENT_PEBS_ST_HSW|PERF_X86_EVENT_EXCL)
350
Andi Kleen86a04462014-08-11 21:27:10 +0200351/* Check flags and event code/umask, and set the HSW load flag */
352#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(code, n) \
353 __EVENT_CONSTRAINT(code, n, \
354 INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
355 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW)
356
Maria Dimakopouloub63b4b42014-11-17 20:07:00 +0100357#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(code, n) \
358 __EVENT_CONSTRAINT(code, n, \
359 INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
360 HWEIGHT(n), 0, \
361 PERF_X86_EVENT_PEBS_LD_HSW|PERF_X86_EVENT_EXCL)
362
Andi Kleen86a04462014-08-11 21:27:10 +0200363/* Check flags and event code/umask, and set the HSW N/A flag */
364#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(code, n) \
365 __EVENT_CONSTRAINT(code, n, \
366 INTEL_ARCH_EVENT_MASK|INTEL_ARCH_EVENT_MASK, \
367 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_NA_HSW)
368
369
Maria Dimakopouloucf30d522013-12-05 01:24:37 +0200370/*
371 * We define the end marker as having a weight of -1
372 * to enable blacklisting of events using a counter bitmask
373 * of zero and thus a weight of zero.
374 * The end marker has a weight that cannot possibly be
375 * obtained from counting the bits in the bitmask.
376 */
377#define EVENT_CONSTRAINT_END { .weight = -1 }
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300378
Maria Dimakopouloucf30d522013-12-05 01:24:37 +0200379/*
380 * Check for end marker with weight == -1
381 */
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300382#define for_each_event_constraint(e, c) \
Maria Dimakopouloucf30d522013-12-05 01:24:37 +0200383 for ((e) = (c); (e)->weight != -1; (e)++)
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300384
385/*
386 * Extra registers for specific events.
387 *
388 * Some events need large masks and require external MSRs.
389 * Those extra MSRs end up being shared for all events on
390 * a PMU and sometimes between PMU of sibling HT threads.
391 * In either case, the kernel needs to handle conflicting
392 * accesses to those extra, shared, regs. The data structure
393 * to manage those registers is stored in cpu_hw_event.
394 */
395struct extra_reg {
396 unsigned int event;
397 unsigned int msr;
398 u64 config_mask;
399 u64 valid_mask;
400 int idx; /* per_xxx->regs[] reg index */
Kan Liang338b5222014-07-14 12:25:56 -0700401 bool extra_msr_access;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300402};
403
404#define EVENT_EXTRA_REG(e, ms, m, vm, i) { \
Kan Liang338b5222014-07-14 12:25:56 -0700405 .event = (e), \
406 .msr = (ms), \
407 .config_mask = (m), \
408 .valid_mask = (vm), \
409 .idx = EXTRA_REG_##i, \
410 .extra_msr_access = true, \
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300411 }
412
413#define INTEL_EVENT_EXTRA_REG(event, msr, vm, idx) \
414 EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm, idx)
415
Stephane Eranianf20093e2013-01-24 16:10:32 +0100416#define INTEL_UEVENT_EXTRA_REG(event, msr, vm, idx) \
417 EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT | \
418 ARCH_PERFMON_EVENTSEL_UMASK, vm, idx)
419
420#define INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(c) \
421 INTEL_UEVENT_EXTRA_REG(c, \
422 MSR_PEBS_LD_LAT_THRESHOLD, \
423 0xffff, \
424 LDLAT)
425
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300426#define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0, RSP_0)
427
428union perf_capabilities {
429 struct {
430 u64 lbr_format:6;
431 u64 pebs_trap:1;
432 u64 pebs_arch_reg:1;
433 u64 pebs_format:4;
434 u64 smm_freeze:1;
Andi Kleen069e0c32013-06-25 08:12:33 -0700435 /*
436 * PMU supports separate counter range for writing
437 * values > 32bit.
438 */
439 u64 full_width_write:1;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300440 };
441 u64 capabilities;
442};
443
Peter Zijlstrac1d6f422011-12-06 14:07:15 +0100444struct x86_pmu_quirk {
445 struct x86_pmu_quirk *next;
446 void (*func)(void);
447};
448
Peter Zijlstraf9b4eeb2012-03-12 12:44:35 +0100449union x86_pmu_config {
450 struct {
451 u64 event:8,
452 umask:8,
453 usr:1,
454 os:1,
455 edge:1,
456 pc:1,
457 interrupt:1,
458 __reserved1:1,
459 en:1,
460 inv:1,
461 cmask:8,
462 event2:4,
463 __reserved2:4,
464 go:1,
465 ho:1;
466 } bits;
467 u64 value;
468};
469
470#define X86_CONFIG(args...) ((union x86_pmu_config){.bits = {args}}).value
471
Alexander Shishkin48070342015-01-14 14:18:20 +0200472enum {
473 x86_lbr_exclusive_lbr,
Alexander Shishkin80623822015-01-30 12:40:35 +0200474 x86_lbr_exclusive_bts,
Alexander Shishkin48070342015-01-14 14:18:20 +0200475 x86_lbr_exclusive_pt,
476 x86_lbr_exclusive_max,
477};
478
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300479/*
480 * struct x86_pmu - generic x86 pmu
481 */
482struct x86_pmu {
483 /*
484 * Generic x86 PMC bits
485 */
486 const char *name;
487 int version;
488 int (*handle_irq)(struct pt_regs *);
489 void (*disable_all)(void);
490 void (*enable_all)(int added);
491 void (*enable)(struct perf_event *);
492 void (*disable)(struct perf_event *);
493 int (*hw_config)(struct perf_event *event);
494 int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign);
495 unsigned eventsel;
496 unsigned perfctr;
Jacob Shin4c1fd172013-02-06 11:26:27 -0600497 int (*addr_offset)(int index, bool eventsel);
Jacob Shin0fbdad02013-02-06 11:26:28 -0600498 int (*rdpmc_index)(int index);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300499 u64 (*event_map)(int);
500 int max_events;
501 int num_counters;
502 int num_counters_fixed;
503 int cntval_bits;
504 u64 cntval_mask;
Gleb Natapovffb871b2011-11-10 14:57:26 +0200505 union {
506 unsigned long events_maskl;
507 unsigned long events_mask[BITS_TO_LONGS(ARCH_PERFMON_EVENTS_COUNT)];
508 };
509 int events_mask_len;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300510 int apic;
511 u64 max_period;
512 struct event_constraint *
513 (*get_event_constraints)(struct cpu_hw_events *cpuc,
Stephane Eranian79cba822014-11-17 20:06:56 +0100514 int idx,
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300515 struct perf_event *event);
516
517 void (*put_event_constraints)(struct cpu_hw_events *cpuc,
518 struct perf_event *event);
Maria Dimakopoulouc5362c02014-11-17 20:06:55 +0100519
520 void (*commit_scheduling)(struct cpu_hw_events *cpuc,
521 struct perf_event *event,
522 int cntr);
523
524 void (*start_scheduling)(struct cpu_hw_events *cpuc);
525
526 void (*stop_scheduling)(struct cpu_hw_events *cpuc);
527
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300528 struct event_constraint *event_constraints;
Peter Zijlstrac1d6f422011-12-06 14:07:15 +0100529 struct x86_pmu_quirk *quirks;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300530 int perfctr_second_write;
Andi Kleen72db5592013-06-17 17:36:50 -0700531 bool late_ack;
Andi Kleen294fe0f2015-02-17 18:18:06 -0800532 unsigned (*limit_period)(struct perf_event *event, unsigned l);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300533
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +0100534 /*
535 * sysfs attrs
536 */
Peter Zijlstrae97df762014-02-05 20:48:51 +0100537 int attr_rdpmc_broken;
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +0100538 int attr_rdpmc;
Jiri Olsa641cc932012-03-15 20:09:14 +0100539 struct attribute **format_attrs;
Stephane Eranianf20093e2013-01-24 16:10:32 +0100540 struct attribute **event_attrs;
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +0100541
Jiri Olsaa4747392012-10-10 14:53:11 +0200542 ssize_t (*events_sysfs_show)(char *page, u64 config);
Andi Kleen1a6461b2013-01-24 16:10:25 +0100543 struct attribute **cpu_events;
Jiri Olsaa4747392012-10-10 14:53:11 +0200544
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +0100545 /*
546 * CPU Hotplug hooks
547 */
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300548 int (*cpu_prepare)(int cpu);
549 void (*cpu_starting)(int cpu);
550 void (*cpu_dying)(int cpu);
551 void (*cpu_dead)(int cpu);
Peter Zijlstrac93dc842012-06-08 14:50:50 +0200552
553 void (*check_microcode)(void);
Yan, Zhengba532502014-11-04 21:55:58 -0500554 void (*sched_task)(struct perf_event_context *ctx,
555 bool sched_in);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300556
557 /*
558 * Intel Arch Perfmon v2+
559 */
560 u64 intel_ctrl;
561 union perf_capabilities intel_cap;
562
563 /*
564 * Intel DebugStore bits
565 */
Peter Zijlstra597ed952012-07-09 13:50:23 +0200566 unsigned int bts :1,
Peter Zijlstra3e0091e2012-06-26 23:38:39 +0200567 bts_active :1,
568 pebs :1,
569 pebs_active :1,
570 pebs_broken :1;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300571 int pebs_record_size;
572 void (*drain_pebs)(struct pt_regs *regs);
573 struct event_constraint *pebs_constraints;
Peter Zijlstra0780c922012-06-05 10:26:43 +0200574 void (*pebs_aliases)(struct perf_event *event);
Andi Kleen70ab7002012-06-05 17:56:48 -0700575 int max_pebs_events;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300576
577 /*
578 * Intel LBR
579 */
580 unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */
581 int lbr_nr; /* hardware stack size */
Stephane Eranianb36817e2012-02-09 23:20:53 +0100582 u64 lbr_sel_mask; /* LBR_SELECT valid bits */
583 const int *lbr_sel_map; /* lbr_select mappings */
Andi Kleenb7af41a2013-09-20 07:40:44 -0700584 bool lbr_double_abort; /* duplicated lbr aborts */
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300585
586 /*
Alexander Shishkin48070342015-01-14 14:18:20 +0200587 * Intel PT/LBR/BTS are exclusive
588 */
589 atomic_t lbr_exclusive[x86_lbr_exclusive_max];
590
591 /*
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300592 * Extra registers for events
593 */
594 struct extra_reg *extra_regs;
Stephane Eranian9a5e3fb2014-11-17 20:06:53 +0100595 unsigned int flags;
Gleb Natapov144d31e2011-10-05 14:01:21 +0200596
597 /*
598 * Intel host/guest support (KVM)
599 */
600 struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300601};
602
Yan, Zhenge18bf522014-11-04 21:56:03 -0500603struct x86_perf_task_context {
604 u64 lbr_from[MAX_LBR_ENTRIES];
605 u64 lbr_to[MAX_LBR_ENTRIES];
606 int lbr_callstack_users;
607 int lbr_stack_state;
608};
609
Peter Zijlstrac1d6f422011-12-06 14:07:15 +0100610#define x86_add_quirk(func_) \
611do { \
612 static struct x86_pmu_quirk __quirk __initdata = { \
613 .func = func_, \
614 }; \
615 __quirk.next = x86_pmu.quirks; \
616 x86_pmu.quirks = &__quirk; \
617} while (0)
618
Stephane Eranian9a5e3fb2014-11-17 20:06:53 +0100619/*
620 * x86_pmu flags
621 */
622#define PMU_FL_NO_HT_SHARING 0x1 /* no hyper-threading resource sharing */
623#define PMU_FL_HAS_RSP_1 0x2 /* has 2 equivalent offcore_rsp regs */
Maria Dimakopoulou6f6539c2014-11-17 20:06:57 +0100624#define PMU_FL_EXCL_CNTRS 0x4 /* has exclusive counter requirements */
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300625
Stephane Eranian3a54aaa2013-01-24 16:10:26 +0100626#define EVENT_VAR(_id) event_attr_##_id
627#define EVENT_PTR(_id) &event_attr_##_id.attr.attr
628
629#define EVENT_ATTR(_name, _id) \
630static struct perf_pmu_events_attr EVENT_VAR(_id) = { \
631 .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \
632 .id = PERF_COUNT_HW_##_id, \
633 .event_str = NULL, \
634};
635
636#define EVENT_ATTR_STR(_name, v, str) \
637static struct perf_pmu_events_attr event_attr_##v = { \
638 .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \
639 .id = 0, \
640 .event_str = str, \
641};
642
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300643extern struct x86_pmu x86_pmu __read_mostly;
644
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -0500645static inline bool x86_pmu_has_lbr_callstack(void)
646{
647 return x86_pmu.lbr_sel_map &&
648 x86_pmu.lbr_sel_map[PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] > 0;
649}
650
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300651DECLARE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
652
653int x86_perf_event_set_period(struct perf_event *event);
654
655/*
656 * Generalized hw caching related hw_event table, filled
657 * in on a per model basis. A value of 0 means
658 * 'not supported', -1 means 'hw_event makes no sense on
659 * this CPU', any other value means the raw hw_event
660 * ID.
661 */
662
663#define C(x) PERF_COUNT_HW_CACHE_##x
664
665extern u64 __read_mostly hw_cache_event_ids
666 [PERF_COUNT_HW_CACHE_MAX]
667 [PERF_COUNT_HW_CACHE_OP_MAX]
668 [PERF_COUNT_HW_CACHE_RESULT_MAX];
669extern u64 __read_mostly hw_cache_extra_regs
670 [PERF_COUNT_HW_CACHE_MAX]
671 [PERF_COUNT_HW_CACHE_OP_MAX]
672 [PERF_COUNT_HW_CACHE_RESULT_MAX];
673
674u64 x86_perf_event_update(struct perf_event *event);
675
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300676static inline unsigned int x86_pmu_config_addr(int index)
677{
Jacob Shin4c1fd172013-02-06 11:26:27 -0600678 return x86_pmu.eventsel + (x86_pmu.addr_offset ?
679 x86_pmu.addr_offset(index, true) : index);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300680}
681
682static inline unsigned int x86_pmu_event_addr(int index)
683{
Jacob Shin4c1fd172013-02-06 11:26:27 -0600684 return x86_pmu.perfctr + (x86_pmu.addr_offset ?
685 x86_pmu.addr_offset(index, false) : index);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300686}
687
Jacob Shin0fbdad02013-02-06 11:26:28 -0600688static inline int x86_pmu_rdpmc_index(int index)
689{
690 return x86_pmu.rdpmc_index ? x86_pmu.rdpmc_index(index) : index;
691}
692
Alexander Shishkin48070342015-01-14 14:18:20 +0200693int x86_add_exclusive(unsigned int what);
694
695void x86_del_exclusive(unsigned int what);
696
697void hw_perf_lbr_event_destroy(struct perf_event *event);
698
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300699int x86_setup_perfctr(struct perf_event *event);
700
701int x86_pmu_hw_config(struct perf_event *event);
702
703void x86_pmu_disable_all(void);
704
705static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc,
706 u64 enable_mask)
707{
Joerg Roedel1018faa2012-02-29 14:57:32 +0100708 u64 disable_mask = __this_cpu_read(cpu_hw_events.perf_ctr_virt_mask);
709
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300710 if (hwc->extra_reg.reg)
711 wrmsrl(hwc->extra_reg.reg, hwc->extra_reg.config);
Joerg Roedel1018faa2012-02-29 14:57:32 +0100712 wrmsrl(hwc->config_base, (hwc->config | enable_mask) & ~disable_mask);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300713}
714
715void x86_pmu_enable_all(int added);
716
Andrew Hunter43b457802013-05-23 11:07:03 -0700717int perf_assign_events(struct perf_event **events, int n,
Yan, Zheng4b4969b2012-06-15 14:31:30 +0800718 int wmin, int wmax, int *assign);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300719int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign);
720
721void x86_pmu_stop(struct perf_event *event, int flags);
722
723static inline void x86_pmu_disable_event(struct perf_event *event)
724{
725 struct hw_perf_event *hwc = &event->hw;
726
727 wrmsrl(hwc->config_base, hwc->config);
728}
729
730void x86_pmu_enable_event(struct perf_event *event);
731
732int x86_pmu_handle_irq(struct pt_regs *regs);
733
734extern struct event_constraint emptyconstraint;
735
736extern struct event_constraint unconstrained;
737
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100738static inline bool kernel_ip(unsigned long ip)
739{
740#ifdef CONFIG_X86_32
741 return ip > PAGE_OFFSET;
742#else
743 return (long)ip < 0;
744#endif
745}
746
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +0200747/*
748 * Not all PMUs provide the right context information to place the reported IP
749 * into full context. Specifically segment registers are typically not
750 * supplied.
751 *
752 * Assuming the address is a linear address (it is for IBS), we fake the CS and
753 * vm86 mode using the known zero-based code segment and 'fix up' the registers
754 * to reflect this.
755 *
756 * Intel PEBS/LBR appear to typically provide the effective address, nothing
757 * much we can do about that but pray and treat it like a linear address.
758 */
759static inline void set_linear_ip(struct pt_regs *regs, unsigned long ip)
760{
761 regs->cs = kernel_ip(ip) ? __KERNEL_CS : __USER_CS;
762 if (regs->flags & X86_VM_MASK)
763 regs->flags ^= (PERF_EFLAGS_VM | X86_VM_MASK);
764 regs->ip = ip;
765}
766
Jiri Olsa0bf79d42012-10-10 14:53:14 +0200767ssize_t x86_event_sysfs_show(char *page, u64 config, u64 event);
Jiri Olsa20550a42012-10-10 14:53:15 +0200768ssize_t intel_event_sysfs_show(char *page, u64 config);
Jiri Olsa43c032f2012-10-10 14:53:13 +0200769
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300770#ifdef CONFIG_CPU_SUP_AMD
771
772int amd_pmu_init(void);
773
774#else /* CONFIG_CPU_SUP_AMD */
775
776static inline int amd_pmu_init(void)
777{
778 return 0;
779}
780
781#endif /* CONFIG_CPU_SUP_AMD */
782
783#ifdef CONFIG_CPU_SUP_INTEL
784
Alexander Shishkin48070342015-01-14 14:18:20 +0200785static inline bool intel_pmu_needs_lbr_smpl(struct perf_event *event)
786{
787 /* user explicitly requested branch sampling */
788 if (has_branch_stack(event))
789 return true;
790
791 /* implicit branch sampling to correct PEBS skid */
792 if (x86_pmu.intel_cap.pebs_trap && event->attr.precise_ip > 1 &&
793 x86_pmu.intel_cap.pebs_format < 2)
794 return true;
795
796 return false;
797}
798
799static inline bool intel_pmu_has_bts(struct perf_event *event)
800{
801 if (event->attr.config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS &&
802 !event->attr.freq && event->hw.sample_period == 1)
803 return true;
804
805 return false;
806}
807
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300808int intel_pmu_save_and_restart(struct perf_event *event);
809
810struct event_constraint *
Stephane Eranian79cba822014-11-17 20:06:56 +0100811x86_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
812 struct perf_event *event);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300813
814struct intel_shared_regs *allocate_shared_regs(int cpu);
815
816int intel_pmu_init(void);
817
818void init_debug_store_on_cpu(int cpu);
819
820void fini_debug_store_on_cpu(int cpu);
821
822void release_ds_buffers(void);
823
824void reserve_ds_buffers(void);
825
826extern struct event_constraint bts_constraint;
827
828void intel_pmu_enable_bts(u64 config);
829
830void intel_pmu_disable_bts(void);
831
832int intel_pmu_drain_bts_buffer(void);
833
834extern struct event_constraint intel_core2_pebs_event_constraints[];
835
836extern struct event_constraint intel_atom_pebs_event_constraints[];
837
Yan, Zheng1fa64182013-07-18 17:02:24 +0800838extern struct event_constraint intel_slm_pebs_event_constraints[];
839
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300840extern struct event_constraint intel_nehalem_pebs_event_constraints[];
841
842extern struct event_constraint intel_westmere_pebs_event_constraints[];
843
844extern struct event_constraint intel_snb_pebs_event_constraints[];
845
Stephane Eranian20a36e32012-09-11 01:07:01 +0200846extern struct event_constraint intel_ivb_pebs_event_constraints[];
847
Andi Kleen30443182013-06-17 17:36:49 -0700848extern struct event_constraint intel_hsw_pebs_event_constraints[];
849
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300850struct event_constraint *intel_pebs_constraints(struct perf_event *event);
851
852void intel_pmu_pebs_enable(struct perf_event *event);
853
854void intel_pmu_pebs_disable(struct perf_event *event);
855
856void intel_pmu_pebs_enable_all(void);
857
858void intel_pmu_pebs_disable_all(void);
859
860void intel_ds_init(void);
861
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500862void intel_pmu_lbr_sched_task(struct perf_event_context *ctx, bool sched_in);
863
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300864void intel_pmu_lbr_reset(void);
865
866void intel_pmu_lbr_enable(struct perf_event *event);
867
868void intel_pmu_lbr_disable(struct perf_event *event);
869
870void intel_pmu_lbr_enable_all(void);
871
872void intel_pmu_lbr_disable_all(void);
873
874void intel_pmu_lbr_read(void);
875
876void intel_pmu_lbr_init_core(void);
877
878void intel_pmu_lbr_init_nhm(void);
879
880void intel_pmu_lbr_init_atom(void);
881
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100882void intel_pmu_lbr_init_snb(void);
883
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -0500884void intel_pmu_lbr_init_hsw(void);
885
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100886int intel_pmu_setup_lbr_filter(struct perf_event *event);
887
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200888void intel_pt_interrupt(void);
889
Alexander Shishkin80623822015-01-30 12:40:35 +0200890int intel_bts_interrupt(void);
891
892void intel_bts_enable_local(void);
893
894void intel_bts_disable_local(void);
895
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300896int p4_pmu_init(void);
897
898int p6_pmu_init(void);
899
Vince Weavere717bf42012-09-26 14:12:52 -0400900int knc_pmu_init(void);
901
Stephane Eranianf20093e2013-01-24 16:10:32 +0100902ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr,
903 char *page);
904
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300905#else /* CONFIG_CPU_SUP_INTEL */
906
907static inline void reserve_ds_buffers(void)
908{
909}
910
911static inline void release_ds_buffers(void)
912{
913}
914
915static inline int intel_pmu_init(void)
916{
917 return 0;
918}
919
920static inline struct intel_shared_regs *allocate_shared_regs(int cpu)
921{
922 return NULL;
923}
924
925#endif /* CONFIG_CPU_SUP_INTEL */