Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2 | * Performance events x86 architecture code |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 3 | * |
Ingo Molnar | 9814451 | 2009-04-29 14:52:50 +0200 | [diff] [blame] | 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> |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 9 | * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com> |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 10 | * Copyright (C) 2009 Google, Inc., Stephane Eranian |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 11 | * |
| 12 | * For licencing details see kernel-base/COPYING |
| 13 | */ |
| 14 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 15 | #include <linux/perf_event.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 16 | #include <linux/capability.h> |
| 17 | #include <linux/notifier.h> |
| 18 | #include <linux/hardirq.h> |
| 19 | #include <linux/kprobes.h> |
Thomas Gleixner | 4ac1329 | 2008-12-09 21:43:39 +0100 | [diff] [blame] | 20 | #include <linux/module.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 21 | #include <linux/kdebug.h> |
| 22 | #include <linux/sched.h> |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 23 | #include <linux/uaccess.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 25 | #include <linux/highmem.h> |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 26 | #include <linux/cpu.h> |
Peter Zijlstra | 272d30b | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 27 | #include <linux/bitops.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 28 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 29 | #include <asm/apic.h> |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 30 | #include <asm/stacktrace.h> |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 31 | #include <asm/nmi.h> |
Torok Edwin | 257ef9d | 2010-03-17 12:07:16 +0200 | [diff] [blame] | 32 | #include <asm/compat.h> |
Lin Ming | 6909262 | 2011-03-03 10:34:50 +0800 | [diff] [blame] | 33 | #include <asm/smp.h> |
Robert Richter | c8e5910 | 2011-04-16 02:27:55 +0200 | [diff] [blame] | 34 | #include <asm/alternative.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 35 | |
Peter Zijlstra | 7645a24 | 2010-03-08 13:51:31 +0100 | [diff] [blame] | 36 | #if 0 |
| 37 | #undef wrmsrl |
| 38 | #define wrmsrl(msr, val) \ |
| 39 | do { \ |
| 40 | trace_printk("wrmsrl(%lx, %lx)\n", (unsigned long)(msr),\ |
| 41 | (unsigned long)(val)); \ |
| 42 | native_write_msr((msr), (u32)((u64)(val)), \ |
| 43 | (u32)((u64)(val) >> 32)); \ |
| 44 | } while (0) |
| 45 | #endif |
| 46 | |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 47 | /* |
Stephane Eranian | efc9f05 | 2011-06-06 16:57:03 +0200 | [diff] [blame^] | 48 | * | NHM/WSM | SNB | |
| 49 | * register ------------------------------- |
| 50 | * | HT | no HT | HT | no HT | |
| 51 | *----------------------------------------- |
| 52 | * offcore | core | core | cpu | core | |
| 53 | * lbr_sel | core | core | cpu | core | |
| 54 | * ld_lat | cpu | core | cpu | core | |
| 55 | *----------------------------------------- |
| 56 | * |
| 57 | * Given that there is a small number of shared regs, |
| 58 | * we can pre-allocate their slot in the per-cpu |
| 59 | * per-core reg tables. |
| 60 | */ |
| 61 | enum extra_reg_type { |
| 62 | EXTRA_REG_NONE = -1, /* not used */ |
| 63 | |
| 64 | EXTRA_REG_RSP_0 = 0, /* offcore_response_0 */ |
| 65 | EXTRA_REG_RSP_1 = 1, /* offcore_response_1 */ |
| 66 | |
| 67 | EXTRA_REG_MAX /* number of entries needed */ |
| 68 | }; |
| 69 | |
| 70 | /* |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 71 | * best effort, GUP based copy_from_user() that assumes IRQ or NMI context |
| 72 | */ |
| 73 | static unsigned long |
| 74 | copy_from_user_nmi(void *to, const void __user *from, unsigned long n) |
| 75 | { |
| 76 | unsigned long offset, addr = (unsigned long)from; |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 77 | unsigned long size, len = 0; |
| 78 | struct page *page; |
| 79 | void *map; |
| 80 | int ret; |
| 81 | |
| 82 | do { |
| 83 | ret = __get_user_pages_fast(addr, 1, 0, &page); |
| 84 | if (!ret) |
| 85 | break; |
| 86 | |
| 87 | offset = addr & (PAGE_SIZE - 1); |
| 88 | size = min(PAGE_SIZE - offset, n - len); |
| 89 | |
Peter Zijlstra | 7a837d1 | 2010-10-26 14:21:53 -0700 | [diff] [blame] | 90 | map = kmap_atomic(page); |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 91 | memcpy(to, map+offset, size); |
Peter Zijlstra | 7a837d1 | 2010-10-26 14:21:53 -0700 | [diff] [blame] | 92 | kunmap_atomic(map); |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 93 | put_page(page); |
| 94 | |
| 95 | len += size; |
| 96 | to += size; |
| 97 | addr += size; |
| 98 | |
| 99 | } while (len < n); |
| 100 | |
| 101 | return len; |
| 102 | } |
| 103 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 104 | struct event_constraint { |
Peter Zijlstra | c91e0f5 | 2010-01-22 15:25:59 +0100 | [diff] [blame] | 105 | union { |
| 106 | unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
Peter Zijlstra | b622d64 | 2010-02-01 15:36:30 +0100 | [diff] [blame] | 107 | u64 idxmsk64; |
Peter Zijlstra | c91e0f5 | 2010-01-22 15:25:59 +0100 | [diff] [blame] | 108 | }; |
Peter Zijlstra | b622d64 | 2010-02-01 15:36:30 +0100 | [diff] [blame] | 109 | u64 code; |
| 110 | u64 cmask; |
Peter Zijlstra | 272d30b | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 111 | int weight; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 112 | }; |
| 113 | |
Stephane Eranian | 38331f6 | 2010-02-08 17:17:01 +0200 | [diff] [blame] | 114 | struct amd_nb { |
| 115 | int nb_id; /* NorthBridge id */ |
| 116 | int refcnt; /* reference count */ |
| 117 | struct perf_event *owners[X86_PMC_IDX_MAX]; |
| 118 | struct event_constraint event_constraints[X86_PMC_IDX_MAX]; |
| 119 | }; |
| 120 | |
Andi Kleen | a7e3ed1 | 2011-03-03 10:34:47 +0800 | [diff] [blame] | 121 | struct intel_percore; |
| 122 | |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 123 | #define MAX_LBR_ENTRIES 16 |
| 124 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 125 | struct cpu_hw_events { |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 126 | /* |
| 127 | * Generic x86 PMC bits |
| 128 | */ |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 129 | struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */ |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 130 | unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
Robert Richter | 63e6be6 | 2010-09-15 18:20:34 +0200 | [diff] [blame] | 131 | unsigned long running[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 132 | int enabled; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 133 | |
| 134 | int n_events; |
| 135 | int n_added; |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 136 | int n_txn; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 137 | int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */ |
Stephane Eranian | 447a194 | 2010-02-01 14:50:01 +0200 | [diff] [blame] | 138 | u64 tags[X86_PMC_IDX_MAX]; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 139 | struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */ |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 140 | |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 141 | unsigned int group_flag; |
| 142 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 143 | /* |
| 144 | * Intel DebugStore bits |
| 145 | */ |
| 146 | struct debug_store *ds; |
| 147 | u64 pebs_enabled; |
| 148 | |
| 149 | /* |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 150 | * Intel LBR bits |
| 151 | */ |
| 152 | int lbr_users; |
| 153 | void *lbr_context; |
| 154 | struct perf_branch_stack lbr_stack; |
| 155 | struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES]; |
| 156 | |
| 157 | /* |
Stephane Eranian | efc9f05 | 2011-06-06 16:57:03 +0200 | [diff] [blame^] | 158 | * manage shared (per-core, per-cpu) registers |
| 159 | * used on Intel NHM/WSM/SNB |
Andi Kleen | a7e3ed1 | 2011-03-03 10:34:47 +0800 | [diff] [blame] | 160 | */ |
Stephane Eranian | efc9f05 | 2011-06-06 16:57:03 +0200 | [diff] [blame^] | 161 | struct intel_shared_regs *shared_regs; |
Andi Kleen | a7e3ed1 | 2011-03-03 10:34:47 +0800 | [diff] [blame] | 162 | |
| 163 | /* |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 164 | * AMD specific bits |
| 165 | */ |
Stephane Eranian | 38331f6 | 2010-02-08 17:17:01 +0200 | [diff] [blame] | 166 | struct amd_nb *amd_nb; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 167 | }; |
| 168 | |
Peter Zijlstra | fce877e | 2010-01-29 13:25:12 +0100 | [diff] [blame] | 169 | #define __EVENT_CONSTRAINT(c, n, m, w) {\ |
Peter Zijlstra | b622d64 | 2010-02-01 15:36:30 +0100 | [diff] [blame] | 170 | { .idxmsk64 = (n) }, \ |
Peter Zijlstra | c91e0f5 | 2010-01-22 15:25:59 +0100 | [diff] [blame] | 171 | .code = (c), \ |
| 172 | .cmask = (m), \ |
Peter Zijlstra | fce877e | 2010-01-29 13:25:12 +0100 | [diff] [blame] | 173 | .weight = (w), \ |
Peter Zijlstra | c91e0f5 | 2010-01-22 15:25:59 +0100 | [diff] [blame] | 174 | } |
Stephane Eranian | b690081 | 2009-10-06 16:42:09 +0200 | [diff] [blame] | 175 | |
Peter Zijlstra | fce877e | 2010-01-29 13:25:12 +0100 | [diff] [blame] | 176 | #define EVENT_CONSTRAINT(c, n, m) \ |
| 177 | __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n)) |
| 178 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 179 | /* |
| 180 | * Constraint on the Event code. |
| 181 | */ |
Peter Zijlstra | ed8777f | 2010-01-27 23:07:46 +0100 | [diff] [blame] | 182 | #define INTEL_EVENT_CONSTRAINT(c, n) \ |
Robert Richter | a098f44 | 2010-03-30 11:28:21 +0200 | [diff] [blame] | 183 | EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT) |
Peter Zijlstra | 8433be1 | 2010-01-22 15:38:26 +0100 | [diff] [blame] | 184 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 185 | /* |
| 186 | * Constraint on the Event code + UMask + fixed-mask |
Robert Richter | a098f44 | 2010-03-30 11:28:21 +0200 | [diff] [blame] | 187 | * |
| 188 | * filter mask to validate fixed counter events. |
| 189 | * the following filters disqualify for fixed counters: |
| 190 | * - inv |
| 191 | * - edge |
| 192 | * - cnt-mask |
| 193 | * The other filters are supported by fixed counters. |
| 194 | * The any-thread option is supported starting with v3. |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 195 | */ |
Peter Zijlstra | ed8777f | 2010-01-27 23:07:46 +0100 | [diff] [blame] | 196 | #define FIXED_EVENT_CONSTRAINT(c, n) \ |
Robert Richter | a098f44 | 2010-03-30 11:28:21 +0200 | [diff] [blame] | 197 | EVENT_CONSTRAINT(c, (1ULL << (32+n)), X86_RAW_EVENT_MASK) |
Peter Zijlstra | 8433be1 | 2010-01-22 15:38:26 +0100 | [diff] [blame] | 198 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 199 | /* |
| 200 | * Constraint on the Event code + UMask |
| 201 | */ |
Lin Ming | b06b3d4 | 2011-03-02 21:27:04 +0800 | [diff] [blame] | 202 | #define INTEL_UEVENT_CONSTRAINT(c, n) \ |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 203 | EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK) |
| 204 | |
Peter Zijlstra | ed8777f | 2010-01-27 23:07:46 +0100 | [diff] [blame] | 205 | #define EVENT_CONSTRAINT_END \ |
| 206 | EVENT_CONSTRAINT(0, 0, 0) |
| 207 | |
| 208 | #define for_each_event_constraint(e, c) \ |
Robert Richter | a1f2b70 | 2010-04-13 22:23:15 +0200 | [diff] [blame] | 209 | for ((e) = (c); (e)->weight; (e)++) |
Stephane Eranian | b690081 | 2009-10-06 16:42:09 +0200 | [diff] [blame] | 210 | |
Andi Kleen | a7e3ed1 | 2011-03-03 10:34:47 +0800 | [diff] [blame] | 211 | /* |
Stephane Eranian | efc9f05 | 2011-06-06 16:57:03 +0200 | [diff] [blame^] | 212 | * Per register state. |
| 213 | */ |
| 214 | struct er_account { |
| 215 | raw_spinlock_t lock; /* per-core: protect structure */ |
| 216 | u64 config; /* extra MSR config */ |
| 217 | u64 reg; /* extra MSR number */ |
| 218 | atomic_t ref; /* reference count */ |
| 219 | }; |
| 220 | |
| 221 | /* |
Andi Kleen | a7e3ed1 | 2011-03-03 10:34:47 +0800 | [diff] [blame] | 222 | * Extra registers for specific events. |
Stephane Eranian | efc9f05 | 2011-06-06 16:57:03 +0200 | [diff] [blame^] | 223 | * |
Andi Kleen | a7e3ed1 | 2011-03-03 10:34:47 +0800 | [diff] [blame] | 224 | * Some events need large masks and require external MSRs. |
Stephane Eranian | efc9f05 | 2011-06-06 16:57:03 +0200 | [diff] [blame^] | 225 | * Those extra MSRs end up being shared for all events on |
| 226 | * a PMU and sometimes between PMU of sibling HT threads. |
| 227 | * In either case, the kernel needs to handle conflicting |
| 228 | * accesses to those extra, shared, regs. The data structure |
| 229 | * to manage those registers is stored in cpu_hw_event. |
Andi Kleen | a7e3ed1 | 2011-03-03 10:34:47 +0800 | [diff] [blame] | 230 | */ |
| 231 | struct extra_reg { |
| 232 | unsigned int event; |
| 233 | unsigned int msr; |
| 234 | u64 config_mask; |
| 235 | u64 valid_mask; |
Stephane Eranian | efc9f05 | 2011-06-06 16:57:03 +0200 | [diff] [blame^] | 236 | int idx; /* per_xxx->regs[] reg index */ |
Andi Kleen | a7e3ed1 | 2011-03-03 10:34:47 +0800 | [diff] [blame] | 237 | }; |
| 238 | |
Stephane Eranian | efc9f05 | 2011-06-06 16:57:03 +0200 | [diff] [blame^] | 239 | #define EVENT_EXTRA_REG(e, ms, m, vm, i) { \ |
Andi Kleen | a7e3ed1 | 2011-03-03 10:34:47 +0800 | [diff] [blame] | 240 | .event = (e), \ |
| 241 | .msr = (ms), \ |
| 242 | .config_mask = (m), \ |
| 243 | .valid_mask = (vm), \ |
Stephane Eranian | efc9f05 | 2011-06-06 16:57:03 +0200 | [diff] [blame^] | 244 | .idx = EXTRA_REG_##i \ |
Andi Kleen | a7e3ed1 | 2011-03-03 10:34:47 +0800 | [diff] [blame] | 245 | } |
Stephane Eranian | efc9f05 | 2011-06-06 16:57:03 +0200 | [diff] [blame^] | 246 | |
| 247 | #define INTEL_EVENT_EXTRA_REG(event, msr, vm, idx) \ |
| 248 | EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm, idx) |
| 249 | |
| 250 | #define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0, RSP_0) |
Andi Kleen | a7e3ed1 | 2011-03-03 10:34:47 +0800 | [diff] [blame] | 251 | |
Peter Zijlstra | 8db909a | 2010-03-03 17:07:40 +0100 | [diff] [blame] | 252 | union perf_capabilities { |
| 253 | struct { |
| 254 | u64 lbr_format : 6; |
| 255 | u64 pebs_trap : 1; |
| 256 | u64 pebs_arch_reg : 1; |
| 257 | u64 pebs_format : 4; |
| 258 | u64 smm_freeze : 1; |
| 259 | }; |
| 260 | u64 capabilities; |
| 261 | }; |
| 262 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 263 | /* |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 264 | * struct x86_pmu - generic x86 pmu |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 265 | */ |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 266 | struct x86_pmu { |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 267 | /* |
| 268 | * Generic x86 PMC bits |
| 269 | */ |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 270 | const char *name; |
| 271 | int version; |
Yong Wang | a328810 | 2009-06-03 13:12:55 +0800 | [diff] [blame] | 272 | int (*handle_irq)(struct pt_regs *); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 273 | void (*disable_all)(void); |
Peter Zijlstra | 11164cd | 2010-03-26 14:08:44 +0100 | [diff] [blame] | 274 | void (*enable_all)(int added); |
Peter Zijlstra | aff3d91 | 2010-03-02 20:32:08 +0100 | [diff] [blame] | 275 | void (*enable)(struct perf_event *); |
| 276 | void (*disable)(struct perf_event *); |
Cyrill Gorcunov | 1880c4a | 2011-06-23 16:49:18 +0400 | [diff] [blame] | 277 | void (*hw_watchdog_set_attr)(struct perf_event_attr *attr); |
Peter Zijlstra | b4cdc5c | 2010-03-30 17:00:06 +0200 | [diff] [blame] | 278 | int (*hw_config)(struct perf_event *event); |
Cyrill Gorcunov | a072738 | 2010-03-11 19:54:39 +0300 | [diff] [blame] | 279 | int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign); |
Jaswinder Singh Rajput | 169e41e | 2009-02-28 18:37:49 +0530 | [diff] [blame] | 280 | unsigned eventsel; |
| 281 | unsigned perfctr; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 282 | u64 (*event_map)(int); |
Jaswinder Singh Rajput | 169e41e | 2009-02-28 18:37:49 +0530 | [diff] [blame] | 283 | int max_events; |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 284 | int num_counters; |
| 285 | int num_counters_fixed; |
| 286 | int cntval_bits; |
| 287 | u64 cntval_mask; |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 288 | int apic; |
Robert Richter | c619b8f | 2009-04-29 12:47:23 +0200 | [diff] [blame] | 289 | u64 max_period; |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 290 | struct event_constraint * |
| 291 | (*get_event_constraints)(struct cpu_hw_events *cpuc, |
| 292 | struct perf_event *event); |
| 293 | |
Peter Zijlstra | c91e0f5 | 2010-01-22 15:25:59 +0100 | [diff] [blame] | 294 | void (*put_event_constraints)(struct cpu_hw_events *cpuc, |
| 295 | struct perf_event *event); |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 296 | struct event_constraint *event_constraints; |
Peter Zijlstra | 3c44780 | 2010-03-04 21:49:01 +0100 | [diff] [blame] | 297 | void (*quirks)(void); |
Cyrill Gorcunov | 68aa00a | 2010-06-03 01:23:04 +0400 | [diff] [blame] | 298 | int perfctr_second_write; |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 299 | |
Peter Zijlstra | b38b24e | 2010-03-23 19:31:15 +0100 | [diff] [blame] | 300 | int (*cpu_prepare)(int cpu); |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 301 | void (*cpu_starting)(int cpu); |
| 302 | void (*cpu_dying)(int cpu); |
| 303 | void (*cpu_dead)(int cpu); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 304 | |
| 305 | /* |
| 306 | * Intel Arch Perfmon v2+ |
| 307 | */ |
Peter Zijlstra | 8db909a | 2010-03-03 17:07:40 +0100 | [diff] [blame] | 308 | u64 intel_ctrl; |
| 309 | union perf_capabilities intel_cap; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 310 | |
| 311 | /* |
| 312 | * Intel DebugStore bits |
| 313 | */ |
| 314 | int bts, pebs; |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 315 | int bts_active, pebs_active; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 316 | int pebs_record_size; |
| 317 | void (*drain_pebs)(struct pt_regs *regs); |
| 318 | struct event_constraint *pebs_constraints; |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 319 | |
| 320 | /* |
| 321 | * Intel LBR |
| 322 | */ |
| 323 | unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */ |
| 324 | int lbr_nr; /* hardware stack size */ |
Andi Kleen | a7e3ed1 | 2011-03-03 10:34:47 +0800 | [diff] [blame] | 325 | |
| 326 | /* |
| 327 | * Extra registers for events |
| 328 | */ |
| 329 | struct extra_reg *extra_regs; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 330 | }; |
| 331 | |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 332 | static struct x86_pmu x86_pmu __read_mostly; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 333 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 334 | static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 335 | .enabled = 1, |
| 336 | }; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 337 | |
Peter Zijlstra | 07088ed | 2010-03-02 20:16:01 +0100 | [diff] [blame] | 338 | static int x86_perf_event_set_period(struct perf_event *event); |
Stephane Eranian | b690081 | 2009-10-06 16:42:09 +0200 | [diff] [blame] | 339 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 340 | /* |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 341 | * Generalized hw caching related hw_event table, filled |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 342 | * in on a per model basis. A value of 0 means |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 343 | * 'not supported', -1 means 'hw_event makes no sense on |
| 344 | * this CPU', any other value means the raw hw_event |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 345 | * ID. |
| 346 | */ |
| 347 | |
| 348 | #define C(x) PERF_COUNT_HW_CACHE_##x |
| 349 | |
| 350 | static u64 __read_mostly hw_cache_event_ids |
| 351 | [PERF_COUNT_HW_CACHE_MAX] |
| 352 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 353 | [PERF_COUNT_HW_CACHE_RESULT_MAX]; |
Andi Kleen | e994d7d | 2011-03-03 10:34:48 +0800 | [diff] [blame] | 354 | static u64 __read_mostly hw_cache_extra_regs |
| 355 | [PERF_COUNT_HW_CACHE_MAX] |
| 356 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 357 | [PERF_COUNT_HW_CACHE_RESULT_MAX]; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 358 | |
Cyrill Gorcunov | 1880c4a | 2011-06-23 16:49:18 +0400 | [diff] [blame] | 359 | void hw_nmi_watchdog_set_attr(struct perf_event_attr *wd_attr) |
| 360 | { |
| 361 | if (x86_pmu.hw_watchdog_set_attr) |
| 362 | x86_pmu.hw_watchdog_set_attr(wd_attr); |
| 363 | } |
| 364 | |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 365 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 366 | * Propagate event elapsed time into the generic event. |
| 367 | * Can only be executed on the CPU where the event is active. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 368 | * Returns the delta events processed. |
| 369 | */ |
Robert Richter | 4b7bfd0 | 2009-04-29 12:47:22 +0200 | [diff] [blame] | 370 | static u64 |
Peter Zijlstra | cc2ad4b | 2010-03-02 20:18:39 +0100 | [diff] [blame] | 371 | x86_perf_event_update(struct perf_event *event) |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 372 | { |
Peter Zijlstra | cc2ad4b | 2010-03-02 20:18:39 +0100 | [diff] [blame] | 373 | struct hw_perf_event *hwc = &event->hw; |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 374 | int shift = 64 - x86_pmu.cntval_bits; |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 375 | u64 prev_raw_count, new_raw_count; |
Peter Zijlstra | cc2ad4b | 2010-03-02 20:18:39 +0100 | [diff] [blame] | 376 | int idx = hwc->idx; |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 377 | s64 delta; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 378 | |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 379 | if (idx == X86_PMC_IDX_FIXED_BTS) |
| 380 | return 0; |
| 381 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 382 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 383 | * Careful: an NMI might modify the previous event value. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 384 | * |
| 385 | * Our tactic to handle this is to first atomically read and |
| 386 | * exchange a new raw count - then add that new-prev delta |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 387 | * count to the generic event atomically: |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 388 | */ |
| 389 | again: |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 390 | prev_raw_count = local64_read(&hwc->prev_count); |
Robert Richter | 73d6e52 | 2011-02-02 17:40:59 +0100 | [diff] [blame] | 391 | rdmsrl(hwc->event_base, new_raw_count); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 392 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 393 | if (local64_cmpxchg(&hwc->prev_count, prev_raw_count, |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 394 | new_raw_count) != prev_raw_count) |
| 395 | goto again; |
| 396 | |
| 397 | /* |
| 398 | * Now we have the new raw value and have updated the prev |
| 399 | * timestamp already. We can now calculate the elapsed delta |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 400 | * (event-)time and add that to the generic event. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 401 | * |
| 402 | * Careful, not all hw sign-extends above the physical width |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 403 | * of the count. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 404 | */ |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 405 | delta = (new_raw_count << shift) - (prev_raw_count << shift); |
| 406 | delta >>= shift; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 407 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 408 | local64_add(delta, &event->count); |
| 409 | local64_sub(delta, &hwc->period_left); |
Robert Richter | 4b7bfd0 | 2009-04-29 12:47:22 +0200 | [diff] [blame] | 410 | |
| 411 | return new_raw_count; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 412 | } |
| 413 | |
Robert Richter | 4979d27 | 2011-02-02 17:36:12 +0100 | [diff] [blame] | 414 | static inline int x86_pmu_addr_offset(int index) |
| 415 | { |
Robert Richter | c8e5910 | 2011-04-16 02:27:55 +0200 | [diff] [blame] | 416 | int offset; |
| 417 | |
| 418 | /* offset = X86_FEATURE_PERFCTR_CORE ? index << 1 : index */ |
| 419 | alternative_io(ASM_NOP2, |
| 420 | "shll $1, %%eax", |
| 421 | X86_FEATURE_PERFCTR_CORE, |
| 422 | "=a" (offset), |
| 423 | "a" (index)); |
| 424 | |
| 425 | return offset; |
Robert Richter | 4979d27 | 2011-02-02 17:36:12 +0100 | [diff] [blame] | 426 | } |
| 427 | |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 428 | static inline unsigned int x86_pmu_config_addr(int index) |
| 429 | { |
Robert Richter | 4979d27 | 2011-02-02 17:36:12 +0100 | [diff] [blame] | 430 | return x86_pmu.eventsel + x86_pmu_addr_offset(index); |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static inline unsigned int x86_pmu_event_addr(int index) |
| 434 | { |
Robert Richter | 4979d27 | 2011-02-02 17:36:12 +0100 | [diff] [blame] | 435 | return x86_pmu.perfctr + x86_pmu_addr_offset(index); |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 436 | } |
| 437 | |
Andi Kleen | a7e3ed1 | 2011-03-03 10:34:47 +0800 | [diff] [blame] | 438 | /* |
| 439 | * Find and validate any extra registers to set up. |
| 440 | */ |
| 441 | static int x86_pmu_extra_regs(u64 config, struct perf_event *event) |
| 442 | { |
Stephane Eranian | efc9f05 | 2011-06-06 16:57:03 +0200 | [diff] [blame^] | 443 | struct hw_perf_event_extra *reg; |
Andi Kleen | a7e3ed1 | 2011-03-03 10:34:47 +0800 | [diff] [blame] | 444 | struct extra_reg *er; |
| 445 | |
Stephane Eranian | efc9f05 | 2011-06-06 16:57:03 +0200 | [diff] [blame^] | 446 | reg = &event->hw.extra_reg; |
Andi Kleen | a7e3ed1 | 2011-03-03 10:34:47 +0800 | [diff] [blame] | 447 | |
| 448 | if (!x86_pmu.extra_regs) |
| 449 | return 0; |
| 450 | |
| 451 | for (er = x86_pmu.extra_regs; er->msr; er++) { |
| 452 | if (er->event != (config & er->config_mask)) |
| 453 | continue; |
| 454 | if (event->attr.config1 & ~er->valid_mask) |
| 455 | return -EINVAL; |
Stephane Eranian | efc9f05 | 2011-06-06 16:57:03 +0200 | [diff] [blame^] | 456 | |
| 457 | reg->idx = er->idx; |
| 458 | reg->config = event->attr.config1; |
| 459 | reg->reg = er->msr; |
Andi Kleen | a7e3ed1 | 2011-03-03 10:34:47 +0800 | [diff] [blame] | 460 | break; |
| 461 | } |
| 462 | return 0; |
| 463 | } |
| 464 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 465 | static atomic_t active_events; |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 466 | static DEFINE_MUTEX(pmc_reserve_mutex); |
| 467 | |
Robert Richter | b27ea29 | 2010-03-17 12:49:10 +0100 | [diff] [blame] | 468 | #ifdef CONFIG_X86_LOCAL_APIC |
| 469 | |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 470 | static bool reserve_pmc_hardware(void) |
| 471 | { |
| 472 | int i; |
| 473 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 474 | for (i = 0; i < x86_pmu.num_counters; i++) { |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 475 | if (!reserve_perfctr_nmi(x86_pmu_event_addr(i))) |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 476 | goto perfctr_fail; |
| 477 | } |
| 478 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 479 | for (i = 0; i < x86_pmu.num_counters; i++) { |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 480 | if (!reserve_evntsel_nmi(x86_pmu_config_addr(i))) |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 481 | goto eventsel_fail; |
| 482 | } |
| 483 | |
| 484 | return true; |
| 485 | |
| 486 | eventsel_fail: |
| 487 | for (i--; i >= 0; i--) |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 488 | release_evntsel_nmi(x86_pmu_config_addr(i)); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 489 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 490 | i = x86_pmu.num_counters; |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 491 | |
| 492 | perfctr_fail: |
| 493 | for (i--; i >= 0; i--) |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 494 | release_perfctr_nmi(x86_pmu_event_addr(i)); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 495 | |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 496 | return false; |
| 497 | } |
| 498 | |
| 499 | static void release_pmc_hardware(void) |
| 500 | { |
| 501 | int i; |
| 502 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 503 | for (i = 0; i < x86_pmu.num_counters; i++) { |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 504 | release_perfctr_nmi(x86_pmu_event_addr(i)); |
| 505 | release_evntsel_nmi(x86_pmu_config_addr(i)); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 506 | } |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 507 | } |
| 508 | |
Robert Richter | b27ea29 | 2010-03-17 12:49:10 +0100 | [diff] [blame] | 509 | #else |
| 510 | |
| 511 | static bool reserve_pmc_hardware(void) { return true; } |
| 512 | static void release_pmc_hardware(void) {} |
| 513 | |
| 514 | #endif |
| 515 | |
Don Zickus | 33c6d6a | 2010-11-22 16:55:23 -0500 | [diff] [blame] | 516 | static bool check_hw_exists(void) |
| 517 | { |
| 518 | u64 val, val_new = 0; |
Peter Zijlstra | 4407204 | 2010-12-08 15:56:23 +0100 | [diff] [blame] | 519 | int i, reg, ret = 0; |
Don Zickus | 33c6d6a | 2010-11-22 16:55:23 -0500 | [diff] [blame] | 520 | |
Peter Zijlstra | 4407204 | 2010-12-08 15:56:23 +0100 | [diff] [blame] | 521 | /* |
| 522 | * Check to see if the BIOS enabled any of the counters, if so |
| 523 | * complain and bail. |
| 524 | */ |
| 525 | for (i = 0; i < x86_pmu.num_counters; i++) { |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 526 | reg = x86_pmu_config_addr(i); |
Peter Zijlstra | 4407204 | 2010-12-08 15:56:23 +0100 | [diff] [blame] | 527 | ret = rdmsrl_safe(reg, &val); |
| 528 | if (ret) |
| 529 | goto msr_fail; |
| 530 | if (val & ARCH_PERFMON_EVENTSEL_ENABLE) |
| 531 | goto bios_fail; |
| 532 | } |
| 533 | |
| 534 | if (x86_pmu.num_counters_fixed) { |
| 535 | reg = MSR_ARCH_PERFMON_FIXED_CTR_CTRL; |
| 536 | ret = rdmsrl_safe(reg, &val); |
| 537 | if (ret) |
| 538 | goto msr_fail; |
| 539 | for (i = 0; i < x86_pmu.num_counters_fixed; i++) { |
| 540 | if (val & (0x03 << i*4)) |
| 541 | goto bios_fail; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | * Now write a value and read it back to see if it matches, |
| 547 | * this is needed to detect certain hardware emulators (qemu/kvm) |
| 548 | * that don't trap on the MSR access and always return 0s. |
| 549 | */ |
Don Zickus | 33c6d6a | 2010-11-22 16:55:23 -0500 | [diff] [blame] | 550 | val = 0xabcdUL; |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 551 | ret = checking_wrmsrl(x86_pmu_event_addr(0), val); |
| 552 | ret |= rdmsrl_safe(x86_pmu_event_addr(0), &val_new); |
Don Zickus | 33c6d6a | 2010-11-22 16:55:23 -0500 | [diff] [blame] | 553 | if (ret || val != val_new) |
Peter Zijlstra | 4407204 | 2010-12-08 15:56:23 +0100 | [diff] [blame] | 554 | goto msr_fail; |
Don Zickus | 33c6d6a | 2010-11-22 16:55:23 -0500 | [diff] [blame] | 555 | |
| 556 | return true; |
Peter Zijlstra | 4407204 | 2010-12-08 15:56:23 +0100 | [diff] [blame] | 557 | |
| 558 | bios_fail: |
Ingo Molnar | 45daae5 | 2011-03-25 10:24:23 +0100 | [diff] [blame] | 559 | /* |
| 560 | * We still allow the PMU driver to operate: |
| 561 | */ |
| 562 | printk(KERN_CONT "Broken BIOS detected, complain to your hardware vendor.\n"); |
Peter Zijlstra | 4407204 | 2010-12-08 15:56:23 +0100 | [diff] [blame] | 563 | printk(KERN_ERR FW_BUG "the BIOS has corrupted hw-PMU resources (MSR %x is %Lx)\n", reg, val); |
Ingo Molnar | 45daae5 | 2011-03-25 10:24:23 +0100 | [diff] [blame] | 564 | |
| 565 | return true; |
Peter Zijlstra | 4407204 | 2010-12-08 15:56:23 +0100 | [diff] [blame] | 566 | |
| 567 | msr_fail: |
| 568 | printk(KERN_CONT "Broken PMU hardware detected, using software events only.\n"); |
Ingo Molnar | 45daae5 | 2011-03-25 10:24:23 +0100 | [diff] [blame] | 569 | |
Peter Zijlstra | 4407204 | 2010-12-08 15:56:23 +0100 | [diff] [blame] | 570 | return false; |
Don Zickus | 33c6d6a | 2010-11-22 16:55:23 -0500 | [diff] [blame] | 571 | } |
| 572 | |
Peter Zijlstra | f80c9e3 | 2010-10-19 14:50:02 +0200 | [diff] [blame] | 573 | static void reserve_ds_buffers(void); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 574 | static void release_ds_buffers(void); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 575 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 576 | static void hw_perf_event_destroy(struct perf_event *event) |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 577 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 578 | if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) { |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 579 | release_pmc_hardware(); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 580 | release_ds_buffers(); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 581 | mutex_unlock(&pmc_reserve_mutex); |
| 582 | } |
| 583 | } |
| 584 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 585 | static inline int x86_pmu_initialized(void) |
| 586 | { |
| 587 | return x86_pmu.handle_irq != NULL; |
| 588 | } |
| 589 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 590 | static inline int |
Andi Kleen | e994d7d | 2011-03-03 10:34:48 +0800 | [diff] [blame] | 591 | set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event *event) |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 592 | { |
Andi Kleen | e994d7d | 2011-03-03 10:34:48 +0800 | [diff] [blame] | 593 | struct perf_event_attr *attr = &event->attr; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 594 | unsigned int cache_type, cache_op, cache_result; |
| 595 | u64 config, val; |
| 596 | |
| 597 | config = attr->config; |
| 598 | |
| 599 | cache_type = (config >> 0) & 0xff; |
| 600 | if (cache_type >= PERF_COUNT_HW_CACHE_MAX) |
| 601 | return -EINVAL; |
| 602 | |
| 603 | cache_op = (config >> 8) & 0xff; |
| 604 | if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX) |
| 605 | return -EINVAL; |
| 606 | |
| 607 | cache_result = (config >> 16) & 0xff; |
| 608 | if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX) |
| 609 | return -EINVAL; |
| 610 | |
| 611 | val = hw_cache_event_ids[cache_type][cache_op][cache_result]; |
| 612 | |
| 613 | if (val == 0) |
| 614 | return -ENOENT; |
| 615 | |
| 616 | if (val == -1) |
| 617 | return -EINVAL; |
| 618 | |
| 619 | hwc->config |= val; |
Andi Kleen | e994d7d | 2011-03-03 10:34:48 +0800 | [diff] [blame] | 620 | attr->config1 = hw_cache_extra_regs[cache_type][cache_op][cache_result]; |
| 621 | return x86_pmu_extra_regs(val, event); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 622 | } |
| 623 | |
Robert Richter | c1726f3 | 2010-04-13 22:23:11 +0200 | [diff] [blame] | 624 | static int x86_setup_perfctr(struct perf_event *event) |
| 625 | { |
| 626 | struct perf_event_attr *attr = &event->attr; |
| 627 | struct hw_perf_event *hwc = &event->hw; |
| 628 | u64 config; |
| 629 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 630 | if (!is_sampling_event(event)) { |
Robert Richter | c1726f3 | 2010-04-13 22:23:11 +0200 | [diff] [blame] | 631 | hwc->sample_period = x86_pmu.max_period; |
| 632 | hwc->last_period = hwc->sample_period; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 633 | local64_set(&hwc->period_left, hwc->sample_period); |
Robert Richter | c1726f3 | 2010-04-13 22:23:11 +0200 | [diff] [blame] | 634 | } else { |
| 635 | /* |
| 636 | * If we have a PMU initialized but no APIC |
| 637 | * interrupts, we cannot sample hardware |
| 638 | * events (user-space has to fall back and |
| 639 | * sample via a hrtimer based software event): |
| 640 | */ |
| 641 | if (!x86_pmu.apic) |
| 642 | return -EOPNOTSUPP; |
| 643 | } |
| 644 | |
Ingo Molnar | b52c55c | 2011-04-22 08:44:38 +0200 | [diff] [blame] | 645 | /* |
| 646 | * Do not allow config1 (extended registers) to propagate, |
| 647 | * there's no sane user-space generalization yet: |
| 648 | */ |
Robert Richter | c1726f3 | 2010-04-13 22:23:11 +0200 | [diff] [blame] | 649 | if (attr->type == PERF_TYPE_RAW) |
Ingo Molnar | b52c55c | 2011-04-22 08:44:38 +0200 | [diff] [blame] | 650 | return 0; |
Robert Richter | c1726f3 | 2010-04-13 22:23:11 +0200 | [diff] [blame] | 651 | |
| 652 | if (attr->type == PERF_TYPE_HW_CACHE) |
Andi Kleen | e994d7d | 2011-03-03 10:34:48 +0800 | [diff] [blame] | 653 | return set_ext_hw_attr(hwc, event); |
Robert Richter | c1726f3 | 2010-04-13 22:23:11 +0200 | [diff] [blame] | 654 | |
| 655 | if (attr->config >= x86_pmu.max_events) |
| 656 | return -EINVAL; |
| 657 | |
| 658 | /* |
| 659 | * The generic map: |
| 660 | */ |
| 661 | config = x86_pmu.event_map(attr->config); |
| 662 | |
| 663 | if (config == 0) |
| 664 | return -ENOENT; |
| 665 | |
| 666 | if (config == -1LL) |
| 667 | return -EINVAL; |
| 668 | |
| 669 | /* |
| 670 | * Branch tracing: |
| 671 | */ |
Peter Zijlstra | 18a073a | 2011-04-26 13:24:33 +0200 | [diff] [blame] | 672 | if (attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS && |
| 673 | !attr->freq && hwc->sample_period == 1) { |
Robert Richter | c1726f3 | 2010-04-13 22:23:11 +0200 | [diff] [blame] | 674 | /* BTS is not supported by this architecture. */ |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 675 | if (!x86_pmu.bts_active) |
Robert Richter | c1726f3 | 2010-04-13 22:23:11 +0200 | [diff] [blame] | 676 | return -EOPNOTSUPP; |
| 677 | |
| 678 | /* BTS is currently only allowed for user-mode. */ |
| 679 | if (!attr->exclude_kernel) |
| 680 | return -EOPNOTSUPP; |
| 681 | } |
| 682 | |
| 683 | hwc->config |= config; |
| 684 | |
| 685 | return 0; |
| 686 | } |
Robert Richter | 4261e0e | 2010-04-13 22:23:10 +0200 | [diff] [blame] | 687 | |
Peter Zijlstra | b4cdc5c | 2010-03-30 17:00:06 +0200 | [diff] [blame] | 688 | static int x86_pmu_hw_config(struct perf_event *event) |
Cyrill Gorcunov | a072738 | 2010-03-11 19:54:39 +0300 | [diff] [blame] | 689 | { |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 690 | if (event->attr.precise_ip) { |
| 691 | int precise = 0; |
| 692 | |
| 693 | /* Support for constant skid */ |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 694 | if (x86_pmu.pebs_active) { |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 695 | precise++; |
| 696 | |
Peter Zijlstra | 5553be2 | 2010-10-19 14:38:11 +0200 | [diff] [blame] | 697 | /* Support for IP fixup */ |
| 698 | if (x86_pmu.lbr_nr) |
| 699 | precise++; |
| 700 | } |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 701 | |
| 702 | if (event->attr.precise_ip > precise) |
| 703 | return -EOPNOTSUPP; |
| 704 | } |
| 705 | |
Cyrill Gorcunov | a072738 | 2010-03-11 19:54:39 +0300 | [diff] [blame] | 706 | /* |
| 707 | * Generate PMC IRQs: |
| 708 | * (keep 'enabled' bit clear for now) |
| 709 | */ |
Peter Zijlstra | b4cdc5c | 2010-03-30 17:00:06 +0200 | [diff] [blame] | 710 | event->hw.config = ARCH_PERFMON_EVENTSEL_INT; |
Cyrill Gorcunov | a072738 | 2010-03-11 19:54:39 +0300 | [diff] [blame] | 711 | |
| 712 | /* |
| 713 | * Count user and OS events unless requested not to |
| 714 | */ |
Peter Zijlstra | b4cdc5c | 2010-03-30 17:00:06 +0200 | [diff] [blame] | 715 | if (!event->attr.exclude_user) |
| 716 | event->hw.config |= ARCH_PERFMON_EVENTSEL_USR; |
| 717 | if (!event->attr.exclude_kernel) |
| 718 | event->hw.config |= ARCH_PERFMON_EVENTSEL_OS; |
| 719 | |
| 720 | if (event->attr.type == PERF_TYPE_RAW) |
| 721 | event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK; |
Cyrill Gorcunov | a072738 | 2010-03-11 19:54:39 +0300 | [diff] [blame] | 722 | |
Robert Richter | 9d0fcba6 | 2010-04-13 22:23:12 +0200 | [diff] [blame] | 723 | return x86_setup_perfctr(event); |
Cyrill Gorcunov | a072738 | 2010-03-11 19:54:39 +0300 | [diff] [blame] | 724 | } |
| 725 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 726 | /* |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame] | 727 | * Setup the hardware configuration for a given attr_type |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 728 | */ |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 729 | static int __x86_pmu_event_init(struct perf_event *event) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 730 | { |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 731 | int err; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 732 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 733 | if (!x86_pmu_initialized()) |
| 734 | return -ENODEV; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 735 | |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 736 | err = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 737 | if (!atomic_inc_not_zero(&active_events)) { |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 738 | mutex_lock(&pmc_reserve_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 739 | if (atomic_read(&active_events) == 0) { |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 740 | if (!reserve_pmc_hardware()) |
| 741 | err = -EBUSY; |
Peter Zijlstra | f80c9e3 | 2010-10-19 14:50:02 +0200 | [diff] [blame] | 742 | else |
| 743 | reserve_ds_buffers(); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 744 | } |
| 745 | if (!err) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 746 | atomic_inc(&active_events); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 747 | mutex_unlock(&pmc_reserve_mutex); |
| 748 | } |
| 749 | if (err) |
| 750 | return err; |
| 751 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 752 | event->destroy = hw_perf_event_destroy; |
Peter Zijlstra | a1792cdac | 2009-09-09 10:04:47 +0200 | [diff] [blame] | 753 | |
Robert Richter | 4261e0e | 2010-04-13 22:23:10 +0200 | [diff] [blame] | 754 | event->hw.idx = -1; |
| 755 | event->hw.last_cpu = -1; |
| 756 | event->hw.last_tag = ~0ULL; |
Stephane Eranian | b690081 | 2009-10-06 16:42:09 +0200 | [diff] [blame] | 757 | |
Stephane Eranian | efc9f05 | 2011-06-06 16:57:03 +0200 | [diff] [blame^] | 758 | /* mark unused */ |
| 759 | event->hw.extra_reg.idx = EXTRA_REG_NONE; |
| 760 | |
Robert Richter | 9d0fcba6 | 2010-04-13 22:23:12 +0200 | [diff] [blame] | 761 | return x86_pmu.hw_config(event); |
Robert Richter | 4261e0e | 2010-04-13 22:23:10 +0200 | [diff] [blame] | 762 | } |
| 763 | |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 764 | static void x86_pmu_disable_all(void) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 765 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 766 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 767 | int idx; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 768 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 769 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 770 | u64 val; |
| 771 | |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 772 | if (!test_bit(idx, cpuc->active_mask)) |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 773 | continue; |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 774 | rdmsrl(x86_pmu_config_addr(idx), val); |
Robert Richter | bb1165d | 2010-03-01 14:21:23 +0100 | [diff] [blame] | 775 | if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE)) |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 776 | continue; |
Robert Richter | bb1165d | 2010-03-01 14:21:23 +0100 | [diff] [blame] | 777 | val &= ~ARCH_PERFMON_EVENTSEL_ENABLE; |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 778 | wrmsrl(x86_pmu_config_addr(idx), val); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 779 | } |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 780 | } |
| 781 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 782 | static void x86_pmu_disable(struct pmu *pmu) |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 783 | { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 784 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 785 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 786 | if (!x86_pmu_initialized()) |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 787 | return; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 788 | |
Peter Zijlstra | 1a6e21f | 2010-01-27 23:07:47 +0100 | [diff] [blame] | 789 | if (!cpuc->enabled) |
| 790 | return; |
| 791 | |
| 792 | cpuc->n_added = 0; |
| 793 | cpuc->enabled = 0; |
| 794 | barrier(); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 795 | |
| 796 | x86_pmu.disable_all(); |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 797 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 798 | |
Robert Richter | d45dd92 | 2011-02-02 17:40:56 +0100 | [diff] [blame] | 799 | static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc, |
| 800 | u64 enable_mask) |
| 801 | { |
Stephane Eranian | efc9f05 | 2011-06-06 16:57:03 +0200 | [diff] [blame^] | 802 | if (hwc->extra_reg.reg) |
| 803 | wrmsrl(hwc->extra_reg.reg, hwc->extra_reg.config); |
Robert Richter | 73d6e52 | 2011-02-02 17:40:59 +0100 | [diff] [blame] | 804 | wrmsrl(hwc->config_base, hwc->config | enable_mask); |
Robert Richter | d45dd92 | 2011-02-02 17:40:56 +0100 | [diff] [blame] | 805 | } |
| 806 | |
Peter Zijlstra | 11164cd | 2010-03-26 14:08:44 +0100 | [diff] [blame] | 807 | static void x86_pmu_enable_all(int added) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 808 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 809 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 810 | int idx; |
| 811 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 812 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
Robert Richter | d45dd92 | 2011-02-02 17:40:56 +0100 | [diff] [blame] | 813 | struct hw_perf_event *hwc = &cpuc->events[idx]->hw; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 814 | |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 815 | if (!test_bit(idx, cpuc->active_mask)) |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 816 | continue; |
Peter Zijlstra | 984b838 | 2009-07-10 09:59:56 +0200 | [diff] [blame] | 817 | |
Robert Richter | d45dd92 | 2011-02-02 17:40:56 +0100 | [diff] [blame] | 818 | __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 822 | static struct pmu pmu; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 823 | |
| 824 | static inline int is_x86_event(struct perf_event *event) |
| 825 | { |
| 826 | return event->pmu == &pmu; |
| 827 | } |
| 828 | |
| 829 | static int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign) |
| 830 | { |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 831 | struct event_constraint *c, *constraints[X86_PMC_IDX_MAX]; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 832 | unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
Peter Zijlstra | c933c1a | 2010-01-22 16:40:12 +0100 | [diff] [blame] | 833 | int i, j, w, wmax, num = 0; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 834 | struct hw_perf_event *hwc; |
| 835 | |
| 836 | bitmap_zero(used_mask, X86_PMC_IDX_MAX); |
| 837 | |
| 838 | for (i = 0; i < n; i++) { |
Peter Zijlstra | b622d64 | 2010-02-01 15:36:30 +0100 | [diff] [blame] | 839 | c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]); |
| 840 | constraints[i] = c; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | /* |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 844 | * fastpath, try to reuse previous register |
| 845 | */ |
Peter Zijlstra | c933c1a | 2010-01-22 16:40:12 +0100 | [diff] [blame] | 846 | for (i = 0; i < n; i++) { |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 847 | hwc = &cpuc->event_list[i]->hw; |
Peter Zijlstra | 81269a0 | 2010-01-22 14:55:22 +0100 | [diff] [blame] | 848 | c = constraints[i]; |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 849 | |
| 850 | /* never assigned */ |
| 851 | if (hwc->idx == -1) |
| 852 | break; |
| 853 | |
| 854 | /* constraint still honored */ |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 855 | if (!test_bit(hwc->idx, c->idxmsk)) |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 856 | break; |
| 857 | |
| 858 | /* not already used */ |
| 859 | if (test_bit(hwc->idx, used_mask)) |
| 860 | break; |
| 861 | |
Peter Zijlstra | 34538ee | 2010-03-02 21:16:55 +0100 | [diff] [blame] | 862 | __set_bit(hwc->idx, used_mask); |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 863 | if (assign) |
| 864 | assign[i] = hwc->idx; |
| 865 | } |
Peter Zijlstra | c933c1a | 2010-01-22 16:40:12 +0100 | [diff] [blame] | 866 | if (i == n) |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 867 | goto done; |
| 868 | |
| 869 | /* |
| 870 | * begin slow path |
| 871 | */ |
| 872 | |
| 873 | bitmap_zero(used_mask, X86_PMC_IDX_MAX); |
| 874 | |
| 875 | /* |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 876 | * weight = number of possible counters |
| 877 | * |
| 878 | * 1 = most constrained, only works on one counter |
| 879 | * wmax = least constrained, works on any counter |
| 880 | * |
| 881 | * assign events to counters starting with most |
| 882 | * constrained events. |
| 883 | */ |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 884 | wmax = x86_pmu.num_counters; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 885 | |
| 886 | /* |
| 887 | * when fixed event counters are present, |
| 888 | * wmax is incremented by 1 to account |
| 889 | * for one more choice |
| 890 | */ |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 891 | if (x86_pmu.num_counters_fixed) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 892 | wmax++; |
| 893 | |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 894 | for (w = 1, num = n; num && w <= wmax; w++) { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 895 | /* for each event */ |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 896 | for (i = 0; num && i < n; i++) { |
Peter Zijlstra | 81269a0 | 2010-01-22 14:55:22 +0100 | [diff] [blame] | 897 | c = constraints[i]; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 898 | hwc = &cpuc->event_list[i]->hw; |
| 899 | |
Peter Zijlstra | 272d30b | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 900 | if (c->weight != w) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 901 | continue; |
| 902 | |
Akinobu Mita | 984b3f5 | 2010-03-05 13:41:37 -0800 | [diff] [blame] | 903 | for_each_set_bit(j, c->idxmsk, X86_PMC_IDX_MAX) { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 904 | if (!test_bit(j, used_mask)) |
| 905 | break; |
| 906 | } |
| 907 | |
| 908 | if (j == X86_PMC_IDX_MAX) |
| 909 | break; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 910 | |
Peter Zijlstra | 34538ee | 2010-03-02 21:16:55 +0100 | [diff] [blame] | 911 | __set_bit(j, used_mask); |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 912 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 913 | if (assign) |
| 914 | assign[i] = j; |
| 915 | num--; |
| 916 | } |
| 917 | } |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 918 | done: |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 919 | /* |
| 920 | * scheduling failed or is just a simulation, |
| 921 | * free resources if necessary |
| 922 | */ |
| 923 | if (!assign || num) { |
| 924 | for (i = 0; i < n; i++) { |
| 925 | if (x86_pmu.put_event_constraints) |
| 926 | x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]); |
| 927 | } |
| 928 | } |
| 929 | return num ? -ENOSPC : 0; |
| 930 | } |
| 931 | |
| 932 | /* |
| 933 | * dogrp: true if must collect siblings events (group) |
| 934 | * returns total number of events and error code |
| 935 | */ |
| 936 | static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp) |
| 937 | { |
| 938 | struct perf_event *event; |
| 939 | int n, max_count; |
| 940 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 941 | max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 942 | |
| 943 | /* current number of events already accepted */ |
| 944 | n = cpuc->n_events; |
| 945 | |
| 946 | if (is_x86_event(leader)) { |
| 947 | if (n >= max_count) |
| 948 | return -ENOSPC; |
| 949 | cpuc->event_list[n] = leader; |
| 950 | n++; |
| 951 | } |
| 952 | if (!dogrp) |
| 953 | return n; |
| 954 | |
| 955 | list_for_each_entry(event, &leader->sibling_list, group_entry) { |
| 956 | if (!is_x86_event(event) || |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 957 | event->state <= PERF_EVENT_STATE_OFF) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 958 | continue; |
| 959 | |
| 960 | if (n >= max_count) |
| 961 | return -ENOSPC; |
| 962 | |
| 963 | cpuc->event_list[n] = event; |
| 964 | n++; |
| 965 | } |
| 966 | return n; |
| 967 | } |
| 968 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 969 | static inline void x86_assign_hw_event(struct perf_event *event, |
Stephane Eranian | 447a194 | 2010-02-01 14:50:01 +0200 | [diff] [blame] | 970 | struct cpu_hw_events *cpuc, int i) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 971 | { |
Stephane Eranian | 447a194 | 2010-02-01 14:50:01 +0200 | [diff] [blame] | 972 | struct hw_perf_event *hwc = &event->hw; |
| 973 | |
| 974 | hwc->idx = cpuc->assign[i]; |
| 975 | hwc->last_cpu = smp_processor_id(); |
| 976 | hwc->last_tag = ++cpuc->tags[i]; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 977 | |
| 978 | if (hwc->idx == X86_PMC_IDX_FIXED_BTS) { |
| 979 | hwc->config_base = 0; |
| 980 | hwc->event_base = 0; |
| 981 | } else if (hwc->idx >= X86_PMC_IDX_FIXED) { |
| 982 | hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL; |
Stephane Eranian | fc66c52 | 2011-03-19 18:20:05 +0100 | [diff] [blame] | 983 | hwc->event_base = MSR_ARCH_PERFMON_FIXED_CTR0 + (hwc->idx - X86_PMC_IDX_FIXED); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 984 | } else { |
Robert Richter | 73d6e52 | 2011-02-02 17:40:59 +0100 | [diff] [blame] | 985 | hwc->config_base = x86_pmu_config_addr(hwc->idx); |
| 986 | hwc->event_base = x86_pmu_event_addr(hwc->idx); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | |
Stephane Eranian | 447a194 | 2010-02-01 14:50:01 +0200 | [diff] [blame] | 990 | static inline int match_prev_assignment(struct hw_perf_event *hwc, |
| 991 | struct cpu_hw_events *cpuc, |
| 992 | int i) |
| 993 | { |
| 994 | return hwc->idx == cpuc->assign[i] && |
| 995 | hwc->last_cpu == smp_processor_id() && |
| 996 | hwc->last_tag == cpuc->tags[i]; |
| 997 | } |
| 998 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 999 | static void x86_pmu_start(struct perf_event *event, int flags); |
| 1000 | static void x86_pmu_stop(struct perf_event *event, int flags); |
Peter Zijlstra | 2e84187 | 2010-01-25 15:58:43 +0100 | [diff] [blame] | 1001 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1002 | static void x86_pmu_enable(struct pmu *pmu) |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1003 | { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1004 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 1005 | struct perf_event *event; |
| 1006 | struct hw_perf_event *hwc; |
Peter Zijlstra | 11164cd | 2010-03-26 14:08:44 +0100 | [diff] [blame] | 1007 | int i, added = cpuc->n_added; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1008 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 1009 | if (!x86_pmu_initialized()) |
Ingo Molnar | 2b9ff0d | 2008-12-14 18:36:30 +0100 | [diff] [blame] | 1010 | return; |
Peter Zijlstra | 1a6e21f | 2010-01-27 23:07:47 +0100 | [diff] [blame] | 1011 | |
| 1012 | if (cpuc->enabled) |
| 1013 | return; |
| 1014 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1015 | if (cpuc->n_added) { |
Peter Zijlstra | 19925ce | 2010-03-06 13:20:40 +0100 | [diff] [blame] | 1016 | int n_running = cpuc->n_events - cpuc->n_added; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1017 | /* |
| 1018 | * apply assignment obtained either from |
| 1019 | * hw_perf_group_sched_in() or x86_pmu_enable() |
| 1020 | * |
| 1021 | * step1: save events moving to new counters |
| 1022 | * step2: reprogram moved events into new counters |
| 1023 | */ |
Peter Zijlstra | 19925ce | 2010-03-06 13:20:40 +0100 | [diff] [blame] | 1024 | for (i = 0; i < n_running; i++) { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1025 | event = cpuc->event_list[i]; |
| 1026 | hwc = &event->hw; |
| 1027 | |
Stephane Eranian | 447a194 | 2010-02-01 14:50:01 +0200 | [diff] [blame] | 1028 | /* |
| 1029 | * we can avoid reprogramming counter if: |
| 1030 | * - assigned same counter as last time |
| 1031 | * - running on same CPU as last time |
| 1032 | * - no other event has used the counter since |
| 1033 | */ |
| 1034 | if (hwc->idx == -1 || |
| 1035 | match_prev_assignment(hwc, cpuc, i)) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1036 | continue; |
| 1037 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1038 | /* |
| 1039 | * Ensure we don't accidentally enable a stopped |
| 1040 | * counter simply because we rescheduled. |
| 1041 | */ |
| 1042 | if (hwc->state & PERF_HES_STOPPED) |
| 1043 | hwc->state |= PERF_HES_ARCH; |
| 1044 | |
| 1045 | x86_pmu_stop(event, PERF_EF_UPDATE); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | for (i = 0; i < cpuc->n_events; i++) { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1049 | event = cpuc->event_list[i]; |
| 1050 | hwc = &event->hw; |
| 1051 | |
Peter Zijlstra | 45e16a6 | 2010-03-11 13:40:30 +0100 | [diff] [blame] | 1052 | if (!match_prev_assignment(hwc, cpuc, i)) |
Stephane Eranian | 447a194 | 2010-02-01 14:50:01 +0200 | [diff] [blame] | 1053 | x86_assign_hw_event(event, cpuc, i); |
Peter Zijlstra | 45e16a6 | 2010-03-11 13:40:30 +0100 | [diff] [blame] | 1054 | else if (i < n_running) |
| 1055 | continue; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1056 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1057 | if (hwc->state & PERF_HES_ARCH) |
| 1058 | continue; |
| 1059 | |
| 1060 | x86_pmu_start(event, PERF_EF_RELOAD); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1061 | } |
| 1062 | cpuc->n_added = 0; |
| 1063 | perf_events_lapic_init(); |
| 1064 | } |
Peter Zijlstra | 1a6e21f | 2010-01-27 23:07:47 +0100 | [diff] [blame] | 1065 | |
| 1066 | cpuc->enabled = 1; |
| 1067 | barrier(); |
| 1068 | |
Peter Zijlstra | 11164cd | 2010-03-26 14:08:44 +0100 | [diff] [blame] | 1069 | x86_pmu.enable_all(added); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1070 | } |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1071 | |
Peter Zijlstra | aff3d91 | 2010-03-02 20:32:08 +0100 | [diff] [blame] | 1072 | static inline void x86_pmu_disable_event(struct perf_event *event) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1073 | { |
Peter Zijlstra | aff3d91 | 2010-03-02 20:32:08 +0100 | [diff] [blame] | 1074 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | 7645a24 | 2010-03-08 13:51:31 +0100 | [diff] [blame] | 1075 | |
Robert Richter | 73d6e52 | 2011-02-02 17:40:59 +0100 | [diff] [blame] | 1076 | wrmsrl(hwc->config_base, hwc->config); |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1077 | } |
| 1078 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 1079 | static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1080 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1081 | /* |
| 1082 | * Set the next IRQ period, based on the hwc->period_left value. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1083 | * To be called with the event disabled in hw: |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1084 | */ |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1085 | static int |
Peter Zijlstra | 07088ed | 2010-03-02 20:16:01 +0100 | [diff] [blame] | 1086 | x86_perf_event_set_period(struct perf_event *event) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1087 | { |
Peter Zijlstra | 07088ed | 2010-03-02 20:16:01 +0100 | [diff] [blame] | 1088 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1089 | s64 left = local64_read(&hwc->period_left); |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1090 | s64 period = hwc->sample_period; |
Peter Zijlstra | 7645a24 | 2010-03-08 13:51:31 +0100 | [diff] [blame] | 1091 | int ret = 0, idx = hwc->idx; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1092 | |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1093 | if (idx == X86_PMC_IDX_FIXED_BTS) |
| 1094 | return 0; |
| 1095 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1096 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1097 | * If we are way outside a reasonable range then just skip forward: |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1098 | */ |
| 1099 | if (unlikely(left <= -period)) { |
| 1100 | left = period; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1101 | local64_set(&hwc->period_left, left); |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1102 | hwc->last_period = period; |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1103 | ret = 1; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | if (unlikely(left <= 0)) { |
| 1107 | left += period; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1108 | local64_set(&hwc->period_left, left); |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1109 | hwc->last_period = period; |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1110 | ret = 1; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1111 | } |
Ingo Molnar | 1c80f4b | 2009-05-15 08:25:22 +0200 | [diff] [blame] | 1112 | /* |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 1113 | * Quirk: certain CPUs dont like it if just 1 hw_event is left: |
Ingo Molnar | 1c80f4b | 2009-05-15 08:25:22 +0200 | [diff] [blame] | 1114 | */ |
| 1115 | if (unlikely(left < 2)) |
| 1116 | left = 2; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1117 | |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1118 | if (left > x86_pmu.max_period) |
| 1119 | left = x86_pmu.max_period; |
| 1120 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 1121 | per_cpu(pmc_prev_left[idx], smp_processor_id()) = left; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1122 | |
| 1123 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1124 | * The hw event starts counting from this event offset, |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1125 | * mark it to be able to extra future deltas: |
| 1126 | */ |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1127 | local64_set(&hwc->prev_count, (u64)-left); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1128 | |
Robert Richter | 73d6e52 | 2011-02-02 17:40:59 +0100 | [diff] [blame] | 1129 | wrmsrl(hwc->event_base, (u64)(-left) & x86_pmu.cntval_mask); |
Cyrill Gorcunov | 68aa00a | 2010-06-03 01:23:04 +0400 | [diff] [blame] | 1130 | |
| 1131 | /* |
| 1132 | * Due to erratum on certan cpu we need |
| 1133 | * a second write to be sure the register |
| 1134 | * is updated properly |
| 1135 | */ |
| 1136 | if (x86_pmu.perfctr_second_write) { |
Robert Richter | 73d6e52 | 2011-02-02 17:40:59 +0100 | [diff] [blame] | 1137 | wrmsrl(hwc->event_base, |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1138 | (u64)(-left) & x86_pmu.cntval_mask); |
Cyrill Gorcunov | 68aa00a | 2010-06-03 01:23:04 +0400 | [diff] [blame] | 1139 | } |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1140 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1141 | perf_event_update_userpage(event); |
Peter Zijlstra | 194002b | 2009-06-22 16:35:24 +0200 | [diff] [blame] | 1142 | |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1143 | return ret; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1144 | } |
| 1145 | |
Peter Zijlstra | aff3d91 | 2010-03-02 20:32:08 +0100 | [diff] [blame] | 1146 | static void x86_pmu_enable_event(struct perf_event *event) |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 1147 | { |
Tejun Heo | 0a3aee0 | 2010-12-18 16:28:55 +0100 | [diff] [blame] | 1148 | if (__this_cpu_read(cpu_hw_events.enabled)) |
Robert Richter | 31fa58a | 2010-04-13 22:23:14 +0200 | [diff] [blame] | 1149 | __x86_pmu_enable_event(&event->hw, |
| 1150 | ARCH_PERFMON_EVENTSEL_ENABLE); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1151 | } |
| 1152 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1153 | /* |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1154 | * Add a single event to the PMU. |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1155 | * |
| 1156 | * The event is added to the group of enabled events |
| 1157 | * but only if it can be scehduled with existing events. |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1158 | */ |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1159 | static int x86_pmu_add(struct perf_event *event, int flags) |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1160 | { |
| 1161 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1162 | struct hw_perf_event *hwc; |
| 1163 | int assign[X86_PMC_IDX_MAX]; |
| 1164 | int n, n0, ret; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1165 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1166 | hwc = &event->hw; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1167 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1168 | perf_pmu_disable(event->pmu); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1169 | n0 = cpuc->n_events; |
Peter Zijlstra | 24cd7f5 | 2010-06-11 17:32:03 +0200 | [diff] [blame] | 1170 | ret = n = collect_events(cpuc, event, false); |
| 1171 | if (ret < 0) |
| 1172 | goto out; |
Ingo Molnar | 53b441a | 2009-05-25 21:41:28 +0200 | [diff] [blame] | 1173 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1174 | hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED; |
| 1175 | if (!(flags & PERF_EF_START)) |
| 1176 | hwc->state |= PERF_HES_ARCH; |
| 1177 | |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 1178 | /* |
| 1179 | * If group events scheduling transaction was started, |
Lucas De Marchi | 0d2eb44 | 2011-03-17 16:24:16 -0300 | [diff] [blame] | 1180 | * skip the schedulability test here, it will be performed |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1181 | * at commit time (->commit_txn) as a whole |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 1182 | */ |
Peter Zijlstra | 8d2cacb | 2010-05-25 17:49:05 +0200 | [diff] [blame] | 1183 | if (cpuc->group_flag & PERF_EVENT_TXN) |
Peter Zijlstra | 24cd7f5 | 2010-06-11 17:32:03 +0200 | [diff] [blame] | 1184 | goto done_collect; |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 1185 | |
Cyrill Gorcunov | a072738 | 2010-03-11 19:54:39 +0300 | [diff] [blame] | 1186 | ret = x86_pmu.schedule_events(cpuc, n, assign); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1187 | if (ret) |
Peter Zijlstra | 24cd7f5 | 2010-06-11 17:32:03 +0200 | [diff] [blame] | 1188 | goto out; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1189 | /* |
| 1190 | * copy new assignment, now we know it is possible |
| 1191 | * will be used by hw_perf_enable() |
| 1192 | */ |
| 1193 | memcpy(cpuc->assign, assign, n*sizeof(int)); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1194 | |
Peter Zijlstra | 24cd7f5 | 2010-06-11 17:32:03 +0200 | [diff] [blame] | 1195 | done_collect: |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1196 | cpuc->n_events = n; |
Peter Zijlstra | 356e1f2 | 2010-03-06 13:49:56 +0100 | [diff] [blame] | 1197 | cpuc->n_added += n - n0; |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 1198 | cpuc->n_txn += n - n0; |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 1199 | |
Peter Zijlstra | 24cd7f5 | 2010-06-11 17:32:03 +0200 | [diff] [blame] | 1200 | ret = 0; |
| 1201 | out: |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1202 | perf_pmu_enable(event->pmu); |
Peter Zijlstra | 24cd7f5 | 2010-06-11 17:32:03 +0200 | [diff] [blame] | 1203 | return ret; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1204 | } |
| 1205 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1206 | static void x86_pmu_start(struct perf_event *event, int flags) |
Stephane Eranian | d76a081 | 2010-02-08 17:06:01 +0200 | [diff] [blame] | 1207 | { |
Peter Zijlstra | c08053e | 2010-03-06 13:19:24 +0100 | [diff] [blame] | 1208 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 1209 | int idx = event->hw.idx; |
| 1210 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1211 | if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED))) |
| 1212 | return; |
Stephane Eranian | d76a081 | 2010-02-08 17:06:01 +0200 | [diff] [blame] | 1213 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1214 | if (WARN_ON_ONCE(idx == -1)) |
| 1215 | return; |
| 1216 | |
| 1217 | if (flags & PERF_EF_RELOAD) { |
| 1218 | WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE)); |
| 1219 | x86_perf_event_set_period(event); |
| 1220 | } |
| 1221 | |
| 1222 | event->hw.state = 0; |
| 1223 | |
Peter Zijlstra | c08053e | 2010-03-06 13:19:24 +0100 | [diff] [blame] | 1224 | cpuc->events[idx] = event; |
| 1225 | __set_bit(idx, cpuc->active_mask); |
Robert Richter | 63e6be6 | 2010-09-15 18:20:34 +0200 | [diff] [blame] | 1226 | __set_bit(idx, cpuc->running); |
Peter Zijlstra | aff3d91 | 2010-03-02 20:32:08 +0100 | [diff] [blame] | 1227 | x86_pmu.enable(event); |
Peter Zijlstra | c08053e | 2010-03-06 13:19:24 +0100 | [diff] [blame] | 1228 | perf_event_update_userpage(event); |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 1229 | } |
| 1230 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1231 | void perf_event_print_debug(void) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1232 | { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1233 | u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1234 | u64 pebs; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1235 | struct cpu_hw_events *cpuc; |
Peter Zijlstra | 5bb9efe | 2009-05-13 08:12:51 +0200 | [diff] [blame] | 1236 | unsigned long flags; |
Ingo Molnar | 1e12567 | 2008-12-09 12:18:18 +0100 | [diff] [blame] | 1237 | int cpu, idx; |
| 1238 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1239 | if (!x86_pmu.num_counters) |
Ingo Molnar | 1e12567 | 2008-12-09 12:18:18 +0100 | [diff] [blame] | 1240 | return; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1241 | |
Peter Zijlstra | 5bb9efe | 2009-05-13 08:12:51 +0200 | [diff] [blame] | 1242 | local_irq_save(flags); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1243 | |
| 1244 | cpu = smp_processor_id(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1245 | cpuc = &per_cpu(cpu_hw_events, cpu); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1246 | |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 1247 | if (x86_pmu.version >= 2) { |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1248 | rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl); |
| 1249 | rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status); |
| 1250 | rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow); |
| 1251 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1252 | rdmsrl(MSR_IA32_PEBS_ENABLE, pebs); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1253 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1254 | pr_info("\n"); |
| 1255 | pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl); |
| 1256 | pr_info("CPU#%d: status: %016llx\n", cpu, status); |
| 1257 | pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow); |
| 1258 | pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1259 | pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1260 | } |
Peter Zijlstra | 7645a24 | 2010-03-08 13:51:31 +0100 | [diff] [blame] | 1261 | pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1262 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1263 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 1264 | rdmsrl(x86_pmu_config_addr(idx), pmc_ctrl); |
| 1265 | rdmsrl(x86_pmu_event_addr(idx), pmc_count); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1266 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 1267 | prev_left = per_cpu(pmc_prev_left[idx], cpu); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1268 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1269 | pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n", |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1270 | cpu, idx, pmc_ctrl); |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1271 | pr_info("CPU#%d: gen-PMC%d count: %016llx\n", |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1272 | cpu, idx, pmc_count); |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1273 | pr_info("CPU#%d: gen-PMC%d left: %016llx\n", |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1274 | cpu, idx, prev_left); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1275 | } |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1276 | for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1277 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count); |
| 1278 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1279 | pr_info("CPU#%d: fixed-PMC%d count: %016llx\n", |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1280 | cpu, idx, pmc_count); |
| 1281 | } |
Peter Zijlstra | 5bb9efe | 2009-05-13 08:12:51 +0200 | [diff] [blame] | 1282 | local_irq_restore(flags); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1283 | } |
| 1284 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1285 | static void x86_pmu_stop(struct perf_event *event, int flags) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1286 | { |
Stephane Eranian | d76a081 | 2010-02-08 17:06:01 +0200 | [diff] [blame] | 1287 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1288 | struct hw_perf_event *hwc = &event->hw; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1289 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1290 | if (__test_and_clear_bit(hwc->idx, cpuc->active_mask)) { |
| 1291 | x86_pmu.disable(event); |
| 1292 | cpuc->events[hwc->idx] = NULL; |
| 1293 | WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED); |
| 1294 | hwc->state |= PERF_HES_STOPPED; |
| 1295 | } |
Peter Zijlstra | 71e2d28 | 2010-03-08 17:51:33 +0100 | [diff] [blame] | 1296 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1297 | if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) { |
| 1298 | /* |
| 1299 | * Drain the remaining delta count out of a event |
| 1300 | * that we are disabling: |
| 1301 | */ |
| 1302 | x86_perf_event_update(event); |
| 1303 | hwc->state |= PERF_HES_UPTODATE; |
| 1304 | } |
Peter Zijlstra | 2e84187 | 2010-01-25 15:58:43 +0100 | [diff] [blame] | 1305 | } |
| 1306 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1307 | static void x86_pmu_del(struct perf_event *event, int flags) |
Peter Zijlstra | 2e84187 | 2010-01-25 15:58:43 +0100 | [diff] [blame] | 1308 | { |
| 1309 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 1310 | int i; |
| 1311 | |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 1312 | /* |
| 1313 | * If we're called during a txn, we don't need to do anything. |
| 1314 | * The events never got scheduled and ->cancel_txn will truncate |
| 1315 | * the event_list. |
| 1316 | */ |
Peter Zijlstra | 8d2cacb | 2010-05-25 17:49:05 +0200 | [diff] [blame] | 1317 | if (cpuc->group_flag & PERF_EVENT_TXN) |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 1318 | return; |
| 1319 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1320 | x86_pmu_stop(event, PERF_EF_UPDATE); |
Peter Zijlstra | 194002b | 2009-06-22 16:35:24 +0200 | [diff] [blame] | 1321 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1322 | for (i = 0; i < cpuc->n_events; i++) { |
| 1323 | if (event == cpuc->event_list[i]) { |
| 1324 | |
| 1325 | if (x86_pmu.put_event_constraints) |
| 1326 | x86_pmu.put_event_constraints(cpuc, event); |
| 1327 | |
| 1328 | while (++i < cpuc->n_events) |
| 1329 | cpuc->event_list[i-1] = cpuc->event_list[i]; |
| 1330 | |
| 1331 | --cpuc->n_events; |
Peter Zijlstra | 6c9687a | 2010-01-25 11:57:25 +0100 | [diff] [blame] | 1332 | break; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1333 | } |
| 1334 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1335 | perf_event_update_userpage(event); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1336 | } |
| 1337 | |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 1338 | static int x86_pmu_handle_irq(struct pt_regs *regs) |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 1339 | { |
Peter Zijlstra | df1a132 | 2009-06-10 21:02:22 +0200 | [diff] [blame] | 1340 | struct perf_sample_data data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1341 | struct cpu_hw_events *cpuc; |
| 1342 | struct perf_event *event; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1343 | int idx, handled = 0; |
Ingo Molnar | 9029a5e | 2009-05-15 08:26:20 +0200 | [diff] [blame] | 1344 | u64 val; |
| 1345 | |
Peter Zijlstra | dc1d628 | 2010-03-03 15:55:04 +0100 | [diff] [blame] | 1346 | perf_sample_data_init(&data, 0); |
Peter Zijlstra | df1a132 | 2009-06-10 21:02:22 +0200 | [diff] [blame] | 1347 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1348 | cpuc = &__get_cpu_var(cpu_hw_events); |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 1349 | |
Don Zickus | 2bce5da | 2011-04-27 06:32:33 -0400 | [diff] [blame] | 1350 | /* |
| 1351 | * Some chipsets need to unmask the LVTPC in a particular spot |
| 1352 | * inside the nmi handler. As a result, the unmasking was pushed |
| 1353 | * into all the nmi handlers. |
| 1354 | * |
| 1355 | * This generic handler doesn't seem to have any issues where the |
| 1356 | * unmasking occurs so it was left at the top. |
| 1357 | */ |
| 1358 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
| 1359 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1360 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
Robert Richter | 63e6be6 | 2010-09-15 18:20:34 +0200 | [diff] [blame] | 1361 | if (!test_bit(idx, cpuc->active_mask)) { |
| 1362 | /* |
| 1363 | * Though we deactivated the counter some cpus |
| 1364 | * might still deliver spurious interrupts still |
| 1365 | * in flight. Catch them: |
| 1366 | */ |
| 1367 | if (__test_and_clear_bit(idx, cpuc->running)) |
| 1368 | handled++; |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 1369 | continue; |
Robert Richter | 63e6be6 | 2010-09-15 18:20:34 +0200 | [diff] [blame] | 1370 | } |
Peter Zijlstra | 962bf7a | 2009-05-13 13:21:36 +0200 | [diff] [blame] | 1371 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1372 | event = cpuc->events[idx]; |
Peter Zijlstra | a4016a7 | 2009-05-14 14:52:17 +0200 | [diff] [blame] | 1373 | |
Peter Zijlstra | cc2ad4b | 2010-03-02 20:18:39 +0100 | [diff] [blame] | 1374 | val = x86_perf_event_update(event); |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1375 | if (val & (1ULL << (x86_pmu.cntval_bits - 1))) |
Peter Zijlstra | 48e22d5 | 2009-05-25 17:39:04 +0200 | [diff] [blame] | 1376 | continue; |
Peter Zijlstra | 962bf7a | 2009-05-13 13:21:36 +0200 | [diff] [blame] | 1377 | |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1378 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1379 | * event overflow |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1380 | */ |
Robert Richter | 4177c42 | 2010-09-02 15:07:48 -0400 | [diff] [blame] | 1381 | handled++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1382 | data.period = event->hw.last_period; |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1383 | |
Peter Zijlstra | 07088ed | 2010-03-02 20:16:01 +0100 | [diff] [blame] | 1384 | if (!x86_perf_event_set_period(event)) |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1385 | continue; |
| 1386 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 1387 | if (perf_event_overflow(event, &data, regs)) |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1388 | x86_pmu_stop(event, 0); |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 1389 | } |
Peter Zijlstra | 962bf7a | 2009-05-13 13:21:36 +0200 | [diff] [blame] | 1390 | |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1391 | if (handled) |
| 1392 | inc_irq_stat(apic_perf_irqs); |
| 1393 | |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 1394 | return handled; |
| 1395 | } |
Robert Richter | 39d81ea | 2009-04-29 12:47:05 +0200 | [diff] [blame] | 1396 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1397 | void perf_events_lapic_init(void) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1398 | { |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1399 | if (!x86_pmu.apic || !x86_pmu_initialized()) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1400 | return; |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 1401 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1402 | /* |
Yong Wang | c323d95 | 2009-05-29 13:28:35 +0800 | [diff] [blame] | 1403 | * Always use NMI for PMU |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1404 | */ |
Yong Wang | c323d95 | 2009-05-29 13:28:35 +0800 | [diff] [blame] | 1405 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1406 | } |
| 1407 | |
Robert Richter | 4177c42 | 2010-09-02 15:07:48 -0400 | [diff] [blame] | 1408 | struct pmu_nmi_state { |
| 1409 | unsigned int marked; |
| 1410 | int handled; |
| 1411 | }; |
| 1412 | |
| 1413 | static DEFINE_PER_CPU(struct pmu_nmi_state, pmu_nmi); |
| 1414 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1415 | static int __kprobes |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1416 | perf_event_nmi_handler(struct notifier_block *self, |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1417 | unsigned long cmd, void *__args) |
| 1418 | { |
| 1419 | struct die_args *args = __args; |
Robert Richter | 4177c42 | 2010-09-02 15:07:48 -0400 | [diff] [blame] | 1420 | unsigned int this_nmi; |
| 1421 | int handled; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1422 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1423 | if (!atomic_read(&active_events)) |
Peter Zijlstra | 63a809a | 2009-05-01 12:23:17 +0200 | [diff] [blame] | 1424 | return NOTIFY_DONE; |
| 1425 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1426 | switch (cmd) { |
| 1427 | case DIE_NMI: |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1428 | break; |
Robert Richter | 4177c42 | 2010-09-02 15:07:48 -0400 | [diff] [blame] | 1429 | case DIE_NMIUNKNOWN: |
| 1430 | this_nmi = percpu_read(irq_stat.__nmi_count); |
Tejun Heo | 0a3aee0 | 2010-12-18 16:28:55 +0100 | [diff] [blame] | 1431 | if (this_nmi != __this_cpu_read(pmu_nmi.marked)) |
Robert Richter | 4177c42 | 2010-09-02 15:07:48 -0400 | [diff] [blame] | 1432 | /* let the kernel handle the unknown nmi */ |
| 1433 | return NOTIFY_DONE; |
| 1434 | /* |
| 1435 | * This one is a PMU back-to-back nmi. Two events |
| 1436 | * trigger 'simultaneously' raising two back-to-back |
| 1437 | * NMIs. If the first NMI handles both, the latter |
| 1438 | * will be empty and daze the CPU. So, we drop it to |
| 1439 | * avoid false-positive 'unknown nmi' messages. |
| 1440 | */ |
| 1441 | return NOTIFY_STOP; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1442 | default: |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1443 | return NOTIFY_DONE; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1444 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1445 | |
Robert Richter | 4177c42 | 2010-09-02 15:07:48 -0400 | [diff] [blame] | 1446 | handled = x86_pmu.handle_irq(args->regs); |
| 1447 | if (!handled) |
| 1448 | return NOTIFY_DONE; |
| 1449 | |
| 1450 | this_nmi = percpu_read(irq_stat.__nmi_count); |
| 1451 | if ((handled > 1) || |
| 1452 | /* the next nmi could be a back-to-back nmi */ |
Tejun Heo | 0a3aee0 | 2010-12-18 16:28:55 +0100 | [diff] [blame] | 1453 | ((__this_cpu_read(pmu_nmi.marked) == this_nmi) && |
| 1454 | (__this_cpu_read(pmu_nmi.handled) > 1))) { |
Robert Richter | 4177c42 | 2010-09-02 15:07:48 -0400 | [diff] [blame] | 1455 | /* |
| 1456 | * We could have two subsequent back-to-back nmis: The |
| 1457 | * first handles more than one counter, the 2nd |
| 1458 | * handles only one counter and the 3rd handles no |
| 1459 | * counter. |
| 1460 | * |
| 1461 | * This is the 2nd nmi because the previous was |
| 1462 | * handling more than one counter. We will mark the |
| 1463 | * next (3rd) and then drop it if unhandled. |
| 1464 | */ |
Tejun Heo | 0a3aee0 | 2010-12-18 16:28:55 +0100 | [diff] [blame] | 1465 | __this_cpu_write(pmu_nmi.marked, this_nmi + 1); |
| 1466 | __this_cpu_write(pmu_nmi.handled, handled); |
Robert Richter | 4177c42 | 2010-09-02 15:07:48 -0400 | [diff] [blame] | 1467 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1468 | |
Peter Zijlstra | a4016a7 | 2009-05-14 14:52:17 +0200 | [diff] [blame] | 1469 | return NOTIFY_STOP; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1470 | } |
| 1471 | |
Peter Zijlstra | f22f54f | 2010-02-26 12:05:05 +0100 | [diff] [blame] | 1472 | static __read_mostly struct notifier_block perf_event_nmi_notifier = { |
| 1473 | .notifier_call = perf_event_nmi_handler, |
| 1474 | .next = NULL, |
Don Zickus | 166d751 | 2011-01-06 16:18:49 -0500 | [diff] [blame] | 1475 | .priority = NMI_LOCAL_LOW_PRIOR, |
Peter Zijlstra | f22f54f | 2010-02-26 12:05:05 +0100 | [diff] [blame] | 1476 | }; |
| 1477 | |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1478 | static struct event_constraint unconstrained; |
Stephane Eranian | 38331f6 | 2010-02-08 17:17:01 +0200 | [diff] [blame] | 1479 | static struct event_constraint emptyconstraint; |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1480 | |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1481 | static struct event_constraint * |
Peter Zijlstra | f22f54f | 2010-02-26 12:05:05 +0100 | [diff] [blame] | 1482 | x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1483 | { |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1484 | struct event_constraint *c; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1485 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1486 | if (x86_pmu.event_constraints) { |
| 1487 | for_each_event_constraint(c, x86_pmu.event_constraints) { |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1488 | if ((event->hw.config & c->cmask) == c->code) |
| 1489 | return c; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1490 | } |
| 1491 | } |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1492 | |
| 1493 | return &unconstrained; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1494 | } |
| 1495 | |
Peter Zijlstra | f22f54f | 2010-02-26 12:05:05 +0100 | [diff] [blame] | 1496 | #include "perf_event_amd.c" |
| 1497 | #include "perf_event_p6.c" |
Cyrill Gorcunov | a072738 | 2010-03-11 19:54:39 +0300 | [diff] [blame] | 1498 | #include "perf_event_p4.c" |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 1499 | #include "perf_event_intel_lbr.c" |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1500 | #include "perf_event_intel_ds.c" |
Peter Zijlstra | f22f54f | 2010-02-26 12:05:05 +0100 | [diff] [blame] | 1501 | #include "perf_event_intel.c" |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1502 | |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1503 | static int __cpuinit |
| 1504 | x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu) |
| 1505 | { |
| 1506 | unsigned int cpu = (long)hcpu; |
Peter Zijlstra | b38b24e | 2010-03-23 19:31:15 +0100 | [diff] [blame] | 1507 | int ret = NOTIFY_OK; |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1508 | |
| 1509 | switch (action & ~CPU_TASKS_FROZEN) { |
| 1510 | case CPU_UP_PREPARE: |
| 1511 | if (x86_pmu.cpu_prepare) |
Peter Zijlstra | b38b24e | 2010-03-23 19:31:15 +0100 | [diff] [blame] | 1512 | ret = x86_pmu.cpu_prepare(cpu); |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1513 | break; |
| 1514 | |
| 1515 | case CPU_STARTING: |
| 1516 | if (x86_pmu.cpu_starting) |
| 1517 | x86_pmu.cpu_starting(cpu); |
| 1518 | break; |
| 1519 | |
| 1520 | case CPU_DYING: |
| 1521 | if (x86_pmu.cpu_dying) |
| 1522 | x86_pmu.cpu_dying(cpu); |
| 1523 | break; |
| 1524 | |
Peter Zijlstra | b38b24e | 2010-03-23 19:31:15 +0100 | [diff] [blame] | 1525 | case CPU_UP_CANCELED: |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1526 | case CPU_DEAD: |
| 1527 | if (x86_pmu.cpu_dead) |
| 1528 | x86_pmu.cpu_dead(cpu); |
| 1529 | break; |
| 1530 | |
| 1531 | default: |
| 1532 | break; |
| 1533 | } |
| 1534 | |
Peter Zijlstra | b38b24e | 2010-03-23 19:31:15 +0100 | [diff] [blame] | 1535 | return ret; |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1536 | } |
| 1537 | |
Cyrill Gorcunov | 1255803 | 2009-12-10 19:56:34 +0300 | [diff] [blame] | 1538 | static void __init pmu_check_apic(void) |
| 1539 | { |
| 1540 | if (cpu_has_apic) |
| 1541 | return; |
| 1542 | |
| 1543 | x86_pmu.apic = 0; |
| 1544 | pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n"); |
| 1545 | pr_info("no hardware sampling interrupt available.\n"); |
| 1546 | } |
| 1547 | |
Yinghai Lu | dda9911 | 2011-01-21 15:30:01 -0800 | [diff] [blame] | 1548 | static int __init init_hw_perf_events(void) |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1549 | { |
Peter Zijlstra | b622d64 | 2010-02-01 15:36:30 +0100 | [diff] [blame] | 1550 | struct event_constraint *c; |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 1551 | int err; |
| 1552 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1553 | pr_info("Performance Events: "); |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1554 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1555 | switch (boot_cpu_data.x86_vendor) { |
| 1556 | case X86_VENDOR_INTEL: |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 1557 | err = intel_pmu_init(); |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1558 | break; |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1559 | case X86_VENDOR_AMD: |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 1560 | err = amd_pmu_init(); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1561 | break; |
Robert Richter | 4138960 | 2009-04-29 12:47:00 +0200 | [diff] [blame] | 1562 | default: |
Peter Zijlstra | 004417a | 2010-11-25 18:38:29 +0100 | [diff] [blame] | 1563 | return 0; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1564 | } |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1565 | if (err != 0) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1566 | pr_cont("no PMU driver, software events only.\n"); |
Peter Zijlstra | 004417a | 2010-11-25 18:38:29 +0100 | [diff] [blame] | 1567 | return 0; |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1568 | } |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1569 | |
Cyrill Gorcunov | 1255803 | 2009-12-10 19:56:34 +0300 | [diff] [blame] | 1570 | pmu_check_apic(); |
| 1571 | |
Don Zickus | 33c6d6a | 2010-11-22 16:55:23 -0500 | [diff] [blame] | 1572 | /* sanity check that the hardware exists or is emulated */ |
Peter Zijlstra | 4407204 | 2010-12-08 15:56:23 +0100 | [diff] [blame] | 1573 | if (!check_hw_exists()) |
Peter Zijlstra | 004417a | 2010-11-25 18:38:29 +0100 | [diff] [blame] | 1574 | return 0; |
Don Zickus | 33c6d6a | 2010-11-22 16:55:23 -0500 | [diff] [blame] | 1575 | |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1576 | pr_cont("%s PMU driver.\n", x86_pmu.name); |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 1577 | |
Peter Zijlstra | 3c44780 | 2010-03-04 21:49:01 +0100 | [diff] [blame] | 1578 | if (x86_pmu.quirks) |
| 1579 | x86_pmu.quirks(); |
| 1580 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1581 | if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1582 | WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!", |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1583 | x86_pmu.num_counters, X86_PMC_MAX_GENERIC); |
| 1584 | x86_pmu.num_counters = X86_PMC_MAX_GENERIC; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1585 | } |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1586 | x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1587 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1588 | if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1589 | WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!", |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1590 | x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED); |
| 1591 | x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED; |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 1592 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1593 | |
Robert Richter | d6dc0b4 | 2010-03-17 12:49:13 +0100 | [diff] [blame] | 1594 | x86_pmu.intel_ctrl |= |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1595 | ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED; |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 1596 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1597 | perf_events_lapic_init(); |
| 1598 | register_die_notifier(&perf_event_nmi_notifier); |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1599 | |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1600 | unconstrained = (struct event_constraint) |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1601 | __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1, |
| 1602 | 0, x86_pmu.num_counters); |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1603 | |
Peter Zijlstra | b622d64 | 2010-02-01 15:36:30 +0100 | [diff] [blame] | 1604 | if (x86_pmu.event_constraints) { |
| 1605 | for_each_event_constraint(c, x86_pmu.event_constraints) { |
Robert Richter | a098f44 | 2010-03-30 11:28:21 +0200 | [diff] [blame] | 1606 | if (c->cmask != X86_RAW_EVENT_MASK) |
Peter Zijlstra | b622d64 | 2010-02-01 15:36:30 +0100 | [diff] [blame] | 1607 | continue; |
| 1608 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1609 | c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1; |
| 1610 | c->weight += x86_pmu.num_counters; |
Peter Zijlstra | b622d64 | 2010-02-01 15:36:30 +0100 | [diff] [blame] | 1611 | } |
| 1612 | } |
| 1613 | |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 1614 | pr_info("... version: %d\n", x86_pmu.version); |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1615 | pr_info("... bit width: %d\n", x86_pmu.cntval_bits); |
| 1616 | pr_info("... generic registers: %d\n", x86_pmu.num_counters); |
| 1617 | pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask); |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 1618 | pr_info("... max period: %016Lx\n", x86_pmu.max_period); |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1619 | pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed); |
Robert Richter | d6dc0b4 | 2010-03-17 12:49:13 +0100 | [diff] [blame] | 1620 | pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl); |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1621 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 1622 | perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW); |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1623 | perf_cpu_notifier(x86_pmu_notifier); |
Peter Zijlstra | 004417a | 2010-11-25 18:38:29 +0100 | [diff] [blame] | 1624 | |
| 1625 | return 0; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1626 | } |
Peter Zijlstra | 004417a | 2010-11-25 18:38:29 +0100 | [diff] [blame] | 1627 | early_initcall(init_hw_perf_events); |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1628 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1629 | static inline void x86_pmu_read(struct perf_event *event) |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1630 | { |
Peter Zijlstra | cc2ad4b | 2010-03-02 20:18:39 +0100 | [diff] [blame] | 1631 | x86_perf_event_update(event); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1632 | } |
| 1633 | |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 1634 | /* |
| 1635 | * Start group events scheduling transaction |
| 1636 | * Set the flag to make pmu::enable() not perform the |
| 1637 | * schedulability test, it will be performed at commit time |
| 1638 | */ |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 1639 | static void x86_pmu_start_txn(struct pmu *pmu) |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 1640 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1641 | perf_pmu_disable(pmu); |
Tejun Heo | 0a3aee0 | 2010-12-18 16:28:55 +0100 | [diff] [blame] | 1642 | __this_cpu_or(cpu_hw_events.group_flag, PERF_EVENT_TXN); |
| 1643 | __this_cpu_write(cpu_hw_events.n_txn, 0); |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | /* |
| 1647 | * Stop group events scheduling transaction |
| 1648 | * Clear the flag and pmu::enable() will perform the |
| 1649 | * schedulability test. |
| 1650 | */ |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 1651 | static void x86_pmu_cancel_txn(struct pmu *pmu) |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 1652 | { |
Tejun Heo | 0a3aee0 | 2010-12-18 16:28:55 +0100 | [diff] [blame] | 1653 | __this_cpu_and(cpu_hw_events.group_flag, ~PERF_EVENT_TXN); |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 1654 | /* |
| 1655 | * Truncate the collected events. |
| 1656 | */ |
Tejun Heo | 0a3aee0 | 2010-12-18 16:28:55 +0100 | [diff] [blame] | 1657 | __this_cpu_sub(cpu_hw_events.n_added, __this_cpu_read(cpu_hw_events.n_txn)); |
| 1658 | __this_cpu_sub(cpu_hw_events.n_events, __this_cpu_read(cpu_hw_events.n_txn)); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1659 | perf_pmu_enable(pmu); |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | /* |
| 1663 | * Commit group events scheduling transaction |
| 1664 | * Perform the group schedulability test as a whole |
| 1665 | * Return 0 if success |
| 1666 | */ |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 1667 | static int x86_pmu_commit_txn(struct pmu *pmu) |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 1668 | { |
| 1669 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 1670 | int assign[X86_PMC_IDX_MAX]; |
| 1671 | int n, ret; |
| 1672 | |
| 1673 | n = cpuc->n_events; |
| 1674 | |
| 1675 | if (!x86_pmu_initialized()) |
| 1676 | return -EAGAIN; |
| 1677 | |
| 1678 | ret = x86_pmu.schedule_events(cpuc, n, assign); |
| 1679 | if (ret) |
| 1680 | return ret; |
| 1681 | |
| 1682 | /* |
| 1683 | * copy new assignment, now we know it is possible |
| 1684 | * will be used by hw_perf_enable() |
| 1685 | */ |
| 1686 | memcpy(cpuc->assign, assign, n*sizeof(int)); |
| 1687 | |
Peter Zijlstra | 8d2cacb | 2010-05-25 17:49:05 +0200 | [diff] [blame] | 1688 | cpuc->group_flag &= ~PERF_EVENT_TXN; |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1689 | perf_pmu_enable(pmu); |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 1690 | return 0; |
| 1691 | } |
| 1692 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1693 | /* |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1694 | * validate that we can schedule this event |
| 1695 | */ |
| 1696 | static int validate_event(struct perf_event *event) |
| 1697 | { |
| 1698 | struct cpu_hw_events *fake_cpuc; |
| 1699 | struct event_constraint *c; |
| 1700 | int ret = 0; |
| 1701 | |
| 1702 | fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO); |
| 1703 | if (!fake_cpuc) |
| 1704 | return -ENOMEM; |
| 1705 | |
| 1706 | c = x86_pmu.get_event_constraints(fake_cpuc, event); |
| 1707 | |
| 1708 | if (!c || !c->weight) |
| 1709 | ret = -ENOSPC; |
| 1710 | |
| 1711 | if (x86_pmu.put_event_constraints) |
| 1712 | x86_pmu.put_event_constraints(fake_cpuc, event); |
| 1713 | |
| 1714 | kfree(fake_cpuc); |
| 1715 | |
| 1716 | return ret; |
| 1717 | } |
| 1718 | |
| 1719 | /* |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1720 | * validate a single event group |
| 1721 | * |
| 1722 | * validation include: |
Ingo Molnar | 184f412 | 2010-01-27 08:39:39 +0100 | [diff] [blame] | 1723 | * - check events are compatible which each other |
| 1724 | * - events do not compete for the same counter |
| 1725 | * - number of events <= number of counters |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1726 | * |
| 1727 | * validation ensures the group can be loaded onto the |
| 1728 | * PMU if it was the only group available. |
| 1729 | */ |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1730 | static int validate_group(struct perf_event *event) |
| 1731 | { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1732 | struct perf_event *leader = event->group_leader; |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1733 | struct cpu_hw_events *fake_cpuc; |
| 1734 | int ret, n; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1735 | |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1736 | ret = -ENOMEM; |
| 1737 | fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO); |
| 1738 | if (!fake_cpuc) |
| 1739 | goto out; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1740 | /* |
| 1741 | * the event is not yet connected with its |
| 1742 | * siblings therefore we must first collect |
| 1743 | * existing siblings, then add the new event |
| 1744 | * before we can simulate the scheduling |
| 1745 | */ |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1746 | ret = -ENOSPC; |
| 1747 | n = collect_events(fake_cpuc, leader, true); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1748 | if (n < 0) |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1749 | goto out_free; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1750 | |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1751 | fake_cpuc->n_events = n; |
| 1752 | n = collect_events(fake_cpuc, event, false); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1753 | if (n < 0) |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1754 | goto out_free; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1755 | |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1756 | fake_cpuc->n_events = n; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1757 | |
Cyrill Gorcunov | a072738 | 2010-03-11 19:54:39 +0300 | [diff] [blame] | 1758 | ret = x86_pmu.schedule_events(fake_cpuc, n, NULL); |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1759 | |
| 1760 | out_free: |
| 1761 | kfree(fake_cpuc); |
| 1762 | out: |
| 1763 | return ret; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1764 | } |
| 1765 | |
Yinghai Lu | dda9911 | 2011-01-21 15:30:01 -0800 | [diff] [blame] | 1766 | static int x86_pmu_event_init(struct perf_event *event) |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1767 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 1768 | struct pmu *tmp; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1769 | int err; |
| 1770 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1771 | switch (event->attr.type) { |
| 1772 | case PERF_TYPE_RAW: |
| 1773 | case PERF_TYPE_HARDWARE: |
| 1774 | case PERF_TYPE_HW_CACHE: |
| 1775 | break; |
| 1776 | |
| 1777 | default: |
| 1778 | return -ENOENT; |
| 1779 | } |
| 1780 | |
| 1781 | err = __x86_pmu_event_init(event); |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1782 | if (!err) { |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 1783 | /* |
| 1784 | * we temporarily connect event to its pmu |
| 1785 | * such that validate_group() can classify |
| 1786 | * it as an x86 event using is_x86_event() |
| 1787 | */ |
| 1788 | tmp = event->pmu; |
| 1789 | event->pmu = &pmu; |
| 1790 | |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1791 | if (event->group_leader != event) |
| 1792 | err = validate_group(event); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1793 | else |
| 1794 | err = validate_event(event); |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 1795 | |
| 1796 | event->pmu = tmp; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1797 | } |
Peter Zijlstra | a1792cdac | 2009-09-09 10:04:47 +0200 | [diff] [blame] | 1798 | if (err) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1799 | if (event->destroy) |
| 1800 | event->destroy(event); |
Peter Zijlstra | a1792cdac | 2009-09-09 10:04:47 +0200 | [diff] [blame] | 1801 | } |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1802 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1803 | return err; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1804 | } |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1805 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1806 | static struct pmu pmu = { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1807 | .pmu_enable = x86_pmu_enable, |
| 1808 | .pmu_disable = x86_pmu_disable, |
| 1809 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1810 | .event_init = x86_pmu_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1811 | |
| 1812 | .add = x86_pmu_add, |
| 1813 | .del = x86_pmu_del, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1814 | .start = x86_pmu_start, |
| 1815 | .stop = x86_pmu_stop, |
| 1816 | .read = x86_pmu_read, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1817 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1818 | .start_txn = x86_pmu_start_txn, |
| 1819 | .cancel_txn = x86_pmu_cancel_txn, |
| 1820 | .commit_txn = x86_pmu_commit_txn, |
| 1821 | }; |
| 1822 | |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1823 | /* |
| 1824 | * callchain support |
| 1825 | */ |
| 1826 | |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1827 | static int backtrace_stack(void *data, char *name) |
| 1828 | { |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 1829 | return 0; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1830 | } |
| 1831 | |
| 1832 | static void backtrace_address(void *data, unsigned long addr, int reliable) |
| 1833 | { |
| 1834 | struct perf_callchain_entry *entry = data; |
| 1835 | |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 1836 | perf_callchain_store(entry, addr); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | static const struct stacktrace_ops backtrace_ops = { |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1840 | .stack = backtrace_stack, |
| 1841 | .address = backtrace_address, |
Frederic Weisbecker | 06d65bd | 2009-12-17 05:40:34 +0100 | [diff] [blame] | 1842 | .walk_stack = print_context_stack_bp, |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1843 | }; |
| 1844 | |
Frederic Weisbecker | 56962b444 | 2010-06-30 23:03:51 +0200 | [diff] [blame] | 1845 | void |
| 1846 | perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs) |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1847 | { |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 1848 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) { |
| 1849 | /* TODO: We don't support guest os callchain now */ |
Peter Zijlstra | ed80526 | 2010-08-20 14:30:41 +0200 | [diff] [blame] | 1850 | return; |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 1851 | } |
| 1852 | |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 1853 | perf_callchain_store(entry, regs->ip); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1854 | |
Namhyung Kim | e8e999cf | 2011-03-18 11:40:06 +0900 | [diff] [blame] | 1855 | dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1856 | } |
| 1857 | |
Torok Edwin | 257ef9d | 2010-03-17 12:07:16 +0200 | [diff] [blame] | 1858 | #ifdef CONFIG_COMPAT |
| 1859 | static inline int |
| 1860 | perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry) |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 1861 | { |
Torok Edwin | 257ef9d | 2010-03-17 12:07:16 +0200 | [diff] [blame] | 1862 | /* 32-bit process in 64-bit kernel. */ |
| 1863 | struct stack_frame_ia32 frame; |
| 1864 | const void __user *fp; |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 1865 | |
Torok Edwin | 257ef9d | 2010-03-17 12:07:16 +0200 | [diff] [blame] | 1866 | if (!test_thread_flag(TIF_IA32)) |
| 1867 | return 0; |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 1868 | |
Torok Edwin | 257ef9d | 2010-03-17 12:07:16 +0200 | [diff] [blame] | 1869 | fp = compat_ptr(regs->bp); |
| 1870 | while (entry->nr < PERF_MAX_STACK_DEPTH) { |
| 1871 | unsigned long bytes; |
| 1872 | frame.next_frame = 0; |
| 1873 | frame.return_address = 0; |
| 1874 | |
| 1875 | bytes = copy_from_user_nmi(&frame, fp, sizeof(frame)); |
| 1876 | if (bytes != sizeof(frame)) |
| 1877 | break; |
| 1878 | |
| 1879 | if (fp < compat_ptr(regs->sp)) |
| 1880 | break; |
| 1881 | |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 1882 | perf_callchain_store(entry, frame.return_address); |
Torok Edwin | 257ef9d | 2010-03-17 12:07:16 +0200 | [diff] [blame] | 1883 | fp = compat_ptr(frame.next_frame); |
| 1884 | } |
| 1885 | return 1; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1886 | } |
Torok Edwin | 257ef9d | 2010-03-17 12:07:16 +0200 | [diff] [blame] | 1887 | #else |
| 1888 | static inline int |
| 1889 | perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry) |
| 1890 | { |
| 1891 | return 0; |
| 1892 | } |
| 1893 | #endif |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1894 | |
Frederic Weisbecker | 56962b444 | 2010-06-30 23:03:51 +0200 | [diff] [blame] | 1895 | void |
| 1896 | perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs) |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1897 | { |
| 1898 | struct stack_frame frame; |
| 1899 | const void __user *fp; |
| 1900 | |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 1901 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) { |
| 1902 | /* TODO: We don't support guest os callchain now */ |
Peter Zijlstra | ed80526 | 2010-08-20 14:30:41 +0200 | [diff] [blame] | 1903 | return; |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 1904 | } |
Ingo Molnar | 5a6cec3 | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1905 | |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 1906 | fp = (void __user *)regs->bp; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1907 | |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 1908 | perf_callchain_store(entry, regs->ip); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1909 | |
Torok Edwin | 257ef9d | 2010-03-17 12:07:16 +0200 | [diff] [blame] | 1910 | if (perf_callchain_user32(regs, entry)) |
| 1911 | return; |
| 1912 | |
Peter Zijlstra | f9188e0 | 2009-06-18 22:20:52 +0200 | [diff] [blame] | 1913 | while (entry->nr < PERF_MAX_STACK_DEPTH) { |
Torok Edwin | 257ef9d | 2010-03-17 12:07:16 +0200 | [diff] [blame] | 1914 | unsigned long bytes; |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 1915 | frame.next_frame = NULL; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1916 | frame.return_address = 0; |
| 1917 | |
Torok Edwin | 257ef9d | 2010-03-17 12:07:16 +0200 | [diff] [blame] | 1918 | bytes = copy_from_user_nmi(&frame, fp, sizeof(frame)); |
| 1919 | if (bytes != sizeof(frame)) |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1920 | break; |
| 1921 | |
Ingo Molnar | 5a6cec3 | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1922 | if ((unsigned long)fp < regs->sp) |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1923 | break; |
| 1924 | |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 1925 | perf_callchain_store(entry, frame.return_address); |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 1926 | fp = frame.next_frame; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1927 | } |
| 1928 | } |
| 1929 | |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 1930 | unsigned long perf_instruction_pointer(struct pt_regs *regs) |
| 1931 | { |
| 1932 | unsigned long ip; |
Zhang, Yanmin | dcf46b9 | 2010-04-20 10:13:58 +0800 | [diff] [blame] | 1933 | |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 1934 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) |
| 1935 | ip = perf_guest_cbs->get_guest_ip(); |
| 1936 | else |
| 1937 | ip = instruction_pointer(regs); |
Zhang, Yanmin | dcf46b9 | 2010-04-20 10:13:58 +0800 | [diff] [blame] | 1938 | |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 1939 | return ip; |
| 1940 | } |
| 1941 | |
| 1942 | unsigned long perf_misc_flags(struct pt_regs *regs) |
| 1943 | { |
| 1944 | int misc = 0; |
Zhang, Yanmin | dcf46b9 | 2010-04-20 10:13:58 +0800 | [diff] [blame] | 1945 | |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 1946 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) { |
Zhang, Yanmin | dcf46b9 | 2010-04-20 10:13:58 +0800 | [diff] [blame] | 1947 | if (perf_guest_cbs->is_user_mode()) |
| 1948 | misc |= PERF_RECORD_MISC_GUEST_USER; |
| 1949 | else |
| 1950 | misc |= PERF_RECORD_MISC_GUEST_KERNEL; |
| 1951 | } else { |
| 1952 | if (user_mode(regs)) |
| 1953 | misc |= PERF_RECORD_MISC_USER; |
| 1954 | else |
| 1955 | misc |= PERF_RECORD_MISC_KERNEL; |
| 1956 | } |
| 1957 | |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 1958 | if (regs->flags & PERF_EFLAGS_EXACT) |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1959 | misc |= PERF_RECORD_MISC_EXACT_IP; |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 1960 | |
| 1961 | return misc; |
| 1962 | } |