perf_event.c 37.4 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 12 13 14
 * 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
 *  Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
 */

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 <asm/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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/* Sparc64 chips have two performance counters, 32-bits each, with
 * overflow interrupts generated on transition from 0xffffffff to 0.
 * The counters are accessed in one go using a 64-bit register.
 *
 * 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.
 *
 * The control register has 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.
 *
 * 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.
 */

56
#define MAX_HWEVENTS			2
57
#define MAX_PCRS			1
58 59 60 61
#define MAX_PERIOD			((1UL << 32) - 1)

#define PIC_UPPER_INDEX			0
#define PIC_LOWER_INDEX			1
62
#define PIC_NO_INDEX			-1
63

64
struct cpu_hw_events {
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	/* 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];

93 94
	/* Software copy of %pcr register(s) on this cpu.  */
	u64			pcr[MAX_HWEVENTS];
95 96

	/* Enabled/disable state.  */
97
	int			enabled;
98 99

	unsigned int		group_flag;
100
};
101
DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { .enabled = 1, };
102

103 104 105 106
/* 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.
 */
107 108 109 110 111 112 113 114
struct perf_event_map {
	u16	encoding;
	u8	pic_mask;
#define PIC_NONE	0x00
#define PIC_UPPER	0x01
#define PIC_LOWER	0x02
};

115
/* Encode a perf_event_map entry into a long.  */
116 117 118 119 120
static unsigned long perf_event_encode(const struct perf_event_map *pmap)
{
	return ((unsigned long) pmap->encoding << 16) | pmap->pic_mask;
}

121 122 123 124 125 126
static u8 perf_event_get_msk(unsigned long val)
{
	return val & 0xff;
}

static u64 perf_event_get_enc(unsigned long val)
127
{
128
	return val >> 16;
129 130
}

131 132 133 134 135 136 137 138 139 140
#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];

141 142
struct sparc_pmu {
	const struct perf_event_map	*(*event_map)(int);
143
	const cache_map_t		*cache_map;
144
	int				max_events;
145 146
	u32				(*read_pmc)(int);
	void				(*write_pmc)(int, u64);
147 148 149
	int				upper_shift;
	int				lower_shift;
	int				event_mask;
150 151
	int				user_bit;
	int				priv_bit;
152
	int				hv_bit;
153
	int				irq_bit;
154 155
	int				upper_nop;
	int				lower_nop;
156 157 158
	unsigned int			flags;
#define SPARC_PMU_ALL_EXCLUDES_SAME	0x00000001
#define SPARC_PMU_HAS_CONFLICTS		0x00000002
159
	int				max_hw_events;
160 161
	int				num_pcrs;
	int				num_pic_regs;
162 163
};

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
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);
}

192
static const struct perf_event_map ultra3_perfmon_event_map[] = {
193 194 195 196 197 198
	[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 },
};

199
static const struct perf_event_map *ultra3_event_map(int event_id)
200
{
201
	return &ultra3_perfmon_event_map[event_id];
202 203
}

204
static const cache_map_t ultra3_cache_map = {
205 206 207 208 209 210 211 212 213 214 215 216 217 218 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
[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 },
	},
},
289 290 291 292 293 294 295 296 297 298 299 300 301 302
[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 },
	},
},
303 304
};

305 306 307 308
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),
309 310
	.read_pmc	= sparc_default_read_pmc,
	.write_pmc	= sparc_default_write_pmc,
311 312 313
	.upper_shift	= 11,
	.lower_shift	= 4,
	.event_mask	= 0x3f,
314 315
	.user_bit	= PCR_UTRACE,
	.priv_bit	= PCR_STRACE,
316 317
	.upper_nop	= 0x1c,
	.lower_nop	= 0x14,
318 319
	.flags		= (SPARC_PMU_ALL_EXCLUDES_SAME |
			   SPARC_PMU_HAS_CONFLICTS),
320
	.max_hw_events	= 2,
321 322
	.num_pcrs	= 1,
	.num_pic_regs	= 1,
323 324
};

325 326
/* Niagara1 is very limited.  The upper PIC is hard-locked to count
 * only instructions, so it is free running which creates all kinds of
327
 * problems.  Some hardware designs make one wonder if the creator
328 329 330 331 332 333 334 335 336 337 338 339 340 341 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
 * 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 },
	},
},
427 428 429 430 431 432 433 434 435 436 437 438 439 440
[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 },
	},
},
441 442 443 444 445 446
};

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),
447 448
	.read_pmc	= sparc_default_read_pmc,
	.write_pmc	= sparc_default_write_pmc,
449 450 451
	.upper_shift	= 0,
	.lower_shift	= 4,
	.event_mask	= 0x7,
452 453
	.user_bit	= PCR_UTRACE,
	.priv_bit	= PCR_STRACE,
454 455
	.upper_nop	= 0x0,
	.lower_nop	= 0x0,
456 457
	.flags		= (SPARC_PMU_ALL_EXCLUDES_SAME |
			   SPARC_PMU_HAS_CONFLICTS),
458
	.max_hw_events	= 2,
459 460
	.num_pcrs	= 1,
	.num_pic_regs	= 1,
461 462
};

463 464 465 466 467 468 469 470 471
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 },
};

472
static const struct perf_event_map *niagara2_event_map(int event_id)
473
{
474
	return &niagara2_perfmon_event_map[event_id];
475 476
}

477 478 479 480 481 482 483 484 485 486 487 488 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
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 },
555 556 557 558 559 560 561 562 563 564 565 566 567 568
		[ 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 },
569 570 571 572 573 574 575 576 577
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
		[ C(RESULT_MISS)   ] = { CACHE_OP_UNSUPPORTED },
	},
},
};

578 579
static const struct sparc_pmu niagara2_pmu = {
	.event_map	= niagara2_event_map,
580
	.cache_map	= &niagara2_cache_map,
581
	.max_events	= ARRAY_SIZE(niagara2_perfmon_event_map),
582 583
	.read_pmc	= sparc_default_read_pmc,
	.write_pmc	= sparc_default_write_pmc,
584 585 586
	.upper_shift	= 19,
	.lower_shift	= 6,
	.event_mask	= 0xfff,
587 588 589
	.user_bit	= PCR_UTRACE,
	.priv_bit	= PCR_STRACE,
	.hv_bit		= PCR_N2_HTRACE,
590
	.irq_bit	= 0x30,
591 592
	.upper_nop	= 0x220,
	.lower_nop	= 0x220,
593 594
	.flags		= (SPARC_PMU_ALL_EXCLUDES_SAME |
			   SPARC_PMU_HAS_CONFLICTS),
595
	.max_hw_events	= 2,
596 597
	.num_pcrs	= 1,
	.num_pic_regs	= 1,
598 599
};

600 601
static const struct sparc_pmu *sparc_pmu __read_mostly;

602
static u64 event_encoding(u64 event_id, int idx)
603 604
{
	if (idx == PIC_UPPER_INDEX)
605
		event_id <<= sparc_pmu->upper_shift;
606
	else
607 608
		event_id <<= sparc_pmu->lower_shift;
	return event_id;
609 610 611 612 613 614 615 616 617 618
}

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 ?
619 620
			      sparc_pmu->upper_nop :
			      sparc_pmu->lower_nop, idx);
621 622
}

623
static inline void sparc_pmu_enable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
624 625 626
{
	u64 val, mask = mask_for_index(idx);

627
	val = cpuc->pcr[0];
628 629
	val &= ~mask;
	val |= hwc->config;
630
	cpuc->pcr[0] = val;
631

632
	pcr_ops->write_pcr(0, cpuc->pcr[0]);
633 634
}

635
static inline void sparc_pmu_disable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
636 637 638
{
	u64 mask = mask_for_index(idx);
	u64 nop = nop_for_index(idx);
639
	u64 val;
640

641
	val = cpuc->pcr[0];
642 643
	val &= ~mask;
	val |= nop;
644
	cpuc->pcr[0] = val;
645

646
	pcr_ops->write_pcr(0, cpuc->pcr[0]);
647 648
}

649 650 651 652 653 654 655 656
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:
657
	prev_raw_count = local64_read(&hwc->prev_count);
658
	new_raw_count = sparc_pmu->read_pmc(idx);
659

660
	if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
661 662 663 664 665 666
			     new_raw_count) != prev_raw_count)
		goto again;

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

667 668
	local64_add(delta, &event->count);
	local64_sub(delta, &hwc->period_left);
669 670 671 672

	return new_raw_count;
}

673
static int sparc_perf_event_set_period(struct perf_event *event,
674
				       struct hw_perf_event *hwc, int idx)
675
{
676
	s64 left = local64_read(&hwc->period_left);
677 678 679 680 681
	s64 period = hwc->sample_period;
	int ret = 0;

	if (unlikely(left <= -period)) {
		left = period;
682
		local64_set(&hwc->period_left, left);
683 684 685 686 687 688
		hwc->last_period = period;
		ret = 1;
	}

	if (unlikely(left <= 0)) {
		left += period;
689
		local64_set(&hwc->period_left, left);
690 691 692 693 694 695
		hwc->last_period = period;
		ret = 1;
	}
	if (left > MAX_PERIOD)
		left = MAX_PERIOD;

696
	local64_set(&hwc->prev_count, (u64)-left);
697

698
	sparc_pmu->write_pmc(idx, (u64)(-left) & 0xffffffff);
699

700
	perf_event_update_userpage(event);
701 702 703 704

	return ret;
}

705 706 707 708 709
/* If performance event entries have been added, move existing
 * events around (if necessary) and then assign new entries to
 * counters.
 */
static u64 maybe_change_configuration(struct cpu_hw_events *cpuc, u64 pcr)
710
{
711
	int i;
712

713 714
	if (!cpuc->n_added)
		goto out;
715

716 717 718
	/* Read in the counters which are moving.  */
	for (i = 0; i < cpuc->n_events; i++) {
		struct perf_event *cp = cpuc->event[i];
719

720 721 722 723 724 725 726
		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;
		}
	}
727

728 729 730 731 732 733 734 735 736 737 738 739 740 741
	/* 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]);
742
		pcr &= ~mask_for_index(idx);
P
Peter Zijlstra 已提交
743 744 745 746
		if (hwc->state & PERF_HES_STOPPED)
			pcr |= nop_for_index(idx);
		else
			pcr |= event_encoding(enc, idx);
747 748 749
	}
out:
	return pcr;
750 751
}

P
Peter Zijlstra 已提交
752
static void sparc_pmu_enable(struct pmu *pmu)
753
{
754
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
755

756 757
	if (cpuc->enabled)
		return;
758

759 760
	cpuc->enabled = 1;
	barrier();
761

762 763
	if (cpuc->n_events) {
		u64 pcr = maybe_change_configuration(cpuc, cpuc->pcr[0]);
764

765 766 767 768
		/* We require that all of the events have the same
		 * configuration, so just fetch the settings from the
		 * first entry.
		 */
769
		cpuc->pcr[0] = pcr | cpuc->event[0]->hw.config_base;
770
	}
771

772
	pcr_ops->write_pcr(0, cpuc->pcr[0]);
773 774
}

P
Peter Zijlstra 已提交
775
static void sparc_pmu_disable(struct pmu *pmu)
776 777
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
778
	int i;
779 780 781 782 783 784 785

	if (!cpuc->enabled)
		return;

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

786 787
	for (i = 0; i < sparc_pmu->num_pcrs; i++) {
		u64 val = cpuc->pcr[i];
788

789 790 791 792 793
		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]);
	}
794 795
}

P
Peter Zijlstra 已提交
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
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)
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
	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)
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
	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)
841
{
842
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
843 844
	unsigned long flags;
	int i;
845

846
	local_irq_save(flags);
P
Peter Zijlstra 已提交
847
	perf_pmu_disable(event->pmu);
848 849 850

	for (i = 0; i < cpuc->n_events; i++) {
		if (event == cpuc->event[i]) {
P
Peter Zijlstra 已提交
851 852 853 854
			/* Absorb the final count and turn off the
			 * event.
			 */
			sparc_pmu_stop(event, PERF_EF_UPDATE);
855 856 857 858 859 860 861 862 863 864 865 866

			/* 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);
867

868 869 870 871
			cpuc->n_events--;
			break;
		}
	}
872

P
Peter Zijlstra 已提交
873
	perf_pmu_enable(event->pmu);
874 875 876
	local_irq_restore(flags);
}

877
static void sparc_pmu_read(struct perf_event *event)
878
{
879 880
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
	int idx = active_event_index(cpuc, event);
881
	struct hw_perf_event *hwc = &event->hw;
882

883
	sparc_perf_event_update(event, hwc, idx);
884 885
}

886
static atomic_t active_events = ATOMIC_INIT(0);
887 888
static DEFINE_MUTEX(pmc_grab_mutex);

889 890 891
static void perf_stop_nmi_watchdog(void *unused)
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
892
	int i;
893 894

	stop_nmi_watchdog(NULL);
895 896
	for (i = 0; i < sparc_pmu->num_pcrs; i++)
		cpuc->pcr[i] = pcr_ops->read_pcr(i);
897 898
}

899
void perf_event_grab_pmc(void)
900
{
901
	if (atomic_inc_not_zero(&active_events))
902 903 904
		return;

	mutex_lock(&pmc_grab_mutex);
905
	if (atomic_read(&active_events) == 0) {
906
		if (atomic_read(&nmi_active) > 0) {
907
			on_each_cpu(perf_stop_nmi_watchdog, NULL, 1);
908 909
			BUG_ON(atomic_read(&nmi_active) != 0);
		}
910
		atomic_inc(&active_events);
911 912 913 914
	}
	mutex_unlock(&pmc_grab_mutex);
}

915
void perf_event_release_pmc(void)
916
{
917
	if (atomic_dec_and_mutex_lock(&active_events, &pmc_grab_mutex)) {
918 919 920 921 922 923
		if (atomic_read(&nmi_active) == 0)
			on_each_cpu(start_nmi_watchdog, NULL, 1);
		mutex_unlock(&pmc_grab_mutex);
	}
}

924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
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;
}

955
static void hw_perf_event_destroy(struct perf_event *event)
956
{
957
	perf_event_release_pmc();
958 959
}

960 961 962
/* 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.
963 964 965 966 967 968
 *
 * 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.
969
 */
970 971
static int sparc_check_constraints(struct perf_event **evts,
				   unsigned long *events, int n_ev)
972
{
973 974 975 976 977 978 979 980 981
	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;

982
	if (n_ev > sparc_pmu->max_hw_events)
983 984
		return -1;

985 986 987 988 989 990 991 992
	if (!(sparc_pmu->flags & SPARC_PMU_HAS_CONFLICTS)) {
		int i;

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

993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
	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;
1015 1016
	}

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
	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.  */
1033
	return -1;
1034 1035 1036 1037 1038 1039

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

1042 1043 1044 1045 1046 1047
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;

1048 1049 1050
	if (!(sparc_pmu->flags & SPARC_PMU_ALL_EXCLUDES_SAME))
		return 0;

1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
	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,
1074 1075
			  struct perf_event *evts[], unsigned long *events,
			  int *current_idx)
1076 1077 1078 1079 1080 1081 1082 1083
{
	struct perf_event *event;
	int n = 0;

	if (!is_software_event(group)) {
		if (n >= max_count)
			return -1;
		evts[n] = group;
1084 1085
		events[n] = group->hw.event_base;
		current_idx[n++] = PIC_NO_INDEX;
1086 1087 1088 1089 1090 1091 1092
	}
	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;
1093 1094
			events[n] = event->hw.event_base;
			current_idx[n++] = PIC_NO_INDEX;
1095 1096 1097 1098 1099
		}
	}
	return n;
}

P
Peter Zijlstra 已提交
1100
static int sparc_pmu_add(struct perf_event *event, int ef_flags)
1101 1102 1103 1104 1105 1106
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
	int n0, ret = -EAGAIN;
	unsigned long flags;

	local_irq_save(flags);
P
Peter Zijlstra 已提交
1107
	perf_pmu_disable(event->pmu);
1108 1109

	n0 = cpuc->n_events;
1110
	if (n0 >= sparc_pmu->max_hw_events)
1111 1112 1113 1114 1115 1116
		goto out;

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

P
Peter Zijlstra 已提交
1117 1118 1119 1120
	event->hw.state = PERF_HES_UPTODATE;
	if (!(ef_flags & PERF_EF_START))
		event->hw.state |= PERF_HES_STOPPED;

1121 1122
	/*
	 * If group events scheduling transaction was started,
L
Lucas De Marchi 已提交
1123
	 * skip the schedulability test here, it will be performed
1124 1125
	 * at commit time(->commit_txn) as a whole
	 */
1126
	if (cpuc->group_flag & PERF_EVENT_TXN)
1127 1128
		goto nocheck;

1129 1130 1131 1132 1133
	if (check_excludes(cpuc->event, n0, 1))
		goto out;
	if (sparc_check_constraints(cpuc->event, cpuc->events, n0 + 1))
		goto out;

1134
nocheck:
1135 1136 1137 1138 1139
	cpuc->n_events++;
	cpuc->n_added++;

	ret = 0;
out:
P
Peter Zijlstra 已提交
1140
	perf_pmu_enable(event->pmu);
1141 1142 1143 1144
	local_irq_restore(flags);
	return ret;
}

1145
static int sparc_pmu_event_init(struct perf_event *event)
1146
{
1147
	struct perf_event_attr *attr = &event->attr;
1148
	struct perf_event *evts[MAX_HWEVENTS];
1149
	struct hw_perf_event *hwc = &event->hw;
1150
	unsigned long events[MAX_HWEVENTS];
1151
	int current_idx_dmy[MAX_HWEVENTS];
1152
	const struct perf_event_map *pmap;
1153
	int n;
1154 1155 1156 1157

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

1158 1159 1160 1161
	/* does not support taken branch sampling */
	if (has_branch_stack(event))
		return -EOPNOTSUPP;

1162 1163
	switch (attr->type) {
	case PERF_TYPE_HARDWARE:
1164 1165 1166
		if (attr->config >= sparc_pmu->max_events)
			return -EINVAL;
		pmap = sparc_pmu->event_map(attr->config);
1167 1168 1169
		break;

	case PERF_TYPE_HW_CACHE:
1170 1171 1172
		pmap = sparc_map_cache_event(attr->config);
		if (IS_ERR(pmap))
			return PTR_ERR(pmap);
1173 1174 1175
		break;

	case PERF_TYPE_RAW:
1176 1177
		pmap = NULL;
		break;
1178

1179 1180 1181 1182 1183
	default:
		return -ENOENT;

	}

1184 1185 1186
	if (pmap) {
		hwc->event_base = perf_event_encode(pmap);
	} else {
1187 1188
		/*
		 * User gives us "(encoding << 16) | pic_mask" for
1189 1190 1191 1192 1193
		 * PERF_TYPE_RAW events.
		 */
		hwc->event_base = attr->config;
	}

1194
	/* We save the enable bits in the config_base.  */
1195
	hwc->config_base = sparc_pmu->irq_bit;
1196
	if (!attr->exclude_user)
1197
		hwc->config_base |= sparc_pmu->user_bit;
1198
	if (!attr->exclude_kernel)
1199
		hwc->config_base |= sparc_pmu->priv_bit;
1200 1201
	if (!attr->exclude_hv)
		hwc->config_base |= sparc_pmu->hv_bit;
1202

1203 1204 1205
	n = 0;
	if (event->group_leader != event) {
		n = collect_events(event->group_leader,
1206
				   sparc_pmu->max_hw_events - 1,
1207
				   evts, events, current_idx_dmy);
1208 1209 1210
		if (n < 0)
			return -EINVAL;
	}
1211
	events[n] = hwc->event_base;
1212 1213 1214 1215 1216
	evts[n] = event;

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

1217
	if (sparc_check_constraints(evts, events, n + 1))
1218 1219
		return -EINVAL;

1220 1221
	hwc->idx = PIC_NO_INDEX;

1222 1223 1224 1225 1226 1227
	/* 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;

1228 1229 1230
	if (!hwc->sample_period) {
		hwc->sample_period = MAX_PERIOD;
		hwc->last_period = hwc->sample_period;
1231
		local64_set(&hwc->period_left, hwc->sample_period);
1232 1233 1234 1235 1236
	}

	return 0;
}

1237 1238 1239 1240 1241
/*
 * Start group events scheduling transaction
 * Set the flag to make pmu::enable() not perform the
 * schedulability test, it will be performed at commit time
 */
P
Peter Zijlstra 已提交
1242
static void sparc_pmu_start_txn(struct pmu *pmu)
1243 1244 1245
{
	struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);

P
Peter Zijlstra 已提交
1246
	perf_pmu_disable(pmu);
1247
	cpuhw->group_flag |= PERF_EVENT_TXN;
1248 1249 1250 1251 1252 1253 1254
}

/*
 * Stop group events scheduling transaction
 * Clear the flag and pmu::enable() will perform the
 * schedulability test.
 */
P
Peter Zijlstra 已提交
1255
static void sparc_pmu_cancel_txn(struct pmu *pmu)
1256 1257 1258
{
	struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);

1259
	cpuhw->group_flag &= ~PERF_EVENT_TXN;
P
Peter Zijlstra 已提交
1260
	perf_pmu_enable(pmu);
1261 1262 1263 1264 1265 1266 1267
}

/*
 * Commit group events scheduling transaction
 * Perform the group schedulability test as a whole
 * Return 0 if success
 */
P
Peter Zijlstra 已提交
1268
static int sparc_pmu_commit_txn(struct pmu *pmu)
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
	int n;

	if (!sparc_pmu)
		return -EINVAL;

	cpuc = &__get_cpu_var(cpu_hw_events);
	n = cpuc->n_events;
	if (check_excludes(cpuc->event, 0, n))
		return -EINVAL;
	if (sparc_check_constraints(cpuc->event, cpuc->events, n))
		return -EAGAIN;

1283
	cpuc->group_flag &= ~PERF_EVENT_TXN;
P
Peter Zijlstra 已提交
1284
	perf_pmu_enable(pmu);
1285 1286 1287
	return 0;
}

P
Peter Zijlstra 已提交
1288
static struct pmu pmu = {
P
Peter Zijlstra 已提交
1289 1290
	.pmu_enable	= sparc_pmu_enable,
	.pmu_disable	= sparc_pmu_disable,
1291
	.event_init	= sparc_pmu_event_init,
P
Peter Zijlstra 已提交
1292 1293 1294 1295
	.add		= sparc_pmu_add,
	.del		= sparc_pmu_del,
	.start		= sparc_pmu_start,
	.stop		= sparc_pmu_stop,
1296
	.read		= sparc_pmu_read,
1297 1298 1299
	.start_txn	= sparc_pmu_start_txn,
	.cancel_txn	= sparc_pmu_cancel_txn,
	.commit_txn	= sparc_pmu_commit_txn,
1300 1301
};

1302
void perf_event_print_debug(void)
1303 1304
{
	unsigned long flags;
1305
	int cpu, i;
1306 1307 1308 1309 1310 1311 1312 1313 1314

	if (!sparc_pmu)
		return;

	local_irq_save(flags);

	cpu = smp_processor_id();

	pr_info("\n");
1315 1316 1317 1318 1319 1320
	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));
1321 1322 1323 1324

	local_irq_restore(flags);
}

1325
static int __kprobes perf_event_nmi_handler(struct notifier_block *self,
1326
					    unsigned long cmd, void *__args)
1327 1328 1329
{
	struct die_args *args = __args;
	struct perf_sample_data data;
1330
	struct cpu_hw_events *cpuc;
1331
	struct pt_regs *regs;
1332
	int i;
1333

1334
	if (!atomic_read(&active_events))
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
		return NOTIFY_DONE;

	switch (cmd) {
	case DIE_NMI:
		break;

	default:
		return NOTIFY_DONE;
	}

	regs = args->regs;

1347
	cpuc = &__get_cpu_var(cpu_hw_events);
1348 1349 1350 1351 1352 1353 1354 1355

	/* 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.
	 */
1356 1357 1358
	if (sparc_pmu->irq_bit &&
	    sparc_pmu->num_pcrs == 1)
		pcr_ops->write_pcr(0, cpuc->pcr[0]);
1359

1360 1361 1362
	for (i = 0; i < cpuc->n_events; i++) {
		struct perf_event *event = cpuc->event[i];
		int idx = cpuc->current_idx[i];
1363
		struct hw_perf_event *hwc;
1364 1365
		u64 val;

1366 1367 1368 1369
		if (sparc_pmu->irq_bit &&
		    sparc_pmu->num_pcrs > 1)
			pcr_ops->write_pcr(idx, cpuc->pcr[idx]);

1370 1371
		hwc = &event->hw;
		val = sparc_perf_event_update(event, hwc, idx);
1372 1373 1374
		if (val & (1ULL << 31))
			continue;

1375
		perf_sample_data_init(&data, 0, hwc->last_period);
1376
		if (!sparc_perf_event_set_period(event, hwc, idx))
1377 1378
			continue;

1379
		if (perf_event_overflow(event, &data, regs))
P
Peter Zijlstra 已提交
1380
			sparc_pmu_stop(event, 0);
1381 1382 1383 1384 1385
	}

	return NOTIFY_STOP;
}

1386 1387
static __read_mostly struct notifier_block perf_event_nmi_notifier = {
	.notifier_call		= perf_event_nmi_handler,
1388 1389 1390 1391
};

static bool __init supported_pmu(void)
{
1392 1393 1394 1395 1396
	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;
1397 1398
		return true;
	}
1399 1400 1401 1402
	if (!strcmp(sparc_pmu_type, "niagara")) {
		sparc_pmu = &niagara1_pmu;
		return true;
	}
1403 1404
	if (!strcmp(sparc_pmu_type, "niagara2") ||
	    !strcmp(sparc_pmu_type, "niagara3")) {
1405 1406 1407
		sparc_pmu = &niagara2_pmu;
		return true;
	}
1408 1409 1410
	return false;
}

1411
int __init init_hw_perf_events(void)
1412
{
1413
	pr_info("Performance events: ");
1414 1415 1416

	if (!supported_pmu()) {
		pr_cont("No support for PMU type '%s'\n", sparc_pmu_type);
1417
		return 0;
1418 1419 1420 1421
	}

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

P
Peter Zijlstra 已提交
1422
	perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
1423
	register_die_notifier(&perf_event_nmi_notifier);
1424 1425

	return 0;
1426
}
1427
early_initcall(init_hw_perf_events);
1428

1429 1430
void perf_callchain_kernel(struct perf_callchain_entry *entry,
			   struct pt_regs *regs)
1431 1432
{
	unsigned long ksp, fp;
1433 1434 1435
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
	int graph = 0;
#endif
1436

1437 1438
	stack_trace_flush();

1439
	perf_callchain_store(entry, regs->tpc);
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462

	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;
		}
1463
		perf_callchain_store(entry, pc);
1464 1465 1466 1467 1468
#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;
1469
				perf_callchain_store(entry, pc);
1470 1471 1472 1473
				graph++;
			}
		}
#endif
1474 1475 1476
	} while (entry->nr < PERF_MAX_STACK_DEPTH);
}

1477 1478
static void perf_callchain_user_64(struct perf_callchain_entry *entry,
				   struct pt_regs *regs)
1479 1480 1481
{
	unsigned long ufp;

1482
	perf_callchain_store(entry, regs->tpc);
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494

	ufp = regs->u_regs[UREG_I6] + STACK_BIAS;
	do {
		struct sparc_stackf *usf, sf;
		unsigned long pc;

		usf = (struct sparc_stackf *) ufp;
		if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
			break;

		pc = sf.callers_pc;
		ufp = (unsigned long)sf.fp + STACK_BIAS;
1495
		perf_callchain_store(entry, pc);
1496 1497 1498
	} while (entry->nr < PERF_MAX_STACK_DEPTH);
}

1499 1500
static void perf_callchain_user_32(struct perf_callchain_entry *entry,
				   struct pt_regs *regs)
1501 1502 1503
{
	unsigned long ufp;

1504
	perf_callchain_store(entry, regs->tpc);
1505

1506
	ufp = regs->u_regs[UREG_I6] & 0xffffffffUL;
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
	do {
		struct sparc_stackf32 *usf, sf;
		unsigned long pc;

		usf = (struct sparc_stackf32 *) ufp;
		if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
			break;

		pc = sf.callers_pc;
		ufp = (unsigned long)sf.fp;
1517
		perf_callchain_store(entry, pc);
1518 1519 1520
	} while (entry->nr < PERF_MAX_STACK_DEPTH);
}

1521 1522
void
perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
1523
{
1524 1525 1526 1527 1528
	flushw_user();
	if (test_thread_flag(TIF_32BIT))
		perf_callchain_user_32(entry, regs);
	else
		perf_callchain_user_64(entry, regs);
1529
}