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 | |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/kernel.h> |
Will Deacon | 181193f | 2010-04-30 11:32:44 +0100 | [diff] [blame] | 17 | #include <linux/module.h> |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 18 | #include <linux/perf_event.h> |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 19 | #include <linux/platform_device.h> |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 20 | #include <linux/spinlock.h> |
| 21 | #include <linux/uaccess.h> |
| 22 | |
| 23 | #include <asm/cputype.h> |
| 24 | #include <asm/irq.h> |
| 25 | #include <asm/irq_regs.h> |
| 26 | #include <asm/pmu.h> |
| 27 | #include <asm/stacktrace.h> |
| 28 | |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 29 | static struct platform_device *pmu_device; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * Hardware lock to serialize accesses to PMU registers. Needed for the |
| 33 | * read/modify/write sequences. |
| 34 | */ |
Will Deacon | 961ec6da | 2010-12-02 18:01:49 +0100 | [diff] [blame] | 35 | static DEFINE_RAW_SPINLOCK(pmu_lock); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 36 | |
| 37 | /* |
Will Deacon | ecf5a89 | 2011-07-19 22:43:28 +0100 | [diff] [blame^] | 38 | * 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] | 39 | * another platform that supports more, we need to increase this to be the |
| 40 | * largest of all platforms. |
Jean PIHET | 796d129 | 2010-01-26 18:51:05 +0100 | [diff] [blame] | 41 | * |
| 42 | * ARMv7 supports up to 32 events: |
| 43 | * cycle counter CCNT + 31 events counters CNT0..30. |
| 44 | * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters. |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 45 | */ |
Will Deacon | ecf5a89 | 2011-07-19 22:43:28 +0100 | [diff] [blame^] | 46 | #define ARMPMU_MAX_HWEVENTS 32 |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 47 | |
| 48 | /* The events for a given CPU. */ |
| 49 | struct cpu_hw_events { |
| 50 | /* |
Will Deacon | ecf5a89 | 2011-07-19 22:43:28 +0100 | [diff] [blame^] | 51 | * The events that are active on the CPU for the given index. |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 52 | */ |
| 53 | struct perf_event *events[ARMPMU_MAX_HWEVENTS]; |
| 54 | |
| 55 | /* |
| 56 | * A 1 bit for an index indicates that the counter is being used for |
| 57 | * an event. A 0 means that the counter can be used. |
| 58 | */ |
| 59 | unsigned long used_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)]; |
| 60 | |
| 61 | /* |
| 62 | * A 1 bit for an index indicates that the counter is actively being |
| 63 | * used. |
| 64 | */ |
| 65 | unsigned long active_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)]; |
| 66 | }; |
Will Deacon | 4d6b7a7 | 2010-11-30 18:15:53 +0100 | [diff] [blame] | 67 | static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events); |
Will Deacon | 181193f | 2010-04-30 11:32:44 +0100 | [diff] [blame] | 68 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 69 | struct arm_pmu { |
Will Deacon | 181193f | 2010-04-30 11:32:44 +0100 | [diff] [blame] | 70 | enum arm_perf_pmu_ids id; |
Will Deacon | 0b390e2 | 2011-07-27 15:18:59 +0100 | [diff] [blame] | 71 | cpumask_t active_irqs; |
Will Deacon | 6299483 | 2010-11-13 18:45:27 +0000 | [diff] [blame] | 72 | const char *name; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 73 | irqreturn_t (*handle_irq)(int irq_num, void *dev); |
| 74 | void (*enable)(struct hw_perf_event *evt, int idx); |
| 75 | void (*disable)(struct hw_perf_event *evt, int idx); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 76 | int (*get_event_idx)(struct cpu_hw_events *cpuc, |
| 77 | struct hw_perf_event *hwc); |
| 78 | u32 (*read_counter)(int idx); |
| 79 | void (*write_counter)(int idx, u32 val); |
| 80 | void (*start)(void); |
| 81 | void (*stop)(void); |
Will Deacon | 574b69c | 2011-03-25 13:13:34 +0100 | [diff] [blame] | 82 | void (*reset)(void *); |
Will Deacon | 84fee97 | 2010-11-13 17:13:56 +0000 | [diff] [blame] | 83 | const unsigned (*cache_map)[PERF_COUNT_HW_CACHE_MAX] |
| 84 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 85 | [PERF_COUNT_HW_CACHE_RESULT_MAX]; |
| 86 | const unsigned (*event_map)[PERF_COUNT_HW_MAX]; |
| 87 | u32 raw_event_mask; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 88 | int num_events; |
| 89 | u64 max_period; |
| 90 | }; |
| 91 | |
| 92 | /* Set at runtime when we know what CPU type we are. */ |
Mark Rutland | a6c93af | 2011-04-15 11:14:38 +0100 | [diff] [blame] | 93 | static struct arm_pmu *armpmu; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 94 | |
Will Deacon | 181193f | 2010-04-30 11:32:44 +0100 | [diff] [blame] | 95 | enum arm_perf_pmu_ids |
| 96 | armpmu_get_pmu_id(void) |
| 97 | { |
| 98 | int id = -ENODEV; |
| 99 | |
| 100 | if (armpmu != NULL) |
| 101 | id = armpmu->id; |
| 102 | |
| 103 | return id; |
| 104 | } |
| 105 | EXPORT_SYMBOL_GPL(armpmu_get_pmu_id); |
| 106 | |
Will Deacon | 929f519 | 2010-04-30 11:34:26 +0100 | [diff] [blame] | 107 | int |
| 108 | armpmu_get_max_events(void) |
| 109 | { |
| 110 | int max_events = 0; |
| 111 | |
| 112 | if (armpmu != NULL) |
| 113 | max_events = armpmu->num_events; |
| 114 | |
| 115 | return max_events; |
| 116 | } |
| 117 | EXPORT_SYMBOL_GPL(armpmu_get_max_events); |
| 118 | |
Matt Fleming | 3bf101b | 2010-09-27 20:22:24 +0100 | [diff] [blame] | 119 | int perf_num_counters(void) |
| 120 | { |
| 121 | return armpmu_get_max_events(); |
| 122 | } |
| 123 | EXPORT_SYMBOL_GPL(perf_num_counters); |
| 124 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 125 | #define HW_OP_UNSUPPORTED 0xFFFF |
| 126 | |
| 127 | #define C(_x) \ |
| 128 | PERF_COUNT_HW_CACHE_##_x |
| 129 | |
| 130 | #define CACHE_OP_UNSUPPORTED 0xFFFF |
| 131 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 132 | static int |
| 133 | armpmu_map_cache_event(u64 config) |
| 134 | { |
| 135 | unsigned int cache_type, cache_op, cache_result, ret; |
| 136 | |
| 137 | cache_type = (config >> 0) & 0xff; |
| 138 | if (cache_type >= PERF_COUNT_HW_CACHE_MAX) |
| 139 | return -EINVAL; |
| 140 | |
| 141 | cache_op = (config >> 8) & 0xff; |
| 142 | if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX) |
| 143 | return -EINVAL; |
| 144 | |
| 145 | cache_result = (config >> 16) & 0xff; |
| 146 | if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX) |
| 147 | return -EINVAL; |
| 148 | |
Will Deacon | 84fee97 | 2010-11-13 17:13:56 +0000 | [diff] [blame] | 149 | ret = (int)(*armpmu->cache_map)[cache_type][cache_op][cache_result]; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 150 | |
| 151 | if (ret == CACHE_OP_UNSUPPORTED) |
| 152 | return -ENOENT; |
| 153 | |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | static int |
Will Deacon | 84fee97 | 2010-11-13 17:13:56 +0000 | [diff] [blame] | 158 | armpmu_map_event(u64 config) |
| 159 | { |
| 160 | int mapping = (*armpmu->event_map)[config]; |
| 161 | return mapping == HW_OP_UNSUPPORTED ? -EOPNOTSUPP : mapping; |
| 162 | } |
| 163 | |
| 164 | static int |
| 165 | armpmu_map_raw_event(u64 config) |
| 166 | { |
| 167 | return (int)(config & armpmu->raw_event_mask); |
| 168 | } |
| 169 | |
| 170 | static int |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 171 | armpmu_event_set_period(struct perf_event *event, |
| 172 | struct hw_perf_event *hwc, |
| 173 | int idx) |
| 174 | { |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 175 | s64 left = local64_read(&hwc->period_left); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 176 | s64 period = hwc->sample_period; |
| 177 | int ret = 0; |
| 178 | |
| 179 | if (unlikely(left <= -period)) { |
| 180 | left = period; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 181 | local64_set(&hwc->period_left, left); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 182 | hwc->last_period = period; |
| 183 | ret = 1; |
| 184 | } |
| 185 | |
| 186 | if (unlikely(left <= 0)) { |
| 187 | left += period; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 188 | local64_set(&hwc->period_left, left); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 189 | hwc->last_period = period; |
| 190 | ret = 1; |
| 191 | } |
| 192 | |
| 193 | if (left > (s64)armpmu->max_period) |
| 194 | left = armpmu->max_period; |
| 195 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 196 | local64_set(&hwc->prev_count, (u64)-left); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 197 | |
| 198 | armpmu->write_counter(idx, (u64)(-left) & 0xffffffff); |
| 199 | |
| 200 | perf_event_update_userpage(event); |
| 201 | |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | static u64 |
| 206 | armpmu_event_update(struct perf_event *event, |
| 207 | struct hw_perf_event *hwc, |
Will Deacon | a737823 | 2011-03-25 17:12:37 +0100 | [diff] [blame] | 208 | int idx, int overflow) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 209 | { |
Will Deacon | a737823 | 2011-03-25 17:12:37 +0100 | [diff] [blame] | 210 | u64 delta, prev_raw_count, new_raw_count; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 211 | |
| 212 | again: |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 213 | prev_raw_count = local64_read(&hwc->prev_count); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 214 | new_raw_count = armpmu->read_counter(idx); |
| 215 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 216 | if (local64_cmpxchg(&hwc->prev_count, prev_raw_count, |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 217 | new_raw_count) != prev_raw_count) |
| 218 | goto again; |
| 219 | |
Will Deacon | a737823 | 2011-03-25 17:12:37 +0100 | [diff] [blame] | 220 | new_raw_count &= armpmu->max_period; |
| 221 | prev_raw_count &= armpmu->max_period; |
| 222 | |
| 223 | if (overflow) |
Will Deacon | 6759788 | 2011-04-05 14:01:24 +0100 | [diff] [blame] | 224 | delta = armpmu->max_period - prev_raw_count + new_raw_count + 1; |
Will Deacon | a737823 | 2011-03-25 17:12:37 +0100 | [diff] [blame] | 225 | else |
| 226 | delta = new_raw_count - prev_raw_count; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 227 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 228 | local64_add(delta, &event->count); |
| 229 | local64_sub(delta, &hwc->period_left); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 230 | |
| 231 | return new_raw_count; |
| 232 | } |
| 233 | |
| 234 | static void |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 235 | armpmu_read(struct perf_event *event) |
| 236 | { |
| 237 | struct hw_perf_event *hwc = &event->hw; |
| 238 | |
| 239 | /* Don't read disabled counters! */ |
| 240 | if (hwc->idx < 0) |
| 241 | return; |
| 242 | |
Will Deacon | a737823 | 2011-03-25 17:12:37 +0100 | [diff] [blame] | 243 | armpmu_event_update(event, hwc, hwc->idx, 0); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static void |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 247 | armpmu_stop(struct perf_event *event, int flags) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 248 | { |
| 249 | struct hw_perf_event *hwc = &event->hw; |
| 250 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 251 | if (!armpmu) |
| 252 | return; |
| 253 | |
| 254 | /* |
| 255 | * ARM pmu always has to update the counter, so ignore |
| 256 | * PERF_EF_UPDATE, see comments in armpmu_start(). |
| 257 | */ |
| 258 | if (!(hwc->state & PERF_HES_STOPPED)) { |
| 259 | armpmu->disable(hwc, hwc->idx); |
| 260 | barrier(); /* why? */ |
Will Deacon | a737823 | 2011-03-25 17:12:37 +0100 | [diff] [blame] | 261 | armpmu_event_update(event, hwc, hwc->idx, 0); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 262 | hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | static void |
| 267 | armpmu_start(struct perf_event *event, int flags) |
| 268 | { |
| 269 | struct hw_perf_event *hwc = &event->hw; |
| 270 | |
| 271 | if (!armpmu) |
| 272 | return; |
| 273 | |
| 274 | /* |
| 275 | * ARM pmu always has to reprogram the period, so ignore |
| 276 | * PERF_EF_RELOAD, see the comment below. |
| 277 | */ |
| 278 | if (flags & PERF_EF_RELOAD) |
| 279 | WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE)); |
| 280 | |
| 281 | hwc->state = 0; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 282 | /* |
| 283 | * 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] | 284 | * were stopped we simply disabled the IRQ source and the counter |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 285 | * may have been left counting. If we don't do this step then we may |
| 286 | * get an interrupt too soon or *way* too late if the overflow has |
| 287 | * happened since disabling. |
| 288 | */ |
| 289 | armpmu_event_set_period(event, hwc, hwc->idx); |
| 290 | armpmu->enable(hwc, hwc->idx); |
| 291 | } |
| 292 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 293 | static void |
| 294 | armpmu_del(struct perf_event *event, int flags) |
| 295 | { |
| 296 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 297 | struct hw_perf_event *hwc = &event->hw; |
| 298 | int idx = hwc->idx; |
| 299 | |
| 300 | WARN_ON(idx < 0); |
| 301 | |
| 302 | clear_bit(idx, cpuc->active_mask); |
| 303 | armpmu_stop(event, PERF_EF_UPDATE); |
| 304 | cpuc->events[idx] = NULL; |
| 305 | clear_bit(idx, cpuc->used_mask); |
| 306 | |
| 307 | perf_event_update_userpage(event); |
| 308 | } |
| 309 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 310 | static int |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 311 | armpmu_add(struct perf_event *event, int flags) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 312 | { |
| 313 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 314 | struct hw_perf_event *hwc = &event->hw; |
| 315 | int idx; |
| 316 | int err = 0; |
| 317 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 318 | perf_pmu_disable(event->pmu); |
Peter Zijlstra | 24cd7f5 | 2010-06-11 17:32:03 +0200 | [diff] [blame] | 319 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 320 | /* If we don't have a space for the counter then finish early. */ |
| 321 | idx = armpmu->get_event_idx(cpuc, hwc); |
| 322 | if (idx < 0) { |
| 323 | err = idx; |
| 324 | goto out; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * If there is an event in the counter we are going to use then make |
| 329 | * sure it is disabled. |
| 330 | */ |
| 331 | event->hw.idx = idx; |
| 332 | armpmu->disable(hwc, idx); |
| 333 | cpuc->events[idx] = event; |
| 334 | set_bit(idx, cpuc->active_mask); |
| 335 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 336 | hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE; |
| 337 | if (flags & PERF_EF_START) |
| 338 | armpmu_start(event, PERF_EF_RELOAD); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 339 | |
| 340 | /* Propagate our changes to the userspace mapping. */ |
| 341 | perf_event_update_userpage(event); |
| 342 | |
| 343 | out: |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 344 | perf_pmu_enable(event->pmu); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 345 | return err; |
| 346 | } |
| 347 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 348 | static struct pmu pmu; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 349 | |
| 350 | static int |
| 351 | validate_event(struct cpu_hw_events *cpuc, |
| 352 | struct perf_event *event) |
| 353 | { |
| 354 | struct hw_perf_event fake_event = event->hw; |
| 355 | |
Will Deacon | 65b4711 | 2010-09-02 09:32:08 +0100 | [diff] [blame] | 356 | if (event->pmu != &pmu || event->state <= PERF_EVENT_STATE_OFF) |
| 357 | return 1; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 358 | |
| 359 | return armpmu->get_event_idx(cpuc, &fake_event) >= 0; |
| 360 | } |
| 361 | |
| 362 | static int |
| 363 | validate_group(struct perf_event *event) |
| 364 | { |
| 365 | struct perf_event *sibling, *leader = event->group_leader; |
| 366 | struct cpu_hw_events fake_pmu; |
| 367 | |
| 368 | memset(&fake_pmu, 0, sizeof(fake_pmu)); |
| 369 | |
| 370 | if (!validate_event(&fake_pmu, leader)) |
| 371 | return -ENOSPC; |
| 372 | |
| 373 | list_for_each_entry(sibling, &leader->sibling_list, group_entry) { |
| 374 | if (!validate_event(&fake_pmu, sibling)) |
| 375 | return -ENOSPC; |
| 376 | } |
| 377 | |
| 378 | if (!validate_event(&fake_pmu, event)) |
| 379 | return -ENOSPC; |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
Rabin Vincent | 0e25a5c | 2011-02-08 09:24:36 +0530 | [diff] [blame] | 384 | static irqreturn_t armpmu_platform_irq(int irq, void *dev) |
| 385 | { |
| 386 | struct arm_pmu_platdata *plat = dev_get_platdata(&pmu_device->dev); |
| 387 | |
| 388 | return plat->handle_irq(irq, dev, armpmu->handle_irq); |
| 389 | } |
| 390 | |
Will Deacon | 0b390e2 | 2011-07-27 15:18:59 +0100 | [diff] [blame] | 391 | static void |
| 392 | armpmu_release_hardware(void) |
| 393 | { |
| 394 | int i, irq, irqs; |
| 395 | |
| 396 | irqs = min(pmu_device->num_resources, num_possible_cpus()); |
| 397 | |
| 398 | for (i = 0; i < irqs; ++i) { |
| 399 | if (!cpumask_test_and_clear_cpu(i, &armpmu->active_irqs)) |
| 400 | continue; |
| 401 | irq = platform_get_irq(pmu_device, i); |
| 402 | if (irq >= 0) |
| 403 | free_irq(irq, NULL); |
| 404 | } |
| 405 | |
| 406 | armpmu->stop(); |
| 407 | release_pmu(ARM_PMU_DEVICE_CPU); |
| 408 | } |
| 409 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 410 | static int |
| 411 | armpmu_reserve_hardware(void) |
| 412 | { |
Rabin Vincent | 0e25a5c | 2011-02-08 09:24:36 +0530 | [diff] [blame] | 413 | struct arm_pmu_platdata *plat; |
| 414 | irq_handler_t handle_irq; |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 415 | int i, err, irq, irqs; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 416 | |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 417 | err = reserve_pmu(ARM_PMU_DEVICE_CPU); |
| 418 | if (err) { |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 419 | pr_warning("unable to reserve pmu\n"); |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 420 | return err; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 421 | } |
| 422 | |
Rabin Vincent | 0e25a5c | 2011-02-08 09:24:36 +0530 | [diff] [blame] | 423 | plat = dev_get_platdata(&pmu_device->dev); |
| 424 | if (plat && plat->handle_irq) |
| 425 | handle_irq = armpmu_platform_irq; |
| 426 | else |
| 427 | handle_irq = armpmu->handle_irq; |
| 428 | |
Will Deacon | 0b390e2 | 2011-07-27 15:18:59 +0100 | [diff] [blame] | 429 | irqs = min(pmu_device->num_resources, num_possible_cpus()); |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 430 | if (irqs < 1) { |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 431 | pr_err("no irqs for PMUs defined\n"); |
| 432 | return -ENODEV; |
| 433 | } |
| 434 | |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 435 | for (i = 0; i < irqs; ++i) { |
Will Deacon | 0b390e2 | 2011-07-27 15:18:59 +0100 | [diff] [blame] | 436 | err = 0; |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 437 | irq = platform_get_irq(pmu_device, i); |
| 438 | if (irq < 0) |
| 439 | continue; |
| 440 | |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 441 | /* |
| 442 | * If we have a single PMU interrupt that we can't shift, |
| 443 | * assume that we're running on a uniprocessor machine and |
Will Deacon | 0b390e2 | 2011-07-27 15:18:59 +0100 | [diff] [blame] | 444 | * continue. Otherwise, continue without this interrupt. |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 445 | */ |
Will Deacon | 0b390e2 | 2011-07-27 15:18:59 +0100 | [diff] [blame] | 446 | if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) { |
| 447 | pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n", |
| 448 | irq, i); |
| 449 | continue; |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 450 | } |
| 451 | |
Rabin Vincent | 0e25a5c | 2011-02-08 09:24:36 +0530 | [diff] [blame] | 452 | err = request_irq(irq, handle_irq, |
Will Deacon | ddee87f | 2010-02-25 15:04:14 +0100 | [diff] [blame] | 453 | IRQF_DISABLED | IRQF_NOBALANCING, |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 454 | "arm-pmu", NULL); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 455 | if (err) { |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 456 | pr_err("unable to request IRQ%d for ARM PMU counters\n", |
| 457 | irq); |
Will Deacon | 0b390e2 | 2011-07-27 15:18:59 +0100 | [diff] [blame] | 458 | armpmu_release_hardware(); |
| 459 | return err; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 460 | } |
Will Deacon | 0b390e2 | 2011-07-27 15:18:59 +0100 | [diff] [blame] | 461 | |
| 462 | cpumask_set_cpu(i, &armpmu->active_irqs); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 463 | } |
| 464 | |
Will Deacon | 0b390e2 | 2011-07-27 15:18:59 +0100 | [diff] [blame] | 465 | return 0; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | static atomic_t active_events = ATOMIC_INIT(0); |
| 469 | static DEFINE_MUTEX(pmu_reserve_mutex); |
| 470 | |
| 471 | static void |
| 472 | hw_perf_event_destroy(struct perf_event *event) |
| 473 | { |
| 474 | if (atomic_dec_and_mutex_lock(&active_events, &pmu_reserve_mutex)) { |
| 475 | armpmu_release_hardware(); |
| 476 | mutex_unlock(&pmu_reserve_mutex); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | static int |
| 481 | __hw_perf_event_init(struct perf_event *event) |
| 482 | { |
| 483 | struct hw_perf_event *hwc = &event->hw; |
| 484 | int mapping, err; |
| 485 | |
| 486 | /* Decode the generic type into an ARM event identifier. */ |
| 487 | if (PERF_TYPE_HARDWARE == event->attr.type) { |
Will Deacon | 84fee97 | 2010-11-13 17:13:56 +0000 | [diff] [blame] | 488 | mapping = armpmu_map_event(event->attr.config); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 489 | } else if (PERF_TYPE_HW_CACHE == event->attr.type) { |
| 490 | mapping = armpmu_map_cache_event(event->attr.config); |
| 491 | } else if (PERF_TYPE_RAW == event->attr.type) { |
Will Deacon | 84fee97 | 2010-11-13 17:13:56 +0000 | [diff] [blame] | 492 | mapping = armpmu_map_raw_event(event->attr.config); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 493 | } else { |
| 494 | pr_debug("event type %x not supported\n", event->attr.type); |
| 495 | return -EOPNOTSUPP; |
| 496 | } |
| 497 | |
| 498 | if (mapping < 0) { |
| 499 | pr_debug("event %x:%llx not supported\n", event->attr.type, |
| 500 | event->attr.config); |
| 501 | return mapping; |
| 502 | } |
| 503 | |
| 504 | /* |
| 505 | * Check whether we need to exclude the counter from certain modes. |
| 506 | * The ARM performance counters are on all of the time so if someone |
| 507 | * has asked us for some excludes then we have to fail. |
| 508 | */ |
| 509 | if (event->attr.exclude_kernel || event->attr.exclude_user || |
| 510 | event->attr.exclude_hv || event->attr.exclude_idle) { |
| 511 | pr_debug("ARM performance counters do not support " |
| 512 | "mode exclusion\n"); |
| 513 | return -EPERM; |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | * We don't assign an index until we actually place the event onto |
| 518 | * hardware. Use -1 to signify that we haven't decided where to put it |
| 519 | * yet. For SMP systems, each core has it's own PMU so we can't do any |
| 520 | * clever allocation or constraints checking at this point. |
| 521 | */ |
| 522 | hwc->idx = -1; |
| 523 | |
| 524 | /* |
| 525 | * Store the event encoding into the config_base field. config and |
| 526 | * event_base are unused as the only 2 things we need to know are |
| 527 | * the event mapping and the counter to use. The counter to use is |
| 528 | * also the indx and the config_base is the event type. |
| 529 | */ |
| 530 | hwc->config_base = (unsigned long)mapping; |
| 531 | hwc->config = 0; |
| 532 | hwc->event_base = 0; |
| 533 | |
| 534 | if (!hwc->sample_period) { |
| 535 | hwc->sample_period = armpmu->max_period; |
| 536 | hwc->last_period = hwc->sample_period; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 537 | local64_set(&hwc->period_left, hwc->sample_period); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | err = 0; |
| 541 | if (event->group_leader != event) { |
| 542 | err = validate_group(event); |
| 543 | if (err) |
| 544 | return -EINVAL; |
| 545 | } |
| 546 | |
| 547 | return err; |
| 548 | } |
| 549 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 550 | static int armpmu_event_init(struct perf_event *event) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 551 | { |
| 552 | int err = 0; |
| 553 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 554 | switch (event->attr.type) { |
| 555 | case PERF_TYPE_RAW: |
| 556 | case PERF_TYPE_HARDWARE: |
| 557 | case PERF_TYPE_HW_CACHE: |
| 558 | break; |
| 559 | |
| 560 | default: |
| 561 | return -ENOENT; |
| 562 | } |
| 563 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 564 | if (!armpmu) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 565 | return -ENODEV; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 566 | |
| 567 | event->destroy = hw_perf_event_destroy; |
| 568 | |
| 569 | if (!atomic_inc_not_zero(&active_events)) { |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 570 | mutex_lock(&pmu_reserve_mutex); |
| 571 | if (atomic_read(&active_events) == 0) { |
| 572 | err = armpmu_reserve_hardware(); |
| 573 | } |
| 574 | |
| 575 | if (!err) |
| 576 | atomic_inc(&active_events); |
| 577 | mutex_unlock(&pmu_reserve_mutex); |
| 578 | } |
| 579 | |
| 580 | if (err) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 581 | return err; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 582 | |
| 583 | err = __hw_perf_event_init(event); |
| 584 | if (err) |
| 585 | hw_perf_event_destroy(event); |
| 586 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 587 | return err; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 588 | } |
| 589 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 590 | static void armpmu_enable(struct pmu *pmu) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 591 | { |
| 592 | /* Enable all of the perf events on hardware. */ |
Will Deacon | f4f3843 | 2011-07-01 14:38:12 +0100 | [diff] [blame] | 593 | int idx, enabled = 0; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 594 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 595 | |
| 596 | if (!armpmu) |
| 597 | return; |
| 598 | |
Will Deacon | ecf5a89 | 2011-07-19 22:43:28 +0100 | [diff] [blame^] | 599 | for (idx = 0; idx < armpmu->num_events; ++idx) { |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 600 | struct perf_event *event = cpuc->events[idx]; |
| 601 | |
| 602 | if (!event) |
| 603 | continue; |
| 604 | |
| 605 | armpmu->enable(&event->hw, idx); |
Will Deacon | f4f3843 | 2011-07-01 14:38:12 +0100 | [diff] [blame] | 606 | enabled = 1; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 607 | } |
| 608 | |
Will Deacon | f4f3843 | 2011-07-01 14:38:12 +0100 | [diff] [blame] | 609 | if (enabled) |
| 610 | armpmu->start(); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 611 | } |
| 612 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 613 | static void armpmu_disable(struct pmu *pmu) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 614 | { |
| 615 | if (armpmu) |
| 616 | armpmu->stop(); |
| 617 | } |
| 618 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 619 | static struct pmu pmu = { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 620 | .pmu_enable = armpmu_enable, |
| 621 | .pmu_disable = armpmu_disable, |
| 622 | .event_init = armpmu_event_init, |
| 623 | .add = armpmu_add, |
| 624 | .del = armpmu_del, |
| 625 | .start = armpmu_start, |
| 626 | .stop = armpmu_stop, |
| 627 | .read = armpmu_read, |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 628 | }; |
| 629 | |
Will Deacon | 43eab87 | 2010-11-13 19:04:32 +0000 | [diff] [blame] | 630 | /* Include the PMU-specific implementations. */ |
| 631 | #include "perf_event_xscale.c" |
| 632 | #include "perf_event_v6.c" |
| 633 | #include "perf_event_v7.c" |
Will Deacon | 49e6a32 | 2010-04-30 11:33:33 +0100 | [diff] [blame] | 634 | |
Will Deacon | 574b69c | 2011-03-25 13:13:34 +0100 | [diff] [blame] | 635 | /* |
| 636 | * Ensure the PMU has sane values out of reset. |
| 637 | * This requires SMP to be available, so exists as a separate initcall. |
| 638 | */ |
| 639 | static int __init |
| 640 | armpmu_reset(void) |
| 641 | { |
| 642 | if (armpmu && armpmu->reset) |
| 643 | return on_each_cpu(armpmu->reset, NULL, 1); |
| 644 | return 0; |
| 645 | } |
| 646 | arch_initcall(armpmu_reset); |
| 647 | |
Will Deacon | b0e8959 | 2011-07-26 22:10:28 +0100 | [diff] [blame] | 648 | /* |
| 649 | * PMU platform driver and devicetree bindings. |
| 650 | */ |
| 651 | static struct of_device_id armpmu_of_device_ids[] = { |
| 652 | {.compatible = "arm,cortex-a9-pmu"}, |
| 653 | {.compatible = "arm,cortex-a8-pmu"}, |
| 654 | {.compatible = "arm,arm1136-pmu"}, |
| 655 | {.compatible = "arm,arm1176-pmu"}, |
| 656 | {}, |
| 657 | }; |
| 658 | |
| 659 | static struct platform_device_id armpmu_plat_device_ids[] = { |
| 660 | {.name = "arm-pmu"}, |
| 661 | {}, |
| 662 | }; |
| 663 | |
| 664 | static int __devinit armpmu_device_probe(struct platform_device *pdev) |
| 665 | { |
| 666 | pmu_device = pdev; |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | static struct platform_driver armpmu_driver = { |
| 671 | .driver = { |
| 672 | .name = "arm-pmu", |
| 673 | .of_match_table = armpmu_of_device_ids, |
| 674 | }, |
| 675 | .probe = armpmu_device_probe, |
| 676 | .id_table = armpmu_plat_device_ids, |
| 677 | }; |
| 678 | |
| 679 | static int __init register_pmu_driver(void) |
| 680 | { |
| 681 | return platform_driver_register(&armpmu_driver); |
| 682 | } |
| 683 | device_initcall(register_pmu_driver); |
| 684 | |
| 685 | /* |
| 686 | * CPU PMU identification and registration. |
| 687 | */ |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 688 | static int __init |
| 689 | init_hw_perf_events(void) |
| 690 | { |
| 691 | unsigned long cpuid = read_cpuid_id(); |
| 692 | unsigned long implementor = (cpuid & 0xFF000000) >> 24; |
| 693 | unsigned long part_number = (cpuid & 0xFFF0); |
| 694 | |
Will Deacon | 49e6a32 | 2010-04-30 11:33:33 +0100 | [diff] [blame] | 695 | /* ARM Ltd CPUs. */ |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 696 | if (0x41 == implementor) { |
| 697 | switch (part_number) { |
| 698 | case 0xB360: /* ARM1136 */ |
| 699 | case 0xB560: /* ARM1156 */ |
| 700 | case 0xB760: /* ARM1176 */ |
Will Deacon | 3cb314b | 2010-11-13 17:37:46 +0000 | [diff] [blame] | 701 | armpmu = armv6pmu_init(); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 702 | break; |
| 703 | case 0xB020: /* ARM11mpcore */ |
Will Deacon | 3cb314b | 2010-11-13 17:37:46 +0000 | [diff] [blame] | 704 | armpmu = armv6mpcore_pmu_init(); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 705 | break; |
Jean PIHET | 796d129 | 2010-01-26 18:51:05 +0100 | [diff] [blame] | 706 | case 0xC080: /* Cortex-A8 */ |
Will Deacon | 3cb314b | 2010-11-13 17:37:46 +0000 | [diff] [blame] | 707 | armpmu = armv7_a8_pmu_init(); |
Jean PIHET | 796d129 | 2010-01-26 18:51:05 +0100 | [diff] [blame] | 708 | break; |
| 709 | case 0xC090: /* Cortex-A9 */ |
Will Deacon | 3cb314b | 2010-11-13 17:37:46 +0000 | [diff] [blame] | 710 | armpmu = armv7_a9_pmu_init(); |
Jean PIHET | 796d129 | 2010-01-26 18:51:05 +0100 | [diff] [blame] | 711 | break; |
Will Deacon | 0c205cb | 2011-06-03 17:40:15 +0100 | [diff] [blame] | 712 | case 0xC050: /* Cortex-A5 */ |
| 713 | armpmu = armv7_a5_pmu_init(); |
| 714 | break; |
Will Deacon | 14abd03 | 2011-01-19 14:24:38 +0000 | [diff] [blame] | 715 | case 0xC0F0: /* Cortex-A15 */ |
| 716 | armpmu = armv7_a15_pmu_init(); |
| 717 | break; |
Will Deacon | 49e6a32 | 2010-04-30 11:33:33 +0100 | [diff] [blame] | 718 | } |
| 719 | /* Intel CPUs [xscale]. */ |
| 720 | } else if (0x69 == implementor) { |
| 721 | part_number = (cpuid >> 13) & 0x7; |
| 722 | switch (part_number) { |
| 723 | case 1: |
Will Deacon | 3cb314b | 2010-11-13 17:37:46 +0000 | [diff] [blame] | 724 | armpmu = xscale1pmu_init(); |
Will Deacon | 49e6a32 | 2010-04-30 11:33:33 +0100 | [diff] [blame] | 725 | break; |
| 726 | case 2: |
Will Deacon | 3cb314b | 2010-11-13 17:37:46 +0000 | [diff] [blame] | 727 | armpmu = xscale2pmu_init(); |
Will Deacon | 49e6a32 | 2010-04-30 11:33:33 +0100 | [diff] [blame] | 728 | break; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
Will Deacon | 49e6a32 | 2010-04-30 11:33:33 +0100 | [diff] [blame] | 732 | if (armpmu) { |
Jean PIHET | 796d129 | 2010-01-26 18:51:05 +0100 | [diff] [blame] | 733 | pr_info("enabled with %s PMU driver, %d counters available\n", |
Will Deacon | 6299483 | 2010-11-13 18:45:27 +0000 | [diff] [blame] | 734 | armpmu->name, armpmu->num_events); |
Will Deacon | 49e6a32 | 2010-04-30 11:33:33 +0100 | [diff] [blame] | 735 | } else { |
| 736 | pr_info("no hardware support available\n"); |
Will Deacon | 49e6a32 | 2010-04-30 11:33:33 +0100 | [diff] [blame] | 737 | } |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 738 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 739 | perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 740 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 741 | return 0; |
| 742 | } |
Peter Zijlstra | 004417a | 2010-11-25 18:38:29 +0100 | [diff] [blame] | 743 | early_initcall(init_hw_perf_events); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 744 | |
| 745 | /* |
| 746 | * Callchain handling code. |
| 747 | */ |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 748 | |
| 749 | /* |
| 750 | * The registers we're interested in are at the end of the variable |
| 751 | * length saved register structure. The fp points at the end of this |
| 752 | * structure so the address of this struct is: |
| 753 | * (struct frame_tail *)(xxx->fp)-1 |
| 754 | * |
| 755 | * This code has been adapted from the ARM OProfile support. |
| 756 | */ |
| 757 | struct frame_tail { |
Will Deacon | 4d6b7a7 | 2010-11-30 18:15:53 +0100 | [diff] [blame] | 758 | struct frame_tail __user *fp; |
| 759 | unsigned long sp; |
| 760 | unsigned long lr; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 761 | } __attribute__((packed)); |
| 762 | |
| 763 | /* |
| 764 | * Get the return address for a single stackframe and return a pointer to the |
| 765 | * next frame tail. |
| 766 | */ |
Will Deacon | 4d6b7a7 | 2010-11-30 18:15:53 +0100 | [diff] [blame] | 767 | static struct frame_tail __user * |
| 768 | user_backtrace(struct frame_tail __user *tail, |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 769 | struct perf_callchain_entry *entry) |
| 770 | { |
| 771 | struct frame_tail buftail; |
| 772 | |
| 773 | /* Also check accessibility of one struct frame_tail beyond */ |
| 774 | if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) |
| 775 | return NULL; |
| 776 | if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail))) |
| 777 | return NULL; |
| 778 | |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 779 | perf_callchain_store(entry, buftail.lr); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 780 | |
| 781 | /* |
| 782 | * Frame pointers should strictly progress back up the stack |
| 783 | * (towards higher addresses). |
| 784 | */ |
Rabin Vincent | cb06199 | 2011-02-09 11:35:12 +0100 | [diff] [blame] | 785 | if (tail + 1 >= buftail.fp) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 786 | return NULL; |
| 787 | |
| 788 | return buftail.fp - 1; |
| 789 | } |
| 790 | |
Frederic Weisbecker | 56962b4 | 2010-06-30 23:03:51 +0200 | [diff] [blame] | 791 | void |
| 792 | perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 793 | { |
Will Deacon | 4d6b7a7 | 2010-11-30 18:15:53 +0100 | [diff] [blame] | 794 | struct frame_tail __user *tail; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 795 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 796 | |
Will Deacon | 4d6b7a7 | 2010-11-30 18:15:53 +0100 | [diff] [blame] | 797 | tail = (struct frame_tail __user *)regs->ARM_fp - 1; |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 798 | |
Sonny Rao | 860ad78 | 2011-04-18 22:12:59 +0100 | [diff] [blame] | 799 | while ((entry->nr < PERF_MAX_STACK_DEPTH) && |
| 800 | tail && !((unsigned long)tail & 0x3)) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 801 | tail = user_backtrace(tail, entry); |
| 802 | } |
| 803 | |
| 804 | /* |
| 805 | * Gets called by walk_stackframe() for every stackframe. This will be called |
| 806 | * whist unwinding the stackframe and is like a subroutine return so we use |
| 807 | * the PC. |
| 808 | */ |
| 809 | static int |
| 810 | callchain_trace(struct stackframe *fr, |
| 811 | void *data) |
| 812 | { |
| 813 | struct perf_callchain_entry *entry = data; |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 814 | perf_callchain_store(entry, fr->pc); |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 815 | return 0; |
| 816 | } |
| 817 | |
Frederic Weisbecker | 56962b4 | 2010-06-30 23:03:51 +0200 | [diff] [blame] | 818 | void |
| 819 | perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs) |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 820 | { |
| 821 | struct stackframe fr; |
| 822 | |
Jamie Iles | 1b8873a | 2010-02-02 20:25:44 +0100 | [diff] [blame] | 823 | fr.fp = regs->ARM_fp; |
| 824 | fr.sp = regs->ARM_sp; |
| 825 | fr.lr = regs->ARM_lr; |
| 826 | fr.pc = regs->ARM_pc; |
| 827 | walk_stackframe(&fr, callchain_trace, entry); |
| 828 | } |