diff options
Diffstat (limited to 'arch/arm/kernel/perf_event.c')
-rw-r--r-- | arch/arm/kernel/perf_event.c | 475 |
1 files changed, 254 insertions, 221 deletions
diff --git a/arch/arm/kernel/perf_event.c b/arch/arm/kernel/perf_event.c index 53c9c2610cbc..e6e5d7c84f1a 100644 --- a/arch/arm/kernel/perf_event.c +++ b/arch/arm/kernel/perf_event.c | |||
@@ -12,6 +12,7 @@ | |||
12 | */ | 12 | */ |
13 | #define pr_fmt(fmt) "hw perfevents: " fmt | 13 | #define pr_fmt(fmt) "hw perfevents: " fmt |
14 | 14 | ||
15 | #include <linux/bitmap.h> | ||
15 | #include <linux/interrupt.h> | 16 | #include <linux/interrupt.h> |
16 | #include <linux/kernel.h> | 17 | #include <linux/kernel.h> |
17 | #include <linux/module.h> | 18 | #include <linux/module.h> |
@@ -26,16 +27,8 @@ | |||
26 | #include <asm/pmu.h> | 27 | #include <asm/pmu.h> |
27 | #include <asm/stacktrace.h> | 28 | #include <asm/stacktrace.h> |
28 | 29 | ||
29 | static struct platform_device *pmu_device; | ||
30 | |||
31 | /* | ||
32 | * Hardware lock to serialize accesses to PMU registers. Needed for the | ||
33 | * read/modify/write sequences. | ||
34 | */ | ||
35 | static DEFINE_RAW_SPINLOCK(pmu_lock); | ||
36 | |||
37 | /* | 30 | /* |
38 | * ARMv6 supports a maximum of 3 events, starting from index 1. If we add | 31 | * ARMv6 supports a maximum of 3 events, starting from index 0. If we add |
39 | * another platform that supports more, we need to increase this to be the | 32 | * another platform that supports more, we need to increase this to be the |
40 | * largest of all platforms. | 33 | * largest of all platforms. |
41 | * | 34 | * |
@@ -43,62 +36,24 @@ static DEFINE_RAW_SPINLOCK(pmu_lock); | |||
43 | * cycle counter CCNT + 31 events counters CNT0..30. | 36 | * cycle counter CCNT + 31 events counters CNT0..30. |
44 | * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters. | 37 | * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters. |
45 | */ | 38 | */ |
46 | #define ARMPMU_MAX_HWEVENTS 33 | 39 | #define ARMPMU_MAX_HWEVENTS 32 |
47 | 40 | ||
48 | /* The events for a given CPU. */ | 41 | static DEFINE_PER_CPU(struct perf_event * [ARMPMU_MAX_HWEVENTS], hw_events); |
49 | struct cpu_hw_events { | 42 | static DEFINE_PER_CPU(unsigned long [BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)], used_mask); |
50 | /* | 43 | static DEFINE_PER_CPU(struct pmu_hw_events, cpu_hw_events); |
51 | * The events that are active on the CPU for the given index. Index 0 | ||
52 | * is reserved. | ||
53 | */ | ||
54 | struct perf_event *events[ARMPMU_MAX_HWEVENTS]; | ||
55 | |||
56 | /* | ||
57 | * A 1 bit for an index indicates that the counter is being used for | ||
58 | * an event. A 0 means that the counter can be used. | ||
59 | */ | ||
60 | unsigned long used_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)]; | ||
61 | 44 | ||
62 | /* | 45 | #define to_arm_pmu(p) (container_of(p, struct arm_pmu, pmu)) |
63 | * A 1 bit for an index indicates that the counter is actively being | ||
64 | * used. | ||
65 | */ | ||
66 | unsigned long active_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)]; | ||
67 | }; | ||
68 | static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events); | ||
69 | |||
70 | struct arm_pmu { | ||
71 | enum arm_perf_pmu_ids id; | ||
72 | const char *name; | ||
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); | ||
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); | ||
82 | void (*reset)(void *); | ||
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; | ||
88 | int num_events; | ||
89 | u64 max_period; | ||
90 | }; | ||
91 | 46 | ||
92 | /* Set at runtime when we know what CPU type we are. */ | 47 | /* Set at runtime when we know what CPU type we are. */ |
93 | static const struct arm_pmu *armpmu; | 48 | static struct arm_pmu *cpu_pmu; |
94 | 49 | ||
95 | enum arm_perf_pmu_ids | 50 | enum arm_perf_pmu_ids |
96 | armpmu_get_pmu_id(void) | 51 | armpmu_get_pmu_id(void) |
97 | { | 52 | { |
98 | int id = -ENODEV; | 53 | int id = -ENODEV; |
99 | 54 | ||
100 | if (armpmu != NULL) | 55 | if (cpu_pmu != NULL) |
101 | id = armpmu->id; | 56 | id = cpu_pmu->id; |
102 | 57 | ||
103 | return id; | 58 | return id; |
104 | } | 59 | } |
@@ -109,8 +64,8 @@ armpmu_get_max_events(void) | |||
109 | { | 64 | { |
110 | int max_events = 0; | 65 | int max_events = 0; |
111 | 66 | ||
112 | if (armpmu != NULL) | 67 | if (cpu_pmu != NULL) |
113 | max_events = armpmu->num_events; | 68 | max_events = cpu_pmu->num_events; |
114 | 69 | ||
115 | return max_events; | 70 | return max_events; |
116 | } | 71 | } |
@@ -130,7 +85,11 @@ EXPORT_SYMBOL_GPL(perf_num_counters); | |||
130 | #define CACHE_OP_UNSUPPORTED 0xFFFF | 85 | #define CACHE_OP_UNSUPPORTED 0xFFFF |
131 | 86 | ||
132 | static int | 87 | static int |
133 | armpmu_map_cache_event(u64 config) | 88 | armpmu_map_cache_event(const unsigned (*cache_map) |
89 | [PERF_COUNT_HW_CACHE_MAX] | ||
90 | [PERF_COUNT_HW_CACHE_OP_MAX] | ||
91 | [PERF_COUNT_HW_CACHE_RESULT_MAX], | ||
92 | u64 config) | ||
134 | { | 93 | { |
135 | unsigned int cache_type, cache_op, cache_result, ret; | 94 | unsigned int cache_type, cache_op, cache_result, ret; |
136 | 95 | ||
@@ -146,7 +105,7 @@ armpmu_map_cache_event(u64 config) | |||
146 | if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX) | 105 | if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX) |
147 | return -EINVAL; | 106 | return -EINVAL; |
148 | 107 | ||
149 | ret = (int)(*armpmu->cache_map)[cache_type][cache_op][cache_result]; | 108 | ret = (int)(*cache_map)[cache_type][cache_op][cache_result]; |
150 | 109 | ||
151 | if (ret == CACHE_OP_UNSUPPORTED) | 110 | if (ret == CACHE_OP_UNSUPPORTED) |
152 | return -ENOENT; | 111 | return -ENOENT; |
@@ -155,23 +114,46 @@ armpmu_map_cache_event(u64 config) | |||
155 | } | 114 | } |
156 | 115 | ||
157 | static int | 116 | static int |
158 | armpmu_map_event(u64 config) | 117 | armpmu_map_event(const unsigned (*event_map)[PERF_COUNT_HW_MAX], u64 config) |
159 | { | 118 | { |
160 | int mapping = (*armpmu->event_map)[config]; | 119 | int mapping = (*event_map)[config]; |
161 | return mapping == HW_OP_UNSUPPORTED ? -EOPNOTSUPP : mapping; | 120 | return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping; |
162 | } | 121 | } |
163 | 122 | ||
164 | static int | 123 | static int |
165 | armpmu_map_raw_event(u64 config) | 124 | armpmu_map_raw_event(u32 raw_event_mask, u64 config) |
166 | { | 125 | { |
167 | return (int)(config & armpmu->raw_event_mask); | 126 | return (int)(config & raw_event_mask); |
168 | } | 127 | } |
169 | 128 | ||
170 | static int | 129 | static int map_cpu_event(struct perf_event *event, |
130 | const unsigned (*event_map)[PERF_COUNT_HW_MAX], | ||
131 | const unsigned (*cache_map) | ||
132 | [PERF_COUNT_HW_CACHE_MAX] | ||
133 | [PERF_COUNT_HW_CACHE_OP_MAX] | ||
134 | [PERF_COUNT_HW_CACHE_RESULT_MAX], | ||
135 | u32 raw_event_mask) | ||
136 | { | ||
137 | u64 config = event->attr.config; | ||
138 | |||
139 | switch (event->attr.type) { | ||
140 | case PERF_TYPE_HARDWARE: | ||
141 | return armpmu_map_event(event_map, config); | ||
142 | case PERF_TYPE_HW_CACHE: | ||
143 | return armpmu_map_cache_event(cache_map, config); | ||
144 | case PERF_TYPE_RAW: | ||
145 | return armpmu_map_raw_event(raw_event_mask, config); | ||
146 | } | ||
147 | |||
148 | return -ENOENT; | ||
149 | } | ||
150 | |||
151 | int | ||
171 | armpmu_event_set_period(struct perf_event *event, | 152 | armpmu_event_set_period(struct perf_event *event, |
172 | struct hw_perf_event *hwc, | 153 | struct hw_perf_event *hwc, |
173 | int idx) | 154 | int idx) |
174 | { | 155 | { |
156 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); | ||
175 | s64 left = local64_read(&hwc->period_left); | 157 | s64 left = local64_read(&hwc->period_left); |
176 | s64 period = hwc->sample_period; | 158 | s64 period = hwc->sample_period; |
177 | int ret = 0; | 159 | int ret = 0; |
@@ -202,11 +184,12 @@ armpmu_event_set_period(struct perf_event *event, | |||
202 | return ret; | 184 | return ret; |
203 | } | 185 | } |
204 | 186 | ||
205 | static u64 | 187 | u64 |
206 | armpmu_event_update(struct perf_event *event, | 188 | armpmu_event_update(struct perf_event *event, |
207 | struct hw_perf_event *hwc, | 189 | struct hw_perf_event *hwc, |
208 | int idx, int overflow) | 190 | int idx, int overflow) |
209 | { | 191 | { |
192 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); | ||
210 | u64 delta, prev_raw_count, new_raw_count; | 193 | u64 delta, prev_raw_count, new_raw_count; |
211 | 194 | ||
212 | again: | 195 | again: |
@@ -246,11 +229,9 @@ armpmu_read(struct perf_event *event) | |||
246 | static void | 229 | static void |
247 | armpmu_stop(struct perf_event *event, int flags) | 230 | armpmu_stop(struct perf_event *event, int flags) |
248 | { | 231 | { |
232 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); | ||
249 | struct hw_perf_event *hwc = &event->hw; | 233 | struct hw_perf_event *hwc = &event->hw; |
250 | 234 | ||
251 | if (!armpmu) | ||
252 | return; | ||
253 | |||
254 | /* | 235 | /* |
255 | * ARM pmu always has to update the counter, so ignore | 236 | * ARM pmu always has to update the counter, so ignore |
256 | * PERF_EF_UPDATE, see comments in armpmu_start(). | 237 | * PERF_EF_UPDATE, see comments in armpmu_start(). |
@@ -266,11 +247,9 @@ armpmu_stop(struct perf_event *event, int flags) | |||
266 | static void | 247 | static void |
267 | armpmu_start(struct perf_event *event, int flags) | 248 | armpmu_start(struct perf_event *event, int flags) |
268 | { | 249 | { |
250 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); | ||
269 | struct hw_perf_event *hwc = &event->hw; | 251 | struct hw_perf_event *hwc = &event->hw; |
270 | 252 | ||
271 | if (!armpmu) | ||
272 | return; | ||
273 | |||
274 | /* | 253 | /* |
275 | * ARM pmu always has to reprogram the period, so ignore | 254 | * ARM pmu always has to reprogram the period, so ignore |
276 | * PERF_EF_RELOAD, see the comment below. | 255 | * PERF_EF_RELOAD, see the comment below. |
@@ -293,16 +272,16 @@ armpmu_start(struct perf_event *event, int flags) | |||
293 | static void | 272 | static void |
294 | armpmu_del(struct perf_event *event, int flags) | 273 | armpmu_del(struct perf_event *event, int flags) |
295 | { | 274 | { |
296 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | 275 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
276 | struct pmu_hw_events *hw_events = armpmu->get_hw_events(); | ||
297 | struct hw_perf_event *hwc = &event->hw; | 277 | struct hw_perf_event *hwc = &event->hw; |
298 | int idx = hwc->idx; | 278 | int idx = hwc->idx; |
299 | 279 | ||
300 | WARN_ON(idx < 0); | 280 | WARN_ON(idx < 0); |
301 | 281 | ||
302 | clear_bit(idx, cpuc->active_mask); | ||
303 | armpmu_stop(event, PERF_EF_UPDATE); | 282 | armpmu_stop(event, PERF_EF_UPDATE); |
304 | cpuc->events[idx] = NULL; | 283 | hw_events->events[idx] = NULL; |
305 | clear_bit(idx, cpuc->used_mask); | 284 | clear_bit(idx, hw_events->used_mask); |
306 | 285 | ||
307 | perf_event_update_userpage(event); | 286 | perf_event_update_userpage(event); |
308 | } | 287 | } |
@@ -310,7 +289,8 @@ armpmu_del(struct perf_event *event, int flags) | |||
310 | static int | 289 | static int |
311 | armpmu_add(struct perf_event *event, int flags) | 290 | armpmu_add(struct perf_event *event, int flags) |
312 | { | 291 | { |
313 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | 292 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
293 | struct pmu_hw_events *hw_events = armpmu->get_hw_events(); | ||
314 | struct hw_perf_event *hwc = &event->hw; | 294 | struct hw_perf_event *hwc = &event->hw; |
315 | int idx; | 295 | int idx; |
316 | int err = 0; | 296 | int err = 0; |
@@ -318,7 +298,7 @@ armpmu_add(struct perf_event *event, int flags) | |||
318 | perf_pmu_disable(event->pmu); | 298 | perf_pmu_disable(event->pmu); |
319 | 299 | ||
320 | /* If we don't have a space for the counter then finish early. */ | 300 | /* If we don't have a space for the counter then finish early. */ |
321 | idx = armpmu->get_event_idx(cpuc, hwc); | 301 | idx = armpmu->get_event_idx(hw_events, hwc); |
322 | if (idx < 0) { | 302 | if (idx < 0) { |
323 | err = idx; | 303 | err = idx; |
324 | goto out; | 304 | goto out; |
@@ -330,8 +310,7 @@ armpmu_add(struct perf_event *event, int flags) | |||
330 | */ | 310 | */ |
331 | event->hw.idx = idx; | 311 | event->hw.idx = idx; |
332 | armpmu->disable(hwc, idx); | 312 | armpmu->disable(hwc, idx); |
333 | cpuc->events[idx] = event; | 313 | hw_events->events[idx] = event; |
334 | set_bit(idx, cpuc->active_mask); | ||
335 | 314 | ||
336 | hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE; | 315 | hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE; |
337 | if (flags & PERF_EF_START) | 316 | if (flags & PERF_EF_START) |
@@ -345,25 +324,25 @@ out: | |||
345 | return err; | 324 | return err; |
346 | } | 325 | } |
347 | 326 | ||
348 | static struct pmu pmu; | ||
349 | |||
350 | static int | 327 | static int |
351 | validate_event(struct cpu_hw_events *cpuc, | 328 | validate_event(struct pmu_hw_events *hw_events, |
352 | struct perf_event *event) | 329 | struct perf_event *event) |
353 | { | 330 | { |
331 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); | ||
354 | struct hw_perf_event fake_event = event->hw; | 332 | struct hw_perf_event fake_event = event->hw; |
333 | struct pmu *leader_pmu = event->group_leader->pmu; | ||
355 | 334 | ||
356 | if (event->pmu != &pmu || event->state <= PERF_EVENT_STATE_OFF) | 335 | if (event->pmu != leader_pmu || event->state <= PERF_EVENT_STATE_OFF) |
357 | return 1; | 336 | return 1; |
358 | 337 | ||
359 | return armpmu->get_event_idx(cpuc, &fake_event) >= 0; | 338 | return armpmu->get_event_idx(hw_events, &fake_event) >= 0; |
360 | } | 339 | } |
361 | 340 | ||
362 | static int | 341 | static int |
363 | validate_group(struct perf_event *event) | 342 | validate_group(struct perf_event *event) |
364 | { | 343 | { |
365 | struct perf_event *sibling, *leader = event->group_leader; | 344 | struct perf_event *sibling, *leader = event->group_leader; |
366 | struct cpu_hw_events fake_pmu; | 345 | struct pmu_hw_events fake_pmu; |
367 | 346 | ||
368 | memset(&fake_pmu, 0, sizeof(fake_pmu)); | 347 | memset(&fake_pmu, 0, sizeof(fake_pmu)); |
369 | 348 | ||
@@ -383,110 +362,119 @@ validate_group(struct perf_event *event) | |||
383 | 362 | ||
384 | static irqreturn_t armpmu_platform_irq(int irq, void *dev) | 363 | static irqreturn_t armpmu_platform_irq(int irq, void *dev) |
385 | { | 364 | { |
386 | struct arm_pmu_platdata *plat = dev_get_platdata(&pmu_device->dev); | 365 | struct arm_pmu *armpmu = (struct arm_pmu *) dev; |
366 | struct platform_device *plat_device = armpmu->plat_device; | ||
367 | struct arm_pmu_platdata *plat = dev_get_platdata(&plat_device->dev); | ||
387 | 368 | ||
388 | return plat->handle_irq(irq, dev, armpmu->handle_irq); | 369 | return plat->handle_irq(irq, dev, armpmu->handle_irq); |
389 | } | 370 | } |
390 | 371 | ||
372 | static void | ||
373 | armpmu_release_hardware(struct arm_pmu *armpmu) | ||
374 | { | ||
375 | int i, irq, irqs; | ||
376 | struct platform_device *pmu_device = armpmu->plat_device; | ||
377 | |||
378 | irqs = min(pmu_device->num_resources, num_possible_cpus()); | ||
379 | |||
380 | for (i = 0; i < irqs; ++i) { | ||
381 | if (!cpumask_test_and_clear_cpu(i, &armpmu->active_irqs)) | ||
382 | continue; | ||
383 | irq = platform_get_irq(pmu_device, i); | ||
384 | if (irq >= 0) | ||
385 | free_irq(irq, armpmu); | ||
386 | } | ||
387 | |||
388 | release_pmu(armpmu->type); | ||
389 | } | ||
390 | |||
391 | static int | 391 | static int |
392 | armpmu_reserve_hardware(void) | 392 | armpmu_reserve_hardware(struct arm_pmu *armpmu) |
393 | { | 393 | { |
394 | struct arm_pmu_platdata *plat; | 394 | struct arm_pmu_platdata *plat; |
395 | irq_handler_t handle_irq; | 395 | irq_handler_t handle_irq; |
396 | int i, err = -ENODEV, irq; | 396 | int i, err, irq, irqs; |
397 | struct platform_device *pmu_device = armpmu->plat_device; | ||
397 | 398 | ||
398 | pmu_device = reserve_pmu(ARM_PMU_DEVICE_CPU); | 399 | err = reserve_pmu(armpmu->type); |
399 | if (IS_ERR(pmu_device)) { | 400 | if (err) { |
400 | pr_warning("unable to reserve pmu\n"); | 401 | pr_warning("unable to reserve pmu\n"); |
401 | return PTR_ERR(pmu_device); | 402 | return err; |
402 | } | 403 | } |
403 | 404 | ||
404 | init_pmu(ARM_PMU_DEVICE_CPU); | ||
405 | |||
406 | plat = dev_get_platdata(&pmu_device->dev); | 405 | plat = dev_get_platdata(&pmu_device->dev); |
407 | if (plat && plat->handle_irq) | 406 | if (plat && plat->handle_irq) |
408 | handle_irq = armpmu_platform_irq; | 407 | handle_irq = armpmu_platform_irq; |
409 | else | 408 | else |
410 | handle_irq = armpmu->handle_irq; | 409 | handle_irq = armpmu->handle_irq; |
411 | 410 | ||
412 | if (pmu_device->num_resources < 1) { | 411 | irqs = min(pmu_device->num_resources, num_possible_cpus()); |
412 | if (irqs < 1) { | ||
413 | pr_err("no irqs for PMUs defined\n"); | 413 | pr_err("no irqs for PMUs defined\n"); |
414 | return -ENODEV; | 414 | return -ENODEV; |
415 | } | 415 | } |
416 | 416 | ||
417 | for (i = 0; i < pmu_device->num_resources; ++i) { | 417 | for (i = 0; i < irqs; ++i) { |
418 | err = 0; | ||
418 | irq = platform_get_irq(pmu_device, i); | 419 | irq = platform_get_irq(pmu_device, i); |
419 | if (irq < 0) | 420 | if (irq < 0) |
420 | continue; | 421 | continue; |
421 | 422 | ||
423 | /* | ||
424 | * If we have a single PMU interrupt that we can't shift, | ||
425 | * assume that we're running on a uniprocessor machine and | ||
426 | * continue. Otherwise, continue without this interrupt. | ||
427 | */ | ||
428 | if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) { | ||
429 | pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n", | ||
430 | irq, i); | ||
431 | continue; | ||
432 | } | ||
433 | |||
422 | err = request_irq(irq, handle_irq, | 434 | err = request_irq(irq, handle_irq, |
423 | IRQF_DISABLED | IRQF_NOBALANCING, | 435 | IRQF_DISABLED | IRQF_NOBALANCING, |
424 | "armpmu", NULL); | 436 | "arm-pmu", armpmu); |
425 | if (err) { | 437 | if (err) { |
426 | pr_warning("unable to request IRQ%d for ARM perf " | 438 | pr_err("unable to request IRQ%d for ARM PMU counters\n", |
427 | "counters\n", irq); | 439 | irq); |
428 | break; | 440 | armpmu_release_hardware(armpmu); |
441 | return err; | ||
429 | } | 442 | } |
430 | } | ||
431 | 443 | ||
432 | if (err) { | 444 | cpumask_set_cpu(i, &armpmu->active_irqs); |
433 | for (i = i - 1; i >= 0; --i) { | ||
434 | irq = platform_get_irq(pmu_device, i); | ||
435 | if (irq >= 0) | ||
436 | free_irq(irq, NULL); | ||
437 | } | ||
438 | release_pmu(ARM_PMU_DEVICE_CPU); | ||
439 | pmu_device = NULL; | ||
440 | } | 445 | } |
441 | 446 | ||
442 | return err; | 447 | return 0; |
443 | } | 448 | } |
444 | 449 | ||
445 | static void | 450 | static void |
446 | armpmu_release_hardware(void) | 451 | hw_perf_event_destroy(struct perf_event *event) |
447 | { | 452 | { |
448 | int i, irq; | 453 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
454 | atomic_t *active_events = &armpmu->active_events; | ||
455 | struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex; | ||
449 | 456 | ||
450 | for (i = pmu_device->num_resources - 1; i >= 0; --i) { | 457 | if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) { |
451 | irq = platform_get_irq(pmu_device, i); | 458 | armpmu_release_hardware(armpmu); |
452 | if (irq >= 0) | 459 | mutex_unlock(pmu_reserve_mutex); |
453 | free_irq(irq, NULL); | ||
454 | } | 460 | } |
455 | armpmu->stop(); | ||
456 | |||
457 | release_pmu(ARM_PMU_DEVICE_CPU); | ||
458 | pmu_device = NULL; | ||
459 | } | 461 | } |
460 | 462 | ||
461 | static atomic_t active_events = ATOMIC_INIT(0); | 463 | static int |
462 | static DEFINE_MUTEX(pmu_reserve_mutex); | 464 | event_requires_mode_exclusion(struct perf_event_attr *attr) |
463 | |||
464 | static void | ||
465 | hw_perf_event_destroy(struct perf_event *event) | ||
466 | { | 465 | { |
467 | if (atomic_dec_and_mutex_lock(&active_events, &pmu_reserve_mutex)) { | 466 | return attr->exclude_idle || attr->exclude_user || |
468 | armpmu_release_hardware(); | 467 | attr->exclude_kernel || attr->exclude_hv; |
469 | mutex_unlock(&pmu_reserve_mutex); | ||
470 | } | ||
471 | } | 468 | } |
472 | 469 | ||
473 | static int | 470 | static int |
474 | __hw_perf_event_init(struct perf_event *event) | 471 | __hw_perf_event_init(struct perf_event *event) |
475 | { | 472 | { |
473 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); | ||
476 | struct hw_perf_event *hwc = &event->hw; | 474 | struct hw_perf_event *hwc = &event->hw; |
477 | int mapping, err; | 475 | int mapping, err; |
478 | 476 | ||
479 | /* Decode the generic type into an ARM event identifier. */ | 477 | mapping = armpmu->map_event(event); |
480 | if (PERF_TYPE_HARDWARE == event->attr.type) { | ||
481 | mapping = armpmu_map_event(event->attr.config); | ||
482 | } else if (PERF_TYPE_HW_CACHE == event->attr.type) { | ||
483 | mapping = armpmu_map_cache_event(event->attr.config); | ||
484 | } else if (PERF_TYPE_RAW == event->attr.type) { | ||
485 | mapping = armpmu_map_raw_event(event->attr.config); | ||
486 | } else { | ||
487 | pr_debug("event type %x not supported\n", event->attr.type); | ||
488 | return -EOPNOTSUPP; | ||
489 | } | ||
490 | 478 | ||
491 | if (mapping < 0) { | 479 | if (mapping < 0) { |
492 | pr_debug("event %x:%llx not supported\n", event->attr.type, | 480 | pr_debug("event %x:%llx not supported\n", event->attr.type, |
@@ -495,34 +483,31 @@ __hw_perf_event_init(struct perf_event *event) | |||
495 | } | 483 | } |
496 | 484 | ||
497 | /* | 485 | /* |
486 | * We don't assign an index until we actually place the event onto | ||
487 | * hardware. Use -1 to signify that we haven't decided where to put it | ||
488 | * yet. For SMP systems, each core has it's own PMU so we can't do any | ||
489 | * clever allocation or constraints checking at this point. | ||
490 | */ | ||
491 | hwc->idx = -1; | ||
492 | hwc->config_base = 0; | ||
493 | hwc->config = 0; | ||
494 | hwc->event_base = 0; | ||
495 | |||
496 | /* | ||
498 | * Check whether we need to exclude the counter from certain modes. | 497 | * Check whether we need to exclude the counter from certain modes. |
499 | * The ARM performance counters are on all of the time so if someone | ||
500 | * has asked us for some excludes then we have to fail. | ||
501 | */ | 498 | */ |
502 | if (event->attr.exclude_kernel || event->attr.exclude_user || | 499 | if ((!armpmu->set_event_filter || |
503 | event->attr.exclude_hv || event->attr.exclude_idle) { | 500 | armpmu->set_event_filter(hwc, &event->attr)) && |
501 | event_requires_mode_exclusion(&event->attr)) { | ||
504 | pr_debug("ARM performance counters do not support " | 502 | pr_debug("ARM performance counters do not support " |
505 | "mode exclusion\n"); | 503 | "mode exclusion\n"); |
506 | return -EPERM; | 504 | return -EPERM; |
507 | } | 505 | } |
508 | 506 | ||
509 | /* | 507 | /* |
510 | * We don't assign an index until we actually place the event onto | 508 | * Store the event encoding into the config_base field. |
511 | * hardware. Use -1 to signify that we haven't decided where to put it | ||
512 | * yet. For SMP systems, each core has it's own PMU so we can't do any | ||
513 | * clever allocation or constraints checking at this point. | ||
514 | */ | 509 | */ |
515 | hwc->idx = -1; | 510 | hwc->config_base |= (unsigned long)mapping; |
516 | |||
517 | /* | ||
518 | * Store the event encoding into the config_base field. config and | ||
519 | * event_base are unused as the only 2 things we need to know are | ||
520 | * the event mapping and the counter to use. The counter to use is | ||
521 | * also the indx and the config_base is the event type. | ||
522 | */ | ||
523 | hwc->config_base = (unsigned long)mapping; | ||
524 | hwc->config = 0; | ||
525 | hwc->event_base = 0; | ||
526 | 511 | ||
527 | if (!hwc->sample_period) { | 512 | if (!hwc->sample_period) { |
528 | hwc->sample_period = armpmu->max_period; | 513 | hwc->sample_period = armpmu->max_period; |
@@ -542,32 +527,23 @@ __hw_perf_event_init(struct perf_event *event) | |||
542 | 527 | ||
543 | static int armpmu_event_init(struct perf_event *event) | 528 | static int armpmu_event_init(struct perf_event *event) |
544 | { | 529 | { |
530 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); | ||
545 | int err = 0; | 531 | int err = 0; |
532 | atomic_t *active_events = &armpmu->active_events; | ||
546 | 533 | ||
547 | switch (event->attr.type) { | 534 | if (armpmu->map_event(event) == -ENOENT) |
548 | case PERF_TYPE_RAW: | ||
549 | case PERF_TYPE_HARDWARE: | ||
550 | case PERF_TYPE_HW_CACHE: | ||
551 | break; | ||
552 | |||
553 | default: | ||
554 | return -ENOENT; | 535 | return -ENOENT; |
555 | } | ||
556 | |||
557 | if (!armpmu) | ||
558 | return -ENODEV; | ||
559 | 536 | ||
560 | event->destroy = hw_perf_event_destroy; | 537 | event->destroy = hw_perf_event_destroy; |
561 | 538 | ||
562 | if (!atomic_inc_not_zero(&active_events)) { | 539 | if (!atomic_inc_not_zero(active_events)) { |
563 | mutex_lock(&pmu_reserve_mutex); | 540 | mutex_lock(&armpmu->reserve_mutex); |
564 | if (atomic_read(&active_events) == 0) { | 541 | if (atomic_read(active_events) == 0) |
565 | err = armpmu_reserve_hardware(); | 542 | err = armpmu_reserve_hardware(armpmu); |
566 | } | ||
567 | 543 | ||
568 | if (!err) | 544 | if (!err) |
569 | atomic_inc(&active_events); | 545 | atomic_inc(active_events); |
570 | mutex_unlock(&pmu_reserve_mutex); | 546 | mutex_unlock(&armpmu->reserve_mutex); |
571 | } | 547 | } |
572 | 548 | ||
573 | if (err) | 549 | if (err) |
@@ -582,22 +558,9 @@ static int armpmu_event_init(struct perf_event *event) | |||
582 | 558 | ||
583 | static void armpmu_enable(struct pmu *pmu) | 559 | static void armpmu_enable(struct pmu *pmu) |
584 | { | 560 | { |
585 | /* Enable all of the perf events on hardware. */ | 561 | struct arm_pmu *armpmu = to_arm_pmu(pmu); |
586 | int idx, enabled = 0; | 562 | struct pmu_hw_events *hw_events = armpmu->get_hw_events(); |
587 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | 563 | int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events); |
588 | |||
589 | if (!armpmu) | ||
590 | return; | ||
591 | |||
592 | for (idx = 0; idx <= armpmu->num_events; ++idx) { | ||
593 | struct perf_event *event = cpuc->events[idx]; | ||
594 | |||
595 | if (!event) | ||
596 | continue; | ||
597 | |||
598 | armpmu->enable(&event->hw, idx); | ||
599 | enabled = 1; | ||
600 | } | ||
601 | 564 | ||
602 | if (enabled) | 565 | if (enabled) |
603 | armpmu->start(); | 566 | armpmu->start(); |
@@ -605,20 +568,32 @@ static void armpmu_enable(struct pmu *pmu) | |||
605 | 568 | ||
606 | static void armpmu_disable(struct pmu *pmu) | 569 | static void armpmu_disable(struct pmu *pmu) |
607 | { | 570 | { |
608 | if (armpmu) | 571 | struct arm_pmu *armpmu = to_arm_pmu(pmu); |
609 | armpmu->stop(); | 572 | armpmu->stop(); |
610 | } | 573 | } |
611 | 574 | ||
612 | static struct pmu pmu = { | 575 | static void __init armpmu_init(struct arm_pmu *armpmu) |
613 | .pmu_enable = armpmu_enable, | 576 | { |
614 | .pmu_disable = armpmu_disable, | 577 | atomic_set(&armpmu->active_events, 0); |
615 | .event_init = armpmu_event_init, | 578 | mutex_init(&armpmu->reserve_mutex); |
616 | .add = armpmu_add, | 579 | |
617 | .del = armpmu_del, | 580 | armpmu->pmu = (struct pmu) { |
618 | .start = armpmu_start, | 581 | .pmu_enable = armpmu_enable, |
619 | .stop = armpmu_stop, | 582 | .pmu_disable = armpmu_disable, |
620 | .read = armpmu_read, | 583 | .event_init = armpmu_event_init, |
621 | }; | 584 | .add = armpmu_add, |
585 | .del = armpmu_del, | ||
586 | .start = armpmu_start, | ||
587 | .stop = armpmu_stop, | ||
588 | .read = armpmu_read, | ||
589 | }; | ||
590 | } | ||
591 | |||
592 | int __init armpmu_register(struct arm_pmu *armpmu, char *name, int type) | ||
593 | { | ||
594 | armpmu_init(armpmu); | ||
595 | return perf_pmu_register(&armpmu->pmu, name, type); | ||
596 | } | ||
622 | 597 | ||
623 | /* Include the PMU-specific implementations. */ | 598 | /* Include the PMU-specific implementations. */ |
624 | #include "perf_event_xscale.c" | 599 | #include "perf_event_xscale.c" |
@@ -630,14 +605,72 @@ static struct pmu pmu = { | |||
630 | * This requires SMP to be available, so exists as a separate initcall. | 605 | * This requires SMP to be available, so exists as a separate initcall. |
631 | */ | 606 | */ |
632 | static int __init | 607 | static int __init |
633 | armpmu_reset(void) | 608 | cpu_pmu_reset(void) |
609 | { | ||
610 | if (cpu_pmu && cpu_pmu->reset) | ||
611 | return on_each_cpu(cpu_pmu->reset, NULL, 1); | ||
612 | return 0; | ||
613 | } | ||
614 | arch_initcall(cpu_pmu_reset); | ||
615 | |||
616 | /* | ||
617 | * PMU platform driver and devicetree bindings. | ||
618 | */ | ||
619 | static struct of_device_id armpmu_of_device_ids[] = { | ||
620 | {.compatible = "arm,cortex-a9-pmu"}, | ||
621 | {.compatible = "arm,cortex-a8-pmu"}, | ||
622 | {.compatible = "arm,arm1136-pmu"}, | ||
623 | {.compatible = "arm,arm1176-pmu"}, | ||
624 | {}, | ||
625 | }; | ||
626 | |||
627 | static struct platform_device_id armpmu_plat_device_ids[] = { | ||
628 | {.name = "arm-pmu"}, | ||
629 | {}, | ||
630 | }; | ||
631 | |||
632 | static int __devinit armpmu_device_probe(struct platform_device *pdev) | ||
634 | { | 633 | { |
635 | if (armpmu && armpmu->reset) | 634 | cpu_pmu->plat_device = pdev; |
636 | return on_each_cpu(armpmu->reset, NULL, 1); | ||
637 | return 0; | 635 | return 0; |
638 | } | 636 | } |
639 | arch_initcall(armpmu_reset); | ||
640 | 637 | ||
638 | static struct platform_driver armpmu_driver = { | ||
639 | .driver = { | ||
640 | .name = "arm-pmu", | ||
641 | .of_match_table = armpmu_of_device_ids, | ||
642 | }, | ||
643 | .probe = armpmu_device_probe, | ||
644 | .id_table = armpmu_plat_device_ids, | ||
645 | }; | ||
646 | |||
647 | static int __init register_pmu_driver(void) | ||
648 | { | ||
649 | return platform_driver_register(&armpmu_driver); | ||
650 | } | ||
651 | device_initcall(register_pmu_driver); | ||
652 | |||
653 | static struct pmu_hw_events *armpmu_get_cpu_events(void) | ||
654 | { | ||
655 | return &__get_cpu_var(cpu_hw_events); | ||
656 | } | ||
657 | |||
658 | static void __init cpu_pmu_init(struct arm_pmu *armpmu) | ||
659 | { | ||
660 | int cpu; | ||
661 | for_each_possible_cpu(cpu) { | ||
662 | struct pmu_hw_events *events = &per_cpu(cpu_hw_events, cpu); | ||
663 | events->events = per_cpu(hw_events, cpu); | ||
664 | events->used_mask = per_cpu(used_mask, cpu); | ||
665 | raw_spin_lock_init(&events->pmu_lock); | ||
666 | } | ||
667 | armpmu->get_hw_events = armpmu_get_cpu_events; | ||
668 | armpmu->type = ARM_PMU_DEVICE_CPU; | ||
669 | } | ||
670 | |||
671 | /* | ||
672 | * CPU PMU identification and registration. | ||
673 | */ | ||
641 | static int __init | 674 | static int __init |
642 | init_hw_perf_events(void) | 675 | init_hw_perf_events(void) |
643 | { | 676 | { |
@@ -651,22 +684,22 @@ init_hw_perf_events(void) | |||
651 | case 0xB360: /* ARM1136 */ | 684 | case 0xB360: /* ARM1136 */ |
652 | case 0xB560: /* ARM1156 */ | 685 | case 0xB560: /* ARM1156 */ |
653 | case 0xB760: /* ARM1176 */ | 686 | case 0xB760: /* ARM1176 */ |
654 | armpmu = armv6pmu_init(); | 687 | cpu_pmu = armv6pmu_init(); |
655 | break; | 688 | break; |
656 | case 0xB020: /* ARM11mpcore */ | 689 | case 0xB020: /* ARM11mpcore */ |
657 | armpmu = armv6mpcore_pmu_init(); | 690 | cpu_pmu = armv6mpcore_pmu_init(); |
658 | break; | 691 | break; |
659 | case 0xC080: /* Cortex-A8 */ | 692 | case 0xC080: /* Cortex-A8 */ |
660 | armpmu = armv7_a8_pmu_init(); | 693 | cpu_pmu = armv7_a8_pmu_init(); |
661 | break; | 694 | break; |
662 | case 0xC090: /* Cortex-A9 */ | 695 | case 0xC090: /* Cortex-A9 */ |
663 | armpmu = armv7_a9_pmu_init(); | 696 | cpu_pmu = armv7_a9_pmu_init(); |
664 | break; | 697 | break; |
665 | case 0xC050: /* Cortex-A5 */ | 698 | case 0xC050: /* Cortex-A5 */ |
666 | armpmu = armv7_a5_pmu_init(); | 699 | cpu_pmu = armv7_a5_pmu_init(); |
667 | break; | 700 | break; |
668 | case 0xC0F0: /* Cortex-A15 */ | 701 | case 0xC0F0: /* Cortex-A15 */ |
669 | armpmu = armv7_a15_pmu_init(); | 702 | cpu_pmu = armv7_a15_pmu_init(); |
670 | break; | 703 | break; |
671 | } | 704 | } |
672 | /* Intel CPUs [xscale]. */ | 705 | /* Intel CPUs [xscale]. */ |
@@ -674,23 +707,23 @@ init_hw_perf_events(void) | |||
674 | part_number = (cpuid >> 13) & 0x7; | 707 | part_number = (cpuid >> 13) & 0x7; |
675 | switch (part_number) { | 708 | switch (part_number) { |
676 | case 1: | 709 | case 1: |
677 | armpmu = xscale1pmu_init(); | 710 | cpu_pmu = xscale1pmu_init(); |
678 | break; | 711 | break; |
679 | case 2: | 712 | case 2: |
680 | armpmu = xscale2pmu_init(); | 713 | cpu_pmu = xscale2pmu_init(); |
681 | break; | 714 | break; |
682 | } | 715 | } |
683 | } | 716 | } |
684 | 717 | ||
685 | if (armpmu) { | 718 | if (cpu_pmu) { |
686 | pr_info("enabled with %s PMU driver, %d counters available\n", | 719 | pr_info("enabled with %s PMU driver, %d counters available\n", |
687 | armpmu->name, armpmu->num_events); | 720 | cpu_pmu->name, cpu_pmu->num_events); |
721 | cpu_pmu_init(cpu_pmu); | ||
722 | armpmu_register(cpu_pmu, "cpu", PERF_TYPE_RAW); | ||
688 | } else { | 723 | } else { |
689 | pr_info("no hardware support available\n"); | 724 | pr_info("no hardware support available\n"); |
690 | } | 725 | } |
691 | 726 | ||
692 | perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW); | ||
693 | |||
694 | return 0; | 727 | return 0; |
695 | } | 728 | } |
696 | early_initcall(init_hw_perf_events); | 729 | early_initcall(init_hw_perf_events); |