Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 2 | #include <linux/bitops.h> |
| 3 | #include <linux/types.h> |
| 4 | #include <linux/slab.h> |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 5 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 6 | #include <asm/perf_event.h> |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 7 | #include <asm/insn.h> |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 8 | |
Borislav Petkov | 27f6d22 | 2016-02-10 10:55:23 +0100 | [diff] [blame] | 9 | #include "../perf_event.h" |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 10 | |
| 11 | /* The size of a BTS record in bytes: */ |
| 12 | #define BTS_RECORD_SIZE 24 |
| 13 | |
| 14 | #define BTS_BUFFER_SIZE (PAGE_SIZE << 4) |
Yan, Zheng | 1561749 | 2015-05-06 15:33:52 -0400 | [diff] [blame] | 15 | #define PEBS_BUFFER_SIZE (PAGE_SIZE << 4) |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 16 | #define PEBS_FIXUP_SIZE PAGE_SIZE |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * pebs_record_32 for p4 and core not supported |
| 20 | |
| 21 | struct pebs_record_32 { |
| 22 | u32 flags, ip; |
| 23 | u32 ax, bc, cx, dx; |
| 24 | u32 si, di, bp, sp; |
| 25 | }; |
| 26 | |
| 27 | */ |
| 28 | |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 29 | union intel_x86_pebs_dse { |
| 30 | u64 val; |
| 31 | struct { |
| 32 | unsigned int ld_dse:4; |
| 33 | unsigned int ld_stlb_miss:1; |
| 34 | unsigned int ld_locked:1; |
| 35 | unsigned int ld_reserved:26; |
| 36 | }; |
| 37 | struct { |
| 38 | unsigned int st_l1d_hit:1; |
| 39 | unsigned int st_reserved1:3; |
| 40 | unsigned int st_stlb_miss:1; |
| 41 | unsigned int st_locked:1; |
| 42 | unsigned int st_reserved2:26; |
| 43 | }; |
| 44 | }; |
| 45 | |
| 46 | |
| 47 | /* |
| 48 | * Map PEBS Load Latency Data Source encodings to generic |
| 49 | * memory data source information |
| 50 | */ |
| 51 | #define P(a, b) PERF_MEM_S(a, b) |
| 52 | #define OP_LH (P(OP, LOAD) | P(LVL, HIT)) |
Andi Kleen | 6ae5fa6 | 2017-08-16 15:21:54 -0700 | [diff] [blame] | 53 | #define LEVEL(x) P(LVLNUM, x) |
| 54 | #define REM P(REMOTE, REMOTE) |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 55 | #define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS)) |
| 56 | |
Andi Kleen | e17dc65 | 2016-03-01 14:25:24 -0800 | [diff] [blame] | 57 | /* Version for Sandy Bridge and later */ |
| 58 | static u64 pebs_data_source[] = { |
Andi Kleen | 6ae5fa6 | 2017-08-16 15:21:54 -0700 | [diff] [blame] | 59 | P(OP, LOAD) | P(LVL, MISS) | LEVEL(L3) | P(SNOOP, NA),/* 0x00:ukn L3 */ |
| 60 | OP_LH | P(LVL, L1) | LEVEL(L1) | P(SNOOP, NONE), /* 0x01: L1 local */ |
| 61 | OP_LH | P(LVL, LFB) | LEVEL(LFB) | P(SNOOP, NONE), /* 0x02: LFB hit */ |
| 62 | OP_LH | P(LVL, L2) | LEVEL(L2) | P(SNOOP, NONE), /* 0x03: L2 hit */ |
| 63 | OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, NONE), /* 0x04: L3 hit */ |
| 64 | OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, MISS), /* 0x05: L3 hit, snoop miss */ |
| 65 | OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT), /* 0x06: L3 hit, snoop hit */ |
| 66 | OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM), /* 0x07: L3 hit, snoop hitm */ |
| 67 | OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HIT), /* 0x08: L3 miss snoop hit */ |
| 68 | OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HITM), /* 0x09: L3 miss snoop hitm*/ |
| 69 | OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | P(SNOOP, HIT), /* 0x0a: L3 miss, shared */ |
| 70 | OP_LH | P(LVL, REM_RAM1) | REM | LEVEL(L3) | P(SNOOP, HIT), /* 0x0b: L3 miss, shared */ |
| 71 | OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | SNOOP_NONE_MISS, /* 0x0c: L3 miss, excl */ |
| 72 | OP_LH | P(LVL, REM_RAM1) | LEVEL(RAM) | REM | SNOOP_NONE_MISS, /* 0x0d: L3 miss, excl */ |
| 73 | OP_LH | P(LVL, IO) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0e: I/O */ |
| 74 | OP_LH | P(LVL, UNC) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0f: uncached */ |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 75 | }; |
| 76 | |
Andi Kleen | e17dc65 | 2016-03-01 14:25:24 -0800 | [diff] [blame] | 77 | /* Patch up minor differences in the bits */ |
| 78 | void __init intel_pmu_pebs_data_source_nhm(void) |
| 79 | { |
Andi Kleen | 6ae5fa6 | 2017-08-16 15:21:54 -0700 | [diff] [blame] | 80 | pebs_data_source[0x05] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT); |
| 81 | pebs_data_source[0x06] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM); |
| 82 | pebs_data_source[0x07] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM); |
| 83 | } |
| 84 | |
| 85 | void __init intel_pmu_pebs_data_source_skl(bool pmem) |
| 86 | { |
| 87 | u64 pmem_or_l4 = pmem ? LEVEL(PMEM) : LEVEL(L4); |
| 88 | |
| 89 | pebs_data_source[0x08] = OP_LH | pmem_or_l4 | P(SNOOP, HIT); |
| 90 | pebs_data_source[0x09] = OP_LH | pmem_or_l4 | REM | P(SNOOP, HIT); |
| 91 | pebs_data_source[0x0b] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, NONE); |
| 92 | pebs_data_source[0x0c] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOPX, FWD); |
| 93 | pebs_data_source[0x0d] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOP, HITM); |
Andi Kleen | e17dc65 | 2016-03-01 14:25:24 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Stephane Eranian | 9ad64c0 | 2013-01-24 16:10:34 +0100 | [diff] [blame] | 96 | static u64 precise_store_data(u64 status) |
| 97 | { |
| 98 | union intel_x86_pebs_dse dse; |
| 99 | u64 val = P(OP, STORE) | P(SNOOP, NA) | P(LVL, L1) | P(TLB, L2); |
| 100 | |
| 101 | dse.val = status; |
| 102 | |
| 103 | /* |
| 104 | * bit 4: TLB access |
| 105 | * 1 = stored missed 2nd level TLB |
| 106 | * |
| 107 | * so it either hit the walker or the OS |
| 108 | * otherwise hit 2nd level TLB |
| 109 | */ |
| 110 | if (dse.st_stlb_miss) |
| 111 | val |= P(TLB, MISS); |
| 112 | else |
| 113 | val |= P(TLB, HIT); |
| 114 | |
| 115 | /* |
| 116 | * bit 0: hit L1 data cache |
| 117 | * if not set, then all we know is that |
| 118 | * it missed L1D |
| 119 | */ |
| 120 | if (dse.st_l1d_hit) |
| 121 | val |= P(LVL, HIT); |
| 122 | else |
| 123 | val |= P(LVL, MISS); |
| 124 | |
| 125 | /* |
| 126 | * bit 5: Locked prefix |
| 127 | */ |
| 128 | if (dse.st_locked) |
| 129 | val |= P(LOCK, LOCKED); |
| 130 | |
| 131 | return val; |
| 132 | } |
| 133 | |
Stephane Eranian | c8aab2e | 2014-08-11 21:27:13 +0200 | [diff] [blame] | 134 | static u64 precise_datala_hsw(struct perf_event *event, u64 status) |
Andi Kleen | f9134f3 | 2013-06-17 17:36:52 -0700 | [diff] [blame] | 135 | { |
| 136 | union perf_mem_data_src dse; |
| 137 | |
Stephane Eranian | 770eee1 | 2014-08-11 21:27:12 +0200 | [diff] [blame] | 138 | dse.val = PERF_MEM_NA; |
| 139 | |
| 140 | if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) |
| 141 | dse.mem_op = PERF_MEM_OP_STORE; |
| 142 | else if (event->hw.flags & PERF_X86_EVENT_PEBS_LD_HSW) |
| 143 | dse.mem_op = PERF_MEM_OP_LOAD; |
Stephane Eranian | 722e76e | 2014-05-15 17:56:44 +0200 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | * L1 info only valid for following events: |
| 147 | * |
| 148 | * MEM_UOPS_RETIRED.STLB_MISS_STORES |
| 149 | * MEM_UOPS_RETIRED.LOCK_STORES |
| 150 | * MEM_UOPS_RETIRED.SPLIT_STORES |
| 151 | * MEM_UOPS_RETIRED.ALL_STORES |
| 152 | */ |
Stephane Eranian | c8aab2e | 2014-08-11 21:27:13 +0200 | [diff] [blame] | 153 | if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) { |
| 154 | if (status & 1) |
| 155 | dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_HIT; |
| 156 | else |
| 157 | dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_MISS; |
| 158 | } |
Andi Kleen | f9134f3 | 2013-06-17 17:36:52 -0700 | [diff] [blame] | 159 | return dse.val; |
| 160 | } |
| 161 | |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 162 | static u64 load_latency_data(u64 status) |
| 163 | { |
| 164 | union intel_x86_pebs_dse dse; |
| 165 | u64 val; |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 166 | |
| 167 | dse.val = status; |
| 168 | |
| 169 | /* |
| 170 | * use the mapping table for bit 0-3 |
| 171 | */ |
| 172 | val = pebs_data_source[dse.ld_dse]; |
| 173 | |
| 174 | /* |
| 175 | * Nehalem models do not support TLB, Lock infos |
| 176 | */ |
Andi Kleen | 9529835 | 2017-08-16 15:21:53 -0700 | [diff] [blame] | 177 | if (x86_pmu.pebs_no_tlb) { |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 178 | val |= P(TLB, NA) | P(LOCK, NA); |
| 179 | return val; |
| 180 | } |
| 181 | /* |
| 182 | * bit 4: TLB access |
| 183 | * 0 = did not miss 2nd level TLB |
| 184 | * 1 = missed 2nd level TLB |
| 185 | */ |
| 186 | if (dse.ld_stlb_miss) |
| 187 | val |= P(TLB, MISS) | P(TLB, L2); |
| 188 | else |
| 189 | val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2); |
| 190 | |
| 191 | /* |
| 192 | * bit 5: locked prefix |
| 193 | */ |
| 194 | if (dse.ld_locked) |
| 195 | val |= P(LOCK, LOCKED); |
| 196 | |
| 197 | return val; |
| 198 | } |
| 199 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 200 | struct pebs_record_core { |
| 201 | u64 flags, ip; |
| 202 | u64 ax, bx, cx, dx; |
| 203 | u64 si, di, bp, sp; |
| 204 | u64 r8, r9, r10, r11; |
| 205 | u64 r12, r13, r14, r15; |
| 206 | }; |
| 207 | |
| 208 | struct pebs_record_nhm { |
| 209 | u64 flags, ip; |
| 210 | u64 ax, bx, cx, dx; |
| 211 | u64 si, di, bp, sp; |
| 212 | u64 r8, r9, r10, r11; |
| 213 | u64 r12, r13, r14, r15; |
| 214 | u64 status, dla, dse, lat; |
| 215 | }; |
| 216 | |
Andi Kleen | 130768b | 2013-06-17 17:36:47 -0700 | [diff] [blame] | 217 | /* |
| 218 | * Same as pebs_record_nhm, with two additional fields. |
| 219 | */ |
| 220 | struct pebs_record_hsw { |
Andi Kleen | 748e86a | 2013-09-05 20:37:39 -0700 | [diff] [blame] | 221 | u64 flags, ip; |
| 222 | u64 ax, bx, cx, dx; |
| 223 | u64 si, di, bp, sp; |
| 224 | u64 r8, r9, r10, r11; |
| 225 | u64 r12, r13, r14, r15; |
| 226 | u64 status, dla, dse, lat; |
Peter Zijlstra | d2beea4 | 2013-09-12 13:00:47 +0200 | [diff] [blame] | 227 | u64 real_ip, tsx_tuning; |
Andi Kleen | 748e86a | 2013-09-05 20:37:39 -0700 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | union hsw_tsx_tuning { |
| 231 | struct { |
| 232 | u32 cycles_last_block : 32, |
| 233 | hle_abort : 1, |
| 234 | rtm_abort : 1, |
| 235 | instruction_abort : 1, |
| 236 | non_instruction_abort : 1, |
| 237 | retry : 1, |
| 238 | data_conflict : 1, |
| 239 | capacity_writes : 1, |
| 240 | capacity_reads : 1; |
| 241 | }; |
| 242 | u64 value; |
Andi Kleen | 130768b | 2013-06-17 17:36:47 -0700 | [diff] [blame] | 243 | }; |
| 244 | |
Andi Kleen | a405bad | 2013-09-20 07:40:40 -0700 | [diff] [blame] | 245 | #define PEBS_HSW_TSX_FLAGS 0xff00000000ULL |
| 246 | |
Andi Kleen | 2f7ebf2 | 2015-05-10 12:22:40 -0700 | [diff] [blame] | 247 | /* Same as HSW, plus TSC */ |
| 248 | |
| 249 | struct pebs_record_skl { |
| 250 | u64 flags, ip; |
| 251 | u64 ax, bx, cx, dx; |
| 252 | u64 si, di, bp, sp; |
| 253 | u64 r8, r9, r10, r11; |
| 254 | u64 r12, r13, r14, r15; |
| 255 | u64 status, dla, dse, lat; |
| 256 | u64 real_ip, tsx_tuning; |
| 257 | u64 tsc; |
| 258 | }; |
| 259 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 260 | void init_debug_store_on_cpu(int cpu) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 261 | { |
| 262 | struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; |
| 263 | |
| 264 | if (!ds) |
| 265 | return; |
| 266 | |
| 267 | wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, |
| 268 | (u32)((u64)(unsigned long)ds), |
| 269 | (u32)((u64)(unsigned long)ds >> 32)); |
| 270 | } |
| 271 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 272 | void fini_debug_store_on_cpu(int cpu) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 273 | { |
| 274 | if (!per_cpu(cpu_hw_events, cpu).ds) |
| 275 | return; |
| 276 | |
| 277 | wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0); |
| 278 | } |
| 279 | |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 280 | static DEFINE_PER_CPU(void *, insn_buffer); |
| 281 | |
Peter Zijlstra | 5ee25c8 | 2010-10-19 14:15:04 +0200 | [diff] [blame] | 282 | static int alloc_pebs_buffer(int cpu) |
| 283 | { |
| 284 | struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; |
Peter Zijlstra | 96681fc | 2010-10-19 14:55:33 +0200 | [diff] [blame] | 285 | int node = cpu_to_node(cpu); |
Yan, Zheng | 3569c0d | 2015-05-06 15:33:50 -0400 | [diff] [blame] | 286 | int max; |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 287 | void *buffer, *ibuffer; |
Peter Zijlstra | 5ee25c8 | 2010-10-19 14:15:04 +0200 | [diff] [blame] | 288 | |
| 289 | if (!x86_pmu.pebs) |
| 290 | return 0; |
| 291 | |
Jiri Olsa | e72daf3 | 2016-03-01 20:03:52 +0100 | [diff] [blame] | 292 | buffer = kzalloc_node(x86_pmu.pebs_buffer_size, GFP_KERNEL, node); |
Peter Zijlstra | 5ee25c8 | 2010-10-19 14:15:04 +0200 | [diff] [blame] | 293 | if (unlikely(!buffer)) |
| 294 | return -ENOMEM; |
| 295 | |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 296 | /* |
| 297 | * HSW+ already provides us the eventing ip; no need to allocate this |
| 298 | * buffer then. |
| 299 | */ |
| 300 | if (x86_pmu.intel_cap.pebs_format < 2) { |
| 301 | ibuffer = kzalloc_node(PEBS_FIXUP_SIZE, GFP_KERNEL, node); |
| 302 | if (!ibuffer) { |
| 303 | kfree(buffer); |
| 304 | return -ENOMEM; |
| 305 | } |
| 306 | per_cpu(insn_buffer, cpu) = ibuffer; |
| 307 | } |
| 308 | |
Jiri Olsa | e72daf3 | 2016-03-01 20:03:52 +0100 | [diff] [blame] | 309 | max = x86_pmu.pebs_buffer_size / x86_pmu.pebs_record_size; |
Peter Zijlstra | 5ee25c8 | 2010-10-19 14:15:04 +0200 | [diff] [blame] | 310 | |
| 311 | ds->pebs_buffer_base = (u64)(unsigned long)buffer; |
| 312 | ds->pebs_index = ds->pebs_buffer_base; |
| 313 | ds->pebs_absolute_maximum = ds->pebs_buffer_base + |
| 314 | max * x86_pmu.pebs_record_size; |
| 315 | |
Peter Zijlstra | 5ee25c8 | 2010-10-19 14:15:04 +0200 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | |
Peter Zijlstra | b39f88a | 2010-10-19 14:08:29 +0200 | [diff] [blame] | 319 | static void release_pebs_buffer(int cpu) |
| 320 | { |
| 321 | struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; |
| 322 | |
| 323 | if (!ds || !x86_pmu.pebs) |
| 324 | return; |
| 325 | |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 326 | kfree(per_cpu(insn_buffer, cpu)); |
| 327 | per_cpu(insn_buffer, cpu) = NULL; |
| 328 | |
Peter Zijlstra | b39f88a | 2010-10-19 14:08:29 +0200 | [diff] [blame] | 329 | kfree((void *)(unsigned long)ds->pebs_buffer_base); |
| 330 | ds->pebs_buffer_base = 0; |
| 331 | } |
| 332 | |
Peter Zijlstra | 5ee25c8 | 2010-10-19 14:15:04 +0200 | [diff] [blame] | 333 | static int alloc_bts_buffer(int cpu) |
| 334 | { |
| 335 | struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; |
Peter Zijlstra | 96681fc | 2010-10-19 14:55:33 +0200 | [diff] [blame] | 336 | int node = cpu_to_node(cpu); |
Peter Zijlstra | 5ee25c8 | 2010-10-19 14:15:04 +0200 | [diff] [blame] | 337 | int max, thresh; |
| 338 | void *buffer; |
| 339 | |
| 340 | if (!x86_pmu.bts) |
| 341 | return 0; |
| 342 | |
David Rientjes | 4485154 | 2014-06-30 16:04:08 -0700 | [diff] [blame] | 343 | buffer = kzalloc_node(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, node); |
| 344 | if (unlikely(!buffer)) { |
| 345 | WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__); |
Peter Zijlstra | 5ee25c8 | 2010-10-19 14:15:04 +0200 | [diff] [blame] | 346 | return -ENOMEM; |
David Rientjes | 4485154 | 2014-06-30 16:04:08 -0700 | [diff] [blame] | 347 | } |
Peter Zijlstra | 5ee25c8 | 2010-10-19 14:15:04 +0200 | [diff] [blame] | 348 | |
| 349 | max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE; |
| 350 | thresh = max / 16; |
| 351 | |
| 352 | ds->bts_buffer_base = (u64)(unsigned long)buffer; |
| 353 | ds->bts_index = ds->bts_buffer_base; |
| 354 | ds->bts_absolute_maximum = ds->bts_buffer_base + |
| 355 | max * BTS_RECORD_SIZE; |
| 356 | ds->bts_interrupt_threshold = ds->bts_absolute_maximum - |
| 357 | thresh * BTS_RECORD_SIZE; |
| 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
Peter Zijlstra | b39f88a | 2010-10-19 14:08:29 +0200 | [diff] [blame] | 362 | static void release_bts_buffer(int cpu) |
| 363 | { |
| 364 | struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; |
| 365 | |
| 366 | if (!ds || !x86_pmu.bts) |
| 367 | return; |
| 368 | |
| 369 | kfree((void *)(unsigned long)ds->bts_buffer_base); |
| 370 | ds->bts_buffer_base = 0; |
| 371 | } |
| 372 | |
Peter Zijlstra | 65af94b | 2010-10-19 14:37:23 +0200 | [diff] [blame] | 373 | static int alloc_ds_buffer(int cpu) |
| 374 | { |
Peter Zijlstra | 96681fc | 2010-10-19 14:55:33 +0200 | [diff] [blame] | 375 | int node = cpu_to_node(cpu); |
Peter Zijlstra | 65af94b | 2010-10-19 14:37:23 +0200 | [diff] [blame] | 376 | struct debug_store *ds; |
| 377 | |
Joe Perches | 7bfb7e6 | 2013-08-29 13:59:17 -0700 | [diff] [blame] | 378 | ds = kzalloc_node(sizeof(*ds), GFP_KERNEL, node); |
Peter Zijlstra | 65af94b | 2010-10-19 14:37:23 +0200 | [diff] [blame] | 379 | if (unlikely(!ds)) |
| 380 | return -ENOMEM; |
| 381 | |
| 382 | per_cpu(cpu_hw_events, cpu).ds = ds; |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static void release_ds_buffer(int cpu) |
| 388 | { |
| 389 | struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; |
| 390 | |
| 391 | if (!ds) |
| 392 | return; |
| 393 | |
| 394 | per_cpu(cpu_hw_events, cpu).ds = NULL; |
| 395 | kfree(ds); |
| 396 | } |
| 397 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 398 | void release_ds_buffers(void) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 399 | { |
| 400 | int cpu; |
| 401 | |
| 402 | if (!x86_pmu.bts && !x86_pmu.pebs) |
| 403 | return; |
| 404 | |
| 405 | get_online_cpus(); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 406 | for_each_online_cpu(cpu) |
| 407 | fini_debug_store_on_cpu(cpu); |
| 408 | |
| 409 | for_each_possible_cpu(cpu) { |
Peter Zijlstra | b39f88a | 2010-10-19 14:08:29 +0200 | [diff] [blame] | 410 | release_pebs_buffer(cpu); |
| 411 | release_bts_buffer(cpu); |
Peter Zijlstra | 65af94b | 2010-10-19 14:37:23 +0200 | [diff] [blame] | 412 | release_ds_buffer(cpu); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 413 | } |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 414 | put_online_cpus(); |
| 415 | } |
| 416 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 417 | void reserve_ds_buffers(void) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 418 | { |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 419 | int bts_err = 0, pebs_err = 0; |
| 420 | int cpu; |
| 421 | |
| 422 | x86_pmu.bts_active = 0; |
| 423 | x86_pmu.pebs_active = 0; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 424 | |
| 425 | if (!x86_pmu.bts && !x86_pmu.pebs) |
Peter Zijlstra | f80c9e3 | 2010-10-19 14:50:02 +0200 | [diff] [blame] | 426 | return; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 427 | |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 428 | if (!x86_pmu.bts) |
| 429 | bts_err = 1; |
| 430 | |
| 431 | if (!x86_pmu.pebs) |
| 432 | pebs_err = 1; |
| 433 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 434 | get_online_cpus(); |
| 435 | |
| 436 | for_each_possible_cpu(cpu) { |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 437 | if (alloc_ds_buffer(cpu)) { |
| 438 | bts_err = 1; |
| 439 | pebs_err = 1; |
| 440 | } |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 441 | |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 442 | if (!bts_err && alloc_bts_buffer(cpu)) |
| 443 | bts_err = 1; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 444 | |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 445 | if (!pebs_err && alloc_pebs_buffer(cpu)) |
| 446 | pebs_err = 1; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 447 | |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 448 | if (bts_err && pebs_err) |
| 449 | break; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 450 | } |
| 451 | |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 452 | if (bts_err) { |
| 453 | for_each_possible_cpu(cpu) |
| 454 | release_bts_buffer(cpu); |
| 455 | } |
| 456 | |
| 457 | if (pebs_err) { |
| 458 | for_each_possible_cpu(cpu) |
| 459 | release_pebs_buffer(cpu); |
| 460 | } |
| 461 | |
| 462 | if (bts_err && pebs_err) { |
| 463 | for_each_possible_cpu(cpu) |
| 464 | release_ds_buffer(cpu); |
| 465 | } else { |
| 466 | if (x86_pmu.bts && !bts_err) |
| 467 | x86_pmu.bts_active = 1; |
| 468 | |
| 469 | if (x86_pmu.pebs && !pebs_err) |
| 470 | x86_pmu.pebs_active = 1; |
| 471 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 472 | for_each_online_cpu(cpu) |
| 473 | init_debug_store_on_cpu(cpu); |
| 474 | } |
| 475 | |
| 476 | put_online_cpus(); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* |
| 480 | * BTS |
| 481 | */ |
| 482 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 483 | struct event_constraint bts_constraint = |
Robert Richter | 15c7ad5 | 2012-06-20 20:46:33 +0200 | [diff] [blame] | 484 | EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 485 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 486 | void intel_pmu_enable_bts(u64 config) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 487 | { |
| 488 | unsigned long debugctlmsr; |
| 489 | |
| 490 | debugctlmsr = get_debugctlmsr(); |
| 491 | |
Peter Zijlstra | 7c5ecaf | 2010-03-25 14:51:49 +0100 | [diff] [blame] | 492 | debugctlmsr |= DEBUGCTLMSR_TR; |
| 493 | debugctlmsr |= DEBUGCTLMSR_BTS; |
Alexander Shishkin | 8062382 | 2015-01-30 12:40:35 +0200 | [diff] [blame] | 494 | if (config & ARCH_PERFMON_EVENTSEL_INT) |
| 495 | debugctlmsr |= DEBUGCTLMSR_BTINT; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 496 | |
| 497 | if (!(config & ARCH_PERFMON_EVENTSEL_OS)) |
Peter Zijlstra | 7c5ecaf | 2010-03-25 14:51:49 +0100 | [diff] [blame] | 498 | debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 499 | |
| 500 | if (!(config & ARCH_PERFMON_EVENTSEL_USR)) |
Peter Zijlstra | 7c5ecaf | 2010-03-25 14:51:49 +0100 | [diff] [blame] | 501 | debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 502 | |
| 503 | update_debugctlmsr(debugctlmsr); |
| 504 | } |
| 505 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 506 | void intel_pmu_disable_bts(void) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 507 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 508 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 509 | unsigned long debugctlmsr; |
| 510 | |
| 511 | if (!cpuc->ds) |
| 512 | return; |
| 513 | |
| 514 | debugctlmsr = get_debugctlmsr(); |
| 515 | |
| 516 | debugctlmsr &= |
Peter Zijlstra | 7c5ecaf | 2010-03-25 14:51:49 +0100 | [diff] [blame] | 517 | ~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT | |
| 518 | DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 519 | |
| 520 | update_debugctlmsr(debugctlmsr); |
| 521 | } |
| 522 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 523 | int intel_pmu_drain_bts_buffer(void) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 524 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 525 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 526 | struct debug_store *ds = cpuc->ds; |
| 527 | struct bts_record { |
| 528 | u64 from; |
| 529 | u64 to; |
| 530 | u64 flags; |
| 531 | }; |
Robert Richter | 15c7ad5 | 2012-06-20 20:46:33 +0200 | [diff] [blame] | 532 | struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS]; |
Alexander Shishkin | a09d31f4 | 2015-08-31 17:09:27 +0300 | [diff] [blame] | 533 | struct bts_record *at, *base, *top; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 534 | struct perf_output_handle handle; |
| 535 | struct perf_event_header header; |
| 536 | struct perf_sample_data data; |
Alexander Shishkin | a09d31f4 | 2015-08-31 17:09:27 +0300 | [diff] [blame] | 537 | unsigned long skip = 0; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 538 | struct pt_regs regs; |
| 539 | |
| 540 | if (!event) |
Stephane Eranian | b0b2072 | 2010-09-10 13:28:01 +0200 | [diff] [blame] | 541 | return 0; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 542 | |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 543 | if (!x86_pmu.bts_active) |
Stephane Eranian | b0b2072 | 2010-09-10 13:28:01 +0200 | [diff] [blame] | 544 | return 0; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 545 | |
Alexander Shishkin | a09d31f4 | 2015-08-31 17:09:27 +0300 | [diff] [blame] | 546 | base = (struct bts_record *)(unsigned long)ds->bts_buffer_base; |
| 547 | top = (struct bts_record *)(unsigned long)ds->bts_index; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 548 | |
Alexander Shishkin | a09d31f4 | 2015-08-31 17:09:27 +0300 | [diff] [blame] | 549 | if (top <= base) |
Stephane Eranian | b0b2072 | 2010-09-10 13:28:01 +0200 | [diff] [blame] | 550 | return 0; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 551 | |
Stephane Eranian | 0e48026 | 2013-03-19 16:10:38 +0100 | [diff] [blame] | 552 | memset(®s, 0, sizeof(regs)); |
| 553 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 554 | ds->bts_index = ds->bts_buffer_base; |
| 555 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 556 | perf_sample_data_init(&data, 0, event->hw.last_period); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 557 | |
| 558 | /* |
Alexander Shishkin | a09d31f4 | 2015-08-31 17:09:27 +0300 | [diff] [blame] | 559 | * BTS leaks kernel addresses in branches across the cpl boundary, |
| 560 | * such as traps or system calls, so unless the user is asking for |
| 561 | * kernel tracing (and right now it's not possible), we'd need to |
| 562 | * filter them out. But first we need to count how many of those we |
| 563 | * have in the current batch. This is an extra O(n) pass, however, |
| 564 | * it's much faster than the other one especially considering that |
| 565 | * n <= 2560 (BTS_BUFFER_SIZE / BTS_RECORD_SIZE * 15/16; see the |
| 566 | * alloc_bts_buffer()). |
| 567 | */ |
| 568 | for (at = base; at < top; at++) { |
| 569 | /* |
| 570 | * Note that right now *this* BTS code only works if |
| 571 | * attr::exclude_kernel is set, but let's keep this extra |
| 572 | * check here in case that changes. |
| 573 | */ |
| 574 | if (event->attr.exclude_kernel && |
| 575 | (kernel_ip(at->from) || kernel_ip(at->to))) |
| 576 | skip++; |
| 577 | } |
| 578 | |
| 579 | /* |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 580 | * Prepare a generic sample, i.e. fill in the invariant fields. |
| 581 | * We will overwrite the from and to address before we output |
| 582 | * the sample. |
| 583 | */ |
Peter Zijlstra | e8d8a90 | 2016-03-18 17:31:27 +0100 | [diff] [blame] | 584 | rcu_read_lock(); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 585 | perf_prepare_sample(&header, &data, event, ®s); |
| 586 | |
Alexander Shishkin | a09d31f4 | 2015-08-31 17:09:27 +0300 | [diff] [blame] | 587 | if (perf_output_begin(&handle, event, header.size * |
| 588 | (top - base - skip))) |
Peter Zijlstra | e8d8a90 | 2016-03-18 17:31:27 +0100 | [diff] [blame] | 589 | goto unlock; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 590 | |
Alexander Shishkin | a09d31f4 | 2015-08-31 17:09:27 +0300 | [diff] [blame] | 591 | for (at = base; at < top; at++) { |
| 592 | /* Filter out any records that contain kernel addresses. */ |
| 593 | if (event->attr.exclude_kernel && |
| 594 | (kernel_ip(at->from) || kernel_ip(at->to))) |
| 595 | continue; |
| 596 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 597 | data.ip = at->from; |
| 598 | data.addr = at->to; |
| 599 | |
| 600 | perf_output_sample(&handle, &header, &data, event); |
| 601 | } |
| 602 | |
| 603 | perf_output_end(&handle); |
| 604 | |
| 605 | /* There's new data available. */ |
| 606 | event->hw.interrupts++; |
| 607 | event->pending_kill = POLL_IN; |
Peter Zijlstra | e8d8a90 | 2016-03-18 17:31:27 +0100 | [diff] [blame] | 608 | unlock: |
| 609 | rcu_read_unlock(); |
Stephane Eranian | b0b2072 | 2010-09-10 13:28:01 +0200 | [diff] [blame] | 610 | return 1; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 611 | } |
| 612 | |
Yan, Zheng | 9c964ef | 2015-05-06 15:33:51 -0400 | [diff] [blame] | 613 | static inline void intel_pmu_drain_pebs_buffer(void) |
| 614 | { |
| 615 | struct pt_regs regs; |
| 616 | |
| 617 | x86_pmu.drain_pebs(®s); |
| 618 | } |
| 619 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 620 | /* |
| 621 | * PEBS |
| 622 | */ |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 623 | struct event_constraint intel_core2_pebs_event_constraints[] = { |
Andi Kleen | af4bdcf | 2014-09-24 07:34:48 -0700 | [diff] [blame] | 624 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */ |
| 625 | INTEL_FLAGS_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */ |
| 626 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */ |
| 627 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */ |
| 628 | INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */ |
Peter Zijlstra | 517e634 | 2015-04-11 12:16:22 +0200 | [diff] [blame] | 629 | /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ |
| 630 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01), |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 631 | EVENT_CONSTRAINT_END |
| 632 | }; |
| 633 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 634 | struct event_constraint intel_atom_pebs_event_constraints[] = { |
Andi Kleen | af4bdcf | 2014-09-24 07:34:48 -0700 | [diff] [blame] | 635 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */ |
| 636 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */ |
| 637 | INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */ |
Peter Zijlstra | 517e634 | 2015-04-11 12:16:22 +0200 | [diff] [blame] | 638 | /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ |
| 639 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01), |
Stephane Eranian | 673d188 | 2015-12-03 21:03:10 +0100 | [diff] [blame] | 640 | /* Allow all events as PEBS with no flags */ |
| 641 | INTEL_ALL_EVENT_CONSTRAINT(0, 0x1), |
Stephane Eranian | 17e3162 | 2011-03-02 17:05:01 +0200 | [diff] [blame] | 642 | EVENT_CONSTRAINT_END |
| 643 | }; |
| 644 | |
Yan, Zheng | 1fa6418 | 2013-07-18 17:02:24 +0800 | [diff] [blame] | 645 | struct event_constraint intel_slm_pebs_event_constraints[] = { |
Kan Liang | 3363673 | 2015-01-12 17:42:21 +0000 | [diff] [blame] | 646 | /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ |
| 647 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x1), |
Andi Kleen | 86a0446 | 2014-08-11 21:27:10 +0200 | [diff] [blame] | 648 | /* Allow all events as PEBS with no flags */ |
| 649 | INTEL_ALL_EVENT_CONSTRAINT(0, 0x1), |
Yan, Zheng | 1fa6418 | 2013-07-18 17:02:24 +0800 | [diff] [blame] | 650 | EVENT_CONSTRAINT_END |
| 651 | }; |
| 652 | |
Kan Liang | 8b92c3a | 2016-04-15 00:42:47 -0700 | [diff] [blame] | 653 | struct event_constraint intel_glm_pebs_event_constraints[] = { |
| 654 | /* Allow all events as PEBS with no flags */ |
| 655 | INTEL_ALL_EVENT_CONSTRAINT(0, 0x1), |
| 656 | EVENT_CONSTRAINT_END |
| 657 | }; |
| 658 | |
Kan Liang | dd0b06b | 2017-07-12 09:44:23 -0400 | [diff] [blame] | 659 | struct event_constraint intel_glp_pebs_event_constraints[] = { |
| 660 | /* Allow all events as PEBS with no flags */ |
| 661 | INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), |
| 662 | EVENT_CONSTRAINT_END |
| 663 | }; |
| 664 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 665 | struct event_constraint intel_nehalem_pebs_event_constraints[] = { |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 666 | INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */ |
Andi Kleen | af4bdcf | 2014-09-24 07:34:48 -0700 | [diff] [blame] | 667 | INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */ |
| 668 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */ |
| 669 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INST_RETIRED.ANY */ |
Lin Ming | 7d5d02d | 2011-03-09 23:21:29 +0800 | [diff] [blame] | 670 | INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */ |
Andi Kleen | af4bdcf | 2014-09-24 07:34:48 -0700 | [diff] [blame] | 671 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */ |
| 672 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */ |
| 673 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */ |
| 674 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */ |
| 675 | INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */ |
| 676 | INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */ |
Peter Zijlstra | 517e634 | 2015-04-11 12:16:22 +0200 | [diff] [blame] | 677 | /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ |
| 678 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f), |
Stephane Eranian | 17e3162 | 2011-03-02 17:05:01 +0200 | [diff] [blame] | 679 | EVENT_CONSTRAINT_END |
| 680 | }; |
| 681 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 682 | struct event_constraint intel_westmere_pebs_event_constraints[] = { |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 683 | INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */ |
Andi Kleen | af4bdcf | 2014-09-24 07:34:48 -0700 | [diff] [blame] | 684 | INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */ |
| 685 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */ |
| 686 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INSTR_RETIRED.* */ |
Lin Ming | 7d5d02d | 2011-03-09 23:21:29 +0800 | [diff] [blame] | 687 | INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */ |
Andi Kleen | af4bdcf | 2014-09-24 07:34:48 -0700 | [diff] [blame] | 688 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */ |
| 689 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */ |
| 690 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */ |
| 691 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */ |
| 692 | INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */ |
| 693 | INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */ |
Peter Zijlstra | 517e634 | 2015-04-11 12:16:22 +0200 | [diff] [blame] | 694 | /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ |
| 695 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f), |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 696 | EVENT_CONSTRAINT_END |
| 697 | }; |
| 698 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 699 | struct event_constraint intel_snb_pebs_event_constraints[] = { |
Andi Kleen | 0dbc947 | 2014-09-24 07:34:47 -0700 | [diff] [blame] | 700 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */ |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 701 | INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */ |
Stephane Eranian | 9ad64c0 | 2013-01-24 16:10:34 +0100 | [diff] [blame] | 702 | INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */ |
Andi Kleen | 86a0446 | 2014-08-11 21:27:10 +0200 | [diff] [blame] | 703 | /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ |
| 704 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf), |
Maria Dimakopoulou | b63b4b4 | 2014-11-17 20:07:00 +0100 | [diff] [blame] | 705 | INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */ |
| 706 | INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ |
| 707 | INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */ |
| 708 | INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */ |
Andi Kleen | 86a0446 | 2014-08-11 21:27:10 +0200 | [diff] [blame] | 709 | /* Allow all events as PEBS with no flags */ |
| 710 | INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), |
Lin Ming | b06b3d4 | 2011-03-02 21:27:04 +0800 | [diff] [blame] | 711 | EVENT_CONSTRAINT_END |
| 712 | }; |
| 713 | |
Stephane Eranian | 20a36e3 | 2012-09-11 01:07:01 +0200 | [diff] [blame] | 714 | struct event_constraint intel_ivb_pebs_event_constraints[] = { |
Andi Kleen | 0dbc947 | 2014-09-24 07:34:47 -0700 | [diff] [blame] | 715 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */ |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 716 | INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */ |
Stephane Eranian | 9ad64c0 | 2013-01-24 16:10:34 +0100 | [diff] [blame] | 717 | INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */ |
Andi Kleen | 86a0446 | 2014-08-11 21:27:10 +0200 | [diff] [blame] | 718 | /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ |
| 719 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf), |
Andi Kleen | 7246976 | 2015-12-04 03:50:52 -0800 | [diff] [blame] | 720 | /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */ |
| 721 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2), |
Maria Dimakopoulou | b63b4b4 | 2014-11-17 20:07:00 +0100 | [diff] [blame] | 722 | INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */ |
| 723 | INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ |
| 724 | INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */ |
| 725 | INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */ |
Andi Kleen | 86a0446 | 2014-08-11 21:27:10 +0200 | [diff] [blame] | 726 | /* Allow all events as PEBS with no flags */ |
| 727 | INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), |
Stephane Eranian | 20a36e3 | 2012-09-11 01:07:01 +0200 | [diff] [blame] | 728 | EVENT_CONSTRAINT_END |
| 729 | }; |
| 730 | |
Andi Kleen | 3044318 | 2013-06-17 17:36:49 -0700 | [diff] [blame] | 731 | struct event_constraint intel_hsw_pebs_event_constraints[] = { |
Andi Kleen | 0dbc947 | 2014-09-24 07:34:47 -0700 | [diff] [blame] | 732 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */ |
Andi Kleen | 86a0446 | 2014-08-11 21:27:10 +0200 | [diff] [blame] | 733 | INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */ |
| 734 | /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ |
| 735 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf), |
Andi Kleen | 7246976 | 2015-12-04 03:50:52 -0800 | [diff] [blame] | 736 | /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */ |
| 737 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2), |
Andi Kleen | 86a0446 | 2014-08-11 21:27:10 +0200 | [diff] [blame] | 738 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */ |
Maria Dimakopoulou | b63b4b4 | 2014-11-17 20:07:00 +0100 | [diff] [blame] | 739 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */ |
| 740 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */ |
| 741 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */ |
| 742 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */ |
| 743 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */ |
| 744 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */ |
| 745 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */ |
| 746 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ |
| 747 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */ |
| 748 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */ |
Andi Kleen | 86a0446 | 2014-08-11 21:27:10 +0200 | [diff] [blame] | 749 | /* Allow all events as PEBS with no flags */ |
| 750 | INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), |
Andi Kleen | 3044318 | 2013-06-17 17:36:49 -0700 | [diff] [blame] | 751 | EVENT_CONSTRAINT_END |
| 752 | }; |
| 753 | |
Stephane Eranian | b3e6246 | 2016-03-03 20:50:42 +0100 | [diff] [blame] | 754 | struct event_constraint intel_bdw_pebs_event_constraints[] = { |
| 755 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */ |
| 756 | INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */ |
| 757 | /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ |
| 758 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf), |
| 759 | /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */ |
| 760 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2), |
| 761 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */ |
| 762 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */ |
| 763 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */ |
| 764 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */ |
| 765 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */ |
| 766 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */ |
| 767 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */ |
| 768 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */ |
| 769 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ |
| 770 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */ |
| 771 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */ |
| 772 | /* Allow all events as PEBS with no flags */ |
| 773 | INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), |
| 774 | EVENT_CONSTRAINT_END |
| 775 | }; |
| 776 | |
| 777 | |
Andi Kleen | 9a92e16 | 2015-05-10 12:22:44 -0700 | [diff] [blame] | 778 | struct event_constraint intel_skl_pebs_event_constraints[] = { |
| 779 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x1c0, 0x2), /* INST_RETIRED.PREC_DIST */ |
Andi Kleen | 7246976 | 2015-12-04 03:50:52 -0800 | [diff] [blame] | 780 | /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */ |
| 781 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2), |
Andi Kleen | 442f5c7 | 2015-12-04 03:50:32 -0800 | [diff] [blame] | 782 | /* INST_RETIRED.TOTAL_CYCLES_PS (inv=1, cmask=16) (cycles:p). */ |
| 783 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f), |
Andi Kleen | 9a92e16 | 2015-05-10 12:22:44 -0700 | [diff] [blame] | 784 | INTEL_PLD_CONSTRAINT(0x1cd, 0xf), /* MEM_TRANS_RETIRED.* */ |
| 785 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */ |
| 786 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */ |
| 787 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */ |
| 788 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x22d0, 0xf), /* MEM_INST_RETIRED.LOCK_STORES */ |
| 789 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */ |
| 790 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */ |
| 791 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */ |
| 792 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */ |
| 793 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_RETIRED.* */ |
| 794 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_L3_HIT_RETIRED.* */ |
| 795 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_L3_MISS_RETIRED.* */ |
| 796 | /* Allow all events as PEBS with no flags */ |
| 797 | INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), |
| 798 | EVENT_CONSTRAINT_END |
| 799 | }; |
| 800 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 801 | struct event_constraint *intel_pebs_constraints(struct perf_event *event) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 802 | { |
| 803 | struct event_constraint *c; |
| 804 | |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 805 | if (!event->attr.precise_ip) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 806 | return NULL; |
| 807 | |
| 808 | if (x86_pmu.pebs_constraints) { |
| 809 | for_each_event_constraint(c, x86_pmu.pebs_constraints) { |
Stephane Eranian | 9fac2cf | 2013-01-24 16:10:27 +0100 | [diff] [blame] | 810 | if ((event->hw.config & c->cmask) == c->code) { |
| 811 | event->hw.flags |= c->flags; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 812 | return c; |
Stephane Eranian | 9fac2cf | 2013-01-24 16:10:27 +0100 | [diff] [blame] | 813 | } |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 814 | } |
| 815 | } |
| 816 | |
| 817 | return &emptyconstraint; |
| 818 | } |
| 819 | |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 820 | /* |
| 821 | * We need the sched_task callback even for per-cpu events when we use |
| 822 | * the large interrupt threshold, such that we can provide PID and TID |
| 823 | * to PEBS samples. |
| 824 | */ |
| 825 | static inline bool pebs_needs_sched_cb(struct cpu_hw_events *cpuc) |
Yan, Zheng | 3569c0d | 2015-05-06 15:33:50 -0400 | [diff] [blame] | 826 | { |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 827 | return cpuc->n_pebs && (cpuc->n_pebs == cpuc->n_large_pebs); |
| 828 | } |
| 829 | |
Jiri Olsa | df6c3db | 2017-07-19 09:52:47 +0200 | [diff] [blame] | 830 | void intel_pmu_pebs_sched_task(struct perf_event_context *ctx, bool sched_in) |
| 831 | { |
| 832 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 833 | |
| 834 | if (!sched_in && pebs_needs_sched_cb(cpuc)) |
| 835 | intel_pmu_drain_pebs_buffer(); |
| 836 | } |
| 837 | |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 838 | static inline void pebs_update_threshold(struct cpu_hw_events *cpuc) |
| 839 | { |
| 840 | struct debug_store *ds = cpuc->ds; |
| 841 | u64 threshold; |
| 842 | |
| 843 | if (cpuc->n_pebs == cpuc->n_large_pebs) { |
| 844 | threshold = ds->pebs_absolute_maximum - |
| 845 | x86_pmu.max_pebs_events * x86_pmu.pebs_record_size; |
| 846 | } else { |
| 847 | threshold = ds->pebs_buffer_base + x86_pmu.pebs_record_size; |
| 848 | } |
| 849 | |
| 850 | ds->pebs_interrupt_threshold = threshold; |
| 851 | } |
| 852 | |
| 853 | static void |
| 854 | pebs_update_state(bool needed_cb, struct cpu_hw_events *cpuc, struct pmu *pmu) |
| 855 | { |
Jiri Olsa | b6a32f0 | 2016-08-18 11:09:52 +0200 | [diff] [blame] | 856 | /* |
| 857 | * Make sure we get updated with the first PEBS |
| 858 | * event. It will trigger also during removal, but |
| 859 | * that does not hurt: |
| 860 | */ |
| 861 | bool update = cpuc->n_pebs == 1; |
| 862 | |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 863 | if (needed_cb != pebs_needs_sched_cb(cpuc)) { |
| 864 | if (!needed_cb) |
| 865 | perf_sched_cb_inc(pmu); |
| 866 | else |
| 867 | perf_sched_cb_dec(pmu); |
| 868 | |
Jiri Olsa | b6a32f0 | 2016-08-18 11:09:52 +0200 | [diff] [blame] | 869 | update = true; |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 870 | } |
Jiri Olsa | b6a32f0 | 2016-08-18 11:09:52 +0200 | [diff] [blame] | 871 | |
| 872 | if (update) |
| 873 | pebs_update_threshold(cpuc); |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 874 | } |
| 875 | |
Peter Zijlstra | 68f7082 | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 876 | void intel_pmu_pebs_add(struct perf_event *event) |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 877 | { |
| 878 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 879 | struct hw_perf_event *hwc = &event->hw; |
| 880 | bool needed_cb = pebs_needs_sched_cb(cpuc); |
| 881 | |
| 882 | cpuc->n_pebs++; |
| 883 | if (hwc->flags & PERF_X86_EVENT_FREERUNNING) |
| 884 | cpuc->n_large_pebs++; |
| 885 | |
| 886 | pebs_update_state(needed_cb, cpuc, event->ctx->pmu); |
Yan, Zheng | 3569c0d | 2015-05-06 15:33:50 -0400 | [diff] [blame] | 887 | } |
| 888 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 889 | void intel_pmu_pebs_enable(struct perf_event *event) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 890 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 891 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 892 | struct hw_perf_event *hwc = &event->hw; |
Yan, Zheng | 851559e | 2015-05-06 15:33:47 -0400 | [diff] [blame] | 893 | struct debug_store *ds = cpuc->ds; |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 894 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 895 | hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT; |
| 896 | |
Peter Zijlstra | ad0e6cf | 2010-03-06 19:49:06 +0100 | [diff] [blame] | 897 | cpuc->pebs_enabled |= 1ULL << hwc->idx; |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 898 | |
| 899 | if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT) |
| 900 | cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32); |
Stephane Eranian | 9ad64c0 | 2013-01-24 16:10:34 +0100 | [diff] [blame] | 901 | else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST) |
| 902 | cpuc->pebs_enabled |= 1ULL << 63; |
Yan, Zheng | 851559e | 2015-05-06 15:33:47 -0400 | [diff] [blame] | 903 | |
Yan, Zheng | 3569c0d | 2015-05-06 15:33:50 -0400 | [diff] [blame] | 904 | /* |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 905 | * Use auto-reload if possible to save a MSR write in the PMI. |
| 906 | * This must be done in pmu::start(), because PERF_EVENT_IOC_PERIOD. |
Yan, Zheng | 3569c0d | 2015-05-06 15:33:50 -0400 | [diff] [blame] | 907 | */ |
Yan, Zheng | 851559e | 2015-05-06 15:33:47 -0400 | [diff] [blame] | 908 | if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) { |
| 909 | ds->pebs_event_reset[hwc->idx] = |
| 910 | (u64)(-hwc->sample_period) & x86_pmu.cntval_mask; |
Jiri Olsa | dc853e2 | 2017-07-14 18:35:51 +0200 | [diff] [blame] | 911 | } else { |
| 912 | ds->pebs_event_reset[hwc->idx] = 0; |
Yan, Zheng | 851559e | 2015-05-06 15:33:47 -0400 | [diff] [blame] | 913 | } |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 914 | } |
Yan, Zheng | 3569c0d | 2015-05-06 15:33:50 -0400 | [diff] [blame] | 915 | |
Peter Zijlstra | 68f7082 | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 916 | void intel_pmu_pebs_del(struct perf_event *event) |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 917 | { |
| 918 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 919 | struct hw_perf_event *hwc = &event->hw; |
| 920 | bool needed_cb = pebs_needs_sched_cb(cpuc); |
| 921 | |
| 922 | cpuc->n_pebs--; |
| 923 | if (hwc->flags & PERF_X86_EVENT_FREERUNNING) |
| 924 | cpuc->n_large_pebs--; |
| 925 | |
| 926 | pebs_update_state(needed_cb, cpuc, event->ctx->pmu); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 927 | } |
| 928 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 929 | void intel_pmu_pebs_disable(struct perf_event *event) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 930 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 931 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 932 | struct hw_perf_event *hwc = &event->hw; |
Liang, Kan | 2a853e1 | 2015-07-03 20:08:27 +0000 | [diff] [blame] | 933 | |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 934 | if (cpuc->n_pebs == cpuc->n_large_pebs) |
Liang, Kan | 2a853e1 | 2015-07-03 20:08:27 +0000 | [diff] [blame] | 935 | intel_pmu_drain_pebs_buffer(); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 936 | |
Peter Zijlstra | ad0e6cf | 2010-03-06 19:49:06 +0100 | [diff] [blame] | 937 | cpuc->pebs_enabled &= ~(1ULL << hwc->idx); |
Stephane Eranian | 983433b | 2013-06-21 16:20:41 +0200 | [diff] [blame] | 938 | |
Peter Zijlstra | b371b59 | 2015-05-21 10:57:13 +0200 | [diff] [blame] | 939 | if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT) |
Stephane Eranian | 983433b | 2013-06-21 16:20:41 +0200 | [diff] [blame] | 940 | cpuc->pebs_enabled &= ~(1ULL << (hwc->idx + 32)); |
Peter Zijlstra | b371b59 | 2015-05-21 10:57:13 +0200 | [diff] [blame] | 941 | else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST) |
Stephane Eranian | 983433b | 2013-06-21 16:20:41 +0200 | [diff] [blame] | 942 | cpuc->pebs_enabled &= ~(1ULL << 63); |
| 943 | |
Peter Zijlstra | 4807e3d | 2010-03-06 13:47:07 +0100 | [diff] [blame] | 944 | if (cpuc->enabled) |
Peter Zijlstra | ad0e6cf | 2010-03-06 19:49:06 +0100 | [diff] [blame] | 945 | wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 946 | |
| 947 | hwc->config |= ARCH_PERFMON_EVENTSEL_INT; |
| 948 | } |
| 949 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 950 | void intel_pmu_pebs_enable_all(void) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 951 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 952 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 953 | |
| 954 | if (cpuc->pebs_enabled) |
| 955 | wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled); |
| 956 | } |
| 957 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 958 | void intel_pmu_pebs_disable_all(void) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 959 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 960 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 961 | |
| 962 | if (cpuc->pebs_enabled) |
| 963 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); |
| 964 | } |
| 965 | |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 966 | static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs) |
| 967 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 968 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 969 | unsigned long from = cpuc->lbr_entries[0].from; |
| 970 | unsigned long old_to, to = cpuc->lbr_entries[0].to; |
| 971 | unsigned long ip = regs->ip; |
Peter Zijlstra | 57d1c0c | 2011-10-07 13:36:40 +0200 | [diff] [blame] | 972 | int is_64bit = 0; |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 973 | void *kaddr; |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 974 | int size; |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 975 | |
Peter Zijlstra | 8db909a | 2010-03-03 17:07:40 +0100 | [diff] [blame] | 976 | /* |
| 977 | * We don't need to fixup if the PEBS assist is fault like |
| 978 | */ |
| 979 | if (!x86_pmu.intel_cap.pebs_trap) |
| 980 | return 1; |
| 981 | |
Peter Zijlstra | a562b18 | 2010-03-05 16:29:14 +0100 | [diff] [blame] | 982 | /* |
| 983 | * No LBR entry, no basic block, no rewinding |
| 984 | */ |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 985 | if (!cpuc->lbr_stack.nr || !from || !to) |
| 986 | return 0; |
| 987 | |
Peter Zijlstra | a562b18 | 2010-03-05 16:29:14 +0100 | [diff] [blame] | 988 | /* |
| 989 | * Basic blocks should never cross user/kernel boundaries |
| 990 | */ |
| 991 | if (kernel_ip(ip) != kernel_ip(to)) |
| 992 | return 0; |
| 993 | |
| 994 | /* |
| 995 | * unsigned math, either ip is before the start (impossible) or |
| 996 | * the basic block is larger than 1 page (sanity) |
| 997 | */ |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 998 | if ((ip - to) > PEBS_FIXUP_SIZE) |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 999 | return 0; |
| 1000 | |
| 1001 | /* |
| 1002 | * We sampled a branch insn, rewind using the LBR stack |
| 1003 | */ |
| 1004 | if (ip == to) { |
Peter Zijlstra | d07bdfd | 2012-07-10 09:42:15 +0200 | [diff] [blame] | 1005 | set_linear_ip(regs, from); |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1006 | return 1; |
| 1007 | } |
| 1008 | |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 1009 | size = ip - to; |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 1010 | if (!kernel_ip(ip)) { |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 1011 | int bytes; |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 1012 | u8 *buf = this_cpu_read(insn_buffer); |
| 1013 | |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 1014 | /* 'size' must fit our buffer, see above */ |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 1015 | bytes = copy_from_user_nmi(buf, (void __user *)to, size); |
Peter Zijlstra | 0a19684 | 2013-10-30 21:16:22 +0100 | [diff] [blame] | 1016 | if (bytes != 0) |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 1017 | return 0; |
| 1018 | |
| 1019 | kaddr = buf; |
| 1020 | } else { |
| 1021 | kaddr = (void *)to; |
| 1022 | } |
| 1023 | |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1024 | do { |
| 1025 | struct insn insn; |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1026 | |
| 1027 | old_to = to; |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1028 | |
Peter Zijlstra | 57d1c0c | 2011-10-07 13:36:40 +0200 | [diff] [blame] | 1029 | #ifdef CONFIG_X86_64 |
| 1030 | is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32); |
| 1031 | #endif |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 1032 | insn_init(&insn, kaddr, size, is_64bit); |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1033 | insn_get_length(&insn); |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 1034 | /* |
| 1035 | * Make sure there was not a problem decoding the |
| 1036 | * instruction and getting the length. This is |
| 1037 | * doubly important because we have an infinite |
| 1038 | * loop if insn.length=0. |
| 1039 | */ |
| 1040 | if (!insn.length) |
| 1041 | break; |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 1042 | |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1043 | to += insn.length; |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 1044 | kaddr += insn.length; |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 1045 | size -= insn.length; |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1046 | } while (to < ip); |
| 1047 | |
| 1048 | if (to == ip) { |
Peter Zijlstra | d07bdfd | 2012-07-10 09:42:15 +0200 | [diff] [blame] | 1049 | set_linear_ip(regs, old_to); |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1050 | return 1; |
| 1051 | } |
| 1052 | |
Peter Zijlstra | a562b18 | 2010-03-05 16:29:14 +0100 | [diff] [blame] | 1053 | /* |
| 1054 | * Even though we decoded the basic block, the instruction stream |
| 1055 | * never matched the given IP, either the TO or the IP got corrupted. |
| 1056 | */ |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1057 | return 0; |
| 1058 | } |
| 1059 | |
Andi Kleen | 2f7ebf2 | 2015-05-10 12:22:40 -0700 | [diff] [blame] | 1060 | static inline u64 intel_hsw_weight(struct pebs_record_skl *pebs) |
Andi Kleen | 748e86a | 2013-09-05 20:37:39 -0700 | [diff] [blame] | 1061 | { |
| 1062 | if (pebs->tsx_tuning) { |
| 1063 | union hsw_tsx_tuning tsx = { .value = pebs->tsx_tuning }; |
| 1064 | return tsx.cycles_last_block; |
| 1065 | } |
| 1066 | return 0; |
| 1067 | } |
| 1068 | |
Andi Kleen | 2f7ebf2 | 2015-05-10 12:22:40 -0700 | [diff] [blame] | 1069 | static inline u64 intel_hsw_transaction(struct pebs_record_skl *pebs) |
Andi Kleen | a405bad | 2013-09-20 07:40:40 -0700 | [diff] [blame] | 1070 | { |
| 1071 | u64 txn = (pebs->tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32; |
| 1072 | |
| 1073 | /* For RTM XABORTs also log the abort code from AX */ |
| 1074 | if ((txn & PERF_TXN_TRANSACTION) && (pebs->ax & 1)) |
| 1075 | txn |= ((pebs->ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT; |
| 1076 | return txn; |
| 1077 | } |
| 1078 | |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1079 | static void setup_pebs_sample_data(struct perf_event *event, |
| 1080 | struct pt_regs *iregs, void *__pebs, |
| 1081 | struct perf_sample_data *data, |
| 1082 | struct pt_regs *regs) |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1083 | { |
Stephane Eranian | c8aab2e | 2014-08-11 21:27:13 +0200 | [diff] [blame] | 1084 | #define PERF_X86_EVENT_PEBS_HSW_PREC \ |
| 1085 | (PERF_X86_EVENT_PEBS_ST_HSW | \ |
| 1086 | PERF_X86_EVENT_PEBS_LD_HSW | \ |
| 1087 | PERF_X86_EVENT_PEBS_NA_HSW) |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1088 | /* |
Peter Zijlstra | d2beea4 | 2013-09-12 13:00:47 +0200 | [diff] [blame] | 1089 | * We cast to the biggest pebs_record but are careful not to |
| 1090 | * unconditionally access the 'extra' entries. |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1091 | */ |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 1092 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Andi Kleen | 2f7ebf2 | 2015-05-10 12:22:40 -0700 | [diff] [blame] | 1093 | struct pebs_record_skl *pebs = __pebs; |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 1094 | u64 sample_type; |
Stephane Eranian | c8aab2e | 2014-08-11 21:27:13 +0200 | [diff] [blame] | 1095 | int fll, fst, dsrc; |
| 1096 | int fl = event->hw.flags; |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1097 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1098 | if (pebs == NULL) |
| 1099 | return; |
| 1100 | |
Stephane Eranian | c8aab2e | 2014-08-11 21:27:13 +0200 | [diff] [blame] | 1101 | sample_type = event->attr.sample_type; |
| 1102 | dsrc = sample_type & PERF_SAMPLE_DATA_SRC; |
| 1103 | |
| 1104 | fll = fl & PERF_X86_EVENT_PEBS_LDLAT; |
| 1105 | fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC); |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 1106 | |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1107 | perf_sample_data_init(data, 0, event->hw.last_period); |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1108 | |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1109 | data->period = event->hw.last_period; |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 1110 | |
| 1111 | /* |
Stephane Eranian | c8aab2e | 2014-08-11 21:27:13 +0200 | [diff] [blame] | 1112 | * Use latency for weight (only avail with PEBS-LL) |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 1113 | */ |
Stephane Eranian | c8aab2e | 2014-08-11 21:27:13 +0200 | [diff] [blame] | 1114 | if (fll && (sample_type & PERF_SAMPLE_WEIGHT)) |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1115 | data->weight = pebs->lat; |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 1116 | |
Stephane Eranian | c8aab2e | 2014-08-11 21:27:13 +0200 | [diff] [blame] | 1117 | /* |
| 1118 | * data.data_src encodes the data source |
| 1119 | */ |
| 1120 | if (dsrc) { |
| 1121 | u64 val = PERF_MEM_NA; |
| 1122 | if (fll) |
| 1123 | val = load_latency_data(pebs->dse); |
| 1124 | else if (fst && (fl & PERF_X86_EVENT_PEBS_HSW_PREC)) |
| 1125 | val = precise_datala_hsw(event, pebs->dse); |
| 1126 | else if (fst) |
| 1127 | val = precise_store_data(pebs->dse); |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1128 | data->data_src.val = val; |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 1129 | } |
| 1130 | |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1131 | /* |
Peter Zijlstra | b800058 | 2016-11-17 18:17:31 +0100 | [diff] [blame] | 1132 | * We use the interrupt regs as a base because the PEBS record does not |
| 1133 | * contain a full regs set, specifically it seems to lack segment |
| 1134 | * descriptors, which get used by things like user_mode(). |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1135 | * |
Peter Zijlstra | b800058 | 2016-11-17 18:17:31 +0100 | [diff] [blame] | 1136 | * In the simple case fix up only the IP for PERF_SAMPLE_IP. |
| 1137 | * |
| 1138 | * We must however always use BP,SP from iregs for the unwinder to stay |
| 1139 | * sane; the record BP,SP can point into thin air when the record is |
| 1140 | * from a previous PMI context or an (I)RET happend between the record |
| 1141 | * and PMI. |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1142 | */ |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1143 | *regs = *iregs; |
| 1144 | regs->flags = pebs->flags; |
| 1145 | set_linear_ip(regs, pebs->ip); |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1146 | |
Stephane Eranian | aea4855 | 2014-09-24 13:48:38 +0200 | [diff] [blame] | 1147 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1148 | regs->ax = pebs->ax; |
| 1149 | regs->bx = pebs->bx; |
| 1150 | regs->cx = pebs->cx; |
| 1151 | regs->dx = pebs->dx; |
| 1152 | regs->si = pebs->si; |
| 1153 | regs->di = pebs->di; |
Stephane Eranian | aea4855 | 2014-09-24 13:48:38 +0200 | [diff] [blame] | 1154 | |
Peter Zijlstra | b800058 | 2016-11-17 18:17:31 +0100 | [diff] [blame] | 1155 | /* |
| 1156 | * Per the above; only set BP,SP if we don't need callchains. |
| 1157 | * |
| 1158 | * XXX: does this make sense? |
| 1159 | */ |
| 1160 | if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) { |
| 1161 | regs->bp = pebs->bp; |
| 1162 | regs->sp = pebs->sp; |
| 1163 | } |
| 1164 | |
| 1165 | /* |
| 1166 | * Preserve PERF_EFLAGS_VM from set_linear_ip(). |
| 1167 | */ |
| 1168 | regs->flags = pebs->flags | (regs->flags & PERF_EFLAGS_VM); |
Stephane Eranian | aea4855 | 2014-09-24 13:48:38 +0200 | [diff] [blame] | 1169 | #ifndef CONFIG_X86_32 |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1170 | regs->r8 = pebs->r8; |
| 1171 | regs->r9 = pebs->r9; |
| 1172 | regs->r10 = pebs->r10; |
| 1173 | regs->r11 = pebs->r11; |
| 1174 | regs->r12 = pebs->r12; |
| 1175 | regs->r13 = pebs->r13; |
| 1176 | regs->r14 = pebs->r14; |
| 1177 | regs->r15 = pebs->r15; |
Stephane Eranian | aea4855 | 2014-09-24 13:48:38 +0200 | [diff] [blame] | 1178 | #endif |
| 1179 | } |
| 1180 | |
Andi Kleen | 130768b | 2013-06-17 17:36:47 -0700 | [diff] [blame] | 1181 | if (event->attr.precise_ip > 1 && x86_pmu.intel_cap.pebs_format >= 2) { |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1182 | regs->ip = pebs->real_ip; |
| 1183 | regs->flags |= PERF_EFLAGS_EXACT; |
| 1184 | } else if (event->attr.precise_ip > 1 && intel_pmu_pebs_fixup_ip(regs)) |
| 1185 | regs->flags |= PERF_EFLAGS_EXACT; |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1186 | else |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1187 | regs->flags &= ~PERF_EFLAGS_EXACT; |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1188 | |
Kan Liang | fc7ce9c | 2017-08-28 20:52:49 -0400 | [diff] [blame] | 1189 | if ((sample_type & (PERF_SAMPLE_ADDR | PERF_SAMPLE_PHYS_ADDR)) && |
Peter Zijlstra | d2beea4 | 2013-09-12 13:00:47 +0200 | [diff] [blame] | 1190 | x86_pmu.intel_cap.pebs_format >= 1) |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1191 | data->addr = pebs->dla; |
Andi Kleen | f9134f3 | 2013-06-17 17:36:52 -0700 | [diff] [blame] | 1192 | |
Andi Kleen | a405bad | 2013-09-20 07:40:40 -0700 | [diff] [blame] | 1193 | if (x86_pmu.intel_cap.pebs_format >= 2) { |
| 1194 | /* Only set the TSX weight when no memory weight. */ |
Stephane Eranian | c8aab2e | 2014-08-11 21:27:13 +0200 | [diff] [blame] | 1195 | if ((sample_type & PERF_SAMPLE_WEIGHT) && !fll) |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1196 | data->weight = intel_hsw_weight(pebs); |
Andi Kleen | a405bad | 2013-09-20 07:40:40 -0700 | [diff] [blame] | 1197 | |
Stephane Eranian | c8aab2e | 2014-08-11 21:27:13 +0200 | [diff] [blame] | 1198 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1199 | data->txn = intel_hsw_transaction(pebs); |
Andi Kleen | a405bad | 2013-09-20 07:40:40 -0700 | [diff] [blame] | 1200 | } |
Andi Kleen | 748e86a | 2013-09-05 20:37:39 -0700 | [diff] [blame] | 1201 | |
Andi Kleen | 2f7ebf2 | 2015-05-10 12:22:40 -0700 | [diff] [blame] | 1202 | /* |
| 1203 | * v3 supplies an accurate time stamp, so we use that |
| 1204 | * for the time stamp. |
| 1205 | * |
| 1206 | * We can only do this for the default trace clock. |
| 1207 | */ |
| 1208 | if (x86_pmu.intel_cap.pebs_format >= 3 && |
| 1209 | event->attr.use_clockid == 0) |
| 1210 | data->time = native_sched_clock_from_tsc(pebs->tsc); |
| 1211 | |
Stephane Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 1212 | if (has_branch_stack(event)) |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1213 | data->br_stack = &cpuc->lbr_stack; |
| 1214 | } |
| 1215 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1216 | static inline void * |
| 1217 | get_next_pebs_record_by_bit(void *base, void *top, int bit) |
| 1218 | { |
| 1219 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 1220 | void *at; |
| 1221 | u64 pebs_status; |
| 1222 | |
Stephane Eranian | 1424a09 | 2015-12-03 23:33:18 +0100 | [diff] [blame] | 1223 | /* |
| 1224 | * fmt0 does not have a status bitfield (does not use |
| 1225 | * perf_record_nhm format) |
| 1226 | */ |
| 1227 | if (x86_pmu.intel_cap.pebs_format < 1) |
| 1228 | return base; |
| 1229 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1230 | if (base == NULL) |
| 1231 | return NULL; |
| 1232 | |
| 1233 | for (at = base; at < top; at += x86_pmu.pebs_record_size) { |
| 1234 | struct pebs_record_nhm *p = at; |
| 1235 | |
| 1236 | if (test_bit(bit, (unsigned long *)&p->status)) { |
Peter Zijlstra | a3d8654 | 2015-05-12 15:18:18 +0200 | [diff] [blame] | 1237 | /* PEBS v3 has accurate status bits */ |
| 1238 | if (x86_pmu.intel_cap.pebs_format >= 3) |
| 1239 | return at; |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1240 | |
| 1241 | if (p->status == (1 << bit)) |
| 1242 | return at; |
| 1243 | |
| 1244 | /* clear non-PEBS bit and re-check */ |
| 1245 | pebs_status = p->status & cpuc->pebs_enabled; |
Kan Liang | fd583ad | 2017-04-04 15:14:06 -0400 | [diff] [blame] | 1246 | pebs_status &= PEBS_COUNTER_MASK; |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1247 | if (pebs_status == (1 << bit)) |
| 1248 | return at; |
| 1249 | } |
| 1250 | } |
| 1251 | return NULL; |
| 1252 | } |
| 1253 | |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1254 | static void __intel_pmu_pebs_event(struct perf_event *event, |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1255 | struct pt_regs *iregs, |
| 1256 | void *base, void *top, |
| 1257 | int bit, int count) |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1258 | { |
| 1259 | struct perf_sample_data data; |
| 1260 | struct pt_regs regs; |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1261 | void *at = get_next_pebs_record_by_bit(base, top, bit); |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1262 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1263 | if (!intel_pmu_save_and_restart(event) && |
| 1264 | !(event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)) |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1265 | return; |
| 1266 | |
Peter Zijlstra | a3d8654 | 2015-05-12 15:18:18 +0200 | [diff] [blame] | 1267 | while (count > 1) { |
| 1268 | setup_pebs_sample_data(event, iregs, at, &data, ®s); |
| 1269 | perf_event_output(event, &data, ®s); |
| 1270 | at += x86_pmu.pebs_record_size; |
| 1271 | at = get_next_pebs_record_by_bit(at, top, bit); |
| 1272 | count--; |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1273 | } |
Stephane Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 1274 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1275 | setup_pebs_sample_data(event, iregs, at, &data, ®s); |
| 1276 | |
| 1277 | /* |
| 1278 | * All but the last records are processed. |
| 1279 | * The last one is left to be able to call the overflow handler. |
| 1280 | */ |
| 1281 | if (perf_event_overflow(event, &data, ®s)) { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1282 | x86_pmu_stop(event, 0); |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1283 | return; |
| 1284 | } |
| 1285 | |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1286 | } |
| 1287 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1288 | static void intel_pmu_drain_pebs_core(struct pt_regs *iregs) |
| 1289 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 1290 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1291 | struct debug_store *ds = cpuc->ds; |
| 1292 | struct perf_event *event = cpuc->events[0]; /* PMC0 only */ |
| 1293 | struct pebs_record_core *at, *top; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1294 | int n; |
| 1295 | |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 1296 | if (!x86_pmu.pebs_active) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1297 | return; |
| 1298 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1299 | at = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base; |
| 1300 | top = (struct pebs_record_core *)(unsigned long)ds->pebs_index; |
| 1301 | |
Peter Zijlstra | d80c750 | 2010-03-09 11:41:02 +0100 | [diff] [blame] | 1302 | /* |
| 1303 | * Whatever else happens, drain the thing |
| 1304 | */ |
| 1305 | ds->pebs_index = ds->pebs_buffer_base; |
| 1306 | |
| 1307 | if (!test_bit(0, cpuc->active_mask)) |
Peter Zijlstra | 8f4aebd | 2010-03-06 13:26:11 +0100 | [diff] [blame] | 1308 | return; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1309 | |
Peter Zijlstra | d80c750 | 2010-03-09 11:41:02 +0100 | [diff] [blame] | 1310 | WARN_ON_ONCE(!event); |
| 1311 | |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1312 | if (!event->attr.precise_ip) |
Peter Zijlstra | d80c750 | 2010-03-09 11:41:02 +0100 | [diff] [blame] | 1313 | return; |
| 1314 | |
Stephane Eranian | 1424a09 | 2015-12-03 23:33:18 +0100 | [diff] [blame] | 1315 | n = top - at; |
Peter Zijlstra | d80c750 | 2010-03-09 11:41:02 +0100 | [diff] [blame] | 1316 | if (n <= 0) |
| 1317 | return; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1318 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1319 | __intel_pmu_pebs_event(event, iregs, at, top, 0, n); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1320 | } |
| 1321 | |
Peter Zijlstra | d2beea4 | 2013-09-12 13:00:47 +0200 | [diff] [blame] | 1322 | static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1323 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 1324 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1325 | struct debug_store *ds = cpuc->ds; |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1326 | struct perf_event *event; |
| 1327 | void *base, *at, *top; |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1328 | short counts[MAX_PEBS_EVENTS] = {}; |
Kan Liang | f38b0db | 2015-05-10 15:13:14 -0400 | [diff] [blame] | 1329 | short error[MAX_PEBS_EVENTS] = {}; |
Peter Zijlstra | a3d8654 | 2015-05-12 15:18:18 +0200 | [diff] [blame] | 1330 | int bit, i; |
Peter Zijlstra | d2beea4 | 2013-09-12 13:00:47 +0200 | [diff] [blame] | 1331 | |
| 1332 | if (!x86_pmu.pebs_active) |
| 1333 | return; |
| 1334 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1335 | base = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base; |
Peter Zijlstra | d2beea4 | 2013-09-12 13:00:47 +0200 | [diff] [blame] | 1336 | top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1337 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1338 | ds->pebs_index = ds->pebs_buffer_base; |
| 1339 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1340 | if (unlikely(base >= top)) |
Peter Zijlstra | d2beea4 | 2013-09-12 13:00:47 +0200 | [diff] [blame] | 1341 | return; |
| 1342 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1343 | for (at = base; at < top; at += x86_pmu.pebs_record_size) { |
Andi Kleen | 130768b | 2013-06-17 17:36:47 -0700 | [diff] [blame] | 1344 | struct pebs_record_nhm *p = at; |
Peter Zijlstra | 75f8085 | 2015-07-15 14:35:46 +0200 | [diff] [blame] | 1345 | u64 pebs_status; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1346 | |
Peter Zijlstra | 8ef9b84 | 2016-09-07 14:42:55 +0200 | [diff] [blame] | 1347 | pebs_status = p->status & cpuc->pebs_enabled; |
| 1348 | pebs_status &= (1ULL << x86_pmu.max_pebs_events) - 1; |
| 1349 | |
| 1350 | /* PEBS v3 has more accurate status bits */ |
Peter Zijlstra | a3d8654 | 2015-05-12 15:18:18 +0200 | [diff] [blame] | 1351 | if (x86_pmu.intel_cap.pebs_format >= 3) { |
Peter Zijlstra | 8ef9b84 | 2016-09-07 14:42:55 +0200 | [diff] [blame] | 1352 | for_each_set_bit(bit, (unsigned long *)&pebs_status, |
| 1353 | x86_pmu.max_pebs_events) |
Peter Zijlstra | a3d8654 | 2015-05-12 15:18:18 +0200 | [diff] [blame] | 1354 | counts[bit]++; |
| 1355 | |
| 1356 | continue; |
| 1357 | } |
| 1358 | |
Andi Kleen | 01330d7 | 2015-12-03 13:22:20 -0800 | [diff] [blame] | 1359 | /* |
| 1360 | * On some CPUs the PEBS status can be zero when PEBS is |
| 1361 | * racing with clearing of GLOBAL_STATUS. |
| 1362 | * |
| 1363 | * Normally we would drop that record, but in the |
| 1364 | * case when there is only a single active PEBS event |
| 1365 | * we can assume it's for that event. |
| 1366 | */ |
| 1367 | if (!pebs_status && cpuc->pebs_enabled && |
| 1368 | !(cpuc->pebs_enabled & (cpuc->pebs_enabled-1))) |
| 1369 | pebs_status = cpuc->pebs_enabled; |
| 1370 | |
Peter Zijlstra | 75f8085 | 2015-07-15 14:35:46 +0200 | [diff] [blame] | 1371 | bit = find_first_bit((unsigned long *)&pebs_status, |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1372 | x86_pmu.max_pebs_events); |
Andi Kleen | 957ea1f | 2015-12-03 13:22:19 -0800 | [diff] [blame] | 1373 | if (bit >= x86_pmu.max_pebs_events) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1374 | continue; |
Peter Zijlstra | 75f8085 | 2015-07-15 14:35:46 +0200 | [diff] [blame] | 1375 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1376 | /* |
| 1377 | * The PEBS hardware does not deal well with the situation |
| 1378 | * when events happen near to each other and multiple bits |
| 1379 | * are set. But it should happen rarely. |
| 1380 | * |
| 1381 | * If these events include one PEBS and multiple non-PEBS |
| 1382 | * events, it doesn't impact PEBS record. The record will |
| 1383 | * be handled normally. (slow path) |
| 1384 | * |
| 1385 | * If these events include two or more PEBS events, the |
| 1386 | * records for the events can be collapsed into a single |
| 1387 | * one, and it's not possible to reconstruct all events |
| 1388 | * that caused the PEBS record. It's called collision. |
| 1389 | * If collision happened, the record will be dropped. |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1390 | */ |
Peter Zijlstra | 75f8085 | 2015-07-15 14:35:46 +0200 | [diff] [blame] | 1391 | if (p->status != (1ULL << bit)) { |
| 1392 | for_each_set_bit(i, (unsigned long *)&pebs_status, |
| 1393 | x86_pmu.max_pebs_events) |
| 1394 | error[i]++; |
| 1395 | continue; |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1396 | } |
Peter Zijlstra | 75f8085 | 2015-07-15 14:35:46 +0200 | [diff] [blame] | 1397 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1398 | counts[bit]++; |
| 1399 | } |
| 1400 | |
| 1401 | for (bit = 0; bit < x86_pmu.max_pebs_events; bit++) { |
Kan Liang | f38b0db | 2015-05-10 15:13:14 -0400 | [diff] [blame] | 1402 | if ((counts[bit] == 0) && (error[bit] == 0)) |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1403 | continue; |
Peter Zijlstra | 75f8085 | 2015-07-15 14:35:46 +0200 | [diff] [blame] | 1404 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1405 | event = cpuc->events[bit]; |
Peter Zijlstra | 8ef9b84 | 2016-09-07 14:42:55 +0200 | [diff] [blame] | 1406 | if (WARN_ON_ONCE(!event)) |
| 1407 | continue; |
| 1408 | |
| 1409 | if (WARN_ON_ONCE(!event->attr.precise_ip)) |
| 1410 | continue; |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1411 | |
Kan Liang | f38b0db | 2015-05-10 15:13:14 -0400 | [diff] [blame] | 1412 | /* log dropped samples number */ |
Jiri Olsa | 475113d | 2016-12-28 14:31:03 +0100 | [diff] [blame] | 1413 | if (error[bit]) { |
Kan Liang | f38b0db | 2015-05-10 15:13:14 -0400 | [diff] [blame] | 1414 | perf_log_lost_samples(event, error[bit]); |
| 1415 | |
Jiri Olsa | 475113d | 2016-12-28 14:31:03 +0100 | [diff] [blame] | 1416 | if (perf_event_account_interrupt(event)) |
| 1417 | x86_pmu_stop(event, 0); |
| 1418 | } |
| 1419 | |
Kan Liang | f38b0db | 2015-05-10 15:13:14 -0400 | [diff] [blame] | 1420 | if (counts[bit]) { |
| 1421 | __intel_pmu_pebs_event(event, iregs, base, |
| 1422 | top, bit, counts[bit]); |
| 1423 | } |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1424 | } |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | /* |
| 1428 | * BTS, PEBS probe and setup |
| 1429 | */ |
| 1430 | |
Mathias Krause | 066ce64 | 2014-08-26 18:49:45 +0200 | [diff] [blame] | 1431 | void __init intel_ds_init(void) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1432 | { |
| 1433 | /* |
| 1434 | * No support for 32bit formats |
| 1435 | */ |
| 1436 | if (!boot_cpu_has(X86_FEATURE_DTES64)) |
| 1437 | return; |
| 1438 | |
| 1439 | x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS); |
| 1440 | x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS); |
Jiri Olsa | e72daf3 | 2016-03-01 20:03:52 +0100 | [diff] [blame] | 1441 | x86_pmu.pebs_buffer_size = PEBS_BUFFER_SIZE; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1442 | if (x86_pmu.pebs) { |
Peter Zijlstra | 8db909a | 2010-03-03 17:07:40 +0100 | [diff] [blame] | 1443 | char pebs_type = x86_pmu.intel_cap.pebs_trap ? '+' : '-'; |
| 1444 | int format = x86_pmu.intel_cap.pebs_format; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1445 | |
| 1446 | switch (format) { |
| 1447 | case 0: |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 1448 | pr_cont("PEBS fmt0%c, ", pebs_type); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1449 | x86_pmu.pebs_record_size = sizeof(struct pebs_record_core); |
Jiri Olsa | e72daf3 | 2016-03-01 20:03:52 +0100 | [diff] [blame] | 1450 | /* |
| 1451 | * Using >PAGE_SIZE buffers makes the WRMSR to |
| 1452 | * PERF_GLOBAL_CTRL in intel_pmu_enable_all() |
| 1453 | * mysteriously hang on Core2. |
| 1454 | * |
| 1455 | * As a workaround, we don't do this. |
| 1456 | */ |
| 1457 | x86_pmu.pebs_buffer_size = PAGE_SIZE; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1458 | x86_pmu.drain_pebs = intel_pmu_drain_pebs_core; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1459 | break; |
| 1460 | |
| 1461 | case 1: |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 1462 | pr_cont("PEBS fmt1%c, ", pebs_type); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1463 | x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm); |
| 1464 | x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1465 | break; |
| 1466 | |
Andi Kleen | 130768b | 2013-06-17 17:36:47 -0700 | [diff] [blame] | 1467 | case 2: |
| 1468 | pr_cont("PEBS fmt2%c, ", pebs_type); |
| 1469 | x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw); |
Peter Zijlstra | d2beea4 | 2013-09-12 13:00:47 +0200 | [diff] [blame] | 1470 | x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm; |
Andi Kleen | 130768b | 2013-06-17 17:36:47 -0700 | [diff] [blame] | 1471 | break; |
| 1472 | |
Andi Kleen | 2f7ebf2 | 2015-05-10 12:22:40 -0700 | [diff] [blame] | 1473 | case 3: |
| 1474 | pr_cont("PEBS fmt3%c, ", pebs_type); |
| 1475 | x86_pmu.pebs_record_size = |
| 1476 | sizeof(struct pebs_record_skl); |
| 1477 | x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm; |
Andi Kleen | a7b58d2 | 2015-05-27 21:13:14 -0700 | [diff] [blame] | 1478 | x86_pmu.free_running_flags |= PERF_SAMPLE_TIME; |
Andi Kleen | 2f7ebf2 | 2015-05-10 12:22:40 -0700 | [diff] [blame] | 1479 | break; |
| 1480 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1481 | default: |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 1482 | pr_cont("no PEBS fmt%d%c, ", format, pebs_type); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1483 | x86_pmu.pebs = 0; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1484 | } |
| 1485 | } |
| 1486 | } |
Stephane Eranian | 1d9d863 | 2013-03-15 14:26:07 +0100 | [diff] [blame] | 1487 | |
| 1488 | void perf_restore_debug_store(void) |
| 1489 | { |
Linus Torvalds | 2a6e06b | 2013-03-17 15:44:43 -0700 | [diff] [blame] | 1490 | struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds); |
| 1491 | |
Stephane Eranian | 1d9d863 | 2013-03-15 14:26:07 +0100 | [diff] [blame] | 1492 | if (!x86_pmu.bts && !x86_pmu.pebs) |
| 1493 | return; |
| 1494 | |
Linus Torvalds | 2a6e06b | 2013-03-17 15:44:43 -0700 | [diff] [blame] | 1495 | wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds); |
Stephane Eranian | 1d9d863 | 2013-03-15 14:26:07 +0100 | [diff] [blame] | 1496 | } |