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