2 * Performance events x86 architecture header
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
12 * For licencing details see kernel-base/COPYING
15 #include <linux/perf_event.h>
19 #define wrmsrl(msr, val) \
21 unsigned int _msr = (msr); \
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)); \
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 *-----------------------------------------
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.
44 EXTRA_REG_NONE
= -1, /* not used */
46 EXTRA_REG_RSP_0
= 0, /* offcore_response_0 */
47 EXTRA_REG_RSP_1
= 1, /* offcore_response_1 */
48 EXTRA_REG_LBR
= 2, /* lbr_select */
49 EXTRA_REG_LDLAT
= 3, /* ld_lat_threshold */
51 EXTRA_REG_MAX
/* number of entries needed */
54 struct event_constraint
{
56 unsigned long idxmsk
[BITS_TO_LONGS(X86_PMC_IDX_MAX
)];
66 * struct hw_perf_event.flags flags
68 #define PERF_X86_EVENT_PEBS_LDLAT 0x1 /* ld+ldlat data address sampling */
69 #define PERF_X86_EVENT_PEBS_ST 0x2 /* st data address sampling */
70 #define PERF_X86_EVENT_PEBS_ST_HSW 0x4 /* haswell style st data sampling */
71 #define PERF_X86_EVENT_COMMITTED 0x8 /* event passed commit_txn */
74 int nb_id
; /* NorthBridge id */
75 int refcnt
; /* reference count */
76 struct perf_event
*owners
[X86_PMC_IDX_MAX
];
77 struct event_constraint event_constraints
[X86_PMC_IDX_MAX
];
80 /* The maximal number of PEBS events: */
81 #define MAX_PEBS_EVENTS 8
84 * A debug store configuration.
86 * We only support architectures that use 64bit fields.
91 u64 bts_absolute_maximum
;
92 u64 bts_interrupt_threshold
;
95 u64 pebs_absolute_maximum
;
96 u64 pebs_interrupt_threshold
;
97 u64 pebs_event_reset
[MAX_PEBS_EVENTS
];
101 * Per register state.
104 raw_spinlock_t lock
; /* per-core: protect structure */
105 u64 config
; /* extra MSR config */
106 u64 reg
; /* extra MSR number */
107 atomic_t ref
; /* reference count */
113 * Used to coordinate shared registers between HT threads or
114 * among events on a single PMU.
116 struct intel_shared_regs
{
117 struct er_account regs
[EXTRA_REG_MAX
];
118 int refcnt
; /* per-core: #HT threads */
119 unsigned core_id
; /* per-core: core id */
122 #define MAX_LBR_ENTRIES 16
124 struct cpu_hw_events
{
126 * Generic x86 PMC bits
128 struct perf_event
*events
[X86_PMC_IDX_MAX
]; /* in counter order */
129 unsigned long active_mask
[BITS_TO_LONGS(X86_PMC_IDX_MAX
)];
130 unsigned long running
[BITS_TO_LONGS(X86_PMC_IDX_MAX
)];
136 int assign
[X86_PMC_IDX_MAX
]; /* event to counter assignment */
137 u64 tags
[X86_PMC_IDX_MAX
];
138 struct perf_event
*event_list
[X86_PMC_IDX_MAX
]; /* in enabled order */
140 unsigned int group_flag
;
144 * Intel DebugStore bits
146 struct debug_store
*ds
;
154 struct perf_branch_stack lbr_stack
;
155 struct perf_branch_entry lbr_entries
[MAX_LBR_ENTRIES
];
156 struct er_account
*lbr_sel
;
160 * Intel host/guest exclude bits
162 u64 intel_ctrl_guest_mask
;
163 u64 intel_ctrl_host_mask
;
164 struct perf_guest_switch_msr guest_switch_msrs
[X86_PMC_IDX_MAX
];
167 * manage shared (per-core, per-cpu) registers
168 * used on Intel NHM/WSM/SNB
170 struct intel_shared_regs
*shared_regs
;
175 struct amd_nb
*amd_nb
;
176 /* Inverted mask of bits to clear in the perf_ctr ctrl registers */
177 u64 perf_ctr_virt_mask
;
179 void *kfree_on_online
;
182 #define __EVENT_CONSTRAINT(c, n, m, w, o, f) {\
183 { .idxmsk64 = (n) }, \
191 #define EVENT_CONSTRAINT(c, n, m) \
192 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 0, 0)
195 * The overlap flag marks event constraints with overlapping counter
196 * masks. This is the case if the counter mask of such an event is not
197 * a subset of any other counter mask of a constraint with an equal or
198 * higher weight, e.g.:
200 * c_overlaps = EVENT_CONSTRAINT_OVERLAP(0, 0x09, 0);
201 * c_another1 = EVENT_CONSTRAINT(0, 0x07, 0);
202 * c_another2 = EVENT_CONSTRAINT(0, 0x38, 0);
204 * The event scheduler may not select the correct counter in the first
205 * cycle because it needs to know which subsequent events will be
206 * scheduled. It may fail to schedule the events then. So we set the
207 * overlap flag for such constraints to give the scheduler a hint which
208 * events to select for counter rescheduling.
210 * Care must be taken as the rescheduling algorithm is O(n!) which
211 * will increase scheduling cycles for an over-commited system
212 * dramatically. The number of such EVENT_CONSTRAINT_OVERLAP() macros
213 * and its counter masks must be kept at a minimum.
215 #define EVENT_CONSTRAINT_OVERLAP(c, n, m) \
216 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 1, 0)
219 * Constraint on the Event code.
221 #define INTEL_EVENT_CONSTRAINT(c, n) \
222 EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT)
225 * Constraint on the Event code + UMask + fixed-mask
227 * filter mask to validate fixed counter events.
228 * the following filters disqualify for fixed counters:
233 * - in_tx_checkpointed
234 * The other filters are supported by fixed counters.
235 * The any-thread option is supported starting with v3.
237 #define FIXED_EVENT_FLAGS (X86_RAW_EVENT_MASK|HSW_IN_TX|HSW_IN_TX_CHECKPOINTED)
238 #define FIXED_EVENT_CONSTRAINT(c, n) \
239 EVENT_CONSTRAINT(c, (1ULL << (32+n)), FIXED_EVENT_FLAGS)
242 * Constraint on the Event code + UMask
244 #define INTEL_UEVENT_CONSTRAINT(c, n) \
245 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
247 #define INTEL_PLD_CONSTRAINT(c, n) \
248 __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK, \
249 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LDLAT)
251 #define INTEL_PST_CONSTRAINT(c, n) \
252 __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK, \
253 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST)
255 /* DataLA version of store sampling without extra enable bit. */
256 #define INTEL_PST_HSW_CONSTRAINT(c, n) \
257 __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK, \
258 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST_HSW)
260 #define EVENT_CONSTRAINT_END \
261 EVENT_CONSTRAINT(0, 0, 0)
263 #define for_each_event_constraint(e, c) \
264 for ((e) = (c); (e)->weight; (e)++)
267 * Extra registers for specific events.
269 * Some events need large masks and require external MSRs.
270 * Those extra MSRs end up being shared for all events on
271 * a PMU and sometimes between PMU of sibling HT threads.
272 * In either case, the kernel needs to handle conflicting
273 * accesses to those extra, shared, regs. The data structure
274 * to manage those registers is stored in cpu_hw_event.
281 int idx
; /* per_xxx->regs[] reg index */
284 #define EVENT_EXTRA_REG(e, ms, m, vm, i) { \
287 .config_mask = (m), \
288 .valid_mask = (vm), \
289 .idx = EXTRA_REG_##i, \
292 #define INTEL_EVENT_EXTRA_REG(event, msr, vm, idx) \
293 EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm, idx)
295 #define INTEL_UEVENT_EXTRA_REG(event, msr, vm, idx) \
296 EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT | \
297 ARCH_PERFMON_EVENTSEL_UMASK, vm, idx)
299 #define INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(c) \
300 INTEL_UEVENT_EXTRA_REG(c, \
301 MSR_PEBS_LD_LAT_THRESHOLD, \
305 #define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0, RSP_0)
307 union perf_capabilities
{
315 * PMU supports separate counter range for writing
318 u64 full_width_write
:1;
323 struct x86_pmu_quirk
{
324 struct x86_pmu_quirk
*next
;
328 union x86_pmu_config
{
349 #define X86_CONFIG(args...) ((union x86_pmu_config){.bits = {args}}).value
352 * struct x86_pmu - generic x86 pmu
356 * Generic x86 PMC bits
360 int (*handle_irq
)(struct pt_regs
*);
361 void (*disable_all
)(void);
362 void (*enable_all
)(int added
);
363 void (*enable
)(struct perf_event
*);
364 void (*disable
)(struct perf_event
*);
365 int (*hw_config
)(struct perf_event
*event
);
366 int (*schedule_events
)(struct cpu_hw_events
*cpuc
, int n
, int *assign
);
369 int (*addr_offset
)(int index
, bool eventsel
);
370 int (*rdpmc_index
)(int index
);
371 u64 (*event_map
)(int);
374 int num_counters_fixed
;
378 unsigned long events_maskl
;
379 unsigned long events_mask
[BITS_TO_LONGS(ARCH_PERFMON_EVENTS_COUNT
)];
384 struct event_constraint
*
385 (*get_event_constraints
)(struct cpu_hw_events
*cpuc
,
386 struct perf_event
*event
);
388 void (*put_event_constraints
)(struct cpu_hw_events
*cpuc
,
389 struct perf_event
*event
);
390 struct event_constraint
*event_constraints
;
391 struct x86_pmu_quirk
*quirks
;
392 int perfctr_second_write
;
399 struct attribute
**format_attrs
;
400 struct attribute
**event_attrs
;
402 ssize_t (*events_sysfs_show
)(char *page
, u64 config
);
403 struct attribute
**cpu_events
;
408 int (*cpu_prepare
)(int cpu
);
409 void (*cpu_starting
)(int cpu
);
410 void (*cpu_dying
)(int cpu
);
411 void (*cpu_dead
)(int cpu
);
413 void (*check_microcode
)(void);
414 void (*flush_branch_stack
)(void);
417 * Intel Arch Perfmon v2+
420 union perf_capabilities intel_cap
;
423 * Intel DebugStore bits
430 int pebs_record_size
;
431 void (*drain_pebs
)(struct pt_regs
*regs
);
432 struct event_constraint
*pebs_constraints
;
433 void (*pebs_aliases
)(struct perf_event
*event
);
439 unsigned long lbr_tos
, lbr_from
, lbr_to
; /* MSR base regs */
440 int lbr_nr
; /* hardware stack size */
441 u64 lbr_sel_mask
; /* LBR_SELECT valid bits */
442 const int *lbr_sel_map
; /* lbr_select mappings */
445 * Extra registers for events
447 struct extra_reg
*extra_regs
;
448 unsigned int er_flags
;
451 * Intel host/guest support (KVM)
453 struct perf_guest_switch_msr
*(*guest_get_msrs
)(int *nr
);
456 #define x86_add_quirk(func_) \
458 static struct x86_pmu_quirk __quirk __initdata = { \
461 __quirk.next = x86_pmu.quirks; \
462 x86_pmu.quirks = &__quirk; \
465 #define ERF_NO_HT_SHARING 1
466 #define ERF_HAS_RSP_1 2
468 #define EVENT_VAR(_id) event_attr_##_id
469 #define EVENT_PTR(_id) &event_attr_##_id.attr.attr
471 #define EVENT_ATTR(_name, _id) \
472 static struct perf_pmu_events_attr EVENT_VAR(_id) = { \
473 .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \
474 .id = PERF_COUNT_HW_##_id, \
478 #define EVENT_ATTR_STR(_name, v, str) \
479 static struct perf_pmu_events_attr event_attr_##v = { \
480 .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \
485 extern struct x86_pmu x86_pmu __read_mostly
;
487 DECLARE_PER_CPU(struct cpu_hw_events
, cpu_hw_events
);
489 int x86_perf_event_set_period(struct perf_event
*event
);
492 * Generalized hw caching related hw_event table, filled
493 * in on a per model basis. A value of 0 means
494 * 'not supported', -1 means 'hw_event makes no sense on
495 * this CPU', any other value means the raw hw_event
499 #define C(x) PERF_COUNT_HW_CACHE_##x
501 extern u64 __read_mostly hw_cache_event_ids
502 [PERF_COUNT_HW_CACHE_MAX
]
503 [PERF_COUNT_HW_CACHE_OP_MAX
]
504 [PERF_COUNT_HW_CACHE_RESULT_MAX
];
505 extern u64 __read_mostly hw_cache_extra_regs
506 [PERF_COUNT_HW_CACHE_MAX
]
507 [PERF_COUNT_HW_CACHE_OP_MAX
]
508 [PERF_COUNT_HW_CACHE_RESULT_MAX
];
510 u64
x86_perf_event_update(struct perf_event
*event
);
512 static inline unsigned int x86_pmu_config_addr(int index
)
514 return x86_pmu
.eventsel
+ (x86_pmu
.addr_offset
?
515 x86_pmu
.addr_offset(index
, true) : index
);
518 static inline unsigned int x86_pmu_event_addr(int index
)
520 return x86_pmu
.perfctr
+ (x86_pmu
.addr_offset
?
521 x86_pmu
.addr_offset(index
, false) : index
);
524 static inline int x86_pmu_rdpmc_index(int index
)
526 return x86_pmu
.rdpmc_index
? x86_pmu
.rdpmc_index(index
) : index
;
529 int x86_setup_perfctr(struct perf_event
*event
);
531 int x86_pmu_hw_config(struct perf_event
*event
);
533 void x86_pmu_disable_all(void);
535 static inline void __x86_pmu_enable_event(struct hw_perf_event
*hwc
,
538 u64 disable_mask
= __this_cpu_read(cpu_hw_events
.perf_ctr_virt_mask
);
540 if (hwc
->extra_reg
.reg
)
541 wrmsrl(hwc
->extra_reg
.reg
, hwc
->extra_reg
.config
);
542 wrmsrl(hwc
->config_base
, (hwc
->config
| enable_mask
) & ~disable_mask
);
545 void x86_pmu_enable_all(int added
);
547 int perf_assign_events(struct perf_event
**events
, int n
,
548 int wmin
, int wmax
, int *assign
);
549 int x86_schedule_events(struct cpu_hw_events
*cpuc
, int n
, int *assign
);
551 void x86_pmu_stop(struct perf_event
*event
, int flags
);
553 static inline void x86_pmu_disable_event(struct perf_event
*event
)
555 struct hw_perf_event
*hwc
= &event
->hw
;
557 wrmsrl(hwc
->config_base
, hwc
->config
);
560 void x86_pmu_enable_event(struct perf_event
*event
);
562 int x86_pmu_handle_irq(struct pt_regs
*regs
);
564 extern struct event_constraint emptyconstraint
;
566 extern struct event_constraint unconstrained
;
568 static inline bool kernel_ip(unsigned long ip
)
571 return ip
> PAGE_OFFSET
;
578 * Not all PMUs provide the right context information to place the reported IP
579 * into full context. Specifically segment registers are typically not
582 * Assuming the address is a linear address (it is for IBS), we fake the CS and
583 * vm86 mode using the known zero-based code segment and 'fix up' the registers
586 * Intel PEBS/LBR appear to typically provide the effective address, nothing
587 * much we can do about that but pray and treat it like a linear address.
589 static inline void set_linear_ip(struct pt_regs
*regs
, unsigned long ip
)
591 regs
->cs
= kernel_ip(ip
) ? __KERNEL_CS
: __USER_CS
;
592 if (regs
->flags
& X86_VM_MASK
)
593 regs
->flags
^= (PERF_EFLAGS_VM
| X86_VM_MASK
);
597 ssize_t
x86_event_sysfs_show(char *page
, u64 config
, u64 event
);
598 ssize_t
intel_event_sysfs_show(char *page
, u64 config
);
600 #ifdef CONFIG_CPU_SUP_AMD
602 int amd_pmu_init(void);
604 #else /* CONFIG_CPU_SUP_AMD */
606 static inline int amd_pmu_init(void)
611 #endif /* CONFIG_CPU_SUP_AMD */
613 #ifdef CONFIG_CPU_SUP_INTEL
615 int intel_pmu_save_and_restart(struct perf_event
*event
);
617 struct event_constraint
*
618 x86_get_event_constraints(struct cpu_hw_events
*cpuc
, struct perf_event
*event
);
620 struct intel_shared_regs
*allocate_shared_regs(int cpu
);
622 int intel_pmu_init(void);
624 void init_debug_store_on_cpu(int cpu
);
626 void fini_debug_store_on_cpu(int cpu
);
628 void release_ds_buffers(void);
630 void reserve_ds_buffers(void);
632 extern struct event_constraint bts_constraint
;
634 void intel_pmu_enable_bts(u64 config
);
636 void intel_pmu_disable_bts(void);
638 int intel_pmu_drain_bts_buffer(void);
640 extern struct event_constraint intel_core2_pebs_event_constraints
[];
642 extern struct event_constraint intel_atom_pebs_event_constraints
[];
644 extern struct event_constraint intel_slm_pebs_event_constraints
[];
646 extern struct event_constraint intel_nehalem_pebs_event_constraints
[];
648 extern struct event_constraint intel_westmere_pebs_event_constraints
[];
650 extern struct event_constraint intel_snb_pebs_event_constraints
[];
652 extern struct event_constraint intel_ivb_pebs_event_constraints
[];
654 extern struct event_constraint intel_hsw_pebs_event_constraints
[];
656 struct event_constraint
*intel_pebs_constraints(struct perf_event
*event
);
658 void intel_pmu_pebs_enable(struct perf_event
*event
);
660 void intel_pmu_pebs_disable(struct perf_event
*event
);
662 void intel_pmu_pebs_enable_all(void);
664 void intel_pmu_pebs_disable_all(void);
666 void intel_ds_init(void);
668 void intel_pmu_lbr_reset(void);
670 void intel_pmu_lbr_enable(struct perf_event
*event
);
672 void intel_pmu_lbr_disable(struct perf_event
*event
);
674 void intel_pmu_lbr_enable_all(void);
676 void intel_pmu_lbr_disable_all(void);
678 void intel_pmu_lbr_read(void);
680 void intel_pmu_lbr_init_core(void);
682 void intel_pmu_lbr_init_nhm(void);
684 void intel_pmu_lbr_init_atom(void);
686 void intel_pmu_lbr_init_snb(void);
688 int intel_pmu_setup_lbr_filter(struct perf_event
*event
);
690 int p4_pmu_init(void);
692 int p6_pmu_init(void);
694 int knc_pmu_init(void);
696 ssize_t
events_sysfs_show(struct device
*dev
, struct device_attribute
*attr
,
699 #else /* CONFIG_CPU_SUP_INTEL */
701 static inline void reserve_ds_buffers(void)
705 static inline void release_ds_buffers(void)
709 static inline int intel_pmu_init(void)
714 static inline struct intel_shared_regs
*allocate_shared_regs(int cpu
)
719 #endif /* CONFIG_CPU_SUP_INTEL */