1 // SPDX-License-Identifier: GPL-2.0
2 /* Performance event support for sparc64.
6 * This code is based almost entirely upon the x86 perf event
10 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
11 * Copyright (C) 2009 Jaswinder Singh Rajput
12 * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
13 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra
16 #include <linux/perf_event.h>
17 #include <linux/kprobes.h>
18 #include <linux/ftrace.h>
19 #include <linux/kernel.h>
20 #include <linux/kdebug.h>
21 #include <linux/mutex.h>
23 #include <asm/stacktrace.h>
24 #include <asm/cpudata.h>
25 #include <linux/uaccess.h>
26 #include <linux/atomic.h>
27 #include <linux/sched/clock.h>
30 #include <asm/cacheflush.h>
35 /* Two classes of sparc64 chips currently exist. All of which have
36 * 32-bit counters which can generate overflow interrupts on the
37 * transition from 0xffffffff to 0.
39 * All chips upto and including SPARC-T3 have two performance
40 * counters. The two 32-bit counters are accessed in one go using a
41 * single 64-bit register.
43 * On these older chips both counters are controlled using a single
44 * control register. The only way to stop all sampling is to clear
45 * all of the context (user, supervisor, hypervisor) sampling enable
46 * bits. But these bits apply to both counters, thus the two counters
47 * can't be enabled/disabled individually.
49 * Furthermore, the control register on these older chips have two
50 * event fields, one for each of the two counters. It's thus nearly
51 * impossible to have one counter going while keeping the other one
52 * stopped. Therefore it is possible to get overflow interrupts for
53 * counters not currently "in use" and that condition must be checked
54 * in the overflow interrupt handler.
56 * So we use a hack, in that we program inactive counters with the
57 * "sw_count0" and "sw_count1" events. These count how many times
58 * the instruction "sethi %hi(0xfc000), %g0" is executed. It's an
59 * unusual way to encode a NOP and therefore will not trigger in
62 * Starting with SPARC-T4 we have one control register per counter.
63 * And the counters are stored in individual registers. The registers
64 * for the counters are 64-bit but only a 32-bit counter is
65 * implemented. The event selections on SPARC-T4 lack any
66 * restrictions, therefore we can elide all of the complicated
67 * conflict resolution code we have for SPARC-T3 and earlier chips.
70 #define MAX_HWEVENTS 4
72 #define MAX_PERIOD ((1UL << 32) - 1)
74 #define PIC_UPPER_INDEX 0
75 #define PIC_LOWER_INDEX 1
76 #define PIC_NO_INDEX -1
78 struct cpu_hw_events {
79 /* Number of events currently scheduled onto this cpu.
80 * This tells how many entries in the arrays below
85 /* Number of new events added since the last hw_perf_disable().
86 * This works because the perf event layer always adds new
87 * events inside of a perf_{disable,enable}() sequence.
91 /* Array of events current scheduled on this cpu. */
92 struct perf_event *event[MAX_HWEVENTS];
94 /* Array of encoded longs, specifying the %pcr register
95 * encoding and the mask of PIC counters this even can
96 * be scheduled on. See perf_event_encode() et al.
98 unsigned long events[MAX_HWEVENTS];
100 /* The current counter index assigned to an event. When the
101 * event hasn't been programmed into the cpu yet, this will
102 * hold PIC_NO_INDEX. The event->hw.idx value tells us where
103 * we ought to schedule the event.
105 int current_idx[MAX_HWEVENTS];
107 /* Software copy of %pcr register(s) on this cpu. */
108 u64 pcr[MAX_HWEVENTS];
110 /* Enabled/disable state. */
113 unsigned int txn_flags;
115 static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { .enabled = 1, };
117 /* An event map describes the characteristics of a performance
118 * counter event. In particular it gives the encoding as well as
119 * a mask telling which counters the event can be measured on.
121 * The mask is unused on SPARC-T4 and later.
123 struct perf_event_map {
126 #define PIC_NONE 0x00
127 #define PIC_UPPER 0x01
128 #define PIC_LOWER 0x02
131 /* Encode a perf_event_map entry into a long. */
132 static unsigned long perf_event_encode(const struct perf_event_map *pmap)
134 return ((unsigned long) pmap->encoding << 16) | pmap->pic_mask;
137 static u8 perf_event_get_msk(unsigned long val)
142 static u64 perf_event_get_enc(unsigned long val)
147 #define C(x) PERF_COUNT_HW_CACHE_##x
149 #define CACHE_OP_UNSUPPORTED 0xfffe
150 #define CACHE_OP_NONSENSE 0xffff
152 typedef struct perf_event_map cache_map_t
153 [PERF_COUNT_HW_CACHE_MAX]
154 [PERF_COUNT_HW_CACHE_OP_MAX]
155 [PERF_COUNT_HW_CACHE_RESULT_MAX];
158 const struct perf_event_map *(*event_map)(int);
159 const cache_map_t *cache_map;
161 u32 (*read_pmc)(int);
162 void (*write_pmc)(int, u64);
173 #define SPARC_PMU_ALL_EXCLUDES_SAME 0x00000001
174 #define SPARC_PMU_HAS_CONFLICTS 0x00000002
180 static u32 sparc_default_read_pmc(int idx)
184 val = pcr_ops->read_pic(0);
185 if (idx == PIC_UPPER_INDEX)
188 return val & 0xffffffff;
191 static void sparc_default_write_pmc(int idx, u64 val)
193 u64 shift, mask, pic;
196 if (idx == PIC_UPPER_INDEX)
199 mask = ((u64) 0xffffffff) << shift;
202 pic = pcr_ops->read_pic(0);
205 pcr_ops->write_pic(0, pic);
208 static const struct perf_event_map ultra3_perfmon_event_map[] = {
209 [PERF_COUNT_HW_CPU_CYCLES] = { 0x0000, PIC_UPPER | PIC_LOWER },
210 [PERF_COUNT_HW_INSTRUCTIONS] = { 0x0001, PIC_UPPER | PIC_LOWER },
211 [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0009, PIC_LOWER },
212 [PERF_COUNT_HW_CACHE_MISSES] = { 0x0009, PIC_UPPER },
215 static const struct perf_event_map *ultra3_event_map(int event_id)
217 return &ultra3_perfmon_event_map[event_id];
220 static const cache_map_t ultra3_cache_map = {
223 [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
224 [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
227 [C(RESULT_ACCESS)] = { 0x0a, PIC_LOWER },
228 [C(RESULT_MISS)] = { 0x0a, PIC_UPPER },
231 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
232 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
237 [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
238 [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
241 [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
242 [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
244 [ C(OP_PREFETCH) ] = {
245 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
246 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
251 [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER, },
252 [C(RESULT_MISS)] = { 0x0c, PIC_UPPER, },
255 [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER },
256 [C(RESULT_MISS)] = { 0x0c, PIC_UPPER },
259 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
260 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
265 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
266 [C(RESULT_MISS)] = { 0x12, PIC_UPPER, },
269 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
270 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
272 [ C(OP_PREFETCH) ] = {
273 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
274 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
279 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
280 [C(RESULT_MISS)] = { 0x11, PIC_UPPER, },
283 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
284 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
286 [ C(OP_PREFETCH) ] = {
287 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
288 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
293 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
294 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
297 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
298 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
300 [ C(OP_PREFETCH) ] = {
301 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
302 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
307 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
308 [C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
311 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
312 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
314 [ C(OP_PREFETCH) ] = {
315 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
316 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
321 static const struct sparc_pmu ultra3_pmu = {
322 .event_map = ultra3_event_map,
323 .cache_map = &ultra3_cache_map,
324 .max_events = ARRAY_SIZE(ultra3_perfmon_event_map),
325 .read_pmc = sparc_default_read_pmc,
326 .write_pmc = sparc_default_write_pmc,
330 .user_bit = PCR_UTRACE,
331 .priv_bit = PCR_STRACE,
334 .flags = (SPARC_PMU_ALL_EXCLUDES_SAME |
335 SPARC_PMU_HAS_CONFLICTS),
341 /* Niagara1 is very limited. The upper PIC is hard-locked to count
342 * only instructions, so it is free running which creates all kinds of
343 * problems. Some hardware designs make one wonder if the creator
344 * even looked at how this stuff gets used by software.
346 static const struct perf_event_map niagara1_perfmon_event_map[] = {
347 [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, PIC_UPPER },
348 [PERF_COUNT_HW_INSTRUCTIONS] = { 0x00, PIC_UPPER },
349 [PERF_COUNT_HW_CACHE_REFERENCES] = { 0, PIC_NONE },
350 [PERF_COUNT_HW_CACHE_MISSES] = { 0x03, PIC_LOWER },
353 static const struct perf_event_map *niagara1_event_map(int event_id)
355 return &niagara1_perfmon_event_map[event_id];
358 static const cache_map_t niagara1_cache_map = {
361 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
362 [C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
365 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
366 [C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
369 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
370 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
375 [C(RESULT_ACCESS)] = { 0x00, PIC_UPPER },
376 [C(RESULT_MISS)] = { 0x02, PIC_LOWER, },
379 [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
380 [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
382 [ C(OP_PREFETCH) ] = {
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)] = { 0x07, PIC_LOWER, },
393 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
394 [C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
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)] = { 0x05, 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)] = { 0x04, PIC_LOWER, },
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 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
432 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
435 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
436 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
438 [ C(OP_PREFETCH) ] = {
439 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
440 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
445 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
446 [C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
449 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
450 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
452 [ C(OP_PREFETCH) ] = {
453 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
454 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
459 static const struct sparc_pmu niagara1_pmu = {
460 .event_map = niagara1_event_map,
461 .cache_map = &niagara1_cache_map,
462 .max_events = ARRAY_SIZE(niagara1_perfmon_event_map),
463 .read_pmc = sparc_default_read_pmc,
464 .write_pmc = sparc_default_write_pmc,
468 .user_bit = PCR_UTRACE,
469 .priv_bit = PCR_STRACE,
472 .flags = (SPARC_PMU_ALL_EXCLUDES_SAME |
473 SPARC_PMU_HAS_CONFLICTS),
479 static const struct perf_event_map niagara2_perfmon_event_map[] = {
480 [PERF_COUNT_HW_CPU_CYCLES] = { 0x02ff, PIC_UPPER | PIC_LOWER },
481 [PERF_COUNT_HW_INSTRUCTIONS] = { 0x02ff, PIC_UPPER | PIC_LOWER },
482 [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0208, PIC_UPPER | PIC_LOWER },
483 [PERF_COUNT_HW_CACHE_MISSES] = { 0x0302, PIC_UPPER | PIC_LOWER },
484 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x0201, PIC_UPPER | PIC_LOWER },
485 [PERF_COUNT_HW_BRANCH_MISSES] = { 0x0202, PIC_UPPER | PIC_LOWER },
488 static const struct perf_event_map *niagara2_event_map(int event_id)
490 return &niagara2_perfmon_event_map[event_id];
493 static const cache_map_t niagara2_cache_map = {
496 [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
497 [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
500 [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
501 [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
504 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
505 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
510 [C(RESULT_ACCESS)] = { 0x02ff, PIC_UPPER | PIC_LOWER, },
511 [C(RESULT_MISS)] = { 0x0301, PIC_UPPER | PIC_LOWER, },
514 [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
515 [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
517 [ C(OP_PREFETCH) ] = {
518 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
519 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
524 [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
525 [C(RESULT_MISS)] = { 0x0330, PIC_UPPER | PIC_LOWER, },
528 [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
529 [C(RESULT_MISS)] = { 0x0320, PIC_UPPER | PIC_LOWER, },
532 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
533 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
538 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
539 [C(RESULT_MISS)] = { 0x0b08, PIC_UPPER | PIC_LOWER, },
542 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
543 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
545 [ C(OP_PREFETCH) ] = {
546 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
547 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
552 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
553 [C(RESULT_MISS)] = { 0xb04, PIC_UPPER | PIC_LOWER, },
556 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
557 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
559 [ C(OP_PREFETCH) ] = {
560 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
561 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
566 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
567 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
570 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
571 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
573 [ C(OP_PREFETCH) ] = {
574 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
575 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
580 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
581 [C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
584 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
585 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
587 [ C(OP_PREFETCH) ] = {
588 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
589 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
594 static const struct sparc_pmu niagara2_pmu = {
595 .event_map = niagara2_event_map,
596 .cache_map = &niagara2_cache_map,
597 .max_events = ARRAY_SIZE(niagara2_perfmon_event_map),
598 .read_pmc = sparc_default_read_pmc,
599 .write_pmc = sparc_default_write_pmc,
603 .user_bit = PCR_UTRACE,
604 .priv_bit = PCR_STRACE,
605 .hv_bit = PCR_N2_HTRACE,
609 .flags = (SPARC_PMU_ALL_EXCLUDES_SAME |
610 SPARC_PMU_HAS_CONFLICTS),
616 static const struct perf_event_map niagara4_perfmon_event_map[] = {
617 [PERF_COUNT_HW_CPU_CYCLES] = { (26 << 6) },
618 [PERF_COUNT_HW_INSTRUCTIONS] = { (3 << 6) | 0x3f },
619 [PERF_COUNT_HW_CACHE_REFERENCES] = { (3 << 6) | 0x04 },
620 [PERF_COUNT_HW_CACHE_MISSES] = { (16 << 6) | 0x07 },
621 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { (4 << 6) | 0x01 },
622 [PERF_COUNT_HW_BRANCH_MISSES] = { (25 << 6) | 0x0f },
625 static const struct perf_event_map *niagara4_event_map(int event_id)
627 return &niagara4_perfmon_event_map[event_id];
630 static const cache_map_t niagara4_cache_map = {
633 [C(RESULT_ACCESS)] = { (3 << 6) | 0x04 },
634 [C(RESULT_MISS)] = { (16 << 6) | 0x07 },
637 [C(RESULT_ACCESS)] = { (3 << 6) | 0x08 },
638 [C(RESULT_MISS)] = { (16 << 6) | 0x07 },
641 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
642 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
647 [C(RESULT_ACCESS)] = { (3 << 6) | 0x3f },
648 [C(RESULT_MISS)] = { (11 << 6) | 0x03 },
651 [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
652 [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
654 [ C(OP_PREFETCH) ] = {
655 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
656 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
661 [C(RESULT_ACCESS)] = { (3 << 6) | 0x04 },
662 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
665 [C(RESULT_ACCESS)] = { (3 << 6) | 0x08 },
666 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
669 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
670 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
675 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
676 [C(RESULT_MISS)] = { (17 << 6) | 0x3f },
679 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
680 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
682 [ C(OP_PREFETCH) ] = {
683 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
684 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
689 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
690 [C(RESULT_MISS)] = { (6 << 6) | 0x3f },
693 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
694 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
696 [ C(OP_PREFETCH) ] = {
697 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
698 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
703 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
704 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
707 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
708 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
710 [ C(OP_PREFETCH) ] = {
711 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
712 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
717 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
718 [C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
721 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
722 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
724 [ C(OP_PREFETCH) ] = {
725 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
726 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
731 static u32 sparc_vt_read_pmc(int idx)
733 u64 val = pcr_ops->read_pic(idx);
735 return val & 0xffffffff;
738 static void sparc_vt_write_pmc(int idx, u64 val)
742 pcr = pcr_ops->read_pcr(idx);
743 /* ensure ov and ntc are reset */
744 pcr &= ~(PCR_N4_OV | PCR_N4_NTC);
746 pcr_ops->write_pic(idx, val & 0xffffffff);
748 pcr_ops->write_pcr(idx, pcr);
751 static const struct sparc_pmu niagara4_pmu = {
752 .event_map = niagara4_event_map,
753 .cache_map = &niagara4_cache_map,
754 .max_events = ARRAY_SIZE(niagara4_perfmon_event_map),
755 .read_pmc = sparc_vt_read_pmc,
756 .write_pmc = sparc_vt_write_pmc,
760 .user_bit = PCR_N4_UTRACE,
761 .priv_bit = PCR_N4_STRACE,
763 /* We explicitly don't support hypervisor tracing. The T4
764 * generates the overflow event for precise events via a trap
765 * which will not be generated (ie. it's completely lost) if
766 * we happen to be in the hypervisor when the event triggers.
767 * Essentially, the overflow event reporting is completely
768 * unusable when you have hypervisor mode tracing enabled.
772 .irq_bit = PCR_N4_TOE,
781 static const struct sparc_pmu sparc_m7_pmu = {
782 .event_map = niagara4_event_map,
783 .cache_map = &niagara4_cache_map,
784 .max_events = ARRAY_SIZE(niagara4_perfmon_event_map),
785 .read_pmc = sparc_vt_read_pmc,
786 .write_pmc = sparc_vt_write_pmc,
790 .user_bit = PCR_N4_UTRACE,
791 .priv_bit = PCR_N4_STRACE,
793 /* We explicitly don't support hypervisor tracing. */
796 .irq_bit = PCR_N4_TOE,
804 static const struct sparc_pmu *sparc_pmu __read_mostly;
806 static u64 event_encoding(u64 event_id, int idx)
808 if (idx == PIC_UPPER_INDEX)
809 event_id <<= sparc_pmu->upper_shift;
811 event_id <<= sparc_pmu->lower_shift;
815 static u64 mask_for_index(int idx)
817 return event_encoding(sparc_pmu->event_mask, idx);
820 static u64 nop_for_index(int idx)
822 return event_encoding(idx == PIC_UPPER_INDEX ?
823 sparc_pmu->upper_nop :
824 sparc_pmu->lower_nop, idx);
827 static inline void sparc_pmu_enable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
829 u64 enc, val, mask = mask_for_index(idx);
832 if (sparc_pmu->num_pcrs > 1)
835 enc = perf_event_get_enc(cpuc->events[idx]);
837 val = cpuc->pcr[pcr_index];
839 val |= event_encoding(enc, idx);
840 cpuc->pcr[pcr_index] = val;
842 pcr_ops->write_pcr(pcr_index, cpuc->pcr[pcr_index]);
845 static inline void sparc_pmu_disable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
847 u64 mask = mask_for_index(idx);
848 u64 nop = nop_for_index(idx);
852 if (sparc_pmu->num_pcrs > 1)
855 val = cpuc->pcr[pcr_index];
858 cpuc->pcr[pcr_index] = val;
860 pcr_ops->write_pcr(pcr_index, cpuc->pcr[pcr_index]);
863 static u64 sparc_perf_event_update(struct perf_event *event,
864 struct hw_perf_event *hwc, int idx)
867 u64 prev_raw_count, new_raw_count;
871 prev_raw_count = local64_read(&hwc->prev_count);
872 new_raw_count = sparc_pmu->read_pmc(idx);
874 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
875 new_raw_count) != prev_raw_count)
878 delta = (new_raw_count << shift) - (prev_raw_count << shift);
881 local64_add(delta, &event->count);
882 local64_sub(delta, &hwc->period_left);
884 return new_raw_count;
887 static int sparc_perf_event_set_period(struct perf_event *event,
888 struct hw_perf_event *hwc, int idx)
890 s64 left = local64_read(&hwc->period_left);
891 s64 period = hwc->sample_period;
894 if (unlikely(left <= -period)) {
896 local64_set(&hwc->period_left, left);
897 hwc->last_period = period;
901 if (unlikely(left <= 0)) {
903 local64_set(&hwc->period_left, left);
904 hwc->last_period = period;
907 if (left > MAX_PERIOD)
910 local64_set(&hwc->prev_count, (u64)-left);
912 sparc_pmu->write_pmc(idx, (u64)(-left) & 0xffffffff);
914 perf_event_update_userpage(event);
919 static void read_in_all_counters(struct cpu_hw_events *cpuc)
923 for (i = 0; i < cpuc->n_events; i++) {
924 struct perf_event *cp = cpuc->event[i];
926 if (cpuc->current_idx[i] != PIC_NO_INDEX &&
927 cpuc->current_idx[i] != cp->hw.idx) {
928 sparc_perf_event_update(cp, &cp->hw,
929 cpuc->current_idx[i]);
930 cpuc->current_idx[i] = PIC_NO_INDEX;
931 if (cp->hw.state & PERF_HES_STOPPED)
932 cp->hw.state |= PERF_HES_ARCH;
937 /* On this PMU all PICs are programmed using a single PCR. Calculate
938 * the combined control register value.
940 * For such chips we require that all of the events have the same
941 * configuration, so just fetch the settings from the first entry.
943 static void calculate_single_pcr(struct cpu_hw_events *cpuc)
950 /* Assign to counters all unassigned events. */
951 for (i = 0; i < cpuc->n_events; i++) {
952 struct perf_event *cp = cpuc->event[i];
953 struct hw_perf_event *hwc = &cp->hw;
957 if (cpuc->current_idx[i] != PIC_NO_INDEX)
960 sparc_perf_event_set_period(cp, hwc, idx);
961 cpuc->current_idx[i] = idx;
963 enc = perf_event_get_enc(cpuc->events[i]);
964 cpuc->pcr[0] &= ~mask_for_index(idx);
965 if (hwc->state & PERF_HES_ARCH) {
966 cpuc->pcr[0] |= nop_for_index(idx);
968 cpuc->pcr[0] |= event_encoding(enc, idx);
973 cpuc->pcr[0] |= cpuc->event[0]->hw.config_base;
976 static void sparc_pmu_start(struct perf_event *event, int flags);
978 /* On this PMU each PIC has it's own PCR control register. */
979 static void calculate_multiple_pcrs(struct cpu_hw_events *cpuc)
986 for (i = 0; i < cpuc->n_events; i++) {
987 struct perf_event *cp = cpuc->event[i];
988 struct hw_perf_event *hwc = &cp->hw;
991 if (cpuc->current_idx[i] != PIC_NO_INDEX)
994 cpuc->current_idx[i] = idx;
996 if (cp->hw.state & PERF_HES_ARCH)
999 sparc_pmu_start(cp, PERF_EF_RELOAD);
1002 for (i = 0; i < cpuc->n_events; i++) {
1003 struct perf_event *cp = cpuc->event[i];
1004 int idx = cp->hw.idx;
1006 cpuc->pcr[idx] |= cp->hw.config_base;
1010 /* If performance event entries have been added, move existing events
1011 * around (if necessary) and then assign new entries to counters.
1013 static void update_pcrs_for_enable(struct cpu_hw_events *cpuc)
1016 read_in_all_counters(cpuc);
1018 if (sparc_pmu->num_pcrs == 1) {
1019 calculate_single_pcr(cpuc);
1021 calculate_multiple_pcrs(cpuc);
1025 static void sparc_pmu_enable(struct pmu *pmu)
1027 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1037 update_pcrs_for_enable(cpuc);
1039 for (i = 0; i < sparc_pmu->num_pcrs; i++)
1040 pcr_ops->write_pcr(i, cpuc->pcr[i]);
1043 static void sparc_pmu_disable(struct pmu *pmu)
1045 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1054 for (i = 0; i < sparc_pmu->num_pcrs; i++) {
1055 u64 val = cpuc->pcr[i];
1057 val &= ~(sparc_pmu->user_bit | sparc_pmu->priv_bit |
1058 sparc_pmu->hv_bit | sparc_pmu->irq_bit);
1060 pcr_ops->write_pcr(i, cpuc->pcr[i]);
1064 static int active_event_index(struct cpu_hw_events *cpuc,
1065 struct perf_event *event)
1069 for (i = 0; i < cpuc->n_events; i++) {
1070 if (cpuc->event[i] == event)
1073 BUG_ON(i == cpuc->n_events);
1074 return cpuc->current_idx[i];
1077 static void sparc_pmu_start(struct perf_event *event, int flags)
1079 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1080 int idx = active_event_index(cpuc, event);
1082 if (flags & PERF_EF_RELOAD) {
1083 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1084 sparc_perf_event_set_period(event, &event->hw, idx);
1087 event->hw.state = 0;
1089 sparc_pmu_enable_event(cpuc, &event->hw, idx);
1091 perf_event_update_userpage(event);
1094 static void sparc_pmu_stop(struct perf_event *event, int flags)
1096 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1097 int idx = active_event_index(cpuc, event);
1099 if (!(event->hw.state & PERF_HES_STOPPED)) {
1100 sparc_pmu_disable_event(cpuc, &event->hw, idx);
1101 event->hw.state |= PERF_HES_STOPPED;
1104 if (!(event->hw.state & PERF_HES_UPTODATE) && (flags & PERF_EF_UPDATE)) {
1105 sparc_perf_event_update(event, &event->hw, idx);
1106 event->hw.state |= PERF_HES_UPTODATE;
1110 static void sparc_pmu_del(struct perf_event *event, int _flags)
1112 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1113 unsigned long flags;
1116 local_irq_save(flags);
1118 for (i = 0; i < cpuc->n_events; i++) {
1119 if (event == cpuc->event[i]) {
1120 /* Absorb the final count and turn off the
1123 sparc_pmu_stop(event, PERF_EF_UPDATE);
1125 /* Shift remaining entries down into
1126 * the existing slot.
1128 while (++i < cpuc->n_events) {
1129 cpuc->event[i - 1] = cpuc->event[i];
1130 cpuc->events[i - 1] = cpuc->events[i];
1131 cpuc->current_idx[i - 1] =
1132 cpuc->current_idx[i];
1135 perf_event_update_userpage(event);
1142 local_irq_restore(flags);
1145 static void sparc_pmu_read(struct perf_event *event)
1147 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1148 int idx = active_event_index(cpuc, event);
1149 struct hw_perf_event *hwc = &event->hw;
1151 sparc_perf_event_update(event, hwc, idx);
1154 static atomic_t active_events = ATOMIC_INIT(0);
1155 static DEFINE_MUTEX(pmc_grab_mutex);
1157 static void perf_stop_nmi_watchdog(void *unused)
1159 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1162 stop_nmi_watchdog(NULL);
1163 for (i = 0; i < sparc_pmu->num_pcrs; i++)
1164 cpuc->pcr[i] = pcr_ops->read_pcr(i);
1167 static void perf_event_grab_pmc(void)
1169 if (atomic_inc_not_zero(&active_events))
1172 mutex_lock(&pmc_grab_mutex);
1173 if (atomic_read(&active_events) == 0) {
1174 if (atomic_read(&nmi_active) > 0) {
1175 on_each_cpu(perf_stop_nmi_watchdog, NULL, 1);
1176 BUG_ON(atomic_read(&nmi_active) != 0);
1178 atomic_inc(&active_events);
1180 mutex_unlock(&pmc_grab_mutex);
1183 static void perf_event_release_pmc(void)
1185 if (atomic_dec_and_mutex_lock(&active_events, &pmc_grab_mutex)) {
1186 if (atomic_read(&nmi_active) == 0)
1187 on_each_cpu(start_nmi_watchdog, NULL, 1);
1188 mutex_unlock(&pmc_grab_mutex);
1192 static const struct perf_event_map *sparc_map_cache_event(u64 config)
1194 unsigned int cache_type, cache_op, cache_result;
1195 const struct perf_event_map *pmap;
1197 if (!sparc_pmu->cache_map)
1198 return ERR_PTR(-ENOENT);
1200 cache_type = (config >> 0) & 0xff;
1201 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
1202 return ERR_PTR(-EINVAL);
1204 cache_op = (config >> 8) & 0xff;
1205 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
1206 return ERR_PTR(-EINVAL);
1208 cache_result = (config >> 16) & 0xff;
1209 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
1210 return ERR_PTR(-EINVAL);
1212 pmap = &((*sparc_pmu->cache_map)[cache_type][cache_op][cache_result]);
1214 if (pmap->encoding == CACHE_OP_UNSUPPORTED)
1215 return ERR_PTR(-ENOENT);
1217 if (pmap->encoding == CACHE_OP_NONSENSE)
1218 return ERR_PTR(-EINVAL);
1223 static void hw_perf_event_destroy(struct perf_event *event)
1225 perf_event_release_pmc();
1228 /* Make sure all events can be scheduled into the hardware at
1229 * the same time. This is simplified by the fact that we only
1230 * need to support 2 simultaneous HW events.
1232 * As a side effect, the evts[]->hw.idx values will be assigned
1233 * on success. These are pending indexes. When the events are
1234 * actually programmed into the chip, these values will propagate
1235 * to the per-cpu cpuc->current_idx[] slots, see the code in
1236 * maybe_change_configuration() for details.
1238 static int sparc_check_constraints(struct perf_event **evts,
1239 unsigned long *events, int n_ev)
1241 u8 msk0 = 0, msk1 = 0;
1244 /* This case is possible when we are invoked from
1245 * hw_perf_group_sched_in().
1250 if (n_ev > sparc_pmu->max_hw_events)
1253 if (!(sparc_pmu->flags & SPARC_PMU_HAS_CONFLICTS)) {
1256 for (i = 0; i < n_ev; i++)
1257 evts[i]->hw.idx = i;
1261 msk0 = perf_event_get_msk(events[0]);
1263 if (msk0 & PIC_LOWER)
1268 msk1 = perf_event_get_msk(events[1]);
1270 /* If both events can go on any counter, OK. */
1271 if (msk0 == (PIC_UPPER | PIC_LOWER) &&
1272 msk1 == (PIC_UPPER | PIC_LOWER))
1275 /* If one event is limited to a specific counter,
1276 * and the other can go on both, OK.
1278 if ((msk0 == PIC_UPPER || msk0 == PIC_LOWER) &&
1279 msk1 == (PIC_UPPER | PIC_LOWER)) {
1280 if (msk0 & PIC_LOWER)
1285 if ((msk1 == PIC_UPPER || msk1 == PIC_LOWER) &&
1286 msk0 == (PIC_UPPER | PIC_LOWER)) {
1287 if (msk1 & PIC_UPPER)
1292 /* If the events are fixed to different counters, OK. */
1293 if ((msk0 == PIC_UPPER && msk1 == PIC_LOWER) ||
1294 (msk0 == PIC_LOWER && msk1 == PIC_UPPER)) {
1295 if (msk0 & PIC_LOWER)
1300 /* Otherwise, there is a conflict. */
1304 evts[0]->hw.idx = idx0;
1306 evts[1]->hw.idx = idx0 ^ 1;
1310 static int check_excludes(struct perf_event **evts, int n_prev, int n_new)
1312 int eu = 0, ek = 0, eh = 0;
1313 struct perf_event *event;
1316 if (!(sparc_pmu->flags & SPARC_PMU_ALL_EXCLUDES_SAME))
1324 for (i = 0; i < n; i++) {
1327 eu = event->attr.exclude_user;
1328 ek = event->attr.exclude_kernel;
1329 eh = event->attr.exclude_hv;
1331 } else if (event->attr.exclude_user != eu ||
1332 event->attr.exclude_kernel != ek ||
1333 event->attr.exclude_hv != eh) {
1341 static int collect_events(struct perf_event *group, int max_count,
1342 struct perf_event *evts[], unsigned long *events,
1345 struct perf_event *event;
1348 if (!is_software_event(group)) {
1352 events[n] = group->hw.event_base;
1353 current_idx[n++] = PIC_NO_INDEX;
1355 for_each_sibling_event(event, group) {
1356 if (!is_software_event(event) &&
1357 event->state != PERF_EVENT_STATE_OFF) {
1361 events[n] = event->hw.event_base;
1362 current_idx[n++] = PIC_NO_INDEX;
1368 static int sparc_pmu_add(struct perf_event *event, int ef_flags)
1370 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1371 int n0, ret = -EAGAIN;
1372 unsigned long flags;
1374 local_irq_save(flags);
1376 n0 = cpuc->n_events;
1377 if (n0 >= sparc_pmu->max_hw_events)
1380 cpuc->event[n0] = event;
1381 cpuc->events[n0] = event->hw.event_base;
1382 cpuc->current_idx[n0] = PIC_NO_INDEX;
1384 event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
1385 if (!(ef_flags & PERF_EF_START))
1386 event->hw.state |= PERF_HES_ARCH;
1389 * If group events scheduling transaction was started,
1390 * skip the schedulability test here, it will be performed
1391 * at commit time(->commit_txn) as a whole
1393 if (cpuc->txn_flags & PERF_PMU_TXN_ADD)
1396 if (check_excludes(cpuc->event, n0, 1))
1398 if (sparc_check_constraints(cpuc->event, cpuc->events, n0 + 1))
1407 local_irq_restore(flags);
1411 static int sparc_pmu_event_init(struct perf_event *event)
1413 struct perf_event_attr *attr = &event->attr;
1414 struct perf_event *evts[MAX_HWEVENTS];
1415 struct hw_perf_event *hwc = &event->hw;
1416 unsigned long events[MAX_HWEVENTS];
1417 int current_idx_dmy[MAX_HWEVENTS];
1418 const struct perf_event_map *pmap;
1421 if (atomic_read(&nmi_active) < 0)
1424 /* does not support taken branch sampling */
1425 if (has_branch_stack(event))
1428 switch (attr->type) {
1429 case PERF_TYPE_HARDWARE:
1430 if (attr->config >= sparc_pmu->max_events)
1432 pmap = sparc_pmu->event_map(attr->config);
1435 case PERF_TYPE_HW_CACHE:
1436 pmap = sparc_map_cache_event(attr->config);
1438 return PTR_ERR(pmap);
1451 hwc->event_base = perf_event_encode(pmap);
1454 * User gives us "(encoding << 16) | pic_mask" for
1455 * PERF_TYPE_RAW events.
1457 hwc->event_base = attr->config;
1460 /* We save the enable bits in the config_base. */
1461 hwc->config_base = sparc_pmu->irq_bit;
1462 if (!attr->exclude_user)
1463 hwc->config_base |= sparc_pmu->user_bit;
1464 if (!attr->exclude_kernel)
1465 hwc->config_base |= sparc_pmu->priv_bit;
1466 if (!attr->exclude_hv)
1467 hwc->config_base |= sparc_pmu->hv_bit;
1470 if (event->group_leader != event) {
1471 n = collect_events(event->group_leader,
1472 sparc_pmu->max_hw_events - 1,
1473 evts, events, current_idx_dmy);
1477 events[n] = hwc->event_base;
1480 if (check_excludes(evts, n, 1))
1483 if (sparc_check_constraints(evts, events, n + 1))
1486 hwc->idx = PIC_NO_INDEX;
1488 /* Try to do all error checking before this point, as unwinding
1489 * state after grabbing the PMC is difficult.
1491 perf_event_grab_pmc();
1492 event->destroy = hw_perf_event_destroy;
1494 if (!hwc->sample_period) {
1495 hwc->sample_period = MAX_PERIOD;
1496 hwc->last_period = hwc->sample_period;
1497 local64_set(&hwc->period_left, hwc->sample_period);
1504 * Start group events scheduling transaction
1505 * Set the flag to make pmu::enable() not perform the
1506 * schedulability test, it will be performed at commit time
1508 static void sparc_pmu_start_txn(struct pmu *pmu, unsigned int txn_flags)
1510 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
1512 WARN_ON_ONCE(cpuhw->txn_flags); /* txn already in flight */
1514 cpuhw->txn_flags = txn_flags;
1515 if (txn_flags & ~PERF_PMU_TXN_ADD)
1518 perf_pmu_disable(pmu);
1522 * Stop group events scheduling transaction
1523 * Clear the flag and pmu::enable() will perform the
1524 * schedulability test.
1526 static void sparc_pmu_cancel_txn(struct pmu *pmu)
1528 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
1529 unsigned int txn_flags;
1531 WARN_ON_ONCE(!cpuhw->txn_flags); /* no txn in flight */
1533 txn_flags = cpuhw->txn_flags;
1534 cpuhw->txn_flags = 0;
1535 if (txn_flags & ~PERF_PMU_TXN_ADD)
1538 perf_pmu_enable(pmu);
1542 * Commit group events scheduling transaction
1543 * Perform the group schedulability test as a whole
1544 * Return 0 if success
1546 static int sparc_pmu_commit_txn(struct pmu *pmu)
1548 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1554 WARN_ON_ONCE(!cpuc->txn_flags); /* no txn in flight */
1556 if (cpuc->txn_flags & ~PERF_PMU_TXN_ADD) {
1557 cpuc->txn_flags = 0;
1562 if (check_excludes(cpuc->event, 0, n))
1564 if (sparc_check_constraints(cpuc->event, cpuc->events, n))
1567 cpuc->txn_flags = 0;
1568 perf_pmu_enable(pmu);
1572 static struct pmu pmu = {
1573 .pmu_enable = sparc_pmu_enable,
1574 .pmu_disable = sparc_pmu_disable,
1575 .event_init = sparc_pmu_event_init,
1576 .add = sparc_pmu_add,
1577 .del = sparc_pmu_del,
1578 .start = sparc_pmu_start,
1579 .stop = sparc_pmu_stop,
1580 .read = sparc_pmu_read,
1581 .start_txn = sparc_pmu_start_txn,
1582 .cancel_txn = sparc_pmu_cancel_txn,
1583 .commit_txn = sparc_pmu_commit_txn,
1586 void perf_event_print_debug(void)
1588 unsigned long flags;
1594 local_irq_save(flags);
1596 cpu = smp_processor_id();
1599 for (i = 0; i < sparc_pmu->num_pcrs; i++)
1600 pr_info("CPU#%d: PCR%d[%016llx]\n",
1601 cpu, i, pcr_ops->read_pcr(i));
1602 for (i = 0; i < sparc_pmu->num_pic_regs; i++)
1603 pr_info("CPU#%d: PIC%d[%016llx]\n",
1604 cpu, i, pcr_ops->read_pic(i));
1606 local_irq_restore(flags);
1609 static int __kprobes perf_event_nmi_handler(struct notifier_block *self,
1610 unsigned long cmd, void *__args)
1612 struct die_args *args = __args;
1613 struct perf_sample_data data;
1614 struct cpu_hw_events *cpuc;
1615 struct pt_regs *regs;
1620 if (!atomic_read(&active_events))
1631 start_clock = sched_clock();
1635 cpuc = this_cpu_ptr(&cpu_hw_events);
1637 /* If the PMU has the TOE IRQ enable bits, we need to do a
1638 * dummy write to the %pcr to clear the overflow bits and thus
1641 * Do this before we peek at the counters to determine
1642 * overflow so we don't lose any events.
1644 if (sparc_pmu->irq_bit &&
1645 sparc_pmu->num_pcrs == 1)
1646 pcr_ops->write_pcr(0, cpuc->pcr[0]);
1648 for (i = 0; i < cpuc->n_events; i++) {
1649 struct perf_event *event = cpuc->event[i];
1650 int idx = cpuc->current_idx[i];
1651 struct hw_perf_event *hwc;
1654 if (sparc_pmu->irq_bit &&
1655 sparc_pmu->num_pcrs > 1)
1656 pcr_ops->write_pcr(idx, cpuc->pcr[idx]);
1659 val = sparc_perf_event_update(event, hwc, idx);
1660 if (val & (1ULL << 31))
1663 perf_sample_data_init(&data, 0, hwc->last_period);
1664 if (!sparc_perf_event_set_period(event, hwc, idx))
1667 if (perf_event_overflow(event, &data, regs))
1668 sparc_pmu_stop(event, 0);
1671 finish_clock = sched_clock();
1673 perf_sample_event_took(finish_clock - start_clock);
1678 static __read_mostly struct notifier_block perf_event_nmi_notifier = {
1679 .notifier_call = perf_event_nmi_handler,
1682 static bool __init supported_pmu(void)
1684 if (!strcmp(sparc_pmu_type, "ultra3") ||
1685 !strcmp(sparc_pmu_type, "ultra3+") ||
1686 !strcmp(sparc_pmu_type, "ultra3i") ||
1687 !strcmp(sparc_pmu_type, "ultra4+")) {
1688 sparc_pmu = &ultra3_pmu;
1691 if (!strcmp(sparc_pmu_type, "niagara")) {
1692 sparc_pmu = &niagara1_pmu;
1695 if (!strcmp(sparc_pmu_type, "niagara2") ||
1696 !strcmp(sparc_pmu_type, "niagara3")) {
1697 sparc_pmu = &niagara2_pmu;
1700 if (!strcmp(sparc_pmu_type, "niagara4") ||
1701 !strcmp(sparc_pmu_type, "niagara5")) {
1702 sparc_pmu = &niagara4_pmu;
1705 if (!strcmp(sparc_pmu_type, "sparc-m7")) {
1706 sparc_pmu = &sparc_m7_pmu;
1712 static int __init init_hw_perf_events(void)
1716 pr_info("Performance events: ");
1718 err = pcr_arch_init();
1719 if (err || !supported_pmu()) {
1720 pr_cont("No support for PMU type '%s'\n", sparc_pmu_type);
1724 pr_cont("Supported PMU type is '%s'\n", sparc_pmu_type);
1726 perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
1727 register_die_notifier(&perf_event_nmi_notifier);
1731 pure_initcall(init_hw_perf_events);
1733 void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
1734 struct pt_regs *regs)
1736 unsigned long ksp, fp;
1737 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1741 stack_trace_flush();
1743 perf_callchain_store(entry, regs->tpc);
1745 ksp = regs->u_regs[UREG_I6];
1746 fp = ksp + STACK_BIAS;
1748 struct sparc_stackf *sf;
1749 struct pt_regs *regs;
1752 if (!kstack_valid(current_thread_info(), fp))
1755 sf = (struct sparc_stackf *) fp;
1756 regs = (struct pt_regs *) (sf + 1);
1758 if (kstack_is_trap_frame(current_thread_info(), regs)) {
1759 if (user_mode(regs))
1762 fp = regs->u_regs[UREG_I6] + STACK_BIAS;
1764 pc = sf->callers_pc;
1765 fp = (unsigned long)sf->fp + STACK_BIAS;
1767 perf_callchain_store(entry, pc);
1768 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1769 if ((pc + 8UL) == (unsigned long) &return_to_handler) {
1770 struct ftrace_ret_stack *ret_stack;
1771 ret_stack = ftrace_graph_get_ret_stack(current,
1774 pc = ret_stack->ret;
1775 perf_callchain_store(entry, pc);
1780 } while (entry->nr < entry->max_stack);
1784 valid_user_frame(const void __user *fp, unsigned long size)
1786 /* addresses should be at least 4-byte aligned */
1787 if (((unsigned long) fp) & 3)
1790 return (__range_not_ok(fp, size, TASK_SIZE) == 0);
1793 static void perf_callchain_user_64(struct perf_callchain_entry_ctx *entry,
1794 struct pt_regs *regs)
1798 ufp = regs->u_regs[UREG_FP] + STACK_BIAS;
1800 struct sparc_stackf __user *usf;
1801 struct sparc_stackf sf;
1804 usf = (struct sparc_stackf __user *)ufp;
1805 if (!valid_user_frame(usf, sizeof(sf)))
1808 if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
1812 ufp = (unsigned long)sf.fp + STACK_BIAS;
1813 perf_callchain_store(entry, pc);
1814 } while (entry->nr < entry->max_stack);
1817 static void perf_callchain_user_32(struct perf_callchain_entry_ctx *entry,
1818 struct pt_regs *regs)
1822 ufp = regs->u_regs[UREG_FP] & 0xffffffffUL;
1826 if (thread32_stack_is_64bit(ufp)) {
1827 struct sparc_stackf __user *usf;
1828 struct sparc_stackf sf;
1831 usf = (struct sparc_stackf __user *)ufp;
1832 if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
1834 pc = sf.callers_pc & 0xffffffff;
1835 ufp = ((unsigned long) sf.fp) & 0xffffffff;
1837 struct sparc_stackf32 __user *usf;
1838 struct sparc_stackf32 sf;
1839 usf = (struct sparc_stackf32 __user *)ufp;
1840 if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
1843 ufp = (unsigned long)sf.fp;
1845 perf_callchain_store(entry, pc);
1846 } while (entry->nr < entry->max_stack);
1850 perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
1852 u64 saved_fault_address = current_thread_info()->fault_address;
1853 u8 saved_fault_code = get_thread_fault_code();
1855 perf_callchain_store(entry, regs->tpc);
1862 pagefault_disable();
1864 if (test_thread_flag(TIF_32BIT))
1865 perf_callchain_user_32(entry, regs);
1867 perf_callchain_user_64(entry, regs);
1871 set_thread_fault_code(saved_fault_code);
1872 current_thread_info()->fault_address = saved_fault_address;