perf_event_intel_ds.c 32.0 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

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

#define BTS_BUFFER_SIZE		(PAGE_SIZE << 4)
14
#define PEBS_BUFFER_SIZE	(PAGE_SIZE << 4)
15
#define PEBS_FIXUP_SIZE		PAGE_SIZE
16 17 18 19 20 21 22 23 24 25 26 27

/*
 * 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;
};

 */

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 72
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 */
};

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 110
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;
}

111
static u64 precise_datala_hsw(struct perf_event *event, u64 status)
112 113 114
{
	union perf_mem_data_src dse;

115 116 117 118 119 120
	dse.val = PERF_MEM_NA;

	if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW)
		dse.mem_op = PERF_MEM_OP_STORE;
	else if (event->hw.flags & PERF_X86_EVENT_PEBS_LD_HSW)
		dse.mem_op = PERF_MEM_OP_LOAD;
121 122 123 124 125 126 127 128 129

	/*
	 * L1 info only valid for following events:
	 *
	 * MEM_UOPS_RETIRED.STLB_MISS_STORES
	 * MEM_UOPS_RETIRED.LOCK_STORES
	 * MEM_UOPS_RETIRED.SPLIT_STORES
	 * MEM_UOPS_RETIRED.ALL_STORES
	 */
130 131 132 133 134 135
	if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) {
		if (status & 1)
			dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_HIT;
		else
			dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_MISS;
	}
136 137 138
	return dse.val;
}

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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
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;
}

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
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;
};

197 198 199 200
/*
 * Same as pebs_record_nhm, with two additional fields.
 */
struct pebs_record_hsw {
201 202 203 204 205 206
	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;
207
	u64 real_ip, tsx_tuning;
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
};

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;
223 224
};

225 226
#define PEBS_HSW_TSX_FLAGS	0xff00000000ULL

227
void init_debug_store_on_cpu(int cpu)
228 229 230 231 232 233 234 235 236 237 238
{
	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));
}

239
void fini_debug_store_on_cpu(int cpu)
240 241 242 243 244 245 246
{
	if (!per_cpu(cpu_hw_events, cpu).ds)
		return;

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

247 248
static DEFINE_PER_CPU(void *, insn_buffer);

249 250 251
static int alloc_pebs_buffer(int cpu)
{
	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
252
	int node = cpu_to_node(cpu);
253
	int max;
254
	void *buffer, *ibuffer;
255 256 257 258

	if (!x86_pmu.pebs)
		return 0;

259
	buffer = kzalloc_node(PEBS_BUFFER_SIZE, GFP_KERNEL, node);
260 261 262
	if (unlikely(!buffer))
		return -ENOMEM;

263 264 265 266 267 268 269 270 271 272 273 274 275
	/*
	 * HSW+ already provides us the eventing ip; no need to allocate this
	 * buffer then.
	 */
	if (x86_pmu.intel_cap.pebs_format < 2) {
		ibuffer = kzalloc_node(PEBS_FIXUP_SIZE, GFP_KERNEL, node);
		if (!ibuffer) {
			kfree(buffer);
			return -ENOMEM;
		}
		per_cpu(insn_buffer, cpu) = ibuffer;
	}

276 277 278 279 280 281 282 283 284 285
	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;

	return 0;
}

286 287 288 289 290 291 292
static void release_pebs_buffer(int cpu)
{
	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;

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

293 294 295
	kfree(per_cpu(insn_buffer, cpu));
	per_cpu(insn_buffer, cpu) = NULL;

296 297 298 299
	kfree((void *)(unsigned long)ds->pebs_buffer_base);
	ds->pebs_buffer_base = 0;
}

300 301 302
static int alloc_bts_buffer(int cpu)
{
	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
303
	int node = cpu_to_node(cpu);
304 305 306 307 308 309
	int max, thresh;
	void *buffer;

	if (!x86_pmu.bts)
		return 0;

310 311 312
	buffer = kzalloc_node(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, node);
	if (unlikely(!buffer)) {
		WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__);
313
		return -ENOMEM;
314
	}
315 316 317 318 319 320 321 322 323 324 325 326 327 328

	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;
}

329 330 331 332 333 334 335 336 337 338 339
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;
}

340 341
static int alloc_ds_buffer(int cpu)
{
342
	int node = cpu_to_node(cpu);
343 344
	struct debug_store *ds;

345
	ds = kzalloc_node(sizeof(*ds), GFP_KERNEL, node);
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
	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);
}

365
void release_ds_buffers(void)
366 367 368 369 370 371 372 373 374 375 376
{
	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) {
377 378
		release_pebs_buffer(cpu);
		release_bts_buffer(cpu);
379
		release_ds_buffer(cpu);
380 381 382 383
	}
	put_online_cpus();
}

384
void reserve_ds_buffers(void)
385
{
386 387 388 389 390
	int bts_err = 0, pebs_err = 0;
	int cpu;

	x86_pmu.bts_active = 0;
	x86_pmu.pebs_active = 0;
391 392

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

395 396 397 398 399 400
	if (!x86_pmu.bts)
		bts_err = 1;

	if (!x86_pmu.pebs)
		pebs_err = 1;

401 402 403
	get_online_cpus();

	for_each_possible_cpu(cpu) {
404 405 406 407
		if (alloc_ds_buffer(cpu)) {
			bts_err = 1;
			pebs_err = 1;
		}
408

409 410 411 412 413
		if (!bts_err && alloc_bts_buffer(cpu))
			bts_err = 1;

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

415
		if (bts_err && pebs_err)
416
			break;
417 418 419 420 421 422
	}

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

424 425 426
	if (pebs_err) {
		for_each_possible_cpu(cpu)
			release_pebs_buffer(cpu);
427 428
	}

429 430 431 432 433 434 435 436 437 438
	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;

439 440 441 442 443 444 445 446 447 448 449
		for_each_online_cpu(cpu)
			init_debug_store_on_cpu(cpu);
	}

	put_online_cpus();
}

/*
 * BTS
 */

450
struct event_constraint bts_constraint =
451
	EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0);
452

453
void intel_pmu_enable_bts(u64 config)
454 455 456 457 458
{
	unsigned long debugctlmsr;

	debugctlmsr = get_debugctlmsr();

459 460
	debugctlmsr |= DEBUGCTLMSR_TR;
	debugctlmsr |= DEBUGCTLMSR_BTS;
461 462
	if (config & ARCH_PERFMON_EVENTSEL_INT)
		debugctlmsr |= DEBUGCTLMSR_BTINT;
463 464

	if (!(config & ARCH_PERFMON_EVENTSEL_OS))
465
		debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
466 467

	if (!(config & ARCH_PERFMON_EVENTSEL_USR))
468
		debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR;
469 470 471 472

	update_debugctlmsr(debugctlmsr);
}

473
void intel_pmu_disable_bts(void)
474
{
475
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
476 477 478 479 480 481 482 483
	unsigned long debugctlmsr;

	if (!cpuc->ds)
		return;

	debugctlmsr = get_debugctlmsr();

	debugctlmsr &=
484 485
		~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT |
		  DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR);
486 487 488 489

	update_debugctlmsr(debugctlmsr);
}

490
int intel_pmu_drain_bts_buffer(void)
491
{
492
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
493 494 495 496 497 498
	struct debug_store *ds = cpuc->ds;
	struct bts_record {
		u64	from;
		u64	to;
		u64	flags;
	};
499
	struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
500 501 502 503 504 505 506
	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)
507
		return 0;
508

509
	if (!x86_pmu.bts_active)
510
		return 0;
511 512 513 514 515

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

	if (top <= at)
516
		return 0;
517

518 519
	memset(&regs, 0, sizeof(regs));

520 521
	ds->bts_index = ds->bts_buffer_base;

522
	perf_sample_data_init(&data, 0, event->hw.last_period);
523 524 525 526 527 528 529 530

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

531
	if (perf_output_begin(&handle, event, header.size * (top - at)))
532
		return 1;
533 534 535 536 537 538 539 540 541 542 543 544 545

	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;
546
	return 1;
547 548
}

549 550 551 552 553 554 555 556 557 558 559 560 561
static inline void intel_pmu_drain_pebs_buffer(void)
{
	struct pt_regs regs;

	x86_pmu.drain_pebs(&regs);
}

void intel_pmu_pebs_sched_task(struct perf_event_context *ctx, bool sched_in)
{
	if (!sched_in)
		intel_pmu_drain_pebs_buffer();
}

562 563 564
/*
 * PEBS
 */
565
struct event_constraint intel_core2_pebs_event_constraints[] = {
566 567 568 569 570
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
	INTEL_FLAGS_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1),    /* MEM_LOAD_RETIRED.* */
571 572
	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01),
573 574 575
	EVENT_CONSTRAINT_END
};

576
struct event_constraint intel_atom_pebs_event_constraints[] = {
577 578 579
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1),    /* MEM_LOAD_RETIRED.* */
580 581
	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01),
582 583 584
	EVENT_CONSTRAINT_END
};

585
struct event_constraint intel_slm_pebs_event_constraints[] = {
586 587
	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x1),
588 589
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
590 591 592
	EVENT_CONSTRAINT_END
};

593
struct event_constraint intel_nehalem_pebs_event_constraints[] = {
594
	INTEL_PLD_CONSTRAINT(0x100b, 0xf),      /* MEM_INST_RETIRED.* */
595 596 597
	INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf),    /* MEM_UNCORE_RETIRED.* */
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf),    /* INST_RETIRED.ANY */
598
	INTEL_EVENT_CONSTRAINT(0xc2, 0xf),    /* UOPS_RETIRED.* */
599 600 601 602 603 604
	INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf),    /* SSEX_UOPS_RETIRED.* */
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf),    /* MEM_LOAD_RETIRED.* */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf),    /* FP_ASSIST.* */
605 606
	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
607 608 609
	EVENT_CONSTRAINT_END
};

610
struct event_constraint intel_westmere_pebs_event_constraints[] = {
611
	INTEL_PLD_CONSTRAINT(0x100b, 0xf),      /* MEM_INST_RETIRED.* */
612 613 614
	INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf),    /* MEM_UNCORE_RETIRED.* */
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf),    /* INSTR_RETIRED.* */
615
	INTEL_EVENT_CONSTRAINT(0xc2, 0xf),    /* UOPS_RETIRED.* */
616 617 618 619 620 621
	INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xc5, 0xf),    /* BR_MISP_RETIRED.* */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf),    /* SSEX_UOPS_RETIRED.* */
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf),    /* MEM_LOAD_RETIRED.* */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf),    /* FP_ASSIST.* */
622 623
	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
624 625 626
	EVENT_CONSTRAINT_END
};

627
struct event_constraint intel_snb_pebs_event_constraints[] = {
628
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
629
	INTEL_PLD_CONSTRAINT(0x01cd, 0x8),    /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
630
	INTEL_PST_CONSTRAINT(0x02cd, 0x8),    /* MEM_TRANS_RETIRED.PRECISE_STORES */
631 632
	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
633 634 635 636
        INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf),    /* MEM_UOP_RETIRED.* */
        INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
        INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf),    /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
        INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf),    /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
637 638
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
639 640 641
	EVENT_CONSTRAINT_END
};

642
struct event_constraint intel_ivb_pebs_event_constraints[] = {
643
        INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
644
        INTEL_PLD_CONSTRAINT(0x01cd, 0x8),    /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
645
	INTEL_PST_CONSTRAINT(0x02cd, 0x8),    /* MEM_TRANS_RETIRED.PRECISE_STORES */
646 647
	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
648 649 650 651
	INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf),    /* MEM_UOP_RETIRED.* */
	INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
	INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf),    /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
	INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf),    /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
652 653
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
654 655 656
        EVENT_CONSTRAINT_END
};

657
struct event_constraint intel_hsw_pebs_event_constraints[] = {
658
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
659 660 661 662
	INTEL_PLD_CONSTRAINT(0x01cd, 0xf),    /* MEM_TRANS_RETIRED.* */
	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
663 664 665 666 667 668 669 670 671 672
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd2, 0xf),    /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd3, 0xf),    /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
673 674
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
675 676 677
	EVENT_CONSTRAINT_END
};

678
struct event_constraint *intel_pebs_constraints(struct perf_event *event)
679 680 681
{
	struct event_constraint *c;

P
Peter Zijlstra 已提交
682
	if (!event->attr.precise_ip)
683 684 685 686
		return NULL;

	if (x86_pmu.pebs_constraints) {
		for_each_event_constraint(c, x86_pmu.pebs_constraints) {
687 688
			if ((event->hw.config & c->cmask) == c->code) {
				event->hw.flags |= c->flags;
689
				return c;
690
			}
691 692 693 694 695 696
		}
	}

	return &emptyconstraint;
}

697 698 699 700 701
static inline bool pebs_is_enabled(struct cpu_hw_events *cpuc)
{
	return (cpuc->pebs_enabled & ((1ULL << MAX_PEBS_EVENTS) - 1));
}

702
void intel_pmu_pebs_enable(struct perf_event *event)
703
{
704
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
705
	struct hw_perf_event *hwc = &event->hw;
706
	struct debug_store *ds = cpuc->ds;
707 708
	bool first_pebs;
	u64 threshold;
709 710 711

	hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;

712
	first_pebs = !pebs_is_enabled(cpuc);
713
	cpuc->pebs_enabled |= 1ULL << hwc->idx;
714 715 716

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

720 721 722 723 724 725 726
	/*
	 * When the event is constrained enough we can use a larger
	 * threshold and run the event with less frequent PMI.
	 */
	if (hwc->flags & PERF_X86_EVENT_FREERUNNING) {
		threshold = ds->pebs_absolute_maximum -
			x86_pmu.max_pebs_events * x86_pmu.pebs_record_size;
727 728 729

		if (first_pebs)
			perf_sched_cb_inc(event->ctx->pmu);
730 731
	} else {
		threshold = ds->pebs_buffer_base + x86_pmu.pebs_record_size;
732 733 734 735 736 737 738 739

		/*
		 * If not all events can use larger buffer,
		 * roll back to threshold = 1
		 */
		if (!first_pebs &&
		    (ds->pebs_interrupt_threshold > threshold))
			perf_sched_cb_dec(event->ctx->pmu);
740 741
	}

742 743 744 745 746
	/* Use auto-reload if possible to save a MSR write in the PMI */
	if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
		ds->pebs_event_reset[hwc->idx] =
			(u64)(-hwc->sample_period) & x86_pmu.cntval_mask;
	}
747 748 749

	if (first_pebs || ds->pebs_interrupt_threshold > threshold)
		ds->pebs_interrupt_threshold = threshold;
750 751
}

752
void intel_pmu_pebs_disable(struct perf_event *event)
753
{
754
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
755
	struct hw_perf_event *hwc = &event->hw;
756
	struct debug_store *ds = cpuc->ds;
757

758
	cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
759

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

765 766 767 768 769 770 771
	if (ds->pebs_interrupt_threshold >
	    ds->pebs_buffer_base + x86_pmu.pebs_record_size) {
		intel_pmu_drain_pebs_buffer();
		if (!pebs_is_enabled(cpuc))
			perf_sched_cb_dec(event->ctx->pmu);
	}

772
	if (cpuc->enabled)
773
		wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
774 775 776 777

	hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
}

778
void intel_pmu_pebs_enable_all(void)
779
{
780
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
781 782 783 784 785

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

786
void intel_pmu_pebs_disable_all(void)
787
{
788
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
789 790 791 792 793

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

794 795
static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
{
796
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
797 798 799
	unsigned long from = cpuc->lbr_entries[0].from;
	unsigned long old_to, to = cpuc->lbr_entries[0].to;
	unsigned long ip = regs->ip;
800
	int is_64bit = 0;
801
	void *kaddr;
802
	int size;
803

804 805 806 807 808 809
	/*
	 * 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 已提交
810 811 812
	/*
	 * No LBR entry, no basic block, no rewinding
	 */
813 814 815
	if (!cpuc->lbr_stack.nr || !from || !to)
		return 0;

P
Peter Zijlstra 已提交
816 817 818 819 820 821 822 823 824 825
	/*
	 * 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)
	 */
826
	if ((ip - to) > PEBS_FIXUP_SIZE)
827 828 829 830 831 832
		return 0;

	/*
	 * We sampled a branch insn, rewind using the LBR stack
	 */
	if (ip == to) {
833
		set_linear_ip(regs, from);
834 835 836
		return 1;
	}

837
	size = ip - to;
838
	if (!kernel_ip(ip)) {
839
		int bytes;
840 841
		u8 *buf = this_cpu_read(insn_buffer);

842
		/* 'size' must fit our buffer, see above */
843
		bytes = copy_from_user_nmi(buf, (void __user *)to, size);
844
		if (bytes != 0)
845 846 847 848 849 850 851
			return 0;

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

852 853 854 855 856
	do {
		struct insn insn;

		old_to = to;

857 858 859
#ifdef CONFIG_X86_64
		is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
#endif
860
		insn_init(&insn, kaddr, size, is_64bit);
861
		insn_get_length(&insn);
862 863 864 865 866 867 868 869
		/*
		 * Make sure there was not a problem decoding the
		 * instruction and getting the length.  This is
		 * doubly important because we have an infinite
		 * loop if insn.length=0.
		 */
		if (!insn.length)
			break;
870

871
		to += insn.length;
872
		kaddr += insn.length;
873
		size -= insn.length;
874 875 876
	} while (to < ip);

	if (to == ip) {
877
		set_linear_ip(regs, old_to);
878 879 880
		return 1;
	}

P
Peter Zijlstra 已提交
881 882 883 884
	/*
	 * Even though we decoded the basic block, the instruction stream
	 * never matched the given IP, either the TO or the IP got corrupted.
	 */
885 886 887
	return 0;
}

888 889 890 891 892 893 894 895 896
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;
}

897 898 899 900 901 902 903 904 905 906
static inline u64 intel_hsw_transaction(struct pebs_record_hsw *pebs)
{
	u64 txn = (pebs->tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32;

	/* For RTM XABORTs also log the abort code from AX */
	if ((txn & PERF_TXN_TRANSACTION) && (pebs->ax & 1))
		txn |= ((pebs->ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
	return txn;
}

907 908 909 910
static void setup_pebs_sample_data(struct perf_event *event,
				   struct pt_regs *iregs, void *__pebs,
				   struct perf_sample_data *data,
				   struct pt_regs *regs)
911
{
912 913 914 915
#define PERF_X86_EVENT_PEBS_HSW_PREC \
		(PERF_X86_EVENT_PEBS_ST_HSW | \
		 PERF_X86_EVENT_PEBS_LD_HSW | \
		 PERF_X86_EVENT_PEBS_NA_HSW)
916
	/*
917 918
	 * We cast to the biggest pebs_record but are careful not to
	 * unconditionally access the 'extra' entries.
919
	 */
920
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
921
	struct pebs_record_hsw *pebs = __pebs;
922
	u64 sample_type;
923 924
	int fll, fst, dsrc;
	int fl = event->hw.flags;
925

926 927 928
	if (pebs == NULL)
		return;

929 930 931 932 933
	sample_type = event->attr.sample_type;
	dsrc = sample_type & PERF_SAMPLE_DATA_SRC;

	fll = fl & PERF_X86_EVENT_PEBS_LDLAT;
	fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC);
934

935
	perf_sample_data_init(data, 0, event->hw.last_period);
936

937
	data->period = event->hw.last_period;
938 939

	/*
940
	 * Use latency for weight (only avail with PEBS-LL)
941
	 */
942
	if (fll && (sample_type & PERF_SAMPLE_WEIGHT))
943
		data->weight = pebs->lat;
944 945 946 947 948 949 950 951 952 953 954 955

	/*
	 * data.data_src encodes the data source
	 */
	if (dsrc) {
		u64 val = PERF_MEM_NA;
		if (fll)
			val = load_latency_data(pebs->dse);
		else if (fst && (fl & PERF_X86_EVENT_PEBS_HSW_PREC))
			val = precise_datala_hsw(event, pebs->dse);
		else if (fst)
			val = precise_store_data(pebs->dse);
956
		data->data_src.val = val;
957 958
	}

959 960 961 962 963 964 965 966 967 968
	/*
	 * 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.
	 */
969 970 971 972 973
	*regs = *iregs;
	regs->flags = pebs->flags;
	set_linear_ip(regs, pebs->ip);
	regs->bp = pebs->bp;
	regs->sp = pebs->sp;
974

975
	if (sample_type & PERF_SAMPLE_REGS_INTR) {
976 977 978 979 980 981 982 983 984 985
		regs->ax = pebs->ax;
		regs->bx = pebs->bx;
		regs->cx = pebs->cx;
		regs->dx = pebs->dx;
		regs->si = pebs->si;
		regs->di = pebs->di;
		regs->bp = pebs->bp;
		regs->sp = pebs->sp;

		regs->flags = pebs->flags;
986
#ifndef CONFIG_X86_32
987 988 989 990 991 992 993 994
		regs->r8 = pebs->r8;
		regs->r9 = pebs->r9;
		regs->r10 = pebs->r10;
		regs->r11 = pebs->r11;
		regs->r12 = pebs->r12;
		regs->r13 = pebs->r13;
		regs->r14 = pebs->r14;
		regs->r15 = pebs->r15;
995 996 997
#endif
	}

998
	if (event->attr.precise_ip > 1 && x86_pmu.intel_cap.pebs_format >= 2) {
999 1000 1001 1002
		regs->ip = pebs->real_ip;
		regs->flags |= PERF_EFLAGS_EXACT;
	} else if (event->attr.precise_ip > 1 && intel_pmu_pebs_fixup_ip(regs))
		regs->flags |= PERF_EFLAGS_EXACT;
1003
	else
1004
		regs->flags &= ~PERF_EFLAGS_EXACT;
1005

1006
	if ((sample_type & PERF_SAMPLE_ADDR) &&
1007
	    x86_pmu.intel_cap.pebs_format >= 1)
1008
		data->addr = pebs->dla;
1009

1010 1011
	if (x86_pmu.intel_cap.pebs_format >= 2) {
		/* Only set the TSX weight when no memory weight. */
1012
		if ((sample_type & PERF_SAMPLE_WEIGHT) && !fll)
1013
			data->weight = intel_hsw_weight(pebs);
1014

1015
		if (sample_type & PERF_SAMPLE_TRANSACTION)
1016
			data->txn = intel_hsw_transaction(pebs);
1017
	}
1018

1019
	if (has_branch_stack(event))
1020 1021 1022
		data->br_stack = &cpuc->lbr_stack;
}

1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
static inline void *
get_next_pebs_record_by_bit(void *base, void *top, int bit)
{
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
	void *at;
	u64 pebs_status;

	if (base == NULL)
		return NULL;

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

		if (test_bit(bit, (unsigned long *)&p->status)) {
1037 1038 1039
			/* PEBS v3 has accurate status bits */
			if (x86_pmu.intel_cap.pebs_format >= 3)
				return at;
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053

			if (p->status == (1 << bit))
				return at;

			/* clear non-PEBS bit and re-check */
			pebs_status = p->status & cpuc->pebs_enabled;
			pebs_status &= (1ULL << MAX_PEBS_EVENTS) - 1;
			if (pebs_status == (1 << bit))
				return at;
		}
	}
	return NULL;
}

1054
static void __intel_pmu_pebs_event(struct perf_event *event,
1055 1056 1057
				   struct pt_regs *iregs,
				   void *base, void *top,
				   int bit, int count)
1058 1059 1060
{
	struct perf_sample_data data;
	struct pt_regs regs;
1061
	void *at = get_next_pebs_record_by_bit(base, top, bit);
1062

1063 1064
	if (!intel_pmu_save_and_restart(event) &&
	    !(event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD))
1065 1066
		return;

1067 1068 1069 1070 1071 1072
	while (count > 1) {
		setup_pebs_sample_data(event, iregs, at, &data, &regs);
		perf_event_output(event, &data, &regs);
		at += x86_pmu.pebs_record_size;
		at = get_next_pebs_record_by_bit(at, top, bit);
		count--;
1073 1074 1075
	}

	setup_pebs_sample_data(event, iregs, at, &data, &regs);
1076

1077 1078 1079 1080 1081
	/*
	 * All but the last records are processed.
	 * The last one is left to be able to call the overflow handler.
	 */
	if (perf_event_overflow(event, &data, &regs)) {
P
Peter Zijlstra 已提交
1082
		x86_pmu_stop(event, 0);
1083 1084 1085
		return;
	}

1086 1087
}

1088 1089
static void intel_pmu_drain_pebs_core(struct pt_regs *iregs)
{
1090
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1091 1092 1093 1094 1095
	struct debug_store *ds = cpuc->ds;
	struct perf_event *event = cpuc->events[0]; /* PMC0 only */
	struct pebs_record_core *at, *top;
	int n;

1096
	if (!x86_pmu.pebs_active)
1097 1098 1099 1100 1101
		return;

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

1102 1103 1104 1105 1106 1107
	/*
	 * Whatever else happens, drain the thing
	 */
	ds->pebs_index = ds->pebs_buffer_base;

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

1110 1111
	WARN_ON_ONCE(!event);

P
Peter Zijlstra 已提交
1112
	if (!event->attr.precise_ip)
1113 1114
		return;

1115
	n = (top - at) / x86_pmu.pebs_record_size;
1116 1117
	if (n <= 0)
		return;
1118

1119
	__intel_pmu_pebs_event(event, iregs, at, top, 0, n);
1120 1121
}

1122
static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
1123
{
1124
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1125
	struct debug_store *ds = cpuc->ds;
1126 1127 1128
	struct perf_event *event;
	void *base, *at, *top;
	short counts[MAX_PEBS_EVENTS] = {};
1129
	short error[MAX_PEBS_EVENTS] = {};
1130
	int bit, i;
1131 1132 1133 1134

	if (!x86_pmu.pebs_active)
		return;

1135
	base = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
1136
	top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
1137 1138 1139

	ds->pebs_index = ds->pebs_buffer_base;

1140
	if (unlikely(base >= top))
1141 1142
		return;

1143
	for (at = base; at < top; at += x86_pmu.pebs_record_size) {
1144
		struct pebs_record_nhm *p = at;
1145

1146 1147 1148 1149 1150 1151 1152 1153 1154
		/* PEBS v3 has accurate status bits */
		if (x86_pmu.intel_cap.pebs_format >= 3) {
			for_each_set_bit(bit, (unsigned long *)&p->status,
					 MAX_PEBS_EVENTS)
				counts[bit]++;

			continue;
		}

1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
		bit = find_first_bit((unsigned long *)&p->status,
					x86_pmu.max_pebs_events);
		if (bit >= x86_pmu.max_pebs_events)
			continue;
		if (!test_bit(bit, cpuc->active_mask))
			continue;
		/*
		 * The PEBS hardware does not deal well with the situation
		 * when events happen near to each other and multiple bits
		 * are set. But it should happen rarely.
		 *
		 * If these events include one PEBS and multiple non-PEBS
		 * events, it doesn't impact PEBS record. The record will
		 * be handled normally. (slow path)
		 *
		 * If these events include two or more PEBS events, the
		 * records for the events can be collapsed into a single
		 * one, and it's not possible to reconstruct all events
		 * that caused the PEBS record. It's called collision.
		 * If collision happened, the record will be dropped.
		 *
		 */
		if (p->status != (1 << bit)) {
			u64 pebs_status;
1179

1180 1181 1182
			/* slow path */
			pebs_status = p->status & cpuc->pebs_enabled;
			pebs_status &= (1ULL << MAX_PEBS_EVENTS) - 1;
1183 1184 1185 1186
			if (pebs_status != (1 << bit)) {
				for_each_set_bit(i, (unsigned long *)&pebs_status,
						 MAX_PEBS_EVENTS)
					error[i]++;
1187
				continue;
1188
			}
1189
		}
1190 1191
		counts[bit]++;
	}
1192

1193
	for (bit = 0; bit < x86_pmu.max_pebs_events; bit++) {
1194
		if ((counts[bit] == 0) && (error[bit] == 0))
1195
			continue;
1196 1197 1198
		event = cpuc->events[bit];
		WARN_ON_ONCE(!event);
		WARN_ON_ONCE(!event->attr.precise_ip);
1199

1200 1201 1202 1203 1204 1205 1206 1207
		/* log dropped samples number */
		if (error[bit])
			perf_log_lost_samples(event, error[bit]);

		if (counts[bit]) {
			__intel_pmu_pebs_event(event, iregs, base,
					       top, bit, counts[bit]);
		}
1208 1209 1210 1211 1212 1213 1214
	}
}

/*
 * BTS, PEBS probe and setup
 */

1215
void __init intel_ds_init(void)
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
{
	/*
	 * 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) {
1226 1227
		char pebs_type = x86_pmu.intel_cap.pebs_trap ?  '+' : '-';
		int format = x86_pmu.intel_cap.pebs_format;
1228 1229 1230

		switch (format) {
		case 0:
1231
			printk(KERN_CONT "PEBS fmt0%c, ", pebs_type);
1232 1233 1234 1235 1236
			x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
			x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
			break;

		case 1:
1237
			printk(KERN_CONT "PEBS fmt1%c, ", pebs_type);
1238 1239 1240 1241
			x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
			break;

1242 1243 1244
		case 2:
			pr_cont("PEBS fmt2%c, ", pebs_type);
			x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw);
1245
			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
1246 1247
			break;

1248
		default:
1249
			printk(KERN_CONT "no PEBS fmt%d%c, ", format, pebs_type);
1250 1251 1252 1253
			x86_pmu.pebs = 0;
		}
	}
}
1254 1255 1256

void perf_restore_debug_store(void)
{
1257 1258
	struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);

1259 1260 1261
	if (!x86_pmu.bts && !x86_pmu.pebs)
		return;

1262
	wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds);
1263
}