1 /* Performance event support for sparc64.
3 * Copyright (C) 2009 David S. Miller <davem@davemloft.net>
5 * This code is based almost entirely upon the x86 perf event
8 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
9 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
10 * Copyright (C) 2009 Jaswinder Singh Rajput
11 * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
12 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
15 #include <linux/perf_event.h>
16 #include <linux/kprobes.h>
17 #include <linux/kernel.h>
18 #include <linux/kdebug.h>
19 #include <linux/mutex.h>
21 #include <asm/cpudata.h>
22 #include <asm/atomic.h>
26 /* Sparc64 chips have two performance counters, 32-bits each, with
27 * overflow interrupts generated on transition from 0xffffffff to 0.
28 * The counters are accessed in one go using a 64-bit register.
30 * Both counters are controlled using a single control register. The
31 * only way to stop all sampling is to clear all of the context (user,
32 * supervisor, hypervisor) sampling enable bits. But these bits apply
33 * to both counters, thus the two counters can't be enabled/disabled
36 * The control register has two event fields, one for each of the two
37 * counters. It's thus nearly impossible to have one counter going
38 * while keeping the other one stopped. Therefore it is possible to
39 * get overflow interrupts for counters not currently "in use" and
40 * that condition must be checked in the overflow interrupt handler.
42 * So we use a hack, in that we program inactive counters with the
43 * "sw_count0" and "sw_count1" events. These count how many times
44 * the instruction "sethi %hi(0xfc000), %g0" is executed. It's an
45 * unusual way to encode a NOP and therefore will not trigger in
49 #define MAX_HWEVENTS 2
50 #define MAX_PERIOD ((1UL << 32) - 1)
52 #define PIC_UPPER_INDEX 0
53 #define PIC_LOWER_INDEX 1
55 struct cpu_hw_events {
56 struct perf_event *events[MAX_HWEVENTS];
57 unsigned long used_mask[BITS_TO_LONGS(MAX_HWEVENTS)];
58 unsigned long active_mask[BITS_TO_LONGS(MAX_HWEVENTS)];
61 DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { .enabled = 1, };
63 struct perf_event_map {
67 #define PIC_UPPER 0x01
68 #define PIC_LOWER 0x02
71 static unsigned long perf_event_encode(const struct perf_event_map *pmap)
73 return ((unsigned long) pmap->encoding << 16) | pmap->pic_mask;
76 static void perf_event_decode(unsigned long val, u16 *enc, u8 *msk)
82 #define C(x) PERF_COUNT_HW_CACHE_##x
84 #define CACHE_OP_UNSUPPORTED 0xfffe
85 #define CACHE_OP_NONSENSE 0xffff
87 typedef struct perf_event_map cache_map_t
88 [PERF_COUNT_HW_CACHE_MAX]
89 [PERF_COUNT_HW_CACHE_OP_MAX]
90 [PERF_COUNT_HW_CACHE_RESULT_MAX];
93 const struct perf_event_map *(*event_map)(int);
94 const cache_map_t *cache_map;
105 static const struct perf_event_map ultra3_perfmon_event_map[] = {
106 [PERF_COUNT_HW_CPU_CYCLES] = { 0x0000, PIC_UPPER | PIC_LOWER },
107 [PERF_COUNT_HW_INSTRUCTIONS] = { 0x0001, PIC_UPPER | PIC_LOWER },
108 [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0009, PIC_LOWER },
109 [PERF_COUNT_HW_CACHE_MISSES] = { 0x0009, PIC_UPPER },
112 static const struct perf_event_map *ultra3_event_map(int event_id)
114 return &ultra3_perfmon_event_map[event_id];
117 static const cache_map_t ultra3_cache_map = {
120 [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
121 [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
124 [C(RESULT_ACCESS)] = { 0x0a, PIC_LOWER },
125 [C(RESULT_MISS)] = { 0x0a, PIC_UPPER },
128 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
129 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
134 [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
135 [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
138 [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
139 [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
141 [ C(OP_PREFETCH) ] = {
142 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
143 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
148 [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER, },
149 [C(RESULT_MISS)] = { 0x0c, PIC_UPPER, },
152 [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER },
153 [C(RESULT_MISS)] = { 0x0c, PIC_UPPER },
156 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
157 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
162 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
163 [C(RESULT_MISS)] = { 0x12, PIC_UPPER, },
166 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
167 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
169 [ C(OP_PREFETCH) ] = {
170 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
171 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
176 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
177 [C(RESULT_MISS)] = { 0x11, PIC_UPPER, },
180 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
181 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
183 [ C(OP_PREFETCH) ] = {
184 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
185 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
190 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
191 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
194 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
195 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
197 [ C(OP_PREFETCH) ] = {
198 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
199 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
204 static const struct sparc_pmu ultra3_pmu = {
205 .event_map = ultra3_event_map,
206 .cache_map = &ultra3_cache_map,
207 .max_events = ARRAY_SIZE(ultra3_perfmon_event_map),
215 /* Niagara1 is very limited. The upper PIC is hard-locked to count
216 * only instructions, so it is free running which creates all kinds of
217 * problems. Some hardware designs make one wonder if the creator
218 * even looked at how this stuff gets used by software.
220 static const struct perf_event_map niagara1_perfmon_event_map[] = {
221 [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, PIC_UPPER },
222 [PERF_COUNT_HW_INSTRUCTIONS] = { 0x00, PIC_UPPER },
223 [PERF_COUNT_HW_CACHE_REFERENCES] = { 0, PIC_NONE },
224 [PERF_COUNT_HW_CACHE_MISSES] = { 0x03, PIC_LOWER },
227 static const struct perf_event_map *niagara1_event_map(int event_id)
229 return &niagara1_perfmon_event_map[event_id];
232 static const cache_map_t niagara1_cache_map = {
235 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
236 [C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
239 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
240 [C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
243 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
244 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
249 [C(RESULT_ACCESS)] = { 0x00, PIC_UPPER },
250 [C(RESULT_MISS)] = { 0x02, PIC_LOWER, },
253 [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
254 [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
256 [ C(OP_PREFETCH) ] = {
257 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
258 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
263 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
264 [C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
267 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
268 [C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
271 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
272 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
277 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
278 [C(RESULT_MISS)] = { 0x05, PIC_LOWER, },
281 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
282 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
284 [ C(OP_PREFETCH) ] = {
285 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
286 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
291 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
292 [C(RESULT_MISS)] = { 0x04, PIC_LOWER, },
295 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
296 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
298 [ C(OP_PREFETCH) ] = {
299 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
300 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
305 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
306 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
309 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
310 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
312 [ C(OP_PREFETCH) ] = {
313 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
314 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
319 static const struct sparc_pmu niagara1_pmu = {
320 .event_map = niagara1_event_map,
321 .cache_map = &niagara1_cache_map,
322 .max_events = ARRAY_SIZE(niagara1_perfmon_event_map),
330 static const struct perf_event_map niagara2_perfmon_event_map[] = {
331 [PERF_COUNT_HW_CPU_CYCLES] = { 0x02ff, PIC_UPPER | PIC_LOWER },
332 [PERF_COUNT_HW_INSTRUCTIONS] = { 0x02ff, PIC_UPPER | PIC_LOWER },
333 [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0208, PIC_UPPER | PIC_LOWER },
334 [PERF_COUNT_HW_CACHE_MISSES] = { 0x0302, PIC_UPPER | PIC_LOWER },
335 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x0201, PIC_UPPER | PIC_LOWER },
336 [PERF_COUNT_HW_BRANCH_MISSES] = { 0x0202, PIC_UPPER | PIC_LOWER },
339 static const struct perf_event_map *niagara2_event_map(int event_id)
341 return &niagara2_perfmon_event_map[event_id];
344 static const cache_map_t niagara2_cache_map = {
347 [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
348 [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
351 [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
352 [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
355 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
356 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
361 [C(RESULT_ACCESS)] = { 0x02ff, PIC_UPPER | PIC_LOWER, },
362 [C(RESULT_MISS)] = { 0x0301, PIC_UPPER | PIC_LOWER, },
365 [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
366 [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
368 [ C(OP_PREFETCH) ] = {
369 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
370 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
375 [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
376 [C(RESULT_MISS)] = { 0x0330, PIC_UPPER | PIC_LOWER, },
379 [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
380 [C(RESULT_MISS)] = { 0x0320, PIC_UPPER | PIC_LOWER, },
383 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
384 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
389 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
390 [C(RESULT_MISS)] = { 0x0b08, PIC_UPPER | PIC_LOWER, },
393 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
394 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
396 [ C(OP_PREFETCH) ] = {
397 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
398 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
403 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
404 [C(RESULT_MISS)] = { 0xb04, PIC_UPPER | PIC_LOWER, },
407 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
408 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
410 [ C(OP_PREFETCH) ] = {
411 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
412 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
417 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
418 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
421 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
422 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
424 [ C(OP_PREFETCH) ] = {
425 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
426 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
431 static const struct sparc_pmu niagara2_pmu = {
432 .event_map = niagara2_event_map,
433 .cache_map = &niagara2_cache_map,
434 .max_events = ARRAY_SIZE(niagara2_perfmon_event_map),
444 static const struct sparc_pmu *sparc_pmu __read_mostly;
446 static u64 event_encoding(u64 event_id, int idx)
448 if (idx == PIC_UPPER_INDEX)
449 event_id <<= sparc_pmu->upper_shift;
451 event_id <<= sparc_pmu->lower_shift;
455 static u64 mask_for_index(int idx)
457 return event_encoding(sparc_pmu->event_mask, idx);
460 static u64 nop_for_index(int idx)
462 return event_encoding(idx == PIC_UPPER_INDEX ?
463 sparc_pmu->upper_nop :
464 sparc_pmu->lower_nop, idx);
467 static inline void sparc_pmu_enable_event(struct hw_perf_event *hwc, int idx)
469 u64 val, mask = mask_for_index(idx);
471 val = pcr_ops->read();
472 pcr_ops->write((val & ~mask) | hwc->config);
475 static inline void sparc_pmu_disable_event(struct hw_perf_event *hwc, int idx)
477 u64 mask = mask_for_index(idx);
478 u64 nop = nop_for_index(idx);
479 u64 val = pcr_ops->read();
481 pcr_ops->write((val & ~mask) | nop);
484 void hw_perf_enable(void)
486 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
496 val = pcr_ops->read();
498 for (i = 0; i < MAX_HWEVENTS; i++) {
499 struct perf_event *cp = cpuc->events[i];
500 struct hw_perf_event *hwc;
505 val |= hwc->config_base;
511 void hw_perf_disable(void)
513 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
521 val = pcr_ops->read();
522 val &= ~(PCR_UTRACE | PCR_STRACE |
523 sparc_pmu->hv_bit | sparc_pmu->irq_bit);
527 static u32 read_pmc(int idx)
532 if (idx == PIC_UPPER_INDEX)
535 return val & 0xffffffff;
538 static void write_pmc(int idx, u64 val)
540 u64 shift, mask, pic;
543 if (idx == PIC_UPPER_INDEX)
546 mask = ((u64) 0xffffffff) << shift;
555 static int sparc_perf_event_set_period(struct perf_event *event,
556 struct hw_perf_event *hwc, int idx)
558 s64 left = atomic64_read(&hwc->period_left);
559 s64 period = hwc->sample_period;
562 if (unlikely(left <= -period)) {
564 atomic64_set(&hwc->period_left, left);
565 hwc->last_period = period;
569 if (unlikely(left <= 0)) {
571 atomic64_set(&hwc->period_left, left);
572 hwc->last_period = period;
575 if (left > MAX_PERIOD)
578 atomic64_set(&hwc->prev_count, (u64)-left);
580 write_pmc(idx, (u64)(-left) & 0xffffffff);
582 perf_event_update_userpage(event);
587 static int sparc_pmu_enable(struct perf_event *event)
589 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
590 struct hw_perf_event *hwc = &event->hw;
593 if (test_and_set_bit(idx, cpuc->used_mask))
596 sparc_pmu_disable_event(hwc, idx);
598 cpuc->events[idx] = event;
599 set_bit(idx, cpuc->active_mask);
601 sparc_perf_event_set_period(event, hwc, idx);
602 sparc_pmu_enable_event(hwc, idx);
603 perf_event_update_userpage(event);
607 static u64 sparc_perf_event_update(struct perf_event *event,
608 struct hw_perf_event *hwc, int idx)
611 u64 prev_raw_count, new_raw_count;
615 prev_raw_count = atomic64_read(&hwc->prev_count);
616 new_raw_count = read_pmc(idx);
618 if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
619 new_raw_count) != prev_raw_count)
622 delta = (new_raw_count << shift) - (prev_raw_count << shift);
625 atomic64_add(delta, &event->count);
626 atomic64_sub(delta, &hwc->period_left);
628 return new_raw_count;
631 static void sparc_pmu_disable(struct perf_event *event)
633 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
634 struct hw_perf_event *hwc = &event->hw;
637 clear_bit(idx, cpuc->active_mask);
638 sparc_pmu_disable_event(hwc, idx);
642 sparc_perf_event_update(event, hwc, idx);
643 cpuc->events[idx] = NULL;
644 clear_bit(idx, cpuc->used_mask);
646 perf_event_update_userpage(event);
649 static void sparc_pmu_read(struct perf_event *event)
651 struct hw_perf_event *hwc = &event->hw;
652 sparc_perf_event_update(event, hwc, hwc->idx);
655 static void sparc_pmu_unthrottle(struct perf_event *event)
657 struct hw_perf_event *hwc = &event->hw;
658 sparc_pmu_enable_event(hwc, hwc->idx);
661 static atomic_t active_events = ATOMIC_INIT(0);
662 static DEFINE_MUTEX(pmc_grab_mutex);
664 void perf_event_grab_pmc(void)
666 if (atomic_inc_not_zero(&active_events))
669 mutex_lock(&pmc_grab_mutex);
670 if (atomic_read(&active_events) == 0) {
671 if (atomic_read(&nmi_active) > 0) {
672 on_each_cpu(stop_nmi_watchdog, NULL, 1);
673 BUG_ON(atomic_read(&nmi_active) != 0);
675 atomic_inc(&active_events);
677 mutex_unlock(&pmc_grab_mutex);
680 void perf_event_release_pmc(void)
682 if (atomic_dec_and_mutex_lock(&active_events, &pmc_grab_mutex)) {
683 if (atomic_read(&nmi_active) == 0)
684 on_each_cpu(start_nmi_watchdog, NULL, 1);
685 mutex_unlock(&pmc_grab_mutex);
689 static const struct perf_event_map *sparc_map_cache_event(u64 config)
691 unsigned int cache_type, cache_op, cache_result;
692 const struct perf_event_map *pmap;
694 if (!sparc_pmu->cache_map)
695 return ERR_PTR(-ENOENT);
697 cache_type = (config >> 0) & 0xff;
698 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
699 return ERR_PTR(-EINVAL);
701 cache_op = (config >> 8) & 0xff;
702 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
703 return ERR_PTR(-EINVAL);
705 cache_result = (config >> 16) & 0xff;
706 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
707 return ERR_PTR(-EINVAL);
709 pmap = &((*sparc_pmu->cache_map)[cache_type][cache_op][cache_result]);
711 if (pmap->encoding == CACHE_OP_UNSUPPORTED)
712 return ERR_PTR(-ENOENT);
714 if (pmap->encoding == CACHE_OP_NONSENSE)
715 return ERR_PTR(-EINVAL);
720 static void hw_perf_event_destroy(struct perf_event *event)
722 perf_event_release_pmc();
725 /* Make sure all events can be scheduled into the hardware at
726 * the same time. This is simplified by the fact that we only
727 * need to support 2 simultaneous HW events.
729 static int sparc_check_constraints(unsigned long *events, int n_ev)
731 if (n_ev <= perf_max_events) {
738 perf_event_decode(events[0], &dummy, &msk1);
739 perf_event_decode(events[1], &dummy, &msk2);
741 /* If both events can go on any counter, OK. */
742 if (msk1 == (PIC_UPPER | PIC_LOWER) &&
743 msk2 == (PIC_UPPER | PIC_LOWER))
746 /* If one event is limited to a specific counter,
747 * and the other can go on both, OK.
749 if ((msk1 == PIC_UPPER || msk1 == PIC_LOWER) &&
750 msk2 == (PIC_UPPER | PIC_LOWER))
752 if ((msk2 == PIC_UPPER || msk2 == PIC_LOWER) &&
753 msk1 == (PIC_UPPER | PIC_LOWER))
756 /* If the events are fixed to different counters, OK. */
757 if ((msk1 == PIC_UPPER && msk2 == PIC_LOWER) ||
758 (msk1 == PIC_LOWER && msk2 == PIC_UPPER))
761 /* Otherwise, there is a conflict. */
767 static int check_excludes(struct perf_event **evts, int n_prev, int n_new)
769 int eu = 0, ek = 0, eh = 0;
770 struct perf_event *event;
778 for (i = 0; i < n; i++) {
781 eu = event->attr.exclude_user;
782 ek = event->attr.exclude_kernel;
783 eh = event->attr.exclude_hv;
785 } else if (event->attr.exclude_user != eu ||
786 event->attr.exclude_kernel != ek ||
787 event->attr.exclude_hv != eh) {
795 static int collect_events(struct perf_event *group, int max_count,
796 struct perf_event *evts[], unsigned long *events)
798 struct perf_event *event;
801 if (!is_software_event(group)) {
805 events[n++] = group->hw.event_base;
807 list_for_each_entry(event, &group->sibling_list, group_entry) {
808 if (!is_software_event(event) &&
809 event->state != PERF_EVENT_STATE_OFF) {
813 events[n++] = event->hw.event_base;
819 static int __hw_perf_event_init(struct perf_event *event)
821 struct perf_event_attr *attr = &event->attr;
822 struct perf_event *evts[MAX_HWEVENTS];
823 struct hw_perf_event *hwc = &event->hw;
824 unsigned long events[MAX_HWEVENTS];
825 const struct perf_event_map *pmap;
829 if (atomic_read(&nmi_active) < 0)
832 if (attr->type == PERF_TYPE_HARDWARE) {
833 if (attr->config >= sparc_pmu->max_events)
835 pmap = sparc_pmu->event_map(attr->config);
836 } else if (attr->type == PERF_TYPE_HW_CACHE) {
837 pmap = sparc_map_cache_event(attr->config);
839 return PTR_ERR(pmap);
843 /* We save the enable bits in the config_base. So to
844 * turn off sampling just write 'config', and to enable
845 * things write 'config | config_base'.
847 hwc->config_base = sparc_pmu->irq_bit;
848 if (!attr->exclude_user)
849 hwc->config_base |= PCR_UTRACE;
850 if (!attr->exclude_kernel)
851 hwc->config_base |= PCR_STRACE;
852 if (!attr->exclude_hv)
853 hwc->config_base |= sparc_pmu->hv_bit;
855 hwc->event_base = perf_event_encode(pmap);
857 enc = pmap->encoding;
860 if (event->group_leader != event) {
861 n = collect_events(event->group_leader,
867 events[n] = hwc->event_base;
870 if (check_excludes(evts, n, 1))
873 if (sparc_check_constraints(events, n + 1))
876 /* Try to do all error checking before this point, as unwinding
877 * state after grabbing the PMC is difficult.
879 perf_event_grab_pmc();
880 event->destroy = hw_perf_event_destroy;
882 if (!hwc->sample_period) {
883 hwc->sample_period = MAX_PERIOD;
884 hwc->last_period = hwc->sample_period;
885 atomic64_set(&hwc->period_left, hwc->sample_period);
888 if (pmap->pic_mask & PIC_UPPER) {
889 hwc->idx = PIC_UPPER_INDEX;
890 enc <<= sparc_pmu->upper_shift;
892 hwc->idx = PIC_LOWER_INDEX;
893 enc <<= sparc_pmu->lower_shift;
900 static const struct pmu pmu = {
901 .enable = sparc_pmu_enable,
902 .disable = sparc_pmu_disable,
903 .read = sparc_pmu_read,
904 .unthrottle = sparc_pmu_unthrottle,
907 const struct pmu *hw_perf_event_init(struct perf_event *event)
909 int err = __hw_perf_event_init(event);
916 void perf_event_print_debug(void)
925 local_irq_save(flags);
927 cpu = smp_processor_id();
929 pcr = pcr_ops->read();
933 pr_info("CPU#%d: PCR[%016llx] PIC[%016llx]\n",
936 local_irq_restore(flags);
939 static int __kprobes perf_event_nmi_handler(struct notifier_block *self,
940 unsigned long cmd, void *__args)
942 struct die_args *args = __args;
943 struct perf_sample_data data;
944 struct cpu_hw_events *cpuc;
945 struct pt_regs *regs;
948 if (!atomic_read(&active_events))
963 cpuc = &__get_cpu_var(cpu_hw_events);
964 for (idx = 0; idx < MAX_HWEVENTS; idx++) {
965 struct perf_event *event = cpuc->events[idx];
966 struct hw_perf_event *hwc;
969 if (!test_bit(idx, cpuc->active_mask))
972 val = sparc_perf_event_update(event, hwc, idx);
973 if (val & (1ULL << 31))
976 data.period = event->hw.last_period;
977 if (!sparc_perf_event_set_period(event, hwc, idx))
980 if (perf_event_overflow(event, 1, &data, regs))
981 sparc_pmu_disable_event(hwc, idx);
987 static __read_mostly struct notifier_block perf_event_nmi_notifier = {
988 .notifier_call = perf_event_nmi_handler,
991 static bool __init supported_pmu(void)
993 if (!strcmp(sparc_pmu_type, "ultra3") ||
994 !strcmp(sparc_pmu_type, "ultra3+") ||
995 !strcmp(sparc_pmu_type, "ultra3i") ||
996 !strcmp(sparc_pmu_type, "ultra4+")) {
997 sparc_pmu = &ultra3_pmu;
1000 if (!strcmp(sparc_pmu_type, "niagara")) {
1001 sparc_pmu = &niagara1_pmu;
1004 if (!strcmp(sparc_pmu_type, "niagara2")) {
1005 sparc_pmu = &niagara2_pmu;
1011 void __init init_hw_perf_events(void)
1013 pr_info("Performance events: ");
1015 if (!supported_pmu()) {
1016 pr_cont("No support for PMU type '%s'\n", sparc_pmu_type);
1020 pr_cont("Supported PMU type is '%s'\n", sparc_pmu_type);
1022 /* All sparc64 PMUs currently have 2 events. But this simple
1023 * driver only supports one active event at a time.
1025 perf_max_events = 1;
1027 register_die_notifier(&perf_event_nmi_notifier);