Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Performance counter x86 architecture code |
| 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> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 9 | * |
| 10 | * For licencing details see kernel-base/COPYING |
| 11 | */ |
| 12 | |
| 13 | #include <linux/perf_counter.h> |
| 14 | #include <linux/capability.h> |
| 15 | #include <linux/notifier.h> |
| 16 | #include <linux/hardirq.h> |
| 17 | #include <linux/kprobes.h> |
Thomas Gleixner | 4ac1329 | 2008-12-09 21:43:39 +0100 | [diff] [blame] | 18 | #include <linux/module.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 19 | #include <linux/kdebug.h> |
| 20 | #include <linux/sched.h> |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 21 | #include <linux/uaccess.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 22 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 23 | #include <asm/apic.h> |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 24 | #include <asm/stacktrace.h> |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 25 | #include <asm/nmi.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 26 | |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 27 | static u64 perf_counter_mask __read_mostly; |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 28 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 29 | struct cpu_hw_counters { |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 30 | struct perf_counter *counters[X86_PMC_IDX_MAX]; |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 31 | unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
| 32 | unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
Mike Galbraith | 4b39fd9 | 2009-01-23 14:36:16 +0100 | [diff] [blame] | 33 | unsigned long interrupts; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 34 | int enabled; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | /* |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 38 | * struct x86_pmu - generic x86 pmu |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 39 | */ |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 40 | struct x86_pmu { |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 41 | const char *name; |
| 42 | int version; |
Robert Richter | 39d81ea | 2009-04-29 12:47:05 +0200 | [diff] [blame] | 43 | int (*handle_irq)(struct pt_regs *, int); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 44 | void (*disable_all)(void); |
| 45 | void (*enable_all)(void); |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 46 | void (*enable)(struct hw_perf_counter *, int); |
Robert Richter | d436989 | 2009-04-29 12:47:19 +0200 | [diff] [blame] | 47 | void (*disable)(struct hw_perf_counter *, int); |
Jaswinder Singh Rajput | 169e41e | 2009-02-28 18:37:49 +0530 | [diff] [blame] | 48 | unsigned eventsel; |
| 49 | unsigned perfctr; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 50 | u64 (*event_map)(int); |
| 51 | u64 (*raw_event)(u64); |
Jaswinder Singh Rajput | 169e41e | 2009-02-28 18:37:49 +0530 | [diff] [blame] | 52 | int max_events; |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 53 | int num_counters; |
| 54 | int num_counters_fixed; |
| 55 | int counter_bits; |
| 56 | u64 counter_mask; |
Robert Richter | c619b8f | 2009-04-29 12:47:23 +0200 | [diff] [blame] | 57 | u64 max_period; |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 58 | u64 intel_ctrl; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 59 | }; |
| 60 | |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 61 | static struct x86_pmu x86_pmu __read_mostly; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 62 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 63 | static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters) = { |
| 64 | .enabled = 1, |
| 65 | }; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 66 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 67 | /* |
| 68 | * Intel PerfMon v3. Used on Core2 and later. |
| 69 | */ |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 70 | static const u64 intel_perfmon_event_map[] = |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 71 | { |
Ingo Molnar | f650a67 | 2008-12-23 12:17:29 +0100 | [diff] [blame] | 72 | [PERF_COUNT_CPU_CYCLES] = 0x003c, |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 73 | [PERF_COUNT_INSTRUCTIONS] = 0x00c0, |
| 74 | [PERF_COUNT_CACHE_REFERENCES] = 0x4f2e, |
| 75 | [PERF_COUNT_CACHE_MISSES] = 0x412e, |
| 76 | [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4, |
| 77 | [PERF_COUNT_BRANCH_MISSES] = 0x00c5, |
Ingo Molnar | f650a67 | 2008-12-23 12:17:29 +0100 | [diff] [blame] | 78 | [PERF_COUNT_BUS_CYCLES] = 0x013c, |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 79 | }; |
| 80 | |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 81 | static u64 intel_pmu_event_map(int event) |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 82 | { |
| 83 | return intel_perfmon_event_map[event]; |
| 84 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 85 | |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 86 | static u64 intel_pmu_raw_event(u64 event) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 87 | { |
Peter Zijlstra | 82bae4f8 | 2009-03-13 12:21:31 +0100 | [diff] [blame] | 88 | #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL |
| 89 | #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL |
Peter Zijlstra | ff99be5 | 2009-05-25 17:39:03 +0200 | [diff] [blame] | 90 | #define CORE_EVNTSEL_EDGE_MASK 0x00040000ULL |
| 91 | #define CORE_EVNTSEL_INV_MASK 0x00800000ULL |
Peter Zijlstra | 82bae4f8 | 2009-03-13 12:21:31 +0100 | [diff] [blame] | 92 | #define CORE_EVNTSEL_COUNTER_MASK 0xFF000000ULL |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 93 | |
| 94 | #define CORE_EVNTSEL_MASK \ |
| 95 | (CORE_EVNTSEL_EVENT_MASK | \ |
| 96 | CORE_EVNTSEL_UNIT_MASK | \ |
Peter Zijlstra | ff99be5 | 2009-05-25 17:39:03 +0200 | [diff] [blame] | 97 | CORE_EVNTSEL_EDGE_MASK | \ |
| 98 | CORE_EVNTSEL_INV_MASK | \ |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 99 | CORE_EVNTSEL_COUNTER_MASK) |
| 100 | |
| 101 | return event & CORE_EVNTSEL_MASK; |
| 102 | } |
| 103 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 104 | /* |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 105 | * AMD Performance Monitor K7 and later. |
| 106 | */ |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 107 | static const u64 amd_perfmon_event_map[] = |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 108 | { |
| 109 | [PERF_COUNT_CPU_CYCLES] = 0x0076, |
| 110 | [PERF_COUNT_INSTRUCTIONS] = 0x00c0, |
| 111 | [PERF_COUNT_CACHE_REFERENCES] = 0x0080, |
| 112 | [PERF_COUNT_CACHE_MISSES] = 0x0081, |
| 113 | [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4, |
| 114 | [PERF_COUNT_BRANCH_MISSES] = 0x00c5, |
| 115 | }; |
| 116 | |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 117 | static u64 amd_pmu_event_map(int event) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 118 | { |
| 119 | return amd_perfmon_event_map[event]; |
| 120 | } |
| 121 | |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 122 | static u64 amd_pmu_raw_event(u64 event) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 123 | { |
Peter Zijlstra | 82bae4f8 | 2009-03-13 12:21:31 +0100 | [diff] [blame] | 124 | #define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL |
| 125 | #define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL |
Peter Zijlstra | ff99be5 | 2009-05-25 17:39:03 +0200 | [diff] [blame] | 126 | #define K7_EVNTSEL_EDGE_MASK 0x000040000ULL |
| 127 | #define K7_EVNTSEL_INV_MASK 0x000800000ULL |
Peter Zijlstra | 82bae4f8 | 2009-03-13 12:21:31 +0100 | [diff] [blame] | 128 | #define K7_EVNTSEL_COUNTER_MASK 0x0FF000000ULL |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 129 | |
| 130 | #define K7_EVNTSEL_MASK \ |
| 131 | (K7_EVNTSEL_EVENT_MASK | \ |
| 132 | K7_EVNTSEL_UNIT_MASK | \ |
Peter Zijlstra | ff99be5 | 2009-05-25 17:39:03 +0200 | [diff] [blame] | 133 | K7_EVNTSEL_EDGE_MASK | \ |
| 134 | K7_EVNTSEL_INV_MASK | \ |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 135 | K7_EVNTSEL_COUNTER_MASK) |
| 136 | |
| 137 | return event & K7_EVNTSEL_MASK; |
| 138 | } |
| 139 | |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 140 | /* |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 141 | * Propagate counter elapsed time into the generic counter. |
| 142 | * Can only be executed on the CPU where the counter is active. |
| 143 | * Returns the delta events processed. |
| 144 | */ |
Robert Richter | 4b7bfd0 | 2009-04-29 12:47:22 +0200 | [diff] [blame] | 145 | static u64 |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 146 | x86_perf_counter_update(struct perf_counter *counter, |
| 147 | struct hw_perf_counter *hwc, int idx) |
| 148 | { |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 149 | int shift = 64 - x86_pmu.counter_bits; |
| 150 | u64 prev_raw_count, new_raw_count; |
| 151 | s64 delta; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 152 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 153 | /* |
| 154 | * Careful: an NMI might modify the previous counter value. |
| 155 | * |
| 156 | * Our tactic to handle this is to first atomically read and |
| 157 | * exchange a new raw count - then add that new-prev delta |
| 158 | * count to the generic counter atomically: |
| 159 | */ |
| 160 | again: |
| 161 | prev_raw_count = atomic64_read(&hwc->prev_count); |
| 162 | rdmsrl(hwc->counter_base + idx, new_raw_count); |
| 163 | |
| 164 | if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count, |
| 165 | new_raw_count) != prev_raw_count) |
| 166 | goto again; |
| 167 | |
| 168 | /* |
| 169 | * Now we have the new raw value and have updated the prev |
| 170 | * timestamp already. We can now calculate the elapsed delta |
| 171 | * (counter-)time and add that to the generic counter. |
| 172 | * |
| 173 | * Careful, not all hw sign-extends above the physical width |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 174 | * of the count. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 175 | */ |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 176 | delta = (new_raw_count << shift) - (prev_raw_count << shift); |
| 177 | delta >>= shift; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 178 | |
| 179 | atomic64_add(delta, &counter->count); |
| 180 | atomic64_sub(delta, &hwc->period_left); |
Robert Richter | 4b7bfd0 | 2009-04-29 12:47:22 +0200 | [diff] [blame] | 181 | |
| 182 | return new_raw_count; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Peter Zijlstra | ba77813 | 2009-05-04 18:47:44 +0200 | [diff] [blame] | 185 | static atomic_t active_counters; |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 186 | static DEFINE_MUTEX(pmc_reserve_mutex); |
| 187 | |
| 188 | static bool reserve_pmc_hardware(void) |
| 189 | { |
| 190 | int i; |
| 191 | |
| 192 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 193 | disable_lapic_nmi_watchdog(); |
| 194 | |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 195 | for (i = 0; i < x86_pmu.num_counters; i++) { |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 196 | if (!reserve_perfctr_nmi(x86_pmu.perfctr + i)) |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 197 | goto perfctr_fail; |
| 198 | } |
| 199 | |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 200 | for (i = 0; i < x86_pmu.num_counters; i++) { |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 201 | if (!reserve_evntsel_nmi(x86_pmu.eventsel + i)) |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 202 | goto eventsel_fail; |
| 203 | } |
| 204 | |
| 205 | return true; |
| 206 | |
| 207 | eventsel_fail: |
| 208 | for (i--; i >= 0; i--) |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 209 | release_evntsel_nmi(x86_pmu.eventsel + i); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 210 | |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 211 | i = x86_pmu.num_counters; |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 212 | |
| 213 | perfctr_fail: |
| 214 | for (i--; i >= 0; i--) |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 215 | release_perfctr_nmi(x86_pmu.perfctr + i); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 216 | |
| 217 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 218 | enable_lapic_nmi_watchdog(); |
| 219 | |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | static void release_pmc_hardware(void) |
| 224 | { |
| 225 | int i; |
| 226 | |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 227 | for (i = 0; i < x86_pmu.num_counters; i++) { |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 228 | release_perfctr_nmi(x86_pmu.perfctr + i); |
| 229 | release_evntsel_nmi(x86_pmu.eventsel + i); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 233 | enable_lapic_nmi_watchdog(); |
| 234 | } |
| 235 | |
| 236 | static void hw_perf_counter_destroy(struct perf_counter *counter) |
| 237 | { |
Peter Zijlstra | ba77813 | 2009-05-04 18:47:44 +0200 | [diff] [blame] | 238 | if (atomic_dec_and_mutex_lock(&active_counters, &pmc_reserve_mutex)) { |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 239 | release_pmc_hardware(); |
| 240 | mutex_unlock(&pmc_reserve_mutex); |
| 241 | } |
| 242 | } |
| 243 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 244 | static inline int x86_pmu_initialized(void) |
| 245 | { |
| 246 | return x86_pmu.handle_irq != NULL; |
| 247 | } |
| 248 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 249 | /* |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 250 | * Setup the hardware configuration for a given hw_event_type |
| 251 | */ |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 252 | static int __hw_perf_counter_init(struct perf_counter *counter) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 253 | { |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 254 | struct perf_counter_hw_event *hw_event = &counter->hw_event; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 255 | struct hw_perf_counter *hwc = &counter->hw; |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 256 | int err; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 257 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 258 | if (!x86_pmu_initialized()) |
| 259 | return -ENODEV; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 260 | |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 261 | err = 0; |
Peter Zijlstra | ba77813 | 2009-05-04 18:47:44 +0200 | [diff] [blame] | 262 | if (!atomic_inc_not_zero(&active_counters)) { |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 263 | mutex_lock(&pmc_reserve_mutex); |
Peter Zijlstra | ba77813 | 2009-05-04 18:47:44 +0200 | [diff] [blame] | 264 | if (atomic_read(&active_counters) == 0 && !reserve_pmc_hardware()) |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 265 | err = -EBUSY; |
| 266 | else |
Peter Zijlstra | ba77813 | 2009-05-04 18:47:44 +0200 | [diff] [blame] | 267 | atomic_inc(&active_counters); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 268 | mutex_unlock(&pmc_reserve_mutex); |
| 269 | } |
| 270 | if (err) |
| 271 | return err; |
| 272 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 273 | /* |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 274 | * Generate PMC IRQs: |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 275 | * (keep 'enabled' bit clear for now) |
| 276 | */ |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 277 | hwc->config = ARCH_PERFMON_EVENTSEL_INT; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 278 | |
| 279 | /* |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 280 | * Count user and OS events unless requested not to. |
| 281 | */ |
| 282 | if (!hw_event->exclude_user) |
| 283 | hwc->config |= ARCH_PERFMON_EVENTSEL_USR; |
| 284 | if (!hw_event->exclude_kernel) |
| 285 | hwc->config |= ARCH_PERFMON_EVENTSEL_OS; |
| 286 | |
| 287 | /* |
| 288 | * If privileged enough, allow NMI events: |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 289 | */ |
| 290 | hwc->nmi = 0; |
Peter Zijlstra | a026dfe | 2009-05-13 10:02:57 +0200 | [diff] [blame] | 291 | if (hw_event->nmi) { |
| 292 | if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN)) |
| 293 | return -EACCES; |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 294 | hwc->nmi = 1; |
Peter Zijlstra | a026dfe | 2009-05-13 10:02:57 +0200 | [diff] [blame] | 295 | } |
Ingo Molnar | b68f1d2 | 2009-05-17 19:37:25 +0200 | [diff] [blame] | 296 | perf_counters_lapic_init(hwc->nmi); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 297 | |
Ingo Molnar | d2517a4 | 2009-05-17 10:04:45 +0200 | [diff] [blame] | 298 | if (!hwc->irq_period) |
| 299 | hwc->irq_period = x86_pmu.max_period; |
| 300 | |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 301 | atomic64_set(&hwc->period_left, |
| 302 | min(x86_pmu.max_period, hwc->irq_period)); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 303 | |
| 304 | /* |
Thomas Gleixner | dfa7c89 | 2008-12-08 19:35:37 +0100 | [diff] [blame] | 305 | * Raw event type provide the config in the event structure |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 306 | */ |
Peter Zijlstra | f4a2deb4 | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 307 | if (perf_event_raw(hw_event)) { |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 308 | hwc->config |= x86_pmu.raw_event(perf_event_config(hw_event)); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 309 | } else { |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 310 | if (perf_event_id(hw_event) >= x86_pmu.max_events) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 311 | return -EINVAL; |
| 312 | /* |
| 313 | * The generic map: |
| 314 | */ |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 315 | hwc->config |= x86_pmu.event_map(perf_event_id(hw_event)); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 316 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 317 | |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 318 | counter->destroy = hw_perf_counter_destroy; |
| 319 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 320 | return 0; |
| 321 | } |
| 322 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 323 | static void intel_pmu_disable_all(void) |
Thomas Gleixner | 4ac1329 | 2008-12-09 21:43:39 +0100 | [diff] [blame] | 324 | { |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 325 | wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0); |
Thomas Gleixner | 4ac1329 | 2008-12-09 21:43:39 +0100 | [diff] [blame] | 326 | } |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 327 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 328 | static void amd_pmu_disable_all(void) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 329 | { |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 330 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 331 | int idx; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 332 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 333 | if (!cpuc->enabled) |
| 334 | return; |
| 335 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 336 | cpuc->enabled = 0; |
Peter Zijlstra | 60b3df9 | 2009-03-13 12:21:30 +0100 | [diff] [blame] | 337 | /* |
| 338 | * ensure we write the disable before we start disabling the |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 339 | * counters proper, so that amd_pmu_enable_counter() does the |
| 340 | * right thing. |
Peter Zijlstra | 60b3df9 | 2009-03-13 12:21:30 +0100 | [diff] [blame] | 341 | */ |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 342 | barrier(); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 343 | |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 344 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 345 | u64 val; |
| 346 | |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 347 | if (!test_bit(idx, cpuc->active_mask)) |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 348 | continue; |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 349 | rdmsrl(MSR_K7_EVNTSEL0 + idx, val); |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 350 | if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE)) |
| 351 | continue; |
| 352 | val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE; |
| 353 | wrmsrl(MSR_K7_EVNTSEL0 + idx, val); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 354 | } |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 355 | } |
| 356 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 357 | void hw_perf_disable(void) |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 358 | { |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 359 | if (!x86_pmu_initialized()) |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 360 | return; |
| 361 | return x86_pmu.disable_all(); |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 362 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 363 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 364 | static void intel_pmu_enable_all(void) |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 365 | { |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 366 | wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl); |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 367 | } |
| 368 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 369 | static void amd_pmu_enable_all(void) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 370 | { |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 371 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 372 | int idx; |
| 373 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 374 | if (cpuc->enabled) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 375 | return; |
| 376 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 377 | cpuc->enabled = 1; |
| 378 | barrier(); |
| 379 | |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 380 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 381 | u64 val; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 382 | |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 383 | if (!test_bit(idx, cpuc->active_mask)) |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 384 | continue; |
| 385 | rdmsrl(MSR_K7_EVNTSEL0 + idx, val); |
| 386 | if (val & ARCH_PERFMON_EVENTSEL0_ENABLE) |
| 387 | continue; |
| 388 | val |= ARCH_PERFMON_EVENTSEL0_ENABLE; |
| 389 | wrmsrl(MSR_K7_EVNTSEL0 + idx, val); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 393 | void hw_perf_enable(void) |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 394 | { |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 395 | if (!x86_pmu_initialized()) |
Ingo Molnar | 2b9ff0d | 2008-12-14 18:36:30 +0100 | [diff] [blame] | 396 | return; |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 397 | x86_pmu.enable_all(); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 398 | } |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 399 | |
Robert Richter | 19d84da | 2009-04-29 12:47:25 +0200 | [diff] [blame] | 400 | static inline u64 intel_pmu_get_status(void) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 401 | { |
| 402 | u64 status; |
| 403 | |
| 404 | rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status); |
| 405 | |
| 406 | return status; |
| 407 | } |
| 408 | |
Robert Richter | dee5d90 | 2009-04-29 12:47:07 +0200 | [diff] [blame] | 409 | static inline void intel_pmu_ack_status(u64 ack) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 410 | { |
| 411 | wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack); |
| 412 | } |
| 413 | |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 414 | static inline void x86_pmu_enable_counter(struct hw_perf_counter *hwc, int idx) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 415 | { |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 416 | int err; |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 417 | err = checking_wrmsrl(hwc->config_base + idx, |
| 418 | hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE); |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 419 | } |
| 420 | |
Robert Richter | d436989 | 2009-04-29 12:47:19 +0200 | [diff] [blame] | 421 | static inline void x86_pmu_disable_counter(struct hw_perf_counter *hwc, int idx) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 422 | { |
Robert Richter | d436989 | 2009-04-29 12:47:19 +0200 | [diff] [blame] | 423 | int err; |
Robert Richter | d436989 | 2009-04-29 12:47:19 +0200 | [diff] [blame] | 424 | err = checking_wrmsrl(hwc->config_base + idx, |
| 425 | hwc->config); |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 426 | } |
| 427 | |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 428 | static inline void |
Robert Richter | d436989 | 2009-04-29 12:47:19 +0200 | [diff] [blame] | 429 | intel_pmu_disable_fixed(struct hw_perf_counter *hwc, int __idx) |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 430 | { |
| 431 | int idx = __idx - X86_PMC_IDX_FIXED; |
| 432 | u64 ctrl_val, mask; |
| 433 | int err; |
| 434 | |
| 435 | mask = 0xfULL << (idx * 4); |
| 436 | |
| 437 | rdmsrl(hwc->config_base, ctrl_val); |
| 438 | ctrl_val &= ~mask; |
| 439 | err = checking_wrmsrl(hwc->config_base, ctrl_val); |
| 440 | } |
| 441 | |
| 442 | static inline void |
Robert Richter | d436989 | 2009-04-29 12:47:19 +0200 | [diff] [blame] | 443 | intel_pmu_disable_counter(struct hw_perf_counter *hwc, int idx) |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 444 | { |
Robert Richter | d436989 | 2009-04-29 12:47:19 +0200 | [diff] [blame] | 445 | if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) { |
| 446 | intel_pmu_disable_fixed(hwc, idx); |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | x86_pmu_disable_counter(hwc, idx); |
| 451 | } |
| 452 | |
| 453 | static inline void |
| 454 | amd_pmu_disable_counter(struct hw_perf_counter *hwc, int idx) |
| 455 | { |
| 456 | x86_pmu_disable_counter(hwc, idx); |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 457 | } |
| 458 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 459 | static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 460 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 461 | /* |
| 462 | * Set the next IRQ period, based on the hwc->period_left value. |
| 463 | * To be called with the counter disabled in hw: |
| 464 | */ |
| 465 | static void |
Robert Richter | 26816c2 | 2009-04-29 12:47:08 +0200 | [diff] [blame] | 466 | x86_perf_counter_set_period(struct perf_counter *counter, |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 467 | struct hw_perf_counter *hwc, int idx) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 468 | { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 469 | s64 left = atomic64_read(&hwc->period_left); |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 470 | s64 period = min(x86_pmu.max_period, hwc->irq_period); |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 471 | int err; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 472 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 473 | /* |
| 474 | * If we are way outside a reasoable range then just skip forward: |
| 475 | */ |
| 476 | if (unlikely(left <= -period)) { |
| 477 | left = period; |
| 478 | atomic64_set(&hwc->period_left, left); |
| 479 | } |
| 480 | |
| 481 | if (unlikely(left <= 0)) { |
| 482 | left += period; |
| 483 | atomic64_set(&hwc->period_left, left); |
| 484 | } |
Ingo Molnar | 1c80f4b | 2009-05-15 08:25:22 +0200 | [diff] [blame] | 485 | /* |
| 486 | * Quirk: certain CPUs dont like it if just 1 event is left: |
| 487 | */ |
| 488 | if (unlikely(left < 2)) |
| 489 | left = 2; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 490 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 491 | per_cpu(prev_left[idx], smp_processor_id()) = left; |
| 492 | |
| 493 | /* |
| 494 | * The hw counter starts counting from this counter offset, |
| 495 | * mark it to be able to extra future deltas: |
| 496 | */ |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 497 | atomic64_set(&hwc->prev_count, (u64)-left); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 498 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 499 | err = checking_wrmsrl(hwc->counter_base + idx, |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 500 | (u64)(-left) & x86_pmu.counter_mask); |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static inline void |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 504 | intel_pmu_enable_fixed(struct hw_perf_counter *hwc, int __idx) |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 505 | { |
| 506 | int idx = __idx - X86_PMC_IDX_FIXED; |
| 507 | u64 ctrl_val, bits, mask; |
| 508 | int err; |
| 509 | |
| 510 | /* |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 511 | * Enable IRQ generation (0x8), |
| 512 | * and enable ring-3 counting (0x2) and ring-0 counting (0x1) |
| 513 | * if requested: |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 514 | */ |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 515 | bits = 0x8ULL; |
| 516 | if (hwc->config & ARCH_PERFMON_EVENTSEL_USR) |
| 517 | bits |= 0x2; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 518 | if (hwc->config & ARCH_PERFMON_EVENTSEL_OS) |
| 519 | bits |= 0x1; |
| 520 | bits <<= (idx * 4); |
| 521 | mask = 0xfULL << (idx * 4); |
| 522 | |
| 523 | rdmsrl(hwc->config_base, ctrl_val); |
| 524 | ctrl_val &= ~mask; |
| 525 | ctrl_val |= bits; |
| 526 | err = checking_wrmsrl(hwc->config_base, ctrl_val); |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 527 | } |
| 528 | |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 529 | static void intel_pmu_enable_counter(struct hw_perf_counter *hwc, int idx) |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 530 | { |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 531 | if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) { |
| 532 | intel_pmu_enable_fixed(hwc, idx); |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | x86_pmu_enable_counter(hwc, idx); |
| 537 | } |
| 538 | |
| 539 | static void amd_pmu_enable_counter(struct hw_perf_counter *hwc, int idx) |
| 540 | { |
| 541 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); |
| 542 | |
| 543 | if (cpuc->enabled) |
| 544 | x86_pmu_enable_counter(hwc, idx); |
Jaswinder Singh Rajput | 2b583d8 | 2008-12-27 19:15:43 +0530 | [diff] [blame] | 545 | else |
Robert Richter | d436989 | 2009-04-29 12:47:19 +0200 | [diff] [blame] | 546 | x86_pmu_disable_counter(hwc, idx); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 547 | } |
| 548 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 549 | static int |
| 550 | fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc) |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 551 | { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 552 | unsigned int event; |
| 553 | |
Robert Richter | ef7b3e0 | 2009-04-29 12:47:24 +0200 | [diff] [blame] | 554 | if (!x86_pmu.num_counters_fixed) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 555 | return -1; |
| 556 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 557 | if (unlikely(hwc->nmi)) |
| 558 | return -1; |
| 559 | |
| 560 | event = hwc->config & ARCH_PERFMON_EVENT_MASK; |
| 561 | |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 562 | if (unlikely(event == x86_pmu.event_map(PERF_COUNT_INSTRUCTIONS))) |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 563 | return X86_PMC_IDX_FIXED_INSTRUCTIONS; |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 564 | if (unlikely(event == x86_pmu.event_map(PERF_COUNT_CPU_CYCLES))) |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 565 | return X86_PMC_IDX_FIXED_CPU_CYCLES; |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 566 | if (unlikely(event == x86_pmu.event_map(PERF_COUNT_BUS_CYCLES))) |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 567 | return X86_PMC_IDX_FIXED_BUS_CYCLES; |
| 568 | |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 569 | return -1; |
| 570 | } |
| 571 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 572 | /* |
| 573 | * Find a PMC slot for the freshly enabled / scheduled in counter: |
| 574 | */ |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 575 | static int x86_pmu_enable(struct perf_counter *counter) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 576 | { |
| 577 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); |
| 578 | struct hw_perf_counter *hwc = &counter->hw; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 579 | int idx; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 580 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 581 | idx = fixed_mode_idx(counter, hwc); |
| 582 | if (idx >= 0) { |
| 583 | /* |
| 584 | * Try to get the fixed counter, if that is already taken |
| 585 | * then try to get a generic counter: |
| 586 | */ |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 587 | if (test_and_set_bit(idx, cpuc->used_mask)) |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 588 | goto try_generic; |
Ingo Molnar | 0dff86a | 2008-12-23 12:28:12 +0100 | [diff] [blame] | 589 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 590 | hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL; |
| 591 | /* |
| 592 | * We set it so that counter_base + idx in wrmsr/rdmsr maps to |
| 593 | * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2: |
| 594 | */ |
| 595 | hwc->counter_base = |
| 596 | MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 597 | hwc->idx = idx; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 598 | } else { |
| 599 | idx = hwc->idx; |
| 600 | /* Try to get the previous generic counter again */ |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 601 | if (test_and_set_bit(idx, cpuc->used_mask)) { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 602 | try_generic: |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 603 | idx = find_first_zero_bit(cpuc->used_mask, |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 604 | x86_pmu.num_counters); |
| 605 | if (idx == x86_pmu.num_counters) |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 606 | return -EAGAIN; |
| 607 | |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 608 | set_bit(idx, cpuc->used_mask); |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 609 | hwc->idx = idx; |
| 610 | } |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 611 | hwc->config_base = x86_pmu.eventsel; |
| 612 | hwc->counter_base = x86_pmu.perfctr; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 613 | } |
| 614 | |
Robert Richter | d436989 | 2009-04-29 12:47:19 +0200 | [diff] [blame] | 615 | x86_pmu.disable(hwc, idx); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 616 | |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 617 | cpuc->counters[idx] = counter; |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 618 | set_bit(idx, cpuc->active_mask); |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 619 | |
Robert Richter | 26816c2 | 2009-04-29 12:47:08 +0200 | [diff] [blame] | 620 | x86_perf_counter_set_period(counter, hwc, idx); |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 621 | x86_pmu.enable(hwc, idx); |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 622 | |
| 623 | return 0; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | void perf_counter_print_debug(void) |
| 627 | { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 628 | u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed; |
Ingo Molnar | 0dff86a | 2008-12-23 12:28:12 +0100 | [diff] [blame] | 629 | struct cpu_hw_counters *cpuc; |
Peter Zijlstra | 5bb9efe | 2009-05-13 08:12:51 +0200 | [diff] [blame] | 630 | unsigned long flags; |
Ingo Molnar | 1e12567 | 2008-12-09 12:18:18 +0100 | [diff] [blame] | 631 | int cpu, idx; |
| 632 | |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 633 | if (!x86_pmu.num_counters) |
Ingo Molnar | 1e12567 | 2008-12-09 12:18:18 +0100 | [diff] [blame] | 634 | return; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 635 | |
Peter Zijlstra | 5bb9efe | 2009-05-13 08:12:51 +0200 | [diff] [blame] | 636 | local_irq_save(flags); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 637 | |
| 638 | cpu = smp_processor_id(); |
Ingo Molnar | 0dff86a | 2008-12-23 12:28:12 +0100 | [diff] [blame] | 639 | cpuc = &per_cpu(cpu_hw_counters, cpu); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 640 | |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 641 | if (x86_pmu.version >= 2) { |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 642 | rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl); |
| 643 | rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status); |
| 644 | rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow); |
| 645 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 646 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 647 | pr_info("\n"); |
| 648 | pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl); |
| 649 | pr_info("CPU#%d: status: %016llx\n", cpu, status); |
| 650 | pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow); |
| 651 | pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 652 | } |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 653 | pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used_mask); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 654 | |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 655 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 656 | rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl); |
| 657 | rdmsrl(x86_pmu.perfctr + idx, pmc_count); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 658 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 659 | prev_left = per_cpu(prev_left[idx], cpu); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 660 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 661 | pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n", |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 662 | cpu, idx, pmc_ctrl); |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 663 | pr_info("CPU#%d: gen-PMC%d count: %016llx\n", |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 664 | cpu, idx, pmc_count); |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 665 | pr_info("CPU#%d: gen-PMC%d left: %016llx\n", |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 666 | cpu, idx, prev_left); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 667 | } |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 668 | for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 669 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count); |
| 670 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 671 | pr_info("CPU#%d: fixed-PMC%d count: %016llx\n", |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 672 | cpu, idx, pmc_count); |
| 673 | } |
Peter Zijlstra | 5bb9efe | 2009-05-13 08:12:51 +0200 | [diff] [blame] | 674 | local_irq_restore(flags); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 675 | } |
| 676 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 677 | static void x86_pmu_disable(struct perf_counter *counter) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 678 | { |
| 679 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); |
| 680 | struct hw_perf_counter *hwc = &counter->hw; |
Robert Richter | 6f00cad | 2009-04-29 12:47:17 +0200 | [diff] [blame] | 681 | int idx = hwc->idx; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 682 | |
Robert Richter | 0953423 | 2009-04-29 12:47:16 +0200 | [diff] [blame] | 683 | /* |
| 684 | * Must be done before we disable, otherwise the nmi handler |
| 685 | * could reenable again: |
| 686 | */ |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 687 | clear_bit(idx, cpuc->active_mask); |
Robert Richter | d436989 | 2009-04-29 12:47:19 +0200 | [diff] [blame] | 688 | x86_pmu.disable(hwc, idx); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 689 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 690 | /* |
| 691 | * Make sure the cleared pointer becomes visible before we |
| 692 | * (potentially) free the counter: |
| 693 | */ |
Robert Richter | 527e26a | 2009-04-29 12:47:02 +0200 | [diff] [blame] | 694 | barrier(); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 695 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 696 | /* |
| 697 | * Drain the remaining delta count out of a counter |
| 698 | * that we are disabling: |
| 699 | */ |
| 700 | x86_perf_counter_update(counter, hwc, idx); |
Robert Richter | 0953423 | 2009-04-29 12:47:16 +0200 | [diff] [blame] | 701 | cpuc->counters[idx] = NULL; |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 702 | clear_bit(idx, cpuc->used_mask); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 703 | } |
| 704 | |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 705 | /* |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 706 | * Save and restart an expired counter. Called by NMI contexts, |
| 707 | * so it has to be careful about preempting normal counter ops: |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 708 | */ |
Robert Richter | 55de0f2 | 2009-04-29 12:47:09 +0200 | [diff] [blame] | 709 | static void intel_pmu_save_and_restart(struct perf_counter *counter) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 710 | { |
| 711 | struct hw_perf_counter *hwc = &counter->hw; |
| 712 | int idx = hwc->idx; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 713 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 714 | x86_perf_counter_update(counter, hwc, idx); |
Robert Richter | 26816c2 | 2009-04-29 12:47:08 +0200 | [diff] [blame] | 715 | x86_perf_counter_set_period(counter, hwc, idx); |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 716 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 717 | if (counter->state == PERF_COUNTER_STATE_ACTIVE) |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 718 | intel_pmu_enable_counter(hwc, idx); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 719 | } |
| 720 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 721 | /* |
| 722 | * This handler is triggered by the local APIC, so the APIC IRQ handling |
| 723 | * rules apply: |
| 724 | */ |
Robert Richter | 39d81ea | 2009-04-29 12:47:05 +0200 | [diff] [blame] | 725 | static int intel_pmu_handle_irq(struct pt_regs *regs, int nmi) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 726 | { |
Ingo Molnar | 9029a5e | 2009-05-15 08:26:20 +0200 | [diff] [blame] | 727 | struct cpu_hw_counters *cpuc; |
| 728 | struct cpu_hw_counters; |
| 729 | int bit, cpu, loops; |
Mike Galbraith | 4b39fd9 | 2009-01-23 14:36:16 +0100 | [diff] [blame] | 730 | u64 ack, status; |
Ingo Molnar | 9029a5e | 2009-05-15 08:26:20 +0200 | [diff] [blame] | 731 | |
| 732 | cpu = smp_processor_id(); |
| 733 | cpuc = &per_cpu(cpu_hw_counters, cpu); |
Ingo Molnar | 43874d2 | 2008-12-09 12:23:59 +0100 | [diff] [blame] | 734 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 735 | perf_disable(); |
Robert Richter | 19d84da | 2009-04-29 12:47:25 +0200 | [diff] [blame] | 736 | status = intel_pmu_get_status(); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 737 | if (!status) { |
| 738 | perf_enable(); |
| 739 | return 0; |
| 740 | } |
Ingo Molnar | 87b9cf4 | 2008-12-08 14:20:16 +0100 | [diff] [blame] | 741 | |
Ingo Molnar | 9029a5e | 2009-05-15 08:26:20 +0200 | [diff] [blame] | 742 | loops = 0; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 743 | again: |
Ingo Molnar | 9029a5e | 2009-05-15 08:26:20 +0200 | [diff] [blame] | 744 | if (++loops > 100) { |
| 745 | WARN_ONCE(1, "perfcounters: irq loop stuck!\n"); |
Ingo Molnar | 34adc80 | 2009-05-20 20:13:28 +0200 | [diff] [blame] | 746 | perf_counter_print_debug(); |
Ingo Molnar | 9029a5e | 2009-05-15 08:26:20 +0200 | [diff] [blame] | 747 | return 1; |
| 748 | } |
| 749 | |
Mike Galbraith | d278c48 | 2009-02-09 07:38:50 +0100 | [diff] [blame] | 750 | inc_irq_stat(apic_perf_irqs); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 751 | ack = status; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 752 | for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) { |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 753 | struct perf_counter *counter = cpuc->counters[bit]; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 754 | |
| 755 | clear_bit(bit, (unsigned long *) &status); |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 756 | if (!test_bit(bit, cpuc->active_mask)) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 757 | continue; |
| 758 | |
Robert Richter | 55de0f2 | 2009-04-29 12:47:09 +0200 | [diff] [blame] | 759 | intel_pmu_save_and_restart(counter); |
Peter Zijlstra | 78f13e9 | 2009-04-08 15:01:33 +0200 | [diff] [blame] | 760 | if (perf_counter_overflow(counter, nmi, regs, 0)) |
Robert Richter | d436989 | 2009-04-29 12:47:19 +0200 | [diff] [blame] | 761 | intel_pmu_disable_counter(&counter->hw, bit); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 762 | } |
| 763 | |
Robert Richter | dee5d90 | 2009-04-29 12:47:07 +0200 | [diff] [blame] | 764 | intel_pmu_ack_status(ack); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 765 | |
| 766 | /* |
| 767 | * Repeat if there is more work to be done: |
| 768 | */ |
Robert Richter | 19d84da | 2009-04-29 12:47:25 +0200 | [diff] [blame] | 769 | status = intel_pmu_get_status(); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 770 | if (status) |
| 771 | goto again; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 772 | |
Peter Zijlstra | 48e22d5 | 2009-05-25 17:39:04 +0200 | [diff] [blame^] | 773 | perf_enable(); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 774 | |
| 775 | return 1; |
Mike Galbraith | 1b023a9 | 2009-01-23 10:13:01 +0100 | [diff] [blame] | 776 | } |
| 777 | |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 778 | static int amd_pmu_handle_irq(struct pt_regs *regs, int nmi) |
| 779 | { |
Peter Zijlstra | 48e22d5 | 2009-05-25 17:39:04 +0200 | [diff] [blame^] | 780 | int cpu, idx, handled = 0; |
Ingo Molnar | 9029a5e | 2009-05-15 08:26:20 +0200 | [diff] [blame] | 781 | struct cpu_hw_counters *cpuc; |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 782 | struct perf_counter *counter; |
| 783 | struct hw_perf_counter *hwc; |
Ingo Molnar | 9029a5e | 2009-05-15 08:26:20 +0200 | [diff] [blame] | 784 | u64 val; |
| 785 | |
| 786 | cpu = smp_processor_id(); |
| 787 | cpuc = &per_cpu(cpu_hw_counters, cpu); |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 788 | |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 789 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 790 | if (!test_bit(idx, cpuc->active_mask)) |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 791 | continue; |
Peter Zijlstra | 962bf7a | 2009-05-13 13:21:36 +0200 | [diff] [blame] | 792 | |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 793 | counter = cpuc->counters[idx]; |
| 794 | hwc = &counter->hw; |
Peter Zijlstra | a4016a7 | 2009-05-14 14:52:17 +0200 | [diff] [blame] | 795 | |
| 796 | if (counter->hw_event.nmi != nmi) |
Peter Zijlstra | 48e22d5 | 2009-05-25 17:39:04 +0200 | [diff] [blame^] | 797 | continue; |
Peter Zijlstra | a4016a7 | 2009-05-14 14:52:17 +0200 | [diff] [blame] | 798 | |
Robert Richter | 4b7bfd0 | 2009-04-29 12:47:22 +0200 | [diff] [blame] | 799 | val = x86_perf_counter_update(counter, hwc, idx); |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 800 | if (val & (1ULL << (x86_pmu.counter_bits - 1))) |
Peter Zijlstra | 48e22d5 | 2009-05-25 17:39:04 +0200 | [diff] [blame^] | 801 | continue; |
Peter Zijlstra | 962bf7a | 2009-05-13 13:21:36 +0200 | [diff] [blame] | 802 | |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 803 | /* counter overflow */ |
| 804 | x86_perf_counter_set_period(counter, hwc, idx); |
| 805 | handled = 1; |
| 806 | inc_irq_stat(apic_perf_irqs); |
Peter Zijlstra | 48e22d5 | 2009-05-25 17:39:04 +0200 | [diff] [blame^] | 807 | if (perf_counter_overflow(counter, nmi, regs, 0)) |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 808 | amd_pmu_disable_counter(hwc, idx); |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 809 | } |
Peter Zijlstra | 962bf7a | 2009-05-13 13:21:36 +0200 | [diff] [blame] | 810 | |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 811 | return handled; |
| 812 | } |
Robert Richter | 39d81ea | 2009-04-29 12:47:05 +0200 | [diff] [blame] | 813 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 814 | void smp_perf_counter_interrupt(struct pt_regs *regs) |
| 815 | { |
| 816 | irq_enter(); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 817 | apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR); |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 818 | ack_APIC_irq(); |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 819 | x86_pmu.handle_irq(regs, 0); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 820 | irq_exit(); |
| 821 | } |
| 822 | |
Peter Zijlstra | b6276f3 | 2009-04-06 11:45:03 +0200 | [diff] [blame] | 823 | void smp_perf_pending_interrupt(struct pt_regs *regs) |
| 824 | { |
| 825 | irq_enter(); |
| 826 | ack_APIC_irq(); |
| 827 | inc_irq_stat(apic_pending_irqs); |
| 828 | perf_counter_do_pending(); |
| 829 | irq_exit(); |
| 830 | } |
| 831 | |
| 832 | void set_perf_counter_pending(void) |
| 833 | { |
| 834 | apic->send_IPI_self(LOCAL_PENDING_VECTOR); |
| 835 | } |
| 836 | |
Mike Galbraith | 3415dd9 | 2009-01-23 14:16:53 +0100 | [diff] [blame] | 837 | void perf_counters_lapic_init(int nmi) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 838 | { |
| 839 | u32 apic_val; |
| 840 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 841 | if (!x86_pmu_initialized()) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 842 | return; |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 843 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 844 | /* |
| 845 | * Enable the performance counter vector in the APIC LVT: |
| 846 | */ |
| 847 | apic_val = apic_read(APIC_LVTERR); |
| 848 | |
| 849 | apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED); |
| 850 | if (nmi) |
| 851 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
| 852 | else |
| 853 | apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR); |
| 854 | apic_write(APIC_LVTERR, apic_val); |
| 855 | } |
| 856 | |
| 857 | static int __kprobes |
| 858 | perf_counter_nmi_handler(struct notifier_block *self, |
| 859 | unsigned long cmd, void *__args) |
| 860 | { |
| 861 | struct die_args *args = __args; |
| 862 | struct pt_regs *regs; |
| 863 | |
Peter Zijlstra | ba77813 | 2009-05-04 18:47:44 +0200 | [diff] [blame] | 864 | if (!atomic_read(&active_counters)) |
Peter Zijlstra | 63a809a | 2009-05-01 12:23:17 +0200 | [diff] [blame] | 865 | return NOTIFY_DONE; |
| 866 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 867 | switch (cmd) { |
| 868 | case DIE_NMI: |
| 869 | case DIE_NMI_IPI: |
| 870 | break; |
| 871 | |
| 872 | default: |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 873 | return NOTIFY_DONE; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 874 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 875 | |
| 876 | regs = args->regs; |
| 877 | |
| 878 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
Peter Zijlstra | a4016a7 | 2009-05-14 14:52:17 +0200 | [diff] [blame] | 879 | /* |
| 880 | * Can't rely on the handled return value to say it was our NMI, two |
| 881 | * counters could trigger 'simultaneously' raising two back-to-back NMIs. |
| 882 | * |
| 883 | * If the first NMI handles both, the latter will be empty and daze |
| 884 | * the CPU. |
| 885 | */ |
| 886 | x86_pmu.handle_irq(regs, 1); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 887 | |
Peter Zijlstra | a4016a7 | 2009-05-14 14:52:17 +0200 | [diff] [blame] | 888 | return NOTIFY_STOP; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | static __read_mostly struct notifier_block perf_counter_nmi_notifier = { |
Mike Galbraith | 5b75af0 | 2009-02-04 17:11:34 +0100 | [diff] [blame] | 892 | .notifier_call = perf_counter_nmi_handler, |
| 893 | .next = NULL, |
| 894 | .priority = 1 |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 895 | }; |
| 896 | |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 897 | static struct x86_pmu intel_pmu = { |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 898 | .name = "Intel", |
Robert Richter | 39d81ea | 2009-04-29 12:47:05 +0200 | [diff] [blame] | 899 | .handle_irq = intel_pmu_handle_irq, |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 900 | .disable_all = intel_pmu_disable_all, |
| 901 | .enable_all = intel_pmu_enable_all, |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 902 | .enable = intel_pmu_enable_counter, |
| 903 | .disable = intel_pmu_disable_counter, |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 904 | .eventsel = MSR_ARCH_PERFMON_EVENTSEL0, |
| 905 | .perfctr = MSR_ARCH_PERFMON_PERFCTR0, |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 906 | .event_map = intel_pmu_event_map, |
| 907 | .raw_event = intel_pmu_raw_event, |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 908 | .max_events = ARRAY_SIZE(intel_perfmon_event_map), |
Robert Richter | c619b8f | 2009-04-29 12:47:23 +0200 | [diff] [blame] | 909 | /* |
| 910 | * Intel PMCs cannot be accessed sanely above 32 bit width, |
| 911 | * so we install an artificial 1<<31 period regardless of |
| 912 | * the generic counter period: |
| 913 | */ |
| 914 | .max_period = (1ULL << 31) - 1, |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 915 | }; |
| 916 | |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 917 | static struct x86_pmu amd_pmu = { |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 918 | .name = "AMD", |
Robert Richter | 39d81ea | 2009-04-29 12:47:05 +0200 | [diff] [blame] | 919 | .handle_irq = amd_pmu_handle_irq, |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 920 | .disable_all = amd_pmu_disable_all, |
| 921 | .enable_all = amd_pmu_enable_all, |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 922 | .enable = amd_pmu_enable_counter, |
| 923 | .disable = amd_pmu_disable_counter, |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 924 | .eventsel = MSR_K7_EVNTSEL0, |
| 925 | .perfctr = MSR_K7_PERFCTR0, |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 926 | .event_map = amd_pmu_event_map, |
| 927 | .raw_event = amd_pmu_raw_event, |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 928 | .max_events = ARRAY_SIZE(amd_perfmon_event_map), |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 929 | .num_counters = 4, |
| 930 | .counter_bits = 48, |
| 931 | .counter_mask = (1ULL << 48) - 1, |
Robert Richter | c619b8f | 2009-04-29 12:47:23 +0200 | [diff] [blame] | 932 | /* use highest bit to detect overflow */ |
| 933 | .max_period = (1ULL << 47) - 1, |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 934 | }; |
| 935 | |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 936 | static int intel_pmu_init(void) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 937 | { |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 938 | union cpuid10_edx edx; |
Ingo Molnar | 7bb497b | 2009-03-18 08:59:21 +0100 | [diff] [blame] | 939 | union cpuid10_eax eax; |
| 940 | unsigned int unused; |
| 941 | unsigned int ebx; |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 942 | int version; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 943 | |
Robert Richter | da1a776 | 2009-04-29 12:46:58 +0200 | [diff] [blame] | 944 | if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 945 | return -ENODEV; |
Robert Richter | da1a776 | 2009-04-29 12:46:58 +0200 | [diff] [blame] | 946 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 947 | /* |
| 948 | * Check whether the Architectural PerfMon supports |
| 949 | * Branch Misses Retired Event or not. |
| 950 | */ |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 951 | cpuid(10, &eax.full, &ebx, &unused, &edx.full); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 952 | if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED) |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 953 | return -ENODEV; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 954 | |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 955 | version = eax.split.version_id; |
| 956 | if (version < 2) |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 957 | return -ENODEV; |
Ingo Molnar | 7bb497b | 2009-03-18 08:59:21 +0100 | [diff] [blame] | 958 | |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 959 | x86_pmu = intel_pmu; |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 960 | x86_pmu.version = version; |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 961 | x86_pmu.num_counters = eax.split.num_counters; |
Ingo Molnar | 066d7de | 2009-05-04 19:04:09 +0200 | [diff] [blame] | 962 | |
| 963 | /* |
| 964 | * Quirk: v2 perfmon does not report fixed-purpose counters, so |
| 965 | * assume at least 3 counters: |
| 966 | */ |
| 967 | x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3); |
| 968 | |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 969 | x86_pmu.counter_bits = eax.split.bit_width; |
| 970 | x86_pmu.counter_mask = (1ULL << eax.split.bit_width) - 1; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 971 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 972 | rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl); |
| 973 | |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 974 | return 0; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 975 | } |
| 976 | |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 977 | static int amd_pmu_init(void) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 978 | { |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 979 | x86_pmu = amd_pmu; |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 980 | return 0; |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 981 | } |
| 982 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 983 | void __init init_hw_perf_counters(void) |
| 984 | { |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 985 | int err; |
| 986 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 987 | switch (boot_cpu_data.x86_vendor) { |
| 988 | case X86_VENDOR_INTEL: |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 989 | err = intel_pmu_init(); |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 990 | break; |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 991 | case X86_VENDOR_AMD: |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 992 | err = amd_pmu_init(); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 993 | break; |
Robert Richter | 4138960 | 2009-04-29 12:47:00 +0200 | [diff] [blame] | 994 | default: |
| 995 | return; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 996 | } |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 997 | if (err != 0) |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 998 | return; |
| 999 | |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 1000 | pr_info("%s Performance Monitoring support detected.\n", x86_pmu.name); |
| 1001 | pr_info("... version: %d\n", x86_pmu.version); |
| 1002 | pr_info("... bit width: %d\n", x86_pmu.counter_bits); |
| 1003 | |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 1004 | pr_info("... num counters: %d\n", x86_pmu.num_counters); |
| 1005 | if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) { |
| 1006 | x86_pmu.num_counters = X86_PMC_MAX_GENERIC; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1007 | WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!", |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 1008 | x86_pmu.num_counters, X86_PMC_MAX_GENERIC); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1009 | } |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 1010 | perf_counter_mask = (1 << x86_pmu.num_counters) - 1; |
| 1011 | perf_max_counters = x86_pmu.num_counters; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1012 | |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 1013 | pr_info("... value mask: %016Lx\n", x86_pmu.counter_mask); |
Robert Richter | c619b8f | 2009-04-29 12:47:23 +0200 | [diff] [blame] | 1014 | pr_info("... max period: %016Lx\n", x86_pmu.max_period); |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1015 | |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 1016 | if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) { |
| 1017 | x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED; |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 1018 | WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!", |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 1019 | x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED); |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 1020 | } |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 1021 | pr_info("... fixed counters: %d\n", x86_pmu.num_counters_fixed); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1022 | |
Robert Richter | 0933e5c | 2009-04-29 12:47:12 +0200 | [diff] [blame] | 1023 | perf_counter_mask |= |
| 1024 | ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED; |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 1025 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1026 | pr_info("... counter mask: %016Lx\n", perf_counter_mask); |
Ingo Molnar | 75f224c | 2008-12-14 21:58:46 +0100 | [diff] [blame] | 1027 | |
Ingo Molnar | b68f1d2 | 2009-05-17 19:37:25 +0200 | [diff] [blame] | 1028 | perf_counters_lapic_init(1); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1029 | register_die_notifier(&perf_counter_nmi_notifier); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1030 | } |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1031 | |
Robert Richter | bb775fc | 2009-04-29 12:47:14 +0200 | [diff] [blame] | 1032 | static inline void x86_pmu_read(struct perf_counter *counter) |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1033 | { |
| 1034 | x86_perf_counter_update(counter, &counter->hw, counter->hw.idx); |
| 1035 | } |
| 1036 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 1037 | static const struct pmu pmu = { |
| 1038 | .enable = x86_pmu_enable, |
| 1039 | .disable = x86_pmu_disable, |
| 1040 | .read = x86_pmu_read, |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1041 | }; |
| 1042 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 1043 | const struct pmu *hw_perf_counter_init(struct perf_counter *counter) |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1044 | { |
| 1045 | int err; |
| 1046 | |
| 1047 | err = __hw_perf_counter_init(counter); |
| 1048 | if (err) |
Peter Zijlstra | 9ea98e1 | 2009-03-30 19:07:09 +0200 | [diff] [blame] | 1049 | return ERR_PTR(err); |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1050 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 1051 | return &pmu; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1052 | } |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1053 | |
| 1054 | /* |
| 1055 | * callchain support |
| 1056 | */ |
| 1057 | |
| 1058 | static inline |
| 1059 | void callchain_store(struct perf_callchain_entry *entry, unsigned long ip) |
| 1060 | { |
| 1061 | if (entry->nr < MAX_STACK_DEPTH) |
| 1062 | entry->ip[entry->nr++] = ip; |
| 1063 | } |
| 1064 | |
| 1065 | static DEFINE_PER_CPU(struct perf_callchain_entry, irq_entry); |
| 1066 | static DEFINE_PER_CPU(struct perf_callchain_entry, nmi_entry); |
| 1067 | |
| 1068 | |
| 1069 | static void |
| 1070 | backtrace_warning_symbol(void *data, char *msg, unsigned long symbol) |
| 1071 | { |
| 1072 | /* Ignore warnings */ |
| 1073 | } |
| 1074 | |
| 1075 | static void backtrace_warning(void *data, char *msg) |
| 1076 | { |
| 1077 | /* Ignore warnings */ |
| 1078 | } |
| 1079 | |
| 1080 | static int backtrace_stack(void *data, char *name) |
| 1081 | { |
| 1082 | /* Don't bother with IRQ stacks for now */ |
| 1083 | return -1; |
| 1084 | } |
| 1085 | |
| 1086 | static void backtrace_address(void *data, unsigned long addr, int reliable) |
| 1087 | { |
| 1088 | struct perf_callchain_entry *entry = data; |
| 1089 | |
| 1090 | if (reliable) |
| 1091 | callchain_store(entry, addr); |
| 1092 | } |
| 1093 | |
| 1094 | static const struct stacktrace_ops backtrace_ops = { |
| 1095 | .warning = backtrace_warning, |
| 1096 | .warning_symbol = backtrace_warning_symbol, |
| 1097 | .stack = backtrace_stack, |
| 1098 | .address = backtrace_address, |
| 1099 | }; |
| 1100 | |
| 1101 | static void |
| 1102 | perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry) |
| 1103 | { |
| 1104 | unsigned long bp; |
| 1105 | char *stack; |
Peter Zijlstra | 5872bdb8 | 2009-04-02 11:12:03 +0200 | [diff] [blame] | 1106 | int nr = entry->nr; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1107 | |
| 1108 | callchain_store(entry, instruction_pointer(regs)); |
| 1109 | |
| 1110 | stack = ((char *)regs + sizeof(struct pt_regs)); |
| 1111 | #ifdef CONFIG_FRAME_POINTER |
| 1112 | bp = frame_pointer(regs); |
| 1113 | #else |
| 1114 | bp = 0; |
| 1115 | #endif |
| 1116 | |
| 1117 | dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, entry); |
Peter Zijlstra | 5872bdb8 | 2009-04-02 11:12:03 +0200 | [diff] [blame] | 1118 | |
| 1119 | entry->kernel = entry->nr - nr; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | |
| 1123 | struct stack_frame { |
| 1124 | const void __user *next_fp; |
| 1125 | unsigned long return_address; |
| 1126 | }; |
| 1127 | |
| 1128 | static int copy_stack_frame(const void __user *fp, struct stack_frame *frame) |
| 1129 | { |
| 1130 | int ret; |
| 1131 | |
| 1132 | if (!access_ok(VERIFY_READ, fp, sizeof(*frame))) |
| 1133 | return 0; |
| 1134 | |
| 1135 | ret = 1; |
| 1136 | pagefault_disable(); |
| 1137 | if (__copy_from_user_inatomic(frame, fp, sizeof(*frame))) |
| 1138 | ret = 0; |
| 1139 | pagefault_enable(); |
| 1140 | |
| 1141 | return ret; |
| 1142 | } |
| 1143 | |
| 1144 | static void |
| 1145 | perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry) |
| 1146 | { |
| 1147 | struct stack_frame frame; |
| 1148 | const void __user *fp; |
Peter Zijlstra | 5872bdb8 | 2009-04-02 11:12:03 +0200 | [diff] [blame] | 1149 | int nr = entry->nr; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1150 | |
| 1151 | regs = (struct pt_regs *)current->thread.sp0 - 1; |
| 1152 | fp = (void __user *)regs->bp; |
| 1153 | |
| 1154 | callchain_store(entry, regs->ip); |
| 1155 | |
| 1156 | while (entry->nr < MAX_STACK_DEPTH) { |
| 1157 | frame.next_fp = NULL; |
| 1158 | frame.return_address = 0; |
| 1159 | |
| 1160 | if (!copy_stack_frame(fp, &frame)) |
| 1161 | break; |
| 1162 | |
| 1163 | if ((unsigned long)fp < user_stack_pointer(regs)) |
| 1164 | break; |
| 1165 | |
| 1166 | callchain_store(entry, frame.return_address); |
| 1167 | fp = frame.next_fp; |
| 1168 | } |
Peter Zijlstra | 5872bdb8 | 2009-04-02 11:12:03 +0200 | [diff] [blame] | 1169 | |
| 1170 | entry->user = entry->nr - nr; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | static void |
| 1174 | perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry) |
| 1175 | { |
| 1176 | int is_user; |
| 1177 | |
| 1178 | if (!regs) |
| 1179 | return; |
| 1180 | |
| 1181 | is_user = user_mode(regs); |
| 1182 | |
| 1183 | if (!current || current->pid == 0) |
| 1184 | return; |
| 1185 | |
| 1186 | if (is_user && current->state != TASK_RUNNING) |
| 1187 | return; |
| 1188 | |
| 1189 | if (!is_user) |
| 1190 | perf_callchain_kernel(regs, entry); |
| 1191 | |
| 1192 | if (current->mm) |
| 1193 | perf_callchain_user(regs, entry); |
| 1194 | } |
| 1195 | |
| 1196 | struct perf_callchain_entry *perf_callchain(struct pt_regs *regs) |
| 1197 | { |
| 1198 | struct perf_callchain_entry *entry; |
| 1199 | |
| 1200 | if (in_nmi()) |
| 1201 | entry = &__get_cpu_var(nmi_entry); |
| 1202 | else |
| 1203 | entry = &__get_cpu_var(irq_entry); |
| 1204 | |
| 1205 | entry->nr = 0; |
Peter Zijlstra | 5872bdb8 | 2009-04-02 11:12:03 +0200 | [diff] [blame] | 1206 | entry->hv = 0; |
| 1207 | entry->kernel = 0; |
| 1208 | entry->user = 0; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1209 | |
| 1210 | perf_do_callchain(regs, entry); |
| 1211 | |
| 1212 | return entry; |
| 1213 | } |