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> 107010d129SBorislav Petkov 1127f6d22bSBorislav Petkov #include "../perf_event.h" 127010d129SBorislav Petkov 1310043e02SThomas Gleixner /* Waste a full page so it can be mapped into the cpu_entry_area */ 1410043e02SThomas Gleixner DEFINE_PER_CPU_PAGE_ALIGNED(struct debug_store, cpu_debug_store); 1510043e02SThomas Gleixner 167010d129SBorislav Petkov /* The size of a BTS record in bytes: */ 177010d129SBorislav Petkov #define BTS_RECORD_SIZE 24 187010d129SBorislav Petkov 197010d129SBorislav Petkov #define PEBS_FIXUP_SIZE PAGE_SIZE 207010d129SBorislav Petkov 217010d129SBorislav Petkov /* 227010d129SBorislav Petkov * pebs_record_32 for p4 and core not supported 237010d129SBorislav Petkov 247010d129SBorislav Petkov struct pebs_record_32 { 257010d129SBorislav Petkov u32 flags, ip; 267010d129SBorislav Petkov u32 ax, bc, cx, dx; 277010d129SBorislav Petkov u32 si, di, bp, sp; 287010d129SBorislav Petkov }; 297010d129SBorislav Petkov 307010d129SBorislav Petkov */ 317010d129SBorislav Petkov 327010d129SBorislav Petkov union intel_x86_pebs_dse { 337010d129SBorislav Petkov u64 val; 347010d129SBorislav Petkov struct { 357010d129SBorislav Petkov unsigned int ld_dse:4; 367010d129SBorislav Petkov unsigned int ld_stlb_miss:1; 377010d129SBorislav Petkov unsigned int ld_locked:1; 387010d129SBorislav Petkov unsigned int ld_reserved:26; 397010d129SBorislav Petkov }; 407010d129SBorislav Petkov struct { 417010d129SBorislav Petkov unsigned int st_l1d_hit:1; 427010d129SBorislav Petkov unsigned int st_reserved1:3; 437010d129SBorislav Petkov unsigned int st_stlb_miss:1; 447010d129SBorislav Petkov unsigned int st_locked:1; 457010d129SBorislav Petkov unsigned int st_reserved2:26; 467010d129SBorislav Petkov }; 477010d129SBorislav Petkov }; 487010d129SBorislav Petkov 497010d129SBorislav Petkov 507010d129SBorislav Petkov /* 517010d129SBorislav Petkov * Map PEBS Load Latency Data Source encodings to generic 527010d129SBorislav Petkov * memory data source information 537010d129SBorislav Petkov */ 547010d129SBorislav Petkov #define P(a, b) PERF_MEM_S(a, b) 557010d129SBorislav Petkov #define OP_LH (P(OP, LOAD) | P(LVL, HIT)) 566ae5fa61SAndi Kleen #define LEVEL(x) P(LVLNUM, x) 576ae5fa61SAndi Kleen #define REM P(REMOTE, REMOTE) 587010d129SBorislav Petkov #define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS)) 597010d129SBorislav Petkov 60e17dc653SAndi Kleen /* Version for Sandy Bridge and later */ 61e17dc653SAndi Kleen static u64 pebs_data_source[] = { 626ae5fa61SAndi Kleen P(OP, LOAD) | P(LVL, MISS) | LEVEL(L3) | P(SNOOP, NA),/* 0x00:ukn L3 */ 636ae5fa61SAndi Kleen OP_LH | P(LVL, L1) | LEVEL(L1) | P(SNOOP, NONE), /* 0x01: L1 local */ 646ae5fa61SAndi Kleen OP_LH | P(LVL, LFB) | LEVEL(LFB) | P(SNOOP, NONE), /* 0x02: LFB hit */ 656ae5fa61SAndi Kleen OP_LH | P(LVL, L2) | LEVEL(L2) | P(SNOOP, NONE), /* 0x03: L2 hit */ 666ae5fa61SAndi Kleen OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, NONE), /* 0x04: L3 hit */ 676ae5fa61SAndi Kleen OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, MISS), /* 0x05: L3 hit, snoop miss */ 686ae5fa61SAndi Kleen OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT), /* 0x06: L3 hit, snoop hit */ 696ae5fa61SAndi Kleen OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM), /* 0x07: L3 hit, snoop hitm */ 706ae5fa61SAndi Kleen OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HIT), /* 0x08: L3 miss snoop hit */ 716ae5fa61SAndi Kleen OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HITM), /* 0x09: L3 miss snoop hitm*/ 726ae5fa61SAndi Kleen OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | P(SNOOP, HIT), /* 0x0a: L3 miss, shared */ 736ae5fa61SAndi Kleen OP_LH | P(LVL, REM_RAM1) | REM | LEVEL(L3) | P(SNOOP, HIT), /* 0x0b: L3 miss, shared */ 746ae5fa61SAndi Kleen OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | SNOOP_NONE_MISS, /* 0x0c: L3 miss, excl */ 756ae5fa61SAndi Kleen OP_LH | P(LVL, REM_RAM1) | LEVEL(RAM) | REM | SNOOP_NONE_MISS, /* 0x0d: L3 miss, excl */ 766ae5fa61SAndi Kleen OP_LH | P(LVL, IO) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0e: I/O */ 776ae5fa61SAndi Kleen OP_LH | P(LVL, UNC) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0f: uncached */ 787010d129SBorislav Petkov }; 797010d129SBorislav Petkov 80e17dc653SAndi Kleen /* Patch up minor differences in the bits */ 81e17dc653SAndi Kleen void __init intel_pmu_pebs_data_source_nhm(void) 82e17dc653SAndi Kleen { 836ae5fa61SAndi Kleen pebs_data_source[0x05] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT); 846ae5fa61SAndi Kleen pebs_data_source[0x06] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM); 856ae5fa61SAndi Kleen pebs_data_source[0x07] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM); 866ae5fa61SAndi Kleen } 876ae5fa61SAndi Kleen 886ae5fa61SAndi Kleen void __init intel_pmu_pebs_data_source_skl(bool pmem) 896ae5fa61SAndi Kleen { 906ae5fa61SAndi Kleen u64 pmem_or_l4 = pmem ? LEVEL(PMEM) : LEVEL(L4); 916ae5fa61SAndi Kleen 926ae5fa61SAndi Kleen pebs_data_source[0x08] = OP_LH | pmem_or_l4 | P(SNOOP, HIT); 936ae5fa61SAndi Kleen pebs_data_source[0x09] = OP_LH | pmem_or_l4 | REM | P(SNOOP, HIT); 946ae5fa61SAndi Kleen pebs_data_source[0x0b] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, NONE); 956ae5fa61SAndi Kleen pebs_data_source[0x0c] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOPX, FWD); 966ae5fa61SAndi Kleen pebs_data_source[0x0d] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOP, HITM); 97e17dc653SAndi Kleen } 98e17dc653SAndi Kleen 997010d129SBorislav Petkov static u64 precise_store_data(u64 status) 1007010d129SBorislav Petkov { 1017010d129SBorislav Petkov union intel_x86_pebs_dse dse; 1027010d129SBorislav Petkov u64 val = P(OP, STORE) | P(SNOOP, NA) | P(LVL, L1) | P(TLB, L2); 1037010d129SBorislav Petkov 1047010d129SBorislav Petkov dse.val = status; 1057010d129SBorislav Petkov 1067010d129SBorislav Petkov /* 1077010d129SBorislav Petkov * bit 4: TLB access 1087010d129SBorislav Petkov * 1 = stored missed 2nd level TLB 1097010d129SBorislav Petkov * 1107010d129SBorislav Petkov * so it either hit the walker or the OS 1117010d129SBorislav Petkov * otherwise hit 2nd level TLB 1127010d129SBorislav Petkov */ 1137010d129SBorislav Petkov if (dse.st_stlb_miss) 1147010d129SBorislav Petkov val |= P(TLB, MISS); 1157010d129SBorislav Petkov else 1167010d129SBorislav Petkov val |= P(TLB, HIT); 1177010d129SBorislav Petkov 1187010d129SBorislav Petkov /* 1197010d129SBorislav Petkov * bit 0: hit L1 data cache 1207010d129SBorislav Petkov * if not set, then all we know is that 1217010d129SBorislav Petkov * it missed L1D 1227010d129SBorislav Petkov */ 1237010d129SBorislav Petkov if (dse.st_l1d_hit) 1247010d129SBorislav Petkov val |= P(LVL, HIT); 1257010d129SBorislav Petkov else 1267010d129SBorislav Petkov val |= P(LVL, MISS); 1277010d129SBorislav Petkov 1287010d129SBorislav Petkov /* 1297010d129SBorislav Petkov * bit 5: Locked prefix 1307010d129SBorislav Petkov */ 1317010d129SBorislav Petkov if (dse.st_locked) 1327010d129SBorislav Petkov val |= P(LOCK, LOCKED); 1337010d129SBorislav Petkov 1347010d129SBorislav Petkov return val; 1357010d129SBorislav Petkov } 1367010d129SBorislav Petkov 1377010d129SBorislav Petkov static u64 precise_datala_hsw(struct perf_event *event, u64 status) 1387010d129SBorislav Petkov { 1397010d129SBorislav Petkov union perf_mem_data_src dse; 1407010d129SBorislav Petkov 1417010d129SBorislav Petkov dse.val = PERF_MEM_NA; 1427010d129SBorislav Petkov 1437010d129SBorislav Petkov if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) 1447010d129SBorislav Petkov dse.mem_op = PERF_MEM_OP_STORE; 1457010d129SBorislav Petkov else if (event->hw.flags & PERF_X86_EVENT_PEBS_LD_HSW) 1467010d129SBorislav Petkov dse.mem_op = PERF_MEM_OP_LOAD; 1477010d129SBorislav Petkov 1487010d129SBorislav Petkov /* 1497010d129SBorislav Petkov * L1 info only valid for following events: 1507010d129SBorislav Petkov * 1517010d129SBorislav Petkov * MEM_UOPS_RETIRED.STLB_MISS_STORES 1527010d129SBorislav Petkov * MEM_UOPS_RETIRED.LOCK_STORES 1537010d129SBorislav Petkov * MEM_UOPS_RETIRED.SPLIT_STORES 1547010d129SBorislav Petkov * MEM_UOPS_RETIRED.ALL_STORES 1557010d129SBorislav Petkov */ 1567010d129SBorislav Petkov if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) { 1577010d129SBorislav Petkov if (status & 1) 1587010d129SBorislav Petkov dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_HIT; 1597010d129SBorislav Petkov else 1607010d129SBorislav Petkov dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_MISS; 1617010d129SBorislav Petkov } 1627010d129SBorislav Petkov return dse.val; 1637010d129SBorislav Petkov } 1647010d129SBorislav Petkov 1657010d129SBorislav Petkov static u64 load_latency_data(u64 status) 1667010d129SBorislav Petkov { 1677010d129SBorislav Petkov union intel_x86_pebs_dse dse; 1687010d129SBorislav Petkov u64 val; 1697010d129SBorislav Petkov 1707010d129SBorislav Petkov dse.val = status; 1717010d129SBorislav Petkov 1727010d129SBorislav Petkov /* 1737010d129SBorislav Petkov * use the mapping table for bit 0-3 1747010d129SBorislav Petkov */ 1757010d129SBorislav Petkov val = pebs_data_source[dse.ld_dse]; 1767010d129SBorislav Petkov 1777010d129SBorislav Petkov /* 1787010d129SBorislav Petkov * Nehalem models do not support TLB, Lock infos 1797010d129SBorislav Petkov */ 18095298355SAndi Kleen if (x86_pmu.pebs_no_tlb) { 1817010d129SBorislav Petkov val |= P(TLB, NA) | P(LOCK, NA); 1827010d129SBorislav Petkov return val; 1837010d129SBorislav Petkov } 1847010d129SBorislav Petkov /* 1857010d129SBorislav Petkov * bit 4: TLB access 1867010d129SBorislav Petkov * 0 = did not miss 2nd level TLB 1877010d129SBorislav Petkov * 1 = missed 2nd level TLB 1887010d129SBorislav Petkov */ 1897010d129SBorislav Petkov if (dse.ld_stlb_miss) 1907010d129SBorislav Petkov val |= P(TLB, MISS) | P(TLB, L2); 1917010d129SBorislav Petkov else 1927010d129SBorislav Petkov val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2); 1937010d129SBorislav Petkov 1947010d129SBorislav Petkov /* 1957010d129SBorislav Petkov * bit 5: locked prefix 1967010d129SBorislav Petkov */ 1977010d129SBorislav Petkov if (dse.ld_locked) 1987010d129SBorislav Petkov val |= P(LOCK, LOCKED); 1997010d129SBorislav Petkov 2007010d129SBorislav Petkov return val; 2017010d129SBorislav Petkov } 2027010d129SBorislav Petkov 2037010d129SBorislav Petkov struct pebs_record_core { 2047010d129SBorislav Petkov u64 flags, ip; 2057010d129SBorislav Petkov u64 ax, bx, cx, dx; 2067010d129SBorislav Petkov u64 si, di, bp, sp; 2077010d129SBorislav Petkov u64 r8, r9, r10, r11; 2087010d129SBorislav Petkov u64 r12, r13, r14, r15; 2097010d129SBorislav Petkov }; 2107010d129SBorislav Petkov 2117010d129SBorislav Petkov struct pebs_record_nhm { 2127010d129SBorislav Petkov u64 flags, ip; 2137010d129SBorislav Petkov u64 ax, bx, cx, dx; 2147010d129SBorislav Petkov u64 si, di, bp, sp; 2157010d129SBorislav Petkov u64 r8, r9, r10, r11; 2167010d129SBorislav Petkov u64 r12, r13, r14, r15; 2177010d129SBorislav Petkov u64 status, dla, dse, lat; 2187010d129SBorislav Petkov }; 2197010d129SBorislav Petkov 2207010d129SBorislav Petkov /* 2217010d129SBorislav Petkov * Same as pebs_record_nhm, with two additional fields. 2227010d129SBorislav Petkov */ 2237010d129SBorislav Petkov struct pebs_record_hsw { 2247010d129SBorislav Petkov u64 flags, ip; 2257010d129SBorislav Petkov u64 ax, bx, cx, dx; 2267010d129SBorislav Petkov u64 si, di, bp, sp; 2277010d129SBorislav Petkov u64 r8, r9, r10, r11; 2287010d129SBorislav Petkov u64 r12, r13, r14, r15; 2297010d129SBorislav Petkov u64 status, dla, dse, lat; 2307010d129SBorislav Petkov u64 real_ip, tsx_tuning; 2317010d129SBorislav Petkov }; 2327010d129SBorislav Petkov 2337010d129SBorislav Petkov union hsw_tsx_tuning { 2347010d129SBorislav Petkov struct { 2357010d129SBorislav Petkov u32 cycles_last_block : 32, 2367010d129SBorislav Petkov hle_abort : 1, 2377010d129SBorislav Petkov rtm_abort : 1, 2387010d129SBorislav Petkov instruction_abort : 1, 2397010d129SBorislav Petkov non_instruction_abort : 1, 2407010d129SBorislav Petkov retry : 1, 2417010d129SBorislav Petkov data_conflict : 1, 2427010d129SBorislav Petkov capacity_writes : 1, 2437010d129SBorislav Petkov capacity_reads : 1; 2447010d129SBorislav Petkov }; 2457010d129SBorislav Petkov u64 value; 2467010d129SBorislav Petkov }; 2477010d129SBorislav Petkov 2487010d129SBorislav Petkov #define PEBS_HSW_TSX_FLAGS 0xff00000000ULL 2497010d129SBorislav Petkov 2507010d129SBorislav Petkov /* Same as HSW, plus TSC */ 2517010d129SBorislav Petkov 2527010d129SBorislav Petkov struct pebs_record_skl { 2537010d129SBorislav Petkov u64 flags, ip; 2547010d129SBorislav Petkov u64 ax, bx, cx, dx; 2557010d129SBorislav Petkov u64 si, di, bp, sp; 2567010d129SBorislav Petkov u64 r8, r9, r10, r11; 2577010d129SBorislav Petkov u64 r12, r13, r14, r15; 2587010d129SBorislav Petkov u64 status, dla, dse, lat; 2597010d129SBorislav Petkov u64 real_ip, tsx_tuning; 2607010d129SBorislav Petkov u64 tsc; 2617010d129SBorislav Petkov }; 2627010d129SBorislav Petkov 2637010d129SBorislav Petkov void init_debug_store_on_cpu(int cpu) 2647010d129SBorislav Petkov { 2657010d129SBorislav Petkov struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; 2667010d129SBorislav Petkov 2677010d129SBorislav Petkov if (!ds) 2687010d129SBorislav Petkov return; 2697010d129SBorislav Petkov 2707010d129SBorislav Petkov wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 2717010d129SBorislav Petkov (u32)((u64)(unsigned long)ds), 2727010d129SBorislav Petkov (u32)((u64)(unsigned long)ds >> 32)); 2737010d129SBorislav Petkov } 2747010d129SBorislav Petkov 2757010d129SBorislav Petkov void fini_debug_store_on_cpu(int cpu) 2767010d129SBorislav Petkov { 2777010d129SBorislav Petkov if (!per_cpu(cpu_hw_events, cpu).ds) 2787010d129SBorislav Petkov return; 2797010d129SBorislav Petkov 2807010d129SBorislav Petkov wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0); 2817010d129SBorislav Petkov } 2827010d129SBorislav Petkov 2837010d129SBorislav Petkov static DEFINE_PER_CPU(void *, insn_buffer); 2847010d129SBorislav Petkov 285c1961a46SHugh Dickins static void ds_update_cea(void *cea, void *addr, size_t size, pgprot_t prot) 286c1961a46SHugh Dickins { 28742f3bdc5SPeter Zijlstra unsigned long start = (unsigned long)cea; 288c1961a46SHugh Dickins phys_addr_t pa; 289c1961a46SHugh Dickins size_t msz = 0; 290c1961a46SHugh Dickins 291c1961a46SHugh Dickins pa = virt_to_phys(addr); 29242f3bdc5SPeter Zijlstra 29342f3bdc5SPeter Zijlstra preempt_disable(); 294c1961a46SHugh Dickins for (; msz < size; msz += PAGE_SIZE, pa += PAGE_SIZE, cea += PAGE_SIZE) 295c1961a46SHugh Dickins cea_set_pte(cea, pa, prot); 29642f3bdc5SPeter Zijlstra 29742f3bdc5SPeter Zijlstra /* 29842f3bdc5SPeter Zijlstra * This is a cross-CPU update of the cpu_entry_area, we must shoot down 29942f3bdc5SPeter Zijlstra * all TLB entries for it. 30042f3bdc5SPeter Zijlstra */ 30142f3bdc5SPeter Zijlstra flush_tlb_kernel_range(start, start + size); 30242f3bdc5SPeter Zijlstra preempt_enable(); 303c1961a46SHugh Dickins } 304c1961a46SHugh Dickins 305c1961a46SHugh Dickins static void ds_clear_cea(void *cea, size_t size) 306c1961a46SHugh Dickins { 30742f3bdc5SPeter Zijlstra unsigned long start = (unsigned long)cea; 308c1961a46SHugh Dickins size_t msz = 0; 309c1961a46SHugh Dickins 31042f3bdc5SPeter Zijlstra preempt_disable(); 311c1961a46SHugh Dickins for (; msz < size; msz += PAGE_SIZE, cea += PAGE_SIZE) 312c1961a46SHugh Dickins cea_set_pte(cea, 0, PAGE_NONE); 31342f3bdc5SPeter Zijlstra 31442f3bdc5SPeter Zijlstra flush_tlb_kernel_range(start, start + size); 31542f3bdc5SPeter Zijlstra preempt_enable(); 316c1961a46SHugh Dickins } 317c1961a46SHugh Dickins 318c1961a46SHugh Dickins static void *dsalloc_pages(size_t size, gfp_t flags, int cpu) 319c1961a46SHugh Dickins { 320c1961a46SHugh Dickins unsigned int order = get_order(size); 321c1961a46SHugh Dickins int node = cpu_to_node(cpu); 322c1961a46SHugh Dickins struct page *page; 323c1961a46SHugh Dickins 324c1961a46SHugh Dickins page = __alloc_pages_node(node, flags | __GFP_ZERO, order); 325c1961a46SHugh Dickins return page ? page_address(page) : NULL; 326c1961a46SHugh Dickins } 327c1961a46SHugh Dickins 328c1961a46SHugh Dickins static void dsfree_pages(const void *buffer, size_t size) 329c1961a46SHugh Dickins { 330c1961a46SHugh Dickins if (buffer) 331c1961a46SHugh Dickins free_pages((unsigned long)buffer, get_order(size)); 332c1961a46SHugh Dickins } 333c1961a46SHugh Dickins 3347010d129SBorislav Petkov static int alloc_pebs_buffer(int cpu) 3357010d129SBorislav Petkov { 336c1961a46SHugh Dickins struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu); 337c1961a46SHugh Dickins struct debug_store *ds = hwev->ds; 338c1961a46SHugh Dickins size_t bsiz = x86_pmu.pebs_buffer_size; 339c1961a46SHugh Dickins int max, node = cpu_to_node(cpu); 340c1961a46SHugh Dickins void *buffer, *ibuffer, *cea; 3417010d129SBorislav Petkov 3427010d129SBorislav Petkov if (!x86_pmu.pebs) 3437010d129SBorislav Petkov return 0; 3447010d129SBorislav Petkov 345c1961a46SHugh Dickins buffer = dsalloc_pages(bsiz, GFP_KERNEL, cpu); 3467010d129SBorislav Petkov if (unlikely(!buffer)) 3477010d129SBorislav Petkov return -ENOMEM; 3487010d129SBorislav Petkov 3497010d129SBorislav Petkov /* 3507010d129SBorislav Petkov * HSW+ already provides us the eventing ip; no need to allocate this 3517010d129SBorislav Petkov * buffer then. 3527010d129SBorislav Petkov */ 3537010d129SBorislav Petkov if (x86_pmu.intel_cap.pebs_format < 2) { 3547010d129SBorislav Petkov ibuffer = kzalloc_node(PEBS_FIXUP_SIZE, GFP_KERNEL, node); 3557010d129SBorislav Petkov if (!ibuffer) { 356c1961a46SHugh Dickins dsfree_pages(buffer, bsiz); 3577010d129SBorislav Petkov return -ENOMEM; 3587010d129SBorislav Petkov } 3597010d129SBorislav Petkov per_cpu(insn_buffer, cpu) = ibuffer; 3607010d129SBorislav Petkov } 361c1961a46SHugh Dickins hwev->ds_pebs_vaddr = buffer; 362c1961a46SHugh Dickins /* Update the cpu entry area mapping */ 363c1961a46SHugh Dickins cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer; 364c1961a46SHugh Dickins ds->pebs_buffer_base = (unsigned long) cea; 365c1961a46SHugh Dickins ds_update_cea(cea, buffer, bsiz, PAGE_KERNEL); 3667010d129SBorislav Petkov ds->pebs_index = ds->pebs_buffer_base; 367c1961a46SHugh Dickins max = x86_pmu.pebs_record_size * (bsiz / x86_pmu.pebs_record_size); 368c1961a46SHugh Dickins ds->pebs_absolute_maximum = ds->pebs_buffer_base + max; 3697010d129SBorislav Petkov return 0; 3707010d129SBorislav Petkov } 3717010d129SBorislav Petkov 3727010d129SBorislav Petkov static void release_pebs_buffer(int cpu) 3737010d129SBorislav Petkov { 374c1961a46SHugh Dickins struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu); 375c1961a46SHugh Dickins void *cea; 3767010d129SBorislav Petkov 377efe951d3SPeter Zijlstra if (!x86_pmu.pebs) 3787010d129SBorislav Petkov return; 3797010d129SBorislav Petkov 3807010d129SBorislav Petkov kfree(per_cpu(insn_buffer, cpu)); 3817010d129SBorislav Petkov per_cpu(insn_buffer, cpu) = NULL; 3827010d129SBorislav Petkov 383c1961a46SHugh Dickins /* Clear the fixmap */ 384c1961a46SHugh Dickins cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer; 385c1961a46SHugh Dickins ds_clear_cea(cea, x86_pmu.pebs_buffer_size); 386c1961a46SHugh Dickins dsfree_pages(hwev->ds_pebs_vaddr, x86_pmu.pebs_buffer_size); 387c1961a46SHugh Dickins hwev->ds_pebs_vaddr = NULL; 3887010d129SBorislav Petkov } 3897010d129SBorislav Petkov 3907010d129SBorislav Petkov static int alloc_bts_buffer(int cpu) 3917010d129SBorislav Petkov { 392c1961a46SHugh Dickins struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu); 393c1961a46SHugh Dickins struct debug_store *ds = hwev->ds; 394c1961a46SHugh Dickins void *buffer, *cea; 395c1961a46SHugh Dickins int max; 3967010d129SBorislav Petkov 3977010d129SBorislav Petkov if (!x86_pmu.bts) 3987010d129SBorislav Petkov return 0; 3997010d129SBorislav Petkov 400c1961a46SHugh Dickins buffer = dsalloc_pages(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, cpu); 4017010d129SBorislav Petkov if (unlikely(!buffer)) { 4027010d129SBorislav Petkov WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__); 4037010d129SBorislav Petkov return -ENOMEM; 4047010d129SBorislav Petkov } 405c1961a46SHugh Dickins hwev->ds_bts_vaddr = buffer; 406c1961a46SHugh Dickins /* Update the fixmap */ 407c1961a46SHugh Dickins cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer; 408c1961a46SHugh Dickins ds->bts_buffer_base = (unsigned long) cea; 409c1961a46SHugh Dickins ds_update_cea(cea, buffer, BTS_BUFFER_SIZE, PAGE_KERNEL); 4107010d129SBorislav Petkov ds->bts_index = ds->bts_buffer_base; 4112c991e40SHugh Dickins max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE; 4122c991e40SHugh Dickins ds->bts_absolute_maximum = ds->bts_buffer_base + 4132c991e40SHugh Dickins max * BTS_RECORD_SIZE; 4142c991e40SHugh Dickins ds->bts_interrupt_threshold = ds->bts_absolute_maximum - 4152c991e40SHugh Dickins (max / 16) * BTS_RECORD_SIZE; 4167010d129SBorislav Petkov return 0; 4177010d129SBorislav Petkov } 4187010d129SBorislav Petkov 4197010d129SBorislav Petkov static void release_bts_buffer(int cpu) 4207010d129SBorislav Petkov { 421c1961a46SHugh Dickins struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu); 422c1961a46SHugh Dickins void *cea; 4237010d129SBorislav Petkov 424efe951d3SPeter Zijlstra if (!x86_pmu.bts) 4257010d129SBorislav Petkov return; 4267010d129SBorislav Petkov 427c1961a46SHugh Dickins /* Clear the fixmap */ 428c1961a46SHugh Dickins cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer; 429c1961a46SHugh Dickins ds_clear_cea(cea, BTS_BUFFER_SIZE); 430c1961a46SHugh Dickins dsfree_pages(hwev->ds_bts_vaddr, BTS_BUFFER_SIZE); 431c1961a46SHugh Dickins hwev->ds_bts_vaddr = NULL; 4327010d129SBorislav Petkov } 4337010d129SBorislav Petkov 4347010d129SBorislav Petkov static int alloc_ds_buffer(int cpu) 4357010d129SBorislav Petkov { 436c1961a46SHugh Dickins struct debug_store *ds = &get_cpu_entry_area(cpu)->cpu_debug_store; 4377010d129SBorislav Petkov 438c1961a46SHugh Dickins memset(ds, 0, sizeof(*ds)); 4397010d129SBorislav Petkov per_cpu(cpu_hw_events, cpu).ds = ds; 4407010d129SBorislav Petkov return 0; 4417010d129SBorislav Petkov } 4427010d129SBorislav Petkov 4437010d129SBorislav Petkov static void release_ds_buffer(int cpu) 4447010d129SBorislav Petkov { 4457010d129SBorislav Petkov per_cpu(cpu_hw_events, cpu).ds = NULL; 4467010d129SBorislav Petkov } 4477010d129SBorislav Petkov 4487010d129SBorislav Petkov void release_ds_buffers(void) 4497010d129SBorislav Petkov { 4507010d129SBorislav Petkov int cpu; 4517010d129SBorislav Petkov 4527010d129SBorislav Petkov if (!x86_pmu.bts && !x86_pmu.pebs) 4537010d129SBorislav Petkov return; 4547010d129SBorislav Petkov 455efe951d3SPeter Zijlstra for_each_possible_cpu(cpu) 456efe951d3SPeter Zijlstra release_ds_buffer(cpu); 457efe951d3SPeter Zijlstra 458efe951d3SPeter Zijlstra for_each_possible_cpu(cpu) { 459efe951d3SPeter Zijlstra /* 460efe951d3SPeter Zijlstra * Again, ignore errors from offline CPUs, they will no longer 461efe951d3SPeter Zijlstra * observe cpu_hw_events.ds and not program the DS_AREA when 462efe951d3SPeter Zijlstra * they come up. 463efe951d3SPeter Zijlstra */ 4647010d129SBorislav Petkov fini_debug_store_on_cpu(cpu); 465efe951d3SPeter Zijlstra } 4667010d129SBorislav Petkov 4677010d129SBorislav Petkov for_each_possible_cpu(cpu) { 4687010d129SBorislav Petkov release_pebs_buffer(cpu); 4697010d129SBorislav Petkov release_bts_buffer(cpu); 4707010d129SBorislav Petkov } 4717010d129SBorislav Petkov } 4727010d129SBorislav Petkov 4737010d129SBorislav Petkov void reserve_ds_buffers(void) 4747010d129SBorislav Petkov { 4757010d129SBorislav Petkov int bts_err = 0, pebs_err = 0; 4767010d129SBorislav Petkov int cpu; 4777010d129SBorislav Petkov 4787010d129SBorislav Petkov x86_pmu.bts_active = 0; 4797010d129SBorislav Petkov x86_pmu.pebs_active = 0; 4807010d129SBorislav Petkov 4817010d129SBorislav Petkov if (!x86_pmu.bts && !x86_pmu.pebs) 4827010d129SBorislav Petkov return; 4837010d129SBorislav Petkov 4847010d129SBorislav Petkov if (!x86_pmu.bts) 4857010d129SBorislav Petkov bts_err = 1; 4867010d129SBorislav Petkov 4877010d129SBorislav Petkov if (!x86_pmu.pebs) 4887010d129SBorislav Petkov pebs_err = 1; 4897010d129SBorislav Petkov 4907010d129SBorislav Petkov for_each_possible_cpu(cpu) { 4917010d129SBorislav Petkov if (alloc_ds_buffer(cpu)) { 4927010d129SBorislav Petkov bts_err = 1; 4937010d129SBorislav Petkov pebs_err = 1; 4947010d129SBorislav Petkov } 4957010d129SBorislav Petkov 4967010d129SBorislav Petkov if (!bts_err && alloc_bts_buffer(cpu)) 4977010d129SBorislav Petkov bts_err = 1; 4987010d129SBorislav Petkov 4997010d129SBorislav Petkov if (!pebs_err && alloc_pebs_buffer(cpu)) 5007010d129SBorislav Petkov pebs_err = 1; 5017010d129SBorislav Petkov 5027010d129SBorislav Petkov if (bts_err && pebs_err) 5037010d129SBorislav Petkov break; 5047010d129SBorislav Petkov } 5057010d129SBorislav Petkov 5067010d129SBorislav Petkov if (bts_err) { 5077010d129SBorislav Petkov for_each_possible_cpu(cpu) 5087010d129SBorislav Petkov release_bts_buffer(cpu); 5097010d129SBorislav Petkov } 5107010d129SBorislav Petkov 5117010d129SBorislav Petkov if (pebs_err) { 5127010d129SBorislav Petkov for_each_possible_cpu(cpu) 5137010d129SBorislav Petkov release_pebs_buffer(cpu); 5147010d129SBorislav Petkov } 5157010d129SBorislav Petkov 5167010d129SBorislav Petkov if (bts_err && pebs_err) { 5177010d129SBorislav Petkov for_each_possible_cpu(cpu) 5187010d129SBorislav Petkov release_ds_buffer(cpu); 5197010d129SBorislav Petkov } else { 5207010d129SBorislav Petkov if (x86_pmu.bts && !bts_err) 5217010d129SBorislav Petkov x86_pmu.bts_active = 1; 5227010d129SBorislav Petkov 5237010d129SBorislav Petkov if (x86_pmu.pebs && !pebs_err) 5247010d129SBorislav Petkov x86_pmu.pebs_active = 1; 5257010d129SBorislav Petkov 526efe951d3SPeter Zijlstra for_each_possible_cpu(cpu) { 527efe951d3SPeter Zijlstra /* 528efe951d3SPeter Zijlstra * Ignores wrmsr_on_cpu() errors for offline CPUs they 529efe951d3SPeter Zijlstra * will get this call through intel_pmu_cpu_starting(). 530efe951d3SPeter Zijlstra */ 5317010d129SBorislav Petkov init_debug_store_on_cpu(cpu); 5327010d129SBorislav Petkov } 533efe951d3SPeter Zijlstra } 5347010d129SBorislav Petkov } 5357010d129SBorislav Petkov 5367010d129SBorislav Petkov /* 5377010d129SBorislav Petkov * BTS 5387010d129SBorislav Petkov */ 5397010d129SBorislav Petkov 5407010d129SBorislav Petkov struct event_constraint bts_constraint = 5417010d129SBorislav Petkov EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0); 5427010d129SBorislav Petkov 5437010d129SBorislav Petkov void intel_pmu_enable_bts(u64 config) 5447010d129SBorislav Petkov { 5457010d129SBorislav Petkov unsigned long debugctlmsr; 5467010d129SBorislav Petkov 5477010d129SBorislav Petkov debugctlmsr = get_debugctlmsr(); 5487010d129SBorislav Petkov 5497010d129SBorislav Petkov debugctlmsr |= DEBUGCTLMSR_TR; 5507010d129SBorislav Petkov debugctlmsr |= DEBUGCTLMSR_BTS; 5517010d129SBorislav Petkov if (config & ARCH_PERFMON_EVENTSEL_INT) 5527010d129SBorislav Petkov debugctlmsr |= DEBUGCTLMSR_BTINT; 5537010d129SBorislav Petkov 5547010d129SBorislav Petkov if (!(config & ARCH_PERFMON_EVENTSEL_OS)) 5557010d129SBorislav Petkov debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS; 5567010d129SBorislav Petkov 5577010d129SBorislav Petkov if (!(config & ARCH_PERFMON_EVENTSEL_USR)) 5587010d129SBorislav Petkov debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR; 5597010d129SBorislav Petkov 5607010d129SBorislav Petkov update_debugctlmsr(debugctlmsr); 5617010d129SBorislav Petkov } 5627010d129SBorislav Petkov 5637010d129SBorislav Petkov void intel_pmu_disable_bts(void) 5647010d129SBorislav Petkov { 5657010d129SBorislav Petkov struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 5667010d129SBorislav Petkov unsigned long debugctlmsr; 5677010d129SBorislav Petkov 5687010d129SBorislav Petkov if (!cpuc->ds) 5697010d129SBorislav Petkov return; 5707010d129SBorislav Petkov 5717010d129SBorislav Petkov debugctlmsr = get_debugctlmsr(); 5727010d129SBorislav Petkov 5737010d129SBorislav Petkov debugctlmsr &= 5747010d129SBorislav Petkov ~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT | 5757010d129SBorislav Petkov DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR); 5767010d129SBorislav Petkov 5777010d129SBorislav Petkov update_debugctlmsr(debugctlmsr); 5787010d129SBorislav Petkov } 5797010d129SBorislav Petkov 5807010d129SBorislav Petkov int intel_pmu_drain_bts_buffer(void) 5817010d129SBorislav Petkov { 5827010d129SBorislav Petkov struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 5837010d129SBorislav Petkov struct debug_store *ds = cpuc->ds; 5847010d129SBorislav Petkov struct bts_record { 5857010d129SBorislav Petkov u64 from; 5867010d129SBorislav Petkov u64 to; 5877010d129SBorislav Petkov u64 flags; 5887010d129SBorislav Petkov }; 5897010d129SBorislav Petkov struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS]; 5907010d129SBorislav Petkov struct bts_record *at, *base, *top; 5917010d129SBorislav Petkov struct perf_output_handle handle; 5927010d129SBorislav Petkov struct perf_event_header header; 5937010d129SBorislav Petkov struct perf_sample_data data; 5947010d129SBorislav Petkov unsigned long skip = 0; 5957010d129SBorislav Petkov struct pt_regs regs; 5967010d129SBorislav Petkov 5977010d129SBorislav Petkov if (!event) 5987010d129SBorislav Petkov return 0; 5997010d129SBorislav Petkov 6007010d129SBorislav Petkov if (!x86_pmu.bts_active) 6017010d129SBorislav Petkov return 0; 6027010d129SBorislav Petkov 6037010d129SBorislav Petkov base = (struct bts_record *)(unsigned long)ds->bts_buffer_base; 6047010d129SBorislav Petkov top = (struct bts_record *)(unsigned long)ds->bts_index; 6057010d129SBorislav Petkov 6067010d129SBorislav Petkov if (top <= base) 6077010d129SBorislav Petkov return 0; 6087010d129SBorislav Petkov 6097010d129SBorislav Petkov memset(®s, 0, sizeof(regs)); 6107010d129SBorislav Petkov 6117010d129SBorislav Petkov ds->bts_index = ds->bts_buffer_base; 6127010d129SBorislav Petkov 6137010d129SBorislav Petkov perf_sample_data_init(&data, 0, event->hw.last_period); 6147010d129SBorislav Petkov 6157010d129SBorislav Petkov /* 6167010d129SBorislav Petkov * BTS leaks kernel addresses in branches across the cpl boundary, 6177010d129SBorislav Petkov * such as traps or system calls, so unless the user is asking for 6187010d129SBorislav Petkov * kernel tracing (and right now it's not possible), we'd need to 6197010d129SBorislav Petkov * filter them out. But first we need to count how many of those we 6207010d129SBorislav Petkov * have in the current batch. This is an extra O(n) pass, however, 6217010d129SBorislav Petkov * it's much faster than the other one especially considering that 6227010d129SBorislav Petkov * n <= 2560 (BTS_BUFFER_SIZE / BTS_RECORD_SIZE * 15/16; see the 6237010d129SBorislav Petkov * alloc_bts_buffer()). 6247010d129SBorislav Petkov */ 6257010d129SBorislav Petkov for (at = base; at < top; at++) { 6267010d129SBorislav Petkov /* 6277010d129SBorislav Petkov * Note that right now *this* BTS code only works if 6287010d129SBorislav Petkov * attr::exclude_kernel is set, but let's keep this extra 6297010d129SBorislav Petkov * check here in case that changes. 6307010d129SBorislav Petkov */ 6317010d129SBorislav Petkov if (event->attr.exclude_kernel && 6327010d129SBorislav Petkov (kernel_ip(at->from) || kernel_ip(at->to))) 6337010d129SBorislav Petkov skip++; 6347010d129SBorislav Petkov } 6357010d129SBorislav Petkov 6367010d129SBorislav Petkov /* 6377010d129SBorislav Petkov * Prepare a generic sample, i.e. fill in the invariant fields. 6387010d129SBorislav Petkov * We will overwrite the from and to address before we output 6397010d129SBorislav Petkov * the sample. 6407010d129SBorislav Petkov */ 641e8d8a90fSPeter Zijlstra rcu_read_lock(); 6427010d129SBorislav Petkov perf_prepare_sample(&header, &data, event, ®s); 6437010d129SBorislav Petkov 6447010d129SBorislav Petkov if (perf_output_begin(&handle, event, header.size * 6457010d129SBorislav Petkov (top - base - skip))) 646e8d8a90fSPeter Zijlstra goto unlock; 6477010d129SBorislav Petkov 6487010d129SBorislav Petkov for (at = base; at < top; at++) { 6497010d129SBorislav Petkov /* Filter out any records that contain kernel addresses. */ 6507010d129SBorislav Petkov if (event->attr.exclude_kernel && 6517010d129SBorislav Petkov (kernel_ip(at->from) || kernel_ip(at->to))) 6527010d129SBorislav Petkov continue; 6537010d129SBorislav Petkov 6547010d129SBorislav Petkov data.ip = at->from; 6557010d129SBorislav Petkov data.addr = at->to; 6567010d129SBorislav Petkov 6577010d129SBorislav Petkov perf_output_sample(&handle, &header, &data, event); 6587010d129SBorislav Petkov } 6597010d129SBorislav Petkov 6607010d129SBorislav Petkov perf_output_end(&handle); 6617010d129SBorislav Petkov 6627010d129SBorislav Petkov /* There's new data available. */ 6637010d129SBorislav Petkov event->hw.interrupts++; 6647010d129SBorislav Petkov event->pending_kill = POLL_IN; 665e8d8a90fSPeter Zijlstra unlock: 666e8d8a90fSPeter Zijlstra rcu_read_unlock(); 6677010d129SBorislav Petkov return 1; 6687010d129SBorislav Petkov } 6697010d129SBorislav Petkov 6707010d129SBorislav Petkov static inline void intel_pmu_drain_pebs_buffer(void) 6717010d129SBorislav Petkov { 6727010d129SBorislav Petkov struct pt_regs regs; 6737010d129SBorislav Petkov 6747010d129SBorislav Petkov x86_pmu.drain_pebs(®s); 6757010d129SBorislav Petkov } 6767010d129SBorislav Petkov 6777010d129SBorislav Petkov /* 6787010d129SBorislav Petkov * PEBS 6797010d129SBorislav Petkov */ 6807010d129SBorislav Petkov struct event_constraint intel_core2_pebs_event_constraints[] = { 6817010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */ 6827010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */ 6837010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */ 6847010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */ 6857010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */ 6867010d129SBorislav Petkov /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ 6877010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01), 6887010d129SBorislav Petkov EVENT_CONSTRAINT_END 6897010d129SBorislav Petkov }; 6907010d129SBorislav Petkov 6917010d129SBorislav Petkov struct event_constraint intel_atom_pebs_event_constraints[] = { 6927010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */ 6937010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */ 6947010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */ 6957010d129SBorislav Petkov /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ 6967010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01), 6977010d129SBorislav Petkov /* Allow all events as PEBS with no flags */ 6987010d129SBorislav Petkov INTEL_ALL_EVENT_CONSTRAINT(0, 0x1), 6997010d129SBorislav Petkov EVENT_CONSTRAINT_END 7007010d129SBorislav Petkov }; 7017010d129SBorislav Petkov 7027010d129SBorislav Petkov struct event_constraint intel_slm_pebs_event_constraints[] = { 7037010d129SBorislav Petkov /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ 7047010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x1), 7057010d129SBorislav Petkov /* Allow all events as PEBS with no flags */ 7067010d129SBorislav Petkov INTEL_ALL_EVENT_CONSTRAINT(0, 0x1), 7077010d129SBorislav Petkov EVENT_CONSTRAINT_END 7087010d129SBorislav Petkov }; 7097010d129SBorislav Petkov 7108b92c3a7SKan Liang struct event_constraint intel_glm_pebs_event_constraints[] = { 7118b92c3a7SKan Liang /* Allow all events as PEBS with no flags */ 7128b92c3a7SKan Liang INTEL_ALL_EVENT_CONSTRAINT(0, 0x1), 7138b92c3a7SKan Liang EVENT_CONSTRAINT_END 7148b92c3a7SKan Liang }; 7158b92c3a7SKan Liang 716dd0b06b5SKan Liang struct event_constraint intel_glp_pebs_event_constraints[] = { 717dd0b06b5SKan Liang /* Allow all events as PEBS with no flags */ 718dd0b06b5SKan Liang INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), 719dd0b06b5SKan Liang EVENT_CONSTRAINT_END 720dd0b06b5SKan Liang }; 721dd0b06b5SKan Liang 7227010d129SBorislav Petkov struct event_constraint intel_nehalem_pebs_event_constraints[] = { 7237010d129SBorislav Petkov INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */ 7247010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */ 7257010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */ 7267010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INST_RETIRED.ANY */ 7277010d129SBorislav Petkov INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */ 7287010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */ 7297010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */ 7307010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */ 7317010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */ 7327010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */ 7337010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */ 7347010d129SBorislav Petkov /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ 7357010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f), 7367010d129SBorislav Petkov EVENT_CONSTRAINT_END 7377010d129SBorislav Petkov }; 7387010d129SBorislav Petkov 7397010d129SBorislav Petkov struct event_constraint intel_westmere_pebs_event_constraints[] = { 7407010d129SBorislav Petkov INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */ 7417010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */ 7427010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */ 7437010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INSTR_RETIRED.* */ 7447010d129SBorislav Petkov INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */ 7457010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */ 7467010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */ 7477010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */ 7487010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */ 7497010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */ 7507010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */ 7517010d129SBorislav Petkov /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ 7527010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f), 7537010d129SBorislav Petkov EVENT_CONSTRAINT_END 7547010d129SBorislav Petkov }; 7557010d129SBorislav Petkov 7567010d129SBorislav Petkov struct event_constraint intel_snb_pebs_event_constraints[] = { 7577010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */ 7587010d129SBorislav Petkov INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */ 7597010d129SBorislav Petkov INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */ 7607010d129SBorislav Petkov /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ 7617010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf), 7627010d129SBorislav Petkov INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */ 7637010d129SBorislav Petkov INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ 7647010d129SBorislav Petkov INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */ 7657010d129SBorislav Petkov INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */ 7667010d129SBorislav Petkov /* Allow all events as PEBS with no flags */ 7677010d129SBorislav Petkov INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), 7687010d129SBorislav Petkov EVENT_CONSTRAINT_END 7697010d129SBorislav Petkov }; 7707010d129SBorislav Petkov 7717010d129SBorislav Petkov struct event_constraint intel_ivb_pebs_event_constraints[] = { 7727010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */ 7737010d129SBorislav Petkov INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */ 7747010d129SBorislav Petkov INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */ 7757010d129SBorislav Petkov /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ 7767010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf), 7777010d129SBorislav Petkov /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */ 7787010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2), 7797010d129SBorislav Petkov INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */ 7807010d129SBorislav Petkov INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ 7817010d129SBorislav Petkov INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */ 7827010d129SBorislav Petkov INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */ 7837010d129SBorislav Petkov /* Allow all events as PEBS with no flags */ 7847010d129SBorislav Petkov INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), 7857010d129SBorislav Petkov EVENT_CONSTRAINT_END 7867010d129SBorislav Petkov }; 7877010d129SBorislav Petkov 7887010d129SBorislav Petkov struct event_constraint intel_hsw_pebs_event_constraints[] = { 7897010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */ 7907010d129SBorislav Petkov INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */ 7917010d129SBorislav Petkov /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ 7927010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf), 7937010d129SBorislav Petkov /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */ 7947010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2), 7957010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */ 7967010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */ 7977010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */ 7987010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */ 7997010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */ 8007010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */ 8017010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */ 8027010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */ 8037010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ 8047010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */ 8057010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */ 8067010d129SBorislav Petkov /* Allow all events as PEBS with no flags */ 8077010d129SBorislav Petkov INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), 8087010d129SBorislav Petkov EVENT_CONSTRAINT_END 8097010d129SBorislav Petkov }; 8107010d129SBorislav Petkov 811b3e62463SStephane Eranian struct event_constraint intel_bdw_pebs_event_constraints[] = { 812b3e62463SStephane Eranian INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */ 813b3e62463SStephane Eranian INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */ 814b3e62463SStephane Eranian /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ 815b3e62463SStephane Eranian INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf), 816b3e62463SStephane Eranian /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */ 817b3e62463SStephane Eranian INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2), 818b3e62463SStephane Eranian INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */ 819b3e62463SStephane Eranian INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */ 820b3e62463SStephane Eranian INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */ 821b3e62463SStephane Eranian INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */ 822b3e62463SStephane Eranian INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */ 823b3e62463SStephane Eranian INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */ 824b3e62463SStephane Eranian INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */ 825b3e62463SStephane Eranian INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */ 826b3e62463SStephane Eranian INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ 827b3e62463SStephane Eranian INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */ 828b3e62463SStephane Eranian INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */ 829b3e62463SStephane Eranian /* Allow all events as PEBS with no flags */ 830b3e62463SStephane Eranian INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), 831b3e62463SStephane Eranian EVENT_CONSTRAINT_END 832b3e62463SStephane Eranian }; 833b3e62463SStephane Eranian 834b3e62463SStephane Eranian 8357010d129SBorislav Petkov struct event_constraint intel_skl_pebs_event_constraints[] = { 8367010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT(0x1c0, 0x2), /* INST_RETIRED.PREC_DIST */ 8377010d129SBorislav Petkov /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */ 8387010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2), 8397010d129SBorislav Petkov /* INST_RETIRED.TOTAL_CYCLES_PS (inv=1, cmask=16) (cycles:p). */ 8407010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f), 8417010d129SBorislav Petkov INTEL_PLD_CONSTRAINT(0x1cd, 0xf), /* MEM_TRANS_RETIRED.* */ 8427010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */ 8437010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */ 8447010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */ 8457010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x22d0, 0xf), /* MEM_INST_RETIRED.LOCK_STORES */ 8467010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */ 8477010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */ 8487010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */ 8497010d129SBorislav Petkov INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */ 8507010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_RETIRED.* */ 8517010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_L3_HIT_RETIRED.* */ 8527010d129SBorislav Petkov INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_L3_MISS_RETIRED.* */ 8537010d129SBorislav Petkov /* Allow all events as PEBS with no flags */ 8547010d129SBorislav Petkov INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), 8557010d129SBorislav Petkov EVENT_CONSTRAINT_END 8567010d129SBorislav Petkov }; 8577010d129SBorislav Petkov 8587010d129SBorislav Petkov struct event_constraint *intel_pebs_constraints(struct perf_event *event) 8597010d129SBorislav Petkov { 8607010d129SBorislav Petkov struct event_constraint *c; 8617010d129SBorislav Petkov 8627010d129SBorislav Petkov if (!event->attr.precise_ip) 8637010d129SBorislav Petkov return NULL; 8647010d129SBorislav Petkov 8657010d129SBorislav Petkov if (x86_pmu.pebs_constraints) { 8667010d129SBorislav Petkov for_each_event_constraint(c, x86_pmu.pebs_constraints) { 8677010d129SBorislav Petkov if ((event->hw.config & c->cmask) == c->code) { 8687010d129SBorislav Petkov event->hw.flags |= c->flags; 8697010d129SBorislav Petkov return c; 8707010d129SBorislav Petkov } 8717010d129SBorislav Petkov } 8727010d129SBorislav Petkov } 8737010d129SBorislav Petkov 8747010d129SBorislav Petkov return &emptyconstraint; 8757010d129SBorislav Petkov } 8767010d129SBorislav Petkov 87709e61b4fSPeter Zijlstra /* 87809e61b4fSPeter Zijlstra * We need the sched_task callback even for per-cpu events when we use 87909e61b4fSPeter Zijlstra * the large interrupt threshold, such that we can provide PID and TID 88009e61b4fSPeter Zijlstra * to PEBS samples. 88109e61b4fSPeter Zijlstra */ 88209e61b4fSPeter Zijlstra static inline bool pebs_needs_sched_cb(struct cpu_hw_events *cpuc) 8837010d129SBorislav Petkov { 88409e61b4fSPeter Zijlstra return cpuc->n_pebs && (cpuc->n_pebs == cpuc->n_large_pebs); 88509e61b4fSPeter Zijlstra } 88609e61b4fSPeter Zijlstra 887df6c3db8SJiri Olsa void intel_pmu_pebs_sched_task(struct perf_event_context *ctx, bool sched_in) 888df6c3db8SJiri Olsa { 889df6c3db8SJiri Olsa struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 890df6c3db8SJiri Olsa 891df6c3db8SJiri Olsa if (!sched_in && pebs_needs_sched_cb(cpuc)) 892df6c3db8SJiri Olsa intel_pmu_drain_pebs_buffer(); 893df6c3db8SJiri Olsa } 894df6c3db8SJiri Olsa 89509e61b4fSPeter Zijlstra static inline void pebs_update_threshold(struct cpu_hw_events *cpuc) 89609e61b4fSPeter Zijlstra { 89709e61b4fSPeter Zijlstra struct debug_store *ds = cpuc->ds; 89809e61b4fSPeter Zijlstra u64 threshold; 89909e61b4fSPeter Zijlstra 90009e61b4fSPeter Zijlstra if (cpuc->n_pebs == cpuc->n_large_pebs) { 90109e61b4fSPeter Zijlstra threshold = ds->pebs_absolute_maximum - 90209e61b4fSPeter Zijlstra x86_pmu.max_pebs_events * x86_pmu.pebs_record_size; 90309e61b4fSPeter Zijlstra } else { 90409e61b4fSPeter Zijlstra threshold = ds->pebs_buffer_base + x86_pmu.pebs_record_size; 90509e61b4fSPeter Zijlstra } 90609e61b4fSPeter Zijlstra 90709e61b4fSPeter Zijlstra ds->pebs_interrupt_threshold = threshold; 90809e61b4fSPeter Zijlstra } 90909e61b4fSPeter Zijlstra 91009e61b4fSPeter Zijlstra static void 91109e61b4fSPeter Zijlstra pebs_update_state(bool needed_cb, struct cpu_hw_events *cpuc, struct pmu *pmu) 91209e61b4fSPeter Zijlstra { 913b6a32f02SJiri Olsa /* 914b6a32f02SJiri Olsa * Make sure we get updated with the first PEBS 915b6a32f02SJiri Olsa * event. It will trigger also during removal, but 916b6a32f02SJiri Olsa * that does not hurt: 917b6a32f02SJiri Olsa */ 918b6a32f02SJiri Olsa bool update = cpuc->n_pebs == 1; 919b6a32f02SJiri Olsa 92009e61b4fSPeter Zijlstra if (needed_cb != pebs_needs_sched_cb(cpuc)) { 92109e61b4fSPeter Zijlstra if (!needed_cb) 92209e61b4fSPeter Zijlstra perf_sched_cb_inc(pmu); 92309e61b4fSPeter Zijlstra else 92409e61b4fSPeter Zijlstra perf_sched_cb_dec(pmu); 92509e61b4fSPeter Zijlstra 926b6a32f02SJiri Olsa update = true; 92709e61b4fSPeter Zijlstra } 928b6a32f02SJiri Olsa 929b6a32f02SJiri Olsa if (update) 930b6a32f02SJiri Olsa pebs_update_threshold(cpuc); 93109e61b4fSPeter Zijlstra } 93209e61b4fSPeter Zijlstra 93368f7082fSPeter Zijlstra void intel_pmu_pebs_add(struct perf_event *event) 93409e61b4fSPeter Zijlstra { 93509e61b4fSPeter Zijlstra struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 93609e61b4fSPeter Zijlstra struct hw_perf_event *hwc = &event->hw; 93709e61b4fSPeter Zijlstra bool needed_cb = pebs_needs_sched_cb(cpuc); 93809e61b4fSPeter Zijlstra 93909e61b4fSPeter Zijlstra cpuc->n_pebs++; 940174afc3eSKan Liang if (hwc->flags & PERF_X86_EVENT_LARGE_PEBS) 94109e61b4fSPeter Zijlstra cpuc->n_large_pebs++; 94209e61b4fSPeter Zijlstra 94309e61b4fSPeter Zijlstra pebs_update_state(needed_cb, cpuc, event->ctx->pmu); 9447010d129SBorislav Petkov } 9457010d129SBorislav Petkov 9467010d129SBorislav Petkov void intel_pmu_pebs_enable(struct perf_event *event) 9477010d129SBorislav Petkov { 9487010d129SBorislav Petkov struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 9497010d129SBorislav Petkov struct hw_perf_event *hwc = &event->hw; 9507010d129SBorislav Petkov struct debug_store *ds = cpuc->ds; 95109e61b4fSPeter Zijlstra 9527010d129SBorislav Petkov hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT; 9537010d129SBorislav Petkov 9547010d129SBorislav Petkov cpuc->pebs_enabled |= 1ULL << hwc->idx; 9557010d129SBorislav Petkov 9567010d129SBorislav Petkov if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT) 9577010d129SBorislav Petkov cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32); 9587010d129SBorislav Petkov else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST) 9597010d129SBorislav Petkov cpuc->pebs_enabled |= 1ULL << 63; 9607010d129SBorislav Petkov 9617010d129SBorislav Petkov /* 96209e61b4fSPeter Zijlstra * Use auto-reload if possible to save a MSR write in the PMI. 96309e61b4fSPeter Zijlstra * This must be done in pmu::start(), because PERF_EVENT_IOC_PERIOD. 9647010d129SBorislav Petkov */ 9657010d129SBorislav Petkov if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) { 9667010d129SBorislav Petkov ds->pebs_event_reset[hwc->idx] = 9677010d129SBorislav Petkov (u64)(-hwc->sample_period) & x86_pmu.cntval_mask; 968dc853e26SJiri Olsa } else { 969dc853e26SJiri Olsa ds->pebs_event_reset[hwc->idx] = 0; 9707010d129SBorislav Petkov } 97109e61b4fSPeter Zijlstra } 9727010d129SBorislav Petkov 97368f7082fSPeter Zijlstra void intel_pmu_pebs_del(struct perf_event *event) 97409e61b4fSPeter Zijlstra { 97509e61b4fSPeter Zijlstra struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 97609e61b4fSPeter Zijlstra struct hw_perf_event *hwc = &event->hw; 97709e61b4fSPeter Zijlstra bool needed_cb = pebs_needs_sched_cb(cpuc); 97809e61b4fSPeter Zijlstra 97909e61b4fSPeter Zijlstra cpuc->n_pebs--; 980174afc3eSKan Liang if (hwc->flags & PERF_X86_EVENT_LARGE_PEBS) 98109e61b4fSPeter Zijlstra cpuc->n_large_pebs--; 98209e61b4fSPeter Zijlstra 98309e61b4fSPeter Zijlstra pebs_update_state(needed_cb, cpuc, event->ctx->pmu); 9847010d129SBorislav Petkov } 9857010d129SBorislav Petkov 9867010d129SBorislav Petkov void intel_pmu_pebs_disable(struct perf_event *event) 9877010d129SBorislav Petkov { 9887010d129SBorislav Petkov struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 9897010d129SBorislav Petkov struct hw_perf_event *hwc = &event->hw; 9907010d129SBorislav Petkov 99109e61b4fSPeter Zijlstra if (cpuc->n_pebs == cpuc->n_large_pebs) 9927010d129SBorislav Petkov intel_pmu_drain_pebs_buffer(); 9937010d129SBorislav Petkov 9947010d129SBorislav Petkov cpuc->pebs_enabled &= ~(1ULL << hwc->idx); 9957010d129SBorislav Petkov 9967010d129SBorislav Petkov if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT) 9977010d129SBorislav Petkov cpuc->pebs_enabled &= ~(1ULL << (hwc->idx + 32)); 9987010d129SBorislav Petkov else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST) 9997010d129SBorislav Petkov cpuc->pebs_enabled &= ~(1ULL << 63); 10007010d129SBorislav Petkov 10017010d129SBorislav Petkov if (cpuc->enabled) 10027010d129SBorislav Petkov wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled); 10037010d129SBorislav Petkov 10047010d129SBorislav Petkov hwc->config |= ARCH_PERFMON_EVENTSEL_INT; 10057010d129SBorislav Petkov } 10067010d129SBorislav Petkov 10077010d129SBorislav Petkov void intel_pmu_pebs_enable_all(void) 10087010d129SBorislav Petkov { 10097010d129SBorislav Petkov struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 10107010d129SBorislav Petkov 10117010d129SBorislav Petkov if (cpuc->pebs_enabled) 10127010d129SBorislav Petkov wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled); 10137010d129SBorislav Petkov } 10147010d129SBorislav Petkov 10157010d129SBorislav Petkov void intel_pmu_pebs_disable_all(void) 10167010d129SBorislav Petkov { 10177010d129SBorislav Petkov struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 10187010d129SBorislav Petkov 10197010d129SBorislav Petkov if (cpuc->pebs_enabled) 10207010d129SBorislav Petkov wrmsrl(MSR_IA32_PEBS_ENABLE, 0); 10217010d129SBorislav Petkov } 10227010d129SBorislav Petkov 10237010d129SBorislav Petkov static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs) 10247010d129SBorislav Petkov { 10257010d129SBorislav Petkov struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 10267010d129SBorislav Petkov unsigned long from = cpuc->lbr_entries[0].from; 10277010d129SBorislav Petkov unsigned long old_to, to = cpuc->lbr_entries[0].to; 10287010d129SBorislav Petkov unsigned long ip = regs->ip; 10297010d129SBorislav Petkov int is_64bit = 0; 10307010d129SBorislav Petkov void *kaddr; 10317010d129SBorislav Petkov int size; 10327010d129SBorislav Petkov 10337010d129SBorislav Petkov /* 10347010d129SBorislav Petkov * We don't need to fixup if the PEBS assist is fault like 10357010d129SBorislav Petkov */ 10367010d129SBorislav Petkov if (!x86_pmu.intel_cap.pebs_trap) 10377010d129SBorislav Petkov return 1; 10387010d129SBorislav Petkov 10397010d129SBorislav Petkov /* 10407010d129SBorislav Petkov * No LBR entry, no basic block, no rewinding 10417010d129SBorislav Petkov */ 10427010d129SBorislav Petkov if (!cpuc->lbr_stack.nr || !from || !to) 10437010d129SBorislav Petkov return 0; 10447010d129SBorislav Petkov 10457010d129SBorislav Petkov /* 10467010d129SBorislav Petkov * Basic blocks should never cross user/kernel boundaries 10477010d129SBorislav Petkov */ 10487010d129SBorislav Petkov if (kernel_ip(ip) != kernel_ip(to)) 10497010d129SBorislav Petkov return 0; 10507010d129SBorislav Petkov 10517010d129SBorislav Petkov /* 10527010d129SBorislav Petkov * unsigned math, either ip is before the start (impossible) or 10537010d129SBorislav Petkov * the basic block is larger than 1 page (sanity) 10547010d129SBorislav Petkov */ 10557010d129SBorislav Petkov if ((ip - to) > PEBS_FIXUP_SIZE) 10567010d129SBorislav Petkov return 0; 10577010d129SBorislav Petkov 10587010d129SBorislav Petkov /* 10597010d129SBorislav Petkov * We sampled a branch insn, rewind using the LBR stack 10607010d129SBorislav Petkov */ 10617010d129SBorislav Petkov if (ip == to) { 10627010d129SBorislav Petkov set_linear_ip(regs, from); 10637010d129SBorislav Petkov return 1; 10647010d129SBorislav Petkov } 10657010d129SBorislav Petkov 10667010d129SBorislav Petkov size = ip - to; 10677010d129SBorislav Petkov if (!kernel_ip(ip)) { 10687010d129SBorislav Petkov int bytes; 10697010d129SBorislav Petkov u8 *buf = this_cpu_read(insn_buffer); 10707010d129SBorislav Petkov 10717010d129SBorislav Petkov /* 'size' must fit our buffer, see above */ 10727010d129SBorislav Petkov bytes = copy_from_user_nmi(buf, (void __user *)to, size); 10737010d129SBorislav Petkov if (bytes != 0) 10747010d129SBorislav Petkov return 0; 10757010d129SBorislav Petkov 10767010d129SBorislav Petkov kaddr = buf; 10777010d129SBorislav Petkov } else { 10787010d129SBorislav Petkov kaddr = (void *)to; 10797010d129SBorislav Petkov } 10807010d129SBorislav Petkov 10817010d129SBorislav Petkov do { 10827010d129SBorislav Petkov struct insn insn; 10837010d129SBorislav Petkov 10847010d129SBorislav Petkov old_to = to; 10857010d129SBorislav Petkov 10867010d129SBorislav Petkov #ifdef CONFIG_X86_64 10877010d129SBorislav Petkov is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32); 10887010d129SBorislav Petkov #endif 10897010d129SBorislav Petkov insn_init(&insn, kaddr, size, is_64bit); 10907010d129SBorislav Petkov insn_get_length(&insn); 10917010d129SBorislav Petkov /* 10927010d129SBorislav Petkov * Make sure there was not a problem decoding the 10937010d129SBorislav Petkov * instruction and getting the length. This is 10947010d129SBorislav Petkov * doubly important because we have an infinite 10957010d129SBorislav Petkov * loop if insn.length=0. 10967010d129SBorislav Petkov */ 10977010d129SBorislav Petkov if (!insn.length) 10987010d129SBorislav Petkov break; 10997010d129SBorislav Petkov 11007010d129SBorislav Petkov to += insn.length; 11017010d129SBorislav Petkov kaddr += insn.length; 11027010d129SBorislav Petkov size -= insn.length; 11037010d129SBorislav Petkov } while (to < ip); 11047010d129SBorislav Petkov 11057010d129SBorislav Petkov if (to == ip) { 11067010d129SBorislav Petkov set_linear_ip(regs, old_to); 11077010d129SBorislav Petkov return 1; 11087010d129SBorislav Petkov } 11097010d129SBorislav Petkov 11107010d129SBorislav Petkov /* 11117010d129SBorislav Petkov * Even though we decoded the basic block, the instruction stream 11127010d129SBorislav Petkov * never matched the given IP, either the TO or the IP got corrupted. 11137010d129SBorislav Petkov */ 11147010d129SBorislav Petkov return 0; 11157010d129SBorislav Petkov } 11167010d129SBorislav Petkov 11177010d129SBorislav Petkov static inline u64 intel_hsw_weight(struct pebs_record_skl *pebs) 11187010d129SBorislav Petkov { 11197010d129SBorislav Petkov if (pebs->tsx_tuning) { 11207010d129SBorislav Petkov union hsw_tsx_tuning tsx = { .value = pebs->tsx_tuning }; 11217010d129SBorislav Petkov return tsx.cycles_last_block; 11227010d129SBorislav Petkov } 11237010d129SBorislav Petkov return 0; 11247010d129SBorislav Petkov } 11257010d129SBorislav Petkov 11267010d129SBorislav Petkov static inline u64 intel_hsw_transaction(struct pebs_record_skl *pebs) 11277010d129SBorislav Petkov { 11287010d129SBorislav Petkov u64 txn = (pebs->tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32; 11297010d129SBorislav Petkov 11307010d129SBorislav Petkov /* For RTM XABORTs also log the abort code from AX */ 11317010d129SBorislav Petkov if ((txn & PERF_TXN_TRANSACTION) && (pebs->ax & 1)) 11327010d129SBorislav Petkov txn |= ((pebs->ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT; 11337010d129SBorislav Petkov return txn; 11347010d129SBorislav Petkov } 11357010d129SBorislav Petkov 11367010d129SBorislav Petkov static void setup_pebs_sample_data(struct perf_event *event, 11377010d129SBorislav Petkov struct pt_regs *iregs, void *__pebs, 11387010d129SBorislav Petkov struct perf_sample_data *data, 11397010d129SBorislav Petkov struct pt_regs *regs) 11407010d129SBorislav Petkov { 11417010d129SBorislav Petkov #define PERF_X86_EVENT_PEBS_HSW_PREC \ 11427010d129SBorislav Petkov (PERF_X86_EVENT_PEBS_ST_HSW | \ 11437010d129SBorislav Petkov PERF_X86_EVENT_PEBS_LD_HSW | \ 11447010d129SBorislav Petkov PERF_X86_EVENT_PEBS_NA_HSW) 11457010d129SBorislav Petkov /* 11467010d129SBorislav Petkov * We cast to the biggest pebs_record but are careful not to 11477010d129SBorislav Petkov * unconditionally access the 'extra' entries. 11487010d129SBorislav Petkov */ 11497010d129SBorislav Petkov struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 11507010d129SBorislav Petkov struct pebs_record_skl *pebs = __pebs; 11517010d129SBorislav Petkov u64 sample_type; 11527010d129SBorislav Petkov int fll, fst, dsrc; 11537010d129SBorislav Petkov int fl = event->hw.flags; 11547010d129SBorislav Petkov 11557010d129SBorislav Petkov if (pebs == NULL) 11567010d129SBorislav Petkov return; 11577010d129SBorislav Petkov 11587010d129SBorislav Petkov sample_type = event->attr.sample_type; 11597010d129SBorislav Petkov dsrc = sample_type & PERF_SAMPLE_DATA_SRC; 11607010d129SBorislav Petkov 11617010d129SBorislav Petkov fll = fl & PERF_X86_EVENT_PEBS_LDLAT; 11627010d129SBorislav Petkov fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC); 11637010d129SBorislav Petkov 11647010d129SBorislav Petkov perf_sample_data_init(data, 0, event->hw.last_period); 11657010d129SBorislav Petkov 11667010d129SBorislav Petkov data->period = event->hw.last_period; 11677010d129SBorislav Petkov 11687010d129SBorislav Petkov /* 11697010d129SBorislav Petkov * Use latency for weight (only avail with PEBS-LL) 11707010d129SBorislav Petkov */ 11717010d129SBorislav Petkov if (fll && (sample_type & PERF_SAMPLE_WEIGHT)) 11727010d129SBorislav Petkov data->weight = pebs->lat; 11737010d129SBorislav Petkov 11747010d129SBorislav Petkov /* 11757010d129SBorislav Petkov * data.data_src encodes the data source 11767010d129SBorislav Petkov */ 11777010d129SBorislav Petkov if (dsrc) { 11787010d129SBorislav Petkov u64 val = PERF_MEM_NA; 11797010d129SBorislav Petkov if (fll) 11807010d129SBorislav Petkov val = load_latency_data(pebs->dse); 11817010d129SBorislav Petkov else if (fst && (fl & PERF_X86_EVENT_PEBS_HSW_PREC)) 11827010d129SBorislav Petkov val = precise_datala_hsw(event, pebs->dse); 11837010d129SBorislav Petkov else if (fst) 11847010d129SBorislav Petkov val = precise_store_data(pebs->dse); 11857010d129SBorislav Petkov data->data_src.val = val; 11867010d129SBorislav Petkov } 11877010d129SBorislav Petkov 11887010d129SBorislav Petkov /* 1189*6cbc304fSPeter Zijlstra * We must however always use iregs for the unwinder to stay sane; the 1190*6cbc304fSPeter Zijlstra * record BP,SP,IP can point into thin air when the record is from a 1191*6cbc304fSPeter Zijlstra * previous PMI context or an (I)RET happend between the record and 1192*6cbc304fSPeter Zijlstra * PMI. 1193*6cbc304fSPeter Zijlstra */ 1194*6cbc304fSPeter Zijlstra if (sample_type & PERF_SAMPLE_CALLCHAIN) 1195*6cbc304fSPeter Zijlstra data->callchain = perf_callchain(event, iregs); 1196*6cbc304fSPeter Zijlstra 1197*6cbc304fSPeter Zijlstra /* 1198b8000586SPeter Zijlstra * We use the interrupt regs as a base because the PEBS record does not 1199b8000586SPeter Zijlstra * contain a full regs set, specifically it seems to lack segment 1200b8000586SPeter Zijlstra * descriptors, which get used by things like user_mode(). 12017010d129SBorislav Petkov * 1202b8000586SPeter Zijlstra * In the simple case fix up only the IP for PERF_SAMPLE_IP. 12037010d129SBorislav Petkov */ 12047010d129SBorislav Petkov *regs = *iregs; 1205d1e7e602SStephane Eranian 1206d1e7e602SStephane Eranian /* 1207d1e7e602SStephane Eranian * Initialize regs_>flags from PEBS, 1208d1e7e602SStephane Eranian * Clear exact bit (which uses x86 EFLAGS Reserved bit 3), 1209d1e7e602SStephane Eranian * i.e., do not rely on it being zero: 1210d1e7e602SStephane Eranian */ 1211d1e7e602SStephane Eranian regs->flags = pebs->flags & ~PERF_EFLAGS_EXACT; 12127010d129SBorislav Petkov 12137010d129SBorislav Petkov if (sample_type & PERF_SAMPLE_REGS_INTR) { 12147010d129SBorislav Petkov regs->ax = pebs->ax; 12157010d129SBorislav Petkov regs->bx = pebs->bx; 12167010d129SBorislav Petkov regs->cx = pebs->cx; 12177010d129SBorislav Petkov regs->dx = pebs->dx; 12187010d129SBorislav Petkov regs->si = pebs->si; 12197010d129SBorislav Petkov regs->di = pebs->di; 1220b8000586SPeter Zijlstra 12217010d129SBorislav Petkov regs->bp = pebs->bp; 12227010d129SBorislav Petkov regs->sp = pebs->sp; 12237010d129SBorislav Petkov 12247010d129SBorislav Petkov #ifndef CONFIG_X86_32 12257010d129SBorislav Petkov regs->r8 = pebs->r8; 12267010d129SBorislav Petkov regs->r9 = pebs->r9; 12277010d129SBorislav Petkov regs->r10 = pebs->r10; 12287010d129SBorislav Petkov regs->r11 = pebs->r11; 12297010d129SBorislav Petkov regs->r12 = pebs->r12; 12307010d129SBorislav Petkov regs->r13 = pebs->r13; 12317010d129SBorislav Petkov regs->r14 = pebs->r14; 12327010d129SBorislav Petkov regs->r15 = pebs->r15; 12337010d129SBorislav Petkov #endif 12347010d129SBorislav Petkov } 12357010d129SBorislav Petkov 123671eb9ee9SStephane Eranian if (event->attr.precise_ip > 1) { 1237d1e7e602SStephane Eranian /* 1238d1e7e602SStephane Eranian * Haswell and later processors have an 'eventing IP' 1239d1e7e602SStephane Eranian * (real IP) which fixes the off-by-1 skid in hardware. 1240d1e7e602SStephane Eranian * Use it when precise_ip >= 2 : 1241d1e7e602SStephane Eranian */ 124271eb9ee9SStephane Eranian if (x86_pmu.intel_cap.pebs_format >= 2) { 124371eb9ee9SStephane Eranian set_linear_ip(regs, pebs->real_ip); 12447010d129SBorislav Petkov regs->flags |= PERF_EFLAGS_EXACT; 124571eb9ee9SStephane Eranian } else { 1246d1e7e602SStephane Eranian /* Otherwise, use PEBS off-by-1 IP: */ 124771eb9ee9SStephane Eranian set_linear_ip(regs, pebs->ip); 124871eb9ee9SStephane Eranian 1249d1e7e602SStephane Eranian /* 1250d1e7e602SStephane Eranian * With precise_ip >= 2, try to fix up the off-by-1 IP 1251d1e7e602SStephane Eranian * using the LBR. If successful, the fixup function 1252d1e7e602SStephane Eranian * corrects regs->ip and calls set_linear_ip() on regs: 1253d1e7e602SStephane Eranian */ 125471eb9ee9SStephane Eranian if (intel_pmu_pebs_fixup_ip(regs)) 12557010d129SBorislav Petkov regs->flags |= PERF_EFLAGS_EXACT; 125671eb9ee9SStephane Eranian } 1257d1e7e602SStephane Eranian } else { 1258d1e7e602SStephane Eranian /* 1259d1e7e602SStephane Eranian * When precise_ip == 1, return the PEBS off-by-1 IP, 1260d1e7e602SStephane Eranian * no fixup attempted: 1261d1e7e602SStephane Eranian */ 126271eb9ee9SStephane Eranian set_linear_ip(regs, pebs->ip); 1263d1e7e602SStephane Eranian } 126471eb9ee9SStephane Eranian 12657010d129SBorislav Petkov 1266fc7ce9c7SKan Liang if ((sample_type & (PERF_SAMPLE_ADDR | PERF_SAMPLE_PHYS_ADDR)) && 12677010d129SBorislav Petkov x86_pmu.intel_cap.pebs_format >= 1) 12687010d129SBorislav Petkov data->addr = pebs->dla; 12697010d129SBorislav Petkov 12707010d129SBorislav Petkov if (x86_pmu.intel_cap.pebs_format >= 2) { 12717010d129SBorislav Petkov /* Only set the TSX weight when no memory weight. */ 12727010d129SBorislav Petkov if ((sample_type & PERF_SAMPLE_WEIGHT) && !fll) 12737010d129SBorislav Petkov data->weight = intel_hsw_weight(pebs); 12747010d129SBorislav Petkov 12757010d129SBorislav Petkov if (sample_type & PERF_SAMPLE_TRANSACTION) 12767010d129SBorislav Petkov data->txn = intel_hsw_transaction(pebs); 12777010d129SBorislav Petkov } 12787010d129SBorislav Petkov 12797010d129SBorislav Petkov /* 12807010d129SBorislav Petkov * v3 supplies an accurate time stamp, so we use that 12817010d129SBorislav Petkov * for the time stamp. 12827010d129SBorislav Petkov * 12837010d129SBorislav Petkov * We can only do this for the default trace clock. 12847010d129SBorislav Petkov */ 12857010d129SBorislav Petkov if (x86_pmu.intel_cap.pebs_format >= 3 && 12867010d129SBorislav Petkov event->attr.use_clockid == 0) 12877010d129SBorislav Petkov data->time = native_sched_clock_from_tsc(pebs->tsc); 12887010d129SBorislav Petkov 12897010d129SBorislav Petkov if (has_branch_stack(event)) 12907010d129SBorislav Petkov data->br_stack = &cpuc->lbr_stack; 12917010d129SBorislav Petkov } 12927010d129SBorislav Petkov 12937010d129SBorislav Petkov static inline void * 12947010d129SBorislav Petkov get_next_pebs_record_by_bit(void *base, void *top, int bit) 12957010d129SBorislav Petkov { 12967010d129SBorislav Petkov struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 12977010d129SBorislav Petkov void *at; 12987010d129SBorislav Petkov u64 pebs_status; 12997010d129SBorislav Petkov 13007010d129SBorislav Petkov /* 13017010d129SBorislav Petkov * fmt0 does not have a status bitfield (does not use 13027010d129SBorislav Petkov * perf_record_nhm format) 13037010d129SBorislav Petkov */ 13047010d129SBorislav Petkov if (x86_pmu.intel_cap.pebs_format < 1) 13057010d129SBorislav Petkov return base; 13067010d129SBorislav Petkov 13077010d129SBorislav Petkov if (base == NULL) 13087010d129SBorislav Petkov return NULL; 13097010d129SBorislav Petkov 13107010d129SBorislav Petkov for (at = base; at < top; at += x86_pmu.pebs_record_size) { 13117010d129SBorislav Petkov struct pebs_record_nhm *p = at; 13127010d129SBorislav Petkov 13137010d129SBorislav Petkov if (test_bit(bit, (unsigned long *)&p->status)) { 13147010d129SBorislav Petkov /* PEBS v3 has accurate status bits */ 13157010d129SBorislav Petkov if (x86_pmu.intel_cap.pebs_format >= 3) 13167010d129SBorislav Petkov return at; 13177010d129SBorislav Petkov 13187010d129SBorislav Petkov if (p->status == (1 << bit)) 13197010d129SBorislav Petkov return at; 13207010d129SBorislav Petkov 13217010d129SBorislav Petkov /* clear non-PEBS bit and re-check */ 13227010d129SBorislav Petkov pebs_status = p->status & cpuc->pebs_enabled; 1323fd583ad1SKan Liang pebs_status &= PEBS_COUNTER_MASK; 13247010d129SBorislav Petkov if (pebs_status == (1 << bit)) 13257010d129SBorislav Petkov return at; 13267010d129SBorislav Petkov } 13277010d129SBorislav Petkov } 13287010d129SBorislav Petkov return NULL; 13297010d129SBorislav Petkov } 13307010d129SBorislav Petkov 13315bee2cc6SKan Liang void intel_pmu_auto_reload_read(struct perf_event *event) 13325bee2cc6SKan Liang { 13335bee2cc6SKan Liang WARN_ON(!(event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)); 13345bee2cc6SKan Liang 13355bee2cc6SKan Liang perf_pmu_disable(event->pmu); 13365bee2cc6SKan Liang intel_pmu_drain_pebs_buffer(); 13375bee2cc6SKan Liang perf_pmu_enable(event->pmu); 13385bee2cc6SKan Liang } 13395bee2cc6SKan Liang 1340d31fc13fSKan Liang /* 1341d31fc13fSKan Liang * Special variant of intel_pmu_save_and_restart() for auto-reload. 1342d31fc13fSKan Liang */ 1343d31fc13fSKan Liang static int 1344d31fc13fSKan Liang intel_pmu_save_and_restart_reload(struct perf_event *event, int count) 1345d31fc13fSKan Liang { 1346d31fc13fSKan Liang struct hw_perf_event *hwc = &event->hw; 1347d31fc13fSKan Liang int shift = 64 - x86_pmu.cntval_bits; 1348d31fc13fSKan Liang u64 period = hwc->sample_period; 1349d31fc13fSKan Liang u64 prev_raw_count, new_raw_count; 1350d31fc13fSKan Liang s64 new, old; 1351d31fc13fSKan Liang 1352d31fc13fSKan Liang WARN_ON(!period); 1353d31fc13fSKan Liang 1354d31fc13fSKan Liang /* 1355d31fc13fSKan Liang * drain_pebs() only happens when the PMU is disabled. 1356d31fc13fSKan Liang */ 1357d31fc13fSKan Liang WARN_ON(this_cpu_read(cpu_hw_events.enabled)); 1358d31fc13fSKan Liang 1359d31fc13fSKan Liang prev_raw_count = local64_read(&hwc->prev_count); 1360d31fc13fSKan Liang rdpmcl(hwc->event_base_rdpmc, new_raw_count); 1361d31fc13fSKan Liang local64_set(&hwc->prev_count, new_raw_count); 1362d31fc13fSKan Liang 1363d31fc13fSKan Liang /* 1364d31fc13fSKan Liang * Since the counter increments a negative counter value and 1365d31fc13fSKan Liang * overflows on the sign switch, giving the interval: 1366d31fc13fSKan Liang * 1367d31fc13fSKan Liang * [-period, 0] 1368d31fc13fSKan Liang * 1369d31fc13fSKan Liang * the difference between two consequtive reads is: 1370d31fc13fSKan Liang * 1371d31fc13fSKan Liang * A) value2 - value1; 1372d31fc13fSKan Liang * when no overflows have happened in between, 1373d31fc13fSKan Liang * 1374d31fc13fSKan Liang * B) (0 - value1) + (value2 - (-period)); 1375d31fc13fSKan Liang * when one overflow happened in between, 1376d31fc13fSKan Liang * 1377d31fc13fSKan Liang * C) (0 - value1) + (n - 1) * (period) + (value2 - (-period)); 1378d31fc13fSKan Liang * when @n overflows happened in between. 1379d31fc13fSKan Liang * 1380d31fc13fSKan Liang * Here A) is the obvious difference, B) is the extension to the 1381d31fc13fSKan Liang * discrete interval, where the first term is to the top of the 1382d31fc13fSKan Liang * interval and the second term is from the bottom of the next 1383d31fc13fSKan Liang * interval and C) the extension to multiple intervals, where the 1384d31fc13fSKan Liang * middle term is the whole intervals covered. 1385d31fc13fSKan Liang * 1386d31fc13fSKan Liang * An equivalent of C, by reduction, is: 1387d31fc13fSKan Liang * 1388d31fc13fSKan Liang * value2 - value1 + n * period 1389d31fc13fSKan Liang */ 1390d31fc13fSKan Liang new = ((s64)(new_raw_count << shift) >> shift); 1391d31fc13fSKan Liang old = ((s64)(prev_raw_count << shift) >> shift); 1392d31fc13fSKan Liang local64_add(new - old + count * period, &event->count); 1393d31fc13fSKan Liang 1394d31fc13fSKan Liang perf_event_update_userpage(event); 1395d31fc13fSKan Liang 1396d31fc13fSKan Liang return 0; 1397d31fc13fSKan Liang } 1398d31fc13fSKan Liang 13997010d129SBorislav Petkov static void __intel_pmu_pebs_event(struct perf_event *event, 14007010d129SBorislav Petkov struct pt_regs *iregs, 14017010d129SBorislav Petkov void *base, void *top, 14027010d129SBorislav Petkov int bit, int count) 14037010d129SBorislav Petkov { 1404d31fc13fSKan Liang struct hw_perf_event *hwc = &event->hw; 14057010d129SBorislav Petkov struct perf_sample_data data; 14067010d129SBorislav Petkov struct pt_regs regs; 14077010d129SBorislav Petkov void *at = get_next_pebs_record_by_bit(base, top, bit); 14087010d129SBorislav Petkov 1409d31fc13fSKan Liang if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) { 1410d31fc13fSKan Liang /* 1411d31fc13fSKan Liang * Now, auto-reload is only enabled in fixed period mode. 1412d31fc13fSKan Liang * The reload value is always hwc->sample_period. 1413d31fc13fSKan Liang * May need to change it, if auto-reload is enabled in 1414d31fc13fSKan Liang * freq mode later. 1415d31fc13fSKan Liang */ 1416d31fc13fSKan Liang intel_pmu_save_and_restart_reload(event, count); 1417d31fc13fSKan Liang } else if (!intel_pmu_save_and_restart(event)) 14187010d129SBorislav Petkov return; 14197010d129SBorislav Petkov 14207010d129SBorislav Petkov while (count > 1) { 14217010d129SBorislav Petkov setup_pebs_sample_data(event, iregs, at, &data, ®s); 14227010d129SBorislav Petkov perf_event_output(event, &data, ®s); 14237010d129SBorislav Petkov at += x86_pmu.pebs_record_size; 14247010d129SBorislav Petkov at = get_next_pebs_record_by_bit(at, top, bit); 14257010d129SBorislav Petkov count--; 14267010d129SBorislav Petkov } 14277010d129SBorislav Petkov 14287010d129SBorislav Petkov setup_pebs_sample_data(event, iregs, at, &data, ®s); 14297010d129SBorislav Petkov 14307010d129SBorislav Petkov /* 14317010d129SBorislav Petkov * All but the last records are processed. 14327010d129SBorislav Petkov * The last one is left to be able to call the overflow handler. 14337010d129SBorislav Petkov */ 14347010d129SBorislav Petkov if (perf_event_overflow(event, &data, ®s)) { 14357010d129SBorislav Petkov x86_pmu_stop(event, 0); 14367010d129SBorislav Petkov return; 14377010d129SBorislav Petkov } 14387010d129SBorislav Petkov 14397010d129SBorislav Petkov } 14407010d129SBorislav Petkov 14417010d129SBorislav Petkov static void intel_pmu_drain_pebs_core(struct pt_regs *iregs) 14427010d129SBorislav Petkov { 14437010d129SBorislav Petkov struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 14447010d129SBorislav Petkov struct debug_store *ds = cpuc->ds; 14457010d129SBorislav Petkov struct perf_event *event = cpuc->events[0]; /* PMC0 only */ 14467010d129SBorislav Petkov struct pebs_record_core *at, *top; 14477010d129SBorislav Petkov int n; 14487010d129SBorislav Petkov 14497010d129SBorislav Petkov if (!x86_pmu.pebs_active) 14507010d129SBorislav Petkov return; 14517010d129SBorislav Petkov 14527010d129SBorislav Petkov at = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base; 14537010d129SBorislav Petkov top = (struct pebs_record_core *)(unsigned long)ds->pebs_index; 14547010d129SBorislav Petkov 14557010d129SBorislav Petkov /* 14567010d129SBorislav Petkov * Whatever else happens, drain the thing 14577010d129SBorislav Petkov */ 14587010d129SBorislav Petkov ds->pebs_index = ds->pebs_buffer_base; 14597010d129SBorislav Petkov 14607010d129SBorislav Petkov if (!test_bit(0, cpuc->active_mask)) 14617010d129SBorislav Petkov return; 14627010d129SBorislav Petkov 14637010d129SBorislav Petkov WARN_ON_ONCE(!event); 14647010d129SBorislav Petkov 14657010d129SBorislav Petkov if (!event->attr.precise_ip) 14667010d129SBorislav Petkov return; 14677010d129SBorislav Petkov 14687010d129SBorislav Petkov n = top - at; 1469d31fc13fSKan Liang if (n <= 0) { 1470d31fc13fSKan Liang if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD) 1471d31fc13fSKan Liang intel_pmu_save_and_restart_reload(event, 0); 14727010d129SBorislav Petkov return; 1473d31fc13fSKan Liang } 14747010d129SBorislav Petkov 14757010d129SBorislav Petkov __intel_pmu_pebs_event(event, iregs, at, top, 0, n); 14767010d129SBorislav Petkov } 14777010d129SBorislav Petkov 14787010d129SBorislav Petkov static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs) 14797010d129SBorislav Petkov { 14807010d129SBorislav Petkov struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 14817010d129SBorislav Petkov struct debug_store *ds = cpuc->ds; 14827010d129SBorislav Petkov struct perf_event *event; 14837010d129SBorislav Petkov void *base, *at, *top; 14847010d129SBorislav Petkov short counts[MAX_PEBS_EVENTS] = {}; 14857010d129SBorislav Petkov short error[MAX_PEBS_EVENTS] = {}; 14867010d129SBorislav Petkov int bit, i; 14877010d129SBorislav Petkov 14887010d129SBorislav Petkov if (!x86_pmu.pebs_active) 14897010d129SBorislav Petkov return; 14907010d129SBorislav Petkov 14917010d129SBorislav Petkov base = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base; 14927010d129SBorislav Petkov top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index; 14937010d129SBorislav Petkov 14947010d129SBorislav Petkov ds->pebs_index = ds->pebs_buffer_base; 14957010d129SBorislav Petkov 1496d31fc13fSKan Liang if (unlikely(base >= top)) { 1497d31fc13fSKan Liang /* 1498d31fc13fSKan Liang * The drain_pebs() could be called twice in a short period 1499d31fc13fSKan Liang * for auto-reload event in pmu::read(). There are no 1500d31fc13fSKan Liang * overflows have happened in between. 1501d31fc13fSKan Liang * It needs to call intel_pmu_save_and_restart_reload() to 1502d31fc13fSKan Liang * update the event->count for this case. 1503d31fc13fSKan Liang */ 1504d31fc13fSKan Liang for_each_set_bit(bit, (unsigned long *)&cpuc->pebs_enabled, 1505d31fc13fSKan Liang x86_pmu.max_pebs_events) { 1506d31fc13fSKan Liang event = cpuc->events[bit]; 1507d31fc13fSKan Liang if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD) 1508d31fc13fSKan Liang intel_pmu_save_and_restart_reload(event, 0); 1509d31fc13fSKan Liang } 15107010d129SBorislav Petkov return; 1511d31fc13fSKan Liang } 15127010d129SBorislav Petkov 15137010d129SBorislav Petkov for (at = base; at < top; at += x86_pmu.pebs_record_size) { 15147010d129SBorislav Petkov struct pebs_record_nhm *p = at; 15157010d129SBorislav Petkov u64 pebs_status; 15167010d129SBorislav Petkov 15178ef9b845SPeter Zijlstra pebs_status = p->status & cpuc->pebs_enabled; 15188ef9b845SPeter Zijlstra pebs_status &= (1ULL << x86_pmu.max_pebs_events) - 1; 15198ef9b845SPeter Zijlstra 15208ef9b845SPeter Zijlstra /* PEBS v3 has more accurate status bits */ 15217010d129SBorislav Petkov if (x86_pmu.intel_cap.pebs_format >= 3) { 15228ef9b845SPeter Zijlstra for_each_set_bit(bit, (unsigned long *)&pebs_status, 15238ef9b845SPeter Zijlstra x86_pmu.max_pebs_events) 15247010d129SBorislav Petkov counts[bit]++; 15257010d129SBorislav Petkov 15267010d129SBorislav Petkov continue; 15277010d129SBorislav Petkov } 15287010d129SBorislav Petkov 15297010d129SBorislav Petkov /* 15307010d129SBorislav Petkov * On some CPUs the PEBS status can be zero when PEBS is 15317010d129SBorislav Petkov * racing with clearing of GLOBAL_STATUS. 15327010d129SBorislav Petkov * 15337010d129SBorislav Petkov * Normally we would drop that record, but in the 15347010d129SBorislav Petkov * case when there is only a single active PEBS event 15357010d129SBorislav Petkov * we can assume it's for that event. 15367010d129SBorislav Petkov */ 15377010d129SBorislav Petkov if (!pebs_status && cpuc->pebs_enabled && 15387010d129SBorislav Petkov !(cpuc->pebs_enabled & (cpuc->pebs_enabled-1))) 15397010d129SBorislav Petkov pebs_status = cpuc->pebs_enabled; 15407010d129SBorislav Petkov 15417010d129SBorislav Petkov bit = find_first_bit((unsigned long *)&pebs_status, 15427010d129SBorislav Petkov x86_pmu.max_pebs_events); 15437010d129SBorislav Petkov if (bit >= x86_pmu.max_pebs_events) 15447010d129SBorislav Petkov continue; 15457010d129SBorislav Petkov 15467010d129SBorislav Petkov /* 15477010d129SBorislav Petkov * The PEBS hardware does not deal well with the situation 15487010d129SBorislav Petkov * when events happen near to each other and multiple bits 15497010d129SBorislav Petkov * are set. But it should happen rarely. 15507010d129SBorislav Petkov * 15517010d129SBorislav Petkov * If these events include one PEBS and multiple non-PEBS 15527010d129SBorislav Petkov * events, it doesn't impact PEBS record. The record will 15537010d129SBorislav Petkov * be handled normally. (slow path) 15547010d129SBorislav Petkov * 15557010d129SBorislav Petkov * If these events include two or more PEBS events, the 15567010d129SBorislav Petkov * records for the events can be collapsed into a single 15577010d129SBorislav Petkov * one, and it's not possible to reconstruct all events 15587010d129SBorislav Petkov * that caused the PEBS record. It's called collision. 15597010d129SBorislav Petkov * If collision happened, the record will be dropped. 15607010d129SBorislav Petkov */ 15617010d129SBorislav Petkov if (p->status != (1ULL << bit)) { 15627010d129SBorislav Petkov for_each_set_bit(i, (unsigned long *)&pebs_status, 15637010d129SBorislav Petkov x86_pmu.max_pebs_events) 15647010d129SBorislav Petkov error[i]++; 15657010d129SBorislav Petkov continue; 15667010d129SBorislav Petkov } 15677010d129SBorislav Petkov 15687010d129SBorislav Petkov counts[bit]++; 15697010d129SBorislav Petkov } 15707010d129SBorislav Petkov 15717010d129SBorislav Petkov for (bit = 0; bit < x86_pmu.max_pebs_events; bit++) { 15727010d129SBorislav Petkov if ((counts[bit] == 0) && (error[bit] == 0)) 15737010d129SBorislav Petkov continue; 15747010d129SBorislav Petkov 15757010d129SBorislav Petkov event = cpuc->events[bit]; 15768ef9b845SPeter Zijlstra if (WARN_ON_ONCE(!event)) 15778ef9b845SPeter Zijlstra continue; 15788ef9b845SPeter Zijlstra 15798ef9b845SPeter Zijlstra if (WARN_ON_ONCE(!event->attr.precise_ip)) 15808ef9b845SPeter Zijlstra continue; 15817010d129SBorislav Petkov 15827010d129SBorislav Petkov /* log dropped samples number */ 1583475113d9SJiri Olsa if (error[bit]) { 15847010d129SBorislav Petkov perf_log_lost_samples(event, error[bit]); 15857010d129SBorislav Petkov 1586475113d9SJiri Olsa if (perf_event_account_interrupt(event)) 1587475113d9SJiri Olsa x86_pmu_stop(event, 0); 1588475113d9SJiri Olsa } 1589475113d9SJiri Olsa 15907010d129SBorislav Petkov if (counts[bit]) { 15917010d129SBorislav Petkov __intel_pmu_pebs_event(event, iregs, base, 15927010d129SBorislav Petkov top, bit, counts[bit]); 15937010d129SBorislav Petkov } 15947010d129SBorislav Petkov } 15957010d129SBorislav Petkov } 15967010d129SBorislav Petkov 15977010d129SBorislav Petkov /* 15987010d129SBorislav Petkov * BTS, PEBS probe and setup 15997010d129SBorislav Petkov */ 16007010d129SBorislav Petkov 16017010d129SBorislav Petkov void __init intel_ds_init(void) 16027010d129SBorislav Petkov { 16037010d129SBorislav Petkov /* 16047010d129SBorislav Petkov * No support for 32bit formats 16057010d129SBorislav Petkov */ 16067010d129SBorislav Petkov if (!boot_cpu_has(X86_FEATURE_DTES64)) 16077010d129SBorislav Petkov return; 16087010d129SBorislav Petkov 16097010d129SBorislav Petkov x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS); 16107010d129SBorislav Petkov x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS); 1611e72daf3fSJiri Olsa x86_pmu.pebs_buffer_size = PEBS_BUFFER_SIZE; 16127010d129SBorislav Petkov if (x86_pmu.pebs) { 16137010d129SBorislav Petkov char pebs_type = x86_pmu.intel_cap.pebs_trap ? '+' : '-'; 16147010d129SBorislav Petkov int format = x86_pmu.intel_cap.pebs_format; 16157010d129SBorislav Petkov 16167010d129SBorislav Petkov switch (format) { 16177010d129SBorislav Petkov case 0: 16187010d129SBorislav Petkov pr_cont("PEBS fmt0%c, ", pebs_type); 16197010d129SBorislav Petkov x86_pmu.pebs_record_size = sizeof(struct pebs_record_core); 1620e72daf3fSJiri Olsa /* 1621e72daf3fSJiri Olsa * Using >PAGE_SIZE buffers makes the WRMSR to 1622e72daf3fSJiri Olsa * PERF_GLOBAL_CTRL in intel_pmu_enable_all() 1623e72daf3fSJiri Olsa * mysteriously hang on Core2. 1624e72daf3fSJiri Olsa * 1625e72daf3fSJiri Olsa * As a workaround, we don't do this. 1626e72daf3fSJiri Olsa */ 1627e72daf3fSJiri Olsa x86_pmu.pebs_buffer_size = PAGE_SIZE; 16287010d129SBorislav Petkov x86_pmu.drain_pebs = intel_pmu_drain_pebs_core; 16297010d129SBorislav Petkov break; 16307010d129SBorislav Petkov 16317010d129SBorislav Petkov case 1: 16327010d129SBorislav Petkov pr_cont("PEBS fmt1%c, ", pebs_type); 16337010d129SBorislav Petkov x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm); 16347010d129SBorislav Petkov x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm; 16357010d129SBorislav Petkov break; 16367010d129SBorislav Petkov 16377010d129SBorislav Petkov case 2: 16387010d129SBorislav Petkov pr_cont("PEBS fmt2%c, ", pebs_type); 16397010d129SBorislav Petkov x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw); 16407010d129SBorislav Petkov x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm; 16417010d129SBorislav Petkov break; 16427010d129SBorislav Petkov 16437010d129SBorislav Petkov case 3: 16447010d129SBorislav Petkov pr_cont("PEBS fmt3%c, ", pebs_type); 16457010d129SBorislav Petkov x86_pmu.pebs_record_size = 16467010d129SBorislav Petkov sizeof(struct pebs_record_skl); 16477010d129SBorislav Petkov x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm; 1648174afc3eSKan Liang x86_pmu.large_pebs_flags |= PERF_SAMPLE_TIME; 16497010d129SBorislav Petkov break; 16507010d129SBorislav Petkov 16517010d129SBorislav Petkov default: 16527010d129SBorislav Petkov pr_cont("no PEBS fmt%d%c, ", format, pebs_type); 16537010d129SBorislav Petkov x86_pmu.pebs = 0; 16547010d129SBorislav Petkov } 16557010d129SBorislav Petkov } 16567010d129SBorislav Petkov } 16577010d129SBorislav Petkov 16587010d129SBorislav Petkov void perf_restore_debug_store(void) 16597010d129SBorislav Petkov { 16607010d129SBorislav Petkov struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds); 16617010d129SBorislav Petkov 16627010d129SBorislav Petkov if (!x86_pmu.bts && !x86_pmu.pebs) 16637010d129SBorislav Petkov return; 16647010d129SBorislav Petkov 16657010d129SBorislav Petkov wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds); 16667010d129SBorislav Petkov } 1667