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> 6 7 #include <asm/cpu_entry_area.h> 8 #include <asm/debugreg.h> 9 #include <asm/perf_event.h> 10 #include <asm/tlbflush.h> 11 #include <asm/insn.h> 12 #include <asm/io.h> 13 #include <asm/msr.h> 14 #include <asm/timer.h> 15 16 #include "../perf_event.h" 17 18 /* Waste a full page so it can be mapped into the cpu_entry_area */ 19 DEFINE_PER_CPU_PAGE_ALIGNED(struct debug_store, cpu_debug_store); 20 21 /* The size of a BTS record in bytes: */ 22 #define BTS_RECORD_SIZE 24 23 24 #define PEBS_FIXUP_SIZE PAGE_SIZE 25 26 /* 27 * pebs_record_32 for p4 and core not supported 28 29 struct pebs_record_32 { 30 u32 flags, ip; 31 u32 ax, bc, cx, dx; 32 u32 si, di, bp, sp; 33 }; 34 35 */ 36 37 union intel_x86_pebs_dse { 38 u64 val; 39 struct { 40 unsigned int ld_dse:4; 41 unsigned int ld_stlb_miss:1; 42 unsigned int ld_locked:1; 43 unsigned int ld_data_blk:1; 44 unsigned int ld_addr_blk:1; 45 unsigned int ld_reserved:24; 46 }; 47 struct { 48 unsigned int st_l1d_hit:1; 49 unsigned int st_reserved1:3; 50 unsigned int st_stlb_miss:1; 51 unsigned int st_locked:1; 52 unsigned int st_reserved2:26; 53 }; 54 struct { 55 unsigned int st_lat_dse:4; 56 unsigned int st_lat_stlb_miss:1; 57 unsigned int st_lat_locked:1; 58 unsigned int ld_reserved3:26; 59 }; 60 struct { 61 unsigned int mtl_dse:5; 62 unsigned int mtl_locked:1; 63 unsigned int mtl_stlb_miss:1; 64 unsigned int mtl_fwd_blk:1; 65 unsigned int ld_reserved4:24; 66 }; 67 struct { 68 unsigned int lnc_dse:8; 69 unsigned int ld_reserved5:2; 70 unsigned int lnc_stlb_miss:1; 71 unsigned int lnc_locked:1; 72 unsigned int lnc_data_blk:1; 73 unsigned int lnc_addr_blk:1; 74 unsigned int ld_reserved6:18; 75 }; 76 }; 77 78 79 /* 80 * Map PEBS Load Latency Data Source encodings to generic 81 * memory data source information 82 */ 83 #define P(a, b) PERF_MEM_S(a, b) 84 #define OP_LH (P(OP, LOAD) | P(LVL, HIT)) 85 #define LEVEL(x) P(LVLNUM, x) 86 #define REM P(REMOTE, REMOTE) 87 #define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS)) 88 89 /* Version for Sandy Bridge and later */ 90 static u64 pebs_data_source[PERF_PEBS_DATA_SOURCE_MAX] = { 91 P(OP, LOAD) | P(LVL, MISS) | LEVEL(L3) | P(SNOOP, NA),/* 0x00:ukn L3 */ 92 OP_LH | P(LVL, L1) | LEVEL(L1) | P(SNOOP, NONE), /* 0x01: L1 local */ 93 OP_LH | P(LVL, LFB) | LEVEL(LFB) | P(SNOOP, NONE), /* 0x02: LFB hit */ 94 OP_LH | P(LVL, L2) | LEVEL(L2) | P(SNOOP, NONE), /* 0x03: L2 hit */ 95 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, NONE), /* 0x04: L3 hit */ 96 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, MISS), /* 0x05: L3 hit, snoop miss */ 97 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT), /* 0x06: L3 hit, snoop hit */ 98 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM), /* 0x07: L3 hit, snoop hitm */ 99 OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HIT), /* 0x08: L3 miss snoop hit */ 100 OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HITM), /* 0x09: L3 miss snoop hitm*/ 101 OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | P(SNOOP, HIT), /* 0x0a: L3 miss, shared */ 102 OP_LH | P(LVL, REM_RAM1) | REM | LEVEL(L3) | P(SNOOP, HIT), /* 0x0b: L3 miss, shared */ 103 OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | SNOOP_NONE_MISS, /* 0x0c: L3 miss, excl */ 104 OP_LH | P(LVL, REM_RAM1) | LEVEL(RAM) | REM | SNOOP_NONE_MISS, /* 0x0d: L3 miss, excl */ 105 OP_LH | P(LVL, IO) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0e: I/O */ 106 OP_LH | P(LVL, UNC) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0f: uncached */ 107 }; 108 109 /* Patch up minor differences in the bits */ 110 void __init intel_pmu_pebs_data_source_nhm(void) 111 { 112 pebs_data_source[0x05] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT); 113 pebs_data_source[0x06] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM); 114 pebs_data_source[0x07] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM); 115 } 116 117 static void __init __intel_pmu_pebs_data_source_skl(bool pmem, u64 *data_source) 118 { 119 u64 pmem_or_l4 = pmem ? LEVEL(PMEM) : LEVEL(L4); 120 121 data_source[0x08] = OP_LH | pmem_or_l4 | P(SNOOP, HIT); 122 data_source[0x09] = OP_LH | pmem_or_l4 | REM | P(SNOOP, HIT); 123 data_source[0x0b] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, NONE); 124 data_source[0x0c] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOPX, FWD); 125 data_source[0x0d] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOP, HITM); 126 } 127 128 void __init intel_pmu_pebs_data_source_skl(bool pmem) 129 { 130 __intel_pmu_pebs_data_source_skl(pmem, pebs_data_source); 131 } 132 133 static void __init __intel_pmu_pebs_data_source_grt(u64 *data_source) 134 { 135 data_source[0x05] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT); 136 data_source[0x06] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM); 137 data_source[0x08] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOPX, FWD); 138 } 139 140 void __init intel_pmu_pebs_data_source_grt(void) 141 { 142 __intel_pmu_pebs_data_source_grt(pebs_data_source); 143 } 144 145 void __init intel_pmu_pebs_data_source_adl(void) 146 { 147 u64 *data_source; 148 149 data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX].pebs_data_source; 150 memcpy(data_source, pebs_data_source, sizeof(pebs_data_source)); 151 __intel_pmu_pebs_data_source_skl(false, data_source); 152 153 data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_ATOM_IDX].pebs_data_source; 154 memcpy(data_source, pebs_data_source, sizeof(pebs_data_source)); 155 __intel_pmu_pebs_data_source_grt(data_source); 156 } 157 158 static void __init __intel_pmu_pebs_data_source_cmt(u64 *data_source) 159 { 160 data_source[0x07] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOPX, FWD); 161 data_source[0x08] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM); 162 data_source[0x0a] = OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | P(SNOOP, NONE); 163 data_source[0x0b] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, NONE); 164 data_source[0x0c] = OP_LH | LEVEL(RAM) | REM | P(SNOOPX, FWD); 165 data_source[0x0d] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, HITM); 166 } 167 168 void __init intel_pmu_pebs_data_source_mtl(void) 169 { 170 u64 *data_source; 171 172 data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX].pebs_data_source; 173 memcpy(data_source, pebs_data_source, sizeof(pebs_data_source)); 174 __intel_pmu_pebs_data_source_skl(false, data_source); 175 176 data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_ATOM_IDX].pebs_data_source; 177 memcpy(data_source, pebs_data_source, sizeof(pebs_data_source)); 178 __intel_pmu_pebs_data_source_cmt(data_source); 179 } 180 181 void __init intel_pmu_pebs_data_source_arl_h(void) 182 { 183 u64 *data_source; 184 185 intel_pmu_pebs_data_source_lnl(); 186 187 data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_TINY_IDX].pebs_data_source; 188 memcpy(data_source, pebs_data_source, sizeof(pebs_data_source)); 189 __intel_pmu_pebs_data_source_cmt(data_source); 190 } 191 192 void __init intel_pmu_pebs_data_source_cmt(void) 193 { 194 __intel_pmu_pebs_data_source_cmt(pebs_data_source); 195 } 196 197 /* Version for Lion Cove and later */ 198 static u64 lnc_pebs_data_source[PERF_PEBS_DATA_SOURCE_MAX] = { 199 P(OP, LOAD) | P(LVL, MISS) | LEVEL(L3) | P(SNOOP, NA), /* 0x00: ukn L3 */ 200 OP_LH | P(LVL, L1) | LEVEL(L1) | P(SNOOP, NONE), /* 0x01: L1 hit */ 201 OP_LH | P(LVL, L1) | LEVEL(L1) | P(SNOOP, NONE), /* 0x02: L1 hit */ 202 OP_LH | P(LVL, LFB) | LEVEL(LFB) | P(SNOOP, NONE), /* 0x03: LFB/L1 Miss Handling Buffer hit */ 203 0, /* 0x04: Reserved */ 204 OP_LH | P(LVL, L2) | LEVEL(L2) | P(SNOOP, NONE), /* 0x05: L2 Hit */ 205 OP_LH | LEVEL(L2_MHB) | P(SNOOP, NONE), /* 0x06: L2 Miss Handling Buffer Hit */ 206 0, /* 0x07: Reserved */ 207 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, NONE), /* 0x08: L3 Hit */ 208 0, /* 0x09: Reserved */ 209 0, /* 0x0a: Reserved */ 210 0, /* 0x0b: Reserved */ 211 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOPX, FWD), /* 0x0c: L3 Hit Snoop Fwd */ 212 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM), /* 0x0d: L3 Hit Snoop HitM */ 213 0, /* 0x0e: Reserved */ 214 P(OP, LOAD) | P(LVL, MISS) | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM), /* 0x0f: L3 Miss Snoop HitM */ 215 OP_LH | LEVEL(MSC) | P(SNOOP, NONE), /* 0x10: Memory-side Cache Hit */ 216 OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | P(SNOOP, NONE), /* 0x11: Local Memory Hit */ 217 }; 218 219 void __init intel_pmu_pebs_data_source_lnl(void) 220 { 221 u64 *data_source; 222 223 data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX].pebs_data_source; 224 memcpy(data_source, lnc_pebs_data_source, sizeof(lnc_pebs_data_source)); 225 226 data_source = x86_pmu.hybrid_pmu[X86_HYBRID_PMU_ATOM_IDX].pebs_data_source; 227 memcpy(data_source, pebs_data_source, sizeof(pebs_data_source)); 228 __intel_pmu_pebs_data_source_cmt(data_source); 229 } 230 231 static u64 precise_store_data(u64 status) 232 { 233 union intel_x86_pebs_dse dse; 234 u64 val = P(OP, STORE) | P(SNOOP, NA) | P(LVL, L1) | P(TLB, L2); 235 236 dse.val = status; 237 238 /* 239 * bit 4: TLB access 240 * 1 = stored missed 2nd level TLB 241 * 242 * so it either hit the walker or the OS 243 * otherwise hit 2nd level TLB 244 */ 245 if (dse.st_stlb_miss) 246 val |= P(TLB, MISS); 247 else 248 val |= P(TLB, HIT); 249 250 /* 251 * bit 0: hit L1 data cache 252 * if not set, then all we know is that 253 * it missed L1D 254 */ 255 if (dse.st_l1d_hit) 256 val |= P(LVL, HIT); 257 else 258 val |= P(LVL, MISS); 259 260 /* 261 * bit 5: Locked prefix 262 */ 263 if (dse.st_locked) 264 val |= P(LOCK, LOCKED); 265 266 return val; 267 } 268 269 static u64 precise_datala_hsw(struct perf_event *event, u64 status) 270 { 271 union perf_mem_data_src dse; 272 273 dse.val = PERF_MEM_NA; 274 275 if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) 276 dse.mem_op = PERF_MEM_OP_STORE; 277 else if (event->hw.flags & PERF_X86_EVENT_PEBS_LD_HSW) 278 dse.mem_op = PERF_MEM_OP_LOAD; 279 280 /* 281 * L1 info only valid for following events: 282 * 283 * MEM_UOPS_RETIRED.STLB_MISS_STORES 284 * MEM_UOPS_RETIRED.LOCK_STORES 285 * MEM_UOPS_RETIRED.SPLIT_STORES 286 * MEM_UOPS_RETIRED.ALL_STORES 287 */ 288 if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) { 289 if (status & 1) 290 dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_HIT; 291 else 292 dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_MISS; 293 } 294 return dse.val; 295 } 296 297 static inline void pebs_set_tlb_lock(u64 *val, bool tlb, bool lock) 298 { 299 /* 300 * TLB access 301 * 0 = did not miss 2nd level TLB 302 * 1 = missed 2nd level TLB 303 */ 304 if (tlb) 305 *val |= P(TLB, MISS) | P(TLB, L2); 306 else 307 *val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2); 308 309 /* locked prefix */ 310 if (lock) 311 *val |= P(LOCK, LOCKED); 312 } 313 314 /* Retrieve the latency data for e-core of ADL */ 315 static u64 __grt_latency_data(struct perf_event *event, u64 status, 316 u8 dse, bool tlb, bool lock, bool blk) 317 { 318 u64 val; 319 320 WARN_ON_ONCE(hybrid_pmu(event->pmu)->pmu_type == hybrid_big); 321 322 dse &= PERF_PEBS_DATA_SOURCE_GRT_MASK; 323 val = hybrid_var(event->pmu, pebs_data_source)[dse]; 324 325 pebs_set_tlb_lock(&val, tlb, lock); 326 327 if (blk) 328 val |= P(BLK, DATA); 329 else 330 val |= P(BLK, NA); 331 332 return val; 333 } 334 335 u64 grt_latency_data(struct perf_event *event, u64 status) 336 { 337 union intel_x86_pebs_dse dse; 338 339 dse.val = status; 340 341 return __grt_latency_data(event, status, dse.ld_dse, 342 dse.ld_locked, dse.ld_stlb_miss, 343 dse.ld_data_blk); 344 } 345 346 /* Retrieve the latency data for e-core of MTL */ 347 u64 cmt_latency_data(struct perf_event *event, u64 status) 348 { 349 union intel_x86_pebs_dse dse; 350 351 dse.val = status; 352 353 return __grt_latency_data(event, status, dse.mtl_dse, 354 dse.mtl_stlb_miss, dse.mtl_locked, 355 dse.mtl_fwd_blk); 356 } 357 358 static u64 lnc_latency_data(struct perf_event *event, u64 status) 359 { 360 union intel_x86_pebs_dse dse; 361 union perf_mem_data_src src; 362 u64 val; 363 364 dse.val = status; 365 366 /* LNC core latency data */ 367 val = hybrid_var(event->pmu, pebs_data_source)[status & PERF_PEBS_DATA_SOURCE_MASK]; 368 if (!val) 369 val = P(OP, LOAD) | LEVEL(NA) | P(SNOOP, NA); 370 371 if (dse.lnc_stlb_miss) 372 val |= P(TLB, MISS) | P(TLB, L2); 373 else 374 val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2); 375 376 if (dse.lnc_locked) 377 val |= P(LOCK, LOCKED); 378 379 if (dse.lnc_data_blk) 380 val |= P(BLK, DATA); 381 if (dse.lnc_addr_blk) 382 val |= P(BLK, ADDR); 383 if (!dse.lnc_data_blk && !dse.lnc_addr_blk) 384 val |= P(BLK, NA); 385 386 src.val = val; 387 if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) 388 src.mem_op = P(OP, STORE); 389 390 return src.val; 391 } 392 393 u64 lnl_latency_data(struct perf_event *event, u64 status) 394 { 395 struct x86_hybrid_pmu *pmu = hybrid_pmu(event->pmu); 396 397 if (pmu->pmu_type == hybrid_small) 398 return cmt_latency_data(event, status); 399 400 return lnc_latency_data(event, status); 401 } 402 403 u64 arl_h_latency_data(struct perf_event *event, u64 status) 404 { 405 struct x86_hybrid_pmu *pmu = hybrid_pmu(event->pmu); 406 407 if (pmu->pmu_type == hybrid_tiny) 408 return cmt_latency_data(event, status); 409 410 return lnl_latency_data(event, status); 411 } 412 413 static u64 load_latency_data(struct perf_event *event, u64 status) 414 { 415 union intel_x86_pebs_dse dse; 416 u64 val; 417 418 dse.val = status; 419 420 /* 421 * use the mapping table for bit 0-3 422 */ 423 val = hybrid_var(event->pmu, pebs_data_source)[dse.ld_dse]; 424 425 /* 426 * Nehalem models do not support TLB, Lock infos 427 */ 428 if (x86_pmu.pebs_no_tlb) { 429 val |= P(TLB, NA) | P(LOCK, NA); 430 return val; 431 } 432 433 pebs_set_tlb_lock(&val, dse.ld_stlb_miss, dse.ld_locked); 434 435 /* 436 * Ice Lake and earlier models do not support block infos. 437 */ 438 if (!x86_pmu.pebs_block) { 439 val |= P(BLK, NA); 440 return val; 441 } 442 /* 443 * bit 6: load was blocked since its data could not be forwarded 444 * from a preceding store 445 */ 446 if (dse.ld_data_blk) 447 val |= P(BLK, DATA); 448 449 /* 450 * bit 7: load was blocked due to potential address conflict with 451 * a preceding store 452 */ 453 if (dse.ld_addr_blk) 454 val |= P(BLK, ADDR); 455 456 if (!dse.ld_data_blk && !dse.ld_addr_blk) 457 val |= P(BLK, NA); 458 459 return val; 460 } 461 462 static u64 store_latency_data(struct perf_event *event, u64 status) 463 { 464 union intel_x86_pebs_dse dse; 465 union perf_mem_data_src src; 466 u64 val; 467 468 dse.val = status; 469 470 /* 471 * use the mapping table for bit 0-3 472 */ 473 val = hybrid_var(event->pmu, pebs_data_source)[dse.st_lat_dse]; 474 475 pebs_set_tlb_lock(&val, dse.st_lat_stlb_miss, dse.st_lat_locked); 476 477 val |= P(BLK, NA); 478 479 /* 480 * the pebs_data_source table is only for loads 481 * so override the mem_op to say STORE instead 482 */ 483 src.val = val; 484 src.mem_op = P(OP,STORE); 485 486 return src.val; 487 } 488 489 struct pebs_record_core { 490 u64 flags, ip; 491 u64 ax, bx, cx, dx; 492 u64 si, di, bp, sp; 493 u64 r8, r9, r10, r11; 494 u64 r12, r13, r14, r15; 495 }; 496 497 struct pebs_record_nhm { 498 u64 flags, ip; 499 u64 ax, bx, cx, dx; 500 u64 si, di, bp, sp; 501 u64 r8, r9, r10, r11; 502 u64 r12, r13, r14, r15; 503 u64 status, dla, dse, lat; 504 }; 505 506 /* 507 * Same as pebs_record_nhm, with two additional fields. 508 */ 509 struct pebs_record_hsw { 510 u64 flags, ip; 511 u64 ax, bx, cx, dx; 512 u64 si, di, bp, sp; 513 u64 r8, r9, r10, r11; 514 u64 r12, r13, r14, r15; 515 u64 status, dla, dse, lat; 516 u64 real_ip, tsx_tuning; 517 }; 518 519 union hsw_tsx_tuning { 520 struct { 521 u32 cycles_last_block : 32, 522 hle_abort : 1, 523 rtm_abort : 1, 524 instruction_abort : 1, 525 non_instruction_abort : 1, 526 retry : 1, 527 data_conflict : 1, 528 capacity_writes : 1, 529 capacity_reads : 1; 530 }; 531 u64 value; 532 }; 533 534 #define PEBS_HSW_TSX_FLAGS 0xff00000000ULL 535 536 /* Same as HSW, plus TSC */ 537 538 struct pebs_record_skl { 539 u64 flags, ip; 540 u64 ax, bx, cx, dx; 541 u64 si, di, bp, sp; 542 u64 r8, r9, r10, r11; 543 u64 r12, r13, r14, r15; 544 u64 status, dla, dse, lat; 545 u64 real_ip, tsx_tuning; 546 u64 tsc; 547 }; 548 549 void init_debug_store_on_cpu(int cpu) 550 { 551 struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; 552 553 if (!ds) 554 return; 555 556 wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 557 (u32)((u64)(unsigned long)ds), 558 (u32)((u64)(unsigned long)ds >> 32)); 559 } 560 561 void fini_debug_store_on_cpu(int cpu) 562 { 563 if (!per_cpu(cpu_hw_events, cpu).ds) 564 return; 565 566 wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0); 567 } 568 569 static DEFINE_PER_CPU(void *, insn_buffer); 570 571 static void ds_update_cea(void *cea, void *addr, size_t size, pgprot_t prot) 572 { 573 unsigned long start = (unsigned long)cea; 574 phys_addr_t pa; 575 size_t msz = 0; 576 577 pa = virt_to_phys(addr); 578 579 preempt_disable(); 580 for (; msz < size; msz += PAGE_SIZE, pa += PAGE_SIZE, cea += PAGE_SIZE) 581 cea_set_pte(cea, pa, prot); 582 583 /* 584 * This is a cross-CPU update of the cpu_entry_area, we must shoot down 585 * all TLB entries for it. 586 */ 587 flush_tlb_kernel_range(start, start + size); 588 preempt_enable(); 589 } 590 591 static void ds_clear_cea(void *cea, size_t size) 592 { 593 unsigned long start = (unsigned long)cea; 594 size_t msz = 0; 595 596 preempt_disable(); 597 for (; msz < size; msz += PAGE_SIZE, cea += PAGE_SIZE) 598 cea_set_pte(cea, 0, PAGE_NONE); 599 600 flush_tlb_kernel_range(start, start + size); 601 preempt_enable(); 602 } 603 604 static void *dsalloc_pages(size_t size, gfp_t flags, int cpu) 605 { 606 unsigned int order = get_order(size); 607 int node = cpu_to_node(cpu); 608 struct page *page; 609 610 page = __alloc_pages_node(node, flags | __GFP_ZERO, order); 611 return page ? page_address(page) : NULL; 612 } 613 614 static void dsfree_pages(const void *buffer, size_t size) 615 { 616 if (buffer) 617 free_pages((unsigned long)buffer, get_order(size)); 618 } 619 620 static int alloc_pebs_buffer(int cpu) 621 { 622 struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu); 623 struct debug_store *ds = hwev->ds; 624 size_t bsiz = x86_pmu.pebs_buffer_size; 625 int max, node = cpu_to_node(cpu); 626 void *buffer, *insn_buff, *cea; 627 628 if (!x86_pmu.pebs) 629 return 0; 630 631 buffer = dsalloc_pages(bsiz, GFP_KERNEL, cpu); 632 if (unlikely(!buffer)) 633 return -ENOMEM; 634 635 /* 636 * HSW+ already provides us the eventing ip; no need to allocate this 637 * buffer then. 638 */ 639 if (x86_pmu.intel_cap.pebs_format < 2) { 640 insn_buff = kzalloc_node(PEBS_FIXUP_SIZE, GFP_KERNEL, node); 641 if (!insn_buff) { 642 dsfree_pages(buffer, bsiz); 643 return -ENOMEM; 644 } 645 per_cpu(insn_buffer, cpu) = insn_buff; 646 } 647 hwev->ds_pebs_vaddr = buffer; 648 /* Update the cpu entry area mapping */ 649 cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer; 650 ds->pebs_buffer_base = (unsigned long) cea; 651 ds_update_cea(cea, buffer, bsiz, PAGE_KERNEL); 652 ds->pebs_index = ds->pebs_buffer_base; 653 max = x86_pmu.pebs_record_size * (bsiz / x86_pmu.pebs_record_size); 654 ds->pebs_absolute_maximum = ds->pebs_buffer_base + max; 655 return 0; 656 } 657 658 static void release_pebs_buffer(int cpu) 659 { 660 struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu); 661 void *cea; 662 663 if (!x86_pmu.pebs) 664 return; 665 666 kfree(per_cpu(insn_buffer, cpu)); 667 per_cpu(insn_buffer, cpu) = NULL; 668 669 /* Clear the fixmap */ 670 cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer; 671 ds_clear_cea(cea, x86_pmu.pebs_buffer_size); 672 dsfree_pages(hwev->ds_pebs_vaddr, x86_pmu.pebs_buffer_size); 673 hwev->ds_pebs_vaddr = NULL; 674 } 675 676 static int alloc_bts_buffer(int cpu) 677 { 678 struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu); 679 struct debug_store *ds = hwev->ds; 680 void *buffer, *cea; 681 int max; 682 683 if (!x86_pmu.bts) 684 return 0; 685 686 buffer = dsalloc_pages(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, cpu); 687 if (unlikely(!buffer)) { 688 WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__); 689 return -ENOMEM; 690 } 691 hwev->ds_bts_vaddr = buffer; 692 /* Update the fixmap */ 693 cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer; 694 ds->bts_buffer_base = (unsigned long) cea; 695 ds_update_cea(cea, buffer, BTS_BUFFER_SIZE, PAGE_KERNEL); 696 ds->bts_index = ds->bts_buffer_base; 697 max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE; 698 ds->bts_absolute_maximum = ds->bts_buffer_base + 699 max * BTS_RECORD_SIZE; 700 ds->bts_interrupt_threshold = ds->bts_absolute_maximum - 701 (max / 16) * BTS_RECORD_SIZE; 702 return 0; 703 } 704 705 static void release_bts_buffer(int cpu) 706 { 707 struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu); 708 void *cea; 709 710 if (!x86_pmu.bts) 711 return; 712 713 /* Clear the fixmap */ 714 cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer; 715 ds_clear_cea(cea, BTS_BUFFER_SIZE); 716 dsfree_pages(hwev->ds_bts_vaddr, BTS_BUFFER_SIZE); 717 hwev->ds_bts_vaddr = NULL; 718 } 719 720 static int alloc_ds_buffer(int cpu) 721 { 722 struct debug_store *ds = &get_cpu_entry_area(cpu)->cpu_debug_store; 723 724 memset(ds, 0, sizeof(*ds)); 725 per_cpu(cpu_hw_events, cpu).ds = ds; 726 return 0; 727 } 728 729 static void release_ds_buffer(int cpu) 730 { 731 per_cpu(cpu_hw_events, cpu).ds = NULL; 732 } 733 734 void release_ds_buffers(void) 735 { 736 int cpu; 737 738 if (!x86_pmu.bts && !x86_pmu.pebs) 739 return; 740 741 for_each_possible_cpu(cpu) 742 release_ds_buffer(cpu); 743 744 for_each_possible_cpu(cpu) { 745 /* 746 * Again, ignore errors from offline CPUs, they will no longer 747 * observe cpu_hw_events.ds and not program the DS_AREA when 748 * they come up. 749 */ 750 fini_debug_store_on_cpu(cpu); 751 } 752 753 for_each_possible_cpu(cpu) { 754 release_pebs_buffer(cpu); 755 release_bts_buffer(cpu); 756 } 757 } 758 759 void reserve_ds_buffers(void) 760 { 761 int bts_err = 0, pebs_err = 0; 762 int cpu; 763 764 x86_pmu.bts_active = 0; 765 x86_pmu.pebs_active = 0; 766 767 if (!x86_pmu.bts && !x86_pmu.pebs) 768 return; 769 770 if (!x86_pmu.bts) 771 bts_err = 1; 772 773 if (!x86_pmu.pebs) 774 pebs_err = 1; 775 776 for_each_possible_cpu(cpu) { 777 if (alloc_ds_buffer(cpu)) { 778 bts_err = 1; 779 pebs_err = 1; 780 } 781 782 if (!bts_err && alloc_bts_buffer(cpu)) 783 bts_err = 1; 784 785 if (!pebs_err && alloc_pebs_buffer(cpu)) 786 pebs_err = 1; 787 788 if (bts_err && pebs_err) 789 break; 790 } 791 792 if (bts_err) { 793 for_each_possible_cpu(cpu) 794 release_bts_buffer(cpu); 795 } 796 797 if (pebs_err) { 798 for_each_possible_cpu(cpu) 799 release_pebs_buffer(cpu); 800 } 801 802 if (bts_err && pebs_err) { 803 for_each_possible_cpu(cpu) 804 release_ds_buffer(cpu); 805 } else { 806 if (x86_pmu.bts && !bts_err) 807 x86_pmu.bts_active = 1; 808 809 if (x86_pmu.pebs && !pebs_err) 810 x86_pmu.pebs_active = 1; 811 812 for_each_possible_cpu(cpu) { 813 /* 814 * Ignores wrmsr_on_cpu() errors for offline CPUs they 815 * will get this call through intel_pmu_cpu_starting(). 816 */ 817 init_debug_store_on_cpu(cpu); 818 } 819 } 820 } 821 822 /* 823 * BTS 824 */ 825 826 struct event_constraint bts_constraint = 827 EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0); 828 829 void intel_pmu_enable_bts(u64 config) 830 { 831 unsigned long debugctlmsr; 832 833 debugctlmsr = get_debugctlmsr(); 834 835 debugctlmsr |= DEBUGCTLMSR_TR; 836 debugctlmsr |= DEBUGCTLMSR_BTS; 837 if (config & ARCH_PERFMON_EVENTSEL_INT) 838 debugctlmsr |= DEBUGCTLMSR_BTINT; 839 840 if (!(config & ARCH_PERFMON_EVENTSEL_OS)) 841 debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS; 842 843 if (!(config & ARCH_PERFMON_EVENTSEL_USR)) 844 debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR; 845 846 update_debugctlmsr(debugctlmsr); 847 } 848 849 void intel_pmu_disable_bts(void) 850 { 851 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 852 unsigned long debugctlmsr; 853 854 if (!cpuc->ds) 855 return; 856 857 debugctlmsr = get_debugctlmsr(); 858 859 debugctlmsr &= 860 ~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT | 861 DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR); 862 863 update_debugctlmsr(debugctlmsr); 864 } 865 866 int intel_pmu_drain_bts_buffer(void) 867 { 868 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 869 struct debug_store *ds = cpuc->ds; 870 struct bts_record { 871 u64 from; 872 u64 to; 873 u64 flags; 874 }; 875 struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS]; 876 struct bts_record *at, *base, *top; 877 struct perf_output_handle handle; 878 struct perf_event_header header; 879 struct perf_sample_data data; 880 unsigned long skip = 0; 881 struct pt_regs regs; 882 883 if (!event) 884 return 0; 885 886 if (!x86_pmu.bts_active) 887 return 0; 888 889 base = (struct bts_record *)(unsigned long)ds->bts_buffer_base; 890 top = (struct bts_record *)(unsigned long)ds->bts_index; 891 892 if (top <= base) 893 return 0; 894 895 memset(®s, 0, sizeof(regs)); 896 897 ds->bts_index = ds->bts_buffer_base; 898 899 perf_sample_data_init(&data, 0, event->hw.last_period); 900 901 /* 902 * BTS leaks kernel addresses in branches across the cpl boundary, 903 * such as traps or system calls, so unless the user is asking for 904 * kernel tracing (and right now it's not possible), we'd need to 905 * filter them out. But first we need to count how many of those we 906 * have in the current batch. This is an extra O(n) pass, however, 907 * it's much faster than the other one especially considering that 908 * n <= 2560 (BTS_BUFFER_SIZE / BTS_RECORD_SIZE * 15/16; see the 909 * alloc_bts_buffer()). 910 */ 911 for (at = base; at < top; at++) { 912 /* 913 * Note that right now *this* BTS code only works if 914 * attr::exclude_kernel is set, but let's keep this extra 915 * check here in case that changes. 916 */ 917 if (event->attr.exclude_kernel && 918 (kernel_ip(at->from) || kernel_ip(at->to))) 919 skip++; 920 } 921 922 /* 923 * Prepare a generic sample, i.e. fill in the invariant fields. 924 * We will overwrite the from and to address before we output 925 * the sample. 926 */ 927 rcu_read_lock(); 928 perf_prepare_sample(&data, event, ®s); 929 perf_prepare_header(&header, &data, event, ®s); 930 931 if (perf_output_begin(&handle, &data, event, 932 header.size * (top - base - skip))) 933 goto unlock; 934 935 for (at = base; at < top; at++) { 936 /* Filter out any records that contain kernel addresses. */ 937 if (event->attr.exclude_kernel && 938 (kernel_ip(at->from) || kernel_ip(at->to))) 939 continue; 940 941 data.ip = at->from; 942 data.addr = at->to; 943 944 perf_output_sample(&handle, &header, &data, event); 945 } 946 947 perf_output_end(&handle); 948 949 /* There's new data available. */ 950 event->hw.interrupts++; 951 event->pending_kill = POLL_IN; 952 unlock: 953 rcu_read_unlock(); 954 return 1; 955 } 956 957 void intel_pmu_drain_pebs_buffer(void) 958 { 959 struct perf_sample_data data; 960 961 static_call(x86_pmu_drain_pebs)(NULL, &data); 962 } 963 964 /* 965 * PEBS 966 */ 967 struct event_constraint intel_core2_pebs_event_constraints[] = { 968 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */ 969 INTEL_FLAGS_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */ 970 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */ 971 INTEL_FLAGS_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */ 972 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */ 973 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ 974 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x01), 975 EVENT_CONSTRAINT_END 976 }; 977 978 struct event_constraint intel_atom_pebs_event_constraints[] = { 979 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */ 980 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */ 981 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */ 982 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ 983 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x01), 984 /* Allow all events as PEBS with no flags */ 985 INTEL_ALL_EVENT_CONSTRAINT(0, 0x1), 986 EVENT_CONSTRAINT_END 987 }; 988 989 struct event_constraint intel_slm_pebs_event_constraints[] = { 990 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ 991 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x1), 992 /* Allow all events as PEBS with no flags */ 993 INTEL_ALL_EVENT_CONSTRAINT(0, 0x1), 994 EVENT_CONSTRAINT_END 995 }; 996 997 struct event_constraint intel_glm_pebs_event_constraints[] = { 998 /* Allow all events as PEBS with no flags */ 999 INTEL_ALL_EVENT_CONSTRAINT(0, 0x1), 1000 EVENT_CONSTRAINT_END 1001 }; 1002 1003 struct event_constraint intel_grt_pebs_event_constraints[] = { 1004 /* Allow all events as PEBS with no flags */ 1005 INTEL_HYBRID_LAT_CONSTRAINT(0x5d0, 0x3), 1006 INTEL_HYBRID_LAT_CONSTRAINT(0x6d0, 0xf), 1007 EVENT_CONSTRAINT_END 1008 }; 1009 1010 struct event_constraint intel_nehalem_pebs_event_constraints[] = { 1011 INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */ 1012 INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */ 1013 INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */ 1014 INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INST_RETIRED.ANY */ 1015 INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */ 1016 INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */ 1017 INTEL_FLAGS_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */ 1018 INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */ 1019 INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */ 1020 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */ 1021 INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */ 1022 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ 1023 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f), 1024 EVENT_CONSTRAINT_END 1025 }; 1026 1027 struct event_constraint intel_westmere_pebs_event_constraints[] = { 1028 INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */ 1029 INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */ 1030 INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */ 1031 INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INSTR_RETIRED.* */ 1032 INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */ 1033 INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */ 1034 INTEL_FLAGS_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */ 1035 INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */ 1036 INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */ 1037 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */ 1038 INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */ 1039 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ 1040 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f), 1041 EVENT_CONSTRAINT_END 1042 }; 1043 1044 struct event_constraint intel_snb_pebs_event_constraints[] = { 1045 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */ 1046 INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */ 1047 INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */ 1048 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ 1049 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf), 1050 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */ 1051 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ 1052 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */ 1053 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */ 1054 /* Allow all events as PEBS with no flags */ 1055 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), 1056 EVENT_CONSTRAINT_END 1057 }; 1058 1059 struct event_constraint intel_ivb_pebs_event_constraints[] = { 1060 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */ 1061 INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */ 1062 INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */ 1063 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ 1064 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf), 1065 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */ 1066 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2), 1067 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */ 1068 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ 1069 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */ 1070 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */ 1071 /* Allow all events as PEBS with no flags */ 1072 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), 1073 EVENT_CONSTRAINT_END 1074 }; 1075 1076 struct event_constraint intel_hsw_pebs_event_constraints[] = { 1077 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */ 1078 INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */ 1079 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ 1080 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf), 1081 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */ 1082 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2), 1083 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */ 1084 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */ 1085 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */ 1086 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */ 1087 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */ 1088 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */ 1089 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */ 1090 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */ 1091 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ 1092 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */ 1093 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */ 1094 /* Allow all events as PEBS with no flags */ 1095 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), 1096 EVENT_CONSTRAINT_END 1097 }; 1098 1099 struct event_constraint intel_bdw_pebs_event_constraints[] = { 1100 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */ 1101 INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */ 1102 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ 1103 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf), 1104 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */ 1105 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2), 1106 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */ 1107 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */ 1108 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */ 1109 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */ 1110 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */ 1111 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */ 1112 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */ 1113 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */ 1114 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ 1115 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */ 1116 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */ 1117 /* Allow all events as PEBS with no flags */ 1118 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), 1119 EVENT_CONSTRAINT_END 1120 }; 1121 1122 1123 struct event_constraint intel_skl_pebs_event_constraints[] = { 1124 INTEL_FLAGS_UEVENT_CONSTRAINT(0x1c0, 0x2), /* INST_RETIRED.PREC_DIST */ 1125 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */ 1126 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2), 1127 /* INST_RETIRED.TOTAL_CYCLES_PS (inv=1, cmask=16) (cycles:p). */ 1128 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f), 1129 INTEL_PLD_CONSTRAINT(0x1cd, 0xf), /* MEM_TRANS_RETIRED.* */ 1130 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */ 1131 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */ 1132 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */ 1133 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x22d0, 0xf), /* MEM_INST_RETIRED.LOCK_STORES */ 1134 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */ 1135 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */ 1136 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */ 1137 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */ 1138 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_RETIRED.* */ 1139 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_L3_HIT_RETIRED.* */ 1140 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_L3_MISS_RETIRED.* */ 1141 /* Allow all events as PEBS with no flags */ 1142 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), 1143 EVENT_CONSTRAINT_END 1144 }; 1145 1146 struct event_constraint intel_icl_pebs_event_constraints[] = { 1147 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x100000000ULL), /* old INST_RETIRED.PREC_DIST */ 1148 INTEL_FLAGS_UEVENT_CONSTRAINT(0x0100, 0x100000000ULL), /* INST_RETIRED.PREC_DIST */ 1149 INTEL_FLAGS_UEVENT_CONSTRAINT(0x0400, 0x800000000ULL), /* SLOTS */ 1150 1151 INTEL_PLD_CONSTRAINT(0x1cd, 0xff), /* MEM_TRANS_RETIRED.LOAD_LATENCY */ 1152 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */ 1153 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */ 1154 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */ 1155 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */ 1156 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */ 1157 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */ 1158 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */ 1159 1160 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD_RANGE(0xd1, 0xd4, 0xf), /* MEM_LOAD_*_RETIRED.* */ 1161 1162 INTEL_FLAGS_EVENT_CONSTRAINT(0xd0, 0xf), /* MEM_INST_RETIRED.* */ 1163 1164 /* 1165 * Everything else is handled by PMU_FL_PEBS_ALL, because we 1166 * need the full constraints from the main table. 1167 */ 1168 1169 EVENT_CONSTRAINT_END 1170 }; 1171 1172 struct event_constraint intel_glc_pebs_event_constraints[] = { 1173 INTEL_FLAGS_UEVENT_CONSTRAINT(0x100, 0x100000000ULL), /* INST_RETIRED.PREC_DIST */ 1174 INTEL_FLAGS_UEVENT_CONSTRAINT(0x0400, 0x800000000ULL), 1175 1176 INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xfe), 1177 INTEL_PLD_CONSTRAINT(0x1cd, 0xfe), 1178 INTEL_PSD_CONSTRAINT(0x2cd, 0x1), 1179 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */ 1180 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */ 1181 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */ 1182 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */ 1183 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */ 1184 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */ 1185 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */ 1186 1187 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD_RANGE(0xd1, 0xd4, 0xf), 1188 1189 INTEL_FLAGS_EVENT_CONSTRAINT(0xd0, 0xf), 1190 1191 /* 1192 * Everything else is handled by PMU_FL_PEBS_ALL, because we 1193 * need the full constraints from the main table. 1194 */ 1195 1196 EVENT_CONSTRAINT_END 1197 }; 1198 1199 struct event_constraint intel_lnc_pebs_event_constraints[] = { 1200 INTEL_FLAGS_UEVENT_CONSTRAINT(0x100, 0x100000000ULL), /* INST_RETIRED.PREC_DIST */ 1201 INTEL_FLAGS_UEVENT_CONSTRAINT(0x0400, 0x800000000ULL), 1202 1203 INTEL_HYBRID_LDLAT_CONSTRAINT(0x1cd, 0x3fc), 1204 INTEL_HYBRID_STLAT_CONSTRAINT(0x2cd, 0x3), 1205 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */ 1206 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */ 1207 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */ 1208 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */ 1209 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */ 1210 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */ 1211 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */ 1212 1213 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD_RANGE(0xd1, 0xd4, 0xf), 1214 1215 INTEL_FLAGS_EVENT_CONSTRAINT(0xd0, 0xf), 1216 1217 /* 1218 * Everything else is handled by PMU_FL_PEBS_ALL, because we 1219 * need the full constraints from the main table. 1220 */ 1221 1222 EVENT_CONSTRAINT_END 1223 }; 1224 1225 struct event_constraint *intel_pebs_constraints(struct perf_event *event) 1226 { 1227 struct event_constraint *pebs_constraints = hybrid(event->pmu, pebs_constraints); 1228 struct event_constraint *c; 1229 1230 if (!event->attr.precise_ip) 1231 return NULL; 1232 1233 if (pebs_constraints) { 1234 for_each_event_constraint(c, pebs_constraints) { 1235 if (constraint_match(c, event->hw.config)) { 1236 event->hw.flags |= c->flags; 1237 return c; 1238 } 1239 } 1240 } 1241 1242 /* 1243 * Extended PEBS support 1244 * Makes the PEBS code search the normal constraints. 1245 */ 1246 if (x86_pmu.flags & PMU_FL_PEBS_ALL) 1247 return NULL; 1248 1249 return &emptyconstraint; 1250 } 1251 1252 /* 1253 * We need the sched_task callback even for per-cpu events when we use 1254 * the large interrupt threshold, such that we can provide PID and TID 1255 * to PEBS samples. 1256 */ 1257 static inline bool pebs_needs_sched_cb(struct cpu_hw_events *cpuc) 1258 { 1259 if (cpuc->n_pebs == cpuc->n_pebs_via_pt) 1260 return false; 1261 1262 return cpuc->n_pebs && (cpuc->n_pebs == cpuc->n_large_pebs); 1263 } 1264 1265 void intel_pmu_pebs_sched_task(struct perf_event_pmu_context *pmu_ctx, bool sched_in) 1266 { 1267 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1268 1269 if (!sched_in && pebs_needs_sched_cb(cpuc)) 1270 intel_pmu_drain_pebs_buffer(); 1271 } 1272 1273 static inline void pebs_update_threshold(struct cpu_hw_events *cpuc) 1274 { 1275 struct debug_store *ds = cpuc->ds; 1276 int max_pebs_events = intel_pmu_max_num_pebs(cpuc->pmu); 1277 u64 threshold; 1278 int reserved; 1279 1280 if (cpuc->n_pebs_via_pt) 1281 return; 1282 1283 if (x86_pmu.flags & PMU_FL_PEBS_ALL) 1284 reserved = max_pebs_events + x86_pmu_max_num_counters_fixed(cpuc->pmu); 1285 else 1286 reserved = max_pebs_events; 1287 1288 if (cpuc->n_pebs == cpuc->n_large_pebs) { 1289 threshold = ds->pebs_absolute_maximum - 1290 reserved * cpuc->pebs_record_size; 1291 } else { 1292 threshold = ds->pebs_buffer_base + cpuc->pebs_record_size; 1293 } 1294 1295 ds->pebs_interrupt_threshold = threshold; 1296 } 1297 1298 #define PEBS_DATACFG_CNTRS(x) \ 1299 ((x >> PEBS_DATACFG_CNTR_SHIFT) & PEBS_DATACFG_CNTR_MASK) 1300 1301 #define PEBS_DATACFG_CNTR_BIT(x) \ 1302 (((1ULL << x) & PEBS_DATACFG_CNTR_MASK) << PEBS_DATACFG_CNTR_SHIFT) 1303 1304 #define PEBS_DATACFG_FIX(x) \ 1305 ((x >> PEBS_DATACFG_FIX_SHIFT) & PEBS_DATACFG_FIX_MASK) 1306 1307 #define PEBS_DATACFG_FIX_BIT(x) \ 1308 (((1ULL << (x)) & PEBS_DATACFG_FIX_MASK) \ 1309 << PEBS_DATACFG_FIX_SHIFT) 1310 1311 static void adaptive_pebs_record_size_update(void) 1312 { 1313 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1314 u64 pebs_data_cfg = cpuc->pebs_data_cfg; 1315 int sz = sizeof(struct pebs_basic); 1316 1317 if (pebs_data_cfg & PEBS_DATACFG_MEMINFO) 1318 sz += sizeof(struct pebs_meminfo); 1319 if (pebs_data_cfg & PEBS_DATACFG_GP) 1320 sz += sizeof(struct pebs_gprs); 1321 if (pebs_data_cfg & PEBS_DATACFG_XMMS) 1322 sz += sizeof(struct pebs_xmm); 1323 if (pebs_data_cfg & PEBS_DATACFG_LBRS) 1324 sz += x86_pmu.lbr_nr * sizeof(struct lbr_entry); 1325 if (pebs_data_cfg & (PEBS_DATACFG_METRICS | PEBS_DATACFG_CNTR)) { 1326 sz += sizeof(struct pebs_cntr_header); 1327 1328 /* Metrics base and Metrics Data */ 1329 if (pebs_data_cfg & PEBS_DATACFG_METRICS) 1330 sz += 2 * sizeof(u64); 1331 1332 if (pebs_data_cfg & PEBS_DATACFG_CNTR) { 1333 sz += (hweight64(PEBS_DATACFG_CNTRS(pebs_data_cfg)) + 1334 hweight64(PEBS_DATACFG_FIX(pebs_data_cfg))) * 1335 sizeof(u64); 1336 } 1337 } 1338 1339 cpuc->pebs_record_size = sz; 1340 } 1341 1342 static void __intel_pmu_pebs_update_cfg(struct perf_event *event, 1343 int idx, u64 *pebs_data_cfg) 1344 { 1345 if (is_metric_event(event)) { 1346 *pebs_data_cfg |= PEBS_DATACFG_METRICS; 1347 return; 1348 } 1349 1350 *pebs_data_cfg |= PEBS_DATACFG_CNTR; 1351 1352 if (idx >= INTEL_PMC_IDX_FIXED) 1353 *pebs_data_cfg |= PEBS_DATACFG_FIX_BIT(idx - INTEL_PMC_IDX_FIXED); 1354 else 1355 *pebs_data_cfg |= PEBS_DATACFG_CNTR_BIT(idx); 1356 } 1357 1358 1359 static void intel_pmu_late_setup(void) 1360 { 1361 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1362 struct perf_event *event; 1363 u64 pebs_data_cfg = 0; 1364 int i; 1365 1366 for (i = 0; i < cpuc->n_events; i++) { 1367 event = cpuc->event_list[i]; 1368 if (!is_pebs_counter_event_group(event)) 1369 continue; 1370 __intel_pmu_pebs_update_cfg(event, cpuc->assign[i], &pebs_data_cfg); 1371 } 1372 1373 if (pebs_data_cfg & ~cpuc->pebs_data_cfg) 1374 cpuc->pebs_data_cfg |= pebs_data_cfg | PEBS_UPDATE_DS_SW; 1375 } 1376 1377 #define PERF_PEBS_MEMINFO_TYPE (PERF_SAMPLE_ADDR | PERF_SAMPLE_DATA_SRC | \ 1378 PERF_SAMPLE_PHYS_ADDR | \ 1379 PERF_SAMPLE_WEIGHT_TYPE | \ 1380 PERF_SAMPLE_TRANSACTION | \ 1381 PERF_SAMPLE_DATA_PAGE_SIZE) 1382 1383 static u64 pebs_update_adaptive_cfg(struct perf_event *event) 1384 { 1385 struct perf_event_attr *attr = &event->attr; 1386 u64 sample_type = attr->sample_type; 1387 u64 pebs_data_cfg = 0; 1388 bool gprs, tsx_weight; 1389 1390 if (!(sample_type & ~(PERF_SAMPLE_IP|PERF_SAMPLE_TIME)) && 1391 attr->precise_ip > 1) 1392 return pebs_data_cfg; 1393 1394 if (sample_type & PERF_PEBS_MEMINFO_TYPE) 1395 pebs_data_cfg |= PEBS_DATACFG_MEMINFO; 1396 1397 /* 1398 * We need GPRs when: 1399 * + user requested them 1400 * + precise_ip < 2 for the non event IP 1401 * + For RTM TSX weight we need GPRs for the abort code. 1402 */ 1403 gprs = ((sample_type & PERF_SAMPLE_REGS_INTR) && 1404 (attr->sample_regs_intr & PEBS_GP_REGS)) || 1405 ((sample_type & PERF_SAMPLE_REGS_USER) && 1406 (attr->sample_regs_user & PEBS_GP_REGS)); 1407 1408 tsx_weight = (sample_type & PERF_SAMPLE_WEIGHT_TYPE) && 1409 ((attr->config & INTEL_ARCH_EVENT_MASK) == 1410 x86_pmu.rtm_abort_event); 1411 1412 if (gprs || (attr->precise_ip < 2) || tsx_weight) 1413 pebs_data_cfg |= PEBS_DATACFG_GP; 1414 1415 if ((sample_type & PERF_SAMPLE_REGS_INTR) && 1416 (attr->sample_regs_intr & PERF_REG_EXTENDED_MASK)) 1417 pebs_data_cfg |= PEBS_DATACFG_XMMS; 1418 1419 if (sample_type & PERF_SAMPLE_BRANCH_STACK) { 1420 /* 1421 * For now always log all LBRs. Could configure this 1422 * later. 1423 */ 1424 pebs_data_cfg |= PEBS_DATACFG_LBRS | 1425 ((x86_pmu.lbr_nr-1) << PEBS_DATACFG_LBR_SHIFT); 1426 } 1427 1428 return pebs_data_cfg; 1429 } 1430 1431 static void 1432 pebs_update_state(bool needed_cb, struct cpu_hw_events *cpuc, 1433 struct perf_event *event, bool add) 1434 { 1435 struct pmu *pmu = event->pmu; 1436 1437 /* 1438 * Make sure we get updated with the first PEBS event. 1439 * During removal, ->pebs_data_cfg is still valid for 1440 * the last PEBS event. Don't clear it. 1441 */ 1442 if ((cpuc->n_pebs == 1) && add) 1443 cpuc->pebs_data_cfg = PEBS_UPDATE_DS_SW; 1444 1445 if (needed_cb != pebs_needs_sched_cb(cpuc)) { 1446 if (!needed_cb) 1447 perf_sched_cb_inc(pmu); 1448 else 1449 perf_sched_cb_dec(pmu); 1450 1451 cpuc->pebs_data_cfg |= PEBS_UPDATE_DS_SW; 1452 } 1453 1454 /* 1455 * The PEBS record doesn't shrink on pmu::del(). Doing so would require 1456 * iterating all remaining PEBS events to reconstruct the config. 1457 */ 1458 if (x86_pmu.intel_cap.pebs_baseline && add) { 1459 u64 pebs_data_cfg; 1460 1461 pebs_data_cfg = pebs_update_adaptive_cfg(event); 1462 /* 1463 * Be sure to update the thresholds when we change the record. 1464 */ 1465 if (pebs_data_cfg & ~cpuc->pebs_data_cfg) 1466 cpuc->pebs_data_cfg |= pebs_data_cfg | PEBS_UPDATE_DS_SW; 1467 } 1468 } 1469 1470 void intel_pmu_pebs_add(struct perf_event *event) 1471 { 1472 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1473 struct hw_perf_event *hwc = &event->hw; 1474 bool needed_cb = pebs_needs_sched_cb(cpuc); 1475 1476 cpuc->n_pebs++; 1477 if (hwc->flags & PERF_X86_EVENT_LARGE_PEBS) 1478 cpuc->n_large_pebs++; 1479 if (hwc->flags & PERF_X86_EVENT_PEBS_VIA_PT) 1480 cpuc->n_pebs_via_pt++; 1481 1482 pebs_update_state(needed_cb, cpuc, event, true); 1483 } 1484 1485 static void intel_pmu_pebs_via_pt_disable(struct perf_event *event) 1486 { 1487 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1488 1489 if (!is_pebs_pt(event)) 1490 return; 1491 1492 if (!(cpuc->pebs_enabled & ~PEBS_VIA_PT_MASK)) 1493 cpuc->pebs_enabled &= ~PEBS_VIA_PT_MASK; 1494 } 1495 1496 static void intel_pmu_pebs_via_pt_enable(struct perf_event *event) 1497 { 1498 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1499 struct hw_perf_event *hwc = &event->hw; 1500 struct debug_store *ds = cpuc->ds; 1501 u64 value = ds->pebs_event_reset[hwc->idx]; 1502 u32 base = MSR_RELOAD_PMC0; 1503 unsigned int idx = hwc->idx; 1504 1505 if (!is_pebs_pt(event)) 1506 return; 1507 1508 if (!(event->hw.flags & PERF_X86_EVENT_LARGE_PEBS)) 1509 cpuc->pebs_enabled |= PEBS_PMI_AFTER_EACH_RECORD; 1510 1511 cpuc->pebs_enabled |= PEBS_OUTPUT_PT; 1512 1513 if (hwc->idx >= INTEL_PMC_IDX_FIXED) { 1514 base = MSR_RELOAD_FIXED_CTR0; 1515 idx = hwc->idx - INTEL_PMC_IDX_FIXED; 1516 if (x86_pmu.intel_cap.pebs_format < 5) 1517 value = ds->pebs_event_reset[MAX_PEBS_EVENTS_FMT4 + idx]; 1518 else 1519 value = ds->pebs_event_reset[MAX_PEBS_EVENTS + idx]; 1520 } 1521 wrmsrq(base + idx, value); 1522 } 1523 1524 static inline void intel_pmu_drain_large_pebs(struct cpu_hw_events *cpuc) 1525 { 1526 if (cpuc->n_pebs == cpuc->n_large_pebs && 1527 cpuc->n_pebs != cpuc->n_pebs_via_pt) 1528 intel_pmu_drain_pebs_buffer(); 1529 } 1530 1531 void intel_pmu_pebs_enable(struct perf_event *event) 1532 { 1533 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1534 u64 pebs_data_cfg = cpuc->pebs_data_cfg & ~PEBS_UPDATE_DS_SW; 1535 struct hw_perf_event *hwc = &event->hw; 1536 struct debug_store *ds = cpuc->ds; 1537 unsigned int idx = hwc->idx; 1538 1539 hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT; 1540 1541 cpuc->pebs_enabled |= 1ULL << hwc->idx; 1542 1543 if ((event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT) && (x86_pmu.version < 5)) 1544 cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32); 1545 else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST) 1546 cpuc->pebs_enabled |= 1ULL << 63; 1547 1548 if (x86_pmu.intel_cap.pebs_baseline) { 1549 hwc->config |= ICL_EVENTSEL_ADAPTIVE; 1550 if (pebs_data_cfg != cpuc->active_pebs_data_cfg) { 1551 /* 1552 * drain_pebs() assumes uniform record size; 1553 * hence we need to drain when changing said 1554 * size. 1555 */ 1556 intel_pmu_drain_pebs_buffer(); 1557 adaptive_pebs_record_size_update(); 1558 wrmsrq(MSR_PEBS_DATA_CFG, pebs_data_cfg); 1559 cpuc->active_pebs_data_cfg = pebs_data_cfg; 1560 } 1561 } 1562 if (cpuc->pebs_data_cfg & PEBS_UPDATE_DS_SW) { 1563 cpuc->pebs_data_cfg = pebs_data_cfg; 1564 pebs_update_threshold(cpuc); 1565 } 1566 1567 if (idx >= INTEL_PMC_IDX_FIXED) { 1568 if (x86_pmu.intel_cap.pebs_format < 5) 1569 idx = MAX_PEBS_EVENTS_FMT4 + (idx - INTEL_PMC_IDX_FIXED); 1570 else 1571 idx = MAX_PEBS_EVENTS + (idx - INTEL_PMC_IDX_FIXED); 1572 } 1573 1574 /* 1575 * Use auto-reload if possible to save a MSR write in the PMI. 1576 * This must be done in pmu::start(), because PERF_EVENT_IOC_PERIOD. 1577 */ 1578 if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) { 1579 ds->pebs_event_reset[idx] = 1580 (u64)(-hwc->sample_period) & x86_pmu.cntval_mask; 1581 } else { 1582 ds->pebs_event_reset[idx] = 0; 1583 } 1584 1585 intel_pmu_pebs_via_pt_enable(event); 1586 } 1587 1588 void intel_pmu_pebs_del(struct perf_event *event) 1589 { 1590 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1591 struct hw_perf_event *hwc = &event->hw; 1592 bool needed_cb = pebs_needs_sched_cb(cpuc); 1593 1594 cpuc->n_pebs--; 1595 if (hwc->flags & PERF_X86_EVENT_LARGE_PEBS) 1596 cpuc->n_large_pebs--; 1597 if (hwc->flags & PERF_X86_EVENT_PEBS_VIA_PT) 1598 cpuc->n_pebs_via_pt--; 1599 1600 pebs_update_state(needed_cb, cpuc, event, false); 1601 } 1602 1603 void intel_pmu_pebs_disable(struct perf_event *event) 1604 { 1605 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1606 struct hw_perf_event *hwc = &event->hw; 1607 1608 intel_pmu_drain_large_pebs(cpuc); 1609 1610 cpuc->pebs_enabled &= ~(1ULL << hwc->idx); 1611 1612 if ((event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT) && 1613 (x86_pmu.version < 5)) 1614 cpuc->pebs_enabled &= ~(1ULL << (hwc->idx + 32)); 1615 else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST) 1616 cpuc->pebs_enabled &= ~(1ULL << 63); 1617 1618 intel_pmu_pebs_via_pt_disable(event); 1619 1620 if (cpuc->enabled) 1621 wrmsrq(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled); 1622 1623 hwc->config |= ARCH_PERFMON_EVENTSEL_INT; 1624 } 1625 1626 void intel_pmu_pebs_enable_all(void) 1627 { 1628 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1629 1630 if (cpuc->pebs_enabled) 1631 wrmsrq(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled); 1632 } 1633 1634 void intel_pmu_pebs_disable_all(void) 1635 { 1636 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1637 1638 if (cpuc->pebs_enabled) 1639 __intel_pmu_pebs_disable_all(); 1640 } 1641 1642 static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs) 1643 { 1644 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1645 unsigned long from = cpuc->lbr_entries[0].from; 1646 unsigned long old_to, to = cpuc->lbr_entries[0].to; 1647 unsigned long ip = regs->ip; 1648 int is_64bit = 0; 1649 void *kaddr; 1650 int size; 1651 1652 /* 1653 * We don't need to fixup if the PEBS assist is fault like 1654 */ 1655 if (!x86_pmu.intel_cap.pebs_trap) 1656 return 1; 1657 1658 /* 1659 * No LBR entry, no basic block, no rewinding 1660 */ 1661 if (!cpuc->lbr_stack.nr || !from || !to) 1662 return 0; 1663 1664 /* 1665 * Basic blocks should never cross user/kernel boundaries 1666 */ 1667 if (kernel_ip(ip) != kernel_ip(to)) 1668 return 0; 1669 1670 /* 1671 * unsigned math, either ip is before the start (impossible) or 1672 * the basic block is larger than 1 page (sanity) 1673 */ 1674 if ((ip - to) > PEBS_FIXUP_SIZE) 1675 return 0; 1676 1677 /* 1678 * We sampled a branch insn, rewind using the LBR stack 1679 */ 1680 if (ip == to) { 1681 set_linear_ip(regs, from); 1682 return 1; 1683 } 1684 1685 size = ip - to; 1686 if (!kernel_ip(ip)) { 1687 int bytes; 1688 u8 *buf = this_cpu_read(insn_buffer); 1689 1690 /* 'size' must fit our buffer, see above */ 1691 bytes = copy_from_user_nmi(buf, (void __user *)to, size); 1692 if (bytes != 0) 1693 return 0; 1694 1695 kaddr = buf; 1696 } else { 1697 kaddr = (void *)to; 1698 } 1699 1700 do { 1701 struct insn insn; 1702 1703 old_to = to; 1704 1705 #ifdef CONFIG_X86_64 1706 is_64bit = kernel_ip(to) || any_64bit_mode(regs); 1707 #endif 1708 insn_init(&insn, kaddr, size, is_64bit); 1709 1710 /* 1711 * Make sure there was not a problem decoding the instruction. 1712 * This is doubly important because we have an infinite loop if 1713 * insn.length=0. 1714 */ 1715 if (insn_get_length(&insn)) 1716 break; 1717 1718 to += insn.length; 1719 kaddr += insn.length; 1720 size -= insn.length; 1721 } while (to < ip); 1722 1723 if (to == ip) { 1724 set_linear_ip(regs, old_to); 1725 return 1; 1726 } 1727 1728 /* 1729 * Even though we decoded the basic block, the instruction stream 1730 * never matched the given IP, either the TO or the IP got corrupted. 1731 */ 1732 return 0; 1733 } 1734 1735 static inline u64 intel_get_tsx_weight(u64 tsx_tuning) 1736 { 1737 if (tsx_tuning) { 1738 union hsw_tsx_tuning tsx = { .value = tsx_tuning }; 1739 return tsx.cycles_last_block; 1740 } 1741 return 0; 1742 } 1743 1744 static inline u64 intel_get_tsx_transaction(u64 tsx_tuning, u64 ax) 1745 { 1746 u64 txn = (tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32; 1747 1748 /* For RTM XABORTs also log the abort code from AX */ 1749 if ((txn & PERF_TXN_TRANSACTION) && (ax & 1)) 1750 txn |= ((ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT; 1751 return txn; 1752 } 1753 1754 static inline u64 get_pebs_status(void *n) 1755 { 1756 if (x86_pmu.intel_cap.pebs_format < 4) 1757 return ((struct pebs_record_nhm *)n)->status; 1758 return ((struct pebs_basic *)n)->applicable_counters; 1759 } 1760 1761 #define PERF_X86_EVENT_PEBS_HSW_PREC \ 1762 (PERF_X86_EVENT_PEBS_ST_HSW | \ 1763 PERF_X86_EVENT_PEBS_LD_HSW | \ 1764 PERF_X86_EVENT_PEBS_NA_HSW) 1765 1766 static u64 get_data_src(struct perf_event *event, u64 aux) 1767 { 1768 u64 val = PERF_MEM_NA; 1769 int fl = event->hw.flags; 1770 bool fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC); 1771 1772 if (fl & PERF_X86_EVENT_PEBS_LDLAT) 1773 val = load_latency_data(event, aux); 1774 else if (fl & PERF_X86_EVENT_PEBS_STLAT) 1775 val = store_latency_data(event, aux); 1776 else if (fl & PERF_X86_EVENT_PEBS_LAT_HYBRID) 1777 val = x86_pmu.pebs_latency_data(event, aux); 1778 else if (fst && (fl & PERF_X86_EVENT_PEBS_HSW_PREC)) 1779 val = precise_datala_hsw(event, aux); 1780 else if (fst) 1781 val = precise_store_data(aux); 1782 return val; 1783 } 1784 1785 static void setup_pebs_time(struct perf_event *event, 1786 struct perf_sample_data *data, 1787 u64 tsc) 1788 { 1789 /* Converting to a user-defined clock is not supported yet. */ 1790 if (event->attr.use_clockid != 0) 1791 return; 1792 1793 /* 1794 * Doesn't support the conversion when the TSC is unstable. 1795 * The TSC unstable case is a corner case and very unlikely to 1796 * happen. If it happens, the TSC in a PEBS record will be 1797 * dropped and fall back to perf_event_clock(). 1798 */ 1799 if (!using_native_sched_clock() || !sched_clock_stable()) 1800 return; 1801 1802 data->time = native_sched_clock_from_tsc(tsc) + __sched_clock_offset; 1803 data->sample_flags |= PERF_SAMPLE_TIME; 1804 } 1805 1806 #define PERF_SAMPLE_ADDR_TYPE (PERF_SAMPLE_ADDR | \ 1807 PERF_SAMPLE_PHYS_ADDR | \ 1808 PERF_SAMPLE_DATA_PAGE_SIZE) 1809 1810 static void setup_pebs_fixed_sample_data(struct perf_event *event, 1811 struct pt_regs *iregs, void *__pebs, 1812 struct perf_sample_data *data, 1813 struct pt_regs *regs) 1814 { 1815 /* 1816 * We cast to the biggest pebs_record but are careful not to 1817 * unconditionally access the 'extra' entries. 1818 */ 1819 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1820 struct pebs_record_skl *pebs = __pebs; 1821 u64 sample_type; 1822 int fll; 1823 1824 if (pebs == NULL) 1825 return; 1826 1827 sample_type = event->attr.sample_type; 1828 fll = event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT; 1829 1830 perf_sample_data_init(data, 0, event->hw.last_period); 1831 1832 data->period = event->hw.last_period; 1833 1834 /* 1835 * Use latency for weight (only avail with PEBS-LL) 1836 */ 1837 if (fll && (sample_type & PERF_SAMPLE_WEIGHT_TYPE)) { 1838 data->weight.full = pebs->lat; 1839 data->sample_flags |= PERF_SAMPLE_WEIGHT_TYPE; 1840 } 1841 1842 /* 1843 * data.data_src encodes the data source 1844 */ 1845 if (sample_type & PERF_SAMPLE_DATA_SRC) { 1846 data->data_src.val = get_data_src(event, pebs->dse); 1847 data->sample_flags |= PERF_SAMPLE_DATA_SRC; 1848 } 1849 1850 /* 1851 * We must however always use iregs for the unwinder to stay sane; the 1852 * record BP,SP,IP can point into thin air when the record is from a 1853 * previous PMI context or an (I)RET happened between the record and 1854 * PMI. 1855 */ 1856 perf_sample_save_callchain(data, event, iregs); 1857 1858 /* 1859 * We use the interrupt regs as a base because the PEBS record does not 1860 * contain a full regs set, specifically it seems to lack segment 1861 * descriptors, which get used by things like user_mode(). 1862 * 1863 * In the simple case fix up only the IP for PERF_SAMPLE_IP. 1864 */ 1865 *regs = *iregs; 1866 1867 /* 1868 * Initialize regs_>flags from PEBS, 1869 * Clear exact bit (which uses x86 EFLAGS Reserved bit 3), 1870 * i.e., do not rely on it being zero: 1871 */ 1872 regs->flags = pebs->flags & ~PERF_EFLAGS_EXACT; 1873 1874 if (sample_type & PERF_SAMPLE_REGS_INTR) { 1875 regs->ax = pebs->ax; 1876 regs->bx = pebs->bx; 1877 regs->cx = pebs->cx; 1878 regs->dx = pebs->dx; 1879 regs->si = pebs->si; 1880 regs->di = pebs->di; 1881 1882 regs->bp = pebs->bp; 1883 regs->sp = pebs->sp; 1884 1885 #ifndef CONFIG_X86_32 1886 regs->r8 = pebs->r8; 1887 regs->r9 = pebs->r9; 1888 regs->r10 = pebs->r10; 1889 regs->r11 = pebs->r11; 1890 regs->r12 = pebs->r12; 1891 regs->r13 = pebs->r13; 1892 regs->r14 = pebs->r14; 1893 regs->r15 = pebs->r15; 1894 #endif 1895 } 1896 1897 if (event->attr.precise_ip > 1) { 1898 /* 1899 * Haswell and later processors have an 'eventing IP' 1900 * (real IP) which fixes the off-by-1 skid in hardware. 1901 * Use it when precise_ip >= 2 : 1902 */ 1903 if (x86_pmu.intel_cap.pebs_format >= 2) { 1904 set_linear_ip(regs, pebs->real_ip); 1905 regs->flags |= PERF_EFLAGS_EXACT; 1906 } else { 1907 /* Otherwise, use PEBS off-by-1 IP: */ 1908 set_linear_ip(regs, pebs->ip); 1909 1910 /* 1911 * With precise_ip >= 2, try to fix up the off-by-1 IP 1912 * using the LBR. If successful, the fixup function 1913 * corrects regs->ip and calls set_linear_ip() on regs: 1914 */ 1915 if (intel_pmu_pebs_fixup_ip(regs)) 1916 regs->flags |= PERF_EFLAGS_EXACT; 1917 } 1918 } else { 1919 /* 1920 * When precise_ip == 1, return the PEBS off-by-1 IP, 1921 * no fixup attempted: 1922 */ 1923 set_linear_ip(regs, pebs->ip); 1924 } 1925 1926 1927 if ((sample_type & PERF_SAMPLE_ADDR_TYPE) && 1928 x86_pmu.intel_cap.pebs_format >= 1) { 1929 data->addr = pebs->dla; 1930 data->sample_flags |= PERF_SAMPLE_ADDR; 1931 } 1932 1933 if (x86_pmu.intel_cap.pebs_format >= 2) { 1934 /* Only set the TSX weight when no memory weight. */ 1935 if ((sample_type & PERF_SAMPLE_WEIGHT_TYPE) && !fll) { 1936 data->weight.full = intel_get_tsx_weight(pebs->tsx_tuning); 1937 data->sample_flags |= PERF_SAMPLE_WEIGHT_TYPE; 1938 } 1939 if (sample_type & PERF_SAMPLE_TRANSACTION) { 1940 data->txn = intel_get_tsx_transaction(pebs->tsx_tuning, 1941 pebs->ax); 1942 data->sample_flags |= PERF_SAMPLE_TRANSACTION; 1943 } 1944 } 1945 1946 /* 1947 * v3 supplies an accurate time stamp, so we use that 1948 * for the time stamp. 1949 * 1950 * We can only do this for the default trace clock. 1951 */ 1952 if (x86_pmu.intel_cap.pebs_format >= 3) 1953 setup_pebs_time(event, data, pebs->tsc); 1954 1955 perf_sample_save_brstack(data, event, &cpuc->lbr_stack, NULL); 1956 } 1957 1958 static void adaptive_pebs_save_regs(struct pt_regs *regs, 1959 struct pebs_gprs *gprs) 1960 { 1961 regs->ax = gprs->ax; 1962 regs->bx = gprs->bx; 1963 regs->cx = gprs->cx; 1964 regs->dx = gprs->dx; 1965 regs->si = gprs->si; 1966 regs->di = gprs->di; 1967 regs->bp = gprs->bp; 1968 regs->sp = gprs->sp; 1969 #ifndef CONFIG_X86_32 1970 regs->r8 = gprs->r8; 1971 regs->r9 = gprs->r9; 1972 regs->r10 = gprs->r10; 1973 regs->r11 = gprs->r11; 1974 regs->r12 = gprs->r12; 1975 regs->r13 = gprs->r13; 1976 regs->r14 = gprs->r14; 1977 regs->r15 = gprs->r15; 1978 #endif 1979 } 1980 1981 static void intel_perf_event_update_pmc(struct perf_event *event, u64 pmc) 1982 { 1983 int shift = 64 - x86_pmu.cntval_bits; 1984 struct hw_perf_event *hwc; 1985 u64 delta, prev_pmc; 1986 1987 /* 1988 * A recorded counter may not have an assigned event in the 1989 * following cases. The value should be dropped. 1990 * - An event is deleted. There is still an active PEBS event. 1991 * The PEBS record doesn't shrink on pmu::del(). 1992 * If the counter of the deleted event once occurred in a PEBS 1993 * record, PEBS still records the counter until the counter is 1994 * reassigned. 1995 * - An event is stopped for some reason, e.g., throttled. 1996 * During this period, another event is added and takes the 1997 * counter of the stopped event. The stopped event is assigned 1998 * to another new and uninitialized counter, since the 1999 * x86_pmu_start(RELOAD) is not invoked for a stopped event. 2000 * The PEBS__DATA_CFG is updated regardless of the event state. 2001 * The uninitialized counter can be recorded in a PEBS record. 2002 * But the cpuc->events[uninitialized_counter] is always NULL, 2003 * because the event is stopped. The uninitialized value is 2004 * safely dropped. 2005 */ 2006 if (!event) 2007 return; 2008 2009 hwc = &event->hw; 2010 prev_pmc = local64_read(&hwc->prev_count); 2011 2012 /* Only update the count when the PMU is disabled */ 2013 WARN_ON(this_cpu_read(cpu_hw_events.enabled)); 2014 local64_set(&hwc->prev_count, pmc); 2015 2016 delta = (pmc << shift) - (prev_pmc << shift); 2017 delta >>= shift; 2018 2019 local64_add(delta, &event->count); 2020 local64_sub(delta, &hwc->period_left); 2021 } 2022 2023 static inline void __setup_pebs_counter_group(struct cpu_hw_events *cpuc, 2024 struct perf_event *event, 2025 struct pebs_cntr_header *cntr, 2026 void *next_record) 2027 { 2028 int bit; 2029 2030 for_each_set_bit(bit, (unsigned long *)&cntr->cntr, INTEL_PMC_MAX_GENERIC) { 2031 intel_perf_event_update_pmc(cpuc->events[bit], *(u64 *)next_record); 2032 next_record += sizeof(u64); 2033 } 2034 2035 for_each_set_bit(bit, (unsigned long *)&cntr->fixed, INTEL_PMC_MAX_FIXED) { 2036 /* The slots event will be handled with perf_metric later */ 2037 if ((cntr->metrics == INTEL_CNTR_METRICS) && 2038 (bit + INTEL_PMC_IDX_FIXED == INTEL_PMC_IDX_FIXED_SLOTS)) { 2039 next_record += sizeof(u64); 2040 continue; 2041 } 2042 intel_perf_event_update_pmc(cpuc->events[bit + INTEL_PMC_IDX_FIXED], 2043 *(u64 *)next_record); 2044 next_record += sizeof(u64); 2045 } 2046 2047 /* HW will reload the value right after the overflow. */ 2048 if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD) 2049 local64_set(&event->hw.prev_count, (u64)-event->hw.sample_period); 2050 2051 if (cntr->metrics == INTEL_CNTR_METRICS) { 2052 static_call(intel_pmu_update_topdown_event) 2053 (cpuc->events[INTEL_PMC_IDX_FIXED_SLOTS], 2054 (u64 *)next_record); 2055 next_record += 2 * sizeof(u64); 2056 } 2057 } 2058 2059 #define PEBS_LATENCY_MASK 0xffff 2060 2061 /* 2062 * With adaptive PEBS the layout depends on what fields are configured. 2063 */ 2064 static void setup_pebs_adaptive_sample_data(struct perf_event *event, 2065 struct pt_regs *iregs, void *__pebs, 2066 struct perf_sample_data *data, 2067 struct pt_regs *regs) 2068 { 2069 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 2070 struct pebs_basic *basic = __pebs; 2071 void *next_record = basic + 1; 2072 u64 sample_type, format_group; 2073 struct pebs_meminfo *meminfo = NULL; 2074 struct pebs_gprs *gprs = NULL; 2075 struct x86_perf_regs *perf_regs; 2076 2077 if (basic == NULL) 2078 return; 2079 2080 perf_regs = container_of(regs, struct x86_perf_regs, regs); 2081 perf_regs->xmm_regs = NULL; 2082 2083 sample_type = event->attr.sample_type; 2084 format_group = basic->format_group; 2085 perf_sample_data_init(data, 0, event->hw.last_period); 2086 data->period = event->hw.last_period; 2087 2088 setup_pebs_time(event, data, basic->tsc); 2089 2090 /* 2091 * We must however always use iregs for the unwinder to stay sane; the 2092 * record BP,SP,IP can point into thin air when the record is from a 2093 * previous PMI context or an (I)RET happened between the record and 2094 * PMI. 2095 */ 2096 perf_sample_save_callchain(data, event, iregs); 2097 2098 *regs = *iregs; 2099 /* The ip in basic is EventingIP */ 2100 set_linear_ip(regs, basic->ip); 2101 regs->flags = PERF_EFLAGS_EXACT; 2102 2103 if (sample_type & PERF_SAMPLE_WEIGHT_STRUCT) { 2104 if (x86_pmu.flags & PMU_FL_RETIRE_LATENCY) 2105 data->weight.var3_w = basic->retire_latency; 2106 else 2107 data->weight.var3_w = 0; 2108 } 2109 2110 /* 2111 * The record for MEMINFO is in front of GP 2112 * But PERF_SAMPLE_TRANSACTION needs gprs->ax. 2113 * Save the pointer here but process later. 2114 */ 2115 if (format_group & PEBS_DATACFG_MEMINFO) { 2116 meminfo = next_record; 2117 next_record = meminfo + 1; 2118 } 2119 2120 if (format_group & PEBS_DATACFG_GP) { 2121 gprs = next_record; 2122 next_record = gprs + 1; 2123 2124 if (event->attr.precise_ip < 2) { 2125 set_linear_ip(regs, gprs->ip); 2126 regs->flags &= ~PERF_EFLAGS_EXACT; 2127 } 2128 2129 if (sample_type & (PERF_SAMPLE_REGS_INTR | PERF_SAMPLE_REGS_USER)) 2130 adaptive_pebs_save_regs(regs, gprs); 2131 } 2132 2133 if (format_group & PEBS_DATACFG_MEMINFO) { 2134 if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) { 2135 u64 latency = x86_pmu.flags & PMU_FL_INSTR_LATENCY ? 2136 meminfo->cache_latency : meminfo->mem_latency; 2137 2138 if (x86_pmu.flags & PMU_FL_INSTR_LATENCY) 2139 data->weight.var2_w = meminfo->instr_latency; 2140 2141 /* 2142 * Although meminfo::latency is defined as a u64, 2143 * only the lower 32 bits include the valid data 2144 * in practice on Ice Lake and earlier platforms. 2145 */ 2146 if (sample_type & PERF_SAMPLE_WEIGHT) { 2147 data->weight.full = latency ?: 2148 intel_get_tsx_weight(meminfo->tsx_tuning); 2149 } else { 2150 data->weight.var1_dw = (u32)latency ?: 2151 intel_get_tsx_weight(meminfo->tsx_tuning); 2152 } 2153 2154 data->sample_flags |= PERF_SAMPLE_WEIGHT_TYPE; 2155 } 2156 2157 if (sample_type & PERF_SAMPLE_DATA_SRC) { 2158 data->data_src.val = get_data_src(event, meminfo->aux); 2159 data->sample_flags |= PERF_SAMPLE_DATA_SRC; 2160 } 2161 2162 if (sample_type & PERF_SAMPLE_ADDR_TYPE) { 2163 data->addr = meminfo->address; 2164 data->sample_flags |= PERF_SAMPLE_ADDR; 2165 } 2166 2167 if (sample_type & PERF_SAMPLE_TRANSACTION) { 2168 data->txn = intel_get_tsx_transaction(meminfo->tsx_tuning, 2169 gprs ? gprs->ax : 0); 2170 data->sample_flags |= PERF_SAMPLE_TRANSACTION; 2171 } 2172 } 2173 2174 if (format_group & PEBS_DATACFG_XMMS) { 2175 struct pebs_xmm *xmm = next_record; 2176 2177 next_record = xmm + 1; 2178 perf_regs->xmm_regs = xmm->xmm; 2179 } 2180 2181 if (format_group & PEBS_DATACFG_LBRS) { 2182 struct lbr_entry *lbr = next_record; 2183 int num_lbr = ((format_group >> PEBS_DATACFG_LBR_SHIFT) 2184 & 0xff) + 1; 2185 next_record = next_record + num_lbr * sizeof(struct lbr_entry); 2186 2187 if (has_branch_stack(event)) { 2188 intel_pmu_store_pebs_lbrs(lbr); 2189 intel_pmu_lbr_save_brstack(data, cpuc, event); 2190 } 2191 } 2192 2193 if (format_group & (PEBS_DATACFG_CNTR | PEBS_DATACFG_METRICS)) { 2194 struct pebs_cntr_header *cntr = next_record; 2195 unsigned int nr; 2196 2197 next_record += sizeof(struct pebs_cntr_header); 2198 /* 2199 * The PEBS_DATA_CFG is a global register, which is the 2200 * superset configuration for all PEBS events. 2201 * For the PEBS record of non-sample-read group, ignore 2202 * the counter snapshot fields. 2203 */ 2204 if (is_pebs_counter_event_group(event)) { 2205 __setup_pebs_counter_group(cpuc, event, cntr, next_record); 2206 data->sample_flags |= PERF_SAMPLE_READ; 2207 } 2208 2209 nr = hweight32(cntr->cntr) + hweight32(cntr->fixed); 2210 if (cntr->metrics == INTEL_CNTR_METRICS) 2211 nr += 2; 2212 next_record += nr * sizeof(u64); 2213 } 2214 2215 WARN_ONCE(next_record != __pebs + basic->format_size, 2216 "PEBS record size %u, expected %llu, config %llx\n", 2217 basic->format_size, 2218 (u64)(next_record - __pebs), 2219 format_group); 2220 } 2221 2222 static inline void * 2223 get_next_pebs_record_by_bit(void *base, void *top, int bit) 2224 { 2225 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 2226 void *at; 2227 u64 pebs_status; 2228 2229 /* 2230 * fmt0 does not have a status bitfield (does not use 2231 * perf_record_nhm format) 2232 */ 2233 if (x86_pmu.intel_cap.pebs_format < 1) 2234 return base; 2235 2236 if (base == NULL) 2237 return NULL; 2238 2239 for (at = base; at < top; at += cpuc->pebs_record_size) { 2240 unsigned long status = get_pebs_status(at); 2241 2242 if (test_bit(bit, (unsigned long *)&status)) { 2243 /* PEBS v3 has accurate status bits */ 2244 if (x86_pmu.intel_cap.pebs_format >= 3) 2245 return at; 2246 2247 if (status == (1 << bit)) 2248 return at; 2249 2250 /* clear non-PEBS bit and re-check */ 2251 pebs_status = status & cpuc->pebs_enabled; 2252 pebs_status &= PEBS_COUNTER_MASK; 2253 if (pebs_status == (1 << bit)) 2254 return at; 2255 } 2256 } 2257 return NULL; 2258 } 2259 2260 /* 2261 * Special variant of intel_pmu_save_and_restart() for auto-reload. 2262 */ 2263 static int 2264 intel_pmu_save_and_restart_reload(struct perf_event *event, int count) 2265 { 2266 struct hw_perf_event *hwc = &event->hw; 2267 int shift = 64 - x86_pmu.cntval_bits; 2268 u64 period = hwc->sample_period; 2269 u64 prev_raw_count, new_raw_count; 2270 s64 new, old; 2271 2272 WARN_ON(!period); 2273 2274 /* 2275 * drain_pebs() only happens when the PMU is disabled. 2276 */ 2277 WARN_ON(this_cpu_read(cpu_hw_events.enabled)); 2278 2279 prev_raw_count = local64_read(&hwc->prev_count); 2280 new_raw_count = rdpmc(hwc->event_base_rdpmc); 2281 local64_set(&hwc->prev_count, new_raw_count); 2282 2283 /* 2284 * Since the counter increments a negative counter value and 2285 * overflows on the sign switch, giving the interval: 2286 * 2287 * [-period, 0] 2288 * 2289 * the difference between two consecutive reads is: 2290 * 2291 * A) value2 - value1; 2292 * when no overflows have happened in between, 2293 * 2294 * B) (0 - value1) + (value2 - (-period)); 2295 * when one overflow happened in between, 2296 * 2297 * C) (0 - value1) + (n - 1) * (period) + (value2 - (-period)); 2298 * when @n overflows happened in between. 2299 * 2300 * Here A) is the obvious difference, B) is the extension to the 2301 * discrete interval, where the first term is to the top of the 2302 * interval and the second term is from the bottom of the next 2303 * interval and C) the extension to multiple intervals, where the 2304 * middle term is the whole intervals covered. 2305 * 2306 * An equivalent of C, by reduction, is: 2307 * 2308 * value2 - value1 + n * period 2309 */ 2310 new = ((s64)(new_raw_count << shift) >> shift); 2311 old = ((s64)(prev_raw_count << shift) >> shift); 2312 local64_add(new - old + count * period, &event->count); 2313 2314 local64_set(&hwc->period_left, -new); 2315 2316 perf_event_update_userpage(event); 2317 2318 return 0; 2319 } 2320 2321 typedef void (*setup_fn)(struct perf_event *, struct pt_regs *, void *, 2322 struct perf_sample_data *, struct pt_regs *); 2323 2324 static struct pt_regs dummy_iregs; 2325 2326 static __always_inline void 2327 __intel_pmu_pebs_event(struct perf_event *event, 2328 struct pt_regs *iregs, 2329 struct pt_regs *regs, 2330 struct perf_sample_data *data, 2331 void *at, 2332 setup_fn setup_sample) 2333 { 2334 setup_sample(event, iregs, at, data, regs); 2335 perf_event_output(event, data, regs); 2336 } 2337 2338 static __always_inline void 2339 __intel_pmu_pebs_last_event(struct perf_event *event, 2340 struct pt_regs *iregs, 2341 struct pt_regs *regs, 2342 struct perf_sample_data *data, 2343 void *at, 2344 int count, 2345 setup_fn setup_sample) 2346 { 2347 struct hw_perf_event *hwc = &event->hw; 2348 2349 setup_sample(event, iregs, at, data, regs); 2350 if (iregs == &dummy_iregs) { 2351 /* 2352 * The PEBS records may be drained in the non-overflow context, 2353 * e.g., large PEBS + context switch. Perf should treat the 2354 * last record the same as other PEBS records, and doesn't 2355 * invoke the generic overflow handler. 2356 */ 2357 perf_event_output(event, data, regs); 2358 } else { 2359 /* 2360 * All but the last records are processed. 2361 * The last one is left to be able to call the overflow handler. 2362 */ 2363 if (perf_event_overflow(event, data, regs)) 2364 x86_pmu_stop(event, 0); 2365 } 2366 2367 if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) { 2368 if ((is_pebs_counter_event_group(event))) { 2369 /* 2370 * The value of each sample has been updated when setup 2371 * the corresponding sample data. 2372 */ 2373 perf_event_update_userpage(event); 2374 } else { 2375 /* 2376 * Now, auto-reload is only enabled in fixed period mode. 2377 * The reload value is always hwc->sample_period. 2378 * May need to change it, if auto-reload is enabled in 2379 * freq mode later. 2380 */ 2381 intel_pmu_save_and_restart_reload(event, count); 2382 } 2383 } else { 2384 /* 2385 * For a non-precise event, it's possible the 2386 * counters-snapshotting records a positive value for the 2387 * overflowed event. Then the HW auto-reload mechanism 2388 * reset the counter to 0 immediately, because the 2389 * pebs_event_reset is cleared if the PERF_X86_EVENT_AUTO_RELOAD 2390 * is not set. The counter backwards may be observed in a 2391 * PMI handler. 2392 * 2393 * Since the event value has been updated when processing the 2394 * counters-snapshotting record, only needs to set the new 2395 * period for the counter. 2396 */ 2397 if (is_pebs_counter_event_group(event)) 2398 static_call(x86_pmu_set_period)(event); 2399 else 2400 intel_pmu_save_and_restart(event); 2401 } 2402 } 2403 2404 static __always_inline void 2405 __intel_pmu_pebs_events(struct perf_event *event, 2406 struct pt_regs *iregs, 2407 struct perf_sample_data *data, 2408 void *base, void *top, 2409 int bit, int count, 2410 setup_fn setup_sample) 2411 { 2412 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 2413 struct x86_perf_regs perf_regs; 2414 struct pt_regs *regs = &perf_regs.regs; 2415 void *at = get_next_pebs_record_by_bit(base, top, bit); 2416 int cnt = count; 2417 2418 if (!iregs) 2419 iregs = &dummy_iregs; 2420 2421 while (cnt > 1) { 2422 __intel_pmu_pebs_event(event, iregs, regs, data, at, setup_sample); 2423 at += cpuc->pebs_record_size; 2424 at = get_next_pebs_record_by_bit(at, top, bit); 2425 cnt--; 2426 } 2427 2428 __intel_pmu_pebs_last_event(event, iregs, regs, data, at, count, setup_sample); 2429 } 2430 2431 static void intel_pmu_drain_pebs_core(struct pt_regs *iregs, struct perf_sample_data *data) 2432 { 2433 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 2434 struct debug_store *ds = cpuc->ds; 2435 struct perf_event *event = cpuc->events[0]; /* PMC0 only */ 2436 struct pebs_record_core *at, *top; 2437 int n; 2438 2439 if (!x86_pmu.pebs_active) 2440 return; 2441 2442 at = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base; 2443 top = (struct pebs_record_core *)(unsigned long)ds->pebs_index; 2444 2445 /* 2446 * Whatever else happens, drain the thing 2447 */ 2448 ds->pebs_index = ds->pebs_buffer_base; 2449 2450 if (!test_bit(0, cpuc->active_mask)) 2451 return; 2452 2453 WARN_ON_ONCE(!event); 2454 2455 if (!event->attr.precise_ip) 2456 return; 2457 2458 n = top - at; 2459 if (n <= 0) { 2460 if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD) 2461 intel_pmu_save_and_restart_reload(event, 0); 2462 return; 2463 } 2464 2465 __intel_pmu_pebs_events(event, iregs, data, at, top, 0, n, 2466 setup_pebs_fixed_sample_data); 2467 } 2468 2469 static void intel_pmu_pebs_event_update_no_drain(struct cpu_hw_events *cpuc, int size) 2470 { 2471 struct perf_event *event; 2472 int bit; 2473 2474 /* 2475 * The drain_pebs() could be called twice in a short period 2476 * for auto-reload event in pmu::read(). There are no 2477 * overflows have happened in between. 2478 * It needs to call intel_pmu_save_and_restart_reload() to 2479 * update the event->count for this case. 2480 */ 2481 for_each_set_bit(bit, (unsigned long *)&cpuc->pebs_enabled, size) { 2482 event = cpuc->events[bit]; 2483 if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD) 2484 intel_pmu_save_and_restart_reload(event, 0); 2485 } 2486 } 2487 2488 static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs, struct perf_sample_data *data) 2489 { 2490 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 2491 struct debug_store *ds = cpuc->ds; 2492 struct perf_event *event; 2493 void *base, *at, *top; 2494 short counts[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {}; 2495 short error[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {}; 2496 int max_pebs_events = intel_pmu_max_num_pebs(NULL); 2497 int bit, i, size; 2498 u64 mask; 2499 2500 if (!x86_pmu.pebs_active) 2501 return; 2502 2503 base = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base; 2504 top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index; 2505 2506 ds->pebs_index = ds->pebs_buffer_base; 2507 2508 mask = x86_pmu.pebs_events_mask; 2509 size = max_pebs_events; 2510 if (x86_pmu.flags & PMU_FL_PEBS_ALL) { 2511 mask |= x86_pmu.fixed_cntr_mask64 << INTEL_PMC_IDX_FIXED; 2512 size = INTEL_PMC_IDX_FIXED + x86_pmu_max_num_counters_fixed(NULL); 2513 } 2514 2515 if (unlikely(base >= top)) { 2516 intel_pmu_pebs_event_update_no_drain(cpuc, size); 2517 return; 2518 } 2519 2520 for (at = base; at < top; at += x86_pmu.pebs_record_size) { 2521 struct pebs_record_nhm *p = at; 2522 u64 pebs_status; 2523 2524 pebs_status = p->status & cpuc->pebs_enabled; 2525 pebs_status &= mask; 2526 2527 /* PEBS v3 has more accurate status bits */ 2528 if (x86_pmu.intel_cap.pebs_format >= 3) { 2529 for_each_set_bit(bit, (unsigned long *)&pebs_status, size) 2530 counts[bit]++; 2531 2532 continue; 2533 } 2534 2535 /* 2536 * On some CPUs the PEBS status can be zero when PEBS is 2537 * racing with clearing of GLOBAL_STATUS. 2538 * 2539 * Normally we would drop that record, but in the 2540 * case when there is only a single active PEBS event 2541 * we can assume it's for that event. 2542 */ 2543 if (!pebs_status && cpuc->pebs_enabled && 2544 !(cpuc->pebs_enabled & (cpuc->pebs_enabled-1))) 2545 pebs_status = p->status = cpuc->pebs_enabled; 2546 2547 bit = find_first_bit((unsigned long *)&pebs_status, 2548 max_pebs_events); 2549 2550 if (!(x86_pmu.pebs_events_mask & (1 << bit))) 2551 continue; 2552 2553 /* 2554 * The PEBS hardware does not deal well with the situation 2555 * when events happen near to each other and multiple bits 2556 * are set. But it should happen rarely. 2557 * 2558 * If these events include one PEBS and multiple non-PEBS 2559 * events, it doesn't impact PEBS record. The record will 2560 * be handled normally. (slow path) 2561 * 2562 * If these events include two or more PEBS events, the 2563 * records for the events can be collapsed into a single 2564 * one, and it's not possible to reconstruct all events 2565 * that caused the PEBS record. It's called collision. 2566 * If collision happened, the record will be dropped. 2567 */ 2568 if (pebs_status != (1ULL << bit)) { 2569 for_each_set_bit(i, (unsigned long *)&pebs_status, size) 2570 error[i]++; 2571 continue; 2572 } 2573 2574 counts[bit]++; 2575 } 2576 2577 for_each_set_bit(bit, (unsigned long *)&mask, size) { 2578 if ((counts[bit] == 0) && (error[bit] == 0)) 2579 continue; 2580 2581 event = cpuc->events[bit]; 2582 if (WARN_ON_ONCE(!event)) 2583 continue; 2584 2585 if (WARN_ON_ONCE(!event->attr.precise_ip)) 2586 continue; 2587 2588 /* log dropped samples number */ 2589 if (error[bit]) { 2590 perf_log_lost_samples(event, error[bit]); 2591 2592 if (iregs && perf_event_account_interrupt(event)) 2593 x86_pmu_stop(event, 0); 2594 } 2595 2596 if (counts[bit]) { 2597 __intel_pmu_pebs_events(event, iregs, data, base, 2598 top, bit, counts[bit], 2599 setup_pebs_fixed_sample_data); 2600 } 2601 } 2602 } 2603 2604 static void intel_pmu_drain_pebs_icl(struct pt_regs *iregs, struct perf_sample_data *data) 2605 { 2606 short counts[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] = {}; 2607 void *last[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS]; 2608 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 2609 struct debug_store *ds = cpuc->ds; 2610 struct x86_perf_regs perf_regs; 2611 struct pt_regs *regs = &perf_regs.regs; 2612 struct pebs_basic *basic; 2613 struct perf_event *event; 2614 void *base, *at, *top; 2615 int bit; 2616 u64 mask; 2617 2618 if (!x86_pmu.pebs_active) 2619 return; 2620 2621 base = (struct pebs_basic *)(unsigned long)ds->pebs_buffer_base; 2622 top = (struct pebs_basic *)(unsigned long)ds->pebs_index; 2623 2624 ds->pebs_index = ds->pebs_buffer_base; 2625 2626 mask = hybrid(cpuc->pmu, pebs_events_mask) | 2627 (hybrid(cpuc->pmu, fixed_cntr_mask64) << INTEL_PMC_IDX_FIXED); 2628 2629 if (unlikely(base >= top)) { 2630 intel_pmu_pebs_event_update_no_drain(cpuc, X86_PMC_IDX_MAX); 2631 return; 2632 } 2633 2634 if (!iregs) 2635 iregs = &dummy_iregs; 2636 2637 /* Process all but the last event for each counter. */ 2638 for (at = base; at < top; at += basic->format_size) { 2639 u64 pebs_status; 2640 2641 basic = at; 2642 if (basic->format_size != cpuc->pebs_record_size) 2643 continue; 2644 2645 pebs_status = basic->applicable_counters & cpuc->pebs_enabled & mask; 2646 for_each_set_bit(bit, (unsigned long *)&pebs_status, X86_PMC_IDX_MAX) { 2647 event = cpuc->events[bit]; 2648 2649 if (WARN_ON_ONCE(!event) || 2650 WARN_ON_ONCE(!event->attr.precise_ip)) 2651 continue; 2652 2653 if (counts[bit]++) { 2654 __intel_pmu_pebs_event(event, iregs, regs, data, last[bit], 2655 setup_pebs_adaptive_sample_data); 2656 } 2657 last[bit] = at; 2658 } 2659 } 2660 2661 for_each_set_bit(bit, (unsigned long *)&mask, X86_PMC_IDX_MAX) { 2662 if (!counts[bit]) 2663 continue; 2664 2665 event = cpuc->events[bit]; 2666 2667 __intel_pmu_pebs_last_event(event, iregs, regs, data, last[bit], 2668 counts[bit], setup_pebs_adaptive_sample_data); 2669 } 2670 } 2671 2672 /* 2673 * BTS, PEBS probe and setup 2674 */ 2675 2676 void __init intel_ds_init(void) 2677 { 2678 /* 2679 * No support for 32bit formats 2680 */ 2681 if (!boot_cpu_has(X86_FEATURE_DTES64)) 2682 return; 2683 2684 x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS); 2685 x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS); 2686 x86_pmu.pebs_buffer_size = PEBS_BUFFER_SIZE; 2687 if (x86_pmu.version <= 4) 2688 x86_pmu.pebs_no_isolation = 1; 2689 2690 if (x86_pmu.pebs) { 2691 char pebs_type = x86_pmu.intel_cap.pebs_trap ? '+' : '-'; 2692 char *pebs_qual = ""; 2693 int format = x86_pmu.intel_cap.pebs_format; 2694 2695 if (format < 4) 2696 x86_pmu.intel_cap.pebs_baseline = 0; 2697 2698 switch (format) { 2699 case 0: 2700 pr_cont("PEBS fmt0%c, ", pebs_type); 2701 x86_pmu.pebs_record_size = sizeof(struct pebs_record_core); 2702 /* 2703 * Using >PAGE_SIZE buffers makes the WRMSR to 2704 * PERF_GLOBAL_CTRL in intel_pmu_enable_all() 2705 * mysteriously hang on Core2. 2706 * 2707 * As a workaround, we don't do this. 2708 */ 2709 x86_pmu.pebs_buffer_size = PAGE_SIZE; 2710 x86_pmu.drain_pebs = intel_pmu_drain_pebs_core; 2711 break; 2712 2713 case 1: 2714 pr_cont("PEBS fmt1%c, ", pebs_type); 2715 x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm); 2716 x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm; 2717 break; 2718 2719 case 2: 2720 pr_cont("PEBS fmt2%c, ", pebs_type); 2721 x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw); 2722 x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm; 2723 break; 2724 2725 case 3: 2726 pr_cont("PEBS fmt3%c, ", pebs_type); 2727 x86_pmu.pebs_record_size = 2728 sizeof(struct pebs_record_skl); 2729 x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm; 2730 x86_pmu.large_pebs_flags |= PERF_SAMPLE_TIME; 2731 break; 2732 2733 case 6: 2734 if (x86_pmu.intel_cap.pebs_baseline) { 2735 x86_pmu.large_pebs_flags |= PERF_SAMPLE_READ; 2736 x86_pmu.late_setup = intel_pmu_late_setup; 2737 } 2738 fallthrough; 2739 case 5: 2740 x86_pmu.pebs_ept = 1; 2741 fallthrough; 2742 case 4: 2743 x86_pmu.drain_pebs = intel_pmu_drain_pebs_icl; 2744 x86_pmu.pebs_record_size = sizeof(struct pebs_basic); 2745 if (x86_pmu.intel_cap.pebs_baseline) { 2746 x86_pmu.large_pebs_flags |= 2747 PERF_SAMPLE_BRANCH_STACK | 2748 PERF_SAMPLE_TIME; 2749 x86_pmu.flags |= PMU_FL_PEBS_ALL; 2750 x86_pmu.pebs_capable = ~0ULL; 2751 pebs_qual = "-baseline"; 2752 x86_get_pmu(smp_processor_id())->capabilities |= PERF_PMU_CAP_EXTENDED_REGS; 2753 } else { 2754 /* Only basic record supported */ 2755 x86_pmu.large_pebs_flags &= 2756 ~(PERF_SAMPLE_ADDR | 2757 PERF_SAMPLE_TIME | 2758 PERF_SAMPLE_DATA_SRC | 2759 PERF_SAMPLE_TRANSACTION | 2760 PERF_SAMPLE_REGS_USER | 2761 PERF_SAMPLE_REGS_INTR); 2762 } 2763 pr_cont("PEBS fmt%d%c%s, ", format, pebs_type, pebs_qual); 2764 2765 /* 2766 * The PEBS-via-PT is not supported on hybrid platforms, 2767 * because not all CPUs of a hybrid machine support it. 2768 * The global x86_pmu.intel_cap, which only contains the 2769 * common capabilities, is used to check the availability 2770 * of the feature. The per-PMU pebs_output_pt_available 2771 * in a hybrid machine should be ignored. 2772 */ 2773 if (x86_pmu.intel_cap.pebs_output_pt_available) { 2774 pr_cont("PEBS-via-PT, "); 2775 x86_get_pmu(smp_processor_id())->capabilities |= PERF_PMU_CAP_AUX_OUTPUT; 2776 } 2777 2778 break; 2779 2780 default: 2781 pr_cont("no PEBS fmt%d%c, ", format, pebs_type); 2782 x86_pmu.pebs = 0; 2783 } 2784 } 2785 } 2786 2787 void perf_restore_debug_store(void) 2788 { 2789 struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds); 2790 2791 if (!x86_pmu.bts && !x86_pmu.pebs) 2792 return; 2793 2794 wrmsrq(MSR_IA32_DS_AREA, (unsigned long)ds); 2795 } 2796