perf_event_intel_ds.c 27.2 KB
Newer Older
1 2 3
#include <linux/bitops.h>
#include <linux/types.h>
#include <linux/slab.h>
4

5
#include <asm/perf_event.h>
6
#include <asm/insn.h>
7 8

#include "perf_event.h"
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

/* The size of a BTS record in bytes: */
#define BTS_RECORD_SIZE		24

#define BTS_BUFFER_SIZE		(PAGE_SIZE << 4)
#define PEBS_BUFFER_SIZE	PAGE_SIZE

/*
 * pebs_record_32 for p4 and core not supported

struct pebs_record_32 {
	u32 flags, ip;
	u32 ax, bc, cx, dx;
	u32 si, di, bp, sp;
};

 */

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
union intel_x86_pebs_dse {
	u64 val;
	struct {
		unsigned int ld_dse:4;
		unsigned int ld_stlb_miss:1;
		unsigned int ld_locked:1;
		unsigned int ld_reserved:26;
	};
	struct {
		unsigned int st_l1d_hit:1;
		unsigned int st_reserved1:3;
		unsigned int st_stlb_miss:1;
		unsigned int st_locked:1;
		unsigned int st_reserved2:26;
	};
};


/*
 * Map PEBS Load Latency Data Source encodings to generic
 * memory data source information
 */
#define P(a, b) PERF_MEM_S(a, b)
#define OP_LH (P(OP, LOAD) | P(LVL, HIT))
#define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS))

static const u64 pebs_data_source[] = {
	P(OP, LOAD) | P(LVL, MISS) | P(LVL, L3) | P(SNOOP, NA),/* 0x00:ukn L3 */
	OP_LH | P(LVL, L1)  | P(SNOOP, NONE),	/* 0x01: L1 local */
	OP_LH | P(LVL, LFB) | P(SNOOP, NONE),	/* 0x02: LFB hit */
	OP_LH | P(LVL, L2)  | P(SNOOP, NONE),	/* 0x03: L2 hit */
	OP_LH | P(LVL, L3)  | P(SNOOP, NONE),	/* 0x04: L3 hit */
	OP_LH | P(LVL, L3)  | P(SNOOP, MISS),	/* 0x05: L3 hit, snoop miss */
	OP_LH | P(LVL, L3)  | P(SNOOP, HIT),	/* 0x06: L3 hit, snoop hit */
	OP_LH | P(LVL, L3)  | P(SNOOP, HITM),	/* 0x07: L3 hit, snoop hitm */
	OP_LH | P(LVL, REM_CCE1) | P(SNOOP, HIT),  /* 0x08: L3 miss snoop hit */
	OP_LH | P(LVL, REM_CCE1) | P(SNOOP, HITM), /* 0x09: L3 miss snoop hitm*/
	OP_LH | P(LVL, LOC_RAM)  | P(SNOOP, HIT),  /* 0x0a: L3 miss, shared */
	OP_LH | P(LVL, REM_RAM1) | P(SNOOP, HIT),  /* 0x0b: L3 miss, shared */
	OP_LH | P(LVL, LOC_RAM)  | SNOOP_NONE_MISS,/* 0x0c: L3 miss, excl */
	OP_LH | P(LVL, REM_RAM1) | SNOOP_NONE_MISS,/* 0x0d: L3 miss, excl */
	OP_LH | P(LVL, IO)  | P(SNOOP, NONE), /* 0x0e: I/O */
	OP_LH | P(LVL, UNC) | P(SNOOP, NONE), /* 0x0f: uncached */
};

72 73 74 75 76 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 105 106 107 108 109
static u64 precise_store_data(u64 status)
{
	union intel_x86_pebs_dse dse;
	u64 val = P(OP, STORE) | P(SNOOP, NA) | P(LVL, L1) | P(TLB, L2);

	dse.val = status;

	/*
	 * bit 4: TLB access
	 * 1 = stored missed 2nd level TLB
	 *
	 * so it either hit the walker or the OS
	 * otherwise hit 2nd level TLB
	 */
	if (dse.st_stlb_miss)
		val |= P(TLB, MISS);
	else
		val |= P(TLB, HIT);

	/*
	 * bit 0: hit L1 data cache
	 * if not set, then all we know is that
	 * it missed L1D
	 */
	if (dse.st_l1d_hit)
		val |= P(LVL, HIT);
	else
		val |= P(LVL, MISS);

	/*
	 * bit 5: Locked prefix
	 */
	if (dse.st_locked)
		val |= P(LOCK, LOCKED);

	return val;
}

110 111 112 113 114 115 116 117 118 119 120 121 122
static u64 precise_store_data_hsw(u64 status)
{
	union perf_mem_data_src dse;

	dse.val = 0;
	dse.mem_op = PERF_MEM_OP_STORE;
	dse.mem_lvl = PERF_MEM_LVL_NA;
	if (status & 1)
		dse.mem_lvl = PERF_MEM_LVL_L1;
	/* Nothing else supported. Sorry. */
	return dse.val;
}

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
static u64 load_latency_data(u64 status)
{
	union intel_x86_pebs_dse dse;
	u64 val;
	int model = boot_cpu_data.x86_model;
	int fam = boot_cpu_data.x86;

	dse.val = status;

	/*
	 * use the mapping table for bit 0-3
	 */
	val = pebs_data_source[dse.ld_dse];

	/*
	 * Nehalem models do not support TLB, Lock infos
	 */
	if (fam == 0x6 && (model == 26 || model == 30
	    || model == 31 || model == 46)) {
		val |= P(TLB, NA) | P(LOCK, NA);
		return val;
	}
	/*
	 * bit 4: TLB access
	 * 0 = did not miss 2nd level TLB
	 * 1 = missed 2nd level TLB
	 */
	if (dse.ld_stlb_miss)
		val |= P(TLB, MISS) | P(TLB, L2);
	else
		val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2);

	/*
	 * bit 5: locked prefix
	 */
	if (dse.ld_locked)
		val |= P(LOCK, LOCKED);

	return val;
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
struct pebs_record_core {
	u64 flags, ip;
	u64 ax, bx, cx, dx;
	u64 si, di, bp, sp;
	u64 r8,  r9,  r10, r11;
	u64 r12, r13, r14, r15;
};

struct pebs_record_nhm {
	u64 flags, ip;
	u64 ax, bx, cx, dx;
	u64 si, di, bp, sp;
	u64 r8,  r9,  r10, r11;
	u64 r12, r13, r14, r15;
	u64 status, dla, dse, lat;
};

181 182 183 184
/*
 * Same as pebs_record_nhm, with two additional fields.
 */
struct pebs_record_hsw {
185 186 187 188 189 190
	u64 flags, ip;
	u64 ax, bx, cx, dx;
	u64 si, di, bp, sp;
	u64 r8,  r9,  r10, r11;
	u64 r12, r13, r14, r15;
	u64 status, dla, dse, lat;
191
	u64 real_ip, tsx_tuning;
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
};

union hsw_tsx_tuning {
	struct {
		u32 cycles_last_block     : 32,
		    hle_abort		  : 1,
		    rtm_abort		  : 1,
		    instruction_abort     : 1,
		    non_instruction_abort : 1,
		    retry		  : 1,
		    data_conflict	  : 1,
		    capacity_writes	  : 1,
		    capacity_reads	  : 1;
	};
	u64	    value;
207 208
};

209
void init_debug_store_on_cpu(int cpu)
210 211 212 213 214 215 216 217 218 219 220
{
	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;

	if (!ds)
		return;

	wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA,
		     (u32)((u64)(unsigned long)ds),
		     (u32)((u64)(unsigned long)ds >> 32));
}

221
void fini_debug_store_on_cpu(int cpu)
222 223 224 225 226 227 228
{
	if (!per_cpu(cpu_hw_events, cpu).ds)
		return;

	wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0);
}

229 230 231
static int alloc_pebs_buffer(int cpu)
{
	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
232
	int node = cpu_to_node(cpu);
233 234 235 236 237 238
	int max, thresh = 1; /* always use a single PEBS record */
	void *buffer;

	if (!x86_pmu.pebs)
		return 0;

239
	buffer = kzalloc_node(PEBS_BUFFER_SIZE, GFP_KERNEL, node);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	if (unlikely(!buffer))
		return -ENOMEM;

	max = PEBS_BUFFER_SIZE / x86_pmu.pebs_record_size;

	ds->pebs_buffer_base = (u64)(unsigned long)buffer;
	ds->pebs_index = ds->pebs_buffer_base;
	ds->pebs_absolute_maximum = ds->pebs_buffer_base +
		max * x86_pmu.pebs_record_size;

	ds->pebs_interrupt_threshold = ds->pebs_buffer_base +
		thresh * x86_pmu.pebs_record_size;

	return 0;
}

256 257 258 259 260 261 262 263 264 265 266
static void release_pebs_buffer(int cpu)
{
	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;

	if (!ds || !x86_pmu.pebs)
		return;

	kfree((void *)(unsigned long)ds->pebs_buffer_base);
	ds->pebs_buffer_base = 0;
}

267 268 269
static int alloc_bts_buffer(int cpu)
{
	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
270
	int node = cpu_to_node(cpu);
271 272 273 274 275 276
	int max, thresh;
	void *buffer;

	if (!x86_pmu.bts)
		return 0;

277
	buffer = kzalloc_node(BTS_BUFFER_SIZE, GFP_KERNEL, node);
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
	if (unlikely(!buffer))
		return -ENOMEM;

	max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE;
	thresh = max / 16;

	ds->bts_buffer_base = (u64)(unsigned long)buffer;
	ds->bts_index = ds->bts_buffer_base;
	ds->bts_absolute_maximum = ds->bts_buffer_base +
		max * BTS_RECORD_SIZE;
	ds->bts_interrupt_threshold = ds->bts_absolute_maximum -
		thresh * BTS_RECORD_SIZE;

	return 0;
}

294 295 296 297 298 299 300 301 302 303 304
static void release_bts_buffer(int cpu)
{
	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;

	if (!ds || !x86_pmu.bts)
		return;

	kfree((void *)(unsigned long)ds->bts_buffer_base);
	ds->bts_buffer_base = 0;
}

305 306
static int alloc_ds_buffer(int cpu)
{
307
	int node = cpu_to_node(cpu);
308 309
	struct debug_store *ds;

310
	ds = kzalloc_node(sizeof(*ds), GFP_KERNEL, node);
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
	if (unlikely(!ds))
		return -ENOMEM;

	per_cpu(cpu_hw_events, cpu).ds = ds;

	return 0;
}

static void release_ds_buffer(int cpu)
{
	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;

	if (!ds)
		return;

	per_cpu(cpu_hw_events, cpu).ds = NULL;
	kfree(ds);
}

330
void release_ds_buffers(void)
331 332 333 334 335 336 337 338 339 340 341
{
	int cpu;

	if (!x86_pmu.bts && !x86_pmu.pebs)
		return;

	get_online_cpus();
	for_each_online_cpu(cpu)
		fini_debug_store_on_cpu(cpu);

	for_each_possible_cpu(cpu) {
342 343
		release_pebs_buffer(cpu);
		release_bts_buffer(cpu);
344
		release_ds_buffer(cpu);
345 346 347 348
	}
	put_online_cpus();
}

349
void reserve_ds_buffers(void)
350
{
351 352 353 354 355
	int bts_err = 0, pebs_err = 0;
	int cpu;

	x86_pmu.bts_active = 0;
	x86_pmu.pebs_active = 0;
356 357

	if (!x86_pmu.bts && !x86_pmu.pebs)
358
		return;
359

360 361 362 363 364 365
	if (!x86_pmu.bts)
		bts_err = 1;

	if (!x86_pmu.pebs)
		pebs_err = 1;

366 367 368
	get_online_cpus();

	for_each_possible_cpu(cpu) {
369 370 371 372
		if (alloc_ds_buffer(cpu)) {
			bts_err = 1;
			pebs_err = 1;
		}
373

374 375 376 377 378
		if (!bts_err && alloc_bts_buffer(cpu))
			bts_err = 1;

		if (!pebs_err && alloc_pebs_buffer(cpu))
			pebs_err = 1;
379

380
		if (bts_err && pebs_err)
381
			break;
382 383 384 385 386 387
	}

	if (bts_err) {
		for_each_possible_cpu(cpu)
			release_bts_buffer(cpu);
	}
388

389 390 391
	if (pebs_err) {
		for_each_possible_cpu(cpu)
			release_pebs_buffer(cpu);
392 393
	}

394 395 396 397 398 399 400 401 402 403
	if (bts_err && pebs_err) {
		for_each_possible_cpu(cpu)
			release_ds_buffer(cpu);
	} else {
		if (x86_pmu.bts && !bts_err)
			x86_pmu.bts_active = 1;

		if (x86_pmu.pebs && !pebs_err)
			x86_pmu.pebs_active = 1;

404 405 406 407 408 409 410 411 412 413 414
		for_each_online_cpu(cpu)
			init_debug_store_on_cpu(cpu);
	}

	put_online_cpus();
}

/*
 * BTS
 */

415
struct event_constraint bts_constraint =
416
	EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0);
417

418
void intel_pmu_enable_bts(u64 config)
419 420 421 422 423
{
	unsigned long debugctlmsr;

	debugctlmsr = get_debugctlmsr();

424 425 426
	debugctlmsr |= DEBUGCTLMSR_TR;
	debugctlmsr |= DEBUGCTLMSR_BTS;
	debugctlmsr |= DEBUGCTLMSR_BTINT;
427 428

	if (!(config & ARCH_PERFMON_EVENTSEL_OS))
429
		debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
430 431

	if (!(config & ARCH_PERFMON_EVENTSEL_USR))
432
		debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR;
433 434 435 436

	update_debugctlmsr(debugctlmsr);
}

437
void intel_pmu_disable_bts(void)
438 439 440 441 442 443 444 445 446 447
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
	unsigned long debugctlmsr;

	if (!cpuc->ds)
		return;

	debugctlmsr = get_debugctlmsr();

	debugctlmsr &=
448 449
		~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT |
		  DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR);
450 451 452 453

	update_debugctlmsr(debugctlmsr);
}

454
int intel_pmu_drain_bts_buffer(void)
455 456 457 458 459 460 461 462
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
	struct debug_store *ds = cpuc->ds;
	struct bts_record {
		u64	from;
		u64	to;
		u64	flags;
	};
463
	struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
464 465 466 467 468 469 470
	struct bts_record *at, *top;
	struct perf_output_handle handle;
	struct perf_event_header header;
	struct perf_sample_data data;
	struct pt_regs regs;

	if (!event)
471
		return 0;
472

473
	if (!x86_pmu.bts_active)
474
		return 0;
475 476 477 478 479

	at  = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
	top = (struct bts_record *)(unsigned long)ds->bts_index;

	if (top <= at)
480
		return 0;
481

482 483
	memset(&regs, 0, sizeof(regs));

484 485
	ds->bts_index = ds->bts_buffer_base;

486
	perf_sample_data_init(&data, 0, event->hw.last_period);
487 488 489 490 491 492 493 494

	/*
	 * Prepare a generic sample, i.e. fill in the invariant fields.
	 * We will overwrite the from and to address before we output
	 * the sample.
	 */
	perf_prepare_sample(&header, &data, event, &regs);

495
	if (perf_output_begin(&handle, event, header.size * (top - at)))
496
		return 1;
497 498 499 500 501 502 503 504 505 506 507 508 509

	for (; at < top; at++) {
		data.ip		= at->from;
		data.addr	= at->to;

		perf_output_sample(&handle, &header, &data, event);
	}

	perf_output_end(&handle);

	/* There's new data available. */
	event->hw.interrupts++;
	event->pending_kill = POLL_IN;
510
	return 1;
511 512 513 514 515
}

/*
 * PEBS
 */
516
struct event_constraint intel_core2_pebs_event_constraints[] = {
517 518 519 520 521
	INTEL_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
	INTEL_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
	INTEL_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
	INTEL_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
	INTEL_EVENT_CONSTRAINT(0xcb, 0x1),    /* MEM_LOAD_RETIRED.* */
522 523 524
	EVENT_CONSTRAINT_END
};

525
struct event_constraint intel_atom_pebs_event_constraints[] = {
526 527 528
	INTEL_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
	INTEL_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
	INTEL_EVENT_CONSTRAINT(0xcb, 0x1),    /* MEM_LOAD_RETIRED.* */
529 530 531
	EVENT_CONSTRAINT_END
};

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
struct event_constraint intel_slm_pebs_event_constraints[] = {
	INTEL_UEVENT_CONSTRAINT(0x0103, 0x1), /* REHABQ.LD_BLOCK_ST_FORWARD_PS */
	INTEL_UEVENT_CONSTRAINT(0x0803, 0x1), /* REHABQ.LD_SPLITS_PS */
	INTEL_UEVENT_CONSTRAINT(0x0204, 0x1), /* MEM_UOPS_RETIRED.L2_HIT_LOADS_PS */
	INTEL_UEVENT_CONSTRAINT(0x0404, 0x1), /* MEM_UOPS_RETIRED.L2_MISS_LOADS_PS */
	INTEL_UEVENT_CONSTRAINT(0x0804, 0x1), /* MEM_UOPS_RETIRED.DTLB_MISS_LOADS_PS */
	INTEL_UEVENT_CONSTRAINT(0x2004, 0x1), /* MEM_UOPS_RETIRED.HITM_PS */
	INTEL_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY_PS */
	INTEL_UEVENT_CONSTRAINT(0x00c4, 0x1), /* BR_INST_RETIRED.ALL_BRANCHES_PS */
	INTEL_UEVENT_CONSTRAINT(0x7ec4, 0x1), /* BR_INST_RETIRED.JCC_PS */
	INTEL_UEVENT_CONSTRAINT(0xbfc4, 0x1), /* BR_INST_RETIRED.FAR_BRANCH_PS */
	INTEL_UEVENT_CONSTRAINT(0xebc4, 0x1), /* BR_INST_RETIRED.NON_RETURN_IND_PS */
	INTEL_UEVENT_CONSTRAINT(0xf7c4, 0x1), /* BR_INST_RETIRED.RETURN_PS */
	INTEL_UEVENT_CONSTRAINT(0xf9c4, 0x1), /* BR_INST_RETIRED.CALL_PS */
	INTEL_UEVENT_CONSTRAINT(0xfbc4, 0x1), /* BR_INST_RETIRED.IND_CALL_PS */
	INTEL_UEVENT_CONSTRAINT(0xfdc4, 0x1), /* BR_INST_RETIRED.REL_CALL_PS */
	INTEL_UEVENT_CONSTRAINT(0xfec4, 0x1), /* BR_INST_RETIRED.TAKEN_JCC_PS */
	INTEL_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_MISP_RETIRED.ALL_BRANCHES_PS */
	INTEL_UEVENT_CONSTRAINT(0x7ec5, 0x1), /* BR_INST_MISP_RETIRED.JCC_PS */
	INTEL_UEVENT_CONSTRAINT(0xebc5, 0x1), /* BR_INST_MISP_RETIRED.NON_RETURN_IND_PS */
	INTEL_UEVENT_CONSTRAINT(0xf7c5, 0x1), /* BR_INST_MISP_RETIRED.RETURN_PS */
	INTEL_UEVENT_CONSTRAINT(0xfbc5, 0x1), /* BR_INST_MISP_RETIRED.IND_CALL_PS */
	INTEL_UEVENT_CONSTRAINT(0xfec5, 0x1), /* BR_INST_MISP_RETIRED.TAKEN_JCC_PS */
	EVENT_CONSTRAINT_END
};

558
struct event_constraint intel_nehalem_pebs_event_constraints[] = {
559
	INTEL_PLD_CONSTRAINT(0x100b, 0xf),      /* MEM_INST_RETIRED.* */
560 561 562 563 564 565 566 567 568 569
	INTEL_EVENT_CONSTRAINT(0x0f, 0xf),    /* MEM_UNCORE_RETIRED.* */
	INTEL_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
	INTEL_EVENT_CONSTRAINT(0xc0, 0xf),    /* INST_RETIRED.ANY */
	INTEL_EVENT_CONSTRAINT(0xc2, 0xf),    /* UOPS_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
	INTEL_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
	INTEL_EVENT_CONSTRAINT(0xc7, 0xf),    /* SSEX_UOPS_RETIRED.* */
	INTEL_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
	INTEL_EVENT_CONSTRAINT(0xcb, 0xf),    /* MEM_LOAD_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0xf7, 0xf),    /* FP_ASSIST.* */
570 571 572
	EVENT_CONSTRAINT_END
};

573
struct event_constraint intel_westmere_pebs_event_constraints[] = {
574
	INTEL_PLD_CONSTRAINT(0x100b, 0xf),      /* MEM_INST_RETIRED.* */
575 576 577 578 579 580 581 582 583 584
	INTEL_EVENT_CONSTRAINT(0x0f, 0xf),    /* MEM_UNCORE_RETIRED.* */
	INTEL_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
	INTEL_EVENT_CONSTRAINT(0xc0, 0xf),    /* INSTR_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0xc2, 0xf),    /* UOPS_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0xc5, 0xf),    /* BR_MISP_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0xc7, 0xf),    /* SSEX_UOPS_RETIRED.* */
	INTEL_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
	INTEL_EVENT_CONSTRAINT(0xcb, 0xf),    /* MEM_LOAD_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0xf7, 0xf),    /* FP_ASSIST.* */
585 586 587
	EVENT_CONSTRAINT_END
};

588
struct event_constraint intel_snb_pebs_event_constraints[] = {
589 590 591 592 593
	INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
	INTEL_UEVENT_CONSTRAINT(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
	INTEL_UEVENT_CONSTRAINT(0x02c2, 0xf), /* UOPS_RETIRED.RETIRE_SLOTS */
	INTEL_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0xc5, 0xf),    /* BR_MISP_RETIRED.* */
594
	INTEL_PLD_CONSTRAINT(0x01cd, 0x8),    /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
595
	INTEL_PST_CONSTRAINT(0x02cd, 0x8),    /* MEM_TRANS_RETIRED.PRECISE_STORES */
596
	INTEL_EVENT_CONSTRAINT(0xd0, 0xf),    /* MEM_UOP_RETIRED.* */
597 598 599
	INTEL_EVENT_CONSTRAINT(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0xd2, 0xf),    /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
	INTEL_UEVENT_CONSTRAINT(0x02d4, 0xf), /* MEM_LOAD_UOPS_MISC_RETIRED.LLC_MISS */
600 601 602
	EVENT_CONSTRAINT_END
};

603 604 605 606 607 608
struct event_constraint intel_ivb_pebs_event_constraints[] = {
        INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
        INTEL_UEVENT_CONSTRAINT(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
        INTEL_UEVENT_CONSTRAINT(0x02c2, 0xf), /* UOPS_RETIRED.RETIRE_SLOTS */
        INTEL_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
        INTEL_EVENT_CONSTRAINT(0xc5, 0xf),    /* BR_MISP_RETIRED.* */
609
        INTEL_PLD_CONSTRAINT(0x01cd, 0x8),    /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
610
	INTEL_PST_CONSTRAINT(0x02cd, 0x8),    /* MEM_TRANS_RETIRED.PRECISE_STORES */
611 612 613 614 615 616 617
        INTEL_EVENT_CONSTRAINT(0xd0, 0xf),    /* MEM_UOP_RETIRED.* */
        INTEL_EVENT_CONSTRAINT(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
        INTEL_EVENT_CONSTRAINT(0xd2, 0xf),    /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
        INTEL_EVENT_CONSTRAINT(0xd3, 0xf),    /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
        EVENT_CONSTRAINT_END
};

618 619
struct event_constraint intel_hsw_pebs_event_constraints[] = {
	INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
620
	INTEL_PST_HSW_CONSTRAINT(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
621 622 623 624 625
	INTEL_UEVENT_CONSTRAINT(0x02c2, 0xf), /* UOPS_RETIRED.RETIRE_SLOTS */
	INTEL_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
	INTEL_UEVENT_CONSTRAINT(0x01c5, 0xf), /* BR_MISP_RETIRED.CONDITIONAL */
	INTEL_UEVENT_CONSTRAINT(0x04c5, 0xf), /* BR_MISP_RETIRED.ALL_BRANCHES */
	INTEL_UEVENT_CONSTRAINT(0x20c5, 0xf), /* BR_MISP_RETIRED.NEAR_TAKEN */
626
	INTEL_PLD_CONSTRAINT(0x01cd, 0x8),    /* MEM_TRANS_RETIRED.* */
627 628 629 630 631 632 633 634 635
	/* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
	INTEL_UEVENT_CONSTRAINT(0x11d0, 0xf),
	/* MEM_UOPS_RETIRED.STLB_MISS_STORES */
	INTEL_UEVENT_CONSTRAINT(0x12d0, 0xf),
	INTEL_UEVENT_CONSTRAINT(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
	INTEL_UEVENT_CONSTRAINT(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
	/* MEM_UOPS_RETIRED.SPLIT_STORES */
	INTEL_UEVENT_CONSTRAINT(0x42d0, 0xf),
	INTEL_UEVENT_CONSTRAINT(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
636
	INTEL_PST_HSW_CONSTRAINT(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
	INTEL_UEVENT_CONSTRAINT(0x01d1, 0xf), /* MEM_LOAD_UOPS_RETIRED.L1_HIT */
	INTEL_UEVENT_CONSTRAINT(0x02d1, 0xf), /* MEM_LOAD_UOPS_RETIRED.L2_HIT */
	INTEL_UEVENT_CONSTRAINT(0x04d1, 0xf), /* MEM_LOAD_UOPS_RETIRED.L3_HIT */
	/* MEM_LOAD_UOPS_RETIRED.HIT_LFB */
	INTEL_UEVENT_CONSTRAINT(0x40d1, 0xf),
	/* MEM_LOAD_UOPS_LLC_HIT_RETIRED.XSNP_MISS */
	INTEL_UEVENT_CONSTRAINT(0x01d2, 0xf),
	/* MEM_LOAD_UOPS_LLC_HIT_RETIRED.XSNP_HIT */
	INTEL_UEVENT_CONSTRAINT(0x02d2, 0xf),
	/* MEM_LOAD_UOPS_LLC_MISS_RETIRED.LOCAL_DRAM */
	INTEL_UEVENT_CONSTRAINT(0x01d3, 0xf),
	INTEL_UEVENT_CONSTRAINT(0x04c8, 0xf), /* HLE_RETIRED.Abort */
	INTEL_UEVENT_CONSTRAINT(0x04c9, 0xf), /* RTM_RETIRED.Abort */

	EVENT_CONSTRAINT_END
};

654
struct event_constraint *intel_pebs_constraints(struct perf_event *event)
655 656 657
{
	struct event_constraint *c;

P
Peter Zijlstra 已提交
658
	if (!event->attr.precise_ip)
659 660 661 662
		return NULL;

	if (x86_pmu.pebs_constraints) {
		for_each_event_constraint(c, x86_pmu.pebs_constraints) {
663 664
			if ((event->hw.config & c->cmask) == c->code) {
				event->hw.flags |= c->flags;
665
				return c;
666
			}
667 668 669 670 671 672
		}
	}

	return &emptyconstraint;
}

673
void intel_pmu_pebs_enable(struct perf_event *event)
674 675
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
676
	struct hw_perf_event *hwc = &event->hw;
677 678 679

	hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;

680
	cpuc->pebs_enabled |= 1ULL << hwc->idx;
681 682 683

	if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
		cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32);
684 685
	else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
		cpuc->pebs_enabled |= 1ULL << 63;
686 687
}

688
void intel_pmu_pebs_disable(struct perf_event *event)
689 690
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
691
	struct hw_perf_event *hwc = &event->hw;
692

693
	cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
694 695 696 697 698 699

	if (event->hw.constraint->flags & PERF_X86_EVENT_PEBS_LDLAT)
		cpuc->pebs_enabled &= ~(1ULL << (hwc->idx + 32));
	else if (event->hw.constraint->flags & PERF_X86_EVENT_PEBS_ST)
		cpuc->pebs_enabled &= ~(1ULL << 63);

700
	if (cpuc->enabled)
701
		wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
702 703 704 705

	hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
}

706
void intel_pmu_pebs_enable_all(void)
707 708 709 710 711 712 713
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);

	if (cpuc->pebs_enabled)
		wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
}

714
void intel_pmu_pebs_disable_all(void)
715 716 717 718 719 720 721
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);

	if (cpuc->pebs_enabled)
		wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
}

722 723 724 725 726 727
static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
	unsigned long from = cpuc->lbr_entries[0].from;
	unsigned long old_to, to = cpuc->lbr_entries[0].to;
	unsigned long ip = regs->ip;
728
	int is_64bit = 0;
729

730 731 732 733 734 735
	/*
	 * We don't need to fixup if the PEBS assist is fault like
	 */
	if (!x86_pmu.intel_cap.pebs_trap)
		return 1;

P
Peter Zijlstra 已提交
736 737 738
	/*
	 * No LBR entry, no basic block, no rewinding
	 */
739 740 741
	if (!cpuc->lbr_stack.nr || !from || !to)
		return 0;

P
Peter Zijlstra 已提交
742 743 744 745 746 747 748 749 750 751 752
	/*
	 * Basic blocks should never cross user/kernel boundaries
	 */
	if (kernel_ip(ip) != kernel_ip(to))
		return 0;

	/*
	 * unsigned math, either ip is before the start (impossible) or
	 * the basic block is larger than 1 page (sanity)
	 */
	if ((ip - to) > PAGE_SIZE)
753 754 755 756 757 758
		return 0;

	/*
	 * We sampled a branch insn, rewind using the LBR stack
	 */
	if (ip == to) {
759
		set_linear_ip(regs, from);
760 761 762 763 764 765 766 767 768 769
		return 1;
	}

	do {
		struct insn insn;
		u8 buf[MAX_INSN_SIZE];
		void *kaddr;

		old_to = to;
		if (!kernel_ip(ip)) {
P
Peter Zijlstra 已提交
770
			int bytes, size = MAX_INSN_SIZE;
771 772 773 774 775 776 777 778 779

			bytes = copy_from_user_nmi(buf, (void __user *)to, size);
			if (bytes != size)
				return 0;

			kaddr = buf;
		} else
			kaddr = (void *)to;

780 781 782 783
#ifdef CONFIG_X86_64
		is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
#endif
		insn_init(&insn, kaddr, is_64bit);
784 785 786 787 788
		insn_get_length(&insn);
		to += insn.length;
	} while (to < ip);

	if (to == ip) {
789
		set_linear_ip(regs, old_to);
790 791 792
		return 1;
	}

P
Peter Zijlstra 已提交
793 794 795 796
	/*
	 * Even though we decoded the basic block, the instruction stream
	 * never matched the given IP, either the TO or the IP got corrupted.
	 */
797 798 799
	return 0;
}

800 801 802 803 804 805 806 807 808
static inline u64 intel_hsw_weight(struct pebs_record_hsw *pebs)
{
	if (pebs->tsx_tuning) {
		union hsw_tsx_tuning tsx = { .value = pebs->tsx_tuning };
		return tsx.cycles_last_block;
	}
	return 0;
}

809 810 811 812
static void __intel_pmu_pebs_event(struct perf_event *event,
				   struct pt_regs *iregs, void *__pebs)
{
	/*
813 814
	 * We cast to the biggest pebs_record but are careful not to
	 * unconditionally access the 'extra' entries.
815
	 */
816
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
817
	struct pebs_record_hsw *pebs = __pebs;
818 819
	struct perf_sample_data data;
	struct pt_regs regs;
820
	u64 sample_type;
821
	int fll, fst;
822 823 824 825

	if (!intel_pmu_save_and_restart(event))
		return;

826
	fll = event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT;
827 828
	fst = event->hw.flags & (PERF_X86_EVENT_PEBS_ST |
				 PERF_X86_EVENT_PEBS_ST_HSW);
829

830
	perf_sample_data_init(&data, 0, event->hw.last_period);
831

832 833 834 835 836 837
	data.period = event->hw.last_period;
	sample_type = event->attr.sample_type;

	/*
	 * if PEBS-LL or PreciseStore
	 */
838
	if (fll || fst) {
839 840 841 842 843 844 845 846 847 848 849 850
		/*
		 * Use latency for weight (only avail with PEBS-LL)
		 */
		if (fll && (sample_type & PERF_SAMPLE_WEIGHT))
			data.weight = pebs->lat;

		/*
		 * data.data_src encodes the data source
		 */
		if (sample_type & PERF_SAMPLE_DATA_SRC) {
			if (fll)
				data.data_src.val = load_latency_data(pebs->dse);
851 852 853
			else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW)
				data.data_src.val =
					precise_store_data_hsw(pebs->dse);
854 855
			else
				data.data_src.val = precise_store_data(pebs->dse);
856 857 858
		}
	}

859 860 861 862 863 864 865 866 867 868 869
	/*
	 * We use the interrupt regs as a base because the PEBS record
	 * does not contain a full regs set, specifically it seems to
	 * lack segment descriptors, which get used by things like
	 * user_mode().
	 *
	 * In the simple case fix up only the IP and BP,SP regs, for
	 * PERF_SAMPLE_IP and PERF_SAMPLE_CALLCHAIN to function properly.
	 * A possible PERF_SAMPLE_REGS will have to transfer all regs.
	 */
	regs = *iregs;
870 871
	regs.flags = pebs->flags;
	set_linear_ip(&regs, pebs->ip);
872 873 874
	regs.bp = pebs->bp;
	regs.sp = pebs->sp;

875
	if (event->attr.precise_ip > 1 && x86_pmu.intel_cap.pebs_format >= 2) {
876
		regs.ip = pebs->real_ip;
877 878
		regs.flags |= PERF_EFLAGS_EXACT;
	} else if (event->attr.precise_ip > 1 && intel_pmu_pebs_fixup_ip(&regs))
879 880 881 882
		regs.flags |= PERF_EFLAGS_EXACT;
	else
		regs.flags &= ~PERF_EFLAGS_EXACT;

883
	if ((event->attr.sample_type & PERF_SAMPLE_ADDR) &&
884
	    x86_pmu.intel_cap.pebs_format >= 1)
885 886
		data.addr = pebs->dla;

887
	/* Only set the TSX weight when no memory weight was requested. */
888
	if ((event->attr.sample_type & PERF_SAMPLE_WEIGHT) && !fll &&
889 890 891
	    (x86_pmu.intel_cap.pebs_format >= 2))
		data.weight = intel_hsw_weight(pebs);

892 893 894
	if (has_branch_stack(event))
		data.br_stack = &cpuc->lbr_stack;

895
	if (perf_event_overflow(event, &data, &regs))
P
Peter Zijlstra 已提交
896
		x86_pmu_stop(event, 0);
897 898
}

899 900 901 902 903 904 905 906
static void intel_pmu_drain_pebs_core(struct pt_regs *iregs)
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
	struct debug_store *ds = cpuc->ds;
	struct perf_event *event = cpuc->events[0]; /* PMC0 only */
	struct pebs_record_core *at, *top;
	int n;

907
	if (!x86_pmu.pebs_active)
908 909 910 911 912
		return;

	at  = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base;
	top = (struct pebs_record_core *)(unsigned long)ds->pebs_index;

913 914 915 916 917 918
	/*
	 * Whatever else happens, drain the thing
	 */
	ds->pebs_index = ds->pebs_buffer_base;

	if (!test_bit(0, cpuc->active_mask))
P
Peter Zijlstra 已提交
919
		return;
920

921 922
	WARN_ON_ONCE(!event);

P
Peter Zijlstra 已提交
923
	if (!event->attr.precise_ip)
924 925 926 927 928
		return;

	n = top - at;
	if (n <= 0)
		return;
929

930 931 932 933
	/*
	 * Should not happen, we program the threshold at 1 and do not
	 * set a reset value.
	 */
934
	WARN_ONCE(n > 1, "bad leftover pebs %d\n", n);
935 936
	at += n - 1;

937
	__intel_pmu_pebs_event(event, iregs, at);
938 939
}

940
static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
941 942 943 944
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
	struct debug_store *ds = cpuc->ds;
	struct perf_event *event = NULL;
945
	void *at, *top;
946
	u64 status = 0;
947 948 949 950 951 952 953
	int bit, n;

	if (!x86_pmu.pebs_active)
		return;

	at  = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
	top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
954 955 956

	ds->pebs_index = ds->pebs_buffer_base;

957 958 959 960 961 962 963 964 965 966 967
	n = (top - at) / x86_pmu.pebs_record_size;
	if (n <= 0)
		return;

	/*
	 * Should not happen, we program the threshold at 1 and do not
	 * set a reset value.
	 */
	WARN_ONCE(n > x86_pmu.max_pebs_events,
		  "Unexpected number of pebs records %d\n", n);

968 969
	for (; at < top; at += x86_pmu.pebs_record_size) {
		struct pebs_record_nhm *p = at;
970

971 972
		for_each_set_bit(bit, (unsigned long *)&p->status,
				 x86_pmu.max_pebs_events) {
973 974
			event = cpuc->events[bit];
			if (!test_bit(bit, cpuc->active_mask))
975 976
				continue;

977 978
			WARN_ON_ONCE(!event);

P
Peter Zijlstra 已提交
979
			if (!event->attr.precise_ip)
980 981 982 983 984 985
				continue;

			if (__test_and_set_bit(bit, (unsigned long *)&status))
				continue;

			break;
986 987
		}

988
		if (!event || bit >= x86_pmu.max_pebs_events)
989 990
			continue;

991
		__intel_pmu_pebs_event(event, iregs, at);
992 993 994 995 996 997 998
	}
}

/*
 * BTS, PEBS probe and setup
 */

999
void intel_ds_init(void)
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
{
	/*
	 * No support for 32bit formats
	 */
	if (!boot_cpu_has(X86_FEATURE_DTES64))
		return;

	x86_pmu.bts  = boot_cpu_has(X86_FEATURE_BTS);
	x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS);
	if (x86_pmu.pebs) {
1010 1011
		char pebs_type = x86_pmu.intel_cap.pebs_trap ?  '+' : '-';
		int format = x86_pmu.intel_cap.pebs_format;
1012 1013 1014

		switch (format) {
		case 0:
1015
			printk(KERN_CONT "PEBS fmt0%c, ", pebs_type);
1016 1017 1018 1019 1020
			x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
			x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
			break;

		case 1:
1021
			printk(KERN_CONT "PEBS fmt1%c, ", pebs_type);
1022 1023 1024 1025
			x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
			break;

1026 1027 1028
		case 2:
			pr_cont("PEBS fmt2%c, ", pebs_type);
			x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw);
1029
			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
1030 1031
			break;

1032
		default:
1033
			printk(KERN_CONT "no PEBS fmt%d%c, ", format, pebs_type);
1034 1035 1036 1037
			x86_pmu.pebs = 0;
		}
	}
}
1038 1039 1040

void perf_restore_debug_store(void)
{
1041 1042
	struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);

1043 1044 1045
	if (!x86_pmu.bts && !x86_pmu.pebs)
		return;

1046
	wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds);
1047
}