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

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

#include "perf_event.h"
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

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

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

/*
 * pebs_record_32 for p4 and core not supported

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

 */

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

44
void init_debug_store_on_cpu(int cpu)
45 46 47 48 49 50 51 52 53 54 55
{
	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));
}

56
void fini_debug_store_on_cpu(int cpu)
57 58 59 60 61 62 63
{
	if (!per_cpu(cpu_hw_events, cpu).ds)
		return;

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

64 65 66
static int alloc_pebs_buffer(int cpu)
{
	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
67
	int node = cpu_to_node(cpu);
68 69 70 71 72 73
	int max, thresh = 1; /* always use a single PEBS record */
	void *buffer;

	if (!x86_pmu.pebs)
		return 0;

74
	buffer = kmalloc_node(PEBS_BUFFER_SIZE, GFP_KERNEL | __GFP_ZERO, node);
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	if (unlikely(!buffer))
		return -ENOMEM;

	max = PEBS_BUFFER_SIZE / x86_pmu.pebs_record_size;

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

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

	return 0;
}

91 92 93 94 95 96 97 98 99 100 101
static void release_pebs_buffer(int cpu)
{
	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;

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

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

102 103 104
static int alloc_bts_buffer(int cpu)
{
	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
105
	int node = cpu_to_node(cpu);
106 107 108 109 110 111
	int max, thresh;
	void *buffer;

	if (!x86_pmu.bts)
		return 0;

112
	buffer = kmalloc_node(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_ZERO, node);
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	if (unlikely(!buffer))
		return -ENOMEM;

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

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

	return 0;
}

129 130 131 132 133 134 135 136 137 138 139
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;
}

140 141
static int alloc_ds_buffer(int cpu)
{
142
	int node = cpu_to_node(cpu);
143 144
	struct debug_store *ds;

145
	ds = kmalloc_node(sizeof(*ds), GFP_KERNEL | __GFP_ZERO, node);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	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);
}

165
void release_ds_buffers(void)
166 167 168 169 170 171 172 173 174 175 176
{
	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) {
177 178
		release_pebs_buffer(cpu);
		release_bts_buffer(cpu);
179
		release_ds_buffer(cpu);
180 181 182 183
	}
	put_online_cpus();
}

184
void reserve_ds_buffers(void)
185
{
186 187 188 189 190
	int bts_err = 0, pebs_err = 0;
	int cpu;

	x86_pmu.bts_active = 0;
	x86_pmu.pebs_active = 0;
191 192

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

195 196 197 198 199 200
	if (!x86_pmu.bts)
		bts_err = 1;

	if (!x86_pmu.pebs)
		pebs_err = 1;

201 202 203
	get_online_cpus();

	for_each_possible_cpu(cpu) {
204 205 206 207
		if (alloc_ds_buffer(cpu)) {
			bts_err = 1;
			pebs_err = 1;
		}
208

209 210 211 212 213
		if (!bts_err && alloc_bts_buffer(cpu))
			bts_err = 1;

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

215
		if (bts_err && pebs_err)
216
			break;
217 218 219 220 221 222
	}

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

224 225 226
	if (pebs_err) {
		for_each_possible_cpu(cpu)
			release_pebs_buffer(cpu);
227 228
	}

229 230 231 232 233 234 235 236 237 238
	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;

239 240 241 242 243 244 245 246 247 248 249
		for_each_online_cpu(cpu)
			init_debug_store_on_cpu(cpu);
	}

	put_online_cpus();
}

/*
 * BTS
 */

250
struct event_constraint bts_constraint =
251 252
	EVENT_CONSTRAINT(0, 1ULL << X86_PMC_IDX_FIXED_BTS, 0);

253
void intel_pmu_enable_bts(u64 config)
254 255 256 257 258
{
	unsigned long debugctlmsr;

	debugctlmsr = get_debugctlmsr();

259 260 261
	debugctlmsr |= DEBUGCTLMSR_TR;
	debugctlmsr |= DEBUGCTLMSR_BTS;
	debugctlmsr |= DEBUGCTLMSR_BTINT;
262 263

	if (!(config & ARCH_PERFMON_EVENTSEL_OS))
264
		debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
265 266

	if (!(config & ARCH_PERFMON_EVENTSEL_USR))
267
		debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR;
268 269 270 271

	update_debugctlmsr(debugctlmsr);
}

272
void intel_pmu_disable_bts(void)
273 274 275 276 277 278 279 280 281 282
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
	unsigned long debugctlmsr;

	if (!cpuc->ds)
		return;

	debugctlmsr = get_debugctlmsr();

	debugctlmsr &=
283 284
		~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT |
		  DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR);
285 286 287 288

	update_debugctlmsr(debugctlmsr);
}

289
int intel_pmu_drain_bts_buffer(void)
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
	struct debug_store *ds = cpuc->ds;
	struct bts_record {
		u64	from;
		u64	to;
		u64	flags;
	};
	struct perf_event *event = cpuc->events[X86_PMC_IDX_FIXED_BTS];
	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)
306
		return 0;
307

308
	if (!x86_pmu.bts_active)
309
		return 0;
310 311 312 313 314

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

	if (top <= at)
315
		return 0;
316 317 318

	ds->bts_index = ds->bts_buffer_base;

319
	perf_sample_data_init(&data, 0, event->hw.last_period);
320 321 322 323 324 325 326 327 328
	regs.ip     = 0;

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

329
	if (perf_output_begin(&handle, event, header.size * (top - at)))
330
		return 1;
331 332 333 334 335 336 337 338 339 340 341 342 343

	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;
344
	return 1;
345 346 347 348 349
}

/*
 * PEBS
 */
350
struct event_constraint intel_core2_pebs_event_constraints[] = {
351 352 353 354 355
	INTEL_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
	INTEL_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
	INTEL_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
	INTEL_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
	INTEL_EVENT_CONSTRAINT(0xcb, 0x1),    /* MEM_LOAD_RETIRED.* */
356 357 358
	EVENT_CONSTRAINT_END
};

359
struct event_constraint intel_atom_pebs_event_constraints[] = {
360 361 362
	INTEL_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
	INTEL_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
	INTEL_EVENT_CONSTRAINT(0xcb, 0x1),    /* MEM_LOAD_RETIRED.* */
363 364 365
	EVENT_CONSTRAINT_END
};

366
struct event_constraint intel_nehalem_pebs_event_constraints[] = {
367 368 369 370 371 372 373 374 375 376 377
	INTEL_EVENT_CONSTRAINT(0x0b, 0xf),    /* MEM_INST_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0x0f, 0xf),    /* MEM_UNCORE_RETIRED.* */
	INTEL_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
	INTEL_EVENT_CONSTRAINT(0xc0, 0xf),    /* INST_RETIRED.ANY */
	INTEL_EVENT_CONSTRAINT(0xc2, 0xf),    /* UOPS_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
	INTEL_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
	INTEL_EVENT_CONSTRAINT(0xc7, 0xf),    /* SSEX_UOPS_RETIRED.* */
	INTEL_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
	INTEL_EVENT_CONSTRAINT(0xcb, 0xf),    /* MEM_LOAD_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0xf7, 0xf),    /* FP_ASSIST.* */
378 379 380
	EVENT_CONSTRAINT_END
};

381
struct event_constraint intel_westmere_pebs_event_constraints[] = {
382 383 384 385 386 387 388 389 390 391 392
	INTEL_EVENT_CONSTRAINT(0x0b, 0xf),    /* MEM_INST_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0x0f, 0xf),    /* MEM_UNCORE_RETIRED.* */
	INTEL_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
	INTEL_EVENT_CONSTRAINT(0xc0, 0xf),    /* INSTR_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0xc2, 0xf),    /* UOPS_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0xc5, 0xf),    /* BR_MISP_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0xc7, 0xf),    /* SSEX_UOPS_RETIRED.* */
	INTEL_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
	INTEL_EVENT_CONSTRAINT(0xcb, 0xf),    /* MEM_LOAD_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0xf7, 0xf),    /* FP_ASSIST.* */
393 394 395
	EVENT_CONSTRAINT_END
};

396
struct event_constraint intel_snb_pebs_event_constraints[] = {
397 398 399 400 401 402
	INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
	INTEL_UEVENT_CONSTRAINT(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
	INTEL_UEVENT_CONSTRAINT(0x02c2, 0xf), /* UOPS_RETIRED.RETIRE_SLOTS */
	INTEL_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0xc5, 0xf),    /* BR_MISP_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0xcd, 0x8),    /* MEM_TRANS_RETIRED.* */
403
	INTEL_EVENT_CONSTRAINT(0xd0, 0xf),    /* MEM_UOP_RETIRED.* */
404 405 406
	INTEL_EVENT_CONSTRAINT(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
	INTEL_EVENT_CONSTRAINT(0xd2, 0xf),    /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
	INTEL_UEVENT_CONSTRAINT(0x02d4, 0xf), /* MEM_LOAD_UOPS_MISC_RETIRED.LLC_MISS */
407 408 409
	EVENT_CONSTRAINT_END
};

410
struct event_constraint *intel_pebs_constraints(struct perf_event *event)
411 412 413
{
	struct event_constraint *c;

P
Peter Zijlstra 已提交
414
	if (!event->attr.precise_ip)
415 416 417 418 419 420 421 422 423 424 425 426
		return NULL;

	if (x86_pmu.pebs_constraints) {
		for_each_event_constraint(c, x86_pmu.pebs_constraints) {
			if ((event->hw.config & c->cmask) == c->code)
				return c;
		}
	}

	return &emptyconstraint;
}

427
void intel_pmu_pebs_enable(struct perf_event *event)
428 429
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
430
	struct hw_perf_event *hwc = &event->hw;
431 432 433

	hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;

434
	cpuc->pebs_enabled |= 1ULL << hwc->idx;
435 436
}

437
void intel_pmu_pebs_disable(struct perf_event *event)
438 439
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
440
	struct hw_perf_event *hwc = &event->hw;
441

442
	cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
443
	if (cpuc->enabled)
444
		wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
445 446 447 448

	hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
}

449
void intel_pmu_pebs_enable_all(void)
450 451 452 453 454 455 456
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);

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

457
void intel_pmu_pebs_disable_all(void)
458 459 460 461 462 463 464
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);

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

465 466 467 468 469 470
static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
	unsigned long from = cpuc->lbr_entries[0].from;
	unsigned long old_to, to = cpuc->lbr_entries[0].to;
	unsigned long ip = regs->ip;
471
	int is_64bit = 0;
472

473 474 475 476 477 478
	/*
	 * 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 已提交
479 480 481
	/*
	 * No LBR entry, no basic block, no rewinding
	 */
482 483 484
	if (!cpuc->lbr_stack.nr || !from || !to)
		return 0;

P
Peter Zijlstra 已提交
485 486 487 488 489 490 491 492 493 494 495
	/*
	 * Basic blocks should never cross user/kernel boundaries
	 */
	if (kernel_ip(ip) != kernel_ip(to))
		return 0;

	/*
	 * unsigned math, either ip is before the start (impossible) or
	 * the basic block is larger than 1 page (sanity)
	 */
	if ((ip - to) > PAGE_SIZE)
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
		return 0;

	/*
	 * We sampled a branch insn, rewind using the LBR stack
	 */
	if (ip == to) {
		regs->ip = from;
		return 1;
	}

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

		old_to = to;
		if (!kernel_ip(ip)) {
P
Peter Zijlstra 已提交
513
			int bytes, size = MAX_INSN_SIZE;
514 515 516 517 518 519 520 521 522

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

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

523 524 525 526
#ifdef CONFIG_X86_64
		is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
#endif
		insn_init(&insn, kaddr, is_64bit);
527 528 529 530 531 532 533 534 535
		insn_get_length(&insn);
		to += insn.length;
	} while (to < ip);

	if (to == ip) {
		regs->ip = old_to;
		return 1;
	}

P
Peter Zijlstra 已提交
536 537 538 539
	/*
	 * Even though we decoded the basic block, the instruction stream
	 * never matched the given IP, either the TO or the IP got corrupted.
	 */
540 541 542
	return 0;
}

543 544 545 546 547 548 549 550
static void __intel_pmu_pebs_event(struct perf_event *event,
				   struct pt_regs *iregs, void *__pebs)
{
	/*
	 * We cast to pebs_record_core since that is a subset of
	 * both formats and we don't use the other fields in this
	 * routine.
	 */
551
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
552 553 554 555 556 557 558
	struct pebs_record_core *pebs = __pebs;
	struct perf_sample_data data;
	struct pt_regs regs;

	if (!intel_pmu_save_and_restart(event))
		return;

559
	perf_sample_data_init(&data, 0, event->hw.last_period);
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575

	/*
	 * We use the interrupt regs as a base because the PEBS record
	 * does not contain a full regs set, specifically it seems to
	 * lack segment descriptors, which get used by things like
	 * user_mode().
	 *
	 * In the simple case fix up only the IP and BP,SP regs, for
	 * PERF_SAMPLE_IP and PERF_SAMPLE_CALLCHAIN to function properly.
	 * A possible PERF_SAMPLE_REGS will have to transfer all regs.
	 */
	regs = *iregs;
	regs.ip = pebs->ip;
	regs.bp = pebs->bp;
	regs.sp = pebs->sp;

P
Peter Zijlstra 已提交
576
	if (event->attr.precise_ip > 1 && intel_pmu_pebs_fixup_ip(&regs))
577 578 579 580
		regs.flags |= PERF_EFLAGS_EXACT;
	else
		regs.flags &= ~PERF_EFLAGS_EXACT;

581 582 583
	if (has_branch_stack(event))
		data.br_stack = &cpuc->lbr_stack;

584
	if (perf_event_overflow(event, &data, &regs))
P
Peter Zijlstra 已提交
585
		x86_pmu_stop(event, 0);
586 587
}

588 589 590 591 592 593 594 595
static void intel_pmu_drain_pebs_core(struct pt_regs *iregs)
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
	struct debug_store *ds = cpuc->ds;
	struct perf_event *event = cpuc->events[0]; /* PMC0 only */
	struct pebs_record_core *at, *top;
	int n;

596
	if (!x86_pmu.pebs_active)
597 598 599 600 601
		return;

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

602 603 604 605 606 607
	/*
	 * Whatever else happens, drain the thing
	 */
	ds->pebs_index = ds->pebs_buffer_base;

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

610 611
	WARN_ON_ONCE(!event);

P
Peter Zijlstra 已提交
612
	if (!event->attr.precise_ip)
613 614 615 616 617
		return;

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

619 620 621 622
	/*
	 * Should not happen, we program the threshold at 1 and do not
	 * set a reset value.
	 */
623
	WARN_ONCE(n > 1, "bad leftover pebs %d\n", n);
624 625
	at += n - 1;

626
	__intel_pmu_pebs_event(event, iregs, at);
627 628 629 630 631 632 633 634
}

static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
	struct debug_store *ds = cpuc->ds;
	struct pebs_record_nhm *at, *top;
	struct perf_event *event = NULL;
635
	u64 status = 0;
636 637
	int bit, n;

638
	if (!x86_pmu.pebs_active)
639 640 641 642 643 644 645 646
		return;

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

	ds->pebs_index = ds->pebs_buffer_base;

	n = top - at;
647 648
	if (n <= 0)
		return;
649 650 651 652 653

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

	for ( ; at < top; at++) {
657
		for_each_set_bit(bit, (unsigned long *)&at->status, x86_pmu.max_pebs_events) {
658 659
			event = cpuc->events[bit];
			if (!test_bit(bit, cpuc->active_mask))
660 661
				continue;

662 663
			WARN_ON_ONCE(!event);

P
Peter Zijlstra 已提交
664
			if (!event->attr.precise_ip)
665 666 667 668 669 670
				continue;

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

			break;
671 672
		}

673
		if (!event || bit >= x86_pmu.max_pebs_events)
674 675
			continue;

676
		__intel_pmu_pebs_event(event, iregs, at);
677 678 679 680 681 682 683
	}
}

/*
 * BTS, PEBS probe and setup
 */

684
void intel_ds_init(void)
685 686 687 688 689 690 691 692 693 694
{
	/*
	 * 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) {
695 696
		char pebs_type = x86_pmu.intel_cap.pebs_trap ?  '+' : '-';
		int format = x86_pmu.intel_cap.pebs_format;
697 698 699

		switch (format) {
		case 0:
700
			printk(KERN_CONT "PEBS fmt0%c, ", pebs_type);
701 702 703 704 705
			x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
			x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
			break;

		case 1:
706
			printk(KERN_CONT "PEBS fmt1%c, ", pebs_type);
707 708 709 710 711
			x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
			break;

		default:
712
			printk(KERN_CONT "no PEBS fmt%d%c, ", format, pebs_type);
713 714 715 716
			x86_pmu.pebs = 0;
		}
	}
}