blob: 9be089abb5d1e2b7f95e4b6b1dffbd5aa9c0fbe7 [file] [log] [blame]
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001/* Performance event support for sparc64.
David S. Miller59abbd12009-09-10 06:28:20 -07002 *
David S. Miller4f6dbe42010-01-19 00:26:13 -08003 * Copyright (C) 2009, 2010 David S. Miller <davem@davemloft.net>
David S. Miller59abbd12009-09-10 06:28:20 -07004 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005 * This code is based almost entirely upon the x86 perf event
David S. Miller59abbd12009-09-10 06:28:20 -07006 * 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 Molnarcdd6c482009-09-21 12:02:48 +020015#include <linux/perf_event.h>
David S. Miller59abbd12009-09-10 06:28:20 -070016#include <linux/kprobes.h>
David S. Miller667f0ce2010-04-21 03:08:11 -070017#include <linux/ftrace.h>
David S. Miller59abbd12009-09-10 06:28:20 -070018#include <linux/kernel.h>
19#include <linux/kdebug.h>
20#include <linux/mutex.h>
21
David S. Miller4f6dbe42010-01-19 00:26:13 -080022#include <asm/stacktrace.h>
David S. Miller59abbd12009-09-10 06:28:20 -070023#include <asm/cpudata.h>
David S. Miller4f6dbe42010-01-19 00:26:13 -080024#include <asm/uaccess.h>
Arun Sharma600634972011-07-26 16:09:06 -070025#include <linux/atomic.h>
David S. Miller59abbd12009-09-10 06:28:20 -070026#include <asm/nmi.h>
27#include <asm/pcr.h>
David Howellsd550bbd2012-03-28 18:30:03 +010028#include <asm/cacheflush.h>
David S. Miller59abbd12009-09-10 06:28:20 -070029
Sam Ravnborgcb1b8202011-04-21 15:45:45 -070030#include "kernel.h"
David S. Miller4f6dbe42010-01-19 00:26:13 -080031#include "kstack.h"
32
David S. Miller59abbd12009-09-10 06:28:20 -070033/* 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 Molnarcdd6c482009-09-21 12:02:48 +020056#define MAX_HWEVENTS 2
David S. Miller3f1a2092012-08-17 02:51:21 -070057#define MAX_PCRS 1
David S. Miller59abbd12009-09-10 06:28:20 -070058#define MAX_PERIOD ((1UL << 32) - 1)
59
60#define PIC_UPPER_INDEX 0
61#define PIC_LOWER_INDEX 1
David S. Millere7bef6b2010-01-20 02:59:47 -080062#define PIC_NO_INDEX -1
David S. Miller59abbd12009-09-10 06:28:20 -070063
Ingo Molnarcdd6c482009-09-21 12:02:48 +020064struct cpu_hw_events {
David S. Millere7bef6b2010-01-20 02:59:47 -080065 /* 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. Miller3f1a2092012-08-17 02:51:21 -070093 /* Software copy of %pcr register(s) on this cpu. */
94 u64 pcr[MAX_HWEVENTS];
David S. Millere7bef6b2010-01-20 02:59:47 -080095
96 /* Enabled/disable state. */
David S. Millerd1751382009-09-29 21:27:06 -070097 int enabled;
Lin Minga13c3af2010-04-23 13:56:33 +080098
99 unsigned int group_flag;
David S. Miller59abbd12009-09-10 06:28:20 -0700100};
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200101DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { .enabled = 1, };
David S. Miller59abbd12009-09-10 06:28:20 -0700102
David S. Millere7bef6b2010-01-20 02:59:47 -0800103/* 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. Miller59abbd12009-09-10 06:28:20 -0700107struct 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. Millere7bef6b2010-01-20 02:59:47 -0800115/* Encode a perf_event_map entry into a long. */
David S. Millera72a8a52009-09-28 17:35:20 -0700116static 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. Millere7bef6b2010-01-20 02:59:47 -0800121static u8 perf_event_get_msk(unsigned long val)
David S. Millera72a8a52009-09-28 17:35:20 -0700122{
David S. Millere7bef6b2010-01-20 02:59:47 -0800123 return val & 0xff;
124}
125
126static u64 perf_event_get_enc(unsigned long val)
127{
128 return val >> 16;
David S. Millera72a8a52009-09-28 17:35:20 -0700129}
130
David S. Miller2ce4da22009-09-26 20:42:10 -0700131#define C(x) PERF_COUNT_HW_CACHE_##x
132
133#define CACHE_OP_UNSUPPORTED 0xfffe
134#define CACHE_OP_NONSENSE 0xffff
135
136typedef 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. Miller59abbd12009-09-10 06:28:20 -0700141struct sparc_pmu {
142 const struct perf_event_map *(*event_map)(int);
David S. Miller2ce4da22009-09-26 20:42:10 -0700143 const cache_map_t *cache_map;
David S. Miller59abbd12009-09-10 06:28:20 -0700144 int max_events;
David S. Miller53443032012-08-17 02:37:06 -0700145 u32 (*read_pmc)(int);
146 void (*write_pmc)(int, u64);
David S. Miller59abbd12009-09-10 06:28:20 -0700147 int upper_shift;
148 int lower_shift;
149 int event_mask;
David S. Miller7ac2ed22012-08-17 02:41:32 -0700150 int user_bit;
151 int priv_bit;
David S. Miller91b92862009-09-10 07:09:06 -0700152 int hv_bit;
David S. Miller496c07e2009-09-10 07:10:59 -0700153 int irq_bit;
David S. Miller660d1372009-09-10 07:13:26 -0700154 int upper_nop;
155 int lower_nop;
David S. Millerb38e99f2012-08-17 02:31:10 -0700156 unsigned int flags;
157#define SPARC_PMU_ALL_EXCLUDES_SAME 0x00000001
158#define SPARC_PMU_HAS_CONFLICTS 0x00000002
David S. Miller59660492012-08-17 02:33:44 -0700159 int max_hw_events;
David S. Miller3f1a2092012-08-17 02:51:21 -0700160 int num_pcrs;
161 int num_pic_regs;
David S. Miller59abbd12009-09-10 06:28:20 -0700162};
163
David S. Miller53443032012-08-17 02:37:06 -0700164static 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
175static 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. Miller28e8f9b2009-09-26 20:54:22 -0700192static const struct perf_event_map ultra3_perfmon_event_map[] = {
David S. Miller59abbd12009-09-10 06:28:20 -0700193 [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. Miller28e8f9b2009-09-26 20:54:22 -0700199static const struct perf_event_map *ultra3_event_map(int event_id)
David S. Miller59abbd12009-09-10 06:28:20 -0700200{
David S. Miller28e8f9b2009-09-26 20:54:22 -0700201 return &ultra3_perfmon_event_map[event_id];
David S. Miller59abbd12009-09-10 06:28:20 -0700202}
203
David S. Miller28e8f9b2009-09-26 20:54:22 -0700204static const cache_map_t ultra3_cache_map = {
David S. Miller2ce4da22009-09-26 20:42:10 -0700205[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 Zijlstra89d6c0b2011-04-22 23:37:06 +0200289[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. Miller2ce4da22009-09-26 20:42:10 -0700303};
304
David S. Miller28e8f9b2009-09-26 20:54:22 -0700305static 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. Miller53443032012-08-17 02:37:06 -0700309 .read_pmc = sparc_default_read_pmc,
310 .write_pmc = sparc_default_write_pmc,
David S. Miller59abbd12009-09-10 06:28:20 -0700311 .upper_shift = 11,
312 .lower_shift = 4,
313 .event_mask = 0x3f,
David S. Miller7ac2ed22012-08-17 02:41:32 -0700314 .user_bit = PCR_UTRACE,
315 .priv_bit = PCR_STRACE,
David S. Miller660d1372009-09-10 07:13:26 -0700316 .upper_nop = 0x1c,
317 .lower_nop = 0x14,
David S. Millerb38e99f2012-08-17 02:31:10 -0700318 .flags = (SPARC_PMU_ALL_EXCLUDES_SAME |
319 SPARC_PMU_HAS_CONFLICTS),
David S. Miller59660492012-08-17 02:33:44 -0700320 .max_hw_events = 2,
David S. Miller3f1a2092012-08-17 02:51:21 -0700321 .num_pcrs = 1,
322 .num_pic_regs = 1,
David S. Miller59abbd12009-09-10 06:28:20 -0700323};
324
David S. Miller7eebda62009-09-26 21:23:41 -0700325/* 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. Miller6e804252009-09-29 15:10:23 -0700327 * problems. Some hardware designs make one wonder if the creator
David S. Miller7eebda62009-09-26 21:23:41 -0700328 * even looked at how this stuff gets used by software.
329 */
330static 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
337static const struct perf_event_map *niagara1_event_map(int event_id)
338{
339 return &niagara1_perfmon_event_map[event_id];
340}
341
342static 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 Zijlstra89d6c0b2011-04-22 23:37:06 +0200427[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. Miller7eebda62009-09-26 21:23:41 -0700441};
442
443static 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. Miller53443032012-08-17 02:37:06 -0700447 .read_pmc = sparc_default_read_pmc,
448 .write_pmc = sparc_default_write_pmc,
David S. Miller7eebda62009-09-26 21:23:41 -0700449 .upper_shift = 0,
450 .lower_shift = 4,
451 .event_mask = 0x7,
David S. Miller7ac2ed22012-08-17 02:41:32 -0700452 .user_bit = PCR_UTRACE,
453 .priv_bit = PCR_STRACE,
David S. Miller7eebda62009-09-26 21:23:41 -0700454 .upper_nop = 0x0,
455 .lower_nop = 0x0,
David S. Millerb38e99f2012-08-17 02:31:10 -0700456 .flags = (SPARC_PMU_ALL_EXCLUDES_SAME |
457 SPARC_PMU_HAS_CONFLICTS),
David S. Miller59660492012-08-17 02:33:44 -0700458 .max_hw_events = 2,
David S. Miller3f1a2092012-08-17 02:51:21 -0700459 .num_pcrs = 1,
460 .num_pic_regs = 1,
David S. Miller7eebda62009-09-26 21:23:41 -0700461};
462
David S. Millerb73d8842009-09-10 07:22:18 -0700463static 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 Molnarcdd6c482009-09-21 12:02:48 +0200472static const struct perf_event_map *niagara2_event_map(int event_id)
David S. Millerb73d8842009-09-10 07:22:18 -0700473{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200474 return &niagara2_perfmon_event_map[event_id];
David S. Millerb73d8842009-09-10 07:22:18 -0700475}
476
David S. Millerd0b86482009-09-26 21:04:16 -0700477static 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 Zijlstra89d6c0b2011-04-22 23:37:06 +0200562[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. Millerd0b86482009-09-26 21:04:16 -0700576};
577
David S. Millerb73d8842009-09-10 07:22:18 -0700578static const struct sparc_pmu niagara2_pmu = {
579 .event_map = niagara2_event_map,
David S. Millerd0b86482009-09-26 21:04:16 -0700580 .cache_map = &niagara2_cache_map,
David S. Millerb73d8842009-09-10 07:22:18 -0700581 .max_events = ARRAY_SIZE(niagara2_perfmon_event_map),
David S. Miller53443032012-08-17 02:37:06 -0700582 .read_pmc = sparc_default_read_pmc,
583 .write_pmc = sparc_default_write_pmc,
David S. Millerb73d8842009-09-10 07:22:18 -0700584 .upper_shift = 19,
585 .lower_shift = 6,
586 .event_mask = 0xfff,
David S. Miller7ac2ed22012-08-17 02:41:32 -0700587 .user_bit = PCR_UTRACE,
588 .priv_bit = PCR_STRACE,
589 .hv_bit = PCR_N2_HTRACE,
David S. Millerde23cf32009-10-09 00:42:40 -0700590 .irq_bit = 0x30,
David S. Millerb73d8842009-09-10 07:22:18 -0700591 .upper_nop = 0x220,
592 .lower_nop = 0x220,
David S. Millerb38e99f2012-08-17 02:31:10 -0700593 .flags = (SPARC_PMU_ALL_EXCLUDES_SAME |
594 SPARC_PMU_HAS_CONFLICTS),
David S. Miller59660492012-08-17 02:33:44 -0700595 .max_hw_events = 2,
David S. Miller3f1a2092012-08-17 02:51:21 -0700596 .num_pcrs = 1,
597 .num_pic_regs = 1,
David S. Millerb73d8842009-09-10 07:22:18 -0700598};
599
David S. Miller59abbd12009-09-10 06:28:20 -0700600static const struct sparc_pmu *sparc_pmu __read_mostly;
601
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200602static u64 event_encoding(u64 event_id, int idx)
David S. Miller59abbd12009-09-10 06:28:20 -0700603{
604 if (idx == PIC_UPPER_INDEX)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200605 event_id <<= sparc_pmu->upper_shift;
David S. Miller59abbd12009-09-10 06:28:20 -0700606 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200607 event_id <<= sparc_pmu->lower_shift;
608 return event_id;
David S. Miller59abbd12009-09-10 06:28:20 -0700609}
610
611static u64 mask_for_index(int idx)
612{
613 return event_encoding(sparc_pmu->event_mask, idx);
614}
615
616static u64 nop_for_index(int idx)
617{
618 return event_encoding(idx == PIC_UPPER_INDEX ?
David S. Miller660d1372009-09-10 07:13:26 -0700619 sparc_pmu->upper_nop :
620 sparc_pmu->lower_nop, idx);
David S. Miller59abbd12009-09-10 06:28:20 -0700621}
622
David S. Millerd1751382009-09-29 21:27:06 -0700623static inline void sparc_pmu_enable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
David S. Miller59abbd12009-09-10 06:28:20 -0700624{
625 u64 val, mask = mask_for_index(idx);
626
David S. Miller3f1a2092012-08-17 02:51:21 -0700627 val = cpuc->pcr[0];
David S. Millerd1751382009-09-29 21:27:06 -0700628 val &= ~mask;
629 val |= hwc->config;
David S. Miller3f1a2092012-08-17 02:51:21 -0700630 cpuc->pcr[0] = val;
David S. Millerd1751382009-09-29 21:27:06 -0700631
David S. Miller3f1a2092012-08-17 02:51:21 -0700632 pcr_ops->write_pcr(0, cpuc->pcr[0]);
David S. Miller59abbd12009-09-10 06:28:20 -0700633}
634
David S. Millerd1751382009-09-29 21:27:06 -0700635static inline void sparc_pmu_disable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
David S. Miller59abbd12009-09-10 06:28:20 -0700636{
637 u64 mask = mask_for_index(idx);
638 u64 nop = nop_for_index(idx);
David S. Millerd1751382009-09-29 21:27:06 -0700639 u64 val;
David S. Miller59abbd12009-09-10 06:28:20 -0700640
David S. Miller3f1a2092012-08-17 02:51:21 -0700641 val = cpuc->pcr[0];
David S. Millerd1751382009-09-29 21:27:06 -0700642 val &= ~mask;
643 val |= nop;
David S. Miller3f1a2092012-08-17 02:51:21 -0700644 cpuc->pcr[0] = val;
David S. Millerd1751382009-09-29 21:27:06 -0700645
David S. Miller3f1a2092012-08-17 02:51:21 -0700646 pcr_ops->write_pcr(0, cpuc->pcr[0]);
David S. Miller59abbd12009-09-10 06:28:20 -0700647}
648
David S. Millere7bef6b2010-01-20 02:59:47 -0800649static u64 sparc_perf_event_update(struct perf_event *event,
650 struct hw_perf_event *hwc, int idx)
651{
652 int shift = 64 - 32;
653 u64 prev_raw_count, new_raw_count;
654 s64 delta;
655
656again:
Peter Zijlstrae7850592010-05-21 14:43:08 +0200657 prev_raw_count = local64_read(&hwc->prev_count);
David S. Miller53443032012-08-17 02:37:06 -0700658 new_raw_count = sparc_pmu->read_pmc(idx);
David S. Millere7bef6b2010-01-20 02:59:47 -0800659
Peter Zijlstrae7850592010-05-21 14:43:08 +0200660 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
David S. Millere7bef6b2010-01-20 02:59:47 -0800661 new_raw_count) != prev_raw_count)
662 goto again;
663
664 delta = (new_raw_count << shift) - (prev_raw_count << shift);
665 delta >>= shift;
666
Peter Zijlstrae7850592010-05-21 14:43:08 +0200667 local64_add(delta, &event->count);
668 local64_sub(delta, &hwc->period_left);
David S. Millere7bef6b2010-01-20 02:59:47 -0800669
670 return new_raw_count;
671}
672
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200673static int sparc_perf_event_set_period(struct perf_event *event,
David S. Millerd29862f2009-09-28 17:37:12 -0700674 struct hw_perf_event *hwc, int idx)
David S. Miller59abbd12009-09-10 06:28:20 -0700675{
Peter Zijlstrae7850592010-05-21 14:43:08 +0200676 s64 left = local64_read(&hwc->period_left);
David S. Miller59abbd12009-09-10 06:28:20 -0700677 s64 period = hwc->sample_period;
678 int ret = 0;
679
680 if (unlikely(left <= -period)) {
681 left = period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200682 local64_set(&hwc->period_left, left);
David S. Miller59abbd12009-09-10 06:28:20 -0700683 hwc->last_period = period;
684 ret = 1;
685 }
686
687 if (unlikely(left <= 0)) {
688 left += period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200689 local64_set(&hwc->period_left, left);
David S. Miller59abbd12009-09-10 06:28:20 -0700690 hwc->last_period = period;
691 ret = 1;
692 }
693 if (left > MAX_PERIOD)
694 left = MAX_PERIOD;
695
Peter Zijlstrae7850592010-05-21 14:43:08 +0200696 local64_set(&hwc->prev_count, (u64)-left);
David S. Miller59abbd12009-09-10 06:28:20 -0700697
David S. Miller53443032012-08-17 02:37:06 -0700698 sparc_pmu->write_pmc(idx, (u64)(-left) & 0xffffffff);
David S. Miller59abbd12009-09-10 06:28:20 -0700699
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200700 perf_event_update_userpage(event);
David S. Miller59abbd12009-09-10 06:28:20 -0700701
702 return ret;
703}
704
David S. Millere7bef6b2010-01-20 02:59:47 -0800705/* If performance event entries have been added, move existing
706 * events around (if necessary) and then assign new entries to
707 * counters.
708 */
709static u64 maybe_change_configuration(struct cpu_hw_events *cpuc, u64 pcr)
David S. Miller59abbd12009-09-10 06:28:20 -0700710{
David S. Millere7bef6b2010-01-20 02:59:47 -0800711 int i;
David S. Miller59abbd12009-09-10 06:28:20 -0700712
David S. Millere7bef6b2010-01-20 02:59:47 -0800713 if (!cpuc->n_added)
714 goto out;
David S. Miller59abbd12009-09-10 06:28:20 -0700715
David S. Millere7bef6b2010-01-20 02:59:47 -0800716 /* Read in the counters which are moving. */
717 for (i = 0; i < cpuc->n_events; i++) {
718 struct perf_event *cp = cpuc->event[i];
David S. Miller59abbd12009-09-10 06:28:20 -0700719
David S. Millere7bef6b2010-01-20 02:59:47 -0800720 if (cpuc->current_idx[i] != PIC_NO_INDEX &&
721 cpuc->current_idx[i] != cp->hw.idx) {
722 sparc_perf_event_update(cp, &cp->hw,
723 cpuc->current_idx[i]);
724 cpuc->current_idx[i] = PIC_NO_INDEX;
725 }
726 }
David S. Miller59abbd12009-09-10 06:28:20 -0700727
David S. Millere7bef6b2010-01-20 02:59:47 -0800728 /* Assign to counters all unassigned events. */
729 for (i = 0; i < cpuc->n_events; i++) {
730 struct perf_event *cp = cpuc->event[i];
731 struct hw_perf_event *hwc = &cp->hw;
732 int idx = hwc->idx;
733 u64 enc;
734
735 if (cpuc->current_idx[i] != PIC_NO_INDEX)
736 continue;
737
738 sparc_perf_event_set_period(cp, hwc, idx);
739 cpuc->current_idx[i] = idx;
740
741 enc = perf_event_get_enc(cpuc->events[i]);
David S. Millerb7d45c32010-06-23 11:39:02 -0700742 pcr &= ~mask_for_index(idx);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200743 if (hwc->state & PERF_HES_STOPPED)
744 pcr |= nop_for_index(idx);
745 else
746 pcr |= event_encoding(enc, idx);
David S. Millere7bef6b2010-01-20 02:59:47 -0800747 }
748out:
749 return pcr;
David S. Miller59abbd12009-09-10 06:28:20 -0700750}
751
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200752static void sparc_pmu_enable(struct pmu *pmu)
David S. Miller59abbd12009-09-10 06:28:20 -0700753{
David S. Millere7bef6b2010-01-20 02:59:47 -0800754 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
755 u64 pcr;
David S. Miller59abbd12009-09-10 06:28:20 -0700756
David S. Millere7bef6b2010-01-20 02:59:47 -0800757 if (cpuc->enabled)
758 return;
David S. Miller59abbd12009-09-10 06:28:20 -0700759
David S. Millere7bef6b2010-01-20 02:59:47 -0800760 cpuc->enabled = 1;
761 barrier();
David S. Miller59abbd12009-09-10 06:28:20 -0700762
David S. Miller3f1a2092012-08-17 02:51:21 -0700763 pcr = cpuc->pcr[0];
David S. Millere7bef6b2010-01-20 02:59:47 -0800764 if (!cpuc->n_events) {
765 pcr = 0;
766 } else {
767 pcr = maybe_change_configuration(cpuc, pcr);
David S. Miller59abbd12009-09-10 06:28:20 -0700768
David S. Millere7bef6b2010-01-20 02:59:47 -0800769 /* We require that all of the events have the same
770 * configuration, so just fetch the settings from the
771 * first entry.
772 */
David S. Miller3f1a2092012-08-17 02:51:21 -0700773 cpuc->pcr[0] = pcr | cpuc->event[0]->hw.config_base;
David S. Millere7bef6b2010-01-20 02:59:47 -0800774 }
David S. Miller59abbd12009-09-10 06:28:20 -0700775
David S. Miller3f1a2092012-08-17 02:51:21 -0700776 pcr_ops->write_pcr(0, cpuc->pcr[0]);
David S. Millere7bef6b2010-01-20 02:59:47 -0800777}
778
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200779static void sparc_pmu_disable(struct pmu *pmu)
David S. Millere7bef6b2010-01-20 02:59:47 -0800780{
781 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
David S. Miller3f1a2092012-08-17 02:51:21 -0700782 int i;
David S. Millere7bef6b2010-01-20 02:59:47 -0800783
784 if (!cpuc->enabled)
785 return;
786
787 cpuc->enabled = 0;
788 cpuc->n_added = 0;
789
David S. Miller3f1a2092012-08-17 02:51:21 -0700790 for (i = 0; i < sparc_pmu->num_pcrs; i++) {
791 u64 val = cpuc->pcr[i];
David S. Millere7bef6b2010-01-20 02:59:47 -0800792
David S. Miller3f1a2092012-08-17 02:51:21 -0700793 val &= ~(sparc_pmu->user_bit | sparc_pmu->priv_bit |
794 sparc_pmu->hv_bit | sparc_pmu->irq_bit);
795 cpuc->pcr[i] = val;
796 pcr_ops->write_pcr(i, cpuc->pcr[i]);
797 }
David S. Miller59abbd12009-09-10 06:28:20 -0700798}
799
David S. Millere7bef6b2010-01-20 02:59:47 -0800800static int active_event_index(struct cpu_hw_events *cpuc,
801 struct perf_event *event)
802{
803 int i;
804
805 for (i = 0; i < cpuc->n_events; i++) {
806 if (cpuc->event[i] == event)
807 break;
808 }
809 BUG_ON(i == cpuc->n_events);
810 return cpuc->current_idx[i];
David S. Miller59abbd12009-09-10 06:28:20 -0700811}
812
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200813static void sparc_pmu_start(struct perf_event *event, int flags)
814{
815 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
816 int idx = active_event_index(cpuc, event);
817
818 if (flags & PERF_EF_RELOAD) {
819 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
820 sparc_perf_event_set_period(event, &event->hw, idx);
821 }
822
823 event->hw.state = 0;
824
825 sparc_pmu_enable_event(cpuc, &event->hw, idx);
826}
827
828static void sparc_pmu_stop(struct perf_event *event, int flags)
829{
830 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
831 int idx = active_event_index(cpuc, event);
832
833 if (!(event->hw.state & PERF_HES_STOPPED)) {
834 sparc_pmu_disable_event(cpuc, &event->hw, idx);
835 event->hw.state |= PERF_HES_STOPPED;
836 }
837
838 if (!(event->hw.state & PERF_HES_UPTODATE) && (flags & PERF_EF_UPDATE)) {
839 sparc_perf_event_update(event, &event->hw, idx);
840 event->hw.state |= PERF_HES_UPTODATE;
841 }
842}
843
844static void sparc_pmu_del(struct perf_event *event, int _flags)
845{
846 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
847 unsigned long flags;
848 int i;
849
850 local_irq_save(flags);
851 perf_pmu_disable(event->pmu);
852
853 for (i = 0; i < cpuc->n_events; i++) {
854 if (event == cpuc->event[i]) {
855 /* Absorb the final count and turn off the
856 * event.
857 */
858 sparc_pmu_stop(event, PERF_EF_UPDATE);
859
860 /* Shift remaining entries down into
861 * the existing slot.
862 */
863 while (++i < cpuc->n_events) {
864 cpuc->event[i - 1] = cpuc->event[i];
865 cpuc->events[i - 1] = cpuc->events[i];
866 cpuc->current_idx[i - 1] =
867 cpuc->current_idx[i];
868 }
869
870 perf_event_update_userpage(event);
871
872 cpuc->n_events--;
873 break;
874 }
875 }
876
877 perf_pmu_enable(event->pmu);
878 local_irq_restore(flags);
879}
880
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200881static void sparc_pmu_read(struct perf_event *event)
David S. Miller59abbd12009-09-10 06:28:20 -0700882{
David S. Millere7bef6b2010-01-20 02:59:47 -0800883 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
884 int idx = active_event_index(cpuc, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200885 struct hw_perf_event *hwc = &event->hw;
David S. Millerd1751382009-09-29 21:27:06 -0700886
David S. Millere7bef6b2010-01-20 02:59:47 -0800887 sparc_perf_event_update(event, hwc, idx);
David S. Miller59abbd12009-09-10 06:28:20 -0700888}
889
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200890static atomic_t active_events = ATOMIC_INIT(0);
David S. Miller59abbd12009-09-10 06:28:20 -0700891static DEFINE_MUTEX(pmc_grab_mutex);
892
David S. Millerd1751382009-09-29 21:27:06 -0700893static void perf_stop_nmi_watchdog(void *unused)
894{
895 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
David S. Miller3f1a2092012-08-17 02:51:21 -0700896 int i;
David S. Millerd1751382009-09-29 21:27:06 -0700897
898 stop_nmi_watchdog(NULL);
David S. Miller3f1a2092012-08-17 02:51:21 -0700899 for (i = 0; i < sparc_pmu->num_pcrs; i++)
900 cpuc->pcr[i] = pcr_ops->read_pcr(i);
David S. Millerd1751382009-09-29 21:27:06 -0700901}
902
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200903void perf_event_grab_pmc(void)
David S. Miller59abbd12009-09-10 06:28:20 -0700904{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200905 if (atomic_inc_not_zero(&active_events))
David S. Miller59abbd12009-09-10 06:28:20 -0700906 return;
907
908 mutex_lock(&pmc_grab_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200909 if (atomic_read(&active_events) == 0) {
David S. Miller59abbd12009-09-10 06:28:20 -0700910 if (atomic_read(&nmi_active) > 0) {
David S. Millerd1751382009-09-29 21:27:06 -0700911 on_each_cpu(perf_stop_nmi_watchdog, NULL, 1);
David S. Miller59abbd12009-09-10 06:28:20 -0700912 BUG_ON(atomic_read(&nmi_active) != 0);
913 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200914 atomic_inc(&active_events);
David S. Miller59abbd12009-09-10 06:28:20 -0700915 }
916 mutex_unlock(&pmc_grab_mutex);
917}
918
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200919void perf_event_release_pmc(void)
David S. Miller59abbd12009-09-10 06:28:20 -0700920{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200921 if (atomic_dec_and_mutex_lock(&active_events, &pmc_grab_mutex)) {
David S. Miller59abbd12009-09-10 06:28:20 -0700922 if (atomic_read(&nmi_active) == 0)
923 on_each_cpu(start_nmi_watchdog, NULL, 1);
924 mutex_unlock(&pmc_grab_mutex);
925 }
926}
927
David S. Miller2ce4da22009-09-26 20:42:10 -0700928static const struct perf_event_map *sparc_map_cache_event(u64 config)
929{
930 unsigned int cache_type, cache_op, cache_result;
931 const struct perf_event_map *pmap;
932
933 if (!sparc_pmu->cache_map)
934 return ERR_PTR(-ENOENT);
935
936 cache_type = (config >> 0) & 0xff;
937 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
938 return ERR_PTR(-EINVAL);
939
940 cache_op = (config >> 8) & 0xff;
941 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
942 return ERR_PTR(-EINVAL);
943
944 cache_result = (config >> 16) & 0xff;
945 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
946 return ERR_PTR(-EINVAL);
947
948 pmap = &((*sparc_pmu->cache_map)[cache_type][cache_op][cache_result]);
949
950 if (pmap->encoding == CACHE_OP_UNSUPPORTED)
951 return ERR_PTR(-ENOENT);
952
953 if (pmap->encoding == CACHE_OP_NONSENSE)
954 return ERR_PTR(-EINVAL);
955
956 return pmap;
957}
958
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200959static void hw_perf_event_destroy(struct perf_event *event)
David S. Miller59abbd12009-09-10 06:28:20 -0700960{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200961 perf_event_release_pmc();
David S. Miller59abbd12009-09-10 06:28:20 -0700962}
963
David S. Millera72a8a52009-09-28 17:35:20 -0700964/* Make sure all events can be scheduled into the hardware at
965 * the same time. This is simplified by the fact that we only
966 * need to support 2 simultaneous HW events.
David S. Millere7bef6b2010-01-20 02:59:47 -0800967 *
968 * As a side effect, the evts[]->hw.idx values will be assigned
969 * on success. These are pending indexes. When the events are
970 * actually programmed into the chip, these values will propagate
971 * to the per-cpu cpuc->current_idx[] slots, see the code in
972 * maybe_change_configuration() for details.
David S. Millera72a8a52009-09-28 17:35:20 -0700973 */
David S. Millere7bef6b2010-01-20 02:59:47 -0800974static int sparc_check_constraints(struct perf_event **evts,
975 unsigned long *events, int n_ev)
David S. Millera72a8a52009-09-28 17:35:20 -0700976{
David S. Millere7bef6b2010-01-20 02:59:47 -0800977 u8 msk0 = 0, msk1 = 0;
978 int idx0 = 0;
David S. Millera72a8a52009-09-28 17:35:20 -0700979
David S. Millere7bef6b2010-01-20 02:59:47 -0800980 /* This case is possible when we are invoked from
981 * hw_perf_group_sched_in().
982 */
983 if (!n_ev)
984 return 0;
David S. Millera72a8a52009-09-28 17:35:20 -0700985
David S. Miller59660492012-08-17 02:33:44 -0700986 if (n_ev > sparc_pmu->max_hw_events)
David S. Millere7bef6b2010-01-20 02:59:47 -0800987 return -1;
David S. Millera72a8a52009-09-28 17:35:20 -0700988
David S. Millerb38e99f2012-08-17 02:31:10 -0700989 if (!(sparc_pmu->flags & SPARC_PMU_HAS_CONFLICTS)) {
990 int i;
991
992 for (i = 0; i < n_ev; i++)
993 evts[i]->hw.idx = i;
994 return 0;
995 }
996
David S. Millere7bef6b2010-01-20 02:59:47 -0800997 msk0 = perf_event_get_msk(events[0]);
998 if (n_ev == 1) {
999 if (msk0 & PIC_LOWER)
1000 idx0 = 1;
1001 goto success;
1002 }
1003 BUG_ON(n_ev != 2);
1004 msk1 = perf_event_get_msk(events[1]);
David S. Millera72a8a52009-09-28 17:35:20 -07001005
David S. Millere7bef6b2010-01-20 02:59:47 -08001006 /* If both events can go on any counter, OK. */
1007 if (msk0 == (PIC_UPPER | PIC_LOWER) &&
1008 msk1 == (PIC_UPPER | PIC_LOWER))
1009 goto success;
David S. Millera72a8a52009-09-28 17:35:20 -07001010
David S. Millere7bef6b2010-01-20 02:59:47 -08001011 /* If one event is limited to a specific counter,
1012 * and the other can go on both, OK.
1013 */
1014 if ((msk0 == PIC_UPPER || msk0 == PIC_LOWER) &&
1015 msk1 == (PIC_UPPER | PIC_LOWER)) {
1016 if (msk0 & PIC_LOWER)
1017 idx0 = 1;
1018 goto success;
David S. Millera72a8a52009-09-28 17:35:20 -07001019 }
1020
David S. Millere7bef6b2010-01-20 02:59:47 -08001021 if ((msk1 == PIC_UPPER || msk1 == PIC_LOWER) &&
1022 msk0 == (PIC_UPPER | PIC_LOWER)) {
1023 if (msk1 & PIC_UPPER)
1024 idx0 = 1;
1025 goto success;
1026 }
1027
1028 /* If the events are fixed to different counters, OK. */
1029 if ((msk0 == PIC_UPPER && msk1 == PIC_LOWER) ||
1030 (msk0 == PIC_LOWER && msk1 == PIC_UPPER)) {
1031 if (msk0 & PIC_LOWER)
1032 idx0 = 1;
1033 goto success;
1034 }
1035
1036 /* Otherwise, there is a conflict. */
David S. Millera72a8a52009-09-28 17:35:20 -07001037 return -1;
David S. Millere7bef6b2010-01-20 02:59:47 -08001038
1039success:
1040 evts[0]->hw.idx = idx0;
1041 if (n_ev == 2)
1042 evts[1]->hw.idx = idx0 ^ 1;
1043 return 0;
David S. Millera72a8a52009-09-28 17:35:20 -07001044}
1045
David S. Miller01552f72009-09-27 20:43:07 -07001046static int check_excludes(struct perf_event **evts, int n_prev, int n_new)
1047{
1048 int eu = 0, ek = 0, eh = 0;
1049 struct perf_event *event;
1050 int i, n, first;
1051
David S. Millerb38e99f2012-08-17 02:31:10 -07001052 if (!(sparc_pmu->flags & SPARC_PMU_ALL_EXCLUDES_SAME))
1053 return 0;
1054
David S. Miller01552f72009-09-27 20:43:07 -07001055 n = n_prev + n_new;
1056 if (n <= 1)
1057 return 0;
1058
1059 first = 1;
1060 for (i = 0; i < n; i++) {
1061 event = evts[i];
1062 if (first) {
1063 eu = event->attr.exclude_user;
1064 ek = event->attr.exclude_kernel;
1065 eh = event->attr.exclude_hv;
1066 first = 0;
1067 } else if (event->attr.exclude_user != eu ||
1068 event->attr.exclude_kernel != ek ||
1069 event->attr.exclude_hv != eh) {
1070 return -EAGAIN;
1071 }
1072 }
1073
1074 return 0;
1075}
1076
1077static int collect_events(struct perf_event *group, int max_count,
David S. Millere7bef6b2010-01-20 02:59:47 -08001078 struct perf_event *evts[], unsigned long *events,
1079 int *current_idx)
David S. Miller01552f72009-09-27 20:43:07 -07001080{
1081 struct perf_event *event;
1082 int n = 0;
1083
1084 if (!is_software_event(group)) {
1085 if (n >= max_count)
1086 return -1;
1087 evts[n] = group;
David S. Millere7bef6b2010-01-20 02:59:47 -08001088 events[n] = group->hw.event_base;
1089 current_idx[n++] = PIC_NO_INDEX;
David S. Miller01552f72009-09-27 20:43:07 -07001090 }
1091 list_for_each_entry(event, &group->sibling_list, group_entry) {
1092 if (!is_software_event(event) &&
1093 event->state != PERF_EVENT_STATE_OFF) {
1094 if (n >= max_count)
1095 return -1;
1096 evts[n] = event;
David S. Millere7bef6b2010-01-20 02:59:47 -08001097 events[n] = event->hw.event_base;
1098 current_idx[n++] = PIC_NO_INDEX;
David S. Miller01552f72009-09-27 20:43:07 -07001099 }
1100 }
1101 return n;
1102}
1103
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001104static int sparc_pmu_add(struct perf_event *event, int ef_flags)
David S. Millere7bef6b2010-01-20 02:59:47 -08001105{
1106 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1107 int n0, ret = -EAGAIN;
1108 unsigned long flags;
1109
1110 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001111 perf_pmu_disable(event->pmu);
David S. Millere7bef6b2010-01-20 02:59:47 -08001112
1113 n0 = cpuc->n_events;
David S. Miller59660492012-08-17 02:33:44 -07001114 if (n0 >= sparc_pmu->max_hw_events)
David S. Millere7bef6b2010-01-20 02:59:47 -08001115 goto out;
1116
1117 cpuc->event[n0] = event;
1118 cpuc->events[n0] = event->hw.event_base;
1119 cpuc->current_idx[n0] = PIC_NO_INDEX;
1120
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001121 event->hw.state = PERF_HES_UPTODATE;
1122 if (!(ef_flags & PERF_EF_START))
1123 event->hw.state |= PERF_HES_STOPPED;
1124
Lin Minga13c3af2010-04-23 13:56:33 +08001125 /*
1126 * If group events scheduling transaction was started,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001127 * skip the schedulability test here, it will be performed
Lin Minga13c3af2010-04-23 13:56:33 +08001128 * at commit time(->commit_txn) as a whole
1129 */
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001130 if (cpuc->group_flag & PERF_EVENT_TXN)
Lin Minga13c3af2010-04-23 13:56:33 +08001131 goto nocheck;
1132
David S. Millere7bef6b2010-01-20 02:59:47 -08001133 if (check_excludes(cpuc->event, n0, 1))
1134 goto out;
1135 if (sparc_check_constraints(cpuc->event, cpuc->events, n0 + 1))
1136 goto out;
1137
Lin Minga13c3af2010-04-23 13:56:33 +08001138nocheck:
David S. Millere7bef6b2010-01-20 02:59:47 -08001139 cpuc->n_events++;
1140 cpuc->n_added++;
1141
1142 ret = 0;
1143out:
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001144 perf_pmu_enable(event->pmu);
David S. Millere7bef6b2010-01-20 02:59:47 -08001145 local_irq_restore(flags);
1146 return ret;
1147}
1148
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001149static int sparc_pmu_event_init(struct perf_event *event)
David S. Miller59abbd12009-09-10 06:28:20 -07001150{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001151 struct perf_event_attr *attr = &event->attr;
David S. Miller01552f72009-09-27 20:43:07 -07001152 struct perf_event *evts[MAX_HWEVENTS];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001153 struct hw_perf_event *hwc = &event->hw;
David S. Millera72a8a52009-09-28 17:35:20 -07001154 unsigned long events[MAX_HWEVENTS];
David S. Millere7bef6b2010-01-20 02:59:47 -08001155 int current_idx_dmy[MAX_HWEVENTS];
David S. Miller59abbd12009-09-10 06:28:20 -07001156 const struct perf_event_map *pmap;
David S. Miller01552f72009-09-27 20:43:07 -07001157 int n;
David S. Miller59abbd12009-09-10 06:28:20 -07001158
1159 if (atomic_read(&nmi_active) < 0)
1160 return -ENODEV;
1161
Stephane Eranian2481c5f2012-02-09 23:20:59 +01001162 /* does not support taken branch sampling */
1163 if (has_branch_stack(event))
1164 return -EOPNOTSUPP;
1165
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001166 switch (attr->type) {
1167 case PERF_TYPE_HARDWARE:
David S. Miller2ce4da22009-09-26 20:42:10 -07001168 if (attr->config >= sparc_pmu->max_events)
1169 return -EINVAL;
1170 pmap = sparc_pmu->event_map(attr->config);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001171 break;
1172
1173 case PERF_TYPE_HW_CACHE:
David S. Miller2ce4da22009-09-26 20:42:10 -07001174 pmap = sparc_map_cache_event(attr->config);
1175 if (IS_ERR(pmap))
1176 return PTR_ERR(pmap);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001177 break;
1178
1179 case PERF_TYPE_RAW:
Ingo Molnard0303d72010-09-23 08:02:09 +02001180 pmap = NULL;
1181 break;
David S. Miller59abbd12009-09-10 06:28:20 -07001182
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001183 default:
1184 return -ENOENT;
1185
1186 }
1187
David S. Millerb343ae52010-09-12 17:20:24 -07001188 if (pmap) {
1189 hwc->event_base = perf_event_encode(pmap);
1190 } else {
Ingo Molnard0303d72010-09-23 08:02:09 +02001191 /*
1192 * User gives us "(encoding << 16) | pic_mask" for
David S. Millerb343ae52010-09-12 17:20:24 -07001193 * PERF_TYPE_RAW events.
1194 */
1195 hwc->event_base = attr->config;
1196 }
1197
David S. Millere7bef6b2010-01-20 02:59:47 -08001198 /* We save the enable bits in the config_base. */
David S. Miller496c07e2009-09-10 07:10:59 -07001199 hwc->config_base = sparc_pmu->irq_bit;
David S. Miller59abbd12009-09-10 06:28:20 -07001200 if (!attr->exclude_user)
David S. Miller7ac2ed22012-08-17 02:41:32 -07001201 hwc->config_base |= sparc_pmu->user_bit;
David S. Miller59abbd12009-09-10 06:28:20 -07001202 if (!attr->exclude_kernel)
David S. Miller7ac2ed22012-08-17 02:41:32 -07001203 hwc->config_base |= sparc_pmu->priv_bit;
David S. Miller91b92862009-09-10 07:09:06 -07001204 if (!attr->exclude_hv)
1205 hwc->config_base |= sparc_pmu->hv_bit;
David S. Miller59abbd12009-09-10 06:28:20 -07001206
David S. Miller01552f72009-09-27 20:43:07 -07001207 n = 0;
1208 if (event->group_leader != event) {
1209 n = collect_events(event->group_leader,
David S. Miller59660492012-08-17 02:33:44 -07001210 sparc_pmu->max_hw_events - 1,
David S. Millere7bef6b2010-01-20 02:59:47 -08001211 evts, events, current_idx_dmy);
David S. Miller01552f72009-09-27 20:43:07 -07001212 if (n < 0)
1213 return -EINVAL;
1214 }
David S. Millera72a8a52009-09-28 17:35:20 -07001215 events[n] = hwc->event_base;
David S. Miller01552f72009-09-27 20:43:07 -07001216 evts[n] = event;
1217
1218 if (check_excludes(evts, n, 1))
1219 return -EINVAL;
1220
David S. Millere7bef6b2010-01-20 02:59:47 -08001221 if (sparc_check_constraints(evts, events, n + 1))
David S. Millera72a8a52009-09-28 17:35:20 -07001222 return -EINVAL;
1223
David S. Millere7bef6b2010-01-20 02:59:47 -08001224 hwc->idx = PIC_NO_INDEX;
1225
David S. Miller01552f72009-09-27 20:43:07 -07001226 /* Try to do all error checking before this point, as unwinding
1227 * state after grabbing the PMC is difficult.
1228 */
1229 perf_event_grab_pmc();
1230 event->destroy = hw_perf_event_destroy;
1231
David S. Miller59abbd12009-09-10 06:28:20 -07001232 if (!hwc->sample_period) {
1233 hwc->sample_period = MAX_PERIOD;
1234 hwc->last_period = hwc->sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001235 local64_set(&hwc->period_left, hwc->sample_period);
David S. Miller59abbd12009-09-10 06:28:20 -07001236 }
1237
David S. Miller59abbd12009-09-10 06:28:20 -07001238 return 0;
1239}
1240
Lin Minga13c3af2010-04-23 13:56:33 +08001241/*
1242 * Start group events scheduling transaction
1243 * Set the flag to make pmu::enable() not perform the
1244 * schedulability test, it will be performed at commit time
1245 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001246static void sparc_pmu_start_txn(struct pmu *pmu)
Lin Minga13c3af2010-04-23 13:56:33 +08001247{
1248 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1249
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001250 perf_pmu_disable(pmu);
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001251 cpuhw->group_flag |= PERF_EVENT_TXN;
Lin Minga13c3af2010-04-23 13:56:33 +08001252}
1253
1254/*
1255 * Stop group events scheduling transaction
1256 * Clear the flag and pmu::enable() will perform the
1257 * schedulability test.
1258 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001259static void sparc_pmu_cancel_txn(struct pmu *pmu)
Lin Minga13c3af2010-04-23 13:56:33 +08001260{
1261 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1262
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001263 cpuhw->group_flag &= ~PERF_EVENT_TXN;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001264 perf_pmu_enable(pmu);
Lin Minga13c3af2010-04-23 13:56:33 +08001265}
1266
1267/*
1268 * Commit group events scheduling transaction
1269 * Perform the group schedulability test as a whole
1270 * Return 0 if success
1271 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001272static int sparc_pmu_commit_txn(struct pmu *pmu)
Lin Minga13c3af2010-04-23 13:56:33 +08001273{
1274 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1275 int n;
1276
1277 if (!sparc_pmu)
1278 return -EINVAL;
1279
1280 cpuc = &__get_cpu_var(cpu_hw_events);
1281 n = cpuc->n_events;
1282 if (check_excludes(cpuc->event, 0, n))
1283 return -EINVAL;
1284 if (sparc_check_constraints(cpuc->event, cpuc->events, n))
1285 return -EAGAIN;
1286
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001287 cpuc->group_flag &= ~PERF_EVENT_TXN;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001288 perf_pmu_enable(pmu);
Lin Minga13c3af2010-04-23 13:56:33 +08001289 return 0;
1290}
1291
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001292static struct pmu pmu = {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001293 .pmu_enable = sparc_pmu_enable,
1294 .pmu_disable = sparc_pmu_disable,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001295 .event_init = sparc_pmu_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001296 .add = sparc_pmu_add,
1297 .del = sparc_pmu_del,
1298 .start = sparc_pmu_start,
1299 .stop = sparc_pmu_stop,
David S. Miller59abbd12009-09-10 06:28:20 -07001300 .read = sparc_pmu_read,
Lin Minga13c3af2010-04-23 13:56:33 +08001301 .start_txn = sparc_pmu_start_txn,
1302 .cancel_txn = sparc_pmu_cancel_txn,
1303 .commit_txn = sparc_pmu_commit_txn,
David S. Miller59abbd12009-09-10 06:28:20 -07001304};
1305
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001306void perf_event_print_debug(void)
David S. Miller59abbd12009-09-10 06:28:20 -07001307{
1308 unsigned long flags;
David S. Miller3f1a2092012-08-17 02:51:21 -07001309 int cpu, i;
David S. Miller59abbd12009-09-10 06:28:20 -07001310
1311 if (!sparc_pmu)
1312 return;
1313
1314 local_irq_save(flags);
1315
1316 cpu = smp_processor_id();
1317
David S. Miller59abbd12009-09-10 06:28:20 -07001318 pr_info("\n");
David S. Miller3f1a2092012-08-17 02:51:21 -07001319 for (i = 0; i < sparc_pmu->num_pcrs; i++)
1320 pr_info("CPU#%d: PCR%d[%016llx]\n",
1321 cpu, i, pcr_ops->read_pcr(i));
1322 for (i = 0; i < sparc_pmu->num_pic_regs; i++)
1323 pr_info("CPU#%d: PIC%d[%016llx]\n",
1324 cpu, i, pcr_ops->read_pic(i));
David S. Miller59abbd12009-09-10 06:28:20 -07001325
1326 local_irq_restore(flags);
1327}
1328
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001329static int __kprobes perf_event_nmi_handler(struct notifier_block *self,
David S. Millerd29862f2009-09-28 17:37:12 -07001330 unsigned long cmd, void *__args)
David S. Miller59abbd12009-09-10 06:28:20 -07001331{
1332 struct die_args *args = __args;
1333 struct perf_sample_data data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001334 struct cpu_hw_events *cpuc;
David S. Miller59abbd12009-09-10 06:28:20 -07001335 struct pt_regs *regs;
David S. Millere7bef6b2010-01-20 02:59:47 -08001336 int i;
David S. Miller59abbd12009-09-10 06:28:20 -07001337
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001338 if (!atomic_read(&active_events))
David S. Miller59abbd12009-09-10 06:28:20 -07001339 return NOTIFY_DONE;
1340
1341 switch (cmd) {
1342 case DIE_NMI:
1343 break;
1344
1345 default:
1346 return NOTIFY_DONE;
1347 }
1348
1349 regs = args->regs;
1350
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001351 cpuc = &__get_cpu_var(cpu_hw_events);
David S. Millere04ed382010-01-04 23:16:03 -08001352
1353 /* If the PMU has the TOE IRQ enable bits, we need to do a
1354 * dummy write to the %pcr to clear the overflow bits and thus
1355 * the interrupt.
1356 *
1357 * Do this before we peek at the counters to determine
1358 * overflow so we don't lose any events.
1359 */
David S. Miller3f1a2092012-08-17 02:51:21 -07001360 if (sparc_pmu->irq_bit &&
1361 sparc_pmu->num_pcrs == 1)
1362 pcr_ops->write_pcr(0, cpuc->pcr[0]);
David S. Millere04ed382010-01-04 23:16:03 -08001363
David S. Millere7bef6b2010-01-20 02:59:47 -08001364 for (i = 0; i < cpuc->n_events; i++) {
1365 struct perf_event *event = cpuc->event[i];
1366 int idx = cpuc->current_idx[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001367 struct hw_perf_event *hwc;
David S. Miller59abbd12009-09-10 06:28:20 -07001368 u64 val;
1369
David S. Miller3f1a2092012-08-17 02:51:21 -07001370 if (sparc_pmu->irq_bit &&
1371 sparc_pmu->num_pcrs > 1)
1372 pcr_ops->write_pcr(idx, cpuc->pcr[idx]);
1373
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001374 hwc = &event->hw;
1375 val = sparc_perf_event_update(event, hwc, idx);
David S. Miller59abbd12009-09-10 06:28:20 -07001376 if (val & (1ULL << 31))
1377 continue;
1378
Robert Richterfd0d0002012-04-02 20:19:08 +02001379 perf_sample_data_init(&data, 0, hwc->last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001380 if (!sparc_perf_event_set_period(event, hwc, idx))
David S. Miller59abbd12009-09-10 06:28:20 -07001381 continue;
1382
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001383 if (perf_event_overflow(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001384 sparc_pmu_stop(event, 0);
David S. Miller59abbd12009-09-10 06:28:20 -07001385 }
1386
1387 return NOTIFY_STOP;
1388}
1389
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001390static __read_mostly struct notifier_block perf_event_nmi_notifier = {
1391 .notifier_call = perf_event_nmi_handler,
David S. Miller59abbd12009-09-10 06:28:20 -07001392};
1393
1394static bool __init supported_pmu(void)
1395{
David S. Miller28e8f9b2009-09-26 20:54:22 -07001396 if (!strcmp(sparc_pmu_type, "ultra3") ||
1397 !strcmp(sparc_pmu_type, "ultra3+") ||
1398 !strcmp(sparc_pmu_type, "ultra3i") ||
1399 !strcmp(sparc_pmu_type, "ultra4+")) {
1400 sparc_pmu = &ultra3_pmu;
David S. Miller59abbd12009-09-10 06:28:20 -07001401 return true;
1402 }
David S. Miller7eebda62009-09-26 21:23:41 -07001403 if (!strcmp(sparc_pmu_type, "niagara")) {
1404 sparc_pmu = &niagara1_pmu;
1405 return true;
1406 }
David S. Miller4ba991d2011-07-27 21:06:16 -07001407 if (!strcmp(sparc_pmu_type, "niagara2") ||
1408 !strcmp(sparc_pmu_type, "niagara3")) {
David S. Millerb73d8842009-09-10 07:22:18 -07001409 sparc_pmu = &niagara2_pmu;
1410 return true;
1411 }
David S. Miller59abbd12009-09-10 06:28:20 -07001412 return false;
1413}
1414
Peter Zijlstra004417a2010-11-25 18:38:29 +01001415int __init init_hw_perf_events(void)
David S. Miller59abbd12009-09-10 06:28:20 -07001416{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001417 pr_info("Performance events: ");
David S. Miller59abbd12009-09-10 06:28:20 -07001418
1419 if (!supported_pmu()) {
1420 pr_cont("No support for PMU type '%s'\n", sparc_pmu_type);
Peter Zijlstra004417a2010-11-25 18:38:29 +01001421 return 0;
David S. Miller59abbd12009-09-10 06:28:20 -07001422 }
1423
1424 pr_cont("Supported PMU type is '%s'\n", sparc_pmu_type);
1425
Peter Zijlstra2e80a822010-11-17 23:17:36 +01001426 perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001427 register_die_notifier(&perf_event_nmi_notifier);
Peter Zijlstra004417a2010-11-25 18:38:29 +01001428
1429 return 0;
David S. Miller59abbd12009-09-10 06:28:20 -07001430}
Ingo Molnarefc70d22010-12-10 00:27:23 +01001431early_initcall(init_hw_perf_events);
David S. Miller4f6dbe42010-01-19 00:26:13 -08001432
Frederic Weisbecker56962b42010-06-30 23:03:51 +02001433void perf_callchain_kernel(struct perf_callchain_entry *entry,
1434 struct pt_regs *regs)
David S. Miller4f6dbe42010-01-19 00:26:13 -08001435{
1436 unsigned long ksp, fp;
David S. Miller667f0ce2010-04-21 03:08:11 -07001437#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1438 int graph = 0;
1439#endif
David S. Miller4f6dbe42010-01-19 00:26:13 -08001440
Frederic Weisbecker56962b42010-06-30 23:03:51 +02001441 stack_trace_flush();
1442
Frederic Weisbecker70791ce2010-06-29 19:34:05 +02001443 perf_callchain_store(entry, regs->tpc);
David S. Miller4f6dbe42010-01-19 00:26:13 -08001444
1445 ksp = regs->u_regs[UREG_I6];
1446 fp = ksp + STACK_BIAS;
1447 do {
1448 struct sparc_stackf *sf;
1449 struct pt_regs *regs;
1450 unsigned long pc;
1451
1452 if (!kstack_valid(current_thread_info(), fp))
1453 break;
1454
1455 sf = (struct sparc_stackf *) fp;
1456 regs = (struct pt_regs *) (sf + 1);
1457
1458 if (kstack_is_trap_frame(current_thread_info(), regs)) {
1459 if (user_mode(regs))
1460 break;
1461 pc = regs->tpc;
1462 fp = regs->u_regs[UREG_I6] + STACK_BIAS;
1463 } else {
1464 pc = sf->callers_pc;
1465 fp = (unsigned long)sf->fp + STACK_BIAS;
1466 }
Frederic Weisbecker70791ce2010-06-29 19:34:05 +02001467 perf_callchain_store(entry, pc);
David S. Miller667f0ce2010-04-21 03:08:11 -07001468#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1469 if ((pc + 8UL) == (unsigned long) &return_to_handler) {
1470 int index = current->curr_ret_stack;
1471 if (current->ret_stack && index >= graph) {
1472 pc = current->ret_stack[index - graph].ret;
Frederic Weisbecker70791ce2010-06-29 19:34:05 +02001473 perf_callchain_store(entry, pc);
David S. Miller667f0ce2010-04-21 03:08:11 -07001474 graph++;
1475 }
1476 }
1477#endif
David S. Miller4f6dbe42010-01-19 00:26:13 -08001478 } while (entry->nr < PERF_MAX_STACK_DEPTH);
1479}
1480
Frederic Weisbecker56962b42010-06-30 23:03:51 +02001481static void perf_callchain_user_64(struct perf_callchain_entry *entry,
1482 struct pt_regs *regs)
David S. Miller4f6dbe42010-01-19 00:26:13 -08001483{
1484 unsigned long ufp;
1485
Frederic Weisbecker70791ce2010-06-29 19:34:05 +02001486 perf_callchain_store(entry, regs->tpc);
David S. Miller4f6dbe42010-01-19 00:26:13 -08001487
1488 ufp = regs->u_regs[UREG_I6] + STACK_BIAS;
1489 do {
1490 struct sparc_stackf *usf, sf;
1491 unsigned long pc;
1492
1493 usf = (struct sparc_stackf *) ufp;
1494 if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
1495 break;
1496
1497 pc = sf.callers_pc;
1498 ufp = (unsigned long)sf.fp + STACK_BIAS;
Frederic Weisbecker70791ce2010-06-29 19:34:05 +02001499 perf_callchain_store(entry, pc);
David S. Miller4f6dbe42010-01-19 00:26:13 -08001500 } while (entry->nr < PERF_MAX_STACK_DEPTH);
1501}
1502
Frederic Weisbecker56962b42010-06-30 23:03:51 +02001503static void perf_callchain_user_32(struct perf_callchain_entry *entry,
1504 struct pt_regs *regs)
David S. Miller4f6dbe42010-01-19 00:26:13 -08001505{
1506 unsigned long ufp;
1507
Frederic Weisbecker70791ce2010-06-29 19:34:05 +02001508 perf_callchain_store(entry, regs->tpc);
David S. Miller4f6dbe42010-01-19 00:26:13 -08001509
David S. Miller9e8307e2010-03-29 13:08:52 -07001510 ufp = regs->u_regs[UREG_I6] & 0xffffffffUL;
David S. Miller4f6dbe42010-01-19 00:26:13 -08001511 do {
1512 struct sparc_stackf32 *usf, sf;
1513 unsigned long pc;
1514
1515 usf = (struct sparc_stackf32 *) ufp;
1516 if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
1517 break;
1518
1519 pc = sf.callers_pc;
1520 ufp = (unsigned long)sf.fp;
Frederic Weisbecker70791ce2010-06-29 19:34:05 +02001521 perf_callchain_store(entry, pc);
David S. Miller4f6dbe42010-01-19 00:26:13 -08001522 } while (entry->nr < PERF_MAX_STACK_DEPTH);
1523}
1524
Frederic Weisbecker56962b42010-06-30 23:03:51 +02001525void
1526perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
David S. Miller4f6dbe42010-01-19 00:26:13 -08001527{
Frederic Weisbecker56962b42010-06-30 23:03:51 +02001528 flushw_user();
1529 if (test_thread_flag(TIF_32BIT))
1530 perf_callchain_user_32(entry, regs);
1531 else
1532 perf_callchain_user_64(entry, regs);
David S. Miller4f6dbe42010-01-19 00:26:13 -08001533}