Merge remote-tracking branch 'ftrace/for-next'
[deliverable/linux.git] / arch / x86 / events / intel / core.c
CommitLineData
a7e3ed1e 1/*
efc9f05d
SE
2 * Per core/cpu state
3 *
4 * Used to coordinate shared registers between HT threads or
5 * among events on a single PMU.
a7e3ed1e 6 */
de0428a7 7
c767a54b
JP
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
de0428a7
KW
10#include <linux/stddef.h>
11#include <linux/types.h>
12#include <linux/init.h>
13#include <linux/slab.h>
69c60c88 14#include <linux/export.h>
aacfbe6a 15#include <linux/nmi.h>
de0428a7 16
3a632cb2 17#include <asm/cpufeature.h>
de0428a7 18#include <asm/hardirq.h>
ef5f9f47 19#include <asm/intel-family.h>
de0428a7
KW
20#include <asm/apic.h>
21
27f6d22b 22#include "../perf_event.h"
a7e3ed1e 23
f22f54f4 24/*
b622d644 25 * Intel PerfMon, used on Core and later.
f22f54f4 26 */
ec75a716 27static u64 intel_perfmon_event_map[PERF_COUNT_HW_MAX] __read_mostly =
f22f54f4 28{
c3b7cdf1
PE
29 [PERF_COUNT_HW_CPU_CYCLES] = 0x003c,
30 [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
31 [PERF_COUNT_HW_CACHE_REFERENCES] = 0x4f2e,
32 [PERF_COUNT_HW_CACHE_MISSES] = 0x412e,
33 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
34 [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
35 [PERF_COUNT_HW_BUS_CYCLES] = 0x013c,
36 [PERF_COUNT_HW_REF_CPU_CYCLES] = 0x0300, /* pseudo-encoding */
f22f54f4
PZ
37};
38
5c543e3c 39static struct event_constraint intel_core_event_constraints[] __read_mostly =
f22f54f4
PZ
40{
41 INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
42 INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
43 INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
44 INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
45 INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
46 INTEL_EVENT_CONSTRAINT(0xc1, 0x1), /* FP_COMP_INSTR_RET */
47 EVENT_CONSTRAINT_END
48};
49
5c543e3c 50static struct event_constraint intel_core2_event_constraints[] __read_mostly =
f22f54f4 51{
b622d644
PZ
52 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
53 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
cd09c0c4 54 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
f22f54f4
PZ
55 INTEL_EVENT_CONSTRAINT(0x10, 0x1), /* FP_COMP_OPS_EXE */
56 INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
57 INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
58 INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
59 INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
60 INTEL_EVENT_CONSTRAINT(0x18, 0x1), /* IDLE_DURING_DIV */
61 INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
62 INTEL_EVENT_CONSTRAINT(0xa1, 0x1), /* RS_UOPS_DISPATCH_CYCLES */
b622d644 63 INTEL_EVENT_CONSTRAINT(0xc9, 0x1), /* ITLB_MISS_RETIRED (T30-9) */
f22f54f4
PZ
64 INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED */
65 EVENT_CONSTRAINT_END
66};
67
5c543e3c 68static struct event_constraint intel_nehalem_event_constraints[] __read_mostly =
f22f54f4 69{
b622d644
PZ
70 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
71 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
cd09c0c4 72 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
f22f54f4
PZ
73 INTEL_EVENT_CONSTRAINT(0x40, 0x3), /* L1D_CACHE_LD */
74 INTEL_EVENT_CONSTRAINT(0x41, 0x3), /* L1D_CACHE_ST */
75 INTEL_EVENT_CONSTRAINT(0x42, 0x3), /* L1D_CACHE_LOCK */
76 INTEL_EVENT_CONSTRAINT(0x43, 0x3), /* L1D_ALL_REF */
77 INTEL_EVENT_CONSTRAINT(0x48, 0x3), /* L1D_PEND_MISS */
78 INTEL_EVENT_CONSTRAINT(0x4e, 0x3), /* L1D_PREFETCH */
79 INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
80 INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */
81 EVENT_CONSTRAINT_END
82};
83
5c543e3c 84static struct extra_reg intel_nehalem_extra_regs[] __read_mostly =
a7e3ed1e 85{
53ad0447
YZ
86 /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
87 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0xffff, RSP_0),
f20093ee 88 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x100b),
a7e3ed1e
AK
89 EVENT_EXTRA_END
90};
91
5c543e3c 92static struct event_constraint intel_westmere_event_constraints[] __read_mostly =
f22f54f4 93{
b622d644
PZ
94 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
95 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
cd09c0c4 96 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
f22f54f4
PZ
97 INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
98 INTEL_EVENT_CONSTRAINT(0x60, 0x1), /* OFFCORE_REQUESTS_OUTSTANDING */
99 INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */
d1100770 100 INTEL_EVENT_CONSTRAINT(0xb3, 0x1), /* SNOOPQ_REQUEST_OUTSTANDING */
f22f54f4
PZ
101 EVENT_CONSTRAINT_END
102};
103
5c543e3c 104static struct event_constraint intel_snb_event_constraints[] __read_mostly =
b06b3d49
LM
105{
106 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
107 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
cd09c0c4 108 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
fd4a5aef
SE
109 INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_DISPATCH */
110 INTEL_UEVENT_CONSTRAINT(0x05a3, 0xf), /* CYCLE_ACTIVITY.STALLS_L2_PENDING */
111 INTEL_UEVENT_CONSTRAINT(0x02a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
112 INTEL_UEVENT_CONSTRAINT(0x06a3, 0x4), /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
b06b3d49 113 INTEL_EVENT_CONSTRAINT(0x48, 0x4), /* L1D_PEND_MISS.PENDING */
b06b3d49
LM
114 INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
115 INTEL_EVENT_CONSTRAINT(0xcd, 0x8), /* MEM_TRANS_RETIRED.LOAD_LATENCY */
f8378f52
AK
116 INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_DISPATCH */
117 INTEL_UEVENT_CONSTRAINT(0x02a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
93fcf72c 118
9010ae4a
SE
119 /*
120 * When HT is off these events can only run on the bottom 4 counters
121 * When HT is on, they are impacted by the HT bug and require EXCL access
122 */
93fcf72c
MD
123 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */
124 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
125 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
126 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
127
b06b3d49
LM
128 EVENT_CONSTRAINT_END
129};
130
69943182
SE
131static struct event_constraint intel_ivb_event_constraints[] __read_mostly =
132{
133 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
134 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
135 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
136 INTEL_UEVENT_CONSTRAINT(0x0148, 0x4), /* L1D_PEND_MISS.PENDING */
137 INTEL_UEVENT_CONSTRAINT(0x0279, 0xf), /* IDQ.EMTPY */
138 INTEL_UEVENT_CONSTRAINT(0x019c, 0xf), /* IDQ_UOPS_NOT_DELIVERED.CORE */
6113af14 139 INTEL_UEVENT_CONSTRAINT(0x02a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_LDM_PENDING */
69943182
SE
140 INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_EXECUTE */
141 INTEL_UEVENT_CONSTRAINT(0x05a3, 0xf), /* CYCLE_ACTIVITY.STALLS_L2_PENDING */
142 INTEL_UEVENT_CONSTRAINT(0x06a3, 0xf), /* CYCLE_ACTIVITY.STALLS_LDM_PENDING */
143 INTEL_UEVENT_CONSTRAINT(0x08a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
144 INTEL_UEVENT_CONSTRAINT(0x0ca3, 0x4), /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
145 INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
93fcf72c 146
9010ae4a
SE
147 /*
148 * When HT is off these events can only run on the bottom 4 counters
149 * When HT is on, they are impacted by the HT bug and require EXCL access
150 */
93fcf72c
MD
151 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */
152 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
153 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
154 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
155
69943182
SE
156 EVENT_CONSTRAINT_END
157};
158
5c543e3c 159static struct extra_reg intel_westmere_extra_regs[] __read_mostly =
a7e3ed1e 160{
53ad0447
YZ
161 /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
162 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0xffff, RSP_0),
163 INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0xffff, RSP_1),
f20093ee 164 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x100b),
a7e3ed1e
AK
165 EVENT_EXTRA_END
166};
167
0af3ac1f
AK
168static struct event_constraint intel_v1_event_constraints[] __read_mostly =
169{
170 EVENT_CONSTRAINT_END
171};
172
5c543e3c 173static struct event_constraint intel_gen_event_constraints[] __read_mostly =
f22f54f4 174{
b622d644
PZ
175 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
176 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
cd09c0c4 177 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
f22f54f4
PZ
178 EVENT_CONSTRAINT_END
179};
180
1fa64180
YZ
181static struct event_constraint intel_slm_event_constraints[] __read_mostly =
182{
183 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
184 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
1fa64180
YZ
185 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* pseudo CPU_CLK_UNHALTED.REF */
186 EVENT_CONSTRAINT_END
187};
188
20f36278 189static struct event_constraint intel_skl_event_constraints[] = {
9a92e16f
AK
190 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
191 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
192 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
193 INTEL_UEVENT_CONSTRAINT(0x1c0, 0x2), /* INST_RETIRED.PREC_DIST */
9010ae4a
SE
194
195 /*
196 * when HT is off, these can only run on the bottom 4 counters
197 */
198 INTEL_EVENT_CONSTRAINT(0xd0, 0xf), /* MEM_INST_RETIRED.* */
199 INTEL_EVENT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_RETIRED.* */
200 INTEL_EVENT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_L3_HIT_RETIRED.* */
201 INTEL_EVENT_CONSTRAINT(0xcd, 0xf), /* MEM_TRANS_RETIRED.* */
202 INTEL_EVENT_CONSTRAINT(0xc6, 0xf), /* FRONTEND_RETIRED.* */
203
9a92e16f
AK
204 EVENT_CONSTRAINT_END
205};
206
1e7b9390 207static struct extra_reg intel_knl_extra_regs[] __read_mostly = {
9c489fce
LO
208 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x799ffbb6e7ull, RSP_0),
209 INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OFFCORE_RSP_1, 0x399ffbffe7ull, RSP_1),
1e7b9390
HC
210 EVENT_EXTRA_END
211};
212
ee89cbc2 213static struct extra_reg intel_snb_extra_regs[] __read_mostly = {
53ad0447
YZ
214 /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
215 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x3f807f8fffull, RSP_0),
216 INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0x3f807f8fffull, RSP_1),
f20093ee 217 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd),
f1923820
SE
218 EVENT_EXTRA_END
219};
220
221static struct extra_reg intel_snbep_extra_regs[] __read_mostly = {
53ad0447
YZ
222 /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
223 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x3fffff8fffull, RSP_0),
224 INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0x3fffff8fffull, RSP_1),
f1a52789 225 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd),
ee89cbc2
SE
226 EVENT_EXTRA_END
227};
228
9a92e16f
AK
229static struct extra_reg intel_skl_extra_regs[] __read_mostly = {
230 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x3fffff8fffull, RSP_0),
231 INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0x3fffff8fffull, RSP_1),
232 INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd),
dfe1f3cb
AK
233 /*
234 * Note the low 8 bits eventsel code is not a continuous field, containing
235 * some #GPing bits. These are masked out.
236 */
237 INTEL_UEVENT_EXTRA_REG(0x01c6, MSR_PEBS_FRONTEND, 0x7fff17, FE),
9a92e16f
AK
238 EVENT_EXTRA_END
239};
240
7f2ee91f
IM
241EVENT_ATTR_STR(mem-loads, mem_ld_nhm, "event=0x0b,umask=0x10,ldlat=3");
242EVENT_ATTR_STR(mem-loads, mem_ld_snb, "event=0xcd,umask=0x1,ldlat=3");
243EVENT_ATTR_STR(mem-stores, mem_st_snb, "event=0xcd,umask=0x2");
f20093ee 244
20f36278 245static struct attribute *nhm_events_attrs[] = {
f20093ee
SE
246 EVENT_PTR(mem_ld_nhm),
247 NULL,
248};
249
a39fcae7
AK
250/*
251 * topdown events for Intel Core CPUs.
252 *
253 * The events are all in slots, which is a free slot in a 4 wide
254 * pipeline. Some events are already reported in slots, for cycle
255 * events we multiply by the pipeline width (4).
256 *
257 * With Hyper Threading on, topdown metrics are either summed or averaged
258 * between the threads of a core: (count_t0 + count_t1).
259 *
260 * For the average case the metric is always scaled to pipeline width,
261 * so we use factor 2 ((count_t0 + count_t1) / 2 * 4)
262 */
263
264EVENT_ATTR_STR_HT(topdown-total-slots, td_total_slots,
265 "event=0x3c,umask=0x0", /* cpu_clk_unhalted.thread */
266 "event=0x3c,umask=0x0,any=1"); /* cpu_clk_unhalted.thread_any */
267EVENT_ATTR_STR_HT(topdown-total-slots.scale, td_total_slots_scale, "4", "2");
268EVENT_ATTR_STR(topdown-slots-issued, td_slots_issued,
269 "event=0xe,umask=0x1"); /* uops_issued.any */
270EVENT_ATTR_STR(topdown-slots-retired, td_slots_retired,
271 "event=0xc2,umask=0x2"); /* uops_retired.retire_slots */
272EVENT_ATTR_STR(topdown-fetch-bubbles, td_fetch_bubbles,
273 "event=0x9c,umask=0x1"); /* idq_uops_not_delivered_core */
274EVENT_ATTR_STR_HT(topdown-recovery-bubbles, td_recovery_bubbles,
275 "event=0xd,umask=0x3,cmask=1", /* int_misc.recovery_cycles */
276 "event=0xd,umask=0x3,cmask=1,any=1"); /* int_misc.recovery_cycles_any */
277EVENT_ATTR_STR_HT(topdown-recovery-bubbles.scale, td_recovery_bubbles_scale,
278 "4", "2");
279
20f36278 280static struct attribute *snb_events_attrs[] = {
f20093ee 281 EVENT_PTR(mem_ld_snb),
9ad64c0f 282 EVENT_PTR(mem_st_snb),
a39fcae7
AK
283 EVENT_PTR(td_slots_issued),
284 EVENT_PTR(td_slots_retired),
285 EVENT_PTR(td_fetch_bubbles),
286 EVENT_PTR(td_total_slots),
287 EVENT_PTR(td_total_slots_scale),
288 EVENT_PTR(td_recovery_bubbles),
289 EVENT_PTR(td_recovery_bubbles_scale),
f20093ee
SE
290 NULL,
291};
292
3a632cb2
AK
293static struct event_constraint intel_hsw_event_constraints[] = {
294 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
295 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
296 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
e0fbac1c 297 INTEL_UEVENT_CONSTRAINT(0x148, 0x4), /* L1D_PEND_MISS.PENDING */
3a632cb2
AK
298 INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
299 INTEL_EVENT_CONSTRAINT(0xcd, 0x8), /* MEM_TRANS_RETIRED.LOAD_LATENCY */
300 /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
c420f19b 301 INTEL_UEVENT_CONSTRAINT(0x08a3, 0x4),
3a632cb2 302 /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
c420f19b 303 INTEL_UEVENT_CONSTRAINT(0x0ca3, 0x4),
3a632cb2 304 /* CYCLE_ACTIVITY.CYCLES_NO_EXECUTE */
c420f19b 305 INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf),
93fcf72c 306
9010ae4a
SE
307 /*
308 * When HT is off these events can only run on the bottom 4 counters
309 * When HT is on, they are impacted by the HT bug and require EXCL access
310 */
93fcf72c
MD
311 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */
312 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
313 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
314 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
315
3a632cb2
AK
316 EVENT_CONSTRAINT_END
317};
318
20f36278 319static struct event_constraint intel_bdw_event_constraints[] = {
91f1b705
AK
320 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
321 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
322 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
323 INTEL_UEVENT_CONSTRAINT(0x148, 0x4), /* L1D_PEND_MISS.PENDING */
b7883a1c 324 INTEL_UBIT_EVENT_CONSTRAINT(0x8a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_MISS */
9010ae4a
SE
325 /*
326 * when HT is off, these can only run on the bottom 4 counters
327 */
328 INTEL_EVENT_CONSTRAINT(0xd0, 0xf), /* MEM_INST_RETIRED.* */
329 INTEL_EVENT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_RETIRED.* */
330 INTEL_EVENT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_L3_HIT_RETIRED.* */
331 INTEL_EVENT_CONSTRAINT(0xcd, 0xf), /* MEM_TRANS_RETIRED.* */
91f1b705
AK
332 EVENT_CONSTRAINT_END
333};
334
f22f54f4
PZ
335static u64 intel_pmu_event_map(int hw_event)
336{
337 return intel_perfmon_event_map[hw_event];
338}
339
9a92e16f
AK
340/*
341 * Notes on the events:
342 * - data reads do not include code reads (comparable to earlier tables)
343 * - data counts include speculative execution (except L1 write, dtlb, bpu)
344 * - remote node access includes remote memory, remote cache, remote mmio.
345 * - prefetches are not included in the counts.
346 * - icache miss does not include decoded icache
347 */
348
349#define SKL_DEMAND_DATA_RD BIT_ULL(0)
350#define SKL_DEMAND_RFO BIT_ULL(1)
351#define SKL_ANY_RESPONSE BIT_ULL(16)
352#define SKL_SUPPLIER_NONE BIT_ULL(17)
353#define SKL_L3_MISS_LOCAL_DRAM BIT_ULL(26)
354#define SKL_L3_MISS_REMOTE_HOP0_DRAM BIT_ULL(27)
355#define SKL_L3_MISS_REMOTE_HOP1_DRAM BIT_ULL(28)
356#define SKL_L3_MISS_REMOTE_HOP2P_DRAM BIT_ULL(29)
357#define SKL_L3_MISS (SKL_L3_MISS_LOCAL_DRAM| \
358 SKL_L3_MISS_REMOTE_HOP0_DRAM| \
359 SKL_L3_MISS_REMOTE_HOP1_DRAM| \
360 SKL_L3_MISS_REMOTE_HOP2P_DRAM)
361#define SKL_SPL_HIT BIT_ULL(30)
362#define SKL_SNOOP_NONE BIT_ULL(31)
363#define SKL_SNOOP_NOT_NEEDED BIT_ULL(32)
364#define SKL_SNOOP_MISS BIT_ULL(33)
365#define SKL_SNOOP_HIT_NO_FWD BIT_ULL(34)
366#define SKL_SNOOP_HIT_WITH_FWD BIT_ULL(35)
367#define SKL_SNOOP_HITM BIT_ULL(36)
368#define SKL_SNOOP_NON_DRAM BIT_ULL(37)
369#define SKL_ANY_SNOOP (SKL_SPL_HIT|SKL_SNOOP_NONE| \
370 SKL_SNOOP_NOT_NEEDED|SKL_SNOOP_MISS| \
371 SKL_SNOOP_HIT_NO_FWD|SKL_SNOOP_HIT_WITH_FWD| \
372 SKL_SNOOP_HITM|SKL_SNOOP_NON_DRAM)
373#define SKL_DEMAND_READ SKL_DEMAND_DATA_RD
374#define SKL_SNOOP_DRAM (SKL_SNOOP_NONE| \
375 SKL_SNOOP_NOT_NEEDED|SKL_SNOOP_MISS| \
376 SKL_SNOOP_HIT_NO_FWD|SKL_SNOOP_HIT_WITH_FWD| \
377 SKL_SNOOP_HITM|SKL_SPL_HIT)
378#define SKL_DEMAND_WRITE SKL_DEMAND_RFO
379#define SKL_LLC_ACCESS SKL_ANY_RESPONSE
380#define SKL_L3_MISS_REMOTE (SKL_L3_MISS_REMOTE_HOP0_DRAM| \
381 SKL_L3_MISS_REMOTE_HOP1_DRAM| \
382 SKL_L3_MISS_REMOTE_HOP2P_DRAM)
383
384static __initconst const u64 skl_hw_cache_event_ids
385 [PERF_COUNT_HW_CACHE_MAX]
386 [PERF_COUNT_HW_CACHE_OP_MAX]
387 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
388{
389 [ C(L1D ) ] = {
390 [ C(OP_READ) ] = {
391 [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_INST_RETIRED.ALL_LOADS */
392 [ C(RESULT_MISS) ] = 0x151, /* L1D.REPLACEMENT */
393 },
394 [ C(OP_WRITE) ] = {
395 [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_INST_RETIRED.ALL_STORES */
396 [ C(RESULT_MISS) ] = 0x0,
397 },
398 [ C(OP_PREFETCH) ] = {
399 [ C(RESULT_ACCESS) ] = 0x0,
400 [ C(RESULT_MISS) ] = 0x0,
401 },
402 },
403 [ C(L1I ) ] = {
404 [ C(OP_READ) ] = {
405 [ C(RESULT_ACCESS) ] = 0x0,
406 [ C(RESULT_MISS) ] = 0x283, /* ICACHE_64B.MISS */
407 },
408 [ C(OP_WRITE) ] = {
409 [ C(RESULT_ACCESS) ] = -1,
410 [ C(RESULT_MISS) ] = -1,
411 },
412 [ C(OP_PREFETCH) ] = {
413 [ C(RESULT_ACCESS) ] = 0x0,
414 [ C(RESULT_MISS) ] = 0x0,
415 },
416 },
417 [ C(LL ) ] = {
418 [ C(OP_READ) ] = {
419 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
420 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
421 },
422 [ C(OP_WRITE) ] = {
423 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
424 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
425 },
426 [ C(OP_PREFETCH) ] = {
427 [ C(RESULT_ACCESS) ] = 0x0,
428 [ C(RESULT_MISS) ] = 0x0,
429 },
430 },
431 [ C(DTLB) ] = {
432 [ C(OP_READ) ] = {
433 [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_INST_RETIRED.ALL_LOADS */
434 [ C(RESULT_MISS) ] = 0x608, /* DTLB_LOAD_MISSES.WALK_COMPLETED */
435 },
436 [ C(OP_WRITE) ] = {
437 [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_INST_RETIRED.ALL_STORES */
438 [ C(RESULT_MISS) ] = 0x649, /* DTLB_STORE_MISSES.WALK_COMPLETED */
439 },
440 [ C(OP_PREFETCH) ] = {
441 [ C(RESULT_ACCESS) ] = 0x0,
442 [ C(RESULT_MISS) ] = 0x0,
443 },
444 },
445 [ C(ITLB) ] = {
446 [ C(OP_READ) ] = {
447 [ C(RESULT_ACCESS) ] = 0x2085, /* ITLB_MISSES.STLB_HIT */
448 [ C(RESULT_MISS) ] = 0xe85, /* ITLB_MISSES.WALK_COMPLETED */
449 },
450 [ C(OP_WRITE) ] = {
451 [ C(RESULT_ACCESS) ] = -1,
452 [ C(RESULT_MISS) ] = -1,
453 },
454 [ C(OP_PREFETCH) ] = {
455 [ C(RESULT_ACCESS) ] = -1,
456 [ C(RESULT_MISS) ] = -1,
457 },
458 },
459 [ C(BPU ) ] = {
460 [ C(OP_READ) ] = {
461 [ C(RESULT_ACCESS) ] = 0xc4, /* BR_INST_RETIRED.ALL_BRANCHES */
462 [ C(RESULT_MISS) ] = 0xc5, /* BR_MISP_RETIRED.ALL_BRANCHES */
463 },
464 [ C(OP_WRITE) ] = {
465 [ C(RESULT_ACCESS) ] = -1,
466 [ C(RESULT_MISS) ] = -1,
467 },
468 [ C(OP_PREFETCH) ] = {
469 [ C(RESULT_ACCESS) ] = -1,
470 [ C(RESULT_MISS) ] = -1,
471 },
472 },
473 [ C(NODE) ] = {
474 [ C(OP_READ) ] = {
475 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
476 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
477 },
478 [ C(OP_WRITE) ] = {
479 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
480 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
481 },
482 [ C(OP_PREFETCH) ] = {
483 [ C(RESULT_ACCESS) ] = 0x0,
484 [ C(RESULT_MISS) ] = 0x0,
485 },
486 },
487};
488
489static __initconst const u64 skl_hw_cache_extra_regs
490 [PERF_COUNT_HW_CACHE_MAX]
491 [PERF_COUNT_HW_CACHE_OP_MAX]
492 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
493{
494 [ C(LL ) ] = {
495 [ C(OP_READ) ] = {
496 [ C(RESULT_ACCESS) ] = SKL_DEMAND_READ|
497 SKL_LLC_ACCESS|SKL_ANY_SNOOP,
498 [ C(RESULT_MISS) ] = SKL_DEMAND_READ|
499 SKL_L3_MISS|SKL_ANY_SNOOP|
500 SKL_SUPPLIER_NONE,
501 },
502 [ C(OP_WRITE) ] = {
503 [ C(RESULT_ACCESS) ] = SKL_DEMAND_WRITE|
504 SKL_LLC_ACCESS|SKL_ANY_SNOOP,
505 [ C(RESULT_MISS) ] = SKL_DEMAND_WRITE|
506 SKL_L3_MISS|SKL_ANY_SNOOP|
507 SKL_SUPPLIER_NONE,
508 },
509 [ C(OP_PREFETCH) ] = {
510 [ C(RESULT_ACCESS) ] = 0x0,
511 [ C(RESULT_MISS) ] = 0x0,
512 },
513 },
514 [ C(NODE) ] = {
515 [ C(OP_READ) ] = {
516 [ C(RESULT_ACCESS) ] = SKL_DEMAND_READ|
517 SKL_L3_MISS_LOCAL_DRAM|SKL_SNOOP_DRAM,
518 [ C(RESULT_MISS) ] = SKL_DEMAND_READ|
519 SKL_L3_MISS_REMOTE|SKL_SNOOP_DRAM,
520 },
521 [ C(OP_WRITE) ] = {
522 [ C(RESULT_ACCESS) ] = SKL_DEMAND_WRITE|
523 SKL_L3_MISS_LOCAL_DRAM|SKL_SNOOP_DRAM,
524 [ C(RESULT_MISS) ] = SKL_DEMAND_WRITE|
525 SKL_L3_MISS_REMOTE|SKL_SNOOP_DRAM,
526 },
527 [ C(OP_PREFETCH) ] = {
528 [ C(RESULT_ACCESS) ] = 0x0,
529 [ C(RESULT_MISS) ] = 0x0,
530 },
531 },
532};
533
74e6543f
YZ
534#define SNB_DMND_DATA_RD (1ULL << 0)
535#define SNB_DMND_RFO (1ULL << 1)
536#define SNB_DMND_IFETCH (1ULL << 2)
537#define SNB_DMND_WB (1ULL << 3)
538#define SNB_PF_DATA_RD (1ULL << 4)
539#define SNB_PF_RFO (1ULL << 5)
540#define SNB_PF_IFETCH (1ULL << 6)
541#define SNB_LLC_DATA_RD (1ULL << 7)
542#define SNB_LLC_RFO (1ULL << 8)
543#define SNB_LLC_IFETCH (1ULL << 9)
544#define SNB_BUS_LOCKS (1ULL << 10)
545#define SNB_STRM_ST (1ULL << 11)
546#define SNB_OTHER (1ULL << 15)
547#define SNB_RESP_ANY (1ULL << 16)
548#define SNB_NO_SUPP (1ULL << 17)
549#define SNB_LLC_HITM (1ULL << 18)
550#define SNB_LLC_HITE (1ULL << 19)
551#define SNB_LLC_HITS (1ULL << 20)
552#define SNB_LLC_HITF (1ULL << 21)
553#define SNB_LOCAL (1ULL << 22)
554#define SNB_REMOTE (0xffULL << 23)
555#define SNB_SNP_NONE (1ULL << 31)
556#define SNB_SNP_NOT_NEEDED (1ULL << 32)
557#define SNB_SNP_MISS (1ULL << 33)
558#define SNB_NO_FWD (1ULL << 34)
559#define SNB_SNP_FWD (1ULL << 35)
560#define SNB_HITM (1ULL << 36)
561#define SNB_NON_DRAM (1ULL << 37)
562
563#define SNB_DMND_READ (SNB_DMND_DATA_RD|SNB_LLC_DATA_RD)
564#define SNB_DMND_WRITE (SNB_DMND_RFO|SNB_LLC_RFO)
565#define SNB_DMND_PREFETCH (SNB_PF_DATA_RD|SNB_PF_RFO)
566
567#define SNB_SNP_ANY (SNB_SNP_NONE|SNB_SNP_NOT_NEEDED| \
568 SNB_SNP_MISS|SNB_NO_FWD|SNB_SNP_FWD| \
569 SNB_HITM)
570
571#define SNB_DRAM_ANY (SNB_LOCAL|SNB_REMOTE|SNB_SNP_ANY)
572#define SNB_DRAM_REMOTE (SNB_REMOTE|SNB_SNP_ANY)
573
574#define SNB_L3_ACCESS SNB_RESP_ANY
575#define SNB_L3_MISS (SNB_DRAM_ANY|SNB_NON_DRAM)
576
577static __initconst const u64 snb_hw_cache_extra_regs
578 [PERF_COUNT_HW_CACHE_MAX]
579 [PERF_COUNT_HW_CACHE_OP_MAX]
580 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
581{
582 [ C(LL ) ] = {
583 [ C(OP_READ) ] = {
584 [ C(RESULT_ACCESS) ] = SNB_DMND_READ|SNB_L3_ACCESS,
585 [ C(RESULT_MISS) ] = SNB_DMND_READ|SNB_L3_MISS,
586 },
587 [ C(OP_WRITE) ] = {
588 [ C(RESULT_ACCESS) ] = SNB_DMND_WRITE|SNB_L3_ACCESS,
589 [ C(RESULT_MISS) ] = SNB_DMND_WRITE|SNB_L3_MISS,
590 },
591 [ C(OP_PREFETCH) ] = {
592 [ C(RESULT_ACCESS) ] = SNB_DMND_PREFETCH|SNB_L3_ACCESS,
593 [ C(RESULT_MISS) ] = SNB_DMND_PREFETCH|SNB_L3_MISS,
594 },
595 },
596 [ C(NODE) ] = {
597 [ C(OP_READ) ] = {
598 [ C(RESULT_ACCESS) ] = SNB_DMND_READ|SNB_DRAM_ANY,
599 [ C(RESULT_MISS) ] = SNB_DMND_READ|SNB_DRAM_REMOTE,
600 },
601 [ C(OP_WRITE) ] = {
602 [ C(RESULT_ACCESS) ] = SNB_DMND_WRITE|SNB_DRAM_ANY,
603 [ C(RESULT_MISS) ] = SNB_DMND_WRITE|SNB_DRAM_REMOTE,
604 },
605 [ C(OP_PREFETCH) ] = {
606 [ C(RESULT_ACCESS) ] = SNB_DMND_PREFETCH|SNB_DRAM_ANY,
607 [ C(RESULT_MISS) ] = SNB_DMND_PREFETCH|SNB_DRAM_REMOTE,
608 },
609 },
610};
611
b06b3d49
LM
612static __initconst const u64 snb_hw_cache_event_ids
613 [PERF_COUNT_HW_CACHE_MAX]
614 [PERF_COUNT_HW_CACHE_OP_MAX]
615 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
616{
617 [ C(L1D) ] = {
618 [ C(OP_READ) ] = {
619 [ C(RESULT_ACCESS) ] = 0xf1d0, /* MEM_UOP_RETIRED.LOADS */
620 [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPLACEMENT */
621 },
622 [ C(OP_WRITE) ] = {
623 [ C(RESULT_ACCESS) ] = 0xf2d0, /* MEM_UOP_RETIRED.STORES */
624 [ C(RESULT_MISS) ] = 0x0851, /* L1D.ALL_M_REPLACEMENT */
625 },
626 [ C(OP_PREFETCH) ] = {
627 [ C(RESULT_ACCESS) ] = 0x0,
628 [ C(RESULT_MISS) ] = 0x024e, /* HW_PRE_REQ.DL1_MISS */
629 },
630 },
631 [ C(L1I ) ] = {
632 [ C(OP_READ) ] = {
633 [ C(RESULT_ACCESS) ] = 0x0,
634 [ C(RESULT_MISS) ] = 0x0280, /* ICACHE.MISSES */
635 },
636 [ C(OP_WRITE) ] = {
637 [ C(RESULT_ACCESS) ] = -1,
638 [ C(RESULT_MISS) ] = -1,
639 },
640 [ C(OP_PREFETCH) ] = {
641 [ C(RESULT_ACCESS) ] = 0x0,
642 [ C(RESULT_MISS) ] = 0x0,
643 },
644 },
645 [ C(LL ) ] = {
b06b3d49 646 [ C(OP_READ) ] = {
63b6a675 647 /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
b06b3d49 648 [ C(RESULT_ACCESS) ] = 0x01b7,
63b6a675
PZ
649 /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
650 [ C(RESULT_MISS) ] = 0x01b7,
b06b3d49
LM
651 },
652 [ C(OP_WRITE) ] = {
63b6a675 653 /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
b06b3d49 654 [ C(RESULT_ACCESS) ] = 0x01b7,
63b6a675
PZ
655 /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
656 [ C(RESULT_MISS) ] = 0x01b7,
b06b3d49
LM
657 },
658 [ C(OP_PREFETCH) ] = {
63b6a675 659 /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
b06b3d49 660 [ C(RESULT_ACCESS) ] = 0x01b7,
63b6a675
PZ
661 /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
662 [ C(RESULT_MISS) ] = 0x01b7,
b06b3d49
LM
663 },
664 },
665 [ C(DTLB) ] = {
666 [ C(OP_READ) ] = {
667 [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOP_RETIRED.ALL_LOADS */
668 [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.CAUSES_A_WALK */
669 },
670 [ C(OP_WRITE) ] = {
671 [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOP_RETIRED.ALL_STORES */
672 [ C(RESULT_MISS) ] = 0x0149, /* DTLB_STORE_MISSES.MISS_CAUSES_A_WALK */
673 },
674 [ C(OP_PREFETCH) ] = {
675 [ C(RESULT_ACCESS) ] = 0x0,
676 [ C(RESULT_MISS) ] = 0x0,
677 },
678 },
679 [ C(ITLB) ] = {
680 [ C(OP_READ) ] = {
681 [ C(RESULT_ACCESS) ] = 0x1085, /* ITLB_MISSES.STLB_HIT */
682 [ C(RESULT_MISS) ] = 0x0185, /* ITLB_MISSES.CAUSES_A_WALK */
683 },
684 [ C(OP_WRITE) ] = {
685 [ C(RESULT_ACCESS) ] = -1,
686 [ C(RESULT_MISS) ] = -1,
687 },
688 [ C(OP_PREFETCH) ] = {
689 [ C(RESULT_ACCESS) ] = -1,
690 [ C(RESULT_MISS) ] = -1,
691 },
692 },
693 [ C(BPU ) ] = {
694 [ C(OP_READ) ] = {
695 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
696 [ C(RESULT_MISS) ] = 0x00c5, /* BR_MISP_RETIRED.ALL_BRANCHES */
697 },
698 [ C(OP_WRITE) ] = {
699 [ C(RESULT_ACCESS) ] = -1,
700 [ C(RESULT_MISS) ] = -1,
701 },
702 [ C(OP_PREFETCH) ] = {
703 [ C(RESULT_ACCESS) ] = -1,
704 [ C(RESULT_MISS) ] = -1,
705 },
706 },
89d6c0b5
PZ
707 [ C(NODE) ] = {
708 [ C(OP_READ) ] = {
74e6543f
YZ
709 [ C(RESULT_ACCESS) ] = 0x01b7,
710 [ C(RESULT_MISS) ] = 0x01b7,
89d6c0b5
PZ
711 },
712 [ C(OP_WRITE) ] = {
74e6543f
YZ
713 [ C(RESULT_ACCESS) ] = 0x01b7,
714 [ C(RESULT_MISS) ] = 0x01b7,
89d6c0b5
PZ
715 },
716 [ C(OP_PREFETCH) ] = {
74e6543f
YZ
717 [ C(RESULT_ACCESS) ] = 0x01b7,
718 [ C(RESULT_MISS) ] = 0x01b7,
89d6c0b5
PZ
719 },
720 },
721
b06b3d49
LM
722};
723
0f1b5ca2
AK
724/*
725 * Notes on the events:
726 * - data reads do not include code reads (comparable to earlier tables)
727 * - data counts include speculative execution (except L1 write, dtlb, bpu)
728 * - remote node access includes remote memory, remote cache, remote mmio.
729 * - prefetches are not included in the counts because they are not
730 * reliably counted.
731 */
732
733#define HSW_DEMAND_DATA_RD BIT_ULL(0)
734#define HSW_DEMAND_RFO BIT_ULL(1)
735#define HSW_ANY_RESPONSE BIT_ULL(16)
736#define HSW_SUPPLIER_NONE BIT_ULL(17)
737#define HSW_L3_MISS_LOCAL_DRAM BIT_ULL(22)
738#define HSW_L3_MISS_REMOTE_HOP0 BIT_ULL(27)
739#define HSW_L3_MISS_REMOTE_HOP1 BIT_ULL(28)
740#define HSW_L3_MISS_REMOTE_HOP2P BIT_ULL(29)
741#define HSW_L3_MISS (HSW_L3_MISS_LOCAL_DRAM| \
742 HSW_L3_MISS_REMOTE_HOP0|HSW_L3_MISS_REMOTE_HOP1| \
743 HSW_L3_MISS_REMOTE_HOP2P)
744#define HSW_SNOOP_NONE BIT_ULL(31)
745#define HSW_SNOOP_NOT_NEEDED BIT_ULL(32)
746#define HSW_SNOOP_MISS BIT_ULL(33)
747#define HSW_SNOOP_HIT_NO_FWD BIT_ULL(34)
748#define HSW_SNOOP_HIT_WITH_FWD BIT_ULL(35)
749#define HSW_SNOOP_HITM BIT_ULL(36)
750#define HSW_SNOOP_NON_DRAM BIT_ULL(37)
751#define HSW_ANY_SNOOP (HSW_SNOOP_NONE| \
752 HSW_SNOOP_NOT_NEEDED|HSW_SNOOP_MISS| \
753 HSW_SNOOP_HIT_NO_FWD|HSW_SNOOP_HIT_WITH_FWD| \
754 HSW_SNOOP_HITM|HSW_SNOOP_NON_DRAM)
755#define HSW_SNOOP_DRAM (HSW_ANY_SNOOP & ~HSW_SNOOP_NON_DRAM)
756#define HSW_DEMAND_READ HSW_DEMAND_DATA_RD
757#define HSW_DEMAND_WRITE HSW_DEMAND_RFO
758#define HSW_L3_MISS_REMOTE (HSW_L3_MISS_REMOTE_HOP0|\
759 HSW_L3_MISS_REMOTE_HOP1|HSW_L3_MISS_REMOTE_HOP2P)
760#define HSW_LLC_ACCESS HSW_ANY_RESPONSE
761
91f1b705
AK
762#define BDW_L3_MISS_LOCAL BIT(26)
763#define BDW_L3_MISS (BDW_L3_MISS_LOCAL| \
764 HSW_L3_MISS_REMOTE_HOP0|HSW_L3_MISS_REMOTE_HOP1| \
765 HSW_L3_MISS_REMOTE_HOP2P)
766
767
0f1b5ca2
AK
768static __initconst const u64 hsw_hw_cache_event_ids
769 [PERF_COUNT_HW_CACHE_MAX]
770 [PERF_COUNT_HW_CACHE_OP_MAX]
771 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
772{
773 [ C(L1D ) ] = {
774 [ C(OP_READ) ] = {
775 [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
776 [ C(RESULT_MISS) ] = 0x151, /* L1D.REPLACEMENT */
777 },
778 [ C(OP_WRITE) ] = {
779 [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
780 [ C(RESULT_MISS) ] = 0x0,
781 },
782 [ C(OP_PREFETCH) ] = {
783 [ C(RESULT_ACCESS) ] = 0x0,
784 [ C(RESULT_MISS) ] = 0x0,
785 },
786 },
787 [ C(L1I ) ] = {
788 [ C(OP_READ) ] = {
789 [ C(RESULT_ACCESS) ] = 0x0,
790 [ C(RESULT_MISS) ] = 0x280, /* ICACHE.MISSES */
791 },
792 [ C(OP_WRITE) ] = {
793 [ C(RESULT_ACCESS) ] = -1,
794 [ C(RESULT_MISS) ] = -1,
795 },
796 [ C(OP_PREFETCH) ] = {
797 [ C(RESULT_ACCESS) ] = 0x0,
798 [ C(RESULT_MISS) ] = 0x0,
799 },
800 },
801 [ C(LL ) ] = {
802 [ C(OP_READ) ] = {
803 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
804 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
805 },
806 [ C(OP_WRITE) ] = {
807 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
808 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
809 },
810 [ C(OP_PREFETCH) ] = {
811 [ C(RESULT_ACCESS) ] = 0x0,
812 [ C(RESULT_MISS) ] = 0x0,
813 },
814 },
815 [ C(DTLB) ] = {
816 [ C(OP_READ) ] = {
817 [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
818 [ C(RESULT_MISS) ] = 0x108, /* DTLB_LOAD_MISSES.MISS_CAUSES_A_WALK */
819 },
820 [ C(OP_WRITE) ] = {
821 [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
822 [ C(RESULT_MISS) ] = 0x149, /* DTLB_STORE_MISSES.MISS_CAUSES_A_WALK */
823 },
824 [ C(OP_PREFETCH) ] = {
825 [ C(RESULT_ACCESS) ] = 0x0,
826 [ C(RESULT_MISS) ] = 0x0,
827 },
828 },
829 [ C(ITLB) ] = {
830 [ C(OP_READ) ] = {
831 [ C(RESULT_ACCESS) ] = 0x6085, /* ITLB_MISSES.STLB_HIT */
832 [ C(RESULT_MISS) ] = 0x185, /* ITLB_MISSES.MISS_CAUSES_A_WALK */
833 },
834 [ C(OP_WRITE) ] = {
835 [ C(RESULT_ACCESS) ] = -1,
836 [ C(RESULT_MISS) ] = -1,
837 },
838 [ C(OP_PREFETCH) ] = {
839 [ C(RESULT_ACCESS) ] = -1,
840 [ C(RESULT_MISS) ] = -1,
841 },
842 },
843 [ C(BPU ) ] = {
844 [ C(OP_READ) ] = {
845 [ C(RESULT_ACCESS) ] = 0xc4, /* BR_INST_RETIRED.ALL_BRANCHES */
846 [ C(RESULT_MISS) ] = 0xc5, /* BR_MISP_RETIRED.ALL_BRANCHES */
847 },
848 [ C(OP_WRITE) ] = {
849 [ C(RESULT_ACCESS) ] = -1,
850 [ C(RESULT_MISS) ] = -1,
851 },
852 [ C(OP_PREFETCH) ] = {
853 [ C(RESULT_ACCESS) ] = -1,
854 [ C(RESULT_MISS) ] = -1,
855 },
856 },
857 [ C(NODE) ] = {
858 [ C(OP_READ) ] = {
859 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
860 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
861 },
862 [ C(OP_WRITE) ] = {
863 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
864 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
865 },
866 [ C(OP_PREFETCH) ] = {
867 [ C(RESULT_ACCESS) ] = 0x0,
868 [ C(RESULT_MISS) ] = 0x0,
869 },
870 },
871};
872
873static __initconst const u64 hsw_hw_cache_extra_regs
874 [PERF_COUNT_HW_CACHE_MAX]
875 [PERF_COUNT_HW_CACHE_OP_MAX]
876 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
877{
878 [ C(LL ) ] = {
879 [ C(OP_READ) ] = {
880 [ C(RESULT_ACCESS) ] = HSW_DEMAND_READ|
881 HSW_LLC_ACCESS,
882 [ C(RESULT_MISS) ] = HSW_DEMAND_READ|
883 HSW_L3_MISS|HSW_ANY_SNOOP,
884 },
885 [ C(OP_WRITE) ] = {
886 [ C(RESULT_ACCESS) ] = HSW_DEMAND_WRITE|
887 HSW_LLC_ACCESS,
888 [ C(RESULT_MISS) ] = HSW_DEMAND_WRITE|
889 HSW_L3_MISS|HSW_ANY_SNOOP,
890 },
891 [ C(OP_PREFETCH) ] = {
892 [ C(RESULT_ACCESS) ] = 0x0,
893 [ C(RESULT_MISS) ] = 0x0,
894 },
895 },
896 [ C(NODE) ] = {
897 [ C(OP_READ) ] = {
898 [ C(RESULT_ACCESS) ] = HSW_DEMAND_READ|
899 HSW_L3_MISS_LOCAL_DRAM|
900 HSW_SNOOP_DRAM,
901 [ C(RESULT_MISS) ] = HSW_DEMAND_READ|
902 HSW_L3_MISS_REMOTE|
903 HSW_SNOOP_DRAM,
904 },
905 [ C(OP_WRITE) ] = {
906 [ C(RESULT_ACCESS) ] = HSW_DEMAND_WRITE|
907 HSW_L3_MISS_LOCAL_DRAM|
908 HSW_SNOOP_DRAM,
909 [ C(RESULT_MISS) ] = HSW_DEMAND_WRITE|
910 HSW_L3_MISS_REMOTE|
911 HSW_SNOOP_DRAM,
912 },
913 [ C(OP_PREFETCH) ] = {
914 [ C(RESULT_ACCESS) ] = 0x0,
915 [ C(RESULT_MISS) ] = 0x0,
916 },
917 },
918};
919
caaa8be3 920static __initconst const u64 westmere_hw_cache_event_ids
f22f54f4
PZ
921 [PERF_COUNT_HW_CACHE_MAX]
922 [PERF_COUNT_HW_CACHE_OP_MAX]
923 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
924{
925 [ C(L1D) ] = {
926 [ C(OP_READ) ] = {
927 [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
928 [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPL */
929 },
930 [ C(OP_WRITE) ] = {
931 [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
932 [ C(RESULT_MISS) ] = 0x0251, /* L1D.M_REPL */
933 },
934 [ C(OP_PREFETCH) ] = {
935 [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
936 [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
937 },
938 },
939 [ C(L1I ) ] = {
940 [ C(OP_READ) ] = {
941 [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
942 [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
943 },
944 [ C(OP_WRITE) ] = {
945 [ C(RESULT_ACCESS) ] = -1,
946 [ C(RESULT_MISS) ] = -1,
947 },
948 [ C(OP_PREFETCH) ] = {
949 [ C(RESULT_ACCESS) ] = 0x0,
950 [ C(RESULT_MISS) ] = 0x0,
951 },
952 },
953 [ C(LL ) ] = {
954 [ C(OP_READ) ] = {
63b6a675 955 /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
e994d7d2 956 [ C(RESULT_ACCESS) ] = 0x01b7,
63b6a675
PZ
957 /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
958 [ C(RESULT_MISS) ] = 0x01b7,
f22f54f4 959 },
e994d7d2
AK
960 /*
961 * Use RFO, not WRITEBACK, because a write miss would typically occur
962 * on RFO.
963 */
f22f54f4 964 [ C(OP_WRITE) ] = {
63b6a675
PZ
965 /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
966 [ C(RESULT_ACCESS) ] = 0x01b7,
967 /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
e994d7d2 968 [ C(RESULT_MISS) ] = 0x01b7,
f22f54f4
PZ
969 },
970 [ C(OP_PREFETCH) ] = {
63b6a675 971 /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
e994d7d2 972 [ C(RESULT_ACCESS) ] = 0x01b7,
63b6a675
PZ
973 /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
974 [ C(RESULT_MISS) ] = 0x01b7,
f22f54f4
PZ
975 },
976 },
977 [ C(DTLB) ] = {
978 [ C(OP_READ) ] = {
979 [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
980 [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
981 },
982 [ C(OP_WRITE) ] = {
983 [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
984 [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
985 },
986 [ C(OP_PREFETCH) ] = {
987 [ C(RESULT_ACCESS) ] = 0x0,
988 [ C(RESULT_MISS) ] = 0x0,
989 },
990 },
991 [ C(ITLB) ] = {
992 [ C(OP_READ) ] = {
993 [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
994 [ C(RESULT_MISS) ] = 0x0185, /* ITLB_MISSES.ANY */
995 },
996 [ C(OP_WRITE) ] = {
997 [ C(RESULT_ACCESS) ] = -1,
998 [ C(RESULT_MISS) ] = -1,
999 },
1000 [ C(OP_PREFETCH) ] = {
1001 [ C(RESULT_ACCESS) ] = -1,
1002 [ C(RESULT_MISS) ] = -1,
1003 },
1004 },
1005 [ C(BPU ) ] = {
1006 [ C(OP_READ) ] = {
1007 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
1008 [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
1009 },
1010 [ C(OP_WRITE) ] = {
1011 [ C(RESULT_ACCESS) ] = -1,
1012 [ C(RESULT_MISS) ] = -1,
1013 },
1014 [ C(OP_PREFETCH) ] = {
1015 [ C(RESULT_ACCESS) ] = -1,
1016 [ C(RESULT_MISS) ] = -1,
1017 },
1018 },
89d6c0b5
PZ
1019 [ C(NODE) ] = {
1020 [ C(OP_READ) ] = {
1021 [ C(RESULT_ACCESS) ] = 0x01b7,
1022 [ C(RESULT_MISS) ] = 0x01b7,
1023 },
1024 [ C(OP_WRITE) ] = {
1025 [ C(RESULT_ACCESS) ] = 0x01b7,
1026 [ C(RESULT_MISS) ] = 0x01b7,
1027 },
1028 [ C(OP_PREFETCH) ] = {
1029 [ C(RESULT_ACCESS) ] = 0x01b7,
1030 [ C(RESULT_MISS) ] = 0x01b7,
1031 },
1032 },
f22f54f4
PZ
1033};
1034
e994d7d2 1035/*
63b6a675
PZ
1036 * Nehalem/Westmere MSR_OFFCORE_RESPONSE bits;
1037 * See IA32 SDM Vol 3B 30.6.1.3
e994d7d2
AK
1038 */
1039
63b6a675
PZ
1040#define NHM_DMND_DATA_RD (1 << 0)
1041#define NHM_DMND_RFO (1 << 1)
1042#define NHM_DMND_IFETCH (1 << 2)
1043#define NHM_DMND_WB (1 << 3)
1044#define NHM_PF_DATA_RD (1 << 4)
1045#define NHM_PF_DATA_RFO (1 << 5)
1046#define NHM_PF_IFETCH (1 << 6)
1047#define NHM_OFFCORE_OTHER (1 << 7)
1048#define NHM_UNCORE_HIT (1 << 8)
1049#define NHM_OTHER_CORE_HIT_SNP (1 << 9)
1050#define NHM_OTHER_CORE_HITM (1 << 10)
1051 /* reserved */
1052#define NHM_REMOTE_CACHE_FWD (1 << 12)
1053#define NHM_REMOTE_DRAM (1 << 13)
1054#define NHM_LOCAL_DRAM (1 << 14)
1055#define NHM_NON_DRAM (1 << 15)
1056
87e24f4b
PZ
1057#define NHM_LOCAL (NHM_LOCAL_DRAM|NHM_REMOTE_CACHE_FWD)
1058#define NHM_REMOTE (NHM_REMOTE_DRAM)
63b6a675
PZ
1059
1060#define NHM_DMND_READ (NHM_DMND_DATA_RD)
1061#define NHM_DMND_WRITE (NHM_DMND_RFO|NHM_DMND_WB)
1062#define NHM_DMND_PREFETCH (NHM_PF_DATA_RD|NHM_PF_DATA_RFO)
1063
1064#define NHM_L3_HIT (NHM_UNCORE_HIT|NHM_OTHER_CORE_HIT_SNP|NHM_OTHER_CORE_HITM)
87e24f4b 1065#define NHM_L3_MISS (NHM_NON_DRAM|NHM_LOCAL_DRAM|NHM_REMOTE_DRAM|NHM_REMOTE_CACHE_FWD)
63b6a675 1066#define NHM_L3_ACCESS (NHM_L3_HIT|NHM_L3_MISS)
e994d7d2
AK
1067
1068static __initconst const u64 nehalem_hw_cache_extra_regs
1069 [PERF_COUNT_HW_CACHE_MAX]
1070 [PERF_COUNT_HW_CACHE_OP_MAX]
1071 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1072{
1073 [ C(LL ) ] = {
1074 [ C(OP_READ) ] = {
63b6a675
PZ
1075 [ C(RESULT_ACCESS) ] = NHM_DMND_READ|NHM_L3_ACCESS,
1076 [ C(RESULT_MISS) ] = NHM_DMND_READ|NHM_L3_MISS,
e994d7d2
AK
1077 },
1078 [ C(OP_WRITE) ] = {
63b6a675
PZ
1079 [ C(RESULT_ACCESS) ] = NHM_DMND_WRITE|NHM_L3_ACCESS,
1080 [ C(RESULT_MISS) ] = NHM_DMND_WRITE|NHM_L3_MISS,
e994d7d2
AK
1081 },
1082 [ C(OP_PREFETCH) ] = {
63b6a675
PZ
1083 [ C(RESULT_ACCESS) ] = NHM_DMND_PREFETCH|NHM_L3_ACCESS,
1084 [ C(RESULT_MISS) ] = NHM_DMND_PREFETCH|NHM_L3_MISS,
e994d7d2 1085 },
89d6c0b5
PZ
1086 },
1087 [ C(NODE) ] = {
1088 [ C(OP_READ) ] = {
87e24f4b
PZ
1089 [ C(RESULT_ACCESS) ] = NHM_DMND_READ|NHM_LOCAL|NHM_REMOTE,
1090 [ C(RESULT_MISS) ] = NHM_DMND_READ|NHM_REMOTE,
89d6c0b5
PZ
1091 },
1092 [ C(OP_WRITE) ] = {
87e24f4b
PZ
1093 [ C(RESULT_ACCESS) ] = NHM_DMND_WRITE|NHM_LOCAL|NHM_REMOTE,
1094 [ C(RESULT_MISS) ] = NHM_DMND_WRITE|NHM_REMOTE,
89d6c0b5
PZ
1095 },
1096 [ C(OP_PREFETCH) ] = {
87e24f4b
PZ
1097 [ C(RESULT_ACCESS) ] = NHM_DMND_PREFETCH|NHM_LOCAL|NHM_REMOTE,
1098 [ C(RESULT_MISS) ] = NHM_DMND_PREFETCH|NHM_REMOTE,
89d6c0b5
PZ
1099 },
1100 },
e994d7d2
AK
1101};
1102
caaa8be3 1103static __initconst const u64 nehalem_hw_cache_event_ids
f22f54f4
PZ
1104 [PERF_COUNT_HW_CACHE_MAX]
1105 [PERF_COUNT_HW_CACHE_OP_MAX]
1106 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1107{
1108 [ C(L1D) ] = {
1109 [ C(OP_READ) ] = {
f4929bd3
PZ
1110 [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
1111 [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPL */
f22f54f4
PZ
1112 },
1113 [ C(OP_WRITE) ] = {
f4929bd3
PZ
1114 [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
1115 [ C(RESULT_MISS) ] = 0x0251, /* L1D.M_REPL */
f22f54f4
PZ
1116 },
1117 [ C(OP_PREFETCH) ] = {
1118 [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
1119 [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
1120 },
1121 },
1122 [ C(L1I ) ] = {
1123 [ C(OP_READ) ] = {
1124 [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
1125 [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
1126 },
1127 [ C(OP_WRITE) ] = {
1128 [ C(RESULT_ACCESS) ] = -1,
1129 [ C(RESULT_MISS) ] = -1,
1130 },
1131 [ C(OP_PREFETCH) ] = {
1132 [ C(RESULT_ACCESS) ] = 0x0,
1133 [ C(RESULT_MISS) ] = 0x0,
1134 },
1135 },
1136 [ C(LL ) ] = {
1137 [ C(OP_READ) ] = {
e994d7d2
AK
1138 /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
1139 [ C(RESULT_ACCESS) ] = 0x01b7,
1140 /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
1141 [ C(RESULT_MISS) ] = 0x01b7,
f22f54f4 1142 },
e994d7d2
AK
1143 /*
1144 * Use RFO, not WRITEBACK, because a write miss would typically occur
1145 * on RFO.
1146 */
f22f54f4 1147 [ C(OP_WRITE) ] = {
e994d7d2
AK
1148 /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
1149 [ C(RESULT_ACCESS) ] = 0x01b7,
1150 /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
1151 [ C(RESULT_MISS) ] = 0x01b7,
f22f54f4
PZ
1152 },
1153 [ C(OP_PREFETCH) ] = {
e994d7d2
AK
1154 /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
1155 [ C(RESULT_ACCESS) ] = 0x01b7,
1156 /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
1157 [ C(RESULT_MISS) ] = 0x01b7,
f22f54f4
PZ
1158 },
1159 },
1160 [ C(DTLB) ] = {
1161 [ C(OP_READ) ] = {
1162 [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
1163 [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
1164 },
1165 [ C(OP_WRITE) ] = {
1166 [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
1167 [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
1168 },
1169 [ C(OP_PREFETCH) ] = {
1170 [ C(RESULT_ACCESS) ] = 0x0,
1171 [ C(RESULT_MISS) ] = 0x0,
1172 },
1173 },
1174 [ C(ITLB) ] = {
1175 [ C(OP_READ) ] = {
1176 [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
1177 [ C(RESULT_MISS) ] = 0x20c8, /* ITLB_MISS_RETIRED */
1178 },
1179 [ C(OP_WRITE) ] = {
1180 [ C(RESULT_ACCESS) ] = -1,
1181 [ C(RESULT_MISS) ] = -1,
1182 },
1183 [ C(OP_PREFETCH) ] = {
1184 [ C(RESULT_ACCESS) ] = -1,
1185 [ C(RESULT_MISS) ] = -1,
1186 },
1187 },
1188 [ C(BPU ) ] = {
1189 [ C(OP_READ) ] = {
1190 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
1191 [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
1192 },
1193 [ C(OP_WRITE) ] = {
1194 [ C(RESULT_ACCESS) ] = -1,
1195 [ C(RESULT_MISS) ] = -1,
1196 },
1197 [ C(OP_PREFETCH) ] = {
1198 [ C(RESULT_ACCESS) ] = -1,
1199 [ C(RESULT_MISS) ] = -1,
1200 },
1201 },
89d6c0b5
PZ
1202 [ C(NODE) ] = {
1203 [ C(OP_READ) ] = {
1204 [ C(RESULT_ACCESS) ] = 0x01b7,
1205 [ C(RESULT_MISS) ] = 0x01b7,
1206 },
1207 [ C(OP_WRITE) ] = {
1208 [ C(RESULT_ACCESS) ] = 0x01b7,
1209 [ C(RESULT_MISS) ] = 0x01b7,
1210 },
1211 [ C(OP_PREFETCH) ] = {
1212 [ C(RESULT_ACCESS) ] = 0x01b7,
1213 [ C(RESULT_MISS) ] = 0x01b7,
1214 },
1215 },
f22f54f4
PZ
1216};
1217
caaa8be3 1218static __initconst const u64 core2_hw_cache_event_ids
f22f54f4
PZ
1219 [PERF_COUNT_HW_CACHE_MAX]
1220 [PERF_COUNT_HW_CACHE_OP_MAX]
1221 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1222{
1223 [ C(L1D) ] = {
1224 [ C(OP_READ) ] = {
1225 [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
1226 [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
1227 },
1228 [ C(OP_WRITE) ] = {
1229 [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
1230 [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
1231 },
1232 [ C(OP_PREFETCH) ] = {
1233 [ C(RESULT_ACCESS) ] = 0x104e, /* L1D_PREFETCH.REQUESTS */
1234 [ C(RESULT_MISS) ] = 0,
1235 },
1236 },
1237 [ C(L1I ) ] = {
1238 [ C(OP_READ) ] = {
1239 [ C(RESULT_ACCESS) ] = 0x0080, /* L1I.READS */
1240 [ C(RESULT_MISS) ] = 0x0081, /* L1I.MISSES */
1241 },
1242 [ C(OP_WRITE) ] = {
1243 [ C(RESULT_ACCESS) ] = -1,
1244 [ C(RESULT_MISS) ] = -1,
1245 },
1246 [ C(OP_PREFETCH) ] = {
1247 [ C(RESULT_ACCESS) ] = 0,
1248 [ C(RESULT_MISS) ] = 0,
1249 },
1250 },
1251 [ C(LL ) ] = {
1252 [ C(OP_READ) ] = {
1253 [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
1254 [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
1255 },
1256 [ C(OP_WRITE) ] = {
1257 [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
1258 [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
1259 },
1260 [ C(OP_PREFETCH) ] = {
1261 [ C(RESULT_ACCESS) ] = 0,
1262 [ C(RESULT_MISS) ] = 0,
1263 },
1264 },
1265 [ C(DTLB) ] = {
1266 [ C(OP_READ) ] = {
1267 [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
1268 [ C(RESULT_MISS) ] = 0x0208, /* DTLB_MISSES.MISS_LD */
1269 },
1270 [ C(OP_WRITE) ] = {
1271 [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
1272 [ C(RESULT_MISS) ] = 0x0808, /* DTLB_MISSES.MISS_ST */
1273 },
1274 [ C(OP_PREFETCH) ] = {
1275 [ C(RESULT_ACCESS) ] = 0,
1276 [ C(RESULT_MISS) ] = 0,
1277 },
1278 },
1279 [ C(ITLB) ] = {
1280 [ C(OP_READ) ] = {
1281 [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
1282 [ C(RESULT_MISS) ] = 0x1282, /* ITLBMISSES */
1283 },
1284 [ C(OP_WRITE) ] = {
1285 [ C(RESULT_ACCESS) ] = -1,
1286 [ C(RESULT_MISS) ] = -1,
1287 },
1288 [ C(OP_PREFETCH) ] = {
1289 [ C(RESULT_ACCESS) ] = -1,
1290 [ C(RESULT_MISS) ] = -1,
1291 },
1292 },
1293 [ C(BPU ) ] = {
1294 [ C(OP_READ) ] = {
1295 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
1296 [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
1297 },
1298 [ C(OP_WRITE) ] = {
1299 [ C(RESULT_ACCESS) ] = -1,
1300 [ C(RESULT_MISS) ] = -1,
1301 },
1302 [ C(OP_PREFETCH) ] = {
1303 [ C(RESULT_ACCESS) ] = -1,
1304 [ C(RESULT_MISS) ] = -1,
1305 },
1306 },
1307};
1308
caaa8be3 1309static __initconst const u64 atom_hw_cache_event_ids
f22f54f4
PZ
1310 [PERF_COUNT_HW_CACHE_MAX]
1311 [PERF_COUNT_HW_CACHE_OP_MAX]
1312 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1313{
1314 [ C(L1D) ] = {
1315 [ C(OP_READ) ] = {
1316 [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE.LD */
1317 [ C(RESULT_MISS) ] = 0,
1318 },
1319 [ C(OP_WRITE) ] = {
1320 [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE.ST */
1321 [ C(RESULT_MISS) ] = 0,
1322 },
1323 [ C(OP_PREFETCH) ] = {
1324 [ C(RESULT_ACCESS) ] = 0x0,
1325 [ C(RESULT_MISS) ] = 0,
1326 },
1327 },
1328 [ C(L1I ) ] = {
1329 [ C(OP_READ) ] = {
1330 [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
1331 [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
1332 },
1333 [ C(OP_WRITE) ] = {
1334 [ C(RESULT_ACCESS) ] = -1,
1335 [ C(RESULT_MISS) ] = -1,
1336 },
1337 [ C(OP_PREFETCH) ] = {
1338 [ C(RESULT_ACCESS) ] = 0,
1339 [ C(RESULT_MISS) ] = 0,
1340 },
1341 },
1342 [ C(LL ) ] = {
1343 [ C(OP_READ) ] = {
1344 [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
1345 [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
1346 },
1347 [ C(OP_WRITE) ] = {
1348 [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
1349 [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
1350 },
1351 [ C(OP_PREFETCH) ] = {
1352 [ C(RESULT_ACCESS) ] = 0,
1353 [ C(RESULT_MISS) ] = 0,
1354 },
1355 },
1356 [ C(DTLB) ] = {
1357 [ C(OP_READ) ] = {
1358 [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE_LD.MESI (alias) */
1359 [ C(RESULT_MISS) ] = 0x0508, /* DTLB_MISSES.MISS_LD */
1360 },
1361 [ C(OP_WRITE) ] = {
1362 [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE_ST.MESI (alias) */
1363 [ C(RESULT_MISS) ] = 0x0608, /* DTLB_MISSES.MISS_ST */
1364 },
1365 [ C(OP_PREFETCH) ] = {
1366 [ C(RESULT_ACCESS) ] = 0,
1367 [ C(RESULT_MISS) ] = 0,
1368 },
1369 },
1370 [ C(ITLB) ] = {
1371 [ C(OP_READ) ] = {
1372 [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
1373 [ C(RESULT_MISS) ] = 0x0282, /* ITLB.MISSES */
1374 },
1375 [ C(OP_WRITE) ] = {
1376 [ C(RESULT_ACCESS) ] = -1,
1377 [ C(RESULT_MISS) ] = -1,
1378 },
1379 [ C(OP_PREFETCH) ] = {
1380 [ C(RESULT_ACCESS) ] = -1,
1381 [ C(RESULT_MISS) ] = -1,
1382 },
1383 },
1384 [ C(BPU ) ] = {
1385 [ C(OP_READ) ] = {
1386 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
1387 [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
1388 },
1389 [ C(OP_WRITE) ] = {
1390 [ C(RESULT_ACCESS) ] = -1,
1391 [ C(RESULT_MISS) ] = -1,
1392 },
1393 [ C(OP_PREFETCH) ] = {
1394 [ C(RESULT_ACCESS) ] = -1,
1395 [ C(RESULT_MISS) ] = -1,
1396 },
1397 },
1398};
1399
eb12b8ec
AK
1400EVENT_ATTR_STR(topdown-total-slots, td_total_slots_slm, "event=0x3c");
1401EVENT_ATTR_STR(topdown-total-slots.scale, td_total_slots_scale_slm, "2");
1402/* no_alloc_cycles.not_delivered */
1403EVENT_ATTR_STR(topdown-fetch-bubbles, td_fetch_bubbles_slm,
1404 "event=0xca,umask=0x50");
1405EVENT_ATTR_STR(topdown-fetch-bubbles.scale, td_fetch_bubbles_scale_slm, "2");
1406/* uops_retired.all */
1407EVENT_ATTR_STR(topdown-slots-issued, td_slots_issued_slm,
1408 "event=0xc2,umask=0x10");
1409/* uops_retired.all */
1410EVENT_ATTR_STR(topdown-slots-retired, td_slots_retired_slm,
1411 "event=0xc2,umask=0x10");
1412
1413static struct attribute *slm_events_attrs[] = {
1414 EVENT_PTR(td_total_slots_slm),
1415 EVENT_PTR(td_total_slots_scale_slm),
1416 EVENT_PTR(td_fetch_bubbles_slm),
1417 EVENT_PTR(td_fetch_bubbles_scale_slm),
1418 EVENT_PTR(td_slots_issued_slm),
1419 EVENT_PTR(td_slots_retired_slm),
1420 NULL
1421};
1422
1fa64180
YZ
1423static struct extra_reg intel_slm_extra_regs[] __read_mostly =
1424{
1425 /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
06c939c1 1426 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x768005ffffull, RSP_0),
ae3f011f 1427 INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OFFCORE_RSP_1, 0x368005ffffull, RSP_1),
1fa64180
YZ
1428 EVENT_EXTRA_END
1429};
1430
1431#define SLM_DMND_READ SNB_DMND_DATA_RD
1432#define SLM_DMND_WRITE SNB_DMND_RFO
1433#define SLM_DMND_PREFETCH (SNB_PF_DATA_RD|SNB_PF_RFO)
1434
1435#define SLM_SNP_ANY (SNB_SNP_NONE|SNB_SNP_MISS|SNB_NO_FWD|SNB_HITM)
1436#define SLM_LLC_ACCESS SNB_RESP_ANY
1437#define SLM_LLC_MISS (SLM_SNP_ANY|SNB_NON_DRAM)
1438
1439static __initconst const u64 slm_hw_cache_extra_regs
1440 [PERF_COUNT_HW_CACHE_MAX]
1441 [PERF_COUNT_HW_CACHE_OP_MAX]
1442 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1443{
1444 [ C(LL ) ] = {
1445 [ C(OP_READ) ] = {
1446 [ C(RESULT_ACCESS) ] = SLM_DMND_READ|SLM_LLC_ACCESS,
6d374056 1447 [ C(RESULT_MISS) ] = 0,
1fa64180
YZ
1448 },
1449 [ C(OP_WRITE) ] = {
1450 [ C(RESULT_ACCESS) ] = SLM_DMND_WRITE|SLM_LLC_ACCESS,
1451 [ C(RESULT_MISS) ] = SLM_DMND_WRITE|SLM_LLC_MISS,
1452 },
1453 [ C(OP_PREFETCH) ] = {
1454 [ C(RESULT_ACCESS) ] = SLM_DMND_PREFETCH|SLM_LLC_ACCESS,
1455 [ C(RESULT_MISS) ] = SLM_DMND_PREFETCH|SLM_LLC_MISS,
1456 },
1457 },
1458};
1459
1460static __initconst const u64 slm_hw_cache_event_ids
1461 [PERF_COUNT_HW_CACHE_MAX]
1462 [PERF_COUNT_HW_CACHE_OP_MAX]
1463 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
1464{
1465 [ C(L1D) ] = {
1466 [ C(OP_READ) ] = {
1467 [ C(RESULT_ACCESS) ] = 0,
1468 [ C(RESULT_MISS) ] = 0x0104, /* LD_DCU_MISS */
1469 },
1470 [ C(OP_WRITE) ] = {
1471 [ C(RESULT_ACCESS) ] = 0,
1472 [ C(RESULT_MISS) ] = 0,
1473 },
1474 [ C(OP_PREFETCH) ] = {
1475 [ C(RESULT_ACCESS) ] = 0,
1476 [ C(RESULT_MISS) ] = 0,
1477 },
1478 },
1479 [ C(L1I ) ] = {
1480 [ C(OP_READ) ] = {
1481 [ C(RESULT_ACCESS) ] = 0x0380, /* ICACHE.ACCESSES */
1482 [ C(RESULT_MISS) ] = 0x0280, /* ICACGE.MISSES */
1483 },
1484 [ C(OP_WRITE) ] = {
1485 [ C(RESULT_ACCESS) ] = -1,
1486 [ C(RESULT_MISS) ] = -1,
1487 },
1488 [ C(OP_PREFETCH) ] = {
1489 [ C(RESULT_ACCESS) ] = 0,
1490 [ C(RESULT_MISS) ] = 0,
1491 },
1492 },
1493 [ C(LL ) ] = {
1494 [ C(OP_READ) ] = {
1495 /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
1496 [ C(RESULT_ACCESS) ] = 0x01b7,
6d374056 1497 [ C(RESULT_MISS) ] = 0,
1fa64180
YZ
1498 },
1499 [ C(OP_WRITE) ] = {
1500 /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
1501 [ C(RESULT_ACCESS) ] = 0x01b7,
1502 /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
1503 [ C(RESULT_MISS) ] = 0x01b7,
1504 },
1505 [ C(OP_PREFETCH) ] = {
1506 /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
1507 [ C(RESULT_ACCESS) ] = 0x01b7,
1508 /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
1509 [ C(RESULT_MISS) ] = 0x01b7,
1510 },
1511 },
1512 [ C(DTLB) ] = {
1513 [ C(OP_READ) ] = {
1514 [ C(RESULT_ACCESS) ] = 0,
1515 [ C(RESULT_MISS) ] = 0x0804, /* LD_DTLB_MISS */
1516 },
1517 [ C(OP_WRITE) ] = {
1518 [ C(RESULT_ACCESS) ] = 0,
1519 [ C(RESULT_MISS) ] = 0,
1520 },
1521 [ C(OP_PREFETCH) ] = {
1522 [ C(RESULT_ACCESS) ] = 0,
1523 [ C(RESULT_MISS) ] = 0,
1524 },
1525 },
1526 [ C(ITLB) ] = {
1527 [ C(OP_READ) ] = {
1528 [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
6d374056 1529 [ C(RESULT_MISS) ] = 0x40205, /* PAGE_WALKS.I_SIDE_WALKS */
1fa64180
YZ
1530 },
1531 [ C(OP_WRITE) ] = {
1532 [ C(RESULT_ACCESS) ] = -1,
1533 [ C(RESULT_MISS) ] = -1,
1534 },
1535 [ C(OP_PREFETCH) ] = {
1536 [ C(RESULT_ACCESS) ] = -1,
1537 [ C(RESULT_MISS) ] = -1,
1538 },
1539 },
1540 [ C(BPU ) ] = {
1541 [ C(OP_READ) ] = {
1542 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
1543 [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
1544 },
1545 [ C(OP_WRITE) ] = {
1546 [ C(RESULT_ACCESS) ] = -1,
1547 [ C(RESULT_MISS) ] = -1,
1548 },
1549 [ C(OP_PREFETCH) ] = {
1550 [ C(RESULT_ACCESS) ] = -1,
1551 [ C(RESULT_MISS) ] = -1,
1552 },
1553 },
1554};
1555
8b92c3a7
KL
1556static struct extra_reg intel_glm_extra_regs[] __read_mostly = {
1557 /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
1558 INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x760005ffbfull, RSP_0),
1559 INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OFFCORE_RSP_1, 0x360005ffbfull, RSP_1),
1560 EVENT_EXTRA_END
1561};
1562
1563#define GLM_DEMAND_DATA_RD BIT_ULL(0)
1564#define GLM_DEMAND_RFO BIT_ULL(1)
1565#define GLM_ANY_RESPONSE BIT_ULL(16)
1566#define GLM_SNP_NONE_OR_MISS BIT_ULL(33)
1567#define GLM_DEMAND_READ GLM_DEMAND_DATA_RD
1568#define GLM_DEMAND_WRITE GLM_DEMAND_RFO
1569#define GLM_DEMAND_PREFETCH (SNB_PF_DATA_RD|SNB_PF_RFO)
1570#define GLM_LLC_ACCESS GLM_ANY_RESPONSE
1571#define GLM_SNP_ANY (GLM_SNP_NONE_OR_MISS|SNB_NO_FWD|SNB_HITM)
1572#define GLM_LLC_MISS (GLM_SNP_ANY|SNB_NON_DRAM)
1573
1574static __initconst const u64 glm_hw_cache_event_ids
1575 [PERF_COUNT_HW_CACHE_MAX]
1576 [PERF_COUNT_HW_CACHE_OP_MAX]
1577 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
1578 [C(L1D)] = {
1579 [C(OP_READ)] = {
1580 [C(RESULT_ACCESS)] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
1581 [C(RESULT_MISS)] = 0x0,
1582 },
1583 [C(OP_WRITE)] = {
1584 [C(RESULT_ACCESS)] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
1585 [C(RESULT_MISS)] = 0x0,
1586 },
1587 [C(OP_PREFETCH)] = {
1588 [C(RESULT_ACCESS)] = 0x0,
1589 [C(RESULT_MISS)] = 0x0,
1590 },
1591 },
1592 [C(L1I)] = {
1593 [C(OP_READ)] = {
1594 [C(RESULT_ACCESS)] = 0x0380, /* ICACHE.ACCESSES */
1595 [C(RESULT_MISS)] = 0x0280, /* ICACHE.MISSES */
1596 },
1597 [C(OP_WRITE)] = {
1598 [C(RESULT_ACCESS)] = -1,
1599 [C(RESULT_MISS)] = -1,
1600 },
1601 [C(OP_PREFETCH)] = {
1602 [C(RESULT_ACCESS)] = 0x0,
1603 [C(RESULT_MISS)] = 0x0,
1604 },
1605 },
1606 [C(LL)] = {
1607 [C(OP_READ)] = {
1608 [C(RESULT_ACCESS)] = 0x1b7, /* OFFCORE_RESPONSE */
1609 [C(RESULT_MISS)] = 0x1b7, /* OFFCORE_RESPONSE */
1610 },
1611 [C(OP_WRITE)] = {
1612 [C(RESULT_ACCESS)] = 0x1b7, /* OFFCORE_RESPONSE */
1613 [C(RESULT_MISS)] = 0x1b7, /* OFFCORE_RESPONSE */
1614 },
1615 [C(OP_PREFETCH)] = {
1616 [C(RESULT_ACCESS)] = 0x1b7, /* OFFCORE_RESPONSE */
1617 [C(RESULT_MISS)] = 0x1b7, /* OFFCORE_RESPONSE */
1618 },
1619 },
1620 [C(DTLB)] = {
1621 [C(OP_READ)] = {
1622 [C(RESULT_ACCESS)] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
1623 [C(RESULT_MISS)] = 0x0,
1624 },
1625 [C(OP_WRITE)] = {
1626 [C(RESULT_ACCESS)] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
1627 [C(RESULT_MISS)] = 0x0,
1628 },
1629 [C(OP_PREFETCH)] = {
1630 [C(RESULT_ACCESS)] = 0x0,
1631 [C(RESULT_MISS)] = 0x0,
1632 },
1633 },
1634 [C(ITLB)] = {
1635 [C(OP_READ)] = {
1636 [C(RESULT_ACCESS)] = 0x00c0, /* INST_RETIRED.ANY_P */
1637 [C(RESULT_MISS)] = 0x0481, /* ITLB.MISS */
1638 },
1639 [C(OP_WRITE)] = {
1640 [C(RESULT_ACCESS)] = -1,
1641 [C(RESULT_MISS)] = -1,
1642 },
1643 [C(OP_PREFETCH)] = {
1644 [C(RESULT_ACCESS)] = -1,
1645 [C(RESULT_MISS)] = -1,
1646 },
1647 },
1648 [C(BPU)] = {
1649 [C(OP_READ)] = {
1650 [C(RESULT_ACCESS)] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
1651 [C(RESULT_MISS)] = 0x00c5, /* BR_MISP_RETIRED.ALL_BRANCHES */
1652 },
1653 [C(OP_WRITE)] = {
1654 [C(RESULT_ACCESS)] = -1,
1655 [C(RESULT_MISS)] = -1,
1656 },
1657 [C(OP_PREFETCH)] = {
1658 [C(RESULT_ACCESS)] = -1,
1659 [C(RESULT_MISS)] = -1,
1660 },
1661 },
1662};
1663
1664static __initconst const u64 glm_hw_cache_extra_regs
1665 [PERF_COUNT_HW_CACHE_MAX]
1666 [PERF_COUNT_HW_CACHE_OP_MAX]
1667 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
1668 [C(LL)] = {
1669 [C(OP_READ)] = {
1670 [C(RESULT_ACCESS)] = GLM_DEMAND_READ|
1671 GLM_LLC_ACCESS,
1672 [C(RESULT_MISS)] = GLM_DEMAND_READ|
1673 GLM_LLC_MISS,
1674 },
1675 [C(OP_WRITE)] = {
1676 [C(RESULT_ACCESS)] = GLM_DEMAND_WRITE|
1677 GLM_LLC_ACCESS,
1678 [C(RESULT_MISS)] = GLM_DEMAND_WRITE|
1679 GLM_LLC_MISS,
1680 },
1681 [C(OP_PREFETCH)] = {
1682 [C(RESULT_ACCESS)] = GLM_DEMAND_PREFETCH|
1683 GLM_LLC_ACCESS,
1684 [C(RESULT_MISS)] = GLM_DEMAND_PREFETCH|
1685 GLM_LLC_MISS,
1686 },
1687 },
1688};
1689
1e7b9390
HC
1690#define KNL_OT_L2_HITE BIT_ULL(19) /* Other Tile L2 Hit */
1691#define KNL_OT_L2_HITF BIT_ULL(20) /* Other Tile L2 Hit */
1692#define KNL_MCDRAM_LOCAL BIT_ULL(21)
1693#define KNL_MCDRAM_FAR BIT_ULL(22)
1694#define KNL_DDR_LOCAL BIT_ULL(23)
1695#define KNL_DDR_FAR BIT_ULL(24)
1696#define KNL_DRAM_ANY (KNL_MCDRAM_LOCAL | KNL_MCDRAM_FAR | \
1697 KNL_DDR_LOCAL | KNL_DDR_FAR)
1698#define KNL_L2_READ SLM_DMND_READ
1699#define KNL_L2_WRITE SLM_DMND_WRITE
1700#define KNL_L2_PREFETCH SLM_DMND_PREFETCH
1701#define KNL_L2_ACCESS SLM_LLC_ACCESS
1702#define KNL_L2_MISS (KNL_OT_L2_HITE | KNL_OT_L2_HITF | \
1703 KNL_DRAM_ANY | SNB_SNP_ANY | \
1704 SNB_NON_DRAM)
1705
1706static __initconst const u64 knl_hw_cache_extra_regs
1707 [PERF_COUNT_HW_CACHE_MAX]
1708 [PERF_COUNT_HW_CACHE_OP_MAX]
1709 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
1710 [C(LL)] = {
1711 [C(OP_READ)] = {
1712 [C(RESULT_ACCESS)] = KNL_L2_READ | KNL_L2_ACCESS,
1713 [C(RESULT_MISS)] = 0,
1714 },
1715 [C(OP_WRITE)] = {
1716 [C(RESULT_ACCESS)] = KNL_L2_WRITE | KNL_L2_ACCESS,
1717 [C(RESULT_MISS)] = KNL_L2_WRITE | KNL_L2_MISS,
1718 },
1719 [C(OP_PREFETCH)] = {
1720 [C(RESULT_ACCESS)] = KNL_L2_PREFETCH | KNL_L2_ACCESS,
1721 [C(RESULT_MISS)] = KNL_L2_PREFETCH | KNL_L2_MISS,
1722 },
1723 },
1724};
1725
1a78d937 1726/*
c3d266c8
KL
1727 * Used from PMIs where the LBRs are already disabled.
1728 *
1729 * This function could be called consecutively. It is required to remain in
1730 * disabled state if called consecutively.
1731 *
1732 * During consecutive calls, the same disable value will be written to related
1733 * registers, so the PMU state remains unchanged. hw.state in
1734 * intel_bts_disable_local will remain PERF_HES_STOPPED too in consecutive
1735 * calls.
1a78d937
AK
1736 */
1737static void __intel_pmu_disable_all(void)
f22f54f4 1738{
89cbc767 1739 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
f22f54f4
PZ
1740
1741 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
1742
15c7ad51 1743 if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask))
f22f54f4 1744 intel_pmu_disable_bts();
8062382c
AS
1745 else
1746 intel_bts_disable_local();
ca037701
PZ
1747
1748 intel_pmu_pebs_disable_all();
1a78d937
AK
1749}
1750
1751static void intel_pmu_disable_all(void)
1752{
1753 __intel_pmu_disable_all();
caff2bef 1754 intel_pmu_lbr_disable_all();
f22f54f4
PZ
1755}
1756
1a78d937 1757static void __intel_pmu_enable_all(int added, bool pmi)
f22f54f4 1758{
89cbc767 1759 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
f22f54f4 1760
d329527e 1761 intel_pmu_pebs_enable_all();
1a78d937 1762 intel_pmu_lbr_enable_all(pmi);
144d31e6
GN
1763 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL,
1764 x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_guest_mask);
f22f54f4 1765
15c7ad51 1766 if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask)) {
f22f54f4 1767 struct perf_event *event =
15c7ad51 1768 cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
f22f54f4
PZ
1769
1770 if (WARN_ON_ONCE(!event))
1771 return;
1772
1773 intel_pmu_enable_bts(event->hw.config);
8062382c
AS
1774 } else
1775 intel_bts_enable_local();
f22f54f4
PZ
1776}
1777
1a78d937
AK
1778static void intel_pmu_enable_all(int added)
1779{
1780 __intel_pmu_enable_all(added, false);
1781}
1782
11164cd4
PZ
1783/*
1784 * Workaround for:
1785 * Intel Errata AAK100 (model 26)
1786 * Intel Errata AAP53 (model 30)
40b91cd1 1787 * Intel Errata BD53 (model 44)
11164cd4 1788 *
351af072
ZY
1789 * The official story:
1790 * These chips need to be 'reset' when adding counters by programming the
1791 * magic three (non-counting) events 0x4300B5, 0x4300D2, and 0x4300B1 either
1792 * in sequence on the same PMC or on different PMCs.
1793 *
1794 * In practise it appears some of these events do in fact count, and
1795 * we need to programm all 4 events.
11164cd4 1796 */
351af072 1797static void intel_pmu_nhm_workaround(void)
11164cd4 1798{
89cbc767 1799 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
351af072
ZY
1800 static const unsigned long nhm_magic[4] = {
1801 0x4300B5,
1802 0x4300D2,
1803 0x4300B1,
1804 0x4300B1
1805 };
1806 struct perf_event *event;
1807 int i;
11164cd4 1808
351af072
ZY
1809 /*
1810 * The Errata requires below steps:
1811 * 1) Clear MSR_IA32_PEBS_ENABLE and MSR_CORE_PERF_GLOBAL_CTRL;
1812 * 2) Configure 4 PERFEVTSELx with the magic events and clear
1813 * the corresponding PMCx;
1814 * 3) set bit0~bit3 of MSR_CORE_PERF_GLOBAL_CTRL;
1815 * 4) Clear MSR_CORE_PERF_GLOBAL_CTRL;
1816 * 5) Clear 4 pairs of ERFEVTSELx and PMCx;
1817 */
11164cd4 1818
351af072
ZY
1819 /*
1820 * The real steps we choose are a little different from above.
1821 * A) To reduce MSR operations, we don't run step 1) as they
1822 * are already cleared before this function is called;
1823 * B) Call x86_perf_event_update to save PMCx before configuring
1824 * PERFEVTSELx with magic number;
1825 * C) With step 5), we do clear only when the PERFEVTSELx is
1826 * not used currently.
1827 * D) Call x86_perf_event_set_period to restore PMCx;
1828 */
11164cd4 1829
351af072
ZY
1830 /* We always operate 4 pairs of PERF Counters */
1831 for (i = 0; i < 4; i++) {
1832 event = cpuc->events[i];
1833 if (event)
1834 x86_perf_event_update(event);
1835 }
11164cd4 1836
351af072
ZY
1837 for (i = 0; i < 4; i++) {
1838 wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + i, nhm_magic[i]);
1839 wrmsrl(MSR_ARCH_PERFMON_PERFCTR0 + i, 0x0);
1840 }
1841
1842 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0xf);
1843 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0x0);
11164cd4 1844
351af072
ZY
1845 for (i = 0; i < 4; i++) {
1846 event = cpuc->events[i];
1847
1848 if (event) {
1849 x86_perf_event_set_period(event);
31fa58af 1850 __x86_pmu_enable_event(&event->hw,
351af072
ZY
1851 ARCH_PERFMON_EVENTSEL_ENABLE);
1852 } else
1853 wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + i, 0x0);
11164cd4 1854 }
351af072
ZY
1855}
1856
1857static void intel_pmu_nhm_enable_all(int added)
1858{
1859 if (added)
1860 intel_pmu_nhm_workaround();
11164cd4
PZ
1861 intel_pmu_enable_all(added);
1862}
1863
f22f54f4
PZ
1864static inline u64 intel_pmu_get_status(void)
1865{
1866 u64 status;
1867
1868 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
1869
1870 return status;
1871}
1872
1873static inline void intel_pmu_ack_status(u64 ack)
1874{
1875 wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
1876}
1877
ca037701 1878static void intel_pmu_disable_fixed(struct hw_perf_event *hwc)
f22f54f4 1879{
15c7ad51 1880 int idx = hwc->idx - INTEL_PMC_IDX_FIXED;
f22f54f4
PZ
1881 u64 ctrl_val, mask;
1882
1883 mask = 0xfULL << (idx * 4);
1884
1885 rdmsrl(hwc->config_base, ctrl_val);
1886 ctrl_val &= ~mask;
7645a24c 1887 wrmsrl(hwc->config_base, ctrl_val);
f22f54f4
PZ
1888}
1889
2b9e344d
PZ
1890static inline bool event_is_checkpointed(struct perf_event *event)
1891{
1892 return (event->hw.config & HSW_IN_TX_CHECKPOINTED) != 0;
1893}
1894
ca037701 1895static void intel_pmu_disable_event(struct perf_event *event)
f22f54f4 1896{
aff3d91a 1897 struct hw_perf_event *hwc = &event->hw;
89cbc767 1898 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
aff3d91a 1899
15c7ad51 1900 if (unlikely(hwc->idx == INTEL_PMC_IDX_FIXED_BTS)) {
f22f54f4
PZ
1901 intel_pmu_disable_bts();
1902 intel_pmu_drain_bts_buffer();
1903 return;
1904 }
1905
144d31e6
GN
1906 cpuc->intel_ctrl_guest_mask &= ~(1ull << hwc->idx);
1907 cpuc->intel_ctrl_host_mask &= ~(1ull << hwc->idx);
2b9e344d 1908 cpuc->intel_cp_status &= ~(1ull << hwc->idx);
144d31e6 1909
f22f54f4 1910 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
aff3d91a 1911 intel_pmu_disable_fixed(hwc);
f22f54f4
PZ
1912 return;
1913 }
1914
aff3d91a 1915 x86_pmu_disable_event(event);
ca037701 1916
ab608344 1917 if (unlikely(event->attr.precise_ip))
ef21f683 1918 intel_pmu_pebs_disable(event);
f22f54f4
PZ
1919}
1920
68f7082f
PZ
1921static void intel_pmu_del_event(struct perf_event *event)
1922{
1923 if (needs_branch_stack(event))
1924 intel_pmu_lbr_del(event);
1925 if (event->attr.precise_ip)
1926 intel_pmu_pebs_del(event);
1927}
1928
ca037701 1929static void intel_pmu_enable_fixed(struct hw_perf_event *hwc)
f22f54f4 1930{
15c7ad51 1931 int idx = hwc->idx - INTEL_PMC_IDX_FIXED;
f22f54f4 1932 u64 ctrl_val, bits, mask;
f22f54f4
PZ
1933
1934 /*
1935 * Enable IRQ generation (0x8),
1936 * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
1937 * if requested:
1938 */
1939 bits = 0x8ULL;
1940 if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
1941 bits |= 0x2;
1942 if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
1943 bits |= 0x1;
1944
1945 /*
1946 * ANY bit is supported in v3 and up
1947 */
1948 if (x86_pmu.version > 2 && hwc->config & ARCH_PERFMON_EVENTSEL_ANY)
1949 bits |= 0x4;
1950
1951 bits <<= (idx * 4);
1952 mask = 0xfULL << (idx * 4);
1953
1954 rdmsrl(hwc->config_base, ctrl_val);
1955 ctrl_val &= ~mask;
1956 ctrl_val |= bits;
7645a24c 1957 wrmsrl(hwc->config_base, ctrl_val);
f22f54f4
PZ
1958}
1959
aff3d91a 1960static void intel_pmu_enable_event(struct perf_event *event)
f22f54f4 1961{
aff3d91a 1962 struct hw_perf_event *hwc = &event->hw;
89cbc767 1963 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
aff3d91a 1964
15c7ad51 1965 if (unlikely(hwc->idx == INTEL_PMC_IDX_FIXED_BTS)) {
0a3aee0d 1966 if (!__this_cpu_read(cpu_hw_events.enabled))
f22f54f4
PZ
1967 return;
1968
1969 intel_pmu_enable_bts(hwc->config);
1970 return;
1971 }
1972
144d31e6
GN
1973 if (event->attr.exclude_host)
1974 cpuc->intel_ctrl_guest_mask |= (1ull << hwc->idx);
1975 if (event->attr.exclude_guest)
1976 cpuc->intel_ctrl_host_mask |= (1ull << hwc->idx);
1977
2b9e344d
PZ
1978 if (unlikely(event_is_checkpointed(event)))
1979 cpuc->intel_cp_status |= (1ull << hwc->idx);
1980
f22f54f4 1981 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
aff3d91a 1982 intel_pmu_enable_fixed(hwc);
f22f54f4
PZ
1983 return;
1984 }
1985
ab608344 1986 if (unlikely(event->attr.precise_ip))
ef21f683 1987 intel_pmu_pebs_enable(event);
ca037701 1988
31fa58af 1989 __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
f22f54f4
PZ
1990}
1991
68f7082f
PZ
1992static void intel_pmu_add_event(struct perf_event *event)
1993{
1994 if (event->attr.precise_ip)
1995 intel_pmu_pebs_add(event);
1996 if (needs_branch_stack(event))
1997 intel_pmu_lbr_add(event);
1998}
1999
f22f54f4
PZ
2000/*
2001 * Save and restart an expired event. Called by NMI contexts,
2002 * so it has to be careful about preempting normal event ops:
2003 */
de0428a7 2004int intel_pmu_save_and_restart(struct perf_event *event)
f22f54f4 2005{
cc2ad4ba 2006 x86_perf_event_update(event);
2dbf0116
AK
2007 /*
2008 * For a checkpointed counter always reset back to 0. This
2009 * avoids a situation where the counter overflows, aborts the
2010 * transaction and is then set back to shortly before the
2011 * overflow, and overflows and aborts again.
2012 */
2013 if (unlikely(event_is_checkpointed(event))) {
2014 /* No race with NMIs because the counter should not be armed */
2015 wrmsrl(event->hw.event_base, 0);
2016 local64_set(&event->hw.prev_count, 0);
2017 }
cc2ad4ba 2018 return x86_perf_event_set_period(event);
f22f54f4
PZ
2019}
2020
2021static void intel_pmu_reset(void)
2022{
0a3aee0d 2023 struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
f22f54f4
PZ
2024 unsigned long flags;
2025 int idx;
2026
948b1bb8 2027 if (!x86_pmu.num_counters)
f22f54f4
PZ
2028 return;
2029
2030 local_irq_save(flags);
2031
c767a54b 2032 pr_info("clearing PMU state on CPU#%d\n", smp_processor_id());
f22f54f4 2033
948b1bb8 2034 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
715c85b1
PA
2035 wrmsrl_safe(x86_pmu_config_addr(idx), 0ull);
2036 wrmsrl_safe(x86_pmu_event_addr(idx), 0ull);
f22f54f4 2037 }
948b1bb8 2038 for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++)
715c85b1 2039 wrmsrl_safe(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull);
948b1bb8 2040
f22f54f4
PZ
2041 if (ds)
2042 ds->bts_index = ds->bts_buffer_base;
2043
8882edf7
AK
2044 /* Ack all overflows and disable fixed counters */
2045 if (x86_pmu.version >= 2) {
2046 intel_pmu_ack_status(intel_pmu_get_status());
2047 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
2048 }
2049
2050 /* Reset LBRs and LBR freezing */
2051 if (x86_pmu.lbr_nr) {
2052 update_debugctlmsr(get_debugctlmsr() &
2053 ~(DEBUGCTLMSR_FREEZE_LBRS_ON_PMI|DEBUGCTLMSR_LBR));
2054 }
2055
f22f54f4
PZ
2056 local_irq_restore(flags);
2057}
2058
2059/*
2060 * This handler is triggered by the local APIC, so the APIC IRQ handling
2061 * rules apply:
2062 */
2063static int intel_pmu_handle_irq(struct pt_regs *regs)
2064{
2065 struct perf_sample_data data;
2066 struct cpu_hw_events *cpuc;
2067 int bit, loops;
2e556b5b 2068 u64 status;
b0b2072d 2069 int handled;
f22f54f4 2070
89cbc767 2071 cpuc = this_cpu_ptr(&cpu_hw_events);
f22f54f4 2072
2bce5dac 2073 /*
72db5596
AK
2074 * No known reason to not always do late ACK,
2075 * but just in case do it opt-in.
2bce5dac 2076 */
72db5596
AK
2077 if (!x86_pmu.late_ack)
2078 apic_write(APIC_LVTPC, APIC_DM_NMI);
1a78d937 2079 __intel_pmu_disable_all();
b0b2072d 2080 handled = intel_pmu_drain_bts_buffer();
8062382c 2081 handled += intel_bts_interrupt();
f22f54f4 2082 status = intel_pmu_get_status();
a3ef2229
MM
2083 if (!status)
2084 goto done;
f22f54f4
PZ
2085
2086 loops = 0;
2087again:
0f29e573 2088 intel_pmu_lbr_read();
2e556b5b 2089 intel_pmu_ack_status(status);
f22f54f4 2090 if (++loops > 100) {
ae0def05
DH
2091 static bool warned = false;
2092 if (!warned) {
2093 WARN(1, "perfevents: irq loop stuck!\n");
2094 perf_event_print_debug();
2095 warned = true;
2096 }
f22f54f4 2097 intel_pmu_reset();
3fb2b8dd 2098 goto done;
f22f54f4
PZ
2099 }
2100
2101 inc_irq_stat(apic_perf_irqs);
ca037701 2102
caff2bef 2103
b292d7a1 2104 /*
d8020bee
AK
2105 * Ignore a range of extra bits in status that do not indicate
2106 * overflow by themselves.
b292d7a1 2107 */
d8020bee
AK
2108 status &= ~(GLOBAL_STATUS_COND_CHG |
2109 GLOBAL_STATUS_ASIF |
2110 GLOBAL_STATUS_LBRS_FROZEN);
2111 if (!status)
2112 goto done;
b292d7a1 2113
ca037701
PZ
2114 /*
2115 * PEBS overflow sets bit 62 in the global status register
2116 */
de725dec
PZ
2117 if (__test_and_clear_bit(62, (unsigned long *)&status)) {
2118 handled++;
ca037701 2119 x86_pmu.drain_pebs(regs);
8077eca0
SE
2120 /*
2121 * There are cases where, even though, the PEBS ovfl bit is set
2122 * in GLOBAL_OVF_STATUS, the PEBS events may also have their
2123 * overflow bits set for their counters. We must clear them
2124 * here because they have been processed as exact samples in
2125 * the drain_pebs() routine. They must not be processed again
2126 * in the for_each_bit_set() loop for regular samples below.
2127 */
2128 status &= ~cpuc->pebs_enabled;
2129 status &= x86_pmu.intel_ctrl | GLOBAL_STATUS_TRACE_TOPAPMI;
de725dec 2130 }
ca037701 2131
52ca9ced
AS
2132 /*
2133 * Intel PT
2134 */
2135 if (__test_and_clear_bit(55, (unsigned long *)&status)) {
2136 handled++;
2137 intel_pt_interrupt();
2138 }
2139
2dbf0116 2140 /*
2b9e344d
PZ
2141 * Checkpointed counters can lead to 'spurious' PMIs because the
2142 * rollback caused by the PMI will have cleared the overflow status
2143 * bit. Therefore always force probe these counters.
2dbf0116 2144 */
2b9e344d 2145 status |= cpuc->intel_cp_status;
2dbf0116 2146
984b3f57 2147 for_each_set_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
f22f54f4
PZ
2148 struct perf_event *event = cpuc->events[bit];
2149
de725dec
PZ
2150 handled++;
2151
f22f54f4
PZ
2152 if (!test_bit(bit, cpuc->active_mask))
2153 continue;
2154
2155 if (!intel_pmu_save_and_restart(event))
2156 continue;
2157
fd0d000b 2158 perf_sample_data_init(&data, 0, event->hw.last_period);
f22f54f4 2159
60ce0fbd
SE
2160 if (has_branch_stack(event))
2161 data.br_stack = &cpuc->lbr_stack;
2162
a8b0ca17 2163 if (perf_event_overflow(event, &data, regs))
a4eaf7f1 2164 x86_pmu_stop(event, 0);
f22f54f4
PZ
2165 }
2166
f22f54f4
PZ
2167 /*
2168 * Repeat if there is more work to be done:
2169 */
2170 status = intel_pmu_get_status();
2171 if (status)
2172 goto again;
2173
3fb2b8dd 2174done:
c3d266c8
KL
2175 /* Only restore PMU state when it's active. See x86_pmu_disable(). */
2176 if (cpuc->enabled)
2177 __intel_pmu_enable_all(0, true);
2178
72db5596
AK
2179 /*
2180 * Only unmask the NMI after the overflow counters
2181 * have been reset. This avoids spurious NMIs on
2182 * Haswell CPUs.
2183 */
2184 if (x86_pmu.late_ack)
2185 apic_write(APIC_LVTPC, APIC_DM_NMI);
de725dec 2186 return handled;
f22f54f4
PZ
2187}
2188
f22f54f4 2189static struct event_constraint *
ca037701 2190intel_bts_constraints(struct perf_event *event)
f22f54f4 2191{
ca037701
PZ
2192 struct hw_perf_event *hwc = &event->hw;
2193 unsigned int hw_event, bts_event;
f22f54f4 2194
18a073a3
PZ
2195 if (event->attr.freq)
2196 return NULL;
2197
ca037701
PZ
2198 hw_event = hwc->config & INTEL_ARCH_EVENT_MASK;
2199 bts_event = x86_pmu.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS);
f22f54f4 2200
ca037701 2201 if (unlikely(hw_event == bts_event && hwc->sample_period == 1))
f22f54f4 2202 return &bts_constraint;
ca037701 2203
f22f54f4
PZ
2204 return NULL;
2205}
2206
ae3f011f 2207static int intel_alt_er(int idx, u64 config)
b79e8941 2208{
e01d8718
PZ
2209 int alt_idx = idx;
2210
9a5e3fb5 2211 if (!(x86_pmu.flags & PMU_FL_HAS_RSP_1))
5a425294 2212 return idx;
b79e8941 2213
5a425294 2214 if (idx == EXTRA_REG_RSP_0)
ae3f011f 2215 alt_idx = EXTRA_REG_RSP_1;
5a425294
PZ
2216
2217 if (idx == EXTRA_REG_RSP_1)
ae3f011f 2218 alt_idx = EXTRA_REG_RSP_0;
5a425294 2219
ae3f011f
KL
2220 if (config & ~x86_pmu.extra_regs[alt_idx].valid_mask)
2221 return idx;
2222
2223 return alt_idx;
5a425294
PZ
2224}
2225
2226static void intel_fixup_er(struct perf_event *event, int idx)
2227{
2228 event->hw.extra_reg.idx = idx;
2229
2230 if (idx == EXTRA_REG_RSP_0) {
b79e8941 2231 event->hw.config &= ~INTEL_ARCH_EVENT_MASK;
53ad0447 2232 event->hw.config |= x86_pmu.extra_regs[EXTRA_REG_RSP_0].event;
b79e8941 2233 event->hw.extra_reg.reg = MSR_OFFCORE_RSP_0;
5a425294
PZ
2234 } else if (idx == EXTRA_REG_RSP_1) {
2235 event->hw.config &= ~INTEL_ARCH_EVENT_MASK;
53ad0447 2236 event->hw.config |= x86_pmu.extra_regs[EXTRA_REG_RSP_1].event;
5a425294 2237 event->hw.extra_reg.reg = MSR_OFFCORE_RSP_1;
b79e8941 2238 }
b79e8941
PZ
2239}
2240
efc9f05d
SE
2241/*
2242 * manage allocation of shared extra msr for certain events
2243 *
2244 * sharing can be:
2245 * per-cpu: to be shared between the various events on a single PMU
2246 * per-core: per-cpu + shared by HT threads
2247 */
a7e3ed1e 2248static struct event_constraint *
efc9f05d 2249__intel_shared_reg_get_constraints(struct cpu_hw_events *cpuc,
b36817e8
SE
2250 struct perf_event *event,
2251 struct hw_perf_event_extra *reg)
a7e3ed1e 2252{
efc9f05d 2253 struct event_constraint *c = &emptyconstraint;
a7e3ed1e 2254 struct er_account *era;
cd8a38d3 2255 unsigned long flags;
5a425294 2256 int idx = reg->idx;
a7e3ed1e 2257
5a425294
PZ
2258 /*
2259 * reg->alloc can be set due to existing state, so for fake cpuc we
2260 * need to ignore this, otherwise we might fail to allocate proper fake
2261 * state for this extra reg constraint. Also see the comment below.
2262 */
2263 if (reg->alloc && !cpuc->is_fake)
b36817e8 2264 return NULL; /* call x86_get_event_constraint() */
a7e3ed1e 2265
b79e8941 2266again:
5a425294 2267 era = &cpuc->shared_regs->regs[idx];
cd8a38d3
SE
2268 /*
2269 * we use spin_lock_irqsave() to avoid lockdep issues when
2270 * passing a fake cpuc
2271 */
2272 raw_spin_lock_irqsave(&era->lock, flags);
efc9f05d
SE
2273
2274 if (!atomic_read(&era->ref) || era->config == reg->config) {
2275
5a425294
PZ
2276 /*
2277 * If its a fake cpuc -- as per validate_{group,event}() we
2278 * shouldn't touch event state and we can avoid doing so
2279 * since both will only call get_event_constraints() once
2280 * on each event, this avoids the need for reg->alloc.
2281 *
2282 * Not doing the ER fixup will only result in era->reg being
2283 * wrong, but since we won't actually try and program hardware
2284 * this isn't a problem either.
2285 */
2286 if (!cpuc->is_fake) {
2287 if (idx != reg->idx)
2288 intel_fixup_er(event, idx);
2289
2290 /*
2291 * x86_schedule_events() can call get_event_constraints()
2292 * multiple times on events in the case of incremental
2293 * scheduling(). reg->alloc ensures we only do the ER
2294 * allocation once.
2295 */
2296 reg->alloc = 1;
2297 }
2298
efc9f05d
SE
2299 /* lock in msr value */
2300 era->config = reg->config;
2301 era->reg = reg->reg;
2302
2303 /* one more user */
2304 atomic_inc(&era->ref);
2305
a7e3ed1e 2306 /*
b36817e8
SE
2307 * need to call x86_get_event_constraint()
2308 * to check if associated event has constraints
a7e3ed1e 2309 */
b36817e8 2310 c = NULL;
5a425294 2311 } else {
ae3f011f 2312 idx = intel_alt_er(idx, reg->config);
5a425294
PZ
2313 if (idx != reg->idx) {
2314 raw_spin_unlock_irqrestore(&era->lock, flags);
2315 goto again;
2316 }
a7e3ed1e 2317 }
cd8a38d3 2318 raw_spin_unlock_irqrestore(&era->lock, flags);
a7e3ed1e 2319
efc9f05d
SE
2320 return c;
2321}
2322
2323static void
2324__intel_shared_reg_put_constraints(struct cpu_hw_events *cpuc,
2325 struct hw_perf_event_extra *reg)
2326{
2327 struct er_account *era;
2328
2329 /*
5a425294
PZ
2330 * Only put constraint if extra reg was actually allocated. Also takes
2331 * care of event which do not use an extra shared reg.
2332 *
2333 * Also, if this is a fake cpuc we shouldn't touch any event state
2334 * (reg->alloc) and we don't care about leaving inconsistent cpuc state
2335 * either since it'll be thrown out.
efc9f05d 2336 */
5a425294 2337 if (!reg->alloc || cpuc->is_fake)
efc9f05d
SE
2338 return;
2339
2340 era = &cpuc->shared_regs->regs[reg->idx];
2341
2342 /* one fewer user */
2343 atomic_dec(&era->ref);
2344
2345 /* allocate again next time */
2346 reg->alloc = 0;
2347}
2348
2349static struct event_constraint *
2350intel_shared_regs_constraints(struct cpu_hw_events *cpuc,
2351 struct perf_event *event)
2352{
b36817e8
SE
2353 struct event_constraint *c = NULL, *d;
2354 struct hw_perf_event_extra *xreg, *breg;
2355
2356 xreg = &event->hw.extra_reg;
2357 if (xreg->idx != EXTRA_REG_NONE) {
2358 c = __intel_shared_reg_get_constraints(cpuc, event, xreg);
2359 if (c == &emptyconstraint)
2360 return c;
2361 }
2362 breg = &event->hw.branch_reg;
2363 if (breg->idx != EXTRA_REG_NONE) {
2364 d = __intel_shared_reg_get_constraints(cpuc, event, breg);
2365 if (d == &emptyconstraint) {
2366 __intel_shared_reg_put_constraints(cpuc, xreg);
2367 c = d;
2368 }
2369 }
efc9f05d 2370 return c;
a7e3ed1e
AK
2371}
2372
de0428a7 2373struct event_constraint *
79cba822
SE
2374x86_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
2375 struct perf_event *event)
de0428a7
KW
2376{
2377 struct event_constraint *c;
2378
2379 if (x86_pmu.event_constraints) {
2380 for_each_event_constraint(c, x86_pmu.event_constraints) {
9fac2cf3 2381 if ((event->hw.config & c->cmask) == c->code) {
9fac2cf3 2382 event->hw.flags |= c->flags;
de0428a7 2383 return c;
9fac2cf3 2384 }
de0428a7
KW
2385 }
2386 }
2387
2388 return &unconstrained;
2389}
2390
f22f54f4 2391static struct event_constraint *
e979121b 2392__intel_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
79cba822 2393 struct perf_event *event)
f22f54f4
PZ
2394{
2395 struct event_constraint *c;
2396
ca037701
PZ
2397 c = intel_bts_constraints(event);
2398 if (c)
2399 return c;
2400
687805e4 2401 c = intel_shared_regs_constraints(cpuc, event);
f22f54f4
PZ
2402 if (c)
2403 return c;
2404
687805e4 2405 c = intel_pebs_constraints(event);
a7e3ed1e
AK
2406 if (c)
2407 return c;
2408
79cba822 2409 return x86_get_event_constraints(cpuc, idx, event);
f22f54f4
PZ
2410}
2411
e979121b
MD
2412static void
2413intel_start_scheduling(struct cpu_hw_events *cpuc)
2414{
2415 struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
1c565833 2416 struct intel_excl_states *xl;
e979121b 2417 int tid = cpuc->excl_thread_id;
e979121b
MD
2418
2419 /*
2420 * nothing needed if in group validation mode
2421 */
b37609c3 2422 if (cpuc->is_fake || !is_ht_workaround_enabled())
e979121b 2423 return;
b37609c3 2424
e979121b
MD
2425 /*
2426 * no exclusion needed
2427 */
17186ccd 2428 if (WARN_ON_ONCE(!excl_cntrs))
e979121b
MD
2429 return;
2430
e979121b
MD
2431 xl = &excl_cntrs->states[tid];
2432
2433 xl->sched_started = true;
e979121b
MD
2434 /*
2435 * lock shared state until we are done scheduling
2436 * in stop_event_scheduling()
2437 * makes scheduling appear as a transaction
2438 */
e979121b 2439 raw_spin_lock(&excl_cntrs->lock);
e979121b
MD
2440}
2441
0c41e756
PZ
2442static void intel_commit_scheduling(struct cpu_hw_events *cpuc, int idx, int cntr)
2443{
2444 struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
2445 struct event_constraint *c = cpuc->event_constraint[idx];
2446 struct intel_excl_states *xl;
2447 int tid = cpuc->excl_thread_id;
2448
2449 if (cpuc->is_fake || !is_ht_workaround_enabled())
2450 return;
2451
2452 if (WARN_ON_ONCE(!excl_cntrs))
2453 return;
2454
2455 if (!(c->flags & PERF_X86_EVENT_DYNAMIC))
2456 return;
2457
2458 xl = &excl_cntrs->states[tid];
2459
2460 lockdep_assert_held(&excl_cntrs->lock);
2461
1fe684e3 2462 if (c->flags & PERF_X86_EVENT_EXCL)
43ef205b 2463 xl->state[cntr] = INTEL_EXCL_EXCLUSIVE;
1fe684e3 2464 else
43ef205b 2465 xl->state[cntr] = INTEL_EXCL_SHARED;
0c41e756
PZ
2466}
2467
e979121b
MD
2468static void
2469intel_stop_scheduling(struct cpu_hw_events *cpuc)
2470{
2471 struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
1c565833 2472 struct intel_excl_states *xl;
e979121b 2473 int tid = cpuc->excl_thread_id;
e979121b
MD
2474
2475 /*
2476 * nothing needed if in group validation mode
2477 */
b37609c3 2478 if (cpuc->is_fake || !is_ht_workaround_enabled())
e979121b
MD
2479 return;
2480 /*
2481 * no exclusion needed
2482 */
17186ccd 2483 if (WARN_ON_ONCE(!excl_cntrs))
e979121b
MD
2484 return;
2485
e979121b
MD
2486 xl = &excl_cntrs->states[tid];
2487
e979121b
MD
2488 xl->sched_started = false;
2489 /*
2490 * release shared state lock (acquired in intel_start_scheduling())
2491 */
2492 raw_spin_unlock(&excl_cntrs->lock);
2493}
2494
2495static struct event_constraint *
2496intel_get_excl_constraints(struct cpu_hw_events *cpuc, struct perf_event *event,
2497 int idx, struct event_constraint *c)
2498{
e979121b 2499 struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
1c565833 2500 struct intel_excl_states *xlo;
e979121b 2501 int tid = cpuc->excl_thread_id;
1c565833 2502 int is_excl, i;
e979121b
MD
2503
2504 /*
2505 * validating a group does not require
2506 * enforcing cross-thread exclusion
2507 */
b37609c3
SE
2508 if (cpuc->is_fake || !is_ht_workaround_enabled())
2509 return c;
2510
2511 /*
2512 * no exclusion needed
2513 */
17186ccd 2514 if (WARN_ON_ONCE(!excl_cntrs))
e979121b 2515 return c;
e979121b 2516
e979121b
MD
2517 /*
2518 * because we modify the constraint, we need
2519 * to make a copy. Static constraints come
2520 * from static const tables.
2521 *
2522 * only needed when constraint has not yet
2523 * been cloned (marked dynamic)
2524 */
2525 if (!(c->flags & PERF_X86_EVENT_DYNAMIC)) {
aaf932e8 2526 struct event_constraint *cx;
e979121b 2527
e979121b
MD
2528 /*
2529 * grab pre-allocated constraint entry
2530 */
2531 cx = &cpuc->constraint_list[idx];
2532
2533 /*
2534 * initialize dynamic constraint
2535 * with static constraint
2536 */
aaf932e8 2537 *cx = *c;
e979121b
MD
2538
2539 /*
2540 * mark constraint as dynamic, so we
2541 * can free it later on
2542 */
2543 cx->flags |= PERF_X86_EVENT_DYNAMIC;
aaf932e8 2544 c = cx;
e979121b
MD
2545 }
2546
2547 /*
2548 * From here on, the constraint is dynamic.
2549 * Either it was just allocated above, or it
2550 * was allocated during a earlier invocation
2551 * of this function
2552 */
2553
1c565833
PZ
2554 /*
2555 * state of sibling HT
2556 */
2557 xlo = &excl_cntrs->states[tid ^ 1];
2558
2559 /*
2560 * event requires exclusive counter access
2561 * across HT threads
2562 */
2563 is_excl = c->flags & PERF_X86_EVENT_EXCL;
2564 if (is_excl && !(event->hw.flags & PERF_X86_EVENT_EXCL_ACCT)) {
2565 event->hw.flags |= PERF_X86_EVENT_EXCL_ACCT;
2566 if (!cpuc->n_excl++)
2567 WRITE_ONCE(excl_cntrs->has_exclusive[tid], 1);
2568 }
2569
e979121b
MD
2570 /*
2571 * Modify static constraint with current dynamic
2572 * state of thread
2573 *
2574 * EXCLUSIVE: sibling counter measuring exclusive event
2575 * SHARED : sibling counter measuring non-exclusive event
2576 * UNUSED : sibling counter unused
2577 */
aaf932e8 2578 for_each_set_bit(i, c->idxmsk, X86_PMC_IDX_MAX) {
e979121b
MD
2579 /*
2580 * exclusive event in sibling counter
2581 * our corresponding counter cannot be used
2582 * regardless of our event
2583 */
1c565833 2584 if (xlo->state[i] == INTEL_EXCL_EXCLUSIVE)
aaf932e8 2585 __clear_bit(i, c->idxmsk);
e979121b
MD
2586 /*
2587 * if measuring an exclusive event, sibling
2588 * measuring non-exclusive, then counter cannot
2589 * be used
2590 */
1c565833 2591 if (is_excl && xlo->state[i] == INTEL_EXCL_SHARED)
aaf932e8 2592 __clear_bit(i, c->idxmsk);
e979121b
MD
2593 }
2594
2595 /*
2596 * recompute actual bit weight for scheduling algorithm
2597 */
aaf932e8 2598 c->weight = hweight64(c->idxmsk64);
e979121b
MD
2599
2600 /*
2601 * if we return an empty mask, then switch
2602 * back to static empty constraint to avoid
2603 * the cost of freeing later on
2604 */
aaf932e8
PZ
2605 if (c->weight == 0)
2606 c = &emptyconstraint;
e979121b 2607
aaf932e8 2608 return c;
e979121b
MD
2609}
2610
2611static struct event_constraint *
2612intel_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
2613 struct perf_event *event)
2614{
ebfb4988 2615 struct event_constraint *c1 = NULL;
a90738c2 2616 struct event_constraint *c2;
e979121b 2617
ebfb4988
PZ
2618 if (idx >= 0) /* fake does < 0 */
2619 c1 = cpuc->event_constraint[idx];
2620
e979121b
MD
2621 /*
2622 * first time only
2623 * - static constraint: no change across incremental scheduling calls
2624 * - dynamic constraint: handled by intel_get_excl_constraints()
2625 */
a90738c2
SE
2626 c2 = __intel_get_event_constraints(cpuc, idx, event);
2627 if (c1 && (c1->flags & PERF_X86_EVENT_DYNAMIC)) {
2628 bitmap_copy(c1->idxmsk, c2->idxmsk, X86_PMC_IDX_MAX);
2629 c1->weight = c2->weight;
2630 c2 = c1;
2631 }
e979121b
MD
2632
2633 if (cpuc->excl_cntrs)
a90738c2 2634 return intel_get_excl_constraints(cpuc, event, idx, c2);
e979121b 2635
a90738c2 2636 return c2;
e979121b
MD
2637}
2638
2639static void intel_put_excl_constraints(struct cpu_hw_events *cpuc,
2640 struct perf_event *event)
2641{
2642 struct hw_perf_event *hwc = &event->hw;
2643 struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
e979121b 2644 int tid = cpuc->excl_thread_id;
1c565833 2645 struct intel_excl_states *xl;
e979121b
MD
2646
2647 /*
2648 * nothing needed if in group validation mode
2649 */
2650 if (cpuc->is_fake)
2651 return;
2652
17186ccd 2653 if (WARN_ON_ONCE(!excl_cntrs))
e979121b
MD
2654 return;
2655
cc1790cf
PZ
2656 if (hwc->flags & PERF_X86_EVENT_EXCL_ACCT) {
2657 hwc->flags &= ~PERF_X86_EVENT_EXCL_ACCT;
2658 if (!--cpuc->n_excl)
2659 WRITE_ONCE(excl_cntrs->has_exclusive[tid], 0);
2660 }
e979121b
MD
2661
2662 /*
ba040653
PZ
2663 * If event was actually assigned, then mark the counter state as
2664 * unused now.
e979121b 2665 */
ba040653
PZ
2666 if (hwc->idx >= 0) {
2667 xl = &excl_cntrs->states[tid];
2668
2669 /*
2670 * put_constraint may be called from x86_schedule_events()
2671 * which already has the lock held so here make locking
2672 * conditional.
2673 */
2674 if (!xl->sched_started)
2675 raw_spin_lock(&excl_cntrs->lock);
e979121b 2676
1c565833 2677 xl->state[hwc->idx] = INTEL_EXCL_UNUSED;
e979121b 2678
ba040653
PZ
2679 if (!xl->sched_started)
2680 raw_spin_unlock(&excl_cntrs->lock);
2681 }
e979121b
MD
2682}
2683
efc9f05d
SE
2684static void
2685intel_put_shared_regs_event_constraints(struct cpu_hw_events *cpuc,
a7e3ed1e
AK
2686 struct perf_event *event)
2687{
efc9f05d 2688 struct hw_perf_event_extra *reg;
a7e3ed1e 2689
efc9f05d
SE
2690 reg = &event->hw.extra_reg;
2691 if (reg->idx != EXTRA_REG_NONE)
2692 __intel_shared_reg_put_constraints(cpuc, reg);
b36817e8
SE
2693
2694 reg = &event->hw.branch_reg;
2695 if (reg->idx != EXTRA_REG_NONE)
2696 __intel_shared_reg_put_constraints(cpuc, reg);
efc9f05d 2697}
a7e3ed1e 2698
efc9f05d
SE
2699static void intel_put_event_constraints(struct cpu_hw_events *cpuc,
2700 struct perf_event *event)
2701{
2702 intel_put_shared_regs_event_constraints(cpuc, event);
e979121b
MD
2703
2704 /*
2705 * is PMU has exclusive counter restrictions, then
2706 * all events are subject to and must call the
2707 * put_excl_constraints() routine
2708 */
b371b594 2709 if (cpuc->excl_cntrs)
e979121b 2710 intel_put_excl_constraints(cpuc, event);
e979121b
MD
2711}
2712
0780c927 2713static void intel_pebs_aliases_core2(struct perf_event *event)
b4cdc5c2 2714{
0780c927 2715 if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
7639dae0
PZ
2716 /*
2717 * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P
2718 * (0x003c) so that we can use it with PEBS.
2719 *
2720 * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't
2721 * PEBS capable. However we can use INST_RETIRED.ANY_P
2722 * (0x00c0), which is a PEBS capable event, to get the same
2723 * count.
2724 *
2725 * INST_RETIRED.ANY_P counts the number of cycles that retires
2726 * CNTMASK instructions. By setting CNTMASK to a value (16)
2727 * larger than the maximum number of instructions that can be
2728 * retired per cycle (4) and then inverting the condition, we
2729 * count all cycles that retire 16 or less instructions, which
2730 * is every cycle.
2731 *
2732 * Thereby we gain a PEBS capable cycle counter.
2733 */
f9b4eeb8
PZ
2734 u64 alt_config = X86_CONFIG(.event=0xc0, .inv=1, .cmask=16);
2735
0780c927
PZ
2736 alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
2737 event->hw.config = alt_config;
2738 }
2739}
2740
2741static void intel_pebs_aliases_snb(struct perf_event *event)
2742{
2743 if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
2744 /*
2745 * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P
2746 * (0x003c) so that we can use it with PEBS.
2747 *
2748 * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't
2749 * PEBS capable. However we can use UOPS_RETIRED.ALL
2750 * (0x01c2), which is a PEBS capable event, to get the same
2751 * count.
2752 *
2753 * UOPS_RETIRED.ALL counts the number of cycles that retires
2754 * CNTMASK micro-ops. By setting CNTMASK to a value (16)
2755 * larger than the maximum number of micro-ops that can be
2756 * retired per cycle (4) and then inverting the condition, we
2757 * count all cycles that retire 16 or less micro-ops, which
2758 * is every cycle.
2759 *
2760 * Thereby we gain a PEBS capable cycle counter.
2761 */
2762 u64 alt_config = X86_CONFIG(.event=0xc2, .umask=0x01, .inv=1, .cmask=16);
7639dae0
PZ
2763
2764 alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
2765 event->hw.config = alt_config;
2766 }
0780c927
PZ
2767}
2768
72469764
AK
2769static void intel_pebs_aliases_precdist(struct perf_event *event)
2770{
2771 if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
2772 /*
2773 * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P
2774 * (0x003c) so that we can use it with PEBS.
2775 *
2776 * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't
2777 * PEBS capable. However we can use INST_RETIRED.PREC_DIST
2778 * (0x01c0), which is a PEBS capable event, to get the same
2779 * count.
2780 *
2781 * The PREC_DIST event has special support to minimize sample
2782 * shadowing effects. One drawback is that it can be
2783 * only programmed on counter 1, but that seems like an
2784 * acceptable trade off.
2785 */
2786 u64 alt_config = X86_CONFIG(.event=0xc0, .umask=0x01, .inv=1, .cmask=16);
2787
2788 alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
2789 event->hw.config = alt_config;
2790 }
2791}
2792
2793static void intel_pebs_aliases_ivb(struct perf_event *event)
2794{
2795 if (event->attr.precise_ip < 3)
2796 return intel_pebs_aliases_snb(event);
2797 return intel_pebs_aliases_precdist(event);
2798}
2799
2800static void intel_pebs_aliases_skl(struct perf_event *event)
2801{
2802 if (event->attr.precise_ip < 3)
2803 return intel_pebs_aliases_core2(event);
2804 return intel_pebs_aliases_precdist(event);
2805}
2806
a7b58d21
AK
2807static unsigned long intel_pmu_free_running_flags(struct perf_event *event)
2808{
2809 unsigned long flags = x86_pmu.free_running_flags;
2810
2811 if (event->attr.use_clockid)
2812 flags &= ~PERF_SAMPLE_TIME;
2813 return flags;
2814}
2815
0780c927
PZ
2816static int intel_pmu_hw_config(struct perf_event *event)
2817{
2818 int ret = x86_pmu_hw_config(event);
2819
2820 if (ret)
2821 return ret;
2822
851559e3 2823 if (event->attr.precise_ip) {
3569c0d7 2824 if (!event->attr.freq) {
851559e3 2825 event->hw.flags |= PERF_X86_EVENT_AUTO_RELOAD;
a7b58d21
AK
2826 if (!(event->attr.sample_type &
2827 ~intel_pmu_free_running_flags(event)))
3569c0d7
YZ
2828 event->hw.flags |= PERF_X86_EVENT_FREERUNNING;
2829 }
851559e3
YZ
2830 if (x86_pmu.pebs_aliases)
2831 x86_pmu.pebs_aliases(event);
2832 }
7639dae0 2833
a46a2300 2834 if (needs_branch_stack(event)) {
60ce0fbd
SE
2835 ret = intel_pmu_setup_lbr_filter(event);
2836 if (ret)
2837 return ret;
48070342
AS
2838
2839 /*
2840 * BTS is set up earlier in this path, so don't account twice
2841 */
2842 if (!intel_pmu_has_bts(event)) {
2843 /* disallow lbr if conflicting events are present */
2844 if (x86_add_exclusive(x86_lbr_exclusive_lbr))
2845 return -EBUSY;
2846
2847 event->destroy = hw_perf_lbr_event_destroy;
2848 }
60ce0fbd
SE
2849 }
2850
b4cdc5c2
PZ
2851 if (event->attr.type != PERF_TYPE_RAW)
2852 return 0;
2853
2854 if (!(event->attr.config & ARCH_PERFMON_EVENTSEL_ANY))
2855 return 0;
2856
2857 if (x86_pmu.version < 3)
2858 return -EINVAL;
2859
2860 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2861 return -EACCES;
2862
2863 event->hw.config |= ARCH_PERFMON_EVENTSEL_ANY;
2864
2865 return 0;
2866}
2867
144d31e6
GN
2868struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr)
2869{
2870 if (x86_pmu.guest_get_msrs)
2871 return x86_pmu.guest_get_msrs(nr);
2872 *nr = 0;
2873 return NULL;
2874}
2875EXPORT_SYMBOL_GPL(perf_guest_get_msrs);
2876
2877static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr)
2878{
89cbc767 2879 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
144d31e6
GN
2880 struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;
2881
2882 arr[0].msr = MSR_CORE_PERF_GLOBAL_CTRL;
2883 arr[0].host = x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_guest_mask;
2884 arr[0].guest = x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_host_mask;
26a4f3c0
GN
2885 /*
2886 * If PMU counter has PEBS enabled it is not enough to disable counter
2887 * on a guest entry since PEBS memory write can overshoot guest entry
2888 * and corrupt guest memory. Disabling PEBS solves the problem.
2889 */
2890 arr[1].msr = MSR_IA32_PEBS_ENABLE;
2891 arr[1].host = cpuc->pebs_enabled;
2892 arr[1].guest = 0;
144d31e6 2893
26a4f3c0 2894 *nr = 2;
144d31e6
GN
2895 return arr;
2896}
2897
2898static struct perf_guest_switch_msr *core_guest_get_msrs(int *nr)
2899{
89cbc767 2900 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
144d31e6
GN
2901 struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;
2902 int idx;
2903
2904 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
2905 struct perf_event *event = cpuc->events[idx];
2906
2907 arr[idx].msr = x86_pmu_config_addr(idx);
2908 arr[idx].host = arr[idx].guest = 0;
2909
2910 if (!test_bit(idx, cpuc->active_mask))
2911 continue;
2912
2913 arr[idx].host = arr[idx].guest =
2914 event->hw.config | ARCH_PERFMON_EVENTSEL_ENABLE;
2915
2916 if (event->attr.exclude_host)
2917 arr[idx].host &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
2918 else if (event->attr.exclude_guest)
2919 arr[idx].guest &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
2920 }
2921
2922 *nr = x86_pmu.num_counters;
2923 return arr;
2924}
2925
2926static void core_pmu_enable_event(struct perf_event *event)
2927{
2928 if (!event->attr.exclude_host)
2929 x86_pmu_enable_event(event);
2930}
2931
2932static void core_pmu_enable_all(int added)
2933{
89cbc767 2934 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
144d31e6
GN
2935 int idx;
2936
2937 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
2938 struct hw_perf_event *hwc = &cpuc->events[idx]->hw;
2939
2940 if (!test_bit(idx, cpuc->active_mask) ||
2941 cpuc->events[idx]->attr.exclude_host)
2942 continue;
2943
2944 __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
2945 }
2946}
2947
3a632cb2
AK
2948static int hsw_hw_config(struct perf_event *event)
2949{
2950 int ret = intel_pmu_hw_config(event);
2951
2952 if (ret)
2953 return ret;
2954 if (!boot_cpu_has(X86_FEATURE_RTM) && !boot_cpu_has(X86_FEATURE_HLE))
2955 return 0;
2956 event->hw.config |= event->attr.config & (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED);
2957
2958 /*
2959 * IN_TX/IN_TX-CP filters are not supported by the Haswell PMU with
2960 * PEBS or in ANY thread mode. Since the results are non-sensical forbid
2961 * this combination.
2962 */
2963 if ((event->hw.config & (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED)) &&
2964 ((event->hw.config & ARCH_PERFMON_EVENTSEL_ANY) ||
2965 event->attr.precise_ip > 0))
2966 return -EOPNOTSUPP;
2967
2dbf0116
AK
2968 if (event_is_checkpointed(event)) {
2969 /*
2970 * Sampling of checkpointed events can cause situations where
2971 * the CPU constantly aborts because of a overflow, which is
2972 * then checkpointed back and ignored. Forbid checkpointing
2973 * for sampling.
2974 *
2975 * But still allow a long sampling period, so that perf stat
2976 * from KVM works.
2977 */
2978 if (event->attr.sample_period > 0 &&
2979 event->attr.sample_period < 0x7fffffff)
2980 return -EOPNOTSUPP;
2981 }
3a632cb2
AK
2982 return 0;
2983}
2984
2985static struct event_constraint counter2_constraint =
2986 EVENT_CONSTRAINT(0, 0x4, 0);
2987
2988static struct event_constraint *
79cba822
SE
2989hsw_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
2990 struct perf_event *event)
3a632cb2 2991{
79cba822
SE
2992 struct event_constraint *c;
2993
2994 c = intel_get_event_constraints(cpuc, idx, event);
3a632cb2
AK
2995
2996 /* Handle special quirk on in_tx_checkpointed only in counter 2 */
2997 if (event->hw.config & HSW_IN_TX_CHECKPOINTED) {
2998 if (c->idxmsk64 & (1U << 2))
2999 return &counter2_constraint;
3000 return &emptyconstraint;
3001 }
3002
3003 return c;
3004}
3005
294fe0f5
AK
3006/*
3007 * Broadwell:
3008 *
3009 * The INST_RETIRED.ALL period always needs to have lowest 6 bits cleared
3010 * (BDM55) and it must not use a period smaller than 100 (BDM11). We combine
3011 * the two to enforce a minimum period of 128 (the smallest value that has bits
3012 * 0-5 cleared and >= 100).
3013 *
3014 * Because of how the code in x86_perf_event_set_period() works, the truncation
3015 * of the lower 6 bits is 'harmless' as we'll occasionally add a longer period
3016 * to make up for the 'lost' events due to carrying the 'error' in period_left.
3017 *
3018 * Therefore the effective (average) period matches the requested period,
3019 * despite coarser hardware granularity.
3020 */
3021static unsigned bdw_limit_period(struct perf_event *event, unsigned left)
3022{
3023 if ((event->hw.config & INTEL_ARCH_EVENT_MASK) ==
3024 X86_CONFIG(.event=0xc0, .umask=0x01)) {
3025 if (left < 128)
3026 left = 128;
3027 left &= ~0x3fu;
3028 }
3029 return left;
3030}
3031
641cc938
JO
3032PMU_FORMAT_ATTR(event, "config:0-7" );
3033PMU_FORMAT_ATTR(umask, "config:8-15" );
3034PMU_FORMAT_ATTR(edge, "config:18" );
3035PMU_FORMAT_ATTR(pc, "config:19" );
3036PMU_FORMAT_ATTR(any, "config:21" ); /* v3 + */
3037PMU_FORMAT_ATTR(inv, "config:23" );
3038PMU_FORMAT_ATTR(cmask, "config:24-31" );
3a632cb2
AK
3039PMU_FORMAT_ATTR(in_tx, "config:32");
3040PMU_FORMAT_ATTR(in_tx_cp, "config:33");
641cc938
JO
3041
3042static struct attribute *intel_arch_formats_attr[] = {
3043 &format_attr_event.attr,
3044 &format_attr_umask.attr,
3045 &format_attr_edge.attr,
3046 &format_attr_pc.attr,
3047 &format_attr_inv.attr,
3048 &format_attr_cmask.attr,
3049 NULL,
3050};
3051
0bf79d44
JO
3052ssize_t intel_event_sysfs_show(char *page, u64 config)
3053{
3054 u64 event = (config & ARCH_PERFMON_EVENTSEL_EVENT);
3055
3056 return x86_event_sysfs_show(page, config, event);
3057}
3058
de0428a7 3059struct intel_shared_regs *allocate_shared_regs(int cpu)
efc9f05d
SE
3060{
3061 struct intel_shared_regs *regs;
3062 int i;
3063
3064 regs = kzalloc_node(sizeof(struct intel_shared_regs),
3065 GFP_KERNEL, cpu_to_node(cpu));
3066 if (regs) {
3067 /*
3068 * initialize the locks to keep lockdep happy
3069 */
3070 for (i = 0; i < EXTRA_REG_MAX; i++)
3071 raw_spin_lock_init(&regs->regs[i].lock);
3072
3073 regs->core_id = -1;
3074 }
3075 return regs;
3076}
3077
6f6539ca
MD
3078static struct intel_excl_cntrs *allocate_excl_cntrs(int cpu)
3079{
3080 struct intel_excl_cntrs *c;
6f6539ca
MD
3081
3082 c = kzalloc_node(sizeof(struct intel_excl_cntrs),
3083 GFP_KERNEL, cpu_to_node(cpu));
3084 if (c) {
3085 raw_spin_lock_init(&c->lock);
6f6539ca
MD
3086 c->core_id = -1;
3087 }
3088 return c;
3089}
3090
a7e3ed1e
AK
3091static int intel_pmu_cpu_prepare(int cpu)
3092{
3093 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
3094
6f6539ca
MD
3095 if (x86_pmu.extra_regs || x86_pmu.lbr_sel_map) {
3096 cpuc->shared_regs = allocate_shared_regs(cpu);
3097 if (!cpuc->shared_regs)
dbc72b7a 3098 goto err;
6f6539ca 3099 }
69092624 3100
6f6539ca
MD
3101 if (x86_pmu.flags & PMU_FL_EXCL_CNTRS) {
3102 size_t sz = X86_PMC_IDX_MAX * sizeof(struct event_constraint);
3103
3104 cpuc->constraint_list = kzalloc(sz, GFP_KERNEL);
3105 if (!cpuc->constraint_list)
dbc72b7a 3106 goto err_shared_regs;
6f6539ca
MD
3107
3108 cpuc->excl_cntrs = allocate_excl_cntrs(cpu);
dbc72b7a
PZ
3109 if (!cpuc->excl_cntrs)
3110 goto err_constraint_list;
3111
6f6539ca
MD
3112 cpuc->excl_thread_id = 0;
3113 }
a7e3ed1e 3114
95ca792c 3115 return 0;
dbc72b7a
PZ
3116
3117err_constraint_list:
3118 kfree(cpuc->constraint_list);
3119 cpuc->constraint_list = NULL;
3120
3121err_shared_regs:
3122 kfree(cpuc->shared_regs);
3123 cpuc->shared_regs = NULL;
3124
3125err:
95ca792c 3126 return -ENOMEM;
a7e3ed1e
AK
3127}
3128
74846d35
PZ
3129static void intel_pmu_cpu_starting(int cpu)
3130{
a7e3ed1e
AK
3131 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
3132 int core_id = topology_core_id(cpu);
3133 int i;
3134
69092624
LM
3135 init_debug_store_on_cpu(cpu);
3136 /*
3137 * Deal with CPUs that don't clear their LBRs on power-up.
3138 */
3139 intel_pmu_lbr_reset();
3140
b36817e8
SE
3141 cpuc->lbr_sel = NULL;
3142
3143 if (!cpuc->shared_regs)
69092624
LM
3144 return;
3145
9a5e3fb5 3146 if (!(x86_pmu.flags & PMU_FL_NO_HT_SHARING)) {
06931e62 3147 for_each_cpu(i, topology_sibling_cpumask(cpu)) {
b36817e8 3148 struct intel_shared_regs *pc;
a7e3ed1e 3149
b36817e8
SE
3150 pc = per_cpu(cpu_hw_events, i).shared_regs;
3151 if (pc && pc->core_id == core_id) {
8f04b853 3152 cpuc->kfree_on_online[0] = cpuc->shared_regs;
b36817e8
SE
3153 cpuc->shared_regs = pc;
3154 break;
3155 }
a7e3ed1e 3156 }
b36817e8
SE
3157 cpuc->shared_regs->core_id = core_id;
3158 cpuc->shared_regs->refcnt++;
a7e3ed1e
AK
3159 }
3160
b36817e8
SE
3161 if (x86_pmu.lbr_sel_map)
3162 cpuc->lbr_sel = &cpuc->shared_regs->regs[EXTRA_REG_LBR];
6f6539ca
MD
3163
3164 if (x86_pmu.flags & PMU_FL_EXCL_CNTRS) {
06931e62 3165 for_each_cpu(i, topology_sibling_cpumask(cpu)) {
6f6539ca
MD
3166 struct intel_excl_cntrs *c;
3167
3168 c = per_cpu(cpu_hw_events, i).excl_cntrs;
3169 if (c && c->core_id == core_id) {
3170 cpuc->kfree_on_online[1] = cpuc->excl_cntrs;
3171 cpuc->excl_cntrs = c;
3172 cpuc->excl_thread_id = 1;
3173 break;
3174 }
3175 }
3176 cpuc->excl_cntrs->core_id = core_id;
3177 cpuc->excl_cntrs->refcnt++;
3178 }
74846d35
PZ
3179}
3180
b37609c3 3181static void free_excl_cntrs(int cpu)
74846d35 3182{
a7e3ed1e 3183 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
6f6539ca 3184 struct intel_excl_cntrs *c;
a7e3ed1e 3185
6f6539ca
MD
3186 c = cpuc->excl_cntrs;
3187 if (c) {
3188 if (c->core_id == -1 || --c->refcnt == 0)
3189 kfree(c);
3190 cpuc->excl_cntrs = NULL;
3191 kfree(cpuc->constraint_list);
3192 cpuc->constraint_list = NULL;
3193 }
b37609c3 3194}
a7e3ed1e 3195
b37609c3
SE
3196static void intel_pmu_cpu_dying(int cpu)
3197{
3198 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
3199 struct intel_shared_regs *pc;
3200
3201 pc = cpuc->shared_regs;
3202 if (pc) {
3203 if (pc->core_id == -1 || --pc->refcnt == 0)
3204 kfree(pc);
3205 cpuc->shared_regs = NULL;
e979121b
MD
3206 }
3207
b37609c3
SE
3208 free_excl_cntrs(cpu);
3209
74846d35
PZ
3210 fini_debug_store_on_cpu(cpu);
3211}
3212
9c964efa
YZ
3213static void intel_pmu_sched_task(struct perf_event_context *ctx,
3214 bool sched_in)
3215{
3216 if (x86_pmu.pebs_active)
3217 intel_pmu_pebs_sched_task(ctx, sched_in);
3218 if (x86_pmu.lbr_nr)
3219 intel_pmu_lbr_sched_task(ctx, sched_in);
3220}
3221
641cc938
JO
3222PMU_FORMAT_ATTR(offcore_rsp, "config1:0-63");
3223
a63fcab4
SE
3224PMU_FORMAT_ATTR(ldlat, "config1:0-15");
3225
d0dc8494
AK
3226PMU_FORMAT_ATTR(frontend, "config1:0-23");
3227
641cc938
JO
3228static struct attribute *intel_arch3_formats_attr[] = {
3229 &format_attr_event.attr,
3230 &format_attr_umask.attr,
3231 &format_attr_edge.attr,
3232 &format_attr_pc.attr,
3233 &format_attr_any.attr,
3234 &format_attr_inv.attr,
3235 &format_attr_cmask.attr,
3a632cb2
AK
3236 &format_attr_in_tx.attr,
3237 &format_attr_in_tx_cp.attr,
641cc938
JO
3238
3239 &format_attr_offcore_rsp.attr, /* XXX do NHM/WSM + SNB breakout */
a63fcab4 3240 &format_attr_ldlat.attr, /* PEBS load latency */
641cc938
JO
3241 NULL,
3242};
3243
d0dc8494
AK
3244static struct attribute *skl_format_attr[] = {
3245 &format_attr_frontend.attr,
3246 NULL,
3247};
3248
3b6e0421
JO
3249static __initconst const struct x86_pmu core_pmu = {
3250 .name = "core",
3251 .handle_irq = x86_pmu_handle_irq,
3252 .disable_all = x86_pmu_disable_all,
3253 .enable_all = core_pmu_enable_all,
3254 .enable = core_pmu_enable_event,
3255 .disable = x86_pmu_disable_event,
3256 .hw_config = x86_pmu_hw_config,
3257 .schedule_events = x86_schedule_events,
3258 .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
3259 .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
3260 .event_map = intel_pmu_event_map,
3261 .max_events = ARRAY_SIZE(intel_perfmon_event_map),
3262 .apic = 1,
a7b58d21
AK
3263 .free_running_flags = PEBS_FREERUNNING_FLAGS,
3264
3b6e0421
JO
3265 /*
3266 * Intel PMCs cannot be accessed sanely above 32-bit width,
3267 * so we install an artificial 1<<31 period regardless of
3268 * the generic event period:
3269 */
3270 .max_period = (1ULL<<31) - 1,
3271 .get_event_constraints = intel_get_event_constraints,
3272 .put_event_constraints = intel_put_event_constraints,
3273 .event_constraints = intel_core_event_constraints,
3274 .guest_get_msrs = core_guest_get_msrs,
3275 .format_attrs = intel_arch_formats_attr,
3276 .events_sysfs_show = intel_event_sysfs_show,
3277
3278 /*
3279 * Virtual (or funny metal) CPU can define x86_pmu.extra_regs
3280 * together with PMU version 1 and thus be using core_pmu with
3281 * shared_regs. We need following callbacks here to allocate
3282 * it properly.
3283 */
3284 .cpu_prepare = intel_pmu_cpu_prepare,
3285 .cpu_starting = intel_pmu_cpu_starting,
3286 .cpu_dying = intel_pmu_cpu_dying,
3287};
3288
caaa8be3 3289static __initconst const struct x86_pmu intel_pmu = {
f22f54f4
PZ
3290 .name = "Intel",
3291 .handle_irq = intel_pmu_handle_irq,
3292 .disable_all = intel_pmu_disable_all,
3293 .enable_all = intel_pmu_enable_all,
3294 .enable = intel_pmu_enable_event,
3295 .disable = intel_pmu_disable_event,
68f7082f
PZ
3296 .add = intel_pmu_add_event,
3297 .del = intel_pmu_del_event,
b4cdc5c2 3298 .hw_config = intel_pmu_hw_config,
a072738e 3299 .schedule_events = x86_schedule_events,
f22f54f4
PZ
3300 .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
3301 .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
3302 .event_map = intel_pmu_event_map,
f22f54f4
PZ
3303 .max_events = ARRAY_SIZE(intel_perfmon_event_map),
3304 .apic = 1,
a7b58d21 3305 .free_running_flags = PEBS_FREERUNNING_FLAGS,
f22f54f4
PZ
3306 /*
3307 * Intel PMCs cannot be accessed sanely above 32 bit width,
3308 * so we install an artificial 1<<31 period regardless of
3309 * the generic event period:
3310 */
3311 .max_period = (1ULL << 31) - 1,
3f6da390 3312 .get_event_constraints = intel_get_event_constraints,
a7e3ed1e 3313 .put_event_constraints = intel_put_event_constraints,
0780c927 3314 .pebs_aliases = intel_pebs_aliases_core2,
3f6da390 3315
641cc938 3316 .format_attrs = intel_arch3_formats_attr,
0bf79d44 3317 .events_sysfs_show = intel_event_sysfs_show,
641cc938 3318
a7e3ed1e 3319 .cpu_prepare = intel_pmu_cpu_prepare,
74846d35
PZ
3320 .cpu_starting = intel_pmu_cpu_starting,
3321 .cpu_dying = intel_pmu_cpu_dying,
144d31e6 3322 .guest_get_msrs = intel_guest_get_msrs,
9c964efa 3323 .sched_task = intel_pmu_sched_task,
f22f54f4
PZ
3324};
3325
c1d6f42f 3326static __init void intel_clovertown_quirk(void)
3c44780b
PZ
3327{
3328 /*
3329 * PEBS is unreliable due to:
3330 *
3331 * AJ67 - PEBS may experience CPL leaks
3332 * AJ68 - PEBS PMI may be delayed by one event
3333 * AJ69 - GLOBAL_STATUS[62] will only be set when DEBUGCTL[12]
3334 * AJ106 - FREEZE_LBRS_ON_PMI doesn't work in combination with PEBS
3335 *
3336 * AJ67 could be worked around by restricting the OS/USR flags.
3337 * AJ69 could be worked around by setting PMU_FREEZE_ON_PMI.
3338 *
3339 * AJ106 could possibly be worked around by not allowing LBR
3340 * usage from PEBS, including the fixup.
3341 * AJ68 could possibly be worked around by always programming
ec75a716 3342 * a pebs_event_reset[0] value and coping with the lost events.
3c44780b
PZ
3343 *
3344 * But taken together it might just make sense to not enable PEBS on
3345 * these chips.
3346 */
c767a54b 3347 pr_warn("PEBS disabled due to CPU errata\n");
3c44780b
PZ
3348 x86_pmu.pebs = 0;
3349 x86_pmu.pebs_constraints = NULL;
3350}
3351
c93dc84c
PZ
3352static int intel_snb_pebs_broken(int cpu)
3353{
3354 u32 rev = UINT_MAX; /* default to broken for unknown models */
3355
3356 switch (cpu_data(cpu).x86_model) {
ef5f9f47 3357 case INTEL_FAM6_SANDYBRIDGE:
c93dc84c
PZ
3358 rev = 0x28;
3359 break;
3360
ef5f9f47 3361 case INTEL_FAM6_SANDYBRIDGE_X:
c93dc84c
PZ
3362 switch (cpu_data(cpu).x86_mask) {
3363 case 6: rev = 0x618; break;
3364 case 7: rev = 0x70c; break;
3365 }
3366 }
3367
3368 return (cpu_data(cpu).microcode < rev);
3369}
3370
3371static void intel_snb_check_microcode(void)
3372{
3373 int pebs_broken = 0;
3374 int cpu;
3375
3376 get_online_cpus();
3377 for_each_online_cpu(cpu) {
3378 if ((pebs_broken = intel_snb_pebs_broken(cpu)))
3379 break;
3380 }
3381 put_online_cpus();
3382
3383 if (pebs_broken == x86_pmu.pebs_broken)
3384 return;
3385
3386 /*
3387 * Serialized by the microcode lock..
3388 */
3389 if (x86_pmu.pebs_broken) {
3390 pr_info("PEBS enabled due to microcode update\n");
3391 x86_pmu.pebs_broken = 0;
3392 } else {
3393 pr_info("PEBS disabled due to CPU errata, please upgrade microcode\n");
3394 x86_pmu.pebs_broken = 1;
3395 }
3396}
3397
19fc9ddd
DCC
3398static bool is_lbr_from(unsigned long msr)
3399{
3400 unsigned long lbr_from_nr = x86_pmu.lbr_from + x86_pmu.lbr_nr;
3401
3402 return x86_pmu.lbr_from <= msr && msr < lbr_from_nr;
3403}
3404
338b522c
KL
3405/*
3406 * Under certain circumstances, access certain MSR may cause #GP.
3407 * The function tests if the input MSR can be safely accessed.
3408 */
3409static bool check_msr(unsigned long msr, u64 mask)
3410{
3411 u64 val_old, val_new, val_tmp;
3412
3413 /*
3414 * Read the current value, change it and read it back to see if it
3415 * matches, this is needed to detect certain hardware emulators
3416 * (qemu/kvm) that don't trap on the MSR access and always return 0s.
3417 */
3418 if (rdmsrl_safe(msr, &val_old))
3419 return false;
3420
3421 /*
3422 * Only change the bits which can be updated by wrmsrl.
3423 */
3424 val_tmp = val_old ^ mask;
19fc9ddd
DCC
3425
3426 if (is_lbr_from(msr))
3427 val_tmp = lbr_from_signext_quirk_wr(val_tmp);
3428
338b522c
KL
3429 if (wrmsrl_safe(msr, val_tmp) ||
3430 rdmsrl_safe(msr, &val_new))
3431 return false;
3432
19fc9ddd
DCC
3433 /*
3434 * Quirk only affects validation in wrmsr(), so wrmsrl()'s value
3435 * should equal rdmsrl()'s even with the quirk.
3436 */
338b522c
KL
3437 if (val_new != val_tmp)
3438 return false;
3439
19fc9ddd
DCC
3440 if (is_lbr_from(msr))
3441 val_old = lbr_from_signext_quirk_wr(val_old);
3442
338b522c
KL
3443 /* Here it's sure that the MSR can be safely accessed.
3444 * Restore the old value and return.
3445 */
3446 wrmsrl(msr, val_old);
3447
3448 return true;
3449}
3450
c1d6f42f 3451static __init void intel_sandybridge_quirk(void)
6a600a8b 3452{
c93dc84c
PZ
3453 x86_pmu.check_microcode = intel_snb_check_microcode;
3454 intel_snb_check_microcode();
6a600a8b
PZ
3455}
3456
c1d6f42f
PZ
3457static const struct { int id; char *name; } intel_arch_events_map[] __initconst = {
3458 { PERF_COUNT_HW_CPU_CYCLES, "cpu cycles" },
3459 { PERF_COUNT_HW_INSTRUCTIONS, "instructions" },
3460 { PERF_COUNT_HW_BUS_CYCLES, "bus cycles" },
3461 { PERF_COUNT_HW_CACHE_REFERENCES, "cache references" },
3462 { PERF_COUNT_HW_CACHE_MISSES, "cache misses" },
3463 { PERF_COUNT_HW_BRANCH_INSTRUCTIONS, "branch instructions" },
3464 { PERF_COUNT_HW_BRANCH_MISSES, "branch misses" },
ffb871bc
GN
3465};
3466
c1d6f42f
PZ
3467static __init void intel_arch_events_quirk(void)
3468{
3469 int bit;
3470
3471 /* disable event that reported as not presend by cpuid */
3472 for_each_set_bit(bit, x86_pmu.events_mask, ARRAY_SIZE(intel_arch_events_map)) {
3473 intel_perfmon_event_map[intel_arch_events_map[bit].id] = 0;
c767a54b
JP
3474 pr_warn("CPUID marked event: \'%s\' unavailable\n",
3475 intel_arch_events_map[bit].name);
c1d6f42f
PZ
3476 }
3477}
3478
3479static __init void intel_nehalem_quirk(void)
3480{
3481 union cpuid10_ebx ebx;
3482
3483 ebx.full = x86_pmu.events_maskl;
3484 if (ebx.split.no_branch_misses_retired) {
3485 /*
3486 * Erratum AAJ80 detected, we work it around by using
3487 * the BR_MISP_EXEC.ANY event. This will over-count
3488 * branch-misses, but it's still much better than the
3489 * architectural event which is often completely bogus:
3490 */
3491 intel_perfmon_event_map[PERF_COUNT_HW_BRANCH_MISSES] = 0x7f89;
3492 ebx.split.no_branch_misses_retired = 0;
3493 x86_pmu.events_maskl = ebx.full;
c767a54b 3494 pr_info("CPU erratum AAJ80 worked around\n");
c1d6f42f
PZ
3495 }
3496}
3497
93fcf72c
MD
3498/*
3499 * enable software workaround for errata:
3500 * SNB: BJ122
3501 * IVB: BV98
3502 * HSW: HSD29
3503 *
3504 * Only needed when HT is enabled. However detecting
b37609c3
SE
3505 * if HT is enabled is difficult (model specific). So instead,
3506 * we enable the workaround in the early boot, and verify if
3507 * it is needed in a later initcall phase once we have valid
3508 * topology information to check if HT is actually enabled
93fcf72c
MD
3509 */
3510static __init void intel_ht_bug(void)
3511{
b37609c3 3512 x86_pmu.flags |= PMU_FL_EXCL_CNTRS | PMU_FL_EXCL_ENABLED;
93fcf72c 3513
93fcf72c 3514 x86_pmu.start_scheduling = intel_start_scheduling;
0c41e756 3515 x86_pmu.commit_scheduling = intel_commit_scheduling;
93fcf72c 3516 x86_pmu.stop_scheduling = intel_stop_scheduling;
93fcf72c
MD
3517}
3518
7f2ee91f
IM
3519EVENT_ATTR_STR(mem-loads, mem_ld_hsw, "event=0xcd,umask=0x1,ldlat=3");
3520EVENT_ATTR_STR(mem-stores, mem_st_hsw, "event=0xd0,umask=0x82")
f9134f36 3521
4b2c4f1f 3522/* Haswell special events */
7f2ee91f
IM
3523EVENT_ATTR_STR(tx-start, tx_start, "event=0xc9,umask=0x1");
3524EVENT_ATTR_STR(tx-commit, tx_commit, "event=0xc9,umask=0x2");
3525EVENT_ATTR_STR(tx-abort, tx_abort, "event=0xc9,umask=0x4");
3526EVENT_ATTR_STR(tx-capacity, tx_capacity, "event=0x54,umask=0x2");
3527EVENT_ATTR_STR(tx-conflict, tx_conflict, "event=0x54,umask=0x1");
3528EVENT_ATTR_STR(el-start, el_start, "event=0xc8,umask=0x1");
3529EVENT_ATTR_STR(el-commit, el_commit, "event=0xc8,umask=0x2");
3530EVENT_ATTR_STR(el-abort, el_abort, "event=0xc8,umask=0x4");
3531EVENT_ATTR_STR(el-capacity, el_capacity, "event=0x54,umask=0x2");
3532EVENT_ATTR_STR(el-conflict, el_conflict, "event=0x54,umask=0x1");
3533EVENT_ATTR_STR(cycles-t, cycles_t, "event=0x3c,in_tx=1");
3534EVENT_ATTR_STR(cycles-ct, cycles_ct, "event=0x3c,in_tx=1,in_tx_cp=1");
4b2c4f1f 3535
f9134f36 3536static struct attribute *hsw_events_attrs[] = {
4b2c4f1f
AK
3537 EVENT_PTR(tx_start),
3538 EVENT_PTR(tx_commit),
3539 EVENT_PTR(tx_abort),
3540 EVENT_PTR(tx_capacity),
3541 EVENT_PTR(tx_conflict),
3542 EVENT_PTR(el_start),
3543 EVENT_PTR(el_commit),
3544 EVENT_PTR(el_abort),
3545 EVENT_PTR(el_capacity),
3546 EVENT_PTR(el_conflict),
3547 EVENT_PTR(cycles_t),
3548 EVENT_PTR(cycles_ct),
f9134f36
AK
3549 EVENT_PTR(mem_ld_hsw),
3550 EVENT_PTR(mem_st_hsw),
a39fcae7
AK
3551 EVENT_PTR(td_slots_issued),
3552 EVENT_PTR(td_slots_retired),
3553 EVENT_PTR(td_fetch_bubbles),
3554 EVENT_PTR(td_total_slots),
3555 EVENT_PTR(td_total_slots_scale),
3556 EVENT_PTR(td_recovery_bubbles),
3557 EVENT_PTR(td_recovery_bubbles_scale),
f9134f36
AK
3558 NULL
3559};
3560
de0428a7 3561__init int intel_pmu_init(void)
f22f54f4
PZ
3562{
3563 union cpuid10_edx edx;
3564 union cpuid10_eax eax;
ffb871bc 3565 union cpuid10_ebx ebx;
a1eac7ac 3566 struct event_constraint *c;
f22f54f4 3567 unsigned int unused;
338b522c
KL
3568 struct extra_reg *er;
3569 int version, i;
f22f54f4
PZ
3570
3571 if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
a072738e
CG
3572 switch (boot_cpu_data.x86) {
3573 case 0x6:
3574 return p6_pmu_init();
e717bf4e
VW
3575 case 0xb:
3576 return knc_pmu_init();
a072738e
CG
3577 case 0xf:
3578 return p4_pmu_init();
3579 }
f22f54f4 3580 return -ENODEV;
f22f54f4
PZ
3581 }
3582
3583 /*
3584 * Check whether the Architectural PerfMon supports
3585 * Branch Misses Retired hw_event or not.
3586 */
ffb871bc
GN
3587 cpuid(10, &eax.full, &ebx.full, &unused, &edx.full);
3588 if (eax.split.mask_length < ARCH_PERFMON_EVENTS_COUNT)
f22f54f4
PZ
3589 return -ENODEV;
3590
3591 version = eax.split.version_id;
3592 if (version < 2)
3593 x86_pmu = core_pmu;
3594 else
3595 x86_pmu = intel_pmu;
3596
3597 x86_pmu.version = version;
948b1bb8
RR
3598 x86_pmu.num_counters = eax.split.num_counters;
3599 x86_pmu.cntval_bits = eax.split.bit_width;
3600 x86_pmu.cntval_mask = (1ULL << eax.split.bit_width) - 1;
f22f54f4 3601
c1d6f42f
PZ
3602 x86_pmu.events_maskl = ebx.full;
3603 x86_pmu.events_mask_len = eax.split.mask_length;
3604
70ab7003
AK
3605 x86_pmu.max_pebs_events = min_t(unsigned, MAX_PEBS_EVENTS, x86_pmu.num_counters);
3606
f22f54f4
PZ
3607 /*
3608 * Quirk: v2 perfmon does not report fixed-purpose events, so
3609 * assume at least 3 events:
3610 */
3611 if (version > 1)
948b1bb8 3612 x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3);
f22f54f4 3613
c9b08884 3614 if (boot_cpu_has(X86_FEATURE_PDCM)) {
8db909a7
PZ
3615 u64 capabilities;
3616
3617 rdmsrl(MSR_IA32_PERF_CAPABILITIES, capabilities);
3618 x86_pmu.intel_cap.capabilities = capabilities;
3619 }
3620
ca037701
PZ
3621 intel_ds_init();
3622
c1d6f42f
PZ
3623 x86_add_quirk(intel_arch_events_quirk); /* Install first, so it runs last */
3624
f22f54f4
PZ
3625 /*
3626 * Install the hw-cache-events table:
3627 */
3628 switch (boot_cpu_data.x86_model) {
ef5f9f47 3629 case INTEL_FAM6_CORE_YONAH:
f22f54f4
PZ
3630 pr_cont("Core events, ");
3631 break;
3632
ef5f9f47 3633 case INTEL_FAM6_CORE2_MEROM:
c1d6f42f 3634 x86_add_quirk(intel_clovertown_quirk);
ef5f9f47
DH
3635 case INTEL_FAM6_CORE2_MEROM_L:
3636 case INTEL_FAM6_CORE2_PENRYN:
3637 case INTEL_FAM6_CORE2_DUNNINGTON:
f22f54f4
PZ
3638 memcpy(hw_cache_event_ids, core2_hw_cache_event_ids,
3639 sizeof(hw_cache_event_ids));
3640
caff2bef
PZ
3641 intel_pmu_lbr_init_core();
3642
f22f54f4 3643 x86_pmu.event_constraints = intel_core2_event_constraints;
17e31629 3644 x86_pmu.pebs_constraints = intel_core2_pebs_event_constraints;
f22f54f4
PZ
3645 pr_cont("Core2 events, ");
3646 break;
3647
ef5f9f47
DH
3648 case INTEL_FAM6_NEHALEM:
3649 case INTEL_FAM6_NEHALEM_EP:
3650 case INTEL_FAM6_NEHALEM_EX:
f22f54f4
PZ
3651 memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids,
3652 sizeof(hw_cache_event_ids));
e994d7d2
AK
3653 memcpy(hw_cache_extra_regs, nehalem_hw_cache_extra_regs,
3654 sizeof(hw_cache_extra_regs));
f22f54f4 3655
caff2bef
PZ
3656 intel_pmu_lbr_init_nhm();
3657
f22f54f4 3658 x86_pmu.event_constraints = intel_nehalem_event_constraints;
17e31629 3659 x86_pmu.pebs_constraints = intel_nehalem_pebs_event_constraints;
11164cd4 3660 x86_pmu.enable_all = intel_pmu_nhm_enable_all;
a7e3ed1e 3661 x86_pmu.extra_regs = intel_nehalem_extra_regs;
ec75a716 3662
f20093ee
SE
3663 x86_pmu.cpu_events = nhm_events_attrs;
3664
91fc4cc0 3665 /* UOPS_ISSUED.STALLED_CYCLES */
f9b4eeb8
PZ
3666 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
3667 X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
91fc4cc0 3668 /* UOPS_EXECUTED.CORE_ACTIVE_CYCLES,c=1,i=1 */
f9b4eeb8
PZ
3669 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
3670 X86_CONFIG(.event=0xb1, .umask=0x3f, .inv=1, .cmask=1);
94403f88 3671
e17dc653 3672 intel_pmu_pebs_data_source_nhm();
c1d6f42f 3673 x86_add_quirk(intel_nehalem_quirk);
ec75a716 3674
11164cd4 3675 pr_cont("Nehalem events, ");
f22f54f4 3676 break;
caff2bef 3677
ef5f9f47
DH
3678 case INTEL_FAM6_ATOM_PINEVIEW:
3679 case INTEL_FAM6_ATOM_LINCROFT:
3680 case INTEL_FAM6_ATOM_PENWELL:
3681 case INTEL_FAM6_ATOM_CLOVERVIEW:
3682 case INTEL_FAM6_ATOM_CEDARVIEW:
f22f54f4
PZ
3683 memcpy(hw_cache_event_ids, atom_hw_cache_event_ids,
3684 sizeof(hw_cache_event_ids));
3685
caff2bef
PZ
3686 intel_pmu_lbr_init_atom();
3687
f22f54f4 3688 x86_pmu.event_constraints = intel_gen_event_constraints;
17e31629 3689 x86_pmu.pebs_constraints = intel_atom_pebs_event_constraints;
673d188b 3690 x86_pmu.pebs_aliases = intel_pebs_aliases_core2;
f22f54f4
PZ
3691 pr_cont("Atom events, ");
3692 break;
3693
ef5f9f47
DH
3694 case INTEL_FAM6_ATOM_SILVERMONT1:
3695 case INTEL_FAM6_ATOM_SILVERMONT2:
3696 case INTEL_FAM6_ATOM_AIRMONT:
1fa64180
YZ
3697 memcpy(hw_cache_event_ids, slm_hw_cache_event_ids,
3698 sizeof(hw_cache_event_ids));
3699 memcpy(hw_cache_extra_regs, slm_hw_cache_extra_regs,
3700 sizeof(hw_cache_extra_regs));
3701
f21d5adc 3702 intel_pmu_lbr_init_slm();
1fa64180
YZ
3703
3704 x86_pmu.event_constraints = intel_slm_event_constraints;
3705 x86_pmu.pebs_constraints = intel_slm_pebs_event_constraints;
3706 x86_pmu.extra_regs = intel_slm_extra_regs;
9a5e3fb5 3707 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
eb12b8ec 3708 x86_pmu.cpu_events = slm_events_attrs;
1fa64180
YZ
3709 pr_cont("Silvermont events, ");
3710 break;
3711
ef5f9f47
DH
3712 case INTEL_FAM6_ATOM_GOLDMONT:
3713 case INTEL_FAM6_ATOM_DENVERTON:
8b92c3a7
KL
3714 memcpy(hw_cache_event_ids, glm_hw_cache_event_ids,
3715 sizeof(hw_cache_event_ids));
3716 memcpy(hw_cache_extra_regs, glm_hw_cache_extra_regs,
3717 sizeof(hw_cache_extra_regs));
3718
3719 intel_pmu_lbr_init_skl();
3720
3721 x86_pmu.event_constraints = intel_slm_event_constraints;
3722 x86_pmu.pebs_constraints = intel_glm_pebs_event_constraints;
3723 x86_pmu.extra_regs = intel_glm_extra_regs;
3724 /*
3725 * It's recommended to use CPU_CLK_UNHALTED.CORE_P + NPEBS
3726 * for precise cycles.
3727 * :pp is identical to :ppp
3728 */
3729 x86_pmu.pebs_aliases = NULL;
3730 x86_pmu.pebs_prec_dist = true;
ccbebba4 3731 x86_pmu.lbr_pt_coexist = true;
8b92c3a7
KL
3732 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
3733 pr_cont("Goldmont events, ");
3734 break;
3735
ef5f9f47
DH
3736 case INTEL_FAM6_WESTMERE:
3737 case INTEL_FAM6_WESTMERE_EP:
3738 case INTEL_FAM6_WESTMERE_EX:
f22f54f4
PZ
3739 memcpy(hw_cache_event_ids, westmere_hw_cache_event_ids,
3740 sizeof(hw_cache_event_ids));
e994d7d2
AK
3741 memcpy(hw_cache_extra_regs, nehalem_hw_cache_extra_regs,
3742 sizeof(hw_cache_extra_regs));
f22f54f4 3743
caff2bef
PZ
3744 intel_pmu_lbr_init_nhm();
3745
f22f54f4 3746 x86_pmu.event_constraints = intel_westmere_event_constraints;
40b91cd1 3747 x86_pmu.enable_all = intel_pmu_nhm_enable_all;
17e31629 3748 x86_pmu.pebs_constraints = intel_westmere_pebs_event_constraints;
a7e3ed1e 3749 x86_pmu.extra_regs = intel_westmere_extra_regs;
9a5e3fb5 3750 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
30112039 3751
f20093ee
SE
3752 x86_pmu.cpu_events = nhm_events_attrs;
3753
30112039 3754 /* UOPS_ISSUED.STALLED_CYCLES */
f9b4eeb8
PZ
3755 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
3756 X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
30112039 3757 /* UOPS_EXECUTED.CORE_ACTIVE_CYCLES,c=1,i=1 */
f9b4eeb8
PZ
3758 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
3759 X86_CONFIG(.event=0xb1, .umask=0x3f, .inv=1, .cmask=1);
30112039 3760
e17dc653 3761 intel_pmu_pebs_data_source_nhm();
f22f54f4
PZ
3762 pr_cont("Westmere events, ");
3763 break;
b622d644 3764
ef5f9f47
DH
3765 case INTEL_FAM6_SANDYBRIDGE:
3766 case INTEL_FAM6_SANDYBRIDGE_X:
47a8863d 3767 x86_add_quirk(intel_sandybridge_quirk);
93fcf72c 3768 x86_add_quirk(intel_ht_bug);
b06b3d49
LM
3769 memcpy(hw_cache_event_ids, snb_hw_cache_event_ids,
3770 sizeof(hw_cache_event_ids));
74e6543f
YZ
3771 memcpy(hw_cache_extra_regs, snb_hw_cache_extra_regs,
3772 sizeof(hw_cache_extra_regs));
b06b3d49 3773
c5cc2cd9 3774 intel_pmu_lbr_init_snb();
b06b3d49
LM
3775
3776 x86_pmu.event_constraints = intel_snb_event_constraints;
de0428a7 3777 x86_pmu.pebs_constraints = intel_snb_pebs_event_constraints;
0780c927 3778 x86_pmu.pebs_aliases = intel_pebs_aliases_snb;
ef5f9f47 3779 if (boot_cpu_data.x86_model == INTEL_FAM6_SANDYBRIDGE_X)
f1923820
SE
3780 x86_pmu.extra_regs = intel_snbep_extra_regs;
3781 else
3782 x86_pmu.extra_regs = intel_snb_extra_regs;
93fcf72c
MD
3783
3784
ee89cbc2 3785 /* all extra regs are per-cpu when HT is on */
9a5e3fb5
SE
3786 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
3787 x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
e04d1b23 3788
f20093ee
SE
3789 x86_pmu.cpu_events = snb_events_attrs;
3790
e04d1b23 3791 /* UOPS_ISSUED.ANY,c=1,i=1 to count stall cycles */
f9b4eeb8
PZ
3792 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
3793 X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
e04d1b23 3794 /* UOPS_DISPATCHED.THREAD,c=1,i=1 to count stall cycles*/
f9b4eeb8
PZ
3795 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
3796 X86_CONFIG(.event=0xb1, .umask=0x01, .inv=1, .cmask=1);
e04d1b23 3797
b06b3d49
LM
3798 pr_cont("SandyBridge events, ");
3799 break;
0f7c29ce 3800
ef5f9f47
DH
3801 case INTEL_FAM6_IVYBRIDGE:
3802 case INTEL_FAM6_IVYBRIDGE_X:
93fcf72c 3803 x86_add_quirk(intel_ht_bug);
20a36e39
SE
3804 memcpy(hw_cache_event_ids, snb_hw_cache_event_ids,
3805 sizeof(hw_cache_event_ids));
1996388e
VW
3806 /* dTLB-load-misses on IVB is different than SNB */
3807 hw_cache_event_ids[C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = 0x8108; /* DTLB_LOAD_MISSES.DEMAND_LD_MISS_CAUSES_A_WALK */
3808
20a36e39
SE
3809 memcpy(hw_cache_extra_regs, snb_hw_cache_extra_regs,
3810 sizeof(hw_cache_extra_regs));
3811
3812 intel_pmu_lbr_init_snb();
3813
69943182 3814 x86_pmu.event_constraints = intel_ivb_event_constraints;
20a36e39 3815 x86_pmu.pebs_constraints = intel_ivb_pebs_event_constraints;
72469764
AK
3816 x86_pmu.pebs_aliases = intel_pebs_aliases_ivb;
3817 x86_pmu.pebs_prec_dist = true;
ef5f9f47 3818 if (boot_cpu_data.x86_model == INTEL_FAM6_IVYBRIDGE_X)
f1923820
SE
3819 x86_pmu.extra_regs = intel_snbep_extra_regs;
3820 else
3821 x86_pmu.extra_regs = intel_snb_extra_regs;
20a36e39 3822 /* all extra regs are per-cpu when HT is on */
9a5e3fb5
SE
3823 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
3824 x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
20a36e39 3825
f20093ee
SE
3826 x86_pmu.cpu_events = snb_events_attrs;
3827
20a36e39
SE
3828 /* UOPS_ISSUED.ANY,c=1,i=1 to count stall cycles */
3829 intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
3830 X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
3831
3832 pr_cont("IvyBridge events, ");
3833 break;
3834
b06b3d49 3835
ef5f9f47
DH
3836 case INTEL_FAM6_HASWELL_CORE:
3837 case INTEL_FAM6_HASWELL_X:
3838 case INTEL_FAM6_HASWELL_ULT:
3839 case INTEL_FAM6_HASWELL_GT3E:
93fcf72c 3840 x86_add_quirk(intel_ht_bug);
72db5596 3841 x86_pmu.late_ack = true;
0f1b5ca2
AK
3842 memcpy(hw_cache_event_ids, hsw_hw_cache_event_ids, sizeof(hw_cache_event_ids));
3843 memcpy(hw_cache_extra_regs, hsw_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
3a632cb2 3844
e9d7f7cd 3845 intel_pmu_lbr_init_hsw();
3a632cb2
AK
3846
3847 x86_pmu.event_constraints = intel_hsw_event_constraints;
3044318f 3848 x86_pmu.pebs_constraints = intel_hsw_pebs_event_constraints;
36bbb2f2 3849 x86_pmu.extra_regs = intel_snbep_extra_regs;
72469764
AK
3850 x86_pmu.pebs_aliases = intel_pebs_aliases_ivb;
3851 x86_pmu.pebs_prec_dist = true;
3a632cb2 3852 /* all extra regs are per-cpu when HT is on */
9a5e3fb5
SE
3853 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
3854 x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
3a632cb2
AK
3855
3856 x86_pmu.hw_config = hsw_hw_config;
3857 x86_pmu.get_event_constraints = hsw_get_event_constraints;
f9134f36 3858 x86_pmu.cpu_events = hsw_events_attrs;
b7af41a1 3859 x86_pmu.lbr_double_abort = true;
3a632cb2
AK
3860 pr_cont("Haswell events, ");
3861 break;
3862
ef5f9f47
DH
3863 case INTEL_FAM6_BROADWELL_CORE:
3864 case INTEL_FAM6_BROADWELL_XEON_D:
3865 case INTEL_FAM6_BROADWELL_GT3E:
3866 case INTEL_FAM6_BROADWELL_X:
91f1b705
AK
3867 x86_pmu.late_ack = true;
3868 memcpy(hw_cache_event_ids, hsw_hw_cache_event_ids, sizeof(hw_cache_event_ids));
3869 memcpy(hw_cache_extra_regs, hsw_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
3870
3871 /* L3_MISS_LOCAL_DRAM is BIT(26) in Broadwell */
3872 hw_cache_extra_regs[C(LL)][C(OP_READ)][C(RESULT_MISS)] = HSW_DEMAND_READ |
3873 BDW_L3_MISS|HSW_SNOOP_DRAM;
3874 hw_cache_extra_regs[C(LL)][C(OP_WRITE)][C(RESULT_MISS)] = HSW_DEMAND_WRITE|BDW_L3_MISS|
3875 HSW_SNOOP_DRAM;
3876 hw_cache_extra_regs[C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = HSW_DEMAND_READ|
3877 BDW_L3_MISS_LOCAL|HSW_SNOOP_DRAM;
3878 hw_cache_extra_regs[C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = HSW_DEMAND_WRITE|
3879 BDW_L3_MISS_LOCAL|HSW_SNOOP_DRAM;
3880
78d504bc 3881 intel_pmu_lbr_init_hsw();
91f1b705
AK
3882
3883 x86_pmu.event_constraints = intel_bdw_event_constraints;
b3e62463 3884 x86_pmu.pebs_constraints = intel_bdw_pebs_event_constraints;
91f1b705 3885 x86_pmu.extra_regs = intel_snbep_extra_regs;
72469764
AK
3886 x86_pmu.pebs_aliases = intel_pebs_aliases_ivb;
3887 x86_pmu.pebs_prec_dist = true;
91f1b705 3888 /* all extra regs are per-cpu when HT is on */
9a5e3fb5
SE
3889 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
3890 x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
91f1b705
AK
3891
3892 x86_pmu.hw_config = hsw_hw_config;
3893 x86_pmu.get_event_constraints = hsw_get_event_constraints;
3894 x86_pmu.cpu_events = hsw_events_attrs;
294fe0f5 3895 x86_pmu.limit_period = bdw_limit_period;
91f1b705
AK
3896 pr_cont("Broadwell events, ");
3897 break;
3898
ef5f9f47 3899 case INTEL_FAM6_XEON_PHI_KNL:
1e7b9390
HC
3900 memcpy(hw_cache_event_ids,
3901 slm_hw_cache_event_ids, sizeof(hw_cache_event_ids));
3902 memcpy(hw_cache_extra_regs,
3903 knl_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
3904 intel_pmu_lbr_init_knl();
3905
3906 x86_pmu.event_constraints = intel_slm_event_constraints;
3907 x86_pmu.pebs_constraints = intel_slm_pebs_event_constraints;
3908 x86_pmu.extra_regs = intel_knl_extra_regs;
3909
3910 /* all extra regs are per-cpu when HT is on */
3911 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
3912 x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
3913
3914 pr_cont("Knights Landing events, ");
3915 break;
3916
ef5f9f47
DH
3917 case INTEL_FAM6_SKYLAKE_MOBILE:
3918 case INTEL_FAM6_SKYLAKE_DESKTOP:
3919 case INTEL_FAM6_SKYLAKE_X:
3920 case INTEL_FAM6_KABYLAKE_MOBILE:
3921 case INTEL_FAM6_KABYLAKE_DESKTOP:
9a92e16f
AK
3922 x86_pmu.late_ack = true;
3923 memcpy(hw_cache_event_ids, skl_hw_cache_event_ids, sizeof(hw_cache_event_ids));
3924 memcpy(hw_cache_extra_regs, skl_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
3925 intel_pmu_lbr_init_skl();
3926
a39fcae7
AK
3927 /* INT_MISC.RECOVERY_CYCLES has umask 1 in Skylake */
3928 event_attr_td_recovery_bubbles.event_str_noht =
3929 "event=0xd,umask=0x1,cmask=1";
3930 event_attr_td_recovery_bubbles.event_str_ht =
3931 "event=0xd,umask=0x1,cmask=1,any=1";
3932
9a92e16f
AK
3933 x86_pmu.event_constraints = intel_skl_event_constraints;
3934 x86_pmu.pebs_constraints = intel_skl_pebs_event_constraints;
3935 x86_pmu.extra_regs = intel_skl_extra_regs;
72469764
AK
3936 x86_pmu.pebs_aliases = intel_pebs_aliases_skl;
3937 x86_pmu.pebs_prec_dist = true;
9a92e16f
AK
3938 /* all extra regs are per-cpu when HT is on */
3939 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
3940 x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
3941
3942 x86_pmu.hw_config = hsw_hw_config;
3943 x86_pmu.get_event_constraints = hsw_get_event_constraints;
d0dc8494
AK
3944 x86_pmu.format_attrs = merge_attr(intel_arch3_formats_attr,
3945 skl_format_attr);
9a92e16f
AK
3946 WARN_ON(!x86_pmu.format_attrs);
3947 x86_pmu.cpu_events = hsw_events_attrs;
3948 pr_cont("Skylake events, ");
3949 break;
3950
f22f54f4 3951 default:
0af3ac1f
AK
3952 switch (x86_pmu.version) {
3953 case 1:
3954 x86_pmu.event_constraints = intel_v1_event_constraints;
3955 pr_cont("generic architected perfmon v1, ");
3956 break;
3957 default:
3958 /*
3959 * default constraints for v2 and up
3960 */
3961 x86_pmu.event_constraints = intel_gen_event_constraints;
3962 pr_cont("generic architected perfmon, ");
3963 break;
3964 }
f22f54f4 3965 }
ffb871bc 3966
a1eac7ac
RR
3967 if (x86_pmu.num_counters > INTEL_PMC_MAX_GENERIC) {
3968 WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
3969 x86_pmu.num_counters, INTEL_PMC_MAX_GENERIC);
3970 x86_pmu.num_counters = INTEL_PMC_MAX_GENERIC;
3971 }
3972 x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
3973
3974 if (x86_pmu.num_counters_fixed > INTEL_PMC_MAX_FIXED) {
3975 WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
3976 x86_pmu.num_counters_fixed, INTEL_PMC_MAX_FIXED);
3977 x86_pmu.num_counters_fixed = INTEL_PMC_MAX_FIXED;
3978 }
3979
3980 x86_pmu.intel_ctrl |=
3981 ((1LL << x86_pmu.num_counters_fixed)-1) << INTEL_PMC_IDX_FIXED;
3982
3983 if (x86_pmu.event_constraints) {
3984 /*
3985 * event on fixed counter2 (REF_CYCLES) only works on this
3986 * counter, so do not extend mask to generic counters
3987 */
3988 for_each_event_constraint(c, x86_pmu.event_constraints) {
2c33645d
PI
3989 if (c->cmask == FIXED_EVENT_FLAGS
3990 && c->idxmsk64 != INTEL_PMC_MSK_FIXED_REF_CYCLES) {
3991 c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1;
a1eac7ac 3992 }
2c33645d 3993 c->idxmsk64 &=
6d6f2833 3994 ~(~0ULL << (INTEL_PMC_IDX_FIXED + x86_pmu.num_counters_fixed));
2c33645d 3995 c->weight = hweight64(c->idxmsk64);
a1eac7ac
RR
3996 }
3997 }
3998
338b522c
KL
3999 /*
4000 * Access LBR MSR may cause #GP under certain circumstances.
4001 * E.g. KVM doesn't support LBR MSR
4002 * Check all LBT MSR here.
4003 * Disable LBR access if any LBR MSRs can not be accessed.
4004 */
4005 if (x86_pmu.lbr_nr && !check_msr(x86_pmu.lbr_tos, 0x3UL))
4006 x86_pmu.lbr_nr = 0;
4007 for (i = 0; i < x86_pmu.lbr_nr; i++) {
4008 if (!(check_msr(x86_pmu.lbr_from + i, 0xffffUL) &&
4009 check_msr(x86_pmu.lbr_to + i, 0xffffUL)))
4010 x86_pmu.lbr_nr = 0;
4011 }
4012
f09509b9
DCC
4013 if (x86_pmu.lbr_nr)
4014 pr_cont("%d-deep LBR, ", x86_pmu.lbr_nr);
338b522c
KL
4015 /*
4016 * Access extra MSR may cause #GP under certain circumstances.
4017 * E.g. KVM doesn't support offcore event
4018 * Check all extra_regs here.
4019 */
4020 if (x86_pmu.extra_regs) {
4021 for (er = x86_pmu.extra_regs; er->msr; er++) {
8c4fe709 4022 er->extra_msr_access = check_msr(er->msr, 0x11UL);
338b522c
KL
4023 /* Disable LBR select mapping */
4024 if ((er->idx == EXTRA_REG_LBR) && !er->extra_msr_access)
4025 x86_pmu.lbr_sel_map = NULL;
4026 }
4027 }
4028
069e0c3c
AK
4029 /* Support full width counters using alternative MSR range */
4030 if (x86_pmu.intel_cap.full_width_write) {
4031 x86_pmu.max_period = x86_pmu.cntval_mask;
4032 x86_pmu.perfctr = MSR_IA32_PMC0;
4033 pr_cont("full-width counters, ");
4034 }
4035
f22f54f4
PZ
4036 return 0;
4037}
b37609c3
SE
4038
4039/*
4040 * HT bug: phase 2 init
4041 * Called once we have valid topology information to check
4042 * whether or not HT is enabled
4043 * If HT is off, then we disable the workaround
4044 */
4045static __init int fixup_ht_bug(void)
4046{
030ba6cd 4047 int c;
b37609c3
SE
4048 /*
4049 * problem not present on this CPU model, nothing to do
4050 */
4051 if (!(x86_pmu.flags & PMU_FL_EXCL_ENABLED))
4052 return 0;
4053
030ba6cd 4054 if (topology_max_smt_threads() > 1) {
b37609c3
SE
4055 pr_info("PMU erratum BJ122, BV98, HSD29 worked around, HT is on\n");
4056 return 0;
4057 }
4058
ec6a9066 4059 if (lockup_detector_suspend() != 0) {
999bbe49
UO
4060 pr_debug("failed to disable PMU erratum BJ122, BV98, HSD29 workaround\n");
4061 return 0;
4062 }
b37609c3
SE
4063
4064 x86_pmu.flags &= ~(PMU_FL_EXCL_CNTRS | PMU_FL_EXCL_ENABLED);
4065
b37609c3 4066 x86_pmu.start_scheduling = NULL;
0c41e756 4067 x86_pmu.commit_scheduling = NULL;
b37609c3
SE
4068 x86_pmu.stop_scheduling = NULL;
4069
ec6a9066 4070 lockup_detector_resume();
b37609c3
SE
4071
4072 get_online_cpus();
4073
4074 for_each_online_cpu(c) {
4075 free_excl_cntrs(c);
4076 }
4077
4078 put_online_cpus();
4079 pr_info("PMU erratum BJ122, BV98, HSD29 workaround disabled, HT off\n");
4080 return 0;
4081}
4082subsys_initcall(fixup_ht_bug)
This page took 0.507008 seconds and 5 git commands to generate.