]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/perf/arm_pmu.c
077be952d64cab75811d4432ad8781abc08949f0
[mirror_ubuntu-artful-kernel.git] / drivers / perf / arm_pmu.c
1 #undef DEBUG
2
3 /*
4 * ARM performance counter support.
5 *
6 * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
7 * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
8 *
9 * This code is based on the sparc64 perf event code, which is in turn based
10 * on the x86 code.
11 */
12 #define pr_fmt(fmt) "hw perfevents: " fmt
13
14 #include <linux/bitmap.h>
15 #include <linux/cpumask.h>
16 #include <linux/cpu_pm.h>
17 #include <linux/export.h>
18 #include <linux/kernel.h>
19 #include <linux/of_device.h>
20 #include <linux/perf/arm_pmu.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/spinlock.h>
24 #include <linux/irq.h>
25 #include <linux/irqdesc.h>
26
27 #include <asm/cputype.h>
28 #include <asm/irq_regs.h>
29
30 static int
31 armpmu_map_cache_event(const unsigned (*cache_map)
32 [PERF_COUNT_HW_CACHE_MAX]
33 [PERF_COUNT_HW_CACHE_OP_MAX]
34 [PERF_COUNT_HW_CACHE_RESULT_MAX],
35 u64 config)
36 {
37 unsigned int cache_type, cache_op, cache_result, ret;
38
39 cache_type = (config >> 0) & 0xff;
40 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
41 return -EINVAL;
42
43 cache_op = (config >> 8) & 0xff;
44 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
45 return -EINVAL;
46
47 cache_result = (config >> 16) & 0xff;
48 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
49 return -EINVAL;
50
51 ret = (int)(*cache_map)[cache_type][cache_op][cache_result];
52
53 if (ret == CACHE_OP_UNSUPPORTED)
54 return -ENOENT;
55
56 return ret;
57 }
58
59 static int
60 armpmu_map_hw_event(const unsigned (*event_map)[PERF_COUNT_HW_MAX], u64 config)
61 {
62 int mapping;
63
64 if (config >= PERF_COUNT_HW_MAX)
65 return -EINVAL;
66
67 mapping = (*event_map)[config];
68 return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping;
69 }
70
71 static int
72 armpmu_map_raw_event(u32 raw_event_mask, u64 config)
73 {
74 return (int)(config & raw_event_mask);
75 }
76
77 int
78 armpmu_map_event(struct perf_event *event,
79 const unsigned (*event_map)[PERF_COUNT_HW_MAX],
80 const unsigned (*cache_map)
81 [PERF_COUNT_HW_CACHE_MAX]
82 [PERF_COUNT_HW_CACHE_OP_MAX]
83 [PERF_COUNT_HW_CACHE_RESULT_MAX],
84 u32 raw_event_mask)
85 {
86 u64 config = event->attr.config;
87 int type = event->attr.type;
88
89 if (type == event->pmu->type)
90 return armpmu_map_raw_event(raw_event_mask, config);
91
92 switch (type) {
93 case PERF_TYPE_HARDWARE:
94 return armpmu_map_hw_event(event_map, config);
95 case PERF_TYPE_HW_CACHE:
96 return armpmu_map_cache_event(cache_map, config);
97 case PERF_TYPE_RAW:
98 return armpmu_map_raw_event(raw_event_mask, config);
99 }
100
101 return -ENOENT;
102 }
103
104 int armpmu_event_set_period(struct perf_event *event)
105 {
106 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
107 struct hw_perf_event *hwc = &event->hw;
108 s64 left = local64_read(&hwc->period_left);
109 s64 period = hwc->sample_period;
110 int ret = 0;
111
112 if (unlikely(left <= -period)) {
113 left = period;
114 local64_set(&hwc->period_left, left);
115 hwc->last_period = period;
116 ret = 1;
117 }
118
119 if (unlikely(left <= 0)) {
120 left += period;
121 local64_set(&hwc->period_left, left);
122 hwc->last_period = period;
123 ret = 1;
124 }
125
126 /*
127 * Limit the maximum period to prevent the counter value
128 * from overtaking the one we are about to program. In
129 * effect we are reducing max_period to account for
130 * interrupt latency (and we are being very conservative).
131 */
132 if (left > (armpmu->max_period >> 1))
133 left = armpmu->max_period >> 1;
134
135 local64_set(&hwc->prev_count, (u64)-left);
136
137 armpmu->write_counter(event, (u64)(-left) & 0xffffffff);
138
139 perf_event_update_userpage(event);
140
141 return ret;
142 }
143
144 u64 armpmu_event_update(struct perf_event *event)
145 {
146 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
147 struct hw_perf_event *hwc = &event->hw;
148 u64 delta, prev_raw_count, new_raw_count;
149
150 again:
151 prev_raw_count = local64_read(&hwc->prev_count);
152 new_raw_count = armpmu->read_counter(event);
153
154 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
155 new_raw_count) != prev_raw_count)
156 goto again;
157
158 delta = (new_raw_count - prev_raw_count) & armpmu->max_period;
159
160 local64_add(delta, &event->count);
161 local64_sub(delta, &hwc->period_left);
162
163 return new_raw_count;
164 }
165
166 static void
167 armpmu_read(struct perf_event *event)
168 {
169 armpmu_event_update(event);
170 }
171
172 static void
173 armpmu_stop(struct perf_event *event, int flags)
174 {
175 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
176 struct hw_perf_event *hwc = &event->hw;
177
178 /*
179 * ARM pmu always has to update the counter, so ignore
180 * PERF_EF_UPDATE, see comments in armpmu_start().
181 */
182 if (!(hwc->state & PERF_HES_STOPPED)) {
183 armpmu->disable(event);
184 armpmu_event_update(event);
185 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
186 }
187 }
188
189 static void armpmu_start(struct perf_event *event, int flags)
190 {
191 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
192 struct hw_perf_event *hwc = &event->hw;
193
194 /*
195 * ARM pmu always has to reprogram the period, so ignore
196 * PERF_EF_RELOAD, see the comment below.
197 */
198 if (flags & PERF_EF_RELOAD)
199 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
200
201 hwc->state = 0;
202 /*
203 * Set the period again. Some counters can't be stopped, so when we
204 * were stopped we simply disabled the IRQ source and the counter
205 * may have been left counting. If we don't do this step then we may
206 * get an interrupt too soon or *way* too late if the overflow has
207 * happened since disabling.
208 */
209 armpmu_event_set_period(event);
210 armpmu->enable(event);
211 }
212
213 static void
214 armpmu_del(struct perf_event *event, int flags)
215 {
216 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
217 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
218 struct hw_perf_event *hwc = &event->hw;
219 int idx = hwc->idx;
220
221 armpmu_stop(event, PERF_EF_UPDATE);
222 hw_events->events[idx] = NULL;
223 clear_bit(idx, hw_events->used_mask);
224 if (armpmu->clear_event_idx)
225 armpmu->clear_event_idx(hw_events, event);
226
227 perf_event_update_userpage(event);
228 }
229
230 static int
231 armpmu_add(struct perf_event *event, int flags)
232 {
233 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
234 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
235 struct hw_perf_event *hwc = &event->hw;
236 int idx;
237
238 /* An event following a process won't be stopped earlier */
239 if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
240 return -ENOENT;
241
242 /* If we don't have a space for the counter then finish early. */
243 idx = armpmu->get_event_idx(hw_events, event);
244 if (idx < 0)
245 return idx;
246
247 /*
248 * If there is an event in the counter we are going to use then make
249 * sure it is disabled.
250 */
251 event->hw.idx = idx;
252 armpmu->disable(event);
253 hw_events->events[idx] = event;
254
255 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
256 if (flags & PERF_EF_START)
257 armpmu_start(event, PERF_EF_RELOAD);
258
259 /* Propagate our changes to the userspace mapping. */
260 perf_event_update_userpage(event);
261
262 return 0;
263 }
264
265 static int
266 validate_event(struct pmu *pmu, struct pmu_hw_events *hw_events,
267 struct perf_event *event)
268 {
269 struct arm_pmu *armpmu;
270
271 if (is_software_event(event))
272 return 1;
273
274 /*
275 * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
276 * core perf code won't check that the pmu->ctx == leader->ctx
277 * until after pmu->event_init(event).
278 */
279 if (event->pmu != pmu)
280 return 0;
281
282 if (event->state < PERF_EVENT_STATE_OFF)
283 return 1;
284
285 if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
286 return 1;
287
288 armpmu = to_arm_pmu(event->pmu);
289 return armpmu->get_event_idx(hw_events, event) >= 0;
290 }
291
292 static int
293 validate_group(struct perf_event *event)
294 {
295 struct perf_event *sibling, *leader = event->group_leader;
296 struct pmu_hw_events fake_pmu;
297
298 /*
299 * Initialise the fake PMU. We only need to populate the
300 * used_mask for the purposes of validation.
301 */
302 memset(&fake_pmu.used_mask, 0, sizeof(fake_pmu.used_mask));
303
304 if (!validate_event(event->pmu, &fake_pmu, leader))
305 return -EINVAL;
306
307 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
308 if (!validate_event(event->pmu, &fake_pmu, sibling))
309 return -EINVAL;
310 }
311
312 if (!validate_event(event->pmu, &fake_pmu, event))
313 return -EINVAL;
314
315 return 0;
316 }
317
318 static struct arm_pmu_platdata *armpmu_get_platdata(struct arm_pmu *armpmu)
319 {
320 struct platform_device *pdev = armpmu->plat_device;
321
322 return pdev ? dev_get_platdata(&pdev->dev) : NULL;
323 }
324
325 static irqreturn_t armpmu_dispatch_irq(int irq, void *dev)
326 {
327 struct arm_pmu *armpmu;
328 struct arm_pmu_platdata *plat;
329 int ret;
330 u64 start_clock, finish_clock;
331
332 /*
333 * we request the IRQ with a (possibly percpu) struct arm_pmu**, but
334 * the handlers expect a struct arm_pmu*. The percpu_irq framework will
335 * do any necessary shifting, we just need to perform the first
336 * dereference.
337 */
338 armpmu = *(void **)dev;
339
340 plat = armpmu_get_platdata(armpmu);
341
342 start_clock = sched_clock();
343 if (plat && plat->handle_irq)
344 ret = plat->handle_irq(irq, armpmu, armpmu->handle_irq);
345 else
346 ret = armpmu->handle_irq(irq, armpmu);
347 finish_clock = sched_clock();
348
349 perf_sample_event_took(finish_clock - start_clock);
350 return ret;
351 }
352
353 static int
354 event_requires_mode_exclusion(struct perf_event_attr *attr)
355 {
356 return attr->exclude_idle || attr->exclude_user ||
357 attr->exclude_kernel || attr->exclude_hv;
358 }
359
360 static int
361 __hw_perf_event_init(struct perf_event *event)
362 {
363 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
364 struct hw_perf_event *hwc = &event->hw;
365 int mapping;
366
367 mapping = armpmu->map_event(event);
368
369 if (mapping < 0) {
370 pr_debug("event %x:%llx not supported\n", event->attr.type,
371 event->attr.config);
372 return mapping;
373 }
374
375 /*
376 * We don't assign an index until we actually place the event onto
377 * hardware. Use -1 to signify that we haven't decided where to put it
378 * yet. For SMP systems, each core has it's own PMU so we can't do any
379 * clever allocation or constraints checking at this point.
380 */
381 hwc->idx = -1;
382 hwc->config_base = 0;
383 hwc->config = 0;
384 hwc->event_base = 0;
385
386 /*
387 * Check whether we need to exclude the counter from certain modes.
388 */
389 if ((!armpmu->set_event_filter ||
390 armpmu->set_event_filter(hwc, &event->attr)) &&
391 event_requires_mode_exclusion(&event->attr)) {
392 pr_debug("ARM performance counters do not support "
393 "mode exclusion\n");
394 return -EOPNOTSUPP;
395 }
396
397 /*
398 * Store the event encoding into the config_base field.
399 */
400 hwc->config_base |= (unsigned long)mapping;
401
402 if (!is_sampling_event(event)) {
403 /*
404 * For non-sampling runs, limit the sample_period to half
405 * of the counter width. That way, the new counter value
406 * is far less likely to overtake the previous one unless
407 * you have some serious IRQ latency issues.
408 */
409 hwc->sample_period = armpmu->max_period >> 1;
410 hwc->last_period = hwc->sample_period;
411 local64_set(&hwc->period_left, hwc->sample_period);
412 }
413
414 if (event->group_leader != event) {
415 if (validate_group(event) != 0)
416 return -EINVAL;
417 }
418
419 return 0;
420 }
421
422 static int armpmu_event_init(struct perf_event *event)
423 {
424 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
425
426 /*
427 * Reject CPU-affine events for CPUs that are of a different class to
428 * that which this PMU handles. Process-following events (where
429 * event->cpu == -1) can be migrated between CPUs, and thus we have to
430 * reject them later (in armpmu_add) if they're scheduled on a
431 * different class of CPU.
432 */
433 if (event->cpu != -1 &&
434 !cpumask_test_cpu(event->cpu, &armpmu->supported_cpus))
435 return -ENOENT;
436
437 /* does not support taken branch sampling */
438 if (has_branch_stack(event))
439 return -EOPNOTSUPP;
440
441 if (armpmu->map_event(event) == -ENOENT)
442 return -ENOENT;
443
444 return __hw_perf_event_init(event);
445 }
446
447 static void armpmu_enable(struct pmu *pmu)
448 {
449 struct arm_pmu *armpmu = to_arm_pmu(pmu);
450 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
451 int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
452
453 /* For task-bound events we may be called on other CPUs */
454 if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
455 return;
456
457 if (enabled)
458 armpmu->start(armpmu);
459 }
460
461 static void armpmu_disable(struct pmu *pmu)
462 {
463 struct arm_pmu *armpmu = to_arm_pmu(pmu);
464
465 /* For task-bound events we may be called on other CPUs */
466 if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
467 return;
468
469 armpmu->stop(armpmu);
470 }
471
472 /*
473 * In heterogeneous systems, events are specific to a particular
474 * microarchitecture, and aren't suitable for another. Thus, only match CPUs of
475 * the same microarchitecture.
476 */
477 static int armpmu_filter_match(struct perf_event *event)
478 {
479 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
480 unsigned int cpu = smp_processor_id();
481 return cpumask_test_cpu(cpu, &armpmu->supported_cpus);
482 }
483
484 static ssize_t armpmu_cpumask_show(struct device *dev,
485 struct device_attribute *attr, char *buf)
486 {
487 struct arm_pmu *armpmu = to_arm_pmu(dev_get_drvdata(dev));
488 return cpumap_print_to_pagebuf(true, buf, &armpmu->supported_cpus);
489 }
490
491 static DEVICE_ATTR(cpus, S_IRUGO, armpmu_cpumask_show, NULL);
492
493 static struct attribute *armpmu_common_attrs[] = {
494 &dev_attr_cpus.attr,
495 NULL,
496 };
497
498 static struct attribute_group armpmu_common_attr_group = {
499 .attrs = armpmu_common_attrs,
500 };
501
502 /* Set at runtime when we know what CPU type we are. */
503 static struct arm_pmu *__oprofile_cpu_pmu;
504
505 /*
506 * Despite the names, these two functions are CPU-specific and are used
507 * by the OProfile/perf code.
508 */
509 const char *perf_pmu_name(void)
510 {
511 if (!__oprofile_cpu_pmu)
512 return NULL;
513
514 return __oprofile_cpu_pmu->name;
515 }
516 EXPORT_SYMBOL_GPL(perf_pmu_name);
517
518 int perf_num_counters(void)
519 {
520 int max_events = 0;
521
522 if (__oprofile_cpu_pmu != NULL)
523 max_events = __oprofile_cpu_pmu->num_events;
524
525 return max_events;
526 }
527 EXPORT_SYMBOL_GPL(perf_num_counters);
528
529 static void armpmu_free_irq(struct arm_pmu *armpmu, int cpu)
530 {
531 struct pmu_hw_events __percpu *hw_events = armpmu->hw_events;
532 int irq = per_cpu(hw_events->irq, cpu);
533
534 if (!cpumask_test_and_clear_cpu(cpu, &armpmu->active_irqs))
535 return;
536
537 if (irq_is_percpu(irq)) {
538 free_percpu_irq(irq, &hw_events->percpu_pmu);
539 cpumask_clear(&armpmu->active_irqs);
540 return;
541 }
542
543 free_irq(irq, per_cpu_ptr(&hw_events->percpu_pmu, cpu));
544 }
545
546 static void armpmu_free_irqs(struct arm_pmu *armpmu)
547 {
548 int cpu;
549
550 for_each_cpu(cpu, &armpmu->supported_cpus)
551 armpmu_free_irq(armpmu, cpu);
552 }
553
554 static int armpmu_request_irq(struct arm_pmu *armpmu, int cpu)
555 {
556 int err = 0;
557 struct pmu_hw_events __percpu *hw_events = armpmu->hw_events;
558 const irq_handler_t handler = armpmu_dispatch_irq;
559 int irq = per_cpu(hw_events->irq, cpu);
560 if (!irq)
561 return 0;
562
563 if (irq_is_percpu(irq) && cpumask_empty(&armpmu->active_irqs)) {
564 err = request_percpu_irq(irq, handler, "arm-pmu",
565 &hw_events->percpu_pmu);
566 } else if (irq_is_percpu(irq)) {
567 int other_cpu = cpumask_first(&armpmu->active_irqs);
568 int other_irq = per_cpu(hw_events->irq, other_cpu);
569
570 if (irq != other_irq) {
571 pr_warn("mismatched PPIs detected.\n");
572 err = -EINVAL;
573 }
574 } else {
575 err = request_irq(irq, handler,
576 IRQF_NOBALANCING | IRQF_NO_THREAD, "arm-pmu",
577 per_cpu_ptr(&hw_events->percpu_pmu, cpu));
578 }
579
580 if (err) {
581 pr_err("unable to request IRQ%d for ARM PMU counters\n",
582 irq);
583 return err;
584 }
585
586 cpumask_set_cpu(cpu, &armpmu->active_irqs);
587
588 return 0;
589 }
590
591 static int armpmu_request_irqs(struct arm_pmu *armpmu)
592 {
593 int cpu, err;
594
595 for_each_cpu(cpu, &armpmu->supported_cpus) {
596 err = armpmu_request_irq(armpmu, cpu);
597 if (err)
598 break;
599 }
600
601 return err;
602 }
603
604 static int armpmu_get_cpu_irq(struct arm_pmu *pmu, int cpu)
605 {
606 struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
607 return per_cpu(hw_events->irq, cpu);
608 }
609
610 /*
611 * PMU hardware loses all context when a CPU goes offline.
612 * When a CPU is hotplugged back in, since some hardware registers are
613 * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
614 * junk values out of them.
615 */
616 static int arm_perf_starting_cpu(unsigned int cpu, struct hlist_node *node)
617 {
618 struct arm_pmu *pmu = hlist_entry_safe(node, struct arm_pmu, node);
619 int irq;
620
621 if (!cpumask_test_cpu(cpu, &pmu->supported_cpus))
622 return 0;
623 if (pmu->reset)
624 pmu->reset(pmu);
625
626 irq = armpmu_get_cpu_irq(pmu, cpu);
627 if (irq) {
628 if (irq_is_percpu(irq)) {
629 enable_percpu_irq(irq, IRQ_TYPE_NONE);
630 return 0;
631 }
632
633 if (irq_force_affinity(irq, cpumask_of(cpu)) &&
634 num_possible_cpus() > 1) {
635 pr_warn("unable to set irq affinity (irq=%d, cpu=%u)\n",
636 irq, cpu);
637 }
638 }
639
640 return 0;
641 }
642
643 static int arm_perf_teardown_cpu(unsigned int cpu, struct hlist_node *node)
644 {
645 struct arm_pmu *pmu = hlist_entry_safe(node, struct arm_pmu, node);
646 int irq;
647
648 if (!cpumask_test_cpu(cpu, &pmu->supported_cpus))
649 return 0;
650
651 irq = armpmu_get_cpu_irq(pmu, cpu);
652 if (irq && irq_is_percpu(irq))
653 disable_percpu_irq(irq);
654
655 return 0;
656 }
657
658 #ifdef CONFIG_CPU_PM
659 static void cpu_pm_pmu_setup(struct arm_pmu *armpmu, unsigned long cmd)
660 {
661 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
662 struct perf_event *event;
663 int idx;
664
665 for (idx = 0; idx < armpmu->num_events; idx++) {
666 /*
667 * If the counter is not used skip it, there is no
668 * need of stopping/restarting it.
669 */
670 if (!test_bit(idx, hw_events->used_mask))
671 continue;
672
673 event = hw_events->events[idx];
674
675 switch (cmd) {
676 case CPU_PM_ENTER:
677 /*
678 * Stop and update the counter
679 */
680 armpmu_stop(event, PERF_EF_UPDATE);
681 break;
682 case CPU_PM_EXIT:
683 case CPU_PM_ENTER_FAILED:
684 /*
685 * Restore and enable the counter.
686 * armpmu_start() indirectly calls
687 *
688 * perf_event_update_userpage()
689 *
690 * that requires RCU read locking to be functional,
691 * wrap the call within RCU_NONIDLE to make the
692 * RCU subsystem aware this cpu is not idle from
693 * an RCU perspective for the armpmu_start() call
694 * duration.
695 */
696 RCU_NONIDLE(armpmu_start(event, PERF_EF_RELOAD));
697 break;
698 default:
699 break;
700 }
701 }
702 }
703
704 static int cpu_pm_pmu_notify(struct notifier_block *b, unsigned long cmd,
705 void *v)
706 {
707 struct arm_pmu *armpmu = container_of(b, struct arm_pmu, cpu_pm_nb);
708 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
709 int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
710
711 if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
712 return NOTIFY_DONE;
713
714 /*
715 * Always reset the PMU registers on power-up even if
716 * there are no events running.
717 */
718 if (cmd == CPU_PM_EXIT && armpmu->reset)
719 armpmu->reset(armpmu);
720
721 if (!enabled)
722 return NOTIFY_OK;
723
724 switch (cmd) {
725 case CPU_PM_ENTER:
726 armpmu->stop(armpmu);
727 cpu_pm_pmu_setup(armpmu, cmd);
728 break;
729 case CPU_PM_EXIT:
730 cpu_pm_pmu_setup(armpmu, cmd);
731 case CPU_PM_ENTER_FAILED:
732 armpmu->start(armpmu);
733 break;
734 default:
735 return NOTIFY_DONE;
736 }
737
738 return NOTIFY_OK;
739 }
740
741 static int cpu_pm_pmu_register(struct arm_pmu *cpu_pmu)
742 {
743 cpu_pmu->cpu_pm_nb.notifier_call = cpu_pm_pmu_notify;
744 return cpu_pm_register_notifier(&cpu_pmu->cpu_pm_nb);
745 }
746
747 static void cpu_pm_pmu_unregister(struct arm_pmu *cpu_pmu)
748 {
749 cpu_pm_unregister_notifier(&cpu_pmu->cpu_pm_nb);
750 }
751 #else
752 static inline int cpu_pm_pmu_register(struct arm_pmu *cpu_pmu) { return 0; }
753 static inline void cpu_pm_pmu_unregister(struct arm_pmu *cpu_pmu) { }
754 #endif
755
756 static int cpu_pmu_init(struct arm_pmu *cpu_pmu)
757 {
758 int err;
759
760 err = armpmu_request_irqs(cpu_pmu);
761 if (err)
762 goto out;
763
764 err = cpuhp_state_add_instance(CPUHP_AP_PERF_ARM_STARTING,
765 &cpu_pmu->node);
766 if (err)
767 goto out;
768
769 err = cpu_pm_pmu_register(cpu_pmu);
770 if (err)
771 goto out_unregister;
772
773 return 0;
774
775 out_unregister:
776 cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_STARTING,
777 &cpu_pmu->node);
778 out:
779 armpmu_free_irqs(cpu_pmu);
780 return err;
781 }
782
783 static void cpu_pmu_destroy(struct arm_pmu *cpu_pmu)
784 {
785 cpu_pm_pmu_unregister(cpu_pmu);
786 cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_STARTING,
787 &cpu_pmu->node);
788 }
789
790 /*
791 * CPU PMU identification and probing.
792 */
793 static int probe_current_pmu(struct arm_pmu *pmu,
794 const struct pmu_probe_info *info)
795 {
796 int cpu = get_cpu();
797 unsigned int cpuid = read_cpuid_id();
798 int ret = -ENODEV;
799
800 pr_info("probing PMU on CPU %d\n", cpu);
801
802 for (; info->init != NULL; info++) {
803 if ((cpuid & info->mask) != info->cpuid)
804 continue;
805 ret = info->init(pmu);
806 break;
807 }
808
809 put_cpu();
810 return ret;
811 }
812
813 static int pmu_parse_percpu_irq(struct arm_pmu *pmu, int irq)
814 {
815 int cpu, ret;
816 struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
817
818 ret = irq_get_percpu_devid_partition(irq, &pmu->supported_cpus);
819 if (ret)
820 return ret;
821
822 for_each_cpu(cpu, &pmu->supported_cpus)
823 per_cpu(hw_events->irq, cpu) = irq;
824
825 return 0;
826 }
827
828 static bool pmu_has_irq_affinity(struct device_node *node)
829 {
830 return !!of_find_property(node, "interrupt-affinity", NULL);
831 }
832
833 static int pmu_parse_irq_affinity(struct device_node *node, int i)
834 {
835 struct device_node *dn;
836 int cpu;
837
838 /*
839 * If we don't have an interrupt-affinity property, we guess irq
840 * affinity matches our logical CPU order, as we used to assume.
841 * This is fragile, so we'll warn in pmu_parse_irqs().
842 */
843 if (!pmu_has_irq_affinity(node))
844 return i;
845
846 dn = of_parse_phandle(node, "interrupt-affinity", i);
847 if (!dn) {
848 pr_warn("failed to parse interrupt-affinity[%d] for %s\n",
849 i, node->name);
850 return -EINVAL;
851 }
852
853 /* Now look up the logical CPU number */
854 for_each_possible_cpu(cpu) {
855 struct device_node *cpu_dn;
856
857 cpu_dn = of_cpu_device_node_get(cpu);
858 of_node_put(cpu_dn);
859
860 if (dn == cpu_dn)
861 break;
862 }
863
864 if (cpu >= nr_cpu_ids) {
865 pr_warn("failed to find logical CPU for %s\n", dn->name);
866 }
867
868 of_node_put(dn);
869
870 return cpu;
871 }
872
873 static int pmu_parse_irqs(struct arm_pmu *pmu)
874 {
875 int i = 0, irqs;
876 struct platform_device *pdev = pmu->plat_device;
877 struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
878
879 irqs = platform_irq_count(pdev);
880 if (irqs < 0) {
881 pr_err("unable to count PMU IRQs\n");
882 return irqs;
883 }
884
885 /*
886 * In this case we have no idea which CPUs are covered by the PMU.
887 * To match our prior behaviour, we assume all CPUs in this case.
888 */
889 if (irqs == 0) {
890 pr_warn("no irqs for PMU, sampling events not supported\n");
891 pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
892 cpumask_setall(&pmu->supported_cpus);
893 return 0;
894 }
895
896 if (irqs == 1) {
897 int irq = platform_get_irq(pdev, 0);
898 if (irq && irq_is_percpu(irq))
899 return pmu_parse_percpu_irq(pmu, irq);
900 }
901
902 if (!pmu_has_irq_affinity(pdev->dev.of_node)) {
903 pr_warn("no interrupt-affinity property for %s, guessing.\n",
904 of_node_full_name(pdev->dev.of_node));
905 }
906
907 /*
908 * Some platforms have all PMU IRQs OR'd into a single IRQ, with a
909 * special platdata function that attempts to demux them.
910 */
911 if (dev_get_platdata(&pdev->dev))
912 cpumask_setall(&pmu->supported_cpus);
913
914 for (i = 0; i < irqs; i++) {
915 int cpu, irq;
916
917 irq = platform_get_irq(pdev, i);
918 if (WARN_ON(irq <= 0))
919 continue;
920
921 if (irq_is_percpu(irq)) {
922 pr_warn("multiple PPIs or mismatched SPI/PPI detected\n");
923 return -EINVAL;
924 }
925
926 cpu = pmu_parse_irq_affinity(pdev->dev.of_node, i);
927 if (cpu < 0)
928 return cpu;
929 if (cpu >= nr_cpu_ids)
930 continue;
931
932 if (per_cpu(hw_events->irq, cpu)) {
933 pr_warn("multiple PMU IRQs for the same CPU detected\n");
934 return -EINVAL;
935 }
936
937 per_cpu(hw_events->irq, cpu) = irq;
938 cpumask_set_cpu(cpu, &pmu->supported_cpus);
939 }
940
941 return 0;
942 }
943
944 static struct arm_pmu *armpmu_alloc(void)
945 {
946 struct arm_pmu *pmu;
947 int cpu;
948
949 pmu = kzalloc(sizeof(*pmu), GFP_KERNEL);
950 if (!pmu) {
951 pr_info("failed to allocate PMU device!\n");
952 goto out;
953 }
954
955 pmu->hw_events = alloc_percpu(struct pmu_hw_events);
956 if (!pmu->hw_events) {
957 pr_info("failed to allocate per-cpu PMU data.\n");
958 goto out_free_pmu;
959 }
960
961 pmu->pmu = (struct pmu) {
962 .pmu_enable = armpmu_enable,
963 .pmu_disable = armpmu_disable,
964 .event_init = armpmu_event_init,
965 .add = armpmu_add,
966 .del = armpmu_del,
967 .start = armpmu_start,
968 .stop = armpmu_stop,
969 .read = armpmu_read,
970 .filter_match = armpmu_filter_match,
971 .attr_groups = pmu->attr_groups,
972 /*
973 * This is a CPU PMU potentially in a heterogeneous
974 * configuration (e.g. big.LITTLE). This is not an uncore PMU,
975 * and we have taken ctx sharing into account (e.g. with our
976 * pmu::filter_match callback and pmu::event_init group
977 * validation).
978 */
979 .capabilities = PERF_PMU_CAP_HETEROGENEOUS_CPUS,
980 };
981
982 pmu->attr_groups[ARMPMU_ATTR_GROUP_COMMON] =
983 &armpmu_common_attr_group;
984
985 for_each_possible_cpu(cpu) {
986 struct pmu_hw_events *events;
987
988 events = per_cpu_ptr(pmu->hw_events, cpu);
989 raw_spin_lock_init(&events->pmu_lock);
990 events->percpu_pmu = pmu;
991 }
992
993 return pmu;
994
995 out_free_pmu:
996 kfree(pmu);
997 out:
998 return NULL;
999 }
1000
1001 static void armpmu_free(struct arm_pmu *pmu)
1002 {
1003 free_percpu(pmu->hw_events);
1004 kfree(pmu);
1005 }
1006
1007 int armpmu_register(struct arm_pmu *pmu)
1008 {
1009 int ret;
1010
1011 ret = cpu_pmu_init(pmu);
1012 if (ret)
1013 return ret;
1014
1015 ret = perf_pmu_register(&pmu->pmu, pmu->name, -1);
1016 if (ret)
1017 goto out_destroy;
1018
1019 if (!__oprofile_cpu_pmu)
1020 __oprofile_cpu_pmu = pmu;
1021
1022 pr_info("enabled with %s PMU driver, %d counters available\n",
1023 pmu->name, pmu->num_events);
1024
1025 return 0;
1026
1027 out_destroy:
1028 cpu_pmu_destroy(pmu);
1029 return ret;
1030 }
1031
1032 int arm_pmu_device_probe(struct platform_device *pdev,
1033 const struct of_device_id *of_table,
1034 const struct pmu_probe_info *probe_table)
1035 {
1036 const struct of_device_id *of_id;
1037 armpmu_init_fn init_fn;
1038 struct device_node *node = pdev->dev.of_node;
1039 struct arm_pmu *pmu;
1040 int ret = -ENODEV;
1041
1042 pmu = armpmu_alloc();
1043 if (!pmu)
1044 return -ENOMEM;
1045
1046 pmu->plat_device = pdev;
1047
1048 ret = pmu_parse_irqs(pmu);
1049 if (ret)
1050 goto out_free;
1051
1052 if (node && (of_id = of_match_node(of_table, pdev->dev.of_node))) {
1053 init_fn = of_id->data;
1054
1055 pmu->secure_access = of_property_read_bool(pdev->dev.of_node,
1056 "secure-reg-access");
1057
1058 /* arm64 systems boot only as non-secure */
1059 if (IS_ENABLED(CONFIG_ARM64) && pmu->secure_access) {
1060 pr_warn("ignoring \"secure-reg-access\" property for arm64\n");
1061 pmu->secure_access = false;
1062 }
1063
1064 ret = init_fn(pmu);
1065 } else if (probe_table) {
1066 cpumask_setall(&pmu->supported_cpus);
1067 ret = probe_current_pmu(pmu, probe_table);
1068 }
1069
1070 if (ret) {
1071 pr_info("%s: failed to probe PMU!\n", of_node_full_name(node));
1072 goto out_free;
1073 }
1074
1075 ret = armpmu_register(pmu);
1076 if (ret)
1077 goto out_free;
1078
1079 return 0;
1080
1081 out_free:
1082 pr_info("%s: failed to register PMU devices!\n",
1083 of_node_full_name(node));
1084 armpmu_free(pmu);
1085 return ret;
1086 }
1087
1088 static int arm_pmu_hp_init(void)
1089 {
1090 int ret;
1091
1092 ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_STARTING,
1093 "perf/arm/pmu:starting",
1094 arm_perf_starting_cpu,
1095 arm_perf_teardown_cpu);
1096 if (ret)
1097 pr_err("CPU hotplug notifier for ARM PMU could not be registered: %d\n",
1098 ret);
1099 return ret;
1100 }
1101 subsys_initcall(arm_pmu_hp_init);