blob: 34ce49f80eac53185dfe32b951bb10e91224b4e2 [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>
David S. Miller59abbd12009-09-10 06:28:20 -070025#include <asm/atomic.h>
26#include <asm/nmi.h>
27#include <asm/pcr.h>
28
David S. Miller4f6dbe42010-01-19 00:26:13 -080029#include "kstack.h"
30
David S. Miller59abbd12009-09-10 06:28:20 -070031/* Sparc64 chips have two performance counters, 32-bits each, with
32 * overflow interrupts generated on transition from 0xffffffff to 0.
33 * The counters are accessed in one go using a 64-bit register.
34 *
35 * Both counters are controlled using a single control register. The
36 * only way to stop all sampling is to clear all of the context (user,
37 * supervisor, hypervisor) sampling enable bits. But these bits apply
38 * to both counters, thus the two counters can't be enabled/disabled
39 * individually.
40 *
41 * The control register has two event fields, one for each of the two
42 * counters. It's thus nearly impossible to have one counter going
43 * while keeping the other one stopped. Therefore it is possible to
44 * get overflow interrupts for counters not currently "in use" and
45 * that condition must be checked in the overflow interrupt handler.
46 *
47 * So we use a hack, in that we program inactive counters with the
48 * "sw_count0" and "sw_count1" events. These count how many times
49 * the instruction "sethi %hi(0xfc000), %g0" is executed. It's an
50 * unusual way to encode a NOP and therefore will not trigger in
51 * normal code.
52 */
53
Ingo Molnarcdd6c482009-09-21 12:02:48 +020054#define MAX_HWEVENTS 2
David S. Miller59abbd12009-09-10 06:28:20 -070055#define MAX_PERIOD ((1UL << 32) - 1)
56
57#define PIC_UPPER_INDEX 0
58#define PIC_LOWER_INDEX 1
David S. Millere7bef6b2010-01-20 02:59:47 -080059#define PIC_NO_INDEX -1
David S. Miller59abbd12009-09-10 06:28:20 -070060
Ingo Molnarcdd6c482009-09-21 12:02:48 +020061struct cpu_hw_events {
David S. Millere7bef6b2010-01-20 02:59:47 -080062 /* Number of events currently scheduled onto this cpu.
63 * This tells how many entries in the arrays below
64 * are valid.
65 */
66 int n_events;
67
68 /* Number of new events added since the last hw_perf_disable().
69 * This works because the perf event layer always adds new
70 * events inside of a perf_{disable,enable}() sequence.
71 */
72 int n_added;
73
74 /* Array of events current scheduled on this cpu. */
75 struct perf_event *event[MAX_HWEVENTS];
76
77 /* Array of encoded longs, specifying the %pcr register
78 * encoding and the mask of PIC counters this even can
79 * be scheduled on. See perf_event_encode() et al.
80 */
81 unsigned long events[MAX_HWEVENTS];
82
83 /* The current counter index assigned to an event. When the
84 * event hasn't been programmed into the cpu yet, this will
85 * hold PIC_NO_INDEX. The event->hw.idx value tells us where
86 * we ought to schedule the event.
87 */
88 int current_idx[MAX_HWEVENTS];
89
90 /* Software copy of %pcr register on this cpu. */
David S. Millerd1751382009-09-29 21:27:06 -070091 u64 pcr;
David S. Millere7bef6b2010-01-20 02:59:47 -080092
93 /* Enabled/disable state. */
David S. Millerd1751382009-09-29 21:27:06 -070094 int enabled;
David S. Miller59abbd12009-09-10 06:28:20 -070095};
Ingo Molnarcdd6c482009-09-21 12:02:48 +020096DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { .enabled = 1, };
David S. Miller59abbd12009-09-10 06:28:20 -070097
David S. Millere7bef6b2010-01-20 02:59:47 -080098/* An event map describes the characteristics of a performance
99 * counter event. In particular it gives the encoding as well as
100 * a mask telling which counters the event can be measured on.
101 */
David S. Miller59abbd12009-09-10 06:28:20 -0700102struct perf_event_map {
103 u16 encoding;
104 u8 pic_mask;
105#define PIC_NONE 0x00
106#define PIC_UPPER 0x01
107#define PIC_LOWER 0x02
108};
109
David S. Millere7bef6b2010-01-20 02:59:47 -0800110/* Encode a perf_event_map entry into a long. */
David S. Millera72a8a52009-09-28 17:35:20 -0700111static unsigned long perf_event_encode(const struct perf_event_map *pmap)
112{
113 return ((unsigned long) pmap->encoding << 16) | pmap->pic_mask;
114}
115
David S. Millere7bef6b2010-01-20 02:59:47 -0800116static u8 perf_event_get_msk(unsigned long val)
David S. Millera72a8a52009-09-28 17:35:20 -0700117{
David S. Millere7bef6b2010-01-20 02:59:47 -0800118 return val & 0xff;
119}
120
121static u64 perf_event_get_enc(unsigned long val)
122{
123 return val >> 16;
David S. Millera72a8a52009-09-28 17:35:20 -0700124}
125
David S. Miller2ce4da22009-09-26 20:42:10 -0700126#define C(x) PERF_COUNT_HW_CACHE_##x
127
128#define CACHE_OP_UNSUPPORTED 0xfffe
129#define CACHE_OP_NONSENSE 0xffff
130
131typedef struct perf_event_map cache_map_t
132 [PERF_COUNT_HW_CACHE_MAX]
133 [PERF_COUNT_HW_CACHE_OP_MAX]
134 [PERF_COUNT_HW_CACHE_RESULT_MAX];
135
David S. Miller59abbd12009-09-10 06:28:20 -0700136struct sparc_pmu {
137 const struct perf_event_map *(*event_map)(int);
David S. Miller2ce4da22009-09-26 20:42:10 -0700138 const cache_map_t *cache_map;
David S. Miller59abbd12009-09-10 06:28:20 -0700139 int max_events;
140 int upper_shift;
141 int lower_shift;
142 int event_mask;
David S. Miller91b92862009-09-10 07:09:06 -0700143 int hv_bit;
David S. Miller496c07e2009-09-10 07:10:59 -0700144 int irq_bit;
David S. Miller660d1372009-09-10 07:13:26 -0700145 int upper_nop;
146 int lower_nop;
David S. Miller59abbd12009-09-10 06:28:20 -0700147};
148
David S. Miller28e8f9b2009-09-26 20:54:22 -0700149static const struct perf_event_map ultra3_perfmon_event_map[] = {
David S. Miller59abbd12009-09-10 06:28:20 -0700150 [PERF_COUNT_HW_CPU_CYCLES] = { 0x0000, PIC_UPPER | PIC_LOWER },
151 [PERF_COUNT_HW_INSTRUCTIONS] = { 0x0001, PIC_UPPER | PIC_LOWER },
152 [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0009, PIC_LOWER },
153 [PERF_COUNT_HW_CACHE_MISSES] = { 0x0009, PIC_UPPER },
154};
155
David S. Miller28e8f9b2009-09-26 20:54:22 -0700156static const struct perf_event_map *ultra3_event_map(int event_id)
David S. Miller59abbd12009-09-10 06:28:20 -0700157{
David S. Miller28e8f9b2009-09-26 20:54:22 -0700158 return &ultra3_perfmon_event_map[event_id];
David S. Miller59abbd12009-09-10 06:28:20 -0700159}
160
David S. Miller28e8f9b2009-09-26 20:54:22 -0700161static const cache_map_t ultra3_cache_map = {
David S. Miller2ce4da22009-09-26 20:42:10 -0700162[C(L1D)] = {
163 [C(OP_READ)] = {
164 [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
165 [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
166 },
167 [C(OP_WRITE)] = {
168 [C(RESULT_ACCESS)] = { 0x0a, PIC_LOWER },
169 [C(RESULT_MISS)] = { 0x0a, PIC_UPPER },
170 },
171 [C(OP_PREFETCH)] = {
172 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
173 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
174 },
175},
176[C(L1I)] = {
177 [C(OP_READ)] = {
178 [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
179 [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
180 },
181 [ C(OP_WRITE) ] = {
182 [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
183 [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
184 },
185 [ C(OP_PREFETCH) ] = {
186 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
187 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
188 },
189},
190[C(LL)] = {
191 [C(OP_READ)] = {
192 [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER, },
193 [C(RESULT_MISS)] = { 0x0c, PIC_UPPER, },
194 },
195 [C(OP_WRITE)] = {
196 [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER },
197 [C(RESULT_MISS)] = { 0x0c, PIC_UPPER },
198 },
199 [C(OP_PREFETCH)] = {
200 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
201 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
202 },
203},
204[C(DTLB)] = {
205 [C(OP_READ)] = {
206 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
207 [C(RESULT_MISS)] = { 0x12, PIC_UPPER, },
208 },
209 [ C(OP_WRITE) ] = {
210 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
211 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
212 },
213 [ C(OP_PREFETCH) ] = {
214 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
215 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
216 },
217},
218[C(ITLB)] = {
219 [C(OP_READ)] = {
220 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
221 [C(RESULT_MISS)] = { 0x11, PIC_UPPER, },
222 },
223 [ C(OP_WRITE) ] = {
224 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
225 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
226 },
227 [ C(OP_PREFETCH) ] = {
228 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
229 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
230 },
231},
232[C(BPU)] = {
233 [C(OP_READ)] = {
234 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
235 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
236 },
237 [ C(OP_WRITE) ] = {
238 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
239 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
240 },
241 [ C(OP_PREFETCH) ] = {
242 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
243 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
244 },
245},
246};
247
David S. Miller28e8f9b2009-09-26 20:54:22 -0700248static const struct sparc_pmu ultra3_pmu = {
249 .event_map = ultra3_event_map,
250 .cache_map = &ultra3_cache_map,
251 .max_events = ARRAY_SIZE(ultra3_perfmon_event_map),
David S. Miller59abbd12009-09-10 06:28:20 -0700252 .upper_shift = 11,
253 .lower_shift = 4,
254 .event_mask = 0x3f,
David S. Miller660d1372009-09-10 07:13:26 -0700255 .upper_nop = 0x1c,
256 .lower_nop = 0x14,
David S. Miller59abbd12009-09-10 06:28:20 -0700257};
258
David S. Miller7eebda62009-09-26 21:23:41 -0700259/* Niagara1 is very limited. The upper PIC is hard-locked to count
260 * only instructions, so it is free running which creates all kinds of
David S. Miller6e804252009-09-29 15:10:23 -0700261 * problems. Some hardware designs make one wonder if the creator
David S. Miller7eebda62009-09-26 21:23:41 -0700262 * even looked at how this stuff gets used by software.
263 */
264static const struct perf_event_map niagara1_perfmon_event_map[] = {
265 [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, PIC_UPPER },
266 [PERF_COUNT_HW_INSTRUCTIONS] = { 0x00, PIC_UPPER },
267 [PERF_COUNT_HW_CACHE_REFERENCES] = { 0, PIC_NONE },
268 [PERF_COUNT_HW_CACHE_MISSES] = { 0x03, PIC_LOWER },
269};
270
271static const struct perf_event_map *niagara1_event_map(int event_id)
272{
273 return &niagara1_perfmon_event_map[event_id];
274}
275
276static const cache_map_t niagara1_cache_map = {
277[C(L1D)] = {
278 [C(OP_READ)] = {
279 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
280 [C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
281 },
282 [C(OP_WRITE)] = {
283 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
284 [C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
285 },
286 [C(OP_PREFETCH)] = {
287 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
288 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
289 },
290},
291[C(L1I)] = {
292 [C(OP_READ)] = {
293 [C(RESULT_ACCESS)] = { 0x00, PIC_UPPER },
294 [C(RESULT_MISS)] = { 0x02, PIC_LOWER, },
295 },
296 [ C(OP_WRITE) ] = {
297 [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
298 [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
299 },
300 [ C(OP_PREFETCH) ] = {
301 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
302 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
303 },
304},
305[C(LL)] = {
306 [C(OP_READ)] = {
307 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
308 [C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
309 },
310 [C(OP_WRITE)] = {
311 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
312 [C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
313 },
314 [C(OP_PREFETCH)] = {
315 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
316 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
317 },
318},
319[C(DTLB)] = {
320 [C(OP_READ)] = {
321 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
322 [C(RESULT_MISS)] = { 0x05, PIC_LOWER, },
323 },
324 [ C(OP_WRITE) ] = {
325 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
326 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
327 },
328 [ C(OP_PREFETCH) ] = {
329 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
330 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
331 },
332},
333[C(ITLB)] = {
334 [C(OP_READ)] = {
335 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
336 [C(RESULT_MISS)] = { 0x04, PIC_LOWER, },
337 },
338 [ C(OP_WRITE) ] = {
339 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
340 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
341 },
342 [ C(OP_PREFETCH) ] = {
343 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
344 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
345 },
346},
347[C(BPU)] = {
348 [C(OP_READ)] = {
349 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
350 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
351 },
352 [ C(OP_WRITE) ] = {
353 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
354 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
355 },
356 [ C(OP_PREFETCH) ] = {
357 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
358 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
359 },
360},
361};
362
363static const struct sparc_pmu niagara1_pmu = {
364 .event_map = niagara1_event_map,
365 .cache_map = &niagara1_cache_map,
366 .max_events = ARRAY_SIZE(niagara1_perfmon_event_map),
367 .upper_shift = 0,
368 .lower_shift = 4,
369 .event_mask = 0x7,
370 .upper_nop = 0x0,
371 .lower_nop = 0x0,
372};
373
David S. Millerb73d8842009-09-10 07:22:18 -0700374static const struct perf_event_map niagara2_perfmon_event_map[] = {
375 [PERF_COUNT_HW_CPU_CYCLES] = { 0x02ff, PIC_UPPER | PIC_LOWER },
376 [PERF_COUNT_HW_INSTRUCTIONS] = { 0x02ff, PIC_UPPER | PIC_LOWER },
377 [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0208, PIC_UPPER | PIC_LOWER },
378 [PERF_COUNT_HW_CACHE_MISSES] = { 0x0302, PIC_UPPER | PIC_LOWER },
379 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x0201, PIC_UPPER | PIC_LOWER },
380 [PERF_COUNT_HW_BRANCH_MISSES] = { 0x0202, PIC_UPPER | PIC_LOWER },
381};
382
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200383static const struct perf_event_map *niagara2_event_map(int event_id)
David S. Millerb73d8842009-09-10 07:22:18 -0700384{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200385 return &niagara2_perfmon_event_map[event_id];
David S. Millerb73d8842009-09-10 07:22:18 -0700386}
387
David S. Millerd0b86482009-09-26 21:04:16 -0700388static const cache_map_t niagara2_cache_map = {
389[C(L1D)] = {
390 [C(OP_READ)] = {
391 [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
392 [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
393 },
394 [C(OP_WRITE)] = {
395 [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
396 [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
397 },
398 [C(OP_PREFETCH)] = {
399 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
400 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
401 },
402},
403[C(L1I)] = {
404 [C(OP_READ)] = {
405 [C(RESULT_ACCESS)] = { 0x02ff, PIC_UPPER | PIC_LOWER, },
406 [C(RESULT_MISS)] = { 0x0301, PIC_UPPER | PIC_LOWER, },
407 },
408 [ C(OP_WRITE) ] = {
409 [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
410 [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
411 },
412 [ C(OP_PREFETCH) ] = {
413 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
414 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
415 },
416},
417[C(LL)] = {
418 [C(OP_READ)] = {
419 [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
420 [C(RESULT_MISS)] = { 0x0330, PIC_UPPER | PIC_LOWER, },
421 },
422 [C(OP_WRITE)] = {
423 [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
424 [C(RESULT_MISS)] = { 0x0320, PIC_UPPER | PIC_LOWER, },
425 },
426 [C(OP_PREFETCH)] = {
427 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
428 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
429 },
430},
431[C(DTLB)] = {
432 [C(OP_READ)] = {
433 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
434 [C(RESULT_MISS)] = { 0x0b08, PIC_UPPER | PIC_LOWER, },
435 },
436 [ C(OP_WRITE) ] = {
437 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
438 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
439 },
440 [ C(OP_PREFETCH) ] = {
441 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
442 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
443 },
444},
445[C(ITLB)] = {
446 [C(OP_READ)] = {
447 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
448 [C(RESULT_MISS)] = { 0xb04, PIC_UPPER | PIC_LOWER, },
449 },
450 [ C(OP_WRITE) ] = {
451 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
452 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
453 },
454 [ C(OP_PREFETCH) ] = {
455 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
456 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
457 },
458},
459[C(BPU)] = {
460 [C(OP_READ)] = {
461 [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
462 [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
463 },
464 [ C(OP_WRITE) ] = {
465 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
466 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
467 },
468 [ C(OP_PREFETCH) ] = {
469 [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
470 [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
471 },
472},
473};
474
David S. Millerb73d8842009-09-10 07:22:18 -0700475static const struct sparc_pmu niagara2_pmu = {
476 .event_map = niagara2_event_map,
David S. Millerd0b86482009-09-26 21:04:16 -0700477 .cache_map = &niagara2_cache_map,
David S. Millerb73d8842009-09-10 07:22:18 -0700478 .max_events = ARRAY_SIZE(niagara2_perfmon_event_map),
479 .upper_shift = 19,
480 .lower_shift = 6,
481 .event_mask = 0xfff,
482 .hv_bit = 0x8,
David S. Millerde23cf32009-10-09 00:42:40 -0700483 .irq_bit = 0x30,
David S. Millerb73d8842009-09-10 07:22:18 -0700484 .upper_nop = 0x220,
485 .lower_nop = 0x220,
486};
487
David S. Miller59abbd12009-09-10 06:28:20 -0700488static const struct sparc_pmu *sparc_pmu __read_mostly;
489
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200490static u64 event_encoding(u64 event_id, int idx)
David S. Miller59abbd12009-09-10 06:28:20 -0700491{
492 if (idx == PIC_UPPER_INDEX)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200493 event_id <<= sparc_pmu->upper_shift;
David S. Miller59abbd12009-09-10 06:28:20 -0700494 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200495 event_id <<= sparc_pmu->lower_shift;
496 return event_id;
David S. Miller59abbd12009-09-10 06:28:20 -0700497}
498
499static u64 mask_for_index(int idx)
500{
501 return event_encoding(sparc_pmu->event_mask, idx);
502}
503
504static u64 nop_for_index(int idx)
505{
506 return event_encoding(idx == PIC_UPPER_INDEX ?
David S. Miller660d1372009-09-10 07:13:26 -0700507 sparc_pmu->upper_nop :
508 sparc_pmu->lower_nop, idx);
David S. Miller59abbd12009-09-10 06:28:20 -0700509}
510
David S. Millerd1751382009-09-29 21:27:06 -0700511static 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 -0700512{
513 u64 val, mask = mask_for_index(idx);
514
David S. Millerd1751382009-09-29 21:27:06 -0700515 val = cpuc->pcr;
516 val &= ~mask;
517 val |= hwc->config;
518 cpuc->pcr = val;
519
520 pcr_ops->write(cpuc->pcr);
David S. Miller59abbd12009-09-10 06:28:20 -0700521}
522
David S. Millerd1751382009-09-29 21:27:06 -0700523static 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 -0700524{
525 u64 mask = mask_for_index(idx);
526 u64 nop = nop_for_index(idx);
David S. Millerd1751382009-09-29 21:27:06 -0700527 u64 val;
David S. Miller59abbd12009-09-10 06:28:20 -0700528
David S. Millerd1751382009-09-29 21:27:06 -0700529 val = cpuc->pcr;
530 val &= ~mask;
531 val |= nop;
532 cpuc->pcr = val;
533
534 pcr_ops->write(cpuc->pcr);
David S. Miller59abbd12009-09-10 06:28:20 -0700535}
536
David S. Miller59abbd12009-09-10 06:28:20 -0700537static u32 read_pmc(int idx)
538{
539 u64 val;
540
541 read_pic(val);
542 if (idx == PIC_UPPER_INDEX)
543 val >>= 32;
544
545 return val & 0xffffffff;
546}
547
548static void write_pmc(int idx, u64 val)
549{
550 u64 shift, mask, pic;
551
552 shift = 0;
553 if (idx == PIC_UPPER_INDEX)
554 shift = 32;
555
556 mask = ((u64) 0xffffffff) << shift;
557 val <<= shift;
558
559 read_pic(pic);
560 pic &= ~mask;
561 pic |= val;
562 write_pic(pic);
563}
564
David S. Millere7bef6b2010-01-20 02:59:47 -0800565static u64 sparc_perf_event_update(struct perf_event *event,
566 struct hw_perf_event *hwc, int idx)
567{
568 int shift = 64 - 32;
569 u64 prev_raw_count, new_raw_count;
570 s64 delta;
571
572again:
573 prev_raw_count = atomic64_read(&hwc->prev_count);
574 new_raw_count = read_pmc(idx);
575
576 if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
577 new_raw_count) != prev_raw_count)
578 goto again;
579
580 delta = (new_raw_count << shift) - (prev_raw_count << shift);
581 delta >>= shift;
582
583 atomic64_add(delta, &event->count);
584 atomic64_sub(delta, &hwc->period_left);
585
586 return new_raw_count;
587}
588
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200589static int sparc_perf_event_set_period(struct perf_event *event,
David S. Millerd29862f2009-09-28 17:37:12 -0700590 struct hw_perf_event *hwc, int idx)
David S. Miller59abbd12009-09-10 06:28:20 -0700591{
592 s64 left = atomic64_read(&hwc->period_left);
593 s64 period = hwc->sample_period;
594 int ret = 0;
595
596 if (unlikely(left <= -period)) {
597 left = period;
598 atomic64_set(&hwc->period_left, left);
599 hwc->last_period = period;
600 ret = 1;
601 }
602
603 if (unlikely(left <= 0)) {
604 left += period;
605 atomic64_set(&hwc->period_left, left);
606 hwc->last_period = period;
607 ret = 1;
608 }
609 if (left > MAX_PERIOD)
610 left = MAX_PERIOD;
611
612 atomic64_set(&hwc->prev_count, (u64)-left);
613
614 write_pmc(idx, (u64)(-left) & 0xffffffff);
615
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200616 perf_event_update_userpage(event);
David S. Miller59abbd12009-09-10 06:28:20 -0700617
618 return ret;
619}
620
David S. Millere7bef6b2010-01-20 02:59:47 -0800621/* If performance event entries have been added, move existing
622 * events around (if necessary) and then assign new entries to
623 * counters.
624 */
625static u64 maybe_change_configuration(struct cpu_hw_events *cpuc, u64 pcr)
David S. Miller59abbd12009-09-10 06:28:20 -0700626{
David S. Millere7bef6b2010-01-20 02:59:47 -0800627 int i;
David S. Miller59abbd12009-09-10 06:28:20 -0700628
David S. Millere7bef6b2010-01-20 02:59:47 -0800629 if (!cpuc->n_added)
630 goto out;
David S. Miller59abbd12009-09-10 06:28:20 -0700631
David S. Millere7bef6b2010-01-20 02:59:47 -0800632 /* Read in the counters which are moving. */
633 for (i = 0; i < cpuc->n_events; i++) {
634 struct perf_event *cp = cpuc->event[i];
David S. Miller59abbd12009-09-10 06:28:20 -0700635
David S. Millere7bef6b2010-01-20 02:59:47 -0800636 if (cpuc->current_idx[i] != PIC_NO_INDEX &&
637 cpuc->current_idx[i] != cp->hw.idx) {
638 sparc_perf_event_update(cp, &cp->hw,
639 cpuc->current_idx[i]);
640 cpuc->current_idx[i] = PIC_NO_INDEX;
641 }
642 }
David S. Miller59abbd12009-09-10 06:28:20 -0700643
David S. Millere7bef6b2010-01-20 02:59:47 -0800644 /* Assign to counters all unassigned events. */
645 for (i = 0; i < cpuc->n_events; i++) {
646 struct perf_event *cp = cpuc->event[i];
647 struct hw_perf_event *hwc = &cp->hw;
648 int idx = hwc->idx;
649 u64 enc;
650
651 if (cpuc->current_idx[i] != PIC_NO_INDEX)
652 continue;
653
654 sparc_perf_event_set_period(cp, hwc, idx);
655 cpuc->current_idx[i] = idx;
656
657 enc = perf_event_get_enc(cpuc->events[i]);
658 pcr |= event_encoding(enc, idx);
659 }
660out:
661 return pcr;
David S. Miller59abbd12009-09-10 06:28:20 -0700662}
663
David S. Millere7bef6b2010-01-20 02:59:47 -0800664void hw_perf_enable(void)
David S. Miller59abbd12009-09-10 06:28:20 -0700665{
David S. Millere7bef6b2010-01-20 02:59:47 -0800666 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
667 u64 pcr;
David S. Miller59abbd12009-09-10 06:28:20 -0700668
David S. Millere7bef6b2010-01-20 02:59:47 -0800669 if (cpuc->enabled)
670 return;
David S. Miller59abbd12009-09-10 06:28:20 -0700671
David S. Millere7bef6b2010-01-20 02:59:47 -0800672 cpuc->enabled = 1;
673 barrier();
David S. Miller59abbd12009-09-10 06:28:20 -0700674
David S. Millere7bef6b2010-01-20 02:59:47 -0800675 pcr = cpuc->pcr;
676 if (!cpuc->n_events) {
677 pcr = 0;
678 } else {
679 pcr = maybe_change_configuration(cpuc, pcr);
David S. Miller59abbd12009-09-10 06:28:20 -0700680
David S. Millere7bef6b2010-01-20 02:59:47 -0800681 /* We require that all of the events have the same
682 * configuration, so just fetch the settings from the
683 * first entry.
684 */
685 cpuc->pcr = pcr | cpuc->event[0]->hw.config_base;
686 }
David S. Miller59abbd12009-09-10 06:28:20 -0700687
David S. Millere7bef6b2010-01-20 02:59:47 -0800688 pcr_ops->write(cpuc->pcr);
689}
690
691void hw_perf_disable(void)
692{
693 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
694 u64 val;
695
696 if (!cpuc->enabled)
697 return;
698
699 cpuc->enabled = 0;
700 cpuc->n_added = 0;
701
702 val = cpuc->pcr;
703 val &= ~(PCR_UTRACE | PCR_STRACE |
704 sparc_pmu->hv_bit | sparc_pmu->irq_bit);
705 cpuc->pcr = val;
706
707 pcr_ops->write(cpuc->pcr);
David S. Miller59abbd12009-09-10 06:28:20 -0700708}
709
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200710static void sparc_pmu_disable(struct perf_event *event)
David S. Miller59abbd12009-09-10 06:28:20 -0700711{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200712 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
713 struct hw_perf_event *hwc = &event->hw;
David S. Millere7bef6b2010-01-20 02:59:47 -0800714 unsigned long flags;
715 int i;
David S. Miller59abbd12009-09-10 06:28:20 -0700716
David S. Millere7bef6b2010-01-20 02:59:47 -0800717 local_irq_save(flags);
718 perf_disable();
David S. Miller59abbd12009-09-10 06:28:20 -0700719
David S. Millere7bef6b2010-01-20 02:59:47 -0800720 for (i = 0; i < cpuc->n_events; i++) {
721 if (event == cpuc->event[i]) {
722 int idx = cpuc->current_idx[i];
David S. Miller59abbd12009-09-10 06:28:20 -0700723
David S. Millere7bef6b2010-01-20 02:59:47 -0800724 /* Shift remaining entries down into
725 * the existing slot.
726 */
727 while (++i < cpuc->n_events) {
728 cpuc->event[i - 1] = cpuc->event[i];
729 cpuc->events[i - 1] = cpuc->events[i];
730 cpuc->current_idx[i - 1] =
731 cpuc->current_idx[i];
732 }
David S. Miller59abbd12009-09-10 06:28:20 -0700733
David S. Millere7bef6b2010-01-20 02:59:47 -0800734 /* Absorb the final count and turn off the
735 * event.
736 */
737 sparc_pmu_disable_event(cpuc, hwc, idx);
738 barrier();
739 sparc_perf_event_update(event, hwc, idx);
740
741 perf_event_update_userpage(event);
742
743 cpuc->n_events--;
744 break;
745 }
746 }
747
748 perf_enable();
749 local_irq_restore(flags);
750}
751
752static int active_event_index(struct cpu_hw_events *cpuc,
753 struct perf_event *event)
754{
755 int i;
756
757 for (i = 0; i < cpuc->n_events; i++) {
758 if (cpuc->event[i] == event)
759 break;
760 }
761 BUG_ON(i == cpuc->n_events);
762 return cpuc->current_idx[i];
David S. Miller59abbd12009-09-10 06:28:20 -0700763}
764
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200765static void sparc_pmu_read(struct perf_event *event)
David S. Miller59abbd12009-09-10 06:28:20 -0700766{
David S. Millere7bef6b2010-01-20 02:59:47 -0800767 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
768 int idx = active_event_index(cpuc, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200769 struct hw_perf_event *hwc = &event->hw;
David S. Millerd1751382009-09-29 21:27:06 -0700770
David S. Millere7bef6b2010-01-20 02:59:47 -0800771 sparc_perf_event_update(event, hwc, idx);
David S. Miller59abbd12009-09-10 06:28:20 -0700772}
773
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200774static void sparc_pmu_unthrottle(struct perf_event *event)
David S. Miller59abbd12009-09-10 06:28:20 -0700775{
David S. Millerd1751382009-09-29 21:27:06 -0700776 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
David S. Millere7bef6b2010-01-20 02:59:47 -0800777 int idx = active_event_index(cpuc, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200778 struct hw_perf_event *hwc = &event->hw;
David S. Millerd1751382009-09-29 21:27:06 -0700779
David S. Millere7bef6b2010-01-20 02:59:47 -0800780 sparc_pmu_enable_event(cpuc, hwc, idx);
David S. Miller59abbd12009-09-10 06:28:20 -0700781}
782
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200783static atomic_t active_events = ATOMIC_INIT(0);
David S. Miller59abbd12009-09-10 06:28:20 -0700784static DEFINE_MUTEX(pmc_grab_mutex);
785
David S. Millerd1751382009-09-29 21:27:06 -0700786static void perf_stop_nmi_watchdog(void *unused)
787{
788 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
789
790 stop_nmi_watchdog(NULL);
791 cpuc->pcr = pcr_ops->read();
792}
793
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200794void perf_event_grab_pmc(void)
David S. Miller59abbd12009-09-10 06:28:20 -0700795{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200796 if (atomic_inc_not_zero(&active_events))
David S. Miller59abbd12009-09-10 06:28:20 -0700797 return;
798
799 mutex_lock(&pmc_grab_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200800 if (atomic_read(&active_events) == 0) {
David S. Miller59abbd12009-09-10 06:28:20 -0700801 if (atomic_read(&nmi_active) > 0) {
David S. Millerd1751382009-09-29 21:27:06 -0700802 on_each_cpu(perf_stop_nmi_watchdog, NULL, 1);
David S. Miller59abbd12009-09-10 06:28:20 -0700803 BUG_ON(atomic_read(&nmi_active) != 0);
804 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200805 atomic_inc(&active_events);
David S. Miller59abbd12009-09-10 06:28:20 -0700806 }
807 mutex_unlock(&pmc_grab_mutex);
808}
809
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200810void perf_event_release_pmc(void)
David S. Miller59abbd12009-09-10 06:28:20 -0700811{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200812 if (atomic_dec_and_mutex_lock(&active_events, &pmc_grab_mutex)) {
David S. Miller59abbd12009-09-10 06:28:20 -0700813 if (atomic_read(&nmi_active) == 0)
814 on_each_cpu(start_nmi_watchdog, NULL, 1);
815 mutex_unlock(&pmc_grab_mutex);
816 }
817}
818
David S. Miller2ce4da22009-09-26 20:42:10 -0700819static const struct perf_event_map *sparc_map_cache_event(u64 config)
820{
821 unsigned int cache_type, cache_op, cache_result;
822 const struct perf_event_map *pmap;
823
824 if (!sparc_pmu->cache_map)
825 return ERR_PTR(-ENOENT);
826
827 cache_type = (config >> 0) & 0xff;
828 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
829 return ERR_PTR(-EINVAL);
830
831 cache_op = (config >> 8) & 0xff;
832 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
833 return ERR_PTR(-EINVAL);
834
835 cache_result = (config >> 16) & 0xff;
836 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
837 return ERR_PTR(-EINVAL);
838
839 pmap = &((*sparc_pmu->cache_map)[cache_type][cache_op][cache_result]);
840
841 if (pmap->encoding == CACHE_OP_UNSUPPORTED)
842 return ERR_PTR(-ENOENT);
843
844 if (pmap->encoding == CACHE_OP_NONSENSE)
845 return ERR_PTR(-EINVAL);
846
847 return pmap;
848}
849
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200850static void hw_perf_event_destroy(struct perf_event *event)
David S. Miller59abbd12009-09-10 06:28:20 -0700851{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200852 perf_event_release_pmc();
David S. Miller59abbd12009-09-10 06:28:20 -0700853}
854
David S. Millera72a8a52009-09-28 17:35:20 -0700855/* Make sure all events can be scheduled into the hardware at
856 * the same time. This is simplified by the fact that we only
857 * need to support 2 simultaneous HW events.
David S. Millere7bef6b2010-01-20 02:59:47 -0800858 *
859 * As a side effect, the evts[]->hw.idx values will be assigned
860 * on success. These are pending indexes. When the events are
861 * actually programmed into the chip, these values will propagate
862 * to the per-cpu cpuc->current_idx[] slots, see the code in
863 * maybe_change_configuration() for details.
David S. Millera72a8a52009-09-28 17:35:20 -0700864 */
David S. Millere7bef6b2010-01-20 02:59:47 -0800865static int sparc_check_constraints(struct perf_event **evts,
866 unsigned long *events, int n_ev)
David S. Millera72a8a52009-09-28 17:35:20 -0700867{
David S. Millere7bef6b2010-01-20 02:59:47 -0800868 u8 msk0 = 0, msk1 = 0;
869 int idx0 = 0;
David S. Millera72a8a52009-09-28 17:35:20 -0700870
David S. Millere7bef6b2010-01-20 02:59:47 -0800871 /* This case is possible when we are invoked from
872 * hw_perf_group_sched_in().
873 */
874 if (!n_ev)
875 return 0;
David S. Millera72a8a52009-09-28 17:35:20 -0700876
David S. Millere7bef6b2010-01-20 02:59:47 -0800877 if (n_ev > perf_max_events)
878 return -1;
David S. Millera72a8a52009-09-28 17:35:20 -0700879
David S. Millere7bef6b2010-01-20 02:59:47 -0800880 msk0 = perf_event_get_msk(events[0]);
881 if (n_ev == 1) {
882 if (msk0 & PIC_LOWER)
883 idx0 = 1;
884 goto success;
885 }
886 BUG_ON(n_ev != 2);
887 msk1 = perf_event_get_msk(events[1]);
David S. Millera72a8a52009-09-28 17:35:20 -0700888
David S. Millere7bef6b2010-01-20 02:59:47 -0800889 /* If both events can go on any counter, OK. */
890 if (msk0 == (PIC_UPPER | PIC_LOWER) &&
891 msk1 == (PIC_UPPER | PIC_LOWER))
892 goto success;
David S. Millera72a8a52009-09-28 17:35:20 -0700893
David S. Millere7bef6b2010-01-20 02:59:47 -0800894 /* If one event is limited to a specific counter,
895 * and the other can go on both, OK.
896 */
897 if ((msk0 == PIC_UPPER || msk0 == PIC_LOWER) &&
898 msk1 == (PIC_UPPER | PIC_LOWER)) {
899 if (msk0 & PIC_LOWER)
900 idx0 = 1;
901 goto success;
David S. Millera72a8a52009-09-28 17:35:20 -0700902 }
903
David S. Millere7bef6b2010-01-20 02:59:47 -0800904 if ((msk1 == PIC_UPPER || msk1 == PIC_LOWER) &&
905 msk0 == (PIC_UPPER | PIC_LOWER)) {
906 if (msk1 & PIC_UPPER)
907 idx0 = 1;
908 goto success;
909 }
910
911 /* If the events are fixed to different counters, OK. */
912 if ((msk0 == PIC_UPPER && msk1 == PIC_LOWER) ||
913 (msk0 == PIC_LOWER && msk1 == PIC_UPPER)) {
914 if (msk0 & PIC_LOWER)
915 idx0 = 1;
916 goto success;
917 }
918
919 /* Otherwise, there is a conflict. */
David S. Millera72a8a52009-09-28 17:35:20 -0700920 return -1;
David S. Millere7bef6b2010-01-20 02:59:47 -0800921
922success:
923 evts[0]->hw.idx = idx0;
924 if (n_ev == 2)
925 evts[1]->hw.idx = idx0 ^ 1;
926 return 0;
David S. Millera72a8a52009-09-28 17:35:20 -0700927}
928
David S. Miller01552f72009-09-27 20:43:07 -0700929static int check_excludes(struct perf_event **evts, int n_prev, int n_new)
930{
931 int eu = 0, ek = 0, eh = 0;
932 struct perf_event *event;
933 int i, n, first;
934
935 n = n_prev + n_new;
936 if (n <= 1)
937 return 0;
938
939 first = 1;
940 for (i = 0; i < n; i++) {
941 event = evts[i];
942 if (first) {
943 eu = event->attr.exclude_user;
944 ek = event->attr.exclude_kernel;
945 eh = event->attr.exclude_hv;
946 first = 0;
947 } else if (event->attr.exclude_user != eu ||
948 event->attr.exclude_kernel != ek ||
949 event->attr.exclude_hv != eh) {
950 return -EAGAIN;
951 }
952 }
953
954 return 0;
955}
956
957static int collect_events(struct perf_event *group, int max_count,
David S. Millere7bef6b2010-01-20 02:59:47 -0800958 struct perf_event *evts[], unsigned long *events,
959 int *current_idx)
David S. Miller01552f72009-09-27 20:43:07 -0700960{
961 struct perf_event *event;
962 int n = 0;
963
964 if (!is_software_event(group)) {
965 if (n >= max_count)
966 return -1;
967 evts[n] = group;
David S. Millere7bef6b2010-01-20 02:59:47 -0800968 events[n] = group->hw.event_base;
969 current_idx[n++] = PIC_NO_INDEX;
David S. Miller01552f72009-09-27 20:43:07 -0700970 }
971 list_for_each_entry(event, &group->sibling_list, group_entry) {
972 if (!is_software_event(event) &&
973 event->state != PERF_EVENT_STATE_OFF) {
974 if (n >= max_count)
975 return -1;
976 evts[n] = event;
David S. Millere7bef6b2010-01-20 02:59:47 -0800977 events[n] = event->hw.event_base;
978 current_idx[n++] = PIC_NO_INDEX;
David S. Miller01552f72009-09-27 20:43:07 -0700979 }
980 }
981 return n;
982}
983
Peter Zijlstra6e377382010-02-11 13:21:58 +0100984static void event_sched_in(struct perf_event *event)
David S. Millere7bef6b2010-01-20 02:59:47 -0800985{
986 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +0100987 event->oncpu = smp_processor_id();
David S. Millere7bef6b2010-01-20 02:59:47 -0800988 event->tstamp_running += event->ctx->time - event->tstamp_stopped;
989 if (is_software_event(event))
990 event->pmu->enable(event);
991}
992
993int hw_perf_group_sched_in(struct perf_event *group_leader,
994 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100995 struct perf_event_context *ctx)
David S. Millere7bef6b2010-01-20 02:59:47 -0800996{
997 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
998 struct perf_event *sub;
999 int n0, n;
1000
1001 if (!sparc_pmu)
1002 return 0;
1003
1004 n0 = cpuc->n_events;
1005 n = collect_events(group_leader, perf_max_events - n0,
1006 &cpuc->event[n0], &cpuc->events[n0],
1007 &cpuc->current_idx[n0]);
1008 if (n < 0)
1009 return -EAGAIN;
1010 if (check_excludes(cpuc->event, n0, n))
1011 return -EINVAL;
1012 if (sparc_check_constraints(cpuc->event, cpuc->events, n + n0))
1013 return -EAGAIN;
1014 cpuc->n_events = n0 + n;
1015 cpuc->n_added += n;
1016
1017 cpuctx->active_oncpu += n;
1018 n = 1;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001019 event_sched_in(group_leader);
David S. Millere7bef6b2010-01-20 02:59:47 -08001020 list_for_each_entry(sub, &group_leader->sibling_list, group_entry) {
1021 if (sub->state != PERF_EVENT_STATE_OFF) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01001022 event_sched_in(sub);
David S. Millere7bef6b2010-01-20 02:59:47 -08001023 n++;
1024 }
1025 }
1026 ctx->nr_active += n;
1027
1028 return 1;
1029}
1030
1031static int sparc_pmu_enable(struct perf_event *event)
1032{
1033 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1034 int n0, ret = -EAGAIN;
1035 unsigned long flags;
1036
1037 local_irq_save(flags);
1038 perf_disable();
1039
1040 n0 = cpuc->n_events;
1041 if (n0 >= perf_max_events)
1042 goto out;
1043
1044 cpuc->event[n0] = event;
1045 cpuc->events[n0] = event->hw.event_base;
1046 cpuc->current_idx[n0] = PIC_NO_INDEX;
1047
1048 if (check_excludes(cpuc->event, n0, 1))
1049 goto out;
1050 if (sparc_check_constraints(cpuc->event, cpuc->events, n0 + 1))
1051 goto out;
1052
1053 cpuc->n_events++;
1054 cpuc->n_added++;
1055
1056 ret = 0;
1057out:
1058 perf_enable();
1059 local_irq_restore(flags);
1060 return ret;
1061}
1062
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001063static int __hw_perf_event_init(struct perf_event *event)
David S. Miller59abbd12009-09-10 06:28:20 -07001064{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001065 struct perf_event_attr *attr = &event->attr;
David S. Miller01552f72009-09-27 20:43:07 -07001066 struct perf_event *evts[MAX_HWEVENTS];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001067 struct hw_perf_event *hwc = &event->hw;
David S. Millera72a8a52009-09-28 17:35:20 -07001068 unsigned long events[MAX_HWEVENTS];
David S. Millere7bef6b2010-01-20 02:59:47 -08001069 int current_idx_dmy[MAX_HWEVENTS];
David S. Miller59abbd12009-09-10 06:28:20 -07001070 const struct perf_event_map *pmap;
David S. Miller01552f72009-09-27 20:43:07 -07001071 int n;
David S. Miller59abbd12009-09-10 06:28:20 -07001072
1073 if (atomic_read(&nmi_active) < 0)
1074 return -ENODEV;
1075
David S. Miller2ce4da22009-09-26 20:42:10 -07001076 if (attr->type == PERF_TYPE_HARDWARE) {
1077 if (attr->config >= sparc_pmu->max_events)
1078 return -EINVAL;
1079 pmap = sparc_pmu->event_map(attr->config);
1080 } else if (attr->type == PERF_TYPE_HW_CACHE) {
1081 pmap = sparc_map_cache_event(attr->config);
1082 if (IS_ERR(pmap))
1083 return PTR_ERR(pmap);
1084 } else
David S. Miller59abbd12009-09-10 06:28:20 -07001085 return -EOPNOTSUPP;
1086
David S. Millere7bef6b2010-01-20 02:59:47 -08001087 /* We save the enable bits in the config_base. */
David S. Miller496c07e2009-09-10 07:10:59 -07001088 hwc->config_base = sparc_pmu->irq_bit;
David S. Miller59abbd12009-09-10 06:28:20 -07001089 if (!attr->exclude_user)
1090 hwc->config_base |= PCR_UTRACE;
1091 if (!attr->exclude_kernel)
1092 hwc->config_base |= PCR_STRACE;
David S. Miller91b92862009-09-10 07:09:06 -07001093 if (!attr->exclude_hv)
1094 hwc->config_base |= sparc_pmu->hv_bit;
David S. Miller59abbd12009-09-10 06:28:20 -07001095
David S. Millera72a8a52009-09-28 17:35:20 -07001096 hwc->event_base = perf_event_encode(pmap);
1097
David S. Miller01552f72009-09-27 20:43:07 -07001098 n = 0;
1099 if (event->group_leader != event) {
1100 n = collect_events(event->group_leader,
1101 perf_max_events - 1,
David S. Millere7bef6b2010-01-20 02:59:47 -08001102 evts, events, current_idx_dmy);
David S. Miller01552f72009-09-27 20:43:07 -07001103 if (n < 0)
1104 return -EINVAL;
1105 }
David S. Millera72a8a52009-09-28 17:35:20 -07001106 events[n] = hwc->event_base;
David S. Miller01552f72009-09-27 20:43:07 -07001107 evts[n] = event;
1108
1109 if (check_excludes(evts, n, 1))
1110 return -EINVAL;
1111
David S. Millere7bef6b2010-01-20 02:59:47 -08001112 if (sparc_check_constraints(evts, events, n + 1))
David S. Millera72a8a52009-09-28 17:35:20 -07001113 return -EINVAL;
1114
David S. Millere7bef6b2010-01-20 02:59:47 -08001115 hwc->idx = PIC_NO_INDEX;
1116
David S. Miller01552f72009-09-27 20:43:07 -07001117 /* Try to do all error checking before this point, as unwinding
1118 * state after grabbing the PMC is difficult.
1119 */
1120 perf_event_grab_pmc();
1121 event->destroy = hw_perf_event_destroy;
1122
David S. Miller59abbd12009-09-10 06:28:20 -07001123 if (!hwc->sample_period) {
1124 hwc->sample_period = MAX_PERIOD;
1125 hwc->last_period = hwc->sample_period;
1126 atomic64_set(&hwc->period_left, hwc->sample_period);
1127 }
1128
David S. Miller59abbd12009-09-10 06:28:20 -07001129 return 0;
1130}
1131
1132static const struct pmu pmu = {
1133 .enable = sparc_pmu_enable,
1134 .disable = sparc_pmu_disable,
1135 .read = sparc_pmu_read,
1136 .unthrottle = sparc_pmu_unthrottle,
1137};
1138
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001139const struct pmu *hw_perf_event_init(struct perf_event *event)
David S. Miller59abbd12009-09-10 06:28:20 -07001140{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001141 int err = __hw_perf_event_init(event);
David S. Miller59abbd12009-09-10 06:28:20 -07001142
1143 if (err)
1144 return ERR_PTR(err);
1145 return &pmu;
1146}
1147
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001148void perf_event_print_debug(void)
David S. Miller59abbd12009-09-10 06:28:20 -07001149{
1150 unsigned long flags;
1151 u64 pcr, pic;
1152 int cpu;
1153
1154 if (!sparc_pmu)
1155 return;
1156
1157 local_irq_save(flags);
1158
1159 cpu = smp_processor_id();
1160
1161 pcr = pcr_ops->read();
1162 read_pic(pic);
1163
1164 pr_info("\n");
1165 pr_info("CPU#%d: PCR[%016llx] PIC[%016llx]\n",
1166 cpu, pcr, pic);
1167
1168 local_irq_restore(flags);
1169}
1170
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001171static int __kprobes perf_event_nmi_handler(struct notifier_block *self,
David S. Millerd29862f2009-09-28 17:37:12 -07001172 unsigned long cmd, void *__args)
David S. Miller59abbd12009-09-10 06:28:20 -07001173{
1174 struct die_args *args = __args;
1175 struct perf_sample_data data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001176 struct cpu_hw_events *cpuc;
David S. Miller59abbd12009-09-10 06:28:20 -07001177 struct pt_regs *regs;
David S. Millere7bef6b2010-01-20 02:59:47 -08001178 int i;
David S. Miller59abbd12009-09-10 06:28:20 -07001179
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001180 if (!atomic_read(&active_events))
David S. Miller59abbd12009-09-10 06:28:20 -07001181 return NOTIFY_DONE;
1182
1183 switch (cmd) {
1184 case DIE_NMI:
1185 break;
1186
1187 default:
1188 return NOTIFY_DONE;
1189 }
1190
1191 regs = args->regs;
1192
Peter Zijlstradc1d6282010-03-03 15:55:04 +01001193 perf_sample_data_init(&data, 0);
David S. Miller59abbd12009-09-10 06:28:20 -07001194
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001195 cpuc = &__get_cpu_var(cpu_hw_events);
David S. Millere04ed382010-01-04 23:16:03 -08001196
1197 /* If the PMU has the TOE IRQ enable bits, we need to do a
1198 * dummy write to the %pcr to clear the overflow bits and thus
1199 * the interrupt.
1200 *
1201 * Do this before we peek at the counters to determine
1202 * overflow so we don't lose any events.
1203 */
1204 if (sparc_pmu->irq_bit)
1205 pcr_ops->write(cpuc->pcr);
1206
David S. Millere7bef6b2010-01-20 02:59:47 -08001207 for (i = 0; i < cpuc->n_events; i++) {
1208 struct perf_event *event = cpuc->event[i];
1209 int idx = cpuc->current_idx[i];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001210 struct hw_perf_event *hwc;
David S. Miller59abbd12009-09-10 06:28:20 -07001211 u64 val;
1212
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001213 hwc = &event->hw;
1214 val = sparc_perf_event_update(event, hwc, idx);
David S. Miller59abbd12009-09-10 06:28:20 -07001215 if (val & (1ULL << 31))
1216 continue;
1217
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001218 data.period = event->hw.last_period;
1219 if (!sparc_perf_event_set_period(event, hwc, idx))
David S. Miller59abbd12009-09-10 06:28:20 -07001220 continue;
1221
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001222 if (perf_event_overflow(event, 1, &data, regs))
David S. Millerd1751382009-09-29 21:27:06 -07001223 sparc_pmu_disable_event(cpuc, hwc, idx);
David S. Miller59abbd12009-09-10 06:28:20 -07001224 }
1225
1226 return NOTIFY_STOP;
1227}
1228
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001229static __read_mostly struct notifier_block perf_event_nmi_notifier = {
1230 .notifier_call = perf_event_nmi_handler,
David S. Miller59abbd12009-09-10 06:28:20 -07001231};
1232
1233static bool __init supported_pmu(void)
1234{
David S. Miller28e8f9b2009-09-26 20:54:22 -07001235 if (!strcmp(sparc_pmu_type, "ultra3") ||
1236 !strcmp(sparc_pmu_type, "ultra3+") ||
1237 !strcmp(sparc_pmu_type, "ultra3i") ||
1238 !strcmp(sparc_pmu_type, "ultra4+")) {
1239 sparc_pmu = &ultra3_pmu;
David S. Miller59abbd12009-09-10 06:28:20 -07001240 return true;
1241 }
David S. Miller7eebda62009-09-26 21:23:41 -07001242 if (!strcmp(sparc_pmu_type, "niagara")) {
1243 sparc_pmu = &niagara1_pmu;
1244 return true;
1245 }
David S. Millerb73d8842009-09-10 07:22:18 -07001246 if (!strcmp(sparc_pmu_type, "niagara2")) {
1247 sparc_pmu = &niagara2_pmu;
1248 return true;
1249 }
David S. Miller59abbd12009-09-10 06:28:20 -07001250 return false;
1251}
1252
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001253void __init init_hw_perf_events(void)
David S. Miller59abbd12009-09-10 06:28:20 -07001254{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001255 pr_info("Performance events: ");
David S. Miller59abbd12009-09-10 06:28:20 -07001256
1257 if (!supported_pmu()) {
1258 pr_cont("No support for PMU type '%s'\n", sparc_pmu_type);
1259 return;
1260 }
1261
1262 pr_cont("Supported PMU type is '%s'\n", sparc_pmu_type);
1263
David S. Millere7bef6b2010-01-20 02:59:47 -08001264 /* All sparc64 PMUs currently have 2 events. */
1265 perf_max_events = 2;
David S. Miller59abbd12009-09-10 06:28:20 -07001266
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001267 register_die_notifier(&perf_event_nmi_notifier);
David S. Miller59abbd12009-09-10 06:28:20 -07001268}
David S. Miller4f6dbe42010-01-19 00:26:13 -08001269
1270static inline void callchain_store(struct perf_callchain_entry *entry, u64 ip)
1271{
1272 if (entry->nr < PERF_MAX_STACK_DEPTH)
1273 entry->ip[entry->nr++] = ip;
1274}
1275
1276static void perf_callchain_kernel(struct pt_regs *regs,
1277 struct perf_callchain_entry *entry)
1278{
1279 unsigned long ksp, fp;
David S. Miller667f0ce2010-04-21 03:08:11 -07001280#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1281 int graph = 0;
1282#endif
David S. Miller4f6dbe42010-01-19 00:26:13 -08001283
1284 callchain_store(entry, PERF_CONTEXT_KERNEL);
1285 callchain_store(entry, regs->tpc);
1286
1287 ksp = regs->u_regs[UREG_I6];
1288 fp = ksp + STACK_BIAS;
1289 do {
1290 struct sparc_stackf *sf;
1291 struct pt_regs *regs;
1292 unsigned long pc;
1293
1294 if (!kstack_valid(current_thread_info(), fp))
1295 break;
1296
1297 sf = (struct sparc_stackf *) fp;
1298 regs = (struct pt_regs *) (sf + 1);
1299
1300 if (kstack_is_trap_frame(current_thread_info(), regs)) {
1301 if (user_mode(regs))
1302 break;
1303 pc = regs->tpc;
1304 fp = regs->u_regs[UREG_I6] + STACK_BIAS;
1305 } else {
1306 pc = sf->callers_pc;
1307 fp = (unsigned long)sf->fp + STACK_BIAS;
1308 }
1309 callchain_store(entry, pc);
David S. Miller667f0ce2010-04-21 03:08:11 -07001310#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1311 if ((pc + 8UL) == (unsigned long) &return_to_handler) {
1312 int index = current->curr_ret_stack;
1313 if (current->ret_stack && index >= graph) {
1314 pc = current->ret_stack[index - graph].ret;
1315 callchain_store(entry, pc);
1316 graph++;
1317 }
1318 }
1319#endif
David S. Miller4f6dbe42010-01-19 00:26:13 -08001320 } while (entry->nr < PERF_MAX_STACK_DEPTH);
1321}
1322
1323static void perf_callchain_user_64(struct pt_regs *regs,
1324 struct perf_callchain_entry *entry)
1325{
1326 unsigned long ufp;
1327
1328 callchain_store(entry, PERF_CONTEXT_USER);
1329 callchain_store(entry, regs->tpc);
1330
1331 ufp = regs->u_regs[UREG_I6] + STACK_BIAS;
1332 do {
1333 struct sparc_stackf *usf, sf;
1334 unsigned long pc;
1335
1336 usf = (struct sparc_stackf *) ufp;
1337 if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
1338 break;
1339
1340 pc = sf.callers_pc;
1341 ufp = (unsigned long)sf.fp + STACK_BIAS;
1342 callchain_store(entry, pc);
1343 } while (entry->nr < PERF_MAX_STACK_DEPTH);
1344}
1345
1346static void perf_callchain_user_32(struct pt_regs *regs,
1347 struct perf_callchain_entry *entry)
1348{
1349 unsigned long ufp;
1350
1351 callchain_store(entry, PERF_CONTEXT_USER);
1352 callchain_store(entry, regs->tpc);
1353
David S. Miller9e8307e2010-03-29 13:08:52 -07001354 ufp = regs->u_regs[UREG_I6] & 0xffffffffUL;
David S. Miller4f6dbe42010-01-19 00:26:13 -08001355 do {
1356 struct sparc_stackf32 *usf, sf;
1357 unsigned long pc;
1358
1359 usf = (struct sparc_stackf32 *) ufp;
1360 if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
1361 break;
1362
1363 pc = sf.callers_pc;
1364 ufp = (unsigned long)sf.fp;
1365 callchain_store(entry, pc);
1366 } while (entry->nr < PERF_MAX_STACK_DEPTH);
1367}
1368
1369/* Like powerpc we can't get PMU interrupts within the PMU handler,
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001370 * so no need for separate NMI and IRQ chains as on x86.
David S. Miller4f6dbe42010-01-19 00:26:13 -08001371 */
1372static DEFINE_PER_CPU(struct perf_callchain_entry, callchain);
1373
1374struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1375{
1376 struct perf_callchain_entry *entry = &__get_cpu_var(callchain);
1377
1378 entry->nr = 0;
1379 if (!user_mode(regs)) {
1380 stack_trace_flush();
1381 perf_callchain_kernel(regs, entry);
1382 if (current->mm)
1383 regs = task_pt_regs(current);
1384 else
1385 regs = NULL;
1386 }
1387 if (regs) {
1388 flushw_user();
1389 if (test_thread_flag(TIF_32BIT))
1390 perf_callchain_user_32(regs, entry);
1391 else
1392 perf_callchain_user_64(regs, entry);
1393 }
1394 return entry;
1395}