ds.c 46.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4
#include <linux/bitops.h>
#include <linux/types.h>
#include <linux/slab.h>
5

6
#include <asm/cpu_entry_area.h>
7
#include <asm/perf_event.h>
8
#include <asm/tlbflush.h>
9
#include <asm/insn.h>
10

11
#include "../perf_event.h"
12

13 14 15
/* Waste a full page so it can be mapped into the cpu_entry_area */
DEFINE_PER_CPU_PAGE_ALIGNED(struct debug_store, cpu_debug_store);

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

19
#define PEBS_FIXUP_SIZE		PAGE_SIZE
20 21 22 23 24 25 26 27 28 29 30 31

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

 */

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
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))
56 57
#define LEVEL(x) P(LVLNUM, x)
#define REM P(REMOTE, REMOTE)
58 59
#define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS))

60 61
/* Version for Sandy Bridge and later */
static u64 pebs_data_source[] = {
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
	P(OP, LOAD) | P(LVL, MISS) | LEVEL(L3) | P(SNOOP, NA),/* 0x00:ukn L3 */
	OP_LH | P(LVL, L1)  | LEVEL(L1) | P(SNOOP, NONE),  /* 0x01: L1 local */
	OP_LH | P(LVL, LFB) | LEVEL(LFB) | P(SNOOP, NONE), /* 0x02: LFB hit */
	OP_LH | P(LVL, L2)  | LEVEL(L2) | P(SNOOP, NONE),  /* 0x03: L2 hit */
	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, NONE),  /* 0x04: L3 hit */
	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, MISS),  /* 0x05: L3 hit, snoop miss */
	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, HIT),   /* 0x06: L3 hit, snoop hit */
	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, HITM),  /* 0x07: L3 hit, snoop hitm */
	OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HIT),  /* 0x08: L3 miss snoop hit */
	OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HITM), /* 0x09: L3 miss snoop hitm*/
	OP_LH | P(LVL, LOC_RAM)  | LEVEL(RAM) | P(SNOOP, HIT),       /* 0x0a: L3 miss, shared */
	OP_LH | P(LVL, REM_RAM1) | REM | LEVEL(L3) | P(SNOOP, HIT),  /* 0x0b: L3 miss, shared */
	OP_LH | P(LVL, LOC_RAM)  | LEVEL(RAM) | SNOOP_NONE_MISS,     /* 0x0c: L3 miss, excl */
	OP_LH | P(LVL, REM_RAM1) | LEVEL(RAM) | REM | SNOOP_NONE_MISS, /* 0x0d: L3 miss, excl */
	OP_LH | P(LVL, IO)  | LEVEL(NA) | P(SNOOP, NONE), /* 0x0e: I/O */
	OP_LH | P(LVL, UNC) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0f: uncached */
78 79
};

80 81 82
/* Patch up minor differences in the bits */
void __init intel_pmu_pebs_data_source_nhm(void)
{
83 84 85 86 87 88 89 90 91 92 93 94 95 96
	pebs_data_source[0x05] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT);
	pebs_data_source[0x06] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
	pebs_data_source[0x07] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
}

void __init intel_pmu_pebs_data_source_skl(bool pmem)
{
	u64 pmem_or_l4 = pmem ? LEVEL(PMEM) : LEVEL(L4);

	pebs_data_source[0x08] = OP_LH | pmem_or_l4 | P(SNOOP, HIT);
	pebs_data_source[0x09] = OP_LH | pmem_or_l4 | REM | P(SNOOP, HIT);
	pebs_data_source[0x0b] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, NONE);
	pebs_data_source[0x0c] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOPX, FWD);
	pebs_data_source[0x0d] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOP, HITM);
97 98
}

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
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;
}

137
static u64 precise_datala_hsw(struct perf_event *event, u64 status)
138 139 140
{
	union perf_mem_data_src dse;

141 142 143 144 145 146
	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;
147 148 149 150 151 152 153 154 155

	/*
	 * 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
	 */
156 157 158 159 160 161
	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;
	}
162 163 164
	return dse.val;
}

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;

	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
	 */
180
	if (x86_pmu.pebs_no_tlb) {
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
		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;
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
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;
};

220 221 222 223
/*
 * Same as pebs_record_nhm, with two additional fields.
 */
struct pebs_record_hsw {
224 225 226 227 228 229
	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;
230
	u64 real_ip, tsx_tuning;
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
};

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

248 249
#define PEBS_HSW_TSX_FLAGS	0xff00000000ULL

250 251 252 253 254 255 256 257 258 259 260 261 262
/* Same as HSW, plus TSC */

struct pebs_record_skl {
	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;
	u64 real_ip, tsx_tuning;
	u64 tsc;
};

263
void init_debug_store_on_cpu(int cpu)
264 265 266 267 268 269 270 271 272 273 274
{
	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));
}

275
void fini_debug_store_on_cpu(int cpu)
276 277 278 279 280 281 282
{
	if (!per_cpu(cpu_hw_events, cpu).ds)
		return;

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

283 284
static DEFINE_PER_CPU(void *, insn_buffer);

285
static void ds_update_cea(void *cea, void *addr, size_t size, pgprot_t prot)
286
{
287
	unsigned long start = (unsigned long)cea;
288 289 290 291
	phys_addr_t pa;
	size_t msz = 0;

	pa = virt_to_phys(addr);
292 293

	preempt_disable();
294 295
	for (; msz < size; msz += PAGE_SIZE, pa += PAGE_SIZE, cea += PAGE_SIZE)
		cea_set_pte(cea, pa, prot);
296 297 298 299 300 301 302

	/*
	 * This is a cross-CPU update of the cpu_entry_area, we must shoot down
	 * all TLB entries for it.
	 */
	flush_tlb_kernel_range(start, start + size);
	preempt_enable();
303 304 305 306
}

static void ds_clear_cea(void *cea, size_t size)
{
307
	unsigned long start = (unsigned long)cea;
308 309
	size_t msz = 0;

310
	preempt_disable();
311 312
	for (; msz < size; msz += PAGE_SIZE, cea += PAGE_SIZE)
		cea_set_pte(cea, 0, PAGE_NONE);
313 314 315

	flush_tlb_kernel_range(start, start + size);
	preempt_enable();
316 317 318 319 320
}

static void *dsalloc_pages(size_t size, gfp_t flags, int cpu)
{
	unsigned int order = get_order(size);
321
	int node = cpu_to_node(cpu);
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	struct page *page;

	page = __alloc_pages_node(node, flags | __GFP_ZERO, order);
	return page ? page_address(page) : NULL;
}

static void dsfree_pages(const void *buffer, size_t size)
{
	if (buffer)
		free_pages((unsigned long)buffer, get_order(size));
}

static int alloc_pebs_buffer(int cpu)
{
	struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
	struct debug_store *ds = hwev->ds;
	size_t bsiz = x86_pmu.pebs_buffer_size;
	int max, node = cpu_to_node(cpu);
	void *buffer, *ibuffer, *cea;
341 342 343 344

	if (!x86_pmu.pebs)
		return 0;

345
	buffer = dsalloc_pages(bsiz, GFP_KERNEL, cpu);
346 347 348
	if (unlikely(!buffer))
		return -ENOMEM;

349 350 351 352 353 354 355
	/*
	 * 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) {
356
			dsfree_pages(buffer, bsiz);
357 358 359 360
			return -ENOMEM;
		}
		per_cpu(insn_buffer, cpu) = ibuffer;
	}
361 362 363 364 365
	hwev->ds_pebs_vaddr = buffer;
	/* Update the cpu entry area mapping */
	cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer;
	ds->pebs_buffer_base = (unsigned long) cea;
	ds_update_cea(cea, buffer, bsiz, PAGE_KERNEL);
366
	ds->pebs_index = ds->pebs_buffer_base;
367 368
	max = x86_pmu.pebs_record_size * (bsiz / x86_pmu.pebs_record_size);
	ds->pebs_absolute_maximum = ds->pebs_buffer_base + max;
369 370 371
	return 0;
}

372 373
static void release_pebs_buffer(int cpu)
{
374 375
	struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
	void *cea;
376

377
	if (!x86_pmu.pebs)
378 379
		return;

380 381 382
	kfree(per_cpu(insn_buffer, cpu));
	per_cpu(insn_buffer, cpu) = NULL;

383 384 385 386 387
	/* Clear the fixmap */
	cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer;
	ds_clear_cea(cea, x86_pmu.pebs_buffer_size);
	dsfree_pages(hwev->ds_pebs_vaddr, x86_pmu.pebs_buffer_size);
	hwev->ds_pebs_vaddr = NULL;
388 389
}

390 391
static int alloc_bts_buffer(int cpu)
{
392 393 394 395
	struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
	struct debug_store *ds = hwev->ds;
	void *buffer, *cea;
	int max;
396 397 398 399

	if (!x86_pmu.bts)
		return 0;

400
	buffer = dsalloc_pages(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, cpu);
401 402
	if (unlikely(!buffer)) {
		WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__);
403
		return -ENOMEM;
404
	}
405 406 407 408 409
	hwev->ds_bts_vaddr = buffer;
	/* Update the fixmap */
	cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer;
	ds->bts_buffer_base = (unsigned long) cea;
	ds_update_cea(cea, buffer, BTS_BUFFER_SIZE, PAGE_KERNEL);
410
	ds->bts_index = ds->bts_buffer_base;
411 412 413 414 415
	max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE;
	ds->bts_absolute_maximum = ds->bts_buffer_base +
					max * BTS_RECORD_SIZE;
	ds->bts_interrupt_threshold = ds->bts_absolute_maximum -
					(max / 16) * BTS_RECORD_SIZE;
416 417 418
	return 0;
}

419 420
static void release_bts_buffer(int cpu)
{
421 422
	struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
	void *cea;
423

424
	if (!x86_pmu.bts)
425 426
		return;

427 428 429 430 431
	/* Clear the fixmap */
	cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer;
	ds_clear_cea(cea, BTS_BUFFER_SIZE);
	dsfree_pages(hwev->ds_bts_vaddr, BTS_BUFFER_SIZE);
	hwev->ds_bts_vaddr = NULL;
432 433
}

434 435
static int alloc_ds_buffer(int cpu)
{
436
	struct debug_store *ds = &get_cpu_entry_area(cpu)->cpu_debug_store;
437

438
	memset(ds, 0, sizeof(*ds));
439 440 441 442 443 444 445 446 447
	per_cpu(cpu_hw_events, cpu).ds = ds;
	return 0;
}

static void release_ds_buffer(int cpu)
{
	per_cpu(cpu_hw_events, cpu).ds = NULL;
}

448
void release_ds_buffers(void)
449 450 451 452 453 454
{
	int cpu;

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

455 456 457 458 459 460 461 462 463
	for_each_possible_cpu(cpu)
		release_ds_buffer(cpu);

	for_each_possible_cpu(cpu) {
		/*
		 * Again, ignore errors from offline CPUs, they will no longer
		 * observe cpu_hw_events.ds and not program the DS_AREA when
		 * they come up.
		 */
464
		fini_debug_store_on_cpu(cpu);
465
	}
466 467

	for_each_possible_cpu(cpu) {
468 469
		release_pebs_buffer(cpu);
		release_bts_buffer(cpu);
470 471 472
	}
}

473
void reserve_ds_buffers(void)
474
{
475 476 477 478 479
	int bts_err = 0, pebs_err = 0;
	int cpu;

	x86_pmu.bts_active = 0;
	x86_pmu.pebs_active = 0;
480 481

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

484 485 486 487 488 489
	if (!x86_pmu.bts)
		bts_err = 1;

	if (!x86_pmu.pebs)
		pebs_err = 1;

490
	for_each_possible_cpu(cpu) {
491 492 493 494
		if (alloc_ds_buffer(cpu)) {
			bts_err = 1;
			pebs_err = 1;
		}
495

496 497 498 499 500
		if (!bts_err && alloc_bts_buffer(cpu))
			bts_err = 1;

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

502
		if (bts_err && pebs_err)
503
			break;
504 505 506 507 508 509
	}

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

511 512 513
	if (pebs_err) {
		for_each_possible_cpu(cpu)
			release_pebs_buffer(cpu);
514 515
	}

516 517 518 519 520 521 522 523 524 525
	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;

526 527 528 529 530
		for_each_possible_cpu(cpu) {
			/*
			 * Ignores wrmsr_on_cpu() errors for offline CPUs they
			 * will get this call through intel_pmu_cpu_starting().
			 */
531
			init_debug_store_on_cpu(cpu);
532
		}
533 534 535 536 537 538 539
	}
}

/*
 * BTS
 */

540
struct event_constraint bts_constraint =
541
	EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0);
542

543
void intel_pmu_enable_bts(u64 config)
544 545 546 547 548
{
	unsigned long debugctlmsr;

	debugctlmsr = get_debugctlmsr();

549 550
	debugctlmsr |= DEBUGCTLMSR_TR;
	debugctlmsr |= DEBUGCTLMSR_BTS;
551 552
	if (config & ARCH_PERFMON_EVENTSEL_INT)
		debugctlmsr |= DEBUGCTLMSR_BTINT;
553 554

	if (!(config & ARCH_PERFMON_EVENTSEL_OS))
555
		debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
556 557

	if (!(config & ARCH_PERFMON_EVENTSEL_USR))
558
		debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR;
559 560 561 562

	update_debugctlmsr(debugctlmsr);
}

563
void intel_pmu_disable_bts(void)
564
{
565
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
566 567 568 569 570 571 572 573
	unsigned long debugctlmsr;

	if (!cpuc->ds)
		return;

	debugctlmsr = get_debugctlmsr();

	debugctlmsr &=
574 575
		~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT |
		  DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR);
576 577 578 579

	update_debugctlmsr(debugctlmsr);
}

580
int intel_pmu_drain_bts_buffer(void)
581
{
582
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
583 584 585 586 587 588
	struct debug_store *ds = cpuc->ds;
	struct bts_record {
		u64	from;
		u64	to;
		u64	flags;
	};
589
	struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
590
	struct bts_record *at, *base, *top;
591 592 593
	struct perf_output_handle handle;
	struct perf_event_header header;
	struct perf_sample_data data;
594
	unsigned long skip = 0;
595 596 597
	struct pt_regs regs;

	if (!event)
598
		return 0;
599

600
	if (!x86_pmu.bts_active)
601
		return 0;
602

603 604
	base = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
	top  = (struct bts_record *)(unsigned long)ds->bts_index;
605

606
	if (top <= base)
607
		return 0;
608

609 610
	memset(&regs, 0, sizeof(regs));

611 612
	ds->bts_index = ds->bts_buffer_base;

613
	perf_sample_data_init(&data, 0, event->hw.last_period);
614

615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
	/*
	 * BTS leaks kernel addresses in branches across the cpl boundary,
	 * such as traps or system calls, so unless the user is asking for
	 * kernel tracing (and right now it's not possible), we'd need to
	 * filter them out. But first we need to count how many of those we
	 * have in the current batch. This is an extra O(n) pass, however,
	 * it's much faster than the other one especially considering that
	 * n <= 2560 (BTS_BUFFER_SIZE / BTS_RECORD_SIZE * 15/16; see the
	 * alloc_bts_buffer()).
	 */
	for (at = base; at < top; at++) {
		/*
		 * Note that right now *this* BTS code only works if
		 * attr::exclude_kernel is set, but let's keep this extra
		 * check here in case that changes.
		 */
		if (event->attr.exclude_kernel &&
		    (kernel_ip(at->from) || kernel_ip(at->to)))
			skip++;
	}

636 637 638 639 640
	/*
	 * Prepare a generic sample, i.e. fill in the invariant fields.
	 * We will overwrite the from and to address before we output
	 * the sample.
	 */
P
Peter Zijlstra 已提交
641
	rcu_read_lock();
642 643
	perf_prepare_sample(&header, &data, event, &regs);

644 645
	if (perf_output_begin(&handle, event, header.size *
			      (top - base - skip)))
P
Peter Zijlstra 已提交
646
		goto unlock;
647

648 649 650 651 652 653
	for (at = base; at < top; at++) {
		/* Filter out any records that contain kernel addresses. */
		if (event->attr.exclude_kernel &&
		    (kernel_ip(at->from) || kernel_ip(at->to)))
			continue;

654 655 656 657 658 659 660 661 662 663 664
		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;
P
Peter Zijlstra 已提交
665 666
unlock:
	rcu_read_unlock();
667
	return 1;
668 669
}

670 671 672 673 674 675 676
static inline void intel_pmu_drain_pebs_buffer(void)
{
	struct pt_regs regs;

	x86_pmu.drain_pebs(&regs);
}

677 678 679
/*
 * PEBS
 */
680
struct event_constraint intel_core2_pebs_event_constraints[] = {
681 682 683 684 685
	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.* */
686 687
	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01),
688 689 690
	EVENT_CONSTRAINT_END
};

691
struct event_constraint intel_atom_pebs_event_constraints[] = {
692 693 694
	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.* */
695 696
	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01),
697 698
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
699 700 701
	EVENT_CONSTRAINT_END
};

702
struct event_constraint intel_slm_pebs_event_constraints[] = {
703 704
	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x1),
705 706
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
707 708 709
	EVENT_CONSTRAINT_END
};

710 711 712 713 714 715
struct event_constraint intel_glm_pebs_event_constraints[] = {
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
	EVENT_CONSTRAINT_END
};

716
struct event_constraint intel_nehalem_pebs_event_constraints[] = {
717
	INTEL_PLD_CONSTRAINT(0x100b, 0xf),      /* MEM_INST_RETIRED.* */
718 719 720
	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 */
721
	INTEL_EVENT_CONSTRAINT(0xc2, 0xf),    /* UOPS_RETIRED.* */
722 723 724 725 726 727
	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.* */
728 729
	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
730 731 732
	EVENT_CONSTRAINT_END
};

733
struct event_constraint intel_westmere_pebs_event_constraints[] = {
734
	INTEL_PLD_CONSTRAINT(0x100b, 0xf),      /* MEM_INST_RETIRED.* */
735 736 737
	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.* */
738
	INTEL_EVENT_CONSTRAINT(0xc2, 0xf),    /* UOPS_RETIRED.* */
739 740 741 742 743 744
	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.* */
745 746
	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
747 748 749
	EVENT_CONSTRAINT_END
};

750
struct event_constraint intel_snb_pebs_event_constraints[] = {
751
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
752
	INTEL_PLD_CONSTRAINT(0x01cd, 0x8),    /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
753
	INTEL_PST_CONSTRAINT(0x02cd, 0x8),    /* MEM_TRANS_RETIRED.PRECISE_STORES */
754 755
	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
756 757 758 759
        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.* */
760 761
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
762 763 764
	EVENT_CONSTRAINT_END
};

765
struct event_constraint intel_ivb_pebs_event_constraints[] = {
766
        INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
767
        INTEL_PLD_CONSTRAINT(0x01cd, 0x8),    /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
768
	INTEL_PST_CONSTRAINT(0x02cd, 0x8),    /* MEM_TRANS_RETIRED.PRECISE_STORES */
769 770
	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
771 772
	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
773 774 775 776
	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.* */
777 778
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
779 780 781
        EVENT_CONSTRAINT_END
};

782
struct event_constraint intel_hsw_pebs_event_constraints[] = {
783
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
784 785 786
	INTEL_PLD_CONSTRAINT(0x01cd, 0xf),    /* MEM_TRANS_RETIRED.* */
	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
787 788
	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
789
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
790 791 792 793 794 795 796 797 798 799
	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.* */
800 801 802 803 804
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
	EVENT_CONSTRAINT_END
};

805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
struct event_constraint intel_bdw_pebs_event_constraints[] = {
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
	INTEL_PLD_CONSTRAINT(0x01cd, 0xf),    /* MEM_TRANS_RETIRED.* */
	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf),    /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf),    /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
	EVENT_CONSTRAINT_END
};


829 830
struct event_constraint intel_skl_pebs_event_constraints[] = {
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x1c0, 0x2),	/* INST_RETIRED.PREC_DIST */
831 832
	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
833 834
	/* INST_RETIRED.TOTAL_CYCLES_PS (inv=1, cmask=16) (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
835 836 837 838 839 840 841 842 843 844 845 846
	INTEL_PLD_CONSTRAINT(0x1cd, 0xf),		      /* MEM_TRANS_RETIRED.* */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x22d0, 0xf), /* MEM_INST_RETIRED.LOCK_STORES */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */
	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf),    /* MEM_LOAD_RETIRED.* */
	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf),    /* MEM_LOAD_L3_HIT_RETIRED.* */
	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf),    /* MEM_LOAD_L3_MISS_RETIRED.* */
847 848
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
849 850 851
	EVENT_CONSTRAINT_END
};

852
struct event_constraint *intel_pebs_constraints(struct perf_event *event)
853 854 855
{
	struct event_constraint *c;

P
Peter Zijlstra 已提交
856
	if (!event->attr.precise_ip)
857 858 859 860
		return NULL;

	if (x86_pmu.pebs_constraints) {
		for_each_event_constraint(c, x86_pmu.pebs_constraints) {
861 862
			if ((event->hw.config & c->cmask) == c->code) {
				event->hw.flags |= c->flags;
863
				return c;
864
			}
865 866 867
		}
	}

868 869 870 871 872 873 874
	/*
	 * Extended PEBS support
	 * Makes the PEBS code search the normal constraints.
	 */
	if (x86_pmu.flags & PMU_FL_PEBS_ALL)
		return NULL;

875 876 877
	return &emptyconstraint;
}

878 879 880 881 882 883 884 885 886 887
/*
 * We need the sched_task callback even for per-cpu events when we use
 * the large interrupt threshold, such that we can provide PID and TID
 * to PEBS samples.
 */
static inline bool pebs_needs_sched_cb(struct cpu_hw_events *cpuc)
{
	return cpuc->n_pebs && (cpuc->n_pebs == cpuc->n_large_pebs);
}

888 889 890 891 892 893 894 895
void intel_pmu_pebs_sched_task(struct perf_event_context *ctx, bool sched_in)
{
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);

	if (!sched_in && pebs_needs_sched_cb(cpuc))
		intel_pmu_drain_pebs_buffer();
}

896 897 898 899
static inline void pebs_update_threshold(struct cpu_hw_events *cpuc)
{
	struct debug_store *ds = cpuc->ds;
	u64 threshold;
900 901 902 903 904 905
	int reserved;

	if (x86_pmu.flags & PMU_FL_PEBS_ALL)
		reserved = x86_pmu.max_pebs_events + x86_pmu.num_counters_fixed;
	else
		reserved = x86_pmu.max_pebs_events;
906 907 908

	if (cpuc->n_pebs == cpuc->n_large_pebs) {
		threshold = ds->pebs_absolute_maximum -
909
			reserved * x86_pmu.pebs_record_size;
910 911 912 913 914 915 916 917 918 919
	} else {
		threshold = ds->pebs_buffer_base + x86_pmu.pebs_record_size;
	}

	ds->pebs_interrupt_threshold = threshold;
}

static void
pebs_update_state(bool needed_cb, struct cpu_hw_events *cpuc, struct pmu *pmu)
{
920 921 922 923 924 925 926
	/*
	 * Make sure we get updated with the first PEBS
	 * event. It will trigger also during removal, but
	 * that does not hurt:
	 */
	bool update = cpuc->n_pebs == 1;

927 928 929 930 931 932
	if (needed_cb != pebs_needs_sched_cb(cpuc)) {
		if (!needed_cb)
			perf_sched_cb_inc(pmu);
		else
			perf_sched_cb_dec(pmu);

933
		update = true;
934
	}
935 936 937

	if (update)
		pebs_update_threshold(cpuc);
938 939
}

940
void intel_pmu_pebs_add(struct perf_event *event)
941
{
942 943 944 945 946
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
	struct hw_perf_event *hwc = &event->hw;
	bool needed_cb = pebs_needs_sched_cb(cpuc);

	cpuc->n_pebs++;
947
	if (hwc->flags & PERF_X86_EVENT_LARGE_PEBS)
948 949 950
		cpuc->n_large_pebs++;

	pebs_update_state(needed_cb, cpuc, event->ctx->pmu);
951 952
}

953
void intel_pmu_pebs_enable(struct perf_event *event)
954
{
955
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
956
	struct hw_perf_event *hwc = &event->hw;
957
	struct debug_store *ds = cpuc->ds;
958

959 960
	hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;

961
	cpuc->pebs_enabled |= 1ULL << hwc->idx;
962 963 964

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

968
	/*
969 970
	 * Use auto-reload if possible to save a MSR write in the PMI.
	 * This must be done in pmu::start(), because PERF_EVENT_IOC_PERIOD.
971
	 */
972
	if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
973 974 975 976 977
		unsigned int idx = hwc->idx;

		if (idx >= INTEL_PMC_IDX_FIXED)
			idx = MAX_PEBS_EVENTS + (idx - INTEL_PMC_IDX_FIXED);
		ds->pebs_event_reset[idx] =
978
			(u64)(-hwc->sample_period) & x86_pmu.cntval_mask;
979 980
	} else {
		ds->pebs_event_reset[hwc->idx] = 0;
981
	}
982 983
}

984
void intel_pmu_pebs_del(struct perf_event *event)
985 986 987 988 989 990
{
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
	struct hw_perf_event *hwc = &event->hw;
	bool needed_cb = pebs_needs_sched_cb(cpuc);

	cpuc->n_pebs--;
991
	if (hwc->flags & PERF_X86_EVENT_LARGE_PEBS)
992
		cpuc->n_large_pebs--;
993

994
	pebs_update_state(needed_cb, cpuc, event->ctx->pmu);
995 996
}

997
void intel_pmu_pebs_disable(struct perf_event *event)
998
{
999
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1000
	struct hw_perf_event *hwc = &event->hw;
1001

1002
	if (cpuc->n_pebs == cpuc->n_large_pebs)
1003
		intel_pmu_drain_pebs_buffer();
1004

1005
	cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
1006

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

1012
	if (cpuc->enabled)
1013
		wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
1014 1015 1016 1017

	hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
}

1018
void intel_pmu_pebs_enable_all(void)
1019
{
1020
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1021 1022 1023 1024 1025

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

1026
void intel_pmu_pebs_disable_all(void)
1027
{
1028
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1029 1030 1031 1032 1033

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

1034 1035
static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
{
1036
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1037 1038 1039
	unsigned long from = cpuc->lbr_entries[0].from;
	unsigned long old_to, to = cpuc->lbr_entries[0].to;
	unsigned long ip = regs->ip;
1040
	int is_64bit = 0;
1041
	void *kaddr;
1042
	int size;
1043

1044 1045 1046 1047 1048 1049
	/*
	 * 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 已提交
1050 1051 1052
	/*
	 * No LBR entry, no basic block, no rewinding
	 */
1053 1054 1055
	if (!cpuc->lbr_stack.nr || !from || !to)
		return 0;

P
Peter Zijlstra 已提交
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
	/*
	 * 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)
	 */
1066
	if ((ip - to) > PEBS_FIXUP_SIZE)
1067 1068 1069 1070 1071 1072
		return 0;

	/*
	 * We sampled a branch insn, rewind using the LBR stack
	 */
	if (ip == to) {
1073
		set_linear_ip(regs, from);
1074 1075 1076
		return 1;
	}

1077
	size = ip - to;
1078
	if (!kernel_ip(ip)) {
1079
		int bytes;
1080 1081
		u8 *buf = this_cpu_read(insn_buffer);

1082
		/* 'size' must fit our buffer, see above */
1083
		bytes = copy_from_user_nmi(buf, (void __user *)to, size);
1084
		if (bytes != 0)
1085 1086 1087 1088 1089 1090 1091
			return 0;

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

1092 1093 1094 1095 1096
	do {
		struct insn insn;

		old_to = to;

1097 1098 1099
#ifdef CONFIG_X86_64
		is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
#endif
1100
		insn_init(&insn, kaddr, size, is_64bit);
1101
		insn_get_length(&insn);
1102 1103 1104 1105 1106 1107 1108 1109
		/*
		 * 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;
1110

1111
		to += insn.length;
1112
		kaddr += insn.length;
1113
		size -= insn.length;
1114 1115 1116
	} while (to < ip);

	if (to == ip) {
1117
		set_linear_ip(regs, old_to);
1118 1119 1120
		return 1;
	}

P
Peter Zijlstra 已提交
1121 1122 1123 1124
	/*
	 * Even though we decoded the basic block, the instruction stream
	 * never matched the given IP, either the TO or the IP got corrupted.
	 */
1125 1126 1127
	return 0;
}

1128
static inline u64 intel_get_tsx_weight(u64 tsx_tuning)
1129
{
1130 1131
	if (tsx_tuning) {
		union hsw_tsx_tuning tsx = { .value = tsx_tuning };
1132 1133 1134 1135 1136
		return tsx.cycles_last_block;
	}
	return 0;
}

1137
static inline u64 intel_get_tsx_transaction(u64 tsx_tuning, u64 ax)
1138
{
1139
	u64 txn = (tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32;
1140 1141

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

1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
#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)

static u64 get_data_src(struct perf_event *event, u64 aux)
{
	u64 val = PERF_MEM_NA;
	int fl = event->hw.flags;
	bool fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC);

	if (fl & PERF_X86_EVENT_PEBS_LDLAT)
		val = load_latency_data(aux);
	else if (fst && (fl & PERF_X86_EVENT_PEBS_HSW_PREC))
		val = precise_datala_hsw(event, aux);
	else if (fst)
		val = precise_store_data(aux);
	return val;
}

1167 1168 1169 1170
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)
1171 1172
{
	/*
1173 1174
	 * We cast to the biggest pebs_record but are careful not to
	 * unconditionally access the 'extra' entries.
1175
	 */
1176
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1177
	struct pebs_record_skl *pebs = __pebs;
1178
	u64 sample_type;
1179
	int fll;
1180

1181 1182 1183
	if (pebs == NULL)
		return;

1184
	sample_type = event->attr.sample_type;
1185
	fll = event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT;
1186

1187
	perf_sample_data_init(data, 0, event->hw.last_period);
1188

1189
	data->period = event->hw.last_period;
1190 1191

	/*
1192
	 * Use latency for weight (only avail with PEBS-LL)
1193
	 */
1194
	if (fll && (sample_type & PERF_SAMPLE_WEIGHT))
1195
		data->weight = pebs->lat;
1196 1197 1198 1199

	/*
	 * data.data_src encodes the data source
	 */
1200 1201
	if (sample_type & PERF_SAMPLE_DATA_SRC)
		data->data_src.val = get_data_src(event, pebs->dse);
1202

1203 1204 1205
	/*
	 * We must however always use iregs for the unwinder to stay sane; the
	 * record BP,SP,IP can point into thin air when the record is from a
I
Ingo Molnar 已提交
1206
	 * previous PMI context or an (I)RET happened between the record and
1207 1208 1209 1210 1211
	 * PMI.
	 */
	if (sample_type & PERF_SAMPLE_CALLCHAIN)
		data->callchain = perf_callchain(event, iregs);

1212
	/*
1213 1214 1215
	 * 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().
1216
	 *
1217
	 * In the simple case fix up only the IP for PERF_SAMPLE_IP.
1218
	 */
1219
	*regs = *iregs;
1220 1221 1222 1223 1224 1225 1226

	/*
	 * Initialize regs_>flags from PEBS,
	 * Clear exact bit (which uses x86 EFLAGS Reserved bit 3),
	 * i.e., do not rely on it being zero:
	 */
	regs->flags = pebs->flags & ~PERF_EFLAGS_EXACT;
1227

1228
	if (sample_type & PERF_SAMPLE_REGS_INTR) {
1229 1230 1231 1232 1233 1234 1235
		regs->ax = pebs->ax;
		regs->bx = pebs->bx;
		regs->cx = pebs->cx;
		regs->dx = pebs->dx;
		regs->si = pebs->si;
		regs->di = pebs->di;

1236 1237
		regs->bp = pebs->bp;
		regs->sp = pebs->sp;
1238

1239
#ifndef CONFIG_X86_32
1240 1241 1242 1243 1244 1245 1246 1247
		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;
1248 1249 1250
#endif
	}

1251
	if (event->attr.precise_ip > 1) {
1252 1253 1254 1255 1256
		/*
		 * Haswell and later processors have an 'eventing IP'
		 * (real IP) which fixes the off-by-1 skid in hardware.
		 * Use it when precise_ip >= 2 :
		 */
1257 1258 1259 1260
		if (x86_pmu.intel_cap.pebs_format >= 2) {
			set_linear_ip(regs, pebs->real_ip);
			regs->flags |= PERF_EFLAGS_EXACT;
		} else {
1261
			/* Otherwise, use PEBS off-by-1 IP: */
1262 1263
			set_linear_ip(regs, pebs->ip);

1264 1265 1266 1267 1268
			/*
			 * With precise_ip >= 2, try to fix up the off-by-1 IP
			 * using the LBR. If successful, the fixup function
			 * corrects regs->ip and calls set_linear_ip() on regs:
			 */
1269 1270 1271
			if (intel_pmu_pebs_fixup_ip(regs))
				regs->flags |= PERF_EFLAGS_EXACT;
		}
1272 1273 1274 1275 1276
	} else {
		/*
		 * When precise_ip == 1, return the PEBS off-by-1 IP,
		 * no fixup attempted:
		 */
1277
		set_linear_ip(regs, pebs->ip);
1278
	}
1279

1280

1281
	if ((sample_type & (PERF_SAMPLE_ADDR | PERF_SAMPLE_PHYS_ADDR)) &&
1282
	    x86_pmu.intel_cap.pebs_format >= 1)
1283
		data->addr = pebs->dla;
1284

1285 1286
	if (x86_pmu.intel_cap.pebs_format >= 2) {
		/* Only set the TSX weight when no memory weight. */
1287
		if ((sample_type & PERF_SAMPLE_WEIGHT) && !fll)
1288
			data->weight = intel_get_tsx_weight(pebs->tsx_tuning);
1289

1290
		if (sample_type & PERF_SAMPLE_TRANSACTION)
1291 1292
			data->txn = intel_get_tsx_transaction(pebs->tsx_tuning,
							      pebs->ax);
1293
	}
1294

1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
	/*
	 * v3 supplies an accurate time stamp, so we use that
	 * for the time stamp.
	 *
	 * We can only do this for the default trace clock.
	 */
	if (x86_pmu.intel_cap.pebs_format >= 3 &&
		event->attr.use_clockid == 0)
		data->time = native_sched_clock_from_tsc(pebs->tsc);

1305
	if (has_branch_stack(event))
1306 1307 1308
		data->br_stack = &cpuc->lbr_stack;
}

1309 1310 1311 1312 1313 1314 1315
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;

1316 1317 1318 1319 1320 1321 1322
	/*
	 * fmt0 does not have a status bitfield (does not use
	 * perf_record_nhm format)
	 */
	if (x86_pmu.intel_cap.pebs_format < 1)
		return base;

1323 1324 1325 1326 1327 1328 1329
	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)) {
1330 1331 1332
			/* PEBS v3 has accurate status bits */
			if (x86_pmu.intel_cap.pebs_format >= 3)
				return at;
1333 1334 1335 1336 1337 1338

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

			/* clear non-PEBS bit and re-check */
			pebs_status = p->status & cpuc->pebs_enabled;
1339
			pebs_status &= PEBS_COUNTER_MASK;
1340 1341 1342 1343 1344 1345 1346
			if (pebs_status == (1 << bit))
				return at;
		}
	}
	return NULL;
}

1347 1348 1349 1350 1351 1352 1353 1354 1355
void intel_pmu_auto_reload_read(struct perf_event *event)
{
	WARN_ON(!(event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD));

	perf_pmu_disable(event->pmu);
	intel_pmu_drain_pebs_buffer();
	perf_pmu_enable(event->pmu);
}

1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
/*
 * Special variant of intel_pmu_save_and_restart() for auto-reload.
 */
static int
intel_pmu_save_and_restart_reload(struct perf_event *event, int count)
{
	struct hw_perf_event *hwc = &event->hw;
	int shift = 64 - x86_pmu.cntval_bits;
	u64 period = hwc->sample_period;
	u64 prev_raw_count, new_raw_count;
	s64 new, old;

	WARN_ON(!period);

	/*
	 * drain_pebs() only happens when the PMU is disabled.
	 */
	WARN_ON(this_cpu_read(cpu_hw_events.enabled));

	prev_raw_count = local64_read(&hwc->prev_count);
	rdpmcl(hwc->event_base_rdpmc, new_raw_count);
	local64_set(&hwc->prev_count, new_raw_count);

	/*
	 * Since the counter increments a negative counter value and
	 * overflows on the sign switch, giving the interval:
	 *
	 *   [-period, 0]
	 *
	 * the difference between two consequtive reads is:
	 *
	 *   A) value2 - value1;
	 *      when no overflows have happened in between,
	 *
	 *   B) (0 - value1) + (value2 - (-period));
	 *      when one overflow happened in between,
	 *
	 *   C) (0 - value1) + (n - 1) * (period) + (value2 - (-period));
	 *      when @n overflows happened in between.
	 *
	 * Here A) is the obvious difference, B) is the extension to the
	 * discrete interval, where the first term is to the top of the
	 * interval and the second term is from the bottom of the next
	 * interval and C) the extension to multiple intervals, where the
	 * middle term is the whole intervals covered.
	 *
	 * An equivalent of C, by reduction, is:
	 *
	 *   value2 - value1 + n * period
	 */
	new = ((s64)(new_raw_count << shift) >> shift);
	old = ((s64)(prev_raw_count << shift) >> shift);
	local64_add(new - old + count * period, &event->count);

	perf_event_update_userpage(event);

	return 0;
}

1415
static void __intel_pmu_pebs_event(struct perf_event *event,
1416 1417 1418
				   struct pt_regs *iregs,
				   void *base, void *top,
				   int bit, int count)
1419
{
1420
	struct hw_perf_event *hwc = &event->hw;
1421 1422
	struct perf_sample_data data;
	struct pt_regs regs;
1423
	void *at = get_next_pebs_record_by_bit(base, top, bit);
1424

1425 1426 1427 1428 1429 1430 1431 1432 1433
	if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
		/*
		 * Now, auto-reload is only enabled in fixed period mode.
		 * The reload value is always hwc->sample_period.
		 * May need to change it, if auto-reload is enabled in
		 * freq mode later.
		 */
		intel_pmu_save_and_restart_reload(event, count);
	} else if (!intel_pmu_save_and_restart(event))
1434 1435
		return;

1436 1437 1438 1439 1440 1441
	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--;
1442 1443 1444
	}

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

1446 1447 1448 1449 1450
	/*
	 * 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 已提交
1451
		x86_pmu_stop(event, 0);
1452 1453 1454
		return;
	}

1455 1456
}

1457 1458
static void intel_pmu_drain_pebs_core(struct pt_regs *iregs)
{
1459
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1460 1461 1462 1463 1464
	struct debug_store *ds = cpuc->ds;
	struct perf_event *event = cpuc->events[0]; /* PMC0 only */
	struct pebs_record_core *at, *top;
	int n;

1465
	if (!x86_pmu.pebs_active)
1466 1467 1468 1469 1470
		return;

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

1471 1472 1473 1474 1475 1476
	/*
	 * Whatever else happens, drain the thing
	 */
	ds->pebs_index = ds->pebs_buffer_base;

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

1479 1480
	WARN_ON_ONCE(!event);

P
Peter Zijlstra 已提交
1481
	if (!event->attr.precise_ip)
1482 1483
		return;

1484
	n = top - at;
1485 1486 1487
	if (n <= 0) {
		if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)
			intel_pmu_save_and_restart_reload(event, 0);
1488
		return;
1489
	}
1490

1491
	__intel_pmu_pebs_event(event, iregs, at, top, 0, n);
1492 1493
}

1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
static void intel_pmu_pebs_event_update_no_drain(struct cpu_hw_events *cpuc, int size)
{
	struct perf_event *event;
	int bit;

	/*
	 * The drain_pebs() could be called twice in a short period
	 * for auto-reload event in pmu::read(). There are no
	 * overflows have happened in between.
	 * It needs to call intel_pmu_save_and_restart_reload() to
	 * update the event->count for this case.
	 */
	for_each_set_bit(bit, (unsigned long *)&cpuc->pebs_enabled, size) {
		event = cpuc->events[bit];
		if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)
			intel_pmu_save_and_restart_reload(event, 0);
	}
}

1513
static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
1514
{
1515
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1516
	struct debug_store *ds = cpuc->ds;
1517 1518
	struct perf_event *event;
	void *base, *at, *top;
1519 1520 1521 1522
	short counts[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {};
	short error[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {};
	int bit, i, size;
	u64 mask;
1523 1524 1525 1526

	if (!x86_pmu.pebs_active)
		return;

1527
	base = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
1528
	top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
1529 1530 1531

	ds->pebs_index = ds->pebs_buffer_base;

1532 1533 1534 1535 1536 1537 1538
	mask = (1ULL << x86_pmu.max_pebs_events) - 1;
	size = x86_pmu.max_pebs_events;
	if (x86_pmu.flags & PMU_FL_PEBS_ALL) {
		mask |= ((1ULL << x86_pmu.num_counters_fixed) - 1) << INTEL_PMC_IDX_FIXED;
		size = INTEL_PMC_IDX_FIXED + x86_pmu.num_counters_fixed;
	}

1539
	if (unlikely(base >= top)) {
1540
		intel_pmu_pebs_event_update_no_drain(cpuc, size);
1541
		return;
1542
	}
1543

1544
	for (at = base; at < top; at += x86_pmu.pebs_record_size) {
1545
		struct pebs_record_nhm *p = at;
1546
		u64 pebs_status;
1547

1548
		pebs_status = p->status & cpuc->pebs_enabled;
1549
		pebs_status &= mask;
1550 1551

		/* PEBS v3 has more accurate status bits */
1552
		if (x86_pmu.intel_cap.pebs_format >= 3) {
1553
			for_each_set_bit(bit, (unsigned long *)&pebs_status,
1554
					 size)
1555 1556 1557 1558 1559
				counts[bit]++;

			continue;
		}

1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
		/*
		 * On some CPUs the PEBS status can be zero when PEBS is
		 * racing with clearing of GLOBAL_STATUS.
		 *
		 * Normally we would drop that record, but in the
		 * case when there is only a single active PEBS event
		 * we can assume it's for that event.
		 */
		if (!pebs_status && cpuc->pebs_enabled &&
			!(cpuc->pebs_enabled & (cpuc->pebs_enabled-1)))
			pebs_status = cpuc->pebs_enabled;

1572
		bit = find_first_bit((unsigned long *)&pebs_status,
1573
					x86_pmu.max_pebs_events);
1574
		if (bit >= x86_pmu.max_pebs_events)
1575
			continue;
1576

1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
		/*
		 * 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.
		 */
1592 1593 1594 1595 1596
		if (p->status != (1ULL << bit)) {
			for_each_set_bit(i, (unsigned long *)&pebs_status,
					 x86_pmu.max_pebs_events)
				error[i]++;
			continue;
1597
		}
1598

1599 1600
		counts[bit]++;
	}
1601

1602
	for (bit = 0; bit < size; bit++) {
1603
		if ((counts[bit] == 0) && (error[bit] == 0))
1604
			continue;
1605

1606
		event = cpuc->events[bit];
1607 1608 1609 1610 1611
		if (WARN_ON_ONCE(!event))
			continue;

		if (WARN_ON_ONCE(!event->attr.precise_ip))
			continue;
1612

1613
		/* log dropped samples number */
1614
		if (error[bit]) {
1615 1616
			perf_log_lost_samples(event, error[bit]);

1617 1618 1619 1620
			if (perf_event_account_interrupt(event))
				x86_pmu_stop(event, 0);
		}

1621 1622 1623 1624
		if (counts[bit]) {
			__intel_pmu_pebs_event(event, iregs, base,
					       top, bit, counts[bit]);
		}
1625 1626 1627 1628 1629 1630 1631
	}
}

/*
 * BTS, PEBS probe and setup
 */

1632
void __init intel_ds_init(void)
1633 1634 1635 1636 1637 1638 1639 1640 1641
{
	/*
	 * 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);
1642
	x86_pmu.pebs_buffer_size = PEBS_BUFFER_SIZE;
1643
	if (x86_pmu.version <= 4) {
1644
		x86_pmu.pebs_no_isolation = 1;
1645 1646
		x86_pmu.pebs_no_xmm_regs = 1;
	}
1647
	if (x86_pmu.pebs) {
1648 1649
		char pebs_type = x86_pmu.intel_cap.pebs_trap ?  '+' : '-';
		int format = x86_pmu.intel_cap.pebs_format;
1650 1651 1652

		switch (format) {
		case 0:
1653
			pr_cont("PEBS fmt0%c, ", pebs_type);
1654
			x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
1655 1656 1657 1658 1659 1660 1661 1662
			/*
			 * Using >PAGE_SIZE buffers makes the WRMSR to
			 * PERF_GLOBAL_CTRL in intel_pmu_enable_all()
			 * mysteriously hang on Core2.
			 *
			 * As a workaround, we don't do this.
			 */
			x86_pmu.pebs_buffer_size = PAGE_SIZE;
1663 1664 1665 1666
			x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
			break;

		case 1:
1667
			pr_cont("PEBS fmt1%c, ", pebs_type);
1668 1669 1670 1671
			x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
			break;

1672 1673 1674
		case 2:
			pr_cont("PEBS fmt2%c, ", pebs_type);
			x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw);
1675
			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
1676 1677
			break;

1678 1679 1680 1681 1682
		case 3:
			pr_cont("PEBS fmt3%c, ", pebs_type);
			x86_pmu.pebs_record_size =
						sizeof(struct pebs_record_skl);
			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
1683
			x86_pmu.large_pebs_flags |= PERF_SAMPLE_TIME;
1684 1685
			break;

1686
		default:
1687
			pr_cont("no PEBS fmt%d%c, ", format, pebs_type);
1688 1689 1690 1691
			x86_pmu.pebs = 0;
		}
	}
}
1692 1693 1694

void perf_restore_debug_store(void)
{
1695 1696
	struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);

1697 1698 1699
	if (!x86_pmu.bts && !x86_pmu.pebs)
		return;

1700
	wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds);
1701
}