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