blob: b4d41829da4f2aafaaf53d763839954493074620 [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
Peter Zijlstra90eec102015-11-16 11:08:45 +01008 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra
Kevin Winchesterde0428a2011-08-30 20:41:05 -03009 * 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
Thomas Gleixner10043e02017-12-04 15:07:49 +010017#include <asm/intel_ds.h>
18
Andi Kleenf1ad4482015-12-01 17:01:00 -080019/* To enable MSR tracing please use the generic trace points. */
Peter Zijlstra1c2ac3f2012-05-14 15:25:34 +020020
Kevin Winchesterde0428a2011-08-30 20:41:05 -030021/*
22 * | NHM/WSM | SNB |
23 * register -------------------------------
24 * | HT | no HT | HT | no HT |
25 *-----------------------------------------
26 * offcore | core | core | cpu | core |
27 * lbr_sel | core | core | cpu | core |
28 * ld_lat | cpu | core | cpu | core |
29 *-----------------------------------------
30 *
31 * Given that there is a small number of shared regs,
32 * we can pre-allocate their slot in the per-cpu
33 * per-core reg tables.
34 */
35enum extra_reg_type {
36 EXTRA_REG_NONE = -1, /* not used */
37
38 EXTRA_REG_RSP_0 = 0, /* offcore_response_0 */
39 EXTRA_REG_RSP_1 = 1, /* offcore_response_1 */
Stephane Eranianb36817e2012-02-09 23:20:53 +010040 EXTRA_REG_LBR = 2, /* lbr_select */
Stephane Eranianf20093e2013-01-24 16:10:32 +010041 EXTRA_REG_LDLAT = 3, /* ld_lat_threshold */
Andi Kleend0dc8492015-09-09 14:53:59 -070042 EXTRA_REG_FE = 4, /* fe_* */
Kevin Winchesterde0428a2011-08-30 20:41:05 -030043
44 EXTRA_REG_MAX /* number of entries needed */
45};
46
47struct event_constraint {
48 union {
49 unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
50 u64 idxmsk64;
51 };
52 u64 code;
53 u64 cmask;
54 int weight;
Robert Richterbc1738f2011-11-18 12:35:22 +010055 int overlap;
Stephane Eranian9fac2cf2013-01-24 16:10:27 +010056 int flags;
Kevin Winchesterde0428a2011-08-30 20:41:05 -030057};
Peter Zijlstra1f6a1e22019-03-14 12:58:52 +010058
Stephane Eranianf20093e2013-01-24 16:10:32 +010059/*
Stephane Eranian2f7f73a2013-06-20 18:42:54 +020060 * struct hw_perf_event.flags flags
Stephane Eranianf20093e2013-01-24 16:10:32 +010061 */
Peter Zijlstrac857eb52015-04-15 20:14:53 +020062#define PERF_X86_EVENT_PEBS_LDLAT 0x0001 /* ld+ldlat data address sampling */
63#define PERF_X86_EVENT_PEBS_ST 0x0002 /* st data address sampling */
64#define PERF_X86_EVENT_PEBS_ST_HSW 0x0004 /* haswell style datala, store */
Peter Zijlstra1f6a1e22019-03-14 12:58:52 +010065#define PERF_X86_EVENT_PEBS_LD_HSW 0x0008 /* haswell style datala, load */
66#define PERF_X86_EVENT_PEBS_NA_HSW 0x0010 /* haswell style datala, unknown */
67#define PERF_X86_EVENT_EXCL 0x0020 /* HT exclusivity on counter */
68#define PERF_X86_EVENT_DYNAMIC 0x0040 /* dynamic alloc'd constraint */
69#define PERF_X86_EVENT_RDPMC_ALLOWED 0x0080 /* grant rdpmc permission */
70#define PERF_X86_EVENT_EXCL_ACCT 0x0100 /* accounted EXCL event */
71#define PERF_X86_EVENT_AUTO_RELOAD 0x0200 /* use PEBS auto-reload */
72#define PERF_X86_EVENT_LARGE_PEBS 0x0400 /* use large PEBS */
Kevin Winchesterde0428a2011-08-30 20:41:05 -030073
74struct amd_nb {
75 int nb_id; /* NorthBridge id */
76 int refcnt; /* reference count */
77 struct perf_event *owners[X86_PMC_IDX_MAX];
78 struct event_constraint event_constraints[X86_PMC_IDX_MAX];
79};
80
Kan Liangfd583ad2017-04-04 15:14:06 -040081#define PEBS_COUNTER_MASK ((1ULL << MAX_PEBS_EVENTS) - 1)
Kevin Winchesterde0428a2011-08-30 20:41:05 -030082
83/*
Yan, Zheng3569c0d2015-05-06 15:33:50 -040084 * Flags PEBS can handle without an PMI.
85 *
Yan, Zheng9c964ef2015-05-06 15:33:51 -040086 * TID can only be handled by flushing at context switch.
Andi Kleen2fe1bc12017-08-31 14:46:30 -070087 * REGS_USER can be handled for events limited to ring 3.
Yan, Zheng9c964ef2015-05-06 15:33:51 -040088 *
Yan, Zheng3569c0d2015-05-06 15:33:50 -040089 */
Kan Liang174afc32018-03-12 10:45:37 -040090#define LARGE_PEBS_FLAGS \
Yan, Zheng9c964ef2015-05-06 15:33:51 -040091 (PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_ADDR | \
Yan, Zheng3569c0d2015-05-06 15:33:50 -040092 PERF_SAMPLE_ID | PERF_SAMPLE_CPU | PERF_SAMPLE_STREAM_ID | \
93 PERF_SAMPLE_DATA_SRC | PERF_SAMPLE_IDENTIFIER | \
Andi Kleen2fe1bc12017-08-31 14:46:30 -070094 PERF_SAMPLE_TRANSACTION | PERF_SAMPLE_PHYS_ADDR | \
Jiri Olsa11974912018-02-01 09:38:12 +010095 PERF_SAMPLE_REGS_INTR | PERF_SAMPLE_REGS_USER | \
96 PERF_SAMPLE_PERIOD)
Yan, Zheng3569c0d2015-05-06 15:33:50 -040097
Andi Kleen2fe1bc12017-08-31 14:46:30 -070098#define PEBS_REGS \
99 (PERF_REG_X86_AX | \
100 PERF_REG_X86_BX | \
101 PERF_REG_X86_CX | \
102 PERF_REG_X86_DX | \
103 PERF_REG_X86_DI | \
104 PERF_REG_X86_SI | \
105 PERF_REG_X86_SP | \
106 PERF_REG_X86_BP | \
107 PERF_REG_X86_IP | \
108 PERF_REG_X86_FLAGS | \
109 PERF_REG_X86_R8 | \
110 PERF_REG_X86_R9 | \
111 PERF_REG_X86_R10 | \
112 PERF_REG_X86_R11 | \
113 PERF_REG_X86_R12 | \
114 PERF_REG_X86_R13 | \
115 PERF_REG_X86_R14 | \
116 PERF_REG_X86_R15)
117
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300118/*
119 * Per register state.
120 */
121struct er_account {
Peter Zijlstrab8000582016-11-17 18:17:31 +0100122 raw_spinlock_t lock; /* per-core: protect structure */
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300123 u64 config; /* extra MSR config */
124 u64 reg; /* extra MSR number */
125 atomic_t ref; /* reference count */
126};
127
128/*
129 * Per core/cpu state
130 *
131 * Used to coordinate shared registers between HT threads or
132 * among events on a single PMU.
133 */
134struct intel_shared_regs {
135 struct er_account regs[EXTRA_REG_MAX];
136 int refcnt; /* per-core: #HT threads */
137 unsigned core_id; /* per-core: core id */
138};
139
Maria Dimakopoulou6f6539c2014-11-17 20:06:57 +0100140enum intel_excl_state_type {
141 INTEL_EXCL_UNUSED = 0, /* counter is unused */
142 INTEL_EXCL_SHARED = 1, /* counter can be used by both threads */
143 INTEL_EXCL_EXCLUSIVE = 2, /* counter can be used by one thread only */
144};
145
146struct intel_excl_states {
Maria Dimakopoulou6f6539c2014-11-17 20:06:57 +0100147 enum intel_excl_state_type state[X86_PMC_IDX_MAX];
Maria Dimakopouloue9791212014-11-17 20:06:58 +0100148 bool sched_started; /* true if scheduling has started */
Maria Dimakopoulou6f6539c2014-11-17 20:06:57 +0100149};
150
151struct intel_excl_cntrs {
152 raw_spinlock_t lock;
153
154 struct intel_excl_states states[2];
155
Peter Zijlstracc1790c2015-05-21 10:57:17 +0200156 union {
157 u16 has_exclusive[2];
158 u32 exclusive_present;
159 };
160
Maria Dimakopoulou6f6539c2014-11-17 20:06:57 +0100161 int refcnt; /* per-core: #HT threads */
162 unsigned core_id; /* per-core: core id */
163};
164
Kan Liang8b077e4a2018-06-05 08:38:46 -0700165struct x86_perf_task_context;
Andi Kleen9a92e162015-05-10 12:22:44 -0700166#define MAX_LBR_ENTRIES 32
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300167
Stephane Eranian90413462014-11-17 20:06:54 +0100168enum {
169 X86_PERF_KFREE_SHARED = 0,
170 X86_PERF_KFREE_EXCL = 1,
171 X86_PERF_KFREE_MAX
172};
173
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300174struct cpu_hw_events {
175 /*
176 * Generic x86 PMC bits
177 */
178 struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */
179 unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
180 unsigned long running[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
181 int enabled;
182
Peter Zijlstrac347a2f2014-02-24 12:26:21 +0100183 int n_events; /* the # of events in the below arrays */
184 int n_added; /* the # last events in the below arrays;
185 they've never been enabled yet */
186 int n_txn; /* the # last events in the below arrays;
187 added in the current transaction */
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300188 int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */
189 u64 tags[X86_PMC_IDX_MAX];
Peter Zijlstrab371b592015-05-21 10:57:13 +0200190
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300191 struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */
Peter Zijlstrab371b592015-05-21 10:57:13 +0200192 struct event_constraint *event_constraint[X86_PMC_IDX_MAX];
193
Peter Zijlstracc1790c2015-05-21 10:57:17 +0200194 int n_excl; /* the number of exclusive events */
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300195
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -0700196 unsigned int txn_flags;
Peter Zijlstra5a4252942012-06-05 15:30:31 +0200197 int is_fake;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300198
199 /*
200 * Intel DebugStore bits
201 */
202 struct debug_store *ds;
Hugh Dickinsc1961a42017-12-04 15:07:50 +0100203 void *ds_pebs_vaddr;
204 void *ds_bts_vaddr;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300205 u64 pebs_enabled;
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +0200206 int n_pebs;
207 int n_large_pebs;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300208
209 /*
210 * Intel LBR bits
211 */
212 int lbr_users;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300213 struct perf_branch_stack lbr_stack;
214 struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES];
Stephane Eranianb36817e2012-02-09 23:20:53 +0100215 struct er_account *lbr_sel;
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100216 u64 br_sel;
Kan Liang8b077e4a2018-06-05 08:38:46 -0700217 struct x86_perf_task_context *last_task_ctx;
218 int last_log_id;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300219
220 /*
Gleb Natapov144d31e2011-10-05 14:01:21 +0200221 * Intel host/guest exclude bits
222 */
223 u64 intel_ctrl_guest_mask;
224 u64 intel_ctrl_host_mask;
225 struct perf_guest_switch_msr guest_switch_msrs[X86_PMC_IDX_MAX];
226
227 /*
Peter Zijlstra2b9e3442013-09-12 12:53:44 +0200228 * Intel checkpoint mask
229 */
230 u64 intel_cp_status;
231
232 /*
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300233 * manage shared (per-core, per-cpu) registers
234 * used on Intel NHM/WSM/SNB
235 */
236 struct intel_shared_regs *shared_regs;
Maria Dimakopoulou6f6539c2014-11-17 20:06:57 +0100237 /*
238 * manage exclusive counter access between hyperthread
239 */
240 struct event_constraint *constraint_list; /* in enable order */
241 struct intel_excl_cntrs *excl_cntrs;
242 int excl_thread_id; /* 0 or 1 */
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300243
244 /*
Peter Zijlstra (Intel)400816f2019-03-05 22:23:18 +0100245 * SKL TSX_FORCE_ABORT shadow
246 */
247 u64 tfa_shadow;
248
249 /*
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300250 * AMD specific bits
251 */
Joerg Roedel1018faa2012-02-29 14:57:32 +0100252 struct amd_nb *amd_nb;
253 /* Inverted mask of bits to clear in the perf_ctr ctrl registers */
254 u64 perf_ctr_virt_mask;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300255
Stephane Eranian90413462014-11-17 20:06:54 +0100256 void *kfree_on_online[X86_PERF_KFREE_MAX];
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300257};
258
Stephane Eranian9fac2cf2013-01-24 16:10:27 +0100259#define __EVENT_CONSTRAINT(c, n, m, w, o, f) {\
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300260 { .idxmsk64 = (n) }, \
261 .code = (c), \
262 .cmask = (m), \
263 .weight = (w), \
Robert Richterbc1738f2011-11-18 12:35:22 +0100264 .overlap = (o), \
Stephane Eranian9fac2cf2013-01-24 16:10:27 +0100265 .flags = f, \
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300266}
267
268#define EVENT_CONSTRAINT(c, n, m) \
Stephane Eranian9fac2cf2013-01-24 16:10:27 +0100269 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 0, 0)
Robert Richterbc1738f2011-11-18 12:35:22 +0100270
Maria Dimakopoulou6f6539c2014-11-17 20:06:57 +0100271#define INTEL_EXCLEVT_CONSTRAINT(c, n) \
272 __EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT, HWEIGHT(n),\
273 0, PERF_X86_EVENT_EXCL)
274
Robert Richterbc1738f2011-11-18 12:35:22 +0100275/*
276 * The overlap flag marks event constraints with overlapping counter
277 * masks. This is the case if the counter mask of such an event is not
278 * a subset of any other counter mask of a constraint with an equal or
279 * higher weight, e.g.:
280 *
281 * c_overlaps = EVENT_CONSTRAINT_OVERLAP(0, 0x09, 0);
282 * c_another1 = EVENT_CONSTRAINT(0, 0x07, 0);
283 * c_another2 = EVENT_CONSTRAINT(0, 0x38, 0);
284 *
285 * The event scheduler may not select the correct counter in the first
286 * cycle because it needs to know which subsequent events will be
287 * scheduled. It may fail to schedule the events then. So we set the
288 * overlap flag for such constraints to give the scheduler a hint which
289 * events to select for counter rescheduling.
290 *
291 * Care must be taken as the rescheduling algorithm is O(n!) which
Adam Buchbinder6a6256f2016-02-23 15:34:30 -0800292 * will increase scheduling cycles for an over-committed system
Robert Richterbc1738f2011-11-18 12:35:22 +0100293 * dramatically. The number of such EVENT_CONSTRAINT_OVERLAP() macros
294 * and its counter masks must be kept at a minimum.
295 */
296#define EVENT_CONSTRAINT_OVERLAP(c, n, m) \
Stephane Eranian9fac2cf2013-01-24 16:10:27 +0100297 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 1, 0)
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300298
299/*
300 * Constraint on the Event code.
301 */
302#define INTEL_EVENT_CONSTRAINT(c, n) \
303 EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT)
304
305/*
306 * Constraint on the Event code + UMask + fixed-mask
307 *
308 * filter mask to validate fixed counter events.
309 * the following filters disqualify for fixed counters:
310 * - inv
311 * - edge
312 * - cnt-mask
Andi Kleen3a632cb2013-06-17 17:36:48 -0700313 * - in_tx
314 * - in_tx_checkpointed
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300315 * The other filters are supported by fixed counters.
316 * The any-thread option is supported starting with v3.
317 */
Andi Kleen3a632cb2013-06-17 17:36:48 -0700318#define FIXED_EVENT_FLAGS (X86_RAW_EVENT_MASK|HSW_IN_TX|HSW_IN_TX_CHECKPOINTED)
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300319#define FIXED_EVENT_CONSTRAINT(c, n) \
Andi Kleen3a632cb2013-06-17 17:36:48 -0700320 EVENT_CONSTRAINT(c, (1ULL << (32+n)), FIXED_EVENT_FLAGS)
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300321
322/*
323 * Constraint on the Event code + UMask
324 */
325#define INTEL_UEVENT_CONSTRAINT(c, n) \
326 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
327
Andi Kleenb7883a12015-11-16 16:21:07 -0800328/* Constraint on specific umask bit only + event */
329#define INTEL_UBIT_EVENT_CONSTRAINT(c, n) \
330 EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT|(c))
331
Andi Kleen7550ddf2014-09-24 07:34:46 -0700332/* Like UEVENT_CONSTRAINT, but match flags too */
333#define INTEL_FLAGS_UEVENT_CONSTRAINT(c, n) \
334 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS)
335
Maria Dimakopouloue9791212014-11-17 20:06:58 +0100336#define INTEL_EXCLUEVT_CONSTRAINT(c, n) \
337 __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK, \
338 HWEIGHT(n), 0, PERF_X86_EVENT_EXCL)
339
Stephane Eranianf20093e2013-01-24 16:10:32 +0100340#define INTEL_PLD_CONSTRAINT(c, n) \
Andi Kleen86a04462014-08-11 21:27:10 +0200341 __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
Stephane Eranianf20093e2013-01-24 16:10:32 +0100342 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LDLAT)
343
Stephane Eranian9ad64c02013-01-24 16:10:34 +0100344#define INTEL_PST_CONSTRAINT(c, n) \
Andi Kleen86a04462014-08-11 21:27:10 +0200345 __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
Stephane Eranian9ad64c02013-01-24 16:10:34 +0100346 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST)
347
Andi Kleen86a04462014-08-11 21:27:10 +0200348/* Event constraint, but match on all event flags too. */
349#define INTEL_FLAGS_EVENT_CONSTRAINT(c, n) \
350 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS)
351
352/* Check only flags, but allow all event/umask */
353#define INTEL_ALL_EVENT_CONSTRAINT(code, n) \
354 EVENT_CONSTRAINT(code, n, X86_ALL_EVENT_FLAGS)
355
356/* Check flags and event code, and set the HSW store flag */
357#define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_ST(code, n) \
358 __EVENT_CONSTRAINT(code, n, \
359 ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
Andi Kleenf9134f32013-06-17 17:36:52 -0700360 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST_HSW)
361
Andi Kleen86a04462014-08-11 21:27:10 +0200362/* Check flags and event code, and set the HSW load flag */
363#define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(code, n) \
Maria Dimakopouloub63b4b42014-11-17 20:07:00 +0100364 __EVENT_CONSTRAINT(code, n, \
Andi Kleen86a04462014-08-11 21:27:10 +0200365 ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
366 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW)
367
Maria Dimakopouloub63b4b42014-11-17 20:07:00 +0100368#define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(code, n) \
369 __EVENT_CONSTRAINT(code, n, \
370 ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
371 HWEIGHT(n), 0, \
372 PERF_X86_EVENT_PEBS_LD_HSW|PERF_X86_EVENT_EXCL)
373
Andi Kleen86a04462014-08-11 21:27:10 +0200374/* Check flags and event code/umask, and set the HSW store flag */
375#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(code, n) \
376 __EVENT_CONSTRAINT(code, n, \
377 INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
378 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST_HSW)
379
Maria Dimakopouloub63b4b42014-11-17 20:07:00 +0100380#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(code, n) \
381 __EVENT_CONSTRAINT(code, n, \
382 INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
383 HWEIGHT(n), 0, \
384 PERF_X86_EVENT_PEBS_ST_HSW|PERF_X86_EVENT_EXCL)
385
Andi Kleen86a04462014-08-11 21:27:10 +0200386/* Check flags and event code/umask, and set the HSW load flag */
387#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(code, n) \
388 __EVENT_CONSTRAINT(code, n, \
389 INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
390 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW)
391
Maria Dimakopouloub63b4b42014-11-17 20:07:00 +0100392#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(code, n) \
393 __EVENT_CONSTRAINT(code, n, \
394 INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
395 HWEIGHT(n), 0, \
396 PERF_X86_EVENT_PEBS_LD_HSW|PERF_X86_EVENT_EXCL)
397
Andi Kleen86a04462014-08-11 21:27:10 +0200398/* Check flags and event code/umask, and set the HSW N/A flag */
399#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(code, n) \
400 __EVENT_CONSTRAINT(code, n, \
Jiri Olsa169b9322015-11-09 10:24:31 +0100401 INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
Andi Kleen86a04462014-08-11 21:27:10 +0200402 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_NA_HSW)
403
404
Maria Dimakopouloucf30d522013-12-05 01:24:37 +0200405/*
406 * We define the end marker as having a weight of -1
407 * to enable blacklisting of events using a counter bitmask
408 * of zero and thus a weight of zero.
409 * The end marker has a weight that cannot possibly be
410 * obtained from counting the bits in the bitmask.
411 */
412#define EVENT_CONSTRAINT_END { .weight = -1 }
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300413
Maria Dimakopouloucf30d522013-12-05 01:24:37 +0200414/*
415 * Check for end marker with weight == -1
416 */
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300417#define for_each_event_constraint(e, c) \
Maria Dimakopouloucf30d522013-12-05 01:24:37 +0200418 for ((e) = (c); (e)->weight != -1; (e)++)
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300419
420/*
421 * Extra registers for specific events.
422 *
423 * Some events need large masks and require external MSRs.
424 * Those extra MSRs end up being shared for all events on
425 * a PMU and sometimes between PMU of sibling HT threads.
426 * In either case, the kernel needs to handle conflicting
427 * accesses to those extra, shared, regs. The data structure
428 * to manage those registers is stored in cpu_hw_event.
429 */
430struct extra_reg {
431 unsigned int event;
432 unsigned int msr;
433 u64 config_mask;
434 u64 valid_mask;
435 int idx; /* per_xxx->regs[] reg index */
Kan Liang338b5222014-07-14 12:25:56 -0700436 bool extra_msr_access;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300437};
438
439#define EVENT_EXTRA_REG(e, ms, m, vm, i) { \
Kan Liang338b5222014-07-14 12:25:56 -0700440 .event = (e), \
441 .msr = (ms), \
442 .config_mask = (m), \
443 .valid_mask = (vm), \
444 .idx = EXTRA_REG_##i, \
445 .extra_msr_access = true, \
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300446 }
447
448#define INTEL_EVENT_EXTRA_REG(event, msr, vm, idx) \
449 EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm, idx)
450
Stephane Eranianf20093e2013-01-24 16:10:32 +0100451#define INTEL_UEVENT_EXTRA_REG(event, msr, vm, idx) \
452 EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT | \
453 ARCH_PERFMON_EVENTSEL_UMASK, vm, idx)
454
455#define INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(c) \
456 INTEL_UEVENT_EXTRA_REG(c, \
457 MSR_PEBS_LD_LAT_THRESHOLD, \
458 0xffff, \
459 LDLAT)
460
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300461#define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0, RSP_0)
462
463union perf_capabilities {
464 struct {
465 u64 lbr_format:6;
466 u64 pebs_trap:1;
467 u64 pebs_arch_reg:1;
468 u64 pebs_format:4;
469 u64 smm_freeze:1;
Andi Kleen069e0c32013-06-25 08:12:33 -0700470 /*
471 * PMU supports separate counter range for writing
472 * values > 32bit.
473 */
474 u64 full_width_write:1;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300475 };
476 u64 capabilities;
477};
478
Peter Zijlstrac1d6f422011-12-06 14:07:15 +0100479struct x86_pmu_quirk {
480 struct x86_pmu_quirk *next;
481 void (*func)(void);
482};
483
Peter Zijlstraf9b4eeb2012-03-12 12:44:35 +0100484union x86_pmu_config {
485 struct {
486 u64 event:8,
487 umask:8,
488 usr:1,
489 os:1,
490 edge:1,
491 pc:1,
492 interrupt:1,
493 __reserved1:1,
494 en:1,
495 inv:1,
496 cmask:8,
497 event2:4,
498 __reserved2:4,
499 go:1,
500 ho:1;
501 } bits;
502 u64 value;
503};
504
505#define X86_CONFIG(args...) ((union x86_pmu_config){.bits = {args}}).value
506
Alexander Shishkin48070342015-01-14 14:18:20 +0200507enum {
508 x86_lbr_exclusive_lbr,
Alexander Shishkin80623822015-01-30 12:40:35 +0200509 x86_lbr_exclusive_bts,
Alexander Shishkin48070342015-01-14 14:18:20 +0200510 x86_lbr_exclusive_pt,
511 x86_lbr_exclusive_max,
512};
513
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300514/*
515 * struct x86_pmu - generic x86 pmu
516 */
517struct x86_pmu {
518 /*
519 * Generic x86 PMC bits
520 */
521 const char *name;
522 int version;
523 int (*handle_irq)(struct pt_regs *);
524 void (*disable_all)(void);
525 void (*enable_all)(int added);
526 void (*enable)(struct perf_event *);
527 void (*disable)(struct perf_event *);
Peter Zijlstra68f70822016-07-06 18:02:43 +0200528 void (*add)(struct perf_event *);
529 void (*del)(struct perf_event *);
Kan Liangbcfbe5c2018-02-12 14:20:32 -0800530 void (*read)(struct perf_event *event);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300531 int (*hw_config)(struct perf_event *event);
532 int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign);
533 unsigned eventsel;
534 unsigned perfctr;
Jacob Shin4c1fd172013-02-06 11:26:27 -0600535 int (*addr_offset)(int index, bool eventsel);
Jacob Shin0fbdad02013-02-06 11:26:28 -0600536 int (*rdpmc_index)(int index);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300537 u64 (*event_map)(int);
538 int max_events;
539 int num_counters;
540 int num_counters_fixed;
541 int cntval_bits;
542 u64 cntval_mask;
Gleb Natapovffb871b2011-11-10 14:57:26 +0200543 union {
544 unsigned long events_maskl;
545 unsigned long events_mask[BITS_TO_LONGS(ARCH_PERFMON_EVENTS_COUNT)];
546 };
547 int events_mask_len;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300548 int apic;
549 u64 max_period;
550 struct event_constraint *
551 (*get_event_constraints)(struct cpu_hw_events *cpuc,
Stephane Eranian79cba822014-11-17 20:06:56 +0100552 int idx,
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300553 struct perf_event *event);
554
555 void (*put_event_constraints)(struct cpu_hw_events *cpuc,
556 struct perf_event *event);
Maria Dimakopoulouc5362c02014-11-17 20:06:55 +0100557
Maria Dimakopoulouc5362c02014-11-17 20:06:55 +0100558 void (*start_scheduling)(struct cpu_hw_events *cpuc);
559
Peter Zijlstra0c41e752015-05-21 10:57:32 +0200560 void (*commit_scheduling)(struct cpu_hw_events *cpuc, int idx, int cntr);
561
Maria Dimakopoulouc5362c02014-11-17 20:06:55 +0100562 void (*stop_scheduling)(struct cpu_hw_events *cpuc);
563
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300564 struct event_constraint *event_constraints;
Peter Zijlstrac1d6f422011-12-06 14:07:15 +0100565 struct x86_pmu_quirk *quirks;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300566 int perfctr_second_write;
Kan Liangf605cfc2018-03-01 12:54:54 -0500567 u64 (*limit_period)(struct perf_event *event, u64 l);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300568
Andi Kleenaf3bdb92018-08-08 00:12:07 -0700569 /* PMI handler bits */
570 unsigned int late_ack :1,
571 counter_freezing :1;
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +0100572 /*
573 * sysfs attrs
574 */
Peter Zijlstrae97df762014-02-05 20:48:51 +0100575 int attr_rdpmc_broken;
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +0100576 int attr_rdpmc;
Jiri Olsa641cc932012-03-15 20:09:14 +0100577 struct attribute **format_attrs;
Stephane Eranianf20093e2013-01-24 16:10:32 +0100578 struct attribute **event_attrs;
Andi Kleenb00233b2017-08-22 11:52:01 -0700579 struct attribute **caps_attrs;
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +0100580
Jiri Olsaa4747392012-10-10 14:53:11 +0200581 ssize_t (*events_sysfs_show)(char *page, u64 config);
Andi Kleen1a6461b2013-01-24 16:10:25 +0100582 struct attribute **cpu_events;
Jiri Olsaa4747392012-10-10 14:53:11 +0200583
Kan Liang60893272017-05-12 07:51:13 -0700584 unsigned long attr_freeze_on_smi;
585 struct attribute **attrs;
586
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +0100587 /*
588 * CPU Hotplug hooks
589 */
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300590 int (*cpu_prepare)(int cpu);
591 void (*cpu_starting)(int cpu);
592 void (*cpu_dying)(int cpu);
593 void (*cpu_dead)(int cpu);
Peter Zijlstrac93dc842012-06-08 14:50:50 +0200594
595 void (*check_microcode)(void);
Yan, Zhengba532502014-11-04 21:55:58 -0500596 void (*sched_task)(struct perf_event_context *ctx,
597 bool sched_in);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300598
599 /*
600 * Intel Arch Perfmon v2+
601 */
602 u64 intel_ctrl;
603 union perf_capabilities intel_cap;
604
605 /*
606 * Intel DebugStore bits
607 */
Andi Kleen9b545c02019-02-04 14:23:30 -0800608 unsigned int bts :1,
609 bts_active :1,
610 pebs :1,
611 pebs_active :1,
612 pebs_broken :1,
613 pebs_prec_dist :1,
614 pebs_no_tlb :1,
615 pebs_no_isolation :1;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300616 int pebs_record_size;
Jiri Olsae72daf32016-03-01 20:03:52 +0100617 int pebs_buffer_size;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300618 void (*drain_pebs)(struct pt_regs *regs);
619 struct event_constraint *pebs_constraints;
Peter Zijlstra0780c922012-06-05 10:26:43 +0200620 void (*pebs_aliases)(struct perf_event *event);
Andi Kleen70ab7002012-06-05 17:56:48 -0700621 int max_pebs_events;
Kan Liang174afc32018-03-12 10:45:37 -0400622 unsigned long large_pebs_flags;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300623
624 /*
625 * Intel LBR
626 */
627 unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */
628 int lbr_nr; /* hardware stack size */
Stephane Eranianb36817e2012-02-09 23:20:53 +0100629 u64 lbr_sel_mask; /* LBR_SELECT valid bits */
630 const int *lbr_sel_map; /* lbr_select mappings */
Andi Kleenb7af41a2013-09-20 07:40:44 -0700631 bool lbr_double_abort; /* duplicated lbr aborts */
Andi Kleenb0c1ef52016-12-08 16:14:17 -0800632 bool lbr_pt_coexist; /* (LBR|BTS) may coexist with PT */
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300633
634 /*
Alexander Shishkin48070342015-01-14 14:18:20 +0200635 * Intel PT/LBR/BTS are exclusive
636 */
637 atomic_t lbr_exclusive[x86_lbr_exclusive_max];
638
639 /*
Peter Zijlstra32b62f42016-03-25 15:52:35 +0100640 * AMD bits
641 */
642 unsigned int amd_nb_constraints : 1;
643
644 /*
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300645 * Extra registers for events
646 */
647 struct extra_reg *extra_regs;
Stephane Eranian9a5e3fb2014-11-17 20:06:53 +0100648 unsigned int flags;
Gleb Natapov144d31e2011-10-05 14:01:21 +0200649
650 /*
651 * Intel host/guest support (KVM)
652 */
653 struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr);
Jiri Olsa81ec3f32019-02-04 13:35:32 +0100654
655 /*
656 * Check period value for PERF_EVENT_IOC_PERIOD ioctl.
657 */
658 int (*check_period) (struct perf_event *event, u64 period);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300659};
660
Yan, Zhenge18bf522014-11-04 21:56:03 -0500661struct x86_perf_task_context {
662 u64 lbr_from[MAX_LBR_ENTRIES];
663 u64 lbr_to[MAX_LBR_ENTRIES];
Andi Kleen50eab8f2015-05-10 12:22:43 -0700664 u64 lbr_info[MAX_LBR_ENTRIES];
Andi Kleenb28ae952015-10-20 11:46:33 -0700665 int tos;
Kan Liang0592e572018-06-05 08:38:45 -0700666 int valid_lbrs;
Yan, Zhenge18bf522014-11-04 21:56:03 -0500667 int lbr_callstack_users;
668 int lbr_stack_state;
Kan Liang8b077e4a2018-06-05 08:38:46 -0700669 int log_id;
Yan, Zhenge18bf522014-11-04 21:56:03 -0500670};
671
Peter Zijlstrac1d6f422011-12-06 14:07:15 +0100672#define x86_add_quirk(func_) \
673do { \
674 static struct x86_pmu_quirk __quirk __initdata = { \
675 .func = func_, \
676 }; \
677 __quirk.next = x86_pmu.quirks; \
678 x86_pmu.quirks = &__quirk; \
679} while (0)
680
Stephane Eranian9a5e3fb2014-11-17 20:06:53 +0100681/*
682 * x86_pmu flags
683 */
684#define PMU_FL_NO_HT_SHARING 0x1 /* no hyper-threading resource sharing */
685#define PMU_FL_HAS_RSP_1 0x2 /* has 2 equivalent offcore_rsp regs */
Maria Dimakopoulou6f6539c2014-11-17 20:06:57 +0100686#define PMU_FL_EXCL_CNTRS 0x4 /* has exclusive counter requirements */
Stephane Eranianb37609c2014-11-17 20:07:04 +0100687#define PMU_FL_EXCL_ENABLED 0x8 /* exclusive counter active */
Kan Liang31962342018-03-08 18:15:39 -0800688#define PMU_FL_PEBS_ALL 0x10 /* all events are valid PEBS events */
Peter Zijlstra (Intel)400816f2019-03-05 22:23:18 +0100689#define PMU_FL_TFA 0x20 /* deal with TSX force abort */
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300690
Stephane Eranian3a54aaa2013-01-24 16:10:26 +0100691#define EVENT_VAR(_id) event_attr_##_id
692#define EVENT_PTR(_id) &event_attr_##_id.attr.attr
693
694#define EVENT_ATTR(_name, _id) \
695static struct perf_pmu_events_attr EVENT_VAR(_id) = { \
696 .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \
697 .id = PERF_COUNT_HW_##_id, \
698 .event_str = NULL, \
699};
700
701#define EVENT_ATTR_STR(_name, v, str) \
702static struct perf_pmu_events_attr event_attr_##v = { \
703 .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \
704 .id = 0, \
705 .event_str = str, \
706};
707
Andi Kleenfc07e9f2016-05-19 17:09:56 -0700708#define EVENT_ATTR_STR_HT(_name, v, noht, ht) \
709static struct perf_pmu_events_ht_attr event_attr_##v = { \
710 .attr = __ATTR(_name, 0444, events_ht_sysfs_show, NULL),\
711 .id = 0, \
712 .event_str_noht = noht, \
713 .event_str_ht = ht, \
714}
715
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300716extern struct x86_pmu x86_pmu __read_mostly;
717
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -0500718static inline bool x86_pmu_has_lbr_callstack(void)
719{
720 return x86_pmu.lbr_sel_map &&
721 x86_pmu.lbr_sel_map[PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] > 0;
722}
723
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300724DECLARE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
725
726int x86_perf_event_set_period(struct perf_event *event);
727
728/*
729 * Generalized hw caching related hw_event table, filled
730 * in on a per model basis. A value of 0 means
731 * 'not supported', -1 means 'hw_event makes no sense on
732 * this CPU', any other value means the raw hw_event
733 * ID.
734 */
735
736#define C(x) PERF_COUNT_HW_CACHE_##x
737
738extern u64 __read_mostly hw_cache_event_ids
739 [PERF_COUNT_HW_CACHE_MAX]
740 [PERF_COUNT_HW_CACHE_OP_MAX]
741 [PERF_COUNT_HW_CACHE_RESULT_MAX];
742extern u64 __read_mostly hw_cache_extra_regs
743 [PERF_COUNT_HW_CACHE_MAX]
744 [PERF_COUNT_HW_CACHE_OP_MAX]
745 [PERF_COUNT_HW_CACHE_RESULT_MAX];
746
747u64 x86_perf_event_update(struct perf_event *event);
748
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300749static inline unsigned int x86_pmu_config_addr(int index)
750{
Jacob Shin4c1fd172013-02-06 11:26:27 -0600751 return x86_pmu.eventsel + (x86_pmu.addr_offset ?
752 x86_pmu.addr_offset(index, true) : index);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300753}
754
755static inline unsigned int x86_pmu_event_addr(int index)
756{
Jacob Shin4c1fd172013-02-06 11:26:27 -0600757 return x86_pmu.perfctr + (x86_pmu.addr_offset ?
758 x86_pmu.addr_offset(index, false) : index);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300759}
760
Jacob Shin0fbdad02013-02-06 11:26:28 -0600761static inline int x86_pmu_rdpmc_index(int index)
762{
763 return x86_pmu.rdpmc_index ? x86_pmu.rdpmc_index(index) : index;
764}
765
Alexander Shishkin48070342015-01-14 14:18:20 +0200766int x86_add_exclusive(unsigned int what);
767
768void x86_del_exclusive(unsigned int what);
769
Alexander Shishkin6b099d92015-06-11 15:13:56 +0300770int x86_reserve_hardware(void);
771
772void x86_release_hardware(void);
773
Andi Kleenb00233b2017-08-22 11:52:01 -0700774int x86_pmu_max_precise(void);
775
Alexander Shishkin48070342015-01-14 14:18:20 +0200776void hw_perf_lbr_event_destroy(struct perf_event *event);
777
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300778int x86_setup_perfctr(struct perf_event *event);
779
780int x86_pmu_hw_config(struct perf_event *event);
781
782void x86_pmu_disable_all(void);
783
784static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc,
785 u64 enable_mask)
786{
Joerg Roedel1018faa2012-02-29 14:57:32 +0100787 u64 disable_mask = __this_cpu_read(cpu_hw_events.perf_ctr_virt_mask);
788
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300789 if (hwc->extra_reg.reg)
790 wrmsrl(hwc->extra_reg.reg, hwc->extra_reg.config);
Joerg Roedel1018faa2012-02-29 14:57:32 +0100791 wrmsrl(hwc->config_base, (hwc->config | enable_mask) & ~disable_mask);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300792}
793
794void x86_pmu_enable_all(int added);
795
Peter Zijlstrab371b592015-05-21 10:57:13 +0200796int perf_assign_events(struct event_constraint **constraints, int n,
Peter Zijlstracc1790c2015-05-21 10:57:17 +0200797 int wmin, int wmax, int gpmax, int *assign);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300798int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign);
799
800void x86_pmu_stop(struct perf_event *event, int flags);
801
802static inline void x86_pmu_disable_event(struct perf_event *event)
803{
804 struct hw_perf_event *hwc = &event->hw;
805
806 wrmsrl(hwc->config_base, hwc->config);
807}
808
809void x86_pmu_enable_event(struct perf_event *event);
810
811int x86_pmu_handle_irq(struct pt_regs *regs);
812
813extern struct event_constraint emptyconstraint;
814
815extern struct event_constraint unconstrained;
816
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100817static inline bool kernel_ip(unsigned long ip)
818{
819#ifdef CONFIG_X86_32
820 return ip > PAGE_OFFSET;
821#else
822 return (long)ip < 0;
823#endif
824}
825
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +0200826/*
827 * Not all PMUs provide the right context information to place the reported IP
828 * into full context. Specifically segment registers are typically not
829 * supplied.
830 *
831 * Assuming the address is a linear address (it is for IBS), we fake the CS and
832 * vm86 mode using the known zero-based code segment and 'fix up' the registers
833 * to reflect this.
834 *
835 * Intel PEBS/LBR appear to typically provide the effective address, nothing
836 * much we can do about that but pray and treat it like a linear address.
837 */
838static inline void set_linear_ip(struct pt_regs *regs, unsigned long ip)
839{
840 regs->cs = kernel_ip(ip) ? __KERNEL_CS : __USER_CS;
841 if (regs->flags & X86_VM_MASK)
842 regs->flags ^= (PERF_EFLAGS_VM | X86_VM_MASK);
843 regs->ip = ip;
844}
845
Jiri Olsa0bf79d42012-10-10 14:53:14 +0200846ssize_t x86_event_sysfs_show(char *page, u64 config, u64 event);
Jiri Olsa20550a42012-10-10 14:53:15 +0200847ssize_t intel_event_sysfs_show(char *page, u64 config);
Jiri Olsa43c032f2012-10-10 14:53:13 +0200848
Andi Kleen47732d82015-06-29 14:22:13 -0700849struct attribute **merge_attr(struct attribute **a, struct attribute **b);
850
Huang Ruia49ac9f2016-03-25 11:18:25 +0800851ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr,
852 char *page);
Andi Kleenfc07e9f2016-05-19 17:09:56 -0700853ssize_t events_ht_sysfs_show(struct device *dev, struct device_attribute *attr,
854 char *page);
Huang Ruia49ac9f2016-03-25 11:18:25 +0800855
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300856#ifdef CONFIG_CPU_SUP_AMD
857
858int amd_pmu_init(void);
859
860#else /* CONFIG_CPU_SUP_AMD */
861
862static inline int amd_pmu_init(void)
863{
864 return 0;
865}
866
867#endif /* CONFIG_CPU_SUP_AMD */
868
869#ifdef CONFIG_CPU_SUP_INTEL
870
Jiri Olsa81ec3f32019-02-04 13:35:32 +0100871static inline bool intel_pmu_has_bts_period(struct perf_event *event, u64 period)
Alexander Shishkin48070342015-01-14 14:18:20 +0200872{
Jiri Olsa67266c12018-11-21 11:16:11 +0100873 struct hw_perf_event *hwc = &event->hw;
874 unsigned int hw_event, bts_event;
Alexander Shishkin48070342015-01-14 14:18:20 +0200875
Jiri Olsa67266c12018-11-21 11:16:11 +0100876 if (event->attr.freq)
877 return false;
878
879 hw_event = hwc->config & INTEL_ARCH_EVENT_MASK;
880 bts_event = x86_pmu.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS);
881
Jiri Olsa81ec3f32019-02-04 13:35:32 +0100882 return hw_event == bts_event && period == 1;
883}
884
885static inline bool intel_pmu_has_bts(struct perf_event *event)
886{
887 struct hw_perf_event *hwc = &event->hw;
888
889 return intel_pmu_has_bts_period(event, hwc->sample_period);
Alexander Shishkin48070342015-01-14 14:18:20 +0200890}
891
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300892int intel_pmu_save_and_restart(struct perf_event *event);
893
894struct event_constraint *
Stephane Eranian79cba822014-11-17 20:06:56 +0100895x86_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
896 struct perf_event *event);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300897
Peter Zijlstra (Intel)d01b1f92019-03-05 22:23:15 +0100898extern int intel_cpuc_prepare(struct cpu_hw_events *cpuc, int cpu);
899extern void intel_cpuc_finish(struct cpu_hw_events *cpuc);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300900
901int intel_pmu_init(void);
902
903void init_debug_store_on_cpu(int cpu);
904
905void fini_debug_store_on_cpu(int cpu);
906
907void release_ds_buffers(void);
908
909void reserve_ds_buffers(void);
910
911extern struct event_constraint bts_constraint;
912
913void intel_pmu_enable_bts(u64 config);
914
915void intel_pmu_disable_bts(void);
916
917int intel_pmu_drain_bts_buffer(void);
918
919extern struct event_constraint intel_core2_pebs_event_constraints[];
920
921extern struct event_constraint intel_atom_pebs_event_constraints[];
922
Yan, Zheng1fa64182013-07-18 17:02:24 +0800923extern struct event_constraint intel_slm_pebs_event_constraints[];
924
Kan Liang8b92c3a2016-04-15 00:42:47 -0700925extern struct event_constraint intel_glm_pebs_event_constraints[];
926
Kan Liangdd0b06b2017-07-12 09:44:23 -0400927extern struct event_constraint intel_glp_pebs_event_constraints[];
928
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300929extern struct event_constraint intel_nehalem_pebs_event_constraints[];
930
931extern struct event_constraint intel_westmere_pebs_event_constraints[];
932
933extern struct event_constraint intel_snb_pebs_event_constraints[];
934
Stephane Eranian20a36e32012-09-11 01:07:01 +0200935extern struct event_constraint intel_ivb_pebs_event_constraints[];
936
Andi Kleen30443182013-06-17 17:36:49 -0700937extern struct event_constraint intel_hsw_pebs_event_constraints[];
938
Stephane Eranianb3e62462016-03-03 20:50:42 +0100939extern struct event_constraint intel_bdw_pebs_event_constraints[];
940
Andi Kleen9a92e162015-05-10 12:22:44 -0700941extern struct event_constraint intel_skl_pebs_event_constraints[];
942
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300943struct event_constraint *intel_pebs_constraints(struct perf_event *event);
944
Peter Zijlstra68f70822016-07-06 18:02:43 +0200945void intel_pmu_pebs_add(struct perf_event *event);
946
947void intel_pmu_pebs_del(struct perf_event *event);
948
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300949void intel_pmu_pebs_enable(struct perf_event *event);
950
951void intel_pmu_pebs_disable(struct perf_event *event);
952
953void intel_pmu_pebs_enable_all(void);
954
955void intel_pmu_pebs_disable_all(void);
956
Yan, Zheng9c964ef2015-05-06 15:33:51 -0400957void intel_pmu_pebs_sched_task(struct perf_event_context *ctx, bool sched_in);
958
Kan Liang5bee2cc2018-02-12 14:20:33 -0800959void intel_pmu_auto_reload_read(struct perf_event *event);
960
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300961void intel_ds_init(void);
962
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500963void intel_pmu_lbr_sched_task(struct perf_event_context *ctx, bool sched_in);
964
David Carrillo-Cisneros19fc9dd2016-06-21 11:31:11 -0700965u64 lbr_from_signext_quirk_wr(u64 val);
966
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300967void intel_pmu_lbr_reset(void);
968
Peter Zijlstra68f70822016-07-06 18:02:43 +0200969void intel_pmu_lbr_add(struct perf_event *event);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300970
Peter Zijlstra68f70822016-07-06 18:02:43 +0200971void intel_pmu_lbr_del(struct perf_event *event);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300972
Andi Kleen1a78d932015-03-20 10:11:23 -0700973void intel_pmu_lbr_enable_all(bool pmi);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300974
975void intel_pmu_lbr_disable_all(void);
976
977void intel_pmu_lbr_read(void);
978
979void intel_pmu_lbr_init_core(void);
980
981void intel_pmu_lbr_init_nhm(void);
982
983void intel_pmu_lbr_init_atom(void);
984
Kan Liangf21d5ad2016-04-15 00:53:45 -0700985void intel_pmu_lbr_init_slm(void);
986
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100987void intel_pmu_lbr_init_snb(void);
988
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -0500989void intel_pmu_lbr_init_hsw(void);
990
Andi Kleen9a92e162015-05-10 12:22:44 -0700991void intel_pmu_lbr_init_skl(void);
992
Harish Chegondi1e7b9392015-12-07 14:28:18 -0800993void intel_pmu_lbr_init_knl(void);
994
Andi Kleene17dc652016-03-01 14:25:24 -0800995void intel_pmu_pebs_data_source_nhm(void);
996
Andi Kleen6ae5fa62017-08-16 15:21:54 -0700997void intel_pmu_pebs_data_source_skl(bool pmem);
998
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100999int intel_pmu_setup_lbr_filter(struct perf_event *event);
1000
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001001void intel_pt_interrupt(void);
1002
Alexander Shishkin80623822015-01-30 12:40:35 +02001003int intel_bts_interrupt(void);
1004
1005void intel_bts_enable_local(void);
1006
1007void intel_bts_disable_local(void);
1008
Kevin Winchesterde0428a2011-08-30 20:41:05 -03001009int p4_pmu_init(void);
1010
1011int p6_pmu_init(void);
1012
Vince Weavere717bf42012-09-26 14:12:52 -04001013int knc_pmu_init(void);
1014
Stephane Eranianb37609c2014-11-17 20:07:04 +01001015static inline int is_ht_workaround_enabled(void)
1016{
1017 return !!(x86_pmu.flags & PMU_FL_EXCL_ENABLED);
1018}
Andi Kleen47732d82015-06-29 14:22:13 -07001019
Kevin Winchesterde0428a2011-08-30 20:41:05 -03001020#else /* CONFIG_CPU_SUP_INTEL */
1021
1022static inline void reserve_ds_buffers(void)
1023{
1024}
1025
1026static inline void release_ds_buffers(void)
1027{
1028}
1029
1030static inline int intel_pmu_init(void)
1031{
1032 return 0;
1033}
1034
Peter Zijlstraf764c582019-03-15 09:14:10 +01001035static inline int intel_cpuc_prepare(struct cpu_hw_events *cpuc, int cpu)
Kevin Winchesterde0428a2011-08-30 20:41:05 -03001036{
Peter Zijlstra (Intel)d01b1f92019-03-05 22:23:15 +01001037 return 0;
1038}
1039
Peter Zijlstraf764c582019-03-15 09:14:10 +01001040static inline void intel_cpuc_finish(struct cpu_hw_events *cpuc)
Peter Zijlstra (Intel)d01b1f92019-03-05 22:23:15 +01001041{
Kevin Winchesterde0428a2011-08-30 20:41:05 -03001042}
1043
Peter Zijlstracc1790c2015-05-21 10:57:17 +02001044static inline int is_ht_workaround_enabled(void)
1045{
1046 return 0;
1047}
Kevin Winchesterde0428a2011-08-30 20:41:05 -03001048#endif /* CONFIG_CPU_SUP_INTEL */