Commit | Line | Data |
---|---|---|
1b8873a0 JI |
1 | #undef DEBUG |
2 | ||
3 | /* | |
4 | * ARM performance counter support. | |
5 | * | |
6 | * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles | |
43eab878 | 7 | * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com> |
796d1295 | 8 | * |
1b8873a0 JI |
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 | ||
1b8873a0 | 15 | #include <linux/kernel.h> |
49c006b9 | 16 | #include <linux/platform_device.h> |
7be2958e | 17 | #include <linux/pm_runtime.h> |
5505b206 | 18 | #include <linux/uaccess.h> |
1b8873a0 | 19 | |
1b8873a0 JI |
20 | #include <asm/irq_regs.h> |
21 | #include <asm/pmu.h> | |
22 | #include <asm/stacktrace.h> | |
23 | ||
1b8873a0 | 24 | static int |
e1f431b5 MR |
25 | armpmu_map_cache_event(const unsigned (*cache_map) |
26 | [PERF_COUNT_HW_CACHE_MAX] | |
27 | [PERF_COUNT_HW_CACHE_OP_MAX] | |
28 | [PERF_COUNT_HW_CACHE_RESULT_MAX], | |
29 | u64 config) | |
1b8873a0 JI |
30 | { |
31 | unsigned int cache_type, cache_op, cache_result, ret; | |
32 | ||
33 | cache_type = (config >> 0) & 0xff; | |
34 | if (cache_type >= PERF_COUNT_HW_CACHE_MAX) | |
35 | return -EINVAL; | |
36 | ||
37 | cache_op = (config >> 8) & 0xff; | |
38 | if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX) | |
39 | return -EINVAL; | |
40 | ||
41 | cache_result = (config >> 16) & 0xff; | |
42 | if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX) | |
43 | return -EINVAL; | |
44 | ||
e1f431b5 | 45 | ret = (int)(*cache_map)[cache_type][cache_op][cache_result]; |
1b8873a0 JI |
46 | |
47 | if (ret == CACHE_OP_UNSUPPORTED) | |
48 | return -ENOENT; | |
49 | ||
50 | return ret; | |
51 | } | |
52 | ||
84fee97a | 53 | static int |
6dbc0029 | 54 | armpmu_map_hw_event(const unsigned (*event_map)[PERF_COUNT_HW_MAX], u64 config) |
84fee97a | 55 | { |
e1f431b5 MR |
56 | int mapping = (*event_map)[config]; |
57 | return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping; | |
84fee97a WD |
58 | } |
59 | ||
60 | static int | |
e1f431b5 | 61 | armpmu_map_raw_event(u32 raw_event_mask, u64 config) |
84fee97a | 62 | { |
e1f431b5 MR |
63 | return (int)(config & raw_event_mask); |
64 | } | |
65 | ||
6dbc0029 WD |
66 | int |
67 | armpmu_map_event(struct perf_event *event, | |
68 | const unsigned (*event_map)[PERF_COUNT_HW_MAX], | |
69 | const unsigned (*cache_map) | |
70 | [PERF_COUNT_HW_CACHE_MAX] | |
71 | [PERF_COUNT_HW_CACHE_OP_MAX] | |
72 | [PERF_COUNT_HW_CACHE_RESULT_MAX], | |
73 | u32 raw_event_mask) | |
e1f431b5 MR |
74 | { |
75 | u64 config = event->attr.config; | |
76 | ||
77 | switch (event->attr.type) { | |
78 | case PERF_TYPE_HARDWARE: | |
6dbc0029 | 79 | return armpmu_map_hw_event(event_map, config); |
e1f431b5 MR |
80 | case PERF_TYPE_HW_CACHE: |
81 | return armpmu_map_cache_event(cache_map, config); | |
82 | case PERF_TYPE_RAW: | |
83 | return armpmu_map_raw_event(raw_event_mask, config); | |
84 | } | |
85 | ||
86 | return -ENOENT; | |
84fee97a WD |
87 | } |
88 | ||
ed6f2a52 | 89 | int armpmu_event_set_period(struct perf_event *event) |
1b8873a0 | 90 | { |
8a16b34e | 91 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
ed6f2a52 | 92 | struct hw_perf_event *hwc = &event->hw; |
e7850595 | 93 | s64 left = local64_read(&hwc->period_left); |
1b8873a0 JI |
94 | s64 period = hwc->sample_period; |
95 | int ret = 0; | |
96 | ||
3581fe0e WD |
97 | /* The period may have been changed by PERF_EVENT_IOC_PERIOD */ |
98 | if (unlikely(period != hwc->last_period)) | |
99 | left = period - (hwc->last_period - left); | |
100 | ||
1b8873a0 JI |
101 | if (unlikely(left <= -period)) { |
102 | left = period; | |
e7850595 | 103 | local64_set(&hwc->period_left, left); |
1b8873a0 JI |
104 | hwc->last_period = period; |
105 | ret = 1; | |
106 | } | |
107 | ||
108 | if (unlikely(left <= 0)) { | |
109 | left += period; | |
e7850595 | 110 | local64_set(&hwc->period_left, left); |
1b8873a0 JI |
111 | hwc->last_period = period; |
112 | ret = 1; | |
113 | } | |
114 | ||
115 | if (left > (s64)armpmu->max_period) | |
116 | left = armpmu->max_period; | |
117 | ||
e7850595 | 118 | local64_set(&hwc->prev_count, (u64)-left); |
1b8873a0 | 119 | |
ed6f2a52 | 120 | armpmu->write_counter(event, (u64)(-left) & 0xffffffff); |
1b8873a0 JI |
121 | |
122 | perf_event_update_userpage(event); | |
123 | ||
124 | return ret; | |
125 | } | |
126 | ||
ed6f2a52 | 127 | u64 armpmu_event_update(struct perf_event *event) |
1b8873a0 | 128 | { |
8a16b34e | 129 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
ed6f2a52 | 130 | struct hw_perf_event *hwc = &event->hw; |
a737823d | 131 | u64 delta, prev_raw_count, new_raw_count; |
1b8873a0 JI |
132 | |
133 | again: | |
e7850595 | 134 | prev_raw_count = local64_read(&hwc->prev_count); |
ed6f2a52 | 135 | new_raw_count = armpmu->read_counter(event); |
1b8873a0 | 136 | |
e7850595 | 137 | if (local64_cmpxchg(&hwc->prev_count, prev_raw_count, |
1b8873a0 JI |
138 | new_raw_count) != prev_raw_count) |
139 | goto again; | |
140 | ||
57273471 | 141 | delta = (new_raw_count - prev_raw_count) & armpmu->max_period; |
1b8873a0 | 142 | |
e7850595 PZ |
143 | local64_add(delta, &event->count); |
144 | local64_sub(delta, &hwc->period_left); | |
1b8873a0 JI |
145 | |
146 | return new_raw_count; | |
147 | } | |
148 | ||
149 | static void | |
a4eaf7f1 | 150 | armpmu_read(struct perf_event *event) |
1b8873a0 | 151 | { |
1b8873a0 | 152 | struct hw_perf_event *hwc = &event->hw; |
1b8873a0 | 153 | |
a4eaf7f1 PZ |
154 | /* Don't read disabled counters! */ |
155 | if (hwc->idx < 0) | |
156 | return; | |
1b8873a0 | 157 | |
ed6f2a52 | 158 | armpmu_event_update(event); |
1b8873a0 JI |
159 | } |
160 | ||
161 | static void | |
a4eaf7f1 | 162 | armpmu_stop(struct perf_event *event, int flags) |
1b8873a0 | 163 | { |
8a16b34e | 164 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
1b8873a0 JI |
165 | struct hw_perf_event *hwc = &event->hw; |
166 | ||
a4eaf7f1 PZ |
167 | /* |
168 | * ARM pmu always has to update the counter, so ignore | |
169 | * PERF_EF_UPDATE, see comments in armpmu_start(). | |
170 | */ | |
171 | if (!(hwc->state & PERF_HES_STOPPED)) { | |
ed6f2a52 SK |
172 | armpmu->disable(event); |
173 | armpmu_event_update(event); | |
a4eaf7f1 PZ |
174 | hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE; |
175 | } | |
1b8873a0 JI |
176 | } |
177 | ||
ed6f2a52 | 178 | static void armpmu_start(struct perf_event *event, int flags) |
1b8873a0 | 179 | { |
8a16b34e | 180 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
1b8873a0 JI |
181 | struct hw_perf_event *hwc = &event->hw; |
182 | ||
a4eaf7f1 PZ |
183 | /* |
184 | * ARM pmu always has to reprogram the period, so ignore | |
185 | * PERF_EF_RELOAD, see the comment below. | |
186 | */ | |
187 | if (flags & PERF_EF_RELOAD) | |
188 | WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE)); | |
189 | ||
190 | hwc->state = 0; | |
1b8873a0 JI |
191 | /* |
192 | * Set the period again. Some counters can't be stopped, so when we | |
a4eaf7f1 | 193 | * were stopped we simply disabled the IRQ source and the counter |
1b8873a0 JI |
194 | * may have been left counting. If we don't do this step then we may |
195 | * get an interrupt too soon or *way* too late if the overflow has | |
196 | * happened since disabling. | |
197 | */ | |
ed6f2a52 SK |
198 | armpmu_event_set_period(event); |
199 | armpmu->enable(event); | |
1b8873a0 JI |
200 | } |
201 | ||
a4eaf7f1 PZ |
202 | static void |
203 | armpmu_del(struct perf_event *event, int flags) | |
204 | { | |
8a16b34e | 205 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
8be3f9a2 | 206 | struct pmu_hw_events *hw_events = armpmu->get_hw_events(); |
a4eaf7f1 PZ |
207 | struct hw_perf_event *hwc = &event->hw; |
208 | int idx = hwc->idx; | |
209 | ||
210 | WARN_ON(idx < 0); | |
211 | ||
a4eaf7f1 | 212 | armpmu_stop(event, PERF_EF_UPDATE); |
8be3f9a2 MR |
213 | hw_events->events[idx] = NULL; |
214 | clear_bit(idx, hw_events->used_mask); | |
a4eaf7f1 PZ |
215 | |
216 | perf_event_update_userpage(event); | |
217 | } | |
218 | ||
1b8873a0 | 219 | static int |
a4eaf7f1 | 220 | armpmu_add(struct perf_event *event, int flags) |
1b8873a0 | 221 | { |
8a16b34e | 222 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
8be3f9a2 | 223 | struct pmu_hw_events *hw_events = armpmu->get_hw_events(); |
1b8873a0 JI |
224 | struct hw_perf_event *hwc = &event->hw; |
225 | int idx; | |
226 | int err = 0; | |
227 | ||
33696fc0 | 228 | perf_pmu_disable(event->pmu); |
24cd7f54 | 229 | |
1b8873a0 | 230 | /* If we don't have a space for the counter then finish early. */ |
ed6f2a52 | 231 | idx = armpmu->get_event_idx(hw_events, event); |
1b8873a0 JI |
232 | if (idx < 0) { |
233 | err = idx; | |
234 | goto out; | |
235 | } | |
236 | ||
237 | /* | |
238 | * If there is an event in the counter we are going to use then make | |
239 | * sure it is disabled. | |
240 | */ | |
241 | event->hw.idx = idx; | |
ed6f2a52 | 242 | armpmu->disable(event); |
8be3f9a2 | 243 | hw_events->events[idx] = event; |
1b8873a0 | 244 | |
a4eaf7f1 PZ |
245 | hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE; |
246 | if (flags & PERF_EF_START) | |
247 | armpmu_start(event, PERF_EF_RELOAD); | |
1b8873a0 JI |
248 | |
249 | /* Propagate our changes to the userspace mapping. */ | |
250 | perf_event_update_userpage(event); | |
251 | ||
252 | out: | |
33696fc0 | 253 | perf_pmu_enable(event->pmu); |
1b8873a0 JI |
254 | return err; |
255 | } | |
256 | ||
1b8873a0 | 257 | static int |
8be3f9a2 | 258 | validate_event(struct pmu_hw_events *hw_events, |
1b8873a0 JI |
259 | struct perf_event *event) |
260 | { | |
8a16b34e | 261 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
7b9f72c6 | 262 | struct pmu *leader_pmu = event->group_leader->pmu; |
1b8873a0 | 263 | |
7b9f72c6 | 264 | if (event->pmu != leader_pmu || event->state <= PERF_EVENT_STATE_OFF) |
65b4711f | 265 | return 1; |
1b8873a0 | 266 | |
ed6f2a52 | 267 | return armpmu->get_event_idx(hw_events, event) >= 0; |
1b8873a0 JI |
268 | } |
269 | ||
270 | static int | |
271 | validate_group(struct perf_event *event) | |
272 | { | |
273 | struct perf_event *sibling, *leader = event->group_leader; | |
8be3f9a2 | 274 | struct pmu_hw_events fake_pmu; |
bce34d14 | 275 | DECLARE_BITMAP(fake_used_mask, ARMPMU_MAX_HWEVENTS); |
1b8873a0 | 276 | |
bce34d14 WD |
277 | /* |
278 | * Initialise the fake PMU. We only need to populate the | |
279 | * used_mask for the purposes of validation. | |
280 | */ | |
281 | memset(fake_used_mask, 0, sizeof(fake_used_mask)); | |
282 | fake_pmu.used_mask = fake_used_mask; | |
1b8873a0 JI |
283 | |
284 | if (!validate_event(&fake_pmu, leader)) | |
aa2bc1ad | 285 | return -EINVAL; |
1b8873a0 JI |
286 | |
287 | list_for_each_entry(sibling, &leader->sibling_list, group_entry) { | |
288 | if (!validate_event(&fake_pmu, sibling)) | |
aa2bc1ad | 289 | return -EINVAL; |
1b8873a0 JI |
290 | } |
291 | ||
292 | if (!validate_event(&fake_pmu, event)) | |
aa2bc1ad | 293 | return -EINVAL; |
1b8873a0 JI |
294 | |
295 | return 0; | |
296 | } | |
297 | ||
051f1b13 | 298 | static irqreturn_t armpmu_dispatch_irq(int irq, void *dev) |
0e25a5c9 | 299 | { |
8a16b34e | 300 | struct arm_pmu *armpmu = (struct arm_pmu *) dev; |
a9356a04 MR |
301 | struct platform_device *plat_device = armpmu->plat_device; |
302 | struct arm_pmu_platdata *plat = dev_get_platdata(&plat_device->dev); | |
0e25a5c9 | 303 | |
051f1b13 SK |
304 | if (plat && plat->handle_irq) |
305 | return plat->handle_irq(irq, dev, armpmu->handle_irq); | |
306 | else | |
307 | return armpmu->handle_irq(irq, dev); | |
0e25a5c9 RV |
308 | } |
309 | ||
0b390e21 | 310 | static void |
8a16b34e | 311 | armpmu_release_hardware(struct arm_pmu *armpmu) |
0b390e21 | 312 | { |
ed6f2a52 | 313 | armpmu->free_irq(armpmu); |
051f1b13 | 314 | pm_runtime_put_sync(&armpmu->plat_device->dev); |
0b390e21 WD |
315 | } |
316 | ||
1b8873a0 | 317 | static int |
8a16b34e | 318 | armpmu_reserve_hardware(struct arm_pmu *armpmu) |
1b8873a0 | 319 | { |
051f1b13 | 320 | int err; |
a9356a04 | 321 | struct platform_device *pmu_device = armpmu->plat_device; |
1b8873a0 | 322 | |
e5a21327 WD |
323 | if (!pmu_device) |
324 | return -ENODEV; | |
325 | ||
7be2958e | 326 | pm_runtime_get_sync(&pmu_device->dev); |
ed6f2a52 | 327 | err = armpmu->request_irq(armpmu, armpmu_dispatch_irq); |
051f1b13 SK |
328 | if (err) { |
329 | armpmu_release_hardware(armpmu); | |
330 | return err; | |
49c006b9 | 331 | } |
1b8873a0 | 332 | |
0b390e21 | 333 | return 0; |
1b8873a0 JI |
334 | } |
335 | ||
1b8873a0 JI |
336 | static void |
337 | hw_perf_event_destroy(struct perf_event *event) | |
338 | { | |
8a16b34e | 339 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
03b7898d MR |
340 | atomic_t *active_events = &armpmu->active_events; |
341 | struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex; | |
342 | ||
343 | if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) { | |
8a16b34e | 344 | armpmu_release_hardware(armpmu); |
03b7898d | 345 | mutex_unlock(pmu_reserve_mutex); |
1b8873a0 JI |
346 | } |
347 | } | |
348 | ||
05d22fde WD |
349 | static int |
350 | event_requires_mode_exclusion(struct perf_event_attr *attr) | |
351 | { | |
352 | return attr->exclude_idle || attr->exclude_user || | |
353 | attr->exclude_kernel || attr->exclude_hv; | |
354 | } | |
355 | ||
1b8873a0 JI |
356 | static int |
357 | __hw_perf_event_init(struct perf_event *event) | |
358 | { | |
8a16b34e | 359 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
1b8873a0 JI |
360 | struct hw_perf_event *hwc = &event->hw; |
361 | int mapping, err; | |
362 | ||
e1f431b5 | 363 | mapping = armpmu->map_event(event); |
1b8873a0 JI |
364 | |
365 | if (mapping < 0) { | |
366 | pr_debug("event %x:%llx not supported\n", event->attr.type, | |
367 | event->attr.config); | |
368 | return mapping; | |
369 | } | |
370 | ||
05d22fde WD |
371 | /* |
372 | * We don't assign an index until we actually place the event onto | |
373 | * hardware. Use -1 to signify that we haven't decided where to put it | |
374 | * yet. For SMP systems, each core has it's own PMU so we can't do any | |
375 | * clever allocation or constraints checking at this point. | |
376 | */ | |
377 | hwc->idx = -1; | |
378 | hwc->config_base = 0; | |
379 | hwc->config = 0; | |
380 | hwc->event_base = 0; | |
381 | ||
1b8873a0 JI |
382 | /* |
383 | * Check whether we need to exclude the counter from certain modes. | |
1b8873a0 | 384 | */ |
05d22fde WD |
385 | if ((!armpmu->set_event_filter || |
386 | armpmu->set_event_filter(hwc, &event->attr)) && | |
387 | event_requires_mode_exclusion(&event->attr)) { | |
1b8873a0 JI |
388 | pr_debug("ARM performance counters do not support " |
389 | "mode exclusion\n"); | |
fdeb8e35 | 390 | return -EOPNOTSUPP; |
1b8873a0 JI |
391 | } |
392 | ||
393 | /* | |
05d22fde | 394 | * Store the event encoding into the config_base field. |
1b8873a0 | 395 | */ |
05d22fde | 396 | hwc->config_base |= (unsigned long)mapping; |
1b8873a0 JI |
397 | |
398 | if (!hwc->sample_period) { | |
57273471 WD |
399 | /* |
400 | * For non-sampling runs, limit the sample_period to half | |
401 | * of the counter width. That way, the new counter value | |
402 | * is far less likely to overtake the previous one unless | |
403 | * you have some serious IRQ latency issues. | |
404 | */ | |
405 | hwc->sample_period = armpmu->max_period >> 1; | |
1b8873a0 | 406 | hwc->last_period = hwc->sample_period; |
e7850595 | 407 | local64_set(&hwc->period_left, hwc->sample_period); |
1b8873a0 JI |
408 | } |
409 | ||
410 | err = 0; | |
411 | if (event->group_leader != event) { | |
412 | err = validate_group(event); | |
413 | if (err) | |
414 | return -EINVAL; | |
415 | } | |
416 | ||
417 | return err; | |
418 | } | |
419 | ||
b0a873eb | 420 | static int armpmu_event_init(struct perf_event *event) |
1b8873a0 | 421 | { |
8a16b34e | 422 | struct arm_pmu *armpmu = to_arm_pmu(event->pmu); |
1b8873a0 | 423 | int err = 0; |
03b7898d | 424 | atomic_t *active_events = &armpmu->active_events; |
1b8873a0 | 425 | |
2481c5fa SE |
426 | /* does not support taken branch sampling */ |
427 | if (has_branch_stack(event)) | |
428 | return -EOPNOTSUPP; | |
429 | ||
e1f431b5 | 430 | if (armpmu->map_event(event) == -ENOENT) |
b0a873eb | 431 | return -ENOENT; |
b0a873eb | 432 | |
1b8873a0 JI |
433 | event->destroy = hw_perf_event_destroy; |
434 | ||
03b7898d MR |
435 | if (!atomic_inc_not_zero(active_events)) { |
436 | mutex_lock(&armpmu->reserve_mutex); | |
437 | if (atomic_read(active_events) == 0) | |
8a16b34e | 438 | err = armpmu_reserve_hardware(armpmu); |
1b8873a0 JI |
439 | |
440 | if (!err) | |
03b7898d MR |
441 | atomic_inc(active_events); |
442 | mutex_unlock(&armpmu->reserve_mutex); | |
1b8873a0 JI |
443 | } |
444 | ||
445 | if (err) | |
b0a873eb | 446 | return err; |
1b8873a0 JI |
447 | |
448 | err = __hw_perf_event_init(event); | |
449 | if (err) | |
450 | hw_perf_event_destroy(event); | |
451 | ||
b0a873eb | 452 | return err; |
1b8873a0 JI |
453 | } |
454 | ||
a4eaf7f1 | 455 | static void armpmu_enable(struct pmu *pmu) |
1b8873a0 | 456 | { |
8be3f9a2 | 457 | struct arm_pmu *armpmu = to_arm_pmu(pmu); |
8be3f9a2 | 458 | struct pmu_hw_events *hw_events = armpmu->get_hw_events(); |
7325eaec | 459 | int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events); |
1b8873a0 | 460 | |
f4f38430 | 461 | if (enabled) |
ed6f2a52 | 462 | armpmu->start(armpmu); |
1b8873a0 JI |
463 | } |
464 | ||
a4eaf7f1 | 465 | static void armpmu_disable(struct pmu *pmu) |
1b8873a0 | 466 | { |
8a16b34e | 467 | struct arm_pmu *armpmu = to_arm_pmu(pmu); |
ed6f2a52 | 468 | armpmu->stop(armpmu); |
1b8873a0 JI |
469 | } |
470 | ||
7be2958e JH |
471 | #ifdef CONFIG_PM_RUNTIME |
472 | static int armpmu_runtime_resume(struct device *dev) | |
473 | { | |
474 | struct arm_pmu_platdata *plat = dev_get_platdata(dev); | |
475 | ||
476 | if (plat && plat->runtime_resume) | |
477 | return plat->runtime_resume(dev); | |
478 | ||
479 | return 0; | |
480 | } | |
481 | ||
482 | static int armpmu_runtime_suspend(struct device *dev) | |
483 | { | |
484 | struct arm_pmu_platdata *plat = dev_get_platdata(dev); | |
485 | ||
486 | if (plat && plat->runtime_suspend) | |
487 | return plat->runtime_suspend(dev); | |
488 | ||
489 | return 0; | |
490 | } | |
491 | #endif | |
492 | ||
6dbc0029 WD |
493 | const struct dev_pm_ops armpmu_dev_pm_ops = { |
494 | SET_RUNTIME_PM_OPS(armpmu_runtime_suspend, armpmu_runtime_resume, NULL) | |
495 | }; | |
496 | ||
03b7898d MR |
497 | static void __init armpmu_init(struct arm_pmu *armpmu) |
498 | { | |
499 | atomic_set(&armpmu->active_events, 0); | |
500 | mutex_init(&armpmu->reserve_mutex); | |
8a16b34e MR |
501 | |
502 | armpmu->pmu = (struct pmu) { | |
503 | .pmu_enable = armpmu_enable, | |
504 | .pmu_disable = armpmu_disable, | |
505 | .event_init = armpmu_event_init, | |
506 | .add = armpmu_add, | |
507 | .del = armpmu_del, | |
508 | .start = armpmu_start, | |
509 | .stop = armpmu_stop, | |
510 | .read = armpmu_read, | |
511 | }; | |
512 | } | |
513 | ||
04236f9f | 514 | int armpmu_register(struct arm_pmu *armpmu, char *name, int type) |
8a16b34e MR |
515 | { |
516 | armpmu_init(armpmu); | |
04236f9f WD |
517 | pr_info("enabled with %s PMU driver, %d counters available\n", |
518 | armpmu->name, armpmu->num_events); | |
8a16b34e | 519 | return perf_pmu_register(&armpmu->pmu, name, type); |
03b7898d MR |
520 | } |
521 | ||
1b8873a0 JI |
522 | /* |
523 | * Callchain handling code. | |
524 | */ | |
1b8873a0 JI |
525 | |
526 | /* | |
527 | * The registers we're interested in are at the end of the variable | |
528 | * length saved register structure. The fp points at the end of this | |
529 | * structure so the address of this struct is: | |
530 | * (struct frame_tail *)(xxx->fp)-1 | |
531 | * | |
532 | * This code has been adapted from the ARM OProfile support. | |
533 | */ | |
534 | struct frame_tail { | |
4d6b7a77 WD |
535 | struct frame_tail __user *fp; |
536 | unsigned long sp; | |
537 | unsigned long lr; | |
1b8873a0 JI |
538 | } __attribute__((packed)); |
539 | ||
540 | /* | |
541 | * Get the return address for a single stackframe and return a pointer to the | |
542 | * next frame tail. | |
543 | */ | |
4d6b7a77 WD |
544 | static struct frame_tail __user * |
545 | user_backtrace(struct frame_tail __user *tail, | |
1b8873a0 JI |
546 | struct perf_callchain_entry *entry) |
547 | { | |
548 | struct frame_tail buftail; | |
549 | ||
550 | /* Also check accessibility of one struct frame_tail beyond */ | |
551 | if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) | |
552 | return NULL; | |
553 | if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail))) | |
554 | return NULL; | |
555 | ||
70791ce9 | 556 | perf_callchain_store(entry, buftail.lr); |
1b8873a0 JI |
557 | |
558 | /* | |
559 | * Frame pointers should strictly progress back up the stack | |
560 | * (towards higher addresses). | |
561 | */ | |
cb06199b | 562 | if (tail + 1 >= buftail.fp) |
1b8873a0 JI |
563 | return NULL; |
564 | ||
565 | return buftail.fp - 1; | |
566 | } | |
567 | ||
56962b44 FW |
568 | void |
569 | perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs) | |
1b8873a0 | 570 | { |
4d6b7a77 | 571 | struct frame_tail __user *tail; |
1b8873a0 | 572 | |
e50c5418 MZ |
573 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) { |
574 | /* We don't support guest os callchain now */ | |
575 | return; | |
576 | } | |
1b8873a0 | 577 | |
4d6b7a77 | 578 | tail = (struct frame_tail __user *)regs->ARM_fp - 1; |
1b8873a0 | 579 | |
860ad782 SR |
580 | while ((entry->nr < PERF_MAX_STACK_DEPTH) && |
581 | tail && !((unsigned long)tail & 0x3)) | |
1b8873a0 JI |
582 | tail = user_backtrace(tail, entry); |
583 | } | |
584 | ||
585 | /* | |
586 | * Gets called by walk_stackframe() for every stackframe. This will be called | |
587 | * whist unwinding the stackframe and is like a subroutine return so we use | |
588 | * the PC. | |
589 | */ | |
590 | static int | |
591 | callchain_trace(struct stackframe *fr, | |
592 | void *data) | |
593 | { | |
594 | struct perf_callchain_entry *entry = data; | |
70791ce9 | 595 | perf_callchain_store(entry, fr->pc); |
1b8873a0 JI |
596 | return 0; |
597 | } | |
598 | ||
56962b44 FW |
599 | void |
600 | perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs) | |
1b8873a0 JI |
601 | { |
602 | struct stackframe fr; | |
603 | ||
e50c5418 MZ |
604 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) { |
605 | /* We don't support guest os callchain now */ | |
606 | return; | |
607 | } | |
608 | ||
1b8873a0 JI |
609 | fr.fp = regs->ARM_fp; |
610 | fr.sp = regs->ARM_sp; | |
611 | fr.lr = regs->ARM_lr; | |
612 | fr.pc = regs->ARM_pc; | |
613 | walk_stackframe(&fr, callchain_trace, entry); | |
614 | } | |
e50c5418 MZ |
615 | |
616 | unsigned long perf_instruction_pointer(struct pt_regs *regs) | |
617 | { | |
618 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) | |
619 | return perf_guest_cbs->get_guest_ip(); | |
620 | ||
621 | return instruction_pointer(regs); | |
622 | } | |
623 | ||
624 | unsigned long perf_misc_flags(struct pt_regs *regs) | |
625 | { | |
626 | int misc = 0; | |
627 | ||
628 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) { | |
629 | if (perf_guest_cbs->is_user_mode()) | |
630 | misc |= PERF_RECORD_MISC_GUEST_USER; | |
631 | else | |
632 | misc |= PERF_RECORD_MISC_GUEST_KERNEL; | |
633 | } else { | |
634 | if (user_mode(regs)) | |
635 | misc |= PERF_RECORD_MISC_USER; | |
636 | else | |
637 | misc |= PERF_RECORD_MISC_KERNEL; | |
638 | } | |
639 | ||
640 | return misc; | |
641 | } |