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