xref: /openbmc/linux/arch/x86/events/intel/ds.c (revision 31046500)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
27010d129SBorislav Petkov #include <linux/bitops.h>
37010d129SBorislav Petkov #include <linux/types.h>
47010d129SBorislav Petkov #include <linux/slab.h>
57010d129SBorislav Petkov 
6c1961a46SHugh Dickins #include <asm/cpu_entry_area.h>
77010d129SBorislav Petkov #include <asm/perf_event.h>
842f3bdc5SPeter Zijlstra #include <asm/tlbflush.h>
97010d129SBorislav Petkov #include <asm/insn.h>
1059e9f587SSean Christopherson #include <asm/io.h>
117010d129SBorislav Petkov 
1227f6d22bSBorislav Petkov #include "../perf_event.h"
137010d129SBorislav Petkov 
1410043e02SThomas Gleixner /* Waste a full page so it can be mapped into the cpu_entry_area */
1510043e02SThomas Gleixner DEFINE_PER_CPU_PAGE_ALIGNED(struct debug_store, cpu_debug_store);
1610043e02SThomas Gleixner 
177010d129SBorislav Petkov /* The size of a BTS record in bytes: */
187010d129SBorislav Petkov #define BTS_RECORD_SIZE		24
197010d129SBorislav Petkov 
207010d129SBorislav Petkov #define PEBS_FIXUP_SIZE		PAGE_SIZE
217010d129SBorislav Petkov 
227010d129SBorislav Petkov /*
237010d129SBorislav Petkov  * pebs_record_32 for p4 and core not supported
247010d129SBorislav Petkov 
257010d129SBorislav Petkov struct pebs_record_32 {
267010d129SBorislav Petkov 	u32 flags, ip;
277010d129SBorislav Petkov 	u32 ax, bc, cx, dx;
287010d129SBorislav Petkov 	u32 si, di, bp, sp;
297010d129SBorislav Petkov };
307010d129SBorislav Petkov 
317010d129SBorislav Petkov  */
327010d129SBorislav Petkov 
337010d129SBorislav Petkov union intel_x86_pebs_dse {
347010d129SBorislav Petkov 	u64 val;
357010d129SBorislav Petkov 	struct {
367010d129SBorislav Petkov 		unsigned int ld_dse:4;
377010d129SBorislav Petkov 		unsigned int ld_stlb_miss:1;
387010d129SBorislav Petkov 		unsigned int ld_locked:1;
3961b985e3SKan Liang 		unsigned int ld_data_blk:1;
4061b985e3SKan Liang 		unsigned int ld_addr_blk:1;
4161b985e3SKan Liang 		unsigned int ld_reserved:24;
427010d129SBorislav Petkov 	};
437010d129SBorislav Petkov 	struct {
447010d129SBorislav Petkov 		unsigned int st_l1d_hit:1;
457010d129SBorislav Petkov 		unsigned int st_reserved1:3;
467010d129SBorislav Petkov 		unsigned int st_stlb_miss:1;
477010d129SBorislav Petkov 		unsigned int st_locked:1;
487010d129SBorislav Petkov 		unsigned int st_reserved2:26;
497010d129SBorislav Petkov 	};
5061b985e3SKan Liang 	struct {
5161b985e3SKan Liang 		unsigned int st_lat_dse:4;
5261b985e3SKan Liang 		unsigned int st_lat_stlb_miss:1;
5361b985e3SKan Liang 		unsigned int st_lat_locked:1;
5461b985e3SKan Liang 		unsigned int ld_reserved3:26;
5561b985e3SKan Liang 	};
5638aaf921SKan Liang 	struct {
5738aaf921SKan Liang 		unsigned int mtl_dse:5;
5838aaf921SKan Liang 		unsigned int mtl_locked:1;
5938aaf921SKan Liang 		unsigned int mtl_stlb_miss:1;
6038aaf921SKan Liang 		unsigned int mtl_fwd_blk:1;
6138aaf921SKan Liang 		unsigned int ld_reserved4:24;
6238aaf921SKan Liang 	};
637010d129SBorislav Petkov };
647010d129SBorislav Petkov 
657010d129SBorislav Petkov 
667010d129SBorislav Petkov /*
677010d129SBorislav Petkov  * Map PEBS Load Latency Data Source encodings to generic
687010d129SBorislav Petkov  * memory data source information
697010d129SBorislav Petkov  */
707010d129SBorislav Petkov #define P(a, b) PERF_MEM_S(a, b)
717010d129SBorislav Petkov #define OP_LH (P(OP, LOAD) | P(LVL, HIT))
726ae5fa61SAndi Kleen #define LEVEL(x) P(LVLNUM, x)
736ae5fa61SAndi Kleen #define REM P(REMOTE, REMOTE)
747010d129SBorislav Petkov #define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS))
757010d129SBorislav Petkov 
76e17dc653SAndi Kleen /* Version for Sandy Bridge and later */
77e17dc653SAndi Kleen static u64 pebs_data_source[] = {
786ae5fa61SAndi Kleen 	P(OP, LOAD) | P(LVL, MISS) | LEVEL(L3) | P(SNOOP, NA),/* 0x00:ukn L3 */
796ae5fa61SAndi Kleen 	OP_LH | P(LVL, L1)  | LEVEL(L1) | P(SNOOP, NONE),  /* 0x01: L1 local */
806ae5fa61SAndi Kleen 	OP_LH | P(LVL, LFB) | LEVEL(LFB) | P(SNOOP, NONE), /* 0x02: LFB hit */
816ae5fa61SAndi Kleen 	OP_LH | P(LVL, L2)  | LEVEL(L2) | P(SNOOP, NONE),  /* 0x03: L2 hit */
826ae5fa61SAndi Kleen 	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, NONE),  /* 0x04: L3 hit */
836ae5fa61SAndi Kleen 	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, MISS),  /* 0x05: L3 hit, snoop miss */
846ae5fa61SAndi Kleen 	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, HIT),   /* 0x06: L3 hit, snoop hit */
856ae5fa61SAndi Kleen 	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, HITM),  /* 0x07: L3 hit, snoop hitm */
866ae5fa61SAndi Kleen 	OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HIT),  /* 0x08: L3 miss snoop hit */
876ae5fa61SAndi Kleen 	OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HITM), /* 0x09: L3 miss snoop hitm*/
886ae5fa61SAndi Kleen 	OP_LH | P(LVL, LOC_RAM)  | LEVEL(RAM) | P(SNOOP, HIT),       /* 0x0a: L3 miss, shared */
896ae5fa61SAndi Kleen 	OP_LH | P(LVL, REM_RAM1) | REM | LEVEL(L3) | P(SNOOP, HIT),  /* 0x0b: L3 miss, shared */
906ae5fa61SAndi Kleen 	OP_LH | P(LVL, LOC_RAM)  | LEVEL(RAM) | SNOOP_NONE_MISS,     /* 0x0c: L3 miss, excl */
916ae5fa61SAndi Kleen 	OP_LH | P(LVL, REM_RAM1) | LEVEL(RAM) | REM | SNOOP_NONE_MISS, /* 0x0d: L3 miss, excl */
926ae5fa61SAndi Kleen 	OP_LH | P(LVL, IO)  | LEVEL(NA) | P(SNOOP, NONE), /* 0x0e: I/O */
936ae5fa61SAndi Kleen 	OP_LH | P(LVL, UNC) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0f: uncached */
947010d129SBorislav Petkov };
957010d129SBorislav Petkov 
96e17dc653SAndi Kleen /* Patch up minor differences in the bits */
97e17dc653SAndi Kleen void __init intel_pmu_pebs_data_source_nhm(void)
98e17dc653SAndi Kleen {
996ae5fa61SAndi Kleen 	pebs_data_source[0x05] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT);
1006ae5fa61SAndi Kleen 	pebs_data_source[0x06] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
1016ae5fa61SAndi Kleen 	pebs_data_source[0x07] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
1026ae5fa61SAndi Kleen }
1036ae5fa61SAndi Kleen 
104ccf170e9SKan Liang static void __init __intel_pmu_pebs_data_source_skl(bool pmem, u64 *data_source)
1056ae5fa61SAndi Kleen {
1066ae5fa61SAndi Kleen 	u64 pmem_or_l4 = pmem ? LEVEL(PMEM) : LEVEL(L4);
1076ae5fa61SAndi Kleen 
108ccf170e9SKan Liang 	data_source[0x08] = OP_LH | pmem_or_l4 | P(SNOOP, HIT);
109ccf170e9SKan Liang 	data_source[0x09] = OP_LH | pmem_or_l4 | REM | P(SNOOP, HIT);
110ccf170e9SKan Liang 	data_source[0x0b] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, NONE);
111ccf170e9SKan Liang 	data_source[0x0c] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOPX, FWD);
112ccf170e9SKan Liang 	data_source[0x0d] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOP, HITM);
113ccf170e9SKan Liang }
114ccf170e9SKan Liang 
115ccf170e9SKan Liang void __init intel_pmu_pebs_data_source_skl(bool pmem)
116ccf170e9SKan Liang {
117ccf170e9SKan Liang 	__intel_pmu_pebs_data_source_skl(pmem, pebs_data_source);
118ccf170e9SKan Liang }
119ccf170e9SKan Liang 
12024919fdeSKan Liang static void __init __intel_pmu_pebs_data_source_grt(u64 *data_source)
121ccf170e9SKan Liang {
122ccf170e9SKan Liang 	data_source[0x05] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT);
123ccf170e9SKan Liang 	data_source[0x06] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
124ccf170e9SKan Liang 	data_source[0x08] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOPX, FWD);
125ccf170e9SKan Liang }
126ccf170e9SKan Liang 
12724919fdeSKan Liang void __init intel_pmu_pebs_data_source_grt(void)
12824919fdeSKan Liang {
12924919fdeSKan Liang 	__intel_pmu_pebs_data_source_grt(pebs_data_source);
13024919fdeSKan Liang }
13124919fdeSKan Liang 
132ccf170e9SKan Liang void __init intel_pmu_pebs_data_source_adl(void)
133ccf170e9SKan Liang {
134ccf170e9SKan Liang 	u64 *data_source;
135ccf170e9SKan Liang 
136ccf170e9SKan Liang 	data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX].pebs_data_source;
137ccf170e9SKan Liang 	memcpy(data_source, pebs_data_source, sizeof(pebs_data_source));
138ccf170e9SKan Liang 	__intel_pmu_pebs_data_source_skl(false, data_source);
139ccf170e9SKan Liang 
140ccf170e9SKan Liang 	data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_ATOM_IDX].pebs_data_source;
141ccf170e9SKan Liang 	memcpy(data_source, pebs_data_source, sizeof(pebs_data_source));
14224919fdeSKan Liang 	__intel_pmu_pebs_data_source_grt(data_source);
143e17dc653SAndi Kleen }
144e17dc653SAndi Kleen 
14538aaf921SKan Liang static void __init intel_pmu_pebs_data_source_cmt(u64 *data_source)
14638aaf921SKan Liang {
14738aaf921SKan Liang 	data_source[0x07] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOPX, FWD);
14838aaf921SKan Liang 	data_source[0x08] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
14938aaf921SKan Liang 	data_source[0x0a] = OP_LH | P(LVL, LOC_RAM)  | LEVEL(RAM) | P(SNOOP, NONE);
15038aaf921SKan Liang 	data_source[0x0b] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, NONE);
15138aaf921SKan Liang 	data_source[0x0c] = OP_LH | LEVEL(RAM) | REM | P(SNOOPX, FWD);
15238aaf921SKan Liang 	data_source[0x0d] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, HITM);
15338aaf921SKan Liang }
15438aaf921SKan Liang 
15538aaf921SKan Liang void __init intel_pmu_pebs_data_source_mtl(void)
15638aaf921SKan Liang {
15738aaf921SKan Liang 	u64 *data_source;
15838aaf921SKan Liang 
15938aaf921SKan Liang 	data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX].pebs_data_source;
16038aaf921SKan Liang 	memcpy(data_source, pebs_data_source, sizeof(pebs_data_source));
16138aaf921SKan Liang 	__intel_pmu_pebs_data_source_skl(false, data_source);
16238aaf921SKan Liang 
16338aaf921SKan Liang 	data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_ATOM_IDX].pebs_data_source;
16438aaf921SKan Liang 	memcpy(data_source, pebs_data_source, sizeof(pebs_data_source));
16538aaf921SKan Liang 	intel_pmu_pebs_data_source_cmt(data_source);
16638aaf921SKan Liang }
16738aaf921SKan Liang 
1687010d129SBorislav Petkov static u64 precise_store_data(u64 status)
1697010d129SBorislav Petkov {
1707010d129SBorislav Petkov 	union intel_x86_pebs_dse dse;
1717010d129SBorislav Petkov 	u64 val = P(OP, STORE) | P(SNOOP, NA) | P(LVL, L1) | P(TLB, L2);
1727010d129SBorislav Petkov 
1737010d129SBorislav Petkov 	dse.val = status;
1747010d129SBorislav Petkov 
1757010d129SBorislav Petkov 	/*
1767010d129SBorislav Petkov 	 * bit 4: TLB access
1777010d129SBorislav Petkov 	 * 1 = stored missed 2nd level TLB
1787010d129SBorislav Petkov 	 *
1797010d129SBorislav Petkov 	 * so it either hit the walker or the OS
1807010d129SBorislav Petkov 	 * otherwise hit 2nd level TLB
1817010d129SBorislav Petkov 	 */
1827010d129SBorislav Petkov 	if (dse.st_stlb_miss)
1837010d129SBorislav Petkov 		val |= P(TLB, MISS);
1847010d129SBorislav Petkov 	else
1857010d129SBorislav Petkov 		val |= P(TLB, HIT);
1867010d129SBorislav Petkov 
1877010d129SBorislav Petkov 	/*
1887010d129SBorislav Petkov 	 * bit 0: hit L1 data cache
1897010d129SBorislav Petkov 	 * if not set, then all we know is that
1907010d129SBorislav Petkov 	 * it missed L1D
1917010d129SBorislav Petkov 	 */
1927010d129SBorislav Petkov 	if (dse.st_l1d_hit)
1937010d129SBorislav Petkov 		val |= P(LVL, HIT);
1947010d129SBorislav Petkov 	else
1957010d129SBorislav Petkov 		val |= P(LVL, MISS);
1967010d129SBorislav Petkov 
1977010d129SBorislav Petkov 	/*
1987010d129SBorislav Petkov 	 * bit 5: Locked prefix
1997010d129SBorislav Petkov 	 */
2007010d129SBorislav Petkov 	if (dse.st_locked)
2017010d129SBorislav Petkov 		val |= P(LOCK, LOCKED);
2027010d129SBorislav Petkov 
2037010d129SBorislav Petkov 	return val;
2047010d129SBorislav Petkov }
2057010d129SBorislav Petkov 
2067010d129SBorislav Petkov static u64 precise_datala_hsw(struct perf_event *event, u64 status)
2077010d129SBorislav Petkov {
2087010d129SBorislav Petkov 	union perf_mem_data_src dse;
2097010d129SBorislav Petkov 
2107010d129SBorislav Petkov 	dse.val = PERF_MEM_NA;
2117010d129SBorislav Petkov 
2127010d129SBorislav Petkov 	if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW)
2137010d129SBorislav Petkov 		dse.mem_op = PERF_MEM_OP_STORE;
2147010d129SBorislav Petkov 	else if (event->hw.flags & PERF_X86_EVENT_PEBS_LD_HSW)
2157010d129SBorislav Petkov 		dse.mem_op = PERF_MEM_OP_LOAD;
2167010d129SBorislav Petkov 
2177010d129SBorislav Petkov 	/*
2187010d129SBorislav Petkov 	 * L1 info only valid for following events:
2197010d129SBorislav Petkov 	 *
2207010d129SBorislav Petkov 	 * MEM_UOPS_RETIRED.STLB_MISS_STORES
2217010d129SBorislav Petkov 	 * MEM_UOPS_RETIRED.LOCK_STORES
2227010d129SBorislav Petkov 	 * MEM_UOPS_RETIRED.SPLIT_STORES
2237010d129SBorislav Petkov 	 * MEM_UOPS_RETIRED.ALL_STORES
2247010d129SBorislav Petkov 	 */
2257010d129SBorislav Petkov 	if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) {
2267010d129SBorislav Petkov 		if (status & 1)
2277010d129SBorislav Petkov 			dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_HIT;
2287010d129SBorislav Petkov 		else
2297010d129SBorislav Petkov 			dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_MISS;
2307010d129SBorislav Petkov 	}
2317010d129SBorislav Petkov 	return dse.val;
2327010d129SBorislav Petkov }
2337010d129SBorislav Petkov 
23439a41278SKan Liang static inline void pebs_set_tlb_lock(u64 *val, bool tlb, bool lock)
23539a41278SKan Liang {
23639a41278SKan Liang 	/*
23739a41278SKan Liang 	 * TLB access
23839a41278SKan Liang 	 * 0 = did not miss 2nd level TLB
23939a41278SKan Liang 	 * 1 = missed 2nd level TLB
24039a41278SKan Liang 	 */
24139a41278SKan Liang 	if (tlb)
24239a41278SKan Liang 		*val |= P(TLB, MISS) | P(TLB, L2);
24339a41278SKan Liang 	else
24439a41278SKan Liang 		*val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2);
24539a41278SKan Liang 
24639a41278SKan Liang 	/* locked prefix */
24739a41278SKan Liang 	if (lock)
24839a41278SKan Liang 		*val |= P(LOCK, LOCKED);
24939a41278SKan Liang }
25039a41278SKan Liang 
25139a41278SKan Liang /* Retrieve the latency data for e-core of ADL */
25238aaf921SKan Liang static u64 __adl_latency_data_small(struct perf_event *event, u64 status,
25338aaf921SKan Liang 				     u8 dse, bool tlb, bool lock, bool blk)
25439a41278SKan Liang {
25539a41278SKan Liang 	u64 val;
25639a41278SKan Liang 
25739a41278SKan Liang 	WARN_ON_ONCE(hybrid_pmu(event->pmu)->cpu_type == hybrid_big);
25839a41278SKan Liang 
25938aaf921SKan Liang 	dse &= PERF_PEBS_DATA_SOURCE_MASK;
26038aaf921SKan Liang 	val = hybrid_var(event->pmu, pebs_data_source)[dse];
26139a41278SKan Liang 
26238aaf921SKan Liang 	pebs_set_tlb_lock(&val, tlb, lock);
26339a41278SKan Liang 
26438aaf921SKan Liang 	if (blk)
26539a41278SKan Liang 		val |= P(BLK, DATA);
26639a41278SKan Liang 	else
26739a41278SKan Liang 		val |= P(BLK, NA);
26839a41278SKan Liang 
26939a41278SKan Liang 	return val;
27039a41278SKan Liang }
27139a41278SKan Liang 
27238aaf921SKan Liang u64 adl_latency_data_small(struct perf_event *event, u64 status)
27338aaf921SKan Liang {
27438aaf921SKan Liang 	union intel_x86_pebs_dse dse;
27538aaf921SKan Liang 
27638aaf921SKan Liang 	dse.val = status;
27738aaf921SKan Liang 
27838aaf921SKan Liang 	return __adl_latency_data_small(event, status, dse.ld_dse,
27938aaf921SKan Liang 					dse.ld_locked, dse.ld_stlb_miss,
28038aaf921SKan Liang 					dse.ld_data_blk);
28138aaf921SKan Liang }
28238aaf921SKan Liang 
28338aaf921SKan Liang /* Retrieve the latency data for e-core of MTL */
28438aaf921SKan Liang u64 mtl_latency_data_small(struct perf_event *event, u64 status)
28538aaf921SKan Liang {
28638aaf921SKan Liang 	union intel_x86_pebs_dse dse;
28738aaf921SKan Liang 
28838aaf921SKan Liang 	dse.val = status;
28938aaf921SKan Liang 
29038aaf921SKan Liang 	return __adl_latency_data_small(event, status, dse.mtl_dse,
29138aaf921SKan Liang 					dse.mtl_stlb_miss, dse.mtl_locked,
29238aaf921SKan Liang 					dse.mtl_fwd_blk);
29338aaf921SKan Liang }
29438aaf921SKan Liang 
295ccf170e9SKan Liang static u64 load_latency_data(struct perf_event *event, u64 status)
2967010d129SBorislav Petkov {
2977010d129SBorislav Petkov 	union intel_x86_pebs_dse dse;
2987010d129SBorislav Petkov 	u64 val;
2997010d129SBorislav Petkov 
3007010d129SBorislav Petkov 	dse.val = status;
3017010d129SBorislav Petkov 
3027010d129SBorislav Petkov 	/*
3037010d129SBorislav Petkov 	 * use the mapping table for bit 0-3
3047010d129SBorislav Petkov 	 */
305ccf170e9SKan Liang 	val = hybrid_var(event->pmu, pebs_data_source)[dse.ld_dse];
3067010d129SBorislav Petkov 
3077010d129SBorislav Petkov 	/*
3087010d129SBorislav Petkov 	 * Nehalem models do not support TLB, Lock infos
3097010d129SBorislav Petkov 	 */
31095298355SAndi Kleen 	if (x86_pmu.pebs_no_tlb) {
3117010d129SBorislav Petkov 		val |= P(TLB, NA) | P(LOCK, NA);
3127010d129SBorislav Petkov 		return val;
3137010d129SBorislav Petkov 	}
3147010d129SBorislav Petkov 
31539a41278SKan Liang 	pebs_set_tlb_lock(&val, dse.ld_stlb_miss, dse.ld_locked);
3167010d129SBorislav Petkov 
31761b985e3SKan Liang 	/*
31861b985e3SKan Liang 	 * Ice Lake and earlier models do not support block infos.
31961b985e3SKan Liang 	 */
32061b985e3SKan Liang 	if (!x86_pmu.pebs_block) {
32161b985e3SKan Liang 		val |= P(BLK, NA);
32261b985e3SKan Liang 		return val;
32361b985e3SKan Liang 	}
32461b985e3SKan Liang 	/*
32561b985e3SKan Liang 	 * bit 6: load was blocked since its data could not be forwarded
32661b985e3SKan Liang 	 *        from a preceding store
32761b985e3SKan Liang 	 */
32861b985e3SKan Liang 	if (dse.ld_data_blk)
32961b985e3SKan Liang 		val |= P(BLK, DATA);
33061b985e3SKan Liang 
33161b985e3SKan Liang 	/*
33261b985e3SKan Liang 	 * bit 7: load was blocked due to potential address conflict with
33361b985e3SKan Liang 	 *        a preceding store
33461b985e3SKan Liang 	 */
33561b985e3SKan Liang 	if (dse.ld_addr_blk)
33661b985e3SKan Liang 		val |= P(BLK, ADDR);
33761b985e3SKan Liang 
33861b985e3SKan Liang 	if (!dse.ld_data_blk && !dse.ld_addr_blk)
33961b985e3SKan Liang 		val |= P(BLK, NA);
34061b985e3SKan Liang 
34161b985e3SKan Liang 	return val;
34261b985e3SKan Liang }
34361b985e3SKan Liang 
344ccf170e9SKan Liang static u64 store_latency_data(struct perf_event *event, u64 status)
34561b985e3SKan Liang {
34661b985e3SKan Liang 	union intel_x86_pebs_dse dse;
347d4bdb0beSStephane Eranian 	union perf_mem_data_src src;
34861b985e3SKan Liang 	u64 val;
34961b985e3SKan Liang 
35061b985e3SKan Liang 	dse.val = status;
35161b985e3SKan Liang 
35261b985e3SKan Liang 	/*
35361b985e3SKan Liang 	 * use the mapping table for bit 0-3
35461b985e3SKan Liang 	 */
355ccf170e9SKan Liang 	val = hybrid_var(event->pmu, pebs_data_source)[dse.st_lat_dse];
35661b985e3SKan Liang 
35739a41278SKan Liang 	pebs_set_tlb_lock(&val, dse.st_lat_stlb_miss, dse.st_lat_locked);
35861b985e3SKan Liang 
35961b985e3SKan Liang 	val |= P(BLK, NA);
36061b985e3SKan Liang 
361d4bdb0beSStephane Eranian 	/*
362d4bdb0beSStephane Eranian 	 * the pebs_data_source table is only for loads
363d4bdb0beSStephane Eranian 	 * so override the mem_op to say STORE instead
364d4bdb0beSStephane Eranian 	 */
365d4bdb0beSStephane Eranian 	src.val = val;
366d4bdb0beSStephane Eranian 	src.mem_op = P(OP,STORE);
367d4bdb0beSStephane Eranian 
368d4bdb0beSStephane Eranian 	return src.val;
3697010d129SBorislav Petkov }
3707010d129SBorislav Petkov 
3717010d129SBorislav Petkov struct pebs_record_core {
3727010d129SBorislav Petkov 	u64 flags, ip;
3737010d129SBorislav Petkov 	u64 ax, bx, cx, dx;
3747010d129SBorislav Petkov 	u64 si, di, bp, sp;
3757010d129SBorislav Petkov 	u64 r8,  r9,  r10, r11;
3767010d129SBorislav Petkov 	u64 r12, r13, r14, r15;
3777010d129SBorislav Petkov };
3787010d129SBorislav Petkov 
3797010d129SBorislav Petkov struct pebs_record_nhm {
3807010d129SBorislav Petkov 	u64 flags, ip;
3817010d129SBorislav Petkov 	u64 ax, bx, cx, dx;
3827010d129SBorislav Petkov 	u64 si, di, bp, sp;
3837010d129SBorislav Petkov 	u64 r8,  r9,  r10, r11;
3847010d129SBorislav Petkov 	u64 r12, r13, r14, r15;
3857010d129SBorislav Petkov 	u64 status, dla, dse, lat;
3867010d129SBorislav Petkov };
3877010d129SBorislav Petkov 
3887010d129SBorislav Petkov /*
3897010d129SBorislav Petkov  * Same as pebs_record_nhm, with two additional fields.
3907010d129SBorislav Petkov  */
3917010d129SBorislav Petkov struct pebs_record_hsw {
3927010d129SBorislav Petkov 	u64 flags, ip;
3937010d129SBorislav Petkov 	u64 ax, bx, cx, dx;
3947010d129SBorislav Petkov 	u64 si, di, bp, sp;
3957010d129SBorislav Petkov 	u64 r8,  r9,  r10, r11;
3967010d129SBorislav Petkov 	u64 r12, r13, r14, r15;
3977010d129SBorislav Petkov 	u64 status, dla, dse, lat;
3987010d129SBorislav Petkov 	u64 real_ip, tsx_tuning;
3997010d129SBorislav Petkov };
4007010d129SBorislav Petkov 
4017010d129SBorislav Petkov union hsw_tsx_tuning {
4027010d129SBorislav Petkov 	struct {
4037010d129SBorislav Petkov 		u32 cycles_last_block     : 32,
4047010d129SBorislav Petkov 		    hle_abort		  : 1,
4057010d129SBorislav Petkov 		    rtm_abort		  : 1,
4067010d129SBorislav Petkov 		    instruction_abort     : 1,
4077010d129SBorislav Petkov 		    non_instruction_abort : 1,
4087010d129SBorislav Petkov 		    retry		  : 1,
4097010d129SBorislav Petkov 		    data_conflict	  : 1,
4107010d129SBorislav Petkov 		    capacity_writes	  : 1,
4117010d129SBorislav Petkov 		    capacity_reads	  : 1;
4127010d129SBorislav Petkov 	};
4137010d129SBorislav Petkov 	u64	    value;
4147010d129SBorislav Petkov };
4157010d129SBorislav Petkov 
4167010d129SBorislav Petkov #define PEBS_HSW_TSX_FLAGS	0xff00000000ULL
4177010d129SBorislav Petkov 
4187010d129SBorislav Petkov /* Same as HSW, plus TSC */
4197010d129SBorislav Petkov 
4207010d129SBorislav Petkov struct pebs_record_skl {
4217010d129SBorislav Petkov 	u64 flags, ip;
4227010d129SBorislav Petkov 	u64 ax, bx, cx, dx;
4237010d129SBorislav Petkov 	u64 si, di, bp, sp;
4247010d129SBorislav Petkov 	u64 r8,  r9,  r10, r11;
4257010d129SBorislav Petkov 	u64 r12, r13, r14, r15;
4267010d129SBorislav Petkov 	u64 status, dla, dse, lat;
4277010d129SBorislav Petkov 	u64 real_ip, tsx_tuning;
4287010d129SBorislav Petkov 	u64 tsc;
4297010d129SBorislav Petkov };
4307010d129SBorislav Petkov 
4317010d129SBorislav Petkov void init_debug_store_on_cpu(int cpu)
4327010d129SBorislav Petkov {
4337010d129SBorislav Petkov 	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
4347010d129SBorislav Petkov 
4357010d129SBorislav Petkov 	if (!ds)
4367010d129SBorislav Petkov 		return;
4377010d129SBorislav Petkov 
4387010d129SBorislav Petkov 	wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA,
4397010d129SBorislav Petkov 		     (u32)((u64)(unsigned long)ds),
4407010d129SBorislav Petkov 		     (u32)((u64)(unsigned long)ds >> 32));
4417010d129SBorislav Petkov }
4427010d129SBorislav Petkov 
4437010d129SBorislav Petkov void fini_debug_store_on_cpu(int cpu)
4447010d129SBorislav Petkov {
4457010d129SBorislav Petkov 	if (!per_cpu(cpu_hw_events, cpu).ds)
4467010d129SBorislav Petkov 		return;
4477010d129SBorislav Petkov 
4487010d129SBorislav Petkov 	wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0);
4497010d129SBorislav Petkov }
4507010d129SBorislav Petkov 
4517010d129SBorislav Petkov static DEFINE_PER_CPU(void *, insn_buffer);
4527010d129SBorislav Petkov 
453c1961a46SHugh Dickins static void ds_update_cea(void *cea, void *addr, size_t size, pgprot_t prot)
454c1961a46SHugh Dickins {
45542f3bdc5SPeter Zijlstra 	unsigned long start = (unsigned long)cea;
456c1961a46SHugh Dickins 	phys_addr_t pa;
457c1961a46SHugh Dickins 	size_t msz = 0;
458c1961a46SHugh Dickins 
459c1961a46SHugh Dickins 	pa = virt_to_phys(addr);
46042f3bdc5SPeter Zijlstra 
46142f3bdc5SPeter Zijlstra 	preempt_disable();
462c1961a46SHugh Dickins 	for (; msz < size; msz += PAGE_SIZE, pa += PAGE_SIZE, cea += PAGE_SIZE)
463c1961a46SHugh Dickins 		cea_set_pte(cea, pa, prot);
46442f3bdc5SPeter Zijlstra 
46542f3bdc5SPeter Zijlstra 	/*
46642f3bdc5SPeter Zijlstra 	 * This is a cross-CPU update of the cpu_entry_area, we must shoot down
46742f3bdc5SPeter Zijlstra 	 * all TLB entries for it.
46842f3bdc5SPeter Zijlstra 	 */
46942f3bdc5SPeter Zijlstra 	flush_tlb_kernel_range(start, start + size);
47042f3bdc5SPeter Zijlstra 	preempt_enable();
471c1961a46SHugh Dickins }
472c1961a46SHugh Dickins 
473c1961a46SHugh Dickins static void ds_clear_cea(void *cea, size_t size)
474c1961a46SHugh Dickins {
47542f3bdc5SPeter Zijlstra 	unsigned long start = (unsigned long)cea;
476c1961a46SHugh Dickins 	size_t msz = 0;
477c1961a46SHugh Dickins 
47842f3bdc5SPeter Zijlstra 	preempt_disable();
479c1961a46SHugh Dickins 	for (; msz < size; msz += PAGE_SIZE, cea += PAGE_SIZE)
480c1961a46SHugh Dickins 		cea_set_pte(cea, 0, PAGE_NONE);
48142f3bdc5SPeter Zijlstra 
48242f3bdc5SPeter Zijlstra 	flush_tlb_kernel_range(start, start + size);
48342f3bdc5SPeter Zijlstra 	preempt_enable();
484c1961a46SHugh Dickins }
485c1961a46SHugh Dickins 
486c1961a46SHugh Dickins static void *dsalloc_pages(size_t size, gfp_t flags, int cpu)
487c1961a46SHugh Dickins {
488c1961a46SHugh Dickins 	unsigned int order = get_order(size);
489c1961a46SHugh Dickins 	int node = cpu_to_node(cpu);
490c1961a46SHugh Dickins 	struct page *page;
491c1961a46SHugh Dickins 
492c1961a46SHugh Dickins 	page = __alloc_pages_node(node, flags | __GFP_ZERO, order);
493c1961a46SHugh Dickins 	return page ? page_address(page) : NULL;
494c1961a46SHugh Dickins }
495c1961a46SHugh Dickins 
496c1961a46SHugh Dickins static void dsfree_pages(const void *buffer, size_t size)
497c1961a46SHugh Dickins {
498c1961a46SHugh Dickins 	if (buffer)
499c1961a46SHugh Dickins 		free_pages((unsigned long)buffer, get_order(size));
500c1961a46SHugh Dickins }
501c1961a46SHugh Dickins 
5027010d129SBorislav Petkov static int alloc_pebs_buffer(int cpu)
5037010d129SBorislav Petkov {
504c1961a46SHugh Dickins 	struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
505c1961a46SHugh Dickins 	struct debug_store *ds = hwev->ds;
506c1961a46SHugh Dickins 	size_t bsiz = x86_pmu.pebs_buffer_size;
507c1961a46SHugh Dickins 	int max, node = cpu_to_node(cpu);
5081fc654cfSIngo Molnar 	void *buffer, *insn_buff, *cea;
5097010d129SBorislav Petkov 
5107010d129SBorislav Petkov 	if (!x86_pmu.pebs)
5117010d129SBorislav Petkov 		return 0;
5127010d129SBorislav Petkov 
513c1961a46SHugh Dickins 	buffer = dsalloc_pages(bsiz, GFP_KERNEL, cpu);
5147010d129SBorislav Petkov 	if (unlikely(!buffer))
5157010d129SBorislav Petkov 		return -ENOMEM;
5167010d129SBorislav Petkov 
5177010d129SBorislav Petkov 	/*
5187010d129SBorislav Petkov 	 * HSW+ already provides us the eventing ip; no need to allocate this
5197010d129SBorislav Petkov 	 * buffer then.
5207010d129SBorislav Petkov 	 */
5217010d129SBorislav Petkov 	if (x86_pmu.intel_cap.pebs_format < 2) {
5221fc654cfSIngo Molnar 		insn_buff = kzalloc_node(PEBS_FIXUP_SIZE, GFP_KERNEL, node);
5231fc654cfSIngo Molnar 		if (!insn_buff) {
524c1961a46SHugh Dickins 			dsfree_pages(buffer, bsiz);
5257010d129SBorislav Petkov 			return -ENOMEM;
5267010d129SBorislav Petkov 		}
5271fc654cfSIngo Molnar 		per_cpu(insn_buffer, cpu) = insn_buff;
5287010d129SBorislav Petkov 	}
529c1961a46SHugh Dickins 	hwev->ds_pebs_vaddr = buffer;
530c1961a46SHugh Dickins 	/* Update the cpu entry area mapping */
531c1961a46SHugh Dickins 	cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer;
532c1961a46SHugh Dickins 	ds->pebs_buffer_base = (unsigned long) cea;
533c1961a46SHugh Dickins 	ds_update_cea(cea, buffer, bsiz, PAGE_KERNEL);
5347010d129SBorislav Petkov 	ds->pebs_index = ds->pebs_buffer_base;
535c1961a46SHugh Dickins 	max = x86_pmu.pebs_record_size * (bsiz / x86_pmu.pebs_record_size);
536c1961a46SHugh Dickins 	ds->pebs_absolute_maximum = ds->pebs_buffer_base + max;
5377010d129SBorislav Petkov 	return 0;
5387010d129SBorislav Petkov }
5397010d129SBorislav Petkov 
5407010d129SBorislav Petkov static void release_pebs_buffer(int cpu)
5417010d129SBorislav Petkov {
542c1961a46SHugh Dickins 	struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
543c1961a46SHugh Dickins 	void *cea;
5447010d129SBorislav Petkov 
545efe951d3SPeter Zijlstra 	if (!x86_pmu.pebs)
5467010d129SBorislav Petkov 		return;
5477010d129SBorislav Petkov 
5487010d129SBorislav Petkov 	kfree(per_cpu(insn_buffer, cpu));
5497010d129SBorislav Petkov 	per_cpu(insn_buffer, cpu) = NULL;
5507010d129SBorislav Petkov 
551c1961a46SHugh Dickins 	/* Clear the fixmap */
552c1961a46SHugh Dickins 	cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer;
553c1961a46SHugh Dickins 	ds_clear_cea(cea, x86_pmu.pebs_buffer_size);
554c1961a46SHugh Dickins 	dsfree_pages(hwev->ds_pebs_vaddr, x86_pmu.pebs_buffer_size);
555c1961a46SHugh Dickins 	hwev->ds_pebs_vaddr = NULL;
5567010d129SBorislav Petkov }
5577010d129SBorislav Petkov 
5587010d129SBorislav Petkov static int alloc_bts_buffer(int cpu)
5597010d129SBorislav Petkov {
560c1961a46SHugh Dickins 	struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
561c1961a46SHugh Dickins 	struct debug_store *ds = hwev->ds;
562c1961a46SHugh Dickins 	void *buffer, *cea;
563c1961a46SHugh Dickins 	int max;
5647010d129SBorislav Petkov 
5657010d129SBorislav Petkov 	if (!x86_pmu.bts)
5667010d129SBorislav Petkov 		return 0;
5677010d129SBorislav Petkov 
568c1961a46SHugh Dickins 	buffer = dsalloc_pages(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, cpu);
5697010d129SBorislav Petkov 	if (unlikely(!buffer)) {
5707010d129SBorislav Petkov 		WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__);
5717010d129SBorislav Petkov 		return -ENOMEM;
5727010d129SBorislav Petkov 	}
573c1961a46SHugh Dickins 	hwev->ds_bts_vaddr = buffer;
574c1961a46SHugh Dickins 	/* Update the fixmap */
575c1961a46SHugh Dickins 	cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer;
576c1961a46SHugh Dickins 	ds->bts_buffer_base = (unsigned long) cea;
577c1961a46SHugh Dickins 	ds_update_cea(cea, buffer, BTS_BUFFER_SIZE, PAGE_KERNEL);
5787010d129SBorislav Petkov 	ds->bts_index = ds->bts_buffer_base;
5792c991e40SHugh Dickins 	max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE;
5802c991e40SHugh Dickins 	ds->bts_absolute_maximum = ds->bts_buffer_base +
5812c991e40SHugh Dickins 					max * BTS_RECORD_SIZE;
5822c991e40SHugh Dickins 	ds->bts_interrupt_threshold = ds->bts_absolute_maximum -
5832c991e40SHugh Dickins 					(max / 16) * BTS_RECORD_SIZE;
5847010d129SBorislav Petkov 	return 0;
5857010d129SBorislav Petkov }
5867010d129SBorislav Petkov 
5877010d129SBorislav Petkov static void release_bts_buffer(int cpu)
5887010d129SBorislav Petkov {
589c1961a46SHugh Dickins 	struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
590c1961a46SHugh Dickins 	void *cea;
5917010d129SBorislav Petkov 
592efe951d3SPeter Zijlstra 	if (!x86_pmu.bts)
5937010d129SBorislav Petkov 		return;
5947010d129SBorislav Petkov 
595c1961a46SHugh Dickins 	/* Clear the fixmap */
596c1961a46SHugh Dickins 	cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer;
597c1961a46SHugh Dickins 	ds_clear_cea(cea, BTS_BUFFER_SIZE);
598c1961a46SHugh Dickins 	dsfree_pages(hwev->ds_bts_vaddr, BTS_BUFFER_SIZE);
599c1961a46SHugh Dickins 	hwev->ds_bts_vaddr = NULL;
6007010d129SBorislav Petkov }
6017010d129SBorislav Petkov 
6027010d129SBorislav Petkov static int alloc_ds_buffer(int cpu)
6037010d129SBorislav Petkov {
604c1961a46SHugh Dickins 	struct debug_store *ds = &get_cpu_entry_area(cpu)->cpu_debug_store;
6057010d129SBorislav Petkov 
606c1961a46SHugh Dickins 	memset(ds, 0, sizeof(*ds));
6077010d129SBorislav Petkov 	per_cpu(cpu_hw_events, cpu).ds = ds;
6087010d129SBorislav Petkov 	return 0;
6097010d129SBorislav Petkov }
6107010d129SBorislav Petkov 
6117010d129SBorislav Petkov static void release_ds_buffer(int cpu)
6127010d129SBorislav Petkov {
6137010d129SBorislav Petkov 	per_cpu(cpu_hw_events, cpu).ds = NULL;
6147010d129SBorislav Petkov }
6157010d129SBorislav Petkov 
6167010d129SBorislav Petkov void release_ds_buffers(void)
6177010d129SBorislav Petkov {
6187010d129SBorislav Petkov 	int cpu;
6197010d129SBorislav Petkov 
6207010d129SBorislav Petkov 	if (!x86_pmu.bts && !x86_pmu.pebs)
6217010d129SBorislav Petkov 		return;
6227010d129SBorislav Petkov 
623efe951d3SPeter Zijlstra 	for_each_possible_cpu(cpu)
624efe951d3SPeter Zijlstra 		release_ds_buffer(cpu);
625efe951d3SPeter Zijlstra 
626efe951d3SPeter Zijlstra 	for_each_possible_cpu(cpu) {
627efe951d3SPeter Zijlstra 		/*
628efe951d3SPeter Zijlstra 		 * Again, ignore errors from offline CPUs, they will no longer
629efe951d3SPeter Zijlstra 		 * observe cpu_hw_events.ds and not program the DS_AREA when
630efe951d3SPeter Zijlstra 		 * they come up.
631efe951d3SPeter Zijlstra 		 */
6327010d129SBorislav Petkov 		fini_debug_store_on_cpu(cpu);
633efe951d3SPeter Zijlstra 	}
6347010d129SBorislav Petkov 
6357010d129SBorislav Petkov 	for_each_possible_cpu(cpu) {
6367010d129SBorislav Petkov 		release_pebs_buffer(cpu);
6377010d129SBorislav Petkov 		release_bts_buffer(cpu);
6387010d129SBorislav Petkov 	}
6397010d129SBorislav Petkov }
6407010d129SBorislav Petkov 
6417010d129SBorislav Petkov void reserve_ds_buffers(void)
6427010d129SBorislav Petkov {
6437010d129SBorislav Petkov 	int bts_err = 0, pebs_err = 0;
6447010d129SBorislav Petkov 	int cpu;
6457010d129SBorislav Petkov 
6467010d129SBorislav Petkov 	x86_pmu.bts_active = 0;
6477010d129SBorislav Petkov 	x86_pmu.pebs_active = 0;
6487010d129SBorislav Petkov 
6497010d129SBorislav Petkov 	if (!x86_pmu.bts && !x86_pmu.pebs)
6507010d129SBorislav Petkov 		return;
6517010d129SBorislav Petkov 
6527010d129SBorislav Petkov 	if (!x86_pmu.bts)
6537010d129SBorislav Petkov 		bts_err = 1;
6547010d129SBorislav Petkov 
6557010d129SBorislav Petkov 	if (!x86_pmu.pebs)
6567010d129SBorislav Petkov 		pebs_err = 1;
6577010d129SBorislav Petkov 
6587010d129SBorislav Petkov 	for_each_possible_cpu(cpu) {
6597010d129SBorislav Petkov 		if (alloc_ds_buffer(cpu)) {
6607010d129SBorislav Petkov 			bts_err = 1;
6617010d129SBorislav Petkov 			pebs_err = 1;
6627010d129SBorislav Petkov 		}
6637010d129SBorislav Petkov 
6647010d129SBorislav Petkov 		if (!bts_err && alloc_bts_buffer(cpu))
6657010d129SBorislav Petkov 			bts_err = 1;
6667010d129SBorislav Petkov 
6677010d129SBorislav Petkov 		if (!pebs_err && alloc_pebs_buffer(cpu))
6687010d129SBorislav Petkov 			pebs_err = 1;
6697010d129SBorislav Petkov 
6707010d129SBorislav Petkov 		if (bts_err && pebs_err)
6717010d129SBorislav Petkov 			break;
6727010d129SBorislav Petkov 	}
6737010d129SBorislav Petkov 
6747010d129SBorislav Petkov 	if (bts_err) {
6757010d129SBorislav Petkov 		for_each_possible_cpu(cpu)
6767010d129SBorislav Petkov 			release_bts_buffer(cpu);
6777010d129SBorislav Petkov 	}
6787010d129SBorislav Petkov 
6797010d129SBorislav Petkov 	if (pebs_err) {
6807010d129SBorislav Petkov 		for_each_possible_cpu(cpu)
6817010d129SBorislav Petkov 			release_pebs_buffer(cpu);
6827010d129SBorislav Petkov 	}
6837010d129SBorislav Petkov 
6847010d129SBorislav Petkov 	if (bts_err && pebs_err) {
6857010d129SBorislav Petkov 		for_each_possible_cpu(cpu)
6867010d129SBorislav Petkov 			release_ds_buffer(cpu);
6877010d129SBorislav Petkov 	} else {
6887010d129SBorislav Petkov 		if (x86_pmu.bts && !bts_err)
6897010d129SBorislav Petkov 			x86_pmu.bts_active = 1;
6907010d129SBorislav Petkov 
6917010d129SBorislav Petkov 		if (x86_pmu.pebs && !pebs_err)
6927010d129SBorislav Petkov 			x86_pmu.pebs_active = 1;
6937010d129SBorislav Petkov 
694efe951d3SPeter Zijlstra 		for_each_possible_cpu(cpu) {
695efe951d3SPeter Zijlstra 			/*
696efe951d3SPeter Zijlstra 			 * Ignores wrmsr_on_cpu() errors for offline CPUs they
697efe951d3SPeter Zijlstra 			 * will get this call through intel_pmu_cpu_starting().
698efe951d3SPeter Zijlstra 			 */
6997010d129SBorislav Petkov 			init_debug_store_on_cpu(cpu);
7007010d129SBorislav Petkov 		}
701efe951d3SPeter Zijlstra 	}
7027010d129SBorislav Petkov }
7037010d129SBorislav Petkov 
7047010d129SBorislav Petkov /*
7057010d129SBorislav Petkov  * BTS
7067010d129SBorislav Petkov  */
7077010d129SBorislav Petkov 
7087010d129SBorislav Petkov struct event_constraint bts_constraint =
7097010d129SBorislav Petkov 	EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0);
7107010d129SBorislav Petkov 
7117010d129SBorislav Petkov void intel_pmu_enable_bts(u64 config)
7127010d129SBorislav Petkov {
7137010d129SBorislav Petkov 	unsigned long debugctlmsr;
7147010d129SBorislav Petkov 
7157010d129SBorislav Petkov 	debugctlmsr = get_debugctlmsr();
7167010d129SBorislav Petkov 
7177010d129SBorislav Petkov 	debugctlmsr |= DEBUGCTLMSR_TR;
7187010d129SBorislav Petkov 	debugctlmsr |= DEBUGCTLMSR_BTS;
7197010d129SBorislav Petkov 	if (config & ARCH_PERFMON_EVENTSEL_INT)
7207010d129SBorislav Petkov 		debugctlmsr |= DEBUGCTLMSR_BTINT;
7217010d129SBorislav Petkov 
7227010d129SBorislav Petkov 	if (!(config & ARCH_PERFMON_EVENTSEL_OS))
7237010d129SBorislav Petkov 		debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
7247010d129SBorislav Petkov 
7257010d129SBorislav Petkov 	if (!(config & ARCH_PERFMON_EVENTSEL_USR))
7267010d129SBorislav Petkov 		debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR;
7277010d129SBorislav Petkov 
7287010d129SBorislav Petkov 	update_debugctlmsr(debugctlmsr);
7297010d129SBorislav Petkov }
7307010d129SBorislav Petkov 
7317010d129SBorislav Petkov void intel_pmu_disable_bts(void)
7327010d129SBorislav Petkov {
7337010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
7347010d129SBorislav Petkov 	unsigned long debugctlmsr;
7357010d129SBorislav Petkov 
7367010d129SBorislav Petkov 	if (!cpuc->ds)
7377010d129SBorislav Petkov 		return;
7387010d129SBorislav Petkov 
7397010d129SBorislav Petkov 	debugctlmsr = get_debugctlmsr();
7407010d129SBorislav Petkov 
7417010d129SBorislav Petkov 	debugctlmsr &=
7427010d129SBorislav Petkov 		~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT |
7437010d129SBorislav Petkov 		  DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR);
7447010d129SBorislav Petkov 
7457010d129SBorislav Petkov 	update_debugctlmsr(debugctlmsr);
7467010d129SBorislav Petkov }
7477010d129SBorislav Petkov 
7487010d129SBorislav Petkov int intel_pmu_drain_bts_buffer(void)
7497010d129SBorislav Petkov {
7507010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
7517010d129SBorislav Petkov 	struct debug_store *ds = cpuc->ds;
7527010d129SBorislav Petkov 	struct bts_record {
7537010d129SBorislav Petkov 		u64	from;
7547010d129SBorislav Petkov 		u64	to;
7557010d129SBorislav Petkov 		u64	flags;
7567010d129SBorislav Petkov 	};
7577010d129SBorislav Petkov 	struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
7587010d129SBorislav Petkov 	struct bts_record *at, *base, *top;
7597010d129SBorislav Petkov 	struct perf_output_handle handle;
7607010d129SBorislav Petkov 	struct perf_event_header header;
7617010d129SBorislav Petkov 	struct perf_sample_data data;
7627010d129SBorislav Petkov 	unsigned long skip = 0;
7637010d129SBorislav Petkov 	struct pt_regs regs;
7647010d129SBorislav Petkov 
7657010d129SBorislav Petkov 	if (!event)
7667010d129SBorislav Petkov 		return 0;
7677010d129SBorislav Petkov 
7687010d129SBorislav Petkov 	if (!x86_pmu.bts_active)
7697010d129SBorislav Petkov 		return 0;
7707010d129SBorislav Petkov 
7717010d129SBorislav Petkov 	base = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
7727010d129SBorislav Petkov 	top  = (struct bts_record *)(unsigned long)ds->bts_index;
7737010d129SBorislav Petkov 
7747010d129SBorislav Petkov 	if (top <= base)
7757010d129SBorislav Petkov 		return 0;
7767010d129SBorislav Petkov 
7777010d129SBorislav Petkov 	memset(&regs, 0, sizeof(regs));
7787010d129SBorislav Petkov 
7797010d129SBorislav Petkov 	ds->bts_index = ds->bts_buffer_base;
7807010d129SBorislav Petkov 
7817010d129SBorislav Petkov 	perf_sample_data_init(&data, 0, event->hw.last_period);
7827010d129SBorislav Petkov 
7837010d129SBorislav Petkov 	/*
7847010d129SBorislav Petkov 	 * BTS leaks kernel addresses in branches across the cpl boundary,
7857010d129SBorislav Petkov 	 * such as traps or system calls, so unless the user is asking for
7867010d129SBorislav Petkov 	 * kernel tracing (and right now it's not possible), we'd need to
7877010d129SBorislav Petkov 	 * filter them out. But first we need to count how many of those we
7887010d129SBorislav Petkov 	 * have in the current batch. This is an extra O(n) pass, however,
7897010d129SBorislav Petkov 	 * it's much faster than the other one especially considering that
7907010d129SBorislav Petkov 	 * n <= 2560 (BTS_BUFFER_SIZE / BTS_RECORD_SIZE * 15/16; see the
7917010d129SBorislav Petkov 	 * alloc_bts_buffer()).
7927010d129SBorislav Petkov 	 */
7937010d129SBorislav Petkov 	for (at = base; at < top; at++) {
7947010d129SBorislav Petkov 		/*
7957010d129SBorislav Petkov 		 * Note that right now *this* BTS code only works if
7967010d129SBorislav Petkov 		 * attr::exclude_kernel is set, but let's keep this extra
7977010d129SBorislav Petkov 		 * check here in case that changes.
7987010d129SBorislav Petkov 		 */
7997010d129SBorislav Petkov 		if (event->attr.exclude_kernel &&
8007010d129SBorislav Petkov 		    (kernel_ip(at->from) || kernel_ip(at->to)))
8017010d129SBorislav Petkov 			skip++;
8027010d129SBorislav Petkov 	}
8037010d129SBorislav Petkov 
8047010d129SBorislav Petkov 	/*
8057010d129SBorislav Petkov 	 * Prepare a generic sample, i.e. fill in the invariant fields.
8067010d129SBorislav Petkov 	 * We will overwrite the from and to address before we output
8077010d129SBorislav Petkov 	 * the sample.
8087010d129SBorislav Petkov 	 */
809e8d8a90fSPeter Zijlstra 	rcu_read_lock();
8107010d129SBorislav Petkov 	perf_prepare_sample(&header, &data, event, &regs);
8117010d129SBorislav Petkov 
812267fb273SPeter Zijlstra 	if (perf_output_begin(&handle, &data, event,
813267fb273SPeter Zijlstra 			      header.size * (top - base - skip)))
814e8d8a90fSPeter Zijlstra 		goto unlock;
8157010d129SBorislav Petkov 
8167010d129SBorislav Petkov 	for (at = base; at < top; at++) {
8177010d129SBorislav Petkov 		/* Filter out any records that contain kernel addresses. */
8187010d129SBorislav Petkov 		if (event->attr.exclude_kernel &&
8197010d129SBorislav Petkov 		    (kernel_ip(at->from) || kernel_ip(at->to)))
8207010d129SBorislav Petkov 			continue;
8217010d129SBorislav Petkov 
8227010d129SBorislav Petkov 		data.ip		= at->from;
8237010d129SBorislav Petkov 		data.addr	= at->to;
8247010d129SBorislav Petkov 
8257010d129SBorislav Petkov 		perf_output_sample(&handle, &header, &data, event);
8267010d129SBorislav Petkov 	}
8277010d129SBorislav Petkov 
8287010d129SBorislav Petkov 	perf_output_end(&handle);
8297010d129SBorislav Petkov 
8307010d129SBorislav Petkov 	/* There's new data available. */
8317010d129SBorislav Petkov 	event->hw.interrupts++;
8327010d129SBorislav Petkov 	event->pending_kill = POLL_IN;
833e8d8a90fSPeter Zijlstra unlock:
834e8d8a90fSPeter Zijlstra 	rcu_read_unlock();
8357010d129SBorislav Petkov 	return 1;
8367010d129SBorislav Petkov }
8377010d129SBorislav Petkov 
8387010d129SBorislav Petkov static inline void intel_pmu_drain_pebs_buffer(void)
8397010d129SBorislav Petkov {
8409dfa9a5cSPeter Zijlstra 	struct perf_sample_data data;
8419dfa9a5cSPeter Zijlstra 
8429dfa9a5cSPeter Zijlstra 	x86_pmu.drain_pebs(NULL, &data);
8437010d129SBorislav Petkov }
8447010d129SBorislav Petkov 
8457010d129SBorislav Petkov /*
8467010d129SBorislav Petkov  * PEBS
8477010d129SBorislav Petkov  */
8487010d129SBorislav Petkov struct event_constraint intel_core2_pebs_event_constraints[] = {
8497010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
8507010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
8517010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
8527010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
8537010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1),    /* MEM_LOAD_RETIRED.* */
8547010d129SBorislav Petkov 	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
85523e3983aSStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x01),
8567010d129SBorislav Petkov 	EVENT_CONSTRAINT_END
8577010d129SBorislav Petkov };
8587010d129SBorislav Petkov 
8597010d129SBorislav Petkov struct event_constraint intel_atom_pebs_event_constraints[] = {
8607010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
8617010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
8627010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1),    /* MEM_LOAD_RETIRED.* */
8637010d129SBorislav Petkov 	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
86423e3983aSStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x01),
8657010d129SBorislav Petkov 	/* Allow all events as PEBS with no flags */
8667010d129SBorislav Petkov 	INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
8677010d129SBorislav Petkov 	EVENT_CONSTRAINT_END
8687010d129SBorislav Petkov };
8697010d129SBorislav Petkov 
8707010d129SBorislav Petkov struct event_constraint intel_slm_pebs_event_constraints[] = {
8717010d129SBorislav Petkov 	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
87223e3983aSStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x1),
8737010d129SBorislav Petkov 	/* Allow all events as PEBS with no flags */
8747010d129SBorislav Petkov 	INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
8757010d129SBorislav Petkov 	EVENT_CONSTRAINT_END
8767010d129SBorislav Petkov };
8777010d129SBorislav Petkov 
8788b92c3a7SKan Liang struct event_constraint intel_glm_pebs_event_constraints[] = {
8798b92c3a7SKan Liang 	/* Allow all events as PEBS with no flags */
8808b92c3a7SKan Liang 	INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
8818b92c3a7SKan Liang 	EVENT_CONSTRAINT_END
8828b92c3a7SKan Liang };
8838b92c3a7SKan Liang 
884f83d2f91SKan Liang struct event_constraint intel_grt_pebs_event_constraints[] = {
885f83d2f91SKan Liang 	/* Allow all events as PEBS with no flags */
886cde643ffSKan Liang 	INTEL_HYBRID_LAT_CONSTRAINT(0x5d0, 0x3),
88739a41278SKan Liang 	INTEL_HYBRID_LAT_CONSTRAINT(0x6d0, 0xf),
888f83d2f91SKan Liang 	EVENT_CONSTRAINT_END
889f83d2f91SKan Liang };
890f83d2f91SKan Liang 
8917010d129SBorislav Petkov struct event_constraint intel_nehalem_pebs_event_constraints[] = {
8927010d129SBorislav Petkov 	INTEL_PLD_CONSTRAINT(0x100b, 0xf),      /* MEM_INST_RETIRED.* */
8937010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf),    /* MEM_UNCORE_RETIRED.* */
8947010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
8957010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf),    /* INST_RETIRED.ANY */
8967010d129SBorislav Petkov 	INTEL_EVENT_CONSTRAINT(0xc2, 0xf),    /* UOPS_RETIRED.* */
8977010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
8987010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
8997010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf),    /* SSEX_UOPS_RETIRED.* */
9007010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
9017010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf),    /* MEM_LOAD_RETIRED.* */
9027010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf),    /* FP_ASSIST.* */
9037010d129SBorislav Petkov 	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
90423e3983aSStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f),
9057010d129SBorislav Petkov 	EVENT_CONSTRAINT_END
9067010d129SBorislav Petkov };
9077010d129SBorislav Petkov 
9087010d129SBorislav Petkov struct event_constraint intel_westmere_pebs_event_constraints[] = {
9097010d129SBorislav Petkov 	INTEL_PLD_CONSTRAINT(0x100b, 0xf),      /* MEM_INST_RETIRED.* */
9107010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf),    /* MEM_UNCORE_RETIRED.* */
9117010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
9127010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf),    /* INSTR_RETIRED.* */
9137010d129SBorislav Petkov 	INTEL_EVENT_CONSTRAINT(0xc2, 0xf),    /* UOPS_RETIRED.* */
9147010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
9157010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc5, 0xf),    /* BR_MISP_RETIRED.* */
9167010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf),    /* SSEX_UOPS_RETIRED.* */
9177010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
9187010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf),    /* MEM_LOAD_RETIRED.* */
9197010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf),    /* FP_ASSIST.* */
9207010d129SBorislav Petkov 	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
92123e3983aSStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f),
9227010d129SBorislav Petkov 	EVENT_CONSTRAINT_END
9237010d129SBorislav Petkov };
9247010d129SBorislav Petkov 
9257010d129SBorislav Petkov struct event_constraint intel_snb_pebs_event_constraints[] = {
9267010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
9277010d129SBorislav Petkov 	INTEL_PLD_CONSTRAINT(0x01cd, 0x8),    /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
9287010d129SBorislav Petkov 	INTEL_PST_CONSTRAINT(0x02cd, 0x8),    /* MEM_TRANS_RETIRED.PRECISE_STORES */
9297010d129SBorislav Petkov 	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
93023e3983aSStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf),
9317010d129SBorislav Petkov         INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf),    /* MEM_UOP_RETIRED.* */
9327010d129SBorislav Petkov         INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
9337010d129SBorislav Petkov         INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf),    /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
9347010d129SBorislav Petkov         INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf),    /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
9357010d129SBorislav Petkov 	/* Allow all events as PEBS with no flags */
9367010d129SBorislav Petkov 	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
9377010d129SBorislav Petkov 	EVENT_CONSTRAINT_END
9387010d129SBorislav Petkov };
9397010d129SBorislav Petkov 
9407010d129SBorislav Petkov struct event_constraint intel_ivb_pebs_event_constraints[] = {
9417010d129SBorislav Petkov         INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
9427010d129SBorislav Petkov         INTEL_PLD_CONSTRAINT(0x01cd, 0x8),    /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
9437010d129SBorislav Petkov 	INTEL_PST_CONSTRAINT(0x02cd, 0x8),    /* MEM_TRANS_RETIRED.PRECISE_STORES */
9447010d129SBorislav Petkov 	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
94523e3983aSStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf),
9467010d129SBorislav Petkov 	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
94723e3983aSStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2),
9487010d129SBorislav Petkov 	INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf),    /* MEM_UOP_RETIRED.* */
9497010d129SBorislav Petkov 	INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
9507010d129SBorislav Petkov 	INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf),    /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
9517010d129SBorislav Petkov 	INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf),    /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
9527010d129SBorislav Petkov 	/* Allow all events as PEBS with no flags */
9537010d129SBorislav Petkov 	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
9547010d129SBorislav Petkov         EVENT_CONSTRAINT_END
9557010d129SBorislav Petkov };
9567010d129SBorislav Petkov 
9577010d129SBorislav Petkov struct event_constraint intel_hsw_pebs_event_constraints[] = {
9587010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
9597010d129SBorislav Petkov 	INTEL_PLD_CONSTRAINT(0x01cd, 0xf),    /* MEM_TRANS_RETIRED.* */
9607010d129SBorislav Petkov 	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
96123e3983aSStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf),
9627010d129SBorislav Petkov 	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
96323e3983aSStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2),
9647010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
9657010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
9667010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
9677010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
9687010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
9697010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
9707010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
9717010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
9727010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
9737010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd2, 0xf),    /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
9747010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd3, 0xf),    /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
9757010d129SBorislav Petkov 	/* Allow all events as PEBS with no flags */
9767010d129SBorislav Petkov 	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
9777010d129SBorislav Petkov 	EVENT_CONSTRAINT_END
9787010d129SBorislav Petkov };
9797010d129SBorislav Petkov 
980b3e62463SStephane Eranian struct event_constraint intel_bdw_pebs_event_constraints[] = {
981b3e62463SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
982b3e62463SStephane Eranian 	INTEL_PLD_CONSTRAINT(0x01cd, 0xf),    /* MEM_TRANS_RETIRED.* */
983b3e62463SStephane Eranian 	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
98423e3983aSStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf),
985b3e62463SStephane Eranian 	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
98623e3983aSStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2),
987b3e62463SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
988b3e62463SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
989b3e62463SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
990b3e62463SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
991b3e62463SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
992b3e62463SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
993b3e62463SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
994b3e62463SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
995b3e62463SStephane Eranian 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
996b3e62463SStephane Eranian 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf),    /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
997b3e62463SStephane Eranian 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf),    /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
998b3e62463SStephane Eranian 	/* Allow all events as PEBS with no flags */
999b3e62463SStephane Eranian 	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
1000b3e62463SStephane Eranian 	EVENT_CONSTRAINT_END
1001b3e62463SStephane Eranian };
1002b3e62463SStephane Eranian 
1003b3e62463SStephane Eranian 
10047010d129SBorislav Petkov struct event_constraint intel_skl_pebs_event_constraints[] = {
10057010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x1c0, 0x2),	/* INST_RETIRED.PREC_DIST */
10067010d129SBorislav Petkov 	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
100723e3983aSStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2),
10087010d129SBorislav Petkov 	/* INST_RETIRED.TOTAL_CYCLES_PS (inv=1, cmask=16) (cycles:p). */
100923e3983aSStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f),
10107010d129SBorislav Petkov 	INTEL_PLD_CONSTRAINT(0x1cd, 0xf),		      /* MEM_TRANS_RETIRED.* */
10117010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */
10127010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */
10137010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */
10147010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x22d0, 0xf), /* MEM_INST_RETIRED.LOCK_STORES */
10157010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */
10167010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */
10177010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */
10187010d129SBorislav Petkov 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */
10197010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf),    /* MEM_LOAD_RETIRED.* */
10207010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf),    /* MEM_LOAD_L3_HIT_RETIRED.* */
10217010d129SBorislav Petkov 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf),    /* MEM_LOAD_L3_MISS_RETIRED.* */
10227010d129SBorislav Petkov 	/* Allow all events as PEBS with no flags */
10237010d129SBorislav Petkov 	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
10247010d129SBorislav Petkov 	EVENT_CONSTRAINT_END
10257010d129SBorislav Petkov };
10267010d129SBorislav Petkov 
102760176089SKan Liang struct event_constraint intel_icl_pebs_event_constraints[] = {
10282de71ee1SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x100000000ULL),	/* old INST_RETIRED.PREC_DIST */
10292de71ee1SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x0100, 0x100000000ULL),	/* INST_RETIRED.PREC_DIST */
10303d0c3953SKan Liang 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x0400, 0x800000000ULL),	/* SLOTS */
103160176089SKan Liang 
103260176089SKan Liang 	INTEL_PLD_CONSTRAINT(0x1cd, 0xff),			/* MEM_TRANS_RETIRED.LOAD_LATENCY */
1033acc5568bSKan Liang 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf),	/* MEM_INST_RETIRED.STLB_MISS_LOADS */
1034acc5568bSKan Liang 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf),	/* MEM_INST_RETIRED.STLB_MISS_STORES */
1035acc5568bSKan Liang 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf),	/* MEM_INST_RETIRED.LOCK_LOADS */
1036acc5568bSKan Liang 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf),	/* MEM_INST_RETIRED.SPLIT_LOADS */
1037acc5568bSKan Liang 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf),	/* MEM_INST_RETIRED.SPLIT_STORES */
1038acc5568bSKan Liang 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf),	/* MEM_INST_RETIRED.ALL_LOADS */
1039acc5568bSKan Liang 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf),	/* MEM_INST_RETIRED.ALL_STORES */
104060176089SKan Liang 
104160176089SKan Liang 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD_RANGE(0xd1, 0xd4, 0xf), /* MEM_LOAD_*_RETIRED.* */
104260176089SKan Liang 
104360176089SKan Liang 	INTEL_FLAGS_EVENT_CONSTRAINT(0xd0, 0xf),		/* MEM_INST_RETIRED.* */
104460176089SKan Liang 
104560176089SKan Liang 	/*
104660176089SKan Liang 	 * Everything else is handled by PMU_FL_PEBS_ALL, because we
104760176089SKan Liang 	 * need the full constraints from the main table.
104860176089SKan Liang 	 */
104960176089SKan Liang 
105060176089SKan Liang 	EVENT_CONSTRAINT_END
105160176089SKan Liang };
105260176089SKan Liang 
105361b985e3SKan Liang struct event_constraint intel_spr_pebs_event_constraints[] = {
10542de71ee1SStephane Eranian 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x100, 0x100000000ULL),	/* INST_RETIRED.PREC_DIST */
105561b985e3SKan Liang 	INTEL_FLAGS_UEVENT_CONSTRAINT(0x0400, 0x800000000ULL),
105661b985e3SKan Liang 
105761b985e3SKan Liang 	INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xfe),
105861b985e3SKan Liang 	INTEL_PLD_CONSTRAINT(0x1cd, 0xfe),
105961b985e3SKan Liang 	INTEL_PSD_CONSTRAINT(0x2cd, 0x1),
10600916886bSKan Liang 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf),	/* MEM_INST_RETIRED.STLB_MISS_LOADS */
10610916886bSKan Liang 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf),	/* MEM_INST_RETIRED.STLB_MISS_STORES */
10620916886bSKan Liang 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf),	/* MEM_INST_RETIRED.LOCK_LOADS */
10630916886bSKan Liang 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf),	/* MEM_INST_RETIRED.SPLIT_LOADS */
10640916886bSKan Liang 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf),	/* MEM_INST_RETIRED.SPLIT_STORES */
10650916886bSKan Liang 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf),	/* MEM_INST_RETIRED.ALL_LOADS */
10660916886bSKan Liang 	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf),	/* MEM_INST_RETIRED.ALL_STORES */
106761b985e3SKan Liang 
106861b985e3SKan Liang 	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD_RANGE(0xd1, 0xd4, 0xf),
106961b985e3SKan Liang 
107061b985e3SKan Liang 	INTEL_FLAGS_EVENT_CONSTRAINT(0xd0, 0xf),
107161b985e3SKan Liang 
107261b985e3SKan Liang 	/*
107361b985e3SKan Liang 	 * Everything else is handled by PMU_FL_PEBS_ALL, because we
107461b985e3SKan Liang 	 * need the full constraints from the main table.
107561b985e3SKan Liang 	 */
107661b985e3SKan Liang 
107761b985e3SKan Liang 	EVENT_CONSTRAINT_END
107861b985e3SKan Liang };
107961b985e3SKan Liang 
10807010d129SBorislav Petkov struct event_constraint *intel_pebs_constraints(struct perf_event *event)
10817010d129SBorislav Petkov {
108224ee38ffSKan Liang 	struct event_constraint *pebs_constraints = hybrid(event->pmu, pebs_constraints);
10837010d129SBorislav Petkov 	struct event_constraint *c;
10847010d129SBorislav Petkov 
10857010d129SBorislav Petkov 	if (!event->attr.precise_ip)
10867010d129SBorislav Petkov 		return NULL;
10877010d129SBorislav Petkov 
108824ee38ffSKan Liang 	if (pebs_constraints) {
108924ee38ffSKan Liang 		for_each_event_constraint(c, pebs_constraints) {
109063b79f6eSPeter Zijlstra 			if (constraint_match(c, event->hw.config)) {
10917010d129SBorislav Petkov 				event->hw.flags |= c->flags;
10927010d129SBorislav Petkov 				return c;
10937010d129SBorislav Petkov 			}
10947010d129SBorislav Petkov 		}
10957010d129SBorislav Petkov 	}
10967010d129SBorislav Petkov 
109731962340SKan Liang 	/*
109831962340SKan Liang 	 * Extended PEBS support
109931962340SKan Liang 	 * Makes the PEBS code search the normal constraints.
110031962340SKan Liang 	 */
110131962340SKan Liang 	if (x86_pmu.flags & PMU_FL_PEBS_ALL)
110231962340SKan Liang 		return NULL;
110331962340SKan Liang 
11047010d129SBorislav Petkov 	return &emptyconstraint;
11057010d129SBorislav Petkov }
11067010d129SBorislav Petkov 
110709e61b4fSPeter Zijlstra /*
110809e61b4fSPeter Zijlstra  * We need the sched_task callback even for per-cpu events when we use
110909e61b4fSPeter Zijlstra  * the large interrupt threshold, such that we can provide PID and TID
111009e61b4fSPeter Zijlstra  * to PEBS samples.
111109e61b4fSPeter Zijlstra  */
111209e61b4fSPeter Zijlstra static inline bool pebs_needs_sched_cb(struct cpu_hw_events *cpuc)
11137010d129SBorislav Petkov {
111442880f72SAlexander Shishkin 	if (cpuc->n_pebs == cpuc->n_pebs_via_pt)
111542880f72SAlexander Shishkin 		return false;
111642880f72SAlexander Shishkin 
111709e61b4fSPeter Zijlstra 	return cpuc->n_pebs && (cpuc->n_pebs == cpuc->n_large_pebs);
111809e61b4fSPeter Zijlstra }
111909e61b4fSPeter Zijlstra 
1120bd275681SPeter Zijlstra void intel_pmu_pebs_sched_task(struct perf_event_pmu_context *pmu_ctx, bool sched_in)
1121df6c3db8SJiri Olsa {
1122df6c3db8SJiri Olsa 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1123df6c3db8SJiri Olsa 
1124df6c3db8SJiri Olsa 	if (!sched_in && pebs_needs_sched_cb(cpuc))
1125df6c3db8SJiri Olsa 		intel_pmu_drain_pebs_buffer();
1126df6c3db8SJiri Olsa }
1127df6c3db8SJiri Olsa 
112809e61b4fSPeter Zijlstra static inline void pebs_update_threshold(struct cpu_hw_events *cpuc)
112909e61b4fSPeter Zijlstra {
113009e61b4fSPeter Zijlstra 	struct debug_store *ds = cpuc->ds;
1131d4b294bfSKan Liang 	int max_pebs_events = hybrid(cpuc->pmu, max_pebs_events);
1132d4b294bfSKan Liang 	int num_counters_fixed = hybrid(cpuc->pmu, num_counters_fixed);
113309e61b4fSPeter Zijlstra 	u64 threshold;
1134ec71a398SKan Liang 	int reserved;
1135ec71a398SKan Liang 
113642880f72SAlexander Shishkin 	if (cpuc->n_pebs_via_pt)
113742880f72SAlexander Shishkin 		return;
113842880f72SAlexander Shishkin 
1139ec71a398SKan Liang 	if (x86_pmu.flags & PMU_FL_PEBS_ALL)
1140d4b294bfSKan Liang 		reserved = max_pebs_events + num_counters_fixed;
1141ec71a398SKan Liang 	else
1142d4b294bfSKan Liang 		reserved = max_pebs_events;
114309e61b4fSPeter Zijlstra 
114409e61b4fSPeter Zijlstra 	if (cpuc->n_pebs == cpuc->n_large_pebs) {
114509e61b4fSPeter Zijlstra 		threshold = ds->pebs_absolute_maximum -
1146c22497f5SKan Liang 			reserved * cpuc->pebs_record_size;
114709e61b4fSPeter Zijlstra 	} else {
1148c22497f5SKan Liang 		threshold = ds->pebs_buffer_base + cpuc->pebs_record_size;
114909e61b4fSPeter Zijlstra 	}
115009e61b4fSPeter Zijlstra 
115109e61b4fSPeter Zijlstra 	ds->pebs_interrupt_threshold = threshold;
115209e61b4fSPeter Zijlstra }
115309e61b4fSPeter Zijlstra 
1154c22497f5SKan Liang static void adaptive_pebs_record_size_update(void)
115509e61b4fSPeter Zijlstra {
1156c22497f5SKan Liang 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1157c22497f5SKan Liang 	u64 pebs_data_cfg = cpuc->pebs_data_cfg;
1158c22497f5SKan Liang 	int sz = sizeof(struct pebs_basic);
1159c22497f5SKan Liang 
1160c22497f5SKan Liang 	if (pebs_data_cfg & PEBS_DATACFG_MEMINFO)
1161c22497f5SKan Liang 		sz += sizeof(struct pebs_meminfo);
1162c22497f5SKan Liang 	if (pebs_data_cfg & PEBS_DATACFG_GP)
1163c22497f5SKan Liang 		sz += sizeof(struct pebs_gprs);
1164c22497f5SKan Liang 	if (pebs_data_cfg & PEBS_DATACFG_XMMS)
1165c22497f5SKan Liang 		sz += sizeof(struct pebs_xmm);
1166c22497f5SKan Liang 	if (pebs_data_cfg & PEBS_DATACFG_LBRS)
11675624986dSKan Liang 		sz += x86_pmu.lbr_nr * sizeof(struct lbr_entry);
1168c22497f5SKan Liang 
1169c22497f5SKan Liang 	cpuc->pebs_record_size = sz;
1170c22497f5SKan Liang }
1171c22497f5SKan Liang 
1172c22497f5SKan Liang #define PERF_PEBS_MEMINFO_TYPE	(PERF_SAMPLE_ADDR | PERF_SAMPLE_DATA_SRC |   \
11732a6c6b7dSKan Liang 				PERF_SAMPLE_PHYS_ADDR |			     \
11742a6c6b7dSKan Liang 				PERF_SAMPLE_WEIGHT_TYPE |		     \
117576a5433fSKan Liang 				PERF_SAMPLE_TRANSACTION |		     \
117676a5433fSKan Liang 				PERF_SAMPLE_DATA_PAGE_SIZE)
1177c22497f5SKan Liang 
1178c22497f5SKan Liang static u64 pebs_update_adaptive_cfg(struct perf_event *event)
1179c22497f5SKan Liang {
1180c22497f5SKan Liang 	struct perf_event_attr *attr = &event->attr;
1181c22497f5SKan Liang 	u64 sample_type = attr->sample_type;
1182c22497f5SKan Liang 	u64 pebs_data_cfg = 0;
1183c22497f5SKan Liang 	bool gprs, tsx_weight;
1184c22497f5SKan Liang 
1185c22497f5SKan Liang 	if (!(sample_type & ~(PERF_SAMPLE_IP|PERF_SAMPLE_TIME)) &&
1186c22497f5SKan Liang 	    attr->precise_ip > 1)
1187c22497f5SKan Liang 		return pebs_data_cfg;
1188c22497f5SKan Liang 
1189c22497f5SKan Liang 	if (sample_type & PERF_PEBS_MEMINFO_TYPE)
1190c22497f5SKan Liang 		pebs_data_cfg |= PEBS_DATACFG_MEMINFO;
1191c22497f5SKan Liang 
1192c22497f5SKan Liang 	/*
1193c22497f5SKan Liang 	 * We need GPRs when:
1194c22497f5SKan Liang 	 * + user requested them
1195c22497f5SKan Liang 	 * + precise_ip < 2 for the non event IP
1196c22497f5SKan Liang 	 * + For RTM TSX weight we need GPRs for the abort code.
1197c22497f5SKan Liang 	 */
1198c22497f5SKan Liang 	gprs = (sample_type & PERF_SAMPLE_REGS_INTR) &&
1199c22497f5SKan Liang 	       (attr->sample_regs_intr & PEBS_GP_REGS);
1200c22497f5SKan Liang 
12012a6c6b7dSKan Liang 	tsx_weight = (sample_type & PERF_SAMPLE_WEIGHT_TYPE) &&
1202c22497f5SKan Liang 		     ((attr->config & INTEL_ARCH_EVENT_MASK) ==
1203c22497f5SKan Liang 		      x86_pmu.rtm_abort_event);
1204c22497f5SKan Liang 
1205c22497f5SKan Liang 	if (gprs || (attr->precise_ip < 2) || tsx_weight)
1206c22497f5SKan Liang 		pebs_data_cfg |= PEBS_DATACFG_GP;
1207c22497f5SKan Liang 
1208c22497f5SKan Liang 	if ((sample_type & PERF_SAMPLE_REGS_INTR) &&
1209dce86ac7SKan Liang 	    (attr->sample_regs_intr & PERF_REG_EXTENDED_MASK))
1210c22497f5SKan Liang 		pebs_data_cfg |= PEBS_DATACFG_XMMS;
1211c22497f5SKan Liang 
1212c22497f5SKan Liang 	if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
1213c22497f5SKan Liang 		/*
1214c22497f5SKan Liang 		 * For now always log all LBRs. Could configure this
1215c22497f5SKan Liang 		 * later.
1216c22497f5SKan Liang 		 */
1217c22497f5SKan Liang 		pebs_data_cfg |= PEBS_DATACFG_LBRS |
1218c22497f5SKan Liang 			((x86_pmu.lbr_nr-1) << PEBS_DATACFG_LBR_SHIFT);
1219c22497f5SKan Liang 	}
1220c22497f5SKan Liang 
1221c22497f5SKan Liang 	return pebs_data_cfg;
1222c22497f5SKan Liang }
1223c22497f5SKan Liang 
1224c22497f5SKan Liang static void
1225c22497f5SKan Liang pebs_update_state(bool needed_cb, struct cpu_hw_events *cpuc,
1226c22497f5SKan Liang 		  struct perf_event *event, bool add)
1227c22497f5SKan Liang {
1228bd275681SPeter Zijlstra 	struct pmu *pmu = event->pmu;
1229b6a32f02SJiri Olsa 	/*
1230b6a32f02SJiri Olsa 	 * Make sure we get updated with the first PEBS
1231b6a32f02SJiri Olsa 	 * event. It will trigger also during removal, but
1232b6a32f02SJiri Olsa 	 * that does not hurt:
1233b6a32f02SJiri Olsa 	 */
1234b6a32f02SJiri Olsa 	bool update = cpuc->n_pebs == 1;
1235b6a32f02SJiri Olsa 
123609e61b4fSPeter Zijlstra 	if (needed_cb != pebs_needs_sched_cb(cpuc)) {
123709e61b4fSPeter Zijlstra 		if (!needed_cb)
123809e61b4fSPeter Zijlstra 			perf_sched_cb_inc(pmu);
123909e61b4fSPeter Zijlstra 		else
124009e61b4fSPeter Zijlstra 			perf_sched_cb_dec(pmu);
124109e61b4fSPeter Zijlstra 
1242b6a32f02SJiri Olsa 		update = true;
124309e61b4fSPeter Zijlstra 	}
1244b6a32f02SJiri Olsa 
1245c22497f5SKan Liang 	/*
1246c22497f5SKan Liang 	 * The PEBS record doesn't shrink on pmu::del(). Doing so would require
1247c22497f5SKan Liang 	 * iterating all remaining PEBS events to reconstruct the config.
1248c22497f5SKan Liang 	 */
1249c22497f5SKan Liang 	if (x86_pmu.intel_cap.pebs_baseline && add) {
1250c22497f5SKan Liang 		u64 pebs_data_cfg;
1251c22497f5SKan Liang 
1252c22497f5SKan Liang 		/* Clear pebs_data_cfg and pebs_record_size for first PEBS. */
1253c22497f5SKan Liang 		if (cpuc->n_pebs == 1) {
1254c22497f5SKan Liang 			cpuc->pebs_data_cfg = 0;
1255c22497f5SKan Liang 			cpuc->pebs_record_size = sizeof(struct pebs_basic);
1256c22497f5SKan Liang 		}
1257c22497f5SKan Liang 
1258c22497f5SKan Liang 		pebs_data_cfg = pebs_update_adaptive_cfg(event);
1259c22497f5SKan Liang 
1260c22497f5SKan Liang 		/* Update pebs_record_size if new event requires more data. */
1261c22497f5SKan Liang 		if (pebs_data_cfg & ~cpuc->pebs_data_cfg) {
1262c22497f5SKan Liang 			cpuc->pebs_data_cfg |= pebs_data_cfg;
1263c22497f5SKan Liang 			adaptive_pebs_record_size_update();
1264c22497f5SKan Liang 			update = true;
1265c22497f5SKan Liang 		}
1266c22497f5SKan Liang 	}
1267c22497f5SKan Liang 
1268b6a32f02SJiri Olsa 	if (update)
1269b6a32f02SJiri Olsa 		pebs_update_threshold(cpuc);
127009e61b4fSPeter Zijlstra }
127109e61b4fSPeter Zijlstra 
127268f7082fSPeter Zijlstra void intel_pmu_pebs_add(struct perf_event *event)
127309e61b4fSPeter Zijlstra {
127409e61b4fSPeter Zijlstra 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
127509e61b4fSPeter Zijlstra 	struct hw_perf_event *hwc = &event->hw;
127609e61b4fSPeter Zijlstra 	bool needed_cb = pebs_needs_sched_cb(cpuc);
127709e61b4fSPeter Zijlstra 
127809e61b4fSPeter Zijlstra 	cpuc->n_pebs++;
1279174afc3eSKan Liang 	if (hwc->flags & PERF_X86_EVENT_LARGE_PEBS)
128009e61b4fSPeter Zijlstra 		cpuc->n_large_pebs++;
128142880f72SAlexander Shishkin 	if (hwc->flags & PERF_X86_EVENT_PEBS_VIA_PT)
128242880f72SAlexander Shishkin 		cpuc->n_pebs_via_pt++;
128309e61b4fSPeter Zijlstra 
1284c22497f5SKan Liang 	pebs_update_state(needed_cb, cpuc, event, true);
12857010d129SBorislav Petkov }
12867010d129SBorislav Petkov 
128742880f72SAlexander Shishkin static void intel_pmu_pebs_via_pt_disable(struct perf_event *event)
128842880f72SAlexander Shishkin {
128942880f72SAlexander Shishkin 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
129042880f72SAlexander Shishkin 
129142880f72SAlexander Shishkin 	if (!is_pebs_pt(event))
129242880f72SAlexander Shishkin 		return;
129342880f72SAlexander Shishkin 
129442880f72SAlexander Shishkin 	if (!(cpuc->pebs_enabled & ~PEBS_VIA_PT_MASK))
129542880f72SAlexander Shishkin 		cpuc->pebs_enabled &= ~PEBS_VIA_PT_MASK;
129642880f72SAlexander Shishkin }
129742880f72SAlexander Shishkin 
129842880f72SAlexander Shishkin static void intel_pmu_pebs_via_pt_enable(struct perf_event *event)
129942880f72SAlexander Shishkin {
130042880f72SAlexander Shishkin 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
130142880f72SAlexander Shishkin 	struct hw_perf_event *hwc = &event->hw;
130242880f72SAlexander Shishkin 	struct debug_store *ds = cpuc->ds;
13034c58d922SLike Xu 	u64 value = ds->pebs_event_reset[hwc->idx];
13044c58d922SLike Xu 	u32 base = MSR_RELOAD_PMC0;
13054c58d922SLike Xu 	unsigned int idx = hwc->idx;
130642880f72SAlexander Shishkin 
130742880f72SAlexander Shishkin 	if (!is_pebs_pt(event))
130842880f72SAlexander Shishkin 		return;
130942880f72SAlexander Shishkin 
131042880f72SAlexander Shishkin 	if (!(event->hw.flags & PERF_X86_EVENT_LARGE_PEBS))
131142880f72SAlexander Shishkin 		cpuc->pebs_enabled |= PEBS_PMI_AFTER_EACH_RECORD;
131242880f72SAlexander Shishkin 
131342880f72SAlexander Shishkin 	cpuc->pebs_enabled |= PEBS_OUTPUT_PT;
131442880f72SAlexander Shishkin 
13154c58d922SLike Xu 	if (hwc->idx >= INTEL_PMC_IDX_FIXED) {
13164c58d922SLike Xu 		base = MSR_RELOAD_FIXED_CTR0;
13174c58d922SLike Xu 		idx = hwc->idx - INTEL_PMC_IDX_FIXED;
13182145e77fSKan Liang 		if (x86_pmu.intel_cap.pebs_format < 5)
13192145e77fSKan Liang 			value = ds->pebs_event_reset[MAX_PEBS_EVENTS_FMT4 + idx];
13202145e77fSKan Liang 		else
13214c58d922SLike Xu 			value = ds->pebs_event_reset[MAX_PEBS_EVENTS + idx];
13224c58d922SLike Xu 	}
13234c58d922SLike Xu 	wrmsrl(base + idx, value);
132442880f72SAlexander Shishkin }
132542880f72SAlexander Shishkin 
13267010d129SBorislav Petkov void intel_pmu_pebs_enable(struct perf_event *event)
13277010d129SBorislav Petkov {
13287010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
13297010d129SBorislav Petkov 	struct hw_perf_event *hwc = &event->hw;
13307010d129SBorislav Petkov 	struct debug_store *ds = cpuc->ds;
13314c58d922SLike Xu 	unsigned int idx = hwc->idx;
133209e61b4fSPeter Zijlstra 
13337010d129SBorislav Petkov 	hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;
13347010d129SBorislav Petkov 
13357010d129SBorislav Petkov 	cpuc->pebs_enabled |= 1ULL << hwc->idx;
13367010d129SBorislav Petkov 
133760176089SKan Liang 	if ((event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT) && (x86_pmu.version < 5))
13387010d129SBorislav Petkov 		cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32);
13397010d129SBorislav Petkov 	else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
13407010d129SBorislav Petkov 		cpuc->pebs_enabled |= 1ULL << 63;
13417010d129SBorislav Petkov 
1342c22497f5SKan Liang 	if (x86_pmu.intel_cap.pebs_baseline) {
1343c22497f5SKan Liang 		hwc->config |= ICL_EVENTSEL_ADAPTIVE;
1344c22497f5SKan Liang 		if (cpuc->pebs_data_cfg != cpuc->active_pebs_data_cfg) {
1345c22497f5SKan Liang 			wrmsrl(MSR_PEBS_DATA_CFG, cpuc->pebs_data_cfg);
1346c22497f5SKan Liang 			cpuc->active_pebs_data_cfg = cpuc->pebs_data_cfg;
1347c22497f5SKan Liang 		}
1348c22497f5SKan Liang 	}
1349c22497f5SKan Liang 
13502145e77fSKan Liang 	if (idx >= INTEL_PMC_IDX_FIXED) {
13512145e77fSKan Liang 		if (x86_pmu.intel_cap.pebs_format < 5)
13522145e77fSKan Liang 			idx = MAX_PEBS_EVENTS_FMT4 + (idx - INTEL_PMC_IDX_FIXED);
13532145e77fSKan Liang 		else
13544c58d922SLike Xu 			idx = MAX_PEBS_EVENTS + (idx - INTEL_PMC_IDX_FIXED);
13552145e77fSKan Liang 	}
13564c58d922SLike Xu 
13577010d129SBorislav Petkov 	/*
135809e61b4fSPeter Zijlstra 	 * Use auto-reload if possible to save a MSR write in the PMI.
135909e61b4fSPeter Zijlstra 	 * This must be done in pmu::start(), because PERF_EVENT_IOC_PERIOD.
13607010d129SBorislav Petkov 	 */
13617010d129SBorislav Petkov 	if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
1362ec71a398SKan Liang 		ds->pebs_event_reset[idx] =
13637010d129SBorislav Petkov 			(u64)(-hwc->sample_period) & x86_pmu.cntval_mask;
1364dc853e26SJiri Olsa 	} else {
13654c58d922SLike Xu 		ds->pebs_event_reset[idx] = 0;
13667010d129SBorislav Petkov 	}
136742880f72SAlexander Shishkin 
136842880f72SAlexander Shishkin 	intel_pmu_pebs_via_pt_enable(event);
136909e61b4fSPeter Zijlstra }
13707010d129SBorislav Petkov 
137168f7082fSPeter Zijlstra void intel_pmu_pebs_del(struct perf_event *event)
137209e61b4fSPeter Zijlstra {
137309e61b4fSPeter Zijlstra 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
137409e61b4fSPeter Zijlstra 	struct hw_perf_event *hwc = &event->hw;
137509e61b4fSPeter Zijlstra 	bool needed_cb = pebs_needs_sched_cb(cpuc);
137609e61b4fSPeter Zijlstra 
137709e61b4fSPeter Zijlstra 	cpuc->n_pebs--;
1378174afc3eSKan Liang 	if (hwc->flags & PERF_X86_EVENT_LARGE_PEBS)
137909e61b4fSPeter Zijlstra 		cpuc->n_large_pebs--;
138042880f72SAlexander Shishkin 	if (hwc->flags & PERF_X86_EVENT_PEBS_VIA_PT)
138142880f72SAlexander Shishkin 		cpuc->n_pebs_via_pt--;
138209e61b4fSPeter Zijlstra 
1383c22497f5SKan Liang 	pebs_update_state(needed_cb, cpuc, event, false);
13847010d129SBorislav Petkov }
13857010d129SBorislav Petkov 
13867010d129SBorislav Petkov void intel_pmu_pebs_disable(struct perf_event *event)
13877010d129SBorislav Petkov {
13887010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
13897010d129SBorislav Petkov 	struct hw_perf_event *hwc = &event->hw;
13907010d129SBorislav Petkov 
139142880f72SAlexander Shishkin 	if (cpuc->n_pebs == cpuc->n_large_pebs &&
139242880f72SAlexander Shishkin 	    cpuc->n_pebs != cpuc->n_pebs_via_pt)
13937010d129SBorislav Petkov 		intel_pmu_drain_pebs_buffer();
13947010d129SBorislav Petkov 
13957010d129SBorislav Petkov 	cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
13967010d129SBorislav Petkov 
139760176089SKan Liang 	if ((event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT) &&
139860176089SKan Liang 	    (x86_pmu.version < 5))
13997010d129SBorislav Petkov 		cpuc->pebs_enabled &= ~(1ULL << (hwc->idx + 32));
14007010d129SBorislav Petkov 	else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
14017010d129SBorislav Petkov 		cpuc->pebs_enabled &= ~(1ULL << 63);
14027010d129SBorislav Petkov 
140342880f72SAlexander Shishkin 	intel_pmu_pebs_via_pt_disable(event);
140442880f72SAlexander Shishkin 
14057010d129SBorislav Petkov 	if (cpuc->enabled)
14067010d129SBorislav Petkov 		wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
14077010d129SBorislav Petkov 
14087010d129SBorislav Petkov 	hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
14097010d129SBorislav Petkov }
14107010d129SBorislav Petkov 
14117010d129SBorislav Petkov void intel_pmu_pebs_enable_all(void)
14127010d129SBorislav Petkov {
14137010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
14147010d129SBorislav Petkov 
14157010d129SBorislav Petkov 	if (cpuc->pebs_enabled)
14167010d129SBorislav Petkov 		wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
14177010d129SBorislav Petkov }
14187010d129SBorislav Petkov 
14197010d129SBorislav Petkov void intel_pmu_pebs_disable_all(void)
14207010d129SBorislav Petkov {
14217010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
14227010d129SBorislav Petkov 
14237010d129SBorislav Petkov 	if (cpuc->pebs_enabled)
1424c22ac2a3SSong Liu 		__intel_pmu_pebs_disable_all();
14257010d129SBorislav Petkov }
14267010d129SBorislav Petkov 
14277010d129SBorislav Petkov static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
14287010d129SBorislav Petkov {
14297010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
14307010d129SBorislav Petkov 	unsigned long from = cpuc->lbr_entries[0].from;
14317010d129SBorislav Petkov 	unsigned long old_to, to = cpuc->lbr_entries[0].to;
14327010d129SBorislav Petkov 	unsigned long ip = regs->ip;
14337010d129SBorislav Petkov 	int is_64bit = 0;
14347010d129SBorislav Petkov 	void *kaddr;
14357010d129SBorislav Petkov 	int size;
14367010d129SBorislav Petkov 
14377010d129SBorislav Petkov 	/*
14387010d129SBorislav Petkov 	 * We don't need to fixup if the PEBS assist is fault like
14397010d129SBorislav Petkov 	 */
14407010d129SBorislav Petkov 	if (!x86_pmu.intel_cap.pebs_trap)
14417010d129SBorislav Petkov 		return 1;
14427010d129SBorislav Petkov 
14437010d129SBorislav Petkov 	/*
14447010d129SBorislav Petkov 	 * No LBR entry, no basic block, no rewinding
14457010d129SBorislav Petkov 	 */
14467010d129SBorislav Petkov 	if (!cpuc->lbr_stack.nr || !from || !to)
14477010d129SBorislav Petkov 		return 0;
14487010d129SBorislav Petkov 
14497010d129SBorislav Petkov 	/*
14507010d129SBorislav Petkov 	 * Basic blocks should never cross user/kernel boundaries
14517010d129SBorislav Petkov 	 */
14527010d129SBorislav Petkov 	if (kernel_ip(ip) != kernel_ip(to))
14537010d129SBorislav Petkov 		return 0;
14547010d129SBorislav Petkov 
14557010d129SBorislav Petkov 	/*
14567010d129SBorislav Petkov 	 * unsigned math, either ip is before the start (impossible) or
14577010d129SBorislav Petkov 	 * the basic block is larger than 1 page (sanity)
14587010d129SBorislav Petkov 	 */
14597010d129SBorislav Petkov 	if ((ip - to) > PEBS_FIXUP_SIZE)
14607010d129SBorislav Petkov 		return 0;
14617010d129SBorislav Petkov 
14627010d129SBorislav Petkov 	/*
14637010d129SBorislav Petkov 	 * We sampled a branch insn, rewind using the LBR stack
14647010d129SBorislav Petkov 	 */
14657010d129SBorislav Petkov 	if (ip == to) {
14667010d129SBorislav Petkov 		set_linear_ip(regs, from);
14677010d129SBorislav Petkov 		return 1;
14687010d129SBorislav Petkov 	}
14697010d129SBorislav Petkov 
14707010d129SBorislav Petkov 	size = ip - to;
14717010d129SBorislav Petkov 	if (!kernel_ip(ip)) {
14727010d129SBorislav Petkov 		int bytes;
14737010d129SBorislav Petkov 		u8 *buf = this_cpu_read(insn_buffer);
14747010d129SBorislav Petkov 
14757010d129SBorislav Petkov 		/* 'size' must fit our buffer, see above */
14767010d129SBorislav Petkov 		bytes = copy_from_user_nmi(buf, (void __user *)to, size);
14777010d129SBorislav Petkov 		if (bytes != 0)
14787010d129SBorislav Petkov 			return 0;
14797010d129SBorislav Petkov 
14807010d129SBorislav Petkov 		kaddr = buf;
14817010d129SBorislav Petkov 	} else {
14827010d129SBorislav Petkov 		kaddr = (void *)to;
14837010d129SBorislav Petkov 	}
14847010d129SBorislav Petkov 
14857010d129SBorislav Petkov 	do {
14867010d129SBorislav Petkov 		struct insn insn;
14877010d129SBorislav Petkov 
14887010d129SBorislav Petkov 		old_to = to;
14897010d129SBorislav Petkov 
14907010d129SBorislav Petkov #ifdef CONFIG_X86_64
1491375d4bfdSGabriel Krisman Bertazi 		is_64bit = kernel_ip(to) || any_64bit_mode(regs);
14927010d129SBorislav Petkov #endif
14937010d129SBorislav Petkov 		insn_init(&insn, kaddr, size, is_64bit);
14942ff49881SBorislav Petkov 
14957010d129SBorislav Petkov 		/*
14962ff49881SBorislav Petkov 		 * Make sure there was not a problem decoding the instruction.
14972ff49881SBorislav Petkov 		 * This is doubly important because we have an infinite loop if
14982ff49881SBorislav Petkov 		 * insn.length=0.
14997010d129SBorislav Petkov 		 */
15002ff49881SBorislav Petkov 		if (insn_get_length(&insn))
15017010d129SBorislav Petkov 			break;
15027010d129SBorislav Petkov 
15037010d129SBorislav Petkov 		to += insn.length;
15047010d129SBorislav Petkov 		kaddr += insn.length;
15057010d129SBorislav Petkov 		size -= insn.length;
15067010d129SBorislav Petkov 	} while (to < ip);
15077010d129SBorislav Petkov 
15087010d129SBorislav Petkov 	if (to == ip) {
15097010d129SBorislav Petkov 		set_linear_ip(regs, old_to);
15107010d129SBorislav Petkov 		return 1;
15117010d129SBorislav Petkov 	}
15127010d129SBorislav Petkov 
15137010d129SBorislav Petkov 	/*
15147010d129SBorislav Petkov 	 * Even though we decoded the basic block, the instruction stream
15157010d129SBorislav Petkov 	 * never matched the given IP, either the TO or the IP got corrupted.
15167010d129SBorislav Petkov 	 */
15177010d129SBorislav Petkov 	return 0;
15187010d129SBorislav Petkov }
15197010d129SBorislav Petkov 
152048f38aa4SAndi Kleen static inline u64 intel_get_tsx_weight(u64 tsx_tuning)
15217010d129SBorislav Petkov {
152248f38aa4SAndi Kleen 	if (tsx_tuning) {
152348f38aa4SAndi Kleen 		union hsw_tsx_tuning tsx = { .value = tsx_tuning };
15247010d129SBorislav Petkov 		return tsx.cycles_last_block;
15257010d129SBorislav Petkov 	}
15267010d129SBorislav Petkov 	return 0;
15277010d129SBorislav Petkov }
15287010d129SBorislav Petkov 
152948f38aa4SAndi Kleen static inline u64 intel_get_tsx_transaction(u64 tsx_tuning, u64 ax)
15307010d129SBorislav Petkov {
153148f38aa4SAndi Kleen 	u64 txn = (tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32;
15327010d129SBorislav Petkov 
15337010d129SBorislav Petkov 	/* For RTM XABORTs also log the abort code from AX */
153448f38aa4SAndi Kleen 	if ((txn & PERF_TXN_TRANSACTION) && (ax & 1))
153548f38aa4SAndi Kleen 		txn |= ((ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
15367010d129SBorislav Petkov 	return txn;
15377010d129SBorislav Petkov }
15387010d129SBorislav Petkov 
1539c22497f5SKan Liang static inline u64 get_pebs_status(void *n)
1540c22497f5SKan Liang {
1541c22497f5SKan Liang 	if (x86_pmu.intel_cap.pebs_format < 4)
1542c22497f5SKan Liang 		return ((struct pebs_record_nhm *)n)->status;
1543c22497f5SKan Liang 	return ((struct pebs_basic *)n)->applicable_counters;
1544c22497f5SKan Liang }
1545c22497f5SKan Liang 
154648f38aa4SAndi Kleen #define PERF_X86_EVENT_PEBS_HSW_PREC \
154748f38aa4SAndi Kleen 		(PERF_X86_EVENT_PEBS_ST_HSW | \
154848f38aa4SAndi Kleen 		 PERF_X86_EVENT_PEBS_LD_HSW | \
154948f38aa4SAndi Kleen 		 PERF_X86_EVENT_PEBS_NA_HSW)
155048f38aa4SAndi Kleen 
155148f38aa4SAndi Kleen static u64 get_data_src(struct perf_event *event, u64 aux)
155248f38aa4SAndi Kleen {
155348f38aa4SAndi Kleen 	u64 val = PERF_MEM_NA;
155448f38aa4SAndi Kleen 	int fl = event->hw.flags;
155548f38aa4SAndi Kleen 	bool fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC);
155648f38aa4SAndi Kleen 
155748f38aa4SAndi Kleen 	if (fl & PERF_X86_EVENT_PEBS_LDLAT)
1558ccf170e9SKan Liang 		val = load_latency_data(event, aux);
155961b985e3SKan Liang 	else if (fl & PERF_X86_EVENT_PEBS_STLAT)
1560ccf170e9SKan Liang 		val = store_latency_data(event, aux);
156139a41278SKan Liang 	else if (fl & PERF_X86_EVENT_PEBS_LAT_HYBRID)
156239a41278SKan Liang 		val = x86_pmu.pebs_latency_data(event, aux);
156348f38aa4SAndi Kleen 	else if (fst && (fl & PERF_X86_EVENT_PEBS_HSW_PREC))
156448f38aa4SAndi Kleen 		val = precise_datala_hsw(event, aux);
156548f38aa4SAndi Kleen 	else if (fst)
156648f38aa4SAndi Kleen 		val = precise_store_data(aux);
156748f38aa4SAndi Kleen 	return val;
156848f38aa4SAndi Kleen }
156948f38aa4SAndi Kleen 
157076a5433fSKan Liang #define PERF_SAMPLE_ADDR_TYPE	(PERF_SAMPLE_ADDR |		\
157176a5433fSKan Liang 				 PERF_SAMPLE_PHYS_ADDR |	\
157276a5433fSKan Liang 				 PERF_SAMPLE_DATA_PAGE_SIZE)
157376a5433fSKan Liang 
1574c22497f5SKan Liang static void setup_pebs_fixed_sample_data(struct perf_event *event,
15757010d129SBorislav Petkov 				   struct pt_regs *iregs, void *__pebs,
15767010d129SBorislav Petkov 				   struct perf_sample_data *data,
15777010d129SBorislav Petkov 				   struct pt_regs *regs)
15787010d129SBorislav Petkov {
15797010d129SBorislav Petkov 	/*
15807010d129SBorislav Petkov 	 * We cast to the biggest pebs_record but are careful not to
15817010d129SBorislav Petkov 	 * unconditionally access the 'extra' entries.
15827010d129SBorislav Petkov 	 */
15837010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
15847010d129SBorislav Petkov 	struct pebs_record_skl *pebs = __pebs;
15857010d129SBorislav Petkov 	u64 sample_type;
158648f38aa4SAndi Kleen 	int fll;
15877010d129SBorislav Petkov 
15887010d129SBorislav Petkov 	if (pebs == NULL)
15897010d129SBorislav Petkov 		return;
15907010d129SBorislav Petkov 
15917010d129SBorislav Petkov 	sample_type = event->attr.sample_type;
159248f38aa4SAndi Kleen 	fll = event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT;
15937010d129SBorislav Petkov 
15947010d129SBorislav Petkov 	perf_sample_data_init(data, 0, event->hw.last_period);
15957010d129SBorislav Petkov 
15967010d129SBorislav Petkov 	data->period = event->hw.last_period;
15977010d129SBorislav Petkov 
15987010d129SBorislav Petkov 	/*
15997010d129SBorislav Petkov 	 * Use latency for weight (only avail with PEBS-LL)
16007010d129SBorislav Petkov 	 */
16012abe681dSKan Liang 	if (fll && (sample_type & PERF_SAMPLE_WEIGHT_TYPE)) {
16022a6c6b7dSKan Liang 		data->weight.full = pebs->lat;
16032abe681dSKan Liang 		data->sample_flags |= PERF_SAMPLE_WEIGHT_TYPE;
16042abe681dSKan Liang 	}
16057010d129SBorislav Petkov 
16067010d129SBorislav Petkov 	/*
16077010d129SBorislav Petkov 	 * data.data_src encodes the data source
16087010d129SBorislav Petkov 	 */
1609e16fd7f2SKan Liang 	if (sample_type & PERF_SAMPLE_DATA_SRC) {
161048f38aa4SAndi Kleen 		data->data_src.val = get_data_src(event, pebs->dse);
1611e16fd7f2SKan Liang 		data->sample_flags |= PERF_SAMPLE_DATA_SRC;
1612e16fd7f2SKan Liang 	}
16137010d129SBorislav Petkov 
16147010d129SBorislav Petkov 	/*
16156cbc304fSPeter Zijlstra 	 * We must however always use iregs for the unwinder to stay sane; the
16166cbc304fSPeter Zijlstra 	 * record BP,SP,IP can point into thin air when the record is from a
1617a97673a1SIngo Molnar 	 * previous PMI context or an (I)RET happened between the record and
16186cbc304fSPeter Zijlstra 	 * PMI.
16196cbc304fSPeter Zijlstra 	 */
1620*31046500SNamhyung Kim 	if (sample_type & PERF_SAMPLE_CALLCHAIN)
1621*31046500SNamhyung Kim 		perf_sample_save_callchain(data, event, iregs);
16226cbc304fSPeter Zijlstra 
16236cbc304fSPeter Zijlstra 	/*
1624b8000586SPeter Zijlstra 	 * We use the interrupt regs as a base because the PEBS record does not
1625b8000586SPeter Zijlstra 	 * contain a full regs set, specifically it seems to lack segment
1626b8000586SPeter Zijlstra 	 * descriptors, which get used by things like user_mode().
16277010d129SBorislav Petkov 	 *
1628b8000586SPeter Zijlstra 	 * In the simple case fix up only the IP for PERF_SAMPLE_IP.
16297010d129SBorislav Petkov 	 */
16307010d129SBorislav Petkov 	*regs = *iregs;
1631d1e7e602SStephane Eranian 
1632d1e7e602SStephane Eranian 	/*
1633d1e7e602SStephane Eranian 	 * Initialize regs_>flags from PEBS,
1634d1e7e602SStephane Eranian 	 * Clear exact bit (which uses x86 EFLAGS Reserved bit 3),
1635d1e7e602SStephane Eranian 	 * i.e., do not rely on it being zero:
1636d1e7e602SStephane Eranian 	 */
1637d1e7e602SStephane Eranian 	regs->flags = pebs->flags & ~PERF_EFLAGS_EXACT;
16387010d129SBorislav Petkov 
16397010d129SBorislav Petkov 	if (sample_type & PERF_SAMPLE_REGS_INTR) {
16407010d129SBorislav Petkov 		regs->ax = pebs->ax;
16417010d129SBorislav Petkov 		regs->bx = pebs->bx;
16427010d129SBorislav Petkov 		regs->cx = pebs->cx;
16437010d129SBorislav Petkov 		regs->dx = pebs->dx;
16447010d129SBorislav Petkov 		regs->si = pebs->si;
16457010d129SBorislav Petkov 		regs->di = pebs->di;
1646b8000586SPeter Zijlstra 
16477010d129SBorislav Petkov 		regs->bp = pebs->bp;
16487010d129SBorislav Petkov 		regs->sp = pebs->sp;
16497010d129SBorislav Petkov 
16507010d129SBorislav Petkov #ifndef CONFIG_X86_32
16517010d129SBorislav Petkov 		regs->r8 = pebs->r8;
16527010d129SBorislav Petkov 		regs->r9 = pebs->r9;
16537010d129SBorislav Petkov 		regs->r10 = pebs->r10;
16547010d129SBorislav Petkov 		regs->r11 = pebs->r11;
16557010d129SBorislav Petkov 		regs->r12 = pebs->r12;
16567010d129SBorislav Petkov 		regs->r13 = pebs->r13;
16577010d129SBorislav Petkov 		regs->r14 = pebs->r14;
16587010d129SBorislav Petkov 		regs->r15 = pebs->r15;
16597010d129SBorislav Petkov #endif
16607010d129SBorislav Petkov 	}
16617010d129SBorislav Petkov 
166271eb9ee9SStephane Eranian 	if (event->attr.precise_ip > 1) {
1663d1e7e602SStephane Eranian 		/*
1664d1e7e602SStephane Eranian 		 * Haswell and later processors have an 'eventing IP'
1665d1e7e602SStephane Eranian 		 * (real IP) which fixes the off-by-1 skid in hardware.
1666d1e7e602SStephane Eranian 		 * Use it when precise_ip >= 2 :
1667d1e7e602SStephane Eranian 		 */
166871eb9ee9SStephane Eranian 		if (x86_pmu.intel_cap.pebs_format >= 2) {
166971eb9ee9SStephane Eranian 			set_linear_ip(regs, pebs->real_ip);
16707010d129SBorislav Petkov 			regs->flags |= PERF_EFLAGS_EXACT;
167171eb9ee9SStephane Eranian 		} else {
1672d1e7e602SStephane Eranian 			/* Otherwise, use PEBS off-by-1 IP: */
167371eb9ee9SStephane Eranian 			set_linear_ip(regs, pebs->ip);
167471eb9ee9SStephane Eranian 
1675d1e7e602SStephane Eranian 			/*
1676d1e7e602SStephane Eranian 			 * With precise_ip >= 2, try to fix up the off-by-1 IP
1677d1e7e602SStephane Eranian 			 * using the LBR. If successful, the fixup function
1678d1e7e602SStephane Eranian 			 * corrects regs->ip and calls set_linear_ip() on regs:
1679d1e7e602SStephane Eranian 			 */
168071eb9ee9SStephane Eranian 			if (intel_pmu_pebs_fixup_ip(regs))
16817010d129SBorislav Petkov 				regs->flags |= PERF_EFLAGS_EXACT;
168271eb9ee9SStephane Eranian 		}
1683d1e7e602SStephane Eranian 	} else {
1684d1e7e602SStephane Eranian 		/*
1685d1e7e602SStephane Eranian 		 * When precise_ip == 1, return the PEBS off-by-1 IP,
1686d1e7e602SStephane Eranian 		 * no fixup attempted:
1687d1e7e602SStephane Eranian 		 */
168871eb9ee9SStephane Eranian 		set_linear_ip(regs, pebs->ip);
1689d1e7e602SStephane Eranian 	}
169071eb9ee9SStephane Eranian 
16917010d129SBorislav Petkov 
169276a5433fSKan Liang 	if ((sample_type & PERF_SAMPLE_ADDR_TYPE) &&
16937b084630SNamhyung Kim 	    x86_pmu.intel_cap.pebs_format >= 1) {
16947010d129SBorislav Petkov 		data->addr = pebs->dla;
16957b084630SNamhyung Kim 		data->sample_flags |= PERF_SAMPLE_ADDR;
16967b084630SNamhyung Kim 	}
16977010d129SBorislav Petkov 
16987010d129SBorislav Petkov 	if (x86_pmu.intel_cap.pebs_format >= 2) {
16997010d129SBorislav Petkov 		/* Only set the TSX weight when no memory weight. */
17002abe681dSKan Liang 		if ((sample_type & PERF_SAMPLE_WEIGHT_TYPE) && !fll) {
17012a6c6b7dSKan Liang 			data->weight.full = intel_get_tsx_weight(pebs->tsx_tuning);
17022abe681dSKan Liang 			data->sample_flags |= PERF_SAMPLE_WEIGHT_TYPE;
17032abe681dSKan Liang 		}
1704ee9db0e1SKan Liang 		if (sample_type & PERF_SAMPLE_TRANSACTION) {
170548f38aa4SAndi Kleen 			data->txn = intel_get_tsx_transaction(pebs->tsx_tuning,
170648f38aa4SAndi Kleen 							      pebs->ax);
1707ee9db0e1SKan Liang 			data->sample_flags |= PERF_SAMPLE_TRANSACTION;
1708ee9db0e1SKan Liang 		}
17097010d129SBorislav Petkov 	}
17107010d129SBorislav Petkov 
17117010d129SBorislav Petkov 	/*
17127010d129SBorislav Petkov 	 * v3 supplies an accurate time stamp, so we use that
17137010d129SBorislav Petkov 	 * for the time stamp.
17147010d129SBorislav Petkov 	 *
17157010d129SBorislav Petkov 	 * We can only do this for the default trace clock.
17167010d129SBorislav Petkov 	 */
17177010d129SBorislav Petkov 	if (x86_pmu.intel_cap.pebs_format >= 3 &&
171847a3aeb3SKan Liang 		event->attr.use_clockid == 0) {
17197010d129SBorislav Petkov 		data->time = native_sched_clock_from_tsc(pebs->tsc);
172047a3aeb3SKan Liang 		data->sample_flags |= PERF_SAMPLE_TIME;
172147a3aeb3SKan Liang 	}
17227010d129SBorislav Petkov 
1723a9a931e2SKan Liang 	if (has_branch_stack(event)) {
17247010d129SBorislav Petkov 		data->br_stack = &cpuc->lbr_stack;
1725a9a931e2SKan Liang 		data->sample_flags |= PERF_SAMPLE_BRANCH_STACK;
1726a9a931e2SKan Liang 	}
17277010d129SBorislav Petkov }
17287010d129SBorislav Petkov 
1729c22497f5SKan Liang static void adaptive_pebs_save_regs(struct pt_regs *regs,
1730c22497f5SKan Liang 				    struct pebs_gprs *gprs)
1731c22497f5SKan Liang {
1732c22497f5SKan Liang 	regs->ax = gprs->ax;
1733c22497f5SKan Liang 	regs->bx = gprs->bx;
1734c22497f5SKan Liang 	regs->cx = gprs->cx;
1735c22497f5SKan Liang 	regs->dx = gprs->dx;
1736c22497f5SKan Liang 	regs->si = gprs->si;
1737c22497f5SKan Liang 	regs->di = gprs->di;
1738c22497f5SKan Liang 	regs->bp = gprs->bp;
1739c22497f5SKan Liang 	regs->sp = gprs->sp;
1740c22497f5SKan Liang #ifndef CONFIG_X86_32
1741c22497f5SKan Liang 	regs->r8 = gprs->r8;
1742c22497f5SKan Liang 	regs->r9 = gprs->r9;
1743c22497f5SKan Liang 	regs->r10 = gprs->r10;
1744c22497f5SKan Liang 	regs->r11 = gprs->r11;
1745c22497f5SKan Liang 	regs->r12 = gprs->r12;
1746c22497f5SKan Liang 	regs->r13 = gprs->r13;
1747c22497f5SKan Liang 	regs->r14 = gprs->r14;
1748c22497f5SKan Liang 	regs->r15 = gprs->r15;
1749c22497f5SKan Liang #endif
1750c22497f5SKan Liang }
1751c22497f5SKan Liang 
175261b985e3SKan Liang #define PEBS_LATENCY_MASK			0xffff
175361b985e3SKan Liang #define PEBS_CACHE_LATENCY_OFFSET		32
1754c87a3109SKan Liang #define PEBS_RETIRE_LATENCY_OFFSET		32
175561b985e3SKan Liang 
1756c22497f5SKan Liang /*
1757c22497f5SKan Liang  * With adaptive PEBS the layout depends on what fields are configured.
1758c22497f5SKan Liang  */
1759c22497f5SKan Liang 
1760c22497f5SKan Liang static void setup_pebs_adaptive_sample_data(struct perf_event *event,
1761c22497f5SKan Liang 					    struct pt_regs *iregs, void *__pebs,
1762c22497f5SKan Liang 					    struct perf_sample_data *data,
1763c22497f5SKan Liang 					    struct pt_regs *regs)
1764c22497f5SKan Liang {
1765c22497f5SKan Liang 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1766c22497f5SKan Liang 	struct pebs_basic *basic = __pebs;
1767c22497f5SKan Liang 	void *next_record = basic + 1;
1768c22497f5SKan Liang 	u64 sample_type;
1769c22497f5SKan Liang 	u64 format_size;
1770c22497f5SKan Liang 	struct pebs_meminfo *meminfo = NULL;
1771c22497f5SKan Liang 	struct pebs_gprs *gprs = NULL;
1772c22497f5SKan Liang 	struct x86_perf_regs *perf_regs;
1773c22497f5SKan Liang 
1774c22497f5SKan Liang 	if (basic == NULL)
1775c22497f5SKan Liang 		return;
1776c22497f5SKan Liang 
1777c22497f5SKan Liang 	perf_regs = container_of(regs, struct x86_perf_regs, regs);
1778c22497f5SKan Liang 	perf_regs->xmm_regs = NULL;
1779c22497f5SKan Liang 
1780c22497f5SKan Liang 	sample_type = event->attr.sample_type;
1781c22497f5SKan Liang 	format_size = basic->format_size;
1782c22497f5SKan Liang 	perf_sample_data_init(data, 0, event->hw.last_period);
1783c22497f5SKan Liang 	data->period = event->hw.last_period;
1784c22497f5SKan Liang 
178547a3aeb3SKan Liang 	if (event->attr.use_clockid == 0) {
1786c22497f5SKan Liang 		data->time = native_sched_clock_from_tsc(basic->tsc);
178747a3aeb3SKan Liang 		data->sample_flags |= PERF_SAMPLE_TIME;
178847a3aeb3SKan Liang 	}
1789c22497f5SKan Liang 
1790c22497f5SKan Liang 	/*
1791c22497f5SKan Liang 	 * We must however always use iregs for the unwinder to stay sane; the
1792c22497f5SKan Liang 	 * record BP,SP,IP can point into thin air when the record is from a
1793c22497f5SKan Liang 	 * previous PMI context or an (I)RET happened between the record and
1794c22497f5SKan Liang 	 * PMI.
1795c22497f5SKan Liang 	 */
1796*31046500SNamhyung Kim 	if (sample_type & PERF_SAMPLE_CALLCHAIN)
1797*31046500SNamhyung Kim 		perf_sample_save_callchain(data, event, iregs);
1798c22497f5SKan Liang 
1799c22497f5SKan Liang 	*regs = *iregs;
1800c22497f5SKan Liang 	/* The ip in basic is EventingIP */
1801c22497f5SKan Liang 	set_linear_ip(regs, basic->ip);
1802c22497f5SKan Liang 	regs->flags = PERF_EFLAGS_EXACT;
1803c22497f5SKan Liang 
1804c87a3109SKan Liang 	if ((sample_type & PERF_SAMPLE_WEIGHT_STRUCT) && (x86_pmu.flags & PMU_FL_RETIRE_LATENCY))
1805c87a3109SKan Liang 		data->weight.var3_w = format_size >> PEBS_RETIRE_LATENCY_OFFSET & PEBS_LATENCY_MASK;
1806c87a3109SKan Liang 
1807c22497f5SKan Liang 	/*
1808c22497f5SKan Liang 	 * The record for MEMINFO is in front of GP
1809c22497f5SKan Liang 	 * But PERF_SAMPLE_TRANSACTION needs gprs->ax.
1810c22497f5SKan Liang 	 * Save the pointer here but process later.
1811c22497f5SKan Liang 	 */
1812c22497f5SKan Liang 	if (format_size & PEBS_DATACFG_MEMINFO) {
1813c22497f5SKan Liang 		meminfo = next_record;
1814c22497f5SKan Liang 		next_record = meminfo + 1;
1815c22497f5SKan Liang 	}
1816c22497f5SKan Liang 
1817c22497f5SKan Liang 	if (format_size & PEBS_DATACFG_GP) {
1818c22497f5SKan Liang 		gprs = next_record;
1819c22497f5SKan Liang 		next_record = gprs + 1;
1820c22497f5SKan Liang 
1821c22497f5SKan Liang 		if (event->attr.precise_ip < 2) {
1822c22497f5SKan Liang 			set_linear_ip(regs, gprs->ip);
1823c22497f5SKan Liang 			regs->flags &= ~PERF_EFLAGS_EXACT;
1824c22497f5SKan Liang 		}
1825c22497f5SKan Liang 
1826c22497f5SKan Liang 		if (sample_type & PERF_SAMPLE_REGS_INTR)
1827c22497f5SKan Liang 			adaptive_pebs_save_regs(regs, gprs);
1828c22497f5SKan Liang 	}
1829c22497f5SKan Liang 
1830c22497f5SKan Liang 	if (format_size & PEBS_DATACFG_MEMINFO) {
183161b985e3SKan Liang 		if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) {
183261b985e3SKan Liang 			u64 weight = meminfo->latency;
183361b985e3SKan Liang 
183461b985e3SKan Liang 			if (x86_pmu.flags & PMU_FL_INSTR_LATENCY) {
183561b985e3SKan Liang 				data->weight.var2_w = weight & PEBS_LATENCY_MASK;
183661b985e3SKan Liang 				weight >>= PEBS_CACHE_LATENCY_OFFSET;
183761b985e3SKan Liang 			}
183861b985e3SKan Liang 
183961b985e3SKan Liang 			/*
184061b985e3SKan Liang 			 * Although meminfo::latency is defined as a u64,
184161b985e3SKan Liang 			 * only the lower 32 bits include the valid data
184261b985e3SKan Liang 			 * in practice on Ice Lake and earlier platforms.
184361b985e3SKan Liang 			 */
184461b985e3SKan Liang 			if (sample_type & PERF_SAMPLE_WEIGHT) {
184561b985e3SKan Liang 				data->weight.full = weight ?:
1846c22497f5SKan Liang 					intel_get_tsx_weight(meminfo->tsx_tuning);
184761b985e3SKan Liang 			} else {
184861b985e3SKan Liang 				data->weight.var1_dw = (u32)(weight & PEBS_LATENCY_MASK) ?:
184961b985e3SKan Liang 					intel_get_tsx_weight(meminfo->tsx_tuning);
185061b985e3SKan Liang 			}
18512abe681dSKan Liang 			data->sample_flags |= PERF_SAMPLE_WEIGHT_TYPE;
185261b985e3SKan Liang 		}
1853c22497f5SKan Liang 
1854e16fd7f2SKan Liang 		if (sample_type & PERF_SAMPLE_DATA_SRC) {
1855c22497f5SKan Liang 			data->data_src.val = get_data_src(event, meminfo->aux);
1856e16fd7f2SKan Liang 			data->sample_flags |= PERF_SAMPLE_DATA_SRC;
1857e16fd7f2SKan Liang 		}
1858c22497f5SKan Liang 
18597b084630SNamhyung Kim 		if (sample_type & PERF_SAMPLE_ADDR_TYPE) {
1860c22497f5SKan Liang 			data->addr = meminfo->address;
18617b084630SNamhyung Kim 			data->sample_flags |= PERF_SAMPLE_ADDR;
18627b084630SNamhyung Kim 		}
1863c22497f5SKan Liang 
1864ee9db0e1SKan Liang 		if (sample_type & PERF_SAMPLE_TRANSACTION) {
1865c22497f5SKan Liang 			data->txn = intel_get_tsx_transaction(meminfo->tsx_tuning,
1866c22497f5SKan Liang 							  gprs ? gprs->ax : 0);
1867ee9db0e1SKan Liang 			data->sample_flags |= PERF_SAMPLE_TRANSACTION;
1868ee9db0e1SKan Liang 		}
1869c22497f5SKan Liang 	}
1870c22497f5SKan Liang 
1871c22497f5SKan Liang 	if (format_size & PEBS_DATACFG_XMMS) {
1872c22497f5SKan Liang 		struct pebs_xmm *xmm = next_record;
1873c22497f5SKan Liang 
1874c22497f5SKan Liang 		next_record = xmm + 1;
1875c22497f5SKan Liang 		perf_regs->xmm_regs = xmm->xmm;
1876c22497f5SKan Liang 	}
1877c22497f5SKan Liang 
1878c22497f5SKan Liang 	if (format_size & PEBS_DATACFG_LBRS) {
18795624986dSKan Liang 		struct lbr_entry *lbr = next_record;
1880c22497f5SKan Liang 		int num_lbr = ((format_size >> PEBS_DATACFG_LBR_SHIFT)
1881c22497f5SKan Liang 					& 0xff) + 1;
18825624986dSKan Liang 		next_record = next_record + num_lbr * sizeof(struct lbr_entry);
1883c22497f5SKan Liang 
1884c22497f5SKan Liang 		if (has_branch_stack(event)) {
1885c22497f5SKan Liang 			intel_pmu_store_pebs_lbrs(lbr);
1886c22497f5SKan Liang 			data->br_stack = &cpuc->lbr_stack;
1887a9a931e2SKan Liang 			data->sample_flags |= PERF_SAMPLE_BRANCH_STACK;
1888c22497f5SKan Liang 		}
1889c22497f5SKan Liang 	}
1890c22497f5SKan Liang 
1891c22497f5SKan Liang 	WARN_ONCE(next_record != __pebs + (format_size >> 48),
1892c22497f5SKan Liang 			"PEBS record size %llu, expected %llu, config %llx\n",
1893c22497f5SKan Liang 			format_size >> 48,
1894c22497f5SKan Liang 			(u64)(next_record - __pebs),
1895c22497f5SKan Liang 			basic->format_size);
1896c22497f5SKan Liang }
1897c22497f5SKan Liang 
18987010d129SBorislav Petkov static inline void *
18997010d129SBorislav Petkov get_next_pebs_record_by_bit(void *base, void *top, int bit)
19007010d129SBorislav Petkov {
19017010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
19027010d129SBorislav Petkov 	void *at;
19037010d129SBorislav Petkov 	u64 pebs_status;
19047010d129SBorislav Petkov 
19057010d129SBorislav Petkov 	/*
19067010d129SBorislav Petkov 	 * fmt0 does not have a status bitfield (does not use
19077010d129SBorislav Petkov 	 * perf_record_nhm format)
19087010d129SBorislav Petkov 	 */
19097010d129SBorislav Petkov 	if (x86_pmu.intel_cap.pebs_format < 1)
19107010d129SBorislav Petkov 		return base;
19117010d129SBorislav Petkov 
19127010d129SBorislav Petkov 	if (base == NULL)
19137010d129SBorislav Petkov 		return NULL;
19147010d129SBorislav Petkov 
1915c22497f5SKan Liang 	for (at = base; at < top; at += cpuc->pebs_record_size) {
1916c22497f5SKan Liang 		unsigned long status = get_pebs_status(at);
19177010d129SBorislav Petkov 
1918c22497f5SKan Liang 		if (test_bit(bit, (unsigned long *)&status)) {
19197010d129SBorislav Petkov 			/* PEBS v3 has accurate status bits */
19207010d129SBorislav Petkov 			if (x86_pmu.intel_cap.pebs_format >= 3)
19217010d129SBorislav Petkov 				return at;
19227010d129SBorislav Petkov 
1923c22497f5SKan Liang 			if (status == (1 << bit))
19247010d129SBorislav Petkov 				return at;
19257010d129SBorislav Petkov 
19267010d129SBorislav Petkov 			/* clear non-PEBS bit and re-check */
1927c22497f5SKan Liang 			pebs_status = status & cpuc->pebs_enabled;
1928fd583ad1SKan Liang 			pebs_status &= PEBS_COUNTER_MASK;
19297010d129SBorislav Petkov 			if (pebs_status == (1 << bit))
19307010d129SBorislav Petkov 				return at;
19317010d129SBorislav Petkov 		}
19327010d129SBorislav Petkov 	}
19337010d129SBorislav Petkov 	return NULL;
19347010d129SBorislav Petkov }
19357010d129SBorislav Petkov 
19365bee2cc6SKan Liang void intel_pmu_auto_reload_read(struct perf_event *event)
19375bee2cc6SKan Liang {
19385bee2cc6SKan Liang 	WARN_ON(!(event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD));
19395bee2cc6SKan Liang 
19405bee2cc6SKan Liang 	perf_pmu_disable(event->pmu);
19415bee2cc6SKan Liang 	intel_pmu_drain_pebs_buffer();
19425bee2cc6SKan Liang 	perf_pmu_enable(event->pmu);
19435bee2cc6SKan Liang }
19445bee2cc6SKan Liang 
1945d31fc13fSKan Liang /*
1946d31fc13fSKan Liang  * Special variant of intel_pmu_save_and_restart() for auto-reload.
1947d31fc13fSKan Liang  */
1948d31fc13fSKan Liang static int
1949d31fc13fSKan Liang intel_pmu_save_and_restart_reload(struct perf_event *event, int count)
1950d31fc13fSKan Liang {
1951d31fc13fSKan Liang 	struct hw_perf_event *hwc = &event->hw;
1952d31fc13fSKan Liang 	int shift = 64 - x86_pmu.cntval_bits;
1953d31fc13fSKan Liang 	u64 period = hwc->sample_period;
1954d31fc13fSKan Liang 	u64 prev_raw_count, new_raw_count;
1955d31fc13fSKan Liang 	s64 new, old;
1956d31fc13fSKan Liang 
1957d31fc13fSKan Liang 	WARN_ON(!period);
1958d31fc13fSKan Liang 
1959d31fc13fSKan Liang 	/*
1960d31fc13fSKan Liang 	 * drain_pebs() only happens when the PMU is disabled.
1961d31fc13fSKan Liang 	 */
1962d31fc13fSKan Liang 	WARN_ON(this_cpu_read(cpu_hw_events.enabled));
1963d31fc13fSKan Liang 
1964d31fc13fSKan Liang 	prev_raw_count = local64_read(&hwc->prev_count);
1965d31fc13fSKan Liang 	rdpmcl(hwc->event_base_rdpmc, new_raw_count);
1966d31fc13fSKan Liang 	local64_set(&hwc->prev_count, new_raw_count);
1967d31fc13fSKan Liang 
1968d31fc13fSKan Liang 	/*
1969d31fc13fSKan Liang 	 * Since the counter increments a negative counter value and
1970d31fc13fSKan Liang 	 * overflows on the sign switch, giving the interval:
1971d31fc13fSKan Liang 	 *
1972d31fc13fSKan Liang 	 *   [-period, 0]
1973d31fc13fSKan Liang 	 *
1974d9f6e12fSIngo Molnar 	 * the difference between two consecutive reads is:
1975d31fc13fSKan Liang 	 *
1976d31fc13fSKan Liang 	 *   A) value2 - value1;
1977d31fc13fSKan Liang 	 *      when no overflows have happened in between,
1978d31fc13fSKan Liang 	 *
1979d31fc13fSKan Liang 	 *   B) (0 - value1) + (value2 - (-period));
1980d31fc13fSKan Liang 	 *      when one overflow happened in between,
1981d31fc13fSKan Liang 	 *
1982d31fc13fSKan Liang 	 *   C) (0 - value1) + (n - 1) * (period) + (value2 - (-period));
1983d31fc13fSKan Liang 	 *      when @n overflows happened in between.
1984d31fc13fSKan Liang 	 *
1985d31fc13fSKan Liang 	 * Here A) is the obvious difference, B) is the extension to the
1986d31fc13fSKan Liang 	 * discrete interval, where the first term is to the top of the
1987d31fc13fSKan Liang 	 * interval and the second term is from the bottom of the next
1988d31fc13fSKan Liang 	 * interval and C) the extension to multiple intervals, where the
1989d31fc13fSKan Liang 	 * middle term is the whole intervals covered.
1990d31fc13fSKan Liang 	 *
1991d31fc13fSKan Liang 	 * An equivalent of C, by reduction, is:
1992d31fc13fSKan Liang 	 *
1993d31fc13fSKan Liang 	 *   value2 - value1 + n * period
1994d31fc13fSKan Liang 	 */
1995d31fc13fSKan Liang 	new = ((s64)(new_raw_count << shift) >> shift);
1996d31fc13fSKan Liang 	old = ((s64)(prev_raw_count << shift) >> shift);
1997d31fc13fSKan Liang 	local64_add(new - old + count * period, &event->count);
1998d31fc13fSKan Liang 
1999f861854eSKan Liang 	local64_set(&hwc->period_left, -new);
2000f861854eSKan Liang 
2001d31fc13fSKan Liang 	perf_event_update_userpage(event);
2002d31fc13fSKan Liang 
2003d31fc13fSKan Liang 	return 0;
2004d31fc13fSKan Liang }
2005d31fc13fSKan Liang 
20069dfa9a5cSPeter Zijlstra static __always_inline void
20079dfa9a5cSPeter Zijlstra __intel_pmu_pebs_event(struct perf_event *event,
20087010d129SBorislav Petkov 		       struct pt_regs *iregs,
20099dfa9a5cSPeter Zijlstra 		       struct perf_sample_data *data,
20107010d129SBorislav Petkov 		       void *base, void *top,
2011c22497f5SKan Liang 		       int bit, int count,
2012c22497f5SKan Liang 		       void (*setup_sample)(struct perf_event *,
2013c22497f5SKan Liang 					    struct pt_regs *,
2014c22497f5SKan Liang 					    void *,
2015c22497f5SKan Liang 					    struct perf_sample_data *,
2016c22497f5SKan Liang 					    struct pt_regs *))
20177010d129SBorislav Petkov {
2018c22497f5SKan Liang 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2019d31fc13fSKan Liang 	struct hw_perf_event *hwc = &event->hw;
2020c22497f5SKan Liang 	struct x86_perf_regs perf_regs;
2021c22497f5SKan Liang 	struct pt_regs *regs = &perf_regs.regs;
20227010d129SBorislav Petkov 	void *at = get_next_pebs_record_by_bit(base, top, bit);
2023e506d1daSPeter Zijlstra 	static struct pt_regs dummy_iregs;
20247010d129SBorislav Petkov 
2025d31fc13fSKan Liang 	if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
2026d31fc13fSKan Liang 		/*
2027d31fc13fSKan Liang 		 * Now, auto-reload is only enabled in fixed period mode.
2028d31fc13fSKan Liang 		 * The reload value is always hwc->sample_period.
2029d31fc13fSKan Liang 		 * May need to change it, if auto-reload is enabled in
2030d31fc13fSKan Liang 		 * freq mode later.
2031d31fc13fSKan Liang 		 */
2032d31fc13fSKan Liang 		intel_pmu_save_and_restart_reload(event, count);
2033d31fc13fSKan Liang 	} else if (!intel_pmu_save_and_restart(event))
20347010d129SBorislav Petkov 		return;
20357010d129SBorislav Petkov 
203635d1ce6bSKan Liang 	if (!iregs)
203735d1ce6bSKan Liang 		iregs = &dummy_iregs;
203835d1ce6bSKan Liang 
20397010d129SBorislav Petkov 	while (count > 1) {
20409dfa9a5cSPeter Zijlstra 		setup_sample(event, iregs, at, data, regs);
20419dfa9a5cSPeter Zijlstra 		perf_event_output(event, data, regs);
2042c22497f5SKan Liang 		at += cpuc->pebs_record_size;
20437010d129SBorislav Petkov 		at = get_next_pebs_record_by_bit(at, top, bit);
20447010d129SBorislav Petkov 		count--;
20457010d129SBorislav Petkov 	}
20467010d129SBorislav Petkov 
20479dfa9a5cSPeter Zijlstra 	setup_sample(event, iregs, at, data, regs);
204835d1ce6bSKan Liang 	if (iregs == &dummy_iregs) {
204935d1ce6bSKan Liang 		/*
205035d1ce6bSKan Liang 		 * The PEBS records may be drained in the non-overflow context,
205135d1ce6bSKan Liang 		 * e.g., large PEBS + context switch. Perf should treat the
205235d1ce6bSKan Liang 		 * last record the same as other PEBS records, and doesn't
205335d1ce6bSKan Liang 		 * invoke the generic overflow handler.
205435d1ce6bSKan Liang 		 */
20559dfa9a5cSPeter Zijlstra 		perf_event_output(event, data, regs);
205635d1ce6bSKan Liang 	} else {
20577010d129SBorislav Petkov 		/*
20587010d129SBorislav Petkov 		 * All but the last records are processed.
20597010d129SBorislav Petkov 		 * The last one is left to be able to call the overflow handler.
20607010d129SBorislav Petkov 		 */
20619dfa9a5cSPeter Zijlstra 		if (perf_event_overflow(event, data, regs))
20627010d129SBorislav Petkov 			x86_pmu_stop(event, 0);
20637010d129SBorislav Petkov 	}
20647010d129SBorislav Petkov }
20657010d129SBorislav Petkov 
20669dfa9a5cSPeter Zijlstra static void intel_pmu_drain_pebs_core(struct pt_regs *iregs, struct perf_sample_data *data)
20677010d129SBorislav Petkov {
20687010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
20697010d129SBorislav Petkov 	struct debug_store *ds = cpuc->ds;
20707010d129SBorislav Petkov 	struct perf_event *event = cpuc->events[0]; /* PMC0 only */
20717010d129SBorislav Petkov 	struct pebs_record_core *at, *top;
20727010d129SBorislav Petkov 	int n;
20737010d129SBorislav Petkov 
20747010d129SBorislav Petkov 	if (!x86_pmu.pebs_active)
20757010d129SBorislav Petkov 		return;
20767010d129SBorislav Petkov 
20777010d129SBorislav Petkov 	at  = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base;
20787010d129SBorislav Petkov 	top = (struct pebs_record_core *)(unsigned long)ds->pebs_index;
20797010d129SBorislav Petkov 
20807010d129SBorislav Petkov 	/*
20817010d129SBorislav Petkov 	 * Whatever else happens, drain the thing
20827010d129SBorislav Petkov 	 */
20837010d129SBorislav Petkov 	ds->pebs_index = ds->pebs_buffer_base;
20847010d129SBorislav Petkov 
20857010d129SBorislav Petkov 	if (!test_bit(0, cpuc->active_mask))
20867010d129SBorislav Petkov 		return;
20877010d129SBorislav Petkov 
20887010d129SBorislav Petkov 	WARN_ON_ONCE(!event);
20897010d129SBorislav Petkov 
20907010d129SBorislav Petkov 	if (!event->attr.precise_ip)
20917010d129SBorislav Petkov 		return;
20927010d129SBorislav Petkov 
20937010d129SBorislav Petkov 	n = top - at;
2094d31fc13fSKan Liang 	if (n <= 0) {
2095d31fc13fSKan Liang 		if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)
2096d31fc13fSKan Liang 			intel_pmu_save_and_restart_reload(event, 0);
20977010d129SBorislav Petkov 		return;
2098d31fc13fSKan Liang 	}
20997010d129SBorislav Petkov 
21009dfa9a5cSPeter Zijlstra 	__intel_pmu_pebs_event(event, iregs, data, at, top, 0, n,
2101c22497f5SKan Liang 			       setup_pebs_fixed_sample_data);
21027010d129SBorislav Petkov }
21037010d129SBorislav Petkov 
2104477f00f9SKan Liang static void intel_pmu_pebs_event_update_no_drain(struct cpu_hw_events *cpuc, int size)
2105477f00f9SKan Liang {
2106477f00f9SKan Liang 	struct perf_event *event;
2107477f00f9SKan Liang 	int bit;
2108477f00f9SKan Liang 
2109477f00f9SKan Liang 	/*
2110477f00f9SKan Liang 	 * The drain_pebs() could be called twice in a short period
2111477f00f9SKan Liang 	 * for auto-reload event in pmu::read(). There are no
2112477f00f9SKan Liang 	 * overflows have happened in between.
2113477f00f9SKan Liang 	 * It needs to call intel_pmu_save_and_restart_reload() to
2114477f00f9SKan Liang 	 * update the event->count for this case.
2115477f00f9SKan Liang 	 */
2116477f00f9SKan Liang 	for_each_set_bit(bit, (unsigned long *)&cpuc->pebs_enabled, size) {
2117477f00f9SKan Liang 		event = cpuc->events[bit];
2118477f00f9SKan Liang 		if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)
2119477f00f9SKan Liang 			intel_pmu_save_and_restart_reload(event, 0);
2120477f00f9SKan Liang 	}
2121477f00f9SKan Liang }
2122477f00f9SKan Liang 
21239dfa9a5cSPeter Zijlstra static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs, struct perf_sample_data *data)
21247010d129SBorislav Petkov {
21257010d129SBorislav Petkov 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
21267010d129SBorislav Petkov 	struct debug_store *ds = cpuc->ds;
21277010d129SBorislav Petkov 	struct perf_event *event;
21287010d129SBorislav Petkov 	void *base, *at, *top;
2129ec71a398SKan Liang 	short counts[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {};
2130ec71a398SKan Liang 	short error[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {};
2131ec71a398SKan Liang 	int bit, i, size;
2132ec71a398SKan Liang 	u64 mask;
21337010d129SBorislav Petkov 
21347010d129SBorislav Petkov 	if (!x86_pmu.pebs_active)
21357010d129SBorislav Petkov 		return;
21367010d129SBorislav Petkov 
21377010d129SBorislav Petkov 	base = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
21387010d129SBorislav Petkov 	top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
21397010d129SBorislav Petkov 
21407010d129SBorislav Petkov 	ds->pebs_index = ds->pebs_buffer_base;
21417010d129SBorislav Petkov 
2142ec71a398SKan Liang 	mask = (1ULL << x86_pmu.max_pebs_events) - 1;
2143ec71a398SKan Liang 	size = x86_pmu.max_pebs_events;
2144ec71a398SKan Liang 	if (x86_pmu.flags & PMU_FL_PEBS_ALL) {
2145ec71a398SKan Liang 		mask |= ((1ULL << x86_pmu.num_counters_fixed) - 1) << INTEL_PMC_IDX_FIXED;
2146ec71a398SKan Liang 		size = INTEL_PMC_IDX_FIXED + x86_pmu.num_counters_fixed;
2147ec71a398SKan Liang 	}
2148ec71a398SKan Liang 
2149d31fc13fSKan Liang 	if (unlikely(base >= top)) {
2150477f00f9SKan Liang 		intel_pmu_pebs_event_update_no_drain(cpuc, size);
21517010d129SBorislav Petkov 		return;
2152d31fc13fSKan Liang 	}
21537010d129SBorislav Petkov 
21547010d129SBorislav Petkov 	for (at = base; at < top; at += x86_pmu.pebs_record_size) {
21557010d129SBorislav Petkov 		struct pebs_record_nhm *p = at;
21567010d129SBorislav Petkov 		u64 pebs_status;
21577010d129SBorislav Petkov 
21588ef9b845SPeter Zijlstra 		pebs_status = p->status & cpuc->pebs_enabled;
2159ec71a398SKan Liang 		pebs_status &= mask;
21608ef9b845SPeter Zijlstra 
21618ef9b845SPeter Zijlstra 		/* PEBS v3 has more accurate status bits */
21627010d129SBorislav Petkov 		if (x86_pmu.intel_cap.pebs_format >= 3) {
2163c22497f5SKan Liang 			for_each_set_bit(bit, (unsigned long *)&pebs_status, size)
21647010d129SBorislav Petkov 				counts[bit]++;
21657010d129SBorislav Petkov 
21667010d129SBorislav Petkov 			continue;
21677010d129SBorislav Petkov 		}
21687010d129SBorislav Petkov 
21697010d129SBorislav Petkov 		/*
21707010d129SBorislav Petkov 		 * On some CPUs the PEBS status can be zero when PEBS is
21717010d129SBorislav Petkov 		 * racing with clearing of GLOBAL_STATUS.
21727010d129SBorislav Petkov 		 *
21737010d129SBorislav Petkov 		 * Normally we would drop that record, but in the
21747010d129SBorislav Petkov 		 * case when there is only a single active PEBS event
21757010d129SBorislav Petkov 		 * we can assume it's for that event.
21767010d129SBorislav Petkov 		 */
21777010d129SBorislav Petkov 		if (!pebs_status && cpuc->pebs_enabled &&
21787010d129SBorislav Petkov 			!(cpuc->pebs_enabled & (cpuc->pebs_enabled-1)))
2179d88d05a9SKan Liang 			pebs_status = p->status = cpuc->pebs_enabled;
21807010d129SBorislav Petkov 
21817010d129SBorislav Petkov 		bit = find_first_bit((unsigned long *)&pebs_status,
21827010d129SBorislav Petkov 					x86_pmu.max_pebs_events);
21837010d129SBorislav Petkov 		if (bit >= x86_pmu.max_pebs_events)
21847010d129SBorislav Petkov 			continue;
21857010d129SBorislav Petkov 
21867010d129SBorislav Petkov 		/*
21877010d129SBorislav Petkov 		 * The PEBS hardware does not deal well with the situation
21887010d129SBorislav Petkov 		 * when events happen near to each other and multiple bits
21897010d129SBorislav Petkov 		 * are set. But it should happen rarely.
21907010d129SBorislav Petkov 		 *
21917010d129SBorislav Petkov 		 * If these events include one PEBS and multiple non-PEBS
21927010d129SBorislav Petkov 		 * events, it doesn't impact PEBS record. The record will
21937010d129SBorislav Petkov 		 * be handled normally. (slow path)
21947010d129SBorislav Petkov 		 *
21957010d129SBorislav Petkov 		 * If these events include two or more PEBS events, the
21967010d129SBorislav Petkov 		 * records for the events can be collapsed into a single
21977010d129SBorislav Petkov 		 * one, and it's not possible to reconstruct all events
21987010d129SBorislav Petkov 		 * that caused the PEBS record. It's called collision.
21997010d129SBorislav Petkov 		 * If collision happened, the record will be dropped.
22007010d129SBorislav Petkov 		 */
2201fc17db8aSStephane Eranian 		if (pebs_status != (1ULL << bit)) {
2202c22497f5SKan Liang 			for_each_set_bit(i, (unsigned long *)&pebs_status, size)
22037010d129SBorislav Petkov 				error[i]++;
22047010d129SBorislav Petkov 			continue;
22057010d129SBorislav Petkov 		}
22067010d129SBorislav Petkov 
22077010d129SBorislav Petkov 		counts[bit]++;
22087010d129SBorislav Petkov 	}
22097010d129SBorislav Petkov 
2210c22497f5SKan Liang 	for_each_set_bit(bit, (unsigned long *)&mask, size) {
22117010d129SBorislav Petkov 		if ((counts[bit] == 0) && (error[bit] == 0))
22127010d129SBorislav Petkov 			continue;
22137010d129SBorislav Petkov 
22147010d129SBorislav Petkov 		event = cpuc->events[bit];
22158ef9b845SPeter Zijlstra 		if (WARN_ON_ONCE(!event))
22168ef9b845SPeter Zijlstra 			continue;
22178ef9b845SPeter Zijlstra 
22188ef9b845SPeter Zijlstra 		if (WARN_ON_ONCE(!event->attr.precise_ip))
22198ef9b845SPeter Zijlstra 			continue;
22207010d129SBorislav Petkov 
22217010d129SBorislav Petkov 		/* log dropped samples number */
2222475113d9SJiri Olsa 		if (error[bit]) {
22237010d129SBorislav Petkov 			perf_log_lost_samples(event, error[bit]);
22247010d129SBorislav Petkov 
22255debf021SNamhyung Kim 			if (iregs && perf_event_account_interrupt(event))
2226475113d9SJiri Olsa 				x86_pmu_stop(event, 0);
2227475113d9SJiri Olsa 		}
2228475113d9SJiri Olsa 
22297010d129SBorislav Petkov 		if (counts[bit]) {
22309dfa9a5cSPeter Zijlstra 			__intel_pmu_pebs_event(event, iregs, data, base,
2231c22497f5SKan Liang 					       top, bit, counts[bit],
2232c22497f5SKan Liang 					       setup_pebs_fixed_sample_data);
22337010d129SBorislav Petkov 		}
22347010d129SBorislav Petkov 	}
22357010d129SBorislav Petkov }
22367010d129SBorislav Petkov 
22379dfa9a5cSPeter Zijlstra static void intel_pmu_drain_pebs_icl(struct pt_regs *iregs, struct perf_sample_data *data)
2238c22497f5SKan Liang {
2239c22497f5SKan Liang 	short counts[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {};
2240c22497f5SKan Liang 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2241d4b294bfSKan Liang 	int max_pebs_events = hybrid(cpuc->pmu, max_pebs_events);
2242d4b294bfSKan Liang 	int num_counters_fixed = hybrid(cpuc->pmu, num_counters_fixed);
2243c22497f5SKan Liang 	struct debug_store *ds = cpuc->ds;
2244c22497f5SKan Liang 	struct perf_event *event;
2245c22497f5SKan Liang 	void *base, *at, *top;
2246c22497f5SKan Liang 	int bit, size;
2247c22497f5SKan Liang 	u64 mask;
2248c22497f5SKan Liang 
2249c22497f5SKan Liang 	if (!x86_pmu.pebs_active)
2250c22497f5SKan Liang 		return;
2251c22497f5SKan Liang 
2252c22497f5SKan Liang 	base = (struct pebs_basic *)(unsigned long)ds->pebs_buffer_base;
2253c22497f5SKan Liang 	top = (struct pebs_basic *)(unsigned long)ds->pebs_index;
2254c22497f5SKan Liang 
2255c22497f5SKan Liang 	ds->pebs_index = ds->pebs_buffer_base;
2256c22497f5SKan Liang 
2257d4b294bfSKan Liang 	mask = ((1ULL << max_pebs_events) - 1) |
2258d4b294bfSKan Liang 	       (((1ULL << num_counters_fixed) - 1) << INTEL_PMC_IDX_FIXED);
2259d4b294bfSKan Liang 	size = INTEL_PMC_IDX_FIXED + num_counters_fixed;
2260c22497f5SKan Liang 
2261c22497f5SKan Liang 	if (unlikely(base >= top)) {
2262c22497f5SKan Liang 		intel_pmu_pebs_event_update_no_drain(cpuc, size);
2263c22497f5SKan Liang 		return;
2264c22497f5SKan Liang 	}
2265c22497f5SKan Liang 
2266c22497f5SKan Liang 	for (at = base; at < top; at += cpuc->pebs_record_size) {
2267c22497f5SKan Liang 		u64 pebs_status;
2268c22497f5SKan Liang 
2269c22497f5SKan Liang 		pebs_status = get_pebs_status(at) & cpuc->pebs_enabled;
2270c22497f5SKan Liang 		pebs_status &= mask;
2271c22497f5SKan Liang 
2272c22497f5SKan Liang 		for_each_set_bit(bit, (unsigned long *)&pebs_status, size)
2273c22497f5SKan Liang 			counts[bit]++;
2274c22497f5SKan Liang 	}
2275c22497f5SKan Liang 
2276c22497f5SKan Liang 	for_each_set_bit(bit, (unsigned long *)&mask, size) {
2277c22497f5SKan Liang 		if (counts[bit] == 0)
2278c22497f5SKan Liang 			continue;
2279c22497f5SKan Liang 
2280c22497f5SKan Liang 		event = cpuc->events[bit];
2281c22497f5SKan Liang 		if (WARN_ON_ONCE(!event))
2282c22497f5SKan Liang 			continue;
2283c22497f5SKan Liang 
2284c22497f5SKan Liang 		if (WARN_ON_ONCE(!event->attr.precise_ip))
2285c22497f5SKan Liang 			continue;
2286c22497f5SKan Liang 
22879dfa9a5cSPeter Zijlstra 		__intel_pmu_pebs_event(event, iregs, data, base,
2288c22497f5SKan Liang 				       top, bit, counts[bit],
2289c22497f5SKan Liang 				       setup_pebs_adaptive_sample_data);
2290c22497f5SKan Liang 	}
2291c22497f5SKan Liang }
2292c22497f5SKan Liang 
22937010d129SBorislav Petkov /*
22947010d129SBorislav Petkov  * BTS, PEBS probe and setup
22957010d129SBorislav Petkov  */
22967010d129SBorislav Petkov 
22977010d129SBorislav Petkov void __init intel_ds_init(void)
22987010d129SBorislav Petkov {
22997010d129SBorislav Petkov 	/*
23007010d129SBorislav Petkov 	 * No support for 32bit formats
23017010d129SBorislav Petkov 	 */
23027010d129SBorislav Petkov 	if (!boot_cpu_has(X86_FEATURE_DTES64))
23037010d129SBorislav Petkov 		return;
23047010d129SBorislav Petkov 
23057010d129SBorislav Petkov 	x86_pmu.bts  = boot_cpu_has(X86_FEATURE_BTS);
23067010d129SBorislav Petkov 	x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS);
2307e72daf3fSJiri Olsa 	x86_pmu.pebs_buffer_size = PEBS_BUFFER_SIZE;
2308cd6b984fSKan Liang 	if (x86_pmu.version <= 4)
23099b545c04SAndi Kleen 		x86_pmu.pebs_no_isolation = 1;
2310cd6b984fSKan Liang 
23117010d129SBorislav Petkov 	if (x86_pmu.pebs) {
23127010d129SBorislav Petkov 		char pebs_type = x86_pmu.intel_cap.pebs_trap ?  '+' : '-';
2313c22497f5SKan Liang 		char *pebs_qual = "";
23147010d129SBorislav Petkov 		int format = x86_pmu.intel_cap.pebs_format;
23157010d129SBorislav Petkov 
2316c22497f5SKan Liang 		if (format < 4)
2317c22497f5SKan Liang 			x86_pmu.intel_cap.pebs_baseline = 0;
2318c22497f5SKan Liang 
23197010d129SBorislav Petkov 		switch (format) {
23207010d129SBorislav Petkov 		case 0:
23217010d129SBorislav Petkov 			pr_cont("PEBS fmt0%c, ", pebs_type);
23227010d129SBorislav Petkov 			x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
2323e72daf3fSJiri Olsa 			/*
2324e72daf3fSJiri Olsa 			 * Using >PAGE_SIZE buffers makes the WRMSR to
2325e72daf3fSJiri Olsa 			 * PERF_GLOBAL_CTRL in intel_pmu_enable_all()
2326e72daf3fSJiri Olsa 			 * mysteriously hang on Core2.
2327e72daf3fSJiri Olsa 			 *
2328e72daf3fSJiri Olsa 			 * As a workaround, we don't do this.
2329e72daf3fSJiri Olsa 			 */
2330e72daf3fSJiri Olsa 			x86_pmu.pebs_buffer_size = PAGE_SIZE;
23317010d129SBorislav Petkov 			x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
23327010d129SBorislav Petkov 			break;
23337010d129SBorislav Petkov 
23347010d129SBorislav Petkov 		case 1:
23357010d129SBorislav Petkov 			pr_cont("PEBS fmt1%c, ", pebs_type);
23367010d129SBorislav Petkov 			x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
23377010d129SBorislav Petkov 			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
23387010d129SBorislav Petkov 			break;
23397010d129SBorislav Petkov 
23407010d129SBorislav Petkov 		case 2:
23417010d129SBorislav Petkov 			pr_cont("PEBS fmt2%c, ", pebs_type);
23427010d129SBorislav Petkov 			x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw);
23437010d129SBorislav Petkov 			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
23447010d129SBorislav Petkov 			break;
23457010d129SBorislav Petkov 
23467010d129SBorislav Petkov 		case 3:
23477010d129SBorislav Petkov 			pr_cont("PEBS fmt3%c, ", pebs_type);
23487010d129SBorislav Petkov 			x86_pmu.pebs_record_size =
23497010d129SBorislav Petkov 						sizeof(struct pebs_record_skl);
23507010d129SBorislav Petkov 			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
2351174afc3eSKan Liang 			x86_pmu.large_pebs_flags |= PERF_SAMPLE_TIME;
23527010d129SBorislav Petkov 			break;
23537010d129SBorislav Petkov 
2354c22497f5SKan Liang 		case 4:
23552145e77fSKan Liang 		case 5:
2356c22497f5SKan Liang 			x86_pmu.drain_pebs = intel_pmu_drain_pebs_icl;
2357c22497f5SKan Liang 			x86_pmu.pebs_record_size = sizeof(struct pebs_basic);
2358c22497f5SKan Liang 			if (x86_pmu.intel_cap.pebs_baseline) {
2359c22497f5SKan Liang 				x86_pmu.large_pebs_flags |=
2360c22497f5SKan Liang 					PERF_SAMPLE_BRANCH_STACK |
2361c22497f5SKan Liang 					PERF_SAMPLE_TIME;
2362c22497f5SKan Liang 				x86_pmu.flags |= PMU_FL_PEBS_ALL;
23637d359886SPeter Zijlstra 				x86_pmu.pebs_capable = ~0ULL;
2364c22497f5SKan Liang 				pebs_qual = "-baseline";
236561e76d53SKan Liang 				x86_get_pmu(smp_processor_id())->capabilities |= PERF_PMU_CAP_EXTENDED_REGS;
2366c22497f5SKan Liang 			} else {
2367c22497f5SKan Liang 				/* Only basic record supported */
2368c22497f5SKan Liang 				x86_pmu.large_pebs_flags &=
2369c22497f5SKan Liang 					~(PERF_SAMPLE_ADDR |
2370c22497f5SKan Liang 					  PERF_SAMPLE_TIME |
2371c22497f5SKan Liang 					  PERF_SAMPLE_DATA_SRC |
2372c22497f5SKan Liang 					  PERF_SAMPLE_TRANSACTION |
2373c22497f5SKan Liang 					  PERF_SAMPLE_REGS_USER |
2374c22497f5SKan Liang 					  PERF_SAMPLE_REGS_INTR);
2375c22497f5SKan Liang 			}
2376c22497f5SKan Liang 			pr_cont("PEBS fmt4%c%s, ", pebs_type, pebs_qual);
237742880f72SAlexander Shishkin 
2378d0946a88SKan Liang 			if (!is_hybrid() && x86_pmu.intel_cap.pebs_output_pt_available) {
237942880f72SAlexander Shishkin 				pr_cont("PEBS-via-PT, ");
238061e76d53SKan Liang 				x86_get_pmu(smp_processor_id())->capabilities |= PERF_PMU_CAP_AUX_OUTPUT;
238142880f72SAlexander Shishkin 			}
238242880f72SAlexander Shishkin 
2383c22497f5SKan Liang 			break;
2384c22497f5SKan Liang 
23857010d129SBorislav Petkov 		default:
23867010d129SBorislav Petkov 			pr_cont("no PEBS fmt%d%c, ", format, pebs_type);
23877010d129SBorislav Petkov 			x86_pmu.pebs = 0;
23887010d129SBorislav Petkov 		}
23897010d129SBorislav Petkov 	}
23907010d129SBorislav Petkov }
23917010d129SBorislav Petkov 
23927010d129SBorislav Petkov void perf_restore_debug_store(void)
23937010d129SBorislav Petkov {
23947010d129SBorislav Petkov 	struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
23957010d129SBorislav Petkov 
23967010d129SBorislav Petkov 	if (!x86_pmu.bts && !x86_pmu.pebs)
23977010d129SBorislav Petkov 		return;
23987010d129SBorislav Petkov 
23997010d129SBorislav Petkov 	wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds);
24007010d129SBorislav Petkov }
2401