blob: cb4e43bce98ab46e262f3b235765e5ebd6270ddd [file] [log] [blame]
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001#ifndef _ASM_X86_PERF_EVENT_H
2#define _ASM_X86_PERF_EVENT_H
Thomas Gleixner003a46c2007-10-15 13:57:47 +02003
Ingo Molnareb2b8612008-12-17 09:09:13 +01004/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005 * Performance event hw details:
Ingo Molnareb2b8612008-12-17 09:09:13 +01006 */
7
Robert Richter15c7ad52012-06-20 20:46:33 +02008#define INTEL_PMC_MAX_GENERIC 32
9#define INTEL_PMC_MAX_FIXED 3
10#define INTEL_PMC_IDX_FIXED 32
Ingo Molnareb2b8612008-12-17 09:09:13 +010011
Ingo Molnar862a1a52008-12-17 13:09:20 +010012#define X86_PMC_IDX_MAX 64
13
Ingo Molnar241771e2008-12-03 10:39:53 +010014#define MSR_ARCH_PERFMON_PERFCTR0 0xc1
15#define MSR_ARCH_PERFMON_PERFCTR1 0xc2
Thomas Gleixner003a46c2007-10-15 13:57:47 +020016
Ingo Molnar241771e2008-12-03 10:39:53 +010017#define MSR_ARCH_PERFMON_EVENTSEL0 0x186
18#define MSR_ARCH_PERFMON_EVENTSEL1 0x187
Thomas Gleixner003a46c2007-10-15 13:57:47 +020019
Robert Richtera098f442010-03-30 11:28:21 +020020#define ARCH_PERFMON_EVENTSEL_EVENT 0x000000FFULL
21#define ARCH_PERFMON_EVENTSEL_UMASK 0x0000FF00ULL
22#define ARCH_PERFMON_EVENTSEL_USR (1ULL << 16)
23#define ARCH_PERFMON_EVENTSEL_OS (1ULL << 17)
24#define ARCH_PERFMON_EVENTSEL_EDGE (1ULL << 18)
Gleb Natapova7b9d2c2012-02-26 16:55:40 +020025#define ARCH_PERFMON_EVENTSEL_PIN_CONTROL (1ULL << 19)
Robert Richtera098f442010-03-30 11:28:21 +020026#define ARCH_PERFMON_EVENTSEL_INT (1ULL << 20)
27#define ARCH_PERFMON_EVENTSEL_ANY (1ULL << 21)
28#define ARCH_PERFMON_EVENTSEL_ENABLE (1ULL << 22)
29#define ARCH_PERFMON_EVENTSEL_INV (1ULL << 23)
30#define ARCH_PERFMON_EVENTSEL_CMASK 0xFF000000ULL
Thomas Gleixner003a46c2007-10-15 13:57:47 +020031
Joerg Roedel011af852011-10-05 14:01:17 +020032#define AMD_PERFMON_EVENTSEL_GUESTONLY (1ULL << 40)
33#define AMD_PERFMON_EVENTSEL_HOSTONLY (1ULL << 41)
34
Robert Richtera098f442010-03-30 11:28:21 +020035#define AMD64_EVENTSEL_EVENT \
36 (ARCH_PERFMON_EVENTSEL_EVENT | (0x0FULL << 32))
37#define INTEL_ARCH_EVENT_MASK \
38 (ARCH_PERFMON_EVENTSEL_UMASK | ARCH_PERFMON_EVENTSEL_EVENT)
Stephane Eranian1da53e02010-01-18 10:58:01 +020039
Robert Richtera098f442010-03-30 11:28:21 +020040#define X86_RAW_EVENT_MASK \
41 (ARCH_PERFMON_EVENTSEL_EVENT | \
42 ARCH_PERFMON_EVENTSEL_UMASK | \
43 ARCH_PERFMON_EVENTSEL_EDGE | \
44 ARCH_PERFMON_EVENTSEL_INV | \
45 ARCH_PERFMON_EVENTSEL_CMASK)
46#define AMD64_RAW_EVENT_MASK \
47 (X86_RAW_EVENT_MASK | \
48 AMD64_EVENTSEL_EVENT)
Robert Richteree5789d2011-09-21 11:30:17 +020049#define AMD64_NUM_COUNTERS 4
Robert Richterb1dc3c42012-06-20 20:46:35 +020050#define AMD64_NUM_COUNTERS_CORE 6
Stephane Eranian04a705df2009-10-06 16:42:08 +020051
Robert Richteree5789d2011-09-21 11:30:17 +020052#define ARCH_PERFMON_UNHALTED_CORE_CYCLES_SEL 0x3c
Ingo Molnar241771e2008-12-03 10:39:53 +010053#define ARCH_PERFMON_UNHALTED_CORE_CYCLES_UMASK (0x00 << 8)
Robert Richteree5789d2011-09-21 11:30:17 +020054#define ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX 0
Thomas Gleixner003a46c2007-10-15 13:57:47 +020055#define ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT \
Ingo Molnar241771e2008-12-03 10:39:53 +010056 (1 << (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX))
57
Robert Richteree5789d2011-09-21 11:30:17 +020058#define ARCH_PERFMON_BRANCH_MISSES_RETIRED 6
Gleb Natapovffb871b2011-11-10 14:57:26 +020059#define ARCH_PERFMON_EVENTS_COUNT 7
Thomas Gleixner003a46c2007-10-15 13:57:47 +020060
Ingo Molnareb2b8612008-12-17 09:09:13 +010061/*
62 * Intel "Architectural Performance Monitoring" CPUID
63 * detection/enumeration details:
64 */
Thomas Gleixner003a46c2007-10-15 13:57:47 +020065union cpuid10_eax {
66 struct {
67 unsigned int version_id:8;
Robert Richter948b1bb2010-03-29 18:36:50 +020068 unsigned int num_counters:8;
Thomas Gleixner003a46c2007-10-15 13:57:47 +020069 unsigned int bit_width:8;
70 unsigned int mask_length:8;
71 } split;
72 unsigned int full;
73};
74
Gleb Natapovffb871b2011-11-10 14:57:26 +020075union cpuid10_ebx {
76 struct {
77 unsigned int no_unhalted_core_cycles:1;
78 unsigned int no_instructions_retired:1;
79 unsigned int no_unhalted_reference_cycles:1;
80 unsigned int no_llc_reference:1;
81 unsigned int no_llc_misses:1;
82 unsigned int no_branch_instruction_retired:1;
83 unsigned int no_branch_misses_retired:1;
84 } split;
85 unsigned int full;
86};
87
Ingo Molnar703e9372008-12-17 10:51:15 +010088union cpuid10_edx {
89 struct {
Livio Soarese768aee2010-06-03 15:00:31 -040090 unsigned int num_counters_fixed:5;
91 unsigned int bit_width_fixed:8;
92 unsigned int reserved:19;
Ingo Molnar703e9372008-12-17 10:51:15 +010093 } split;
94 unsigned int full;
95};
96
Gleb Natapovb3d94682011-11-10 14:57:27 +020097struct x86_pmu_capability {
98 int version;
99 int num_counters_gp;
100 int num_counters_fixed;
101 int bit_width_gp;
102 int bit_width_fixed;
103 unsigned int events_mask;
104 int events_mask_len;
105};
Ingo Molnar703e9372008-12-17 10:51:15 +0100106
107/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200108 * Fixed-purpose performance events:
Ingo Molnar703e9372008-12-17 10:51:15 +0100109 */
110
Ingo Molnar862a1a52008-12-17 13:09:20 +0100111/*
112 * All 3 fixed-mode PMCs are configured via this single MSR:
113 */
Stephane Eraniancd09c0c2011-12-11 00:28:51 +0100114#define MSR_ARCH_PERFMON_FIXED_CTR_CTRL 0x38d
Ingo Molnar862a1a52008-12-17 13:09:20 +0100115
116/*
117 * The counts are available in three separate MSRs:
118 */
119
Ingo Molnar703e9372008-12-17 10:51:15 +0100120/* Instr_Retired.Any: */
Stephane Eraniancd09c0c2011-12-11 00:28:51 +0100121#define MSR_ARCH_PERFMON_FIXED_CTR0 0x309
Robert Richter15c7ad52012-06-20 20:46:33 +0200122#define INTEL_PMC_IDX_FIXED_INSTRUCTIONS (INTEL_PMC_IDX_FIXED + 0)
Ingo Molnar703e9372008-12-17 10:51:15 +0100123
124/* CPU_CLK_Unhalted.Core: */
Stephane Eraniancd09c0c2011-12-11 00:28:51 +0100125#define MSR_ARCH_PERFMON_FIXED_CTR1 0x30a
Robert Richter15c7ad52012-06-20 20:46:33 +0200126#define INTEL_PMC_IDX_FIXED_CPU_CYCLES (INTEL_PMC_IDX_FIXED + 1)
Ingo Molnar703e9372008-12-17 10:51:15 +0100127
128/* CPU_CLK_Unhalted.Ref: */
Stephane Eraniancd09c0c2011-12-11 00:28:51 +0100129#define MSR_ARCH_PERFMON_FIXED_CTR2 0x30b
Robert Richter15c7ad52012-06-20 20:46:33 +0200130#define INTEL_PMC_IDX_FIXED_REF_CYCLES (INTEL_PMC_IDX_FIXED + 2)
131#define INTEL_PMC_MSK_FIXED_REF_CYCLES (1ULL << INTEL_PMC_IDX_FIXED_REF_CYCLES)
Ingo Molnar703e9372008-12-17 10:51:15 +0100132
Markus Metzger30dd5682009-07-21 15:56:48 +0200133/*
134 * We model BTS tracing as another fixed-mode PMC.
135 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200136 * We choose a value in the middle of the fixed event range, since lower
137 * values are used by actual fixed events and higher values are used
Markus Metzger30dd5682009-07-21 15:56:48 +0200138 * to indicate other overflow conditions in the PERF_GLOBAL_STATUS msr.
139 */
Robert Richter15c7ad52012-06-20 20:46:33 +0200140#define INTEL_PMC_IDX_FIXED_BTS (INTEL_PMC_IDX_FIXED + 16)
Markus Metzger30dd5682009-07-21 15:56:48 +0200141
Robert Richteree5789d2011-09-21 11:30:17 +0200142/*
143 * IBS cpuid feature detection
144 */
145
146#define IBS_CPUID_FEATURES 0x8000001b
147
148/*
149 * Same bit mask as for IBS cpuid feature flags (Fn8000_001B_EAX), but
150 * bit 0 is used to indicate the existence of IBS.
151 */
152#define IBS_CAPS_AVAIL (1U<<0)
153#define IBS_CAPS_FETCHSAM (1U<<1)
154#define IBS_CAPS_OPSAM (1U<<2)
155#define IBS_CAPS_RDWROPCNT (1U<<3)
156#define IBS_CAPS_OPCNT (1U<<4)
157#define IBS_CAPS_BRNTRGT (1U<<5)
158#define IBS_CAPS_OPCNTEXT (1U<<6)
Robert Richterd47e8232012-04-02 20:19:11 +0200159#define IBS_CAPS_RIPINVALIDCHK (1U<<7)
Robert Richteree5789d2011-09-21 11:30:17 +0200160
161#define IBS_CAPS_DEFAULT (IBS_CAPS_AVAIL \
162 | IBS_CAPS_FETCHSAM \
163 | IBS_CAPS_OPSAM)
164
165/*
166 * IBS APIC setup
167 */
168#define IBSCTL 0x1cc
169#define IBSCTL_LVT_OFFSET_VALID (1ULL<<8)
170#define IBSCTL_LVT_OFFSET_MASK 0x0F
171
Robert Richterd47e8232012-04-02 20:19:11 +0200172/* ibs fetch bits/masks */
Robert Richterb47fad32010-09-22 17:45:39 +0200173#define IBS_FETCH_RAND_EN (1ULL<<57)
174#define IBS_FETCH_VAL (1ULL<<49)
175#define IBS_FETCH_ENABLE (1ULL<<48)
176#define IBS_FETCH_CNT 0xFFFF0000ULL
177#define IBS_FETCH_MAX_CNT 0x0000FFFFULL
Robert Richter1d6040f2010-02-25 19:40:46 +0100178
Robert Richterd47e8232012-04-02 20:19:11 +0200179/* ibs op bits/masks */
Robert Richterdb98c5f2011-12-15 17:56:39 +0100180/* lower 4 bits of the current count are ignored: */
181#define IBS_OP_CUR_CNT (0xFFFF0ULL<<32)
Robert Richterb47fad32010-09-22 17:45:39 +0200182#define IBS_OP_CNT_CTL (1ULL<<19)
183#define IBS_OP_VAL (1ULL<<18)
184#define IBS_OP_ENABLE (1ULL<<17)
185#define IBS_OP_MAX_CNT 0x0000FFFFULL
186#define IBS_OP_MAX_CNT_EXT 0x007FFFFFULL /* not a register bit mask */
Robert Richterd47e8232012-04-02 20:19:11 +0200187#define IBS_RIP_INVALID (1ULL<<38)
Markus Metzger30dd5682009-07-21 15:56:48 +0200188
Robert Richter978da302012-05-11 11:44:59 +0200189#ifdef CONFIG_X86_LOCAL_APIC
Robert Richterb7169162011-09-21 11:30:18 +0200190extern u32 get_ibs_caps(void);
Robert Richter978da302012-05-11 11:44:59 +0200191#else
192static inline u32 get_ibs_caps(void) { return 0; }
193#endif
Robert Richterb7169162011-09-21 11:30:18 +0200194
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200195#ifdef CONFIG_PERF_EVENTS
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200196extern void perf_events_lapic_init(void);
Peter Zijlstra194002b2009-06-22 16:35:24 +0200197
Peter Zijlstraef21f682010-03-03 13:12:23 +0100198/*
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +0200199 * Abuse bits {3,5} of the cpu eflags register. These flags are otherwise
200 * unused and ABI specified to be 0, so nobody should care what we do with
201 * them.
202 *
203 * EXACT - the IP points to the exact instruction that triggered the
204 * event (HW bugs exempt).
205 * VM - original X86_VM_MASK; see set_linear_ip().
Peter Zijlstraef21f682010-03-03 13:12:23 +0100206 */
207#define PERF_EFLAGS_EXACT (1UL << 3)
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +0200208#define PERF_EFLAGS_VM (1UL << 5)
Peter Zijlstraef21f682010-03-03 13:12:23 +0100209
Zhang, Yanmin39447b32010-04-19 13:32:41 +0800210struct pt_regs;
211extern unsigned long perf_instruction_pointer(struct pt_regs *regs);
212extern unsigned long perf_misc_flags(struct pt_regs *regs);
213#define perf_misc_flags(regs) perf_misc_flags(regs)
Peter Zijlstraef21f682010-03-03 13:12:23 +0100214
Frederic Weisbeckerb0f82b82010-05-20 07:47:21 +0200215#include <asm/stacktrace.h>
216
217/*
218 * We abuse bit 3 from flags to pass exact information, see perf_misc_flags
219 * and the comment with PERF_EFLAGS_EXACT.
220 */
221#define perf_arch_fetch_caller_regs(regs, __ip) { \
222 (regs)->ip = (__ip); \
223 (regs)->bp = caller_frame_pointer(); \
224 (regs)->cs = __KERNEL_CS; \
225 regs->flags = 0; \
Frederic Weisbecker9e462942011-07-02 15:00:52 +0200226 asm volatile( \
227 _ASM_MOV "%%"_ASM_SP ", %0\n" \
228 : "=m" ((regs)->sp) \
229 :: "memory" \
230 ); \
Frederic Weisbeckerb0f82b82010-05-20 07:47:21 +0200231}
232
Gleb Natapov144d31e2011-10-05 14:01:21 +0200233struct perf_guest_switch_msr {
234 unsigned msr;
235 u64 host, guest;
236};
237
238extern struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr);
Gleb Natapovb3d94682011-11-10 14:57:27 +0200239extern void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap);
Peter Zijlstrac93dc842012-06-08 14:50:50 +0200240extern void perf_check_microcode(void);
Ingo Molnar241771e2008-12-03 10:39:53 +0100241#else
Jovi Zhang35d56ca92012-07-17 10:14:41 +0800242static inline struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr)
Gleb Natapov144d31e2011-10-05 14:01:21 +0200243{
244 *nr = 0;
245 return NULL;
246}
247
Gleb Natapovb3d94682011-11-10 14:57:27 +0200248static inline void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)
249{
250 memset(cap, 0, sizeof(*cap));
251}
252
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200253static inline void perf_events_lapic_init(void) { }
Peter Zijlstrac93dc842012-06-08 14:50:50 +0200254static inline void perf_check_microcode(void) { }
Ingo Molnar241771e2008-12-03 10:39:53 +0100255#endif
256
Joerg Roedel1018faa2012-02-29 14:57:32 +0100257#if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD)
258 extern void amd_pmu_enable_virt(void);
259 extern void amd_pmu_disable_virt(void);
260#else
261 static inline void amd_pmu_enable_virt(void) { }
262 static inline void amd_pmu_disable_virt(void) { }
263#endif
264
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200265#endif /* _ASM_X86_PERF_EVENT_H */