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