perf/x86/intel: Make the HT bug workaround conditional on HT enabled
[linux-2.6-block.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
1c2ac3fd
PZ
17#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
de0428a7
KW
29/*
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 */
b36817e8 48 EXTRA_REG_LBR = 2, /* lbr_select */
f20093ee 49 EXTRA_REG_LDLAT = 3, /* ld_lat_threshold */
de0428a7
KW
50
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;
bc1738f6 62 int overlap;
9fac2cf3 63 int flags;
de0428a7 64};
f20093ee 65/*
2f7f73a5 66 * struct hw_perf_event.flags flags
f20093ee
SE
67 */
68#define PERF_X86_EVENT_PEBS_LDLAT 0x1 /* ld+ldlat data address sampling */
9ad64c0f 69#define PERF_X86_EVENT_PEBS_ST 0x2 /* st data address sampling */
86a04461 70#define PERF_X86_EVENT_PEBS_ST_HSW 0x4 /* haswell style datala, store */
2f7f73a5 71#define PERF_X86_EVENT_COMMITTED 0x8 /* event passed commit_txn */
86a04461
AK
72#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 */
6f6539ca 74#define PERF_X86_EVENT_EXCL 0x40 /* HT exclusivity on counter */
e979121b 75#define PERF_X86_EVENT_DYNAMIC 0x80 /* dynamic alloc'd constraint */
7911d3f7
AL
76#define PERF_X86_EVENT_RDPMC_ALLOWED 0x40 /* grant rdpmc permission */
77
de0428a7
KW
78
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: */
70ab7003 87#define MAX_PEBS_EVENTS 8
de0428a7
KW
88
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
6f6539ca
MD
128enum 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];
c02cdbf6
SE
137 int num_alloc_cntrs;/* #counters allocated */
138 int max_alloc_cntrs;/* max #counters allowed */
e979121b 139 bool sched_started; /* true if scheduling has started */
6f6539ca
MD
140};
141
142struct intel_excl_cntrs {
143 raw_spinlock_t lock;
144
145 struct intel_excl_states states[2];
146
147 int refcnt; /* per-core: #HT threads */
148 unsigned core_id; /* per-core: core id */
149};
150
de0428a7
KW
151#define MAX_LBR_ENTRIES 16
152
90413464
SE
153enum {
154 X86_PERF_KFREE_SHARED = 0,
155 X86_PERF_KFREE_EXCL = 1,
156 X86_PERF_KFREE_MAX
157};
158
de0428a7
KW
159struct cpu_hw_events {
160 /*
161 * Generic x86 PMC bits
162 */
163 struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */
164 unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
165 unsigned long running[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
166 int enabled;
167
c347a2f1
PZ
168 int n_events; /* the # of events in the below arrays */
169 int n_added; /* the # last events in the below arrays;
170 they've never been enabled yet */
171 int n_txn; /* the # last events in the below arrays;
172 added in the current transaction */
de0428a7
KW
173 int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */
174 u64 tags[X86_PMC_IDX_MAX];
175 struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */
176
177 unsigned int group_flag;
5a425294 178 int is_fake;
de0428a7
KW
179
180 /*
181 * Intel DebugStore bits
182 */
183 struct debug_store *ds;
184 u64 pebs_enabled;
185
186 /*
187 * Intel LBR bits
188 */
189 int lbr_users;
190 void *lbr_context;
191 struct perf_branch_stack lbr_stack;
192 struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES];
b36817e8 193 struct er_account *lbr_sel;
3e702ff6 194 u64 br_sel;
de0428a7 195
144d31e6
GN
196 /*
197 * Intel host/guest exclude bits
198 */
199 u64 intel_ctrl_guest_mask;
200 u64 intel_ctrl_host_mask;
201 struct perf_guest_switch_msr guest_switch_msrs[X86_PMC_IDX_MAX];
202
2b9e344d
PZ
203 /*
204 * Intel checkpoint mask
205 */
206 u64 intel_cp_status;
207
de0428a7
KW
208 /*
209 * manage shared (per-core, per-cpu) registers
210 * used on Intel NHM/WSM/SNB
211 */
212 struct intel_shared_regs *shared_regs;
6f6539ca
MD
213 /*
214 * manage exclusive counter access between hyperthread
215 */
216 struct event_constraint *constraint_list; /* in enable order */
217 struct intel_excl_cntrs *excl_cntrs;
218 int excl_thread_id; /* 0 or 1 */
de0428a7
KW
219
220 /*
221 * AMD specific bits
222 */
1018faa6
JR
223 struct amd_nb *amd_nb;
224 /* Inverted mask of bits to clear in the perf_ctr ctrl registers */
225 u64 perf_ctr_virt_mask;
de0428a7 226
90413464 227 void *kfree_on_online[X86_PERF_KFREE_MAX];
de0428a7
KW
228};
229
9fac2cf3 230#define __EVENT_CONSTRAINT(c, n, m, w, o, f) {\
de0428a7
KW
231 { .idxmsk64 = (n) }, \
232 .code = (c), \
233 .cmask = (m), \
234 .weight = (w), \
bc1738f6 235 .overlap = (o), \
9fac2cf3 236 .flags = f, \
de0428a7
KW
237}
238
239#define EVENT_CONSTRAINT(c, n, m) \
9fac2cf3 240 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 0, 0)
bc1738f6 241
6f6539ca
MD
242#define INTEL_EXCLEVT_CONSTRAINT(c, n) \
243 __EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT, HWEIGHT(n),\
244 0, PERF_X86_EVENT_EXCL)
245
bc1738f6
RR
246/*
247 * The overlap flag marks event constraints with overlapping counter
248 * masks. This is the case if the counter mask of such an event is not
249 * a subset of any other counter mask of a constraint with an equal or
250 * higher weight, e.g.:
251 *
252 * c_overlaps = EVENT_CONSTRAINT_OVERLAP(0, 0x09, 0);
253 * c_another1 = EVENT_CONSTRAINT(0, 0x07, 0);
254 * c_another2 = EVENT_CONSTRAINT(0, 0x38, 0);
255 *
256 * The event scheduler may not select the correct counter in the first
257 * cycle because it needs to know which subsequent events will be
258 * scheduled. It may fail to schedule the events then. So we set the
259 * overlap flag for such constraints to give the scheduler a hint which
260 * events to select for counter rescheduling.
261 *
262 * Care must be taken as the rescheduling algorithm is O(n!) which
263 * will increase scheduling cycles for an over-commited system
264 * dramatically. The number of such EVENT_CONSTRAINT_OVERLAP() macros
265 * and its counter masks must be kept at a minimum.
266 */
267#define EVENT_CONSTRAINT_OVERLAP(c, n, m) \
9fac2cf3 268 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 1, 0)
de0428a7
KW
269
270/*
271 * Constraint on the Event code.
272 */
273#define INTEL_EVENT_CONSTRAINT(c, n) \
274 EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT)
275
276/*
277 * Constraint on the Event code + UMask + fixed-mask
278 *
279 * filter mask to validate fixed counter events.
280 * the following filters disqualify for fixed counters:
281 * - inv
282 * - edge
283 * - cnt-mask
3a632cb2
AK
284 * - in_tx
285 * - in_tx_checkpointed
de0428a7
KW
286 * The other filters are supported by fixed counters.
287 * The any-thread option is supported starting with v3.
288 */
3a632cb2 289#define FIXED_EVENT_FLAGS (X86_RAW_EVENT_MASK|HSW_IN_TX|HSW_IN_TX_CHECKPOINTED)
de0428a7 290#define FIXED_EVENT_CONSTRAINT(c, n) \
3a632cb2 291 EVENT_CONSTRAINT(c, (1ULL << (32+n)), FIXED_EVENT_FLAGS)
de0428a7
KW
292
293/*
294 * Constraint on the Event code + UMask
295 */
296#define INTEL_UEVENT_CONSTRAINT(c, n) \
297 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
298
7550ddff
AK
299/* Like UEVENT_CONSTRAINT, but match flags too */
300#define INTEL_FLAGS_UEVENT_CONSTRAINT(c, n) \
301 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS)
302
e979121b
MD
303#define INTEL_EXCLUEVT_CONSTRAINT(c, n) \
304 __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK, \
305 HWEIGHT(n), 0, PERF_X86_EVENT_EXCL)
306
f20093ee 307#define INTEL_PLD_CONSTRAINT(c, n) \
86a04461 308 __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
f20093ee
SE
309 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LDLAT)
310
9ad64c0f 311#define INTEL_PST_CONSTRAINT(c, n) \
86a04461 312 __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
9ad64c0f
SE
313 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST)
314
86a04461
AK
315/* Event constraint, but match on all event flags too. */
316#define INTEL_FLAGS_EVENT_CONSTRAINT(c, n) \
317 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS)
318
319/* Check only flags, but allow all event/umask */
320#define INTEL_ALL_EVENT_CONSTRAINT(code, n) \
321 EVENT_CONSTRAINT(code, n, X86_ALL_EVENT_FLAGS)
322
323/* Check flags and event code, and set the HSW store flag */
324#define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_ST(code, n) \
325 __EVENT_CONSTRAINT(code, n, \
326 ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
327 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST_HSW)
328
329/* Check flags and event code, and set the HSW load flag */
330#define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(code, n) \
b63b4b45 331 __EVENT_CONSTRAINT(code, n, \
86a04461
AK
332 ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
333 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW)
334
b63b4b45
MD
335#define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(code, n) \
336 __EVENT_CONSTRAINT(code, n, \
337 ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
338 HWEIGHT(n), 0, \
339 PERF_X86_EVENT_PEBS_LD_HSW|PERF_X86_EVENT_EXCL)
340
86a04461
AK
341/* Check flags and event code/umask, and set the HSW store flag */
342#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(code, n) \
343 __EVENT_CONSTRAINT(code, n, \
344 INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
f9134f36
AK
345 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST_HSW)
346
b63b4b45
MD
347#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(code, n) \
348 __EVENT_CONSTRAINT(code, n, \
349 INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
350 HWEIGHT(n), 0, \
351 PERF_X86_EVENT_PEBS_ST_HSW|PERF_X86_EVENT_EXCL)
352
86a04461
AK
353/* Check flags and event code/umask, and set the HSW load flag */
354#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(code, n) \
355 __EVENT_CONSTRAINT(code, n, \
356 INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
357 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW)
358
b63b4b45
MD
359#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(code, n) \
360 __EVENT_CONSTRAINT(code, n, \
361 INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
362 HWEIGHT(n), 0, \
363 PERF_X86_EVENT_PEBS_LD_HSW|PERF_X86_EVENT_EXCL)
364
86a04461
AK
365/* Check flags and event code/umask, and set the HSW N/A flag */
366#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(code, n) \
367 __EVENT_CONSTRAINT(code, n, \
368 INTEL_ARCH_EVENT_MASK|INTEL_ARCH_EVENT_MASK, \
369 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_NA_HSW)
370
371
cf30d52e
MD
372/*
373 * We define the end marker as having a weight of -1
374 * to enable blacklisting of events using a counter bitmask
375 * of zero and thus a weight of zero.
376 * The end marker has a weight that cannot possibly be
377 * obtained from counting the bits in the bitmask.
378 */
379#define EVENT_CONSTRAINT_END { .weight = -1 }
de0428a7 380
cf30d52e
MD
381/*
382 * Check for end marker with weight == -1
383 */
de0428a7 384#define for_each_event_constraint(e, c) \
cf30d52e 385 for ((e) = (c); (e)->weight != -1; (e)++)
de0428a7
KW
386
387/*
388 * Extra registers for specific events.
389 *
390 * Some events need large masks and require external MSRs.
391 * Those extra MSRs end up being shared for all events on
392 * a PMU and sometimes between PMU of sibling HT threads.
393 * In either case, the kernel needs to handle conflicting
394 * accesses to those extra, shared, regs. The data structure
395 * to manage those registers is stored in cpu_hw_event.
396 */
397struct extra_reg {
398 unsigned int event;
399 unsigned int msr;
400 u64 config_mask;
401 u64 valid_mask;
402 int idx; /* per_xxx->regs[] reg index */
338b522c 403 bool extra_msr_access;
de0428a7
KW
404};
405
406#define EVENT_EXTRA_REG(e, ms, m, vm, i) { \
338b522c
KL
407 .event = (e), \
408 .msr = (ms), \
409 .config_mask = (m), \
410 .valid_mask = (vm), \
411 .idx = EXTRA_REG_##i, \
412 .extra_msr_access = true, \
de0428a7
KW
413 }
414
415#define INTEL_EVENT_EXTRA_REG(event, msr, vm, idx) \
416 EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm, idx)
417
f20093ee
SE
418#define INTEL_UEVENT_EXTRA_REG(event, msr, vm, idx) \
419 EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT | \
420 ARCH_PERFMON_EVENTSEL_UMASK, vm, idx)
421
422#define INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(c) \
423 INTEL_UEVENT_EXTRA_REG(c, \
424 MSR_PEBS_LD_LAT_THRESHOLD, \
425 0xffff, \
426 LDLAT)
427
de0428a7
KW
428#define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0, RSP_0)
429
430union perf_capabilities {
431 struct {
432 u64 lbr_format:6;
433 u64 pebs_trap:1;
434 u64 pebs_arch_reg:1;
435 u64 pebs_format:4;
436 u64 smm_freeze:1;
069e0c3c
AK
437 /*
438 * PMU supports separate counter range for writing
439 * values > 32bit.
440 */
441 u64 full_width_write:1;
de0428a7
KW
442 };
443 u64 capabilities;
444};
445
c1d6f42f
PZ
446struct x86_pmu_quirk {
447 struct x86_pmu_quirk *next;
448 void (*func)(void);
449};
450
f9b4eeb8
PZ
451union x86_pmu_config {
452 struct {
453 u64 event:8,
454 umask:8,
455 usr:1,
456 os:1,
457 edge:1,
458 pc:1,
459 interrupt:1,
460 __reserved1:1,
461 en:1,
462 inv:1,
463 cmask:8,
464 event2:4,
465 __reserved2:4,
466 go:1,
467 ho:1;
468 } bits;
469 u64 value;
470};
471
472#define X86_CONFIG(args...) ((union x86_pmu_config){.bits = {args}}).value
473
48070342
AS
474enum {
475 x86_lbr_exclusive_lbr,
8062382c 476 x86_lbr_exclusive_bts,
48070342
AS
477 x86_lbr_exclusive_pt,
478 x86_lbr_exclusive_max,
479};
480
de0428a7
KW
481/*
482 * struct x86_pmu - generic x86 pmu
483 */
484struct x86_pmu {
485 /*
486 * Generic x86 PMC bits
487 */
488 const char *name;
489 int version;
490 int (*handle_irq)(struct pt_regs *);
491 void (*disable_all)(void);
492 void (*enable_all)(int added);
493 void (*enable)(struct perf_event *);
494 void (*disable)(struct perf_event *);
495 int (*hw_config)(struct perf_event *event);
496 int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign);
497 unsigned eventsel;
498 unsigned perfctr;
4c1fd17a 499 int (*addr_offset)(int index, bool eventsel);
0fbdad07 500 int (*rdpmc_index)(int index);
de0428a7
KW
501 u64 (*event_map)(int);
502 int max_events;
503 int num_counters;
504 int num_counters_fixed;
505 int cntval_bits;
506 u64 cntval_mask;
ffb871bc
GN
507 union {
508 unsigned long events_maskl;
509 unsigned long events_mask[BITS_TO_LONGS(ARCH_PERFMON_EVENTS_COUNT)];
510 };
511 int events_mask_len;
de0428a7
KW
512 int apic;
513 u64 max_period;
514 struct event_constraint *
515 (*get_event_constraints)(struct cpu_hw_events *cpuc,
79cba822 516 int idx,
de0428a7
KW
517 struct perf_event *event);
518
519 void (*put_event_constraints)(struct cpu_hw_events *cpuc,
520 struct perf_event *event);
c5362c0c
MD
521
522 void (*commit_scheduling)(struct cpu_hw_events *cpuc,
523 struct perf_event *event,
524 int cntr);
525
526 void (*start_scheduling)(struct cpu_hw_events *cpuc);
527
528 void (*stop_scheduling)(struct cpu_hw_events *cpuc);
529
de0428a7 530 struct event_constraint *event_constraints;
c1d6f42f 531 struct x86_pmu_quirk *quirks;
de0428a7 532 int perfctr_second_write;
72db5596 533 bool late_ack;
294fe0f5 534 unsigned (*limit_period)(struct perf_event *event, unsigned l);
de0428a7 535
0c9d42ed
PZ
536 /*
537 * sysfs attrs
538 */
e97df763 539 int attr_rdpmc_broken;
0c9d42ed 540 int attr_rdpmc;
641cc938 541 struct attribute **format_attrs;
f20093ee 542 struct attribute **event_attrs;
0c9d42ed 543
a4747393 544 ssize_t (*events_sysfs_show)(char *page, u64 config);
1a6461b1 545 struct attribute **cpu_events;
a4747393 546
0c9d42ed
PZ
547 /*
548 * CPU Hotplug hooks
549 */
de0428a7
KW
550 int (*cpu_prepare)(int cpu);
551 void (*cpu_starting)(int cpu);
552 void (*cpu_dying)(int cpu);
553 void (*cpu_dead)(int cpu);
c93dc84c
PZ
554
555 void (*check_microcode)(void);
ba532500
YZ
556 void (*sched_task)(struct perf_event_context *ctx,
557 bool sched_in);
de0428a7
KW
558
559 /*
560 * Intel Arch Perfmon v2+
561 */
562 u64 intel_ctrl;
563 union perf_capabilities intel_cap;
564
565 /*
566 * Intel DebugStore bits
567 */
597ed953 568 unsigned int bts :1,
3e0091e2
PZ
569 bts_active :1,
570 pebs :1,
571 pebs_active :1,
572 pebs_broken :1;
de0428a7
KW
573 int pebs_record_size;
574 void (*drain_pebs)(struct pt_regs *regs);
575 struct event_constraint *pebs_constraints;
0780c927 576 void (*pebs_aliases)(struct perf_event *event);
70ab7003 577 int max_pebs_events;
de0428a7
KW
578
579 /*
580 * Intel LBR
581 */
582 unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */
583 int lbr_nr; /* hardware stack size */
b36817e8
SE
584 u64 lbr_sel_mask; /* LBR_SELECT valid bits */
585 const int *lbr_sel_map; /* lbr_select mappings */
b7af41a1 586 bool lbr_double_abort; /* duplicated lbr aborts */
de0428a7 587
48070342
AS
588 /*
589 * Intel PT/LBR/BTS are exclusive
590 */
591 atomic_t lbr_exclusive[x86_lbr_exclusive_max];
592
de0428a7
KW
593 /*
594 * Extra registers for events
595 */
596 struct extra_reg *extra_regs;
9a5e3fb5 597 unsigned int flags;
144d31e6
GN
598
599 /*
600 * Intel host/guest support (KVM)
601 */
602 struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr);
de0428a7
KW
603};
604
e18bf526
YZ
605struct x86_perf_task_context {
606 u64 lbr_from[MAX_LBR_ENTRIES];
607 u64 lbr_to[MAX_LBR_ENTRIES];
608 int lbr_callstack_users;
609 int lbr_stack_state;
610};
611
c1d6f42f
PZ
612#define x86_add_quirk(func_) \
613do { \
614 static struct x86_pmu_quirk __quirk __initdata = { \
615 .func = func_, \
616 }; \
617 __quirk.next = x86_pmu.quirks; \
618 x86_pmu.quirks = &__quirk; \
619} while (0)
620
9a5e3fb5
SE
621/*
622 * x86_pmu flags
623 */
624#define PMU_FL_NO_HT_SHARING 0x1 /* no hyper-threading resource sharing */
625#define PMU_FL_HAS_RSP_1 0x2 /* has 2 equivalent offcore_rsp regs */
6f6539ca 626#define PMU_FL_EXCL_CNTRS 0x4 /* has exclusive counter requirements */
b37609c3 627#define PMU_FL_EXCL_ENABLED 0x8 /* exclusive counter active */
de0428a7 628
3a54aaa0
SE
629#define EVENT_VAR(_id) event_attr_##_id
630#define EVENT_PTR(_id) &event_attr_##_id.attr.attr
631
632#define EVENT_ATTR(_name, _id) \
633static struct perf_pmu_events_attr EVENT_VAR(_id) = { \
634 .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \
635 .id = PERF_COUNT_HW_##_id, \
636 .event_str = NULL, \
637};
638
639#define EVENT_ATTR_STR(_name, v, str) \
640static struct perf_pmu_events_attr event_attr_##v = { \
641 .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \
642 .id = 0, \
643 .event_str = str, \
644};
645
de0428a7
KW
646extern struct x86_pmu x86_pmu __read_mostly;
647
e9d7f7cd
YZ
648static inline bool x86_pmu_has_lbr_callstack(void)
649{
650 return x86_pmu.lbr_sel_map &&
651 x86_pmu.lbr_sel_map[PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] > 0;
652}
653
de0428a7
KW
654DECLARE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
655
656int x86_perf_event_set_period(struct perf_event *event);
657
658/*
659 * Generalized hw caching related hw_event table, filled
660 * in on a per model basis. A value of 0 means
661 * 'not supported', -1 means 'hw_event makes no sense on
662 * this CPU', any other value means the raw hw_event
663 * ID.
664 */
665
666#define C(x) PERF_COUNT_HW_CACHE_##x
667
668extern u64 __read_mostly hw_cache_event_ids
669 [PERF_COUNT_HW_CACHE_MAX]
670 [PERF_COUNT_HW_CACHE_OP_MAX]
671 [PERF_COUNT_HW_CACHE_RESULT_MAX];
672extern u64 __read_mostly hw_cache_extra_regs
673 [PERF_COUNT_HW_CACHE_MAX]
674 [PERF_COUNT_HW_CACHE_OP_MAX]
675 [PERF_COUNT_HW_CACHE_RESULT_MAX];
676
677u64 x86_perf_event_update(struct perf_event *event);
678
de0428a7
KW
679static inline unsigned int x86_pmu_config_addr(int index)
680{
4c1fd17a
JS
681 return x86_pmu.eventsel + (x86_pmu.addr_offset ?
682 x86_pmu.addr_offset(index, true) : index);
de0428a7
KW
683}
684
685static inline unsigned int x86_pmu_event_addr(int index)
686{
4c1fd17a
JS
687 return x86_pmu.perfctr + (x86_pmu.addr_offset ?
688 x86_pmu.addr_offset(index, false) : index);
de0428a7
KW
689}
690
0fbdad07
JS
691static inline int x86_pmu_rdpmc_index(int index)
692{
693 return x86_pmu.rdpmc_index ? x86_pmu.rdpmc_index(index) : index;
694}
695
48070342
AS
696int x86_add_exclusive(unsigned int what);
697
698void x86_del_exclusive(unsigned int what);
699
700void hw_perf_lbr_event_destroy(struct perf_event *event);
701
de0428a7
KW
702int x86_setup_perfctr(struct perf_event *event);
703
704int x86_pmu_hw_config(struct perf_event *event);
705
706void x86_pmu_disable_all(void);
707
708static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc,
709 u64 enable_mask)
710{
1018faa6
JR
711 u64 disable_mask = __this_cpu_read(cpu_hw_events.perf_ctr_virt_mask);
712
de0428a7
KW
713 if (hwc->extra_reg.reg)
714 wrmsrl(hwc->extra_reg.reg, hwc->extra_reg.config);
1018faa6 715 wrmsrl(hwc->config_base, (hwc->config | enable_mask) & ~disable_mask);
de0428a7
KW
716}
717
718void x86_pmu_enable_all(int added);
719
43b45780 720int perf_assign_events(struct perf_event **events, int n,
4b4969b1 721 int wmin, int wmax, int *assign);
de0428a7
KW
722int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign);
723
724void x86_pmu_stop(struct perf_event *event, int flags);
725
726static inline void x86_pmu_disable_event(struct perf_event *event)
727{
728 struct hw_perf_event *hwc = &event->hw;
729
730 wrmsrl(hwc->config_base, hwc->config);
731}
732
733void x86_pmu_enable_event(struct perf_event *event);
734
735int x86_pmu_handle_irq(struct pt_regs *regs);
736
737extern struct event_constraint emptyconstraint;
738
739extern struct event_constraint unconstrained;
740
3e702ff6
SE
741static inline bool kernel_ip(unsigned long ip)
742{
743#ifdef CONFIG_X86_32
744 return ip > PAGE_OFFSET;
745#else
746 return (long)ip < 0;
747#endif
748}
749
d07bdfd3
PZ
750/*
751 * Not all PMUs provide the right context information to place the reported IP
752 * into full context. Specifically segment registers are typically not
753 * supplied.
754 *
755 * Assuming the address is a linear address (it is for IBS), we fake the CS and
756 * vm86 mode using the known zero-based code segment and 'fix up' the registers
757 * to reflect this.
758 *
759 * Intel PEBS/LBR appear to typically provide the effective address, nothing
760 * much we can do about that but pray and treat it like a linear address.
761 */
762static inline void set_linear_ip(struct pt_regs *regs, unsigned long ip)
763{
764 regs->cs = kernel_ip(ip) ? __KERNEL_CS : __USER_CS;
765 if (regs->flags & X86_VM_MASK)
766 regs->flags ^= (PERF_EFLAGS_VM | X86_VM_MASK);
767 regs->ip = ip;
768}
769
0bf79d44 770ssize_t x86_event_sysfs_show(char *page, u64 config, u64 event);
20550a43 771ssize_t intel_event_sysfs_show(char *page, u64 config);
43c032fe 772
de0428a7
KW
773#ifdef CONFIG_CPU_SUP_AMD
774
775int amd_pmu_init(void);
776
777#else /* CONFIG_CPU_SUP_AMD */
778
779static inline int amd_pmu_init(void)
780{
781 return 0;
782}
783
784#endif /* CONFIG_CPU_SUP_AMD */
785
786#ifdef CONFIG_CPU_SUP_INTEL
787
48070342
AS
788static inline bool intel_pmu_needs_lbr_smpl(struct perf_event *event)
789{
790 /* user explicitly requested branch sampling */
791 if (has_branch_stack(event))
792 return true;
793
794 /* implicit branch sampling to correct PEBS skid */
795 if (x86_pmu.intel_cap.pebs_trap && event->attr.precise_ip > 1 &&
796 x86_pmu.intel_cap.pebs_format < 2)
797 return true;
798
799 return false;
800}
801
802static inline bool intel_pmu_has_bts(struct perf_event *event)
803{
804 if (event->attr.config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS &&
805 !event->attr.freq && event->hw.sample_period == 1)
806 return true;
807
808 return false;
809}
810
de0428a7
KW
811int intel_pmu_save_and_restart(struct perf_event *event);
812
813struct event_constraint *
79cba822
SE
814x86_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
815 struct perf_event *event);
de0428a7
KW
816
817struct intel_shared_regs *allocate_shared_regs(int cpu);
818
819int intel_pmu_init(void);
820
821void init_debug_store_on_cpu(int cpu);
822
823void fini_debug_store_on_cpu(int cpu);
824
825void release_ds_buffers(void);
826
827void reserve_ds_buffers(void);
828
829extern struct event_constraint bts_constraint;
830
831void intel_pmu_enable_bts(u64 config);
832
833void intel_pmu_disable_bts(void);
834
835int intel_pmu_drain_bts_buffer(void);
836
837extern struct event_constraint intel_core2_pebs_event_constraints[];
838
839extern struct event_constraint intel_atom_pebs_event_constraints[];
840
1fa64180
YZ
841extern struct event_constraint intel_slm_pebs_event_constraints[];
842
de0428a7
KW
843extern struct event_constraint intel_nehalem_pebs_event_constraints[];
844
845extern struct event_constraint intel_westmere_pebs_event_constraints[];
846
847extern struct event_constraint intel_snb_pebs_event_constraints[];
848
20a36e39
SE
849extern struct event_constraint intel_ivb_pebs_event_constraints[];
850
3044318f
AK
851extern struct event_constraint intel_hsw_pebs_event_constraints[];
852
de0428a7
KW
853struct event_constraint *intel_pebs_constraints(struct perf_event *event);
854
855void intel_pmu_pebs_enable(struct perf_event *event);
856
857void intel_pmu_pebs_disable(struct perf_event *event);
858
859void intel_pmu_pebs_enable_all(void);
860
861void intel_pmu_pebs_disable_all(void);
862
863void intel_ds_init(void);
864
2a0ad3b3
YZ
865void intel_pmu_lbr_sched_task(struct perf_event_context *ctx, bool sched_in);
866
de0428a7
KW
867void intel_pmu_lbr_reset(void);
868
869void intel_pmu_lbr_enable(struct perf_event *event);
870
871void intel_pmu_lbr_disable(struct perf_event *event);
872
873void intel_pmu_lbr_enable_all(void);
874
875void intel_pmu_lbr_disable_all(void);
876
877void intel_pmu_lbr_read(void);
878
879void intel_pmu_lbr_init_core(void);
880
881void intel_pmu_lbr_init_nhm(void);
882
883void intel_pmu_lbr_init_atom(void);
884
c5cc2cd9
SE
885void intel_pmu_lbr_init_snb(void);
886
e9d7f7cd
YZ
887void intel_pmu_lbr_init_hsw(void);
888
60ce0fbd
SE
889int intel_pmu_setup_lbr_filter(struct perf_event *event);
890
52ca9ced
AS
891void intel_pt_interrupt(void);
892
8062382c
AS
893int intel_bts_interrupt(void);
894
895void intel_bts_enable_local(void);
896
897void intel_bts_disable_local(void);
898
de0428a7
KW
899int p4_pmu_init(void);
900
901int p6_pmu_init(void);
902
e717bf4e
VW
903int knc_pmu_init(void);
904
f20093ee
SE
905ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr,
906 char *page);
907
b37609c3
SE
908static inline int is_ht_workaround_enabled(void)
909{
910 return !!(x86_pmu.flags & PMU_FL_EXCL_ENABLED);
911}
de0428a7
KW
912#else /* CONFIG_CPU_SUP_INTEL */
913
914static inline void reserve_ds_buffers(void)
915{
916}
917
918static inline void release_ds_buffers(void)
919{
920}
921
922static inline int intel_pmu_init(void)
923{
924 return 0;
925}
926
927static inline struct intel_shared_regs *allocate_shared_regs(int cpu)
928{
929 return NULL;
930}
931
932#endif /* CONFIG_CPU_SUP_INTEL */