Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2 | * Performance events x86 architecture code |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 3 | * |
Ingo Molnar | 9814451 | 2009-04-29 14:52:50 +0200 | [diff] [blame] | 4 | * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de> |
| 5 | * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar |
| 6 | * Copyright (C) 2009 Jaswinder Singh Rajput |
| 7 | * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter |
| 8 | * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 9 | * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com> |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 10 | * Copyright (C) 2009 Google, Inc., Stephane Eranian |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 11 | * |
| 12 | * For licencing details see kernel-base/COPYING |
| 13 | */ |
| 14 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 15 | #include <linux/perf_event.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 16 | #include <linux/capability.h> |
| 17 | #include <linux/notifier.h> |
| 18 | #include <linux/hardirq.h> |
| 19 | #include <linux/kprobes.h> |
Thomas Gleixner | 4ac1329 | 2008-12-09 21:43:39 +0100 | [diff] [blame] | 20 | #include <linux/module.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 21 | #include <linux/kdebug.h> |
| 22 | #include <linux/sched.h> |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 23 | #include <linux/uaccess.h> |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 24 | #include <linux/highmem.h> |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 25 | #include <linux/cpu.h> |
Peter Zijlstra | 272d30b | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 26 | #include <linux/bitops.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 27 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 28 | #include <asm/apic.h> |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 29 | #include <asm/stacktrace.h> |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 30 | #include <asm/nmi.h> |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 31 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 32 | static u64 perf_event_mask __read_mostly; |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 33 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 34 | /* The maximal number of PEBS events: */ |
| 35 | #define MAX_PEBS_EVENTS 4 |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 36 | |
| 37 | /* The size of a BTS record in bytes: */ |
| 38 | #define BTS_RECORD_SIZE 24 |
| 39 | |
| 40 | /* The size of a per-cpu BTS buffer in bytes: */ |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 41 | #define BTS_BUFFER_SIZE (BTS_RECORD_SIZE * 2048) |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 42 | |
| 43 | /* The BTS overflow threshold in bytes from the end of the buffer: */ |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 44 | #define BTS_OVFL_TH (BTS_RECORD_SIZE * 128) |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 45 | |
| 46 | |
| 47 | /* |
| 48 | * Bits in the debugctlmsr controlling branch tracing. |
| 49 | */ |
| 50 | #define X86_DEBUGCTL_TR (1 << 6) |
| 51 | #define X86_DEBUGCTL_BTS (1 << 7) |
| 52 | #define X86_DEBUGCTL_BTINT (1 << 8) |
| 53 | #define X86_DEBUGCTL_BTS_OFF_OS (1 << 9) |
| 54 | #define X86_DEBUGCTL_BTS_OFF_USR (1 << 10) |
| 55 | |
| 56 | /* |
| 57 | * A debug store configuration. |
| 58 | * |
| 59 | * We only support architectures that use 64bit fields. |
| 60 | */ |
| 61 | struct debug_store { |
| 62 | u64 bts_buffer_base; |
| 63 | u64 bts_index; |
| 64 | u64 bts_absolute_maximum; |
| 65 | u64 bts_interrupt_threshold; |
| 66 | u64 pebs_buffer_base; |
| 67 | u64 pebs_index; |
| 68 | u64 pebs_absolute_maximum; |
| 69 | u64 pebs_interrupt_threshold; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 70 | u64 pebs_event_reset[MAX_PEBS_EVENTS]; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 71 | }; |
| 72 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 73 | struct event_constraint { |
Peter Zijlstra | c91e0f5 | 2010-01-22 15:25:59 +0100 | [diff] [blame] | 74 | union { |
| 75 | unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
| 76 | u64 idxmsk64[1]; |
| 77 | }; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 78 | int code; |
| 79 | int cmask; |
Peter Zijlstra | 272d30b | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 80 | int weight; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 81 | }; |
| 82 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 83 | struct cpu_hw_events { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 84 | struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */ |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 85 | unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
Mike Galbraith | 4b39fd9 | 2009-01-23 14:36:16 +0100 | [diff] [blame] | 86 | unsigned long interrupts; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 87 | int enabled; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 88 | struct debug_store *ds; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 89 | |
| 90 | int n_events; |
| 91 | int n_added; |
| 92 | int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */ |
| 93 | struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */ |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 94 | }; |
| 95 | |
Peter Zijlstra | fce877e | 2010-01-29 13:25:12 +0100 | [diff] [blame^] | 96 | #define __EVENT_CONSTRAINT(c, n, m, w) {\ |
Peter Zijlstra | c91e0f5 | 2010-01-22 15:25:59 +0100 | [diff] [blame] | 97 | { .idxmsk64[0] = (n) }, \ |
| 98 | .code = (c), \ |
| 99 | .cmask = (m), \ |
Peter Zijlstra | fce877e | 2010-01-29 13:25:12 +0100 | [diff] [blame^] | 100 | .weight = (w), \ |
Peter Zijlstra | c91e0f5 | 2010-01-22 15:25:59 +0100 | [diff] [blame] | 101 | } |
Stephane Eranian | b690081 | 2009-10-06 16:42:09 +0200 | [diff] [blame] | 102 | |
Peter Zijlstra | fce877e | 2010-01-29 13:25:12 +0100 | [diff] [blame^] | 103 | #define EVENT_CONSTRAINT(c, n, m) \ |
| 104 | __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n)) |
| 105 | |
Peter Zijlstra | ed8777f | 2010-01-27 23:07:46 +0100 | [diff] [blame] | 106 | #define INTEL_EVENT_CONSTRAINT(c, n) \ |
| 107 | EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVTSEL_MASK) |
Peter Zijlstra | 8433be1 | 2010-01-22 15:38:26 +0100 | [diff] [blame] | 108 | |
Peter Zijlstra | ed8777f | 2010-01-27 23:07:46 +0100 | [diff] [blame] | 109 | #define FIXED_EVENT_CONSTRAINT(c, n) \ |
| 110 | EVENT_CONSTRAINT(c, n, INTEL_ARCH_FIXED_MASK) |
Peter Zijlstra | 8433be1 | 2010-01-22 15:38:26 +0100 | [diff] [blame] | 111 | |
Peter Zijlstra | ed8777f | 2010-01-27 23:07:46 +0100 | [diff] [blame] | 112 | #define EVENT_CONSTRAINT_END \ |
| 113 | EVENT_CONSTRAINT(0, 0, 0) |
| 114 | |
| 115 | #define for_each_event_constraint(e, c) \ |
| 116 | for ((e) = (c); (e)->cmask; (e)++) |
Stephane Eranian | b690081 | 2009-10-06 16:42:09 +0200 | [diff] [blame] | 117 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 118 | /* |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 119 | * struct x86_pmu - generic x86 pmu |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 120 | */ |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 121 | struct x86_pmu { |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 122 | const char *name; |
| 123 | int version; |
Yong Wang | a328810 | 2009-06-03 13:12:55 +0800 | [diff] [blame] | 124 | int (*handle_irq)(struct pt_regs *); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 125 | void (*disable_all)(void); |
| 126 | void (*enable_all)(void); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 127 | void (*enable)(struct hw_perf_event *, int); |
| 128 | void (*disable)(struct hw_perf_event *, int); |
Jaswinder Singh Rajput | 169e41e | 2009-02-28 18:37:49 +0530 | [diff] [blame] | 129 | unsigned eventsel; |
| 130 | unsigned perfctr; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 131 | u64 (*event_map)(int); |
| 132 | u64 (*raw_event)(u64); |
Jaswinder Singh Rajput | 169e41e | 2009-02-28 18:37:49 +0530 | [diff] [blame] | 133 | int max_events; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 134 | int num_events; |
| 135 | int num_events_fixed; |
| 136 | int event_bits; |
| 137 | u64 event_mask; |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 138 | int apic; |
Robert Richter | c619b8f | 2009-04-29 12:47:23 +0200 | [diff] [blame] | 139 | u64 max_period; |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 140 | u64 intel_ctrl; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 141 | void (*enable_bts)(u64 config); |
| 142 | void (*disable_bts)(void); |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 143 | |
| 144 | struct event_constraint * |
| 145 | (*get_event_constraints)(struct cpu_hw_events *cpuc, |
| 146 | struct perf_event *event); |
| 147 | |
Peter Zijlstra | c91e0f5 | 2010-01-22 15:25:59 +0100 | [diff] [blame] | 148 | void (*put_event_constraints)(struct cpu_hw_events *cpuc, |
| 149 | struct perf_event *event); |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 150 | struct event_constraint *event_constraints; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 151 | }; |
| 152 | |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 153 | static struct x86_pmu x86_pmu __read_mostly; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 154 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 155 | static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 156 | .enabled = 1, |
| 157 | }; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 158 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 159 | static int x86_perf_event_set_period(struct perf_event *event, |
| 160 | struct hw_perf_event *hwc, int idx); |
Stephane Eranian | b690081 | 2009-10-06 16:42:09 +0200 | [diff] [blame] | 161 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 162 | /* |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 163 | * Not sure about some of these |
| 164 | */ |
| 165 | static const u64 p6_perfmon_event_map[] = |
| 166 | { |
| 167 | [PERF_COUNT_HW_CPU_CYCLES] = 0x0079, |
| 168 | [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0, |
Ingo Molnar | f64cccc | 2009-08-11 10:26:33 +0200 | [diff] [blame] | 169 | [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0f2e, |
| 170 | [PERF_COUNT_HW_CACHE_MISSES] = 0x012e, |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 171 | [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4, |
| 172 | [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5, |
| 173 | [PERF_COUNT_HW_BUS_CYCLES] = 0x0062, |
| 174 | }; |
| 175 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 176 | static u64 p6_pmu_event_map(int hw_event) |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 177 | { |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 178 | return p6_perfmon_event_map[hw_event]; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 179 | } |
| 180 | |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 181 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 182 | * Event setting that is specified not to count anything. |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 183 | * We use this to effectively disable a counter. |
| 184 | * |
| 185 | * L2_RQSTS with 0 MESI unit mask. |
| 186 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 187 | #define P6_NOP_EVENT 0x0000002EULL |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 188 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 189 | static u64 p6_pmu_raw_event(u64 hw_event) |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 190 | { |
| 191 | #define P6_EVNTSEL_EVENT_MASK 0x000000FFULL |
| 192 | #define P6_EVNTSEL_UNIT_MASK 0x0000FF00ULL |
| 193 | #define P6_EVNTSEL_EDGE_MASK 0x00040000ULL |
| 194 | #define P6_EVNTSEL_INV_MASK 0x00800000ULL |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 195 | #define P6_EVNTSEL_REG_MASK 0xFF000000ULL |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 196 | |
| 197 | #define P6_EVNTSEL_MASK \ |
| 198 | (P6_EVNTSEL_EVENT_MASK | \ |
| 199 | P6_EVNTSEL_UNIT_MASK | \ |
| 200 | P6_EVNTSEL_EDGE_MASK | \ |
| 201 | P6_EVNTSEL_INV_MASK | \ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 202 | P6_EVNTSEL_REG_MASK) |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 203 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 204 | return hw_event & P6_EVNTSEL_MASK; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 205 | } |
| 206 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 207 | static struct event_constraint intel_p6_event_constraints[] = |
Stephane Eranian | b690081 | 2009-10-06 16:42:09 +0200 | [diff] [blame] | 208 | { |
Peter Zijlstra | 8433be1 | 2010-01-22 15:38:26 +0100 | [diff] [blame] | 209 | INTEL_EVENT_CONSTRAINT(0xc1, 0x1), /* FLOPS */ |
| 210 | INTEL_EVENT_CONSTRAINT(0x10, 0x1), /* FP_COMP_OPS_EXE */ |
| 211 | INTEL_EVENT_CONSTRAINT(0x11, 0x1), /* FP_ASSIST */ |
| 212 | INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */ |
| 213 | INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */ |
| 214 | INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */ |
Stephane Eranian | b690081 | 2009-10-06 16:42:09 +0200 | [diff] [blame] | 215 | EVENT_CONSTRAINT_END |
| 216 | }; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 217 | |
| 218 | /* |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 219 | * Intel PerfMon v3. Used on Core2 and later. |
| 220 | */ |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 221 | static const u64 intel_perfmon_event_map[] = |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 222 | { |
Peter Zijlstra | f4dbfa8 | 2009-06-11 14:06:28 +0200 | [diff] [blame] | 223 | [PERF_COUNT_HW_CPU_CYCLES] = 0x003c, |
| 224 | [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0, |
| 225 | [PERF_COUNT_HW_CACHE_REFERENCES] = 0x4f2e, |
| 226 | [PERF_COUNT_HW_CACHE_MISSES] = 0x412e, |
| 227 | [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4, |
| 228 | [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5, |
| 229 | [PERF_COUNT_HW_BUS_CYCLES] = 0x013c, |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 230 | }; |
| 231 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 232 | static struct event_constraint intel_core_event_constraints[] = |
Stephane Eranian | b690081 | 2009-10-06 16:42:09 +0200 | [diff] [blame] | 233 | { |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 234 | INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */ |
| 235 | INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */ |
| 236 | INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */ |
| 237 | INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */ |
| 238 | INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */ |
| 239 | INTEL_EVENT_CONSTRAINT(0xc1, 0x1), /* FP_COMP_INSTR_RET */ |
| 240 | EVENT_CONSTRAINT_END |
| 241 | }; |
| 242 | |
| 243 | static struct event_constraint intel_core2_event_constraints[] = |
| 244 | { |
Peter Zijlstra | 8433be1 | 2010-01-22 15:38:26 +0100 | [diff] [blame] | 245 | FIXED_EVENT_CONSTRAINT(0xc0, (0x3|(1ULL<<32))), /* INSTRUCTIONS_RETIRED */ |
| 246 | FIXED_EVENT_CONSTRAINT(0x3c, (0x3|(1ULL<<33))), /* UNHALTED_CORE_CYCLES */ |
| 247 | INTEL_EVENT_CONSTRAINT(0x10, 0x1), /* FP_COMP_OPS_EXE */ |
| 248 | INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */ |
| 249 | INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */ |
| 250 | INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */ |
| 251 | INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */ |
| 252 | INTEL_EVENT_CONSTRAINT(0x18, 0x1), /* IDLE_DURING_DIV */ |
| 253 | INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */ |
| 254 | INTEL_EVENT_CONSTRAINT(0xa1, 0x1), /* RS_UOPS_DISPATCH_CYCLES */ |
| 255 | INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED */ |
Stephane Eranian | b690081 | 2009-10-06 16:42:09 +0200 | [diff] [blame] | 256 | EVENT_CONSTRAINT_END |
| 257 | }; |
| 258 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 259 | static struct event_constraint intel_nehalem_event_constraints[] = |
Stephane Eranian | b690081 | 2009-10-06 16:42:09 +0200 | [diff] [blame] | 260 | { |
Peter Zijlstra | 452a339 | 2010-01-27 23:07:48 +0100 | [diff] [blame] | 261 | FIXED_EVENT_CONSTRAINT(0xc0, (0xf|(1ULL<<32))), /* INSTRUCTIONS_RETIRED */ |
| 262 | FIXED_EVENT_CONSTRAINT(0x3c, (0xf|(1ULL<<33))), /* UNHALTED_CORE_CYCLES */ |
Peter Zijlstra | 8433be1 | 2010-01-22 15:38:26 +0100 | [diff] [blame] | 263 | INTEL_EVENT_CONSTRAINT(0x40, 0x3), /* L1D_CACHE_LD */ |
| 264 | INTEL_EVENT_CONSTRAINT(0x41, 0x3), /* L1D_CACHE_ST */ |
| 265 | INTEL_EVENT_CONSTRAINT(0x42, 0x3), /* L1D_CACHE_LOCK */ |
| 266 | INTEL_EVENT_CONSTRAINT(0x43, 0x3), /* L1D_ALL_REF */ |
Peter Zijlstra | 452a339 | 2010-01-27 23:07:48 +0100 | [diff] [blame] | 267 | INTEL_EVENT_CONSTRAINT(0x48, 0x3), /* L1D_PEND_MISS */ |
Peter Zijlstra | 8433be1 | 2010-01-22 15:38:26 +0100 | [diff] [blame] | 268 | INTEL_EVENT_CONSTRAINT(0x4e, 0x3), /* L1D_PREFETCH */ |
Peter Zijlstra | 8433be1 | 2010-01-22 15:38:26 +0100 | [diff] [blame] | 269 | INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */ |
Peter Zijlstra | 452a339 | 2010-01-27 23:07:48 +0100 | [diff] [blame] | 270 | INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */ |
| 271 | EVENT_CONSTRAINT_END |
| 272 | }; |
| 273 | |
| 274 | static struct event_constraint intel_westmere_event_constraints[] = |
| 275 | { |
| 276 | FIXED_EVENT_CONSTRAINT(0xc0, (0xf|(1ULL<<32))), /* INSTRUCTIONS_RETIRED */ |
| 277 | FIXED_EVENT_CONSTRAINT(0x3c, (0xf|(1ULL<<33))), /* UNHALTED_CORE_CYCLES */ |
| 278 | INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */ |
| 279 | INTEL_EVENT_CONSTRAINT(0x60, 0x1), /* OFFCORE_REQUESTS_OUTSTANDING */ |
| 280 | INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */ |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 281 | EVENT_CONSTRAINT_END |
| 282 | }; |
| 283 | |
| 284 | static struct event_constraint intel_gen_event_constraints[] = |
| 285 | { |
Peter Zijlstra | 8433be1 | 2010-01-22 15:38:26 +0100 | [diff] [blame] | 286 | FIXED_EVENT_CONSTRAINT(0xc0, (0x3|(1ULL<<32))), /* INSTRUCTIONS_RETIRED */ |
| 287 | FIXED_EVENT_CONSTRAINT(0x3c, (0x3|(1ULL<<33))), /* UNHALTED_CORE_CYCLES */ |
Stephane Eranian | b690081 | 2009-10-06 16:42:09 +0200 | [diff] [blame] | 288 | EVENT_CONSTRAINT_END |
| 289 | }; |
| 290 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 291 | static u64 intel_pmu_event_map(int hw_event) |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 292 | { |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 293 | return intel_perfmon_event_map[hw_event]; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 294 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 295 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 296 | /* |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 297 | * Generalized hw caching related hw_event table, filled |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 298 | * in on a per model basis. A value of 0 means |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 299 | * 'not supported', -1 means 'hw_event makes no sense on |
| 300 | * this CPU', any other value means the raw hw_event |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 301 | * ID. |
| 302 | */ |
| 303 | |
| 304 | #define C(x) PERF_COUNT_HW_CACHE_##x |
| 305 | |
| 306 | static u64 __read_mostly hw_cache_event_ids |
| 307 | [PERF_COUNT_HW_CACHE_MAX] |
| 308 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 309 | [PERF_COUNT_HW_CACHE_RESULT_MAX]; |
| 310 | |
Peter Zijlstra | 452a339 | 2010-01-27 23:07:48 +0100 | [diff] [blame] | 311 | static __initconst u64 westmere_hw_cache_event_ids |
| 312 | [PERF_COUNT_HW_CACHE_MAX] |
| 313 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 314 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = |
| 315 | { |
| 316 | [ C(L1D) ] = { |
| 317 | [ C(OP_READ) ] = { |
| 318 | [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */ |
| 319 | [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPL */ |
| 320 | }, |
| 321 | [ C(OP_WRITE) ] = { |
| 322 | [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */ |
| 323 | [ C(RESULT_MISS) ] = 0x0251, /* L1D.M_REPL */ |
| 324 | }, |
| 325 | [ C(OP_PREFETCH) ] = { |
| 326 | [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */ |
| 327 | [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */ |
| 328 | }, |
| 329 | }, |
| 330 | [ C(L1I ) ] = { |
| 331 | [ C(OP_READ) ] = { |
| 332 | [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */ |
| 333 | [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */ |
| 334 | }, |
| 335 | [ C(OP_WRITE) ] = { |
| 336 | [ C(RESULT_ACCESS) ] = -1, |
| 337 | [ C(RESULT_MISS) ] = -1, |
| 338 | }, |
| 339 | [ C(OP_PREFETCH) ] = { |
| 340 | [ C(RESULT_ACCESS) ] = 0x0, |
| 341 | [ C(RESULT_MISS) ] = 0x0, |
| 342 | }, |
| 343 | }, |
| 344 | [ C(LL ) ] = { |
| 345 | [ C(OP_READ) ] = { |
| 346 | [ C(RESULT_ACCESS) ] = 0x0324, /* L2_RQSTS.LOADS */ |
| 347 | [ C(RESULT_MISS) ] = 0x0224, /* L2_RQSTS.LD_MISS */ |
| 348 | }, |
| 349 | [ C(OP_WRITE) ] = { |
| 350 | [ C(RESULT_ACCESS) ] = 0x0c24, /* L2_RQSTS.RFOS */ |
| 351 | [ C(RESULT_MISS) ] = 0x0824, /* L2_RQSTS.RFO_MISS */ |
| 352 | }, |
| 353 | [ C(OP_PREFETCH) ] = { |
| 354 | [ C(RESULT_ACCESS) ] = 0x4f2e, /* LLC Reference */ |
| 355 | [ C(RESULT_MISS) ] = 0x412e, /* LLC Misses */ |
| 356 | }, |
| 357 | }, |
| 358 | [ C(DTLB) ] = { |
| 359 | [ C(OP_READ) ] = { |
| 360 | [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */ |
| 361 | [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */ |
| 362 | }, |
| 363 | [ C(OP_WRITE) ] = { |
| 364 | [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */ |
| 365 | [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */ |
| 366 | }, |
| 367 | [ C(OP_PREFETCH) ] = { |
| 368 | [ C(RESULT_ACCESS) ] = 0x0, |
| 369 | [ C(RESULT_MISS) ] = 0x0, |
| 370 | }, |
| 371 | }, |
| 372 | [ C(ITLB) ] = { |
| 373 | [ C(OP_READ) ] = { |
| 374 | [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */ |
| 375 | [ C(RESULT_MISS) ] = 0x0185, /* ITLB_MISSES.ANY */ |
| 376 | }, |
| 377 | [ C(OP_WRITE) ] = { |
| 378 | [ C(RESULT_ACCESS) ] = -1, |
| 379 | [ C(RESULT_MISS) ] = -1, |
| 380 | }, |
| 381 | [ C(OP_PREFETCH) ] = { |
| 382 | [ C(RESULT_ACCESS) ] = -1, |
| 383 | [ C(RESULT_MISS) ] = -1, |
| 384 | }, |
| 385 | }, |
| 386 | [ C(BPU ) ] = { |
| 387 | [ C(OP_READ) ] = { |
| 388 | [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */ |
| 389 | [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */ |
| 390 | }, |
| 391 | [ C(OP_WRITE) ] = { |
| 392 | [ C(RESULT_ACCESS) ] = -1, |
| 393 | [ C(RESULT_MISS) ] = -1, |
| 394 | }, |
| 395 | [ C(OP_PREFETCH) ] = { |
| 396 | [ C(RESULT_ACCESS) ] = -1, |
| 397 | [ C(RESULT_MISS) ] = -1, |
| 398 | }, |
| 399 | }, |
| 400 | }; |
| 401 | |
Hiroshi Shimamoto | db48ccc | 2009-11-12 11:25:34 +0900 | [diff] [blame] | 402 | static __initconst u64 nehalem_hw_cache_event_ids |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 403 | [PERF_COUNT_HW_CACHE_MAX] |
| 404 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 405 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = |
| 406 | { |
| 407 | [ C(L1D) ] = { |
| 408 | [ C(OP_READ) ] = { |
| 409 | [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */ |
| 410 | [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */ |
| 411 | }, |
| 412 | [ C(OP_WRITE) ] = { |
| 413 | [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */ |
| 414 | [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */ |
| 415 | }, |
| 416 | [ C(OP_PREFETCH) ] = { |
| 417 | [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */ |
| 418 | [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */ |
| 419 | }, |
| 420 | }, |
| 421 | [ C(L1I ) ] = { |
| 422 | [ C(OP_READ) ] = { |
Yong Wang | fecc8ac | 2009-06-09 21:15:53 +0800 | [diff] [blame] | 423 | [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */ |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 424 | [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */ |
| 425 | }, |
| 426 | [ C(OP_WRITE) ] = { |
| 427 | [ C(RESULT_ACCESS) ] = -1, |
| 428 | [ C(RESULT_MISS) ] = -1, |
| 429 | }, |
| 430 | [ C(OP_PREFETCH) ] = { |
| 431 | [ C(RESULT_ACCESS) ] = 0x0, |
| 432 | [ C(RESULT_MISS) ] = 0x0, |
| 433 | }, |
| 434 | }, |
Peter Zijlstra | 8be6e8f | 2009-06-11 14:19:11 +0200 | [diff] [blame] | 435 | [ C(LL ) ] = { |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 436 | [ C(OP_READ) ] = { |
| 437 | [ C(RESULT_ACCESS) ] = 0x0324, /* L2_RQSTS.LOADS */ |
| 438 | [ C(RESULT_MISS) ] = 0x0224, /* L2_RQSTS.LD_MISS */ |
| 439 | }, |
| 440 | [ C(OP_WRITE) ] = { |
| 441 | [ C(RESULT_ACCESS) ] = 0x0c24, /* L2_RQSTS.RFOS */ |
| 442 | [ C(RESULT_MISS) ] = 0x0824, /* L2_RQSTS.RFO_MISS */ |
| 443 | }, |
| 444 | [ C(OP_PREFETCH) ] = { |
Peter Zijlstra | 8be6e8f | 2009-06-11 14:19:11 +0200 | [diff] [blame] | 445 | [ C(RESULT_ACCESS) ] = 0x4f2e, /* LLC Reference */ |
| 446 | [ C(RESULT_MISS) ] = 0x412e, /* LLC Misses */ |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 447 | }, |
| 448 | }, |
| 449 | [ C(DTLB) ] = { |
| 450 | [ C(OP_READ) ] = { |
| 451 | [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */ |
| 452 | [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */ |
| 453 | }, |
| 454 | [ C(OP_WRITE) ] = { |
| 455 | [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */ |
| 456 | [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */ |
| 457 | }, |
| 458 | [ C(OP_PREFETCH) ] = { |
| 459 | [ C(RESULT_ACCESS) ] = 0x0, |
| 460 | [ C(RESULT_MISS) ] = 0x0, |
| 461 | }, |
| 462 | }, |
| 463 | [ C(ITLB) ] = { |
| 464 | [ C(OP_READ) ] = { |
| 465 | [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */ |
Yong Wang | fecc8ac | 2009-06-09 21:15:53 +0800 | [diff] [blame] | 466 | [ C(RESULT_MISS) ] = 0x20c8, /* ITLB_MISS_RETIRED */ |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 467 | }, |
| 468 | [ C(OP_WRITE) ] = { |
| 469 | [ C(RESULT_ACCESS) ] = -1, |
| 470 | [ C(RESULT_MISS) ] = -1, |
| 471 | }, |
| 472 | [ C(OP_PREFETCH) ] = { |
| 473 | [ C(RESULT_ACCESS) ] = -1, |
| 474 | [ C(RESULT_MISS) ] = -1, |
| 475 | }, |
| 476 | }, |
| 477 | [ C(BPU ) ] = { |
| 478 | [ C(OP_READ) ] = { |
| 479 | [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */ |
| 480 | [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */ |
| 481 | }, |
| 482 | [ C(OP_WRITE) ] = { |
| 483 | [ C(RESULT_ACCESS) ] = -1, |
| 484 | [ C(RESULT_MISS) ] = -1, |
| 485 | }, |
| 486 | [ C(OP_PREFETCH) ] = { |
| 487 | [ C(RESULT_ACCESS) ] = -1, |
| 488 | [ C(RESULT_MISS) ] = -1, |
| 489 | }, |
| 490 | }, |
| 491 | }; |
| 492 | |
Hiroshi Shimamoto | db48ccc | 2009-11-12 11:25:34 +0900 | [diff] [blame] | 493 | static __initconst u64 core2_hw_cache_event_ids |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 494 | [PERF_COUNT_HW_CACHE_MAX] |
| 495 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 496 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = |
| 497 | { |
Thomas Gleixner | 0312af8 | 2009-06-08 07:42:04 +0200 | [diff] [blame] | 498 | [ C(L1D) ] = { |
| 499 | [ C(OP_READ) ] = { |
| 500 | [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */ |
| 501 | [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */ |
| 502 | }, |
| 503 | [ C(OP_WRITE) ] = { |
| 504 | [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */ |
| 505 | [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */ |
| 506 | }, |
| 507 | [ C(OP_PREFETCH) ] = { |
| 508 | [ C(RESULT_ACCESS) ] = 0x104e, /* L1D_PREFETCH.REQUESTS */ |
| 509 | [ C(RESULT_MISS) ] = 0, |
| 510 | }, |
| 511 | }, |
| 512 | [ C(L1I ) ] = { |
| 513 | [ C(OP_READ) ] = { |
| 514 | [ C(RESULT_ACCESS) ] = 0x0080, /* L1I.READS */ |
| 515 | [ C(RESULT_MISS) ] = 0x0081, /* L1I.MISSES */ |
| 516 | }, |
| 517 | [ C(OP_WRITE) ] = { |
| 518 | [ C(RESULT_ACCESS) ] = -1, |
| 519 | [ C(RESULT_MISS) ] = -1, |
| 520 | }, |
| 521 | [ C(OP_PREFETCH) ] = { |
| 522 | [ C(RESULT_ACCESS) ] = 0, |
| 523 | [ C(RESULT_MISS) ] = 0, |
| 524 | }, |
| 525 | }, |
Peter Zijlstra | 8be6e8f | 2009-06-11 14:19:11 +0200 | [diff] [blame] | 526 | [ C(LL ) ] = { |
Thomas Gleixner | 0312af8 | 2009-06-08 07:42:04 +0200 | [diff] [blame] | 527 | [ C(OP_READ) ] = { |
| 528 | [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */ |
| 529 | [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */ |
| 530 | }, |
| 531 | [ C(OP_WRITE) ] = { |
| 532 | [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */ |
| 533 | [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */ |
| 534 | }, |
| 535 | [ C(OP_PREFETCH) ] = { |
| 536 | [ C(RESULT_ACCESS) ] = 0, |
| 537 | [ C(RESULT_MISS) ] = 0, |
| 538 | }, |
| 539 | }, |
| 540 | [ C(DTLB) ] = { |
| 541 | [ C(OP_READ) ] = { |
| 542 | [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */ |
| 543 | [ C(RESULT_MISS) ] = 0x0208, /* DTLB_MISSES.MISS_LD */ |
| 544 | }, |
| 545 | [ C(OP_WRITE) ] = { |
| 546 | [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */ |
| 547 | [ C(RESULT_MISS) ] = 0x0808, /* DTLB_MISSES.MISS_ST */ |
| 548 | }, |
| 549 | [ C(OP_PREFETCH) ] = { |
| 550 | [ C(RESULT_ACCESS) ] = 0, |
| 551 | [ C(RESULT_MISS) ] = 0, |
| 552 | }, |
| 553 | }, |
| 554 | [ C(ITLB) ] = { |
| 555 | [ C(OP_READ) ] = { |
| 556 | [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */ |
| 557 | [ C(RESULT_MISS) ] = 0x1282, /* ITLBMISSES */ |
| 558 | }, |
| 559 | [ C(OP_WRITE) ] = { |
| 560 | [ C(RESULT_ACCESS) ] = -1, |
| 561 | [ C(RESULT_MISS) ] = -1, |
| 562 | }, |
| 563 | [ C(OP_PREFETCH) ] = { |
| 564 | [ C(RESULT_ACCESS) ] = -1, |
| 565 | [ C(RESULT_MISS) ] = -1, |
| 566 | }, |
| 567 | }, |
| 568 | [ C(BPU ) ] = { |
| 569 | [ C(OP_READ) ] = { |
| 570 | [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */ |
| 571 | [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */ |
| 572 | }, |
| 573 | [ C(OP_WRITE) ] = { |
| 574 | [ C(RESULT_ACCESS) ] = -1, |
| 575 | [ C(RESULT_MISS) ] = -1, |
| 576 | }, |
| 577 | [ C(OP_PREFETCH) ] = { |
| 578 | [ C(RESULT_ACCESS) ] = -1, |
| 579 | [ C(RESULT_MISS) ] = -1, |
| 580 | }, |
| 581 | }, |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 582 | }; |
| 583 | |
Hiroshi Shimamoto | db48ccc | 2009-11-12 11:25:34 +0900 | [diff] [blame] | 584 | static __initconst u64 atom_hw_cache_event_ids |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 585 | [PERF_COUNT_HW_CACHE_MAX] |
| 586 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 587 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = |
| 588 | { |
Thomas Gleixner | ad68922 | 2009-06-08 09:30:41 +0200 | [diff] [blame] | 589 | [ C(L1D) ] = { |
| 590 | [ C(OP_READ) ] = { |
| 591 | [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE.LD */ |
| 592 | [ C(RESULT_MISS) ] = 0, |
| 593 | }, |
| 594 | [ C(OP_WRITE) ] = { |
Yong Wang | fecc8ac | 2009-06-09 21:15:53 +0800 | [diff] [blame] | 595 | [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE.ST */ |
Thomas Gleixner | ad68922 | 2009-06-08 09:30:41 +0200 | [diff] [blame] | 596 | [ C(RESULT_MISS) ] = 0, |
| 597 | }, |
| 598 | [ C(OP_PREFETCH) ] = { |
| 599 | [ C(RESULT_ACCESS) ] = 0x0, |
| 600 | [ C(RESULT_MISS) ] = 0, |
| 601 | }, |
| 602 | }, |
| 603 | [ C(L1I ) ] = { |
| 604 | [ C(OP_READ) ] = { |
Yong Wang | fecc8ac | 2009-06-09 21:15:53 +0800 | [diff] [blame] | 605 | [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */ |
| 606 | [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */ |
Thomas Gleixner | ad68922 | 2009-06-08 09:30:41 +0200 | [diff] [blame] | 607 | }, |
| 608 | [ C(OP_WRITE) ] = { |
| 609 | [ C(RESULT_ACCESS) ] = -1, |
| 610 | [ C(RESULT_MISS) ] = -1, |
| 611 | }, |
| 612 | [ C(OP_PREFETCH) ] = { |
| 613 | [ C(RESULT_ACCESS) ] = 0, |
| 614 | [ C(RESULT_MISS) ] = 0, |
| 615 | }, |
| 616 | }, |
Peter Zijlstra | 8be6e8f | 2009-06-11 14:19:11 +0200 | [diff] [blame] | 617 | [ C(LL ) ] = { |
Thomas Gleixner | ad68922 | 2009-06-08 09:30:41 +0200 | [diff] [blame] | 618 | [ C(OP_READ) ] = { |
| 619 | [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */ |
| 620 | [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */ |
| 621 | }, |
| 622 | [ C(OP_WRITE) ] = { |
| 623 | [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */ |
| 624 | [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */ |
| 625 | }, |
| 626 | [ C(OP_PREFETCH) ] = { |
| 627 | [ C(RESULT_ACCESS) ] = 0, |
| 628 | [ C(RESULT_MISS) ] = 0, |
| 629 | }, |
| 630 | }, |
| 631 | [ C(DTLB) ] = { |
| 632 | [ C(OP_READ) ] = { |
Yong Wang | fecc8ac | 2009-06-09 21:15:53 +0800 | [diff] [blame] | 633 | [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE_LD.MESI (alias) */ |
Thomas Gleixner | ad68922 | 2009-06-08 09:30:41 +0200 | [diff] [blame] | 634 | [ C(RESULT_MISS) ] = 0x0508, /* DTLB_MISSES.MISS_LD */ |
| 635 | }, |
| 636 | [ C(OP_WRITE) ] = { |
Yong Wang | fecc8ac | 2009-06-09 21:15:53 +0800 | [diff] [blame] | 637 | [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE_ST.MESI (alias) */ |
Thomas Gleixner | ad68922 | 2009-06-08 09:30:41 +0200 | [diff] [blame] | 638 | [ C(RESULT_MISS) ] = 0x0608, /* DTLB_MISSES.MISS_ST */ |
| 639 | }, |
| 640 | [ C(OP_PREFETCH) ] = { |
| 641 | [ C(RESULT_ACCESS) ] = 0, |
| 642 | [ C(RESULT_MISS) ] = 0, |
| 643 | }, |
| 644 | }, |
| 645 | [ C(ITLB) ] = { |
| 646 | [ C(OP_READ) ] = { |
| 647 | [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */ |
| 648 | [ C(RESULT_MISS) ] = 0x0282, /* ITLB.MISSES */ |
| 649 | }, |
| 650 | [ C(OP_WRITE) ] = { |
| 651 | [ C(RESULT_ACCESS) ] = -1, |
| 652 | [ C(RESULT_MISS) ] = -1, |
| 653 | }, |
| 654 | [ C(OP_PREFETCH) ] = { |
| 655 | [ C(RESULT_ACCESS) ] = -1, |
| 656 | [ C(RESULT_MISS) ] = -1, |
| 657 | }, |
| 658 | }, |
| 659 | [ C(BPU ) ] = { |
| 660 | [ C(OP_READ) ] = { |
| 661 | [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */ |
| 662 | [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */ |
| 663 | }, |
| 664 | [ C(OP_WRITE) ] = { |
| 665 | [ C(RESULT_ACCESS) ] = -1, |
| 666 | [ C(RESULT_MISS) ] = -1, |
| 667 | }, |
| 668 | [ C(OP_PREFETCH) ] = { |
| 669 | [ C(RESULT_ACCESS) ] = -1, |
| 670 | [ C(RESULT_MISS) ] = -1, |
| 671 | }, |
| 672 | }, |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 673 | }; |
| 674 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 675 | static u64 intel_pmu_raw_event(u64 hw_event) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 676 | { |
Peter Zijlstra | 82bae4f8 | 2009-03-13 12:21:31 +0100 | [diff] [blame] | 677 | #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL |
| 678 | #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL |
Peter Zijlstra | ff99be5 | 2009-05-25 17:39:03 +0200 | [diff] [blame] | 679 | #define CORE_EVNTSEL_EDGE_MASK 0x00040000ULL |
| 680 | #define CORE_EVNTSEL_INV_MASK 0x00800000ULL |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 681 | #define CORE_EVNTSEL_REG_MASK 0xFF000000ULL |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 682 | |
Ingo Molnar | 128f048 | 2009-06-03 22:19:36 +0200 | [diff] [blame] | 683 | #define CORE_EVNTSEL_MASK \ |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 684 | (INTEL_ARCH_EVTSEL_MASK | \ |
| 685 | INTEL_ARCH_UNIT_MASK | \ |
| 686 | INTEL_ARCH_EDGE_MASK | \ |
| 687 | INTEL_ARCH_INV_MASK | \ |
| 688 | INTEL_ARCH_CNT_MASK) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 689 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 690 | return hw_event & CORE_EVNTSEL_MASK; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 691 | } |
| 692 | |
Hiroshi Shimamoto | db48ccc | 2009-11-12 11:25:34 +0900 | [diff] [blame] | 693 | static __initconst u64 amd_hw_cache_event_ids |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 694 | [PERF_COUNT_HW_CACHE_MAX] |
| 695 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 696 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = |
| 697 | { |
| 698 | [ C(L1D) ] = { |
| 699 | [ C(OP_READ) ] = { |
Jaswinder Singh Rajput | f4db43a | 2009-06-13 01:06:21 +0530 | [diff] [blame] | 700 | [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */ |
| 701 | [ C(RESULT_MISS) ] = 0x0041, /* Data Cache Misses */ |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 702 | }, |
| 703 | [ C(OP_WRITE) ] = { |
Jaswinder Singh Rajput | d9f2a5e | 2009-06-20 13:19:25 +0530 | [diff] [blame] | 704 | [ C(RESULT_ACCESS) ] = 0x0142, /* Data Cache Refills :system */ |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 705 | [ C(RESULT_MISS) ] = 0, |
| 706 | }, |
| 707 | [ C(OP_PREFETCH) ] = { |
Jaswinder Singh Rajput | f4db43a | 2009-06-13 01:06:21 +0530 | [diff] [blame] | 708 | [ C(RESULT_ACCESS) ] = 0x0267, /* Data Prefetcher :attempts */ |
| 709 | [ C(RESULT_MISS) ] = 0x0167, /* Data Prefetcher :cancelled */ |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 710 | }, |
| 711 | }, |
| 712 | [ C(L1I ) ] = { |
| 713 | [ C(OP_READ) ] = { |
| 714 | [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction cache fetches */ |
| 715 | [ C(RESULT_MISS) ] = 0x0081, /* Instruction cache misses */ |
| 716 | }, |
| 717 | [ C(OP_WRITE) ] = { |
| 718 | [ C(RESULT_ACCESS) ] = -1, |
| 719 | [ C(RESULT_MISS) ] = -1, |
| 720 | }, |
| 721 | [ C(OP_PREFETCH) ] = { |
Jaswinder Singh Rajput | f4db43a | 2009-06-13 01:06:21 +0530 | [diff] [blame] | 722 | [ C(RESULT_ACCESS) ] = 0x014B, /* Prefetch Instructions :Load */ |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 723 | [ C(RESULT_MISS) ] = 0, |
| 724 | }, |
| 725 | }, |
Peter Zijlstra | 8be6e8f | 2009-06-11 14:19:11 +0200 | [diff] [blame] | 726 | [ C(LL ) ] = { |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 727 | [ C(OP_READ) ] = { |
Jaswinder Singh Rajput | f4db43a | 2009-06-13 01:06:21 +0530 | [diff] [blame] | 728 | [ C(RESULT_ACCESS) ] = 0x037D, /* Requests to L2 Cache :IC+DC */ |
| 729 | [ C(RESULT_MISS) ] = 0x037E, /* L2 Cache Misses : IC+DC */ |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 730 | }, |
| 731 | [ C(OP_WRITE) ] = { |
Jaswinder Singh Rajput | f4db43a | 2009-06-13 01:06:21 +0530 | [diff] [blame] | 732 | [ C(RESULT_ACCESS) ] = 0x017F, /* L2 Fill/Writeback */ |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 733 | [ C(RESULT_MISS) ] = 0, |
| 734 | }, |
| 735 | [ C(OP_PREFETCH) ] = { |
| 736 | [ C(RESULT_ACCESS) ] = 0, |
| 737 | [ C(RESULT_MISS) ] = 0, |
| 738 | }, |
| 739 | }, |
| 740 | [ C(DTLB) ] = { |
| 741 | [ C(OP_READ) ] = { |
Jaswinder Singh Rajput | f4db43a | 2009-06-13 01:06:21 +0530 | [diff] [blame] | 742 | [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */ |
| 743 | [ C(RESULT_MISS) ] = 0x0046, /* L1 DTLB and L2 DLTB Miss */ |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 744 | }, |
| 745 | [ C(OP_WRITE) ] = { |
| 746 | [ C(RESULT_ACCESS) ] = 0, |
| 747 | [ C(RESULT_MISS) ] = 0, |
| 748 | }, |
| 749 | [ C(OP_PREFETCH) ] = { |
| 750 | [ C(RESULT_ACCESS) ] = 0, |
| 751 | [ C(RESULT_MISS) ] = 0, |
| 752 | }, |
| 753 | }, |
| 754 | [ C(ITLB) ] = { |
| 755 | [ C(OP_READ) ] = { |
| 756 | [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction fecthes */ |
| 757 | [ C(RESULT_MISS) ] = 0x0085, /* Instr. fetch ITLB misses */ |
| 758 | }, |
| 759 | [ C(OP_WRITE) ] = { |
| 760 | [ C(RESULT_ACCESS) ] = -1, |
| 761 | [ C(RESULT_MISS) ] = -1, |
| 762 | }, |
| 763 | [ C(OP_PREFETCH) ] = { |
| 764 | [ C(RESULT_ACCESS) ] = -1, |
| 765 | [ C(RESULT_MISS) ] = -1, |
| 766 | }, |
| 767 | }, |
| 768 | [ C(BPU ) ] = { |
| 769 | [ C(OP_READ) ] = { |
| 770 | [ C(RESULT_ACCESS) ] = 0x00c2, /* Retired Branch Instr. */ |
| 771 | [ C(RESULT_MISS) ] = 0x00c3, /* Retired Mispredicted BI */ |
| 772 | }, |
| 773 | [ C(OP_WRITE) ] = { |
| 774 | [ C(RESULT_ACCESS) ] = -1, |
| 775 | [ C(RESULT_MISS) ] = -1, |
| 776 | }, |
| 777 | [ C(OP_PREFETCH) ] = { |
| 778 | [ C(RESULT_ACCESS) ] = -1, |
| 779 | [ C(RESULT_MISS) ] = -1, |
| 780 | }, |
| 781 | }, |
| 782 | }; |
| 783 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 784 | /* |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 785 | * AMD Performance Monitor K7 and later. |
| 786 | */ |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 787 | static const u64 amd_perfmon_event_map[] = |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 788 | { |
Peter Zijlstra | f4dbfa8 | 2009-06-11 14:06:28 +0200 | [diff] [blame] | 789 | [PERF_COUNT_HW_CPU_CYCLES] = 0x0076, |
| 790 | [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0, |
| 791 | [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0080, |
| 792 | [PERF_COUNT_HW_CACHE_MISSES] = 0x0081, |
| 793 | [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4, |
| 794 | [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5, |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 795 | }; |
| 796 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 797 | static u64 amd_pmu_event_map(int hw_event) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 798 | { |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 799 | return amd_perfmon_event_map[hw_event]; |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 800 | } |
| 801 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 802 | static u64 amd_pmu_raw_event(u64 hw_event) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 803 | { |
Peter Zijlstra | 82bae4f8 | 2009-03-13 12:21:31 +0100 | [diff] [blame] | 804 | #define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL |
| 805 | #define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL |
Peter Zijlstra | ff99be5 | 2009-05-25 17:39:03 +0200 | [diff] [blame] | 806 | #define K7_EVNTSEL_EDGE_MASK 0x000040000ULL |
| 807 | #define K7_EVNTSEL_INV_MASK 0x000800000ULL |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 808 | #define K7_EVNTSEL_REG_MASK 0x0FF000000ULL |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 809 | |
| 810 | #define K7_EVNTSEL_MASK \ |
| 811 | (K7_EVNTSEL_EVENT_MASK | \ |
| 812 | K7_EVNTSEL_UNIT_MASK | \ |
Peter Zijlstra | ff99be5 | 2009-05-25 17:39:03 +0200 | [diff] [blame] | 813 | K7_EVNTSEL_EDGE_MASK | \ |
| 814 | K7_EVNTSEL_INV_MASK | \ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 815 | K7_EVNTSEL_REG_MASK) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 816 | |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 817 | return hw_event & K7_EVNTSEL_MASK; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 818 | } |
| 819 | |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 820 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 821 | * Propagate event elapsed time into the generic event. |
| 822 | * Can only be executed on the CPU where the event is active. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 823 | * Returns the delta events processed. |
| 824 | */ |
Robert Richter | 4b7bfd0 | 2009-04-29 12:47:22 +0200 | [diff] [blame] | 825 | static u64 |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 826 | x86_perf_event_update(struct perf_event *event, |
| 827 | struct hw_perf_event *hwc, int idx) |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 828 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 829 | int shift = 64 - x86_pmu.event_bits; |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 830 | u64 prev_raw_count, new_raw_count; |
| 831 | s64 delta; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 832 | |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 833 | if (idx == X86_PMC_IDX_FIXED_BTS) |
| 834 | return 0; |
| 835 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 836 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 837 | * Careful: an NMI might modify the previous event value. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 838 | * |
| 839 | * Our tactic to handle this is to first atomically read and |
| 840 | * exchange a new raw count - then add that new-prev delta |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 841 | * count to the generic event atomically: |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 842 | */ |
| 843 | again: |
| 844 | prev_raw_count = atomic64_read(&hwc->prev_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 845 | rdmsrl(hwc->event_base + idx, new_raw_count); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 846 | |
| 847 | if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count, |
| 848 | new_raw_count) != prev_raw_count) |
| 849 | goto again; |
| 850 | |
| 851 | /* |
| 852 | * Now we have the new raw value and have updated the prev |
| 853 | * timestamp already. We can now calculate the elapsed delta |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 854 | * (event-)time and add that to the generic event. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 855 | * |
| 856 | * Careful, not all hw sign-extends above the physical width |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 857 | * of the count. |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 858 | */ |
Peter Zijlstra | ec3232b | 2009-05-13 09:45:19 +0200 | [diff] [blame] | 859 | delta = (new_raw_count << shift) - (prev_raw_count << shift); |
| 860 | delta >>= shift; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 861 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 862 | atomic64_add(delta, &event->count); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 863 | atomic64_sub(delta, &hwc->period_left); |
Robert Richter | 4b7bfd0 | 2009-04-29 12:47:22 +0200 | [diff] [blame] | 864 | |
| 865 | return new_raw_count; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 866 | } |
| 867 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 868 | static atomic_t active_events; |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 869 | static DEFINE_MUTEX(pmc_reserve_mutex); |
| 870 | |
| 871 | static bool reserve_pmc_hardware(void) |
| 872 | { |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 873 | #ifdef CONFIG_X86_LOCAL_APIC |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 874 | int i; |
| 875 | |
| 876 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 877 | disable_lapic_nmi_watchdog(); |
| 878 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 879 | for (i = 0; i < x86_pmu.num_events; i++) { |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 880 | if (!reserve_perfctr_nmi(x86_pmu.perfctr + i)) |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 881 | goto perfctr_fail; |
| 882 | } |
| 883 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 884 | for (i = 0; i < x86_pmu.num_events; i++) { |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 885 | if (!reserve_evntsel_nmi(x86_pmu.eventsel + i)) |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 886 | goto eventsel_fail; |
| 887 | } |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 888 | #endif |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 889 | |
| 890 | return true; |
| 891 | |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 892 | #ifdef CONFIG_X86_LOCAL_APIC |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 893 | eventsel_fail: |
| 894 | for (i--; i >= 0; i--) |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 895 | release_evntsel_nmi(x86_pmu.eventsel + i); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 896 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 897 | i = x86_pmu.num_events; |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 898 | |
| 899 | perfctr_fail: |
| 900 | for (i--; i >= 0; i--) |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 901 | release_perfctr_nmi(x86_pmu.perfctr + i); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 902 | |
| 903 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 904 | enable_lapic_nmi_watchdog(); |
| 905 | |
| 906 | return false; |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 907 | #endif |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | static void release_pmc_hardware(void) |
| 911 | { |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 912 | #ifdef CONFIG_X86_LOCAL_APIC |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 913 | int i; |
| 914 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 915 | for (i = 0; i < x86_pmu.num_events; i++) { |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 916 | release_perfctr_nmi(x86_pmu.perfctr + i); |
| 917 | release_evntsel_nmi(x86_pmu.eventsel + i); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 921 | enable_lapic_nmi_watchdog(); |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 922 | #endif |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 923 | } |
| 924 | |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 925 | static inline bool bts_available(void) |
| 926 | { |
| 927 | return x86_pmu.enable_bts != NULL; |
| 928 | } |
| 929 | |
| 930 | static inline void init_debug_store_on_cpu(int cpu) |
| 931 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 932 | struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 933 | |
| 934 | if (!ds) |
| 935 | return; |
| 936 | |
| 937 | wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, |
markus.t.metzger@intel.com | 596da17 | 2009-09-02 16:04:47 +0200 | [diff] [blame] | 938 | (u32)((u64)(unsigned long)ds), |
| 939 | (u32)((u64)(unsigned long)ds >> 32)); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | static inline void fini_debug_store_on_cpu(int cpu) |
| 943 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 944 | if (!per_cpu(cpu_hw_events, cpu).ds) |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 945 | return; |
| 946 | |
| 947 | wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0); |
| 948 | } |
| 949 | |
| 950 | static void release_bts_hardware(void) |
| 951 | { |
| 952 | int cpu; |
| 953 | |
| 954 | if (!bts_available()) |
| 955 | return; |
| 956 | |
| 957 | get_online_cpus(); |
| 958 | |
| 959 | for_each_online_cpu(cpu) |
| 960 | fini_debug_store_on_cpu(cpu); |
| 961 | |
| 962 | for_each_possible_cpu(cpu) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 963 | struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 964 | |
| 965 | if (!ds) |
| 966 | continue; |
| 967 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 968 | per_cpu(cpu_hw_events, cpu).ds = NULL; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 969 | |
markus.t.metzger@intel.com | 596da17 | 2009-09-02 16:04:47 +0200 | [diff] [blame] | 970 | kfree((void *)(unsigned long)ds->bts_buffer_base); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 971 | kfree(ds); |
| 972 | } |
| 973 | |
| 974 | put_online_cpus(); |
| 975 | } |
| 976 | |
| 977 | static int reserve_bts_hardware(void) |
| 978 | { |
| 979 | int cpu, err = 0; |
| 980 | |
| 981 | if (!bts_available()) |
markus.t.metzger@intel.com | 747b50a | 2009-09-02 16:04:46 +0200 | [diff] [blame] | 982 | return 0; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 983 | |
| 984 | get_online_cpus(); |
| 985 | |
| 986 | for_each_possible_cpu(cpu) { |
| 987 | struct debug_store *ds; |
| 988 | void *buffer; |
| 989 | |
| 990 | err = -ENOMEM; |
| 991 | buffer = kzalloc(BTS_BUFFER_SIZE, GFP_KERNEL); |
| 992 | if (unlikely(!buffer)) |
| 993 | break; |
| 994 | |
| 995 | ds = kzalloc(sizeof(*ds), GFP_KERNEL); |
| 996 | if (unlikely(!ds)) { |
| 997 | kfree(buffer); |
| 998 | break; |
| 999 | } |
| 1000 | |
markus.t.metzger@intel.com | 596da17 | 2009-09-02 16:04:47 +0200 | [diff] [blame] | 1001 | ds->bts_buffer_base = (u64)(unsigned long)buffer; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1002 | ds->bts_index = ds->bts_buffer_base; |
| 1003 | ds->bts_absolute_maximum = |
| 1004 | ds->bts_buffer_base + BTS_BUFFER_SIZE; |
| 1005 | ds->bts_interrupt_threshold = |
| 1006 | ds->bts_absolute_maximum - BTS_OVFL_TH; |
| 1007 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1008 | per_cpu(cpu_hw_events, cpu).ds = ds; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1009 | err = 0; |
| 1010 | } |
| 1011 | |
| 1012 | if (err) |
| 1013 | release_bts_hardware(); |
| 1014 | else { |
| 1015 | for_each_online_cpu(cpu) |
| 1016 | init_debug_store_on_cpu(cpu); |
| 1017 | } |
| 1018 | |
| 1019 | put_online_cpus(); |
| 1020 | |
| 1021 | return err; |
| 1022 | } |
| 1023 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1024 | static void hw_perf_event_destroy(struct perf_event *event) |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 1025 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1026 | if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) { |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 1027 | release_pmc_hardware(); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1028 | release_bts_hardware(); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 1029 | mutex_unlock(&pmc_reserve_mutex); |
| 1030 | } |
| 1031 | } |
| 1032 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 1033 | static inline int x86_pmu_initialized(void) |
| 1034 | { |
| 1035 | return x86_pmu.handle_irq != NULL; |
| 1036 | } |
| 1037 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 1038 | static inline int |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1039 | 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] | 1040 | { |
| 1041 | unsigned int cache_type, cache_op, cache_result; |
| 1042 | u64 config, val; |
| 1043 | |
| 1044 | config = attr->config; |
| 1045 | |
| 1046 | cache_type = (config >> 0) & 0xff; |
| 1047 | if (cache_type >= PERF_COUNT_HW_CACHE_MAX) |
| 1048 | return -EINVAL; |
| 1049 | |
| 1050 | cache_op = (config >> 8) & 0xff; |
| 1051 | if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX) |
| 1052 | return -EINVAL; |
| 1053 | |
| 1054 | cache_result = (config >> 16) & 0xff; |
| 1055 | if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX) |
| 1056 | return -EINVAL; |
| 1057 | |
| 1058 | val = hw_cache_event_ids[cache_type][cache_op][cache_result]; |
| 1059 | |
| 1060 | if (val == 0) |
| 1061 | return -ENOENT; |
| 1062 | |
| 1063 | if (val == -1) |
| 1064 | return -EINVAL; |
| 1065 | |
| 1066 | hwc->config |= val; |
| 1067 | |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1071 | static void intel_pmu_enable_bts(u64 config) |
| 1072 | { |
| 1073 | unsigned long debugctlmsr; |
| 1074 | |
| 1075 | debugctlmsr = get_debugctlmsr(); |
| 1076 | |
| 1077 | debugctlmsr |= X86_DEBUGCTL_TR; |
| 1078 | debugctlmsr |= X86_DEBUGCTL_BTS; |
| 1079 | debugctlmsr |= X86_DEBUGCTL_BTINT; |
| 1080 | |
| 1081 | if (!(config & ARCH_PERFMON_EVENTSEL_OS)) |
| 1082 | debugctlmsr |= X86_DEBUGCTL_BTS_OFF_OS; |
| 1083 | |
| 1084 | if (!(config & ARCH_PERFMON_EVENTSEL_USR)) |
| 1085 | debugctlmsr |= X86_DEBUGCTL_BTS_OFF_USR; |
| 1086 | |
| 1087 | update_debugctlmsr(debugctlmsr); |
| 1088 | } |
| 1089 | |
| 1090 | static void intel_pmu_disable_bts(void) |
| 1091 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1092 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1093 | unsigned long debugctlmsr; |
| 1094 | |
| 1095 | if (!cpuc->ds) |
| 1096 | return; |
| 1097 | |
| 1098 | debugctlmsr = get_debugctlmsr(); |
| 1099 | |
| 1100 | debugctlmsr &= |
| 1101 | ~(X86_DEBUGCTL_TR | X86_DEBUGCTL_BTS | X86_DEBUGCTL_BTINT | |
| 1102 | X86_DEBUGCTL_BTS_OFF_OS | X86_DEBUGCTL_BTS_OFF_USR); |
| 1103 | |
| 1104 | update_debugctlmsr(debugctlmsr); |
| 1105 | } |
| 1106 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1107 | /* |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame] | 1108 | * Setup the hardware configuration for a given attr_type |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1109 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1110 | static int __hw_perf_event_init(struct perf_event *event) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1111 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1112 | struct perf_event_attr *attr = &event->attr; |
| 1113 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 1114 | u64 config; |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 1115 | int err; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1116 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 1117 | if (!x86_pmu_initialized()) |
| 1118 | return -ENODEV; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1119 | |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 1120 | err = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1121 | if (!atomic_inc_not_zero(&active_events)) { |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 1122 | mutex_lock(&pmc_reserve_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1123 | if (atomic_read(&active_events) == 0) { |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1124 | if (!reserve_pmc_hardware()) |
| 1125 | err = -EBUSY; |
| 1126 | else |
markus.t.metzger@intel.com | 747b50a | 2009-09-02 16:04:46 +0200 | [diff] [blame] | 1127 | err = reserve_bts_hardware(); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1128 | } |
| 1129 | if (!err) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1130 | atomic_inc(&active_events); |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 1131 | mutex_unlock(&pmc_reserve_mutex); |
| 1132 | } |
| 1133 | if (err) |
| 1134 | return err; |
| 1135 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1136 | event->destroy = hw_perf_event_destroy; |
Peter Zijlstra | a1792cdac | 2009-09-09 10:04:47 +0200 | [diff] [blame] | 1137 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1138 | /* |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 1139 | * Generate PMC IRQs: |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1140 | * (keep 'enabled' bit clear for now) |
| 1141 | */ |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 1142 | hwc->config = ARCH_PERFMON_EVENTSEL_INT; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1143 | |
Stephane Eranian | b690081 | 2009-10-06 16:42:09 +0200 | [diff] [blame] | 1144 | hwc->idx = -1; |
| 1145 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1146 | /* |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 1147 | * Count user and OS events unless requested not to. |
| 1148 | */ |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame] | 1149 | if (!attr->exclude_user) |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 1150 | hwc->config |= ARCH_PERFMON_EVENTSEL_USR; |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame] | 1151 | if (!attr->exclude_kernel) |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 1152 | hwc->config |= ARCH_PERFMON_EVENTSEL_OS; |
| 1153 | |
Peter Zijlstra | bd2b5b1 | 2009-06-10 13:40:57 +0200 | [diff] [blame] | 1154 | if (!hwc->sample_period) { |
Peter Zijlstra | b23f332 | 2009-06-02 15:13:03 +0200 | [diff] [blame] | 1155 | hwc->sample_period = x86_pmu.max_period; |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1156 | hwc->last_period = hwc->sample_period; |
Peter Zijlstra | bd2b5b1 | 2009-06-10 13:40:57 +0200 | [diff] [blame] | 1157 | atomic64_set(&hwc->period_left, hwc->sample_period); |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1158 | } else { |
| 1159 | /* |
| 1160 | * If we have a PMU initialized but no APIC |
| 1161 | * interrupts, we cannot sample hardware |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1162 | * events (user-space has to fall back and |
| 1163 | * sample via a hrtimer based software event): |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 1164 | */ |
| 1165 | if (!x86_pmu.apic) |
| 1166 | return -EOPNOTSUPP; |
Peter Zijlstra | bd2b5b1 | 2009-06-10 13:40:57 +0200 | [diff] [blame] | 1167 | } |
Ingo Molnar | d2517a4 | 2009-05-17 10:04:45 +0200 | [diff] [blame] | 1168 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1169 | /* |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 1170 | * Raw hw_event type provide the config in the hw_event structure |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1171 | */ |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 1172 | if (attr->type == PERF_TYPE_RAW) { |
| 1173 | hwc->config |= x86_pmu.raw_event(attr->config); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 1174 | return 0; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1175 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1176 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 1177 | if (attr->type == PERF_TYPE_HW_CACHE) |
| 1178 | return set_ext_hw_attr(hwc, attr); |
| 1179 | |
| 1180 | if (attr->config >= x86_pmu.max_events) |
| 1181 | return -EINVAL; |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 1182 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 1183 | /* |
| 1184 | * The generic map: |
| 1185 | */ |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 1186 | config = x86_pmu.event_map(attr->config); |
| 1187 | |
| 1188 | if (config == 0) |
| 1189 | return -ENOENT; |
| 1190 | |
| 1191 | if (config == -1LL) |
| 1192 | return -EINVAL; |
| 1193 | |
markus.t.metzger@intel.com | 747b50a | 2009-09-02 16:04:46 +0200 | [diff] [blame] | 1194 | /* |
| 1195 | * Branch tracing: |
| 1196 | */ |
| 1197 | if ((attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS) && |
markus.t.metzger@intel.com | 1653192 | 2009-09-02 16:04:48 +0200 | [diff] [blame] | 1198 | (hwc->sample_period == 1)) { |
| 1199 | /* BTS is not supported by this architecture. */ |
| 1200 | if (!bts_available()) |
| 1201 | return -EOPNOTSUPP; |
| 1202 | |
| 1203 | /* BTS is currently only allowed for user-mode. */ |
| 1204 | if (hwc->config & ARCH_PERFMON_EVENTSEL_OS) |
| 1205 | return -EOPNOTSUPP; |
| 1206 | } |
markus.t.metzger@intel.com | 747b50a | 2009-09-02 16:04:46 +0200 | [diff] [blame] | 1207 | |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 1208 | hwc->config |= config; |
Peter Zijlstra | 4e935e4 | 2009-03-30 19:07:16 +0200 | [diff] [blame] | 1209 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1210 | return 0; |
| 1211 | } |
| 1212 | |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1213 | static void p6_pmu_disable_all(void) |
| 1214 | { |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 1215 | u64 val; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1216 | |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1217 | /* p6 only has one enable register */ |
| 1218 | rdmsrl(MSR_P6_EVNTSEL0, val); |
| 1219 | val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE; |
| 1220 | wrmsrl(MSR_P6_EVNTSEL0, val); |
| 1221 | } |
| 1222 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1223 | static void intel_pmu_disable_all(void) |
Thomas Gleixner | 4ac1329 | 2008-12-09 21:43:39 +0100 | [diff] [blame] | 1224 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1225 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1226 | |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 1227 | wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1228 | |
| 1229 | if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask)) |
| 1230 | intel_pmu_disable_bts(); |
Thomas Gleixner | 4ac1329 | 2008-12-09 21:43:39 +0100 | [diff] [blame] | 1231 | } |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1232 | |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 1233 | static void x86_pmu_disable_all(void) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1234 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1235 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1236 | int idx; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1237 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1238 | for (idx = 0; idx < x86_pmu.num_events; idx++) { |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1239 | u64 val; |
| 1240 | |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 1241 | if (!test_bit(idx, cpuc->active_mask)) |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 1242 | continue; |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 1243 | rdmsrl(x86_pmu.eventsel + idx, val); |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 1244 | if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE)) |
| 1245 | continue; |
| 1246 | val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE; |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 1247 | wrmsrl(x86_pmu.eventsel + idx, val); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1248 | } |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1249 | } |
| 1250 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1251 | void hw_perf_disable(void) |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1252 | { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1253 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 1254 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 1255 | if (!x86_pmu_initialized()) |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1256 | return; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1257 | |
Peter Zijlstra | 1a6e21f | 2010-01-27 23:07:47 +0100 | [diff] [blame] | 1258 | if (!cpuc->enabled) |
| 1259 | return; |
| 1260 | |
| 1261 | cpuc->n_added = 0; |
| 1262 | cpuc->enabled = 0; |
| 1263 | barrier(); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1264 | |
| 1265 | x86_pmu.disable_all(); |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1266 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1267 | |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1268 | static void p6_pmu_enable_all(void) |
| 1269 | { |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1270 | unsigned long val; |
| 1271 | |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1272 | /* p6 only has one enable register */ |
| 1273 | rdmsrl(MSR_P6_EVNTSEL0, val); |
| 1274 | val |= ARCH_PERFMON_EVENTSEL0_ENABLE; |
| 1275 | wrmsrl(MSR_P6_EVNTSEL0, val); |
| 1276 | } |
| 1277 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1278 | static void intel_pmu_enable_all(void) |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1279 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1280 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1281 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1282 | wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1283 | |
| 1284 | if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1285 | struct perf_event *event = |
| 1286 | cpuc->events[X86_PMC_IDX_FIXED_BTS]; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1287 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1288 | if (WARN_ON_ONCE(!event)) |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1289 | return; |
| 1290 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1291 | intel_pmu_enable_bts(event->hw.config); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1292 | } |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 1293 | } |
| 1294 | |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 1295 | static void x86_pmu_enable_all(void) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1296 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1297 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1298 | int idx; |
| 1299 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1300 | for (idx = 0; idx < x86_pmu.num_events; idx++) { |
| 1301 | struct perf_event *event = cpuc->events[idx]; |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 1302 | u64 val; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1303 | |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 1304 | if (!test_bit(idx, cpuc->active_mask)) |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 1305 | continue; |
Peter Zijlstra | 984b838 | 2009-07-10 09:59:56 +0200 | [diff] [blame] | 1306 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1307 | val = event->hw.config; |
Robert Richter | 4295ee6 | 2009-04-29 12:47:01 +0200 | [diff] [blame] | 1308 | val |= ARCH_PERFMON_EVENTSEL0_ENABLE; |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 1309 | wrmsrl(x86_pmu.eventsel + idx, val); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1310 | } |
| 1311 | } |
| 1312 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1313 | static const struct pmu pmu; |
| 1314 | |
| 1315 | static inline int is_x86_event(struct perf_event *event) |
| 1316 | { |
| 1317 | return event->pmu == &pmu; |
| 1318 | } |
| 1319 | |
| 1320 | static int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign) |
| 1321 | { |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1322 | struct event_constraint *c, *constraints[X86_PMC_IDX_MAX]; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1323 | unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
Peter Zijlstra | c933c1a | 2010-01-22 16:40:12 +0100 | [diff] [blame] | 1324 | int i, j, w, wmax, num = 0; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1325 | struct hw_perf_event *hwc; |
| 1326 | |
| 1327 | bitmap_zero(used_mask, X86_PMC_IDX_MAX); |
| 1328 | |
| 1329 | for (i = 0; i < n; i++) { |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1330 | constraints[i] = |
| 1331 | x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | /* |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 1335 | * fastpath, try to reuse previous register |
| 1336 | */ |
Peter Zijlstra | c933c1a | 2010-01-22 16:40:12 +0100 | [diff] [blame] | 1337 | for (i = 0; i < n; i++) { |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 1338 | hwc = &cpuc->event_list[i]->hw; |
Peter Zijlstra | 81269a0 | 2010-01-22 14:55:22 +0100 | [diff] [blame] | 1339 | c = constraints[i]; |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 1340 | |
| 1341 | /* never assigned */ |
| 1342 | if (hwc->idx == -1) |
| 1343 | break; |
| 1344 | |
| 1345 | /* constraint still honored */ |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1346 | if (!test_bit(hwc->idx, c->idxmsk)) |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 1347 | break; |
| 1348 | |
| 1349 | /* not already used */ |
| 1350 | if (test_bit(hwc->idx, used_mask)) |
| 1351 | break; |
| 1352 | |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 1353 | set_bit(hwc->idx, used_mask); |
| 1354 | if (assign) |
| 1355 | assign[i] = hwc->idx; |
| 1356 | } |
Peter Zijlstra | c933c1a | 2010-01-22 16:40:12 +0100 | [diff] [blame] | 1357 | if (i == n) |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 1358 | goto done; |
| 1359 | |
| 1360 | /* |
| 1361 | * begin slow path |
| 1362 | */ |
| 1363 | |
| 1364 | bitmap_zero(used_mask, X86_PMC_IDX_MAX); |
| 1365 | |
| 1366 | /* |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1367 | * weight = number of possible counters |
| 1368 | * |
| 1369 | * 1 = most constrained, only works on one counter |
| 1370 | * wmax = least constrained, works on any counter |
| 1371 | * |
| 1372 | * assign events to counters starting with most |
| 1373 | * constrained events. |
| 1374 | */ |
| 1375 | wmax = x86_pmu.num_events; |
| 1376 | |
| 1377 | /* |
| 1378 | * when fixed event counters are present, |
| 1379 | * wmax is incremented by 1 to account |
| 1380 | * for one more choice |
| 1381 | */ |
| 1382 | if (x86_pmu.num_events_fixed) |
| 1383 | wmax++; |
| 1384 | |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 1385 | for (w = 1, num = n; num && w <= wmax; w++) { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1386 | /* for each event */ |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 1387 | for (i = 0; num && i < n; i++) { |
Peter Zijlstra | 81269a0 | 2010-01-22 14:55:22 +0100 | [diff] [blame] | 1388 | c = constraints[i]; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1389 | hwc = &cpuc->event_list[i]->hw; |
| 1390 | |
Peter Zijlstra | 272d30b | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1391 | if (c->weight != w) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1392 | continue; |
| 1393 | |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 1394 | for_each_bit(j, c->idxmsk, X86_PMC_IDX_MAX) { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1395 | if (!test_bit(j, used_mask)) |
| 1396 | break; |
| 1397 | } |
| 1398 | |
| 1399 | if (j == X86_PMC_IDX_MAX) |
| 1400 | break; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1401 | |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 1402 | set_bit(j, used_mask); |
| 1403 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1404 | if (assign) |
| 1405 | assign[i] = j; |
| 1406 | num--; |
| 1407 | } |
| 1408 | } |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 1409 | done: |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1410 | /* |
| 1411 | * scheduling failed or is just a simulation, |
| 1412 | * free resources if necessary |
| 1413 | */ |
| 1414 | if (!assign || num) { |
| 1415 | for (i = 0; i < n; i++) { |
| 1416 | if (x86_pmu.put_event_constraints) |
| 1417 | x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]); |
| 1418 | } |
| 1419 | } |
| 1420 | return num ? -ENOSPC : 0; |
| 1421 | } |
| 1422 | |
| 1423 | /* |
| 1424 | * dogrp: true if must collect siblings events (group) |
| 1425 | * returns total number of events and error code |
| 1426 | */ |
| 1427 | static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp) |
| 1428 | { |
| 1429 | struct perf_event *event; |
| 1430 | int n, max_count; |
| 1431 | |
| 1432 | max_count = x86_pmu.num_events + x86_pmu.num_events_fixed; |
| 1433 | |
| 1434 | /* current number of events already accepted */ |
| 1435 | n = cpuc->n_events; |
| 1436 | |
| 1437 | if (is_x86_event(leader)) { |
| 1438 | if (n >= max_count) |
| 1439 | return -ENOSPC; |
| 1440 | cpuc->event_list[n] = leader; |
| 1441 | n++; |
| 1442 | } |
| 1443 | if (!dogrp) |
| 1444 | return n; |
| 1445 | |
| 1446 | list_for_each_entry(event, &leader->sibling_list, group_entry) { |
| 1447 | if (!is_x86_event(event) || |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 1448 | event->state <= PERF_EVENT_STATE_OFF) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1449 | continue; |
| 1450 | |
| 1451 | if (n >= max_count) |
| 1452 | return -ENOSPC; |
| 1453 | |
| 1454 | cpuc->event_list[n] = event; |
| 1455 | n++; |
| 1456 | } |
| 1457 | return n; |
| 1458 | } |
| 1459 | |
| 1460 | |
| 1461 | static inline void x86_assign_hw_event(struct perf_event *event, |
| 1462 | struct hw_perf_event *hwc, int idx) |
| 1463 | { |
| 1464 | hwc->idx = idx; |
| 1465 | |
| 1466 | if (hwc->idx == X86_PMC_IDX_FIXED_BTS) { |
| 1467 | hwc->config_base = 0; |
| 1468 | hwc->event_base = 0; |
| 1469 | } else if (hwc->idx >= X86_PMC_IDX_FIXED) { |
| 1470 | hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL; |
| 1471 | /* |
| 1472 | * We set it so that event_base + idx in wrmsr/rdmsr maps to |
| 1473 | * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2: |
| 1474 | */ |
| 1475 | hwc->event_base = |
| 1476 | MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED; |
| 1477 | } else { |
| 1478 | hwc->config_base = x86_pmu.eventsel; |
| 1479 | hwc->event_base = x86_pmu.perfctr; |
| 1480 | } |
| 1481 | } |
| 1482 | |
Peter Zijlstra | 2e84187 | 2010-01-25 15:58:43 +0100 | [diff] [blame] | 1483 | static void __x86_pmu_disable(struct perf_event *event, struct cpu_hw_events *cpuc); |
| 1484 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1485 | void hw_perf_enable(void) |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1486 | { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1487 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 1488 | struct perf_event *event; |
| 1489 | struct hw_perf_event *hwc; |
| 1490 | int i; |
| 1491 | |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 1492 | if (!x86_pmu_initialized()) |
Ingo Molnar | 2b9ff0d | 2008-12-14 18:36:30 +0100 | [diff] [blame] | 1493 | return; |
Peter Zijlstra | 1a6e21f | 2010-01-27 23:07:47 +0100 | [diff] [blame] | 1494 | |
| 1495 | if (cpuc->enabled) |
| 1496 | return; |
| 1497 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1498 | if (cpuc->n_added) { |
| 1499 | /* |
| 1500 | * apply assignment obtained either from |
| 1501 | * hw_perf_group_sched_in() or x86_pmu_enable() |
| 1502 | * |
| 1503 | * step1: save events moving to new counters |
| 1504 | * step2: reprogram moved events into new counters |
| 1505 | */ |
| 1506 | for (i = 0; i < cpuc->n_events; i++) { |
| 1507 | |
| 1508 | event = cpuc->event_list[i]; |
| 1509 | hwc = &event->hw; |
| 1510 | |
| 1511 | if (hwc->idx == -1 || hwc->idx == cpuc->assign[i]) |
| 1512 | continue; |
| 1513 | |
Peter Zijlstra | 2e84187 | 2010-01-25 15:58:43 +0100 | [diff] [blame] | 1514 | __x86_pmu_disable(event, cpuc); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1515 | |
| 1516 | hwc->idx = -1; |
| 1517 | } |
| 1518 | |
| 1519 | for (i = 0; i < cpuc->n_events; i++) { |
| 1520 | |
| 1521 | event = cpuc->event_list[i]; |
| 1522 | hwc = &event->hw; |
| 1523 | |
| 1524 | if (hwc->idx == -1) { |
| 1525 | x86_assign_hw_event(event, hwc, cpuc->assign[i]); |
| 1526 | x86_perf_event_set_period(event, hwc, hwc->idx); |
| 1527 | } |
| 1528 | /* |
| 1529 | * need to mark as active because x86_pmu_disable() |
| 1530 | * clear active_mask and eventsp[] yet it preserves |
| 1531 | * idx |
| 1532 | */ |
| 1533 | set_bit(hwc->idx, cpuc->active_mask); |
| 1534 | cpuc->events[hwc->idx] = event; |
| 1535 | |
| 1536 | x86_pmu.enable(hwc, hwc->idx); |
| 1537 | perf_event_update_userpage(event); |
| 1538 | } |
| 1539 | cpuc->n_added = 0; |
| 1540 | perf_events_lapic_init(); |
| 1541 | } |
Peter Zijlstra | 1a6e21f | 2010-01-27 23:07:47 +0100 | [diff] [blame] | 1542 | |
| 1543 | cpuc->enabled = 1; |
| 1544 | barrier(); |
| 1545 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1546 | x86_pmu.enable_all(); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1547 | } |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1548 | |
Robert Richter | 19d84da | 2009-04-29 12:47:25 +0200 | [diff] [blame] | 1549 | static inline u64 intel_pmu_get_status(void) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1550 | { |
| 1551 | u64 status; |
| 1552 | |
| 1553 | rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status); |
| 1554 | |
| 1555 | return status; |
| 1556 | } |
| 1557 | |
Robert Richter | dee5d90 | 2009-04-29 12:47:07 +0200 | [diff] [blame] | 1558 | static inline void intel_pmu_ack_status(u64 ack) |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1559 | { |
| 1560 | wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack); |
| 1561 | } |
| 1562 | |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 1563 | 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] | 1564 | { |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1565 | (void)checking_wrmsrl(hwc->config_base + idx, |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 1566 | hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE); |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1567 | } |
| 1568 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1569 | 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] | 1570 | { |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1571 | (void)checking_wrmsrl(hwc->config_base + idx, hwc->config); |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 1572 | } |
| 1573 | |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 1574 | static inline void |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1575 | intel_pmu_disable_fixed(struct hw_perf_event *hwc, int __idx) |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1576 | { |
| 1577 | int idx = __idx - X86_PMC_IDX_FIXED; |
| 1578 | u64 ctrl_val, mask; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1579 | |
| 1580 | mask = 0xfULL << (idx * 4); |
| 1581 | |
| 1582 | rdmsrl(hwc->config_base, ctrl_val); |
| 1583 | ctrl_val &= ~mask; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1584 | (void)checking_wrmsrl(hwc->config_base, ctrl_val); |
| 1585 | } |
| 1586 | |
| 1587 | static inline void |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1588 | p6_pmu_disable_event(struct hw_perf_event *hwc, int idx) |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1589 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1590 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 1591 | u64 val = P6_NOP_EVENT; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1592 | |
Peter Zijlstra | 9c74fb5 | 2009-07-08 10:21:41 +0200 | [diff] [blame] | 1593 | if (cpuc->enabled) |
| 1594 | val |= ARCH_PERFMON_EVENTSEL0_ENABLE; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1595 | |
| 1596 | (void)checking_wrmsrl(hwc->config_base + idx, val); |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | static inline void |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1600 | intel_pmu_disable_event(struct hw_perf_event *hwc, int idx) |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 1601 | { |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1602 | if (unlikely(idx == X86_PMC_IDX_FIXED_BTS)) { |
| 1603 | intel_pmu_disable_bts(); |
| 1604 | return; |
| 1605 | } |
| 1606 | |
Robert Richter | d436989 | 2009-04-29 12:47:19 +0200 | [diff] [blame] | 1607 | if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) { |
| 1608 | intel_pmu_disable_fixed(hwc, idx); |
| 1609 | return; |
| 1610 | } |
| 1611 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1612 | x86_pmu_disable_event(hwc, idx); |
Robert Richter | d436989 | 2009-04-29 12:47:19 +0200 | [diff] [blame] | 1613 | } |
| 1614 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 1615 | static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1616 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1617 | /* |
| 1618 | * Set the next IRQ period, based on the hwc->period_left value. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1619 | * To be called with the event disabled in hw: |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1620 | */ |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1621 | static int |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1622 | x86_perf_event_set_period(struct perf_event *event, |
| 1623 | struct hw_perf_event *hwc, int idx) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1624 | { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1625 | s64 left = atomic64_read(&hwc->period_left); |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1626 | s64 period = hwc->sample_period; |
| 1627 | int err, ret = 0; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1628 | |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1629 | if (idx == X86_PMC_IDX_FIXED_BTS) |
| 1630 | return 0; |
| 1631 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1632 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1633 | * If we are way outside a reasonable range then just skip forward: |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1634 | */ |
| 1635 | if (unlikely(left <= -period)) { |
| 1636 | left = period; |
| 1637 | atomic64_set(&hwc->period_left, left); |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1638 | hwc->last_period = period; |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1639 | ret = 1; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | if (unlikely(left <= 0)) { |
| 1643 | left += period; |
| 1644 | atomic64_set(&hwc->period_left, left); |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 1645 | hwc->last_period = period; |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1646 | ret = 1; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1647 | } |
Ingo Molnar | 1c80f4b | 2009-05-15 08:25:22 +0200 | [diff] [blame] | 1648 | /* |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 1649 | * 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] | 1650 | */ |
| 1651 | if (unlikely(left < 2)) |
| 1652 | left = 2; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1653 | |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1654 | if (left > x86_pmu.max_period) |
| 1655 | left = x86_pmu.max_period; |
| 1656 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 1657 | per_cpu(pmc_prev_left[idx], smp_processor_id()) = left; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1658 | |
| 1659 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1660 | * The hw event starts counting from this event offset, |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1661 | * mark it to be able to extra future deltas: |
| 1662 | */ |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1663 | atomic64_set(&hwc->prev_count, (u64)-left); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1664 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1665 | err = checking_wrmsrl(hwc->event_base + idx, |
| 1666 | (u64)(-left) & x86_pmu.event_mask); |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1667 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1668 | perf_event_update_userpage(event); |
Peter Zijlstra | 194002b | 2009-06-22 16:35:24 +0200 | [diff] [blame] | 1669 | |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1670 | return ret; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | static inline void |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1674 | intel_pmu_enable_fixed(struct hw_perf_event *hwc, int __idx) |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1675 | { |
| 1676 | int idx = __idx - X86_PMC_IDX_FIXED; |
| 1677 | u64 ctrl_val, bits, mask; |
| 1678 | int err; |
| 1679 | |
| 1680 | /* |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 1681 | * Enable IRQ generation (0x8), |
| 1682 | * and enable ring-3 counting (0x2) and ring-0 counting (0x1) |
| 1683 | * if requested: |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1684 | */ |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 1685 | bits = 0x8ULL; |
| 1686 | if (hwc->config & ARCH_PERFMON_EVENTSEL_USR) |
| 1687 | bits |= 0x2; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1688 | if (hwc->config & ARCH_PERFMON_EVENTSEL_OS) |
| 1689 | bits |= 0x1; |
Stephane Eranian | b27d515 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1690 | |
| 1691 | /* |
| 1692 | * ANY bit is supported in v3 and up |
| 1693 | */ |
| 1694 | if (x86_pmu.version > 2 && hwc->config & ARCH_PERFMON_EVENTSEL_ANY) |
| 1695 | bits |= 0x4; |
| 1696 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1697 | bits <<= (idx * 4); |
| 1698 | mask = 0xfULL << (idx * 4); |
| 1699 | |
| 1700 | rdmsrl(hwc->config_base, ctrl_val); |
| 1701 | ctrl_val &= ~mask; |
| 1702 | ctrl_val |= bits; |
| 1703 | err = checking_wrmsrl(hwc->config_base, ctrl_val); |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 1704 | } |
| 1705 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1706 | 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] | 1707 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1708 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Peter Zijlstra | 984b838 | 2009-07-10 09:59:56 +0200 | [diff] [blame] | 1709 | u64 val; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1710 | |
Peter Zijlstra | 984b838 | 2009-07-10 09:59:56 +0200 | [diff] [blame] | 1711 | val = hwc->config; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1712 | if (cpuc->enabled) |
Peter Zijlstra | 984b838 | 2009-07-10 09:59:56 +0200 | [diff] [blame] | 1713 | val |= ARCH_PERFMON_EVENTSEL0_ENABLE; |
| 1714 | |
| 1715 | (void)checking_wrmsrl(hwc->config_base + idx, val); |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1719 | 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] | 1720 | { |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1721 | if (unlikely(idx == X86_PMC_IDX_FIXED_BTS)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1722 | if (!__get_cpu_var(cpu_hw_events).enabled) |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1723 | return; |
| 1724 | |
| 1725 | intel_pmu_enable_bts(hwc->config); |
| 1726 | return; |
| 1727 | } |
| 1728 | |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 1729 | if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) { |
| 1730 | intel_pmu_enable_fixed(hwc, idx); |
| 1731 | return; |
| 1732 | } |
| 1733 | |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 1734 | __x86_pmu_enable_event(hwc, idx); |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 1735 | } |
| 1736 | |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 1737 | static void x86_pmu_enable_event(struct hw_perf_event *hwc, int idx) |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 1738 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1739 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Robert Richter | 7c90cc4 | 2009-04-29 12:47:18 +0200 | [diff] [blame] | 1740 | if (cpuc->enabled) |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 1741 | __x86_pmu_enable_event(hwc, idx); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1742 | } |
| 1743 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1744 | /* |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1745 | * activate a single event |
| 1746 | * |
| 1747 | * The event is added to the group of enabled events |
| 1748 | * but only if it can be scehduled with existing events. |
| 1749 | * |
| 1750 | * Called with PMU disabled. If successful and return value 1, |
| 1751 | * then guaranteed to call perf_enable() and hw_perf_enable() |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1752 | */ |
| 1753 | static int x86_pmu_enable(struct perf_event *event) |
| 1754 | { |
| 1755 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1756 | struct hw_perf_event *hwc; |
| 1757 | int assign[X86_PMC_IDX_MAX]; |
| 1758 | int n, n0, ret; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1759 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1760 | hwc = &event->hw; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 1761 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1762 | n0 = cpuc->n_events; |
| 1763 | n = collect_events(cpuc, event, false); |
| 1764 | if (n < 0) |
| 1765 | return n; |
Ingo Molnar | 53b441a | 2009-05-25 21:41:28 +0200 | [diff] [blame] | 1766 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1767 | ret = x86_schedule_events(cpuc, n, assign); |
| 1768 | if (ret) |
| 1769 | return ret; |
| 1770 | /* |
| 1771 | * copy new assignment, now we know it is possible |
| 1772 | * will be used by hw_perf_enable() |
| 1773 | */ |
| 1774 | memcpy(cpuc->assign, assign, n*sizeof(int)); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1775 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1776 | cpuc->n_events = n; |
| 1777 | cpuc->n_added = n - n0; |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 1778 | |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 1779 | return 0; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1780 | } |
| 1781 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1782 | static void x86_pmu_unthrottle(struct perf_event *event) |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 1783 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1784 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 1785 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 1786 | |
| 1787 | if (WARN_ON_ONCE(hwc->idx >= X86_PMC_IDX_MAX || |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1788 | cpuc->events[hwc->idx] != event)) |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 1789 | return; |
| 1790 | |
| 1791 | x86_pmu.enable(hwc, hwc->idx); |
| 1792 | } |
| 1793 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1794 | void perf_event_print_debug(void) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1795 | { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1796 | u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1797 | struct cpu_hw_events *cpuc; |
Peter Zijlstra | 5bb9efe | 2009-05-13 08:12:51 +0200 | [diff] [blame] | 1798 | unsigned long flags; |
Ingo Molnar | 1e12567 | 2008-12-09 12:18:18 +0100 | [diff] [blame] | 1799 | int cpu, idx; |
| 1800 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1801 | if (!x86_pmu.num_events) |
Ingo Molnar | 1e12567 | 2008-12-09 12:18:18 +0100 | [diff] [blame] | 1802 | return; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1803 | |
Peter Zijlstra | 5bb9efe | 2009-05-13 08:12:51 +0200 | [diff] [blame] | 1804 | local_irq_save(flags); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1805 | |
| 1806 | cpu = smp_processor_id(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1807 | cpuc = &per_cpu(cpu_hw_events, cpu); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1808 | |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 1809 | if (x86_pmu.version >= 2) { |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1810 | rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl); |
| 1811 | rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status); |
| 1812 | rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow); |
| 1813 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1814 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1815 | pr_info("\n"); |
| 1816 | pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl); |
| 1817 | pr_info("CPU#%d: status: %016llx\n", cpu, status); |
| 1818 | pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow); |
| 1819 | pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 1820 | } |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1821 | pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1822 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1823 | for (idx = 0; idx < x86_pmu.num_events; idx++) { |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 1824 | rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl); |
| 1825 | rdmsrl(x86_pmu.perfctr + idx, pmc_count); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1826 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 1827 | prev_left = per_cpu(pmc_prev_left[idx], cpu); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1828 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1829 | pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n", |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1830 | cpu, idx, pmc_ctrl); |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1831 | pr_info("CPU#%d: gen-PMC%d count: %016llx\n", |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1832 | cpu, idx, pmc_count); |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1833 | pr_info("CPU#%d: gen-PMC%d left: %016llx\n", |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1834 | cpu, idx, prev_left); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1835 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1836 | for (idx = 0; idx < x86_pmu.num_events_fixed; idx++) { |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1837 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count); |
| 1838 | |
Jaswinder Singh Rajput | a1ef58f | 2009-02-28 18:45:39 +0530 | [diff] [blame] | 1839 | pr_info("CPU#%d: fixed-PMC%d count: %016llx\n", |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1840 | cpu, idx, pmc_count); |
| 1841 | } |
Peter Zijlstra | 5bb9efe | 2009-05-13 08:12:51 +0200 | [diff] [blame] | 1842 | local_irq_restore(flags); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1843 | } |
| 1844 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1845 | static void intel_pmu_drain_bts_buffer(struct cpu_hw_events *cpuc) |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1846 | { |
| 1847 | struct debug_store *ds = cpuc->ds; |
| 1848 | struct bts_record { |
| 1849 | u64 from; |
| 1850 | u64 to; |
| 1851 | u64 flags; |
| 1852 | }; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1853 | 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] | 1854 | struct bts_record *at, *top; |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 1855 | struct perf_output_handle handle; |
| 1856 | struct perf_event_header header; |
| 1857 | struct perf_sample_data data; |
| 1858 | struct pt_regs regs; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1859 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1860 | if (!event) |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1861 | return; |
| 1862 | |
| 1863 | if (!ds) |
| 1864 | return; |
| 1865 | |
markus.t.metzger@intel.com | 596da17 | 2009-09-02 16:04:47 +0200 | [diff] [blame] | 1866 | at = (struct bts_record *)(unsigned long)ds->bts_buffer_base; |
| 1867 | top = (struct bts_record *)(unsigned long)ds->bts_index; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1868 | |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 1869 | if (top <= at) |
| 1870 | return; |
| 1871 | |
markus.t.metzger@intel.com | 596da17 | 2009-09-02 16:04:47 +0200 | [diff] [blame] | 1872 | ds->bts_index = ds->bts_buffer_base; |
| 1873 | |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1874 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1875 | data.period = event->hw.last_period; |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 1876 | data.addr = 0; |
Xiao Guangrong | 5e855db | 2009-12-10 17:08:54 +0800 | [diff] [blame] | 1877 | data.raw = NULL; |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 1878 | regs.ip = 0; |
| 1879 | |
| 1880 | /* |
| 1881 | * Prepare a generic sample, i.e. fill in the invariant fields. |
| 1882 | * We will overwrite the from and to address before we output |
| 1883 | * the sample. |
| 1884 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1885 | perf_prepare_sample(&header, &data, event, ®s); |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 1886 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1887 | if (perf_output_begin(&handle, event, |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 1888 | header.size * (top - at), 1, 1)) |
| 1889 | return; |
| 1890 | |
| 1891 | for (; at < top; at++) { |
| 1892 | data.ip = at->from; |
| 1893 | data.addr = at->to; |
| 1894 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1895 | perf_output_sample(&handle, &header, &data, event); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1896 | } |
| 1897 | |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 1898 | perf_output_end(&handle); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1899 | |
| 1900 | /* There's new data available. */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1901 | event->hw.interrupts++; |
| 1902 | event->pending_kill = POLL_IN; |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1903 | } |
| 1904 | |
Peter Zijlstra | 2e84187 | 2010-01-25 15:58:43 +0100 | [diff] [blame] | 1905 | static void __x86_pmu_disable(struct perf_event *event, struct cpu_hw_events *cpuc) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1906 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1907 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | 2e84187 | 2010-01-25 15:58:43 +0100 | [diff] [blame] | 1908 | int idx = hwc->idx; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1909 | |
Robert Richter | 0953423 | 2009-04-29 12:47:16 +0200 | [diff] [blame] | 1910 | /* |
| 1911 | * Must be done before we disable, otherwise the nmi handler |
| 1912 | * could reenable again: |
| 1913 | */ |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 1914 | clear_bit(idx, cpuc->active_mask); |
Robert Richter | d436989 | 2009-04-29 12:47:19 +0200 | [diff] [blame] | 1915 | x86_pmu.disable(hwc, idx); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1916 | |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 1917 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1918 | * Drain the remaining delta count out of a event |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1919 | * that we are disabling: |
| 1920 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1921 | x86_perf_event_update(event, hwc, idx); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1922 | |
| 1923 | /* Drain the remaining BTS records. */ |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 1924 | if (unlikely(idx == X86_PMC_IDX_FIXED_BTS)) |
| 1925 | intel_pmu_drain_bts_buffer(cpuc); |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1926 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1927 | cpuc->events[idx] = NULL; |
Peter Zijlstra | 2e84187 | 2010-01-25 15:58:43 +0100 | [diff] [blame] | 1928 | } |
| 1929 | |
| 1930 | static void x86_pmu_disable(struct perf_event *event) |
| 1931 | { |
| 1932 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 1933 | int i; |
| 1934 | |
| 1935 | __x86_pmu_disable(event, cpuc); |
Peter Zijlstra | 194002b | 2009-06-22 16:35:24 +0200 | [diff] [blame] | 1936 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1937 | for (i = 0; i < cpuc->n_events; i++) { |
| 1938 | if (event == cpuc->event_list[i]) { |
| 1939 | |
| 1940 | if (x86_pmu.put_event_constraints) |
| 1941 | x86_pmu.put_event_constraints(cpuc, event); |
| 1942 | |
| 1943 | while (++i < cpuc->n_events) |
| 1944 | cpuc->event_list[i-1] = cpuc->event_list[i]; |
| 1945 | |
| 1946 | --cpuc->n_events; |
Peter Zijlstra | 6c9687a | 2010-01-25 11:57:25 +0100 | [diff] [blame] | 1947 | break; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 1948 | } |
| 1949 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1950 | perf_event_update_userpage(event); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1951 | } |
| 1952 | |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 1953 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1954 | * Save and restart an expired event. Called by NMI contexts, |
| 1955 | * so it has to be careful about preempting normal event ops: |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 1956 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1957 | static int intel_pmu_save_and_restart(struct perf_event *event) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1958 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1959 | struct hw_perf_event *hwc = &event->hw; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1960 | int idx = hwc->idx; |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1961 | int ret; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1962 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1963 | x86_perf_event_update(event, hwc, idx); |
| 1964 | ret = x86_perf_event_set_period(event, hwc, idx); |
Ingo Molnar | 7e2ae34 | 2008-12-09 11:40:46 +0100 | [diff] [blame] | 1965 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1966 | if (event->state == PERF_EVENT_STATE_ACTIVE) |
| 1967 | intel_pmu_enable_event(hwc, idx); |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 1968 | |
| 1969 | return ret; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1970 | } |
| 1971 | |
Ingo Molnar | aaba980 | 2009-05-26 08:10:00 +0200 | [diff] [blame] | 1972 | static void intel_pmu_reset(void) |
| 1973 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1974 | struct debug_store *ds = __get_cpu_var(cpu_hw_events).ds; |
Ingo Molnar | aaba980 | 2009-05-26 08:10:00 +0200 | [diff] [blame] | 1975 | unsigned long flags; |
| 1976 | int idx; |
| 1977 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1978 | if (!x86_pmu.num_events) |
Ingo Molnar | aaba980 | 2009-05-26 08:10:00 +0200 | [diff] [blame] | 1979 | return; |
| 1980 | |
| 1981 | local_irq_save(flags); |
| 1982 | |
| 1983 | printk("clearing PMU state on CPU#%d\n", smp_processor_id()); |
| 1984 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1985 | for (idx = 0; idx < x86_pmu.num_events; idx++) { |
Ingo Molnar | aaba980 | 2009-05-26 08:10:00 +0200 | [diff] [blame] | 1986 | checking_wrmsrl(x86_pmu.eventsel + idx, 0ull); |
| 1987 | checking_wrmsrl(x86_pmu.perfctr + idx, 0ull); |
| 1988 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1989 | for (idx = 0; idx < x86_pmu.num_events_fixed; idx++) { |
Ingo Molnar | aaba980 | 2009-05-26 08:10:00 +0200 | [diff] [blame] | 1990 | checking_wrmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull); |
| 1991 | } |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 1992 | if (ds) |
| 1993 | ds->bts_index = ds->bts_buffer_base; |
Ingo Molnar | aaba980 | 2009-05-26 08:10:00 +0200 | [diff] [blame] | 1994 | |
| 1995 | local_irq_restore(flags); |
| 1996 | } |
| 1997 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 1998 | /* |
| 1999 | * This handler is triggered by the local APIC, so the APIC IRQ handling |
| 2000 | * rules apply: |
| 2001 | */ |
Yong Wang | a328810 | 2009-06-03 13:12:55 +0800 | [diff] [blame] | 2002 | static int intel_pmu_handle_irq(struct pt_regs *regs) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2003 | { |
Peter Zijlstra | df1a132 | 2009-06-10 21:02:22 +0200 | [diff] [blame] | 2004 | struct perf_sample_data data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2005 | struct cpu_hw_events *cpuc; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 2006 | int bit, loops; |
Mike Galbraith | 4b39fd9 | 2009-01-23 14:36:16 +0100 | [diff] [blame] | 2007 | u64 ack, status; |
Ingo Molnar | 9029a5e | 2009-05-15 08:26:20 +0200 | [diff] [blame] | 2008 | |
Peter Zijlstra | df1a132 | 2009-06-10 21:02:22 +0200 | [diff] [blame] | 2009 | data.addr = 0; |
Xiao Guangrong | 5e855db | 2009-12-10 17:08:54 +0800 | [diff] [blame] | 2010 | data.raw = NULL; |
Peter Zijlstra | df1a132 | 2009-06-10 21:02:22 +0200 | [diff] [blame] | 2011 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2012 | cpuc = &__get_cpu_var(cpu_hw_events); |
Ingo Molnar | 43874d2 | 2008-12-09 12:23:59 +0100 | [diff] [blame] | 2013 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 2014 | perf_disable(); |
Markus Metzger | 5622f29 | 2009-09-15 13:00:23 +0200 | [diff] [blame] | 2015 | intel_pmu_drain_bts_buffer(cpuc); |
Robert Richter | 19d84da | 2009-04-29 12:47:25 +0200 | [diff] [blame] | 2016 | status = intel_pmu_get_status(); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 2017 | if (!status) { |
| 2018 | perf_enable(); |
| 2019 | return 0; |
| 2020 | } |
Ingo Molnar | 87b9cf4 | 2008-12-08 14:20:16 +0100 | [diff] [blame] | 2021 | |
Ingo Molnar | 9029a5e | 2009-05-15 08:26:20 +0200 | [diff] [blame] | 2022 | loops = 0; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2023 | again: |
Ingo Molnar | 9029a5e | 2009-05-15 08:26:20 +0200 | [diff] [blame] | 2024 | if (++loops > 100) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2025 | WARN_ONCE(1, "perfevents: irq loop stuck!\n"); |
| 2026 | perf_event_print_debug(); |
Ingo Molnar | aaba980 | 2009-05-26 08:10:00 +0200 | [diff] [blame] | 2027 | intel_pmu_reset(); |
| 2028 | perf_enable(); |
Ingo Molnar | 9029a5e | 2009-05-15 08:26:20 +0200 | [diff] [blame] | 2029 | return 1; |
| 2030 | } |
| 2031 | |
Mike Galbraith | d278c48 | 2009-02-09 07:38:50 +0100 | [diff] [blame] | 2032 | inc_irq_stat(apic_perf_irqs); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2033 | ack = status; |
Ingo Molnar | 2f18d1e | 2008-12-22 11:10:42 +0100 | [diff] [blame] | 2034 | for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2035 | struct perf_event *event = cpuc->events[bit]; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2036 | |
| 2037 | clear_bit(bit, (unsigned long *) &status); |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 2038 | if (!test_bit(bit, cpuc->active_mask)) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2039 | continue; |
| 2040 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2041 | if (!intel_pmu_save_and_restart(event)) |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 2042 | continue; |
| 2043 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2044 | data.period = event->hw.last_period; |
Peter Zijlstra | 60f916d | 2009-06-15 19:00:20 +0200 | [diff] [blame] | 2045 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2046 | if (perf_event_overflow(event, 1, &data, regs)) |
| 2047 | intel_pmu_disable_event(&event->hw, bit); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2048 | } |
| 2049 | |
Robert Richter | dee5d90 | 2009-04-29 12:47:07 +0200 | [diff] [blame] | 2050 | intel_pmu_ack_status(ack); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2051 | |
| 2052 | /* |
| 2053 | * Repeat if there is more work to be done: |
| 2054 | */ |
Robert Richter | 19d84da | 2009-04-29 12:47:25 +0200 | [diff] [blame] | 2055 | status = intel_pmu_get_status(); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2056 | if (status) |
| 2057 | goto again; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 2058 | |
Peter Zijlstra | 48e22d5 | 2009-05-25 17:39:04 +0200 | [diff] [blame] | 2059 | perf_enable(); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 2060 | |
| 2061 | return 1; |
Mike Galbraith | 1b023a9 | 2009-01-23 10:13:01 +0100 | [diff] [blame] | 2062 | } |
| 2063 | |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 2064 | static int x86_pmu_handle_irq(struct pt_regs *regs) |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 2065 | { |
Peter Zijlstra | df1a132 | 2009-06-10 21:02:22 +0200 | [diff] [blame] | 2066 | struct perf_sample_data data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2067 | struct cpu_hw_events *cpuc; |
| 2068 | struct perf_event *event; |
| 2069 | struct hw_perf_event *hwc; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 2070 | int idx, handled = 0; |
Ingo Molnar | 9029a5e | 2009-05-15 08:26:20 +0200 | [diff] [blame] | 2071 | u64 val; |
| 2072 | |
Peter Zijlstra | df1a132 | 2009-06-10 21:02:22 +0200 | [diff] [blame] | 2073 | data.addr = 0; |
Xiao Guangrong | 5e855db | 2009-12-10 17:08:54 +0800 | [diff] [blame] | 2074 | data.raw = NULL; |
Peter Zijlstra | df1a132 | 2009-06-10 21:02:22 +0200 | [diff] [blame] | 2075 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2076 | cpuc = &__get_cpu_var(cpu_hw_events); |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 2077 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2078 | for (idx = 0; idx < x86_pmu.num_events; idx++) { |
Robert Richter | 43f6201 | 2009-04-29 16:55:56 +0200 | [diff] [blame] | 2079 | if (!test_bit(idx, cpuc->active_mask)) |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 2080 | continue; |
Peter Zijlstra | 962bf7a | 2009-05-13 13:21:36 +0200 | [diff] [blame] | 2081 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2082 | event = cpuc->events[idx]; |
| 2083 | hwc = &event->hw; |
Peter Zijlstra | a4016a7 | 2009-05-14 14:52:17 +0200 | [diff] [blame] | 2084 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2085 | val = x86_perf_event_update(event, hwc, idx); |
| 2086 | if (val & (1ULL << (x86_pmu.event_bits - 1))) |
Peter Zijlstra | 48e22d5 | 2009-05-25 17:39:04 +0200 | [diff] [blame] | 2087 | continue; |
Peter Zijlstra | 962bf7a | 2009-05-13 13:21:36 +0200 | [diff] [blame] | 2088 | |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 2089 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2090 | * event overflow |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 2091 | */ |
| 2092 | handled = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2093 | data.period = event->hw.last_period; |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 2094 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2095 | if (!x86_perf_event_set_period(event, hwc, idx)) |
Peter Zijlstra | e4abb5d | 2009-06-02 16:08:20 +0200 | [diff] [blame] | 2096 | continue; |
| 2097 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2098 | if (perf_event_overflow(event, 1, &data, regs)) |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 2099 | x86_pmu.disable(hwc, idx); |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 2100 | } |
Peter Zijlstra | 962bf7a | 2009-05-13 13:21:36 +0200 | [diff] [blame] | 2101 | |
Peter Zijlstra | 9e350de | 2009-06-10 21:34:59 +0200 | [diff] [blame] | 2102 | if (handled) |
| 2103 | inc_irq_stat(apic_perf_irqs); |
| 2104 | |
Robert Richter | a29aa8a | 2009-04-29 12:47:21 +0200 | [diff] [blame] | 2105 | return handled; |
| 2106 | } |
Robert Richter | 39d81ea | 2009-04-29 12:47:05 +0200 | [diff] [blame] | 2107 | |
Peter Zijlstra | b6276f3 | 2009-04-06 11:45:03 +0200 | [diff] [blame] | 2108 | void smp_perf_pending_interrupt(struct pt_regs *regs) |
| 2109 | { |
| 2110 | irq_enter(); |
| 2111 | ack_APIC_irq(); |
| 2112 | inc_irq_stat(apic_pending_irqs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2113 | perf_event_do_pending(); |
Peter Zijlstra | b6276f3 | 2009-04-06 11:45:03 +0200 | [diff] [blame] | 2114 | irq_exit(); |
| 2115 | } |
| 2116 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2117 | void set_perf_event_pending(void) |
Peter Zijlstra | b6276f3 | 2009-04-06 11:45:03 +0200 | [diff] [blame] | 2118 | { |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 2119 | #ifdef CONFIG_X86_LOCAL_APIC |
Peter Zijlstra | 7d42896 | 2009-09-23 11:03:37 +0200 | [diff] [blame] | 2120 | if (!x86_pmu.apic || !x86_pmu_initialized()) |
| 2121 | return; |
| 2122 | |
Peter Zijlstra | b6276f3 | 2009-04-06 11:45:03 +0200 | [diff] [blame] | 2123 | apic->send_IPI_self(LOCAL_PENDING_VECTOR); |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 2124 | #endif |
Peter Zijlstra | b6276f3 | 2009-04-06 11:45:03 +0200 | [diff] [blame] | 2125 | } |
| 2126 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2127 | void perf_events_lapic_init(void) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2128 | { |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 2129 | #ifdef CONFIG_X86_LOCAL_APIC |
| 2130 | if (!x86_pmu.apic || !x86_pmu_initialized()) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2131 | return; |
Robert Richter | 85cf9db | 2009-04-29 12:47:20 +0200 | [diff] [blame] | 2132 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2133 | /* |
Yong Wang | c323d95 | 2009-05-29 13:28:35 +0800 | [diff] [blame] | 2134 | * Always use NMI for PMU |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2135 | */ |
Yong Wang | c323d95 | 2009-05-29 13:28:35 +0800 | [diff] [blame] | 2136 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 2137 | #endif |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | static int __kprobes |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2141 | perf_event_nmi_handler(struct notifier_block *self, |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2142 | unsigned long cmd, void *__args) |
| 2143 | { |
| 2144 | struct die_args *args = __args; |
| 2145 | struct pt_regs *regs; |
| 2146 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2147 | if (!atomic_read(&active_events)) |
Peter Zijlstra | 63a809a | 2009-05-01 12:23:17 +0200 | [diff] [blame] | 2148 | return NOTIFY_DONE; |
| 2149 | |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 2150 | switch (cmd) { |
| 2151 | case DIE_NMI: |
| 2152 | case DIE_NMI_IPI: |
| 2153 | break; |
| 2154 | |
| 2155 | default: |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2156 | return NOTIFY_DONE; |
Peter Zijlstra | b0f3f28 | 2009-03-05 18:08:27 +0100 | [diff] [blame] | 2157 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2158 | |
| 2159 | regs = args->regs; |
| 2160 | |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 2161 | #ifdef CONFIG_X86_LOCAL_APIC |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2162 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 2163 | #endif |
Peter Zijlstra | a4016a7 | 2009-05-14 14:52:17 +0200 | [diff] [blame] | 2164 | /* |
| 2165 | * 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] | 2166 | * events could trigger 'simultaneously' raising two back-to-back NMIs. |
Peter Zijlstra | a4016a7 | 2009-05-14 14:52:17 +0200 | [diff] [blame] | 2167 | * |
| 2168 | * If the first NMI handles both, the latter will be empty and daze |
| 2169 | * the CPU. |
| 2170 | */ |
Yong Wang | a328810 | 2009-06-03 13:12:55 +0800 | [diff] [blame] | 2171 | x86_pmu.handle_irq(regs); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2172 | |
Peter Zijlstra | a4016a7 | 2009-05-14 14:52:17 +0200 | [diff] [blame] | 2173 | return NOTIFY_STOP; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2174 | } |
| 2175 | |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 2176 | static struct event_constraint unconstrained; |
| 2177 | |
Peter Zijlstra | c91e0f5 | 2010-01-22 15:25:59 +0100 | [diff] [blame] | 2178 | static struct event_constraint bts_constraint = |
| 2179 | EVENT_CONSTRAINT(0, 1ULL << X86_PMC_IDX_FIXED_BTS, 0); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2180 | |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 2181 | static struct event_constraint * |
| 2182 | intel_special_constraints(struct perf_event *event) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2183 | { |
| 2184 | unsigned int hw_event; |
| 2185 | |
| 2186 | hw_event = event->hw.config & INTEL_ARCH_EVENT_MASK; |
| 2187 | |
| 2188 | if (unlikely((hw_event == |
| 2189 | x86_pmu.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS)) && |
| 2190 | (event->hw.sample_period == 1))) { |
| 2191 | |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 2192 | return &bts_constraint; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2193 | } |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 2194 | return NULL; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2195 | } |
| 2196 | |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 2197 | static struct event_constraint * |
| 2198 | intel_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2199 | { |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 2200 | struct event_constraint *c; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2201 | |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 2202 | c = intel_special_constraints(event); |
| 2203 | if (c) |
| 2204 | return c; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2205 | |
| 2206 | if (x86_pmu.event_constraints) { |
| 2207 | for_each_event_constraint(c, x86_pmu.event_constraints) { |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 2208 | if ((event->hw.config & c->cmask) == c->code) |
| 2209 | return c; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2210 | } |
| 2211 | } |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 2212 | |
| 2213 | return &unconstrained; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2214 | } |
| 2215 | |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 2216 | static struct event_constraint * |
| 2217 | amd_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event) |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2218 | { |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 2219 | return &unconstrained; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2220 | } |
| 2221 | |
| 2222 | static int x86_event_sched_in(struct perf_event *event, |
| 2223 | struct perf_cpu_context *cpuctx, int cpu) |
| 2224 | { |
| 2225 | int ret = 0; |
| 2226 | |
| 2227 | event->state = PERF_EVENT_STATE_ACTIVE; |
| 2228 | event->oncpu = cpu; |
| 2229 | event->tstamp_running += event->ctx->time - event->tstamp_stopped; |
| 2230 | |
| 2231 | if (!is_x86_event(event)) |
| 2232 | ret = event->pmu->enable(event); |
| 2233 | |
| 2234 | if (!ret && !is_software_event(event)) |
| 2235 | cpuctx->active_oncpu++; |
| 2236 | |
| 2237 | if (!ret && event->attr.exclusive) |
| 2238 | cpuctx->exclusive = 1; |
| 2239 | |
| 2240 | return ret; |
| 2241 | } |
| 2242 | |
| 2243 | static void x86_event_sched_out(struct perf_event *event, |
| 2244 | struct perf_cpu_context *cpuctx, int cpu) |
| 2245 | { |
| 2246 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 2247 | event->oncpu = -1; |
| 2248 | |
| 2249 | if (!is_x86_event(event)) |
| 2250 | event->pmu->disable(event); |
| 2251 | |
| 2252 | event->tstamp_running -= event->ctx->time - event->tstamp_stopped; |
| 2253 | |
| 2254 | if (!is_software_event(event)) |
| 2255 | cpuctx->active_oncpu--; |
| 2256 | |
| 2257 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
| 2258 | cpuctx->exclusive = 0; |
| 2259 | } |
| 2260 | |
| 2261 | /* |
| 2262 | * Called to enable a whole group of events. |
| 2263 | * Returns 1 if the group was enabled, or -EAGAIN if it could not be. |
| 2264 | * Assumes the caller has disabled interrupts and has |
| 2265 | * frozen the PMU with hw_perf_save_disable. |
| 2266 | * |
| 2267 | * called with PMU disabled. If successful and return value 1, |
| 2268 | * then guaranteed to call perf_enable() and hw_perf_enable() |
| 2269 | */ |
| 2270 | int hw_perf_group_sched_in(struct perf_event *leader, |
| 2271 | struct perf_cpu_context *cpuctx, |
| 2272 | struct perf_event_context *ctx, int cpu) |
| 2273 | { |
| 2274 | struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu); |
| 2275 | struct perf_event *sub; |
| 2276 | int assign[X86_PMC_IDX_MAX]; |
| 2277 | int n0, n1, ret; |
| 2278 | |
| 2279 | /* n0 = total number of events */ |
| 2280 | n0 = collect_events(cpuc, leader, true); |
| 2281 | if (n0 < 0) |
| 2282 | return n0; |
| 2283 | |
| 2284 | ret = x86_schedule_events(cpuc, n0, assign); |
| 2285 | if (ret) |
| 2286 | return ret; |
| 2287 | |
| 2288 | ret = x86_event_sched_in(leader, cpuctx, cpu); |
| 2289 | if (ret) |
| 2290 | return ret; |
| 2291 | |
| 2292 | n1 = 1; |
| 2293 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 2294 | if (sub->state > PERF_EVENT_STATE_OFF) { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2295 | ret = x86_event_sched_in(sub, cpuctx, cpu); |
| 2296 | if (ret) |
| 2297 | goto undo; |
| 2298 | ++n1; |
| 2299 | } |
| 2300 | } |
| 2301 | /* |
| 2302 | * copy new assignment, now we know it is possible |
| 2303 | * will be used by hw_perf_enable() |
| 2304 | */ |
| 2305 | memcpy(cpuc->assign, assign, n0*sizeof(int)); |
| 2306 | |
| 2307 | cpuc->n_events = n0; |
| 2308 | cpuc->n_added = n1; |
| 2309 | ctx->nr_active += n1; |
| 2310 | |
| 2311 | /* |
| 2312 | * 1 means successful and events are active |
| 2313 | * This is not quite true because we defer |
| 2314 | * actual activation until hw_perf_enable() but |
| 2315 | * this way we* ensure caller won't try to enable |
| 2316 | * individual events |
| 2317 | */ |
| 2318 | return 1; |
| 2319 | undo: |
| 2320 | x86_event_sched_out(leader, cpuctx, cpu); |
| 2321 | n0 = 1; |
| 2322 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
| 2323 | if (sub->state == PERF_EVENT_STATE_ACTIVE) { |
| 2324 | x86_event_sched_out(sub, cpuctx, cpu); |
| 2325 | if (++n0 == n1) |
| 2326 | break; |
| 2327 | } |
| 2328 | } |
| 2329 | return ret; |
| 2330 | } |
| 2331 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2332 | static __read_mostly struct notifier_block perf_event_nmi_notifier = { |
| 2333 | .notifier_call = perf_event_nmi_handler, |
Mike Galbraith | 5b75af0 | 2009-02-04 17:11:34 +0100 | [diff] [blame] | 2334 | .next = NULL, |
| 2335 | .priority = 1 |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2336 | }; |
| 2337 | |
Hiroshi Shimamoto | db48ccc | 2009-11-12 11:25:34 +0900 | [diff] [blame] | 2338 | static __initconst struct x86_pmu p6_pmu = { |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 2339 | .name = "p6", |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 2340 | .handle_irq = x86_pmu_handle_irq, |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 2341 | .disable_all = p6_pmu_disable_all, |
| 2342 | .enable_all = p6_pmu_enable_all, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2343 | .enable = p6_pmu_enable_event, |
| 2344 | .disable = p6_pmu_disable_event, |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 2345 | .eventsel = MSR_P6_EVNTSEL0, |
| 2346 | .perfctr = MSR_P6_PERFCTR0, |
| 2347 | .event_map = p6_pmu_event_map, |
| 2348 | .raw_event = p6_pmu_raw_event, |
| 2349 | .max_events = ARRAY_SIZE(p6_perfmon_event_map), |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 2350 | .apic = 1, |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 2351 | .max_period = (1ULL << 31) - 1, |
| 2352 | .version = 0, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2353 | .num_events = 2, |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 2354 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2355 | * Events have 40 bits implemented. However they are designed such |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 2356 | * 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] | 2357 | * 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] | 2358 | * |
| 2359 | * See IA-32 Intel Architecture Software developer manual Vol 3B |
| 2360 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2361 | .event_bits = 32, |
| 2362 | .event_mask = (1ULL << 32) - 1, |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2363 | .get_event_constraints = intel_get_event_constraints, |
| 2364 | .event_constraints = intel_p6_event_constraints |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 2365 | }; |
| 2366 | |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 2367 | static __initconst struct x86_pmu core_pmu = { |
| 2368 | .name = "core", |
| 2369 | .handle_irq = x86_pmu_handle_irq, |
| 2370 | .disable_all = x86_pmu_disable_all, |
| 2371 | .enable_all = x86_pmu_enable_all, |
| 2372 | .enable = x86_pmu_enable_event, |
| 2373 | .disable = x86_pmu_disable_event, |
| 2374 | .eventsel = MSR_ARCH_PERFMON_EVENTSEL0, |
| 2375 | .perfctr = MSR_ARCH_PERFMON_PERFCTR0, |
| 2376 | .event_map = intel_pmu_event_map, |
| 2377 | .raw_event = intel_pmu_raw_event, |
| 2378 | .max_events = ARRAY_SIZE(intel_perfmon_event_map), |
| 2379 | .apic = 1, |
| 2380 | /* |
| 2381 | * Intel PMCs cannot be accessed sanely above 32 bit width, |
| 2382 | * so we install an artificial 1<<31 period regardless of |
| 2383 | * the generic event period: |
| 2384 | */ |
| 2385 | .max_period = (1ULL << 31) - 1, |
| 2386 | .get_event_constraints = intel_get_event_constraints, |
| 2387 | .event_constraints = intel_core_event_constraints, |
| 2388 | }; |
| 2389 | |
Hiroshi Shimamoto | db48ccc | 2009-11-12 11:25:34 +0900 | [diff] [blame] | 2390 | static __initconst struct x86_pmu intel_pmu = { |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 2391 | .name = "Intel", |
Robert Richter | 39d81ea | 2009-04-29 12:47:05 +0200 | [diff] [blame] | 2392 | .handle_irq = intel_pmu_handle_irq, |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 2393 | .disable_all = intel_pmu_disable_all, |
| 2394 | .enable_all = intel_pmu_enable_all, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2395 | .enable = intel_pmu_enable_event, |
| 2396 | .disable = intel_pmu_disable_event, |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 2397 | .eventsel = MSR_ARCH_PERFMON_EVENTSEL0, |
| 2398 | .perfctr = MSR_ARCH_PERFMON_PERFCTR0, |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 2399 | .event_map = intel_pmu_event_map, |
| 2400 | .raw_event = intel_pmu_raw_event, |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 2401 | .max_events = ARRAY_SIZE(intel_perfmon_event_map), |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 2402 | .apic = 1, |
Robert Richter | c619b8f | 2009-04-29 12:47:23 +0200 | [diff] [blame] | 2403 | /* |
| 2404 | * Intel PMCs cannot be accessed sanely above 32 bit width, |
| 2405 | * so we install an artificial 1<<31 period regardless of |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2406 | * the generic event period: |
Robert Richter | c619b8f | 2009-04-29 12:47:23 +0200 | [diff] [blame] | 2407 | */ |
| 2408 | .max_period = (1ULL << 31) - 1, |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 2409 | .enable_bts = intel_pmu_enable_bts, |
| 2410 | .disable_bts = intel_pmu_disable_bts, |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2411 | .get_event_constraints = intel_get_event_constraints |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 2412 | }; |
| 2413 | |
Hiroshi Shimamoto | db48ccc | 2009-11-12 11:25:34 +0900 | [diff] [blame] | 2414 | static __initconst struct x86_pmu amd_pmu = { |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 2415 | .name = "AMD", |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 2416 | .handle_irq = x86_pmu_handle_irq, |
| 2417 | .disable_all = x86_pmu_disable_all, |
| 2418 | .enable_all = x86_pmu_enable_all, |
| 2419 | .enable = x86_pmu_enable_event, |
| 2420 | .disable = x86_pmu_disable_event, |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 2421 | .eventsel = MSR_K7_EVNTSEL0, |
| 2422 | .perfctr = MSR_K7_PERFCTR0, |
Robert Richter | 5f4ec28 | 2009-04-29 12:47:04 +0200 | [diff] [blame] | 2423 | .event_map = amd_pmu_event_map, |
| 2424 | .raw_event = amd_pmu_raw_event, |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 2425 | .max_events = ARRAY_SIZE(amd_perfmon_event_map), |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2426 | .num_events = 4, |
| 2427 | .event_bits = 48, |
| 2428 | .event_mask = (1ULL << 48) - 1, |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 2429 | .apic = 1, |
Robert Richter | c619b8f | 2009-04-29 12:47:23 +0200 | [diff] [blame] | 2430 | /* use highest bit to detect overflow */ |
| 2431 | .max_period = (1ULL << 47) - 1, |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2432 | .get_event_constraints = amd_get_event_constraints |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 2433 | }; |
| 2434 | |
Hiroshi Shimamoto | db48ccc | 2009-11-12 11:25:34 +0900 | [diff] [blame] | 2435 | static __init int p6_pmu_init(void) |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 2436 | { |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 2437 | switch (boot_cpu_data.x86_model) { |
| 2438 | case 1: |
| 2439 | case 3: /* Pentium Pro */ |
| 2440 | case 5: |
| 2441 | case 6: /* Pentium II */ |
| 2442 | case 7: |
| 2443 | case 8: |
| 2444 | case 11: /* Pentium III */ |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 2445 | case 9: |
| 2446 | case 13: |
Daniel Qarras | f1c6a58 | 2009-07-12 04:32:40 -0700 | [diff] [blame] | 2447 | /* Pentium M */ |
| 2448 | break; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 2449 | default: |
| 2450 | pr_cont("unsupported p6 CPU model %d ", |
| 2451 | boot_cpu_data.x86_model); |
| 2452 | return -ENODEV; |
| 2453 | } |
| 2454 | |
Ingo Molnar | 04da8a4 | 2009-08-11 10:40:08 +0200 | [diff] [blame] | 2455 | x86_pmu = p6_pmu; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 2456 | |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 2457 | return 0; |
| 2458 | } |
| 2459 | |
Hiroshi Shimamoto | db48ccc | 2009-11-12 11:25:34 +0900 | [diff] [blame] | 2460 | static __init int intel_pmu_init(void) |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2461 | { |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 2462 | union cpuid10_edx edx; |
Ingo Molnar | 7bb497b | 2009-03-18 08:59:21 +0100 | [diff] [blame] | 2463 | union cpuid10_eax eax; |
| 2464 | unsigned int unused; |
| 2465 | unsigned int ebx; |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 2466 | int version; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2467 | |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 2468 | if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) { |
| 2469 | /* check for P6 processor family */ |
| 2470 | if (boot_cpu_data.x86 == 6) { |
| 2471 | return p6_pmu_init(); |
| 2472 | } else { |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 2473 | return -ENODEV; |
Vince Weaver | 11d1578 | 2009-07-08 17:46:14 -0400 | [diff] [blame] | 2474 | } |
| 2475 | } |
Robert Richter | da1a776 | 2009-04-29 12:46:58 +0200 | [diff] [blame] | 2476 | |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2477 | /* |
| 2478 | * Check whether the Architectural PerfMon supports |
Ingo Molnar | dfc6509 | 2009-09-21 11:31:35 +0200 | [diff] [blame] | 2479 | * Branch Misses Retired hw_event or not. |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2480 | */ |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 2481 | cpuid(10, &eax.full, &ebx, &unused, &edx.full); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2482 | if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED) |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 2483 | return -ENODEV; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2484 | |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 2485 | version = eax.split.version_id; |
| 2486 | if (version < 2) |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 2487 | x86_pmu = core_pmu; |
| 2488 | else |
| 2489 | x86_pmu = intel_pmu; |
Ingo Molnar | 7bb497b | 2009-03-18 08:59:21 +0100 | [diff] [blame] | 2490 | |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2491 | x86_pmu.version = version; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2492 | x86_pmu.num_events = eax.split.num_events; |
| 2493 | x86_pmu.event_bits = eax.split.bit_width; |
| 2494 | x86_pmu.event_mask = (1ULL << eax.split.bit_width) - 1; |
Ingo Molnar | 066d7de | 2009-05-04 19:04:09 +0200 | [diff] [blame] | 2495 | |
| 2496 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2497 | * Quirk: v2 perfmon does not report fixed-purpose events, so |
| 2498 | * assume at least 3 events: |
Ingo Molnar | 066d7de | 2009-05-04 19:04:09 +0200 | [diff] [blame] | 2499 | */ |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 2500 | if (version > 1) |
| 2501 | 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] | 2502 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 2503 | /* |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2504 | * Install the hw-cache-events table: |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 2505 | */ |
| 2506 | switch (boot_cpu_data.x86_model) { |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 2507 | case 14: /* 65 nm core solo/duo, "Yonah" */ |
| 2508 | pr_cont("Core events, "); |
| 2509 | break; |
| 2510 | |
Yong Wang | dc81081 | 2009-06-10 17:06:12 +0800 | [diff] [blame] | 2511 | case 15: /* original 65 nm celeron/pentium/core2/xeon, "Merom"/"Conroe" */ |
| 2512 | case 22: /* single-core 65 nm celeron/core2solo "Merom-L"/"Conroe-L" */ |
| 2513 | case 23: /* current 45 nm celeron/core2/xeon "Penryn"/"Wolfdale" */ |
| 2514 | case 29: /* six-core 45 nm xeon "Dunnington" */ |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 2515 | memcpy(hw_cache_event_ids, core2_hw_cache_event_ids, |
Thomas Gleixner | 820a644 | 2009-06-08 19:10:25 +0200 | [diff] [blame] | 2516 | sizeof(hw_cache_event_ids)); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 2517 | |
Peter Zijlstra | 8c48e44 | 2010-01-29 13:25:31 +0100 | [diff] [blame] | 2518 | x86_pmu.event_constraints = intel_core2_event_constraints; |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2519 | pr_cont("Core2 events, "); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 2520 | break; |
Peter Zijlstra | 452a339 | 2010-01-27 23:07:48 +0100 | [diff] [blame] | 2521 | |
| 2522 | case 26: /* 45 nm nehalem, "Bloomfield" */ |
| 2523 | case 30: /* 45 nm nehalem, "Lynnfield" */ |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 2524 | memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids, |
Thomas Gleixner | 820a644 | 2009-06-08 19:10:25 +0200 | [diff] [blame] | 2525 | sizeof(hw_cache_event_ids)); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 2526 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2527 | x86_pmu.event_constraints = intel_nehalem_event_constraints; |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2528 | pr_cont("Nehalem/Corei7 events, "); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 2529 | break; |
| 2530 | case 28: |
| 2531 | memcpy(hw_cache_event_ids, atom_hw_cache_event_ids, |
Thomas Gleixner | 820a644 | 2009-06-08 19:10:25 +0200 | [diff] [blame] | 2532 | sizeof(hw_cache_event_ids)); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 2533 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2534 | x86_pmu.event_constraints = intel_gen_event_constraints; |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2535 | pr_cont("Atom events, "); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 2536 | break; |
Peter Zijlstra | 452a339 | 2010-01-27 23:07:48 +0100 | [diff] [blame] | 2537 | |
| 2538 | case 37: /* 32 nm nehalem, "Clarkdale" */ |
| 2539 | case 44: /* 32 nm nehalem, "Gulftown" */ |
| 2540 | memcpy(hw_cache_event_ids, westmere_hw_cache_event_ids, |
| 2541 | sizeof(hw_cache_event_ids)); |
| 2542 | |
| 2543 | x86_pmu.event_constraints = intel_westmere_event_constraints; |
| 2544 | pr_cont("Westmere events, "); |
| 2545 | break; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2546 | default: |
| 2547 | /* |
| 2548 | * default constraints for v2 and up |
| 2549 | */ |
| 2550 | x86_pmu.event_constraints = intel_gen_event_constraints; |
| 2551 | pr_cont("generic architected perfmon, "); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 2552 | } |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 2553 | return 0; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 2554 | } |
| 2555 | |
Hiroshi Shimamoto | db48ccc | 2009-11-12 11:25:34 +0900 | [diff] [blame] | 2556 | static __init int amd_pmu_init(void) |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 2557 | { |
Jaswinder Singh Rajput | 4d2be12 | 2009-06-11 15:28:09 +0530 | [diff] [blame] | 2558 | /* Performance-monitoring supported from K7 and later: */ |
| 2559 | if (boot_cpu_data.x86 < 6) |
| 2560 | return -ENODEV; |
| 2561 | |
Robert Richter | 4a06bd8 | 2009-04-29 12:47:11 +0200 | [diff] [blame] | 2562 | x86_pmu = amd_pmu; |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 2563 | |
Jaswinder Singh Rajput | f4db43a | 2009-06-13 01:06:21 +0530 | [diff] [blame] | 2564 | /* Events are common for all AMDs */ |
| 2565 | memcpy(hw_cache_event_ids, amd_hw_cache_event_ids, |
| 2566 | sizeof(hw_cache_event_ids)); |
Thomas Gleixner | f86748e | 2009-06-08 22:33:10 +0200 | [diff] [blame] | 2567 | |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 2568 | return 0; |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 2569 | } |
| 2570 | |
Cyrill Gorcunov | 1255803 | 2009-12-10 19:56:34 +0300 | [diff] [blame] | 2571 | static void __init pmu_check_apic(void) |
| 2572 | { |
| 2573 | if (cpu_has_apic) |
| 2574 | return; |
| 2575 | |
| 2576 | x86_pmu.apic = 0; |
| 2577 | pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n"); |
| 2578 | pr_info("no hardware sampling interrupt available.\n"); |
| 2579 | } |
| 2580 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2581 | void __init init_hw_perf_events(void) |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 2582 | { |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 2583 | int err; |
| 2584 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2585 | pr_info("Performance Events: "); |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2586 | |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 2587 | switch (boot_cpu_data.x86_vendor) { |
| 2588 | case X86_VENDOR_INTEL: |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 2589 | err = intel_pmu_init(); |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 2590 | break; |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 2591 | case X86_VENDOR_AMD: |
Robert Richter | 72eae04 | 2009-04-29 12:47:10 +0200 | [diff] [blame] | 2592 | err = amd_pmu_init(); |
Jaswinder Singh Rajput | f87ad35 | 2009-02-27 20:15:14 +0530 | [diff] [blame] | 2593 | break; |
Robert Richter | 4138960 | 2009-04-29 12:47:00 +0200 | [diff] [blame] | 2594 | default: |
| 2595 | return; |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 2596 | } |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2597 | if (err != 0) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2598 | pr_cont("no PMU driver, software events only.\n"); |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 2599 | return; |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2600 | } |
Jaswinder Singh Rajput | b56a380 | 2009-02-27 18:09:09 +0530 | [diff] [blame] | 2601 | |
Cyrill Gorcunov | 1255803 | 2009-12-10 19:56:34 +0300 | [diff] [blame] | 2602 | pmu_check_apic(); |
| 2603 | |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2604 | pr_cont("%s PMU driver.\n", x86_pmu.name); |
Robert Richter | faa28ae | 2009-04-29 12:47:13 +0200 | [diff] [blame] | 2605 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2606 | if (x86_pmu.num_events > X86_PMC_MAX_GENERIC) { |
| 2607 | WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!", |
| 2608 | x86_pmu.num_events, X86_PMC_MAX_GENERIC); |
| 2609 | x86_pmu.num_events = X86_PMC_MAX_GENERIC; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2610 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2611 | perf_event_mask = (1 << x86_pmu.num_events) - 1; |
| 2612 | perf_max_events = x86_pmu.num_events; |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2613 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2614 | if (x86_pmu.num_events_fixed > X86_PMC_MAX_FIXED) { |
| 2615 | WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!", |
| 2616 | x86_pmu.num_events_fixed, X86_PMC_MAX_FIXED); |
| 2617 | x86_pmu.num_events_fixed = X86_PMC_MAX_FIXED; |
Ingo Molnar | 703e937 | 2008-12-17 10:51:15 +0100 | [diff] [blame] | 2618 | } |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2619 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2620 | perf_event_mask |= |
| 2621 | ((1LL << x86_pmu.num_events_fixed)-1) << X86_PMC_IDX_FIXED; |
| 2622 | x86_pmu.intel_ctrl = perf_event_mask; |
Ingo Molnar | 862a1a5 | 2008-12-17 13:09:20 +0100 | [diff] [blame] | 2623 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2624 | perf_events_lapic_init(); |
| 2625 | register_die_notifier(&perf_event_nmi_notifier); |
Ingo Molnar | 1123e3a | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2626 | |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 2627 | unconstrained = (struct event_constraint) |
Peter Zijlstra | fce877e | 2010-01-29 13:25:12 +0100 | [diff] [blame^] | 2628 | __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_events) - 1, |
| 2629 | 0, x86_pmu.num_events); |
Peter Zijlstra | 63b1464 | 2010-01-22 16:32:17 +0100 | [diff] [blame] | 2630 | |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 2631 | pr_info("... version: %d\n", x86_pmu.version); |
| 2632 | pr_info("... bit width: %d\n", x86_pmu.event_bits); |
| 2633 | pr_info("... generic registers: %d\n", x86_pmu.num_events); |
| 2634 | pr_info("... value mask: %016Lx\n", x86_pmu.event_mask); |
| 2635 | pr_info("... max period: %016Lx\n", x86_pmu.max_period); |
| 2636 | pr_info("... fixed-purpose events: %d\n", x86_pmu.num_events_fixed); |
| 2637 | pr_info("... event mask: %016Lx\n", perf_event_mask); |
Ingo Molnar | 241771e | 2008-12-03 10:39:53 +0100 | [diff] [blame] | 2638 | } |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 2639 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2640 | static inline void x86_pmu_read(struct perf_event *event) |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 2641 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2642 | x86_perf_event_update(event, &event->hw, event->hw.idx); |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 2643 | } |
| 2644 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 2645 | static const struct pmu pmu = { |
| 2646 | .enable = x86_pmu_enable, |
| 2647 | .disable = x86_pmu_disable, |
| 2648 | .read = x86_pmu_read, |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 2649 | .unthrottle = x86_pmu_unthrottle, |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 2650 | }; |
| 2651 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2652 | /* |
| 2653 | * validate a single event group |
| 2654 | * |
| 2655 | * validation include: |
Ingo Molnar | 184f412 | 2010-01-27 08:39:39 +0100 | [diff] [blame] | 2656 | * - check events are compatible which each other |
| 2657 | * - events do not compete for the same counter |
| 2658 | * - number of events <= number of counters |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2659 | * |
| 2660 | * validation ensures the group can be loaded onto the |
| 2661 | * PMU if it was the only group available. |
| 2662 | */ |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 2663 | static int validate_group(struct perf_event *event) |
| 2664 | { |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2665 | struct perf_event *leader = event->group_leader; |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 2666 | struct cpu_hw_events *fake_cpuc; |
| 2667 | int ret, n; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 2668 | |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 2669 | ret = -ENOMEM; |
| 2670 | fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO); |
| 2671 | if (!fake_cpuc) |
| 2672 | goto out; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 2673 | |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2674 | /* |
| 2675 | * the event is not yet connected with its |
| 2676 | * siblings therefore we must first collect |
| 2677 | * existing siblings, then add the new event |
| 2678 | * before we can simulate the scheduling |
| 2679 | */ |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 2680 | ret = -ENOSPC; |
| 2681 | n = collect_events(fake_cpuc, leader, true); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2682 | if (n < 0) |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 2683 | goto out_free; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 2684 | |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 2685 | fake_cpuc->n_events = n; |
| 2686 | n = collect_events(fake_cpuc, event, false); |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2687 | if (n < 0) |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 2688 | goto out_free; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 2689 | |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 2690 | fake_cpuc->n_events = n; |
Stephane Eranian | 1da53e0 | 2010-01-18 10:58:01 +0200 | [diff] [blame] | 2691 | |
Peter Zijlstra | 502568d | 2010-01-22 14:35:46 +0100 | [diff] [blame] | 2692 | ret = x86_schedule_events(fake_cpuc, n, NULL); |
| 2693 | |
| 2694 | out_free: |
| 2695 | kfree(fake_cpuc); |
| 2696 | out: |
| 2697 | return ret; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 2698 | } |
| 2699 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2700 | const struct pmu *hw_perf_event_init(struct perf_event *event) |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 2701 | { |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 2702 | const struct pmu *tmp; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 2703 | int err; |
| 2704 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2705 | err = __hw_perf_event_init(event); |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 2706 | if (!err) { |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 2707 | /* |
| 2708 | * we temporarily connect event to its pmu |
| 2709 | * such that validate_group() can classify |
| 2710 | * it as an x86 event using is_x86_event() |
| 2711 | */ |
| 2712 | tmp = event->pmu; |
| 2713 | event->pmu = &pmu; |
| 2714 | |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 2715 | if (event->group_leader != event) |
| 2716 | err = validate_group(event); |
Stephane Eranian | 8113070 | 2010-01-21 17:39:01 +0200 | [diff] [blame] | 2717 | |
| 2718 | event->pmu = tmp; |
Peter Zijlstra | fe9081c | 2009-10-08 11:56:07 +0200 | [diff] [blame] | 2719 | } |
Peter Zijlstra | a1792cdac | 2009-09-09 10:04:47 +0200 | [diff] [blame] | 2720 | if (err) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2721 | if (event->destroy) |
| 2722 | event->destroy(event); |
Peter Zijlstra | 9ea98e1 | 2009-03-30 19:07:09 +0200 | [diff] [blame] | 2723 | return ERR_PTR(err); |
Peter Zijlstra | a1792cdac | 2009-09-09 10:04:47 +0200 | [diff] [blame] | 2724 | } |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 2725 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 2726 | return &pmu; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 2727 | } |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2728 | |
| 2729 | /* |
| 2730 | * callchain support |
| 2731 | */ |
| 2732 | |
| 2733 | static inline |
Peter Zijlstra | f9188e0 | 2009-06-18 22:20:52 +0200 | [diff] [blame] | 2734 | void callchain_store(struct perf_callchain_entry *entry, u64 ip) |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2735 | { |
Peter Zijlstra | f9188e0 | 2009-06-18 22:20:52 +0200 | [diff] [blame] | 2736 | if (entry->nr < PERF_MAX_STACK_DEPTH) |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2737 | entry->ip[entry->nr++] = ip; |
| 2738 | } |
| 2739 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 2740 | static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_irq_entry); |
| 2741 | static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_nmi_entry); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2742 | |
| 2743 | |
| 2744 | static void |
| 2745 | backtrace_warning_symbol(void *data, char *msg, unsigned long symbol) |
| 2746 | { |
| 2747 | /* Ignore warnings */ |
| 2748 | } |
| 2749 | |
| 2750 | static void backtrace_warning(void *data, char *msg) |
| 2751 | { |
| 2752 | /* Ignore warnings */ |
| 2753 | } |
| 2754 | |
| 2755 | static int backtrace_stack(void *data, char *name) |
| 2756 | { |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 2757 | return 0; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | static void backtrace_address(void *data, unsigned long addr, int reliable) |
| 2761 | { |
| 2762 | struct perf_callchain_entry *entry = data; |
| 2763 | |
| 2764 | if (reliable) |
| 2765 | callchain_store(entry, addr); |
| 2766 | } |
| 2767 | |
| 2768 | static const struct stacktrace_ops backtrace_ops = { |
| 2769 | .warning = backtrace_warning, |
| 2770 | .warning_symbol = backtrace_warning_symbol, |
| 2771 | .stack = backtrace_stack, |
| 2772 | .address = backtrace_address, |
Frederic Weisbecker | 06d65bd | 2009-12-17 05:40:34 +0100 | [diff] [blame] | 2773 | .walk_stack = print_context_stack_bp, |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2774 | }; |
| 2775 | |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 2776 | #include "../dumpstack.h" |
| 2777 | |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2778 | static void |
| 2779 | perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry) |
| 2780 | { |
Peter Zijlstra | f9188e0 | 2009-06-18 22:20:52 +0200 | [diff] [blame] | 2781 | callchain_store(entry, PERF_CONTEXT_KERNEL); |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 2782 | callchain_store(entry, regs->ip); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2783 | |
Frederic Weisbecker | 48b5ba9 | 2009-12-31 05:53:02 +0100 | [diff] [blame] | 2784 | dump_trace(NULL, regs, NULL, regs->bp, &backtrace_ops, entry); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2785 | } |
| 2786 | |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 2787 | /* |
| 2788 | * best effort, GUP based copy_from_user() that assumes IRQ or NMI context |
| 2789 | */ |
| 2790 | static unsigned long |
| 2791 | 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] | 2792 | { |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 2793 | unsigned long offset, addr = (unsigned long)from; |
| 2794 | int type = in_nmi() ? KM_NMI : KM_IRQ0; |
| 2795 | unsigned long size, len = 0; |
| 2796 | struct page *page; |
| 2797 | void *map; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2798 | int ret; |
| 2799 | |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 2800 | do { |
| 2801 | ret = __get_user_pages_fast(addr, 1, 0, &page); |
| 2802 | if (!ret) |
| 2803 | break; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2804 | |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 2805 | offset = addr & (PAGE_SIZE - 1); |
| 2806 | size = min(PAGE_SIZE - offset, n - len); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2807 | |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 2808 | map = kmap_atomic(page, type); |
| 2809 | memcpy(to, map+offset, size); |
| 2810 | kunmap_atomic(map, type); |
| 2811 | put_page(page); |
| 2812 | |
| 2813 | len += size; |
| 2814 | to += size; |
| 2815 | addr += size; |
| 2816 | |
| 2817 | } while (len < n); |
| 2818 | |
| 2819 | return len; |
| 2820 | } |
| 2821 | |
| 2822 | static int copy_stack_frame(const void __user *fp, struct stack_frame *frame) |
| 2823 | { |
| 2824 | unsigned long bytes; |
| 2825 | |
| 2826 | bytes = copy_from_user_nmi(frame, fp, sizeof(*frame)); |
| 2827 | |
| 2828 | return bytes == sizeof(*frame); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2829 | } |
| 2830 | |
| 2831 | static void |
| 2832 | perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry) |
| 2833 | { |
| 2834 | struct stack_frame frame; |
| 2835 | const void __user *fp; |
| 2836 | |
Ingo Molnar | 5a6cec3 | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2837 | if (!user_mode(regs)) |
| 2838 | regs = task_pt_regs(current); |
| 2839 | |
Peter Zijlstra | 74193ef | 2009-06-15 13:07:24 +0200 | [diff] [blame] | 2840 | fp = (void __user *)regs->bp; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2841 | |
Peter Zijlstra | f9188e0 | 2009-06-18 22:20:52 +0200 | [diff] [blame] | 2842 | callchain_store(entry, PERF_CONTEXT_USER); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2843 | callchain_store(entry, regs->ip); |
| 2844 | |
Peter Zijlstra | f9188e0 | 2009-06-18 22:20:52 +0200 | [diff] [blame] | 2845 | while (entry->nr < PERF_MAX_STACK_DEPTH) { |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 2846 | frame.next_frame = NULL; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2847 | frame.return_address = 0; |
| 2848 | |
| 2849 | if (!copy_stack_frame(fp, &frame)) |
| 2850 | break; |
| 2851 | |
Ingo Molnar | 5a6cec3 | 2009-05-29 11:25:09 +0200 | [diff] [blame] | 2852 | if ((unsigned long)fp < regs->sp) |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2853 | break; |
| 2854 | |
| 2855 | callchain_store(entry, frame.return_address); |
Ingo Molnar | 038e836 | 2009-06-15 09:57:59 +0200 | [diff] [blame] | 2856 | fp = frame.next_frame; |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2857 | } |
| 2858 | } |
| 2859 | |
| 2860 | static void |
| 2861 | perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry) |
| 2862 | { |
| 2863 | int is_user; |
| 2864 | |
| 2865 | if (!regs) |
| 2866 | return; |
| 2867 | |
| 2868 | is_user = user_mode(regs); |
| 2869 | |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2870 | if (is_user && current->state != TASK_RUNNING) |
| 2871 | return; |
| 2872 | |
| 2873 | if (!is_user) |
| 2874 | perf_callchain_kernel(regs, entry); |
| 2875 | |
| 2876 | if (current->mm) |
| 2877 | perf_callchain_user(regs, entry); |
| 2878 | } |
| 2879 | |
| 2880 | struct perf_callchain_entry *perf_callchain(struct pt_regs *regs) |
| 2881 | { |
| 2882 | struct perf_callchain_entry *entry; |
| 2883 | |
| 2884 | if (in_nmi()) |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 2885 | entry = &__get_cpu_var(pmc_nmi_entry); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2886 | else |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 2887 | entry = &__get_cpu_var(pmc_irq_entry); |
Peter Zijlstra | d7d59fb | 2009-03-30 19:07:15 +0200 | [diff] [blame] | 2888 | |
| 2889 | entry->nr = 0; |
| 2890 | |
| 2891 | perf_do_callchain(regs, entry); |
| 2892 | |
| 2893 | return entry; |
| 2894 | } |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 2895 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2896 | void hw_perf_event_setup_online(int cpu) |
Markus Metzger | 30dd568 | 2009-07-21 15:56:48 +0200 | [diff] [blame] | 2897 | { |
| 2898 | init_debug_store_on_cpu(cpu); |
| 2899 | } |