blob: 23b5710b17478835b0243a53c97c3664fc45bcfd [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 */
Kevin Winchesterde0428a2011-08-30 20:41:05 -030049
50 EXTRA_REG_MAX /* number of entries needed */
51};
52
53struct event_constraint {
54 union {
55 unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
56 u64 idxmsk64;
57 };
58 u64 code;
59 u64 cmask;
60 int weight;
Robert Richterbc1738f2011-11-18 12:35:22 +010061 int overlap;
Kevin Winchesterde0428a2011-08-30 20:41:05 -030062};
63
64struct amd_nb {
65 int nb_id; /* NorthBridge id */
66 int refcnt; /* reference count */
67 struct perf_event *owners[X86_PMC_IDX_MAX];
68 struct event_constraint event_constraints[X86_PMC_IDX_MAX];
69};
70
71/* The maximal number of PEBS events: */
72#define MAX_PEBS_EVENTS 4
73
74/*
75 * A debug store configuration.
76 *
77 * We only support architectures that use 64bit fields.
78 */
79struct debug_store {
80 u64 bts_buffer_base;
81 u64 bts_index;
82 u64 bts_absolute_maximum;
83 u64 bts_interrupt_threshold;
84 u64 pebs_buffer_base;
85 u64 pebs_index;
86 u64 pebs_absolute_maximum;
87 u64 pebs_interrupt_threshold;
88 u64 pebs_event_reset[MAX_PEBS_EVENTS];
89};
90
91/*
92 * Per register state.
93 */
94struct er_account {
95 raw_spinlock_t lock; /* per-core: protect structure */
96 u64 config; /* extra MSR config */
97 u64 reg; /* extra MSR number */
98 atomic_t ref; /* reference count */
99};
100
101/*
102 * Per core/cpu state
103 *
104 * Used to coordinate shared registers between HT threads or
105 * among events on a single PMU.
106 */
107struct intel_shared_regs {
108 struct er_account regs[EXTRA_REG_MAX];
109 int refcnt; /* per-core: #HT threads */
110 unsigned core_id; /* per-core: core id */
111};
112
113#define MAX_LBR_ENTRIES 16
114
115struct cpu_hw_events {
116 /*
117 * Generic x86 PMC bits
118 */
119 struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */
120 unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
121 unsigned long running[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
122 int enabled;
123
124 int n_events;
125 int n_added;
126 int n_txn;
127 int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */
128 u64 tags[X86_PMC_IDX_MAX];
129 struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */
130
131 unsigned int group_flag;
Peter Zijlstra5a4252942012-06-05 15:30:31 +0200132 int is_fake;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300133
134 /*
135 * Intel DebugStore bits
136 */
137 struct debug_store *ds;
138 u64 pebs_enabled;
139
140 /*
141 * Intel LBR bits
142 */
143 int lbr_users;
144 void *lbr_context;
145 struct perf_branch_stack lbr_stack;
146 struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES];
Stephane Eranianb36817e2012-02-09 23:20:53 +0100147 struct er_account *lbr_sel;
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100148 u64 br_sel;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300149
150 /*
Gleb Natapov144d31e2011-10-05 14:01:21 +0200151 * Intel host/guest exclude bits
152 */
153 u64 intel_ctrl_guest_mask;
154 u64 intel_ctrl_host_mask;
155 struct perf_guest_switch_msr guest_switch_msrs[X86_PMC_IDX_MAX];
156
157 /*
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300158 * manage shared (per-core, per-cpu) registers
159 * used on Intel NHM/WSM/SNB
160 */
161 struct intel_shared_regs *shared_regs;
162
163 /*
164 * AMD specific bits
165 */
Joerg Roedel1018faa2012-02-29 14:57:32 +0100166 struct amd_nb *amd_nb;
167 /* Inverted mask of bits to clear in the perf_ctr ctrl registers */
168 u64 perf_ctr_virt_mask;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300169
170 void *kfree_on_online;
171};
172
Robert Richterbc1738f2011-11-18 12:35:22 +0100173#define __EVENT_CONSTRAINT(c, n, m, w, o) {\
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300174 { .idxmsk64 = (n) }, \
175 .code = (c), \
176 .cmask = (m), \
177 .weight = (w), \
Robert Richterbc1738f2011-11-18 12:35:22 +0100178 .overlap = (o), \
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300179}
180
181#define EVENT_CONSTRAINT(c, n, m) \
Robert Richterbc1738f2011-11-18 12:35:22 +0100182 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 0)
183
184/*
185 * The overlap flag marks event constraints with overlapping counter
186 * masks. This is the case if the counter mask of such an event is not
187 * a subset of any other counter mask of a constraint with an equal or
188 * higher weight, e.g.:
189 *
190 * c_overlaps = EVENT_CONSTRAINT_OVERLAP(0, 0x09, 0);
191 * c_another1 = EVENT_CONSTRAINT(0, 0x07, 0);
192 * c_another2 = EVENT_CONSTRAINT(0, 0x38, 0);
193 *
194 * The event scheduler may not select the correct counter in the first
195 * cycle because it needs to know which subsequent events will be
196 * scheduled. It may fail to schedule the events then. So we set the
197 * overlap flag for such constraints to give the scheduler a hint which
198 * events to select for counter rescheduling.
199 *
200 * Care must be taken as the rescheduling algorithm is O(n!) which
201 * will increase scheduling cycles for an over-commited system
202 * dramatically. The number of such EVENT_CONSTRAINT_OVERLAP() macros
203 * and its counter masks must be kept at a minimum.
204 */
205#define EVENT_CONSTRAINT_OVERLAP(c, n, m) \
206 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 1)
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300207
208/*
209 * Constraint on the Event code.
210 */
211#define INTEL_EVENT_CONSTRAINT(c, n) \
212 EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT)
213
214/*
215 * Constraint on the Event code + UMask + fixed-mask
216 *
217 * filter mask to validate fixed counter events.
218 * the following filters disqualify for fixed counters:
219 * - inv
220 * - edge
221 * - cnt-mask
222 * The other filters are supported by fixed counters.
223 * The any-thread option is supported starting with v3.
224 */
225#define FIXED_EVENT_CONSTRAINT(c, n) \
226 EVENT_CONSTRAINT(c, (1ULL << (32+n)), X86_RAW_EVENT_MASK)
227
228/*
229 * Constraint on the Event code + UMask
230 */
231#define INTEL_UEVENT_CONSTRAINT(c, n) \
232 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
233
234#define EVENT_CONSTRAINT_END \
235 EVENT_CONSTRAINT(0, 0, 0)
236
237#define for_each_event_constraint(e, c) \
238 for ((e) = (c); (e)->weight; (e)++)
239
240/*
241 * Extra registers for specific events.
242 *
243 * Some events need large masks and require external MSRs.
244 * Those extra MSRs end up being shared for all events on
245 * a PMU and sometimes between PMU of sibling HT threads.
246 * In either case, the kernel needs to handle conflicting
247 * accesses to those extra, shared, regs. The data structure
248 * to manage those registers is stored in cpu_hw_event.
249 */
250struct extra_reg {
251 unsigned int event;
252 unsigned int msr;
253 u64 config_mask;
254 u64 valid_mask;
255 int idx; /* per_xxx->regs[] reg index */
256};
257
258#define EVENT_EXTRA_REG(e, ms, m, vm, i) { \
259 .event = (e), \
260 .msr = (ms), \
261 .config_mask = (m), \
262 .valid_mask = (vm), \
263 .idx = EXTRA_REG_##i \
264 }
265
266#define INTEL_EVENT_EXTRA_REG(event, msr, vm, idx) \
267 EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm, idx)
268
269#define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0, RSP_0)
270
271union perf_capabilities {
272 struct {
273 u64 lbr_format:6;
274 u64 pebs_trap:1;
275 u64 pebs_arch_reg:1;
276 u64 pebs_format:4;
277 u64 smm_freeze:1;
278 };
279 u64 capabilities;
280};
281
Peter Zijlstrac1d6f422011-12-06 14:07:15 +0100282struct x86_pmu_quirk {
283 struct x86_pmu_quirk *next;
284 void (*func)(void);
285};
286
Peter Zijlstraf9b4eeb2012-03-12 12:44:35 +0100287union x86_pmu_config {
288 struct {
289 u64 event:8,
290 umask:8,
291 usr:1,
292 os:1,
293 edge:1,
294 pc:1,
295 interrupt:1,
296 __reserved1:1,
297 en:1,
298 inv:1,
299 cmask:8,
300 event2:4,
301 __reserved2:4,
302 go:1,
303 ho:1;
304 } bits;
305 u64 value;
306};
307
308#define X86_CONFIG(args...) ((union x86_pmu_config){.bits = {args}}).value
309
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300310/*
311 * struct x86_pmu - generic x86 pmu
312 */
313struct x86_pmu {
314 /*
315 * Generic x86 PMC bits
316 */
317 const char *name;
318 int version;
319 int (*handle_irq)(struct pt_regs *);
320 void (*disable_all)(void);
321 void (*enable_all)(int added);
322 void (*enable)(struct perf_event *);
323 void (*disable)(struct perf_event *);
324 int (*hw_config)(struct perf_event *event);
325 int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign);
326 unsigned eventsel;
327 unsigned perfctr;
328 u64 (*event_map)(int);
329 int max_events;
330 int num_counters;
331 int num_counters_fixed;
332 int cntval_bits;
333 u64 cntval_mask;
Gleb Natapovffb871b2011-11-10 14:57:26 +0200334 union {
335 unsigned long events_maskl;
336 unsigned long events_mask[BITS_TO_LONGS(ARCH_PERFMON_EVENTS_COUNT)];
337 };
338 int events_mask_len;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300339 int apic;
340 u64 max_period;
341 struct event_constraint *
342 (*get_event_constraints)(struct cpu_hw_events *cpuc,
343 struct perf_event *event);
344
345 void (*put_event_constraints)(struct cpu_hw_events *cpuc,
346 struct perf_event *event);
347 struct event_constraint *event_constraints;
Peter Zijlstrac1d6f422011-12-06 14:07:15 +0100348 struct x86_pmu_quirk *quirks;
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300349 int perfctr_second_write;
350
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +0100351 /*
352 * sysfs attrs
353 */
354 int attr_rdpmc;
Jiri Olsa641cc932012-03-15 20:09:14 +0100355 struct attribute **format_attrs;
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +0100356
357 /*
358 * CPU Hotplug hooks
359 */
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300360 int (*cpu_prepare)(int cpu);
361 void (*cpu_starting)(int cpu);
362 void (*cpu_dying)(int cpu);
363 void (*cpu_dead)(int cpu);
Stephane Eraniand010b332012-02-09 23:21:00 +0100364 void (*flush_branch_stack)(void);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300365
366 /*
367 * Intel Arch Perfmon v2+
368 */
369 u64 intel_ctrl;
370 union perf_capabilities intel_cap;
371
372 /*
373 * Intel DebugStore bits
374 */
375 int bts, pebs;
376 int bts_active, pebs_active;
377 int pebs_record_size;
378 void (*drain_pebs)(struct pt_regs *regs);
379 struct event_constraint *pebs_constraints;
Peter Zijlstra0780c922012-06-05 10:26:43 +0200380 void (*pebs_aliases)(struct perf_event *event);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300381
382 /*
383 * Intel LBR
384 */
385 unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */
386 int lbr_nr; /* hardware stack size */
Stephane Eranianb36817e2012-02-09 23:20:53 +0100387 u64 lbr_sel_mask; /* LBR_SELECT valid bits */
388 const int *lbr_sel_map; /* lbr_select mappings */
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300389
390 /*
391 * Extra registers for events
392 */
393 struct extra_reg *extra_regs;
394 unsigned int er_flags;
Gleb Natapov144d31e2011-10-05 14:01:21 +0200395
396 /*
397 * Intel host/guest support (KVM)
398 */
399 struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300400};
401
Peter Zijlstrac1d6f422011-12-06 14:07:15 +0100402#define x86_add_quirk(func_) \
403do { \
404 static struct x86_pmu_quirk __quirk __initdata = { \
405 .func = func_, \
406 }; \
407 __quirk.next = x86_pmu.quirks; \
408 x86_pmu.quirks = &__quirk; \
409} while (0)
410
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300411#define ERF_NO_HT_SHARING 1
412#define ERF_HAS_RSP_1 2
413
414extern struct x86_pmu x86_pmu __read_mostly;
415
416DECLARE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
417
418int x86_perf_event_set_period(struct perf_event *event);
419
420/*
421 * Generalized hw caching related hw_event table, filled
422 * in on a per model basis. A value of 0 means
423 * 'not supported', -1 means 'hw_event makes no sense on
424 * this CPU', any other value means the raw hw_event
425 * ID.
426 */
427
428#define C(x) PERF_COUNT_HW_CACHE_##x
429
430extern u64 __read_mostly hw_cache_event_ids
431 [PERF_COUNT_HW_CACHE_MAX]
432 [PERF_COUNT_HW_CACHE_OP_MAX]
433 [PERF_COUNT_HW_CACHE_RESULT_MAX];
434extern u64 __read_mostly hw_cache_extra_regs
435 [PERF_COUNT_HW_CACHE_MAX]
436 [PERF_COUNT_HW_CACHE_OP_MAX]
437 [PERF_COUNT_HW_CACHE_RESULT_MAX];
438
439u64 x86_perf_event_update(struct perf_event *event);
440
441static inline int x86_pmu_addr_offset(int index)
442{
443 int offset;
444
445 /* offset = X86_FEATURE_PERFCTR_CORE ? index << 1 : index */
446 alternative_io(ASM_NOP2,
447 "shll $1, %%eax",
448 X86_FEATURE_PERFCTR_CORE,
449 "=a" (offset),
450 "a" (index));
451
452 return offset;
453}
454
455static inline unsigned int x86_pmu_config_addr(int index)
456{
457 return x86_pmu.eventsel + x86_pmu_addr_offset(index);
458}
459
460static inline unsigned int x86_pmu_event_addr(int index)
461{
462 return x86_pmu.perfctr + x86_pmu_addr_offset(index);
463}
464
465int x86_setup_perfctr(struct perf_event *event);
466
467int x86_pmu_hw_config(struct perf_event *event);
468
469void x86_pmu_disable_all(void);
470
471static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc,
472 u64 enable_mask)
473{
Joerg Roedel1018faa2012-02-29 14:57:32 +0100474 u64 disable_mask = __this_cpu_read(cpu_hw_events.perf_ctr_virt_mask);
475
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300476 if (hwc->extra_reg.reg)
477 wrmsrl(hwc->extra_reg.reg, hwc->extra_reg.config);
Joerg Roedel1018faa2012-02-29 14:57:32 +0100478 wrmsrl(hwc->config_base, (hwc->config | enable_mask) & ~disable_mask);
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300479}
480
481void x86_pmu_enable_all(int added);
482
483int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign);
484
485void x86_pmu_stop(struct perf_event *event, int flags);
486
487static inline void x86_pmu_disable_event(struct perf_event *event)
488{
489 struct hw_perf_event *hwc = &event->hw;
490
491 wrmsrl(hwc->config_base, hwc->config);
492}
493
494void x86_pmu_enable_event(struct perf_event *event);
495
496int x86_pmu_handle_irq(struct pt_regs *regs);
497
498extern struct event_constraint emptyconstraint;
499
500extern struct event_constraint unconstrained;
501
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100502static inline bool kernel_ip(unsigned long ip)
503{
504#ifdef CONFIG_X86_32
505 return ip > PAGE_OFFSET;
506#else
507 return (long)ip < 0;
508#endif
509}
510
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300511#ifdef CONFIG_CPU_SUP_AMD
512
513int amd_pmu_init(void);
514
515#else /* CONFIG_CPU_SUP_AMD */
516
517static inline int amd_pmu_init(void)
518{
519 return 0;
520}
521
522#endif /* CONFIG_CPU_SUP_AMD */
523
524#ifdef CONFIG_CPU_SUP_INTEL
525
526int intel_pmu_save_and_restart(struct perf_event *event);
527
528struct event_constraint *
529x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event);
530
531struct intel_shared_regs *allocate_shared_regs(int cpu);
532
533int intel_pmu_init(void);
534
535void init_debug_store_on_cpu(int cpu);
536
537void fini_debug_store_on_cpu(int cpu);
538
539void release_ds_buffers(void);
540
541void reserve_ds_buffers(void);
542
543extern struct event_constraint bts_constraint;
544
545void intel_pmu_enable_bts(u64 config);
546
547void intel_pmu_disable_bts(void);
548
549int intel_pmu_drain_bts_buffer(void);
550
551extern struct event_constraint intel_core2_pebs_event_constraints[];
552
553extern struct event_constraint intel_atom_pebs_event_constraints[];
554
555extern struct event_constraint intel_nehalem_pebs_event_constraints[];
556
557extern struct event_constraint intel_westmere_pebs_event_constraints[];
558
559extern struct event_constraint intel_snb_pebs_event_constraints[];
560
561struct event_constraint *intel_pebs_constraints(struct perf_event *event);
562
563void intel_pmu_pebs_enable(struct perf_event *event);
564
565void intel_pmu_pebs_disable(struct perf_event *event);
566
567void intel_pmu_pebs_enable_all(void);
568
569void intel_pmu_pebs_disable_all(void);
570
571void intel_ds_init(void);
572
573void intel_pmu_lbr_reset(void);
574
575void intel_pmu_lbr_enable(struct perf_event *event);
576
577void intel_pmu_lbr_disable(struct perf_event *event);
578
579void intel_pmu_lbr_enable_all(void);
580
581void intel_pmu_lbr_disable_all(void);
582
583void intel_pmu_lbr_read(void);
584
585void intel_pmu_lbr_init_core(void);
586
587void intel_pmu_lbr_init_nhm(void);
588
589void intel_pmu_lbr_init_atom(void);
590
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100591void intel_pmu_lbr_init_snb(void);
592
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100593int intel_pmu_setup_lbr_filter(struct perf_event *event);
594
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300595int p4_pmu_init(void);
596
597int p6_pmu_init(void);
598
599#else /* CONFIG_CPU_SUP_INTEL */
600
601static inline void reserve_ds_buffers(void)
602{
603}
604
605static inline void release_ds_buffers(void)
606{
607}
608
609static inline int intel_pmu_init(void)
610{
611 return 0;
612}
613
614static inline struct intel_shared_regs *allocate_shared_regs(int cpu)
615{
616 return NULL;
617}
618
619#endif /* CONFIG_CPU_SUP_INTEL */