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