Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 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 | |
Peter Zijlstra | 1c2ac3f | 2012-05-14 15:25:34 +0200 | [diff] [blame] | 17 | #if 0 |
| 18 | #undef wrmsrl |
| 19 | #define wrmsrl(msr, val) \ |
| 20 | do { \ |
| 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 Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 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 | */ |
| 43 | enum 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 Eranian | b36817e | 2012-02-09 23:20:53 +0100 | [diff] [blame] | 48 | EXTRA_REG_LBR = 2, /* lbr_select */ |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 49 | EXTRA_REG_LDLAT = 3, /* ld_lat_threshold */ |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 50 | |
| 51 | EXTRA_REG_MAX /* number of entries needed */ |
| 52 | }; |
| 53 | |
| 54 | struct 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; |
Robert Richter | bc1738f | 2011-11-18 12:35:22 +0100 | [diff] [blame] | 62 | int overlap; |
Stephane Eranian | 9fac2cf | 2013-01-24 16:10:27 +0100 | [diff] [blame] | 63 | int flags; |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 64 | }; |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 65 | /* |
Stephane Eranian | 2f7f73a | 2013-06-20 18:42:54 +0200 | [diff] [blame] | 66 | * struct hw_perf_event.flags flags |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 67 | */ |
| 68 | #define PERF_X86_EVENT_PEBS_LDLAT 0x1 /* ld+ldlat data address sampling */ |
Stephane Eranian | 9ad64c0 | 2013-01-24 16:10:34 +0100 | [diff] [blame] | 69 | #define PERF_X86_EVENT_PEBS_ST 0x2 /* st data address sampling */ |
Andi Kleen | f9134f3 | 2013-06-17 17:36:52 -0700 | [diff] [blame] | 70 | #define PERF_X86_EVENT_PEBS_ST_HSW 0x4 /* haswell style st data sampling */ |
Stephane Eranian | 2f7f73a | 2013-06-20 18:42:54 +0200 | [diff] [blame] | 71 | #define PERF_X86_EVENT_COMMITTED 0x8 /* event passed commit_txn */ |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 72 | |
| 73 | struct amd_nb { |
| 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]; |
| 78 | }; |
| 79 | |
| 80 | /* The maximal number of PEBS events: */ |
Andi Kleen | 70ab700 | 2012-06-05 17:56:48 -0700 | [diff] [blame] | 81 | #define MAX_PEBS_EVENTS 8 |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 82 | |
| 83 | /* |
| 84 | * A debug store configuration. |
| 85 | * |
| 86 | * We only support architectures that use 64bit fields. |
| 87 | */ |
| 88 | struct debug_store { |
| 89 | u64 bts_buffer_base; |
| 90 | u64 bts_index; |
| 91 | u64 bts_absolute_maximum; |
| 92 | u64 bts_interrupt_threshold; |
| 93 | u64 pebs_buffer_base; |
| 94 | u64 pebs_index; |
| 95 | u64 pebs_absolute_maximum; |
| 96 | u64 pebs_interrupt_threshold; |
| 97 | u64 pebs_event_reset[MAX_PEBS_EVENTS]; |
| 98 | }; |
| 99 | |
| 100 | /* |
| 101 | * Per register state. |
| 102 | */ |
| 103 | struct er_account { |
| 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 */ |
| 108 | }; |
| 109 | |
| 110 | /* |
| 111 | * Per core/cpu state |
| 112 | * |
| 113 | * Used to coordinate shared registers between HT threads or |
| 114 | * among events on a single PMU. |
| 115 | */ |
| 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 */ |
| 120 | }; |
| 121 | |
| 122 | #define MAX_LBR_ENTRIES 16 |
| 123 | |
| 124 | struct cpu_hw_events { |
| 125 | /* |
| 126 | * Generic x86 PMC bits |
| 127 | */ |
| 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)]; |
| 131 | int enabled; |
| 132 | |
Peter Zijlstra | c347a2f | 2014-02-24 12:26:21 +0100 | [diff] [blame] | 133 | int n_events; /* the # of events in the below arrays */ |
| 134 | int n_added; /* the # last events in the below arrays; |
| 135 | they've never been enabled yet */ |
| 136 | int n_txn; /* the # last events in the below arrays; |
| 137 | added in the current transaction */ |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 138 | int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */ |
| 139 | u64 tags[X86_PMC_IDX_MAX]; |
| 140 | struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */ |
| 141 | |
| 142 | unsigned int group_flag; |
Peter Zijlstra | 5a425294 | 2012-06-05 15:30:31 +0200 | [diff] [blame] | 143 | int is_fake; |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | * Intel DebugStore bits |
| 147 | */ |
| 148 | struct debug_store *ds; |
| 149 | u64 pebs_enabled; |
| 150 | |
| 151 | /* |
| 152 | * Intel LBR bits |
| 153 | */ |
| 154 | int lbr_users; |
| 155 | void *lbr_context; |
| 156 | struct perf_branch_stack lbr_stack; |
| 157 | struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES]; |
Stephane Eranian | b36817e | 2012-02-09 23:20:53 +0100 | [diff] [blame] | 158 | struct er_account *lbr_sel; |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 159 | u64 br_sel; |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 160 | |
| 161 | /* |
Gleb Natapov | 144d31e | 2011-10-05 14:01:21 +0200 | [diff] [blame] | 162 | * Intel host/guest exclude bits |
| 163 | */ |
| 164 | u64 intel_ctrl_guest_mask; |
| 165 | u64 intel_ctrl_host_mask; |
| 166 | struct perf_guest_switch_msr guest_switch_msrs[X86_PMC_IDX_MAX]; |
| 167 | |
| 168 | /* |
Peter Zijlstra | 2b9e344 | 2013-09-12 12:53:44 +0200 | [diff] [blame] | 169 | * Intel checkpoint mask |
| 170 | */ |
| 171 | u64 intel_cp_status; |
| 172 | |
| 173 | /* |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 174 | * manage shared (per-core, per-cpu) registers |
| 175 | * used on Intel NHM/WSM/SNB |
| 176 | */ |
| 177 | struct intel_shared_regs *shared_regs; |
| 178 | |
| 179 | /* |
| 180 | * AMD specific bits |
| 181 | */ |
Joerg Roedel | 1018faa | 2012-02-29 14:57:32 +0100 | [diff] [blame] | 182 | struct amd_nb *amd_nb; |
| 183 | /* Inverted mask of bits to clear in the perf_ctr ctrl registers */ |
| 184 | u64 perf_ctr_virt_mask; |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 185 | |
| 186 | void *kfree_on_online; |
| 187 | }; |
| 188 | |
Stephane Eranian | 9fac2cf | 2013-01-24 16:10:27 +0100 | [diff] [blame] | 189 | #define __EVENT_CONSTRAINT(c, n, m, w, o, f) {\ |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 190 | { .idxmsk64 = (n) }, \ |
| 191 | .code = (c), \ |
| 192 | .cmask = (m), \ |
| 193 | .weight = (w), \ |
Robert Richter | bc1738f | 2011-11-18 12:35:22 +0100 | [diff] [blame] | 194 | .overlap = (o), \ |
Stephane Eranian | 9fac2cf | 2013-01-24 16:10:27 +0100 | [diff] [blame] | 195 | .flags = f, \ |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | #define EVENT_CONSTRAINT(c, n, m) \ |
Stephane Eranian | 9fac2cf | 2013-01-24 16:10:27 +0100 | [diff] [blame] | 199 | __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 0, 0) |
Robert Richter | bc1738f | 2011-11-18 12:35:22 +0100 | [diff] [blame] | 200 | |
| 201 | /* |
| 202 | * The overlap flag marks event constraints with overlapping counter |
| 203 | * masks. This is the case if the counter mask of such an event is not |
| 204 | * a subset of any other counter mask of a constraint with an equal or |
| 205 | * higher weight, e.g.: |
| 206 | * |
| 207 | * c_overlaps = EVENT_CONSTRAINT_OVERLAP(0, 0x09, 0); |
| 208 | * c_another1 = EVENT_CONSTRAINT(0, 0x07, 0); |
| 209 | * c_another2 = EVENT_CONSTRAINT(0, 0x38, 0); |
| 210 | * |
| 211 | * The event scheduler may not select the correct counter in the first |
| 212 | * cycle because it needs to know which subsequent events will be |
| 213 | * scheduled. It may fail to schedule the events then. So we set the |
| 214 | * overlap flag for such constraints to give the scheduler a hint which |
| 215 | * events to select for counter rescheduling. |
| 216 | * |
| 217 | * Care must be taken as the rescheduling algorithm is O(n!) which |
| 218 | * will increase scheduling cycles for an over-commited system |
| 219 | * dramatically. The number of such EVENT_CONSTRAINT_OVERLAP() macros |
| 220 | * and its counter masks must be kept at a minimum. |
| 221 | */ |
| 222 | #define EVENT_CONSTRAINT_OVERLAP(c, n, m) \ |
Stephane Eranian | 9fac2cf | 2013-01-24 16:10:27 +0100 | [diff] [blame] | 223 | __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 1, 0) |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 224 | |
| 225 | /* |
| 226 | * Constraint on the Event code. |
| 227 | */ |
| 228 | #define INTEL_EVENT_CONSTRAINT(c, n) \ |
| 229 | EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT) |
| 230 | |
| 231 | /* |
| 232 | * Constraint on the Event code + UMask + fixed-mask |
| 233 | * |
| 234 | * filter mask to validate fixed counter events. |
| 235 | * the following filters disqualify for fixed counters: |
| 236 | * - inv |
| 237 | * - edge |
| 238 | * - cnt-mask |
Andi Kleen | 3a632cb | 2013-06-17 17:36:48 -0700 | [diff] [blame] | 239 | * - in_tx |
| 240 | * - in_tx_checkpointed |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 241 | * The other filters are supported by fixed counters. |
| 242 | * The any-thread option is supported starting with v3. |
| 243 | */ |
Andi Kleen | 3a632cb | 2013-06-17 17:36:48 -0700 | [diff] [blame] | 244 | #define FIXED_EVENT_FLAGS (X86_RAW_EVENT_MASK|HSW_IN_TX|HSW_IN_TX_CHECKPOINTED) |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 245 | #define FIXED_EVENT_CONSTRAINT(c, n) \ |
Andi Kleen | 3a632cb | 2013-06-17 17:36:48 -0700 | [diff] [blame] | 246 | EVENT_CONSTRAINT(c, (1ULL << (32+n)), FIXED_EVENT_FLAGS) |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 247 | |
| 248 | /* |
| 249 | * Constraint on the Event code + UMask |
| 250 | */ |
| 251 | #define INTEL_UEVENT_CONSTRAINT(c, n) \ |
| 252 | EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK) |
| 253 | |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 254 | #define INTEL_PLD_CONSTRAINT(c, n) \ |
| 255 | __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK, \ |
| 256 | HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LDLAT) |
| 257 | |
Stephane Eranian | 9ad64c0 | 2013-01-24 16:10:34 +0100 | [diff] [blame] | 258 | #define INTEL_PST_CONSTRAINT(c, n) \ |
| 259 | __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK, \ |
| 260 | HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST) |
| 261 | |
Andi Kleen | f9134f3 | 2013-06-17 17:36:52 -0700 | [diff] [blame] | 262 | /* DataLA version of store sampling without extra enable bit. */ |
| 263 | #define INTEL_PST_HSW_CONSTRAINT(c, n) \ |
| 264 | __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK, \ |
| 265 | HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST_HSW) |
| 266 | |
Maria Dimakopoulou | cf30d52 | 2013-12-05 01:24:37 +0200 | [diff] [blame] | 267 | /* |
| 268 | * We define the end marker as having a weight of -1 |
| 269 | * to enable blacklisting of events using a counter bitmask |
| 270 | * of zero and thus a weight of zero. |
| 271 | * The end marker has a weight that cannot possibly be |
| 272 | * obtained from counting the bits in the bitmask. |
| 273 | */ |
| 274 | #define EVENT_CONSTRAINT_END { .weight = -1 } |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 275 | |
Maria Dimakopoulou | cf30d52 | 2013-12-05 01:24:37 +0200 | [diff] [blame] | 276 | /* |
| 277 | * Check for end marker with weight == -1 |
| 278 | */ |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 279 | #define for_each_event_constraint(e, c) \ |
Maria Dimakopoulou | cf30d52 | 2013-12-05 01:24:37 +0200 | [diff] [blame] | 280 | for ((e) = (c); (e)->weight != -1; (e)++) |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 281 | |
| 282 | /* |
| 283 | * Extra registers for specific events. |
| 284 | * |
| 285 | * Some events need large masks and require external MSRs. |
| 286 | * Those extra MSRs end up being shared for all events on |
| 287 | * a PMU and sometimes between PMU of sibling HT threads. |
| 288 | * In either case, the kernel needs to handle conflicting |
| 289 | * accesses to those extra, shared, regs. The data structure |
| 290 | * to manage those registers is stored in cpu_hw_event. |
| 291 | */ |
| 292 | struct extra_reg { |
| 293 | unsigned int event; |
| 294 | unsigned int msr; |
| 295 | u64 config_mask; |
| 296 | u64 valid_mask; |
| 297 | int idx; /* per_xxx->regs[] reg index */ |
Kan Liang | 338b522 | 2014-07-14 12:25:56 -0700 | [diff] [blame^] | 298 | bool extra_msr_access; |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 299 | }; |
| 300 | |
| 301 | #define EVENT_EXTRA_REG(e, ms, m, vm, i) { \ |
Kan Liang | 338b522 | 2014-07-14 12:25:56 -0700 | [diff] [blame^] | 302 | .event = (e), \ |
| 303 | .msr = (ms), \ |
| 304 | .config_mask = (m), \ |
| 305 | .valid_mask = (vm), \ |
| 306 | .idx = EXTRA_REG_##i, \ |
| 307 | .extra_msr_access = true, \ |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | #define INTEL_EVENT_EXTRA_REG(event, msr, vm, idx) \ |
| 311 | EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm, idx) |
| 312 | |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 313 | #define INTEL_UEVENT_EXTRA_REG(event, msr, vm, idx) \ |
| 314 | EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT | \ |
| 315 | ARCH_PERFMON_EVENTSEL_UMASK, vm, idx) |
| 316 | |
| 317 | #define INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(c) \ |
| 318 | INTEL_UEVENT_EXTRA_REG(c, \ |
| 319 | MSR_PEBS_LD_LAT_THRESHOLD, \ |
| 320 | 0xffff, \ |
| 321 | LDLAT) |
| 322 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 323 | #define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0, RSP_0) |
| 324 | |
| 325 | union perf_capabilities { |
| 326 | struct { |
| 327 | u64 lbr_format:6; |
| 328 | u64 pebs_trap:1; |
| 329 | u64 pebs_arch_reg:1; |
| 330 | u64 pebs_format:4; |
| 331 | u64 smm_freeze:1; |
Andi Kleen | 069e0c3 | 2013-06-25 08:12:33 -0700 | [diff] [blame] | 332 | /* |
| 333 | * PMU supports separate counter range for writing |
| 334 | * values > 32bit. |
| 335 | */ |
| 336 | u64 full_width_write:1; |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 337 | }; |
| 338 | u64 capabilities; |
| 339 | }; |
| 340 | |
Peter Zijlstra | c1d6f42 | 2011-12-06 14:07:15 +0100 | [diff] [blame] | 341 | struct x86_pmu_quirk { |
| 342 | struct x86_pmu_quirk *next; |
| 343 | void (*func)(void); |
| 344 | }; |
| 345 | |
Peter Zijlstra | f9b4eeb | 2012-03-12 12:44:35 +0100 | [diff] [blame] | 346 | union x86_pmu_config { |
| 347 | struct { |
| 348 | u64 event:8, |
| 349 | umask:8, |
| 350 | usr:1, |
| 351 | os:1, |
| 352 | edge:1, |
| 353 | pc:1, |
| 354 | interrupt:1, |
| 355 | __reserved1:1, |
| 356 | en:1, |
| 357 | inv:1, |
| 358 | cmask:8, |
| 359 | event2:4, |
| 360 | __reserved2:4, |
| 361 | go:1, |
| 362 | ho:1; |
| 363 | } bits; |
| 364 | u64 value; |
| 365 | }; |
| 366 | |
| 367 | #define X86_CONFIG(args...) ((union x86_pmu_config){.bits = {args}}).value |
| 368 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 369 | /* |
| 370 | * struct x86_pmu - generic x86 pmu |
| 371 | */ |
| 372 | struct x86_pmu { |
| 373 | /* |
| 374 | * Generic x86 PMC bits |
| 375 | */ |
| 376 | const char *name; |
| 377 | int version; |
| 378 | int (*handle_irq)(struct pt_regs *); |
| 379 | void (*disable_all)(void); |
| 380 | void (*enable_all)(int added); |
| 381 | void (*enable)(struct perf_event *); |
| 382 | void (*disable)(struct perf_event *); |
| 383 | int (*hw_config)(struct perf_event *event); |
| 384 | int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign); |
| 385 | unsigned eventsel; |
| 386 | unsigned perfctr; |
Jacob Shin | 4c1fd17 | 2013-02-06 11:26:27 -0600 | [diff] [blame] | 387 | int (*addr_offset)(int index, bool eventsel); |
Jacob Shin | 0fbdad0 | 2013-02-06 11:26:28 -0600 | [diff] [blame] | 388 | int (*rdpmc_index)(int index); |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 389 | u64 (*event_map)(int); |
| 390 | int max_events; |
| 391 | int num_counters; |
| 392 | int num_counters_fixed; |
| 393 | int cntval_bits; |
| 394 | u64 cntval_mask; |
Gleb Natapov | ffb871b | 2011-11-10 14:57:26 +0200 | [diff] [blame] | 395 | union { |
| 396 | unsigned long events_maskl; |
| 397 | unsigned long events_mask[BITS_TO_LONGS(ARCH_PERFMON_EVENTS_COUNT)]; |
| 398 | }; |
| 399 | int events_mask_len; |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 400 | int apic; |
| 401 | u64 max_period; |
| 402 | struct event_constraint * |
| 403 | (*get_event_constraints)(struct cpu_hw_events *cpuc, |
| 404 | struct perf_event *event); |
| 405 | |
| 406 | void (*put_event_constraints)(struct cpu_hw_events *cpuc, |
| 407 | struct perf_event *event); |
| 408 | struct event_constraint *event_constraints; |
Peter Zijlstra | c1d6f42 | 2011-12-06 14:07:15 +0100 | [diff] [blame] | 409 | struct x86_pmu_quirk *quirks; |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 410 | int perfctr_second_write; |
Andi Kleen | 72db559 | 2013-06-17 17:36:50 -0700 | [diff] [blame] | 411 | bool late_ack; |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 412 | |
Peter Zijlstra | 0c9d42e | 2011-11-20 23:30:47 +0100 | [diff] [blame] | 413 | /* |
| 414 | * sysfs attrs |
| 415 | */ |
Peter Zijlstra | e97df76 | 2014-02-05 20:48:51 +0100 | [diff] [blame] | 416 | int attr_rdpmc_broken; |
Peter Zijlstra | 0c9d42e | 2011-11-20 23:30:47 +0100 | [diff] [blame] | 417 | int attr_rdpmc; |
Jiri Olsa | 641cc93 | 2012-03-15 20:09:14 +0100 | [diff] [blame] | 418 | struct attribute **format_attrs; |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 419 | struct attribute **event_attrs; |
Peter Zijlstra | 0c9d42e | 2011-11-20 23:30:47 +0100 | [diff] [blame] | 420 | |
Jiri Olsa | a474739 | 2012-10-10 14:53:11 +0200 | [diff] [blame] | 421 | ssize_t (*events_sysfs_show)(char *page, u64 config); |
Andi Kleen | 1a6461b | 2013-01-24 16:10:25 +0100 | [diff] [blame] | 422 | struct attribute **cpu_events; |
Jiri Olsa | a474739 | 2012-10-10 14:53:11 +0200 | [diff] [blame] | 423 | |
Peter Zijlstra | 0c9d42e | 2011-11-20 23:30:47 +0100 | [diff] [blame] | 424 | /* |
| 425 | * CPU Hotplug hooks |
| 426 | */ |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 427 | int (*cpu_prepare)(int cpu); |
| 428 | void (*cpu_starting)(int cpu); |
| 429 | void (*cpu_dying)(int cpu); |
| 430 | void (*cpu_dead)(int cpu); |
Peter Zijlstra | c93dc84 | 2012-06-08 14:50:50 +0200 | [diff] [blame] | 431 | |
| 432 | void (*check_microcode)(void); |
Stephane Eranian | d010b33 | 2012-02-09 23:21:00 +0100 | [diff] [blame] | 433 | void (*flush_branch_stack)(void); |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 434 | |
| 435 | /* |
| 436 | * Intel Arch Perfmon v2+ |
| 437 | */ |
| 438 | u64 intel_ctrl; |
| 439 | union perf_capabilities intel_cap; |
| 440 | |
| 441 | /* |
| 442 | * Intel DebugStore bits |
| 443 | */ |
Peter Zijlstra | 597ed95 | 2012-07-09 13:50:23 +0200 | [diff] [blame] | 444 | unsigned int bts :1, |
Peter Zijlstra | 3e0091e | 2012-06-26 23:38:39 +0200 | [diff] [blame] | 445 | bts_active :1, |
| 446 | pebs :1, |
| 447 | pebs_active :1, |
| 448 | pebs_broken :1; |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 449 | int pebs_record_size; |
| 450 | void (*drain_pebs)(struct pt_regs *regs); |
| 451 | struct event_constraint *pebs_constraints; |
Peter Zijlstra | 0780c92 | 2012-06-05 10:26:43 +0200 | [diff] [blame] | 452 | void (*pebs_aliases)(struct perf_event *event); |
Andi Kleen | 70ab700 | 2012-06-05 17:56:48 -0700 | [diff] [blame] | 453 | int max_pebs_events; |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 454 | |
| 455 | /* |
| 456 | * Intel LBR |
| 457 | */ |
| 458 | unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */ |
| 459 | int lbr_nr; /* hardware stack size */ |
Stephane Eranian | b36817e | 2012-02-09 23:20:53 +0100 | [diff] [blame] | 460 | u64 lbr_sel_mask; /* LBR_SELECT valid bits */ |
| 461 | const int *lbr_sel_map; /* lbr_select mappings */ |
Andi Kleen | b7af41a | 2013-09-20 07:40:44 -0700 | [diff] [blame] | 462 | bool lbr_double_abort; /* duplicated lbr aborts */ |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 463 | |
| 464 | /* |
| 465 | * Extra registers for events |
| 466 | */ |
| 467 | struct extra_reg *extra_regs; |
| 468 | unsigned int er_flags; |
Gleb Natapov | 144d31e | 2011-10-05 14:01:21 +0200 | [diff] [blame] | 469 | |
| 470 | /* |
| 471 | * Intel host/guest support (KVM) |
| 472 | */ |
| 473 | struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr); |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 474 | }; |
| 475 | |
Peter Zijlstra | c1d6f42 | 2011-12-06 14:07:15 +0100 | [diff] [blame] | 476 | #define x86_add_quirk(func_) \ |
| 477 | do { \ |
| 478 | static struct x86_pmu_quirk __quirk __initdata = { \ |
| 479 | .func = func_, \ |
| 480 | }; \ |
| 481 | __quirk.next = x86_pmu.quirks; \ |
| 482 | x86_pmu.quirks = &__quirk; \ |
| 483 | } while (0) |
| 484 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 485 | #define ERF_NO_HT_SHARING 1 |
| 486 | #define ERF_HAS_RSP_1 2 |
| 487 | |
Stephane Eranian | 3a54aaa | 2013-01-24 16:10:26 +0100 | [diff] [blame] | 488 | #define EVENT_VAR(_id) event_attr_##_id |
| 489 | #define EVENT_PTR(_id) &event_attr_##_id.attr.attr |
| 490 | |
| 491 | #define EVENT_ATTR(_name, _id) \ |
| 492 | static struct perf_pmu_events_attr EVENT_VAR(_id) = { \ |
| 493 | .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \ |
| 494 | .id = PERF_COUNT_HW_##_id, \ |
| 495 | .event_str = NULL, \ |
| 496 | }; |
| 497 | |
| 498 | #define EVENT_ATTR_STR(_name, v, str) \ |
| 499 | static struct perf_pmu_events_attr event_attr_##v = { \ |
| 500 | .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \ |
| 501 | .id = 0, \ |
| 502 | .event_str = str, \ |
| 503 | }; |
| 504 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 505 | extern struct x86_pmu x86_pmu __read_mostly; |
| 506 | |
| 507 | DECLARE_PER_CPU(struct cpu_hw_events, cpu_hw_events); |
| 508 | |
| 509 | int x86_perf_event_set_period(struct perf_event *event); |
| 510 | |
| 511 | /* |
| 512 | * Generalized hw caching related hw_event table, filled |
| 513 | * in on a per model basis. A value of 0 means |
| 514 | * 'not supported', -1 means 'hw_event makes no sense on |
| 515 | * this CPU', any other value means the raw hw_event |
| 516 | * ID. |
| 517 | */ |
| 518 | |
| 519 | #define C(x) PERF_COUNT_HW_CACHE_##x |
| 520 | |
| 521 | extern u64 __read_mostly hw_cache_event_ids |
| 522 | [PERF_COUNT_HW_CACHE_MAX] |
| 523 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 524 | [PERF_COUNT_HW_CACHE_RESULT_MAX]; |
| 525 | extern u64 __read_mostly hw_cache_extra_regs |
| 526 | [PERF_COUNT_HW_CACHE_MAX] |
| 527 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 528 | [PERF_COUNT_HW_CACHE_RESULT_MAX]; |
| 529 | |
| 530 | u64 x86_perf_event_update(struct perf_event *event); |
| 531 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 532 | static inline unsigned int x86_pmu_config_addr(int index) |
| 533 | { |
Jacob Shin | 4c1fd17 | 2013-02-06 11:26:27 -0600 | [diff] [blame] | 534 | return x86_pmu.eventsel + (x86_pmu.addr_offset ? |
| 535 | x86_pmu.addr_offset(index, true) : index); |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | static inline unsigned int x86_pmu_event_addr(int index) |
| 539 | { |
Jacob Shin | 4c1fd17 | 2013-02-06 11:26:27 -0600 | [diff] [blame] | 540 | return x86_pmu.perfctr + (x86_pmu.addr_offset ? |
| 541 | x86_pmu.addr_offset(index, false) : index); |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 542 | } |
| 543 | |
Jacob Shin | 0fbdad0 | 2013-02-06 11:26:28 -0600 | [diff] [blame] | 544 | static inline int x86_pmu_rdpmc_index(int index) |
| 545 | { |
| 546 | return x86_pmu.rdpmc_index ? x86_pmu.rdpmc_index(index) : index; |
| 547 | } |
| 548 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 549 | int x86_setup_perfctr(struct perf_event *event); |
| 550 | |
| 551 | int x86_pmu_hw_config(struct perf_event *event); |
| 552 | |
| 553 | void x86_pmu_disable_all(void); |
| 554 | |
| 555 | static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc, |
| 556 | u64 enable_mask) |
| 557 | { |
Joerg Roedel | 1018faa | 2012-02-29 14:57:32 +0100 | [diff] [blame] | 558 | u64 disable_mask = __this_cpu_read(cpu_hw_events.perf_ctr_virt_mask); |
| 559 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 560 | if (hwc->extra_reg.reg) |
| 561 | wrmsrl(hwc->extra_reg.reg, hwc->extra_reg.config); |
Joerg Roedel | 1018faa | 2012-02-29 14:57:32 +0100 | [diff] [blame] | 562 | wrmsrl(hwc->config_base, (hwc->config | enable_mask) & ~disable_mask); |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | void x86_pmu_enable_all(int added); |
| 566 | |
Andrew Hunter | 43b45780 | 2013-05-23 11:07:03 -0700 | [diff] [blame] | 567 | int perf_assign_events(struct perf_event **events, int n, |
Yan, Zheng | 4b4969b | 2012-06-15 14:31:30 +0800 | [diff] [blame] | 568 | int wmin, int wmax, int *assign); |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 569 | int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign); |
| 570 | |
| 571 | void x86_pmu_stop(struct perf_event *event, int flags); |
| 572 | |
| 573 | static inline void x86_pmu_disable_event(struct perf_event *event) |
| 574 | { |
| 575 | struct hw_perf_event *hwc = &event->hw; |
| 576 | |
| 577 | wrmsrl(hwc->config_base, hwc->config); |
| 578 | } |
| 579 | |
| 580 | void x86_pmu_enable_event(struct perf_event *event); |
| 581 | |
| 582 | int x86_pmu_handle_irq(struct pt_regs *regs); |
| 583 | |
| 584 | extern struct event_constraint emptyconstraint; |
| 585 | |
| 586 | extern struct event_constraint unconstrained; |
| 587 | |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 588 | static inline bool kernel_ip(unsigned long ip) |
| 589 | { |
| 590 | #ifdef CONFIG_X86_32 |
| 591 | return ip > PAGE_OFFSET; |
| 592 | #else |
| 593 | return (long)ip < 0; |
| 594 | #endif |
| 595 | } |
| 596 | |
Peter Zijlstra | d07bdfd | 2012-07-10 09:42:15 +0200 | [diff] [blame] | 597 | /* |
| 598 | * Not all PMUs provide the right context information to place the reported IP |
| 599 | * into full context. Specifically segment registers are typically not |
| 600 | * supplied. |
| 601 | * |
| 602 | * Assuming the address is a linear address (it is for IBS), we fake the CS and |
| 603 | * vm86 mode using the known zero-based code segment and 'fix up' the registers |
| 604 | * to reflect this. |
| 605 | * |
| 606 | * Intel PEBS/LBR appear to typically provide the effective address, nothing |
| 607 | * much we can do about that but pray and treat it like a linear address. |
| 608 | */ |
| 609 | static inline void set_linear_ip(struct pt_regs *regs, unsigned long ip) |
| 610 | { |
| 611 | regs->cs = kernel_ip(ip) ? __KERNEL_CS : __USER_CS; |
| 612 | if (regs->flags & X86_VM_MASK) |
| 613 | regs->flags ^= (PERF_EFLAGS_VM | X86_VM_MASK); |
| 614 | regs->ip = ip; |
| 615 | } |
| 616 | |
Jiri Olsa | 0bf79d4 | 2012-10-10 14:53:14 +0200 | [diff] [blame] | 617 | ssize_t x86_event_sysfs_show(char *page, u64 config, u64 event); |
Jiri Olsa | 20550a4 | 2012-10-10 14:53:15 +0200 | [diff] [blame] | 618 | ssize_t intel_event_sysfs_show(char *page, u64 config); |
Jiri Olsa | 43c032f | 2012-10-10 14:53:13 +0200 | [diff] [blame] | 619 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 620 | #ifdef CONFIG_CPU_SUP_AMD |
| 621 | |
| 622 | int amd_pmu_init(void); |
| 623 | |
| 624 | #else /* CONFIG_CPU_SUP_AMD */ |
| 625 | |
| 626 | static inline int amd_pmu_init(void) |
| 627 | { |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | #endif /* CONFIG_CPU_SUP_AMD */ |
| 632 | |
| 633 | #ifdef CONFIG_CPU_SUP_INTEL |
| 634 | |
| 635 | int intel_pmu_save_and_restart(struct perf_event *event); |
| 636 | |
| 637 | struct event_constraint * |
| 638 | x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event); |
| 639 | |
| 640 | struct intel_shared_regs *allocate_shared_regs(int cpu); |
| 641 | |
| 642 | int intel_pmu_init(void); |
| 643 | |
| 644 | void init_debug_store_on_cpu(int cpu); |
| 645 | |
| 646 | void fini_debug_store_on_cpu(int cpu); |
| 647 | |
| 648 | void release_ds_buffers(void); |
| 649 | |
| 650 | void reserve_ds_buffers(void); |
| 651 | |
| 652 | extern struct event_constraint bts_constraint; |
| 653 | |
| 654 | void intel_pmu_enable_bts(u64 config); |
| 655 | |
| 656 | void intel_pmu_disable_bts(void); |
| 657 | |
| 658 | int intel_pmu_drain_bts_buffer(void); |
| 659 | |
| 660 | extern struct event_constraint intel_core2_pebs_event_constraints[]; |
| 661 | |
| 662 | extern struct event_constraint intel_atom_pebs_event_constraints[]; |
| 663 | |
Yan, Zheng | 1fa6418 | 2013-07-18 17:02:24 +0800 | [diff] [blame] | 664 | extern struct event_constraint intel_slm_pebs_event_constraints[]; |
| 665 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 666 | extern struct event_constraint intel_nehalem_pebs_event_constraints[]; |
| 667 | |
| 668 | extern struct event_constraint intel_westmere_pebs_event_constraints[]; |
| 669 | |
| 670 | extern struct event_constraint intel_snb_pebs_event_constraints[]; |
| 671 | |
Stephane Eranian | 20a36e3 | 2012-09-11 01:07:01 +0200 | [diff] [blame] | 672 | extern struct event_constraint intel_ivb_pebs_event_constraints[]; |
| 673 | |
Andi Kleen | 3044318 | 2013-06-17 17:36:49 -0700 | [diff] [blame] | 674 | extern struct event_constraint intel_hsw_pebs_event_constraints[]; |
| 675 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 676 | struct event_constraint *intel_pebs_constraints(struct perf_event *event); |
| 677 | |
| 678 | void intel_pmu_pebs_enable(struct perf_event *event); |
| 679 | |
| 680 | void intel_pmu_pebs_disable(struct perf_event *event); |
| 681 | |
| 682 | void intel_pmu_pebs_enable_all(void); |
| 683 | |
| 684 | void intel_pmu_pebs_disable_all(void); |
| 685 | |
| 686 | void intel_ds_init(void); |
| 687 | |
| 688 | void intel_pmu_lbr_reset(void); |
| 689 | |
| 690 | void intel_pmu_lbr_enable(struct perf_event *event); |
| 691 | |
| 692 | void intel_pmu_lbr_disable(struct perf_event *event); |
| 693 | |
| 694 | void intel_pmu_lbr_enable_all(void); |
| 695 | |
| 696 | void intel_pmu_lbr_disable_all(void); |
| 697 | |
| 698 | void intel_pmu_lbr_read(void); |
| 699 | |
| 700 | void intel_pmu_lbr_init_core(void); |
| 701 | |
| 702 | void intel_pmu_lbr_init_nhm(void); |
| 703 | |
| 704 | void intel_pmu_lbr_init_atom(void); |
| 705 | |
Stephane Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 706 | void intel_pmu_lbr_init_snb(void); |
| 707 | |
Stephane Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 708 | int intel_pmu_setup_lbr_filter(struct perf_event *event); |
| 709 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 710 | int p4_pmu_init(void); |
| 711 | |
| 712 | int p6_pmu_init(void); |
| 713 | |
Vince Weaver | e717bf4 | 2012-09-26 14:12:52 -0400 | [diff] [blame] | 714 | int knc_pmu_init(void); |
| 715 | |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 716 | ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr, |
| 717 | char *page); |
| 718 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 719 | #else /* CONFIG_CPU_SUP_INTEL */ |
| 720 | |
| 721 | static inline void reserve_ds_buffers(void) |
| 722 | { |
| 723 | } |
| 724 | |
| 725 | static inline void release_ds_buffers(void) |
| 726 | { |
| 727 | } |
| 728 | |
| 729 | static inline int intel_pmu_init(void) |
| 730 | { |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | static inline struct intel_shared_regs *allocate_shared_regs(int cpu) |
| 735 | { |
| 736 | return NULL; |
| 737 | } |
| 738 | |
| 739 | #endif /* CONFIG_CPU_SUP_INTEL */ |