4 * ARM performance counter support.
6 * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
7 * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
9 * This code is based on the sparc64 perf event code, which is in turn based
12 #define pr_fmt(fmt) "hw perfevents: " fmt
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>
27 #include <asm/cputype.h>
28 #include <asm/irq_regs.h>
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
],
37 unsigned int cache_type
, cache_op
, cache_result
, ret
;
39 cache_type
= (config
>> 0) & 0xff;
40 if (cache_type
>= PERF_COUNT_HW_CACHE_MAX
)
43 cache_op
= (config
>> 8) & 0xff;
44 if (cache_op
>= PERF_COUNT_HW_CACHE_OP_MAX
)
47 cache_result
= (config
>> 16) & 0xff;
48 if (cache_result
>= PERF_COUNT_HW_CACHE_RESULT_MAX
)
51 ret
= (int)(*cache_map
)[cache_type
][cache_op
][cache_result
];
53 if (ret
== CACHE_OP_UNSUPPORTED
)
60 armpmu_map_hw_event(const unsigned (*event_map
)[PERF_COUNT_HW_MAX
], u64 config
)
64 if (config
>= PERF_COUNT_HW_MAX
)
67 mapping
= (*event_map
)[config
];
68 return mapping
== HW_OP_UNSUPPORTED
? -ENOENT
: mapping
;
72 armpmu_map_raw_event(u32 raw_event_mask
, u64 config
)
74 return (int)(config
& raw_event_mask
);
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
],
86 u64 config
= event
->attr
.config
;
87 int type
= event
->attr
.type
;
89 if (type
== event
->pmu
->type
)
90 return armpmu_map_raw_event(raw_event_mask
, config
);
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
);
98 return armpmu_map_raw_event(raw_event_mask
, config
);
104 int armpmu_event_set_period(struct perf_event
*event
)
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
;
112 if (unlikely(left
<= -period
)) {
114 local64_set(&hwc
->period_left
, left
);
115 hwc
->last_period
= period
;
119 if (unlikely(left
<= 0)) {
121 local64_set(&hwc
->period_left
, left
);
122 hwc
->last_period
= period
;
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).
132 if (left
> (armpmu
->max_period
>> 1))
133 left
= armpmu
->max_period
>> 1;
135 local64_set(&hwc
->prev_count
, (u64
)-left
);
137 armpmu
->write_counter(event
, (u64
)(-left
) & 0xffffffff);
139 perf_event_update_userpage(event
);
144 u64
armpmu_event_update(struct perf_event
*event
)
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
;
151 prev_raw_count
= local64_read(&hwc
->prev_count
);
152 new_raw_count
= armpmu
->read_counter(event
);
154 if (local64_cmpxchg(&hwc
->prev_count
, prev_raw_count
,
155 new_raw_count
) != prev_raw_count
)
158 delta
= (new_raw_count
- prev_raw_count
) & armpmu
->max_period
;
160 local64_add(delta
, &event
->count
);
161 local64_sub(delta
, &hwc
->period_left
);
163 return new_raw_count
;
167 armpmu_read(struct perf_event
*event
)
169 armpmu_event_update(event
);
173 armpmu_stop(struct perf_event
*event
, int flags
)
175 struct arm_pmu
*armpmu
= to_arm_pmu(event
->pmu
);
176 struct hw_perf_event
*hwc
= &event
->hw
;
179 * ARM pmu always has to update the counter, so ignore
180 * PERF_EF_UPDATE, see comments in armpmu_start().
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
;
189 static void armpmu_start(struct perf_event
*event
, int flags
)
191 struct arm_pmu
*armpmu
= to_arm_pmu(event
->pmu
);
192 struct hw_perf_event
*hwc
= &event
->hw
;
195 * ARM pmu always has to reprogram the period, so ignore
196 * PERF_EF_RELOAD, see the comment below.
198 if (flags
& PERF_EF_RELOAD
)
199 WARN_ON_ONCE(!(hwc
->state
& PERF_HES_UPTODATE
));
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.
209 armpmu_event_set_period(event
);
210 armpmu
->enable(event
);
214 armpmu_del(struct perf_event
*event
, int flags
)
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
;
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
);
227 perf_event_update_userpage(event
);
231 armpmu_add(struct perf_event
*event
, int flags
)
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
;
238 /* An event following a process won't be stopped earlier */
239 if (!cpumask_test_cpu(smp_processor_id(), &armpmu
->supported_cpus
))
242 /* If we don't have a space for the counter then finish early. */
243 idx
= armpmu
->get_event_idx(hw_events
, event
);
248 * If there is an event in the counter we are going to use then make
249 * sure it is disabled.
252 armpmu
->disable(event
);
253 hw_events
->events
[idx
] = event
;
255 hwc
->state
= PERF_HES_STOPPED
| PERF_HES_UPTODATE
;
256 if (flags
& PERF_EF_START
)
257 armpmu_start(event
, PERF_EF_RELOAD
);
259 /* Propagate our changes to the userspace mapping. */
260 perf_event_update_userpage(event
);
266 validate_event(struct pmu
*pmu
, struct pmu_hw_events
*hw_events
,
267 struct perf_event
*event
)
269 struct arm_pmu
*armpmu
;
271 if (is_software_event(event
))
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).
279 if (event
->pmu
!= pmu
)
282 if (event
->state
< PERF_EVENT_STATE_OFF
)
285 if (event
->state
== PERF_EVENT_STATE_OFF
&& !event
->attr
.enable_on_exec
)
288 armpmu
= to_arm_pmu(event
->pmu
);
289 return armpmu
->get_event_idx(hw_events
, event
) >= 0;
293 validate_group(struct perf_event
*event
)
295 struct perf_event
*sibling
, *leader
= event
->group_leader
;
296 struct pmu_hw_events fake_pmu
;
299 * Initialise the fake PMU. We only need to populate the
300 * used_mask for the purposes of validation.
302 memset(&fake_pmu
.used_mask
, 0, sizeof(fake_pmu
.used_mask
));
304 if (!validate_event(event
->pmu
, &fake_pmu
, leader
))
307 list_for_each_entry(sibling
, &leader
->sibling_list
, group_entry
) {
308 if (!validate_event(event
->pmu
, &fake_pmu
, sibling
))
312 if (!validate_event(event
->pmu
, &fake_pmu
, event
))
318 static struct arm_pmu_platdata
*armpmu_get_platdata(struct arm_pmu
*armpmu
)
320 struct platform_device
*pdev
= armpmu
->plat_device
;
322 return pdev
? dev_get_platdata(&pdev
->dev
) : NULL
;
325 static irqreturn_t
armpmu_dispatch_irq(int irq
, void *dev
)
327 struct arm_pmu
*armpmu
;
328 struct arm_pmu_platdata
*plat
;
330 u64 start_clock
, finish_clock
;
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
338 armpmu
= *(void **)dev
;
340 plat
= armpmu_get_platdata(armpmu
);
342 start_clock
= sched_clock();
343 if (plat
&& plat
->handle_irq
)
344 ret
= plat
->handle_irq(irq
, armpmu
, armpmu
->handle_irq
);
346 ret
= armpmu
->handle_irq(irq
, armpmu
);
347 finish_clock
= sched_clock();
349 perf_sample_event_took(finish_clock
- start_clock
);
354 event_requires_mode_exclusion(struct perf_event_attr
*attr
)
356 return attr
->exclude_idle
|| attr
->exclude_user
||
357 attr
->exclude_kernel
|| attr
->exclude_hv
;
361 __hw_perf_event_init(struct perf_event
*event
)
363 struct arm_pmu
*armpmu
= to_arm_pmu(event
->pmu
);
364 struct hw_perf_event
*hwc
= &event
->hw
;
367 mapping
= armpmu
->map_event(event
);
370 pr_debug("event %x:%llx not supported\n", event
->attr
.type
,
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.
382 hwc
->config_base
= 0;
387 * Check whether we need to exclude the counter from certain modes.
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 "
398 * Store the event encoding into the config_base field.
400 hwc
->config_base
|= (unsigned long)mapping
;
402 if (!is_sampling_event(event
)) {
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.
409 hwc
->sample_period
= armpmu
->max_period
>> 1;
410 hwc
->last_period
= hwc
->sample_period
;
411 local64_set(&hwc
->period_left
, hwc
->sample_period
);
414 if (event
->group_leader
!= event
) {
415 if (validate_group(event
) != 0)
422 static int armpmu_event_init(struct perf_event
*event
)
424 struct arm_pmu
*armpmu
= to_arm_pmu(event
->pmu
);
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.
433 if (event
->cpu
!= -1 &&
434 !cpumask_test_cpu(event
->cpu
, &armpmu
->supported_cpus
))
437 /* does not support taken branch sampling */
438 if (has_branch_stack(event
))
441 if (armpmu
->map_event(event
) == -ENOENT
)
444 return __hw_perf_event_init(event
);
447 static void armpmu_enable(struct pmu
*pmu
)
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
);
453 /* For task-bound events we may be called on other CPUs */
454 if (!cpumask_test_cpu(smp_processor_id(), &armpmu
->supported_cpus
))
458 armpmu
->start(armpmu
);
461 static void armpmu_disable(struct pmu
*pmu
)
463 struct arm_pmu
*armpmu
= to_arm_pmu(pmu
);
465 /* For task-bound events we may be called on other CPUs */
466 if (!cpumask_test_cpu(smp_processor_id(), &armpmu
->supported_cpus
))
469 armpmu
->stop(armpmu
);
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.
477 static int armpmu_filter_match(struct perf_event
*event
)
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
);
484 static ssize_t
armpmu_cpumask_show(struct device
*dev
,
485 struct device_attribute
*attr
, char *buf
)
487 struct arm_pmu
*armpmu
= to_arm_pmu(dev_get_drvdata(dev
));
488 return cpumap_print_to_pagebuf(true, buf
, &armpmu
->supported_cpus
);
491 static DEVICE_ATTR(cpus
, S_IRUGO
, armpmu_cpumask_show
, NULL
);
493 static struct attribute
*armpmu_common_attrs
[] = {
498 static struct attribute_group armpmu_common_attr_group
= {
499 .attrs
= armpmu_common_attrs
,
502 /* Set at runtime when we know what CPU type we are. */
503 static struct arm_pmu
*__oprofile_cpu_pmu
;
506 * Despite the names, these two functions are CPU-specific and are used
507 * by the OProfile/perf code.
509 const char *perf_pmu_name(void)
511 if (!__oprofile_cpu_pmu
)
514 return __oprofile_cpu_pmu
->name
;
516 EXPORT_SYMBOL_GPL(perf_pmu_name
);
518 int perf_num_counters(void)
522 if (__oprofile_cpu_pmu
!= NULL
)
523 max_events
= __oprofile_cpu_pmu
->num_events
;
527 EXPORT_SYMBOL_GPL(perf_num_counters
);
529 static void armpmu_free_irq(struct arm_pmu
*armpmu
, int cpu
)
531 struct pmu_hw_events __percpu
*hw_events
= armpmu
->hw_events
;
532 int irq
= per_cpu(hw_events
->irq
, cpu
);
534 if (!cpumask_test_and_clear_cpu(cpu
, &armpmu
->active_irqs
))
537 if (irq_is_percpu(irq
)) {
538 free_percpu_irq(irq
, &hw_events
->percpu_pmu
);
539 cpumask_clear(&armpmu
->active_irqs
);
543 free_irq(irq
, per_cpu_ptr(&hw_events
->percpu_pmu
, cpu
));
546 static void armpmu_free_irqs(struct arm_pmu
*armpmu
)
550 for_each_cpu(cpu
, &armpmu
->supported_cpus
)
551 armpmu_free_irq(armpmu
, cpu
);
554 static int armpmu_request_irq(struct arm_pmu
*armpmu
, int cpu
)
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
);
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
);
570 if (irq
!= other_irq
) {
571 pr_warn("mismatched PPIs detected.\n");
575 err
= request_irq(irq
, handler
,
576 IRQF_NOBALANCING
| IRQF_NO_THREAD
, "arm-pmu",
577 per_cpu_ptr(&hw_events
->percpu_pmu
, cpu
));
581 pr_err("unable to request IRQ%d for ARM PMU counters\n",
586 cpumask_set_cpu(cpu
, &armpmu
->active_irqs
);
591 static int armpmu_request_irqs(struct arm_pmu
*armpmu
)
595 for_each_cpu(cpu
, &armpmu
->supported_cpus
) {
596 err
= armpmu_request_irq(armpmu
, cpu
);
604 static int armpmu_get_cpu_irq(struct arm_pmu
*pmu
, int cpu
)
606 struct pmu_hw_events __percpu
*hw_events
= pmu
->hw_events
;
607 return per_cpu(hw_events
->irq
, cpu
);
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.
616 static int arm_perf_starting_cpu(unsigned int cpu
, struct hlist_node
*node
)
618 struct arm_pmu
*pmu
= hlist_entry_safe(node
, struct arm_pmu
, node
);
621 if (!cpumask_test_cpu(cpu
, &pmu
->supported_cpus
))
626 irq
= armpmu_get_cpu_irq(pmu
, cpu
);
628 if (irq_is_percpu(irq
)) {
629 enable_percpu_irq(irq
, IRQ_TYPE_NONE
);
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",
643 static int arm_perf_teardown_cpu(unsigned int cpu
, struct hlist_node
*node
)
645 struct arm_pmu
*pmu
= hlist_entry_safe(node
, struct arm_pmu
, node
);
648 if (!cpumask_test_cpu(cpu
, &pmu
->supported_cpus
))
651 irq
= armpmu_get_cpu_irq(pmu
, cpu
);
652 if (irq
&& irq_is_percpu(irq
))
653 disable_percpu_irq(irq
);
659 static void cpu_pm_pmu_setup(struct arm_pmu
*armpmu
, unsigned long cmd
)
661 struct pmu_hw_events
*hw_events
= this_cpu_ptr(armpmu
->hw_events
);
662 struct perf_event
*event
;
665 for (idx
= 0; idx
< armpmu
->num_events
; idx
++) {
667 * If the counter is not used skip it, there is no
668 * need of stopping/restarting it.
670 if (!test_bit(idx
, hw_events
->used_mask
))
673 event
= hw_events
->events
[idx
];
678 * Stop and update the counter
680 armpmu_stop(event
, PERF_EF_UPDATE
);
683 case CPU_PM_ENTER_FAILED
:
685 * Restore and enable the counter.
686 * armpmu_start() indirectly calls
688 * perf_event_update_userpage()
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
696 RCU_NONIDLE(armpmu_start(event
, PERF_EF_RELOAD
));
704 static int cpu_pm_pmu_notify(struct notifier_block
*b
, unsigned long cmd
,
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
);
711 if (!cpumask_test_cpu(smp_processor_id(), &armpmu
->supported_cpus
))
715 * Always reset the PMU registers on power-up even if
716 * there are no events running.
718 if (cmd
== CPU_PM_EXIT
&& armpmu
->reset
)
719 armpmu
->reset(armpmu
);
726 armpmu
->stop(armpmu
);
727 cpu_pm_pmu_setup(armpmu
, cmd
);
730 cpu_pm_pmu_setup(armpmu
, cmd
);
731 case CPU_PM_ENTER_FAILED
:
732 armpmu
->start(armpmu
);
741 static int cpu_pm_pmu_register(struct arm_pmu
*cpu_pmu
)
743 cpu_pmu
->cpu_pm_nb
.notifier_call
= cpu_pm_pmu_notify
;
744 return cpu_pm_register_notifier(&cpu_pmu
->cpu_pm_nb
);
747 static void cpu_pm_pmu_unregister(struct arm_pmu
*cpu_pmu
)
749 cpu_pm_unregister_notifier(&cpu_pmu
->cpu_pm_nb
);
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
) { }
756 static int cpu_pmu_init(struct arm_pmu
*cpu_pmu
)
760 err
= armpmu_request_irqs(cpu_pmu
);
764 err
= cpuhp_state_add_instance(CPUHP_AP_PERF_ARM_STARTING
,
769 err
= cpu_pm_pmu_register(cpu_pmu
);
776 cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_STARTING
,
779 armpmu_free_irqs(cpu_pmu
);
783 static void cpu_pmu_destroy(struct arm_pmu
*cpu_pmu
)
785 cpu_pm_pmu_unregister(cpu_pmu
);
786 cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_STARTING
,
791 * CPU PMU identification and probing.
793 static int probe_current_pmu(struct arm_pmu
*pmu
,
794 const struct pmu_probe_info
*info
)
797 unsigned int cpuid
= read_cpuid_id();
800 pr_info("probing PMU on CPU %d\n", cpu
);
802 for (; info
->init
!= NULL
; info
++) {
803 if ((cpuid
& info
->mask
) != info
->cpuid
)
805 ret
= info
->init(pmu
);
813 static int pmu_parse_percpu_irq(struct arm_pmu
*pmu
, int irq
)
816 struct pmu_hw_events __percpu
*hw_events
= pmu
->hw_events
;
818 ret
= irq_get_percpu_devid_partition(irq
, &pmu
->supported_cpus
);
822 for_each_cpu(cpu
, &pmu
->supported_cpus
)
823 per_cpu(hw_events
->irq
, cpu
) = irq
;
828 static bool pmu_has_irq_affinity(struct device_node
*node
)
830 return !!of_find_property(node
, "interrupt-affinity", NULL
);
833 static int pmu_parse_irq_affinity(struct device_node
*node
, int i
)
835 struct device_node
*dn
;
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().
843 if (!pmu_has_irq_affinity(node
))
846 dn
= of_parse_phandle(node
, "interrupt-affinity", i
);
848 pr_warn("failed to parse interrupt-affinity[%d] for %s\n",
853 /* Now look up the logical CPU number */
854 for_each_possible_cpu(cpu
) {
855 struct device_node
*cpu_dn
;
857 cpu_dn
= of_cpu_device_node_get(cpu
);
864 if (cpu
>= nr_cpu_ids
) {
865 pr_warn("failed to find logical CPU for %s\n", dn
->name
);
873 static int pmu_parse_irqs(struct arm_pmu
*pmu
)
876 struct platform_device
*pdev
= pmu
->plat_device
;
877 struct pmu_hw_events __percpu
*hw_events
= pmu
->hw_events
;
879 irqs
= platform_irq_count(pdev
);
881 pr_err("unable to count PMU IRQs\n");
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.
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
);
897 int irq
= platform_get_irq(pdev
, 0);
898 if (irq
&& irq_is_percpu(irq
))
899 return pmu_parse_percpu_irq(pmu
, irq
);
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
));
908 * Some platforms have all PMU IRQs OR'd into a single IRQ, with a
909 * special platdata function that attempts to demux them.
911 if (dev_get_platdata(&pdev
->dev
))
912 cpumask_setall(&pmu
->supported_cpus
);
914 for (i
= 0; i
< irqs
; i
++) {
917 irq
= platform_get_irq(pdev
, i
);
918 if (WARN_ON(irq
<= 0))
921 if (irq_is_percpu(irq
)) {
922 pr_warn("multiple PPIs or mismatched SPI/PPI detected\n");
926 cpu
= pmu_parse_irq_affinity(pdev
->dev
.of_node
, i
);
929 if (cpu
>= nr_cpu_ids
)
932 if (per_cpu(hw_events
->irq
, cpu
)) {
933 pr_warn("multiple PMU IRQs for the same CPU detected\n");
937 per_cpu(hw_events
->irq
, cpu
) = irq
;
938 cpumask_set_cpu(cpu
, &pmu
->supported_cpus
);
944 static struct arm_pmu
*armpmu_alloc(void)
949 pmu
= kzalloc(sizeof(*pmu
), GFP_KERNEL
);
951 pr_info("failed to allocate PMU device!\n");
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");
961 pmu
->pmu
= (struct pmu
) {
962 .pmu_enable
= armpmu_enable
,
963 .pmu_disable
= armpmu_disable
,
964 .event_init
= armpmu_event_init
,
967 .start
= armpmu_start
,
970 .filter_match
= armpmu_filter_match
,
971 .attr_groups
= pmu
->attr_groups
,
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
979 .capabilities
= PERF_PMU_CAP_HETEROGENEOUS_CPUS
,
982 pmu
->attr_groups
[ARMPMU_ATTR_GROUP_COMMON
] =
983 &armpmu_common_attr_group
;
985 for_each_possible_cpu(cpu
) {
986 struct pmu_hw_events
*events
;
988 events
= per_cpu_ptr(pmu
->hw_events
, cpu
);
989 raw_spin_lock_init(&events
->pmu_lock
);
990 events
->percpu_pmu
= pmu
;
1001 static void armpmu_free(struct arm_pmu
*pmu
)
1003 free_percpu(pmu
->hw_events
);
1007 int armpmu_register(struct arm_pmu
*pmu
)
1011 ret
= cpu_pmu_init(pmu
);
1015 ret
= perf_pmu_register(&pmu
->pmu
, pmu
->name
, -1);
1019 if (!__oprofile_cpu_pmu
)
1020 __oprofile_cpu_pmu
= pmu
;
1022 pr_info("enabled with %s PMU driver, %d counters available\n",
1023 pmu
->name
, pmu
->num_events
);
1028 cpu_pmu_destroy(pmu
);
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
)
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
;
1042 pmu
= armpmu_alloc();
1046 pmu
->plat_device
= pdev
;
1048 ret
= pmu_parse_irqs(pmu
);
1052 if (node
&& (of_id
= of_match_node(of_table
, pdev
->dev
.of_node
))) {
1053 init_fn
= of_id
->data
;
1055 pmu
->secure_access
= of_property_read_bool(pdev
->dev
.of_node
,
1056 "secure-reg-access");
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;
1065 } else if (probe_table
) {
1066 cpumask_setall(&pmu
->supported_cpus
);
1067 ret
= probe_current_pmu(pmu
, probe_table
);
1071 pr_info("%s: failed to probe PMU!\n", of_node_full_name(node
));
1075 ret
= armpmu_register(pmu
);
1082 pr_info("%s: failed to register PMU devices!\n",
1083 of_node_full_name(node
));
1088 static int arm_pmu_hp_init(void)
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
);
1097 pr_err("CPU hotplug notifier for ARM PMU could not be registered: %d\n",
1101 subsys_initcall(arm_pmu_hp_init
);