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> |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 24 | #include <linux/highmem.h> |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 25 | #include <linux/cpu.h> |
Peter Zijlstra | 272d30b | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 26 | #include <linux/bitops.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 27 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 28 | #include <asm/apic.h> |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 29 | #include <asm/stacktrace.h> |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 30 | #include <asm/nmi.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 31 | |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 32 | /* |
| 33 | * best effort, GUP based copy_from_user() that assumes IRQ or NMI context |
| 34 | */ |
| 35 | static unsigned long |
| 36 | copy_from_user_nmi(void *to, const void __user *from, unsigned long n) |
| 37 | { |
| 38 | unsigned long offset, addr = (unsigned long)from; |
| 39 | int type = in_nmi() ? KM_NMI : KM_IRQ0; |
| 40 | unsigned long size, len = 0; |
| 41 | struct page *page; |
| 42 | void *map; |
| 43 | int ret; |
| 44 | |
| 45 | do { |
| 46 | ret = __get_user_pages_fast(addr, 1, 0, &page); |
| 47 | if (!ret) |
| 48 | break; |
| 49 | |
| 50 | offset = addr & (PAGE_SIZE - 1); |
| 51 | size = min(PAGE_SIZE - offset, n - len); |
| 52 | |
| 53 | map = kmap_atomic(page, type); |
| 54 | memcpy(to, map+offset, size); |
| 55 | kunmap_atomic(map, type); |
| 56 | put_page(page); |
| 57 | |
| 58 | len += size; |
| 59 | to += size; |
| 60 | addr += size; |
| 61 | |
| 62 | } while (len < n); |
| 63 | |
| 64 | return len; |
| 65 | } |
| 66 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 67 | static u64 perf_event_mask __read_mostly; |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 68 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 69 | struct event_constraint { |
Peter Zijlstra | c91e0f5 | 2010-01-22 15:25:59 +0100 | [diff] [blame] | 70 | union { |
| 71 | unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
Peter Zijlstra | b622d64 | 2010-02-01 15:36:30 +0100 | [diff] [blame] | 72 | u64 idxmsk64; |
Peter Zijlstra | c91e0f5 | 2010-01-22 15:25:59 +0100 | [diff] [blame] | 73 | }; |
Peter Zijlstra | b622d64 | 2010-02-01 15:36:30 +0100 | [diff] [blame] | 74 | u64 code; |
| 75 | u64 cmask; |
Peter Zijlstra | 272d30b | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 76 | int weight; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
Stephane Eranian | 38331f6 | 2010-02-08 17:17:01 +0200 | [diff] [blame] | 79 | struct amd_nb { |
| 80 | int nb_id; /* NorthBridge id */ |
| 81 | int refcnt; /* reference count */ |
| 82 | struct perf_event *owners[X86_PMC_IDX_MAX]; |
| 83 | struct event_constraint event_constraints[X86_PMC_IDX_MAX]; |
| 84 | }; |
| 85 | |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 86 | #define MAX_LBR_ENTRIES 16 |
| 87 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 88 | struct cpu_hw_events { |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 89 | /* |
| 90 | * Generic x86 PMC bits |
| 91 | */ |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 92 | struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */ |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 93 | unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
Mike Galbraith | 4b39fd9 | 2009-01-23 14:36:16 +0100 | [diff] [blame] | 94 | unsigned long interrupts; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 95 | int enabled; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 96 | |
| 97 | int n_events; |
| 98 | int n_added; |
| 99 | int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */ |
Stephane Eranian | 447a194 | 2010-02-01 14:50:01 +0200 | [diff] [blame] | 100 | u64 tags[X86_PMC_IDX_MAX]; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 101 | struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */ |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * Intel DebugStore bits |
| 105 | */ |
| 106 | struct debug_store *ds; |
| 107 | u64 pebs_enabled; |
| 108 | |
| 109 | /* |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 110 | * Intel LBR bits |
| 111 | */ |
| 112 | int lbr_users; |
| 113 | void *lbr_context; |
| 114 | struct perf_branch_stack lbr_stack; |
| 115 | struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES]; |
| 116 | |
| 117 | /* |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 118 | * AMD specific bits |
| 119 | */ |
Stephane Eranian | 38331f6 | 2010-02-08 17:17:01 +0200 | [diff] [blame] | 120 | struct amd_nb *amd_nb; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 121 | }; |
| 122 | |
Peter Zijlstra | fce877e | 2010-01-29 13:25:12 +0100 | [diff] [blame] | 123 | #define __EVENT_CONSTRAINT(c, n, m, w) {\ |
Peter Zijlstra | b622d64 | 2010-02-01 15:36:30 +0100 | [diff] [blame] | 124 | { .idxmsk64 = (n) }, \ |
Peter Zijlstra | c91e0f5 | 2010-01-22 15:25:59 +0100 | [diff] [blame] | 125 | .code = (c), \ |
| 126 | .cmask = (m), \ |
Peter Zijlstra | fce877e | 2010-01-29 13:25:12 +0100 | [diff] [blame] | 127 | .weight = (w), \ |
Peter Zijlstra | c91e0f5 | 2010-01-22 15:25:59 +0100 | [diff] [blame] | 128 | } |
Stephane Eranian | b690081 | 2009-10-06 16:42:09 +0200 | [diff] [blame] | 129 | |
Peter Zijlstra | fce877e | 2010-01-29 13:25:12 +0100 | [diff] [blame] | 130 | #define EVENT_CONSTRAINT(c, n, m) \ |
| 131 | __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n)) |
| 132 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 133 | /* |
| 134 | * Constraint on the Event code. |
| 135 | */ |
Peter Zijlstra | ed8777f | 2010-01-27 23:07:46 +0100 | [diff] [blame] | 136 | #define INTEL_EVENT_CONSTRAINT(c, n) \ |
| 137 | EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVTSEL_MASK) |
Peter Zijlstra | 8433be1 | 2010-01-22 15:38:26 +0100 | [diff] [blame] | 138 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 139 | /* |
| 140 | * Constraint on the Event code + UMask + fixed-mask |
| 141 | */ |
Peter Zijlstra | ed8777f | 2010-01-27 23:07:46 +0100 | [diff] [blame] | 142 | #define FIXED_EVENT_CONSTRAINT(c, n) \ |
Peter Zijlstra | b622d64 | 2010-02-01 15:36:30 +0100 | [diff] [blame] | 143 | EVENT_CONSTRAINT(c, (1ULL << (32+n)), INTEL_ARCH_FIXED_MASK) |
Peter Zijlstra | 8433be1 | 2010-01-22 15:38:26 +0100 | [diff] [blame] | 144 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 145 | /* |
| 146 | * Constraint on the Event code + UMask |
| 147 | */ |
| 148 | #define PEBS_EVENT_CONSTRAINT(c, n) \ |
| 149 | EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK) |
| 150 | |
Peter Zijlstra | ed8777f | 2010-01-27 23:07:46 +0100 | [diff] [blame] | 151 | #define EVENT_CONSTRAINT_END \ |
| 152 | EVENT_CONSTRAINT(0, 0, 0) |
| 153 | |
| 154 | #define for_each_event_constraint(e, c) \ |
| 155 | for ((e) = (c); (e)->cmask; (e)++) |
Stephane Eranian | b690081 | 2009-10-06 16:42:09 +0200 | [diff] [blame] | 156 | |
Peter Zijlstra | 8db909a | 2010-03-03 17:07:40 +0100 | [diff] [blame^] | 157 | union perf_capabilities { |
| 158 | struct { |
| 159 | u64 lbr_format : 6; |
| 160 | u64 pebs_trap : 1; |
| 161 | u64 pebs_arch_reg : 1; |
| 162 | u64 pebs_format : 4; |
| 163 | u64 smm_freeze : 1; |
| 164 | }; |
| 165 | u64 capabilities; |
| 166 | }; |
| 167 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 168 | /* |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 169 | * struct x86_pmu - generic x86 pmu |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 170 | */ |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 171 | struct x86_pmu { |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 172 | /* |
| 173 | * Generic x86 PMC bits |
| 174 | */ |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 175 | const char *name; |
| 176 | int version; |
Yong Wang | a328810 | 2009-06-03 13:12:55 +0800 | [diff] [blame] | 177 | int (*handle_irq)(struct pt_regs *); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 178 | void (*disable_all)(void); |
| 179 | void (*enable_all)(void); |
Peter Zijlstra | aff3d91 | 2010-03-02 20:32:08 +0100 | [diff] [blame] | 180 | void (*enable)(struct perf_event *); |
| 181 | void (*disable)(struct perf_event *); |
Jaswinder Singh Rajput | 169e41e | 2009-02-28 18:37:49 +0530 | [diff] [blame] | 182 | unsigned eventsel; |
| 183 | unsigned perfctr; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 184 | u64 (*event_map)(int); |
| 185 | u64 (*raw_event)(u64); |
Jaswinder Singh Rajput | 169e41e | 2009-02-28 18:37:49 +0530 | [diff] [blame] | 186 | int max_events; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 187 | int num_events; |
| 188 | int num_events_fixed; |
| 189 | int event_bits; |
| 190 | u64 event_mask; |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 191 | int apic; |
Robert Richter | c619b8f | 2009-04-29 12:47:23 +0200 | [diff] [blame] | 192 | u64 max_period; |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 193 | struct event_constraint * |
| 194 | (*get_event_constraints)(struct cpu_hw_events *cpuc, |
| 195 | struct perf_event *event); |
| 196 | |
Peter Zijlstra | c91e0f5 | 2010-01-22 15:25:59 +0100 | [diff] [blame] | 197 | void (*put_event_constraints)(struct cpu_hw_events *cpuc, |
| 198 | struct perf_event *event); |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 199 | struct event_constraint *event_constraints; |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 200 | |
| 201 | void (*cpu_prepare)(int cpu); |
| 202 | void (*cpu_starting)(int cpu); |
| 203 | void (*cpu_dying)(int cpu); |
| 204 | void (*cpu_dead)(int cpu); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 205 | |
| 206 | /* |
| 207 | * Intel Arch Perfmon v2+ |
| 208 | */ |
Peter Zijlstra | 8db909a | 2010-03-03 17:07:40 +0100 | [diff] [blame^] | 209 | u64 intel_ctrl; |
| 210 | union perf_capabilities intel_cap; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 211 | |
| 212 | /* |
| 213 | * Intel DebugStore bits |
| 214 | */ |
| 215 | int bts, pebs; |
| 216 | int pebs_record_size; |
| 217 | void (*drain_pebs)(struct pt_regs *regs); |
| 218 | struct event_constraint *pebs_constraints; |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 219 | |
| 220 | /* |
| 221 | * Intel LBR |
| 222 | */ |
| 223 | unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */ |
| 224 | int lbr_nr; /* hardware stack size */ |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 225 | }; |
| 226 | |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 227 | static struct x86_pmu x86_pmu __read_mostly; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 228 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 229 | static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 230 | .enabled = 1, |
| 231 | }; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 232 | |
Peter Zijlstra | 07088ed | 2010-03-02 20:16:01 +0100 | [diff] [blame] | 233 | static int x86_perf_event_set_period(struct perf_event *event); |
Stephane Eranian | b690081 | 2009-10-06 16:42:09 +0200 | [diff] [blame] | 234 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 235 | /* |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 236 | * Generalized hw caching related hw_event table, filled |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 237 | * in on a per model basis. A value of 0 means |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 238 | * 'not supported', -1 means 'hw_event makes no sense on |
| 239 | * this CPU', any other value means the raw hw_event |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 240 | * ID. |
| 241 | */ |
| 242 | |
| 243 | #define C(x) PERF_COUNT_HW_CACHE_##x |
| 244 | |
| 245 | static u64 __read_mostly hw_cache_event_ids |
| 246 | [PERF_COUNT_HW_CACHE_MAX] |
| 247 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 248 | [PERF_COUNT_HW_CACHE_RESULT_MAX]; |
| 249 | |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 250 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 251 | * Propagate event elapsed time into the generic event. |
| 252 | * Can only be executed on the CPU where the event is active. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 253 | * Returns the delta events processed. |
| 254 | */ |
Robert Richter | 4b7bfd0 | 2009-04-29 12:47:22 +0200 | [diff] [blame] | 255 | static u64 |
Peter Zijlstra | cc2ad4b | 2010-03-02 20:18:39 +0100 | [diff] [blame] | 256 | x86_perf_event_update(struct perf_event *event) |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 257 | { |
Peter Zijlstra | cc2ad4b | 2010-03-02 20:18:39 +0100 | [diff] [blame] | 258 | struct hw_perf_event *hwc = &event->hw; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 259 | int shift = 64 - x86_pmu.event_bits; |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 260 | u64 prev_raw_count, new_raw_count; |
Peter Zijlstra | cc2ad4b | 2010-03-02 20:18:39 +0100 | [diff] [blame] | 261 | int idx = hwc->idx; |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 262 | s64 delta; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 263 | |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 264 | if (idx == X86_PMC_IDX_FIXED_BTS) |
| 265 | return 0; |
| 266 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 267 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 268 | * Careful: an NMI might modify the previous event value. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 269 | * |
| 270 | * Our tactic to handle this is to first atomically read and |
| 271 | * exchange a new raw count - then add that new-prev delta |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 272 | * count to the generic event atomically: |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 273 | */ |
| 274 | again: |
| 275 | prev_raw_count = atomic64_read(&hwc->prev_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 276 | rdmsrl(hwc->event_base + idx, new_raw_count); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 277 | |
| 278 | if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count, |
| 279 | new_raw_count) != prev_raw_count) |
| 280 | goto again; |
| 281 | |
| 282 | /* |
| 283 | * Now we have the new raw value and have updated the prev |
| 284 | * timestamp already. We can now calculate the elapsed delta |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 285 | * (event-)time and add that to the generic event. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 286 | * |
| 287 | * Careful, not all hw sign-extends above the physical width |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 288 | * of the count. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 289 | */ |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 290 | delta = (new_raw_count << shift) - (prev_raw_count << shift); |
| 291 | delta >>= shift; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 292 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 293 | atomic64_add(delta, &event->count); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 294 | atomic64_sub(delta, &hwc->period_left); |
Robert Richter | 4b7bfd0 | 2009-04-29 12:47:22 +0200 | [diff] [blame] | 295 | |
| 296 | return new_raw_count; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 297 | } |
| 298 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 299 | static atomic_t active_events; |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 300 | static DEFINE_MUTEX(pmc_reserve_mutex); |
| 301 | |
| 302 | static bool reserve_pmc_hardware(void) |
| 303 | { |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 304 | #ifdef CONFIG_X86_LOCAL_APIC |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 305 | int i; |
| 306 | |
| 307 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 308 | disable_lapic_nmi_watchdog(); |
| 309 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 310 | for (i = 0; i < x86_pmu.num_events; i++) { |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 311 | if (!reserve_perfctr_nmi(x86_pmu.perfctr + i)) |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 312 | goto perfctr_fail; |
| 313 | } |
| 314 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 315 | for (i = 0; i < x86_pmu.num_events; i++) { |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 316 | if (!reserve_evntsel_nmi(x86_pmu.eventsel + i)) |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 317 | goto eventsel_fail; |
| 318 | } |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 319 | #endif |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 320 | |
| 321 | return true; |
| 322 | |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 323 | #ifdef CONFIG_X86_LOCAL_APIC |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 324 | eventsel_fail: |
| 325 | for (i--; i >= 0; i--) |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 326 | release_evntsel_nmi(x86_pmu.eventsel + i); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 327 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 328 | i = x86_pmu.num_events; |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 329 | |
| 330 | perfctr_fail: |
| 331 | for (i--; i >= 0; i--) |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 332 | release_perfctr_nmi(x86_pmu.perfctr + i); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 333 | |
| 334 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 335 | enable_lapic_nmi_watchdog(); |
| 336 | |
| 337 | return false; |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 338 | #endif |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | static void release_pmc_hardware(void) |
| 342 | { |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 343 | #ifdef CONFIG_X86_LOCAL_APIC |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 344 | int i; |
| 345 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 346 | for (i = 0; i < x86_pmu.num_events; i++) { |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 347 | release_perfctr_nmi(x86_pmu.perfctr + i); |
| 348 | release_evntsel_nmi(x86_pmu.eventsel + i); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 352 | enable_lapic_nmi_watchdog(); |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 353 | #endif |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 356 | static int reserve_ds_buffers(void); |
| 357 | static void release_ds_buffers(void); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 358 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 359 | static void hw_perf_event_destroy(struct perf_event *event) |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 360 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 361 | if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) { |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 362 | release_pmc_hardware(); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 363 | release_ds_buffers(); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 364 | mutex_unlock(&pmc_reserve_mutex); |
| 365 | } |
| 366 | } |
| 367 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 368 | static inline int x86_pmu_initialized(void) |
| 369 | { |
| 370 | return x86_pmu.handle_irq != NULL; |
| 371 | } |
| 372 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 373 | static inline int |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 374 | set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event_attr *attr) |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 375 | { |
| 376 | unsigned int cache_type, cache_op, cache_result; |
| 377 | u64 config, val; |
| 378 | |
| 379 | config = attr->config; |
| 380 | |
| 381 | cache_type = (config >> 0) & 0xff; |
| 382 | if (cache_type >= PERF_COUNT_HW_CACHE_MAX) |
| 383 | return -EINVAL; |
| 384 | |
| 385 | cache_op = (config >> 8) & 0xff; |
| 386 | if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX) |
| 387 | return -EINVAL; |
| 388 | |
| 389 | cache_result = (config >> 16) & 0xff; |
| 390 | if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX) |
| 391 | return -EINVAL; |
| 392 | |
| 393 | val = hw_cache_event_ids[cache_type][cache_op][cache_result]; |
| 394 | |
| 395 | if (val == 0) |
| 396 | return -ENOENT; |
| 397 | |
| 398 | if (val == -1) |
| 399 | return -EINVAL; |
| 400 | |
| 401 | hwc->config |= val; |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 406 | /* |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame] | 407 | * Setup the hardware configuration for a given attr_type |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 408 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 409 | static int __hw_perf_event_init(struct perf_event *event) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 410 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 411 | struct perf_event_attr *attr = &event->attr; |
| 412 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 413 | u64 config; |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 414 | int err; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 415 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 416 | if (!x86_pmu_initialized()) |
| 417 | return -ENODEV; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 418 | |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 419 | err = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 420 | if (!atomic_inc_not_zero(&active_events)) { |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 421 | mutex_lock(&pmc_reserve_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 422 | if (atomic_read(&active_events) == 0) { |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 423 | if (!reserve_pmc_hardware()) |
| 424 | err = -EBUSY; |
| 425 | else |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 426 | err = reserve_ds_buffers(); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 427 | } |
| 428 | if (!err) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 429 | atomic_inc(&active_events); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 430 | mutex_unlock(&pmc_reserve_mutex); |
| 431 | } |
| 432 | if (err) |
| 433 | return err; |
| 434 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 435 | event->destroy = hw_perf_event_destroy; |
Peter Zijlstra | a1792cdac | 2009-09-09 10:04:47 +0200 | [diff] [blame] | 436 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 437 | /* |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 438 | * Generate PMC IRQs: |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 439 | * (keep 'enabled' bit clear for now) |
| 440 | */ |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 441 | hwc->config = ARCH_PERFMON_EVENTSEL_INT; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 442 | |
Stephane Eranian | b690081 | 2009-10-06 16:42:09 +0200 | [diff] [blame] | 443 | hwc->idx = -1; |
Stephane Eranian | 447a194 | 2010-02-01 14:50:01 +0200 | [diff] [blame] | 444 | hwc->last_cpu = -1; |
| 445 | hwc->last_tag = ~0ULL; |
Stephane Eranian | b690081 | 2009-10-06 16:42:09 +0200 | [diff] [blame] | 446 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 447 | /* |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 448 | * Count user and OS events unless requested not to. |
| 449 | */ |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame] | 450 | if (!attr->exclude_user) |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 451 | hwc->config |= ARCH_PERFMON_EVENTSEL_USR; |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame] | 452 | if (!attr->exclude_kernel) |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 453 | hwc->config |= ARCH_PERFMON_EVENTSEL_OS; |
| 454 | |
Peter Zijlstra | bd2b5b1 | 2009-06-10 13:40:57 +0200 | [diff] [blame] | 455 | if (!hwc->sample_period) { |
Peter Zijlstra | b23f332 | 2009-06-02 15:13:03 +0200 | [diff] [blame] | 456 | hwc->sample_period = x86_pmu.max_period; |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 457 | hwc->last_period = hwc->sample_period; |
Peter Zijlstra | bd2b5b1 | 2009-06-10 13:40:57 +0200 | [diff] [blame] | 458 | atomic64_set(&hwc->period_left, hwc->sample_period); |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 459 | } else { |
| 460 | /* |
| 461 | * If we have a PMU initialized but no APIC |
| 462 | * interrupts, we cannot sample hardware |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 463 | * events (user-space has to fall back and |
| 464 | * sample via a hrtimer based software event): |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 465 | */ |
| 466 | if (!x86_pmu.apic) |
| 467 | return -EOPNOTSUPP; |
Peter Zijlstra | bd2b5b1 | 2009-06-10 13:40:57 +0200 | [diff] [blame] | 468 | } |
Ingo Molnar | d2517a4 | 2009-05-17 10:04:45 +0200 | [diff] [blame] | 469 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 470 | /* |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 471 | * Raw hw_event type provide the config in the hw_event structure |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 472 | */ |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 473 | if (attr->type == PERF_TYPE_RAW) { |
| 474 | hwc->config |= x86_pmu.raw_event(attr->config); |
Peter Zijlstra | 320ebf0 | 2010-03-02 12:35:37 +0100 | [diff] [blame] | 475 | if ((hwc->config & ARCH_PERFMON_EVENTSEL_ANY) && |
| 476 | perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) |
| 477 | return -EACCES; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 478 | return 0; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 479 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 480 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 481 | if (attr->type == PERF_TYPE_HW_CACHE) |
| 482 | return set_ext_hw_attr(hwc, attr); |
| 483 | |
| 484 | if (attr->config >= x86_pmu.max_events) |
| 485 | return -EINVAL; |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 486 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 487 | /* |
| 488 | * The generic map: |
| 489 | */ |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 490 | config = x86_pmu.event_map(attr->config); |
| 491 | |
| 492 | if (config == 0) |
| 493 | return -ENOENT; |
| 494 | |
| 495 | if (config == -1LL) |
| 496 | return -EINVAL; |
| 497 | |
markus.t.metzger@intel.com | 747b50a | 2009-09-02 16:04:46 +0200 | [diff] [blame] | 498 | /* |
| 499 | * Branch tracing: |
| 500 | */ |
| 501 | if ((attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS) && |
markus.t.metzger@intel.com | 1653192 | 2009-09-02 16:04:48 +0200 | [diff] [blame] | 502 | (hwc->sample_period == 1)) { |
| 503 | /* BTS is not supported by this architecture. */ |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 504 | if (!x86_pmu.bts) |
markus.t.metzger@intel.com | 1653192 | 2009-09-02 16:04:48 +0200 | [diff] [blame] | 505 | return -EOPNOTSUPP; |
| 506 | |
| 507 | /* BTS is currently only allowed for user-mode. */ |
| 508 | if (hwc->config & ARCH_PERFMON_EVENTSEL_OS) |
| 509 | return -EOPNOTSUPP; |
| 510 | } |
markus.t.metzger@intel.com | 747b50a | 2009-09-02 16:04:46 +0200 | [diff] [blame] | 511 | |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 512 | hwc->config |= config; |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 513 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 514 | return 0; |
| 515 | } |
| 516 | |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 517 | static void x86_pmu_disable_all(void) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 518 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 519 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 520 | int idx; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 521 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 522 | for (idx = 0; idx < x86_pmu.num_events; idx++) { |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 523 | u64 val; |
| 524 | |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 525 | if (!test_bit(idx, cpuc->active_mask)) |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 526 | continue; |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 527 | rdmsrl(x86_pmu.eventsel + idx, val); |
Robert Richter | bb1165d | 2010-03-01 14:21:23 +0100 | [diff] [blame] | 528 | if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE)) |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 529 | continue; |
Robert Richter | bb1165d | 2010-03-01 14:21:23 +0100 | [diff] [blame] | 530 | val &= ~ARCH_PERFMON_EVENTSEL_ENABLE; |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 531 | wrmsrl(x86_pmu.eventsel + idx, val); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 532 | } |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 533 | } |
| 534 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 535 | void hw_perf_disable(void) |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 536 | { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 537 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 538 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 539 | if (!x86_pmu_initialized()) |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 540 | return; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 541 | |
Peter Zijlstra | 1a6e21f | 2010-01-27 23:07:47 +0100 | [diff] [blame] | 542 | if (!cpuc->enabled) |
| 543 | return; |
| 544 | |
| 545 | cpuc->n_added = 0; |
| 546 | cpuc->enabled = 0; |
| 547 | barrier(); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 548 | |
| 549 | x86_pmu.disable_all(); |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 550 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 551 | |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 552 | static void x86_pmu_enable_all(void) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 553 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 554 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 555 | int idx; |
| 556 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 557 | for (idx = 0; idx < x86_pmu.num_events; idx++) { |
| 558 | struct perf_event *event = cpuc->events[idx]; |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 559 | u64 val; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 560 | |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 561 | if (!test_bit(idx, cpuc->active_mask)) |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 562 | continue; |
Peter Zijlstra | 984b838 | 2009-07-10 09:59:56 +0200 | [diff] [blame] | 563 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 564 | val = event->hw.config; |
Robert Richter | bb1165d | 2010-03-01 14:21:23 +0100 | [diff] [blame] | 565 | val |= ARCH_PERFMON_EVENTSEL_ENABLE; |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 566 | wrmsrl(x86_pmu.eventsel + idx, val); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 570 | static const struct pmu pmu; |
| 571 | |
| 572 | static inline int is_x86_event(struct perf_event *event) |
| 573 | { |
| 574 | return event->pmu == &pmu; |
| 575 | } |
| 576 | |
| 577 | static int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign) |
| 578 | { |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 579 | struct event_constraint *c, *constraints[X86_PMC_IDX_MAX]; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 580 | unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
Peter Zijlstra | c933c1a | 2010-01-22 16:40:12 +0100 | [diff] [blame] | 581 | int i, j, w, wmax, num = 0; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 582 | struct hw_perf_event *hwc; |
| 583 | |
| 584 | bitmap_zero(used_mask, X86_PMC_IDX_MAX); |
| 585 | |
| 586 | for (i = 0; i < n; i++) { |
Peter Zijlstra | b622d64 | 2010-02-01 15:36:30 +0100 | [diff] [blame] | 587 | c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]); |
| 588 | constraints[i] = c; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | /* |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 592 | * fastpath, try to reuse previous register |
| 593 | */ |
Peter Zijlstra | c933c1a | 2010-01-22 16:40:12 +0100 | [diff] [blame] | 594 | for (i = 0; i < n; i++) { |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 595 | hwc = &cpuc->event_list[i]->hw; |
Peter Zijlstra | 81269a0 | 2010-01-22 14:55:22 +0100 | [diff] [blame] | 596 | c = constraints[i]; |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 597 | |
| 598 | /* never assigned */ |
| 599 | if (hwc->idx == -1) |
| 600 | break; |
| 601 | |
| 602 | /* constraint still honored */ |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 603 | if (!test_bit(hwc->idx, c->idxmsk)) |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 604 | break; |
| 605 | |
| 606 | /* not already used */ |
| 607 | if (test_bit(hwc->idx, used_mask)) |
| 608 | break; |
| 609 | |
Peter Zijlstra | 34538ee | 2010-03-02 21:16:55 +0100 | [diff] [blame] | 610 | __set_bit(hwc->idx, used_mask); |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 611 | if (assign) |
| 612 | assign[i] = hwc->idx; |
| 613 | } |
Peter Zijlstra | c933c1a | 2010-01-22 16:40:12 +0100 | [diff] [blame] | 614 | if (i == n) |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 615 | goto done; |
| 616 | |
| 617 | /* |
| 618 | * begin slow path |
| 619 | */ |
| 620 | |
| 621 | bitmap_zero(used_mask, X86_PMC_IDX_MAX); |
| 622 | |
| 623 | /* |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 624 | * weight = number of possible counters |
| 625 | * |
| 626 | * 1 = most constrained, only works on one counter |
| 627 | * wmax = least constrained, works on any counter |
| 628 | * |
| 629 | * assign events to counters starting with most |
| 630 | * constrained events. |
| 631 | */ |
| 632 | wmax = x86_pmu.num_events; |
| 633 | |
| 634 | /* |
| 635 | * when fixed event counters are present, |
| 636 | * wmax is incremented by 1 to account |
| 637 | * for one more choice |
| 638 | */ |
| 639 | if (x86_pmu.num_events_fixed) |
| 640 | wmax++; |
| 641 | |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 642 | for (w = 1, num = n; num && w <= wmax; w++) { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 643 | /* for each event */ |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 644 | for (i = 0; num && i < n; i++) { |
Peter Zijlstra | 81269a0 | 2010-01-22 14:55:22 +0100 | [diff] [blame] | 645 | c = constraints[i]; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 646 | hwc = &cpuc->event_list[i]->hw; |
| 647 | |
Peter Zijlstra | 272d30b | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 648 | if (c->weight != w) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 649 | continue; |
| 650 | |
Akinobu Mita | 984b3f5 | 2010-03-05 13:41:37 -0800 | [diff] [blame] | 651 | for_each_set_bit(j, c->idxmsk, X86_PMC_IDX_MAX) { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 652 | if (!test_bit(j, used_mask)) |
| 653 | break; |
| 654 | } |
| 655 | |
| 656 | if (j == X86_PMC_IDX_MAX) |
| 657 | break; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 658 | |
Peter Zijlstra | 34538ee | 2010-03-02 21:16:55 +0100 | [diff] [blame] | 659 | __set_bit(j, used_mask); |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 660 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 661 | if (assign) |
| 662 | assign[i] = j; |
| 663 | num--; |
| 664 | } |
| 665 | } |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 666 | done: |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 667 | /* |
| 668 | * scheduling failed or is just a simulation, |
| 669 | * free resources if necessary |
| 670 | */ |
| 671 | if (!assign || num) { |
| 672 | for (i = 0; i < n; i++) { |
| 673 | if (x86_pmu.put_event_constraints) |
| 674 | x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]); |
| 675 | } |
| 676 | } |
| 677 | return num ? -ENOSPC : 0; |
| 678 | } |
| 679 | |
| 680 | /* |
| 681 | * dogrp: true if must collect siblings events (group) |
| 682 | * returns total number of events and error code |
| 683 | */ |
| 684 | static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp) |
| 685 | { |
| 686 | struct perf_event *event; |
| 687 | int n, max_count; |
| 688 | |
| 689 | max_count = x86_pmu.num_events + x86_pmu.num_events_fixed; |
| 690 | |
| 691 | /* current number of events already accepted */ |
| 692 | n = cpuc->n_events; |
| 693 | |
| 694 | if (is_x86_event(leader)) { |
| 695 | if (n >= max_count) |
| 696 | return -ENOSPC; |
| 697 | cpuc->event_list[n] = leader; |
| 698 | n++; |
| 699 | } |
| 700 | if (!dogrp) |
| 701 | return n; |
| 702 | |
| 703 | list_for_each_entry(event, &leader->sibling_list, group_entry) { |
| 704 | if (!is_x86_event(event) || |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 705 | event->state <= PERF_EVENT_STATE_OFF) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 706 | continue; |
| 707 | |
| 708 | if (n >= max_count) |
| 709 | return -ENOSPC; |
| 710 | |
| 711 | cpuc->event_list[n] = event; |
| 712 | n++; |
| 713 | } |
| 714 | return n; |
| 715 | } |
| 716 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 717 | static inline void x86_assign_hw_event(struct perf_event *event, |
Stephane Eranian | 447a194 | 2010-02-01 14:50:01 +0200 | [diff] [blame] | 718 | struct cpu_hw_events *cpuc, int i) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 719 | { |
Stephane Eranian | 447a194 | 2010-02-01 14:50:01 +0200 | [diff] [blame] | 720 | struct hw_perf_event *hwc = &event->hw; |
| 721 | |
| 722 | hwc->idx = cpuc->assign[i]; |
| 723 | hwc->last_cpu = smp_processor_id(); |
| 724 | hwc->last_tag = ++cpuc->tags[i]; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 725 | |
| 726 | if (hwc->idx == X86_PMC_IDX_FIXED_BTS) { |
| 727 | hwc->config_base = 0; |
| 728 | hwc->event_base = 0; |
| 729 | } else if (hwc->idx >= X86_PMC_IDX_FIXED) { |
| 730 | hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL; |
| 731 | /* |
| 732 | * We set it so that event_base + idx in wrmsr/rdmsr maps to |
| 733 | * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2: |
| 734 | */ |
| 735 | hwc->event_base = |
| 736 | MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED; |
| 737 | } else { |
| 738 | hwc->config_base = x86_pmu.eventsel; |
| 739 | hwc->event_base = x86_pmu.perfctr; |
| 740 | } |
| 741 | } |
| 742 | |
Stephane Eranian | 447a194 | 2010-02-01 14:50:01 +0200 | [diff] [blame] | 743 | static inline int match_prev_assignment(struct hw_perf_event *hwc, |
| 744 | struct cpu_hw_events *cpuc, |
| 745 | int i) |
| 746 | { |
| 747 | return hwc->idx == cpuc->assign[i] && |
| 748 | hwc->last_cpu == smp_processor_id() && |
| 749 | hwc->last_tag == cpuc->tags[i]; |
| 750 | } |
| 751 | |
Peter Zijlstra | c08053e | 2010-03-06 13:19:24 +0100 | [diff] [blame] | 752 | static int x86_pmu_start(struct perf_event *event); |
Stephane Eranian | d76a081 | 2010-02-08 17:06:01 +0200 | [diff] [blame] | 753 | static void x86_pmu_stop(struct perf_event *event); |
Peter Zijlstra | 2e84187 | 2010-01-25 15:58:43 +0100 | [diff] [blame] | 754 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 755 | void hw_perf_enable(void) |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 756 | { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 757 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 758 | struct perf_event *event; |
| 759 | struct hw_perf_event *hwc; |
| 760 | int i; |
| 761 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 762 | if (!x86_pmu_initialized()) |
Ingo Molnar | 2b9ff0d | 2008-12-14 18:36:30 +0100 | [diff] [blame] | 763 | return; |
Peter Zijlstra | 1a6e21f | 2010-01-27 23:07:47 +0100 | [diff] [blame] | 764 | |
| 765 | if (cpuc->enabled) |
| 766 | return; |
| 767 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 768 | if (cpuc->n_added) { |
Peter Zijlstra | 19925ce | 2010-03-06 13:20:40 +0100 | [diff] [blame] | 769 | int n_running = cpuc->n_events - cpuc->n_added; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 770 | /* |
| 771 | * apply assignment obtained either from |
| 772 | * hw_perf_group_sched_in() or x86_pmu_enable() |
| 773 | * |
| 774 | * step1: save events moving to new counters |
| 775 | * step2: reprogram moved events into new counters |
| 776 | */ |
Peter Zijlstra | 19925ce | 2010-03-06 13:20:40 +0100 | [diff] [blame] | 777 | for (i = 0; i < n_running; i++) { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 778 | |
| 779 | event = cpuc->event_list[i]; |
| 780 | hwc = &event->hw; |
| 781 | |
Stephane Eranian | 447a194 | 2010-02-01 14:50:01 +0200 | [diff] [blame] | 782 | /* |
| 783 | * we can avoid reprogramming counter if: |
| 784 | * - assigned same counter as last time |
| 785 | * - running on same CPU as last time |
| 786 | * - no other event has used the counter since |
| 787 | */ |
| 788 | if (hwc->idx == -1 || |
| 789 | match_prev_assignment(hwc, cpuc, i)) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 790 | continue; |
| 791 | |
Stephane Eranian | d76a081 | 2010-02-08 17:06:01 +0200 | [diff] [blame] | 792 | x86_pmu_stop(event); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 793 | |
| 794 | hwc->idx = -1; |
| 795 | } |
| 796 | |
| 797 | for (i = 0; i < cpuc->n_events; i++) { |
| 798 | |
| 799 | event = cpuc->event_list[i]; |
| 800 | hwc = &event->hw; |
| 801 | |
Peter Zijlstra | f3d46b2 | 2010-03-06 13:24:58 +0100 | [diff] [blame] | 802 | if (i < n_running && |
| 803 | match_prev_assignment(hwc, cpuc, i)) |
| 804 | continue; |
| 805 | |
Peter Zijlstra | c08053e | 2010-03-06 13:19:24 +0100 | [diff] [blame] | 806 | if (hwc->idx == -1) |
Stephane Eranian | 447a194 | 2010-02-01 14:50:01 +0200 | [diff] [blame] | 807 | x86_assign_hw_event(event, cpuc, i); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 808 | |
Peter Zijlstra | c08053e | 2010-03-06 13:19:24 +0100 | [diff] [blame] | 809 | x86_pmu_start(event); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 810 | } |
| 811 | cpuc->n_added = 0; |
| 812 | perf_events_lapic_init(); |
| 813 | } |
Peter Zijlstra | 1a6e21f | 2010-01-27 23:07:47 +0100 | [diff] [blame] | 814 | |
| 815 | cpuc->enabled = 1; |
| 816 | barrier(); |
| 817 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 818 | x86_pmu.enable_all(); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 819 | } |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 820 | |
Peter Zijlstra | aff3d91 | 2010-03-02 20:32:08 +0100 | [diff] [blame] | 821 | static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 822 | { |
Peter Zijlstra | aff3d91 | 2010-03-02 20:32:08 +0100 | [diff] [blame] | 823 | (void)checking_wrmsrl(hwc->config_base + hwc->idx, |
Robert Richter | bb1165d | 2010-03-01 14:21:23 +0100 | [diff] [blame] | 824 | hwc->config | ARCH_PERFMON_EVENTSEL_ENABLE); |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 825 | } |
| 826 | |
Peter Zijlstra | aff3d91 | 2010-03-02 20:32:08 +0100 | [diff] [blame] | 827 | static inline void x86_pmu_disable_event(struct perf_event *event) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 828 | { |
Peter Zijlstra | aff3d91 | 2010-03-02 20:32:08 +0100 | [diff] [blame] | 829 | struct hw_perf_event *hwc = &event->hw; |
| 830 | (void)checking_wrmsrl(hwc->config_base + hwc->idx, hwc->config); |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 831 | } |
| 832 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 833 | static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 834 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 835 | /* |
| 836 | * Set the next IRQ period, based on the hwc->period_left value. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 837 | * To be called with the event disabled in hw: |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 838 | */ |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 839 | static int |
Peter Zijlstra | 07088ed | 2010-03-02 20:16:01 +0100 | [diff] [blame] | 840 | x86_perf_event_set_period(struct perf_event *event) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 841 | { |
Peter Zijlstra | 07088ed | 2010-03-02 20:16:01 +0100 | [diff] [blame] | 842 | struct hw_perf_event *hwc = &event->hw; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 843 | s64 left = atomic64_read(&hwc->period_left); |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 844 | s64 period = hwc->sample_period; |
Peter Zijlstra | 07088ed | 2010-03-02 20:16:01 +0100 | [diff] [blame] | 845 | int err, ret = 0, idx = hwc->idx; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 846 | |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 847 | if (idx == X86_PMC_IDX_FIXED_BTS) |
| 848 | return 0; |
| 849 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 850 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 851 | * If we are way outside a reasonable range then just skip forward: |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 852 | */ |
| 853 | if (unlikely(left <= -period)) { |
| 854 | left = period; |
| 855 | atomic64_set(&hwc->period_left, left); |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 856 | hwc->last_period = period; |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 857 | ret = 1; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | if (unlikely(left <= 0)) { |
| 861 | left += period; |
| 862 | atomic64_set(&hwc->period_left, left); |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 863 | hwc->last_period = period; |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 864 | ret = 1; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 865 | } |
Ingo Molnar | 1c80f4b | 2009-05-15 08:25:22 +0200 | [diff] [blame] | 866 | /* |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 867 | * 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] | 868 | */ |
| 869 | if (unlikely(left < 2)) |
| 870 | left = 2; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 871 | |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 872 | if (left > x86_pmu.max_period) |
| 873 | left = x86_pmu.max_period; |
| 874 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 875 | per_cpu(pmc_prev_left[idx], smp_processor_id()) = left; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 876 | |
| 877 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 878 | * The hw event starts counting from this event offset, |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 879 | * mark it to be able to extra future deltas: |
| 880 | */ |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 881 | atomic64_set(&hwc->prev_count, (u64)-left); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 882 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 883 | err = checking_wrmsrl(hwc->event_base + idx, |
| 884 | (u64)(-left) & x86_pmu.event_mask); |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 885 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 886 | perf_event_update_userpage(event); |
Peter Zijlstra | 194002b | 2009-06-22 16:35:24 +0200 | [diff] [blame] | 887 | |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 888 | return ret; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 889 | } |
| 890 | |
Peter Zijlstra | aff3d91 | 2010-03-02 20:32:08 +0100 | [diff] [blame] | 891 | static void x86_pmu_enable_event(struct perf_event *event) |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 892 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 893 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 894 | if (cpuc->enabled) |
Peter Zijlstra | aff3d91 | 2010-03-02 20:32:08 +0100 | [diff] [blame] | 895 | __x86_pmu_enable_event(&event->hw); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 896 | } |
| 897 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 898 | /* |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 899 | * activate a single event |
| 900 | * |
| 901 | * The event is added to the group of enabled events |
| 902 | * but only if it can be scehduled with existing events. |
| 903 | * |
| 904 | * Called with PMU disabled. If successful and return value 1, |
| 905 | * then guaranteed to call perf_enable() and hw_perf_enable() |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 906 | */ |
| 907 | static int x86_pmu_enable(struct perf_event *event) |
| 908 | { |
| 909 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 910 | struct hw_perf_event *hwc; |
| 911 | int assign[X86_PMC_IDX_MAX]; |
| 912 | int n, n0, ret; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 913 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 914 | hwc = &event->hw; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 915 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 916 | n0 = cpuc->n_events; |
| 917 | n = collect_events(cpuc, event, false); |
| 918 | if (n < 0) |
| 919 | return n; |
Ingo Molnar | 53b441a | 2009-05-25 21:41:28 +0200 | [diff] [blame] | 920 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 921 | ret = x86_schedule_events(cpuc, n, assign); |
| 922 | if (ret) |
| 923 | return ret; |
| 924 | /* |
| 925 | * copy new assignment, now we know it is possible |
| 926 | * will be used by hw_perf_enable() |
| 927 | */ |
| 928 | memcpy(cpuc->assign, assign, n*sizeof(int)); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 929 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 930 | cpuc->n_events = n; |
Peter Zijlstra | 356e1f2 | 2010-03-06 13:49:56 +0100 | [diff] [blame] | 931 | cpuc->n_added += n - n0; |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 932 | |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 933 | return 0; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 934 | } |
| 935 | |
Stephane Eranian | d76a081 | 2010-02-08 17:06:01 +0200 | [diff] [blame] | 936 | static int x86_pmu_start(struct perf_event *event) |
| 937 | { |
Peter Zijlstra | c08053e | 2010-03-06 13:19:24 +0100 | [diff] [blame] | 938 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 939 | int idx = event->hw.idx; |
| 940 | |
| 941 | if (idx == -1) |
Stephane Eranian | d76a081 | 2010-02-08 17:06:01 +0200 | [diff] [blame] | 942 | return -EAGAIN; |
| 943 | |
Peter Zijlstra | 07088ed | 2010-03-02 20:16:01 +0100 | [diff] [blame] | 944 | x86_perf_event_set_period(event); |
Peter Zijlstra | c08053e | 2010-03-06 13:19:24 +0100 | [diff] [blame] | 945 | cpuc->events[idx] = event; |
| 946 | __set_bit(idx, cpuc->active_mask); |
Peter Zijlstra | aff3d91 | 2010-03-02 20:32:08 +0100 | [diff] [blame] | 947 | x86_pmu.enable(event); |
Peter Zijlstra | c08053e | 2010-03-06 13:19:24 +0100 | [diff] [blame] | 948 | perf_event_update_userpage(event); |
Stephane Eranian | d76a081 | 2010-02-08 17:06:01 +0200 | [diff] [blame] | 949 | |
| 950 | return 0; |
| 951 | } |
| 952 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 953 | static void x86_pmu_unthrottle(struct perf_event *event) |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 954 | { |
Peter Zijlstra | 71e2d28 | 2010-03-08 17:51:33 +0100 | [diff] [blame] | 955 | int ret = x86_pmu_start(event); |
| 956 | WARN_ON_ONCE(ret); |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 957 | } |
| 958 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 959 | void perf_event_print_debug(void) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 960 | { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 961 | u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 962 | u64 pebs; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 963 | struct cpu_hw_events *cpuc; |
Peter Zijlstra | 5bb9efe | 2009-05-13 08:12:51 +0200 | [diff] [blame] | 964 | unsigned long flags; |
Ingo Molnar | 1e12567 | 2008-12-09 12:18:18 +0100 | [diff] [blame] | 965 | int cpu, idx; |
| 966 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 967 | if (!x86_pmu.num_events) |
Ingo Molnar | 1e12567 | 2008-12-09 12:18:18 +0100 | [diff] [blame] | 968 | return; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 969 | |
Peter Zijlstra | 5bb9efe | 2009-05-13 08:12:51 +0200 | [diff] [blame] | 970 | local_irq_save(flags); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 971 | |
| 972 | cpu = smp_processor_id(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 973 | cpuc = &per_cpu(cpu_hw_events, cpu); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 974 | |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 975 | if (x86_pmu.version >= 2) { |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 976 | rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl); |
| 977 | rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status); |
| 978 | rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow); |
| 979 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 980 | rdmsrl(MSR_IA32_PEBS_ENABLE, pebs); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 981 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 982 | pr_info("\n"); |
| 983 | pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl); |
| 984 | pr_info("CPU#%d: status: %016llx\n", cpu, status); |
| 985 | pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow); |
| 986 | pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 987 | pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 988 | } |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 989 | pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 990 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 991 | for (idx = 0; idx < x86_pmu.num_events; idx++) { |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 992 | rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl); |
| 993 | rdmsrl(x86_pmu.perfctr + idx, pmc_count); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 994 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 995 | prev_left = per_cpu(pmc_prev_left[idx], cpu); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 996 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 997 | pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n", |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 998 | cpu, idx, pmc_ctrl); |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 999 | pr_info("CPU#%d: gen-PMC%d count: %016llx\n", |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1000 | cpu, idx, pmc_count); |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1001 | pr_info("CPU#%d: gen-PMC%d left: %016llx\n", |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1002 | cpu, idx, prev_left); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1003 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1004 | for (idx = 0; idx < x86_pmu.num_events_fixed; idx++) { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1005 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count); |
| 1006 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1007 | pr_info("CPU#%d: fixed-PMC%d count: %016llx\n", |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1008 | cpu, idx, pmc_count); |
| 1009 | } |
Peter Zijlstra | 5bb9efe | 2009-05-13 08:12:51 +0200 | [diff] [blame] | 1010 | local_irq_restore(flags); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1011 | } |
| 1012 | |
Stephane Eranian | d76a081 | 2010-02-08 17:06:01 +0200 | [diff] [blame] | 1013 | static void x86_pmu_stop(struct perf_event *event) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1014 | { |
Stephane Eranian | d76a081 | 2010-02-08 17:06:01 +0200 | [diff] [blame] | 1015 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1016 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | 2e84187 | 2010-01-25 15:58:43 +0100 | [diff] [blame] | 1017 | int idx = hwc->idx; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1018 | |
Peter Zijlstra | 71e2d28 | 2010-03-08 17:51:33 +0100 | [diff] [blame] | 1019 | if (!__test_and_clear_bit(idx, cpuc->active_mask)) |
| 1020 | return; |
| 1021 | |
Peter Zijlstra | aff3d91 | 2010-03-02 20:32:08 +0100 | [diff] [blame] | 1022 | x86_pmu.disable(event); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1023 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1024 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1025 | * Drain the remaining delta count out of a event |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1026 | * that we are disabling: |
| 1027 | */ |
Peter Zijlstra | cc2ad4b | 2010-03-02 20:18:39 +0100 | [diff] [blame] | 1028 | x86_perf_event_update(event); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1029 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1030 | cpuc->events[idx] = NULL; |
Peter Zijlstra | 2e84187 | 2010-01-25 15:58:43 +0100 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | static void x86_pmu_disable(struct perf_event *event) |
| 1034 | { |
| 1035 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 1036 | int i; |
| 1037 | |
Stephane Eranian | d76a081 | 2010-02-08 17:06:01 +0200 | [diff] [blame] | 1038 | x86_pmu_stop(event); |
Peter Zijlstra | 194002b | 2009-06-22 16:35:24 +0200 | [diff] [blame] | 1039 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1040 | for (i = 0; i < cpuc->n_events; i++) { |
| 1041 | if (event == cpuc->event_list[i]) { |
| 1042 | |
| 1043 | if (x86_pmu.put_event_constraints) |
| 1044 | x86_pmu.put_event_constraints(cpuc, event); |
| 1045 | |
| 1046 | while (++i < cpuc->n_events) |
| 1047 | cpuc->event_list[i-1] = cpuc->event_list[i]; |
| 1048 | |
| 1049 | --cpuc->n_events; |
Peter Zijlstra | 6c9687a | 2010-01-25 11:57:25 +0100 | [diff] [blame] | 1050 | break; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1051 | } |
| 1052 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1053 | perf_event_update_userpage(event); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1054 | } |
| 1055 | |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 1056 | static int x86_pmu_handle_irq(struct pt_regs *regs) |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 1057 | { |
Peter Zijlstra | df1a132 | 2009-06-10 21:02:22 +0200 | [diff] [blame] | 1058 | struct perf_sample_data data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1059 | struct cpu_hw_events *cpuc; |
| 1060 | struct perf_event *event; |
| 1061 | struct hw_perf_event *hwc; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1062 | int idx, handled = 0; |
Ingo Molnar | 9029a5e | 2009-05-15 08:26:20 +0200 | [diff] [blame] | 1063 | u64 val; |
| 1064 | |
Peter Zijlstra | dc1d628 | 2010-03-03 15:55:04 +0100 | [diff] [blame] | 1065 | perf_sample_data_init(&data, 0); |
Peter Zijlstra | df1a132 | 2009-06-10 21:02:22 +0200 | [diff] [blame] | 1066 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1067 | cpuc = &__get_cpu_var(cpu_hw_events); |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 1068 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1069 | for (idx = 0; idx < x86_pmu.num_events; idx++) { |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 1070 | if (!test_bit(idx, cpuc->active_mask)) |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 1071 | continue; |
Peter Zijlstra | 962bf7a | 2009-05-13 13:21:36 +0200 | [diff] [blame] | 1072 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1073 | event = cpuc->events[idx]; |
| 1074 | hwc = &event->hw; |
Peter Zijlstra | a4016a7 | 2009-05-14 14:52:17 +0200 | [diff] [blame] | 1075 | |
Peter Zijlstra | cc2ad4b | 2010-03-02 20:18:39 +0100 | [diff] [blame] | 1076 | val = x86_perf_event_update(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1077 | if (val & (1ULL << (x86_pmu.event_bits - 1))) |
Peter Zijlstra | 48e22d5 | 2009-05-25 17:39:04 +0200 | [diff] [blame] | 1078 | continue; |
Peter Zijlstra | 962bf7a | 2009-05-13 13:21:36 +0200 | [diff] [blame] | 1079 | |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1080 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1081 | * event overflow |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1082 | */ |
| 1083 | handled = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1084 | data.period = event->hw.last_period; |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1085 | |
Peter Zijlstra | 07088ed | 2010-03-02 20:16:01 +0100 | [diff] [blame] | 1086 | if (!x86_perf_event_set_period(event)) |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1087 | continue; |
| 1088 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1089 | if (perf_event_overflow(event, 1, &data, regs)) |
Peter Zijlstra | 71e2d28 | 2010-03-08 17:51:33 +0100 | [diff] [blame] | 1090 | x86_pmu_stop(event); |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 1091 | } |
Peter Zijlstra | 962bf7a | 2009-05-13 13:21:36 +0200 | [diff] [blame] | 1092 | |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1093 | if (handled) |
| 1094 | inc_irq_stat(apic_perf_irqs); |
| 1095 | |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 1096 | return handled; |
| 1097 | } |
Robert Richter | 39d81ea | 2009-04-29 12:47:05 +0200 | [diff] [blame] | 1098 | |
Peter Zijlstra | b6276f3 | 2009-04-06 11:45:03 +0200 | [diff] [blame] | 1099 | void smp_perf_pending_interrupt(struct pt_regs *regs) |
| 1100 | { |
| 1101 | irq_enter(); |
| 1102 | ack_APIC_irq(); |
| 1103 | inc_irq_stat(apic_pending_irqs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1104 | perf_event_do_pending(); |
Peter Zijlstra | b6276f3 | 2009-04-06 11:45:03 +0200 | [diff] [blame] | 1105 | irq_exit(); |
| 1106 | } |
| 1107 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1108 | void set_perf_event_pending(void) |
Peter Zijlstra | b6276f3 | 2009-04-06 11:45:03 +0200 | [diff] [blame] | 1109 | { |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1110 | #ifdef CONFIG_X86_LOCAL_APIC |
Peter Zijlstra | 7d42896 | 2009-09-23 11:03:37 +0200 | [diff] [blame] | 1111 | if (!x86_pmu.apic || !x86_pmu_initialized()) |
| 1112 | return; |
| 1113 | |
Peter Zijlstra | b6276f3 | 2009-04-06 11:45:03 +0200 | [diff] [blame] | 1114 | apic->send_IPI_self(LOCAL_PENDING_VECTOR); |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1115 | #endif |
Peter Zijlstra | b6276f3 | 2009-04-06 11:45:03 +0200 | [diff] [blame] | 1116 | } |
| 1117 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1118 | void perf_events_lapic_init(void) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1119 | { |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1120 | #ifdef CONFIG_X86_LOCAL_APIC |
| 1121 | if (!x86_pmu.apic || !x86_pmu_initialized()) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1122 | return; |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 1123 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1124 | /* |
Yong Wang | c323d95 | 2009-05-29 13:28:35 +0800 | [diff] [blame] | 1125 | * Always use NMI for PMU |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1126 | */ |
Yong Wang | c323d95 | 2009-05-29 13:28:35 +0800 | [diff] [blame] | 1127 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1128 | #endif |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | static int __kprobes |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1132 | perf_event_nmi_handler(struct notifier_block *self, |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1133 | unsigned long cmd, void *__args) |
| 1134 | { |
| 1135 | struct die_args *args = __args; |
| 1136 | struct pt_regs *regs; |
| 1137 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1138 | if (!atomic_read(&active_events)) |
Peter Zijlstra | 63a809a | 2009-05-01 12:23:17 +0200 | [diff] [blame] | 1139 | return NOTIFY_DONE; |
| 1140 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1141 | switch (cmd) { |
| 1142 | case DIE_NMI: |
| 1143 | case DIE_NMI_IPI: |
| 1144 | break; |
| 1145 | |
| 1146 | default: |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1147 | return NOTIFY_DONE; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1148 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1149 | |
| 1150 | regs = args->regs; |
| 1151 | |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1152 | #ifdef CONFIG_X86_LOCAL_APIC |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1153 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1154 | #endif |
Peter Zijlstra | a4016a7 | 2009-05-14 14:52:17 +0200 | [diff] [blame] | 1155 | /* |
| 1156 | * Can't rely on the handled return value to say it was our NMI, two |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1157 | * events could trigger 'simultaneously' raising two back-to-back NMIs. |
Peter Zijlstra | a4016a7 | 2009-05-14 14:52:17 +0200 | [diff] [blame] | 1158 | * |
| 1159 | * If the first NMI handles both, the latter will be empty and daze |
| 1160 | * the CPU. |
| 1161 | */ |
Yong Wang | a328810 | 2009-06-03 13:12:55 +0800 | [diff] [blame] | 1162 | x86_pmu.handle_irq(regs); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1163 | |
Peter Zijlstra | a4016a7 | 2009-05-14 14:52:17 +0200 | [diff] [blame] | 1164 | return NOTIFY_STOP; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1165 | } |
| 1166 | |
Peter Zijlstra | f22f54f | 2010-02-26 12:05:05 +0100 | [diff] [blame] | 1167 | static __read_mostly struct notifier_block perf_event_nmi_notifier = { |
| 1168 | .notifier_call = perf_event_nmi_handler, |
| 1169 | .next = NULL, |
| 1170 | .priority = 1 |
| 1171 | }; |
| 1172 | |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1173 | static struct event_constraint unconstrained; |
Stephane Eranian | 38331f6 | 2010-02-08 17:17:01 +0200 | [diff] [blame] | 1174 | static struct event_constraint emptyconstraint; |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1175 | |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1176 | static struct event_constraint * |
Peter Zijlstra | f22f54f | 2010-02-26 12:05:05 +0100 | [diff] [blame] | 1177 | 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] | 1178 | { |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1179 | struct event_constraint *c; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1180 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1181 | if (x86_pmu.event_constraints) { |
| 1182 | for_each_event_constraint(c, x86_pmu.event_constraints) { |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1183 | if ((event->hw.config & c->cmask) == c->code) |
| 1184 | return c; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1185 | } |
| 1186 | } |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1187 | |
| 1188 | return &unconstrained; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1189 | } |
| 1190 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1191 | static int x86_event_sched_in(struct perf_event *event, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1192 | struct perf_cpu_context *cpuctx) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1193 | { |
| 1194 | int ret = 0; |
| 1195 | |
| 1196 | event->state = PERF_EVENT_STATE_ACTIVE; |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1197 | event->oncpu = smp_processor_id(); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1198 | event->tstamp_running += event->ctx->time - event->tstamp_stopped; |
| 1199 | |
| 1200 | if (!is_x86_event(event)) |
| 1201 | ret = event->pmu->enable(event); |
| 1202 | |
| 1203 | if (!ret && !is_software_event(event)) |
| 1204 | cpuctx->active_oncpu++; |
| 1205 | |
| 1206 | if (!ret && event->attr.exclusive) |
| 1207 | cpuctx->exclusive = 1; |
| 1208 | |
| 1209 | return ret; |
| 1210 | } |
| 1211 | |
| 1212 | static void x86_event_sched_out(struct perf_event *event, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1213 | struct perf_cpu_context *cpuctx) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1214 | { |
| 1215 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 1216 | event->oncpu = -1; |
| 1217 | |
| 1218 | if (!is_x86_event(event)) |
| 1219 | event->pmu->disable(event); |
| 1220 | |
| 1221 | event->tstamp_running -= event->ctx->time - event->tstamp_stopped; |
| 1222 | |
| 1223 | if (!is_software_event(event)) |
| 1224 | cpuctx->active_oncpu--; |
| 1225 | |
| 1226 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
| 1227 | cpuctx->exclusive = 0; |
| 1228 | } |
| 1229 | |
| 1230 | /* |
| 1231 | * Called to enable a whole group of events. |
| 1232 | * Returns 1 if the group was enabled, or -EAGAIN if it could not be. |
| 1233 | * Assumes the caller has disabled interrupts and has |
| 1234 | * frozen the PMU with hw_perf_save_disable. |
| 1235 | * |
| 1236 | * called with PMU disabled. If successful and return value 1, |
| 1237 | * then guaranteed to call perf_enable() and hw_perf_enable() |
| 1238 | */ |
| 1239 | int hw_perf_group_sched_in(struct perf_event *leader, |
| 1240 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1241 | struct perf_event_context *ctx) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1242 | { |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1243 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1244 | struct perf_event *sub; |
| 1245 | int assign[X86_PMC_IDX_MAX]; |
| 1246 | int n0, n1, ret; |
| 1247 | |
| 1248 | /* n0 = total number of events */ |
| 1249 | n0 = collect_events(cpuc, leader, true); |
| 1250 | if (n0 < 0) |
| 1251 | return n0; |
| 1252 | |
| 1253 | ret = x86_schedule_events(cpuc, n0, assign); |
| 1254 | if (ret) |
| 1255 | return ret; |
| 1256 | |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1257 | ret = x86_event_sched_in(leader, cpuctx); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1258 | if (ret) |
| 1259 | return ret; |
| 1260 | |
| 1261 | n1 = 1; |
| 1262 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 1263 | if (sub->state > PERF_EVENT_STATE_OFF) { |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1264 | ret = x86_event_sched_in(sub, cpuctx); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1265 | if (ret) |
| 1266 | goto undo; |
| 1267 | ++n1; |
| 1268 | } |
| 1269 | } |
| 1270 | /* |
| 1271 | * copy new assignment, now we know it is possible |
| 1272 | * will be used by hw_perf_enable() |
| 1273 | */ |
| 1274 | memcpy(cpuc->assign, assign, n0*sizeof(int)); |
| 1275 | |
| 1276 | cpuc->n_events = n0; |
Peter Zijlstra | 356e1f2 | 2010-03-06 13:49:56 +0100 | [diff] [blame] | 1277 | cpuc->n_added += n1; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1278 | ctx->nr_active += n1; |
| 1279 | |
| 1280 | /* |
| 1281 | * 1 means successful and events are active |
| 1282 | * This is not quite true because we defer |
| 1283 | * actual activation until hw_perf_enable() but |
| 1284 | * this way we* ensure caller won't try to enable |
| 1285 | * individual events |
| 1286 | */ |
| 1287 | return 1; |
| 1288 | undo: |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1289 | x86_event_sched_out(leader, cpuctx); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1290 | n0 = 1; |
| 1291 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
| 1292 | if (sub->state == PERF_EVENT_STATE_ACTIVE) { |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1293 | x86_event_sched_out(sub, cpuctx); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1294 | if (++n0 == n1) |
| 1295 | break; |
| 1296 | } |
| 1297 | } |
| 1298 | return ret; |
| 1299 | } |
| 1300 | |
Peter Zijlstra | f22f54f | 2010-02-26 12:05:05 +0100 | [diff] [blame] | 1301 | #include "perf_event_amd.c" |
| 1302 | #include "perf_event_p6.c" |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 1303 | #include "perf_event_intel_lbr.c" |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1304 | #include "perf_event_intel_ds.c" |
Peter Zijlstra | f22f54f | 2010-02-26 12:05:05 +0100 | [diff] [blame] | 1305 | #include "perf_event_intel.c" |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1306 | |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1307 | static int __cpuinit |
| 1308 | x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu) |
| 1309 | { |
| 1310 | unsigned int cpu = (long)hcpu; |
| 1311 | |
| 1312 | switch (action & ~CPU_TASKS_FROZEN) { |
| 1313 | case CPU_UP_PREPARE: |
| 1314 | if (x86_pmu.cpu_prepare) |
| 1315 | x86_pmu.cpu_prepare(cpu); |
| 1316 | break; |
| 1317 | |
| 1318 | case CPU_STARTING: |
| 1319 | if (x86_pmu.cpu_starting) |
| 1320 | x86_pmu.cpu_starting(cpu); |
| 1321 | break; |
| 1322 | |
| 1323 | case CPU_DYING: |
| 1324 | if (x86_pmu.cpu_dying) |
| 1325 | x86_pmu.cpu_dying(cpu); |
| 1326 | break; |
| 1327 | |
| 1328 | case CPU_DEAD: |
| 1329 | if (x86_pmu.cpu_dead) |
| 1330 | x86_pmu.cpu_dead(cpu); |
| 1331 | break; |
| 1332 | |
| 1333 | default: |
| 1334 | break; |
| 1335 | } |
| 1336 | |
| 1337 | return NOTIFY_OK; |
| 1338 | } |
| 1339 | |
Cyrill Gorcunov | 1255803 | 2009-12-10 19:56:34 +0300 | [diff] [blame] | 1340 | static void __init pmu_check_apic(void) |
| 1341 | { |
| 1342 | if (cpu_has_apic) |
| 1343 | return; |
| 1344 | |
| 1345 | x86_pmu.apic = 0; |
| 1346 | pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n"); |
| 1347 | pr_info("no hardware sampling interrupt available.\n"); |
| 1348 | } |
| 1349 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1350 | void __init init_hw_perf_events(void) |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1351 | { |
Peter Zijlstra | b622d64 | 2010-02-01 15:36:30 +0100 | [diff] [blame] | 1352 | struct event_constraint *c; |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 1353 | int err; |
| 1354 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1355 | pr_info("Performance Events: "); |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1356 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1357 | switch (boot_cpu_data.x86_vendor) { |
| 1358 | case X86_VENDOR_INTEL: |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 1359 | err = intel_pmu_init(); |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1360 | break; |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1361 | case X86_VENDOR_AMD: |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 1362 | err = amd_pmu_init(); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1363 | break; |
Robert Richter | 4138960 | 2009-04-29 12:47:00 +0200 | [diff] [blame] | 1364 | default: |
| 1365 | return; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1366 | } |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1367 | if (err != 0) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1368 | pr_cont("no PMU driver, software events only.\n"); |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1369 | return; |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1370 | } |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1371 | |
Cyrill Gorcunov | 1255803 | 2009-12-10 19:56:34 +0300 | [diff] [blame] | 1372 | pmu_check_apic(); |
| 1373 | |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1374 | pr_cont("%s PMU driver.\n", x86_pmu.name); |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 1375 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1376 | if (x86_pmu.num_events > X86_PMC_MAX_GENERIC) { |
| 1377 | WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!", |
| 1378 | x86_pmu.num_events, X86_PMC_MAX_GENERIC); |
| 1379 | x86_pmu.num_events = X86_PMC_MAX_GENERIC; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1380 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1381 | perf_event_mask = (1 << x86_pmu.num_events) - 1; |
| 1382 | perf_max_events = x86_pmu.num_events; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1383 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1384 | if (x86_pmu.num_events_fixed > X86_PMC_MAX_FIXED) { |
| 1385 | WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!", |
| 1386 | x86_pmu.num_events_fixed, X86_PMC_MAX_FIXED); |
| 1387 | x86_pmu.num_events_fixed = X86_PMC_MAX_FIXED; |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 1388 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1389 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1390 | perf_event_mask |= |
| 1391 | ((1LL << x86_pmu.num_events_fixed)-1) << X86_PMC_IDX_FIXED; |
| 1392 | x86_pmu.intel_ctrl = perf_event_mask; |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 1393 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1394 | perf_events_lapic_init(); |
| 1395 | register_die_notifier(&perf_event_nmi_notifier); |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1396 | |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1397 | unconstrained = (struct event_constraint) |
Peter Zijlstra | fce877e | 2010-01-29 13:25:12 +0100 | [diff] [blame] | 1398 | __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_events) - 1, |
| 1399 | 0, x86_pmu.num_events); |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1400 | |
Peter Zijlstra | b622d64 | 2010-02-01 15:36:30 +0100 | [diff] [blame] | 1401 | if (x86_pmu.event_constraints) { |
| 1402 | for_each_event_constraint(c, x86_pmu.event_constraints) { |
| 1403 | if (c->cmask != INTEL_ARCH_FIXED_MASK) |
| 1404 | continue; |
| 1405 | |
| 1406 | c->idxmsk64 |= (1ULL << x86_pmu.num_events) - 1; |
| 1407 | c->weight += x86_pmu.num_events; |
| 1408 | } |
| 1409 | } |
| 1410 | |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 1411 | pr_info("... version: %d\n", x86_pmu.version); |
| 1412 | pr_info("... bit width: %d\n", x86_pmu.event_bits); |
| 1413 | pr_info("... generic registers: %d\n", x86_pmu.num_events); |
| 1414 | pr_info("... value mask: %016Lx\n", x86_pmu.event_mask); |
| 1415 | pr_info("... max period: %016Lx\n", x86_pmu.max_period); |
| 1416 | pr_info("... fixed-purpose events: %d\n", x86_pmu.num_events_fixed); |
| 1417 | pr_info("... event mask: %016Lx\n", perf_event_mask); |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1418 | |
| 1419 | perf_cpu_notifier(x86_pmu_notifier); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1420 | } |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1421 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1422 | static inline void x86_pmu_read(struct perf_event *event) |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1423 | { |
Peter Zijlstra | cc2ad4b | 2010-03-02 20:18:39 +0100 | [diff] [blame] | 1424 | x86_perf_event_update(event); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1425 | } |
| 1426 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 1427 | static const struct pmu pmu = { |
| 1428 | .enable = x86_pmu_enable, |
| 1429 | .disable = x86_pmu_disable, |
Stephane Eranian | d76a081 | 2010-02-08 17:06:01 +0200 | [diff] [blame] | 1430 | .start = x86_pmu_start, |
| 1431 | .stop = x86_pmu_stop, |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 1432 | .read = x86_pmu_read, |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 1433 | .unthrottle = x86_pmu_unthrottle, |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1434 | }; |
| 1435 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1436 | /* |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1437 | * validate that we can schedule this event |
| 1438 | */ |
| 1439 | static int validate_event(struct perf_event *event) |
| 1440 | { |
| 1441 | struct cpu_hw_events *fake_cpuc; |
| 1442 | struct event_constraint *c; |
| 1443 | int ret = 0; |
| 1444 | |
| 1445 | fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO); |
| 1446 | if (!fake_cpuc) |
| 1447 | return -ENOMEM; |
| 1448 | |
| 1449 | c = x86_pmu.get_event_constraints(fake_cpuc, event); |
| 1450 | |
| 1451 | if (!c || !c->weight) |
| 1452 | ret = -ENOSPC; |
| 1453 | |
| 1454 | if (x86_pmu.put_event_constraints) |
| 1455 | x86_pmu.put_event_constraints(fake_cpuc, event); |
| 1456 | |
| 1457 | kfree(fake_cpuc); |
| 1458 | |
| 1459 | return ret; |
| 1460 | } |
| 1461 | |
| 1462 | /* |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1463 | * validate a single event group |
| 1464 | * |
| 1465 | * validation include: |
Ingo Molnar | 184f412 | 2010-01-27 08:39:39 +0100 | [diff] [blame] | 1466 | * - check events are compatible which each other |
| 1467 | * - events do not compete for the same counter |
| 1468 | * - number of events <= number of counters |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1469 | * |
| 1470 | * validation ensures the group can be loaded onto the |
| 1471 | * PMU if it was the only group available. |
| 1472 | */ |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1473 | static int validate_group(struct perf_event *event) |
| 1474 | { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1475 | struct perf_event *leader = event->group_leader; |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1476 | struct cpu_hw_events *fake_cpuc; |
| 1477 | int ret, n; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1478 | |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1479 | ret = -ENOMEM; |
| 1480 | fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO); |
| 1481 | if (!fake_cpuc) |
| 1482 | goto out; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1483 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1484 | /* |
| 1485 | * the event is not yet connected with its |
| 1486 | * siblings therefore we must first collect |
| 1487 | * existing siblings, then add the new event |
| 1488 | * before we can simulate the scheduling |
| 1489 | */ |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1490 | ret = -ENOSPC; |
| 1491 | n = collect_events(fake_cpuc, leader, true); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1492 | if (n < 0) |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1493 | goto out_free; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1494 | |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1495 | fake_cpuc->n_events = n; |
| 1496 | n = collect_events(fake_cpuc, event, false); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1497 | if (n < 0) |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1498 | goto out_free; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1499 | |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1500 | fake_cpuc->n_events = n; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1501 | |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1502 | ret = x86_schedule_events(fake_cpuc, n, NULL); |
| 1503 | |
| 1504 | out_free: |
| 1505 | kfree(fake_cpuc); |
| 1506 | out: |
| 1507 | return ret; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1508 | } |
| 1509 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1510 | const struct pmu *hw_perf_event_init(struct perf_event *event) |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1511 | { |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 1512 | const struct pmu *tmp; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1513 | int err; |
| 1514 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1515 | err = __hw_perf_event_init(event); |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1516 | if (!err) { |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 1517 | /* |
| 1518 | * we temporarily connect event to its pmu |
| 1519 | * such that validate_group() can classify |
| 1520 | * it as an x86 event using is_x86_event() |
| 1521 | */ |
| 1522 | tmp = event->pmu; |
| 1523 | event->pmu = &pmu; |
| 1524 | |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1525 | if (event->group_leader != event) |
| 1526 | err = validate_group(event); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1527 | else |
| 1528 | err = validate_event(event); |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 1529 | |
| 1530 | event->pmu = tmp; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1531 | } |
Peter Zijlstra | a1792cdac | 2009-09-09 10:04:47 +0200 | [diff] [blame] | 1532 | if (err) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1533 | if (event->destroy) |
| 1534 | event->destroy(event); |
Peter Zijlstra | 9ea98e1 | 2009-03-30 19:07:09 +0200 | [diff] [blame] | 1535 | return ERR_PTR(err); |
Peter Zijlstra | a1792cdac | 2009-09-09 10:04:47 +0200 | [diff] [blame] | 1536 | } |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1537 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 1538 | return &pmu; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1539 | } |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1540 | |
| 1541 | /* |
| 1542 | * callchain support |
| 1543 | */ |
| 1544 | |
| 1545 | static inline |
Peter Zijlstra | f9188e0 | 2009-06-18 22:20:52 +0200 | [diff] [blame] | 1546 | void callchain_store(struct perf_callchain_entry *entry, u64 ip) |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1547 | { |
Peter Zijlstra | f9188e0 | 2009-06-18 22:20:52 +0200 | [diff] [blame] | 1548 | if (entry->nr < PERF_MAX_STACK_DEPTH) |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1549 | entry->ip[entry->nr++] = ip; |
| 1550 | } |
| 1551 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 1552 | static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_irq_entry); |
| 1553 | static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_nmi_entry); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1554 | |
| 1555 | |
| 1556 | static void |
| 1557 | backtrace_warning_symbol(void *data, char *msg, unsigned long symbol) |
| 1558 | { |
| 1559 | /* Ignore warnings */ |
| 1560 | } |
| 1561 | |
| 1562 | static void backtrace_warning(void *data, char *msg) |
| 1563 | { |
| 1564 | /* Ignore warnings */ |
| 1565 | } |
| 1566 | |
| 1567 | static int backtrace_stack(void *data, char *name) |
| 1568 | { |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 1569 | return 0; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | static void backtrace_address(void *data, unsigned long addr, int reliable) |
| 1573 | { |
| 1574 | struct perf_callchain_entry *entry = data; |
| 1575 | |
| 1576 | if (reliable) |
| 1577 | callchain_store(entry, addr); |
| 1578 | } |
| 1579 | |
| 1580 | static const struct stacktrace_ops backtrace_ops = { |
| 1581 | .warning = backtrace_warning, |
| 1582 | .warning_symbol = backtrace_warning_symbol, |
| 1583 | .stack = backtrace_stack, |
| 1584 | .address = backtrace_address, |
Frederic Weisbecker | 06d65bd | 2009-12-17 05:40:34 +0100 | [diff] [blame] | 1585 | .walk_stack = print_context_stack_bp, |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1586 | }; |
| 1587 | |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 1588 | #include "../dumpstack.h" |
| 1589 | |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1590 | static void |
| 1591 | perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry) |
| 1592 | { |
Peter Zijlstra | f9188e0 | 2009-06-18 22:20:52 +0200 | [diff] [blame] | 1593 | callchain_store(entry, PERF_CONTEXT_KERNEL); |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 1594 | callchain_store(entry, regs->ip); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1595 | |
Frederic Weisbecker | 48b5ba9 | 2009-12-31 05:53:02 +0100 | [diff] [blame] | 1596 | dump_trace(NULL, regs, NULL, regs->bp, &backtrace_ops, entry); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1597 | } |
| 1598 | |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 1599 | static int copy_stack_frame(const void __user *fp, struct stack_frame *frame) |
| 1600 | { |
| 1601 | unsigned long bytes; |
| 1602 | |
| 1603 | bytes = copy_from_user_nmi(frame, fp, sizeof(*frame)); |
| 1604 | |
| 1605 | return bytes == sizeof(*frame); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | static void |
| 1609 | perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry) |
| 1610 | { |
| 1611 | struct stack_frame frame; |
| 1612 | const void __user *fp; |
| 1613 | |
Ingo Molnar | 5a6cec3 | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1614 | if (!user_mode(regs)) |
| 1615 | regs = task_pt_regs(current); |
| 1616 | |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 1617 | fp = (void __user *)regs->bp; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1618 | |
Peter Zijlstra | f9188e0 | 2009-06-18 22:20:52 +0200 | [diff] [blame] | 1619 | callchain_store(entry, PERF_CONTEXT_USER); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1620 | callchain_store(entry, regs->ip); |
| 1621 | |
Peter Zijlstra | f9188e0 | 2009-06-18 22:20:52 +0200 | [diff] [blame] | 1622 | while (entry->nr < PERF_MAX_STACK_DEPTH) { |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 1623 | frame.next_frame = NULL; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1624 | frame.return_address = 0; |
| 1625 | |
| 1626 | if (!copy_stack_frame(fp, &frame)) |
| 1627 | break; |
| 1628 | |
Ingo Molnar | 5a6cec3 | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1629 | if ((unsigned long)fp < regs->sp) |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1630 | break; |
| 1631 | |
| 1632 | callchain_store(entry, frame.return_address); |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 1633 | fp = frame.next_frame; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | static void |
| 1638 | perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry) |
| 1639 | { |
| 1640 | int is_user; |
| 1641 | |
| 1642 | if (!regs) |
| 1643 | return; |
| 1644 | |
| 1645 | is_user = user_mode(regs); |
| 1646 | |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1647 | if (is_user && current->state != TASK_RUNNING) |
| 1648 | return; |
| 1649 | |
| 1650 | if (!is_user) |
| 1651 | perf_callchain_kernel(regs, entry); |
| 1652 | |
| 1653 | if (current->mm) |
| 1654 | perf_callchain_user(regs, entry); |
| 1655 | } |
| 1656 | |
| 1657 | struct perf_callchain_entry *perf_callchain(struct pt_regs *regs) |
| 1658 | { |
| 1659 | struct perf_callchain_entry *entry; |
| 1660 | |
| 1661 | if (in_nmi()) |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 1662 | entry = &__get_cpu_var(pmc_nmi_entry); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1663 | else |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 1664 | entry = &__get_cpu_var(pmc_irq_entry); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1665 | |
| 1666 | entry->nr = 0; |
| 1667 | |
| 1668 | perf_do_callchain(regs, entry); |
| 1669 | |
| 1670 | return entry; |
| 1671 | } |