4 * Used to coordinate shared registers between HT threads or
5 * among events on a single PMU.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/stddef.h>
11 #include <linux/types.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/export.h>
15 #include <linux/nmi.h>
17 #include <asm/cpufeature.h>
18 #include <asm/hardirq.h>
21 #include "perf_event.h"
24 * Intel PerfMon, used on Core and later.
26 static u64 intel_perfmon_event_map[PERF_COUNT_HW_MAX] __read_mostly =
28 [PERF_COUNT_HW_CPU_CYCLES] = 0x003c,
29 [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
30 [PERF_COUNT_HW_CACHE_REFERENCES] = 0x4f2e,
31 [PERF_COUNT_HW_CACHE_MISSES] = 0x412e,
32 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
33 [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
34 [PERF_COUNT_HW_BUS_CYCLES] = 0x013c,
35 [PERF_COUNT_HW_REF_CPU_CYCLES] = 0x0300, /* pseudo-encoding */
38 static struct event_constraint intel_core_event_constraints[] __read_mostly =
40 INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
41 INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
42 INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
43 INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
44 INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
45 INTEL_EVENT_CONSTRAINT(0xc1, 0x1), /* FP_COMP_INSTR_RET */
49 static struct event_constraint intel_core2_event_constraints[] __read_mostly =
51 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
52 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
53 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
54 INTEL_EVENT_CONSTRAINT(0x10, 0x1), /* FP_COMP_OPS_EXE */
55 INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
56 INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
57 INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
58 INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
59 INTEL_EVENT_CONSTRAINT(0x18, 0x1), /* IDLE_DURING_DIV */
60 INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
61 INTEL_EVENT_CONSTRAINT(0xa1, 0x1), /* RS_UOPS_DISPATCH_CYCLES */
62 INTEL_EVENT_CONSTRAINT(0xc9, 0x1), /* ITLB_MISS_RETIRED (T30-9) */
63 INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED */
67 static struct event_constraint intel_nehalem_event_constraints[] __read_mostly =
69 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
70 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
71 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
72 INTEL_EVENT_CONSTRAINT(0x40, 0x3), /* L1D_CACHE_LD */
73 INTEL_EVENT_CONSTRAINT(0x41, 0x3), /* L1D_CACHE_ST */
74 INTEL_EVENT_CONSTRAINT(0x42, 0x3), /* L1D_CACHE_LOCK */
75 INTEL_EVENT_CONSTRAINT(0x43, 0x3), /* L1D_ALL_REF */
76 INTEL_EVENT_CONSTRAINT(0x48, 0x3), /* L1D_PEND_MISS */
77 INTEL_EVENT_CONSTRAINT(0x4e, 0x3), /* L1D_PREFETCH */
78 INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
79 INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */
83 static struct extra_reg intel_nehalem_extra_regs[] __read_mostly =
85 /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
86 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0xffff, RSP_0),
87 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x100b),
91 static struct event_constraint intel_westmere_event_constraints[] __read_mostly =
93 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
94 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
95 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
96 INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
97 INTEL_EVENT_CONSTRAINT(0x60, 0x1), /* OFFCORE_REQUESTS_OUTSTANDING */
98 INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */
99 INTEL_EVENT_CONSTRAINT(0xb3, 0x1), /* SNOOPQ_REQUEST_OUTSTANDING */
103 static struct event_constraint intel_snb_event_constraints[] __read_mostly =
105 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
106 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
107 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
108 INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_DISPATCH */
109 INTEL_UEVENT_CONSTRAINT(0x05a3, 0xf), /* CYCLE_ACTIVITY.STALLS_L2_PENDING */
110 INTEL_UEVENT_CONSTRAINT(0x02a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
111 INTEL_UEVENT_CONSTRAINT(0x06a3, 0x4), /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
112 INTEL_EVENT_CONSTRAINT(0x48, 0x4), /* L1D_PEND_MISS.PENDING */
113 INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
114 INTEL_EVENT_CONSTRAINT(0xcd, 0x8), /* MEM_TRANS_RETIRED.LOAD_LATENCY */
115 INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_DISPATCH */
116 INTEL_UEVENT_CONSTRAINT(0x02a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
118 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */
119 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
120 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
121 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
126 static struct event_constraint intel_ivb_event_constraints[] __read_mostly =
128 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
129 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
130 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
131 INTEL_UEVENT_CONSTRAINT(0x0148, 0x4), /* L1D_PEND_MISS.PENDING */
132 INTEL_UEVENT_CONSTRAINT(0x0279, 0xf), /* IDQ.EMTPY */
133 INTEL_UEVENT_CONSTRAINT(0x019c, 0xf), /* IDQ_UOPS_NOT_DELIVERED.CORE */
134 INTEL_UEVENT_CONSTRAINT(0x02a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_LDM_PENDING */
135 INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_EXECUTE */
136 INTEL_UEVENT_CONSTRAINT(0x05a3, 0xf), /* CYCLE_ACTIVITY.STALLS_L2_PENDING */
137 INTEL_UEVENT_CONSTRAINT(0x06a3, 0xf), /* CYCLE_ACTIVITY.STALLS_LDM_PENDING */
138 INTEL_UEVENT_CONSTRAINT(0x08a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
139 INTEL_UEVENT_CONSTRAINT(0x0ca3, 0x4), /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
140 INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
142 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */
143 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
144 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
145 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
150 static struct extra_reg intel_westmere_extra_regs[] __read_mostly =
152 /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
153 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0xffff, RSP_0),
154 INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0xffff, RSP_1),
155 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x100b),
159 static struct event_constraint intel_v1_event_constraints[] __read_mostly =
164 static struct event_constraint intel_gen_event_constraints[] __read_mostly =
166 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
167 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
168 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
172 static struct event_constraint intel_slm_event_constraints[] __read_mostly =
174 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
175 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
176 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* pseudo CPU_CLK_UNHALTED.REF */
180 struct event_constraint intel_skl_event_constraints[] = {
181 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
182 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
183 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
184 INTEL_UEVENT_CONSTRAINT(0x1c0, 0x2), /* INST_RETIRED.PREC_DIST */
188 static struct extra_reg intel_snb_extra_regs[] __read_mostly = {
189 /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
190 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x3f807f8fffull, RSP_0),
191 INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0x3f807f8fffull, RSP_1),
192 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd),
196 static struct extra_reg intel_snbep_extra_regs[] __read_mostly = {
197 /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
198 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x3fffff8fffull, RSP_0),
199 INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0x3fffff8fffull, RSP_1),
200 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd),
204 static struct extra_reg intel_skl_extra_regs[] __read_mostly = {
205 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x3fffff8fffull, RSP_0),
206 INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0x3fffff8fffull, RSP_1),
207 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd),
209 * Note the low 8 bits eventsel code is not a continuous field, containing
210 * some #GPing bits. These are masked out.
212 INTEL_UEVENT_EXTRA_REG(0x01c6, MSR_PEBS_FRONTEND, 0x7fff17, FE),
216 EVENT_ATTR_STR(mem-loads, mem_ld_nhm, "event=0x0b,umask=0x10,ldlat=3");
217 EVENT_ATTR_STR(mem-loads, mem_ld_snb, "event=0xcd,umask=0x1,ldlat=3");
218 EVENT_ATTR_STR(mem-stores, mem_st_snb, "event=0xcd,umask=0x2");
220 struct attribute *nhm_events_attrs[] = {
221 EVENT_PTR(mem_ld_nhm),
225 struct attribute *snb_events_attrs[] = {
226 EVENT_PTR(mem_ld_snb),
227 EVENT_PTR(mem_st_snb),
231 static struct event_constraint intel_hsw_event_constraints[] = {
232 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
233 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
234 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
235 INTEL_UEVENT_CONSTRAINT(0x148, 0x4), /* L1D_PEND_MISS.PENDING */
236 INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
237 INTEL_EVENT_CONSTRAINT(0xcd, 0x8), /* MEM_TRANS_RETIRED.LOAD_LATENCY */
238 /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
239 INTEL_UEVENT_CONSTRAINT(0x08a3, 0x4),
240 /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
241 INTEL_UEVENT_CONSTRAINT(0x0ca3, 0x4),
242 /* CYCLE_ACTIVITY.CYCLES_NO_EXECUTE */
243 INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf),
245 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */
246 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
247 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
248 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
253 struct event_constraint intel_bdw_event_constraints[] = {
254 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
255 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
256 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
257 INTEL_UEVENT_CONSTRAINT(0x148, 0x4), /* L1D_PEND_MISS.PENDING */
258 INTEL_UEVENT_CONSTRAINT(0x8a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_MISS */
262 static u64 intel_pmu_event_map(int hw_event)
264 return intel_perfmon_event_map[hw_event];
268 * Notes on the events:
269 * - data reads do not include code reads (comparable to earlier tables)
270 * - data counts include speculative execution (except L1 write, dtlb, bpu)
271 * - remote node access includes remote memory, remote cache, remote mmio.
272 * - prefetches are not included in the counts.
273 * - icache miss does not include decoded icache
276 #define SKL_DEMAND_DATA_RD BIT_ULL(0)
277 #define SKL_DEMAND_RFO BIT_ULL(1)
278 #define SKL_ANY_RESPONSE BIT_ULL(16)
279 #define SKL_SUPPLIER_NONE BIT_ULL(17)
280 #define SKL_L3_MISS_LOCAL_DRAM BIT_ULL(26)
281 #define SKL_L3_MISS_REMOTE_HOP0_DRAM BIT_ULL(27)
282 #define SKL_L3_MISS_REMOTE_HOP1_DRAM BIT_ULL(28)
283 #define SKL_L3_MISS_REMOTE_HOP2P_DRAM BIT_ULL(29)
284 #define SKL_L3_MISS (SKL_L3_MISS_LOCAL_DRAM| \
285 SKL_L3_MISS_REMOTE_HOP0_DRAM| \
286 SKL_L3_MISS_REMOTE_HOP1_DRAM| \
287 SKL_L3_MISS_REMOTE_HOP2P_DRAM)
288 #define SKL_SPL_HIT BIT_ULL(30)
289 #define SKL_SNOOP_NONE BIT_ULL(31)
290 #define SKL_SNOOP_NOT_NEEDED BIT_ULL(32)
291 #define SKL_SNOOP_MISS BIT_ULL(33)
292 #define SKL_SNOOP_HIT_NO_FWD BIT_ULL(34)
293 #define SKL_SNOOP_HIT_WITH_FWD BIT_ULL(35)
294 #define SKL_SNOOP_HITM BIT_ULL(36)
295 #define SKL_SNOOP_NON_DRAM BIT_ULL(37)
296 #define SKL_ANY_SNOOP (SKL_SPL_HIT|SKL_SNOOP_NONE| \
297 SKL_SNOOP_NOT_NEEDED|SKL_SNOOP_MISS| \
298 SKL_SNOOP_HIT_NO_FWD|SKL_SNOOP_HIT_WITH_FWD| \
299 SKL_SNOOP_HITM|SKL_SNOOP_NON_DRAM)
300 #define SKL_DEMAND_READ SKL_DEMAND_DATA_RD
301 #define SKL_SNOOP_DRAM (SKL_SNOOP_NONE| \
302 SKL_SNOOP_NOT_NEEDED|SKL_SNOOP_MISS| \
303 SKL_SNOOP_HIT_NO_FWD|SKL_SNOOP_HIT_WITH_FWD| \
304 SKL_SNOOP_HITM|SKL_SPL_HIT)
305 #define SKL_DEMAND_WRITE SKL_DEMAND_RFO
306 #define SKL_LLC_ACCESS SKL_ANY_RESPONSE
307 #define SKL_L3_MISS_REMOTE (SKL_L3_MISS_REMOTE_HOP0_DRAM| \
308 SKL_L3_MISS_REMOTE_HOP1_DRAM| \
309 SKL_L3_MISS_REMOTE_HOP2P_DRAM)
311 static __initconst const u64 skl_hw_cache_event_ids
312 [PERF_COUNT_HW_CACHE_MAX]
313 [PERF_COUNT_HW_CACHE_OP_MAX]
314 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
318 [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_INST_RETIRED.ALL_LOADS */
319 [ C(RESULT_MISS) ] = 0x151, /* L1D.REPLACEMENT */
322 [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_INST_RETIRED.ALL_STORES */
323 [ C(RESULT_MISS) ] = 0x0,
325 [ C(OP_PREFETCH) ] = {
326 [ C(RESULT_ACCESS) ] = 0x0,
327 [ C(RESULT_MISS) ] = 0x0,
332 [ C(RESULT_ACCESS) ] = 0x0,
333 [ C(RESULT_MISS) ] = 0x283, /* ICACHE_64B.MISS */
336 [ C(RESULT_ACCESS) ] = -1,
337 [ C(RESULT_MISS) ] = -1,
339 [ C(OP_PREFETCH) ] = {
340 [ C(RESULT_ACCESS) ] = 0x0,
341 [ C(RESULT_MISS) ] = 0x0,
346 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
347 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
350 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
351 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
353 [ C(OP_PREFETCH) ] = {
354 [ C(RESULT_ACCESS) ] = 0x0,
355 [ C(RESULT_MISS) ] = 0x0,
360 [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_INST_RETIRED.ALL_LOADS */
361 [ C(RESULT_MISS) ] = 0x608, /* DTLB_LOAD_MISSES.WALK_COMPLETED */
364 [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_INST_RETIRED.ALL_STORES */
365 [ C(RESULT_MISS) ] = 0x649, /* DTLB_STORE_MISSES.WALK_COMPLETED */
367 [ C(OP_PREFETCH) ] = {
368 [ C(RESULT_ACCESS) ] = 0x0,
369 [ C(RESULT_MISS) ] = 0x0,
374 [ C(RESULT_ACCESS) ] = 0x2085, /* ITLB_MISSES.STLB_HIT */
375 [ C(RESULT_MISS) ] = 0xe85, /* ITLB_MISSES.WALK_COMPLETED */
378 [ C(RESULT_ACCESS) ] = -1,
379 [ C(RESULT_MISS) ] = -1,
381 [ C(OP_PREFETCH) ] = {
382 [ C(RESULT_ACCESS) ] = -1,
383 [ C(RESULT_MISS) ] = -1,
388 [ C(RESULT_ACCESS) ] = 0xc4, /* BR_INST_RETIRED.ALL_BRANCHES */
389 [ C(RESULT_MISS) ] = 0xc5, /* BR_MISP_RETIRED.ALL_BRANCHES */
392 [ C(RESULT_ACCESS) ] = -1,
393 [ C(RESULT_MISS) ] = -1,
395 [ C(OP_PREFETCH) ] = {
396 [ C(RESULT_ACCESS) ] = -1,
397 [ C(RESULT_MISS) ] = -1,
402 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
403 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
406 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
407 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
409 [ C(OP_PREFETCH) ] = {
410 [ C(RESULT_ACCESS) ] = 0x0,
411 [ C(RESULT_MISS) ] = 0x0,
416 static __initconst const u64 skl_hw_cache_extra_regs
417 [PERF_COUNT_HW_CACHE_MAX]
418 [PERF_COUNT_HW_CACHE_OP_MAX]
419 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
423 [ C(RESULT_ACCESS) ] = SKL_DEMAND_READ|
424 SKL_LLC_ACCESS|SKL_ANY_SNOOP,
425 [ C(RESULT_MISS) ] = SKL_DEMAND_READ|
426 SKL_L3_MISS|SKL_ANY_SNOOP|
430 [ C(RESULT_ACCESS) ] = SKL_DEMAND_WRITE|
431 SKL_LLC_ACCESS|SKL_ANY_SNOOP,
432 [ C(RESULT_MISS) ] = SKL_DEMAND_WRITE|
433 SKL_L3_MISS|SKL_ANY_SNOOP|
436 [ C(OP_PREFETCH) ] = {
437 [ C(RESULT_ACCESS) ] = 0x0,
438 [ C(RESULT_MISS) ] = 0x0,
443 [ C(RESULT_ACCESS) ] = SKL_DEMAND_READ|
444 SKL_L3_MISS_LOCAL_DRAM|SKL_SNOOP_DRAM,
445 [ C(RESULT_MISS) ] = SKL_DEMAND_READ|
446 SKL_L3_MISS_REMOTE|SKL_SNOOP_DRAM,
449 [ C(RESULT_ACCESS) ] = SKL_DEMAND_WRITE|
450 SKL_L3_MISS_LOCAL_DRAM|SKL_SNOOP_DRAM,
451 [ C(RESULT_MISS) ] = SKL_DEMAND_WRITE|
452 SKL_L3_MISS_REMOTE|SKL_SNOOP_DRAM,
454 [ C(OP_PREFETCH) ] = {
455 [ C(RESULT_ACCESS) ] = 0x0,
456 [ C(RESULT_MISS) ] = 0x0,
461 #define SNB_DMND_DATA_RD (1ULL << 0)
462 #define SNB_DMND_RFO (1ULL << 1)
463 #define SNB_DMND_IFETCH (1ULL << 2)
464 #define SNB_DMND_WB (1ULL << 3)
465 #define SNB_PF_DATA_RD (1ULL << 4)
466 #define SNB_PF_RFO (1ULL << 5)
467 #define SNB_PF_IFETCH (1ULL << 6)
468 #define SNB_LLC_DATA_RD (1ULL << 7)
469 #define SNB_LLC_RFO (1ULL << 8)
470 #define SNB_LLC_IFETCH (1ULL << 9)
471 #define SNB_BUS_LOCKS (1ULL << 10)
472 #define SNB_STRM_ST (1ULL << 11)
473 #define SNB_OTHER (1ULL << 15)
474 #define SNB_RESP_ANY (1ULL << 16)
475 #define SNB_NO_SUPP (1ULL << 17)
476 #define SNB_LLC_HITM (1ULL << 18)
477 #define SNB_LLC_HITE (1ULL << 19)
478 #define SNB_LLC_HITS (1ULL << 20)
479 #define SNB_LLC_HITF (1ULL << 21)
480 #define SNB_LOCAL (1ULL << 22)
481 #define SNB_REMOTE (0xffULL << 23)
482 #define SNB_SNP_NONE (1ULL << 31)
483 #define SNB_SNP_NOT_NEEDED (1ULL << 32)
484 #define SNB_SNP_MISS (1ULL << 33)
485 #define SNB_NO_FWD (1ULL << 34)
486 #define SNB_SNP_FWD (1ULL << 35)
487 #define SNB_HITM (1ULL << 36)
488 #define SNB_NON_DRAM (1ULL << 37)
490 #define SNB_DMND_READ (SNB_DMND_DATA_RD|SNB_LLC_DATA_RD)
491 #define SNB_DMND_WRITE (SNB_DMND_RFO|SNB_LLC_RFO)
492 #define SNB_DMND_PREFETCH (SNB_PF_DATA_RD|SNB_PF_RFO)
494 #define SNB_SNP_ANY (SNB_SNP_NONE|SNB_SNP_NOT_NEEDED| \
495 SNB_SNP_MISS|SNB_NO_FWD|SNB_SNP_FWD| \
498 #define SNB_DRAM_ANY (SNB_LOCAL|SNB_REMOTE|SNB_SNP_ANY)
499 #define SNB_DRAM_REMOTE (SNB_REMOTE|SNB_SNP_ANY)
501 #define SNB_L3_ACCESS SNB_RESP_ANY
502 #define SNB_L3_MISS (SNB_DRAM_ANY|SNB_NON_DRAM)
504 static __initconst const u64 snb_hw_cache_extra_regs
505 [PERF_COUNT_HW_CACHE_MAX]
506 [PERF_COUNT_HW_CACHE_OP_MAX]
507 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
511 [ C(RESULT_ACCESS) ] = SNB_DMND_READ|SNB_L3_ACCESS,
512 [ C(RESULT_MISS) ] = SNB_DMND_READ|SNB_L3_MISS,
515 [ C(RESULT_ACCESS) ] = SNB_DMND_WRITE|SNB_L3_ACCESS,
516 [ C(RESULT_MISS) ] = SNB_DMND_WRITE|SNB_L3_MISS,
518 [ C(OP_PREFETCH) ] = {
519 [ C(RESULT_ACCESS) ] = SNB_DMND_PREFETCH|SNB_L3_ACCESS,
520 [ C(RESULT_MISS) ] = SNB_DMND_PREFETCH|SNB_L3_MISS,
525 [ C(RESULT_ACCESS) ] = SNB_DMND_READ|SNB_DRAM_ANY,
526 [ C(RESULT_MISS) ] = SNB_DMND_READ|SNB_DRAM_REMOTE,
529 [ C(RESULT_ACCESS) ] = SNB_DMND_WRITE|SNB_DRAM_ANY,
530 [ C(RESULT_MISS) ] = SNB_DMND_WRITE|SNB_DRAM_REMOTE,
532 [ C(OP_PREFETCH) ] = {
533 [ C(RESULT_ACCESS) ] = SNB_DMND_PREFETCH|SNB_DRAM_ANY,
534 [ C(RESULT_MISS) ] = SNB_DMND_PREFETCH|SNB_DRAM_REMOTE,
539 static __initconst const u64 snb_hw_cache_event_ids
540 [PERF_COUNT_HW_CACHE_MAX]
541 [PERF_COUNT_HW_CACHE_OP_MAX]
542 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
546 [ C(RESULT_ACCESS) ] = 0xf1d0, /* MEM_UOP_RETIRED.LOADS */
547 [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPLACEMENT */
550 [ C(RESULT_ACCESS) ] = 0xf2d0, /* MEM_UOP_RETIRED.STORES */
551 [ C(RESULT_MISS) ] = 0x0851, /* L1D.ALL_M_REPLACEMENT */
553 [ C(OP_PREFETCH) ] = {
554 [ C(RESULT_ACCESS) ] = 0x0,
555 [ C(RESULT_MISS) ] = 0x024e, /* HW_PRE_REQ.DL1_MISS */
560 [ C(RESULT_ACCESS) ] = 0x0,
561 [ C(RESULT_MISS) ] = 0x0280, /* ICACHE.MISSES */
564 [ C(RESULT_ACCESS) ] = -1,
565 [ C(RESULT_MISS) ] = -1,
567 [ C(OP_PREFETCH) ] = {
568 [ C(RESULT_ACCESS) ] = 0x0,
569 [ C(RESULT_MISS) ] = 0x0,
574 /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
575 [ C(RESULT_ACCESS) ] = 0x01b7,
576 /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
577 [ C(RESULT_MISS) ] = 0x01b7,
580 /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
581 [ C(RESULT_ACCESS) ] = 0x01b7,
582 /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
583 [ C(RESULT_MISS) ] = 0x01b7,
585 [ C(OP_PREFETCH) ] = {
586 /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
587 [ C(RESULT_ACCESS) ] = 0x01b7,
588 /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
589 [ C(RESULT_MISS) ] = 0x01b7,
594 [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOP_RETIRED.ALL_LOADS */
595 [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.CAUSES_A_WALK */
598 [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOP_RETIRED.ALL_STORES */
599 [ C(RESULT_MISS) ] = 0x0149, /* DTLB_STORE_MISSES.MISS_CAUSES_A_WALK */
601 [ C(OP_PREFETCH) ] = {
602 [ C(RESULT_ACCESS) ] = 0x0,
603 [ C(RESULT_MISS) ] = 0x0,
608 [ C(RESULT_ACCESS) ] = 0x1085, /* ITLB_MISSES.STLB_HIT */
609 [ C(RESULT_MISS) ] = 0x0185, /* ITLB_MISSES.CAUSES_A_WALK */
612 [ C(RESULT_ACCESS) ] = -1,
613 [ C(RESULT_MISS) ] = -1,
615 [ C(OP_PREFETCH) ] = {
616 [ C(RESULT_ACCESS) ] = -1,
617 [ C(RESULT_MISS) ] = -1,
622 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
623 [ C(RESULT_MISS) ] = 0x00c5, /* BR_MISP_RETIRED.ALL_BRANCHES */
626 [ C(RESULT_ACCESS) ] = -1,
627 [ C(RESULT_MISS) ] = -1,
629 [ C(OP_PREFETCH) ] = {
630 [ C(RESULT_ACCESS) ] = -1,
631 [ C(RESULT_MISS) ] = -1,
636 [ C(RESULT_ACCESS) ] = 0x01b7,
637 [ C(RESULT_MISS) ] = 0x01b7,
640 [ C(RESULT_ACCESS) ] = 0x01b7,
641 [ C(RESULT_MISS) ] = 0x01b7,
643 [ C(OP_PREFETCH) ] = {
644 [ C(RESULT_ACCESS) ] = 0x01b7,
645 [ C(RESULT_MISS) ] = 0x01b7,
652 * Notes on the events:
653 * - data reads do not include code reads (comparable to earlier tables)
654 * - data counts include speculative execution (except L1 write, dtlb, bpu)
655 * - remote node access includes remote memory, remote cache, remote mmio.
656 * - prefetches are not included in the counts because they are not
660 #define HSW_DEMAND_DATA_RD BIT_ULL(0)
661 #define HSW_DEMAND_RFO BIT_ULL(1)
662 #define HSW_ANY_RESPONSE BIT_ULL(16)
663 #define HSW_SUPPLIER_NONE BIT_ULL(17)
664 #define HSW_L3_MISS_LOCAL_DRAM BIT_ULL(22)
665 #define HSW_L3_MISS_REMOTE_HOP0 BIT_ULL(27)
666 #define HSW_L3_MISS_REMOTE_HOP1 BIT_ULL(28)
667 #define HSW_L3_MISS_REMOTE_HOP2P BIT_ULL(29)
668 #define HSW_L3_MISS (HSW_L3_MISS_LOCAL_DRAM| \
669 HSW_L3_MISS_REMOTE_HOP0|HSW_L3_MISS_REMOTE_HOP1| \
670 HSW_L3_MISS_REMOTE_HOP2P)
671 #define HSW_SNOOP_NONE BIT_ULL(31)
672 #define HSW_SNOOP_NOT_NEEDED BIT_ULL(32)
673 #define HSW_SNOOP_MISS BIT_ULL(33)
674 #define HSW_SNOOP_HIT_NO_FWD BIT_ULL(34)
675 #define HSW_SNOOP_HIT_WITH_FWD BIT_ULL(35)
676 #define HSW_SNOOP_HITM BIT_ULL(36)
677 #define HSW_SNOOP_NON_DRAM BIT_ULL(37)
678 #define HSW_ANY_SNOOP (HSW_SNOOP_NONE| \
679 HSW_SNOOP_NOT_NEEDED|HSW_SNOOP_MISS| \
680 HSW_SNOOP_HIT_NO_FWD|HSW_SNOOP_HIT_WITH_FWD| \
681 HSW_SNOOP_HITM|HSW_SNOOP_NON_DRAM)
682 #define HSW_SNOOP_DRAM (HSW_ANY_SNOOP & ~HSW_SNOOP_NON_DRAM)
683 #define HSW_DEMAND_READ HSW_DEMAND_DATA_RD
684 #define HSW_DEMAND_WRITE HSW_DEMAND_RFO
685 #define HSW_L3_MISS_REMOTE (HSW_L3_MISS_REMOTE_HOP0|\
686 HSW_L3_MISS_REMOTE_HOP1|HSW_L3_MISS_REMOTE_HOP2P)
687 #define HSW_LLC_ACCESS HSW_ANY_RESPONSE
689 #define BDW_L3_MISS_LOCAL BIT(26)
690 #define BDW_L3_MISS (BDW_L3_MISS_LOCAL| \
691 HSW_L3_MISS_REMOTE_HOP0|HSW_L3_MISS_REMOTE_HOP1| \
692 HSW_L3_MISS_REMOTE_HOP2P)
695 static __initconst const u64 hsw_hw_cache_event_ids
696 [PERF_COUNT_HW_CACHE_MAX]
697 [PERF_COUNT_HW_CACHE_OP_MAX]
698 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
702 [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
703 [ C(RESULT_MISS) ] = 0x151, /* L1D.REPLACEMENT */
706 [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
707 [ C(RESULT_MISS) ] = 0x0,
709 [ C(OP_PREFETCH) ] = {
710 [ C(RESULT_ACCESS) ] = 0x0,
711 [ C(RESULT_MISS) ] = 0x0,
716 [ C(RESULT_ACCESS) ] = 0x0,
717 [ C(RESULT_MISS) ] = 0x280, /* ICACHE.MISSES */
720 [ C(RESULT_ACCESS) ] = -1,
721 [ C(RESULT_MISS) ] = -1,
723 [ C(OP_PREFETCH) ] = {
724 [ C(RESULT_ACCESS) ] = 0x0,
725 [ C(RESULT_MISS) ] = 0x0,
730 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
731 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
734 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
735 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
737 [ C(OP_PREFETCH) ] = {
738 [ C(RESULT_ACCESS) ] = 0x0,
739 [ C(RESULT_MISS) ] = 0x0,
744 [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
745 [ C(RESULT_MISS) ] = 0x108, /* DTLB_LOAD_MISSES.MISS_CAUSES_A_WALK */
748 [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
749 [ C(RESULT_MISS) ] = 0x149, /* DTLB_STORE_MISSES.MISS_CAUSES_A_WALK */
751 [ C(OP_PREFETCH) ] = {
752 [ C(RESULT_ACCESS) ] = 0x0,
753 [ C(RESULT_MISS) ] = 0x0,
758 [ C(RESULT_ACCESS) ] = 0x6085, /* ITLB_MISSES.STLB_HIT */
759 [ C(RESULT_MISS) ] = 0x185, /* ITLB_MISSES.MISS_CAUSES_A_WALK */
762 [ C(RESULT_ACCESS) ] = -1,
763 [ C(RESULT_MISS) ] = -1,
765 [ C(OP_PREFETCH) ] = {
766 [ C(RESULT_ACCESS) ] = -1,
767 [ C(RESULT_MISS) ] = -1,
772 [ C(RESULT_ACCESS) ] = 0xc4, /* BR_INST_RETIRED.ALL_BRANCHES */
773 [ C(RESULT_MISS) ] = 0xc5, /* BR_MISP_RETIRED.ALL_BRANCHES */
776 [ C(RESULT_ACCESS) ] = -1,
777 [ C(RESULT_MISS) ] = -1,
779 [ C(OP_PREFETCH) ] = {
780 [ C(RESULT_ACCESS) ] = -1,
781 [ C(RESULT_MISS) ] = -1,
786 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
787 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
790 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
791 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
793 [ C(OP_PREFETCH) ] = {
794 [ C(RESULT_ACCESS) ] = 0x0,
795 [ C(RESULT_MISS) ] = 0x0,
800 static __initconst const u64 hsw_hw_cache_extra_regs
801 [PERF_COUNT_HW_CACHE_MAX]
802 [PERF_COUNT_HW_CACHE_OP_MAX]
803 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
807 [ C(RESULT_ACCESS) ] = HSW_DEMAND_READ|
809 [ C(RESULT_MISS) ] = HSW_DEMAND_READ|
810 HSW_L3_MISS|HSW_ANY_SNOOP,
813 [ C(RESULT_ACCESS) ] = HSW_DEMAND_WRITE|
815 [ C(RESULT_MISS) ] = HSW_DEMAND_WRITE|
816 HSW_L3_MISS|HSW_ANY_SNOOP,
818 [ C(OP_PREFETCH) ] = {
819 [ C(RESULT_ACCESS) ] = 0x0,
820 [ C(RESULT_MISS) ] = 0x0,
825 [ C(RESULT_ACCESS) ] = HSW_DEMAND_READ|
826 HSW_L3_MISS_LOCAL_DRAM|
828 [ C(RESULT_MISS) ] = HSW_DEMAND_READ|
833 [ C(RESULT_ACCESS) ] = HSW_DEMAND_WRITE|
834 HSW_L3_MISS_LOCAL_DRAM|
836 [ C(RESULT_MISS) ] = HSW_DEMAND_WRITE|
840 [ C(OP_PREFETCH) ] = {
841 [ C(RESULT_ACCESS) ] = 0x0,
842 [ C(RESULT_MISS) ] = 0x0,
847 static __initconst const u64 westmere_hw_cache_event_ids
848 [PERF_COUNT_HW_CACHE_MAX]
849 [PERF_COUNT_HW_CACHE_OP_MAX]
850 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
854 [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
855 [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPL */
858 [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
859 [ C(RESULT_MISS) ] = 0x0251, /* L1D.M_REPL */
861 [ C(OP_PREFETCH) ] = {
862 [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
863 [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
868 [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
869 [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
872 [ C(RESULT_ACCESS) ] = -1,
873 [ C(RESULT_MISS) ] = -1,
875 [ C(OP_PREFETCH) ] = {
876 [ C(RESULT_ACCESS) ] = 0x0,
877 [ C(RESULT_MISS) ] = 0x0,
882 /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
883 [ C(RESULT_ACCESS) ] = 0x01b7,
884 /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
885 [ C(RESULT_MISS) ] = 0x01b7,
888 * Use RFO, not WRITEBACK, because a write miss would typically occur
892 /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
893 [ C(RESULT_ACCESS) ] = 0x01b7,
894 /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
895 [ C(RESULT_MISS) ] = 0x01b7,
897 [ C(OP_PREFETCH) ] = {
898 /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
899 [ C(RESULT_ACCESS) ] = 0x01b7,
900 /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
901 [ C(RESULT_MISS) ] = 0x01b7,
906 [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
907 [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
910 [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
911 [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
913 [ C(OP_PREFETCH) ] = {
914 [ C(RESULT_ACCESS) ] = 0x0,
915 [ C(RESULT_MISS) ] = 0x0,
920 [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
921 [ C(RESULT_MISS) ] = 0x0185, /* ITLB_MISSES.ANY */
924 [ C(RESULT_ACCESS) ] = -1,
925 [ C(RESULT_MISS) ] = -1,
927 [ C(OP_PREFETCH) ] = {
928 [ C(RESULT_ACCESS) ] = -1,
929 [ C(RESULT_MISS) ] = -1,
934 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
935 [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
938 [ C(RESULT_ACCESS) ] = -1,
939 [ C(RESULT_MISS) ] = -1,
941 [ C(OP_PREFETCH) ] = {
942 [ C(RESULT_ACCESS) ] = -1,
943 [ C(RESULT_MISS) ] = -1,
948 [ C(RESULT_ACCESS) ] = 0x01b7,
949 [ C(RESULT_MISS) ] = 0x01b7,
952 [ C(RESULT_ACCESS) ] = 0x01b7,
953 [ C(RESULT_MISS) ] = 0x01b7,
955 [ C(OP_PREFETCH) ] = {
956 [ C(RESULT_ACCESS) ] = 0x01b7,
957 [ C(RESULT_MISS) ] = 0x01b7,
963 * Nehalem/Westmere MSR_OFFCORE_RESPONSE bits;
964 * See IA32 SDM Vol 3B 30.6.1.3
967 #define NHM_DMND_DATA_RD (1 << 0)
968 #define NHM_DMND_RFO (1 << 1)
969 #define NHM_DMND_IFETCH (1 << 2)
970 #define NHM_DMND_WB (1 << 3)
971 #define NHM_PF_DATA_RD (1 << 4)
972 #define NHM_PF_DATA_RFO (1 << 5)
973 #define NHM_PF_IFETCH (1 << 6)
974 #define NHM_OFFCORE_OTHER (1 << 7)
975 #define NHM_UNCORE_HIT (1 << 8)
976 #define NHM_OTHER_CORE_HIT_SNP (1 << 9)
977 #define NHM_OTHER_CORE_HITM (1 << 10)
979 #define NHM_REMOTE_CACHE_FWD (1 << 12)
980 #define NHM_REMOTE_DRAM (1 << 13)
981 #define NHM_LOCAL_DRAM (1 << 14)
982 #define NHM_NON_DRAM (1 << 15)
984 #define NHM_LOCAL (NHM_LOCAL_DRAM|NHM_REMOTE_CACHE_FWD)
985 #define NHM_REMOTE (NHM_REMOTE_DRAM)
987 #define NHM_DMND_READ (NHM_DMND_DATA_RD)
988 #define NHM_DMND_WRITE (NHM_DMND_RFO|NHM_DMND_WB)
989 #define NHM_DMND_PREFETCH (NHM_PF_DATA_RD|NHM_PF_DATA_RFO)
991 #define NHM_L3_HIT (NHM_UNCORE_HIT|NHM_OTHER_CORE_HIT_SNP|NHM_OTHER_CORE_HITM)
992 #define NHM_L3_MISS (NHM_NON_DRAM|NHM_LOCAL_DRAM|NHM_REMOTE_DRAM|NHM_REMOTE_CACHE_FWD)
993 #define NHM_L3_ACCESS (NHM_L3_HIT|NHM_L3_MISS)
995 static __initconst const u64 nehalem_hw_cache_extra_regs
996 [PERF_COUNT_HW_CACHE_MAX]
997 [PERF_COUNT_HW_CACHE_OP_MAX]
998 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1002 [ C(RESULT_ACCESS) ] = NHM_DMND_READ|NHM_L3_ACCESS,
1003 [ C(RESULT_MISS) ] = NHM_DMND_READ|NHM_L3_MISS,
1006 [ C(RESULT_ACCESS) ] = NHM_DMND_WRITE|NHM_L3_ACCESS,
1007 [ C(RESULT_MISS) ] = NHM_DMND_WRITE|NHM_L3_MISS,
1009 [ C(OP_PREFETCH) ] = {
1010 [ C(RESULT_ACCESS) ] = NHM_DMND_PREFETCH|NHM_L3_ACCESS,
1011 [ C(RESULT_MISS) ] = NHM_DMND_PREFETCH|NHM_L3_MISS,
1016 [ C(RESULT_ACCESS) ] = NHM_DMND_READ|NHM_LOCAL|NHM_REMOTE,
1017 [ C(RESULT_MISS) ] = NHM_DMND_READ|NHM_REMOTE,
1020 [ C(RESULT_ACCESS) ] = NHM_DMND_WRITE|NHM_LOCAL|NHM_REMOTE,
1021 [ C(RESULT_MISS) ] = NHM_DMND_WRITE|NHM_REMOTE,
1023 [ C(OP_PREFETCH) ] = {
1024 [ C(RESULT_ACCESS) ] = NHM_DMND_PREFETCH|NHM_LOCAL|NHM_REMOTE,
1025 [ C(RESULT_MISS) ] = NHM_DMND_PREFETCH|NHM_REMOTE,
1030 static __initconst const u64 nehalem_hw_cache_event_ids
1031 [PERF_COUNT_HW_CACHE_MAX]
1032 [PERF_COUNT_HW_CACHE_OP_MAX]
1033 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1037 [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
1038 [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPL */
1041 [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
1042 [ C(RESULT_MISS) ] = 0x0251, /* L1D.M_REPL */
1044 [ C(OP_PREFETCH) ] = {
1045 [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
1046 [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
1051 [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
1052 [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
1055 [ C(RESULT_ACCESS) ] = -1,
1056 [ C(RESULT_MISS) ] = -1,
1058 [ C(OP_PREFETCH) ] = {
1059 [ C(RESULT_ACCESS) ] = 0x0,
1060 [ C(RESULT_MISS) ] = 0x0,
1065 /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
1066 [ C(RESULT_ACCESS) ] = 0x01b7,
1067 /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
1068 [ C(RESULT_MISS) ] = 0x01b7,
1071 * Use RFO, not WRITEBACK, because a write miss would typically occur
1075 /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
1076 [ C(RESULT_ACCESS) ] = 0x01b7,
1077 /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
1078 [ C(RESULT_MISS) ] = 0x01b7,
1080 [ C(OP_PREFETCH) ] = {
1081 /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
1082 [ C(RESULT_ACCESS) ] = 0x01b7,
1083 /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
1084 [ C(RESULT_MISS) ] = 0x01b7,
1089 [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
1090 [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
1093 [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
1094 [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
1096 [ C(OP_PREFETCH) ] = {
1097 [ C(RESULT_ACCESS) ] = 0x0,
1098 [ C(RESULT_MISS) ] = 0x0,
1103 [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
1104 [ C(RESULT_MISS) ] = 0x20c8, /* ITLB_MISS_RETIRED */
1107 [ C(RESULT_ACCESS) ] = -1,
1108 [ C(RESULT_MISS) ] = -1,
1110 [ C(OP_PREFETCH) ] = {
1111 [ C(RESULT_ACCESS) ] = -1,
1112 [ C(RESULT_MISS) ] = -1,
1117 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
1118 [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
1121 [ C(RESULT_ACCESS) ] = -1,
1122 [ C(RESULT_MISS) ] = -1,
1124 [ C(OP_PREFETCH) ] = {
1125 [ C(RESULT_ACCESS) ] = -1,
1126 [ C(RESULT_MISS) ] = -1,
1131 [ C(RESULT_ACCESS) ] = 0x01b7,
1132 [ C(RESULT_MISS) ] = 0x01b7,
1135 [ C(RESULT_ACCESS) ] = 0x01b7,
1136 [ C(RESULT_MISS) ] = 0x01b7,
1138 [ C(OP_PREFETCH) ] = {
1139 [ C(RESULT_ACCESS) ] = 0x01b7,
1140 [ C(RESULT_MISS) ] = 0x01b7,
1145 static __initconst const u64 core2_hw_cache_event_ids
1146 [PERF_COUNT_HW_CACHE_MAX]
1147 [PERF_COUNT_HW_CACHE_OP_MAX]
1148 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1152 [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
1153 [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
1156 [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
1157 [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
1159 [ C(OP_PREFETCH) ] = {
1160 [ C(RESULT_ACCESS) ] = 0x104e, /* L1D_PREFETCH.REQUESTS */
1161 [ C(RESULT_MISS) ] = 0,
1166 [ C(RESULT_ACCESS) ] = 0x0080, /* L1I.READS */
1167 [ C(RESULT_MISS) ] = 0x0081, /* L1I.MISSES */
1170 [ C(RESULT_ACCESS) ] = -1,
1171 [ C(RESULT_MISS) ] = -1,
1173 [ C(OP_PREFETCH) ] = {
1174 [ C(RESULT_ACCESS) ] = 0,
1175 [ C(RESULT_MISS) ] = 0,
1180 [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
1181 [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
1184 [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
1185 [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
1187 [ C(OP_PREFETCH) ] = {
1188 [ C(RESULT_ACCESS) ] = 0,
1189 [ C(RESULT_MISS) ] = 0,
1194 [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
1195 [ C(RESULT_MISS) ] = 0x0208, /* DTLB_MISSES.MISS_LD */
1198 [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
1199 [ C(RESULT_MISS) ] = 0x0808, /* DTLB_MISSES.MISS_ST */
1201 [ C(OP_PREFETCH) ] = {
1202 [ C(RESULT_ACCESS) ] = 0,
1203 [ C(RESULT_MISS) ] = 0,
1208 [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
1209 [ C(RESULT_MISS) ] = 0x1282, /* ITLBMISSES */
1212 [ C(RESULT_ACCESS) ] = -1,
1213 [ C(RESULT_MISS) ] = -1,
1215 [ C(OP_PREFETCH) ] = {
1216 [ C(RESULT_ACCESS) ] = -1,
1217 [ C(RESULT_MISS) ] = -1,
1222 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
1223 [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
1226 [ C(RESULT_ACCESS) ] = -1,
1227 [ C(RESULT_MISS) ] = -1,
1229 [ C(OP_PREFETCH) ] = {
1230 [ C(RESULT_ACCESS) ] = -1,
1231 [ C(RESULT_MISS) ] = -1,
1236 static __initconst const u64 atom_hw_cache_event_ids
1237 [PERF_COUNT_HW_CACHE_MAX]
1238 [PERF_COUNT_HW_CACHE_OP_MAX]
1239 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1243 [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE.LD */
1244 [ C(RESULT_MISS) ] = 0,
1247 [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE.ST */
1248 [ C(RESULT_MISS) ] = 0,
1250 [ C(OP_PREFETCH) ] = {
1251 [ C(RESULT_ACCESS) ] = 0x0,
1252 [ C(RESULT_MISS) ] = 0,
1257 [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
1258 [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
1261 [ C(RESULT_ACCESS) ] = -1,
1262 [ C(RESULT_MISS) ] = -1,
1264 [ C(OP_PREFETCH) ] = {
1265 [ C(RESULT_ACCESS) ] = 0,
1266 [ C(RESULT_MISS) ] = 0,
1271 [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
1272 [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
1275 [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
1276 [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
1278 [ C(OP_PREFETCH) ] = {
1279 [ C(RESULT_ACCESS) ] = 0,
1280 [ C(RESULT_MISS) ] = 0,
1285 [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE_LD.MESI (alias) */
1286 [ C(RESULT_MISS) ] = 0x0508, /* DTLB_MISSES.MISS_LD */
1289 [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE_ST.MESI (alias) */
1290 [ C(RESULT_MISS) ] = 0x0608, /* DTLB_MISSES.MISS_ST */
1292 [ C(OP_PREFETCH) ] = {
1293 [ C(RESULT_ACCESS) ] = 0,
1294 [ C(RESULT_MISS) ] = 0,
1299 [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
1300 [ C(RESULT_MISS) ] = 0x0282, /* ITLB.MISSES */
1303 [ C(RESULT_ACCESS) ] = -1,
1304 [ C(RESULT_MISS) ] = -1,
1306 [ C(OP_PREFETCH) ] = {
1307 [ C(RESULT_ACCESS) ] = -1,
1308 [ C(RESULT_MISS) ] = -1,
1313 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
1314 [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
1317 [ C(RESULT_ACCESS) ] = -1,
1318 [ C(RESULT_MISS) ] = -1,
1320 [ C(OP_PREFETCH) ] = {
1321 [ C(RESULT_ACCESS) ] = -1,
1322 [ C(RESULT_MISS) ] = -1,
1327 static struct extra_reg intel_slm_extra_regs[] __read_mostly =
1329 /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
1330 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x768005ffffull, RSP_0),
1331 INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OFFCORE_RSP_1, 0x368005ffffull, RSP_1),
1335 #define SLM_DMND_READ SNB_DMND_DATA_RD
1336 #define SLM_DMND_WRITE SNB_DMND_RFO
1337 #define SLM_DMND_PREFETCH (SNB_PF_DATA_RD|SNB_PF_RFO)
1339 #define SLM_SNP_ANY (SNB_SNP_NONE|SNB_SNP_MISS|SNB_NO_FWD|SNB_HITM)
1340 #define SLM_LLC_ACCESS SNB_RESP_ANY
1341 #define SLM_LLC_MISS (SLM_SNP_ANY|SNB_NON_DRAM)
1343 static __initconst const u64 slm_hw_cache_extra_regs
1344 [PERF_COUNT_HW_CACHE_MAX]
1345 [PERF_COUNT_HW_CACHE_OP_MAX]
1346 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1350 [ C(RESULT_ACCESS) ] = SLM_DMND_READ|SLM_LLC_ACCESS,
1351 [ C(RESULT_MISS) ] = 0,
1354 [ C(RESULT_ACCESS) ] = SLM_DMND_WRITE|SLM_LLC_ACCESS,
1355 [ C(RESULT_MISS) ] = SLM_DMND_WRITE|SLM_LLC_MISS,
1357 [ C(OP_PREFETCH) ] = {
1358 [ C(RESULT_ACCESS) ] = SLM_DMND_PREFETCH|SLM_LLC_ACCESS,
1359 [ C(RESULT_MISS) ] = SLM_DMND_PREFETCH|SLM_LLC_MISS,
1364 static __initconst const u64 slm_hw_cache_event_ids
1365 [PERF_COUNT_HW_CACHE_MAX]
1366 [PERF_COUNT_HW_CACHE_OP_MAX]
1367 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1371 [ C(RESULT_ACCESS) ] = 0,
1372 [ C(RESULT_MISS) ] = 0x0104, /* LD_DCU_MISS */
1375 [ C(RESULT_ACCESS) ] = 0,
1376 [ C(RESULT_MISS) ] = 0,
1378 [ C(OP_PREFETCH) ] = {
1379 [ C(RESULT_ACCESS) ] = 0,
1380 [ C(RESULT_MISS) ] = 0,
1385 [ C(RESULT_ACCESS) ] = 0x0380, /* ICACHE.ACCESSES */
1386 [ C(RESULT_MISS) ] = 0x0280, /* ICACGE.MISSES */
1389 [ C(RESULT_ACCESS) ] = -1,
1390 [ C(RESULT_MISS) ] = -1,
1392 [ C(OP_PREFETCH) ] = {
1393 [ C(RESULT_ACCESS) ] = 0,
1394 [ C(RESULT_MISS) ] = 0,
1399 /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
1400 [ C(RESULT_ACCESS) ] = 0x01b7,
1401 [ C(RESULT_MISS) ] = 0,
1404 /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
1405 [ C(RESULT_ACCESS) ] = 0x01b7,
1406 /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
1407 [ C(RESULT_MISS) ] = 0x01b7,
1409 [ C(OP_PREFETCH) ] = {
1410 /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
1411 [ C(RESULT_ACCESS) ] = 0x01b7,
1412 /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
1413 [ C(RESULT_MISS) ] = 0x01b7,
1418 [ C(RESULT_ACCESS) ] = 0,
1419 [ C(RESULT_MISS) ] = 0x0804, /* LD_DTLB_MISS */
1422 [ C(RESULT_ACCESS) ] = 0,
1423 [ C(RESULT_MISS) ] = 0,
1425 [ C(OP_PREFETCH) ] = {
1426 [ C(RESULT_ACCESS) ] = 0,
1427 [ C(RESULT_MISS) ] = 0,
1432 [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
1433 [ C(RESULT_MISS) ] = 0x40205, /* PAGE_WALKS.I_SIDE_WALKS */
1436 [ C(RESULT_ACCESS) ] = -1,
1437 [ C(RESULT_MISS) ] = -1,
1439 [ C(OP_PREFETCH) ] = {
1440 [ C(RESULT_ACCESS) ] = -1,
1441 [ C(RESULT_MISS) ] = -1,
1446 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
1447 [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
1450 [ C(RESULT_ACCESS) ] = -1,
1451 [ C(RESULT_MISS) ] = -1,
1453 [ C(OP_PREFETCH) ] = {
1454 [ C(RESULT_ACCESS) ] = -1,
1455 [ C(RESULT_MISS) ] = -1,
1461 * Used from PMIs where the LBRs are already disabled.
1463 * This function could be called consecutively. It is required to remain in
1464 * disabled state if called consecutively.
1466 * During consecutive calls, the same disable value will be written to related
1467 * registers, so the PMU state remains unchanged. hw.state in
1468 * intel_bts_disable_local will remain PERF_HES_STOPPED too in consecutive
1471 static void __intel_pmu_disable_all(void)
1473 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1475 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
1477 if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask))
1478 intel_pmu_disable_bts();
1480 intel_bts_disable_local();
1482 intel_pmu_pebs_disable_all();
1485 static void intel_pmu_disable_all(void)
1487 __intel_pmu_disable_all();
1488 intel_pmu_lbr_disable_all();
1491 static void __intel_pmu_enable_all(int added, bool pmi)
1493 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1495 intel_pmu_pebs_enable_all();
1496 intel_pmu_lbr_enable_all(pmi);
1497 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL,
1498 x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_guest_mask);
1500 if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask)) {
1501 struct perf_event *event =
1502 cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
1504 if (WARN_ON_ONCE(!event))
1507 intel_pmu_enable_bts(event->hw.config);
1509 intel_bts_enable_local();
1512 static void intel_pmu_enable_all(int added)
1514 __intel_pmu_enable_all(added, false);
1519 * Intel Errata AAK100 (model 26)
1520 * Intel Errata AAP53 (model 30)
1521 * Intel Errata BD53 (model 44)
1523 * The official story:
1524 * These chips need to be 'reset' when adding counters by programming the
1525 * magic three (non-counting) events 0x4300B5, 0x4300D2, and 0x4300B1 either
1526 * in sequence on the same PMC or on different PMCs.
1528 * In practise it appears some of these events do in fact count, and
1529 * we need to programm all 4 events.
1531 static void intel_pmu_nhm_workaround(void)
1533 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1534 static const unsigned long nhm_magic[4] = {
1540 struct perf_event *event;
1544 * The Errata requires below steps:
1545 * 1) Clear MSR_IA32_PEBS_ENABLE and MSR_CORE_PERF_GLOBAL_CTRL;
1546 * 2) Configure 4 PERFEVTSELx with the magic events and clear
1547 * the corresponding PMCx;
1548 * 3) set bit0~bit3 of MSR_CORE_PERF_GLOBAL_CTRL;
1549 * 4) Clear MSR_CORE_PERF_GLOBAL_CTRL;
1550 * 5) Clear 4 pairs of ERFEVTSELx and PMCx;
1554 * The real steps we choose are a little different from above.
1555 * A) To reduce MSR operations, we don't run step 1) as they
1556 * are already cleared before this function is called;
1557 * B) Call x86_perf_event_update to save PMCx before configuring
1558 * PERFEVTSELx with magic number;
1559 * C) With step 5), we do clear only when the PERFEVTSELx is
1560 * not used currently.
1561 * D) Call x86_perf_event_set_period to restore PMCx;
1564 /* We always operate 4 pairs of PERF Counters */
1565 for (i = 0; i < 4; i++) {
1566 event = cpuc->events[i];
1568 x86_perf_event_update(event);
1571 for (i = 0; i < 4; i++) {
1572 wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + i, nhm_magic[i]);
1573 wrmsrl(MSR_ARCH_PERFMON_PERFCTR0 + i, 0x0);
1576 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0xf);
1577 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0x0);
1579 for (i = 0; i < 4; i++) {
1580 event = cpuc->events[i];
1583 x86_perf_event_set_period(event);
1584 __x86_pmu_enable_event(&event->hw,
1585 ARCH_PERFMON_EVENTSEL_ENABLE);
1587 wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + i, 0x0);
1591 static void intel_pmu_nhm_enable_all(int added)
1594 intel_pmu_nhm_workaround();
1595 intel_pmu_enable_all(added);
1598 static inline u64 intel_pmu_get_status(void)
1602 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
1607 static inline void intel_pmu_ack_status(u64 ack)
1609 wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
1612 static void intel_pmu_disable_fixed(struct hw_perf_event *hwc)
1614 int idx = hwc->idx - INTEL_PMC_IDX_FIXED;
1617 mask = 0xfULL << (idx * 4);
1619 rdmsrl(hwc->config_base, ctrl_val);
1621 wrmsrl(hwc->config_base, ctrl_val);
1624 static inline bool event_is_checkpointed(struct perf_event *event)
1626 return (event->hw.config & HSW_IN_TX_CHECKPOINTED) != 0;
1629 static void intel_pmu_disable_event(struct perf_event *event)
1631 struct hw_perf_event *hwc = &event->hw;
1632 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1634 if (unlikely(hwc->idx == INTEL_PMC_IDX_FIXED_BTS)) {
1635 intel_pmu_disable_bts();
1636 intel_pmu_drain_bts_buffer();
1640 cpuc->intel_ctrl_guest_mask &= ~(1ull << hwc->idx);
1641 cpuc->intel_ctrl_host_mask &= ~(1ull << hwc->idx);
1642 cpuc->intel_cp_status &= ~(1ull << hwc->idx);
1645 * must disable before any actual event
1646 * because any event may be combined with LBR
1648 if (needs_branch_stack(event))
1649 intel_pmu_lbr_disable(event);
1651 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
1652 intel_pmu_disable_fixed(hwc);
1656 x86_pmu_disable_event(event);
1658 if (unlikely(event->attr.precise_ip))
1659 intel_pmu_pebs_disable(event);
1662 static void intel_pmu_enable_fixed(struct hw_perf_event *hwc)
1664 int idx = hwc->idx - INTEL_PMC_IDX_FIXED;
1665 u64 ctrl_val, bits, mask;
1668 * Enable IRQ generation (0x8),
1669 * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
1673 if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
1675 if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
1679 * ANY bit is supported in v3 and up
1681 if (x86_pmu.version > 2 && hwc->config & ARCH_PERFMON_EVENTSEL_ANY)
1685 mask = 0xfULL << (idx * 4);
1687 rdmsrl(hwc->config_base, ctrl_val);
1690 wrmsrl(hwc->config_base, ctrl_val);
1693 static void intel_pmu_enable_event(struct perf_event *event)
1695 struct hw_perf_event *hwc = &event->hw;
1696 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1698 if (unlikely(hwc->idx == INTEL_PMC_IDX_FIXED_BTS)) {
1699 if (!__this_cpu_read(cpu_hw_events.enabled))
1702 intel_pmu_enable_bts(hwc->config);
1706 * must enabled before any actual event
1707 * because any event may be combined with LBR
1709 if (needs_branch_stack(event))
1710 intel_pmu_lbr_enable(event);
1712 if (event->attr.exclude_host)
1713 cpuc->intel_ctrl_guest_mask |= (1ull << hwc->idx);
1714 if (event->attr.exclude_guest)
1715 cpuc->intel_ctrl_host_mask |= (1ull << hwc->idx);
1717 if (unlikely(event_is_checkpointed(event)))
1718 cpuc->intel_cp_status |= (1ull << hwc->idx);
1720 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
1721 intel_pmu_enable_fixed(hwc);
1725 if (unlikely(event->attr.precise_ip))
1726 intel_pmu_pebs_enable(event);
1728 __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
1732 * Save and restart an expired event. Called by NMI contexts,
1733 * so it has to be careful about preempting normal event ops:
1735 int intel_pmu_save_and_restart(struct perf_event *event)
1737 x86_perf_event_update(event);
1739 * For a checkpointed counter always reset back to 0. This
1740 * avoids a situation where the counter overflows, aborts the
1741 * transaction and is then set back to shortly before the
1742 * overflow, and overflows and aborts again.
1744 if (unlikely(event_is_checkpointed(event))) {
1745 /* No race with NMIs because the counter should not be armed */
1746 wrmsrl(event->hw.event_base, 0);
1747 local64_set(&event->hw.prev_count, 0);
1749 return x86_perf_event_set_period(event);
1752 static void intel_pmu_reset(void)
1754 struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
1755 unsigned long flags;
1758 if (!x86_pmu.num_counters)
1761 local_irq_save(flags);
1763 pr_info("clearing PMU state on CPU#%d\n", smp_processor_id());
1765 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
1766 wrmsrl_safe(x86_pmu_config_addr(idx), 0ull);
1767 wrmsrl_safe(x86_pmu_event_addr(idx), 0ull);
1769 for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++)
1770 wrmsrl_safe(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull);
1773 ds->bts_index = ds->bts_buffer_base;
1775 /* Ack all overflows and disable fixed counters */
1776 if (x86_pmu.version >= 2) {
1777 intel_pmu_ack_status(intel_pmu_get_status());
1778 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
1781 /* Reset LBRs and LBR freezing */
1782 if (x86_pmu.lbr_nr) {
1783 update_debugctlmsr(get_debugctlmsr() &
1784 ~(DEBUGCTLMSR_FREEZE_LBRS_ON_PMI|DEBUGCTLMSR_LBR));
1787 local_irq_restore(flags);
1791 * This handler is triggered by the local APIC, so the APIC IRQ handling
1794 static int intel_pmu_handle_irq(struct pt_regs *regs)
1796 struct perf_sample_data data;
1797 struct cpu_hw_events *cpuc;
1802 cpuc = this_cpu_ptr(&cpu_hw_events);
1805 * No known reason to not always do late ACK,
1806 * but just in case do it opt-in.
1808 if (!x86_pmu.late_ack)
1809 apic_write(APIC_LVTPC, APIC_DM_NMI);
1810 __intel_pmu_disable_all();
1811 handled = intel_pmu_drain_bts_buffer();
1812 handled += intel_bts_interrupt();
1813 status = intel_pmu_get_status();
1819 intel_pmu_lbr_read();
1820 intel_pmu_ack_status(status);
1821 if (++loops > 100) {
1822 static bool warned = false;
1824 WARN(1, "perfevents: irq loop stuck!\n");
1825 perf_event_print_debug();
1832 inc_irq_stat(apic_perf_irqs);
1836 * Ignore a range of extra bits in status that do not indicate
1837 * overflow by themselves.
1839 status &= ~(GLOBAL_STATUS_COND_CHG |
1840 GLOBAL_STATUS_ASIF |
1841 GLOBAL_STATUS_LBRS_FROZEN);
1846 * PEBS overflow sets bit 62 in the global status register
1848 if (__test_and_clear_bit(62, (unsigned long *)&status)) {
1850 x86_pmu.drain_pebs(regs);
1852 * There are cases where, even though, the PEBS ovfl bit is set
1853 * in GLOBAL_OVF_STATUS, the PEBS events may also have their
1854 * overflow bits set for their counters. We must clear them
1855 * here because they have been processed as exact samples in
1856 * the drain_pebs() routine. They must not be processed again
1857 * in the for_each_bit_set() loop for regular samples below.
1859 status &= ~cpuc->pebs_enabled;
1860 status &= x86_pmu.intel_ctrl | GLOBAL_STATUS_TRACE_TOPAPMI;
1866 if (__test_and_clear_bit(55, (unsigned long *)&status)) {
1868 intel_pt_interrupt();
1872 * Checkpointed counters can lead to 'spurious' PMIs because the
1873 * rollback caused by the PMI will have cleared the overflow status
1874 * bit. Therefore always force probe these counters.
1876 status |= cpuc->intel_cp_status;
1878 for_each_set_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
1879 struct perf_event *event = cpuc->events[bit];
1883 if (!test_bit(bit, cpuc->active_mask))
1886 if (!intel_pmu_save_and_restart(event))
1889 perf_sample_data_init(&data, 0, event->hw.last_period);
1891 if (has_branch_stack(event))
1892 data.br_stack = &cpuc->lbr_stack;
1894 if (perf_event_overflow(event, &data, regs))
1895 x86_pmu_stop(event, 0);
1899 * Repeat if there is more work to be done:
1901 status = intel_pmu_get_status();
1906 /* Only restore PMU state when it's active. See x86_pmu_disable(). */
1908 __intel_pmu_enable_all(0, true);
1911 * Only unmask the NMI after the overflow counters
1912 * have been reset. This avoids spurious NMIs on
1915 if (x86_pmu.late_ack)
1916 apic_write(APIC_LVTPC, APIC_DM_NMI);
1920 static struct event_constraint *
1921 intel_bts_constraints(struct perf_event *event)
1923 struct hw_perf_event *hwc = &event->hw;
1924 unsigned int hw_event, bts_event;
1926 if (event->attr.freq)
1929 hw_event = hwc->config & INTEL_ARCH_EVENT_MASK;
1930 bts_event = x86_pmu.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS);
1932 if (unlikely(hw_event == bts_event && hwc->sample_period == 1))
1933 return &bts_constraint;
1938 static int intel_alt_er(int idx, u64 config)
1941 if (!(x86_pmu.flags & PMU_FL_HAS_RSP_1))
1944 if (idx == EXTRA_REG_RSP_0)
1945 alt_idx = EXTRA_REG_RSP_1;
1947 if (idx == EXTRA_REG_RSP_1)
1948 alt_idx = EXTRA_REG_RSP_0;
1950 if (config & ~x86_pmu.extra_regs[alt_idx].valid_mask)
1956 static void intel_fixup_er(struct perf_event *event, int idx)
1958 event->hw.extra_reg.idx = idx;
1960 if (idx == EXTRA_REG_RSP_0) {
1961 event->hw.config &= ~INTEL_ARCH_EVENT_MASK;
1962 event->hw.config |= x86_pmu.extra_regs[EXTRA_REG_RSP_0].event;
1963 event->hw.extra_reg.reg = MSR_OFFCORE_RSP_0;
1964 } else if (idx == EXTRA_REG_RSP_1) {
1965 event->hw.config &= ~INTEL_ARCH_EVENT_MASK;
1966 event->hw.config |= x86_pmu.extra_regs[EXTRA_REG_RSP_1].event;
1967 event->hw.extra_reg.reg = MSR_OFFCORE_RSP_1;
1972 * manage allocation of shared extra msr for certain events
1975 * per-cpu: to be shared between the various events on a single PMU
1976 * per-core: per-cpu + shared by HT threads
1978 static struct event_constraint *
1979 __intel_shared_reg_get_constraints(struct cpu_hw_events *cpuc,
1980 struct perf_event *event,
1981 struct hw_perf_event_extra *reg)
1983 struct event_constraint *c = &emptyconstraint;
1984 struct er_account *era;
1985 unsigned long flags;
1989 * reg->alloc can be set due to existing state, so for fake cpuc we
1990 * need to ignore this, otherwise we might fail to allocate proper fake
1991 * state for this extra reg constraint. Also see the comment below.
1993 if (reg->alloc && !cpuc->is_fake)
1994 return NULL; /* call x86_get_event_constraint() */
1997 era = &cpuc->shared_regs->regs[idx];
1999 * we use spin_lock_irqsave() to avoid lockdep issues when
2000 * passing a fake cpuc
2002 raw_spin_lock_irqsave(&era->lock, flags);
2004 if (!atomic_read(&era->ref) || era->config == reg->config) {
2007 * If its a fake cpuc -- as per validate_{group,event}() we
2008 * shouldn't touch event state and we can avoid doing so
2009 * since both will only call get_event_constraints() once
2010 * on each event, this avoids the need for reg->alloc.
2012 * Not doing the ER fixup will only result in era->reg being
2013 * wrong, but since we won't actually try and program hardware
2014 * this isn't a problem either.
2016 if (!cpuc->is_fake) {
2017 if (idx != reg->idx)
2018 intel_fixup_er(event, idx);
2021 * x86_schedule_events() can call get_event_constraints()
2022 * multiple times on events in the case of incremental
2023 * scheduling(). reg->alloc ensures we only do the ER
2029 /* lock in msr value */
2030 era->config = reg->config;
2031 era->reg = reg->reg;
2034 atomic_inc(&era->ref);
2037 * need to call x86_get_event_constraint()
2038 * to check if associated event has constraints
2042 idx = intel_alt_er(idx, reg->config);
2043 if (idx != reg->idx) {
2044 raw_spin_unlock_irqrestore(&era->lock, flags);
2048 raw_spin_unlock_irqrestore(&era->lock, flags);
2054 __intel_shared_reg_put_constraints(struct cpu_hw_events *cpuc,
2055 struct hw_perf_event_extra *reg)
2057 struct er_account *era;
2060 * Only put constraint if extra reg was actually allocated. Also takes
2061 * care of event which do not use an extra shared reg.
2063 * Also, if this is a fake cpuc we shouldn't touch any event state
2064 * (reg->alloc) and we don't care about leaving inconsistent cpuc state
2065 * either since it'll be thrown out.
2067 if (!reg->alloc || cpuc->is_fake)
2070 era = &cpuc->shared_regs->regs[reg->idx];
2072 /* one fewer user */
2073 atomic_dec(&era->ref);
2075 /* allocate again next time */
2079 static struct event_constraint *
2080 intel_shared_regs_constraints(struct cpu_hw_events *cpuc,
2081 struct perf_event *event)
2083 struct event_constraint *c = NULL, *d;
2084 struct hw_perf_event_extra *xreg, *breg;
2086 xreg = &event->hw.extra_reg;
2087 if (xreg->idx != EXTRA_REG_NONE) {
2088 c = __intel_shared_reg_get_constraints(cpuc, event, xreg);
2089 if (c == &emptyconstraint)
2092 breg = &event->hw.branch_reg;
2093 if (breg->idx != EXTRA_REG_NONE) {
2094 d = __intel_shared_reg_get_constraints(cpuc, event, breg);
2095 if (d == &emptyconstraint) {
2096 __intel_shared_reg_put_constraints(cpuc, xreg);
2103 struct event_constraint *
2104 x86_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
2105 struct perf_event *event)
2107 struct event_constraint *c;
2109 if (x86_pmu.event_constraints) {
2110 for_each_event_constraint(c, x86_pmu.event_constraints) {
2111 if ((event->hw.config & c->cmask) == c->code) {
2112 event->hw.flags |= c->flags;
2118 return &unconstrained;
2121 static struct event_constraint *
2122 __intel_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
2123 struct perf_event *event)
2125 struct event_constraint *c;
2127 c = intel_bts_constraints(event);
2131 c = intel_shared_regs_constraints(cpuc, event);
2135 c = intel_pebs_constraints(event);
2139 return x86_get_event_constraints(cpuc, idx, event);
2143 intel_start_scheduling(struct cpu_hw_events *cpuc)
2145 struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
2146 struct intel_excl_states *xl;
2147 int tid = cpuc->excl_thread_id;
2150 * nothing needed if in group validation mode
2152 if (cpuc->is_fake || !is_ht_workaround_enabled())
2156 * no exclusion needed
2158 if (WARN_ON_ONCE(!excl_cntrs))
2161 xl = &excl_cntrs->states[tid];
2163 xl->sched_started = true;
2165 * lock shared state until we are done scheduling
2166 * in stop_event_scheduling()
2167 * makes scheduling appear as a transaction
2169 raw_spin_lock(&excl_cntrs->lock);
2172 static void intel_commit_scheduling(struct cpu_hw_events *cpuc, int idx, int cntr)
2174 struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
2175 struct event_constraint *c = cpuc->event_constraint[idx];
2176 struct intel_excl_states *xl;
2177 int tid = cpuc->excl_thread_id;
2179 if (cpuc->is_fake || !is_ht_workaround_enabled())
2182 if (WARN_ON_ONCE(!excl_cntrs))
2185 if (!(c->flags & PERF_X86_EVENT_DYNAMIC))
2188 xl = &excl_cntrs->states[tid];
2190 lockdep_assert_held(&excl_cntrs->lock);
2192 if (c->flags & PERF_X86_EVENT_EXCL)
2193 xl->state[cntr] = INTEL_EXCL_EXCLUSIVE;
2195 xl->state[cntr] = INTEL_EXCL_SHARED;
2199 intel_stop_scheduling(struct cpu_hw_events *cpuc)
2201 struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
2202 struct intel_excl_states *xl;
2203 int tid = cpuc->excl_thread_id;
2206 * nothing needed if in group validation mode
2208 if (cpuc->is_fake || !is_ht_workaround_enabled())
2211 * no exclusion needed
2213 if (WARN_ON_ONCE(!excl_cntrs))
2216 xl = &excl_cntrs->states[tid];
2218 xl->sched_started = false;
2220 * release shared state lock (acquired in intel_start_scheduling())
2222 raw_spin_unlock(&excl_cntrs->lock);
2225 static struct event_constraint *
2226 intel_get_excl_constraints(struct cpu_hw_events *cpuc, struct perf_event *event,
2227 int idx, struct event_constraint *c)
2229 struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
2230 struct intel_excl_states *xlo;
2231 int tid = cpuc->excl_thread_id;
2235 * validating a group does not require
2236 * enforcing cross-thread exclusion
2238 if (cpuc->is_fake || !is_ht_workaround_enabled())
2242 * no exclusion needed
2244 if (WARN_ON_ONCE(!excl_cntrs))
2248 * because we modify the constraint, we need
2249 * to make a copy. Static constraints come
2250 * from static const tables.
2252 * only needed when constraint has not yet
2253 * been cloned (marked dynamic)
2255 if (!(c->flags & PERF_X86_EVENT_DYNAMIC)) {
2256 struct event_constraint *cx;
2259 * grab pre-allocated constraint entry
2261 cx = &cpuc->constraint_list[idx];
2264 * initialize dynamic constraint
2265 * with static constraint
2270 * mark constraint as dynamic, so we
2271 * can free it later on
2273 cx->flags |= PERF_X86_EVENT_DYNAMIC;
2278 * From here on, the constraint is dynamic.
2279 * Either it was just allocated above, or it
2280 * was allocated during a earlier invocation
2285 * state of sibling HT
2287 xlo = &excl_cntrs->states[tid ^ 1];
2290 * event requires exclusive counter access
2293 is_excl = c->flags & PERF_X86_EVENT_EXCL;
2294 if (is_excl && !(event->hw.flags & PERF_X86_EVENT_EXCL_ACCT)) {
2295 event->hw.flags |= PERF_X86_EVENT_EXCL_ACCT;
2296 if (!cpuc->n_excl++)
2297 WRITE_ONCE(excl_cntrs->has_exclusive[tid], 1);
2301 * Modify static constraint with current dynamic
2304 * EXCLUSIVE: sibling counter measuring exclusive event
2305 * SHARED : sibling counter measuring non-exclusive event
2306 * UNUSED : sibling counter unused
2308 for_each_set_bit(i, c->idxmsk, X86_PMC_IDX_MAX) {
2310 * exclusive event in sibling counter
2311 * our corresponding counter cannot be used
2312 * regardless of our event
2314 if (xlo->state[i] == INTEL_EXCL_EXCLUSIVE)
2315 __clear_bit(i, c->idxmsk);
2317 * if measuring an exclusive event, sibling
2318 * measuring non-exclusive, then counter cannot
2321 if (is_excl && xlo->state[i] == INTEL_EXCL_SHARED)
2322 __clear_bit(i, c->idxmsk);
2326 * recompute actual bit weight for scheduling algorithm
2328 c->weight = hweight64(c->idxmsk64);
2331 * if we return an empty mask, then switch
2332 * back to static empty constraint to avoid
2333 * the cost of freeing later on
2336 c = &emptyconstraint;
2341 static struct event_constraint *
2342 intel_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
2343 struct perf_event *event)
2345 struct event_constraint *c1 = NULL;
2346 struct event_constraint *c2;
2348 if (idx >= 0) /* fake does < 0 */
2349 c1 = cpuc->event_constraint[idx];
2353 * - static constraint: no change across incremental scheduling calls
2354 * - dynamic constraint: handled by intel_get_excl_constraints()
2356 c2 = __intel_get_event_constraints(cpuc, idx, event);
2357 if (c1 && (c1->flags & PERF_X86_EVENT_DYNAMIC)) {
2358 bitmap_copy(c1->idxmsk, c2->idxmsk, X86_PMC_IDX_MAX);
2359 c1->weight = c2->weight;
2363 if (cpuc->excl_cntrs)
2364 return intel_get_excl_constraints(cpuc, event, idx, c2);
2369 static void intel_put_excl_constraints(struct cpu_hw_events *cpuc,
2370 struct perf_event *event)
2372 struct hw_perf_event *hwc = &event->hw;
2373 struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
2374 int tid = cpuc->excl_thread_id;
2375 struct intel_excl_states *xl;
2378 * nothing needed if in group validation mode
2383 if (WARN_ON_ONCE(!excl_cntrs))
2386 if (hwc->flags & PERF_X86_EVENT_EXCL_ACCT) {
2387 hwc->flags &= ~PERF_X86_EVENT_EXCL_ACCT;
2388 if (!--cpuc->n_excl)
2389 WRITE_ONCE(excl_cntrs->has_exclusive[tid], 0);
2393 * If event was actually assigned, then mark the counter state as
2396 if (hwc->idx >= 0) {
2397 xl = &excl_cntrs->states[tid];
2400 * put_constraint may be called from x86_schedule_events()
2401 * which already has the lock held so here make locking
2404 if (!xl->sched_started)
2405 raw_spin_lock(&excl_cntrs->lock);
2407 xl->state[hwc->idx] = INTEL_EXCL_UNUSED;
2409 if (!xl->sched_started)
2410 raw_spin_unlock(&excl_cntrs->lock);
2415 intel_put_shared_regs_event_constraints(struct cpu_hw_events *cpuc,
2416 struct perf_event *event)
2418 struct hw_perf_event_extra *reg;
2420 reg = &event->hw.extra_reg;
2421 if (reg->idx != EXTRA_REG_NONE)
2422 __intel_shared_reg_put_constraints(cpuc, reg);
2424 reg = &event->hw.branch_reg;
2425 if (reg->idx != EXTRA_REG_NONE)
2426 __intel_shared_reg_put_constraints(cpuc, reg);
2429 static void intel_put_event_constraints(struct cpu_hw_events *cpuc,
2430 struct perf_event *event)
2432 intel_put_shared_regs_event_constraints(cpuc, event);
2435 * is PMU has exclusive counter restrictions, then
2436 * all events are subject to and must call the
2437 * put_excl_constraints() routine
2439 if (cpuc->excl_cntrs)
2440 intel_put_excl_constraints(cpuc, event);
2443 static void intel_pebs_aliases_core2(struct perf_event *event)
2445 if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
2447 * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P
2448 * (0x003c) so that we can use it with PEBS.
2450 * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't
2451 * PEBS capable. However we can use INST_RETIRED.ANY_P
2452 * (0x00c0), which is a PEBS capable event, to get the same
2455 * INST_RETIRED.ANY_P counts the number of cycles that retires
2456 * CNTMASK instructions. By setting CNTMASK to a value (16)
2457 * larger than the maximum number of instructions that can be
2458 * retired per cycle (4) and then inverting the condition, we
2459 * count all cycles that retire 16 or less instructions, which
2462 * Thereby we gain a PEBS capable cycle counter.
2464 u64 alt_config = X86_CONFIG(.event=0xc0, .inv=1, .cmask=16);
2466 alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
2467 event->hw.config = alt_config;
2471 static void intel_pebs_aliases_snb(struct perf_event *event)
2473 if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
2475 * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P
2476 * (0x003c) so that we can use it with PEBS.
2478 * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't
2479 * PEBS capable. However we can use UOPS_RETIRED.ALL
2480 * (0x01c2), which is a PEBS capable event, to get the same
2483 * UOPS_RETIRED.ALL counts the number of cycles that retires
2484 * CNTMASK micro-ops. By setting CNTMASK to a value (16)
2485 * larger than the maximum number of micro-ops that can be
2486 * retired per cycle (4) and then inverting the condition, we
2487 * count all cycles that retire 16 or less micro-ops, which
2490 * Thereby we gain a PEBS capable cycle counter.
2492 u64 alt_config = X86_CONFIG(.event=0xc2, .umask=0x01, .inv=1, .cmask=16);
2494 alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
2495 event->hw.config = alt_config;
2499 static unsigned long intel_pmu_free_running_flags(struct perf_event *event)
2501 unsigned long flags = x86_pmu.free_running_flags;
2503 if (event->attr.use_clockid)
2504 flags &= ~PERF_SAMPLE_TIME;
2508 static int intel_pmu_hw_config(struct perf_event *event)
2510 int ret = x86_pmu_hw_config(event);
2515 if (event->attr.precise_ip) {
2516 if (!event->attr.freq) {
2517 event->hw.flags |= PERF_X86_EVENT_AUTO_RELOAD;
2518 if (!(event->attr.sample_type &
2519 ~intel_pmu_free_running_flags(event)))
2520 event->hw.flags |= PERF_X86_EVENT_FREERUNNING;
2522 if (x86_pmu.pebs_aliases)
2523 x86_pmu.pebs_aliases(event);
2526 if (needs_branch_stack(event)) {
2527 ret = intel_pmu_setup_lbr_filter(event);
2532 * BTS is set up earlier in this path, so don't account twice
2534 if (!intel_pmu_has_bts(event)) {
2535 /* disallow lbr if conflicting events are present */
2536 if (x86_add_exclusive(x86_lbr_exclusive_lbr))
2539 event->destroy = hw_perf_lbr_event_destroy;
2543 if (event->attr.type != PERF_TYPE_RAW)
2546 if (!(event->attr.config & ARCH_PERFMON_EVENTSEL_ANY))
2549 if (x86_pmu.version < 3)
2552 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2555 event->hw.config |= ARCH_PERFMON_EVENTSEL_ANY;
2560 struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr)
2562 if (x86_pmu.guest_get_msrs)
2563 return x86_pmu.guest_get_msrs(nr);
2567 EXPORT_SYMBOL_GPL(perf_guest_get_msrs);
2569 static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr)
2571 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2572 struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;
2574 arr[0].msr = MSR_CORE_PERF_GLOBAL_CTRL;
2575 arr[0].host = x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_guest_mask;
2576 arr[0].guest = x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_host_mask;
2578 * If PMU counter has PEBS enabled it is not enough to disable counter
2579 * on a guest entry since PEBS memory write can overshoot guest entry
2580 * and corrupt guest memory. Disabling PEBS solves the problem.
2582 arr[1].msr = MSR_IA32_PEBS_ENABLE;
2583 arr[1].host = cpuc->pebs_enabled;
2590 static struct perf_guest_switch_msr *core_guest_get_msrs(int *nr)
2592 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2593 struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;
2596 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
2597 struct perf_event *event = cpuc->events[idx];
2599 arr[idx].msr = x86_pmu_config_addr(idx);
2600 arr[idx].host = arr[idx].guest = 0;
2602 if (!test_bit(idx, cpuc->active_mask))
2605 arr[idx].host = arr[idx].guest =
2606 event->hw.config | ARCH_PERFMON_EVENTSEL_ENABLE;
2608 if (event->attr.exclude_host)
2609 arr[idx].host &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
2610 else if (event->attr.exclude_guest)
2611 arr[idx].guest &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
2614 *nr = x86_pmu.num_counters;
2618 static void core_pmu_enable_event(struct perf_event *event)
2620 if (!event->attr.exclude_host)
2621 x86_pmu_enable_event(event);
2624 static void core_pmu_enable_all(int added)
2626 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2629 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
2630 struct hw_perf_event *hwc = &cpuc->events[idx]->hw;
2632 if (!test_bit(idx, cpuc->active_mask) ||
2633 cpuc->events[idx]->attr.exclude_host)
2636 __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
2640 static int hsw_hw_config(struct perf_event *event)
2642 int ret = intel_pmu_hw_config(event);
2646 if (!boot_cpu_has(X86_FEATURE_RTM) && !boot_cpu_has(X86_FEATURE_HLE))
2648 event->hw.config |= event->attr.config & (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED);
2651 * IN_TX/IN_TX-CP filters are not supported by the Haswell PMU with
2652 * PEBS or in ANY thread mode. Since the results are non-sensical forbid
2655 if ((event->hw.config & (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED)) &&
2656 ((event->hw.config & ARCH_PERFMON_EVENTSEL_ANY) ||
2657 event->attr.precise_ip > 0))
2660 if (event_is_checkpointed(event)) {
2662 * Sampling of checkpointed events can cause situations where
2663 * the CPU constantly aborts because of a overflow, which is
2664 * then checkpointed back and ignored. Forbid checkpointing
2667 * But still allow a long sampling period, so that perf stat
2670 if (event->attr.sample_period > 0 &&
2671 event->attr.sample_period < 0x7fffffff)
2677 static struct event_constraint counter2_constraint =
2678 EVENT_CONSTRAINT(0, 0x4, 0);
2680 static struct event_constraint *
2681 hsw_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
2682 struct perf_event *event)
2684 struct event_constraint *c;
2686 c = intel_get_event_constraints(cpuc, idx, event);
2688 /* Handle special quirk on in_tx_checkpointed only in counter 2 */
2689 if (event->hw.config & HSW_IN_TX_CHECKPOINTED) {
2690 if (c->idxmsk64 & (1U << 2))
2691 return &counter2_constraint;
2692 return &emptyconstraint;
2701 * The INST_RETIRED.ALL period always needs to have lowest 6 bits cleared
2702 * (BDM55) and it must not use a period smaller than 100 (BDM11). We combine
2703 * the two to enforce a minimum period of 128 (the smallest value that has bits
2704 * 0-5 cleared and >= 100).
2706 * Because of how the code in x86_perf_event_set_period() works, the truncation
2707 * of the lower 6 bits is 'harmless' as we'll occasionally add a longer period
2708 * to make up for the 'lost' events due to carrying the 'error' in period_left.
2710 * Therefore the effective (average) period matches the requested period,
2711 * despite coarser hardware granularity.
2713 static unsigned bdw_limit_period(struct perf_event *event, unsigned left)
2715 if ((event->hw.config & INTEL_ARCH_EVENT_MASK) ==
2716 X86_CONFIG(.event=0xc0, .umask=0x01)) {
2724 PMU_FORMAT_ATTR(event, "config:0-7" );
2725 PMU_FORMAT_ATTR(umask, "config:8-15" );
2726 PMU_FORMAT_ATTR(edge, "config:18" );
2727 PMU_FORMAT_ATTR(pc, "config:19" );
2728 PMU_FORMAT_ATTR(any, "config:21" ); /* v3 + */
2729 PMU_FORMAT_ATTR(inv, "config:23" );
2730 PMU_FORMAT_ATTR(cmask, "config:24-31" );
2731 PMU_FORMAT_ATTR(in_tx, "config:32");
2732 PMU_FORMAT_ATTR(in_tx_cp, "config:33");
2734 static struct attribute *intel_arch_formats_attr[] = {
2735 &format_attr_event.attr,
2736 &format_attr_umask.attr,
2737 &format_attr_edge.attr,
2738 &format_attr_pc.attr,
2739 &format_attr_inv.attr,
2740 &format_attr_cmask.attr,
2744 ssize_t intel_event_sysfs_show(char *page, u64 config)
2746 u64 event = (config & ARCH_PERFMON_EVENTSEL_EVENT);
2748 return x86_event_sysfs_show(page, config, event);
2751 struct intel_shared_regs *allocate_shared_regs(int cpu)
2753 struct intel_shared_regs *regs;
2756 regs = kzalloc_node(sizeof(struct intel_shared_regs),
2757 GFP_KERNEL, cpu_to_node(cpu));
2760 * initialize the locks to keep lockdep happy
2762 for (i = 0; i < EXTRA_REG_MAX; i++)
2763 raw_spin_lock_init(®s->regs[i].lock);
2770 static struct intel_excl_cntrs *allocate_excl_cntrs(int cpu)
2772 struct intel_excl_cntrs *c;
2774 c = kzalloc_node(sizeof(struct intel_excl_cntrs),
2775 GFP_KERNEL, cpu_to_node(cpu));
2777 raw_spin_lock_init(&c->lock);
2783 static int intel_pmu_cpu_prepare(int cpu)
2785 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
2787 if (x86_pmu.extra_regs || x86_pmu.lbr_sel_map) {
2788 cpuc->shared_regs = allocate_shared_regs(cpu);
2789 if (!cpuc->shared_regs)
2793 if (x86_pmu.flags & PMU_FL_EXCL_CNTRS) {
2794 size_t sz = X86_PMC_IDX_MAX * sizeof(struct event_constraint);
2796 cpuc->constraint_list = kzalloc(sz, GFP_KERNEL);
2797 if (!cpuc->constraint_list)
2798 goto err_shared_regs;
2800 cpuc->excl_cntrs = allocate_excl_cntrs(cpu);
2801 if (!cpuc->excl_cntrs)
2802 goto err_constraint_list;
2804 cpuc->excl_thread_id = 0;
2809 err_constraint_list:
2810 kfree(cpuc->constraint_list);
2811 cpuc->constraint_list = NULL;
2814 kfree(cpuc->shared_regs);
2815 cpuc->shared_regs = NULL;
2821 static void intel_pmu_cpu_starting(int cpu)
2823 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
2824 int core_id = topology_core_id(cpu);
2827 init_debug_store_on_cpu(cpu);
2829 * Deal with CPUs that don't clear their LBRs on power-up.
2831 intel_pmu_lbr_reset();
2833 cpuc->lbr_sel = NULL;
2835 if (!cpuc->shared_regs)
2838 if (!(x86_pmu.flags & PMU_FL_NO_HT_SHARING)) {
2839 void **onln = &cpuc->kfree_on_online[X86_PERF_KFREE_SHARED];
2841 for_each_cpu(i, topology_sibling_cpumask(cpu)) {
2842 struct intel_shared_regs *pc;
2844 pc = per_cpu(cpu_hw_events, i).shared_regs;
2845 if (pc && pc->core_id == core_id) {
2846 *onln = cpuc->shared_regs;
2847 cpuc->shared_regs = pc;
2851 cpuc->shared_regs->core_id = core_id;
2852 cpuc->shared_regs->refcnt++;
2855 if (x86_pmu.lbr_sel_map)
2856 cpuc->lbr_sel = &cpuc->shared_regs->regs[EXTRA_REG_LBR];
2858 if (x86_pmu.flags & PMU_FL_EXCL_CNTRS) {
2859 for_each_cpu(i, topology_sibling_cpumask(cpu)) {
2860 struct intel_excl_cntrs *c;
2862 c = per_cpu(cpu_hw_events, i).excl_cntrs;
2863 if (c && c->core_id == core_id) {
2864 cpuc->kfree_on_online[1] = cpuc->excl_cntrs;
2865 cpuc->excl_cntrs = c;
2866 cpuc->excl_thread_id = 1;
2870 cpuc->excl_cntrs->core_id = core_id;
2871 cpuc->excl_cntrs->refcnt++;
2875 static void free_excl_cntrs(int cpu)
2877 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
2878 struct intel_excl_cntrs *c;
2880 c = cpuc->excl_cntrs;
2882 if (c->core_id == -1 || --c->refcnt == 0)
2884 cpuc->excl_cntrs = NULL;
2885 kfree(cpuc->constraint_list);
2886 cpuc->constraint_list = NULL;
2890 static void intel_pmu_cpu_dying(int cpu)
2892 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
2893 struct intel_shared_regs *pc;
2895 pc = cpuc->shared_regs;
2897 if (pc->core_id == -1 || --pc->refcnt == 0)
2899 cpuc->shared_regs = NULL;
2902 free_excl_cntrs(cpu);
2904 fini_debug_store_on_cpu(cpu);
2907 static void intel_pmu_sched_task(struct perf_event_context *ctx,
2910 if (x86_pmu.pebs_active)
2911 intel_pmu_pebs_sched_task(ctx, sched_in);
2913 intel_pmu_lbr_sched_task(ctx, sched_in);
2916 PMU_FORMAT_ATTR(offcore_rsp, "config1:0-63");
2918 PMU_FORMAT_ATTR(ldlat, "config1:0-15");
2920 PMU_FORMAT_ATTR(frontend, "config1:0-23");
2922 static struct attribute *intel_arch3_formats_attr[] = {
2923 &format_attr_event.attr,
2924 &format_attr_umask.attr,
2925 &format_attr_edge.attr,
2926 &format_attr_pc.attr,
2927 &format_attr_any.attr,
2928 &format_attr_inv.attr,
2929 &format_attr_cmask.attr,
2930 &format_attr_in_tx.attr,
2931 &format_attr_in_tx_cp.attr,
2933 &format_attr_offcore_rsp.attr, /* XXX do NHM/WSM + SNB breakout */
2934 &format_attr_ldlat.attr, /* PEBS load latency */
2938 static struct attribute *skl_format_attr[] = {
2939 &format_attr_frontend.attr,
2943 static __initconst const struct x86_pmu core_pmu = {
2945 .handle_irq = x86_pmu_handle_irq,
2946 .disable_all = x86_pmu_disable_all,
2947 .enable_all = core_pmu_enable_all,
2948 .enable = core_pmu_enable_event,
2949 .disable = x86_pmu_disable_event,
2950 .hw_config = x86_pmu_hw_config,
2951 .schedule_events = x86_schedule_events,
2952 .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
2953 .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
2954 .event_map = intel_pmu_event_map,
2955 .max_events = ARRAY_SIZE(intel_perfmon_event_map),
2957 .free_running_flags = PEBS_FREERUNNING_FLAGS,
2960 * Intel PMCs cannot be accessed sanely above 32-bit width,
2961 * so we install an artificial 1<<31 period regardless of
2962 * the generic event period:
2964 .max_period = (1ULL<<31) - 1,
2965 .get_event_constraints = intel_get_event_constraints,
2966 .put_event_constraints = intel_put_event_constraints,
2967 .event_constraints = intel_core_event_constraints,
2968 .guest_get_msrs = core_guest_get_msrs,
2969 .format_attrs = intel_arch_formats_attr,
2970 .events_sysfs_show = intel_event_sysfs_show,
2973 * Virtual (or funny metal) CPU can define x86_pmu.extra_regs
2974 * together with PMU version 1 and thus be using core_pmu with
2975 * shared_regs. We need following callbacks here to allocate
2978 .cpu_prepare = intel_pmu_cpu_prepare,
2979 .cpu_starting = intel_pmu_cpu_starting,
2980 .cpu_dying = intel_pmu_cpu_dying,
2983 static __initconst const struct x86_pmu intel_pmu = {
2985 .handle_irq = intel_pmu_handle_irq,
2986 .disable_all = intel_pmu_disable_all,
2987 .enable_all = intel_pmu_enable_all,
2988 .enable = intel_pmu_enable_event,
2989 .disable = intel_pmu_disable_event,
2990 .hw_config = intel_pmu_hw_config,
2991 .schedule_events = x86_schedule_events,
2992 .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
2993 .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
2994 .event_map = intel_pmu_event_map,
2995 .max_events = ARRAY_SIZE(intel_perfmon_event_map),
2997 .free_running_flags = PEBS_FREERUNNING_FLAGS,
2999 * Intel PMCs cannot be accessed sanely above 32 bit width,
3000 * so we install an artificial 1<<31 period regardless of
3001 * the generic event period:
3003 .max_period = (1ULL << 31) - 1,
3004 .get_event_constraints = intel_get_event_constraints,
3005 .put_event_constraints = intel_put_event_constraints,
3006 .pebs_aliases = intel_pebs_aliases_core2,
3008 .format_attrs = intel_arch3_formats_attr,
3009 .events_sysfs_show = intel_event_sysfs_show,
3011 .cpu_prepare = intel_pmu_cpu_prepare,
3012 .cpu_starting = intel_pmu_cpu_starting,
3013 .cpu_dying = intel_pmu_cpu_dying,
3014 .guest_get_msrs = intel_guest_get_msrs,
3015 .sched_task = intel_pmu_sched_task,
3018 static __init void intel_clovertown_quirk(void)
3021 * PEBS is unreliable due to:
3023 * AJ67 - PEBS may experience CPL leaks
3024 * AJ68 - PEBS PMI may be delayed by one event
3025 * AJ69 - GLOBAL_STATUS[62] will only be set when DEBUGCTL[12]
3026 * AJ106 - FREEZE_LBRS_ON_PMI doesn't work in combination with PEBS
3028 * AJ67 could be worked around by restricting the OS/USR flags.
3029 * AJ69 could be worked around by setting PMU_FREEZE_ON_PMI.
3031 * AJ106 could possibly be worked around by not allowing LBR
3032 * usage from PEBS, including the fixup.
3033 * AJ68 could possibly be worked around by always programming
3034 * a pebs_event_reset[0] value and coping with the lost events.
3036 * But taken together it might just make sense to not enable PEBS on
3039 pr_warn("PEBS disabled due to CPU errata\n");
3041 x86_pmu.pebs_constraints = NULL;
3044 static int intel_snb_pebs_broken(int cpu)
3046 u32 rev = UINT_MAX; /* default to broken for unknown models */
3048 switch (cpu_data(cpu).x86_model) {
3053 case 45: /* SNB-EP */
3054 switch (cpu_data(cpu).x86_mask) {
3055 case 6: rev = 0x618; break;
3056 case 7: rev = 0x70c; break;
3060 return (cpu_data(cpu).microcode < rev);
3063 static void intel_snb_check_microcode(void)
3065 int pebs_broken = 0;
3069 for_each_online_cpu(cpu) {
3070 if ((pebs_broken = intel_snb_pebs_broken(cpu)))
3075 if (pebs_broken == x86_pmu.pebs_broken)
3079 * Serialized by the microcode lock..
3081 if (x86_pmu.pebs_broken) {
3082 pr_info("PEBS enabled due to microcode update\n");
3083 x86_pmu.pebs_broken = 0;
3085 pr_info("PEBS disabled due to CPU errata, please upgrade microcode\n");
3086 x86_pmu.pebs_broken = 1;
3091 * Under certain circumstances, access certain MSR may cause #GP.
3092 * The function tests if the input MSR can be safely accessed.
3094 static bool check_msr(unsigned long msr, u64 mask)
3096 u64 val_old, val_new, val_tmp;
3099 * Read the current value, change it and read it back to see if it
3100 * matches, this is needed to detect certain hardware emulators
3101 * (qemu/kvm) that don't trap on the MSR access and always return 0s.
3103 if (rdmsrl_safe(msr, &val_old))
3107 * Only change the bits which can be updated by wrmsrl.
3109 val_tmp = val_old ^ mask;
3110 if (wrmsrl_safe(msr, val_tmp) ||
3111 rdmsrl_safe(msr, &val_new))
3114 if (val_new != val_tmp)
3117 /* Here it's sure that the MSR can be safely accessed.
3118 * Restore the old value and return.
3120 wrmsrl(msr, val_old);
3125 static __init void intel_sandybridge_quirk(void)
3127 x86_pmu.check_microcode = intel_snb_check_microcode;
3128 intel_snb_check_microcode();
3131 static const struct { int id; char *name; } intel_arch_events_map[] __initconst = {
3132 { PERF_COUNT_HW_CPU_CYCLES, "cpu cycles" },
3133 { PERF_COUNT_HW_INSTRUCTIONS, "instructions" },
3134 { PERF_COUNT_HW_BUS_CYCLES, "bus cycles" },
3135 { PERF_COUNT_HW_CACHE_REFERENCES, "cache references" },
3136 { PERF_COUNT_HW_CACHE_MISSES, "cache misses" },
3137 { PERF_COUNT_HW_BRANCH_INSTRUCTIONS, "branch instructions" },
3138 { PERF_COUNT_HW_BRANCH_MISSES, "branch misses" },
3141 static __init void intel_arch_events_quirk(void)
3145 /* disable event that reported as not presend by cpuid */
3146 for_each_set_bit(bit, x86_pmu.events_mask, ARRAY_SIZE(intel_arch_events_map)) {
3147 intel_perfmon_event_map[intel_arch_events_map[bit].id] = 0;
3148 pr_warn("CPUID marked event: \'%s\' unavailable\n",
3149 intel_arch_events_map[bit].name);
3153 static __init void intel_nehalem_quirk(void)
3155 union cpuid10_ebx ebx;
3157 ebx.full = x86_pmu.events_maskl;
3158 if (ebx.split.no_branch_misses_retired) {
3160 * Erratum AAJ80 detected, we work it around by using
3161 * the BR_MISP_EXEC.ANY event. This will over-count
3162 * branch-misses, but it's still much better than the
3163 * architectural event which is often completely bogus:
3165 intel_perfmon_event_map[PERF_COUNT_HW_BRANCH_MISSES] = 0x7f89;
3166 ebx.split.no_branch_misses_retired = 0;
3167 x86_pmu.events_maskl = ebx.full;
3168 pr_info("CPU erratum AAJ80 worked around\n");
3173 * enable software workaround for errata:
3178 * Only needed when HT is enabled. However detecting
3179 * if HT is enabled is difficult (model specific). So instead,
3180 * we enable the workaround in the early boot, and verify if
3181 * it is needed in a later initcall phase once we have valid
3182 * topology information to check if HT is actually enabled
3184 static __init void intel_ht_bug(void)
3186 x86_pmu.flags |= PMU_FL_EXCL_CNTRS | PMU_FL_EXCL_ENABLED;
3188 x86_pmu.start_scheduling = intel_start_scheduling;
3189 x86_pmu.commit_scheduling = intel_commit_scheduling;
3190 x86_pmu.stop_scheduling = intel_stop_scheduling;
3193 EVENT_ATTR_STR(mem-loads, mem_ld_hsw, "event=0xcd,umask=0x1,ldlat=3");
3194 EVENT_ATTR_STR(mem-stores, mem_st_hsw, "event=0xd0,umask=0x82")
3196 /* Haswell special events */
3197 EVENT_ATTR_STR(tx-start, tx_start, "event=0xc9,umask=0x1");
3198 EVENT_ATTR_STR(tx-commit, tx_commit, "event=0xc9,umask=0x2");
3199 EVENT_ATTR_STR(tx-abort, tx_abort, "event=0xc9,umask=0x4");
3200 EVENT_ATTR_STR(tx-capacity, tx_capacity, "event=0x54,umask=0x2");
3201 EVENT_ATTR_STR(tx-conflict, tx_conflict, "event=0x54,umask=0x1");
3202 EVENT_ATTR_STR(el-start, el_start, "event=0xc8,umask=0x1");
3203 EVENT_ATTR_STR(el-commit, el_commit, "event=0xc8,umask=0x2");
3204 EVENT_ATTR_STR(el-abort, el_abort, "event=0xc8,umask=0x4");
3205 EVENT_ATTR_STR(el-capacity, el_capacity, "event=0x54,umask=0x2");
3206 EVENT_ATTR_STR(el-conflict, el_conflict, "event=0x54,umask=0x1");
3207 EVENT_ATTR_STR(cycles-t, cycles_t, "event=0x3c,in_tx=1");
3208 EVENT_ATTR_STR(cycles-ct, cycles_ct, "event=0x3c,in_tx=1,in_tx_cp=1");
3210 static struct attribute *hsw_events_attrs[] = {
3211 EVENT_PTR(tx_start),
3212 EVENT_PTR(tx_commit),
3213 EVENT_PTR(tx_abort),
3214 EVENT_PTR(tx_capacity),
3215 EVENT_PTR(tx_conflict),
3216 EVENT_PTR(el_start),
3217 EVENT_PTR(el_commit),
3218 EVENT_PTR(el_abort),
3219 EVENT_PTR(el_capacity),
3220 EVENT_PTR(el_conflict),
3221 EVENT_PTR(cycles_t),
3222 EVENT_PTR(cycles_ct),
3223 EVENT_PTR(mem_ld_hsw),
3224 EVENT_PTR(mem_st_hsw),
3228 __init int intel_pmu_init(void)
3230 union cpuid10_edx edx;
3231 union cpuid10_eax eax;
3232 union cpuid10_ebx ebx;
3233 struct event_constraint *c;
3234 unsigned int unused;
3235 struct extra_reg *er;
3238 if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
3239 switch (boot_cpu_data.x86) {
3241 return p6_pmu_init();
3243 return knc_pmu_init();
3245 return p4_pmu_init();
3251 * Check whether the Architectural PerfMon supports
3252 * Branch Misses Retired hw_event or not.
3254 cpuid(10, &eax.full, &ebx.full, &unused, &edx.full);
3255 if (eax.split.mask_length < ARCH_PERFMON_EVENTS_COUNT)
3258 version = eax.split.version_id;
3262 x86_pmu = intel_pmu;
3264 x86_pmu.version = version;
3265 x86_pmu.num_counters = eax.split.num_counters;
3266 x86_pmu.cntval_bits = eax.split.bit_width;
3267 x86_pmu.cntval_mask = (1ULL << eax.split.bit_width) - 1;
3269 x86_pmu.events_maskl = ebx.full;
3270 x86_pmu.events_mask_len = eax.split.mask_length;
3272 x86_pmu.max_pebs_events = min_t(unsigned, MAX_PEBS_EVENTS, x86_pmu.num_counters);
3275 * Quirk: v2 perfmon does not report fixed-purpose events, so
3276 * assume at least 3 events:
3279 x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3);
3281 if (boot_cpu_has(X86_FEATURE_PDCM)) {
3284 rdmsrl(MSR_IA32_PERF_CAPABILITIES, capabilities);
3285 x86_pmu.intel_cap.capabilities = capabilities;
3290 x86_add_quirk(intel_arch_events_quirk); /* Install first, so it runs last */
3293 * Install the hw-cache-events table:
3295 switch (boot_cpu_data.x86_model) {
3296 case 14: /* 65nm Core "Yonah" */
3297 pr_cont("Core events, ");
3300 case 15: /* 65nm Core2 "Merom" */
3301 x86_add_quirk(intel_clovertown_quirk);
3302 case 22: /* 65nm Core2 "Merom-L" */
3303 case 23: /* 45nm Core2 "Penryn" */
3304 case 29: /* 45nm Core2 "Dunnington (MP) */
3305 memcpy(hw_cache_event_ids, core2_hw_cache_event_ids,
3306 sizeof(hw_cache_event_ids));
3308 intel_pmu_lbr_init_core();
3310 x86_pmu.event_constraints = intel_core2_event_constraints;
3311 x86_pmu.pebs_constraints = intel_core2_pebs_event_constraints;
3312 pr_cont("Core2 events, ");
3315 case 30: /* 45nm Nehalem */
3316 case 26: /* 45nm Nehalem-EP */
3317 case 46: /* 45nm Nehalem-EX */
3318 memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids,
3319 sizeof(hw_cache_event_ids));
3320 memcpy(hw_cache_extra_regs, nehalem_hw_cache_extra_regs,
3321 sizeof(hw_cache_extra_regs));
3323 intel_pmu_lbr_init_nhm();
3325 x86_pmu.event_constraints = intel_nehalem_event_constraints;
3326 x86_pmu.pebs_constraints = intel_nehalem_pebs_event_constraints;
3327 x86_pmu.enable_all = intel_pmu_nhm_enable_all;
3328 x86_pmu.extra_regs = intel_nehalem_extra_regs;
3330 x86_pmu.cpu_events = nhm_events_attrs;
3332 /* UOPS_ISSUED.STALLED_CYCLES */
3333 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
3334 X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
3335 /* UOPS_EXECUTED.CORE_ACTIVE_CYCLES,c=1,i=1 */
3336 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
3337 X86_CONFIG(.event=0xb1, .umask=0x3f, .inv=1, .cmask=1);
3339 intel_pmu_pebs_data_source_nhm();
3340 x86_add_quirk(intel_nehalem_quirk);
3342 pr_cont("Nehalem events, ");
3345 case 28: /* 45nm Atom "Pineview" */
3346 case 38: /* 45nm Atom "Lincroft" */
3347 case 39: /* 32nm Atom "Penwell" */
3348 case 53: /* 32nm Atom "Cloverview" */
3349 case 54: /* 32nm Atom "Cedarview" */
3350 memcpy(hw_cache_event_ids, atom_hw_cache_event_ids,
3351 sizeof(hw_cache_event_ids));
3353 intel_pmu_lbr_init_atom();
3355 x86_pmu.event_constraints = intel_gen_event_constraints;
3356 x86_pmu.pebs_constraints = intel_atom_pebs_event_constraints;
3357 pr_cont("Atom events, ");
3360 case 55: /* 22nm Atom "Silvermont" */
3361 case 76: /* 14nm Atom "Airmont" */
3362 case 77: /* 22nm Atom "Silvermont Avoton/Rangely" */
3363 memcpy(hw_cache_event_ids, slm_hw_cache_event_ids,
3364 sizeof(hw_cache_event_ids));
3365 memcpy(hw_cache_extra_regs, slm_hw_cache_extra_regs,
3366 sizeof(hw_cache_extra_regs));
3368 intel_pmu_lbr_init_atom();
3370 x86_pmu.event_constraints = intel_slm_event_constraints;
3371 x86_pmu.pebs_constraints = intel_slm_pebs_event_constraints;
3372 x86_pmu.extra_regs = intel_slm_extra_regs;
3373 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
3374 pr_cont("Silvermont events, ");
3377 case 37: /* 32nm Westmere */
3378 case 44: /* 32nm Westmere-EP */
3379 case 47: /* 32nm Westmere-EX */
3380 memcpy(hw_cache_event_ids, westmere_hw_cache_event_ids,
3381 sizeof(hw_cache_event_ids));
3382 memcpy(hw_cache_extra_regs, nehalem_hw_cache_extra_regs,
3383 sizeof(hw_cache_extra_regs));
3385 intel_pmu_lbr_init_nhm();
3387 x86_pmu.event_constraints = intel_westmere_event_constraints;
3388 x86_pmu.enable_all = intel_pmu_nhm_enable_all;
3389 x86_pmu.pebs_constraints = intel_westmere_pebs_event_constraints;
3390 x86_pmu.extra_regs = intel_westmere_extra_regs;
3391 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
3393 x86_pmu.cpu_events = nhm_events_attrs;
3395 /* UOPS_ISSUED.STALLED_CYCLES */
3396 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
3397 X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
3398 /* UOPS_EXECUTED.CORE_ACTIVE_CYCLES,c=1,i=1 */
3399 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
3400 X86_CONFIG(.event=0xb1, .umask=0x3f, .inv=1, .cmask=1);
3402 intel_pmu_pebs_data_source_nhm();
3403 pr_cont("Westmere events, ");
3406 case 42: /* 32nm SandyBridge */
3407 case 45: /* 32nm SandyBridge-E/EN/EP */
3408 x86_add_quirk(intel_sandybridge_quirk);
3409 x86_add_quirk(intel_ht_bug);
3410 memcpy(hw_cache_event_ids, snb_hw_cache_event_ids,
3411 sizeof(hw_cache_event_ids));
3412 memcpy(hw_cache_extra_regs, snb_hw_cache_extra_regs,
3413 sizeof(hw_cache_extra_regs));
3415 intel_pmu_lbr_init_snb();
3417 x86_pmu.event_constraints = intel_snb_event_constraints;
3418 x86_pmu.pebs_constraints = intel_snb_pebs_event_constraints;
3419 x86_pmu.pebs_aliases = intel_pebs_aliases_snb;
3420 if (boot_cpu_data.x86_model == 45)
3421 x86_pmu.extra_regs = intel_snbep_extra_regs;
3423 x86_pmu.extra_regs = intel_snb_extra_regs;
3426 /* all extra regs are per-cpu when HT is on */
3427 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
3428 x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
3430 x86_pmu.cpu_events = snb_events_attrs;
3432 /* UOPS_ISSUED.ANY,c=1,i=1 to count stall cycles */
3433 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
3434 X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
3435 /* UOPS_DISPATCHED.THREAD,c=1,i=1 to count stall cycles*/
3436 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
3437 X86_CONFIG(.event=0xb1, .umask=0x01, .inv=1, .cmask=1);
3439 pr_cont("SandyBridge events, ");
3442 case 58: /* 22nm IvyBridge */
3443 case 62: /* 22nm IvyBridge-EP/EX */
3444 x86_add_quirk(intel_ht_bug);
3445 memcpy(hw_cache_event_ids, snb_hw_cache_event_ids,
3446 sizeof(hw_cache_event_ids));
3447 /* dTLB-load-misses on IVB is different than SNB */
3448 hw_cache_event_ids[C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = 0x8108; /* DTLB_LOAD_MISSES.DEMAND_LD_MISS_CAUSES_A_WALK */
3450 memcpy(hw_cache_extra_regs, snb_hw_cache_extra_regs,
3451 sizeof(hw_cache_extra_regs));
3453 intel_pmu_lbr_init_snb();
3455 x86_pmu.event_constraints = intel_ivb_event_constraints;
3456 x86_pmu.pebs_constraints = intel_ivb_pebs_event_constraints;
3457 x86_pmu.pebs_aliases = intel_pebs_aliases_snb;
3458 if (boot_cpu_data.x86_model == 62)
3459 x86_pmu.extra_regs = intel_snbep_extra_regs;
3461 x86_pmu.extra_regs = intel_snb_extra_regs;
3462 /* all extra regs are per-cpu when HT is on */
3463 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
3464 x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
3466 x86_pmu.cpu_events = snb_events_attrs;
3468 /* UOPS_ISSUED.ANY,c=1,i=1 to count stall cycles */
3469 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
3470 X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
3472 pr_cont("IvyBridge events, ");
3476 case 60: /* 22nm Haswell Core */
3477 case 63: /* 22nm Haswell Server */
3478 case 69: /* 22nm Haswell ULT */
3479 case 70: /* 22nm Haswell + GT3e (Intel Iris Pro graphics) */
3480 x86_add_quirk(intel_ht_bug);
3481 x86_pmu.late_ack = true;
3482 memcpy(hw_cache_event_ids, hsw_hw_cache_event_ids, sizeof(hw_cache_event_ids));
3483 memcpy(hw_cache_extra_regs, hsw_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
3485 intel_pmu_lbr_init_hsw();
3487 x86_pmu.event_constraints = intel_hsw_event_constraints;
3488 x86_pmu.pebs_constraints = intel_hsw_pebs_event_constraints;
3489 x86_pmu.extra_regs = intel_snbep_extra_regs;
3490 x86_pmu.pebs_aliases = intel_pebs_aliases_snb;
3491 /* all extra regs are per-cpu when HT is on */
3492 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
3493 x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
3495 x86_pmu.hw_config = hsw_hw_config;
3496 x86_pmu.get_event_constraints = hsw_get_event_constraints;
3497 x86_pmu.cpu_events = hsw_events_attrs;
3498 x86_pmu.lbr_double_abort = true;
3499 pr_cont("Haswell events, ");
3502 case 61: /* 14nm Broadwell Core-M */
3503 case 86: /* 14nm Broadwell Xeon D */
3504 case 71: /* 14nm Broadwell + GT3e (Intel Iris Pro graphics) */
3505 case 79: /* 14nm Broadwell Server */
3506 x86_pmu.late_ack = true;
3507 memcpy(hw_cache_event_ids, hsw_hw_cache_event_ids, sizeof(hw_cache_event_ids));
3508 memcpy(hw_cache_extra_regs, hsw_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
3510 /* L3_MISS_LOCAL_DRAM is BIT(26) in Broadwell */
3511 hw_cache_extra_regs[C(LL)][C(OP_READ)][C(RESULT_MISS)] = HSW_DEMAND_READ |
3512 BDW_L3_MISS|HSW_SNOOP_DRAM;
3513 hw_cache_extra_regs[C(LL)][C(OP_WRITE)][C(RESULT_MISS)] = HSW_DEMAND_WRITE|BDW_L3_MISS|
3515 hw_cache_extra_regs[C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = HSW_DEMAND_READ|
3516 BDW_L3_MISS_LOCAL|HSW_SNOOP_DRAM;
3517 hw_cache_extra_regs[C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = HSW_DEMAND_WRITE|
3518 BDW_L3_MISS_LOCAL|HSW_SNOOP_DRAM;
3520 intel_pmu_lbr_init_hsw();
3522 x86_pmu.event_constraints = intel_bdw_event_constraints;
3523 x86_pmu.pebs_constraints = intel_hsw_pebs_event_constraints;
3524 x86_pmu.extra_regs = intel_snbep_extra_regs;
3525 x86_pmu.pebs_aliases = intel_pebs_aliases_snb;
3526 /* all extra regs are per-cpu when HT is on */
3527 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
3528 x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
3530 x86_pmu.hw_config = hsw_hw_config;
3531 x86_pmu.get_event_constraints = hsw_get_event_constraints;
3532 x86_pmu.cpu_events = hsw_events_attrs;
3533 x86_pmu.limit_period = bdw_limit_period;
3534 pr_cont("Broadwell events, ");
3537 case 78: /* 14nm Skylake Mobile */
3538 case 94: /* 14nm Skylake Desktop */
3539 x86_pmu.late_ack = true;
3540 memcpy(hw_cache_event_ids, skl_hw_cache_event_ids, sizeof(hw_cache_event_ids));
3541 memcpy(hw_cache_extra_regs, skl_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
3542 intel_pmu_lbr_init_skl();
3544 x86_pmu.event_constraints = intel_skl_event_constraints;
3545 x86_pmu.pebs_constraints = intel_skl_pebs_event_constraints;
3546 x86_pmu.extra_regs = intel_skl_extra_regs;
3547 x86_pmu.pebs_aliases = intel_pebs_aliases_snb;
3548 /* all extra regs are per-cpu when HT is on */
3549 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
3550 x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
3552 x86_pmu.hw_config = hsw_hw_config;
3553 x86_pmu.get_event_constraints = hsw_get_event_constraints;
3554 x86_pmu.format_attrs = merge_attr(intel_arch3_formats_attr,
3556 WARN_ON(!x86_pmu.format_attrs);
3557 x86_pmu.cpu_events = hsw_events_attrs;
3558 pr_cont("Skylake events, ");
3562 switch (x86_pmu.version) {
3564 x86_pmu.event_constraints = intel_v1_event_constraints;
3565 pr_cont("generic architected perfmon v1, ");
3569 * default constraints for v2 and up
3571 x86_pmu.event_constraints = intel_gen_event_constraints;
3572 pr_cont("generic architected perfmon, ");
3577 if (x86_pmu.num_counters > INTEL_PMC_MAX_GENERIC) {
3578 WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
3579 x86_pmu.num_counters, INTEL_PMC_MAX_GENERIC);
3580 x86_pmu.num_counters = INTEL_PMC_MAX_GENERIC;
3582 x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
3584 if (x86_pmu.num_counters_fixed > INTEL_PMC_MAX_FIXED) {
3585 WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
3586 x86_pmu.num_counters_fixed, INTEL_PMC_MAX_FIXED);
3587 x86_pmu.num_counters_fixed = INTEL_PMC_MAX_FIXED;
3590 x86_pmu.intel_ctrl |=
3591 ((1LL << x86_pmu.num_counters_fixed)-1) << INTEL_PMC_IDX_FIXED;
3593 if (x86_pmu.event_constraints) {
3595 * event on fixed counter2 (REF_CYCLES) only works on this
3596 * counter, so do not extend mask to generic counters
3598 for_each_event_constraint(c, x86_pmu.event_constraints) {
3599 if (c->cmask == FIXED_EVENT_FLAGS
3600 && c->idxmsk64 != INTEL_PMC_MSK_FIXED_REF_CYCLES) {
3601 c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1;
3604 ~(~0UL << (INTEL_PMC_IDX_FIXED + x86_pmu.num_counters_fixed));
3605 c->weight = hweight64(c->idxmsk64);
3610 * Access LBR MSR may cause #GP under certain circumstances.
3611 * E.g. KVM doesn't support LBR MSR
3612 * Check all LBT MSR here.
3613 * Disable LBR access if any LBR MSRs can not be accessed.
3615 if (x86_pmu.lbr_nr && !check_msr(x86_pmu.lbr_tos, 0x3UL))
3617 for (i = 0; i < x86_pmu.lbr_nr; i++) {
3618 if (!(check_msr(x86_pmu.lbr_from + i, 0xffffUL) &&
3619 check_msr(x86_pmu.lbr_to + i, 0xffffUL)))
3624 * Access extra MSR may cause #GP under certain circumstances.
3625 * E.g. KVM doesn't support offcore event
3626 * Check all extra_regs here.
3628 if (x86_pmu.extra_regs) {
3629 for (er = x86_pmu.extra_regs; er->msr; er++) {
3630 er->extra_msr_access = check_msr(er->msr, 0x11UL);
3631 /* Disable LBR select mapping */
3632 if ((er->idx == EXTRA_REG_LBR) && !er->extra_msr_access)
3633 x86_pmu.lbr_sel_map = NULL;
3637 /* Support full width counters using alternative MSR range */
3638 if (x86_pmu.intel_cap.full_width_write) {
3639 x86_pmu.max_period = x86_pmu.cntval_mask;
3640 x86_pmu.perfctr = MSR_IA32_PMC0;
3641 pr_cont("full-width counters, ");
3648 * HT bug: phase 2 init
3649 * Called once we have valid topology information to check
3650 * whether or not HT is enabled
3651 * If HT is off, then we disable the workaround
3653 static __init int fixup_ht_bug(void)
3655 int cpu = smp_processor_id();
3658 * problem not present on this CPU model, nothing to do
3660 if (!(x86_pmu.flags & PMU_FL_EXCL_ENABLED))
3663 w = cpumask_weight(topology_sibling_cpumask(cpu));
3665 pr_info("PMU erratum BJ122, BV98, HSD29 worked around, HT is on\n");
3669 if (lockup_detector_suspend() != 0) {
3670 pr_debug("failed to disable PMU erratum BJ122, BV98, HSD29 workaround\n");
3674 x86_pmu.flags &= ~(PMU_FL_EXCL_CNTRS | PMU_FL_EXCL_ENABLED);
3676 x86_pmu.start_scheduling = NULL;
3677 x86_pmu.commit_scheduling = NULL;
3678 x86_pmu.stop_scheduling = NULL;
3680 lockup_detector_resume();
3684 for_each_online_cpu(c) {
3689 pr_info("PMU erratum BJ122, BV98, HSD29 workaround disabled, HT off\n");
3692 subsys_initcall(fixup_ht_bug)