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