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