xref: /openbmc/linux/arch/x86/events/intel/ds.c (revision e17dc653)
17010d129SBorislav Petkov #include <linux/bitops.h>
27010d129SBorislav Petkov #include <linux/types.h>
37010d129SBorislav Petkov #include <linux/slab.h>
47010d129SBorislav Petkov 
57010d129SBorislav Petkov #include <asm/perf_event.h>
67010d129SBorislav Petkov #include <asm/insn.h>
77010d129SBorislav Petkov 
827f6d22bSBorislav Petkov #include "../perf_event.h"
97010d129SBorislav Petkov 
107010d129SBorislav Petkov /* The size of a BTS record in bytes: */
117010d129SBorislav Petkov #define BTS_RECORD_SIZE		24
127010d129SBorislav Petkov 
137010d129SBorislav Petkov #define BTS_BUFFER_SIZE		(PAGE_SIZE << 4)
147010d129SBorislav Petkov #define PEBS_BUFFER_SIZE	(PAGE_SIZE << 4)
157010d129SBorislav Petkov #define PEBS_FIXUP_SIZE		PAGE_SIZE
167010d129SBorislav Petkov 
177010d129SBorislav Petkov /*
187010d129SBorislav Petkov  * pebs_record_32 for p4 and core not supported
197010d129SBorislav Petkov 
207010d129SBorislav Petkov struct pebs_record_32 {
217010d129SBorislav Petkov 	u32 flags, ip;
227010d129SBorislav Petkov 	u32 ax, bc, cx, dx;
237010d129SBorislav Petkov 	u32 si, di, bp, sp;
247010d129SBorislav Petkov };
257010d129SBorislav Petkov 
267010d129SBorislav Petkov  */
277010d129SBorislav Petkov 
287010d129SBorislav Petkov union intel_x86_pebs_dse {
297010d129SBorislav Petkov 	u64 val;
307010d129SBorislav Petkov 	struct {
317010d129SBorislav Petkov 		unsigned int ld_dse:4;
327010d129SBorislav Petkov 		unsigned int ld_stlb_miss:1;
337010d129SBorislav Petkov 		unsigned int ld_locked:1;
347010d129SBorislav Petkov 		unsigned int ld_reserved:26;
357010d129SBorislav Petkov 	};
367010d129SBorislav Petkov 	struct {
377010d129SBorislav Petkov 		unsigned int st_l1d_hit:1;
387010d129SBorislav Petkov 		unsigned int st_reserved1:3;
397010d129SBorislav Petkov 		unsigned int st_stlb_miss:1;
407010d129SBorislav Petkov 		unsigned int st_locked:1;
417010d129SBorislav Petkov 		unsigned int st_reserved2:26;
427010d129SBorislav Petkov 	};
437010d129SBorislav Petkov };
447010d129SBorislav Petkov 
457010d129SBorislav Petkov 
467010d129SBorislav Petkov /*
477010d129SBorislav Petkov  * Map PEBS Load Latency Data Source encodings to generic
487010d129SBorislav Petkov  * memory data source information
497010d129SBorislav Petkov  */
507010d129SBorislav Petkov #define P(a, b) PERF_MEM_S(a, b)
517010d129SBorislav Petkov #define OP_LH (P(OP, LOAD) | P(LVL, HIT))
527010d129SBorislav Petkov #define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS))
537010d129SBorislav Petkov 
54*e17dc653SAndi Kleen /* Version for Sandy Bridge and later */
55*e17dc653SAndi Kleen static u64 pebs_data_source[] = {
567010d129SBorislav Petkov 	P(OP, LOAD) | P(LVL, MISS) | P(LVL, L3) | P(SNOOP, NA),/* 0x00:ukn L3 */
577010d129SBorislav Petkov 	OP_LH | P(LVL, L1)  | P(SNOOP, NONE),	/* 0x01: L1 local */
587010d129SBorislav Petkov 	OP_LH | P(LVL, LFB) | P(SNOOP, NONE),	/* 0x02: LFB hit */
597010d129SBorislav Petkov 	OP_LH | P(LVL, L2)  | P(SNOOP, NONE),	/* 0x03: L2 hit */
607010d129SBorislav Petkov 	OP_LH | P(LVL, L3)  | P(SNOOP, NONE),	/* 0x04: L3 hit */
617010d129SBorislav Petkov 	OP_LH | P(LVL, L3)  | P(SNOOP, MISS),	/* 0x05: L3 hit, snoop miss */
627010d129SBorislav Petkov 	OP_LH | P(LVL, L3)  | P(SNOOP, HIT),	/* 0x06: L3 hit, snoop hit */
637010d129SBorislav Petkov 	OP_LH | P(LVL, L3)  | P(SNOOP, HITM),	/* 0x07: L3 hit, snoop hitm */
647010d129SBorislav Petkov 	OP_LH | P(LVL, REM_CCE1) | P(SNOOP, HIT),  /* 0x08: L3 miss snoop hit */
657010d129SBorislav Petkov 	OP_LH | P(LVL, REM_CCE1) | P(SNOOP, HITM), /* 0x09: L3 miss snoop hitm*/
667010d129SBorislav Petkov 	OP_LH | P(LVL, LOC_RAM)  | P(SNOOP, HIT),  /* 0x0a: L3 miss, shared */
677010d129SBorislav Petkov 	OP_LH | P(LVL, REM_RAM1) | P(SNOOP, HIT),  /* 0x0b: L3 miss, shared */
687010d129SBorislav Petkov 	OP_LH | P(LVL, LOC_RAM)  | SNOOP_NONE_MISS,/* 0x0c: L3 miss, excl */
697010d129SBorislav Petkov 	OP_LH | P(LVL, REM_RAM1) | SNOOP_NONE_MISS,/* 0x0d: L3 miss, excl */
707010d129SBorislav Petkov 	OP_LH | P(LVL, IO)  | P(SNOOP, NONE), /* 0x0e: I/O */
717010d129SBorislav Petkov 	OP_LH | P(LVL, UNC) | P(SNOOP, NONE), /* 0x0f: uncached */
727010d129SBorislav Petkov };
737010d129SBorislav Petkov 
74*e17dc653SAndi Kleen /* Patch up minor differences in the bits */
75*e17dc653SAndi Kleen void __init intel_pmu_pebs_data_source_nhm(void)
76*e17dc653SAndi Kleen {
77*e17dc653SAndi Kleen 	pebs_data_source[0x05] = OP_LH | P(LVL, L3)  | P(SNOOP, HIT);
78*e17dc653SAndi Kleen 	pebs_data_source[0x06] = OP_LH | P(LVL, L3)  | P(SNOOP, HITM);
79*e17dc653SAndi Kleen 	pebs_data_source[0x07] = OP_LH | P(LVL, L3)  | P(SNOOP, HITM);
80*e17dc653SAndi Kleen }
81*e17dc653SAndi Kleen 
827010d129SBorislav Petkov static u64 precise_store_data(u64 status)
837010d129SBorislav Petkov {
847010d129SBorislav Petkov 	union intel_x86_pebs_dse dse;
857010d129SBorislav Petkov 	u64 val = P(OP, STORE) | P(SNOOP, NA) | P(LVL, L1) | P(TLB, L2);
867010d129SBorislav Petkov 
877010d129SBorislav Petkov 	dse.val = status;
887010d129SBorislav Petkov 
897010d129SBorislav Petkov 	/*
907010d129SBorislav Petkov 	 * bit 4: TLB access
917010d129SBorislav Petkov 	 * 1 = stored missed 2nd level TLB
927010d129SBorislav Petkov 	 *
937010d129SBorislav Petkov 	 * so it either hit the walker or the OS
947010d129SBorislav Petkov 	 * otherwise hit 2nd level TLB
957010d129SBorislav Petkov 	 */
967010d129SBorislav Petkov 	if (dse.st_stlb_miss)
977010d129SBorislav Petkov 		val |= P(TLB, MISS);
987010d129SBorislav Petkov 	else
997010d129SBorislav Petkov 		val |= P(TLB, HIT);
1007010d129SBorislav Petkov 
1017010d129SBorislav Petkov 	/*
1027010d129SBorislav Petkov 	 * bit 0: hit L1 data cache
1037010d129SBorislav Petkov 	 * if not set, then all we know is that
1047010d129SBorislav Petkov 	 * it missed L1D
1057010d129SBorislav Petkov 	 */
1067010d129SBorislav Petkov 	if (dse.st_l1d_hit)
1077010d129SBorislav Petkov 		val |= P(LVL, HIT);
1087010d129SBorislav Petkov 	else
1097010d129SBorislav Petkov 		val |= P(LVL, MISS);
1107010d129SBorislav Petkov 
1117010d129SBorislav Petkov 	/*
1127010d129SBorislav Petkov 	 * bit 5: Locked prefix
1137010d129SBorislav Petkov 	 */
1147010d129SBorislav Petkov 	if (dse.st_locked)
1157010d129SBorislav Petkov 		val |= P(LOCK, LOCKED);
1167010d129SBorislav Petkov 
1177010d129SBorislav Petkov 	return val;
1187010d129SBorislav Petkov }
1197010d129SBorislav Petkov 
1207010d129SBorislav Petkov static u64 precise_datala_hsw(struct perf_event *event, u64 status)
1217010d129SBorislav Petkov {
1227010d129SBorislav Petkov 	union perf_mem_data_src dse;
1237010d129SBorislav Petkov 
1247010d129SBorislav Petkov 	dse.val = PERF_MEM_NA;
1257010d129SBorislav Petkov 
1267010d129SBorislav Petkov 	if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW)
1277010d129SBorislav Petkov 		dse.mem_op = PERF_MEM_OP_STORE;
1287010d129SBorislav Petkov 	else if (event->hw.flags & PERF_X86_EVENT_PEBS_LD_HSW)
1297010d129SBorislav Petkov 		dse.mem_op = PERF_MEM_OP_LOAD;
1307010d129SBorislav Petkov 
1317010d129SBorislav Petkov 	/*
1327010d129SBorislav Petkov 	 * L1 info only valid for following events:
1337010d129SBorislav Petkov 	 *
1347010d129SBorislav Petkov 	 * MEM_UOPS_RETIRED.STLB_MISS_STORES
1357010d129SBorislav Petkov 	 * MEM_UOPS_RETIRED.LOCK_STORES
1367010d129SBorislav Petkov 	 * MEM_UOPS_RETIRED.SPLIT_STORES
1377010d129SBorislav Petkov 	 * MEM_UOPS_RETIRED.ALL_STORES
1387010d129SBorislav Petkov 	 */
1397010d129SBorislav Petkov 	if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) {
1407010d129SBorislav Petkov 		if (status & 1)
1417010d129SBorislav Petkov 			dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_HIT;
1427010d129SBorislav Petkov 		else
1437010d129SBorislav Petkov 			dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_MISS;
1447010d129SBorislav Petkov 	}
1457010d129SBorislav Petkov 	return dse.val;
1467010d129SBorislav Petkov }
1477010d129SBorislav Petkov 
1487010d129SBorislav Petkov static u64 load_latency_data(u64 status)
1497010d129SBorislav Petkov {
1507010d129SBorislav Petkov 	union intel_x86_pebs_dse dse;
1517010d129SBorislav Petkov 	u64 val;
1527010d129SBorislav Petkov 	int model = boot_cpu_data.x86_model;
1537010d129SBorislav Petkov 	int fam = boot_cpu_data.x86;
1547010d129SBorislav Petkov 
1557010d129SBorislav Petkov 	dse.val = status;
1567010d129SBorislav Petkov 
1577010d129SBorislav Petkov 	/*
1587010d129SBorislav Petkov 	 * use the mapping table for bit 0-3
1597010d129SBorislav Petkov 	 */
1607010d129SBorislav Petkov 	val = pebs_data_source[dse.ld_dse];
1617010d129SBorislav Petkov 
1627010d129SBorislav Petkov 	/*
1637010d129SBorislav Petkov 	 * Nehalem models do not support TLB, Lock infos
1647010d129SBorislav Petkov 	 */
1657010d129SBorislav Petkov 	if (fam == 0x6 && (model == 26 || model == 30
1667010d129SBorislav Petkov 	    || model == 31 || model == 46)) {
1677010d129SBorislav Petkov 		val |= P(TLB, NA) | P(LOCK, NA);
1687010d129SBorislav Petkov 		return val;
1697010d129SBorislav Petkov 	}
1707010d129SBorislav Petkov 	/*
1717010d129SBorislav Petkov 	 * bit 4: TLB access
1727010d129SBorislav Petkov 	 * 0 = did not miss 2nd level TLB
1737010d129SBorislav Petkov 	 * 1 = missed 2nd level TLB
1747010d129SBorislav Petkov 	 */
1757010d129SBorislav Petkov 	if (dse.ld_stlb_miss)
1767010d129SBorislav Petkov 		val |= P(TLB, MISS) | P(TLB, L2);
1777010d129SBorislav Petkov 	else
1787010d129SBorislav Petkov 		val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2);
1797010d129SBorislav Petkov 
1807010d129SBorislav Petkov 	/*
1817010d129SBorislav Petkov 	 * bit 5: locked prefix
1827010d129SBorislav Petkov 	 */
1837010d129SBorislav Petkov 	if (dse.ld_locked)
1847010d129SBorislav Petkov 		val |= P(LOCK, LOCKED);
1857010d129SBorislav Petkov 
1867010d129SBorislav Petkov 	return val;
1877010d129SBorislav Petkov }
1887010d129SBorislav Petkov 
1897010d129SBorislav Petkov struct pebs_record_core {
1907010d129SBorislav Petkov 	u64 flags, ip;
1917010d129SBorislav Petkov 	u64 ax, bx, cx, dx;
1927010d129SBorislav Petkov 	u64 si, di, bp, sp;
1937010d129SBorislav Petkov 	u64 r8,  r9,  r10, r11;
1947010d129SBorislav Petkov 	u64 r12, r13, r14, r15;
1957010d129SBorislav Petkov };
1967010d129SBorislav Petkov 
1977010d129SBorislav Petkov struct pebs_record_nhm {
1987010d129SBorislav Petkov 	u64 flags, ip;
1997010d129SBorislav Petkov 	u64 ax, bx, cx, dx;
2007010d129SBorislav Petkov 	u64 si, di, bp, sp;
2017010d129SBorislav Petkov 	u64 r8,  r9,  r10, r11;
2027010d129SBorislav Petkov 	u64 r12, r13, r14, r15;
2037010d129SBorislav Petkov 	u64 status, dla, dse, lat;
2047010d129SBorislav Petkov };
2057010d129SBorislav Petkov 
2067010d129SBorislav Petkov /*
2077010d129SBorislav Petkov  * Same as pebs_record_nhm, with two additional fields.
2087010d129SBorislav Petkov  */
2097010d129SBorislav Petkov struct pebs_record_hsw {
2107010d129SBorislav Petkov 	u64 flags, ip;
2117010d129SBorislav Petkov 	u64 ax, bx, cx, dx;
2127010d129SBorislav Petkov 	u64 si, di, bp, sp;
2137010d129SBorislav Petkov 	u64 r8,  r9,  r10, r11;
2147010d129SBorislav Petkov 	u64 r12, r13, r14, r15;
2157010d129SBorislav Petkov 	u64 status, dla, dse, lat;
2167010d129SBorislav Petkov 	u64 real_ip, tsx_tuning;
2177010d129SBorislav Petkov };
2187010d129SBorislav Petkov 
2197010d129SBorislav Petkov union hsw_tsx_tuning {
2207010d129SBorislav Petkov 	struct {
2217010d129SBorislav Petkov 		u32 cycles_last_block     : 32,
2227010d129SBorislav Petkov 		    hle_abort		  : 1,
2237010d129SBorislav Petkov 		    rtm_abort		  : 1,
2247010d129SBorislav Petkov 		    instruction_abort     : 1,
2257010d129SBorislav Petkov 		    non_instruction_abort : 1,
2267010d129SBorislav Petkov 		    retry		  : 1,
2277010d129SBorislav Petkov 		    data_conflict	  : 1,
2287010d129SBorislav Petkov 		    capacity_writes	  : 1,
2297010d129SBorislav Petkov 		    capacity_reads	  : 1;
2307010d129SBorislav Petkov 	};
2317010d129SBorislav Petkov 	u64	    value;
2327010d129SBorislav Petkov };
2337010d129SBorislav Petkov 
2347010d129SBorislav Petkov #define PEBS_HSW_TSX_FLAGS	0xff00000000ULL
2357010d129SBorislav Petkov 
2367010d129SBorislav Petkov /* Same as HSW, plus TSC */
2377010d129SBorislav Petkov 
2387010d129SBorislav Petkov struct pebs_record_skl {
2397010d129SBorislav Petkov 	u64 flags, ip;
2407010d129SBorislav Petkov 	u64 ax, bx, cx, dx;
2417010d129SBorislav Petkov 	u64 si, di, bp, sp;
2427010d129SBorislav Petkov 	u64 r8,  r9,  r10, r11;
2437010d129SBorislav Petkov 	u64 r12, r13, r14, r15;
2447010d129SBorislav Petkov 	u64 status, dla, dse, lat;
2457010d129SBorislav Petkov 	u64 real_ip, tsx_tuning;
2467010d129SBorislav Petkov 	u64 tsc;
2477010d129SBorislav Petkov };
2487010d129SBorislav Petkov 
2497010d129SBorislav Petkov void init_debug_store_on_cpu(int cpu)
2507010d129SBorislav Petkov {
2517010d129SBorislav Petkov 	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
2527010d129SBorislav Petkov 
2537010d129SBorislav Petkov 	if (!ds)
2547010d129SBorislav Petkov 		return;
2557010d129SBorislav Petkov 
2567010d129SBorislav Petkov 	wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA,
2577010d129SBorislav Petkov 		     (u32)((u64)(unsigned long)ds),
2587010d129SBorislav Petkov 		     (u32)((u64)(unsigned long)ds >> 32));
2597010d129SBorislav Petkov }
2607010d129SBorislav Petkov 
2617010d129SBorislav Petkov void fini_debug_store_on_cpu(int cpu)
2627010d129SBorislav Petkov {
2637010d129SBorislav Petkov 	if (!per_cpu(cpu_hw_events, cpu).ds)
2647010d129SBorislav Petkov 		return;
2657010d129SBorislav Petkov 
2667010d129SBorislav Petkov 	wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0);
2677010d129SBorislav Petkov }
2687010d129SBorislav Petkov 
2697010d129SBorislav Petkov static DEFINE_PER_CPU(void *, insn_buffer);
2707010d129SBorislav Petkov 
2717010d129SBorislav Petkov static int alloc_pebs_buffer(int cpu)
2727010d129SBorislav Petkov {
2737010d129SBorislav Petkov 	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
2747010d129SBorislav Petkov 	int node = cpu_to_node(cpu);
2757010d129SBorislav Petkov 	int max;
2767010d129SBorislav Petkov 	void *buffer, *ibuffer;
2777010d129SBorislav Petkov 
2787010d129SBorislav Petkov 	if (!x86_pmu.pebs)
2797010d129SBorislav Petkov 		return 0;
2807010d129SBorislav Petkov 
281e72daf3fSJiri Olsa 	buffer = kzalloc_node(x86_pmu.pebs_buffer_size, GFP_KERNEL, node);
2827010d129SBorislav Petkov 	if (unlikely(!buffer))
2837010d129SBorislav Petkov 		return -ENOMEM;
2847010d129SBorislav Petkov 
2857010d129SBorislav Petkov 	/*
2867010d129SBorislav Petkov 	 * HSW+ already provides us the eventing ip; no need to allocate this
2877010d129SBorislav Petkov 	 * buffer then.
2887010d129SBorislav Petkov 	 */
2897010d129SBorislav Petkov 	if (x86_pmu.intel_cap.pebs_format < 2) {
2907010d129SBorislav Petkov 		ibuffer = kzalloc_node(PEBS_FIXUP_SIZE, GFP_KERNEL, node);
2917010d129SBorislav Petkov 		if (!ibuffer) {
2927010d129SBorislav Petkov 			kfree(buffer);
2937010d129SBorislav Petkov 			return -ENOMEM;
2947010d129SBorislav Petkov 		}
2957010d129SBorislav Petkov 		per_cpu(insn_buffer, cpu) = ibuffer;
2967010d129SBorislav Petkov 	}
2977010d129SBorislav Petkov 
298e72daf3fSJiri Olsa 	max = x86_pmu.pebs_buffer_size / x86_pmu.pebs_record_size;
2997010d129SBorislav Petkov 
3007010d129SBorislav Petkov 	ds->pebs_buffer_base = (u64)(unsigned long)buffer;
3017010d129SBorislav Petkov 	ds->pebs_index = ds->pebs_buffer_base;
3027010d129SBorislav Petkov 	ds->pebs_absolute_maximum = ds->pebs_buffer_base +
3037010d129SBorislav Petkov 		max * x86_pmu.pebs_record_size;
3047010d129SBorislav Petkov 
3057010d129SBorislav Petkov 	return 0;
3067010d129SBorislav Petkov }
3077010d129SBorislav Petkov 
3087010d129SBorislav Petkov static void release_pebs_buffer(int cpu)
3097010d129SBorislav Petkov {
3107010d129SBorislav Petkov 	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
3117010d129SBorislav Petkov 
3127010d129SBorislav Petkov 	if (!ds || !x86_pmu.pebs)
3137010d129SBorislav Petkov 		return;
3147010d129SBorislav Petkov 
3157010d129SBorislav Petkov 	kfree(per_cpu(insn_buffer, cpu));
3167010d129SBorislav Petkov 	per_cpu(insn_buffer, cpu) = NULL;
3177010d129SBorislav Petkov 
3187010d129SBorislav Petkov 	kfree((void *)(unsigned long)ds->pebs_buffer_base);
3197010d129SBorislav Petkov 	ds->pebs_buffer_base = 0;
3207010d129SBorislav Petkov }
3217010d129SBorislav Petkov 
3227010d129SBorislav Petkov static int alloc_bts_buffer(int cpu)
3237010d129SBorislav Petkov {
3247010d129SBorislav Petkov 	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
3257010d129SBorislav Petkov 	int node = cpu_to_node(cpu);
3267010d129SBorislav Petkov 	int max, thresh;
3277010d129SBorislav Petkov 	void *buffer;
3287010d129SBorislav Petkov 
3297010d129SBorislav Petkov 	if (!x86_pmu.bts)
3307010d129SBorislav Petkov 		return 0;
3317010d129SBorislav Petkov 
3327010d129SBorislav Petkov 	buffer = kzalloc_node(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, node);
3337010d129SBorislav Petkov 	if (unlikely(!buffer)) {
3347010d129SBorislav Petkov 		WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__);
3357010d129SBorislav Petkov 		return -ENOMEM;
3367010d129SBorislav Petkov 	}
3377010d129SBorislav Petkov 
3387010d129SBorislav Petkov 	max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE;
3397010d129SBorislav Petkov 	thresh = max / 16;
3407010d129SBorislav Petkov 
3417010d129SBorislav Petkov 	ds->bts_buffer_base = (u64)(unsigned long)buffer;
3427010d129SBorislav Petkov 	ds->bts_index = ds->bts_buffer_base;
3437010d129SBorislav Petkov 	ds->bts_absolute_maximum = ds->bts_buffer_base +
3447010d129SBorislav Petkov 		max * BTS_RECORD_SIZE;
3457010d129SBorislav Petkov 	ds->bts_interrupt_threshold = ds->bts_absolute_maximum -
3467010d129SBorislav Petkov 		thresh * BTS_RECORD_SIZE;
3477010d129SBorislav Petkov 
3487010d129SBorislav Petkov 	return 0;
3497010d129SBorislav Petkov }
3507010d129SBorislav Petkov 
3517010d129SBorislav Petkov static void release_bts_buffer(int cpu)
3527010d129SBorislav Petkov {
3537010d129SBorislav Petkov 	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
3547010d129SBorislav Petkov 
3557010d129SBorislav Petkov 	if (!ds || !x86_pmu.bts)
3567010d129SBorislav Petkov 		return;
3577010d129SBorislav Petkov 
3587010d129SBorislav Petkov 	kfree((void *)(unsigned long)ds->bts_buffer_base);
3597010d129SBorislav Petkov 	ds->bts_buffer_base = 0;
3607010d129SBorislav Petkov }
3617010d129SBorislav Petkov 
3627010d129SBorislav Petkov static int alloc_ds_buffer(int cpu)
3637010d129SBorislav Petkov {
3647010d129SBorislav Petkov 	int node = cpu_to_node(cpu);
3657010d129SBorislav Petkov 	struct debug_store *ds;
3667010d129SBorislav Petkov 
3677010d129SBorislav Petkov 	ds = kzalloc_node(sizeof(*ds), GFP_KERNEL, node);
3687010d129SBorislav Petkov 	if (unlikely(!ds))
3697010d129SBorislav Petkov 		return -ENOMEM;
3707010d129SBorislav Petkov 
3717010d129SBorislav Petkov 	per_cpu(cpu_hw_events, cpu).ds = ds;
3727010d129SBorislav Petkov 
3737010d129SBorislav Petkov 	return 0;
3747010d129SBorislav Petkov }
3757010d129SBorislav Petkov 
3767010d129SBorislav Petkov static void release_ds_buffer(int cpu)
3777010d129SBorislav Petkov {
3787010d129SBorislav Petkov 	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
3797010d129SBorislav Petkov 
3807010d129SBorislav Petkov 	if (!ds)
3817010d129SBorislav Petkov 		return;
3827010d129SBorislav Petkov 
3837010d129SBorislav Petkov 	per_cpu(cpu_hw_events, cpu).ds = NULL;
3847010d129SBorislav Petkov 	kfree(ds);
3857010d129SBorislav Petkov }
3867010d129SBorislav Petkov 
3877010d129SBorislav Petkov void release_ds_buffers(void)
3887010d129SBorislav Petkov {
3897010d129SBorislav Petkov 	int cpu;
3907010d129SBorislav Petkov 
3917010d129SBorislav Petkov 	if (!x86_pmu.bts && !x86_pmu.pebs)
3927010d129SBorislav Petkov 		return;
3937010d129SBorislav Petkov 
3947010d129SBorislav Petkov 	get_online_cpus();
3957010d129SBorislav Petkov 	for_each_online_cpu(cpu)
3967010d129SBorislav Petkov 		fini_debug_store_on_cpu(cpu);
3977010d129SBorislav Petkov 
3987010d129SBorislav Petkov 	for_each_possible_cpu(cpu) {
3997010d129SBorislav Petkov 		release_pebs_buffer(cpu);
4007010d129SBorislav Petkov 		release_bts_buffer(cpu);
4017010d129SBorislav Petkov 		release_ds_buffer(cpu);
4027010d129SBorislav Petkov 	}
4037010d129SBorislav Petkov 	put_online_cpus();
4047010d129SBorislav Petkov }
4057010d129SBorislav Petkov 
4067010d129SBorislav Petkov void reserve_ds_buffers(void)
4077010d129SBorislav Petkov {
4087010d129SBorislav Petkov 	int bts_err = 0, pebs_err = 0;
4097010d129SBorislav Petkov 	int cpu;
4107010d129SBorislav Petkov 
4117010d129SBorislav Petkov 	x86_pmu.bts_active = 0;
4127010d129SBorislav Petkov 	x86_pmu.pebs_active = 0;
4137010d129SBorislav Petkov 
4147010d129SBorislav Petkov 	if (!x86_pmu.bts && !x86_pmu.pebs)
4157010d129SBorislav Petkov 		return;
4167010d129SBorislav Petkov 
4177010d129SBorislav Petkov 	if (!x86_pmu.bts)
4187010d129SBorislav Petkov 		bts_err = 1;
4197010d129SBorislav Petkov 
4207010d129SBorislav Petkov 	if (!x86_pmu.pebs)
4217010d129SBorislav Petkov 		pebs_err = 1;
4227010d129SBorislav Petkov 
4237010d129SBorislav Petkov 	get_online_cpus();
4247010d129SBorislav Petkov 
4257010d129SBorislav Petkov 	for_each_possible_cpu(cpu) {
4267010d129SBorislav Petkov 		if (alloc_ds_buffer(cpu)) {
4277010d129SBorislav Petkov 			bts_err = 1;
4287010d129SBorislav Petkov 			pebs_err = 1;
4297010d129SBorislav Petkov 		}
4307010d129SBorislav Petkov 
4317010d129SBorislav Petkov 		if (!bts_err && alloc_bts_buffer(cpu))
4327010d129SBorislav Petkov 			bts_err = 1;
4337010d129SBorislav Petkov 
4347010d129SBorislav Petkov 		if (!pebs_err && alloc_pebs_buffer(cpu))
4357010d129SBorislav Petkov 			pebs_err = 1;
4367010d129SBorislav Petkov 
4377010d129SBorislav Petkov 		if (bts_err && pebs_err)
4387010d129SBorislav Petkov 			break;
4397010d129SBorislav Petkov 	}
4407010d129SBorislav Petkov 
4417010d129SBorislav Petkov 	if (bts_err) {
4427010d129SBorislav Petkov 		for_each_possible_cpu(cpu)
4437010d129SBorislav Petkov 			release_bts_buffer(cpu);
4447010d129SBorislav Petkov 	}
4457010d129SBorislav Petkov 
4467010d129SBorislav Petkov 	if (pebs_err) {
4477010d129SBorislav Petkov 		for_each_possible_cpu(cpu)
4487010d129SBorislav Petkov 			release_pebs_buffer(cpu);
4497010d129SBorislav Petkov 	}
4507010d129SBorislav Petkov 
4517010d129SBorislav Petkov 	if (bts_err && pebs_err) {
4527010d129SBorislav Petkov 		for_each_possible_cpu(cpu)
4537010d129SBorislav Petkov 			release_ds_buffer(cpu);
4547010d129SBorislav Petkov 	} else {
4557010d129SBorislav Petkov 		if (x86_pmu.bts && !bts_err)
4567010d129SBorislav Petkov 			x86_pmu.bts_active = 1;
4577010d129SBorislav Petkov 
4587010d129SBorislav Petkov 		if (x86_pmu.pebs && !pebs_err)
4597010d129SBorislav Petkov 			x86_pmu.pebs_active = 1;
4607010d129SBorislav Petkov 
4617010d129SBorislav Petkov 		for_each_online_cpu(cpu)
4627010d129SBorislav Petkov 			init_debug_store_on_cpu(cpu);
4637010d129SBorislav Petkov 	}
4647010d129SBorislav Petkov 
4657010d129SBorislav Petkov 	put_online_cpus();
4667010d129SBorislav Petkov }
4677010d129SBorislav Petkov 
4687010d129SBorislav Petkov /*
4697010d129SBorislav Petkov  * BTS
4707010d129SBorislav Petkov  */
4717010d129SBorislav Petkov 
4727010d129SBorislav Petkov struct event_constraint bts_constraint =
4737010d129SBorislav Petkov 	EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0);
4747010d129SBorislav Petkov 
4757010d129SBorislav Petkov void intel_pmu_enable_bts(u64 config)
4767010d129SBorislav Petkov {
4777010d129SBorislav Petkov 	unsigned long debugctlmsr;
4787010d129SBorislav Petkov 
4797010d129SBorislav Petkov 	debugctlmsr = get_debugctlmsr();
4807010d129SBorislav Petkov 
4817010d129SBorislav Petkov 	debugctlmsr |= DEBUGCTLMSR_TR;
4827010d129SBorislav Petkov 	debugctlmsr |= DEBUGCTLMSR_BTS;
4837010d129SBorislav Petkov 	if (config & ARCH_PERFMON_EVENTSEL_INT)
4847010d129SBorislav Petkov 		debugctlmsr |= DEBUGCTLMSR_BTINT;
4857010d129SBorislav Petkov 
4867010d129SBorislav Petkov 	if (!(config & ARCH_PERFMON_EVENTSEL_OS))
4877010d129SBorislav Petkov 		debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
4887010d129SBorislav Petkov 
4897010d129SBorislav Petkov 	if (!(config & ARCH_PERFMON_EVENTSEL_USR))
4907010d129SBorislav Petkov 		debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR;
4917010d129SBorislav Petkov 
4927010d129SBorislav Petkov 	update_debugctlmsr(debugctlmsr);
4937010d129SBorislav Petkov }
4947010d129SBorislav Petkov 
4957010d129SBorislav Petkov void intel_pmu_disable_bts(void)
4967010d129SBorislav Petkov {
4977010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
4987010d129SBorislav Petkov 	unsigned long debugctlmsr;
4997010d129SBorislav Petkov 
5007010d129SBorislav Petkov 	if (!cpuc->ds)
5017010d129SBorislav Petkov 		return;
5027010d129SBorislav Petkov 
5037010d129SBorislav Petkov 	debugctlmsr = get_debugctlmsr();
5047010d129SBorislav Petkov 
5057010d129SBorislav Petkov 	debugctlmsr &=
5067010d129SBorislav Petkov 		~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT |
5077010d129SBorislav Petkov 		  DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR);
5087010d129SBorislav Petkov 
5097010d129SBorislav Petkov 	update_debugctlmsr(debugctlmsr);
5107010d129SBorislav Petkov }
5117010d129SBorislav Petkov 
5127010d129SBorislav Petkov int intel_pmu_drain_bts_buffer(void)
5137010d129SBorislav Petkov {
5147010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
5157010d129SBorislav Petkov 	struct debug_store *ds = cpuc->ds;
5167010d129SBorislav Petkov 	struct bts_record {
5177010d129SBorislav Petkov 		u64	from;
5187010d129SBorislav Petkov 		u64	to;
5197010d129SBorislav Petkov 		u64	flags;
5207010d129SBorislav Petkov 	};
5217010d129SBorislav Petkov 	struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
5227010d129SBorislav Petkov 	struct bts_record *at, *base, *top;
5237010d129SBorislav Petkov 	struct perf_output_handle handle;
5247010d129SBorislav Petkov 	struct perf_event_header header;
5257010d129SBorislav Petkov 	struct perf_sample_data data;
5267010d129SBorislav Petkov 	unsigned long skip = 0;
5277010d129SBorislav Petkov 	struct pt_regs regs;
5287010d129SBorislav Petkov 
5297010d129SBorislav Petkov 	if (!event)
5307010d129SBorislav Petkov 		return 0;
5317010d129SBorislav Petkov 
5327010d129SBorislav Petkov 	if (!x86_pmu.bts_active)
5337010d129SBorislav Petkov 		return 0;
5347010d129SBorislav Petkov 
5357010d129SBorislav Petkov 	base = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
5367010d129SBorislav Petkov 	top  = (struct bts_record *)(unsigned long)ds->bts_index;
5377010d129SBorislav Petkov 
5387010d129SBorislav Petkov 	if (top <= base)
5397010d129SBorislav Petkov 		return 0;
5407010d129SBorislav Petkov 
5417010d129SBorislav Petkov 	memset(&regs, 0, sizeof(regs));
5427010d129SBorislav Petkov 
5437010d129SBorislav Petkov 	ds->bts_index = ds->bts_buffer_base;
5447010d129SBorislav Petkov 
5457010d129SBorislav Petkov 	perf_sample_data_init(&data, 0, event->hw.last_period);
5467010d129SBorislav Petkov 
5477010d129SBorislav Petkov 	/*
5487010d129SBorislav Petkov 	 * BTS leaks kernel addresses in branches across the cpl boundary,
5497010d129SBorislav Petkov 	 * such as traps or system calls, so unless the user is asking for
5507010d129SBorislav Petkov 	 * kernel tracing (and right now it's not possible), we'd need to
5517010d129SBorislav Petkov 	 * filter them out. But first we need to count how many of those we
5527010d129SBorislav Petkov 	 * have in the current batch. This is an extra O(n) pass, however,
5537010d129SBorislav Petkov 	 * it's much faster than the other one especially considering that
5547010d129SBorislav Petkov 	 * n <= 2560 (BTS_BUFFER_SIZE / BTS_RECORD_SIZE * 15/16; see the
5557010d129SBorislav Petkov 	 * alloc_bts_buffer()).
5567010d129SBorislav Petkov 	 */
5577010d129SBorislav Petkov 	for (at = base; at < top; at++) {
5587010d129SBorislav Petkov 		/*
5597010d129SBorislav Petkov 		 * Note that right now *this* BTS code only works if
5607010d129SBorislav Petkov 		 * attr::exclude_kernel is set, but let's keep this extra
5617010d129SBorislav Petkov 		 * check here in case that changes.
5627010d129SBorislav Petkov 		 */
5637010d129SBorislav Petkov 		if (event->attr.exclude_kernel &&
5647010d129SBorislav Petkov 		    (kernel_ip(at->from) || kernel_ip(at->to)))
5657010d129SBorislav Petkov 			skip++;
5667010d129SBorislav Petkov 	}
5677010d129SBorislav Petkov 
5687010d129SBorislav Petkov 	/*
5697010d129SBorislav Petkov 	 * Prepare a generic sample, i.e. fill in the invariant fields.
5707010d129SBorislav Petkov 	 * We will overwrite the from and to address before we output
5717010d129SBorislav Petkov 	 * the sample.
5727010d129SBorislav Petkov 	 */
5737010d129SBorislav Petkov 	perf_prepare_sample(&header, &data, event, &regs);
5747010d129SBorislav Petkov 
5757010d129SBorislav Petkov 	if (perf_output_begin(&handle, event, header.size *
5767010d129SBorislav Petkov 			      (top - base - skip)))
5777010d129SBorislav Petkov 		return 1;
5787010d129SBorislav Petkov 
5797010d129SBorislav Petkov 	for (at = base; at < top; at++) {
5807010d129SBorislav Petkov 		/* Filter out any records that contain kernel addresses. */
5817010d129SBorislav Petkov 		if (event->attr.exclude_kernel &&
5827010d129SBorislav Petkov 		    (kernel_ip(at->from) || kernel_ip(at->to)))
5837010d129SBorislav Petkov 			continue;
5847010d129SBorislav Petkov 
5857010d129SBorislav Petkov 		data.ip		= at->from;
5867010d129SBorislav Petkov 		data.addr	= at->to;
5877010d129SBorislav Petkov 
5887010d129SBorislav Petkov 		perf_output_sample(&handle, &header, &data, event);
5897010d129SBorislav Petkov 	}
5907010d129SBorislav Petkov 
5917010d129SBorislav Petkov 	perf_output_end(&handle);
5927010d129SBorislav Petkov 
5937010d129SBorislav Petkov 	/* There's new data available. */
5947010d129SBorislav Petkov 	event->hw.interrupts++;
5957010d129SBorislav Petkov 	event->pending_kill = POLL_IN;
5967010d129SBorislav Petkov 	return 1;
5977010d129SBorislav Petkov }
5987010d129SBorislav Petkov 
5997010d129SBorislav Petkov static inline void intel_pmu_drain_pebs_buffer(void)
6007010d129SBorislav Petkov {
6017010d129SBorislav Petkov 	struct pt_regs regs;
6027010d129SBorislav Petkov 
6037010d129SBorislav Petkov 	x86_pmu.drain_pebs(&regs);
6047010d129SBorislav Petkov }
6057010d129SBorislav Petkov 
6067010d129SBorislav Petkov void intel_pmu_pebs_sched_task(struct perf_event_context *ctx, bool sched_in)
6077010d129SBorislav Petkov {
6087010d129SBorislav Petkov 	if (!sched_in)
6097010d129SBorislav Petkov 		intel_pmu_drain_pebs_buffer();
6107010d129SBorislav Petkov }
6117010d129SBorislav Petkov 
6127010d129SBorislav Petkov /*
6137010d129SBorislav Petkov  * PEBS
6147010d129SBorislav Petkov  */
6157010d129SBorislav Petkov struct event_constraint intel_core2_pebs_event_constraints[] = {
6167010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
6177010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
6187010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
6197010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
6207010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1),    /* MEM_LOAD_RETIRED.* */
6217010d129SBorislav Petkov 	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
6227010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01),
6237010d129SBorislav Petkov 	EVENT_CONSTRAINT_END
6247010d129SBorislav Petkov };
6257010d129SBorislav Petkov 
6267010d129SBorislav Petkov struct event_constraint intel_atom_pebs_event_constraints[] = {
6277010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
6287010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
6297010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1),    /* MEM_LOAD_RETIRED.* */
6307010d129SBorislav Petkov 	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
6317010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01),
6327010d129SBorislav Petkov 	/* Allow all events as PEBS with no flags */
6337010d129SBorislav Petkov 	INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
6347010d129SBorislav Petkov 	EVENT_CONSTRAINT_END
6357010d129SBorislav Petkov };
6367010d129SBorislav Petkov 
6377010d129SBorislav Petkov struct event_constraint intel_slm_pebs_event_constraints[] = {
6387010d129SBorislav Petkov 	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
6397010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x1),
6407010d129SBorislav Petkov 	/* Allow all events as PEBS with no flags */
6417010d129SBorislav Petkov 	INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
6427010d129SBorislav Petkov 	EVENT_CONSTRAINT_END
6437010d129SBorislav Petkov };
6447010d129SBorislav Petkov 
6457010d129SBorislav Petkov struct event_constraint intel_nehalem_pebs_event_constraints[] = {
6467010d129SBorislav Petkov 	INTEL_PLD_CONSTRAINT(0x100b, 0xf),      /* MEM_INST_RETIRED.* */
6477010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf),    /* MEM_UNCORE_RETIRED.* */
6487010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
6497010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf),    /* INST_RETIRED.ANY */
6507010d129SBorislav Petkov 	INTEL_EVENT_CONSTRAINT(0xc2, 0xf),    /* UOPS_RETIRED.* */
6517010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
6527010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
6537010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf),    /* SSEX_UOPS_RETIRED.* */
6547010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
6557010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf),    /* MEM_LOAD_RETIRED.* */
6567010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf),    /* FP_ASSIST.* */
6577010d129SBorislav Petkov 	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
6587010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
6597010d129SBorislav Petkov 	EVENT_CONSTRAINT_END
6607010d129SBorislav Petkov };
6617010d129SBorislav Petkov 
6627010d129SBorislav Petkov struct event_constraint intel_westmere_pebs_event_constraints[] = {
6637010d129SBorislav Petkov 	INTEL_PLD_CONSTRAINT(0x100b, 0xf),      /* MEM_INST_RETIRED.* */
6647010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf),    /* MEM_UNCORE_RETIRED.* */
6657010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
6667010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf),    /* INSTR_RETIRED.* */
6677010d129SBorislav Petkov 	INTEL_EVENT_CONSTRAINT(0xc2, 0xf),    /* UOPS_RETIRED.* */
6687010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
6697010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc5, 0xf),    /* BR_MISP_RETIRED.* */
6707010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf),    /* SSEX_UOPS_RETIRED.* */
6717010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
6727010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf),    /* MEM_LOAD_RETIRED.* */
6737010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf),    /* FP_ASSIST.* */
6747010d129SBorislav Petkov 	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
6757010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
6767010d129SBorislav Petkov 	EVENT_CONSTRAINT_END
6777010d129SBorislav Petkov };
6787010d129SBorislav Petkov 
6797010d129SBorislav Petkov struct event_constraint intel_snb_pebs_event_constraints[] = {
6807010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
6817010d129SBorislav Petkov 	INTEL_PLD_CONSTRAINT(0x01cd, 0x8),    /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
6827010d129SBorislav Petkov 	INTEL_PST_CONSTRAINT(0x02cd, 0x8),    /* MEM_TRANS_RETIRED.PRECISE_STORES */
6837010d129SBorislav Petkov 	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
6847010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
6857010d129SBorislav Petkov         INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf),    /* MEM_UOP_RETIRED.* */
6867010d129SBorislav Petkov         INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
6877010d129SBorislav Petkov         INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf),    /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
6887010d129SBorislav Petkov         INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf),    /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
6897010d129SBorislav Petkov 	/* Allow all events as PEBS with no flags */
6907010d129SBorislav Petkov 	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
6917010d129SBorislav Petkov 	EVENT_CONSTRAINT_END
6927010d129SBorislav Petkov };
6937010d129SBorislav Petkov 
6947010d129SBorislav Petkov struct event_constraint intel_ivb_pebs_event_constraints[] = {
6957010d129SBorislav Petkov         INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
6967010d129SBorislav Petkov         INTEL_PLD_CONSTRAINT(0x01cd, 0x8),    /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
6977010d129SBorislav Petkov 	INTEL_PST_CONSTRAINT(0x02cd, 0x8),    /* MEM_TRANS_RETIRED.PRECISE_STORES */
6987010d129SBorislav Petkov 	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
6997010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
7007010d129SBorislav Petkov 	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
7017010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
7027010d129SBorislav Petkov 	INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf),    /* MEM_UOP_RETIRED.* */
7037010d129SBorislav Petkov 	INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
7047010d129SBorislav Petkov 	INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf),    /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
7057010d129SBorislav Petkov 	INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf),    /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
7067010d129SBorislav Petkov 	/* Allow all events as PEBS with no flags */
7077010d129SBorislav Petkov 	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
7087010d129SBorislav Petkov         EVENT_CONSTRAINT_END
7097010d129SBorislav Petkov };
7107010d129SBorislav Petkov 
7117010d129SBorislav Petkov struct event_constraint intel_hsw_pebs_event_constraints[] = {
7127010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
7137010d129SBorislav Petkov 	INTEL_PLD_CONSTRAINT(0x01cd, 0xf),    /* MEM_TRANS_RETIRED.* */
7147010d129SBorislav Petkov 	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
7157010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
7167010d129SBorislav Petkov 	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
7177010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
7187010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
7197010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
7207010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
7217010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
7227010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
7237010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
7247010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
7257010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
7267010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
7277010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd2, 0xf),    /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
7287010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd3, 0xf),    /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
7297010d129SBorislav Petkov 	/* Allow all events as PEBS with no flags */
7307010d129SBorislav Petkov 	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
7317010d129SBorislav Petkov 	EVENT_CONSTRAINT_END
7327010d129SBorislav Petkov };
7337010d129SBorislav Petkov 
734b3e62463SStephane Eranian struct event_constraint intel_bdw_pebs_event_constraints[] = {
735b3e62463SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
736b3e62463SStephane Eranian 	INTEL_PLD_CONSTRAINT(0x01cd, 0xf),    /* MEM_TRANS_RETIRED.* */
737b3e62463SStephane Eranian 	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
738b3e62463SStephane Eranian 	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
739b3e62463SStephane Eranian 	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
740b3e62463SStephane Eranian 	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
741b3e62463SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
742b3e62463SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
743b3e62463SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
744b3e62463SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
745b3e62463SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
746b3e62463SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
747b3e62463SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
748b3e62463SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
749b3e62463SStephane Eranian 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
750b3e62463SStephane Eranian 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf),    /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
751b3e62463SStephane Eranian 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf),    /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
752b3e62463SStephane Eranian 	/* Allow all events as PEBS with no flags */
753b3e62463SStephane Eranian 	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
754b3e62463SStephane Eranian 	EVENT_CONSTRAINT_END
755b3e62463SStephane Eranian };
756b3e62463SStephane Eranian 
757b3e62463SStephane Eranian 
7587010d129SBorislav Petkov struct event_constraint intel_skl_pebs_event_constraints[] = {
7597010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x1c0, 0x2),	/* INST_RETIRED.PREC_DIST */
7607010d129SBorislav Petkov 	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
7617010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
7627010d129SBorislav Petkov 	/* INST_RETIRED.TOTAL_CYCLES_PS (inv=1, cmask=16) (cycles:p). */
7637010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
7647010d129SBorislav Petkov 	INTEL_PLD_CONSTRAINT(0x1cd, 0xf),		      /* MEM_TRANS_RETIRED.* */
7657010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */
7667010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */
7677010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */
7687010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x22d0, 0xf), /* MEM_INST_RETIRED.LOCK_STORES */
7697010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */
7707010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */
7717010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */
7727010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */
7737010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf),    /* MEM_LOAD_RETIRED.* */
7747010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf),    /* MEM_LOAD_L3_HIT_RETIRED.* */
7757010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf),    /* MEM_LOAD_L3_MISS_RETIRED.* */
7767010d129SBorislav Petkov 	/* Allow all events as PEBS with no flags */
7777010d129SBorislav Petkov 	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
7787010d129SBorislav Petkov 	EVENT_CONSTRAINT_END
7797010d129SBorislav Petkov };
7807010d129SBorislav Petkov 
7817010d129SBorislav Petkov struct event_constraint *intel_pebs_constraints(struct perf_event *event)
7827010d129SBorislav Petkov {
7837010d129SBorislav Petkov 	struct event_constraint *c;
7847010d129SBorislav Petkov 
7857010d129SBorislav Petkov 	if (!event->attr.precise_ip)
7867010d129SBorislav Petkov 		return NULL;
7877010d129SBorislav Petkov 
7887010d129SBorislav Petkov 	if (x86_pmu.pebs_constraints) {
7897010d129SBorislav Petkov 		for_each_event_constraint(c, x86_pmu.pebs_constraints) {
7907010d129SBorislav Petkov 			if ((event->hw.config & c->cmask) == c->code) {
7917010d129SBorislav Petkov 				event->hw.flags |= c->flags;
7927010d129SBorislav Petkov 				return c;
7937010d129SBorislav Petkov 			}
7947010d129SBorislav Petkov 		}
7957010d129SBorislav Petkov 	}
7967010d129SBorislav Petkov 
7977010d129SBorislav Petkov 	return &emptyconstraint;
7987010d129SBorislav Petkov }
7997010d129SBorislav Petkov 
8007010d129SBorislav Petkov static inline bool pebs_is_enabled(struct cpu_hw_events *cpuc)
8017010d129SBorislav Petkov {
8027010d129SBorislav Petkov 	return (cpuc->pebs_enabled & ((1ULL << MAX_PEBS_EVENTS) - 1));
8037010d129SBorislav Petkov }
8047010d129SBorislav Petkov 
8057010d129SBorislav Petkov void intel_pmu_pebs_enable(struct perf_event *event)
8067010d129SBorislav Petkov {
8077010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
8087010d129SBorislav Petkov 	struct hw_perf_event *hwc = &event->hw;
8097010d129SBorislav Petkov 	struct debug_store *ds = cpuc->ds;
8107010d129SBorislav Petkov 	bool first_pebs;
8117010d129SBorislav Petkov 	u64 threshold;
8127010d129SBorislav Petkov 
8137010d129SBorislav Petkov 	hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;
8147010d129SBorislav Petkov 
8157010d129SBorislav Petkov 	first_pebs = !pebs_is_enabled(cpuc);
8167010d129SBorislav Petkov 	cpuc->pebs_enabled |= 1ULL << hwc->idx;
8177010d129SBorislav Petkov 
8187010d129SBorislav Petkov 	if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
8197010d129SBorislav Petkov 		cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32);
8207010d129SBorislav Petkov 	else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
8217010d129SBorislav Petkov 		cpuc->pebs_enabled |= 1ULL << 63;
8227010d129SBorislav Petkov 
8237010d129SBorislav Petkov 	/*
8247010d129SBorislav Petkov 	 * When the event is constrained enough we can use a larger
8257010d129SBorislav Petkov 	 * threshold and run the event with less frequent PMI.
8267010d129SBorislav Petkov 	 */
8277010d129SBorislav Petkov 	if (hwc->flags & PERF_X86_EVENT_FREERUNNING) {
8287010d129SBorislav Petkov 		threshold = ds->pebs_absolute_maximum -
8297010d129SBorislav Petkov 			x86_pmu.max_pebs_events * x86_pmu.pebs_record_size;
8307010d129SBorislav Petkov 
8317010d129SBorislav Petkov 		if (first_pebs)
8327010d129SBorislav Petkov 			perf_sched_cb_inc(event->ctx->pmu);
8337010d129SBorislav Petkov 	} else {
8347010d129SBorislav Petkov 		threshold = ds->pebs_buffer_base + x86_pmu.pebs_record_size;
8357010d129SBorislav Petkov 
8367010d129SBorislav Petkov 		/*
8377010d129SBorislav Petkov 		 * If not all events can use larger buffer,
8387010d129SBorislav Petkov 		 * roll back to threshold = 1
8397010d129SBorislav Petkov 		 */
8407010d129SBorislav Petkov 		if (!first_pebs &&
8417010d129SBorislav Petkov 		    (ds->pebs_interrupt_threshold > threshold))
8427010d129SBorislav Petkov 			perf_sched_cb_dec(event->ctx->pmu);
8437010d129SBorislav Petkov 	}
8447010d129SBorislav Petkov 
8457010d129SBorislav Petkov 	/* Use auto-reload if possible to save a MSR write in the PMI */
8467010d129SBorislav Petkov 	if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
8477010d129SBorislav Petkov 		ds->pebs_event_reset[hwc->idx] =
8487010d129SBorislav Petkov 			(u64)(-hwc->sample_period) & x86_pmu.cntval_mask;
8497010d129SBorislav Petkov 	}
8507010d129SBorislav Petkov 
8517010d129SBorislav Petkov 	if (first_pebs || ds->pebs_interrupt_threshold > threshold)
8527010d129SBorislav Petkov 		ds->pebs_interrupt_threshold = threshold;
8537010d129SBorislav Petkov }
8547010d129SBorislav Petkov 
8557010d129SBorislav Petkov void intel_pmu_pebs_disable(struct perf_event *event)
8567010d129SBorislav Petkov {
8577010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
8587010d129SBorislav Petkov 	struct hw_perf_event *hwc = &event->hw;
8597010d129SBorislav Petkov 	struct debug_store *ds = cpuc->ds;
8607010d129SBorislav Petkov 	bool large_pebs = ds->pebs_interrupt_threshold >
8617010d129SBorislav Petkov 		ds->pebs_buffer_base + x86_pmu.pebs_record_size;
8627010d129SBorislav Petkov 
8637010d129SBorislav Petkov 	if (large_pebs)
8647010d129SBorislav Petkov 		intel_pmu_drain_pebs_buffer();
8657010d129SBorislav Petkov 
8667010d129SBorislav Petkov 	cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
8677010d129SBorislav Petkov 
8687010d129SBorislav Petkov 	if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
8697010d129SBorislav Petkov 		cpuc->pebs_enabled &= ~(1ULL << (hwc->idx + 32));
8707010d129SBorislav Petkov 	else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
8717010d129SBorislav Petkov 		cpuc->pebs_enabled &= ~(1ULL << 63);
8727010d129SBorislav Petkov 
8737010d129SBorislav Petkov 	if (large_pebs && !pebs_is_enabled(cpuc))
8747010d129SBorislav Petkov 		perf_sched_cb_dec(event->ctx->pmu);
8757010d129SBorislav Petkov 
8767010d129SBorislav Petkov 	if (cpuc->enabled)
8777010d129SBorislav Petkov 		wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
8787010d129SBorislav Petkov 
8797010d129SBorislav Petkov 	hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
8807010d129SBorislav Petkov }
8817010d129SBorislav Petkov 
8827010d129SBorislav Petkov void intel_pmu_pebs_enable_all(void)
8837010d129SBorislav Petkov {
8847010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
8857010d129SBorislav Petkov 
8867010d129SBorislav Petkov 	if (cpuc->pebs_enabled)
8877010d129SBorislav Petkov 		wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
8887010d129SBorislav Petkov }
8897010d129SBorislav Petkov 
8907010d129SBorislav Petkov void intel_pmu_pebs_disable_all(void)
8917010d129SBorislav Petkov {
8927010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
8937010d129SBorislav Petkov 
8947010d129SBorislav Petkov 	if (cpuc->pebs_enabled)
8957010d129SBorislav Petkov 		wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
8967010d129SBorislav Petkov }
8977010d129SBorislav Petkov 
8987010d129SBorislav Petkov static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
8997010d129SBorislav Petkov {
9007010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
9017010d129SBorislav Petkov 	unsigned long from = cpuc->lbr_entries[0].from;
9027010d129SBorislav Petkov 	unsigned long old_to, to = cpuc->lbr_entries[0].to;
9037010d129SBorislav Petkov 	unsigned long ip = regs->ip;
9047010d129SBorislav Petkov 	int is_64bit = 0;
9057010d129SBorislav Petkov 	void *kaddr;
9067010d129SBorislav Petkov 	int size;
9077010d129SBorislav Petkov 
9087010d129SBorislav Petkov 	/*
9097010d129SBorislav Petkov 	 * We don't need to fixup if the PEBS assist is fault like
9107010d129SBorislav Petkov 	 */
9117010d129SBorislav Petkov 	if (!x86_pmu.intel_cap.pebs_trap)
9127010d129SBorislav Petkov 		return 1;
9137010d129SBorislav Petkov 
9147010d129SBorislav Petkov 	/*
9157010d129SBorislav Petkov 	 * No LBR entry, no basic block, no rewinding
9167010d129SBorislav Petkov 	 */
9177010d129SBorislav Petkov 	if (!cpuc->lbr_stack.nr || !from || !to)
9187010d129SBorislav Petkov 		return 0;
9197010d129SBorislav Petkov 
9207010d129SBorislav Petkov 	/*
9217010d129SBorislav Petkov 	 * Basic blocks should never cross user/kernel boundaries
9227010d129SBorislav Petkov 	 */
9237010d129SBorislav Petkov 	if (kernel_ip(ip) != kernel_ip(to))
9247010d129SBorislav Petkov 		return 0;
9257010d129SBorislav Petkov 
9267010d129SBorislav Petkov 	/*
9277010d129SBorislav Petkov 	 * unsigned math, either ip is before the start (impossible) or
9287010d129SBorislav Petkov 	 * the basic block is larger than 1 page (sanity)
9297010d129SBorislav Petkov 	 */
9307010d129SBorislav Petkov 	if ((ip - to) > PEBS_FIXUP_SIZE)
9317010d129SBorislav Petkov 		return 0;
9327010d129SBorislav Petkov 
9337010d129SBorislav Petkov 	/*
9347010d129SBorislav Petkov 	 * We sampled a branch insn, rewind using the LBR stack
9357010d129SBorislav Petkov 	 */
9367010d129SBorislav Petkov 	if (ip == to) {
9377010d129SBorislav Petkov 		set_linear_ip(regs, from);
9387010d129SBorislav Petkov 		return 1;
9397010d129SBorislav Petkov 	}
9407010d129SBorislav Petkov 
9417010d129SBorislav Petkov 	size = ip - to;
9427010d129SBorislav Petkov 	if (!kernel_ip(ip)) {
9437010d129SBorislav Petkov 		int bytes;
9447010d129SBorislav Petkov 		u8 *buf = this_cpu_read(insn_buffer);
9457010d129SBorislav Petkov 
9467010d129SBorislav Petkov 		/* 'size' must fit our buffer, see above */
9477010d129SBorislav Petkov 		bytes = copy_from_user_nmi(buf, (void __user *)to, size);
9487010d129SBorislav Petkov 		if (bytes != 0)
9497010d129SBorislav Petkov 			return 0;
9507010d129SBorislav Petkov 
9517010d129SBorislav Petkov 		kaddr = buf;
9527010d129SBorislav Petkov 	} else {
9537010d129SBorislav Petkov 		kaddr = (void *)to;
9547010d129SBorislav Petkov 	}
9557010d129SBorislav Petkov 
9567010d129SBorislav Petkov 	do {
9577010d129SBorislav Petkov 		struct insn insn;
9587010d129SBorislav Petkov 
9597010d129SBorislav Petkov 		old_to = to;
9607010d129SBorislav Petkov 
9617010d129SBorislav Petkov #ifdef CONFIG_X86_64
9627010d129SBorislav Petkov 		is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
9637010d129SBorislav Petkov #endif
9647010d129SBorislav Petkov 		insn_init(&insn, kaddr, size, is_64bit);
9657010d129SBorislav Petkov 		insn_get_length(&insn);
9667010d129SBorislav Petkov 		/*
9677010d129SBorislav Petkov 		 * Make sure there was not a problem decoding the
9687010d129SBorislav Petkov 		 * instruction and getting the length.  This is
9697010d129SBorislav Petkov 		 * doubly important because we have an infinite
9707010d129SBorislav Petkov 		 * loop if insn.length=0.
9717010d129SBorislav Petkov 		 */
9727010d129SBorislav Petkov 		if (!insn.length)
9737010d129SBorislav Petkov 			break;
9747010d129SBorislav Petkov 
9757010d129SBorislav Petkov 		to += insn.length;
9767010d129SBorislav Petkov 		kaddr += insn.length;
9777010d129SBorislav Petkov 		size -= insn.length;
9787010d129SBorislav Petkov 	} while (to < ip);
9797010d129SBorislav Petkov 
9807010d129SBorislav Petkov 	if (to == ip) {
9817010d129SBorislav Petkov 		set_linear_ip(regs, old_to);
9827010d129SBorislav Petkov 		return 1;
9837010d129SBorislav Petkov 	}
9847010d129SBorislav Petkov 
9857010d129SBorislav Petkov 	/*
9867010d129SBorislav Petkov 	 * Even though we decoded the basic block, the instruction stream
9877010d129SBorislav Petkov 	 * never matched the given IP, either the TO or the IP got corrupted.
9887010d129SBorislav Petkov 	 */
9897010d129SBorislav Petkov 	return 0;
9907010d129SBorislav Petkov }
9917010d129SBorislav Petkov 
9927010d129SBorislav Petkov static inline u64 intel_hsw_weight(struct pebs_record_skl *pebs)
9937010d129SBorislav Petkov {
9947010d129SBorislav Petkov 	if (pebs->tsx_tuning) {
9957010d129SBorislav Petkov 		union hsw_tsx_tuning tsx = { .value = pebs->tsx_tuning };
9967010d129SBorislav Petkov 		return tsx.cycles_last_block;
9977010d129SBorislav Petkov 	}
9987010d129SBorislav Petkov 	return 0;
9997010d129SBorislav Petkov }
10007010d129SBorislav Petkov 
10017010d129SBorislav Petkov static inline u64 intel_hsw_transaction(struct pebs_record_skl *pebs)
10027010d129SBorislav Petkov {
10037010d129SBorislav Petkov 	u64 txn = (pebs->tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32;
10047010d129SBorislav Petkov 
10057010d129SBorislav Petkov 	/* For RTM XABORTs also log the abort code from AX */
10067010d129SBorislav Petkov 	if ((txn & PERF_TXN_TRANSACTION) && (pebs->ax & 1))
10077010d129SBorislav Petkov 		txn |= ((pebs->ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
10087010d129SBorislav Petkov 	return txn;
10097010d129SBorislav Petkov }
10107010d129SBorislav Petkov 
10117010d129SBorislav Petkov static void setup_pebs_sample_data(struct perf_event *event,
10127010d129SBorislav Petkov 				   struct pt_regs *iregs, void *__pebs,
10137010d129SBorislav Petkov 				   struct perf_sample_data *data,
10147010d129SBorislav Petkov 				   struct pt_regs *regs)
10157010d129SBorislav Petkov {
10167010d129SBorislav Petkov #define PERF_X86_EVENT_PEBS_HSW_PREC \
10177010d129SBorislav Petkov 		(PERF_X86_EVENT_PEBS_ST_HSW | \
10187010d129SBorislav Petkov 		 PERF_X86_EVENT_PEBS_LD_HSW | \
10197010d129SBorislav Petkov 		 PERF_X86_EVENT_PEBS_NA_HSW)
10207010d129SBorislav Petkov 	/*
10217010d129SBorislav Petkov 	 * We cast to the biggest pebs_record but are careful not to
10227010d129SBorislav Petkov 	 * unconditionally access the 'extra' entries.
10237010d129SBorislav Petkov 	 */
10247010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
10257010d129SBorislav Petkov 	struct pebs_record_skl *pebs = __pebs;
10267010d129SBorislav Petkov 	u64 sample_type;
10277010d129SBorislav Petkov 	int fll, fst, dsrc;
10287010d129SBorislav Petkov 	int fl = event->hw.flags;
10297010d129SBorislav Petkov 
10307010d129SBorislav Petkov 	if (pebs == NULL)
10317010d129SBorislav Petkov 		return;
10327010d129SBorislav Petkov 
10337010d129SBorislav Petkov 	sample_type = event->attr.sample_type;
10347010d129SBorislav Petkov 	dsrc = sample_type & PERF_SAMPLE_DATA_SRC;
10357010d129SBorislav Petkov 
10367010d129SBorislav Petkov 	fll = fl & PERF_X86_EVENT_PEBS_LDLAT;
10377010d129SBorislav Petkov 	fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC);
10387010d129SBorislav Petkov 
10397010d129SBorislav Petkov 	perf_sample_data_init(data, 0, event->hw.last_period);
10407010d129SBorislav Petkov 
10417010d129SBorislav Petkov 	data->period = event->hw.last_period;
10427010d129SBorislav Petkov 
10437010d129SBorislav Petkov 	/*
10447010d129SBorislav Petkov 	 * Use latency for weight (only avail with PEBS-LL)
10457010d129SBorislav Petkov 	 */
10467010d129SBorislav Petkov 	if (fll && (sample_type & PERF_SAMPLE_WEIGHT))
10477010d129SBorislav Petkov 		data->weight = pebs->lat;
10487010d129SBorislav Petkov 
10497010d129SBorislav Petkov 	/*
10507010d129SBorislav Petkov 	 * data.data_src encodes the data source
10517010d129SBorislav Petkov 	 */
10527010d129SBorislav Petkov 	if (dsrc) {
10537010d129SBorislav Petkov 		u64 val = PERF_MEM_NA;
10547010d129SBorislav Petkov 		if (fll)
10557010d129SBorislav Petkov 			val = load_latency_data(pebs->dse);
10567010d129SBorislav Petkov 		else if (fst && (fl & PERF_X86_EVENT_PEBS_HSW_PREC))
10577010d129SBorislav Petkov 			val = precise_datala_hsw(event, pebs->dse);
10587010d129SBorislav Petkov 		else if (fst)
10597010d129SBorislav Petkov 			val = precise_store_data(pebs->dse);
10607010d129SBorislav Petkov 		data->data_src.val = val;
10617010d129SBorislav Petkov 	}
10627010d129SBorislav Petkov 
10637010d129SBorislav Petkov 	/*
10647010d129SBorislav Petkov 	 * We use the interrupt regs as a base because the PEBS record
10657010d129SBorislav Petkov 	 * does not contain a full regs set, specifically it seems to
10667010d129SBorislav Petkov 	 * lack segment descriptors, which get used by things like
10677010d129SBorislav Petkov 	 * user_mode().
10687010d129SBorislav Petkov 	 *
10697010d129SBorislav Petkov 	 * In the simple case fix up only the IP and BP,SP regs, for
10707010d129SBorislav Petkov 	 * PERF_SAMPLE_IP and PERF_SAMPLE_CALLCHAIN to function properly.
10717010d129SBorislav Petkov 	 * A possible PERF_SAMPLE_REGS will have to transfer all regs.
10727010d129SBorislav Petkov 	 */
10737010d129SBorislav Petkov 	*regs = *iregs;
10747010d129SBorislav Petkov 	regs->flags = pebs->flags;
10757010d129SBorislav Petkov 	set_linear_ip(regs, pebs->ip);
10767010d129SBorislav Petkov 	regs->bp = pebs->bp;
10777010d129SBorislav Petkov 	regs->sp = pebs->sp;
10787010d129SBorislav Petkov 
10797010d129SBorislav Petkov 	if (sample_type & PERF_SAMPLE_REGS_INTR) {
10807010d129SBorislav Petkov 		regs->ax = pebs->ax;
10817010d129SBorislav Petkov 		regs->bx = pebs->bx;
10827010d129SBorislav Petkov 		regs->cx = pebs->cx;
10837010d129SBorislav Petkov 		regs->dx = pebs->dx;
10847010d129SBorislav Petkov 		regs->si = pebs->si;
10857010d129SBorislav Petkov 		regs->di = pebs->di;
10867010d129SBorislav Petkov 		regs->bp = pebs->bp;
10877010d129SBorislav Petkov 		regs->sp = pebs->sp;
10887010d129SBorislav Petkov 
10897010d129SBorislav Petkov 		regs->flags = pebs->flags;
10907010d129SBorislav Petkov #ifndef CONFIG_X86_32
10917010d129SBorislav Petkov 		regs->r8 = pebs->r8;
10927010d129SBorislav Petkov 		regs->r9 = pebs->r9;
10937010d129SBorislav Petkov 		regs->r10 = pebs->r10;
10947010d129SBorislav Petkov 		regs->r11 = pebs->r11;
10957010d129SBorislav Petkov 		regs->r12 = pebs->r12;
10967010d129SBorislav Petkov 		regs->r13 = pebs->r13;
10977010d129SBorislav Petkov 		regs->r14 = pebs->r14;
10987010d129SBorislav Petkov 		regs->r15 = pebs->r15;
10997010d129SBorislav Petkov #endif
11007010d129SBorislav Petkov 	}
11017010d129SBorislav Petkov 
11027010d129SBorislav Petkov 	if (event->attr.precise_ip > 1 && x86_pmu.intel_cap.pebs_format >= 2) {
11037010d129SBorislav Petkov 		regs->ip = pebs->real_ip;
11047010d129SBorislav Petkov 		regs->flags |= PERF_EFLAGS_EXACT;
11057010d129SBorislav Petkov 	} else if (event->attr.precise_ip > 1 && intel_pmu_pebs_fixup_ip(regs))
11067010d129SBorislav Petkov 		regs->flags |= PERF_EFLAGS_EXACT;
11077010d129SBorislav Petkov 	else
11087010d129SBorislav Petkov 		regs->flags &= ~PERF_EFLAGS_EXACT;
11097010d129SBorislav Petkov 
11107010d129SBorislav Petkov 	if ((sample_type & PERF_SAMPLE_ADDR) &&
11117010d129SBorislav Petkov 	    x86_pmu.intel_cap.pebs_format >= 1)
11127010d129SBorislav Petkov 		data->addr = pebs->dla;
11137010d129SBorislav Petkov 
11147010d129SBorislav Petkov 	if (x86_pmu.intel_cap.pebs_format >= 2) {
11157010d129SBorislav Petkov 		/* Only set the TSX weight when no memory weight. */
11167010d129SBorislav Petkov 		if ((sample_type & PERF_SAMPLE_WEIGHT) && !fll)
11177010d129SBorislav Petkov 			data->weight = intel_hsw_weight(pebs);
11187010d129SBorislav Petkov 
11197010d129SBorislav Petkov 		if (sample_type & PERF_SAMPLE_TRANSACTION)
11207010d129SBorislav Petkov 			data->txn = intel_hsw_transaction(pebs);
11217010d129SBorislav Petkov 	}
11227010d129SBorislav Petkov 
11237010d129SBorislav Petkov 	/*
11247010d129SBorislav Petkov 	 * v3 supplies an accurate time stamp, so we use that
11257010d129SBorislav Petkov 	 * for the time stamp.
11267010d129SBorislav Petkov 	 *
11277010d129SBorislav Petkov 	 * We can only do this for the default trace clock.
11287010d129SBorislav Petkov 	 */
11297010d129SBorislav Petkov 	if (x86_pmu.intel_cap.pebs_format >= 3 &&
11307010d129SBorislav Petkov 		event->attr.use_clockid == 0)
11317010d129SBorislav Petkov 		data->time = native_sched_clock_from_tsc(pebs->tsc);
11327010d129SBorislav Petkov 
11337010d129SBorislav Petkov 	if (has_branch_stack(event))
11347010d129SBorislav Petkov 		data->br_stack = &cpuc->lbr_stack;
11357010d129SBorislav Petkov }
11367010d129SBorislav Petkov 
11377010d129SBorislav Petkov static inline void *
11387010d129SBorislav Petkov get_next_pebs_record_by_bit(void *base, void *top, int bit)
11397010d129SBorislav Petkov {
11407010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
11417010d129SBorislav Petkov 	void *at;
11427010d129SBorislav Petkov 	u64 pebs_status;
11437010d129SBorislav Petkov 
11447010d129SBorislav Petkov 	/*
11457010d129SBorislav Petkov 	 * fmt0 does not have a status bitfield (does not use
11467010d129SBorislav Petkov 	 * perf_record_nhm format)
11477010d129SBorislav Petkov 	 */
11487010d129SBorislav Petkov 	if (x86_pmu.intel_cap.pebs_format < 1)
11497010d129SBorislav Petkov 		return base;
11507010d129SBorislav Petkov 
11517010d129SBorislav Petkov 	if (base == NULL)
11527010d129SBorislav Petkov 		return NULL;
11537010d129SBorislav Petkov 
11547010d129SBorislav Petkov 	for (at = base; at < top; at += x86_pmu.pebs_record_size) {
11557010d129SBorislav Petkov 		struct pebs_record_nhm *p = at;
11567010d129SBorislav Petkov 
11577010d129SBorislav Petkov 		if (test_bit(bit, (unsigned long *)&p->status)) {
11587010d129SBorislav Petkov 			/* PEBS v3 has accurate status bits */
11597010d129SBorislav Petkov 			if (x86_pmu.intel_cap.pebs_format >= 3)
11607010d129SBorislav Petkov 				return at;
11617010d129SBorislav Petkov 
11627010d129SBorislav Petkov 			if (p->status == (1 << bit))
11637010d129SBorislav Petkov 				return at;
11647010d129SBorislav Petkov 
11657010d129SBorislav Petkov 			/* clear non-PEBS bit and re-check */
11667010d129SBorislav Petkov 			pebs_status = p->status & cpuc->pebs_enabled;
11677010d129SBorislav Petkov 			pebs_status &= (1ULL << MAX_PEBS_EVENTS) - 1;
11687010d129SBorislav Petkov 			if (pebs_status == (1 << bit))
11697010d129SBorislav Petkov 				return at;
11707010d129SBorislav Petkov 		}
11717010d129SBorislav Petkov 	}
11727010d129SBorislav Petkov 	return NULL;
11737010d129SBorislav Petkov }
11747010d129SBorislav Petkov 
11757010d129SBorislav Petkov static void __intel_pmu_pebs_event(struct perf_event *event,
11767010d129SBorislav Petkov 				   struct pt_regs *iregs,
11777010d129SBorislav Petkov 				   void *base, void *top,
11787010d129SBorislav Petkov 				   int bit, int count)
11797010d129SBorislav Petkov {
11807010d129SBorislav Petkov 	struct perf_sample_data data;
11817010d129SBorislav Petkov 	struct pt_regs regs;
11827010d129SBorislav Petkov 	void *at = get_next_pebs_record_by_bit(base, top, bit);
11837010d129SBorislav Petkov 
11847010d129SBorislav Petkov 	if (!intel_pmu_save_and_restart(event) &&
11857010d129SBorislav Petkov 	    !(event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD))
11867010d129SBorislav Petkov 		return;
11877010d129SBorislav Petkov 
11887010d129SBorislav Petkov 	while (count > 1) {
11897010d129SBorislav Petkov 		setup_pebs_sample_data(event, iregs, at, &data, &regs);
11907010d129SBorislav Petkov 		perf_event_output(event, &data, &regs);
11917010d129SBorislav Petkov 		at += x86_pmu.pebs_record_size;
11927010d129SBorislav Petkov 		at = get_next_pebs_record_by_bit(at, top, bit);
11937010d129SBorislav Petkov 		count--;
11947010d129SBorislav Petkov 	}
11957010d129SBorislav Petkov 
11967010d129SBorislav Petkov 	setup_pebs_sample_data(event, iregs, at, &data, &regs);
11977010d129SBorislav Petkov 
11987010d129SBorislav Petkov 	/*
11997010d129SBorislav Petkov 	 * All but the last records are processed.
12007010d129SBorislav Petkov 	 * The last one is left to be able to call the overflow handler.
12017010d129SBorislav Petkov 	 */
12027010d129SBorislav Petkov 	if (perf_event_overflow(event, &data, &regs)) {
12037010d129SBorislav Petkov 		x86_pmu_stop(event, 0);
12047010d129SBorislav Petkov 		return;
12057010d129SBorislav Petkov 	}
12067010d129SBorislav Petkov 
12077010d129SBorislav Petkov }
12087010d129SBorislav Petkov 
12097010d129SBorislav Petkov static void intel_pmu_drain_pebs_core(struct pt_regs *iregs)
12107010d129SBorislav Petkov {
12117010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
12127010d129SBorislav Petkov 	struct debug_store *ds = cpuc->ds;
12137010d129SBorislav Petkov 	struct perf_event *event = cpuc->events[0]; /* PMC0 only */
12147010d129SBorislav Petkov 	struct pebs_record_core *at, *top;
12157010d129SBorislav Petkov 	int n;
12167010d129SBorislav Petkov 
12177010d129SBorislav Petkov 	if (!x86_pmu.pebs_active)
12187010d129SBorislav Petkov 		return;
12197010d129SBorislav Petkov 
12207010d129SBorislav Petkov 	at  = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base;
12217010d129SBorislav Petkov 	top = (struct pebs_record_core *)(unsigned long)ds->pebs_index;
12227010d129SBorislav Petkov 
12237010d129SBorislav Petkov 	/*
12247010d129SBorislav Petkov 	 * Whatever else happens, drain the thing
12257010d129SBorislav Petkov 	 */
12267010d129SBorislav Petkov 	ds->pebs_index = ds->pebs_buffer_base;
12277010d129SBorislav Petkov 
12287010d129SBorislav Petkov 	if (!test_bit(0, cpuc->active_mask))
12297010d129SBorislav Petkov 		return;
12307010d129SBorislav Petkov 
12317010d129SBorislav Petkov 	WARN_ON_ONCE(!event);
12327010d129SBorislav Petkov 
12337010d129SBorislav Petkov 	if (!event->attr.precise_ip)
12347010d129SBorislav Petkov 		return;
12357010d129SBorislav Petkov 
12367010d129SBorislav Petkov 	n = top - at;
12377010d129SBorislav Petkov 	if (n <= 0)
12387010d129SBorislav Petkov 		return;
12397010d129SBorislav Petkov 
12407010d129SBorislav Petkov 	__intel_pmu_pebs_event(event, iregs, at, top, 0, n);
12417010d129SBorislav Petkov }
12427010d129SBorislav Petkov 
12437010d129SBorislav Petkov static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
12447010d129SBorislav Petkov {
12457010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
12467010d129SBorislav Petkov 	struct debug_store *ds = cpuc->ds;
12477010d129SBorislav Petkov 	struct perf_event *event;
12487010d129SBorislav Petkov 	void *base, *at, *top;
12497010d129SBorislav Petkov 	short counts[MAX_PEBS_EVENTS] = {};
12507010d129SBorislav Petkov 	short error[MAX_PEBS_EVENTS] = {};
12517010d129SBorislav Petkov 	int bit, i;
12527010d129SBorislav Petkov 
12537010d129SBorislav Petkov 	if (!x86_pmu.pebs_active)
12547010d129SBorislav Petkov 		return;
12557010d129SBorislav Petkov 
12567010d129SBorislav Petkov 	base = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
12577010d129SBorislav Petkov 	top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
12587010d129SBorislav Petkov 
12597010d129SBorislav Petkov 	ds->pebs_index = ds->pebs_buffer_base;
12607010d129SBorislav Petkov 
12617010d129SBorislav Petkov 	if (unlikely(base >= top))
12627010d129SBorislav Petkov 		return;
12637010d129SBorislav Petkov 
12647010d129SBorislav Petkov 	for (at = base; at < top; at += x86_pmu.pebs_record_size) {
12657010d129SBorislav Petkov 		struct pebs_record_nhm *p = at;
12667010d129SBorislav Petkov 		u64 pebs_status;
12677010d129SBorislav Petkov 
12687010d129SBorislav Petkov 		/* PEBS v3 has accurate status bits */
12697010d129SBorislav Petkov 		if (x86_pmu.intel_cap.pebs_format >= 3) {
12707010d129SBorislav Petkov 			for_each_set_bit(bit, (unsigned long *)&p->status,
12717010d129SBorislav Petkov 					 MAX_PEBS_EVENTS)
12727010d129SBorislav Petkov 				counts[bit]++;
12737010d129SBorislav Petkov 
12747010d129SBorislav Petkov 			continue;
12757010d129SBorislav Petkov 		}
12767010d129SBorislav Petkov 
12777010d129SBorislav Petkov 		pebs_status = p->status & cpuc->pebs_enabled;
12787010d129SBorislav Petkov 		pebs_status &= (1ULL << x86_pmu.max_pebs_events) - 1;
12797010d129SBorislav Petkov 
12807010d129SBorislav Petkov 		/*
12817010d129SBorislav Petkov 		 * On some CPUs the PEBS status can be zero when PEBS is
12827010d129SBorislav Petkov 		 * racing with clearing of GLOBAL_STATUS.
12837010d129SBorislav Petkov 		 *
12847010d129SBorislav Petkov 		 * Normally we would drop that record, but in the
12857010d129SBorislav Petkov 		 * case when there is only a single active PEBS event
12867010d129SBorislav Petkov 		 * we can assume it's for that event.
12877010d129SBorislav Petkov 		 */
12887010d129SBorislav Petkov 		if (!pebs_status && cpuc->pebs_enabled &&
12897010d129SBorislav Petkov 			!(cpuc->pebs_enabled & (cpuc->pebs_enabled-1)))
12907010d129SBorislav Petkov 			pebs_status = cpuc->pebs_enabled;
12917010d129SBorislav Petkov 
12927010d129SBorislav Petkov 		bit = find_first_bit((unsigned long *)&pebs_status,
12937010d129SBorislav Petkov 					x86_pmu.max_pebs_events);
12947010d129SBorislav Petkov 		if (bit >= x86_pmu.max_pebs_events)
12957010d129SBorislav Petkov 			continue;
12967010d129SBorislav Petkov 
12977010d129SBorislav Petkov 		/*
12987010d129SBorislav Petkov 		 * The PEBS hardware does not deal well with the situation
12997010d129SBorislav Petkov 		 * when events happen near to each other and multiple bits
13007010d129SBorislav Petkov 		 * are set. But it should happen rarely.
13017010d129SBorislav Petkov 		 *
13027010d129SBorislav Petkov 		 * If these events include one PEBS and multiple non-PEBS
13037010d129SBorislav Petkov 		 * events, it doesn't impact PEBS record. The record will
13047010d129SBorislav Petkov 		 * be handled normally. (slow path)
13057010d129SBorislav Petkov 		 *
13067010d129SBorislav Petkov 		 * If these events include two or more PEBS events, the
13077010d129SBorislav Petkov 		 * records for the events can be collapsed into a single
13087010d129SBorislav Petkov 		 * one, and it's not possible to reconstruct all events
13097010d129SBorislav Petkov 		 * that caused the PEBS record. It's called collision.
13107010d129SBorislav Petkov 		 * If collision happened, the record will be dropped.
13117010d129SBorislav Petkov 		 */
13127010d129SBorislav Petkov 		if (p->status != (1ULL << bit)) {
13137010d129SBorislav Petkov 			for_each_set_bit(i, (unsigned long *)&pebs_status,
13147010d129SBorislav Petkov 					 x86_pmu.max_pebs_events)
13157010d129SBorislav Petkov 				error[i]++;
13167010d129SBorislav Petkov 			continue;
13177010d129SBorislav Petkov 		}
13187010d129SBorislav Petkov 
13197010d129SBorislav Petkov 		counts[bit]++;
13207010d129SBorislav Petkov 	}
13217010d129SBorislav Petkov 
13227010d129SBorislav Petkov 	for (bit = 0; bit < x86_pmu.max_pebs_events; bit++) {
13237010d129SBorislav Petkov 		if ((counts[bit] == 0) && (error[bit] == 0))
13247010d129SBorislav Petkov 			continue;
13257010d129SBorislav Petkov 
13267010d129SBorislav Petkov 		event = cpuc->events[bit];
13277010d129SBorislav Petkov 		WARN_ON_ONCE(!event);
13287010d129SBorislav Petkov 		WARN_ON_ONCE(!event->attr.precise_ip);
13297010d129SBorislav Petkov 
13307010d129SBorislav Petkov 		/* log dropped samples number */
13317010d129SBorislav Petkov 		if (error[bit])
13327010d129SBorislav Petkov 			perf_log_lost_samples(event, error[bit]);
13337010d129SBorislav Petkov 
13347010d129SBorislav Petkov 		if (counts[bit]) {
13357010d129SBorislav Petkov 			__intel_pmu_pebs_event(event, iregs, base,
13367010d129SBorislav Petkov 					       top, bit, counts[bit]);
13377010d129SBorislav Petkov 		}
13387010d129SBorislav Petkov 	}
13397010d129SBorislav Petkov }
13407010d129SBorislav Petkov 
13417010d129SBorislav Petkov /*
13427010d129SBorislav Petkov  * BTS, PEBS probe and setup
13437010d129SBorislav Petkov  */
13447010d129SBorislav Petkov 
13457010d129SBorislav Petkov void __init intel_ds_init(void)
13467010d129SBorislav Petkov {
13477010d129SBorislav Petkov 	/*
13487010d129SBorislav Petkov 	 * No support for 32bit formats
13497010d129SBorislav Petkov 	 */
13507010d129SBorislav Petkov 	if (!boot_cpu_has(X86_FEATURE_DTES64))
13517010d129SBorislav Petkov 		return;
13527010d129SBorislav Petkov 
13537010d129SBorislav Petkov 	x86_pmu.bts  = boot_cpu_has(X86_FEATURE_BTS);
13547010d129SBorislav Petkov 	x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS);
1355e72daf3fSJiri Olsa 	x86_pmu.pebs_buffer_size = PEBS_BUFFER_SIZE;
13567010d129SBorislav Petkov 	if (x86_pmu.pebs) {
13577010d129SBorislav Petkov 		char pebs_type = x86_pmu.intel_cap.pebs_trap ?  '+' : '-';
13587010d129SBorislav Petkov 		int format = x86_pmu.intel_cap.pebs_format;
13597010d129SBorislav Petkov 
13607010d129SBorislav Petkov 		switch (format) {
13617010d129SBorislav Petkov 		case 0:
13627010d129SBorislav Petkov 			pr_cont("PEBS fmt0%c, ", pebs_type);
13637010d129SBorislav Petkov 			x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
1364e72daf3fSJiri Olsa 			/*
1365e72daf3fSJiri Olsa 			 * Using >PAGE_SIZE buffers makes the WRMSR to
1366e72daf3fSJiri Olsa 			 * PERF_GLOBAL_CTRL in intel_pmu_enable_all()
1367e72daf3fSJiri Olsa 			 * mysteriously hang on Core2.
1368e72daf3fSJiri Olsa 			 *
1369e72daf3fSJiri Olsa 			 * As a workaround, we don't do this.
1370e72daf3fSJiri Olsa 			 */
1371e72daf3fSJiri Olsa 			x86_pmu.pebs_buffer_size = PAGE_SIZE;
13727010d129SBorislav Petkov 			x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
13737010d129SBorislav Petkov 			break;
13747010d129SBorislav Petkov 
13757010d129SBorislav Petkov 		case 1:
13767010d129SBorislav Petkov 			pr_cont("PEBS fmt1%c, ", pebs_type);
13777010d129SBorislav Petkov 			x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
13787010d129SBorislav Petkov 			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
13797010d129SBorislav Petkov 			break;
13807010d129SBorislav Petkov 
13817010d129SBorislav Petkov 		case 2:
13827010d129SBorislav Petkov 			pr_cont("PEBS fmt2%c, ", pebs_type);
13837010d129SBorislav Petkov 			x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw);
13847010d129SBorislav Petkov 			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
13857010d129SBorislav Petkov 			break;
13867010d129SBorislav Petkov 
13877010d129SBorislav Petkov 		case 3:
13887010d129SBorislav Petkov 			pr_cont("PEBS fmt3%c, ", pebs_type);
13897010d129SBorislav Petkov 			x86_pmu.pebs_record_size =
13907010d129SBorislav Petkov 						sizeof(struct pebs_record_skl);
13917010d129SBorislav Petkov 			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
13927010d129SBorislav Petkov 			x86_pmu.free_running_flags |= PERF_SAMPLE_TIME;
13937010d129SBorislav Petkov 			break;
13947010d129SBorislav Petkov 
13957010d129SBorislav Petkov 		default:
13967010d129SBorislav Petkov 			pr_cont("no PEBS fmt%d%c, ", format, pebs_type);
13977010d129SBorislav Petkov 			x86_pmu.pebs = 0;
13987010d129SBorislav Petkov 		}
13997010d129SBorislav Petkov 	}
14007010d129SBorislav Petkov }
14017010d129SBorislav Petkov 
14027010d129SBorislav Petkov void perf_restore_debug_store(void)
14037010d129SBorislav Petkov {
14047010d129SBorislav Petkov 	struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
14057010d129SBorislav Petkov 
14067010d129SBorislav Petkov 	if (!x86_pmu.bts && !x86_pmu.pebs)
14077010d129SBorislav Petkov 		return;
14087010d129SBorislav Petkov 
14097010d129SBorislav Petkov 	wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds);
14107010d129SBorislav Petkov }
1411