Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1 | /* Performance event support for sparc64. |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 2 | * |
David S. Miller | 4f6dbe4 | 2010-01-19 00:26:13 -0800 | [diff] [blame] | 3 | * Copyright (C) 2009, 2010 David S. Miller <davem@davemloft.net> |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 4 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5 | * This code is based almost entirely upon the x86 perf event |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 6 | * code, which is: |
| 7 | * |
| 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> |
| 13 | */ |
| 14 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 15 | #include <linux/perf_event.h> |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 16 | #include <linux/kprobes.h> |
David S. Miller | 667f0ce | 2010-04-21 03:08:11 -0700 | [diff] [blame] | 17 | #include <linux/ftrace.h> |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 18 | #include <linux/kernel.h> |
| 19 | #include <linux/kdebug.h> |
| 20 | #include <linux/mutex.h> |
| 21 | |
David S. Miller | 4f6dbe4 | 2010-01-19 00:26:13 -0800 | [diff] [blame] | 22 | #include <asm/stacktrace.h> |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 23 | #include <asm/cpudata.h> |
David S. Miller | 4f6dbe4 | 2010-01-19 00:26:13 -0800 | [diff] [blame] | 24 | #include <asm/uaccess.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 25 | #include <linux/atomic.h> |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 26 | #include <asm/nmi.h> |
| 27 | #include <asm/pcr.h> |
David Howells | d550bbd | 2012-03-28 18:30:03 +0100 | [diff] [blame] | 28 | #include <asm/cacheflush.h> |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 29 | |
Sam Ravnborg | cb1b820 | 2011-04-21 15:45:45 -0700 | [diff] [blame] | 30 | #include "kernel.h" |
David S. Miller | 4f6dbe4 | 2010-01-19 00:26:13 -0800 | [diff] [blame] | 31 | #include "kstack.h" |
| 32 | |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 33 | /* Sparc64 chips have two performance counters, 32-bits each, with |
| 34 | * overflow interrupts generated on transition from 0xffffffff to 0. |
| 35 | * The counters are accessed in one go using a 64-bit register. |
| 36 | * |
| 37 | * Both counters are controlled using a single control register. The |
| 38 | * only way to stop all sampling is to clear all of the context (user, |
| 39 | * supervisor, hypervisor) sampling enable bits. But these bits apply |
| 40 | * to both counters, thus the two counters can't be enabled/disabled |
| 41 | * individually. |
| 42 | * |
| 43 | * The control register has two event fields, one for each of the two |
| 44 | * counters. It's thus nearly impossible to have one counter going |
| 45 | * while keeping the other one stopped. Therefore it is possible to |
| 46 | * get overflow interrupts for counters not currently "in use" and |
| 47 | * that condition must be checked in the overflow interrupt handler. |
| 48 | * |
| 49 | * So we use a hack, in that we program inactive counters with the |
| 50 | * "sw_count0" and "sw_count1" events. These count how many times |
| 51 | * the instruction "sethi %hi(0xfc000), %g0" is executed. It's an |
| 52 | * unusual way to encode a NOP and therefore will not trigger in |
| 53 | * normal code. |
| 54 | */ |
| 55 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 56 | #define MAX_HWEVENTS 2 |
David S. Miller | 3f1a209 | 2012-08-17 02:51:21 -0700 | [diff] [blame] | 57 | #define MAX_PCRS 1 |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 58 | #define MAX_PERIOD ((1UL << 32) - 1) |
| 59 | |
| 60 | #define PIC_UPPER_INDEX 0 |
| 61 | #define PIC_LOWER_INDEX 1 |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 62 | #define PIC_NO_INDEX -1 |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 63 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 64 | struct cpu_hw_events { |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 65 | /* Number of events currently scheduled onto this cpu. |
| 66 | * This tells how many entries in the arrays below |
| 67 | * are valid. |
| 68 | */ |
| 69 | int n_events; |
| 70 | |
| 71 | /* Number of new events added since the last hw_perf_disable(). |
| 72 | * This works because the perf event layer always adds new |
| 73 | * events inside of a perf_{disable,enable}() sequence. |
| 74 | */ |
| 75 | int n_added; |
| 76 | |
| 77 | /* Array of events current scheduled on this cpu. */ |
| 78 | struct perf_event *event[MAX_HWEVENTS]; |
| 79 | |
| 80 | /* Array of encoded longs, specifying the %pcr register |
| 81 | * encoding and the mask of PIC counters this even can |
| 82 | * be scheduled on. See perf_event_encode() et al. |
| 83 | */ |
| 84 | unsigned long events[MAX_HWEVENTS]; |
| 85 | |
| 86 | /* The current counter index assigned to an event. When the |
| 87 | * event hasn't been programmed into the cpu yet, this will |
| 88 | * hold PIC_NO_INDEX. The event->hw.idx value tells us where |
| 89 | * we ought to schedule the event. |
| 90 | */ |
| 91 | int current_idx[MAX_HWEVENTS]; |
| 92 | |
David S. Miller | 3f1a209 | 2012-08-17 02:51:21 -0700 | [diff] [blame] | 93 | /* Software copy of %pcr register(s) on this cpu. */ |
| 94 | u64 pcr[MAX_HWEVENTS]; |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 95 | |
| 96 | /* Enabled/disable state. */ |
David S. Miller | d175138 | 2009-09-29 21:27:06 -0700 | [diff] [blame] | 97 | int enabled; |
Lin Ming | a13c3af | 2010-04-23 13:56:33 +0800 | [diff] [blame] | 98 | |
| 99 | unsigned int group_flag; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 100 | }; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 101 | DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { .enabled = 1, }; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 102 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 103 | /* An event map describes the characteristics of a performance |
| 104 | * counter event. In particular it gives the encoding as well as |
| 105 | * a mask telling which counters the event can be measured on. |
| 106 | */ |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 107 | struct perf_event_map { |
| 108 | u16 encoding; |
| 109 | u8 pic_mask; |
| 110 | #define PIC_NONE 0x00 |
| 111 | #define PIC_UPPER 0x01 |
| 112 | #define PIC_LOWER 0x02 |
| 113 | }; |
| 114 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 115 | /* Encode a perf_event_map entry into a long. */ |
David S. Miller | a72a8a5 | 2009-09-28 17:35:20 -0700 | [diff] [blame] | 116 | static unsigned long perf_event_encode(const struct perf_event_map *pmap) |
| 117 | { |
| 118 | return ((unsigned long) pmap->encoding << 16) | pmap->pic_mask; |
| 119 | } |
| 120 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 121 | static u8 perf_event_get_msk(unsigned long val) |
David S. Miller | a72a8a5 | 2009-09-28 17:35:20 -0700 | [diff] [blame] | 122 | { |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 123 | return val & 0xff; |
| 124 | } |
| 125 | |
| 126 | static u64 perf_event_get_enc(unsigned long val) |
| 127 | { |
| 128 | return val >> 16; |
David S. Miller | a72a8a5 | 2009-09-28 17:35:20 -0700 | [diff] [blame] | 129 | } |
| 130 | |
David S. Miller | 2ce4da2 | 2009-09-26 20:42:10 -0700 | [diff] [blame] | 131 | #define C(x) PERF_COUNT_HW_CACHE_##x |
| 132 | |
| 133 | #define CACHE_OP_UNSUPPORTED 0xfffe |
| 134 | #define CACHE_OP_NONSENSE 0xffff |
| 135 | |
| 136 | typedef struct perf_event_map cache_map_t |
| 137 | [PERF_COUNT_HW_CACHE_MAX] |
| 138 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 139 | [PERF_COUNT_HW_CACHE_RESULT_MAX]; |
| 140 | |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 141 | struct sparc_pmu { |
| 142 | const struct perf_event_map *(*event_map)(int); |
David S. Miller | 2ce4da2 | 2009-09-26 20:42:10 -0700 | [diff] [blame] | 143 | const cache_map_t *cache_map; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 144 | int max_events; |
David S. Miller | 5344303 | 2012-08-17 02:37:06 -0700 | [diff] [blame] | 145 | u32 (*read_pmc)(int); |
| 146 | void (*write_pmc)(int, u64); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 147 | int upper_shift; |
| 148 | int lower_shift; |
| 149 | int event_mask; |
David S. Miller | 7ac2ed2 | 2012-08-17 02:41:32 -0700 | [diff] [blame] | 150 | int user_bit; |
| 151 | int priv_bit; |
David S. Miller | 91b9286 | 2009-09-10 07:09:06 -0700 | [diff] [blame] | 152 | int hv_bit; |
David S. Miller | 496c07e | 2009-09-10 07:10:59 -0700 | [diff] [blame] | 153 | int irq_bit; |
David S. Miller | 660d137 | 2009-09-10 07:13:26 -0700 | [diff] [blame] | 154 | int upper_nop; |
| 155 | int lower_nop; |
David S. Miller | b38e99f | 2012-08-17 02:31:10 -0700 | [diff] [blame] | 156 | unsigned int flags; |
| 157 | #define SPARC_PMU_ALL_EXCLUDES_SAME 0x00000001 |
| 158 | #define SPARC_PMU_HAS_CONFLICTS 0x00000002 |
David S. Miller | 5966049 | 2012-08-17 02:33:44 -0700 | [diff] [blame] | 159 | int max_hw_events; |
David S. Miller | 3f1a209 | 2012-08-17 02:51:21 -0700 | [diff] [blame] | 160 | int num_pcrs; |
| 161 | int num_pic_regs; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 162 | }; |
| 163 | |
David S. Miller | 5344303 | 2012-08-17 02:37:06 -0700 | [diff] [blame] | 164 | static u32 sparc_default_read_pmc(int idx) |
| 165 | { |
| 166 | u64 val; |
| 167 | |
| 168 | val = pcr_ops->read_pic(0); |
| 169 | if (idx == PIC_UPPER_INDEX) |
| 170 | val >>= 32; |
| 171 | |
| 172 | return val & 0xffffffff; |
| 173 | } |
| 174 | |
| 175 | static void sparc_default_write_pmc(int idx, u64 val) |
| 176 | { |
| 177 | u64 shift, mask, pic; |
| 178 | |
| 179 | shift = 0; |
| 180 | if (idx == PIC_UPPER_INDEX) |
| 181 | shift = 32; |
| 182 | |
| 183 | mask = ((u64) 0xffffffff) << shift; |
| 184 | val <<= shift; |
| 185 | |
| 186 | pic = pcr_ops->read_pic(0); |
| 187 | pic &= ~mask; |
| 188 | pic |= val; |
| 189 | pcr_ops->write_pic(0, pic); |
| 190 | } |
| 191 | |
David S. Miller | 28e8f9b | 2009-09-26 20:54:22 -0700 | [diff] [blame] | 192 | static const struct perf_event_map ultra3_perfmon_event_map[] = { |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 193 | [PERF_COUNT_HW_CPU_CYCLES] = { 0x0000, PIC_UPPER | PIC_LOWER }, |
| 194 | [PERF_COUNT_HW_INSTRUCTIONS] = { 0x0001, PIC_UPPER | PIC_LOWER }, |
| 195 | [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0009, PIC_LOWER }, |
| 196 | [PERF_COUNT_HW_CACHE_MISSES] = { 0x0009, PIC_UPPER }, |
| 197 | }; |
| 198 | |
David S. Miller | 28e8f9b | 2009-09-26 20:54:22 -0700 | [diff] [blame] | 199 | static const struct perf_event_map *ultra3_event_map(int event_id) |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 200 | { |
David S. Miller | 28e8f9b | 2009-09-26 20:54:22 -0700 | [diff] [blame] | 201 | return &ultra3_perfmon_event_map[event_id]; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 202 | } |
| 203 | |
David S. Miller | 28e8f9b | 2009-09-26 20:54:22 -0700 | [diff] [blame] | 204 | static const cache_map_t ultra3_cache_map = { |
David S. Miller | 2ce4da2 | 2009-09-26 20:42:10 -0700 | [diff] [blame] | 205 | [C(L1D)] = { |
| 206 | [C(OP_READ)] = { |
| 207 | [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, }, |
| 208 | [C(RESULT_MISS)] = { 0x09, PIC_UPPER, }, |
| 209 | }, |
| 210 | [C(OP_WRITE)] = { |
| 211 | [C(RESULT_ACCESS)] = { 0x0a, PIC_LOWER }, |
| 212 | [C(RESULT_MISS)] = { 0x0a, PIC_UPPER }, |
| 213 | }, |
| 214 | [C(OP_PREFETCH)] = { |
| 215 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 216 | [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED }, |
| 217 | }, |
| 218 | }, |
| 219 | [C(L1I)] = { |
| 220 | [C(OP_READ)] = { |
| 221 | [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, }, |
| 222 | [C(RESULT_MISS)] = { 0x09, PIC_UPPER, }, |
| 223 | }, |
| 224 | [ C(OP_WRITE) ] = { |
| 225 | [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE }, |
| 226 | [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE }, |
| 227 | }, |
| 228 | [ C(OP_PREFETCH) ] = { |
| 229 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 230 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 231 | }, |
| 232 | }, |
| 233 | [C(LL)] = { |
| 234 | [C(OP_READ)] = { |
| 235 | [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER, }, |
| 236 | [C(RESULT_MISS)] = { 0x0c, PIC_UPPER, }, |
| 237 | }, |
| 238 | [C(OP_WRITE)] = { |
| 239 | [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER }, |
| 240 | [C(RESULT_MISS)] = { 0x0c, PIC_UPPER }, |
| 241 | }, |
| 242 | [C(OP_PREFETCH)] = { |
| 243 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 244 | [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED }, |
| 245 | }, |
| 246 | }, |
| 247 | [C(DTLB)] = { |
| 248 | [C(OP_READ)] = { |
| 249 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 250 | [C(RESULT_MISS)] = { 0x12, PIC_UPPER, }, |
| 251 | }, |
| 252 | [ C(OP_WRITE) ] = { |
| 253 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 254 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 255 | }, |
| 256 | [ C(OP_PREFETCH) ] = { |
| 257 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 258 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 259 | }, |
| 260 | }, |
| 261 | [C(ITLB)] = { |
| 262 | [C(OP_READ)] = { |
| 263 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 264 | [C(RESULT_MISS)] = { 0x11, PIC_UPPER, }, |
| 265 | }, |
| 266 | [ C(OP_WRITE) ] = { |
| 267 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 268 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 269 | }, |
| 270 | [ C(OP_PREFETCH) ] = { |
| 271 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 272 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 273 | }, |
| 274 | }, |
| 275 | [C(BPU)] = { |
| 276 | [C(OP_READ)] = { |
| 277 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 278 | [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED }, |
| 279 | }, |
| 280 | [ C(OP_WRITE) ] = { |
| 281 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 282 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 283 | }, |
| 284 | [ C(OP_PREFETCH) ] = { |
| 285 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 286 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 287 | }, |
| 288 | }, |
Peter Zijlstra | 89d6c0b | 2011-04-22 23:37:06 +0200 | [diff] [blame] | 289 | [C(NODE)] = { |
| 290 | [C(OP_READ)] = { |
| 291 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 292 | [C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 293 | }, |
| 294 | [ C(OP_WRITE) ] = { |
| 295 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 296 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 297 | }, |
| 298 | [ C(OP_PREFETCH) ] = { |
| 299 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 300 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 301 | }, |
| 302 | }, |
David S. Miller | 2ce4da2 | 2009-09-26 20:42:10 -0700 | [diff] [blame] | 303 | }; |
| 304 | |
David S. Miller | 28e8f9b | 2009-09-26 20:54:22 -0700 | [diff] [blame] | 305 | static const struct sparc_pmu ultra3_pmu = { |
| 306 | .event_map = ultra3_event_map, |
| 307 | .cache_map = &ultra3_cache_map, |
| 308 | .max_events = ARRAY_SIZE(ultra3_perfmon_event_map), |
David S. Miller | 5344303 | 2012-08-17 02:37:06 -0700 | [diff] [blame] | 309 | .read_pmc = sparc_default_read_pmc, |
| 310 | .write_pmc = sparc_default_write_pmc, |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 311 | .upper_shift = 11, |
| 312 | .lower_shift = 4, |
| 313 | .event_mask = 0x3f, |
David S. Miller | 7ac2ed2 | 2012-08-17 02:41:32 -0700 | [diff] [blame] | 314 | .user_bit = PCR_UTRACE, |
| 315 | .priv_bit = PCR_STRACE, |
David S. Miller | 660d137 | 2009-09-10 07:13:26 -0700 | [diff] [blame] | 316 | .upper_nop = 0x1c, |
| 317 | .lower_nop = 0x14, |
David S. Miller | b38e99f | 2012-08-17 02:31:10 -0700 | [diff] [blame] | 318 | .flags = (SPARC_PMU_ALL_EXCLUDES_SAME | |
| 319 | SPARC_PMU_HAS_CONFLICTS), |
David S. Miller | 5966049 | 2012-08-17 02:33:44 -0700 | [diff] [blame] | 320 | .max_hw_events = 2, |
David S. Miller | 3f1a209 | 2012-08-17 02:51:21 -0700 | [diff] [blame] | 321 | .num_pcrs = 1, |
| 322 | .num_pic_regs = 1, |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 323 | }; |
| 324 | |
David S. Miller | 7eebda6 | 2009-09-26 21:23:41 -0700 | [diff] [blame] | 325 | /* Niagara1 is very limited. The upper PIC is hard-locked to count |
| 326 | * only instructions, so it is free running which creates all kinds of |
David S. Miller | 6e80425 | 2009-09-29 15:10:23 -0700 | [diff] [blame] | 327 | * problems. Some hardware designs make one wonder if the creator |
David S. Miller | 7eebda6 | 2009-09-26 21:23:41 -0700 | [diff] [blame] | 328 | * even looked at how this stuff gets used by software. |
| 329 | */ |
| 330 | static const struct perf_event_map niagara1_perfmon_event_map[] = { |
| 331 | [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, PIC_UPPER }, |
| 332 | [PERF_COUNT_HW_INSTRUCTIONS] = { 0x00, PIC_UPPER }, |
| 333 | [PERF_COUNT_HW_CACHE_REFERENCES] = { 0, PIC_NONE }, |
| 334 | [PERF_COUNT_HW_CACHE_MISSES] = { 0x03, PIC_LOWER }, |
| 335 | }; |
| 336 | |
| 337 | static const struct perf_event_map *niagara1_event_map(int event_id) |
| 338 | { |
| 339 | return &niagara1_perfmon_event_map[event_id]; |
| 340 | } |
| 341 | |
| 342 | static const cache_map_t niagara1_cache_map = { |
| 343 | [C(L1D)] = { |
| 344 | [C(OP_READ)] = { |
| 345 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 346 | [C(RESULT_MISS)] = { 0x03, PIC_LOWER, }, |
| 347 | }, |
| 348 | [C(OP_WRITE)] = { |
| 349 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 350 | [C(RESULT_MISS)] = { 0x03, PIC_LOWER, }, |
| 351 | }, |
| 352 | [C(OP_PREFETCH)] = { |
| 353 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 354 | [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED }, |
| 355 | }, |
| 356 | }, |
| 357 | [C(L1I)] = { |
| 358 | [C(OP_READ)] = { |
| 359 | [C(RESULT_ACCESS)] = { 0x00, PIC_UPPER }, |
| 360 | [C(RESULT_MISS)] = { 0x02, PIC_LOWER, }, |
| 361 | }, |
| 362 | [ C(OP_WRITE) ] = { |
| 363 | [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE }, |
| 364 | [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE }, |
| 365 | }, |
| 366 | [ C(OP_PREFETCH) ] = { |
| 367 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 368 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 369 | }, |
| 370 | }, |
| 371 | [C(LL)] = { |
| 372 | [C(OP_READ)] = { |
| 373 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 374 | [C(RESULT_MISS)] = { 0x07, PIC_LOWER, }, |
| 375 | }, |
| 376 | [C(OP_WRITE)] = { |
| 377 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 378 | [C(RESULT_MISS)] = { 0x07, PIC_LOWER, }, |
| 379 | }, |
| 380 | [C(OP_PREFETCH)] = { |
| 381 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 382 | [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED }, |
| 383 | }, |
| 384 | }, |
| 385 | [C(DTLB)] = { |
| 386 | [C(OP_READ)] = { |
| 387 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 388 | [C(RESULT_MISS)] = { 0x05, PIC_LOWER, }, |
| 389 | }, |
| 390 | [ C(OP_WRITE) ] = { |
| 391 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 392 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 393 | }, |
| 394 | [ C(OP_PREFETCH) ] = { |
| 395 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 396 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 397 | }, |
| 398 | }, |
| 399 | [C(ITLB)] = { |
| 400 | [C(OP_READ)] = { |
| 401 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 402 | [C(RESULT_MISS)] = { 0x04, PIC_LOWER, }, |
| 403 | }, |
| 404 | [ C(OP_WRITE) ] = { |
| 405 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 406 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 407 | }, |
| 408 | [ C(OP_PREFETCH) ] = { |
| 409 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 410 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 411 | }, |
| 412 | }, |
| 413 | [C(BPU)] = { |
| 414 | [C(OP_READ)] = { |
| 415 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 416 | [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED }, |
| 417 | }, |
| 418 | [ C(OP_WRITE) ] = { |
| 419 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 420 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 421 | }, |
| 422 | [ C(OP_PREFETCH) ] = { |
| 423 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 424 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 425 | }, |
| 426 | }, |
Peter Zijlstra | 89d6c0b | 2011-04-22 23:37:06 +0200 | [diff] [blame] | 427 | [C(NODE)] = { |
| 428 | [C(OP_READ)] = { |
| 429 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 430 | [C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 431 | }, |
| 432 | [ C(OP_WRITE) ] = { |
| 433 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 434 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 435 | }, |
| 436 | [ C(OP_PREFETCH) ] = { |
| 437 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 438 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 439 | }, |
| 440 | }, |
David S. Miller | 7eebda6 | 2009-09-26 21:23:41 -0700 | [diff] [blame] | 441 | }; |
| 442 | |
| 443 | static const struct sparc_pmu niagara1_pmu = { |
| 444 | .event_map = niagara1_event_map, |
| 445 | .cache_map = &niagara1_cache_map, |
| 446 | .max_events = ARRAY_SIZE(niagara1_perfmon_event_map), |
David S. Miller | 5344303 | 2012-08-17 02:37:06 -0700 | [diff] [blame] | 447 | .read_pmc = sparc_default_read_pmc, |
| 448 | .write_pmc = sparc_default_write_pmc, |
David S. Miller | 7eebda6 | 2009-09-26 21:23:41 -0700 | [diff] [blame] | 449 | .upper_shift = 0, |
| 450 | .lower_shift = 4, |
| 451 | .event_mask = 0x7, |
David S. Miller | 7ac2ed2 | 2012-08-17 02:41:32 -0700 | [diff] [blame] | 452 | .user_bit = PCR_UTRACE, |
| 453 | .priv_bit = PCR_STRACE, |
David S. Miller | 7eebda6 | 2009-09-26 21:23:41 -0700 | [diff] [blame] | 454 | .upper_nop = 0x0, |
| 455 | .lower_nop = 0x0, |
David S. Miller | b38e99f | 2012-08-17 02:31:10 -0700 | [diff] [blame] | 456 | .flags = (SPARC_PMU_ALL_EXCLUDES_SAME | |
| 457 | SPARC_PMU_HAS_CONFLICTS), |
David S. Miller | 5966049 | 2012-08-17 02:33:44 -0700 | [diff] [blame] | 458 | .max_hw_events = 2, |
David S. Miller | 3f1a209 | 2012-08-17 02:51:21 -0700 | [diff] [blame] | 459 | .num_pcrs = 1, |
| 460 | .num_pic_regs = 1, |
David S. Miller | 7eebda6 | 2009-09-26 21:23:41 -0700 | [diff] [blame] | 461 | }; |
| 462 | |
David S. Miller | b73d884 | 2009-09-10 07:22:18 -0700 | [diff] [blame] | 463 | static const struct perf_event_map niagara2_perfmon_event_map[] = { |
| 464 | [PERF_COUNT_HW_CPU_CYCLES] = { 0x02ff, PIC_UPPER | PIC_LOWER }, |
| 465 | [PERF_COUNT_HW_INSTRUCTIONS] = { 0x02ff, PIC_UPPER | PIC_LOWER }, |
| 466 | [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0208, PIC_UPPER | PIC_LOWER }, |
| 467 | [PERF_COUNT_HW_CACHE_MISSES] = { 0x0302, PIC_UPPER | PIC_LOWER }, |
| 468 | [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x0201, PIC_UPPER | PIC_LOWER }, |
| 469 | [PERF_COUNT_HW_BRANCH_MISSES] = { 0x0202, PIC_UPPER | PIC_LOWER }, |
| 470 | }; |
| 471 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 472 | static const struct perf_event_map *niagara2_event_map(int event_id) |
David S. Miller | b73d884 | 2009-09-10 07:22:18 -0700 | [diff] [blame] | 473 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 474 | return &niagara2_perfmon_event_map[event_id]; |
David S. Miller | b73d884 | 2009-09-10 07:22:18 -0700 | [diff] [blame] | 475 | } |
| 476 | |
David S. Miller | d0b8648 | 2009-09-26 21:04:16 -0700 | [diff] [blame] | 477 | static const cache_map_t niagara2_cache_map = { |
| 478 | [C(L1D)] = { |
| 479 | [C(OP_READ)] = { |
| 480 | [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, }, |
| 481 | [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, }, |
| 482 | }, |
| 483 | [C(OP_WRITE)] = { |
| 484 | [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, }, |
| 485 | [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, }, |
| 486 | }, |
| 487 | [C(OP_PREFETCH)] = { |
| 488 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 489 | [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED }, |
| 490 | }, |
| 491 | }, |
| 492 | [C(L1I)] = { |
| 493 | [C(OP_READ)] = { |
| 494 | [C(RESULT_ACCESS)] = { 0x02ff, PIC_UPPER | PIC_LOWER, }, |
| 495 | [C(RESULT_MISS)] = { 0x0301, PIC_UPPER | PIC_LOWER, }, |
| 496 | }, |
| 497 | [ C(OP_WRITE) ] = { |
| 498 | [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE }, |
| 499 | [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE }, |
| 500 | }, |
| 501 | [ C(OP_PREFETCH) ] = { |
| 502 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 503 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 504 | }, |
| 505 | }, |
| 506 | [C(LL)] = { |
| 507 | [C(OP_READ)] = { |
| 508 | [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, }, |
| 509 | [C(RESULT_MISS)] = { 0x0330, PIC_UPPER | PIC_LOWER, }, |
| 510 | }, |
| 511 | [C(OP_WRITE)] = { |
| 512 | [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, }, |
| 513 | [C(RESULT_MISS)] = { 0x0320, PIC_UPPER | PIC_LOWER, }, |
| 514 | }, |
| 515 | [C(OP_PREFETCH)] = { |
| 516 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 517 | [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED }, |
| 518 | }, |
| 519 | }, |
| 520 | [C(DTLB)] = { |
| 521 | [C(OP_READ)] = { |
| 522 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 523 | [C(RESULT_MISS)] = { 0x0b08, PIC_UPPER | PIC_LOWER, }, |
| 524 | }, |
| 525 | [ C(OP_WRITE) ] = { |
| 526 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 527 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 528 | }, |
| 529 | [ C(OP_PREFETCH) ] = { |
| 530 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 531 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 532 | }, |
| 533 | }, |
| 534 | [C(ITLB)] = { |
| 535 | [C(OP_READ)] = { |
| 536 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 537 | [C(RESULT_MISS)] = { 0xb04, PIC_UPPER | PIC_LOWER, }, |
| 538 | }, |
| 539 | [ C(OP_WRITE) ] = { |
| 540 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 541 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 542 | }, |
| 543 | [ C(OP_PREFETCH) ] = { |
| 544 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 545 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 546 | }, |
| 547 | }, |
| 548 | [C(BPU)] = { |
| 549 | [C(OP_READ)] = { |
| 550 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 551 | [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED }, |
| 552 | }, |
| 553 | [ C(OP_WRITE) ] = { |
| 554 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 555 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 556 | }, |
| 557 | [ C(OP_PREFETCH) ] = { |
| 558 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 559 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 560 | }, |
| 561 | }, |
Peter Zijlstra | 89d6c0b | 2011-04-22 23:37:06 +0200 | [diff] [blame] | 562 | [C(NODE)] = { |
| 563 | [C(OP_READ)] = { |
| 564 | [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED }, |
| 565 | [C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 566 | }, |
| 567 | [ C(OP_WRITE) ] = { |
| 568 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 569 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 570 | }, |
| 571 | [ C(OP_PREFETCH) ] = { |
| 572 | [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED }, |
| 573 | [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED }, |
| 574 | }, |
| 575 | }, |
David S. Miller | d0b8648 | 2009-09-26 21:04:16 -0700 | [diff] [blame] | 576 | }; |
| 577 | |
David S. Miller | b73d884 | 2009-09-10 07:22:18 -0700 | [diff] [blame] | 578 | static const struct sparc_pmu niagara2_pmu = { |
| 579 | .event_map = niagara2_event_map, |
David S. Miller | d0b8648 | 2009-09-26 21:04:16 -0700 | [diff] [blame] | 580 | .cache_map = &niagara2_cache_map, |
David S. Miller | b73d884 | 2009-09-10 07:22:18 -0700 | [diff] [blame] | 581 | .max_events = ARRAY_SIZE(niagara2_perfmon_event_map), |
David S. Miller | 5344303 | 2012-08-17 02:37:06 -0700 | [diff] [blame] | 582 | .read_pmc = sparc_default_read_pmc, |
| 583 | .write_pmc = sparc_default_write_pmc, |
David S. Miller | b73d884 | 2009-09-10 07:22:18 -0700 | [diff] [blame] | 584 | .upper_shift = 19, |
| 585 | .lower_shift = 6, |
| 586 | .event_mask = 0xfff, |
David S. Miller | 7ac2ed2 | 2012-08-17 02:41:32 -0700 | [diff] [blame] | 587 | .user_bit = PCR_UTRACE, |
| 588 | .priv_bit = PCR_STRACE, |
| 589 | .hv_bit = PCR_N2_HTRACE, |
David S. Miller | de23cf3 | 2009-10-09 00:42:40 -0700 | [diff] [blame] | 590 | .irq_bit = 0x30, |
David S. Miller | b73d884 | 2009-09-10 07:22:18 -0700 | [diff] [blame] | 591 | .upper_nop = 0x220, |
| 592 | .lower_nop = 0x220, |
David S. Miller | b38e99f | 2012-08-17 02:31:10 -0700 | [diff] [blame] | 593 | .flags = (SPARC_PMU_ALL_EXCLUDES_SAME | |
| 594 | SPARC_PMU_HAS_CONFLICTS), |
David S. Miller | 5966049 | 2012-08-17 02:33:44 -0700 | [diff] [blame] | 595 | .max_hw_events = 2, |
David S. Miller | 3f1a209 | 2012-08-17 02:51:21 -0700 | [diff] [blame] | 596 | .num_pcrs = 1, |
| 597 | .num_pic_regs = 1, |
David S. Miller | b73d884 | 2009-09-10 07:22:18 -0700 | [diff] [blame] | 598 | }; |
| 599 | |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 600 | static const struct sparc_pmu *sparc_pmu __read_mostly; |
| 601 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 602 | static u64 event_encoding(u64 event_id, int idx) |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 603 | { |
| 604 | if (idx == PIC_UPPER_INDEX) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 605 | event_id <<= sparc_pmu->upper_shift; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 606 | else |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 607 | event_id <<= sparc_pmu->lower_shift; |
| 608 | return event_id; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | static u64 mask_for_index(int idx) |
| 612 | { |
| 613 | return event_encoding(sparc_pmu->event_mask, idx); |
| 614 | } |
| 615 | |
| 616 | static u64 nop_for_index(int idx) |
| 617 | { |
| 618 | return event_encoding(idx == PIC_UPPER_INDEX ? |
David S. Miller | 660d137 | 2009-09-10 07:13:26 -0700 | [diff] [blame] | 619 | sparc_pmu->upper_nop : |
| 620 | sparc_pmu->lower_nop, idx); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 621 | } |
| 622 | |
David S. Miller | d175138 | 2009-09-29 21:27:06 -0700 | [diff] [blame] | 623 | static inline void sparc_pmu_enable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx) |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 624 | { |
| 625 | u64 val, mask = mask_for_index(idx); |
David S. Miller | b4f061a | 2012-08-17 03:14:01 -0700 | [diff] [blame^] | 626 | int pcr_index = 0; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 627 | |
David S. Miller | b4f061a | 2012-08-17 03:14:01 -0700 | [diff] [blame^] | 628 | if (sparc_pmu->num_pcrs > 1) |
| 629 | pcr_index = idx; |
| 630 | |
| 631 | val = cpuc->pcr[pcr_index]; |
David S. Miller | d175138 | 2009-09-29 21:27:06 -0700 | [diff] [blame] | 632 | val &= ~mask; |
| 633 | val |= hwc->config; |
David S. Miller | b4f061a | 2012-08-17 03:14:01 -0700 | [diff] [blame^] | 634 | cpuc->pcr[pcr_index] = val; |
David S. Miller | d175138 | 2009-09-29 21:27:06 -0700 | [diff] [blame] | 635 | |
David S. Miller | b4f061a | 2012-08-17 03:14:01 -0700 | [diff] [blame^] | 636 | pcr_ops->write_pcr(pcr_index, cpuc->pcr[pcr_index]); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 637 | } |
| 638 | |
David S. Miller | d175138 | 2009-09-29 21:27:06 -0700 | [diff] [blame] | 639 | static inline void sparc_pmu_disable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx) |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 640 | { |
| 641 | u64 mask = mask_for_index(idx); |
| 642 | u64 nop = nop_for_index(idx); |
David S. Miller | b4f061a | 2012-08-17 03:14:01 -0700 | [diff] [blame^] | 643 | int pcr_index = 0; |
David S. Miller | d175138 | 2009-09-29 21:27:06 -0700 | [diff] [blame] | 644 | u64 val; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 645 | |
David S. Miller | b4f061a | 2012-08-17 03:14:01 -0700 | [diff] [blame^] | 646 | if (sparc_pmu->num_pcrs > 1) |
| 647 | pcr_index = idx; |
| 648 | |
| 649 | val = cpuc->pcr[pcr_index]; |
David S. Miller | d175138 | 2009-09-29 21:27:06 -0700 | [diff] [blame] | 650 | val &= ~mask; |
| 651 | val |= nop; |
David S. Miller | b4f061a | 2012-08-17 03:14:01 -0700 | [diff] [blame^] | 652 | cpuc->pcr[pcr_index] = val; |
David S. Miller | d175138 | 2009-09-29 21:27:06 -0700 | [diff] [blame] | 653 | |
David S. Miller | b4f061a | 2012-08-17 03:14:01 -0700 | [diff] [blame^] | 654 | pcr_ops->write_pcr(pcr_index, cpuc->pcr[pcr_index]); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 655 | } |
| 656 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 657 | static u64 sparc_perf_event_update(struct perf_event *event, |
| 658 | struct hw_perf_event *hwc, int idx) |
| 659 | { |
| 660 | int shift = 64 - 32; |
| 661 | u64 prev_raw_count, new_raw_count; |
| 662 | s64 delta; |
| 663 | |
| 664 | again: |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 665 | prev_raw_count = local64_read(&hwc->prev_count); |
David S. Miller | 5344303 | 2012-08-17 02:37:06 -0700 | [diff] [blame] | 666 | new_raw_count = sparc_pmu->read_pmc(idx); |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 667 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 668 | if (local64_cmpxchg(&hwc->prev_count, prev_raw_count, |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 669 | new_raw_count) != prev_raw_count) |
| 670 | goto again; |
| 671 | |
| 672 | delta = (new_raw_count << shift) - (prev_raw_count << shift); |
| 673 | delta >>= shift; |
| 674 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 675 | local64_add(delta, &event->count); |
| 676 | local64_sub(delta, &hwc->period_left); |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 677 | |
| 678 | return new_raw_count; |
| 679 | } |
| 680 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 681 | static int sparc_perf_event_set_period(struct perf_event *event, |
David S. Miller | d29862f | 2009-09-28 17:37:12 -0700 | [diff] [blame] | 682 | struct hw_perf_event *hwc, int idx) |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 683 | { |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 684 | s64 left = local64_read(&hwc->period_left); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 685 | s64 period = hwc->sample_period; |
| 686 | int ret = 0; |
| 687 | |
| 688 | if (unlikely(left <= -period)) { |
| 689 | left = period; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 690 | local64_set(&hwc->period_left, left); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 691 | hwc->last_period = period; |
| 692 | ret = 1; |
| 693 | } |
| 694 | |
| 695 | if (unlikely(left <= 0)) { |
| 696 | left += period; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 697 | local64_set(&hwc->period_left, left); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 698 | hwc->last_period = period; |
| 699 | ret = 1; |
| 700 | } |
| 701 | if (left > MAX_PERIOD) |
| 702 | left = MAX_PERIOD; |
| 703 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 704 | local64_set(&hwc->prev_count, (u64)-left); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 705 | |
David S. Miller | 5344303 | 2012-08-17 02:37:06 -0700 | [diff] [blame] | 706 | sparc_pmu->write_pmc(idx, (u64)(-left) & 0xffffffff); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 707 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 708 | perf_event_update_userpage(event); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 709 | |
| 710 | return ret; |
| 711 | } |
| 712 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 713 | /* If performance event entries have been added, move existing |
| 714 | * events around (if necessary) and then assign new entries to |
| 715 | * counters. |
| 716 | */ |
| 717 | static u64 maybe_change_configuration(struct cpu_hw_events *cpuc, u64 pcr) |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 718 | { |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 719 | int i; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 720 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 721 | if (!cpuc->n_added) |
| 722 | goto out; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 723 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 724 | /* Read in the counters which are moving. */ |
| 725 | for (i = 0; i < cpuc->n_events; i++) { |
| 726 | struct perf_event *cp = cpuc->event[i]; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 727 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 728 | if (cpuc->current_idx[i] != PIC_NO_INDEX && |
| 729 | cpuc->current_idx[i] != cp->hw.idx) { |
| 730 | sparc_perf_event_update(cp, &cp->hw, |
| 731 | cpuc->current_idx[i]); |
| 732 | cpuc->current_idx[i] = PIC_NO_INDEX; |
| 733 | } |
| 734 | } |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 735 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 736 | /* Assign to counters all unassigned events. */ |
| 737 | for (i = 0; i < cpuc->n_events; i++) { |
| 738 | struct perf_event *cp = cpuc->event[i]; |
| 739 | struct hw_perf_event *hwc = &cp->hw; |
| 740 | int idx = hwc->idx; |
| 741 | u64 enc; |
| 742 | |
| 743 | if (cpuc->current_idx[i] != PIC_NO_INDEX) |
| 744 | continue; |
| 745 | |
| 746 | sparc_perf_event_set_period(cp, hwc, idx); |
| 747 | cpuc->current_idx[i] = idx; |
| 748 | |
| 749 | enc = perf_event_get_enc(cpuc->events[i]); |
David S. Miller | b7d45c3 | 2010-06-23 11:39:02 -0700 | [diff] [blame] | 750 | pcr &= ~mask_for_index(idx); |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 751 | if (hwc->state & PERF_HES_STOPPED) |
| 752 | pcr |= nop_for_index(idx); |
| 753 | else |
| 754 | pcr |= event_encoding(enc, idx); |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 755 | } |
| 756 | out: |
| 757 | return pcr; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 760 | static void sparc_pmu_enable(struct pmu *pmu) |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 761 | { |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 762 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 763 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 764 | if (cpuc->enabled) |
| 765 | return; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 766 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 767 | cpuc->enabled = 1; |
| 768 | barrier(); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 769 | |
David S. Miller | 5ab9684 | 2012-08-17 03:09:39 -0700 | [diff] [blame] | 770 | if (cpuc->n_events) { |
| 771 | u64 pcr = maybe_change_configuration(cpuc, cpuc->pcr[0]); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 772 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 773 | /* We require that all of the events have the same |
| 774 | * configuration, so just fetch the settings from the |
| 775 | * first entry. |
| 776 | */ |
David S. Miller | 3f1a209 | 2012-08-17 02:51:21 -0700 | [diff] [blame] | 777 | cpuc->pcr[0] = pcr | cpuc->event[0]->hw.config_base; |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 778 | } |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 779 | |
David S. Miller | 3f1a209 | 2012-08-17 02:51:21 -0700 | [diff] [blame] | 780 | pcr_ops->write_pcr(0, cpuc->pcr[0]); |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 781 | } |
| 782 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 783 | static void sparc_pmu_disable(struct pmu *pmu) |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 784 | { |
| 785 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
David S. Miller | 3f1a209 | 2012-08-17 02:51:21 -0700 | [diff] [blame] | 786 | int i; |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 787 | |
| 788 | if (!cpuc->enabled) |
| 789 | return; |
| 790 | |
| 791 | cpuc->enabled = 0; |
| 792 | cpuc->n_added = 0; |
| 793 | |
David S. Miller | 3f1a209 | 2012-08-17 02:51:21 -0700 | [diff] [blame] | 794 | for (i = 0; i < sparc_pmu->num_pcrs; i++) { |
| 795 | u64 val = cpuc->pcr[i]; |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 796 | |
David S. Miller | 3f1a209 | 2012-08-17 02:51:21 -0700 | [diff] [blame] | 797 | val &= ~(sparc_pmu->user_bit | sparc_pmu->priv_bit | |
| 798 | sparc_pmu->hv_bit | sparc_pmu->irq_bit); |
| 799 | cpuc->pcr[i] = val; |
| 800 | pcr_ops->write_pcr(i, cpuc->pcr[i]); |
| 801 | } |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 802 | } |
| 803 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 804 | static int active_event_index(struct cpu_hw_events *cpuc, |
| 805 | struct perf_event *event) |
| 806 | { |
| 807 | int i; |
| 808 | |
| 809 | for (i = 0; i < cpuc->n_events; i++) { |
| 810 | if (cpuc->event[i] == event) |
| 811 | break; |
| 812 | } |
| 813 | BUG_ON(i == cpuc->n_events); |
| 814 | return cpuc->current_idx[i]; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 817 | static void sparc_pmu_start(struct perf_event *event, int flags) |
| 818 | { |
| 819 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 820 | int idx = active_event_index(cpuc, event); |
| 821 | |
| 822 | if (flags & PERF_EF_RELOAD) { |
| 823 | WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE)); |
| 824 | sparc_perf_event_set_period(event, &event->hw, idx); |
| 825 | } |
| 826 | |
| 827 | event->hw.state = 0; |
| 828 | |
| 829 | sparc_pmu_enable_event(cpuc, &event->hw, idx); |
| 830 | } |
| 831 | |
| 832 | static void sparc_pmu_stop(struct perf_event *event, int flags) |
| 833 | { |
| 834 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 835 | int idx = active_event_index(cpuc, event); |
| 836 | |
| 837 | if (!(event->hw.state & PERF_HES_STOPPED)) { |
| 838 | sparc_pmu_disable_event(cpuc, &event->hw, idx); |
| 839 | event->hw.state |= PERF_HES_STOPPED; |
| 840 | } |
| 841 | |
| 842 | if (!(event->hw.state & PERF_HES_UPTODATE) && (flags & PERF_EF_UPDATE)) { |
| 843 | sparc_perf_event_update(event, &event->hw, idx); |
| 844 | event->hw.state |= PERF_HES_UPTODATE; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | static void sparc_pmu_del(struct perf_event *event, int _flags) |
| 849 | { |
| 850 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 851 | unsigned long flags; |
| 852 | int i; |
| 853 | |
| 854 | local_irq_save(flags); |
| 855 | perf_pmu_disable(event->pmu); |
| 856 | |
| 857 | for (i = 0; i < cpuc->n_events; i++) { |
| 858 | if (event == cpuc->event[i]) { |
| 859 | /* Absorb the final count and turn off the |
| 860 | * event. |
| 861 | */ |
| 862 | sparc_pmu_stop(event, PERF_EF_UPDATE); |
| 863 | |
| 864 | /* Shift remaining entries down into |
| 865 | * the existing slot. |
| 866 | */ |
| 867 | while (++i < cpuc->n_events) { |
| 868 | cpuc->event[i - 1] = cpuc->event[i]; |
| 869 | cpuc->events[i - 1] = cpuc->events[i]; |
| 870 | cpuc->current_idx[i - 1] = |
| 871 | cpuc->current_idx[i]; |
| 872 | } |
| 873 | |
| 874 | perf_event_update_userpage(event); |
| 875 | |
| 876 | cpuc->n_events--; |
| 877 | break; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | perf_pmu_enable(event->pmu); |
| 882 | local_irq_restore(flags); |
| 883 | } |
| 884 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 885 | static void sparc_pmu_read(struct perf_event *event) |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 886 | { |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 887 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 888 | int idx = active_event_index(cpuc, event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 889 | struct hw_perf_event *hwc = &event->hw; |
David S. Miller | d175138 | 2009-09-29 21:27:06 -0700 | [diff] [blame] | 890 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 891 | sparc_perf_event_update(event, hwc, idx); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 894 | static atomic_t active_events = ATOMIC_INIT(0); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 895 | static DEFINE_MUTEX(pmc_grab_mutex); |
| 896 | |
David S. Miller | d175138 | 2009-09-29 21:27:06 -0700 | [diff] [blame] | 897 | static void perf_stop_nmi_watchdog(void *unused) |
| 898 | { |
| 899 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
David S. Miller | 3f1a209 | 2012-08-17 02:51:21 -0700 | [diff] [blame] | 900 | int i; |
David S. Miller | d175138 | 2009-09-29 21:27:06 -0700 | [diff] [blame] | 901 | |
| 902 | stop_nmi_watchdog(NULL); |
David S. Miller | 3f1a209 | 2012-08-17 02:51:21 -0700 | [diff] [blame] | 903 | for (i = 0; i < sparc_pmu->num_pcrs; i++) |
| 904 | cpuc->pcr[i] = pcr_ops->read_pcr(i); |
David S. Miller | d175138 | 2009-09-29 21:27:06 -0700 | [diff] [blame] | 905 | } |
| 906 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 907 | void perf_event_grab_pmc(void) |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 908 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 909 | if (atomic_inc_not_zero(&active_events)) |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 910 | return; |
| 911 | |
| 912 | mutex_lock(&pmc_grab_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 913 | if (atomic_read(&active_events) == 0) { |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 914 | if (atomic_read(&nmi_active) > 0) { |
David S. Miller | d175138 | 2009-09-29 21:27:06 -0700 | [diff] [blame] | 915 | on_each_cpu(perf_stop_nmi_watchdog, NULL, 1); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 916 | BUG_ON(atomic_read(&nmi_active) != 0); |
| 917 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 918 | atomic_inc(&active_events); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 919 | } |
| 920 | mutex_unlock(&pmc_grab_mutex); |
| 921 | } |
| 922 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 923 | void perf_event_release_pmc(void) |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 924 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 925 | if (atomic_dec_and_mutex_lock(&active_events, &pmc_grab_mutex)) { |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 926 | if (atomic_read(&nmi_active) == 0) |
| 927 | on_each_cpu(start_nmi_watchdog, NULL, 1); |
| 928 | mutex_unlock(&pmc_grab_mutex); |
| 929 | } |
| 930 | } |
| 931 | |
David S. Miller | 2ce4da2 | 2009-09-26 20:42:10 -0700 | [diff] [blame] | 932 | static const struct perf_event_map *sparc_map_cache_event(u64 config) |
| 933 | { |
| 934 | unsigned int cache_type, cache_op, cache_result; |
| 935 | const struct perf_event_map *pmap; |
| 936 | |
| 937 | if (!sparc_pmu->cache_map) |
| 938 | return ERR_PTR(-ENOENT); |
| 939 | |
| 940 | cache_type = (config >> 0) & 0xff; |
| 941 | if (cache_type >= PERF_COUNT_HW_CACHE_MAX) |
| 942 | return ERR_PTR(-EINVAL); |
| 943 | |
| 944 | cache_op = (config >> 8) & 0xff; |
| 945 | if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX) |
| 946 | return ERR_PTR(-EINVAL); |
| 947 | |
| 948 | cache_result = (config >> 16) & 0xff; |
| 949 | if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX) |
| 950 | return ERR_PTR(-EINVAL); |
| 951 | |
| 952 | pmap = &((*sparc_pmu->cache_map)[cache_type][cache_op][cache_result]); |
| 953 | |
| 954 | if (pmap->encoding == CACHE_OP_UNSUPPORTED) |
| 955 | return ERR_PTR(-ENOENT); |
| 956 | |
| 957 | if (pmap->encoding == CACHE_OP_NONSENSE) |
| 958 | return ERR_PTR(-EINVAL); |
| 959 | |
| 960 | return pmap; |
| 961 | } |
| 962 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 963 | static void hw_perf_event_destroy(struct perf_event *event) |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 964 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 965 | perf_event_release_pmc(); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 966 | } |
| 967 | |
David S. Miller | a72a8a5 | 2009-09-28 17:35:20 -0700 | [diff] [blame] | 968 | /* Make sure all events can be scheduled into the hardware at |
| 969 | * the same time. This is simplified by the fact that we only |
| 970 | * need to support 2 simultaneous HW events. |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 971 | * |
| 972 | * As a side effect, the evts[]->hw.idx values will be assigned |
| 973 | * on success. These are pending indexes. When the events are |
| 974 | * actually programmed into the chip, these values will propagate |
| 975 | * to the per-cpu cpuc->current_idx[] slots, see the code in |
| 976 | * maybe_change_configuration() for details. |
David S. Miller | a72a8a5 | 2009-09-28 17:35:20 -0700 | [diff] [blame] | 977 | */ |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 978 | static int sparc_check_constraints(struct perf_event **evts, |
| 979 | unsigned long *events, int n_ev) |
David S. Miller | a72a8a5 | 2009-09-28 17:35:20 -0700 | [diff] [blame] | 980 | { |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 981 | u8 msk0 = 0, msk1 = 0; |
| 982 | int idx0 = 0; |
David S. Miller | a72a8a5 | 2009-09-28 17:35:20 -0700 | [diff] [blame] | 983 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 984 | /* This case is possible when we are invoked from |
| 985 | * hw_perf_group_sched_in(). |
| 986 | */ |
| 987 | if (!n_ev) |
| 988 | return 0; |
David S. Miller | a72a8a5 | 2009-09-28 17:35:20 -0700 | [diff] [blame] | 989 | |
David S. Miller | 5966049 | 2012-08-17 02:33:44 -0700 | [diff] [blame] | 990 | if (n_ev > sparc_pmu->max_hw_events) |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 991 | return -1; |
David S. Miller | a72a8a5 | 2009-09-28 17:35:20 -0700 | [diff] [blame] | 992 | |
David S. Miller | b38e99f | 2012-08-17 02:31:10 -0700 | [diff] [blame] | 993 | if (!(sparc_pmu->flags & SPARC_PMU_HAS_CONFLICTS)) { |
| 994 | int i; |
| 995 | |
| 996 | for (i = 0; i < n_ev; i++) |
| 997 | evts[i]->hw.idx = i; |
| 998 | return 0; |
| 999 | } |
| 1000 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1001 | msk0 = perf_event_get_msk(events[0]); |
| 1002 | if (n_ev == 1) { |
| 1003 | if (msk0 & PIC_LOWER) |
| 1004 | idx0 = 1; |
| 1005 | goto success; |
| 1006 | } |
| 1007 | BUG_ON(n_ev != 2); |
| 1008 | msk1 = perf_event_get_msk(events[1]); |
David S. Miller | a72a8a5 | 2009-09-28 17:35:20 -0700 | [diff] [blame] | 1009 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1010 | /* If both events can go on any counter, OK. */ |
| 1011 | if (msk0 == (PIC_UPPER | PIC_LOWER) && |
| 1012 | msk1 == (PIC_UPPER | PIC_LOWER)) |
| 1013 | goto success; |
David S. Miller | a72a8a5 | 2009-09-28 17:35:20 -0700 | [diff] [blame] | 1014 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1015 | /* If one event is limited to a specific counter, |
| 1016 | * and the other can go on both, OK. |
| 1017 | */ |
| 1018 | if ((msk0 == PIC_UPPER || msk0 == PIC_LOWER) && |
| 1019 | msk1 == (PIC_UPPER | PIC_LOWER)) { |
| 1020 | if (msk0 & PIC_LOWER) |
| 1021 | idx0 = 1; |
| 1022 | goto success; |
David S. Miller | a72a8a5 | 2009-09-28 17:35:20 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1025 | if ((msk1 == PIC_UPPER || msk1 == PIC_LOWER) && |
| 1026 | msk0 == (PIC_UPPER | PIC_LOWER)) { |
| 1027 | if (msk1 & PIC_UPPER) |
| 1028 | idx0 = 1; |
| 1029 | goto success; |
| 1030 | } |
| 1031 | |
| 1032 | /* If the events are fixed to different counters, OK. */ |
| 1033 | if ((msk0 == PIC_UPPER && msk1 == PIC_LOWER) || |
| 1034 | (msk0 == PIC_LOWER && msk1 == PIC_UPPER)) { |
| 1035 | if (msk0 & PIC_LOWER) |
| 1036 | idx0 = 1; |
| 1037 | goto success; |
| 1038 | } |
| 1039 | |
| 1040 | /* Otherwise, there is a conflict. */ |
David S. Miller | a72a8a5 | 2009-09-28 17:35:20 -0700 | [diff] [blame] | 1041 | return -1; |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1042 | |
| 1043 | success: |
| 1044 | evts[0]->hw.idx = idx0; |
| 1045 | if (n_ev == 2) |
| 1046 | evts[1]->hw.idx = idx0 ^ 1; |
| 1047 | return 0; |
David S. Miller | a72a8a5 | 2009-09-28 17:35:20 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
David S. Miller | 01552f7 | 2009-09-27 20:43:07 -0700 | [diff] [blame] | 1050 | static int check_excludes(struct perf_event **evts, int n_prev, int n_new) |
| 1051 | { |
| 1052 | int eu = 0, ek = 0, eh = 0; |
| 1053 | struct perf_event *event; |
| 1054 | int i, n, first; |
| 1055 | |
David S. Miller | b38e99f | 2012-08-17 02:31:10 -0700 | [diff] [blame] | 1056 | if (!(sparc_pmu->flags & SPARC_PMU_ALL_EXCLUDES_SAME)) |
| 1057 | return 0; |
| 1058 | |
David S. Miller | 01552f7 | 2009-09-27 20:43:07 -0700 | [diff] [blame] | 1059 | n = n_prev + n_new; |
| 1060 | if (n <= 1) |
| 1061 | return 0; |
| 1062 | |
| 1063 | first = 1; |
| 1064 | for (i = 0; i < n; i++) { |
| 1065 | event = evts[i]; |
| 1066 | if (first) { |
| 1067 | eu = event->attr.exclude_user; |
| 1068 | ek = event->attr.exclude_kernel; |
| 1069 | eh = event->attr.exclude_hv; |
| 1070 | first = 0; |
| 1071 | } else if (event->attr.exclude_user != eu || |
| 1072 | event->attr.exclude_kernel != ek || |
| 1073 | event->attr.exclude_hv != eh) { |
| 1074 | return -EAGAIN; |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
| 1081 | static int collect_events(struct perf_event *group, int max_count, |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1082 | struct perf_event *evts[], unsigned long *events, |
| 1083 | int *current_idx) |
David S. Miller | 01552f7 | 2009-09-27 20:43:07 -0700 | [diff] [blame] | 1084 | { |
| 1085 | struct perf_event *event; |
| 1086 | int n = 0; |
| 1087 | |
| 1088 | if (!is_software_event(group)) { |
| 1089 | if (n >= max_count) |
| 1090 | return -1; |
| 1091 | evts[n] = group; |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1092 | events[n] = group->hw.event_base; |
| 1093 | current_idx[n++] = PIC_NO_INDEX; |
David S. Miller | 01552f7 | 2009-09-27 20:43:07 -0700 | [diff] [blame] | 1094 | } |
| 1095 | list_for_each_entry(event, &group->sibling_list, group_entry) { |
| 1096 | if (!is_software_event(event) && |
| 1097 | event->state != PERF_EVENT_STATE_OFF) { |
| 1098 | if (n >= max_count) |
| 1099 | return -1; |
| 1100 | evts[n] = event; |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1101 | events[n] = event->hw.event_base; |
| 1102 | current_idx[n++] = PIC_NO_INDEX; |
David S. Miller | 01552f7 | 2009-09-27 20:43:07 -0700 | [diff] [blame] | 1103 | } |
| 1104 | } |
| 1105 | return n; |
| 1106 | } |
| 1107 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1108 | static int sparc_pmu_add(struct perf_event *event, int ef_flags) |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1109 | { |
| 1110 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 1111 | int n0, ret = -EAGAIN; |
| 1112 | unsigned long flags; |
| 1113 | |
| 1114 | local_irq_save(flags); |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1115 | perf_pmu_disable(event->pmu); |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1116 | |
| 1117 | n0 = cpuc->n_events; |
David S. Miller | 5966049 | 2012-08-17 02:33:44 -0700 | [diff] [blame] | 1118 | if (n0 >= sparc_pmu->max_hw_events) |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1119 | goto out; |
| 1120 | |
| 1121 | cpuc->event[n0] = event; |
| 1122 | cpuc->events[n0] = event->hw.event_base; |
| 1123 | cpuc->current_idx[n0] = PIC_NO_INDEX; |
| 1124 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1125 | event->hw.state = PERF_HES_UPTODATE; |
| 1126 | if (!(ef_flags & PERF_EF_START)) |
| 1127 | event->hw.state |= PERF_HES_STOPPED; |
| 1128 | |
Lin Ming | a13c3af | 2010-04-23 13:56:33 +0800 | [diff] [blame] | 1129 | /* |
| 1130 | * If group events scheduling transaction was started, |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1131 | * skip the schedulability test here, it will be performed |
Lin Ming | a13c3af | 2010-04-23 13:56:33 +0800 | [diff] [blame] | 1132 | * at commit time(->commit_txn) as a whole |
| 1133 | */ |
Peter Zijlstra | 8d2cacb | 2010-05-25 17:49:05 +0200 | [diff] [blame] | 1134 | if (cpuc->group_flag & PERF_EVENT_TXN) |
Lin Ming | a13c3af | 2010-04-23 13:56:33 +0800 | [diff] [blame] | 1135 | goto nocheck; |
| 1136 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1137 | if (check_excludes(cpuc->event, n0, 1)) |
| 1138 | goto out; |
| 1139 | if (sparc_check_constraints(cpuc->event, cpuc->events, n0 + 1)) |
| 1140 | goto out; |
| 1141 | |
Lin Ming | a13c3af | 2010-04-23 13:56:33 +0800 | [diff] [blame] | 1142 | nocheck: |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1143 | cpuc->n_events++; |
| 1144 | cpuc->n_added++; |
| 1145 | |
| 1146 | ret = 0; |
| 1147 | out: |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1148 | perf_pmu_enable(event->pmu); |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1149 | local_irq_restore(flags); |
| 1150 | return ret; |
| 1151 | } |
| 1152 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1153 | static int sparc_pmu_event_init(struct perf_event *event) |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1154 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1155 | struct perf_event_attr *attr = &event->attr; |
David S. Miller | 01552f7 | 2009-09-27 20:43:07 -0700 | [diff] [blame] | 1156 | struct perf_event *evts[MAX_HWEVENTS]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1157 | struct hw_perf_event *hwc = &event->hw; |
David S. Miller | a72a8a5 | 2009-09-28 17:35:20 -0700 | [diff] [blame] | 1158 | unsigned long events[MAX_HWEVENTS]; |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1159 | int current_idx_dmy[MAX_HWEVENTS]; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1160 | const struct perf_event_map *pmap; |
David S. Miller | 01552f7 | 2009-09-27 20:43:07 -0700 | [diff] [blame] | 1161 | int n; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1162 | |
| 1163 | if (atomic_read(&nmi_active) < 0) |
| 1164 | return -ENODEV; |
| 1165 | |
Stephane Eranian | 2481c5f | 2012-02-09 23:20:59 +0100 | [diff] [blame] | 1166 | /* does not support taken branch sampling */ |
| 1167 | if (has_branch_stack(event)) |
| 1168 | return -EOPNOTSUPP; |
| 1169 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1170 | switch (attr->type) { |
| 1171 | case PERF_TYPE_HARDWARE: |
David S. Miller | 2ce4da2 | 2009-09-26 20:42:10 -0700 | [diff] [blame] | 1172 | if (attr->config >= sparc_pmu->max_events) |
| 1173 | return -EINVAL; |
| 1174 | pmap = sparc_pmu->event_map(attr->config); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1175 | break; |
| 1176 | |
| 1177 | case PERF_TYPE_HW_CACHE: |
David S. Miller | 2ce4da2 | 2009-09-26 20:42:10 -0700 | [diff] [blame] | 1178 | pmap = sparc_map_cache_event(attr->config); |
| 1179 | if (IS_ERR(pmap)) |
| 1180 | return PTR_ERR(pmap); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1181 | break; |
| 1182 | |
| 1183 | case PERF_TYPE_RAW: |
Ingo Molnar | d0303d7 | 2010-09-23 08:02:09 +0200 | [diff] [blame] | 1184 | pmap = NULL; |
| 1185 | break; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1186 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1187 | default: |
| 1188 | return -ENOENT; |
| 1189 | |
| 1190 | } |
| 1191 | |
David S. Miller | b343ae5 | 2010-09-12 17:20:24 -0700 | [diff] [blame] | 1192 | if (pmap) { |
| 1193 | hwc->event_base = perf_event_encode(pmap); |
| 1194 | } else { |
Ingo Molnar | d0303d7 | 2010-09-23 08:02:09 +0200 | [diff] [blame] | 1195 | /* |
| 1196 | * User gives us "(encoding << 16) | pic_mask" for |
David S. Miller | b343ae5 | 2010-09-12 17:20:24 -0700 | [diff] [blame] | 1197 | * PERF_TYPE_RAW events. |
| 1198 | */ |
| 1199 | hwc->event_base = attr->config; |
| 1200 | } |
| 1201 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1202 | /* We save the enable bits in the config_base. */ |
David S. Miller | 496c07e | 2009-09-10 07:10:59 -0700 | [diff] [blame] | 1203 | hwc->config_base = sparc_pmu->irq_bit; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1204 | if (!attr->exclude_user) |
David S. Miller | 7ac2ed2 | 2012-08-17 02:41:32 -0700 | [diff] [blame] | 1205 | hwc->config_base |= sparc_pmu->user_bit; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1206 | if (!attr->exclude_kernel) |
David S. Miller | 7ac2ed2 | 2012-08-17 02:41:32 -0700 | [diff] [blame] | 1207 | hwc->config_base |= sparc_pmu->priv_bit; |
David S. Miller | 91b9286 | 2009-09-10 07:09:06 -0700 | [diff] [blame] | 1208 | if (!attr->exclude_hv) |
| 1209 | hwc->config_base |= sparc_pmu->hv_bit; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1210 | |
David S. Miller | 01552f7 | 2009-09-27 20:43:07 -0700 | [diff] [blame] | 1211 | n = 0; |
| 1212 | if (event->group_leader != event) { |
| 1213 | n = collect_events(event->group_leader, |
David S. Miller | 5966049 | 2012-08-17 02:33:44 -0700 | [diff] [blame] | 1214 | sparc_pmu->max_hw_events - 1, |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1215 | evts, events, current_idx_dmy); |
David S. Miller | 01552f7 | 2009-09-27 20:43:07 -0700 | [diff] [blame] | 1216 | if (n < 0) |
| 1217 | return -EINVAL; |
| 1218 | } |
David S. Miller | a72a8a5 | 2009-09-28 17:35:20 -0700 | [diff] [blame] | 1219 | events[n] = hwc->event_base; |
David S. Miller | 01552f7 | 2009-09-27 20:43:07 -0700 | [diff] [blame] | 1220 | evts[n] = event; |
| 1221 | |
| 1222 | if (check_excludes(evts, n, 1)) |
| 1223 | return -EINVAL; |
| 1224 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1225 | if (sparc_check_constraints(evts, events, n + 1)) |
David S. Miller | a72a8a5 | 2009-09-28 17:35:20 -0700 | [diff] [blame] | 1226 | return -EINVAL; |
| 1227 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1228 | hwc->idx = PIC_NO_INDEX; |
| 1229 | |
David S. Miller | 01552f7 | 2009-09-27 20:43:07 -0700 | [diff] [blame] | 1230 | /* Try to do all error checking before this point, as unwinding |
| 1231 | * state after grabbing the PMC is difficult. |
| 1232 | */ |
| 1233 | perf_event_grab_pmc(); |
| 1234 | event->destroy = hw_perf_event_destroy; |
| 1235 | |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1236 | if (!hwc->sample_period) { |
| 1237 | hwc->sample_period = MAX_PERIOD; |
| 1238 | hwc->last_period = hwc->sample_period; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1239 | local64_set(&hwc->period_left, hwc->sample_period); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1242 | return 0; |
| 1243 | } |
| 1244 | |
Lin Ming | a13c3af | 2010-04-23 13:56:33 +0800 | [diff] [blame] | 1245 | /* |
| 1246 | * Start group events scheduling transaction |
| 1247 | * Set the flag to make pmu::enable() not perform the |
| 1248 | * schedulability test, it will be performed at commit time |
| 1249 | */ |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 1250 | static void sparc_pmu_start_txn(struct pmu *pmu) |
Lin Ming | a13c3af | 2010-04-23 13:56:33 +0800 | [diff] [blame] | 1251 | { |
| 1252 | struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events); |
| 1253 | |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1254 | perf_pmu_disable(pmu); |
Peter Zijlstra | 8d2cacb | 2010-05-25 17:49:05 +0200 | [diff] [blame] | 1255 | cpuhw->group_flag |= PERF_EVENT_TXN; |
Lin Ming | a13c3af | 2010-04-23 13:56:33 +0800 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | /* |
| 1259 | * Stop group events scheduling transaction |
| 1260 | * Clear the flag and pmu::enable() will perform the |
| 1261 | * schedulability test. |
| 1262 | */ |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 1263 | static void sparc_pmu_cancel_txn(struct pmu *pmu) |
Lin Ming | a13c3af | 2010-04-23 13:56:33 +0800 | [diff] [blame] | 1264 | { |
| 1265 | struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events); |
| 1266 | |
Peter Zijlstra | 8d2cacb | 2010-05-25 17:49:05 +0200 | [diff] [blame] | 1267 | cpuhw->group_flag &= ~PERF_EVENT_TXN; |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1268 | perf_pmu_enable(pmu); |
Lin Ming | a13c3af | 2010-04-23 13:56:33 +0800 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | /* |
| 1272 | * Commit group events scheduling transaction |
| 1273 | * Perform the group schedulability test as a whole |
| 1274 | * Return 0 if success |
| 1275 | */ |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 1276 | static int sparc_pmu_commit_txn(struct pmu *pmu) |
Lin Ming | a13c3af | 2010-04-23 13:56:33 +0800 | [diff] [blame] | 1277 | { |
| 1278 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 1279 | int n; |
| 1280 | |
| 1281 | if (!sparc_pmu) |
| 1282 | return -EINVAL; |
| 1283 | |
| 1284 | cpuc = &__get_cpu_var(cpu_hw_events); |
| 1285 | n = cpuc->n_events; |
| 1286 | if (check_excludes(cpuc->event, 0, n)) |
| 1287 | return -EINVAL; |
| 1288 | if (sparc_check_constraints(cpuc->event, cpuc->events, n)) |
| 1289 | return -EAGAIN; |
| 1290 | |
Peter Zijlstra | 8d2cacb | 2010-05-25 17:49:05 +0200 | [diff] [blame] | 1291 | cpuc->group_flag &= ~PERF_EVENT_TXN; |
Peter Zijlstra | 33696fc | 2010-06-14 08:49:00 +0200 | [diff] [blame] | 1292 | perf_pmu_enable(pmu); |
Lin Ming | a13c3af | 2010-04-23 13:56:33 +0800 | [diff] [blame] | 1293 | return 0; |
| 1294 | } |
| 1295 | |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 1296 | static struct pmu pmu = { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1297 | .pmu_enable = sparc_pmu_enable, |
| 1298 | .pmu_disable = sparc_pmu_disable, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 1299 | .event_init = sparc_pmu_event_init, |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1300 | .add = sparc_pmu_add, |
| 1301 | .del = sparc_pmu_del, |
| 1302 | .start = sparc_pmu_start, |
| 1303 | .stop = sparc_pmu_stop, |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1304 | .read = sparc_pmu_read, |
Lin Ming | a13c3af | 2010-04-23 13:56:33 +0800 | [diff] [blame] | 1305 | .start_txn = sparc_pmu_start_txn, |
| 1306 | .cancel_txn = sparc_pmu_cancel_txn, |
| 1307 | .commit_txn = sparc_pmu_commit_txn, |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1308 | }; |
| 1309 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1310 | void perf_event_print_debug(void) |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1311 | { |
| 1312 | unsigned long flags; |
David S. Miller | 3f1a209 | 2012-08-17 02:51:21 -0700 | [diff] [blame] | 1313 | int cpu, i; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1314 | |
| 1315 | if (!sparc_pmu) |
| 1316 | return; |
| 1317 | |
| 1318 | local_irq_save(flags); |
| 1319 | |
| 1320 | cpu = smp_processor_id(); |
| 1321 | |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1322 | pr_info("\n"); |
David S. Miller | 3f1a209 | 2012-08-17 02:51:21 -0700 | [diff] [blame] | 1323 | for (i = 0; i < sparc_pmu->num_pcrs; i++) |
| 1324 | pr_info("CPU#%d: PCR%d[%016llx]\n", |
| 1325 | cpu, i, pcr_ops->read_pcr(i)); |
| 1326 | for (i = 0; i < sparc_pmu->num_pic_regs; i++) |
| 1327 | pr_info("CPU#%d: PIC%d[%016llx]\n", |
| 1328 | cpu, i, pcr_ops->read_pic(i)); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1329 | |
| 1330 | local_irq_restore(flags); |
| 1331 | } |
| 1332 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1333 | static int __kprobes perf_event_nmi_handler(struct notifier_block *self, |
David S. Miller | d29862f | 2009-09-28 17:37:12 -0700 | [diff] [blame] | 1334 | unsigned long cmd, void *__args) |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1335 | { |
| 1336 | struct die_args *args = __args; |
| 1337 | struct perf_sample_data data; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1338 | struct cpu_hw_events *cpuc; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1339 | struct pt_regs *regs; |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1340 | int i; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1341 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1342 | if (!atomic_read(&active_events)) |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1343 | return NOTIFY_DONE; |
| 1344 | |
| 1345 | switch (cmd) { |
| 1346 | case DIE_NMI: |
| 1347 | break; |
| 1348 | |
| 1349 | default: |
| 1350 | return NOTIFY_DONE; |
| 1351 | } |
| 1352 | |
| 1353 | regs = args->regs; |
| 1354 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1355 | cpuc = &__get_cpu_var(cpu_hw_events); |
David S. Miller | e04ed38 | 2010-01-04 23:16:03 -0800 | [diff] [blame] | 1356 | |
| 1357 | /* If the PMU has the TOE IRQ enable bits, we need to do a |
| 1358 | * dummy write to the %pcr to clear the overflow bits and thus |
| 1359 | * the interrupt. |
| 1360 | * |
| 1361 | * Do this before we peek at the counters to determine |
| 1362 | * overflow so we don't lose any events. |
| 1363 | */ |
David S. Miller | 3f1a209 | 2012-08-17 02:51:21 -0700 | [diff] [blame] | 1364 | if (sparc_pmu->irq_bit && |
| 1365 | sparc_pmu->num_pcrs == 1) |
| 1366 | pcr_ops->write_pcr(0, cpuc->pcr[0]); |
David S. Miller | e04ed38 | 2010-01-04 23:16:03 -0800 | [diff] [blame] | 1367 | |
David S. Miller | e7bef6b | 2010-01-20 02:59:47 -0800 | [diff] [blame] | 1368 | for (i = 0; i < cpuc->n_events; i++) { |
| 1369 | struct perf_event *event = cpuc->event[i]; |
| 1370 | int idx = cpuc->current_idx[i]; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1371 | struct hw_perf_event *hwc; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1372 | u64 val; |
| 1373 | |
David S. Miller | 3f1a209 | 2012-08-17 02:51:21 -0700 | [diff] [blame] | 1374 | if (sparc_pmu->irq_bit && |
| 1375 | sparc_pmu->num_pcrs > 1) |
| 1376 | pcr_ops->write_pcr(idx, cpuc->pcr[idx]); |
| 1377 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1378 | hwc = &event->hw; |
| 1379 | val = sparc_perf_event_update(event, hwc, idx); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1380 | if (val & (1ULL << 31)) |
| 1381 | continue; |
| 1382 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 1383 | perf_sample_data_init(&data, 0, hwc->last_period); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1384 | if (!sparc_perf_event_set_period(event, hwc, idx)) |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1385 | continue; |
| 1386 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 1387 | if (perf_event_overflow(event, &data, regs)) |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1388 | sparc_pmu_stop(event, 0); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | return NOTIFY_STOP; |
| 1392 | } |
| 1393 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1394 | static __read_mostly struct notifier_block perf_event_nmi_notifier = { |
| 1395 | .notifier_call = perf_event_nmi_handler, |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1396 | }; |
| 1397 | |
| 1398 | static bool __init supported_pmu(void) |
| 1399 | { |
David S. Miller | 28e8f9b | 2009-09-26 20:54:22 -0700 | [diff] [blame] | 1400 | if (!strcmp(sparc_pmu_type, "ultra3") || |
| 1401 | !strcmp(sparc_pmu_type, "ultra3+") || |
| 1402 | !strcmp(sparc_pmu_type, "ultra3i") || |
| 1403 | !strcmp(sparc_pmu_type, "ultra4+")) { |
| 1404 | sparc_pmu = &ultra3_pmu; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1405 | return true; |
| 1406 | } |
David S. Miller | 7eebda6 | 2009-09-26 21:23:41 -0700 | [diff] [blame] | 1407 | if (!strcmp(sparc_pmu_type, "niagara")) { |
| 1408 | sparc_pmu = &niagara1_pmu; |
| 1409 | return true; |
| 1410 | } |
David S. Miller | 4ba991d | 2011-07-27 21:06:16 -0700 | [diff] [blame] | 1411 | if (!strcmp(sparc_pmu_type, "niagara2") || |
| 1412 | !strcmp(sparc_pmu_type, "niagara3")) { |
David S. Miller | b73d884 | 2009-09-10 07:22:18 -0700 | [diff] [blame] | 1413 | sparc_pmu = &niagara2_pmu; |
| 1414 | return true; |
| 1415 | } |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1416 | return false; |
| 1417 | } |
| 1418 | |
Peter Zijlstra | 004417a | 2010-11-25 18:38:29 +0100 | [diff] [blame] | 1419 | int __init init_hw_perf_events(void) |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1420 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1421 | pr_info("Performance events: "); |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1422 | |
| 1423 | if (!supported_pmu()) { |
| 1424 | pr_cont("No support for PMU type '%s'\n", sparc_pmu_type); |
Peter Zijlstra | 004417a | 2010-11-25 18:38:29 +0100 | [diff] [blame] | 1425 | return 0; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | pr_cont("Supported PMU type is '%s'\n", sparc_pmu_type); |
| 1429 | |
Peter Zijlstra | 2e80a82 | 2010-11-17 23:17:36 +0100 | [diff] [blame] | 1430 | perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1431 | register_die_notifier(&perf_event_nmi_notifier); |
Peter Zijlstra | 004417a | 2010-11-25 18:38:29 +0100 | [diff] [blame] | 1432 | |
| 1433 | return 0; |
David S. Miller | 59abbd1 | 2009-09-10 06:28:20 -0700 | [diff] [blame] | 1434 | } |
Ingo Molnar | efc70d2 | 2010-12-10 00:27:23 +0100 | [diff] [blame] | 1435 | early_initcall(init_hw_perf_events); |
David S. Miller | 4f6dbe4 | 2010-01-19 00:26:13 -0800 | [diff] [blame] | 1436 | |
Frederic Weisbecker | 56962b4 | 2010-06-30 23:03:51 +0200 | [diff] [blame] | 1437 | void perf_callchain_kernel(struct perf_callchain_entry *entry, |
| 1438 | struct pt_regs *regs) |
David S. Miller | 4f6dbe4 | 2010-01-19 00:26:13 -0800 | [diff] [blame] | 1439 | { |
| 1440 | unsigned long ksp, fp; |
David S. Miller | 667f0ce | 2010-04-21 03:08:11 -0700 | [diff] [blame] | 1441 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 1442 | int graph = 0; |
| 1443 | #endif |
David S. Miller | 4f6dbe4 | 2010-01-19 00:26:13 -0800 | [diff] [blame] | 1444 | |
Frederic Weisbecker | 56962b4 | 2010-06-30 23:03:51 +0200 | [diff] [blame] | 1445 | stack_trace_flush(); |
| 1446 | |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 1447 | perf_callchain_store(entry, regs->tpc); |
David S. Miller | 4f6dbe4 | 2010-01-19 00:26:13 -0800 | [diff] [blame] | 1448 | |
| 1449 | ksp = regs->u_regs[UREG_I6]; |
| 1450 | fp = ksp + STACK_BIAS; |
| 1451 | do { |
| 1452 | struct sparc_stackf *sf; |
| 1453 | struct pt_regs *regs; |
| 1454 | unsigned long pc; |
| 1455 | |
| 1456 | if (!kstack_valid(current_thread_info(), fp)) |
| 1457 | break; |
| 1458 | |
| 1459 | sf = (struct sparc_stackf *) fp; |
| 1460 | regs = (struct pt_regs *) (sf + 1); |
| 1461 | |
| 1462 | if (kstack_is_trap_frame(current_thread_info(), regs)) { |
| 1463 | if (user_mode(regs)) |
| 1464 | break; |
| 1465 | pc = regs->tpc; |
| 1466 | fp = regs->u_regs[UREG_I6] + STACK_BIAS; |
| 1467 | } else { |
| 1468 | pc = sf->callers_pc; |
| 1469 | fp = (unsigned long)sf->fp + STACK_BIAS; |
| 1470 | } |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 1471 | perf_callchain_store(entry, pc); |
David S. Miller | 667f0ce | 2010-04-21 03:08:11 -0700 | [diff] [blame] | 1472 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 1473 | if ((pc + 8UL) == (unsigned long) &return_to_handler) { |
| 1474 | int index = current->curr_ret_stack; |
| 1475 | if (current->ret_stack && index >= graph) { |
| 1476 | pc = current->ret_stack[index - graph].ret; |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 1477 | perf_callchain_store(entry, pc); |
David S. Miller | 667f0ce | 2010-04-21 03:08:11 -0700 | [diff] [blame] | 1478 | graph++; |
| 1479 | } |
| 1480 | } |
| 1481 | #endif |
David S. Miller | 4f6dbe4 | 2010-01-19 00:26:13 -0800 | [diff] [blame] | 1482 | } while (entry->nr < PERF_MAX_STACK_DEPTH); |
| 1483 | } |
| 1484 | |
Frederic Weisbecker | 56962b4 | 2010-06-30 23:03:51 +0200 | [diff] [blame] | 1485 | static void perf_callchain_user_64(struct perf_callchain_entry *entry, |
| 1486 | struct pt_regs *regs) |
David S. Miller | 4f6dbe4 | 2010-01-19 00:26:13 -0800 | [diff] [blame] | 1487 | { |
| 1488 | unsigned long ufp; |
| 1489 | |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 1490 | perf_callchain_store(entry, regs->tpc); |
David S. Miller | 4f6dbe4 | 2010-01-19 00:26:13 -0800 | [diff] [blame] | 1491 | |
| 1492 | ufp = regs->u_regs[UREG_I6] + STACK_BIAS; |
| 1493 | do { |
| 1494 | struct sparc_stackf *usf, sf; |
| 1495 | unsigned long pc; |
| 1496 | |
| 1497 | usf = (struct sparc_stackf *) ufp; |
| 1498 | if (__copy_from_user_inatomic(&sf, usf, sizeof(sf))) |
| 1499 | break; |
| 1500 | |
| 1501 | pc = sf.callers_pc; |
| 1502 | ufp = (unsigned long)sf.fp + STACK_BIAS; |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 1503 | perf_callchain_store(entry, pc); |
David S. Miller | 4f6dbe4 | 2010-01-19 00:26:13 -0800 | [diff] [blame] | 1504 | } while (entry->nr < PERF_MAX_STACK_DEPTH); |
| 1505 | } |
| 1506 | |
Frederic Weisbecker | 56962b4 | 2010-06-30 23:03:51 +0200 | [diff] [blame] | 1507 | static void perf_callchain_user_32(struct perf_callchain_entry *entry, |
| 1508 | struct pt_regs *regs) |
David S. Miller | 4f6dbe4 | 2010-01-19 00:26:13 -0800 | [diff] [blame] | 1509 | { |
| 1510 | unsigned long ufp; |
| 1511 | |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 1512 | perf_callchain_store(entry, regs->tpc); |
David S. Miller | 4f6dbe4 | 2010-01-19 00:26:13 -0800 | [diff] [blame] | 1513 | |
David S. Miller | 9e8307e | 2010-03-29 13:08:52 -0700 | [diff] [blame] | 1514 | ufp = regs->u_regs[UREG_I6] & 0xffffffffUL; |
David S. Miller | 4f6dbe4 | 2010-01-19 00:26:13 -0800 | [diff] [blame] | 1515 | do { |
| 1516 | struct sparc_stackf32 *usf, sf; |
| 1517 | unsigned long pc; |
| 1518 | |
| 1519 | usf = (struct sparc_stackf32 *) ufp; |
| 1520 | if (__copy_from_user_inatomic(&sf, usf, sizeof(sf))) |
| 1521 | break; |
| 1522 | |
| 1523 | pc = sf.callers_pc; |
| 1524 | ufp = (unsigned long)sf.fp; |
Frederic Weisbecker | 70791ce | 2010-06-29 19:34:05 +0200 | [diff] [blame] | 1525 | perf_callchain_store(entry, pc); |
David S. Miller | 4f6dbe4 | 2010-01-19 00:26:13 -0800 | [diff] [blame] | 1526 | } while (entry->nr < PERF_MAX_STACK_DEPTH); |
| 1527 | } |
| 1528 | |
Frederic Weisbecker | 56962b4 | 2010-06-30 23:03:51 +0200 | [diff] [blame] | 1529 | void |
| 1530 | perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs) |
David S. Miller | 4f6dbe4 | 2010-01-19 00:26:13 -0800 | [diff] [blame] | 1531 | { |
Frederic Weisbecker | 56962b4 | 2010-06-30 23:03:51 +0200 | [diff] [blame] | 1532 | flushw_user(); |
| 1533 | if (test_thread_flag(TIF_32BIT)) |
| 1534 | perf_callchain_user_32(entry, regs); |
| 1535 | else |
| 1536 | perf_callchain_user_64(entry, regs); |
David S. Miller | 4f6dbe4 | 2010-01-19 00:26:13 -0800 | [diff] [blame] | 1537 | } |