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