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> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 10 | * |
| 11 | * For licencing details see kernel-base/COPYING |
| 12 | */ |
| 13 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 14 | #include <linux/perf_event.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 15 | #include <linux/capability.h> |
| 16 | #include <linux/notifier.h> |
| 17 | #include <linux/hardirq.h> |
| 18 | #include <linux/kprobes.h> |
Thomas Gleixner | 4ac1329 | 2008-12-09 21:43:39 +0100 | [diff] [blame] | 19 | #include <linux/module.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 20 | #include <linux/kdebug.h> |
| 21 | #include <linux/sched.h> |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 22 | #include <linux/uaccess.h> |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 23 | #include <linux/highmem.h> |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 24 | #include <linux/cpu.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 25 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 26 | #include <asm/apic.h> |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 27 | #include <asm/stacktrace.h> |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 28 | #include <asm/nmi.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 29 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 30 | static u64 perf_event_mask __read_mostly; |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 31 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 32 | /* The maximal number of PEBS events: */ |
| 33 | #define MAX_PEBS_EVENTS 4 |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 34 | |
| 35 | /* The size of a BTS record in bytes: */ |
| 36 | #define BTS_RECORD_SIZE 24 |
| 37 | |
| 38 | /* The size of a per-cpu BTS buffer in bytes: */ |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 39 | #define BTS_BUFFER_SIZE (BTS_RECORD_SIZE * 2048) |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 40 | |
| 41 | /* The BTS overflow threshold in bytes from the end of the buffer: */ |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 42 | #define BTS_OVFL_TH (BTS_RECORD_SIZE * 128) |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 43 | |
| 44 | |
| 45 | /* |
| 46 | * Bits in the debugctlmsr controlling branch tracing. |
| 47 | */ |
| 48 | #define X86_DEBUGCTL_TR (1 << 6) |
| 49 | #define X86_DEBUGCTL_BTS (1 << 7) |
| 50 | #define X86_DEBUGCTL_BTINT (1 << 8) |
| 51 | #define X86_DEBUGCTL_BTS_OFF_OS (1 << 9) |
| 52 | #define X86_DEBUGCTL_BTS_OFF_USR (1 << 10) |
| 53 | |
| 54 | /* |
| 55 | * A debug store configuration. |
| 56 | * |
| 57 | * We only support architectures that use 64bit fields. |
| 58 | */ |
| 59 | struct debug_store { |
| 60 | u64 bts_buffer_base; |
| 61 | u64 bts_index; |
| 62 | u64 bts_absolute_maximum; |
| 63 | u64 bts_interrupt_threshold; |
| 64 | u64 pebs_buffer_base; |
| 65 | u64 pebs_index; |
| 66 | u64 pebs_absolute_maximum; |
| 67 | u64 pebs_interrupt_threshold; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 68 | u64 pebs_event_reset[MAX_PEBS_EVENTS]; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 71 | struct cpu_hw_events { |
| 72 | struct perf_event *events[X86_PMC_IDX_MAX]; |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 73 | unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
| 74 | unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
Mike Galbraith | 4b39fd9 | 2009-01-23 14:36:16 +0100 | [diff] [blame] | 75 | unsigned long interrupts; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 76 | int enabled; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 77 | struct debug_store *ds; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | /* |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 81 | * struct x86_pmu - generic x86 pmu |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 82 | */ |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 83 | struct x86_pmu { |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 84 | const char *name; |
| 85 | int version; |
Yong Wang | a328810 | 2009-06-03 13:12:55 +0800 | [diff] [blame] | 86 | int (*handle_irq)(struct pt_regs *); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 87 | void (*disable_all)(void); |
| 88 | void (*enable_all)(void); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 89 | void (*enable)(struct hw_perf_event *, int); |
| 90 | void (*disable)(struct hw_perf_event *, int); |
Jaswinder Singh Rajput | 169e41e | 2009-02-28 18:37:49 +0530 | [diff] [blame] | 91 | unsigned eventsel; |
| 92 | unsigned perfctr; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 93 | u64 (*event_map)(int); |
| 94 | u64 (*raw_event)(u64); |
Jaswinder Singh Rajput | 169e41e | 2009-02-28 18:37:49 +0530 | [diff] [blame] | 95 | int max_events; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 96 | int num_events; |
| 97 | int num_events_fixed; |
| 98 | int event_bits; |
| 99 | u64 event_mask; |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 100 | int apic; |
Robert Richter | c619b8f | 2009-04-29 12:47:23 +0200 | [diff] [blame] | 101 | u64 max_period; |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 102 | u64 intel_ctrl; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 103 | void (*enable_bts)(u64 config); |
| 104 | void (*disable_bts)(void); |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 105 | }; |
| 106 | |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 107 | static struct x86_pmu x86_pmu __read_mostly; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 108 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 109 | static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 110 | .enabled = 1, |
| 111 | }; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 112 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 113 | /* |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 114 | * Not sure about some of these |
| 115 | */ |
| 116 | static const u64 p6_perfmon_event_map[] = |
| 117 | { |
| 118 | [PERF_COUNT_HW_CPU_CYCLES] = 0x0079, |
| 119 | [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0, |
Ingo Molnar | f64cccc | 2009-08-11 10:26:33 +0200 | [diff] [blame] | 120 | [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0f2e, |
| 121 | [PERF_COUNT_HW_CACHE_MISSES] = 0x012e, |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 122 | [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4, |
| 123 | [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5, |
| 124 | [PERF_COUNT_HW_BUS_CYCLES] = 0x0062, |
| 125 | }; |
| 126 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 127 | static u64 p6_pmu_event_map(int hw_event) |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 128 | { |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 129 | return p6_perfmon_event_map[hw_event]; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 130 | } |
| 131 | |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 132 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 133 | * Event setting that is specified not to count anything. |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 134 | * We use this to effectively disable a counter. |
| 135 | * |
| 136 | * L2_RQSTS with 0 MESI unit mask. |
| 137 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 138 | #define P6_NOP_EVENT 0x0000002EULL |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 139 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 140 | static u64 p6_pmu_raw_event(u64 hw_event) |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 141 | { |
| 142 | #define P6_EVNTSEL_EVENT_MASK 0x000000FFULL |
| 143 | #define P6_EVNTSEL_UNIT_MASK 0x0000FF00ULL |
| 144 | #define P6_EVNTSEL_EDGE_MASK 0x00040000ULL |
| 145 | #define P6_EVNTSEL_INV_MASK 0x00800000ULL |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 146 | #define P6_EVNTSEL_REG_MASK 0xFF000000ULL |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 147 | |
| 148 | #define P6_EVNTSEL_MASK \ |
| 149 | (P6_EVNTSEL_EVENT_MASK | \ |
| 150 | P6_EVNTSEL_UNIT_MASK | \ |
| 151 | P6_EVNTSEL_EDGE_MASK | \ |
| 152 | P6_EVNTSEL_INV_MASK | \ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 153 | P6_EVNTSEL_REG_MASK) |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 154 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 155 | return hw_event & P6_EVNTSEL_MASK; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | |
| 159 | /* |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 160 | * Intel PerfMon v3. Used on Core2 and later. |
| 161 | */ |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 162 | static const u64 intel_perfmon_event_map[] = |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 163 | { |
Peter Zijlstra | f4dbfa8 | 2009-06-11 14:06:28 +0200 | [diff] [blame] | 164 | [PERF_COUNT_HW_CPU_CYCLES] = 0x003c, |
| 165 | [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0, |
| 166 | [PERF_COUNT_HW_CACHE_REFERENCES] = 0x4f2e, |
| 167 | [PERF_COUNT_HW_CACHE_MISSES] = 0x412e, |
| 168 | [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4, |
| 169 | [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5, |
| 170 | [PERF_COUNT_HW_BUS_CYCLES] = 0x013c, |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 171 | }; |
| 172 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 173 | static u64 intel_pmu_event_map(int hw_event) |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 174 | { |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 175 | return intel_perfmon_event_map[hw_event]; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 176 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 177 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 178 | /* |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 179 | * Generalized hw caching related hw_event table, filled |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 180 | * in on a per model basis. A value of 0 means |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 181 | * 'not supported', -1 means 'hw_event makes no sense on |
| 182 | * this CPU', any other value means the raw hw_event |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 183 | * ID. |
| 184 | */ |
| 185 | |
| 186 | #define C(x) PERF_COUNT_HW_CACHE_##x |
| 187 | |
| 188 | static u64 __read_mostly hw_cache_event_ids |
| 189 | [PERF_COUNT_HW_CACHE_MAX] |
| 190 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 191 | [PERF_COUNT_HW_CACHE_RESULT_MAX]; |
| 192 | |
| 193 | static const u64 nehalem_hw_cache_event_ids |
| 194 | [PERF_COUNT_HW_CACHE_MAX] |
| 195 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 196 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = |
| 197 | { |
| 198 | [ C(L1D) ] = { |
| 199 | [ C(OP_READ) ] = { |
| 200 | [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */ |
| 201 | [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */ |
| 202 | }, |
| 203 | [ C(OP_WRITE) ] = { |
| 204 | [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */ |
| 205 | [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */ |
| 206 | }, |
| 207 | [ C(OP_PREFETCH) ] = { |
| 208 | [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */ |
| 209 | [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */ |
| 210 | }, |
| 211 | }, |
| 212 | [ C(L1I ) ] = { |
| 213 | [ C(OP_READ) ] = { |
Yong Wang | fecc8ac | 2009-06-09 21:15:53 +0800 | [diff] [blame] | 214 | [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */ |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 215 | [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */ |
| 216 | }, |
| 217 | [ C(OP_WRITE) ] = { |
| 218 | [ C(RESULT_ACCESS) ] = -1, |
| 219 | [ C(RESULT_MISS) ] = -1, |
| 220 | }, |
| 221 | [ C(OP_PREFETCH) ] = { |
| 222 | [ C(RESULT_ACCESS) ] = 0x0, |
| 223 | [ C(RESULT_MISS) ] = 0x0, |
| 224 | }, |
| 225 | }, |
Peter Zijlstra | 8be6e8f | 2009-06-11 14:19:11 +0200 | [diff] [blame] | 226 | [ C(LL ) ] = { |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 227 | [ C(OP_READ) ] = { |
| 228 | [ C(RESULT_ACCESS) ] = 0x0324, /* L2_RQSTS.LOADS */ |
| 229 | [ C(RESULT_MISS) ] = 0x0224, /* L2_RQSTS.LD_MISS */ |
| 230 | }, |
| 231 | [ C(OP_WRITE) ] = { |
| 232 | [ C(RESULT_ACCESS) ] = 0x0c24, /* L2_RQSTS.RFOS */ |
| 233 | [ C(RESULT_MISS) ] = 0x0824, /* L2_RQSTS.RFO_MISS */ |
| 234 | }, |
| 235 | [ C(OP_PREFETCH) ] = { |
Peter Zijlstra | 8be6e8f | 2009-06-11 14:19:11 +0200 | [diff] [blame] | 236 | [ C(RESULT_ACCESS) ] = 0x4f2e, /* LLC Reference */ |
| 237 | [ C(RESULT_MISS) ] = 0x412e, /* LLC Misses */ |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 238 | }, |
| 239 | }, |
| 240 | [ C(DTLB) ] = { |
| 241 | [ C(OP_READ) ] = { |
| 242 | [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */ |
| 243 | [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */ |
| 244 | }, |
| 245 | [ C(OP_WRITE) ] = { |
| 246 | [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */ |
| 247 | [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */ |
| 248 | }, |
| 249 | [ C(OP_PREFETCH) ] = { |
| 250 | [ C(RESULT_ACCESS) ] = 0x0, |
| 251 | [ C(RESULT_MISS) ] = 0x0, |
| 252 | }, |
| 253 | }, |
| 254 | [ C(ITLB) ] = { |
| 255 | [ C(OP_READ) ] = { |
| 256 | [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */ |
Yong Wang | fecc8ac | 2009-06-09 21:15:53 +0800 | [diff] [blame] | 257 | [ C(RESULT_MISS) ] = 0x20c8, /* ITLB_MISS_RETIRED */ |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 258 | }, |
| 259 | [ C(OP_WRITE) ] = { |
| 260 | [ C(RESULT_ACCESS) ] = -1, |
| 261 | [ C(RESULT_MISS) ] = -1, |
| 262 | }, |
| 263 | [ C(OP_PREFETCH) ] = { |
| 264 | [ C(RESULT_ACCESS) ] = -1, |
| 265 | [ C(RESULT_MISS) ] = -1, |
| 266 | }, |
| 267 | }, |
| 268 | [ C(BPU ) ] = { |
| 269 | [ C(OP_READ) ] = { |
| 270 | [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */ |
| 271 | [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */ |
| 272 | }, |
| 273 | [ C(OP_WRITE) ] = { |
| 274 | [ C(RESULT_ACCESS) ] = -1, |
| 275 | [ C(RESULT_MISS) ] = -1, |
| 276 | }, |
| 277 | [ C(OP_PREFETCH) ] = { |
| 278 | [ C(RESULT_ACCESS) ] = -1, |
| 279 | [ C(RESULT_MISS) ] = -1, |
| 280 | }, |
| 281 | }, |
| 282 | }; |
| 283 | |
| 284 | static const u64 core2_hw_cache_event_ids |
| 285 | [PERF_COUNT_HW_CACHE_MAX] |
| 286 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 287 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = |
| 288 | { |
Thomas Gleixner | 0312af8 | 2009-06-08 07:42:04 +0200 | [diff] [blame] | 289 | [ C(L1D) ] = { |
| 290 | [ C(OP_READ) ] = { |
| 291 | [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */ |
| 292 | [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */ |
| 293 | }, |
| 294 | [ C(OP_WRITE) ] = { |
| 295 | [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */ |
| 296 | [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */ |
| 297 | }, |
| 298 | [ C(OP_PREFETCH) ] = { |
| 299 | [ C(RESULT_ACCESS) ] = 0x104e, /* L1D_PREFETCH.REQUESTS */ |
| 300 | [ C(RESULT_MISS) ] = 0, |
| 301 | }, |
| 302 | }, |
| 303 | [ C(L1I ) ] = { |
| 304 | [ C(OP_READ) ] = { |
| 305 | [ C(RESULT_ACCESS) ] = 0x0080, /* L1I.READS */ |
| 306 | [ C(RESULT_MISS) ] = 0x0081, /* L1I.MISSES */ |
| 307 | }, |
| 308 | [ C(OP_WRITE) ] = { |
| 309 | [ C(RESULT_ACCESS) ] = -1, |
| 310 | [ C(RESULT_MISS) ] = -1, |
| 311 | }, |
| 312 | [ C(OP_PREFETCH) ] = { |
| 313 | [ C(RESULT_ACCESS) ] = 0, |
| 314 | [ C(RESULT_MISS) ] = 0, |
| 315 | }, |
| 316 | }, |
Peter Zijlstra | 8be6e8f | 2009-06-11 14:19:11 +0200 | [diff] [blame] | 317 | [ C(LL ) ] = { |
Thomas Gleixner | 0312af8 | 2009-06-08 07:42:04 +0200 | [diff] [blame] | 318 | [ C(OP_READ) ] = { |
| 319 | [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */ |
| 320 | [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */ |
| 321 | }, |
| 322 | [ C(OP_WRITE) ] = { |
| 323 | [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */ |
| 324 | [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */ |
| 325 | }, |
| 326 | [ C(OP_PREFETCH) ] = { |
| 327 | [ C(RESULT_ACCESS) ] = 0, |
| 328 | [ C(RESULT_MISS) ] = 0, |
| 329 | }, |
| 330 | }, |
| 331 | [ C(DTLB) ] = { |
| 332 | [ C(OP_READ) ] = { |
| 333 | [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */ |
| 334 | [ C(RESULT_MISS) ] = 0x0208, /* DTLB_MISSES.MISS_LD */ |
| 335 | }, |
| 336 | [ C(OP_WRITE) ] = { |
| 337 | [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */ |
| 338 | [ C(RESULT_MISS) ] = 0x0808, /* DTLB_MISSES.MISS_ST */ |
| 339 | }, |
| 340 | [ C(OP_PREFETCH) ] = { |
| 341 | [ C(RESULT_ACCESS) ] = 0, |
| 342 | [ C(RESULT_MISS) ] = 0, |
| 343 | }, |
| 344 | }, |
| 345 | [ C(ITLB) ] = { |
| 346 | [ C(OP_READ) ] = { |
| 347 | [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */ |
| 348 | [ C(RESULT_MISS) ] = 0x1282, /* ITLBMISSES */ |
| 349 | }, |
| 350 | [ C(OP_WRITE) ] = { |
| 351 | [ C(RESULT_ACCESS) ] = -1, |
| 352 | [ C(RESULT_MISS) ] = -1, |
| 353 | }, |
| 354 | [ C(OP_PREFETCH) ] = { |
| 355 | [ C(RESULT_ACCESS) ] = -1, |
| 356 | [ C(RESULT_MISS) ] = -1, |
| 357 | }, |
| 358 | }, |
| 359 | [ C(BPU ) ] = { |
| 360 | [ C(OP_READ) ] = { |
| 361 | [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */ |
| 362 | [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */ |
| 363 | }, |
| 364 | [ C(OP_WRITE) ] = { |
| 365 | [ C(RESULT_ACCESS) ] = -1, |
| 366 | [ C(RESULT_MISS) ] = -1, |
| 367 | }, |
| 368 | [ C(OP_PREFETCH) ] = { |
| 369 | [ C(RESULT_ACCESS) ] = -1, |
| 370 | [ C(RESULT_MISS) ] = -1, |
| 371 | }, |
| 372 | }, |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 373 | }; |
| 374 | |
| 375 | static const u64 atom_hw_cache_event_ids |
| 376 | [PERF_COUNT_HW_CACHE_MAX] |
| 377 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 378 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = |
| 379 | { |
Thomas Gleixner | ad68922 | 2009-06-08 09:30:41 +0200 | [diff] [blame] | 380 | [ C(L1D) ] = { |
| 381 | [ C(OP_READ) ] = { |
| 382 | [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE.LD */ |
| 383 | [ C(RESULT_MISS) ] = 0, |
| 384 | }, |
| 385 | [ C(OP_WRITE) ] = { |
Yong Wang | fecc8ac | 2009-06-09 21:15:53 +0800 | [diff] [blame] | 386 | [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE.ST */ |
Thomas Gleixner | ad68922 | 2009-06-08 09:30:41 +0200 | [diff] [blame] | 387 | [ C(RESULT_MISS) ] = 0, |
| 388 | }, |
| 389 | [ C(OP_PREFETCH) ] = { |
| 390 | [ C(RESULT_ACCESS) ] = 0x0, |
| 391 | [ C(RESULT_MISS) ] = 0, |
| 392 | }, |
| 393 | }, |
| 394 | [ C(L1I ) ] = { |
| 395 | [ C(OP_READ) ] = { |
Yong Wang | fecc8ac | 2009-06-09 21:15:53 +0800 | [diff] [blame] | 396 | [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */ |
| 397 | [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */ |
Thomas Gleixner | ad68922 | 2009-06-08 09:30:41 +0200 | [diff] [blame] | 398 | }, |
| 399 | [ C(OP_WRITE) ] = { |
| 400 | [ C(RESULT_ACCESS) ] = -1, |
| 401 | [ C(RESULT_MISS) ] = -1, |
| 402 | }, |
| 403 | [ C(OP_PREFETCH) ] = { |
| 404 | [ C(RESULT_ACCESS) ] = 0, |
| 405 | [ C(RESULT_MISS) ] = 0, |
| 406 | }, |
| 407 | }, |
Peter Zijlstra | 8be6e8f | 2009-06-11 14:19:11 +0200 | [diff] [blame] | 408 | [ C(LL ) ] = { |
Thomas Gleixner | ad68922 | 2009-06-08 09:30:41 +0200 | [diff] [blame] | 409 | [ C(OP_READ) ] = { |
| 410 | [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */ |
| 411 | [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */ |
| 412 | }, |
| 413 | [ C(OP_WRITE) ] = { |
| 414 | [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */ |
| 415 | [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */ |
| 416 | }, |
| 417 | [ C(OP_PREFETCH) ] = { |
| 418 | [ C(RESULT_ACCESS) ] = 0, |
| 419 | [ C(RESULT_MISS) ] = 0, |
| 420 | }, |
| 421 | }, |
| 422 | [ C(DTLB) ] = { |
| 423 | [ C(OP_READ) ] = { |
Yong Wang | fecc8ac | 2009-06-09 21:15:53 +0800 | [diff] [blame] | 424 | [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE_LD.MESI (alias) */ |
Thomas Gleixner | ad68922 | 2009-06-08 09:30:41 +0200 | [diff] [blame] | 425 | [ C(RESULT_MISS) ] = 0x0508, /* DTLB_MISSES.MISS_LD */ |
| 426 | }, |
| 427 | [ C(OP_WRITE) ] = { |
Yong Wang | fecc8ac | 2009-06-09 21:15:53 +0800 | [diff] [blame] | 428 | [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE_ST.MESI (alias) */ |
Thomas Gleixner | ad68922 | 2009-06-08 09:30:41 +0200 | [diff] [blame] | 429 | [ C(RESULT_MISS) ] = 0x0608, /* DTLB_MISSES.MISS_ST */ |
| 430 | }, |
| 431 | [ C(OP_PREFETCH) ] = { |
| 432 | [ C(RESULT_ACCESS) ] = 0, |
| 433 | [ C(RESULT_MISS) ] = 0, |
| 434 | }, |
| 435 | }, |
| 436 | [ C(ITLB) ] = { |
| 437 | [ C(OP_READ) ] = { |
| 438 | [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */ |
| 439 | [ C(RESULT_MISS) ] = 0x0282, /* ITLB.MISSES */ |
| 440 | }, |
| 441 | [ C(OP_WRITE) ] = { |
| 442 | [ C(RESULT_ACCESS) ] = -1, |
| 443 | [ C(RESULT_MISS) ] = -1, |
| 444 | }, |
| 445 | [ C(OP_PREFETCH) ] = { |
| 446 | [ C(RESULT_ACCESS) ] = -1, |
| 447 | [ C(RESULT_MISS) ] = -1, |
| 448 | }, |
| 449 | }, |
| 450 | [ C(BPU ) ] = { |
| 451 | [ C(OP_READ) ] = { |
| 452 | [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */ |
| 453 | [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */ |
| 454 | }, |
| 455 | [ C(OP_WRITE) ] = { |
| 456 | [ C(RESULT_ACCESS) ] = -1, |
| 457 | [ C(RESULT_MISS) ] = -1, |
| 458 | }, |
| 459 | [ C(OP_PREFETCH) ] = { |
| 460 | [ C(RESULT_ACCESS) ] = -1, |
| 461 | [ C(RESULT_MISS) ] = -1, |
| 462 | }, |
| 463 | }, |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 464 | }; |
| 465 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 466 | static u64 intel_pmu_raw_event(u64 hw_event) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 467 | { |
Peter Zijlstra | 82bae4f8 | 2009-03-13 12:21:31 +0100 | [diff] [blame] | 468 | #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL |
| 469 | #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL |
Peter Zijlstra | ff99be5 | 2009-05-25 17:39:03 +0200 | [diff] [blame] | 470 | #define CORE_EVNTSEL_EDGE_MASK 0x00040000ULL |
| 471 | #define CORE_EVNTSEL_INV_MASK 0x00800000ULL |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 472 | #define CORE_EVNTSEL_REG_MASK 0xFF000000ULL |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 473 | |
Ingo Molnar | 128f048 | 2009-06-03 22:19:36 +0200 | [diff] [blame] | 474 | #define CORE_EVNTSEL_MASK \ |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 475 | (CORE_EVNTSEL_EVENT_MASK | \ |
| 476 | CORE_EVNTSEL_UNIT_MASK | \ |
Peter Zijlstra | ff99be5 | 2009-05-25 17:39:03 +0200 | [diff] [blame] | 477 | CORE_EVNTSEL_EDGE_MASK | \ |
| 478 | CORE_EVNTSEL_INV_MASK | \ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 479 | CORE_EVNTSEL_REG_MASK) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 480 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 481 | return hw_event & CORE_EVNTSEL_MASK; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 482 | } |
| 483 | |
Jaswinder Singh Rajput | f4db43a | 2009-06-13 01:06:21 +0530 | [diff] [blame] | 484 | static const u64 amd_hw_cache_event_ids |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 485 | [PERF_COUNT_HW_CACHE_MAX] |
| 486 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 487 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = |
| 488 | { |
| 489 | [ C(L1D) ] = { |
| 490 | [ C(OP_READ) ] = { |
Jaswinder Singh Rajput | f4db43a | 2009-06-13 01:06:21 +0530 | [diff] [blame] | 491 | [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */ |
| 492 | [ C(RESULT_MISS) ] = 0x0041, /* Data Cache Misses */ |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 493 | }, |
| 494 | [ C(OP_WRITE) ] = { |
Jaswinder Singh Rajput | d9f2a5e | 2009-06-20 13:19:25 +0530 | [diff] [blame] | 495 | [ C(RESULT_ACCESS) ] = 0x0142, /* Data Cache Refills :system */ |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 496 | [ C(RESULT_MISS) ] = 0, |
| 497 | }, |
| 498 | [ C(OP_PREFETCH) ] = { |
Jaswinder Singh Rajput | f4db43a | 2009-06-13 01:06:21 +0530 | [diff] [blame] | 499 | [ C(RESULT_ACCESS) ] = 0x0267, /* Data Prefetcher :attempts */ |
| 500 | [ C(RESULT_MISS) ] = 0x0167, /* Data Prefetcher :cancelled */ |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 501 | }, |
| 502 | }, |
| 503 | [ C(L1I ) ] = { |
| 504 | [ C(OP_READ) ] = { |
| 505 | [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction cache fetches */ |
| 506 | [ C(RESULT_MISS) ] = 0x0081, /* Instruction cache misses */ |
| 507 | }, |
| 508 | [ C(OP_WRITE) ] = { |
| 509 | [ C(RESULT_ACCESS) ] = -1, |
| 510 | [ C(RESULT_MISS) ] = -1, |
| 511 | }, |
| 512 | [ C(OP_PREFETCH) ] = { |
Jaswinder Singh Rajput | f4db43a | 2009-06-13 01:06:21 +0530 | [diff] [blame] | 513 | [ C(RESULT_ACCESS) ] = 0x014B, /* Prefetch Instructions :Load */ |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 514 | [ C(RESULT_MISS) ] = 0, |
| 515 | }, |
| 516 | }, |
Peter Zijlstra | 8be6e8f | 2009-06-11 14:19:11 +0200 | [diff] [blame] | 517 | [ C(LL ) ] = { |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 518 | [ C(OP_READ) ] = { |
Jaswinder Singh Rajput | f4db43a | 2009-06-13 01:06:21 +0530 | [diff] [blame] | 519 | [ C(RESULT_ACCESS) ] = 0x037D, /* Requests to L2 Cache :IC+DC */ |
| 520 | [ C(RESULT_MISS) ] = 0x037E, /* L2 Cache Misses : IC+DC */ |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 521 | }, |
| 522 | [ C(OP_WRITE) ] = { |
Jaswinder Singh Rajput | f4db43a | 2009-06-13 01:06:21 +0530 | [diff] [blame] | 523 | [ C(RESULT_ACCESS) ] = 0x017F, /* L2 Fill/Writeback */ |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 524 | [ C(RESULT_MISS) ] = 0, |
| 525 | }, |
| 526 | [ C(OP_PREFETCH) ] = { |
| 527 | [ C(RESULT_ACCESS) ] = 0, |
| 528 | [ C(RESULT_MISS) ] = 0, |
| 529 | }, |
| 530 | }, |
| 531 | [ C(DTLB) ] = { |
| 532 | [ C(OP_READ) ] = { |
Jaswinder Singh Rajput | f4db43a | 2009-06-13 01:06:21 +0530 | [diff] [blame] | 533 | [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */ |
| 534 | [ C(RESULT_MISS) ] = 0x0046, /* L1 DTLB and L2 DLTB Miss */ |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 535 | }, |
| 536 | [ C(OP_WRITE) ] = { |
| 537 | [ C(RESULT_ACCESS) ] = 0, |
| 538 | [ C(RESULT_MISS) ] = 0, |
| 539 | }, |
| 540 | [ C(OP_PREFETCH) ] = { |
| 541 | [ C(RESULT_ACCESS) ] = 0, |
| 542 | [ C(RESULT_MISS) ] = 0, |
| 543 | }, |
| 544 | }, |
| 545 | [ C(ITLB) ] = { |
| 546 | [ C(OP_READ) ] = { |
| 547 | [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction fecthes */ |
| 548 | [ C(RESULT_MISS) ] = 0x0085, /* Instr. fetch ITLB misses */ |
| 549 | }, |
| 550 | [ C(OP_WRITE) ] = { |
| 551 | [ C(RESULT_ACCESS) ] = -1, |
| 552 | [ C(RESULT_MISS) ] = -1, |
| 553 | }, |
| 554 | [ C(OP_PREFETCH) ] = { |
| 555 | [ C(RESULT_ACCESS) ] = -1, |
| 556 | [ C(RESULT_MISS) ] = -1, |
| 557 | }, |
| 558 | }, |
| 559 | [ C(BPU ) ] = { |
| 560 | [ C(OP_READ) ] = { |
| 561 | [ C(RESULT_ACCESS) ] = 0x00c2, /* Retired Branch Instr. */ |
| 562 | [ C(RESULT_MISS) ] = 0x00c3, /* Retired Mispredicted BI */ |
| 563 | }, |
| 564 | [ C(OP_WRITE) ] = { |
| 565 | [ C(RESULT_ACCESS) ] = -1, |
| 566 | [ C(RESULT_MISS) ] = -1, |
| 567 | }, |
| 568 | [ C(OP_PREFETCH) ] = { |
| 569 | [ C(RESULT_ACCESS) ] = -1, |
| 570 | [ C(RESULT_MISS) ] = -1, |
| 571 | }, |
| 572 | }, |
| 573 | }; |
| 574 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 575 | /* |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 576 | * AMD Performance Monitor K7 and later. |
| 577 | */ |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 578 | static const u64 amd_perfmon_event_map[] = |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 579 | { |
Peter Zijlstra | f4dbfa8 | 2009-06-11 14:06:28 +0200 | [diff] [blame] | 580 | [PERF_COUNT_HW_CPU_CYCLES] = 0x0076, |
| 581 | [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0, |
| 582 | [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0080, |
| 583 | [PERF_COUNT_HW_CACHE_MISSES] = 0x0081, |
| 584 | [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4, |
| 585 | [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5, |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 586 | }; |
| 587 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 588 | static u64 amd_pmu_event_map(int hw_event) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 589 | { |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 590 | return amd_perfmon_event_map[hw_event]; |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 591 | } |
| 592 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 593 | static u64 amd_pmu_raw_event(u64 hw_event) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 594 | { |
Peter Zijlstra | 82bae4f8 | 2009-03-13 12:21:31 +0100 | [diff] [blame] | 595 | #define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL |
| 596 | #define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL |
Peter Zijlstra | ff99be5 | 2009-05-25 17:39:03 +0200 | [diff] [blame] | 597 | #define K7_EVNTSEL_EDGE_MASK 0x000040000ULL |
| 598 | #define K7_EVNTSEL_INV_MASK 0x000800000ULL |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 599 | #define K7_EVNTSEL_REG_MASK 0x0FF000000ULL |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 600 | |
| 601 | #define K7_EVNTSEL_MASK \ |
| 602 | (K7_EVNTSEL_EVENT_MASK | \ |
| 603 | K7_EVNTSEL_UNIT_MASK | \ |
Peter Zijlstra | ff99be5 | 2009-05-25 17:39:03 +0200 | [diff] [blame] | 604 | K7_EVNTSEL_EDGE_MASK | \ |
| 605 | K7_EVNTSEL_INV_MASK | \ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 606 | K7_EVNTSEL_REG_MASK) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 607 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 608 | return hw_event & K7_EVNTSEL_MASK; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 609 | } |
| 610 | |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 611 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 612 | * Propagate event elapsed time into the generic event. |
| 613 | * Can only be executed on the CPU where the event is active. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 614 | * Returns the delta events processed. |
| 615 | */ |
Robert Richter | 4b7bfd0 | 2009-04-29 12:47:22 +0200 | [diff] [blame] | 616 | static u64 |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 617 | x86_perf_event_update(struct perf_event *event, |
| 618 | struct hw_perf_event *hwc, int idx) |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 619 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 620 | int shift = 64 - x86_pmu.event_bits; |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 621 | u64 prev_raw_count, new_raw_count; |
| 622 | s64 delta; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 623 | |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 624 | if (idx == X86_PMC_IDX_FIXED_BTS) |
| 625 | return 0; |
| 626 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 627 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 628 | * Careful: an NMI might modify the previous event value. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 629 | * |
| 630 | * Our tactic to handle this is to first atomically read and |
| 631 | * exchange a new raw count - then add that new-prev delta |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 632 | * count to the generic event atomically: |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 633 | */ |
| 634 | again: |
| 635 | prev_raw_count = atomic64_read(&hwc->prev_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 636 | rdmsrl(hwc->event_base + idx, new_raw_count); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 637 | |
| 638 | if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count, |
| 639 | new_raw_count) != prev_raw_count) |
| 640 | goto again; |
| 641 | |
| 642 | /* |
| 643 | * Now we have the new raw value and have updated the prev |
| 644 | * timestamp already. We can now calculate the elapsed delta |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 645 | * (event-)time and add that to the generic event. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 646 | * |
| 647 | * Careful, not all hw sign-extends above the physical width |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 648 | * of the count. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 649 | */ |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 650 | delta = (new_raw_count << shift) - (prev_raw_count << shift); |
| 651 | delta >>= shift; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 652 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 653 | atomic64_add(delta, &event->count); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 654 | atomic64_sub(delta, &hwc->period_left); |
Robert Richter | 4b7bfd0 | 2009-04-29 12:47:22 +0200 | [diff] [blame] | 655 | |
| 656 | return new_raw_count; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 657 | } |
| 658 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 659 | static atomic_t active_events; |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 660 | static DEFINE_MUTEX(pmc_reserve_mutex); |
| 661 | |
| 662 | static bool reserve_pmc_hardware(void) |
| 663 | { |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 664 | #ifdef CONFIG_X86_LOCAL_APIC |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 665 | int i; |
| 666 | |
| 667 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 668 | disable_lapic_nmi_watchdog(); |
| 669 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 670 | for (i = 0; i < x86_pmu.num_events; i++) { |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 671 | if (!reserve_perfctr_nmi(x86_pmu.perfctr + i)) |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 672 | goto perfctr_fail; |
| 673 | } |
| 674 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 675 | for (i = 0; i < x86_pmu.num_events; i++) { |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 676 | if (!reserve_evntsel_nmi(x86_pmu.eventsel + i)) |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 677 | goto eventsel_fail; |
| 678 | } |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 679 | #endif |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 680 | |
| 681 | return true; |
| 682 | |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 683 | #ifdef CONFIG_X86_LOCAL_APIC |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 684 | eventsel_fail: |
| 685 | for (i--; i >= 0; i--) |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 686 | release_evntsel_nmi(x86_pmu.eventsel + i); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 687 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 688 | i = x86_pmu.num_events; |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 689 | |
| 690 | perfctr_fail: |
| 691 | for (i--; i >= 0; i--) |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 692 | release_perfctr_nmi(x86_pmu.perfctr + i); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 693 | |
| 694 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 695 | enable_lapic_nmi_watchdog(); |
| 696 | |
| 697 | return false; |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 698 | #endif |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | static void release_pmc_hardware(void) |
| 702 | { |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 703 | #ifdef CONFIG_X86_LOCAL_APIC |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 704 | int i; |
| 705 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 706 | for (i = 0; i < x86_pmu.num_events; i++) { |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 707 | release_perfctr_nmi(x86_pmu.perfctr + i); |
| 708 | release_evntsel_nmi(x86_pmu.eventsel + i); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 712 | enable_lapic_nmi_watchdog(); |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 713 | #endif |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 714 | } |
| 715 | |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 716 | static inline bool bts_available(void) |
| 717 | { |
| 718 | return x86_pmu.enable_bts != NULL; |
| 719 | } |
| 720 | |
| 721 | static inline void init_debug_store_on_cpu(int cpu) |
| 722 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 723 | struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 724 | |
| 725 | if (!ds) |
| 726 | return; |
| 727 | |
| 728 | wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, |
markus.t.metzger@intel.com | 596da17 | 2009-09-02 16:04:47 +0200 | [diff] [blame] | 729 | (u32)((u64)(unsigned long)ds), |
| 730 | (u32)((u64)(unsigned long)ds >> 32)); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | static inline void fini_debug_store_on_cpu(int cpu) |
| 734 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 735 | if (!per_cpu(cpu_hw_events, cpu).ds) |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 736 | return; |
| 737 | |
| 738 | wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0); |
| 739 | } |
| 740 | |
| 741 | static void release_bts_hardware(void) |
| 742 | { |
| 743 | int cpu; |
| 744 | |
| 745 | if (!bts_available()) |
| 746 | return; |
| 747 | |
| 748 | get_online_cpus(); |
| 749 | |
| 750 | for_each_online_cpu(cpu) |
| 751 | fini_debug_store_on_cpu(cpu); |
| 752 | |
| 753 | for_each_possible_cpu(cpu) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 754 | struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 755 | |
| 756 | if (!ds) |
| 757 | continue; |
| 758 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 759 | per_cpu(cpu_hw_events, cpu).ds = NULL; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 760 | |
markus.t.metzger@intel.com | 596da17 | 2009-09-02 16:04:47 +0200 | [diff] [blame] | 761 | kfree((void *)(unsigned long)ds->bts_buffer_base); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 762 | kfree(ds); |
| 763 | } |
| 764 | |
| 765 | put_online_cpus(); |
| 766 | } |
| 767 | |
| 768 | static int reserve_bts_hardware(void) |
| 769 | { |
| 770 | int cpu, err = 0; |
| 771 | |
| 772 | if (!bts_available()) |
markus.t.metzger@intel.com | 747b50a | 2009-09-02 16:04:46 +0200 | [diff] [blame] | 773 | return 0; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 774 | |
| 775 | get_online_cpus(); |
| 776 | |
| 777 | for_each_possible_cpu(cpu) { |
| 778 | struct debug_store *ds; |
| 779 | void *buffer; |
| 780 | |
| 781 | err = -ENOMEM; |
| 782 | buffer = kzalloc(BTS_BUFFER_SIZE, GFP_KERNEL); |
| 783 | if (unlikely(!buffer)) |
| 784 | break; |
| 785 | |
| 786 | ds = kzalloc(sizeof(*ds), GFP_KERNEL); |
| 787 | if (unlikely(!ds)) { |
| 788 | kfree(buffer); |
| 789 | break; |
| 790 | } |
| 791 | |
markus.t.metzger@intel.com | 596da17 | 2009-09-02 16:04:47 +0200 | [diff] [blame] | 792 | ds->bts_buffer_base = (u64)(unsigned long)buffer; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 793 | ds->bts_index = ds->bts_buffer_base; |
| 794 | ds->bts_absolute_maximum = |
| 795 | ds->bts_buffer_base + BTS_BUFFER_SIZE; |
| 796 | ds->bts_interrupt_threshold = |
| 797 | ds->bts_absolute_maximum - BTS_OVFL_TH; |
| 798 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 799 | per_cpu(cpu_hw_events, cpu).ds = ds; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 800 | err = 0; |
| 801 | } |
| 802 | |
| 803 | if (err) |
| 804 | release_bts_hardware(); |
| 805 | else { |
| 806 | for_each_online_cpu(cpu) |
| 807 | init_debug_store_on_cpu(cpu); |
| 808 | } |
| 809 | |
| 810 | put_online_cpus(); |
| 811 | |
| 812 | return err; |
| 813 | } |
| 814 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 815 | static void hw_perf_event_destroy(struct perf_event *event) |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 816 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 817 | if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) { |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 818 | release_pmc_hardware(); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 819 | release_bts_hardware(); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 820 | mutex_unlock(&pmc_reserve_mutex); |
| 821 | } |
| 822 | } |
| 823 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 824 | static inline int x86_pmu_initialized(void) |
| 825 | { |
| 826 | return x86_pmu.handle_irq != NULL; |
| 827 | } |
| 828 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 829 | static inline int |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 830 | set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event_attr *attr) |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 831 | { |
| 832 | unsigned int cache_type, cache_op, cache_result; |
| 833 | u64 config, val; |
| 834 | |
| 835 | config = attr->config; |
| 836 | |
| 837 | cache_type = (config >> 0) & 0xff; |
| 838 | if (cache_type >= PERF_COUNT_HW_CACHE_MAX) |
| 839 | return -EINVAL; |
| 840 | |
| 841 | cache_op = (config >> 8) & 0xff; |
| 842 | if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX) |
| 843 | return -EINVAL; |
| 844 | |
| 845 | cache_result = (config >> 16) & 0xff; |
| 846 | if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX) |
| 847 | return -EINVAL; |
| 848 | |
| 849 | val = hw_cache_event_ids[cache_type][cache_op][cache_result]; |
| 850 | |
| 851 | if (val == 0) |
| 852 | return -ENOENT; |
| 853 | |
| 854 | if (val == -1) |
| 855 | return -EINVAL; |
| 856 | |
| 857 | hwc->config |= val; |
| 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 862 | static void intel_pmu_enable_bts(u64 config) |
| 863 | { |
| 864 | unsigned long debugctlmsr; |
| 865 | |
| 866 | debugctlmsr = get_debugctlmsr(); |
| 867 | |
| 868 | debugctlmsr |= X86_DEBUGCTL_TR; |
| 869 | debugctlmsr |= X86_DEBUGCTL_BTS; |
| 870 | debugctlmsr |= X86_DEBUGCTL_BTINT; |
| 871 | |
| 872 | if (!(config & ARCH_PERFMON_EVENTSEL_OS)) |
| 873 | debugctlmsr |= X86_DEBUGCTL_BTS_OFF_OS; |
| 874 | |
| 875 | if (!(config & ARCH_PERFMON_EVENTSEL_USR)) |
| 876 | debugctlmsr |= X86_DEBUGCTL_BTS_OFF_USR; |
| 877 | |
| 878 | update_debugctlmsr(debugctlmsr); |
| 879 | } |
| 880 | |
| 881 | static void intel_pmu_disable_bts(void) |
| 882 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 883 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 884 | unsigned long debugctlmsr; |
| 885 | |
| 886 | if (!cpuc->ds) |
| 887 | return; |
| 888 | |
| 889 | debugctlmsr = get_debugctlmsr(); |
| 890 | |
| 891 | debugctlmsr &= |
| 892 | ~(X86_DEBUGCTL_TR | X86_DEBUGCTL_BTS | X86_DEBUGCTL_BTINT | |
| 893 | X86_DEBUGCTL_BTS_OFF_OS | X86_DEBUGCTL_BTS_OFF_USR); |
| 894 | |
| 895 | update_debugctlmsr(debugctlmsr); |
| 896 | } |
| 897 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 898 | /* |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame] | 899 | * Setup the hardware configuration for a given attr_type |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 900 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 901 | static int __hw_perf_event_init(struct perf_event *event) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 902 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 903 | struct perf_event_attr *attr = &event->attr; |
| 904 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 905 | u64 config; |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 906 | int err; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 907 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 908 | if (!x86_pmu_initialized()) |
| 909 | return -ENODEV; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 910 | |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 911 | err = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 912 | if (!atomic_inc_not_zero(&active_events)) { |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 913 | mutex_lock(&pmc_reserve_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 914 | if (atomic_read(&active_events) == 0) { |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 915 | if (!reserve_pmc_hardware()) |
| 916 | err = -EBUSY; |
| 917 | else |
markus.t.metzger@intel.com | 747b50a | 2009-09-02 16:04:46 +0200 | [diff] [blame] | 918 | err = reserve_bts_hardware(); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 919 | } |
| 920 | if (!err) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 921 | atomic_inc(&active_events); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 922 | mutex_unlock(&pmc_reserve_mutex); |
| 923 | } |
| 924 | if (err) |
| 925 | return err; |
| 926 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 927 | event->destroy = hw_perf_event_destroy; |
Peter Zijlstra | a1792cdac | 2009-09-09 10:04:47 +0200 | [diff] [blame] | 928 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 929 | /* |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 930 | * Generate PMC IRQs: |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 931 | * (keep 'enabled' bit clear for now) |
| 932 | */ |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 933 | hwc->config = ARCH_PERFMON_EVENTSEL_INT; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 934 | |
| 935 | /* |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 936 | * Count user and OS events unless requested not to. |
| 937 | */ |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame] | 938 | if (!attr->exclude_user) |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 939 | hwc->config |= ARCH_PERFMON_EVENTSEL_USR; |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame] | 940 | if (!attr->exclude_kernel) |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 941 | hwc->config |= ARCH_PERFMON_EVENTSEL_OS; |
| 942 | |
Peter Zijlstra | bd2b5b1 | 2009-06-10 13:40:57 +0200 | [diff] [blame] | 943 | if (!hwc->sample_period) { |
Peter Zijlstra | b23f332 | 2009-06-02 15:13:03 +0200 | [diff] [blame] | 944 | hwc->sample_period = x86_pmu.max_period; |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 945 | hwc->last_period = hwc->sample_period; |
Peter Zijlstra | bd2b5b1 | 2009-06-10 13:40:57 +0200 | [diff] [blame] | 946 | atomic64_set(&hwc->period_left, hwc->sample_period); |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 947 | } else { |
| 948 | /* |
| 949 | * If we have a PMU initialized but no APIC |
| 950 | * interrupts, we cannot sample hardware |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 951 | * events (user-space has to fall back and |
| 952 | * sample via a hrtimer based software event): |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 953 | */ |
| 954 | if (!x86_pmu.apic) |
| 955 | return -EOPNOTSUPP; |
Peter Zijlstra | bd2b5b1 | 2009-06-10 13:40:57 +0200 | [diff] [blame] | 956 | } |
Ingo Molnar | d2517a4 | 2009-05-17 10:04:45 +0200 | [diff] [blame] | 957 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 958 | /* |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 959 | * Raw hw_event type provide the config in the hw_event structure |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 960 | */ |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 961 | if (attr->type == PERF_TYPE_RAW) { |
| 962 | hwc->config |= x86_pmu.raw_event(attr->config); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 963 | return 0; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 964 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 965 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 966 | if (attr->type == PERF_TYPE_HW_CACHE) |
| 967 | return set_ext_hw_attr(hwc, attr); |
| 968 | |
| 969 | if (attr->config >= x86_pmu.max_events) |
| 970 | return -EINVAL; |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 971 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 972 | /* |
| 973 | * The generic map: |
| 974 | */ |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 975 | config = x86_pmu.event_map(attr->config); |
| 976 | |
| 977 | if (config == 0) |
| 978 | return -ENOENT; |
| 979 | |
| 980 | if (config == -1LL) |
| 981 | return -EINVAL; |
| 982 | |
markus.t.metzger@intel.com | 747b50a | 2009-09-02 16:04:46 +0200 | [diff] [blame] | 983 | /* |
| 984 | * Branch tracing: |
| 985 | */ |
| 986 | if ((attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS) && |
markus.t.metzger@intel.com | 1653192 | 2009-09-02 16:04:48 +0200 | [diff] [blame] | 987 | (hwc->sample_period == 1)) { |
| 988 | /* BTS is not supported by this architecture. */ |
| 989 | if (!bts_available()) |
| 990 | return -EOPNOTSUPP; |
| 991 | |
| 992 | /* BTS is currently only allowed for user-mode. */ |
| 993 | if (hwc->config & ARCH_PERFMON_EVENTSEL_OS) |
| 994 | return -EOPNOTSUPP; |
| 995 | } |
markus.t.metzger@intel.com | 747b50a | 2009-09-02 16:04:46 +0200 | [diff] [blame] | 996 | |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 997 | hwc->config |= config; |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 998 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 999 | return 0; |
| 1000 | } |
| 1001 | |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1002 | static void p6_pmu_disable_all(void) |
| 1003 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1004 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 1005 | u64 val; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1006 | |
| 1007 | if (!cpuc->enabled) |
| 1008 | return; |
| 1009 | |
| 1010 | cpuc->enabled = 0; |
| 1011 | barrier(); |
| 1012 | |
| 1013 | /* p6 only has one enable register */ |
| 1014 | rdmsrl(MSR_P6_EVNTSEL0, val); |
| 1015 | val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE; |
| 1016 | wrmsrl(MSR_P6_EVNTSEL0, val); |
| 1017 | } |
| 1018 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1019 | static void intel_pmu_disable_all(void) |
Thomas Gleixner | 4ac1329 | 2008-12-09 21:43:39 +0100 | [diff] [blame] | 1020 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1021 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1022 | |
| 1023 | if (!cpuc->enabled) |
| 1024 | return; |
| 1025 | |
| 1026 | cpuc->enabled = 0; |
| 1027 | barrier(); |
| 1028 | |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 1029 | wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1030 | |
| 1031 | if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask)) |
| 1032 | intel_pmu_disable_bts(); |
Thomas Gleixner | 4ac1329 | 2008-12-09 21:43:39 +0100 | [diff] [blame] | 1033 | } |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1034 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1035 | static void amd_pmu_disable_all(void) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1036 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1037 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1038 | int idx; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1039 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1040 | if (!cpuc->enabled) |
| 1041 | return; |
| 1042 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1043 | cpuc->enabled = 0; |
Peter Zijlstra | 60b3df9 | 2009-03-13 12:21:30 +0100 | [diff] [blame] | 1044 | /* |
| 1045 | * ensure we write the disable before we start disabling the |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1046 | * events proper, so that amd_pmu_enable_event() does the |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 1047 | * right thing. |
Peter Zijlstra | 60b3df9 | 2009-03-13 12:21:30 +0100 | [diff] [blame] | 1048 | */ |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1049 | barrier(); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1050 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1051 | for (idx = 0; idx < x86_pmu.num_events; idx++) { |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1052 | u64 val; |
| 1053 | |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 1054 | if (!test_bit(idx, cpuc->active_mask)) |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 1055 | continue; |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1056 | rdmsrl(MSR_K7_EVNTSEL0 + idx, val); |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 1057 | if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE)) |
| 1058 | continue; |
| 1059 | val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE; |
| 1060 | wrmsrl(MSR_K7_EVNTSEL0 + idx, val); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1061 | } |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1062 | } |
| 1063 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1064 | void hw_perf_disable(void) |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1065 | { |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 1066 | if (!x86_pmu_initialized()) |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1067 | return; |
| 1068 | return x86_pmu.disable_all(); |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1069 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1070 | |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1071 | static void p6_pmu_enable_all(void) |
| 1072 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1073 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1074 | unsigned long val; |
| 1075 | |
| 1076 | if (cpuc->enabled) |
| 1077 | return; |
| 1078 | |
| 1079 | cpuc->enabled = 1; |
| 1080 | barrier(); |
| 1081 | |
| 1082 | /* p6 only has one enable register */ |
| 1083 | rdmsrl(MSR_P6_EVNTSEL0, val); |
| 1084 | val |= ARCH_PERFMON_EVENTSEL0_ENABLE; |
| 1085 | wrmsrl(MSR_P6_EVNTSEL0, val); |
| 1086 | } |
| 1087 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1088 | static void intel_pmu_enable_all(void) |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1089 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1090 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1091 | |
| 1092 | if (cpuc->enabled) |
| 1093 | return; |
| 1094 | |
| 1095 | cpuc->enabled = 1; |
| 1096 | barrier(); |
| 1097 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1098 | wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1099 | |
| 1100 | if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1101 | struct perf_event *event = |
| 1102 | cpuc->events[X86_PMC_IDX_FIXED_BTS]; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1103 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1104 | if (WARN_ON_ONCE(!event)) |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1105 | return; |
| 1106 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1107 | intel_pmu_enable_bts(event->hw.config); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1108 | } |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1109 | } |
| 1110 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1111 | static void amd_pmu_enable_all(void) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1112 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1113 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1114 | int idx; |
| 1115 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1116 | if (cpuc->enabled) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1117 | return; |
| 1118 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1119 | cpuc->enabled = 1; |
| 1120 | barrier(); |
| 1121 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1122 | for (idx = 0; idx < x86_pmu.num_events; idx++) { |
| 1123 | struct perf_event *event = cpuc->events[idx]; |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 1124 | u64 val; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1125 | |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 1126 | if (!test_bit(idx, cpuc->active_mask)) |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 1127 | continue; |
Peter Zijlstra | 984b838 | 2009-07-10 09:59:56 +0200 | [diff] [blame] | 1128 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1129 | val = event->hw.config; |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 1130 | val |= ARCH_PERFMON_EVENTSEL0_ENABLE; |
| 1131 | wrmsrl(MSR_K7_EVNTSEL0 + idx, val); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1135 | void hw_perf_enable(void) |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1136 | { |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 1137 | if (!x86_pmu_initialized()) |
Ingo Molnar | 2b9ff0d | 2008-12-14 18:36:30 +0100 | [diff] [blame] | 1138 | return; |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1139 | x86_pmu.enable_all(); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1140 | } |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1141 | |
Robert Richter | 19d84da | 2009-04-29 12:47:25 +0200 | [diff] [blame] | 1142 | static inline u64 intel_pmu_get_status(void) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1143 | { |
| 1144 | u64 status; |
| 1145 | |
| 1146 | rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status); |
| 1147 | |
| 1148 | return status; |
| 1149 | } |
| 1150 | |
Robert Richter | dee5d90 | 2009-04-29 12:47:07 +0200 | [diff] [blame] | 1151 | static inline void intel_pmu_ack_status(u64 ack) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1152 | { |
| 1153 | wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack); |
| 1154 | } |
| 1155 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1156 | static inline void x86_pmu_enable_event(struct hw_perf_event *hwc, int idx) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1157 | { |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1158 | (void)checking_wrmsrl(hwc->config_base + idx, |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 1159 | hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE); |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1160 | } |
| 1161 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1162 | static inline void x86_pmu_disable_event(struct hw_perf_event *hwc, int idx) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1163 | { |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1164 | (void)checking_wrmsrl(hwc->config_base + idx, hwc->config); |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1165 | } |
| 1166 | |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 1167 | static inline void |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1168 | intel_pmu_disable_fixed(struct hw_perf_event *hwc, int __idx) |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1169 | { |
| 1170 | int idx = __idx - X86_PMC_IDX_FIXED; |
| 1171 | u64 ctrl_val, mask; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1172 | |
| 1173 | mask = 0xfULL << (idx * 4); |
| 1174 | |
| 1175 | rdmsrl(hwc->config_base, ctrl_val); |
| 1176 | ctrl_val &= ~mask; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1177 | (void)checking_wrmsrl(hwc->config_base, ctrl_val); |
| 1178 | } |
| 1179 | |
| 1180 | static inline void |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1181 | p6_pmu_disable_event(struct hw_perf_event *hwc, int idx) |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1182 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1183 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 1184 | u64 val = P6_NOP_EVENT; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1185 | |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 1186 | if (cpuc->enabled) |
| 1187 | val |= ARCH_PERFMON_EVENTSEL0_ENABLE; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1188 | |
| 1189 | (void)checking_wrmsrl(hwc->config_base + idx, val); |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | static inline void |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1193 | intel_pmu_disable_event(struct hw_perf_event *hwc, int idx) |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 1194 | { |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1195 | if (unlikely(idx == X86_PMC_IDX_FIXED_BTS)) { |
| 1196 | intel_pmu_disable_bts(); |
| 1197 | return; |
| 1198 | } |
| 1199 | |
Robert Richter | d436989 | 2009-04-29 12:47:19 +0200 | [diff] [blame] | 1200 | if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) { |
| 1201 | intel_pmu_disable_fixed(hwc, idx); |
| 1202 | return; |
| 1203 | } |
| 1204 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1205 | x86_pmu_disable_event(hwc, idx); |
Robert Richter | d436989 | 2009-04-29 12:47:19 +0200 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | static inline void |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1209 | amd_pmu_disable_event(struct hw_perf_event *hwc, int idx) |
Robert Richter | d436989 | 2009-04-29 12:47:19 +0200 | [diff] [blame] | 1210 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1211 | x86_pmu_disable_event(hwc, idx); |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 1212 | } |
| 1213 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 1214 | static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1215 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1216 | /* |
| 1217 | * Set the next IRQ period, based on the hwc->period_left value. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1218 | * To be called with the event disabled in hw: |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1219 | */ |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1220 | static int |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1221 | x86_perf_event_set_period(struct perf_event *event, |
| 1222 | struct hw_perf_event *hwc, int idx) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1223 | { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1224 | s64 left = atomic64_read(&hwc->period_left); |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1225 | s64 period = hwc->sample_period; |
| 1226 | int err, ret = 0; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1227 | |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1228 | if (idx == X86_PMC_IDX_FIXED_BTS) |
| 1229 | return 0; |
| 1230 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1231 | /* |
| 1232 | * If we are way outside a reasoable range then just skip forward: |
| 1233 | */ |
| 1234 | if (unlikely(left <= -period)) { |
| 1235 | left = period; |
| 1236 | atomic64_set(&hwc->period_left, left); |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1237 | hwc->last_period = period; |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1238 | ret = 1; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | if (unlikely(left <= 0)) { |
| 1242 | left += period; |
| 1243 | atomic64_set(&hwc->period_left, left); |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1244 | hwc->last_period = period; |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1245 | ret = 1; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1246 | } |
Ingo Molnar | 1c80f4b | 2009-05-15 08:25:22 +0200 | [diff] [blame] | 1247 | /* |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 1248 | * 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] | 1249 | */ |
| 1250 | if (unlikely(left < 2)) |
| 1251 | left = 2; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1252 | |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1253 | if (left > x86_pmu.max_period) |
| 1254 | left = x86_pmu.max_period; |
| 1255 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 1256 | per_cpu(pmc_prev_left[idx], smp_processor_id()) = left; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1257 | |
| 1258 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1259 | * The hw event starts counting from this event offset, |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1260 | * mark it to be able to extra future deltas: |
| 1261 | */ |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1262 | atomic64_set(&hwc->prev_count, (u64)-left); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1263 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1264 | err = checking_wrmsrl(hwc->event_base + idx, |
| 1265 | (u64)(-left) & x86_pmu.event_mask); |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1266 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1267 | perf_event_update_userpage(event); |
Peter Zijlstra | 194002b | 2009-06-22 16:35:24 +0200 | [diff] [blame] | 1268 | |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1269 | return ret; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | static inline void |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1273 | intel_pmu_enable_fixed(struct hw_perf_event *hwc, int __idx) |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1274 | { |
| 1275 | int idx = __idx - X86_PMC_IDX_FIXED; |
| 1276 | u64 ctrl_val, bits, mask; |
| 1277 | int err; |
| 1278 | |
| 1279 | /* |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 1280 | * Enable IRQ generation (0x8), |
| 1281 | * and enable ring-3 counting (0x2) and ring-0 counting (0x1) |
| 1282 | * if requested: |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1283 | */ |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 1284 | bits = 0x8ULL; |
| 1285 | if (hwc->config & ARCH_PERFMON_EVENTSEL_USR) |
| 1286 | bits |= 0x2; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1287 | if (hwc->config & ARCH_PERFMON_EVENTSEL_OS) |
| 1288 | bits |= 0x1; |
| 1289 | bits <<= (idx * 4); |
| 1290 | mask = 0xfULL << (idx * 4); |
| 1291 | |
| 1292 | rdmsrl(hwc->config_base, ctrl_val); |
| 1293 | ctrl_val &= ~mask; |
| 1294 | ctrl_val |= bits; |
| 1295 | err = checking_wrmsrl(hwc->config_base, ctrl_val); |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 1296 | } |
| 1297 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1298 | static void p6_pmu_enable_event(struct hw_perf_event *hwc, int idx) |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1299 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1300 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Peter Zijlstra | 984b838 | 2009-07-10 09:59:56 +0200 | [diff] [blame] | 1301 | u64 val; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1302 | |
Peter Zijlstra | 984b838 | 2009-07-10 09:59:56 +0200 | [diff] [blame] | 1303 | val = hwc->config; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1304 | if (cpuc->enabled) |
Peter Zijlstra | 984b838 | 2009-07-10 09:59:56 +0200 | [diff] [blame] | 1305 | val |= ARCH_PERFMON_EVENTSEL0_ENABLE; |
| 1306 | |
| 1307 | (void)checking_wrmsrl(hwc->config_base + idx, val); |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1311 | static void intel_pmu_enable_event(struct hw_perf_event *hwc, int idx) |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 1312 | { |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1313 | if (unlikely(idx == X86_PMC_IDX_FIXED_BTS)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1314 | if (!__get_cpu_var(cpu_hw_events).enabled) |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1315 | return; |
| 1316 | |
| 1317 | intel_pmu_enable_bts(hwc->config); |
| 1318 | return; |
| 1319 | } |
| 1320 | |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 1321 | if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) { |
| 1322 | intel_pmu_enable_fixed(hwc, idx); |
| 1323 | return; |
| 1324 | } |
| 1325 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1326 | x86_pmu_enable_event(hwc, idx); |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 1327 | } |
| 1328 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1329 | static void amd_pmu_enable_event(struct hw_perf_event *hwc, int idx) |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 1330 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1331 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 1332 | |
| 1333 | if (cpuc->enabled) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1334 | x86_pmu_enable_event(hwc, idx); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1335 | } |
| 1336 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1337 | static int |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1338 | fixed_mode_idx(struct perf_event *event, struct hw_perf_event *hwc) |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 1339 | { |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 1340 | unsigned int hw_event; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1341 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 1342 | hw_event = hwc->config & ARCH_PERFMON_EVENT_MASK; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1343 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 1344 | if (unlikely((hw_event == |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1345 | x86_pmu.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS)) && |
| 1346 | (hwc->sample_period == 1))) |
| 1347 | return X86_PMC_IDX_FIXED_BTS; |
| 1348 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1349 | if (!x86_pmu.num_events_fixed) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1350 | return -1; |
| 1351 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 1352 | if (unlikely(hw_event == x86_pmu.event_map(PERF_COUNT_HW_INSTRUCTIONS))) |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1353 | return X86_PMC_IDX_FIXED_INSTRUCTIONS; |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 1354 | if (unlikely(hw_event == x86_pmu.event_map(PERF_COUNT_HW_CPU_CYCLES))) |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1355 | return X86_PMC_IDX_FIXED_CPU_CYCLES; |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 1356 | if (unlikely(hw_event == x86_pmu.event_map(PERF_COUNT_HW_BUS_CYCLES))) |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1357 | return X86_PMC_IDX_FIXED_BUS_CYCLES; |
| 1358 | |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 1359 | return -1; |
| 1360 | } |
| 1361 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1362 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1363 | * Find a PMC slot for the freshly enabled / scheduled in event: |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1364 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1365 | static int x86_pmu_enable(struct perf_event *event) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1366 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1367 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 1368 | struct hw_perf_event *hwc = &event->hw; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1369 | int idx; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1370 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1371 | idx = fixed_mode_idx(event, hwc); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1372 | if (idx == X86_PMC_IDX_FIXED_BTS) { |
markus.t.metzger@intel.com | 747b50a | 2009-09-02 16:04:46 +0200 | [diff] [blame] | 1373 | /* BTS is already occupied. */ |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1374 | if (test_and_set_bit(idx, cpuc->used_mask)) |
markus.t.metzger@intel.com | 747b50a | 2009-09-02 16:04:46 +0200 | [diff] [blame] | 1375 | return -EAGAIN; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1376 | |
| 1377 | hwc->config_base = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1378 | hwc->event_base = 0; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1379 | hwc->idx = idx; |
| 1380 | } else if (idx >= 0) { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1381 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1382 | * Try to get the fixed event, if that is already taken |
| 1383 | * then try to get a generic event: |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1384 | */ |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 1385 | if (test_and_set_bit(idx, cpuc->used_mask)) |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1386 | goto try_generic; |
Ingo Molnar | 0dff86a | 2008-12-23 12:28:12 +0100 | [diff] [blame] | 1387 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1388 | hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL; |
| 1389 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1390 | * We set it so that event_base + idx in wrmsr/rdmsr maps to |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1391 | * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2: |
| 1392 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1393 | hwc->event_base = |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1394 | MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1395 | hwc->idx = idx; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1396 | } else { |
| 1397 | idx = hwc->idx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1398 | /* Try to get the previous generic event again */ |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 1399 | if (test_and_set_bit(idx, cpuc->used_mask)) { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1400 | try_generic: |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 1401 | idx = find_first_zero_bit(cpuc->used_mask, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1402 | x86_pmu.num_events); |
| 1403 | if (idx == x86_pmu.num_events) |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1404 | return -EAGAIN; |
| 1405 | |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 1406 | set_bit(idx, cpuc->used_mask); |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1407 | hwc->idx = idx; |
| 1408 | } |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 1409 | hwc->config_base = x86_pmu.eventsel; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1410 | hwc->event_base = x86_pmu.perfctr; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1411 | } |
| 1412 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1413 | perf_events_lapic_init(); |
Ingo Molnar | 53b441a | 2009-05-25 21:41:28 +0200 | [diff] [blame] | 1414 | |
Robert Richter | d436989 | 2009-04-29 12:47:19 +0200 | [diff] [blame] | 1415 | x86_pmu.disable(hwc, idx); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1416 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1417 | cpuc->events[idx] = event; |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 1418 | set_bit(idx, cpuc->active_mask); |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 1419 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1420 | x86_perf_event_set_period(event, hwc, idx); |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 1421 | x86_pmu.enable(hwc, idx); |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 1422 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1423 | perf_event_update_userpage(event); |
Peter Zijlstra | 194002b | 2009-06-22 16:35:24 +0200 | [diff] [blame] | 1424 | |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 1425 | return 0; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1426 | } |
| 1427 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1428 | static void x86_pmu_unthrottle(struct perf_event *event) |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 1429 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1430 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 1431 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 1432 | |
| 1433 | if (WARN_ON_ONCE(hwc->idx >= X86_PMC_IDX_MAX || |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1434 | cpuc->events[hwc->idx] != event)) |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 1435 | return; |
| 1436 | |
| 1437 | x86_pmu.enable(hwc, hwc->idx); |
| 1438 | } |
| 1439 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1440 | void perf_event_print_debug(void) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1441 | { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1442 | u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1443 | struct cpu_hw_events *cpuc; |
Peter Zijlstra | 5bb9efe | 2009-05-13 08:12:51 +0200 | [diff] [blame] | 1444 | unsigned long flags; |
Ingo Molnar | 1e12567 | 2008-12-09 12:18:18 +0100 | [diff] [blame] | 1445 | int cpu, idx; |
| 1446 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1447 | if (!x86_pmu.num_events) |
Ingo Molnar | 1e12567 | 2008-12-09 12:18:18 +0100 | [diff] [blame] | 1448 | return; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1449 | |
Peter Zijlstra | 5bb9efe | 2009-05-13 08:12:51 +0200 | [diff] [blame] | 1450 | local_irq_save(flags); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1451 | |
| 1452 | cpu = smp_processor_id(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1453 | cpuc = &per_cpu(cpu_hw_events, cpu); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1454 | |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 1455 | if (x86_pmu.version >= 2) { |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1456 | rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl); |
| 1457 | rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status); |
| 1458 | rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow); |
| 1459 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1460 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1461 | pr_info("\n"); |
| 1462 | pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl); |
| 1463 | pr_info("CPU#%d: status: %016llx\n", cpu, status); |
| 1464 | pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow); |
| 1465 | pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1466 | } |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 1467 | pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used_mask); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1468 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1469 | for (idx = 0; idx < x86_pmu.num_events; idx++) { |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 1470 | rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl); |
| 1471 | rdmsrl(x86_pmu.perfctr + idx, pmc_count); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1472 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 1473 | prev_left = per_cpu(pmc_prev_left[idx], cpu); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1474 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1475 | pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n", |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1476 | cpu, idx, pmc_ctrl); |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1477 | pr_info("CPU#%d: gen-PMC%d count: %016llx\n", |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1478 | cpu, idx, pmc_count); |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1479 | pr_info("CPU#%d: gen-PMC%d left: %016llx\n", |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1480 | cpu, idx, prev_left); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1481 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1482 | for (idx = 0; idx < x86_pmu.num_events_fixed; idx++) { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1483 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count); |
| 1484 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1485 | pr_info("CPU#%d: fixed-PMC%d count: %016llx\n", |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1486 | cpu, idx, pmc_count); |
| 1487 | } |
Peter Zijlstra | 5bb9efe | 2009-05-13 08:12:51 +0200 | [diff] [blame] | 1488 | local_irq_restore(flags); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1489 | } |
| 1490 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1491 | static void intel_pmu_drain_bts_buffer(struct cpu_hw_events *cpuc) |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1492 | { |
| 1493 | struct debug_store *ds = cpuc->ds; |
| 1494 | struct bts_record { |
| 1495 | u64 from; |
| 1496 | u64 to; |
| 1497 | u64 flags; |
| 1498 | }; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1499 | struct perf_event *event = cpuc->events[X86_PMC_IDX_FIXED_BTS]; |
markus.t.metzger@intel.com | 596da17 | 2009-09-02 16:04:47 +0200 | [diff] [blame] | 1500 | struct bts_record *at, *top; |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 1501 | struct perf_output_handle handle; |
| 1502 | struct perf_event_header header; |
| 1503 | struct perf_sample_data data; |
| 1504 | struct pt_regs regs; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1505 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1506 | if (!event) |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1507 | return; |
| 1508 | |
| 1509 | if (!ds) |
| 1510 | return; |
| 1511 | |
markus.t.metzger@intel.com | 596da17 | 2009-09-02 16:04:47 +0200 | [diff] [blame] | 1512 | at = (struct bts_record *)(unsigned long)ds->bts_buffer_base; |
| 1513 | top = (struct bts_record *)(unsigned long)ds->bts_index; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1514 | |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 1515 | if (top <= at) |
| 1516 | return; |
| 1517 | |
markus.t.metzger@intel.com | 596da17 | 2009-09-02 16:04:47 +0200 | [diff] [blame] | 1518 | ds->bts_index = ds->bts_buffer_base; |
| 1519 | |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1520 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1521 | data.period = event->hw.last_period; |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 1522 | data.addr = 0; |
| 1523 | regs.ip = 0; |
| 1524 | |
| 1525 | /* |
| 1526 | * Prepare a generic sample, i.e. fill in the invariant fields. |
| 1527 | * We will overwrite the from and to address before we output |
| 1528 | * the sample. |
| 1529 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1530 | perf_prepare_sample(&header, &data, event, ®s); |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 1531 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1532 | if (perf_output_begin(&handle, event, |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 1533 | header.size * (top - at), 1, 1)) |
| 1534 | return; |
| 1535 | |
| 1536 | for (; at < top; at++) { |
| 1537 | data.ip = at->from; |
| 1538 | data.addr = at->to; |
| 1539 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1540 | perf_output_sample(&handle, &header, &data, event); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1541 | } |
| 1542 | |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 1543 | perf_output_end(&handle); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1544 | |
| 1545 | /* There's new data available. */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1546 | event->hw.interrupts++; |
| 1547 | event->pending_kill = POLL_IN; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1548 | } |
| 1549 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1550 | static void x86_pmu_disable(struct perf_event *event) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1551 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1552 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 1553 | struct hw_perf_event *hwc = &event->hw; |
Robert Richter | 6f00cad | 2009-04-29 12:47:17 +0200 | [diff] [blame] | 1554 | int idx = hwc->idx; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1555 | |
Robert Richter | 0953423 | 2009-04-29 12:47:16 +0200 | [diff] [blame] | 1556 | /* |
| 1557 | * Must be done before we disable, otherwise the nmi handler |
| 1558 | * could reenable again: |
| 1559 | */ |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 1560 | clear_bit(idx, cpuc->active_mask); |
Robert Richter | d436989 | 2009-04-29 12:47:19 +0200 | [diff] [blame] | 1561 | x86_pmu.disable(hwc, idx); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1562 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1563 | /* |
| 1564 | * Make sure the cleared pointer becomes visible before we |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1565 | * (potentially) free the event: |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1566 | */ |
Robert Richter | 527e26a | 2009-04-29 12:47:02 +0200 | [diff] [blame] | 1567 | barrier(); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1568 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1569 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1570 | * Drain the remaining delta count out of a event |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1571 | * that we are disabling: |
| 1572 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1573 | x86_perf_event_update(event, hwc, idx); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1574 | |
| 1575 | /* Drain the remaining BTS records. */ |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 1576 | if (unlikely(idx == X86_PMC_IDX_FIXED_BTS)) |
| 1577 | intel_pmu_drain_bts_buffer(cpuc); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1578 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1579 | cpuc->events[idx] = NULL; |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 1580 | clear_bit(idx, cpuc->used_mask); |
Peter Zijlstra | 194002b | 2009-06-22 16:35:24 +0200 | [diff] [blame] | 1581 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1582 | perf_event_update_userpage(event); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1583 | } |
| 1584 | |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 1585 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1586 | * Save and restart an expired event. Called by NMI contexts, |
| 1587 | * so it has to be careful about preempting normal event ops: |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 1588 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1589 | static int intel_pmu_save_and_restart(struct perf_event *event) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1590 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1591 | struct hw_perf_event *hwc = &event->hw; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1592 | int idx = hwc->idx; |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1593 | int ret; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1594 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1595 | x86_perf_event_update(event, hwc, idx); |
| 1596 | ret = x86_perf_event_set_period(event, hwc, idx); |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 1597 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1598 | if (event->state == PERF_EVENT_STATE_ACTIVE) |
| 1599 | intel_pmu_enable_event(hwc, idx); |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1600 | |
| 1601 | return ret; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1602 | } |
| 1603 | |
Ingo Molnar | aaba980 | 2009-05-26 08:10:00 +0200 | [diff] [blame] | 1604 | static void intel_pmu_reset(void) |
| 1605 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1606 | struct debug_store *ds = __get_cpu_var(cpu_hw_events).ds; |
Ingo Molnar | aaba980 | 2009-05-26 08:10:00 +0200 | [diff] [blame] | 1607 | unsigned long flags; |
| 1608 | int idx; |
| 1609 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1610 | if (!x86_pmu.num_events) |
Ingo Molnar | aaba980 | 2009-05-26 08:10:00 +0200 | [diff] [blame] | 1611 | return; |
| 1612 | |
| 1613 | local_irq_save(flags); |
| 1614 | |
| 1615 | printk("clearing PMU state on CPU#%d\n", smp_processor_id()); |
| 1616 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1617 | for (idx = 0; idx < x86_pmu.num_events; idx++) { |
Ingo Molnar | aaba980 | 2009-05-26 08:10:00 +0200 | [diff] [blame] | 1618 | checking_wrmsrl(x86_pmu.eventsel + idx, 0ull); |
| 1619 | checking_wrmsrl(x86_pmu.perfctr + idx, 0ull); |
| 1620 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1621 | for (idx = 0; idx < x86_pmu.num_events_fixed; idx++) { |
Ingo Molnar | aaba980 | 2009-05-26 08:10:00 +0200 | [diff] [blame] | 1622 | checking_wrmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull); |
| 1623 | } |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1624 | if (ds) |
| 1625 | ds->bts_index = ds->bts_buffer_base; |
Ingo Molnar | aaba980 | 2009-05-26 08:10:00 +0200 | [diff] [blame] | 1626 | |
| 1627 | local_irq_restore(flags); |
| 1628 | } |
| 1629 | |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1630 | static int p6_pmu_handle_irq(struct pt_regs *regs) |
| 1631 | { |
| 1632 | struct perf_sample_data data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1633 | struct cpu_hw_events *cpuc; |
| 1634 | struct perf_event *event; |
| 1635 | struct hw_perf_event *hwc; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1636 | int idx, handled = 0; |
| 1637 | u64 val; |
| 1638 | |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1639 | data.addr = 0; |
| 1640 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1641 | cpuc = &__get_cpu_var(cpu_hw_events); |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1642 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1643 | for (idx = 0; idx < x86_pmu.num_events; idx++) { |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1644 | if (!test_bit(idx, cpuc->active_mask)) |
| 1645 | continue; |
| 1646 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1647 | event = cpuc->events[idx]; |
| 1648 | hwc = &event->hw; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1649 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1650 | val = x86_perf_event_update(event, hwc, idx); |
| 1651 | if (val & (1ULL << (x86_pmu.event_bits - 1))) |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1652 | continue; |
| 1653 | |
| 1654 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1655 | * event overflow |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1656 | */ |
| 1657 | handled = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1658 | data.period = event->hw.last_period; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1659 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1660 | if (!x86_perf_event_set_period(event, hwc, idx)) |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1661 | continue; |
| 1662 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1663 | if (perf_event_overflow(event, 1, &data, regs)) |
| 1664 | p6_pmu_disable_event(hwc, idx); |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | if (handled) |
| 1668 | inc_irq_stat(apic_perf_irqs); |
| 1669 | |
| 1670 | return handled; |
| 1671 | } |
Ingo Molnar | aaba980 | 2009-05-26 08:10:00 +0200 | [diff] [blame] | 1672 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1673 | /* |
| 1674 | * This handler is triggered by the local APIC, so the APIC IRQ handling |
| 1675 | * rules apply: |
| 1676 | */ |
Yong Wang | a328810 | 2009-06-03 13:12:55 +0800 | [diff] [blame] | 1677 | static int intel_pmu_handle_irq(struct pt_regs *regs) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1678 | { |
Peter Zijlstra | df1a132 | 2009-06-10 21:02:22 +0200 | [diff] [blame] | 1679 | struct perf_sample_data data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1680 | struct cpu_hw_events *cpuc; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1681 | int bit, loops; |
Mike Galbraith | 4b39fd9 | 2009-01-23 14:36:16 +0100 | [diff] [blame] | 1682 | u64 ack, status; |
Ingo Molnar | 9029a5e | 2009-05-15 08:26:20 +0200 | [diff] [blame] | 1683 | |
Peter Zijlstra | df1a132 | 2009-06-10 21:02:22 +0200 | [diff] [blame] | 1684 | data.addr = 0; |
| 1685 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1686 | cpuc = &__get_cpu_var(cpu_hw_events); |
Ingo Molnar | 43874d2 | 2008-12-09 12:23:59 +0100 | [diff] [blame] | 1687 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1688 | perf_disable(); |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 1689 | intel_pmu_drain_bts_buffer(cpuc); |
Robert Richter | 19d84da | 2009-04-29 12:47:25 +0200 | [diff] [blame] | 1690 | status = intel_pmu_get_status(); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1691 | if (!status) { |
| 1692 | perf_enable(); |
| 1693 | return 0; |
| 1694 | } |
Ingo Molnar | 87b9cf4 | 2008-12-08 14:20:16 +0100 | [diff] [blame] | 1695 | |
Ingo Molnar | 9029a5e | 2009-05-15 08:26:20 +0200 | [diff] [blame] | 1696 | loops = 0; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1697 | again: |
Ingo Molnar | 9029a5e | 2009-05-15 08:26:20 +0200 | [diff] [blame] | 1698 | if (++loops > 100) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1699 | WARN_ONCE(1, "perfevents: irq loop stuck!\n"); |
| 1700 | perf_event_print_debug(); |
Ingo Molnar | aaba980 | 2009-05-26 08:10:00 +0200 | [diff] [blame] | 1701 | intel_pmu_reset(); |
| 1702 | perf_enable(); |
Ingo Molnar | 9029a5e | 2009-05-15 08:26:20 +0200 | [diff] [blame] | 1703 | return 1; |
| 1704 | } |
| 1705 | |
Mike Galbraith | d278c48 | 2009-02-09 07:38:50 +0100 | [diff] [blame] | 1706 | inc_irq_stat(apic_perf_irqs); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1707 | ack = status; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1708 | for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1709 | struct perf_event *event = cpuc->events[bit]; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1710 | |
| 1711 | clear_bit(bit, (unsigned long *) &status); |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 1712 | if (!test_bit(bit, cpuc->active_mask)) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1713 | continue; |
| 1714 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1715 | if (!intel_pmu_save_and_restart(event)) |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1716 | continue; |
| 1717 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1718 | data.period = event->hw.last_period; |
Peter Zijlstra | 60f916d | 2009-06-15 19:00:20 +0200 | [diff] [blame] | 1719 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1720 | if (perf_event_overflow(event, 1, &data, regs)) |
| 1721 | intel_pmu_disable_event(&event->hw, bit); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1722 | } |
| 1723 | |
Robert Richter | dee5d90 | 2009-04-29 12:47:07 +0200 | [diff] [blame] | 1724 | intel_pmu_ack_status(ack); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1725 | |
| 1726 | /* |
| 1727 | * Repeat if there is more work to be done: |
| 1728 | */ |
Robert Richter | 19d84da | 2009-04-29 12:47:25 +0200 | [diff] [blame] | 1729 | status = intel_pmu_get_status(); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1730 | if (status) |
| 1731 | goto again; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1732 | |
Peter Zijlstra | 48e22d5 | 2009-05-25 17:39:04 +0200 | [diff] [blame] | 1733 | perf_enable(); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1734 | |
| 1735 | return 1; |
Mike Galbraith | 1b023a9 | 2009-01-23 10:13:01 +0100 | [diff] [blame] | 1736 | } |
| 1737 | |
Yong Wang | a328810 | 2009-06-03 13:12:55 +0800 | [diff] [blame] | 1738 | static int amd_pmu_handle_irq(struct pt_regs *regs) |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 1739 | { |
Peter Zijlstra | df1a132 | 2009-06-10 21:02:22 +0200 | [diff] [blame] | 1740 | struct perf_sample_data data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1741 | struct cpu_hw_events *cpuc; |
| 1742 | struct perf_event *event; |
| 1743 | struct hw_perf_event *hwc; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1744 | int idx, handled = 0; |
Ingo Molnar | 9029a5e | 2009-05-15 08:26:20 +0200 | [diff] [blame] | 1745 | u64 val; |
| 1746 | |
Peter Zijlstra | df1a132 | 2009-06-10 21:02:22 +0200 | [diff] [blame] | 1747 | data.addr = 0; |
| 1748 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1749 | cpuc = &__get_cpu_var(cpu_hw_events); |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 1750 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1751 | for (idx = 0; idx < x86_pmu.num_events; idx++) { |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 1752 | if (!test_bit(idx, cpuc->active_mask)) |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 1753 | continue; |
Peter Zijlstra | 962bf7a | 2009-05-13 13:21:36 +0200 | [diff] [blame] | 1754 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1755 | event = cpuc->events[idx]; |
| 1756 | hwc = &event->hw; |
Peter Zijlstra | a4016a7 | 2009-05-14 14:52:17 +0200 | [diff] [blame] | 1757 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1758 | val = x86_perf_event_update(event, hwc, idx); |
| 1759 | if (val & (1ULL << (x86_pmu.event_bits - 1))) |
Peter Zijlstra | 48e22d5 | 2009-05-25 17:39:04 +0200 | [diff] [blame] | 1760 | continue; |
Peter Zijlstra | 962bf7a | 2009-05-13 13:21:36 +0200 | [diff] [blame] | 1761 | |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1762 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1763 | * event overflow |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1764 | */ |
| 1765 | handled = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1766 | data.period = event->hw.last_period; |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1767 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1768 | if (!x86_perf_event_set_period(event, hwc, idx)) |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1769 | continue; |
| 1770 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1771 | if (perf_event_overflow(event, 1, &data, regs)) |
| 1772 | amd_pmu_disable_event(hwc, idx); |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 1773 | } |
Peter Zijlstra | 962bf7a | 2009-05-13 13:21:36 +0200 | [diff] [blame] | 1774 | |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1775 | if (handled) |
| 1776 | inc_irq_stat(apic_perf_irqs); |
| 1777 | |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 1778 | return handled; |
| 1779 | } |
Robert Richter | 39d81ea | 2009-04-29 12:47:05 +0200 | [diff] [blame] | 1780 | |
Peter Zijlstra | b6276f3 | 2009-04-06 11:45:03 +0200 | [diff] [blame] | 1781 | void smp_perf_pending_interrupt(struct pt_regs *regs) |
| 1782 | { |
| 1783 | irq_enter(); |
| 1784 | ack_APIC_irq(); |
| 1785 | inc_irq_stat(apic_pending_irqs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1786 | perf_event_do_pending(); |
Peter Zijlstra | b6276f3 | 2009-04-06 11:45:03 +0200 | [diff] [blame] | 1787 | irq_exit(); |
| 1788 | } |
| 1789 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1790 | void set_perf_event_pending(void) |
Peter Zijlstra | b6276f3 | 2009-04-06 11:45:03 +0200 | [diff] [blame] | 1791 | { |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1792 | #ifdef CONFIG_X86_LOCAL_APIC |
Peter Zijlstra | b6276f3 | 2009-04-06 11:45:03 +0200 | [diff] [blame] | 1793 | apic->send_IPI_self(LOCAL_PENDING_VECTOR); |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1794 | #endif |
Peter Zijlstra | b6276f3 | 2009-04-06 11:45:03 +0200 | [diff] [blame] | 1795 | } |
| 1796 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1797 | void perf_events_lapic_init(void) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1798 | { |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1799 | #ifdef CONFIG_X86_LOCAL_APIC |
| 1800 | if (!x86_pmu.apic || !x86_pmu_initialized()) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1801 | return; |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 1802 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1803 | /* |
Yong Wang | c323d95 | 2009-05-29 13:28:35 +0800 | [diff] [blame] | 1804 | * Always use NMI for PMU |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1805 | */ |
Yong Wang | c323d95 | 2009-05-29 13:28:35 +0800 | [diff] [blame] | 1806 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1807 | #endif |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | static int __kprobes |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1811 | perf_event_nmi_handler(struct notifier_block *self, |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1812 | unsigned long cmd, void *__args) |
| 1813 | { |
| 1814 | struct die_args *args = __args; |
| 1815 | struct pt_regs *regs; |
| 1816 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1817 | if (!atomic_read(&active_events)) |
Peter Zijlstra | 63a809a | 2009-05-01 12:23:17 +0200 | [diff] [blame] | 1818 | return NOTIFY_DONE; |
| 1819 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1820 | switch (cmd) { |
| 1821 | case DIE_NMI: |
| 1822 | case DIE_NMI_IPI: |
| 1823 | break; |
| 1824 | |
| 1825 | default: |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1826 | return NOTIFY_DONE; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1827 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1828 | |
| 1829 | regs = args->regs; |
| 1830 | |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1831 | #ifdef CONFIG_X86_LOCAL_APIC |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1832 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1833 | #endif |
Peter Zijlstra | a4016a7 | 2009-05-14 14:52:17 +0200 | [diff] [blame] | 1834 | /* |
| 1835 | * Can't rely on the handled return value to say it was our NMI, two |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1836 | * events could trigger 'simultaneously' raising two back-to-back NMIs. |
Peter Zijlstra | a4016a7 | 2009-05-14 14:52:17 +0200 | [diff] [blame] | 1837 | * |
| 1838 | * If the first NMI handles both, the latter will be empty and daze |
| 1839 | * the CPU. |
| 1840 | */ |
Yong Wang | a328810 | 2009-06-03 13:12:55 +0800 | [diff] [blame] | 1841 | x86_pmu.handle_irq(regs); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1842 | |
Peter Zijlstra | a4016a7 | 2009-05-14 14:52:17 +0200 | [diff] [blame] | 1843 | return NOTIFY_STOP; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1844 | } |
| 1845 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1846 | static __read_mostly struct notifier_block perf_event_nmi_notifier = { |
| 1847 | .notifier_call = perf_event_nmi_handler, |
Mike Galbraith | 5b75af0 | 2009-02-04 17:11:34 +0100 | [diff] [blame] | 1848 | .next = NULL, |
| 1849 | .priority = 1 |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1850 | }; |
| 1851 | |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1852 | static struct x86_pmu p6_pmu = { |
| 1853 | .name = "p6", |
| 1854 | .handle_irq = p6_pmu_handle_irq, |
| 1855 | .disable_all = p6_pmu_disable_all, |
| 1856 | .enable_all = p6_pmu_enable_all, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1857 | .enable = p6_pmu_enable_event, |
| 1858 | .disable = p6_pmu_disable_event, |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1859 | .eventsel = MSR_P6_EVNTSEL0, |
| 1860 | .perfctr = MSR_P6_PERFCTR0, |
| 1861 | .event_map = p6_pmu_event_map, |
| 1862 | .raw_event = p6_pmu_raw_event, |
| 1863 | .max_events = ARRAY_SIZE(p6_perfmon_event_map), |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1864 | .apic = 1, |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1865 | .max_period = (1ULL << 31) - 1, |
| 1866 | .version = 0, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1867 | .num_events = 2, |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1868 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1869 | * Events have 40 bits implemented. However they are designed such |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1870 | * that bits [32-39] are sign extensions of bit 31. As such the |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1871 | * effective width of a event for P6-like PMU is 32 bits only. |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1872 | * |
| 1873 | * See IA-32 Intel Architecture Software developer manual Vol 3B |
| 1874 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1875 | .event_bits = 32, |
| 1876 | .event_mask = (1ULL << 32) - 1, |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1877 | }; |
| 1878 | |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 1879 | static struct x86_pmu intel_pmu = { |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 1880 | .name = "Intel", |
Robert Richter | 39d81ea | 2009-04-29 12:47:05 +0200 | [diff] [blame] | 1881 | .handle_irq = intel_pmu_handle_irq, |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1882 | .disable_all = intel_pmu_disable_all, |
| 1883 | .enable_all = intel_pmu_enable_all, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1884 | .enable = intel_pmu_enable_event, |
| 1885 | .disable = intel_pmu_disable_event, |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1886 | .eventsel = MSR_ARCH_PERFMON_EVENTSEL0, |
| 1887 | .perfctr = MSR_ARCH_PERFMON_PERFCTR0, |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 1888 | .event_map = intel_pmu_event_map, |
| 1889 | .raw_event = intel_pmu_raw_event, |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1890 | .max_events = ARRAY_SIZE(intel_perfmon_event_map), |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1891 | .apic = 1, |
Robert Richter | c619b8f | 2009-04-29 12:47:23 +0200 | [diff] [blame] | 1892 | /* |
| 1893 | * Intel PMCs cannot be accessed sanely above 32 bit width, |
| 1894 | * so we install an artificial 1<<31 period regardless of |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1895 | * the generic event period: |
Robert Richter | c619b8f | 2009-04-29 12:47:23 +0200 | [diff] [blame] | 1896 | */ |
| 1897 | .max_period = (1ULL << 31) - 1, |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1898 | .enable_bts = intel_pmu_enable_bts, |
| 1899 | .disable_bts = intel_pmu_disable_bts, |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1900 | }; |
| 1901 | |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 1902 | static struct x86_pmu amd_pmu = { |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 1903 | .name = "AMD", |
Robert Richter | 39d81ea | 2009-04-29 12:47:05 +0200 | [diff] [blame] | 1904 | .handle_irq = amd_pmu_handle_irq, |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1905 | .disable_all = amd_pmu_disable_all, |
| 1906 | .enable_all = amd_pmu_enable_all, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1907 | .enable = amd_pmu_enable_event, |
| 1908 | .disable = amd_pmu_disable_event, |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1909 | .eventsel = MSR_K7_EVNTSEL0, |
| 1910 | .perfctr = MSR_K7_PERFCTR0, |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 1911 | .event_map = amd_pmu_event_map, |
| 1912 | .raw_event = amd_pmu_raw_event, |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1913 | .max_events = ARRAY_SIZE(amd_perfmon_event_map), |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1914 | .num_events = 4, |
| 1915 | .event_bits = 48, |
| 1916 | .event_mask = (1ULL << 48) - 1, |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1917 | .apic = 1, |
Robert Richter | c619b8f | 2009-04-29 12:47:23 +0200 | [diff] [blame] | 1918 | /* use highest bit to detect overflow */ |
| 1919 | .max_period = (1ULL << 47) - 1, |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1920 | }; |
| 1921 | |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1922 | static int p6_pmu_init(void) |
| 1923 | { |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1924 | switch (boot_cpu_data.x86_model) { |
| 1925 | case 1: |
| 1926 | case 3: /* Pentium Pro */ |
| 1927 | case 5: |
| 1928 | case 6: /* Pentium II */ |
| 1929 | case 7: |
| 1930 | case 8: |
| 1931 | case 11: /* Pentium III */ |
| 1932 | break; |
| 1933 | case 9: |
| 1934 | case 13: |
Daniel Qarras | f1c6a58 | 2009-07-12 04:32:40 -0700 | [diff] [blame] | 1935 | /* Pentium M */ |
| 1936 | break; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1937 | default: |
| 1938 | pr_cont("unsupported p6 CPU model %d ", |
| 1939 | boot_cpu_data.x86_model); |
| 1940 | return -ENODEV; |
| 1941 | } |
| 1942 | |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1943 | x86_pmu = p6_pmu; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1944 | |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1945 | if (!cpu_has_apic) { |
Ingo Molnar | 3c581a7 | 2009-08-11 10:47:36 +0200 | [diff] [blame] | 1946 | pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n"); |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1947 | pr_info("no hardware sampling interrupt available.\n"); |
| 1948 | x86_pmu.apic = 0; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1949 | } |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1950 | |
| 1951 | return 0; |
| 1952 | } |
| 1953 | |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 1954 | static int intel_pmu_init(void) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1955 | { |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 1956 | union cpuid10_edx edx; |
Ingo Molnar | 7bb497b | 2009-03-18 08:59:21 +0100 | [diff] [blame] | 1957 | union cpuid10_eax eax; |
| 1958 | unsigned int unused; |
| 1959 | unsigned int ebx; |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 1960 | int version; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1961 | |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1962 | if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) { |
| 1963 | /* check for P6 processor family */ |
| 1964 | if (boot_cpu_data.x86 == 6) { |
| 1965 | return p6_pmu_init(); |
| 1966 | } else { |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 1967 | return -ENODEV; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1968 | } |
| 1969 | } |
Robert Richter | da1a776 | 2009-04-29 12:46:58 +0200 | [diff] [blame] | 1970 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1971 | /* |
| 1972 | * Check whether the Architectural PerfMon supports |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 1973 | * Branch Misses Retired hw_event or not. |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1974 | */ |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 1975 | cpuid(10, &eax.full, &ebx, &unused, &edx.full); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1976 | if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED) |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 1977 | return -ENODEV; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1978 | |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 1979 | version = eax.split.version_id; |
| 1980 | if (version < 2) |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 1981 | return -ENODEV; |
Ingo Molnar | 7bb497b | 2009-03-18 08:59:21 +0100 | [diff] [blame] | 1982 | |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1983 | x86_pmu = intel_pmu; |
| 1984 | x86_pmu.version = version; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1985 | x86_pmu.num_events = eax.split.num_events; |
| 1986 | x86_pmu.event_bits = eax.split.bit_width; |
| 1987 | x86_pmu.event_mask = (1ULL << eax.split.bit_width) - 1; |
Ingo Molnar | 066d7de | 2009-05-04 19:04:09 +0200 | [diff] [blame] | 1988 | |
| 1989 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1990 | * Quirk: v2 perfmon does not report fixed-purpose events, so |
| 1991 | * assume at least 3 events: |
Ingo Molnar | 066d7de | 2009-05-04 19:04:09 +0200 | [diff] [blame] | 1992 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1993 | x86_pmu.num_events_fixed = max((int)edx.split.num_events_fixed, 3); |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1994 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 1995 | /* |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 1996 | * Install the hw-cache-events table: |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 1997 | */ |
| 1998 | switch (boot_cpu_data.x86_model) { |
Yong Wang | dc81081 | 2009-06-10 17:06:12 +0800 | [diff] [blame] | 1999 | case 15: /* original 65 nm celeron/pentium/core2/xeon, "Merom"/"Conroe" */ |
| 2000 | case 22: /* single-core 65 nm celeron/core2solo "Merom-L"/"Conroe-L" */ |
| 2001 | case 23: /* current 45 nm celeron/core2/xeon "Penryn"/"Wolfdale" */ |
| 2002 | case 29: /* six-core 45 nm xeon "Dunnington" */ |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 2003 | memcpy(hw_cache_event_ids, core2_hw_cache_event_ids, |
Thomas Gleixner | 820a644 | 2009-06-08 19:10:25 +0200 | [diff] [blame] | 2004 | sizeof(hw_cache_event_ids)); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 2005 | |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2006 | pr_cont("Core2 events, "); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 2007 | break; |
| 2008 | default: |
| 2009 | case 26: |
| 2010 | memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids, |
Thomas Gleixner | 820a644 | 2009-06-08 19:10:25 +0200 | [diff] [blame] | 2011 | sizeof(hw_cache_event_ids)); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 2012 | |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2013 | pr_cont("Nehalem/Corei7 events, "); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 2014 | break; |
| 2015 | case 28: |
| 2016 | memcpy(hw_cache_event_ids, atom_hw_cache_event_ids, |
Thomas Gleixner | 820a644 | 2009-06-08 19:10:25 +0200 | [diff] [blame] | 2017 | sizeof(hw_cache_event_ids)); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 2018 | |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2019 | pr_cont("Atom events, "); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 2020 | break; |
| 2021 | } |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 2022 | return 0; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 2023 | } |
| 2024 | |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 2025 | static int amd_pmu_init(void) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 2026 | { |
Jaswinder Singh Rajput | 4d2be12 | 2009-06-11 15:28:09 +0530 | [diff] [blame] | 2027 | /* Performance-monitoring supported from K7 and later: */ |
| 2028 | if (boot_cpu_data.x86 < 6) |
| 2029 | return -ENODEV; |
| 2030 | |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 2031 | x86_pmu = amd_pmu; |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 2032 | |
Jaswinder Singh Rajput | f4db43a | 2009-06-13 01:06:21 +0530 | [diff] [blame] | 2033 | /* Events are common for all AMDs */ |
| 2034 | memcpy(hw_cache_event_ids, amd_hw_cache_event_ids, |
| 2035 | sizeof(hw_cache_event_ids)); |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 2036 | |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 2037 | return 0; |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 2038 | } |
| 2039 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2040 | void __init init_hw_perf_events(void) |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 2041 | { |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 2042 | int err; |
| 2043 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2044 | pr_info("Performance Events: "); |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2045 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 2046 | switch (boot_cpu_data.x86_vendor) { |
| 2047 | case X86_VENDOR_INTEL: |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 2048 | err = intel_pmu_init(); |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 2049 | break; |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 2050 | case X86_VENDOR_AMD: |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 2051 | err = amd_pmu_init(); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 2052 | break; |
Robert Richter | 4138960 | 2009-04-29 12:47:00 +0200 | [diff] [blame] | 2053 | default: |
| 2054 | return; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 2055 | } |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2056 | if (err != 0) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2057 | pr_cont("no PMU driver, software events only.\n"); |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 2058 | return; |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2059 | } |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 2060 | |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2061 | pr_cont("%s PMU driver.\n", x86_pmu.name); |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 2062 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2063 | if (x86_pmu.num_events > X86_PMC_MAX_GENERIC) { |
| 2064 | WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!", |
| 2065 | x86_pmu.num_events, X86_PMC_MAX_GENERIC); |
| 2066 | x86_pmu.num_events = X86_PMC_MAX_GENERIC; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2067 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2068 | perf_event_mask = (1 << x86_pmu.num_events) - 1; |
| 2069 | perf_max_events = x86_pmu.num_events; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2070 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2071 | if (x86_pmu.num_events_fixed > X86_PMC_MAX_FIXED) { |
| 2072 | WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!", |
| 2073 | x86_pmu.num_events_fixed, X86_PMC_MAX_FIXED); |
| 2074 | x86_pmu.num_events_fixed = X86_PMC_MAX_FIXED; |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 2075 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2076 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2077 | perf_event_mask |= |
| 2078 | ((1LL << x86_pmu.num_events_fixed)-1) << X86_PMC_IDX_FIXED; |
| 2079 | x86_pmu.intel_ctrl = perf_event_mask; |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 2080 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2081 | perf_events_lapic_init(); |
| 2082 | register_die_notifier(&perf_event_nmi_notifier); |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2083 | |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 2084 | pr_info("... version: %d\n", x86_pmu.version); |
| 2085 | pr_info("... bit width: %d\n", x86_pmu.event_bits); |
| 2086 | pr_info("... generic registers: %d\n", x86_pmu.num_events); |
| 2087 | pr_info("... value mask: %016Lx\n", x86_pmu.event_mask); |
| 2088 | pr_info("... max period: %016Lx\n", x86_pmu.max_period); |
| 2089 | pr_info("... fixed-purpose events: %d\n", x86_pmu.num_events_fixed); |
| 2090 | pr_info("... event mask: %016Lx\n", perf_event_mask); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2091 | } |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 2092 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2093 | static inline void x86_pmu_read(struct perf_event *event) |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 2094 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2095 | x86_perf_event_update(event, &event->hw, event->hw.idx); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 2096 | } |
| 2097 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 2098 | static const struct pmu pmu = { |
| 2099 | .enable = x86_pmu_enable, |
| 2100 | .disable = x86_pmu_disable, |
| 2101 | .read = x86_pmu_read, |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 2102 | .unthrottle = x86_pmu_unthrottle, |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 2103 | }; |
| 2104 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2105 | const struct pmu *hw_perf_event_init(struct perf_event *event) |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 2106 | { |
| 2107 | int err; |
| 2108 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2109 | err = __hw_perf_event_init(event); |
Peter Zijlstra | a1792cdac | 2009-09-09 10:04:47 +0200 | [diff] [blame] | 2110 | if (err) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2111 | if (event->destroy) |
| 2112 | event->destroy(event); |
Peter Zijlstra | 9ea98e1 | 2009-03-30 19:07:09 +0200 | [diff] [blame] | 2113 | return ERR_PTR(err); |
Peter Zijlstra | a1792cdac | 2009-09-09 10:04:47 +0200 | [diff] [blame] | 2114 | } |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 2115 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 2116 | return &pmu; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 2117 | } |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2118 | |
| 2119 | /* |
| 2120 | * callchain support |
| 2121 | */ |
| 2122 | |
| 2123 | static inline |
Peter Zijlstra | f9188e0 | 2009-06-18 22:20:52 +0200 | [diff] [blame] | 2124 | void callchain_store(struct perf_callchain_entry *entry, u64 ip) |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2125 | { |
Peter Zijlstra | f9188e0 | 2009-06-18 22:20:52 +0200 | [diff] [blame] | 2126 | if (entry->nr < PERF_MAX_STACK_DEPTH) |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2127 | entry->ip[entry->nr++] = ip; |
| 2128 | } |
| 2129 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 2130 | static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_irq_entry); |
| 2131 | static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_nmi_entry); |
Frederic Weisbecker | 0406ca6 | 2009-07-01 21:02:09 +0200 | [diff] [blame] | 2132 | static DEFINE_PER_CPU(int, in_nmi_frame); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2133 | |
| 2134 | |
| 2135 | static void |
| 2136 | backtrace_warning_symbol(void *data, char *msg, unsigned long symbol) |
| 2137 | { |
| 2138 | /* Ignore warnings */ |
| 2139 | } |
| 2140 | |
| 2141 | static void backtrace_warning(void *data, char *msg) |
| 2142 | { |
| 2143 | /* Ignore warnings */ |
| 2144 | } |
| 2145 | |
| 2146 | static int backtrace_stack(void *data, char *name) |
| 2147 | { |
Frederic Weisbecker | 0406ca6 | 2009-07-01 21:02:09 +0200 | [diff] [blame] | 2148 | per_cpu(in_nmi_frame, smp_processor_id()) = |
| 2149 | x86_is_stack_id(NMI_STACK, name); |
| 2150 | |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 2151 | return 0; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2152 | } |
| 2153 | |
| 2154 | static void backtrace_address(void *data, unsigned long addr, int reliable) |
| 2155 | { |
| 2156 | struct perf_callchain_entry *entry = data; |
| 2157 | |
Frederic Weisbecker | 0406ca6 | 2009-07-01 21:02:09 +0200 | [diff] [blame] | 2158 | if (per_cpu(in_nmi_frame, smp_processor_id())) |
| 2159 | return; |
| 2160 | |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2161 | if (reliable) |
| 2162 | callchain_store(entry, addr); |
| 2163 | } |
| 2164 | |
| 2165 | static const struct stacktrace_ops backtrace_ops = { |
| 2166 | .warning = backtrace_warning, |
| 2167 | .warning_symbol = backtrace_warning_symbol, |
| 2168 | .stack = backtrace_stack, |
| 2169 | .address = backtrace_address, |
| 2170 | }; |
| 2171 | |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 2172 | #include "../dumpstack.h" |
| 2173 | |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2174 | static void |
| 2175 | perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry) |
| 2176 | { |
Peter Zijlstra | f9188e0 | 2009-06-18 22:20:52 +0200 | [diff] [blame] | 2177 | callchain_store(entry, PERF_CONTEXT_KERNEL); |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 2178 | callchain_store(entry, regs->ip); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2179 | |
Peter Zijlstra | f9188e0 | 2009-06-18 22:20:52 +0200 | [diff] [blame] | 2180 | dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2181 | } |
| 2182 | |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 2183 | /* |
| 2184 | * best effort, GUP based copy_from_user() that assumes IRQ or NMI context |
| 2185 | */ |
| 2186 | static unsigned long |
| 2187 | copy_from_user_nmi(void *to, const void __user *from, unsigned long n) |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2188 | { |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 2189 | unsigned long offset, addr = (unsigned long)from; |
| 2190 | int type = in_nmi() ? KM_NMI : KM_IRQ0; |
| 2191 | unsigned long size, len = 0; |
| 2192 | struct page *page; |
| 2193 | void *map; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2194 | int ret; |
| 2195 | |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 2196 | do { |
| 2197 | ret = __get_user_pages_fast(addr, 1, 0, &page); |
| 2198 | if (!ret) |
| 2199 | break; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2200 | |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 2201 | offset = addr & (PAGE_SIZE - 1); |
| 2202 | size = min(PAGE_SIZE - offset, n - len); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2203 | |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 2204 | map = kmap_atomic(page, type); |
| 2205 | memcpy(to, map+offset, size); |
| 2206 | kunmap_atomic(map, type); |
| 2207 | put_page(page); |
| 2208 | |
| 2209 | len += size; |
| 2210 | to += size; |
| 2211 | addr += size; |
| 2212 | |
| 2213 | } while (len < n); |
| 2214 | |
| 2215 | return len; |
| 2216 | } |
| 2217 | |
| 2218 | static int copy_stack_frame(const void __user *fp, struct stack_frame *frame) |
| 2219 | { |
| 2220 | unsigned long bytes; |
| 2221 | |
| 2222 | bytes = copy_from_user_nmi(frame, fp, sizeof(*frame)); |
| 2223 | |
| 2224 | return bytes == sizeof(*frame); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | static void |
| 2228 | perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry) |
| 2229 | { |
| 2230 | struct stack_frame frame; |
| 2231 | const void __user *fp; |
| 2232 | |
Ingo Molnar | 5a6cec3 | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2233 | if (!user_mode(regs)) |
| 2234 | regs = task_pt_regs(current); |
| 2235 | |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 2236 | fp = (void __user *)regs->bp; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2237 | |
Peter Zijlstra | f9188e0 | 2009-06-18 22:20:52 +0200 | [diff] [blame] | 2238 | callchain_store(entry, PERF_CONTEXT_USER); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2239 | callchain_store(entry, regs->ip); |
| 2240 | |
Peter Zijlstra | f9188e0 | 2009-06-18 22:20:52 +0200 | [diff] [blame] | 2241 | while (entry->nr < PERF_MAX_STACK_DEPTH) { |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 2242 | frame.next_frame = NULL; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2243 | frame.return_address = 0; |
| 2244 | |
| 2245 | if (!copy_stack_frame(fp, &frame)) |
| 2246 | break; |
| 2247 | |
Ingo Molnar | 5a6cec3 | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2248 | if ((unsigned long)fp < regs->sp) |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2249 | break; |
| 2250 | |
| 2251 | callchain_store(entry, frame.return_address); |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 2252 | fp = frame.next_frame; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2253 | } |
| 2254 | } |
| 2255 | |
| 2256 | static void |
| 2257 | perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry) |
| 2258 | { |
| 2259 | int is_user; |
| 2260 | |
| 2261 | if (!regs) |
| 2262 | return; |
| 2263 | |
| 2264 | is_user = user_mode(regs); |
| 2265 | |
| 2266 | if (!current || current->pid == 0) |
| 2267 | return; |
| 2268 | |
| 2269 | if (is_user && current->state != TASK_RUNNING) |
| 2270 | return; |
| 2271 | |
| 2272 | if (!is_user) |
| 2273 | perf_callchain_kernel(regs, entry); |
| 2274 | |
| 2275 | if (current->mm) |
| 2276 | perf_callchain_user(regs, entry); |
| 2277 | } |
| 2278 | |
| 2279 | struct perf_callchain_entry *perf_callchain(struct pt_regs *regs) |
| 2280 | { |
| 2281 | struct perf_callchain_entry *entry; |
| 2282 | |
| 2283 | if (in_nmi()) |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 2284 | entry = &__get_cpu_var(pmc_nmi_entry); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2285 | else |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 2286 | entry = &__get_cpu_var(pmc_irq_entry); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2287 | |
| 2288 | entry->nr = 0; |
| 2289 | |
| 2290 | perf_do_callchain(regs, entry); |
| 2291 | |
| 2292 | return entry; |
| 2293 | } |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 2294 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2295 | void hw_perf_event_setup_online(int cpu) |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 2296 | { |
| 2297 | init_debug_store_on_cpu(cpu); |
| 2298 | } |