2 * Meta performance counter support.
3 * Copyright (C) 2012 Imagination Technologies Ltd
5 * This code is based on the sh pmu code:
6 * Copyright (C) 2009 Paul Mundt
8 * and on the arm pmu code:
9 * Copyright (C) 2009 picoChip Designs, Ltd., James Iles
10 * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
12 * This file is subject to the terms and conditions of the GNU General Public
13 * License. See the file "COPYING" in the main directory of this archive
17 #include <linux/atomic.h>
18 #include <linux/export.h>
19 #include <linux/init.h>
20 #include <linux/irqchip/metag.h>
21 #include <linux/perf_event.h>
22 #include <linux/slab.h>
24 #include <asm/core_reg.h>
27 #include <asm/processor.h>
29 #include "perf_event.h"
31 static int _hw_perf_event_init(struct perf_event *);
32 static void _hw_perf_event_destroy(struct perf_event *);
34 /* Determines which core type we are */
35 static struct metag_pmu *metag_pmu __read_mostly;
37 /* Processor specific data */
38 static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
41 const char *perf_pmu_name(void)
46 return metag_pmu->name;
48 EXPORT_SYMBOL_GPL(perf_pmu_name);
50 int perf_num_counters(void)
53 return metag_pmu->max_events;
57 EXPORT_SYMBOL_GPL(perf_num_counters);
59 static inline int metag_pmu_initialised(void)
64 static void release_pmu_hardware(void)
67 unsigned int version = (metag_pmu->version &
68 (METAC_ID_MINOR_BITS | METAC_ID_REV_BITS)) >>
71 /* Early cores don't have overflow interrupts */
75 irq = internal_irq_map(17);
77 free_irq(irq, (void *)1);
79 irq = internal_irq_map(16);
81 free_irq(irq, (void *)0);
84 static int reserve_pmu_hardware(void)
87 unsigned int version = (metag_pmu->version &
88 (METAC_ID_MINOR_BITS | METAC_ID_REV_BITS)) >>
91 /* Early cores don't have overflow interrupts */
96 * Bit 16 on HWSTATMETA is the interrupt for performance counter 0;
97 * similarly, 17 is the interrupt for performance counter 1.
98 * We can't (yet) interrupt on the cycle counter, because it's a
99 * register, however it holds a 32-bit value as opposed to 24-bit.
101 irq[0] = internal_irq_map(16);
103 pr_err("unable to map internal IRQ %d\n", 16);
106 err = request_irq(irq[0], metag_pmu->handle_irq, IRQF_NOBALANCING,
107 "metagpmu0", (void *)0);
109 pr_err("unable to request IRQ%d for metag PMU counters\n",
114 irq[1] = internal_irq_map(17);
116 pr_err("unable to map internal IRQ %d\n", 17);
119 err = request_irq(irq[1], metag_pmu->handle_irq, IRQF_NOBALANCING,
120 "metagpmu1", (void *)1);
122 pr_err("unable to request IRQ%d for metag PMU counters\n",
130 free_irq(irq[0], (void *)0);
136 static void metag_pmu_enable(struct pmu *pmu)
140 static void metag_pmu_disable(struct pmu *pmu)
144 static int metag_pmu_event_init(struct perf_event *event)
147 atomic_t *active_events = &metag_pmu->active_events;
149 if (!metag_pmu_initialised()) {
154 if (has_branch_stack(event))
157 event->destroy = _hw_perf_event_destroy;
159 if (!atomic_inc_not_zero(active_events)) {
160 mutex_lock(&metag_pmu->reserve_mutex);
161 if (atomic_read(active_events) == 0)
162 err = reserve_pmu_hardware();
165 atomic_inc(active_events);
167 mutex_unlock(&metag_pmu->reserve_mutex);
170 /* Hardware and caches counters */
171 switch (event->attr.type) {
172 case PERF_TYPE_HARDWARE:
173 case PERF_TYPE_HW_CACHE:
175 err = _hw_perf_event_init(event);
183 event->destroy(event);
189 void metag_pmu_event_update(struct perf_event *event,
190 struct hw_perf_event *hwc, int idx)
192 u64 prev_raw_count, new_raw_count;
196 * If this counter is chained, it may be that the previous counter
197 * value has been changed beneath us.
199 * To get around this, we read and exchange the new raw count, then
200 * add the delta (new - prev) to the generic counter atomically.
202 * Without interrupts, this is the simplest approach.
205 prev_raw_count = local64_read(&hwc->prev_count);
206 new_raw_count = metag_pmu->read(idx);
208 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
209 new_raw_count) != prev_raw_count)
213 * Calculate the delta and add it to the counter.
215 delta = (new_raw_count - prev_raw_count) & MAX_PERIOD;
217 local64_add(delta, &event->count);
218 local64_sub(delta, &hwc->period_left);
221 int metag_pmu_event_set_period(struct perf_event *event,
222 struct hw_perf_event *hwc, int idx)
224 s64 left = local64_read(&hwc->period_left);
225 s64 period = hwc->sample_period;
228 /* The period may have been changed */
229 if (unlikely(period != hwc->last_period))
230 left += period - hwc->last_period;
232 if (unlikely(left <= -period)) {
234 local64_set(&hwc->period_left, left);
235 hwc->last_period = period;
239 if (unlikely(left <= 0)) {
241 local64_set(&hwc->period_left, left);
242 hwc->last_period = period;
246 if (left > (s64)metag_pmu->max_period)
247 left = metag_pmu->max_period;
249 if (metag_pmu->write) {
250 local64_set(&hwc->prev_count, -(s32)left);
251 metag_pmu->write(idx, -left & MAX_PERIOD);
254 perf_event_update_userpage(event);
259 static void metag_pmu_start(struct perf_event *event, int flags)
261 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
262 struct hw_perf_event *hwc = &event->hw;
265 if (WARN_ON_ONCE(idx == -1))
269 * We always have to reprogram the period, so ignore PERF_EF_RELOAD.
271 if (flags & PERF_EF_RELOAD)
272 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
278 * Some counters can't be stopped (i.e. are core global), so when the
279 * counter was 'stopped' we merely disabled the IRQ. If we don't reset
280 * the period, then we'll either: a) get an overflow too soon;
281 * or b) too late if the overflow happened since disabling.
282 * Obviously, this has little bearing on cores without the overflow
283 * interrupt, as the performance counter resets to zero on write
286 if (metag_pmu->max_period)
287 metag_pmu_event_set_period(event, hwc, hwc->idx);
288 cpuc->events[idx] = event;
289 metag_pmu->enable(hwc, idx);
292 static void metag_pmu_stop(struct perf_event *event, int flags)
294 struct hw_perf_event *hwc = &event->hw;
297 * We should always update the counter on stop; see comment above
300 if (!(hwc->state & PERF_HES_STOPPED)) {
301 metag_pmu_event_update(event, hwc, hwc->idx);
302 metag_pmu->disable(hwc, hwc->idx);
303 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
307 static int metag_pmu_add(struct perf_event *event, int flags)
309 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
310 struct hw_perf_event *hwc = &event->hw;
311 int idx = 0, ret = 0;
313 perf_pmu_disable(event->pmu);
315 /* check whether we're counting instructions */
316 if (hwc->config == 0x100) {
317 if (__test_and_set_bit(METAG_INST_COUNTER,
322 idx = METAG_INST_COUNTER;
324 /* Check whether we have a spare counter */
325 idx = find_first_zero_bit(cpuc->used_mask,
326 atomic_read(&metag_pmu->active_events));
327 if (idx >= METAG_INST_COUNTER) {
332 __set_bit(idx, cpuc->used_mask);
336 /* Make sure the counter is disabled */
337 metag_pmu->disable(hwc, idx);
339 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
340 if (flags & PERF_EF_START)
341 metag_pmu_start(event, PERF_EF_RELOAD);
343 perf_event_update_userpage(event);
345 perf_pmu_enable(event->pmu);
349 static void metag_pmu_del(struct perf_event *event, int flags)
351 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
352 struct hw_perf_event *hwc = &event->hw;
356 metag_pmu_stop(event, PERF_EF_UPDATE);
357 cpuc->events[idx] = NULL;
358 __clear_bit(idx, cpuc->used_mask);
360 perf_event_update_userpage(event);
363 static void metag_pmu_read(struct perf_event *event)
365 struct hw_perf_event *hwc = &event->hw;
367 /* Don't read disabled counters! */
371 metag_pmu_event_update(event, hwc, hwc->idx);
374 static struct pmu pmu = {
375 .pmu_enable = metag_pmu_enable,
376 .pmu_disable = metag_pmu_disable,
378 .event_init = metag_pmu_event_init,
380 .add = metag_pmu_add,
381 .del = metag_pmu_del,
382 .start = metag_pmu_start,
383 .stop = metag_pmu_stop,
384 .read = metag_pmu_read,
387 /* Core counter specific functions */
388 static const int metag_general_events[] = {
389 [PERF_COUNT_HW_CPU_CYCLES] = 0x03,
390 [PERF_COUNT_HW_INSTRUCTIONS] = 0x100,
391 [PERF_COUNT_HW_CACHE_REFERENCES] = -1,
392 [PERF_COUNT_HW_CACHE_MISSES] = -1,
393 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = -1,
394 [PERF_COUNT_HW_BRANCH_MISSES] = -1,
395 [PERF_COUNT_HW_BUS_CYCLES] = -1,
396 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = -1,
397 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = -1,
398 [PERF_COUNT_HW_REF_CPU_CYCLES] = -1,
401 static const int metag_pmu_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
404 [C(RESULT_ACCESS)] = 0x08,
405 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
408 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
409 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
412 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
413 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
418 [C(RESULT_ACCESS)] = 0x09,
419 [C(RESULT_MISS)] = 0x0a,
422 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
423 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
426 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
427 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
432 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
433 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
436 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
437 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
440 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
441 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
446 [C(RESULT_ACCESS)] = 0xd0,
447 [C(RESULT_MISS)] = 0xd2,
450 [C(RESULT_ACCESS)] = 0xd4,
451 [C(RESULT_MISS)] = 0xd5,
454 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
455 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
460 [C(RESULT_ACCESS)] = 0xd1,
461 [C(RESULT_MISS)] = 0xd3,
464 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
465 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
468 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
469 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
474 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
475 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
478 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
479 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
482 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
483 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
488 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
489 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
492 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
493 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
496 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
497 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
503 static void _hw_perf_event_destroy(struct perf_event *event)
505 atomic_t *active_events = &metag_pmu->active_events;
506 struct mutex *pmu_mutex = &metag_pmu->reserve_mutex;
508 if (atomic_dec_and_mutex_lock(active_events, pmu_mutex)) {
509 release_pmu_hardware();
510 mutex_unlock(pmu_mutex);
514 static int _hw_perf_cache_event(int config, int *evp)
516 unsigned long type, op, result;
519 if (!metag_pmu->cache_events)
523 type = config & 0xff;
524 op = (config >> 8) & 0xff;
525 result = (config >> 16) & 0xff;
527 if (type >= PERF_COUNT_HW_CACHE_MAX ||
528 op >= PERF_COUNT_HW_CACHE_OP_MAX ||
529 result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
532 ev = (*metag_pmu->cache_events)[type][op][result];
541 static int _hw_perf_event_init(struct perf_event *event)
543 struct perf_event_attr *attr = &event->attr;
544 struct hw_perf_event *hwc = &event->hw;
545 int mapping = 0, err;
547 switch (attr->type) {
548 case PERF_TYPE_HARDWARE:
549 if (attr->config >= PERF_COUNT_HW_MAX)
552 mapping = metag_pmu->event_map(attr->config);
555 case PERF_TYPE_HW_CACHE:
556 err = _hw_perf_cache_event(attr->config, &mapping);
562 mapping = attr->config;
566 /* Return early if the event is unsupported */
571 * Early cores have "limited" counters - they have no overflow
572 * interrupts - and so are unable to do sampling without extra work
573 * and timer assistance.
575 if (metag_pmu->max_period == 0) {
576 if (hwc->sample_period)
581 * Don't assign an index until the event is placed into the hardware.
582 * -1 signifies that we're still deciding where to put it. On SMP
583 * systems each core has its own set of counters, so we can't do any
584 * constraint checking yet.
588 /* Store the event encoding */
589 hwc->config |= (unsigned long)mapping;
592 * For non-sampling runs, limit the sample_period to half of the
593 * counter width. This way, the new counter value should be less
594 * likely to overtake the previous one (unless there are IRQ latency
597 if (metag_pmu->max_period) {
598 if (!hwc->sample_period) {
599 hwc->sample_period = metag_pmu->max_period >> 1;
600 hwc->last_period = hwc->sample_period;
601 local64_set(&hwc->period_left, hwc->sample_period);
608 static void metag_pmu_enable_counter(struct hw_perf_event *event, int idx)
610 struct cpu_hw_events *events = &__get_cpu_var(cpu_hw_events);
611 unsigned int config = event->config;
612 unsigned int tmp = config & 0xf0;
615 raw_spin_lock_irqsave(&events->pmu_lock, flags);
618 * Check if we're enabling the instruction counter (index of
621 if (METAG_INST_COUNTER == idx) {
622 WARN_ONCE((config != 0x100),
623 "invalid configuration (%d) for counter (%d)\n",
625 local64_set(&event->prev_count, __core_reg_get(TXTACTCYC));
629 /* Check for a core internal or performance channel event. */
634 * Anything other than a cycle count will write the low-
635 * nibble to the correct counter register.
639 perf_addr = (void *)PERF_ICORE(idx);
643 perf_addr = (void *)PERF_CHAN(idx);
652 metag_out32((config & 0x0f), perf_addr);
655 * Now we use the high nibble as the performance event to
661 tmp = ((config & 0xf) << 28) |
662 ((1 << 24) << hard_processor_id());
663 if (metag_pmu->max_period)
665 * Cores supporting overflow interrupts may have had the counter
666 * set to a specific value that needs preserving.
668 tmp |= metag_in32(PERF_COUNT(idx)) & 0x00ffffff;
671 * Older cores reset the counter on write, so prev_count needs
672 * resetting too so we can calculate a correct delta.
674 local64_set(&event->prev_count, 0);
676 metag_out32(tmp, PERF_COUNT(idx));
678 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
681 static void metag_pmu_disable_counter(struct hw_perf_event *event, int idx)
683 struct cpu_hw_events *events = &__get_cpu_var(cpu_hw_events);
684 unsigned int tmp = 0;
688 * The cycle counter can't be disabled per se, as it's a hardware
689 * thread register which is always counting. We merely return if this
690 * is the counter we're attempting to disable.
692 if (METAG_INST_COUNTER == idx)
696 * The counter value _should_ have been read prior to disabling,
697 * as if we're running on an early core then the value gets reset to
698 * 0, and any read after that would be useless. On the newer cores,
699 * however, it's better to read-modify-update this for purposes of
700 * the overflow interrupt.
701 * Here we remove the thread id AND the event nibble (there are at
702 * least two events that count events that are core global and ignore
703 * the thread id mask). This only works because we don't mix thread
704 * performance counts, and event 0x00 requires a thread id mask!
706 raw_spin_lock_irqsave(&events->pmu_lock, flags);
708 tmp = metag_in32(PERF_COUNT(idx));
710 metag_out32(tmp, PERF_COUNT(idx));
712 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
715 static u64 metag_pmu_read_counter(int idx)
719 if (METAG_INST_COUNTER == idx) {
720 tmp = __core_reg_get(TXTACTCYC);
724 tmp = metag_in32(PERF_COUNT(idx)) & 0x00ffffff;
729 static void metag_pmu_write_counter(int idx, u32 val)
731 struct cpu_hw_events *events = &__get_cpu_var(cpu_hw_events);
736 * This _shouldn't_ happen, but if it does, then we can just
737 * ignore the write, as the register is read-only and clear-on-write.
739 if (METAG_INST_COUNTER == idx)
743 * We'll keep the thread mask and event id, and just update the
744 * counter itself. Also , we should bound the value to 24-bits.
746 raw_spin_lock_irqsave(&events->pmu_lock, flags);
749 tmp = metag_in32(PERF_COUNT(idx)) & 0xff000000;
751 metag_out32(val, PERF_COUNT(idx));
753 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
756 static int metag_pmu_event_map(int idx)
758 return metag_general_events[idx];
761 static irqreturn_t metag_pmu_counter_overflow(int irq, void *dev)
764 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
765 struct perf_event *event = cpuhw->events[idx];
766 struct hw_perf_event *hwc = &event->hw;
767 struct pt_regs *regs = get_irq_regs();
768 struct perf_sample_data sampledata;
773 * We need to stop the core temporarily from generating another
774 * interrupt while we disable this counter. However, we don't want
775 * to flag the counter as free
777 __global_lock2(flags);
778 counter = metag_in32(PERF_COUNT(idx));
779 metag_out32((counter & 0x00ffffff), PERF_COUNT(idx));
780 __global_unlock2(flags);
782 /* Update the counts and reset the sample period */
783 metag_pmu_event_update(event, hwc, idx);
784 perf_sample_data_init(&sampledata, 0, hwc->last_period);
785 metag_pmu_event_set_period(event, hwc, idx);
788 * Enable the counter again once core overflow processing has
789 * completed. Note the counter value may have been modified while it was
790 * inactive to set it up ready for the next interrupt.
792 if (!perf_event_overflow(event, &sampledata, regs)) {
793 __global_lock2(flags);
794 counter = (counter & 0xff000000) |
795 (metag_in32(PERF_COUNT(idx)) & 0x00ffffff);
796 metag_out32(counter, PERF_COUNT(idx));
797 __global_unlock2(flags);
803 static struct metag_pmu _metag_pmu = {
804 .handle_irq = metag_pmu_counter_overflow,
805 .enable = metag_pmu_enable_counter,
806 .disable = metag_pmu_disable_counter,
807 .read = metag_pmu_read_counter,
808 .write = metag_pmu_write_counter,
809 .event_map = metag_pmu_event_map,
810 .cache_events = &metag_pmu_cache_events,
811 .max_period = MAX_PERIOD,
812 .max_events = MAX_HWEVENTS,
815 /* PMU CPU hotplug notifier */
816 static int metag_pmu_cpu_notify(struct notifier_block *b, unsigned long action,
819 unsigned int cpu = (unsigned int)hcpu;
820 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
822 if ((action & ~CPU_TASKS_FROZEN) != CPU_STARTING)
825 memset(cpuc, 0, sizeof(struct cpu_hw_events));
826 raw_spin_lock_init(&cpuc->pmu_lock);
831 static struct notifier_block metag_pmu_notifier = {
832 .notifier_call = metag_pmu_cpu_notify,
835 /* PMU Initialisation */
836 static int __init init_hw_perf_events(void)
839 u32 version = *(u32 *)METAC_ID;
840 int major = (version & METAC_ID_MAJOR_BITS) >> METAC_ID_MAJOR_S;
841 int min_rev = (version & (METAC_ID_MINOR_BITS | METAC_ID_REV_BITS))
844 /* Not a Meta 2 core, then not supported */
846 pr_info("no hardware counter support available\n");
848 } else if (0x02 == major) {
849 metag_pmu = &_metag_pmu;
851 if (min_rev < 0x0104) {
853 * A core without overflow interrupts, and clear-on-
856 metag_pmu->handle_irq = NULL;
857 metag_pmu->write = NULL;
858 metag_pmu->max_period = 0;
861 metag_pmu->name = "meta2";
862 metag_pmu->version = version;
863 metag_pmu->pmu = pmu;
866 pr_info("enabled with %s PMU driver, %d counters available\n",
867 metag_pmu->name, metag_pmu->max_events);
869 /* Initialise the active events and reservation mutex */
870 atomic_set(&metag_pmu->active_events, 0);
871 mutex_init(&metag_pmu->reserve_mutex);
873 /* Clear the counters */
874 metag_out32(0, PERF_COUNT(0));
875 metag_out32(0, PERF_COUNT(1));
877 for_each_possible_cpu(cpu) {
878 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
880 memset(cpuc, 0, sizeof(struct cpu_hw_events));
881 raw_spin_lock_init(&cpuc->pmu_lock);
884 register_cpu_notifier(&metag_pmu_notifier);
885 ret = perf_pmu_register(&pmu, metag_pmu->name, PERF_TYPE_RAW);
889 early_initcall(init_hw_perf_events);