Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2 | * Performance events x86 architecture code |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 3 | * |
Ingo Molnar | 9814451 | 2009-04-29 14:52:50 +0200 | [diff] [blame] | 4 | * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de> |
| 5 | * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar |
| 6 | * Copyright (C) 2009 Jaswinder Singh Rajput |
| 7 | * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter |
| 8 | * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 9 | * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com> |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 10 | * Copyright (C) 2009 Google, Inc., Stephane Eranian |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 11 | * |
| 12 | * For licencing details see kernel-base/COPYING |
| 13 | */ |
| 14 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 15 | #include <linux/perf_event.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 16 | #include <linux/capability.h> |
| 17 | #include <linux/notifier.h> |
| 18 | #include <linux/hardirq.h> |
| 19 | #include <linux/kprobes.h> |
Thomas Gleixner | 4ac1329 | 2008-12-09 21:43:39 +0100 | [diff] [blame] | 20 | #include <linux/module.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 21 | #include <linux/kdebug.h> |
| 22 | #include <linux/sched.h> |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 23 | #include <linux/uaccess.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 25 | #include <linux/cpu.h> |
Peter Zijlstra | 272d30b | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 26 | #include <linux/bitops.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 27 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 28 | #include <asm/apic.h> |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 29 | #include <asm/stacktrace.h> |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 30 | #include <asm/nmi.h> |
Torok Edwin | 257ef9d | 2010-03-17 12:07:16 +0200 | [diff] [blame] | 31 | #include <asm/compat.h> |
Lin Ming | 6909262 | 2011-03-03 10:34:50 +0800 | [diff] [blame] | 32 | #include <asm/smp.h> |
Robert Richter | c8e5910 | 2011-04-16 02:27:55 +0200 | [diff] [blame] | 33 | #include <asm/alternative.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 34 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 35 | #include "perf_event.h" |
| 36 | |
Peter Zijlstra | 7645a24 | 2010-03-08 13:51:31 +0100 | [diff] [blame] | 37 | #if 0 |
| 38 | #undef wrmsrl |
| 39 | #define wrmsrl(msr, val) \ |
| 40 | do { \ |
| 41 | trace_printk("wrmsrl(%lx, %lx)\n", (unsigned long)(msr),\ |
| 42 | (unsigned long)(val)); \ |
| 43 | native_write_msr((msr), (u32)((u64)(val)), \ |
| 44 | (u32)((u64)(val) >> 32)); \ |
| 45 | } while (0) |
| 46 | #endif |
| 47 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 48 | struct x86_pmu x86_pmu __read_mostly; |
Stephane Eranian | efc9f05 | 2011-06-06 16:57:03 +0200 | [diff] [blame] | 49 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 50 | DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 51 | .enabled = 1, |
| 52 | }; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 53 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 54 | u64 __read_mostly hw_cache_event_ids |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 55 | [PERF_COUNT_HW_CACHE_MAX] |
| 56 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 57 | [PERF_COUNT_HW_CACHE_RESULT_MAX]; |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 58 | u64 __read_mostly hw_cache_extra_regs |
Andi Kleen | e994d7d | 2011-03-03 10:34:48 +0800 | [diff] [blame] | 59 | [PERF_COUNT_HW_CACHE_MAX] |
| 60 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 61 | [PERF_COUNT_HW_CACHE_RESULT_MAX]; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 62 | |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 63 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 64 | * Propagate event elapsed time into the generic event. |
| 65 | * Can only be executed on the CPU where the event is active. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 66 | * Returns the delta events processed. |
| 67 | */ |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 68 | u64 x86_perf_event_update(struct perf_event *event) |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 69 | { |
Peter Zijlstra | cc2ad4b | 2010-03-02 20:18:39 +0100 | [diff] [blame] | 70 | struct hw_perf_event *hwc = &event->hw; |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 71 | int shift = 64 - x86_pmu.cntval_bits; |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 72 | u64 prev_raw_count, new_raw_count; |
Peter Zijlstra | cc2ad4b | 2010-03-02 20:18:39 +0100 | [diff] [blame] | 73 | int idx = hwc->idx; |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 74 | s64 delta; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 75 | |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 76 | if (idx == X86_PMC_IDX_FIXED_BTS) |
| 77 | return 0; |
| 78 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 79 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 80 | * Careful: an NMI might modify the previous event value. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 81 | * |
| 82 | * Our tactic to handle this is to first atomically read and |
| 83 | * exchange a new raw count - then add that new-prev delta |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 84 | * count to the generic event atomically: |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 85 | */ |
| 86 | again: |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 87 | prev_raw_count = local64_read(&hwc->prev_count); |
Robert Richter | 73d6e52 | 2011-02-02 17:40:59 +0100 | [diff] [blame] | 88 | rdmsrl(hwc->event_base, new_raw_count); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 89 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 90 | if (local64_cmpxchg(&hwc->prev_count, prev_raw_count, |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 91 | new_raw_count) != prev_raw_count) |
| 92 | goto again; |
| 93 | |
| 94 | /* |
| 95 | * Now we have the new raw value and have updated the prev |
| 96 | * timestamp already. We can now calculate the elapsed delta |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 97 | * (event-)time and add that to the generic event. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 98 | * |
| 99 | * Careful, not all hw sign-extends above the physical width |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 100 | * of the count. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 101 | */ |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 102 | delta = (new_raw_count << shift) - (prev_raw_count << shift); |
| 103 | delta >>= shift; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 104 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 105 | local64_add(delta, &event->count); |
| 106 | local64_sub(delta, &hwc->period_left); |
Robert Richter | 4b7bfd0 | 2009-04-29 12:47:22 +0200 | [diff] [blame] | 107 | |
| 108 | return new_raw_count; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Andi Kleen | a7e3ed1 | 2011-03-03 10:34:47 +0800 | [diff] [blame] | 111 | /* |
| 112 | * Find and validate any extra registers to set up. |
| 113 | */ |
| 114 | static int x86_pmu_extra_regs(u64 config, struct perf_event *event) |
| 115 | { |
Stephane Eranian | efc9f05 | 2011-06-06 16:57:03 +0200 | [diff] [blame] | 116 | struct hw_perf_event_extra *reg; |
Andi Kleen | a7e3ed1 | 2011-03-03 10:34:47 +0800 | [diff] [blame] | 117 | struct extra_reg *er; |
| 118 | |
Stephane Eranian | efc9f05 | 2011-06-06 16:57:03 +0200 | [diff] [blame] | 119 | reg = &event->hw.extra_reg; |
Andi Kleen | a7e3ed1 | 2011-03-03 10:34:47 +0800 | [diff] [blame] | 120 | |
| 121 | if (!x86_pmu.extra_regs) |
| 122 | return 0; |
| 123 | |
| 124 | for (er = x86_pmu.extra_regs; er->msr; er++) { |
| 125 | if (er->event != (config & er->config_mask)) |
| 126 | continue; |
| 127 | if (event->attr.config1 & ~er->valid_mask) |
| 128 | return -EINVAL; |
Stephane Eranian | efc9f05 | 2011-06-06 16:57:03 +0200 | [diff] [blame] | 129 | |
| 130 | reg->idx = er->idx; |
| 131 | reg->config = event->attr.config1; |
| 132 | reg->reg = er->msr; |
Andi Kleen | a7e3ed1 | 2011-03-03 10:34:47 +0800 | [diff] [blame] | 133 | break; |
| 134 | } |
| 135 | return 0; |
| 136 | } |
| 137 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 138 | static atomic_t active_events; |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 139 | static DEFINE_MUTEX(pmc_reserve_mutex); |
| 140 | |
Robert Richter | b27ea29 | 2010-03-17 12:49:10 +0100 | [diff] [blame] | 141 | #ifdef CONFIG_X86_LOCAL_APIC |
| 142 | |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 143 | static bool reserve_pmc_hardware(void) |
| 144 | { |
| 145 | int i; |
| 146 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 147 | for (i = 0; i < x86_pmu.num_counters; i++) { |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 148 | if (!reserve_perfctr_nmi(x86_pmu_event_addr(i))) |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 149 | goto perfctr_fail; |
| 150 | } |
| 151 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 152 | for (i = 0; i < x86_pmu.num_counters; i++) { |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 153 | if (!reserve_evntsel_nmi(x86_pmu_config_addr(i))) |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 154 | goto eventsel_fail; |
| 155 | } |
| 156 | |
| 157 | return true; |
| 158 | |
| 159 | eventsel_fail: |
| 160 | for (i--; i >= 0; i--) |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 161 | release_evntsel_nmi(x86_pmu_config_addr(i)); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 162 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 163 | i = x86_pmu.num_counters; |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 164 | |
| 165 | perfctr_fail: |
| 166 | for (i--; i >= 0; i--) |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 167 | release_perfctr_nmi(x86_pmu_event_addr(i)); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 168 | |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 169 | return false; |
| 170 | } |
| 171 | |
| 172 | static void release_pmc_hardware(void) |
| 173 | { |
| 174 | int i; |
| 175 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 176 | for (i = 0; i < x86_pmu.num_counters; i++) { |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 177 | release_perfctr_nmi(x86_pmu_event_addr(i)); |
| 178 | release_evntsel_nmi(x86_pmu_config_addr(i)); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 179 | } |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 180 | } |
| 181 | |
Robert Richter | b27ea29 | 2010-03-17 12:49:10 +0100 | [diff] [blame] | 182 | #else |
| 183 | |
| 184 | static bool reserve_pmc_hardware(void) { return true; } |
| 185 | static void release_pmc_hardware(void) {} |
| 186 | |
| 187 | #endif |
| 188 | |
Don Zickus | 33c6d6a | 2010-11-22 16:55:23 -0500 | [diff] [blame] | 189 | static bool check_hw_exists(void) |
| 190 | { |
| 191 | u64 val, val_new = 0; |
Peter Zijlstra | 4407204 | 2010-12-08 15:56:23 +0100 | [diff] [blame] | 192 | int i, reg, ret = 0; |
Don Zickus | 33c6d6a | 2010-11-22 16:55:23 -0500 | [diff] [blame] | 193 | |
Peter Zijlstra | 4407204 | 2010-12-08 15:56:23 +0100 | [diff] [blame] | 194 | /* |
| 195 | * Check to see if the BIOS enabled any of the counters, if so |
| 196 | * complain and bail. |
| 197 | */ |
| 198 | for (i = 0; i < x86_pmu.num_counters; i++) { |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 199 | reg = x86_pmu_config_addr(i); |
Peter Zijlstra | 4407204 | 2010-12-08 15:56:23 +0100 | [diff] [blame] | 200 | ret = rdmsrl_safe(reg, &val); |
| 201 | if (ret) |
| 202 | goto msr_fail; |
| 203 | if (val & ARCH_PERFMON_EVENTSEL_ENABLE) |
| 204 | goto bios_fail; |
| 205 | } |
| 206 | |
| 207 | if (x86_pmu.num_counters_fixed) { |
| 208 | reg = MSR_ARCH_PERFMON_FIXED_CTR_CTRL; |
| 209 | ret = rdmsrl_safe(reg, &val); |
| 210 | if (ret) |
| 211 | goto msr_fail; |
| 212 | for (i = 0; i < x86_pmu.num_counters_fixed; i++) { |
| 213 | if (val & (0x03 << i*4)) |
| 214 | goto bios_fail; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Now write a value and read it back to see if it matches, |
| 220 | * this is needed to detect certain hardware emulators (qemu/kvm) |
| 221 | * that don't trap on the MSR access and always return 0s. |
| 222 | */ |
Don Zickus | 33c6d6a | 2010-11-22 16:55:23 -0500 | [diff] [blame] | 223 | val = 0xabcdUL; |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 224 | ret = checking_wrmsrl(x86_pmu_event_addr(0), val); |
| 225 | ret |= rdmsrl_safe(x86_pmu_event_addr(0), &val_new); |
Don Zickus | 33c6d6a | 2010-11-22 16:55:23 -0500 | [diff] [blame] | 226 | if (ret || val != val_new) |
Peter Zijlstra | 4407204 | 2010-12-08 15:56:23 +0100 | [diff] [blame] | 227 | goto msr_fail; |
Don Zickus | 33c6d6a | 2010-11-22 16:55:23 -0500 | [diff] [blame] | 228 | |
| 229 | return true; |
Peter Zijlstra | 4407204 | 2010-12-08 15:56:23 +0100 | [diff] [blame] | 230 | |
| 231 | bios_fail: |
Ingo Molnar | 45daae5 | 2011-03-25 10:24:23 +0100 | [diff] [blame] | 232 | /* |
| 233 | * We still allow the PMU driver to operate: |
| 234 | */ |
| 235 | printk(KERN_CONT "Broken BIOS detected, complain to your hardware vendor.\n"); |
Peter Zijlstra | 4407204 | 2010-12-08 15:56:23 +0100 | [diff] [blame] | 236 | printk(KERN_ERR FW_BUG "the BIOS has corrupted hw-PMU resources (MSR %x is %Lx)\n", reg, val); |
Ingo Molnar | 45daae5 | 2011-03-25 10:24:23 +0100 | [diff] [blame] | 237 | |
| 238 | return true; |
Peter Zijlstra | 4407204 | 2010-12-08 15:56:23 +0100 | [diff] [blame] | 239 | |
| 240 | msr_fail: |
| 241 | printk(KERN_CONT "Broken PMU hardware detected, using software events only.\n"); |
Ingo Molnar | 45daae5 | 2011-03-25 10:24:23 +0100 | [diff] [blame] | 242 | |
Peter Zijlstra | 4407204 | 2010-12-08 15:56:23 +0100 | [diff] [blame] | 243 | return false; |
Don Zickus | 33c6d6a | 2010-11-22 16:55:23 -0500 | [diff] [blame] | 244 | } |
| 245 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 246 | static void hw_perf_event_destroy(struct perf_event *event) |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 247 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 248 | if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) { |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 249 | release_pmc_hardware(); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 250 | release_ds_buffers(); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 251 | mutex_unlock(&pmc_reserve_mutex); |
| 252 | } |
| 253 | } |
| 254 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 255 | static inline int x86_pmu_initialized(void) |
| 256 | { |
| 257 | return x86_pmu.handle_irq != NULL; |
| 258 | } |
| 259 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 260 | static inline int |
Andi Kleen | e994d7d | 2011-03-03 10:34:48 +0800 | [diff] [blame] | 261 | set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event *event) |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 262 | { |
Andi Kleen | e994d7d | 2011-03-03 10:34:48 +0800 | [diff] [blame] | 263 | struct perf_event_attr *attr = &event->attr; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 264 | unsigned int cache_type, cache_op, cache_result; |
| 265 | u64 config, val; |
| 266 | |
| 267 | config = attr->config; |
| 268 | |
| 269 | cache_type = (config >> 0) & 0xff; |
| 270 | if (cache_type >= PERF_COUNT_HW_CACHE_MAX) |
| 271 | return -EINVAL; |
| 272 | |
| 273 | cache_op = (config >> 8) & 0xff; |
| 274 | if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX) |
| 275 | return -EINVAL; |
| 276 | |
| 277 | cache_result = (config >> 16) & 0xff; |
| 278 | if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX) |
| 279 | return -EINVAL; |
| 280 | |
| 281 | val = hw_cache_event_ids[cache_type][cache_op][cache_result]; |
| 282 | |
| 283 | if (val == 0) |
| 284 | return -ENOENT; |
| 285 | |
| 286 | if (val == -1) |
| 287 | return -EINVAL; |
| 288 | |
| 289 | hwc->config |= val; |
Andi Kleen | e994d7d | 2011-03-03 10:34:48 +0800 | [diff] [blame] | 290 | attr->config1 = hw_cache_extra_regs[cache_type][cache_op][cache_result]; |
| 291 | return x86_pmu_extra_regs(val, event); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 292 | } |
| 293 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 294 | int x86_setup_perfctr(struct perf_event *event) |
Robert Richter | c1726f3 | 2010-04-13 22:23:11 +0200 | [diff] [blame] | 295 | { |
| 296 | struct perf_event_attr *attr = &event->attr; |
| 297 | struct hw_perf_event *hwc = &event->hw; |
| 298 | u64 config; |
| 299 | |
Franck Bui-Huu | 6c7e550 | 2010-11-23 16:21:43 +0100 | [diff] [blame] | 300 | if (!is_sampling_event(event)) { |
Robert Richter | c1726f3 | 2010-04-13 22:23:11 +0200 | [diff] [blame] | 301 | hwc->sample_period = x86_pmu.max_period; |
| 302 | hwc->last_period = hwc->sample_period; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 303 | local64_set(&hwc->period_left, hwc->sample_period); |
Robert Richter | c1726f3 | 2010-04-13 22:23:11 +0200 | [diff] [blame] | 304 | } else { |
| 305 | /* |
| 306 | * If we have a PMU initialized but no APIC |
| 307 | * interrupts, we cannot sample hardware |
| 308 | * events (user-space has to fall back and |
| 309 | * sample via a hrtimer based software event): |
| 310 | */ |
| 311 | if (!x86_pmu.apic) |
| 312 | return -EOPNOTSUPP; |
| 313 | } |
| 314 | |
| 315 | if (attr->type == PERF_TYPE_RAW) |
Peter Zijlstra | ed13ec5 | 2011-11-14 10:03:25 +0100 | [diff] [blame^] | 316 | return x86_pmu_extra_regs(event->attr.config, event); |
Robert Richter | c1726f3 | 2010-04-13 22:23:11 +0200 | [diff] [blame] | 317 | |
| 318 | if (attr->type == PERF_TYPE_HW_CACHE) |
Andi Kleen | e994d7d | 2011-03-03 10:34:48 +0800 | [diff] [blame] | 319 | return set_ext_hw_attr(hwc, event); |
Robert Richter | c1726f3 | 2010-04-13 22:23:11 +0200 | [diff] [blame] | 320 | |
| 321 | if (attr->config >= x86_pmu.max_events) |
| 322 | return -EINVAL; |
| 323 | |
| 324 | /* |
| 325 | * The generic map: |
| 326 | */ |
| 327 | config = x86_pmu.event_map(attr->config); |
| 328 | |
| 329 | if (config == 0) |
| 330 | return -ENOENT; |
| 331 | |
| 332 | if (config == -1LL) |
| 333 | return -EINVAL; |
| 334 | |
| 335 | /* |
| 336 | * Branch tracing: |
| 337 | */ |
Peter Zijlstra | 18a073a | 2011-04-26 13:24:33 +0200 | [diff] [blame] | 338 | if (attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS && |
| 339 | !attr->freq && hwc->sample_period == 1) { |
Robert Richter | c1726f3 | 2010-04-13 22:23:11 +0200 | [diff] [blame] | 340 | /* BTS is not supported by this architecture. */ |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 341 | if (!x86_pmu.bts_active) |
Robert Richter | c1726f3 | 2010-04-13 22:23:11 +0200 | [diff] [blame] | 342 | return -EOPNOTSUPP; |
| 343 | |
| 344 | /* BTS is currently only allowed for user-mode. */ |
| 345 | if (!attr->exclude_kernel) |
| 346 | return -EOPNOTSUPP; |
| 347 | } |
| 348 | |
| 349 | hwc->config |= config; |
| 350 | |
| 351 | return 0; |
| 352 | } |
Robert Richter | 4261e0e | 2010-04-13 22:23:10 +0200 | [diff] [blame] | 353 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 354 | int x86_pmu_hw_config(struct perf_event *event) |
Cyrill Gorcunov | a072738 | 2010-03-11 19:54:39 +0300 | [diff] [blame] | 355 | { |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 356 | if (event->attr.precise_ip) { |
| 357 | int precise = 0; |
| 358 | |
| 359 | /* Support for constant skid */ |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 360 | if (x86_pmu.pebs_active) { |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 361 | precise++; |
| 362 | |
Peter Zijlstra | 5553be2 | 2010-10-19 14:38:11 +0200 | [diff] [blame] | 363 | /* Support for IP fixup */ |
| 364 | if (x86_pmu.lbr_nr) |
| 365 | precise++; |
| 366 | } |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 367 | |
| 368 | if (event->attr.precise_ip > precise) |
| 369 | return -EOPNOTSUPP; |
| 370 | } |
| 371 | |
Cyrill Gorcunov | a072738 | 2010-03-11 19:54:39 +0300 | [diff] [blame] | 372 | /* |
| 373 | * Generate PMC IRQs: |
| 374 | * (keep 'enabled' bit clear for now) |
| 375 | */ |
Peter Zijlstra | b4cdc5c | 2010-03-30 17:00:06 +0200 | [diff] [blame] | 376 | event->hw.config = ARCH_PERFMON_EVENTSEL_INT; |
Cyrill Gorcunov | a072738 | 2010-03-11 19:54:39 +0300 | [diff] [blame] | 377 | |
| 378 | /* |
| 379 | * Count user and OS events unless requested not to |
| 380 | */ |
Peter Zijlstra | b4cdc5c | 2010-03-30 17:00:06 +0200 | [diff] [blame] | 381 | if (!event->attr.exclude_user) |
| 382 | event->hw.config |= ARCH_PERFMON_EVENTSEL_USR; |
| 383 | if (!event->attr.exclude_kernel) |
| 384 | event->hw.config |= ARCH_PERFMON_EVENTSEL_OS; |
| 385 | |
| 386 | if (event->attr.type == PERF_TYPE_RAW) |
| 387 | event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK; |
Cyrill Gorcunov | a072738 | 2010-03-11 19:54:39 +0300 | [diff] [blame] | 388 | |
Robert Richter | 9d0fcba6 | 2010-04-13 22:23:12 +0200 | [diff] [blame] | 389 | return x86_setup_perfctr(event); |
Cyrill Gorcunov | a072738 | 2010-03-11 19:54:39 +0300 | [diff] [blame] | 390 | } |
| 391 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 392 | /* |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame] | 393 | * Setup the hardware configuration for a given attr_type |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 394 | */ |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 395 | static int __x86_pmu_event_init(struct perf_event *event) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 396 | { |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 397 | int err; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 398 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 399 | if (!x86_pmu_initialized()) |
| 400 | return -ENODEV; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 401 | |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 402 | err = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 403 | if (!atomic_inc_not_zero(&active_events)) { |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 404 | mutex_lock(&pmc_reserve_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 405 | if (atomic_read(&active_events) == 0) { |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 406 | if (!reserve_pmc_hardware()) |
| 407 | err = -EBUSY; |
Peter Zijlstra | f80c9e3 | 2010-10-19 14:50:02 +0200 | [diff] [blame] | 408 | else |
| 409 | reserve_ds_buffers(); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 410 | } |
| 411 | if (!err) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 412 | atomic_inc(&active_events); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 413 | mutex_unlock(&pmc_reserve_mutex); |
| 414 | } |
| 415 | if (err) |
| 416 | return err; |
| 417 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 418 | event->destroy = hw_perf_event_destroy; |
Peter Zijlstra | a1792cdac | 2009-09-09 10:04:47 +0200 | [diff] [blame] | 419 | |
Robert Richter | 4261e0e | 2010-04-13 22:23:10 +0200 | [diff] [blame] | 420 | event->hw.idx = -1; |
| 421 | event->hw.last_cpu = -1; |
| 422 | event->hw.last_tag = ~0ULL; |
Stephane Eranian | b690081 | 2009-10-06 16:42:09 +0200 | [diff] [blame] | 423 | |
Stephane Eranian | efc9f05 | 2011-06-06 16:57:03 +0200 | [diff] [blame] | 424 | /* mark unused */ |
| 425 | event->hw.extra_reg.idx = EXTRA_REG_NONE; |
| 426 | |
Robert Richter | 9d0fcba6 | 2010-04-13 22:23:12 +0200 | [diff] [blame] | 427 | return x86_pmu.hw_config(event); |
Robert Richter | 4261e0e | 2010-04-13 22:23:10 +0200 | [diff] [blame] | 428 | } |
| 429 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 430 | void x86_pmu_disable_all(void) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 431 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 432 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 433 | int idx; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 434 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 435 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 436 | u64 val; |
| 437 | |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 438 | if (!test_bit(idx, cpuc->active_mask)) |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 439 | continue; |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 440 | rdmsrl(x86_pmu_config_addr(idx), val); |
Robert Richter | bb1165d | 2010-03-01 14:21:23 +0100 | [diff] [blame] | 441 | if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE)) |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 442 | continue; |
Robert Richter | bb1165d | 2010-03-01 14:21:23 +0100 | [diff] [blame] | 443 | val &= ~ARCH_PERFMON_EVENTSEL_ENABLE; |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 444 | wrmsrl(x86_pmu_config_addr(idx), val); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 445 | } |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 446 | } |
| 447 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 448 | static void x86_pmu_disable(struct pmu *pmu) |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 449 | { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 450 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 451 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 452 | if (!x86_pmu_initialized()) |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 453 | return; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 454 | |
Peter Zijlstra | 1a6e21f | 2010-01-27 23:07:47 +0100 | [diff] [blame] | 455 | if (!cpuc->enabled) |
| 456 | return; |
| 457 | |
| 458 | cpuc->n_added = 0; |
| 459 | cpuc->enabled = 0; |
| 460 | barrier(); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 461 | |
| 462 | x86_pmu.disable_all(); |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 463 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 464 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 465 | void x86_pmu_enable_all(int added) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 466 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 467 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 468 | int idx; |
| 469 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 470 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
Robert Richter | d45dd92 | 2011-02-02 17:40:56 +0100 | [diff] [blame] | 471 | struct hw_perf_event *hwc = &cpuc->events[idx]->hw; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 472 | |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 473 | if (!test_bit(idx, cpuc->active_mask)) |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 474 | continue; |
Peter Zijlstra | 984b838 | 2009-07-10 09:59:56 +0200 | [diff] [blame] | 475 | |
Robert Richter | d45dd92 | 2011-02-02 17:40:56 +0100 | [diff] [blame] | 476 | __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 480 | static struct pmu pmu; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 481 | |
| 482 | static inline int is_x86_event(struct perf_event *event) |
| 483 | { |
| 484 | return event->pmu == &pmu; |
| 485 | } |
| 486 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 487 | int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 488 | { |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 489 | struct event_constraint *c, *constraints[X86_PMC_IDX_MAX]; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 490 | unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
Peter Zijlstra | c933c1a | 2010-01-22 16:40:12 +0100 | [diff] [blame] | 491 | int i, j, w, wmax, num = 0; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 492 | struct hw_perf_event *hwc; |
| 493 | |
| 494 | bitmap_zero(used_mask, X86_PMC_IDX_MAX); |
| 495 | |
| 496 | for (i = 0; i < n; i++) { |
Peter Zijlstra | b622d64 | 2010-02-01 15:36:30 +0100 | [diff] [blame] | 497 | c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]); |
| 498 | constraints[i] = c; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /* |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 502 | * fastpath, try to reuse previous register |
| 503 | */ |
Peter Zijlstra | c933c1a | 2010-01-22 16:40:12 +0100 | [diff] [blame] | 504 | for (i = 0; i < n; i++) { |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 505 | hwc = &cpuc->event_list[i]->hw; |
Peter Zijlstra | 81269a0 | 2010-01-22 14:55:22 +0100 | [diff] [blame] | 506 | c = constraints[i]; |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 507 | |
| 508 | /* never assigned */ |
| 509 | if (hwc->idx == -1) |
| 510 | break; |
| 511 | |
| 512 | /* constraint still honored */ |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 513 | if (!test_bit(hwc->idx, c->idxmsk)) |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 514 | break; |
| 515 | |
| 516 | /* not already used */ |
| 517 | if (test_bit(hwc->idx, used_mask)) |
| 518 | break; |
| 519 | |
Peter Zijlstra | 34538ee | 2010-03-02 21:16:55 +0100 | [diff] [blame] | 520 | __set_bit(hwc->idx, used_mask); |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 521 | if (assign) |
| 522 | assign[i] = hwc->idx; |
| 523 | } |
Peter Zijlstra | c933c1a | 2010-01-22 16:40:12 +0100 | [diff] [blame] | 524 | if (i == n) |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 525 | goto done; |
| 526 | |
| 527 | /* |
| 528 | * begin slow path |
| 529 | */ |
| 530 | |
| 531 | bitmap_zero(used_mask, X86_PMC_IDX_MAX); |
| 532 | |
| 533 | /* |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 534 | * weight = number of possible counters |
| 535 | * |
| 536 | * 1 = most constrained, only works on one counter |
| 537 | * wmax = least constrained, works on any counter |
| 538 | * |
| 539 | * assign events to counters starting with most |
| 540 | * constrained events. |
| 541 | */ |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 542 | wmax = x86_pmu.num_counters; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 543 | |
| 544 | /* |
| 545 | * when fixed event counters are present, |
| 546 | * wmax is incremented by 1 to account |
| 547 | * for one more choice |
| 548 | */ |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 549 | if (x86_pmu.num_counters_fixed) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 550 | wmax++; |
| 551 | |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 552 | for (w = 1, num = n; num && w <= wmax; w++) { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 553 | /* for each event */ |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 554 | for (i = 0; num && i < n; i++) { |
Peter Zijlstra | 81269a0 | 2010-01-22 14:55:22 +0100 | [diff] [blame] | 555 | c = constraints[i]; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 556 | hwc = &cpuc->event_list[i]->hw; |
| 557 | |
Peter Zijlstra | 272d30b | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 558 | if (c->weight != w) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 559 | continue; |
| 560 | |
Akinobu Mita | 984b3f5 | 2010-03-05 13:41:37 -0800 | [diff] [blame] | 561 | for_each_set_bit(j, c->idxmsk, X86_PMC_IDX_MAX) { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 562 | if (!test_bit(j, used_mask)) |
| 563 | break; |
| 564 | } |
| 565 | |
| 566 | if (j == X86_PMC_IDX_MAX) |
| 567 | break; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 568 | |
Peter Zijlstra | 34538ee | 2010-03-02 21:16:55 +0100 | [diff] [blame] | 569 | __set_bit(j, used_mask); |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 570 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 571 | if (assign) |
| 572 | assign[i] = j; |
| 573 | num--; |
| 574 | } |
| 575 | } |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 576 | done: |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 577 | /* |
| 578 | * scheduling failed or is just a simulation, |
| 579 | * free resources if necessary |
| 580 | */ |
| 581 | if (!assign || num) { |
| 582 | for (i = 0; i < n; i++) { |
| 583 | if (x86_pmu.put_event_constraints) |
| 584 | x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]); |
| 585 | } |
| 586 | } |
Peter Zijlstra | aa2bc1a | 2011-11-09 17:56:37 +0100 | [diff] [blame] | 587 | return num ? -EINVAL : 0; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | /* |
| 591 | * dogrp: true if must collect siblings events (group) |
| 592 | * returns total number of events and error code |
| 593 | */ |
| 594 | static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp) |
| 595 | { |
| 596 | struct perf_event *event; |
| 597 | int n, max_count; |
| 598 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 599 | max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 600 | |
| 601 | /* current number of events already accepted */ |
| 602 | n = cpuc->n_events; |
| 603 | |
| 604 | if (is_x86_event(leader)) { |
| 605 | if (n >= max_count) |
Peter Zijlstra | aa2bc1a | 2011-11-09 17:56:37 +0100 | [diff] [blame] | 606 | return -EINVAL; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 607 | cpuc->event_list[n] = leader; |
| 608 | n++; |
| 609 | } |
| 610 | if (!dogrp) |
| 611 | return n; |
| 612 | |
| 613 | list_for_each_entry(event, &leader->sibling_list, group_entry) { |
| 614 | if (!is_x86_event(event) || |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 615 | event->state <= PERF_EVENT_STATE_OFF) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 616 | continue; |
| 617 | |
| 618 | if (n >= max_count) |
Peter Zijlstra | aa2bc1a | 2011-11-09 17:56:37 +0100 | [diff] [blame] | 619 | return -EINVAL; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 620 | |
| 621 | cpuc->event_list[n] = event; |
| 622 | n++; |
| 623 | } |
| 624 | return n; |
| 625 | } |
| 626 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 627 | static inline void x86_assign_hw_event(struct perf_event *event, |
Stephane Eranian | 447a194 | 2010-02-01 14:50:01 +0200 | [diff] [blame] | 628 | struct cpu_hw_events *cpuc, int i) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 629 | { |
Stephane Eranian | 447a194 | 2010-02-01 14:50:01 +0200 | [diff] [blame] | 630 | struct hw_perf_event *hwc = &event->hw; |
| 631 | |
| 632 | hwc->idx = cpuc->assign[i]; |
| 633 | hwc->last_cpu = smp_processor_id(); |
| 634 | hwc->last_tag = ++cpuc->tags[i]; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 635 | |
| 636 | if (hwc->idx == X86_PMC_IDX_FIXED_BTS) { |
| 637 | hwc->config_base = 0; |
| 638 | hwc->event_base = 0; |
| 639 | } else if (hwc->idx >= X86_PMC_IDX_FIXED) { |
| 640 | hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL; |
Stephane Eranian | fc66c52 | 2011-03-19 18:20:05 +0100 | [diff] [blame] | 641 | hwc->event_base = MSR_ARCH_PERFMON_FIXED_CTR0 + (hwc->idx - X86_PMC_IDX_FIXED); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 642 | } else { |
Robert Richter | 73d6e52 | 2011-02-02 17:40:59 +0100 | [diff] [blame] | 643 | hwc->config_base = x86_pmu_config_addr(hwc->idx); |
| 644 | hwc->event_base = x86_pmu_event_addr(hwc->idx); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
Stephane Eranian | 447a194 | 2010-02-01 14:50:01 +0200 | [diff] [blame] | 648 | static inline int match_prev_assignment(struct hw_perf_event *hwc, |
| 649 | struct cpu_hw_events *cpuc, |
| 650 | int i) |
| 651 | { |
| 652 | return hwc->idx == cpuc->assign[i] && |
| 653 | hwc->last_cpu == smp_processor_id() && |
| 654 | hwc->last_tag == cpuc->tags[i]; |
| 655 | } |
| 656 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 657 | static void x86_pmu_start(struct perf_event *event, int flags); |
Peter Zijlstra | 2e84187 | 2010-01-25 15:58:43 +0100 | [diff] [blame] | 658 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 659 | static void x86_pmu_enable(struct pmu *pmu) |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 660 | { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 661 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 662 | struct perf_event *event; |
| 663 | struct hw_perf_event *hwc; |
Peter Zijlstra | 11164cd | 2010-03-26 14:08:44 +0100 | [diff] [blame] | 664 | int i, added = cpuc->n_added; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 665 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 666 | if (!x86_pmu_initialized()) |
Ingo Molnar | 2b9ff0d | 2008-12-14 18:36:30 +0100 | [diff] [blame] | 667 | return; |
Peter Zijlstra | 1a6e21f | 2010-01-27 23:07:47 +0100 | [diff] [blame] | 668 | |
| 669 | if (cpuc->enabled) |
| 670 | return; |
| 671 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 672 | if (cpuc->n_added) { |
Peter Zijlstra | 19925ce | 2010-03-06 13:20:40 +0100 | [diff] [blame] | 673 | int n_running = cpuc->n_events - cpuc->n_added; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 674 | /* |
| 675 | * apply assignment obtained either from |
| 676 | * hw_perf_group_sched_in() or x86_pmu_enable() |
| 677 | * |
| 678 | * step1: save events moving to new counters |
| 679 | * step2: reprogram moved events into new counters |
| 680 | */ |
Peter Zijlstra | 19925ce | 2010-03-06 13:20:40 +0100 | [diff] [blame] | 681 | for (i = 0; i < n_running; i++) { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 682 | event = cpuc->event_list[i]; |
| 683 | hwc = &event->hw; |
| 684 | |
Stephane Eranian | 447a194 | 2010-02-01 14:50:01 +0200 | [diff] [blame] | 685 | /* |
| 686 | * we can avoid reprogramming counter if: |
| 687 | * - assigned same counter as last time |
| 688 | * - running on same CPU as last time |
| 689 | * - no other event has used the counter since |
| 690 | */ |
| 691 | if (hwc->idx == -1 || |
| 692 | match_prev_assignment(hwc, cpuc, i)) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 693 | continue; |
| 694 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 695 | /* |
| 696 | * Ensure we don't accidentally enable a stopped |
| 697 | * counter simply because we rescheduled. |
| 698 | */ |
| 699 | if (hwc->state & PERF_HES_STOPPED) |
| 700 | hwc->state |= PERF_HES_ARCH; |
| 701 | |
| 702 | x86_pmu_stop(event, PERF_EF_UPDATE); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | for (i = 0; i < cpuc->n_events; i++) { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 706 | event = cpuc->event_list[i]; |
| 707 | hwc = &event->hw; |
| 708 | |
Peter Zijlstra | 45e16a6 | 2010-03-11 13:40:30 +0100 | [diff] [blame] | 709 | if (!match_prev_assignment(hwc, cpuc, i)) |
Stephane Eranian | 447a194 | 2010-02-01 14:50:01 +0200 | [diff] [blame] | 710 | x86_assign_hw_event(event, cpuc, i); |
Peter Zijlstra | 45e16a6 | 2010-03-11 13:40:30 +0100 | [diff] [blame] | 711 | else if (i < n_running) |
| 712 | continue; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 713 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 714 | if (hwc->state & PERF_HES_ARCH) |
| 715 | continue; |
| 716 | |
| 717 | x86_pmu_start(event, PERF_EF_RELOAD); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 718 | } |
| 719 | cpuc->n_added = 0; |
| 720 | perf_events_lapic_init(); |
| 721 | } |
Peter Zijlstra | 1a6e21f | 2010-01-27 23:07:47 +0100 | [diff] [blame] | 722 | |
| 723 | cpuc->enabled = 1; |
| 724 | barrier(); |
| 725 | |
Peter Zijlstra | 11164cd | 2010-03-26 14:08:44 +0100 | [diff] [blame] | 726 | x86_pmu.enable_all(added); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 727 | } |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 728 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 729 | static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 730 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 731 | /* |
| 732 | * Set the next IRQ period, based on the hwc->period_left value. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 733 | * To be called with the event disabled in hw: |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 734 | */ |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 735 | int x86_perf_event_set_period(struct perf_event *event) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 736 | { |
Peter Zijlstra | 07088ed | 2010-03-02 20:16:01 +0100 | [diff] [blame] | 737 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 738 | s64 left = local64_read(&hwc->period_left); |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 739 | s64 period = hwc->sample_period; |
Peter Zijlstra | 7645a24 | 2010-03-08 13:51:31 +0100 | [diff] [blame] | 740 | int ret = 0, idx = hwc->idx; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 741 | |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 742 | if (idx == X86_PMC_IDX_FIXED_BTS) |
| 743 | return 0; |
| 744 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 745 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 746 | * If we are way outside a reasonable range then just skip forward: |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 747 | */ |
| 748 | if (unlikely(left <= -period)) { |
| 749 | left = period; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 750 | local64_set(&hwc->period_left, left); |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 751 | hwc->last_period = period; |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 752 | ret = 1; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | if (unlikely(left <= 0)) { |
| 756 | left += period; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 757 | local64_set(&hwc->period_left, left); |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 758 | hwc->last_period = period; |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 759 | ret = 1; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 760 | } |
Ingo Molnar | 1c80f4b | 2009-05-15 08:25:22 +0200 | [diff] [blame] | 761 | /* |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 762 | * Quirk: certain CPUs dont like it if just 1 hw_event is left: |
Ingo Molnar | 1c80f4b | 2009-05-15 08:25:22 +0200 | [diff] [blame] | 763 | */ |
| 764 | if (unlikely(left < 2)) |
| 765 | left = 2; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 766 | |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 767 | if (left > x86_pmu.max_period) |
| 768 | left = x86_pmu.max_period; |
| 769 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 770 | per_cpu(pmc_prev_left[idx], smp_processor_id()) = left; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 771 | |
| 772 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 773 | * The hw event starts counting from this event offset, |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 774 | * mark it to be able to extra future deltas: |
| 775 | */ |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 776 | local64_set(&hwc->prev_count, (u64)-left); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 777 | |
Robert Richter | 73d6e52 | 2011-02-02 17:40:59 +0100 | [diff] [blame] | 778 | wrmsrl(hwc->event_base, (u64)(-left) & x86_pmu.cntval_mask); |
Cyrill Gorcunov | 68aa00a | 2010-06-03 01:23:04 +0400 | [diff] [blame] | 779 | |
| 780 | /* |
| 781 | * Due to erratum on certan cpu we need |
| 782 | * a second write to be sure the register |
| 783 | * is updated properly |
| 784 | */ |
| 785 | if (x86_pmu.perfctr_second_write) { |
Robert Richter | 73d6e52 | 2011-02-02 17:40:59 +0100 | [diff] [blame] | 786 | wrmsrl(hwc->event_base, |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 787 | (u64)(-left) & x86_pmu.cntval_mask); |
Cyrill Gorcunov | 68aa00a | 2010-06-03 01:23:04 +0400 | [diff] [blame] | 788 | } |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 789 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 790 | perf_event_update_userpage(event); |
Peter Zijlstra | 194002b | 2009-06-22 16:35:24 +0200 | [diff] [blame] | 791 | |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 792 | return ret; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 793 | } |
| 794 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 795 | void x86_pmu_enable_event(struct perf_event *event) |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 796 | { |
Tejun Heo | 0a3aee0 | 2010-12-18 16:28:55 +0100 | [diff] [blame] | 797 | if (__this_cpu_read(cpu_hw_events.enabled)) |
Robert Richter | 31fa58a | 2010-04-13 22:23:14 +0200 | [diff] [blame] | 798 | __x86_pmu_enable_event(&event->hw, |
| 799 | ARCH_PERFMON_EVENTSEL_ENABLE); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 800 | } |
| 801 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 802 | /* |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 803 | * Add a single event to the PMU. |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 804 | * |
| 805 | * The event is added to the group of enabled events |
| 806 | * but only if it can be scehduled with existing events. |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 807 | */ |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 808 | static int x86_pmu_add(struct perf_event *event, int flags) |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 809 | { |
| 810 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 811 | struct hw_perf_event *hwc; |
| 812 | int assign[X86_PMC_IDX_MAX]; |
| 813 | int n, n0, ret; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 814 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 815 | hwc = &event->hw; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 816 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 817 | perf_pmu_disable(event->pmu); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 818 | n0 = cpuc->n_events; |
Peter Zijlstra | 24cd7f5 | 2010-06-11 17:32:03 +0200 | [diff] [blame] | 819 | ret = n = collect_events(cpuc, event, false); |
| 820 | if (ret < 0) |
| 821 | goto out; |
Ingo Molnar | 53b441a | 2009-05-25 21:41:28 +0200 | [diff] [blame] | 822 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 823 | hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED; |
| 824 | if (!(flags & PERF_EF_START)) |
| 825 | hwc->state |= PERF_HES_ARCH; |
| 826 | |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 827 | /* |
| 828 | * If group events scheduling transaction was started, |
Lucas De Marchi | 0d2eb44 | 2011-03-17 16:24:16 -0300 | [diff] [blame] | 829 | * skip the schedulability test here, it will be performed |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 830 | * at commit time (->commit_txn) as a whole |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 831 | */ |
Peter Zijlstra | 8d2cacb | 2010-05-25 17:49:05 +0200 | [diff] [blame] | 832 | if (cpuc->group_flag & PERF_EVENT_TXN) |
Peter Zijlstra | 24cd7f5 | 2010-06-11 17:32:03 +0200 | [diff] [blame] | 833 | goto done_collect; |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 834 | |
Cyrill Gorcunov | a072738 | 2010-03-11 19:54:39 +0300 | [diff] [blame] | 835 | ret = x86_pmu.schedule_events(cpuc, n, assign); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 836 | if (ret) |
Peter Zijlstra | 24cd7f5 | 2010-06-11 17:32:03 +0200 | [diff] [blame] | 837 | goto out; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 838 | /* |
| 839 | * copy new assignment, now we know it is possible |
| 840 | * will be used by hw_perf_enable() |
| 841 | */ |
| 842 | memcpy(cpuc->assign, assign, n*sizeof(int)); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 843 | |
Peter Zijlstra | 24cd7f5 | 2010-06-11 17:32:03 +0200 | [diff] [blame] | 844 | done_collect: |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 845 | cpuc->n_events = n; |
Peter Zijlstra | 356e1f2 | 2010-03-06 13:49:56 +0100 | [diff] [blame] | 846 | cpuc->n_added += n - n0; |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 847 | cpuc->n_txn += n - n0; |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 848 | |
Peter Zijlstra | 24cd7f5 | 2010-06-11 17:32:03 +0200 | [diff] [blame] | 849 | ret = 0; |
| 850 | out: |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 851 | perf_pmu_enable(event->pmu); |
Peter Zijlstra | 24cd7f5 | 2010-06-11 17:32:03 +0200 | [diff] [blame] | 852 | return ret; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 853 | } |
| 854 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 855 | static void x86_pmu_start(struct perf_event *event, int flags) |
Stephane Eranian | d76a081 | 2010-02-08 17:06:01 +0200 | [diff] [blame] | 856 | { |
Peter Zijlstra | c08053e | 2010-03-06 13:19:24 +0100 | [diff] [blame] | 857 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 858 | int idx = event->hw.idx; |
| 859 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 860 | if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED))) |
| 861 | return; |
Stephane Eranian | d76a081 | 2010-02-08 17:06:01 +0200 | [diff] [blame] | 862 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 863 | if (WARN_ON_ONCE(idx == -1)) |
| 864 | return; |
| 865 | |
| 866 | if (flags & PERF_EF_RELOAD) { |
| 867 | WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE)); |
| 868 | x86_perf_event_set_period(event); |
| 869 | } |
| 870 | |
| 871 | event->hw.state = 0; |
| 872 | |
Peter Zijlstra | c08053e | 2010-03-06 13:19:24 +0100 | [diff] [blame] | 873 | cpuc->events[idx] = event; |
| 874 | __set_bit(idx, cpuc->active_mask); |
Robert Richter | 63e6be6 | 2010-09-15 18:20:34 +0200 | [diff] [blame] | 875 | __set_bit(idx, cpuc->running); |
Peter Zijlstra | aff3d91 | 2010-03-02 20:32:08 +0100 | [diff] [blame] | 876 | x86_pmu.enable(event); |
Peter Zijlstra | c08053e | 2010-03-06 13:19:24 +0100 | [diff] [blame] | 877 | perf_event_update_userpage(event); |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 878 | } |
| 879 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 880 | void perf_event_print_debug(void) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 881 | { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 882 | u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 883 | u64 pebs; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 884 | struct cpu_hw_events *cpuc; |
Peter Zijlstra | 5bb9efe | 2009-05-13 08:12:51 +0200 | [diff] [blame] | 885 | unsigned long flags; |
Ingo Molnar | 1e12567 | 2008-12-09 12:18:18 +0100 | [diff] [blame] | 886 | int cpu, idx; |
| 887 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 888 | if (!x86_pmu.num_counters) |
Ingo Molnar | 1e12567 | 2008-12-09 12:18:18 +0100 | [diff] [blame] | 889 | return; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 890 | |
Peter Zijlstra | 5bb9efe | 2009-05-13 08:12:51 +0200 | [diff] [blame] | 891 | local_irq_save(flags); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 892 | |
| 893 | cpu = smp_processor_id(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 894 | cpuc = &per_cpu(cpu_hw_events, cpu); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 895 | |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 896 | if (x86_pmu.version >= 2) { |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 897 | rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl); |
| 898 | rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status); |
| 899 | rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow); |
| 900 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 901 | rdmsrl(MSR_IA32_PEBS_ENABLE, pebs); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 902 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 903 | pr_info("\n"); |
| 904 | pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl); |
| 905 | pr_info("CPU#%d: status: %016llx\n", cpu, status); |
| 906 | pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow); |
| 907 | pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 908 | pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 909 | } |
Peter Zijlstra | 7645a24 | 2010-03-08 13:51:31 +0100 | [diff] [blame] | 910 | pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 911 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 912 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
Robert Richter | 41bf498 | 2011-02-02 17:40:57 +0100 | [diff] [blame] | 913 | rdmsrl(x86_pmu_config_addr(idx), pmc_ctrl); |
| 914 | rdmsrl(x86_pmu_event_addr(idx), pmc_count); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 915 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 916 | prev_left = per_cpu(pmc_prev_left[idx], cpu); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 917 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 918 | pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n", |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 919 | cpu, idx, pmc_ctrl); |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 920 | pr_info("CPU#%d: gen-PMC%d count: %016llx\n", |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 921 | cpu, idx, pmc_count); |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 922 | pr_info("CPU#%d: gen-PMC%d left: %016llx\n", |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 923 | cpu, idx, prev_left); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 924 | } |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 925 | for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 926 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count); |
| 927 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 928 | pr_info("CPU#%d: fixed-PMC%d count: %016llx\n", |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 929 | cpu, idx, pmc_count); |
| 930 | } |
Peter Zijlstra | 5bb9efe | 2009-05-13 08:12:51 +0200 | [diff] [blame] | 931 | local_irq_restore(flags); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 932 | } |
| 933 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 934 | void x86_pmu_stop(struct perf_event *event, int flags) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 935 | { |
Stephane Eranian | d76a081 | 2010-02-08 17:06:01 +0200 | [diff] [blame] | 936 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 937 | struct hw_perf_event *hwc = &event->hw; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 938 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 939 | if (__test_and_clear_bit(hwc->idx, cpuc->active_mask)) { |
| 940 | x86_pmu.disable(event); |
| 941 | cpuc->events[hwc->idx] = NULL; |
| 942 | WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED); |
| 943 | hwc->state |= PERF_HES_STOPPED; |
| 944 | } |
Peter Zijlstra | 71e2d28 | 2010-03-08 17:51:33 +0100 | [diff] [blame] | 945 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 946 | if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) { |
| 947 | /* |
| 948 | * Drain the remaining delta count out of a event |
| 949 | * that we are disabling: |
| 950 | */ |
| 951 | x86_perf_event_update(event); |
| 952 | hwc->state |= PERF_HES_UPTODATE; |
| 953 | } |
Peter Zijlstra | 2e84187 | 2010-01-25 15:58:43 +0100 | [diff] [blame] | 954 | } |
| 955 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 956 | static void x86_pmu_del(struct perf_event *event, int flags) |
Peter Zijlstra | 2e84187 | 2010-01-25 15:58:43 +0100 | [diff] [blame] | 957 | { |
| 958 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 959 | int i; |
| 960 | |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 961 | /* |
| 962 | * If we're called during a txn, we don't need to do anything. |
| 963 | * The events never got scheduled and ->cancel_txn will truncate |
| 964 | * the event_list. |
| 965 | */ |
Peter Zijlstra | 8d2cacb | 2010-05-25 17:49:05 +0200 | [diff] [blame] | 966 | if (cpuc->group_flag & PERF_EVENT_TXN) |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 967 | return; |
| 968 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 969 | x86_pmu_stop(event, PERF_EF_UPDATE); |
Peter Zijlstra | 194002b | 2009-06-22 16:35:24 +0200 | [diff] [blame] | 970 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 971 | for (i = 0; i < cpuc->n_events; i++) { |
| 972 | if (event == cpuc->event_list[i]) { |
| 973 | |
| 974 | if (x86_pmu.put_event_constraints) |
| 975 | x86_pmu.put_event_constraints(cpuc, event); |
| 976 | |
| 977 | while (++i < cpuc->n_events) |
| 978 | cpuc->event_list[i-1] = cpuc->event_list[i]; |
| 979 | |
| 980 | --cpuc->n_events; |
Peter Zijlstra | 6c9687a | 2010-01-25 11:57:25 +0100 | [diff] [blame] | 981 | break; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 982 | } |
| 983 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 984 | perf_event_update_userpage(event); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 985 | } |
| 986 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 987 | int x86_pmu_handle_irq(struct pt_regs *regs) |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 988 | { |
Peter Zijlstra | df1a132 | 2009-06-10 21:02:22 +0200 | [diff] [blame] | 989 | struct perf_sample_data data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 990 | struct cpu_hw_events *cpuc; |
| 991 | struct perf_event *event; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 992 | int idx, handled = 0; |
Ingo Molnar | 9029a5e | 2009-05-15 08:26:20 +0200 | [diff] [blame] | 993 | u64 val; |
| 994 | |
Peter Zijlstra | dc1d628 | 2010-03-03 15:55:04 +0100 | [diff] [blame] | 995 | perf_sample_data_init(&data, 0); |
Peter Zijlstra | df1a132 | 2009-06-10 21:02:22 +0200 | [diff] [blame] | 996 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 997 | cpuc = &__get_cpu_var(cpu_hw_events); |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 998 | |
Don Zickus | 2bce5da | 2011-04-27 06:32:33 -0400 | [diff] [blame] | 999 | /* |
| 1000 | * Some chipsets need to unmask the LVTPC in a particular spot |
| 1001 | * inside the nmi handler. As a result, the unmasking was pushed |
| 1002 | * into all the nmi handlers. |
| 1003 | * |
| 1004 | * This generic handler doesn't seem to have any issues where the |
| 1005 | * unmasking occurs so it was left at the top. |
| 1006 | */ |
| 1007 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
| 1008 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1009 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
Robert Richter | 63e6be6 | 2010-09-15 18:20:34 +0200 | [diff] [blame] | 1010 | if (!test_bit(idx, cpuc->active_mask)) { |
| 1011 | /* |
| 1012 | * Though we deactivated the counter some cpus |
| 1013 | * might still deliver spurious interrupts still |
| 1014 | * in flight. Catch them: |
| 1015 | */ |
| 1016 | if (__test_and_clear_bit(idx, cpuc->running)) |
| 1017 | handled++; |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 1018 | continue; |
Robert Richter | 63e6be6 | 2010-09-15 18:20:34 +0200 | [diff] [blame] | 1019 | } |
Peter Zijlstra | 962bf7a | 2009-05-13 13:21:36 +0200 | [diff] [blame] | 1020 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1021 | event = cpuc->events[idx]; |
Peter Zijlstra | a4016a7 | 2009-05-14 14:52:17 +0200 | [diff] [blame] | 1022 | |
Peter Zijlstra | cc2ad4b | 2010-03-02 20:18:39 +0100 | [diff] [blame] | 1023 | val = x86_perf_event_update(event); |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1024 | if (val & (1ULL << (x86_pmu.cntval_bits - 1))) |
Peter Zijlstra | 48e22d5 | 2009-05-25 17:39:04 +0200 | [diff] [blame] | 1025 | continue; |
Peter Zijlstra | 962bf7a | 2009-05-13 13:21:36 +0200 | [diff] [blame] | 1026 | |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1027 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1028 | * event overflow |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1029 | */ |
Robert Richter | 4177c42 | 2010-09-02 15:07:48 -0400 | [diff] [blame] | 1030 | handled++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1031 | data.period = event->hw.last_period; |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1032 | |
Peter Zijlstra | 07088ed | 2010-03-02 20:16:01 +0100 | [diff] [blame] | 1033 | if (!x86_perf_event_set_period(event)) |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1034 | continue; |
| 1035 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 1036 | if (perf_event_overflow(event, &data, regs)) |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1037 | x86_pmu_stop(event, 0); |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 1038 | } |
Peter Zijlstra | 962bf7a | 2009-05-13 13:21:36 +0200 | [diff] [blame] | 1039 | |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1040 | if (handled) |
| 1041 | inc_irq_stat(apic_perf_irqs); |
| 1042 | |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 1043 | return handled; |
| 1044 | } |
Robert Richter | 39d81ea | 2009-04-29 12:47:05 +0200 | [diff] [blame] | 1045 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1046 | void perf_events_lapic_init(void) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1047 | { |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1048 | if (!x86_pmu.apic || !x86_pmu_initialized()) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1049 | return; |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 1050 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1051 | /* |
Yong Wang | c323d95 | 2009-05-29 13:28:35 +0800 | [diff] [blame] | 1052 | * Always use NMI for PMU |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1053 | */ |
Yong Wang | c323d95 | 2009-05-29 13:28:35 +0800 | [diff] [blame] | 1054 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | static int __kprobes |
Don Zickus | 9c48f1c | 2011-09-30 15:06:21 -0400 | [diff] [blame] | 1058 | perf_event_nmi_handler(unsigned int cmd, struct pt_regs *regs) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1059 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1060 | if (!atomic_read(&active_events)) |
Don Zickus | 9c48f1c | 2011-09-30 15:06:21 -0400 | [diff] [blame] | 1061 | return NMI_DONE; |
Peter Zijlstra | 63a809a | 2009-05-01 12:23:17 +0200 | [diff] [blame] | 1062 | |
Don Zickus | 9c48f1c | 2011-09-30 15:06:21 -0400 | [diff] [blame] | 1063 | return x86_pmu.handle_irq(regs); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1064 | } |
| 1065 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 1066 | struct event_constraint emptyconstraint; |
| 1067 | struct event_constraint unconstrained; |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1068 | |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1069 | static int __cpuinit |
| 1070 | x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu) |
| 1071 | { |
| 1072 | unsigned int cpu = (long)hcpu; |
Peter Zijlstra | 7fdba1c | 2011-07-22 13:41:54 +0200 | [diff] [blame] | 1073 | struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu); |
Peter Zijlstra | b38b24e | 2010-03-23 19:31:15 +0100 | [diff] [blame] | 1074 | int ret = NOTIFY_OK; |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1075 | |
| 1076 | switch (action & ~CPU_TASKS_FROZEN) { |
| 1077 | case CPU_UP_PREPARE: |
Peter Zijlstra | 7fdba1c | 2011-07-22 13:41:54 +0200 | [diff] [blame] | 1078 | cpuc->kfree_on_online = NULL; |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1079 | if (x86_pmu.cpu_prepare) |
Peter Zijlstra | b38b24e | 2010-03-23 19:31:15 +0100 | [diff] [blame] | 1080 | ret = x86_pmu.cpu_prepare(cpu); |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1081 | break; |
| 1082 | |
| 1083 | case CPU_STARTING: |
| 1084 | if (x86_pmu.cpu_starting) |
| 1085 | x86_pmu.cpu_starting(cpu); |
| 1086 | break; |
| 1087 | |
Peter Zijlstra | 7fdba1c | 2011-07-22 13:41:54 +0200 | [diff] [blame] | 1088 | case CPU_ONLINE: |
| 1089 | kfree(cpuc->kfree_on_online); |
| 1090 | break; |
| 1091 | |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1092 | case CPU_DYING: |
| 1093 | if (x86_pmu.cpu_dying) |
| 1094 | x86_pmu.cpu_dying(cpu); |
| 1095 | break; |
| 1096 | |
Peter Zijlstra | b38b24e | 2010-03-23 19:31:15 +0100 | [diff] [blame] | 1097 | case CPU_UP_CANCELED: |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1098 | case CPU_DEAD: |
| 1099 | if (x86_pmu.cpu_dead) |
| 1100 | x86_pmu.cpu_dead(cpu); |
| 1101 | break; |
| 1102 | |
| 1103 | default: |
| 1104 | break; |
| 1105 | } |
| 1106 | |
Peter Zijlstra | b38b24e | 2010-03-23 19:31:15 +0100 | [diff] [blame] | 1107 | return ret; |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1108 | } |
| 1109 | |
Cyrill Gorcunov | 1255803 | 2009-12-10 19:56:34 +0300 | [diff] [blame] | 1110 | static void __init pmu_check_apic(void) |
| 1111 | { |
| 1112 | if (cpu_has_apic) |
| 1113 | return; |
| 1114 | |
| 1115 | x86_pmu.apic = 0; |
| 1116 | pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n"); |
| 1117 | pr_info("no hardware sampling interrupt available.\n"); |
| 1118 | } |
| 1119 | |
Yinghai Lu | dda9911 | 2011-01-21 15:30:01 -0800 | [diff] [blame] | 1120 | static int __init init_hw_perf_events(void) |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1121 | { |
Peter Zijlstra | b622d64 | 2010-02-01 15:36:30 +0100 | [diff] [blame] | 1122 | struct event_constraint *c; |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 1123 | int err; |
| 1124 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1125 | pr_info("Performance Events: "); |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1126 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1127 | switch (boot_cpu_data.x86_vendor) { |
| 1128 | case X86_VENDOR_INTEL: |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 1129 | err = intel_pmu_init(); |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1130 | break; |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1131 | case X86_VENDOR_AMD: |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 1132 | err = amd_pmu_init(); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1133 | break; |
Robert Richter | 4138960 | 2009-04-29 12:47:00 +0200 | [diff] [blame] | 1134 | default: |
Peter Zijlstra | 004417a | 2010-11-25 18:38:29 +0100 | [diff] [blame] | 1135 | return 0; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1136 | } |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1137 | if (err != 0) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1138 | pr_cont("no PMU driver, software events only.\n"); |
Peter Zijlstra | 004417a | 2010-11-25 18:38:29 +0100 | [diff] [blame] | 1139 | return 0; |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1140 | } |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1141 | |
Cyrill Gorcunov | 1255803 | 2009-12-10 19:56:34 +0300 | [diff] [blame] | 1142 | pmu_check_apic(); |
| 1143 | |
Don Zickus | 33c6d6a | 2010-11-22 16:55:23 -0500 | [diff] [blame] | 1144 | /* sanity check that the hardware exists or is emulated */ |
Peter Zijlstra | 4407204 | 2010-12-08 15:56:23 +0100 | [diff] [blame] | 1145 | if (!check_hw_exists()) |
Peter Zijlstra | 004417a | 2010-11-25 18:38:29 +0100 | [diff] [blame] | 1146 | return 0; |
Don Zickus | 33c6d6a | 2010-11-22 16:55:23 -0500 | [diff] [blame] | 1147 | |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1148 | pr_cont("%s PMU driver.\n", x86_pmu.name); |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 1149 | |
Peter Zijlstra | 3c44780 | 2010-03-04 21:49:01 +0100 | [diff] [blame] | 1150 | if (x86_pmu.quirks) |
| 1151 | x86_pmu.quirks(); |
| 1152 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1153 | if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1154 | WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!", |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1155 | x86_pmu.num_counters, X86_PMC_MAX_GENERIC); |
| 1156 | x86_pmu.num_counters = X86_PMC_MAX_GENERIC; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1157 | } |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1158 | x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1159 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1160 | if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1161 | WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!", |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1162 | x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED); |
| 1163 | x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED; |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 1164 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1165 | |
Robert Richter | d6dc0b4 | 2010-03-17 12:49:13 +0100 | [diff] [blame] | 1166 | x86_pmu.intel_ctrl |= |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1167 | ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED; |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 1168 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1169 | perf_events_lapic_init(); |
Don Zickus | 9c48f1c | 2011-09-30 15:06:21 -0400 | [diff] [blame] | 1170 | register_nmi_handler(NMI_LOCAL, perf_event_nmi_handler, 0, "PMI"); |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1171 | |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1172 | unconstrained = (struct event_constraint) |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1173 | __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1, |
| 1174 | 0, x86_pmu.num_counters); |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1175 | |
Peter Zijlstra | b622d64 | 2010-02-01 15:36:30 +0100 | [diff] [blame] | 1176 | if (x86_pmu.event_constraints) { |
| 1177 | for_each_event_constraint(c, x86_pmu.event_constraints) { |
Robert Richter | a098f44 | 2010-03-30 11:28:21 +0200 | [diff] [blame] | 1178 | if (c->cmask != X86_RAW_EVENT_MASK) |
Peter Zijlstra | b622d64 | 2010-02-01 15:36:30 +0100 | [diff] [blame] | 1179 | continue; |
| 1180 | |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1181 | c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1; |
| 1182 | c->weight += x86_pmu.num_counters; |
Peter Zijlstra | b622d64 | 2010-02-01 15:36:30 +0100 | [diff] [blame] | 1183 | } |
| 1184 | } |
| 1185 | |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 1186 | pr_info("... version: %d\n", x86_pmu.version); |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1187 | pr_info("... bit width: %d\n", x86_pmu.cntval_bits); |
| 1188 | pr_info("... generic registers: %d\n", x86_pmu.num_counters); |
| 1189 | pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask); |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 1190 | pr_info("... max period: %016Lx\n", x86_pmu.max_period); |
Robert Richter | 948b1bb | 2010-03-29 18:36:50 +0200 | [diff] [blame] | 1191 | pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed); |
Robert Richter | d6dc0b4 | 2010-03-17 12:49:13 +0100 | [diff] [blame] | 1192 | pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl); |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1193 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 1194 | perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW); |
Peter Zijlstra | 3f6da39 | 2010-03-05 13:01:18 +0100 | [diff] [blame] | 1195 | perf_cpu_notifier(x86_pmu_notifier); |
Peter Zijlstra | 004417a | 2010-11-25 18:38:29 +0100 | [diff] [blame] | 1196 | |
| 1197 | return 0; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1198 | } |
Peter Zijlstra | 004417a | 2010-11-25 18:38:29 +0100 | [diff] [blame] | 1199 | early_initcall(init_hw_perf_events); |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1200 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1201 | static inline void x86_pmu_read(struct perf_event *event) |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1202 | { |
Peter Zijlstra | cc2ad4b | 2010-03-02 20:18:39 +0100 | [diff] [blame] | 1203 | x86_perf_event_update(event); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1204 | } |
| 1205 | |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 1206 | /* |
| 1207 | * Start group events scheduling transaction |
| 1208 | * Set the flag to make pmu::enable() not perform the |
| 1209 | * schedulability test, it will be performed at commit time |
| 1210 | */ |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 1211 | static void x86_pmu_start_txn(struct pmu *pmu) |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 1212 | { |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1213 | perf_pmu_disable(pmu); |
Tejun Heo | 0a3aee0 | 2010-12-18 16:28:55 +0100 | [diff] [blame] | 1214 | __this_cpu_or(cpu_hw_events.group_flag, PERF_EVENT_TXN); |
| 1215 | __this_cpu_write(cpu_hw_events.n_txn, 0); |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | /* |
| 1219 | * Stop group events scheduling transaction |
| 1220 | * Clear the flag and pmu::enable() will perform the |
| 1221 | * schedulability test. |
| 1222 | */ |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 1223 | static void x86_pmu_cancel_txn(struct pmu *pmu) |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 1224 | { |
Tejun Heo | 0a3aee0 | 2010-12-18 16:28:55 +0100 | [diff] [blame] | 1225 | __this_cpu_and(cpu_hw_events.group_flag, ~PERF_EVENT_TXN); |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 1226 | /* |
| 1227 | * Truncate the collected events. |
| 1228 | */ |
Tejun Heo | 0a3aee0 | 2010-12-18 16:28:55 +0100 | [diff] [blame] | 1229 | __this_cpu_sub(cpu_hw_events.n_added, __this_cpu_read(cpu_hw_events.n_txn)); |
| 1230 | __this_cpu_sub(cpu_hw_events.n_events, __this_cpu_read(cpu_hw_events.n_txn)); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1231 | perf_pmu_enable(pmu); |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | /* |
| 1235 | * Commit group events scheduling transaction |
| 1236 | * Perform the group schedulability test as a whole |
| 1237 | * Return 0 if success |
| 1238 | */ |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 1239 | static int x86_pmu_commit_txn(struct pmu *pmu) |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 1240 | { |
| 1241 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 1242 | int assign[X86_PMC_IDX_MAX]; |
| 1243 | int n, ret; |
| 1244 | |
| 1245 | n = cpuc->n_events; |
| 1246 | |
| 1247 | if (!x86_pmu_initialized()) |
| 1248 | return -EAGAIN; |
| 1249 | |
| 1250 | ret = x86_pmu.schedule_events(cpuc, n, assign); |
| 1251 | if (ret) |
| 1252 | return ret; |
| 1253 | |
| 1254 | /* |
| 1255 | * copy new assignment, now we know it is possible |
| 1256 | * will be used by hw_perf_enable() |
| 1257 | */ |
| 1258 | memcpy(cpuc->assign, assign, n*sizeof(int)); |
| 1259 | |
Peter Zijlstra | 8d2cacb | 2010-05-25 17:49:05 +0200 | [diff] [blame] | 1260 | cpuc->group_flag &= ~PERF_EVENT_TXN; |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1261 | perf_pmu_enable(pmu); |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 1262 | return 0; |
| 1263 | } |
Stephane Eranian | cd8a38d | 2011-06-06 16:57:08 +0200 | [diff] [blame] | 1264 | /* |
| 1265 | * a fake_cpuc is used to validate event groups. Due to |
| 1266 | * the extra reg logic, we need to also allocate a fake |
| 1267 | * per_core and per_cpu structure. Otherwise, group events |
| 1268 | * using extra reg may conflict without the kernel being |
| 1269 | * able to catch this when the last event gets added to |
| 1270 | * the group. |
| 1271 | */ |
| 1272 | static void free_fake_cpuc(struct cpu_hw_events *cpuc) |
| 1273 | { |
| 1274 | kfree(cpuc->shared_regs); |
| 1275 | kfree(cpuc); |
| 1276 | } |
| 1277 | |
| 1278 | static struct cpu_hw_events *allocate_fake_cpuc(void) |
| 1279 | { |
| 1280 | struct cpu_hw_events *cpuc; |
| 1281 | int cpu = raw_smp_processor_id(); |
| 1282 | |
| 1283 | cpuc = kzalloc(sizeof(*cpuc), GFP_KERNEL); |
| 1284 | if (!cpuc) |
| 1285 | return ERR_PTR(-ENOMEM); |
| 1286 | |
| 1287 | /* only needed, if we have extra_regs */ |
| 1288 | if (x86_pmu.extra_regs) { |
| 1289 | cpuc->shared_regs = allocate_shared_regs(cpu); |
| 1290 | if (!cpuc->shared_regs) |
| 1291 | goto error; |
| 1292 | } |
| 1293 | return cpuc; |
| 1294 | error: |
| 1295 | free_fake_cpuc(cpuc); |
| 1296 | return ERR_PTR(-ENOMEM); |
| 1297 | } |
Lin Ming | 4d1c52b | 2010-04-23 13:56:12 +0800 | [diff] [blame] | 1298 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1299 | /* |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1300 | * validate that we can schedule this event |
| 1301 | */ |
| 1302 | static int validate_event(struct perf_event *event) |
| 1303 | { |
| 1304 | struct cpu_hw_events *fake_cpuc; |
| 1305 | struct event_constraint *c; |
| 1306 | int ret = 0; |
| 1307 | |
Stephane Eranian | cd8a38d | 2011-06-06 16:57:08 +0200 | [diff] [blame] | 1308 | fake_cpuc = allocate_fake_cpuc(); |
| 1309 | if (IS_ERR(fake_cpuc)) |
| 1310 | return PTR_ERR(fake_cpuc); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1311 | |
| 1312 | c = x86_pmu.get_event_constraints(fake_cpuc, event); |
| 1313 | |
| 1314 | if (!c || !c->weight) |
Peter Zijlstra | aa2bc1a | 2011-11-09 17:56:37 +0100 | [diff] [blame] | 1315 | ret = -EINVAL; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1316 | |
| 1317 | if (x86_pmu.put_event_constraints) |
| 1318 | x86_pmu.put_event_constraints(fake_cpuc, event); |
| 1319 | |
Stephane Eranian | cd8a38d | 2011-06-06 16:57:08 +0200 | [diff] [blame] | 1320 | free_fake_cpuc(fake_cpuc); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1321 | |
| 1322 | return ret; |
| 1323 | } |
| 1324 | |
| 1325 | /* |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1326 | * validate a single event group |
| 1327 | * |
| 1328 | * validation include: |
Ingo Molnar | 184f412 | 2010-01-27 08:39:39 +0100 | [diff] [blame] | 1329 | * - check events are compatible which each other |
| 1330 | * - events do not compete for the same counter |
| 1331 | * - number of events <= number of counters |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1332 | * |
| 1333 | * validation ensures the group can be loaded onto the |
| 1334 | * PMU if it was the only group available. |
| 1335 | */ |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1336 | static int validate_group(struct perf_event *event) |
| 1337 | { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1338 | struct perf_event *leader = event->group_leader; |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1339 | struct cpu_hw_events *fake_cpuc; |
Peter Zijlstra | aa2bc1a | 2011-11-09 17:56:37 +0100 | [diff] [blame] | 1340 | int ret = -EINVAL, n; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1341 | |
Stephane Eranian | cd8a38d | 2011-06-06 16:57:08 +0200 | [diff] [blame] | 1342 | fake_cpuc = allocate_fake_cpuc(); |
| 1343 | if (IS_ERR(fake_cpuc)) |
| 1344 | return PTR_ERR(fake_cpuc); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1345 | /* |
| 1346 | * the event is not yet connected with its |
| 1347 | * siblings therefore we must first collect |
| 1348 | * existing siblings, then add the new event |
| 1349 | * before we can simulate the scheduling |
| 1350 | */ |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1351 | n = collect_events(fake_cpuc, leader, true); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1352 | if (n < 0) |
Stephane Eranian | cd8a38d | 2011-06-06 16:57:08 +0200 | [diff] [blame] | 1353 | goto out; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1354 | |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1355 | fake_cpuc->n_events = n; |
| 1356 | n = collect_events(fake_cpuc, event, false); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1357 | if (n < 0) |
Stephane Eranian | cd8a38d | 2011-06-06 16:57:08 +0200 | [diff] [blame] | 1358 | goto out; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1359 | |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1360 | fake_cpuc->n_events = n; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1361 | |
Cyrill Gorcunov | a072738 | 2010-03-11 19:54:39 +0300 | [diff] [blame] | 1362 | ret = x86_pmu.schedule_events(fake_cpuc, n, NULL); |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1363 | |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1364 | out: |
Stephane Eranian | cd8a38d | 2011-06-06 16:57:08 +0200 | [diff] [blame] | 1365 | free_fake_cpuc(fake_cpuc); |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 1366 | return ret; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1367 | } |
| 1368 | |
Yinghai Lu | dda9911 | 2011-01-21 15:30:01 -0800 | [diff] [blame] | 1369 | static int x86_pmu_event_init(struct perf_event *event) |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1370 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 1371 | struct pmu *tmp; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1372 | int err; |
| 1373 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1374 | switch (event->attr.type) { |
| 1375 | case PERF_TYPE_RAW: |
| 1376 | case PERF_TYPE_HARDWARE: |
| 1377 | case PERF_TYPE_HW_CACHE: |
| 1378 | break; |
| 1379 | |
| 1380 | default: |
| 1381 | return -ENOENT; |
| 1382 | } |
| 1383 | |
| 1384 | err = __x86_pmu_event_init(event); |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1385 | if (!err) { |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 1386 | /* |
| 1387 | * we temporarily connect event to its pmu |
| 1388 | * such that validate_group() can classify |
| 1389 | * it as an x86 event using is_x86_event() |
| 1390 | */ |
| 1391 | tmp = event->pmu; |
| 1392 | event->pmu = &pmu; |
| 1393 | |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1394 | if (event->group_leader != event) |
| 1395 | err = validate_group(event); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1396 | else |
| 1397 | err = validate_event(event); |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 1398 | |
| 1399 | event->pmu = tmp; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1400 | } |
Peter Zijlstra | a1792cdac | 2009-09-09 10:04:47 +0200 | [diff] [blame] | 1401 | if (err) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1402 | if (event->destroy) |
| 1403 | event->destroy(event); |
Peter Zijlstra | a1792cdac | 2009-09-09 10:04:47 +0200 | [diff] [blame] | 1404 | } |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1405 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1406 | return err; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1407 | } |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1408 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1409 | static struct pmu pmu = { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1410 | .pmu_enable = x86_pmu_enable, |
| 1411 | .pmu_disable = x86_pmu_disable, |
| 1412 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1413 | .event_init = x86_pmu_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1414 | |
| 1415 | .add = x86_pmu_add, |
| 1416 | .del = x86_pmu_del, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1417 | .start = x86_pmu_start, |
| 1418 | .stop = x86_pmu_stop, |
| 1419 | .read = x86_pmu_read, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1420 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1421 | .start_txn = x86_pmu_start_txn, |
| 1422 | .cancel_txn = x86_pmu_cancel_txn, |
| 1423 | .commit_txn = x86_pmu_commit_txn, |
| 1424 | }; |
| 1425 | |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1426 | /* |
| 1427 | * callchain support |
| 1428 | */ |
| 1429 | |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1430 | static int backtrace_stack(void *data, char *name) |
| 1431 | { |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 1432 | return 0; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | static void backtrace_address(void *data, unsigned long addr, int reliable) |
| 1436 | { |
| 1437 | struct perf_callchain_entry *entry = data; |
| 1438 | |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 1439 | perf_callchain_store(entry, addr); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | static const struct stacktrace_ops backtrace_ops = { |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1443 | .stack = backtrace_stack, |
| 1444 | .address = backtrace_address, |
Frederic Weisbecker | 06d65bd | 2009-12-17 05:40:34 +0100 | [diff] [blame] | 1445 | .walk_stack = print_context_stack_bp, |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1446 | }; |
| 1447 | |
Frederic Weisbecker | 56962b444 | 2010-06-30 23:03:51 +0200 | [diff] [blame] | 1448 | void |
| 1449 | perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs) |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1450 | { |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 1451 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) { |
| 1452 | /* TODO: We don't support guest os callchain now */ |
Peter Zijlstra | ed80526 | 2010-08-20 14:30:41 +0200 | [diff] [blame] | 1453 | return; |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 1454 | } |
| 1455 | |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 1456 | perf_callchain_store(entry, regs->ip); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1457 | |
Namhyung Kim | e8e999cf | 2011-03-18 11:40:06 +0900 | [diff] [blame] | 1458 | dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1459 | } |
| 1460 | |
Torok Edwin | 257ef9d | 2010-03-17 12:07:16 +0200 | [diff] [blame] | 1461 | #ifdef CONFIG_COMPAT |
| 1462 | static inline int |
| 1463 | perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry) |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 1464 | { |
Torok Edwin | 257ef9d | 2010-03-17 12:07:16 +0200 | [diff] [blame] | 1465 | /* 32-bit process in 64-bit kernel. */ |
| 1466 | struct stack_frame_ia32 frame; |
| 1467 | const void __user *fp; |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 1468 | |
Torok Edwin | 257ef9d | 2010-03-17 12:07:16 +0200 | [diff] [blame] | 1469 | if (!test_thread_flag(TIF_IA32)) |
| 1470 | return 0; |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 1471 | |
Torok Edwin | 257ef9d | 2010-03-17 12:07:16 +0200 | [diff] [blame] | 1472 | fp = compat_ptr(regs->bp); |
| 1473 | while (entry->nr < PERF_MAX_STACK_DEPTH) { |
| 1474 | unsigned long bytes; |
| 1475 | frame.next_frame = 0; |
| 1476 | frame.return_address = 0; |
| 1477 | |
| 1478 | bytes = copy_from_user_nmi(&frame, fp, sizeof(frame)); |
| 1479 | if (bytes != sizeof(frame)) |
| 1480 | break; |
| 1481 | |
| 1482 | if (fp < compat_ptr(regs->sp)) |
| 1483 | break; |
| 1484 | |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 1485 | perf_callchain_store(entry, frame.return_address); |
Torok Edwin | 257ef9d | 2010-03-17 12:07:16 +0200 | [diff] [blame] | 1486 | fp = compat_ptr(frame.next_frame); |
| 1487 | } |
| 1488 | return 1; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1489 | } |
Torok Edwin | 257ef9d | 2010-03-17 12:07:16 +0200 | [diff] [blame] | 1490 | #else |
| 1491 | static inline int |
| 1492 | perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry) |
| 1493 | { |
| 1494 | return 0; |
| 1495 | } |
| 1496 | #endif |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1497 | |
Frederic Weisbecker | 56962b444 | 2010-06-30 23:03:51 +0200 | [diff] [blame] | 1498 | void |
| 1499 | perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs) |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1500 | { |
| 1501 | struct stack_frame frame; |
| 1502 | const void __user *fp; |
| 1503 | |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 1504 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) { |
| 1505 | /* TODO: We don't support guest os callchain now */ |
Peter Zijlstra | ed80526 | 2010-08-20 14:30:41 +0200 | [diff] [blame] | 1506 | return; |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 1507 | } |
Ingo Molnar | 5a6cec3 | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1508 | |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 1509 | fp = (void __user *)regs->bp; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1510 | |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 1511 | perf_callchain_store(entry, regs->ip); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1512 | |
Andrey Vagin | 20afc60 | 2011-08-30 12:32:36 +0400 | [diff] [blame] | 1513 | if (!current->mm) |
| 1514 | return; |
| 1515 | |
Torok Edwin | 257ef9d | 2010-03-17 12:07:16 +0200 | [diff] [blame] | 1516 | if (perf_callchain_user32(regs, entry)) |
| 1517 | return; |
| 1518 | |
Peter Zijlstra | f9188e0 | 2009-06-18 22:20:52 +0200 | [diff] [blame] | 1519 | while (entry->nr < PERF_MAX_STACK_DEPTH) { |
Torok Edwin | 257ef9d | 2010-03-17 12:07:16 +0200 | [diff] [blame] | 1520 | unsigned long bytes; |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 1521 | frame.next_frame = NULL; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1522 | frame.return_address = 0; |
| 1523 | |
Torok Edwin | 257ef9d | 2010-03-17 12:07:16 +0200 | [diff] [blame] | 1524 | bytes = copy_from_user_nmi(&frame, fp, sizeof(frame)); |
| 1525 | if (bytes != sizeof(frame)) |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1526 | break; |
| 1527 | |
Ingo Molnar | 5a6cec3 | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1528 | if ((unsigned long)fp < regs->sp) |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1529 | break; |
| 1530 | |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 1531 | perf_callchain_store(entry, frame.return_address); |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 1532 | fp = frame.next_frame; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 1533 | } |
| 1534 | } |
| 1535 | |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 1536 | unsigned long perf_instruction_pointer(struct pt_regs *regs) |
| 1537 | { |
| 1538 | unsigned long ip; |
Zhang, Yanmin | dcf46b9 | 2010-04-20 10:13:58 +0800 | [diff] [blame] | 1539 | |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 1540 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) |
| 1541 | ip = perf_guest_cbs->get_guest_ip(); |
| 1542 | else |
| 1543 | ip = instruction_pointer(regs); |
Zhang, Yanmin | dcf46b9 | 2010-04-20 10:13:58 +0800 | [diff] [blame] | 1544 | |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 1545 | return ip; |
| 1546 | } |
| 1547 | |
| 1548 | unsigned long perf_misc_flags(struct pt_regs *regs) |
| 1549 | { |
| 1550 | int misc = 0; |
Zhang, Yanmin | dcf46b9 | 2010-04-20 10:13:58 +0800 | [diff] [blame] | 1551 | |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 1552 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) { |
Zhang, Yanmin | dcf46b9 | 2010-04-20 10:13:58 +0800 | [diff] [blame] | 1553 | if (perf_guest_cbs->is_user_mode()) |
| 1554 | misc |= PERF_RECORD_MISC_GUEST_USER; |
| 1555 | else |
| 1556 | misc |= PERF_RECORD_MISC_GUEST_KERNEL; |
| 1557 | } else { |
| 1558 | if (user_mode(regs)) |
| 1559 | misc |= PERF_RECORD_MISC_USER; |
| 1560 | else |
| 1561 | misc |= PERF_RECORD_MISC_KERNEL; |
| 1562 | } |
| 1563 | |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 1564 | if (regs->flags & PERF_EFLAGS_EXACT) |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1565 | misc |= PERF_RECORD_MISC_EXACT_IP; |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 1566 | |
| 1567 | return misc; |
| 1568 | } |