1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/bitops.h>
3 #include <linux/types.h>
4 #include <linux/slab.h>
5 #include <linux/sched/clock.h>
7 #include <asm/cpu_entry_area.h>
8 #include <asm/debugreg.h>
9 #include <asm/perf_event.h>
10 #include <asm/tlbflush.h>
13 #include <asm/timer.h>
15 #include "../perf_event.h"
17 /* Waste a full page so it can be mapped into the cpu_entry_area */
18 DEFINE_PER_CPU_PAGE_ALIGNED(struct debug_store, cpu_debug_store);
20 /* The size of a BTS record in bytes: */
21 #define BTS_RECORD_SIZE 24
23 #define PEBS_FIXUP_SIZE PAGE_SIZE
26 * pebs_record_32 for p4 and core not supported
28 struct pebs_record_32 {
36 union intel_x86_pebs_dse {
39 unsigned int ld_dse:4;
40 unsigned int ld_stlb_miss:1;
41 unsigned int ld_locked:1;
42 unsigned int ld_data_blk:1;
43 unsigned int ld_addr_blk:1;
44 unsigned int ld_reserved:24;
47 unsigned int st_l1d_hit:1;
48 unsigned int st_reserved1:3;
49 unsigned int st_stlb_miss:1;
50 unsigned int st_locked:1;
51 unsigned int st_reserved2:26;
54 unsigned int st_lat_dse:4;
55 unsigned int st_lat_stlb_miss:1;
56 unsigned int st_lat_locked:1;
57 unsigned int ld_reserved3:26;
60 unsigned int mtl_dse:5;
61 unsigned int mtl_locked:1;
62 unsigned int mtl_stlb_miss:1;
63 unsigned int mtl_fwd_blk:1;
64 unsigned int ld_reserved4:24;
70 * Map PEBS Load Latency Data Source encodings to generic
71 * memory data source information
73 #define P(a, b) PERF_MEM_S(a, b)
74 #define OP_LH (P(OP, LOAD) | P(LVL, HIT))
75 #define LEVEL(x) P(LVLNUM, x)
76 #define REM P(REMOTE, REMOTE)
77 #define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS))
79 /* Version for Sandy Bridge and later */
80 static u64 pebs_data_source[] = {
81 P(OP, LOAD) | P(LVL, MISS) | LEVEL(L3) | P(SNOOP, NA),/* 0x00:ukn L3 */
82 OP_LH | P(LVL, L1) | LEVEL(L1) | P(SNOOP, NONE), /* 0x01: L1 local */
83 OP_LH | P(LVL, LFB) | LEVEL(LFB) | P(SNOOP, NONE), /* 0x02: LFB hit */
84 OP_LH | P(LVL, L2) | LEVEL(L2) | P(SNOOP, NONE), /* 0x03: L2 hit */
85 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, NONE), /* 0x04: L3 hit */
86 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, MISS), /* 0x05: L3 hit, snoop miss */
87 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT), /* 0x06: L3 hit, snoop hit */
88 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM), /* 0x07: L3 hit, snoop hitm */
89 OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HIT), /* 0x08: L3 miss snoop hit */
90 OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HITM), /* 0x09: L3 miss snoop hitm*/
91 OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | P(SNOOP, HIT), /* 0x0a: L3 miss, shared */
92 OP_LH | P(LVL, REM_RAM1) | REM | LEVEL(L3) | P(SNOOP, HIT), /* 0x0b: L3 miss, shared */
93 OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | SNOOP_NONE_MISS, /* 0x0c: L3 miss, excl */
94 OP_LH | P(LVL, REM_RAM1) | LEVEL(RAM) | REM | SNOOP_NONE_MISS, /* 0x0d: L3 miss, excl */
95 OP_LH | P(LVL, IO) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0e: I/O */
96 OP_LH | P(LVL, UNC) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0f: uncached */
99 /* Patch up minor differences in the bits */
100 void __init intel_pmu_pebs_data_source_nhm(void)
102 pebs_data_source[0x05] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT);
103 pebs_data_source[0x06] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
104 pebs_data_source[0x07] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
107 static void __init __intel_pmu_pebs_data_source_skl(bool pmem, u64 *data_source)
109 u64 pmem_or_l4 = pmem ? LEVEL(PMEM) : LEVEL(L4);
111 data_source[0x08] = OP_LH | pmem_or_l4 | P(SNOOP, HIT);
112 data_source[0x09] = OP_LH | pmem_or_l4 | REM | P(SNOOP, HIT);
113 data_source[0x0b] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, NONE);
114 data_source[0x0c] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOPX, FWD);
115 data_source[0x0d] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOP, HITM);
118 void __init intel_pmu_pebs_data_source_skl(bool pmem)
120 __intel_pmu_pebs_data_source_skl(pmem, pebs_data_source);
123 static void __init __intel_pmu_pebs_data_source_grt(u64 *data_source)
125 data_source[0x05] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT);
126 data_source[0x06] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
127 data_source[0x08] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOPX, FWD);
130 void __init intel_pmu_pebs_data_source_grt(void)
132 __intel_pmu_pebs_data_source_grt(pebs_data_source);
135 void __init intel_pmu_pebs_data_source_adl(void)
139 data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX].pebs_data_source;
140 memcpy(data_source, pebs_data_source, sizeof(pebs_data_source));
141 __intel_pmu_pebs_data_source_skl(false, data_source);
143 data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_ATOM_IDX].pebs_data_source;
144 memcpy(data_source, pebs_data_source, sizeof(pebs_data_source));
145 __intel_pmu_pebs_data_source_grt(data_source);
148 static void __init __intel_pmu_pebs_data_source_cmt(u64 *data_source)
150 data_source[0x07] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOPX, FWD);
151 data_source[0x08] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
152 data_source[0x0a] = OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | P(SNOOP, NONE);
153 data_source[0x0b] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, NONE);
154 data_source[0x0c] = OP_LH | LEVEL(RAM) | REM | P(SNOOPX, FWD);
155 data_source[0x0d] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, HITM);
158 void __init intel_pmu_pebs_data_source_mtl(void)
162 data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX].pebs_data_source;
163 memcpy(data_source, pebs_data_source, sizeof(pebs_data_source));
164 __intel_pmu_pebs_data_source_skl(false, data_source);
166 data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_ATOM_IDX].pebs_data_source;
167 memcpy(data_source, pebs_data_source, sizeof(pebs_data_source));
168 __intel_pmu_pebs_data_source_cmt(data_source);
171 void __init intel_pmu_pebs_data_source_cmt(void)
173 __intel_pmu_pebs_data_source_cmt(pebs_data_source);
176 static u64 precise_store_data(u64 status)
178 union intel_x86_pebs_dse dse;
179 u64 val = P(OP, STORE) | P(SNOOP, NA) | P(LVL, L1) | P(TLB, L2);
185 * 1 = stored missed 2nd level TLB
187 * so it either hit the walker or the OS
188 * otherwise hit 2nd level TLB
190 if (dse.st_stlb_miss)
196 * bit 0: hit L1 data cache
197 * if not set, then all we know is that
206 * bit 5: Locked prefix
209 val |= P(LOCK, LOCKED);
214 static u64 precise_datala_hsw(struct perf_event *event, u64 status)
216 union perf_mem_data_src dse;
218 dse.val = PERF_MEM_NA;
220 if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW)
221 dse.mem_op = PERF_MEM_OP_STORE;
222 else if (event->hw.flags & PERF_X86_EVENT_PEBS_LD_HSW)
223 dse.mem_op = PERF_MEM_OP_LOAD;
226 * L1 info only valid for following events:
228 * MEM_UOPS_RETIRED.STLB_MISS_STORES
229 * MEM_UOPS_RETIRED.LOCK_STORES
230 * MEM_UOPS_RETIRED.SPLIT_STORES
231 * MEM_UOPS_RETIRED.ALL_STORES
233 if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) {
235 dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_HIT;
237 dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_MISS;
242 static inline void pebs_set_tlb_lock(u64 *val, bool tlb, bool lock)
246 * 0 = did not miss 2nd level TLB
247 * 1 = missed 2nd level TLB
250 *val |= P(TLB, MISS) | P(TLB, L2);
252 *val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2);
256 *val |= P(LOCK, LOCKED);
259 /* Retrieve the latency data for e-core of ADL */
260 static u64 __adl_latency_data_small(struct perf_event *event, u64 status,
261 u8 dse, bool tlb, bool lock, bool blk)
265 WARN_ON_ONCE(hybrid_pmu(event->pmu)->pmu_type == hybrid_big);
267 dse &= PERF_PEBS_DATA_SOURCE_MASK;
268 val = hybrid_var(event->pmu, pebs_data_source)[dse];
270 pebs_set_tlb_lock(&val, tlb, lock);
280 u64 adl_latency_data_small(struct perf_event *event, u64 status)
282 union intel_x86_pebs_dse dse;
286 return __adl_latency_data_small(event, status, dse.ld_dse,
287 dse.ld_locked, dse.ld_stlb_miss,
291 /* Retrieve the latency data for e-core of MTL */
292 u64 mtl_latency_data_small(struct perf_event *event, u64 status)
294 union intel_x86_pebs_dse dse;
298 return __adl_latency_data_small(event, status, dse.mtl_dse,
299 dse.mtl_stlb_miss, dse.mtl_locked,
303 static u64 load_latency_data(struct perf_event *event, u64 status)
305 union intel_x86_pebs_dse dse;
311 * use the mapping table for bit 0-3
313 val = hybrid_var(event->pmu, pebs_data_source)[dse.ld_dse];
316 * Nehalem models do not support TLB, Lock infos
318 if (x86_pmu.pebs_no_tlb) {
319 val |= P(TLB, NA) | P(LOCK, NA);
323 pebs_set_tlb_lock(&val, dse.ld_stlb_miss, dse.ld_locked);
326 * Ice Lake and earlier models do not support block infos.
328 if (!x86_pmu.pebs_block) {
333 * bit 6: load was blocked since its data could not be forwarded
334 * from a preceding store
340 * bit 7: load was blocked due to potential address conflict with
346 if (!dse.ld_data_blk && !dse.ld_addr_blk)
352 static u64 store_latency_data(struct perf_event *event, u64 status)
354 union intel_x86_pebs_dse dse;
355 union perf_mem_data_src src;
361 * use the mapping table for bit 0-3
363 val = hybrid_var(event->pmu, pebs_data_source)[dse.st_lat_dse];
365 pebs_set_tlb_lock(&val, dse.st_lat_stlb_miss, dse.st_lat_locked);
370 * the pebs_data_source table is only for loads
371 * so override the mem_op to say STORE instead
374 src.mem_op = P(OP,STORE);
379 struct pebs_record_core {
383 u64 r8, r9, r10, r11;
384 u64 r12, r13, r14, r15;
387 struct pebs_record_nhm {
391 u64 r8, r9, r10, r11;
392 u64 r12, r13, r14, r15;
393 u64 status, dla, dse, lat;
397 * Same as pebs_record_nhm, with two additional fields.
399 struct pebs_record_hsw {
403 u64 r8, r9, r10, r11;
404 u64 r12, r13, r14, r15;
405 u64 status, dla, dse, lat;
406 u64 real_ip, tsx_tuning;
409 union hsw_tsx_tuning {
411 u32 cycles_last_block : 32,
414 instruction_abort : 1,
415 non_instruction_abort : 1,
424 #define PEBS_HSW_TSX_FLAGS 0xff00000000ULL
426 /* Same as HSW, plus TSC */
428 struct pebs_record_skl {
432 u64 r8, r9, r10, r11;
433 u64 r12, r13, r14, r15;
434 u64 status, dla, dse, lat;
435 u64 real_ip, tsx_tuning;
439 void init_debug_store_on_cpu(int cpu)
441 struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
446 wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA,
447 (u32)((u64)(unsigned long)ds),
448 (u32)((u64)(unsigned long)ds >> 32));
451 void fini_debug_store_on_cpu(int cpu)
453 if (!per_cpu(cpu_hw_events, cpu).ds)
456 wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0);
459 static DEFINE_PER_CPU(void *, insn_buffer);
461 static void ds_update_cea(void *cea, void *addr, size_t size, pgprot_t prot)
463 unsigned long start = (unsigned long)cea;
467 pa = virt_to_phys(addr);
470 for (; msz < size; msz += PAGE_SIZE, pa += PAGE_SIZE, cea += PAGE_SIZE)
471 cea_set_pte(cea, pa, prot);
474 * This is a cross-CPU update of the cpu_entry_area, we must shoot down
475 * all TLB entries for it.
477 flush_tlb_kernel_range(start, start + size);
481 static void ds_clear_cea(void *cea, size_t size)
483 unsigned long start = (unsigned long)cea;
487 for (; msz < size; msz += PAGE_SIZE, cea += PAGE_SIZE)
488 cea_set_pte(cea, 0, PAGE_NONE);
490 flush_tlb_kernel_range(start, start + size);
494 static void *dsalloc_pages(size_t size, gfp_t flags, int cpu)
496 unsigned int order = get_order(size);
497 int node = cpu_to_node(cpu);
500 page = __alloc_pages_node(node, flags | __GFP_ZERO, order);
501 return page ? page_address(page) : NULL;
504 static void dsfree_pages(const void *buffer, size_t size)
507 free_pages((unsigned long)buffer, get_order(size));
510 static int alloc_pebs_buffer(int cpu)
512 struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
513 struct debug_store *ds = hwev->ds;
514 size_t bsiz = x86_pmu.pebs_buffer_size;
515 int max, node = cpu_to_node(cpu);
516 void *buffer, *insn_buff, *cea;
521 buffer = dsalloc_pages(bsiz, GFP_KERNEL, cpu);
522 if (unlikely(!buffer))
526 * HSW+ already provides us the eventing ip; no need to allocate this
529 if (x86_pmu.intel_cap.pebs_format < 2) {
530 insn_buff = kzalloc_node(PEBS_FIXUP_SIZE, GFP_KERNEL, node);
532 dsfree_pages(buffer, bsiz);
535 per_cpu(insn_buffer, cpu) = insn_buff;
537 hwev->ds_pebs_vaddr = buffer;
538 /* Update the cpu entry area mapping */
539 cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer;
540 ds->pebs_buffer_base = (unsigned long) cea;
541 ds_update_cea(cea, buffer, bsiz, PAGE_KERNEL);
542 ds->pebs_index = ds->pebs_buffer_base;
543 max = x86_pmu.pebs_record_size * (bsiz / x86_pmu.pebs_record_size);
544 ds->pebs_absolute_maximum = ds->pebs_buffer_base + max;
548 static void release_pebs_buffer(int cpu)
550 struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
556 kfree(per_cpu(insn_buffer, cpu));
557 per_cpu(insn_buffer, cpu) = NULL;
559 /* Clear the fixmap */
560 cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer;
561 ds_clear_cea(cea, x86_pmu.pebs_buffer_size);
562 dsfree_pages(hwev->ds_pebs_vaddr, x86_pmu.pebs_buffer_size);
563 hwev->ds_pebs_vaddr = NULL;
566 static int alloc_bts_buffer(int cpu)
568 struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
569 struct debug_store *ds = hwev->ds;
576 buffer = dsalloc_pages(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, cpu);
577 if (unlikely(!buffer)) {
578 WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__);
581 hwev->ds_bts_vaddr = buffer;
582 /* Update the fixmap */
583 cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer;
584 ds->bts_buffer_base = (unsigned long) cea;
585 ds_update_cea(cea, buffer, BTS_BUFFER_SIZE, PAGE_KERNEL);
586 ds->bts_index = ds->bts_buffer_base;
587 max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE;
588 ds->bts_absolute_maximum = ds->bts_buffer_base +
589 max * BTS_RECORD_SIZE;
590 ds->bts_interrupt_threshold = ds->bts_absolute_maximum -
591 (max / 16) * BTS_RECORD_SIZE;
595 static void release_bts_buffer(int cpu)
597 struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
603 /* Clear the fixmap */
604 cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer;
605 ds_clear_cea(cea, BTS_BUFFER_SIZE);
606 dsfree_pages(hwev->ds_bts_vaddr, BTS_BUFFER_SIZE);
607 hwev->ds_bts_vaddr = NULL;
610 static int alloc_ds_buffer(int cpu)
612 struct debug_store *ds = &get_cpu_entry_area(cpu)->cpu_debug_store;
614 memset(ds, 0, sizeof(*ds));
615 per_cpu(cpu_hw_events, cpu).ds = ds;
619 static void release_ds_buffer(int cpu)
621 per_cpu(cpu_hw_events, cpu).ds = NULL;
624 void release_ds_buffers(void)
628 if (!x86_pmu.bts && !x86_pmu.pebs)
631 for_each_possible_cpu(cpu)
632 release_ds_buffer(cpu);
634 for_each_possible_cpu(cpu) {
636 * Again, ignore errors from offline CPUs, they will no longer
637 * observe cpu_hw_events.ds and not program the DS_AREA when
640 fini_debug_store_on_cpu(cpu);
643 for_each_possible_cpu(cpu) {
644 release_pebs_buffer(cpu);
645 release_bts_buffer(cpu);
649 void reserve_ds_buffers(void)
651 int bts_err = 0, pebs_err = 0;
654 x86_pmu.bts_active = 0;
655 x86_pmu.pebs_active = 0;
657 if (!x86_pmu.bts && !x86_pmu.pebs)
666 for_each_possible_cpu(cpu) {
667 if (alloc_ds_buffer(cpu)) {
672 if (!bts_err && alloc_bts_buffer(cpu))
675 if (!pebs_err && alloc_pebs_buffer(cpu))
678 if (bts_err && pebs_err)
683 for_each_possible_cpu(cpu)
684 release_bts_buffer(cpu);
688 for_each_possible_cpu(cpu)
689 release_pebs_buffer(cpu);
692 if (bts_err && pebs_err) {
693 for_each_possible_cpu(cpu)
694 release_ds_buffer(cpu);
696 if (x86_pmu.bts && !bts_err)
697 x86_pmu.bts_active = 1;
699 if (x86_pmu.pebs && !pebs_err)
700 x86_pmu.pebs_active = 1;
702 for_each_possible_cpu(cpu) {
704 * Ignores wrmsr_on_cpu() errors for offline CPUs they
705 * will get this call through intel_pmu_cpu_starting().
707 init_debug_store_on_cpu(cpu);
716 struct event_constraint bts_constraint =
717 EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0);
719 void intel_pmu_enable_bts(u64 config)
721 unsigned long debugctlmsr;
723 debugctlmsr = get_debugctlmsr();
725 debugctlmsr |= DEBUGCTLMSR_TR;
726 debugctlmsr |= DEBUGCTLMSR_BTS;
727 if (config & ARCH_PERFMON_EVENTSEL_INT)
728 debugctlmsr |= DEBUGCTLMSR_BTINT;
730 if (!(config & ARCH_PERFMON_EVENTSEL_OS))
731 debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
733 if (!(config & ARCH_PERFMON_EVENTSEL_USR))
734 debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR;
736 update_debugctlmsr(debugctlmsr);
739 void intel_pmu_disable_bts(void)
741 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
742 unsigned long debugctlmsr;
747 debugctlmsr = get_debugctlmsr();
750 ~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT |
751 DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR);
753 update_debugctlmsr(debugctlmsr);
756 int intel_pmu_drain_bts_buffer(void)
758 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
759 struct debug_store *ds = cpuc->ds;
765 struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
766 struct bts_record *at, *base, *top;
767 struct perf_output_handle handle;
768 struct perf_event_header header;
769 struct perf_sample_data data;
770 unsigned long skip = 0;
776 if (!x86_pmu.bts_active)
779 base = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
780 top = (struct bts_record *)(unsigned long)ds->bts_index;
785 memset(®s, 0, sizeof(regs));
787 ds->bts_index = ds->bts_buffer_base;
789 perf_sample_data_init(&data, 0, event->hw.last_period);
792 * BTS leaks kernel addresses in branches across the cpl boundary,
793 * such as traps or system calls, so unless the user is asking for
794 * kernel tracing (and right now it's not possible), we'd need to
795 * filter them out. But first we need to count how many of those we
796 * have in the current batch. This is an extra O(n) pass, however,
797 * it's much faster than the other one especially considering that
798 * n <= 2560 (BTS_BUFFER_SIZE / BTS_RECORD_SIZE * 15/16; see the
799 * alloc_bts_buffer()).
801 for (at = base; at < top; at++) {
803 * Note that right now *this* BTS code only works if
804 * attr::exclude_kernel is set, but let's keep this extra
805 * check here in case that changes.
807 if (event->attr.exclude_kernel &&
808 (kernel_ip(at->from) || kernel_ip(at->to)))
813 * Prepare a generic sample, i.e. fill in the invariant fields.
814 * We will overwrite the from and to address before we output
818 perf_prepare_sample(&data, event, ®s);
819 perf_prepare_header(&header, &data, event, ®s);
821 if (perf_output_begin(&handle, &data, event,
822 header.size * (top - base - skip)))
825 for (at = base; at < top; at++) {
826 /* Filter out any records that contain kernel addresses. */
827 if (event->attr.exclude_kernel &&
828 (kernel_ip(at->from) || kernel_ip(at->to)))
834 perf_output_sample(&handle, &header, &data, event);
837 perf_output_end(&handle);
839 /* There's new data available. */
840 event->hw.interrupts++;
841 event->pending_kill = POLL_IN;
847 static inline void intel_pmu_drain_pebs_buffer(void)
849 struct perf_sample_data data;
851 x86_pmu.drain_pebs(NULL, &data);
857 struct event_constraint intel_core2_pebs_event_constraints[] = {
858 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
859 INTEL_FLAGS_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
860 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
861 INTEL_FLAGS_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
862 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
863 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
864 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x01),
868 struct event_constraint intel_atom_pebs_event_constraints[] = {
869 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
870 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
871 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
872 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
873 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x01),
874 /* Allow all events as PEBS with no flags */
875 INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
879 struct event_constraint intel_slm_pebs_event_constraints[] = {
880 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
881 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x1),
882 /* Allow all events as PEBS with no flags */
883 INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
887 struct event_constraint intel_glm_pebs_event_constraints[] = {
888 /* Allow all events as PEBS with no flags */
889 INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
893 struct event_constraint intel_grt_pebs_event_constraints[] = {
894 /* Allow all events as PEBS with no flags */
895 INTEL_HYBRID_LAT_CONSTRAINT(0x5d0, 0x3),
896 INTEL_HYBRID_LAT_CONSTRAINT(0x6d0, 0xf),
900 struct event_constraint intel_nehalem_pebs_event_constraints[] = {
901 INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
902 INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
903 INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
904 INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INST_RETIRED.ANY */
905 INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
906 INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
907 INTEL_FLAGS_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
908 INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
909 INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
910 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
911 INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
912 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
913 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f),
917 struct event_constraint intel_westmere_pebs_event_constraints[] = {
918 INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
919 INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
920 INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
921 INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INSTR_RETIRED.* */
922 INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
923 INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
924 INTEL_FLAGS_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */
925 INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
926 INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
927 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
928 INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
929 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
930 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f),
934 struct event_constraint intel_snb_pebs_event_constraints[] = {
935 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
936 INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
937 INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
938 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
939 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf),
940 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
941 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
942 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
943 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
944 /* Allow all events as PEBS with no flags */
945 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
949 struct event_constraint intel_ivb_pebs_event_constraints[] = {
950 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
951 INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
952 INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
953 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
954 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf),
955 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
956 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2),
957 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
958 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
959 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
960 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
961 /* Allow all events as PEBS with no flags */
962 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
966 struct event_constraint intel_hsw_pebs_event_constraints[] = {
967 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
968 INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */
969 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
970 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf),
971 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
972 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2),
973 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
974 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
975 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
976 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
977 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
978 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
979 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
980 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
981 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
982 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
983 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
984 /* Allow all events as PEBS with no flags */
985 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
989 struct event_constraint intel_bdw_pebs_event_constraints[] = {
990 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
991 INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */
992 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
993 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf),
994 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
995 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2),
996 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
997 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
998 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
999 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
1000 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
1001 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
1002 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
1003 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
1004 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
1005 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
1006 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
1007 /* Allow all events as PEBS with no flags */
1008 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
1009 EVENT_CONSTRAINT_END
1013 struct event_constraint intel_skl_pebs_event_constraints[] = {
1014 INTEL_FLAGS_UEVENT_CONSTRAINT(0x1c0, 0x2), /* INST_RETIRED.PREC_DIST */
1015 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
1016 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2),
1017 /* INST_RETIRED.TOTAL_CYCLES_PS (inv=1, cmask=16) (cycles:p). */
1018 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f),
1019 INTEL_PLD_CONSTRAINT(0x1cd, 0xf), /* MEM_TRANS_RETIRED.* */
1020 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */
1021 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */
1022 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */
1023 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x22d0, 0xf), /* MEM_INST_RETIRED.LOCK_STORES */
1024 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */
1025 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */
1026 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */
1027 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */
1028 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_RETIRED.* */
1029 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_L3_HIT_RETIRED.* */
1030 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_L3_MISS_RETIRED.* */
1031 /* Allow all events as PEBS with no flags */
1032 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
1033 EVENT_CONSTRAINT_END
1036 struct event_constraint intel_icl_pebs_event_constraints[] = {
1037 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x100000000ULL), /* old INST_RETIRED.PREC_DIST */
1038 INTEL_FLAGS_UEVENT_CONSTRAINT(0x0100, 0x100000000ULL), /* INST_RETIRED.PREC_DIST */
1039 INTEL_FLAGS_UEVENT_CONSTRAINT(0x0400, 0x800000000ULL), /* SLOTS */
1041 INTEL_PLD_CONSTRAINT(0x1cd, 0xff), /* MEM_TRANS_RETIRED.LOAD_LATENCY */
1042 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */
1043 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */
1044 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */
1045 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */
1046 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */
1047 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */
1048 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */
1050 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD_RANGE(0xd1, 0xd4, 0xf), /* MEM_LOAD_*_RETIRED.* */
1052 INTEL_FLAGS_EVENT_CONSTRAINT(0xd0, 0xf), /* MEM_INST_RETIRED.* */
1055 * Everything else is handled by PMU_FL_PEBS_ALL, because we
1056 * need the full constraints from the main table.
1059 EVENT_CONSTRAINT_END
1062 struct event_constraint intel_glc_pebs_event_constraints[] = {
1063 INTEL_FLAGS_UEVENT_CONSTRAINT(0x100, 0x100000000ULL), /* INST_RETIRED.PREC_DIST */
1064 INTEL_FLAGS_UEVENT_CONSTRAINT(0x0400, 0x800000000ULL),
1066 INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xfe),
1067 INTEL_PLD_CONSTRAINT(0x1cd, 0xfe),
1068 INTEL_PSD_CONSTRAINT(0x2cd, 0x1),
1069 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */
1070 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */
1071 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */
1072 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */
1073 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */
1074 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */
1075 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */
1077 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD_RANGE(0xd1, 0xd4, 0xf),
1079 INTEL_FLAGS_EVENT_CONSTRAINT(0xd0, 0xf),
1082 * Everything else is handled by PMU_FL_PEBS_ALL, because we
1083 * need the full constraints from the main table.
1086 EVENT_CONSTRAINT_END
1089 struct event_constraint *intel_pebs_constraints(struct perf_event *event)
1091 struct event_constraint *pebs_constraints = hybrid(event->pmu, pebs_constraints);
1092 struct event_constraint *c;
1094 if (!event->attr.precise_ip)
1097 if (pebs_constraints) {
1098 for_each_event_constraint(c, pebs_constraints) {
1099 if (constraint_match(c, event->hw.config)) {
1100 event->hw.flags |= c->flags;
1107 * Extended PEBS support
1108 * Makes the PEBS code search the normal constraints.
1110 if (x86_pmu.flags & PMU_FL_PEBS_ALL)
1113 return &emptyconstraint;
1117 * We need the sched_task callback even for per-cpu events when we use
1118 * the large interrupt threshold, such that we can provide PID and TID
1121 static inline bool pebs_needs_sched_cb(struct cpu_hw_events *cpuc)
1123 if (cpuc->n_pebs == cpuc->n_pebs_via_pt)
1126 return cpuc->n_pebs && (cpuc->n_pebs == cpuc->n_large_pebs);
1129 void intel_pmu_pebs_sched_task(struct perf_event_pmu_context *pmu_ctx, bool sched_in)
1131 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1133 if (!sched_in && pebs_needs_sched_cb(cpuc))
1134 intel_pmu_drain_pebs_buffer();
1137 static inline void pebs_update_threshold(struct cpu_hw_events *cpuc)
1139 struct debug_store *ds = cpuc->ds;
1140 int max_pebs_events = hybrid(cpuc->pmu, max_pebs_events);
1141 int num_counters_fixed = hybrid(cpuc->pmu, num_counters_fixed);
1145 if (cpuc->n_pebs_via_pt)
1148 if (x86_pmu.flags & PMU_FL_PEBS_ALL)
1149 reserved = max_pebs_events + num_counters_fixed;
1151 reserved = max_pebs_events;
1153 if (cpuc->n_pebs == cpuc->n_large_pebs) {
1154 threshold = ds->pebs_absolute_maximum -
1155 reserved * cpuc->pebs_record_size;
1157 threshold = ds->pebs_buffer_base + cpuc->pebs_record_size;
1160 ds->pebs_interrupt_threshold = threshold;
1163 static void adaptive_pebs_record_size_update(void)
1165 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1166 u64 pebs_data_cfg = cpuc->pebs_data_cfg;
1167 int sz = sizeof(struct pebs_basic);
1169 if (pebs_data_cfg & PEBS_DATACFG_MEMINFO)
1170 sz += sizeof(struct pebs_meminfo);
1171 if (pebs_data_cfg & PEBS_DATACFG_GP)
1172 sz += sizeof(struct pebs_gprs);
1173 if (pebs_data_cfg & PEBS_DATACFG_XMMS)
1174 sz += sizeof(struct pebs_xmm);
1175 if (pebs_data_cfg & PEBS_DATACFG_LBRS)
1176 sz += x86_pmu.lbr_nr * sizeof(struct lbr_entry);
1178 cpuc->pebs_record_size = sz;
1181 #define PERF_PEBS_MEMINFO_TYPE (PERF_SAMPLE_ADDR | PERF_SAMPLE_DATA_SRC | \
1182 PERF_SAMPLE_PHYS_ADDR | \
1183 PERF_SAMPLE_WEIGHT_TYPE | \
1184 PERF_SAMPLE_TRANSACTION | \
1185 PERF_SAMPLE_DATA_PAGE_SIZE)
1187 static u64 pebs_update_adaptive_cfg(struct perf_event *event)
1189 struct perf_event_attr *attr = &event->attr;
1190 u64 sample_type = attr->sample_type;
1191 u64 pebs_data_cfg = 0;
1192 bool gprs, tsx_weight;
1194 if (!(sample_type & ~(PERF_SAMPLE_IP|PERF_SAMPLE_TIME)) &&
1195 attr->precise_ip > 1)
1196 return pebs_data_cfg;
1198 if (sample_type & PERF_PEBS_MEMINFO_TYPE)
1199 pebs_data_cfg |= PEBS_DATACFG_MEMINFO;
1202 * We need GPRs when:
1203 * + user requested them
1204 * + precise_ip < 2 for the non event IP
1205 * + For RTM TSX weight we need GPRs for the abort code.
1207 gprs = (sample_type & PERF_SAMPLE_REGS_INTR) &&
1208 (attr->sample_regs_intr & PEBS_GP_REGS);
1210 tsx_weight = (sample_type & PERF_SAMPLE_WEIGHT_TYPE) &&
1211 ((attr->config & INTEL_ARCH_EVENT_MASK) ==
1212 x86_pmu.rtm_abort_event);
1214 if (gprs || (attr->precise_ip < 2) || tsx_weight)
1215 pebs_data_cfg |= PEBS_DATACFG_GP;
1217 if ((sample_type & PERF_SAMPLE_REGS_INTR) &&
1218 (attr->sample_regs_intr & PERF_REG_EXTENDED_MASK))
1219 pebs_data_cfg |= PEBS_DATACFG_XMMS;
1221 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
1223 * For now always log all LBRs. Could configure this
1226 pebs_data_cfg |= PEBS_DATACFG_LBRS |
1227 ((x86_pmu.lbr_nr-1) << PEBS_DATACFG_LBR_SHIFT);
1230 return pebs_data_cfg;
1234 pebs_update_state(bool needed_cb, struct cpu_hw_events *cpuc,
1235 struct perf_event *event, bool add)
1237 struct pmu *pmu = event->pmu;
1240 * Make sure we get updated with the first PEBS event.
1241 * During removal, ->pebs_data_cfg is still valid for
1242 * the last PEBS event. Don't clear it.
1244 if ((cpuc->n_pebs == 1) && add)
1245 cpuc->pebs_data_cfg = PEBS_UPDATE_DS_SW;
1247 if (needed_cb != pebs_needs_sched_cb(cpuc)) {
1249 perf_sched_cb_inc(pmu);
1251 perf_sched_cb_dec(pmu);
1253 cpuc->pebs_data_cfg |= PEBS_UPDATE_DS_SW;
1257 * The PEBS record doesn't shrink on pmu::del(). Doing so would require
1258 * iterating all remaining PEBS events to reconstruct the config.
1260 if (x86_pmu.intel_cap.pebs_baseline && add) {
1263 pebs_data_cfg = pebs_update_adaptive_cfg(event);
1265 * Be sure to update the thresholds when we change the record.
1267 if (pebs_data_cfg & ~cpuc->pebs_data_cfg)
1268 cpuc->pebs_data_cfg |= pebs_data_cfg | PEBS_UPDATE_DS_SW;
1272 void intel_pmu_pebs_add(struct perf_event *event)
1274 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1275 struct hw_perf_event *hwc = &event->hw;
1276 bool needed_cb = pebs_needs_sched_cb(cpuc);
1279 if (hwc->flags & PERF_X86_EVENT_LARGE_PEBS)
1280 cpuc->n_large_pebs++;
1281 if (hwc->flags & PERF_X86_EVENT_PEBS_VIA_PT)
1282 cpuc->n_pebs_via_pt++;
1284 pebs_update_state(needed_cb, cpuc, event, true);
1287 static void intel_pmu_pebs_via_pt_disable(struct perf_event *event)
1289 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1291 if (!is_pebs_pt(event))
1294 if (!(cpuc->pebs_enabled & ~PEBS_VIA_PT_MASK))
1295 cpuc->pebs_enabled &= ~PEBS_VIA_PT_MASK;
1298 static void intel_pmu_pebs_via_pt_enable(struct perf_event *event)
1300 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1301 struct hw_perf_event *hwc = &event->hw;
1302 struct debug_store *ds = cpuc->ds;
1303 u64 value = ds->pebs_event_reset[hwc->idx];
1304 u32 base = MSR_RELOAD_PMC0;
1305 unsigned int idx = hwc->idx;
1307 if (!is_pebs_pt(event))
1310 if (!(event->hw.flags & PERF_X86_EVENT_LARGE_PEBS))
1311 cpuc->pebs_enabled |= PEBS_PMI_AFTER_EACH_RECORD;
1313 cpuc->pebs_enabled |= PEBS_OUTPUT_PT;
1315 if (hwc->idx >= INTEL_PMC_IDX_FIXED) {
1316 base = MSR_RELOAD_FIXED_CTR0;
1317 idx = hwc->idx - INTEL_PMC_IDX_FIXED;
1318 if (x86_pmu.intel_cap.pebs_format < 5)
1319 value = ds->pebs_event_reset[MAX_PEBS_EVENTS_FMT4 + idx];
1321 value = ds->pebs_event_reset[MAX_PEBS_EVENTS + idx];
1323 wrmsrl(base + idx, value);
1326 static inline void intel_pmu_drain_large_pebs(struct cpu_hw_events *cpuc)
1328 if (cpuc->n_pebs == cpuc->n_large_pebs &&
1329 cpuc->n_pebs != cpuc->n_pebs_via_pt)
1330 intel_pmu_drain_pebs_buffer();
1333 void intel_pmu_pebs_enable(struct perf_event *event)
1335 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1336 u64 pebs_data_cfg = cpuc->pebs_data_cfg & ~PEBS_UPDATE_DS_SW;
1337 struct hw_perf_event *hwc = &event->hw;
1338 struct debug_store *ds = cpuc->ds;
1339 unsigned int idx = hwc->idx;
1341 hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;
1343 cpuc->pebs_enabled |= 1ULL << hwc->idx;
1345 if ((event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT) && (x86_pmu.version < 5))
1346 cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32);
1347 else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
1348 cpuc->pebs_enabled |= 1ULL << 63;
1350 if (x86_pmu.intel_cap.pebs_baseline) {
1351 hwc->config |= ICL_EVENTSEL_ADAPTIVE;
1352 if (pebs_data_cfg != cpuc->active_pebs_data_cfg) {
1354 * drain_pebs() assumes uniform record size;
1355 * hence we need to drain when changing said
1358 intel_pmu_drain_large_pebs(cpuc);
1359 adaptive_pebs_record_size_update();
1360 wrmsrl(MSR_PEBS_DATA_CFG, pebs_data_cfg);
1361 cpuc->active_pebs_data_cfg = pebs_data_cfg;
1364 if (cpuc->pebs_data_cfg & PEBS_UPDATE_DS_SW) {
1365 cpuc->pebs_data_cfg = pebs_data_cfg;
1366 pebs_update_threshold(cpuc);
1369 if (idx >= INTEL_PMC_IDX_FIXED) {
1370 if (x86_pmu.intel_cap.pebs_format < 5)
1371 idx = MAX_PEBS_EVENTS_FMT4 + (idx - INTEL_PMC_IDX_FIXED);
1373 idx = MAX_PEBS_EVENTS + (idx - INTEL_PMC_IDX_FIXED);
1377 * Use auto-reload if possible to save a MSR write in the PMI.
1378 * This must be done in pmu::start(), because PERF_EVENT_IOC_PERIOD.
1380 if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
1381 ds->pebs_event_reset[idx] =
1382 (u64)(-hwc->sample_period) & x86_pmu.cntval_mask;
1384 ds->pebs_event_reset[idx] = 0;
1387 intel_pmu_pebs_via_pt_enable(event);
1390 void intel_pmu_pebs_del(struct perf_event *event)
1392 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1393 struct hw_perf_event *hwc = &event->hw;
1394 bool needed_cb = pebs_needs_sched_cb(cpuc);
1397 if (hwc->flags & PERF_X86_EVENT_LARGE_PEBS)
1398 cpuc->n_large_pebs--;
1399 if (hwc->flags & PERF_X86_EVENT_PEBS_VIA_PT)
1400 cpuc->n_pebs_via_pt--;
1402 pebs_update_state(needed_cb, cpuc, event, false);
1405 void intel_pmu_pebs_disable(struct perf_event *event)
1407 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1408 struct hw_perf_event *hwc = &event->hw;
1410 intel_pmu_drain_large_pebs(cpuc);
1412 cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
1414 if ((event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT) &&
1415 (x86_pmu.version < 5))
1416 cpuc->pebs_enabled &= ~(1ULL << (hwc->idx + 32));
1417 else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
1418 cpuc->pebs_enabled &= ~(1ULL << 63);
1420 intel_pmu_pebs_via_pt_disable(event);
1423 wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
1425 hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
1428 void intel_pmu_pebs_enable_all(void)
1430 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1432 if (cpuc->pebs_enabled)
1433 wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
1436 void intel_pmu_pebs_disable_all(void)
1438 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1440 if (cpuc->pebs_enabled)
1441 __intel_pmu_pebs_disable_all();
1444 static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
1446 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1447 unsigned long from = cpuc->lbr_entries[0].from;
1448 unsigned long old_to, to = cpuc->lbr_entries[0].to;
1449 unsigned long ip = regs->ip;
1455 * We don't need to fixup if the PEBS assist is fault like
1457 if (!x86_pmu.intel_cap.pebs_trap)
1461 * No LBR entry, no basic block, no rewinding
1463 if (!cpuc->lbr_stack.nr || !from || !to)
1467 * Basic blocks should never cross user/kernel boundaries
1469 if (kernel_ip(ip) != kernel_ip(to))
1473 * unsigned math, either ip is before the start (impossible) or
1474 * the basic block is larger than 1 page (sanity)
1476 if ((ip - to) > PEBS_FIXUP_SIZE)
1480 * We sampled a branch insn, rewind using the LBR stack
1483 set_linear_ip(regs, from);
1488 if (!kernel_ip(ip)) {
1490 u8 *buf = this_cpu_read(insn_buffer);
1492 /* 'size' must fit our buffer, see above */
1493 bytes = copy_from_user_nmi(buf, (void __user *)to, size);
1507 #ifdef CONFIG_X86_64
1508 is_64bit = kernel_ip(to) || any_64bit_mode(regs);
1510 insn_init(&insn, kaddr, size, is_64bit);
1513 * Make sure there was not a problem decoding the instruction.
1514 * This is doubly important because we have an infinite loop if
1517 if (insn_get_length(&insn))
1521 kaddr += insn.length;
1522 size -= insn.length;
1526 set_linear_ip(regs, old_to);
1531 * Even though we decoded the basic block, the instruction stream
1532 * never matched the given IP, either the TO or the IP got corrupted.
1537 static inline u64 intel_get_tsx_weight(u64 tsx_tuning)
1540 union hsw_tsx_tuning tsx = { .value = tsx_tuning };
1541 return tsx.cycles_last_block;
1546 static inline u64 intel_get_tsx_transaction(u64 tsx_tuning, u64 ax)
1548 u64 txn = (tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32;
1550 /* For RTM XABORTs also log the abort code from AX */
1551 if ((txn & PERF_TXN_TRANSACTION) && (ax & 1))
1552 txn |= ((ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
1556 static inline u64 get_pebs_status(void *n)
1558 if (x86_pmu.intel_cap.pebs_format < 4)
1559 return ((struct pebs_record_nhm *)n)->status;
1560 return ((struct pebs_basic *)n)->applicable_counters;
1563 #define PERF_X86_EVENT_PEBS_HSW_PREC \
1564 (PERF_X86_EVENT_PEBS_ST_HSW | \
1565 PERF_X86_EVENT_PEBS_LD_HSW | \
1566 PERF_X86_EVENT_PEBS_NA_HSW)
1568 static u64 get_data_src(struct perf_event *event, u64 aux)
1570 u64 val = PERF_MEM_NA;
1571 int fl = event->hw.flags;
1572 bool fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC);
1574 if (fl & PERF_X86_EVENT_PEBS_LDLAT)
1575 val = load_latency_data(event, aux);
1576 else if (fl & PERF_X86_EVENT_PEBS_STLAT)
1577 val = store_latency_data(event, aux);
1578 else if (fl & PERF_X86_EVENT_PEBS_LAT_HYBRID)
1579 val = x86_pmu.pebs_latency_data(event, aux);
1580 else if (fst && (fl & PERF_X86_EVENT_PEBS_HSW_PREC))
1581 val = precise_datala_hsw(event, aux);
1583 val = precise_store_data(aux);
1587 static void setup_pebs_time(struct perf_event *event,
1588 struct perf_sample_data *data,
1591 /* Converting to a user-defined clock is not supported yet. */
1592 if (event->attr.use_clockid != 0)
1596 * Doesn't support the conversion when the TSC is unstable.
1597 * The TSC unstable case is a corner case and very unlikely to
1598 * happen. If it happens, the TSC in a PEBS record will be
1599 * dropped and fall back to perf_event_clock().
1601 if (!using_native_sched_clock() || !sched_clock_stable())
1604 data->time = native_sched_clock_from_tsc(tsc) + __sched_clock_offset;
1605 data->sample_flags |= PERF_SAMPLE_TIME;
1608 #define PERF_SAMPLE_ADDR_TYPE (PERF_SAMPLE_ADDR | \
1609 PERF_SAMPLE_PHYS_ADDR | \
1610 PERF_SAMPLE_DATA_PAGE_SIZE)
1612 static void setup_pebs_fixed_sample_data(struct perf_event *event,
1613 struct pt_regs *iregs, void *__pebs,
1614 struct perf_sample_data *data,
1615 struct pt_regs *regs)
1618 * We cast to the biggest pebs_record but are careful not to
1619 * unconditionally access the 'extra' entries.
1621 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1622 struct pebs_record_skl *pebs = __pebs;
1629 sample_type = event->attr.sample_type;
1630 fll = event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT;
1632 perf_sample_data_init(data, 0, event->hw.last_period);
1634 data->period = event->hw.last_period;
1637 * Use latency for weight (only avail with PEBS-LL)
1639 if (fll && (sample_type & PERF_SAMPLE_WEIGHT_TYPE)) {
1640 data->weight.full = pebs->lat;
1641 data->sample_flags |= PERF_SAMPLE_WEIGHT_TYPE;
1645 * data.data_src encodes the data source
1647 if (sample_type & PERF_SAMPLE_DATA_SRC) {
1648 data->data_src.val = get_data_src(event, pebs->dse);
1649 data->sample_flags |= PERF_SAMPLE_DATA_SRC;
1653 * We must however always use iregs for the unwinder to stay sane; the
1654 * record BP,SP,IP can point into thin air when the record is from a
1655 * previous PMI context or an (I)RET happened between the record and
1658 if (sample_type & PERF_SAMPLE_CALLCHAIN)
1659 perf_sample_save_callchain(data, event, iregs);
1662 * We use the interrupt regs as a base because the PEBS record does not
1663 * contain a full regs set, specifically it seems to lack segment
1664 * descriptors, which get used by things like user_mode().
1666 * In the simple case fix up only the IP for PERF_SAMPLE_IP.
1671 * Initialize regs_>flags from PEBS,
1672 * Clear exact bit (which uses x86 EFLAGS Reserved bit 3),
1673 * i.e., do not rely on it being zero:
1675 regs->flags = pebs->flags & ~PERF_EFLAGS_EXACT;
1677 if (sample_type & PERF_SAMPLE_REGS_INTR) {
1678 regs->ax = pebs->ax;
1679 regs->bx = pebs->bx;
1680 regs->cx = pebs->cx;
1681 regs->dx = pebs->dx;
1682 regs->si = pebs->si;
1683 regs->di = pebs->di;
1685 regs->bp = pebs->bp;
1686 regs->sp = pebs->sp;
1688 #ifndef CONFIG_X86_32
1689 regs->r8 = pebs->r8;
1690 regs->r9 = pebs->r9;
1691 regs->r10 = pebs->r10;
1692 regs->r11 = pebs->r11;
1693 regs->r12 = pebs->r12;
1694 regs->r13 = pebs->r13;
1695 regs->r14 = pebs->r14;
1696 regs->r15 = pebs->r15;
1700 if (event->attr.precise_ip > 1) {
1702 * Haswell and later processors have an 'eventing IP'
1703 * (real IP) which fixes the off-by-1 skid in hardware.
1704 * Use it when precise_ip >= 2 :
1706 if (x86_pmu.intel_cap.pebs_format >= 2) {
1707 set_linear_ip(regs, pebs->real_ip);
1708 regs->flags |= PERF_EFLAGS_EXACT;
1710 /* Otherwise, use PEBS off-by-1 IP: */
1711 set_linear_ip(regs, pebs->ip);
1714 * With precise_ip >= 2, try to fix up the off-by-1 IP
1715 * using the LBR. If successful, the fixup function
1716 * corrects regs->ip and calls set_linear_ip() on regs:
1718 if (intel_pmu_pebs_fixup_ip(regs))
1719 regs->flags |= PERF_EFLAGS_EXACT;
1723 * When precise_ip == 1, return the PEBS off-by-1 IP,
1724 * no fixup attempted:
1726 set_linear_ip(regs, pebs->ip);
1730 if ((sample_type & PERF_SAMPLE_ADDR_TYPE) &&
1731 x86_pmu.intel_cap.pebs_format >= 1) {
1732 data->addr = pebs->dla;
1733 data->sample_flags |= PERF_SAMPLE_ADDR;
1736 if (x86_pmu.intel_cap.pebs_format >= 2) {
1737 /* Only set the TSX weight when no memory weight. */
1738 if ((sample_type & PERF_SAMPLE_WEIGHT_TYPE) && !fll) {
1739 data->weight.full = intel_get_tsx_weight(pebs->tsx_tuning);
1740 data->sample_flags |= PERF_SAMPLE_WEIGHT_TYPE;
1742 if (sample_type & PERF_SAMPLE_TRANSACTION) {
1743 data->txn = intel_get_tsx_transaction(pebs->tsx_tuning,
1745 data->sample_flags |= PERF_SAMPLE_TRANSACTION;
1750 * v3 supplies an accurate time stamp, so we use that
1751 * for the time stamp.
1753 * We can only do this for the default trace clock.
1755 if (x86_pmu.intel_cap.pebs_format >= 3)
1756 setup_pebs_time(event, data, pebs->tsc);
1758 if (has_branch_stack(event))
1759 perf_sample_save_brstack(data, event, &cpuc->lbr_stack, NULL);
1762 static void adaptive_pebs_save_regs(struct pt_regs *regs,
1763 struct pebs_gprs *gprs)
1765 regs->ax = gprs->ax;
1766 regs->bx = gprs->bx;
1767 regs->cx = gprs->cx;
1768 regs->dx = gprs->dx;
1769 regs->si = gprs->si;
1770 regs->di = gprs->di;
1771 regs->bp = gprs->bp;
1772 regs->sp = gprs->sp;
1773 #ifndef CONFIG_X86_32
1774 regs->r8 = gprs->r8;
1775 regs->r9 = gprs->r9;
1776 regs->r10 = gprs->r10;
1777 regs->r11 = gprs->r11;
1778 regs->r12 = gprs->r12;
1779 regs->r13 = gprs->r13;
1780 regs->r14 = gprs->r14;
1781 regs->r15 = gprs->r15;
1785 #define PEBS_LATENCY_MASK 0xffff
1786 #define PEBS_CACHE_LATENCY_OFFSET 32
1787 #define PEBS_RETIRE_LATENCY_OFFSET 32
1790 * With adaptive PEBS the layout depends on what fields are configured.
1793 static void setup_pebs_adaptive_sample_data(struct perf_event *event,
1794 struct pt_regs *iregs, void *__pebs,
1795 struct perf_sample_data *data,
1796 struct pt_regs *regs)
1798 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1799 struct pebs_basic *basic = __pebs;
1800 void *next_record = basic + 1;
1803 struct pebs_meminfo *meminfo = NULL;
1804 struct pebs_gprs *gprs = NULL;
1805 struct x86_perf_regs *perf_regs;
1810 perf_regs = container_of(regs, struct x86_perf_regs, regs);
1811 perf_regs->xmm_regs = NULL;
1813 sample_type = event->attr.sample_type;
1814 format_size = basic->format_size;
1815 perf_sample_data_init(data, 0, event->hw.last_period);
1816 data->period = event->hw.last_period;
1818 setup_pebs_time(event, data, basic->tsc);
1821 * We must however always use iregs for the unwinder to stay sane; the
1822 * record BP,SP,IP can point into thin air when the record is from a
1823 * previous PMI context or an (I)RET happened between the record and
1826 if (sample_type & PERF_SAMPLE_CALLCHAIN)
1827 perf_sample_save_callchain(data, event, iregs);
1830 /* The ip in basic is EventingIP */
1831 set_linear_ip(regs, basic->ip);
1832 regs->flags = PERF_EFLAGS_EXACT;
1834 if ((sample_type & PERF_SAMPLE_WEIGHT_STRUCT) && (x86_pmu.flags & PMU_FL_RETIRE_LATENCY))
1835 data->weight.var3_w = format_size >> PEBS_RETIRE_LATENCY_OFFSET & PEBS_LATENCY_MASK;
1838 * The record for MEMINFO is in front of GP
1839 * But PERF_SAMPLE_TRANSACTION needs gprs->ax.
1840 * Save the pointer here but process later.
1842 if (format_size & PEBS_DATACFG_MEMINFO) {
1843 meminfo = next_record;
1844 next_record = meminfo + 1;
1847 if (format_size & PEBS_DATACFG_GP) {
1849 next_record = gprs + 1;
1851 if (event->attr.precise_ip < 2) {
1852 set_linear_ip(regs, gprs->ip);
1853 regs->flags &= ~PERF_EFLAGS_EXACT;
1856 if (sample_type & PERF_SAMPLE_REGS_INTR)
1857 adaptive_pebs_save_regs(regs, gprs);
1860 if (format_size & PEBS_DATACFG_MEMINFO) {
1861 if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) {
1862 u64 weight = meminfo->latency;
1864 if (x86_pmu.flags & PMU_FL_INSTR_LATENCY) {
1865 data->weight.var2_w = weight & PEBS_LATENCY_MASK;
1866 weight >>= PEBS_CACHE_LATENCY_OFFSET;
1870 * Although meminfo::latency is defined as a u64,
1871 * only the lower 32 bits include the valid data
1872 * in practice on Ice Lake and earlier platforms.
1874 if (sample_type & PERF_SAMPLE_WEIGHT) {
1875 data->weight.full = weight ?:
1876 intel_get_tsx_weight(meminfo->tsx_tuning);
1878 data->weight.var1_dw = (u32)(weight & PEBS_LATENCY_MASK) ?:
1879 intel_get_tsx_weight(meminfo->tsx_tuning);
1881 data->sample_flags |= PERF_SAMPLE_WEIGHT_TYPE;
1884 if (sample_type & PERF_SAMPLE_DATA_SRC) {
1885 data->data_src.val = get_data_src(event, meminfo->aux);
1886 data->sample_flags |= PERF_SAMPLE_DATA_SRC;
1889 if (sample_type & PERF_SAMPLE_ADDR_TYPE) {
1890 data->addr = meminfo->address;
1891 data->sample_flags |= PERF_SAMPLE_ADDR;
1894 if (sample_type & PERF_SAMPLE_TRANSACTION) {
1895 data->txn = intel_get_tsx_transaction(meminfo->tsx_tuning,
1896 gprs ? gprs->ax : 0);
1897 data->sample_flags |= PERF_SAMPLE_TRANSACTION;
1901 if (format_size & PEBS_DATACFG_XMMS) {
1902 struct pebs_xmm *xmm = next_record;
1904 next_record = xmm + 1;
1905 perf_regs->xmm_regs = xmm->xmm;
1908 if (format_size & PEBS_DATACFG_LBRS) {
1909 struct lbr_entry *lbr = next_record;
1910 int num_lbr = ((format_size >> PEBS_DATACFG_LBR_SHIFT)
1912 next_record = next_record + num_lbr * sizeof(struct lbr_entry);
1914 if (has_branch_stack(event)) {
1915 intel_pmu_store_pebs_lbrs(lbr);
1916 intel_pmu_lbr_save_brstack(data, cpuc, event);
1920 WARN_ONCE(next_record != __pebs + (format_size >> 48),
1921 "PEBS record size %llu, expected %llu, config %llx\n",
1923 (u64)(next_record - __pebs),
1924 basic->format_size);
1927 static inline void *
1928 get_next_pebs_record_by_bit(void *base, void *top, int bit)
1930 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1935 * fmt0 does not have a status bitfield (does not use
1936 * perf_record_nhm format)
1938 if (x86_pmu.intel_cap.pebs_format < 1)
1944 for (at = base; at < top; at += cpuc->pebs_record_size) {
1945 unsigned long status = get_pebs_status(at);
1947 if (test_bit(bit, (unsigned long *)&status)) {
1948 /* PEBS v3 has accurate status bits */
1949 if (x86_pmu.intel_cap.pebs_format >= 3)
1952 if (status == (1 << bit))
1955 /* clear non-PEBS bit and re-check */
1956 pebs_status = status & cpuc->pebs_enabled;
1957 pebs_status &= PEBS_COUNTER_MASK;
1958 if (pebs_status == (1 << bit))
1965 void intel_pmu_auto_reload_read(struct perf_event *event)
1967 WARN_ON(!(event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD));
1969 perf_pmu_disable(event->pmu);
1970 intel_pmu_drain_pebs_buffer();
1971 perf_pmu_enable(event->pmu);
1975 * Special variant of intel_pmu_save_and_restart() for auto-reload.
1978 intel_pmu_save_and_restart_reload(struct perf_event *event, int count)
1980 struct hw_perf_event *hwc = &event->hw;
1981 int shift = 64 - x86_pmu.cntval_bits;
1982 u64 period = hwc->sample_period;
1983 u64 prev_raw_count, new_raw_count;
1989 * drain_pebs() only happens when the PMU is disabled.
1991 WARN_ON(this_cpu_read(cpu_hw_events.enabled));
1993 prev_raw_count = local64_read(&hwc->prev_count);
1994 rdpmcl(hwc->event_base_rdpmc, new_raw_count);
1995 local64_set(&hwc->prev_count, new_raw_count);
1998 * Since the counter increments a negative counter value and
1999 * overflows on the sign switch, giving the interval:
2003 * the difference between two consecutive reads is:
2005 * A) value2 - value1;
2006 * when no overflows have happened in between,
2008 * B) (0 - value1) + (value2 - (-period));
2009 * when one overflow happened in between,
2011 * C) (0 - value1) + (n - 1) * (period) + (value2 - (-period));
2012 * when @n overflows happened in between.
2014 * Here A) is the obvious difference, B) is the extension to the
2015 * discrete interval, where the first term is to the top of the
2016 * interval and the second term is from the bottom of the next
2017 * interval and C) the extension to multiple intervals, where the
2018 * middle term is the whole intervals covered.
2020 * An equivalent of C, by reduction, is:
2022 * value2 - value1 + n * period
2024 new = ((s64)(new_raw_count << shift) >> shift);
2025 old = ((s64)(prev_raw_count << shift) >> shift);
2026 local64_add(new - old + count * period, &event->count);
2028 local64_set(&hwc->period_left, -new);
2030 perf_event_update_userpage(event);
2035 static __always_inline void
2036 __intel_pmu_pebs_event(struct perf_event *event,
2037 struct pt_regs *iregs,
2038 struct perf_sample_data *data,
2039 void *base, void *top,
2041 void (*setup_sample)(struct perf_event *,
2044 struct perf_sample_data *,
2047 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2048 struct hw_perf_event *hwc = &event->hw;
2049 struct x86_perf_regs perf_regs;
2050 struct pt_regs *regs = &perf_regs.regs;
2051 void *at = get_next_pebs_record_by_bit(base, top, bit);
2052 static struct pt_regs dummy_iregs;
2054 if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
2056 * Now, auto-reload is only enabled in fixed period mode.
2057 * The reload value is always hwc->sample_period.
2058 * May need to change it, if auto-reload is enabled in
2061 intel_pmu_save_and_restart_reload(event, count);
2062 } else if (!intel_pmu_save_and_restart(event))
2066 iregs = &dummy_iregs;
2069 setup_sample(event, iregs, at, data, regs);
2070 perf_event_output(event, data, regs);
2071 at += cpuc->pebs_record_size;
2072 at = get_next_pebs_record_by_bit(at, top, bit);
2076 setup_sample(event, iregs, at, data, regs);
2077 if (iregs == &dummy_iregs) {
2079 * The PEBS records may be drained in the non-overflow context,
2080 * e.g., large PEBS + context switch. Perf should treat the
2081 * last record the same as other PEBS records, and doesn't
2082 * invoke the generic overflow handler.
2084 perf_event_output(event, data, regs);
2087 * All but the last records are processed.
2088 * The last one is left to be able to call the overflow handler.
2090 if (perf_event_overflow(event, data, regs))
2091 x86_pmu_stop(event, 0);
2095 static void intel_pmu_drain_pebs_core(struct pt_regs *iregs, struct perf_sample_data *data)
2097 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2098 struct debug_store *ds = cpuc->ds;
2099 struct perf_event *event = cpuc->events[0]; /* PMC0 only */
2100 struct pebs_record_core *at, *top;
2103 if (!x86_pmu.pebs_active)
2106 at = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base;
2107 top = (struct pebs_record_core *)(unsigned long)ds->pebs_index;
2110 * Whatever else happens, drain the thing
2112 ds->pebs_index = ds->pebs_buffer_base;
2114 if (!test_bit(0, cpuc->active_mask))
2117 WARN_ON_ONCE(!event);
2119 if (!event->attr.precise_ip)
2124 if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)
2125 intel_pmu_save_and_restart_reload(event, 0);
2129 __intel_pmu_pebs_event(event, iregs, data, at, top, 0, n,
2130 setup_pebs_fixed_sample_data);
2133 static void intel_pmu_pebs_event_update_no_drain(struct cpu_hw_events *cpuc, int size)
2135 struct perf_event *event;
2139 * The drain_pebs() could be called twice in a short period
2140 * for auto-reload event in pmu::read(). There are no
2141 * overflows have happened in between.
2142 * It needs to call intel_pmu_save_and_restart_reload() to
2143 * update the event->count for this case.
2145 for_each_set_bit(bit, (unsigned long *)&cpuc->pebs_enabled, size) {
2146 event = cpuc->events[bit];
2147 if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)
2148 intel_pmu_save_and_restart_reload(event, 0);
2152 static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs, struct perf_sample_data *data)
2154 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2155 struct debug_store *ds = cpuc->ds;
2156 struct perf_event *event;
2157 void *base, *at, *top;
2158 short counts[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {};
2159 short error[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {};
2163 if (!x86_pmu.pebs_active)
2166 base = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
2167 top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
2169 ds->pebs_index = ds->pebs_buffer_base;
2171 mask = (1ULL << x86_pmu.max_pebs_events) - 1;
2172 size = x86_pmu.max_pebs_events;
2173 if (x86_pmu.flags & PMU_FL_PEBS_ALL) {
2174 mask |= ((1ULL << x86_pmu.num_counters_fixed) - 1) << INTEL_PMC_IDX_FIXED;
2175 size = INTEL_PMC_IDX_FIXED + x86_pmu.num_counters_fixed;
2178 if (unlikely(base >= top)) {
2179 intel_pmu_pebs_event_update_no_drain(cpuc, size);
2183 for (at = base; at < top; at += x86_pmu.pebs_record_size) {
2184 struct pebs_record_nhm *p = at;
2187 pebs_status = p->status & cpuc->pebs_enabled;
2188 pebs_status &= mask;
2190 /* PEBS v3 has more accurate status bits */
2191 if (x86_pmu.intel_cap.pebs_format >= 3) {
2192 for_each_set_bit(bit, (unsigned long *)&pebs_status, size)
2199 * On some CPUs the PEBS status can be zero when PEBS is
2200 * racing with clearing of GLOBAL_STATUS.
2202 * Normally we would drop that record, but in the
2203 * case when there is only a single active PEBS event
2204 * we can assume it's for that event.
2206 if (!pebs_status && cpuc->pebs_enabled &&
2207 !(cpuc->pebs_enabled & (cpuc->pebs_enabled-1)))
2208 pebs_status = p->status = cpuc->pebs_enabled;
2210 bit = find_first_bit((unsigned long *)&pebs_status,
2211 x86_pmu.max_pebs_events);
2212 if (bit >= x86_pmu.max_pebs_events)
2216 * The PEBS hardware does not deal well with the situation
2217 * when events happen near to each other and multiple bits
2218 * are set. But it should happen rarely.
2220 * If these events include one PEBS and multiple non-PEBS
2221 * events, it doesn't impact PEBS record. The record will
2222 * be handled normally. (slow path)
2224 * If these events include two or more PEBS events, the
2225 * records for the events can be collapsed into a single
2226 * one, and it's not possible to reconstruct all events
2227 * that caused the PEBS record. It's called collision.
2228 * If collision happened, the record will be dropped.
2230 if (pebs_status != (1ULL << bit)) {
2231 for_each_set_bit(i, (unsigned long *)&pebs_status, size)
2239 for_each_set_bit(bit, (unsigned long *)&mask, size) {
2240 if ((counts[bit] == 0) && (error[bit] == 0))
2243 event = cpuc->events[bit];
2244 if (WARN_ON_ONCE(!event))
2247 if (WARN_ON_ONCE(!event->attr.precise_ip))
2250 /* log dropped samples number */
2252 perf_log_lost_samples(event, error[bit]);
2254 if (iregs && perf_event_account_interrupt(event))
2255 x86_pmu_stop(event, 0);
2259 __intel_pmu_pebs_event(event, iregs, data, base,
2260 top, bit, counts[bit],
2261 setup_pebs_fixed_sample_data);
2266 static void intel_pmu_drain_pebs_icl(struct pt_regs *iregs, struct perf_sample_data *data)
2268 short counts[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {};
2269 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2270 int max_pebs_events = hybrid(cpuc->pmu, max_pebs_events);
2271 int num_counters_fixed = hybrid(cpuc->pmu, num_counters_fixed);
2272 struct debug_store *ds = cpuc->ds;
2273 struct perf_event *event;
2274 void *base, *at, *top;
2278 if (!x86_pmu.pebs_active)
2281 base = (struct pebs_basic *)(unsigned long)ds->pebs_buffer_base;
2282 top = (struct pebs_basic *)(unsigned long)ds->pebs_index;
2284 ds->pebs_index = ds->pebs_buffer_base;
2286 mask = ((1ULL << max_pebs_events) - 1) |
2287 (((1ULL << num_counters_fixed) - 1) << INTEL_PMC_IDX_FIXED);
2288 size = INTEL_PMC_IDX_FIXED + num_counters_fixed;
2290 if (unlikely(base >= top)) {
2291 intel_pmu_pebs_event_update_no_drain(cpuc, size);
2295 for (at = base; at < top; at += cpuc->pebs_record_size) {
2298 pebs_status = get_pebs_status(at) & cpuc->pebs_enabled;
2299 pebs_status &= mask;
2301 for_each_set_bit(bit, (unsigned long *)&pebs_status, size)
2305 for_each_set_bit(bit, (unsigned long *)&mask, size) {
2306 if (counts[bit] == 0)
2309 event = cpuc->events[bit];
2310 if (WARN_ON_ONCE(!event))
2313 if (WARN_ON_ONCE(!event->attr.precise_ip))
2316 __intel_pmu_pebs_event(event, iregs, data, base,
2317 top, bit, counts[bit],
2318 setup_pebs_adaptive_sample_data);
2323 * BTS, PEBS probe and setup
2326 void __init intel_ds_init(void)
2329 * No support for 32bit formats
2331 if (!boot_cpu_has(X86_FEATURE_DTES64))
2334 x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS);
2335 x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS);
2336 x86_pmu.pebs_buffer_size = PEBS_BUFFER_SIZE;
2337 if (x86_pmu.version <= 4)
2338 x86_pmu.pebs_no_isolation = 1;
2341 char pebs_type = x86_pmu.intel_cap.pebs_trap ? '+' : '-';
2342 char *pebs_qual = "";
2343 int format = x86_pmu.intel_cap.pebs_format;
2346 x86_pmu.intel_cap.pebs_baseline = 0;
2350 pr_cont("PEBS fmt0%c, ", pebs_type);
2351 x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
2353 * Using >PAGE_SIZE buffers makes the WRMSR to
2354 * PERF_GLOBAL_CTRL in intel_pmu_enable_all()
2355 * mysteriously hang on Core2.
2357 * As a workaround, we don't do this.
2359 x86_pmu.pebs_buffer_size = PAGE_SIZE;
2360 x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
2364 pr_cont("PEBS fmt1%c, ", pebs_type);
2365 x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
2366 x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
2370 pr_cont("PEBS fmt2%c, ", pebs_type);
2371 x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw);
2372 x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
2376 pr_cont("PEBS fmt3%c, ", pebs_type);
2377 x86_pmu.pebs_record_size =
2378 sizeof(struct pebs_record_skl);
2379 x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
2380 x86_pmu.large_pebs_flags |= PERF_SAMPLE_TIME;
2384 x86_pmu.pebs_ept = 1;
2387 x86_pmu.drain_pebs = intel_pmu_drain_pebs_icl;
2388 x86_pmu.pebs_record_size = sizeof(struct pebs_basic);
2389 if (x86_pmu.intel_cap.pebs_baseline) {
2390 x86_pmu.large_pebs_flags |=
2391 PERF_SAMPLE_BRANCH_STACK |
2393 x86_pmu.flags |= PMU_FL_PEBS_ALL;
2394 x86_pmu.pebs_capable = ~0ULL;
2395 pebs_qual = "-baseline";
2396 x86_get_pmu(smp_processor_id())->capabilities |= PERF_PMU_CAP_EXTENDED_REGS;
2398 /* Only basic record supported */
2399 x86_pmu.large_pebs_flags &=
2400 ~(PERF_SAMPLE_ADDR |
2402 PERF_SAMPLE_DATA_SRC |
2403 PERF_SAMPLE_TRANSACTION |
2404 PERF_SAMPLE_REGS_USER |
2405 PERF_SAMPLE_REGS_INTR);
2407 pr_cont("PEBS fmt4%c%s, ", pebs_type, pebs_qual);
2409 if (!is_hybrid() && x86_pmu.intel_cap.pebs_output_pt_available) {
2410 pr_cont("PEBS-via-PT, ");
2411 x86_get_pmu(smp_processor_id())->capabilities |= PERF_PMU_CAP_AUX_OUTPUT;
2417 pr_cont("no PEBS fmt%d%c, ", format, pebs_type);
2423 void perf_restore_debug_store(void)
2425 struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
2427 if (!x86_pmu.bts && !x86_pmu.pebs)
2430 wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds);