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 |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 7 | * |
| 8 | * For licencing details see kernel-base/COPYING |
| 9 | */ |
| 10 | |
| 11 | #include <linux/perf_counter.h> |
| 12 | #include <linux/capability.h> |
| 13 | #include <linux/notifier.h> |
| 14 | #include <linux/hardirq.h> |
| 15 | #include <linux/kprobes.h> |
Thomas Gleixner | 4ac1329 | 2008-12-09 21:43:39 +0100 | [diff] [blame] | 16 | #include <linux/module.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 17 | #include <linux/kdebug.h> |
| 18 | #include <linux/sched.h> |
| 19 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 20 | #include <asm/apic.h> |
| 21 | |
| 22 | static bool perf_counters_initialized __read_mostly; |
| 23 | |
| 24 | /* |
| 25 | * Number of (generic) HW counters: |
| 26 | */ |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 27 | static int nr_counters_generic __read_mostly; |
| 28 | static u64 perf_counter_mask __read_mostly; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 29 | static u64 counter_value_mask __read_mostly; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 30 | static int counter_value_bits __read_mostly; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 31 | |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 32 | static int nr_counters_fixed __read_mostly; |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 33 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 34 | struct cpu_hw_counters { |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 35 | struct perf_counter *counters[X86_PMC_IDX_MAX]; |
| 36 | unsigned long used[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
Mike Galbraith | 4b39fd9 | 2009-01-23 14:36:16 +0100 | [diff] [blame] | 37 | unsigned long interrupts; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 38 | u64 throttle_ctrl; |
Peter Zijlstra | 184fe4ab | 2009-03-08 11:34:19 +0100 | [diff] [blame] | 39 | unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 40 | int enabled; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | /* |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 44 | * struct pmc_x86_ops - performance counter x86 ops |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 45 | */ |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 46 | struct pmc_x86_ops { |
Jaswinder Singh Rajput | 169e41e | 2009-02-28 18:37:49 +0530 | [diff] [blame] | 47 | u64 (*save_disable_all)(void); |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 48 | void (*restore_all)(u64); |
| 49 | u64 (*get_status)(u64); |
| 50 | void (*ack_status)(u64); |
| 51 | void (*enable)(int, u64); |
| 52 | void (*disable)(int, u64); |
Jaswinder Singh Rajput | 169e41e | 2009-02-28 18:37:49 +0530 | [diff] [blame] | 53 | unsigned eventsel; |
| 54 | unsigned perfctr; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 55 | u64 (*event_map)(int); |
| 56 | u64 (*raw_event)(u64); |
Jaswinder Singh Rajput | 169e41e | 2009-02-28 18:37:49 +0530 | [diff] [blame] | 57 | int max_events; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | static struct pmc_x86_ops *pmc_ops; |
| 61 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 62 | static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters) = { |
| 63 | .enabled = 1, |
| 64 | }; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 65 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 66 | /* |
| 67 | * Intel PerfMon v3. Used on Core2 and later. |
| 68 | */ |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 69 | static const u64 intel_perfmon_event_map[] = |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 70 | { |
Ingo Molnar | f650a67 | 2008-12-23 12:17:29 +0100 | [diff] [blame] | 71 | [PERF_COUNT_CPU_CYCLES] = 0x003c, |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 72 | [PERF_COUNT_INSTRUCTIONS] = 0x00c0, |
| 73 | [PERF_COUNT_CACHE_REFERENCES] = 0x4f2e, |
| 74 | [PERF_COUNT_CACHE_MISSES] = 0x412e, |
| 75 | [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4, |
| 76 | [PERF_COUNT_BRANCH_MISSES] = 0x00c5, |
Ingo Molnar | f650a67 | 2008-12-23 12:17:29 +0100 | [diff] [blame] | 77 | [PERF_COUNT_BUS_CYCLES] = 0x013c, |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 78 | }; |
| 79 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 80 | static u64 pmc_intel_event_map(int event) |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 81 | { |
| 82 | return intel_perfmon_event_map[event]; |
| 83 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 84 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 85 | static u64 pmc_intel_raw_event(u64 event) |
| 86 | { |
| 87 | #define CORE_EVNTSEL_EVENT_MASK 0x000000FF |
| 88 | #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00 |
| 89 | #define CORE_EVNTSEL_COUNTER_MASK 0xFF000000 |
| 90 | |
| 91 | #define CORE_EVNTSEL_MASK \ |
| 92 | (CORE_EVNTSEL_EVENT_MASK | \ |
| 93 | CORE_EVNTSEL_UNIT_MASK | \ |
| 94 | CORE_EVNTSEL_COUNTER_MASK) |
| 95 | |
| 96 | return event & CORE_EVNTSEL_MASK; |
| 97 | } |
| 98 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 99 | /* |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 100 | * AMD Performance Monitor K7 and later. |
| 101 | */ |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 102 | static const u64 amd_perfmon_event_map[] = |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 103 | { |
| 104 | [PERF_COUNT_CPU_CYCLES] = 0x0076, |
| 105 | [PERF_COUNT_INSTRUCTIONS] = 0x00c0, |
| 106 | [PERF_COUNT_CACHE_REFERENCES] = 0x0080, |
| 107 | [PERF_COUNT_CACHE_MISSES] = 0x0081, |
| 108 | [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4, |
| 109 | [PERF_COUNT_BRANCH_MISSES] = 0x00c5, |
| 110 | }; |
| 111 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 112 | static u64 pmc_amd_event_map(int event) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 113 | { |
| 114 | return amd_perfmon_event_map[event]; |
| 115 | } |
| 116 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 117 | static u64 pmc_amd_raw_event(u64 event) |
| 118 | { |
| 119 | #define K7_EVNTSEL_EVENT_MASK 0x7000000FF |
| 120 | #define K7_EVNTSEL_UNIT_MASK 0x00000FF00 |
| 121 | #define K7_EVNTSEL_COUNTER_MASK 0x0FF000000 |
| 122 | |
| 123 | #define K7_EVNTSEL_MASK \ |
| 124 | (K7_EVNTSEL_EVENT_MASK | \ |
| 125 | K7_EVNTSEL_UNIT_MASK | \ |
| 126 | K7_EVNTSEL_COUNTER_MASK) |
| 127 | |
| 128 | return event & K7_EVNTSEL_MASK; |
| 129 | } |
| 130 | |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 131 | /* |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 132 | * Propagate counter elapsed time into the generic counter. |
| 133 | * Can only be executed on the CPU where the counter is active. |
| 134 | * Returns the delta events processed. |
| 135 | */ |
| 136 | static void |
| 137 | x86_perf_counter_update(struct perf_counter *counter, |
| 138 | struct hw_perf_counter *hwc, int idx) |
| 139 | { |
| 140 | u64 prev_raw_count, new_raw_count, delta; |
| 141 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 142 | /* |
| 143 | * Careful: an NMI might modify the previous counter value. |
| 144 | * |
| 145 | * Our tactic to handle this is to first atomically read and |
| 146 | * exchange a new raw count - then add that new-prev delta |
| 147 | * count to the generic counter atomically: |
| 148 | */ |
| 149 | again: |
| 150 | prev_raw_count = atomic64_read(&hwc->prev_count); |
| 151 | rdmsrl(hwc->counter_base + idx, new_raw_count); |
| 152 | |
| 153 | if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count, |
| 154 | new_raw_count) != prev_raw_count) |
| 155 | goto again; |
| 156 | |
| 157 | /* |
| 158 | * Now we have the new raw value and have updated the prev |
| 159 | * timestamp already. We can now calculate the elapsed delta |
| 160 | * (counter-)time and add that to the generic counter. |
| 161 | * |
| 162 | * Careful, not all hw sign-extends above the physical width |
| 163 | * of the count, so we do that by clipping the delta to 32 bits: |
| 164 | */ |
| 165 | delta = (u64)(u32)((s32)new_raw_count - (s32)prev_raw_count); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 166 | |
| 167 | atomic64_add(delta, &counter->count); |
| 168 | atomic64_sub(delta, &hwc->period_left); |
| 169 | } |
| 170 | |
| 171 | /* |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 172 | * Setup the hardware configuration for a given hw_event_type |
| 173 | */ |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 174 | static int __hw_perf_counter_init(struct perf_counter *counter) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 175 | { |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 176 | struct perf_counter_hw_event *hw_event = &counter->hw_event; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 177 | struct hw_perf_counter *hwc = &counter->hw; |
| 178 | |
| 179 | if (unlikely(!perf_counters_initialized)) |
| 180 | return -EINVAL; |
| 181 | |
| 182 | /* |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 183 | * Generate PMC IRQs: |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 184 | * (keep 'enabled' bit clear for now) |
| 185 | */ |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 186 | hwc->config = ARCH_PERFMON_EVENTSEL_INT; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 187 | |
| 188 | /* |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 189 | * Count user and OS events unless requested not to. |
| 190 | */ |
| 191 | if (!hw_event->exclude_user) |
| 192 | hwc->config |= ARCH_PERFMON_EVENTSEL_USR; |
| 193 | if (!hw_event->exclude_kernel) |
| 194 | hwc->config |= ARCH_PERFMON_EVENTSEL_OS; |
| 195 | |
| 196 | /* |
| 197 | * If privileged enough, allow NMI events: |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 198 | */ |
| 199 | hwc->nmi = 0; |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 200 | if (capable(CAP_SYS_ADMIN) && hw_event->nmi) |
| 201 | hwc->nmi = 1; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 202 | |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 203 | hwc->irq_period = hw_event->irq_period; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 204 | /* |
| 205 | * Intel PMCs cannot be accessed sanely above 32 bit width, |
| 206 | * so we install an artificial 1<<31 period regardless of |
| 207 | * the generic counter period: |
| 208 | */ |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 209 | if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) |
| 210 | if ((s64)hwc->irq_period <= 0 || hwc->irq_period > 0x7FFFFFFF) |
| 211 | hwc->irq_period = 0x7FFFFFFF; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 212 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 213 | atomic64_set(&hwc->period_left, hwc->irq_period); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 214 | |
| 215 | /* |
Thomas Gleixner | dfa7c89 | 2008-12-08 19:35:37 +0100 | [diff] [blame] | 216 | * Raw event type provide the config in the event structure |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 217 | */ |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 218 | if (hw_event->raw) { |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 219 | hwc->config |= pmc_ops->raw_event(hw_event->type); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 220 | } else { |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 221 | if (hw_event->type >= pmc_ops->max_events) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 222 | return -EINVAL; |
| 223 | /* |
| 224 | * The generic map: |
| 225 | */ |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 226 | hwc->config |= pmc_ops->event_map(hw_event->type); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 227 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 228 | counter->wakeup_pending = 0; |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 233 | static u64 pmc_intel_save_disable_all(void) |
Thomas Gleixner | 4ac1329 | 2008-12-09 21:43:39 +0100 | [diff] [blame] | 234 | { |
| 235 | u64 ctrl; |
| 236 | |
| 237 | rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl); |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 238 | wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0); |
Ingo Molnar | 2b9ff0d | 2008-12-14 18:36:30 +0100 | [diff] [blame] | 239 | |
Thomas Gleixner | 4ac1329 | 2008-12-09 21:43:39 +0100 | [diff] [blame] | 240 | return ctrl; |
| 241 | } |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 242 | |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 243 | static u64 pmc_amd_save_disable_all(void) |
| 244 | { |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 245 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); |
| 246 | int enabled, idx; |
| 247 | |
| 248 | enabled = cpuc->enabled; |
| 249 | cpuc->enabled = 0; |
| 250 | barrier(); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 251 | |
| 252 | for (idx = 0; idx < nr_counters_generic; idx++) { |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 253 | u64 val; |
| 254 | |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 255 | rdmsrl(MSR_K7_EVNTSEL0 + idx, val); |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 256 | if (val & ARCH_PERFMON_EVENTSEL0_ENABLE) { |
| 257 | val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE; |
| 258 | wrmsrl(MSR_K7_EVNTSEL0 + idx, val); |
| 259 | } |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 260 | } |
| 261 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 262 | return enabled; |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 263 | } |
| 264 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 265 | u64 hw_perf_save_disable(void) |
| 266 | { |
| 267 | if (unlikely(!perf_counters_initialized)) |
| 268 | return 0; |
| 269 | |
| 270 | return pmc_ops->save_disable_all(); |
| 271 | } |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 272 | /* |
| 273 | * Exported because of ACPI idle |
| 274 | */ |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 275 | EXPORT_SYMBOL_GPL(hw_perf_save_disable); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 276 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 277 | static void pmc_intel_restore_all(u64 ctrl) |
| 278 | { |
| 279 | wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl); |
| 280 | } |
| 281 | |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 282 | static void pmc_amd_restore_all(u64 ctrl) |
| 283 | { |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 284 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 285 | int idx; |
| 286 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 287 | cpuc->enabled = ctrl; |
| 288 | barrier(); |
| 289 | if (!ctrl) |
| 290 | return; |
| 291 | |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 292 | for (idx = 0; idx < nr_counters_generic; idx++) { |
Peter Zijlstra | 184fe4ab | 2009-03-08 11:34:19 +0100 | [diff] [blame] | 293 | if (test_bit(idx, cpuc->active_mask)) { |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 294 | u64 val; |
| 295 | |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 296 | rdmsrl(MSR_K7_EVNTSEL0 + idx, val); |
| 297 | val |= ARCH_PERFMON_EVENTSEL0_ENABLE; |
| 298 | wrmsrl(MSR_K7_EVNTSEL0 + idx, val); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 303 | void hw_perf_restore(u64 ctrl) |
| 304 | { |
Ingo Molnar | 2b9ff0d | 2008-12-14 18:36:30 +0100 | [diff] [blame] | 305 | if (unlikely(!perf_counters_initialized)) |
| 306 | return; |
| 307 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 308 | pmc_ops->restore_all(ctrl); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 309 | } |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 310 | /* |
| 311 | * Exported because of ACPI idle |
| 312 | */ |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 313 | EXPORT_SYMBOL_GPL(hw_perf_restore); |
| 314 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 315 | static u64 pmc_intel_get_status(u64 mask) |
| 316 | { |
| 317 | u64 status; |
| 318 | |
| 319 | rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status); |
| 320 | |
| 321 | return status; |
| 322 | } |
| 323 | |
| 324 | static u64 pmc_amd_get_status(u64 mask) |
| 325 | { |
| 326 | u64 status = 0; |
| 327 | int idx; |
| 328 | |
| 329 | for (idx = 0; idx < nr_counters_generic; idx++) { |
| 330 | s64 val; |
| 331 | |
| 332 | if (!(mask & (1 << idx))) |
| 333 | continue; |
| 334 | |
| 335 | rdmsrl(MSR_K7_PERFCTR0 + idx, val); |
| 336 | val <<= (64 - counter_value_bits); |
| 337 | if (val >= 0) |
| 338 | status |= (1 << idx); |
| 339 | } |
| 340 | |
| 341 | return status; |
| 342 | } |
| 343 | |
| 344 | static u64 hw_perf_get_status(u64 mask) |
| 345 | { |
| 346 | if (unlikely(!perf_counters_initialized)) |
| 347 | return 0; |
| 348 | |
| 349 | return pmc_ops->get_status(mask); |
| 350 | } |
| 351 | |
| 352 | static void pmc_intel_ack_status(u64 ack) |
| 353 | { |
| 354 | wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack); |
| 355 | } |
| 356 | |
| 357 | static void pmc_amd_ack_status(u64 ack) |
| 358 | { |
| 359 | } |
| 360 | |
| 361 | static void hw_perf_ack_status(u64 ack) |
| 362 | { |
| 363 | if (unlikely(!perf_counters_initialized)) |
| 364 | return; |
| 365 | |
| 366 | pmc_ops->ack_status(ack); |
| 367 | } |
| 368 | |
| 369 | static void pmc_intel_enable(int idx, u64 config) |
| 370 | { |
| 371 | wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx, |
| 372 | config | ARCH_PERFMON_EVENTSEL0_ENABLE); |
| 373 | } |
| 374 | |
| 375 | static void pmc_amd_enable(int idx, u64 config) |
| 376 | { |
| 377 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); |
| 378 | |
Peter Zijlstra | 184fe4ab | 2009-03-08 11:34:19 +0100 | [diff] [blame] | 379 | set_bit(idx, cpuc->active_mask); |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 380 | if (cpuc->enabled) |
| 381 | config |= ARCH_PERFMON_EVENTSEL0_ENABLE; |
| 382 | |
| 383 | wrmsrl(MSR_K7_EVNTSEL0 + idx, config); |
| 384 | } |
| 385 | |
| 386 | static void hw_perf_enable(int idx, u64 config) |
| 387 | { |
| 388 | if (unlikely(!perf_counters_initialized)) |
| 389 | return; |
| 390 | |
| 391 | pmc_ops->enable(idx, config); |
| 392 | } |
| 393 | |
| 394 | static void pmc_intel_disable(int idx, u64 config) |
| 395 | { |
| 396 | wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx, config); |
| 397 | } |
| 398 | |
| 399 | static void pmc_amd_disable(int idx, u64 config) |
| 400 | { |
| 401 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); |
| 402 | |
Peter Zijlstra | 184fe4ab | 2009-03-08 11:34:19 +0100 | [diff] [blame] | 403 | clear_bit(idx, cpuc->active_mask); |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 404 | wrmsrl(MSR_K7_EVNTSEL0 + idx, config); |
| 405 | |
| 406 | } |
| 407 | |
| 408 | static void hw_perf_disable(int idx, u64 config) |
| 409 | { |
| 410 | if (unlikely(!perf_counters_initialized)) |
| 411 | return; |
| 412 | |
| 413 | pmc_ops->disable(idx, config); |
| 414 | } |
| 415 | |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 416 | static inline void |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 417 | __pmc_fixed_disable(struct perf_counter *counter, |
| 418 | struct hw_perf_counter *hwc, unsigned int __idx) |
| 419 | { |
| 420 | int idx = __idx - X86_PMC_IDX_FIXED; |
| 421 | u64 ctrl_val, mask; |
| 422 | int err; |
| 423 | |
| 424 | mask = 0xfULL << (idx * 4); |
| 425 | |
| 426 | rdmsrl(hwc->config_base, ctrl_val); |
| 427 | ctrl_val &= ~mask; |
| 428 | err = checking_wrmsrl(hwc->config_base, ctrl_val); |
| 429 | } |
| 430 | |
| 431 | static inline void |
Ingo Molnar | eb2b861 | 2008-12-17 09:09:13 +0100 | [diff] [blame] | 432 | __pmc_generic_disable(struct perf_counter *counter, |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 433 | struct hw_perf_counter *hwc, unsigned int idx) |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 434 | { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 435 | if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) |
Jaswinder Singh Rajput | 2b583d8 | 2008-12-27 19:15:43 +0530 | [diff] [blame] | 436 | __pmc_fixed_disable(counter, hwc, idx); |
| 437 | else |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 438 | hw_perf_disable(idx, hwc->config); |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 439 | } |
| 440 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 441 | static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 442 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 443 | /* |
| 444 | * Set the next IRQ period, based on the hwc->period_left value. |
| 445 | * To be called with the counter disabled in hw: |
| 446 | */ |
| 447 | static void |
| 448 | __hw_perf_counter_set_period(struct perf_counter *counter, |
| 449 | struct hw_perf_counter *hwc, int idx) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 450 | { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 451 | s64 left = atomic64_read(&hwc->period_left); |
Peter Zijlstra | 595258a | 2009-03-13 12:21:28 +0100 | [diff] [blame^] | 452 | s64 period = hwc->irq_period; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 453 | int err; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 454 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 455 | /* |
| 456 | * If we are way outside a reasoable range then just skip forward: |
| 457 | */ |
| 458 | if (unlikely(left <= -period)) { |
| 459 | left = period; |
| 460 | atomic64_set(&hwc->period_left, left); |
| 461 | } |
| 462 | |
| 463 | if (unlikely(left <= 0)) { |
| 464 | left += period; |
| 465 | atomic64_set(&hwc->period_left, left); |
| 466 | } |
| 467 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 468 | per_cpu(prev_left[idx], smp_processor_id()) = left; |
| 469 | |
| 470 | /* |
| 471 | * The hw counter starts counting from this counter offset, |
| 472 | * mark it to be able to extra future deltas: |
| 473 | */ |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 474 | atomic64_set(&hwc->prev_count, (u64)-left); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 475 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 476 | err = checking_wrmsrl(hwc->counter_base + idx, |
| 477 | (u64)(-left) & counter_value_mask); |
| 478 | } |
| 479 | |
| 480 | static inline void |
| 481 | __pmc_fixed_enable(struct perf_counter *counter, |
| 482 | struct hw_perf_counter *hwc, unsigned int __idx) |
| 483 | { |
| 484 | int idx = __idx - X86_PMC_IDX_FIXED; |
| 485 | u64 ctrl_val, bits, mask; |
| 486 | int err; |
| 487 | |
| 488 | /* |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 489 | * Enable IRQ generation (0x8), |
| 490 | * and enable ring-3 counting (0x2) and ring-0 counting (0x1) |
| 491 | * if requested: |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 492 | */ |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 493 | bits = 0x8ULL; |
| 494 | if (hwc->config & ARCH_PERFMON_EVENTSEL_USR) |
| 495 | bits |= 0x2; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 496 | if (hwc->config & ARCH_PERFMON_EVENTSEL_OS) |
| 497 | bits |= 0x1; |
| 498 | bits <<= (idx * 4); |
| 499 | mask = 0xfULL << (idx * 4); |
| 500 | |
| 501 | rdmsrl(hwc->config_base, ctrl_val); |
| 502 | ctrl_val &= ~mask; |
| 503 | ctrl_val |= bits; |
| 504 | err = checking_wrmsrl(hwc->config_base, ctrl_val); |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 505 | } |
| 506 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 507 | static void |
Ingo Molnar | eb2b861 | 2008-12-17 09:09:13 +0100 | [diff] [blame] | 508 | __pmc_generic_enable(struct perf_counter *counter, |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 509 | struct hw_perf_counter *hwc, int idx) |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 510 | { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 511 | if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) |
Jaswinder Singh Rajput | 2b583d8 | 2008-12-27 19:15:43 +0530 | [diff] [blame] | 512 | __pmc_fixed_enable(counter, hwc, idx); |
| 513 | else |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 514 | hw_perf_enable(idx, hwc->config); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 515 | } |
| 516 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 517 | static int |
| 518 | fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc) |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 519 | { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 520 | unsigned int event; |
| 521 | |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 522 | if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) |
| 523 | return -1; |
| 524 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 525 | if (unlikely(hwc->nmi)) |
| 526 | return -1; |
| 527 | |
| 528 | event = hwc->config & ARCH_PERFMON_EVENT_MASK; |
| 529 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 530 | if (unlikely(event == pmc_ops->event_map(PERF_COUNT_INSTRUCTIONS))) |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 531 | return X86_PMC_IDX_FIXED_INSTRUCTIONS; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 532 | if (unlikely(event == pmc_ops->event_map(PERF_COUNT_CPU_CYCLES))) |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 533 | return X86_PMC_IDX_FIXED_CPU_CYCLES; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 534 | if (unlikely(event == pmc_ops->event_map(PERF_COUNT_BUS_CYCLES))) |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 535 | return X86_PMC_IDX_FIXED_BUS_CYCLES; |
| 536 | |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 537 | return -1; |
| 538 | } |
| 539 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 540 | /* |
| 541 | * Find a PMC slot for the freshly enabled / scheduled in counter: |
| 542 | */ |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 543 | static int pmc_generic_enable(struct perf_counter *counter) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 544 | { |
| 545 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); |
| 546 | struct hw_perf_counter *hwc = &counter->hw; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 547 | int idx; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 548 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 549 | idx = fixed_mode_idx(counter, hwc); |
| 550 | if (idx >= 0) { |
| 551 | /* |
| 552 | * Try to get the fixed counter, if that is already taken |
| 553 | * then try to get a generic counter: |
| 554 | */ |
| 555 | if (test_and_set_bit(idx, cpuc->used)) |
| 556 | goto try_generic; |
Ingo Molnar | 0dff86a | 2008-12-23 12:28:12 +0100 | [diff] [blame] | 557 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 558 | hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL; |
| 559 | /* |
| 560 | * We set it so that counter_base + idx in wrmsr/rdmsr maps to |
| 561 | * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2: |
| 562 | */ |
| 563 | hwc->counter_base = |
| 564 | MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 565 | hwc->idx = idx; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 566 | } else { |
| 567 | idx = hwc->idx; |
| 568 | /* Try to get the previous generic counter again */ |
| 569 | if (test_and_set_bit(idx, cpuc->used)) { |
| 570 | try_generic: |
| 571 | idx = find_first_zero_bit(cpuc->used, nr_counters_generic); |
| 572 | if (idx == nr_counters_generic) |
| 573 | return -EAGAIN; |
| 574 | |
| 575 | set_bit(idx, cpuc->used); |
| 576 | hwc->idx = idx; |
| 577 | } |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 578 | hwc->config_base = pmc_ops->eventsel; |
| 579 | hwc->counter_base = pmc_ops->perfctr; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | perf_counters_lapic_init(hwc->nmi); |
| 583 | |
Ingo Molnar | eb2b861 | 2008-12-17 09:09:13 +0100 | [diff] [blame] | 584 | __pmc_generic_disable(counter, hwc, idx); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 585 | |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 586 | cpuc->counters[idx] = counter; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 587 | /* |
| 588 | * Make it visible before enabling the hw: |
| 589 | */ |
| 590 | smp_wmb(); |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 591 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 592 | __hw_perf_counter_set_period(counter, hwc, idx); |
Ingo Molnar | eb2b861 | 2008-12-17 09:09:13 +0100 | [diff] [blame] | 593 | __pmc_generic_enable(counter, hwc, idx); |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 594 | |
| 595 | return 0; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | void perf_counter_print_debug(void) |
| 599 | { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 600 | u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed; |
Ingo Molnar | 0dff86a | 2008-12-23 12:28:12 +0100 | [diff] [blame] | 601 | struct cpu_hw_counters *cpuc; |
Ingo Molnar | 1e12567 | 2008-12-09 12:18:18 +0100 | [diff] [blame] | 602 | int cpu, idx; |
| 603 | |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 604 | if (!nr_counters_generic) |
Ingo Molnar | 1e12567 | 2008-12-09 12:18:18 +0100 | [diff] [blame] | 605 | return; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 606 | |
| 607 | local_irq_disable(); |
| 608 | |
| 609 | cpu = smp_processor_id(); |
Ingo Molnar | 0dff86a | 2008-12-23 12:28:12 +0100 | [diff] [blame] | 610 | cpuc = &per_cpu(cpu_hw_counters, cpu); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 611 | |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 612 | if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) { |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 613 | rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl); |
| 614 | rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status); |
| 615 | rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow); |
| 616 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 617 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 618 | pr_info("\n"); |
| 619 | pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl); |
| 620 | pr_info("CPU#%d: status: %016llx\n", cpu, status); |
| 621 | pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow); |
| 622 | pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 623 | } |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 624 | pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 625 | |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 626 | for (idx = 0; idx < nr_counters_generic; idx++) { |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 627 | rdmsrl(pmc_ops->eventsel + idx, pmc_ctrl); |
| 628 | rdmsrl(pmc_ops->perfctr + idx, pmc_count); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 629 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 630 | prev_left = per_cpu(prev_left[idx], cpu); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 631 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 632 | pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n", |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 633 | cpu, idx, pmc_ctrl); |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 634 | pr_info("CPU#%d: gen-PMC%d count: %016llx\n", |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 635 | cpu, idx, pmc_count); |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 636 | pr_info("CPU#%d: gen-PMC%d left: %016llx\n", |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 637 | cpu, idx, prev_left); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 638 | } |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 639 | for (idx = 0; idx < nr_counters_fixed; idx++) { |
| 640 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count); |
| 641 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 642 | pr_info("CPU#%d: fixed-PMC%d count: %016llx\n", |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 643 | cpu, idx, pmc_count); |
| 644 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 645 | local_irq_enable(); |
| 646 | } |
| 647 | |
Ingo Molnar | eb2b861 | 2008-12-17 09:09:13 +0100 | [diff] [blame] | 648 | static void pmc_generic_disable(struct perf_counter *counter) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 649 | { |
| 650 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); |
| 651 | struct hw_perf_counter *hwc = &counter->hw; |
| 652 | unsigned int idx = hwc->idx; |
| 653 | |
Ingo Molnar | eb2b861 | 2008-12-17 09:09:13 +0100 | [diff] [blame] | 654 | __pmc_generic_disable(counter, hwc, idx); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 655 | |
| 656 | clear_bit(idx, cpuc->used); |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 657 | cpuc->counters[idx] = NULL; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 658 | /* |
| 659 | * Make sure the cleared pointer becomes visible before we |
| 660 | * (potentially) free the counter: |
| 661 | */ |
| 662 | smp_wmb(); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 663 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 664 | /* |
| 665 | * Drain the remaining delta count out of a counter |
| 666 | * that we are disabling: |
| 667 | */ |
| 668 | x86_perf_counter_update(counter, hwc, idx); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | static void perf_store_irq_data(struct perf_counter *counter, u64 data) |
| 672 | { |
| 673 | struct perf_data *irqdata = counter->irqdata; |
| 674 | |
| 675 | if (irqdata->len > PERF_DATA_BUFLEN - sizeof(u64)) { |
| 676 | irqdata->overrun++; |
| 677 | } else { |
| 678 | u64 *p = (u64 *) &irqdata->data[irqdata->len]; |
| 679 | |
| 680 | *p = data; |
| 681 | irqdata->len += sizeof(u64); |
| 682 | } |
| 683 | } |
| 684 | |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 685 | /* |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 686 | * Save and restart an expired counter. Called by NMI contexts, |
| 687 | * so it has to be careful about preempting normal counter ops: |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 688 | */ |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 689 | static void perf_save_and_restart(struct perf_counter *counter) |
| 690 | { |
| 691 | struct hw_perf_counter *hwc = &counter->hw; |
| 692 | int idx = hwc->idx; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 693 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 694 | x86_perf_counter_update(counter, hwc, idx); |
| 695 | __hw_perf_counter_set_period(counter, hwc, idx); |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 696 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 697 | if (counter->state == PERF_COUNTER_STATE_ACTIVE) |
Ingo Molnar | eb2b861 | 2008-12-17 09:09:13 +0100 | [diff] [blame] | 698 | __pmc_generic_enable(counter, hwc, idx); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | static void |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 702 | perf_handle_group(struct perf_counter *sibling, u64 *status, u64 *overflown) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 703 | { |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 704 | struct perf_counter *counter, *group_leader = sibling->group_leader; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 705 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 706 | /* |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 707 | * Store sibling timestamps (if any): |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 708 | */ |
| 709 | list_for_each_entry(counter, &group_leader->sibling_list, list_entry) { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 710 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 711 | x86_perf_counter_update(counter, &counter->hw, counter->hw.idx); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 712 | perf_store_irq_data(sibling, counter->hw_event.type); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 713 | perf_store_irq_data(sibling, atomic64_read(&counter->count)); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
| 717 | /* |
Mike Galbraith | 4b39fd9 | 2009-01-23 14:36:16 +0100 | [diff] [blame] | 718 | * Maximum interrupt frequency of 100KHz per CPU |
| 719 | */ |
Jaswinder Singh Rajput | 169e41e | 2009-02-28 18:37:49 +0530 | [diff] [blame] | 720 | #define PERFMON_MAX_INTERRUPTS (100000/HZ) |
Mike Galbraith | 4b39fd9 | 2009-01-23 14:36:16 +0100 | [diff] [blame] | 721 | |
| 722 | /* |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 723 | * This handler is triggered by the local APIC, so the APIC IRQ handling |
| 724 | * rules apply: |
| 725 | */ |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 726 | static int __smp_perf_counter_interrupt(struct pt_regs *regs, int nmi) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 727 | { |
| 728 | int bit, cpu = smp_processor_id(); |
Mike Galbraith | 4b39fd9 | 2009-01-23 14:36:16 +0100 | [diff] [blame] | 729 | u64 ack, status; |
Mike Galbraith | 1b023a9 | 2009-01-23 10:13:01 +0100 | [diff] [blame] | 730 | struct cpu_hw_counters *cpuc = &per_cpu(cpu_hw_counters, cpu); |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 731 | int ret = 0; |
Ingo Molnar | 43874d2 | 2008-12-09 12:23:59 +0100 | [diff] [blame] | 732 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 733 | cpuc->throttle_ctrl = hw_perf_save_disable(); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 734 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 735 | status = hw_perf_get_status(cpuc->throttle_ctrl); |
Ingo Molnar | 87b9cf4 | 2008-12-08 14:20:16 +0100 | [diff] [blame] | 736 | if (!status) |
| 737 | goto out; |
| 738 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 739 | ret = 1; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 740 | again: |
Mike Galbraith | d278c48 | 2009-02-09 07:38:50 +0100 | [diff] [blame] | 741 | inc_irq_stat(apic_perf_irqs); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 742 | ack = status; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 743 | for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) { |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 744 | struct perf_counter *counter = cpuc->counters[bit]; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 745 | |
| 746 | clear_bit(bit, (unsigned long *) &status); |
| 747 | if (!counter) |
| 748 | continue; |
| 749 | |
| 750 | perf_save_and_restart(counter); |
| 751 | |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 752 | switch (counter->hw_event.record_type) { |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 753 | case PERF_RECORD_SIMPLE: |
| 754 | continue; |
| 755 | case PERF_RECORD_IRQ: |
| 756 | perf_store_irq_data(counter, instruction_pointer(regs)); |
| 757 | break; |
| 758 | case PERF_RECORD_GROUP: |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 759 | perf_handle_group(counter, &status, &ack); |
| 760 | break; |
| 761 | } |
| 762 | /* |
| 763 | * From NMI context we cannot call into the scheduler to |
Ingo Molnar | eb2b861 | 2008-12-17 09:09:13 +0100 | [diff] [blame] | 764 | * do a task wakeup - but we mark these generic as |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 765 | * wakeup_pending and initate a wakeup callback: |
| 766 | */ |
| 767 | if (nmi) { |
| 768 | counter->wakeup_pending = 1; |
| 769 | set_tsk_thread_flag(current, TIF_PERF_COUNTERS); |
| 770 | } else { |
| 771 | wake_up(&counter->waitq); |
| 772 | } |
| 773 | } |
| 774 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 775 | hw_perf_ack_status(ack); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 776 | |
| 777 | /* |
| 778 | * Repeat if there is more work to be done: |
| 779 | */ |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 780 | status = hw_perf_get_status(cpuc->throttle_ctrl); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 781 | if (status) |
| 782 | goto again; |
Ingo Molnar | 87b9cf4 | 2008-12-08 14:20:16 +0100 | [diff] [blame] | 783 | out: |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 784 | /* |
Mike Galbraith | 1b023a9 | 2009-01-23 10:13:01 +0100 | [diff] [blame] | 785 | * Restore - do not reenable when global enable is off or throttled: |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 786 | */ |
Mike Galbraith | 4b39fd9 | 2009-01-23 14:36:16 +0100 | [diff] [blame] | 787 | if (++cpuc->interrupts < PERFMON_MAX_INTERRUPTS) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 788 | hw_perf_restore(cpuc->throttle_ctrl); |
| 789 | |
| 790 | return ret; |
Mike Galbraith | 1b023a9 | 2009-01-23 10:13:01 +0100 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | void perf_counter_unthrottle(void) |
| 794 | { |
| 795 | struct cpu_hw_counters *cpuc; |
| 796 | |
| 797 | if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) |
| 798 | return; |
| 799 | |
| 800 | if (unlikely(!perf_counters_initialized)) |
| 801 | return; |
| 802 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 803 | cpuc = &__get_cpu_var(cpu_hw_counters); |
Mike Galbraith | 4b39fd9 | 2009-01-23 14:36:16 +0100 | [diff] [blame] | 804 | if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS) { |
Mike Galbraith | 1b023a9 | 2009-01-23 10:13:01 +0100 | [diff] [blame] | 805 | if (printk_ratelimit()) |
Mike Galbraith | 4b39fd9 | 2009-01-23 14:36:16 +0100 | [diff] [blame] | 806 | printk(KERN_WARNING "PERFMON: max interrupts exceeded!\n"); |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 807 | hw_perf_restore(cpuc->throttle_ctrl); |
Mike Galbraith | 1b023a9 | 2009-01-23 10:13:01 +0100 | [diff] [blame] | 808 | } |
Mike Galbraith | 4b39fd9 | 2009-01-23 14:36:16 +0100 | [diff] [blame] | 809 | cpuc->interrupts = 0; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | void smp_perf_counter_interrupt(struct pt_regs *regs) |
| 813 | { |
| 814 | irq_enter(); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 815 | apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR); |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 816 | ack_APIC_irq(); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 817 | __smp_perf_counter_interrupt(regs, 0); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 818 | irq_exit(); |
| 819 | } |
| 820 | |
| 821 | /* |
| 822 | * This handler is triggered by NMI contexts: |
| 823 | */ |
| 824 | void perf_counter_notify(struct pt_regs *regs) |
| 825 | { |
| 826 | struct cpu_hw_counters *cpuc; |
| 827 | unsigned long flags; |
| 828 | int bit, cpu; |
| 829 | |
| 830 | local_irq_save(flags); |
| 831 | cpu = smp_processor_id(); |
| 832 | cpuc = &per_cpu(cpu_hw_counters, cpu); |
| 833 | |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 834 | for_each_bit(bit, cpuc->used, X86_PMC_IDX_MAX) { |
| 835 | struct perf_counter *counter = cpuc->counters[bit]; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 836 | |
| 837 | if (!counter) |
| 838 | continue; |
| 839 | |
| 840 | if (counter->wakeup_pending) { |
| 841 | counter->wakeup_pending = 0; |
| 842 | wake_up(&counter->waitq); |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | local_irq_restore(flags); |
| 847 | } |
| 848 | |
Mike Galbraith | 3415dd9 | 2009-01-23 14:16:53 +0100 | [diff] [blame] | 849 | void perf_counters_lapic_init(int nmi) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 850 | { |
| 851 | u32 apic_val; |
| 852 | |
| 853 | if (!perf_counters_initialized) |
| 854 | return; |
| 855 | /* |
| 856 | * Enable the performance counter vector in the APIC LVT: |
| 857 | */ |
| 858 | apic_val = apic_read(APIC_LVTERR); |
| 859 | |
| 860 | apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED); |
| 861 | if (nmi) |
| 862 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
| 863 | else |
| 864 | apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR); |
| 865 | apic_write(APIC_LVTERR, apic_val); |
| 866 | } |
| 867 | |
| 868 | static int __kprobes |
| 869 | perf_counter_nmi_handler(struct notifier_block *self, |
| 870 | unsigned long cmd, void *__args) |
| 871 | { |
| 872 | struct die_args *args = __args; |
| 873 | struct pt_regs *regs; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 874 | int ret; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 875 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 876 | switch (cmd) { |
| 877 | case DIE_NMI: |
| 878 | case DIE_NMI_IPI: |
| 879 | break; |
| 880 | |
| 881 | default: |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 882 | return NOTIFY_DONE; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 883 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 884 | |
| 885 | regs = args->regs; |
| 886 | |
| 887 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 888 | ret = __smp_perf_counter_interrupt(regs, 1); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 889 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 890 | return ret ? NOTIFY_STOP : NOTIFY_OK; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | static __read_mostly struct notifier_block perf_counter_nmi_notifier = { |
Mike Galbraith | 5b75af0 | 2009-02-04 17:11:34 +0100 | [diff] [blame] | 894 | .notifier_call = perf_counter_nmi_handler, |
| 895 | .next = NULL, |
| 896 | .priority = 1 |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 897 | }; |
| 898 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 899 | static struct pmc_x86_ops pmc_intel_ops = { |
| 900 | .save_disable_all = pmc_intel_save_disable_all, |
| 901 | .restore_all = pmc_intel_restore_all, |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 902 | .get_status = pmc_intel_get_status, |
| 903 | .ack_status = pmc_intel_ack_status, |
| 904 | .enable = pmc_intel_enable, |
| 905 | .disable = pmc_intel_disable, |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 906 | .eventsel = MSR_ARCH_PERFMON_EVENTSEL0, |
| 907 | .perfctr = MSR_ARCH_PERFMON_PERFCTR0, |
| 908 | .event_map = pmc_intel_event_map, |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 909 | .raw_event = pmc_intel_raw_event, |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 910 | .max_events = ARRAY_SIZE(intel_perfmon_event_map), |
| 911 | }; |
| 912 | |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 913 | static struct pmc_x86_ops pmc_amd_ops = { |
| 914 | .save_disable_all = pmc_amd_save_disable_all, |
| 915 | .restore_all = pmc_amd_restore_all, |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 916 | .get_status = pmc_amd_get_status, |
| 917 | .ack_status = pmc_amd_ack_status, |
| 918 | .enable = pmc_amd_enable, |
| 919 | .disable = pmc_amd_disable, |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 920 | .eventsel = MSR_K7_EVNTSEL0, |
| 921 | .perfctr = MSR_K7_PERFCTR0, |
| 922 | .event_map = pmc_amd_event_map, |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 923 | .raw_event = pmc_amd_raw_event, |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 924 | .max_events = ARRAY_SIZE(amd_perfmon_event_map), |
| 925 | }; |
| 926 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 927 | static struct pmc_x86_ops *pmc_intel_init(void) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 928 | { |
| 929 | union cpuid10_eax eax; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 930 | unsigned int ebx; |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 931 | unsigned int unused; |
| 932 | union cpuid10_edx edx; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 933 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 934 | /* |
| 935 | * Check whether the Architectural PerfMon supports |
| 936 | * Branch Misses Retired Event or not. |
| 937 | */ |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 938 | cpuid(10, &eax.full, &ebx, &unused, &edx.full); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 939 | if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED) |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 940 | return NULL; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 941 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 942 | pr_info("Intel Performance Monitoring support detected.\n"); |
| 943 | pr_info("... version: %d\n", eax.split.version_id); |
| 944 | pr_info("... bit width: %d\n", eax.split.bit_width); |
| 945 | pr_info("... mask length: %d\n", eax.split.mask_length); |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 946 | |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 947 | nr_counters_generic = eax.split.num_counters; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 948 | nr_counters_fixed = edx.split.num_counters_fixed; |
| 949 | counter_value_mask = (1ULL << eax.split.bit_width) - 1; |
| 950 | |
| 951 | return &pmc_intel_ops; |
| 952 | } |
| 953 | |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 954 | static struct pmc_x86_ops *pmc_amd_init(void) |
| 955 | { |
| 956 | nr_counters_generic = 4; |
| 957 | nr_counters_fixed = 0; |
Peter Zijlstra | b5e8acf | 2009-03-05 20:34:21 +0100 | [diff] [blame] | 958 | counter_value_mask = 0x0000FFFFFFFFFFFFULL; |
| 959 | counter_value_bits = 48; |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 960 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 961 | pr_info("AMD Performance Monitoring support detected.\n"); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 962 | |
| 963 | return &pmc_amd_ops; |
| 964 | } |
| 965 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 966 | void __init init_hw_perf_counters(void) |
| 967 | { |
| 968 | if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) |
| 969 | return; |
| 970 | |
| 971 | switch (boot_cpu_data.x86_vendor) { |
| 972 | case X86_VENDOR_INTEL: |
| 973 | pmc_ops = pmc_intel_init(); |
| 974 | break; |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 975 | case X86_VENDOR_AMD: |
| 976 | pmc_ops = pmc_amd_init(); |
| 977 | break; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 978 | } |
| 979 | if (!pmc_ops) |
| 980 | return; |
| 981 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 982 | pr_info("... num counters: %d\n", nr_counters_generic); |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 983 | if (nr_counters_generic > X86_PMC_MAX_GENERIC) { |
| 984 | nr_counters_generic = X86_PMC_MAX_GENERIC; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 985 | WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!", |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 986 | nr_counters_generic, X86_PMC_MAX_GENERIC); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 987 | } |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 988 | perf_counter_mask = (1 << nr_counters_generic) - 1; |
| 989 | perf_max_counters = nr_counters_generic; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 990 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 991 | pr_info("... value mask: %016Lx\n", counter_value_mask); |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 992 | |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 993 | if (nr_counters_fixed > X86_PMC_MAX_FIXED) { |
| 994 | nr_counters_fixed = X86_PMC_MAX_FIXED; |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 995 | WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!", |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 996 | nr_counters_fixed, X86_PMC_MAX_FIXED); |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 997 | } |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 998 | pr_info("... fixed counters: %d\n", nr_counters_fixed); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 999 | |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 1000 | perf_counter_mask |= ((1LL << nr_counters_fixed)-1) << X86_PMC_IDX_FIXED; |
| 1001 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1002 | pr_info("... counter mask: %016Lx\n", perf_counter_mask); |
Ingo Molnar | 75f224c | 2008-12-14 21:58:46 +0100 | [diff] [blame] | 1003 | perf_counters_initialized = true; |
| 1004 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1005 | perf_counters_lapic_init(0); |
| 1006 | register_die_notifier(&perf_counter_nmi_notifier); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1007 | } |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1008 | |
Ingo Molnar | eb2b861 | 2008-12-17 09:09:13 +0100 | [diff] [blame] | 1009 | static void pmc_generic_read(struct perf_counter *counter) |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1010 | { |
| 1011 | x86_perf_counter_update(counter, &counter->hw, counter->hw.idx); |
| 1012 | } |
| 1013 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 1014 | static const struct hw_perf_counter_ops x86_perf_counter_ops = { |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 1015 | .enable = pmc_generic_enable, |
| 1016 | .disable = pmc_generic_disable, |
| 1017 | .read = pmc_generic_read, |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1018 | }; |
| 1019 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 1020 | const struct hw_perf_counter_ops * |
| 1021 | hw_perf_counter_init(struct perf_counter *counter) |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1022 | { |
| 1023 | int err; |
| 1024 | |
| 1025 | err = __hw_perf_counter_init(counter); |
| 1026 | if (err) |
| 1027 | return NULL; |
| 1028 | |
| 1029 | return &x86_perf_counter_ops; |
| 1030 | } |