Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | #include <linux/bitops.h> |
| 3 | #include <linux/types.h> |
| 4 | #include <linux/slab.h> |
| 5 | |
| 6 | #include <asm/cpu_entry_area.h> |
| 7 | #include <asm/perf_event.h> |
| 8 | #include <asm/tlbflush.h> |
| 9 | #include <asm/insn.h> |
| 10 | |
| 11 | #include "../perf_event.h" |
| 12 | |
| 13 | /* Waste a full page so it can be mapped into the cpu_entry_area */ |
| 14 | DEFINE_PER_CPU_PAGE_ALIGNED(struct debug_store, cpu_debug_store); |
| 15 | |
| 16 | /* The size of a BTS record in bytes: */ |
| 17 | #define BTS_RECORD_SIZE 24 |
| 18 | |
| 19 | #define PEBS_FIXUP_SIZE PAGE_SIZE |
| 20 | |
| 21 | /* |
| 22 | * pebs_record_32 for p4 and core not supported |
| 23 | |
| 24 | struct pebs_record_32 { |
| 25 | u32 flags, ip; |
| 26 | u32 ax, bc, cx, dx; |
| 27 | u32 si, di, bp, sp; |
| 28 | }; |
| 29 | |
| 30 | */ |
| 31 | |
| 32 | union intel_x86_pebs_dse { |
| 33 | u64 val; |
| 34 | struct { |
| 35 | unsigned int ld_dse:4; |
| 36 | unsigned int ld_stlb_miss:1; |
| 37 | unsigned int ld_locked:1; |
| 38 | unsigned int ld_reserved:26; |
| 39 | }; |
| 40 | struct { |
| 41 | unsigned int st_l1d_hit:1; |
| 42 | unsigned int st_reserved1:3; |
| 43 | unsigned int st_stlb_miss:1; |
| 44 | unsigned int st_locked:1; |
| 45 | unsigned int st_reserved2:26; |
| 46 | }; |
| 47 | }; |
| 48 | |
| 49 | |
| 50 | /* |
| 51 | * Map PEBS Load Latency Data Source encodings to generic |
| 52 | * memory data source information |
| 53 | */ |
| 54 | #define P(a, b) PERF_MEM_S(a, b) |
| 55 | #define OP_LH (P(OP, LOAD) | P(LVL, HIT)) |
| 56 | #define LEVEL(x) P(LVLNUM, x) |
| 57 | #define REM P(REMOTE, REMOTE) |
| 58 | #define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS)) |
| 59 | |
| 60 | /* Version for Sandy Bridge and later */ |
| 61 | static u64 pebs_data_source[] = { |
| 62 | P(OP, LOAD) | P(LVL, MISS) | LEVEL(L3) | P(SNOOP, NA),/* 0x00:ukn L3 */ |
| 63 | OP_LH | P(LVL, L1) | LEVEL(L1) | P(SNOOP, NONE), /* 0x01: L1 local */ |
| 64 | OP_LH | P(LVL, LFB) | LEVEL(LFB) | P(SNOOP, NONE), /* 0x02: LFB hit */ |
| 65 | OP_LH | P(LVL, L2) | LEVEL(L2) | P(SNOOP, NONE), /* 0x03: L2 hit */ |
| 66 | OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, NONE), /* 0x04: L3 hit */ |
| 67 | OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, MISS), /* 0x05: L3 hit, snoop miss */ |
| 68 | OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT), /* 0x06: L3 hit, snoop hit */ |
| 69 | OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM), /* 0x07: L3 hit, snoop hitm */ |
| 70 | OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HIT), /* 0x08: L3 miss snoop hit */ |
| 71 | OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HITM), /* 0x09: L3 miss snoop hitm*/ |
| 72 | OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | P(SNOOP, HIT), /* 0x0a: L3 miss, shared */ |
| 73 | OP_LH | P(LVL, REM_RAM1) | REM | LEVEL(L3) | P(SNOOP, HIT), /* 0x0b: L3 miss, shared */ |
| 74 | OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | SNOOP_NONE_MISS, /* 0x0c: L3 miss, excl */ |
| 75 | OP_LH | P(LVL, REM_RAM1) | LEVEL(RAM) | REM | SNOOP_NONE_MISS, /* 0x0d: L3 miss, excl */ |
| 76 | OP_LH | P(LVL, IO) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0e: I/O */ |
| 77 | OP_LH | P(LVL, UNC) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0f: uncached */ |
| 78 | }; |
| 79 | |
| 80 | /* Patch up minor differences in the bits */ |
| 81 | void __init intel_pmu_pebs_data_source_nhm(void) |
| 82 | { |
| 83 | pebs_data_source[0x05] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT); |
| 84 | pebs_data_source[0x06] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM); |
| 85 | pebs_data_source[0x07] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM); |
| 86 | } |
| 87 | |
| 88 | void __init intel_pmu_pebs_data_source_skl(bool pmem) |
| 89 | { |
| 90 | u64 pmem_or_l4 = pmem ? LEVEL(PMEM) : LEVEL(L4); |
| 91 | |
| 92 | pebs_data_source[0x08] = OP_LH | pmem_or_l4 | P(SNOOP, HIT); |
| 93 | pebs_data_source[0x09] = OP_LH | pmem_or_l4 | REM | P(SNOOP, HIT); |
| 94 | pebs_data_source[0x0b] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, NONE); |
| 95 | pebs_data_source[0x0c] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOPX, FWD); |
| 96 | pebs_data_source[0x0d] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOP, HITM); |
| 97 | } |
| 98 | |
| 99 | static u64 precise_store_data(u64 status) |
| 100 | { |
| 101 | union intel_x86_pebs_dse dse; |
| 102 | u64 val = P(OP, STORE) | P(SNOOP, NA) | P(LVL, L1) | P(TLB, L2); |
| 103 | |
| 104 | dse.val = status; |
| 105 | |
| 106 | /* |
| 107 | * bit 4: TLB access |
| 108 | * 1 = stored missed 2nd level TLB |
| 109 | * |
| 110 | * so it either hit the walker or the OS |
| 111 | * otherwise hit 2nd level TLB |
| 112 | */ |
| 113 | if (dse.st_stlb_miss) |
| 114 | val |= P(TLB, MISS); |
| 115 | else |
| 116 | val |= P(TLB, HIT); |
| 117 | |
| 118 | /* |
| 119 | * bit 0: hit L1 data cache |
| 120 | * if not set, then all we know is that |
| 121 | * it missed L1D |
| 122 | */ |
| 123 | if (dse.st_l1d_hit) |
| 124 | val |= P(LVL, HIT); |
| 125 | else |
| 126 | val |= P(LVL, MISS); |
| 127 | |
| 128 | /* |
| 129 | * bit 5: Locked prefix |
| 130 | */ |
| 131 | if (dse.st_locked) |
| 132 | val |= P(LOCK, LOCKED); |
| 133 | |
| 134 | return val; |
| 135 | } |
| 136 | |
| 137 | static u64 precise_datala_hsw(struct perf_event *event, u64 status) |
| 138 | { |
| 139 | union perf_mem_data_src dse; |
| 140 | |
| 141 | dse.val = PERF_MEM_NA; |
| 142 | |
| 143 | if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) |
| 144 | dse.mem_op = PERF_MEM_OP_STORE; |
| 145 | else if (event->hw.flags & PERF_X86_EVENT_PEBS_LD_HSW) |
| 146 | dse.mem_op = PERF_MEM_OP_LOAD; |
| 147 | |
| 148 | /* |
| 149 | * L1 info only valid for following events: |
| 150 | * |
| 151 | * MEM_UOPS_RETIRED.STLB_MISS_STORES |
| 152 | * MEM_UOPS_RETIRED.LOCK_STORES |
| 153 | * MEM_UOPS_RETIRED.SPLIT_STORES |
| 154 | * MEM_UOPS_RETIRED.ALL_STORES |
| 155 | */ |
| 156 | if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) { |
| 157 | if (status & 1) |
| 158 | dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_HIT; |
| 159 | else |
| 160 | dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_MISS; |
| 161 | } |
| 162 | return dse.val; |
| 163 | } |
| 164 | |
| 165 | static u64 load_latency_data(u64 status) |
| 166 | { |
| 167 | union intel_x86_pebs_dse dse; |
| 168 | u64 val; |
| 169 | |
| 170 | dse.val = status; |
| 171 | |
| 172 | /* |
| 173 | * use the mapping table for bit 0-3 |
| 174 | */ |
| 175 | val = pebs_data_source[dse.ld_dse]; |
| 176 | |
| 177 | /* |
| 178 | * Nehalem models do not support TLB, Lock infos |
| 179 | */ |
| 180 | if (x86_pmu.pebs_no_tlb) { |
| 181 | val |= P(TLB, NA) | P(LOCK, NA); |
| 182 | return val; |
| 183 | } |
| 184 | /* |
| 185 | * bit 4: TLB access |
| 186 | * 0 = did not miss 2nd level TLB |
| 187 | * 1 = missed 2nd level TLB |
| 188 | */ |
| 189 | if (dse.ld_stlb_miss) |
| 190 | val |= P(TLB, MISS) | P(TLB, L2); |
| 191 | else |
| 192 | val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2); |
| 193 | |
| 194 | /* |
| 195 | * bit 5: locked prefix |
| 196 | */ |
| 197 | if (dse.ld_locked) |
| 198 | val |= P(LOCK, LOCKED); |
| 199 | |
| 200 | return val; |
| 201 | } |
| 202 | |
| 203 | struct pebs_record_core { |
| 204 | u64 flags, ip; |
| 205 | u64 ax, bx, cx, dx; |
| 206 | u64 si, di, bp, sp; |
| 207 | u64 r8, r9, r10, r11; |
| 208 | u64 r12, r13, r14, r15; |
| 209 | }; |
| 210 | |
| 211 | struct pebs_record_nhm { |
| 212 | u64 flags, ip; |
| 213 | u64 ax, bx, cx, dx; |
| 214 | u64 si, di, bp, sp; |
| 215 | u64 r8, r9, r10, r11; |
| 216 | u64 r12, r13, r14, r15; |
| 217 | u64 status, dla, dse, lat; |
| 218 | }; |
| 219 | |
| 220 | /* |
| 221 | * Same as pebs_record_nhm, with two additional fields. |
| 222 | */ |
| 223 | struct pebs_record_hsw { |
| 224 | u64 flags, ip; |
| 225 | u64 ax, bx, cx, dx; |
| 226 | u64 si, di, bp, sp; |
| 227 | u64 r8, r9, r10, r11; |
| 228 | u64 r12, r13, r14, r15; |
| 229 | u64 status, dla, dse, lat; |
| 230 | u64 real_ip, tsx_tuning; |
| 231 | }; |
| 232 | |
| 233 | union hsw_tsx_tuning { |
| 234 | struct { |
| 235 | u32 cycles_last_block : 32, |
| 236 | hle_abort : 1, |
| 237 | rtm_abort : 1, |
| 238 | instruction_abort : 1, |
| 239 | non_instruction_abort : 1, |
| 240 | retry : 1, |
| 241 | data_conflict : 1, |
| 242 | capacity_writes : 1, |
| 243 | capacity_reads : 1; |
| 244 | }; |
| 245 | u64 value; |
| 246 | }; |
| 247 | |
| 248 | #define PEBS_HSW_TSX_FLAGS 0xff00000000ULL |
| 249 | |
| 250 | /* Same as HSW, plus TSC */ |
| 251 | |
| 252 | struct pebs_record_skl { |
| 253 | u64 flags, ip; |
| 254 | u64 ax, bx, cx, dx; |
| 255 | u64 si, di, bp, sp; |
| 256 | u64 r8, r9, r10, r11; |
| 257 | u64 r12, r13, r14, r15; |
| 258 | u64 status, dla, dse, lat; |
| 259 | u64 real_ip, tsx_tuning; |
| 260 | u64 tsc; |
| 261 | }; |
| 262 | |
| 263 | void init_debug_store_on_cpu(int cpu) |
| 264 | { |
| 265 | struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; |
| 266 | |
| 267 | if (!ds) |
| 268 | return; |
| 269 | |
| 270 | wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, |
| 271 | (u32)((u64)(unsigned long)ds), |
| 272 | (u32)((u64)(unsigned long)ds >> 32)); |
| 273 | } |
| 274 | |
| 275 | void fini_debug_store_on_cpu(int cpu) |
| 276 | { |
| 277 | if (!per_cpu(cpu_hw_events, cpu).ds) |
| 278 | return; |
| 279 | |
| 280 | wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0); |
| 281 | } |
| 282 | |
| 283 | static DEFINE_PER_CPU(void *, insn_buffer); |
| 284 | |
| 285 | static void ds_update_cea(void *cea, void *addr, size_t size, pgprot_t prot) |
| 286 | { |
| 287 | unsigned long start = (unsigned long)cea; |
| 288 | phys_addr_t pa; |
| 289 | size_t msz = 0; |
| 290 | |
| 291 | pa = virt_to_phys(addr); |
| 292 | |
| 293 | preempt_disable(); |
| 294 | for (; msz < size; msz += PAGE_SIZE, pa += PAGE_SIZE, cea += PAGE_SIZE) |
| 295 | cea_set_pte(cea, pa, prot); |
| 296 | |
| 297 | /* |
| 298 | * This is a cross-CPU update of the cpu_entry_area, we must shoot down |
| 299 | * all TLB entries for it. |
| 300 | */ |
| 301 | flush_tlb_kernel_range(start, start + size); |
| 302 | preempt_enable(); |
| 303 | } |
| 304 | |
| 305 | static void ds_clear_cea(void *cea, size_t size) |
| 306 | { |
| 307 | unsigned long start = (unsigned long)cea; |
| 308 | size_t msz = 0; |
| 309 | |
| 310 | preempt_disable(); |
| 311 | for (; msz < size; msz += PAGE_SIZE, cea += PAGE_SIZE) |
| 312 | cea_set_pte(cea, 0, PAGE_NONE); |
| 313 | |
| 314 | flush_tlb_kernel_range(start, start + size); |
| 315 | preempt_enable(); |
| 316 | } |
| 317 | |
| 318 | static void *dsalloc_pages(size_t size, gfp_t flags, int cpu) |
| 319 | { |
| 320 | unsigned int order = get_order(size); |
| 321 | int node = cpu_to_node(cpu); |
| 322 | struct page *page; |
| 323 | |
| 324 | page = __alloc_pages_node(node, flags | __GFP_ZERO, order); |
| 325 | return page ? page_address(page) : NULL; |
| 326 | } |
| 327 | |
| 328 | static void dsfree_pages(const void *buffer, size_t size) |
| 329 | { |
| 330 | if (buffer) |
| 331 | free_pages((unsigned long)buffer, get_order(size)); |
| 332 | } |
| 333 | |
| 334 | static int alloc_pebs_buffer(int cpu) |
| 335 | { |
| 336 | struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu); |
| 337 | struct debug_store *ds = hwev->ds; |
| 338 | size_t bsiz = x86_pmu.pebs_buffer_size; |
| 339 | int max, node = cpu_to_node(cpu); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 340 | void *buffer, *insn_buff, *cea; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 341 | |
| 342 | if (!x86_pmu.pebs) |
| 343 | return 0; |
| 344 | |
| 345 | buffer = dsalloc_pages(bsiz, GFP_KERNEL, cpu); |
| 346 | if (unlikely(!buffer)) |
| 347 | return -ENOMEM; |
| 348 | |
| 349 | /* |
| 350 | * HSW+ already provides us the eventing ip; no need to allocate this |
| 351 | * buffer then. |
| 352 | */ |
| 353 | if (x86_pmu.intel_cap.pebs_format < 2) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 354 | insn_buff = kzalloc_node(PEBS_FIXUP_SIZE, GFP_KERNEL, node); |
| 355 | if (!insn_buff) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 356 | dsfree_pages(buffer, bsiz); |
| 357 | return -ENOMEM; |
| 358 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 359 | per_cpu(insn_buffer, cpu) = insn_buff; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 360 | } |
| 361 | hwev->ds_pebs_vaddr = buffer; |
| 362 | /* Update the cpu entry area mapping */ |
| 363 | cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer; |
| 364 | ds->pebs_buffer_base = (unsigned long) cea; |
| 365 | ds_update_cea(cea, buffer, bsiz, PAGE_KERNEL); |
| 366 | ds->pebs_index = ds->pebs_buffer_base; |
| 367 | max = x86_pmu.pebs_record_size * (bsiz / x86_pmu.pebs_record_size); |
| 368 | ds->pebs_absolute_maximum = ds->pebs_buffer_base + max; |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static void release_pebs_buffer(int cpu) |
| 373 | { |
| 374 | struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu); |
| 375 | void *cea; |
| 376 | |
| 377 | if (!x86_pmu.pebs) |
| 378 | return; |
| 379 | |
| 380 | kfree(per_cpu(insn_buffer, cpu)); |
| 381 | per_cpu(insn_buffer, cpu) = NULL; |
| 382 | |
| 383 | /* Clear the fixmap */ |
| 384 | cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer; |
| 385 | ds_clear_cea(cea, x86_pmu.pebs_buffer_size); |
| 386 | dsfree_pages(hwev->ds_pebs_vaddr, x86_pmu.pebs_buffer_size); |
| 387 | hwev->ds_pebs_vaddr = NULL; |
| 388 | } |
| 389 | |
| 390 | static int alloc_bts_buffer(int cpu) |
| 391 | { |
| 392 | struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu); |
| 393 | struct debug_store *ds = hwev->ds; |
| 394 | void *buffer, *cea; |
| 395 | int max; |
| 396 | |
| 397 | if (!x86_pmu.bts) |
| 398 | return 0; |
| 399 | |
| 400 | buffer = dsalloc_pages(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, cpu); |
| 401 | if (unlikely(!buffer)) { |
| 402 | WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__); |
| 403 | return -ENOMEM; |
| 404 | } |
| 405 | hwev->ds_bts_vaddr = buffer; |
| 406 | /* Update the fixmap */ |
| 407 | cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer; |
| 408 | ds->bts_buffer_base = (unsigned long) cea; |
| 409 | ds_update_cea(cea, buffer, BTS_BUFFER_SIZE, PAGE_KERNEL); |
| 410 | ds->bts_index = ds->bts_buffer_base; |
| 411 | max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE; |
| 412 | ds->bts_absolute_maximum = ds->bts_buffer_base + |
| 413 | max * BTS_RECORD_SIZE; |
| 414 | ds->bts_interrupt_threshold = ds->bts_absolute_maximum - |
| 415 | (max / 16) * BTS_RECORD_SIZE; |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | static void release_bts_buffer(int cpu) |
| 420 | { |
| 421 | struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu); |
| 422 | void *cea; |
| 423 | |
| 424 | if (!x86_pmu.bts) |
| 425 | return; |
| 426 | |
| 427 | /* Clear the fixmap */ |
| 428 | cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer; |
| 429 | ds_clear_cea(cea, BTS_BUFFER_SIZE); |
| 430 | dsfree_pages(hwev->ds_bts_vaddr, BTS_BUFFER_SIZE); |
| 431 | hwev->ds_bts_vaddr = NULL; |
| 432 | } |
| 433 | |
| 434 | static int alloc_ds_buffer(int cpu) |
| 435 | { |
| 436 | struct debug_store *ds = &get_cpu_entry_area(cpu)->cpu_debug_store; |
| 437 | |
| 438 | memset(ds, 0, sizeof(*ds)); |
| 439 | per_cpu(cpu_hw_events, cpu).ds = ds; |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | static void release_ds_buffer(int cpu) |
| 444 | { |
| 445 | per_cpu(cpu_hw_events, cpu).ds = NULL; |
| 446 | } |
| 447 | |
| 448 | void release_ds_buffers(void) |
| 449 | { |
| 450 | int cpu; |
| 451 | |
| 452 | if (!x86_pmu.bts && !x86_pmu.pebs) |
| 453 | return; |
| 454 | |
| 455 | for_each_possible_cpu(cpu) |
| 456 | release_ds_buffer(cpu); |
| 457 | |
| 458 | for_each_possible_cpu(cpu) { |
| 459 | /* |
| 460 | * Again, ignore errors from offline CPUs, they will no longer |
| 461 | * observe cpu_hw_events.ds and not program the DS_AREA when |
| 462 | * they come up. |
| 463 | */ |
| 464 | fini_debug_store_on_cpu(cpu); |
| 465 | } |
| 466 | |
| 467 | for_each_possible_cpu(cpu) { |
| 468 | release_pebs_buffer(cpu); |
| 469 | release_bts_buffer(cpu); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | void reserve_ds_buffers(void) |
| 474 | { |
| 475 | int bts_err = 0, pebs_err = 0; |
| 476 | int cpu; |
| 477 | |
| 478 | x86_pmu.bts_active = 0; |
| 479 | x86_pmu.pebs_active = 0; |
| 480 | |
| 481 | if (!x86_pmu.bts && !x86_pmu.pebs) |
| 482 | return; |
| 483 | |
| 484 | if (!x86_pmu.bts) |
| 485 | bts_err = 1; |
| 486 | |
| 487 | if (!x86_pmu.pebs) |
| 488 | pebs_err = 1; |
| 489 | |
| 490 | for_each_possible_cpu(cpu) { |
| 491 | if (alloc_ds_buffer(cpu)) { |
| 492 | bts_err = 1; |
| 493 | pebs_err = 1; |
| 494 | } |
| 495 | |
| 496 | if (!bts_err && alloc_bts_buffer(cpu)) |
| 497 | bts_err = 1; |
| 498 | |
| 499 | if (!pebs_err && alloc_pebs_buffer(cpu)) |
| 500 | pebs_err = 1; |
| 501 | |
| 502 | if (bts_err && pebs_err) |
| 503 | break; |
| 504 | } |
| 505 | |
| 506 | if (bts_err) { |
| 507 | for_each_possible_cpu(cpu) |
| 508 | release_bts_buffer(cpu); |
| 509 | } |
| 510 | |
| 511 | if (pebs_err) { |
| 512 | for_each_possible_cpu(cpu) |
| 513 | release_pebs_buffer(cpu); |
| 514 | } |
| 515 | |
| 516 | if (bts_err && pebs_err) { |
| 517 | for_each_possible_cpu(cpu) |
| 518 | release_ds_buffer(cpu); |
| 519 | } else { |
| 520 | if (x86_pmu.bts && !bts_err) |
| 521 | x86_pmu.bts_active = 1; |
| 522 | |
| 523 | if (x86_pmu.pebs && !pebs_err) |
| 524 | x86_pmu.pebs_active = 1; |
| 525 | |
| 526 | for_each_possible_cpu(cpu) { |
| 527 | /* |
| 528 | * Ignores wrmsr_on_cpu() errors for offline CPUs they |
| 529 | * will get this call through intel_pmu_cpu_starting(). |
| 530 | */ |
| 531 | init_debug_store_on_cpu(cpu); |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | /* |
| 537 | * BTS |
| 538 | */ |
| 539 | |
| 540 | struct event_constraint bts_constraint = |
| 541 | EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0); |
| 542 | |
| 543 | void intel_pmu_enable_bts(u64 config) |
| 544 | { |
| 545 | unsigned long debugctlmsr; |
| 546 | |
| 547 | debugctlmsr = get_debugctlmsr(); |
| 548 | |
| 549 | debugctlmsr |= DEBUGCTLMSR_TR; |
| 550 | debugctlmsr |= DEBUGCTLMSR_BTS; |
| 551 | if (config & ARCH_PERFMON_EVENTSEL_INT) |
| 552 | debugctlmsr |= DEBUGCTLMSR_BTINT; |
| 553 | |
| 554 | if (!(config & ARCH_PERFMON_EVENTSEL_OS)) |
| 555 | debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS; |
| 556 | |
| 557 | if (!(config & ARCH_PERFMON_EVENTSEL_USR)) |
| 558 | debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR; |
| 559 | |
| 560 | update_debugctlmsr(debugctlmsr); |
| 561 | } |
| 562 | |
| 563 | void intel_pmu_disable_bts(void) |
| 564 | { |
| 565 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 566 | unsigned long debugctlmsr; |
| 567 | |
| 568 | if (!cpuc->ds) |
| 569 | return; |
| 570 | |
| 571 | debugctlmsr = get_debugctlmsr(); |
| 572 | |
| 573 | debugctlmsr &= |
| 574 | ~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT | |
| 575 | DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR); |
| 576 | |
| 577 | update_debugctlmsr(debugctlmsr); |
| 578 | } |
| 579 | |
| 580 | int intel_pmu_drain_bts_buffer(void) |
| 581 | { |
| 582 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 583 | struct debug_store *ds = cpuc->ds; |
| 584 | struct bts_record { |
| 585 | u64 from; |
| 586 | u64 to; |
| 587 | u64 flags; |
| 588 | }; |
| 589 | struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS]; |
| 590 | struct bts_record *at, *base, *top; |
| 591 | struct perf_output_handle handle; |
| 592 | struct perf_event_header header; |
| 593 | struct perf_sample_data data; |
| 594 | unsigned long skip = 0; |
| 595 | struct pt_regs regs; |
| 596 | |
| 597 | if (!event) |
| 598 | return 0; |
| 599 | |
| 600 | if (!x86_pmu.bts_active) |
| 601 | return 0; |
| 602 | |
| 603 | base = (struct bts_record *)(unsigned long)ds->bts_buffer_base; |
| 604 | top = (struct bts_record *)(unsigned long)ds->bts_index; |
| 605 | |
| 606 | if (top <= base) |
| 607 | return 0; |
| 608 | |
| 609 | memset(®s, 0, sizeof(regs)); |
| 610 | |
| 611 | ds->bts_index = ds->bts_buffer_base; |
| 612 | |
| 613 | perf_sample_data_init(&data, 0, event->hw.last_period); |
| 614 | |
| 615 | /* |
| 616 | * BTS leaks kernel addresses in branches across the cpl boundary, |
| 617 | * such as traps or system calls, so unless the user is asking for |
| 618 | * kernel tracing (and right now it's not possible), we'd need to |
| 619 | * filter them out. But first we need to count how many of those we |
| 620 | * have in the current batch. This is an extra O(n) pass, however, |
| 621 | * it's much faster than the other one especially considering that |
| 622 | * n <= 2560 (BTS_BUFFER_SIZE / BTS_RECORD_SIZE * 15/16; see the |
| 623 | * alloc_bts_buffer()). |
| 624 | */ |
| 625 | for (at = base; at < top; at++) { |
| 626 | /* |
| 627 | * Note that right now *this* BTS code only works if |
| 628 | * attr::exclude_kernel is set, but let's keep this extra |
| 629 | * check here in case that changes. |
| 630 | */ |
| 631 | if (event->attr.exclude_kernel && |
| 632 | (kernel_ip(at->from) || kernel_ip(at->to))) |
| 633 | skip++; |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | * Prepare a generic sample, i.e. fill in the invariant fields. |
| 638 | * We will overwrite the from and to address before we output |
| 639 | * the sample. |
| 640 | */ |
| 641 | rcu_read_lock(); |
| 642 | perf_prepare_sample(&header, &data, event, ®s); |
| 643 | |
| 644 | if (perf_output_begin(&handle, event, header.size * |
| 645 | (top - base - skip))) |
| 646 | goto unlock; |
| 647 | |
| 648 | for (at = base; at < top; at++) { |
| 649 | /* Filter out any records that contain kernel addresses. */ |
| 650 | if (event->attr.exclude_kernel && |
| 651 | (kernel_ip(at->from) || kernel_ip(at->to))) |
| 652 | continue; |
| 653 | |
| 654 | data.ip = at->from; |
| 655 | data.addr = at->to; |
| 656 | |
| 657 | perf_output_sample(&handle, &header, &data, event); |
| 658 | } |
| 659 | |
| 660 | perf_output_end(&handle); |
| 661 | |
| 662 | /* There's new data available. */ |
| 663 | event->hw.interrupts++; |
| 664 | event->pending_kill = POLL_IN; |
| 665 | unlock: |
| 666 | rcu_read_unlock(); |
| 667 | return 1; |
| 668 | } |
| 669 | |
| 670 | static inline void intel_pmu_drain_pebs_buffer(void) |
| 671 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 672 | x86_pmu.drain_pebs(NULL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /* |
| 676 | * PEBS |
| 677 | */ |
| 678 | struct event_constraint intel_core2_pebs_event_constraints[] = { |
| 679 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */ |
| 680 | INTEL_FLAGS_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */ |
| 681 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */ |
| 682 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */ |
| 683 | INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */ |
| 684 | /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 685 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x01), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 686 | EVENT_CONSTRAINT_END |
| 687 | }; |
| 688 | |
| 689 | struct event_constraint intel_atom_pebs_event_constraints[] = { |
| 690 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */ |
| 691 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */ |
| 692 | INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */ |
| 693 | /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 694 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x01), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 695 | /* Allow all events as PEBS with no flags */ |
| 696 | INTEL_ALL_EVENT_CONSTRAINT(0, 0x1), |
| 697 | EVENT_CONSTRAINT_END |
| 698 | }; |
| 699 | |
| 700 | struct event_constraint intel_slm_pebs_event_constraints[] = { |
| 701 | /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 702 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x1), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 703 | /* Allow all events as PEBS with no flags */ |
| 704 | INTEL_ALL_EVENT_CONSTRAINT(0, 0x1), |
| 705 | EVENT_CONSTRAINT_END |
| 706 | }; |
| 707 | |
| 708 | struct event_constraint intel_glm_pebs_event_constraints[] = { |
| 709 | /* Allow all events as PEBS with no flags */ |
| 710 | INTEL_ALL_EVENT_CONSTRAINT(0, 0x1), |
| 711 | EVENT_CONSTRAINT_END |
| 712 | }; |
| 713 | |
| 714 | struct event_constraint intel_nehalem_pebs_event_constraints[] = { |
| 715 | INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */ |
| 716 | INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */ |
| 717 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */ |
| 718 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INST_RETIRED.ANY */ |
| 719 | INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */ |
| 720 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */ |
| 721 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */ |
| 722 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */ |
| 723 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */ |
| 724 | INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */ |
| 725 | INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */ |
| 726 | /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 727 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 728 | EVENT_CONSTRAINT_END |
| 729 | }; |
| 730 | |
| 731 | struct event_constraint intel_westmere_pebs_event_constraints[] = { |
| 732 | INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */ |
| 733 | INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */ |
| 734 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */ |
| 735 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INSTR_RETIRED.* */ |
| 736 | INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */ |
| 737 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */ |
| 738 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */ |
| 739 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */ |
| 740 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */ |
| 741 | INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */ |
| 742 | INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */ |
| 743 | /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 744 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 745 | EVENT_CONSTRAINT_END |
| 746 | }; |
| 747 | |
| 748 | struct event_constraint intel_snb_pebs_event_constraints[] = { |
| 749 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */ |
| 750 | INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */ |
| 751 | INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */ |
| 752 | /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 753 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 754 | INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */ |
| 755 | INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ |
| 756 | INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */ |
| 757 | INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */ |
| 758 | /* Allow all events as PEBS with no flags */ |
| 759 | INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), |
| 760 | EVENT_CONSTRAINT_END |
| 761 | }; |
| 762 | |
| 763 | struct event_constraint intel_ivb_pebs_event_constraints[] = { |
| 764 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */ |
| 765 | INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */ |
| 766 | INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */ |
| 767 | /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 768 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 769 | /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 770 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 771 | INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */ |
| 772 | INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ |
| 773 | INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */ |
| 774 | INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */ |
| 775 | /* Allow all events as PEBS with no flags */ |
| 776 | INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), |
| 777 | EVENT_CONSTRAINT_END |
| 778 | }; |
| 779 | |
| 780 | struct event_constraint intel_hsw_pebs_event_constraints[] = { |
| 781 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */ |
| 782 | INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */ |
| 783 | /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 784 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 785 | /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 786 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 787 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */ |
| 788 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */ |
| 789 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */ |
| 790 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */ |
| 791 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */ |
| 792 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */ |
| 793 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */ |
| 794 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */ |
| 795 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ |
| 796 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */ |
| 797 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */ |
| 798 | /* Allow all events as PEBS with no flags */ |
| 799 | INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), |
| 800 | EVENT_CONSTRAINT_END |
| 801 | }; |
| 802 | |
| 803 | struct event_constraint intel_bdw_pebs_event_constraints[] = { |
| 804 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */ |
| 805 | INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */ |
| 806 | /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 807 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 808 | /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 809 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 810 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */ |
| 811 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */ |
| 812 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */ |
| 813 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */ |
| 814 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */ |
| 815 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */ |
| 816 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */ |
| 817 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */ |
| 818 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ |
| 819 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */ |
| 820 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */ |
| 821 | /* Allow all events as PEBS with no flags */ |
| 822 | INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), |
| 823 | EVENT_CONSTRAINT_END |
| 824 | }; |
| 825 | |
| 826 | |
| 827 | struct event_constraint intel_skl_pebs_event_constraints[] = { |
| 828 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x1c0, 0x2), /* INST_RETIRED.PREC_DIST */ |
| 829 | /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 830 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 831 | /* INST_RETIRED.TOTAL_CYCLES_PS (inv=1, cmask=16) (cycles:p). */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 832 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 833 | INTEL_PLD_CONSTRAINT(0x1cd, 0xf), /* MEM_TRANS_RETIRED.* */ |
| 834 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */ |
| 835 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */ |
| 836 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */ |
| 837 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x22d0, 0xf), /* MEM_INST_RETIRED.LOCK_STORES */ |
| 838 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */ |
| 839 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */ |
| 840 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */ |
| 841 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */ |
| 842 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_RETIRED.* */ |
| 843 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_L3_HIT_RETIRED.* */ |
| 844 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_L3_MISS_RETIRED.* */ |
| 845 | /* Allow all events as PEBS with no flags */ |
| 846 | INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), |
| 847 | EVENT_CONSTRAINT_END |
| 848 | }; |
| 849 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 850 | struct event_constraint intel_icl_pebs_event_constraints[] = { |
| 851 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x1c0, 0x100000000ULL), /* INST_RETIRED.PREC_DIST */ |
| 852 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x0400, 0x800000000ULL), /* SLOTS */ |
| 853 | |
| 854 | INTEL_PLD_CONSTRAINT(0x1cd, 0xff), /* MEM_TRANS_RETIRED.LOAD_LATENCY */ |
| 855 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x1d0, 0xf), /* MEM_INST_RETIRED.LOAD */ |
| 856 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x2d0, 0xf), /* MEM_INST_RETIRED.STORE */ |
| 857 | |
| 858 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD_RANGE(0xd1, 0xd4, 0xf), /* MEM_LOAD_*_RETIRED.* */ |
| 859 | |
| 860 | INTEL_FLAGS_EVENT_CONSTRAINT(0xd0, 0xf), /* MEM_INST_RETIRED.* */ |
| 861 | |
| 862 | /* |
| 863 | * Everything else is handled by PMU_FL_PEBS_ALL, because we |
| 864 | * need the full constraints from the main table. |
| 865 | */ |
| 866 | |
| 867 | EVENT_CONSTRAINT_END |
| 868 | }; |
| 869 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 870 | struct event_constraint *intel_pebs_constraints(struct perf_event *event) |
| 871 | { |
| 872 | struct event_constraint *c; |
| 873 | |
| 874 | if (!event->attr.precise_ip) |
| 875 | return NULL; |
| 876 | |
| 877 | if (x86_pmu.pebs_constraints) { |
| 878 | for_each_event_constraint(c, x86_pmu.pebs_constraints) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 879 | if (constraint_match(c, event->hw.config)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 880 | event->hw.flags |= c->flags; |
| 881 | return c; |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | * Extended PEBS support |
| 888 | * Makes the PEBS code search the normal constraints. |
| 889 | */ |
| 890 | if (x86_pmu.flags & PMU_FL_PEBS_ALL) |
| 891 | return NULL; |
| 892 | |
| 893 | return &emptyconstraint; |
| 894 | } |
| 895 | |
| 896 | /* |
| 897 | * We need the sched_task callback even for per-cpu events when we use |
| 898 | * the large interrupt threshold, such that we can provide PID and TID |
| 899 | * to PEBS samples. |
| 900 | */ |
| 901 | static inline bool pebs_needs_sched_cb(struct cpu_hw_events *cpuc) |
| 902 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 903 | if (cpuc->n_pebs == cpuc->n_pebs_via_pt) |
| 904 | return false; |
| 905 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 906 | return cpuc->n_pebs && (cpuc->n_pebs == cpuc->n_large_pebs); |
| 907 | } |
| 908 | |
| 909 | void intel_pmu_pebs_sched_task(struct perf_event_context *ctx, bool sched_in) |
| 910 | { |
| 911 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 912 | |
| 913 | if (!sched_in && pebs_needs_sched_cb(cpuc)) |
| 914 | intel_pmu_drain_pebs_buffer(); |
| 915 | } |
| 916 | |
| 917 | static inline void pebs_update_threshold(struct cpu_hw_events *cpuc) |
| 918 | { |
| 919 | struct debug_store *ds = cpuc->ds; |
| 920 | u64 threshold; |
| 921 | int reserved; |
| 922 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 923 | if (cpuc->n_pebs_via_pt) |
| 924 | return; |
| 925 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 926 | if (x86_pmu.flags & PMU_FL_PEBS_ALL) |
| 927 | reserved = x86_pmu.max_pebs_events + x86_pmu.num_counters_fixed; |
| 928 | else |
| 929 | reserved = x86_pmu.max_pebs_events; |
| 930 | |
| 931 | if (cpuc->n_pebs == cpuc->n_large_pebs) { |
| 932 | threshold = ds->pebs_absolute_maximum - |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 933 | reserved * cpuc->pebs_record_size; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 934 | } else { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 935 | threshold = ds->pebs_buffer_base + cpuc->pebs_record_size; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | ds->pebs_interrupt_threshold = threshold; |
| 939 | } |
| 940 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 941 | static void adaptive_pebs_record_size_update(void) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 942 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 943 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 944 | u64 pebs_data_cfg = cpuc->pebs_data_cfg; |
| 945 | int sz = sizeof(struct pebs_basic); |
| 946 | |
| 947 | if (pebs_data_cfg & PEBS_DATACFG_MEMINFO) |
| 948 | sz += sizeof(struct pebs_meminfo); |
| 949 | if (pebs_data_cfg & PEBS_DATACFG_GP) |
| 950 | sz += sizeof(struct pebs_gprs); |
| 951 | if (pebs_data_cfg & PEBS_DATACFG_XMMS) |
| 952 | sz += sizeof(struct pebs_xmm); |
| 953 | if (pebs_data_cfg & PEBS_DATACFG_LBRS) |
| 954 | sz += x86_pmu.lbr_nr * sizeof(struct pebs_lbr_entry); |
| 955 | |
| 956 | cpuc->pebs_record_size = sz; |
| 957 | } |
| 958 | |
| 959 | #define PERF_PEBS_MEMINFO_TYPE (PERF_SAMPLE_ADDR | PERF_SAMPLE_DATA_SRC | \ |
| 960 | PERF_SAMPLE_PHYS_ADDR | PERF_SAMPLE_WEIGHT | \ |
| 961 | PERF_SAMPLE_TRANSACTION) |
| 962 | |
| 963 | static u64 pebs_update_adaptive_cfg(struct perf_event *event) |
| 964 | { |
| 965 | struct perf_event_attr *attr = &event->attr; |
| 966 | u64 sample_type = attr->sample_type; |
| 967 | u64 pebs_data_cfg = 0; |
| 968 | bool gprs, tsx_weight; |
| 969 | |
| 970 | if (!(sample_type & ~(PERF_SAMPLE_IP|PERF_SAMPLE_TIME)) && |
| 971 | attr->precise_ip > 1) |
| 972 | return pebs_data_cfg; |
| 973 | |
| 974 | if (sample_type & PERF_PEBS_MEMINFO_TYPE) |
| 975 | pebs_data_cfg |= PEBS_DATACFG_MEMINFO; |
| 976 | |
| 977 | /* |
| 978 | * We need GPRs when: |
| 979 | * + user requested them |
| 980 | * + precise_ip < 2 for the non event IP |
| 981 | * + For RTM TSX weight we need GPRs for the abort code. |
| 982 | */ |
| 983 | gprs = (sample_type & PERF_SAMPLE_REGS_INTR) && |
| 984 | (attr->sample_regs_intr & PEBS_GP_REGS); |
| 985 | |
| 986 | tsx_weight = (sample_type & PERF_SAMPLE_WEIGHT) && |
| 987 | ((attr->config & INTEL_ARCH_EVENT_MASK) == |
| 988 | x86_pmu.rtm_abort_event); |
| 989 | |
| 990 | if (gprs || (attr->precise_ip < 2) || tsx_weight) |
| 991 | pebs_data_cfg |= PEBS_DATACFG_GP; |
| 992 | |
| 993 | if ((sample_type & PERF_SAMPLE_REGS_INTR) && |
| 994 | (attr->sample_regs_intr & PERF_REG_EXTENDED_MASK)) |
| 995 | pebs_data_cfg |= PEBS_DATACFG_XMMS; |
| 996 | |
| 997 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) { |
| 998 | /* |
| 999 | * For now always log all LBRs. Could configure this |
| 1000 | * later. |
| 1001 | */ |
| 1002 | pebs_data_cfg |= PEBS_DATACFG_LBRS | |
| 1003 | ((x86_pmu.lbr_nr-1) << PEBS_DATACFG_LBR_SHIFT); |
| 1004 | } |
| 1005 | |
| 1006 | return pebs_data_cfg; |
| 1007 | } |
| 1008 | |
| 1009 | static void |
| 1010 | pebs_update_state(bool needed_cb, struct cpu_hw_events *cpuc, |
| 1011 | struct perf_event *event, bool add) |
| 1012 | { |
| 1013 | struct pmu *pmu = event->ctx->pmu; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1014 | /* |
| 1015 | * Make sure we get updated with the first PEBS |
| 1016 | * event. It will trigger also during removal, but |
| 1017 | * that does not hurt: |
| 1018 | */ |
| 1019 | bool update = cpuc->n_pebs == 1; |
| 1020 | |
| 1021 | if (needed_cb != pebs_needs_sched_cb(cpuc)) { |
| 1022 | if (!needed_cb) |
| 1023 | perf_sched_cb_inc(pmu); |
| 1024 | else |
| 1025 | perf_sched_cb_dec(pmu); |
| 1026 | |
| 1027 | update = true; |
| 1028 | } |
| 1029 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1030 | /* |
| 1031 | * The PEBS record doesn't shrink on pmu::del(). Doing so would require |
| 1032 | * iterating all remaining PEBS events to reconstruct the config. |
| 1033 | */ |
| 1034 | if (x86_pmu.intel_cap.pebs_baseline && add) { |
| 1035 | u64 pebs_data_cfg; |
| 1036 | |
| 1037 | /* Clear pebs_data_cfg and pebs_record_size for first PEBS. */ |
| 1038 | if (cpuc->n_pebs == 1) { |
| 1039 | cpuc->pebs_data_cfg = 0; |
| 1040 | cpuc->pebs_record_size = sizeof(struct pebs_basic); |
| 1041 | } |
| 1042 | |
| 1043 | pebs_data_cfg = pebs_update_adaptive_cfg(event); |
| 1044 | |
| 1045 | /* Update pebs_record_size if new event requires more data. */ |
| 1046 | if (pebs_data_cfg & ~cpuc->pebs_data_cfg) { |
| 1047 | cpuc->pebs_data_cfg |= pebs_data_cfg; |
| 1048 | adaptive_pebs_record_size_update(); |
| 1049 | update = true; |
| 1050 | } |
| 1051 | } |
| 1052 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1053 | if (update) |
| 1054 | pebs_update_threshold(cpuc); |
| 1055 | } |
| 1056 | |
| 1057 | void intel_pmu_pebs_add(struct perf_event *event) |
| 1058 | { |
| 1059 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 1060 | struct hw_perf_event *hwc = &event->hw; |
| 1061 | bool needed_cb = pebs_needs_sched_cb(cpuc); |
| 1062 | |
| 1063 | cpuc->n_pebs++; |
| 1064 | if (hwc->flags & PERF_X86_EVENT_LARGE_PEBS) |
| 1065 | cpuc->n_large_pebs++; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1066 | if (hwc->flags & PERF_X86_EVENT_PEBS_VIA_PT) |
| 1067 | cpuc->n_pebs_via_pt++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1068 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1069 | pebs_update_state(needed_cb, cpuc, event, true); |
| 1070 | } |
| 1071 | |
| 1072 | static void intel_pmu_pebs_via_pt_disable(struct perf_event *event) |
| 1073 | { |
| 1074 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 1075 | |
| 1076 | if (!is_pebs_pt(event)) |
| 1077 | return; |
| 1078 | |
| 1079 | if (!(cpuc->pebs_enabled & ~PEBS_VIA_PT_MASK)) |
| 1080 | cpuc->pebs_enabled &= ~PEBS_VIA_PT_MASK; |
| 1081 | } |
| 1082 | |
| 1083 | static void intel_pmu_pebs_via_pt_enable(struct perf_event *event) |
| 1084 | { |
| 1085 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 1086 | struct hw_perf_event *hwc = &event->hw; |
| 1087 | struct debug_store *ds = cpuc->ds; |
| 1088 | |
| 1089 | if (!is_pebs_pt(event)) |
| 1090 | return; |
| 1091 | |
| 1092 | if (!(event->hw.flags & PERF_X86_EVENT_LARGE_PEBS)) |
| 1093 | cpuc->pebs_enabled |= PEBS_PMI_AFTER_EACH_RECORD; |
| 1094 | |
| 1095 | cpuc->pebs_enabled |= PEBS_OUTPUT_PT; |
| 1096 | |
| 1097 | wrmsrl(MSR_RELOAD_PMC0 + hwc->idx, ds->pebs_event_reset[hwc->idx]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | void intel_pmu_pebs_enable(struct perf_event *event) |
| 1101 | { |
| 1102 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 1103 | struct hw_perf_event *hwc = &event->hw; |
| 1104 | struct debug_store *ds = cpuc->ds; |
| 1105 | |
| 1106 | hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT; |
| 1107 | |
| 1108 | cpuc->pebs_enabled |= 1ULL << hwc->idx; |
| 1109 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1110 | if ((event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT) && (x86_pmu.version < 5)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1111 | cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32); |
| 1112 | else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST) |
| 1113 | cpuc->pebs_enabled |= 1ULL << 63; |
| 1114 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1115 | if (x86_pmu.intel_cap.pebs_baseline) { |
| 1116 | hwc->config |= ICL_EVENTSEL_ADAPTIVE; |
| 1117 | if (cpuc->pebs_data_cfg != cpuc->active_pebs_data_cfg) { |
| 1118 | wrmsrl(MSR_PEBS_DATA_CFG, cpuc->pebs_data_cfg); |
| 1119 | cpuc->active_pebs_data_cfg = cpuc->pebs_data_cfg; |
| 1120 | } |
| 1121 | } |
| 1122 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1123 | /* |
| 1124 | * Use auto-reload if possible to save a MSR write in the PMI. |
| 1125 | * This must be done in pmu::start(), because PERF_EVENT_IOC_PERIOD. |
| 1126 | */ |
| 1127 | if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) { |
| 1128 | unsigned int idx = hwc->idx; |
| 1129 | |
| 1130 | if (idx >= INTEL_PMC_IDX_FIXED) |
| 1131 | idx = MAX_PEBS_EVENTS + (idx - INTEL_PMC_IDX_FIXED); |
| 1132 | ds->pebs_event_reset[idx] = |
| 1133 | (u64)(-hwc->sample_period) & x86_pmu.cntval_mask; |
| 1134 | } else { |
| 1135 | ds->pebs_event_reset[hwc->idx] = 0; |
| 1136 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1137 | |
| 1138 | intel_pmu_pebs_via_pt_enable(event); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | void intel_pmu_pebs_del(struct perf_event *event) |
| 1142 | { |
| 1143 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 1144 | struct hw_perf_event *hwc = &event->hw; |
| 1145 | bool needed_cb = pebs_needs_sched_cb(cpuc); |
| 1146 | |
| 1147 | cpuc->n_pebs--; |
| 1148 | if (hwc->flags & PERF_X86_EVENT_LARGE_PEBS) |
| 1149 | cpuc->n_large_pebs--; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1150 | if (hwc->flags & PERF_X86_EVENT_PEBS_VIA_PT) |
| 1151 | cpuc->n_pebs_via_pt--; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1152 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1153 | pebs_update_state(needed_cb, cpuc, event, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | void intel_pmu_pebs_disable(struct perf_event *event) |
| 1157 | { |
| 1158 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 1159 | struct hw_perf_event *hwc = &event->hw; |
| 1160 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1161 | if (cpuc->n_pebs == cpuc->n_large_pebs && |
| 1162 | cpuc->n_pebs != cpuc->n_pebs_via_pt) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1163 | intel_pmu_drain_pebs_buffer(); |
| 1164 | |
| 1165 | cpuc->pebs_enabled &= ~(1ULL << hwc->idx); |
| 1166 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1167 | if ((event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT) && |
| 1168 | (x86_pmu.version < 5)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1169 | cpuc->pebs_enabled &= ~(1ULL << (hwc->idx + 32)); |
| 1170 | else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST) |
| 1171 | cpuc->pebs_enabled &= ~(1ULL << 63); |
| 1172 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1173 | intel_pmu_pebs_via_pt_disable(event); |
| 1174 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1175 | if (cpuc->enabled) |
| 1176 | wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled); |
| 1177 | |
| 1178 | hwc->config |= ARCH_PERFMON_EVENTSEL_INT; |
| 1179 | } |
| 1180 | |
| 1181 | void intel_pmu_pebs_enable_all(void) |
| 1182 | { |
| 1183 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 1184 | |
| 1185 | if (cpuc->pebs_enabled) |
| 1186 | wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled); |
| 1187 | } |
| 1188 | |
| 1189 | void intel_pmu_pebs_disable_all(void) |
| 1190 | { |
| 1191 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 1192 | |
| 1193 | if (cpuc->pebs_enabled) |
| 1194 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); |
| 1195 | } |
| 1196 | |
| 1197 | static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs) |
| 1198 | { |
| 1199 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 1200 | unsigned long from = cpuc->lbr_entries[0].from; |
| 1201 | unsigned long old_to, to = cpuc->lbr_entries[0].to; |
| 1202 | unsigned long ip = regs->ip; |
| 1203 | int is_64bit = 0; |
| 1204 | void *kaddr; |
| 1205 | int size; |
| 1206 | |
| 1207 | /* |
| 1208 | * We don't need to fixup if the PEBS assist is fault like |
| 1209 | */ |
| 1210 | if (!x86_pmu.intel_cap.pebs_trap) |
| 1211 | return 1; |
| 1212 | |
| 1213 | /* |
| 1214 | * No LBR entry, no basic block, no rewinding |
| 1215 | */ |
| 1216 | if (!cpuc->lbr_stack.nr || !from || !to) |
| 1217 | return 0; |
| 1218 | |
| 1219 | /* |
| 1220 | * Basic blocks should never cross user/kernel boundaries |
| 1221 | */ |
| 1222 | if (kernel_ip(ip) != kernel_ip(to)) |
| 1223 | return 0; |
| 1224 | |
| 1225 | /* |
| 1226 | * unsigned math, either ip is before the start (impossible) or |
| 1227 | * the basic block is larger than 1 page (sanity) |
| 1228 | */ |
| 1229 | if ((ip - to) > PEBS_FIXUP_SIZE) |
| 1230 | return 0; |
| 1231 | |
| 1232 | /* |
| 1233 | * We sampled a branch insn, rewind using the LBR stack |
| 1234 | */ |
| 1235 | if (ip == to) { |
| 1236 | set_linear_ip(regs, from); |
| 1237 | return 1; |
| 1238 | } |
| 1239 | |
| 1240 | size = ip - to; |
| 1241 | if (!kernel_ip(ip)) { |
| 1242 | int bytes; |
| 1243 | u8 *buf = this_cpu_read(insn_buffer); |
| 1244 | |
| 1245 | /* 'size' must fit our buffer, see above */ |
| 1246 | bytes = copy_from_user_nmi(buf, (void __user *)to, size); |
| 1247 | if (bytes != 0) |
| 1248 | return 0; |
| 1249 | |
| 1250 | kaddr = buf; |
| 1251 | } else { |
| 1252 | kaddr = (void *)to; |
| 1253 | } |
| 1254 | |
| 1255 | do { |
| 1256 | struct insn insn; |
| 1257 | |
| 1258 | old_to = to; |
| 1259 | |
| 1260 | #ifdef CONFIG_X86_64 |
| 1261 | is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32); |
| 1262 | #endif |
| 1263 | insn_init(&insn, kaddr, size, is_64bit); |
| 1264 | insn_get_length(&insn); |
| 1265 | /* |
| 1266 | * Make sure there was not a problem decoding the |
| 1267 | * instruction and getting the length. This is |
| 1268 | * doubly important because we have an infinite |
| 1269 | * loop if insn.length=0. |
| 1270 | */ |
| 1271 | if (!insn.length) |
| 1272 | break; |
| 1273 | |
| 1274 | to += insn.length; |
| 1275 | kaddr += insn.length; |
| 1276 | size -= insn.length; |
| 1277 | } while (to < ip); |
| 1278 | |
| 1279 | if (to == ip) { |
| 1280 | set_linear_ip(regs, old_to); |
| 1281 | return 1; |
| 1282 | } |
| 1283 | |
| 1284 | /* |
| 1285 | * Even though we decoded the basic block, the instruction stream |
| 1286 | * never matched the given IP, either the TO or the IP got corrupted. |
| 1287 | */ |
| 1288 | return 0; |
| 1289 | } |
| 1290 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1291 | static inline u64 intel_get_tsx_weight(u64 tsx_tuning) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1292 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1293 | if (tsx_tuning) { |
| 1294 | union hsw_tsx_tuning tsx = { .value = tsx_tuning }; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1295 | return tsx.cycles_last_block; |
| 1296 | } |
| 1297 | return 0; |
| 1298 | } |
| 1299 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1300 | static inline u64 intel_get_tsx_transaction(u64 tsx_tuning, u64 ax) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1301 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1302 | u64 txn = (tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1303 | |
| 1304 | /* For RTM XABORTs also log the abort code from AX */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1305 | if ((txn & PERF_TXN_TRANSACTION) && (ax & 1)) |
| 1306 | txn |= ((ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1307 | return txn; |
| 1308 | } |
| 1309 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1310 | static inline u64 get_pebs_status(void *n) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1311 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1312 | if (x86_pmu.intel_cap.pebs_format < 4) |
| 1313 | return ((struct pebs_record_nhm *)n)->status; |
| 1314 | return ((struct pebs_basic *)n)->applicable_counters; |
| 1315 | } |
| 1316 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1317 | #define PERF_X86_EVENT_PEBS_HSW_PREC \ |
| 1318 | (PERF_X86_EVENT_PEBS_ST_HSW | \ |
| 1319 | PERF_X86_EVENT_PEBS_LD_HSW | \ |
| 1320 | PERF_X86_EVENT_PEBS_NA_HSW) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1321 | |
| 1322 | static u64 get_data_src(struct perf_event *event, u64 aux) |
| 1323 | { |
| 1324 | u64 val = PERF_MEM_NA; |
| 1325 | int fl = event->hw.flags; |
| 1326 | bool fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC); |
| 1327 | |
| 1328 | if (fl & PERF_X86_EVENT_PEBS_LDLAT) |
| 1329 | val = load_latency_data(aux); |
| 1330 | else if (fst && (fl & PERF_X86_EVENT_PEBS_HSW_PREC)) |
| 1331 | val = precise_datala_hsw(event, aux); |
| 1332 | else if (fst) |
| 1333 | val = precise_store_data(aux); |
| 1334 | return val; |
| 1335 | } |
| 1336 | |
| 1337 | static void setup_pebs_fixed_sample_data(struct perf_event *event, |
| 1338 | struct pt_regs *iregs, void *__pebs, |
| 1339 | struct perf_sample_data *data, |
| 1340 | struct pt_regs *regs) |
| 1341 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1342 | /* |
| 1343 | * We cast to the biggest pebs_record but are careful not to |
| 1344 | * unconditionally access the 'extra' entries. |
| 1345 | */ |
| 1346 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 1347 | struct pebs_record_skl *pebs = __pebs; |
| 1348 | u64 sample_type; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1349 | int fll; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1350 | |
| 1351 | if (pebs == NULL) |
| 1352 | return; |
| 1353 | |
| 1354 | sample_type = event->attr.sample_type; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1355 | fll = event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1356 | |
| 1357 | perf_sample_data_init(data, 0, event->hw.last_period); |
| 1358 | |
| 1359 | data->period = event->hw.last_period; |
| 1360 | |
| 1361 | /* |
| 1362 | * Use latency for weight (only avail with PEBS-LL) |
| 1363 | */ |
| 1364 | if (fll && (sample_type & PERF_SAMPLE_WEIGHT)) |
| 1365 | data->weight = pebs->lat; |
| 1366 | |
| 1367 | /* |
| 1368 | * data.data_src encodes the data source |
| 1369 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1370 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 1371 | data->data_src.val = get_data_src(event, pebs->dse); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1372 | |
| 1373 | /* |
| 1374 | * We must however always use iregs for the unwinder to stay sane; the |
| 1375 | * record BP,SP,IP can point into thin air when the record is from a |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1376 | * previous PMI context or an (I)RET happened between the record and |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1377 | * PMI. |
| 1378 | */ |
| 1379 | if (sample_type & PERF_SAMPLE_CALLCHAIN) |
| 1380 | data->callchain = perf_callchain(event, iregs); |
| 1381 | |
| 1382 | /* |
| 1383 | * We use the interrupt regs as a base because the PEBS record does not |
| 1384 | * contain a full regs set, specifically it seems to lack segment |
| 1385 | * descriptors, which get used by things like user_mode(). |
| 1386 | * |
| 1387 | * In the simple case fix up only the IP for PERF_SAMPLE_IP. |
| 1388 | */ |
| 1389 | *regs = *iregs; |
| 1390 | |
| 1391 | /* |
| 1392 | * Initialize regs_>flags from PEBS, |
| 1393 | * Clear exact bit (which uses x86 EFLAGS Reserved bit 3), |
| 1394 | * i.e., do not rely on it being zero: |
| 1395 | */ |
| 1396 | regs->flags = pebs->flags & ~PERF_EFLAGS_EXACT; |
| 1397 | |
| 1398 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
| 1399 | regs->ax = pebs->ax; |
| 1400 | regs->bx = pebs->bx; |
| 1401 | regs->cx = pebs->cx; |
| 1402 | regs->dx = pebs->dx; |
| 1403 | regs->si = pebs->si; |
| 1404 | regs->di = pebs->di; |
| 1405 | |
| 1406 | regs->bp = pebs->bp; |
| 1407 | regs->sp = pebs->sp; |
| 1408 | |
| 1409 | #ifndef CONFIG_X86_32 |
| 1410 | regs->r8 = pebs->r8; |
| 1411 | regs->r9 = pebs->r9; |
| 1412 | regs->r10 = pebs->r10; |
| 1413 | regs->r11 = pebs->r11; |
| 1414 | regs->r12 = pebs->r12; |
| 1415 | regs->r13 = pebs->r13; |
| 1416 | regs->r14 = pebs->r14; |
| 1417 | regs->r15 = pebs->r15; |
| 1418 | #endif |
| 1419 | } |
| 1420 | |
| 1421 | if (event->attr.precise_ip > 1) { |
| 1422 | /* |
| 1423 | * Haswell and later processors have an 'eventing IP' |
| 1424 | * (real IP) which fixes the off-by-1 skid in hardware. |
| 1425 | * Use it when precise_ip >= 2 : |
| 1426 | */ |
| 1427 | if (x86_pmu.intel_cap.pebs_format >= 2) { |
| 1428 | set_linear_ip(regs, pebs->real_ip); |
| 1429 | regs->flags |= PERF_EFLAGS_EXACT; |
| 1430 | } else { |
| 1431 | /* Otherwise, use PEBS off-by-1 IP: */ |
| 1432 | set_linear_ip(regs, pebs->ip); |
| 1433 | |
| 1434 | /* |
| 1435 | * With precise_ip >= 2, try to fix up the off-by-1 IP |
| 1436 | * using the LBR. If successful, the fixup function |
| 1437 | * corrects regs->ip and calls set_linear_ip() on regs: |
| 1438 | */ |
| 1439 | if (intel_pmu_pebs_fixup_ip(regs)) |
| 1440 | regs->flags |= PERF_EFLAGS_EXACT; |
| 1441 | } |
| 1442 | } else { |
| 1443 | /* |
| 1444 | * When precise_ip == 1, return the PEBS off-by-1 IP, |
| 1445 | * no fixup attempted: |
| 1446 | */ |
| 1447 | set_linear_ip(regs, pebs->ip); |
| 1448 | } |
| 1449 | |
| 1450 | |
| 1451 | if ((sample_type & (PERF_SAMPLE_ADDR | PERF_SAMPLE_PHYS_ADDR)) && |
| 1452 | x86_pmu.intel_cap.pebs_format >= 1) |
| 1453 | data->addr = pebs->dla; |
| 1454 | |
| 1455 | if (x86_pmu.intel_cap.pebs_format >= 2) { |
| 1456 | /* Only set the TSX weight when no memory weight. */ |
| 1457 | if ((sample_type & PERF_SAMPLE_WEIGHT) && !fll) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1458 | data->weight = intel_get_tsx_weight(pebs->tsx_tuning); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1459 | |
| 1460 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1461 | data->txn = intel_get_tsx_transaction(pebs->tsx_tuning, |
| 1462 | pebs->ax); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | /* |
| 1466 | * v3 supplies an accurate time stamp, so we use that |
| 1467 | * for the time stamp. |
| 1468 | * |
| 1469 | * We can only do this for the default trace clock. |
| 1470 | */ |
| 1471 | if (x86_pmu.intel_cap.pebs_format >= 3 && |
| 1472 | event->attr.use_clockid == 0) |
| 1473 | data->time = native_sched_clock_from_tsc(pebs->tsc); |
| 1474 | |
| 1475 | if (has_branch_stack(event)) |
| 1476 | data->br_stack = &cpuc->lbr_stack; |
| 1477 | } |
| 1478 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1479 | static void adaptive_pebs_save_regs(struct pt_regs *regs, |
| 1480 | struct pebs_gprs *gprs) |
| 1481 | { |
| 1482 | regs->ax = gprs->ax; |
| 1483 | regs->bx = gprs->bx; |
| 1484 | regs->cx = gprs->cx; |
| 1485 | regs->dx = gprs->dx; |
| 1486 | regs->si = gprs->si; |
| 1487 | regs->di = gprs->di; |
| 1488 | regs->bp = gprs->bp; |
| 1489 | regs->sp = gprs->sp; |
| 1490 | #ifndef CONFIG_X86_32 |
| 1491 | regs->r8 = gprs->r8; |
| 1492 | regs->r9 = gprs->r9; |
| 1493 | regs->r10 = gprs->r10; |
| 1494 | regs->r11 = gprs->r11; |
| 1495 | regs->r12 = gprs->r12; |
| 1496 | regs->r13 = gprs->r13; |
| 1497 | regs->r14 = gprs->r14; |
| 1498 | regs->r15 = gprs->r15; |
| 1499 | #endif |
| 1500 | } |
| 1501 | |
| 1502 | /* |
| 1503 | * With adaptive PEBS the layout depends on what fields are configured. |
| 1504 | */ |
| 1505 | |
| 1506 | static void setup_pebs_adaptive_sample_data(struct perf_event *event, |
| 1507 | struct pt_regs *iregs, void *__pebs, |
| 1508 | struct perf_sample_data *data, |
| 1509 | struct pt_regs *regs) |
| 1510 | { |
| 1511 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 1512 | struct pebs_basic *basic = __pebs; |
| 1513 | void *next_record = basic + 1; |
| 1514 | u64 sample_type; |
| 1515 | u64 format_size; |
| 1516 | struct pebs_meminfo *meminfo = NULL; |
| 1517 | struct pebs_gprs *gprs = NULL; |
| 1518 | struct x86_perf_regs *perf_regs; |
| 1519 | |
| 1520 | if (basic == NULL) |
| 1521 | return; |
| 1522 | |
| 1523 | perf_regs = container_of(regs, struct x86_perf_regs, regs); |
| 1524 | perf_regs->xmm_regs = NULL; |
| 1525 | |
| 1526 | sample_type = event->attr.sample_type; |
| 1527 | format_size = basic->format_size; |
| 1528 | perf_sample_data_init(data, 0, event->hw.last_period); |
| 1529 | data->period = event->hw.last_period; |
| 1530 | |
| 1531 | if (event->attr.use_clockid == 0) |
| 1532 | data->time = native_sched_clock_from_tsc(basic->tsc); |
| 1533 | |
| 1534 | /* |
| 1535 | * We must however always use iregs for the unwinder to stay sane; the |
| 1536 | * record BP,SP,IP can point into thin air when the record is from a |
| 1537 | * previous PMI context or an (I)RET happened between the record and |
| 1538 | * PMI. |
| 1539 | */ |
| 1540 | if (sample_type & PERF_SAMPLE_CALLCHAIN) |
| 1541 | data->callchain = perf_callchain(event, iregs); |
| 1542 | |
| 1543 | *regs = *iregs; |
| 1544 | /* The ip in basic is EventingIP */ |
| 1545 | set_linear_ip(regs, basic->ip); |
| 1546 | regs->flags = PERF_EFLAGS_EXACT; |
| 1547 | |
| 1548 | /* |
| 1549 | * The record for MEMINFO is in front of GP |
| 1550 | * But PERF_SAMPLE_TRANSACTION needs gprs->ax. |
| 1551 | * Save the pointer here but process later. |
| 1552 | */ |
| 1553 | if (format_size & PEBS_DATACFG_MEMINFO) { |
| 1554 | meminfo = next_record; |
| 1555 | next_record = meminfo + 1; |
| 1556 | } |
| 1557 | |
| 1558 | if (format_size & PEBS_DATACFG_GP) { |
| 1559 | gprs = next_record; |
| 1560 | next_record = gprs + 1; |
| 1561 | |
| 1562 | if (event->attr.precise_ip < 2) { |
| 1563 | set_linear_ip(regs, gprs->ip); |
| 1564 | regs->flags &= ~PERF_EFLAGS_EXACT; |
| 1565 | } |
| 1566 | |
| 1567 | if (sample_type & PERF_SAMPLE_REGS_INTR) |
| 1568 | adaptive_pebs_save_regs(regs, gprs); |
| 1569 | } |
| 1570 | |
| 1571 | if (format_size & PEBS_DATACFG_MEMINFO) { |
| 1572 | if (sample_type & PERF_SAMPLE_WEIGHT) |
| 1573 | data->weight = meminfo->latency ?: |
| 1574 | intel_get_tsx_weight(meminfo->tsx_tuning); |
| 1575 | |
| 1576 | if (sample_type & PERF_SAMPLE_DATA_SRC) |
| 1577 | data->data_src.val = get_data_src(event, meminfo->aux); |
| 1578 | |
| 1579 | if (sample_type & (PERF_SAMPLE_ADDR | PERF_SAMPLE_PHYS_ADDR)) |
| 1580 | data->addr = meminfo->address; |
| 1581 | |
| 1582 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
| 1583 | data->txn = intel_get_tsx_transaction(meminfo->tsx_tuning, |
| 1584 | gprs ? gprs->ax : 0); |
| 1585 | } |
| 1586 | |
| 1587 | if (format_size & PEBS_DATACFG_XMMS) { |
| 1588 | struct pebs_xmm *xmm = next_record; |
| 1589 | |
| 1590 | next_record = xmm + 1; |
| 1591 | perf_regs->xmm_regs = xmm->xmm; |
| 1592 | } |
| 1593 | |
| 1594 | if (format_size & PEBS_DATACFG_LBRS) { |
| 1595 | struct pebs_lbr *lbr = next_record; |
| 1596 | int num_lbr = ((format_size >> PEBS_DATACFG_LBR_SHIFT) |
| 1597 | & 0xff) + 1; |
| 1598 | next_record = next_record + num_lbr*sizeof(struct pebs_lbr_entry); |
| 1599 | |
| 1600 | if (has_branch_stack(event)) { |
| 1601 | intel_pmu_store_pebs_lbrs(lbr); |
| 1602 | data->br_stack = &cpuc->lbr_stack; |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | WARN_ONCE(next_record != __pebs + (format_size >> 48), |
| 1607 | "PEBS record size %llu, expected %llu, config %llx\n", |
| 1608 | format_size >> 48, |
| 1609 | (u64)(next_record - __pebs), |
| 1610 | basic->format_size); |
| 1611 | } |
| 1612 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1613 | static inline void * |
| 1614 | get_next_pebs_record_by_bit(void *base, void *top, int bit) |
| 1615 | { |
| 1616 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 1617 | void *at; |
| 1618 | u64 pebs_status; |
| 1619 | |
| 1620 | /* |
| 1621 | * fmt0 does not have a status bitfield (does not use |
| 1622 | * perf_record_nhm format) |
| 1623 | */ |
| 1624 | if (x86_pmu.intel_cap.pebs_format < 1) |
| 1625 | return base; |
| 1626 | |
| 1627 | if (base == NULL) |
| 1628 | return NULL; |
| 1629 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1630 | for (at = base; at < top; at += cpuc->pebs_record_size) { |
| 1631 | unsigned long status = get_pebs_status(at); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1632 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1633 | if (test_bit(bit, (unsigned long *)&status)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1634 | /* PEBS v3 has accurate status bits */ |
| 1635 | if (x86_pmu.intel_cap.pebs_format >= 3) |
| 1636 | return at; |
| 1637 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1638 | if (status == (1 << bit)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1639 | return at; |
| 1640 | |
| 1641 | /* clear non-PEBS bit and re-check */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1642 | pebs_status = status & cpuc->pebs_enabled; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1643 | pebs_status &= PEBS_COUNTER_MASK; |
| 1644 | if (pebs_status == (1 << bit)) |
| 1645 | return at; |
| 1646 | } |
| 1647 | } |
| 1648 | return NULL; |
| 1649 | } |
| 1650 | |
| 1651 | void intel_pmu_auto_reload_read(struct perf_event *event) |
| 1652 | { |
| 1653 | WARN_ON(!(event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)); |
| 1654 | |
| 1655 | perf_pmu_disable(event->pmu); |
| 1656 | intel_pmu_drain_pebs_buffer(); |
| 1657 | perf_pmu_enable(event->pmu); |
| 1658 | } |
| 1659 | |
| 1660 | /* |
| 1661 | * Special variant of intel_pmu_save_and_restart() for auto-reload. |
| 1662 | */ |
| 1663 | static int |
| 1664 | intel_pmu_save_and_restart_reload(struct perf_event *event, int count) |
| 1665 | { |
| 1666 | struct hw_perf_event *hwc = &event->hw; |
| 1667 | int shift = 64 - x86_pmu.cntval_bits; |
| 1668 | u64 period = hwc->sample_period; |
| 1669 | u64 prev_raw_count, new_raw_count; |
| 1670 | s64 new, old; |
| 1671 | |
| 1672 | WARN_ON(!period); |
| 1673 | |
| 1674 | /* |
| 1675 | * drain_pebs() only happens when the PMU is disabled. |
| 1676 | */ |
| 1677 | WARN_ON(this_cpu_read(cpu_hw_events.enabled)); |
| 1678 | |
| 1679 | prev_raw_count = local64_read(&hwc->prev_count); |
| 1680 | rdpmcl(hwc->event_base_rdpmc, new_raw_count); |
| 1681 | local64_set(&hwc->prev_count, new_raw_count); |
| 1682 | |
| 1683 | /* |
| 1684 | * Since the counter increments a negative counter value and |
| 1685 | * overflows on the sign switch, giving the interval: |
| 1686 | * |
| 1687 | * [-period, 0] |
| 1688 | * |
| 1689 | * the difference between two consequtive reads is: |
| 1690 | * |
| 1691 | * A) value2 - value1; |
| 1692 | * when no overflows have happened in between, |
| 1693 | * |
| 1694 | * B) (0 - value1) + (value2 - (-period)); |
| 1695 | * when one overflow happened in between, |
| 1696 | * |
| 1697 | * C) (0 - value1) + (n - 1) * (period) + (value2 - (-period)); |
| 1698 | * when @n overflows happened in between. |
| 1699 | * |
| 1700 | * Here A) is the obvious difference, B) is the extension to the |
| 1701 | * discrete interval, where the first term is to the top of the |
| 1702 | * interval and the second term is from the bottom of the next |
| 1703 | * interval and C) the extension to multiple intervals, where the |
| 1704 | * middle term is the whole intervals covered. |
| 1705 | * |
| 1706 | * An equivalent of C, by reduction, is: |
| 1707 | * |
| 1708 | * value2 - value1 + n * period |
| 1709 | */ |
| 1710 | new = ((s64)(new_raw_count << shift) >> shift); |
| 1711 | old = ((s64)(prev_raw_count << shift) >> shift); |
| 1712 | local64_add(new - old + count * period, &event->count); |
| 1713 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1714 | local64_set(&hwc->period_left, -new); |
| 1715 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1716 | perf_event_update_userpage(event); |
| 1717 | |
| 1718 | return 0; |
| 1719 | } |
| 1720 | |
| 1721 | static void __intel_pmu_pebs_event(struct perf_event *event, |
| 1722 | struct pt_regs *iregs, |
| 1723 | void *base, void *top, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1724 | int bit, int count, |
| 1725 | void (*setup_sample)(struct perf_event *, |
| 1726 | struct pt_regs *, |
| 1727 | void *, |
| 1728 | struct perf_sample_data *, |
| 1729 | struct pt_regs *)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1730 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1731 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1732 | struct hw_perf_event *hwc = &event->hw; |
| 1733 | struct perf_sample_data data; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1734 | struct x86_perf_regs perf_regs; |
| 1735 | struct pt_regs *regs = &perf_regs.regs; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1736 | void *at = get_next_pebs_record_by_bit(base, top, bit); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1737 | struct pt_regs dummy_iregs; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1738 | |
| 1739 | if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) { |
| 1740 | /* |
| 1741 | * Now, auto-reload is only enabled in fixed period mode. |
| 1742 | * The reload value is always hwc->sample_period. |
| 1743 | * May need to change it, if auto-reload is enabled in |
| 1744 | * freq mode later. |
| 1745 | */ |
| 1746 | intel_pmu_save_and_restart_reload(event, count); |
| 1747 | } else if (!intel_pmu_save_and_restart(event)) |
| 1748 | return; |
| 1749 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1750 | if (!iregs) |
| 1751 | iregs = &dummy_iregs; |
| 1752 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1753 | while (count > 1) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1754 | setup_sample(event, iregs, at, &data, regs); |
| 1755 | perf_event_output(event, &data, regs); |
| 1756 | at += cpuc->pebs_record_size; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1757 | at = get_next_pebs_record_by_bit(at, top, bit); |
| 1758 | count--; |
| 1759 | } |
| 1760 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1761 | setup_sample(event, iregs, at, &data, regs); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1762 | if (iregs == &dummy_iregs) { |
| 1763 | /* |
| 1764 | * The PEBS records may be drained in the non-overflow context, |
| 1765 | * e.g., large PEBS + context switch. Perf should treat the |
| 1766 | * last record the same as other PEBS records, and doesn't |
| 1767 | * invoke the generic overflow handler. |
| 1768 | */ |
| 1769 | perf_event_output(event, &data, regs); |
| 1770 | } else { |
| 1771 | /* |
| 1772 | * All but the last records are processed. |
| 1773 | * The last one is left to be able to call the overflow handler. |
| 1774 | */ |
| 1775 | if (perf_event_overflow(event, &data, regs)) |
| 1776 | x86_pmu_stop(event, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1777 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | static void intel_pmu_drain_pebs_core(struct pt_regs *iregs) |
| 1781 | { |
| 1782 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 1783 | struct debug_store *ds = cpuc->ds; |
| 1784 | struct perf_event *event = cpuc->events[0]; /* PMC0 only */ |
| 1785 | struct pebs_record_core *at, *top; |
| 1786 | int n; |
| 1787 | |
| 1788 | if (!x86_pmu.pebs_active) |
| 1789 | return; |
| 1790 | |
| 1791 | at = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base; |
| 1792 | top = (struct pebs_record_core *)(unsigned long)ds->pebs_index; |
| 1793 | |
| 1794 | /* |
| 1795 | * Whatever else happens, drain the thing |
| 1796 | */ |
| 1797 | ds->pebs_index = ds->pebs_buffer_base; |
| 1798 | |
| 1799 | if (!test_bit(0, cpuc->active_mask)) |
| 1800 | return; |
| 1801 | |
| 1802 | WARN_ON_ONCE(!event); |
| 1803 | |
| 1804 | if (!event->attr.precise_ip) |
| 1805 | return; |
| 1806 | |
| 1807 | n = top - at; |
| 1808 | if (n <= 0) { |
| 1809 | if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD) |
| 1810 | intel_pmu_save_and_restart_reload(event, 0); |
| 1811 | return; |
| 1812 | } |
| 1813 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1814 | __intel_pmu_pebs_event(event, iregs, at, top, 0, n, |
| 1815 | setup_pebs_fixed_sample_data); |
| 1816 | } |
| 1817 | |
| 1818 | static void intel_pmu_pebs_event_update_no_drain(struct cpu_hw_events *cpuc, int size) |
| 1819 | { |
| 1820 | struct perf_event *event; |
| 1821 | int bit; |
| 1822 | |
| 1823 | /* |
| 1824 | * The drain_pebs() could be called twice in a short period |
| 1825 | * for auto-reload event in pmu::read(). There are no |
| 1826 | * overflows have happened in between. |
| 1827 | * It needs to call intel_pmu_save_and_restart_reload() to |
| 1828 | * update the event->count for this case. |
| 1829 | */ |
| 1830 | for_each_set_bit(bit, (unsigned long *)&cpuc->pebs_enabled, size) { |
| 1831 | event = cpuc->events[bit]; |
| 1832 | if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD) |
| 1833 | intel_pmu_save_and_restart_reload(event, 0); |
| 1834 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs) |
| 1838 | { |
| 1839 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 1840 | struct debug_store *ds = cpuc->ds; |
| 1841 | struct perf_event *event; |
| 1842 | void *base, *at, *top; |
| 1843 | short counts[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {}; |
| 1844 | short error[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {}; |
| 1845 | int bit, i, size; |
| 1846 | u64 mask; |
| 1847 | |
| 1848 | if (!x86_pmu.pebs_active) |
| 1849 | return; |
| 1850 | |
| 1851 | base = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base; |
| 1852 | top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index; |
| 1853 | |
| 1854 | ds->pebs_index = ds->pebs_buffer_base; |
| 1855 | |
| 1856 | mask = (1ULL << x86_pmu.max_pebs_events) - 1; |
| 1857 | size = x86_pmu.max_pebs_events; |
| 1858 | if (x86_pmu.flags & PMU_FL_PEBS_ALL) { |
| 1859 | mask |= ((1ULL << x86_pmu.num_counters_fixed) - 1) << INTEL_PMC_IDX_FIXED; |
| 1860 | size = INTEL_PMC_IDX_FIXED + x86_pmu.num_counters_fixed; |
| 1861 | } |
| 1862 | |
| 1863 | if (unlikely(base >= top)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1864 | intel_pmu_pebs_event_update_no_drain(cpuc, size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1865 | return; |
| 1866 | } |
| 1867 | |
| 1868 | for (at = base; at < top; at += x86_pmu.pebs_record_size) { |
| 1869 | struct pebs_record_nhm *p = at; |
| 1870 | u64 pebs_status; |
| 1871 | |
| 1872 | pebs_status = p->status & cpuc->pebs_enabled; |
| 1873 | pebs_status &= mask; |
| 1874 | |
| 1875 | /* PEBS v3 has more accurate status bits */ |
| 1876 | if (x86_pmu.intel_cap.pebs_format >= 3) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1877 | for_each_set_bit(bit, (unsigned long *)&pebs_status, size) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1878 | counts[bit]++; |
| 1879 | |
| 1880 | continue; |
| 1881 | } |
| 1882 | |
| 1883 | /* |
| 1884 | * On some CPUs the PEBS status can be zero when PEBS is |
| 1885 | * racing with clearing of GLOBAL_STATUS. |
| 1886 | * |
| 1887 | * Normally we would drop that record, but in the |
| 1888 | * case when there is only a single active PEBS event |
| 1889 | * we can assume it's for that event. |
| 1890 | */ |
| 1891 | if (!pebs_status && cpuc->pebs_enabled && |
| 1892 | !(cpuc->pebs_enabled & (cpuc->pebs_enabled-1))) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1893 | pebs_status = p->status = cpuc->pebs_enabled; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1894 | |
| 1895 | bit = find_first_bit((unsigned long *)&pebs_status, |
| 1896 | x86_pmu.max_pebs_events); |
| 1897 | if (bit >= x86_pmu.max_pebs_events) |
| 1898 | continue; |
| 1899 | |
| 1900 | /* |
| 1901 | * The PEBS hardware does not deal well with the situation |
| 1902 | * when events happen near to each other and multiple bits |
| 1903 | * are set. But it should happen rarely. |
| 1904 | * |
| 1905 | * If these events include one PEBS and multiple non-PEBS |
| 1906 | * events, it doesn't impact PEBS record. The record will |
| 1907 | * be handled normally. (slow path) |
| 1908 | * |
| 1909 | * If these events include two or more PEBS events, the |
| 1910 | * records for the events can be collapsed into a single |
| 1911 | * one, and it's not possible to reconstruct all events |
| 1912 | * that caused the PEBS record. It's called collision. |
| 1913 | * If collision happened, the record will be dropped. |
| 1914 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1915 | if (pebs_status != (1ULL << bit)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1916 | for_each_set_bit(i, (unsigned long *)&pebs_status, size) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1917 | error[i]++; |
| 1918 | continue; |
| 1919 | } |
| 1920 | |
| 1921 | counts[bit]++; |
| 1922 | } |
| 1923 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1924 | for_each_set_bit(bit, (unsigned long *)&mask, size) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1925 | if ((counts[bit] == 0) && (error[bit] == 0)) |
| 1926 | continue; |
| 1927 | |
| 1928 | event = cpuc->events[bit]; |
| 1929 | if (WARN_ON_ONCE(!event)) |
| 1930 | continue; |
| 1931 | |
| 1932 | if (WARN_ON_ONCE(!event->attr.precise_ip)) |
| 1933 | continue; |
| 1934 | |
| 1935 | /* log dropped samples number */ |
| 1936 | if (error[bit]) { |
| 1937 | perf_log_lost_samples(event, error[bit]); |
| 1938 | |
| 1939 | if (perf_event_account_interrupt(event)) |
| 1940 | x86_pmu_stop(event, 0); |
| 1941 | } |
| 1942 | |
| 1943 | if (counts[bit]) { |
| 1944 | __intel_pmu_pebs_event(event, iregs, base, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1945 | top, bit, counts[bit], |
| 1946 | setup_pebs_fixed_sample_data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1947 | } |
| 1948 | } |
| 1949 | } |
| 1950 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1951 | static void intel_pmu_drain_pebs_icl(struct pt_regs *iregs) |
| 1952 | { |
| 1953 | short counts[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {}; |
| 1954 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 1955 | struct debug_store *ds = cpuc->ds; |
| 1956 | struct perf_event *event; |
| 1957 | void *base, *at, *top; |
| 1958 | int bit, size; |
| 1959 | u64 mask; |
| 1960 | |
| 1961 | if (!x86_pmu.pebs_active) |
| 1962 | return; |
| 1963 | |
| 1964 | base = (struct pebs_basic *)(unsigned long)ds->pebs_buffer_base; |
| 1965 | top = (struct pebs_basic *)(unsigned long)ds->pebs_index; |
| 1966 | |
| 1967 | ds->pebs_index = ds->pebs_buffer_base; |
| 1968 | |
| 1969 | mask = ((1ULL << x86_pmu.max_pebs_events) - 1) | |
| 1970 | (((1ULL << x86_pmu.num_counters_fixed) - 1) << INTEL_PMC_IDX_FIXED); |
| 1971 | size = INTEL_PMC_IDX_FIXED + x86_pmu.num_counters_fixed; |
| 1972 | |
| 1973 | if (unlikely(base >= top)) { |
| 1974 | intel_pmu_pebs_event_update_no_drain(cpuc, size); |
| 1975 | return; |
| 1976 | } |
| 1977 | |
| 1978 | for (at = base; at < top; at += cpuc->pebs_record_size) { |
| 1979 | u64 pebs_status; |
| 1980 | |
| 1981 | pebs_status = get_pebs_status(at) & cpuc->pebs_enabled; |
| 1982 | pebs_status &= mask; |
| 1983 | |
| 1984 | for_each_set_bit(bit, (unsigned long *)&pebs_status, size) |
| 1985 | counts[bit]++; |
| 1986 | } |
| 1987 | |
| 1988 | for_each_set_bit(bit, (unsigned long *)&mask, size) { |
| 1989 | if (counts[bit] == 0) |
| 1990 | continue; |
| 1991 | |
| 1992 | event = cpuc->events[bit]; |
| 1993 | if (WARN_ON_ONCE(!event)) |
| 1994 | continue; |
| 1995 | |
| 1996 | if (WARN_ON_ONCE(!event->attr.precise_ip)) |
| 1997 | continue; |
| 1998 | |
| 1999 | __intel_pmu_pebs_event(event, iregs, base, |
| 2000 | top, bit, counts[bit], |
| 2001 | setup_pebs_adaptive_sample_data); |
| 2002 | } |
| 2003 | } |
| 2004 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2005 | /* |
| 2006 | * BTS, PEBS probe and setup |
| 2007 | */ |
| 2008 | |
| 2009 | void __init intel_ds_init(void) |
| 2010 | { |
| 2011 | /* |
| 2012 | * No support for 32bit formats |
| 2013 | */ |
| 2014 | if (!boot_cpu_has(X86_FEATURE_DTES64)) |
| 2015 | return; |
| 2016 | |
| 2017 | x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS); |
| 2018 | x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS); |
| 2019 | x86_pmu.pebs_buffer_size = PEBS_BUFFER_SIZE; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2020 | if (x86_pmu.version <= 4) |
| 2021 | x86_pmu.pebs_no_isolation = 1; |
| 2022 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2023 | if (x86_pmu.pebs) { |
| 2024 | char pebs_type = x86_pmu.intel_cap.pebs_trap ? '+' : '-'; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2025 | char *pebs_qual = ""; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2026 | int format = x86_pmu.intel_cap.pebs_format; |
| 2027 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2028 | if (format < 4) |
| 2029 | x86_pmu.intel_cap.pebs_baseline = 0; |
| 2030 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2031 | switch (format) { |
| 2032 | case 0: |
| 2033 | pr_cont("PEBS fmt0%c, ", pebs_type); |
| 2034 | x86_pmu.pebs_record_size = sizeof(struct pebs_record_core); |
| 2035 | /* |
| 2036 | * Using >PAGE_SIZE buffers makes the WRMSR to |
| 2037 | * PERF_GLOBAL_CTRL in intel_pmu_enable_all() |
| 2038 | * mysteriously hang on Core2. |
| 2039 | * |
| 2040 | * As a workaround, we don't do this. |
| 2041 | */ |
| 2042 | x86_pmu.pebs_buffer_size = PAGE_SIZE; |
| 2043 | x86_pmu.drain_pebs = intel_pmu_drain_pebs_core; |
| 2044 | break; |
| 2045 | |
| 2046 | case 1: |
| 2047 | pr_cont("PEBS fmt1%c, ", pebs_type); |
| 2048 | x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm); |
| 2049 | x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm; |
| 2050 | break; |
| 2051 | |
| 2052 | case 2: |
| 2053 | pr_cont("PEBS fmt2%c, ", pebs_type); |
| 2054 | x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw); |
| 2055 | x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm; |
| 2056 | break; |
| 2057 | |
| 2058 | case 3: |
| 2059 | pr_cont("PEBS fmt3%c, ", pebs_type); |
| 2060 | x86_pmu.pebs_record_size = |
| 2061 | sizeof(struct pebs_record_skl); |
| 2062 | x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm; |
| 2063 | x86_pmu.large_pebs_flags |= PERF_SAMPLE_TIME; |
| 2064 | break; |
| 2065 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2066 | case 4: |
| 2067 | x86_pmu.drain_pebs = intel_pmu_drain_pebs_icl; |
| 2068 | x86_pmu.pebs_record_size = sizeof(struct pebs_basic); |
| 2069 | if (x86_pmu.intel_cap.pebs_baseline) { |
| 2070 | x86_pmu.large_pebs_flags |= |
| 2071 | PERF_SAMPLE_BRANCH_STACK | |
| 2072 | PERF_SAMPLE_TIME; |
| 2073 | x86_pmu.flags |= PMU_FL_PEBS_ALL; |
| 2074 | pebs_qual = "-baseline"; |
| 2075 | x86_get_pmu()->capabilities |= PERF_PMU_CAP_EXTENDED_REGS; |
| 2076 | } else { |
| 2077 | /* Only basic record supported */ |
| 2078 | x86_pmu.large_pebs_flags &= |
| 2079 | ~(PERF_SAMPLE_ADDR | |
| 2080 | PERF_SAMPLE_TIME | |
| 2081 | PERF_SAMPLE_DATA_SRC | |
| 2082 | PERF_SAMPLE_TRANSACTION | |
| 2083 | PERF_SAMPLE_REGS_USER | |
| 2084 | PERF_SAMPLE_REGS_INTR); |
| 2085 | } |
| 2086 | pr_cont("PEBS fmt4%c%s, ", pebs_type, pebs_qual); |
| 2087 | |
| 2088 | if (x86_pmu.intel_cap.pebs_output_pt_available) { |
| 2089 | pr_cont("PEBS-via-PT, "); |
| 2090 | x86_get_pmu()->capabilities |= PERF_PMU_CAP_AUX_OUTPUT; |
| 2091 | } |
| 2092 | |
| 2093 | break; |
| 2094 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2095 | default: |
| 2096 | pr_cont("no PEBS fmt%d%c, ", format, pebs_type); |
| 2097 | x86_pmu.pebs = 0; |
| 2098 | } |
| 2099 | } |
| 2100 | } |
| 2101 | |
| 2102 | void perf_restore_debug_store(void) |
| 2103 | { |
| 2104 | struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds); |
| 2105 | |
| 2106 | if (!x86_pmu.bts && !x86_pmu.pebs) |
| 2107 | return; |
| 2108 | |
| 2109 | wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds); |
| 2110 | } |