Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 1 | #undef DEBUG |
| 2 | |
| 3 | /* |
| 4 | * ARM performance counter support. |
| 5 | * |
| 6 | * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles |
Will Deacon | 43eab87 | 2010-11-13 19:04:32 +0000 | [diff] [blame] | 7 | * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com> |
Jean PIHET | 796d129 | 2010-01-26 18:51:05 +0100 | [diff] [blame] | 8 | * |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 9 | * This code is based on the sparc64 perf event code, which is in turn based |
| 10 | * on the x86 code. Callchain code is based on the ARM OProfile backtrace |
| 11 | * code. |
| 12 | */ |
| 13 | #define pr_fmt(fmt) "hw perfevents: " fmt |
| 14 | |
Mark Rutland | 7325eae | 2011-08-23 11:59:49 +0100 | [diff] [blame] | 15 | #include <linux/bitmap.h> |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/kernel.h> |
Paul Gortmaker | ecea4ab | 2011-07-22 10:58:34 -0400 | [diff] [blame] | 18 | #include <linux/export.h> |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 19 | #include <linux/perf_event.h> |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 21 | #include <linux/spinlock.h> |
| 22 | #include <linux/uaccess.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 23 | #include <linux/irq.h> |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 24 | |
| 25 | #include <asm/cputype.h> |
| 26 | #include <asm/irq.h> |
| 27 | #include <asm/irq_regs.h> |
| 28 | #include <asm/pmu.h> |
| 29 | #include <asm/stacktrace.h> |
| 30 | |
Ashwin Chaugule | f53fe44 | 2012-06-07 13:41:37 -0400 | [diff] [blame] | 31 | #include <linux/cpu_pm.h> |
| 32 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 33 | /* |
Will Deacon | ecf5a89 | 2011-07-19 22:43:28 +0100 | [diff] [blame] | 34 | * ARMv6 supports a maximum of 3 events, starting from index 0. If we add |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 35 | * another platform that supports more, we need to increase this to be the |
| 36 | * largest of all platforms. |
Jean PIHET | 796d129 | 2010-01-26 18:51:05 +0100 | [diff] [blame] | 37 | * |
| 38 | * ARMv7 supports up to 32 events: |
| 39 | * cycle counter CCNT + 31 events counters CNT0..30. |
| 40 | * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters. |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 41 | */ |
Will Deacon | ecf5a89 | 2011-07-19 22:43:28 +0100 | [diff] [blame] | 42 | #define ARMPMU_MAX_HWEVENTS 32 |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 43 | |
Mark Rutland | 3fc2c83 | 2011-06-24 11:30:59 +0100 | [diff] [blame] | 44 | static DEFINE_PER_CPU(struct perf_event * [ARMPMU_MAX_HWEVENTS], hw_events); |
| 45 | static DEFINE_PER_CPU(unsigned long [BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)], used_mask); |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 46 | static DEFINE_PER_CPU(struct pmu_hw_events, cpu_hw_events); |
Will Deacon | 181193f | 2010-04-30 11:32:44 +0100 | [diff] [blame] | 47 | |
Mark Rutland | 8a16b34 | 2011-04-28 16:27:54 +0100 | [diff] [blame] | 48 | #define to_arm_pmu(p) (container_of(p, struct arm_pmu, pmu)) |
| 49 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 50 | /* Set at runtime when we know what CPU type we are. */ |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 51 | static struct arm_pmu *cpu_pmu; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 52 | |
Will Deacon | 181193f | 2010-04-30 11:32:44 +0100 | [diff] [blame] | 53 | enum arm_perf_pmu_ids |
| 54 | armpmu_get_pmu_id(void) |
| 55 | { |
| 56 | int id = -ENODEV; |
| 57 | |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 58 | if (cpu_pmu != NULL) |
| 59 | id = cpu_pmu->id; |
Will Deacon | 181193f | 2010-04-30 11:32:44 +0100 | [diff] [blame] | 60 | |
| 61 | return id; |
| 62 | } |
| 63 | EXPORT_SYMBOL_GPL(armpmu_get_pmu_id); |
| 64 | |
Will Deacon | feb45d0 | 2011-11-14 10:33:05 +0000 | [diff] [blame] | 65 | int perf_num_counters(void) |
Will Deacon | 929f519 | 2010-04-30 11:34:26 +0100 | [diff] [blame] | 66 | { |
| 67 | int max_events = 0; |
| 68 | |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 69 | if (cpu_pmu != NULL) |
| 70 | max_events = cpu_pmu->num_events; |
Will Deacon | 929f519 | 2010-04-30 11:34:26 +0100 | [diff] [blame] | 71 | |
| 72 | return max_events; |
| 73 | } |
Matt Fleming | 3bf101b | 2010-09-27 20:22:24 +0100 | [diff] [blame] | 74 | EXPORT_SYMBOL_GPL(perf_num_counters); |
| 75 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 76 | #define HW_OP_UNSUPPORTED 0xFFFF |
| 77 | |
| 78 | #define C(_x) \ |
| 79 | PERF_COUNT_HW_CACHE_##_x |
| 80 | |
| 81 | #define CACHE_OP_UNSUPPORTED 0xFFFF |
| 82 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 83 | static int |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 84 | armpmu_map_cache_event(unsigned (*cache_map) |
Mark Rutland | e1f431b | 2011-04-28 15:47:10 +0100 | [diff] [blame] | 85 | [PERF_COUNT_HW_CACHE_MAX] |
| 86 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 87 | [PERF_COUNT_HW_CACHE_RESULT_MAX], |
| 88 | u64 config) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 89 | { |
| 90 | unsigned int cache_type, cache_op, cache_result, ret; |
| 91 | |
| 92 | cache_type = (config >> 0) & 0xff; |
| 93 | if (cache_type >= PERF_COUNT_HW_CACHE_MAX) |
| 94 | return -EINVAL; |
| 95 | |
| 96 | cache_op = (config >> 8) & 0xff; |
| 97 | if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX) |
| 98 | return -EINVAL; |
| 99 | |
| 100 | cache_result = (config >> 16) & 0xff; |
| 101 | if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX) |
| 102 | return -EINVAL; |
| 103 | |
Mark Rutland | e1f431b | 2011-04-28 15:47:10 +0100 | [diff] [blame] | 104 | ret = (int)(*cache_map)[cache_type][cache_op][cache_result]; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 105 | |
| 106 | if (ret == CACHE_OP_UNSUPPORTED) |
| 107 | return -ENOENT; |
| 108 | |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | static int |
Mark Rutland | e1f431b | 2011-04-28 15:47:10 +0100 | [diff] [blame] | 113 | armpmu_map_event(const unsigned (*event_map)[PERF_COUNT_HW_MAX], u64 config) |
Will Deacon | 84fee97 | 2010-11-13 17:13:56 +0000 | [diff] [blame] | 114 | { |
Mark Rutland | e1f431b | 2011-04-28 15:47:10 +0100 | [diff] [blame] | 115 | int mapping = (*event_map)[config]; |
| 116 | return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping; |
Will Deacon | 84fee97 | 2010-11-13 17:13:56 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static int |
Mark Rutland | e1f431b | 2011-04-28 15:47:10 +0100 | [diff] [blame] | 120 | armpmu_map_raw_event(u32 raw_event_mask, u64 config) |
Will Deacon | 84fee97 | 2010-11-13 17:13:56 +0000 | [diff] [blame] | 121 | { |
Mark Rutland | e1f431b | 2011-04-28 15:47:10 +0100 | [diff] [blame] | 122 | return (int)(config & raw_event_mask); |
| 123 | } |
| 124 | |
| 125 | static int map_cpu_event(struct perf_event *event, |
| 126 | const unsigned (*event_map)[PERF_COUNT_HW_MAX], |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 127 | unsigned (*cache_map) |
Mark Rutland | e1f431b | 2011-04-28 15:47:10 +0100 | [diff] [blame] | 128 | [PERF_COUNT_HW_CACHE_MAX] |
| 129 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 130 | [PERF_COUNT_HW_CACHE_RESULT_MAX], |
| 131 | u32 raw_event_mask) |
| 132 | { |
| 133 | u64 config = event->attr.config; |
| 134 | |
| 135 | switch (event->attr.type) { |
| 136 | case PERF_TYPE_HARDWARE: |
| 137 | return armpmu_map_event(event_map, config); |
| 138 | case PERF_TYPE_HW_CACHE: |
| 139 | return armpmu_map_cache_event(cache_map, config); |
| 140 | case PERF_TYPE_RAW: |
| 141 | return armpmu_map_raw_event(raw_event_mask, config); |
| 142 | } |
| 143 | |
| 144 | return -ENOENT; |
Will Deacon | 84fee97 | 2010-11-13 17:13:56 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Mark Rutland | 0ce4708 | 2011-05-19 10:07:57 +0100 | [diff] [blame] | 147 | int |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 148 | armpmu_event_set_period(struct perf_event *event, |
| 149 | struct hw_perf_event *hwc, |
| 150 | int idx) |
| 151 | { |
Mark Rutland | 8a16b34 | 2011-04-28 16:27:54 +0100 | [diff] [blame] | 152 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 153 | s64 left = local64_read(&hwc->period_left); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 154 | s64 period = hwc->sample_period; |
| 155 | int ret = 0; |
| 156 | |
| 157 | if (unlikely(left <= -period)) { |
| 158 | left = period; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 159 | local64_set(&hwc->period_left, left); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 160 | hwc->last_period = period; |
| 161 | ret = 1; |
| 162 | } |
| 163 | |
| 164 | if (unlikely(left <= 0)) { |
| 165 | left += period; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 166 | local64_set(&hwc->period_left, left); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 167 | hwc->last_period = period; |
| 168 | ret = 1; |
| 169 | } |
| 170 | |
| 171 | if (left > (s64)armpmu->max_period) |
| 172 | left = armpmu->max_period; |
| 173 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 174 | local64_set(&hwc->prev_count, (u64)-left); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 175 | |
| 176 | armpmu->write_counter(idx, (u64)(-left) & 0xffffffff); |
| 177 | |
| 178 | perf_event_update_userpage(event); |
| 179 | |
| 180 | return ret; |
| 181 | } |
| 182 | |
Mark Rutland | 0ce4708 | 2011-05-19 10:07:57 +0100 | [diff] [blame] | 183 | u64 |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 184 | armpmu_event_update(struct perf_event *event, |
| 185 | struct hw_perf_event *hwc, |
Will Deacon | 5727347 | 2012-03-06 17:33:17 +0100 | [diff] [blame] | 186 | int idx) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 187 | { |
Mark Rutland | 8a16b34 | 2011-04-28 16:27:54 +0100 | [diff] [blame] | 188 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
Will Deacon | a737823 | 2011-03-25 17:12:37 +0100 | [diff] [blame] | 189 | u64 delta, prev_raw_count, new_raw_count; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 190 | |
| 191 | again: |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 192 | prev_raw_count = local64_read(&hwc->prev_count); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 193 | new_raw_count = armpmu->read_counter(idx); |
| 194 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 195 | if (local64_cmpxchg(&hwc->prev_count, prev_raw_count, |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 196 | new_raw_count) != prev_raw_count) |
| 197 | goto again; |
| 198 | |
Will Deacon | 5727347 | 2012-03-06 17:33:17 +0100 | [diff] [blame] | 199 | delta = (new_raw_count - prev_raw_count) & armpmu->max_period; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 200 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 201 | local64_add(delta, &event->count); |
| 202 | local64_sub(delta, &hwc->period_left); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 203 | |
| 204 | return new_raw_count; |
| 205 | } |
| 206 | |
| 207 | static void |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 208 | armpmu_read(struct perf_event *event) |
| 209 | { |
| 210 | struct hw_perf_event *hwc = &event->hw; |
| 211 | |
| 212 | /* Don't read disabled counters! */ |
| 213 | if (hwc->idx < 0) |
| 214 | return; |
| 215 | |
Will Deacon | 5727347 | 2012-03-06 17:33:17 +0100 | [diff] [blame] | 216 | armpmu_event_update(event, hwc, hwc->idx); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static void |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 220 | armpmu_stop(struct perf_event *event, int flags) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 221 | { |
Mark Rutland | 8a16b34 | 2011-04-28 16:27:54 +0100 | [diff] [blame] | 222 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 223 | struct hw_perf_event *hwc = &event->hw; |
| 224 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 225 | /* |
| 226 | * ARM pmu always has to update the counter, so ignore |
| 227 | * PERF_EF_UPDATE, see comments in armpmu_start(). |
| 228 | */ |
| 229 | if (!(hwc->state & PERF_HES_STOPPED)) { |
| 230 | armpmu->disable(hwc, hwc->idx); |
| 231 | barrier(); /* why? */ |
Will Deacon | 5727347 | 2012-03-06 17:33:17 +0100 | [diff] [blame] | 232 | armpmu_event_update(event, hwc, hwc->idx); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 233 | hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | static void |
| 238 | armpmu_start(struct perf_event *event, int flags) |
| 239 | { |
Mark Rutland | 8a16b34 | 2011-04-28 16:27:54 +0100 | [diff] [blame] | 240 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 241 | struct hw_perf_event *hwc = &event->hw; |
| 242 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 243 | /* |
| 244 | * ARM pmu always has to reprogram the period, so ignore |
| 245 | * PERF_EF_RELOAD, see the comment below. |
| 246 | */ |
| 247 | if (flags & PERF_EF_RELOAD) |
| 248 | WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE)); |
| 249 | |
| 250 | hwc->state = 0; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 251 | /* |
| 252 | * Set the period again. Some counters can't be stopped, so when we |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 253 | * were stopped we simply disabled the IRQ source and the counter |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 254 | * may have been left counting. If we don't do this step then we may |
| 255 | * get an interrupt too soon or *way* too late if the overflow has |
| 256 | * happened since disabling. |
| 257 | */ |
| 258 | armpmu_event_set_period(event, hwc, hwc->idx); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 259 | armpmu->enable(hwc, hwc->idx, event->cpu); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 260 | } |
| 261 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 262 | static void |
| 263 | armpmu_del(struct perf_event *event, int flags) |
| 264 | { |
Mark Rutland | 8a16b34 | 2011-04-28 16:27:54 +0100 | [diff] [blame] | 265 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 266 | struct pmu_hw_events *hw_events = armpmu->get_hw_events(); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 267 | struct hw_perf_event *hwc = &event->hw; |
| 268 | int idx = hwc->idx; |
| 269 | |
| 270 | WARN_ON(idx < 0); |
| 271 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 272 | armpmu_stop(event, PERF_EF_UPDATE); |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 273 | hw_events->events[idx] = NULL; |
| 274 | clear_bit(idx, hw_events->used_mask); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 275 | |
| 276 | perf_event_update_userpage(event); |
| 277 | } |
| 278 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 279 | static int |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 280 | armpmu_add(struct perf_event *event, int flags) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 281 | { |
Mark Rutland | 8a16b34 | 2011-04-28 16:27:54 +0100 | [diff] [blame] | 282 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 283 | struct pmu_hw_events *hw_events = armpmu->get_hw_events(); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 284 | struct hw_perf_event *hwc = &event->hw; |
| 285 | int idx; |
| 286 | int err = 0; |
| 287 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 288 | perf_pmu_disable(event->pmu); |
Peter Zijlstra | 24cd7f5 | 2010-06-11 17:32:03 +0200 | [diff] [blame] | 289 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 290 | /* If we don't have a space for the counter then finish early. */ |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 291 | idx = armpmu->get_event_idx(hw_events, hwc); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 292 | if (idx < 0) { |
| 293 | err = idx; |
| 294 | goto out; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * If there is an event in the counter we are going to use then make |
| 299 | * sure it is disabled. |
| 300 | */ |
| 301 | event->hw.idx = idx; |
| 302 | armpmu->disable(hwc, idx); |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 303 | hw_events->events[idx] = event; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 304 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 305 | hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE; |
| 306 | if (flags & PERF_EF_START) |
| 307 | armpmu_start(event, PERF_EF_RELOAD); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 308 | |
| 309 | /* Propagate our changes to the userspace mapping. */ |
| 310 | perf_event_update_userpage(event); |
| 311 | |
| 312 | out: |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 313 | perf_pmu_enable(event->pmu); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 314 | return err; |
| 315 | } |
| 316 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 317 | static int |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 318 | validate_event(struct pmu_hw_events *hw_events, |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 319 | struct perf_event *event) |
| 320 | { |
Mark Rutland | 8a16b34 | 2011-04-28 16:27:54 +0100 | [diff] [blame] | 321 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 322 | struct hw_perf_event fake_event = event->hw; |
Mark Rutland | 7b9f72c | 2011-04-27 16:22:21 +0100 | [diff] [blame] | 323 | struct pmu *leader_pmu = event->group_leader->pmu; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 324 | |
Mark Rutland | 7b9f72c | 2011-04-27 16:22:21 +0100 | [diff] [blame] | 325 | if (event->pmu != leader_pmu || event->state <= PERF_EVENT_STATE_OFF) |
Will Deacon | 65b4711 | 2010-09-02 09:32:08 +0100 | [diff] [blame] | 326 | return 1; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 327 | |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 328 | return armpmu->get_event_idx(hw_events, &fake_event) >= 0; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | static int |
| 332 | validate_group(struct perf_event *event) |
| 333 | { |
| 334 | struct perf_event *sibling, *leader = event->group_leader; |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 335 | struct pmu_hw_events fake_pmu; |
Will Deacon | bce34d1 | 2011-11-17 15:05:14 +0000 | [diff] [blame] | 336 | DECLARE_BITMAP(fake_used_mask, ARMPMU_MAX_HWEVENTS); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 337 | |
Will Deacon | bce34d1 | 2011-11-17 15:05:14 +0000 | [diff] [blame] | 338 | /* |
| 339 | * Initialise the fake PMU. We only need to populate the |
| 340 | * used_mask for the purposes of validation. |
| 341 | */ |
| 342 | memset(fake_used_mask, 0, sizeof(fake_used_mask)); |
| 343 | fake_pmu.used_mask = fake_used_mask; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 344 | |
| 345 | if (!validate_event(&fake_pmu, leader)) |
Peter Zijlstra | aa2bc1a | 2011-11-09 17:56:37 +0100 | [diff] [blame] | 346 | return -EINVAL; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 347 | |
| 348 | list_for_each_entry(sibling, &leader->sibling_list, group_entry) { |
| 349 | if (!validate_event(&fake_pmu, sibling)) |
Peter Zijlstra | aa2bc1a | 2011-11-09 17:56:37 +0100 | [diff] [blame] | 350 | return -EINVAL; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | if (!validate_event(&fake_pmu, event)) |
Peter Zijlstra | aa2bc1a | 2011-11-09 17:56:37 +0100 | [diff] [blame] | 354 | return -EINVAL; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
Rabin Vincent | 0e25a5c | 2011-02-08 09:24:36 +0530 | [diff] [blame] | 359 | static irqreturn_t armpmu_platform_irq(int irq, void *dev) |
| 360 | { |
Mark Rutland | 8a16b34 | 2011-04-28 16:27:54 +0100 | [diff] [blame] | 361 | struct arm_pmu *armpmu = (struct arm_pmu *) dev; |
Mark Rutland | a9356a0 | 2011-05-04 09:23:15 +0100 | [diff] [blame] | 362 | struct platform_device *plat_device = armpmu->plat_device; |
| 363 | struct arm_pmu_platdata *plat = dev_get_platdata(&plat_device->dev); |
Rabin Vincent | 0e25a5c | 2011-02-08 09:24:36 +0530 | [diff] [blame] | 364 | |
| 365 | return plat->handle_irq(irq, dev, armpmu->handle_irq); |
| 366 | } |
| 367 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 368 | int |
Ashwin Chaugule | 4afdedc | 2012-01-17 13:23:50 -0500 | [diff] [blame] | 369 | armpmu_generic_request_irq(int irq, irq_handler_t *handle_irq) |
| 370 | { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 371 | return request_irq(irq, *handle_irq, |
| 372 | IRQF_DISABLED | IRQF_NOBALANCING, |
| 373 | "armpmu", NULL); |
| 374 | } |
| 375 | |
| 376 | void |
| 377 | armpmu_generic_free_irq(int irq) |
| 378 | { |
| 379 | if (irq >= 0) |
| 380 | free_irq(irq, NULL); |
Ashwin Chaugule | 4afdedc | 2012-01-17 13:23:50 -0500 | [diff] [blame] | 381 | } |
| 382 | |
Will Deacon | 0b390e2 | 2011-07-27 15:18:59 +0100 | [diff] [blame] | 383 | static void |
Mark Rutland | 8a16b34 | 2011-04-28 16:27:54 +0100 | [diff] [blame] | 384 | armpmu_release_hardware(struct arm_pmu *armpmu) |
Will Deacon | 0b390e2 | 2011-07-27 15:18:59 +0100 | [diff] [blame] | 385 | { |
| 386 | int i, irq, irqs; |
Mark Rutland | a9356a0 | 2011-05-04 09:23:15 +0100 | [diff] [blame] | 387 | struct platform_device *pmu_device = armpmu->plat_device; |
Ashwin Chaugule | f53fe44 | 2012-06-07 13:41:37 -0400 | [diff] [blame] | 388 | |
Will Deacon | 0b390e2 | 2011-07-27 15:18:59 +0100 | [diff] [blame] | 389 | irqs = min(pmu_device->num_resources, num_possible_cpus()); |
| 390 | |
| 391 | for (i = 0; i < irqs; ++i) { |
| 392 | if (!cpumask_test_and_clear_cpu(i, &armpmu->active_irqs)) |
| 393 | continue; |
| 394 | irq = platform_get_irq(pmu_device, i); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 395 | armpmu->free_pmu_irq(irq); |
Will Deacon | 0b390e2 | 2011-07-27 15:18:59 +0100 | [diff] [blame] | 396 | } |
| 397 | |
Mark Rutland | 7ae18a5 | 2011-06-06 10:37:50 +0100 | [diff] [blame] | 398 | release_pmu(armpmu->type); |
Will Deacon | 0b390e2 | 2011-07-27 15:18:59 +0100 | [diff] [blame] | 399 | } |
| 400 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 401 | static int |
Mark Rutland | 8a16b34 | 2011-04-28 16:27:54 +0100 | [diff] [blame] | 402 | armpmu_reserve_hardware(struct arm_pmu *armpmu) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 403 | { |
Rabin Vincent | 0e25a5c | 2011-02-08 09:24:36 +0530 | [diff] [blame] | 404 | struct arm_pmu_platdata *plat; |
| 405 | irq_handler_t handle_irq; |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 406 | int i, err, irq, irqs; |
Mark Rutland | a9356a0 | 2011-05-04 09:23:15 +0100 | [diff] [blame] | 407 | struct platform_device *pmu_device = armpmu->plat_device; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 408 | |
Will Deacon | e5a2132 | 2011-11-22 18:01:46 +0000 | [diff] [blame] | 409 | if (!pmu_device) |
| 410 | return -ENODEV; |
| 411 | |
Mark Rutland | 7ae18a5 | 2011-06-06 10:37:50 +0100 | [diff] [blame] | 412 | err = reserve_pmu(armpmu->type); |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 413 | if (err) { |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 414 | pr_warning("unable to reserve pmu\n"); |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 415 | return err; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 416 | } |
| 417 | |
Rabin Vincent | 0e25a5c | 2011-02-08 09:24:36 +0530 | [diff] [blame] | 418 | plat = dev_get_platdata(&pmu_device->dev); |
| 419 | if (plat && plat->handle_irq) |
| 420 | handle_irq = armpmu_platform_irq; |
| 421 | else |
| 422 | handle_irq = armpmu->handle_irq; |
| 423 | |
Will Deacon | 0b390e2 | 2011-07-27 15:18:59 +0100 | [diff] [blame] | 424 | irqs = min(pmu_device->num_resources, num_possible_cpus()); |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 425 | if (irqs < 1) { |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 426 | pr_err("no irqs for PMUs defined\n"); |
| 427 | return -ENODEV; |
| 428 | } |
| 429 | |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 430 | for (i = 0; i < irqs; ++i) { |
Will Deacon | 0b390e2 | 2011-07-27 15:18:59 +0100 | [diff] [blame] | 431 | err = 0; |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 432 | irq = platform_get_irq(pmu_device, i); |
| 433 | if (irq < 0) |
| 434 | continue; |
| 435 | |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 436 | /* |
| 437 | * If we have a single PMU interrupt that we can't shift, |
| 438 | * assume that we're running on a uniprocessor machine and |
Will Deacon | 0b390e2 | 2011-07-27 15:18:59 +0100 | [diff] [blame] | 439 | * continue. Otherwise, continue without this interrupt. |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 440 | */ |
Will Deacon | 0b390e2 | 2011-07-27 15:18:59 +0100 | [diff] [blame] | 441 | if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) { |
| 442 | pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n", |
| 443 | irq, i); |
| 444 | continue; |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 445 | } |
| 446 | |
Ashwin Chaugule | 4afdedc | 2012-01-17 13:23:50 -0500 | [diff] [blame] | 447 | err = armpmu->request_pmu_irq(irq, &handle_irq); |
| 448 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 449 | if (err) { |
| 450 | pr_warning("unable to request IRQ%d for %s perf " |
| 451 | "counters\n", irq, armpmu->name); |
| 452 | |
| 453 | armpmu_release_hardware(cpu_pmu); |
| 454 | return err; |
| 455 | } |
| 456 | |
Will Deacon | 0b390e2 | 2011-07-27 15:18:59 +0100 | [diff] [blame] | 457 | cpumask_set_cpu(i, &armpmu->active_irqs); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 458 | } |
| 459 | |
Will Deacon | 0b390e2 | 2011-07-27 15:18:59 +0100 | [diff] [blame] | 460 | return 0; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 461 | } |
| 462 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 463 | static void |
| 464 | hw_perf_event_destroy(struct perf_event *event) |
| 465 | { |
Mark Rutland | 8a16b34 | 2011-04-28 16:27:54 +0100 | [diff] [blame] | 466 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
Mark Rutland | 03b7898 | 2011-04-27 11:20:11 +0100 | [diff] [blame] | 467 | atomic_t *active_events = &armpmu->active_events; |
| 468 | struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex; |
| 469 | |
| 470 | if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) { |
Mark Rutland | 8a16b34 | 2011-04-28 16:27:54 +0100 | [diff] [blame] | 471 | armpmu_release_hardware(armpmu); |
Mark Rutland | 03b7898 | 2011-04-27 11:20:11 +0100 | [diff] [blame] | 472 | mutex_unlock(pmu_reserve_mutex); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
| 476 | static int |
Will Deacon | 05d22fd | 2011-07-19 11:57:30 +0100 | [diff] [blame] | 477 | event_requires_mode_exclusion(struct perf_event_attr *attr) |
| 478 | { |
| 479 | return attr->exclude_idle || attr->exclude_user || |
| 480 | attr->exclude_kernel || attr->exclude_hv; |
| 481 | } |
| 482 | |
| 483 | static int |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 484 | __hw_perf_event_init(struct perf_event *event) |
| 485 | { |
Mark Rutland | 8a16b34 | 2011-04-28 16:27:54 +0100 | [diff] [blame] | 486 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 487 | struct hw_perf_event *hwc = &event->hw; |
| 488 | int mapping, err; |
| 489 | |
Mark Rutland | e1f431b | 2011-04-28 15:47:10 +0100 | [diff] [blame] | 490 | mapping = armpmu->map_event(event); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 491 | |
| 492 | if (mapping < 0) { |
| 493 | pr_debug("event %x:%llx not supported\n", event->attr.type, |
| 494 | event->attr.config); |
| 495 | return mapping; |
| 496 | } |
| 497 | |
| 498 | /* |
Will Deacon | 05d22fd | 2011-07-19 11:57:30 +0100 | [diff] [blame] | 499 | * We don't assign an index until we actually place the event onto |
| 500 | * hardware. Use -1 to signify that we haven't decided where to put it |
| 501 | * yet. For SMP systems, each core has it's own PMU so we can't do any |
| 502 | * clever allocation or constraints checking at this point. |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 503 | */ |
Will Deacon | 05d22fd | 2011-07-19 11:57:30 +0100 | [diff] [blame] | 504 | hwc->idx = -1; |
| 505 | hwc->config_base = 0; |
| 506 | hwc->config = 0; |
| 507 | hwc->event_base = 0; |
| 508 | |
| 509 | /* |
| 510 | * Check whether we need to exclude the counter from certain modes. |
| 511 | */ |
| 512 | if ((!armpmu->set_event_filter || |
| 513 | armpmu->set_event_filter(hwc, &event->attr)) && |
| 514 | event_requires_mode_exclusion(&event->attr)) { |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 515 | pr_debug("ARM performance counters do not support " |
| 516 | "mode exclusion\n"); |
| 517 | return -EPERM; |
| 518 | } |
| 519 | |
| 520 | /* |
Will Deacon | 05d22fd | 2011-07-19 11:57:30 +0100 | [diff] [blame] | 521 | * Store the event encoding into the config_base field. |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 522 | */ |
Will Deacon | 05d22fd | 2011-07-19 11:57:30 +0100 | [diff] [blame] | 523 | hwc->config_base |= (unsigned long)mapping; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 524 | |
| 525 | if (!hwc->sample_period) { |
Will Deacon | 5727347 | 2012-03-06 17:33:17 +0100 | [diff] [blame] | 526 | /* |
| 527 | * For non-sampling runs, limit the sample_period to half |
| 528 | * of the counter width. That way, the new counter value |
| 529 | * is far less likely to overtake the previous one unless |
| 530 | * you have some serious IRQ latency issues. |
| 531 | */ |
| 532 | hwc->sample_period = armpmu->max_period >> 1; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 533 | hwc->last_period = hwc->sample_period; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 534 | local64_set(&hwc->period_left, hwc->sample_period); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | err = 0; |
| 538 | if (event->group_leader != event) { |
| 539 | err = validate_group(event); |
| 540 | if (err) |
| 541 | return -EINVAL; |
| 542 | } |
| 543 | |
| 544 | return err; |
| 545 | } |
| 546 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 547 | static int armpmu_event_init(struct perf_event *event) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 548 | { |
Mark Rutland | 8a16b34 | 2011-04-28 16:27:54 +0100 | [diff] [blame] | 549 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 550 | int err = 0; |
Mark Rutland | 03b7898 | 2011-04-27 11:20:11 +0100 | [diff] [blame] | 551 | atomic_t *active_events = &armpmu->active_events; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 552 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 553 | /* does not support taken branch sampling */ |
| 554 | if (has_branch_stack(event)) |
| 555 | return -EOPNOTSUPP; |
| 556 | |
Mark Rutland | e1f431b | 2011-04-28 15:47:10 +0100 | [diff] [blame] | 557 | if (armpmu->map_event(event) == -ENOENT) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 558 | return -ENOENT; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 559 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 560 | event->destroy = hw_perf_event_destroy; |
| 561 | |
Mark Rutland | 03b7898 | 2011-04-27 11:20:11 +0100 | [diff] [blame] | 562 | if (!atomic_inc_not_zero(active_events)) { |
| 563 | mutex_lock(&armpmu->reserve_mutex); |
| 564 | if (atomic_read(active_events) == 0) |
Mark Rutland | 8a16b34 | 2011-04-28 16:27:54 +0100 | [diff] [blame] | 565 | err = armpmu_reserve_hardware(armpmu); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 566 | |
| 567 | if (!err) |
Mark Rutland | 03b7898 | 2011-04-27 11:20:11 +0100 | [diff] [blame] | 568 | atomic_inc(active_events); |
| 569 | mutex_unlock(&armpmu->reserve_mutex); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | if (err) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 573 | return err; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 574 | |
| 575 | err = __hw_perf_event_init(event); |
| 576 | if (err) |
| 577 | hw_perf_event_destroy(event); |
| 578 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 579 | return err; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 580 | } |
| 581 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 582 | static void armpmu_enable(struct pmu *pmu) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 583 | { |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 584 | struct arm_pmu *armpmu = to_arm_pmu(pmu); |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 585 | struct pmu_hw_events *hw_events = armpmu->get_hw_events(); |
Mark Rutland | 7325eae | 2011-08-23 11:59:49 +0100 | [diff] [blame] | 586 | int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 587 | |
Will Deacon | f4f3843 | 2011-07-01 14:38:12 +0100 | [diff] [blame] | 588 | if (enabled) |
| 589 | armpmu->start(); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 590 | } |
| 591 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 592 | static void armpmu_disable(struct pmu *pmu) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 593 | { |
Mark Rutland | 8a16b34 | 2011-04-28 16:27:54 +0100 | [diff] [blame] | 594 | struct arm_pmu *armpmu = to_arm_pmu(pmu); |
Mark Rutland | 4895715 | 2011-04-27 10:31:51 +0100 | [diff] [blame] | 595 | armpmu->stop(); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 596 | } |
| 597 | |
Ashwin Chaugule | 4a81cb8 | 2012-06-07 13:40:54 -0400 | [diff] [blame] | 598 | static void armpmu_init(struct arm_pmu *armpmu) |
Mark Rutland | 03b7898 | 2011-04-27 11:20:11 +0100 | [diff] [blame] | 599 | { |
| 600 | atomic_set(&armpmu->active_events, 0); |
| 601 | mutex_init(&armpmu->reserve_mutex); |
Mark Rutland | 8a16b34 | 2011-04-28 16:27:54 +0100 | [diff] [blame] | 602 | |
| 603 | armpmu->pmu = (struct pmu) { |
| 604 | .pmu_enable = armpmu_enable, |
| 605 | .pmu_disable = armpmu_disable, |
| 606 | .event_init = armpmu_event_init, |
| 607 | .add = armpmu_add, |
| 608 | .del = armpmu_del, |
| 609 | .start = armpmu_start, |
| 610 | .stop = armpmu_stop, |
| 611 | .read = armpmu_read, |
| 612 | }; |
| 613 | } |
| 614 | |
Ashwin Chaugule | 4a81cb8 | 2012-06-07 13:40:54 -0400 | [diff] [blame] | 615 | int armpmu_register(struct arm_pmu *armpmu, char *name, int type) |
Mark Rutland | 8a16b34 | 2011-04-28 16:27:54 +0100 | [diff] [blame] | 616 | { |
| 617 | armpmu_init(armpmu); |
| 618 | return perf_pmu_register(&armpmu->pmu, name, type); |
Mark Rutland | 03b7898 | 2011-04-27 11:20:11 +0100 | [diff] [blame] | 619 | } |
| 620 | |
Will Deacon | 43eab87 | 2010-11-13 19:04:32 +0000 | [diff] [blame] | 621 | /* Include the PMU-specific implementations. */ |
| 622 | #include "perf_event_xscale.c" |
| 623 | #include "perf_event_v6.c" |
| 624 | #include "perf_event_v7.c" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 625 | #include "perf_event_msm_krait.c" |
Ashwin Chaugule | 7cd836e | 2012-06-11 16:26:47 -0400 | [diff] [blame] | 626 | #include "perf_event_msm.c" |
Will Deacon | 49e6a32 | 2010-04-30 11:33:33 +0100 | [diff] [blame] | 627 | |
Will Deacon | 574b69c | 2011-03-25 13:13:34 +0100 | [diff] [blame] | 628 | /* |
| 629 | * Ensure the PMU has sane values out of reset. |
| 630 | * This requires SMP to be available, so exists as a separate initcall. |
| 631 | */ |
| 632 | static int __init |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 633 | cpu_pmu_reset(void) |
Will Deacon | 574b69c | 2011-03-25 13:13:34 +0100 | [diff] [blame] | 634 | { |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 635 | if (cpu_pmu && cpu_pmu->reset) |
| 636 | return on_each_cpu(cpu_pmu->reset, NULL, 1); |
Will Deacon | 574b69c | 2011-03-25 13:13:34 +0100 | [diff] [blame] | 637 | return 0; |
| 638 | } |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 639 | arch_initcall(cpu_pmu_reset); |
Will Deacon | 574b69c | 2011-03-25 13:13:34 +0100 | [diff] [blame] | 640 | |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 641 | /* |
| 642 | * PMU platform driver and devicetree bindings. |
| 643 | */ |
| 644 | static struct of_device_id armpmu_of_device_ids[] = { |
| 645 | {.compatible = "arm,cortex-a9-pmu"}, |
| 646 | {.compatible = "arm,cortex-a8-pmu"}, |
| 647 | {.compatible = "arm,arm1136-pmu"}, |
| 648 | {.compatible = "arm,arm1176-pmu"}, |
| 649 | {}, |
| 650 | }; |
| 651 | |
| 652 | static struct platform_device_id armpmu_plat_device_ids[] = { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 653 | {.name = "cpu-arm-pmu"}, |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 654 | {}, |
| 655 | }; |
| 656 | |
| 657 | static int __devinit armpmu_device_probe(struct platform_device *pdev) |
| 658 | { |
Will Deacon | 6bd0540 | 2011-12-02 18:16:01 +0100 | [diff] [blame] | 659 | if (!cpu_pmu) |
| 660 | return -ENODEV; |
| 661 | |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 662 | cpu_pmu->plat_device = pdev; |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | static struct platform_driver armpmu_driver = { |
| 667 | .driver = { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 668 | .name = "cpu-arm-pmu", |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 669 | .of_match_table = armpmu_of_device_ids, |
| 670 | }, |
| 671 | .probe = armpmu_device_probe, |
| 672 | .id_table = armpmu_plat_device_ids, |
| 673 | }; |
| 674 | |
| 675 | static int __init register_pmu_driver(void) |
| 676 | { |
| 677 | return platform_driver_register(&armpmu_driver); |
| 678 | } |
| 679 | device_initcall(register_pmu_driver); |
| 680 | |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 681 | static struct pmu_hw_events *armpmu_get_cpu_events(void) |
Mark Rutland | 92f701e | 2011-05-04 09:23:51 +0100 | [diff] [blame] | 682 | { |
| 683 | return &__get_cpu_var(cpu_hw_events); |
| 684 | } |
| 685 | |
| 686 | static void __init cpu_pmu_init(struct arm_pmu *armpmu) |
| 687 | { |
Mark Rutland | 0f78d2d | 2011-04-28 10:17:04 +0100 | [diff] [blame] | 688 | int cpu; |
| 689 | for_each_possible_cpu(cpu) { |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 690 | struct pmu_hw_events *events = &per_cpu(cpu_hw_events, cpu); |
Mark Rutland | 3fc2c83 | 2011-06-24 11:30:59 +0100 | [diff] [blame] | 691 | events->events = per_cpu(hw_events, cpu); |
| 692 | events->used_mask = per_cpu(used_mask, cpu); |
Mark Rutland | 0f78d2d | 2011-04-28 10:17:04 +0100 | [diff] [blame] | 693 | raw_spin_lock_init(&events->pmu_lock); |
| 694 | } |
Mark Rutland | 92f701e | 2011-05-04 09:23:51 +0100 | [diff] [blame] | 695 | armpmu->get_hw_events = armpmu_get_cpu_events; |
Mark Rutland | 7ae18a5 | 2011-06-06 10:37:50 +0100 | [diff] [blame] | 696 | armpmu->type = ARM_PMU_DEVICE_CPU; |
Mark Rutland | 92f701e | 2011-05-04 09:23:51 +0100 | [diff] [blame] | 697 | } |
| 698 | |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 699 | /* |
Lorenzo Pieralisi | a0feb6d | 2012-03-06 17:37:45 +0100 | [diff] [blame] | 700 | * PMU hardware loses all context when a CPU goes offline. |
| 701 | * When a CPU is hotplugged back in, since some hardware registers are |
| 702 | * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading |
| 703 | * junk values out of them. |
| 704 | */ |
| 705 | static int __cpuinit pmu_cpu_notify(struct notifier_block *b, |
| 706 | unsigned long action, void *hcpu) |
| 707 | { |
| 708 | if ((action & ~CPU_TASKS_FROZEN) != CPU_STARTING) |
| 709 | return NOTIFY_DONE; |
| 710 | |
| 711 | if (cpu_pmu && cpu_pmu->reset) |
| 712 | cpu_pmu->reset(NULL); |
| 713 | |
| 714 | return NOTIFY_OK; |
| 715 | } |
| 716 | |
Ashwin Chaugule | f53fe44 | 2012-06-07 13:41:37 -0400 | [diff] [blame] | 717 | static void armpmu_update_counters(void) |
| 718 | { |
| 719 | struct pmu_hw_events *hw_events; |
| 720 | int idx; |
| 721 | |
| 722 | if (!cpu_pmu) |
| 723 | return; |
| 724 | |
| 725 | hw_events = cpu_pmu->get_hw_events(); |
| 726 | |
| 727 | for (idx = 0; idx <= cpu_pmu->num_events; ++idx) { |
| 728 | struct perf_event *event = hw_events->events[idx]; |
| 729 | |
| 730 | if (!event) |
| 731 | continue; |
| 732 | |
| 733 | armpmu_read(event); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | static int cpu_has_active_perf(void) |
| 738 | { |
| 739 | struct pmu_hw_events *hw_events; |
| 740 | int enabled; |
| 741 | |
| 742 | if (!cpu_pmu) |
| 743 | return 0; |
| 744 | |
| 745 | hw_events = cpu_pmu->get_hw_events(); |
| 746 | enabled = bitmap_weight(hw_events->used_mask, cpu_pmu->num_events); |
| 747 | |
| 748 | if (enabled) |
| 749 | /*Even one event's existence is good enough.*/ |
| 750 | return 1; |
| 751 | |
| 752 | return 0; |
| 753 | } |
| 754 | |
Lorenzo Pieralisi | a0feb6d | 2012-03-06 17:37:45 +0100 | [diff] [blame] | 755 | static struct notifier_block __cpuinitdata pmu_cpu_notifier = { |
| 756 | .notifier_call = pmu_cpu_notify, |
| 757 | }; |
| 758 | |
Ashwin Chaugule | f53fe44 | 2012-06-07 13:41:37 -0400 | [diff] [blame] | 759 | /*TODO: Unify with pending patch from ARM */ |
| 760 | static int perf_cpu_pm_notifier(struct notifier_block *self, unsigned long cmd, |
| 761 | void *v) |
| 762 | { |
| 763 | switch (cmd) { |
| 764 | case CPU_PM_ENTER: |
| 765 | if (cpu_has_active_perf()) { |
| 766 | armpmu_update_counters(); |
| 767 | perf_pmu_disable(&cpu_pmu->pmu); |
| 768 | } |
| 769 | break; |
| 770 | |
| 771 | case CPU_PM_ENTER_FAILED: |
| 772 | case CPU_PM_EXIT: |
| 773 | if (cpu_has_active_perf() && cpu_pmu->reset) { |
| 774 | cpu_pmu->reset(NULL); |
| 775 | perf_pmu_enable(&cpu_pmu->pmu); |
| 776 | } |
| 777 | break; |
| 778 | } |
| 779 | |
| 780 | return NOTIFY_OK; |
| 781 | } |
| 782 | |
| 783 | static struct notifier_block perf_cpu_pm_notifier_block = { |
| 784 | .notifier_call = perf_cpu_pm_notifier, |
| 785 | }; |
| 786 | |
Lorenzo Pieralisi | a0feb6d | 2012-03-06 17:37:45 +0100 | [diff] [blame] | 787 | /* |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 788 | * CPU PMU identification and registration. |
| 789 | */ |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 790 | static int __init |
| 791 | init_hw_perf_events(void) |
| 792 | { |
| 793 | unsigned long cpuid = read_cpuid_id(); |
| 794 | unsigned long implementor = (cpuid & 0xFF000000) >> 24; |
| 795 | unsigned long part_number = (cpuid & 0xFFF0); |
| 796 | |
Will Deacon | 49e6a32 | 2010-04-30 11:33:33 +0100 | [diff] [blame] | 797 | /* ARM Ltd CPUs. */ |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 798 | if (0x41 == implementor) { |
| 799 | switch (part_number) { |
| 800 | case 0xB360: /* ARM1136 */ |
| 801 | case 0xB560: /* ARM1156 */ |
| 802 | case 0xB760: /* ARM1176 */ |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 803 | cpu_pmu = armv6pmu_init(); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 804 | break; |
| 805 | case 0xB020: /* ARM11mpcore */ |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 806 | cpu_pmu = armv6mpcore_pmu_init(); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 807 | break; |
Jean PIHET | 796d129 | 2010-01-26 18:51:05 +0100 | [diff] [blame] | 808 | case 0xC080: /* Cortex-A8 */ |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 809 | cpu_pmu = armv7_a8_pmu_init(); |
Jean PIHET | 796d129 | 2010-01-26 18:51:05 +0100 | [diff] [blame] | 810 | break; |
| 811 | case 0xC090: /* Cortex-A9 */ |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 812 | cpu_pmu = armv7_a9_pmu_init(); |
Jean PIHET | 796d129 | 2010-01-26 18:51:05 +0100 | [diff] [blame] | 813 | break; |
Will Deacon | 0c205cb | 2011-06-03 17:40:15 +0100 | [diff] [blame] | 814 | case 0xC050: /* Cortex-A5 */ |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 815 | cpu_pmu = armv7_a5_pmu_init(); |
Will Deacon | 0c205cb | 2011-06-03 17:40:15 +0100 | [diff] [blame] | 816 | break; |
Will Deacon | 14abd03 | 2011-01-19 14:24:38 +0000 | [diff] [blame] | 817 | case 0xC0F0: /* Cortex-A15 */ |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 818 | cpu_pmu = armv7_a15_pmu_init(); |
Will Deacon | 14abd03 | 2011-01-19 14:24:38 +0000 | [diff] [blame] | 819 | break; |
Will Deacon | d33c88c | 2012-02-03 14:46:01 +0100 | [diff] [blame] | 820 | case 0xC070: /* Cortex-A7 */ |
| 821 | cpu_pmu = armv7_a7_pmu_init(); |
| 822 | break; |
Will Deacon | 49e6a32 | 2010-04-30 11:33:33 +0100 | [diff] [blame] | 823 | } |
| 824 | /* Intel CPUs [xscale]. */ |
| 825 | } else if (0x69 == implementor) { |
| 826 | part_number = (cpuid >> 13) & 0x7; |
| 827 | switch (part_number) { |
| 828 | case 1: |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 829 | cpu_pmu = xscale1pmu_init(); |
Will Deacon | 49e6a32 | 2010-04-30 11:33:33 +0100 | [diff] [blame] | 830 | break; |
| 831 | case 2: |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 832 | cpu_pmu = xscale2pmu_init(); |
Will Deacon | 49e6a32 | 2010-04-30 11:33:33 +0100 | [diff] [blame] | 833 | break; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 834 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 835 | /* Qualcomm CPUs */ |
| 836 | } else if (0x51 == implementor) { |
| 837 | switch (part_number) { |
| 838 | case 0x00F0: /* 8x50 & 7x30*/ |
Ashwin Chaugule | 7cd836e | 2012-06-11 16:26:47 -0400 | [diff] [blame] | 839 | cpu_pmu = armv7_scorpion_pmu_init(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 840 | break; |
| 841 | case 0x02D0: /* 8x60 */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 842 | // fabricmon_pmu_init(); |
Ashwin Chaugule | 7cd836e | 2012-06-11 16:26:47 -0400 | [diff] [blame] | 843 | cpu_pmu = armv7_scorpionmp_pmu_init(); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 844 | // scorpionmp_l2_pmu_init(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 845 | break; |
| 846 | case 0x0490: /* 8960 sim */ |
| 847 | case 0x04D0: /* 8960 */ |
Neil Leeder | ed41511 | 2012-02-09 13:34:09 -0500 | [diff] [blame] | 848 | case 0x06F0: /* 8064 */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 849 | // fabricmon_pmu_init(); |
| 850 | cpu_pmu = armv7_krait_pmu_init(); |
| 851 | // krait_l2_pmu_init(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 852 | break; |
| 853 | } |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 854 | } |
| 855 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 856 | |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 857 | if (cpu_pmu) { |
Jean PIHET | 796d129 | 2010-01-26 18:51:05 +0100 | [diff] [blame] | 858 | pr_info("enabled with %s PMU driver, %d counters available\n", |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 859 | cpu_pmu->name, cpu_pmu->num_events); |
| 860 | cpu_pmu_init(cpu_pmu); |
Lorenzo Pieralisi | a0feb6d | 2012-03-06 17:37:45 +0100 | [diff] [blame] | 861 | register_cpu_notifier(&pmu_cpu_notifier); |
Mark Rutland | 8be3f9a | 2011-05-17 11:20:11 +0100 | [diff] [blame] | 862 | armpmu_register(cpu_pmu, "cpu", PERF_TYPE_RAW); |
Ashwin Chaugule | f53fe44 | 2012-06-07 13:41:37 -0400 | [diff] [blame] | 863 | cpu_pm_register_notifier(&perf_cpu_pm_notifier_block); |
Will Deacon | 49e6a32 | 2010-04-30 11:33:33 +0100 | [diff] [blame] | 864 | } else { |
| 865 | pr_info("no hardware support available\n"); |
Will Deacon | 49e6a32 | 2010-04-30 11:33:33 +0100 | [diff] [blame] | 866 | } |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 867 | |
| 868 | return 0; |
| 869 | } |
Peter Zijlstra | 004417a | 2010-11-25 18:38:29 +0100 | [diff] [blame] | 870 | early_initcall(init_hw_perf_events); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 871 | |
| 872 | /* |
| 873 | * Callchain handling code. |
| 874 | */ |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 875 | |
| 876 | /* |
| 877 | * The registers we're interested in are at the end of the variable |
| 878 | * length saved register structure. The fp points at the end of this |
| 879 | * structure so the address of this struct is: |
| 880 | * (struct frame_tail *)(xxx->fp)-1 |
| 881 | * |
| 882 | * This code has been adapted from the ARM OProfile support. |
| 883 | */ |
| 884 | struct frame_tail { |
Will Deacon | 4d6b7a7 | 2010-11-30 18:15:53 +0100 | [diff] [blame] | 885 | struct frame_tail __user *fp; |
| 886 | unsigned long sp; |
| 887 | unsigned long lr; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 888 | } __attribute__((packed)); |
| 889 | |
| 890 | /* |
| 891 | * Get the return address for a single stackframe and return a pointer to the |
| 892 | * next frame tail. |
| 893 | */ |
Will Deacon | 4d6b7a7 | 2010-11-30 18:15:53 +0100 | [diff] [blame] | 894 | static struct frame_tail __user * |
| 895 | user_backtrace(struct frame_tail __user *tail, |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 896 | struct perf_callchain_entry *entry) |
| 897 | { |
| 898 | struct frame_tail buftail; |
| 899 | |
| 900 | /* Also check accessibility of one struct frame_tail beyond */ |
| 901 | if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) |
| 902 | return NULL; |
| 903 | if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail))) |
| 904 | return NULL; |
| 905 | |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 906 | perf_callchain_store(entry, buftail.lr); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 907 | |
| 908 | /* |
| 909 | * Frame pointers should strictly progress back up the stack |
| 910 | * (towards higher addresses). |
| 911 | */ |
Rabin Vincent | cb06199 | 2011-02-09 11:35:12 +0100 | [diff] [blame] | 912 | if (tail + 1 >= buftail.fp) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 913 | return NULL; |
| 914 | |
| 915 | return buftail.fp - 1; |
| 916 | } |
| 917 | |
Frederic Weisbecker | 56962b4 | 2010-06-30 23:03:51 +0200 | [diff] [blame] | 918 | void |
| 919 | perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 920 | { |
Will Deacon | 4d6b7a7 | 2010-11-30 18:15:53 +0100 | [diff] [blame] | 921 | struct frame_tail __user *tail; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 922 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 923 | |
Will Deacon | 4d6b7a7 | 2010-11-30 18:15:53 +0100 | [diff] [blame] | 924 | tail = (struct frame_tail __user *)regs->ARM_fp - 1; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 925 | |
Sonny Rao | 860ad78 | 2011-04-18 22:12:59 +0100 | [diff] [blame] | 926 | while ((entry->nr < PERF_MAX_STACK_DEPTH) && |
| 927 | tail && !((unsigned long)tail & 0x3)) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 928 | tail = user_backtrace(tail, entry); |
| 929 | } |
| 930 | |
| 931 | /* |
| 932 | * Gets called by walk_stackframe() for every stackframe. This will be called |
| 933 | * whist unwinding the stackframe and is like a subroutine return so we use |
| 934 | * the PC. |
| 935 | */ |
| 936 | static int |
| 937 | callchain_trace(struct stackframe *fr, |
| 938 | void *data) |
| 939 | { |
| 940 | struct perf_callchain_entry *entry = data; |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 941 | perf_callchain_store(entry, fr->pc); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 942 | return 0; |
| 943 | } |
| 944 | |
Frederic Weisbecker | 56962b4 | 2010-06-30 23:03:51 +0200 | [diff] [blame] | 945 | void |
| 946 | perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 947 | { |
| 948 | struct stackframe fr; |
| 949 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 950 | fr.fp = regs->ARM_fp; |
| 951 | fr.sp = regs->ARM_sp; |
| 952 | fr.lr = regs->ARM_lr; |
| 953 | fr.pc = regs->ARM_pc; |
| 954 | walk_stackframe(&fr, callchain_trace, entry); |
| 955 | } |