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