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