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/export.h>
17 #include <linux/kernel.h>
18 #include <linux/of_device.h>
19 #include <linux/perf/arm_pmu.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
23 #include <linux/irq.h>
24 #include <linux/irqdesc.h>
26 #include <asm/cputype.h>
27 #include <asm/irq_regs.h>
30 armpmu_map_cache_event(const unsigned (*cache_map)
31 [PERF_COUNT_HW_CACHE_MAX]
32 [PERF_COUNT_HW_CACHE_OP_MAX]
33 [PERF_COUNT_HW_CACHE_RESULT_MAX],
36 unsigned int cache_type, cache_op, cache_result, ret;
38 cache_type = (config >> 0) & 0xff;
39 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
42 cache_op = (config >> 8) & 0xff;
43 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
46 cache_result = (config >> 16) & 0xff;
47 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
50 ret = (int)(*cache_map)[cache_type][cache_op][cache_result];
52 if (ret == CACHE_OP_UNSUPPORTED)
59 armpmu_map_hw_event(const unsigned (*event_map)[PERF_COUNT_HW_MAX], u64 config)
63 if (config >= PERF_COUNT_HW_MAX)
66 mapping = (*event_map)[config];
67 return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping;
71 armpmu_map_raw_event(u32 raw_event_mask, u64 config)
73 return (int)(config & raw_event_mask);
77 armpmu_map_event(struct perf_event *event,
78 const unsigned (*event_map)[PERF_COUNT_HW_MAX],
79 const unsigned (*cache_map)
80 [PERF_COUNT_HW_CACHE_MAX]
81 [PERF_COUNT_HW_CACHE_OP_MAX]
82 [PERF_COUNT_HW_CACHE_RESULT_MAX],
85 u64 config = event->attr.config;
86 int type = event->attr.type;
88 if (type == event->pmu->type)
89 return armpmu_map_raw_event(raw_event_mask, config);
92 case PERF_TYPE_HARDWARE:
93 return armpmu_map_hw_event(event_map, config);
94 case PERF_TYPE_HW_CACHE:
95 return armpmu_map_cache_event(cache_map, config);
97 return armpmu_map_raw_event(raw_event_mask, config);
103 int armpmu_event_set_period(struct perf_event *event)
105 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
106 struct hw_perf_event *hwc = &event->hw;
107 s64 left = local64_read(&hwc->period_left);
108 s64 period = hwc->sample_period;
111 if (unlikely(left <= -period)) {
113 local64_set(&hwc->period_left, left);
114 hwc->last_period = period;
118 if (unlikely(left <= 0)) {
120 local64_set(&hwc->period_left, left);
121 hwc->last_period = period;
126 * Limit the maximum period to prevent the counter value
127 * from overtaking the one we are about to program. In
128 * effect we are reducing max_period to account for
129 * interrupt latency (and we are being very conservative).
131 if (left > (armpmu->max_period >> 1))
132 left = armpmu->max_period >> 1;
134 local64_set(&hwc->prev_count, (u64)-left);
136 armpmu->write_counter(event, (u64)(-left) & 0xffffffff);
138 perf_event_update_userpage(event);
143 u64 armpmu_event_update(struct perf_event *event)
145 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
146 struct hw_perf_event *hwc = &event->hw;
147 u64 delta, prev_raw_count, new_raw_count;
150 prev_raw_count = local64_read(&hwc->prev_count);
151 new_raw_count = armpmu->read_counter(event);
153 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
154 new_raw_count) != prev_raw_count)
157 delta = (new_raw_count - prev_raw_count) & armpmu->max_period;
159 local64_add(delta, &event->count);
160 local64_sub(delta, &hwc->period_left);
162 return new_raw_count;
166 armpmu_read(struct perf_event *event)
168 armpmu_event_update(event);
172 armpmu_stop(struct perf_event *event, int flags)
174 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
175 struct hw_perf_event *hwc = &event->hw;
178 * ARM pmu always has to update the counter, so ignore
179 * PERF_EF_UPDATE, see comments in armpmu_start().
181 if (!(hwc->state & PERF_HES_STOPPED)) {
182 armpmu->disable(event);
183 armpmu_event_update(event);
184 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
188 static void armpmu_start(struct perf_event *event, int flags)
190 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
191 struct hw_perf_event *hwc = &event->hw;
194 * ARM pmu always has to reprogram the period, so ignore
195 * PERF_EF_RELOAD, see the comment below.
197 if (flags & PERF_EF_RELOAD)
198 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
202 * Set the period again. Some counters can't be stopped, so when we
203 * were stopped we simply disabled the IRQ source and the counter
204 * may have been left counting. If we don't do this step then we may
205 * get an interrupt too soon or *way* too late if the overflow has
206 * happened since disabling.
208 armpmu_event_set_period(event);
209 armpmu->enable(event);
213 armpmu_del(struct perf_event *event, int flags)
215 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
216 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
217 struct hw_perf_event *hwc = &event->hw;
220 armpmu_stop(event, PERF_EF_UPDATE);
221 hw_events->events[idx] = NULL;
222 clear_bit(idx, hw_events->used_mask);
223 if (armpmu->clear_event_idx)
224 armpmu->clear_event_idx(hw_events, event);
226 perf_event_update_userpage(event);
230 armpmu_add(struct perf_event *event, int flags)
232 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
233 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
234 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 perf_pmu_disable(event->pmu);
244 /* If we don't have a space for the counter then finish early. */
245 idx = armpmu->get_event_idx(hw_events, event);
252 * If there is an event in the counter we are going to use then make
253 * sure it is disabled.
256 armpmu->disable(event);
257 hw_events->events[idx] = event;
259 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
260 if (flags & PERF_EF_START)
261 armpmu_start(event, PERF_EF_RELOAD);
263 /* Propagate our changes to the userspace mapping. */
264 perf_event_update_userpage(event);
267 perf_pmu_enable(event->pmu);
272 validate_event(struct pmu *pmu, struct pmu_hw_events *hw_events,
273 struct perf_event *event)
275 struct arm_pmu *armpmu;
277 if (is_software_event(event))
281 * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
282 * core perf code won't check that the pmu->ctx == leader->ctx
283 * until after pmu->event_init(event).
285 if (event->pmu != pmu)
288 if (event->state < PERF_EVENT_STATE_OFF)
291 if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
294 armpmu = to_arm_pmu(event->pmu);
295 return armpmu->get_event_idx(hw_events, event) >= 0;
299 validate_group(struct perf_event *event)
301 struct perf_event *sibling, *leader = event->group_leader;
302 struct pmu_hw_events fake_pmu;
305 * Initialise the fake PMU. We only need to populate the
306 * used_mask for the purposes of validation.
308 memset(&fake_pmu.used_mask, 0, sizeof(fake_pmu.used_mask));
310 if (!validate_event(event->pmu, &fake_pmu, leader))
313 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
314 if (!validate_event(event->pmu, &fake_pmu, sibling))
318 if (!validate_event(event->pmu, &fake_pmu, event))
324 static irqreturn_t armpmu_dispatch_irq(int irq, void *dev)
326 struct arm_pmu *armpmu;
327 struct platform_device *plat_device;
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;
339 plat_device = armpmu->plat_device;
340 plat = dev_get_platdata(&plat_device->dev);
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 armpmu_release_hardware(struct arm_pmu *armpmu)
356 armpmu->free_irq(armpmu);
360 armpmu_reserve_hardware(struct arm_pmu *armpmu)
362 int err = armpmu->request_irq(armpmu, armpmu_dispatch_irq);
364 armpmu_release_hardware(armpmu);
372 hw_perf_event_destroy(struct perf_event *event)
374 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
375 atomic_t *active_events = &armpmu->active_events;
376 struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex;
378 if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) {
379 armpmu_release_hardware(armpmu);
380 mutex_unlock(pmu_reserve_mutex);
385 event_requires_mode_exclusion(struct perf_event_attr *attr)
387 return attr->exclude_idle || attr->exclude_user ||
388 attr->exclude_kernel || attr->exclude_hv;
392 __hw_perf_event_init(struct perf_event *event)
394 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
395 struct hw_perf_event *hwc = &event->hw;
398 mapping = armpmu->map_event(event);
401 pr_debug("event %x:%llx not supported\n", event->attr.type,
407 * We don't assign an index until we actually place the event onto
408 * hardware. Use -1 to signify that we haven't decided where to put it
409 * yet. For SMP systems, each core has it's own PMU so we can't do any
410 * clever allocation or constraints checking at this point.
413 hwc->config_base = 0;
418 * Check whether we need to exclude the counter from certain modes.
420 if ((!armpmu->set_event_filter ||
421 armpmu->set_event_filter(hwc, &event->attr)) &&
422 event_requires_mode_exclusion(&event->attr)) {
423 pr_debug("ARM performance counters do not support "
429 * Store the event encoding into the config_base field.
431 hwc->config_base |= (unsigned long)mapping;
433 if (!is_sampling_event(event)) {
435 * For non-sampling runs, limit the sample_period to half
436 * of the counter width. That way, the new counter value
437 * is far less likely to overtake the previous one unless
438 * you have some serious IRQ latency issues.
440 hwc->sample_period = armpmu->max_period >> 1;
441 hwc->last_period = hwc->sample_period;
442 local64_set(&hwc->period_left, hwc->sample_period);
445 if (event->group_leader != event) {
446 if (validate_group(event) != 0)
453 static int armpmu_event_init(struct perf_event *event)
455 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
457 atomic_t *active_events = &armpmu->active_events;
460 * Reject CPU-affine events for CPUs that are of a different class to
461 * that which this PMU handles. Process-following events (where
462 * event->cpu == -1) can be migrated between CPUs, and thus we have to
463 * reject them later (in armpmu_add) if they're scheduled on a
464 * different class of CPU.
466 if (event->cpu != -1 &&
467 !cpumask_test_cpu(event->cpu, &armpmu->supported_cpus))
470 /* does not support taken branch sampling */
471 if (has_branch_stack(event))
474 if (armpmu->map_event(event) == -ENOENT)
477 event->destroy = hw_perf_event_destroy;
479 if (!atomic_inc_not_zero(active_events)) {
480 mutex_lock(&armpmu->reserve_mutex);
481 if (atomic_read(active_events) == 0)
482 err = armpmu_reserve_hardware(armpmu);
485 atomic_inc(active_events);
486 mutex_unlock(&armpmu->reserve_mutex);
492 err = __hw_perf_event_init(event);
494 hw_perf_event_destroy(event);
499 static void armpmu_enable(struct pmu *pmu)
501 struct arm_pmu *armpmu = to_arm_pmu(pmu);
502 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
503 int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
505 /* For task-bound events we may be called on other CPUs */
506 if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
510 armpmu->start(armpmu);
513 static void armpmu_disable(struct pmu *pmu)
515 struct arm_pmu *armpmu = to_arm_pmu(pmu);
517 /* For task-bound events we may be called on other CPUs */
518 if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
521 armpmu->stop(armpmu);
525 * In heterogeneous systems, events are specific to a particular
526 * microarchitecture, and aren't suitable for another. Thus, only match CPUs of
527 * the same microarchitecture.
529 static int armpmu_filter_match(struct perf_event *event)
531 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
532 unsigned int cpu = smp_processor_id();
533 return cpumask_test_cpu(cpu, &armpmu->supported_cpus);
536 static void armpmu_init(struct arm_pmu *armpmu)
538 atomic_set(&armpmu->active_events, 0);
539 mutex_init(&armpmu->reserve_mutex);
541 armpmu->pmu = (struct pmu) {
542 .pmu_enable = armpmu_enable,
543 .pmu_disable = armpmu_disable,
544 .event_init = armpmu_event_init,
547 .start = armpmu_start,
550 .filter_match = armpmu_filter_match,
554 int armpmu_register(struct arm_pmu *armpmu, int type)
557 pr_info("enabled with %s PMU driver, %d counters available\n",
558 armpmu->name, armpmu->num_events);
559 return perf_pmu_register(&armpmu->pmu, armpmu->name, type);
562 /* Set at runtime when we know what CPU type we are. */
563 static struct arm_pmu *__oprofile_cpu_pmu;
566 * Despite the names, these two functions are CPU-specific and are used
567 * by the OProfile/perf code.
569 const char *perf_pmu_name(void)
571 if (!__oprofile_cpu_pmu)
574 return __oprofile_cpu_pmu->name;
576 EXPORT_SYMBOL_GPL(perf_pmu_name);
578 int perf_num_counters(void)
582 if (__oprofile_cpu_pmu != NULL)
583 max_events = __oprofile_cpu_pmu->num_events;
587 EXPORT_SYMBOL_GPL(perf_num_counters);
589 static void cpu_pmu_enable_percpu_irq(void *data)
591 int irq = *(int *)data;
593 enable_percpu_irq(irq, IRQ_TYPE_NONE);
596 static void cpu_pmu_disable_percpu_irq(void *data)
598 int irq = *(int *)data;
600 disable_percpu_irq(irq);
603 static void cpu_pmu_free_irq(struct arm_pmu *cpu_pmu)
606 struct platform_device *pmu_device = cpu_pmu->plat_device;
607 struct pmu_hw_events __percpu *hw_events = cpu_pmu->hw_events;
609 irqs = min(pmu_device->num_resources, num_possible_cpus());
611 irq = platform_get_irq(pmu_device, 0);
612 if (irq >= 0 && irq_is_percpu(irq)) {
613 on_each_cpu(cpu_pmu_disable_percpu_irq, &irq, 1);
614 free_percpu_irq(irq, &hw_events->percpu_pmu);
616 for (i = 0; i < irqs; ++i) {
619 if (cpu_pmu->irq_affinity)
620 cpu = cpu_pmu->irq_affinity[i];
622 if (!cpumask_test_and_clear_cpu(cpu, &cpu_pmu->active_irqs))
624 irq = platform_get_irq(pmu_device, i);
626 free_irq(irq, per_cpu_ptr(&hw_events->percpu_pmu, cpu));
631 static int cpu_pmu_request_irq(struct arm_pmu *cpu_pmu, irq_handler_t handler)
633 int i, err, irq, irqs;
634 struct platform_device *pmu_device = cpu_pmu->plat_device;
635 struct pmu_hw_events __percpu *hw_events = cpu_pmu->hw_events;
640 irqs = min(pmu_device->num_resources, num_possible_cpus());
642 pr_warn_once("perf/ARM: No irqs for PMU defined, sampling events not supported\n");
646 irq = platform_get_irq(pmu_device, 0);
647 if (irq >= 0 && irq_is_percpu(irq)) {
648 err = request_percpu_irq(irq, handler, "arm-pmu",
649 &hw_events->percpu_pmu);
651 pr_err("unable to request IRQ%d for ARM PMU counters\n",
655 on_each_cpu(cpu_pmu_enable_percpu_irq, &irq, 1);
657 for (i = 0; i < irqs; ++i) {
661 irq = platform_get_irq(pmu_device, i);
665 if (cpu_pmu->irq_affinity)
666 cpu = cpu_pmu->irq_affinity[i];
669 * If we have a single PMU interrupt that we can't shift,
670 * assume that we're running on a uniprocessor machine and
671 * continue. Otherwise, continue without this interrupt.
673 if (irq_set_affinity(irq, cpumask_of(cpu)) && irqs > 1) {
674 pr_warn("unable to set irq affinity (irq=%d, cpu=%u)\n",
679 err = request_irq(irq, handler,
680 IRQF_NOBALANCING | IRQF_NO_THREAD, "arm-pmu",
681 per_cpu_ptr(&hw_events->percpu_pmu, cpu));
683 pr_err("unable to request IRQ%d for ARM PMU counters\n",
688 cpumask_set_cpu(cpu, &cpu_pmu->active_irqs);
696 * PMU hardware loses all context when a CPU goes offline.
697 * When a CPU is hotplugged back in, since some hardware registers are
698 * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
699 * junk values out of them.
701 static int cpu_pmu_notify(struct notifier_block *b, unsigned long action,
704 int cpu = (unsigned long)hcpu;
705 struct arm_pmu *pmu = container_of(b, struct arm_pmu, hotplug_nb);
707 if ((action & ~CPU_TASKS_FROZEN) != CPU_STARTING)
710 if (!cpumask_test_cpu(cpu, &pmu->supported_cpus))
721 static int cpu_pmu_init(struct arm_pmu *cpu_pmu)
725 struct pmu_hw_events __percpu *cpu_hw_events;
727 cpu_hw_events = alloc_percpu(struct pmu_hw_events);
731 cpu_pmu->hotplug_nb.notifier_call = cpu_pmu_notify;
732 err = register_cpu_notifier(&cpu_pmu->hotplug_nb);
736 for_each_possible_cpu(cpu) {
737 struct pmu_hw_events *events = per_cpu_ptr(cpu_hw_events, cpu);
738 raw_spin_lock_init(&events->pmu_lock);
739 events->percpu_pmu = cpu_pmu;
742 cpu_pmu->hw_events = cpu_hw_events;
743 cpu_pmu->request_irq = cpu_pmu_request_irq;
744 cpu_pmu->free_irq = cpu_pmu_free_irq;
746 /* Ensure the PMU has sane values out of reset. */
748 on_each_cpu_mask(&cpu_pmu->supported_cpus, cpu_pmu->reset,
751 /* If no interrupts available, set the corresponding capability flag */
752 if (!platform_get_irq(cpu_pmu->plat_device, 0))
753 cpu_pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
758 free_percpu(cpu_hw_events);
762 static void cpu_pmu_destroy(struct arm_pmu *cpu_pmu)
764 unregister_cpu_notifier(&cpu_pmu->hotplug_nb);
765 free_percpu(cpu_pmu->hw_events);
769 * CPU PMU identification and probing.
771 static int probe_current_pmu(struct arm_pmu *pmu,
772 const struct pmu_probe_info *info)
775 unsigned int cpuid = read_cpuid_id();
778 pr_info("probing PMU on CPU %d\n", cpu);
780 for (; info->init != NULL; info++) {
781 if ((cpuid & info->mask) != info->cpuid)
783 ret = info->init(pmu);
791 static int of_pmu_irq_cfg(struct arm_pmu *pmu)
794 bool using_spi = false;
795 struct platform_device *pdev = pmu->plat_device;
797 irqs = kcalloc(pdev->num_resources, sizeof(*irqs), GFP_KERNEL);
802 struct device_node *dn;
805 /* See if we have an affinity entry */
806 dn = of_parse_phandle(pdev->dev.of_node, "interrupt-affinity", i);
810 /* Check the IRQ type and prohibit a mix of PPIs and SPIs */
811 irq = platform_get_irq(pdev, i);
813 bool spi = !irq_is_percpu(irq);
815 if (i > 0 && spi != using_spi) {
816 pr_err("PPI/SPI IRQ type mismatch for %s!\n",
825 /* Now look up the logical CPU number */
826 for_each_possible_cpu(cpu) {
827 struct device_node *cpu_dn;
829 cpu_dn = of_cpu_device_node_get(cpu);
836 if (cpu >= nr_cpu_ids) {
837 pr_warn("Failed to find logical CPU for %s\n",
840 cpumask_setall(&pmu->supported_cpus);
845 /* For SPIs, we need to track the affinity per IRQ */
847 if (i >= pdev->num_resources) {
855 /* Keep track of the CPUs containing this PMU type */
856 cpumask_set_cpu(cpu, &pmu->supported_cpus);
861 /* If we didn't manage to parse anything, claim to support all CPUs */
862 if (cpumask_weight(&pmu->supported_cpus) == 0)
863 cpumask_setall(&pmu->supported_cpus);
865 /* If we matched up the IRQ affinities, use them to route the SPIs */
866 if (using_spi && i == pdev->num_resources)
867 pmu->irq_affinity = irqs;
874 int arm_pmu_device_probe(struct platform_device *pdev,
875 const struct of_device_id *of_table,
876 const struct pmu_probe_info *probe_table)
878 const struct of_device_id *of_id;
879 const int (*init_fn)(struct arm_pmu *);
880 struct device_node *node = pdev->dev.of_node;
884 pmu = kzalloc(sizeof(struct arm_pmu), GFP_KERNEL);
886 pr_info("failed to allocate PMU device!\n");
890 if (!__oprofile_cpu_pmu)
891 __oprofile_cpu_pmu = pmu;
893 pmu->plat_device = pdev;
895 if (node && (of_id = of_match_node(of_table, pdev->dev.of_node))) {
896 init_fn = of_id->data;
898 ret = of_pmu_irq_cfg(pmu);
902 ret = probe_current_pmu(pmu, probe_table);
903 cpumask_setall(&pmu->supported_cpus);
907 pr_info("failed to probe PMU!\n");
911 ret = cpu_pmu_init(pmu);
915 ret = armpmu_register(pmu, -1);
922 cpu_pmu_destroy(pmu);
924 pr_info("failed to register PMU devices!\n");