perf_event.c 45.7 KB
Newer Older
1
/* Performance event support for sparc64.
2
 *
3
 * Copyright (C) 2009, 2010 David S. Miller <davem@davemloft.net>
4
 *
5
 * This code is based almost entirely upon the x86 perf event
6 7 8 9 10 11
 * code, which is:
 *
 *  Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
 *  Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
 *  Copyright (C) 2009 Jaswinder Singh Rajput
 *  Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
12
 *  Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra
13 14
 */

15
#include <linux/perf_event.h>
16
#include <linux/kprobes.h>
17
#include <linux/ftrace.h>
18 19 20 21
#include <linux/kernel.h>
#include <linux/kdebug.h>
#include <linux/mutex.h>

22
#include <asm/stacktrace.h>
23
#include <asm/cpudata.h>
24
#include <linux/uaccess.h>
A
Arun Sharma 已提交
25
#include <linux/atomic.h>
26 27
#include <asm/nmi.h>
#include <asm/pcr.h>
28
#include <asm/cacheflush.h>
29

30
#include "kernel.h"
31 32
#include "kstack.h"

33 34 35
/* Two classes of sparc64 chips currently exist.  All of which have
 * 32-bit counters which can generate overflow interrupts on the
 * transition from 0xffffffff to 0.
36
 *
37 38 39
 * All chips upto and including SPARC-T3 have two performance
 * counters.  The two 32-bit counters are accessed in one go using a
 * single 64-bit register.
40
 *
41 42 43 44 45 46 47 48 49 50 51 52
 * On these older chips both counters are controlled using a single
 * control register.  The only way to stop all sampling is to clear
 * all of the context (user, supervisor, hypervisor) sampling enable
 * bits.  But these bits apply to both counters, thus the two counters
 * can't be enabled/disabled individually.
 *
 * Furthermore, the control register on these older chips have two
 * event fields, one for each of the two counters.  It's thus nearly
 * impossible to have one counter going while keeping the other one
 * stopped.  Therefore it is possible to get overflow interrupts for
 * counters not currently "in use" and that condition must be checked
 * in the overflow interrupt handler.
53 54 55 56 57 58
 *
 * So we use a hack, in that we program inactive counters with the
 * "sw_count0" and "sw_count1" events.  These count how many times
 * the instruction "sethi %hi(0xfc000), %g0" is executed.  It's an
 * unusual way to encode a NOP and therefore will not trigger in
 * normal code.
59 60 61 62 63 64 65
 *
 * Starting with SPARC-T4 we have one control register per counter.
 * And the counters are stored in individual registers.  The registers
 * for the counters are 64-bit but only a 32-bit counter is
 * implemented.  The event selections on SPARC-T4 lack any
 * restrictions, therefore we can elide all of the complicated
 * conflict resolution code we have for SPARC-T3 and earlier chips.
66 67
 */

68 69
#define MAX_HWEVENTS			4
#define MAX_PCRS			4
70 71 72 73
#define MAX_PERIOD			((1UL << 32) - 1)

#define PIC_UPPER_INDEX			0
#define PIC_LOWER_INDEX			1
74
#define PIC_NO_INDEX			-1
75

76
struct cpu_hw_events {
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	/* Number of events currently scheduled onto this cpu.
	 * This tells how many entries in the arrays below
	 * are valid.
	 */
	int			n_events;

	/* Number of new events added since the last hw_perf_disable().
	 * This works because the perf event layer always adds new
	 * events inside of a perf_{disable,enable}() sequence.
	 */
	int			n_added;

	/* Array of events current scheduled on this cpu.  */
	struct perf_event	*event[MAX_HWEVENTS];

	/* Array of encoded longs, specifying the %pcr register
	 * encoding and the mask of PIC counters this even can
	 * be scheduled on.  See perf_event_encode() et al.
	 */
	unsigned long		events[MAX_HWEVENTS];

	/* The current counter index assigned to an event.  When the
	 * event hasn't been programmed into the cpu yet, this will
	 * hold PIC_NO_INDEX.  The event->hw.idx value tells us where
	 * we ought to schedule the event.
	 */
	int			current_idx[MAX_HWEVENTS];

105 106
	/* Software copy of %pcr register(s) on this cpu.  */
	u64			pcr[MAX_HWEVENTS];
107 108

	/* Enabled/disable state.  */
109
	int			enabled;
110

111
	unsigned int		txn_flags;
112
};
113
static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { .enabled = 1, };
114

115 116 117
/* An event map describes the characteristics of a performance
 * counter event.  In particular it gives the encoding as well as
 * a mask telling which counters the event can be measured on.
118 119
 *
 * The mask is unused on SPARC-T4 and later.
120
 */
121 122 123 124 125 126 127 128
struct perf_event_map {
	u16	encoding;
	u8	pic_mask;
#define PIC_NONE	0x00
#define PIC_UPPER	0x01
#define PIC_LOWER	0x02
};

129
/* Encode a perf_event_map entry into a long.  */
130 131 132 133 134
static unsigned long perf_event_encode(const struct perf_event_map *pmap)
{
	return ((unsigned long) pmap->encoding << 16) | pmap->pic_mask;
}

135 136 137 138 139 140
static u8 perf_event_get_msk(unsigned long val)
{
	return val & 0xff;
}

static u64 perf_event_get_enc(unsigned long val)
141
{
142
	return val >> 16;
143 144
}

145 146 147 148 149 150 151 152 153 154
#define C(x) PERF_COUNT_HW_CACHE_##x

#define CACHE_OP_UNSUPPORTED	0xfffe
#define CACHE_OP_NONSENSE	0xffff

typedef struct perf_event_map cache_map_t
				[PERF_COUNT_HW_CACHE_MAX]
				[PERF_COUNT_HW_CACHE_OP_MAX]
				[PERF_COUNT_HW_CACHE_RESULT_MAX];

155 156
struct sparc_pmu {
	const struct perf_event_map	*(*event_map)(int);
157
	const cache_map_t		*cache_map;
158
	int				max_events;
159 160
	u32				(*read_pmc)(int);
	void				(*write_pmc)(int, u64);
161 162 163
	int				upper_shift;
	int				lower_shift;
	int				event_mask;
164 165
	int				user_bit;
	int				priv_bit;
166
	int				hv_bit;
167
	int				irq_bit;
168 169
	int				upper_nop;
	int				lower_nop;
170 171 172
	unsigned int			flags;
#define SPARC_PMU_ALL_EXCLUDES_SAME	0x00000001
#define SPARC_PMU_HAS_CONFLICTS		0x00000002
173
	int				max_hw_events;
174 175
	int				num_pcrs;
	int				num_pic_regs;
176 177
};

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
static u32 sparc_default_read_pmc(int idx)
{
	u64 val;

	val = pcr_ops->read_pic(0);
	if (idx == PIC_UPPER_INDEX)
		val >>= 32;

	return val & 0xffffffff;
}

static void sparc_default_write_pmc(int idx, u64 val)
{
	u64 shift, mask, pic;

	shift = 0;
	if (idx == PIC_UPPER_INDEX)
		shift = 32;

	mask = ((u64) 0xffffffff) << shift;
	val <<= shift;

	pic = pcr_ops->read_pic(0);
	pic &= ~mask;
	pic |= val;
	pcr_ops->write_pic(0, pic);
}

206
static const struct perf_event_map ultra3_perfmon_event_map[] = {
207 208 209 210 211 212
	[PERF_COUNT_HW_CPU_CYCLES] = { 0x0000, PIC_UPPER | PIC_LOWER },
	[PERF_COUNT_HW_INSTRUCTIONS] = { 0x0001, PIC_UPPER | PIC_LOWER },
	[PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0009, PIC_LOWER },
	[PERF_COUNT_HW_CACHE_MISSES] = { 0x0009, PIC_UPPER },
};

213
static const struct perf_event_map *ultra3_event_map(int event_id)
214
{
215
	return &ultra3_perfmon_event_map[event_id];
216 217
}

218
static const cache_map_t ultra3_cache_map = {
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
[C(L1D)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
		[C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
	},
	[C(OP_WRITE)] = {
		[C(RESULT_ACCESS)] = { 0x0a, PIC_LOWER },
		[C(RESULT_MISS)] = { 0x0a, PIC_UPPER },
	},
	[C(OP_PREFETCH)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(L1I)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
		[C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
		[ C(RESULT_MISS)   ] = { CACHE_OP_NONSENSE },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(LL)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER, },
		[C(RESULT_MISS)] = { 0x0c, PIC_UPPER, },
	},
	[C(OP_WRITE)] = {
		[C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER },
		[C(RESULT_MISS)] = { 0x0c, PIC_UPPER },
	},
	[C(OP_PREFETCH)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(DTLB)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { 0x12, PIC_UPPER, },
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(ITLB)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { 0x11, PIC_UPPER, },
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(BPU)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
303 304 305 306 307 308 309 310 311 312 313 314 315 316
[C(NODE)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)  ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
317 318
};

319 320 321 322
static const struct sparc_pmu ultra3_pmu = {
	.event_map	= ultra3_event_map,
	.cache_map	= &ultra3_cache_map,
	.max_events	= ARRAY_SIZE(ultra3_perfmon_event_map),
323 324
	.read_pmc	= sparc_default_read_pmc,
	.write_pmc	= sparc_default_write_pmc,
325 326 327
	.upper_shift	= 11,
	.lower_shift	= 4,
	.event_mask	= 0x3f,
328 329
	.user_bit	= PCR_UTRACE,
	.priv_bit	= PCR_STRACE,
330 331
	.upper_nop	= 0x1c,
	.lower_nop	= 0x14,
332 333
	.flags		= (SPARC_PMU_ALL_EXCLUDES_SAME |
			   SPARC_PMU_HAS_CONFLICTS),
334
	.max_hw_events	= 2,
335 336
	.num_pcrs	= 1,
	.num_pic_regs	= 1,
337 338
};

339 340
/* Niagara1 is very limited.  The upper PIC is hard-locked to count
 * only instructions, so it is free running which creates all kinds of
341
 * problems.  Some hardware designs make one wonder if the creator
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
 * even looked at how this stuff gets used by software.
 */
static const struct perf_event_map niagara1_perfmon_event_map[] = {
	[PERF_COUNT_HW_CPU_CYCLES] = { 0x00, PIC_UPPER },
	[PERF_COUNT_HW_INSTRUCTIONS] = { 0x00, PIC_UPPER },
	[PERF_COUNT_HW_CACHE_REFERENCES] = { 0, PIC_NONE },
	[PERF_COUNT_HW_CACHE_MISSES] = { 0x03, PIC_LOWER },
};

static const struct perf_event_map *niagara1_event_map(int event_id)
{
	return &niagara1_perfmon_event_map[event_id];
}

static const cache_map_t niagara1_cache_map = {
[C(L1D)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
	},
	[C(OP_WRITE)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
	},
	[C(OP_PREFETCH)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(L1I)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { 0x00, PIC_UPPER },
		[C(RESULT_MISS)] = { 0x02, PIC_LOWER, },
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
		[ C(RESULT_MISS)   ] = { CACHE_OP_NONSENSE },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(LL)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
	},
	[C(OP_WRITE)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
	},
	[C(OP_PREFETCH)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(DTLB)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { 0x05, PIC_LOWER, },
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(ITLB)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { 0x04, PIC_LOWER, },
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(BPU)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
441 442 443 444 445 446 447 448 449 450 451 452 453 454
[C(NODE)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)  ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
455 456 457 458 459 460
};

static const struct sparc_pmu niagara1_pmu = {
	.event_map	= niagara1_event_map,
	.cache_map	= &niagara1_cache_map,
	.max_events	= ARRAY_SIZE(niagara1_perfmon_event_map),
461 462
	.read_pmc	= sparc_default_read_pmc,
	.write_pmc	= sparc_default_write_pmc,
463 464 465
	.upper_shift	= 0,
	.lower_shift	= 4,
	.event_mask	= 0x7,
466 467
	.user_bit	= PCR_UTRACE,
	.priv_bit	= PCR_STRACE,
468 469
	.upper_nop	= 0x0,
	.lower_nop	= 0x0,
470 471
	.flags		= (SPARC_PMU_ALL_EXCLUDES_SAME |
			   SPARC_PMU_HAS_CONFLICTS),
472
	.max_hw_events	= 2,
473 474
	.num_pcrs	= 1,
	.num_pic_regs	= 1,
475 476
};

477 478 479 480 481 482 483 484 485
static const struct perf_event_map niagara2_perfmon_event_map[] = {
	[PERF_COUNT_HW_CPU_CYCLES] = { 0x02ff, PIC_UPPER | PIC_LOWER },
	[PERF_COUNT_HW_INSTRUCTIONS] = { 0x02ff, PIC_UPPER | PIC_LOWER },
	[PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0208, PIC_UPPER | PIC_LOWER },
	[PERF_COUNT_HW_CACHE_MISSES] = { 0x0302, PIC_UPPER | PIC_LOWER },
	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x0201, PIC_UPPER | PIC_LOWER },
	[PERF_COUNT_HW_BRANCH_MISSES] = { 0x0202, PIC_UPPER | PIC_LOWER },
};

486
static const struct perf_event_map *niagara2_event_map(int event_id)
487
{
488
	return &niagara2_perfmon_event_map[event_id];
489 490
}

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
static const cache_map_t niagara2_cache_map = {
[C(L1D)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
		[C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
	},
	[C(OP_WRITE)] = {
		[C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
		[C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
	},
	[C(OP_PREFETCH)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(L1I)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { 0x02ff, PIC_UPPER | PIC_LOWER, },
		[C(RESULT_MISS)] = { 0x0301, PIC_UPPER | PIC_LOWER, },
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
		[ C(RESULT_MISS)   ] = { CACHE_OP_NONSENSE },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(LL)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
		[C(RESULT_MISS)] = { 0x0330, PIC_UPPER | PIC_LOWER, },
	},
	[C(OP_WRITE)] = {
		[C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
		[C(RESULT_MISS)] = { 0x0320, PIC_UPPER | PIC_LOWER, },
	},
	[C(OP_PREFETCH)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(DTLB)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { 0x0b08, PIC_UPPER | PIC_LOWER, },
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(ITLB)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { 0xb04, PIC_UPPER | PIC_LOWER, },
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(BPU)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
569 570 571 572 573 574 575 576 577 578 579 580 581 582
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(NODE)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)  ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
583 584 585 586 587 588 589 590 591
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
};

592 593
static const struct sparc_pmu niagara2_pmu = {
	.event_map	= niagara2_event_map,
594
	.cache_map	= &niagara2_cache_map,
595
	.max_events	= ARRAY_SIZE(niagara2_perfmon_event_map),
596 597
	.read_pmc	= sparc_default_read_pmc,
	.write_pmc	= sparc_default_write_pmc,
598 599 600
	.upper_shift	= 19,
	.lower_shift	= 6,
	.event_mask	= 0xfff,
601 602 603
	.user_bit	= PCR_UTRACE,
	.priv_bit	= PCR_STRACE,
	.hv_bit		= PCR_N2_HTRACE,
604
	.irq_bit	= 0x30,
605 606
	.upper_nop	= 0x220,
	.lower_nop	= 0x220,
607 608
	.flags		= (SPARC_PMU_ALL_EXCLUDES_SAME |
			   SPARC_PMU_HAS_CONFLICTS),
609
	.max_hw_events	= 2,
610 611
	.num_pcrs	= 1,
	.num_pic_regs	= 1,
612 613
};

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
static const struct perf_event_map niagara4_perfmon_event_map[] = {
	[PERF_COUNT_HW_CPU_CYCLES] = { (26 << 6) },
	[PERF_COUNT_HW_INSTRUCTIONS] = { (3 << 6) | 0x3f },
	[PERF_COUNT_HW_CACHE_REFERENCES] = { (3 << 6) | 0x04 },
	[PERF_COUNT_HW_CACHE_MISSES] = { (16 << 6) | 0x07 },
	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { (4 << 6) | 0x01 },
	[PERF_COUNT_HW_BRANCH_MISSES] = { (25 << 6) | 0x0f },
};

static const struct perf_event_map *niagara4_event_map(int event_id)
{
	return &niagara4_perfmon_event_map[event_id];
}

static const cache_map_t niagara4_cache_map = {
[C(L1D)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { (3 << 6) | 0x04 },
		[C(RESULT_MISS)] = { (16 << 6) | 0x07 },
	},
	[C(OP_WRITE)] = {
		[C(RESULT_ACCESS)] = { (3 << 6) | 0x08 },
		[C(RESULT_MISS)] = { (16 << 6) | 0x07 },
	},
	[C(OP_PREFETCH)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(L1I)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { (3 << 6) | 0x3f },
		[C(RESULT_MISS)] = { (11 << 6) | 0x03 },
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
		[ C(RESULT_MISS)   ] = { CACHE_OP_NONSENSE },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(LL)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { (3 << 6) | 0x04 },
		[C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
	},
	[C(OP_WRITE)] = {
		[C(RESULT_ACCESS)] = { (3 << 6) | 0x08 },
		[C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
	},
	[C(OP_PREFETCH)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(DTLB)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { (17 << 6) | 0x3f },
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(ITLB)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { (6 << 6) | 0x3f },
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(BPU)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
[C(NODE)] = {
	[C(OP_READ)] = {
		[C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
		[C(RESULT_MISS)  ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
};

static u32 sparc_vt_read_pmc(int idx)
{
	u64 val = pcr_ops->read_pic(idx);

	return val & 0xffffffff;
}

static void sparc_vt_write_pmc(int idx, u64 val)
{
	u64 pcr;

	pcr = pcr_ops->read_pcr(idx);
741 742
	/* ensure ov and ntc are reset */
	pcr &= ~(PCR_N4_OV | PCR_N4_NTC);
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778

	pcr_ops->write_pic(idx, val & 0xffffffff);

	pcr_ops->write_pcr(idx, pcr);
}

static const struct sparc_pmu niagara4_pmu = {
	.event_map	= niagara4_event_map,
	.cache_map	= &niagara4_cache_map,
	.max_events	= ARRAY_SIZE(niagara4_perfmon_event_map),
	.read_pmc	= sparc_vt_read_pmc,
	.write_pmc	= sparc_vt_write_pmc,
	.upper_shift	= 5,
	.lower_shift	= 5,
	.event_mask	= 0x7ff,
	.user_bit	= PCR_N4_UTRACE,
	.priv_bit	= PCR_N4_STRACE,

	/* We explicitly don't support hypervisor tracing.  The T4
	 * generates the overflow event for precise events via a trap
	 * which will not be generated (ie. it's completely lost) if
	 * we happen to be in the hypervisor when the event triggers.
	 * Essentially, the overflow event reporting is completely
	 * unusable when you have hypervisor mode tracing enabled.
	 */
	.hv_bit		= 0,

	.irq_bit	= PCR_N4_TOE,
	.upper_nop	= 0,
	.lower_nop	= 0,
	.flags		= 0,
	.max_hw_events	= 4,
	.num_pcrs	= 4,
	.num_pic_regs	= 4,
};

779 780 781 782 783
static const struct sparc_pmu sparc_m7_pmu = {
	.event_map	= niagara4_event_map,
	.cache_map	= &niagara4_cache_map,
	.max_events	= ARRAY_SIZE(niagara4_perfmon_event_map),
	.read_pmc	= sparc_vt_read_pmc,
784
	.write_pmc	= sparc_vt_write_pmc,
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
	.upper_shift	= 5,
	.lower_shift	= 5,
	.event_mask	= 0x7ff,
	.user_bit	= PCR_N4_UTRACE,
	.priv_bit	= PCR_N4_STRACE,

	/* We explicitly don't support hypervisor tracing. */
	.hv_bit		= 0,

	.irq_bit	= PCR_N4_TOE,
	.upper_nop	= 0,
	.lower_nop	= 0,
	.flags		= 0,
	.max_hw_events	= 4,
	.num_pcrs	= 4,
	.num_pic_regs	= 4,
};
802 803
static const struct sparc_pmu *sparc_pmu __read_mostly;

804
static u64 event_encoding(u64 event_id, int idx)
805 806
{
	if (idx == PIC_UPPER_INDEX)
807
		event_id <<= sparc_pmu->upper_shift;
808
	else
809 810
		event_id <<= sparc_pmu->lower_shift;
	return event_id;
811 812 813 814 815 816 817 818 819 820
}

static u64 mask_for_index(int idx)
{
	return event_encoding(sparc_pmu->event_mask, idx);
}

static u64 nop_for_index(int idx)
{
	return event_encoding(idx == PIC_UPPER_INDEX ?
821 822
			      sparc_pmu->upper_nop :
			      sparc_pmu->lower_nop, idx);
823 824
}

825
static inline void sparc_pmu_enable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
826
{
827
	u64 enc, val, mask = mask_for_index(idx);
828
	int pcr_index = 0;
829

830 831 832
	if (sparc_pmu->num_pcrs > 1)
		pcr_index = idx;

833 834
	enc = perf_event_get_enc(cpuc->events[idx]);

835
	val = cpuc->pcr[pcr_index];
836
	val &= ~mask;
837
	val |= event_encoding(enc, idx);
838
	cpuc->pcr[pcr_index] = val;
839

840
	pcr_ops->write_pcr(pcr_index, cpuc->pcr[pcr_index]);
841 842
}

843
static inline void sparc_pmu_disable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
844 845 846
{
	u64 mask = mask_for_index(idx);
	u64 nop = nop_for_index(idx);
847
	int pcr_index = 0;
848
	u64 val;
849

850 851 852 853
	if (sparc_pmu->num_pcrs > 1)
		pcr_index = idx;

	val = cpuc->pcr[pcr_index];
854 855
	val &= ~mask;
	val |= nop;
856
	cpuc->pcr[pcr_index] = val;
857

858
	pcr_ops->write_pcr(pcr_index, cpuc->pcr[pcr_index]);
859 860
}

861 862 863 864 865 866 867 868
static u64 sparc_perf_event_update(struct perf_event *event,
				   struct hw_perf_event *hwc, int idx)
{
	int shift = 64 - 32;
	u64 prev_raw_count, new_raw_count;
	s64 delta;

again:
869
	prev_raw_count = local64_read(&hwc->prev_count);
870
	new_raw_count = sparc_pmu->read_pmc(idx);
871

872
	if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
873 874 875 876 877 878
			     new_raw_count) != prev_raw_count)
		goto again;

	delta = (new_raw_count << shift) - (prev_raw_count << shift);
	delta >>= shift;

879 880
	local64_add(delta, &event->count);
	local64_sub(delta, &hwc->period_left);
881 882 883 884

	return new_raw_count;
}

885
static int sparc_perf_event_set_period(struct perf_event *event,
886
				       struct hw_perf_event *hwc, int idx)
887
{
888
	s64 left = local64_read(&hwc->period_left);
889 890 891 892 893
	s64 period = hwc->sample_period;
	int ret = 0;

	if (unlikely(left <= -period)) {
		left = period;
894
		local64_set(&hwc->period_left, left);
895 896 897 898 899 900
		hwc->last_period = period;
		ret = 1;
	}

	if (unlikely(left <= 0)) {
		left += period;
901
		local64_set(&hwc->period_left, left);
902 903 904 905 906 907
		hwc->last_period = period;
		ret = 1;
	}
	if (left > MAX_PERIOD)
		left = MAX_PERIOD;

908
	local64_set(&hwc->prev_count, (u64)-left);
909

910
	sparc_pmu->write_pmc(idx, (u64)(-left) & 0xffffffff);
911

912
	perf_event_update_userpage(event);
913 914 915 916

	return ret;
}

917
static void read_in_all_counters(struct cpu_hw_events *cpuc)
918
{
919
	int i;
920

921 922
	for (i = 0; i < cpuc->n_events; i++) {
		struct perf_event *cp = cpuc->event[i];
923

924 925 926 927 928 929 930
		if (cpuc->current_idx[i] != PIC_NO_INDEX &&
		    cpuc->current_idx[i] != cp->hw.idx) {
			sparc_perf_event_update(cp, &cp->hw,
						cpuc->current_idx[i]);
			cpuc->current_idx[i] = PIC_NO_INDEX;
		}
	}
931 932 933 934 935 936 937 938 939 940 941 942 943 944
}

/* On this PMU all PICs are programmed using a single PCR.  Calculate
 * the combined control register value.
 *
 * For such chips we require that all of the events have the same
 * configuration, so just fetch the settings from the first entry.
 */
static void calculate_single_pcr(struct cpu_hw_events *cpuc)
{
	int i;

	if (!cpuc->n_added)
		goto out;
945

946 947 948 949 950 951 952 953 954 955 956 957 958 959
	/* Assign to counters all unassigned events.  */
	for (i = 0; i < cpuc->n_events; i++) {
		struct perf_event *cp = cpuc->event[i];
		struct hw_perf_event *hwc = &cp->hw;
		int idx = hwc->idx;
		u64 enc;

		if (cpuc->current_idx[i] != PIC_NO_INDEX)
			continue;

		sparc_perf_event_set_period(cp, hwc, idx);
		cpuc->current_idx[i] = idx;

		enc = perf_event_get_enc(cpuc->events[i]);
960
		cpuc->pcr[0] &= ~mask_for_index(idx);
P
Peter Zijlstra 已提交
961
		if (hwc->state & PERF_HES_STOPPED)
962
			cpuc->pcr[0] |= nop_for_index(idx);
P
Peter Zijlstra 已提交
963
		else
964
			cpuc->pcr[0] |= event_encoding(enc, idx);
965 966
	}
out:
967 968 969
	cpuc->pcr[0] |= cpuc->event[0]->hw.config_base;
}

970 971
static void sparc_pmu_start(struct perf_event *event, int flags);

972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
/* On this PMU each PIC has it's own PCR control register.  */
static void calculate_multiple_pcrs(struct cpu_hw_events *cpuc)
{
	int i;

	if (!cpuc->n_added)
		goto out;

	for (i = 0; i < cpuc->n_events; i++) {
		struct perf_event *cp = cpuc->event[i];
		struct hw_perf_event *hwc = &cp->hw;
		int idx = hwc->idx;

		if (cpuc->current_idx[i] != PIC_NO_INDEX)
			continue;

		cpuc->current_idx[i] = idx;

990
		sparc_pmu_start(cp, PERF_EF_RELOAD);
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
	}
out:
	for (i = 0; i < cpuc->n_events; i++) {
		struct perf_event *cp = cpuc->event[i];
		int idx = cp->hw.idx;

		cpuc->pcr[idx] |= cp->hw.config_base;
	}
}

/* If performance event entries have been added, move existing events
 * around (if necessary) and then assign new entries to counters.
 */
static void update_pcrs_for_enable(struct cpu_hw_events *cpuc)
{
	if (cpuc->n_added)
		read_in_all_counters(cpuc);

	if (sparc_pmu->num_pcrs == 1) {
		calculate_single_pcr(cpuc);
	} else {
		calculate_multiple_pcrs(cpuc);
	}
1014 1015
}

P
Peter Zijlstra 已提交
1016
static void sparc_pmu_enable(struct pmu *pmu)
1017
{
1018
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1019
	int i;
1020

1021 1022
	if (cpuc->enabled)
		return;
1023

1024 1025
	cpuc->enabled = 1;
	barrier();
1026

1027 1028
	if (cpuc->n_events)
		update_pcrs_for_enable(cpuc);
1029

1030 1031
	for (i = 0; i < sparc_pmu->num_pcrs; i++)
		pcr_ops->write_pcr(i, cpuc->pcr[i]);
1032 1033
}

P
Peter Zijlstra 已提交
1034
static void sparc_pmu_disable(struct pmu *pmu)
1035
{
1036
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1037
	int i;
1038 1039 1040 1041 1042 1043 1044

	if (!cpuc->enabled)
		return;

	cpuc->enabled = 0;
	cpuc->n_added = 0;

1045 1046
	for (i = 0; i < sparc_pmu->num_pcrs; i++) {
		u64 val = cpuc->pcr[i];
1047

1048 1049 1050 1051 1052
		val &= ~(sparc_pmu->user_bit | sparc_pmu->priv_bit |
			 sparc_pmu->hv_bit | sparc_pmu->irq_bit);
		cpuc->pcr[i] = val;
		pcr_ops->write_pcr(i, cpuc->pcr[i]);
	}
1053 1054
}

P
Peter Zijlstra 已提交
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
static int active_event_index(struct cpu_hw_events *cpuc,
			      struct perf_event *event)
{
	int i;

	for (i = 0; i < cpuc->n_events; i++) {
		if (cpuc->event[i] == event)
			break;
	}
	BUG_ON(i == cpuc->n_events);
	return cpuc->current_idx[i];
}

static void sparc_pmu_start(struct perf_event *event, int flags)
{
1070
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
P
Peter Zijlstra 已提交
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
	int idx = active_event_index(cpuc, event);

	if (flags & PERF_EF_RELOAD) {
		WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
		sparc_perf_event_set_period(event, &event->hw, idx);
	}

	event->hw.state = 0;

	sparc_pmu_enable_event(cpuc, &event->hw, idx);
}

static void sparc_pmu_stop(struct perf_event *event, int flags)
{
1085
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
P
Peter Zijlstra 已提交
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
	int idx = active_event_index(cpuc, event);

	if (!(event->hw.state & PERF_HES_STOPPED)) {
		sparc_pmu_disable_event(cpuc, &event->hw, idx);
		event->hw.state |= PERF_HES_STOPPED;
	}

	if (!(event->hw.state & PERF_HES_UPTODATE) && (flags & PERF_EF_UPDATE)) {
		sparc_perf_event_update(event, &event->hw, idx);
		event->hw.state |= PERF_HES_UPTODATE;
	}
}

static void sparc_pmu_del(struct perf_event *event, int _flags)
1100
{
1101
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1102 1103
	unsigned long flags;
	int i;
1104

1105 1106 1107 1108
	local_irq_save(flags);

	for (i = 0; i < cpuc->n_events; i++) {
		if (event == cpuc->event[i]) {
P
Peter Zijlstra 已提交
1109 1110 1111 1112
			/* Absorb the final count and turn off the
			 * event.
			 */
			sparc_pmu_stop(event, PERF_EF_UPDATE);
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124

			/* Shift remaining entries down into
			 * the existing slot.
			 */
			while (++i < cpuc->n_events) {
				cpuc->event[i - 1] = cpuc->event[i];
				cpuc->events[i - 1] = cpuc->events[i];
				cpuc->current_idx[i - 1] =
					cpuc->current_idx[i];
			}

			perf_event_update_userpage(event);
1125

1126 1127 1128 1129
			cpuc->n_events--;
			break;
		}
	}
1130

1131 1132 1133
	local_irq_restore(flags);
}

1134
static void sparc_pmu_read(struct perf_event *event)
1135
{
1136
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1137
	int idx = active_event_index(cpuc, event);
1138
	struct hw_perf_event *hwc = &event->hw;
1139

1140
	sparc_perf_event_update(event, hwc, idx);
1141 1142
}

1143
static atomic_t active_events = ATOMIC_INIT(0);
1144 1145
static DEFINE_MUTEX(pmc_grab_mutex);

1146 1147
static void perf_stop_nmi_watchdog(void *unused)
{
1148
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1149
	int i;
1150 1151

	stop_nmi_watchdog(NULL);
1152 1153
	for (i = 0; i < sparc_pmu->num_pcrs; i++)
		cpuc->pcr[i] = pcr_ops->read_pcr(i);
1154 1155
}

1156
static void perf_event_grab_pmc(void)
1157
{
1158
	if (atomic_inc_not_zero(&active_events))
1159 1160 1161
		return;

	mutex_lock(&pmc_grab_mutex);
1162
	if (atomic_read(&active_events) == 0) {
1163
		if (atomic_read(&nmi_active) > 0) {
1164
			on_each_cpu(perf_stop_nmi_watchdog, NULL, 1);
1165 1166
			BUG_ON(atomic_read(&nmi_active) != 0);
		}
1167
		atomic_inc(&active_events);
1168 1169 1170 1171
	}
	mutex_unlock(&pmc_grab_mutex);
}

1172
static void perf_event_release_pmc(void)
1173
{
1174
	if (atomic_dec_and_mutex_lock(&active_events, &pmc_grab_mutex)) {
1175 1176 1177 1178 1179 1180
		if (atomic_read(&nmi_active) == 0)
			on_each_cpu(start_nmi_watchdog, NULL, 1);
		mutex_unlock(&pmc_grab_mutex);
	}
}

1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
static const struct perf_event_map *sparc_map_cache_event(u64 config)
{
	unsigned int cache_type, cache_op, cache_result;
	const struct perf_event_map *pmap;

	if (!sparc_pmu->cache_map)
		return ERR_PTR(-ENOENT);

	cache_type = (config >>  0) & 0xff;
	if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
		return ERR_PTR(-EINVAL);

	cache_op = (config >>  8) & 0xff;
	if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
		return ERR_PTR(-EINVAL);

	cache_result = (config >> 16) & 0xff;
	if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
		return ERR_PTR(-EINVAL);

	pmap = &((*sparc_pmu->cache_map)[cache_type][cache_op][cache_result]);

	if (pmap->encoding == CACHE_OP_UNSUPPORTED)
		return ERR_PTR(-ENOENT);

	if (pmap->encoding == CACHE_OP_NONSENSE)
		return ERR_PTR(-EINVAL);

	return pmap;
}

1212
static void hw_perf_event_destroy(struct perf_event *event)
1213
{
1214
	perf_event_release_pmc();
1215 1216
}

1217 1218 1219
/* Make sure all events can be scheduled into the hardware at
 * the same time.  This is simplified by the fact that we only
 * need to support 2 simultaneous HW events.
1220 1221 1222 1223 1224 1225
 *
 * As a side effect, the evts[]->hw.idx values will be assigned
 * on success.  These are pending indexes.  When the events are
 * actually programmed into the chip, these values will propagate
 * to the per-cpu cpuc->current_idx[] slots, see the code in
 * maybe_change_configuration() for details.
1226
 */
1227 1228
static int sparc_check_constraints(struct perf_event **evts,
				   unsigned long *events, int n_ev)
1229
{
1230 1231 1232 1233 1234 1235 1236 1237 1238
	u8 msk0 = 0, msk1 = 0;
	int idx0 = 0;

	/* This case is possible when we are invoked from
	 * hw_perf_group_sched_in().
	 */
	if (!n_ev)
		return 0;

1239
	if (n_ev > sparc_pmu->max_hw_events)
1240 1241
		return -1;

1242 1243 1244 1245 1246 1247 1248 1249
	if (!(sparc_pmu->flags & SPARC_PMU_HAS_CONFLICTS)) {
		int i;

		for (i = 0; i < n_ev; i++)
			evts[i]->hw.idx = i;
		return 0;
	}

1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
	msk0 = perf_event_get_msk(events[0]);
	if (n_ev == 1) {
		if (msk0 & PIC_LOWER)
			idx0 = 1;
		goto success;
	}
	BUG_ON(n_ev != 2);
	msk1 = perf_event_get_msk(events[1]);

	/* If both events can go on any counter, OK.  */
	if (msk0 == (PIC_UPPER | PIC_LOWER) &&
	    msk1 == (PIC_UPPER | PIC_LOWER))
		goto success;

	/* If one event is limited to a specific counter,
	 * and the other can go on both, OK.
	 */
	if ((msk0 == PIC_UPPER || msk0 == PIC_LOWER) &&
	    msk1 == (PIC_UPPER | PIC_LOWER)) {
		if (msk0 & PIC_LOWER)
			idx0 = 1;
		goto success;
1272 1273
	}

1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
	if ((msk1 == PIC_UPPER || msk1 == PIC_LOWER) &&
	    msk0 == (PIC_UPPER | PIC_LOWER)) {
		if (msk1 & PIC_UPPER)
			idx0 = 1;
		goto success;
	}

	/* If the events are fixed to different counters, OK.  */
	if ((msk0 == PIC_UPPER && msk1 == PIC_LOWER) ||
	    (msk0 == PIC_LOWER && msk1 == PIC_UPPER)) {
		if (msk0 & PIC_LOWER)
			idx0 = 1;
		goto success;
	}

	/* Otherwise, there is a conflict.  */
1290
	return -1;
1291 1292 1293 1294 1295 1296

success:
	evts[0]->hw.idx = idx0;
	if (n_ev == 2)
		evts[1]->hw.idx = idx0 ^ 1;
	return 0;
1297 1298
}

1299 1300 1301 1302 1303 1304
static int check_excludes(struct perf_event **evts, int n_prev, int n_new)
{
	int eu = 0, ek = 0, eh = 0;
	struct perf_event *event;
	int i, n, first;

1305 1306 1307
	if (!(sparc_pmu->flags & SPARC_PMU_ALL_EXCLUDES_SAME))
		return 0;

1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
	n = n_prev + n_new;
	if (n <= 1)
		return 0;

	first = 1;
	for (i = 0; i < n; i++) {
		event = evts[i];
		if (first) {
			eu = event->attr.exclude_user;
			ek = event->attr.exclude_kernel;
			eh = event->attr.exclude_hv;
			first = 0;
		} else if (event->attr.exclude_user != eu ||
			   event->attr.exclude_kernel != ek ||
			   event->attr.exclude_hv != eh) {
			return -EAGAIN;
		}
	}

	return 0;
}

static int collect_events(struct perf_event *group, int max_count,
1331 1332
			  struct perf_event *evts[], unsigned long *events,
			  int *current_idx)
1333 1334 1335 1336 1337 1338 1339 1340
{
	struct perf_event *event;
	int n = 0;

	if (!is_software_event(group)) {
		if (n >= max_count)
			return -1;
		evts[n] = group;
1341 1342
		events[n] = group->hw.event_base;
		current_idx[n++] = PIC_NO_INDEX;
1343 1344 1345 1346 1347 1348 1349
	}
	list_for_each_entry(event, &group->sibling_list, group_entry) {
		if (!is_software_event(event) &&
		    event->state != PERF_EVENT_STATE_OFF) {
			if (n >= max_count)
				return -1;
			evts[n] = event;
1350 1351
			events[n] = event->hw.event_base;
			current_idx[n++] = PIC_NO_INDEX;
1352 1353 1354 1355 1356
		}
	}
	return n;
}

P
Peter Zijlstra 已提交
1357
static int sparc_pmu_add(struct perf_event *event, int ef_flags)
1358
{
1359
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1360 1361 1362 1363 1364 1365
	int n0, ret = -EAGAIN;
	unsigned long flags;

	local_irq_save(flags);

	n0 = cpuc->n_events;
1366
	if (n0 >= sparc_pmu->max_hw_events)
1367 1368 1369 1370 1371 1372
		goto out;

	cpuc->event[n0] = event;
	cpuc->events[n0] = event->hw.event_base;
	cpuc->current_idx[n0] = PIC_NO_INDEX;

P
Peter Zijlstra 已提交
1373 1374 1375 1376
	event->hw.state = PERF_HES_UPTODATE;
	if (!(ef_flags & PERF_EF_START))
		event->hw.state |= PERF_HES_STOPPED;

1377 1378
	/*
	 * If group events scheduling transaction was started,
L
Lucas De Marchi 已提交
1379
	 * skip the schedulability test here, it will be performed
1380 1381
	 * at commit time(->commit_txn) as a whole
	 */
1382
	if (cpuc->txn_flags & PERF_PMU_TXN_ADD)
1383 1384
		goto nocheck;

1385 1386 1387 1388 1389
	if (check_excludes(cpuc->event, n0, 1))
		goto out;
	if (sparc_check_constraints(cpuc->event, cpuc->events, n0 + 1))
		goto out;

1390
nocheck:
1391 1392 1393 1394 1395 1396 1397 1398 1399
	cpuc->n_events++;
	cpuc->n_added++;

	ret = 0;
out:
	local_irq_restore(flags);
	return ret;
}

1400
static int sparc_pmu_event_init(struct perf_event *event)
1401
{
1402
	struct perf_event_attr *attr = &event->attr;
1403
	struct perf_event *evts[MAX_HWEVENTS];
1404
	struct hw_perf_event *hwc = &event->hw;
1405
	unsigned long events[MAX_HWEVENTS];
1406
	int current_idx_dmy[MAX_HWEVENTS];
1407
	const struct perf_event_map *pmap;
1408
	int n;
1409 1410 1411 1412

	if (atomic_read(&nmi_active) < 0)
		return -ENODEV;

1413 1414 1415 1416
	/* does not support taken branch sampling */
	if (has_branch_stack(event))
		return -EOPNOTSUPP;

1417 1418
	switch (attr->type) {
	case PERF_TYPE_HARDWARE:
1419 1420 1421
		if (attr->config >= sparc_pmu->max_events)
			return -EINVAL;
		pmap = sparc_pmu->event_map(attr->config);
1422 1423 1424
		break;

	case PERF_TYPE_HW_CACHE:
1425 1426 1427
		pmap = sparc_map_cache_event(attr->config);
		if (IS_ERR(pmap))
			return PTR_ERR(pmap);
1428 1429 1430
		break;

	case PERF_TYPE_RAW:
1431 1432
		pmap = NULL;
		break;
1433

1434 1435 1436 1437 1438
	default:
		return -ENOENT;

	}

1439 1440 1441
	if (pmap) {
		hwc->event_base = perf_event_encode(pmap);
	} else {
1442 1443
		/*
		 * User gives us "(encoding << 16) | pic_mask" for
1444 1445 1446 1447 1448
		 * PERF_TYPE_RAW events.
		 */
		hwc->event_base = attr->config;
	}

1449
	/* We save the enable bits in the config_base.  */
1450
	hwc->config_base = sparc_pmu->irq_bit;
1451
	if (!attr->exclude_user)
1452
		hwc->config_base |= sparc_pmu->user_bit;
1453
	if (!attr->exclude_kernel)
1454
		hwc->config_base |= sparc_pmu->priv_bit;
1455 1456
	if (!attr->exclude_hv)
		hwc->config_base |= sparc_pmu->hv_bit;
1457

1458 1459 1460
	n = 0;
	if (event->group_leader != event) {
		n = collect_events(event->group_leader,
1461
				   sparc_pmu->max_hw_events - 1,
1462
				   evts, events, current_idx_dmy);
1463 1464 1465
		if (n < 0)
			return -EINVAL;
	}
1466
	events[n] = hwc->event_base;
1467 1468 1469 1470 1471
	evts[n] = event;

	if (check_excludes(evts, n, 1))
		return -EINVAL;

1472
	if (sparc_check_constraints(evts, events, n + 1))
1473 1474
		return -EINVAL;

1475 1476
	hwc->idx = PIC_NO_INDEX;

1477 1478 1479 1480 1481 1482
	/* Try to do all error checking before this point, as unwinding
	 * state after grabbing the PMC is difficult.
	 */
	perf_event_grab_pmc();
	event->destroy = hw_perf_event_destroy;

1483 1484 1485
	if (!hwc->sample_period) {
		hwc->sample_period = MAX_PERIOD;
		hwc->last_period = hwc->sample_period;
1486
		local64_set(&hwc->period_left, hwc->sample_period);
1487 1488 1489 1490 1491
	}

	return 0;
}

1492 1493 1494 1495 1496
/*
 * Start group events scheduling transaction
 * Set the flag to make pmu::enable() not perform the
 * schedulability test, it will be performed at commit time
 */
1497
static void sparc_pmu_start_txn(struct pmu *pmu, unsigned int txn_flags)
1498
{
1499
	struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
1500

1501 1502 1503 1504 1505 1506
	WARN_ON_ONCE(cpuhw->txn_flags);		/* txn already in flight */

	cpuhw->txn_flags = txn_flags;
	if (txn_flags & ~PERF_PMU_TXN_ADD)
		return;

P
Peter Zijlstra 已提交
1507
	perf_pmu_disable(pmu);
1508 1509 1510 1511 1512 1513 1514
}

/*
 * Stop group events scheduling transaction
 * Clear the flag and pmu::enable() will perform the
 * schedulability test.
 */
P
Peter Zijlstra 已提交
1515
static void sparc_pmu_cancel_txn(struct pmu *pmu)
1516
{
1517
	struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
1518 1519 1520 1521 1522 1523 1524 1525
	unsigned int txn_flags;

	WARN_ON_ONCE(!cpuhw->txn_flags);	/* no txn in flight */

	txn_flags = cpuhw->txn_flags;
	cpuhw->txn_flags = 0;
	if (txn_flags & ~PERF_PMU_TXN_ADD)
		return;
1526

P
Peter Zijlstra 已提交
1527
	perf_pmu_enable(pmu);
1528 1529 1530 1531 1532 1533 1534
}

/*
 * Commit group events scheduling transaction
 * Perform the group schedulability test as a whole
 * Return 0 if success
 */
P
Peter Zijlstra 已提交
1535
static int sparc_pmu_commit_txn(struct pmu *pmu)
1536
{
1537
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1538 1539 1540 1541 1542
	int n;

	if (!sparc_pmu)
		return -EINVAL;

1543 1544 1545 1546 1547 1548 1549
	WARN_ON_ONCE(!cpuc->txn_flags);	/* no txn in flight */

	if (cpuc->txn_flags & ~PERF_PMU_TXN_ADD) {
		cpuc->txn_flags = 0;
		return 0;
	}

1550 1551 1552 1553 1554 1555
	n = cpuc->n_events;
	if (check_excludes(cpuc->event, 0, n))
		return -EINVAL;
	if (sparc_check_constraints(cpuc->event, cpuc->events, n))
		return -EAGAIN;

1556
	cpuc->txn_flags = 0;
P
Peter Zijlstra 已提交
1557
	perf_pmu_enable(pmu);
1558 1559 1560
	return 0;
}

P
Peter Zijlstra 已提交
1561
static struct pmu pmu = {
P
Peter Zijlstra 已提交
1562 1563
	.pmu_enable	= sparc_pmu_enable,
	.pmu_disable	= sparc_pmu_disable,
1564
	.event_init	= sparc_pmu_event_init,
P
Peter Zijlstra 已提交
1565 1566 1567 1568
	.add		= sparc_pmu_add,
	.del		= sparc_pmu_del,
	.start		= sparc_pmu_start,
	.stop		= sparc_pmu_stop,
1569
	.read		= sparc_pmu_read,
1570 1571 1572
	.start_txn	= sparc_pmu_start_txn,
	.cancel_txn	= sparc_pmu_cancel_txn,
	.commit_txn	= sparc_pmu_commit_txn,
1573 1574
};

1575
void perf_event_print_debug(void)
1576 1577
{
	unsigned long flags;
1578
	int cpu, i;
1579 1580 1581 1582 1583 1584 1585 1586 1587

	if (!sparc_pmu)
		return;

	local_irq_save(flags);

	cpu = smp_processor_id();

	pr_info("\n");
1588 1589 1590 1591 1592 1593
	for (i = 0; i < sparc_pmu->num_pcrs; i++)
		pr_info("CPU#%d: PCR%d[%016llx]\n",
			cpu, i, pcr_ops->read_pcr(i));
	for (i = 0; i < sparc_pmu->num_pic_regs; i++)
		pr_info("CPU#%d: PIC%d[%016llx]\n",
			cpu, i, pcr_ops->read_pic(i));
1594 1595 1596 1597

	local_irq_restore(flags);
}

1598
static int __kprobes perf_event_nmi_handler(struct notifier_block *self,
1599
					    unsigned long cmd, void *__args)
1600 1601 1602
{
	struct die_args *args = __args;
	struct perf_sample_data data;
1603
	struct cpu_hw_events *cpuc;
1604
	struct pt_regs *regs;
1605
	int i;
1606

1607
	if (!atomic_read(&active_events))
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
		return NOTIFY_DONE;

	switch (cmd) {
	case DIE_NMI:
		break;

	default:
		return NOTIFY_DONE;
	}

	regs = args->regs;

1620
	cpuc = this_cpu_ptr(&cpu_hw_events);
1621 1622 1623 1624 1625 1626 1627 1628

	/* If the PMU has the TOE IRQ enable bits, we need to do a
	 * dummy write to the %pcr to clear the overflow bits and thus
	 * the interrupt.
	 *
	 * Do this before we peek at the counters to determine
	 * overflow so we don't lose any events.
	 */
1629 1630 1631
	if (sparc_pmu->irq_bit &&
	    sparc_pmu->num_pcrs == 1)
		pcr_ops->write_pcr(0, cpuc->pcr[0]);
1632

1633 1634 1635
	for (i = 0; i < cpuc->n_events; i++) {
		struct perf_event *event = cpuc->event[i];
		int idx = cpuc->current_idx[i];
1636
		struct hw_perf_event *hwc;
1637 1638
		u64 val;

1639 1640 1641 1642
		if (sparc_pmu->irq_bit &&
		    sparc_pmu->num_pcrs > 1)
			pcr_ops->write_pcr(idx, cpuc->pcr[idx]);

1643 1644
		hwc = &event->hw;
		val = sparc_perf_event_update(event, hwc, idx);
1645 1646 1647
		if (val & (1ULL << 31))
			continue;

1648
		perf_sample_data_init(&data, 0, hwc->last_period);
1649
		if (!sparc_perf_event_set_period(event, hwc, idx))
1650 1651
			continue;

1652
		if (perf_event_overflow(event, &data, regs))
P
Peter Zijlstra 已提交
1653
			sparc_pmu_stop(event, 0);
1654 1655 1656 1657 1658
	}

	return NOTIFY_STOP;
}

1659 1660
static __read_mostly struct notifier_block perf_event_nmi_notifier = {
	.notifier_call		= perf_event_nmi_handler,
1661 1662 1663 1664
};

static bool __init supported_pmu(void)
{
1665 1666 1667 1668 1669
	if (!strcmp(sparc_pmu_type, "ultra3") ||
	    !strcmp(sparc_pmu_type, "ultra3+") ||
	    !strcmp(sparc_pmu_type, "ultra3i") ||
	    !strcmp(sparc_pmu_type, "ultra4+")) {
		sparc_pmu = &ultra3_pmu;
1670 1671
		return true;
	}
1672 1673 1674 1675
	if (!strcmp(sparc_pmu_type, "niagara")) {
		sparc_pmu = &niagara1_pmu;
		return true;
	}
1676 1677
	if (!strcmp(sparc_pmu_type, "niagara2") ||
	    !strcmp(sparc_pmu_type, "niagara3")) {
1678 1679 1680
		sparc_pmu = &niagara2_pmu;
		return true;
	}
B
bob picco 已提交
1681 1682
	if (!strcmp(sparc_pmu_type, "niagara4") ||
	    !strcmp(sparc_pmu_type, "niagara5")) {
1683 1684 1685
		sparc_pmu = &niagara4_pmu;
		return true;
	}
1686 1687 1688 1689
	if (!strcmp(sparc_pmu_type, "sparc-m7")) {
		sparc_pmu = &sparc_m7_pmu;
		return true;
	}
1690 1691 1692
	return false;
}

1693
static int __init init_hw_perf_events(void)
1694
{
1695 1696
	int err;

1697
	pr_info("Performance events: ");
1698

1699 1700
	err = pcr_arch_init();
	if (err || !supported_pmu()) {
1701
		pr_cont("No support for PMU type '%s'\n", sparc_pmu_type);
1702
		return 0;
1703 1704 1705 1706
	}

	pr_cont("Supported PMU type is '%s'\n", sparc_pmu_type);

P
Peter Zijlstra 已提交
1707
	perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
1708
	register_die_notifier(&perf_event_nmi_notifier);
1709 1710

	return 0;
1711
}
1712
pure_initcall(init_hw_perf_events);
1713

1714 1715
void perf_callchain_kernel(struct perf_callchain_entry *entry,
			   struct pt_regs *regs)
1716 1717
{
	unsigned long ksp, fp;
1718 1719 1720
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
	int graph = 0;
#endif
1721

1722 1723
	stack_trace_flush();

1724
	perf_callchain_store(entry, regs->tpc);
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747

	ksp = regs->u_regs[UREG_I6];
	fp = ksp + STACK_BIAS;
	do {
		struct sparc_stackf *sf;
		struct pt_regs *regs;
		unsigned long pc;

		if (!kstack_valid(current_thread_info(), fp))
			break;

		sf = (struct sparc_stackf *) fp;
		regs = (struct pt_regs *) (sf + 1);

		if (kstack_is_trap_frame(current_thread_info(), regs)) {
			if (user_mode(regs))
				break;
			pc = regs->tpc;
			fp = regs->u_regs[UREG_I6] + STACK_BIAS;
		} else {
			pc = sf->callers_pc;
			fp = (unsigned long)sf->fp + STACK_BIAS;
		}
1748
		perf_callchain_store(entry, pc);
1749 1750 1751 1752 1753
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
		if ((pc + 8UL) == (unsigned long) &return_to_handler) {
			int index = current->curr_ret_stack;
			if (current->ret_stack && index >= graph) {
				pc = current->ret_stack[index - graph].ret;
1754
				perf_callchain_store(entry, pc);
1755 1756 1757 1758
				graph++;
			}
		}
#endif
1759
	} while (entry->nr < sysctl_perf_event_max_stack);
1760 1761
}

1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
static inline int
valid_user_frame(const void __user *fp, unsigned long size)
{
	/* addresses should be at least 4-byte aligned */
	if (((unsigned long) fp) & 3)
		return 0;

	return (__range_not_ok(fp, size, TASK_SIZE) == 0);
}

1772 1773
static void perf_callchain_user_64(struct perf_callchain_entry *entry,
				   struct pt_regs *regs)
1774 1775 1776
{
	unsigned long ufp;

1777
	ufp = regs->u_regs[UREG_FP] + STACK_BIAS;
1778
	do {
1779 1780
		struct sparc_stackf __user *usf;
		struct sparc_stackf sf;
1781 1782
		unsigned long pc;

1783
		usf = (struct sparc_stackf __user *)ufp;
1784 1785 1786
		if (!valid_user_frame(usf, sizeof(sf)))
			break;

1787 1788 1789 1790 1791
		if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
			break;

		pc = sf.callers_pc;
		ufp = (unsigned long)sf.fp + STACK_BIAS;
1792
		perf_callchain_store(entry, pc);
1793
	} while (entry->nr < sysctl_perf_event_max_stack);
1794 1795
}

1796 1797
static void perf_callchain_user_32(struct perf_callchain_entry *entry,
				   struct pt_regs *regs)
1798 1799 1800
{
	unsigned long ufp;

1801
	ufp = regs->u_regs[UREG_FP] & 0xffffffffUL;
1802 1803 1804
	do {
		unsigned long pc;

1805
		if (thread32_stack_is_64bit(ufp)) {
1806 1807
			struct sparc_stackf __user *usf;
			struct sparc_stackf sf;
1808

1809
			ufp += STACK_BIAS;
1810
			usf = (struct sparc_stackf __user *)ufp;
1811 1812 1813 1814 1815
			if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
				break;
			pc = sf.callers_pc & 0xffffffff;
			ufp = ((unsigned long) sf.fp) & 0xffffffff;
		} else {
1816 1817 1818
			struct sparc_stackf32 __user *usf;
			struct sparc_stackf32 sf;
			usf = (struct sparc_stackf32 __user *)ufp;
1819 1820 1821 1822 1823
			if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
				break;
			pc = sf.callers_pc;
			ufp = (unsigned long)sf.fp;
		}
1824
		perf_callchain_store(entry, pc);
1825
	} while (entry->nr < sysctl_perf_event_max_stack);
1826 1827
}

1828 1829
void
perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
1830
{
1831 1832
	u64 saved_fault_address = current_thread_info()->fault_address;
	u8 saved_fault_code = get_thread_fault_code();
1833 1834
	mm_segment_t old_fs;

1835 1836 1837 1838 1839
	perf_callchain_store(entry, regs->tpc);

	if (!current->mm)
		return;

1840 1841 1842
	old_fs = get_fs();
	set_fs(USER_DS);

1843
	flushw_user();
1844 1845 1846

	pagefault_disable();

1847 1848 1849 1850
	if (test_thread_flag(TIF_32BIT))
		perf_callchain_user_32(entry, regs);
	else
		perf_callchain_user_64(entry, regs);
1851 1852

	pagefault_enable();
1853 1854

	set_fs(old_fs);
1855 1856
	set_thread_fault_code(saved_fault_code);
	current_thread_info()->fault_address = saved_fault_address;
1857
}